Skip to content

armgit / Orkas

Public Imported from GitHub
0

Orkas is an open-source (MIT), local-first AI team for your desktop: a Commander turns your goal into to-dos and coordinates 16 built-in specialist agents plus Claude Code, Codex, OpenCode, OpenClaw and Hermes, with projects, scheduled automations and connectors. Free app for macOS & Windows; built-in models or your own keys.

Preserve HTTP retry receipts and typed request deadlines

wyt33d472f4ff9f
diff --git a/src/main/util/retry.ts b/src/main/util/retry.ts
index 01a1ee14..34a3594f 100644
--- a/src/main/util/retry.ts
+++ b/src/main/util/retry.ts
@@ -31,14 +31,24 @@ export function setFetchImplementation(impl: FetchImplementation | null): void {
export class RetriableHttpStatusError extends Error {
status: number;
+ retryAfterMs?: number;
- constructor(status: number, label = 'request') {
+ constructor(status: number, label = 'request', retryAfter?: string | null) {
super(`${label} http ${status}`);
this.name = 'RetriableHttpStatusError';
this.status = status;
+ if (retryAfter) {
+ const seconds = Number(retryAfter);
+ const wait = Number.isFinite(seconds) ? seconds * 1000 : Date.parse(retryAfter) - Date.now();
+ if (Number.isFinite(wait) && wait >= 0) this.retryAfterMs = Math.ceil(wait);
+ }
}
}
+export class RequestTimeoutError extends Error {
+ constructor(message: string) { super(message); this.name = 'RequestTimeoutError'; }
+}
+
export function isRetriableHttpStatus(status: number): boolean {
return status === 408 || status === 429 || (status >= 500 && status < 600);
}
@@ -160,11 +170,11 @@ export async function fetchAndReadWithRetry<T>(
try {
const res = await (opts.fetch || fetchImplementation || fetch)(input, { ...init, signal: composed.signal });
if (isRetriableHttpStatus(res.status)) {
- throw new RetriableHttpStatusError(res.status, label);
+ throw new RetriableHttpStatusError(res.status, label, res.headers.get('retry-after'));
}
return { response: res, body: await readBody(res, composed.signal) };
} catch (err) {
- if (composed.timedOut()) throw new Error(timeoutMessage);
+ if (composed.timedOut()) throw new RequestTimeoutError(timeoutMessage);
throw err;
} finally {
composed.cleanup();
diff --git a/test/main/util/retry.test.ts b/test/main/util/retry.test.ts
index 1bdbd4d3..e4692fd1 100644
--- a/test/main/util/retry.test.ts
+++ b/test/main/util/retry.test.ts
@@ -1,5 +1,5 @@
import { afterEach, describe, expect, it, vi } from 'vitest';
-import { fetchWithRetry, retryAsync } from '../../../src/main/util/retry';
+import { fetchAndReadWithRetry, fetchWithRetry, RequestTimeoutError, RetriableHttpStatusError, retryAsync } from '../../../src/main/util/retry';
afterEach(() => {
vi.useRealTimers();
@@ -38,6 +38,43 @@ describe('retryAsync', () => {
});
describe('fetchWithRetry', () => {
+ it.each([
+ ['2', 2000],
+ ['Sat, 17 Oct 2026 12:00:03 GMT', 3000],
+ ['busy', undefined],
+ ['-1', undefined],
+ ])('retains a bounded caller receipt for Retry-After %s without replaying the request', async (header, wait) => {
+ vi.useFakeTimers();
+ vi.setSystemTime(new Date('2026-10-17T12:00:00Z'));
+ const fetchMock = vi.fn().mockResolvedValue(new Response('busy', {
+ status: 503, headers: { 'retry-after': header },
+ }));
+ const readBody = vi.fn();
+ const failure = await fetchAndReadWithRetry('test:receipt', 'https://example.test/api',
+ undefined, readBody, { retries: 0, fetch: fetchMock }).catch(error => error);
+ expect(failure).toBeInstanceOf(RetriableHttpStatusError);
+ expect(failure.status).toBe(503);
+ expect(failure.retryAfterMs).toBe(wait);
+ expect(fetchMock).toHaveBeenCalledOnce();
+ expect(readBody).not.toHaveBeenCalled();
+ expect(vi.getTimerCount()).toBe(0);
+ });
+
+ it('distinguishes a request deadline from transport failure without another attempt', async () => {
+ vi.useFakeTimers();
+ const fetchMock = vi.fn((_url, init?: RequestInit) => new Promise<Response>((_resolve, reject) => {
+ init?.signal?.addEventListener('abort', () => reject(new Error('aborted')), { once: true });
+ }));
+ const pending = fetchAndReadWithRetry('test:deadline', 'https://example.test/api', undefined,
+ async response => response.text(), { retries: 0, timeoutMs: 100, fetch: fetchMock }).catch(error => error);
+ await vi.advanceTimersByTimeAsync(100);
+ const failure = await pending;
+ expect(failure).toBeInstanceOf(RequestTimeoutError);
+ expect(failure.message).toBe('test:deadline timed out after 100ms');
+ expect(fetchMock).toHaveBeenCalledOnce();
+ expect(vi.getTimerCount()).toBe(0);
+ });
+
it.each(['request', 'options'])('cancels the %s signal during HTTP backoff without another attempt', async source => {
vi.useFakeTimers();
const controller = new AbortController();