|
| 1 | +/** |
| 2 | + * `opencode run` with a socket stdin nobody closes (bin/run-stdin.js). |
| 3 | + * |
| 4 | + * Upstream `run` reads a non-TTY stdin to EOF before it connects to its server, with no timeout. The stub CLI below |
| 5 | + * does the same (`text(process.stdin)` for `run`), so a launcher that hands it a never-closed socket hangs exactly as |
| 6 | + * the real CLI does. The driver spawns the launcher from real Node with `stdio: "pipe"` — a libuv socketpair, the |
| 7 | + * stdio an agent harness or `child_process` caller gives a child — and kills the launcher's process group after 3 s. |
| 8 | + * Offline; no real CLI, credentials or model calls. |
| 9 | + */ |
| 10 | +import { describe, expect, test } from "bun:test" |
| 11 | +import { mkdtempSync, rmSync, writeFileSync } from "node:fs" |
| 12 | +import { tmpdir } from "node:os" |
| 13 | +import path from "node:path" |
| 14 | +import { fileURLToPath } from "node:url" |
| 15 | + |
| 16 | +const bin = fileURLToPath(new URL("../../bin/opencode-guardrails", import.meta.url)) |
| 17 | + |
| 18 | +const cli = ` |
| 19 | +import { text } from "node:stream/consumers" |
| 20 | +const args = process.argv.slice(2) |
| 21 | +const piped = args[0] === "run" && !process.stdin.isTTY ? await text(process.stdin) : null |
| 22 | +process.stdout.write(JSON.stringify({ args, piped })) |
| 23 | +` |
| 24 | + |
| 25 | +// <mode> <launcher> <args...>; modes: open (never written or closed), data, slow-end (written at once, closed later), late |
| 26 | +const driver = ` |
| 27 | +import { spawn, spawnSync } from "node:child_process" |
| 28 | +const [mode, launcher, ...args] = process.argv.slice(2) |
| 29 | +const probe = spawnSync(process.execPath, ["-e", "process.stdout.write(String(require('node:fs').fstatSync(0).isSocket()))"], { stdio: ["pipe", "pipe", "inherit"] }) |
| 30 | +const child = spawn(process.execPath, [launcher, ...args], { stdio: ["pipe", "pipe", "pipe"], detached: true }) |
| 31 | +let stdout = "" |
| 32 | +let stderr = "" |
| 33 | +child.stdout.on("data", (chunk) => (stdout += chunk)) |
| 34 | +child.stderr.on("data", (chunk) => (stderr += chunk)) |
| 35 | +child.stdin.on("error", () => {}) |
| 36 | +if (mode === "data") child.stdin.end("piped text") |
| 37 | +if (mode === "slow-end") { |
| 38 | + child.stdin.write("piped ") |
| 39 | + setTimeout(() => child.stdin.end("text"), 600) |
| 40 | +} |
| 41 | +if (mode === "late") setTimeout(() => child.stdin.end("late text"), 1000) |
| 42 | +const killer = setTimeout(() => process.kill(-child.pid, "SIGKILL"), 3000) |
| 43 | +child.on("close", (code, signal) => { |
| 44 | + clearTimeout(killer) |
| 45 | + process.stdout.write(JSON.stringify({ socket: probe.stdout.toString() === "true", code, signal, stdout, stderr })) |
| 46 | + process.exit(0) |
| 47 | +}) |
| 48 | +` |
| 49 | + |
| 50 | +function launch(mode: string, args: string[], env: Record<string, string>) { |
| 51 | + const dir = mkdtempSync(path.join(tmpdir(), "guardrails-run-stdin-")) |
| 52 | + try { |
| 53 | + writeFileSync(path.join(dir, "cli.mjs"), cli, { mode: 0o600 }) |
| 54 | + writeFileSync(path.join(dir, "driver.mjs"), driver, { mode: 0o600 }) |
| 55 | + const run = Bun.spawnSync(["node", path.join(dir, "driver.mjs"), mode, bin, ...args], { |
| 56 | + cwd: dir, |
| 57 | + timeout: 8_000, |
| 58 | + env: { |
| 59 | + PATH: process.env.PATH, |
| 60 | + HOME: dir, |
| 61 | + USERPROFILE: dir, |
| 62 | + OPENCODE_BIN_PATH: path.join(dir, "cli.mjs"), |
| 63 | + ...env, |
| 64 | + }, |
| 65 | + }) |
| 66 | + const result = JSON.parse(new TextDecoder().decode(run.stdout)) as { |
| 67 | + socket: boolean |
| 68 | + code: number | null |
| 69 | + signal: string | null |
| 70 | + stdout: string |
| 71 | + stderr: string |
| 72 | + } |
| 73 | + return { |
| 74 | + ...result, |
| 75 | + cli: result.code === 0 ? (JSON.parse(result.stdout) as { args: string[]; piped: string | null }) : undefined, |
| 76 | + } |
| 77 | + } finally { |
| 78 | + rmSync(dir, { recursive: true, force: true }) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +// A regression hangs until the driver kills the launcher at 3 s, past bun's 5 s default once spawns are added. |
| 83 | +const HANG = 10_000 |
| 84 | + |
| 85 | +// libuv socketpair stdio and POSIX sh are what these cases exercise; the guardrails job runs on ubuntu-latest. |
| 86 | +describe.skipIf(process.platform === "win32")("run with a socket stdin", () => { |
| 87 | + test( |
| 88 | + "a socket nobody closes no longer hangs run: the CLI gets an empty stdin and the launcher says so", |
| 89 | + () => { |
| 90 | + const result = launch("open", ["run", "hi"], { OPENCODE_GUARDRAILS_STDIN_WAIT_MS: "200" }) |
| 91 | + expect(result.socket).toBe(true) |
| 92 | + expect(result.signal, result.stderr).toBeNull() |
| 93 | + expect(result.code, result.stderr).toBe(0) |
| 94 | + expect(result.cli).toEqual({ args: ["run", "--standalone", "hi"], piped: "" }) |
| 95 | + expect(result.stderr).toContain("no input within 200 ms") |
| 96 | + }, |
| 97 | + HANG, |
| 98 | + ) |
| 99 | + |
| 100 | + test("input written to the socket reaches the CLI unchanged", () => { |
| 101 | + const result = launch("data", ["run", "hi"], { OPENCODE_GUARDRAILS_STDIN_WAIT_MS: "200" }) |
| 102 | + expect(result.code, result.stderr).toBe(0) |
| 103 | + expect(result.cli?.piped).toBe("piped text") |
| 104 | + expect(result.stderr).toBe("") |
| 105 | + }) |
| 106 | + |
| 107 | + test("once input starts, it is read to EOF however long the writer takes to close", () => { |
| 108 | + const result = launch("slow-end", ["run", "hi"], { OPENCODE_GUARDRAILS_STDIN_WAIT_MS: "200" }) |
| 109 | + expect(result.code, result.stderr).toBe(0) |
| 110 | + expect(result.cli?.piped).toBe("piped text") |
| 111 | + }) |
| 112 | + |
| 113 | + test("input that starts after the wait is not passed on, and the launcher says so", () => { |
| 114 | + const result = launch("late", ["run", "hi"], { OPENCODE_GUARDRAILS_STDIN_WAIT_MS: "200" }) |
| 115 | + expect(result.code, result.stderr).toBe(0) |
| 116 | + expect(result.cli?.piped).toBe("") |
| 117 | + expect(result.stderr).toContain("OPENCODE_GUARDRAILS_STDIN_WAIT_MS") |
| 118 | + }) |
| 119 | + |
| 120 | + test( |
| 121 | + "other commands keep the inherited stdin and do not wait on it", |
| 122 | + () => { |
| 123 | + const result = launch("open", ["models"], { OPENCODE_GUARDRAILS_STDIN_WAIT_MS: "20000" }) |
| 124 | + expect(result.signal, result.stderr).toBeNull() |
| 125 | + expect(result.cli).toEqual({ args: ["models", "--standalone"], piped: null }) |
| 126 | + }, |
| 127 | + HANG, |
| 128 | + ) |
| 129 | + |
| 130 | + test("a pipe stdin is left to the CLI, so a slow producer is still waited for", () => { |
| 131 | + const dir = mkdtempSync(path.join(tmpdir(), "guardrails-run-pipe-")) |
| 132 | + try { |
| 133 | + writeFileSync(path.join(dir, "cli.mjs"), cli, { mode: 0o600 }) |
| 134 | + const run = Bun.spawnSync(["sh", "-c", `(sleep 1; printf 'late text') | node "${bin}" run hi`], { |
| 135 | + cwd: dir, |
| 136 | + timeout: 8_000, |
| 137 | + env: { |
| 138 | + PATH: process.env.PATH, |
| 139 | + HOME: dir, |
| 140 | + USERPROFILE: dir, |
| 141 | + OPENCODE_BIN_PATH: path.join(dir, "cli.mjs"), |
| 142 | + OPENCODE_GUARDRAILS_STDIN_WAIT_MS: "200", |
| 143 | + }, |
| 144 | + }) |
| 145 | + expect(run.exitCode, new TextDecoder().decode(run.stderr)).toBe(0) |
| 146 | + expect(JSON.parse(new TextDecoder().decode(run.stdout))).toEqual({ |
| 147 | + args: ["run", "--standalone", "hi"], |
| 148 | + piped: "late text", |
| 149 | + }) |
| 150 | + } finally { |
| 151 | + rmSync(dir, { recursive: true, force: true }) |
| 152 | + } |
| 153 | + }) |
| 154 | +}) |
0 commit comments