fix(agent-core-v2): keep context size readings on the measured path - #1782
Merged
Conversation
The step fold creates the assistant message in the context before the exchange finishes (a skeleton at step.begin, filled by content.part folds during streaming), and the input array llmRequester passes to contextSize.measured() is that same live array — it already includes the output. Taking input.length + output.length therefore counted the folded output twice, storing a measured prefix length one past the live context. The inflated length permanently failed get()'s measured fast path, so reads silently fell back to per-message estimates (e.g. ~50 tokens shown for a ~29k-token "hi"). Take the live context length as the measured prefix instead (input and context are identical under the identity guard), and clamp the measured prefix to the context length in get() so a bad record can never knock reads off the measured path again. Add contextSize tests driving real turns that assert the wire model, get(), and rpc getContext against the exchange totals.
Polls agent.getContext()/agent.getUsage() and streams agent events for a new session after one "hi" against a real server, printing a timeline of when the context/token readings move, plus a final consistency check comparing the measured tokenCount to cumulative usage. Model seeding is optional via KIMI_EXAMPLE_* env; the server token resolves from <kimi-home>/server.token like the v2 e2e helpers.
🦋 Changeset detectedLatest commit: 2090b65 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 |
commit: |
This was referenced Jul 16, 2026
Merged
Merged
ywh114
pushed a commit
to ywh114/kimi-code
that referenced
this pull request
Jul 19, 2026
…oonshotAI#1782) * fix(agent-core-v2): keep context size readings on the measured path The step fold creates the assistant message in the context before the exchange finishes (a skeleton at step.begin, filled by content.part folds during streaming), and the input array llmRequester passes to contextSize.measured() is that same live array — it already includes the output. Taking input.length + output.length therefore counted the folded output twice, storing a measured prefix length one past the live context. The inflated length permanently failed get()'s measured fast path, so reads silently fell back to per-message estimates (e.g. ~50 tokens shown for a ~29k-token "hi"). Take the live context length as the measured prefix instead (input and context are identical under the identity guard), and clamp the measured prefix to the context length in get() so a bad record can never knock reads off the measured path again. Add contextSize tests driving real turns that assert the wire model, get(), and rpc getContext against the exchange totals. * feat(klient): add context-usage example tracing a fresh session Polls agent.getContext()/agent.getUsage() and streams agent events for a new session after one "hi" against a real server, printing a timeline of when the context/token readings move, plus a final consistency check comparing the measured tokenCount to cumulative usage. Model seeding is optional via KIMI_EXAMPLE_* env; the server token resolves from <kimi-home>/server.token like the v2 e2e helpers. * chore: add changeset for the context size fix
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
No linked issue — the problem is described below.
Problem
The context size reading (the TUI footer indicator and API consumers of the agent context token count) could under-report the model's actual context usage by orders of magnitude: after one "hi" on a fresh session, the provider-reported exchange totals ~29k tokens while the reading shows ~50.
Root cause: the step fold creates the assistant message in the context before the exchange finishes (a skeleton at
step.begin, filled bycontent.partfolds during streaming), and the input array handed to the context-size measurement is that same live array — it already includes the output. Computing the measured prefix length asinput.length + output.lengththerefore counted the folded output twice and stored a prefix length one past the live context. The inflated length permanently failed the measured fast path inget(), silently falling back to per-message estimates.What changed
get(), so a bad stored record can never knock reads off the measured path again.get(), and the RPC context read against the exchange totals (3/4 fail without the fix).getContext()/getUsage()plus agent events, with a final consistency check); used to verify the fix end-to-end.Checklist
gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.