fix(strategy): stop condensation from resetting session token totals - #2383
Open
MuskanPaliwal wants to merge 3 commits into
Open
fix(strategy): stop condensation from resetting session token totals#2383MuskanPaliwal wants to merge 3 commits into
MuskanPaliwal wants to merge 3 commits into
Conversation
Entire-Checkpoint: 01M2848G1CNPKDA8YJDA1MDFJA
MuskanPaliwal
marked this pull request as ready for review
September 11, 2026 12:08
Entire-Checkpoint: 01M28J6MX4M92Y88PWYE3WPRFK
Entire-Checkpoint: 01M2A6HV96SV0206QW63R1V23N
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2368
Trail: #5: Preserve session-wide token totals during condensation
SessionState.TokenUsageis the running total shown byentire statusandentire session tokens.sessionData.TokenUsageserves a different purpose: it contains only the usage since the previous checkpoint.During condensation, the checkpoint value was passed to
sessionStateBackfillTokenUsage. Its final branch accepted that value for every agent when input tokens were present.applyBackfilledSessionTokenUsagethen stored it as the session total.As a result, a session with 10,000 cumulative input tokens could appear to have used only the 100 tokens recorded since its latest checkpoint. Later turns accumulated from that smaller value, leaving the total permanently undercounted.
Keep cumulative and checkpoint usage separate
This change removes the agent-agnostic checkpoint fallback.
Non-Copilot agents now retain the cumulative token total collected through their lifecycle hooks. Checkpoint metadata continues to record the smaller checkpoint-window value.
Copilot CLI keeps both existing backfill paths. Its authoritative
session.shutdownrecord can arrive after lifecycle hooks return, so condensation still:If a non-Copilot agent has no cumulative hook data, the CLI now reports no session-wide total. This is deliberate: a missing total is more accurate than presenting one checkpoint鈥檚 usage as the total for the entire session.
Verification
The regression test models a session with 10,000 cumulative input tokens and a 100-token checkpoint. With the previous behavior,
CondenseSessionByIDsaved 100 as the session total. With this change, the total remains 10,000 after the state is reloaded from disk.Additional tests cover both Copilot backfill paths.
Checks completed:
mise run check, including formatting, linting, race-enabled tests, integration tests, and deterministic E2E canaries