Skip to content

fix(subagents): busy 期间完成的结果也会自动唤醒父 Agent - #48

Merged
tt-a1i merged 2 commits into
mainfrom
fix-subagent-lost-wakeup
Aug 21, 2026
Merged

fix(subagents): busy 期间完成的结果也会自动唤醒父 Agent#48
tt-a1i merged 2 commits into
mainfrom
fix-subagent-lost-wakeup

Conversation

@tt-a1i

@tt-a1i tt-a1i commented Aug 21, 2026

Copy link
Copy Markdown
Collaborator

Closes #47

根因

这不是 Windows 专属问题,也不是 agent_settled 事件偶发丢失,而是旧投递策略在下列时序里确定不会唤醒

子代理 B 在父 Agent 忙于处理 A 时完成
→ onSettled: B 放进 pending Map,isIdle=false
→ 父 Agent 完成,触发 agent_settled
→ 旧代码固定 flushResults(false)
→ { deliverAs: "nextTurn" }
→ Pi 等下一次用户 prompt 才注入 B

Pi core 在 agent_settled 前先把 _isAgentRunActive 置为 false;但 OpenPI 没有在这个权威 idle 边界重新选择 wake,而是主动把整批结果降级成 nextTurn。这与 README 和 subagent_spawn 工具说明中的「完成后自动回传并重新唤醒主 Agent」契约冲突。

Windows 审计确认:OpenPI 子代理是同一 Node 进程内的 Pi Session,这条链路没有 process.platform 分支、外部子进程或 session 文件 watcher。Windows 只可能改变任务耗时,让该时序更常出现。

修复:双边无丢唤醒协议,不加 sleep

新增小型 createSubagentResultDelivery 协调器:

  1. child 在 parent 已 idle 后完成 → 立即 followUp + triggerTurn
  2. child 在 parent busy 时完成 → 保留在按 id 键控的 Map;
  3. parent 进入 agent_settled → 把 pending 结果合成一条 follow-up 并唤醒;
  4. 若前一个 extension handler 已经启动另一轮,Pi 会把 follow-up 排进该活动 run,仍不会丢;
  5. subagent_wait 在 flush 前仍可 consume,不会重复投递;Map drain 是 one-shot gate;
  6. 同步 session teardown 导致 delivery 抛错 → 原批次按原顺序恢复,下一边界可重试。

所有未被显式 subagent_wait 消费的后台结果都遵循同一合同:在父 Agent 的 idle 边界使用 followUp + triggerTurn 自动回传。用户中止不会把结果降级为 nextTurn,否则会重新引入「必须再发一条消息才收到结果」的问题。

可见报告仍由 session entry 在真实完成位置显示;注入模型的 message 保持 display:false,不会重复出现。

测试

纯状态机和 wiring 测试覆盖:

  • idle child-settle 立即唤醒;
  • busy child-settle → parent agent_settled → 自动唤醒在进行code review双轨审查的时候主代理无法被后面完成的那个子代理唤醒 #47);
  • 另一 extension 抢先启动 run 时,结果仍 follow-up 进该 run;
  • 多 reviewer 按完成顺序合并为一个父 turn;
  • subagent_wait consume 后不投递;
  • child/parent 两个边界都检查时不重复;
  • wake turn 内又完成的 child 延迟到下一边界;
  • 同步投递失败恢复原批次及顺序;
  • dispatcher 固定使用 followUp + triggerTurn

验证:

  • bun run test745 node tests + 29 vitest 全绿
  • bun run check:退出 0(仅有 file-search/binaries.ts 既存 Effect warnings)

tt-a1i added 2 commits August 22, 2026 00:58
Results that settled while the parent was busy were deliberately flushed as
nextTurn at agent_settled. Pi only consumes that queue on another user prompt,
so fire-and-forget results could not fulfill their documented automatic
re-invocation contract.

Flush the pending batch as one follow-up at the authoritative parent boundary,
while preserving wait consumption, exactly-once delivery, synchronous retry,
and aborted-turn suppression.

Closes #47
@tt-a1i
tt-a1i merged commit 97cc3ff into main Aug 21, 2026
4 checks passed
@tt-a1i
tt-a1i deleted the fix-subagent-lost-wakeup branch August 21, 2026 17:56
@tt-a1i tt-a1i mentioned this pull request Aug 21, 2026
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.

在进行code review双轨审查的时候主代理无法被后面完成的那个子代理唤醒

1 participant