mirror of
https://github.com/getpaseo/paseo.git
synced 2026-11-26 02:05:31 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b3a8be1ecc | ||
|
|
ede17bf91b | ||
|
|
3299ff9978 | ||
|
|
9071d8cffc |
@@ -449,7 +449,11 @@ describe("ClaudeAgentSession persisted subagent replay", () => {
|
||||
describe("a background subagent", () => {
|
||||
// Shapes recorded from Claude Code 2.1.x: the launch returns at once with an async result,
|
||||
// and a child stopped by an interrupt ends on the interrupt placeholder, not an end_turn.
|
||||
function backgroundLaunch({ toolUseId = TOOL_USE_ID, agentId = AGENT_ID } = {}): string[] {
|
||||
function backgroundLaunch({
|
||||
toolUseId = TOOL_USE_ID,
|
||||
agentId = AGENT_ID,
|
||||
name,
|
||||
}: { toolUseId?: string; agentId?: string; name?: string } = {}): string[] {
|
||||
return [
|
||||
parentEntry([
|
||||
{
|
||||
@@ -458,6 +462,7 @@ describe("ClaudeAgentSession persisted subagent replay", () => {
|
||||
name: "Agent",
|
||||
input: {
|
||||
subagent_type: "general-purpose",
|
||||
...(name ? { name } : {}),
|
||||
description: "probe sleeper",
|
||||
run_in_background: true,
|
||||
},
|
||||
@@ -744,6 +749,94 @@ 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: hasLaunchResult ? null : 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("completes a child resumed by name through SendMessage after a recorded stop", async () => {
|
||||
const name = "probe-sleeper";
|
||||
const resumeToolId = "toolu_resume";
|
||||
writeSession({
|
||||
parentLines: [
|
||||
...backgroundLaunch({ name }),
|
||||
taskNotification({ status: "stopped", form: "queue-operation", includeToolUseId: false }),
|
||||
parentEntry([
|
||||
{
|
||||
type: "tool_use",
|
||||
id: resumeToolId,
|
||||
name: "SendMessage",
|
||||
input: { to: name, message: "Continue the task." },
|
||||
},
|
||||
]),
|
||||
JSON.stringify({
|
||||
type: "user",
|
||||
message: {
|
||||
role: "user",
|
||||
content: [
|
||||
{
|
||||
type: "tool_result",
|
||||
tool_use_id: resumeToolId,
|
||||
content: [{ type: "text", text: "Message delivered." }],
|
||||
},
|
||||
],
|
||||
},
|
||||
toolUseResult: { success: true, resumedAgentId: AGENT_ID },
|
||||
}),
|
||||
taskNotification({ status: "completed", form: "queue-operation" }).replace(
|
||||
TOOL_USE_ID,
|
||||
resumeToolId,
|
||||
),
|
||||
taskNotification({ status: "completed" }).replace(TOOL_USE_ID, resumeToolId),
|
||||
],
|
||||
meta: null,
|
||||
sidechainLines: [sidechainEntry({ stopReason: "tool_use" })],
|
||||
});
|
||||
|
||||
const descriptors = upserts(await replayDescriptors());
|
||||
for (const descriptor of descriptors) expect(descriptor.id).toBe(TOOL_USE_ID);
|
||||
expect(descriptors.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,28 @@ 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 resumeTargetsByToolCallId = new Map<string, 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,
|
||||
resumeTargetsByToolCallId,
|
||||
resolveLaunchToolCallId,
|
||||
);
|
||||
for (const notificationOutcome of notificationOutcomes) {
|
||||
outcomes.set(notificationOutcome.toolCallId, { status: notificationOutcome.status });
|
||||
}
|
||||
const content = toObjectRecord(entry.message)?.content;
|
||||
@@ -6836,13 +6863,20 @@ 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) resumeTargetsByToolCallId.set(resume.id, resume.target);
|
||||
if (
|
||||
entry.type !== "user" ||
|
||||
block?.type !== "tool_result" ||
|
||||
typeof block.tool_use_id !== "string"
|
||||
)
|
||||
continue;
|
||||
if (toObjectRecord(entry.toolUseResult)?.status !== "async_launched")
|
||||
const result = toObjectRecord(entry.toolUseResult);
|
||||
const resumedTaskId = readClaudeReplayResumedTaskId(block, result);
|
||||
if (resumedTaskId && resumeTargetsByToolCallId.has(block.tool_use_id)) {
|
||||
resumeTargetsByToolCallId.set(block.tool_use_id, resumedTaskId);
|
||||
}
|
||||
if (result?.status !== "async_launched")
|
||||
outcomes.set(block.tool_use_id, {
|
||||
status: block.is_error === true ? "failed" : "completed",
|
||||
});
|
||||
@@ -6864,6 +6898,8 @@ async function readClaudeReplayParentFacts(
|
||||
function* readClaudeReplayNotificationOutcomes(
|
||||
entry: ClaudeHistoryEntry,
|
||||
linksByAgentId: ReadonlyMap<string, { toolCallId: string }>,
|
||||
resumeTargetsByToolCallId: ReadonlyMap<string, string>,
|
||||
resolveLaunchToolCallId: (taskId: string) => string | undefined,
|
||||
): Generator<{ toolCallId: string; status: ProviderSubagentStatus }> {
|
||||
const notification = readTaskNotificationOutcomeFromHistoryRecord(entry);
|
||||
if (!notification) return;
|
||||
@@ -6871,7 +6907,11 @@ 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 taskId = resumeTargetsByToolCallId.get(notification.toolUseId);
|
||||
const launchToolCallId =
|
||||
taskId && taskId === notification.taskIds[0] ? resolveLaunchToolCallId(taskId) : undefined;
|
||||
yield { toolCallId: launchToolCallId ?? notification.toolUseId, status };
|
||||
return;
|
||||
}
|
||||
// Stopped tasks can omit tool-use-id; their launch results already link the native IDs.
|
||||
@@ -6881,6 +6921,30 @@ function* readClaudeReplayNotificationOutcomes(
|
||||
}
|
||||
}
|
||||
|
||||
function readClaudeReplayResume(
|
||||
entryType: unknown,
|
||||
block: Record<string, unknown> | undefined,
|
||||
): { id: string; target: string } | null {
|
||||
if (
|
||||
entryType !== "assistant" ||
|
||||
block?.type !== "tool_use" ||
|
||||
block.name !== "SendMessage" ||
|
||||
typeof block.id !== "string"
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
const target = readNonEmptyString(toObjectRecord(block.input)?.to);
|
||||
return target ? { id: block.id, target } : null;
|
||||
}
|
||||
|
||||
function readClaudeReplayResumedTaskId(
|
||||
block: Record<string, unknown>,
|
||||
result: Record<string, unknown> | undefined,
|
||||
): string | null {
|
||||
if (block.is_error === true || result?.success !== true) return null;
|
||||
return readNonEmptyString(result.resumedAgentId);
|
||||
}
|
||||
|
||||
function readClaudeReplayTaskDeclaration(
|
||||
entryType: unknown,
|
||||
block: Record<string, unknown> | undefined,
|
||||
|
||||
Reference in New Issue
Block a user