Skip to content

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

TypeScript

test(mcp-host): pin the review round 2 edges of the #720 taxonomy (#720)

Alfredo Lopez31e2322ad6de
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index 258da8a34..2040221ae 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.461",
+ "version": "0.1.462",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.461",
+ "version": "0.1.462",
"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 a470f72ce..fadcf0a3b 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.461",
+ "version": "0.1.462",
"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 4310eb925..ecafab2c3 100644
--- a/mcp-host/src/llm/__tests__/codexLlmProxyClient.test.ts
+++ b/mcp-host/src/llm/__tests__/codexLlmProxyClient.test.ts
@@ -699,12 +699,31 @@ describe('CodexLlmProxyClient rate limits', () => {
expect(err).toMatchObject({ code: 'rate_limited', retryAfterMs: 4000 })
})
- it('G1-11c leaves the JSON error of a non-429 as it is', async () => {
+ // Review round 2 M7: this pins only that the 429 rule does not fire. Which
+ // code a non-429 JSON error should get is a separate question.
+ it('G1-11c does not apply the 429 rule to a non-429 JSON error', async () => {
const { err, fetchFn } = await failure(
- Response.json({ error: 'Service Unavailable' }, { status: 503 })
+ Response.json(
+ { error: 'Service Unavailable' },
+ { status: 503, headers: { 'retry-after': '4' } }
+ )
)
expect(fetchFn).toHaveBeenCalledTimes(1)
- expect(err).toMatchObject({ code: 'Service Unavailable' })
+ // Witness: the non-ok branch ran and threw the proxy error.
+ expect(err).toBeInstanceOf(CodexProxyError)
+ expect((err as CodexProxyError).code).not.toBe('rate_limited')
+ expect((err as CodexProxyError).retryAfterMs).toBeUndefined()
+ })
+
+ it.each([
+ ['1', 1000],
+ ['3600', 3_600_000],
+ ])('G1-6c carries a Retry-After of exactly %s (review round 2 L7)', async (value, ms) => {
+ 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', retryAfterMs: ms })
})
it('G1-6c keeps an HTML 502 as provider_unavailable with no Retry-After', async () => {
@@ -872,7 +891,16 @@ describe('CodexSubscriptionProvider G1 classification', () => {
})
})
- it.each([200, 404.5, '404', 600])(
+ // Review round 2 L11: both ends of 400..499 are accepted.
+ it.each([400, 499])('G1-8d reads an upstreamStatus of exactly %j', async upstreamStatus => {
+ const err = await proxyReply(
+ Response.json({ error: 'upstream_rejected', upstreamStatus }, { status: 422 })
+ )
+ expect(err).toMatchObject({ code: 'upstream_rejected', upstreamStatus })
+ expect(provider.classifyError(err).httpStatus).toBe(upstreamStatus)
+ })
+
+ it.each([200, 399, 404.5, '404', 500, 600])(
'G1-8d ignores an upstreamStatus of %j that is not an integer 4xx',
async upstreamStatus => {
const err = await proxyReply(
@@ -924,6 +952,11 @@ describe('CodexSubscriptionProvider G1 classification', () => {
code: LlmErrorCode.ControlPlaneUnavailable,
retryable: true,
providerCode: 'control_plane_unavailable',
+ // Review round 2 L10: the request never left the Host, but the proxy
+ // client does not claim so. `true` is the conservative reading
+ // (llm/types.ts): it keeps the idempotency key from being revived after
+ // an authorize that already reserved the attempt. Same as dev.
+ providerDispatched: true,
})
})
diff --git a/mcp-host/src/llm/__tests__/codexSubscription.test.ts b/mcp-host/src/llm/__tests__/codexSubscription.test.ts
index ef048660d..91dc1be51 100644
--- a/mcp-host/src/llm/__tests__/codexSubscription.test.ts
+++ b/mcp-host/src/llm/__tests__/codexSubscription.test.ts
@@ -1615,6 +1615,75 @@ describe('CodexSubscriptionProvider Retry-After retry (G1-9, #720)', () => {
expect(wired.stream).toHaveBeenCalledTimes(1)
})
+ it('G1-9h: a retry whose stream fails with provider_unavailable surfaces that error (review round 2 M5)', async () => {
+ vi.useFakeTimers()
+ const outage = new CodexProxyError('provider_unavailable', 'proxy stream failed with 503')
+ const stream = vi.fn().mockRejectedValueOnce(limited(1000)).mockRejectedValueOnce(outage)
+ 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' }])
+ const settled = expect(turn).rejects.toBe(outage)
+ await vi.advanceTimersByTimeAsync(60_000)
+ await settled
+ // Witness: the retry ran; the negative is that nothing ran after it.
+ expect(wired.authorize).toHaveBeenCalledTimes(2)
+ expect(wired.stream).toHaveBeenCalledTimes(2)
+ expect(provider.classifyError(outage).code).toBe(LlmErrorCode.ModelOverloaded)
+ })
+
+ it('G1-9h: a retry whose authorize fails with control_plane_unavailable surfaces that error (review round 2 M5)', async () => {
+ vi.useFakeTimers()
+ const unreachable = new CodexAuthorizeError(
+ 'control_plane_unavailable',
+ 'authorize could not reach the control plane (ECONNREFUSED)'
+ )
+ const authorize = vi
+ .fn()
+ .mockResolvedValueOnce({
+ providerAttemptId: 'attempt-1',
+ requestHash,
+ executionTicket: 'ticket-first',
+ expiresAt: '2026-08-20T10:00:00.000Z',
+ })
+ .mockRejectedValueOnce(unreachable)
+ const wired = deps({
+ authorize,
+ stream: vi.fn().mockRejectedValueOnce(limited(1000)).mockResolvedValueOnce(ok),
+ })
+ const provider = new CodexSubscriptionProvider('gpt-5.3-codex', wired as never)
+
+ const turn = provider.completeSingleTurn([{ role: 'user', content: 'hi' }])
+ const settled = expect(turn).rejects.toBe(unreachable)
+ await vi.advanceTimersByTimeAsync(60_000)
+ await settled
+ // Witness: the second authorize ran; the negative is that it dispatched nothing.
+ expect(wired.authorize).toHaveBeenCalledTimes(2)
+ expect(wired.stream).toHaveBeenCalledTimes(1)
+ expect(provider.classifyError(unreachable)).toMatchObject({
+ code: LlmErrorCode.ControlPlaneUnavailable,
+ providerDispatched: false,
+ })
+ })
+
+ it('G1-9i: a successful retry passes the caller signal to the second stream (review round 2 L9)', async () => {
+ vi.useFakeTimers()
+ const stream = vi.fn().mockRejectedValueOnce(limited(1000)).mockResolvedValueOnce(ok)
+ const wired = deps({ authorize: authorizeTwice(), stream })
+ const provider = new CodexSubscriptionProvider('gpt-5.3-codex', wired as never)
+ const controller = new AbortController()
+
+ const turn = provider.completeSingleTurn([{ role: 'user', content: 'hi' }], {
+ signal: controller.signal,
+ })
+ await vi.advanceTimersByTimeAsync(1000)
+ await expect(turn).resolves.toMatchObject({ content: 'after the wait' })
+ expect(controller.signal.aborted).toBe(false)
+ expect(wired.authorize).toHaveBeenCalledTimes(2)
+ expect(wired.stream).toHaveBeenCalledTimes(2)
+ expect(wired.stream.mock.calls[1][0].signal).toBe(controller.signal)
+ })
+
// 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.
@@ -1693,6 +1762,33 @@ describe('CodexSubscriptionProvider Retry-After retry (G1-9, #720)', () => {
expect(wired.stream).toHaveBeenCalledTimes(0)
})
+ it('G1-11e: a budget_denied 429 with a short Retry-After is not retried (review round 2 M6)', async () => {
+ vi.useFakeTimers()
+ const fetchFn = vi
+ .fn()
+ .mockResolvedValueOnce(
+ Response.json({ error: 'budget_denied' }, { status: 429, headers: { 'retry-after': '1' } })
+ )
+ .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
+
+ // The delay is within the cap, so only the code keeps it from being retried.
+ expect(err).toBeInstanceOf(CodexAuthorizeError)
+ expect(err).toMatchObject({ code: 'budget_denied', retryAfterMs: 1000 })
+ // 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()
diff --git a/mcp-host/src/llm/__tests__/grokLlmProxyClient.test.ts b/mcp-host/src/llm/__tests__/grokLlmProxyClient.test.ts
index 10d35cda7..f167c0951 100644
--- a/mcp-host/src/llm/__tests__/grokLlmProxyClient.test.ts
+++ b/mcp-host/src/llm/__tests__/grokLlmProxyClient.test.ts
@@ -599,12 +599,31 @@ describe('GrokLlmProxyClient rate limits', () => {
expect(err).toMatchObject({ code: 'rate_limited', retryAfterMs: 4000 })
})
- it('G1-11c leaves the JSON error of a non-429 as it is', async () => {
+ // Review round 2 M7: this pins only that the 429 rule does not fire. Which
+ // code a non-429 JSON error should get is a separate question.
+ it('G1-11c does not apply the 429 rule to a non-429 JSON error', async () => {
const { err, fetchFn } = await failure(
- Response.json({ error: 'Service Unavailable' }, { status: 503 })
+ Response.json(
+ { error: 'Service Unavailable' },
+ { status: 503, headers: { 'retry-after': '4' } }
+ )
)
expect(fetchFn).toHaveBeenCalledTimes(1)
- expect(err).toMatchObject({ code: 'Service Unavailable' })
+ // Witness: the non-ok branch ran and threw the proxy error.
+ expect(err).toBeInstanceOf(GrokProxyError)
+ expect((err as GrokProxyError).code).not.toBe('rate_limited')
+ expect((err as GrokProxyError).retryAfterMs).toBeUndefined()
+ })
+
+ it.each([
+ ['1', 1000],
+ ['3600', 3_600_000],
+ ])('G1-6c carries a Retry-After of exactly %s (review round 2 L7)', async (value, ms) => {
+ 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', retryAfterMs: ms })
})
it('G1-6c keeps an HTML 502 as provider_unavailable with no Retry-After', async () => {
@@ -772,7 +791,16 @@ describe('GrokSubscriptionProvider G1 classification', () => {
})
})
- it.each([200, 404.5, '404', 600])(
+ // Review round 2 L11: both ends of 400..499 are accepted.
+ it.each([400, 499])('G1-8d reads an upstreamStatus of exactly %j', async upstreamStatus => {
+ const err = await proxyReply(
+ Response.json({ error: 'upstream_rejected', upstreamStatus }, { status: 422 })
+ )
+ expect(err).toMatchObject({ code: 'upstream_rejected', upstreamStatus })
+ expect(provider.classifyError(err).httpStatus).toBe(upstreamStatus)
+ })
+
+ it.each([200, 399, 404.5, '404', 500, 600])(
'G1-8d ignores an upstreamStatus of %j that is not an integer 4xx',
async upstreamStatus => {
const err = await proxyReply(
@@ -824,6 +852,11 @@ describe('GrokSubscriptionProvider G1 classification', () => {
code: LlmErrorCode.ControlPlaneUnavailable,
retryable: true,
providerCode: 'control_plane_unavailable',
+ // Review round 2 L10: the request never left the Host, but the proxy
+ // client does not claim so. `true` is the conservative reading
+ // (llm/types.ts): it keeps the idempotency key from being revived after
+ // an authorize that already reserved the attempt. Same as dev.
+ providerDispatched: true,
})
})
diff --git a/mcp-host/src/llm/__tests__/grokSubscription.test.ts b/mcp-host/src/llm/__tests__/grokSubscription.test.ts
index a9453a913..41e6f63de 100644
--- a/mcp-host/src/llm/__tests__/grokSubscription.test.ts
+++ b/mcp-host/src/llm/__tests__/grokSubscription.test.ts
@@ -1000,6 +1000,75 @@ describe('GrokSubscriptionProvider Retry-After retry (G1-9, #720)', () => {
expect(wired.proxy.stream).toHaveBeenCalledTimes(1)
})
+ it('G1-9h: a retry whose stream fails with provider_unavailable surfaces that error (review round 2 M5)', async () => {
+ vi.useFakeTimers()
+ const outage = new GrokProxyError('provider_unavailable', 'proxy stream failed with 503')
+ const stream = vi.fn().mockRejectedValueOnce(limited(1000)).mockRejectedValueOnce(outage)
+ const wired = deps({ authorize: authorizeTwice(), stream })
+ const provider = new GrokSubscriptionProvider('grok-4.6', wired as never)
+
+ const turn = provider.completeSingleTurn([{ role: 'user', content: 'hi' }])
+ const settled = expect(turn).rejects.toBe(outage)
+ await vi.advanceTimersByTimeAsync(60_000)
+ await settled
+ // Witness: the retry ran; the negative is that nothing ran after it.
+ expect(wired.authorizer.authorize).toHaveBeenCalledTimes(2)
+ expect(wired.proxy.stream).toHaveBeenCalledTimes(2)
+ expect(provider.classifyError(outage).code).toBe(LlmErrorCode.ModelOverloaded)
+ })
+
+ it('G1-9h: a retry whose authorize fails with control_plane_unavailable surfaces that error (review round 2 M5)', async () => {
+ vi.useFakeTimers()
+ const unreachable = new CodexAuthorizeError(
+ 'control_plane_unavailable',
+ 'authorize could not reach the control plane (ECONNREFUSED)'
+ )
+ const authorize = vi
+ .fn()
+ .mockResolvedValueOnce({
+ providerAttemptId: 'attempt-1',
+ requestHash,
+ executionTicket: 'ticket-first',
+ expiresAt: '2026-08-20T10:00:00.000Z',
+ })
+ .mockRejectedValueOnce(unreachable)
+ const wired = deps({
+ authorize,
+ stream: vi.fn().mockRejectedValueOnce(limited(1000)).mockResolvedValueOnce(ok),
+ })
+ const provider = new GrokSubscriptionProvider('grok-4.6', wired as never)
+
+ const turn = provider.completeSingleTurn([{ role: 'user', content: 'hi' }])
+ const settled = expect(turn).rejects.toBe(unreachable)
+ await vi.advanceTimersByTimeAsync(60_000)
+ await settled
+ // Witness: the second authorize ran; the negative is that it dispatched nothing.
+ expect(wired.authorizer.authorize).toHaveBeenCalledTimes(2)
+ expect(wired.proxy.stream).toHaveBeenCalledTimes(1)
+ expect(provider.classifyError(unreachable)).toMatchObject({
+ code: LlmErrorCode.ControlPlaneUnavailable,
+ providerDispatched: false,
+ })
+ })
+
+ it('G1-9i: a successful retry passes the caller signal to the second stream (review round 2 L9)', async () => {
+ vi.useFakeTimers()
+ const stream = vi.fn().mockRejectedValueOnce(limited(1000)).mockResolvedValueOnce(ok)
+ const wired = deps({ authorize: authorizeTwice(), stream })
+ const provider = new GrokSubscriptionProvider('grok-4.6', wired as never)
+ const controller = new AbortController()
+
+ const turn = provider.completeSingleTurn([{ role: 'user', content: 'hi' }], {
+ signal: controller.signal,
+ })
+ await vi.advanceTimersByTimeAsync(1000)
+ await expect(turn).resolves.toMatchObject({ content: 'after the wait' })
+ expect(controller.signal.aborted).toBe(false)
+ expect(wired.authorizer.authorize).toHaveBeenCalledTimes(2)
+ expect(wired.proxy.stream).toHaveBeenCalledTimes(2)
+ expect(wired.proxy.stream.mock.calls[1][0].signal).toBe(controller.signal)
+ })
+
// 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.
@@ -1078,6 +1147,33 @@ describe('GrokSubscriptionProvider Retry-After retry (G1-9, #720)', () => {
expect(wired.proxy.stream).toHaveBeenCalledTimes(0)
})
+ it('G1-11e: a budget_denied 429 with a short Retry-After is not retried (review round 2 M6)', async () => {
+ vi.useFakeTimers()
+ const fetchFn = vi
+ .fn()
+ .mockResolvedValueOnce(
+ Response.json({ error: 'budget_denied' }, { status: 429, headers: { 'retry-after': '1' } })
+ )
+ .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
+
+ // The delay is within the cap, so only the code keeps it from being retried.
+ expect(err).toBeInstanceOf(CodexAuthorizeError)
+ expect(err).toMatchObject({ code: 'budget_denied', retryAfterMs: 1000 })
+ // 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()
diff --git a/mcp-host/src/llm/__tests__/providerAttemptAuthorizer.test.ts b/mcp-host/src/llm/__tests__/providerAttemptAuthorizer.test.ts
index 3a7fc9dc3..381ede9f2 100644
--- a/mcp-host/src/llm/__tests__/providerAttemptAuthorizer.test.ts
+++ b/mcp-host/src/llm/__tests__/providerAttemptAuthorizer.test.ts
@@ -348,12 +348,48 @@ describe('ProviderAttemptAuthorizer', () => {
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 () => {
+ // Review round 2 L7: the authorize 429 applies the same Retry-After rule as
+ // the proxy clients, both ends of 1..3600 included.
+ it.each(['0', '3601', 'soon', '1.5', 'Wed, 21 Oct 2026 07:28:00 GMT', ''])(
+ 'G1-11d drops the authorize 429 Retry-After value %j instead of guessing',
+ async value => {
+ const { err, fetchFn } = await authorizeFailure(
+ Response.json(
+ { error: 'Too Many Requests' },
+ { status: 429, headers: { 'retry-after': value } }
+ )
+ )
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ // Witness: the 429 rule ran for this value.
+ expect(err).toMatchObject({ code: 'rate_limited' })
+ expect((err as CodexAuthorizeError).retryAfterMs).toBeUndefined()
+ }
+ )
+
+ it.each([
+ ['1', 1000],
+ ['3600', 3_600_000],
+ ])('G1-11d carries an authorize 429 Retry-After of exactly %s', async (value, ms) => {
+ const { err, fetchFn } = await authorizeFailure(
+ Response.json(
+ { error: 'Too Many Requests' },
+ { status: 429, headers: { 'retry-after': value } }
+ )
+ )
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(err).toMatchObject({ code: 'rate_limited', retryAfterMs: ms })
+ })
+
+ // Review round 2 M7: this pins only that the 429 rule does not fire. Which
+ // code a non-429 JSON error should get is a separate question.
+ it('G1-11d does not apply the 429 rule to a non-429 JSON error', 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' })
+ // Witness: the non-ok branch ran and threw the authorize error.
+ expect(err).toBeInstanceOf(CodexAuthorizeError)
+ expect((err as CodexAuthorizeError).code).not.toBe('rate_limited')
expect((err as CodexAuthorizeError).retryAfterMs).toBeUndefined()
})