fix(web-ui): persist and hydrate lastRequestTokenUsage for context display - #2120
Open
xielixing wants to merge 1 commit into
Open
fix(web-ui): persist and hydrate lastRequestTokenUsage for context display#2120xielixing wants to merge 1 commit into
xielixing wants to merge 1 commit into
Conversation
…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
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.
Fix: Context usage display missing or inflated after startup / historical session open
Fixes #2097
Root cause
session.currentTokenUsageis only set by theTokenUsageUpdatedevent, which fires after a model response completes. On startup or when opening a historical session,currentTokenUsageisundefined, 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
lastRequestTokenUsagetoSessionCustomMetadatainterface (session-history.ts)"lastRequestTokenUsage"toUI_CUSTOM_METADATA_KEYSarray (session_application.rs) so the key is persisted/merged correctlybuildSessionCustomMetadata, persistsession.currentTokenUsage(minus the runtimetimestampfield) aslastRequestTokenUsagein custom metadataFlowChatStore.processSession(both instances), hydratecurrentTokenUsagefrommetadata.customMetadata?.lastRequestTokenUsage(addingtimestamp: Date.now()) for non-ACP sessionsloadSessionHistory, ifcurrentTokenUsageis 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 sumsessionMetadata.test.tscovering persistence, omission when absent, and replacement of stale valuesValidation
pnpm --filter web-ui exec tsc --noEmit— no errors in modified filespnpm --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