Skip to content

Commit 1524e96

Browse files
lidge-junclaude
andcommitted
refactor(adapters): consolidate abort/sleep helpers into upstream-retry
google-http and cursor transport-retry carried byte-identical copies of abortError/sleepWithAbort; both now import the canonical helpers from the upstream-retry leaf module. abortAwareSleep stays as a compat re-export. Behavior-neutral; retry delay calculators stay local (different constants). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 00d9439 commit 1524e96

2 files changed

Lines changed: 6 additions & 42 deletions

File tree

src/adapters/cursor/transport-retry.ts

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import type { CursorRunRequest, CursorServerMessage } from "./types";
22
import type { CursorTransport, CursorTransportFactory, CursorTransportFactoryInput } from "./transport";
3+
import { abortError, sleepWithAbort } from "../../upstream-retry";
4+
5+
// Compat: historical name for the shared abortable sleep, kept for external callers.
6+
export { sleepWithAbort as abortAwareSleep } from "../../upstream-retry";
37

48
export const CURSOR_RETRY_ATTEMPTS = 3;
59
export const CURSOR_RETRY_BASE_MS = 250;
@@ -36,31 +40,6 @@ export function cursorRetryDelayMs(attempt: number): number {
3640
return Math.floor(exp * (0.8 + Math.random() * 0.4));
3741
}
3842

39-
function abortError(signal?: AbortSignal): unknown {
40-
return signal?.reason ?? new DOMException("The operation was aborted", "AbortError");
41-
}
42-
43-
export async function abortAwareSleep(ms: number, signal?: AbortSignal): Promise<void> {
44-
if (ms <= 0) return;
45-
if (signal?.aborted) throw abortError(signal);
46-
await new Promise<void>((resolve, reject) => {
47-
let timer: ReturnType<typeof setTimeout>;
48-
const cleanup = () => {
49-
clearTimeout(timer);
50-
signal?.removeEventListener("abort", onAbort);
51-
};
52-
const onAbort = () => {
53-
cleanup();
54-
reject(abortError(signal));
55-
};
56-
timer = setTimeout(() => {
57-
cleanup();
58-
resolve();
59-
}, ms);
60-
signal?.addEventListener("abort", onAbort, { once: true });
61-
});
62-
}
63-
6443
/**
6544
* A transport is safe to retry only if it explicitly reports the run request was never committed.
6645
* A transport without `requestCommitted` is treated as committed (not retryable) — fail safe.
@@ -105,7 +84,7 @@ export async function runCursorTurnWithRetry(
10584
requestUncommitted(transport) &&
10685
isRetryableCursorError(err);
10786
if (!canRetry) throw err;
108-
await abortAwareSleep(cursorRetryDelayMs(attempt), signal);
87+
await sleepWithAbort(cursorRetryDelayMs(attempt), signal);
10988
} finally {
11089
await transport.close?.();
11190
}

src/adapters/google-http.ts

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { AdapterFetchContext, AdapterRequest } from "./base";
22
import { isQuotaExhaustedBody, retryableGoogleStatus, safeGoogleHttpErrorMessage } from "./google-errors";
3+
import { abortError, sleepWithAbort } from "../upstream-retry";
34

45
const GOOGLE_RETRY_ATTEMPTS = 3;
56
const GOOGLE_RETRY_BASE_MS = 250;
@@ -22,22 +23,6 @@ function retryDelayMs(attempt: number, headers?: Headers): number {
2223
return Math.floor(exp * (0.8 + Math.random() * 0.4));
2324
}
2425

25-
function abortError(signal?: AbortSignal): unknown {
26-
return signal?.reason ?? new DOMException("The operation was aborted", "AbortError");
27-
}
28-
29-
async function sleepWithAbort(ms: number, signal?: AbortSignal): Promise<void> {
30-
if (ms <= 0) return;
31-
if (signal?.aborted) throw abortError(signal);
32-
await new Promise<void>((resolve, reject) => {
33-
let timer: ReturnType<typeof setTimeout>;
34-
const cleanup = () => { clearTimeout(timer); signal?.removeEventListener("abort", onAbort); };
35-
const onAbort = () => { cleanup(); reject(abortError(signal)); };
36-
timer = setTimeout(() => { cleanup(); resolve(); }, ms);
37-
signal?.addEventListener("abort", onAbort, { once: true });
38-
});
39-
}
40-
4126
function signalWithAttemptTimeout(parent: AbortSignal | undefined, timeoutMs: number): AbortSignal {
4227
const timeout = AbortSignal.timeout(timeoutMs);
4328
return parent ? AbortSignal.any([parent, timeout]) : timeout;

0 commit comments

Comments
 (0)