Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
330 changes: 327 additions & 3 deletions apps/server/src/orchestration-v2/Adapters/AcpRegistryAdapterV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ const openClaudeReplaySession = Effect.fn("openClaudeReplaySession")(function* (
entries: ReadonlyArray<unknown>,
replayGate?: ProviderReplayGate,
continuationEnabled = true,
agentId = "claude-acp",
) {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
Expand Down Expand Up @@ -248,7 +249,7 @@ const openClaudeReplaySession = Effect.fn("openClaudeReplaySession")(function* (
const adapterOptions = {
selfInvocation: yield* resolveSelfInvocation(),
instanceId,
settings: yield* decodeAcpRegistryAdapterSettings({ agentId: "claude-acp" }),
settings: yield* decodeAcpRegistryAdapterSettings({ agentId }),
environment: {},
...(continuationEnabled ? { continuationRequests } : {}),
makeRuntime: (runtimeInput: Parameters<typeof replayRuntime>[0]) => {
Expand Down Expand Up @@ -1808,6 +1809,290 @@ describe("AcpRegistryAdapterV2", () => {
}).pipe(Effect.provide(layerTest), Effect.scoped),
);

it.effect("retains streamed child tool output through the generic Devin route", () =>
Effect.gen(function* () {
const { update } = claudeFrames;
const session = yield* openClaudeReplaySession(
[
...claudeFrames.open,
...claudeFrames.prompt(1),
update("child-1", {
sessionUpdate: "tool_call_update",
toolCallId: "early-tool",
status: "in_progress",
rawInput: { command: "echo early" },
}),
update("child-1", {
sessionUpdate: "tool_call_content_chunk",
toolCallId: "early-tool",
content: { type: "content", content: { type: "text", text: "early" } },
}),
update("child-1", {
sessionUpdate: "tool_call_update",
toolCallId: "early-tool",
status: "completed",
}),
update(CLAUDE_ROOT_SESSION, {
sessionUpdate: "tool_call_update",
toolCallId: "spawn",
status: "in_progress",
_meta: { "cognition.ai/subagent_started": { agentId: "child-1", task: "Review" } },
}),
...[CLAUDE_ROOT_SESSION, "child-1"].map((sessionId) =>
update(sessionId, {
sessionUpdate: "tool_call_update",
toolCallId: "shared-tool",
title: "Read package",
kind: "execute",
status: "in_progress",
rawInput: { command: "cat package.json" },
...(sessionId === "child-1"
? { content: [{ type: "content", content: { type: "text", text: "one" } }] }
: {}),
}),
),
update("child-1", {
sessionUpdate: "tool_call_content_chunk",
toolCallId: "shared-tool",
content: { type: "content", content: { type: "text", text: "two" } },
}),
update("child-1", {
sessionUpdate: "tool_call_update",
toolCallId: "shared-tool",
status: "completed",
}),
update("child-1", {
sessionUpdate: "tool_call_update",
toolCallId: "shared-tool",
content: [{ type: "content", content: { type: "text", text: "replacement" } }],
}),
update("child-1", {
sessionUpdate: "tool_call_update",
toolCallId: "shared-tool",
content: [],
}),
update("child-1", {
sessionUpdate: "tool_call_update",
toolCallId: "shared-tool",
rawOutput: { stdout: "structured", stderr: "" },
}),
update("child-1", {
sessionUpdate: "tool_call_update",
toolCallId: "shared-tool",
content: [{ type: "content", content: { type: "text", text: "display" } }],
}),
update("child-1", {
sessionUpdate: "tool_call_update",
toolCallId: "shared-tool",
rawOutput: { type: "Text", text: "vendor" },
}),
update("child-1", {
sessionUpdate: "tool_call_update",
toolCallId: "shared-tool",
content: [{ type: "content", content: { type: "text", text: "display" } }],
}),
claudeMarker("tools complete"),
claudeFrames.idle("end_turn"),
claudeFrames.closeRequest,
claudeFrames.closed,
],
undefined,
false,
"devin",
);
yield* session.runtime.startTurn(session.turnInput(1));
const events = yield* session.throughMarker("tools complete");
const child = events.find((event) => event.type === "subagent.updated");
if (child?.type !== "subagent.updated") throw new Error("Missing child");
const tools = events.flatMap((event) =>
event.type === "turn_item.updated" && event.turnItem.type === "dynamic_tool"
? [event.turnItem]
: [],
);
const childTools = tools.filter((item) => item.threadId === child.subagent.childThreadId);
assert.deepEqual(
childTools.map((item) => item.output),
[
[{ type: "content", content: { type: "text", text: "one" } }],
[{ type: "content", content: { type: "text", text: "onetwo" } }],
[{ type: "content", content: { type: "text", text: "onetwo" } }],
[{ type: "content", content: { type: "text", text: "replacement" } }],
[],
{ stdout: "structured", stderr: "" },
{ stdout: "structured", stderr: "" },
{ type: "Text", text: "vendor" },
{ type: "Text", text: "vendor" },
],
);
assert.equal(new Set(childTools.map((item) => item.id)).size, 1);
assert.equal(childTools.at(-1)?.nativeItemRef?.nativeId, "child-1:tool:shared-tool");
assert.equal(childTools.at(-1)?.status, "completed");
assert.deepEqual(childTools.at(-1)?.input, { command: "cat package.json" });
const parentTools = events.flatMap((event) =>
event.type === "turn_item.updated" &&
event.turnItem.type === "command_execution" &&
event.turnItem.threadId === session.turnInput(1).threadId &&
event.turnItem.nativeItemRef?.nativeId === "shared-tool"
? [event.turnItem]
: [],
);
assert.lengthOf(parentTools, 1);
assert.isUndefined(parentTools[0]?.output);
assert.isFalse(
events.some(
(event) => event.type === "message.updated" && event.message.text.includes("onetwo"),
),
);
}).pipe(Effect.provide(layerTest), Effect.scoped),
);

it.effect("keeps stopped child tools interrupted when a late output chunk arrives", () =>
Effect.gen(function* () {
const { update } = claudeFrames;
const session = yield* openClaudeReplaySession(
[
...claudeFrames.open.map((entry) =>
entry.frame.method === "initialize" && entry.type === "emit_inbound"
? {
...entry,
frame: {
...entry.frame,
result: { protocolVersion: 1, agentCapabilities: {}, authMethods: [] },
},
}
: entry,
),
{
type: "expect_outbound",
frame: { kind: "request", method: "session/prompt", params: "<any>" },
},
update(CLAUDE_ROOT_SESSION, {
sessionUpdate: "tool_call_update",
toolCallId: "spawn",
status: "in_progress",
_meta: { "cognition.ai/subagent_started": { agentId: "child-1", task: "Review" } },
}),
update("child-1", {
sessionUpdate: "tool_call_update",
toolCallId: "stopped-tool",
status: "in_progress",
}),
claudeMarker("before cancel"),
{
type: "expect_outbound",
frame: {
kind: "notification",
method: "session/cancel",
params: { sessionId: CLAUDE_ROOT_SESSION },
},
},
update("child-1", {
sessionUpdate: "tool_call_update",
toolCallId: "stopped-tool",
status: "failed",
}),
update("child-1", {
sessionUpdate: "tool_call_update",
toolCallId: "stopped-tool",
content: [{ type: "content", content: { type: "text", text: "stopping" } }],
}),
update("child-1", {
sessionUpdate: "tool_call_content_chunk",
toolCallId: "stopped-tool",
content: { type: "content", content: { type: "text", text: "cancelled" } },
}),
claudeMarker("late output"),
update(CLAUDE_ROOT_SESSION, {
sessionUpdate: "tool_call_update",
toolCallId: "spawn",
status: "failed",
_meta: { "cognition.ai/subagent_completed": { agentId: "child-1", success: false } },
}),
{
type: "emit_inbound",
frame: {
kind: "response",
method: "session/prompt",
result: { stopReason: "cancelled" },
},
},
claudeFrames.closeRequest,
claudeFrames.closed,
],
undefined,
false,
"devin",
);
const starting = yield* session.runtime
.startTurn(session.turnInput(1))
.pipe(Effect.forkScoped);
const started = yield* session.throughMarker("before cancel");
const running = started.findLast((event) => event.type === "provider_turn.updated");
if (running?.type !== "provider_turn.updated") throw new Error("Missing active turn");
const closing = yield* session.runtime
.interruptTurn({
providerThread: session.providerThread,
providerTurnId: running.providerTurn.id,
})
.pipe(Effect.forkScoped);
const events = yield* session.throughMarker("late output");
yield* Fiber.join(closing);
yield* Fiber.join(starting);
const tools = events.flatMap((event) =>
event.type === "turn_item.updated" && event.turnItem.type === "dynamic_tool"
? [event.turnItem]
: [],
);
assert.deepEqual(
tools.map((item) => item.status),
["interrupted", "interrupted", "interrupted"],
);
assert.deepEqual(tools.at(-1)?.output, [
{ type: "content", content: { type: "text", text: "stoppingcancelled" } },
]);
}).pipe(Effect.provide(layerTest), Effect.scoped),
);

it.effect("keeps native child tools interrupted after idle and late output", () =>
Effect.gen(function* () {
const { update, childState } = claudeFrames;
const session = yield* openClaudeReplaySession([
...claudeFrames.open,
...claudeFrames.prompt(1),
...claudeChild.slice(0, -1),
update("child-1", {
sessionUpdate: "tool_call_update",
toolCallId: "unfinished",
status: "in_progress",
}),
childState(CLAUDE_ROOT_SESSION, "child-1", "idle"),
update("child-1", {
sessionUpdate: "tool_call_content_chunk",
toolCallId: "unfinished",
content: { type: "content", content: { type: "text", text: "late" } },
}),
claudeMarker("late native output"),
claudeFrames.idle("end_turn"),
claudeFrames.closeRequest,
claudeFrames.closed,
]);
yield* session.runtime.startTurn(session.turnInput(1));
const events = yield* session.throughMarker("late native output");
const tools = events.flatMap((event) =>
event.type === "turn_item.updated" && event.turnItem.type === "dynamic_tool"
? [event.turnItem]
: [],
);
assert.deepEqual(
tools.map((tool) => tool.status),
["running", "interrupted", "interrupted"],
);
assert.deepEqual(tools.at(-1)?.output, [
{ type: "content", content: { type: "text", text: "late" } },
]);
}).pipe(Effect.provide(layerTest), Effect.scoped),
);

it.effect.each(["error", "end_turn"])(
"hands live Claude child tools to the next run after %s and keeps nested output in the child",
(stopReason) =>
Expand Down Expand Up @@ -1899,6 +2184,18 @@ describe("AcpRegistryAdapterV2", () => {
status: "in_progress",
rawInput: { command: "cat nested/package.json" },
}),
...["one", "two"].map((text) =>
update("grandchild", {
sessionUpdate: "tool_call_content_chunk",
toolCallId: "nested-tool",
content: { type: "content", content: { type: "text", text } },
}),
),
update("grandchild", {
sessionUpdate: "tool_call_update",
toolCallId: "nested-tool",
status: "completed",
}),
update("grandchild", {
sessionUpdate: "agent_message_chunk",
messageId: "nested-reply",
Expand Down Expand Up @@ -1996,7 +2293,7 @@ describe("AcpRegistryAdapterV2", () => {
if (messages[0]?.type === "message.updated") {
assert.equal(messages[0].message.threadId, row.subagent.childThreadId);
}
const nestedTool = nested.find(
const nestedTool = nested.findLast(
(event) =>
event.type === "turn_item.updated" &&
event.turnItem.type === "dynamic_tool" &&
Expand All @@ -2006,9 +2303,36 @@ describe("AcpRegistryAdapterV2", () => {
assert.isDefined(nestedTool);
if (nestedTool?.type === "turn_item.updated") {
assert.equal(nestedTool.turnItem.threadId, row.subagent.childThreadId);
if (nestedTool.turnItem.type === "dynamic_tool")
if (nestedTool.turnItem.type === "dynamic_tool") {
assert.deepEqual(nestedTool.turnItem.input, { command: "cat nested/package.json" });
assert.equal(nestedTool.turnItem.status, "completed");
}
const toolUpdates = nested.flatMap((event) =>
event.type === "turn_item.updated" &&
event.turnItem.type === "dynamic_tool" &&
event.turnItem.id === nestedTool.turnItem.id
? [event.turnItem]
: [],
);
assert.deepEqual(
toolUpdates.map((item) => item.output),
[
null,
[{ type: "content", content: { type: "text", text: "one" } }],
[{ type: "content", content: { type: "text", text: "onetwo" } }],
[{ type: "content", content: { type: "text", text: "onetwo" } }],
],
);
}
assert.isFalse(
nested.some(
(event) =>
event.type === "turn_item.updated" &&
event.turnItem.threadId === session.turnInput(1).threadId &&
event.turnItem.type === "dynamic_tool" &&
JSON.stringify(event.turnItem.output).includes("onetwo"),
),
);
assert.isTrue(
nested.some(
(event) =>
Expand Down
Loading
Loading