Skip to content

fix(subagents): drop the completion notification for an already collected result (#889) - #937

Merged
agegr merged 1 commit into
mainfrom
claude/friendly-ptolemy-5ryyr3
Sep 22, 2026
Merged

agegr merged 1 commit into
mainfrom
claude/friendly-ptolemy-5ryyr3

Conversation

@agegr

@agegr agegr commented Sep 22, 2026 •

Copy link
Copy Markdown
Owner

Fixes #889.

问题

后台子代理一完成就无条件通知父会话(lib/subagent-extension.ts):

if (execution.run.runInBackground) {
  void execution.completion.then((run) => runtime.notifyParent(run)).catch(...)

notifyParent() 里没有任何「父会话是否已经取走结果」的判断,且消息带 triggerTurn: true,因此主 Agent 会把已经通过 get_subagent_result 消费过的结果再处理一遍,真正的最终总结被挤离聊天底部。

为什么不能只加一个标志位

时间线决定了判断点必须落在投递时而不是 completion 解析时:

  1. execute() 结束 → 记录从 getSubagentRuns() 删除 → resolveCompletion(result);
  2. 下一个微任务就调用 notifyParent(),此时父会话通常仍卡在 get_subagent_result(wait: true) 里 → 消息进 followUp 队列;
  3. get_subagent_result 的轮询是 500ms 一次,之后才把同一份结果返回给主 Agent;
  4. 该 turn 结束 → followUp 投递 + 唤醒新 turn → 重复处理。

所以入口处检查一次标志位,在常见路径上标志根本还没被置起来。标记也不能挂在 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 test 1259 通过 / 0 失败,npx tsc --noEmit 无输出,npm run lint 干净。

🤖 Generated with Claude Code

https://claude.ai/code/session_01Nu1KdXrNyzgD7m7mqdEybc

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 22, 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-09-22T13:36:39.494029Z 85f9a4a 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.

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

Comment thread lib/subagent-extension.ts
}
// 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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge 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
@agegr
agegr force-pushed the claude/friendly-ptolemy-5ryyr3 branch from 85f9a4a to e9b3f47 Compare September 22, 2026 13:39
@agegr
agegr merged commit 12d3599 into main Sep 22, 2026
2 checks passed
@agegr
agegr deleted the claude/friendly-ptolemy-5ryyr3 branch October 1, 2026 13:28
windli2018 pushed a commit to aiti-ai/aiti-ai that referenced this pull request Nov 6, 2026
…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]>
windli2018 pushed a commit to aiti-ai/aiti-ai that referenced this pull request Nov 8, 2026
…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]>
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.

get_subagent_result 返回结果后,主 Agent 还会收到重复的 subagent-notification

2 participants