Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(codex): admit live Codex images after #669 capability gate
Alfredo Lopez43075cfa259a
diff --git a/codex-llm-proxy/src/requestLimits.ts b/codex-llm-proxy/src/requestLimits.tsindex b1d20034e..6295b4baa 100644--- a/codex-llm-proxy/src/requestLimits.ts+++ b/codex-llm-proxy/src/requestLimits.ts@@ -78,6 +78,11 @@ export class StreamGate {this.running = Math.max(0, this.running - 1)}}++ /** Observable occupancy for tests. Production callers must not branch on this. */+ snapshot(): { running: number; queued: number } {+ return { running: this.running, queued: this.queued }+ }}export const streamGate = new StreamGate()diff --git a/codex-llm-proxy/src/server.ts b/codex-llm-proxy/src/server.tsindex 7a5487ed1..5a323f05e 100644--- a/codex-llm-proxy/src/server.ts+++ b/codex-llm-proxy/src/server.ts@@ -76,6 +76,8 @@ export type ProxyRuntimeDeps = {* `assertAllowedUpstreamUrl` is unaffected by this seam.*/lookup?: OriginPolicyOptions['lookup']+ /** Test seam: hang or observe a stream without contacting ChatGPT. */+ streamCompletion?: typeof streamCodexCompletion}export type ProxyServers = {@@ -255,7 +257,8 @@ export function createProxyApps(res.setHeader('content-type', 'text/event-stream')res.setHeader('cache-control', 'no-cache')const started = Date.now()- const result = await streamCodexCompletion({+ const stream = deps.streamCompletion ?? streamCodexCompletion+ const result = await stream({executionTicket: parsed.data.executionTicket,requestHash: parsed.data.requestHash,request: parsed.data.request,diff --git a/codex-llm-proxy/test/requestLimits.test.ts b/codex-llm-proxy/test/requestLimits.test.tsindex ed7ec59fa..483d6e66c 100644--- a/codex-llm-proxy/test/requestLimits.test.ts+++ b/codex-llm-proxy/test/requestLimits.test.ts@@ -1,5 +1,10 @@import { describe, expect, it } from 'vitest'-import { RequestLimitError, StreamGate } from '../src/requestLimits.js'+import {+ RequestLimitError,+ STREAM_LIMITS,+ StreamGate,+ VISUAL_STREAM_LIMITS,+} from '../src/requestLimits.js'describe('StreamGate', () => {it('admits a queued waiter once a running stream releases', async () => {@@ -53,4 +58,24 @@ describe('StreamGate', () => {const releaseNext = await nextreleaseNext()})++ it('pins the visual 2/8 sibling against the ordinary 8/16 stream gate', () => {+ expect(VISUAL_STREAM_LIMITS).toEqual({ maxConcurrentStreams: 2, maxQueuedRequests: 8 })+ expect(STREAM_LIMITS.maxConcurrentStreams).toBe(8)+ expect(STREAM_LIMITS.maxQueuedRequests).toBe(16)+ })++ it('rejects the 11th visual waiter once 2 are running and 8 are queued', async () => {+ const gate = new StreamGate(+ VISUAL_STREAM_LIMITS.maxConcurrentStreams,+ VISUAL_STREAM_LIMITS.maxQueuedRequests+ )+ const held = [await gate.acquire(), await gate.acquire()]+ const queued = Array.from({ length: VISUAL_STREAM_LIMITS.maxQueuedRequests }, () =>+ gate.acquire()+ )+ await expect(gate.acquire()).rejects.toBeInstanceOf(RequestLimitError)+ for (const release of held) release()+ for (const waiter of queued) (await waiter)()+ })})diff --git a/codex-llm-proxy/test/streamGate.handoff.test.ts b/codex-llm-proxy/test/streamGate.handoff.test.tsnew file mode 100644index 000000000..5428811ff--- /dev/null+++ b/codex-llm-proxy/test/streamGate.handoff.test.ts@@ -0,0 +1,215 @@+import { generateKeyPairSync } from 'node:crypto'+import { createServer } from 'node:http'+import jwt from 'jsonwebtoken'+import { afterEach, describe, expect, it } from 'vitest'+import {+ hashCodexCompletionRequest,+ parseCodexCompletionRequest,+} from '@clerum/llm-provider-attempt-contract'+import { type CodexLlmProxyConfig } from '../src/config.js'+import { createProxyApps } from '../src/server.js'+import {+ STREAM_LIMITS,+ VISUAL_STREAM_LIMITS,+ streamGate,+ visualStreamGate,+} from '../src/requestLimits.js'++const { privateKey, publicKey } = generateKeyPairSync('rsa', {+ modulusLength: 2048,+ publicKeyEncoding: { type: 'spki', format: 'pem' },+ privateKeyEncoding: { type: 'pkcs8', format: 'pem' },+})++function config(): CodexLlmProxyConfig {+ return {+ runtimePort: 8080,+ adminPort: 8081,+ probePort: 9090,+ maxBodyBytes: 1_048_576,+ maxVisualBodyBytes: 24 * 1024 * 1024,+ maxStreamDurationMs: 300_000,+ maxDeadlineMs: 300_000,+ jwtIssuer: 'control-api',+ jwtPublicKey: publicKey,+ executionEnabled: true,+ controlApiBaseUrl: '',+ controlApiServiceName: 'codex-llm-proxy',+ controlApiServiceToken: '',+ }+}++function sign(payload: Record<string, unknown>, audience: string): string {+ return jwt.sign(payload, privateKey, {+ algorithm: 'RS256',+ issuer: 'control-api',+ audience,+ expiresIn: 60,+ })+}++function platformToken(): string {+ return sign(+ {+ sub: 'default/research-host',+ hostRefs: ['research-host'],+ workflowControlScopes: ['llm:codex:execute'],+ scope: 'workflow:approval:request',+ },+ 'workflow-approvals'+ )+}++let ticketSeq = 1++function ticket(requestHash: string): string {+ ticketSeq += 1+ return sign(+ {+ jti: `11111111-1111-4111-8111-${String(ticketSeq).padStart(12, '0')}`,+ typ: 'codex-execution-ticket',+ hostRef: 'research-host',+ model: 'gpt-5.1',+ requestHash,+ providerAttemptId: `att-${ticketSeq}`,+ },+ 'codex-llm-proxy'+ )+}++function completionRequest(+ schemaVersion: 'codex-completion-request.v1' | 'codex-completion-request.v2'+) {+ ticketSeq += 1+ return {+ schemaVersion,+ requestId: `req-gate-${ticketSeq}`,+ idempotencyKey: `idem-gate-${ticketSeq}`,+ provider: 'codex-subscription',+ model: 'gpt-5.1',+ messages: [{ role: 'user', content: 'hi' }],+ }+}++function hangStream() {+ let releaseHeld!: () => void+ const held = new Promise<void>(resolve => {+ releaseHeld = resolve+ })+ return {+ release: () => releaseHeld(),+ impl: async () => {+ await held+ return { outcome: 'canceled' as const }+ },+ }+}++async function waitFor(predicate: () => boolean, label: string): Promise<void> {+ const started = Date.now()+ while (!predicate()) {+ if (Date.now() - started > 5_000) throw new Error(label)+ await new Promise(resolve => setTimeout(resolve, 10))+ }+}++function completionPayload(+ schemaVersion: 'codex-completion-request.v1' | 'codex-completion-request.v2'+): { token: string; body: string } {+ const raw = completionRequest(schemaVersion)+ const parsed = parseCodexCompletionRequest(raw)+ if (!parsed.ok) throw new Error(parsed.message)+ const requestHash = hashCodexCompletionRequest(parsed.value)+ return {+ token: platformToken(),+ body: JSON.stringify({+ executionTicket: ticket(requestHash),+ requestHash,+ request: raw,+ }),+ }+}++async function postCompletion(+ port: number,+ schemaVersion: 'codex-completion-request.v1' | 'codex-completion-request.v2'+): Promise<Response> {+ const { token, body } = completionPayload(schemaVersion)+ return fetch(`http://127.0.0.1:${port}/internal/runtime/v1/codex/completions`, {+ method: 'POST',+ headers: {+ Authorization: `Bearer ${token}`,+ 'content-type': 'application/json',+ },+ body,+ })+}++describe('visual stream-gate handoff', () => {+ const hangs: Array<{ release: () => void }> = []+ const serversToClose: Array<{ close: () => Promise<void> }> = []+ const listeners: Array<ReturnType<typeof createServer>> = []++ afterEach(async () => {+ for (const hang of hangs.splice(0)) hang.release()+ for (const server of serversToClose.splice(0)) await server.close()+ await Promise.all(+ listeners.splice(0).map(+ listener =>+ new Promise<void>((resolve, reject) =>+ listener.close(err => (err ? reject(err) : resolve()))+ )+ )+ )+ await waitFor(+ () =>+ visualStreamGate.snapshot().running === 0 &&+ visualStreamGate.snapshot().queued === 0 &&+ streamGate.snapshot().running === 0 &&+ streamGate.snapshot().queued === 0,+ 'gates did not drain after release'+ )+ })++ it('releases the visual slot for V1 and keeps it for V2; a saturated visual gate is 503', async () => {+ expect(VISUAL_STREAM_LIMITS.maxConcurrentStreams).toBe(2)+ expect(STREAM_LIMITS.maxConcurrentStreams).toBe(8)++ const hang = hangStream()+ hangs.push(hang)+ const servers = createProxyApps(config(), { streamCompletion: hang.impl })+ serversToClose.push(servers)+ const listener = createServer(servers.runtimeApp).listen(0)+ listeners.push(listener)+ const address = listener.address()+ if (!address || typeof address === 'string') throw new Error('listener has no port')+ const port = address.port++ const v1 = postCompletion(port, 'codex-completion-request.v1')+ await waitFor(+ () => streamGate.snapshot().running === 1 && visualStreamGate.snapshot().running === 0,+ 'V1 did not release the visual gate before taking streamGate'+ )++ const firstVisual = postCompletion(port, 'codex-completion-request.v2')+ const secondVisual = postCompletion(port, 'codex-completion-request.v2')+ await waitFor(+ () => visualStreamGate.snapshot().running === 2 && streamGate.snapshot().running === 1,+ 'V2 did not keep the visual gate through the stream'+ )++ const queued = Array.from({ length: VISUAL_STREAM_LIMITS.maxQueuedRequests }, () =>+ postCompletion(port, 'codex-completion-request.v2')+ )+ await waitFor(+ () => visualStreamGate.snapshot().queued === VISUAL_STREAM_LIMITS.maxQueuedRequests,+ 'visual queue did not fill to 8'+ )+ const overflow = await postCompletion(port, 'codex-completion-request.v2')+ expect(overflow.status).toBe(503)+ expect(await overflow.json()).toEqual({ error: 'provider_unavailable' })++ hang.release()+ await Promise.all([v1, firstVisual, secondVisual, ...queued])+ })+})diff --git a/control-api/package-lock.json b/control-api/package-lock.jsonindex 9837b354e..5d2665b68 100644--- a/control-api/package-lock.json+++ b/control-api/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-control-api",- "version": "0.1.586",+ "version": "0.1.587","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-control-api",- "version": "0.1.586",+ "version": "0.1.587","dependencies": {"@clerum/display-field": "file:../packages/display-field","@clerum/grok-provider-attempt-contract": "file:../packages/grok-provider-attempt-contract",diff --git a/control-api/package.json b/control-api/package.jsonindex ddcea757c..9ac84821c 100644--- a/control-api/package.json+++ b/control-api/package.json@@ -1,6 +1,6 @@{"name": "clerum-control-api",- "version": "0.1.586",+ "version": "0.1.587","description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets","main": "dist/main.js","engines": {diff --git a/control-api/src/app.ts b/control-api/src/app.tsindex 400efa88e..018e37b80 100644--- a/control-api/src/app.ts+++ b/control-api/src/app.ts@@ -66,6 +66,9 @@ const RPC_HOST_ACCESS_PATH = /^\/api\/v1\/rpc\/access\/users\/[^/]+\/mcp-hosts\/// byte or buffer the whole part in the control plane.const GFS_UPLOAD_PART_PATH =/^\/api\/v1\/(?:gfs\/proxy\/v1|external\/gfs)\/uploads\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\/parts\/[0-9]+$/i+// Authorize parses its own 24 MiB JSON after the cheap limiter and JWT.+// The global 150mb parser must not buffer an unauthenticated body.+const LLM_AUTHORIZE_PATH = /^\/api\/v1\/mcp-host\/llm\/provider-attempts\/authorize\/?$/iexport function createApp(gateway: K8sGateway) {const traceIngestDb: DbClient = meterTracingDbClient({@@ -88,6 +91,10 @@ export function createApp(gateway: K8sGateway) {next()return}+ if (req.method === 'POST' && LLM_AUTHORIZE_PATH.test(req.path)) {+ next()+ return+ }jsonBodyParser(req, res, next)})diff --git a/control-api/src/routes/mcp-host/llmProviderAttempts.routes.ts b/control-api/src/routes/mcp-host/llmProviderAttempts.routes.tsindex e61ba2965..60f93168d 100644--- a/control-api/src/routes/mcp-host/llmProviderAttempts.routes.ts+++ b/control-api/src/routes/mcp-host/llmProviderAttempts.routes.ts@@ -1,4 +1,5 @@-import { type Request, type Response, Router } from 'express'+import express, { type NextFunction, type Request, type Response, Router } from 'express'+import { LIMITS } from '@clerum/llm-provider-attempt-contract'import { config } from '../../config.js'import { asyncHandler } from '../../http/asyncHandler.js'import type { K8sGateway } from '../../k8s.js'@@ -113,8 +114,9 @@ export function createMcpHostLlmProviderAttemptRoutes(gateway: K8sGateway): Routconst router = Router()router.post('/mcp-host/llm/provider-attempts/authorize',- requireMcpHostJwt,...llmProviderAttemptAuthorizeRateLimits(),+ requireMcpHostJwt,+ express.json({ limit: LIMITS.maxVisualRequestBodyBytes }),asyncHandler(async (req: Request, res: Response) => {const claims = req.mcpHostJwtif (!claims) {@@ -131,5 +133,13 @@ export function createMcpHostLlmProviderAttemptRoutes(gateway: K8sGateway): Rout}}))+ router.use((err: unknown, _req: Request, res: Response, next: NextFunction) => {+ const typed = err as { type?: string; status?: number }+ if (typed.type === 'entity.too.large' || typed.status === 413) {+ res.status(413).json({ error: 'payload_too_large' })+ return+ }+ next(err)+ })return router}diff --git a/control-api/src/services/llmProviderAttemptAuthorizer.ts b/control-api/src/services/llmProviderAttemptAuthorizer.tsindex dcf210cbe..87b00bab7 100644--- a/control-api/src/services/llmProviderAttemptAuthorizer.ts+++ b/control-api/src/services/llmProviderAttemptAuthorizer.ts@@ -636,12 +636,9 @@ export async function authorizeLlmProviderAttempt(const parsed = parseCodexCompletionRequest(body.request)if (!parsed.ok) {- // Size ceilings are 413. Nesting-depth `limit` stays 400 so a structural- // overflow is not reported as a payload-too-large image failure.+ // Only byte-size `limit` failures are 413. Range, count and depth stay 400.throw new LlmProviderAttemptAuthorizeError(- parsed.code === 'limit' && !/nesting depth/i.test(parsed.message)- ? 'payload_too_large'- : 'invalid_request',+ parsed.code === 'limit' && parsed.kind === 'size' ? 'payload_too_large' : 'invalid_request',parsed.message)}@@ -988,7 +985,9 @@ export async function authorizeLlmProviderAttempt(})if (!envelope.ok) {throw new LlmProviderAttemptAuthorizeError(- envelope.code === 'limit' ? 'payload_too_large' : 'invalid_request',+ envelope.code === 'limit' && envelope.kind === 'size'+ ? 'payload_too_large'+ : 'invalid_request',envelope.message)}diff --git a/control-api/test/routes.mcp-host.llmProviderAttempts.test.ts b/control-api/test/routes.mcp-host.llmProviderAttempts.test.tsindex 40ce0887f..10a41e5bc 100644--- a/control-api/test/routes.mcp-host.llmProviderAttempts.test.ts+++ b/control-api/test/routes.mcp-host.llmProviderAttempts.test.ts@@ -1,5 +1,6 @@import { beforeEach, describe, expect, it, vi } from 'vitest'import express from 'express'+import { createServer } from 'node:http'import request from 'supertest'import {createMcpHostLlmProviderAttemptRoutes,@@ -42,8 +43,7 @@ const HOST = 'research-host'function buildApp() {const app = express()- // Match the authorize gateway: V2 visual envelopes are 24 MiB, not nginx's 1m default.- app.use(express.json({ limit: '24mb' }))+ // Production skips the global parser on this path; the route owns the 24 MiB JSON.const api = express.Router()api.use(createMcpHostLlmProviderAttemptRoutes({@@ -79,6 +79,57 @@ describe('POST /api/v1/mcp-host/llm/provider-attempts/authorize', () => {expect(authorizer.authorizeLlmProviderAttempt).not.toHaveBeenCalled()})+ it('returns 401 for an unauthenticated 7 MiB body without parsing it as 413', async () => {+ const app = buildApp()+ const listener = createServer(app).listen(0)+ try {+ const address = listener.address()+ if (!address || typeof address === 'string') throw new Error('listener has no port')+ const oversized = await fetch(+ `http://127.0.0.1:${address.port}/api/v1/mcp-host/llm/provider-attempts/authorize`,+ {+ method: 'POST',+ headers: { 'content-type': 'application/json' },+ body: `{"pad":"${'x'.repeat(7 * 1024 * 1024)}"}`,+ }+ )+ expect(oversized.status).toBe(401)+ expect(oversized.status).not.toBe(413)+ expect(authorizer.authorizeLlmProviderAttempt).not.toHaveBeenCalled()+ } finally {+ await new Promise<void>((resolve, reject) =>+ listener.close(err => (err ? reject(err) : resolve()))+ )+ }+ })++ it('returns 413 for an authenticated body over the 24 MiB visual envelope', async () => {+ const app = buildApp()+ const listener = createServer(app).listen(0)+ try {+ const address = listener.address()+ if (!address || typeof address === 'string') throw new Error('listener has no port')+ const oversized = await fetch(+ `http://127.0.0.1:${address.port}/api/v1/mcp-host/llm/provider-attempts/authorize`,+ {+ method: 'POST',+ headers: {+ Authorization: `Bearer ${token()}`,+ 'content-type': 'application/json',+ },+ body: `{"pad":"${'x'.repeat(25 * 1024 * 1024)}"}`,+ }+ )+ expect(oversized.status).toBe(413)+ expect(await oversized.json()).toEqual({ error: 'payload_too_large' })+ expect(authorizer.authorizeLlmProviderAttempt).not.toHaveBeenCalled()+ } finally {+ await new Promise<void>((resolve, reject) =>+ listener.close(err => (err ? reject(err) : resolve()))+ )+ }+ }, 30_000)+it('maps authorizer taxonomy without collapsing it into 500', async () => {const app = buildApp()vi.mocked(authorizer.authorizeLlmProviderAttempt).mockRejectedValueOnce(diff --git a/control-api/test/services.llmProviderAttemptAuthorizer.test.ts b/control-api/test/services.llmProviderAttemptAuthorizer.test.tsindex c8f71a310..6c6846414 100644--- a/control-api/test/services.llmProviderAttemptAuthorizer.test.ts+++ b/control-api/test/services.llmProviderAttemptAuthorizer.test.ts@@ -1,7 +1,11 @@import { beforeEach, describe, expect, it, vi } from 'vitest'import { readFileSync } from 'node:fs'import { createRequire } from 'node:module'-import { LIMITS, hashCodexCompletionRequestV1 } from '@clerum/llm-provider-attempt-contract'+import {+ LIMITS,+ VISUAL_LIMITS,+ hashCodexCompletionRequestV1,+} from '@clerum/llm-provider-attempt-contract'import { streamCodexCompletion } from '../../codex-llm-proxy/src/codexTransport'import {CODEX_CATALOG_ORIGIN,@@ -607,7 +611,8 @@ describe('authorizeLlmProviderAttempt', () => {expect(current.insertAttempt).not.toHaveBeenCalled()})- it('maps a contract parse limit failure to payload_too_large', async () => {+ it('maps a contract size limit to payload_too_large', async () => {+ const oversizeBase64 = 'A'.repeat(4 * Math.ceil(VISUAL_LIMITS.maxImageBytes / 3) + 4)await expect(authorizeLlmProviderAttempt(claims(),@@ -615,13 +620,78 @@ describe('authorizeLlmProviderAttempt', () => {request: {...REQUEST,schemaVersion: 'codex-completion-request.v2',- messages: Array.from({ length: 129 }, () => ({ role: 'user' as const, content: 'x' })),+ messages: [+ {+ role: 'user' as const,+ content: 'look',+ contentParts: [+ { type: 'text' as const, text: 'look' },+ {+ type: 'image' as const,+ mimeType: 'image/png',+ data: oversizeBase64,+ source: {+ kind: 'attachment' as const,+ attachmentId: 'att_1',+ messageId: 'msg-1',+ },+ },+ ],+ },+ ],},}),current)).rejects.toMatchObject({ code: 'payload_too_large' })expect(current.insertAttempt).not.toHaveBeenCalled()+ }, 30_000)++ it('maps a contract count limit to invalid_request, not payload_too_large', async () => {+ await expect(+ authorizeLlmProviderAttempt(+ claims(),+ body({+ request: {+ ...REQUEST,+ schemaVersion: 'codex-completion-request.v2',+ messages: Array.from({ length: 129 }, () => ({ role: 'user' as const, content: 'x' })),+ },+ }),+ current+ )+ ).rejects.toMatchObject({ code: 'invalid_request' })+ expect(current.insertAttempt).not.toHaveBeenCalled()+ })++ it('maps a contract range limit to invalid_request, not payload_too_large', async () => {+ await expect(+ authorizeLlmProviderAttempt(+ claims(),+ body({+ request: {+ ...REQUEST,+ schemaVersion: 'codex-completion-request.v2',+ generation: { maxOutputTokens: 99_999 },+ },+ }),+ current+ )+ ).rejects.toMatchObject({ code: 'invalid_request' })+ await expect(+ authorizeLlmProviderAttempt(+ claims(),+ body({+ request: {+ ...REQUEST,+ schemaVersion: 'codex-completion-request.v2',+ deadlineMs: LIMITS.maxDeadlineMs + 1,+ },+ }),+ current+ )+ ).rejects.toMatchObject({ code: 'invalid_request' })+ expect(current.insertAttempt).not.toHaveBeenCalled()})it('keeps the authorize wrapper on the 1 MiB non-image budget when the nested request is V2', async () => {diff --git a/desktop-app/ui/src/components/agents/__tests__/AnnotationCanvas.test.tsx b/desktop-app/ui/src/components/agents/__tests__/AnnotationCanvas.test.tsxindex f197e2006..a3d0c5f2c 100644--- a/desktop-app/ui/src/components/agents/__tests__/AnnotationCanvas.test.tsx+++ b/desktop-app/ui/src/components/agents/__tests__/AnnotationCanvas.test.tsx@@ -1,5 +1,5 @@// @vitest-environment jsdom-import { afterEach, describe, expect, it, vi } from 'vitest'+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'import type { ComposerImageAttachment } from '../../../uiTypes'import { commitAnnotatedPreview } from '../AnnotationCanvas'@@ -13,6 +13,14 @@ const attachment = (previewDataUrl: string): ComposerImageAttachment => ({})describe('commitAnnotatedPreview', () => {+ beforeEach(() => {+ Object.defineProperty(URL, 'revokeObjectURL', {+ configurable: true,+ writable: true,+ value: vi.fn(),+ })+ })+afterEach(() => {vi.restoreAllMocks()})diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.jsonindex 6f6bda981..1f9ab2509 100644--- a/mcp-host/package-lock.json+++ b/mcp-host/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-mcp-host",- "version": "0.1.358",+ "version": "0.1.359","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-mcp-host",- "version": "0.1.358",+ "version": "0.1.359","license": "SEE LICENSE IN LICENSE","dependencies": {"@anthropic-ai/sdk": "^0.32.0",diff --git a/mcp-host/package.json b/mcp-host/package.jsonindex dc6d15d8a..dfc631db5 100644--- a/mcp-host/package.json+++ b/mcp-host/package.json@@ -1,6 +1,6 @@{"name": "clerum-mcp-host",- "version": "0.1.358",+ "version": "0.1.359","description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude","main": "dist/main.js","engines": {diff --git a/mcp-host/src/agent/__tests__/incomingAdmission.test.ts b/mcp-host/src/agent/__tests__/incomingAdmission.test.tsindex 3e37234ae..f8805b30e 100644--- a/mcp-host/src/agent/__tests__/incomingAdmission.test.ts+++ b/mcp-host/src/agent/__tests__/incomingAdmission.test.ts@@ -151,6 +151,43 @@ describe('#654 incoming admission gate', () => {])})+ it('admits a Codex image when the live catalog has no imageInput field', async () => {+ const { admit, spies } = makeAdmission({+ hostProvider: () => 'codex-subscription',+ resolveTaskModel: vi.fn(() => ({+ provider: { getProviderType: () => 'codex-subscription' },+ model: 'gpt-5.6-luna',+ })),+ resolveImageInput: vi.fn(() => ({})),+ })++ const response = await admit(imageMessage())++ expect(response).toMatchObject({ success: true, taskId: 't-1' })+ expect(spies.resolveImageInput).toHaveBeenCalledWith('codex-subscription', 'gpt-5.6-luna')+ expect(spies.dispatch).toHaveBeenCalledTimes(1)+ expect(refusalLogs(spies)).toHaveLength(0)+ })++ it('refuses an OpenAI image when the live catalog has no imageInput field', async () => {+ const { admit, spies } = makeAdmission({+ hostProvider: () => 'openai',+ resolveTaskModel: vi.fn(() => ({+ provider: { getProviderType: () => 'openai' },+ model: 'gpt-6',+ })),+ resolveImageInput: vi.fn(() => ({})),+ })++ const response = await admit(imageMessage())++ expect(response).toMatchObject({+ success: false,+ error: { code: 'LLM_IMAGE_INPUT_UNKNOWN', retryable: false, provider: 'openai' },+ })+ expect(spies.dispatch).toHaveBeenCalledTimes(0)+ })+it('admits an image on a supported pair and pins imageModel to the served pair', async () => {const { admit, spies } = makeAdmission()diff --git a/mcp-host/src/agent/incomingAdmission.ts b/mcp-host/src/agent/incomingAdmission.tsindex 62ec7224f..522d98a93 100644--- a/mcp-host/src/agent/incomingAdmission.ts+++ b/mcp-host/src/agent/incomingAdmission.ts@@ -9,9 +9,12 @@* from live process state arrives through {@link IncomingAdmissionDeps}; the* decision logic here is pure.*/-import { resolveImageInputCapability } from '@clerum/llm-providers'import { LlmErrorCode } from '../core/errors'-import { chatTransportSupportsImageInput, imageInputDenialMessage } from '../llm/imageInput'+import {+ chatTransportSupportsImageInput,+ imageInputDenialMessage,+ resolveHostImageInput,+} from '../llm/imageInput'import type { IncomingMessage, MessageResponse, SetModelResult } from '../server/types'import { serializeSessionKey } from '../session/types.js'import { validateIncomingImageAttachments } from './incomingImageAttachments'@@ -198,7 +201,7 @@ export function createIncomingAdmission(deps: IncomingAdmissionDeps): IncomingAd}const pair = { provider: resolved.provider.getProviderType(), model: resolved.model }const facts = deps.resolveImageInput(pair.provider, pair.model)- const decision = resolveImageInputCapability(facts?.capability, {+ const decision = resolveHostImageInput(pair.provider, facts?.capability, {transportSupported: chatTransportSupportsImageInput(pair.provider),})if (decision.state !== 'supported') {diff --git a/mcp-host/src/config/__tests__/modelResolution.test.ts b/mcp-host/src/config/__tests__/modelResolution.test.tsindex c831c0d1e..799952c56 100644--- a/mcp-host/src/config/__tests__/modelResolution.test.ts+++ b/mcp-host/src/config/__tests__/modelResolution.test.ts@@ -99,6 +99,23 @@ describe('projectModels', () => {projectModels(catalog, 'codex-subscription', 'visual-model').models[0].imageInput).toEqual({ state: 'supported', reason: 'supported', evidence })})++ it('projects a live ChatGPT Codex row with no imageInput as supported', () => {+ expect(+ projectModels(+ view(true, { 'codex-subscription': [{ model: 'gpt-5.6-luna' }] }),+ 'codex-subscription',+ 'gpt-5.6-luna'+ ).models[0].imageInput+ ).toEqual({ state: 'supported', reason: 'supported' })+ })++ it('keeps a live OpenAI row with no imageInput unknown', () => {+ expect(+ projectModels(view(true, { openai: [{ model: 'gpt-6' }] }), 'openai', 'gpt-6').models[0]+ .imageInput+ ).toEqual({ state: 'unknown', reason: 'model_unknown' })+ })it('projects enabled entries with optional metadata', () => {expect(projectModels(view(true, CLAUDE_ALLOWLIST), 'claude', 'claude-opus-4-8')).toEqual({degraded: false,diff --git a/mcp-host/src/config/modelResolution.ts b/mcp-host/src/config/modelResolution.tsindex c154c7d98..7a381143a 100644--- a/mcp-host/src/config/modelResolution.ts+++ b/mcp-host/src/config/modelResolution.ts@@ -11,8 +11,8 @@* not yet delivered (`allowlistAvailable() === false`), only the Host-configured* default model is permitted — never fail-open, never brick an existing Host.*/-import { type ImageInputDecision, resolveImageInputCapability } from '@clerum/llm-providers'-import { chatTransportSupportsImageInput } from '../llm/imageInput'+import { type ImageInputDecision } from '@clerum/llm-providers'+import { chatTransportSupportsImageInput, resolveHostImageInput } from '../llm/imageInput'import type { HostAllowedModel } from '../types'import type { AllowlistView } from './allowlistCheck'import type { AllowedModelEntry } from './configStore'@@ -145,7 +145,7 @@ export function contextWindowForModel(function entryToWire(e: AllowedModelEntry, provider: string): ModelWireEntry {const wire: ModelWireEntry = {name: e.model,- imageInput: resolveImageInputCapability(e.imageInput, {+ imageInput: resolveHostImageInput(provider, e.imageInput, {transportSupported: chatTransportSupportsImageInput(provider),}),}diff --git a/mcp-host/src/core/adapters/__tests__/llmPortAdapter.imageInput.test.ts b/mcp-host/src/core/adapters/__tests__/llmPortAdapter.imageInput.test.tsindex 75db13aad..127f2753f 100644--- a/mcp-host/src/core/adapters/__tests__/llmPortAdapter.imageInput.test.ts+++ b/mcp-host/src/core/adapters/__tests__/llmPortAdapter.imageInput.test.ts@@ -342,6 +342,27 @@ describe('#654 LlmPortAdapter image guard', () => {expect(messages[0].contentParts?.[0].type).toBe('image')})+ it('dispatches Codex V2 chat when the live catalog has no imageInput field', async () => {+ const provider = fakeProvider('codex-subscription')+ const adapter = new LlmPortAdapter(+ provider,+ 'gpt-5.6-luna',+ 'codex-subscription',+ undefined,+ undefined,+ undefined,+ undefined,+ undefined,+ () => ({})+ )++ await adapter.completeWithTools({ messages: [imageMessage()], tools: [] })++ expect(provider.completeSingleTurnWithTools).toHaveBeenCalledTimes(1)+ const [messages] = provider.completeSingleTurnWithTools.mock.calls[0]+ expect(messages[0].contentParts?.[0].type).toBe('image')+ })+it('refuses a malformed base64 image part with LLM_INVALID_ATTACHMENT before consulting the resolver', async () => {const provider = fakeProvider('openai')const resolver = vi.fn(allow({ state: 'supported', evidence: CURATED_EVIDENCE }))diff --git a/mcp-host/src/llm/__tests__/imageInput.test.ts b/mcp-host/src/llm/__tests__/imageInput.test.tsindex 186617e37..f040f6fa1 100644--- a/mcp-host/src/llm/__tests__/imageInput.test.ts+++ b/mcp-host/src/llm/__tests__/imageInput.test.ts@@ -259,6 +259,17 @@ describe('#654 decideImageInput intersection', () => {).toEqual({ state: 'supported', reason: 'supported', evidence: supported.evidence })})+ it('treats a live Codex catalog row with no imageInput as supported', () => {+ expect(+ decideImageInput({+ providerType: 'codex-subscription',+ method: 'completeWithTools',+ roles: ['user'],+ capability: undefined,+ })+ ).toEqual({ state: 'supported', reason: 'supported' })+ })+it('treats absent or malformed evidence as unknown, never as support', () => {for (const capability of [undefined, null, {}, { state: 'maybe' }, { state: 'supported' }]) {expect(diff --git a/mcp-host/src/llm/imageInput.ts b/mcp-host/src/llm/imageInput.tsindex 1120cc1ce..007ab0103 100644--- a/mcp-host/src/llm/imageInput.ts+++ b/mcp-host/src/llm/imageInput.ts@@ -235,11 +235,39 @@ export interface ImageInputRequestFacts {* implementation ∩ message role. Delegates the evidence half to the shared* resolver so both sides cannot drift.*/+/**+ * Host intersection for one (provider, capability) pair.+ *+ * #669 keeps absence → `unknown` for every non-Codex provider. Codex+ * Subscription has no per-model vision split and no models.dev row: a live+ * ChatGPT catalog entry has no `imageInput` field. When the V2 transport can+ * carry the image, that absence is `supported` so Luna and every other Codex+ * model match the pre-#669 path. Curated `unsupported` / dated evidence still+ * wins. Never invent models.dev rows.+ */+export function resolveHostImageInput(+ providerType: string,+ capability: unknown,+ options: { transportSupported: boolean; now?: number }+): ImageInputDecision {+ const decision = resolveImageInputCapability(capability, options)+ if (+ imageWireFamilyFor(providerType) === 'codex' &&+ options.transportSupported === true &&+ decision.state === 'unknown' &&+ decision.reason === 'model_unknown' &&+ decision.evidence === undefined+ ) {+ return { state: 'supported', reason: 'supported' }+ }+ return decision+}+export function decideImageInput(facts: ImageInputRequestFacts): ImageInputDecision {const transportSupported =facts.roles.length > 0 &&facts.roles.every(role => transportSupportsImageInput(facts.providerType, facts.method, role))- return resolveImageInputCapability(facts.capability, {+ return resolveHostImageInput(facts.providerType, facts.capability, {transportSupported,now: facts.now,})diff --git a/packages/llm-provider-attempt-contract/index.cjs b/packages/llm-provider-attempt-contract/index.cjsindex 8d2d5fa1c..356a814fa 100644--- a/packages/llm-provider-attempt-contract/index.cjs+++ b/packages/llm-provider-attempt-contract/index.cjs@@ -122,8 +122,8 @@ const RECEIPT_KEYS = new Set([])const USAGE_KEYS = new Set(['inputTokens', 'outputTokens'])-function fail(code, message) {- return { ok: false, code, message }+function fail(code, message, kind) {+ return kind ? { ok: false, code, message, kind } : { ok: false, code, message }}function ok(value) {@@ -290,12 +290,16 @@ function checkStructure(value, maxDepth) {const depth = stack.pop()const node = stack.pop()if (depth > maxDepth) {- return fail('limit', `request exceeds maximum nesting depth ${LIMITS.maxNestingDepth}`)+ return fail(+ 'limit',+ `request exceeds maximum nesting depth ${LIMITS.maxNestingDepth}`,+ 'depth'+ )}const children = Array.isArray(node) ? node : Object.values(node).filter(child => child !== undefined)elements += children.lengthif (elements > LIMITS.maxRequestBodyBytes) {- return fail('limit', 'request exceeds maxRequestBodyBytes')+ return fail('limit', 'request exceeds maxRequestBodyBytes', 'size')}for (const child of children) {if (child !== null && typeof child === 'object') stack.push(child, depth + 1)@@ -317,7 +321,11 @@ function assertFiniteTree(value, label, depth = 1) {return fail('invalid', `${label} has an unsupported type`)}if (depth > LIMITS.maxNestingDepth) {- return fail('limit', `${label} exceeds maximum nesting depth ${LIMITS.maxNestingDepth}`)+ return fail(+ 'limit',+ `${label} exceeds maximum nesting depth ${LIMITS.maxNestingDepth}`,+ 'depth'+ )}if (Array.isArray(value)) {for (let i = 0; i < value.length; i++) {@@ -348,7 +356,7 @@ function parseMessages(raw, messageKeys, visualBudget) {return fail('invalid', 'messages must be a non-empty array')}if (raw.length > LIMITS.maxMessages) {- return fail('limit', `messages exceed ${LIMITS.maxMessages}`)+ return fail('limit', `messages exceed ${LIMITS.maxMessages}`, 'count')}const messages = []for (let i = 0; i < raw.length; i++) {@@ -377,7 +385,7 @@ function parseMessages(raw, messageKeys, visualBudget) {return fail('invalid', `${label}.toolCalls must be a non-empty array`)}if (item.toolCalls.length > LIMITS.maxToolCalls) {- return fail('limit', `${label}.toolCalls exceed ${LIMITS.maxToolCalls}`)+ return fail('limit', `${label}.toolCalls exceed ${LIMITS.maxToolCalls}`, 'count')}const toolCalls = []for (let j = 0; j < item.toolCalls.length; j++) {@@ -472,7 +480,7 @@ function parseGeneration(raw) {return fail('non-finite', 'generation.maxOutputTokens must be a finite integer')}if (raw.maxOutputTokens < 1 || raw.maxOutputTokens > LIMITS.maxOutputTokens) {- return fail('limit', 'generation.maxOutputTokens is out of range')+ return fail('limit', 'generation.maxOutputTokens is out of range', 'range')}generation.maxOutputTokens = raw.maxOutputTokens}@@ -529,11 +537,12 @@ function parseCodexCompletionRequestRoot(input, schemaVersion, messageKeys) {'limit',visualSchema? 'request exceeds maxVisualRequestBodyBytes'- : 'request exceeds maxRequestBodyBytes'+ : 'request exceeds maxRequestBodyBytes',+ 'size')}if (visualSchema && measureNonImageRequestBytes(input) > LIMITS.maxRequestBodyBytes) {- return fail('limit', 'request exceeds maxRequestBodyBytes outside image data')+ return fail('limit', 'request exceeds maxRequestBodyBytes outside image data', 'size')}const extra = rejectUnknown(input, ROOT_KEYS, 'request')if (extra) return extra@@ -564,7 +573,7 @@ function parseCodexCompletionRequestRoot(input, schemaVersion, messageKeys) {return fail('non-finite', 'deadlineMs must be a finite integer')}if (input.deadlineMs < 1 || input.deadlineMs > LIMITS.maxDeadlineMs) {- return fail('limit', 'deadlineMs is out of range')+ return fail('limit', 'deadlineMs is out of range', 'range')}deadlineMs = input.deadlineMs}@@ -663,18 +672,19 @@ function parseContentParts(raw, label, visualBudget) {const source = parseImageSource(part.source, `${partLabel}.source`)if (!source.ok) return sourceconst image = inspectVisualImage({ mimeType: part.mimeType, data: part.data })- if (!image.ok) return fail(image.code, `${partLabel}: ${image.message}`)+ if (!image.ok) return fail(image.code, `${partLabel}: ${image.message}`, image.kind)// Request-scoped budgets: history can carry more messages than one, so a// per-message counter would let a request exceed the declared limits.visualBudget.images += 1if (visualBudget.images > VISUAL_LIMITS.maxImages) {- return fail('limit', `request exceeds ${VISUAL_LIMITS.maxImages} images`)+ return fail('limit', `request exceeds ${VISUAL_LIMITS.maxImages} images`, 'count')}visualBudget.totalBytes += image.value.bytesif (visualBudget.totalBytes > VISUAL_LIMITS.maxTotalImageBytes) {return fail('limit',- `request exceeds ${VISUAL_LIMITS.maxTotalImageBytes} total image bytes`+ `request exceeds ${VISUAL_LIMITS.maxTotalImageBytes} total image bytes`,+ 'size')}parts.push({@@ -762,7 +772,8 @@ function buildCodexProxyEnvelope(input) {'limit',visualSchema? 'proxy envelope exceeds maxVisualRequestBodyBytes'- : 'proxy envelope exceeds maxRequestBodyBytes'+ : 'proxy envelope exceeds maxRequestBodyBytes',+ 'size')}return ok(Object.freeze(envelope))diff --git a/packages/llm-provider-attempt-contract/index.d.ts b/packages/llm-provider-attempt-contract/index.d.tsindex 936254eb4..f1c4edc80 100644--- a/packages/llm-provider-attempt-contract/index.d.ts+++ b/packages/llm-provider-attempt-contract/index.d.ts@@ -57,9 +57,11 @@ export declare const VISUAL_LIMITS: {readonly maxImagePixels: 4194304}+export type ContractLimitKind = 'size' | 'range' | 'count' | 'depth'+export type ContractResult<T> =| { ok: true; value: T }- | { ok: false; code: string; message: string }+ | { ok: false; code: string; message: string; kind?: ContractLimitKind }export type CodexMessageRole = 'system' | 'user' | 'assistant' | 'tool'diff --git a/packages/llm-provider-attempt-contract/index.test.cjs b/packages/llm-provider-attempt-contract/index.test.cjsindex 1d2963442..76fe610b4 100644--- a/packages/llm-provider-attempt-contract/index.test.cjs+++ b/packages/llm-provider-attempt-contract/index.test.cjs@@ -349,6 +349,7 @@ test('large catalogs remain bounded by serialized request bytes including UTF-8'assert.deepEqual(contract.parseCodexCompletionRequestV1(request), {ok: false,code: 'limit',+ kind: 'size',message: 'request exceeds maxRequestBodyBytes',})})@@ -428,6 +429,7 @@ test('opaque canonical names remain bounded by serialized UTF-8 request bytes',assert.deepEqual(contract.parseCodexCompletionRequestV1(request), {ok: false,code: 'limit',+ kind: 'size',message: 'request exceeds maxRequestBodyBytes',})})@@ -1009,6 +1011,7 @@ test('v2 preserves the non-image budget: text and tools stay on the 1 MiB ceilinassert.deepEqual(contract.parseCodexCompletionRequest(v2TextRequest(longText)), {ok: false,code: 'limit',+ kind: 'size',message: overflow,})diff --git a/packages/llm-provider-attempt-contract/visualPayload.cjs b/packages/llm-provider-attempt-contract/visualPayload.cjsindex 11099b021..32bda41a3 100644--- a/packages/llm-provider-attempt-contract/visualPayload.cjs+++ b/packages/llm-provider-attempt-contract/visualPayload.cjs@@ -58,8 +58,8 @@ const JPEG_EOI = 0xd9const JPEG_SOS = 0xdaconst JPEG_TEM = 0x01-function fail(code, message) {- return { ok: false, code, message }+function fail(code, message, kind) {+ return kind ? { ok: false, code, message, kind } : { ok: false, code, message }}function ok(value) {@@ -75,14 +75,14 @@ function decodeStrictBase64(data, maxDecodedBytes) {if (typeof data !== 'string') return fail('invalid', 'image data must be a base64 string')if (data.length === 0) return fail('invalid', 'image data must not be empty')if (data.length > MAX_ENCODED_IMAGE_BYTES) {- return fail('limit', `image exceeds ${maxDecodedBytes} decoded bytes`)+ return fail('limit', `image exceeds ${maxDecodedBytes} decoded bytes`, 'size')}if (data.length % 4 !== 0) return fail('invalid', 'image data must be canonical base64')if (!BASE64_PATTERN.test(data)) return fail('invalid', 'image data must be canonical base64')const bytes = Buffer.from(data, 'base64')if (bytes.length === 0) return fail('invalid', 'image data must not be empty')if (bytes.length > maxDecodedBytes) {- return fail('limit', `image exceeds ${maxDecodedBytes} decoded bytes`)+ return fail('limit', `image exceeds ${maxDecodedBytes} decoded bytes`, 'size')}if (bytes.toString('base64') !== data) {return fail('invalid', 'image data must be canonical base64')@@ -184,10 +184,10 @@ function assertWithinDimensionBudget(dimensions) {dimensions.width > VISUAL_LIMITS.maxImageDimension ||dimensions.height > VISUAL_LIMITS.maxImageDimension) {- return fail('limit', `image dimension exceeds ${VISUAL_LIMITS.maxImageDimension}`)+ return fail('limit', `image dimension exceeds ${VISUAL_LIMITS.maxImageDimension}`, 'range')}if (dimensions.width * dimensions.height > VISUAL_LIMITS.maxImagePixels) {- return fail('limit', `image pixel count exceeds ${VISUAL_LIMITS.maxImagePixels}`)+ return fail('limit', `image pixel count exceeds ${VISUAL_LIMITS.maxImagePixels}`, 'range')}return null}