Skip to content

fix(subagents): report a run orphaned by a restart as interrupted - #990

Merged
agegr merged 1 commit into
agegr:mainfrom
Light1002:fix/subagent-orphaned-running-status
Sep 30, 2026
Merged

agegr merged 1 commit into
agegr:mainfrom
Light1002:fix/subagent-orphaned-running-status

Conversation

@Light1002

Copy link
Copy Markdown

Found while reviewing the background-notification path for #987; it is a separate bug with a separate fix.

Problem

A background run appends a pi-web:subagent-status running entry when it starts and a pi-web:subagent-result entry when it ends. If the pi-web process stops in between (restart, crash, pi-web upgrade), the child session is left with running as its last lifecycle entry and no result.

After the restart, get() returns that entry verbatim, so:

  • get_subagent_result({ wait: true }) polls forever, and the parent turn never ends;
  • no completion notification will ever arrive, because the run that would have sent it is gone.

Change

lib/subagent-runtime.ts: a run stays in getSubagentRuns() from dispatch until its result entry is written, on every path (completed, failed, aborted, queued-abort). So when get() reads a persisted running / queued status without a table entry and without a running wrapper, the run can never finish. get() now reports it as interrupted, a status subagentFinalText already renders ("was interrupted before completion"). The wait loop returns, and the parent can resume the child.

Only get() changes. The session list and readSubagentRun are untouched, so nothing is written to the session file and no notification or turn is triggered.

Tests

  • A live but idle child with a persisted running or queued status and no active run → interrupted.
  • A child whose wrapper is still running → still running.
  • A child session read back from a real .jsonl after a simulated restart → interrupted.

The two interrupted cases fail on main and pass with the change. tsc and lint are clean. npm test: the only failures are the existing Windows-host ones, and they fail on main too.

A background run writes a "running" status entry when it starts and a
result entry when it ends. If the pi-web process stops in between, the
child session is left with "running" as its last lifecycle entry and no
result. After a restart get() read that entry verbatim, so the run
reported "running" forever and get_subagent_result({ wait: true }) never
returned, leaving the parent stuck.

A run stays in the in-memory run table from dispatch until its result
entry is written, so a persisted running or queued status that reaches
get() without a table entry and without a running wrapper can never
finish. Report it as interrupted, the status the tool already renders
("was interrupted before completion"), so the wait returns and the run
can be resumed.
@agegr

agegr commented Sep 30, 2026

Copy link
Copy Markdown
Owner

Reviewed. The bug is real: after a restart, a background child whose last lifecycle entry is running has no owner, and get() returned that entry as is, so get_subagent_result({ wait: true }) never returned.

What I checked:

  • The invariant holds on every path. A run is put in getSubagentRuns() before its running / queued status entry is written, and it is removed only after its result entry is appended: completed, failed, and provider errors all go through the try/catch in execute, plus the pre-start abort, the queued abort, and the same three paths in resume. So a persisted running with no table entry and no running wrapper can only be left over from a process that stopped.
  • Read-only. Only get() reports interrupted. Nothing is written, no notification or turn is triggered, and resume can pick the child up again. The sidebar keeps showing the persisted status, which is fine for this fix.

The two interrupted tests fail against main's runtime and pass here. I also merged #989, #990 and #991 together onto current main: no conflicts, and tsc --noEmit, eslint, and the full unit suite pass (same pre-existing macOS-only worktree.test.mjs failure as main). LGTM 👍


Generated by Claude Code

@agegr
agegr merged commit 00156d5 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-orphaned-running-status branch October 17, 2026 12:38
windli2018 pushed a commit to aiti-ai/aiti-ai that referenced this pull request Nov 6, 2026
…egr#990)

A background run writes a "running" status entry when it starts and a
result entry when it ends. If the pi-web process stops in between, the
child session is left with "running" as its last lifecycle entry and no
result. After a restart get() read that entry verbatim, so the run
reported "running" forever and get_subagent_result({ wait: true }) never
returned, leaving the parent stuck.

A run stays in the in-memory run table from dispatch until its result
entry is written, so a persisted running or queued status that reaches
get() without a table entry and without a running wrapper can never
finish. Report it as interrupted, the status the tool already renders
("was interrupted before completion"), so the wait returns and the run
can be resumed.

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

A background run writes a "running" status entry when it starts and a
result entry when it ends. If the pi-web process stops in between, the
child session is left with "running" as its last lifecycle entry and no
result. After a restart get() read that entry verbatim, so the run
reported "running" forever and get_subagent_result({ wait: true }) never
returned, leaving the parent stuck.

A run stays in the in-memory run table from dispatch until its result
entry is written, so a persisted running or queued status that reaches
get() without a table entry and without a running wrapper can never
finish. Report it as interrupted, the status the tool already renders
("was interrupted before completion"), so the wait returns and the run
can be resumed.

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.

2 participants