Repository navigation
fix(acp): stopping a Devin turn kills the command it was running #40968
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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( | ||
|
|
@@ -121,6 +124,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<void>; | ||
| readonly disposeAll: Effect.Effect<void>; | ||
| } | ||
|
|
||
|
|
@@ -411,6 +416,42 @@ 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. | ||
| // A kill can fail (EPERM after a command elevates), so the exit wait is bounded. | ||
| 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) => | ||
| Effect.uninterruptible(terminal.kill).pipe( | ||
| Effect.andThen( | ||
| Deferred.await(terminal.exit).pipe(Effect.timeoutOption(KILL_EXIT_CONFIRM_GRACE)), | ||
|
Comment on lines
+438
to
+440
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bound the uninterruptible terminal kill so Stop cannot wait indefinitely.
|
||
| ), | ||
| 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 }, | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| const disposeAll: AcpClientTerminals["disposeAll"] = Effect.uninterruptible( | ||
| Effect.suspend(() => | ||
| Effect.forEach( | ||
|
|
@@ -447,6 +488,7 @@ export const makeAcpClientTerminals = Effect.fn("makeAcpClientTerminals")(functi | |
| release: logged("terminal/release", release), | ||
| readOutputSnapshot, | ||
| readCommandLine, | ||
| killSession, | ||
| disposeAll, | ||
| }; | ||
| }); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stopped turns can leave commands created after the Stop sweep running.
killSessionsnapshots the live terminals undercreationPermitand kills only that snapshot. Aterminal/createthat registers after the snapshot is not killed. This happens in two ways:guardClientTerminalCreate. The guard checks only the execution policy and grants, notstoppedRunQuarantine, which Stop sets. The create then registers a live terminal.creationPermitbehindkillSessionregisters after the snapshot.Either command keeps running until its terminal is released or the provider session closes. The commands also keep consuming live terminal slots.
Deny
terminal/createwhilestoppedRunQuarantineis set. Check it again aftercreationPermitis acquired, so creates that were waiting behind the sweep are also denied.