Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
test(grok-subscription): require the upstream context overflow to reach the Host as such (#739)
Alfredo Lopez8164f417bc99
diff --git a/grok-llm-proxy/test/contractFreeze.test.ts b/grok-llm-proxy/test/contractFreeze.test.tsindex 0654bb447..e7b2e0eaf 100644--- a/grok-llm-proxy/test/contractFreeze.test.ts+++ b/grok-llm-proxy/test/contractFreeze.test.ts@@ -121,4 +121,17 @@ describe('grok-subscription contract freeze', () => {expect(errors).toContain('provider_unavailable')expect(errors).toContain('invalid_tool_arguments')})++ it('T-R10-3 lists context_length_exceeded in the frozen error taxonomy', () => {+ // R10 (M1): the proxy answers the upstream's context-window refusal with+ // this code, as the Codex fixture already records.+ const fixture = JSON.parse(readFileSync(fixturePath, 'utf8')) as {+ errorTaxonomy: unknown+ }+ const errors = fixture.errorTaxonomy+ // Witness: the taxonomy was read and is a non-empty list of codes.+ expect(Array.isArray(errors) && errors.length > 0).toBe(true)+ expect(errors).toContain('provider_unavailable')+ expect(errors).toContain('context_length_exceeded')+ })})diff --git a/grok-llm-proxy/test/grokTransport.conformance.test.ts b/grok-llm-proxy/test/grokTransport.conformance.test.tsindex 12777044c..f0497a6e4 100644--- a/grok-llm-proxy/test/grokTransport.conformance.test.ts+++ b/grok-llm-proxy/test/grokTransport.conformance.test.ts@@ -1387,6 +1387,115 @@ describe('streamGrokCompletion', () => {expect(finalize.mock.calls[0]?.[0]?.receipt?.usage).toBeUndefined()})+ // R10 (M1): the Grok upstream refuses a prompt over the model's context window+ // with an HTTP 400 before any stream starts. This body is the one recorded on+ // 2026-09-23 against cli-chat-proxy.grok.com/v1/responses with grok-4.6: the+ // `code` field is the generic `invalid-argument`, and the only specific marker+ // is the bracketed `[input_too_large]` inside the `error` string.+ describe('non-success upstream bodies (R10)', () => {+ const recordedOverflowBody = JSON.stringify({+ code: 'invalid-argument',+ error:+ "Failed to start sampling: [input_too_large] The prompt is too long for this model's context window (1107771 tokens > 500000 tokens)",+ })++ function streamWith(response: () => Response) {+ const finalize = vi.fn(+ async (_input: Parameters<StreamGrokCompletionInput['finalize']>[0]) => ({+ providerAttemptId: 'att-1',+ outcome: 'error' as const,+ duplicate: false,+ })+ )+ const fetchFn = vi.fn(async (_url: FetchInput, _init?: RequestInit) => response())+ const pending = streamGrokCompletion({+ executionTicket: 'ticket-1',+ requestHash: REQUEST_HASH,+ request: REQUEST,+ ticket: {+ jti: 'jti-1',+ hostRef: 'research-host',+ model: 'gpt-5.1',+ requestHash: REQUEST_HASH,+ providerAttemptId: 'att-1',+ },+ redeem: async () => redeemSuccess(),+ finalize,+ fetchFn,+ lookup: async () => [{ address: '1.2.3.4', family: 4 }],+ })+ return { pending, finalize, fetchFn }+ }++ it('T-R10-1a maps the recorded HTTP 400 input_too_large body to context_length_exceeded', async () => {+ const { pending, finalize, fetchFn } = streamWith(+ () =>+ new Response(recordedOverflowBody, {+ status: 400,+ headers: { 'content-type': 'application/json' },+ })+ )+ await expect(pending).rejects.toMatchObject({ code: 'context_length_exceeded' })+ expect(fetchFn).toHaveBeenCalledTimes(1)+ expect(finalize).toHaveBeenCalledWith(+ expect.objectContaining({ receipt: expect.objectContaining({ outcome: 'error' }) })+ )+ })++ it('T-R10-1b keeps the status mapping for every other error body', async () => {+ const other = JSON.stringify({ code: 'invalid-argument', error: 'unsupported parameter' })+ await expect(+ streamWith(() => new Response(other, { status: 400 })).pending+ ).rejects.toMatchObject({ code: 'invalid_request' })+ await expect(+ streamWith(() => new Response('bad request', { status: 400 })).pending+ ).rejects.toMatchObject({ code: 'invalid_request' })+ await expect(+ streamWith(() => new Response('{"error":', { status: 400 })).pending+ ).rejects.toMatchObject({ code: 'invalid_request' })+ // The marker counts only inside the `error` string, not anywhere in the body.+ await expect(+ streamWith(+ () =>+ new Response(JSON.stringify({ code: '[input_too_large]', error: 'other' }), {+ status: 400,+ })+ ).pending+ ).rejects.toMatchObject({ code: 'invalid_request' })+ await expect(+ streamWith(() => new Response(recordedOverflowBody, { status: 401 })).pending+ ).rejects.toMatchObject({ code: 'connection_unavailable' })+ await expect(+ streamWith(() => new Response(other, { status: 503 })).pending+ ).rejects.toMatchObject({ code: 'provider_unavailable' })+ })++ it('T-R10-1c reads a bounded prefix of an endless error body and keeps the status mapping', async () => {+ const chunk = new TextEncoder().encode(`{"pad":"${'x'.repeat(1024)}`)+ let pulled = 0+ let canceled = false+ // highWaterMark 0: nothing is pulled until a reader asks for it.+ const endless = new ReadableStream<Uint8Array>(+ {+ pull(controller) {+ pulled += chunk.byteLength+ controller.enqueue(chunk)+ },+ cancel() {+ canceled = true+ },+ },+ { highWaterMark: 0 }+ )+ const { pending } = streamWith(() => new Response(endless, { status: 400 }))+ await expect(pending).rejects.toMatchObject({ code: 'invalid_request' })+ // Witness: the body was read, and the read stopped at a bound.+ expect(pulled).toBeGreaterThan(0)+ expect(canceled).toBe(true)+ expect(pulled).toBeLessThan(1024 * 1024)+ })+ })+it('emits a tool call only after argument deltas complete', async () => {const frames: unknown[] = []const fetchFn = vi.fn(async (_url: FetchInput, _init?: RequestInit) =>diff --git a/grok-llm-proxy/test/server.security.test.ts b/grok-llm-proxy/test/server.security.test.tsindex 92c007545..6d03838b6 100644--- a/grok-llm-proxy/test/server.security.test.ts+++ b/grok-llm-proxy/test/server.security.test.ts@@ -677,6 +677,20 @@ describe('grok-llm-proxy attempt telemetry', () => {})) as typeof fetch}+ // The upstream's context-window refusal recorded on 2026-09-23 (R10): an+ // HTTP 400 before any stream, with the marker inside the `error` string.+ function contextOverflowUpstream(): typeof fetch {+ return (async () =>+ new Response(+ JSON.stringify({+ code: 'invalid-argument',+ error:+ "Failed to start sampling: [input_too_large] The prompt is too long for this model's context window (1107771 tokens > 500000 tokens)",+ }),+ { status: 400, headers: { 'content-type': 'application/json' } }+ )) as typeof fetch+ }+function denyingClient(code: string): ControlApiClient {return {async redeem() {@@ -843,6 +857,33 @@ describe('grok-llm-proxy attempt telemetry', () => {expect(failureCount(metricsText, 'other')).toBe(0)})+ // R10 (M1): the upstream context overflow reaches the Host as a 400 with its+ // own code and metric label. The 503 default would be classified as an+ // overload and retried with the identical conversation.+ it('(a1c) T-R10-2 answers 400 context_length_exceeded for the upstream context overflow', async () => {+ const { res, receipts, lines, metricsText } = await run({+ providerAttemptId: 'att-context-http',+ fetchFn: contextOverflowUpstream(),+ })+ expect(res.status).toBe(400)+ expect(res.headers['content-type']).toMatch(/^application\/json/)+ expect(res.body).toEqual({ error: 'context_length_exceeded' })+ expect(receipts).toEqual([expect.objectContaining({ outcome: 'error' })])+ expect(lines).toHaveLength(1)+ expect(lines[0]).toMatchObject({+ providerAttemptId: 'att-context-http',+ outcome: 'failed',+ code: 'context_length_exceeded',+ deliveredAs: 'http_status',+ httpStatus: 400,+ toolCalls: 0,+ textChunks: 0,+ })+ expectNoForbiddenKeys(lines[0]!)+ expect(failureCount(metricsText, 'context_length_exceeded')).toBe(1)+ expect(failureCount(metricsText, 'other')).toBe(0)+ })+it('(a2) sends an SSE error frame when text was already streamed', async () => {const { res, lines } = await run({providerAttemptId: 'att-limit-sse',diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.jsonindex aae464852..23b8a8635 100644--- a/mcp-host/package-lock.json+++ b/mcp-host/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-mcp-host",- "version": "0.1.428",+ "version": "0.1.429","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-mcp-host",- "version": "0.1.428",+ "version": "0.1.429","license": "SEE LICENSE IN LICENSE","dependencies": {"@anthropic-ai/sdk": "^0.32.0",diff --git a/mcp-host/package.json b/mcp-host/package.jsonindex 64bb8592f..1dc688c80 100644--- a/mcp-host/package.json+++ b/mcp-host/package.json@@ -1,6 +1,6 @@{"name": "clerum-mcp-host",- "version": "0.1.428",+ "version": "0.1.429","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 a4a189f16..b28f4fe1c 100644--- a/mcp-host/src/llm/__tests__/grokLlmProxyClient.test.ts+++ b/mcp-host/src/llm/__tests__/grokLlmProxyClient.test.ts@@ -215,6 +215,56 @@ describe('GrokLlmProxyClient', () => {expect(classifyFailoverClass(classified.code, classified.retryable)).toBeNull()})+ // R10 (M1) — the Grok upstream refuses a prompt over the model's context+ // window with an HTTP 400 whose `error` string carries `[input_too_large]`+ // (recorded 2026-09-23). The proxy forwards it as a 400+ // `context_length_exceeded`; the Host must read it as the conversation being+ // too long, not as a failed API call.+ it.each([+ {+ path: '400 JSON body before streaming',+ response: {+ ok: false,+ status: 400,+ json: async () => ({ error: 'context_length_exceeded' }),+ },+ message: 'proxy stream failed with 400 (context_length_exceeded)',+ },+ {+ path: 'SSE error frame after a text frame',+ response: {+ ok: true,+ body: sse([+ { type: 'text', text: 'partial' },+ { type: 'error', code: 'context_length_exceeded' },+ ]),+ },+ message: 'proxy stream failed with context_length_exceeded',+ },+ ])(+ 'T-R10-4 classifies the upstream context_length_exceeded from the $path as ContextLengthExceeded',+ 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: 'context_length_exceeded', message })++ const classified = new GrokSubscriptionProvider('grok-4.6', {} as never).classifyError(err)+ expect(classified).toMatchObject({+ code: LlmErrorCode.ContextLengthExceeded,+ retryable: false,+ providerCode: 'context_length_exceeded',+ })+ expect(classifyFailoverClass(classified.code, classified.retryable)).toBeNull()+ }+ )+// R9-7 (L-5) — a gateway in front of the proxy (nginx// `client_max_body_size`) refuses an oversized body with an HTML 413 and no// JSON code. It is the same size refusal, not a provider outage.