Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(desktop): compare the Grok composer total in decoded bytes
Alfredo Lopez390db8afafb2
diff --git a/desktop-app/ui/src/components/agents/ComposerPanel.tsx b/desktop-app/ui/src/components/agents/ComposerPanel.tsxindex 08fbb1f1c..540e98508 100644--- a/desktop-app/ui/src/components/agents/ComposerPanel.tsx+++ b/desktop-app/ui/src/components/agents/ComposerPanel.tsx@@ -25,6 +25,7 @@ import {type ComposerImageBudget,type ComposerImageTotalBudget,composerImageBudget,+ composerImageCountedBytes,} from '@constants/attachments'import { useContextsDataController } from '@hooks/domain/useContextsDataController'import { useMcpServersDataController } from '@hooks/domain/useMcpServersDataController'@@ -208,8 +209,9 @@ export function ComposerPanel({ inline = false, agentSelector }: ComposerPanelPrconst hostModelSelection = useHostModels(selectedAgent ?? '', activeChatId ?? '')const imageAttachmentBlockMessage = hostModelSelection.imageBlockMessage// Size ceilings follow the host provider: Codex keeps #650 (16 MiB / 2048 /- // hop-owned aggregate). Every other image-capable host keeps #669 (3 MiB +- // 8 MB combined). Capability still comes from the host projection, not this.+ // hop-owned aggregate). Grok is 16 MiB per image and 16 MiB decoded in total+ // (#784). Every other image-capable host keeps #669 (3 MiB + 8 MB combined).+ // Capability still comes from the host projection, not this.const imageBudget = composerImageBudget(hostModelSelection.data?.provider)const selectedAgentContext = selectedAgent? String(agentContextByName[selectedAgent] || '').trim()@@ -558,13 +560,16 @@ export function ComposerPanel({ inline = false, agentSelector }: ComposerPanelPr} not added.`)}- // General images travel inline in one 10 MB body (#669); Grok counts the- // same inline bytes against the 16 MiB ingress total (#784). Codex leaves- // the aggregate to the hop (#650).- let totalBase64Bytes = composerImageAttachments.reduce(- (total, attachment) => total + attachment.dataBase64.length,- 0- )+ // General images travel inline in one 10 MB body, counted in base64+ // characters (#669). Grok counts decoded bytes against the 16 MiB ingress+ // total (#784). Codex leaves the aggregate to the hop (#650).+ const totalBudget = imageBudget.total+ let totalCountedBytes = totalBudget+ ? composerImageAttachments.reduce(+ (total, attachment) => total + composerImageCountedBytes(totalBudget, attachment),+ 0+ )+ : 0for (const [index, { file, mimeType }] of selected.entries()) {if (composerImageExceedsPerImageBudget(file.size, imageBudget.maxImageBytes)) {@@ -600,20 +605,24 @@ export function ComposerPanel({ inline = false, agentSelector }: ComposerPanelPrrevokePreviewUrl(previewUrl)continue}+ const candidate = { dataBase64, sizeBytes: file.size }if (- imageBudget.total != null &&- totalBase64Bytes + dataBase64.length > imageBudget.total.maxBase64Bytes+ totalBudget != null &&+ totalCountedBytes + composerImageCountedBytes(totalBudget, candidate) >+ totalBudget.maxBytes) {validationErrors.push(`${file.name || 'Image'} does not fit in this message: the images in one message are limited to ${composerTotalLimitLabel(- imageBudget.total,+ totalBudget,imageBudget.sizeUnit)} in total. Send the attached images first or remove one.`)revokePreviewUrl(previewUrl)continue}- totalBase64Bytes += dataBase64.length+ if (totalBudget != null) {+ totalCountedBytes += composerImageCountedBytes(totalBudget, candidate)+ }accepted.push({id: crypto.randomUUID(),name: buildAttachmentName(file, source, mimeType, index),@@ -668,11 +677,16 @@ export function ComposerPanel({ inline = false, agentSelector }: ComposerPanelPrupdated.dataBase64,imageBudget.maxDimension)- const otherBase64Bytes = composerImageAttachments.reduce(- (total, attachment) =>- attachment.id === updated.id ? total : total + attachment.dataBase64.length,- 0- )+ const totalBudget = imageBudget.total+ const otherCountedBytes = totalBudget+ ? composerImageAttachments.reduce(+ (total, attachment) =>+ attachment.id === updated.id+ ? total+ : total + composerImageCountedBytes(totalBudget, attachment),+ 0+ )+ : 0if (composerImageExceedsPerImageBudget(updated.sizeBytes, imageBudget.maxImageBytes)) {throw new Error(`${updated.name || 'Image'} was kept unchanged. Max size is ${formatComposerMebibytes(@@ -686,12 +700,12 @@ export function ComposerPanel({ inline = false, agentSelector }: ComposerPanelPr)}if (- imageBudget.total != null &&- otherBase64Bytes + updated.dataBase64.length > imageBudget.total.maxBase64Bytes+ totalBudget != null &&+ otherCountedBytes + composerImageCountedBytes(totalBudget, updated) > totalBudget.maxBytes) {throw new Error(`${updated.name || 'Image'} was kept unchanged. The images in one message are limited to ${composerTotalLimitLabel(- imageBudget.total,+ totalBudget,imageBudget.sizeUnit)} in total.`)diff --git a/desktop-app/ui/src/components/agents/__tests__/ComposerPanel.test.tsx b/desktop-app/ui/src/components/agents/__tests__/ComposerPanel.test.tsxindex ee033d594..0c5a5ecea 100644--- a/desktop-app/ui/src/components/agents/__tests__/ComposerPanel.test.tsx+++ b/desktop-app/ui/src/components/agents/__tests__/ComposerPanel.test.tsx@@ -11,7 +11,6 @@ import {COMPOSER_MAX_IMAGE_BYTES,COMPOSER_MAX_TOTAL_IMAGE_BASE64_BYTES,GROK_COMPOSER_MAX_IMAGE_BYTES,- GROK_COMPOSER_MAX_TOTAL_IMAGE_BASE64_BYTES,} from '@constants/attachments'import type { HostModelsResult } from '@hooks/useChatStore'import {@@ -837,25 +836,23 @@ describe('ComposerPanel Grok image budget', () => {Object.assign(composerModelState, SUPPORTED_CAPABILITY)})- // The budget enforces the base64 total (22369624 = 4 * ceil(16 MiB / 3))- // while the copy names the ingress's 16 MiB decoded total, not the rounded- // base64 figure (21 MiB).+ // The budget sums the decoded `sizeBytes` of each image against the+ // ingress's 16 MiB decoded total, and the copy names that figure.it('names the 16 MiB decoded total when an annotation save exceeds it', () => {- const grokTotalBase64Bytes = 4 * Math.ceil((16 * 1024 * 1024) / 3)const already: ComposerImageAttachment = {id: 'already-attached',name: 'already.png',mimeType: 'image/png',- dataBase64: 'A'.repeat(grokTotalBase64Bytes - 32),- sizeBytes: 1,+ dataBase64: 'AAAA',+ sizeBytes: GROK_COMPOSER_MAX_IMAGE_BYTES - 32,previewDataUrl: 'data:image/png;base64,AAAA',}const editing: ComposerImageAttachment = {id: 'editing',name: 'edit.png',mimeType: 'image/png',- dataBase64: 'B'.repeat(16),- sizeBytes: 1,+ dataBase64: 'BBBB',+ sizeBytes: 16,previewDataUrl: 'data:image/png;base64,BBBB',}composerState.composerImageAttachments = [already, editing]@@ -863,23 +860,22 @@ describe('ComposerPanel Grok image budget', () => {fireEvent.click(screen.getByText('edit.png'))expect(annotationCanvasMock.onSave).toEqual(expect.any(Function))- expect(() =>- annotationCanvasMock.onSave?.({- ...editing,- dataBase64: 'B'.repeat(64),- })- ).toThrow(+ // Liveness witness: a save that keeps the total at the limit is applied.+ annotationCanvasMock.onSave?.({ ...editing, sizeBytes: 32 })+ expect(actionsMock.handleUpdateComposerImageAttachment).toHaveBeenCalledTimes(1)++ expect(() => annotationCanvasMock.onSave?.({ ...editing, sizeBytes: 33 })).toThrow('edit.png was kept unchanged. The images in one message are limited to 16 MiB in total.')- expect(actionsMock.handleUpdateComposerImageAttachment).not.toHaveBeenCalled()+ // Still the one call from the accepted save: the refused save never called through.+ expect(actionsMock.handleUpdateComposerImageAttachment).toHaveBeenCalledTimes(1)})it('refuses a picked image that would push the message past the 16 MiB ingress total', async () => {const { container } = render(<ComposerPanel inline />)const rawBytes = Math.floor(GROK_COMPOSER_MAX_IMAGE_BYTES / 2)- const base64Bytes = Math.ceil(rawBytes / 3) * 4- expect(base64Bytes * 2).toBeLessThanOrEqual(GROK_COMPOSER_MAX_TOTAL_IMAGE_BASE64_BYTES)- expect(base64Bytes * 3).toBeGreaterThan(GROK_COMPOSER_MAX_TOTAL_IMAGE_BASE64_BYTES)+ expect(rawBytes * 2).toBeLessThanOrEqual(GROK_COMPOSER_MAX_IMAGE_BYTES)+ expect(rawBytes * 3).toBeGreaterThan(GROK_COMPOSER_MAX_IMAGE_BYTES)const files = ['first.png', 'second.png', 'third.png'].map((name, index) => {const bytes = new Uint8Array(rawBytes)bytes.set(PNG_BYTES)@@ -900,6 +896,67 @@ describe('ComposerPanel Grok image budget', () => {})})+describe('ComposerPanel Grok image total counts decoded bytes', () => {+ beforeEach(() => {+ composerModelState.data = {+ provider: 'grok-subscription',+ hostDefault: 'grok-4.6',+ sessionModel: 'grok-4.6',+ degraded: false,+ models: [{ name: 'grok-4.6', displayName: 'Grok 4.6' }],+ }+ composerModelState.effectiveModel = 'grok-4.6'+ Object.assign(composerModelState, SUPPORTED_CAPABILITY)+ })++ // The ingress counts decoded bytes. 8,388,609 + 8,388,608 decoded bytes is+ // one byte over 16 MiB, while the base64 characters of the two files sum to+ // exactly the 22,369,624 the base64 rule allowed, so only a decoded sum+ // refuses the second image.+ it('refuses the image that takes the decoded total one byte past 16 MiB', async () => {+ const { container } = render(<ComposerPanel inline />)+ const sizes = [8 * 1024 * 1024 + 1, 8 * 1024 * 1024]+ const files = ['first.png', 'second.png'].map((name, index) => {+ const bytes = new Uint8Array(sizes[index] as number)+ bytes.set(PNG_BYTES)+ return imageFile(name, 'image/png', bytes)+ })++ fireEvent.change(pickerInput(container), { target: { files } })++ await waitFor(() =>+ expect(actionsMock.handleAddComposerImageAttachments).toHaveBeenCalledTimes(1)+ )+ const [batch] = addedBatches()+ expect(batch?.map(attachment => [attachment.name, attachment.sizeBytes])).toEqual([+ ['first.png', 8 * 1024 * 1024 + 1],+ ])+ expect(screen.getByRole('alert').textContent).toBe(+ 'second.png does not fit in this message: the images in one message are limited to 16 MiB in total. Send the attached images first or remove one.'+ )+ })++ it('accepts two images whose decoded sizes sum to exactly 16 MiB', async () => {+ const { container } = render(<ComposerPanel inline />)+ const files = ['first.png', 'second.png'].map(name => {+ const bytes = new Uint8Array(8 * 1024 * 1024)+ bytes.set(PNG_BYTES)+ return imageFile(name, 'image/png', bytes)+ })++ fireEvent.change(pickerInput(container), { target: { files } })++ await waitFor(() =>+ expect(actionsMock.handleAddComposerImageAttachments).toHaveBeenCalledTimes(1)+ )+ expect(addedBatches()[0]?.map(attachment => attachment.name)).toEqual([+ 'first.png',+ 'second.png',+ ])+ expect(screen.queryByRole('alert')).toBeNull()+ })+})+describe('ComposerPanel with a pending image after a model switch', () => {const pendingImage: ComposerImageAttachment = {id: 'pending-image-1',diff --git a/desktop-app/ui/src/constants/__tests__/attachments.test.ts b/desktop-app/ui/src/constants/__tests__/attachments.test.tsindex c8d350131..dd05837f1 100644--- a/desktop-app/ui/src/constants/__tests__/attachments.test.ts+++ b/desktop-app/ui/src/constants/__tests__/attachments.test.ts@@ -5,6 +5,7 @@ import {COMPOSER_MAX_IMAGE_BYTES,COMPOSER_MAX_TOTAL_IMAGE_BASE64_BYTES,composerImageBudget,+ composerImageCountedBytes,} from '../attachments'const MIB = 1024 * 1024@@ -15,18 +16,19 @@ describe('composerImageBudget', () => {expect(budget).toEqual({maxImageBytes: 16777216,total: {- maxBase64Bytes: 22369624,+ counts: 'decoded',+ maxBytes: 16777216,labelBytes: 16777216,},maxDimension: null,sizeUnit: 'MiB',})- // 16 MiB decoded, expressed as base64: the total rpc-proxy and mcp-host- // accept for one message, not the 20 MiB xAI allows per image.+ // The total is the decoded 16 MiB that rpc-proxy and mcp-host accept for+ // one message, not the 20 MiB xAI allows per image. The composer sums the+ // decoded `sizeBytes` of each image, so the check matches the ingress to+ // the byte.expect(budget.maxImageBytes).toBe(16 * MIB)- expect(budget.total?.maxBase64Bytes).toBe(4 * Math.ceil((16 * MIB) / 3))- // The copy names the decoded total (16 MiB), not the base64 bytes the- // picker counts (which would round to "21 MiB").+ expect(budget.total?.maxBytes).toBe(16 * MIB)expect(budget.total?.labelBytes).toBe(16 * MIB)expect(15 * MIB).toBeLessThanOrEqual(budget.maxImageBytes)expect(17 * MIB).toBeGreaterThan(budget.maxImageBytes)@@ -49,7 +51,8 @@ describe('composerImageBudget', () => {expect(composerImageBudget(provider)).toEqual({maxImageBytes: COMPOSER_MAX_IMAGE_BYTES,total: {- maxBase64Bytes: COMPOSER_MAX_TOTAL_IMAGE_BASE64_BYTES,+ counts: 'base64',+ maxBytes: COMPOSER_MAX_TOTAL_IMAGE_BASE64_BYTES,labelBytes: COMPOSER_MAX_TOTAL_IMAGE_BASE64_BYTES,},maxDimension: null,@@ -60,3 +63,20 @@ describe('composerImageBudget', () => {})})++describe('composerImageCountedBytes', () => {+ // 5 base64 characters decode to 3 bytes or fewer, so the two units differ.+ const attachment = { dataBase64: 'AAAAA', sizeBytes: 3 }++ it('counts decoded bytes for a decoded total', () => {+ const total = composerImageBudget('grok-subscription').total+ expect(total).not.toBeNull()+ expect(composerImageCountedBytes(total!, attachment)).toBe(3)+ })++ it('counts base64 characters for a base64 total', () => {+ const total = composerImageBudget('anthropic').total+ expect(total).not.toBeNull()+ expect(composerImageCountedBytes(total!, attachment)).toBe(5)+ })+})diff --git a/desktop-app/ui/src/constants/attachments.ts b/desktop-app/ui/src/constants/attachments.tsindex 6a9e57459..eae31c063 100644--- a/desktop-app/ui/src/constants/attachments.ts+++ b/desktop-app/ui/src/constants/attachments.ts@@ -28,30 +28,33 @@ export const CODEX_COMPOSER_MAX_IMAGE_DIMENSION = 2048* not the xAI limit; only tool screenshots can use the contract's 20 MiB.*/export const GROK_COMPOSER_MAX_IMAGE_BYTES = 16 * 1024 * 1024-/** The same 16 MiB ingress total, as the base64 bytes the composer counts. */-export const GROK_COMPOSER_MAX_TOTAL_IMAGE_BASE64_BYTES =- 4 * Math.ceil(GROK_COMPOSER_MAX_IMAGE_BYTES / 3)-export const COMPOSER_ACCEPT_IMAGE_MIME_TYPES = ['image/jpeg', 'image/png'] as constexport const CODEX_SUBSCRIPTION_PROVIDER = 'codex-subscription'export const GROK_SUBSCRIPTION_PROVIDER = 'grok-subscription'/**- * The composer aggregate one message must fit in. The general branch counts- * and names base64 MB (#669); Grok enforces the base64 encoding of the shared- * 16 MiB ingress total but names the decoded figure (#784).+ * The composer aggregate one message must fit in. The general branch sums and+ * names base64 MB (#669). Grok sums the decoded bytes of each image, which is+ * what the shared 16 MiB ingress total counts (#784).*/export type ComposerImageTotalBudget = {- /** Combined base64 bytes the composer counts and enforces. */- maxBase64Bytes: number- /**- * Bytes the total-limit copy names. For Grok this is the decoded ingress- * total, not the base64 bytes the picker counts.- */+ /** What the composer sums: base64 characters or decoded image bytes. */+ counts: 'base64' | 'decoded'+ /** Limit on that sum, in the unit named by `counts`. */+ maxBytes: number+ /** Bytes the total-limit copy names. */labelBytes: number}+/** Bytes one attachment adds to the sum the budget's `counts` names. */+export function composerImageCountedBytes(+ total: ComposerImageTotalBudget,+ attachment: { dataBase64: string; sizeBytes: number }+): number {+ return total.counts === 'base64' ? attachment.dataBase64.length : attachment.sizeBytes+}+export type ComposerImageBudget = {maxImageBytes: number/** `null` → no composer aggregate; the Codex hop owns that ceiling. */@@ -78,7 +81,8 @@ export function composerImageBudget(provider: string | null | undefined): Composreturn {maxImageBytes: GROK_COMPOSER_MAX_IMAGE_BYTES,total: {- maxBase64Bytes: GROK_COMPOSER_MAX_TOTAL_IMAGE_BASE64_BYTES,+ counts: 'decoded',+ maxBytes: GROK_COMPOSER_MAX_IMAGE_BYTES,labelBytes: GROK_COMPOSER_MAX_IMAGE_BYTES,},maxDimension: null,@@ -88,7 +92,8 @@ export function composerImageBudget(provider: string | null | undefined): Composreturn {maxImageBytes: COMPOSER_MAX_IMAGE_BYTES,total: {- maxBase64Bytes: COMPOSER_MAX_TOTAL_IMAGE_BASE64_BYTES,+ counts: 'base64',+ maxBytes: COMPOSER_MAX_TOTAL_IMAGE_BASE64_BYTES,labelBytes: COMPOSER_MAX_TOTAL_IMAGE_BASE64_BYTES,},maxDimension: null,