diff --git a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts index d71b0f7b5e2b8..c52cb3900f253 100644 --- a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts @@ -611,6 +611,7 @@ const makePromptInactivityHarness = Effect.fnUntraced(function* (input: { prompt: AcpSessionRuntime.AcpSessionRuntime["Service"]["prompt"], ) => AcpSessionRuntime.AcpSessionRuntime["Service"]["prompt"]; readonly cancelled?: Deferred.Deferred; + readonly followAgentWorkState?: boolean; }) { const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner; const natives: Array = []; @@ -621,6 +622,7 @@ const makePromptInactivityHarness = Effect.fnUntraced(function* (input: { driver: ACP_TEST_DRIVER, capabilities: AcpProviderCapabilitiesV2, promptInactivityTimeout: "15 minutes", + ...(input.followAgentWorkState === true ? { followAgentWorkState: true } : {}), makeRuntime: makeMockRuntime({ childProcessSpawner, mockAgentPath: yield* bundledMockAgentPath, @@ -639,6 +641,9 @@ const makePromptInactivityHarness = Effect.fnUntraced(function* (input: { }), }, selfInvocation: yield* resolveSelfInvocation(), + ...(input.followAgentWorkState === true + ? { continuationRequests: { offer: () => Effect.void } } + : {}), }); const threadId = ThreadId.make("thread-acp-prompt-inactivity"); const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({ @@ -5191,6 +5196,41 @@ describe("AcpAdapterV2", () => { }).pipe(Effect.provide(layerTest)), ); + it.effect.each([false, true])( + "ends a running tool call when its agent process exits mid-prompt (followAgentWorkState: %s)", + (followAgentWorkState) => + Effect.gen(function* () { + const harness = yield* makePromptInactivityHarness({ + followAgentWorkState, + environment: (runtimeOrdinal) => + runtimeOrdinal === 1 ? { T3_ACP_EMIT_TRACKED_TOOL_CALL_KIND_THEN_HANG: "execute" } : {}, + }); + yield* harness.startTurn(1); + let exited = false; + const statuses = yield* harness.runtime.events.pipe( + Stream.takeUntil((event) => event.type === "turn.terminal"), + Stream.flatMap((event) => + event.type === "turn_item.updated" && + event.turnItem.nativeItemRef?.nativeId === "tracked-tool-call" + ? Stream.make(event.turnItem.status) + : Stream.empty, + ), + // The agent dies while its tool call is open. + Stream.tap(() => + exited + ? Effect.void + : Effect.sync(() => (exited = true)).pipe( + Effect.andThen(harness.natives[0]!.notify("_test/exit", {})), + ), + ), + Stream.runCollect, + Effect.map((collected) => Array.from(collected)), + ); + // regression: #37368 the dead agent's tool call stayed open in the failed turn. + assert.include(["failed", "interrupted", "cancelled"], statuses.at(-1)); + }).pipe(Effect.provide(layerTest)), + ); + it.live("does not poll the process ledger while a cgroup lease owns teardown", () => Effect.gen(function* () { if ((yield* HostProcess.Platform) !== "linux") return; diff --git a/packages/provider-acp/src/server/adapter.ts b/packages/provider-acp/src/server/adapter.ts index d642cfe200cbd..01bfafd68b11d 100644 --- a/packages/provider-acp/src/server/adapter.ts +++ b/packages/provider-acp/src/server/adapter.ts @@ -2550,12 +2550,16 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( yield* Ref.set(continuationRequested, false); yield* cancelPendingRuntimeRequests(); const context = yield* Ref.get(activeTurn); - if (context !== null) + if (context !== null) { + if (!context.finalized && !context.interrupted) { + yield* terminalizeOpenForegroundTools(context, "failed"); + } yield* finalizeTurn( context, "failed", makeProviderFailure({ class: "transport_error" }), ); + } }), ).pipe(Effect.asVoid); const makeRuntimeInput = ( @@ -8973,6 +8977,13 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( if (!interrupted && flavor.restartRuntimeAfterPromptError?.(failure) === true) { yield* Ref.set(runtimeRestartRequired, true); } + // An agent that answered with an error is still alive and may finish its + // tools; any other failure means its process or pipe is gone. + const agentAnswered = + Result.isSuccess(error) && error.success._tag === "AcpRequestError"; + if (!interrupted && !agentAnswered) { + yield* terminalizeOpenForegroundTools(context, "failed"); + } yield* finalizeTurn( context, interrupted ? "interrupted" : "failed",