Skip to content

Commit 7023133

Browse files
committed
fix(server): close unfinished ACP tools when turns cancel or fail
1 parent 96f09c6 commit 7023133

3 files changed

Lines changed: 114 additions & 4 deletions

File tree

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const emitContentThenHang = process.env.T3_ACP_EMIT_CONTENT_THEN_HANG === "1";
4848
const emitPlanThenHang = process.env.T3_ACP_EMIT_PLAN_THEN_HANG === "1";
4949
const grokKillTaskShape = process.env.T3_ACP_GROK_KILL_TASK_SHAPE;
5050
const emitActiveToolThenHang = process.env.T3_ACP_EMIT_ACTIVE_TOOL_THEN_HANG === "1";
51+
const activeToolOutcome = process.env.T3_ACP_ACTIVE_TOOL_OUTCOME;
5152
const emitGrokMonitorPostTurnPoll = process.env.T3_ACP_EMIT_GROK_MONITOR_POST_TURN_POLL === "1";
5253
const emitGrokBackgroundTaskStarted = process.env.T3_ACP_EMIT_GROK_BACKGROUND_TASK_STARTED === "1";
5354
const emitGrokBackgroundTaskPoll = process.env.T3_ACP_GROK_BACKGROUND_TASK_COMPLETION === "poll";
@@ -1592,7 +1593,7 @@ const program = Effect.gen(function* () {
15921593
return yield* Effect.never;
15931594
}
15941595

1595-
if (emitActiveToolThenHang) {
1596+
if (emitActiveToolThenHang || activeToolOutcome !== undefined) {
15961597
const toolCallId = "tool-call-long-running-1";
15971598
yield* agent.client.sessionUpdate({
15981599
sessionId: requestedSessionId,
@@ -1613,6 +1614,25 @@ const program = Effect.gen(function* () {
16131614
status: "in_progress",
16141615
},
16151616
});
1617+
if (activeToolOutcome !== undefined) {
1618+
yield* agent.client.sessionUpdate({
1619+
sessionId: requestedSessionId,
1620+
update: {
1621+
sessionUpdate: "tool_call_update",
1622+
toolCallId: "tool-call-pending-wait",
1623+
title: "Waiting for task",
1624+
kind: "other",
1625+
status: "pending",
1626+
rawInput: { taskId: "framer" },
1627+
},
1628+
});
1629+
}
1630+
if (activeToolOutcome === "failed") {
1631+
return yield* AcpError.AcpRequestError.internalError("Prompt failed with an open tool");
1632+
}
1633+
if (activeToolOutcome === "cancelled" || activeToolOutcome === "end_turn") {
1634+
return yield* finishPrompt(requestedSessionId, activeToolOutcome);
1635+
}
16161636
return yield* Effect.never;
16171637
}
16181638
if (emitEmptySuccessfulBash || (emitEmptySuccessfulBashThenHang && promptCount === 1)) {

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

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3367,6 +3367,90 @@ describe("AcpAdapterV2", () => {
33673367
}).pipe(Effect.provide(layerTest), Effect.scoped),
33683368
);
33693369

3370+
it.live.each(["cancelled", "failed", "end_turn"] as const)(
3371+
"closes unfinished ACP tools before a %s turn settles",
3372+
(outcome) =>
3373+
Effect.gen(function* () {
3374+
const path = yield* Path.Path;
3375+
const instanceId = ProviderInstanceId.make("acp-unfinished-tool");
3376+
const threadId = ThreadId.make("thread-acp-unfinished-tool");
3377+
const adapter = yield* makeAcpAdapterV2({
3378+
instanceId,
3379+
selfInvocation: yield* resolveSelfInvocation(),
3380+
flavor: {
3381+
driver: ACP_TEST_DRIVER,
3382+
capabilities: AcpProviderCapabilitiesV2,
3383+
makeRuntime: makeMockRuntime({
3384+
childProcessSpawner: yield* ChildProcessSpawner.ChildProcessSpawner,
3385+
mockAgentPath: yield* path.fromFileUrl(
3386+
new URL("../../../scripts/acp-mock-agent.ts", import.meta.url),
3387+
),
3388+
environment: { T3_ACP_ACTIVE_TOOL_OUTCOME: outcome },
3389+
}),
3390+
},
3391+
});
3392+
const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
3393+
runtimeMode: "full-access",
3394+
interactionMode: "default",
3395+
cwd: process.cwd(),
3396+
});
3397+
const modelSelection = { instanceId, model: "default" } as const;
3398+
const runtime = yield* adapter.openSession({
3399+
threadId,
3400+
providerSessionId: ProviderSessionId.make("session-acp-unfinished-tool"),
3401+
modelSelection,
3402+
runtimePolicy,
3403+
});
3404+
const providerThread = yield* runtime.ensureThread({
3405+
threadId,
3406+
modelSelection,
3407+
runtimePolicy,
3408+
});
3409+
yield* runtime.startTurn(
3410+
makeTurnInput({
3411+
threadId,
3412+
providerThread,
3413+
instanceId,
3414+
runtimePolicy,
3415+
now: yield* DateTime.now,
3416+
}),
3417+
);
3418+
const events = yield* runtime.events.pipe(
3419+
Stream.takeUntil((event) => event.type === "turn.terminal"),
3420+
Stream.runCollect,
3421+
);
3422+
const toolUpdates = events.flatMap((event) =>
3423+
event.type === "turn_item.updated" &&
3424+
event.turnItem.nativeItemRef?.nativeId === "tool-call-long-running-1"
3425+
? [event.turnItem]
3426+
: [],
3427+
);
3428+
const expectedStatus = outcome === "end_turn" ? "completed" : outcome;
3429+
assert.include(
3430+
toolUpdates.map((item) => item.status),
3431+
"running",
3432+
);
3433+
const lastTool = toolUpdates.at(-1);
3434+
assert.equal(lastTool?.status, outcome === "cancelled" ? "interrupted" : expectedStatus);
3435+
assert.isNotNull(lastTool?.completedAt);
3436+
const lastWait = events.findLast(
3437+
(event) =>
3438+
event.type === "turn_item.updated" &&
3439+
event.turnItem.nativeItemRef?.nativeId === "tool-call-pending-wait",
3440+
);
3441+
assert.equal(lastWait?.type, "turn_item.updated");
3442+
if (lastWait?.type === "turn_item.updated") {
3443+
assert.equal(lastWait.turnItem.type, "dynamic_tool");
3444+
assert.equal(lastWait.turnItem.status, lastTool?.status);
3445+
assert.isNotNull(lastWait.turnItem.completedAt);
3446+
}
3447+
assert.equal(
3448+
events.find((event) => event.type === "turn.terminal")?.status,
3449+
expectedStatus,
3450+
);
3451+
}).pipe(Effect.provide(layerTest), Effect.scoped),
3452+
);
3453+
33703454
it.live("terminalizes an empty successful foreground Bash tool when the turn completes", () =>
33713455
Effect.gen(function* () {
33723456
const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;

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

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

68966896
const terminalizeOpenRunOwnedItems = Effect.fnUntraced(function* (
68976897
context: ActiveAcpTurn,
6898-
options: { readonly terminalizeSubagents: boolean },
6898+
options: {
6899+
readonly terminalizeSubagents: boolean;
6900+
readonly toolStatus: "interrupted" | "failed";
6901+
},
68996902
) {
69006903
for (const tool of context.tools.values()) {
69016904
const status = toolStatus(tool.status);
69026905
if (status === "pending" || status === "running" || status === "waiting") {
6903-
yield* emitTool(context, tool, "interrupted");
6906+
yield* emitTool(context, tool, options.toolStatus);
69046907
}
69056908
}
69066909
if (!options.terminalizeSubagents) return;
@@ -6974,9 +6977,12 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
69746977
}
69756978
if (settledStatus === "completed") {
69766979
yield* terminalizeOpenForegroundTools(context);
6977-
} else if (settledStatus === "interrupted") {
6980+
} else {
6981+
// Provider-side cancellation and prompt failures may omit final tool updates.
6982+
// Close their run-owned items before settlement so they cannot become phantom work.
69786983
yield* terminalizeOpenRunOwnedItems(context, {
69796984
terminalizeSubagents: directStopQuarantine,
6985+
toolStatus: settledStatus === "failed" ? "failed" : "interrupted",
69806986
});
69816987
}
69826988
yield* closeTextStreams(context);

0 commit comments

Comments
 (0)