Skip to content

persistSessionUsageUpdate corrupts totalTokens causing repeated auto-compaction per message #82576

@njuboy11

Description

@njuboy11

Description

When a session compaction runs via incrementCompactionCount, it correctly sets totalTokens and totalTokensFresh=true in the session store. However, the subsequent persistSessionUsageUpdate call (which runs after every model call) unconditionally overwrites totalTokens=undefined and totalTokensFresh=false when there is no fresh context snapshot.

Impact

This causes the preflight compaction guard to fail, forcing a full transcript token re-estimation on every message dispatch. For models with smaller context windows (e.g., MiniMax M2.7-highspeed at 204K tokens), the re-estimation exceeds the compaction threshold, triggering a new auto-compaction on every single message.

Models with larger context windows (e.g., deepseek-v4-flash at 1M tokens) are not affected because the threshold is never exceeded.

Root Cause

In src/auto-reply/reply/session-usage.ts: when hasFreshContextSnapshot is false, totalTokens is undefined but still unconditionally written to the session store, overwriting the value set by incrementCompactionCount after compaction.

Fix

Only update totalTokens/totalTokensFresh when a fresh context snapshot is available. See PR for implementation.

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