Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
test(llm-proxy): pin DNS lookup failures by cause code (R4-L1)
Alfredo Lopezbe532be036e8
diff --git a/codex-llm-proxy/test/originPolicy.test.ts b/codex-llm-proxy/test/originPolicy.test.tsindex 0a7f017df..2e49eb635 100644--- a/codex-llm-proxy/test/originPolicy.test.ts+++ b/codex-llm-proxy/test/originPolicy.test.ts@@ -119,4 +119,47 @@ describe('originPolicy', () => {])).resolves.toBeUndefined()})++ // Review R4-L1: a failed lookup carries its system code on the error itself+ // and names the host in its message. Only a code-shaped code is kept, as+ // the cause the transport maps by code.+ it('maps a failed lookup to its code without the host name', async () => {+ const { assertResolvedUpstream } = await import('../src/originPolicy.js')+ const host = 'codex-origin-policy-r4-l1.invalid'+ let lookups = 0+ const err: unknown = await assertResolvedUpstream(new URL(CODEX_COMPLETIONS_ORIGIN), async () => {+ lookups += 1+ const { lookup } = await import('node:dns/promises')+ const records = await lookup(host, { all: true })+ return records.map(record => ({ address: record.address, family: record.family }))+ }).then(+ () => 'resolved',+ (rejection: unknown) => rejection+ )+ // Witness: the real lookup ran once.+ expect(lookups).toBe(1)+ expect(err).toBeInstanceOf(Error)+ expect(err).not.toBeInstanceOf(OriginDeniedError)+ expect((err as Error).message).toBe('upstream address lookup failed')+ const cause = (err as { cause?: unknown }).cause as Record<string, unknown>+ expect(Object.keys(cause)).toEqual(['code'])+ expect(String(cause.code)).toMatch(/^(ENOTFOUND|EAI_AGAIN)$/)+ expect(JSON.stringify({ message: (err as Error).message, cause })).not.toContain(host)+ })++ it('rethrows a lookup error without a code-shaped code unchanged', async () => {+ const { assertResolvedUpstream } = await import('../src/originPolicy.js')+ const plain = new Error('lookup exploded')+ await expect(+ assertResolvedUpstream(new URL(CODEX_COMPLETIONS_ORIGIN), async () => {+ throw plain+ })+ ).rejects.toBe(plain)+ const oddCode = Object.assign(new Error('odd'), { code: 'not a code' })+ await expect(+ assertResolvedUpstream(new URL(CODEX_COMPLETIONS_ORIGIN), async () => {+ throw oddCode+ })+ ).rejects.toBe(oddCode)+ })})diff --git a/codex-llm-proxy/test/server.security.test.ts b/codex-llm-proxy/test/server.security.test.tsindex 726be12a2..12e3aba98 100644--- a/codex-llm-proxy/test/server.security.test.ts+++ b/codex-llm-proxy/test/server.security.test.ts@@ -1205,6 +1205,7 @@ describe('codex-llm-proxy attempt telemetry', () => {configOverrides?: Partial<CodexLlmProxyConfig>controlApiClient?: ControlApiClientterminal?: string[]+ lookup?: (hostname: string) => Promise<Array<{ address: string; family: number }>>} = {}) {const info = vi.spyOn(logger, 'info')@@ -1213,7 +1214,7 @@ describe('codex-llm-proxy attempt telemetry', () => {const apps = createProxyApps(config({ maxBodyBytes: 65_536, ...options.configOverrides }), {controlApiClient: options.controlApiClient ?? (deniedCode ? denyingClient(deniedCode) : client),fetchFn: options.fetchFn ?? upstream(textDeltas, calls, options.terminal),- lookup,+ lookup: options.lookup ?? lookup,})try {const res = await request(apps.runtimeApp)@@ -1580,6 +1581,101 @@ describe('codex-llm-proxy attempt telemetry', () => {}})+ // Review R4-L1: a DNS failure carries its code on the lookup error itself,+ // not in `cause`, and its message names the upstream host. The attempt line+ // and both admin routes log it by cause code, with no err entry and no host.+ const INVALID_UPSTREAM_HOST = 'codex-r4-l1.invalid'++ function invalidHostLookup(calls: { count: number }) {+ return async () => {+ calls.count += 1+ const { lookup: dnsLookup } = await import('node:dns/promises')+ const records = await dnsLookup(INVALID_UPSTREAM_HOST, { all: true })+ return records.map(record => ({ address: record.address, family: record.family }))+ }+ }++ function countingFetch(calls: { count: number }): typeof fetch {+ return (async () => {+ calls.count += 1+ throw new Error('fetch must not run after a failed lookup')+ }) as typeof fetch+ }++ it('(r4-l1a) logs a DNS lookup failure by cause code, without err or host', async () => {+ const lookups = { count: 0 }+ const fetches = { count: 0 }+ const { res, lines } = await run('att-upstream-dns', 0, 0, undefined, undefined, {+ fetchFn: countingFetch(fetches),+ lookup: invalidHostLookup(lookups),+ })+ expect(res.status).toBe(503)+ expect(res.body).toEqual({ error: 'provider_unavailable' })+ // Witnesses: the real lookup ran once, failed before any fetch, and the+ // failure produced its attempt line.+ expect(lookups.count).toBe(1)+ expect(fetches.count).toBe(0)+ expect(lines).toHaveLength(1)+ expect(lines[0]).toMatchObject({+ providerAttemptId: 'att-upstream-dns',+ outcome: 'failed',+ code: 'provider_unavailable',+ reason: 'upstream fetch failed',+ deliveredAs: 'http_status',+ httpStatus: 503,+ })+ expect(String(lines[0]!.causeCode)).toMatch(/^(ENOTFOUND|EAI_AGAIN)$/)+ expect('err' in lines[0]!).toBe(false)+ expect(serializeWithErrors(lines[0])).not.toContain(INVALID_UPSTREAM_HOST)+ expectNoForbiddenKeys(lines[0]!)+ })++ it('(r4-l1b) logs a catalog DNS lookup failure by cause code on both admin routes', async () => {+ for (const [route, operation] of [+ ['/internal/admin/v1/codex/models', 'catalog_list'],+ ['/internal/admin/v1/codex/test', 'connection_test'],+ ] as const) {+ const lookups = { count: 0 }+ const fetches = { count: 0 }+ const warn = vi.spyOn(logger, 'warn')+ const info = vi.spyOn(logger, 'info')+ const error = vi.spyOn(logger, 'error')+ try {+ const { adminApp } = createProxyApps(+ config({ executionEnabled: true, maxBodyBytes: 65_536 }),+ { fetchFn: countingFetch(fetches), lookup: invalidHostLookup(lookups) }+ )+ const res = await request(adminApp)+ .post(route)+ .set(+ 'Authorization',+ `Bearer ${sign({ sub: 'admin-1', typ: 'codex-admin-permit', operation }, 'codex-llm-proxy-admin')}`+ )+ .send({ accessToken: 'tok' })+ expect(res.status).toBe(503)+ expect(res.body).toEqual({ error: 'provider_unavailable' })+ // Witnesses: the real lookup ran once and failed before any fetch.+ expect(lookups.count).toBe(1)+ expect(fetches.count).toBe(0)+ const logged = [...warn.mock.calls, ...info.mock.calls, ...error.mock.calls].map(+ call => call[0] as unknown as Record<string, unknown>+ )+ const upstream = logged.filter(entry => entry?.event === 'codex_catalog_upstream')+ expect(upstream).toHaveLength(1)+ expect(Object.keys(upstream[0]!).sort()).toEqual(['causeCode', 'event'])+ expect(String(upstream[0]!.causeCode)).toMatch(/^(ENOTFOUND|EAI_AGAIN)$/)+ expect(logged.filter(entry => entry?.event === 'codex_proxy_denied')).toEqual([+ { event: 'codex_proxy_denied', code: 'provider_unavailable' },+ ])+ expect(serializeWithErrors(logged)).not.toContain(INVALID_UPSTREAM_HOST)+ } finally {+ warn.mockRestore()+ info.mockRestore()+ error.mockRestore()+ }+ }+ })+it('logs a mapped control-api failure without an err entry', async () => {const { res, lines } = await run('att-mapped-control-api-error',diff --git a/grok-llm-proxy/test/originPolicy.test.ts b/grok-llm-proxy/test/originPolicy.test.tsindex fa57f4a6b..f47e9b81e 100644--- a/grok-llm-proxy/test/originPolicy.test.ts+++ b/grok-llm-proxy/test/originPolicy.test.ts@@ -142,4 +142,47 @@ describe('originPolicy', () => {])).resolves.toBeUndefined()})++ // Review R4-L1: a failed lookup carries its system code on the error itself+ // and names the host in its message. Only a code-shaped code is kept, as+ // the cause the transport maps by code.+ it('maps a failed lookup to its code without the host name', async () => {+ const { assertResolvedUpstream } = await import('../src/originPolicy.js')+ const host = 'grok-origin-policy-r4-l1.invalid'+ let lookups = 0+ const err: unknown = await assertResolvedUpstream(new URL(GROK_COMPLETIONS_ORIGIN), async () => {+ lookups += 1+ const { lookup } = await import('node:dns/promises')+ const records = await lookup(host, { all: true })+ return records.map(record => ({ address: record.address, family: record.family }))+ }).then(+ () => 'resolved',+ (rejection: unknown) => rejection+ )+ // Witness: the real lookup ran once.+ expect(lookups).toBe(1)+ expect(err).toBeInstanceOf(Error)+ expect(err).not.toBeInstanceOf(OriginDeniedError)+ expect((err as Error).message).toBe('upstream address lookup failed')+ const cause = (err as { cause?: unknown }).cause as Record<string, unknown>+ expect(Object.keys(cause)).toEqual(['code'])+ expect(String(cause.code)).toMatch(/^(ENOTFOUND|EAI_AGAIN)$/)+ expect(JSON.stringify({ message: (err as Error).message, cause })).not.toContain(host)+ })++ it('rethrows a lookup error without a code-shaped code unchanged', async () => {+ const { assertResolvedUpstream } = await import('../src/originPolicy.js')+ const plain = new Error('lookup exploded')+ await expect(+ assertResolvedUpstream(new URL(GROK_COMPLETIONS_ORIGIN), async () => {+ throw plain+ })+ ).rejects.toBe(plain)+ const oddCode = Object.assign(new Error('odd'), { code: 'not a code' })+ await expect(+ assertResolvedUpstream(new URL(GROK_COMPLETIONS_ORIGIN), async () => {+ throw oddCode+ })+ ).rejects.toBe(oddCode)+ })})diff --git a/grok-llm-proxy/test/server.security.test.ts b/grok-llm-proxy/test/server.security.test.tsindex ff71e9d9c..85873ace9 100644--- a/grok-llm-proxy/test/server.security.test.ts+++ b/grok-llm-proxy/test/server.security.test.ts@@ -1294,6 +1294,7 @@ describe('grok-llm-proxy attempt telemetry', () => {maxStreamDurationMs?: numberconfigOverrides?: Partial<GrokLlmProxyConfig>controlApiClient?: ControlApiClient+ lookup?: (hostname: string) => Promise<Array<{ address: string; family: number }>>}) {const info = vi.spyOn(logger, 'info')const warn = vi.spyOn(logger, 'warn')@@ -1303,7 +1304,7 @@ describe('grok-llm-proxy attempt telemetry', () => {options.controlApiClient ??(options.deniedCode ? denyingClient(options.deniedCode) : client),fetchFn: options.fetchFn ?? upstream(options.textDeltas ?? 0, options.calls ?? 0),- lookup,+ lookup: options.lookup ?? lookup,})try {const res = await request(apps.runtimeApp)@@ -1547,6 +1548,102 @@ describe('grok-llm-proxy attempt telemetry', () => {}})+ // Review R4-L1: a DNS failure carries its code on the lookup error itself,+ // not in `cause`, and its message names the upstream host. The attempt line+ // and both admin routes log it by cause code, with no err entry and no host.+ const INVALID_UPSTREAM_HOST = 'grok-r4-l1.invalid'++ function invalidHostLookup(calls: { count: number }) {+ return async () => {+ calls.count += 1+ const { lookup: dnsLookup } = await import('node:dns/promises')+ const records = await dnsLookup(INVALID_UPSTREAM_HOST, { all: true })+ return records.map(record => ({ address: record.address, family: record.family }))+ }+ }++ function countingFetch(calls: { count: number }): typeof fetch {+ return (async () => {+ calls.count += 1+ throw new Error('fetch must not run after a failed lookup')+ }) as typeof fetch+ }++ it('(r4-l1a) logs a DNS lookup failure by cause code, without err or host', async () => {+ const lookups = { count: 0 }+ const fetches = { count: 0 }+ const { res, lines } = await run({+ providerAttemptId: 'att-upstream-dns',+ fetchFn: countingFetch(fetches),+ lookup: invalidHostLookup(lookups),+ })+ expect(res.status).toBe(503)+ expect(res.body).toEqual({ error: 'provider_unavailable' })+ // Witnesses: the real lookup ran once, failed before any fetch, and the+ // failure produced its attempt line.+ expect(lookups.count).toBe(1)+ expect(fetches.count).toBe(0)+ expect(lines).toHaveLength(1)+ expect(lines[0]).toMatchObject({+ providerAttemptId: 'att-upstream-dns',+ outcome: 'failed',+ code: 'provider_unavailable',+ reason: 'upstream fetch failed',+ deliveredAs: 'http_status',+ httpStatus: 503,+ })+ expect(String(lines[0]!.causeCode)).toMatch(/^(ENOTFOUND|EAI_AGAIN)$/)+ expect('err' in lines[0]!).toBe(false)+ expect(serializeWithErrors(lines[0])).not.toContain(INVALID_UPSTREAM_HOST)+ expectNoForbiddenKeys(lines[0]!)+ })++ it('(r4-l1b) logs a catalog DNS lookup failure by cause code on both admin routes', async () => {+ for (const [route, operation] of [+ ['/internal/admin/v1/grok/models', 'catalog_list'],+ ['/internal/admin/v1/grok/test', 'connection_test'],+ ] as const) {+ const lookups = { count: 0 }+ const fetches = { count: 0 }+ const warn = vi.spyOn(logger, 'warn')+ const info = vi.spyOn(logger, 'info')+ const error = vi.spyOn(logger, 'error')+ try {+ const { adminApp } = createProxyApps(+ config({ executionEnabled: true, maxBodyBytes: 65_536 }),+ { fetchFn: countingFetch(fetches), lookup: invalidHostLookup(lookups) }+ )+ const res = await request(adminApp)+ .post(route)+ .set(+ 'Authorization',+ `Bearer ${sign({ sub: 'admin-1', typ: 'grok-admin-permit', operation }, 'grok-llm-proxy-admin')}`+ )+ .send({ accessToken: 'tok' })+ expect(res.status).toBe(503)+ expect(res.body).toEqual({ error: 'provider_unavailable' })+ // Witnesses: the real lookup ran once and failed before any fetch.+ expect(lookups.count).toBe(1)+ expect(fetches.count).toBe(0)+ const logged = [...warn.mock.calls, ...info.mock.calls, ...error.mock.calls].map(+ call => call[0] as unknown as Record<string, unknown>+ )+ const upstream = logged.filter(entry => entry?.event === 'grok_catalog_upstream')+ expect(upstream).toHaveLength(1)+ expect(Object.keys(upstream[0]!).sort()).toEqual(['causeCode', 'event'])+ expect(String(upstream[0]!.causeCode)).toMatch(/^(ENOTFOUND|EAI_AGAIN)$/)+ expect(logged.filter(entry => entry?.event === 'grok_proxy_denied')).toEqual([+ { event: 'grok_proxy_denied', code: 'provider_unavailable' },+ ])+ expect(serializeWithErrors(logged)).not.toContain(INVALID_UPSTREAM_HOST)+ } finally {+ warn.mockRestore()+ info.mockRestore()+ error.mockRestore()+ }+ }+ })+it('logs a mapped control-api failure without an err entry', async () => {const { res, lines } = await run({providerAttemptId: 'att-mapped-control-api-error',