Skip to content

fix(acp): stream background task turns to editors - #5447

Merged
RealKai42 merged 2 commits into
mainfrom
codex/issue-033-acp-background-turns
Nov 25, 2026
Merged

RealKai42 merged 2 commits into
mainfrom
codex/issue-033-acp-background-turns

Conversation

@RealKai42

@RealKai42 RealKai42 commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Requirement or Bug

报告 033 中,ACP 编辑器收到「已在后台开始」后,后台子 agent 完成时的回复和工具调用没有出现在对话里。编辑器此时提交新消息,还可能立即收到 end_turn,而消息实际进入了引擎队列。

Bug Reproduction Steps

  1. 通过 ACP 发一条消息,让主 agent 创建后台子 agent 后先回答「已在后台开始」。
  2. 等原请求返回 end_turn,再让子 agent 完成。主 agent 会收到任务完成通知,并生成下一条回复。
  3. 对照编辑器收到的 session/update。修复前只能找到第一条回复。在通知回合运行时另发一条消息,可以同时观察到过早的 end_turn。

Root Cause

ACP 用当前 session/prompt 的回合 ID 筛选事件。通知回合由引擎发起,没有对应的 ACP 请求,因此回复、思考和工具事件都被过滤。与此同时,引擎提交入口默认允许排队,但 ACP 在提交结果没有立即提供回合 ID 时,会把原请求当作结束。

Code Changes

ACP 按引擎已有的任务来源和回合 ID 识别后台通知,共用原来的 session/update 事件转换。通知回合结束时刷新上下文用量。普通请求仍按自己的回合 ID 结算。

flowchart LR
    A[引擎回合事件] --> B{回合从何处发起}
    B -->|编辑器提示词| C[转发会话更新并结算对应请求]
    B -->|后台任务通知| D[转发会话更新并刷新上下文用量]
    E[IDE 提交新消息] --> F{引擎能立即接收}
    F -->|是| C
    F -->|否| G[返回已有的忙错误]
Loading
  • ACP 会话在收到带任务来源的 turn.started 时记录回合 ID。回复、思考和工具更新都走原有转换,工具审批仍由原有的交互桥处理。后台回合运行期间,未分配回合 ID 的编辑器请求会等到有明确目标后再取消。
  • Klient 透传可选的 rejectIfBusy。ACP 普通提示词使用这个选项。引擎在接收消息前判断是否需要排队,拒绝的请求没有队列或会话元数据写入。Klient 的 memory 和 IPC 都保留忙原因,ACP 继续返回 -32600 和 data.code: turn.agent_busy。
  • 中英文 ACP 参考补充了通知到达的时机及忙错误的重发方式。

Behavior Changes and Affected Users

行为 修复前 修复后 受影响人群 恢复方式
后台任务通知 编辑器看不到引擎新开的通知回合 ACP 通过现有 session/update 发送回复、思考、工具状态和上下文用量 在 Zed 等 ACP 编辑器运行后台子 agent 的用户 现有 ACP 实现可以继续消费原有更新类型
通知回合仍在运行时提交新消息 可能立即收到 end_turn,消息实际排队 收到原有的 turn.agent_busy,被拒绝的消息不会进入会话 在编辑器里连续提交消息的 ACP 用户和脚本 等通知回合结束后重发
等待分配回合 ID 的请求收到取消 无目标的取消可能打到当前的通知回合 关联到编辑器请求后才取消它对应的回合 发送新消息后立即取消的 ACP 用户 使用原有取消操作
引擎普通提交入口 忙时默认排队 默认仍排队。选择 rejectIfBusy 的调用方会在接收前收到忙错误 Klient 和引擎调用方 沿用原调用方式即可继续排队

ACP 仍使用 v1 的更新和错误结构,历史回放走现有入口。回归测试从 ACP RPC 和引擎提交入口分别核对通知、工具状态、忙错误、后续回复和默认排队。

Verification

在 macOS 上先用 main 的 CLI 构建复现。最终构建通过 300 轮真实 ACP 子进程压测、4 个 Playwright 用例和 321 项相关 Vitest 测试。三个受影响库的类型检查、pnpm lint、重复代码检查和中英文文档构建通过。

本地压测明细
  • 用 4 个并发轮换 60 个隔离的 kimi acp 子进程,每个会话连续跑 5 轮。分别模拟具备文件读取能力的编辑器和不声明该能力的编辑器,并对本地模型流随机切分字节。
  • 每一轮检查初始回复和通知回复各出现一次,工具卡片完成,随后的用户提示词获得正确回复。每个子进程都在标准输入关闭后以 0 退出。
  • 100 轮在通知回合进行时提交和取消新请求。全部收到 turn.agent_busy,后台回合继续完成。另有 100 轮将后台回合的写入审批与工具 ID 对上,并核对写入文件的内容。
  • Playwright 通过标准 ACP 和无编辑器能力两种模式复跑报告场景及发送自测。持久回归测试直接观察 ACP RPC、引擎队列和两种 Klient 传输。

P1 补入持久的 ACP 编辑器场景。先等前台回合的用量更新,再释放后台任务。后台更新到达后,复用 ACP SDK 驱动断言新旧回复各出现一次、Read 已完成,以及下一条提示词收到自己的回复。用 PR 基线 3d58fad4e 的 CLI 运行时,两种 ACP 配置均因缺少后台更新而失败。修复版通过 8 项 Playwright 场景与自测,新增场景另重复 24 轮全部通过。E2E 类型检查、pnpm lint 和重复代码检查通过。

Checklist

  • 已阅读 CONTRIBUTING。
  • 已记录报告 033 的复现路径和根因。
  • 已补真实 ACP RPC 和引擎回归用例。
  • 已列出涉及的调用方和行为变化。
  • 已添加对应的 patch changeset。
  • 已同步中英文 ACP 参考文档。

@changeset-bot

changeset-bot Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: f24fff3

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
@moonshot-ai/kimi-code Patch
@moonshot-ai/acp-server Patch
@moonshot-ai/agent-core-v2 Patch
@moonshot-ai/klient 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 25, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-25T16:48:07.618184Z f24fff3 Manual request
🔒 Security Review ❌ Failed 2026-11-25T16:18:32.463897Z
We couldn’t complete this review. Comment @codex security review to try again.
1b781a2 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.

@pkg-pr-new

pkg-pr-new Bot commented Nov 25, 2026 •

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

commit: f24fff3

@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: 1b781a2c83

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

});
});

test('streams a background task notification between editor prompts', { timeout: 30_000 }, async ({ model, testHome }) => {

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 Add the required ACP editor-surface regression

This fixes editor-visible behavior, but the new regression only inspects raw RPC notifications through TestClient; it does not exercise the SDK-validated ACP editor surface. Root AGENTS.md explicitly requires regressions for behavior visible in ACP editors to use tests/e2e. Searching that tree found no scenario covering background-task replies arriving after session/prompt completes. Add a persistent ACP surface scenario that observes the unsolicited reply and completed tool call, then verifies that a subsequent prompt still works; the package-level test can remain as additional coverage.

AGENTS.md reference: AGENTS.md:L61-L61

Useful? React with 👍 / 👎.

@RealKai42

Copy link
Copy Markdown
Collaborator Author

@codex

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: f24fff33cd

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

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