SIGN IN SIGN UP

agent host: drive subagent cleanup from SDK completion events (#315880)

* agent host: drive subagent cleanup from SDK completion events

- Background subagents (e.g. Copilot's `task` tool with `mode: background`)
  continue running after their parent tool call returns. Previously we
  tore down the subagent session as soon as the parent tool's
  `SessionToolCallComplete` was dispatched, which dropped all later
  subagent events on the floor. Any `tool.execution_start` the subagent
  emitted afterwards (e.g. a `problems` call needing confirmation) got
  buffered indefinitely and never reached AHP, leaving the UI hung on
  confirmation.
- Introduces a `subagent_completed` agent signal fired from the SDK's
  `subagent.completed` and `subagent.failed` events, and routes that
  signal to `completeSubagentSession`. The parent tool completion path
  now only drops the pending pre-start signal buffer, which keeps the
  "subagent never started" cleanup path intact without prematurely
  closing a still-running subagent.

Fixes #314827

(Commit message generated by Copilot)

* test: mock agent emits subagent_completed after inner tool

The integration mock's `subagent` prompt previously relied on the
parent `task` tool's `SessionToolCallComplete` to tear down the
child session. With the subagent lifecycle now driven by the SDK's
`subagent.completed` event, the mock must mirror that and emit
`subagent_completed` itself, otherwise the child turn never
finalizes and `turnExecution.integrationTest.ts` fails on
`child subagent session should have at least one turn`.
C
Connor Peet committed
ba66f874db23163e98cecd1412fc4d82d859b7b7
Parent: 06542e1
Committed by GitHub <noreply@github.com> on 5/11/2026, 10:12:24 PM