Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
test(cli): run Ctrl-C quarantine regressions under Bun native terminals (#5244)
time-attack71d442c3857e
diff --git a/test/cli-cwd-dotenv-quarantine.test.ts b/test/cli-cwd-dotenv-quarantine.test.tsindex 70c4aa3c9f..6b0a4aa87b 100644--- a/test/cli-cwd-dotenv-quarantine.test.ts+++ b/test/cli-cwd-dotenv-quarantine.test.ts@@ -17,6 +17,7 @@ import { tmpdir } from 'node:os';import { dirname, join } from 'node:path';import { afterAll, describe, expect, test } from 'bun:test';import { cwdIsOperatorConfigDir } from '../src/core/cli-preflight.ts';+import { ptySupported } from './helpers/tty-harness.ts';import { withEnv } from './helpers/with-env.ts';function findRepoRoot(from: string): string {@@ -222,23 +223,25 @@ describe('cli preflight ordering: cwd quarantine → ~/.gbrain/.env → guardrai// plus a `sh` PRESENCE probe: `GIT_SSL_NO_VERIFY=` (empty) still disables TLS// verification in git, so a dropped key must be ABSENT, not ''.const GIT_BIN = Bun.which('git');-// util-linux `script` gives the wrapper a real pty so process.stdin.isTTY is-// true — the only way to exercise the tty (ignore-only) SIGINT branch faithfully.-const SCRIPT_BIN = Bun.which('script');+// Bun's native terminal (the primitive test/helpers/tty-harness.ts wraps) gives+// the wrapper a real pty on Linux and macOS alike; util-linux and BSD `script`+// disagree on flags and BSD `script` rejects a piped stdin. launchTty itself is+// not used: its hermeticChildEnv base passes auth keys through, and these+// fixtures need their exact hermeticEnv. Windows has no POSIX pty: skipped there.+const NATIVE_PTY = process.platform !== 'win32' && ptySupported();/**- * The command line handed to `script -c`. `script` runs it through `$SHELL`- * (falling back to /bin/sh — hermeticEnv sets no SHELL), and the pty delivers+ * The command line handed to /bin/sh under a real PTY. The pty delivers* Ctrl-C to the WHOLE foreground group, that shell included. Which /bin/sh it- * is then decides what `script` reports, not the wrapper or the re-run:+ * is then decides what the PTY reports, not the wrapper or the re-run:* - bash waits for its foreground child and, when the child did NOT die of* SIGINT, treats the signal as handled and relays the child's status (42);* - dash (/bin/sh on Debian/Ubuntu, whose package also patches out upstream* dash's `sh -c cmd` → exec optimisation) remembers the SIGINT while it- * waits, then re-raises it on itself once the child returns — `script` sees- * a child killed by SIGINT and exits 128+2 = 130 although wrapper and- * re-run both behaved (the ubuntu-latest-only failure of the two pty tests).+ * waits, then re-raises it on itself once the child returns — the PTY sees+ * a child killed by SIGINT rather than exit 42 although the wrapper and+ * re-run both behaved (the original ubuntu-latest-only failure).* `exec` replaces the shell with the wrapper, so no intermediate shell is left- * in the foreground group and the status `script` relays is the wrapper's under+ * in the foreground group and the status the PTY relays is the wrapper's under* either shell. Redirections stay on the exec line: the shell applies them* before it execs, so the all-stdio-non-tty shape below still holds.*/@@ -429,13 +432,13 @@ describe('a cwd .env cannot reach the programs gbrain spawns (sanitized re-run)'});// A3-5(b): with a real terminal attached the wrapper's stdin IS a tty, so it- // takes the ignore-only SIGINT branch. `script` runs the wrapper under a pty;- // writing \x03 to script's stdin is the terminal Ctrl-C, delivered by the pty+ // takes the ignore-only SIGINT branch. Bun's native PTY runs the wrapper;+ // writing \x03 to the PTY is the terminal Ctrl-C, delivered by the pty// to the whole foreground process group (wrapper + re-run) exactly like a real// terminal. The tty wrapper does NOT forward, so the re-run receives SIGINT// exactly once and its once() graceful handler completes (exit 42) — a second,// forwarded delivery would have killed it after once() removed its listener.- test.skipIf(!GIT_BIN || !SCRIPT_BIN)('Ctrl-C from a real terminal (pty) reaches the re-run exactly once: the tty wrapper ignores SIGINT and the once() graceful handler completes (exit 42)', async () => {+ test.skipIf(!GIT_BIN || !NATIVE_PTY)('Ctrl-C from a real terminal (pty) reaches the re-run exactly once: the tty wrapper ignores SIGINT and the once() graceful handler completes (exit 42)', async () => {const { dir, entry, ready } = hostileGitRepo({entryBody: [// serve-http's shape: once('SIGINT') + a graceful-shutdown delay before exit.@@ -445,21 +448,25 @@ describe('a cwd .env cannot reach the programs gbrain spawns (sanitized re-run)'].join('\n'),});const cmd = ptyCommand(`${process.execPath} ${entry} --preflight`); // exec'd: see ptyCommand- const proc = Bun.spawn([SCRIPT_BIN!, '-qec', cmd, '/dev/null'], {- cwd: dir, env: hermeticEnv(dir), stdin: 'pipe', stdout: 'pipe', stderr: 'pipe',+ const proc = Bun.spawn(['/bin/sh', '-c', cmd], {+ cwd: dir, env: hermeticEnv(dir),+ terminal: { cols: 80, rows: 24, data() { /* drain pty output */ } },});- const drain = (async () => { for await (const _ of proc.stdout) { /* mux pty output */ } })();const timer = setTimeout(() => { try { proc.kill('SIGKILL'); } catch { /* exited */ } }, CHILD_KILL_AFTER_MS);try {await waitReady(ready);- proc.stdin!.write('\x03'); // terminal Ctrl-C → pty → the foreground group- proc.stdin!.flush();+ proc.terminal!.write('\x03'); // terminal Ctrl-C → pty → the foreground groupconst code = await proc.exited;- await drain;expect(proc.signalCode).toBeNull();expect(code).toBe(42);} finally {clearTimeout(timer);+ try {+ if (proc.exitCode === null) proc.kill('SIGKILL');+ await proc.exited;+ } finally {+ proc.terminal!.close();+ }}});@@ -499,9 +506,9 @@ describe('a cwd .env cannot reach the programs gbrain spawns (sanitized re-run)'// Ctrl-C reached wrapper and child and the wrapper forwarded a SECOND SIGINT,// aborting the child's once('SIGINT') graceful handler (exit 130). The wrapper// now probes for a controlling terminal (open("/dev/tty")) and forwards only- // when there is none. `script` provides the pty; the shell inside it redirects+ // when there is none. Bun provides the pty; the shell inside it redirects// ALL THREE stdio to non-tty files — the `</dev/null >log 2>&1` shape.- test.skipIf(!GIT_BIN || !SCRIPT_BIN)('Ctrl-C with a controlling terminal but all three stdio non-tty: the wrapper does NOT forward; the once() graceful handler completes (exit 42)', async () => {+ test.skipIf(!GIT_BIN || !NATIVE_PTY)('Ctrl-C with a controlling terminal but all three stdio non-tty: the wrapper does NOT forward; the once() graceful handler completes (exit 42)', async () => {const { dir, entry, ready } = hostileGitRepo({entryBody: [`process.once('SIGINT', () => { setTimeout(() => process.exit(42), ${GRACEFUL_EXIT_DELAY_MS}); });`,@@ -512,21 +519,26 @@ describe('a cwd .env cannot reach the programs gbrain spawns (sanitized re-run)'const out = join(dir, 'wrapper.out');const err = join(dir, 'wrapper.err');const cmd = ptyCommand(`${process.execPath} ${entry} --preflight`, ` </dev/null >${out} 2>${err}`); // exec'd: see ptyCommand- const proc = Bun.spawn([SCRIPT_BIN!, '-qec', cmd, '/dev/null'], {- cwd: dir, env: hermeticEnv(dir), stdin: 'pipe', stdout: 'pipe', stderr: 'pipe',+ const proc = Bun.spawn(['/bin/sh', '-c', cmd], {+ cwd: dir, env: hermeticEnv(dir),+ terminal: { cols: 80, rows: 24, data() { /* drain pty output */ } },});- const drain = (async () => { for await (const _ of proc.stdout) { /* mux pty output */ } })();const timer = setTimeout(() => { try { proc.kill('SIGKILL'); } catch { /* exited */ } }, CHILD_KILL_AFTER_MS);try {await waitReady(ready);- proc.stdin!.write('\x03'); // terminal Ctrl-C → pty → the foreground group (wrapper AND re-run)- proc.stdin!.flush();+ proc.terminal!.write('\x03'); // terminal Ctrl-C → pty → the foreground group (wrapper AND re-run)const code = await proc.exited;- await drain;+ expect(proc.signalCode).toBeNull();expect(code).toBe(42);expect(readFileSync(err, 'utf8')).toContain('[env] Ignoring'); // the wrapper's stderr really was the file, not the pty} finally {clearTimeout(timer);+ try {+ if (proc.exitCode === null) proc.kill('SIGKILL');+ await proc.exited;+ } finally {+ proc.terminal!.close();+ }}});