diff --git a/cli/src/agent/backends/acp/AcpStdioTransport.test.ts b/cli/src/agent/backends/acp/AcpStdioTransport.test.ts index 86e3c2f3..df31d283 100644 --- a/cli/src/agent/backends/acp/AcpStdioTransport.test.ts +++ b/cli/src/agent/backends/acp/AcpStdioTransport.test.ts @@ -19,6 +19,7 @@ const spawnState = vi.hoisted(() => ({ exitHandlers: [] as Array<(code: number | null, signal: NodeJS.Signals | null) => void>, closeHandlers: [] as Array<(code: number | null, signal: NodeJS.Signals | null) => void>, stdoutDataHandlers: [] as Array<(chunk: string) => void>, + stdinErrorHandlers: [] as Array<(error: Error) => void>, stdinEnd: vi.fn(), stdinWrite: vi.fn<(chunk: string) => boolean>(() => true), kill: vi.fn(), @@ -49,6 +50,7 @@ vi.mock('node:child_process', () => ({ spawnState.exitHandlers = []; spawnState.closeHandlers = []; spawnState.stdoutDataHandlers = []; + spawnState.stdinErrorHandlers = []; const handlers = new Map void>>(); const proc = { get pid() { @@ -73,6 +75,9 @@ vi.mock('node:child_process', () => ({ }) }, stdin: { + on: vi.fn((event: string, handler: (error: Error) => void) => { + if (event === 'error') spawnState.stdinErrorHandlers.push(handler); + }), end: (...args: unknown[]) => spawnState.stdinEnd(...args), write: (chunk: string) => spawnState.stdinWrite(chunk) }, @@ -281,6 +286,18 @@ describe('AcpStdioTransport plain-text stdout', () => { }); }); +describe('AcpStdioTransport stdin errors', () => { + test('rejects pending requests when stdin emits EPIPE', async () => { + const transport = await AcpStdioTransport.create({ command: 'test-agent' }); + const pending = transport.sendRequest('initialize', {}); + const failure = Object.assign(new Error('broken pipe'), { code: 'EPIPE' }); + expect(spawnState.stdinErrorHandlers).toHaveLength(1); + spawnState.stdinErrorHandlers[0](failure); + await expect(pending).rejects.toMatchObject({ code: 'EPIPE' }); + await expect(transport.sendRequest('initialize', {})).rejects.toThrow(); + }); +}); + describe('AcpStdioTransport closed stdin writes', () => { afterEach(() => { spawnState.stdinWrite.mockReset(); diff --git a/cli/src/agent/backends/acp/AcpStdioTransport.ts b/cli/src/agent/backends/acp/AcpStdioTransport.ts index 5e728692..76819b6c 100644 --- a/cli/src/agent/backends/acp/AcpStdioTransport.ts +++ b/cli/src/agent/backends/acp/AcpStdioTransport.ts @@ -158,6 +158,12 @@ export class AcpStdioTransport { logger.debug('[ACP] agent CLI guard armed', describeAgentAcpGuardState(childPid)); } + // A child can exit between the liveness check and a write. Node emits + // stream errors separately from the write callback; handle both paths. + this.process.stdin.on('error', (error) => { + this.markClosed(markAcpIndeterminate(error)); + }); + this.process.stdout.setEncoding('utf8'); this.process.stdout.on('data', (chunk) => this.handleStdout(chunk));