Skip to content

Commit fd1adc8

Browse files
StiensWoutcodexclaude
authored
fix(acp): retain streamed child tool output (pingdotgg#32411)
Co-authored-by: GPT 6.1 Sol <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 1b95213 commit fd1adc8

2 files changed

Lines changed: 393 additions & 10 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/AcpRegistryAdapterV2.test.ts‎

Lines changed: 327 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ const openClaudeReplaySession = Effect.fn("openClaudeReplaySession")(function* (
208208
entries: ReadonlyArray<unknown>,
209209
replayGate?: ProviderReplayGate,
210210
continuationEnabled = true,
211+
agentId = "claude-acp",
211212
) {
212213
const fileSystem = yield* FileSystem.FileSystem;
213214
const path = yield* Path.Path;
@@ -248,7 +249,7 @@ const openClaudeReplaySession = Effect.fn("openClaudeReplaySession")(function* (
248249
const adapterOptions = {
249250
selfInvocation: yield* resolveSelfInvocation(),
250251
instanceId,
251-
settings: yield* decodeAcpRegistryAdapterSettings({ agentId: "claude-acp" }),
252+
settings: yield* decodeAcpRegistryAdapterSettings({ agentId }),
252253
environment: {},
253254
...(continuationEnabled ? { continuationRequests } : {}),
254255
makeRuntime: (runtimeInput: Parameters<typeof replayRuntime>[0]) => {
@@ -1808,6 +1809,290 @@ describe("AcpRegistryAdapterV2", () => {
18081809
}).pipe(Effect.provide(layerTest), Effect.scoped),
18091810
);
18101811

1812+
it.effect("retains streamed child tool output through the generic Devin route", () =>
1813+
Effect.gen(function* () {
1814+
const { update } = claudeFrames;
1815+
const session = yield* openClaudeReplaySession(
1816+
[
1817+
...claudeFrames.open,
1818+
...claudeFrames.prompt(1),
1819+
update("child-1", {
1820+
sessionUpdate: "tool_call_update",
1821+
toolCallId: "early-tool",
1822+
status: "in_progress",
1823+
rawInput: { command: "echo early" },
1824+
}),
1825+
update("child-1", {
1826+
sessionUpdate: "tool_call_content_chunk",
1827+
toolCallId: "early-tool",
1828+
content: { type: "content", content: { type: "text", text: "early" } },
1829+
}),
1830+
update("child-1", {
1831+
sessionUpdate: "tool_call_update",
1832+
toolCallId: "early-tool",
1833+
status: "completed",
1834+
}),
1835+
update(CLAUDE_ROOT_SESSION, {
1836+
sessionUpdate: "tool_call_update",
1837+
toolCallId: "spawn",
1838+
status: "in_progress",
1839+
_meta: { "cognition.ai/subagent_started": { agentId: "child-1", task: "Review" } },
1840+
}),
1841+
...[CLAUDE_ROOT_SESSION, "child-1"].map((sessionId) =>
1842+
update(sessionId, {
1843+
sessionUpdate: "tool_call_update",
1844+
toolCallId: "shared-tool",
1845+
title: "Read package",
1846+
kind: "execute",
1847+
status: "in_progress",
1848+
rawInput: { command: "cat package.json" },
1849+
...(sessionId === "child-1"
1850+
? { content: [{ type: "content", content: { type: "text", text: "one" } }] }
1851+
: {}),
1852+
}),
1853+
),
1854+
update("child-1", {
1855+
sessionUpdate: "tool_call_content_chunk",
1856+
toolCallId: "shared-tool",
1857+
content: { type: "content", content: { type: "text", text: "two" } },
1858+
}),
1859+
update("child-1", {
1860+
sessionUpdate: "tool_call_update",
1861+
toolCallId: "shared-tool",
1862+
status: "completed",
1863+
}),
1864+
update("child-1", {
1865+
sessionUpdate: "tool_call_update",
1866+
toolCallId: "shared-tool",
1867+
content: [{ type: "content", content: { type: "text", text: "replacement" } }],
1868+
}),
1869+
update("child-1", {
1870+
sessionUpdate: "tool_call_update",
1871+
toolCallId: "shared-tool",
1872+
content: [],
1873+
}),
1874+
update("child-1", {
1875+
sessionUpdate: "tool_call_update",
1876+
toolCallId: "shared-tool",
1877+
rawOutput: { stdout: "structured", stderr: "" },
1878+
}),
1879+
update("child-1", {
1880+
sessionUpdate: "tool_call_update",
1881+
toolCallId: "shared-tool",
1882+
content: [{ type: "content", content: { type: "text", text: "display" } }],
1883+
}),
1884+
update("child-1", {
1885+
sessionUpdate: "tool_call_update",
1886+
toolCallId: "shared-tool",
1887+
rawOutput: { type: "Text", text: "vendor" },
1888+
}),
1889+
update("child-1", {
1890+
sessionUpdate: "tool_call_update",
1891+
toolCallId: "shared-tool",
1892+
content: [{ type: "content", content: { type: "text", text: "display" } }],
1893+
}),
1894+
claudeMarker("tools complete"),
1895+
claudeFrames.idle("end_turn"),
1896+
claudeFrames.closeRequest,
1897+
claudeFrames.closed,
1898+
],
1899+
undefined,
1900+
false,
1901+
"devin",
1902+
);
1903+
yield* session.runtime.startTurn(session.turnInput(1));
1904+
const events = yield* session.throughMarker("tools complete");
1905+
const child = events.find((event) => event.type === "subagent.updated");
1906+
if (child?.type !== "subagent.updated") throw new Error("Missing child");
1907+
const tools = events.flatMap((event) =>
1908+
event.type === "turn_item.updated" && event.turnItem.type === "dynamic_tool"
1909+
? [event.turnItem]
1910+
: [],
1911+
);
1912+
const childTools = tools.filter((item) => item.threadId === child.subagent.childThreadId);
1913+
assert.deepEqual(
1914+
childTools.map((item) => item.output),
1915+
[
1916+
[{ type: "content", content: { type: "text", text: "one" } }],
1917+
[{ type: "content", content: { type: "text", text: "onetwo" } }],
1918+
[{ type: "content", content: { type: "text", text: "onetwo" } }],
1919+
[{ type: "content", content: { type: "text", text: "replacement" } }],
1920+
[],
1921+
{ stdout: "structured", stderr: "" },
1922+
{ stdout: "structured", stderr: "" },
1923+
{ type: "Text", text: "vendor" },
1924+
{ type: "Text", text: "vendor" },
1925+
],
1926+
);
1927+
assert.equal(new Set(childTools.map((item) => item.id)).size, 1);
1928+
assert.equal(childTools.at(-1)?.nativeItemRef?.nativeId, "child-1:tool:shared-tool");
1929+
assert.equal(childTools.at(-1)?.status, "completed");
1930+
assert.deepEqual(childTools.at(-1)?.input, { command: "cat package.json" });
1931+
const parentTools = events.flatMap((event) =>
1932+
event.type === "turn_item.updated" &&
1933+
event.turnItem.type === "command_execution" &&
1934+
event.turnItem.threadId === session.turnInput(1).threadId &&
1935+
event.turnItem.nativeItemRef?.nativeId === "shared-tool"
1936+
? [event.turnItem]
1937+
: [],
1938+
);
1939+
assert.lengthOf(parentTools, 1);
1940+
assert.isUndefined(parentTools[0]?.output);
1941+
assert.isFalse(
1942+
events.some(
1943+
(event) => event.type === "message.updated" && event.message.text.includes("onetwo"),
1944+
),
1945+
);
1946+
}).pipe(Effect.provide(layerTest), Effect.scoped),
1947+
);
1948+
1949+
it.effect("keeps stopped child tools interrupted when a late output chunk arrives", () =>
1950+
Effect.gen(function* () {
1951+
const { update } = claudeFrames;
1952+
const session = yield* openClaudeReplaySession(
1953+
[
1954+
...claudeFrames.open.map((entry) =>
1955+
entry.frame.method === "initialize" && entry.type === "emit_inbound"
1956+
? {
1957+
...entry,
1958+
frame: {
1959+
...entry.frame,
1960+
result: { protocolVersion: 1, agentCapabilities: {}, authMethods: [] },
1961+
},
1962+
}
1963+
: entry,
1964+
),
1965+
{
1966+
type: "expect_outbound",
1967+
frame: { kind: "request", method: "session/prompt", params: "<any>" },
1968+
},
1969+
update(CLAUDE_ROOT_SESSION, {
1970+
sessionUpdate: "tool_call_update",
1971+
toolCallId: "spawn",
1972+
status: "in_progress",
1973+
_meta: { "cognition.ai/subagent_started": { agentId: "child-1", task: "Review" } },
1974+
}),
1975+
update("child-1", {
1976+
sessionUpdate: "tool_call_update",
1977+
toolCallId: "stopped-tool",
1978+
status: "in_progress",
1979+
}),
1980+
claudeMarker("before cancel"),
1981+
{
1982+
type: "expect_outbound",
1983+
frame: {
1984+
kind: "notification",
1985+
method: "session/cancel",
1986+
params: { sessionId: CLAUDE_ROOT_SESSION },
1987+
},
1988+
},
1989+
update("child-1", {
1990+
sessionUpdate: "tool_call_update",
1991+
toolCallId: "stopped-tool",
1992+
status: "failed",
1993+
}),
1994+
update("child-1", {
1995+
sessionUpdate: "tool_call_update",
1996+
toolCallId: "stopped-tool",
1997+
content: [{ type: "content", content: { type: "text", text: "stopping" } }],
1998+
}),
1999+
update("child-1", {
2000+
sessionUpdate: "tool_call_content_chunk",
2001+
toolCallId: "stopped-tool",
2002+
content: { type: "content", content: { type: "text", text: "cancelled" } },
2003+
}),
2004+
claudeMarker("late output"),
2005+
update(CLAUDE_ROOT_SESSION, {
2006+
sessionUpdate: "tool_call_update",
2007+
toolCallId: "spawn",
2008+
status: "failed",
2009+
_meta: { "cognition.ai/subagent_completed": { agentId: "child-1", success: false } },
2010+
}),
2011+
{
2012+
type: "emit_inbound",
2013+
frame: {
2014+
kind: "response",
2015+
method: "session/prompt",
2016+
result: { stopReason: "cancelled" },
2017+
},
2018+
},
2019+
claudeFrames.closeRequest,
2020+
claudeFrames.closed,
2021+
],
2022+
undefined,
2023+
false,
2024+
"devin",
2025+
);
2026+
const starting = yield* session.runtime
2027+
.startTurn(session.turnInput(1))
2028+
.pipe(Effect.forkScoped);
2029+
const started = yield* session.throughMarker("before cancel");
2030+
const running = started.findLast((event) => event.type === "provider_turn.updated");
2031+
if (running?.type !== "provider_turn.updated") throw new Error("Missing active turn");
2032+
const closing = yield* session.runtime
2033+
.interruptTurn({
2034+
providerThread: session.providerThread,
2035+
providerTurnId: running.providerTurn.id,
2036+
})
2037+
.pipe(Effect.forkScoped);
2038+
const events = yield* session.throughMarker("late output");
2039+
yield* Fiber.join(closing);
2040+
yield* Fiber.join(starting);
2041+
const tools = events.flatMap((event) =>
2042+
event.type === "turn_item.updated" && event.turnItem.type === "dynamic_tool"
2043+
? [event.turnItem]
2044+
: [],
2045+
);
2046+
assert.deepEqual(
2047+
tools.map((item) => item.status),
2048+
["interrupted", "interrupted", "interrupted"],
2049+
);
2050+
assert.deepEqual(tools.at(-1)?.output, [
2051+
{ type: "content", content: { type: "text", text: "stoppingcancelled" } },
2052+
]);
2053+
}).pipe(Effect.provide(layerTest), Effect.scoped),
2054+
);
2055+
2056+
it.effect("keeps native child tools interrupted after idle and late output", () =>
2057+
Effect.gen(function* () {
2058+
const { update, childState } = claudeFrames;
2059+
const session = yield* openClaudeReplaySession([
2060+
...claudeFrames.open,
2061+
...claudeFrames.prompt(1),
2062+
...claudeChild.slice(0, -1),
2063+
update("child-1", {
2064+
sessionUpdate: "tool_call_update",
2065+
toolCallId: "unfinished",
2066+
status: "in_progress",
2067+
}),
2068+
childState(CLAUDE_ROOT_SESSION, "child-1", "idle"),
2069+
update("child-1", {
2070+
sessionUpdate: "tool_call_content_chunk",
2071+
toolCallId: "unfinished",
2072+
content: { type: "content", content: { type: "text", text: "late" } },
2073+
}),
2074+
claudeMarker("late native output"),
2075+
claudeFrames.idle("end_turn"),
2076+
claudeFrames.closeRequest,
2077+
claudeFrames.closed,
2078+
]);
2079+
yield* session.runtime.startTurn(session.turnInput(1));
2080+
const events = yield* session.throughMarker("late native output");
2081+
const tools = events.flatMap((event) =>
2082+
event.type === "turn_item.updated" && event.turnItem.type === "dynamic_tool"
2083+
? [event.turnItem]
2084+
: [],
2085+
);
2086+
assert.deepEqual(
2087+
tools.map((tool) => tool.status),
2088+
["running", "interrupted", "interrupted"],
2089+
);
2090+
assert.deepEqual(tools.at(-1)?.output, [
2091+
{ type: "content", content: { type: "text", text: "late" } },
2092+
]);
2093+
}).pipe(Effect.provide(layerTest), Effect.scoped),
2094+
);
2095+
18112096
it.effect.each(["error", "end_turn"])(
18122097
"hands live Claude child tools to the next run after %s and keeps nested output in the child",
18132098
(stopReason) =>
@@ -1899,6 +2184,18 @@ describe("AcpRegistryAdapterV2", () => {
18992184
status: "in_progress",
19002185
rawInput: { command: "cat nested/package.json" },
19012186
}),
2187+
...["one", "two"].map((text) =>
2188+
update("grandchild", {
2189+
sessionUpdate: "tool_call_content_chunk",
2190+
toolCallId: "nested-tool",
2191+
content: { type: "content", content: { type: "text", text } },
2192+
}),
2193+
),
2194+
update("grandchild", {
2195+
sessionUpdate: "tool_call_update",
2196+
toolCallId: "nested-tool",
2197+
status: "completed",
2198+
}),
19022199
update("grandchild", {
19032200
sessionUpdate: "agent_message_chunk",
19042201
messageId: "nested-reply",
@@ -1996,7 +2293,7 @@ describe("AcpRegistryAdapterV2", () => {
19962293
if (messages[0]?.type === "message.updated") {
19972294
assert.equal(messages[0].message.threadId, row.subagent.childThreadId);
19982295
}
1999-
const nestedTool = nested.find(
2296+
const nestedTool = nested.findLast(
20002297
(event) =>
20012298
event.type === "turn_item.updated" &&
20022299
event.turnItem.type === "dynamic_tool" &&
@@ -2006,9 +2303,36 @@ describe("AcpRegistryAdapterV2", () => {
20062303
assert.isDefined(nestedTool);
20072304
if (nestedTool?.type === "turn_item.updated") {
20082305
assert.equal(nestedTool.turnItem.threadId, row.subagent.childThreadId);
2009-
if (nestedTool.turnItem.type === "dynamic_tool")
2306+
if (nestedTool.turnItem.type === "dynamic_tool") {
20102307
assert.deepEqual(nestedTool.turnItem.input, { command: "cat nested/package.json" });
2308+
assert.equal(nestedTool.turnItem.status, "completed");
2309+
}
2310+
const toolUpdates = nested.flatMap((event) =>
2311+
event.type === "turn_item.updated" &&
2312+
event.turnItem.type === "dynamic_tool" &&
2313+
event.turnItem.id === nestedTool.turnItem.id
2314+
? [event.turnItem]
2315+
: [],
2316+
);
2317+
assert.deepEqual(
2318+
toolUpdates.map((item) => item.output),
2319+
[
2320+
null,
2321+
[{ type: "content", content: { type: "text", text: "one" } }],
2322+
[{ type: "content", content: { type: "text", text: "onetwo" } }],
2323+
[{ type: "content", content: { type: "text", text: "onetwo" } }],
2324+
],
2325+
);
20112326
}
2327+
assert.isFalse(
2328+
nested.some(
2329+
(event) =>
2330+
event.type === "turn_item.updated" &&
2331+
event.turnItem.threadId === session.turnInput(1).threadId &&
2332+
event.turnItem.type === "dynamic_tool" &&
2333+
JSON.stringify(event.turnItem.output).includes("onetwo"),
2334+
),
2335+
);
20122336
assert.isTrue(
20132337
nested.some(
20142338
(event) =>

0 commit comments

Comments
 (0)