fix(tui): recompute stale context usage ratio from status update token counts - #3164
Open
kimi-agent-bot wants to merge 1 commit into
Open
fix(tui): recompute stale context usage ratio from status update token counts#3164kimi-agent-bot wants to merge 1 commit into
kimi-agent-bot wants to merge 1 commit into
Conversation
…n counts v2 engine status events carry contextTokens/maxContextTokens but never contextUsage, so appState.contextUsage was only refreshed by getStatus pulls and then went stale while the token counts kept updating live. The /usage panel and footer render the ratio as a bar but recompute the percentage text from the counts, so a stale ratio showed as a bar that disagreed with the percentage (e.g. bar ~74% next to "18% (180k / 1M)" after compaction or a model switch). Recompute the ratio from the post-patch token counts whenever a status update touches contextTokens or maxContextTokens without carrying an explicit contextUsage. v1 events carry the ratio and are unaffected.
🦋 Changeset detectedLatest commit: 38bdbaf The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Collaborator
Author
|
@codex review |
commit: |
|
Codex Review: Didn't find any major issues. Bravo. Reviewed commit: ℹ️ About Codex in GitHubCodex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback". |
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.
Related Issue
Resolve #3163
Requested by @liruifengv (context window bar rendered ~74% full next to the text "18% (180k / 1M)").
Problem
The
/usagepanel's Context window bar and the footer's context indicator renderappState.contextUsage, but on the v2 engine that ratio is only set bygetStatus()pulls (session start/resume) and then goes stale: v2agent.status.updatedevents carrycontextTokens(token counting service, every turn/compaction) andmaxContextTokens(profile service, on model switch) yet nevercontextUsage— the v2 event payload has no such field. The panel's percentage text recomputes from the live counts, so bar and text drift apart after any compaction or model switch.What changed
In the TUI's
handleStatusUpdate, when an event patchescontextTokensormaxContextTokenswithout carrying an explicitcontextUsage, recompute the ratio from the post-patch counts (unclamped, matching v1 emit / SDKgetStatussemantics;0when the window is unknown). v1 events carry the ratio explicitly and are untouched. This fixes all ratio consumers at once (/usagebar, footer indicator,/status) instead of patching each renderer.Tests: four regression tests in
kimi-tui-message-flow.test.ts(tokens-only event, max-only event, explicit-ratio event, unknown-window event).apps/kimi-codeTUI suite: 2204 passed; the single failure (registry.test.tsadd-dir~/completion) also fails on clean main in this sandbox — it lists home-dir entries and is environment-dependent, unrelated to this change. Typecheck and lint (0 errors) pass.Checklist
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.