Skip to content

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

TypeScript

fix(mcp-host): classify Grok contract limit refusals as context-length failures (#731)

Alfredo Lopez4678ec9b8317
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index 89125b692..49eac9262 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.378",
+ "version": "0.1.379",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.378",
+ "version": "0.1.379",
"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 b47659655..3203be4da 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.378",
+ "version": "0.1.379",
"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/grokSubscription.ts b/mcp-host/src/llm/grokSubscription.ts
index 034a0178c..5d6791d97 100644
--- a/mcp-host/src/llm/grokSubscription.ts
+++ b/mcp-host/src/llm/grokSubscription.ts
@@ -33,6 +33,51 @@ export type GrokAttemptContext = {
userId?: string
}
+/**
+ * The contract `limit` refusals that mean "this turn carries too much".
+ *
+ * The Grok twin of `codexSubscription.ts:62-70`. The two are deliberate
+ * duplicates, not an accident: `grok-provider-attempt-contract/index.cjs:6`
+ * states that this package does not import the Codex contract's LIMITS, and a
+ * shared predicate would either recreate that coupling or pin prose rather than
+ * code. The cost of the duplication is that each copy needs its own tests, which
+ * is what T-C-grok, T-C2-grok, T-C3-grok and T-C5-grok are for. If you change
+ * one list, read the other.
+ *
+ * `fail('limit', …)` guards eight checks here too, and only these are about
+ * volume: the real byte bound (`index.cjs:414`), the element bound that proxies
+ * it (`:214`), `maxMessages` (`:260`) and `messages[i].toolCalls` (`:289`).
+ * Compaction is the remedy for all four, which is exactly what
+ * `ContextLengthExceeded` — "Conversation Too Long" — promises the user.
+ *
+ * The other four are not. Nesting depth (`:199`, `:236`),
+ * `generation.maxOutputTokens` (`:376`) and `deadlineMs` (`:445`) out of range
+ * are malformed or out-of-range parameters, and a shorter conversation fixes
+ * none of them; labelling them a context-length failure would send the user into
+ * a compaction loop that cannot converge. They stay `invalid_request`, which is
+ * what `subscriptionRequestHash.test.ts:155-168` pins for the over-deep schema
+ * across both providers.
+ *
+ * `hashCanonicalGrokRequest` returns `{ ok, code, message }` and nothing else,
+ * so the message is the only discriminator available at this boundary (#731).
+ * The byte pattern is a prefix so it keeps matching the element bound's own
+ * distinct wording.
+ *
+ * `messages exceed` is defence in depth rather than a reachable branch: the
+ * guard in `execute` raises that exact message with this same classification
+ * before `hashCanonicalGrokRequest` runs, so the contract's own copy of it only
+ * arrives here if that guard is ever removed.
+ */
+const CONTEXT_LENGTH_REFUSALS = [
+ /^request exceeds maxRequestBodyBytes/,
+ /^messages exceed \d+$/,
+ /^messages\[\d+\]\.toolCalls exceed \d+$/,
+]
+
+function isContextLengthRefusal(code: string, message: string): boolean {
+ return code === 'limit' && CONTEXT_LENGTH_REFUSALS.some(pattern => pattern.test(message))
+}
+
function mapGrokUsage(usage?: { inputTokens: number; outputTokens: number }): {
usage: { input_tokens: number; output_tokens: number; total_tokens: number }
usage_reported: boolean
@@ -305,7 +350,19 @@ export class GrokSubscriptionProvider implements SingleTurnProvider {
// requestHash mismatch. An invalid request never leaves the process.
const canonical = hashCanonicalGrokRequest(this.buildRequest(messages, tools, options))
if (!canonical.ok) {
- throw new CodexAuthorizeError('invalid_request', canonical.message)
+ // A size refusal (bytes, element bound, message count, tool-call count)
+ // is a context-length failure, not a malformed request; it is thrown
+ // before authorize and dispatch so no provider attempt is spent.
+ // Reported as `invalid_request` it reached the UI as a retryable
+ // "Connection Error" and invited a retry that reproduced it (#731). The
+ // message-count guard above already used this classification, and #728
+ // left this path behind when it added that guard.
+ throw new CodexAuthorizeError(
+ isContextLengthRefusal(canonical.code, canonical.message)
+ ? 'request_limit_exceeded'
+ : 'invalid_request',
+ canonical.message
+ )
}
const { request, requestHash } = canonical.value
const context = this.deps.attemptContext({ model: this.model })