From ca561dce46174b3e6a53e032580b709ff82297e0 Mon Sep 17 00:00:00 2001 From: Jonatas Soares Date: Fri, 13 Nov 2026 20:18:02 -0300 Subject: [PATCH 1/3] fix(acp): stop cancelled steps while reading the opening screen --- .changeset/acp-cancel-opening-screen.md | 5 ++++ packages/acp/src/executor.ts | 2 +- packages/acp/tests/executor.test.ts | 33 +++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 .changeset/acp-cancel-opening-screen.md diff --git a/.changeset/acp-cancel-opening-screen.md b/.changeset/acp-cancel-opening-screen.md new file mode 100644 index 0000000000..2180e756c9 --- /dev/null +++ b/.changeset/acp-cancel-opening-screen.md @@ -0,0 +1,5 @@ +--- +'@e2e-dev/acp': patch +--- + +Stop a cancelled ACP step promptly while it is reading its opening screen, preserving the cancellation reason without waiting for the read to finish. diff --git a/packages/acp/src/executor.ts b/packages/acp/src/executor.ts index 92c626d644..12d2374584 100644 --- a/packages/acp/src/executor.ts +++ b/packages/acp/src/executor.ts @@ -73,7 +73,7 @@ function executor(name: string, options: AcpAgentOptions, launch: () => Promise< async runStep(ctx) { const { held, session } = await ready(ctx, launch, memoryKey); 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 b10f5de767..41f3067d3a 100644 --- a/packages/acp/tests/executor.test.ts +++ b/packages/acp/tests/executor.test.ts @@ -354,6 +354,39 @@ describe('acp', () => { expect(log().filter((entry) => 'prompt' in entry)).toHaveLength(1); }); + 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(); From 074ab53172c4d55e867f12169776930adda9a1c4 Mon Sep 17 00:00:00 2001 From: Jonatas Soares Date: Fri, 13 Nov 2026 20:35:19 -0300 Subject: [PATCH 2/3] fix(acp): check cancellation before starting the opening screen --- packages/acp/src/executor.ts | 1 + packages/acp/tests/executor.test.ts | 30 +++++++++++++++++++ packages/acp/tests/fixtures/scripted-agent.ts | 5 ++-- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/packages/acp/src/executor.ts b/packages/acp/src/executor.ts index 12d2374584..ae5b8fbccd 100644 --- a/packages/acp/src/executor.ts +++ b/packages/acp/src/executor.ts @@ -72,6 +72,7 @@ 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 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. diff --git a/packages/acp/tests/executor.test.ts b/packages/acp/tests/executor.test.ts index 41f3067d3a..7905193396 100644 --- a/packages/acp/tests/executor.test.ts +++ b/packages/acp/tests/executor.test.ts @@ -2,6 +2,7 @@ import { chmodSync, existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync import { tmpdir } from 'node:os'; import { join, relative } from 'node:path'; import { fileURLToPath } from 'node:url'; +import { setImmediate } from 'node:timers/promises'; import { AgentError, type ExecutorActions, type ExecutorModelCall, type StepExecutorContext } from 'e2e'; import { afterEach, describe, expect, it, vi } from 'vitest'; import { acp } from '../src/index.ts'; @@ -354,6 +355,35 @@ describe('acp', () => { expect(log().filter((entry) => 'prompt' in entry)).toHaveLength(1); }); + it('does not read a cancelled step screen after the previous turn stops', async () => { + const directory = mkdtempSync(join(tmpdir(), 'e2e-acp-turn-')); + cleanups.push(() => rmSync(directory, { recursive: true, force: true })); + const release = join(directory, 'release'); + const { executor, log } = scripted([[{ hang: true, cancelUntilFile: release }]]); + const first = context(); + cleanups.push(first.end); + const run = executor.runStep(first.ctx); + await eventually(() => expect(log().filter((entry) => 'prompt' in entry)).toHaveLength(1)); + first.abortStep(new AgentError('STEP_TIMEOUT', 'the first step timed out')); + await expect(run).rejects.toMatchObject({ code: 'STEP_TIMEOUT' }); + const second = context({ attempt: { attempt: first.ctx.attempt as never, end: first.end } }); + const observe = vi.mocked(second.ctx.observe); + const look = observe.getMockImplementation()!; + observe.mockImplementation(async (request) => { + second.ctx.signal.throwIfAborted(); + return look(request); + }); + const next = executor.runStep(second.ctx); + await eventually(() => expect(log()).toContainEqual({ cancelled: true })); + await setImmediate(); + const reason = new AgentError('STEP_TIMEOUT', 'the second step timed out while waiting for the first turn'); + second.abortStep(reason); + writeFileSync(release, ''); + await expect(next).rejects.toBe(reason); + expect(observe).not.toHaveBeenCalled(); + expect(log().filter((entry) => 'prompt' in entry)).toHaveLength(1); + }); + it('ends a cancelled step without waiting for its opening screen', async () => { const { executor, log } = scripted([[pass], [pass]]); const first = context(); diff --git a/packages/acp/tests/fixtures/scripted-agent.ts b/packages/acp/tests/fixtures/scripted-agent.ts index 505eeacbf1..2c4990cdef 100644 --- a/packages/acp/tests/fixtures/scripted-agent.ts +++ b/packages/acp/tests/fixtures/scripted-agent.ts @@ -10,7 +10,7 @@ * answers `initialize`. */ -import { appendFileSync } from 'node:fs'; +import { appendFileSync, existsSync } from 'node:fs'; import { Readable, Writable } from 'node:stream'; import { agent, @@ -43,7 +43,7 @@ type Move = | { readonly say: string } | { readonly fail: string } /** `thenRan` runs a tool of the agent's own without asking once the turn is cancelled, before the turn ends. */ - | { readonly hang: true; readonly thenRan?: string } + | { readonly hang: true; readonly thenRan?: string; readonly cancelUntilFile?: string } | { readonly ignoreCancel: true }; /** Moves per turn in order, or per step: the moves of the first key the prompt contains. */ @@ -217,6 +217,7 @@ agent() cancelled = resolve; }); } + while (move.cancelUntilFile !== undefined && !existsSync(move.cancelUntilFile)) await new Promise((resolve) => setTimeout(resolve, 10)); if (move.thenRan !== undefined) { // Logged first: the executor stops this agent as soon as it reports the tool. log({ ran: move.thenRan }); From f96f9ea37107ac7dd18374250eea9fd0bf0f1106 Mon Sep 17 00:00:00 2001 From: Jonatas Soares Date: Sun, 15 Nov 2026 16:13:53 -0300 Subject: [PATCH 3/3] test(acp): drop the duplicate previous-turn cancellation test Main now carries the previous-turn cancellation from #1761, so the test that waited on the first turn and the scripted agent's cancelUntilFile option duplicate its coverage. Cover the pre-check with a step aborted before runStep instead, keep one setImmediate import, and word the changeset for users. --- .changeset/acp-cancel-opening-screen.md | 2 +- packages/acp/tests/executor.test.ts | 36 +++++-------------- packages/acp/tests/fixtures/scripted-agent.ts | 5 ++- 3 files changed, 11 insertions(+), 32 deletions(-) diff --git a/.changeset/acp-cancel-opening-screen.md b/.changeset/acp-cancel-opening-screen.md index 2180e756c9..5f9103d57f 100644 --- a/.changeset/acp-cancel-opening-screen.md +++ b/.changeset/acp-cancel-opening-screen.md @@ -2,4 +2,4 @@ '@e2e-dev/acp': patch --- -Stop a cancelled ACP step promptly while it is reading its opening screen, preserving the cancellation reason without waiting for the read to finish. +A cancelled ACP step now stops right away instead of waiting for the screen read to finish. diff --git a/packages/acp/tests/executor.test.ts b/packages/acp/tests/executor.test.ts index f464e9712a..c6123e1866 100644 --- a/packages/acp/tests/executor.test.ts +++ b/packages/acp/tests/executor.test.ts @@ -3,7 +3,6 @@ import { tmpdir } from 'node:os'; import { join, relative } from 'node:path'; import { setImmediate } from 'node:timers/promises'; import { fileURLToPath } from 'node:url'; -import { setImmediate } from 'node:timers/promises'; import { AgentError, type ExecutorActions, type ExecutorModelCall, type StepExecutorContext } from 'e2e'; import { afterEach, describe, expect, it, vi } from 'vitest'; import { acp } from '../src/index.ts'; @@ -356,33 +355,14 @@ describe('acp', () => { expect(log().filter((entry) => 'prompt' in entry)).toHaveLength(1); }); - it('does not read a cancelled step screen after the previous turn stops', async () => { - const directory = mkdtempSync(join(tmpdir(), 'e2e-acp-turn-')); - cleanups.push(() => rmSync(directory, { recursive: true, force: true })); - const release = join(directory, 'release'); - const { executor, log } = scripted([[{ hang: true, cancelUntilFile: release }]]); - const first = context(); - cleanups.push(first.end); - const run = executor.runStep(first.ctx); - await eventually(() => expect(log().filter((entry) => 'prompt' in entry)).toHaveLength(1)); - first.abortStep(new AgentError('STEP_TIMEOUT', 'the first step timed out')); - await expect(run).rejects.toMatchObject({ code: 'STEP_TIMEOUT' }); - const second = context({ attempt: { attempt: first.ctx.attempt as never, end: first.end } }); - const observe = vi.mocked(second.ctx.observe); - const look = observe.getMockImplementation()!; - observe.mockImplementation(async (request) => { - second.ctx.signal.throwIfAborted(); - return look(request); - }); - const next = executor.runStep(second.ctx); - await eventually(() => expect(log()).toContainEqual({ cancelled: true })); - await setImmediate(); - const reason = new AgentError('STEP_TIMEOUT', 'the second step timed out while waiting for the first turn'); - second.abortStep(reason); - writeFileSync(release, ''); - await expect(next).rejects.toBe(reason); - expect(observe).not.toHaveBeenCalled(); - 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 () => { diff --git a/packages/acp/tests/fixtures/scripted-agent.ts b/packages/acp/tests/fixtures/scripted-agent.ts index 2c4990cdef..505eeacbf1 100644 --- a/packages/acp/tests/fixtures/scripted-agent.ts +++ b/packages/acp/tests/fixtures/scripted-agent.ts @@ -10,7 +10,7 @@ * answers `initialize`. */ -import { appendFileSync, existsSync } from 'node:fs'; +import { appendFileSync } from 'node:fs'; import { Readable, Writable } from 'node:stream'; import { agent, @@ -43,7 +43,7 @@ type Move = | { readonly say: string } | { readonly fail: string } /** `thenRan` runs a tool of the agent's own without asking once the turn is cancelled, before the turn ends. */ - | { readonly hang: true; readonly thenRan?: string; readonly cancelUntilFile?: string } + | { readonly hang: true; readonly thenRan?: string } | { readonly ignoreCancel: true }; /** Moves per turn in order, or per step: the moves of the first key the prompt contains. */ @@ -217,7 +217,6 @@ agent() cancelled = resolve; }); } - while (move.cancelUntilFile !== undefined && !existsSync(move.cancelUntilFile)) await new Promise((resolve) => setTimeout(resolve, 10)); if (move.thenRan !== undefined) { // Logged first: the executor stops this agent as soon as it reports the tool. log({ ran: move.thenRan });