Skip to content

Commit 7a35053

Browse files
fix(acp): failed turns settle only root foreground tools
Reuse the completed-turn foreground cleanup with a failed status instead of the interrupt cleanup, so background monitors stay registered and subagents keep running. Skip child-session tool entries in that cleanup: they are keyed by task and emitTool would reproject them as new root tools. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent e8ed366 commit 7a35053

3 files changed

Lines changed: 88 additions & 23 deletions

File tree

‎apps/server/scripts/acp-mock-agent.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,39 @@ const program = Effect.gen(function* () {
905905
rawInput: { command: ["long-running-tool"] },
906906
},
907907
});
908+
writeJsonRpcNotification("session/update", {
909+
sessionId: requestedSessionId,
910+
update: {
911+
sessionUpdate: "tool_call_update",
912+
toolCallId: "tool-call-idle-error-monitor",
913+
title: "Monitor",
914+
kind: "execute",
915+
status: "in_progress",
916+
rawInput: { command: ["background-monitor"] },
917+
},
918+
});
919+
writeJsonRpcNotification("session/update", {
920+
sessionId: requestedSessionId,
921+
update: {
922+
sessionUpdate: "tool_call_update",
923+
toolCallId: "tool-call-idle-error-subagent",
924+
title: "Subagent",
925+
kind: "other",
926+
status: "in_progress",
927+
rawInput: {},
928+
},
929+
});
930+
writeJsonRpcNotification("session/update", {
931+
sessionId: "mock-child-session-idle-error",
932+
update: {
933+
sessionUpdate: "tool_call_update",
934+
toolCallId: "tool-call-idle-error-child",
935+
title: "Child tool",
936+
kind: "execute",
937+
status: "in_progress",
938+
rawInput: { command: ["child-tool"] },
939+
},
940+
});
908941
writeJsonRpcNotification("session/update", {
909942
sessionId: requestedSessionId,
910943
update: {

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

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -611,12 +611,30 @@ describe("AcpAdapterV2", () => {
611611
const path = yield* Path.Path;
612612
const instanceId = ProviderInstanceId.make(`idle-error-${details}`);
613613
const threadId = ThreadId.make(`thread-idle-error-${details}`);
614+
const childSessionId = "mock-child-session-idle-error";
614615
const adapter = yield* makeAcpAdapterV2({
615616
instanceId,
616617
selfInvocation: yield* resolveSelfInvocation(),
617618
flavor: {
618619
driver: ACP_TEST_DRIVER,
619620
capabilities: AcpProviderCapabilitiesV2,
621+
extractBackgroundTaskId: (toolCall) =>
622+
toolCall.toolCallId === "tool-call-idle-error-monitor"
623+
? "idle-error-monitor"
624+
: undefined,
625+
extractSubagentUpdate: (toolCall) =>
626+
toolCall.toolCallId === "tool-call-idle-error-subagent"
627+
? {
628+
nativeTaskId: "task-idle-error",
629+
prompt: "child work",
630+
title: "child work",
631+
model: null,
632+
status: "running",
633+
childSessionId,
634+
result: null,
635+
suppressNormalTool: true,
636+
}
637+
: undefined,
620638
makeRuntime: makeMockRuntime({
621639
childProcessSpawner: yield* ChildProcessSpawner.ChildProcessSpawner,
622640
mockAgentPath: yield* path.fromFileUrl(
@@ -682,14 +700,32 @@ describe("AcpAdapterV2", () => {
682700
retryable: null,
683701
},
684702
);
685-
const toolStatuses = failedEvents.flatMap((event) =>
686-
event.type === "turn_item.updated" &&
687-
event.turnItem.nativeItemRef?.nativeId === "tool-call-idle-error-1"
688-
? [event.turnItem.status]
689-
: [],
690-
);
703+
const toolItems = (nativeId: string) =>
704+
failedEvents.flatMap((event) =>
705+
event.type === "turn_item.updated" &&
706+
event.turnItem.nativeItemRef?.nativeId === nativeId
707+
? [event.turnItem]
708+
: [],
709+
);
710+
const toolStatuses = toolItems("tool-call-idle-error-1").map((item) => item.status);
691711
assert.include(toolStatuses, "running");
692-
assert.notInclude(["pending", "running", "waiting"], toolStatuses.at(-1));
712+
assert.equal(toolStatuses.at(-1), "failed");
713+
assert.deepEqual(
714+
toolItems("tool-call-idle-error-monitor").map((item) => item.status),
715+
["running"],
716+
"a background monitor outlives the failed turn and keeps its running row",
717+
);
718+
const childItems = toolItems("task-idle-error:tool:tool-call-idle-error-child");
719+
assert.deepEqual(
720+
childItems.map((item) => item.status),
721+
["running"],
722+
"a child-session tool keeps running in its own thread",
723+
);
724+
assert.isTrue(childItems.every((item) => item.threadId !== threadId));
725+
assert.isEmpty(
726+
toolItems("tool-call-idle-error-child"),
727+
"the failed turn must not reproject the child tool as a root tool",
728+
);
693729

694730
const nextEvents = yield* runTurn(2);
695731
const next = nextEvents.at(-1);

‎packages/provider-acp/src/server/adapter.ts‎

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6062,15 +6062,12 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
60626062

60636063
const terminalizeOpenRunOwnedItems = Effect.fnUntraced(function* (
60646064
context: ActiveAcpTurn,
6065-
options: {
6066-
readonly toolStatus: "interrupted" | "failed";
6067-
readonly terminalizeSubagents: boolean;
6068-
},
6065+
options: { readonly terminalizeSubagents: boolean },
60696066
) {
60706067
for (const tool of context.tools.values()) {
60716068
const status = toolStatus(tool.status);
60726069
if (status === "pending" || status === "running" || status === "waiting") {
6073-
yield* emitTool(context, tool, options.toolStatus);
6070+
yield* emitTool(context, tool, "interrupted");
60746071
}
60756072
}
60766073
if (!options.terminalizeSubagents) return;
@@ -6093,10 +6090,15 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
60936090

60946091
const terminalizeOpenForegroundTools = Effect.fnUntraced(function* (
60956092
context: ActiveAcpTurn,
6093+
status: "completed" | "failed",
60966094
) {
6097-
for (const tool of context.tools.values()) {
6095+
for (const [key, tool] of context.tools) {
6096+
// Child-session tools live under a `${nativeTaskId}:tool:` key and
6097+
// belong to a subagent that outlives this turn; emitTool would
6098+
// reproject them as new root tools.
6099+
if (key !== tool.toolCallId) continue;
60986100
if (!acpCompletedTurnShouldTerminalizeTool(tool, flavor)) continue;
6099-
yield* emitTool(context, tool, "completed");
6101+
yield* emitTool(context, tool, status);
61006102
}
61016103
});
61026104

@@ -6140,18 +6142,12 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
61406142
});
61416143
}
61426144
}
6143-
if (settledStatus === "completed") {
6144-
yield* terminalizeOpenForegroundTools(context);
6145+
if (settledStatus === "completed" || settledStatus === "failed") {
6146+
yield* terminalizeOpenForegroundTools(context, settledStatus);
61456147
} else if (settledStatus === "interrupted") {
61466148
yield* terminalizeOpenRunOwnedItems(context, {
6147-
toolStatus: "interrupted",
61486149
terminalizeSubagents: directStopQuarantine,
61496150
});
6150-
} else if (settledStatus === "failed") {
6151-
yield* terminalizeOpenRunOwnedItems(context, {
6152-
toolStatus: "failed",
6153-
terminalizeSubagents: false,
6154-
});
61556151
}
61566152
yield* closeTextStreams(context);
61576153
if (settledStatus !== "failed") {
@@ -6695,7 +6691,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
66956691
if (result.stopReason === "error") {
66966692
yield* finalizeTurn(
66976693
context,
6698-
context.interrupted ? "interrupted" : "failed",
6694+
"failed",
66996695
makeProviderFailure({
67006696
class: "provider_error",
67016697
message: result.error?.message,

0 commit comments

Comments
 (0)