From 1c228df6c6c8d57975b1ffc37bd6817008d6c601 Mon Sep 17 00:00:00 2001 From: shuv1337 Date: Fri, 13 Nov 2026 13:16:17 -0800 Subject: [PATCH 1/4] test(core): report what a stalled shell permission test was waiting on Co-authored-by: Cursor --- packages/core/test/tool-shell.test.ts | 34 ++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/packages/core/test/tool-shell.test.ts b/packages/core/test/tool-shell.test.ts index e908ea492b2e..7888a7134896 100644 --- a/packages/core/test/tool-shell.test.ts +++ b/packages/core/test/tool-shell.test.ts @@ -364,7 +364,39 @@ const runPermissionCommand = ( expect(yield* permission.list()).toEqual([]) expect(yield* Queue.size(queue)).toBe(0) return { exit, requests } - }).pipe(Effect.scoped, Effect.timeout(Duration.seconds(15))) + }).pipe( + // Inside the scope, so the report sees the call while it is still stuck rather than after it was interrupted. + Effect.timeoutOrElse({ duration: Duration.seconds(15), orElse: () => stalledPermissionCommand(command) }), + Effect.scoped, + ) + +const stalledPermissionCommand = Effect.fnUntraced(function* (command: string) { + const permission = yield* Permission.Service + const shell = yield* Shell.Service + const pending = (yield* permission.list()).map((request) => request.resources) + const running = yield* Effect.forEach(yield* shell.list(), (info) => + Effect.gen(function* () { + const pid = info.pid + return { + shell: info.shell, + pid, + // A running command whose process is gone never had its exit observed. + alive: + pid === undefined + ? undefined + : yield* Effect.try(() => process.kill(pid, 0)).pipe(Effect.orElseSucceed(() => false)), + elapsedMs: Date.now() - info.time.started, + output: yield* shell.output(info.id).pipe( + Effect.map((page) => page.output.slice(-500)), + Effect.orElseSucceed(() => undefined), + ), + } + }), + ) + return yield* Effect.fail( + new Error(`${JSON.stringify(command)} did not settle within 15s: ${JSON.stringify({ pending, running })}`), + ) +}) describe("ShellTool scanner permissions", () => { const test = isWindows || !Bun.which("sh") ? permissionIt.live.skip : permissionIt.live From 2e4e1bb393b9da5b22a3a5fab6bc145d6f759040 Mon Sep 17 00:00:00 2001 From: shuv1337 Date: Fri, 13 Nov 2026 13:25:26 -0800 Subject: [PATCH 2/4] test(core): report shell events, job state and process state for a stalled permission test Co-authored-by: Cursor --- packages/core/test/tool-shell.test.ts | 137 +++++++++++++++++--------- 1 file changed, 91 insertions(+), 46 deletions(-) diff --git a/packages/core/test/tool-shell.test.ts b/packages/core/test/tool-shell.test.ts index 7888a7134896..4fbf7834a37e 100644 --- a/packages/core/test/tool-shell.test.ts +++ b/packages/core/test/tool-shell.test.ts @@ -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" @@ -333,71 +333,116 @@ const withScanner = ( }), ) -// 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, ) => - Effect.gen(function* () { - const permission = yield* Permission.Service - const bus = yield* Bus.Service - const queue = yield* Queue.unbounded() - 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() + 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( - // Inside the scope, so the report sees the call while it is still stuck rather than after it was interrupted. - Effect.timeoutOrElse({ duration: Duration.seconds(15), orElse: () => stalledPermissionCommand(command) }), - Effect.scoped, - ) + }) -const stalledPermissionCommand = Effect.fnUntraced(function* (command: string) { +// 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 running = yield* Effect.forEach(yield* shell.list(), (info) => + const commands = yield* Effect.forEach(new Set(shells.map((event) => event.id)), (id) => Effect.gen(function* () { - const pid = info.pid + const info = yield* shell.get(ID.make(id)).pipe(Effect.orElseSucceed(() => undefined)) + const pid = info?.pid return { - shell: info.shell, + id, + status: info?.status, + job: (yield* jobs.get(id))?.status, pid, - // A running command whose process is gone never had its exit observed. - alive: - pid === undefined - ? undefined - : yield* Effect.try(() => process.kill(pid, 0)).pipe(Effect.orElseSucceed(() => false)), - elapsedMs: Date.now() - info.time.started, - output: yield* shell.output(info.id).pipe( - Effect.map((page) => page.output.slice(-500)), - Effect.orElseSucceed(() => undefined), - ), + process: pid === undefined ? undefined : yield* processState(pid), + tail: info + ? yield* Effect.tryPromise(() => Bun.file(info.file).slice(-500).text()).pipe( + Effect.orElseSucceed(() => undefined), + ) + : undefined, } }), ) - return yield* Effect.fail( - new Error(`${JSON.stringify(command)} did not settle within 15s: ${JSON.stringify({ pending, running })}`), - ) + 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 From d9947b5f031ca10ab2daaacd1bd03c8a433d0bd8 Mon Sep 17 00:00:00 2001 From: shuv1337 Date: Fri, 13 Nov 2026 13:26:19 -0800 Subject: [PATCH 3/4] test(core): read a stalled command's output tail from its size Co-authored-by: Cursor --- packages/core/test/tool-shell.test.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/core/test/tool-shell.test.ts b/packages/core/test/tool-shell.test.ts index 4fbf7834a37e..2f83159d9348 100644 --- a/packages/core/test/tool-shell.test.ts +++ b/packages/core/test/tool-shell.test.ts @@ -416,9 +416,11 @@ const stalledPermissionCommand = Effect.fnUntraced(function* ( pid, process: pid === undefined ? undefined : yield* processState(pid), tail: info - ? yield* Effect.tryPromise(() => Bun.file(info.file).slice(-500).text()).pipe( - Effect.orElseSucceed(() => undefined), - ) + ? 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, } }), From 1c54d5e9558d157910b3da848ab3847864419049 Mon Sep 17 00:00:00 2001 From: shuv1337 Date: Fri, 13 Nov 2026 14:08:05 -0800 Subject: [PATCH 4/4] test(core): stop a command only after it wrote all expected output Co-authored-by: Cursor --- packages/core/test/tool-shell.test.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/core/test/tool-shell.test.ts b/packages/core/test/tool-shell.test.ts index 2f83159d9348..1c70279fb455 100644 --- a/packages/core/test/tool-shell.test.ts +++ b/packages/core/test/tool-shell.test.ts @@ -2313,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 @@ -2334,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) @@ -2358,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") @@ -2406,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}).`, @@ -2583,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,