diff --git a/.changeset/acp-cancel-opening-screen.md b/.changeset/acp-cancel-opening-screen.md new file mode 100644 index 000000000..5f9103d57 --- /dev/null +++ b/.changeset/acp-cancel-opening-screen.md @@ -0,0 +1,5 @@ +--- +'@e2e-dev/acp': patch +--- + +A cancelled ACP step now stops right away instead of waiting for the screen read to finish. diff --git a/packages/acp/src/executor.ts b/packages/acp/src/executor.ts index e9461e893..20cb76b8d 100644 --- a/packages/acp/src/executor.ts +++ b/packages/acp/src/executor.ts @@ -72,8 +72,9 @@ function executor(name: string, options: AcpAgentOptions, launch: () => Promise< cache: 'inherit', async runStep(ctx) { const { held, session } = await ready(ctx, launch, memoryKey); + ctx.signal.throwIfAborted(); const active = activeStep(ctx); - const text = await stepMessage(active, held.tools, held.introduced ? undefined : [RULES, options.system, ctx.agentContext]); + const text = await untilAborted(stepMessage(active, held.tools, held.introduced ? undefined : [RULES, options.system, ctx.agentContext]), ctx.signal); // A step that ended while its screen was read sends no prompt, which no cancel would reach. ctx.signal.throwIfAborted(); held.introduced = true; diff --git a/packages/acp/tests/executor.test.ts b/packages/acp/tests/executor.test.ts index 50cd08ed9..c6123e186 100644 --- a/packages/acp/tests/executor.test.ts +++ b/packages/acp/tests/executor.test.ts @@ -355,6 +355,49 @@ describe('acp', () => { expect(log().filter((entry) => 'prompt' in entry)).toHaveLength(1); }); + it('does not read the screen for an already-aborted step', async () => { + const { executor } = scripted([[pass]]); + const step = context(); + cleanups.push(step.end); + const reason = new AgentError('STEP_TIMEOUT', 'the step ended before it started'); + step.abortStep(reason); + await expect(executor.runStep(step.ctx)).rejects.toBe(reason); + expect(step.ctx.observe).not.toHaveBeenCalled(); + }); + + it('ends a cancelled step without waiting for its opening screen', async () => { + const { executor, log } = scripted([[pass], [pass]]); + const first = context(); + cleanups.push(first.end); + await executor.runStep(first.ctx); + const second = context({ attempt: { attempt: first.ctx.attempt as never, end: first.end } }); + const observe = vi.mocked(second.ctx.observe); + const look = observe.getMockImplementation()!; + let release: () => void = () => undefined; + const heldScreen = new Promise((resolve) => { release = resolve; }); + observe.mockImplementationOnce(async (request) => { + await heldScreen; + return look(request); + }); + const reason = new AgentError('STEP_TIMEOUT', 'the step timed out while reading its screen'); + const rejected = vi.fn(); + const run = executor.runStep(second.ctx); + void run.catch(rejected); + try { + await eventually(() => expect(observe).toHaveBeenCalled()); + second.abortStep(reason); + await vi.waitFor(() => expect(rejected).toHaveBeenCalledWith(reason), { timeout: 1_000 }); + expect(log().filter((entry) => 'prompt' in entry)).toHaveLength(1); + const third = context({ attempt: { attempt: first.ctx.attempt as never, end: first.end } }); + await expect(executor.runStep(third.ctx)).resolves.toMatchObject({ status: 'passed' }); + expect(log().filter((entry) => 'session' in entry)).toHaveLength(1); + } finally { + release(); + await run.catch(() => undefined); + } + expect(log().filter((entry) => 'prompt' in entry)).toHaveLength(2); + }); + it('gives up on a session whose agent never stops the turn of a step that ended', async () => { const { executor, log } = scripted([[{ ignoreCancel: true }], [pass]]); const first = context();