Skip to content

fix(core): wait for subagent background jobs before completing - #57132

Draft
kitlangton wants to merge 1 commit into
devfrom
subagent-background-shell
Draft

kitlangton wants to merge 1 commit into
devfrom
subagent-background-shell

Conversation

@kitlangton

Copy link
Copy Markdown
Contributor

Issue for this PR

Closes #

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

When a subagent launches a background shell (shell with background: true), BACKGROUND_INSTRUCTION in packages/core/src/tool/plugin/shell.ts tells it:

If you have nothing else to do, end your response; you will be resumed automatically when the command finishes.

SubagentJob.start (packages/core/src/session/subagent-job.ts) and SessionRestart.recoverSubagent (packages/core/src/session/execution/restart.ts) only waited for a single sessions.resume(recovery.childSessionID) busy period before settling the subagent job and delivering <subagent state="completed"> to the parent. When a subagent started a background shell and ended its turn as instructed—for example replying "The script is still working through the worktrees (1 of 48 done). I'll pick this up when it completes" or "Waiting for the before/after recording on mini before I write the todo note"—the parent immediately received state="completed" with that interim promise, and when the background shell later finished and woke the child session, no subagent job or observer was left to deliver the final result to the parent.

This adds Job.awaitBackground(sessionID) (packages/core/src/job.ts), which resolves when any in-flight background job whose completion notifies sessionID acknowledges completeBackground (after sessions.synthetic has enqueued the notification and woken sessionID). SubagentJob and SessionRestart.recoverSubagent now loop until the child session is idle and owns no pending background notifications before reading its final assistant response and settling the subagent job.

How did you verify your code works?

  • Added targeted foreground and background tests in packages/core/test/tool-subagent.test.ts where a child subagent starts a background: true shell, ends its turn with an interim message while the shell is still running, gets woken when the shell finishes, and delivers its final response to the parent.
  • Ran bun run script/test.ts test/tool-subagent.test.ts test/subagent-lifetime.test.ts test/job.test.ts test/session-execution.test.ts and bun typecheck in packages/core.

Screenshots / recordings

N/A

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant