Skip to content

Commit 98b2f65

Browse files
committed
fix(server): preserve surviving ACP background work during cleanup
1 parent 7991073 commit 98b2f65

3 files changed

Lines changed: 45 additions & 21 deletions

File tree

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

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,19 +1356,17 @@ const program = Effect.gen(function* () {
13561356
status: "in_progress",
13571357
},
13581358
});
1359-
if (activeToolOutcome !== undefined) {
1360-
yield* agent.client.sessionUpdate({
1361-
sessionId: requestedSessionId,
1362-
update: {
1363-
sessionUpdate: "tool_call_update",
1364-
toolCallId: "tool-call-pending-wait",
1365-
title: "Waiting for task",
1366-
kind: "other",
1367-
status: "pending",
1368-
rawInput: { taskId: "framer" },
1369-
},
1370-
});
1371-
}
1359+
yield* agent.client.sessionUpdate({
1360+
sessionId: requestedSessionId,
1361+
update: {
1362+
sessionUpdate: "tool_call_update",
1363+
toolCallId: "tool-call-pending-wait",
1364+
title: "Waiting for task",
1365+
kind: "other",
1366+
status: "pending",
1367+
rawInput: { taskId: "framer" },
1368+
},
1369+
});
13721370
if (activeToolOutcome === "failed") {
13731371
return yield* AcpError.AcpRequestError.internalError("Prompt failed with an open tool");
13741372
}

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

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4982,19 +4982,36 @@ describe("AcpAdapterV2", () => {
49824982
}).pipe(Effect.provide(layerTest), Effect.scoped),
49834983
);
49844984

4985-
it.live.each(["cancelled", "failed", "end_turn"] as const)(
4986-
"closes unfinished ACP tools before a %s turn settles",
4987-
(outcome) =>
4985+
it.live.each([
4986+
{ outcome: "cancelled", background: false },
4987+
{ outcome: "failed", background: false },
4988+
{ outcome: "end_turn", background: false },
4989+
{ outcome: "cancelled", background: true },
4990+
{ outcome: "failed", background: true },
4991+
] as const)(
4992+
"closes unfinished ACP tools before a $outcome turn settles (background: $background)",
4993+
({ outcome, background }) =>
49884994
Effect.gen(function* () {
49894995
const path = yield* Path.Path;
49904996
const instanceId = ProviderInstanceId.make("acp-unfinished-tool");
49914997
const threadId = ThreadId.make("thread-acp-unfinished-tool");
4998+
let applyMutation: AcpAdapterV2ExtensionContext["applyBackgroundTaskMutation"] | undefined;
49924999
const adapter = yield* makeAcpAdapterV2({
49935000
instanceId,
49945001
selfInvocation: yield* resolveSelfInvocation(),
5002+
continuationRequests: { offer: () => Effect.void },
49955003
flavor: {
49965004
driver: ACP_TEST_DRIVER,
49975005
capabilities: AcpProviderCapabilitiesV2,
5006+
enablePostSettleContinuation: background,
5007+
extractBackgroundTaskId: (tool) =>
5008+
background && tool.toolCallId === "tool-call-long-running-1"
5009+
? "surviving-task"
5010+
: undefined,
5011+
registerExtensions: (context) =>
5012+
Effect.sync(() => {
5013+
applyMutation = context.applyBackgroundTaskMutation;
5014+
}),
49985015
makeRuntime: makeMockRuntime({
49995016
childProcessSpawner: yield* ChildProcessSpawner.ChildProcessSpawner,
50005017
mockAgentPath: yield* path.fromFileUrl(
@@ -5063,6 +5080,17 @@ describe("AcpAdapterV2", () => {
50635080
events.find((event) => event.type === "turn.terminal")?.status,
50645081
expectedStatus,
50655082
);
5083+
if (background) {
5084+
assert.ok(runtime.hasPendingBackgroundWork);
5085+
assert.ok(applyMutation);
5086+
assert.isTrue(yield* runtime.hasPendingBackgroundWork);
5087+
yield* applyMutation({
5088+
sessionId: "mock-session-1",
5089+
taskId: "surviving-task",
5090+
status: "completed",
5091+
});
5092+
assert.isFalse(yield* runtime.hasPendingBackgroundWork);
5093+
}
50665094
}).pipe(Effect.provide(layerTest), Effect.scoped),
50675095
);
50685096

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3529,12 +3529,12 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
35293529
yield* rearmDeferredFinalize(context);
35303530
return;
35313531
}
3532-
if (backgroundTaskId !== undefined) {
3532+
if (backgroundTaskId !== undefined && projectedStatus === undefined) {
35333533
context.toolCallIdsByBackgroundTaskId.set(backgroundTaskId, toolCall.toolCallId);
35343534
if (flavor.isPersistentBackgroundTool?.(toolCall) === true) {
35353535
context.persistentBackgroundTaskIds.add(backgroundTaskId);
35363536
}
3537-
const backgroundStatus = projectedStatus ?? toolStatus(toolCall.status);
3537+
const backgroundStatus = toolStatus(toolCall.status);
35383538
yield* setBackgroundTaskRunning(
35393539
backgroundTaskId,
35403540
backgroundStatus === "pending" || backgroundStatus === "running",
@@ -7142,9 +7142,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
71427142
}
71437143
if (settledStatus === "completed") {
71447144
yield* terminalizeOpenForegroundTools(context);
7145-
} else {
7146-
// Provider-side cancellation and prompt failures may omit final tool updates.
7147-
// Close their run-owned items before settlement so they cannot become phantom work.
7145+
} else if (options?.childTurnsContinue !== true) {
71487146
yield* terminalizeOpenRunOwnedItems(context, {
71497147
terminalizeSubagents: directStopQuarantine,
71507148
toolStatus: settledStatus === "failed" ? "failed" : "interrupted",

0 commit comments

Comments
 (0)