Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/acp/tests/executor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 () => {
Expand Down
44 changes: 39 additions & 5 deletions packages/acp/tests/fixtures/scripted-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading