Skip to content

fix(core): wait for background jobs before reporting subagent result - #57410

Closed
jlongster wants to merge 1 commit into
v2from
subagent-wait-background
Closed

jlongster wants to merge 1 commit into
v2from
subagent-wait-background

Conversation

@jlongster

Copy link
Copy Markdown
Contributor

Problem

A subagent reported its final message to the parent as soon as its child session went idle, even if the child still had background work running (background shell commands, its own background subagents). When that work finished later, its notice woke the child again, but the parent had already received a stale result, and the child's real final answer was never reported.

Root cause

The subagent job's run effect (SubagentJob.start, and the restart-recovery equivalent in SessionRestart) was simply resume(child) followed by reading the latest assistant message. Nothing checked the background jobs the child owned.

Change

Adds SubagentCompletion.settle(jobs, childID, resume). It uses the existing durable background markers (Job.pendingBackground), keyed by owner: recovery.sessionID for shells and recovery.parentSessionID for subagents.

  • Resume the child.
  • If no markers are owned by the child, return right away. When there's no background work, nothing changes and nothing hangs.
  • Otherwise jobs.wait on the owned jobs that are still running, then repeat. A finished job's marker is only cleared after its notification has been admitted to the child's inbox, so the next resume drains that notice. The text reported to the parent is then the child's final message after all of its background work.
  • If only finished but not yet admitted markers remain, wait briefly (50 ms) before checking again, so the loop doesn't spin during the short gap before the notice is admitted.

Both the live subagent job and restart recovery now use settle.

Tests

  • New tool-subagent.test.ts case: the child owns a running background job, and the parent gets no completion until that job finishes and its notice is admitted. This test fails without the fix.
  • bun typecheck in packages/core. Ran tool-subagent, subagent-lifetime, session-execution, job, tool-shell and integration tests: all pass.

Risks

  • A subagent that starts a long-lived background process (such as a dev server) now holds back its result until that process exits. A foreground parent stays blocked until then, though the parent can still background or cancel it.
  • If a background notifier dies before clearing its marker, the subagent keeps re-checking every 50 ms and never reports.

@opencode-agent opencode-agent Bot closed this Oct 20, 2026
@opencode-agent

Copy link
Copy Markdown
Contributor

Thanks for this, @jlongster!

The v2 branch was promoted to dev on 2026-10-10, and v2 work now happens on dev. This pull request targets v2, which is no longer used, so the bot is closing it.

Please open it again with dev as the base. You can reuse the same branch: just create a new pull request against dev.

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.

1 participant