feat(openclaw): tune compaction to keepRecentTokens 3k and convert Tier A to deep-compaction
This commit is contained in:
@@ -17,15 +17,15 @@ import subprocess
|
||||
import sys
|
||||
import time
|
||||
|
||||
THRESHOLD_TOKENS = 20000
|
||||
THRESHOLD_TRANSCRIPT_BYTES = 300000
|
||||
THRESHOLD_EVENT_COUNT = 40
|
||||
THRESHOLD_TOKENS = 15000
|
||||
THRESHOLD_TRANSCRIPT_BYTES = 200000
|
||||
THRESHOLD_EVENT_COUNT = 30
|
||||
CRON_MAX_AGE_HOURS = 48
|
||||
RPC_TIMEOUT_MS = 180000
|
||||
|
||||
# Tier A: Event-logging agents whose state is 100% externalized (DuckDB / JSONL / Groceries DB).
|
||||
# Nightly reset to 0 prevents cross-day hallucination and eliminates LLM compaction token costs.
|
||||
TIER_A_RESET_TARGETS = [
|
||||
# Tier A: High-frequency interaction topics (koch, hebamme, shopping).
|
||||
# Deep-compaction keeps a compact summary + latest 2 turns without completely wiping conversational memory.
|
||||
TIER_A_TARGETS = [
|
||||
("koch", "agent:koch:telegram:group:-1004326921510:topic:21"),
|
||||
("hebamme", "agent:hebamme:telegram:group:-1004326921510:topic:22"),
|
||||
("shopping", "agent:shopping:telegram:group:-1004326921510:topic:115"),
|
||||
@@ -117,9 +117,9 @@ def prune_stale_cron_sessions():
|
||||
|
||||
print(f"Cron pruning complete: {total_pruned} sessions pruned, {total_tokens_reclaimed} tokens reclaimed.")
|
||||
|
||||
def reset_tier_a_sessions():
|
||||
print("=== Tier A: Daily Hard-Reset to 0 (koch, hebamme, shopping) ===")
|
||||
for agent, key in TIER_A_RESET_TARGETS:
|
||||
def compact_tier_a_sessions():
|
||||
print("=== Tier A: Deep-Compaction (koch, hebamme, shopping) ===")
|
||||
for agent, key in TIER_A_TARGETS:
|
||||
db_path = f"/home/node/.openclaw/agents/{agent}/agent/openclaw-agent.sqlite"
|
||||
if not os.path.exists(db_path):
|
||||
continue
|
||||
@@ -130,18 +130,32 @@ def reset_tier_a_sessions():
|
||||
if not row or not row[0]:
|
||||
print(f" [Tier A] {agent} ({key}) already at 0 (no active session).")
|
||||
continue
|
||||
print(f" [Tier A] Resetting {agent} ({key})...")
|
||||
ev_cnt, ev_bytes = get_session_metrics(agent, key, row[0])
|
||||
if ev_cnt <= 2 and ev_bytes < 20000:
|
||||
print(f" [Tier A] {agent} ({key}) has only {ev_cnt} events ({ev_bytes} bytes), skipping.")
|
||||
continue
|
||||
print(f" [Tier A] Deep-compacting {agent} ({key}) [{ev_cnt} events, {ev_bytes//1024}KB]...")
|
||||
r = subprocess.run(
|
||||
["openclaw", "sessions", "delete", key, "--agent", agent, "--yes"],
|
||||
capture_output=True, text=True, timeout=60.0
|
||||
["openclaw", "sessions", "compact", key, "--agent", agent, "--timeout", str(RPC_TIMEOUT_MS)],
|
||||
capture_output=True, text=True
|
||||
)
|
||||
if r.returncode == 0:
|
||||
print(f" ✓ Reset success: {r.stdout.strip()}")
|
||||
print(f" ✓ Deep-compacted: {r.stdout.strip()}")
|
||||
elif "Already compacted" in r.stderr or "Already compacted" in r.stdout:
|
||||
print(f" ✓ Already compacted.")
|
||||
else:
|
||||
print(f" ✗ Reset error: {r.stderr.strip()}")
|
||||
print(f" ✗ Standard compact error: {r.stderr.strip()}; retrying with --max-lines 40...")
|
||||
fb = subprocess.run(
|
||||
["openclaw", "sessions", "compact", key, "--agent", agent, "--max-lines", "40", "--timeout", str(RPC_TIMEOUT_MS)],
|
||||
capture_output=True, text=True
|
||||
)
|
||||
if fb.returncode == 0:
|
||||
print(f" ✓ Truncation fallback success: {fb.stdout.strip()}")
|
||||
else:
|
||||
print(f" ✗ Truncation fallback failed: {fb.stderr.strip()}")
|
||||
except Exception as e:
|
||||
print(f" ✗ Exception resetting {agent}: {e}")
|
||||
time.sleep(1)
|
||||
print(f" ✗ Exception deep-compacting {agent}: {e}")
|
||||
time.sleep(2)
|
||||
|
||||
def get_session_metrics(agent, key, sid=None):
|
||||
db_path = f"/home/node/.openclaw/agents/{agent}/agent/openclaw-agent.sqlite"
|
||||
@@ -166,7 +180,7 @@ def get_session_metrics(agent, key, sid=None):
|
||||
return 0, 0
|
||||
|
||||
def compact_active_sessions():
|
||||
tier_a_keys = set(t[1] for t in TIER_A_RESET_TARGETS)
|
||||
tier_a_keys = set(t[1] for t in TIER_A_TARGETS)
|
||||
print(f"=== Tier B: Running Multi-Agent Compaction (threshold: {THRESHOLD_TOKENS} tokens / {THRESHOLD_TRANSCRIPT_BYTES//1024}KB, LLM summarization) ===")
|
||||
res = subprocess.run(["openclaw", "sessions", "--all-agents", "--json"], capture_output=True, text=True)
|
||||
if res.returncode != 0:
|
||||
@@ -266,7 +280,7 @@ def main():
|
||||
except Exception:
|
||||
pass
|
||||
prune_stale_cron_sessions()
|
||||
reset_tier_a_sessions()
|
||||
compact_tier_a_sessions()
|
||||
compact_active_sessions()
|
||||
checkpoint_wal_journals()
|
||||
vacuum_and_optimize()
|
||||
|
||||
Reference in New Issue
Block a user