Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(contracts): count V2 text repeated in its text parts once
Alfredo Lopez292e1fb5558d
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.jsonindex 6dff9be3d..e27c32cf6 100644--- a/mcp-host/package-lock.json+++ b/mcp-host/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-mcp-host",- "version": "0.1.478",+ "version": "0.1.479","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-mcp-host",- "version": "0.1.478",+ "version": "0.1.479","license": "SEE LICENSE IN LICENSE","dependencies": {"@anthropic-ai/sdk": "^0.32.0",diff --git a/mcp-host/package.json b/mcp-host/package.jsonindex df246a7d9..566043608 100644--- a/mcp-host/package.json+++ b/mcp-host/package.json@@ -1,6 +1,6 @@{"name": "clerum-mcp-host",- "version": "0.1.478",+ "version": "0.1.479","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.tsindex fcc6bebdf..74bd6c95a 100644--- a/mcp-host/src/llm/__tests__/codexSubscription.test.ts+++ b/mcp-host/src/llm/__tests__/codexSubscription.test.ts@@ -1537,7 +1537,7 @@ describe('CodexSubscriptionProvider', () => {// 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 () => {+ it('R3-L3 dispatches a 4.5 MiB prompt beside an image and still refuses text over the cap', async () => {const wired = deps()const provider = new CodexSubscriptionProvider('gpt-5.6-luna', wired as never)const text = 'x'.repeat(4.5 * 1024 * 1024)@@ -1552,22 +1552,28 @@ describe('CodexSubscriptionProvider', () => {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+ // The host derives content from the text parts (projectMessage), so every+ // V2 turn it sends repeats its text; a caller's disagreeing content never+ // reaches the wire.+ await provider.completeSingleTurn([+ { role: 'user', content: 'other', contentParts: [{ type: 'text', text }, image] },+ ])+ expect(wired.authorize).toHaveBeenCalledTimes(2)+ expect(wired.authorize.mock.calls[1][0].request.messages[0].content).toBe(text)++ // Counted once is still counted: text over the non-image cap is refused.+ const over = 'x'.repeat(LIMITS.maxRequestBodyBytes)+ const refused = await provider.completeSingleTurn([- {- role: 'user',- content: `${text.slice(1)}y`,- contentParts: [{ type: 'text', text }, image],- },+ { role: 'user', content: over, contentParts: [{ type: 'text', text: over }, image] },]).catch((e: unknown) => e)- expect(mismatched).toMatchObject({+ expect(refused).toMatchObject({code: 'request_limit_exceeded',message:'codex completion request rejected: request exceeds maxRequestBodyBytes outside image data',})- expect(wired.authorize).toHaveBeenCalledTimes(1)+ expect(wired.authorize).toHaveBeenCalledTimes(2)}, 30_000)it('T-R9-11d keeps conversation-volume refusals on a V2 request as context length', async () => {diff --git a/mcp-host/src/llm/__tests__/grokSubscription.test.ts b/mcp-host/src/llm/__tests__/grokSubscription.test.tsindex a687d6c92..17a0bde7f 100644--- a/mcp-host/src/llm/__tests__/grokSubscription.test.ts+++ b/mcp-host/src/llm/__tests__/grokSubscription.test.ts@@ -1437,7 +1437,7 @@ describe('GrokSubscriptionProvider image input (#784)', () => {// 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 () => {+ it('R3-L3 dispatches a 4.5 MiB prompt beside an image and still refuses text over the cap', async () => {const wired = deps()const provider = new GrokSubscriptionProvider('grok-4.6', wired as never)const text = 'x'.repeat(4.5 * 1024 * 1024)@@ -1452,21 +1452,27 @@ describe('GrokSubscriptionProvider image input (#784)', () => {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+ // The host derives content from the text parts (projectMessage), so every+ // V2 turn it sends repeats its text; a caller's disagreeing content never+ // reaches the wire.+ await provider.completeSingleTurn([+ { role: 'user', content: 'other', contentParts: [{ type: 'text', text }, image] },+ ])+ expect(wired.authorizer.authorize).toHaveBeenCalledTimes(2)+ expect(wired.authorizer.authorize.mock.calls[1][0].request.messages[0].content).toBe(text)++ // Counted once is still counted: text over the non-image cap is refused.+ const over = 'x'.repeat(LIMITS.maxRequestBodyBytes)+ const refused = await provider.completeSingleTurn([- {- role: 'user',- content: `${text.slice(1)}y`,- contentParts: [{ type: 'text', text }, image],- },+ { role: 'user', content: over, contentParts: [{ type: 'text', text: over }, image] },]).catch((e: unknown) => e)- expect(mismatched).toMatchObject({+ expect(refused).toMatchObject({code: 'request_limit_exceeded',message: 'request exceeds maxRequestBodyBytes outside image data',})- expect(wired.authorizer.authorize).toHaveBeenCalledTimes(1)+ expect(wired.authorizer.authorize).toHaveBeenCalledTimes(2)}, 30_000)// #806 review M6, user decision: keep Codex parity. More than maxImagesdiff --git a/packages/grok-provider-attempt-contract/index.cjs b/packages/grok-provider-attempt-contract/index.cjsindex b4bb287d2..9348dcf03 100644--- a/packages/grok-provider-attempt-contract/index.cjs+++ b/packages/grok-provider-attempt-contract/index.cjs@@ -299,38 +299,73 @@ function requestBodyLimitBytes(request) {}/**- * Copy of `messages` with the `data` of every image part replaced by an empty- * string. Only the touched objects are copied (shallow); the input is not- * modified.+ * Copy of `messages` holding only what the non-image budget counts. Every image+ * part's `data` is blanked. A message's text parts are blanked too when their+ * '\n' join equals `content`: V2 carries that text twice, in `content` and in+ * its parts, and V1 counts it once. Text parts that do not repeat `content` are+ * kept, so a mismatched message is measured with both copies (fail closed); the+ * parser refuses it anyway. Only the touched objects are copied (shallow); the+ * input is not modified.*/-function blankImagePayloadsInMessages(messages) {+function nonImageShadowMessages(messages) {if (!Array.isArray(messages)) return messagesreturn messages.map(message => {const parts = isPlainObject(message) ? message.contentParts : undefinedif (!Array.isArray(parts)) return message+ const blankText = textPartsRepeatContent(message.content, parts)return {...message,- contentParts: parts.map(part =>- isPlainObject(part) && part.type === 'image' ? { ...part, data: '' } : part- ),+ contentParts: parts.map(part => {+ if (!isPlainObject(part)) return part+ if (part.type === 'image') return { ...part, data: '' }+ if (blankText && part.type === 'text') return { ...part, text: '' }+ return part+ }),}})}/**- * UTF-8 length of a request with every image payload replaced by an empty- * string: the caller-controlled text/tool share of the body.+ * True when the text parts of `parts`, joined with '\n', equal `content`, the+ * rule the parser enforces. Compared in place, without building the joined+ * string. A part that is not a plain object, a text part whose `text` is not a+ * string, or a `content` that is not a string makes it false, so a malformed+ * message keeps its text on the budget.+ */+function textPartsRepeatContent(content, parts) {+ if (typeof content !== 'string') return false+ let offset = 0+ let first = true+ for (const part of parts) {+ if (!isPlainObject(part)) return false+ if (part.type !== 'text') continue+ if (typeof part.text !== 'string') return false+ if (!first) {+ if (content.charCodeAt(offset) !== 10) return false+ offset += 1+ }+ if (!content.startsWith(part.text, offset)) return false+ offset += part.text.length+ first = false+ }+ return offset === content.length+}++/**+ * UTF-8 length of a request's non-image shadow (`nonImageShadowMessages`): the+ * caller-controlled text/tool share of the body, with repeated V2 text counted+ * once.** V2's larger ceiling exists for image data only, so this share stays on the V1* budget and declaring V2 never buys 35 MiB of text or tool definitions. The* measurement builds a detached shadow — the input's key order, shallow copies,- * only image `data` blanked — measures it and discards it. The original- * request, its hash and its projection are never touched, so this cannot change- * what is authorized or signed.+ * image `data` and repeated text parts blanked — measures it and discards it.+ * The original request, its hash and its projection are never touched, so this+ * cannot change what is authorized or signed.*/function measureNonImageRequestBytes(input) {return Buffer.byteLength(- JSON.stringify({ ...input, messages: blankImagePayloadsInMessages(input.messages) }),+ JSON.stringify({ ...input, messages: nonImageShadowMessages(input.messages) }),'utf8')}@@ -339,7 +374,7 @@ function measureNonImageRequestBytes(input) {* Authorize JSON is a different document from a Grok request or proxy* envelope. `requestBodyLimitBytes(body.request)` still gates the whole* wrapper (35 MiB only when the nested request declares V2). This helper- * blanks image payloads inside `body.request` so wrapper fields — ids,+ * measures the non-image shadow of `body.request` so wrapper fields — ids,* hashes, ticket links — stay on the `maxRequestBodyBytes` non-image budget.*/function measureNonImageAuthorizeBytes(body) {@@ -353,7 +388,7 @@ function measureNonImageAuthorizeBytes(body) {return Buffer.byteLength(JSON.stringify({...body,- request: { ...request, messages: blankImagePayloadsInMessages(request.messages) },+ request: { ...request, messages: nonImageShadowMessages(request.messages) },}),'utf8')@@ -361,8 +396,9 @@ function measureNonImageAuthorizeBytes(body) {/*** Proxy completion JSON includes `executionTicket`, which authorize never- * measured. Blank images and drop the ticket so a body that sat under the- * `maxRequestBodyBytes` authorize budget is not 413'd on redeem by ~1 KB of JWT.+ * measured. Measure the non-image shadow and drop the ticket so a body that sat+ * under the `maxRequestBodyBytes` authorize budget is not 413'd on redeem by+ * ~1 KB of JWT.*/function measureNonImageCompletionBytes(body) {if (!isPlainObject(body)) {diff --git a/packages/llm-provider-attempt-contract/index.cjs b/packages/llm-provider-attempt-contract/index.cjsindex ddfef6cb5..ea465c122 100644--- a/packages/llm-provider-attempt-contract/index.cjs+++ b/packages/llm-provider-attempt-contract/index.cjs@@ -268,38 +268,73 @@ function requestBodyLimitBytes(request) {}/**- * Copy of `messages` with the `data` of every image part replaced by an empty- * string. Only the touched objects are copied (shallow); the input is not- * modified.+ * Copy of `messages` holding only what the non-image budget counts. Every image+ * part's `data` is blanked. A message's text parts are blanked too when their+ * '\n' join equals `content`: V2 carries that text twice, in `content` and in+ * its parts, and V1 counts it once. Text parts that do not repeat `content` are+ * kept, so a mismatched message is measured with both copies (fail closed); the+ * parser refuses it anyway. Only the touched objects are copied (shallow); the+ * input is not modified.*/-function blankImagePayloadsInMessages(messages) {+function nonImageShadowMessages(messages) {if (!Array.isArray(messages)) return messagesreturn messages.map(message => {const parts = isPlainObject(message) ? message.contentParts : undefinedif (!Array.isArray(parts)) return message+ const blankText = textPartsRepeatContent(message.content, parts)return {...message,- contentParts: parts.map(part =>- isPlainObject(part) && part.type === 'image' ? { ...part, data: '' } : part- ),+ contentParts: parts.map(part => {+ if (!isPlainObject(part)) return part+ if (part.type === 'image') return { ...part, data: '' }+ if (blankText && part.type === 'text') return { ...part, text: '' }+ return part+ }),}})}/**- * UTF-8 length of a request with every image payload replaced by an empty- * string: the caller-controlled text/tool share of the body.+ * True when the text parts of `parts`, joined with '\n', equal `content`, the+ * rule the parser enforces. Compared in place, without building the joined+ * string. A part that is not a plain object, a text part whose `text` is not a+ * string, or a `content` that is not a string makes it false, so a malformed+ * message keeps its text on the budget.+ */+function textPartsRepeatContent(content, parts) {+ if (typeof content !== 'string') return false+ let offset = 0+ let first = true+ for (const part of parts) {+ if (!isPlainObject(part)) return false+ if (part.type !== 'text') continue+ if (typeof part.text !== 'string') return false+ if (!first) {+ if (content.charCodeAt(offset) !== 10) return false+ offset += 1+ }+ if (!content.startsWith(part.text, offset)) return false+ offset += part.text.length+ first = false+ }+ return offset === content.length+}++/**+ * UTF-8 length of a request's non-image shadow (`nonImageShadowMessages`): the+ * caller-controlled text/tool share of the body, with repeated V2 text counted+ * once.** V2's larger ceiling exists for image data only, so this share stays on the V1* budget and declaring V2 never buys 24 MiB of text or tool definitions. The* measurement builds a detached shadow — the input's key order, shallow copies,- * only image `data` blanked — measures it and discards it. The original- * request, its hash and its projection are never touched, so this cannot change- * what is authorized or signed.+ * image `data` and repeated text parts blanked — measures it and discards it.+ * The original request, its hash and its projection are never touched, so this+ * cannot change what is authorized or signed.*/function measureNonImageRequestBytes(input) {return Buffer.byteLength(- JSON.stringify({ ...input, messages: blankImagePayloadsInMessages(input.messages) }),+ JSON.stringify({ ...input, messages: nonImageShadowMessages(input.messages) }),'utf8')}@@ -308,7 +343,7 @@ function measureNonImageRequestBytes(input) {* Authorize JSON is a different document from a Codex request or proxy* envelope. `requestBodyLimitBytes(body.request)` still gates the whole* wrapper (24 MiB only when the nested request declares V2). This helper- * blanks image payloads inside `body.request` so wrapper fields — ids,+ * measures the non-image shadow of `body.request` so wrapper fields — ids,* hashes, ticket links — stay on the `maxRequestBodyBytes` non-image budget.*/function measureNonImageAuthorizeBytes(body) {@@ -322,7 +357,7 @@ function measureNonImageAuthorizeBytes(body) {return Buffer.byteLength(JSON.stringify({...body,- request: { ...request, messages: blankImagePayloadsInMessages(request.messages) },+ request: { ...request, messages: nonImageShadowMessages(request.messages) },}),'utf8')@@ -330,8 +365,9 @@ function measureNonImageAuthorizeBytes(body) {/*** Proxy completion JSON includes `executionTicket`, which authorize never- * measured. Blank images and drop the ticket so a body that sat under the- * `maxRequestBodyBytes` authorize budget is not 413'd on redeem by ~1 KB of JWT.+ * measured. Measure the non-image shadow and drop the ticket so a body that sat+ * under the `maxRequestBodyBytes` authorize budget is not 413'd on redeem by+ * ~1 KB of JWT.*/function measureNonImageCompletionBytes(body) {if (!isPlainObject(body)) {