Skip to content

fix(flow-chat): persist exact last request usage for startup display - #2098

Merged
GCWing merged 2 commits into
GCWing:mainfrom
YodonTan:fix/token-usage-display-on-startup
Aug 6, 2026
Merged

fix(flow-chat): persist exact last request usage for startup display#2098
GCWing merged 2 commits into
GCWing:mainfrom
YodonTan:fix/token-usage-display-on-startup

Conversation

@YodonTan

@YodonTan YodonTan commented Aug 6, 2026

Copy link
Copy Markdown

Fixes #2097

Problem

Right after startup (or when opening a historical session) the chat input box shows a wrong context usage:

  • On a fresh start or when opening a historical session, session.currentTokenUsage is undefined because its only writer is the TokenUsageUpdated event, which fires only after a model response. The input box then hides the context percentage and the tooltip omits the last-request context line.
  • Once backfilled from the last completed dialog turn, the value shown is the turn's accumulated input-token sum (across every model round in that turn). Long agentic sessions therefore display an absurd context usage (e.g. 8.9M tokens) right after startup.

Changes

  1. fix(flow-chat): restore context usage display after session hydration — backfill currentTokenUsage from the last completed dialog turn's persisted tokenUsage during hydration commits (loadSessionHistory / refreshPeerSessionSnapshot). Idempotent (keeps any live value), skipped for ACP sessions, overwritten by the next TokenUsageUpdated event.
  2. fix(flow-chat): persist exact last request usage for startup display — 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.

Verification

  • Unit tests: FlowChatStore.test.ts and tokenUsageDisplay.test.ts extended
  • Manually verified with a built installer: after startup and after opening a historical session, the displayed context usage matches the last request instead of being missing or inflated

Tant 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.
@GCWing
GCWing merged commit 3dbf4d9 into GCWing:main Aug 6, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Context usage display is missing or inflated after startup and when opening historical sessions

2 participants