Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(desktop): count everything the posted request charges to the 6 MiB share
Alfredo Lopez1da0a8833427
diff --git a/desktop-app/ui/src/constants/attachments.ts b/desktop-app/ui/src/constants/attachments.tsindex 0f1e39c62..6286ac64b 100644--- a/desktop-app/ui/src/constants/attachments.ts+++ b/desktop-app/ui/src/constants/attachments.ts@@ -32,6 +32,12 @@ export const COMPOSER_MAX_NON_IMAGE_BODY_BYTES = 6 * 1024 * 1024export const COMPOSER_MAX_REQUEST_BODY_BYTES = 24 * 1024 * 1024/** JSON envelope around the text and the attachments: model, revision, keys. */export const COMPOSER_REQUEST_ENVELOPE_BYTES = 4096+/**+ * Fields rpc-proxy adds to the Host request before mcp-host measures it+ * (`rpc-proxy/src/routes/rpc.ts`): `sender`, `messageId`, `metadata` and the+ * trace context, about 720 bytes at their largest.+ */+export const COMPOSER_FORWARDED_FIELDS_BYTES = 2048/*** JSON around one `kind:'file'` entry besides its base64 and its filename:* the id, the two media types, the sha256 hex and the fixed keys.diff --git a/desktop-app/ui/src/hooks/domain/__tests__/useAgentChatController.test.tsx b/desktop-app/ui/src/hooks/domain/__tests__/useAgentChatController.test.tsxindex 2bdd2b902..fafc4617b 100644--- a/desktop-app/ui/src/hooks/domain/__tests__/useAgentChatController.test.tsx+++ b/desktop-app/ui/src/hooks/domain/__tests__/useAgentChatController.test.tsx@@ -14,10 +14,16 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'import { makeTaskKey } from '@contexts/AgentTaskTrackerContext/types'import { act, waitFor } from '@testing-library/react'import { classifyBytes } from '@clerum/gfs-interaction-policy'-import { base64Length } from '@lib/composerFileAdmission'+import { base64Length, composerNonImageShareBytes } from '@lib/composerFileAdmission'+import { buildComposerFileReferences } from '@lib/composerFileReferences'+import { buildComposerRequestContent } from '@lib/composerReferencesPrompt'import { loadHostModels, resetHostModelSelectionStore } from '@lib/hostModelSelectionStore'import type { HostModelsResult } from '../../../../../src/types'-import type { ComposerImageAttachment, ReadyComposerFileAttachment } from '../../../uiTypes'+import type {+ ComposerGlobalFileReference,+ ComposerImageAttachment,+ ReadyComposerFileAttachment,+} from '../../../uiTypes'import { renderController } from './__fixtures__/controllerHarness'import { type MockClerum, installMockClerum, uninstallMockClerum } from './__fixtures__/mockClerum'@@ -1875,7 +1881,7 @@ describe('interrupted generated-file contract', () => {describe('sendAgentMessage — per-message limits for documents (#678)', () => {const MIB = 1024 * 1024const TEXT_SHARE_BLOCKER =- 'The message text and file details take more than 6.0 MiB once encoded. Shorten the message or remove a file.'+ 'The message text and attachment details take more than 6.0 MiB once encoded. Shorten the message or remove an attachment.'const FILE_QUOTA_BLOCKER ='The attached files take more than 16.0 MiB once encoded. Remove a file.'const REQUEST_BODY_BLOCKER =@@ -1951,9 +1957,10 @@ describe('sendAgentMessage — per-message limits for documents (#678)', () => {it('refuses text that overflows the 6 MiB share beside an 11 MiB file, and sends one byte less', async () => {const big = readyTextFile('file-big', 'big.txt', 11 * MIB)const rendered = await mountedWithFiles([big])- // Envelope (4096) + text + the file's JSON name and fixed fields; the file's- // base64 is credited to the file quota.- const fullShareText = 6 * MIB - 4096 - JSON.stringify('big.txt').length - 640+ // Envelope (4096) + the fields rpc-proxy adds (2048) + the text as a JSON+ // string (+2 quotes) + the agent twice as a JSON string (2 × 9) + the file's+ // JSON name and fixed fields; the file's base64 is credited to the file quota.+ const fullShareText = 6 * MIB - 4096 - 2048 - 2 - 18 - JSON.stringify('big.txt').length - 640await act(async () => {await rendered.result.current.handleSendAgentMessage('x'.repeat(fullShareText + 1))@@ -2046,4 +2053,120 @@ describe('sendAgentMessage — per-message limits for documents (#678)', () => {expect(clerum.rpc.invokeHostMessage).toHaveBeenCalledTimes(1)expect(sentAttachmentIds()).toHaveLength(20)})++ const utf8Bytes = (value: string) => new TextEncoder().encode(value).length++ /** Four Global Files references whose names take three UTF-8 bytes per character. */+ function wideNameReferences(): ComposerGlobalFileReference[] {+ return Array.from({ length: 4 }, (_, index) => {+ const resourceId = String(index).repeat(32)+ return {+ id: `global-file:main:${resourceId}`,+ type: 'global_file' as const,+ resourceId,+ drive: 'main',+ gfsUri: `gfs://main/${resourceId}`,+ label: `${'文'.repeat(251)}.txt`,+ version: 1,+ bytes: 2048,+ }+ })+ }++ it('counts the serialized references and the agent in the 6 MiB share, and sends the draft that fills it', async () => {+ const references = wideNameReferences()+ const rendered = await mountedWithFiles([readyTextFile('file-doc', 'doc.txt', 1024)])+ act(() => rendered.result.current.handleAddComposerReferenceAttachments(references))+ const jsonBytes = (value: unknown) => utf8Bytes(JSON.stringify(value))+ const contentBytes = (draft: string) =>+ jsonBytes(buildComposerRequestContent(draft, references))+ // Everything the share holds besides the draft, each part as the request+ // body carries it: envelope, fields rpc-proxy adds, the text with its+ // references section, the serialized references, the agent twice and the+ // file details.+ const referencesBytes = jsonBytes(buildComposerFileReferences(references))+ const fixedBytes =+ 4096 ++ 2048 ++ (contentBytes('x') - 1) ++ referencesBytes ++ 2 * jsonBytes('agent-x') ++ jsonBytes('doc.txt') ++ 640+ const boundary = 6 * MIB - fixedBytes+ // The text grows one byte per draft character: the arithmetic above holds.+ expect(contentBytes('x'.repeat(boundary))).toBe(contentBytes('x') - 1 + boundary)+ // Leaving the references out would let the blocked draft below through.+ expect(referencesBytes).toBeGreaterThan(1)++ await act(async () => {+ await rendered.result.current.handleSendAgentMessage('x'.repeat(boundary + 1))+ })+ expectBlocked(rendered, TEXT_SHARE_BLOCKER)+ expect(rendered.result.current.composerFileAttachments).toHaveLength(1)+ expect(rendered.result.current.composerReferenceAttachments).toHaveLength(4)++ await act(async () => {+ await rendered.result.current.handleSendAgentMessage('x'.repeat(boundary))+ })+ expect(clerum.rpc.invokeHostMessage).toHaveBeenCalledTimes(1)+ const request = clerum.rpc.invokeHostMessage.mock.calls[0]?.[1] as {+ content: string+ fileReferences: Parameters<typeof composerNonImageShareBytes>[0]['fileReferences']+ attachments: Array<Record<string, unknown>>+ }+ // The request carries the text and references the boundary was computed from.+ expect(request.content).toBe(buildComposerRequestContent('x'.repeat(boundary), references))+ expect(request.fileReferences).toEqual(buildComposerFileReferences(references))+ // The estimate bounds what is posted: the request as serialized without the+ // base64 the file quota credits, plus 1 KiB for the fields rpc-proxy adds.+ const postedShareBytes = utf8Bytes(+ JSON.stringify({+ ...request,+ attachments: request.attachments.map(attachment => ({ ...attachment, dataBase64: '' })),+ })+ )+ expect(+ composerNonImageShareBytes({+ content: request.content,+ fileReferences: request.fileReferences,+ hostRef: 'agent-x',+ files: [{ filename: 'doc.txt' }],+ images: [],+ })+ ).toBeGreaterThanOrEqual(postedShareBytes + 1024)+ })++ it('counts the JSON escaping of the text in the share, and sends fewer escaped characters', async () => {+ const rendered = await mountedWithFiles([readyTextFile('file-doc', 'doc.txt', 1024)])+ // A newline is one byte typed and two once escaped in the JSON body.+ await act(async () => {+ await rendered.result.current.handleSendAgentMessage(`a${'\n'.repeat(3.5 * MIB)}b`)+ })+ expectBlocked(rendered, TEXT_SHARE_BLOCKER)++ await act(async () => {+ await rendered.result.current.handleSendAgentMessage(`a${'\n'.repeat(2 * MIB)}b`)+ })+ expect(clerum.rpc.invokeHostMessage).toHaveBeenCalledTimes(1)+ expect(sentAttachmentIds()).toEqual(['file-doc'])+ })++ it('checks the share on a send without attachments, and sends shorter text', async () => {+ clerum.rpc.invokeHostMessage.mockResolvedValue({ response: 'done' })+ const rendered = renderController()+ await settleMount()+ // A double quote is one byte typed and two once escaped in the JSON body.+ await act(async () => {+ await rendered.result.current.handleSendAgentMessage('"'.repeat(Math.floor(3.2 * MIB)))+ })+ expectBlocked(rendered, TEXT_SHARE_BLOCKER)++ await act(async () => {+ await rendered.result.current.handleSendAgentMessage('"'.repeat(Math.floor(2.9 * MIB)))+ })+ expect(clerum.rpc.invokeHostMessage).toHaveBeenCalledTimes(1)+ const request = clerum.rpc.invokeHostMessage.mock.calls[0]?.[1] as { content: string }+ expect(request.content).toHaveLength(Math.floor(2.9 * MIB))+ })})diff --git a/desktop-app/ui/src/hooks/domain/__tests__/useComposerAttachments.files.test.tsx b/desktop-app/ui/src/hooks/domain/__tests__/useComposerAttachments.files.test.tsxindex 1b0f4b695..24131de36 100644--- a/desktop-app/ui/src/hooks/domain/__tests__/useComposerAttachments.files.test.tsx+++ b/desktop-app/ui/src/hooks/domain/__tests__/useComposerAttachments.files.test.tsx@@ -8,6 +8,10 @@import { afterEach, describe, expect, it, vi } from 'vitest'import { act, renderHook, waitFor } from '@testing-library/react'import { COMPOSER_MAX_ATTACHMENTS, COMPOSER_MAX_FILE_BYTES } from '@constants/attachments'+import { buildComposerFileReferences } from '@lib/composerFileReferences'+import { composerRequestBaseContent } from '@lib/composerHostRequest'+import { buildComposerRequestContent } from '@lib/composerReferencesPrompt'+import type { ComposerGlobalFileReference } from '../../../uiTypes'import { useComposerAttachments } from '../useComposerAttachments';(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true@@ -318,4 +322,113 @@ describe('useComposerAttachments — documents (#678)', () => {expect(result.current.composerFileAttachments).toEqual(kept)})++ function globalReferences(count: number, label: (index: number) => string) {+ return Array.from({ length: count }, (_, index): ComposerGlobalFileReference => {+ const resourceId = index.toString(16).padStart(32, '0')+ return {+ id: `global-file:main:${resourceId}`,+ type: 'global_file',+ resourceId,+ drive: 'main',+ gfsUri: `gfs://main/${resourceId}`,+ label: label(index),+ version: 1,+ bytes: 2048,+ }+ })+ }++ function refusalTexts(result: ReturnType<typeof render>['result']): string[] {+ return result.current.composerFileRefusals.map(refusal => refusal.text)+ }++ it('counts the selected references and the agent against the 6 MiB share', () => {+ const { result } = render()+ const references = globalReferences(4, () => `${'文'.repeat(251)}.txt`)+ act(() => {+ result.current.handleAddComposerReferenceAttachments(references)+ })+ const doc = new File([new Uint8Array(1024)], 'doc.txt', { type: 'text/plain' })+ const utf8 = (value: unknown) => new TextEncoder().encode(JSON.stringify(value)).length+ const contentBytes = (draft: string) =>+ utf8(buildComposerRequestContent(composerRequestBaseContent(draft, 0, 1), references))+ // Everything the share holds besides the draft, each part as the request+ // body carries it: envelope, fields rpc-proxy adds, the text with its+ // references section, the serialized references, the agent twice and the+ // file details.+ const referencesBytes = utf8(buildComposerFileReferences(references))+ const fixedBytes =+ 4096 ++ 2048 ++ (contentBytes('x') - 1) ++ referencesBytes ++ 2 * utf8('agent-x') ++ utf8('doc.txt') ++ 640+ const boundary = 6 * 1024 * 1024 - fixedBytes+ // The text grows one byte per draft character: the arithmetic above holds.+ expect(contentBytes('x'.repeat(boundary))).toBe(contentBytes('x') - 1 + boundary)+ // Leaving the references out would admit the refused draft below.+ expect(referencesBytes).toBeGreaterThan(1)++ act(() => {+ result.current.handleAddComposerFiles([doc], 'x'.repeat(boundary + 1))+ })+ expect(refusalTexts(result)).toEqual([+ 'doc.txt does not fit: the text and attachment details can take at most 6.0 MiB per message once encoded.',+ ])+ expect(result.current.composerFileAttachments).toHaveLength(0)++ // Liveness witness: the draft that fills the share exactly is admitted.+ act(() => {+ result.current.handleAddComposerFiles([doc], 'x'.repeat(boundary))+ })+ expect(refusalTexts(result)).toEqual([])+ expect(statuses(result)).toEqual(['reading'])+ })++ it('refuses a file while no agent is selected, and admits it for an agent', () => {+ const doc = textFile('doc.txt', 'hello')+ const withoutAgent = render(null)+ act(() => {+ withoutAgent.result.current.handleAddComposerFiles([doc], '')+ })+ expect(refusalTexts(withoutAgent.result)).toEqual(['Select an agent before attaching files.'])+ expect(withoutAgent.result.current.composerFileAttachments).toHaveLength(0)++ const withAgent = render('agent-x')+ act(() => {+ withAgent.result.current.handleAddComposerFiles([doc], '')+ })+ expect(refusalTexts(withAgent.result)).toEqual([])+ expect(statuses(withAgent.result)).toEqual(['reading'])+ })++ it('refuses a file beside more than 10 Global Files references, and admits it beside 10', () => {+ const doc = textFile('doc.txt', 'hello')+ const eleven = render()+ act(() => {+ eleven.result.current.handleAddComposerReferenceAttachments(+ globalReferences(11, index => `file-${index}.md`)+ )+ })+ act(() => {+ eleven.result.current.handleAddComposerFiles([doc], '')+ })+ expect(refusalTexts(eleven.result)).toEqual(['A message can reference at most 10 files.'])+ expect(eleven.result.current.composerFileAttachments).toHaveLength(0)++ const ten = render()+ act(() => {+ ten.result.current.handleAddComposerReferenceAttachments(+ globalReferences(10, index => `file-${index}.md`)+ )+ })+ act(() => {+ ten.result.current.handleAddComposerFiles([doc], '')+ })+ expect(refusalTexts(ten.result)).toEqual([])+ expect(statuses(ten.result)).toEqual(['reading'])+ })})diff --git a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts b/desktop-app/ui/src/hooks/domain/useAgentChatController.tsindex 0505f486c..5bbc306d0 100644--- a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts+++ b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts@@ -2865,40 +2865,46 @@ export function useAgentChatController({baseContentForRequest,effectiveReferences)- if (effectiveFiles.length > 0) {- // The limits rpc-proxy and mcp-host enforce, most specific first: the- // count, the file quota, the share left for text and file details, then- // the whole body (images included), so the reason names what to remove.- const requestForBudget = {- content: effectiveContentForRequest,- fileReferences: fileReferencesForSend,- hostRef: sendAgent,- files: effectiveFiles,- images: effectiveAttachments,- }- let blocker: string | null = null- if (effectiveAttachments.length + effectiveFiles.length > COMPOSER_MAX_ATTACHMENTS) {- blocker = `A message can carry at most ${COMPOSER_MAX_ATTACHMENTS} attachments.`- } else if (composerFileBase64Bytes(effectiveFiles) > COMPOSER_MAX_TOTAL_FILE_BASE64_BYTES) {- blocker = `The attached files take more than ${formatFileSize(COMPOSER_MAX_TOTAL_FILE_BASE64_BYTES)} once encoded. Remove a file.`- } else if (- composerNonImageShareBytes(requestForBudget) > COMPOSER_MAX_NON_IMAGE_BODY_BYTES- ) {- blocker = `The message text and file details take more than ${formatFileSize(COMPOSER_MAX_NON_IMAGE_BODY_BYTES)} once encoded. Shorten the message or remove a file.`- } else if (- composerRequestBodyBytes(- requestForBudget,- effectiveAttachments.reduce((total, image) => total + image.dataBase64.length, 0)- ) > COMPOSER_MAX_REQUEST_BODY_BYTES- ) {- blocker = `The attachments and text take more than ${formatFileSize(COMPOSER_MAX_REQUEST_BODY_BYTES)} once encoded. Remove an attachment or shorten the message.`- }- if (blocker) {- setAgentError(blocker)- pushToast(blocker, 'error')- releaseSendSetup()- return- }+ // The limits rpc-proxy and mcp-host enforce, most specific first: the+ // count, the file quota, the share left for text and attachment details,+ // then the whole body (images included), so the reason names what to+ // remove. The share applies to every send; the others only when files+ // are attached.+ const requestForBudget = {+ content: effectiveContentForRequest,+ fileReferences: fileReferencesForSend,+ hostRef: sendAgent,+ files: effectiveFiles,+ images: effectiveAttachments,+ }+ const hasFiles = effectiveFiles.length > 0+ let budgetBlocker: string | null = null+ if (+ hasFiles &&+ effectiveAttachments.length + effectiveFiles.length > COMPOSER_MAX_ATTACHMENTS+ ) {+ budgetBlocker = `A message can carry at most ${COMPOSER_MAX_ATTACHMENTS} attachments.`+ } else if (+ hasFiles &&+ composerFileBase64Bytes(effectiveFiles) > COMPOSER_MAX_TOTAL_FILE_BASE64_BYTES+ ) {+ budgetBlocker = `The attached files take more than ${formatFileSize(COMPOSER_MAX_TOTAL_FILE_BASE64_BYTES)} once encoded. Remove a file.`+ } else if (composerNonImageShareBytes(requestForBudget) > COMPOSER_MAX_NON_IMAGE_BODY_BYTES) {+ budgetBlocker = `The message text and attachment details take more than ${formatFileSize(COMPOSER_MAX_NON_IMAGE_BODY_BYTES)} once encoded. Shorten the message or remove an attachment.`+ } else if (+ hasFiles &&+ composerRequestBodyBytes(+ requestForBudget,+ effectiveAttachments.reduce((total, image) => total + image.dataBase64.length, 0)+ ) > COMPOSER_MAX_REQUEST_BODY_BYTES+ ) {+ budgetBlocker = `The attachments and text take more than ${formatFileSize(COMPOSER_MAX_REQUEST_BODY_BYTES)} once encoded. Remove an attachment or shorten the message.`+ }+ if (budgetBlocker) {+ setAgentError(budgetBlocker)+ pushToast(budgetBlocker, 'error')+ releaseSendSetup()+ return}let sendChatId = currentChatId ?? activeChatIdconst sendStillAuthorized = () =>diff --git a/desktop-app/ui/src/hooks/domain/useComposerAttachments.ts b/desktop-app/ui/src/hooks/domain/useComposerAttachments.tsindex e7939f4eb..543faa5f9 100644--- a/desktop-app/ui/src/hooks/domain/useComposerAttachments.ts+++ b/desktop-app/ui/src/hooks/domain/useComposerAttachments.ts@@ -2,6 +2,9 @@ import { useCallback, useEffect, useRef, useState } from 'react'import { COMPOSER_MAX_ATTACHMENTS } from '@constants/attachments'import { clearComposerDraft, clearComposerDraftAfterSend } from '@lib/composerDraftStore'import { composerFileAdmissionError, readComposerFile } from '@lib/composerFileAdmission'+import { buildComposerFileReferences } from '@lib/composerFileReferences'+import { composerRequestBaseContent } from '@lib/composerHostRequest'+import { buildComposerRequestContent } from '@lib/composerReferencesPrompt'import type {ComposerFileAttachment,ComposerFileRefusal,@@ -48,8 +51,10 @@ export function useComposerAttachments({// asynchronous read decide from the latest attachments, not from the render// that started them.const composerFilesRef = useRef<ComposerFileAttachment[]>([])- const composerImageCountRef = useRef(0)- composerImageCountRef.current = composerImageAttachments.length+ const composerImagesRef = useRef<ComposerImageAttachment[]>([])+ composerImagesRef.current = composerImageAttachments+ const composerReferencesRef = useRef<ComposerReferenceAttachment[]>([])+ composerReferencesRef.current = composerReferenceAttachmentsconst commitComposerFiles = useCallback((update: (previous: ComposerFileAttachment[]) => ComposerFileAttachment[]) => {@@ -200,14 +205,39 @@ export function useComposerAttachments({composerAttachmentRevisionRef.current += 1if (!files.length) returnconst refusals: ComposerFileRefusal[] = []+ // Admission measures the request the files would be sent with: the+ // content with its references section, the structured references and the+ // agent it is posted to. Without an agent, or with references the send+ // itself would refuse, no file can be admitted.+ const images = composerImagesRef.current+ const references = composerReferencesRef.current+ let referencesProblem: string | null = null+ let fileReferences: ReturnType<typeof buildComposerFileReferences> = []+ try {+ fileReferences = buildComposerFileReferences(references)+ } catch (error) {+ referencesProblem = error instanceof Error ? error.message : String(error)+ }for (const file of files) {const id = crypto.randomUUID()const current = composerFilesRef.current- const error = composerFileAdmissionError(file, {- attachedCount: composerImageCountRef.current + current.length,- files: current,- request: { content: draft, fileReferences: [], hostRef: selectedAgent ?? '', images: [] },- })+ const error =+ selectedAgent === null+ ? 'Select an agent before attaching files.'+ : (referencesProblem ??+ composerFileAdmissionError(file, {+ attachedCount: images.length + current.length,+ files: current,+ request: {+ content: buildComposerRequestContent(+ composerRequestBaseContent(draft.trim(), images.length, current.length + 1),+ references+ ),+ fileReferences,+ hostRef: selectedAgent,+ images,+ },+ }))composerAttachmentOrderRef.current += 1const base = {id,diff --git a/desktop-app/ui/src/lib/__tests__/composerFileAdmission.test.ts b/desktop-app/ui/src/lib/__tests__/composerFileAdmission.test.tsindex 266f498fe..380f37942 100644--- a/desktop-app/ui/src/lib/__tests__/composerFileAdmission.test.ts+++ b/desktop-app/ui/src/lib/__tests__/composerFileAdmission.test.ts@@ -2,6 +2,7 @@ import { describe, expect, it } from 'vitest'import { createHash } from 'node:crypto'import {COMPOSER_FILE_ENTRY_METADATA_BYTES,+ COMPOSER_FORWARDED_FIELDS_BYTES,COMPOSER_MAX_ATTACHMENTS,COMPOSER_MAX_FILE_BYTES,COMPOSER_MAX_NON_IMAGE_BODY_BYTES,@@ -18,6 +19,17 @@ import {fileNameProblem,readComposerFile,} from '../composerFileAdmission'+import { buildComposerFileReferences } from '../composerFileReferences'++/** UTF-8 bytes of a value as JSON.stringify writes it into the request body. */+function jsonBytes(value: unknown): number {+ return new TextEncoder().encode(JSON.stringify(value)).length+}++/** The fixed part of the share: the envelope, the fields rpc-proxy adds, and+ * `channelId` plus `hostRef`, both the agent `agent-1`. */+const FIXED_SHARE_BYTES =+ COMPOSER_REQUEST_ENVELOPE_BYTES + COMPOSER_FORWARDED_FIELDS_BYTES + 2 * jsonBytes('agent-1')/** The rest of a posted request whose message text is `content`. */function requestWithText(content = '') {@@ -183,17 +195,19 @@ describe('composerFileAdmissionError (#678)', () => {request: requestWithText('a'.repeat(COMPOSER_MAX_NON_IMAGE_BODY_BYTES)),})).toBe(- 'a.bin does not fit: the text and file details can take at most 6.0 MiB per message once encoded.'+ 'a.bin does not fit: the text and attachment details can take at most 6.0 MiB per message once encoded.')})it('admits an 11 MiB file beside text that fills the 6 MiB share exactly (#678)', () => {const file = { name: 'big.bin', size: COMPOSER_MAX_FILE_BYTES }- // The share holds the envelope, the text and the file's JSON name plus- // fixed fields; the file's base64 is credited to the file quota.+ // The share holds the fixed part, the text as a JSON string (two quotes)+ // and the file's JSON name plus fixed fields; the file's base64 is credited+ // to the file quota.const fullShareText =COMPOSER_MAX_NON_IMAGE_BODY_BYTES -- COMPOSER_REQUEST_ENVELOPE_BYTES -+ FIXED_SHARE_BYTES -+ 2 -JSON.stringify('big.bin').length -COMPOSER_FILE_ENTRY_METADATA_BYTESexpect(@@ -207,7 +221,7 @@ describe('composerFileAdmissionError (#678)', () => {...EMPTY_CONTEXT,request: requestWithText('a'.repeat(fullShareText + 1)),})- ).toContain('the text and file details can take at most 6.0 MiB')+ ).toContain('the text and attachment details can take at most 6.0 MiB')})it.each([@@ -232,10 +246,7 @@ describe('composer request body accounting (#678)', () => {it('charges the envelope, the text and the file details, but not the file base64, to the share', () => {const file = { filename: 'a.pdf', sizeBytes: 1000 }expect(composerNonImageShareBytes({ ...requestWithText('a'.repeat(50)), files: [file] })).toBe(- COMPOSER_REQUEST_ENVELOPE_BYTES +- 50 +- JSON.stringify('a.pdf').length +- COMPOSER_FILE_ENTRY_METADATA_BYTES+ FIXED_SHARE_BYTES + 52 + JSON.stringify('a.pdf').length + COMPOSER_FILE_ENTRY_METADATA_BYTES)expect(composerFileDetailBytes(file)).toBe(JSON.stringify('a.pdf').length + COMPOSER_FILE_ENTRY_METADATA_BYTES@@ -255,14 +266,70 @@ describe('composer request body accounting (#678)', () => {files,images: [{ name: 'one.png' }, { name: 'two.png' }],}+ // The image names and fixed fields are already in the share.expect(composerRequestBodyBytes(request, 8_000)).toBe(- composerNonImageShareBytes(request) +- base64Length(1000) +- 8_000 ++ composerNonImageShareBytes(request) + base64Length(1000) + 8_000+ )+ })++ it('charges the text as the JSON string the request carries, escapes included (R1-H1)', () => {+ // A newline is written as two bytes (\n), plus the two quotes.+ expect(composerNonImageShareBytes({ ...requestWithText('\n'.repeat(1000)), files: [] })).toBe(+ FIXED_SHARE_BYTES + 2 * 1000 + 2+ )+ // A quote is two bytes too; a non-ASCII character keeps its UTF-8 length.+ expect(composerNonImageShareBytes({ ...requestWithText('"ñ'), files: [] })).toBe(+ FIXED_SHARE_BYTES + 2 + 2 + 2+ )+ })++ it('charges the serialized fileReferences next to the text (R1-H1)', () => {+ const fileReferences = buildComposerFileReferences(+ ['0123456789abcdef0123456789abcdef', 'fedcba9876543210fedcba9876543210'].map(+ (resourceId, index) => ({+ id: `global-file:main:${resourceId}`,+ type: 'global_file' as const,+ resourceId,+ drive: 'main',+ gfsUri: `gfs://main/${resourceId}`,+ label: `${'文'.repeat(200)}-${index}.md`,+ version: 1,+ bytes: 2048,+ })+ )+ )+ const withoutReferences = composerNonImageShareBytes({ ...requestWithText('hi'), files: [] })+ expect(+ composerNonImageShareBytes({ ...requestWithText('hi'), fileReferences, files: [] }) -+ withoutReferences+ ).toBe(new TextEncoder().encode(JSON.stringify(fileReferences)).length)+ expect(fileReferences).toHaveLength(2)+ })++ it('charges the name and fixed fields of every image to the share (R1-H1)', () => {+ const withoutImages = composerNonImageShareBytes({ ...requestWithText('hi'), files: [] })+ expect(+ composerNonImageShareBytes({+ ...requestWithText('hi'),+ files: [],+ images: [{ name: 'scan.png' }, { name: 'ñ.png' }],+ }) - withoutImages+ ).toBe(+ JSON.stringify('scan.png').length ++ JSON.stringify('ñ.png').length ++ 1 +2 * COMPOSER_FILE_ENTRY_METADATA_BYTES)})+ it('charges the agent twice, as channelId and as hostRef (R1-H1)', () => {+ const request = { ...requestWithText('hi'), files: [] }+ expect(+ composerNonImageShareBytes({ ...request, hostRef: 'agent-12' }) -+ composerNonImageShareBytes(request)+ ).toBe(2)+ })+it('measures a multi-byte name in UTF-8 bytes', () => {const ascii = composerFileDetailBytes({ filename: 'aaaa.txt' })const accented = composerFileDetailBytes({ filename: 'ññññ.txt' })diff --git a/desktop-app/ui/src/lib/composerFileAdmission.ts b/desktop-app/ui/src/lib/composerFileAdmission.tsindex 553971c4e..7c6c419fe 100644--- a/desktop-app/ui/src/lib/composerFileAdmission.ts+++ b/desktop-app/ui/src/lib/composerFileAdmission.ts@@ -1,6 +1,7 @@import { type FileReferenceV1, classifyBytes } from '@clerum/gfs-interaction-policy'import {COMPOSER_FILE_ENTRY_METADATA_BYTES,+ COMPOSER_FORWARDED_FIELDS_BYTES,COMPOSER_MAX_ATTACHMENTS,COMPOSER_MAX_FILE_BYTES,COMPOSER_MAX_NON_IMAGE_BODY_BYTES,@@ -54,15 +55,18 @@ export function base64Length(sizeBytes: number): number {return Math.ceil(sizeBytes / 3) * 4}+/** UTF-8 bytes of `value` as `JSON.stringify` writes it into the request body. */+function jsonBytes(value: unknown): number {+ return textEncoder.encode(JSON.stringify(value)).length+}+/*** Bytes one `kind:'file'` entry adds to the JSON request body besides its* base64: the file name and the fixed fields. rpc-proxy and mcp-host charge* these to the non-image share; only the base64 is credited to the file quota.*/export function composerFileDetailBytes(file: { filename: string }): number {- return (- textEncoder.encode(JSON.stringify(file.filename)).length + COMPOSER_FILE_ENTRY_METADATA_BYTES- )+ return jsonBytes(file.filename) + COMPOSER_FILE_ENTRY_METADATA_BYTES}/** Bytes of base64 the file quota is charged for these files. */@@ -71,22 +75,32 @@ export function composerFileBase64Bytes(files: ReadonlyArray<{ sizeBytes: number}/**- * Bytes of one request body that rpc-proxy and mcp-host credit to neither the- * image quota nor the file quota: the JSON envelope, the message text and the- * non-base64 fields of every `kind:'file'`.+ * Upper bound of the bytes of one request body that rpc-proxy and mcp-host+ * credit to neither the image quota nor the file quota, measured as they+ * measure it: the JSON body after rpc-proxy has added its own fields. It+ * counts the envelope, those fields, the text and the references as JSON+ * (escapes included), the agent (sent as `channelId` and as `hostRef`), and+ * the name and fixed fields of every attachment. Only attachment base64 is+ * left out.*/export function composerNonImageShareBytes(request: ComposerNonImageShareRequest): number {return (COMPOSER_REQUEST_ENVELOPE_BYTES +- textEncoder.encode(request.content).length +- request.files.reduce((total, file) => total + composerFileDetailBytes(file), 0)+ COMPOSER_FORWARDED_FIELDS_BYTES ++ jsonBytes(request.content) ++ (request.fileReferences.length ? jsonBytes(request.fileReferences) : 0) ++ 2 * jsonBytes(request.hostRef) ++ request.files.reduce((total, file) => total + composerFileDetailBytes(file), 0) ++ request.images.reduce(+ (total, image) => total + jsonBytes(image.name) + COMPOSER_FILE_ENTRY_METADATA_BYTES,+ 0+ ))}/*** Bytes of the whole request body: the non-image share plus the base64 of the- * files and of the images. An image entry is counted with the same fixed-field- * allowance as a file entry, which is an upper bound.+ * files and of the images.*/export function composerRequestBodyBytes(request: ComposerNonImageShareRequest & {@@ -95,10 +109,7 @@ export function composerRequestBodyBytes(imageBase64Bytes: number): number {return (- composerNonImageShareBytes(request) +- composerFileBase64Bytes(request.files) +- imageBase64Bytes +- request.images.length * COMPOSER_FILE_ENTRY_METADATA_BYTES+ composerNonImageShareBytes(request) + composerFileBase64Bytes(request.files) + imageBase64Bytes)}@@ -135,7 +146,7 @@ export function composerFileAdmissionError(}const bodyBytes = composerNonImageShareBytes({ ...context.request, files })if (bodyBytes > COMPOSER_MAX_NON_IMAGE_BODY_BYTES) {- return `${file.name} does not fit: the text and file details can take at most ${formatFileSize(COMPOSER_MAX_NON_IMAGE_BODY_BYTES)} per message once encoded.`+ return `${file.name} does not fit: the text and attachment details can take at most ${formatFileSize(COMPOSER_MAX_NON_IMAGE_BODY_BYTES)} per message once encoded.`}return null}