Skip to content

fix(agent-core-v2): return a stopped subagent's partial output to the parent agent - #4430

Merged
RealKai42 merged 1 commit into
mainfrom
fix/subagent-partial-output-on-stop
Oct 26, 2026
Merged

RealKai42 merged 1 commit into
mainfrom
fix/subagent-partial-output-on-stop

Conversation

@RealKai42

Copy link
Copy Markdown
Collaborator

Requirement or Bug

子 agent 被停止、超时或失败时,父 agent 拿不到它这一轮已经写出的内容。

Bug Reproduction Steps

  1. 父 agent 调用 Agent(run_in_background=true) 启动一个子 agent。
  2. 子 agent 先写出一段发现(例如"已定位 src/parser.ts:42 的循环条件"),然后继续调用工具,或开始流式输出报告。
  3. 父 agent 调用 TaskStop 停止它。用户按 Esc 取消前台 Agent 调用、任务超时、子 agent 因步数上限或模型服务报错而失败,也是同样的情况。
  4. 修复前:TaskStop 只返回 status: killed 等四行;前台失败结果只有错误原因;后台超时通知不带任何输出。子 agent 已经写出的内容,父 agent 一个字也看不到。

Root Cause

  • runAgentTurn 只在 turn 成功完成后才读取最后一条 assistant 消息;取消和失败直接抛错,不读取任何文本。
  • SubagentTask 只在 completion 成功时 appendOutput,所以被停止、超时、失败的 agent 任务输出始终为空。
  • TaskStop 为避免重复唤醒会抑制终态通知,于是父 agent 只拿到状态。

这是根本修复:运行过程中采集本轮的公开文字,在任务以停止、超时或失败结束时写入任务输出,再由 TaskStop 和前台失败结果交给父 agent。停止时不额外请求模型,completion 的成功、失败、取消契约不变。

Code Changes

 AgentTool.launch
   subagents.run → runAgentTurn
+    collectRunOutput(子 agent 的 IEventBus)
+      订阅 AssistantDelta / TurnStepStarted / TurnStepRetrying
+      只认本轮 turnId;按 step 分段;重试的那段丢弃;只留最近 32K 字符(码点边界)
     awaitRun(成功、失败、取消的判定与错误类型不变)
+    completion 结束后退订;handle.partialOutput() 只读取文本
   SubagentTask.start
     成功 → appendOutput(summary) → completed(不变)
-    被停止 → killed
-    失败 → failed
+    被停止或失败 → appendOutput("[partial output] …" + 文本) → killed / failed
+  SubagentTask.forceStop(取消超出宽限期时由 TaskService 调用)→ 同样写入
+  输出只写一次:强制结束后迟到的 completion 不会再追加
 TaskStop(仅 agent 任务)
+  追加 agent_id、output_path(已落盘时)、任务输出预览或 none 说明、resume_hint
   终态通知仍被抑制,不唤醒父 turn
 前台 Agent 失败、取消、超时的结果
+  在 subagent error 与 resume_hint 之间内联同一段输出或 none 说明

为什么用事件采集,而不是结束时读上下文:全量压缩会在 step 之间触发,并丢弃全部 assistant 消息,结束时读上下文会丢掉本轮前半段的文字;本轮正在流式输出的那段文字也只在事件里。

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
TaskStop 停止运行中的 agent 任务的返回内容 Wall time / task_id / status / reason 四行 四行不变,后面追加 agent_id、output_path(已落盘时)、带 [partial output] 标注的本轮文字(或 none 说明)和 resume_hint 读这段输出的模型(所有用后台 subagent 的会话)。TUI、kap-server、SDK 都不解析这段文本(已核对) 无,纯追加
TaskStop 停止 Bash、后台提问任务 四行 不变 — —
TaskStop 作用于已结束的任务 返回当前状态 不变,不重复输出内容 — —
前台 Agent 调用失败、被用户取消或超时的工具结果 subagent error: … 后接 resume_hint / next_step 中间插入本轮文字或 none 说明;isError、stop_reason、next_step 不变 模型。TUI 不解析这段文本(AgentSwarm 的解析路径未改) 无
被停止、超时、失败的 agent 任务的任务输出(TaskOutput、WaitFor、output_path 文件) 空 有文字时为带标注的部分输出 读取这些输出的模型 无
后台 agent 超时或被用户在界面上停止时的完成通知 没有输出块 带 <output-file> 指向部分输出(通知逻辑本身未改) 模型 无
wire 中 task.terminated 的 outputTail(后台 agent 任务被停止、超时、失败) 缺省 部分输出的尾部,字段形状不变 desktop 与 web 的任务卡片(code-app 经 kap-server 消费),可能开始显示这段文字 无
成功完成的 agent 任务结果 最后一条消息作为 summary 不变 — —
停止时的模型请求 无 无(测试断言了请求次数) — —

没有改 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 lint 0 错误。全量测试中失败的几项在 main 上同样失败:tool.test.ts 里快进 2 小时假时间的前台超时用例(本机超过 30 秒时限),以及 mcpCore/oauth/service.test.ts 的超时类用例。

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.

… 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.
Copilot AI balanced review requested due to automatic review settings October 26, 2026 10:14
@changeset-bot

changeset-bot Bot commented Oct 26, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 1b9f35b

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.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 26, 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-26T13:14:33.506830Z 1b9f35b Manual request
🔒 Security Review ✅ Completed 2026-10-26T10:17:00.938026Z 1b9f35b 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 Oct 26, 2026

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

commit: 1b9f35b

@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: 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".

@RealKai42

Copy link
Copy Markdown
Collaborator Author

@codex

@chatgpt-codex-connector

Copy link
Copy Markdown

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

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

@RealKai42
RealKai42 merged commit 93137ca into main Oct 26, 2026
17 checks passed
@RealKai42
RealKai42 deleted the fix/subagent-partial-output-on-stop branch October 26, 2026 13:43
This was referenced Oct 27, 2026
@Zhi-lin

Zhi-lin commented Oct 29, 2026

Copy link
Copy Markdown

恢复速度变快,来自父 Agent 更早接受了未经验证的中间结论。是否同时检查恢复后的正确率、重复执行、总成本?

7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 3, 2026
… 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.
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