Skip to content

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

TypeScript

test(codex): share contract-valid image fixtures across hop tests

Alfredo Lopez28907decdbc3
diff --git a/control-api/package-lock.json b/control-api/package-lock.json
index 7744eb931..5ece66ea9 100644
--- a/control-api/package-lock.json
+++ b/control-api/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-api",
- "version": "0.1.556",
+ "version": "0.1.557",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-api",
- "version": "0.1.556",
+ "version": "0.1.557",
"dependencies": {
"@clerum/display-field": "file:../packages/display-field",
"@clerum/image-policy": "file:../packages/image-policy",
diff --git a/control-api/package.json b/control-api/package.json
index c27919fdd..ac1d43bed 100644
--- a/control-api/package.json
+++ b/control-api/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-api",
- "version": "0.1.556",
+ "version": "0.1.557",
"description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets",
"main": "dist/main.js",
"engines": {
diff --git a/control-api/test/services.llmProviderAttemptAuthorizer.test.ts b/control-api/test/services.llmProviderAttemptAuthorizer.test.ts
index 092bef548..e2314d946 100644
--- a/control-api/test/services.llmProviderAttemptAuthorizer.test.ts
+++ b/control-api/test/services.llmProviderAttemptAuthorizer.test.ts
@@ -1,6 +1,6 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { readFileSync } from 'node:fs'
-import { crc32 } from 'node:zlib'
+import { createRequire } from 'node:module'
import { LIMITS, hashCodexCompletionRequestV1 } from '@clerum/llm-provider-attempt-contract'
import { streamCodexCompletion } from '../../codex-llm-proxy/src/codexTransport'
import {
@@ -18,6 +18,13 @@ import {
} from '../src/services/llmProviderAttemptAuthorizer.js'
import type { McpHostAccessClaims } from '../src/utils/auth/mcpHostJwtToken.js'
+const { jpegOfSize, padPngToSize } = createRequire(import.meta.url)(
+ '../../packages/llm-provider-attempt-contract/testImageFixtures.cjs'
+) as {
+ jpegOfSize: (targetBytes: number, width?: number, height?: number) => Buffer
+ padPngToSize: (png: Buffer | string, targetBytes: number) => Buffer
+}
+
// Non-operational sentinel consumed only by the injected external model fixture.
const FIXTURE_ACCESS_VALUE = 'fixture-only'
@@ -72,48 +79,6 @@ const REQUEST = {
const MIB = 1024 * 1024
-/**
- * Structurally framed JPEG of an exact byte length. Matches the shared
- * contract helper: SOI, SOF0 dimensions, SOS, entropy pad, EOI.
- */
-function jpegOfSize(targetBytes: number, width = 2, height = 2): Buffer {
- const sofPayload = Buffer.alloc(9)
- sofPayload[0] = 8
- sofPayload.writeUInt16BE(height, 1)
- sofPayload.writeUInt16BE(width, 3)
- sofPayload[5] = 1
- sofPayload[6] = 1
- sofPayload[7] = 0x11
- sofPayload[8] = 0
- const segmentLength = Buffer.alloc(2)
- segmentLength.writeUInt16BE(sofPayload.length + 2, 0)
- const sof = Buffer.concat([Buffer.from([0xff, 0xc0]), segmentLength, sofPayload])
- const sosPayload = Buffer.alloc(6)
- const sosLength = Buffer.alloc(2)
- sosLength.writeUInt16BE(sosPayload.length + 2, 0)
- const sos = Buffer.concat([Buffer.from([0xff, 0xda]), sosLength, sosPayload])
- const entropyLength = targetBytes - (2 + sof.length + sos.length + 2)
- return Buffer.concat([
- Buffer.from([0xff, 0xd8]),
- sof,
- sos,
- Buffer.alloc(entropyLength, 0x2a),
- Buffer.from([0xff, 0xd9]),
- ])
-}
-
-function padPngToSize(pngBase64: string, imageBytes: number): string {
- const png = Buffer.from(pngBase64, 'base64')
- const payload = Buffer.alloc(imageBytes - png.length - 12, 65)
- Buffer.from('Comment\0').copy(payload)
- const chunk = Buffer.alloc(payload.length + 12)
- chunk.writeUInt32BE(payload.length)
- chunk.write('tEXt', 4)
- payload.copy(chunk, 8)
- chunk.writeUInt32BE(crc32(chunk.subarray(4, -4)) >>> 0, chunk.length - 4)
- return Buffer.concat([png.subarray(0, -12), chunk, png.subarray(-12)]).toString('base64')
-}
-
type VisualImagePart = {
type: string
mimeType: string
@@ -319,7 +284,7 @@ describe('authorizeLlmProviderAttempt', () => {
if (imageBytes > 0) {
image.data =
format === 'png'
- ? padPngToSize(image.data, imageBytes)
+ ? padPngToSize(image.data, imageBytes).toString('base64')
: jpegOfSize(imageBytes).toString('base64')
expect(Buffer.from(image.data, 'base64')).toHaveLength(imageBytes)
}
@@ -398,7 +363,7 @@ describe('authorizeLlmProviderAttempt', () => {
expect(template).toBeDefined()
const data =
item.format === 'png'
- ? padPngToSize(template!.data, item.bytes)
+ ? padPngToSize(template!.data, item.bytes).toString('base64')
: jpegOfSize(item.bytes).toString('base64')
expect(Buffer.from(data, 'base64')).toHaveLength(item.bytes)
return {
diff --git a/external-rest-api/src/releaseManifest.ts b/external-rest-api/src/releaseManifest.ts
index 7a70ede00..aa84e757f 100644
--- a/external-rest-api/src/releaseManifest.ts
+++ b/external-rest-api/src/releaseManifest.ts
@@ -9,7 +9,7 @@ export type ReleaseManifest = {
export const releaseManifest: ReleaseManifest = {
releaseId: 'v0.8.1',
externalRestApiVersion: '0.1.82',
- rpcProxyVersion: '0.1.79',
+ rpcProxyVersion: '0.1.80',
desktopVersion: '0.8.1',
minimumDesktopVersion: '0.1.252',
}
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index 9f9d220a9..fd8e4ee80 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.342",
+ "version": "0.1.343",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.342",
+ "version": "0.1.343",
"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 74be27fa3..3ba267817 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.342",
+ "version": "0.1.343",
"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__/bodylimits.test.ts b/mcp-host/src/__tests__/bodylimits.test.ts
index 5e10c9c73..03ca40472 100644
--- a/mcp-host/src/__tests__/bodylimits.test.ts
+++ b/mcp-host/src/__tests__/bodylimits.test.ts
@@ -16,11 +16,17 @@
*/
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'
import type { AddressInfo } from 'net'
+import { createRequire } from 'node:module'
import type { IncomingMessage } from '../server/types'
+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 PNG_SIGNATURE = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
-const JPEG_SIGNATURE = Buffer.from([0xff, 0xd8, 0xff])
const BASE64_ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
let baseUrl: string
@@ -51,25 +57,22 @@ afterAll(async () => {
const pngBase64Cache = new Map<number, string>()
const jpegBase64Cache = new Map<number, string>()
-function imageBase64(cache: Map<number, string>, signature: Buffer, sizeBytes: number): string {
+function cachedBase64(cache: Map<number, string>, bytes: Buffer, sizeBytes: number): string {
const cached = cache.get(sizeBytes)
if (cached !== undefined) return cached
- const bytes = Buffer.alloc(sizeBytes)
- signature.copy(bytes, 0)
- bytes.fill(0x41, signature.length)
const encoded = bytes.toString('base64')
cache.set(sizeBytes, encoded)
return encoded
}
-/** Canonical base64 of a PNG whose decoded length is exactly `sizeBytes`. */
+/** Canonical base64 of a contract-framed PNG of exactly `sizeBytes`. */
function pngBase64(sizeBytes: number): string {
- return imageBase64(pngBase64Cache, PNG_SIGNATURE, sizeBytes)
+ return cachedBase64(pngBase64Cache, declaredHeaderPngOfSize(sizeBytes), sizeBytes)
}
-/** Canonical base64 of a JPEG whose decoded length is exactly `sizeBytes`. */
+/** Canonical base64 of a contract-framed JPEG of exactly `sizeBytes`. */
function jpegBase64(sizeBytes: number): string {
- return imageBase64(jpegBase64Cache, JPEG_SIGNATURE, sizeBytes)
+ return cachedBase64(jpegBase64Cache, jpegOfSize(sizeBytes), sizeBytes)
}
/**
diff --git a/packages/llm-provider-attempt-contract/index.test.cjs b/packages/llm-provider-attempt-contract/index.test.cjs
index 45e8bff54..f04b912cf 100644
--- a/packages/llm-provider-attempt-contract/index.test.cjs
+++ b/packages/llm-provider-attempt-contract/index.test.cjs
@@ -7,6 +7,13 @@ const path = require('node:path')
const test = require('node:test')
const zlib = require('node:zlib')
const contract = require('./index.cjs')
+const {
+ declaredHeaderPng,
+ declaredHeaderPngOfSize,
+ jpegOfSize,
+ realPng,
+ realPngOfSize,
+} = require('./testImageFixtures.cjs')
const FIXTURE = JSON.parse(
fs.readFileSync(path.join(__dirname, 'fixtures/canonical-request-hashes.json'), 'utf8')
@@ -431,7 +438,6 @@ const IMAGE_DATA = {
jpeg: VISUAL_FIXTURE.jpeg.messages[0].contentParts.find(part => part.type === 'image').data,
}
const MAX_ENCODED_IMAGE_CHARS = 4 * Math.ceil(contract.VISUAL_LIMITS.maxImageBytes / 3)
-const PNG_SIGNATURE = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
/**
* Fixture image parts carry `dataRef` (resolved from visual-requests.json) so
@@ -480,139 +486,11 @@ function pngChunks(bytes) {
return chunks
}
-function pngChunk(type, data) {
- const length = Buffer.alloc(4)
- length.writeUInt32BE(data.length, 0)
- const typeBytes = Buffer.from(type, 'latin1')
- const crc = Buffer.alloc(4)
- crc.writeUInt32BE(zlib.crc32(Buffer.concat([typeBytes, data])) >>> 0, 0)
- return Buffer.concat([length, typeBytes, data, crc])
-}
-
-function pngHeader(width, height) {
- const ihdr = Buffer.alloc(13)
- ihdr.writeUInt32BE(width, 0)
- ihdr.writeUInt32BE(height, 4)
- ihdr[8] = 8
- ihdr[9] = 2
- return ihdr
-}
-
-/** Real PNG: a deflate stream over filtered RGB scanlines plus real CRCs. */
-function realPng(width, height, seed) {
- const stride = 1 + width * 3
- const raw = Buffer.alloc(height * stride)
- let state = seed >>> 0
- for (let i = 0; i < raw.length; i++) {
- if (i % stride === 0) continue
- state ^= state << 13
- state >>>= 0
- state ^= state >>> 17
- state ^= state << 5
- state >>>= 0
- raw[i] = state & 0xff
- }
- return Buffer.concat([
- PNG_SIGNATURE,
- pngChunk('IHDR', pngHeader(width, height)),
- pngChunk('IDAT', zlib.deflateSync(raw, { level: 6 })),
- pngChunk('IEND', Buffer.alloc(0)),
- ])
-}
-
-/**
- * Framed PNG whose IDAT is not a decodable stream. Used only for the declared
- * header budgets: the pixel budget is enforced on IHDR before any decode, which
- * is the documented guarantee boundary of this contract.
- */
-function declaredHeaderPng(width, height) {
- return Buffer.concat([
- PNG_SIGNATURE,
- pngChunk('IHDR', pngHeader(width, height)),
- pngChunk('IDAT', Buffer.alloc(8)),
- pngChunk('IEND', Buffer.alloc(0)),
- ])
-}
-
-/**
- * Declared-header container padded to an exact byte length (signature 8,
- * IHDR 25, IDAT 12 + payload, IEND 12). Also a budget probe only.
- */
-function declaredHeaderPngOfSize(targetBytes) {
- const overhead = 8 + 25 + 12 + 12
- assert.ok(targetBytes > overhead, 'target must leave room for image data')
- return Buffer.concat([
- PNG_SIGNATURE,
- pngChunk('IHDR', pngHeader(2, 2)),
- pngChunk('IDAT', Buffer.alloc(targetBytes - overhead)),
- pngChunk('IEND', Buffer.alloc(0)),
- ])
-}
-
const MIB = 1024 * 1024
// The per-image ceiling this suite replaced. Kept as a literal so the tests
// prove the raise rather than restating the current constant.
const FORMER_IMAGE_CEILING = 524288
-/**
- * Real PNG (correct CRCs, inflatable scanlines) padded to an exact byte length
- * with an ancillary tEXt chunk inserted before IEND. Ancillary chunks are
- * legal anywhere before IEND, so the image stays decodable while the file size
- * is exact.
- */
-function realPngOfSize(targetBytes, width, height, seed) {
- const base = realPng(width, height, seed)
- const padding = targetBytes - base.length
- assert.ok(padding >= 0, `target ${targetBytes} is smaller than the real PNG ${base.length}`)
- if (padding === 0) return base
- assert.ok(padding >= 12, 'target must leave room for the padding chunk')
- const iend = base.subarray(base.length - 12)
- const body = base.subarray(0, base.length - 12)
- const payloadLength = padding - 12
- const text = Buffer.concat([
- Buffer.from('pad\0', 'latin1'),
- Buffer.alloc(Math.max(payloadLength - 4, 0), 0x20),
- ])
- assert.equal(text.length, payloadLength)
- return Buffer.concat([body, pngChunk('tEXt', text), iend])
-}
-
-/**
- * Structurally framed JPEG of an exact byte length: SOI, a frame header that
- * declares the dimensions, a start-of-scan segment, entropy payload and EOI.
- * This is the container the v2 JPEG checks were written against — marker
- * framing and declared dimensions — not an entropy-decodable image; the module
- * documents that it never decodes scan data.
- */
-function jpegOfSize(targetBytes, width = 2, height = 2) {
- const sofPayload = Buffer.alloc(9)
- sofPayload[0] = 8
- sofPayload.writeUInt16BE(height, 1)
- sofPayload.writeUInt16BE(width, 3)
- sofPayload[5] = 1
- sofPayload[6] = 1
- sofPayload[7] = 0x11
- sofPayload[8] = 0
- const segmentLength = Buffer.alloc(2)
- segmentLength.writeUInt16BE(sofPayload.length + 2, 0)
- const sof = Buffer.concat([Buffer.from([0xff, 0xc0]), segmentLength, sofPayload])
-
- const sosPayload = Buffer.alloc(6)
- const sosLength = Buffer.alloc(2)
- sosLength.writeUInt16BE(sosPayload.length + 2, 0)
- const sos = Buffer.concat([Buffer.from([0xff, 0xda]), sosLength, sosPayload])
-
- const entropyLength = targetBytes - (2 + sof.length + sos.length + 2)
- assert.ok(entropyLength >= 1, 'target must leave room for entropy-coded data')
- return Buffer.concat([
- Buffer.from([0xff, 0xd8]),
- sof,
- sos,
- Buffer.alloc(entropyLength, 0x2a),
- Buffer.from([0xff, 0xd9]),
- ])
-}
-
function imagePart(data, mimeType = 'image/png') {
return {
type: 'image',
diff --git a/packages/llm-provider-attempt-contract/package.json b/packages/llm-provider-attempt-contract/package.json
index 513dae455..68b74fbb7 100644
--- a/packages/llm-provider-attempt-contract/package.json
+++ b/packages/llm-provider-attempt-contract/package.json
@@ -10,6 +10,11 @@
"types": "./index.d.ts",
"require": "./index.cjs",
"default": "./index.cjs"
+ },
+ "./test-image-fixtures": {
+ "types": "./testImageFixtures.d.ts",
+ "require": "./testImageFixtures.cjs",
+ "default": "./testImageFixtures.cjs"
}
},
"scripts": {
diff --git a/packages/llm-provider-attempt-contract/testImageFixtures.cjs b/packages/llm-provider-attempt-contract/testImageFixtures.cjs
new file mode 100644
index 000000000..17fea0d35
--- /dev/null
+++ b/packages/llm-provider-attempt-contract/testImageFixtures.cjs
@@ -0,0 +1,180 @@
+'use strict'
+
+/**
+ * Shared test-only image containers for Codex visual-budget suites.
+ *
+ * These bytes are for parsers and hop tests. They do not change JWT issuance,
+ * hostRef binding, or the Desktop/rpc-proxy body allow-list. Production code
+ * must not import this file.
+ */
+
+const zlib = require('node:zlib')
+
+const PNG_SIGNATURE = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
+
+function pngChunk(type, data) {
+ const length = Buffer.alloc(4)
+ length.writeUInt32BE(data.length, 0)
+ const typeBytes = Buffer.from(type, 'latin1')
+ const crc = Buffer.alloc(4)
+ crc.writeUInt32BE(zlib.crc32(Buffer.concat([typeBytes, data])) >>> 0, 0)
+ return Buffer.concat([length, typeBytes, data, crc])
+}
+
+function pngHeader(width, height) {
+ const ihdr = Buffer.alloc(13)
+ ihdr.writeUInt32BE(width, 0)
+ ihdr.writeUInt32BE(height, 4)
+ ihdr[8] = 8
+ ihdr[9] = 2
+ return ihdr
+}
+
+/** Real PNG: a deflate stream over filtered RGB scanlines plus real CRCs. */
+function realPng(width, height, seed) {
+ const stride = 1 + width * 3
+ const raw = Buffer.alloc(height * stride)
+ let state = seed >>> 0
+ for (let i = 0; i < raw.length; i++) {
+ if (i % stride === 0) continue
+ state ^= state << 13
+ state >>>= 0
+ state ^= state >>> 17
+ state ^= state << 5
+ state >>>= 0
+ raw[i] = state & 0xff
+ }
+ return Buffer.concat([
+ PNG_SIGNATURE,
+ pngChunk('IHDR', pngHeader(width, height)),
+ pngChunk('IDAT', zlib.deflateSync(raw, { level: 6 })),
+ pngChunk('IEND', Buffer.alloc(0)),
+ ])
+}
+
+/**
+ * Framed PNG whose IDAT is not a decodable stream. Used only for the declared
+ * header budgets: the pixel budget is enforced on IHDR before any decode.
+ */
+function declaredHeaderPng(width, height) {
+ return Buffer.concat([
+ PNG_SIGNATURE,
+ pngChunk('IHDR', pngHeader(width, height)),
+ pngChunk('IDAT', Buffer.alloc(8)),
+ pngChunk('IEND', Buffer.alloc(0)),
+ ])
+}
+
+/**
+ * Declared-header container padded to an exact byte length (signature 8,
+ * IHDR 25, IDAT 12 + payload, IEND 12).
+ */
+function declaredHeaderPngOfSize(targetBytes) {
+ const overhead = 8 + 25 + 12 + 12
+ if (targetBytes <= overhead) {
+ throw new Error('target must leave room for image data')
+ }
+ return Buffer.concat([
+ PNG_SIGNATURE,
+ pngChunk('IHDR', pngHeader(2, 2)),
+ pngChunk('IDAT', Buffer.alloc(targetBytes - overhead)),
+ pngChunk('IEND', Buffer.alloc(0)),
+ ])
+}
+
+/**
+ * Real PNG padded to an exact byte length with an ancillary tEXt chunk
+ * inserted before IEND.
+ */
+function realPngOfSize(targetBytes, width, height, seed) {
+ const base = realPng(width, height, seed)
+ const padding = targetBytes - base.length
+ if (padding < 0) {
+ throw new Error(`target ${targetBytes} is smaller than the real PNG ${base.length}`)
+ }
+ if (padding === 0) return base
+ if (padding < 12) {
+ throw new Error('target must leave room for the padding chunk')
+ }
+ const iend = base.subarray(base.length - 12)
+ const body = base.subarray(0, base.length - 12)
+ const payloadLength = padding - 12
+ const text = Buffer.concat([
+ Buffer.from('pad\0', 'latin1'),
+ Buffer.alloc(Math.max(payloadLength - 4, 0), 0x20),
+ ])
+ if (text.length !== payloadLength) {
+ throw new Error('padding chunk payload length mismatch')
+ }
+ return Buffer.concat([body, pngChunk('tEXt', text), iend])
+}
+
+/**
+ * Pad an existing PNG (Buffer or base64) to an exact byte length with a tEXt
+ * chunk before IEND. Used when a suite starts from the frozen visual fixture.
+ */
+function padPngToSize(png, targetBytes) {
+ const bytes = Buffer.isBuffer(png) ? png : Buffer.from(String(png), 'base64')
+ const padding = targetBytes - bytes.length
+ if (padding < 0) {
+ throw new Error(`target ${targetBytes} is smaller than the PNG ${bytes.length}`)
+ }
+ if (padding === 0) return Buffer.from(bytes)
+ if (padding < 12) {
+ throw new Error('target must leave room for the padding chunk')
+ }
+ const payload = Buffer.alloc(padding - 12, 0x41)
+ Buffer.from('Comment\0').copy(payload)
+ return Buffer.concat([
+ bytes.subarray(0, bytes.length - 12),
+ pngChunk('tEXt', payload),
+ bytes.subarray(bytes.length - 12),
+ ])
+}
+
+/**
+ * Structurally framed JPEG of an exact byte length: SOI, SOF0 dimensions, SOS,
+ * entropy pad, EOI. Marker framing only; scan data is not entropy-decodable.
+ */
+function jpegOfSize(targetBytes, width = 2, height = 2) {
+ const sofPayload = Buffer.alloc(9)
+ sofPayload[0] = 8
+ sofPayload.writeUInt16BE(height, 1)
+ sofPayload.writeUInt16BE(width, 3)
+ sofPayload[5] = 1
+ sofPayload[6] = 1
+ sofPayload[7] = 0x11
+ sofPayload[8] = 0
+ const segmentLength = Buffer.alloc(2)
+ segmentLength.writeUInt16BE(sofPayload.length + 2, 0)
+ const sof = Buffer.concat([Buffer.from([0xff, 0xc0]), segmentLength, sofPayload])
+
+ const sosPayload = Buffer.alloc(6)
+ const sosLength = Buffer.alloc(2)
+ sosLength.writeUInt16BE(sosPayload.length + 2, 0)
+ const sos = Buffer.concat([Buffer.from([0xff, 0xda]), sosLength, sosPayload])
+
+ const entropyLength = targetBytes - (2 + sof.length + sos.length + 2)
+ if (entropyLength < 1) {
+ throw new Error('target must leave room for entropy-coded data')
+ }
+ return Buffer.concat([
+ Buffer.from([0xff, 0xd8]),
+ sof,
+ sos,
+ Buffer.alloc(entropyLength, 0x2a),
+ Buffer.from([0xff, 0xd9]),
+ ])
+}
+
+module.exports = {
+ PNG_SIGNATURE,
+ pngChunk,
+ pngHeader,
+ realPng,
+ declaredHeaderPng,
+ declaredHeaderPngOfSize,
+ realPngOfSize,
+ padPngToSize,
+ jpegOfSize,
+}
diff --git a/packages/llm-provider-attempt-contract/testImageFixtures.d.ts b/packages/llm-provider-attempt-contract/testImageFixtures.d.ts
new file mode 100644
index 000000000..af521c68a
--- /dev/null
+++ b/packages/llm-provider-attempt-contract/testImageFixtures.d.ts
@@ -0,0 +1,17 @@
+/** Test-only image containers. Not part of the production contract surface. */
+
+export declare const PNG_SIGNATURE: Buffer
+
+export declare function pngChunk(type: string, data: Buffer): Buffer
+export declare function pngHeader(width: number, height: number): Buffer
+export declare function realPng(width: number, height: number, seed: number): Buffer
+export declare function declaredHeaderPng(width: number, height: number): Buffer
+export declare function declaredHeaderPngOfSize(targetBytes: number): Buffer
+export declare function realPngOfSize(
+ targetBytes: number,
+ width: number,
+ height: number,
+ seed: number
+): Buffer
+export declare function padPngToSize(png: Buffer | string, targetBytes: number): Buffer
+export declare function jpegOfSize(targetBytes: number, width?: number, height?: number): Buffer
diff --git a/rpc-proxy/package-lock.json b/rpc-proxy/package-lock.json
index 0f20dcc1b..61f4fc42d 100644
--- a/rpc-proxy/package-lock.json
+++ b/rpc-proxy/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-rpc-proxy",
- "version": "0.1.79",
+ "version": "0.1.80",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-rpc-proxy",
- "version": "0.1.79",
+ "version": "0.1.80",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.21.2",
diff --git a/rpc-proxy/package.json b/rpc-proxy/package.json
index 3c2c83742..17d1d6dd8 100644
--- a/rpc-proxy/package.json
+++ b/rpc-proxy/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-rpc-proxy",
- "version": "0.1.79",
+ "version": "0.1.80",
"description": "External-facing RPC proxy for tenant-scoped MCP access",
"main": "dist/main.js",
"engines": {
diff --git a/rpc-proxy/src/__tests__/bodylimits.test.ts b/rpc-proxy/src/__tests__/bodylimits.test.ts
index e3d2c44f6..20fce14ea 100644
--- a/rpc-proxy/src/__tests__/bodylimits.test.ts
+++ b/rpc-proxy/src/__tests__/bodylimits.test.ts
@@ -16,11 +16,17 @@
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
import type { Server } from 'http'
import type { AddressInfo } from 'net'
+import { createRequire } from 'node:module'
import { createApp } from '../app.js'
+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 PNG_SIGNATURE = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
-const JPEG_SIGNATURE = Buffer.from([0xff, 0xd8, 0xff])
const BASE64_ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
let baseUrl: string
@@ -43,25 +49,22 @@ afterAll(async () => {
const pngBase64Cache = new Map<number, string>()
const jpegBase64Cache = new Map<number, string>()
-function imageBase64(cache: Map<number, string>, signature: Buffer, sizeBytes: number): string {
+function cachedBase64(cache: Map<number, string>, bytes: Buffer, sizeBytes: number): string {
const cached = cache.get(sizeBytes)
if (cached !== undefined) return cached
- const bytes = Buffer.alloc(sizeBytes)
- signature.copy(bytes, 0)
- bytes.fill(0x41, signature.length)
const encoded = bytes.toString('base64')
cache.set(sizeBytes, encoded)
return encoded
}
-/** Canonical base64 of a PNG whose decoded length is exactly `sizeBytes`. */
+/** Canonical base64 of a contract-framed PNG of exactly `sizeBytes`. */
function pngBase64(sizeBytes: number): string {
- return imageBase64(pngBase64Cache, PNG_SIGNATURE, sizeBytes)
+ return cachedBase64(pngBase64Cache, declaredHeaderPngOfSize(sizeBytes), sizeBytes)
}
-/** Canonical base64 of a JPEG whose decoded length is exactly `sizeBytes`. */
+/** Canonical base64 of a contract-framed JPEG of exactly `sizeBytes`. */
function jpegBase64(sizeBytes: number): string {
- return imageBase64(jpegBase64Cache, JPEG_SIGNATURE, sizeBytes)
+ return cachedBase64(jpegBase64Cache, jpegOfSize(sizeBytes), sizeBytes)
}
/**