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; 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 3e0864054c32f..247fc8fe20fa6 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,