Skip to content

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

TypeScript

test(grok): assert the host classifies the tool-call limit, not an outage

Alfredo Lopez7e1a08de3e26
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index 23bfbfa52..fb023890b 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.362",
+ "version": "0.1.363",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.362",
+ "version": "0.1.363",
"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 f91b6d21e..2fc9ce550 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.362",
+ "version": "0.1.363",
"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__/grokLlmProxyClient.test.ts b/mcp-host/src/llm/__tests__/grokLlmProxyClient.test.ts
index bb707409a..6b54f8b88 100644
--- a/mcp-host/src/llm/__tests__/grokLlmProxyClient.test.ts
+++ b/mcp-host/src/llm/__tests__/grokLlmProxyClient.test.ts
@@ -1,9 +1,13 @@
import { describe, expect, it, vi } from 'vitest'
+import { LlmErrorCode } from '../../core/errors'
+import { classifyFailoverClass } from '../failover/classify'
import {
GrokLlmProxyClient,
+ GrokProxyError,
grokProxyErrorMessage,
resolveGrokProxyRuntimeUrl,
} from '../grokLlmProxyClient'
+import { GrokSubscriptionProvider } from '../grokSubscription'
const RUNTIME_BASE = 'http://grok-llm-proxy.control-plane.svc.cluster.local:8080'
const RUNTIME_URL = `${RUNTIME_BASE}/internal/runtime/v1/grok/completions`
@@ -141,6 +145,48 @@ describe('GrokLlmProxyClient', () => {
expect(fetchFn).toHaveBeenCalledTimes(2)
})
+ // The proxy reports the tool-call limit on two wire paths: a 422 JSON body
+ // before any stream frame, or an SSE error frame after one. Both must reach
+ // the provider classifier with the proxy's code intact.
+ it.each([
+ {
+ path: '422 JSON body before streaming',
+ response: {
+ ok: false,
+ status: 422,
+ json: async () => ({ error: 'tool_call_limit_exceeded' }),
+ },
+ message: 'proxy stream failed with 422 (tool_call_limit_exceeded)',
+ },
+ {
+ path: 'SSE error frame after a text frame',
+ response: {
+ ok: true,
+ body: sse([
+ { type: 'text', text: 'partial' },
+ { type: 'error', code: 'tool_call_limit_exceeded' },
+ ]),
+ },
+ message: 'proxy stream failed with tool_call_limit_exceeded',
+ },
+ ])('surfaces tool_call_limit_exceeded from the $path', async ({ response, message }) => {
+ const fetchFn = vi.fn().mockResolvedValue(response)
+ const err = await client(fetchFn)
+ .stream(STREAM_INPUT)
+ .then(
+ () => undefined,
+ (e: unknown) => e
+ )
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(err).toBeInstanceOf(GrokProxyError)
+ expect(err).toMatchObject({ code: 'tool_call_limit_exceeded', message })
+
+ const classified = new GrokSubscriptionProvider('grok-4.6', {} as never).classifyError(err)
+ expect(classified.code).toBe(LlmErrorCode.ToolCallLimitExceeded)
+ expect(classified.retryable).toBe(false)
+ expect(classifyFailoverClass(classified.code, classified.retryable)).toBeNull()
+ })
+
it('fails closed when the proxy emits an SSE error frame after headers', async () => {
const fetchFn = vi.fn().mockResolvedValue({
ok: true,
diff --git a/mcp-host/src/llm/__tests__/grokSubscription.test.ts b/mcp-host/src/llm/__tests__/grokSubscription.test.ts
index 45c1d1a03..0dae697b2 100644
--- a/mcp-host/src/llm/__tests__/grokSubscription.test.ts
+++ b/mcp-host/src/llm/__tests__/grokSubscription.test.ts
@@ -66,12 +66,28 @@ describe('GrokSubscriptionProvider', () => {
}),
})
const provider = new GrokSubscriptionProvider('grok-4.6', wired as never)
- await expect(
- provider.completeSingleTurnWithTools(
- [{ role: 'user', content: 'hi' }],
- [{ name: 'echo', description: 'echo', parameters: {} }]
- )
- ).rejects.toBeInstanceOf(GrokProxyError)
+ const oversized = provider.completeSingleTurnWithTools(
+ [{ role: 'user', content: 'hi' }],
+ [{ name: 'echo', description: 'echo', parameters: {} }]
+ )
+ await expect(oversized).rejects.toBeInstanceOf(GrokProxyError)
+ // The message is unchanged by the taxonomy work; only the code says this
+ // is a contract limit rather than a provider outage.
+ await expect(oversized).rejects.toMatchObject({
+ name: 'GrokProxyError',
+ code: 'tool_call_limit_exceeded',
+ message: 'tool calls exceed 64',
+ })
+
+ const classified = provider.classifyError(
+ new GrokProxyError('tool_call_limit_exceeded', 'tool calls exceed 64')
+ )
+ expect(classified.code).toBe(LlmErrorCode.ToolCallLimitExceeded)
+ expect(classified.retryable).toBe(false)
+ expect(classified.providerDispatched).toBe(true)
+ // Retrying the same request produces the same limit, so failover must not
+ // treat it as a provider outage worth trying elsewhere.
+ expect(classifyFailoverClass(classified.code, classified.retryable)).toBeNull()
})
it.each([
['unknown', 'partial grok text', 'outcome_unknown'],
@@ -289,12 +305,43 @@ describe('GrokSubscriptionProvider', () => {
}),
}) as never
)
- await expect(
- over.completeSingleTurnWithTools(
- [{ role: 'user', content: 'hi' }],
- [{ name: 'echo', description: 'echo', parameters: {} }]
- )
- ).rejects.toThrow(/tool calls exceed 64/)
+ const rejected = over.completeSingleTurnWithTools(
+ [{ role: 'user', content: 'hi' }],
+ [{ name: 'echo', description: 'echo', parameters: {} }]
+ )
+ await expect(rejected).rejects.toThrow(/tool calls exceed 64/)
+ await expect(rejected).rejects.toMatchObject({ code: 'tool_call_limit_exceeded' })
+ })
+
+ it('refuses 129 messages before authorize and classifies the refusal as a context limit', async () => {
+ const wired = deps()
+ const provider = new GrokSubscriptionProvider('grok-4.6', wired as never)
+ const message = { role: 'user' as const, content: 'hi' }
+
+ // Liveness witness for the negative assertion below: the identical call
+ // one message shorter does reach authorize, so "authorize was not called"
+ // reports the guard rather than a provider that never ran.
+ await provider.completeSingleTurn(Array.from({ length: 128 }, () => message))
+ expect(wired.authorizer.authorize).toHaveBeenCalledTimes(1)
+
+ const refused = provider.completeSingleTurn(Array.from({ length: 129 }, () => message))
+ await expect(refused).rejects.toBeInstanceOf(CodexAuthorizeError)
+ await expect(refused).rejects.toMatchObject({
+ code: 'request_limit_exceeded',
+ message: 'messages exceed 128',
+ })
+ expect(wired.authorizer.authorize).toHaveBeenCalledTimes(1)
+
+ // A request too long for the contract is not an outage and not retryable;
+ // `providerDispatched: false` is what proves nothing was billed for it,
+ // and it comes from the authorize arm of the same expression.
+ const classified = provider.classifyError(
+ new CodexAuthorizeError('request_limit_exceeded', 'messages exceed 128')
+ )
+ expect(classified.code).toBe(LlmErrorCode.ContextLengthExceeded)
+ expect(classified.retryable).toBe(false)
+ expect(classified.providerDispatched).toBe(false)
+ expect(classifyFailoverClass(classified.code, classified.retryable)).toBeNull()
})
it('does not treat an unknown empty stream as a successful stop', async () => {