Repository navigation
Conversation
A compaction request may summarize a leading part of the history and keep the rest in the context, so stopping short of the end of its baseline reuses the whole cached prefix and costs nothing. The ledger now reports a compaction request only when, after its first difference from the baseline, it sends again what the baseline already sent, as a compaction that trims the oldest messages does. Main requests keep the strict rule. Role segments of Anthropic and Google messages are marked so their identical text does not count as content sent twice. The undo declaration in the SDK prompt events case only covered the compaction request after the undo, which left out the undone turn and now passes, so it goes away. The two argument correction declarations now say that the retry only drops the trailing correction message.
…ink its input When the compaction request no longer fits, compaction used to cut the history from the head: the pre-shrink kept only the most recent messages, an overflow kept a shrinking recent share, and a truncated or empty summary dropped the oldest message. Each of those changes the start of a request the size of the window, so the provider could reuse none of its cached prefix, and the dropped messages never reached the summary. Compaction now shrinks from the tail instead. It summarizes the leading messages that fit, ending at a split point, and keeps the rest of the history verbatim after the summary, using the kept-tail shape that replay already rebuilds from the legacyTail flag on context.apply_compaction. Trimming the head stays as the fallback when the kept messages would take more than half of the compaction budget, such as after switching to a model with a much smaller window, and when no shorter prefix ends at a split point.
🦋 Changeset detectedLatest commit: 57daf18 The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
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 |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
commit: |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 11eb1edd87
ℹ️ About Codex in GitHub
Your team has set up Codex to 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 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
…r the summary An assistant message kept verbatim after the summary still carried the provider usage of its request, which counted the history the summary just replaced. Compaction sizes the context from the latest assistant usage, so right after a kept-tail compaction the context still looked as large as before and the next turn compacted again. The kept-tail shape now drops that usage, for live compactions and for older sessions with the same shape on replay alike; turn usage is recorded when the message is appended and does not change. The product changesets now say that earlier user messages survive only in the summary on these paths, and that no setting keeps them verbatim.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f941e34300
ℹ️ About Codex in GitHub
Your team has set up Codex to 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 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
The kept-tail shape sized the compacted context from its messages alone, while the normal shape adds the request overhead of the system prompt and tool definitions. The token counting rebase took that smaller number, so the next request reported less used context than it had, and a Kimi request without an output limit would offer the missing overhead as completion budget. The kept-tail shape now adds the same overhead; replay keeps using the tokensAfter recorded on the event.
Requirement or Bug
接着 #4402 的分析继续修。#4402 已经让默认配置下正常的自动压缩不再走预裁剪,但压缩输入放不下时,仍然有三条路径会从头部砍历史:
一旦砍头,接近窗口大小的压缩请求就从第一条消息开始和缓存对不上,整段按冷 prefill 计费。按 #4935 里的数据,代价约是普通回合的 100 倍。而且被砍掉的历史根本没进入摘要,信息直接丢了。
这个 PR 把这三条路径都改成从尾部收缩:压缩请求只带能放下的前一段历史,其余消息原样留在摘要后面。
Bug Reproduction Steps
droppedCount > 0,这个请求几乎没有缓存命中。测试里的复现:
packages/agent-core-v2/test/agent/fullCompaction/fullCompaction.test.ts里的「pre-shrinks compaction history using historical assistant usage anchors」(1M 窗口、按真实用量估算,正是 #4402 的场景)。修复前,压缩请求里没有最老的两轮,droppedCount: 2。Root Cause
fullCompactionService.ts里,压缩输入需要变短时,三处都从头部开始删:preShrinkHistoryToWindowBudgettakeRecentMessagesWithinTokenBudget,只保留最近的消息shrinkCompactionHistoryAfterOverflow,按 0.7、0.5、0.35 的比例保留最近的消息dropOldestMessageAndLeadingToolResults,删掉最老的一条压缩请求本来就是「整段历史 + 压缩指令」,天然延续主请求的前缀。从头部删,等于把这个前缀整段换掉。这次是根本性的修复。从头部删仍然保留为退回路径,只在两种情况下使用:原样保留的消息太多,比如切换到窗口小很多的模型(这正是 #3911 加预裁剪的原因);或者已经找不到更短的切分点。
Code Changes
核心思路:压缩输入放不下时,压缩能放下的前一段历史,其余原样保留。这样压缩请求是主请求的一个前缀加上压缩指令,缓存可以命中,历史也一条不丢。
flowchart TD start["压缩:整段历史 + 指令"] --> fits{放得下?} fits -- 是 --> normal["正常压缩(行为不变)<br/>保留的用户消息 + 摘要 + 续写提示"] fits -- "否:预裁剪 / 服务端超长 / 摘要截断或为空" --> tail{"能在切分点处收短,<br/>且原样保留的消息不超过预算的一半?"} tail -- 是 --> keep["压缩 [start, end)<br/>摘要 + 原样保留的 [end, …)<br/>事件带 legacyTail: true"] tail -- 否 --> head["退回:从头部裁剪(原有做法)"]实现上,用
[start, end)这个区间表示这次实际拿去压缩的历史:start > 0表示从头部裁掉了,end小于历史长度表示尾部原样保留。三条路径共用shrinkFromTail:在预算内找最后一个可以切分的位置,切分规则复用strategy.ts里已有的canSplitAfter。[摘要, ...尾部]这个「保留尾部」形状,事件里写legacyTail: truelegacyTail从 agent-core-v2 最早的版本起就在context.apply_compaction的 schema 里,回放也认它,新旧版本打开会话都能正确重建,不需要改契约。代价见行为变化表阅读指南:
fullCompactionService.ts的compactionRound:range的变化、三处收缩、应用摘要时如何选择形状。shrinkFromTail和preShrinkToWindowBudget:在预算内找切分点,以及原样保留的上限。packages/testkit/src/model/prompt-cache.ts的resendsAfter:账本对压缩请求的新判定。fullCompaction.test.ts里这几个用例:「retries an overflowing compaction on the leading messages…」(新增,用 testkit 模型)、「pre-shrinks compaction history using historical assistant usage anchors…」、「probes only the first compaction response after a truncated summary retries」(删掉断点声明后,它就成了缓存回归测试)。可以略读的部分:
strategy.ts只是导出了canSplitAfter;Anthropic 和 Google 编解码器给角色段加了标记;三个 changeset;两处断点声明改写了原因。Behavior Changes and Affected Users
context.apply_compaction事件compactedCount等于历史长度,砍头时带droppedCountcompactedCount小于历史长度,并带legacyTail: true;droppedCount只统计从头裁掉的条数;tokensAfter和正常形状一样计入请求开销(system 和工具定义),压缩后的 token 计数和下一个请求的usedContextTokens不会少算这部分compacted_count、ACP 的「Messages compacted」compaction_finished.compacted_count正常压缩(输入放得下)的路径和形状没有变。提示词文本没有改动:压缩指令原样不动。契约文件也没有改:
wire-manifest.d.ts和事件 schema 都不变,这次只是用到了已有的legacyTail字段。这次改变了压缩在少数情况下的行为,而且没有配置开关,所以请维护者明确确认:上表里「早期用户消息不再原样保留」这一项损失是否可以接受。
测试覆盖:
request #2 (compaction) does not extend request #0 … messages[1] user became messages[1] assistant。恢复修复后全部通过。expectResumeMatches验证。packages/testkit/test/prompt-cache.test.ts新增的用例验证,原有的「从头裁剪的压缩请求会被报出」用例保留不变。验证细节
只在 macOS(Node 24.21.0)上运行过。这个 PR 不涉及路径、子进程或 shell。
pnpm lintpnpm exec tsc -p <项目>/tsconfig.json --noEmit:testkit、agent-core-v2、node-sdk、kap-server、tests/scenariospackages/agent-core-v2/test/agent/fullCompactionapps/kimi-code/test)加上 node-sdk 的session-prompt-eventscompact-builds-the-next-turn-from-the-summary、keeps-one-prompt-cache-prefix-across-a-restart(TUI、ACP)pnpm run check:duplication <merge-base>Checklist
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.