From b11c1db2771189f1a4598c81a433c2d69c540326 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Kapu=C5=9Bciak?= Date: Mon, 9 Nov 2026 16:22:50 +0100 Subject: [PATCH 1/2] test(acp): keep the delayed fake agent alive until its exit delay is up The session-removal tests need an agent that takes 1.5 s to stop after SIGTERM. Teardown closes stdin and stdout around the signal, and on Linux CI the agent was sometimes gone within 500 ms. The fixture now holds the delayed agent until the delay is up, starts the delay at SIGTERM or stdin's end, logs errors instead of dying, and the tests print the agent's log when it is gone early. --- packages/acp/tests/executor.test.ts | 5 ++- packages/acp/tests/fixtures/scripted-agent.ts | 38 ++++++++++++++++--- 2 files changed, 36 insertions(+), 7 deletions(-) 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..3571ba8f24 100644 --- a/packages/acp/tests/fixtures/scripted-agent.ts +++ b/packages/acp/tests/fixtures/scripted-agent.ts @@ -51,18 +51,46 @@ 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 and logs any error, so a test that sees it gone early can say why. 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')); + process.stdout.on('error', (error) => log({ stdoutError: String(error) })); + process.on('uncaughtException', (error) => log({ uncaught: String(error) })); + process.on('unhandledRejection', (error) => log({ unhandled: String(error) })); + process.on('exit', (code) => log({ exit: code, at: Date.now() })); } let mcp: Client | undefined; From 55944e2c460845e103bbb4cc6632b6e23d5c1107 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Kapu=C5=9Bciak?= Date: Mon, 9 Nov 2026 16:26:31 +0100 Subject: [PATCH 2/2] test(acp): end the delayed fake agent on an error before it is stopping --- packages/acp/tests/fixtures/scripted-agent.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/acp/tests/fixtures/scripted-agent.ts b/packages/acp/tests/fixtures/scripted-agent.ts index 3571ba8f24..b7b605ef33 100644 --- a/packages/acp/tests/fixtures/scripted-agent.ts +++ b/packages/acp/tests/fixtures/scripted-agent.ts @@ -68,7 +68,8 @@ log({ env: Object.keys(process.env).filter((name) => name.toUpperCase().startsWi * 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 and logs any error, so a test that sees it gone early can say why. The + * 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. */ @@ -87,9 +88,14 @@ if (exitDelayMs > 0) { }; process.on('SIGTERM', () => stop('SIGTERM')); process.stdin.on('end', () => stop('stdin end')); - process.stdout.on('error', (error) => log({ stdoutError: String(error) })); - process.on('uncaughtException', (error) => log({ uncaught: String(error) })); - process.on('unhandledRejection', (error) => log({ unhandled: String(error) })); + // 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() })); }