Skip to content

Commit a272f17

Browse files
committed
fix(agents): preserve timeout classification before preparation
1 parent 7ace3c0 commit a272f17

3 files changed

Lines changed: 73 additions & 10 deletions

File tree

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,15 @@ describe("createEmbeddedAttemptExternalAbortController", () => {
212212
}
213213
});
214214

215-
it.each(["stage-start", "prep-cleanup"])("classifies cancellation at %s", async (checkpoint) => {
215+
it.each([
216+
["stage-start", false],
217+
["prep-cleanup", false],
218+
["stage-start", true],
219+
["prep-cleanup", true],
220+
] as const)("classifies abort at %s (timeout=%s)", async (checkpoint, timeout) => {
216221
const source = new AbortController();
217222
const reason = new Error("cancelled during setup");
223+
reason.name = timeout ? "TimeoutError" : "AbortError";
218224
source.abort(reason);
219225
const cleanupAfterEarlyAbort = vi.fn(async () => {});
220226
const state = createAbortState();
@@ -236,6 +242,12 @@ describe("createEmbeddedAttemptExternalAbortController", () => {
236242
expect(state.markAborted).toHaveBeenCalledTimes(1);
237243
expect(state.markExternalAbort).toHaveBeenCalledTimes(1);
238244
expect(state.setPromptError).toHaveBeenCalledWith(reason);
245+
expect(state.markTimedOut).toHaveBeenCalledTimes(timeout ? 1 : 0);
246+
controller.arm();
247+
expect(() => controller.throwIfFired()).toThrow(reason);
248+
expect(state.markExternalAbort).toHaveBeenCalledTimes(1);
249+
expect(state.markTimedOut).toHaveBeenCalledTimes(timeout ? 1 : 0);
250+
controller.dispose();
239251
});
240252
});
241253

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -482,12 +482,16 @@ export function createEmbeddedAttemptExternalAbortController(input: {
482482
let isCompactionPendingOrRetrying: (() => boolean) | undefined;
483483
let isCompactionInFlight: (() => boolean) | undefined;
484484
let removeListener: (() => void) | undefined;
485+
let abortHandled = false;
485486

486487
const onAbort = () => {
487488
const signal = input.abortSignal;
488-
if (!signal) {
489+
if (!signal || abortHandled) {
489490
return;
490491
}
492+
// Preparation checkpoints and the listener share classification and side effects.
493+
// Mark before handoff because aborting live work can synchronously re-enter.
494+
abortHandled = true;
491495
input.state.markExternalAbort();
492496
const reason = getAbortReason(signal);
493497
const isTimeout = reason ? isSignalTimeoutReason(reason) : false;
@@ -526,11 +530,8 @@ export function createEmbeddedAttemptExternalAbortController(input: {
526530
if (!signal?.aborted) {
527531
return undefined;
528532
}
529-
const abortError = createAttemptAbortError(signal);
530-
input.state.markAborted();
531-
input.state.markExternalAbort();
532-
input.state.setPromptError(abortError);
533-
return abortError;
533+
onAbort();
534+
return createAttemptAbortError(signal);
534535
};
535536

536537
return {

‎src/agents/embedded-agent-runner/run/attempt.tool-search-catalog-abort.test.ts‎

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
2+
import {
3+
onInternalDiagnosticEvent,
4+
type DiagnosticEventPayload,
5+
} from "../../../infra/diagnostic-events.js";
6+
import type { createOpenClawCodingTools } from "../../agent-tools.js";
27
import type { ToolSearchCatalogRef } from "../../tool-search.js";
38
import {
49
cleanupTempPaths,
@@ -58,27 +63,40 @@ describe("runEmbeddedAttempt tool-search catalog cleanup", () => {
5863
mode: "code-mode",
5964
tools: { codeMode: { enabled: true } },
6065
cancel: false,
66+
timeout: false,
6167
},
6268
{
6369
mode: "tool-search-tools",
6470
tools: { toolSearch: { enabled: true, mode: "tools" } },
6571
cancel: false,
72+
timeout: false,
6673
},
6774
{
6875
mode: "tool-search-directory",
6976
tools: { toolSearch: { enabled: true, mode: "directory" } },
7077
cancel: false,
78+
timeout: false,
7179
},
7280
{
7381
mode: "cancelled-code-mode",
7482
tools: { codeMode: { enabled: true } },
7583
cancel: true,
84+
timeout: false,
85+
},
86+
{
87+
mode: "timed-out-code-mode",
88+
tools: { codeMode: { enabled: true } },
89+
cancel: true,
90+
timeout: true,
7691
},
7792
] as const)(
7893
"clears the $mode run catalog when preparation fails or is cancelled",
79-
async ({ mode, tools, cancel }) => {
94+
async ({ mode, tools, cancel, timeout }) => {
8095
const runId = `run-catalog-diagnostics-${mode}`;
8196
const diagnosticsError = new Error(`failed ${mode} tool diagnostics`);
97+
if (timeout) {
98+
diagnosticsError.name = "TimeoutError";
99+
}
82100
const abortController = new AbortController();
83101
let catalogRef: ToolSearchCatalogRef | undefined;
84102
const logDiagnostics = vi.fn(() => {
@@ -92,7 +110,16 @@ describe("runEmbeddedAttempt tool-search catalog cleanup", () => {
92110
throw diagnosticsError;
93111
}
94112
});
95-
hoisted.createOpenClawCodingToolsMock.mockImplementation(() => catalogProbeTools());
113+
const cleanup = vi.fn(async (_reason: string) => {});
114+
hoisted.createOpenClawCodingToolsMock.mockImplementation((options) => {
115+
const toolOptions = options as NonNullable<Parameters<typeof createOpenClawCodingTools>[0]>;
116+
toolOptions.registerRunCleanup?.(cleanup);
117+
return catalogProbeTools();
118+
});
119+
const events: DiagnosticEventPayload[] = [];
120+
const unsubscribe = onInternalDiagnosticEvent((event) => events.push(event), {
121+
include: ["run.completed"],
122+
});
96123

97124
const attempt = createContextEngineAttemptRunner({
98125
contextEngine: createContextEngineBootstrapAndAssemble(),
@@ -112,7 +139,30 @@ describe("runEmbeddedAttempt tool-search catalog cleanup", () => {
112139
},
113140
});
114141

115-
await expect(attempt).rejects.toBe(diagnosticsError);
142+
try {
143+
if (timeout) {
144+
await expect(attempt).rejects.toMatchObject({
145+
cause: diagnosticsError,
146+
terminalOutcome: { status: "timeout" },
147+
});
148+
} else {
149+
await expect(attempt).rejects.toBe(diagnosticsError);
150+
}
151+
} finally {
152+
unsubscribe();
153+
}
154+
expect(cleanup).toHaveBeenCalledExactlyOnceWith(
155+
timeout ? "timeout" : cancel ? "cancel" : "completion",
156+
);
157+
if (timeout) {
158+
expect(events).toContainEqual(
159+
expect.objectContaining({
160+
type: "run.completed",
161+
runId,
162+
outcome: "aborted",
163+
}),
164+
);
165+
}
116166
expect(logDiagnostics).toHaveBeenCalledOnce();
117167
expect(catalogRef).toBeDefined();
118168
expect(catalogRef?.current).toBeUndefined();

0 commit comments

Comments
 (0)