Skip to content

Commit 6a1ce59

Browse files
fix(server): recover terminal-fallback MCP calls in ACP child sessions
Child-session tool calls skipped the embedded terminal commands, so a Devin subagent's acp-mcp-call fallback showed as a generic "Ran command" tool instead of the T3 tool it ran. Share the root path's lookup and check both the child's session and the root session, since Devin routes child updates out of the root session after terminals are recorded. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 6ecaccb commit 6a1ce59

2 files changed

Lines changed: 62 additions & 13 deletions

File tree

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,9 +1008,12 @@ describe("AcpAdapterV2", () => {
10081008
);
10091009
type Runtime = AcpSessionRuntime.AcpSessionRuntime["Service"];
10101010
let handler: Parameters<Runtime["handleSessionUpdate"]>[0] | undefined;
1011+
let createTerminal: Parameters<Runtime["handleCreateTerminal"]>[0] | undefined;
10111012
const instanceId = ProviderInstanceId.make("devin-replay");
10121013
const adapter = makeAcpAdapterV2({
10131014
instanceId,
1015+
// Production Devin runs commands through client terminals.
1016+
clientTerminals: { childProcessSpawner, shellCommands: true },
10141017
crypto: yield* Crypto.Crypto,
10151018
fileSystem: yield* FileSystem.FileSystem,
10161019
idAllocator,
@@ -1031,9 +1034,18 @@ describe("AcpAdapterV2", () => {
10311034
Effect.sync(() => {
10321035
handler = next;
10331036
}).pipe(Effect.andThen(runtime.handleSessionUpdate(next))),
1037+
handleCreateTerminal: (next) =>
1038+
Effect.sync(() => {
1039+
createTerminal = next;
1040+
}).pipe(Effect.andThen(runtime.handleCreateTerminal(next))),
10341041
prompt: () =>
10351042
Effect.gen(function* () {
10361043
assert.isDefined(handler);
1044+
assert.isDefined(createTerminal);
1045+
const fallbackTerminal = yield* createTerminal(
1046+
{ sessionId: "mock-session-1", command: "true acp-mcp-call task_status {}" },
1047+
{ requestId: "child-mcp-terminal", method: "terminal/create" },
1048+
);
10371049
// Production thread 54aeb6d7 split after "(command". Metadata shapes
10381050
// below were captured from live Devin sessions showy-mile/fragrant-chamomile.
10391051
const updates = [
@@ -1094,6 +1106,15 @@ describe("AcpAdapterV2", () => {
10941106
"cognition.ai/subagent_context": { parentAgentId: "child-a" },
10951107
},
10961108
},
1109+
{
1110+
sessionUpdate: "tool_call",
1111+
toolCallId: "child-mcp-fallback",
1112+
title: "Ran command",
1113+
kind: "execute",
1114+
status: "completed",
1115+
content: [{ type: "terminal", terminalId: fallbackTerminal.terminalId }],
1116+
_meta: { "cognition.ai/subagent_context": { parentAgentId: "child-a" } },
1117+
},
10971118
{
10981119
sessionUpdate: "tool_call",
10991120
toolCallId: "parent-weather",
@@ -1225,6 +1246,15 @@ describe("AcpAdapterV2", () => {
12251246
);
12261247
assert.deepEqual([...childMessages.values()], ["Checking the code.", "ONE"]);
12271248
assert.equal(task?.prompt, "Run pwd, then reply ONE.");
1249+
// Terminal-fallback MCP calls in a child session keep their T3 identity.
1250+
assert.isTrue(
1251+
items.some(
1252+
(item) =>
1253+
item.threadId === task?.childThreadId &&
1254+
item.type === "dynamic_tool" &&
1255+
item.toolName === "t3-code.task_status",
1256+
),
1257+
);
12281258
const childMcp = items.find(
12291259
(item) =>
12301260
item.threadId === task?.childThreadId &&

‎apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.ts‎

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,6 +1589,21 @@ export function makeAcpAdapterV2(
15891589
embeddedTerminalsByToolCallId.delete(oldest);
15901590
}
15911591
};
1592+
// Command lines of the terminals embedded in a tool call, so MCP calls
1593+
// made through the acp-mcp-call terminal fallback keep their identity.
1594+
const embeddedTerminalCommands = (
1595+
sessionId: string,
1596+
toolCallId: string,
1597+
): ReadonlyArray<string> =>
1598+
(
1599+
embeddedTerminalsByToolCallId.get(sessionScopedId(sessionId, toolCallId))
1600+
?.terminalIds ?? []
1601+
).flatMap((terminalId) => {
1602+
const command =
1603+
clientTerminals?.readCommandLine(terminalId) ??
1604+
agentTerminalsById.get(sessionScopedId(sessionId, terminalId))?.command;
1605+
return command === undefined ? [] : [command];
1606+
});
15921607
// Client terminals (Devin) run with the T3 server's privileges, so they
15931608
// are policy-checked against the active turn policy; a command the user
15941609
// already approved satisfies an "ask" disposition.
@@ -3224,17 +3239,10 @@ export function makeAcpAdapterV2(
32243239
// agent-specific shape and project the same branded dynamic_tool
32253240
// item native providers produce (e.g. the T3 orchestration tools).
32263241
const mcpIdentity = extractMcpToolCallIdentity(toolCall, {
3227-
embeddedTerminalCommands: (
3228-
embeddedTerminalsByToolCallId.get(
3229-
sessionScopedId(context.nativeThreadId, toolCall.toolCallId),
3230-
)?.terminalIds ?? []
3231-
).flatMap((terminalId) => {
3232-
const command =
3233-
clientTerminals?.readCommandLine(terminalId) ??
3234-
agentTerminalsById.get(sessionScopedId(context.nativeThreadId, terminalId))
3235-
?.command;
3236-
return command === undefined ? [] : [command];
3237-
}),
3242+
embeddedTerminalCommands: embeddedTerminalCommands(
3243+
context.nativeThreadId,
3244+
toolCall.toolCallId,
3245+
),
32383246
});
32393247
let turnItem: OrchestrationV2TurnItem;
32403248
if (toolCall.toolCallId.startsWith("acp-compaction:")) {
@@ -4263,7 +4271,8 @@ export function makeAcpAdapterV2(
42634271
return;
42644272
}
42654273
if (context.finalized) return;
4266-
if (notification.sessionId !== (yield* Ref.get(activeSessionId))) {
4274+
const rootSessionId = yield* Ref.get(activeSessionId);
4275+
if (notification.sessionId !== rootSessionId) {
42674276
// Finalize may have completed during the activeSessionId yield.
42684277
if (context.finalized) return;
42694278
if (flavor.extractSubagentUpdate === undefined) return;
@@ -4291,7 +4300,17 @@ export function makeAcpAdapterV2(
42914300
const key = `${nativeTaskId}:tool:${toolCall.toolCallId}`;
42924301
const merged = mergeToolCallState(context.tools.get(key), toolCall);
42934302
context.tools.set(key, merged);
4294-
const mcpIdentity = extractMcpToolCallIdentity(merged);
4303+
// Terminals are remembered under the raw session id: the child's
4304+
// own session, or the root one when the flavor routes child
4305+
// updates out of it (Devin).
4306+
const mcpIdentity = extractMcpToolCallIdentity(merged, {
4307+
embeddedTerminalCommands: [
4308+
...embeddedTerminalCommands(notification.sessionId, toolCall.toolCallId),
4309+
...(rootSessionId === null
4310+
? []
4311+
: embeddedTerminalCommands(rootSessionId, toolCall.toolCallId)),
4312+
],
4313+
});
42954314
const now = yield* DateTime.now;
42964315
const status = toolStatus(merged.status);
42974316
const startedAt = context.toolStartedAt.get(key) ?? now;

0 commit comments

Comments
 (0)