Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 5 additions & 0 deletions .changeset/acp-teardown-eperm.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@e2e-dev/acp": patch
---

Fix `WORKER_CRASH (EPERM)` on Windows when an ACP session tears down: the session directory is now removed only after the agent process exits, with retries for transient locks, and a failed removal never throws.
1 change: 1 addition & 0 deletions docs/reference/config.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,7 @@ command: {
| `reuseExisting` | `true` | `false` |

- `executable` is resolved with `PATH` and never shell-interpreted.
- On Windows, `executable` must name a real executable file: `npm` fails with `spawn npm ENOENT` because npm is `npm.cmd`. Use `node` with the tool's JavaScript entry point instead, for example `node_modules/vite/bin/vite.js`.
- `args` pass verbatim. `{port}` in `args` and `env` values expands to the
port of `url`.
- `cwd` resolves from the project root.
Expand Down
20 changes: 19 additions & 1 deletion packages/acp/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,25 @@ export async function startSession(
if (child.exitCode === null && child.signalCode === null) child.kill('SIGKILL');
}, 3_000).unref();
void serving.then((started) => started.close(), () => undefined);
rmSync(cwd, { recursive: true, force: true });
// Windows cannot remove a directory while a process still has it as its
// working directory, so the removal waits for the child to exit; a
// leftover temp directory is harmless, so a failed removal never throws.
let removed = false;
const removeCwd = () => {
if (removed) return;
removed = true;
try {
rmSync(cwd, { recursive: true, force: true, maxRetries: 10, retryDelay: 200 });
} catch {
// A leftover temp directory is harmless.
}
};
if (child.exitCode !== null || child.signalCode !== null) removeCwd();
else {
child.once('exit', removeCwd);
// A child that never started may emit 'close' without 'exit'.
child.once('close', removeCwd);
}
};

let initialize: InitializeResponse;
Expand Down
39 changes: 38 additions & 1 deletion packages/acp/tests/executor.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mkdtempSync, readFileSync, rmSync } from 'node:fs';
import { existsSync, mkdtempSync, readFileSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join, relative } from 'node:path';
import { fileURLToPath } from 'node:url';
Expand Down Expand Up @@ -489,6 +489,43 @@ describe('acp', () => {
await expect(run).rejects.toMatchObject({ code: 'STEP_TIMEOUT' });
});

it('removes the session directory only after the agent exits', async () => {
const { executor, log } = scripted([[pass]], { env: { ACP_EXIT_DELAY_MS: '1500' } });
const step = context();
cleanups.push(step.end);
await expect(executor.runStep(step.ctx)).resolves.toEqual({ status: 'passed', summary: 'added' });
const pid = log().find((entry) => 'pid' in entry)?.['pid'] as number;
const session = log().find((entry) => 'session' in entry)?.['session'] as { cwd: string };
const cwd = session.cwd;
step.end();
// The agent is still stopping: the directory must still be there. On
// Windows removing a directory a live process holds as its working
// directory throws EPERM and takes the worker down, so the old teardown,
// which removed it at once, failed the test this way.
expect(() => process.kill(pid, 0)).not.toThrow();
expect(existsSync(cwd)).toBe(true);
await eventually(() => {
expect(() => process.kill(pid, 0)).toThrow();
expect(existsSync(cwd)).toBe(false);
});
});

it('removes the session directory at once when the agent already exited', async () => {
const { executor, log } = scripted([[pass]]);
const step = context();
cleanups.push(step.end);
await expect(executor.runStep(step.ctx)).resolves.toEqual({ status: 'passed', summary: 'added' });
const pid = log().find((entry) => 'pid' in entry)?.['pid'] as number;
const session = log().find((entry) => 'session' in entry)?.['session'] as { cwd: string };
const cwd = session.cwd;
process.kill(pid, 'SIGKILL');
await eventually(() => expect(() => process.kill(pid, 0)).toThrow());
// Let the exit event land, so the already-exited branch is the one taken.
await new Promise((resolve) => setTimeout(resolve, 50));
step.end();
await eventually(() => expect(existsSync(cwd)).toBe(false));
});

it('reports an agent that does not start', async () => {
const executor = acp({ command: join(tmpdir(), 'no-such-acp-agent') });
const step = context();
Expand Down
8 changes: 8 additions & 0 deletions packages/acp/tests/fixtures/scripted-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ const log = (entry: unknown) => {
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. */
const exitDelayMs = Number(process.env['ACP_EXIT_DELAY_MS'] ?? '0');
if (exitDelayMs > 0) {
process.on('SIGTERM', () => {
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
setTimeout(() => process.exit(0), exitDelayMs);
});
}

let mcp: Client | undefined;
/** The screen lines the agent read this turn, newest first: each tool result's changes, then the prompt's screen. */
let screen = '';
Expand Down
Loading