Skip to content

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

TypeScript

test(mcp-host): require a 429 to read as rate_limited with its Retry-After

Alfredo Lopez8cccf9dc8b35
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index 2b1b9f464..69a172cf9 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.440",
+ "version": "0.1.441",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.440",
+ "version": "0.1.441",
"license": "SEE LICENSE IN LICENSE",
"dependencies": {
"@anthropic-ai/sdk": "^0.32.0",
diff --git a/mcp-host/package.json b/mcp-host/package.json
index 893808369..efdd06341 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.440",
+ "version": "0.1.441",
"description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude",
"main": "dist/main.js",
"engines": {
diff --git a/mcp-host/src/llm/__tests__/codexLlmProxyClient.test.ts b/mcp-host/src/llm/__tests__/codexLlmProxyClient.test.ts
index fd6369828..663023353 100644
--- a/mcp-host/src/llm/__tests__/codexLlmProxyClient.test.ts
+++ b/mcp-host/src/llm/__tests__/codexLlmProxyClient.test.ts
@@ -622,3 +622,77 @@ describe('CodexLlmProxyClient', () => {
).rejects.toMatchObject({ code: 'rate_limited', dispatched: true })
})
})
+
+// G1-6 (#720): a 429 is a rate limit, whether or not its body carries JSON,
+// and its `Retry-After` (delta-seconds 1..3600) travels on the error.
+describe('CodexLlmProxyClient rate limits', () => {
+ const INPUT = {
+ executionTicket: 'ticket-123456',
+ requestHash: 'a'.repeat(64),
+ request: {},
+ }
+
+ async function failure(response: Response) {
+ const fetchFn = vi.fn<typeof fetch>(async () => response)
+ const client = new CodexLlmProxyClient({
+ runtimeUrl: 'http://proxy/completions',
+ readPlatformJwt: () => 'platform-jwt',
+ fetchFn,
+ })
+ const err = await client.stream(INPUT).then(
+ () => undefined,
+ (e: unknown) => e
+ )
+ return { err, fetchFn }
+ }
+
+ it('G1-6a reads a 429 with no JSON code as rate_limited with its Retry-After', async () => {
+ const { err, fetchFn } = await failure(
+ new Response('<html><body>Too Many Requests</body></html>', {
+ status: 429,
+ headers: { 'content-type': 'text/html', 'retry-after': '7' },
+ })
+ )
+ // Liveness witness: the proxy hop ran and got the 429.
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(err).toBeInstanceOf(CodexProxyError)
+ expect(err).toMatchObject({ code: 'rate_limited', retryAfterMs: 7000 })
+ })
+
+ it('G1-6b carries the Retry-After of a JSON rate_limited reply', async () => {
+ const { err, fetchFn } = await failure(
+ Response.json({ error: 'rate_limited' }, { status: 429, headers: { 'retry-after': '2' } })
+ )
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(err).toMatchObject({ code: 'rate_limited', retryAfterMs: 2000 })
+ })
+
+ it.each(['0', '3601', 'soon', '1.5', 'Wed, 21 Oct 2026 07:28:00 GMT', ''])(
+ 'G1-6c drops the Retry-After value %j instead of guessing',
+ async value => {
+ const { err, fetchFn } = await failure(
+ Response.json({ error: 'rate_limited' }, { status: 429, headers: { 'retry-after': value } })
+ )
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(err).toMatchObject({ code: 'rate_limited' })
+ expect((err as CodexProxyError).retryAfterMs).toBeUndefined()
+ }
+ )
+
+ it('G1-6c keeps the JSON code a 429 carries', async () => {
+ const { err } = await failure(Response.json({ error: 'budget_denied' }, { status: 429 }))
+ expect(err).toMatchObject({ code: 'budget_denied' })
+ })
+
+ it('G1-6c keeps an HTML 502 as provider_unavailable with no Retry-After', async () => {
+ const { err, fetchFn } = await failure(
+ new Response('<html><body>502 Bad Gateway</body></html>', {
+ status: 502,
+ headers: { 'content-type': 'text/html', 'retry-after': '5' },
+ })
+ )
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(err).toMatchObject({ code: 'provider_unavailable' })
+ expect((err as CodexProxyError).retryAfterMs).toBeUndefined()
+ })
+})
diff --git a/mcp-host/src/llm/__tests__/grokLlmProxyClient.test.ts b/mcp-host/src/llm/__tests__/grokLlmProxyClient.test.ts
index a55719220..fda835189 100644
--- a/mcp-host/src/llm/__tests__/grokLlmProxyClient.test.ts
+++ b/mcp-host/src/llm/__tests__/grokLlmProxyClient.test.ts
@@ -531,3 +531,68 @@ describe('GrokLlmProxyClient', () => {
expect(message).not.toMatch(/proxy stream failed/i)
})
})
+
+// G1-6 (#720): a 429 is a rate limit, whether or not its body carries JSON,
+// and its `Retry-After` (delta-seconds 1..3600) travels on the error.
+describe('GrokLlmProxyClient rate limits', () => {
+ async function failure(response: Response) {
+ const fetchFn = vi.fn(async () => response)
+ const err = await client(fetchFn)
+ .stream(STREAM_INPUT)
+ .then(
+ () => undefined,
+ (e: unknown) => e
+ )
+ return { err, fetchFn }
+ }
+
+ it('G1-6a reads a 429 with no JSON code as rate_limited with its Retry-After', async () => {
+ const { err, fetchFn } = await failure(
+ new Response('<html><body>Too Many Requests</body></html>', {
+ status: 429,
+ headers: { 'content-type': 'text/html', 'retry-after': '7' },
+ })
+ )
+ // Liveness witness: the proxy hop ran and got the 429.
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(err).toBeInstanceOf(GrokProxyError)
+ expect(err).toMatchObject({ code: 'rate_limited', retryAfterMs: 7000 })
+ })
+
+ it('G1-6b carries the Retry-After of a JSON rate_limited reply', async () => {
+ const { err, fetchFn } = await failure(
+ Response.json({ error: 'rate_limited' }, { status: 429, headers: { 'retry-after': '2' } })
+ )
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(err).toMatchObject({ code: 'rate_limited', retryAfterMs: 2000 })
+ })
+
+ it.each(['0', '3601', 'soon', '1.5', 'Wed, 21 Oct 2026 07:28:00 GMT', ''])(
+ 'G1-6c drops the Retry-After value %j instead of guessing',
+ async value => {
+ const { err, fetchFn } = await failure(
+ Response.json({ error: 'rate_limited' }, { status: 429, headers: { 'retry-after': value } })
+ )
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(err).toMatchObject({ code: 'rate_limited' })
+ expect((err as GrokProxyError).retryAfterMs).toBeUndefined()
+ }
+ )
+
+ it('G1-6c keeps the JSON code a 429 carries', async () => {
+ const { err } = await failure(Response.json({ error: 'budget_denied' }, { status: 429 }))
+ expect(err).toMatchObject({ code: 'budget_denied' })
+ })
+
+ it('G1-6c keeps an HTML 502 as provider_unavailable with no Retry-After', async () => {
+ const { err, fetchFn } = await failure(
+ new Response('<html><body>502 Bad Gateway</body></html>', {
+ status: 502,
+ headers: { 'content-type': 'text/html', 'retry-after': '5' },
+ })
+ )
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(err).toMatchObject({ code: 'provider_unavailable' })
+ expect((err as GrokProxyError).retryAfterMs).toBeUndefined()
+ })
+})
diff --git a/mcp-host/src/llm/__tests__/providerAttemptAuthorizer.test.ts b/mcp-host/src/llm/__tests__/providerAttemptAuthorizer.test.ts
index 7d983fd49..b650be12f 100644
--- a/mcp-host/src/llm/__tests__/providerAttemptAuthorizer.test.ts
+++ b/mcp-host/src/llm/__tests__/providerAttemptAuthorizer.test.ts
@@ -295,6 +295,23 @@ describe('ProviderAttemptAuthorizer', () => {
})
})
+ // G1-6 (#720): a limiter in front of control-api can answer 429 with no JSON
+ // code. It is a rate limit, not a provider outage.
+ it('G1-6d reads an HTML 429 from the gateway as rate_limited', async () => {
+ const { err, fetchFn } = await authorizeFailure(nginx(429, 'Too Many Requests'))
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(err).toBeInstanceOf(CodexAuthorizeError)
+ expect(err).toMatchObject({ code: 'rate_limited', message: 'authorize failed with 429' })
+ })
+
+ it('G1-6d keeps the JSON code a 429 carries', async () => {
+ const { err, fetchFn } = await authorizeFailure(
+ Response.json({ error: 'budget_denied' }, { status: 429 })
+ )
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(err).toMatchObject({ code: 'budget_denied' })
+ })
+
it('T-R7-1c reads a 413 as payload_too_large whatever JSON code it carries', async () => {
const { err, fetchFn } = await authorizeFailure(
new Response(JSON.stringify({ error: 'invalid_request' }), {