diff --git a/packages/acp/tests/executor.test.ts b/packages/acp/tests/executor.test.ts index 29f2bb3a6a..31d56dc47e 100644 --- a/packages/acp/tests/executor.test.ts +++ b/packages/acp/tests/executor.test.ts @@ -519,7 +519,8 @@ describe('acp', () => { // The agent is still stopping and the removal waits for it, so the // directory must still be there. await new Promise((resolve) => setTimeout(resolve, 500)); - expect(() => process.kill(pid, 0)).not.toThrow(); + // The agent's own log says why when it is gone before its delay is up. + expect(() => process.kill(pid, 0), JSON.stringify(log().slice(-5))).not.toThrow(); expect(existsSync(cwd)).toBe(true); // Let the fallback removal succeed once the agent is gone. chmodSync(cwd, 0o755); @@ -545,7 +546,7 @@ describe('acp', () => { // while the agent is still stopping (it exits 1500ms after SIGTERM). await new Promise((resolve) => setTimeout(resolve, 500)); expect(existsSync(cwd)).toBe(false); - expect(() => process.kill(pid, 0)).not.toThrow(); + expect(() => process.kill(pid, 0), JSON.stringify(log().slice(-5))).not.toThrow(); }); it('reports an agent that does not start', async () => { diff --git a/packages/acp/tests/fixtures/scripted-agent.ts b/packages/acp/tests/fixtures/scripted-agent.ts index 07fe3fc1c2..b7b605ef33 100644 --- a/packages/acp/tests/fixtures/scripted-agent.ts +++ b/packages/acp/tests/fixtures/scripted-agent.ts @@ -51,18 +51,52 @@ const script = JSON.parse(process.env['ACP_SCRIPT'] ?? '[]') as Move[][] | Recor const movesFor = (prompt: string, index: number): Move[] => Array.isArray(script) ? (script[index] ?? []) : (Object.entries(script).find(([key]) => prompt.includes(key))?.[1] ?? []); const log = (entry: unknown) => { - if (process.env['ACP_LOG'] !== undefined) appendFileSync(process.env['ACP_LOG'], `${JSON.stringify(entry)}\n`); + if (process.env['ACP_LOG'] === undefined) return; + try { + appendFileSync(process.env['ACP_LOG'], `${JSON.stringify(entry)}\n`); + } catch { + // The test removed its log directory; the agent may still be stopping. + } }; log({ pid: process.pid }); log({ env: Object.keys(process.env).filter((name) => name.toUpperCase().startsWith('E2E_')).toSorted(), codexConfig: process.env['CODEX_CONFIG'] ?? null }); -/** `ACP_EXIT_DELAY_MS` holds the agent in place after SIGTERM, so a test can watch the session directory while the child is still alive. */ +/** + * `ACP_EXIT_DELAY_MS` holds the agent in place after SIGTERM, so a test can + * watch the session directory while the child is still alive. Teardown closes + * stdin and the stdout pipe around the signal, and either can end a plain + * process first: stdin's EOF can empty the event loop before the signal + * handler runs, and a late write can hit EPIPE. Such an agent holds on through + * both once it is stopping, and logs any error, so a test that sees it gone + * early can say why; an error before that still ends it at once. The + * delay starts at SIGTERM or stdin's end, whichever comes first, so an agent + * whose test worker died without a teardown still exits. + */ const exitDelayMs = Number(process.env['ACP_EXIT_DELAY_MS'] ?? '0'); if (exitDelayMs > 0) { - process.on('SIGTERM', () => { - setTimeout(() => process.exit(0), exitDelayMs); - }); + const hold = setInterval(() => undefined, 60_000); + let stopping = false; + const stop = (reason: string) => { + log({ stopping: reason, at: Date.now() }); + if (stopping) return; + stopping = true; + setTimeout(() => { + clearInterval(hold); + process.exit(0); + }, exitDelayMs); + }; + process.on('SIGTERM', () => stop('SIGTERM')); + process.stdin.on('end', () => stop('stdin end')); + // Once stopping, an error is teardown's doing and the agent holds on; before that it is a real failure and exits at once. + const failed = (kind: string) => (error: unknown) => { + log({ [kind]: String(error), stopping }); + if (!stopping) process.exit(1); + }; + process.stdout.on('error', failed('stdoutError')); + process.on('uncaughtException', failed('uncaught')); + process.on('unhandledRejection', failed('unhandled')); + process.on('exit', (code) => log({ exit: code, at: Date.now() })); } let mcp: Client | undefined;