# Context Limit & Compaction Analysis — Daedalus & Socrates ## Executive Summary **The 20K limit you mentioned is NOT the model context window** — it's likely an OpenClaw-internal compaction threshold. The models themselves support **128K context windows**, but compaction is triggering far too early (~20K tokens), causing: 1. **Frequent context loss** during complex tasks 2. **The ghost bug** — phantom messages from other agents appearing after compaction ## Current Configuration | Agent | Model | Context Window | Max Tokens | Compaction Count | |-------|-------|---------------|------------|------------------| | **Socrates** | kimi-k2.5:cloud | 128,000 | 8,192 | 1+ (observed) | | **Daedalus** | minimax-m2.7:cloud | 128,000 | 8,192 | Unknown | | **Wadsworth** (main) | glm-5.1:cloud | 128,000 | 8,192 | Just triggered | ## The Problem ### 1. Premature Compaction - Models support **128K context** (~100+ pages of text) - Compaction appears to trigger at **~20K tokens** (estimated from session sizes) - This is **16× smaller** than the model's actual capacity - Complex tasks (coding, architecture discussions) hit this wall constantly ### 2. The Ghost Bug (Cross-Agent Contamination) **Root Cause:** When OpenClaw compacts context in a **group chat session**, it includes **ALL assistant messages** from the conversation thread — not just the current agent's responses. **Evidence from contaminated session:** ```jsonl // Matt asks @Daedalus89Bot {"role":"assistant","content":"Waiting on Daedalus to chime in. 🎨"} // ^ This is Wadsworth's message, injected into Socrates's session as generic "assistant" ``` **Result:** After compaction, Socrates sees phantom "assistant" responses that were actually from Daedalus or Wadsworth, causing identity confusion. ## Session Size Analysis | Session | Lines | Size | Status | |---------|-------|------|--------| | Socrates (contaminated backup) | ~4,700 | 263 KB | **Contaminated** | | Socrates (current) | ~471 | 36 KB | Clean so far | | Daedalus (current) | ~134 | 12 KB | Clean | | Main (just compacted) | ~29 | 25 KB | **Just compacted** | **Note:** The contaminated Socrates session (263 KB) suggests a long group chat history that got compacted, mixing in messages from all three agents. ## Recommendations ### Immediate Fixes (Configuration) **1. Increase Compaction Threshold to 100K+** OpenClaw likely has a compaction threshold setting. Look for: - `compactionThreshold` - `contextLimit` - `maxContextBeforeCompaction` - Environment variable: `OPENCLAW_COMPACTION_THRESHOLD` **Recommended:** Set to **100,000 tokens** (leaving 28K buffer for response + system prompts) **2. Disable Compaction for Group Chats (If Possible)** Group chats with multiple agents should either: - Never compact (preserve full history) - Use agent-specific compaction (only summarize that agent's messages) **3. Per-Agent Session Isolation** Current architecture shares session context for group chats. Better approach: - Each agent maintains their own session file - Cross-agent references use `{"agent": "daedalus"}` metadata - Compaction only touches that agent's messages ### Code-Level Fix (OpenClaw Core) The compaction logic needs to filter assistant messages: ```typescript // Pseudocode for the fix function compactContext(session, agentId) { const messages = session.messages.map(msg => { // Tag assistant messages with agent ID if (msg.role === "assistant") { return { ...msg, metadata: { agent: msg.agentId || "unknown" } }; } return msg; }); // When loading compacted context for a specific agent: const agentMessages = messages.filter(msg => msg.role !== "assistant" || msg.metadata?.agent === agentId ); return summarize(agentMessages); } ``` ### Workaround (User-Level) Until fixed in OpenClaw core: 1. **Use DMs for complex tasks** — Avoid group chats for deep work 2. **Start fresh sessions** — If you see ghost messages, clear the session: ```bash rm /home/hoffmann_admin/.openclaw/agents/{socrates,daedalus}/sessions/*.jsonl ``` 3. **Monitor compaction counts** — Check for `authProfileOverrideCompactionCount > 0` ## Detection Script ```bash #!/bin/bash # check-compaction.sh — Detect contaminated sessions echo "=== Checking for compaction contamination ===" # Find sessions with compaction for agent in socrates daedalus main; do echo "\n--- $agent ---" # Check session index for compaction count grep -h "compactionCount\|authProfileOverrideCompactionCount" \ /home/hoffmann_admin/.openclaw/agents/$agent/sessions/sessions.json 2>/dev/null | head -5 # Check for foreign agent mentions echo "Foreign agent mentions:" if [ "$agent" = "socrates" ]; then grep -c "Daedalus\|Wadsworth" /home/hoffmann_admin/.openclaw/agents/$agent/sessions/*.jsonl 2>/dev/null | grep -v ":0$" || echo "None found" elif [ "$agent" = "daedalus" ]; then grep -c "Socrates\|Wadsworth" /home/hoffmann_admin/.openclaw/agents/$agent/sessions/*.jsonl 2>/dev/null | grep -v ":0$" || echo "None found" fi done ``` ## Files Affected **Contaminated backups:** - `/home/hoffmann_admin/.openclaw/agents/session-backups-20260420160143/socrates-*.contaminated-moved.jsonl` - `/home/hoffmann_admin/.openclaw/agents/session-backups-20260420160143/daedalus-*.contaminated-moved.jsonl` **Current sessions to monitor:** - Socrates: `1ab409b1-8e73-4b78-b5fd-761c5b1fdf6b.jsonl` (471 lines, clean) - Daedalus: `00701f9c-7727-4305-ab6b-d1437aeb722a.jsonl` (134 lines, clean) ## Next Steps 1. **Find OpenClaw compaction config** — Check docs, env vars, or config files for the 20K threshold 2. **Increase to 100K+** — This alone will reduce ghost bug frequency by ~5× 3. **File upstream bug** — The cross-agent contamination is an OpenClaw core issue 4. **Monitor sessions** — Run detection script weekly --- **Analyzed:** 2026-04-21 **Status:** Awaiting configuration fix **Impact:** High — affects all multi-agent group chats