Skip to content

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

Description

@YodonTan

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:

  1. 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.
  2. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions