Repository navigation
fix(agent-core-v2): stop background subagents cleanly when the session shuts down - #5583
Conversation
…n shuts down Closing the product while a background subagent runs aborted it twice wrong. The Agent tool turned a non-Error abort reason into "[object Object]" and wrote it into the task notification the agent reads after the session is reopened. And the abort listener looked up the task service on the subagent scope after that scope had been disposed, so the rejected cleanup went unhandled and kimi acp exited with code 1. Read the reason with the existing errorMessage() helper, and resolve the task service once while the scope is alive. The new E2E scenario reopens a session whose background subagent was still running and checks that the product does not call the model on its own and reports the stopped task once, readably.
🦋 Changeset detectedLatest commit: 2360ede 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. |
commit: |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 2360ede7cb
ℹ️ 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".
| expect(requests[2]?.texts('user').join('\n').split('source_kind="background_task"')).toHaveLength(2); | ||
| expect(requests[2]?.texts('user').join('\n')).not.toContain('[object Object]'); | ||
| expect(requests[2]?.userTextsSinceReply.at(-1)).toBe('What happened to the count?'); | ||
| expect(model.requests('subagent')).toHaveLength(1); |
There was a problem hiding this comment.
Keep scenario request assertions on the main lane
This assertion directly inspects the subagent request lane, contrary to the explicit review-enforced rule in tests/e2e/AGENTS.md that scenarios only assert model.requests('main'). Keep this scenario's request assertions on the main lane; the existing held-request cancellation check already observes the background request being aborted, while the main-lane assertions verify the restored session's behavior.
AGENTS.md reference: tests/e2e/AGENTS.md:L52-L52
Useful? React with 👍 / 👎.
Requirement or Bug
用户在后台子 agent 还在跑的时候关掉产品,会碰上两个问题:
count files was stopped. Reason: [object Object],停止原因是乱码。kimi acp时进程崩溃,退出码 1,stderr 是Error: InstantiationService has been disposed。这个 PR 之后,停止原因读不出来时通知里就不写原因;
kimi acp照常以 0 退出。这两个问题是 E2E 场景(见 #5582)在这次补的场景里发现的。Bug Reproduction Steps
Agent,run_in_background: true),子 agent 的模型请求一直不返回。/exit,ACP 由编辑器关掉连接。count files was stopped. Reason: [object Object]。kimi acp exited with code 1,stderr 末尾是InstantiationService has been disposed,栈从stopChildTasks经AbortSignal.onAbort出来。E2E 复现:
TESTKIT_CLI_BUILD=dist pnpm --filter @moonshot-ai/e2e exec playwright test --project=tui --project=acp scenarios/conversation/reopening-with-an-unfinished-background-task-waits-for-the-user.spec.ts。修复前 TUI 红在not.toContain('[object Object]'),ACP 红在重启前的收尾检查;修复后两个面都绿。Root Cause
两处根因不同,都发生在退出时中止后台子 agent 的那一刻。
packages/agent-core-v2/src/agent/tools/agent/agentTool.ts:子 agent 被中止时,停止原因用String(reason)转字符串。退出时传进来的中止原因既不是字符串,也不是Error,于是成了[object Object],写进任务字段,之后进了通知。同一个文件里已有的errorMessage(),以及核心包packages/core/src/tool/tool-executor.ts的stopReasonText(),对这种原因都返回undefined。packages/agent-core-v2/src/session/subagent/runAgentTurn.ts:中止监听器onAbort调用stopChildTasks(target, …),后者在里面才执行target.accessor.get(IAgentTaskService)。退出时子 agent 的作用域先被释放,这次查找就抛错。stopChildTasks是 async 函数,抛错变成了被拒绝的 promise;finally里那次补调onAbort()产生的 promise 没人 await,成了未处理的拒绝,kimi acp因此退出。都是根本修复:1 改用已有的
errorMessage();2 在awaitRunCompletion一开始、作用域还活着时取到任务服务,中止时直接用它,不再去已释放的作用域里查。Code Changes
另外:
scripts/checks/baselines/oxlint-ratchet.json少了一条agentTool.ts的no-unsafe-assignment,删掉的那行String(reason)正是它;新增的 E2E 场景tests/e2e/scenarios/conversation/reopening-with-an-unfinished-background-task-waits-for-the-user.spec.ts从 #5582 挪到这里,和修复一起合入。Behavior Changes and Affected Users
Error的中止原因停止时,任务通知里的原因Reason: [object Object]count files killed.)[object Object]不携带任何信息Error作为中止原因(用户停止、超时等)kimi acp时有后台子 agent 在跑受影响的面:TUI、print 模式、Desktop、Web(都会在退出时中止后台子 agent),ACP(崩溃只在这里出现)。
验证:
packages/agent-core-v2/test/session、test/agent/task、test/features/swarm共 37 个文件、773 条全过。pnpm lint通过(按 ratchet 降了一条基线)。Checklist
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.