feat(openclaw): automate nightly multi-agent session compaction and document incident
This commit is contained in:
Executable
+87
@@ -0,0 +1,87 @@
|
||||
#!/usr/bin/env bash
|
||||
# ==============================================================================
|
||||
# setup-compaction-cron.sh
|
||||
# Declaratively registers the Multi-Agent Nightly Session Compaction in OpenClaw.
|
||||
# Schedule: Every night at 03:30 Europe/Berlin (cron: 30 3 * * *)
|
||||
# ==============================================================================
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
NAMESPACE="${NAMESPACE:-openclaw}"
|
||||
POD_NAME="${POD_NAME:-openclaw-0}"
|
||||
CONTAINER_NAME="${CONTAINER_NAME:-openclaw}"
|
||||
|
||||
JOB_NAME="nightly-session-compaction"
|
||||
CRON_SCHEDULE="30 3 * * *"
|
||||
TIMEZONE="Europe/Berlin"
|
||||
|
||||
echo "=== Registering Multi-Agent Nightly Session Compaction in OpenClaw ($POD_NAME) ==="
|
||||
|
||||
# 1. Install or ensure python compaction script is present on PVC
|
||||
echo "Ensuring /home/node/.openclaw/scripts/compact_sessions.py exists..."
|
||||
kubectl exec -i -n "$NAMESPACE" "$POD_NAME" -c "$CONTAINER_NAME" -- /bin/sh -c 'cat << "EOF" > /home/node/.openclaw/scripts/compact_sessions.py
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Nightly multi-agent session compaction script for OpenClaw.
|
||||
Compacts all active forum topics and main sessions exceeding the token threshold.
|
||||
"""
|
||||
import subprocess
|
||||
import json
|
||||
import sys
|
||||
|
||||
THRESHOLD_TOKENS = 25000
|
||||
MAX_LINES = 250
|
||||
|
||||
def main():
|
||||
print(f"=== Running OpenClaw Nightly Multi-Agent Compaction (threshold: {THRESHOLD_TOKENS} tokens, maxLines: {MAX_LINES}) ===")
|
||||
res = subprocess.run(["openclaw", "sessions", "--all-agents", "--json"], capture_output=True, text=True)
|
||||
if res.returncode != 0:
|
||||
print(f"Failed to list sessions: {res.stderr}")
|
||||
sys.exit(1)
|
||||
|
||||
sessions = json.loads(res.stdout).get("sessions", [])
|
||||
compacted = 0
|
||||
skipped = 0
|
||||
|
||||
for s in sessions:
|
||||
key = s.get("key", "")
|
||||
agent = s.get("agentId") or "main"
|
||||
tokens = s.get("totalTokens") or 0
|
||||
|
||||
# Target main session and all telegram group topic sessions
|
||||
if (":telegram:group" in key or key == "agent:main:main") and not key.endswith(":heartbeat"):
|
||||
if tokens >= THRESHOLD_TOKENS:
|
||||
print(f"Compacting {agent} ({tokens} tokens) -> {key}...")
|
||||
c_res = subprocess.run(
|
||||
["openclaw", "sessions", "compact", key, "--agent", agent, "--max-lines", str(MAX_LINES)],
|
||||
capture_output=True, text=True
|
||||
)
|
||||
if c_res.returncode == 0:
|
||||
print(f" ✓ {c_res.stdout.strip()}")
|
||||
compacted += 1
|
||||
else:
|
||||
print(f" ✗ {c_res.stderr.strip()}")
|
||||
else:
|
||||
skipped += 1
|
||||
|
||||
print(f"Compaction complete: {compacted} compacted, {skipped} skipped.")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
EOF
|
||||
chmod +x /home/node/.openclaw/scripts/compact_sessions.py
|
||||
'
|
||||
|
||||
# 2. Register / Update the OpenClaw cron job
|
||||
echo "Registering OpenClaw Cron automation '$JOB_NAME' ($CRON_SCHEDULE @ $TIMEZONE)..."
|
||||
kubectl exec -n "$NAMESPACE" "$POD_NAME" -c "$CONTAINER_NAME" -- openclaw cron add \
|
||||
--name "Nightly Session Compaction" \
|
||||
--declaration-key "$JOB_NAME" \
|
||||
--description "Nightly multi-agent session compaction for all active topics and main sessions" \
|
||||
--cron "$CRON_SCHEDULE" \
|
||||
--tz "$TIMEZONE" \
|
||||
--session "isolated" \
|
||||
--command "python3 /home/node/.openclaw/scripts/compact_sessions.py"
|
||||
|
||||
echo "=== Compaction Cron automation successfully registered! ==="
|
||||
kubectl exec -n "$NAMESPACE" "$POD_NAME" -c "$CONTAINER_NAME" -- openclaw cron show "$JOB_NAME" 2>/dev/null || true
|
||||
Reference in New Issue
Block a user