Skip to content

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

TypeScript

test(mcp-host): control-api limiter 429 and retry abort gap (RED, #720)

Alfredo Lopez26fa0b9af824
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index 49071a9ff..b8b9e59cf 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.449",
+ "version": "0.1.450",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.449",
+ "version": "0.1.450",
"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 3081caac1..c706445fe 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.449",
+ "version": "0.1.450",
"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 471696e94..ce99e8878 100644
--- a/mcp-host/src/llm/__tests__/codexLlmProxyClient.test.ts
+++ b/mcp-host/src/llm/__tests__/codexLlmProxyClient.test.ts
@@ -686,6 +686,27 @@ describe('CodexLlmProxyClient rate limits', () => {
expect(err).toMatchObject({ code: 'budget_denied' })
})
+ // G1-11 (#720, review R1-B1): a limiter in the control-api shape answers a
+ // reason phrase, not a code. On a 429 only a machine code wins.
+ it('G1-11c reads a 429 whose JSON error is a reason phrase as rate_limited', async () => {
+ const { err, fetchFn } = await failure(
+ Response.json(
+ { error: 'Too Many Requests', retryAfterSeconds: 4 },
+ { status: 429, headers: { 'retry-after': '4' } }
+ )
+ )
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(err).toMatchObject({ code: 'rate_limited', retryAfterMs: 4000 })
+ })
+
+ it('G1-11c leaves the JSON error of a non-429 as it is', async () => {
+ const { err, fetchFn } = await failure(
+ Response.json({ error: 'Service Unavailable' }, { status: 503 })
+ )
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(err).toMatchObject({ code: 'Service Unavailable' })
+ })
+
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>', {
diff --git a/mcp-host/src/llm/__tests__/codexSubscription.test.ts b/mcp-host/src/llm/__tests__/codexSubscription.test.ts
index ed3a28615..35f7c42a7 100644
--- a/mcp-host/src/llm/__tests__/codexSubscription.test.ts
+++ b/mcp-host/src/llm/__tests__/codexSubscription.test.ts
@@ -21,7 +21,11 @@ import type { ChatMessage } from '../../core/types'
import { CodexProxyError } from '../codexLlmProxyClient'
import { CodexSubscriptionProvider, attachmentBudgetRefusalMessage } from '../codexSubscription'
import { classifyFailoverClass } from '../failover/classify'
-import { CodexAuthorizeError } from '../providerAttemptAuthorizer'
+import {
+ CodexAuthorizeError,
+ ProviderAttemptAuthorizer,
+ resolveCodexAuthorizeUrl,
+} from '../providerAttemptAuthorizer'
import { makeProvider } from '../registry'
import {
JPEG_2X2_BASE64,
@@ -1615,4 +1619,122 @@ describe('CodexSubscriptionProvider Retry-After retry (G1-9, #720)', () => {
expect(wired.authorize).toHaveBeenCalledTimes(1)
expect(wired.stream).toHaveBeenCalledTimes(1)
})
+
+ // G1-11 (#720, review R1-B1): the 429 that control-api's own limiters answer
+ // on authorize, in the shape they send it, takes the same single retry as a
+ // proxy 429. The authorizer is the real one; only its fetch is faked.
+ const controlApiLimiter429 = (seconds: number) =>
+ Response.json(
+ { error: 'Too Many Requests', retryAfterSeconds: seconds },
+ { status: 429, headers: { 'retry-after': String(seconds) } }
+ )
+ const authorizedSecond = () =>
+ Response.json({
+ providerAttemptId: 'attempt-2',
+ requestHash,
+ executionTicket: 'ticket-second',
+ expiresAt: '2026-08-20T10:00:00.000Z',
+ })
+ const realAuthorize = (fetchFn: ReturnType<typeof vi.fn>) => {
+ const authorizer = new ProviderAttemptAuthorizer({
+ authorizeUrl: resolveCodexAuthorizeUrl('http://gateway:8092'),
+ readPlatformJwt: () => 'platform-jwt',
+ fetchFn: fetchFn as unknown as typeof fetch,
+ })
+ return vi.fn((body, options) => authorizer.authorize(body, options))
+ }
+
+ it('G1-11a: a control-api limiter 429 on authorize is retried once after its Retry-After', async () => {
+ vi.useFakeTimers()
+ const fetchFn = vi
+ .fn()
+ .mockResolvedValueOnce(controlApiLimiter429(2))
+ .mockResolvedValueOnce(authorizedSecond())
+ const wired = deps({ authorize: realAuthorize(fetchFn), stream: vi.fn().mockResolvedValue(ok) })
+ const provider = new CodexSubscriptionProvider('gpt-5.3-codex', wired as never)
+
+ const turn = provider.completeSingleTurn([{ role: 'user', content: 'hi' }])
+ await vi.advanceTimersByTimeAsync(1999)
+ // Witness: the first authorize ran; nothing was dispatched on its 429.
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(wired.stream).toHaveBeenCalledTimes(0)
+ await vi.advanceTimersByTimeAsync(1)
+ await expect(turn).resolves.toMatchObject({ content: 'after the wait' })
+
+ expect(fetchFn).toHaveBeenCalledTimes(2)
+ const [first, second] = fetchFn.mock.calls.map(call => JSON.parse(call[1].body as string))
+ expect(second.invocationId).toBe(first.invocationId)
+ expect([first.providerAttemptIndex, second.providerAttemptIndex]).toEqual([1, 2])
+ expect(wired.stream).toHaveBeenCalledTimes(1)
+ expect(wired.stream.mock.calls[0][0].executionTicket).toBe('ticket-second')
+ })
+
+ it('G1-11b: a control-api limiter 429 over the cap is thrown as RateLimited with no retry', async () => {
+ vi.useFakeTimers()
+ const fetchFn = vi
+ .fn()
+ .mockResolvedValueOnce(controlApiLimiter429(31))
+ .mockResolvedValueOnce(authorizedSecond())
+ const wired = deps({ authorize: realAuthorize(fetchFn) })
+ const provider = new CodexSubscriptionProvider('gpt-5.3-codex', wired as never)
+
+ const turn = provider.completeSingleTurn([{ role: 'user', content: 'hi' }])
+ const caught = turn.then(
+ () => undefined,
+ (e: unknown) => e
+ )
+ await vi.advanceTimersByTimeAsync(60_000)
+ const err = await caught
+
+ expect(err).toBeInstanceOf(CodexAuthorizeError)
+ expect(err).toMatchObject({ code: 'rate_limited', retryAfterMs: 31_000 })
+ expect(provider.classifyError(err)).toMatchObject({
+ code: LlmErrorCode.RateLimited,
+ retryable: true,
+ providerDispatched: false,
+ })
+ // Witness: the first authorize ran; the negative is that no second one did.
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(wired.stream).toHaveBeenCalledTimes(0)
+ })
+
+ it('G1-12: an abort after the wait resolves and before the second authorize sends nothing more', async () => {
+ vi.useFakeTimers()
+ const controller = new AbortController()
+ const reason = new Error('caller gave up')
+ let abortInGap = false
+ const remove = controller.signal.removeEventListener.bind(controller.signal)
+ vi.spyOn(controller.signal, 'removeEventListener').mockImplementation(
+ (type, listener, opts) => {
+ remove(type, listener, opts)
+ // The wait drops its abort listener once its timer fired: abort right
+ // then, in the gap no listener watches.
+ if (abortInGap) {
+ abortInGap = false
+ controller.abort(reason)
+ }
+ }
+ )
+ const stream = vi
+ .fn()
+ .mockImplementationOnce(async () => {
+ abortInGap = true
+ throw limited(1000)
+ })
+ .mockResolvedValueOnce(ok)
+ const wired = deps({ authorize: authorizeTwice(), stream })
+ const provider = new CodexSubscriptionProvider('gpt-5.3-codex', wired as never)
+
+ const turn = provider.completeSingleTurn([{ role: 'user', content: 'hi' }], {
+ signal: controller.signal,
+ })
+ const settled = expect(turn).rejects.toBe(reason)
+ await vi.advanceTimersByTimeAsync(1000)
+ await settled
+ // Witness: the abort did land in the gap, after the first attempt ran.
+ expect(controller.signal.aborted).toBe(true)
+ expect(abortInGap).toBe(false)
+ expect(wired.authorize).toHaveBeenCalledTimes(1)
+ expect(wired.stream).toHaveBeenCalledTimes(1)
+ })
})
diff --git a/mcp-host/src/llm/__tests__/grokLlmProxyClient.test.ts b/mcp-host/src/llm/__tests__/grokLlmProxyClient.test.ts
index 588f306fd..d58c7b193 100644
--- a/mcp-host/src/llm/__tests__/grokLlmProxyClient.test.ts
+++ b/mcp-host/src/llm/__tests__/grokLlmProxyClient.test.ts
@@ -586,6 +586,27 @@ describe('GrokLlmProxyClient rate limits', () => {
expect(err).toMatchObject({ code: 'budget_denied' })
})
+ // G1-11 (#720, review R1-B1): a limiter in the control-api shape answers a
+ // reason phrase, not a code. On a 429 only a machine code wins.
+ it('G1-11c reads a 429 whose JSON error is a reason phrase as rate_limited', async () => {
+ const { err, fetchFn } = await failure(
+ Response.json(
+ { error: 'Too Many Requests', retryAfterSeconds: 4 },
+ { status: 429, headers: { 'retry-after': '4' } }
+ )
+ )
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(err).toMatchObject({ code: 'rate_limited', retryAfterMs: 4000 })
+ })
+
+ it('G1-11c leaves the JSON error of a non-429 as it is', async () => {
+ const { err, fetchFn } = await failure(
+ Response.json({ error: 'Service Unavailable' }, { status: 503 })
+ )
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(err).toMatchObject({ code: 'Service Unavailable' })
+ })
+
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>', {
diff --git a/mcp-host/src/llm/__tests__/grokSubscription.test.ts b/mcp-host/src/llm/__tests__/grokSubscription.test.ts
index d7d313993..e4cca2e31 100644
--- a/mcp-host/src/llm/__tests__/grokSubscription.test.ts
+++ b/mcp-host/src/llm/__tests__/grokSubscription.test.ts
@@ -5,7 +5,11 @@ import { LlmErrorCode } from '../../core/errors'
import { classifyFailoverClass } from '../failover/classify'
import { GrokProxyError } from '../grokLlmProxyClient'
import { GrokSubscriptionProvider } from '../grokSubscription'
-import { CodexAuthorizeError } from '../providerAttemptAuthorizer'
+import {
+ CodexAuthorizeError,
+ ProviderAttemptAuthorizer,
+ resolveCodexAuthorizeUrl,
+} from '../providerAttemptAuthorizer'
const requestHash = 'a'.repeat(64)
@@ -1000,4 +1004,122 @@ describe('GrokSubscriptionProvider Retry-After retry (G1-9, #720)', () => {
expect(wired.authorizer.authorize).toHaveBeenCalledTimes(1)
expect(wired.proxy.stream).toHaveBeenCalledTimes(1)
})
+
+ // G1-11 (#720, review R1-B1): the 429 that control-api's own limiters answer
+ // on authorize, in the shape they send it, takes the same single retry as a
+ // proxy 429. The authorizer is the real one; only its fetch is faked.
+ const controlApiLimiter429 = (seconds: number) =>
+ Response.json(
+ { error: 'Too Many Requests', retryAfterSeconds: seconds },
+ { status: 429, headers: { 'retry-after': String(seconds) } }
+ )
+ const authorizedSecond = () =>
+ Response.json({
+ providerAttemptId: 'attempt-2',
+ requestHash,
+ executionTicket: 'ticket-second',
+ expiresAt: '2026-08-20T10:00:00.000Z',
+ })
+ const realAuthorize = (fetchFn: ReturnType<typeof vi.fn>) => {
+ const authorizer = new ProviderAttemptAuthorizer({
+ authorizeUrl: resolveCodexAuthorizeUrl('http://gateway:8092'),
+ readPlatformJwt: () => 'platform-jwt',
+ fetchFn: fetchFn as unknown as typeof fetch,
+ })
+ return vi.fn((body, options) => authorizer.authorize(body, options))
+ }
+
+ it('G1-11a: a control-api limiter 429 on authorize is retried once after its Retry-After', async () => {
+ vi.useFakeTimers()
+ const fetchFn = vi
+ .fn()
+ .mockResolvedValueOnce(controlApiLimiter429(2))
+ .mockResolvedValueOnce(authorizedSecond())
+ const wired = deps({ authorize: realAuthorize(fetchFn), stream: vi.fn().mockResolvedValue(ok) })
+ const provider = new GrokSubscriptionProvider('grok-4.6', wired as never)
+
+ const turn = provider.completeSingleTurn([{ role: 'user', content: 'hi' }])
+ await vi.advanceTimersByTimeAsync(1999)
+ // Witness: the first authorize ran; nothing was dispatched on its 429.
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(wired.proxy.stream).toHaveBeenCalledTimes(0)
+ await vi.advanceTimersByTimeAsync(1)
+ await expect(turn).resolves.toMatchObject({ content: 'after the wait' })
+
+ expect(fetchFn).toHaveBeenCalledTimes(2)
+ const [first, second] = fetchFn.mock.calls.map(call => JSON.parse(call[1].body as string))
+ expect(second.invocationId).toBe(first.invocationId)
+ expect([first.providerAttemptIndex, second.providerAttemptIndex]).toEqual([1, 2])
+ expect(wired.proxy.stream).toHaveBeenCalledTimes(1)
+ expect(wired.proxy.stream.mock.calls[0][0].executionTicket).toBe('ticket-second')
+ })
+
+ it('G1-11b: a control-api limiter 429 over the cap is thrown as RateLimited with no retry', async () => {
+ vi.useFakeTimers()
+ const fetchFn = vi
+ .fn()
+ .mockResolvedValueOnce(controlApiLimiter429(31))
+ .mockResolvedValueOnce(authorizedSecond())
+ const wired = deps({ authorize: realAuthorize(fetchFn) })
+ const provider = new GrokSubscriptionProvider('grok-4.6', wired as never)
+
+ const turn = provider.completeSingleTurn([{ role: 'user', content: 'hi' }])
+ const caught = turn.then(
+ () => undefined,
+ (e: unknown) => e
+ )
+ await vi.advanceTimersByTimeAsync(60_000)
+ const err = await caught
+
+ expect(err).toBeInstanceOf(CodexAuthorizeError)
+ expect(err).toMatchObject({ code: 'rate_limited', retryAfterMs: 31_000 })
+ expect(provider.classifyError(err)).toMatchObject({
+ code: LlmErrorCode.RateLimited,
+ retryable: true,
+ providerDispatched: false,
+ })
+ // Witness: the first authorize ran; the negative is that no second one did.
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(wired.proxy.stream).toHaveBeenCalledTimes(0)
+ })
+
+ it('G1-12: an abort after the wait resolves and before the second authorize sends nothing more', async () => {
+ vi.useFakeTimers()
+ const controller = new AbortController()
+ const reason = new Error('caller gave up')
+ let abortInGap = false
+ const remove = controller.signal.removeEventListener.bind(controller.signal)
+ vi.spyOn(controller.signal, 'removeEventListener').mockImplementation(
+ (type, listener, opts) => {
+ remove(type, listener, opts)
+ // The wait drops its abort listener once its timer fired: abort right
+ // then, in the gap no listener watches.
+ if (abortInGap) {
+ abortInGap = false
+ controller.abort(reason)
+ }
+ }
+ )
+ const stream = vi
+ .fn()
+ .mockImplementationOnce(async () => {
+ abortInGap = true
+ throw limited(1000)
+ })
+ .mockResolvedValueOnce(ok)
+ const wired = deps({ authorize: authorizeTwice(), stream })
+ const provider = new GrokSubscriptionProvider('grok-4.6', wired as never)
+
+ const turn = provider.completeSingleTurn([{ role: 'user', content: 'hi' }], {
+ signal: controller.signal,
+ })
+ const settled = expect(turn).rejects.toBe(reason)
+ await vi.advanceTimersByTimeAsync(1000)
+ await settled
+ // Witness: the abort did land in the gap, after the first attempt ran.
+ expect(controller.signal.aborted).toBe(true)
+ expect(abortInGap).toBe(false)
+ expect(wired.authorizer.authorize).toHaveBeenCalledTimes(1)
+ expect(wired.proxy.stream).toHaveBeenCalledTimes(1)
+ })
})
diff --git a/mcp-host/src/llm/__tests__/providerAttemptAuthorizer.test.ts b/mcp-host/src/llm/__tests__/providerAttemptAuthorizer.test.ts
index 51208c31c..3a7fc9dc3 100644
--- a/mcp-host/src/llm/__tests__/providerAttemptAuthorizer.test.ts
+++ b/mcp-host/src/llm/__tests__/providerAttemptAuthorizer.test.ts
@@ -313,6 +313,50 @@ describe('ProviderAttemptAuthorizer', () => {
expect(err).toMatchObject({ code: 'budget_denied' })
})
+ // G1-11 (#720, review R1-B1): control-api's authorize limiters
+ // (`workflowGrantEdgeRateLimitHandler`, `rateLimitMiddleware`) answer 429
+ // with a reason phrase and `Retry-After`. On a 429 only a machine code wins.
+ it('G1-11d reads the control-api limiter 429 as rate_limited with its Retry-After', async () => {
+ const { err, fetchFn } = await authorizeFailure(
+ Response.json(
+ { error: 'Too Many Requests', retryAfterSeconds: 9 },
+ { status: 429, headers: { 'retry-after': '9' } }
+ )
+ )
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(err).toBeInstanceOf(CodexAuthorizeError)
+ expect(err).toMatchObject({
+ code: 'rate_limited',
+ retryAfterMs: 9000,
+ message: 'authorize failed with 429',
+ })
+ })
+
+ it('G1-11d carries the Retry-After of an HTML 429 and of a JSON code', async () => {
+ const html = await authorizeFailure(
+ new Response('<html><body>Too Many Requests</body></html>', {
+ status: 429,
+ headers: { 'content-type': 'text/html', 'retry-after': '3' },
+ })
+ )
+ const coded = await authorizeFailure(
+ Response.json({ error: 'budget_denied' }, { status: 429, headers: { 'retry-after': '3' } })
+ )
+ expect(html.fetchFn).toHaveBeenCalledTimes(1)
+ expect(coded.fetchFn).toHaveBeenCalledTimes(1)
+ expect(html.err).toMatchObject({ code: 'rate_limited', retryAfterMs: 3000 })
+ expect(coded.err).toMatchObject({ code: 'budget_denied', retryAfterMs: 3000 })
+ })
+
+ it('G1-11d leaves the JSON error of a non-429 as it is, with no Retry-After', async () => {
+ const { err, fetchFn } = await authorizeFailure(
+ Response.json({ error: 'Unauthorized' }, { status: 401, headers: { 'retry-after': '3' } })
+ )
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(err).toMatchObject({ code: 'Unauthorized' })
+ expect((err as CodexAuthorizeError).retryAfterMs).toBeUndefined()
+ })
+
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' }), {