Skip to content

fix(agent-core-v2): serialize low-ratio auto compaction - #5339

Open
sailist wants to merge 2 commits into
mainfrom
bug-324-11-02-compaction-low-ratio-race
Open

sailist wants to merge 2 commits into
mainfrom
bug-324-11-02-compaction-low-ratio-race

Conversation

@sailist

@sailist sailist commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Requirement or Bug

Resolve #2720.

当 compaction_trigger_ratio 低于硬阻塞线时,auto compact 在后台运行,后续 agent step 继续写入 assistant/tool 历史,导致压缩提交前的快照校验反复失败。本 PR 让自定义低阈值压缩能够完成,同时保证工具 continuation 先消费原始工具结果。

Bug Reproduction Steps

  1. 在 config.toml 的 [loop_control] 中设置 compaction_trigger_ratio = 0.6。
  2. 使用超过模型窗口 60% 的上下文,运行包含多次工具调用的长回合。
  3. 观察 auto compact 反复出现 begin → cancel;达到硬阻塞线后,取消还可能使回合以 abort 结束。

Root Cause

软阈值触发压缩后,beforeStep 之前只在达到硬阻塞线时等待。压缩在途期间,下一步写入的 assistant/tool 历史改变了提交前快照,因此成功的压缩响应也会因 historySafeToCompact 校验失败而被丢弃,并在上下文未缩小的情况下反复重试。

这是根本修复:在途 auto compact 与后续 step 串行执行,而不是禁止低阈值配置或绕过快照安全校验。

Code Changes

 before step
+  若仅需顺延软压缩且未达硬阈值,先让工具 continuation 消费原始结果
   检查并按需启动 auto compact
-  只在达到硬阈值时等待
+  任意 auto compact 在途时等待;达到硬阈值时仍优先等待

 after tool-calls step
+  只顺延软压缩,不启动新的后台压缩
  • beforeStep 在任何 auto compact 在途时等待,避免后续 step 与压缩提交并发写历史;硬阈值行为保持不变。
  • afterStep 对工具 continuation 只记录一次软压缩顺延。continuation 消费原始工具结果后,再按既有策略触发软压缩;新回合会清理该临时状态。
  • 保留最新 main 对 aborted step 不触发压缩的保护。
  • 回归测试覆盖低阈值在途阻塞、后续回合等待、压缩 telemetry 回合归属、工具 continuation 顺序及恢复后的历史一致性;测试声明数净零。
  • 添加 @moonshot-ai/agent-core-v2 的 patch changeset。

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
低阈值 auto compact 在途时的后续 step 后续 step 与压缩并发,可能反复取消压缩 后续 prompt 可被接受,但下一 LLM step 等压缩完成后再执行 配置较低 compaction_trigger_ratio 的 CLI、Desktop/Web、VS Code、ACP 与 SDK 会话 无;旧行为是会丢失压缩结果的竞态,不作为兼容能力保留
工具调用后的软压缩 工具 step 结束后立即启动软压缩,continuation 可能只能看到压缩后的结果 continuation 先消费原始工具结果,再进行软压缩 使用低阈值且包含连续工具调用的会话 达到硬阻塞线时仍立即压缩并阻塞,既有配置不变
默认阈值与硬阈值路径 达到硬阈值时压缩并等待 不变 使用默认配置或已达到硬阻塞线的会话 N/A

受影响模块为 agent-core-v2 的 full compaction 生命周期钩子。fullCompaction.test.ts 分别覆盖上述三行行为,并保留 aborted step、硬阈值、telemetry 回合归属及 resume 一致性检查;没有修改公开 API、配置契约或持久化格式。无需更新用户文档,因为本次是恢复既有配置应有的行为。

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: issue must have a maintainer's /approve).
  • I have added tests that prove my feature works.
  • The behavior-change table above is complete, and every removed behavior or flipped default is named in the changeset and either has an escape hatch or was explicitly approved by a maintainer in this PR.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.
验证记录
  • m0-dev:fullCompaction.test.ts 120/120 通过。
  • m0-dev:tsc -p packages/agent-core-v2/tsconfig.json --noEmit 通过。
  • lint-pool:完整 pnpm lint 与 check:duplication 通过。
  • 未做 Windows 专项验证;本次不涉及 Windows 专属路径。

@changeset-bot

changeset-bot Bot commented Nov 24, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: db5f4e7

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/agent-core-v2 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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-24T06:29:09.050199Z db5f4e7 PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@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: db5f4e7416

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

this._register(
this.loopService.hooks.onDidFinishStep.register('full-compaction', async (ctx, next) => {
if (!ctx.signal.aborted) await this.afterStep();
if (!ctx.signal.aborted) await this.afterStep(ctx.finishReason === 'tool_calls');

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Detect tool continuations from executed tools

When a low compaction trigger is configured and an OpenAI Responses model invokes a tool, ctx.finishReason is completed, not tool_calls: packages/core/src/llm/builtin/protocol/openai-responses/decode.ts maps a completed response to that value even when its output contains function calls, while ctx.tools records the executed calls. Consequently this branch does not defer soft compaction, so afterStep can compact the newly returned tool result before the continuation consumes it—the exact ordering this change is intended to prevent. Determine the continuation from the executed tools rather than the provider finish reason.

AGENTS.md reference: AGENTS.md:L192-L194

Useful? React with 👍 / 👎.

Comment on lines +520 to 521
if (this._compacting !== null || shouldBlock) {
await this.block(signal, turnId);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Keep soft compaction failures non-blocking

When compactionTriggerRatio is below the hard block ratio and the summary request fails—for example, the provider repeatedly returns an empty or truncated compaction response—this condition now enters block() solely because _compacting is non-null. The rejected compaction promise then propagates through onWillBeginStep, failing the whole user turn without attempting its normal model request, even though shouldBlock is false; previously this soft compaction ran in the background and its failure did not prevent the turn. Serialize the soft attempt without making its failure fatal unless the hard threshold was actually reached.

AGENTS.md reference: AGENTS.md:L192-L194

Useful? React with 👍 / 👎.

@pkg-pr-new

pkg-pr-new Bot commented Nov 24, 2026

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

commit: db5f4e7

This branch has not been deployed

No deployments
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.

Auto compaction cancelled at commit time (historySafeToCompact race with running loop), turn killed with 'This operation was aborted'

1 participant