Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

test(llm-proxy): require unmapped upstream 4xx to answer upstream_rejected

Alfredo Lopez37eede2bdac3
diff --git a/codex-llm-proxy/test/codexTransport.conformance.test.ts b/codex-llm-proxy/test/codexTransport.conformance.test.ts
index 545fdccfd..53d4fa8a3 100644
--- a/codex-llm-proxy/test/codexTransport.conformance.test.ts
+++ b/codex-llm-proxy/test/codexTransport.conformance.test.ts
@@ -1640,6 +1640,28 @@ describe('streamCodexCompletion', () => {
const details = (err as { details?: Record<string, unknown> }).details
expect(details?.retryAfterSeconds).toBeUndefined()
})
+
+ // G1-3 (#720): the same request gets the same 4xx every time, so it is not
+ // an outage to retry or fail over from.
+ it.each([404, 409, 422])(
+ 'G1-3a maps an unmapped upstream %i to upstream_rejected',
+ async (status) => {
+ const err = await streamWith(() => new Response('rejected', { status })).pending.catch(
+ (caught: unknown) => caught
+ )
+ expect(err).toMatchObject({ code: 'upstream_rejected' })
+ expect((err as Error).message).toContain(String(status))
+ }
+ )
+
+ it.each([408, 500, 502, 503, 504])(
+ 'G1-3b keeps an upstream %i as provider_unavailable',
+ async (status) => {
+ await expect(
+ streamWith(() => new Response('busy', { status })).pending
+ ).rejects.toMatchObject({ code: 'provider_unavailable' })
+ }
+ )
})
it('emits a tool call only after argument deltas complete', async () => {
diff --git a/codex-llm-proxy/test/server.security.test.ts b/codex-llm-proxy/test/server.security.test.ts
index 2cb00cfa1..c6abd77da 100644
--- a/codex-llm-proxy/test/server.security.test.ts
+++ b/codex-llm-proxy/test/server.security.test.ts
@@ -1222,6 +1222,34 @@ describe('codex-llm-proxy attempt telemetry', () => {
expect(res.headers['retry-after']).toBeUndefined()
})
+ // G1-3 (#720): an upstream refusal the same request would get again is a
+ // non-retryable 502 upstream_rejected, not a retryable 503.
+ it('(g1-3a) answers 502 upstream_rejected for an unmapped upstream 4xx', async () => {
+ const { res, receipts, lines, metricsText } = await run(
+ 'att-rejected-http',
+ 0,
+ 0,
+ undefined,
+ undefined,
+ { fetchFn: (async () => new Response('not found', { status: 404 })) as typeof fetch }
+ )
+ expect(res.status).toBe(502)
+ expect(res.headers['content-type']).toMatch(/^application\/json/)
+ expect(res.body).toEqual({ error: 'upstream_rejected' })
+ expect(receipts).toEqual([expect.objectContaining({ outcome: 'error' })])
+ expect(lines).toHaveLength(1)
+ expect(lines[0]).toMatchObject({
+ providerAttemptId: 'att-rejected-http',
+ outcome: 'failed',
+ code: 'upstream_rejected',
+ deliveredAs: 'http_status',
+ httpStatus: 502,
+ })
+ expectNoForbiddenKeys(lines[0]!)
+ expect(failureCount(metricsText, 'upstream_rejected')).toBe(1)
+ expect(failureCount(metricsText, 'other')).toBe(0)
+ })
+
// #731 — the recorded upstream context-window refusal reaches the Host as a
// 400 `context_length_exceeded` (the upstream's own status class), not as a
// retryable 503 `provider_unavailable`.
diff --git a/grok-llm-proxy/test/grokTransport.conformance.test.ts b/grok-llm-proxy/test/grokTransport.conformance.test.ts
index 584577a6e..2a803a2b6 100644
--- a/grok-llm-proxy/test/grokTransport.conformance.test.ts
+++ b/grok-llm-proxy/test/grokTransport.conformance.test.ts
@@ -777,7 +777,7 @@ describe('streamGrokCompletion', () => {
expect(result.usage).toEqual({ inputTokens: 4, outputTokens: 5 })
})
- it('maps upstream 402/403 to provider_unavailable', async () => {
+ it('maps upstream 402/403 to upstream_rejected', async () => {
const fetchFn = vi.fn(
async (_url: FetchInput, _init?: RequestInit) => new Response('paywall', { status: 403 })
)
@@ -803,7 +803,7 @@ describe('streamGrokCompletion', () => {
fetchFn,
lookup: async () => [{ address: '1.2.3.4', family: 4 }],
})
- ).rejects.toMatchObject({ code: 'provider_unavailable' })
+ ).rejects.toMatchObject({ code: 'upstream_rejected' })
})
it('rejects a mutated requestHash before redeem', async () => {
@@ -1644,6 +1644,28 @@ describe('streamGrokCompletion', () => {
const details = (err as { details?: Record<string, unknown> }).details
expect(details?.retryAfterSeconds).toBeUndefined()
})
+
+ // G1-3 (#720): the same request gets the same 4xx every time, so it is not
+ // an outage to retry or fail over from. 402/403 is an entitlement refusal.
+ it.each([402, 403, 404, 409, 422])(
+ 'G1-3a maps an unmapped upstream %i to upstream_rejected',
+ async (status) => {
+ const err = await streamWith(() => new Response('rejected', { status })).pending.catch(
+ (caught: unknown) => caught
+ )
+ expect(err).toMatchObject({ code: 'upstream_rejected' })
+ expect((err as Error).message).toContain(String(status))
+ }
+ )
+
+ it.each([408, 500, 502, 503, 504])(
+ 'G1-3b keeps an upstream %i as provider_unavailable',
+ async (status) => {
+ await expect(
+ streamWith(() => new Response('busy', { status })).pending
+ ).rejects.toMatchObject({ code: 'provider_unavailable' })
+ }
+ )
})
it('emits a tool call only after argument deltas complete', async () => {
diff --git a/grok-llm-proxy/test/server.security.test.ts b/grok-llm-proxy/test/server.security.test.ts
index 787dff47f..41b16528d 100644
--- a/grok-llm-proxy/test/server.security.test.ts
+++ b/grok-llm-proxy/test/server.security.test.ts
@@ -1064,6 +1064,30 @@ describe('grok-llm-proxy attempt telemetry', () => {
expect(res.headers['retry-after']).toBeUndefined()
})
+ // G1-3 (#720): an upstream refusal the same request would get again is a
+ // non-retryable 502 upstream_rejected, not a retryable 503.
+ it('(g1-3a) answers 502 upstream_rejected for an unmapped upstream 4xx', async () => {
+ const { res, receipts, lines, metricsText } = await run({
+ providerAttemptId: 'att-rejected-http',
+ fetchFn: (async () => new Response('not found', { status: 404 })) as typeof fetch,
+ })
+ expect(res.status).toBe(502)
+ expect(res.headers['content-type']).toMatch(/^application\/json/)
+ expect(res.body).toEqual({ error: 'upstream_rejected' })
+ expect(receipts).toEqual([expect.objectContaining({ outcome: 'error' })])
+ expect(lines).toHaveLength(1)
+ expect(lines[0]).toMatchObject({
+ providerAttemptId: 'att-rejected-http',
+ outcome: 'failed',
+ code: 'upstream_rejected',
+ deliveredAs: 'http_status',
+ httpStatus: 502,
+ })
+ expectNoForbiddenKeys(lines[0]!)
+ expect(failureCount(metricsText, 'upstream_rejected')).toBe(1)
+ expect(failureCount(metricsText, 'other')).toBe(0)
+ })
+
it('(a) answers 422 and logs one attempt line when 257 calls arrive before any text', async () => {
const { res, receipts, lines, metricsText } = await run({
providerAttemptId: 'att-limit-http',