claude-code icon indicating copy to clipboard operation
claude-code copied to clipboard

SubagentHandback is accepted while the subagent's own background children are still running, so the parent sees an unfinished subagent as done

Open grandmasteri opened this issue 1 month ago • 2 comments

What happens

A subagent can call SubagentHandback while its own background children are still running. The call succeeds and the parent receives the report straight away, with nothing marking it as interim. To the parent, the subagent looks finished while its work is still in flight.

When the child finishes, the subagent is re-invoked, but a second SubagentHandback is refused:

Nothing was sent: your report was already delivered (SubagentHandback delivers one report). Use SendMessage for anything further, then stop.

So the subagent's real result can only reach the parent by SendMessage, through a different channel from a normal report.

The harness already handles the completion signal correctly: the parent's task notification for the subagent is held until the subagent has no live background children. Only the hand-back gets through early.

Repro

  1. From the main session, spawn subagent S in the background with the Agent tool.
  2. In S, spawn child C with the Agent tool, run_in_background: true. C runs sleep 60; echo GC-DONE in the foreground and reports.
  3. In S, immediately call SubagentHandback with "EARLY".

Observed:

  • The SubagentHandback call returns {"success":true,"message":"Report delivered to your caller."}. The main session receives EARLY as S's report about 60 s before C finishes.
  • The task notification for S arrives only after C finishes, which is correct.
  • C's report re-invokes S. S's second SubagentHandback is refused with the text above, so S sends its real result to main by SendMessage.

Control: in the same setup, if S ends its turn with plain text instead of calling SubagentHandback, the main session receives nothing until C finishes. S is then re-invoked by C's report and delivers its real report normally. The same holds when the child is a background Bash command instead of an Agent.

Expected

One of these:

  • SubagentHandback refuses while the caller has live background children, with a message telling the caller to end its turn and wait;
  • the report is delivered marked as interim;
  • the report is held until the children finish, the same way the task notification already is.

Impact

A manager agent that orchestrates mid-layer subagents treats a subagent as done when it is not. The mid-layer agent's real result then arrives later by SendMessage. In practice this pushed our agents into keeping their turn open with long sleep calls while waiting on children, so that they would not hand back early. That wastes wall-clock time and context, and ending the turn would otherwise work.

Environment

  • Claude Code 2.1.303, Linux
  • Background subagents spawned with the Agent tool, auto mode

Related

  • #102867: the hand-back enforcer forces SubagentHandback while a subagent waits on its own Monitor. That is the reverse side of the same gap, since there the hand-back is forced early.
  • #96849: a second SubagentHandback after resume is rejected.

grandmasteri avatar Oct 19 '26 23:10 grandmasteri

A related variant, seen on the same machine with a long-lived orchestrating subagent (Claude Code 2.1.288 and 2.1.296 builds).

Here the early hand-back is not the agent's choice. The harness injects the meta prompt:

[handback-send-enforce] Your report has not been delivered. Call SubagentHandback({message: }) now; the call ends your run.

It does this while the subagent still has its own background subagent running. It fired 11 times in one orchestrating subagent's transcript in a single day. Each time the agent complied, its run ended before its child finished. The child's report then reached the main session rather than the agent that spawned it, and had to be relayed back by hand.

The trigger is narrower than "ended its turn with a live child". In a minimal probe on 2.1.296, a subagent ended its turn with plain text while a background subagent child was still running. It was not prompted; the child's report re-invoked it, as expected. The long-lived agent differs in that it had been resumed many times by SendMessage and ran for hours. Which of those matters is not established.

Expected: the hand-back enforcer should not fire while the caller has live background children, for the same reason as the Monitor case in #102867.

Workaround we use meanwhile: when the enforcer fires with a child still running, the agent hands back a report marked "INTERIM: still running". It sends the real result to its manager by SendMessage once the child's report wakes it.

grandmasteri avatar Oct 21 '26 15:10 grandmasteri

The trigger for the forced variant in the comment above: the child's interim notification. Reproduced on Claude Code 2.1.310 (macOS, auto mode).

When a background child ends its turn while its own background work is still running, the parent receives the child's interim notification ("is waiting on its own background work"). From then on, the parent's turn end no longer counts that child as live, and [handback-send-enforce] fires as if the parent had no live children. A child that ends its turn with plain text and no background work of its own does not trigger it, which matches the minimal 2.1.296 probe above, where nothing fired.

Repro

In the main session, ask for a background general-purpose agent R:

Spawn two background general-purpose agents, R.1 and R.2. Each one runs sleep 600 with run_in_background: true, ends its turn, and reports when the sleep finishes. Do not run any background command yourself. When both have reported, report "R done" with their results.

From R's transcript:

time (UTC) event
02:58:03, 02:58:05 R spawns R.1 and R.2 in the background and ends its turn
02:58:22, 02:58:24 R receives each child's interim notification
02:58:26 [handback-send-enforce] Your report has not been delivered. Call SubagentHandback(...)
02:58:29 R hands back an incomplete report. Its transcript has no later row
03:08:1x R.1 and R.2 finish; their final reports go to the main session, since R has ended

ListAgents listed every node as completed at that point, while their sleep commands were still running.

R has no background job of its own, so nothing wakes it again: whatever it had left to do after its children (for a plan executor, its remaining milestones) is lost unless the main session resumes it with SendMessage. A parent that does have its own background job shows the milder form: when its job finishes it is forced to hand back a partial report while its children still run, then hands back 2–3 times as they report.

Dropping the interim notification with a prompt.submit hook does not help. The child is already counted as notified when the notification is queued, before any hook runs.

How often

Over seven days of one user's transcripts, 364 subagents spawned children of their own. 51 of them (about 7 a day) were forced to hand back while a child was still running. Of those 51, 42 continued only after the main session resumed them with SendMessage, 3 were woken by their own job or a child's notice, and 6 never ran again.

Expected

A child that is waiting on its own background work should keep its parent alive until it really ends, as it does before its interim notification is sent. The enforcer should not fire while any child is in that state, the same fix as for #102867.

SSFSKIM avatar Oct 25 '26 09:10 SSFSKIM