Repository navigation
Keep a primed subagent spawn alive past the next turn - #334132
Closed
Ryan Ewen (RyanEwen) wants to merge 1 commit into
Closed
Ryan Ewen (RyanEwen) wants to merge 1 commit into
Ryan Ewen (RyanEwen) wants to merge 1 commit into
Conversation
`SubagentRegistry` doubles as the agentId cache that `getSubagentTranscript` reads, and `primeFromTranscript` fills it on resume from the Task tool_result suffixes in the parent transcript. Those records described Tasks that had already produced a result, but they were written through `recordSpawn`, so `drainForegroundSpawns` treated every one of them as an in-flight foreground spawn and dropped it at the end of the next turn, with a warning claiming a pending spawn had been abandoned. Two things went with it. The resolver lost its warm cache one turn after priming it, so reopening a restored subagent chat re-ran the whole strategy chain. Worse, a background subagent that outlived a host restart has no `task_started` in the resumed stream to mark its spawn background, so its record was drained too and the later `system.task_notification` no longer found a spawn to complete. Record an agentId learned from a transcript through a separate `cacheAgentId` entry point that does not claim the spawn is in flight, and drain only spawns this process saw start.
Contributor
📬 CODENOTIFYThe following users are being notified based on files changed in this PR: TylerLeonhardtMatched files:
|
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The focused lifecycle fix is internally consistent and covered by relevant regression tests.
Pull request overview
Preserves transcript-primed subagent identities across turn cleanup so restored background subagents can complete correctly.
Changes:
- Distinguishes live spawns from agent-ID-only cache entries.
- Retains cached records during foreground draining.
- Adds regression coverage for restoration and completion.
File summaries
| File | Description |
|---|---|
claudeSubagentSignals.test.ts |
Tests restored background completion. |
claudeSubagentRegistry.test.ts |
Tests cache retention during draining. |
claudeSubagentResolver.ts |
Uses the non-live cache API. |
claudeSubagentRegistry.ts |
Tracks in-flight state and preserves cached identities. |
Review details
- Files reviewed: 4/4 changed files
- Comments generated: 0
- Review effort level: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
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.
Keeps a subagent spawn that was primed from the transcript alive past the next turn, so a restored subagent's identity survives and a background subagent that outlived a host restart can still complete.
Defect
SubagentRegistrydoubles as the agent-id cache the subagent transcript resolver reads, andprimeFromTranscriptfills it on resume. Those records were written throughrecordSpawn, so they looked like ordinary in-flight foreground spawns, anddrainForegroundSpawnsremoved every one of them at the end of the next turn while warning that a pending spawn had been abandoned.Two consequences. The resolver's warm cache is destroyed one turn after priming, so reopening a restored subagent chat re-runs the whole strategy chain. More seriously, a background subagent that outlived a host restart has no task-started event in the resumed stream to mark it as background, so its record is drained too, and the later task notification finds no spawn to complete.
A live foreground spawn is already removed on its own tool result, so the drain was catching almost nothing else.
Fix
A spawn records whether it is actually in flight. The drain now takes only in-flight foreground spawns, and priming records an agent id without claiming the spawn is running.
Tests
drainForegroundSpawns keeps agentId-only records, so a spawn primed from the transcript survives the first resumed turn, driving the real priming patha background subagent known only from the transcript still completes after a turn ends, driving the mapper with a result followed by a task notificationReverting the gate fails both; the second is the substantive one, producing no completion signal at all. 586 passing across the Claude agent, subagent signals, mapper, side effects, registry, resolver, router and replay suites. No new type errors.
Scope
This does not touch the message router. Two open PRs already cover the messages that arrive while no turn is in flight: #331872 routes the background subagent's system notification, and #332075 maps resumed output under the spawn's turn. This change is what makes the spawn still be there when those messages arrive.
Its visible payoff for the restart case is gated on a separate gap: the restore path never creates a subagent chat entry, so the completion this restores currently has nowhere to land. The fix is correct and prerequisite either way, and the registry and mapper behaviour is asserted directly.
Part of #333174.
AI disclosure: this comment and the related code were written with the assistance of AI.
Public patches and patcher scripts
Source patch and installed-bundle workaround mapping. The public collection includes the maintained patchers, rollback instructions, regression scripts, and historical snapshots. Build restrictions and exact installer coverage are documented there.