diff --git a/changelog.d/fix-qa-cleanup-exiting-agent-and-contributor-test-network.md b/changelog.d/fix-qa-cleanup-exiting-agent-and-contributor-test-network.md new file mode 100644 index 000000000..018f1e63c --- /dev/null +++ b/changelog.d/fix-qa-cleanup-exiting-agent-and-contributor-test-network.md @@ -0,0 +1 @@ +**Two gstack tests no longer fail at random.** The QA-only cleanup check no longer refuses on a terminal agent that exited a moment ago and has not been collected by its parent yet, and the contributor-mode test names its base branch so it no longer waits on GitHub and the network, which pushed it past Bun's default five-second limit under load. diff --git a/test/helpers/qa-only-cleanup.ts b/test/helpers/qa-only-cleanup.ts index 74ed64a5f..78ec1a106 100644 --- a/test/helpers/qa-only-cleanup.ts +++ b/test/helpers/qa-only-cleanup.ts @@ -77,7 +77,7 @@ async function settleOwnedBrowser(directory: string, deadlineEpochMs: number): P const fixtureAgents = matches.stdout.split('\n').map(Number).filter(pid => { if (!Number.isSafeInteger(pid) || pid <= 1 || !/terminal-agent/.test(readPidCmdline(pid))) return false; const cwd = processCwd(pid); - if (!cwd && isProcessAlive(pid)) fail(`terminal agent ${pid} has no readable working directory`); + if (!cwd && isProcessAlive(pid) && /terminal-agent/.test(readPidCmdline(pid))) fail(`terminal agent ${pid} has no readable working directory`); return cwd === root; }); const port = fs.existsSync(path.join(stateDir, 'terminal-port')); diff --git a/test/owner-brief-contributor-banned.test.ts b/test/owner-brief-contributor-banned.test.ts index 03ca4a558..88a9e6558 100644 --- a/test/owner-brief-contributor-banned.test.ts +++ b/test/owner-brief-contributor-banned.test.ts @@ -147,7 +147,7 @@ describe('contributor mode', () => { const r = bin('gstack-contributor-mode', ['instructions']); expect(r.status).toBe(EXIT.ok); expect(r.out.trim()).toBe(CONTRIBUTOR_INSTRUCTIONS); - const forced = bin('gstack-contributor-mode', ['detect', '--contributor', '--author', 'x']); + const forced = bin('gstack-contributor-mode', ['detect', '--contributor', '--author', 'x', '--base', 'main']); expect(forced.out.split('\n')[0]).toMatch(/^CONTRIBUTOR_MODE: on author=x source=flag/); expect(forced.out).toContain('CONTRIBUTOR-PR MODE'); }); diff --git a/test/qa-only-cleanup.test.ts b/test/qa-only-cleanup.test.ts index 3d9c3c87e..31bf95f98 100644 --- a/test/qa-only-cleanup.test.ts +++ b/test/qa-only-cleanup.test.ts @@ -162,6 +162,52 @@ Bun.spawnSync = (...args) => { } }, 15_000); +// Free Tests 45856565667 (missing-terminal): the fixture's agent exited between the helper's command-line and working +// directory reads, and its unreaped pid still answered kill(0), so cleanup refused on an agent that was no longer running. +test.skipIf(process.platform !== 'linux')('an agent that exits between its identity reads is not a running fixture agent', async () => { + const dir = fs.mkdtempSync(path.join(fs.realpathSync(os.tmpdir()), 'qa-exit-')); + const stateDir = path.join(dir, '.gstack'); + fs.mkdirSync(stateDir); + fs.writeFileSync(path.join(stateDir, 'browse.json'), JSON.stringify({ pid: process.pid, instanceId: 'owned' })); + fs.writeFileSync(path.join(stateDir, 'terminal-port'), '1'); + const marker = path.join(dir, 'agent-exited'); + const parent = Bun.spawn(['sh', '-c', '"$0" -e "setInterval(() => {}, 1000)" browse/src/terminal-agent.ts --agent-gen=exiting & echo $! > agent-pid; exec sleep 30', + process.execPath], { cwd: dir, stdout: 'ignore', stderr: 'ignore' }); + let restore: (() => void) | undefined; + try { + await waitFor(() => readPidCmdline(parent.pid) === 'sleep 30' && fs.existsSync(path.join(dir, 'agent-pid')) && /terminal-agent/.test(readPidCmdline(Number(fs.readFileSync(path.join(dir, 'agent-pid'), 'utf8'))))); + const agentPid = Number(fs.readFileSync(path.join(dir, 'agent-pid'), 'utf8')); + const preload = path.join(dir, 'exit-after-cmdline.ts'); + fs.writeFileSync(preload, ` +import { plugin } from 'bun'; +import * as fs from 'node:fs'; +globalThis.exitFixtureAgent = () => { + process.kill(${agentPid}, 'SIGKILL'); + while (!/^\\d+ \\(.*\\) Z/.test(fs.readFileSync('/proc/${agentPid}/stat', 'utf8'))) Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 5); + fs.writeFileSync(${JSON.stringify(marker)}, 'zombie'); +}; +plugin({ name: 'exit-after-cmdline', setup(build) { + build.onLoad({ filter: /\\/browse\\/src\\/xvfb\\.ts$/ }, async args => ({ loader: 'ts', contents: (await Bun.file(args.path).text()).replace( + 'export function readPidCmdline(pid: number): string {', + 'export function readPidCmdline(pid: number): string { const value = readPidCmdlineNow(pid); if (pid === ${agentPid} && value) globalThis.exitFixtureAgent(); return value; }\\nfunction readPidCmdlineNow(pid: number): string {') })); +} }); +`); + const original = Bun.spawn; + const spy = spyOn(Bun, 'spawn').mockImplementation(((args: string[], options: any) => { + expect(args[1]).toBe(path.join(ROOT, 'test/helpers/qa-only-cleanup.ts')); + return original([args[0], '--preload', preload, ...args.slice(1)], options); + }) as typeof Bun.spawn); + restore = () => spy.mockRestore(); + await expect(stopQaOnlyBrowser(dir, 3000)).rejects.toThrow(/terminal identity is unavailable \(record absent; terminal-port present; running fixture agents: none;/); + expect(fs.readFileSync(marker, 'utf8')).toBe('zombie'); + } finally { + restore?.(); + parent.kill('SIGKILL'); + await parent.exited; + fs.rmSync(dir, { recursive: true, force: true }); + } +}, 15_000); + // Census 45122546426 (qa-only-no-fix): cleanup ran 58 s after a headless daemon started, before its first 60 s watchdog // tick spawned a terminal agent, and failed on the record that had never been written ("daemon log terminal-agent // lines: none"). The same daemon before its first tick settles with Chromium and no terminal.