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 8c865d43db..146b24d6df 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 @@ -744,6 +744,50 @@ describe("ClaudeAgentSession persisted subagent replay", () => { expect(upserts(await replayDescriptors()).at(-1)).toMatchObject({ status: "completed" }); }); + test.each([ + { history: "with the launch result", hasLaunchResult: true, hasStop: false }, + { history: "after a recorded stop", hasLaunchResult: true, hasStop: true }, + { history: "with only the launch sidecar", hasLaunchResult: false, hasStop: false }, + ])("completes a SendMessage-resumed child $history", async ({ hasLaunchResult, hasStop }) => { + const launch = hasLaunchResult ? backgroundLaunch() : backgroundLaunch().slice(0, 1); + const prior = hasStop + ? [ + taskNotification({ + status: "stopped", + form: "queue-operation", + includeToolUseId: false, + }), + ] + : []; + const resumeToolId = "toolu_resume"; + const completion = taskNotification({ status: "completed", form: "queue-operation" }).replace( + TOOL_USE_ID, + resumeToolId, + ); + writeSession({ + parentLines: [ + ...launch, + ...prior, + parentEntry([ + { + type: "tool_use", + id: resumeToolId, + name: "SendMessage", + input: { to: AGENT_ID, message: "Continue the task." }, + }, + ]), + completion, + ], + meta: JSON.stringify({ toolUseId: TOOL_USE_ID, finished: true }), + sidechainLines: [sidechainEntry({ stopReason: "tool_use" })], + }); + + expect(upserts(await replayDescriptors()).at(-1)).toMatchObject({ + id: TOOL_USE_ID, + status: "completed", + }); + }); + test("honors a later task-only stopped notification after a tool-linked completion", async () => { writeSession({ parentLines: [ diff --git a/packages/server/src/server/agent/providers/claude/agent.ts b/packages/server/src/server/agent/providers/claude/agent.ts index ca44e155a4..85c09df63c 100644 --- a/packages/server/src/server/agent/providers/claude/agent.ts +++ b/packages/server/src/server/agent/providers/claude/agent.ts @@ -5935,7 +5935,15 @@ class ClaudeAgentSession implements AgentSession { private async ingestPersistedSidechains( history: ClaudeReplayHistory, ): Promise { - const parentFacts = await readClaudeReplayParentFacts(history.parentEntries); + const sidecarToolCallIdsByTaskId = new Map(); + for (const { agentId, meta } of history.subagents) { + const toolCallId = meta?.toolUseId?.trim(); + if (toolCallId) sidecarToolCallIdsByTaskId.set(agentId, toolCallId); + } + const parentFacts = await readClaudeReplayParentFacts( + history.parentEntries, + sidecarToolCallIdsByTaskId, + ); // Replay produces the same observations the live task protocol produces, then folds them // with the same function, so identity and status are derived once for both paths. @@ -5943,7 +5951,10 @@ class ClaudeAgentSession implements AgentSession { for (const subagent of history.subagents) { subagents.push({ ...subagent, - parentFacts: await readClaudeReplayParentFacts(subagent.entries), + parentFacts: await readClaudeReplayParentFacts( + subagent.entries, + sidecarToolCallIdsByTaskId, + ), }); } const subagentReplay = await observeReplaySubagents({ @@ -6822,12 +6833,30 @@ function labelReplayedSubagentCard( */ async function readClaudeReplayParentFacts( entries: Iterable | AsyncIterable, + sidecarToolCallIdsByTaskId: ReadonlyMap, ): Promise { const toolCalls = new Map(); const linksByAgentId = new Map(); + const resumedChildrenByToolCallId = new Map< + string, + { taskId: string; launchToolCallId: string } + >(); const outcomes: Map = new Map(); + + const resolveLaunchToolCallId = (taskId: string): string | undefined => { + const sidecarToolCallId = sidecarToolCallIdsByTaskId.get(taskId); + return sidecarToolCallId && toolCalls.has(sidecarToolCallId) + ? sidecarToolCallId + : linksByAgentId.get(taskId)?.toolCallId; + }; + for await (const entry of entries) { - for (const notificationOutcome of readClaudeReplayNotificationOutcomes(entry, linksByAgentId)) { + const notificationOutcomes = readClaudeReplayNotificationOutcomes( + entry, + linksByAgentId, + resumedChildrenByToolCallId, + ); + for (const notificationOutcome of notificationOutcomes) { outcomes.set(notificationOutcome.toolCallId, { status: notificationOutcome.status }); } const content = toObjectRecord(entry.message)?.content; @@ -6836,6 +6865,13 @@ async function readClaudeReplayParentFacts( const block = toObjectRecord(value); const declaration = readClaudeReplayTaskDeclaration(entry.type, block); if (declaration) toolCalls.set(declaration.id, declaration.identity); + const resume = readClaudeReplayResume(entry.type, block); + if (resume) { + const launchToolCallId = resolveLaunchToolCallId(resume.taskId); + if (launchToolCallId) { + resumedChildrenByToolCallId.set(resume.id, { taskId: resume.taskId, launchToolCallId }); + } + } if ( entry.type !== "user" || block?.type !== "tool_result" || @@ -6864,6 +6900,7 @@ async function readClaudeReplayParentFacts( function* readClaudeReplayNotificationOutcomes( entry: ClaudeHistoryEntry, linksByAgentId: ReadonlyMap, + resumedChildrenByToolCallId: ReadonlyMap, ): Generator<{ toolCallId: string; status: ProviderSubagentStatus }> { const notification = readTaskNotificationOutcomeFromHistoryRecord(entry); if (!notification) return; @@ -6871,7 +6908,13 @@ function* readClaudeReplayNotificationOutcomes( if (!status) return; if (notification.toolUseId) { - yield { toolCallId: notification.toolUseId, status }; + // A resumed child keeps its task ID, but Claude reports its outcome under the SendMessage call. + const resumedChild = resumedChildrenByToolCallId.get(notification.toolUseId); + const toolCallId = + resumedChild && resumedChild.taskId === notification.taskIds[0] + ? resumedChild.launchToolCallId + : notification.toolUseId; + yield { toolCallId, status }; return; } // Stopped tasks can omit tool-use-id; their launch results already link the native IDs. @@ -6881,6 +6924,22 @@ function* readClaudeReplayNotificationOutcomes( } } +function readClaudeReplayResume( + entryType: unknown, + block: Record | undefined, +): { id: string; taskId: string } | null { + if ( + entryType !== "assistant" || + block?.type !== "tool_use" || + block.name !== "SendMessage" || + typeof block.id !== "string" + ) { + return null; + } + const taskId = readNonEmptyString(toObjectRecord(block.input)?.to); + return taskId ? { id: block.id, taskId } : null; +} + function readClaudeReplayTaskDeclaration( entryType: unknown, block: Record | undefined,