Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
test(mcp-host): specify Grok limits on authorize and the V2 proxy envelope
Alfredo Lopez5e03976f23c3
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.jsonindex d36460e4b..126c0d67a 100644--- a/mcp-host/package-lock.json+++ b/mcp-host/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-mcp-host",- "version": "0.1.444",+ "version": "0.1.445","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-mcp-host",- "version": "0.1.444",+ "version": "0.1.445","license": "SEE LICENSE IN LICENSE","dependencies": {"@anthropic-ai/sdk": "^0.32.0",diff --git a/mcp-host/package.json b/mcp-host/package.jsonindex f850f608b..e189791ca 100644--- a/mcp-host/package.json+++ b/mcp-host/package.json@@ -1,6 +1,6 @@{"name": "clerum-mcp-host",- "version": "0.1.444",+ "version": "0.1.445","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.tsindex a55719220..546c1514e 100644--- a/mcp-host/src/llm/__tests__/grokLlmProxyClient.test.ts+++ b/mcp-host/src/llm/__tests__/grokLlmProxyClient.test.ts@@ -1,4 +1,9 @@import { describe, expect, it, vi } from 'vitest'+import {+ buildGrokProxyEnvelope,+ hashGrokCompletionRequest,+ parseGrokCompletionRequest,+} from '@clerum/grok-provider-attempt-contract'import { LlmErrorCode } from '../../core/errors'import { classifyFailoverClass } from '../failover/classify'import {@@ -8,6 +13,7 @@ import {resolveGrokProxyRuntimeUrl,} from '../grokLlmProxyClient'import { GrokSubscriptionProvider } from '../grokSubscription'+import { GROK_PNG_2X2_BASE64 } from './grokImageFixtures'const RUNTIME_BASE = 'http://grok-llm-proxy.control-plane.svc.cluster.local:8080'const RUNTIME_URL = `${RUNTIME_BASE}/internal/runtime/v1/grok/completions`@@ -39,6 +45,58 @@ const STREAM_INPUT = {}describe('GrokLlmProxyClient', () => {+ it('T-G4c-4 sends the exact measured V2 envelope and refuses an independent deadline or hash', async () => {+ 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)+ const input = {+ request: parsed.value,+ requestHash: hashGrokCompletionRequest(parsed.value),+ executionTicket: 'fixture-ticket',+ }+ const measured = buildGrokProxyEnvelope(input)+ if (!measured.ok) throw new Error(measured.message)+ const fetchFn = vi.fn<typeof fetch>(+ async () => new Response(sse([{ type: 'done', outcome: 'success' }]))+ )+ const grok = client(fetchFn)+ // grok-llm-proxy refuses an outer deadline on V2 (the deadline is inside+ // the hashed request), so the client must not send one.+ await grok.stream({ ...input, deadlineMs: 1000 })+ expect(JSON.parse(String(fetchFn.mock.calls[0]?.[1]?.body))).toEqual(measured.value)+ expect(measured.value).not.toHaveProperty('deadlineMs')+ await expect(grok.stream({ ...input, deadlineMs: 2000 })).rejects.toMatchObject({+ code: 'invalid_request',+ dispatched: false,+ })+ await expect(grok.stream({ ...input, requestHash: 'a'.repeat(64) })).rejects.toMatchObject({+ code: 'request_hash_mismatch',+ dispatched: false,+ })+ expect(fetchFn).toHaveBeenCalledOnce()+ })+it('streams only to the server-owned Grok runtime URL and ignores a caller-supplied URL', async () => {const fetchFn = vi.fn().mockResolvedValue({ok: true,diff --git a/mcp-host/src/llm/__tests__/providerAttemptAuthorizer.test.ts b/mcp-host/src/llm/__tests__/providerAttemptAuthorizer.test.tsindex 7d983fd49..beb583ef6 100644--- a/mcp-host/src/llm/__tests__/providerAttemptAuthorizer.test.ts+++ b/mcp-host/src/llm/__tests__/providerAttemptAuthorizer.test.ts@@ -1,5 +1,8 @@import { describe, expect, it, vi } from 'vitest'-import { ENVELOPE_ALLOWANCE_BYTES as GROK_CONTRACT_ENVELOPE_ALLOWANCE_BYTES } from '@clerum/grok-provider-attempt-contract'+import {+ ENVELOPE_ALLOWANCE_BYTES as GROK_CONTRACT_ENVELOPE_ALLOWANCE_BYTES,+ LIMITS as GROK_LIMITS,+} from '@clerum/grok-provider-attempt-contract'import {ENVELOPE_ALLOWANCE_BYTES as CODEX_CONTRACT_ENVELOPE_ALLOWANCE_BYTES,LIMITS,@@ -139,6 +142,98 @@ describe('ProviderAttemptAuthorizer', () => {expect(fetchFn).toHaveBeenCalledOnce()})+ // #784: the authorize route is shared and control-api bounds a Grok request+ // with the Grok contract, so the local check must use the same one.+ function authorizerWith(fetchFn: unknown) {+ return new ProviderAttemptAuthorizer({+ authorizeUrl: 'http://gateway/authorize',+ readPlatformJwt: () => 'test-jwt',+ fetchFn: fetchFn as typeof fetch,+ })+ }+ const MIB = 1024 * 1024+ const grokRequest = (schemaVersion: string, padBytes: number) => ({+ schemaVersion,+ provider: 'grok-subscription',+ pad: 'a'.repeat(padBytes),+ })++ it('T-G4c-1 dispatches a 30 MiB Grok V2 envelope that the Codex visual ceiling refuses', async () => {+ const padBytes = 30 * MIB+ expect(padBytes).toBeGreaterThan(LIMITS.maxVisualRequestBodyBytes)+ expect(padBytes).toBeLessThan(GROK_LIMITS.maxVisualRequestBodyBytes)+ // Witness that the size is meaningful: under the Codex contract the same+ // bytes are over the ceiling and never leave the process.+ const codexFetch = vi.fn()+ await expect(+ authorizerWith(codexFetch).authorize(+ realisticEnvelope({+ schemaVersion: 'codex-completion-request.v2',+ provider: 'codex-subscription',+ pad: 'a'.repeat(padBytes),+ })+ )+ ).rejects.toMatchObject({+ code: 'payload_too_large',+ message: `Codex request exceeds ${LIMITS.maxVisualRequestBodyBytes / MIB} MiB; use fewer or smaller images, or reduce context`,+ })+ expect(codexFetch).not.toHaveBeenCalled()++ const fetchFn = vi.fn().mockResolvedValue({ ok: true, json: async () => validAuthorize })+ await expect(+ authorizerWith(fetchFn).authorize(+ realisticEnvelope(grokRequest('grok-completion-request.v2', padBytes))+ )+ ).resolves.toMatchObject({ executionTicket: 'ticket-123456' })+ expect(fetchFn).toHaveBeenCalledOnce()+ })++ it.each([+ [+ 'V2',+ 'grok-completion-request.v2',+ GROK_LIMITS.maxVisualRequestBodyBytes,+ GROK_LIMITS.maxVisualRequestBodyBytes,+ ],+ [+ 'V1',+ 'grok-completion-request.v1',+ GROK_LIMITS.maxRequestBodyBytes + GROK_CONTRACT_ENVELOPE_ALLOWANCE_BYTES,+ GROK_LIMITS.maxRequestBodyBytes,+ ],+ ])(+ 'T-G4c-2 refuses a Grok %s body over its Grok ceiling and names Grok',+ async (_label, schemaVersion, bodyLimit, requestLimit) => {+ const fetchFn = vi.fn()+ await expect(+ authorizerWith(fetchFn).authorize(realisticEnvelope(grokRequest(schemaVersion, bodyLimit)))+ ).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.+ const dispatched = vi.fn().mockResolvedValue({ ok: true, json: async () => validAuthorize })+ await authorizerWith(dispatched).authorize(+ realisticEnvelope(grokRequest(schemaVersion, bodyLimit - MIB))+ )+ expect(dispatched).toHaveBeenCalledOnce()+ }+ )++ 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(+ authorizerWith(fetchFn).authorize(+ realisticEnvelope(grokRequest('grok-completion-request.v1', 16))+ )+ ).rejects.toMatchObject({+ code: 'payload_too_large',+ message: 'Grok request is too large; use fewer or smaller images, or reduce context',+ })+ expect(fetchFn).toHaveBeenCalledOnce()+ })+it('recognizes a non-JSON 413 as a request limit', async () => {const fetchFn = vi.fn(async () => new Response('<h1>Too large</h1>', { status: 413 }))const authorizer = new ProviderAttemptAuthorizer({