Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix: retry Bun DNS timeout embedding errors (#4718)
Jack Nelsonb0f6f6719186
diff --git a/src/core/embed-retry.ts b/src/core/embed-retry.tsindex 2acaa01e8d..9b15fba6a1 100644--- a/src/core/embed-retry.ts+++ b/src/core/embed-retry.ts@@ -283,7 +283,8 @@ export function transientBackoffMs(attempt: number, rng: () => number = Math.ran/*** #3374 — transient NETWORK failures: socket timeouts, connection resets,- * DNS blips. Structured detection first (error `code` / TimeoutError name+ * DNS blips (including Bun's `DNS_ETIMEOUT` code and `ETIMEOUT` getaddrinfo+ * message). Structured detection first (error `code` / TimeoutError name* through the cause chain, matching statusFromCause's walk), message-match* fallback for wrappers that strip the code. Deliberately does NOT match* caller-initiated aborts: the retry loop re-checks `signal.aborted` BEFORE@@ -292,7 +293,7 @@ export function transientBackoffMs(attempt: number, rng: () => number = Math.ran* @internal exported for unit tests.*/export function isTransientNetworkEmbedError(e: unknown): boolean {- const TRANSIENT_CODES = /^(ETIMEDOUT|ESOCKETTIMEDOUT|ECONNRESET|EPIPE|ECONNABORTED|EAI_AGAIN|UND_ERR_CONNECT_TIMEOUT|UND_ERR_HEADERS_TIMEOUT|UND_ERR_BODY_TIMEOUT|UND_ERR_SOCKET)$/;+ const TRANSIENT_CODES = /^(DNS_ETIMEOUT|ETIMEOUT|ETIMEDOUT|ESOCKETTIMEDOUT|ECONNRESET|EPIPE|ECONNABORTED|EAI_AGAIN|UND_ERR_CONNECT_TIMEOUT|UND_ERR_HEADERS_TIMEOUT|UND_ERR_BODY_TIMEOUT|UND_ERR_SOCKET)$/;let cur: unknown = e;for (let depth = 0; depth < 5 && cur !== undefined && cur !== null; depth++) {const obj = cur as { code?: unknown; name?: unknown; cause?: unknown };@@ -301,5 +302,5 @@ export function isTransientNetworkEmbedError(e: unknown): boolean {cur = obj.cause;}const msg = e instanceof Error ? e.message : String(e);- return /\b(ETIMEDOUT|ESOCKETTIMEDOUT|ECONNRESET|EPIPE|EAI_AGAIN)\b|socket hang up|fetch failed|connect(ion)? timeout|connection (reset|closed)|network (error|timeout)|request timed out|timed out/i.test(msg);+ return /\b(DNS_ETIMEOUT|ETIMEOUT|ETIMEDOUT|ESOCKETTIMEDOUT|ECONNRESET|EPIPE|EAI_AGAIN)\b|socket hang up|fetch failed|connect(ion)? timeout|connection (reset|closed)|network (error|timeout)|request timed out|timed out/i.test(msg);}diff --git a/test/embed.serial.test.ts b/test/embed.serial.test.tsindex 7026a76f24..5d452f9786 100644--- a/test/embed.serial.test.ts+++ b/test/embed.serial.test.ts@@ -830,6 +830,13 @@ describe('#3374 — transient network retry branch', () => {expect(isTransientNetworkEmbedError(new Error('socket hang up'))).toBe(true);expect(isTransientNetworkEmbedError(new Error('fetch failed'))).toBe(true);expect(isTransientNetworkEmbedError(new Error('request timed out'))).toBe(true);+ // Bun's DNS resolver uses DNS_ETIMEOUT as its code and ETIMEOUT (without+ // the D in ETIMEDOUT) in getaddrinfo messages.+ expect(isTransientNetworkEmbedError(new Error('getaddrinfo ETIMEOUT api.voyageai.com'))).toBe(true);+ expect(isTransientNetworkEmbedError({ code: 'ETIMEOUT' })).toBe(true);+ expect(isTransientNetworkEmbedError({ code: 'DNS_ETIMEOUT' })).toBe(true);+ expect(isTransientNetworkEmbedError({ cause: { code: 'DNS_ETIMEOUT' } })).toBe(true);+ expect(isTransientNetworkEmbedError(new Error('DNS_ETIMEOUT'))).toBe(true);// NOT transient: plain 500, permanent 4xx, caller aborts.expect(isTransientNetworkEmbedError(new Error('500 internal server error'))).toBe(false);expect(isTransientNetworkEmbedError(new Error('400 invalid input'))).toBe(false);@@ -868,6 +875,23 @@ describe('#3374 — transient network retry branch', () => {expect(result).toHaveLength(1);}, 10_000);+ test('Bun DNS ETIMEOUT retries then succeeds', async () => {+ const { embedBatchWithBackoff } = await import('../src/commands/embed.ts');+ let calls = 0;+ embedBatchBehavior = async () => {+ calls++;+ if (calls === 1) {+ const err = new Error('getaddrinfo ETIMEOUT api.voyageai.com');+ (err as any).code = 'DNS_ETIMEOUT';+ throw err;+ }+ return [new Float32Array(1536)];+ };+ const result = await embedBatchWithBackoff(['x']);+ expect(calls).toBe(2);+ expect(result).toHaveLength(1);+ }, 10_000);+test('caller-initiated abort during the transient sleep bubbles out (never retried)', async () => {const { embedBatchWithBackoff } = await import('../src/commands/embed.ts');const controller = new AbortController();