Repository navigation
fix: wait for the engine to finish disposing before server close resolves - #5250
Conversation
…lves kap-server and acp-server called core.dispose(), which only starts the asynchronous teardown of the engine, so close() resolved while engine services still held file watchers on the data and workspace directories. Callers that removed those directories right after close deleted watched directories, which on Windows keeps the watch handle reporting events until it is closed. Await the instantiation service's disposal after core.dispose(), as the Node SDK already does. The ledger reports disposal errors instead of rejecting, so the remaining cleanup steps still run.
🦋 Changeset detectedLatest commit: f44402a The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 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. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f44402af27
ℹ️ 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".
| await new Promise((resolve) => setTimeout(resolve, 50)); | ||
| expect(events).toEqual(['dispose started']); |
There was a problem hiding this comment.
Replace the fixed sleeps with deterministic shutdown checkpoints
If the cleanup steps after core.dispose() take longer than 50 ms, these regression tests can pass even without the production fix: the sleep expires before close() resolves, the test releases the disposal gate, and both final event sequences then appear correct. This affects both this test and packages/acp-server/test/lifecycle.test.ts:129–131, particularly under the slow or loaded environments motivating this change. Replace the elapsed-time assumption with explicit lifecycle/checkpoint synchronization so the tests reliably distinguish awaiting engine disposal from merely completing unrelated cleanup later. The root AGENTS.md explicitly prohibits sleep-based test synchronization.
AGENTS.md reference: AGENTS.md:L98-L98
Useful? React with 👍 / 👎.
commit: |
Both the kap-server and ACP server now await core.instantiation.disposeAsync() after core.dispose(), so file watchers and other DI services finish closing before shutdown resolves (MoonshotAI#5250). Both regression tests register a SlowToDispose service behind a release gate and assert close waits for it; the changeset is copied.
Requirement or Bug
Layered QA 的 Windows 任务里,vitest 报过 3 次
Worker exited unexpectedly,当时正在跑的进程内 KAP 测试文件中途消失:两次是tests/scenarios/replay.test.ts,一次是packages/acp-server/test/kap-parity.test.ts,其中一次是 main 上的夜间运行。查下来,kap-server 和 acp-server 的
close()都在引擎释放完成之前就返回了:返回时,引擎在数据目录和工作区上开的文件监听还没关,调用方紧接着删目录或者退出进程。这个 PR 让close()等引擎释放完成再返回。Bug Reproduction Steps
node --require预加载一个脚本,记录每个fs.watch的打开和关闭,以及每次fs.promises.rm时目标目录里还开着的监听器。close()在一个还没 dispose 完的引擎服务之前就返回了。Root Cause
close()调用的core.dispose()只是启动instantiation.disposeAsync(),并不等它完成。配置、工作区、技能、指令等引擎服务是在这段异步释放里关闭各自的文件监听的,所以close()返回之后,监听还要再开几毫秒,负载高时更久。Node SDK 的关闭流程早就是先this.app.dispose(),再await this.app.instantiation.disposeAsync(),两个服务端都少了第二步。和 Windows 上 worker 退出的关系:
fs.watch监听的目录后,句柄不会失效,ReadDirectoryChangesW会不停地返回事件、空转,直到句柄关闭。见 nodejs/node#61398,修复在 libuv/libuv#5013,还没进入 Node 的正式版本;CI 用的 Node 24.15.0 受影响。这个 PR 修的是
close()不等释放完成这个根因,不是绕过。Code Changes
close ... core.dispose() + await core.instantiation.disposeAsync() await appendLogStore.drainRetirements() ...kap-server 和 acp-server 各加一行,和 Node SDK 的写法一致。
disposeAsync()返回的是dispose()已经启动的同一个 promise,不会重复释放。onUnexpectedError报告,所以disposeAsync()不会 reject,close()后面的清理步骤照常执行。两条新测试用同一种写法:往引擎里注入一个 dispose 要等闸门打开才完成的服务,确认闸门关着时
close()不会返回,打开后事件顺序是「dispose 开始 → dispose 完成 → close 返回」。测试里有一段 50 毫秒的等待,作用是给不等释放的实现一个返回的机会,从而让它失败;对正确的实现,结果不依赖等待长短。写法和packages/agent-core-v2/test/_base/di/child.test.ts里验证disposeAsync的用例一致。Behavior Changes and Affected Users
close()返回的时机kimi web退出、托管模式下的空闲退出,以及关闭后清理数据目录的测试和脚本close()返回的时机kimi acp(Zed 等 ACP 客户端断开后进程退出),以及在测试里起 ACP 服务端的代码packages/acp-server/属于契约文件的范围。使用方是 Zed 等 ACP 客户端和kimi acp。协议、接口和配置都没有变,上一个版本的客户端照常工作;变的只是服务端进程在退出前把引擎释放做完。桌面端用Promise.allSettled等内嵌服务端关闭,CLI 也没有给服务端的close()设时限,所以不会因为关闭稍慢而超时。测试覆盖:
packages/kap-server/test/boot.test.ts:resolves close only after the engine has finished disposing its services。packages/acp-server/test/lifecycle.test.ts:同名用例,覆盖 ACP 服务端。验证(macOS)
pnpm exec vitest run packages/acp-server packages/kap-server/test/boot.test.ts:179 条全部通过。pnpm exec vitest run packages/kap-server packages/testkit tests/scenarios:1632 条通过,1 条失败,是hosts.test.ts的「lets a managed host sign in and renew with its own Kimi login」。去掉本 PR 的改动后,它在本机同样 3/3 失败;在 main 的 CI 上是通过的,属于本机环境问题,和本 PR 无关。pnpm qa:layers:共删除 456 次目录,其中 15 次是 KAP 测试目录,没有一次还开着监听器。修复前跑 2 遍就有 4 到 7 次。pnpm exec tsc -p packages/kap-server/tsconfig.json --noEmit、pnpm exec tsc -p packages/acp-server/tsconfig.json --noEmit:通过。pnpm lint:通过。Windows:本机无法验证。是否消除了 worker 退出,要看合入后 Layered QA 在 Windows 上的运行记录;此前这类失败大约每 10 次 Windows 运行出现 1 次。
Checklist
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.