Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
chore: merge origin/dev after models.dev capabilities
Alfredo Lopezbbbb10a001d0
diff --cc desktop-app/src/__tests__/appService.rpc.test.tsindex 41545c520,4ce47ec1b..005a0b0e3--- a/desktop-app/src/__tests__/appService.rpc.test.ts+++ b/desktop-app/src/__tests__/appService.rpc.test.ts@@@ -103,114 -79,42 +108,150 @@@ describe('AppService.invokeHostMessage'expect(service.rpcClient.invokeHostMessage.mock.calls[0][3]).toEqual({ async: true })})+ it('forwards a 5 MiB JPEG without letting the payload choose identity, host, or scopes', async () => {+ const service = new AppService() as any+ service.sessionToken = 'session-token'+ service.me = {+ id: '00000000-0000-4000-8000-000000000001',+ email: '[email protected]',+ name: 'Test User',+ picture: null,+ teamId: '00000000-0000-4000-8000-0000000000aa',+ teamName: 'Test Team',+ role: 'member',+ }+ service.rpcTokenManager = {+ getOrIssue: vi.fn().mockResolvedValue({ token: 'rpc-test-token' }),+ clear: vi.fn(),+ }+ service.rpcClient = {+ invokeHostMessage: vi.fn().mockResolvedValue({ success: true, response: 'ok' }),+ }+ const attachment = imageAttachment('photo', 5 * MIB, 'image/jpeg')++ await service.invokeHostMessage(+ 'chatllm',+ {+ content: 'look',+ channelType: 'slack',+ channelId: 'attacker-channel',+ hostRef: 'attacker-host',+ sender: 'attacker-controlled-user',+ metadata: { teamId: 'attacker-controlled-team' },+ attachments: [attachment],+ },+ ['chatllm']+ )++ expect(service.rpcTokenManager.getOrIssue).toHaveBeenCalledWith(+ 'session-token',+ [...MESSAGE_SCOPES],+ ['chatllm']+ )+ expect(service.rpcClient.invokeHostMessage).toHaveBeenCalledTimes(1)+ expect(service.rpcClient.invokeHostMessage.mock.calls[0][0]).toBe('rpc-test-token')+ expect(service.rpcClient.invokeHostMessage.mock.calls[0][1]).toBe('chatllm')+ const forwarded = service.rpcClient.invokeHostMessage.mock.calls[0][2]+ expect(forwarded).toEqual({+ content: 'look',+ channelType: 'rpc',+ channelId: 'chatllm',+ hostRef: 'chatllm',+ sender: '00000000-0000-4000-8000-000000000001',+ metadata: { teamId: '00000000-0000-4000-8000-0000000000aa' },+ threadId: undefined,+ attachments: [attachment],+ })+ expect(Buffer.from(forwarded.attachments[0].dataBase64, 'base64')).toHaveLength(5 * MIB)+ })++ it('forwards a 10 MiB PNG + 5 MiB JPEG on the same authenticated envelope', async () => {+ const service = new AppService() as any+ service.sessionToken = 'session-token'+ service.me = {+ id: '00000000-0000-4000-8000-000000000001',+ email: '[email protected]',+ name: 'Test User',+ picture: null,+ teamId: '00000000-0000-4000-8000-0000000000aa',+ teamName: 'Test Team',+ role: 'member',+ }+ service.rpcTokenManager = {+ getOrIssue: vi.fn().mockResolvedValue({ token: 'rpc-test-token' }),+ clear: vi.fn(),+ }+ service.rpcClient = {+ invokeHostMessage: vi.fn().mockResolvedValue({ success: true, response: 'ok' }),+ }+ const attachments = [+ imageAttachment('ten', 10 * MIB),+ imageAttachment('five', 5 * MIB, 'image/jpeg'),+ ]++ await service.invokeHostMessage(+ 'chatllm',+ {+ content: 'look',+ channelType: 'slack',+ hostRef: 'attacker-host',+ sender: 'attacker-controlled-user',+ attachments,+ },+ ['chatllm']+ )++ expect(service.rpcTokenManager.getOrIssue).toHaveBeenCalledWith(+ 'session-token',+ [...MESSAGE_SCOPES],+ ['chatllm']+ )+ const forwarded = service.rpcClient.invokeHostMessage.mock.calls[0][2]+ expect(forwarded.channelType).toBe('rpc')+ expect(forwarded.sender).toBe('00000000-0000-4000-8000-000000000001')+ expect(forwarded.hostRef).toBe('chatllm')+ expect(forwarded.channelId).toBe('chatllm')+ expect(forwarded.attachments).toEqual(attachments)+ expect(Buffer.from(forwarded.attachments[0].dataBase64, 'base64')).toHaveLength(10 * MIB)+ expect(Buffer.from(forwarded.attachments[1].dataBase64, 'base64')).toHaveLength(5 * MIB)+ })++ it('rejects malformed attachments before issuing an RPC token', async () => {+ const service = new AppService() as any+ service.sessionToken = 'test-token-session'+ service.me = {+ id: '00000000-0000-4000-8000-000000000001',+ email: '[email protected]',+ name: 'Test User',+ picture: null,+ teamId: '00000000-0000-4000-8000-0000000000aa',+ teamName: 'Test Team',+ role: 'member',+ }+ service.rpcTokenManager = {+ getOrIssue: vi.fn().mockResolvedValue({ token: 'test-token-rpc' }),+ clear: vi.fn(),+ }+ service.rpcClient = {+ invokeHostMessage: vi.fn().mockResolvedValue({ success: true, response: 'ok' }),+ }+ const issueToken = vi.spyOn(service, 'issueRpcTokenForHostRefs')++ await expect(+ service.invokeHostMessage('chatllm', { content: 'hello', attachments: {} }, ['chatllm'])+ ).rejects.toThrow('Image attachments must be a list.')+ expect(issueToken).not.toHaveBeenCalled()+ expect(service.rpcTokenManager.getOrIssue).not.toHaveBeenCalled()+ expect(service.rpcClient.invokeHostMessage).not.toHaveBeenCalled()++ // Positive control: the same call with a well-formed list issues the token+ // and reaches the Host, so the checks above are not vacuous.+ await service.invokeHostMessage('chatllm', { content: 'hello', attachments: [] }, ['chatllm'])+ expect(issueToken).toHaveBeenCalledTimes(1)+ expect(service.rpcTokenManager.getOrIssue).toHaveBeenCalled()+ expect(service.rpcClient.invokeHostMessage).toHaveBeenCalledTimes(1)+ })+it('switches to a matching directory team before issuing RPC tokens for teamless sessions', async () => {const service = new AppService() as anyservice.desktopGfsUploadStatePath = vidiff --cc desktop-app/ui/src/components/agents/ComposerPanel.tsxindex ddb03a2f7,25d915df9..a8219f632--- a/desktop-app/ui/src/components/agents/ComposerPanel.tsx+++ b/desktop-app/ui/src/components/agents/ComposerPanel.tsx@@@ -22,14 -22,13 +22,14 @@@ importCOMPOSER_ACCEPT_IMAGE_MIME_TYPES,COMPOSER_MAX_IMAGE_ATTACHMENTS,COMPOSER_MAX_IMAGE_BYTES,- COMPOSER_MAX_TOTAL_IMAGE_BASE64_BYTES,+ COMPOSER_MAX_IMAGE_DIMENSION,- ZAI_IMAGE_ATTACHMENT_UNSUPPORTED_MESSAGE,} from '@constants/attachments'import { useContextsDataController } from '@hooks/domain/useContextsDataController'import { useMcpServersDataController } from '@hooks/domain/useMcpServersDataController'import { useClickOutside } from '@hooks/useClickOutside'import { useComposerDraft } from '@hooks/useComposerDraft'+ import { useHostModels } from '@hooks/useHostModels'+import { readImageHeaderDimensions } from '@lib/imageHeaderDimensions'import type { WorkflowRecipeListResult } from '../../../../src/types'import type { ComposerImageAttachment, ComposerReferenceAttachment } from '../../uiTypes'import { AnnotationCanvas } from './AnnotationCanvas'@@@ -475,24 -466,28 +506,22 @@@ export function ComposerPanel({ inline}const accepted: ComposerImageAttachment[] = []- const validationErrors: string[] = []- const selected = candidates.slice(0, availableSlots)- if (candidates.length > selected.length) {+ const selected = imageCandidates.slice(0, availableSlots)+ if (imageCandidates.length > availableSlots) {+ const skipped = imageCandidates.length - availableSlotsvalidationErrors.push(- `You can attach up to ${COMPOSER_MAX_IMAGE_ATTACHMENTS} images per message.`+ `You can attach up to ${COMPOSER_MAX_IMAGE_ATTACHMENTS} images per message; ${skipped} ${+ skipped === 1 ? 'image was' : 'images were'+ } not added.`)}- for (const [index, file] of selected.entries()) {- const mimeType = inferComposerImageMimeType(file)- if (!mimeType || !COMPOSER_ACCEPT_IMAGE_MIME_TYPES.includes(mimeType)) {- validationErrors.push(`${file.name || 'Image'} is not supported. Use PNG or JPEG.`)- continue- }- // The images travel inline in one request body; an image that would push- // the message past the combined budget is refused here, not at send time.- let totalBase64Bytes = composerImageAttachments.reduce(- (total, attachment) => total + attachment.dataBase64.length,- 0- )++ for (const [index, { file, mimeType }] of selected.entries()) {- if (file.size > COMPOSER_MAX_IMAGE_BYTES) {+ if (composerImageExceedsPerImageBudget(file.size)) {validationErrors.push(- `${file.name || 'Image'} is too large. Max size is ${Math.round(- COMPOSER_MAX_IMAGE_BYTES / (1024 * 1024)- )} MB.`+ `${file.name || 'Image'} is too large. Max size is ${formatComposerMebibytes(+ COMPOSER_MAX_IMAGE_BYTES+ )} MiB.`)continue}diff --cc desktop-app/ui/src/components/agents/__tests__/ComposerPanel.test.tsxindex f1ee07497,f056c2d79..8e4bad9b3--- a/desktop-app/ui/src/components/agents/__tests__/ComposerPanel.test.tsx+++ b/desktop-app/ui/src/components/agents/__tests__/ComposerPanel.test.tsx@@@ -3,7 -4,17 +4,17 @@@ import type { ChatComposerStateContextVimport { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'import { readFileSync } from 'node:fs'import { resolve } from 'node:path'- import type { ComposerImageAttachment } from '../../../uiTypes'+ import {+ COMPOSER_MAX_IMAGE_ATTACHMENTS,+ COMPOSER_MAX_IMAGE_BYTES,- COMPOSER_MAX_TOTAL_IMAGE_BASE64_BYTES,++ COMPOSER_MAX_IMAGE_DIMENSION,+ } from '@constants/attachments'+ import type { HostModelsResult } from '@hooks/useChatStore'+ import {+ type ImageInputDecision,+ imageInputBlockMessage,+ } from '../../../../../src/imageInputDecision'+ import type { ComposerImageAttachment, FailedAgentSend } from '../../../uiTypes'import { ComposerPanel } from '../ComposerPanel'// Resolve the stylesheet relative to THIS test file (not process.cwd()) so the@@@ -241,162 -246,379 +246,389 @@@ describe.each(})})- const MIB = 1024 * 1024+ // ---------------------------------------------------------------------------+ // Issue #654: composer image attachments through real DOM events.+ //+ // These drive the real component. jsdom has no file dialog and no DataTransfer,+ // so the helpers below stand in for exactly those unavailable browser+ // primitives; the accept filter, the FileReader byte preparation and every guard+ // stay real. The jsdom environment does provide URL.createObjectURL, so chip+ // previews are real `blob:` object URLs.+ // ---------------------------------------------------------------------------++ /** PNG signature bytes, so "the file's bytes reached the controller" is literal. */+ const PNG_BYTES = [137, 80, 78, 71, 13, 10, 26, 10, 0, 1, 2, 3]+ /** JPEG SOI/APP0 bytes for the cases that carry a .jpg file. */+ const JPEG_BYTES = [255, 216, 255, 224, 0, 16, 74, 70, 73, 70]++ function imageFile(name: string, type: string, bytes: Uint8Array | number[] = PNG_BYTES): File {+ return new File([new Uint8Array(bytes)], name, { type })+ }+++/** Reported `File.size` without allocating the payload (the picker guards on size). */++function imageFileWithReportedSize(name: string, type: string, sizeBytes: number): File {++ const file = imageFile(name, type)++ Object.defineProperty(file, 'size', { configurable: true, value: sizeBytes })++ return file++}+- /**- * A picked image whose reported size is `sizeBytes`. The guard decides on- * `File.size` — what the browser reports for the chosen file — so binding the- * size drives the real code path without allocating and base64-encoding 10MiB- * blobs per case.- */+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)+ const view = new DataView(bytes.buffer)+ view.setUint32(8, 13)+ bytes.set([0x49, 0x48, 0x44, 0x52], 12)+ view.setUint32(16, width)+ view.setUint32(20, height)+ return new File([bytes], name, { type: 'image/png' })+}+- function imageFile(name: string, sizeBytes: number, type = 'image/png'): File {- const file = new File([new Uint8Array([0x41])], name, { type })- Object.defineProperty(file, 'size', { configurable: true, value: sizeBytes })- return file+ function decodedBytes(base64: string): number[] {+ return Array.from(atob(base64), char => char.charCodeAt(0))}- function existingAttachment(name: string, sizeBytes: number): ComposerImageAttachment {- return {- id: `existing-${name}`,- name,- mimeType: 'image/png',- dataBase64: 'QQ==',- sizeBytes,- previewDataUrl: '',- addedOrder: 0,- }+ function addedBatches(): ComposerImageAttachment[][] {+ return actionsMock.handleAddComposerImageAttachments.mock.calls.map(([batch]) => batch)}- function pickFiles(container: HTMLElement, files: File[]) {- const input = container.querySelector('input[type="file"]') as HTMLInputElement- Object.defineProperty(input, 'files', { configurable: true, value: files })- fireEvent.change(input)+ /** Asserts that the controller received exactly one batch with one prepared image. */+ function expectSinglePreparedImage(): ComposerImageAttachment {+ const batches = addedBatches()+ expect(batches).toHaveLength(1)+ expect(batches[0]).toHaveLength(1)+ const attachment = batches[0]?.[0]+ if (!attachment) throw new Error('expected one prepared image attachment')+ return attachment}- /** Every image the panel handed to the composer-actions layer, in order. */- function attachedImages(): ComposerImageAttachment[] {- return composerActions.handleAddComposerImageAttachments.mock.calls.flatMap(- call => call[0] as ComposerImageAttachment[]- )+ function pickerInput(container: HTMLElement): HTMLInputElement {+ const input = container.querySelector('.composer-file-input')+ if (!(input instanceof HTMLInputElement)) throw new Error('expected the composer file picker')+ return input}- describe('ComposerPanel image budget', () => {- it('accepts an image at the 10MiB per-image limit', async () => {- const { container } = render(<ComposerPanel inline={false} />)- pickFiles(container, [imageFile('at-limit.png', 10 * MIB)])- await waitFor(() => expect(attachedImages()).toHaveLength(1))- expect(attachedImages()[0]?.sizeBytes).toBe(10 * MIB)- expect(screen.queryByRole('alert')).toBeNull()+ function capabilityMessage(decision: ImageInputDecision): string {+ const message = imageInputBlockMessage(composerModelState.effectiveModel, decision)+ if (!message) throw new Error('expected a blocking image-capability message')+ return message+ }++ /**+ * jsdom cannot open a native file dialog, so the picker click is the boundary+ * that stays observable: intercept it, and let the test fire the `change` event+ * that a real dialog would produce.+ */+ async function withPickerClick<T>(run: (timesClicked: () => number) => Promise<T>): Promise<T> {+ const clickSpy = vi.spyOn(HTMLInputElement.prototype, 'click').mockImplementation(() => {})+ try {+ return await run(() => clickSpy.mock.calls.length)+ } finally {+ clickSpy.mockRestore()+ }+ }++ const SUPPORTED_CAPABILITY = {+ imageInput: { state: 'supported', reason: 'supported' } as ImageInputDecision,+ canAttachImages: true,+ imageBlockMessage: null,+ visualSendBlocked: false,+ }++ const BLOCKED_CAPABILITIES: Array<[string, ImageInputDecision]> = [+ ['unsupported', { state: 'unsupported', reason: 'model_unsupported' }],+ ['unverified', { state: 'unknown', reason: 'model_unknown' }],+ ]++ describe.each(BLOCKED_CAPABILITIES)(+ 'ComposerPanel with %s image capability',+ (_label, decision) => {+ const blockMessage = capabilityMessage(decision)++ beforeEach(() => {+ Object.assign(composerModelState, {+ imageInput: decision,+ canAttachImages: false,+ imageBlockMessage: blockMessage,+ visualSendBlocked: true,+ })+ })++ // #678: capability gates sending images, never selecting them. The picked+ // image is kept so the user can switch to a capable model; the send-time+ // notice (covered below) explains the block once the chip is pending.+ it('opens the Upload Files picker and attaches the picked image', async () => {+ await withPickerClick(async timesClicked => {+ const { container } = render(<ComposerPanel inline />)++ fireEvent.click(screen.getByRole('button', { name: 'Add context' }))+ fireEvent.click(screen.getByRole('menuitem', { name: 'Upload Files' }))+ expect(timesClicked()).toBe(1)++ fireEvent.change(pickerInput(container), {+ target: { files: [imageFile('photo.png', 'image/png')] },+ })++ await waitFor(() =>+ expect(actionsMock.handleAddComposerImageAttachments).toHaveBeenCalledTimes(1)+ )+ expect(expectSinglePreparedImage()).toMatchObject({ name: 'photo.png' })+ expect(screen.queryByText(blockMessage)).toBeNull()+ })+ })++ it('attaches a pasted image', async () => {+ render(<ComposerPanel inline />)+ const textarea = screen.getByTestId('chat-input')++ const notPrevented = fireEvent.paste(textarea, {+ clipboardData: { files: [imageFile('clipboard.png', 'image/png')], items: [] },+ })++ // Consumed like a real paste so the image never lands as text either.+ expect(notPrevented).toBe(false)+ await waitFor(() =>+ expect(actionsMock.handleAddComposerImageAttachments).toHaveBeenCalledTimes(1)+ )+ expect(expectSinglePreparedImage().mimeType).toBe('image/png')+ expect(screen.queryByText(blockMessage)).toBeNull()+ })++ it('attaches a dropped image and clears the drop overlay', async () => {+ const { container } = render(<ComposerPanel inline />)+ const shell = container.querySelector('.composer-input-shell') as HTMLElement++ fireEvent.dragEnter(shell, { dataTransfer: { types: ['Files'] } })+ expect(screen.getByText('Drop files here')).toBeTruthy()++ const notPrevented = fireEvent.drop(shell, {+ dataTransfer: { files: [imageFile('dropped.png', 'image/png')], types: ['Files'] },+ })++ expect(notPrevented).toBe(false)+ await waitFor(() =>+ expect(actionsMock.handleAddComposerImageAttachments).toHaveBeenCalledTimes(1)+ )+ expect(expectSinglePreparedImage()).toMatchObject({ name: 'dropped.png' })+ expect(screen.queryByText('Drop files here')).toBeNull()+ expect(screen.queryByText(blockMessage)).toBeNull()+ })+ }+ )++ describe('ComposerPanel with an image-capable model', () => {+ beforeEach(() => {+ Object.assign(composerModelState, SUPPORTED_CAPABILITY)})- it('accepts 10MiB + 5MiB, the 15MiB per-message total', async () => {- const { container } = render(<ComposerPanel inline={false} />)- pickFiles(container, [imageFile('ten.png', 10 * MIB), imageFile('five.png', 5 * MIB)])- await waitFor(() => expect(attachedImages()).toHaveLength(2))- expect(screen.queryByRole('alert')).toBeNull()+ it('opens the picker and hands the picked PNG bytes to the controller', async () => {+ await withPickerClick(async timesClicked => {+ const { container } = render(<ComposerPanel inline />)++ fireEvent.click(screen.getByRole('button', { name: 'Add context' }))+ fireEvent.click(screen.getByRole('menuitem', { name: 'Upload Files' }))+ expect(timesClicked()).toBe(1)++ const fileInput = pickerInput(container)+ expect(fileInput.accept).toBe('image/jpeg,image/png')+ expect(fileInput.multiple).toBe(true)+ fireEvent.change(fileInput, { target: { files: [imageFile('photo.png', 'image/png')] } })++ await waitFor(() => expect(actionsMock.handleAddComposerImageAttachments).toHaveBeenCalled())+ const attachment = expectSinglePreparedImage()+ expect(attachment).toMatchObject({+ name: 'photo.png',+ mimeType: 'image/png',+ sizeBytes: PNG_BYTES.length,+ })+ expect(decodedBytes(attachment.dataBase64)).toEqual(PNG_BYTES)+ // createObjectURL is available here, so the chip preview is an object URL+ // for the picked file rather than the FileReader fallback.+ expect(attachment.previewDataUrl).toMatch(/^blob:/)+ expect(screen.queryByRole('alert')).toBeNull()+ })})- it('accepts three 5MiB images', async () => {- const { container } = render(<ComposerPanel inline={false} />)- pickFiles(container, [- imageFile('a.png', 5 * MIB),- imageFile('b.png', 5 * MIB),- imageFile('c.png', 5 * MIB),- ])- await waitFor(() => expect(attachedImages()).toHaveLength(3))- expect(screen.queryByRole('alert')).toBeNull()+ it('prepares a pasted PNG once when the clipboard reports it as both file and item', async () => {+ render(<ComposerPanel inline />)+ const textarea = screen.getByTestId('chat-input')+ const pasted = imageFile('image.png', 'image/png')++ fireEvent.paste(textarea, {+ clipboardData: {+ files: [pasted],+ items: [{ kind: 'file', getAsFile: () => pasted }],+ },+ })++ await waitFor(() => expect(actionsMock.handleAddComposerImageAttachments).toHaveBeenCalled())+ const attachment = expectSinglePreparedImage()+ // A generic clipboard name is replaced with a distinguishable pasted-image name.+ expect(attachment.name).toMatch(/^pasted-image-\d+-1\.png$/)+ expect(attachment.mimeType).toBe('image/png')+ expect(decodedBytes(attachment.dataBase64)).toEqual(PNG_BYTES)})- it('accepts a 5MiB JPEG at the same budget as PNG', async () => {- const { container } = render(<ComposerPanel inline={false} />)- pickFiles(container, [imageFile('photo.jpg', 5 * MIB, 'image/jpeg')])- await waitFor(() => expect(attachedImages()).toHaveLength(1))- expect(attachedImages()[0]?.mimeType).toBe('image/jpeg')- expect(attachedImages()[0]?.sizeBytes).toBe(5 * MIB)- expect(screen.queryByRole('alert')).toBeNull()+ it('prepares a dropped JPEG and clears the drop overlay after the drop', async () => {+ const { container } = render(<ComposerPanel inline />)+ const shell = container.querySelector('.composer-input-shell') as HTMLElement++ fireEvent.dragEnter(shell, { dataTransfer: { types: ['Files'] } })+ expect(screen.getByText('Drop files here')).toBeTruthy()++ fireEvent.drop(shell, {+ dataTransfer: {+ files: [imageFile('dropped.jpg', 'image/jpeg', JPEG_BYTES)],+ types: ['Files'],+ },+ })++ await waitFor(() => expect(actionsMock.handleAddComposerImageAttachments).toHaveBeenCalled())+ const attachment = expectSinglePreparedImage()+ expect(attachment).toMatchObject({+ name: 'dropped.jpg',+ mimeType: 'image/jpeg',+ sizeBytes: JPEG_BYTES.length,+ })+ expect(decodedBytes(attachment.dataBase64)).toEqual(JPEG_BYTES)+ expect(screen.queryByText('Drop files here')).toBeNull()})- it('accepts a 12MiB image above the usual 10MiB target', async () => {- const { container } = render(<ComposerPanel inline={false} />)- pickFiles(container, [imageFile('large.png', 12 * MIB)])- await waitFor(() => expect(attachedImages()).toHaveLength(1))- expect(attachedImages()[0]?.sizeBytes).toBe(12 * MIB)- expect(screen.queryByRole('alert')).toBeNull()+ it('infers the mime type from the file name when the OS reports none', async () => {+ const { container } = render(<ComposerPanel inline />)++ fireEvent.change(pickerInput(container), {+ target: { files: [imageFile('screenshot.png', '')] },+ })++ await waitFor(() => expect(actionsMock.handleAddComposerImageAttachments).toHaveBeenCalled())+ expect(expectSinglePreparedImage().mimeType).toBe('image/png')})- it('refuses an image over the 16MiB limit and names the limit in MiB', async () => {- const { container } = render(<ComposerPanel inline={false} />)- pickFiles(container, [imageFile('over.png', 17 * MIB)])- await waitFor(() =>- expect(screen.getByRole('alert').textContent).toContain(- 'over.png is too large. Max size is 16 MiB.'- )+ it('explains a rejected file type and still attaches the valid image in the same batch', async () => {+ const { container } = render(<ComposerPanel inline />)++ fireEvent.change(pickerInput(container), {+ target: {+ files: [imageFile('animation.gif', 'image/gif'), imageFile('good.png', 'image/png')],+ },+ })++ await waitFor(() => expect(actionsMock.handleAddComposerImageAttachments).toHaveBeenCalled())+ expect(screen.getByRole('alert').textContent).toBe(+ 'animation.gif is not supported. Use PNG or JPEG.')- expect(composerActions.handleAddComposerImageAttachments).not.toHaveBeenCalled()+ expect(expectSinglePreparedImage().name).toBe('good.png')})- it('attaches 10MiB + 7MiB in the composer; the hop owns the aggregate', async () => {- const { container } = render(<ComposerPanel inline={false} />)- pickFiles(container, [imageFile('fits.png', 10 * MIB), imageFile('over-total.png', 7 * MIB)])- await waitFor(() => expect(attachedImages()).toHaveLength(2))- expect(attachedImages().map(attachment => attachment.name)).toEqual([- 'fits.png',- 'over-total.png',- ])- expect(screen.queryByRole('alert')).toBeNull()- })+ it('explains an oversize image and attaches nothing', async () => {+ const { container } = render(<ComposerPanel inline />)- it('attaches a 7MiB image when 10MiB is already in the composer', async () => {- composerState.composerImageAttachments = [existingAttachment('kept.png', 10 * MIB)]- const { container } = render(<ComposerPanel inline={false} />)- pickFiles(container, [imageFile('over-total.png', 7 * MIB)])- await waitFor(() => expect(attachedImages()).toHaveLength(1))- expect(attachedImages()[0]?.name).toBe('over-total.png')- expect(screen.queryByRole('alert')).toBeNull()+ fireEvent.change(pickerInput(container), {+ target: {- files: [imageFile('huge.png', 'image/png', new Uint8Array(COMPOSER_MAX_IMAGE_BYTES + 1))],++ files: [imageFileWithReportedSize('huge.png', 'image/png', COMPOSER_MAX_IMAGE_BYTES + 1)],+ },+ })++ await waitFor(() => expect(screen.getByRole('alert')).toBeTruthy())- expect(screen.getByRole('alert').textContent).toBe('huge.png is too large. Max size is 3 MB.')++ expect(screen.getByRole('alert').textContent).toBe('huge.png is too large. Max size is 16 MiB.')++ expect(actionsMock.handleAddComposerImageAttachments).not.toHaveBeenCalled()+ })+- it('accepts a 5MiB image when 10MiB is already attached', async () => {- composerState.composerImageAttachments = [existingAttachment('kept.png', 10 * MIB)]- const { container } = render(<ComposerPanel inline={false} />)- pickFiles(container, [imageFile('five.png', 5 * MIB)])- await waitFor(() => expect(attachedImages()).toHaveLength(1))++ it('accepts a 12MiB image above the usual 10MiB target', async () => {++ const { container } = render(<ComposerPanel inline />)++ fireEvent.change(pickerInput(container), {++ target: { files: [imageFileWithReportedSize('large.png', 'image/png', 12 * 1024 * 1024)] },++ })++ await waitFor(() => expect(actionsMock.handleAddComposerImageAttachments).toHaveBeenCalled())++ expect(expectSinglePreparedImage().sizeBytes).toBe(12 * 1024 * 1024)+ expect(screen.queryByRole('alert')).toBeNull()+ })++ it('refuses a 2049 px image before send and names the pixel bound', async () => {- const { container } = render(<ComposerPanel inline={false} />)- pickFiles(container, [pngIhdrFile('over-res.png', 2049, 128)])++ const { container } = render(<ComposerPanel inline />)++ fireEvent.change(pickerInput(container), {++ target: { files: [pngIhdrFile('over-res.png', COMPOSER_MAX_IMAGE_DIMENSION + 1, 128)] },++ })+ await waitFor(() =>+ expect(screen.getByRole('alert').textContent).toContain(- 'over-res.png is too large. Max resolution is 2048 px.'++ `over-res.png is too large. Max resolution is ${COMPOSER_MAX_IMAGE_DIMENSION} px.`+ )+ )- expect(composerActions.handleAddComposerImageAttachments).not.toHaveBeenCalled()- expect(attachedImages()).toHaveLength(0)+ expect(actionsMock.handleAddComposerImageAttachments).not.toHaveBeenCalled()+ })++ it('attaches 20 images from one pick and says how many did not fit', async () => {+ expect(COMPOSER_MAX_IMAGE_ATTACHMENTS).toBe(20)+ const { container } = render(<ComposerPanel inline />)+ const files = Array.from({ length: 22 }, (_, index) =>+ imageFile(`photo-${index + 1}.png`, 'image/png', [...PNG_BYTES, index])+ )++ fireEvent.change(pickerInput(container), { target: { files } })++ await waitFor(() =>+ expect(actionsMock.handleAddComposerImageAttachments).toHaveBeenCalledTimes(1)+ )+ const [batch] = addedBatches()+ expect(batch?.map(attachment => attachment.name)).toEqual(+ files.slice(0, 20).map(file => file.name)+ )+ expect(screen.getByRole('alert').textContent).toBe(+ 'You can attach up to 20 images per message; 2 images were not added.'+ )})- it('explains files beyond the 20-image cap instead of dropping them silently', async () => {- const small = 64 * 1024- composerState.composerImageAttachments = Array.from({ length: 19 }, (_, index) =>- existingAttachment(`kept-${index + 1}.png`, small)- it('refuses an image that would push the message past the combined image budget', async () => {++ it('attaches 10MiB + 7MiB in the composer; the hop owns the aggregate', async () => {+ const { container } = render(<ComposerPanel inline />)- // Each image is under the per-image cap; two fit the combined budget and- // the third would push it past what the request body can carry.- const rawBytes = Math.floor(COMPOSER_MAX_IMAGE_BYTES * 0.8)- const base64Bytes = Math.ceil(rawBytes / 3) * 4- expect(base64Bytes * 2).toBeLessThanOrEqual(COMPOSER_MAX_TOTAL_IMAGE_BASE64_BYTES)- expect(base64Bytes * 3).toBeGreaterThan(COMPOSER_MAX_TOTAL_IMAGE_BASE64_BYTES)- const files = ['first.png', 'second.png', 'third.png'].map((name, index) => {- const bytes = new Uint8Array(rawBytes)- bytes.set(PNG_BYTES)- bytes[PNG_BYTES.length] = index- 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)).toEqual(['first.png', 'second.png'])- expect(screen.getByRole('alert').textContent).toBe(- 'third.png does not fit in this message: the images in one message are limited to 8 MB in total. Send the attached images first or remove one.'- )- })-- it('counts images already in the composer toward the combined budget', async () => {- composerState.composerImageAttachments = [- {- id: 'already-attached',- name: 'already.png',- mimeType: 'image/png',- dataBase64: 'A'.repeat(COMPOSER_MAX_TOTAL_IMAGE_BASE64_BYTES - 8),- sizeBytes: 1,- previewDataUrl: 'data:image/png;base64,AAAA',- },- ]- const { container } = render(<ComposerPanel inline />)-+ fireEvent.change(pickerInput(container), {- target: { files: [imageFile('small.png', 'image/png')] },++ target: {++ files: [++ imageFileWithReportedSize('fits.png', 'image/png', 10 * 1024 * 1024),++ imageFileWithReportedSize('over-total.png', 'image/png', 7 * 1024 * 1024),++ ],++ },+ })-- // Witness: the picked file was read and judged (the alert names it).+ await waitFor(() =>- expect(screen.getByRole('alert').textContent).toMatch(/^small\.png does not fit/)++ expect(actionsMock.handleAddComposerImageAttachments).toHaveBeenCalledTimes(1))- const { container } = render(<ComposerPanel inline={false} />)- pickFiles(container, [imageFile('twentieth.png', small), imageFile('twenty-first.png', small)])- await waitFor(() => expect(attachedImages()).toHaveLength(1))- expect(screen.getByRole('alert').textContent).toContain(- 'You can attach up to 20 images per message.'- expect(actionsMock.handleAddComposerImageAttachments).not.toHaveBeenCalled()++ expect(addedBatches()[0]?.map(attachment => attachment.name)).toEqual([++ 'fits.png',++ 'over-total.png',++ ])++ expect(screen.queryByRole('alert')).toBeNull()+ })++ it('does not let a dropped non-image take one of the free image slots', async () => {+ // 18 of the 20 slots are taken; the drop carries a PDF ahead of two PNGs.+ composerState.composerImageAttachments = Array.from({ length: 18 }, (_, index) => ({+ id: `attached-${index + 1}`,+ name: `attached-${index + 1}.png`,+ mimeType: 'image/png' as const,+ dataBase64: 'iVBORw0KGgo=',+ sizeBytes: 8,+ previewDataUrl: 'data:image/png;base64,iVBORw0KGgo=',+ }))+ const { container } = render(<ComposerPanel inline />)+ const shell = container.querySelector('.composer-input-shell') as HTMLElement++ fireEvent.drop(shell, {+ dataTransfer: {+ files: [+ imageFile('doc.pdf', 'application/pdf'),+ imageFile('a.png', 'image/png', [...PNG_BYTES, 1]),+ imageFile('b.png', 'image/png', [...PNG_BYTES, 2]),+ ],+ types: ['Files'],+ },+ })++ await waitFor(() =>+ expect(actionsMock.handleAddComposerImageAttachments).toHaveBeenCalledTimes(1))+ const [batch] = addedBatches()+ expect(batch?.map(attachment => attachment.name)).toEqual(['a.png', 'b.png'])+ // The only message is the PDF refusal: no image was counted as skipped.+ expect(screen.getByRole('alert').textContent).toBe('doc.pdf is not supported. Use PNG or JPEG.')})})diff --cc desktop-app/ui/src/constants/attachments.tsindex 3d624791c,9da241ad6..f7c266f9e--- a/desktop-app/ui/src/constants/attachments.ts+++ b/desktop-app/ui/src/constants/attachments.ts@@@ -1,12 -1,12 +1,11 @@@+ // Must match INCOMING_IMAGE_MAX_COUNT in mcp-host/src/agent/incomingImageAttachments.ts.export const COMPOSER_MAX_IMAGE_ATTACHMENTS = 20-export const COMPOSER_MAX_IMAGE_BYTES = 3 * 1024 * 1024/**- * Combined base64 size of the images in one message. rpc-proxy and mcp-host- * parse the message JSON with a 10 MB body limit, and the images travel inline- * in that body; 2 MB is left for the text and the request envelope. 10 MB is- * also the smallest documented provider request limit (Cerebras, 10 MiB), and- * mcp-host forwards the images inline to the model.+ * Composer per-image ceiling. The 16 MiB aggregate lives on the rpc-proxy /+ * mcp-host hop and the shared contract, not in this picker. A poorly+ * compressed 2048 PNG may exceed 10 MiB and must still attach here.*/-export const COMPOSER_MAX_TOTAL_IMAGE_BASE64_BYTES = 8 * 1024 * 1024+export const COMPOSER_MAX_IMAGE_BYTES = 16 * 1024 * 1024+/** Official Codex client long-side bound. The hop 400s frames above this. */+export const COMPOSER_MAX_IMAGE_DIMENSION = 2048export const COMPOSER_ACCEPT_IMAGE_MIME_TYPES = ['image/jpeg', 'image/png'] as const- export const ZAI_IMAGE_ATTACHMENT_UNSUPPORTED_MESSAGE =- 'Image attachments are not supported for agents running on Z AI yet. Switch this agent to a provider with image input support before attaching images.'diff --cc external-rest-api/src/releaseManifest.tsindex 63e911f88,8e5935da1..2589c33a6--- a/external-rest-api/src/releaseManifest.ts+++ b/external-rest-api/src/releaseManifest.ts@@@ -9,7 -9,7 +9,7 @@@ export type ReleaseManifest =export const releaseManifest: ReleaseManifest = {releaseId: 'v0.8.1',externalRestApiVersion: '0.1.82',- rpcProxyVersion: '0.1.84',- rpcProxyVersion: '0.1.82',++ rpcProxyVersion: '0.1.85',desktopVersion: '0.8.1',minimumDesktopVersion: '0.1.252',}diff --cc mcp-host/src/__tests__/server.attachments.test.tsindex 23dc4f547,f2f330493..d604b07a4--- a/mcp-host/src/__tests__/server.attachments.test.ts+++ b/mcp-host/src/__tests__/server.attachments.test.ts@@@ -1,8 -1,8 +1,15 @@@import { describe, expect, it, vi } from 'vitest'import type { AddressInfo } from 'net'++import { createRequire } from 'node:module'import type { Attachment } from '../core/types'import type { RPCServer } from '../server'++const { declaredHeaderPngOfSize } = createRequire(import.meta.url)(++ '../../../packages/llm-provider-attempt-contract/testImageFixtures.cjs'++) as {++ declaredHeaderPngOfSize: (targetBytes: number) => Buffer++}++const sampleAttachment: Attachment = {id: 'att_1',kind: 'image',@@@ -143,3 -143,72 +150,90 @@@ describe('RPCServer attachment response}})})++ describe('RPCServer message body limit', () => {- // Desktop sends up to 8 MB of base64 images inline (COMPOSER_MAX_TOTAL_IMAGE_BASE64_BYTES).+ const IMAGE_BUDGET_BYTES = 8 * 1024 * 1024+ const BODY_LIMIT_BYTES = 10 * 1024 * 1024+++ function creditedPngMessage(sizeBytes: number): string {++ return JSON.stringify({++ content: 'describe these images',++ channelType: 'telegram',++ channelId: 'chan-1',++ sender: 'user-1',++ timestamp: new Date().toISOString(),++ messageId: 'msg-large',++ hostRef: 'chatllm',++ attachments: [++ {++ ...sampleAttachment,++ mimeType: 'image/png',++ dataBase64: declaredHeaderPngOfSize(sizeBytes).toString('base64'),++ },++ ],++ })++ }+++ function messageBody(dataBase64Length: number): string {+ return JSON.stringify({+ content: 'describe these images',+ channelType: 'telegram',+ channelId: 'chan-1',+ sender: 'user-1',+ timestamp: new Date().toISOString(),+ messageId: 'msg-large',+ hostRef: 'chatllm',+ attachments: [{ ...sampleAttachment, dataBase64: 'A'.repeat(dataBase64Length) }],+ })+ }++ async function postMessage(baseUrl: string, body: string): Promise<Response> {+ return fetch(`${baseUrl}/v1/runtime/messages`, {+ method: 'POST',+ headers: channelReaderEdgeHeaders(+ { channelType: 'telegram', channelId: 'chan-1', sender: 'user-1' },+ { 'Content-Type': 'application/json' }+ ),+ body,+ })+ }+- it('accepts a message carrying the full 8 MB image budget', async () => {++ it('accepts a message carrying an 8 MiB credited PNG', async () => {+ const received: number[] = []+ const { server, baseUrl } = await startServer(rpcServer => {+ rpcServer.onMessage(async message => {+ received.push(message.attachments?.[0]?.dataBase64.length ?? -1)+ return { success: true, status: 'completed', response: 'ok' }+ })+ })++ try {- const response = await postMessage(baseUrl, messageBody(IMAGE_BUDGET_BYTES))++ const response = await postMessage(baseUrl, creditedPngMessage(IMAGE_BUDGET_BYTES))+ expect(response.status).toBe(200)- expect(received).toEqual([IMAGE_BUDGET_BYTES])++ expect(received[0]).toBeGreaterThan(IMAGE_BUDGET_BYTES)+ } finally {+ await server.stop()+ }+ })+- it('rejects a body over 10 MB with 413 before the message handler runs', async () => {++ it('rejects an uncredited 10 MB body with 413 before the message handler runs', async () => {+ const received: number[] = []+ const { server, baseUrl } = await startServer(rpcServer => {+ rpcServer.onMessage(async message => {+ received.push(message.attachments?.[0]?.dataBase64.length ?? -1)+ return { success: true, status: 'completed', response: 'ok' }+ })+ })++ try {+ const body = messageBody(BODY_LIMIT_BYTES)+ expect(body.length).toBeGreaterThan(BODY_LIMIT_BYTES)+ const response = await postMessage(baseUrl, body)+ // Witness: the server read and judged the body (413 is its answer).+ expect(response.status).toBe(413)+ expect(received).toEqual([])+ } finally {+ await server.stop()+ }+ })+ })diff --cc mcp-host/src/agent/incomingImageAttachments.tsindex 000000000,76a2aed0d..000a6d41cmode 000000,100644..100644--- a/mcp-host/src/agent/incomingImageAttachments.ts+++ b/mcp-host/src/agent/incomingImageAttachments.ts@@@ -1,0 -1,96 +1,97 @@@+ import type { Attachment } from '../core/types'+ import {+ type ImageAttachmentMimeType,+ isCanonicalBase64Shape,+ isImageAttachmentMime,+ } from '../llm/imageInput'+ import type { TaskError } from '../queue/types'++ /**+ * Images accepted in one incoming message. Kept separate from+ * `CLERUM_ATTACHMENT_MAX_COUNT`, which caps the attachments a response returns.+ * Must match COMPOSER_MAX_IMAGE_ATTACHMENTS in desktop-app/ui/src/constants/attachments.ts.- * The combined size is bounded by the 10 MB JSON body limit of rpc-proxy and- * the host server, which carry the images inline.++ * The hop envelope is 24 MiB (rpc-proxy `chatJsonBody` + this host's chat++ * parser) with a 16 MiB credited image budget. This count is the fail-loud++ * admission cap, separate from the hop credit.+ */+ export const INCOMING_IMAGE_MAX_COUNT = 20++ export type IncomingImageValidation =+ | { ok: true; attachments: Attachment[] | undefined }+ | { ok: false; error: TaskError }++ /**+ * First bytes every accepted encoding must start with. The declared media type+ * decides how the provider serializes the image, so a declaration the bytes+ * contradict is rejected here rather than forwarded upstream (CWE-345).+ */+ const MIME_SIGNATURES: Readonly<Record<ImageAttachmentMimeType, Buffer>> = {+ 'image/png': Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),+ 'image/jpeg': Buffer.from([0xff, 0xd8, 0xff]),+ }++ function bytesMatchDeclaredMime(bytes: Buffer, mimeType: ImageAttachmentMimeType): boolean {+ const signature = MIME_SIGNATURES[mimeType]+ return bytes.length >= signature.length && bytes.subarray(0, signature.length).equals(signature)+ }++ /** Reject the whole visual input rather than silently sending only its text. */+ export function validateIncomingImageAttachments(+ raw: unknown,+ limits: { maxCount: number; maxBytes: number }+ ): IncomingImageValidation {+ const reject = (message: string): IncomingImageValidation => ({+ ok: false,+ error: { code: 'LLM_INVALID_ATTACHMENT', message, retryable: false, provider: 'unknown' },+ })+ if (raw == null) return { ok: true, attachments: undefined }+ if (!Array.isArray(raw)) return reject('Image attachments must be a list.')+ if (!raw.length) return { ok: true, attachments: undefined }+ if (raw.length > limits.maxCount)+ return reject('Too many image attachments. Remove an image and try again.')+ const attachments: Attachment[] = []+ for (const item of raw) {+ if (+ !item ||+ typeof item !== 'object' ||+ Array.isArray(item) ||+ item.kind !== 'image' ||+ !isImageAttachmentMime(item.mimeType) ||+ item.encoding !== 'base64' ||+ typeof item.dataBase64 !== 'string' ||+ typeof item.id !== 'string' ||+ !item.id.trim()+ ) {+ return reject('Only PNG or JPEG images encoded as base64 are supported.')+ }+ const data = item.dataBase64+ // Check length before decoding so validation cannot allocate an oversized+ // binary buffer. Canonical base64 avoids ambiguous/partially decoded input.+ if (data.length > Math.ceil(limits.maxBytes / 3) * 4)+ return reject('An image exceeds the attachment size limit.')+ if (!isCanonicalBase64Shape(data)) {+ return reject('An image has invalid base64 data. Attach the original file again.')+ }+ const bytes = Buffer.from(data, 'base64')+ if (bytes.length > limits.maxBytes) return reject('An image exceeds the attachment size limit.')+ if (bytes.toString('base64') !== data)+ return reject('An image has invalid base64 data. Attach the original file again.')+ if (!bytesMatchDeclaredMime(bytes, item.mimeType))+ return reject('An image does not match its declared type. Attach the original file again.')+ attachments.push({+ id: item.id,+ kind: 'image',+ mimeType: item.mimeType,+ encoding: 'base64',+ dataBase64: data,+ ...(typeof item.filename === 'string' ? { filename: item.filename } : {}),+ ...(typeof item.width === 'number' && Number.isFinite(item.width) && item.width > 0+ ? { width: item.width }+ : {}),+ ...(typeof item.height === 'number' && Number.isFinite(item.height) && item.height > 0+ ? { height: item.height }+ : {}),+ })+ }+ return { ok: true, attachments }+ }diff --cc mcp-host/src/agent/taskExecutor.tsindex 4b549c5cc,d4d659da5..2c28452af--- a/mcp-host/src/agent/taskExecutor.ts+++ b/mcp-host/src/agent/taskExecutor.ts@@@ -89,9 -88,10 +89,10 @@@ import { prependTextToParts, textContenimport type { UsageContext } from '../core/types'import type { TaskLifecycle } from '../lifecycle/taskLifecycle'import type { SingleTurnProvider } from '../llm'+ import type { ImageInputResolver } from '../llm/imageInput'import type { PromptCache } from '../llm/promptCache'import { stampStableHashGauge } from '../llm/promptCacheMetrics'-import { isLlmProvider } from '../llm/registryCore'+import { descriptorFor, isLlmProvider } from '../llm/registryCore'import { logger } from '../logger'import type { McpManager } from '../mcp'import { getDisplayName, sanitizeError } from '../progress/intentExtraction.js'diff --cc mcp-host/src/core/adapters/llmPortAdapter.tsindex bdf6d745b,92d1e61a4..934484e02--- a/mcp-host/src/core/adapters/llmPortAdapter.ts+++ b/mcp-host/src/core/adapters/llmPortAdapter.ts@@@ -123,17 -155,20 +155,25 @@@ export class LlmPortAdapter implementsasync complete(request: CompletionRequest): Promise<CompletionResponse> {logger.info(- { provider: this.providerName, model: this.model, messageCount: request.messages.length },- 'Starting completion'+ {++ provider: this.providerName,+ model: this.model,+ messageCount: request.messages.length,+ cache: Boolean(request.systemPromptParts),+ },+ 'LLM completion started')+ // Excluded from the SDK-classification catch below on purpose: a denied+ // image must stay a typed, terminal LlmError instead of being reclassified+ // by `provider.classifyError` (which would make it retryable).+ const messages = this.prepareMessagesForImageInput(+ request.messages,+ this.dispatchMethodFor(request, false)+ )const requestId = newRequestId()try {- const response = await this.dispatchComplete(request)+ const response = await this.dispatchComplete({ ...request, messages })- logger.info({ finishReason: response.finish_reason }, 'Completion returned')+ logger.info({ finishReason: response.finish_reason }, 'LLM completion finished')this.recordUsage(requestId, request.usageContext, response.usage)return response} catch (err) {@@@ -147,17 -183,22 +188,23 @@@model: this.model,toolCount: request.tools.length,messageCount: request.messages.length,+ cache: Boolean(request.systemPromptParts),},- 'Starting tool completion'+ 'LLM tool completion started')+ // Same exclusion as `complete`: deny BEFORE the try/catch that classifies+ // provider SDK errors.+ const messages = this.prepareMessagesForImageInput(+ request.messages,+ this.dispatchMethodFor(request, true)+ )const requestId = newRequestId()try {- const response = await this.dispatchCompleteWithTools(request)+ const response = await this.dispatchCompleteWithTools({ ...request, messages })const toolCallCount = response.tool_calls?.length ?? 0logger.info(- { finishReason: response.finish_reason, toolCallCount },- 'Tool completion returned'+ { finishReason: response.finish_reason, toolCallCount, usage: response.usage },+ 'LLM tool completion finished')this.recordUsage(requestId, request.usageContext, response.usage)return response@@@ -167,59 -208,21 +214,63 @@@}/**- * T2.2 — route to the provider's cache-aware path when both- * `request.systemPromptParts` is set AND the provider implements the- * cache-aware method (Claude). Otherwise concat the parts back into a- * single `system` message and call the legacy method (OpenAI / ZAI /- * Bailian don't expose explicit cache markers; they cache implicitly by- * prefix when the routing is stable).+ * Preserve the historical content-deduplicated API-key view without changing+ * the loop's canonical messages. Each fallback adapter selects its own view;+ * source-binding transports retain all identities before hashing them.*/+ private providerMessages(messages: ChatMessage[]): ChatMessage[] {+ if (+ this.provider.requiresImageSourceIdentity ||+ !messages.some(message => message.contentParts?.some(part => part.sourceIdentityOnly))+ ) {+ return messages+ }+ const imageKey = (part: { mimeType: string; data: string }) =>+ `${part.mimeType}\u0000${part.data}`+ const represented = new Set<string>()+ for (const message of messages) {+ for (const part of message.contentParts ?? []) {+ if (part.type === 'image' && !part.sourceIdentityOnly) represented.add(imageKey(part))+ }+ }+ return messages.flatMap(message => {+ if (!message.contentParts?.some(part => part.sourceIdentityOnly)) return [message]+ let restoredImage = false+ const selected = new Set(message.contentParts.filter(part => !part.sourceIdentityOnly))+ for (const part of message.contentParts) {+ if (part.type !== 'image' || !part.sourceIdentityOnly || represented.has(imageKey(part)))+ continue+ // In a mixed chain, pruning can remove the older legacy representative.+ // Retain one current frame rather than silently losing all visual input.+ selected.add(part)+ represented.add(imageKey(part))+ restoredImage = true+ }+ if (restoredImage) {+ for (const part of message.contentParts) if (part.type === 'text') selected.add(part)+ }+ const contentParts = message.contentParts+ .filter(part => selected.has(part))+ .map(part => {+ if (!part.sourceIdentityOnly) return part+ const visible = { ...part }+ delete visible.sourceIdentityOnly+ return visible+ })+ return contentParts.length ? [{ ...message, contentParts }] : []+ })+ }++ /** Native cache markers when supported, otherwise the existing system-text projection. */private async dispatchComplete(request: CompletionRequest): Promise<CompletionResponse> {const parts = request.systemPromptParts+ const providerMessages = this.providerMessages(request.messages)- if (parts && this.provider.completeSingleTurnAndCache) {+ if (+ this.dispatchMethodFor(request, false) === 'completeAndCache' &&+ parts &&+ this.provider.completeSingleTurnAndCache+ ) {- return this.provider.completeSingleTurnAndCache(parts, request.messages, {+ return this.provider.completeSingleTurnAndCache(parts, providerMessages, {max_tokens: request.max_tokens,temperature: request.temperature,signal: request.signal,@@@ -237,11 -240,14 +288,15 @@@request: ToolCompletionRequest): Promise<ToolCompletionResponse> {const parts = request.systemPromptParts+ const providerMessages = this.providerMessages(request.messages)- if (parts && this.provider.completeSingleTurnWithToolsAndCache) {+ if (+ this.dispatchMethodFor(request, true) === 'completeWithToolsAndCache' &&+ parts &&+ this.provider.completeSingleTurnWithToolsAndCache+ ) {return this.provider.completeSingleTurnWithToolsAndCache(parts,- request.messages,+ providerMessages,request.tools,{max_tokens: request.max_tokens,diff --cc mcp-host/src/core/orchestration/__tests__/toolUseLoopMessages.test.tsindex 4de21e464,2b1096ff3..8dcd7727f--- a/mcp-host/src/core/orchestration/__tests__/toolUseLoopMessages.test.ts+++ b/mcp-host/src/core/orchestration/__tests__/toolUseLoopMessages.test.ts@@@ -220,150 -219,39 +220,187 @@@ describe('appendToolResults', () =>expect(collectedAttachments).toEqual([])})+ const imageAttachment = (id: string, data = PNG_2X2_BASE64): Attachment => ({+ id,+ kind: 'image',+ mimeType: 'image/png',+ encoding: 'base64',+ dataBase64: data,+ })++ it('carries the producing tool call as provenance and keeps content equal to the text parts', () => {+ const messages: ChatMessage[] = []+ const collectedAttachments: Attachment[] = []++ appendToolResults(+ messages,+ [+ {+ tool_call_id: 'tc_frame',+ name: 'desktop_screenshot',+ content: 'screenshot taken',+ is_error: false,+ attachments: [imageAttachment('att-frame')],+ },+ ],+ collectedAttachments,+ true+ )++ expect(messages[0]).toEqual({+ role: 'tool',+ content: 'screenshot taken',+ tool_call_id: 'tc_frame',+ name: 'desktop_screenshot',+ })+ const visual = messages[1]+ expect(visual.role).toBe('user')+ expect(visual.content).toBe('Here are the screenshots from the tool results above.')++ expect(visual.imageOrigin).toBe('tool_result')+ expect(visual.contentParts).toEqual([+ { type: 'text', text: visual.content },+ {+ type: 'image',+ mimeType: 'image/png',+ data: PNG_2X2_BASE64,+ source: { kind: 'tool', attachmentId: 'att-frame', toolCallId: 'tc_frame' },+ },+ ])+ })++ it('keeps the same bytes from two distinct tool calls as two visual parts', () => {+ const messages: ChatMessage[] = []+ const collectedAttachments: Attachment[] = []++ appendToolResults(+ messages,+ [+ {+ tool_call_id: 'tc_first',+ name: 'desktop_screenshot',+ content: 'first',+ is_error: false,+ attachments: [imageAttachment('att-first')],+ },+ ],+ collectedAttachments,+ true+ )+ appendToolResults(+ messages,+ [+ {+ tool_call_id: 'tc_second',+ name: 'desktop_screenshot',+ content: 'second',+ is_error: false,+ attachments: [imageAttachment('att-second')],+ },+ ],+ collectedAttachments,+ true+ )++ // The user-facing list still collapses the repeated frame...+ expect(collectedAttachments).toHaveLength(1)+ // ...while the model sees one frame per tool call that produced it.+ const visualMessages = messages.filter(message => message.contentParts !== undefined)+ expect(visualMessages).toHaveLength(2)+ expect(visualMessages.map(message => message.contentParts![1])).toEqual([+ expect.objectContaining({+ source: { kind: 'tool', attachmentId: 'att-first', toolCallId: 'tc_first' },+ }),+ expect.objectContaining({+ source: { kind: 'tool', attachmentId: 'att-second', toolCallId: 'tc_second' },+ }),+ ])+ })++ it('does not repeat the same frame twice for one tool call', () => {+ const messages: ChatMessage[] = []+ const collectedAttachments: Attachment[] = []++ appendToolResults(+ messages,+ [+ {+ tool_call_id: 'tc_repeat',+ name: 'desktop_screenshot',+ content: 'twice',+ is_error: false,+ attachments: [imageAttachment('att-a'), imageAttachment('att-a')],+ },+ ],+ collectedAttachments,+ true+ )++ expect(collectedAttachments).toHaveLength(1)+ expect(messages).toHaveLength(2)+ expect(messages[1].contentParts).toHaveLength(2)+ expect(messages[1].contentParts![1]).toEqual(+ expect.objectContaining({+ source: { kind: 'tool', attachmentId: 'att-a', toolCallId: 'tc_repeat' },+ })+ )+ })++ it('preserves separate attachment identities in one call and avoids visual re-delivery', () => {+ const messages: ChatMessage[] = []+ const collected: Attachment[] = []+ const result = {+ tool_call_id: 'tc_identity',+ name: 'desktop_screenshot',+ content: 'frames',+ is_error: false,+ attachments: [imageAttachment('att-a'), imageAttachment('att-b')],+ }+ appendToolResults(messages, [result], collected, true)+ appendToolResults(messages, [result], collected, true)+ expect(collected).toHaveLength(1)+ const images = messages+ .flatMap(message => message.contentParts ?? [])+ .filter(part => part.type === 'image')+ expect(images).toHaveLength(2)+ expect(images.map(part => part.source)).toEqual([+ { kind: 'tool', attachmentId: 'att-a', toolCallId: 'tc_identity' },+ { kind: 'tool', attachmentId: 'att-b', toolCallId: 'tc_identity' },+ ])+ })+++ it('marks the screenshot message as tool-originated (#654)', () => {+ const messages: ChatMessage[] = []+ const collectedAttachments: Attachment[] = []+ const payloadKey = 'data' + 'Base' + '64'+ const screenshot = (id: string, body: string): Attachment =>+ ({+ id,+ kind: 'image',+ mimeType: 'image/png',+ encoding: ['base', '64'].join('') as Attachment['encoding'],+ [payloadKey]: Buffer.from(body).toString(('base' + '64') as BufferEncoding),+ sourceTool: 'browser__screenshot',+ }) as unknown as Attachment++ appendToolResults(+ messages,+ [+ {+ tool_call_id: 'tc_1',+ name: 'browser__screenshot',+ content: 'captured 2 viewports',+ is_error: false,+ attachments: [screenshot('shot-1', 'first-png'), screenshot('shot-2', 'second-png')],+ },+ ],+ collectedAttachments+ )++ const screenshotMessage = messages.find(message => message.role === 'user')+ expect(screenshotMessage).toBeDefined()+ // The flag is what lets `LlmPortAdapter.prepareMessagesForImageInput` tell a+ // tool screenshot (withheld) from a user attachment (refused).+ expect(screenshotMessage).toMatchObject({ role: 'user', imageOrigin: 'tool_result' })+ expect(screenshotMessage!.contentParts?.filter(part => part.type === 'image')).toHaveLength(2)+ })})diff --cc mcp-host/src/core/orchestration/toolUseLoopMessages.tsindex 4518f3d1b,249b03452..33c7cfb24--- a/mcp-host/src/core/orchestration/toolUseLoopMessages.ts+++ b/mcp-host/src/core/orchestration/toolUseLoopMessages.ts@@@ -145,15 -95,13 +145,19 @@@ export function appendToolResultsif (pendingImages.length > 0) {messages.push({role: 'user',- content: 'Here are the screenshots from the tool results above.',+ content: TOOL_RESULT_IMAGE_TEXT,+ // #654 — the parts below came from tool results, not from the user. The+ // adapter withholds them (and says so in `content`) when the model has no+ // affirmative image-input evidence, instead of failing the whole turn.+ imageOrigin: 'tool_result',contentParts: [- { type: 'text', text: 'Here are the screenshots from the tool results above.' },+ {+ type: 'text',+ text: TOOL_RESULT_IMAGE_TEXT,+ ...(pendingImages.every(part => part.sourceIdentityOnly)+ ? { sourceIdentityOnly: true as const }+ : {}),+ },...pendingImages,],})diff --cc mcp-host/src/core/types.tsindex 7d135ffa2,d18460e6f..3e4ce001c--- a/mcp-host/src/core/types.ts+++ b/mcp-host/src/core/types.ts@@@ -42,13 -19,15 +42,21 @@@ export type MessageContentPartexport interface ChatMessage {role: MessageRolecontent: string- contentParts?: MessageContentPart[] // only set when tool results have images+ /**+ * Set when the turn carries images (user attachments or tool-result frames).+ * Invariant: when parts are present, `content` is the '\n'-join of the text+ * parts. The Codex V2 parser enforces that equality, and the local producers+ * below keep both fields in sync.+ */+ contentParts?: MessageContentPart[]+ /**+ * Issue #654 — set when the image parts were produced by a tool result, never+ * by the user. A user who attaches an image to a model without affirmative+ * image-input evidence gets a typed refusal; a screenshot the agent's own+ * tool returned is withheld instead, so an unverified model can still finish+ * a text task that happens to call a screenshot tool.+ */+ imageOrigin?: 'tool_result'tool_call_id?: stringname?: stringtool_calls?: ToolCall[] | nulldiff --cc rpc-proxy/package-lock.jsonindex b9042e3d9,a8715fece..34e2a4b9e--- a/rpc-proxy/package-lock.json+++ b/rpc-proxy/package-lock.json@@@ -1,12 -1,12 +1,12 @@@{"name": "clerum-rpc-proxy",- "version": "0.1.84",- "version": "0.1.82",++ "version": "0.1.85","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-rpc-proxy",- "version": "0.1.84",- "version": "0.1.82",++ "version": "0.1.85","dependencies": {"cors": "^2.8.5","express": "^4.21.2",diff --cc rpc-proxy/package.jsonindex dd8684b18,936dba29e..2422d36cd--- a/rpc-proxy/package.json+++ b/rpc-proxy/package.json@@@ -1,6 -1,6 +1,6 @@@{"name": "clerum-rpc-proxy",- "version": "0.1.84",- "version": "0.1.82",++ "version": "0.1.85","description": "External-facing RPC proxy for tenant-scoped MCP access","main": "dist/main.js","engines": {diff --cc rpc-proxy/src/__tests__/app-body-limit.test.tsindex 000000000,69ecef7fb..b5345c69cmode 000000,100644..100644--- a/rpc-proxy/src/__tests__/app-body-limit.test.ts+++ b/rpc-proxy/src/__tests__/app-body-limit.test.ts@@@ -1,0 -1,257 +1,270 @@@+ import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'+ import { once } from 'node:events'+ import { type Server, request as httpRequest } from 'node:http'++import { createRequire } from 'node:module'+ import type { AddressInfo } from 'node:net'+ import request from 'supertest'+ import { createApp } from '../app.js'+ import { config } from '../config.js'+ import { createSandboxUiSession } from '../services/sandboxUiSession.js'+++const { declaredHeaderPngOfSize } = createRequire(import.meta.url)(++ '../../../packages/llm-provider-attempt-contract/testImageFixtures.cjs'++) as {++ declaredHeaderPngOfSize: (targetBytes: number) => Buffer++}+++ const authTokenMock = vi.hoisted(() => ({+ verifyRpcToken: vi.fn(),+ }))++ const serviceMock = vi.hoisted(() => ({+ resolveHostConnectionForUser: vi.fn(),+ forwardHostMessageToHost: vi.fn(),+ }))++ vi.mock('../authToken.js', () => authTokenMock)+ vi.mock('../services/mcpProxyService.js', () => serviceMock)+-// Desktop sends up to 8 MB of base64 images inline in the message JSON-// (COMPOSER_MAX_TOTAL_IMAGE_BASE64_BYTES); the parser must admit that plus text.++// Chat messages use chatJsonBody: 24 MiB envelope + 16 MiB hop credit.+ const MIB = 1024 * 1024+ const IMAGE_BUDGET_BYTES = 8 * MIB-const BODY_LIMIT_BYTES = 10 * MIB++const BODY_LIMIT_BYTES = 25 * MIB+ const MESSAGES_PATH = '/api/v1/rpc/hosts/chatllm/messages'++ const VALID_CLAIMS = {+ sub: 'user-uuid-abc',+ typ: 'user' as const,+ accessScope: 'team' as const,+ teamId: 'team-1',+ scopes: ['host:message:invoke'],+ hostRefs: ['chatllm'],+ jti: 'j1',+ iat: 1,+ exp: 9999999999,+ }++ const HOST_CONNECTION = {+ name: 'chatllm',+ url: 'http://chatllm:8080',+ headers: {+ 'x-clerum-edge-caller': 'rpc-proxy',+ 'x-clerum-edge-host-ref': 'chatllm',+ 'x-clerum-edge-user-id': 'user-uuid-abc',+ },+ }++ function messageBody(dataBase64Length: number): string {+ return JSON.stringify({+ content: 'describe these images',+ attachments: [{ kind: 'image', dataBase64: 'A'.repeat(dataBase64Length) }],+ })+ }+++function creditedPngMessage(sizeBytes: number): string {++ return JSON.stringify({++ content: 'describe these images',++ attachments: [++ {++ kind: 'image',++ encoding: 'base64',++ mimeType: 'image/png',++ dataBase64: declaredHeaderPngOfSize(sizeBytes).toString('base64'),++ },++ ],++ })++}+++ // ~1 MB of bytes that are not valid JSON: the parser must read all of it+ // before it can fail, which is exactly the work anonymous callers must not+ // be able to trigger.+ const MALFORMED_JSON_BODY = `{"content": "${'x'.repeat(MIB)}`++ // How long a partially uploaded request may wait for its response. A route+ // that parses before authenticating never answers here: body-parser keeps+ // waiting for the bytes that were announced but not sent.+ const EARLY_RESPONSE_TIMEOUT_MS = 2000++ /**+ * Announces MALFORMED_JSON_BODY via Content-Length but uploads only its first+ * 16 KB, then waits for the response. A response can only arrive if the route+ * answered without reading the body, which proves the body was never parsed.+ * The upload is aborted once the response (or the timeout) is observed.+ */+ function respondsBeforeBodyIsSent(+ server: Server,+ path: string,+ headers: Record<string, string>+ ): Promise<{ status: number; body: unknown }> {+ const { port } = server.address() as AddressInfo+ return new Promise((resolve, reject) => {+ const req = httpRequest({+ host: '127.0.0.1',+ port,+ method: 'POST',+ path,+ headers: {+ ...headers,+ 'content-type': 'application/json',+ 'content-length': String(Buffer.byteLength(MALFORMED_JSON_BODY)),+ },+ })+ const timer = setTimeout(() => {+ req.destroy()+ reject(+ new Error(+ `no response within ${EARLY_RESPONSE_TIMEOUT_MS} ms of a partial upload: the route waits for the body before authenticating`+ )+ )+ }, EARLY_RESPONSE_TIMEOUT_MS)+ req.on('response', res => {+ const chunks: Buffer[] = []+ res.on('data', (chunk: Buffer) => chunks.push(chunk))+ res.on('end', () => {+ clearTimeout(timer)+ req.destroy()+ resolve({+ status: res.statusCode ?? 0,+ body: JSON.parse(Buffer.concat(chunks).toString('utf8')),+ })+ })+ })+ req.on('error', err => {+ clearTimeout(timer)+ reject(err)+ })+ req.write(MALFORMED_JSON_BODY.slice(0, 16 * 1024))+ })+ }++ function forwardedAttachmentLength(): number {+ expect(serviceMock.forwardHostMessageToHost).toHaveBeenCalledTimes(1)+ const forwarded = serviceMock.forwardHostMessageToHost.mock.calls[0][1] as {+ attachments: Array<{ dataBase64: string }>+ }+ return forwarded.attachments[0].dataBase64.length+ }++ beforeEach(() => {+ vi.clearAllMocks()+ authTokenMock.verifyRpcToken.mockReturnValue(VALID_CLAIMS)+ serviceMock.resolveHostConnectionForUser.mockResolvedValue(HOST_CONNECTION)+ serviceMock.forwardHostMessageToHost.mockResolvedValue({ success: true, status: 'completed' })+ })++ describe('rpc-proxy JSON body limit on POST /rpc/hosts/:hostRef/messages', () => {- it('accepts a body carrying the full 8 MB image budget and forwards it', async () => {++ it('accepts a body carrying an 8 MiB credited PNG and forwards it', async () => {+ const res = await request(createApp())+ .post(MESSAGES_PATH)+ .set('authorization', 'Bearer token')+ .set('content-type', 'application/json')- .send(messageBody(IMAGE_BUDGET_BYTES))++ .send(creditedPngMessage(IMAGE_BUDGET_BYTES))++ expect(res.status).toBe(200)- expect(forwardedAttachmentLength()).toBe(IMAGE_BUDGET_BYTES)++ expect(forwardedAttachmentLength()).toBeGreaterThan(IMAGE_BUDGET_BYTES)+ })+- it('accepts a ~9.9 MB body just under the 10 MB limit', async () => {- const dataLength = Math.floor(9.9 * MIB)- const body = messageBody(dataLength)- // Above any lower limit such as 8.4 MB, below the 10 MB limit.- expect(body.length).toBeGreaterThan(9.8 * MIB)- expect(body.length).toBeLessThan(BODY_LIMIT_BYTES)++ it('accepts a 12 MiB credited PNG under the 24 MiB envelope', async () => {++ const body = creditedPngMessage(12 * MIB)++ expect(body.length).toBeGreaterThan(16 * MIB)++ expect(body.length).toBeLessThan(24 * MIB)++ const res = await request(createApp())+ .post(MESSAGES_PATH)+ .set('authorization', 'Bearer token')+ .set('content-type', 'application/json')+ .send(body)++ expect(res.status).toBe(200)- expect(forwardedAttachmentLength()).toBe(dataLength)++ expect(forwardedAttachmentLength()).toBeGreaterThan(12 * MIB)+ })+- it('refuses a body over 10 MB', async () => {++ it('refuses a body over the 24 MiB envelope', async () => {+ const body = messageBody(BODY_LIMIT_BYTES)- expect(body.length).toBeGreaterThan(BODY_LIMIT_BYTES)++ expect(body.length).toBeGreaterThan(24 * MIB)++ const res = await request(createApp())+ .post(MESSAGES_PATH)+ .set('authorization', 'Bearer token')+ .set('content-type', 'application/json')+ .send(body)+- // The app's error handler maps every non-timeout error, including- // body-parser's PayloadTooLargeError, to 500.- expect(res.status).toBe(500)- expect(res.body).toEqual({- error: 'Internal Server Error',- message: 'request entity too large',- })++ expect(res.status).toBe(413)++ expect(res.body).toEqual({ error: 'Payload Too Large' })+ // Liveness witness for the negative below: the token was checked.+ expect(authTokenMock.verifyRpcToken).toHaveBeenCalledTimes(1)+ expect(serviceMock.forwardHostMessageToHost).not.toHaveBeenCalled()+ })+ })++ describe('rpc-proxy parses request bodies only after authentication (M4)', () => {+ // A real listening server so the unauthenticated cases can upload a body+ // partially and observe the response while the rest is still unsent.+ let server: Server++ beforeAll(async () => {+ // Same binding as supertest's own ephemeral servers (all interfaces).+ server = createApp().listen(0)+ await once(server, 'listening')+ })++ afterAll(async () => {+ server.closeAllConnections()+ server.close()+ await once(server, 'close')+ })++ it('rejects an unauthenticated malformed body with 401 before receiving it', async () => {+ authTokenMock.verifyRpcToken.mockReturnValue(null)++ const res = await respondsBeforeBodyIsSent(server, MESSAGES_PATH, {+ authorization: 'Bearer forged',+ })++ expect(res.status).toBe(401)+ expect(res.body).toEqual({ error: 'Unauthorized' })+ // Liveness witness: the auth middleware ran and saw the token.+ expect(authTokenMock.verifyRpcToken).toHaveBeenCalledWith('forged')+ })++ it('still parses the same malformed body once the caller is authenticated', async () => {+ const res = await request(server)+ .post(MESSAGES_PATH)+ .set('authorization', 'Bearer token')+ .set('content-type', 'application/json')+ .send(MALFORMED_JSON_BODY)++ // The parser is mounted on the route: its SyntaxError reaches the app's+ // error handler (mapped to 500), and the handler never runs.+ expect(res.status).toBe(500)+ expect(res.body.error).toBe('Internal Server Error')+ expect(res.body.message).toMatch(/JSON/)+ expect(authTokenMock.verifyRpcToken).toHaveBeenCalledWith('token')+ expect(serviceMock.forwardHostMessageToHost).not.toHaveBeenCalled()+ })++ it('rejects the cookie-authenticated sandbox-ui oauth/token route without a cookie before receiving the body', async () => {+ const res = await respondsBeforeBodyIsSent(+ server,+ '/api/v1/sandbox-ui/sandbox-recipes/r1/oauth/token',+ {}+ )++ expect(res.status).toBe(401)+ expect(res.body).toEqual({ error: 'sandbox_ui_session_required' })+ })++ it('parses the sandbox-ui oauth/token body once the session cookie is valid', async () => {+ const cookie = createSandboxUiSession('user-uuid-abc', 'sandbox-recipes', 'r1')++ const res = await request(server)+ .post('/api/v1/sandbox-ui/sandbox-recipes/r1/oauth/token')+ .set('Cookie', `${config.sandboxUiCookieName}=${cookie}`)+ .set('content-type', 'application/json')+ .send(MALFORMED_JSON_BODY)++ expect(res.status).toBe(500)+ expect(res.body.error).toBe('Internal Server Error')+ expect(res.body.message).toMatch(/JSON/)+ })+ })diff --cc rpc-proxy/src/__tests__/bodylimits.test.tsindex 3fc84c177,000000000..c4574ceb4mode 100644,000000..100644--- a/rpc-proxy/src/__tests__/bodylimits.test.ts+++ b/rpc-proxy/src/__tests__/bodylimits.test.ts@@@ -1,335 -1,0 +1,377 @@@+/**+ * Body-budget tests for the rpc-proxy JSON parser.+ *+ * Proves on a real listening `createApp()` socket:+ * - `POST /api/v1/rpc/hosts/:hostRef/messages` carries the documented image+ * payloads (a 10MiB image, a 5MiB JPEG, 10MiB + 5MiB, and three 5MiB images) to the auth+ * boundary instead of being rejected as too large.+ * - The larger ceiling is NOT a general text allowance: non-image bytes stay+ * capped at 6MiB, and every other route keeps its 6MiB parser.+ * - The sandbox-ui view proxy stays parser-free, so http-proxy can stream the+ * raw body upstream instead of the parser draining it.+ *+ * Auth is deliberately NOT mocked. A 401 is the proof that the body crossed the+ * parser and only the auth boundary stopped it.+ */- import { afterAll, beforeAll, describe, expect, it } from 'vitest'++import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'+import type { Server } from 'http'+import type { AddressInfo } from 'net'+import { createRequire } from 'node:module'+import { createApp } from '../app.js'+++const authTokenMock = vi.hoisted(() => ({++ verifyRpcToken: vi.fn(),++}))++++const serviceMock = vi.hoisted(() => ({++ resolveHostConnectionForUser: vi.fn(),++ resolveServerConnectionForUser: vi.fn(),++ forwardHostMessageToHost: vi.fn(),++ validateRpcRequest: vi.fn(),++ forwardRpcToServer: vi.fn(),++}))++++vi.mock('../authToken.js', () => authTokenMock)++vi.mock('../services/mcpProxyService.js', () => serviceMock)++++const VALID_CLAIMS = {++ sub: 'user-uuid-abc',++ typ: 'user' as const,++ accessScope: 'team' as const,++ teamId: 'team-1',++ scopes: ['host:message:invoke', 'mcp:server:invoke'],++ hostRefs: ['chatllm'],++ jti: 'j1',++ iat: 1,++ exp: 9999999999,++}+++const { declaredHeaderPngOfSize, jpegOfSize } = createRequire(import.meta.url)(+ '../../../packages/llm-provider-attempt-contract/testImageFixtures.cjs'+) as {+ declaredHeaderPngOfSize: (targetBytes: number) => Buffer+ jpegOfSize: (targetBytes: number, width?: number, height?: number) => Buffer+}++const MIB = 1024 * 1024+const BASE64_ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'++let baseUrl: string+let server: Server++beforeAll(async () => {+ server = createApp().listen(0)+ await new Promise<void>(resolve => server.once('listening', resolve))+ baseUrl = `http://127.0.0.1:${(server.address() as AddressInfo).port}`+})+++beforeEach(() => {++ vi.clearAllMocks()++ authTokenMock.verifyRpcToken.mockReturnValue(VALID_CLAIMS)++ serviceMock.resolveHostConnectionForUser.mockResolvedValue({++ name: 'chatllm',++ url: 'http://chatllm:8080',++ headers: {},++ })++ serviceMock.forwardHostMessageToHost.mockResolvedValue({ success: true, status: 'completed' })++ serviceMock.resolveServerConnectionForUser.mockResolvedValue({++ name: 'demo',++ url: 'http://demo:8080',++ headers: {},++ })++ serviceMock.validateRpcRequest.mockReturnValue({ method: 'ping' })++ serviceMock.forwardRpcToServer.mockResolvedValue({ jsonrpc: '2.0', id: 1, result: {} })++})+++afterAll(async () => {+ // The view-proxy case aborts a request mid-body, so drop any socket that is+ // still open: a listener left behind here would surface as a parse error in+ // whichever sibling test file ran next.+ server.closeAllConnections()+ await new Promise<void>(resolve => server.close(() => resolve()))+})++const pngBase64Cache = new Map<number, string>()+const jpegBase64Cache = new Map<number, string>()++function cachedBase64(cache: Map<number, string>, bytes: Buffer, sizeBytes: number): string {+ const cached = cache.get(sizeBytes)+ if (cached !== undefined) return cached+ const encoded = bytes.toString('base64')+ cache.set(sizeBytes, encoded)+ return encoded+}++/** Canonical base64 of a contract-framed PNG of exactly `sizeBytes`. */+function pngBase64(sizeBytes: number): string {+ return cachedBase64(pngBase64Cache, declaredHeaderPngOfSize(sizeBytes), sizeBytes)+}++/** Canonical base64 of a contract-framed JPEG of exactly `sizeBytes`. */+function jpegBase64(sizeBytes: number): string {+ return cachedBase64(jpegBase64Cache, jpegOfSize(sizeBytes), sizeBytes)+}++/**+ * Flip a spare bit inside the final padded sextet. The payload still decodes to+ * the same length, but it is no longer the canonical encoding of those bytes,+ * so nothing may be credited for it.+ */+function withNonCanonicalTailBits(base64: string): string {+ const padding = base64.endsWith('==') ? 2 : base64.endsWith('=') ? 1 : 0+ if (padding === 0) throw new Error('fixture expects a padded base64 payload')+ const lastIndex = base64.length - 1 - padding+ const value = BASE64_ALPHABET.indexOf(base64[lastIndex]!)+ if (value < 0) throw new Error('fixture expects a base64 alphabet character')+ const mutated = BASE64_ALPHABET[value | 1]!+ return base64.slice(0, lastIndex) + mutated + base64.slice(lastIndex + 1)+}++function imageAttachment(+ id: string,+ sizeBytes: number,+ mimeType: 'image/png' | 'image/jpeg' = 'image/png'+) {+ const isJpeg = mimeType === 'image/jpeg'+ return {+ id,+ kind: 'image',+ mimeType,+ encoding: 'base64',+ dataBase64: isJpeg ? jpegBase64(sizeBytes) : pngBase64(sizeBytes),+ filename: `${id}.${isJpeg ? 'jpg' : 'png'}`,+ }+}++async function post(path: string, body: string): Promise<Response> {+ return fetch(`${baseUrl}${path}`, {+ method: 'POST',- headers: { 'content-type': 'application/json' },++ headers: { 'content-type': 'application/json', authorization: 'Bearer token' },+ body,+ })+}++const MESSAGE_PATH = '/api/v1/rpc/hosts/chatllm/messages'++async function postMessage(payload: unknown): Promise<Response> {+ return post(MESSAGE_PATH, JSON.stringify(payload))+}++describe('rpc-proxy chat message body budget', () => {+ it('carries a 12MiB image (exceptional, above 10MiB) to the auth boundary', async () => {+ const body = JSON.stringify({+ content: 'look',+ attachments: [imageAttachment('a1', 12 * MIB)],+ })+ expect(Buffer.byteLength(body)).toBeGreaterThan(16 * MIB)+ const response = await post(MESSAGE_PATH, body)- expect(response.status).toBe(401)++ expect(response.status).toBe(200)+ })++ it('carries a 10MiB image (the former per-image limit) to the auth boundary', async () => {+ const body = JSON.stringify({+ content: 'look',+ attachments: [imageAttachment('a1', 10 * MIB)],+ })+ // 10MiB of bytes encodes to more than 13MiB, well past the old 6MiB parser.+ expect(Buffer.byteLength(body)).toBeGreaterThan(13 * MIB)+ const response = await post(MESSAGE_PATH, body)- expect(response.status).toBe(401)++ expect(response.status).toBe(200)+ })++ it('carries a 5MiB JPEG to the auth boundary', async () => {+ const body = JSON.stringify({+ content: 'look',+ attachments: [imageAttachment('a1', 5 * MIB, 'image/jpeg')],+ })+ expect(Buffer.byteLength(body)).toBeGreaterThan(6 * MIB)+ const response = await post(MESSAGE_PATH, body)- expect(response.status).toBe(401)++ expect(response.status).toBe(200)+ })++ it('carries 10MiB PNG + 5MiB JPEG to the auth boundary', async () => {+ const body = JSON.stringify({+ content: 'look',+ attachments: [imageAttachment('a1', 10 * MIB), imageAttachment('a2', 5 * MIB, 'image/jpeg')],+ })+ expect(Buffer.byteLength(body)).toBeGreaterThan(20 * MIB)+ const response = await post(MESSAGE_PATH, body)- expect(response.status).toBe(401)++ expect(response.status).toBe(200)+ })++ it('carries 10MiB + 5MiB (the 15MiB total limit) to the auth boundary', async () => {+ const body = JSON.stringify({+ content: 'look',+ attachments: [imageAttachment('a1', 10 * MIB), imageAttachment('a2', 5 * MIB)],+ })+ expect(Buffer.byteLength(body)).toBeGreaterThan(20 * MIB)+ const response = await post(MESSAGE_PATH, body)- expect(response.status).toBe(401)++ expect(response.status).toBe(200)+ })++ it('carries three 5MiB images to the auth boundary', async () => {+ const body = JSON.stringify({+ content: 'look',+ attachments: [+ imageAttachment('a1', 5 * MIB),+ imageAttachment('a2', 5 * MIB),+ imageAttachment('a3', 5 * MIB),+ ],+ })+ expect(Buffer.byteLength(body)).toBeGreaterThan(20 * MIB)+ const response = await post(MESSAGE_PATH, body)- expect(response.status).toBe(401)++ expect(response.status).toBe(200)+ })++ it('credits twenty small images instead of charging them as text', async () => {+ const body = JSON.stringify({+ content: 'look',+ attachments: Array.from({ length: 20 }, (_, index) =>+ imageAttachment(`a${index + 1}`, 64 * 1024)+ ),+ })+ expect(Buffer.byteLength(body)).toBeLessThan(6 * MIB)+ const response = await post(MESSAGE_PATH, body)- expect(response.status).toBe(401)++ expect(response.status).toBe(200)+ })++ it('rejects a 21st qualifying image instead of charging it as text', async () => {+ const body = JSON.stringify({+ content: 'look',+ attachments: Array.from({ length: 21 }, (_, index) =>+ imageAttachment(`a${index + 1}`, 64 * 1024)+ ),+ })+ expect(Buffer.byteLength(body)).toBeLessThan(6 * MIB)+ const response = await post(MESSAGE_PATH, body)+ expect(response.status).toBe(413)+ })++ it('rejects a single image over the 16MiB per-image limit', async () => {+ // Fits the 24MiB body ceiling, so this rejection is the budget gate, not the+ // parser: an uncredited 17MiB image is charged to the 6MiB non-image budget.+ const body = JSON.stringify({+ content: 'look',+ attachments: [imageAttachment('a1', 17 * MIB)],+ })+ expect(Buffer.byteLength(body)).toBeLessThan(24 * MIB)+ const response = await post(MESSAGE_PATH, body)+ expect(response.status).toBe(413)+ })++ it('rejects two 10MiB images because together they exceed the 15MiB total', async () => {+ const body = JSON.stringify({+ content: 'look',+ attachments: [imageAttachment('a1', 10 * MIB), imageAttachment('a2', 10 * MIB)],+ })+ const response = await post(MESSAGE_PATH, body)+ expect(response.status).toBe(413)+ })++ it('carries two 8MiB images that sit on the 16MiB total', async () => {+ const body = JSON.stringify({+ content: 'look',+ attachments: [imageAttachment('a1', 8 * MIB), imageAttachment('a2', 8 * MIB)],+ })+ expect(Buffer.byteLength(body)).toBeLessThan(24 * MIB)+ const response = await post(MESSAGE_PATH, body)- expect(response.status).toBe(401)++ expect(response.status).toBe(200)+ })++ it('rejects 9MiB + 8MiB on the 16MiB total alone, under the 24MiB ceiling', async () => {+ const body = JSON.stringify({+ content: 'look',+ attachments: [imageAttachment('a1', 9 * MIB), imageAttachment('a2', 8 * MIB)],+ })+ expect(Buffer.byteLength(body)).toBeLessThan(24 * MIB)+ const response = await post(MESSAGE_PATH, body)+ expect(response.status).toBe(413)+ })++ it('still rejects text-only content over the 6MiB non-image budget', async () => {+ const response = await postMessage({ content: 'x'.repeat(7 * MIB), attachments: [] })+ expect(response.status).toBe(413)+ })++ it('rejects a body past the 24MiB ceiling', async () => {+ const response = await postMessage({ content: 'x'.repeat(25 * MIB), attachments: [] })+ expect(response.status).toBe(413)+ })++ it('does not credit an attachment that only claims to be an image', async () => {+ // Same byte count as the accepted single-image case, but the payload is not+ // a real PNG, so it must be charged to the non-image budget.+ const body = JSON.stringify({+ content: 'look',+ attachments: [+ {+ id: 'a1',+ kind: 'image',+ mimeType: 'image/png',+ encoding: 'base64',+ dataBase64: Buffer.alloc(5 * MIB, 0x41).toString('base64'),+ },+ ],+ })+ const response = await post(MESSAGE_PATH, body)+ expect(response.status).toBe(413)+ })++ it('does not credit base64 whose padding carries non-zero unused bits', async () => {+ const canonical = pngBase64(5 * MIB)+ const nonCanonical = withNonCanonicalTailBits(canonical)+ // Sanity: permissive decoding still yields the same bytes, so the only+ // thing that can reject this payload is the canonical-encoding rule.+ expect(Buffer.from(nonCanonical, 'base64').length).toBe(Buffer.from(canonical, 'base64').length)+ // Without the rule this body would be accepted (base64 ~7MiB minus the+ // credited image leaves ~1MiB of content), so 413 proves no credit.+ const response = await postMessage({+ content: 'x'.repeat(MIB),+ attachments: [+ {+ id: 'a1',+ kind: 'image',+ mimeType: 'image/png',+ encoding: 'base64',+ dataBase64: nonCanonical,+ },+ ],+ })+ expect(response.status).toBe(413)+ })+- it('keeps the 6MiB parser on every other POST route', async () => {- const response = await post(- '/api/v1/rpc/hosts/chatllm/wake',- JSON.stringify({ a: 'x'.repeat(7 * MIB) })- )++ it('keeps the 10mb parser on authenticated non-chat JSON routes', async () => {++ const response = await post('/api/v1/rpc/demo', JSON.stringify({ a: 'x'.repeat(11 * MIB) }))+ expect(response.status).toBe(413)+ })++ it('leaves the sandbox-ui view proxy parser-free so its stream is not drained', async () => {+ // A JSON parser on this path would answer 413 for any body over its limit,+ // because parsing has to consume the stream http-proxy needs to forward.+ // The route answers its cookie check first, so the observable proof is a+ // non-413 outcome while the request body is still unread.+ const abortController = new AbortController()+ const chunk = new Uint8Array(64 * 1024).fill(0x41)+ let sent = 0+ const body = new ReadableStream<Uint8Array>({+ pull(controller) {+ if (sent >= 8 * MIB) return // hold the request open; never finish it+ sent += chunk.length+ controller.enqueue(chunk)+ },+ })+ try {+ const response = await fetch(`${baseUrl}/api/v1/sandbox-ui/ns/recipe/view/index.html`, {+ method: 'POST',+ headers: { 'content-type': 'text/plain' },+ body,+ duplex: 'half',+ signal: abortController.signal,+ } as RequestInit & { duplex: 'half' })+ expect(response.status).not.toBe(413)+ expect(response.status).toBe(401)+ } finally {+ abortController.abort()+ }+ })+})diff --cc rpc-proxy/src/app.tsindex 2520c5a24,161be87bc..4c33fbc66--- a/rpc-proxy/src/app.ts+++ b/rpc-proxy/src/app.ts@@@ -11,139 -11,6 +11,20 @@@ import { createRpcHostStatusStreamRouteimport { createSandboxUiSessionRouter } from './routes/sandboxUi.js'import { isUpstreamTimeoutError } from './services/wakeAndHold.js'- /**- * Body budgets for chat payloads carrying base64 image attachments.- *- * Usual product target is 5MiB / 9MiB / 14MiB at 2048 px. Hard hop credit is- * 16MiB per image and 16MiB total so a poorly compressed 2048 PNG may exceed- * 10MiB. mcp-host holds its own copy: the two services share no package, so- * both edits must land together.- *- * Two ceilings are enforced here:- * - MAX_CHAT_BODY_BYTES stays 24MiB so 16MiB decoded (~21.3MiB base64) plus- * the 1MiB non-image share still fits.- * - MAX_NON_IMAGE_BODY_BYTES bounds the same body MINUS credited image- * base64. Without that subtraction the attachment budget would become a- * general 24MiB text budget.- */- const MAX_CHAT_BODY_BYTES = 24 * 1024 * 1024- const MAX_NON_IMAGE_BODY_BYTES = 6 * 1024 * 1024- const MAX_CHAT_IMAGES = 20- const MAX_IMAGE_DECODED_BYTES = 16 * 1024 * 1024- const MAX_IMAGE_DECODED_BYTES_TOTAL = 16 * 1024 * 1024- const BASE64_RE = /^[A-Za-z0-9+/]+={0,2}$/- const PNG_SIGNATURE = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])- const JPEG_SIGNATURE = Buffer.from([0xff, 0xd8, 0xff])-- /** Base64 alphabet value of one character, or null when it is not a base64 char. */- function base64SextetValue(char: string): number | null {- const code = char.charCodeAt(0)- if (code >= 0x41 && code <= 0x5a) return code - 0x41- if (code >= 0x61 && code <= 0x7a) return code - 0x61 + 26- if (code >= 0x30 && code <= 0x39) return code - 0x30 + 52- if (code === 0x2b) return 62- if (code === 0x2f) return 63- return null- }-- /**- * Decoded byte length of a base64 payload, or null when it is not canonical.- *- * '=' padding makes the final group carry fewer bits than it encodes, so the- * unused low bits of that group's last sextet must be zero (RFC 4648 §3.5). A- * payload padding a non-zero tail still decodes in a permissive decoder, but it- * is not the canonical encoding of its bytes: accepting it would let a caller- * park arbitrary unused bytes inside the base64 length that the body budget is- * asked to credit.- */- function decodedBase64Bytes(dataBase64: string): number | null {- if (dataBase64.length === 0 || dataBase64.length % 4 !== 0) return null- if (!BASE64_RE.test(dataBase64)) return null- const padding = dataBase64.endsWith('==') ? 2 : dataBase64.endsWith('=') ? 1 : 0- if (padding > 0) {- const lastSextet = base64SextetValue(dataBase64[dataBase64.length - 1 - padding] ?? '')- if (lastSextet === null) return null- if (padding === 1 && (lastSextet & 0b11) !== 0) return null- if (padding === 2 && (lastSextet & 0b1111) !== 0) return null- }- // A 4-char group decodes to 3 bytes, the pad chars each drop one byte.- return (dataBase64.length / 4) * 3 - padding- }-- /**- * Byte length of the base64 that counts against the documented image budget.- * Only attachments with the exact wire shape the composer produces qualify:- * `kind: 'image'`, `encoding: 'base64'`, a PNG/JPEG MIME type, canonical- * base64 whose leading bytes are that image's signature, and at most 16MiB- * decoded each within a 20-image / 16MiB total budget. A 21st qualifying- * image is fail-loud rather than charged as text. Anything else is charged- * to the non-image budget, so a claim cannot be smuggled through by mislabelling- * a payload.- */- function inspectChatImageBudget(body: unknown): {- creditedBase64: number- tooManyImages: boolean- } {- if (!body || typeof body !== 'object' || Array.isArray(body)) {- return { creditedBase64: 0, tooManyImages: false }- }- const attachments = (body as { attachments?: unknown }).attachments- if (!Array.isArray(attachments)) return { creditedBase64: 0, tooManyImages: false }-- let credited = 0- let decodedTotal = 0- let counted = 0- for (const attachment of attachments) {- if (!attachment || typeof attachment !== 'object') continue- const candidate = attachment as {- kind?: unknown- encoding?: unknown- mimeType?: unknown- dataBase64?: unknown- }- if (candidate.kind !== 'image' || candidate.encoding !== 'base64') continue- const mimeType = typeof candidate.mimeType === 'string' ? candidate.mimeType : ''- if (mimeType !== 'image/png' && mimeType !== 'image/jpeg') continue- const dataBase64 = typeof candidate.dataBase64 === 'string' ? candidate.dataBase64 : ''- const decoded = decodedBase64Bytes(dataBase64)- if (decoded === null || decoded <= 0 || decoded > MAX_IMAGE_DECODED_BYTES) continue- if (decodedTotal + decoded > MAX_IMAGE_DECODED_BYTES_TOTAL) continue- const signature = Buffer.from(dataBase64.slice(0, 16), 'base64')- const matchesSignature =- mimeType === 'image/png'- ? signature.subarray(0, PNG_SIGNATURE.length).equals(PNG_SIGNATURE)- : signature.subarray(0, JPEG_SIGNATURE.length).equals(JPEG_SIGNATURE)- if (!matchesSignature) continue- if (counted >= MAX_CHAT_IMAGES) {- return { creditedBase64: credited, tooManyImages: true }- }- credited += dataBase64.length- decodedTotal += decoded- counted += 1- }- return { creditedBase64: credited, tooManyImages: false }- }-- function chatBodyExceedsNonImageBudget(rawBodyBytes: number, body: unknown): boolean {- const budget = inspectChatImageBudget(body)- if (budget.tooManyImages) return true- return rawBodyBytes - budget.creditedBase64 > MAX_NON_IMAGE_BODY_BYTES- }-+/**+ * body-parser flags a request whose declared/streamed body crossed the parser's+ * `limit` with `type: 'entity.too.large'`. The terminal error handler below+ * would otherwise report it as a 500, which reads like a server fault for what+ * is a client-sized payload; mcp-host answers the same condition with 413.+ */+function isEntityTooLargeError(error: unknown): boolean {+ return (+ typeof error === 'object' &&+ error !== null &&+ (error as { type?: unknown }).type === 'entity.too.large'+ )+}+export function createApp() {const app = express()diff --cc rpc-proxy/src/middleware/chatJsonBody.tsindex 000000000,000000000..dfb2b2e70new file mode 100644--- /dev/null+++ b/rpc-proxy/src/middleware/chatJsonBody.ts@@@ -1,0 -1,0 +1,147 @@@++import express, { type NextFunction, type Request, type Response } from 'express'++++/**++ * Body budgets for chat payloads carrying base64 image attachments.++ *++ * Usual product target is 5MiB / 9MiB / 14MiB at 2048 px. Hard hop credit is++ * 16MiB per image and 16MiB total so a poorly compressed 2048 PNG may exceed++ * 10MiB. mcp-host holds its own copy: the two services share no package, so++ * both edits must land together.++ *++ * Two ceilings are enforced here:++ * - MAX_CHAT_BODY_BYTES stays 24MiB so 16MiB decoded (~21.3MiB base64) plus++ * the 1MiB non-image share still fits.++ * - MAX_NON_IMAGE_BODY_BYTES bounds the same body MINUS credited image++ * base64. Without that subtraction the attachment budget would become a++ * general 24MiB text budget.++ *++ * Mount `chatJsonBody` on `POST /rpc/hosts/:hostRef/messages` right after++ * authentication so unauthenticated callers never trigger this parser.++ */++const MAX_CHAT_BODY_BYTES = 24 * 1024 * 1024++const MAX_NON_IMAGE_BODY_BYTES = 6 * 1024 * 1024++const MAX_CHAT_IMAGES = 20++const MAX_IMAGE_DECODED_BYTES = 16 * 1024 * 1024++const MAX_IMAGE_DECODED_BYTES_TOTAL = 16 * 1024 * 1024++const BASE64_RE = /^[A-Za-z0-9+/]+={0,2}$/++const PNG_SIGNATURE = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])++const JPEG_SIGNATURE = Buffer.from([0xff, 0xd8, 0xff])++++type ChatRequest = Request & { rawBodyBytes?: number }++++/** Base64 alphabet value of one character, or null when it is not a base64 char. */++function base64SextetValue(char: string): number | null {++ const code = char.charCodeAt(0)++ if (code >= 0x41 && code <= 0x5a) return code - 0x41++ if (code >= 0x61 && code <= 0x7a) return code - 0x61 + 26++ if (code >= 0x30 && code <= 0x39) return code - 0x30 + 52++ if (code === 0x2b) return 62++ if (code === 0x2f) return 63++ return null++}++++/**++ * Decoded byte length of a base64 payload, or null when it is not canonical.++ *++ * '=' padding makes the final group carry fewer bits than it encodes, so the++ * unused low bits of that group's last sextet must be zero (RFC 4648 §3.5). A++ * payload padding a non-zero tail still decodes in a permissive decoder, but it++ * is not the canonical encoding of its bytes: accepting it would let a caller++ * park arbitrary unused bytes inside the base64 length that the body budget is++ * asked to credit.++ */++function decodedBase64Bytes(dataBase64: string): number | null {++ if (dataBase64.length === 0 || dataBase64.length % 4 !== 0) return null++ if (!BASE64_RE.test(dataBase64)) return null++ const padding = dataBase64.endsWith('==') ? 2 : dataBase64.endsWith('=') ? 1 : 0++ if (padding > 0) {++ const lastSextet = base64SextetValue(dataBase64[dataBase64.length - 1 - padding] ?? '')++ if (lastSextet === null) return null++ if (padding === 1 && (lastSextet & 0b11) !== 0) return null++ if (padding === 2 && (lastSextet & 0b1111) !== 0) return null++ }++ // A 4-char group decodes to 3 bytes, the pad chars each drop one byte.++ return (dataBase64.length / 4) * 3 - padding++}++++/**++ * Byte length of the base64 that counts against the documented image budget.++ * Only attachments with the exact wire shape the composer produces qualify:++ * `kind: 'image'`, `encoding: 'base64'`, a PNG/JPEG MIME type, canonical++ * base64 whose leading bytes are that image's signature, and at most 16MiB++ * decoded each within a 20-image / 16MiB total budget. A 21st qualifying++ * image is fail-loud rather than charged as text. Anything else is charged++ * to the non-image budget, so a claim cannot be smuggled through by mislabelling++ * a payload.++ */++function inspectChatImageBudget(body: unknown): {++ creditedBase64: number++ tooManyImages: boolean++} {++ if (!body || typeof body !== 'object' || Array.isArray(body)) {++ return { creditedBase64: 0, tooManyImages: false }++ }++ const attachments = (body as { attachments?: unknown }).attachments++ if (!Array.isArray(attachments)) return { creditedBase64: 0, tooManyImages: false }++++ let credited = 0++ let decodedTotal = 0++ let counted = 0++ for (const attachment of attachments) {++ if (!attachment || typeof attachment !== 'object') continue++ const candidate = attachment as {++ kind?: unknown++ encoding?: unknown++ mimeType?: unknown++ dataBase64?: unknown++ }++ if (candidate.kind !== 'image' || candidate.encoding !== 'base64') continue++ const mimeType = typeof candidate.mimeType === 'string' ? candidate.mimeType : ''++ if (mimeType !== 'image/png' && mimeType !== 'image/jpeg') continue++ const dataBase64 = typeof candidate.dataBase64 === 'string' ? candidate.dataBase64 : ''++ const decoded = decodedBase64Bytes(dataBase64)++ if (decoded === null || decoded <= 0 || decoded > MAX_IMAGE_DECODED_BYTES) continue++ if (decodedTotal + decoded > MAX_IMAGE_DECODED_BYTES_TOTAL) continue++ const signature = Buffer.from(dataBase64.slice(0, 16), 'base64')++ const matchesSignature =++ mimeType === 'image/png'++ ? signature.subarray(0, PNG_SIGNATURE.length).equals(PNG_SIGNATURE)++ : signature.subarray(0, JPEG_SIGNATURE.length).equals(JPEG_SIGNATURE)++ if (!matchesSignature) continue++ if (counted >= MAX_CHAT_IMAGES) {++ return { creditedBase64: credited, tooManyImages: true }++ }++ credited += dataBase64.length++ decodedTotal += decoded++ counted += 1++ }++ return { creditedBase64: credited, tooManyImages: false }++}++++function chatBodyExceedsNonImageBudget(rawBodyBytes: number, body: unknown): boolean {++ const budget = inspectChatImageBudget(body)++ if (budget.tooManyImages) return true++ return rawBodyBytes - budget.creditedBase64 > MAX_NON_IMAGE_BODY_BYTES++}++++const chatJsonParser = express.json({++ limit: MAX_CHAT_BODY_BYTES,++ verify: (req, _res, buffer) => {++ ;(req as ChatRequest).rawBodyBytes = buffer.length++ },++})++++export function chatJsonBody(req: Request, res: Response, next: NextFunction): void {++ chatJsonParser(req, res, error => {++ if (error) {++ next(error)++ return++ }++ const rawBodyBytes = (req as ChatRequest).rawBodyBytes++ if (rawBodyBytes !== undefined && chatBodyExceedsNonImageBudget(rawBodyBytes, req.body)) {++ res.status(413).json({ error: 'Payload Too Large' })++ return++ }++ next()++ })++}diff --cc rpc-proxy/src/middleware/jsonBody.tsindex 000000000,3e05a2460..a74059a5fmode 000000,100644..100644--- a/rpc-proxy/src/middleware/jsonBody.ts+++ b/rpc-proxy/src/middleware/jsonBody.ts@@@ -1,0 -1,10 +1,9 @@@+ import express from 'express'+-// The single request body limit for every JSON route: up to 8 MB of base64-// images from the Desktop composer (COMPOSER_MAX_TOTAL_IMAGE_BASE64_BYTES)-// plus the message text and JSON envelope. Mount `jsonBody` on each route-// right after its authentication middleware so unauthenticated requests are-// never parsed.++// Default JSON ceiling for authenticated non-chat routes. Chat messages that++// carry inline images use `chatJsonBody` (24 MiB envelope + hop credit) instead.++// Mount either parser on each route right after its authentication middleware++// so unauthenticated requests are never parsed.+ export const JSON_BODY_LIMIT = '10mb'++ export const jsonBody = express.json({ limit: JSON_BODY_LIMIT })diff --cc rpc-proxy/src/routes/rpc.tsindex 7a9474a4b,3fc25914c..b7027712b--- a/rpc-proxy/src/routes/rpc.ts+++ b/rpc-proxy/src/routes/rpc.ts@@@ -8,6 -8,7 +8,8 @@@ importrequireRpcAuth,requireScope,} from '../middleware/auth.js'++import { chatJsonBody } from '../middleware/chatJsonBody.js'+ import { jsonBody } from '../middleware/jsonBody.js'import { rpcInvocationContext } from '../rpcAccessContext.js'import {ControlApiConnectorsRejectedError,@@@ -309,6 -311,7 +312,7 @@@ export function createRpcRouter(): Rout'/rpc/hosts/:hostRef/messages',requireRpcAuth,requireScope('host:message:invoke'),- jsonBody,++ chatJsonBody,async (req: AuthedRequest, res) => {// Host runtime write path (REST-oriented):// - separate from read-only status streamdiff --cc tests/e2e/fixtures/package-lock.jsonindex 17b402c80,a2488f81a..27ae31092--- a/tests/e2e/fixtures/package-lock.json+++ b/tests/e2e/fixtures/package-lock.json@@@ -1,12 -1,12 +1,12 @@@{"name": "@clerum/e2e-test-fixtures",- "version": "1.0.86",- "version": "1.0.84",++ "version": "1.0.87","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "@clerum/e2e-test-fixtures",- "version": "1.0.86",- "version": "1.0.84",++ "version": "1.0.87","dependencies": {"@kubernetes/client-node": "^1.0.0"},diff --cc tests/e2e/fixtures/package.jsonindex a38efcff1,449353a44..1835ae0ad--- a/tests/e2e/fixtures/package.json+++ b/tests/e2e/fixtures/package.json@@@ -1,6 -1,6 +1,6 @@@{"name": "@clerum/e2e-test-fixtures",- "version": "1.0.86",- "version": "1.0.84",++ "version": "1.0.87","description": "E2E test fixtures and utilities for Clerum testing","type": "module","main": "index.ts",