From 41c802fa5802a1f4f0b297796327c958e1462f80 Mon Sep 17 00:00:00 2001 From: Carl Date: Sun, 22 Nov 2026 18:09:14 +0000 Subject: [PATCH 1/4] fix(acp): stopping a Devin turn kills the command it was running Devin answers session/cancel without sending terminal/kill or terminal/release, and T3 only killed client terminals on those requests or when the session closed. After Stop, a foreground command kept running and held a live terminal slot until the session was released. On Stop of an active turn, kill the session's running client terminals after session/cancel. The sweep takes the creation permit so an in-flight terminal/create registers first, and keeps the records so late terminal/output and wait_for_exit calls still answer. Fixes #40961 Co-Authored-By: Claude Opus 5.5 --- .../Adapters/AcpAdapterV2.test.ts | 116 ++++++++++++++++++ packages/provider-acp/src/server/adapter.ts | 8 ++ .../src/server/clientTerminals.ts | 27 ++++ 3 files changed, 151 insertions(+) diff --git a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts index 7815f8d959a2f..d1afb2ca0400f 100644 --- a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts @@ -6824,6 +6824,122 @@ describe("AcpAdapterV2", () => { }).pipe(Effect.provide(layerTest)), ); + // Devin answers session/cancel with "cancelled" and never sends terminal/kill or terminal/release. + it.live("Stop kills the command a Devin turn runs in a client terminal", () => + Effect.gen(function* () { + type Runtime = AcpSessionRuntime.AcpSessionRuntime["Service"]; + let handler: Parameters[0] | undefined; + let createTerminal: Parameters[0] | undefined; + let readTerminalOutput: Parameters[0] | undefined; + let terminalId: string | undefined; + const cancelled = yield* Deferred.make(); + const instanceId = ProviderInstanceId.make("devin-stop-terminal"); + const adapter = yield* makeAcpRegistryAdapterV2({ + instanceId, + selfInvocation: yield* resolveSelfInvocation(), + settings: DEVIN_SETTINGS, + environment: {}, + makeRuntime: makeMockRuntime({ + childProcessSpawner: yield* ChildProcessSpawner.ChildProcessSpawner, + mockAgentPath: yield* bundledMockAgentPath, + wrapRuntime: (runtime) => ({ + ...runtime, + handleSessionUpdate: (next) => + Effect.sync(() => { + handler = next; + }).pipe(Effect.andThen(runtime.handleSessionUpdate(next))), + handleCreateTerminal: (next) => + Effect.sync(() => { + createTerminal = next; + }).pipe(Effect.andThen(runtime.handleCreateTerminal(next))), + handleTerminalOutput: (next) => + Effect.sync(() => { + readTerminalOutput = next; + }).pipe(Effect.andThen(runtime.handleTerminalOutput(next))), + prompt: () => + Effect.gen(function* () { + ({ terminalId } = yield* createTerminal!( + { sessionId: "mock-session-1", command: "sleep 30" }, + { requestId: "exec-1-terminal", method: "terminal/create" }, + )); + yield* handler!({ + sessionId: "mock-session-1", + update: { + sessionUpdate: "tool_call", + toolCallId: "exec-1", + title: "exec", + kind: "execute", + status: "in_progress", + content: [{ type: "terminal", terminalId }], + }, + }); + yield* Deferred.await(cancelled); + return { stopReason: "cancelled" as const }; + }), + cancel: Deferred.succeed(cancelled, undefined).pipe(Effect.asVoid), + }), + }), + }).pipe( + Effect.provide( + Layer.mock(AcpRegistryCatalog)({ resolve: () => Effect.die("runtime is injected") }), + ), + ); + const threadId = ThreadId.make("devin-stop-terminal"); + const modelSelection = { instanceId, model: "default" } as const; + const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({ + runtimeMode: "full-access", + interactionMode: "default", + cwd: process.cwd(), + }); + const runtime = yield* adapter.openSession({ + threadId, + providerSessionId: ProviderSessionId.make("devin-stop-terminal-session"), + modelSelection, + runtimePolicy, + }); + const providerThread = yield* runtime.ensureThread({ + threadId, + modelSelection, + runtimePolicy, + }); + yield* runtime.startTurn( + makeTurnInput({ + threadId, + providerThread, + instanceId, + runtimePolicy, + now: yield* DateTime.now, + }), + ); + const running = Option.getOrThrow( + yield* runtime.events.pipe( + Stream.filter( + (event) => + event.type === "turn_item.updated" && + event.turnItem.nativeItemRef?.nativeId === "exec-1", + ), + Stream.runHead, + ), + ); + if (running.type !== "turn_item.updated" || running.turnItem.providerTurnId === null) { + return yield* Effect.die("Expected the running command item of the turn"); + } + + yield* runtime.interruptTurn({ + providerThread, + providerTurnId: running.turnItem.providerTurnId, + requestRuntimeRestart: true, + }); + const { exitStatus } = yield* readTerminalOutput!( + { sessionId: "mock-session-1", terminalId: terminalId! }, + { requestId: "exec-1-output", method: "terminal/output" }, + ); + // Killed: a signal on POSIX, a non-zero exit code from taskkill on Windows. + assert.notEqual(exitStatus, undefined); + assert.notEqual(exitStatus?.exitCode, 0); + }).pipe(Effect.provide(layerTest)), + ); + it.effect.each([ { label: "grants an ID-only execute approval its tracked command", diff --git a/packages/provider-acp/src/server/adapter.ts b/packages/provider-acp/src/server/adapter.ts index 908c99e4eaeb3..fdfe00e205a1a 100644 --- a/packages/provider-acp/src/server/adapter.ts +++ b/packages/provider-acp/src/server/adapter.ts @@ -10523,6 +10523,14 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( yield* Ref.set(runtimeRestartRequired, true); } } + // Devin answers session/cancel without ending commands in T3's terminals. + if ( + turnInput.requestRuntimeRestart === true && + clientTerminals !== undefined + ) { + const sessionId = yield* Ref.get(activeSessionId); + if (sessionId !== null) yield* clientTerminals.killSession(sessionId); + } const stopped = yield* Deferred.await(context.completed).pipe( Effect.timeoutOption(ACP_STOP_CONFIRM_TIMEOUT_MS), ); diff --git a/packages/provider-acp/src/server/clientTerminals.ts b/packages/provider-acp/src/server/clientTerminals.ts index 592bf47553494..c88397b361c64 100644 --- a/packages/provider-acp/src/server/clientTerminals.ts +++ b/packages/provider-acp/src/server/clientTerminals.ts @@ -121,6 +121,8 @@ export interface AcpClientTerminals { terminalId: string, ) => AcpTerminalOutputSnapshot | undefined; readonly readCommandLine: (terminalId: string) => string | undefined; + /** Kills a session's running commands and waits for their exit; the agent can still read them. */ + readonly killSession: (sessionId: string) => Effect.Effect; readonly disposeAll: Effect.Effect; } @@ -411,6 +413,30 @@ export const makeAcpClientTerminals = Effect.fn("makeAcpClientTerminals")(functi const readCommandLine: AcpClientTerminals["readCommandLine"] = (terminalId) => terminals.get(terminalId)?.commandLine; + // Taking the creation permit lets an in-flight terminal/create register before the sweep. + const killSession: AcpClientTerminals["killSession"] = (sessionId) => + creationPermit + .withPermit( + Effect.sync(() => + Array.from(terminals.values()).filter( + (terminal) => + terminal.sessionId === sessionId && + !terminal.released && + terminal.exitStatus === undefined, + ), + ), + ) + .pipe( + Effect.flatMap((live) => + Effect.forEach( + live, + (terminal) => terminal.kill.pipe(Effect.andThen(Deferred.await(terminal.exit))), + { concurrency: "unbounded", discard: true }, + ), + ), + Effect.uninterruptible, + ); + const disposeAll: AcpClientTerminals["disposeAll"] = Effect.uninterruptible( Effect.suspend(() => Effect.forEach( @@ -447,6 +473,7 @@ export const makeAcpClientTerminals = Effect.fn("makeAcpClientTerminals")(functi release: logged("terminal/release", release), readOutputSnapshot, readCommandLine, + killSession, disposeAll, }; }); From 2f7080a419a15c03c6915adb6c76661c0b84ed07 Mon Sep 17 00:00:00 2001 From: Carl Date: Sun, 22 Nov 2026 18:12:59 +0000 Subject: [PATCH 2/4] fix(acp): bound Stop's wait for a killed Devin command to exit If the OS refuses the kill (EPERM after a command elevates), the process never exits and Stop would wait forever. Keep the signal uninterruptible, wait at most two seconds for the exit to be recorded, and log a warning when it is not. Co-Authored-By: Claude Opus 5.5 --- .../src/server/clientTerminals.test.ts | 34 +++++++++++++++++++ .../src/server/clientTerminals.ts | 19 +++++++++-- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/packages/provider-acp/src/server/clientTerminals.test.ts b/packages/provider-acp/src/server/clientTerminals.test.ts index 87a96999cd1bf..92e129a702357 100644 --- a/packages/provider-acp/src/server/clientTerminals.test.ts +++ b/packages/provider-acp/src/server/clientTerminals.test.ts @@ -97,6 +97,40 @@ describe("AcpClientTerminals", () => { }), ); + it.effect("killSession returns when a killed command never exits", () => + Effect.gen(function* () { + // Models a kill the OS refused (EPERM after the command elevated): the process stays up. + const processExited = yield* Deferred.make(); + const child = ChildProcessSpawner.makeHandle({ + pid: ChildProcessSpawner.ProcessId(1), + exitCode: Deferred.await(processExited).pipe(Effect.as(ChildProcessSpawner.ExitCode(0))), + isRunning: Effect.succeed(true), + kill: () => Effect.void, + stdin: Sink.drain, + stdout: Stream.empty, + stderr: Stream.empty, + all: Stream.empty, + getInputFd: () => Sink.drain, + getOutputFd: () => Stream.empty, + unref: Effect.succeed(Effect.void), + }); + const terminals = yield* makeAcpClientTerminals({ defaultCwd: process.cwd() }).pipe( + Effect.provideService( + ChildProcessSpawner.ChildProcessSpawner, + ChildProcessSpawner.make(() => Effect.succeed(child)), + ), + ); + yield* Effect.addFinalizer(() => + Deferred.succeed(processExited, undefined).pipe(Effect.andThen(terminals.disposeAll)), + ); + yield* terminals.create({ sessionId: "session", command: "fixture" }); + + const kill = yield* terminals.killSession("session").pipe(Effect.forkChild); + yield* TestClock.adjust("2 seconds"); + yield* Fiber.join(kill); + }), + ); + it.live("resolves terminal environment from the requesting ACP session", () => Effect.gen(function* () { const inheritedEnvironment = yield* HostProcess.Environment; diff --git a/packages/provider-acp/src/server/clientTerminals.ts b/packages/provider-acp/src/server/clientTerminals.ts index c88397b361c64..4cfb0b731a728 100644 --- a/packages/provider-acp/src/server/clientTerminals.ts +++ b/packages/provider-acp/src/server/clientTerminals.ts @@ -4,6 +4,7 @@ import * as Duration from "effect/Duration"; import * as Effect from "effect/Effect"; import * as Exit from "effect/Exit"; import * as Fiber from "effect/Fiber"; +import * as Option from "effect/Option"; import * as Scope from "effect/Scope"; import * as Semaphore from "effect/Semaphore"; import * as Stream from "effect/Stream"; @@ -22,6 +23,8 @@ const MAX_RETAINED_OUTPUT_BYTES = 16 * 1024 * 1024; const DEFAULT_OUTPUT_BYTE_LIMIT = 4 * 1024 * 1024; const MAX_OUTPUT_BYTE_LIMIT = 8 * 1024 * 1024; const OUTPUT_DRAIN_GRACE = "1 second"; +// After a kill, the exit fiber may still drain output for up to OUTPUT_DRAIN_GRACE. +const KILL_EXIT_CONFIRM_GRACE = "2 seconds"; const terminalRequestError = (message: string, cause?: unknown) => EffectAcpErrors.AcpRequestError.internalError( @@ -414,6 +417,7 @@ export const makeAcpClientTerminals = Effect.fn("makeAcpClientTerminals")(functi terminals.get(terminalId)?.commandLine; // Taking the creation permit lets an in-flight terminal/create register before the sweep. + // A kill can fail (EPERM after a command elevates), so the exit wait is bounded. const killSession: AcpClientTerminals["killSession"] = (sessionId) => creationPermit .withPermit( @@ -430,11 +434,22 @@ export const makeAcpClientTerminals = Effect.fn("makeAcpClientTerminals")(functi Effect.flatMap((live) => Effect.forEach( live, - (terminal) => terminal.kill.pipe(Effect.andThen(Deferred.await(terminal.exit))), + (terminal) => + Effect.uninterruptible(terminal.kill).pipe( + Effect.andThen( + Deferred.await(terminal.exit).pipe(Effect.timeoutOption(KILL_EXIT_CONFIRM_GRACE)), + ), + Effect.flatMap((exit) => + Option.isNone(exit) + ? Effect.logWarning("ACP client terminal did not exit after kill", { + terminalId: terminal.terminalId, + }) + : Effect.void, + ), + ), { concurrency: "unbounded", discard: true }, ), ), - Effect.uninterruptible, ); const disposeAll: AcpClientTerminals["disposeAll"] = Effect.uninterruptible( From 81a04891af232f8fa46c6bdb374e2e9dd8fca5d1 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 23 Nov 2026 10:19:23 +0000 Subject: [PATCH 3/4] refactor(acp): describe Stop's terminal cleanup without naming an agent Co-Authored-By: Claude Opus 5.5 --- .../server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts | 4 ++-- packages/provider-acp/src/server/adapter.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts index 705a2ba5ebbf7..4bb8392c128b2 100644 --- a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts @@ -6872,8 +6872,8 @@ describe("AcpAdapterV2", () => { }).pipe(Effect.provide(layerTest)), ); - // Devin answers session/cancel with "cancelled" and never sends terminal/kill or terminal/release. - it.live("Stop kills the command a Devin turn runs in a client terminal", () => + // The agent answers session/cancel with "cancelled" and never sends terminal/kill or terminal/release. + it.live("Stop kills the command a turn runs in a client terminal", () => Effect.gen(function* () { type Runtime = AcpSessionRuntime.AcpSessionRuntime["Service"]; let handler: Parameters[0] | undefined; diff --git a/packages/provider-acp/src/server/adapter.ts b/packages/provider-acp/src/server/adapter.ts index 76fd0bdf460d5..892d7e6cb6119 100644 --- a/packages/provider-acp/src/server/adapter.ts +++ b/packages/provider-acp/src/server/adapter.ts @@ -10775,7 +10775,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( yield* Ref.set(runtimeRestartRequired, true); } } - // Devin answers session/cancel without ending commands in T3's terminals. + // T3 owns client terminals; an agent may answer session/cancel without killing them. if ( turnInput.requestRuntimeRestart === true && clientTerminals !== undefined From 2ca56734a02c3b5e36b423741e4092effe41a04c Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 23 Nov 2026 10:27:38 +0000 Subject: [PATCH 4/4] fix(acp): refuse terminal/create after Stop swept the session's commands A create that arrived after the Stop sweep, or waited behind it on the creation permit, registered a command nothing would kill. The guard now checks the stopped-run quarantine and runs under the creation permit. Co-Authored-By: Claude Opus 5.5 --- .../orchestration-v2/Adapters/AcpAdapterV2.test.ts | 5 +++++ packages/provider-acp/src/server/adapter.ts | 12 ++++++++++-- packages/provider-acp/src/server/clientTerminals.ts | 13 ++++++++----- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts index 4bb8392c128b2..c7489bf0d1034 100644 --- a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts @@ -6985,6 +6985,11 @@ describe("AcpAdapterV2", () => { // Killed: a signal on POSIX, a non-zero exit code from taskkill on Windows. assert.notEqual(exitStatus, undefined); assert.notEqual(exitStatus?.exitCode, 0); + const lateCreate = yield* createTerminal!( + { sessionId: "mock-session-1", command: "sleep 30" }, + { requestId: "exec-2-terminal", method: "terminal/create" }, + ).pipe(Effect.exit); + assert.isTrue(Exit.isFailure(lateCreate)); }).pipe(Effect.provide(layerTest)), ); diff --git a/packages/provider-acp/src/server/adapter.ts b/packages/provider-acp/src/server/adapter.ts index 892d7e6cb6119..481d6b3e27ab4 100644 --- a/packages/provider-acp/src/server/adapter.ts +++ b/packages/provider-acp/src/server/adapter.ts @@ -7224,7 +7224,15 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( ), ); - const guardClientTerminalCreate = clientPolicyContext.pipe( + const guardClientTerminalCreate = Effect.gen(function* () { + // Stop already swept this session's terminals; a command started now would outlive it. + if (yield* Ref.get(stoppedRunQuarantine)) { + return yield* EffectAcpErrors.AcpRequestError.internalError( + "The T3 turn was stopped; terminal/create is refused until the next turn.", + ); + } + }).pipe( + Effect.andThen(clientPolicyContext), Effect.flatMap(({ policy, turnKey }) => { const disposition = acpClientExecuteDisposition(policy); if ( @@ -7242,7 +7250,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( ) { if (clientTerminals === undefined) return; yield* targetRuntime.handleCreateTerminal((request) => - guardClientTerminalCreate.pipe(Effect.andThen(clientTerminals.create(request))), + clientTerminals.create(request, guardClientTerminalCreate), ); yield* targetRuntime.handleTerminalOutput(clientTerminals.output); yield* targetRuntime.handleTerminalWaitForExit(clientTerminals.waitForExit); diff --git a/packages/provider-acp/src/server/clientTerminals.ts b/packages/provider-acp/src/server/clientTerminals.ts index 4cfb0b731a728..591f994ddbbc2 100644 --- a/packages/provider-acp/src/server/clientTerminals.ts +++ b/packages/provider-acp/src/server/clientTerminals.ts @@ -103,8 +103,10 @@ interface ManagedAcpTerminal { } export interface AcpClientTerminals { + /** `admit` runs under the creation permit, so a check it makes holds against `killSession`. */ readonly create: ( request: EffectAcpSchema.CreateTerminalRequest, + admit?: Effect.Effect, ) => Effect.Effect; readonly output: ( request: EffectAcpSchema.TerminalOutputRequest, @@ -231,10 +233,11 @@ export const makeAcpClientTerminals = Effect.fn("makeAcpClientTerminals")(functi return Effect.succeed(terminal); }; - const create: AcpClientTerminals["create"] = (request) => + const create: AcpClientTerminals["create"] = (request, admit = Effect.void) => creationPermit.withPermit( Effect.uninterruptibleMask((restore) => Effect.gen(function* () { + yield* admit; const liveCount = Array.from(terminals.values()).filter( (terminal) => !terminal.released && terminal.exitStatus === undefined, ).length; @@ -466,12 +469,12 @@ export const makeAcpClientTerminals = Effect.fn("makeAcpClientTerminals")(functi ); const logged = - ( + , Response>( operation: string, - handler: (request: Request) => Effect.Effect, + handler: (...args: Args) => Effect.Effect, ) => - (request: Request) => - handler(request).pipe( + (...args: Args) => + handler(...args).pipe( Effect.tapError((error) => Effect.logWarning("ACP client terminal operation failed", { operation,