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: 5 additions & 0 deletions .changeset/acp-cancel-opening-screen.md
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 2 additions & 1 deletion packages/acp/src/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
43 changes: 43 additions & 0 deletions packages/acp/tests/executor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>((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();
Expand Down
Loading