Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
test(llm-proxy): require an upstream 429 to answer rate_limited with Retry-After
Alfredo Lopez54c46dcc64e8
diff --git a/codex-llm-proxy/test/codexTransport.conformance.test.ts b/codex-llm-proxy/test/codexTransport.conformance.test.tsindex eb5df9bfc..545fdccfd 100644--- a/codex-llm-proxy/test/codexTransport.conformance.test.ts+++ b/codex-llm-proxy/test/codexTransport.conformance.test.ts@@ -1556,9 +1556,10 @@ describe('streamCodexCompletion', () => {await expect(streamWith(() => new Response(other, { status: 400 })).pending).rejects.toMatchObject({ code: 'invalid_request' })+ // G1-1 (#720): a 429 is a rate limit, not an outage.await expect(streamWith(() => new Response(other, { status: 429 })).pending- ).rejects.toMatchObject({ code: 'provider_unavailable' })+ ).rejects.toMatchObject({ code: 'rate_limited' })await expect(streamWith(() => new Response(contextErrorBody, { status: 401 })).pending).rejects.toMatchObject({ code: 'connection_unavailable' })@@ -1591,6 +1592,54 @@ describe('streamCodexCompletion', () => {expect(canceled).toBe(true)expect(pulled).toBeLessThan(1024 * 1024)})++ // G1-1 (#720): an upstream 429 is a rate limit. Retry-After travels only as+ // whole seconds in 1..3600, the rule gfsClient already applies; any other+ // value is treated as absent.+ it('G1-1a maps an upstream 429 to rate_limited and carries a valid Retry-After', async () => {+ const { pending, finalize } = streamWith(+ () => new Response('slow down', { status: 429, headers: { 'retry-after': '7' } })+ )+ await expect(pending).rejects.toMatchObject({+ code: 'rate_limited',+ details: { retryAfterSeconds: 7 },+ })+ expect(finalize).toHaveBeenCalledWith(+ expect.objectContaining({ receipt: expect.objectContaining({ outcome: 'error' }) })+ )+ })++ it('G1-1b treats a missing Retry-After, or one outside 1..3600 whole seconds, as absent', async () => {+ const values: (string | undefined)[] = [+ undefined,+ '0',+ '3601',+ '1.5',+ '-3',+ '07',+ 'Wed, 21 Oct 2026 07:28:00 GMT',+ '',+ ]+ for (const value of values) {+ const headers: Record<string, string> = value === undefined ? {} : { 'retry-after': value }+ const err = await streamWith(+ () => new Response('slow down', { status: 429, headers })+ ).pending.catch((caught: unknown) => caught)+ // Witness: the 429 arm ran for this value.+ expect(err, `retry-after=${JSON.stringify(value)}`).toMatchObject({ code: 'rate_limited' })+ const details = (err as { details?: Record<string, unknown> }).details+ expect(details?.retryAfterSeconds, `retry-after=${JSON.stringify(value)}`).toBeUndefined()+ }+ })++ it('G1-1c keeps an upstream 503 as provider_unavailable, Retry-After or not', async () => {+ const err = await streamWith(+ () => new Response('busy', { status: 503, headers: { 'retry-after': '7' } })+ ).pending.catch((caught: unknown) => caught)+ expect(err).toMatchObject({ code: 'provider_unavailable' })+ const details = (err as { details?: Record<string, unknown> }).details+ expect(details?.retryAfterSeconds).toBeUndefined()+ })})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.tsindex 068a0eb56..ced2ed0bd 100644--- a/codex-llm-proxy/test/server.security.test.ts+++ b/codex-llm-proxy/test/server.security.test.ts@@ -1167,6 +1167,54 @@ describe('codex-llm-proxy attempt telemetry', () => {expect(timeoutCount(metricsText, 'total')).toBe(0)})+ // G1-1 (#720): an upstream 429 reaches the Host as a 429 rate_limited with+ // the upstream's Retry-After, not as a retryable 503 provider_unavailable.+ function rateLimitedUpstream(retryAfter?: string): typeof fetch {+ return (async () =>+ new Response('slow down', {+ status: 429,+ headers: retryAfter === undefined ? {} : { 'retry-after': retryAfter },+ })) as typeof fetch+ }++ it('(g1-1a) answers 429 rate_limited and forwards a valid upstream Retry-After', async () => {+ const { res, receipts, lines, metricsText } = await run(+ 'att-rate-http',+ 0,+ 0,+ undefined,+ undefined,+ { fetchFn: rateLimitedUpstream('7') }+ )+ expect(res.status).toBe(429)+ expect(res.headers['content-type']).toMatch(/^application\/json/)+ expect(res.headers['retry-after']).toBe('7')+ expect(res.body).toEqual({ error: 'rate_limited' })+ expect(receipts).toEqual([expect.objectContaining({ outcome: 'error' })])+ expect(lines).toHaveLength(1)+ expect(lines[0]).toMatchObject({+ providerAttemptId: 'att-rate-http',+ outcome: 'failed',+ code: 'rate_limited',+ details: { retryAfterSeconds: 7 },+ deliveredAs: 'http_status',+ httpStatus: 429,+ })+ expectNoForbiddenKeys(lines[0]!)+ expect(failureCount(metricsText, 'rate_limited')).toBe(1)+ expect(failureCount(metricsText, 'other')).toBe(0)+ })++ it('(g1-1b) answers 429 with no Retry-After header when the upstream value is invalid', async () => {+ const { res } = await run('att-rate-bad-header', 0, 0, undefined, undefined, {+ fetchFn: rateLimitedUpstream('3601'),+ })+ // Witness: the 429 path ran.+ expect(res.status).toBe(429)+ expect(res.body).toEqual({ error: 'rate_limited' })+ expect(res.headers['retry-after']).toBeUndefined()+ })+// #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.tsindex d1364bb96..584577a6e 100644--- a/grok-llm-proxy/test/grokTransport.conformance.test.ts+++ b/grok-llm-proxy/test/grokTransport.conformance.test.ts@@ -1595,6 +1595,55 @@ describe('streamGrokCompletion', () => {expect(canceled).toBe(true)expect(pulled).toBeLessThan(1024 * 1024)})++ // G1-1 (#720): an upstream 429 is a rate limit. Retry-After travels only as+ // whole seconds in 1..3600, the rule gfsClient already applies; any other+ // value is treated as absent.+ it('G1-1a maps an upstream 429 to rate_limited and carries a valid Retry-After', async () => {+ const { pending, finalize, fetchFn } = streamWith(+ () => new Response('slow down', { status: 429, headers: { 'retry-after': '7' } })+ )+ await expect(pending).rejects.toMatchObject({+ code: 'rate_limited',+ details: { retryAfterSeconds: 7 },+ })+ expect(fetchFn).toHaveBeenCalledTimes(1)+ expect(finalize).toHaveBeenCalledWith(+ expect.objectContaining({ receipt: expect.objectContaining({ outcome: 'error' }) })+ )+ })++ it('G1-1b treats a missing Retry-After, or one outside 1..3600 whole seconds, as absent', async () => {+ const values: (string | undefined)[] = [+ undefined,+ '0',+ '3601',+ '1.5',+ '-3',+ '07',+ 'Wed, 21 Oct 2026 07:28:00 GMT',+ '',+ ]+ for (const value of values) {+ const headers: Record<string, string> = value === undefined ? {} : { 'retry-after': value }+ const err = await streamWith(+ () => new Response('slow down', { status: 429, headers })+ ).pending.catch((caught: unknown) => caught)+ // Witness: the 429 arm ran for this value.+ expect(err, `retry-after=${JSON.stringify(value)}`).toMatchObject({ code: 'rate_limited' })+ const details = (err as { details?: Record<string, unknown> }).details+ expect(details?.retryAfterSeconds, `retry-after=${JSON.stringify(value)}`).toBeUndefined()+ }+ })++ it('G1-1c keeps an upstream 503 as provider_unavailable, Retry-After or not', async () => {+ const err = await streamWith(+ () => new Response('busy', { status: 503, headers: { 'retry-after': '7' } })+ ).pending.catch((caught: unknown) => caught)+ expect(err).toMatchObject({ code: 'provider_unavailable' })+ const details = (err as { details?: Record<string, unknown> }).details+ expect(details?.retryAfterSeconds).toBeUndefined()+ })})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.tsindex daae3ea66..5aa603b2a 100644--- a/grok-llm-proxy/test/server.security.test.ts+++ b/grok-llm-proxy/test/server.security.test.ts@@ -999,6 +999,51 @@ describe('grok-llm-proxy attempt telemetry', () => {expect(visited).toBeGreaterThan(0)}+ // G1-1 (#720): an upstream 429 reaches the Host as a 429 rate_limited with+ // the upstream's Retry-After, not as a retryable 503 provider_unavailable.+ function rateLimitedUpstream(retryAfter?: string): typeof fetch {+ return (async () =>+ new Response('slow down', {+ status: 429,+ headers: retryAfter === undefined ? {} : { 'retry-after': retryAfter },+ })) as typeof fetch+ }++ it('(g1-1a) answers 429 rate_limited and forwards a valid upstream Retry-After', async () => {+ const { res, receipts, lines, metricsText } = await run({+ providerAttemptId: 'att-rate-http',+ fetchFn: rateLimitedUpstream('7'),+ })+ expect(res.status).toBe(429)+ expect(res.headers['content-type']).toMatch(/^application\/json/)+ expect(res.headers['retry-after']).toBe('7')+ expect(res.body).toEqual({ error: 'rate_limited' })+ expect(receipts).toEqual([expect.objectContaining({ outcome: 'error' })])+ expect(lines).toHaveLength(1)+ expect(lines[0]).toMatchObject({+ providerAttemptId: 'att-rate-http',+ outcome: 'failed',+ code: 'rate_limited',+ details: { retryAfterSeconds: 7 },+ deliveredAs: 'http_status',+ httpStatus: 429,+ })+ expectNoForbiddenKeys(lines[0]!)+ expect(failureCount(metricsText, 'rate_limited')).toBe(1)+ expect(failureCount(metricsText, 'other')).toBe(0)+ })++ it('(g1-1b) answers 429 with no Retry-After header when the upstream value is invalid', async () => {+ const { res } = await run({+ providerAttemptId: 'att-rate-bad-header',+ fetchFn: rateLimitedUpstream('3601'),+ })+ // Witness: the 429 path ran.+ expect(res.status).toBe(429)+ expect(res.body).toEqual({ error: 'rate_limited' })+ expect(res.headers['retry-after']).toBeUndefined()+ })+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',