Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
Revert "test(cli): run Ctrl-C quarantine regressions under Bun native terminals (#5244)"
time-attack903303f69475
diff --git a/test/cli-cwd-dotenv-quarantine.test.ts b/test/cli-cwd-dotenv-quarantine.test.tsindex 6b0a4aa87b..70c4aa3c9f 100644--- a/test/cli-cwd-dotenv-quarantine.test.ts+++ b/test/cli-cwd-dotenv-quarantine.test.ts@@ -17,7 +17,6 @@ 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 {@@ -223,25 +222,23 @@ 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');-// 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();+// 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');/**- * The command line handed to /bin/sh under a real PTY. The pty delivers+ * 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* Ctrl-C to the WHOLE foreground group, that shell included. Which /bin/sh it- * is then decides what the PTY reports, not the wrapper or the re-run:+ * is then decides what `script` 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 — 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).+ * 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).* `exec` replaces the shell with the wrapper, so no intermediate shell is left- * in the foreground group and the status the PTY relays is the wrapper's under+ * in the foreground group and the status `script` 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.*/@@ -432,13 +429,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. Bun's native PTY runs the wrapper;- // writing \x03 to the PTY is the terminal Ctrl-C, delivered by the pty+ // 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// 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 || !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 () => {+ 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 () => {const { dir, entry, ready } = hostileGitRepo({entryBody: [// serve-http's shape: once('SIGINT') + a graceful-shutdown delay before exit.@@ -448,25 +445,21 @@ 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(['/bin/sh', '-c', cmd], {- cwd: dir, env: hermeticEnv(dir),- terminal: { cols: 80, rows: 24, data() { /* drain pty output */ } },+ const proc = Bun.spawn([SCRIPT_BIN!, '-qec', cmd, '/dev/null'], {+ cwd: dir, env: hermeticEnv(dir), stdin: 'pipe', stdout: 'pipe', stderr: 'pipe',});+ 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.terminal!.write('\x03'); // terminal Ctrl-C → pty → the foreground group+ proc.stdin!.write('\x03'); // terminal Ctrl-C → pty → the foreground group+ proc.stdin!.flush();const 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();- }}});@@ -506,9 +499,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. Bun provides the pty; the shell inside it redirects+ // when there is none. `script` 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 || !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 () => {+ 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 () => {const { dir, entry, ready } = hostileGitRepo({entryBody: [`process.once('SIGINT', () => { setTimeout(() => process.exit(42), ${GRACEFUL_EXIT_DELAY_MS}); });`,@@ -519,26 +512,21 @@ 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(['/bin/sh', '-c', cmd], {- cwd: dir, env: hermeticEnv(dir),- terminal: { cols: 80, rows: 24, data() { /* drain pty output */ } },+ const proc = Bun.spawn([SCRIPT_BIN!, '-qec', cmd, '/dev/null'], {+ cwd: dir, env: hermeticEnv(dir), stdin: 'pipe', stdout: 'pipe', stderr: 'pipe',});+ 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.terminal!.write('\x03'); // terminal Ctrl-C → pty → the foreground group (wrapper AND re-run)+ proc.stdin!.write('\x03'); // terminal Ctrl-C → pty → the foreground group (wrapper AND re-run)+ proc.stdin!.flush();const code = await proc.exited;- expect(proc.signalCode).toBeNull();+ await drain;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();- }}});