Repository navigation
fix(subagents): drop the completion notification for an already collected result (#889) - #937
Conversation
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. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 85f9a4acb5
ℹ️ 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".
| } | ||
| // The parent now holds this result, so the background completion notification must not | ||
| // deliver the same text again and wake a duplicate turn. | ||
| if (run.runInBackground && TERMINAL_SUBAGENT_STATUSES.has(run.status)) runtime.markResultConsumed(run.sessionId); |
There was a problem hiding this comment.
Scope consumed marks to the specific completion
If get_subagent_result is called after the original notification was already delivered—or for an interrupted run that has no notifier—this leaves a consumed mark keyed only by the session ID. Resuming that background subagent reuses the same ID, so the new completion's notifyParent consumes the stale mark and silently suppresses the new result; track consumption by completion/run identity or only mark results with a pending notification.
AGENTS.md reference: AGENTS.md:L231-L231
Useful? React with 👍 / 👎.
…cted result (#889) A background subagent unconditionally notified its parent when it finished, even when the parent had already taken the same result through `get_subagent_result`. The notification is sent with `triggerTurn: true`, so the parent woke a new turn and processed the result a second time, pushing its real final summary away from the bottom of the chat. `get_subagent_result` now marks a finished background run as consumed, and `notifyParent` takes that mark instead of sending. Checking it only when the completion promise resolves would not help: at that moment the parent is usually still inside its `get_subagent_result` poll (500ms interval), and `deliverAs: "followUp"` would queue the duplicate until that turn ends. So the notification is held while the parent `isRunning()` and the mark is re-checked before sending. An idle parent is still notified immediately, and a result the parent never collected is still delivered once the parent goes idle. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Nu1KdXrNyzgD7m7mqdEybc
85f9a4a to
e9b3f47
Compare
…cted result (agegr#889) (agegr#937) A background subagent unconditionally notified its parent when it finished, even when the parent had already taken the same result through `get_subagent_result`. The notification is sent with `triggerTurn: true`, so the parent woke a new turn and processed the result a second time, pushing its real final summary away from the bottom of the chat. `get_subagent_result` now marks a finished background run as consumed, and `notifyParent` takes that mark instead of sending. Checking it only when the completion promise resolves would not help: at that moment the parent is usually still inside its `get_subagent_result` poll (500ms interval), and `deliverAs: "followUp"` would queue the duplicate until that turn ends. So the notification is held while the parent `isRunning()` and the mark is re-checked before sending. An idle parent is still notified immediately, and a result the parent never collected is still delivered once the parent goes idle. Claude-Session: https://claude.ai/code/session_01Nu1KdXrNyzgD7m7mqdEybc Co-authored-by: Claude <[email protected]>
…cted result (agegr#889) (agegr#937) A background subagent unconditionally notified its parent when it finished, even when the parent had already taken the same result through `get_subagent_result`. The notification is sent with `triggerTurn: true`, so the parent woke a new turn and processed the result a second time, pushing its real final summary away from the bottom of the chat. `get_subagent_result` now marks a finished background run as consumed, and `notifyParent` takes that mark instead of sending. Checking it only when the completion promise resolves would not help: at that moment the parent is usually still inside its `get_subagent_result` poll (500ms interval), and `deliverAs: "followUp"` would queue the duplicate until that turn ends. So the notification is held while the parent `isRunning()` and the mark is re-checked before sending. An idle parent is still notified immediately, and a result the parent never collected is still delivered once the parent goes idle. Claude-Session: https://claude.ai/code/session_01Nu1KdXrNyzgD7m7mqdEybc Co-authored-by: Claude <[email protected]>
Fixes #889.
问题
后台子代理一完成就无条件通知父会话(
lib/subagent-extension.ts):notifyParent()里没有任何「父会话是否已经取走结果」的判断,且消息带triggerTurn: true,因此主 Agent 会把已经通过get_subagent_result消费过的结果再处理一遍,真正的最终总结被挤离聊天底部。为什么不能只加一个标志位
时间线决定了判断点必须落在投递时而不是 completion 解析时:
execute()结束 → 记录从getSubagentRuns()删除 →resolveCompletion(result);notifyParent(),此时父会话通常仍卡在get_subagent_result(wait: true)里 → 消息进 followUp 队列;get_subagent_result的轮询是 500ms 一次,之后才把同一份结果返回给主 Agent;所以入口处检查一次标志位,在常见路径上标志根本还没被置起来。标记也不能挂在
stored.run上——终态时它已经从 map 里删掉了。改动
lib/subagent-extension.ts:SubagentExtensionRuntime增加markResultConsumed(sessionId);get_subagent_result在返回终态(completed / failed / aborted / interrupted)且runInBackground为真时打标记。只标后台运行——前台运行永远不通知,标了没人清。lib/subagent-runtime.ts:新增globalThis.__piSubagentConsumedResults(与现有__piSubagentRuns/__piSubagentQueue同模式)。notifyParent()入口取一次标记;随后在父会话isRunning()期间以 200ms 轮询挂起,每轮重取;退出循环后投递前再取一次。空闲的父会话行为完全不变:立即通知、立即唤醒。AGENTS.md:在「Built-in subagents」下记录这条取舍。与 #935 的
SUBAGENT_NOTIFICATION_PREFIX正交:本 PR 决定是否投递,那条决定投递内容如何被标记。分支已 rebase 到含 #935 / #936 的main。行为变化
父会话正忙时通知推迟到其空闲。由于
deliverAs: "followUp"本来也要等 turn 结束才投递,实际到达时间几乎不变。isRunning()还包含isBashRunning,所以用户手动跑 bash 期间通知同样等待——那个时机注入 turn 本来也不合适。未被取走的结果依旧会在父会话空闲后正常送达。测试
新增 6 个用例:
lib/subagent-runtime.test.mjs:已消费 → 不投递(且标记是「取走」语义,同一 session ID 的下一次运行仍会通知);父会话忙碌期间被消费 → 等待结束后跳过;父会话忙碌但无人消费 → 转空闲后正常投递一次,内容用SUBAGENT_NOTIFICATION_PREFIX拼接,deliverAs/triggerTurn不变。lib/subagent-extension.test.mjs:后台终态结果 → 打标记;仍在 running → 不打;前台终态 → 不打。验证(rebase 后):
npm test1259 通过 / 0 失败,npx tsc --noEmit无输出,npm run lint干净。🤖 Generated with Claude Code
https://claude.ai/code/session_01Nu1KdXrNyzgD7m7mqdEybc