Skip to content

fix(sessions): subagents sent to the background are not done - #6

Merged
MF-DEEV merged 1 commit into
mainfrom
feat/claude-sessions
Oct 22, 2026
Merged

MF-DEEV merged 1 commit into
mainfrom
feat/claude-sessions

Conversation

@MF-DEEV

@MF-DEEV MF-DEEV commented Oct 22, 2026

Copy link
Copy Markdown
Owner

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 with end_turn and 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 Agent result of a background launch carries toolUseResult isAsync: true, status: "async_launched", agentId and description. The agent's end comes as a <task-notification> naming that agentId, which the reader already handled.

Change

  • Reader. An async Agent result 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.
  • Phase. If the registry says busy after 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").
  • Stop hook. The service reads the session's transcript before taking the Stop hook. An agent sent off less than a tick before the turn ended is then known before any notice calls the turn done.
  • Refused background commands. A run_in_background Bash 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.
  • Docs. The spike notes record the registry and result facts. Comments now cover subagents.

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.
  • The real transcript from the reported session was replayed through the reader and engine with the registry busy:
    • Before the fix, at 13:16 the session was finished and a notice went out.
    • After the fix, at 13:16 it is background. After the first agent it is still background, 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 run end to end in the installed Developer app.

Not covered

  • The AI Agents section's reader (AgentUsage/Harness/ClaudeReader.swift) still closes the turn on end_turn, so it shows such sessions as done. That gap was there before this change and needs its own fix.
  • Background agents resumed with SendMessage, and skill forks run in the background, still read as done while they work, as before.

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
MF-DEEV marked this pull request as ready for review October 22, 2026 14:24
@MF-DEEV
MF-DEEV merged commit 4974e4a into main Oct 22, 2026
0 of 2 checks passed
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