Skip to content

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

TypeScript

fix(codex): stop the Host from dropping inbound images at 3

Alfredo Lopezec10139c5630
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index ced3fe0d6..cb1f6606a 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.351",
+ "version": "0.1.352",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.351",
+ "version": "0.1.352",
"license": "SEE LICENSE IN LICENSE",
"dependencies": {
"@anthropic-ai/sdk": "^0.32.0",
diff --git a/mcp-host/package.json b/mcp-host/package.json
index 99b115901..192838156 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.351",
+ "version": "0.1.352",
"description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude",
"main": "dist/main.js",
"engines": {
diff --git a/mcp-host/src/__tests__/incomingImageAttachments.test.ts b/mcp-host/src/__tests__/incomingImageAttachments.test.ts
new file mode 100644
index 000000000..489062430
--- /dev/null
+++ b/mcp-host/src/__tests__/incomingImageAttachments.test.ts
@@ -0,0 +1,63 @@
+import { describe, expect, it, vi } from 'vitest'
+import { VISUAL_LIMITS } from '@clerum/llm-provider-attempt-contract'
+import type { Attachment } from '../core/types'
+import { sanitizeIncomingAttachments } from '../incomingImageAttachments'
+import { JPEG_2X2_BASE64, PNG_2X2_BASE64 } from '../llm/__tests__/codexImageFixtures'
+
+const MAX_BYTES = 52_428_800
+
+function imageAttachment(id: string, mimeType: 'image/png' | 'image/jpeg'): Attachment {
+ return {
+ id,
+ kind: 'image',
+ mimeType,
+ encoding: 'base64',
+ dataBase64: mimeType === 'image/png' ? PNG_2X2_BASE64 : JPEG_2X2_BASE64,
+ }
+}
+
+describe('sanitizeIncomingAttachments', () => {
+ it('keeps five composer images even when the outbound response cap is 3', () => {
+ const incoming = [1, 2, 3, 4, 5].map(index =>
+ imageAttachment(`img-${index}`, index % 2 === 0 ? 'image/jpeg' : 'image/png')
+ )
+ const kept = sanitizeIncomingAttachments(incoming, MAX_BYTES)
+ expect(kept).toHaveLength(5)
+ expect(kept?.map(attachment => attachment.id)).toEqual([
+ 'img-1',
+ 'img-2',
+ 'img-3',
+ 'img-4',
+ 'img-5',
+ ])
+ })
+
+ it('keeps the contract ceiling and drops the rest fail-visible', () => {
+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined)
+ const incoming = Array.from({ length: VISUAL_LIMITS.maxImages + 1 }, (_, index) =>
+ imageAttachment(`img-${index + 1}`, 'image/png')
+ )
+ const kept = sanitizeIncomingAttachments(incoming, MAX_BYTES)
+ expect(kept).toHaveLength(VISUAL_LIMITS.maxImages)
+ expect(warn).toHaveBeenCalledWith(
+ `[Main] Dropping 1 incoming image attachment(s) beyond ${VISUAL_LIMITS.maxImages}`
+ )
+ warn.mockRestore()
+ })
+
+ it('skips non-image attachments instead of occupying an inbound slot', () => {
+ const incoming: Attachment[] = [
+ {
+ id: 'file-1',
+ kind: 'file',
+ mimeType: 'application/pdf',
+ encoding: 'base64',
+ dataBase64: PNG_2X2_BASE64,
+ },
+ imageAttachment('img-1', 'image/png'),
+ ]
+ expect(
+ sanitizeIncomingAttachments(incoming, MAX_BYTES)?.map(attachment => attachment.id)
+ ).toEqual(['img-1'])
+ })
+})
diff --git a/mcp-host/src/incomingImageAttachments.ts b/mcp-host/src/incomingImageAttachments.ts
new file mode 100644
index 000000000..d30bddd7c
--- /dev/null
+++ b/mcp-host/src/incomingImageAttachments.ts
@@ -0,0 +1,53 @@
+/**
+ * Incoming Desktop / rpc-proxy image attachments.
+ *
+ * `CLERUM_ATTACHMENT_MAX_COUNT` is the outbound response delivery cap
+ * (Slack / Telegram / channel-reader). Inbound Codex composer images use the
+ * shared visual contract (`VISUAL_LIMITS.maxImages`) so a live ConfigMap of 3
+ * cannot silently drop the 4th screenshot.
+ */
+import { VISUAL_LIMITS } from '@clerum/llm-provider-attempt-contract'
+import type { Attachment } from './core/types'
+import { approxDecodedBytes } from './shared/encoding'
+
+export function sanitizeIncomingAttachments(
+ raw: Attachment[] | undefined,
+ maxBytes: number
+): Attachment[] | undefined {
+ if (!raw || raw.length === 0) {
+ return undefined
+ }
+
+ const sanitized: Attachment[] = []
+ let droppedOverCap = 0
+ for (const attachment of raw) {
+ if (sanitized.length >= VISUAL_LIMITS.maxImages) {
+ droppedOverCap += 1
+ continue
+ }
+ const isSupportedImageMime =
+ attachment.mimeType === 'image/jpeg' || attachment.mimeType === 'image/png'
+ if (
+ attachment.kind !== 'image' ||
+ !isSupportedImageMime ||
+ attachment.encoding !== 'base64' ||
+ typeof attachment.dataBase64 !== 'string' ||
+ !attachment.dataBase64.trim()
+ ) {
+ continue
+ }
+ const decodedBytes = approxDecodedBytes(attachment.dataBase64)
+ if (decodedBytes > maxBytes) {
+ continue
+ }
+ sanitized.push(attachment)
+ }
+
+ if (droppedOverCap > 0) {
+ console.warn(
+ `[Main] Dropping ${droppedOverCap} incoming image attachment(s) beyond ${VISUAL_LIMITS.maxImages}`
+ )
+ }
+
+ return sanitized.length > 0 ? sanitized : undefined
+}
diff --git a/mcp-host/src/main.ts b/mcp-host/src/main.ts
index e09df9016..12c37e505 100644
--- a/mcp-host/src/main.ts
+++ b/mcp-host/src/main.ts
@@ -55,6 +55,7 @@ import {
resolveGuardrailHookDescriptors,
withResolvedHookDescriptors,
} from './guardrailHookResolver'
+import { sanitizeIncomingAttachments } from './incomingImageAttachments'
import { HostWatcher, LlmHookWatcher, getHost, getLlmHook } from './k8sClient'
import { StatelessHeartbeat } from './lifecycle/statelessHeartbeat'
import { TaskLifecycle } from './lifecycle/taskLifecycle'
@@ -392,37 +393,6 @@ function sanitizeAttachments(raw: Attachment[] | undefined): Attachment[] | unde
return sanitized.length > 0 ? sanitized : undefined
}
-function sanitizeIncomingAttachments(raw: Attachment[] | undefined): Attachment[] | undefined {
- if (!raw || raw.length === 0) {
- return undefined
- }
-
- const sanitized: Attachment[] = []
- for (const attachment of raw) {
- if (sanitized.length >= config.attachmentMaxCount) {
- break
- }
- const isSupportedImageMime =
- attachment.mimeType === 'image/jpeg' || attachment.mimeType === 'image/png'
- if (
- attachment.kind !== 'image' ||
- !isSupportedImageMime ||
- attachment.encoding !== 'base64' ||
- typeof attachment.dataBase64 !== 'string' ||
- !attachment.dataBase64.trim()
- ) {
- continue
- }
- const decodedBytes = approxDecodedBytes(attachment.dataBase64)
- if (decodedBytes > config.attachmentMaxBytes) {
- continue
- }
- sanitized.push(attachment)
- }
-
- return sanitized.length > 0 ? sanitized : undefined
-}
-
/**
* Project the ConfigStore's active-provider credential bag into the `ApiKeys`
* shape. ConfigStore exposes the full multi-slot credential bag (keyed by slot
@@ -1922,7 +1892,10 @@ function handleIncomingMessage(
message: IncomingMessage,
options?: { async?: boolean }
): MessageResponse | Promise<MessageResponse> {
- const sanitizedIncomingAttachments = sanitizeIncomingAttachments(message.attachments)
+ const sanitizedIncomingAttachments = sanitizeIncomingAttachments(
+ message.attachments,
+ config.attachmentMaxBytes
+ )
const normalizedMessage: IncomingMessage = {
...message,
attachments: sanitizedIncomingAttachments,