Skip to content

fix: queue ACP requests until startup completes - #4541

Merged
7Sageer merged 3 commits into
mainfrom
fix/acp-initialize-race
Oct 30, 2026
Merged

7Sageer merged 3 commits into
mainfrom
fix/acp-initialize-race

Conversation

@7Sageer

@7Sageer 7Sageer commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

Requirement or Bug

Zed 等 ACP 客户端会在连接建立后立即发送 initialize,remote environments 扩大启动窗口后,这条请求可能收到 Internal error,导致会话无法开始。本 PR 之后,客户端可以正常完成启动握手;如果服务启动失败,也会收到包含真实原因的错误。

Bug Reproduction Steps

  1. 在包含 remote environments 改动的构建上启动 kimi acp。
  2. 客户端在连接建立后立即发送最小 initialize 请求,无需等待服务启动完成。
  3. 响应返回 -32603 Internal error,错误详情为 Cannot read properties of undefined (reading 'initialize')。

Root Cause

app.connect(stream) 在 server 赋值前开始读取请求。addProvider() 等待 localReady 期间,提前到达的 initialize 会通过 getServer() 解引用仍为 undefined 的 server。

这是根本修复:连接与请求分发之间增加明确的就绪边界。所有处理器等待 serverReady;启动成功时 resolve,provider 注册失败时 reject,因此排队请求不会提前执行或无限等待。

Code Changes

 ACP request
-  getServer().handle(...)  // server may still be undefined
+  await serverReady
+  server.handle(...)

 startup
   connect(stream)
   await addProvider(...)
+  on failure: rejectServer(error) and rethrow
   new AcpServer(...)
+  resolveServer(server)

createAcpAgentApp 同时接受同步或异步 getServer,保留已有同步调用方的类型兼容。serverReady 预置空 catch,避免没有早期请求时产生未处理 rejection;原始启动异常仍由 runAcpServerWithStream 原样抛出。

Behavior Changes and Affected Users

行为 修改前 修改后 依赖旧行为的人群 退路
启动窗口内到达的 ACP 请求 initialize 可能因 server 未赋值返回 -32603 等待服务 ready 后正常处理 Zed 等启动后立即握手的 ACP 客户端用户;无人依赖启动报错 无需,恢复正常握手
启动失败时提前到达的 ACP 请求 返回 server 未赋值的错误,掩盖真实启动原因 返回包含原始启动错误详情的 -32603,请求不会悬挂 嵌入式 ACP 调用方及环境探测失败的客户端 无需,改为明确失败
启动完成后到达的 ACP 请求 正常处理 保持正常处理,ready Promise 已完成 所有现有 ACP 客户端 无需

启动成功、启动窗口内握手、provider 注册失败和既有会话流程均有回归覆盖。ACP 方法、能力矩阵、配置、环境变量及持久化数据格式未改变;prompt 与 KAP 路径不受影响。

验证记录
  • packages/acp-server/test/initialize.test.ts 新增两条用例:启动完成前发送 initialize,以及 provider 注册失败时拒绝已排队请求。
  • 第一条在修复前稳定复现 server 未赋值错误;第二条在缺少 rejection 路径时稳定超时。
  • 最终 ACP 套件 14 个测试文件、153 个测试全部通过。
  • ACP 包类型检查和构建通过,全仓 lint 为 0 errors。
  • 未额外运行真实 Zed 客户端;回归测试使用真实 ACP SDK、NDJSON 流和 agent-core 启动路径。

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.

Copilot AI balanced review requested due to automatic review settings October 30, 2026 03:41
@changeset-bot

changeset-bot Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 425121e

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/kimi-code 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

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@7Hanrui

7Hanrui commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-30T04:16:20.813471Z 425121e Draft marked ready
ℹ️ 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: 48d3bcc6ba

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

Comment on lines +135 to +139
let resolveServer!: (server: AcpServer) => void;
const serverReady = new Promise<AcpServer>((resolve) => {
resolveServer = resolve;
});
const app = createAcpAgentApp(() => serverReady);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Reject queued requests when startup fails

When addProvider rejects—for example, because IHostEnvironment.ready rejects after the connection accepts an early request—runAcpServerWithStream rejects, but this resolve-only promise never settles. The new request handlers therefore remain pending indefinitely, and embedders cannot close the connection because no RunningAcpServer was returned; give serverReady a rejection path and settle or close it on startup failure.

AGENTS.md reference: AGENTS.md:L123-L125

Useful? React with 👍 / 👎.

@7Hanrui

7Hanrui commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: 425121eb1e

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

@7Sageer
7Sageer marked this pull request as ready for review October 30, 2026 04:08
@pkg-pr-new

pkg-pr-new Bot commented Oct 30, 2026

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

commit: 425121e

@7Sageer
7Sageer merged commit 3a0b86e into main Oct 30, 2026
29 of 30 checks passed
@7Sageer
7Sageer deleted the fix/acp-initialize-race branch October 30, 2026 04:17
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.

4 participants