Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(llm-proxy): map network fetch failures in the transport by cause code
Alfredo Lopez256760287191
diff --git a/codex-llm-proxy/src/codexTransport.ts b/codex-llm-proxy/src/codexTransport.tsindex e2d611c95..240d60847 100644--- a/codex-llm-proxy/src/codexTransport.ts+++ b/codex-llm-proxy/src/codexTransport.ts@@ -5,7 +5,11 @@ import {parseCodexCompletionRequest,} from '@clerum/llm-provider-attempt-contract'import { chatgptUpstreamHeaders } from './chatgptUpstreamHeaders.js'-import type { FinalizeAttemptSuccess, RedeemAttemptSuccess } from './controlApiClient.js'+import {+ type FinalizeAttemptSuccess,+ type RedeemAttemptSuccess,+ fetchCauseCode,+} from './controlApiClient.js'import { logger } from './logger.js'import {CODEX_CATALOG_ORIGIN,@@ -42,13 +46,29 @@ export class CodexTransportError extends Error {constructor(readonly code: string,message: string,- readonly details?: Readonly<Record<string, number | string>>+ readonly details?: Readonly<Record<string, number | string>>,+ options?: ErrorOptions) {- super(message)+ super(message, options)this.name = 'CodexTransportError'}}+/**+ * Review R3-L1: a fetch that failed on the network is undici's TypeError, whose+ * cause message names the upstream address. It becomes a mapped transport error+ * that keeps only the cause code, so the attempt line logs `causeCode` and never+ * the error itself. An error with no code-shaped cause is returned undefined and+ * stays unmapped, which logs it as a handler defect.+ */+function upstreamFetchFailure(err: unknown): CodexTransportError | undefined {+ const code = fetchCauseCode(err)+ if (code === undefined) return undefined+ return new CodexTransportError('provider_unavailable', 'upstream fetch failed', undefined, {+ cause: { code },+ })+}+/*** The proxy cut the upstream stream on one of its two bounds. The wire code* stays that of CodexTransportError (`provider_unavailable` for idle silence,@@ -225,7 +245,7 @@ export async function streamCodexCompletion(throw err}outcome = 'error'- throw err+ throw upstreamFetchFailure(err) ?? err} finally {void accessTokenawait finalizeQuietly(input, redeemed, outcome, usage)@@ -999,7 +1019,7 @@ export async function listCodexModels(input: {})} catch (err) {if (signal.aborted) throw catalogTimeoutError()- throw err+ throw catalogFetchFailure(err)}if (response.status === 401 || response.status === 403)return { outcome: 'auth-rejected', models: [] }@@ -1015,7 +1035,16 @@ export async function listCodexModels(input: {return { outcome: 'unavailable', models: [] }}const raw = await readBoundedCatalogBody(response, signal)- const body = JSON.parse(raw) as unknown+ let body: unknown+ try {+ body = JSON.parse(raw)+ } catch {+ logger.warn(+ { event: 'codex_catalog_upstream', reason: 'invalid_json' },+ 'Codex catalog upstream returned a body that is not JSON'+ )+ throw new CodexTransportError('provider_unavailable', 'catalog upstream body is not JSON')+ }return { outcome: 'ready', models: normalizeModels(body) }}@@ -1081,6 +1110,19 @@ function normalizeModels(body: unknown): CatalogModel[] {return models}+// Review R3-L9: the admin route answers a catalog failure with a refusal line+// that carries only the code, so the cause code of a failed fetch is logged+// here before the mapped error is thrown.+function catalogFetchFailure(err: unknown): unknown {+ const failure = upstreamFetchFailure(err)+ if (!failure) return err+ logger.warn(+ { event: 'codex_catalog_upstream', causeCode: fetchCauseCode(err) },+ 'Codex catalog upstream fetch failed'+ )+ return failure+}+function catalogTimeoutError(): CodexTransportError {return new CodexTransportError('provider_unavailable', 'catalog upstream deadline exceeded')}@@ -1112,7 +1154,7 @@ async function readBoundedCatalogBody(response: Response, signal: AbortSignal):await reader.cancel().catch(() => undefined)if (err instanceof CodexTransportError) throw errif (signal.aborted) throw catalogTimeoutError()- throw err+ throw catalogFetchFailure(err)} finally {if (onAbort) signal.removeEventListener('abort', onAbort)try {diff --git a/grok-llm-proxy/src/grokTransport.ts b/grok-llm-proxy/src/grokTransport.tsindex d4ba739df..3bd90882b 100644--- a/grok-llm-proxy/src/grokTransport.ts+++ b/grok-llm-proxy/src/grokTransport.ts@@ -5,7 +5,11 @@ import {hashGrokCompletionRequest,parseGrokCompletionRequest,} from '@clerum/grok-provider-attempt-contract'-import type { FinalizeAttemptSuccess, RedeemAttemptSuccess } from './controlApiClient.js'+import {+ type FinalizeAttemptSuccess,+ type RedeemAttemptSuccess,+ fetchCauseCode,+} from './controlApiClient.js'import { grokUpstreamHeaders } from './grokUpstreamHeaders.js'import { logger } from './logger.js'import {@@ -70,13 +74,29 @@ export class GrokTransportError extends Error {constructor(readonly code: string,message: string,- readonly details?: Readonly<Record<string, number | string>>+ readonly details?: Readonly<Record<string, number | string>>,+ options?: ErrorOptions) {- super(message)+ super(message, options)this.name = 'GrokTransportError'}}+/**+ * Review R3-L1: a fetch that failed on the network is undici's TypeError, whose+ * cause message names the upstream address. It becomes a mapped transport error+ * that keeps only the cause code, so the attempt line logs `causeCode` and never+ * the error itself. An error with no code-shaped cause is returned undefined and+ * stays unmapped, which logs it as a handler defect.+ */+function upstreamFetchFailure(err: unknown): GrokTransportError | undefined {+ const code = fetchCauseCode(err)+ if (code === undefined) return undefined+ return new GrokTransportError('provider_unavailable', 'upstream fetch failed', undefined, {+ cause: { code },+ })+}+/*** The proxy cut the upstream stream on one of its two bounds. The wire code* stays that of GrokTransportError (`provider_unavailable` for idle silence,@@ -252,7 +272,7 @@ export async function streamGrokCompletion(throw err}outcome = 'error'- throw err+ throw upstreamFetchFailure(err) ?? err} finally {void accessTokenawait finalizeQuietly(input, redeemed, outcome, usage)@@ -1062,7 +1082,7 @@ export async function listGrokModels(input: {})} catch (err) {if (signal.aborted) throw catalogTimeoutError()- throw err+ throw catalogFetchFailure(err)}if (response.status === 401) return { outcome: 'auth-rejected', models: [] }if (!response.ok) {@@ -1073,7 +1093,16 @@ export async function listGrokModels(input: {return { outcome: 'unavailable', models: [] }}const raw = await readBoundedCatalogBody(response, signal)- const body = JSON.parse(raw) as unknown+ let body: unknown+ try {+ body = JSON.parse(raw)+ } catch {+ logger.warn(+ { event: 'grok_catalog_upstream', reason: 'invalid_json' },+ 'Grok catalog upstream returned a body that is not JSON'+ )+ throw new GrokTransportError('provider_unavailable', 'catalog upstream body is not JSON')+ }return { outcome: 'ready', models: normalizeModels(body) }}@@ -1139,6 +1168,19 @@ function normalizeModels(body: unknown): CatalogModel[] {return models}+// Review R3-L9: the admin route answers a catalog failure with a refusal line+// that carries only the code, so the cause code of a failed fetch is logged+// here before the mapped error is thrown.+function catalogFetchFailure(err: unknown): unknown {+ const failure = upstreamFetchFailure(err)+ if (!failure) return err+ logger.warn(+ { event: 'grok_catalog_upstream', causeCode: fetchCauseCode(err) },+ 'Grok catalog upstream fetch failed'+ )+ return failure+}+function catalogTimeoutError(): GrokTransportError {return new GrokTransportError('provider_unavailable', 'catalog upstream deadline exceeded')}@@ -1170,7 +1212,7 @@ async function readBoundedCatalogBody(response: Response, signal: AbortSignal):await reader.cancel().catch(() => undefined)if (err instanceof GrokTransportError) throw errif (signal.aborted) throw catalogTimeoutError()- throw err+ throw catalogFetchFailure(err)} finally {if (onAbort) signal.removeEventListener('abort', onAbort)try {