Skip to content

Commit c8ff43d

Browse files
committed
fix(agents): process external attempt abort once
1 parent 28be255 commit c8ff43d

2 files changed

Lines changed: 72 additions & 2 deletions

File tree

‎src/agents/embedded-agent-runner/run/attempt-stream-prepare.test.ts‎

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
import { beforeEach, describe, expect, it, vi } from "vitest";
2+
import {
3+
createReplyOperation,
4+
type ReplyOperation,
5+
} from "../../../auto-reply/reply/reply-run-registry.js";
6+
import { isAgentRunRestartAbortReason } from "../../run-termination.js";
27
import {
38
projectToolSearchTargetTranscriptMessages,
49
type ToolSearchTargetTranscriptProjection,
@@ -45,6 +50,10 @@ function prepareCatalogExecutor(
4550
runAbortController?: AbortController;
4651
sandboxSessionKey?: string;
4752
sessionKey?: string;
53+
replyOperation?: ReplyOperation;
54+
onAttemptAbort?: () => void;
55+
abortRun?: (isTimeout?: boolean, reason?: unknown) => void;
56+
markExternalAbort?: () => void;
4857
},
4958
) {
5059
const runAbortController = options?.runAbortController ?? new AbortController();
@@ -53,6 +62,8 @@ function prepareCatalogExecutor(
5362
runId: "run-output-schema",
5463
sessionId: "session-output-schema",
5564
sessionKey: options?.sessionKey ?? "agent:main:main",
65+
replyOperation: options?.replyOperation,
66+
onAttemptAbort: options?.onAttemptAbort,
5667
} as never,
5768
activeSession: { agent: {}, isStreaming: false } as never,
5869
hookRunner: undefined as never,
@@ -62,8 +73,8 @@ function prepareCatalogExecutor(
6273
toolSearchTargetTranscriptProjections: projections,
6374
isReplaySafeTool: () => false,
6475
runAbortController,
65-
abortRun: vi.fn(),
66-
markExternalAbort: vi.fn(),
76+
abortRun: options?.abortRun ?? vi.fn(),
77+
markExternalAbort: options?.markExternalAbort ?? vi.fn(),
6778
getRunState:
6879
options?.getRunState ??
6980
(() => ({
@@ -443,4 +454,56 @@ describe("prepareEmbeddedAttemptStream", () => {
443454
aborted = true;
444455
expect(prepared.queueHandle.isAborted?.()).toBe(true);
445456
});
457+
458+
it("processes aliased cancel and abort through one external-abort sequence", () => {
459+
const markExternalAbort = vi.fn();
460+
const onAttemptAbort = vi.fn();
461+
const abortRun = vi.fn();
462+
const prepared = prepareCatalogExecutor([], {
463+
markExternalAbort,
464+
onAttemptAbort,
465+
abortRun,
466+
});
467+
468+
prepared.queueHandle.abort("restart");
469+
prepared.queueHandle.cancel("user_abort");
470+
471+
expect(markExternalAbort).toHaveBeenCalledOnce();
472+
expect(onAttemptAbort).toHaveBeenCalledOnce();
473+
expect(abortRun).toHaveBeenCalledOnce();
474+
expect(abortRun.mock.calls[0]?.[0]).toBe(false);
475+
expect(isAgentRunRestartAbortReason(abortRun.mock.calls[0]?.[1])).toBe(true);
476+
});
477+
478+
it("latches external abort before reply cancellation can synchronously re-enter", () => {
479+
const operation = createReplyOperation({
480+
sessionKey: "agent:main:main",
481+
sessionId: "session-output-schema",
482+
resetTriggered: false,
483+
});
484+
const markExternalAbort = vi.fn();
485+
const abortRun = vi.fn();
486+
const onAttemptAbort = vi.fn(() => {
487+
operation.abortByUser();
488+
});
489+
490+
try {
491+
const prepared = prepareCatalogExecutor([], {
492+
replyOperation: operation,
493+
markExternalAbort,
494+
onAttemptAbort,
495+
abortRun,
496+
});
497+
498+
prepared.queueHandle.cancel("superseded");
499+
prepared.queueHandle.abort("restart");
500+
501+
expect(markExternalAbort).toHaveBeenCalledOnce();
502+
expect(onAttemptAbort).toHaveBeenCalledOnce();
503+
expect(abortRun).toHaveBeenCalledOnce();
504+
expect(abortRun).toHaveBeenCalledWith(false, undefined);
505+
} finally {
506+
operation.complete();
507+
}
508+
});
446509
});

‎src/agents/embedded-agent-runner/run/attempt-stream-prepare.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,14 @@ export function prepareEmbeddedAttemptStream(input: {
383383
}
384384
};
385385

386+
let externalAbortAccepted = false;
386387
const abortActiveRunExternally = (reason?: "user_abort" | "restart" | "superseded") => {
388+
// Reply cancellation can synchronously re-enter through this same backend.
389+
// Latch before callbacks so the first reason owns every abort side effect.
390+
if (externalAbortAccepted) {
391+
return;
392+
}
393+
externalAbortAccepted = true;
387394
input.markExternalAbort();
388395
attempt.onAttemptAbort?.();
389396
input.abortRun(false, reason === "restart" ? createAgentRunRestartAbortError() : undefined);

0 commit comments

Comments
 (0)