Repository navigation
fix(agent-core-v2): wait for in-flight auto compaction before the next step - #5454
Merged
Merged
Conversation
…t step With loop_control.compaction_trigger_ratio below the 0.85 blocking ratio, automatic compaction started in the background while the turn kept running. The concurrent step appended its reply, so the compaction was discarded after its request had completed, on every step until the context reached the blocking ratio. When a turn then waited on such a stale compaction, the discard surfaced as an abort and the turn ended as cancelled. Wait for an in-flight automatic compaction before starting a step, and when the compaction a turn waits on is discarded because history changed (for example a goal continuation or task notification appended between turns), compact the current history once more instead of aborting the turn.
🦋 Changeset detectedLatest commit: 6a6d1b9 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: |
5 of 6 tasks
sailist
approved these changes
Nov 26, 2026
5 of 6 tasks
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.
Requirement or Bug
把
loop_control.compaction_trigger_ratio设得低于 0.85 的用户,自动压缩基本不生效:压缩在后台启动后和回合的下一步并发,下一步追加的回复让压缩结果被丢弃。上下文处在触发比例和 0.85 之间时,每一步都白跑一次完整的压缩请求,并弹出 "Context compaction did not finish",上下文直到 0.85 才真正缩小。更糟的是,如果上下文越过 0.85 时还有一个过期的压缩在跑,回合会以interrupted/user_cancelled结束,但用户并没有取消。这个 PR 之后,压缩按配置的比例生效,回合不会再被误判为取消。默认配置(0.85)的行为不变。
Bug Reproduction Steps
config.toml里设置[loop_control] compaction_trigger_ratio = 0.7。wire.jsonl:先有full_compaction.begin,紧接着是full_compaction.cancel,没有context.apply_compaction;界面提示 "Context compaction did not finish";后续请求仍带着完整历史。interrupted、end_reason: user_cancelled结束。tests/scenarios/auto-compaction.test.ts通过 KAP 复现第 1–3 步,新的引擎测试确定性地复现了两种现象。Root Cause
max(triggerRatio, 0.85)。触发比例更低时,beforeStep只在 token 数达到阻塞比例时才等待压缩;在两者之间,压缩在后台跑,回合的下一步同时发出请求。historySafeToCompact只接受压缩期间追加的真实用户输入和注入消息。并发那一步追加的 assistant 和工具消息让检查失败,压缩结果在请求已经跑完、费用已经产生之后被丢弃。block()里等待这个压缩时,丢弃产生的 AbortError 被原样抛给回合,runStepStarting把它当成中止,于是回合以取消结束。sequenceDiagram participant Turn as 回合 participant Compaction as 后台压缩 Turn->>Compaction: 第 N 步后 token 越过触发比例,开始压缩 Turn->>Turn: 第 N+1 步不等待(未到 0.85),请求并追加回复 Compaction-->>Compaction: 摘要返回,历史已变,丢弃 Note over Turn,Compaction: 若此时回合在 0.85 处等待它:丢弃 → AbortError → 回合以 cancelled 结束这是根本修复:步骤不再和压缩赛跑;回合等待的压缩被丢弃时,基于当前历史重新压缩,而不是中止回合。
Code Changes
改动集中在
packages/agent-core-v2/src/agent/fullCompaction/fullCompactionService.ts:compactionRound里两处历史检查失败时,现在都通过discardForChangedHistory给这个压缩任务打上historyChanged标记并取消它本身(之前取消的是当时的_compacting,并且没有区分"历史变化"和"被取消")。block()只在看到这个标记、且回合本身没有被中止时才重新压缩;显式取消压缩(SDK 的cancelCompaction)的处理不变。context.apply_compaction记录以及所有回放它的地方(journal、transcript、undo、replayBuilder、kap-server 的 fold),属于 wire 契约变更system_trigger、后台任务通知、!cmd消息都会在回合之间追加进历史。只重试一次,避免异常情况下反复发压缩请求阅读顺序:先看
fullCompactionService.ts的beforeStep、block、waitForActiveCompaction、discardForChangedHistory,再看测试。CompactionStrategy.shouldBlock现在不再被服务调用,阻塞比例只剩下决定"是否在每步结束后检查"的作用。为了让改动聚焦,这次没有改 strategy,可以后续简化。Behavior Changes and Affected Users
compaction_trigger_ratio< 0.85,有自动压缩在进行时,回合下一步的模型请求compactionTriggerRatio的调用方。旧行为下压缩几乎都被丢弃,没有人能从并发中获益cancelled(user_cancelled)结束默认配置不变的依据:触发比例等于阻塞比例时,
beforeStep原本启动压缩后一定会阻塞;beforeStep时已有压缩在进行,只可能来自自带阻塞的上下文溢出恢复。"重新压缩"分支只在回合等待期间历史出现用户输入、注入消息以外的追加时才会触发,回合停在步骤开始阶段时 loop 不会做这种追加。显式取消压缩不会打historyChanged标记,处理不变。受影响模块与测试覆盖:
agent-core-v2的 full compaction 服务。fullCompaction.test.ts新增 "waits for an auto compaction started below the block ratio before requesting the step":覆盖第一、二行,断言压缩进行中没有主请求发出、压缩生效、回合正常完成。fullCompaction.test.ts新增 "compacts again instead of cancelling the turn when the compaction it waits for is discarded":覆盖第四行,用目标续跑消息让后台压缩过期,断言回合正常完成、用的是重新压缩的摘要。compaction.blocked后再放行压缩(第三行),断言没变。tests/scenarios/auto-compaction.test.ts:通过真实 KAP 会话(40k 窗口、触发比例 0.7)断言压缩生效、压缩后的请求带摘要且不再带旧文件内容、两个回合都正常完成。验证:修复前,两个新引擎测试和 KAP 场景都失败;修复后都通过。没有涉及路径、子进程、shell 或终端输入,只在 macOS 上运行过。
运行记录
reason: cancelled、interruptReason: aborted结束;KAP 场景第二个回合没有 compaction 条目。pnpm lint通过;tsc -p packages/agent-core-v2和tsc -p tests/scenarios通过。fullCompaction.test.ts一起跑时,"names truncated compaction responses when retries are exhausted" 和 "honors loopControl.compactionMaxAttempts for retryable generation failures" 在高负载机器上超时(假定时器加一次advanceTimersByTimeAsync)。它们在未改动的 main 上同样超时,单独运行能通过,与本 PR 无关。Checklist
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.