Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/opencode/src/tool/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { BashArity } from "@/permission/arity"
export { Parameters } from "./shell/prompt"

const MAX_METADATA_LENGTH = 30_000
const FORCE_KILL_AFTER = "5 seconds"
const CWD = new Set(["cd", "chdir", "popd", "pushd", "push-location", "set-location"])
const FILES = new Set([
...CWD,
Expand Down Expand Up @@ -297,6 +298,7 @@ function cmd(shell: string, command: string, cwd: string, env: NodeJS.ProcessEnv
env,
stdin: "ignore",
detached: false,
forceKillAfter: FORCE_KILL_AFTER,
Comment thread
goleary marked this conversation as resolved.
Resolved
})
}

Expand All @@ -306,6 +308,8 @@ function cmd(shell: string, command: string, cwd: string, env: NodeJS.ProcessEnv
env,
stdin: "ignore",
detached: process.platform !== "win32",
// Scope interruption also terminates the process, outside the timeout/abort handlers.
forceKillAfter: FORCE_KILL_AFTER,
})
Comment thread
goleary marked this conversation as resolved.
Resolved
}
const parser = lazy(async () => {
Expand Down Expand Up @@ -547,11 +551,11 @@ export const ShellTool = Tool.define(

if (exit.kind === "abort") {
aborted = true
yield* handle.kill({ forceKillAfter: "3 seconds" }).pipe(Effect.orDie)
yield* handle.kill({ forceKillAfter: FORCE_KILL_AFTER }).pipe(Effect.orDie)
}
if (exit.kind === "timeout") {
expired = true
yield* handle.kill({ forceKillAfter: "3 seconds" }).pipe(Effect.orDie)
yield* handle.kill({ forceKillAfter: FORCE_KILL_AFTER }).pipe(Effect.orDie)
}

return exit.kind === "exit" ? exit.code : null
Expand Down
31 changes: 30 additions & 1 deletion packages/opencode/test/tool/shell.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PermissionV1 } from "@opencode-ai/core/v1/permission"
import { describe, expect } from "bun:test"
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
import { Cause, Effect, Exit, Layer } from "effect"
import { Cause, Deferred, Effect, Exit, Fiber, Layer } from "effect"
import type * as Scope from "effect/Scope"
import os from "os"
import path from "path"
Expand Down Expand Up @@ -1007,6 +1007,35 @@ describe("tool.shell permissions", () => {
})

describe("tool.shell abort", () => {
it.live(
"bounds cleanup when an interrupted command ignores SIGTERM",
() =>
runIn(
projectRoot,
Effect.gen(function* () {
if (process.platform === "win32") return
const ready = yield* Deferred.make<void>()
const fiber = yield* run(
{
command: `${bin} -e 'process.on("SIGTERM", () => {}); console.log("ready"); setTimeout(() => {}, 20000)'`,
},
{
...ctx,
metadata: (input) =>
String(input.metadata?.output).includes("ready")
? Deferred.succeed(ready, undefined).pipe(Effect.asVoid)
: Effect.void,
},
).pipe(Effect.forkScoped)
yield* Deferred.await(ready)
yield* Fiber.interrupt(fiber)
const exit = yield* Fiber.await(fiber)
Comment thread
goleary marked this conversation as resolved.
Resolved
expect(Exit.isFailure(exit)).toBe(true)
Comment thread
goleary marked this conversation as resolved.
Resolved
}),
),
15_000,
)

it.live(
"preserves output when aborted",
() =>
Expand Down
Loading