Skip to content

fix(web-ui): persist and hydrate lastRequestTokenUsage for context display - #2120

Open
xielixing wants to merge 1 commit into
GCWing:mainfrom
xielixing:fix/issue-2097-context-usage-startup
Open

fix(web-ui): persist and hydrate lastRequestTokenUsage for context display#2120
xielixing wants to merge 1 commit into
GCWing:mainfrom
xielixing:fix/issue-2097-context-usage-startup

Conversation

@xielixing

Copy link
Copy Markdown

Fix: Context usage display missing or inflated after startup / historical session open

Fixes #2097

Root cause

session.currentTokenUsage is only set by the TokenUsageUpdated event, which fires after a model response completes. On startup or when opening a historical session, currentTokenUsage is undefined, so the context percentage bar is hidden.

When the code tried to backfill from the last completed dialog turn's tokenUsage, it used the turn's accumulated input-token sum across all model rounds — producing absurd values (e.g. 8.9M tokens for a session that used ~12K).

Changes

  1. Type: Added lastRequestTokenUsage to SessionCustomMetadata interface (session-history.ts)
  2. Rust whitelist: Added "lastRequestTokenUsage" to UI_CUSTOM_METADATA_KEYS array (session_application.rs) so the key is persisted/merged correctly
  3. Persist: In buildSessionCustomMetadata, persist session.currentTokenUsage (minus the runtime timestamp field) as lastRequestTokenUsage in custom metadata
  4. Hydrate: In FlowChatStore.processSession (both instances), hydrate currentTokenUsage from metadata.customMetadata?.lastRequestTokenUsage (adding timestamp: Date.now()) for non-ACP sessions
  5. Fallback: In loadSessionHistory, if currentTokenUsage is still undefined, backfill from the last completed dialog turn that has exactly one model round — this gives a representative single-request token count instead of an accumulated sum
  6. Tests: Added 3 test cases to sessionMetadata.test.ts covering persistence, omission when absent, and replacement of stale values

Validation

  • pnpm --filter web-ui exec tsc --noEmit — no errors in modified files
  • pnpm --filter web-ui exec vitest run src/flow_chat/utils/sessionMetadata.test.ts — 22/22 pass (including 3 new)
  • pnpm --filter web-ui exec vitest run src/flow_chat/utils/tokenUsageDisplay.test.ts — 6/6 pass
  • Rust change is trivial (adding one string to array constant); syntactically verified

…splay

Fixes GCWing#2097 — context usage display was missing on startup (currentTokenUsage
undefined) or inflated when backfilled from accumulated turn token sums.

- Add lastRequestTokenUsage to SessionCustomMetadata type
- Add key to Rust UI_CUSTOM_METADATA_KEYS whitelist
- Persist currentTokenUsage in buildSessionCustomMetadata
- Hydrate currentTokenUsage from persisted metadata in processSession (both instances)
- Fallback: backfill from last completed single-round turn in loadSessionHistory
- Add tests for lastRequestTokenUsage persistence in sessionMetadata.test.ts
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

1 participant