Skip to content

fix(subagents): key the collected-result mark by run, not session (#987) - #989

Merged
agegr merged 2 commits into
agegr:mainfrom
Light1002:fix/subagent-stale-consumed-mark
Sep 30, 2026
Merged

agegr merged 2 commits into
agegr:mainfrom
Light1002:fix/subagent-stale-consumed-mark

Conversation

@Light1002

@Light1002 Light1002 commented Sep 29, 2026 •

Copy link
Copy Markdown

Fixes #987. The issue has the real-session evidence (154 background dispatches, 106 without a notification) and the full mechanism; this PR is the fix it suggests.

Problem

#937 (for #889) made get_subagent_result mark every terminal background result it returns, and notifyParent drops a notification when it can take that mark. The mark is keyed only by the child session id, and only notifyParent ever removes it.

When the parent polls after the notification was already delivered, the mark stays behind with nothing to take it. resume reruns the same session id, so the next run's notifyParent takes that stale mark and returns without notifying. The parent then waits for a notification that never comes. A human nudge usually makes the parent poll again, which leaves a fresh stale mark, so the loss repeats on every later run.

run 1 ends → notifyParent: no mark → delivered ✅
parent calls get_subagent_result → mark(session)      // nothing will ever take it
resume → run 2 ends → notifyParent takes run 1's mark → return ❌

Change

  • lib/subagent-runtime.ts: the mark stores the collected run's completedAt (Map<sessionId, completedAt>), and takeResultConsumed(run) only matches the same run. A resumed run always gets a new completedAt, so a mark left by an earlier run can no longer match it. resume deliberately does not clear the mark: the parent can collect a run and resume it in the same turn while that run's notification is still held, and the mark must keep suppressing it. The globalThis getter replaces a pre-change Set left behind by hot reload.
  • lib/subagent-extension.ts: get_subagent_result passes the run instead of the bare session id.
  • AGENTS.md: one sentence added to the existing notification note.

#889 behaves as before: a result the parent collects while that same run's notification is held still suppresses it.

Tests

npx tsc --noEmit and npm run lint are clean. npm test: all subagent tests pass. The only failures are the existing Windows-host ones (CRLF / PTY / path delimiter), and they fail on main too.

…egr#987)

get_subagent_result marks every terminal background result it returns,
but only notifyParent ever takes that mark. When the parent polls after
the notification was already delivered, the mark stays behind keyed by
the child session id. resume reruns the same session id, so that stale
mark swallowed the next run's completion notification and left the
parent waiting for a notification that never came.

Record the collected run's completedAt with the mark and only drop a
notification whose run matches it. A resumed run always gets a new
completedAt, so a mark left by an earlier run can no longer match it.
resume deliberately leaves the mark in place: the parent can collect a
run and resume it in the same turn while that run's notification is
still held, and the mark must keep suppressing it (agegr#889).
…ed failure

The collected-result mark is set for every terminal background result,
failed ones included, so the agegr#987 loss also hits a retry: run 1 fails,
the parent inspects the failure with get_subagent_result, resumes the
child, and the retry is rejected by the provider at once. On main the
retry's failure notification was taken by run 1's mark and the parent
kept waiting.

Lock that path in: the retry's failure must still reach the parent.
@agegr

agegr commented Sep 30, 2026

Copy link
Copy Markdown
Owner

Reviewed. #987 is real, and the mechanism is exactly as described: notifyParent is the only thing that removes a mark, so a get_subagent_result poll after a delivered notification leaves a mark behind, and resume reuses the session id, so the next run's notifyParent takes it and returns silently.

What I checked:

  • The run identity is stable. completedAt is set once per run and persisted verbatim in the pi-web:subagent-result entry, so the value get_subagent_result sees (from the run table or read back from the file) is the same string notifyParent receives. A resumed run always gets a new one.
  • get_subagent_result 返回结果后,主 Agent 还会收到重复的 subagent-notification #889 still holds. Collecting a run while its notification is held still suppresses it, including the collect-then-resume-in-the-same-turn case. Keeping the mark across resume is what makes that work.
  • Bounded. One entry per session, and interrupted runs (no completedAt, never notified) are not marked.
  • Hot reload. Replacing a leftover Set on globalThis avoids calling Map methods on it.

The new tests fail against main's runtime and pass here. I also merged #989, #990 and #991 together onto current main: no conflicts between them, and tsc --noEmit, eslint, and the full unit suite pass (the one failure, worktree.test.mjs force removal, also fails on main in my macOS temp dir). LGTM 👍


Generated by Claude Code

@agegr
agegr merged commit c8ff7e8 into agegr:main Sep 30, 2026
Kano-u pushed a commit to Kano-u/pi-web that referenced this pull request Oct 2, 2026
…gr#976 agegr#974 agegr#989 agegr#990 agegr#991)

这 8 个提交与我们的分叉没有文本冲突,直接并入:
- agegr#1011 回合答案出现后折叠过程详情
- agegr#1012 移动端打开模型选择器不弹键盘
- agegr#972/agegr#976 行内链接在 CJK 标点处停止、行内公式与货币格式
- agegr#974 首条消息前即报告 system prompt
- agegr#989/agegr#990/agegr#991 子代理:收集标记按 run 计、重启后的中断报告、续跑背景报告标注
@Light1002
Light1002 deleted the fix/subagent-stale-consumed-mark branch October 17, 2026 12:38
windli2018 pushed a commit to aiti-ai/aiti-ai that referenced this pull request Nov 6, 2026
…egr#987) (agegr#989)

* fix(subagents): key the collected-result mark by run, not session (agegr#987)

get_subagent_result marks every terminal background result it returns,
but only notifyParent ever takes that mark. When the parent polls after
the notification was already delivered, the mark stays behind keyed by
the child session id. resume reruns the same session id, so that stale
mark swallowed the next run's completion notification and left the
parent waiting for a notification that never came.

Record the collected run's completedAt with the mark and only drop a
notification whose run matches it. A resumed run always gets a new
completedAt, so a mark left by an earlier run can no longer match it.
resume deliberately leaves the mark in place: the parent can collect a
run and resume it in the same turn while that run's notification is
still held, and the mark must keep suppressing it (agegr#889).

* test(subagents): cover a resumed run that fails right after a collected failure

The collected-result mark is set for every terminal background result,
failed ones included, so the agegr#987 loss also hits a retry: run 1 fails,
the parent inspects the failure with get_subagent_result, resumes the
child, and the retry is rejected by the provider at once. On main the
retry's failure notification was taken by run 1's mark and the parent
kept waiting.

Lock that path in: the retry's failure must still reach the parent.

---------

Co-authored-by: CLAY777 <[email protected]>
windli2018 pushed a commit to aiti-ai/aiti-ai that referenced this pull request Nov 8, 2026
…egr#987) (agegr#989)

* fix(subagents): key the collected-result mark by run, not session (agegr#987)

get_subagent_result marks every terminal background result it returns,
but only notifyParent ever takes that mark. When the parent polls after
the notification was already delivered, the mark stays behind keyed by
the child session id. resume reruns the same session id, so that stale
mark swallowed the next run's completion notification and left the
parent waiting for a notification that never came.

Record the collected run's completedAt with the mark and only drop a
notification whose run matches it. A resumed run always gets a new
completedAt, so a mark left by an earlier run can no longer match it.
resume deliberately leaves the mark in place: the parent can collect a
run and resume it in the same turn while that run's notification is
still held, and the mark must keep suppressing it (agegr#889).

* test(subagents): cover a resumed run that fails right after a collected failure

The collected-result mark is set for every terminal background result,
failed ones included, so the agegr#987 loss also hits a retry: run 1 fails,
the parent inspects the failure with get_subagent_result, resumes the
child, and the retry is rejected by the provider at once. On main the
retry's failure notification was taken by run 1's mark and the parent
kept waiting.

Lock that path in: the retry's failure must still reach the parent.

---------

Co-authored-by: CLAY777 <[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.

Background subagent completion is silently dropped for the next run of the same subagent (parent stays idle until a human nudges it)

2 participants