Skip to content

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

TypeScript

test(mcp-host): specify the Grok envelope allowance and pin image refusals

Alfredo Lopez054f3e197599
diff --git a/grok-llm-proxy/test/grokTransport.conformance.test.ts b/grok-llm-proxy/test/grokTransport.conformance.test.ts
index 53bb31d30..95b0c915c 100644
--- a/grok-llm-proxy/test/grokTransport.conformance.test.ts
+++ b/grok-llm-proxy/test/grokTransport.conformance.test.ts
@@ -259,6 +259,7 @@ describe('streamGrokCompletion', () => {
// A V2 deadline travels inside the hashed request; an outer one is refused.
await expect(streamGrokCompletion({ ...input, deadlineMs: 1000 })).rejects.toMatchObject({
code: 'invalid_request',
+ message: 'Visual request deadlines must be inside the authorized request',
})
expect(redeem).not.toHaveBeenCalled()
expect(fetchFn).not.toHaveBeenCalled()
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index 49071a9ff..b8b9e59cf 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.449",
+ "version": "0.1.450",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.449",
+ "version": "0.1.450",
"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 3081caac1..c706445fe 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.449",
+ "version": "0.1.450",
"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 546c1514e..bd2f9eb8b 100644
--- a/mcp-host/src/llm/__tests__/grokLlmProxyClient.test.ts
+++ b/mcp-host/src/llm/__tests__/grokLlmProxyClient.test.ts
@@ -1,5 +1,6 @@
import { describe, expect, it, vi } from 'vitest'
import {
+ LIMITS as GROK_LIMITS,
buildGrokProxyEnvelope,
hashGrokCompletionRequest,
parseGrokCompletionRequest,
@@ -38,6 +39,40 @@ function client(fetchFn: unknown, extra: { refreshOnUnauthorized?: () => Promise
})
}
+// A well-formed V2 request with one image, authorized under its own hash.
+function visualInput() {
+ const parsed = parseGrokCompletionRequest({
+ schemaVersion: 'grok-completion-request.v2',
+ requestId: 'req-visual',
+ idempotencyKey: 'idem-visual',
+ provider: 'grok-subscription',
+ model: 'grok-4.6',
+ deadlineMs: 1000,
+ messages: [
+ {
+ role: 'user',
+ content: 'Describe the attached image',
+ contentParts: [
+ { type: 'text', text: 'Describe the attached image' },
+ {
+ type: 'image',
+ mimeType: 'image/png',
+ data: GROK_PNG_2X2_BASE64,
+ source: { kind: 'attachment', attachmentId: 'att-1', messageId: 'msg-1' },
+ },
+ ],
+ },
+ ],
+ })
+ if (!parsed.ok) throw new Error(parsed.message)
+ return {
+ request: parsed.value,
+ requestHash: hashGrokCompletionRequest(parsed.value),
+ executionTicket: 'fixture-ticket',
+ }
+}
+type VisualInput = ReturnType<typeof visualInput>
+
const STREAM_INPUT = {
executionTicket: 'ticket-123456',
requestHash: 'a'.repeat(64),
@@ -92,8 +127,46 @@ describe('GrokLlmProxyClient', () => {
})
await expect(grok.stream({ ...input, requestHash: 'a'.repeat(64) })).rejects.toMatchObject({
code: 'request_hash_mismatch',
+ message: 'requestHash does not match the request',
+ dispatched: false,
+ })
+ expect(fetchFn).toHaveBeenCalledOnce()
+ })
+
+ // Each contract refusal on the V2 path keeps its own code, so the Host
+ // classifies a size refusal as a size refusal and never dispatches.
+ it.each([
+ [
+ 'a request the contract rejects',
+ (input: VisualInput) => ({ ...input, request: { ...input.request, requestId: '' } }),
+ { code: 'invalid_request', message: 'requestId is invalid' },
+ ],
+ [
+ 'an envelope over the visual ceiling',
+ (input: VisualInput) => ({
+ ...input,
+ executionTicket: 't'.repeat(GROK_LIMITS.maxVisualRequestBodyBytes),
+ }),
+ { code: 'payload_too_large', message: 'proxy envelope exceeds maxVisualRequestBodyBytes' },
+ ],
+ [
+ 'an envelope the contract rejects',
+ (input: VisualInput) => ({ ...input, executionTicket: 'short' }),
+ { code: 'invalid_request', message: 'executionTicket is invalid' },
+ ],
+ ])('T-G4c-5 refuses %s before the proxy hop', async (_label, mutate, expected) => {
+ const fetchFn = vi.fn<typeof fetch>(
+ async () => new Response(sse([{ type: 'done', outcome: 'success' }]))
+ )
+ const grok = client(fetchFn)
+ const input = visualInput()
+ await expect(grok.stream(mutate(input))).rejects.toMatchObject({
+ ...expected,
dispatched: false,
})
+ expect(fetchFn).not.toHaveBeenCalled()
+ // Witness: the unmodified request reaches the proxy.
+ await grok.stream(input)
expect(fetchFn).toHaveBeenCalledOnce()
})
diff --git a/mcp-host/src/llm/__tests__/grokSubscription.test.ts b/mcp-host/src/llm/__tests__/grokSubscription.test.ts
index 97a400423..1c989c703 100644
--- a/mcp-host/src/llm/__tests__/grokSubscription.test.ts
+++ b/mcp-host/src/llm/__tests__/grokSubscription.test.ts
@@ -7,6 +7,7 @@ import {
import { minifiedMcpResult } from '../../__tests__/fixtures/minifiedMcpResult'
import { LlmErrorCode } from '../../core/errors'
import type { ChatMessage, MessageContentPart } from '../../core/types'
+import { CodexSubscriptionProvider } from '../codexSubscription'
import { classifyFailoverClass } from '../failover/classify'
import { GrokProxyError } from '../grokLlmProxyClient'
import { GrokSubscriptionProvider } from '../grokSubscription'
@@ -1129,6 +1130,47 @@ describe('GrokSubscriptionProvider image input (#784)', () => {
}
)
+ const gfsSource = (attachmentId: string, toolCallId: string) => ({
+ kind: 'gfs' as const,
+ drive: 'main',
+ resourceId: 'a'.repeat(32),
+ gfsUri: `gfs://main/${'a'.repeat(32)}`,
+ version: 7,
+ name: 'image.png',
+ attachmentId,
+ toolCallId,
+ })
+ it.each([
+ ['a tool source', 'empty attachmentId', { kind: 'tool', attachmentId: '', toolCallId: 'tc-1' }],
+ ['a GFS source', 'empty attachmentId', gfsSource(' ', 'gfs-read-call')],
+ ['a GFS source', 'empty toolCallId', gfsSource('gfs-read-attachment', '')],
+ ['an unknown source kind', 'unknown source kind', { kind: 'upload', attachmentId: 'att-1' }],
+ ])(
+ 'T-G4b-6b refuses %s with %s as image_source_invalid before authorize',
+ async (_label, detail, source) => {
+ const wired = deps()
+ const provider = new GrokSubscriptionProvider('grok-4.6', wired as never)
+ const part = { type: 'image', mimeType: 'image/png', data: GROK_PNG_2X2_BASE64, source }
+
+ const rejected = await provider
+ .completeSingleTurn(userWithImages([part as ImagePart]))
+ .catch((e: unknown) => e)
+
+ expect(rejected).toMatchObject({
+ code: 'image_source_invalid',
+ message: `image part has no usable provenance source (${detail}); host producers must attach the attachment or tool call it came from`,
+ })
+ expect(wired.authorizer.authorize).not.toHaveBeenCalled()
+ expect(wired.proxy.stream).not.toHaveBeenCalled()
+
+ // Liveness witness: the same provider authorizes a well-sourced image.
+ await provider.completeSingleTurn(
+ userWithImages([attachmentImage(GROK_PNG_2X2_BASE64, 'att-1')])
+ )
+ expect(wired.authorizer.authorize).toHaveBeenCalledTimes(1)
+ }
+ )
+
const MIB = 1024 * 1024
const GROK_ATTACHMENT_REFUSALS: Array<{
name: string
@@ -1237,4 +1279,45 @@ describe('GrokSubscriptionProvider image input (#784)', () => {
await provider.completeSingleTurn([{ role: 'user', content: 'hi' }])
expect(wired.authorizer.authorize).toHaveBeenCalledTimes(1)
})
+
+ // #806 review M6, user decision: keep Codex parity. More than maxImages
+ // images is `invalid_request`, not an attachment refusal, exactly as Codex
+ // classifies it (codexSubscription.ts, "the maxImages count refusal stays
+ // invalid_request").
+ it('T-G4b-9 refuses more than maxImages images as invalid_request, classified as Codex does', async () => {
+ const wired = deps()
+ const provider = new GrokSubscriptionProvider('grok-4.6', wired as never)
+ const images = Array.from({ length: GROK_VISUAL_LIMITS.maxImages + 1 }, (_, index) =>
+ attachmentImage(GROK_PNG_2X2_BASE64, `att-${index}`)
+ )
+
+ const rejected = await provider
+ .completeSingleTurn(userWithImages(images))
+ .catch((e: unknown) => e)
+
+ // The contract's own sentence is the witness that the canonical-hash path
+ // refused the batch, not an earlier guard.
+ expect(rejected).toBeInstanceOf(CodexAuthorizeError)
+ expect(rejected).toMatchObject({
+ code: 'invalid_request',
+ message: `request exceeds ${GROK_VISUAL_LIMITS.maxImages} images`,
+ })
+ expect(wired.authorizer.authorize).not.toHaveBeenCalled()
+ expect(wired.proxy.stream).not.toHaveBeenCalled()
+ const classified = provider.classifyError(rejected)
+ expect(classified).toMatchObject({
+ code: LlmErrorCode.ApiCallFailed,
+ retryable: false,
+ providerCode: 'invalid_request',
+ providerDispatched: false,
+ })
+ expect(classified).toEqual(
+ new CodexSubscriptionProvider('gpt-5.3-codex', {} as never).classifyError(rejected)
+ )
+
+ // Liveness witness: maxImages images are authorized and streamed.
+ await provider.completeSingleTurn(userWithImages(images.slice(1)))
+ expect(wired.authorizer.authorize).toHaveBeenCalledTimes(1)
+ expect(wired.proxy.stream).toHaveBeenCalledTimes(1)
+ })
})
diff --git a/mcp-host/src/llm/__tests__/providerAttemptAuthorizer.test.ts b/mcp-host/src/llm/__tests__/providerAttemptAuthorizer.test.ts
index beb583ef6..69a46e517 100644
--- a/mcp-host/src/llm/__tests__/providerAttemptAuthorizer.test.ts
+++ b/mcp-host/src/llm/__tests__/providerAttemptAuthorizer.test.ts
@@ -157,6 +157,16 @@ describe('ProviderAttemptAuthorizer', () => {
provider: 'grok-subscription',
pad: 'a'.repeat(padBytes),
})
+ // A realistic authorize body whose serialized JSON is exactly `bytes` long.
+ function envelopeOfBytes(fields: Record<string, unknown>, bytes: number): AuthorizeAttemptBody {
+ const frame = Buffer.byteLength(
+ JSON.stringify(realisticEnvelope({ ...fields, pad: '' })),
+ 'utf8'
+ )
+ const body = realisticEnvelope({ ...fields, pad: 'a'.repeat(bytes - frame) })
+ expect(Buffer.byteLength(JSON.stringify(body), 'utf8')).toBe(bytes)
+ return body
+ }
it('T-G4c-1 dispatches a 30 MiB Grok V2 envelope that the Codex visual ceiling refuses', async () => {
const padBytes = 30 * MIB
@@ -202,25 +212,76 @@ describe('ProviderAttemptAuthorizer', () => {
GROK_LIMITS.maxRequestBodyBytes,
],
])(
- 'T-G4c-2 refuses a Grok %s body over its Grok ceiling and names Grok',
+ 'T-G4c-2 refuses a Grok %s body one byte over its Grok ceiling and names Grok',
async (_label, schemaVersion, bodyLimit, requestLimit) => {
+ const grok = { schemaVersion, provider: 'grok-subscription' }
const fetchFn = vi.fn()
await expect(
- authorizerWith(fetchFn).authorize(realisticEnvelope(grokRequest(schemaVersion, bodyLimit)))
+ authorizerWith(fetchFn).authorize(envelopeOfBytes(grok, bodyLimit + 1))
).rejects.toMatchObject({
code: 'payload_too_large',
message: `Grok request exceeds ${requestLimit / MIB} MiB; use fewer or smaller images, or reduce context`,
})
expect(fetchFn).not.toHaveBeenCalled()
- // Witness: the same request 1 MiB smaller is dispatched.
+ // Witness: the same request at the ceiling exactly is dispatched.
const dispatched = vi.fn().mockResolvedValue({ ok: true, json: async () => validAuthorize })
- await authorizerWith(dispatched).authorize(
- realisticEnvelope(grokRequest(schemaVersion, bodyLimit - MIB))
- )
+ await authorizerWith(dispatched).authorize(envelopeOfBytes(grok, bodyLimit))
expect(dispatched).toHaveBeenCalledOnce()
}
)
+ // Both contracts export 16 KiB today (T-R11-parity), so only a stubbed Grok
+ // value can show which constant bounds a Grok request.
+ it('T-G4c-4 bounds a Grok request with the Grok contract envelope allowance', async () => {
+ const grokAllowance = 64 * 1024
+ vi.resetModules()
+ vi.doMock('@clerum/grok-provider-attempt-contract', async importOriginal => ({
+ ...(await importOriginal<Record<string, unknown>>()),
+ ENVELOPE_ALLOWANCE_BYTES: grokAllowance,
+ }))
+ try {
+ const stubbedContract = await import('@clerum/grok-provider-attempt-contract')
+ expect(stubbedContract.ENVELOPE_ALLOWANCE_BYTES).toBe(grokAllowance)
+ const { ProviderAttemptAuthorizer: Stubbed } = await import('../providerAttemptAuthorizer')
+ const stubbedWith = (fetchFn: unknown) =>
+ new Stubbed({
+ authorizeUrl: 'http://gateway/authorize',
+ readPlatformJwt: () => 'test-jwt',
+ fetchFn: fetchFn as typeof fetch,
+ })
+
+ const grok = { schemaVersion: 'grok-completion-request.v1', provider: 'grok-subscription' }
+ const grokLimit = GROK_LIMITS.maxRequestBodyBytes + grokAllowance
+ const grokDispatched = vi
+ .fn()
+ .mockResolvedValue({ ok: true, json: async () => validAuthorize })
+ await stubbedWith(grokDispatched).authorize(envelopeOfBytes(grok, grokLimit))
+ expect(grokDispatched).toHaveBeenCalledOnce()
+ const grokRefused = vi.fn()
+ await expect(
+ stubbedWith(grokRefused).authorize(envelopeOfBytes(grok, grokLimit + 1))
+ ).rejects.toMatchObject({ code: 'payload_too_large' })
+ expect(grokRefused).not.toHaveBeenCalled()
+
+ // Codex keeps the Codex contract's allowance.
+ const codex = { schemaVersion: 'codex-completion-request.v1', provider: 'codex-subscription' }
+ const codexLimit = LIMITS.maxRequestBodyBytes + CODEX_CONTRACT_ENVELOPE_ALLOWANCE_BYTES
+ const codexRefused = vi.fn()
+ await expect(
+ stubbedWith(codexRefused).authorize(envelopeOfBytes(codex, codexLimit + 1))
+ ).rejects.toMatchObject({ code: 'payload_too_large' })
+ expect(codexRefused).not.toHaveBeenCalled()
+ const codexDispatched = vi
+ .fn()
+ .mockResolvedValue({ ok: true, json: async () => validAuthorize })
+ await stubbedWith(codexDispatched).authorize(envelopeOfBytes(codex, codexLimit))
+ expect(codexDispatched).toHaveBeenCalledOnce()
+ } finally {
+ vi.doUnmock('@clerum/grok-provider-attempt-contract')
+ vi.resetModules()
+ }
+ })
+
it('T-G4c-3 names Grok when the gateway answers a Grok request with 413', async () => {
const fetchFn = vi.fn(async () => new Response('<h1>Too large</h1>', { status: 413 }))
await expect(