Skip to content
Merged
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
141 changes: 111 additions & 30 deletions packages/core/test/tool-shell.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import { PluginSupervisor } from "@opencode/core/plugin/supervisor"
import { Shell } from "@opencode/core/shell"
import { ShellSelect } from "@opencode/core/shell/select"
import { ShellResult } from "@opencode/core/shell/result"
import { ID } from "@opencode/schema/shell"
import { Event, ID } from "@opencode/schema/shell"
import { ShellTool } from "@opencode/core/tool/plugin/shell"
import { ToolOutput } from "@opencode/core/tool-output"
import { Tool } from "@opencode/core/tool"
Expand Down Expand Up @@ -333,38 +333,117 @@ const withScanner = <A, E, R>(
}),
)

// The timeout catches a hung permission flow, not slowness: PowerShell commands on loaded Windows runners have exceeded 5s.
// The timeout catches a hung permission flow or shell command, not slowness: PowerShell commands on loaded Windows
// runners have exceeded 5s.
const permissionCommandTimeout = Duration.seconds(15)

const runPermissionCommand = (
registry: Tool.Interface,
command: string,
marker: string,
replies: ReadonlyArray<Permission.Reply>,
) =>
Effect.gen(function* () {
const permission = yield* Permission.Service
const bus = yield* Bus.Service
const queue = yield* Queue.unbounded<Permission.Request>()
yield* bus.subscribe(Permission.Event.Asked).pipe(
Stream.runForEach((event) => Queue.offer(queue, event.data)),
Effect.forkScoped({ startImmediately: true }),
)
const execution = yield* executeTool(registry, call({ command }, `call-${Permission.ID.create()}`)).pipe(
Effect.forkScoped,
)
const requests = yield* Effect.forEach(replies, (reply) =>
Effect.gen(function* () {
const request = yield* Queue.take(queue)
expect(yield* permission.forSession(sessionID)).toEqual([request])
expect(yield* Effect.promise(() => Bun.file(marker).exists())).toBe(false)
yield* permission.reply({ requestID: request.id, reply })
return request
Effect.suspend(() => {
const started = Date.now()
const shells: Array<{ id: string; event: string; exit?: number; ms: number }> = []
return Effect.gen(function* () {
const permission = yield* Permission.Service
const bus = yield* Bus.Service
const queue = yield* Queue.unbounded<Permission.Request>()
yield* bus.subscribe(Permission.Event.Asked).pipe(
Stream.runForEach((event) => Queue.offer(queue, event.data)),
Effect.forkScoped({ startImmediately: true }),
)
yield* Stream.merge(
bus
.subscribe(Event.Created)
.pipe(Stream.map((event) => ({ id: event.data.info.id, event: "created", ms: Date.now() - started }))),
bus.subscribe(Event.Exited).pipe(
Stream.map((event) => ({
id: event.data.id,
event: event.data.status,
exit: event.data.exit,
ms: Date.now() - started,
})),
),
).pipe(
Stream.runForEach((event) => Effect.sync(() => shells.push(event))),
Effect.forkScoped({ startImmediately: true }),
)
const execution = yield* executeTool(registry, call({ command }, `call-${Permission.ID.create()}`)).pipe(
Effect.forkScoped,
)
const requests = yield* Effect.forEach(replies, (reply) =>
Effect.gen(function* () {
const request = yield* Queue.take(queue)
expect(yield* permission.forSession(sessionID)).toEqual([request])
expect(yield* Effect.promise(() => Bun.file(marker).exists())).toBe(false)
yield* permission.reply({ requestID: request.id, reply })
return request
}),
)
const exit = yield* Fiber.await(execution)
expect(yield* permission.list()).toEqual([])
expect(yield* Queue.size(queue)).toBe(0)
return { exit, requests }
}).pipe(
// Inside the scope, so the report sees the call while it is still stuck rather than after it was interrupted.
Effect.timeoutOrElse({
duration: permissionCommandTimeout,
orElse: () => stalledPermissionCommand(command, shells),
}),
Effect.scoped,
)
const exit = yield* Fiber.await(execution)
expect(yield* permission.list()).toEqual([])
expect(yield* Queue.size(queue)).toBe(0)
return { exit, requests }
}).pipe(Effect.scoped, Effect.timeout(Duration.seconds(15)))
})

// No shell events means the call stalled before spawning; an exited command with a running job stalled in the job.
const stalledPermissionCommand = Effect.fnUntraced(function* (
command: string,
shells: ReadonlyArray<{ id: string; event: string; exit?: number; ms: number }>,
) {
const permission = yield* Permission.Service
const shell = yield* Shell.Service
const jobs = yield* Job.Service
const pending = (yield* permission.list()).map((request) => request.resources)
const commands = yield* Effect.forEach(new Set(shells.map((event) => event.id)), (id) =>
Effect.gen(function* () {
const info = yield* shell.get(ID.make(id)).pipe(Effect.orElseSucceed(() => undefined))
const pid = info?.pid
return {
id,
status: info?.status,
job: (yield* jobs.get(id))?.status,
pid,
process: pid === undefined ? undefined : yield* processState(pid),
tail: info
? yield* Effect.tryPromise(() => {
// A negative slice reads nothing here: the size is unknown until it is asked for.
const file = Bun.file(info.file)
return file.slice(Math.max(0, file.size - 500)).text()
}).pipe(Effect.orElseSucceed(() => undefined))
: undefined,
}
}),
)
const report = `${JSON.stringify(command)} did not settle within ${Duration.format(permissionCommandTimeout)}: ${JSON.stringify({ pending, shells, commands })}`
// Logged as well, in case interrupting the stuck call blocks the scope from closing.
yield* Effect.logError(report)
return yield* Effect.fail(new Error(report))
})

// `ps` shows a zombie (Z) whose exit was never reaped and a process stuck on I/O (D); `kill(pid, 0)` succeeds for both.
const processState = (pid: number) =>
isWindows
? Effect.try(() => process.kill(pid, 0)).pipe(
Effect.as("running"),
Effect.orElseSucceed(() => "gone"),
)
: Effect.sync(
() =>
Bun.spawnSync(["ps", "-o", "stat=,wchan=", "-p", String(pid)])
.stdout.toString()
.trim() || "gone",
)

describe("ShellTool scanner permissions", () => {
const test = isWindows || !Bun.which("sh") ? permissionIt.live.skip : permissionIt.live
Expand Down Expand Up @@ -2234,13 +2313,14 @@ describe("ShellTool", () => {

describe("ShellTool stopped", () => {
// Runs a foreground call the way a session step does, then stops it as the user does (through `cancel`), or
// interrupts it as a server shutdown does.
// interrupts it as a server shutdown does. It stops once the command has written `outputBytes`: bash's stdout is
// line-buffered, so a multi-line printf arrives in several writes, and stopping after the first loses the rest.
const stopCall = (
registry: Tool.Interface,
command: string,
id: string,
how: "cancel" | "interrupt",
waitForOutput = true,
outputBytes = 1,
) =>
Effect.gen(function* () {
const shell = yield* Shell.Service
Expand All @@ -2255,7 +2335,7 @@ describe("ShellTool stopped", () => {
cancel: Deferred.await(cancel),
}).pipe(Effect.forkChild({ startImmediately: true }))
const shellID = ID.make(yield* Deferred.await(started))
while (waitForOutput && (yield* shell.output(shellID, { cursor: Number.MAX_SAFE_INTEGER })).size === 0)
while ((yield* shell.output(shellID, { cursor: Number.MAX_SAFE_INTEGER })).size < outputBytes)
yield* Effect.sleep(Duration.millis(10))
if (how === "cancel") yield* Deferred.succeed(cancel, undefined)
if (how === "interrupt") yield* Fiber.interrupt(fiber)
Expand All @@ -2279,6 +2359,7 @@ describe("ShellTool stopped", () => {
: "printf 'first\\nsecond\\n'; sleep 60",
"call-stopped",
"cancel",
"first\nsecond\n".length,
)
const info = yield* shell.get(shellID)
expect(info.status).toBe("killed")
Expand Down Expand Up @@ -2327,7 +2408,7 @@ describe("ShellTool stopped", () => {
return withSession(tmp.path, (registry) =>
Effect.gen(function* () {
const shell = yield* Shell.Service
const { shellID, result } = yield* stopCall(registry, idleCommand, "call-silent", "cancel", false)
const { shellID, result } = yield* stopCall(registry, idleCommand, "call-silent", "cancel", 0)
const info = yield* shell.get(shellID)
expect(result?.error?.message).toBe(
`[no output; output file: ${info.file}]\n\nCommand interrupted by the user (shell ID: ${shellID}).`,
Expand Down Expand Up @@ -2504,7 +2585,7 @@ describe("ShellTool stopped", () => {
reset()
return withSession(tmp.path, (registry) =>
Effect.gen(function* () {
const { shellID } = yield* stopCall(registry, idleCommand, "call-recorded", "interrupt", false)
const { shellID } = yield* stopCall(registry, idleCommand, "call-recorded", "interrupt", 0)
const request = fake?.requests.find((item) => item.op === "create_pipe")
expect(request?.metadata).toMatchObject({
id: shellID,
Expand Down
Loading