Skip to content

Bug: Session token count not reset after compaction #69287

@RrcioElise

Description

@RrcioElise

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

  1. Start a long-running Telegram group chat session
  2. Continue conversation until totalTokens exceeds compaction threshold
  3. Compaction triggers and clears messages
  4. Observe that totalTokens remains at old value
  5. Next message triggers compaction again
  6. Gateway logs: "Compaction safeguard: no real conversation messages to summarize"
  7. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions