Skip to content

fix(runtime): enable CodeBuddy background subagents in SOP and chat - #1343

Merged
crispinhu27 merged 5 commits into
mainfrom
feature/codebuddy-workspace-background
Nov 25, 2026
Merged

crispinhu27 merged 5 commits into
mainfrom
feature/codebuddy-workspace-background

Conversation

@crispinhu27

@crispinhu27 crispinhu27 commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

What changed

CodeBuddy's background Agent calls could block the parent at dispatch in SOP nodes and conversation workspaces. The platform now sends the prompt over a bidirectional stream-json session and keeps stdin open while background Agents are running. The parent can continue working and collect the native receipt when it needs the result.

  • After the parent result, allow up to five seconds for outstanding child completion notices. Close stdin, record any remaining child IDs in timeout telemetry, and drain the command so a successful Gate can advance.
  • Close stdin on a top-level CodeBuddy error and preserve the error frame even if it races prompt delivery. The existing conversation recovery retries a missing native session without --resume.
  • Preserve the parent reply when a child result arrives later in a conversation workspace.
  • Tell the parent how to collect standalone task results and team inbox receipts.

Scope

  • Platform core / API / runner
  • Shared behavior contract

Compatibility

The fix uses the existing process-control protocol. Platform-hosted execution uses the updated server. Connected execution uses the updated Runner with the updated server.

Validation

  • Targeted Go regressions passed on the rebased branch across internal/runtime, internal/httpapi, and internal/runner. These cover delayed child completion, parent reply preservation, prompt and image framing, workspace startup recovery, and SOP background execution.
  • A live SOP on the test deployment used a temporary patched Runner, received the child receipt, and completed the downstream steps.
  • Live conversation workspaces on local Docker and a registered Runner received matching native receipts and completed a follow-up turn. The parent completed independent work approximately 36 seconds and 28 seconds before the child finished, respectively.
  • CodeBuddy 2.187.0 on a local native sandbox reproduced both review cases. The SOP passed its Gate and the downstream node succeeded on a connected Runner despite the missing child completion notice. An initial workspace handoff and a later turn with a missing journal both retried successfully, with the latter completing in the original workspace.
  • Targeted Go regressions passed with -race after merging the current base. These include the missing completion notice, early resume error, and Runner cleanup after trailing output.
  • python3 scripts/check-public.py and git diff --check passed.

Safety

  • Kept credentials, internal endpoints, and deployment data out of the change.
  • Preserved user files and unrelated changes.

@Robert0622 Robert0622 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

部署实测结论:SOP 里「父 Agent 收集子 Agent 结果」的主路径已修好,但有 2 个 P1 会让节点或会话一直挂住,暂不 approve

实测 head a9bf6106。环境:本机部署(Go 1.26.11,MySQL 8.4.7 全新库,平台以 all 角色运行,native 沙箱),CodeBuddy CLI 2.187.0 + iOA deepseek-v4.1-flash。用同样方式部署的 main b0f06649 做对照。没有对着本仓库做部署实测。

子 Agent 的任务都是 sleep 40; date +%s > child.txt,父 Agent 派发后立刻 date +%s > parent.txt,用两个时间戳判断父 Agent 有没有在派发处被卡住。

✅ 已修好的部分

场景 PR main 对照
SOP 节点:父派发后台子 Agent,先做自己的事,再用 TaskOutput 收集结果 节点 70 秒成功;parent.txt 比 child.txt 早 40 秒写入;遥测里有 agent.background start/finish ✅ 父 Agent 被卡在派发处:parent.txt 比 child.txt 晚 5 秒
SOP 节点:不用 Agent 工具的普通 CodeBuddy 节点 20 秒成功,产物正确 ✅ —
测试 go vet 通过(runtime/runner/httpapi)。`go test ./internal/httpapi -run 'Assistant Prompt

🔴 P1-1:父 Agent 不等子 Agent 就结束时,SOP 节点永远停在 running

CodeBuddy 2.187.0 在父 Agent 输出顶层 result 之后,不会再为后台子 Agent 输出 task_updated/task_notification(completed)。codeBuddyBackgroundStream 只在收到这两类事件时把子 Agent 从 active 里删掉,所以 drained 永远不会 close,stdin 不会关闭,CLI 进程不会退出,ExecutorRunner.Run 一直等着。

实测复现:SOP 提示词要求父 Agent「不需要子 Agent 的结果,不调用 TaskOutput,直接结束」。

  • 13:12:24 父 Agent 已经 submit_gate_result(pass);13:12:27 输出顶层 result
  • 13:12:57 子 Agent 写完 child.txt;之后 stdout 只有 2 次 system init 和 2 次 Stop hook,没有任何 completed 事件,遥测里也只有 agent.background start,没有 finish
  • 21 分钟后(13:33:51),节点 work 仍是 running,下游 verify 仍是 pending,codebuddy 进程还活着(etime 21:45)。main 上同样的 SOP 65 秒就成功了

直接用 CLI 也能复现:--input-format stream-json、stdin 保持打开、父 Agent 不等子 Agent。子 Agent 完成后 stdout 只有 system init。对照组让父 Agent 用 TaskOutput 等待,task_updated completed 和 task_notification 都会出现。也就是说,子 Agent 的完成事件只有在父 Agent 还在跑时才会输出。

平台写的提示里确实要求父 Agent 先收集结果,但 flash 这类模型、或者用户自己的提示词,很容易不照做。一旦不照做,run 会无限期挂住,还占着执行槽,这比改动前「子 Agent 被提前结束」更严重。

🔴 P1-2:CodeBuddy 会话在 workbench_workspace_open 交接到工作区后,这一轮和之后每一轮都挂住

PR 让工作区对话一律走 stream-json,并且 keepInputOpen 会吞掉 API 转发的 EOF。可是 codeBuddyBackgroundStream 只把顶层 result 当作父 Agent 结束。CLI 启动就出错时,只输出一行 {"type":"error",...},然后一直等 stdin,永远不会退出。原来的「resume 失败后不带 resume 重启」兜底也就走不到了。

实测复现:新建项目会话并选 native 沙箱,发「请调用 workbench_workspace_open 打开一个新的个人工作区,打开后回复已打开」:

PR main
attempt 1(平台模式,调用 workbench_workspace_open) completed completed
attempt 2(工作区模式,带 resume) CLI 报 No conversation found with session ID: afdn4bl30epxaj 后不再退出,5 分钟后客户端超时,生成以 context canceled 结束 同样报错,但 CLI 立即退出 → attempt 3 不带 resume,completed
这一轮 挂住(2 次独立复现) 28 秒回复「已打开」
之后再发一条普通消息(「用 shell 写 plain.txt」) 又带着同一个 resume ID 启动,同样挂住 180 秒 —
后台子 Agent 那一轮 被上面的问题挡住,无法测到 60 秒返回,父 Agent 被子 Agent 卡住

直接用 CLI 也能确认:codebuddy -p --input-format stream-json --resume <不存在的ID>,stdin 保持打开时进程会一直等到 stdin 关闭(30 秒);stdin 关闭时 1 秒就退出。

也就是说,用 CodeBuddy 的会话每次从平台模式交接到工作区都会挂住,会话之后也一直用不了。PR 里「工作区对话已实测」的结果,可能来自一个没有经过平台模式交接的会话。

修复建议

  1. observeStdout / handleClaudeStream 遇到 {"type":"error"}(以及没有 result 就结束的情况)时,按父 Agent 失败处理:finishParent(true) 并关闭 stdin,让现有的 resume 兜底继续生效。
  2. 父 Agent 已经输出 result、还有子 Agent 没结束时,不要无限等待。可以设一个有上限的宽限时间,超时就关闭 stdin,交给现有的 drain;也可以在 SOP 里以「gate 已提交 + 父 result」作为结束条件。宽限时间建议可配置,并记一条遥测,说明子 Agent 被提前结束。
  3. 补两个用例,都用假 CLI:一个只输出 {"type":"error"} 然后读 stdin 直到 EOF;另一个输出父 result 后再也不报告子 Agent 完成。断言节点或会话这一轮会在限定时间内结束。

实测脚本(SOP 场景、会话场景)和日志都放在评审方本地归档里。

@Robert0622 Robert0622 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

817078d 复测:上一轮的两个 P1 都已修好,部署实测通过,Approve ✅

这次看 a9bf6106 → 817078d9 的变化:

  • a2b7aaf:父 Agent 结束后,最多再等 5 秒接收子 Agent 的完成通知,然后关闭 stdin,并记一条 agent.background.timeout 遥测。顶层 error 也算父 Agent 结束,这样 resume 失败后能走原来的「不带 resume 重启」兜底。Runner 的 executeSandboxProcess 和 executeHostCommand 加了同样的收尾时限。
  • 合并 main。
  • 817078d:只改测试。修好了 main 上 #1291 留下的 selectSOPExecutionTarget 少传参数的编译错误,并补了一个 e2e 用例,让路由覆盖率回到门槛以上。

实测时 817078d 还没推送。我实测的是 3e91085f,它和 817078d9 相比,除测试以外的代码完全一样。

实测环境:本机部署(Go 1.26.11,MySQL 8.4.7 全新库,平台以 all 角色运行,native 沙箱)。CodeBuddy CLI 2.187.0,iOA deepseek-v4.1-flash。另外注册了一台本机执行机器(Runner 0.12.6),用来测连接执行。没有对着本仓库做部署实测。子 Agent 的任务和上一轮一样,是 sleep 40; date +%s > child.txt;父 Agent 派发后立刻 date +%s > parent.txt。

场景 上一轮 a9bf610 本轮
P1-1 SOP 节点,父 Agent 不等子 Agent 直接结束(平台执行) 21 分钟后节点还是 running 父 Agent 14:19:24 输出 result,14:19:32 记录 agent.background.timeout(1 个子 Agent 没在 5 秒内报告完成),节点同时成功,下游节点接着执行 ✅
P1-1 同上,派到连接的执行机器上 — 父 Agent 14:24:56 结束,14:25:02 节点成功,Runner 日志显示 finished (success),下游节点在同一台机器上被领走 ✅
SOP:父 Agent 派发后先做自己的事,再用 TaskOutput 收集结果(主路径回归) 成功,父比子早 40 秒 75 秒成功,parent_before_child_by=41s,有 agent.background finish,没有超时事件 ✅
SOP:不用 Agent 工具的普通 CodeBuddy 节点 20 秒 20 秒成功 ✅
P1-2 项目会话调 workbench_workspace_open 交接到工作区 挂住(2 次复现) 27 秒、28 秒两次都回复「已打开」。平台日志有 2 条 cli.resume_fallback(CodeBuddy session transcript is missing),兜底生效 ✅
交接后,工作区里父 Agent 收集子 Agent 结果 无法测到 64 秒回复 PARENT_REPLY 并带上 CHILD_OK,父比子早 41 秒 ✅
交接后,工作区里父 Agent 不等子 Agent 无法测到 18 秒回复 PARENT_REPLY,生成状态 completed;下一轮 cat parent.txt child.txt 40 秒正常返回 ✅
测试 go vet ./internal/runtime ./internal/runner ./internal/httpapi ./internal/engine 通过。go test -race(runtime 跑 CodeBuddy|Background|Conversation|Stream|Protocol|Drain,runner 跑 Exec|CodeBuddy|Drain|Sandbox,httpapi 跑 Assistant|Prompt|Stdin|Workspace|CodeBuddy)没有 DATA RACE。失败的 8 个用例在 main 058a530b 上一样失败,是本机临时目录路径、没有 Chromium 和托管 CLI 目录导致的环境问题。817078d 上 go test ./internal/engine -run 'SOPExecution|Executor|AutoAssign' 通过

一个观察(不阻塞,建议后续看一下)

父 Agent 不等子 Agent 时,平台在宽限期后关掉 CLI,节点就结束了。但子 Agent 启动的 shell(sleep 40; date > child.txt)没有跟着退出,平台执行和执行机器上都是这样:节点结束约 20 秒后,child.txt 仍然写进了 run 工作区。这时下游节点已经开始跑了。

  • 本机没有 user namespace,bwrap 用的是测试替身,没有 PID 隔离。真实 bwrap 带 --unshare-pid --die-with-parent,这些进程应该会被一起清掉,所以本机的现象可能是环境造成的。
  • 不过在 Docker 沙箱里,同一个 run 的节点会复用同一个容器(reuse-run-sandbox 默认开启),残留的子进程可能会在下游节点运行时继续改工作区。建议确认 CLI 退出时会清理子 Agent 的进程组,或在 Docker 沙箱里测一下这个场景。

@crispinhu27
crispinhu27 merged commit 78cdab4 into main Nov 25, 2026
16 checks passed
@crispinhu27
crispinhu27 deleted the feature/codebuddy-workspace-background branch November 25, 2026 15:15
Robert0622 pushed a commit that referenced this pull request Nov 25, 2026
Four user-visible errors that arrived with main (#1342 work branch naming,
#1343/#1348 runner auto-assign, #1301 child artifacts) still said SOP.

Co-Authored-By: Claude Code <noreply@anthropic.com>
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.

2 participants