From 9071d8cffc765d9dfd7e7ecd46940f78bfc6c15f Mon Sep 17 00:00:00 2001 From: monarchmaisuriya Date: Mon, 23 Nov 2026 09:36:40 +0530 Subject: [PATCH] fix(claude): restore resumed subagent status on replay A background subagent resumed with SendMessage reports completion under the SendMessage tool id, and a notification written after a lost process has no tool id at all. Replay keyed outcomes by Task tool id only, so the finished child fell back to the store default and showed running after every reopen. Key replayed notification outcomes by task id as well, which is what the live path uses and what stays stable across resumes, and treat a SubagentHandback call as the child's completion in the transcript fallback. Fixes #10568 --- .../claude/agent.sub-agent-replay.test.ts | 80 +++++++++++++++++++ .../server/agent/providers/claude/agent.ts | 20 +++-- .../claude/subagents/replay-source.test.ts | 11 ++- .../claude/subagents/replay-source.ts | 28 ++++++- 4 files changed, 131 insertions(+), 8 deletions(-) diff --git a/packages/server/src/server/agent/providers/claude/agent.sub-agent-replay.test.ts b/packages/server/src/server/agent/providers/claude/agent.sub-agent-replay.test.ts index 31a4c60c43..2ea42669d1 100644 --- a/packages/server/src/server/agent/providers/claude/agent.sub-agent-replay.test.ts +++ b/packages/server/src/server/agent/providers/claude/agent.sub-agent-replay.test.ts @@ -675,6 +675,86 @@ describe("ClaudeAgentSession persisted subagent replay", () => { expect(upserts(await replayDescriptors()).at(-1)).toMatchObject({ status: "completed" }); }); + + test("completes when a SendMessage resume finished it under a new tool id", async () => { + // Recorded from Claude Code 2.1.330: a restart cut the child off, Claude reported it stopped + // with no tool id, SendMessage resumed it, and its completion names the SendMessage call. + const resumeToolUseId = "toolu_resume_send_message"; + const notification = (status: string, toolUseId?: string) => + JSON.stringify({ + type: "queue-operation", + operation: "enqueue", + timestamp: "2026-07-26T06:29:00.000Z", + content: [ + "", + `${AGENT_ID}`, + ...(toolUseId ? [`${toolUseId}`] : []), + `${status}`, + "Background task ended", + "", + ].join("\n"), + }); + writeSession({ + parentLines: [ + ...backgroundLaunch(), + notification("stopped"), + parentEntry([ + { + type: "tool_use", + id: resumeToolUseId, + name: "SendMessage", + input: { to: AGENT_ID, message: "Resume the task." }, + }, + ]), + notification("completed", resumeToolUseId), + ], + meta: JSON.stringify({ toolUseId: TOOL_USE_ID }), + sidechainLines: [sidechainEntry({ stopReason: "tool_use" })], + }); + + expect(upserts(await replayDescriptors()).at(-1)).toMatchObject({ + id: TOOL_USE_ID, + status: "completed", + }); + }); + + test("completes from the child's SubagentHandback report", async () => { + const handbackId = "toolu_handback"; + writeSession({ + parentLines: backgroundLaunch(), + meta: JSON.stringify({ toolUseId: TOOL_USE_ID }), + sidechainLines: [ + JSON.stringify({ + type: "assistant", + isSidechain: true, + agentId: AGENT_ID, + message: { + role: "assistant", + content: [ + { + type: "tool_use", + id: handbackId, + name: "SubagentHandback", + input: { message: "## Report" }, + }, + ], + stop_reason: "tool_use", + }, + }), + JSON.stringify({ + type: "user", + isSidechain: true, + agentId: AGENT_ID, + message: { + role: "user", + content: [{ type: "tool_result", tool_use_id: handbackId, content: "delivered" }], + }, + }), + ], + }); + + expect(upserts(await replayDescriptors()).at(-1)).toMatchObject({ status: "completed" }); + }); }); test("does not replay a skill-spawned subagent the parent never named", async () => { diff --git a/packages/server/src/server/agent/providers/claude/agent.ts b/packages/server/src/server/agent/providers/claude/agent.ts index ffc0ac9fe4..56e64c47bc 100644 --- a/packages/server/src/server/agent/providers/claude/agent.ts +++ b/packages/server/src/server/agent/providers/claude/agent.ts @@ -6796,12 +6796,9 @@ async function readClaudeReplayParentFacts( const toolCalls = new Map(); const linksByAgentId = new Map(); const outcomes: Map = new Map(); + const outcomesByTaskId: Map = new Map(); for await (const entry of entries) { - const notification = readTaskNotificationFromHistoryRecord(entry); - const status = mapTaskStatus(notification?.status ?? undefined); - if (notification?.toolUseId && status) { - outcomes.set(notification.toolUseId, { status }); - } + recordReplayTaskNotificationOutcome(entry, outcomes, outcomesByTaskId); const content = toObjectRecord(entry.message)?.content; if (!Array.isArray(content)) continue; for (const value of content) { @@ -6829,9 +6826,22 @@ async function readClaudeReplayParentFacts( toolCalls, linksByAgentId, outcomesByToolCallId: new Map([...outcomes].filter(([id]) => toolCalls.has(id))), + outcomesByTaskId, }; } +function recordReplayTaskNotificationOutcome( + entry: ClaudeHistoryEntry, + outcomesByToolCallId: Map, + outcomesByTaskId: Map, +): void { + const notification = readTaskNotificationFromHistoryRecord(entry); + const status = mapTaskStatus(notification?.status ?? undefined); + if (!notification || !status) return; + if (notification.toolUseId) outcomesByToolCallId.set(notification.toolUseId, { status }); + if (notification.taskId) outcomesByTaskId.set(notification.taskId, { status }); +} + function readClaudeReplayTaskDeclaration( entryType: unknown, block: Record | undefined, diff --git a/packages/server/src/server/agent/providers/claude/subagents/replay-source.test.ts b/packages/server/src/server/agent/providers/claude/subagents/replay-source.test.ts index fc48e9db3a..3e1844a19b 100644 --- a/packages/server/src/server/agent/providers/claude/subagents/replay-source.test.ts +++ b/packages/server/src/server/agent/providers/claude/subagents/replay-source.test.ts @@ -24,7 +24,12 @@ const TOOL_USE_ID = "toolu_01DgLoPMW9"; const AGENT_ID = "a1730a6215e1f5cf6"; function emptyParent(): ClaudeReplayParentFacts { - return { toolCalls: new Map(), linksByAgentId: new Map(), outcomesByToolCallId: new Map() }; + return { + toolCalls: new Map(), + linksByAgentId: new Map(), + outcomesByToolCallId: new Map(), + outcomesByTaskId: new Map(), + }; } function parentWithTaskCall(failed = false): ClaudeReplayParentFacts { @@ -37,6 +42,7 @@ function parentWithTaskCall(failed = false): ClaudeReplayParentFacts { ]), linksByAgentId: new Map(), outcomesByToolCallId: new Map([[TOOL_USE_ID, { status: failed ? "failed" : "completed" }]]), + outcomesByTaskId: new Map(), }; } @@ -162,6 +168,7 @@ describe("observeReplaySubagents", () => { ]), linksByAgentId: new Map(), outcomesByToolCallId: new Map([[nestedToolUseId, { status: "completed" }]]), + outcomesByTaskId: new Map(), }, }, { @@ -219,6 +226,7 @@ describe("observeReplaySubagents", () => { toolCalls: new Map([[TOOL_USE_ID, { title: "Explore" }]]), linksByAgentId: new Map([[AGENT_ID, { toolCallId: TOOL_USE_ID }]]), outcomesByToolCallId: new Map([[TOOL_USE_ID, { status: "completed" }]]), + outcomesByTaskId: new Map(), }; const [declared, ...rest] = ( @@ -309,6 +317,7 @@ describe("observeReplaySubagents", () => { toolCalls: new Map([[TOOL_USE_ID, { title: "Explore" }]]), linksByAgentId: new Map([[AGENT_ID, { toolCallId: TOOL_USE_ID }]]), outcomesByToolCallId: new Map([[TOOL_USE_ID, { status: "failed" }]]), + outcomesByTaskId: new Map(), }, convertEntry: () => [], }) diff --git a/packages/server/src/server/agent/providers/claude/subagents/replay-source.ts b/packages/server/src/server/agent/providers/claude/subagents/replay-source.ts index 51e5ff9c09..1069761e88 100644 --- a/packages/server/src/server/agent/providers/claude/subagents/replay-source.ts +++ b/packages/server/src/server/agent/providers/claude/subagents/replay-source.ts @@ -5,6 +5,7 @@ import { normalizeProviderReplayTimestamp } from "../../../provider-history-time import type { ProviderSubagentStatus } from "../../../provider-subagents/store.js"; import { isClaudeInterruptPlaceholderText } from "../interrupt-placeholder.js"; import { resolveObservedClaudeModelId } from "../models.js"; +import { isClaudeSubagentHandbackToolName } from "../subagent-handback.js"; import type { ClaudeRestoredSubagent } from "./live-source.js"; import type { SubagentObservation } from "./observation.js"; import { buildClaudeSubagentSubtitle, type ClaudeSubagentUsage } from "./presentation.js"; @@ -147,6 +148,12 @@ export interface ClaudeReplayParentFacts { * none: its immediate result only says the child started. */ outcomesByToolCallId: ReadonlyMap; + /** + * Task id (the child's agentId) -> latest task notification outcome. A SendMessage resume + * keeps the task id but reports under the SendMessage tool id, and a notification written after + * a lost process has no tool id at all, so only the task id follows the child across resumes. + */ + outcomesByTaskId: ReadonlyMap; } interface ParentLink { @@ -188,7 +195,10 @@ function resolveParentLink( : parent.linksByAgentId.get(subagent.agentId)?.toolCallId; if (!toolCallId || !parent.toolCalls.has(toolCallId)) return null; - const status = parent.outcomesByToolCallId.get(toolCallId)?.status ?? null; + const status = + parent.outcomesByTaskId.get(subagent.agentId)?.status ?? + parent.outcomesByToolCallId.get(toolCallId)?.status ?? + null; return { id: toolCallId, toolCallId, status }; } @@ -306,7 +316,9 @@ function readChildTerminalStatus( previous: ProviderSubagentStatus | null, ): ProviderSubagentStatus | null { if (entry.type === "assistant") { - return entry.message?.stop_reason === "end_turn" ? "completed" : null; + return entry.message?.stop_reason === "end_turn" || hasSubagentHandback(entry) + ? "completed" + : null; } if (entry.type !== "user") return previous; const texts = readUserTexts(entry); @@ -314,6 +326,18 @@ function readChildTerminalStatus( return texts.some(isClaudeInterruptPlaceholderText) ? "canceled" : null; } +/** A child in auto mode ends by handing its report back instead of with an end_turn reply. */ +function hasSubagentHandback(entry: ClaudeReplayEntry): boolean { + const content = entry.message?.content; + return ( + Array.isArray(content) && + content.some( + (block: { type?: unknown; name?: unknown } | null) => + block?.type === "tool_use" && isClaudeSubagentHandbackToolName(block.name), + ) + ); +} + /** Tool results carry no user text and do not change the child's terminal status. */ function readUserTexts(entry: ClaudeReplayEntry): string[] { const content = entry.message?.content;