mirror of
https://github.com/getpaseo/paseo.git
synced 2026-11-26 02:05:31 +00:00
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
This commit is contained in:
@@ -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: [
|
||||
"<task-notification>",
|
||||
`<task-id>${AGENT_ID}</task-id>`,
|
||||
...(toolUseId ? [`<tool-use-id>${toolUseId}</tool-use-id>`] : []),
|
||||
`<status>${status}</status>`,
|
||||
"<summary>Background task ended</summary>",
|
||||
"</task-notification>",
|
||||
].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 () => {
|
||||
|
||||
@@ -6796,12 +6796,9 @@ async function readClaudeReplayParentFacts(
|
||||
const toolCalls = new Map<string, { title?: string; description?: string }>();
|
||||
const linksByAgentId = new Map<string, { toolCallId: string }>();
|
||||
const outcomes: Map<string, { status: ProviderSubagentStatus }> = new Map();
|
||||
const outcomesByTaskId: Map<string, { status: ProviderSubagentStatus }> = 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<string, { status: ProviderSubagentStatus }>,
|
||||
outcomesByTaskId: Map<string, { status: ProviderSubagentStatus }>,
|
||||
): 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<string, unknown> | undefined,
|
||||
|
||||
@@ -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: () => [],
|
||||
})
|
||||
|
||||
@@ -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<string, { status: ProviderSubagentStatus }>;
|
||||
/**
|
||||
* 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<string, { status: ProviderSubagentStatus }>;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user