Bug: 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
- 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
{
"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
Root Cause
In dist/model-context-tokens-z5hvDVkk.js, when no real conversation messages exist:
if (!hasRealSummarizable && !hasRealTurnPrefix) {
log.info("Compaction safeguard: no real conversation messages to summarize; writing compaction boundary to suppress re-trigger loop.");
return { compaction: {
summary: buildStructuredFallbackSummary(preparation.previousSummary),
firstKeptEntryId: preparation.firstKeptEntryId,
tokensBefore: preparation.tokensBefore // ❌ Token count not reset
} };
}
The compaction writes a boundary but doesn't reset session.totalTokens, causing the session manager to think tokens are still high.
Proposed Fix
After compaction (in session manager), reset token counters:
// After applying compaction result
if (compactionResult) {
session.totalTokens = 0;
session.estimatedCostUsd = 0;
}
Or in the compaction safeguard return:
return { compaction: {
summary: buildStructuredFallbackSummary(preparation.previousSummary),
firstKeptEntryId: preparation.firstKeptEntryId,
tokensBefore: 0 // Reset to 0 instead of preparation.tokensBefore
} };
Workaround
Delete the session file to force recreation:
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
- Frequency: Occurs reliably after first compaction in long-running sessions
Additional Context
- This affects Telegram group chats most severely due to their long-running nature
- Other agent sessions may also be affected but less noticeable due to shorter lifespans
- The compaction mechanism itself works (messages are cleared), but the accounting is broken
- The issue creates a resource leak where sessions appear to consume tokens they no longer have
Reported by: @alshyib
Date: 2026-04-20
Bug: 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 value"Compaction safeguard: no real conversation messages to summarize"Expected 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
{ "sessionId": "583e6528-9941-4cbd-a7ee-ecec55154432", "totalTokens": 38746, "contextTokens": 70000, "status": "running" }Actual Message Count
Gateway Logs
Root Cause
In
dist/model-context-tokens-z5hvDVkk.js, when no real conversation messages exist:The compaction writes a boundary but doesn't reset
session.totalTokens, causing the session manager to think tokens are still high.Proposed Fix
After compaction (in session manager), reset token counters:
Or in the compaction safeguard return:
Workaround
Delete the session file to force recreation:
Impact
Additional Context
Reported by: @alshyib
Date: 2026-04-20