fix(flow-chat): persist exact last request usage for startup display - #2098
Merged
Conversation
added 2 commits
August 6, 2026 12:50
Startup or opening a historical session left session.currentTokenUsage undefined because the only writer was the TokenUsageUpdated event fired after a model response. The ModelSelector then hid the context percentage (tokenPercentage > 0 guard) and the tooltip omitted the last-request context line (current <= 0 guard). Backfill currentTokenUsage from the last completed dialog turn's persisted tokenUsage during hydrate commits in loadSessionHistory and refreshPeerSessionSnapshot. The backfill is idempotent (keeps any live value) and skipped for ACP sessions. The exact value is still overwritten by the next TokenUsageUpdated event.
The hydration backfill reused the last completed dialog turn's accumulated token usage, which sums input tokens across every model round of that turn. Long agentic sessions therefore showed an absurd context usage (e.g. 8.9M tokens) right after startup. Store the exact last request usage in session metadata (customMetadata.lastRequestTokenUsage, via the existing UI metadata whitelist) on every TokenUsageUpdated for non-ACP sessions, restore it during metadata hydration, and restrict the turn-based fallback to single-round turns where the accumulated value equals a single request.
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 #2097
Problem
Right after startup (or when opening a historical session) the chat input box shows a wrong context usage:
session.currentTokenUsageis undefined because its only writer is theTokenUsageUpdatedevent, which fires only after a model response. The input box then hides the context percentage and the tooltip omits the last-request context line.Changes
fix(flow-chat): restore context usage display after session hydration— backfillcurrentTokenUsagefrom the last completed dialog turn's persistedtokenUsageduring hydration commits (loadSessionHistory/refreshPeerSessionSnapshot). Idempotent (keeps any live value), skipped for ACP sessions, overwritten by the nextTokenUsageUpdatedevent.fix(flow-chat): persist exact last request usage for startup display— store the exact last request usage in session metadata (customMetadata.lastRequestTokenUsagevia the existing UI metadata whitelist) on everyTokenUsageUpdatedfor non-ACP sessions, restore it during metadata hydration, and restrict the turn-based fallback to single-round turns where the accumulated value equals a single request.Verification
FlowChatStore.test.tsandtokenUsageDisplay.test.tsextended