Repository navigation
fix(agent-core-v2): return a stopped subagent's partial output to the parent agent - #4430
Conversation
… parent agent A subagent run only wrote to its task output when it completed. When it was stopped (TaskStop, a user interrupt, or a cancelled parent turn), timed out, or failed, the parent agent saw a bare status and lost whatever the subagent had already written. runAgentTurn now collects the run's public assistant text from the child's AssistantDelta, TurnStepStarted and TurnStepRetrying events: current turn only, the text of retried step attempts dropped, kept to the most recent 32K characters on code point boundaries, and released once the run settles. The run handle exposes it through partialOutput(). SubagentTask writes that text to the task output, marked as incomplete, before settling a killed or failed task, and flushes it in forceStop when cancellation outlives the grace period. The output is written at most once, so a completion that arrives after a forced settle cannot append to it. TaskStop on an agent task now shows that output together with the agent_id, output_path and a resume hint, and still suppresses the terminal notification. A failed or cancelled foreground Agent call inlines the same block. When the subagent wrote nothing, both say so explicitly. No extra model request is made, and completion and cancellation semantics are unchanged.
🦋 Changeset detectedLatest commit: 1b9f35b The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
There was a problem hiding this comment.
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.
Codex Review SummaryThis 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: 1b9f35babf
ℹ️ 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".
|
Codex Review: Didn't find any major issues. Delightful! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
|
恢复速度变快,来自父 Agent 更早接受了未经验证的中间结论。是否同时检查恢复后的正确率、重复执行、总成本? |
… parent agent Port of upstream MoonshotAI/kimi-code 93137ca (MoonshotAI#4430). The src half of this change (runAgentTurn's collectRunOutput, the partialOutput() run handle, SubagentTask's writePartialOutput, TaskStop and foreground-Agent failure rendering) was already present in this tree byte-for-byte, so this commit lands the remaining pieces: the upstream tests, the tools.md note about stopped subagents returning partial text, and the changeset. Upstream summary: a subagent run only wrote to its task output when it completed; when stopped, timed out, or failed, the parent agent lost whatever the subagent had already written. runAgentTurn collects the run's public assistant text (current turn only, retried step attempts dropped, most recent 32K on code point boundaries) and exposes it via partialOutput(); SubagentTask writes it to the task output marked as incomplete; TaskStop shows it with the agent_id, output_path and a resume hint. No extra model request is made, and completion and cancellation semantics are unchanged. Test adaptations: vitest fake timers replaced with the fork's IHostTimers harness in subagent-timeout.test.ts, vi.waitFor with the repo's waitForAssertion helper in tool.test.ts, and the fork's originForRequest prompt-origin variant kept in runAgentTurn.ts.
Requirement or Bug
子 agent 被停止、超时或失败时,父 agent 拿不到它这一轮已经写出的内容。
Bug Reproduction Steps
Agent(run_in_background=true)启动一个子 agent。TaskStop停止它。用户按 Esc 取消前台Agent调用、任务超时、子 agent 因步数上限或模型服务报错而失败,也是同样的情况。TaskStop只返回status: killed等四行;前台失败结果只有错误原因;后台超时通知不带任何输出。子 agent 已经写出的内容,父 agent 一个字也看不到。Root Cause
runAgentTurn只在 turn 成功完成后才读取最后一条 assistant 消息;取消和失败直接抛错,不读取任何文本。SubagentTask只在 completion 成功时appendOutput,所以被停止、超时、失败的 agent 任务输出始终为空。TaskStop为避免重复唤醒会抑制终态通知,于是父 agent 只拿到状态。这是根本修复:运行过程中采集本轮的公开文字,在任务以停止、超时或失败结束时写入任务输出,再由
TaskStop和前台失败结果交给父 agent。停止时不额外请求模型,completion 的成功、失败、取消契约不变。Code Changes
为什么用事件采集,而不是结束时读上下文:全量压缩会在 step 之间触发,并丢弃全部 assistant 消息,结束时读上下文会丢掉本轮前半段的文字;本轮正在流式输出的那段文字也只在事件里。
Behavior Changes and Affected Users
TaskStop停止运行中的 agent 任务的返回内容Wall time/task_id/status/reason四行agent_id、output_path(已落盘时)、带[partial output]标注的本轮文字(或 none 说明)和resume_hintTaskStop停止 Bash、后台提问任务TaskStop作用于已结束的任务Agent调用失败、被用户取消或超时的工具结果subagent error: …后接resume_hint/next_stepisError、stop_reason、next_step不变TaskOutput、WaitFor、output_path文件)<output-file>指向部分输出(通知逻辑本身未改)task.terminated的outputTail(后台 agent 任务被停止、超时、失败)没有改 system prompt、工具描述或提醒文本。新增的模型可见文字都在工具输出里:
[partial output]标注、none 说明、TaskStop的resume_hint。旧会话里已结束的任务没有部分输出,按空处理。受影响模块与测试覆盖:
src/session/subagent/runAgentTurn.ts→test/session/subagent/runAgentTurn.test.ts:只收本轮,排除其他 turn 和 thinking;重试的段丢弃;不依赖可被压缩的上下文;截断不切坏字符;结束后退订。src/agent/tools/agent/subagent-task.ts、src/agent/tools/task/task-stop/taskStopTool.ts→test/agent/task/rpc-events.test.ts:只交付一次且仍抑制通知;没写文字时的说明;恰好完成时保留完整结果;重载后经TaskOutput可读。test/agent/task/subagent-timeout.test.ts:宽限期到期仍交付,迟到的 completion 不覆盖。src/agent/tools/agent/agentTool.ts→test/tool/tool.test.ts:前台被用户停止时内联;成功结果不带标注;空输出时的说明。test/session/subagent/forkParity.test.ts:真实父子链路。后台TaskStop后,父 agent 下一次请求能看到这段文字,没有多余请求,也没有通知;前台被 Esc 取消时,这段文字进入父 agent 上下文。本地验证:
agent-core-v2类型检查通过,根目录pnpm lint0 错误。全量测试中失败的几项在main上同样失败:tool.test.ts里快进 2 小时假时间的前台超时用例(本机超过 30 秒时限),以及mcpCore/oauth/service.test.ts的超时类用例。Checklist
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.