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 (guarded by tokenPercentage > 0) and the tooltip omits the last-request context line (guarded by current <= 0).
- 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.
Root cause
- Session hydration backfilled
currentTokenUsage from the last completed dialog turn's accumulated tokenUsage, which sums input tokens across all model rounds of that turn.
- No exact per-request usage was persisted anywhere, so after a restart the precise last-request usage was unavailable to the frontend.
Fix
PR #XXXX ([fix(flow-chat): persist exact last request usage for startup display]) does two things:
- Backfill
currentTokenUsage from the last completed dialog turn's persisted tokenUsage during hydration commits (loadSessionHistory / refreshPeerSessionSnapshot) so the display is populated right after startup. The backfill is idempotent (keeps any live value), skipped for ACP sessions, and the exact value is still overwritten by the next TokenUsageUpdated event.
- 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
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.
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 (guarded bytokenPercentage > 0) and the tooltip omits the last-request context line (guarded bycurrent <= 0).Root cause
currentTokenUsagefrom the last completed dialog turn's accumulatedtokenUsage, which sums input tokens across all model rounds of that turn.Fix
PR #XXXX ([fix(flow-chat): persist exact last request usage for startup display]) does two things:
currentTokenUsagefrom the last completed dialog turn's persistedtokenUsageduring hydration commits (loadSessionHistory/refreshPeerSessionSnapshot) so the display is populated right after startup. The backfill is idempotent (keeps any live value), skipped for ACP sessions, and the exact value is still overwritten by the nextTokenUsageUpdatedevent.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
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.