mirror of
https://github.com/garrytan/gstack.git
synced 2026-09-28 23:52:28 +02:00
* fix(memory-ingest): --scan-secrets scans the rendered page and fails closed --scan-secrets ran gitleaks on the raw transcript .jsonl, then imported a page rendered from it. gitleaks' assignment rules don't match across a JSON-escaped quote (KEY=\"v\" on disk), so a secret the rendered page shows as KEY="v" was imported unflagged. And the gate skipped a file only on scanner "gitleaks" with findings, so a scan that errored (non-zero exit, 16MB maxBuffer overflow on a file with many findings, unparseable report) or could not run (gitleaks missing, slow-probe cooldown) imported the file unscanned. Scan the rendered page body, the exact bytes writeStaged() writes, via a new secretScanText() helper, and skip the file whenever the scan did not complete. Skipped files stay out of the state file, so the next run retries them. Reword the helper warnings and setup-gbrain/memory.md, which described the fail-open as intended. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(test): reconcile Bun failure markers and footer counts * fix(sync-gbrain): verify source-scoped reads without mutation * fix(test): recognize grounded TTHW target choices structurally * fix(aside): make the readiness probe work under zsh and report why it failed The probe built its deadline into `_T` and expanded it unquoted, so `$_T aside repl …` only worked in a shell that word-splits. zsh does not: it looked for a command literally named "gtimeout 30", the probe answered ASIDE_NOT_RUNNING with Aside installed and ready, and every browsing skill fell back to the bundled Chromium in silence. zsh is the macOS default and Aside is macOS-only, so on a stock Mac the probe could never report READY. The deadline becomes a function, `_gs_d`. It receives the command as "$@", already split, so sh, bash and zsh all behave the same, and the gtimeout → timeout → perl alarm chain is unchanged. A 4th arm runs the call unbounded when none of the three is present, which is what the empty `_T` did before. Not `eval`: it re-parses the string, so the parens and `;` of the perl arm become syntax and that arm dies in bash *and* zsh — on a stock Mac, the arm that actually runs. On failure the probe now prints the CLI's reason after ASIDE_NOT_RUNNING:, the shape gstack-render already uses: the first line that starts with a capital letter, i.e. the CLI's own sentence or Node's `Error:` line below its loader frame. "Not running" covers states with different fixes — no window open for the profile, a NODE_OPTIONS preload that kills the CLI — and a bare verdict sent all of them to "open the Aside app". The BROWSER SETUP prose quotes that reason before asking the user to open the app. The text pin asserted the broken invocation verbatim, so it now pins the function and asserts neither `$_T aside repl` nor an eval form comes back. A second test executes the rendered probe in sh, bash and zsh on each of the four deadline arms with stubbed binaries on a narrowed PATH, plus two failing CLIs: one that prints its own sentence, one that crashes like Node with the useful line below the frame. The deadline function costs zero bytes against the lines it replaces; the reason costs 53 per copy of the probe (44 where the reworded BROWSER SETUP line gives 9 back). That moves four guards by the measured amount: plan-devex-review's skeleton cap to 68,550 (measured 68,544), plan-ceo-review's skeleton cap to 80,150 (measured 80,111) and union ratio to 1.081 (measured 1.0803), and plan-eng-review's union ratio to 1.151 (measured 1.1504). Fixes #2842, #2941. * Clarify engineering review startup and decision flow * Fix Windows readiness fixture PATH and command shim * fix(test): recognize grounded TTHW target choices structurally * Clarify engineering review startup and decision flow * fix(test): restrict QA-only fixture tools to its no-Edit contract * v1.90.0.0 fix(sync-gbrain): guard readiness verdicts and refresh metadata * fix(browse): validate canonical upload targets * fix(gbrain): classify structured PGLite busy response * fix(browse): preserve native extension runtime APIs * Fix displayless browser handoff ownership * Accept unique installed autoplan methodology aliases * fix(skills): preserve positional literals during installation * fix(browse): checksum installer contents through stdin * fix(test): normalize Windows checksum fixture paths * test: emulate unavailable shasum in Windows checksum fixture * fix(investigate): preserve owned freeze lifecycle * fix(review): preserve N+1 retry and Red Team completion * fix: bound Aside readiness and preserve safe fallback * test: exercise setup and Chromium on native ARM * fix: preserve install ownership and ARM browser selection * Fix gbrain ingest scan boundaries and seed observation * Refresh managed ship hooks and supervise expanded paid census * Reject resumed gbrain pages excluded by current policy * Recover zombie agent locks safely and enable CI Python venv * Repair paid actor declarations and Aside pitch assertions * Bump consolidated wave to next free minor release * Clarify CEO review admin choices and option tradeoffs * Preserve CEO mode handoff anchors in clarified workflow * Make Windows portability fixtures use shell-native paths * Restore ARM Bun alias and clarify ship review gates * Refresh ship workflow golden snapshots * Fix Windows DX documentation controls without piped stdin * Decode Codex child pipes without Bun's encoded-stream stall * Bound DX pre-review audit before product questions * Clarify trusted review-start read in paid revalidation * Bump consolidated wave to next free minor release * Clarify CEO review admin choices and option tradeoffs * Preserve CEO mode handoff anchors in clarified workflow * Make Windows portability fixtures use shell-native paths * Restore ARM Bun alias and clarify ship review gates * Refresh ship workflow golden snapshots * Fix Windows DX documentation controls without piped stdin * Decode Codex child pipes without Bun's encoded-stream stall * Bound DX pre-review audit before product questions * Clarify trusted review-start read in paid revalidation * Reconcile new main planning flow and paid judge census * fix: reconcile rebased planning and source-bound validation * test: pin cookie workflow judge to scored Sonnet model * fix: keep terminal agent boot out of module imports * fix: preserve pending-question uncertainty in engineering review * fix: stabilize Windows reliability-wave fixtures * fix: clarify design consultation research workflow * fix: preserve independent design consultation inputs * fix: resolve design taste scope and browser research guidance * fix: make consultation opt-in preflight unambiguous * test: await native Edge owner readiness or terminal result --------- Co-authored-by: Bruce Krysiak <brucek@alum.mit.edu> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Antonio Vitalic <antoninte99@gmail.com>
439 lines
21 KiB
TypeScript
439 lines
21 KiB
TypeScript
/** Free behavioral tests: every Codex launch goes through our temporary shim.
|
|
* Uses '/bin/bash' to launch the shim (excluded from Windows curation).
|
|
*/
|
|
import { afterAll, beforeAll, describe, expect, test } from 'bun:test';
|
|
import * as fs from 'node:fs';
|
|
import * as os from 'node:os';
|
|
import * as path from 'node:path';
|
|
import { spawnSync } from 'node:child_process';
|
|
import { CodexHarnessError, runCodexSkill } from './helpers/codex-session-runner';
|
|
import { runRecordedCodexEval, validateCodexDiscovery } from './helpers/codex-eval';
|
|
import type { EvalTestEntry } from './helpers/eval-store';
|
|
|
|
const shellQuote = (value: string) => `'${value.replaceAll("'", "'\\''")}'`;
|
|
|
|
function alive(pid: number): boolean {
|
|
// A reparented, killed grandchild can briefly be a zombie until init reaps
|
|
// it. It is no longer executing or holding any pipe open.
|
|
const status = spawnSync('ps', ['-p', String(pid), '-o', 'stat='], { encoding: 'utf8', timeout: 2_000 });
|
|
return status.status === 0 && !status.stdout.trim().startsWith('Z');
|
|
}
|
|
|
|
async function withFakeCodex(
|
|
body: string,
|
|
check: (fixture: { dir: string; skillDir: string; shimDir: string; pids: () => number[]; tempHome: () => string }) => Promise<void>,
|
|
) {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'codex-lifecycle-fixture-'));
|
|
const shimDir = path.join(dir, 'shims');
|
|
const skillDir = path.join(dir, 'skill');
|
|
const operatorConfig = path.join(dir, 'operator-codex');
|
|
const pidsFile = path.join(dir, 'pids.json');
|
|
const homeFile = path.join(dir, 'child-home.txt');
|
|
const script = path.join(dir, 'fake-codex.ts');
|
|
fs.mkdirSync(shimDir);
|
|
fs.mkdirSync(skillDir);
|
|
fs.mkdirSync(operatorConfig);
|
|
fs.writeFileSync(path.join(skillDir, 'SKILL.md'), '---\nname: fixture\ndescription: local test\n---\nReview the fixture.\n');
|
|
fs.writeFileSync(script, `
|
|
import * as fs from 'node:fs';
|
|
import { spawn } from 'node:child_process';
|
|
const pidsFile = ${JSON.stringify(pidsFile)};
|
|
fs.writeFileSync(pidsFile, JSON.stringify([process.pid]));
|
|
fs.writeFileSync(${JSON.stringify(homeFile)}, process.env.HOME!);
|
|
${body}
|
|
`);
|
|
fs.writeFileSync(path.join(shimDir, 'codex'), `#!/bin/bash\nexec ${shellQuote(process.execPath)} ${shellQuote(script)} "$@"\n`, { mode: 0o755 });
|
|
const original = { PATH: process.env.PATH, CODEX_HOME: process.env.CODEX_HOME, EVALS_HERMETIC: process.env.EVALS_HERMETIC };
|
|
const pids = () => fs.existsSync(pidsFile) ? JSON.parse(fs.readFileSync(pidsFile, 'utf8')) as number[] : [];
|
|
process.env.PATH = `${shimDir}${path.delimiter}${original.PATH}`;
|
|
process.env.CODEX_HOME = operatorConfig; // Never copy real operator credentials.
|
|
process.env.EVALS_HERMETIC = '1';
|
|
try {
|
|
await check({ dir, skillDir, shimDir, pids, tempHome: () => fs.readFileSync(homeFile, 'utf8') });
|
|
} finally {
|
|
for (const pid of pids()) {
|
|
// An intentionally escaped child tests pipe teardown. Only the fixture
|
|
// owner knows its new group ID, so it is explicitly reaped here.
|
|
try { process.kill(-pid, 'SIGKILL'); } catch { /* no such group */ }
|
|
try { process.kill(pid, 'SIGKILL'); } catch { /* already exited */ }
|
|
}
|
|
for (const [key, value] of Object.entries(original)) {
|
|
if (value === undefined) delete process.env[key];
|
|
else process.env[key] = value;
|
|
}
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
}
|
|
|
|
function childHoldingPipes(detached: boolean, exitCode?: number): string {
|
|
return `
|
|
const child = spawn(process.execPath, ['-e', 'setInterval(() => {}, 1000)'], {
|
|
detached: ${detached}, stdio: ['ignore', 'ignore', 'inherit'],
|
|
});
|
|
fs.writeFileSync(pidsFile, JSON.stringify([process.pid, child.pid]));
|
|
child.unref();
|
|
process.stderr.write('invalid fixture metadata before the pipe stalled\\n');
|
|
${exitCode === undefined ? 'setInterval(() => {}, 1000);' : `process.exit(${exitCode});`}
|
|
`;
|
|
}
|
|
|
|
describe('Codex subprocess lifecycle without API calls', () => {
|
|
test('a missing full skill fixture records one harness error without launching Codex', async () => {
|
|
await withFakeCodex(`process.stdout.write(JSON.stringify({ type: 'item.completed', item: {
|
|
type: 'agent_message', text: 'No gstack review skill is installed here.',
|
|
} }));`, async ({ skillDir, pids }) => {
|
|
const source = path.join(skillDir, 'SKILL.md');
|
|
fs.unlinkSync(source);
|
|
const entries: EvalTestEntry[] = [];
|
|
let failure: unknown;
|
|
try {
|
|
await runRecordedCodexEval({
|
|
name: 'missing-full-fixture', suite: 'codex-lifecycle', budgetMs: 2_000,
|
|
run: (signal) => runCodexSkill({ skillDir, prompt: 'fixture', timeoutMs: 2_000, signal }),
|
|
validate: validateCodexDiscovery,
|
|
record: (entry) => entries.push(entry),
|
|
});
|
|
} catch (error) { failure = error; }
|
|
expect(failure).toBeInstanceOf(Error);
|
|
expect((failure as Error).message).toContain('ENOENT');
|
|
expect((failure as Error).message).toContain(source);
|
|
expect(entries).toHaveLength(1);
|
|
expect(entries[0]).toMatchObject({ passed: false, exit_reason: 'harness_error', error: (failure as Error).message });
|
|
expect(pids()).toEqual([]);
|
|
});
|
|
});
|
|
|
|
test('captures full JSONL/stderr, including split UTF-8, and removes the temporary home', async () => {
|
|
await withFakeCodex(`
|
|
const line = Buffer.from(JSON.stringify({ type: 'item.completed', item: { type: 'agent_message', text: 'gstack review: café' } }) + '\\n');
|
|
for (const byte of line) fs.writeSync(1, Buffer.from([byte]));
|
|
for (const byte of Buffer.from('fixture warning: café\\n')) fs.writeSync(2, Buffer.from([byte]));
|
|
process.stdout.write(JSON.stringify({ type: 'turn.completed', usage: { input_tokens: 7, output_tokens: 3 } }));
|
|
`, async ({ skillDir, tempHome, pids }) => {
|
|
const result = await runCodexSkill({ skillDir, prompt: 'fixture', timeoutMs: 2_000 });
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.output).toBe('gstack review: café');
|
|
expect(result.stderr).toBe('fixture warning: café\n');
|
|
expect(result.tokens).toBe(10);
|
|
expect(result.rawLines).toHaveLength(2);
|
|
expect(fs.existsSync(tempHome())).toBe(false);
|
|
expect(pids().every((pid) => !alive(pid))).toBe(true);
|
|
});
|
|
});
|
|
|
|
for (const exitCode of [2, 137]) {
|
|
test(`retains process exit ${exitCode}`, async () => {
|
|
await withFakeCodex(`process.exit(${exitCode});`, async ({ skillDir }) => {
|
|
const result = await runCodexSkill({ skillDir, prompt: 'fixture', timeoutMs: 2_000 });
|
|
expect(result.exitCode).toBe(exitCode);
|
|
});
|
|
});
|
|
}
|
|
|
|
for (const exitCode of [0, 2]) {
|
|
test(`exit ${exitCode} reaps descendants that have already closed inherited pipes`, async () => {
|
|
await withFakeCodex(`
|
|
const child = spawn(process.execPath, ['-e', 'setInterval(() => {}, 1000)'], { stdio: 'ignore' });
|
|
fs.writeFileSync(pidsFile, JSON.stringify([process.pid, child.pid]));
|
|
child.unref();
|
|
process.exit(${exitCode});
|
|
`, async ({ skillDir, pids, tempHome }) => {
|
|
const result = await runCodexSkill({ skillDir, prompt: 'fixture', timeoutMs: 2_000 });
|
|
expect(result.exitCode).toBe(exitCode);
|
|
expect(pids()).toHaveLength(2);
|
|
const cleanupDeadline = Date.now() + 1_000;
|
|
let allExited = pids().every((pid) => !alive(pid));
|
|
while (!allExited && Date.now() < cleanupDeadline) {
|
|
await new Promise((resolve) => setTimeout(resolve, Math.min(25, cleanupDeadline - Date.now())));
|
|
allExited = pids().every((pid) => !alive(pid));
|
|
}
|
|
expect(allExited).toBe(true);
|
|
expect(fs.existsSync(tempHome())).toBe(false);
|
|
});
|
|
});
|
|
}
|
|
|
|
test('a real SIGKILL is recorded as exit 137, not a timeout', async () => {
|
|
await withFakeCodex("process.kill(process.pid, 'SIGKILL');", async ({ skillDir }) => {
|
|
const result = await runCodexSkill({ skillDir, prompt: 'fixture', timeoutMs: 2_000 });
|
|
expect(result.exitCode).toBe(137);
|
|
});
|
|
});
|
|
|
|
test('timeout kills the process group and drains both streams within the allowance', async () => {
|
|
await withFakeCodex(childHoldingPipes(false), async ({ skillDir, pids, tempHome }) => {
|
|
const started = Date.now();
|
|
const result = await runCodexSkill({ skillDir, prompt: 'fixture', timeoutMs: 750 });
|
|
expect(result.exitCode).toBe(124);
|
|
expect(Date.now() - started).toBeLessThan(5_000);
|
|
expect(pids()).toHaveLength(2);
|
|
expect(pids().every((pid) => !alive(pid))).toBe(true);
|
|
expect(fs.existsSync(tempHome())).toBe(false);
|
|
});
|
|
}, 10_000);
|
|
|
|
test('abort kills a running group and removes its temporary home', async () => {
|
|
await withFakeCodex(childHoldingPipes(false), async ({ skillDir, pids, tempHome }) => {
|
|
const controller = new AbortController();
|
|
const timer = setTimeout(() => controller.abort(), 750);
|
|
try {
|
|
const result = await runCodexSkill({ skillDir, prompt: 'fixture', timeoutMs: 10_000, signal: controller.signal });
|
|
expect(result.exitCode).toBe(124);
|
|
expect(pids()).toHaveLength(2);
|
|
expect(pids().every((pid) => !alive(pid))).toBe(true);
|
|
expect(fs.existsSync(tempHome())).toBe(false);
|
|
} finally { clearTimeout(timer); }
|
|
});
|
|
}, 10_000);
|
|
|
|
test('an already aborted call never launches Codex', async () => {
|
|
await withFakeCodex('process.exit(0);', async ({ skillDir, pids }) => {
|
|
const controller = new AbortController();
|
|
controller.abort();
|
|
const result = await runCodexSkill({ skillDir, prompt: 'fixture', signal: controller.signal });
|
|
expect(result.exitCode).toBe(124);
|
|
expect(pids()).toEqual([]);
|
|
});
|
|
});
|
|
|
|
test('a real error exit survives an inherited stderr stall', async () => {
|
|
await withFakeCodex(childHoldingPipes(false, 2), async ({ skillDir, pids, tempHome }) => {
|
|
const started = Date.now();
|
|
const result = await runCodexSkill({ skillDir, prompt: 'fixture', timeoutMs: 2_000 });
|
|
expect(result.exitCode).toBe(2);
|
|
expect(result.stderr).toContain('invalid fixture metadata');
|
|
expect(Date.now() - started).toBeLessThan(8_000);
|
|
expect(pids().every((pid) => !alive(pid))).toBe(true);
|
|
expect(fs.existsSync(tempHome())).toBe(false);
|
|
});
|
|
}, 10_000);
|
|
|
|
test('exit zero with an escaped stderr holder fails closed and preserves captured evidence', async () => {
|
|
await withFakeCodex(childHoldingPipes(true, 0), async ({ skillDir, tempHome }) => {
|
|
const started = Date.now();
|
|
let failure: unknown;
|
|
try { await runCodexSkill({ skillDir, prompt: 'fixture', timeoutMs: 2_000 }); }
|
|
catch (error) { failure = error; }
|
|
expect(failure).toBeInstanceOf(CodexHarnessError);
|
|
const error = failure as CodexHarnessError;
|
|
expect(error.message).toContain('output drain exceeded 5000ms');
|
|
expect(error.result?.exitCode).toBe(0);
|
|
expect(error.result?.stderr).toContain('invalid fixture metadata');
|
|
expect(Date.now() - started).toBeLessThan(8_000);
|
|
expect(fs.existsSync(tempHome())).toBe(false);
|
|
});
|
|
}, 10_000);
|
|
|
|
test('spawn errors reject instead of masquerading as a successful empty result', async () => {
|
|
await withFakeCodex('process.exit(0);', async ({ skillDir, dir, pids }) => {
|
|
await expect(runCodexSkill({ skillDir, prompt: 'fixture', cwd: path.join(dir, 'missing'), timeoutMs: 2_000 }))
|
|
.rejects.toBeInstanceOf(CodexHarnessError);
|
|
expect(pids()).toEqual([]);
|
|
});
|
|
});
|
|
|
|
test('binary lookup time counts against the same process budget', async () => {
|
|
await withFakeCodex('process.exit(0);', async ({ skillDir, shimDir, pids }) => {
|
|
fs.writeFileSync(path.join(shimDir, 'which'), '#!/bin/bash\nexec sleep 2\n', { mode: 0o755 });
|
|
const started = Date.now();
|
|
// Bun caches executable resolution within a process. A fresh process
|
|
// must see this fake `which` before any earlier lookup caches /usr/bin/which.
|
|
const script = `
|
|
import { runCodexSkill } from ${JSON.stringify(path.join(import.meta.dir, 'helpers', 'codex-session-runner.ts'))};
|
|
console.log(JSON.stringify(await runCodexSkill({ skillDir: ${JSON.stringify(skillDir)}, prompt: 'fixture', timeoutMs: 100 })));
|
|
`;
|
|
const child = Bun.spawnSync([process.execPath, '-e', script], { env: process.env, timeout: 2_000 });
|
|
expect(child.exitCode, child.stderr.toString()).toBe(0);
|
|
const result = JSON.parse(child.stdout.toString());
|
|
expect(result.exitCode).toBe(124);
|
|
expect(Date.now() - started).toBeLessThan(1_500);
|
|
expect(pids()).toEqual([]);
|
|
});
|
|
});
|
|
});
|
|
|
|
// Module mocks live in a fresh Bun process so they cannot affect this file's
|
|
// real fake-executable cases or any other free test in the parent shard.
|
|
describe('Codex stream terminal events', () => {
|
|
const cases = [
|
|
{ stream: 'stdout', fault: 'none', exitCode: 0 },
|
|
...(['stdout', 'stderr'] as const).flatMap(stream =>
|
|
(['premature-close', 'error'] as const).flatMap(fault =>
|
|
[0, 2, 124].map(exitCode => ({ stream, fault, exitCode })))),
|
|
];
|
|
let fixtureDir = '';
|
|
let observations: any[];
|
|
|
|
beforeAll(() => {
|
|
fixtureDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codex-stream-events-'));
|
|
const script = path.join(fixtureDir, 'stream-events.test.ts');
|
|
const output = path.join(fixtureDir, 'observations.json');
|
|
fs.writeFileSync(script, `
|
|
import { mock, test } from 'bun:test';
|
|
import * as fs from 'node:fs';
|
|
import * as path from 'node:path';
|
|
import { EventEmitter } from 'node:events';
|
|
import { PassThrough } from 'node:stream';
|
|
const fixtureDir = ${JSON.stringify(fixtureDir)};
|
|
const cases = ${JSON.stringify(cases)};
|
|
const skillDir = path.join(fixtureDir, 'skill');
|
|
const temporaryDir = path.join(fixtureDir, 'temporary');
|
|
const operatorConfig = path.join(fixtureDir, 'operator-codex');
|
|
for (const dir of [skillDir, temporaryDir, operatorConfig]) fs.mkdirSync(dir);
|
|
fs.writeFileSync(path.join(skillDir, 'SKILL.md'), '---\\nname: fixture\\ndescription: fixture\\n---\\nReview the fixture.\\n');
|
|
process.env.TMPDIR = temporaryDir;
|
|
process.env.CODEX_HOME = operatorConfig;
|
|
let active;
|
|
mock.module('child_process', () => ({
|
|
spawn(command, args, options) {
|
|
if (command !== 'codex') throw new Error('Unexpected executable');
|
|
const current = active;
|
|
current.spawns++;
|
|
current.home = options.env.HOME;
|
|
queueMicrotask(() => {
|
|
const { child, scenario } = current;
|
|
child.stdout.write(JSON.stringify({ type: 'item.completed', item: {
|
|
type: 'agent_message', text: 'The gstack review found no issues in the current branch diff.',
|
|
} }) + '\\n');
|
|
child.stderr.write('retained fixture stderr\\n');
|
|
const affected = child[scenario.stream];
|
|
const other = child[scenario.stream === 'stdout' ? 'stderr' : 'stdout'];
|
|
if (scenario.fault === 'none') affected.end();
|
|
else if (scenario.fault === 'premature-close') affected.destroy();
|
|
else {
|
|
// The runner must preserve the first real stream error when another
|
|
// stream subsequently reports an error during cleanup.
|
|
affected.once('error', () => other.emit('error', new Error('secondary cleanup error')));
|
|
affected.destroy(new Error(scenario.stream + ' primary stream failure'));
|
|
}
|
|
other.end();
|
|
child.exitCode = scenario.exitCode;
|
|
child.emit('exit', scenario.exitCode, null);
|
|
});
|
|
return current.child;
|
|
},
|
|
spawnSync: () => { throw new Error('Unexpected synchronous subprocess'); },
|
|
execFileSync: () => { throw new Error('Unexpected synchronous subprocess'); },
|
|
}));
|
|
mock.module(${JSON.stringify(path.resolve(import.meta.dir, '../scripts/test-strict-output.ts'))}, () => ({
|
|
killProcessGroup(child, signal) {
|
|
if (child !== active.child) throw new Error('Unknown child');
|
|
active.kills.push(signal);
|
|
},
|
|
}));
|
|
Bun.spawnSync = (args) => {
|
|
if (JSON.stringify(args) !== JSON.stringify(['which', 'codex'])) throw new Error('Unexpected binary lookup');
|
|
active.lookups++;
|
|
return { exitCode: 0, stdout: Buffer.from('/fixture/codex\\n'), stderr: Buffer.alloc(0), signalCode: null };
|
|
};
|
|
const { runCodexSkill } = await import(${JSON.stringify(path.join(import.meta.dir, 'helpers/codex-session-runner.ts'))});
|
|
const { runRecordedCodexEval, validateCodexReview } = await import(${JSON.stringify(path.join(import.meta.dir, 'helpers/codex-eval.ts'))});
|
|
test('observe real runner and recorder with controlled stream lifecycles', async () => {
|
|
const observations = [];
|
|
for (const scenario of cases) {
|
|
const child = Object.assign(new EventEmitter(), {
|
|
pid: undefined, stdout: new PassThrough(), stderr: new PassThrough(), exitCode: null,
|
|
kill() { throw new Error('Group cleanup must be intercepted, never sent to an OS process'); },
|
|
});
|
|
const events = [];
|
|
for (const name of ['stdout', 'stderr']) {
|
|
for (const event of ['end', 'close', 'error']) child[name].on(event, () => events.push(name + ':' + event));
|
|
}
|
|
active = { child, scenario, spawns: 0, lookups: 0, kills: [], home: '' };
|
|
const records = [];
|
|
let result;
|
|
let failure;
|
|
try {
|
|
await runRecordedCodexEval({
|
|
name: 'stream-fixture', suite: 'codex-stream-fixture', budgetMs: 1000,
|
|
run: async signal => {
|
|
result = await runCodexSkill({ skillDir, prompt: 'fixture', timeoutMs: 1000, signal });
|
|
return result;
|
|
},
|
|
validate: validateCodexReview,
|
|
record: entry => records.push(entry),
|
|
});
|
|
} catch (error) { failure = error; result ??= error.result; }
|
|
const beforeLate = JSON.stringify({ result, records });
|
|
const observedEvents = [...events];
|
|
const eof = { stdout: child.stdout.readableEnded, stderr: child.stderr.readableEnded };
|
|
// Cleanup retains harmless error listeners. Late notifications cannot
|
|
// mutate an already returned result or manufacture another attempt.
|
|
child.emit('error', new Error('late process error'));
|
|
child.emit('exit', 99, null);
|
|
for (const name of ['stdout', 'stderr']) {
|
|
child[name].emit('end');
|
|
child[name].emit('close');
|
|
child[name].emit('error', new Error('late stream error'));
|
|
child[name].emit('data', 'late stream data');
|
|
}
|
|
await new Promise(resolve => setTimeout(resolve, 0));
|
|
observations.push({
|
|
scenario, result, records, events: observedEvents, eof,
|
|
failure: failure ? { name: failure.name, message: failure.message } : null,
|
|
lateUnchanged: beforeLate === JSON.stringify({ result, records }),
|
|
spawns: active.spawns, lookups: active.lookups, kills: active.kills,
|
|
temporaryHomeRemoved: !!active.home && !fs.existsSync(active.home),
|
|
streamsDestroyed: child.stdout.destroyed && child.stderr.destroyed,
|
|
});
|
|
}
|
|
fs.writeFileSync(${JSON.stringify(output)}, JSON.stringify(observations));
|
|
}, 5000);
|
|
`);
|
|
const child = spawnSync(process.execPath, ['test', script, '--timeout=5000'], {
|
|
encoding: 'utf8', timeout: 10_000,
|
|
});
|
|
expect(child.status, child.stderr || child.stdout).toBe(0);
|
|
observations = JSON.parse(fs.readFileSync(output, 'utf8'));
|
|
expect(observations).toHaveLength(cases.length);
|
|
}, 15_000);
|
|
|
|
afterAll(() => {
|
|
if (fixtureDir) fs.rmSync(fixtureDir, { recursive: true, force: true });
|
|
});
|
|
|
|
for (const [index, scenario] of cases.entries()) {
|
|
test(`${scenario.stream} ${scenario.fault} with exit ${scenario.exitCode}`, () => {
|
|
const observation = observations[index];
|
|
expect(observation.spawns).toBe(1);
|
|
expect(observation.lookups).toBe(1);
|
|
expect(observation.kills).toEqual(['SIGKILL']);
|
|
expect(observation.temporaryHomeRemoved).toBe(true);
|
|
expect(observation.streamsDestroyed).toBe(true);
|
|
expect(observation.lateUnchanged).toBe(true);
|
|
expect(observation.records).toHaveLength(1);
|
|
expect(observation.result).toMatchObject({
|
|
exitCode: scenario.exitCode,
|
|
output: 'The gstack review found no issues in the current branch diff.',
|
|
stderr: 'retained fixture stderr\n',
|
|
});
|
|
const entry = observation.records[0];
|
|
if (scenario.fault === 'none') {
|
|
expect(observation.failure).toBeNull();
|
|
expect(observation.eof).toEqual({ stdout: true, stderr: true });
|
|
expect(entry).toMatchObject({ passed: true, exit_reason: 'success' });
|
|
} else {
|
|
expect(entry.passed).toBe(false);
|
|
expect(entry.error).toContain('retained fixture stderr');
|
|
if (scenario.exitCode !== 0) {
|
|
expect(entry.exit_reason).toBe(scenario.exitCode === 124 ? 'timeout' : `exit_code_${scenario.exitCode}`);
|
|
expect(observation.failure.message).toContain(`Codex exited with code ${scenario.exitCode}`);
|
|
} else {
|
|
expect(observation.failure.name).toBe('CodexHarnessError');
|
|
expect(entry.exit_reason).toBe('harness_error');
|
|
if (scenario.fault === 'error') {
|
|
expect(entry.error).toContain(`${scenario.stream} primary stream failure`);
|
|
expect(entry.error).not.toContain('secondary cleanup error');
|
|
} else {
|
|
expect(observation.eof[scenario.stream]).toBe(false);
|
|
expect(observation.events).not.toContain(`${scenario.stream}:end`);
|
|
expect(observation.events).not.toContain(`${scenario.stream}:error`);
|
|
expect(entry.error).toContain(scenario.stream);
|
|
expect(entry.error).toContain('before EOF');
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
});
|