Skip to content

fix(tui): recompute stale context usage ratio from status update token counts - #3164

Open
kimi-agent-bot wants to merge 1 commit into
MoonshotAI:mainfrom
kimi-agent-bot:fix/tui-stale-context-usage-ratio
Open

fix(tui): recompute stale context usage ratio from status update token counts#3164
kimi-agent-bot wants to merge 1 commit into
MoonshotAI:mainfrom
kimi-agent-bot:fix/tui-stale-context-usage-ratio

Conversation

@kimi-agent-bot

Copy link
Copy Markdown
Collaborator

Related Issue

Resolve #3163

Requested by @liruifengv (context window bar rendered ~74% full next to the text "18% (180k / 1M)").

Problem

The /usage panel's Context window bar and the footer's context indicator render appState.contextUsage, but on the v2 engine that ratio is only set by getStatus() pulls (session start/resume) and then goes stale: v2 agent.status.updated events carry contextTokens (token counting service, every turn/compaction) and maxContextTokens (profile service, on model switch) yet never contextUsage — 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 patches contextTokens or maxContextTokens without carrying an explicit contextUsage, recompute the ratio from the post-patch counts (unclamped, matching v1 emit / SDK getStatus semantics; 0 when the window is unknown). v1 events carry the ratio explicitly and are untouched. This fixes all ratio consumers at once (/usage bar, 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-code TUI suite: 2204 passed; the single failure (registry.test.ts add-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

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: the issue must have a maintainer's /approve).
  • I have added tests that prove my feature works.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

…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-bot

changeset-bot Bot commented Aug 21, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 38bdbaf

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code Patch

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

@kimi-agent-bot

Copy link
Copy Markdown
Collaborator Author

@codex review

@pkg-pr-new

pkg-pr-new Bot commented Aug 21, 2026

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@38bdbaf
npx https://pkg.pr.new/@moonshot-ai/kimi-code@38bdbaf

commit: 38bdbaf

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: 38bdbafcd0

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

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 window bar in /usage and footer shows a stale percentage that disagrees with the token counts

1 participant