Bug Report: Session Token Count Not Reset After Compaction
Summary
After compaction clears messages, session.totalTokens is not reset to 0, causing the compaction safeguard to trigger repeatedly in an infinite loop.
Environment
- OpenClaw Version: 2026.4.15 (041266a)
- OS: macOS (Darwin 25.3.0 arm64)
- Node: v24.13.1
- Agent: main (relay47/claude-opus-4-7)
- Session Type: Telegram group chat (long-running)
Steps to Reproduce
- Start a long-running Telegram group chat session
- Continue conversation until
totalTokens exceeds compaction threshold
- Compaction triggers and clears messages
- Observe that
totalTokens remains at old value
- Next message triggers compaction again
- Gateway logs: "Compaction safeguard: no real conversation messages to summarize"
- Loop repeats indefinitely
Expected Behavior
After compaction clears messages:
session.totalTokens should be reset to 0
session.estimatedCostUsd should be reset to 0
- Next message should start fresh token counting
Actual Behavior
- Messages are cleared (0 messages in JSONL file)
session.totalTokens remains at old value (38746 in our case)
- Compaction safeguard triggers on every message
- Session becomes unusable
Evidence
Session State (from sessions_list)
{
"sessionId": "583e6528-9941-4cbd-a7ee-ecec55154432",
"totalTokens": 38746,
"contextTokens": 70000,
"status": "running"
}
Actual Message Count
$ cat ~/.openclaw/agents/main/sessions/583e6528-9941-4cbd-a7ee-ecec55154432.jsonl | jq -s 'map(select(.role == "user" or .role == "assistant")) | length'
0
Gateway Logs
2026-04-20T17:27:45 [compaction-safeguard] Compaction safeguard: no real conversation messages to summarize
2026-04-20T17:29:51 [compaction-safeguard] Compaction safeguard: no real conversation messages to summarize
2026-04-20T17:33:01 [compaction-safeguard] Compaction safeguard: no real conversation messages to summarize
Proposed Fix
// After compaction clears messages
session.messages = [];
session.totalTokens = 0; // ADD THIS
session.estimatedCostUsd = 0; // ADD THIS
Workaround
rm ~/.openclaw/agents/main/sessions/<session-id>.jsonl
Impact
- Severity: High
- Affected: Long-running sessions (Telegram groups, persistent chats)
- User Experience: Session becomes unusable, requires manual intervention
Reported by: alshyib
Date: 2026-04-20
Version: 2026.4.15 (041266a)
Bug Report: Session Token Count Not Reset After Compaction
Summary
After compaction clears messages,
session.totalTokensis not reset to 0, causing the compaction safeguard to trigger repeatedly in an infinite loop.Environment
Steps to Reproduce
totalTokensexceeds compaction thresholdtotalTokensremains at old valueExpected Behavior
After compaction clears messages:
session.totalTokensshould be reset to 0session.estimatedCostUsdshould be reset to 0Actual Behavior
session.totalTokensremains at old value (38746 in our case)Evidence
Session State (from
sessions_list){ "sessionId": "583e6528-9941-4cbd-a7ee-ecec55154432", "totalTokens": 38746, "contextTokens": 70000, "status": "running" }Actual Message Count
Gateway Logs
Proposed Fix
Workaround
Impact
Reported by: alshyib
Date: 2026-04-20
Version: 2026.4.15 (041266a)