test: deflake terminal-agent concurrent startup (write the placeholder record before spawning, like the product) and qa-evidence resumed pipe (barrier on a full high-water mark instead of a sleep)

This commit is contained in:
garrytan committed 2026-10-02 22:36:44 +00:00
1 parent 3d67b62c9c
commit 9b0a06b180
2 files changed
+18 -12

No files matched your search

+13 -11
View File
@@ -299,25 +299,27 @@ describe('terminal-agent owned lifecycle regression', () => {
const stateFile = path.join(stateDir, 'browse.json');
const barrier = path.join(stateDir, 'go');
const ownerStartTime = readAgentStartTime(process.pid);
const rawAgent = (gen: string, paused: boolean) => Bun.spawn(['bun', 'run', path.join(sourceDir, 'terminal-agent.ts'), `--agent-gen=${gen}`], {
env: { ...process.env, BROWSE_STATE_FILE: stateFile, BROWSE_OWNER_PID: String(process.pid),
BROWSE_OWNER_START_TIME: ownerStartTime, BROWSE_AGENT_GEN: gen, NODE_ENV: 'test',
GSTACK_TERMINAL_OWNER_WATCHDOG_MS: '25',
...(paused ? { GSTACK_TERMINAL_TEST_PUBLISH_BARRIER: barrier } : {}) },
stdio: ['ignore', 'ignore', 'ignore'],
});
const rawAgent = (gen: string, paused: boolean) => {
writeAgentRecord(stateDir, { pid: 0, gen, startedAt: Date.now(), ownerPid: process.pid, ownerStartTime });
const agent = Bun.spawn(['bun', 'run', path.join(sourceDir, 'terminal-agent.ts'), `--agent-gen=${gen}`], {
env: { ...process.env, BROWSE_STATE_FILE: stateFile, BROWSE_OWNER_PID: String(process.pid),
BROWSE_OWNER_START_TIME: ownerStartTime, BROWSE_AGENT_GEN: gen, NODE_ENV: 'test',
GSTACK_TERMINAL_OWNER_WATCHDOG_MS: '25',
...(paused ? { GSTACK_TERMINAL_TEST_PUBLISH_BARRIER: barrier } : {}) },
stdio: ['ignore', 'ignore', 'ignore'],
});
writeAgentRecord(stateDir, { pid: agent.pid, gen, startedAt: Date.now(),
startTime: readAgentStartTime(agent.pid), ownerPid: process.pid, ownerStartTime });
return agent;
};
const old = rawAgent('synthetic-old-generation', true);
let winner: ReturnType<typeof Bun.spawn> | undefined;
try {
writeAgentRecord(stateDir, { pid: old.pid, gen: 'synthetic-old-generation', startedAt: Date.now(),
startTime: readAgentStartTime(old.pid), ownerPid: process.pid, ownerStartTime });
// Startup waits cover a cold `bun run` of the agent; under a fully
// loaded 16-shard run that alone can exceed 3s. They return as soon as
// the file appears, so the ordering contract below is unchanged.
expect(await waitFor(() => fs.existsSync(`${barrier}.ready`), 8000)).toBe(true);
winner = rawAgent('synthetic-new-generation', false);
writeAgentRecord(stateDir, { pid: winner.pid, gen: 'synthetic-new-generation', startedAt: Date.now(),
startTime: readAgentStartTime(winner.pid), ownerPid: process.pid, ownerStartTime });
expect(await waitFor(() => fs.existsSync(path.join(stateDir, 'terminal-port')), 8000)).toBe(true);
const port = fs.readFileSync(path.join(stateDir, 'terminal-port'), 'utf8');
const token = fs.readFileSync(path.join(stateDir, 'terminal-internal-token'), 'utf8');
+5 -1
View File
@@ -162,7 +162,8 @@ test('public permission never exposes detected credentials in output or receipts
test.each(['resumed', 'blocked-forever'])('evidence receipts survive a genuinely %s stdout pipe without extending command execution', async mode => {
const f = fixture();
const preload = path.join(f.root, 'full-pipe.ts');
fs.writeFileSync(preload, `import { write } from 'node:fs'; if (process.argv[1] === ${JSON.stringify(CLI)}) { write(1, Buffer.alloc(2 * 1024 * 1024, 32), () => {}); await Bun.sleep(100); }`);
const saturated = path.join(f.root, 'stdout-saturated');
fs.writeFileSync(preload, `import { existsSync, write } from 'node:fs'; if (process.argv[1] === ${JSON.stringify(CLI)}) { write(1, Buffer.alloc(2 * 1024 * 1024, 32), () => {}); while (!existsSync(${JSON.stringify(saturated)})) await Bun.sleep(10); }`);
const child = spawn(process.execPath, ['--preload', preload, CLI, 'capture', f.root, '001', '--timeout-ms', '1000', '--', process.execPath, '-e', `require('node:fs').writeFileSync(${JSON.stringify(path.join(f.root, 'effect'))}, 'once'); process.exit(69);`], { cwd: f.root, stdio: ['ignore', 'pipe', 'pipe'] });
let stdout = '', stderr = '';
child.stdout!.on('data', bytes => { stdout += bytes; });
@@ -172,6 +173,9 @@ test.each(['resumed', 'blocked-forever'])('evidence receipts survive a genuinely
const closed = new Promise<void>(resolve => child.once('close', () => resolve()));
let timer: ReturnType<typeof setTimeout> | undefined;
try {
for (let index = 0; index < 300 && child.stdout!.readableLength < child.stdout!.readableHighWaterMark; index++) await Bun.sleep(10);
expect(child.stdout!.readableLength).toBeGreaterThanOrEqual(child.stdout!.readableHighWaterMark);
fs.writeFileSync(saturated, '');
const file = path.join(f.root, '.qa-evidence/001/receipt.json');
for (let index = 0; index < 300 && !fs.existsSync(file); index++) await Bun.sleep(10);
expect(fs.existsSync(file)).toBe(true);