Skip to content

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

TypeScript

test(llm-proxy): count V2 text repeated in its parts once, downstream

Alfredo Lopez29efa5a537f2
diff --git a/codex-llm-proxy/test/server.security.test.ts b/codex-llm-proxy/test/server.security.test.ts
index bd800c546..726be12a2 100644
--- a/codex-llm-proxy/test/server.security.test.ts
+++ b/codex-llm-proxy/test/server.security.test.ts
@@ -333,6 +333,61 @@ describe('codex-llm-proxy security surface', () => {
expect(res.body.error).toBe('payload_too_large')
})
+ // Review R3-L3: V2 text repeated in content and in its text parts counts
+ // once on the non-image budget, as the same text does in V1.
+ it('R3-L3 counts V2 text repeated in content and its text parts once', async () => {
+ const { runtimeApp } = createProxyApps(config({ maxBodyBytes: DEFAULT_MAX_BODY_BYTES }))
+ const completion = (content: string, textPart: string) => ({
+ executionTicket: 'invalid-ticket',
+ requestHash: 'a'.repeat(64),
+ request: {
+ schemaVersion: 'codex-completion-request.v2',
+ messages: [
+ {
+ role: 'user',
+ content,
+ contentParts: [
+ { type: 'text', text: textPart },
+ { type: 'image', mimeType: 'image/png', data: 'iVBORw0KGgo=' },
+ ],
+ },
+ ],
+ },
+ })
+ const post = (content: string, textPart: string) =>
+ request(runtimeApp)
+ .post('/internal/runtime/v1/codex/completions')
+ .set('Authorization', `Bearer ${platformToken()}`)
+ .send(completion(content, textPart))
+ // The share the proxy measures: no ticket, no image data, and the text
+ // part blanked because it repeats content.
+ const { executionTicket: _ticket, ...empty } = completion('', '')
+ empty.request.messages[0].contentParts[1] = { type: 'image', mimeType: 'image/png', data: '' }
+ const budget = LIMITS.maxRequestBodyBytes + ENVELOPE_ALLOWANCE_BYTES
+ const exact = budget - Buffer.byteLength(JSON.stringify(empty), 'utf8')
+
+ // A 4.5 MiB prompt beside an image passes the size checks and stops at the
+ // ticket gate.
+ const text = 'x'.repeat(4.5 * 1024 * 1024)
+ const repeated = await post(text, text)
+ expect(repeated.status).toBe(403)
+ expect(repeated.body).toEqual({ error: 'ticket_invalid' })
+
+ // Exact boundary: the largest text reaches the ticket gate, one more byte
+ // is refused 413.
+ const atLimit = await post('x'.repeat(exact), 'x'.repeat(exact))
+ expect(atLimit.status).toBe(403)
+ expect(atLimit.body).toEqual({ error: 'ticket_invalid' })
+ const overLimit = await post('x'.repeat(exact + 1), 'x'.repeat(exact + 1))
+ expect(overLimit.status).toBe(413)
+ expect(overLimit.body).toEqual({ error: 'payload_too_large' })
+
+ // Parts that do not repeat content keep both copies and are refused.
+ const mismatched = await post(`${text.slice(1)}y`, text)
+ expect(mismatched.status).toBe(413)
+ expect(mismatched.body).toEqual({ error: 'payload_too_large' })
+ }, 60_000)
+
it('rate limits the completion endpoint before body parsing and authorization', async () => {
const { runtimeApp } = createProxyApps(
config({ maxBodyBytes: 1024 })
diff --git a/control-api/package-lock.json b/control-api/package-lock.json
index 9c880649c..681d569f4 100644
--- a/control-api/package-lock.json
+++ b/control-api/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-api",
- "version": "0.1.681",
+ "version": "0.1.682",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-api",
- "version": "0.1.681",
+ "version": "0.1.682",
"dependencies": {
"@clerum/display-field": "file:../packages/display-field",
"@clerum/grok-provider-attempt-contract": "file:../packages/grok-provider-attempt-contract",
diff --git a/control-api/package.json b/control-api/package.json
index 2e64e8220..e82aab44f 100644
--- a/control-api/package.json
+++ b/control-api/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-api",
- "version": "0.1.681",
+ "version": "0.1.682",
"description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets",
"main": "dist/main.js",
"engines": {
diff --git a/control-api/test/services.llmProviderAttemptAuthorizer.grok.test.ts b/control-api/test/services.llmProviderAttemptAuthorizer.grok.test.ts
index a2b113633..e989e3785 100644
--- a/control-api/test/services.llmProviderAttemptAuthorizer.grok.test.ts
+++ b/control-api/test/services.llmProviderAttemptAuthorizer.grok.test.ts
@@ -577,6 +577,40 @@ describe('authorizeLlmProviderAttempt grok-subscription', () => {
expect(current.insertAttempt).toHaveBeenCalledTimes(1)
})
+ // Review R3-L3: V2 text sits in content and in its text parts; the
+ // non-image budget counts it once, as the same text counts in V1.
+ it('R3-L3 authorizes a 4.5 MiB prompt beside an image and refuses parts that do not repeat content', async () => {
+ const current = deps()
+ const text = 'x'.repeat(4.5 * 1024 * 1024)
+ const withParts = (content: string, textPart: string) => ({
+ ...REQUEST,
+ schemaVersion: 'grok-completion-request.v2' as const,
+ messages: [
+ {
+ role: 'user' as const,
+ content,
+ contentParts: [{ type: 'text' as const, text: textPart }, fixturePngPart()],
+ },
+ ],
+ })
+ const request = withParts(text, text)
+ const result = await authorizeLlmProviderAttempt(claims(), body({ request }), current)
+ expect(result).toMatchObject({
+ executionTicket: 'grok-ticket.jwt',
+ requestHash: hashGrokCompletionRequest(request),
+ })
+ expect(current.insertAttempt).toHaveBeenCalledTimes(1)
+ // Parts that do not repeat content keep both copies on the budget.
+ await expect(
+ authorizeLlmProviderAttempt(
+ claims(),
+ body({ request: withParts(`${text.slice(1)}y`, text) }),
+ current
+ )
+ ).rejects.toMatchObject({ code: 'payload_too_large' })
+ expect(current.insertAttempt).toHaveBeenCalledTimes(1)
+ }, 30_000)
+
it('checks the V2 proxy envelope inside the transaction, after signing and before commit', async () => {
const transactionEvents: string[] = []
const current = deps({
diff --git a/control-api/test/services.llmProviderAttemptAuthorizer.test.ts b/control-api/test/services.llmProviderAttemptAuthorizer.test.ts
index e24d1c898..1fe9be958 100644
--- a/control-api/test/services.llmProviderAttemptAuthorizer.test.ts
+++ b/control-api/test/services.llmProviderAttemptAuthorizer.test.ts
@@ -6,6 +6,7 @@ import {
ENVELOPE_ALLOWANCE_BYTES as CODEX_CONTRACT_ENVELOPE_ALLOWANCE_BYTES,
LIMITS,
VISUAL_LIMITS,
+ hashCodexCompletionRequest,
hashCodexCompletionRequestV1,
} from '@clerum/llm-provider-attempt-contract'
import { streamCodexCompletion } from '../../codex-llm-proxy/src/codexTransport'
@@ -721,6 +722,50 @@ describe('authorizeLlmProviderAttempt', () => {
expect(current.insertAttempt).not.toHaveBeenCalled()
})
+ // Review R3-L3: V2 text sits in content and in its text parts; the
+ // non-image budget counts it once, as the same text counts in V1.
+ it('R3-L3 authorizes a 4.5 MiB prompt beside an image and refuses parts that do not repeat content', async () => {
+ const fixture = JSON.parse(
+ readFileSync(
+ new URL(
+ '../../packages/llm-provider-attempt-contract/fixtures/visual-requests.json',
+ import.meta.url
+ ),
+ 'utf8'
+ )
+ ) as { png: { messages: Array<{ contentParts: Array<{ type: string }> }> } }
+ const png = fixture.png.messages[0]!.contentParts.find(part => part.type === 'image')
+ expect(png).toBeDefined()
+ const text = 'x'.repeat(4.5 * 1024 * 1024)
+ const withParts = (content: string, textPart: string) => ({
+ ...REQUEST,
+ schemaVersion: 'codex-completion-request.v2',
+ messages: [
+ {
+ role: 'user' as const,
+ content,
+ contentParts: [{ type: 'text' as const, text: textPart }, png],
+ },
+ ],
+ })
+ const request = withParts(text, text)
+ const result = await authorizeLlmProviderAttempt(claims(), body({ request }), current)
+ expect(result).toMatchObject({
+ executionTicket: 'ticket.jwt',
+ requestHash: hashCodexCompletionRequest(request),
+ })
+ expect(current.insertAttempt).toHaveBeenCalledTimes(1)
+ // Parts that do not repeat content keep both copies on the budget.
+ await expect(
+ authorizeLlmProviderAttempt(
+ claims(),
+ body({ request: withParts(`${text.slice(1)}y`, text) }),
+ current
+ )
+ ).rejects.toMatchObject({ code: 'payload_too_large' })
+ expect(current.insertAttempt).toHaveBeenCalledTimes(1)
+ }, 30_000)
+
it('maps an over-dimension image to payload_too_large', async () => {
const current = deps()
await expect(
diff --git a/grok-llm-proxy/test/server.security.test.ts b/grok-llm-proxy/test/server.security.test.ts
index a058e3493..ff71e9d9c 100644
--- a/grok-llm-proxy/test/server.security.test.ts
+++ b/grok-llm-proxy/test/server.security.test.ts
@@ -387,6 +387,61 @@ describe('grok-llm-proxy security surface', () => {
expect(imageSized.body.error).toBe('ticket_invalid')
}, 30_000)
+ // Review R3-L3: V2 text repeated in content and in its text parts counts
+ // once on the non-image budget, as the same text does in V1.
+ it('R3-L3 counts V2 text repeated in content and its text parts once', async () => {
+ const { runtimeApp } = createProxyApps(config({ maxBodyBytes: DEFAULT_MAX_BODY_BYTES }))
+ const completion = (content: string, textPart: string) => ({
+ executionTicket: 'invalid-ticket',
+ requestHash: 'a'.repeat(64),
+ request: {
+ schemaVersion: 'grok-completion-request.v2',
+ messages: [
+ {
+ role: 'user',
+ content,
+ contentParts: [
+ { type: 'text', text: textPart },
+ { type: 'image', mimeType: 'image/png', data: 'iVBORw0KGgo=' },
+ ],
+ },
+ ],
+ },
+ })
+ const post = (content: string, textPart: string) =>
+ request(runtimeApp)
+ .post('/internal/runtime/v1/grok/completions')
+ .set('Authorization', `Bearer ${platformToken()}`)
+ .send(completion(content, textPart))
+ // The share the proxy measures: no ticket, no image data, and the text
+ // part blanked because it repeats content.
+ const { executionTicket: _ticket, ...empty } = completion('', '')
+ empty.request.messages[0].contentParts[1] = { type: 'image', mimeType: 'image/png', data: '' }
+ const budget = LIMITS.maxRequestBodyBytes + ENVELOPE_ALLOWANCE_BYTES
+ const exact = budget - Buffer.byteLength(JSON.stringify(empty), 'utf8')
+
+ // A 4.5 MiB prompt beside an image passes the size checks and stops at the
+ // ticket gate.
+ const text = 'x'.repeat(4.5 * 1024 * 1024)
+ const repeated = await post(text, text)
+ expect(repeated.status).toBe(403)
+ expect(repeated.body).toEqual({ error: 'ticket_invalid' })
+
+ // Exact boundary: the largest text reaches the ticket gate, one more byte
+ // is refused 413.
+ const atLimit = await post('x'.repeat(exact), 'x'.repeat(exact))
+ expect(atLimit.status).toBe(403)
+ expect(atLimit.body).toEqual({ error: 'ticket_invalid' })
+ const overLimit = await post('x'.repeat(exact + 1), 'x'.repeat(exact + 1))
+ expect(overLimit.status).toBe(413)
+ expect(overLimit.body).toEqual({ error: 'payload_too_large' })
+
+ // Parts that do not repeat content keep both copies and are refused.
+ const mismatched = await post(`${text.slice(1)}y`, text)
+ expect(mismatched.status).toBe(413)
+ expect(mismatched.body).toEqual({ error: 'payload_too_large' })
+ }, 60_000)
+
it('rejects a platform JWT whose hostRefs do not bind the ticket hostRef', async () => {
const { runtimeApp } = createProxyApps(config())
const foreign = sign(
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index 7f183a0de..6dff9be3d 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.477",
+ "version": "0.1.478",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.477",
+ "version": "0.1.478",
"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 fc186a3e8..df246a7d9 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.477",
+ "version": "0.1.478",
"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__/codexSubscription.test.ts b/mcp-host/src/llm/__tests__/codexSubscription.test.ts
index 2ffaddb4f..fcc6bebdf 100644
--- a/mcp-host/src/llm/__tests__/codexSubscription.test.ts
+++ b/mcp-host/src/llm/__tests__/codexSubscription.test.ts
@@ -1535,6 +1535,41 @@ describe('CodexSubscriptionProvider', () => {
expect(wired.authorize).not.toHaveBeenCalled()
})
+ // Review R3-L3: V2 text sits in content and in its text part. The host's
+ // canonical check counts it once, as the same text counts in V1.
+ it('R3-L3 dispatches a 4.5 MiB prompt beside an image and keeps refusing parts that do not repeat content', async () => {
+ const wired = deps()
+ const provider = new CodexSubscriptionProvider('gpt-5.6-luna', wired as never)
+ const text = 'x'.repeat(4.5 * 1024 * 1024)
+ const image = imagePart(PNG_2X2_BASE64, 'a')
+
+ await provider.completeSingleTurn([
+ { role: 'user', content: text, contentParts: [{ type: 'text', text }, image] },
+ ])
+ expect(wired.authorize).toHaveBeenCalledTimes(1)
+ expect(wired.stream).toHaveBeenCalledTimes(1)
+ const body = wired.authorize.mock.calls[0][0]
+ expect(body.request.schemaVersion).toBe('codex-completion-request.v2')
+ expect(body.request.messages[0].content).toBe(text)
+
+ // Parts that do not repeat content keep both copies on the budget.
+ const mismatched = await provider
+ .completeSingleTurn([
+ {
+ role: 'user',
+ content: `${text.slice(1)}y`,
+ contentParts: [{ type: 'text', text }, image],
+ },
+ ])
+ .catch((e: unknown) => e)
+ expect(mismatched).toMatchObject({
+ code: 'request_limit_exceeded',
+ message:
+ 'codex completion request rejected: request exceeds maxRequestBodyBytes outside image data',
+ })
+ expect(wired.authorize).toHaveBeenCalledTimes(1)
+ }, 30_000)
+
it('T-R9-11d keeps conversation-volume refusals on a V2 request as context length', async () => {
const wired = deps()
const provider = new CodexSubscriptionProvider('gpt-5.6-luna', wired as never)
diff --git a/mcp-host/src/llm/__tests__/grokSubscription.test.ts b/mcp-host/src/llm/__tests__/grokSubscription.test.ts
index 420eb5599..a687d6c92 100644
--- a/mcp-host/src/llm/__tests__/grokSubscription.test.ts
+++ b/mcp-host/src/llm/__tests__/grokSubscription.test.ts
@@ -1435,6 +1435,40 @@ describe('GrokSubscriptionProvider image input (#784)', () => {
expect(wired.authorizer.authorize).toHaveBeenCalledTimes(1)
})
+ // Review R3-L3: V2 text sits in content and in its text part. The host's
+ // canonical check counts it once, as the same text counts in V1.
+ it('R3-L3 dispatches a 4.5 MiB prompt beside an image and keeps refusing parts that do not repeat content', async () => {
+ const wired = deps()
+ const provider = new GrokSubscriptionProvider('grok-4.6', wired as never)
+ const text = 'x'.repeat(4.5 * 1024 * 1024)
+ const image = attachmentImage(GROK_PNG_2X2_BASE64, 'a')
+
+ await provider.completeSingleTurn([
+ { role: 'user', content: text, contentParts: [{ type: 'text', text }, image] },
+ ])
+ expect(wired.authorizer.authorize).toHaveBeenCalledTimes(1)
+ expect(wired.proxy.stream).toHaveBeenCalledTimes(1)
+ const body = wired.authorizer.authorize.mock.calls[0][0]
+ expect(body.request.schemaVersion).toBe('grok-completion-request.v2')
+ expect(body.request.messages[0].content).toBe(text)
+
+ // Parts that do not repeat content keep both copies on the budget.
+ const mismatched = await provider
+ .completeSingleTurn([
+ {
+ role: 'user',
+ content: `${text.slice(1)}y`,
+ contentParts: [{ type: 'text', text }, image],
+ },
+ ])
+ .catch((e: unknown) => e)
+ expect(mismatched).toMatchObject({
+ code: 'request_limit_exceeded',
+ message: 'request exceeds maxRequestBodyBytes outside image data',
+ })
+ expect(wired.authorizer.authorize).toHaveBeenCalledTimes(1)
+ }, 30_000)
+
// #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