From 43c5b1972af323894dfe11001586f89960dc978c Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 19 Nov 2026 23:02:32 +0000 Subject: [PATCH 1/3] test(server): ACP tool call open when its session closes stays running Reproduces #39410: closing an ACP session mid-prompt, as t3_worktree_handoff does, leaves the open tool call running. Co-Authored-By: Claude Opus 5.5 --- .../Adapters/AcpAdapterV2.test.ts | 42 ++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts index c3d54f06719fa..96eaddf0f093c 100644 --- a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts @@ -622,6 +622,7 @@ const makePromptInactivityHarness = Effect.fnUntraced(function* (input: { ) => AcpSessionRuntime.AcpSessionRuntime["Service"]["prompt"]; readonly cancelled?: Deferred.Deferred; readonly followAgentWorkState?: boolean; + readonly sessionScope?: Scope.Scope; }) { const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner; const natives: Array = []; @@ -662,12 +663,15 @@ const makePromptInactivityHarness = Effect.fnUntraced(function* (input: { cwd: process.cwd(), }); const modelSelection = { instanceId, model: "default" } as const; - const runtime = yield* adapter.openSession({ + const opened = adapter.openSession({ threadId, providerSessionId: ProviderSessionId.make("provider-session-acp-prompt-inactivity"), modelSelection, runtimePolicy, }); + const runtime = yield* input.sessionScope === undefined + ? opened + : opened.pipe(Effect.provideService(Scope.Scope, input.sessionScope)); const providerThread = yield* runtime.ensureThread({ threadId, modelSelection, runtimePolicy }); const startTurn = (ordinal: number) => DateTime.now.pipe( @@ -5780,6 +5784,42 @@ describe("AcpAdapterV2", () => { }).pipe(Effect.provide(layerTest)), ); + it.effect("ends a running T3 MCP tool call when its session closes mid-prompt", () => + Effect.gen(function* () { + const sessionScope = yield* Scope.make(); + const harness = yield* makePromptInactivityHarness({ + sessionScope, + environment: () => ({ T3_ACP_EMIT_TRACKED_TOOL_CALL_KIND_THEN_HANG: "other" }), + }); + const trackedToolStatus = (event: ProviderAdapter.ProviderAdapterV2Event) => + event.type === "turn_item.updated" && + event.turnItem.nativeItemRef?.nativeId === "tracked-tool-call" + ? event.turnItem.status + : undefined; + yield* harness.startTurn(1); + yield* harness.runtime.events.pipe( + Stream.filter((event) => trackedToolStatus(event) !== undefined), + Stream.runHead, + ); + // t3_worktree_handoff releases the live session while its own tool call is open. + yield* Scope.close(sessionScope, Exit.void); + // The event queue outlives the session; only events emitted by the close remain. + const after = yield* harness.runtime.events.pipe( + Stream.filter((event) => trackedToolStatus(event) !== undefined), + Stream.map(trackedToolStatus), + Stream.takeUntil((status) => status !== "pending" && status !== "running"), + Stream.runCollect, + Effect.map((collected) => Array.from(collected)), + Effect.timeoutOption("1 second"), + Effect.forkChild, + ); + yield* TestClock.adjust("1 second"); + const statuses = Option.getOrElse(yield* Fiber.join(after), () => []); + // regression: #39410 the handoff's tool call stayed running after the session closed. + assert.include(["completed", "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; From b7b9e83c0206d20b83dbf10c1c314c642fc13ba0 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 19 Nov 2026 23:11:05 +0000 Subject: [PATCH 2/3] fix(server): worktree handoff no longer leaves Antigravity threads Waiting t3_worktree_handoff releases the ACP session while its own tool call is open. The release drops event subscribers before closing the session, and the ACP runtime did not declare that its tool calls end with its process, so the manager's release scan skipped it and the handoff's tool row stayed running. ACP sessions own their agent process, so declare backgroundTasksEndWithProcess (as Pi does), and end the active turn as interrupted when the session scope closes mid-prompt. Fixes pingdotgg/t3code#39410 Co-Authored-By: Claude Opus 5.5 --- .../src/orchestration-v2/Adapters/AcpAdapterV2.test.ts | 2 ++ packages/provider-acp/src/server/adapter.ts | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts index 96eaddf0f093c..8c356af42446c 100644 --- a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts @@ -5817,6 +5817,8 @@ describe("AcpAdapterV2", () => { const statuses = Option.getOrElse(yield* Fiber.join(after), () => []); // regression: #39410 the handoff's tool call stayed running after the session closed. assert.include(["completed", "failed", "interrupted", "cancelled"], statuses.at(-1)); + // Release subscribers are gone before this close, so the manager ends the persisted item. + assert.isTrue(harness.runtime.backgroundTasksEndWithProcess); }).pipe(Effect.provide(layerTest)), ); diff --git a/packages/provider-acp/src/server/adapter.ts b/packages/provider-acp/src/server/adapter.ts index 3e0864054c32f..effca9c8bbd34 100644 --- a/packages/provider-acp/src/server/adapter.ts +++ b/packages/provider-acp/src/server/adapter.ts @@ -9637,6 +9637,12 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( yield* Ref.update(continuationGeneration, (value) => value + 1); }), ); + // A session released mid-prompt (worktree handoff) never sees its prompt settle. + const openTurn = yield* Ref.get(activeTurn); + if (openTurn !== null && !openTurn.finalized) { + openTurn.interrupted = true; + yield* finalizeTurn(openTurn, "interrupted"); + } const requests = [...(yield* Ref.get(pendingRuntimeRequests)).values()]; yield* Effect.forEach( requests, @@ -9699,6 +9705,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( }, canResumeAfterRestart: canLoadSession || canResumeSession, events: Stream.fromEffectRepeat(Queue.take(events)), + backgroundTasksEndWithProcess: true, ...(flavor.getModelContextWindow === undefined ? {} : { From ec87ac056c0aac3ed64b102912d7feec71559ee7 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 19 Nov 2026 23:27:41 +0000 Subject: [PATCH 3/3] fix(server): end the handoff's tool calls when its session closes ACP background work outlives an idle session release (Stop must still reach it), so backgroundTasksEndWithProcess was a false claim for ACP and broke stop_background_work_after_release. Drop it. Instead, when a workspace handoff closes the exclusive session and RunExecutionService settles the run itself, end the run's open tool calls and subagents there: that process is gone, and the handoff's own tool call is among them. Co-Authored-By: Claude Opus 5.5 --- .../Adapters/AcpAdapterV2.test.ts | 2 - .../orchestration-v2/RunExecutionService.ts | 6 ++- .../SelectionRestart.integration.test.ts | 51 +++++++++++++++++-- packages/provider-acp/src/server/adapter.ts | 1 - 4 files changed, 52 insertions(+), 8 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts index 8c356af42446c..96eaddf0f093c 100644 --- a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts @@ -5817,8 +5817,6 @@ describe("AcpAdapterV2", () => { const statuses = Option.getOrElse(yield* Fiber.join(after), () => []); // regression: #39410 the handoff's tool call stayed running after the session closed. assert.include(["completed", "failed", "interrupted", "cancelled"], statuses.at(-1)); - // Release subscribers are gone before this close, so the manager ends the persisted item. - assert.isTrue(harness.runtime.backgroundTasksEndWithProcess); }).pipe(Effect.provide(layerTest)), ); diff --git a/apps/server/src/orchestration-v2/RunExecutionService.ts b/apps/server/src/orchestration-v2/RunExecutionService.ts index f1d21f4a6bad5..da162282719aa 100644 --- a/apps/server/src/orchestration-v2/RunExecutionService.ts +++ b/apps/server/src/orchestration-v2/RunExecutionService.ts @@ -2055,7 +2055,11 @@ export const layer: Layer.Layer< ).pipe(Effect.as(false)), ), ); - if (handedOff) return; + if (handedOff) { + // Its tool calls, the handoff's own among them, died with that process. + yield* endWorkLostAfterRootRun(completedAt); + return; + } } yield* Effect.logWarning("orchestration V2 provider event ingestion failed", { runId: input.run.id, diff --git a/apps/server/src/orchestration-v2/SelectionRestart.integration.test.ts b/apps/server/src/orchestration-v2/SelectionRestart.integration.test.ts index 683fb14d44cc3..890aa9d97467e 100644 --- a/apps/server/src/orchestration-v2/SelectionRestart.integration.test.ts +++ b/apps/server/src/orchestration-v2/SelectionRestart.integration.test.ts @@ -16,6 +16,7 @@ import { ProviderTurnId, type RunId, ThreadId, + TurnItemId, } from "@t3tools/contracts"; import * as DateTime from "effect/DateTime"; import * as Effect from "effect/Effect"; @@ -462,19 +463,54 @@ it.live.each(["pooled", "exclusive"] as const)( state, mode === "pooled" ? pooledCapabilities : exclusiveCapabilities, ); + const handoffToolId = TurnItemId.make(`turn-item:workspace-handoff:${mode}:tool`); // A session that closes on detach is never asked to interrupt: Claude's cannot answer. + // Its turn is inside the handoff's own tool call, which the closed session never ends. const adapter: ProviderAdapter.ProviderAdapterV2["Service"] = mode === "pooled" ? base : { ...base, openSession: (input) => - base.openSession(input).pipe( - Effect.map((session) => ({ + Effect.gen(function* () { + const session = yield* base.openSession(input); + const toolEvents = + yield* Queue.unbounded(); + return { ...session, + events: Stream.merge(session.events, Stream.fromQueue(toolEvents)), + startTurn: (turn) => + Effect.gen(function* () { + yield* session.startTurn(turn); + const now = yield* DateTime.now; + yield* Queue.offer(toolEvents, { + type: "turn_item.updated", + driver, + turnItem: { + id: handoffToolId, + type: "dynamic_tool", + threadId: turn.threadId, + runId: turn.runId, + nodeId: turn.rootNodeId, + providerThreadId: turn.providerThread.id, + providerTurnId: ProviderTurnId.make(`provider-turn:${turn.attemptId}`), + nativeItemRef: null, + parentItemId: null, + ordinal: 1, + status: "running", + title: "t3_worktree_handoff", + toolName: "t3-code.t3_worktree_handoff", + input: {}, + output: null, + startedAt: now, + completedAt: null, + updatedAt: now, + }, + }); + }), interruptTurn: () => Effect.die("an exclusive handoff must not interrupt"), - })), - ), + }; + }), }; const layerRegistry = ProviderAdapterRegistry.layerSingle(adapter); @@ -585,6 +621,13 @@ it.live.each(["pooled", "exclusive"] as const)( ), "an intentional workspace handoff must not project an interrupt result or a failure", ); + if (mode === "exclusive") { + // regression: #39410 the handoff's tool call stayed running after its session closed. + assert.equal( + projection.turnItems.find((item) => item.id === handoffToolId)?.status, + "interrupted", + ); + } assert.deepEqual( (yield* Ref.get(state)).started.map((turn) => turn.cwd), [cwd, handoffCwd], diff --git a/packages/provider-acp/src/server/adapter.ts b/packages/provider-acp/src/server/adapter.ts index effca9c8bbd34..247fc8fe20fa6 100644 --- a/packages/provider-acp/src/server/adapter.ts +++ b/packages/provider-acp/src/server/adapter.ts @@ -9705,7 +9705,6 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( }, canResumeAfterRestart: canLoadSession || canResumeSession, events: Stream.fromEffectRepeat(Queue.take(events)), - backgroundTasksEndWithProcess: true, ...(flavor.getModelContextWindow === undefined ? {} : {