Repository navigation
fix(sessions): subagents sent to the background are not done - #6
Merged
Merged
Conversation
Claude Code ends the turn that sends subagents to the background, with its Stop hook, but its registry stays "busy" until they finish. The island took the end for the last word and showed the session idle, with a notice that it finished, while Claude waited for them. A subagent launched in the background is now followed like a command left running there, from its launch to the notice of its end, and keeps the session In background, named by what it does, while the registry says busy. The Stop hook reads the session's latest lines first, so an agent sent off just before it is seen. A background command refused before it ran no longer counts as running.
MF-DEEV
marked this pull request as ready for review
October 22, 2026 14:24
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A session whose turn sent subagents to the background showed as Idle, with a notice that it finished, while Claude was still waiting for them ("Waiting for 2 background agents to finish" in the terminal). It now shows In background, named by the agent it waits for, until the last one is done.
Why it happened
Claude Code 2.1.308 writes its registry status as
isLoading || delegatedActive ? "busy" : "idle"(read from its binary). A running background agent counts as delegated work, so the registry stays"busy"."shell"is used only for background Bash. The turn that sent the agents still ends withend_turnand the Stop hook, and the registry is not rewritten at that moment. The engine saw those facts as newer than the registry and took the turn for done.The
Agentresult of a background launch carriestoolUseResultisAsync: true,status: "async_launched",agentIdanddescription. The agent's end comes as a<task-notification>naming thatagentId, which the reader already handled.Change
Agentresult becomes.backgroundStarted(service: false)plus.backgroundTask(task: agentId), so the agent is tracked like a command left running, from launch to its end notice. The label is the agent's description, and only when previews are on.busyafter the turn ended and a background job with an id is still running, the session is.background. Once the registry goes idle the hold is released, so an end the reader missed can't keep a session stuck. Services left running beside it are counted ("N left running").run_in_backgroundBash call refused before it ran, for example by the auto-mode classifier, gets no task id and never ends. It is now closed when its call fails. Without this, the new rule would make every later turn's end flicker to In background and lose its finished notice. Four such calls are in real transcripts.Testing
./build.sh --dev --test-suite=sessions: 326 checks pass. The new engine and reader cases fail without the fix: two background agents across two notification turns, previews off, a foreground agent, a missed end, services beside an agent, and a refused background command.busy:finishedand a notice went out.background. After the first agent it is stillbackground, labelled with the second agent. When the second agent's notice arrives, the session is generating again../build.sh --dev --test: 114,086 checks. One fails,NotchCompactTests.swift:337(clipboard rail realization). That test is flaky: on the same code it passed and failed in back-to-back runs, and this change doesn't touch it.Not covered
AgentUsage/Harness/ClaudeReader.swift) still closes the turn onend_turn, so it shows such sessions as done. That gap was there before this change and needs its own fix.