Skip to content

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

TypeScript

fix(desktop-ui): measure the Codex image budget the way the server does

Alfredo Lopez91ac80cff739
diff --git a/desktop-app/ui/src/components/agents/ComposerPanel.tsx b/desktop-app/ui/src/components/agents/ComposerPanel.tsx
index 964ab76e0..d11c55ad8 100644
--- a/desktop-app/ui/src/components/agents/ComposerPanel.tsx
+++ b/desktop-app/ui/src/components/agents/ComposerPanel.tsx
@@ -23,6 +23,7 @@ import {
COMPOSER_ACCEPT_IMAGE_MIME_TYPES,
COMPOSER_MAX_ATTACHMENTS,
composerImageBudget,
+ composerImageBudgetBytes,
} from '@constants/attachments'
import { useContextsDataController } from '@hooks/domain/useContextsDataController'
import { useMcpServersDataController } from '@hooks/domain/useMcpServersDataController'
@@ -576,10 +577,12 @@ export function ComposerPanel({ inline = false, agentSelector }: ComposerPanelPr
} not added.`
)
}
- // Non-Codex images travel inline in one 10 MB body (#669). Codex leaves
- // the aggregate to the hop (#650).
- let totalBase64Bytes = composerImageAttachments.reduce(
- (total, attachment) => total + attachment.dataBase64.length,
+ // Non-Codex images keep the #669 base64 ceiling; Codex images are held
+ // to the decoded image quota rpc-proxy and mcp-host enforce (#650).
+ const totalMeasure = imageBudget.maxTotal.measure
+ let totalImageBytes = composerImageAttachments.reduce(
+ (total, attachment) =>
+ total + composerImageBudgetBytes(attachment.dataBase64, totalMeasure),
0
)
@@ -617,19 +620,17 @@ export function ComposerPanel({ inline = false, agentSelector }: ComposerPanelPr
revokePreviewUrl(previewUrl)
continue
}
- if (
- imageBudget.maxTotalBase64Bytes != null &&
- totalBase64Bytes + dataBase64.length > imageBudget.maxTotalBase64Bytes
- ) {
+ const imageBytes = composerImageBudgetBytes(dataBase64, totalMeasure)
+ if (totalImageBytes + imageBytes > imageBudget.maxTotal.bytes) {
validationErrors.push(
`${file.name || 'Image'} does not fit in this message: the images in one message are limited to ${formatComposerMebibytes(
- imageBudget.maxTotalBase64Bytes
+ imageBudget.maxTotal.bytes
)} ${imageBudget.sizeUnit} in total. Send the attached images first or remove one.`
)
revokePreviewUrl(previewUrl)
continue
}
- totalBase64Bytes += dataBase64.length
+ totalImageBytes += imageBytes
accepted.push({
id: crypto.randomUUID(),
name: buildAttachmentName(file, source, mimeType, index),
@@ -708,14 +709,17 @@ export function ComposerPanel({ inline = false, agentSelector }: ComposerPanelPr
updated.dataBase64,
imageBudget.maxDimension
)
- const otherBase64Bytes = composerImageAttachments.reduce(
+ const totalMeasure = imageBudget.maxTotal.measure
+ const otherImageBytes = composerImageAttachments.reduce(
(total, attachment) =>
- attachment.id === updated.id ? total : total + attachment.dataBase64.length,
+ attachment.id === updated.id
+ ? total
+ : total + composerImageBudgetBytes(attachment.dataBase64, totalMeasure),
0
)
const combinedOverflow =
- imageBudget.maxTotalBase64Bytes != null &&
- otherBase64Bytes + updated.dataBase64.length > imageBudget.maxTotalBase64Bytes
+ otherImageBytes + composerImageBudgetBytes(updated.dataBase64, totalMeasure) >
+ imageBudget.maxTotal.bytes
if (
!composerImageExceedsPerImageBudget(updated.sizeBytes, imageBudget.maxImageBytes) &&
!dimensionError &&
@@ -738,7 +742,7 @@ export function ComposerPanel({ inline = false, agentSelector }: ComposerPanelPr
}
throw new Error(
`${updated.name || 'Image'} was kept unchanged. The images in one message are limited to ${formatComposerMebibytes(
- imageBudget.maxTotalBase64Bytes ?? 0
+ imageBudget.maxTotal.bytes
)} ${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.tsx
index 4b211fedb..e990b968e 100644
--- a/desktop-app/ui/src/components/agents/__tests__/ComposerPanel.test.tsx
+++ b/desktop-app/ui/src/components/agents/__tests__/ComposerPanel.test.tsx
@@ -423,6 +423,30 @@ function imageFileWithReportedSize(name: string, type: string, sizeBytes: number
return file
}
+const MIB = 1024 * 1024
+
+/** A PNG of exactly `sizeBytes` real bytes; `marker` makes its bytes unique. */
+function pngOfSize(name: string, sizeBytes: number, marker: number): File {
+ const bytes = new Uint8Array(sizeBytes)
+ bytes.set(PNG_BYTES)
+ bytes[PNG_BYTES.length] = marker
+ return imageFile(name, 'image/png', bytes)
+}
+
+/** An attached PNG whose canonical base64 decodes to exactly `decodedBytes` bytes. */
+function attachedImage(id: string, name: string, decodedBytes: number): ComposerImageAttachment {
+ const padding = (3 - (decodedBytes % 3)) % 3
+ const dataBase64 = `iVBORw0KGgo${'A'.repeat(Math.ceil(decodedBytes / 3) * 4 - padding - 11)}${'='.repeat(padding)}`
+ return {
+ id,
+ name,
+ mimeType: 'image/png',
+ dataBase64,
+ sizeBytes: decodedBytes,
+ previewDataUrl: 'data:image/png;base64,iVBORw0KGgo=',
+ }
+}
+
function pngIhdrFile(name: string, width: number, height: number): File {
const bytes = new Uint8Array(8 + 8 + 13)
bytes.set([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a], 0)
@@ -898,24 +922,88 @@ describe('ComposerPanel Codex image budgets', () => {
expect(actionsMock.handleAddComposerImageAttachments).not.toHaveBeenCalled()
})
- it('attaches 10MiB + 7MiB in the composer; the hop owns the aggregate', async () => {
+ it('refuses a second 8.5 MiB PNG: rpc-proxy and mcp-host cap the decoded images at 16 MiB', async () => {
+ const { container } = render(<ComposerPanel inline />)
+ const files = [pngOfSize('first.png', 8.5 * MIB, 1), pngOfSize('second.png', 8.5 * MIB, 2)]
+
+ fireEvent.change(pickerInput(container), { target: { files } })
+
+ await waitFor(() =>
+ expect(actionsMock.handleAddComposerImageAttachments).toHaveBeenCalledTimes(1)
+ )
+ // Witness: the first image fits on its own and is attached.
+ expect(addedBatches()[0]?.map(attachment => attachment.name)).toEqual(['first.png'])
+ 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('attaches two PNGs whose decoded bytes fill the 16 MiB image quota exactly', async () => {
+ const { container } = render(<ComposerPanel inline />)
+ const files = [pngOfSize('left.png', 8 * MIB, 1), pngOfSize('right.png', 8 * MIB, 2)]
+
+ fireEvent.change(pickerInput(container), { target: { files } })
+
+ await waitFor(() =>
+ expect(actionsMock.handleAddComposerImageAttachments).toHaveBeenCalledTimes(1)
+ )
+ const [batch] = addedBatches()
+ expect(batch?.map(attachment => attachment.name)).toEqual(['left.png', 'right.png'])
+ expect(batch?.map(attachment => attachment.sizeBytes)).toEqual([8 * MIB, 8 * MIB])
+ expect(screen.queryByRole('alert')).toBeNull()
+ })
+
+ it('counts the decoded bytes of images already in the composer toward the 16 MiB quota', async () => {
+ composerState.composerImageAttachments = [
+ attachedImage('already', 'already.png', 8.5 * MIB - 2),
+ ]
const { container } = render(<ComposerPanel inline />)
+
fireEvent.change(pickerInput(container), {
- target: {
- files: [
- imageFileWithReportedSize('fits.png', 'image/png', 10 * 1024 * 1024),
- imageFileWithReportedSize('over-total.png', 'image/png', 7 * 1024 * 1024),
- ],
- },
+ target: { files: [pngOfSize('next.png', 7.5 * MIB + 3, 1)] },
+ })
+
+ await waitFor(() =>
+ expect(screen.queryByRole('alert')?.textContent).toBe(
+ 'next.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.'
+ )
+ )
+ expect(actionsMock.handleAddComposerImageAttachments).not.toHaveBeenCalled()
+
+ // Twin: the same composer admits the image one byte smaller, which fills the quota.
+ cleanup()
+ composerState.composerImageAttachments = [
+ attachedImage('already', 'already.png', 8.5 * MIB - 2),
+ ]
+ const twin = render(<ComposerPanel inline />)
+ fireEvent.change(pickerInput(twin.container), {
+ target: { files: [pngOfSize('next.png', 7.5 * MIB + 2, 1)] },
})
await waitFor(() =>
expect(actionsMock.handleAddComposerImageAttachments).toHaveBeenCalledTimes(1)
)
- expect(addedBatches()[0]?.map(attachment => attachment.name)).toEqual([
- 'fits.png',
- 'over-total.png',
- ])
- expect(screen.queryByRole('alert')).toBeNull()
+ expect(expectSinglePreparedImage().name).toBe('next.png')
+ })
+
+ it('refuses an annotation save that would push the decoded images past 16 MiB', () => {
+ const already = attachedImage('already', 'already.png', 8 * MIB)
+ const editing = attachedImage('editing', 'edit.png', 8 * MIB - 3)
+ composerState.composerImageAttachments = [already, editing]
+ render(<ComposerPanel inline />)
+ fireEvent.click(screen.getByText('edit.png'))
+ expect(annotationCanvasMock.onSave).toEqual(expect.any(Function))
+
+ expect(() =>
+ annotationCanvasMock.onSave?.({ ...attachedImage('editing', 'edit.png', 8 * MIB + 1) })
+ ).toThrow(
+ 'edit.png was kept unchanged. The images in one message are limited to 16 MiB in total.'
+ )
+ expect(actionsMock.handleUpdateComposerImageAttachment).not.toHaveBeenCalled()
+
+ // Twin: a save that fills the quota exactly replaces the attachment.
+ const fits = attachedImage('editing', 'edit.png', 8 * MIB)
+ annotationCanvasMock.onSave?.(fits)
+ expect(actionsMock.handleUpdateComposerImageAttachment).toHaveBeenCalledWith(fits)
})
})
diff --git a/desktop-app/ui/src/constants/attachments.ts b/desktop-app/ui/src/constants/attachments.ts
index 6286ac64b..708e55c33 100644
--- a/desktop-app/ui/src/constants/attachments.ts
+++ b/desktop-app/ui/src/constants/attachments.ts
@@ -51,16 +51,25 @@ export const COMPOSER_FILE_ENTRY_METADATA_BYTES = 640
export const COMPOSER_MAX_IMAGE_BYTES = 3 * 1024 * 1024
/**
* Combined base64 size of the images in one non-Codex message. Images have
- * their own quota in rpc-proxy and mcp-host (up to the 24 MiB request body);
- * this is the composer's product ceiling for them.
+ * their own quota in rpc-proxy and mcp-host (16 MiB decoded, up to the 24 MiB
+ * request body); this is the composer's product ceiling for them and is
+ * stricter than that quota.
*/
export const COMPOSER_MAX_TOTAL_IMAGE_BASE64_BYTES = 8 * 1024 * 1024
/**
- * Codex-only (#650) per-image ceiling. The 16 MiB aggregate lives on the
- * Codex chat hop and the shared contract, not in this picker.
+ * Codex-only (#650) per-image ceiling. Must match `MAX_IMAGE_DECODED_BYTES` in
+ * `rpc-proxy/src/middleware/chatJsonBody.ts` and in `mcp-host/src/server.ts`.
*/
export const CODEX_COMPOSER_MAX_IMAGE_BYTES = 16 * 1024 * 1024
+/**
+ * Codex-only (#650) combined size of the images in one message, counted as
+ * their decoded bytes. rpc-proxy and mcp-host sum the decoded bytes of every
+ * image and refuse the request above this, so the composer refuses the image
+ * first. Must match `MAX_IMAGE_DECODED_BYTES_TOTAL` in
+ * `rpc-proxy/src/middleware/chatJsonBody.ts` and in `mcp-host/src/server.ts`.
+ */
+export const CODEX_COMPOSER_MAX_TOTAL_IMAGE_DECODED_BYTES = 16 * 1024 * 1024
/** Official Codex client long-side bound. The Codex hop 400s frames above this. */
export const CODEX_COMPOSER_MAX_IMAGE_DIMENSION = 2048
@@ -70,8 +79,11 @@ export const CODEX_SUBSCRIPTION_PROVIDER = 'codex-subscription'
export type ComposerImageBudget = {
maxImageBytes: number
- /** `null` → no composer aggregate; the Codex hop owns that ceiling. */
- maxTotalBase64Bytes: number | null
+ /**
+ * Combined size of the images in one message, measured either as the length
+ * of their base64 or as their decoded bytes.
+ */
+ maxTotal: { bytes: number; measure: 'base64' | 'decoded' }
/** `null` → no composer pixel bound (general models). */
maxDimension: number | null
sizeUnit: 'MB' | 'MiB'
@@ -82,15 +94,35 @@ export function composerImageBudget(provider: string | null | undefined): Compos
if (provider === CODEX_SUBSCRIPTION_PROVIDER) {
return {
maxImageBytes: CODEX_COMPOSER_MAX_IMAGE_BYTES,
- maxTotalBase64Bytes: null,
+ maxTotal: { bytes: CODEX_COMPOSER_MAX_TOTAL_IMAGE_DECODED_BYTES, measure: 'decoded' },
maxDimension: CODEX_COMPOSER_MAX_IMAGE_DIMENSION,
sizeUnit: 'MiB',
}
}
return {
maxImageBytes: COMPOSER_MAX_IMAGE_BYTES,
- maxTotalBase64Bytes: COMPOSER_MAX_TOTAL_IMAGE_BASE64_BYTES,
+ maxTotal: { bytes: COMPOSER_MAX_TOTAL_IMAGE_BASE64_BYTES, measure: 'base64' },
maxDimension: null,
sizeUnit: 'MB',
}
}
+
+/**
+ * Bytes one image's base64 counts against `ComposerImageBudget.maxTotal`. The
+ * decoded size is computed from the base64 as rpc-proxy and mcp-host compute
+ * it (`decodedBase64Bytes`): three bytes per four characters, minus one per
+ * `=` of padding. The composer only holds canonical base64 (FileReader and
+ * canvas output), so a length that is not a multiple of four is a defect and
+ * throws.
+ */
+export function composerImageBudgetBytes(
+ dataBase64: string,
+ measure: ComposerImageBudget['maxTotal']['measure']
+): number {
+ if (measure === 'base64') return dataBase64.length
+ if (dataBase64.length % 4 !== 0) {
+ throw new Error('Image data is not canonical base64.')
+ }
+ const padding = dataBase64.endsWith('==') ? 2 : dataBase64.endsWith('=') ? 1 : 0
+ return (dataBase64.length / 4) * 3 - padding
+}
diff --git a/desktop-app/ui/src/hooks/domain/__tests__/useAgentChatController.test.tsx b/desktop-app/ui/src/hooks/domain/__tests__/useAgentChatController.test.tsx
index 802ef1bb5..3604cdaac 100644
--- a/desktop-app/ui/src/hooks/domain/__tests__/useAgentChatController.test.tsx
+++ b/desktop-app/ui/src/hooks/domain/__tests__/useAgentChatController.test.tsx
@@ -2059,6 +2059,66 @@ describe('sendAgentMessage — per-message limits for documents (#678)', () => {
expect(sentAttachmentIds()).toEqual(expect.arrayContaining(['file-big', 'image-8mib']))
})
+ it('refuses Codex images and text that pass the 24 MiB body without any file, and sends the text that fills it', async () => {
+ clerum.rpc.invokeHostMessage.mockResolvedValue({ response: 'done' })
+ const rendered = renderController()
+ await settleMount()
+ const codexCatalog: HostModelsResult = {
+ provider: 'codex-subscription',
+ hostDefault: 'gpt-5.5',
+ // A broker-backed provider has no silent default: the session names its model.
+ sessionModel: 'gpt-5.5',
+ degraded: false,
+ modelSelectionRevision: 0,
+ models: [{ name: 'gpt-5.5', imageInput: { state: 'supported', reason: 'supported' } }],
+ }
+ await act(async () => {
+ await loadHostModels(
+ { getHostModels: vi.fn(async () => codexCatalog), setHostModel: vi.fn() },
+ 'agent-x',
+ null
+ )
+ })
+ // Four bytes under the 16 MiB decoded quota Codex admits, as canonical
+ // base64 without padding.
+ const decodedBytes = 16 * MIB - 4
+ const imageBase64Length = base64Length(decodedBytes)
+ expect(imageBase64Length % 4).toBe(0)
+ const image: ComposerImageAttachment = {
+ id: 'image-codex',
+ name: 'scan.png',
+ mimeType: 'image/png',
+ dataBase64: 'A'.repeat(imageBase64Length),
+ sizeBytes: decodedBytes,
+ previewDataUrl: 'data:image/png;base64,AAAA',
+ }
+ act(() => rendered.result.current.handleAddComposerImageAttachments([image]))
+ expect(rendered.result.current.composerFileAttachments).toHaveLength(0)
+ expect(rendered.result.current.composerImageAttachments).toHaveLength(1)
+ // 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
+ // image's JSON name and fixed fields + the image's base64.
+ const fullBodyText =
+ 24 * MIB - 4096 - 2048 - 2 - 18 - JSON.stringify('scan.png').length - 640 - imageBase64Length
+ // Precondition: the text share stays far inside its 6 MiB, so only the
+ // whole-body limit can refuse this send.
+ expect(fullBodyText + 1).toBeLessThan(3 * MIB)
+
+ await act(async () => {
+ await rendered.result.current.handleSendAgentMessage('x'.repeat(fullBodyText + 1))
+ })
+ expectBlocked(rendered, REQUEST_BODY_BLOCKER)
+ expect(rendered.result.current.composerImageAttachments).toHaveLength(1)
+
+ await act(async () => {
+ await rendered.result.current.handleSendAgentMessage('x'.repeat(fullBodyText))
+ })
+ expect(clerum.rpc.invokeHostMessage).toHaveBeenCalledTimes(1)
+ expect(sentAttachmentIds()).toEqual(['image-codex'])
+ const request = clerum.rpc.invokeHostMessage.mock.calls[0]?.[1] as { content: string }
+ expect(request.content).toHaveLength(fullBodyText)
+ })
+
it('refuses a twenty-first attachment and sends twenty', async () => {
const files = Array.from({ length: 21 }, (_, index) =>
readyTextFile(`file-${index}`, `part-${index}.txt`, 10)
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.tsx
index bc6ce656b..63b8a9381 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
@@ -3,7 +3,8 @@
* Issue #678 — the composer's document state machine: a picked file is
* `reading` at once, then `ready`; the user can remove it at any point, and a
* removed file never comes back. A refused or unreadable file leaves no chip,
- * only a notice that the next attach, a send or an agent change clears.
+ * only a notice that the next attach, removing a file, a send or an agent
+ * change clears.
*/
import { afterEach, describe, expect, it, vi } from 'vitest'
import { act, renderHook, waitFor } from '@testing-library/react'
@@ -187,6 +188,34 @@ describe('useComposerAttachments — documents (#678)', () => {
])
})
+ it('clears the refusal when the user removes a file chip to make room', async () => {
+ const { result } = render()
+ const files = Array.from({ length: COMPOSER_MAX_ATTACHMENTS + 1 }, (_, index) =>
+ textFile(`f${index}.txt`, `content ${index}`)
+ )
+ act(() => {
+ result.current.handleAddComposerFiles(files, '')
+ })
+ await waitFor(() =>
+ expect(statuses(result).filter(status => status === 'ready')).toHaveLength(
+ COMPOSER_MAX_ATTACHMENTS
+ )
+ )
+ // Witness: the refusal is on screen before the chip is removed.
+ expect(refusalTexts(result)).toEqual([
+ `A message can carry at most ${COMPOSER_MAX_ATTACHMENTS} attachments.`,
+ ])
+ const [first] = result.current.composerFileAttachments
+
+ act(() => {
+ result.current.handleRemoveComposerFileAttachment(first!.id)
+ })
+
+ expect(result.current.composerFileAttachments).toHaveLength(COMPOSER_MAX_ATTACHMENTS - 1)
+ expect(result.current.composerFileAttachments.map(file => file.id)).not.toContain(first!.id)
+ expect(refusalTexts(result)).toEqual([])
+ })
+
it('does not bring back a file removed while it was being read', async () => {
const { result } = render()
const file = textFile('gone.txt', 'gone')
@@ -356,6 +385,27 @@ describe('useComposerAttachments — documents (#678)', () => {
])
})
+ it('clears the refusal when the user removes an image chip to make room', () => {
+ const { result } = render()
+ act(() => {
+ result.current.handleAddComposerImageAttachments(
+ Array.from({ length: COMPOSER_MAX_ATTACHMENTS + 1 }, (_, index) => image(index))
+ )
+ })
+ // Witness: the twenty-first image was refused with a notice on screen.
+ expect(result.current.composerImageAttachments).toHaveLength(COMPOSER_MAX_ATTACHMENTS)
+ expect(refusalTexts(result)).toEqual([
+ `"${image(COMPOSER_MAX_ATTACHMENTS).name}" was not attached: a message can carry at most ${COMPOSER_MAX_ATTACHMENTS} attachments.`,
+ ])
+
+ act(() => {
+ result.current.handleRemoveComposerImageAttachment(image(0).id)
+ })
+
+ expect(result.current.composerImageAttachments).toHaveLength(COMPOSER_MAX_ATTACHMENTS - 1)
+ expect(refusalTexts(result)).toEqual([])
+ })
+
it('clears the documents when the selected agent changes', async () => {
const { result, rerender } = render('agent-x')
act(() => {
diff --git a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
index a29c2eb34..81dce8ffd 100644
--- a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
+++ b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
@@ -2868,8 +2868,9 @@ export function useAgentChatController({
// 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.
+ // remove. The share and the whole body apply to every send, so images
+ // alone that fill the body are refused here rather than by the proxy;
+ // the count and the file quota only when files are attached.
const requestForBudget = {
content: effectiveContentForRequest,
fileReferences: fileReferencesForSend,
@@ -2892,7 +2893,6 @@ export function useAgentChatController({
} 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)
diff --git a/desktop-app/ui/src/hooks/domain/useComposerAttachments.ts b/desktop-app/ui/src/hooks/domain/useComposerAttachments.ts
index 7888ee03e..55c5c401a 100644
--- a/desktop-app/ui/src/hooks/domain/useComposerAttachments.ts
+++ b/desktop-app/ui/src/hooks/domain/useComposerAttachments.ts
@@ -209,6 +209,9 @@ export function useComposerAttachments({
}
return previous.filter(att => att.id !== attachmentId)
})
+ // Images and files share one per-message count, so removing an image
+ // frees a slot and a refusal such as the attachment count is stale.
+ setComposerFileRefusals([])
},
[commitComposerImages, revokeComposerPreviewUrls]
)
@@ -320,6 +323,9 @@ export function useComposerAttachments({
(attachmentId: string) => {
composerAttachmentRevisionRef.current += 1
commitComposerFiles(previous => previous.filter(item => item.id !== attachmentId))
+ // Removing a file is how the user answers a refusal such as the
+ // attachment count or the file quota, so it must not stay on screen.
+ setComposerFileRefusals([])
clearSendError()
},
[clearSendError, commitComposerFiles]
diff --git a/tests/e2e/integration/chat-body-budget-parity.test.ts b/tests/e2e/integration/chat-body-budget-parity.test.ts
index 69cd57c15..ac32f1c77 100644
--- a/tests/e2e/integration/chat-body-budget-parity.test.ts
+++ b/tests/e2e/integration/chat-body-budget-parity.test.ts
@@ -25,6 +25,10 @@ const sources = {
rpcProxy: readFileSync(join(repoRoot, 'rpc-proxy/src/middleware/chatJsonBody.ts'), 'utf8'),
mcpHost: readFileSync(join(repoRoot, 'mcp-host/src/server.ts'), 'utf8'),
composer: readFileSync(join(repoRoot, 'desktop-app/ui/src/constants/attachments.ts'), 'utf8'),
+ composerAdmission: readFileSync(
+ join(repoRoot, 'desktop-app/ui/src/lib/composerFileAdmission.ts'),
+ 'utf8'
+ ),
hostConfig: readFileSync(join(repoRoot, 'mcp-host/src/config.ts'), 'utf8'),
hostAdmission: readFileSync(join(repoRoot, 'mcp-host/src/agent/incomingAttachments.ts'), 'utf8'),
rpcProxyForwardingTest: readFileSync(
@@ -53,11 +57,14 @@ function constantValue(source: string, name: string, label: string): number {
.reduce((product, factor) => product * factor, 1)
}
-/** Source text of a top-level `function name(...) { ... }`, closed by a column-0 `}`. */
+/**
+ * Source text of a top-level `function name(...) { ... }` (exported or not),
+ * closed by a column-0 `}`.
+ */
function functionSource(source: string, name: string, label: string): string {
return soleMatch(
source,
- new RegExp(`^function ${name}\\([\\s\\S]*?^\\}$`, 'm'),
+ new RegExp(`^(?:export )?function ${name}\\([\\s\\S]*?^\\}$`, 'm'),
`${label} ${name}`
)[0]
}
@@ -159,6 +166,14 @@ describe('chat body budget parity across rpc-proxy, mcp-host and the composer (#
'COMPOSER_MAX_TOTAL_FILE_BASE64_BYTES',
constantValue(sources.rpcProxy, 'MAX_FILE_BASE64_BYTES_TOTAL', 'rpc-proxy'),
],
+ [
+ 'CODEX_COMPOSER_MAX_IMAGE_BYTES',
+ constantValue(sources.rpcProxy, 'MAX_IMAGE_DECODED_BYTES', 'rpc-proxy'),
+ ],
+ [
+ 'CODEX_COMPOSER_MAX_TOTAL_IMAGE_DECODED_BYTES',
+ constantValue(sources.rpcProxy, 'MAX_IMAGE_DECODED_BYTES_TOTAL', 'rpc-proxy'),
+ ],
// The composer counts images and files together, as the Host admission does;
// MAX_CHAT_FILES bounds only the credited files.
[
@@ -171,6 +186,116 @@ describe('chat body budget parity across rpc-proxy, mcp-host and the composer (#
}
})
+ it('the composer counts the Codex image quota in decoded bytes, as both parsers do', () => {
+ const budget = functionSource(sources.composer, 'composerImageBudget', 'composer')
+ expect(soleLine(budget, 'maxTotal: { bytes: CODEX_', 'composer Codex image total')).toBe(
+ "maxTotal: { bytes: CODEX_COMPOSER_MAX_TOTAL_IMAGE_DECODED_BYTES, measure: 'decoded' },"
+ )
+ const composerDecoded = functionSource(sources.composer, 'composerImageBudgetBytes', 'composer')
+ const serverDecoded = functionSource(sources.rpcProxy, 'decodedBase64Bytes', 'rpc-proxy')
+ // The decoded size is the same arithmetic on the same base64 on both sides.
+ for (const prefix of ['const padding = ', 'return (dataBase64.length / 4) * 3']) {
+ expect(soleLine(composerDecoded, prefix, `composer ${prefix}`)).toBe(
+ soleLine(serverDecoded, prefix, `rpc-proxy ${prefix}`)
+ )
+ }
+ // Both refuse only above the quota: a set that fills it exactly is admitted.
+ const inspect = functionSource(sources.rpcProxy, 'inspectChatImageBudget', 'rpc-proxy')
+ expect(soleLine(inspect, 'if (counted >=', 'image quota condition')).toBe(
+ 'if (counted >= MAX_CHAT_IMAGES || decodedTotal + decoded > MAX_IMAGE_DECODED_BYTES_TOTAL) {'
+ )
+ })
+
+ it('the composer estimates the non-image share and the file quota from these constants', () => {
+ // The estimate constants have no server counterpart: they bound the JSON
+ // around the text and the attachments. Pin them and every place the
+ // estimate uses them, so a changed literal or a dropped term fails here.
+ expect(constantValue(sources.composer, 'COMPOSER_REQUEST_ENVELOPE_BYTES', 'composer')).toBe(
+ 4096
+ )
+ expect(constantValue(sources.composer, 'COMPOSER_FILE_ENTRY_METADATA_BYTES', 'composer')).toBe(
+ 640
+ )
+ const admission = sources.composerAdmission
+ const lines: ReadonlyArray<readonly [string, string, string]> = [
+ ['jsonBytes', 'return ', 'return textEncoder.encode(JSON.stringify(value)).length'],
+ ['base64Length', 'return ', 'return Math.ceil(sizeBytes / 3) * 4'],
+ [
+ 'composerFileDetailBytes',
+ 'return ',
+ 'return jsonBytes(file.filename) + COMPOSER_FILE_ENTRY_METADATA_BYTES',
+ ],
+ [
+ 'composerFileBase64Bytes',
+ 'return ',
+ 'return files.reduce((total, file) => total + base64Length(file.sizeBytes), 0)',
+ ],
+ [
+ 'composerNonImageShareBytes',
+ 'COMPOSER_REQUEST_ENVELOPE_BYTES',
+ 'COMPOSER_REQUEST_ENVELOPE_BYTES +',
+ ],
+ [
+ 'composerNonImageShareBytes',
+ 'COMPOSER_FORWARDED_FIELDS_BYTES',
+ 'COMPOSER_FORWARDED_FIELDS_BYTES +',
+ ],
+ ['composerNonImageShareBytes', 'jsonBytes(request.content)', 'jsonBytes(request.content) +'],
+ [
+ 'composerNonImageShareBytes',
+ '(request.fileReferences.length',
+ '(request.fileReferences.length ? jsonBytes(request.fileReferences) : 0) +',
+ ],
+ [
+ 'composerNonImageShareBytes',
+ '2 * jsonBytes(request.hostRef)',
+ '2 * jsonBytes(request.hostRef) +',
+ ],
+ [
+ 'composerNonImageShareBytes',
+ 'request.files.reduce(',
+ 'request.files.reduce((total, file) => total + composerFileDetailBytes(file), 0) +',
+ ],
+ [
+ 'composerNonImageShareBytes',
+ '(total, image) =>',
+ '(total, image) => total + jsonBytes(image.name) + COMPOSER_FILE_ENTRY_METADATA_BYTES,',
+ ],
+ [
+ 'composerFileAdmissionError',
+ 'if (file.size >',
+ 'if (file.size > COMPOSER_MAX_FILE_BYTES) {',
+ ],
+ [
+ 'composerFileAdmissionError',
+ 'if (context.attachedCount',
+ 'if (context.attachedCount >= COMPOSER_MAX_ATTACHMENTS) {',
+ ],
+ [
+ 'composerFileAdmissionError',
+ 'if (composerFileBase64Bytes(',
+ 'if (composerFileBase64Bytes(files) > COMPOSER_MAX_TOTAL_FILE_BASE64_BYTES) {',
+ ],
+ [
+ 'composerFileAdmissionError',
+ 'const bodyBytes =',
+ 'const bodyBytes = composerNonImageShareBytes({ ...context.request, files })',
+ ],
+ [
+ 'composerFileAdmissionError',
+ 'if (bodyBytes >',
+ 'if (bodyBytes > COMPOSER_MAX_NON_IMAGE_BODY_BYTES) {',
+ ],
+ ]
+ for (const [name, prefix, expected] of lines) {
+ const body = functionSource(admission, name, 'composer admission')
+ expect(soleLine(body, prefix, `composer admission ${name} ${prefix}`)).toBe(expected)
+ }
+ // The non-image share is the sum of exactly the terms pinned above.
+ const share = functionSource(admission, 'composerNonImageShareBytes', 'composer admission')
+ expect(share.match(/ \+$/gm), 'composerNonImageShareBytes term count').toHaveLength(6)
+ })
+
it('the composer reserves the headroom rpc-proxy is tested to add when forwarding', () => {
expect(constantValue(sources.composer, 'COMPOSER_FORWARDED_FIELDS_BYTES', 'composer')).toBe(
constantValue(
diff --git a/tests/e2e/package-lock.json b/tests/e2e/package-lock.json
index 882564dd9..da7288544 100644
--- a/tests/e2e/package-lock.json
+++ b/tests/e2e/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-e2e-tests",
- "version": "0.1.100",
+ "version": "0.1.101",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-e2e-tests",
- "version": "0.1.100",
+ "version": "0.1.101",
"devDependencies": {
"@clerum/llm-provider-attempt-contract": "file:../../packages/llm-provider-attempt-contract",
"@types/jsonwebtoken": "^9.0.10",
diff --git a/tests/e2e/package.json b/tests/e2e/package.json
index 77b8a7ffd..0c7ad7fa9 100644
--- a/tests/e2e/package.json
+++ b/tests/e2e/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-e2e-tests",
- "version": "0.1.100",
+ "version": "0.1.101",
"private": true,
"type": "module",
"scripts": {