Skip to content

fix(agent-core-v2): count compaction tokens on the full-request basis - #2699

Merged
sailist merged 1 commit into
MoonshotAI:mainfrom
sailist:fix/compaction-token-full-request-basis
Aug 6, 2026
Merged

fix(agent-core-v2): count compaction tokens on the full-request basis#2699
sailist merged 1 commit into
MoonshotAI:mainfrom
sailist:fix/compaction-token-full-request-basis

Conversation

@sailist

@sailist sailist commented Aug 6, 2026

Copy link
Copy Markdown
Collaborator

Related Issue

No linked issue — the problem is explained below.

Problem

After compaction, the reported token counts read far below the real context size. The compaction result (tokensBefore/tokensAfter, shown in the compaction divider and used to rebase the context-size gauge) was computed on a messages-only basis — summary plus kept user messages — while the context size reported between exchanges comes from measured LLM usage, i.e. the full request (system prompt + tool schemas + messages + output). In a session with ~39k of fixed request overhead, compacting a ~90k context reported "→ 1.3k" even though the real next request was ~36.5k; the gauge dipped to the small messages-only estimate and then jumped back on the following exchange.

What changed

  • tokensBefore is now the full-request estimate (system prompt + non-deferred tools + messages) via the existing request-size helper.
  • tokensAfter adds the estimated request overhead (system prompt + non-deferred tool schemas) on top of the measured summary output tokens and the kept user messages, so it stays on the same full-request basis as the measured exchange anchors. The token-counting ledger rebase after compaction carries this full-basis size, so the reported context size no longer dips and jumps.
  • The PreCompact hook tokenCount uses the same basis.
  • Wire replay still reads the persisted tokensAfter verbatim, so old compaction records restore unchanged.
  • Tests: updated the exact token assertions to the new basis, added a shape-level test pinning the overhead component, and raised the synthetic model window in one test whose 4k window sits below the harness's fixed overhead under the new basis.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue, or explained the problem above.
  • 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.

- tokensBefore/tokensAfter now include the system prompt and non-deferred
  tool schemas, matching the measured-anchor basis the context gauge uses
  between exchanges
- the post-compaction ledger rebase carries the same full-request size, so
  the reported context size no longer dips to a messages-only estimate and
  jumps back on the next exchange
- the PreCompact hook tokenCount uses the same basis
@changeset-bot

changeset-bot Bot commented Aug 6, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 919bd5d

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

@pkg-pr-new

pkg-pr-new Bot commented Aug 6, 2026

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

commit: 919bd5d

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 919bd5d1d1

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

compactedCount: originalHistory.length,
tokensBefore,
summaryOutputTokens: attempt.usage?.output,
requestOverheadTokens: this.requestTokens([]),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Rebase tokens after refreshing the prompt

Because compactionRound() passes requestOverheadTokens here before returning to compactionWorker(), the overhead is captured before profile.refreshSystemPrompt() runs. In the supported path where project instructions/profile prompt change before a manual compaction, tokensAfter and the rebase/status event are computed from the old prompt, while the next request uses the refreshed prompt, so the divider/gauge can still jump after the next exchange. Compute the post-compaction size after the refresh or use the refreshed prompt for this overhead estimate.

Useful? React with 👍 / 👎.

Comment on lines +1438 to +1440
// The window must stay above the harness's fixed request overhead
// (system prompt + tools, ~14k): the post-compaction size is reported on
// the full-request basis, so a smaller window could never be satisfied.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Move inline rationale out of the test body

The scoped agent-core-v2 guide allows comments only in the top-of-file block, but this adds narrative comments inside the test body. Please encode the rationale in the test name/fixture constants or move it to the file header so the test stays within the local convention.

AGENTS.md reference: packages/agent-core-v2/AGENTS.md:L36-L38

Useful? React with 👍 / 👎.

@sailist
sailist merged commit c0b61c6 into MoonshotAI:main Aug 6, 2026
15 checks passed
@github-actions github-actions Bot mentioned this pull request Aug 6, 2026
tpoisonooo pushed a commit to tpoisonooo/kimi-code that referenced this pull request Aug 7, 2026
main's MoonshotAI#2699 counts compaction tokens on the full-request basis, so the
tower tool schemas (default registry) and the /tower skill catalog entry
(system prompt) shift the pinned numbers: +2789 with the default tool
set, +173 with the explicit harness tool list. The 20k-window test keeps
its shape with a 22k window so the post-compaction floor still fits.
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.

1 participant