fix(agent-core-v2): count compaction tokens on the full-request basis - #2699
Conversation
- 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 detectedLatest commit: 919bd5d 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: |
There was a problem hiding this comment.
💡 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([]), |
There was a problem hiding this comment.
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 👍 / 👎.
| // 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. |
There was a problem hiding this comment.
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 👍 / 👎.
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.
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
tokensBeforeis now the full-request estimate (system prompt + non-deferred tools + messages) via the existing request-size helper.tokensAfteradds 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.tokenCountuses the same basis.tokensAfterverbatim, so old compaction records restore unchanged.Checklist
gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.