mirror of
https://github.com/getpaseo/paseo.git
synced 2026-11-26 02:05:31 +00:00
fix(claude): Restore finished subagents after a SendMessage resume
Claude records the resumed child's completion under the SendMessage tool ID. Link that outcome to the original launch so replay applies it in order with earlier task notices. Closes #8959 Co-authored-by: Monarch Maisuriya <monarchmaisuriya7600@gmail.com>
This commit is contained in:
@@ -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: [
|
||||
|
||||
@@ -5935,7 +5935,15 @@ class ClaudeAgentSession implements AgentSession {
|
||||
private async ingestPersistedSidechains(
|
||||
history: ClaudeReplayHistory,
|
||||
): Promise<ClaudeReplayOwnership> {
|
||||
const parentFacts = await readClaudeReplayParentFacts(history.parentEntries);
|
||||
const sidecarToolCallIdsByTaskId = new Map<string, string>();
|
||||
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<ClaudeHistoryEntry> | AsyncIterable<ClaudeHistoryEntry>,
|
||||
sidecarToolCallIdsByTaskId: ReadonlyMap<string, string>,
|
||||
): Promise<ClaudeReplayParentFacts> {
|
||||
const toolCalls = new Map<string, { title?: string; description?: string }>();
|
||||
const linksByAgentId = new Map<string, { toolCallId: string }>();
|
||||
const resumedChildrenByToolCallId = new Map<
|
||||
string,
|
||||
{ taskId: string; launchToolCallId: string }
|
||||
>();
|
||||
const outcomes: Map<string, { status: ProviderSubagentStatus }> = 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<string, { toolCallId: string }>,
|
||||
resumedChildrenByToolCallId: ReadonlyMap<string, { taskId: string; launchToolCallId: string }>,
|
||||
): 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<string, unknown> | 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<string, unknown> | undefined,
|
||||
|
||||
Reference in New Issue
Block a user