Skip to content

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

TypeScript

fix: share one canonical refusal code and log body refusals per request

Alfredo Lopeze263a9f43142
diff --git a/control-api/package-lock.json b/control-api/package-lock.json
index 3a52cd988..de260ddcc 100644
--- a/control-api/package-lock.json
+++ b/control-api/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-api",
- "version": "0.1.683",
+ "version": "0.1.684",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-api",
- "version": "0.1.683",
+ "version": "0.1.684",
"dependencies": {
"@clerum/display-field": "file:../packages/display-field",
"@clerum/grok-provider-attempt-contract": "file:../packages/grok-provider-attempt-contract",
diff --git a/control-api/package.json b/control-api/package.json
index a552b0270..399bf509d 100644
--- a/control-api/package.json
+++ b/control-api/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-api",
- "version": "0.1.683",
+ "version": "0.1.684",
"description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets",
"main": "dist/main.js",
"engines": {
diff --git a/control-api/src/routes/mcp-host/llmProviderAttempts.routes.ts b/control-api/src/routes/mcp-host/llmProviderAttempts.routes.ts
index 56df2364b..7804148bc 100644
--- a/control-api/src/routes/mcp-host/llmProviderAttempts.routes.ts
+++ b/control-api/src/routes/mcp-host/llmProviderAttempts.routes.ts
@@ -183,11 +183,13 @@ export function createMcpHostLlmProviderAttemptRoutes(gateway: K8sGateway): Rout
// parser errors (`request.aborted`, `request.size.invalid`, `stream.*`)
// carry no body and go to the global handler, which logs no body. The log
// carries only the fixed parser `type` and the status: `err.message` of a
- // JSON.parse error can quote a fragment of the body.
- router.use((err: unknown, _req: Request, res: Response, next: NextFunction) => {
+ // JSON.parse error can quote a fragment of the body. It goes through the
+ // request-scoped logger when there is one, so it keeps the correlationId.
+ router.use((err: unknown, req: Request, res: Response, next: NextFunction) => {
const typed = err as { type?: string; status?: number }
+ const requestLog = req.log ?? log
const refuse = (status: number, error: string): void => {
- log.warn({ event: 'llm_provider_attempt_body_refused', type: typed.type, status })
+ requestLog.warn({ event: 'llm_provider_attempt_body_refused', type: typed.type, status })
res.status(status).json({ error })
}
if (typed.type === 'entity.too.large' || typed.status === 413) {
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index 660e6c099..a7fba88cd 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.480",
+ "version": "0.1.481",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.480",
+ "version": "0.1.481",
"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 8efd07c91..0724ea0e8 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.480",
+ "version": "0.1.481",
"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/codexSubscription.ts b/mcp-host/src/llm/codexSubscription.ts
index 1204c0e50..788c6611e 100644
--- a/mcp-host/src/llm/codexSubscription.ts
+++ b/mcp-host/src/llm/codexSubscription.ts
@@ -20,7 +20,7 @@ import {
} from './attachmentBudgetRefusal'
import { CodexLlmProxyClient, CodexProxyError } from './codexLlmProxyClient'
import { classifyUnknown } from './errorClassification'
-import { IMAGE_SOURCE_INVALID, isContextLengthRefusal, projectMessage } from './imageSource'
+import { IMAGE_SOURCE_INVALID, canonicalRefusalCode, projectMessage } from './imageSource'
import { CodexAuthorizeError, ProviderAttemptAuthorizer } from './providerAttemptAuthorizer'
import { rateLimitRetryDelayMs, waitBeforeRetry } from './rateLimitRetry'
import { type LlmProvider, descriptorFor } from './registryCore'
@@ -468,11 +468,7 @@ export class CodexSubscriptionProvider implements SingleTurnProvider {
throw new CodexAuthorizeError(CODEX_ATTACHMENT_TOO_LARGE, attachmentRefusal)
}
throw new CodexAuthorizeError(
- isContextLengthRefusal(canonical.code, canonical.message)
- ? 'request_limit_exceeded'
- : canonical.kind === 'size'
- ? 'payload_too_large'
- : CODEX_REQUEST_INVALID,
+ canonicalRefusalCode(canonical),
`codex completion request rejected: ${canonical.message}`
)
}
diff --git a/mcp-host/src/llm/grokLlmProxyClient.ts b/mcp-host/src/llm/grokLlmProxyClient.ts
index 3af37a3a7..045a19333 100644
--- a/mcp-host/src/llm/grokLlmProxyClient.ts
+++ b/mcp-host/src/llm/grokLlmProxyClient.ts
@@ -4,6 +4,7 @@ import {
buildGrokProxyEnvelope,
} from '@clerum/grok-provider-attempt-contract'
import { fetchCauseCode, isConnectPhaseFailure } from './controlPlaneReachability'
+import { canonicalRefusalCode } from './imageSource'
import { rateLimitedCode, retryAfterMs } from './retryAfter'
import { upstreamRejectedStatus } from './upstreamRejected'
@@ -31,14 +32,14 @@ export function grokProxyErrorMessage(code: string, status?: number): string {
}
/**
- * The code for an envelope the contract refused. Every `limit` failure maps to
- * `payload_too_large` here, including the count, nesting and range kinds; the
- * authorize and transport callers map only `kind: 'size'` that way.
+ * The code for an envelope the contract refused: a hash mismatch keeps its own
+ * code, and every other refusal takes the providers' canonical mapping, so a
+ * `count` refusal is an invalid request and only `kind: 'size'` is
+ * `payload_too_large`.
*/
-function envelopeRefusalCode(contractCode: string): string {
- if (contractCode === 'limit') return 'payload_too_large'
- if (contractCode === 'request_hash_mismatch') return 'request_hash_mismatch'
- return 'invalid_request'
+function envelopeRefusalCode(refusal: { code: string; message: string; kind?: string }): string {
+ if (refusal.code === 'request_hash_mismatch') return 'request_hash_mismatch'
+ return canonicalRefusalCode(refusal)
}
export type GrokProxyErrorOptions = {
@@ -146,7 +147,7 @@ export class GrokLlmProxyClient {
request: input.request as GrokCompletionRequest,
})
if (!envelope.ok) {
- throw new GrokProxyError(envelopeRefusalCode(envelope.code), envelope.message, {
+ throw new GrokProxyError(envelopeRefusalCode(envelope), envelope.message, {
dispatched: false,
})
}
diff --git a/mcp-host/src/llm/grokSubscription.ts b/mcp-host/src/llm/grokSubscription.ts
index 8b4fb7c06..ce09bbe59 100644
--- a/mcp-host/src/llm/grokSubscription.ts
+++ b/mcp-host/src/llm/grokSubscription.ts
@@ -21,7 +21,7 @@ import {
} from './attachmentBudgetRefusal'
import { classifyUnknown } from './errorClassification'
import { GrokLlmProxyClient, GrokProxyError } from './grokLlmProxyClient'
-import { IMAGE_SOURCE_INVALID, isContextLengthRefusal, projectMessage } from './imageSource'
+import { IMAGE_SOURCE_INVALID, canonicalRefusalCode, projectMessage } from './imageSource'
import { CodexAuthorizeError, ProviderAttemptAuthorizer } from './providerAttemptAuthorizer'
import { rateLimitRetryDelayMs, waitBeforeRetry } from './rateLimitRetry'
import { type LlmProvider, descriptorFor } from './registryCore'
@@ -60,17 +60,6 @@ const GROK_REQUEST_INVALID = 'invalid_request'
/** A local image budget refusal (`ATTACHMENT_BUDGET_REFUSALS`). */
const GROK_ATTACHMENT_TOO_LARGE = 'attachment_too_large'
-/**
- * The code for a canonical-request refusal that is not an attachment refusal:
- * a conversation-volume limit is a context-length failure, any other `size`
- * refusal is `payload_too_large`, and everything else is an invalid request.
- */
-function canonicalRefusalCode(refusal: { code: string; message: string; kind?: string }): string {
- if (isContextLengthRefusal(refusal.code, refusal.message)) return 'request_limit_exceeded'
- if (refusal.kind === 'size') return 'payload_too_large'
- return GROK_REQUEST_INVALID
-}
-
function mapGrokUsage(usage?: { inputTokens: number; outputTokens: number }): {
usage: { input_tokens: number; output_tokens: number; total_tokens: number }
usage_reported: boolean
diff --git a/mcp-host/src/llm/imageSource.ts b/mcp-host/src/llm/imageSource.ts
index 348240e64..e0f333e68 100644
--- a/mcp-host/src/llm/imageSource.ts
+++ b/mcp-host/src/llm/imageSource.ts
@@ -183,3 +183,20 @@ const CONTEXT_LENGTH_REFUSALS = [
export function isContextLengthRefusal(code: string, message: string): boolean {
return code === 'limit' && CONTEXT_LENGTH_REFUSALS.some(pattern => pattern.test(message))
}
+
+/**
+ * The code for a contract refusal that is not an attachment refusal, shared by
+ * both providers' pre-dispatch checks and the Grok proxy envelope: a
+ * conversation-volume limit is a context-length failure, any other `size`
+ * refusal is `payload_too_large`, and everything else (`count` included) is an
+ * invalid request.
+ */
+export function canonicalRefusalCode(refusal: {
+ code: string
+ message: string
+ kind?: string
+}): string {
+ if (isContextLengthRefusal(refusal.code, refusal.message)) return 'request_limit_exceeded'
+ if (refusal.kind === 'size') return 'payload_too_large'
+ return 'invalid_request'
+}