Skip to content

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

TypeScript

fix: answer upstream_rejected as 422 and carry the upstream status (#720)

Alfredo Lopezd28532e820ba
diff --git a/codex-llm-proxy/src/codexTransport.ts b/codex-llm-proxy/src/codexTransport.ts
index 8505ccc57..ddfbf6088 100644
--- a/codex-llm-proxy/src/codexTransport.ts
+++ b/codex-llm-proxy/src/codexTransport.ts
@@ -409,7 +409,8 @@ async function dispatchUpstreamStream(
if (response.status >= 400 && response.status < 500 && response.status !== 408) {
throw new CodexTransportError(
'upstream_rejected',
- `upstream rejected the Codex request with status ${response.status}`
+ `upstream rejected the Codex request with status ${response.status}`,
+ { upstreamStatus: response.status }
)
}
throw new CodexTransportError('provider_unavailable', 'upstream completion failed')
diff --git a/codex-llm-proxy/src/server.ts b/codex-llm-proxy/src/server.ts
index efe47ceb0..5a902616f 100644
--- a/codex-llm-proxy/src/server.ts
+++ b/codex-llm-proxy/src/server.ts
@@ -623,8 +623,15 @@ export function createProxyApps(
},
'codex attempt finished'
)
+ // R1-H2: the upstream status of an upstream_rejected travels on both
+ // paths, so the Host can tell an entitlement refusal from a 404.
+ const upstreamStatus =
+ err instanceof CodexTransportError && err.code === 'upstream_rejected'
+ ? err.details?.upstreamStatus
+ : undefined
+ const rejectedBy = typeof upstreamStatus === 'number' ? { upstreamStatus } : {}
if (deliveredAs === 'sse_error') {
- res.write(`data: ${JSON.stringify({ type: 'error', code: mapped.code })}\n\n`)
+ res.write(`data: ${JSON.stringify({ type: 'error', code: mapped.code, ...rejectedBy })}\n\n`)
res.end()
return
}
@@ -637,7 +644,7 @@ export function createProxyApps(
const retryAfter =
err instanceof CodexTransportError ? err.details?.retryAfterSeconds : undefined
if (typeof retryAfter === 'number') res.setHeader('retry-after', String(retryAfter))
- res.status(mapped.status).json({ error: mapped.code })
+ res.status(mapped.status).json({ error: mapped.code, ...rejectedBy })
} finally {
stopHeartbeat?.()
release?.()
@@ -796,8 +803,9 @@ const ATTEMPT_ERROR_STATUS: Record<string, number> = {
// An upstream 429 (G1-1, #720); its Retry-After travels as a header.
rate_limited: 429,
tool_call_limit_exceeded: 422,
- // An upstream 4xx the same request would get again (G1-3, #720).
- upstream_rejected: 502,
+ // An upstream 4xx the same request would get again (G1-3, #720). Not 502:
+ // the gateways answer 502 when nothing behind them answered (R1-H2).
+ upstream_rejected: 422,
// A control-api call no live process received (G1-4, #720).
control_plane_unavailable: 503,
invalid_tool_arguments: 422,
diff --git a/docs/architecture/codex-subscription-transport-contract.md b/docs/architecture/codex-subscription-transport-contract.md
index 64f721870..496e1ded9 100644
--- a/docs/architecture/codex-subscription-transport-contract.md
+++ b/docs/architecture/codex-subscription-transport-contract.md
@@ -467,10 +467,14 @@ code the proxy constructs, and every code it refuses a request with
reply: once SSE bytes are on the wire, the error frame carries the code
alone. The limiter's reply carries the `Retry-After` and draft-7
`RateLimit`/`RateLimit-Policy` headers that express-rate-limit sets.
-- `upstream_rejected`: HTTP 502. The upstream answered the completion with a
+- `upstream_rejected`: HTTP 422. The upstream answered the completion with a
4xx that no narrower code covers (for example 404, 409 or 422). The same
- request would get the same answer, so it is not a provider outage. The
- status is in the log reason. An upstream 408 is transient and stays
+ request would get the same answer, so it is not a provider outage. It is
+ not 502, because the gateways answer 502 when nothing behind them answered.
+ The upstream status travels as `upstreamStatus` on both paths
+ (`{"error":"upstream_rejected","upstreamStatus":404}`, or the same field on
+ the SSE error frame) and in the log line's `details`; the Host exposes it as
+ the classified error's `httpStatus`. An upstream 408 is transient and stays
`provider_unavailable`.
- `control_plane_unavailable`: HTTP 503. The redeem could not reach
control-api; see "Control-plane outage" below.
diff --git a/docs/architecture/grok-subscription-transport-contract.md b/docs/architecture/grok-subscription-transport-contract.md
index cb2f4fd3a..d50085e09 100644
--- a/docs/architecture/grok-subscription-transport-contract.md
+++ b/docs/architecture/grok-subscription-transport-contract.md
@@ -334,10 +334,14 @@ code the proxy constructs, and every code it refuses a request with
reply: once SSE bytes are on the wire, the error frame carries the code
alone. The limiter's reply carries the `Retry-After` and draft-7
`RateLimit`/`RateLimit-Policy` headers that express-rate-limit sets.
-- `upstream_rejected`: HTTP 502. The upstream answered the completion with a
+- `upstream_rejected`: HTTP 422. The upstream answered the completion with a
4xx that no narrower code covers, including a 402/403 entitlement refusal.
The same request would get the same answer, so it is not a provider outage.
- The status is in the log reason. The upstream status mapping is: 400
+ It is not 502, because the gateways answer 502 when nothing behind them
+ answered. The upstream status travels as `upstreamStatus` on both paths
+ (`{"error":"upstream_rejected","upstreamStatus":403}`, or the same field on
+ the SSE error frame) and in the log line's `details`; the Host exposes it as
+ the classified error's `httpStatus`. The upstream status mapping is: 400
`invalid_request`, 401 `connection_unavailable`, 426
`client_upgrade_required`, 429 `rate_limited`, any other 4xx except 408
`upstream_rejected`, anything else (408 and 5xx included)
diff --git a/grok-llm-proxy/src/grokTransport.ts b/grok-llm-proxy/src/grokTransport.ts
index e63afed35..eb6a44515 100644
--- a/grok-llm-proxy/src/grokTransport.ts
+++ b/grok-llm-proxy/src/grokTransport.ts
@@ -449,7 +449,8 @@ async function dispatchUpstreamStream(
if (response.status >= 400 && response.status < 500 && response.status !== 408) {
throw new GrokTransportError(
'upstream_rejected',
- `upstream rejected the Grok request with status ${response.status}`
+ `upstream rejected the Grok request with status ${response.status}`,
+ { upstreamStatus: response.status }
)
}
throw new GrokTransportError('provider_unavailable', 'upstream completion failed')
diff --git a/grok-llm-proxy/src/server.ts b/grok-llm-proxy/src/server.ts
index 86e7ccad4..c5f710e74 100644
--- a/grok-llm-proxy/src/server.ts
+++ b/grok-llm-proxy/src/server.ts
@@ -496,8 +496,15 @@ export function createProxyApps(
},
'grok attempt finished'
)
+ // R1-H2: the upstream status of an upstream_rejected travels on both
+ // paths, so the Host can tell an entitlement refusal from a 404.
+ const upstreamStatus =
+ err instanceof GrokTransportError && err.code === 'upstream_rejected'
+ ? err.details?.upstreamStatus
+ : undefined
+ const rejectedBy = typeof upstreamStatus === 'number' ? { upstreamStatus } : {}
if (deliveredAs === 'sse_error') {
- res.write(`data: ${JSON.stringify({ type: 'error', code: mapped.code })}\n\n`)
+ res.write(`data: ${JSON.stringify({ type: 'error', code: mapped.code, ...rejectedBy })}\n\n`)
res.end()
return
}
@@ -510,7 +517,7 @@ export function createProxyApps(
const retryAfter =
err instanceof GrokTransportError ? err.details?.retryAfterSeconds : undefined
if (typeof retryAfter === 'number') res.setHeader('retry-after', String(retryAfter))
- res.status(mapped.status).json({ error: mapped.code })
+ res.status(mapped.status).json({ error: mapped.code, ...rejectedBy })
} finally {
stopHeartbeat?.()
release?.()
@@ -669,8 +676,9 @@ const ATTEMPT_ERROR_STATUS: Record<string, number> = {
// An upstream 429 (G1-1, #720); its Retry-After travels as a header.
rate_limited: 429,
tool_call_limit_exceeded: 422,
- // An upstream 4xx the same request would get again (G1-3, #720).
- upstream_rejected: 502,
+ // An upstream 4xx the same request would get again (G1-3, #720). Not 502:
+ // the gateways answer 502 when nothing behind them answered (R1-H2).
+ upstream_rejected: 422,
// A control-api call no live process received (G1-4, #720).
control_plane_unavailable: 503,
tool_call_arguments_exceeded: 422,
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index d182ccfca..c37c5eea7 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.454",
+ "version": "0.1.455",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.454",
+ "version": "0.1.455",
"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 52d071419..dd7061a6e 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.454",
+ "version": "0.1.455",
"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/codexLlmProxyClient.ts b/mcp-host/src/llm/codexLlmProxyClient.ts
index 115c6a342..3605e99af 100644
--- a/mcp-host/src/llm/codexLlmProxyClient.ts
+++ b/mcp-host/src/llm/codexLlmProxyClient.ts
@@ -4,6 +4,7 @@ import {
} from '@clerum/llm-provider-attempt-contract'
import { fetchCauseCode, isConnectPhaseFailure } from './controlPlaneReachability'
import { rateLimitedCode, retryAfterMs } from './retryAfter'
+import { upstreamRejectedStatus } from './upstreamRejected'
export const CODEX_PROXY_COMPLETIONS_PATH = '/internal/runtime/v1/codex/completions'
@@ -19,7 +20,9 @@ export class CodexProxyError extends Error {
message: string,
readonly dispatched: boolean = true,
// The delay a 429 advised through Retry-After (G1-6).
- readonly retryAfterMs?: number
+ readonly retryAfterMs?: number,
+ // The upstream 4xx behind an upstream_rejected (R1-H2).
+ readonly upstreamStatus?: number
) {
super(message)
this.name = 'CodexProxyError'
@@ -34,7 +37,7 @@ export type CodexProxyFrame =
outcome: 'success' | 'canceled' | 'error' | 'unknown'
usage?: { inputTokens: number; outputTokens: number }
}
- | { type: 'error'; code: string }
+ | { type: 'error'; code: string; upstreamStatus?: unknown }
export type CodexProxyStreamResult = {
text: string
@@ -164,7 +167,8 @@ export class CodexLlmProxyClient {
? 'Codex request is too large; use fewer or smaller images, or reduce context'
: `proxy stream failed with ${response.status} (${code})`,
true,
- response.status === 429 ? retryAfterMs(response) : undefined
+ response.status === 429 ? retryAfterMs(response) : undefined,
+ upstreamRejectedStatus(code, payload.upstreamStatus)
)
}
if (!response.body) {
@@ -199,7 +203,13 @@ async function readProxySse(body: ReadableStream<Uint8Array>): Promise<CodexProx
if (!line) continue
const frame = JSON.parse(line.slice(6)) as CodexProxyFrame
if (frame.type === 'error') {
- throw new CodexProxyError(frame.code, `proxy stream failed with ${frame.code}`)
+ throw new CodexProxyError(
+ frame.code,
+ `proxy stream failed with ${frame.code}`,
+ true,
+ undefined,
+ upstreamRejectedStatus(frame.code, frame.upstreamStatus)
+ )
}
if (frame.type === 'text') text += frame.text
if (frame.type === 'tool_call') toolCalls.push(frame)
diff --git a/mcp-host/src/llm/codexSubscription.ts b/mcp-host/src/llm/codexSubscription.ts
index b2c014739..3003d323a 100644
--- a/mcp-host/src/llm/codexSubscription.ts
+++ b/mcp-host/src/llm/codexSubscription.ts
@@ -474,12 +474,15 @@ export class CodexSubscriptionProvider implements SingleTurnProvider {
}
// An upstream 4xx the proxy could not map (#720): the same request gets
// the same answer, so it is terminal whatever the generic arm decides.
+ // httpStatus is the upstream's own status, when the proxy sent it (R1-H2).
if (code === 'upstream_rejected') {
+ const upstreamStatus = err instanceof CodexProxyError ? err.upstreamStatus : undefined
return {
code: LlmErrorCode.ApiCallFailed,
retryable: false,
message: err instanceof Error ? err.message : String(err),
providerCode: code,
+ ...(upstreamStatus !== undefined ? { httpStatus: upstreamStatus } : {}),
...(providerDispatched !== undefined ? { providerDispatched } : {}),
}
}
diff --git a/mcp-host/src/llm/grokLlmProxyClient.ts b/mcp-host/src/llm/grokLlmProxyClient.ts
index 990c26fa2..2db15ab40 100644
--- a/mcp-host/src/llm/grokLlmProxyClient.ts
+++ b/mcp-host/src/llm/grokLlmProxyClient.ts
@@ -1,5 +1,6 @@
import { fetchCauseCode, isConnectPhaseFailure } from './controlPlaneReachability'
import { rateLimitedCode, retryAfterMs } from './retryAfter'
+import { upstreamRejectedStatus } from './upstreamRejected'
export const GROK_PROXY_COMPLETIONS_PATH = '/internal/runtime/v1/grok/completions'
@@ -36,7 +37,9 @@ export class GrokProxyError extends Error {
message: string,
readonly dispatched: boolean = true,
// The delay a 429 advised through Retry-After (G1-6).
- readonly retryAfterMs?: number
+ readonly retryAfterMs?: number,
+ // The upstream 4xx behind an upstream_rejected (R1-H2).
+ readonly upstreamStatus?: number
) {
super(message)
this.name = 'GrokProxyError'
@@ -51,7 +54,7 @@ export type GrokProxyFrame =
outcome: 'success' | 'canceled' | 'error' | 'unknown'
usage?: { inputTokens: number; outputTokens: number }
}
- | { type: 'error'; code: string }
+ | { type: 'error'; code: string; upstreamStatus?: unknown }
export type GrokProxyStreamResult = {
text: string
@@ -152,7 +155,8 @@ export class GrokLlmProxyClient {
code,
grokProxyErrorMessage(code, response.status),
true,
- response.status === 429 ? retryAfterMs(response) : undefined
+ response.status === 429 ? retryAfterMs(response) : undefined,
+ upstreamRejectedStatus(code, payload.upstreamStatus)
)
}
if (!response.body) {
@@ -187,7 +191,13 @@ async function readProxySse(body: ReadableStream<Uint8Array>): Promise<GrokProxy
if (!line) continue
const frame = JSON.parse(line.slice(6)) as GrokProxyFrame
if (frame.type === 'error') {
- throw new GrokProxyError(frame.code, grokProxyErrorMessage(frame.code))
+ throw new GrokProxyError(
+ frame.code,
+ grokProxyErrorMessage(frame.code),
+ true,
+ undefined,
+ upstreamRejectedStatus(frame.code, frame.upstreamStatus)
+ )
}
if (frame.type === 'text') text += frame.text
if (frame.type === 'tool_call') toolCalls.push(frame)
diff --git a/mcp-host/src/llm/grokSubscription.ts b/mcp-host/src/llm/grokSubscription.ts
index a22d66523..57dbdbadf 100644
--- a/mcp-host/src/llm/grokSubscription.ts
+++ b/mcp-host/src/llm/grokSubscription.ts
@@ -339,12 +339,15 @@ export class GrokSubscriptionProvider implements SingleTurnProvider {
}
// An upstream 4xx the proxy could not map (#720): the same request gets
// the same answer, so it is terminal whatever the generic arm decides.
+ // httpStatus is the upstream's own status, when the proxy sent it (R1-H2).
if (code === 'upstream_rejected') {
+ const upstreamStatus = err instanceof GrokProxyError ? err.upstreamStatus : undefined
return {
code: LlmErrorCode.ApiCallFailed,
retryable: false,
message: err instanceof Error ? err.message : String(err),
providerCode: code,
+ ...(upstreamStatus !== undefined ? { httpStatus: upstreamStatus } : {}),
...(providerDispatched !== undefined ? { providerDispatched } : {}),
}
}
diff --git a/mcp-host/src/llm/upstreamRejected.ts b/mcp-host/src/llm/upstreamRejected.ts
new file mode 100644
index 000000000..50f570bb3
--- /dev/null
+++ b/mcp-host/src/llm/upstreamRejected.ts
@@ -0,0 +1,15 @@
+/**
+ * The upstream status a proxy sends with `upstream_rejected` (review R1-H2,
+ * #720), so a 402/403 entitlement refusal and a 404 missing route stay apart
+ * downstream. Only an integer 4xx is accepted, and only on that code; any
+ * other value is treated as absent, never guessed.
+ */
+export function upstreamRejectedStatus(code: string, value: unknown): number | undefined {
+ return code === 'upstream_rejected' &&
+ typeof value === 'number' &&
+ Number.isInteger(value) &&
+ value >= 400 &&
+ value <= 499
+ ? value
+ : undefined
+}