Skip to content

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

TypeScript

test(control-api): add physical memory experiment helpers

Alfredo Lopezb656dacc4cc6
diff --git a/scripts/tests/lib/control-api-authorize-memory-companion.ts b/scripts/tests/lib/control-api-authorize-memory-companion.ts
new file mode 100644
index 000000000..4d0c0d316
--- /dev/null
+++ b/scripts/tests/lib/control-api-authorize-memory-companion.ts
@@ -0,0 +1,225 @@
+/** Test-only in-pod driver. Production signing, HTTP, PostgreSQL and terminal lifecycle remain real. */
+import fs from 'node:fs'
+import path from 'node:path'
+import http from 'node:http'
+import { createHash } from 'node:crypto'
+import { createRequire } from 'node:module'
+import { once } from 'node:events'
+import { connectInspector, parseCgroup } from './control-api-authorize-memory-inspector.mjs'
+
+type Frame = { callId: string; kind: string; requestId?: string; length?: number; options?: Record<string, unknown> }
+export async function runCompanion(): Promise<void> {
+ const output = process.stdout.write.bind(process.stdout)
+ // Only allowlisted envelopes use the saved writer; dependency/bootstrap logs
+ // and secret-bearing response bodies cannot reach the controller.
+ process.stdout.write = () => true; process.stderr.write = () => true
+ const emit = (value: unknown) => output(JSON.stringify(value) + '\n')
+ const sockets = new Map<string, any>(), created = new Map<string, any>()
+ const cookieName = 'control_ui_admin_session'
+ const knownErrors = new Set(['authorize_capacity_exceeded', 'payload_too_large', 'invalid_request', 'request_timeout', 'authorize_timeout', 'unauthorized', 'Unauthorized', 'disabled', 'insufficient_scope', 'no_grant', 'model_not_allowed', 'unassigned_connection', 'connection_unavailable', 'budget_denied', 'host_binding_mismatch', 'provider_unavailable', 'stale_generation', 'idempotency_conflict'])
+ let options: any, inspector: any, prod: any, lockClient: any
+ const require = createRequire(path.join(process.cwd(), 'package.json'))
+ const hash = (value: Uint8Array) => createHash('sha256').update(value).digest('hex')
+ const cgroup = () => parseCgroup({ current: fs.readFileSync('/sys/fs/cgroup/memory.current', 'utf8'), peak: fs.readFileSync('/sys/fs/cgroup/memory.peak', 'utf8'), limit: fs.readFileSync('/sys/fs/cgroup/memory.max', 'utf8'), events: fs.readFileSync('/sys/fs/cgroup/memory.events', 'utf8') })
+ function ordinary(method: string, route: string, body?: unknown, cookie = options.cookie): Promise<any> {
+ return new Promise((resolve, reject) => {
+ const request = http.request({ hostname: '127.0.0.1', port: prod.config.port, method, path: route,
+ headers: { 'content-type': 'application/json', ...(cookie ? { cookie: `${cookieName}=${cookie}` } : {}) } }, response => {
+ const chunks: Buffer[] = []; let bytes = 0
+ response.on('data', chunk => { bytes += chunk.length; if (bytes > 65536) request.destroy(new Error('bounded response exceeded')); else chunks.push(chunk) })
+ response.on('end', () => { try { resolve({ status: response.statusCode, headers: response.headers, body: bytes ? JSON.parse(Buffer.concat(chunks).toString()) : null }) } catch { reject(new Error('invalid bounded response')) } })
+ })
+ request.on('error', reject); request.setTimeout(15000, () => request.destroy(new Error('ordinary request deadline')))
+ if (body) request.write(JSON.stringify(body)); request.end()
+ })
+ }
+ async function counts() {
+ const rows = await prod.db.pool.query(`SELECT COUNT(DISTINCT a.id)::int AS total,
+ COUNT(DISTINCT a.id) FILTER (WHERE a.status <> 'finalized')::int AS active,
+ COUNT(DISTINCT t.jti) FILTER (WHERE t.status <> 'finalized' AND t.expires_at > NOW())::int AS active_tickets,
+ COUNT(DISTINCT r.id)::int AS reservations FROM llm_provider_attempts a
+ LEFT JOIN llm_provider_attempt_tickets t ON t.provider_attempt_id = a.id
+ LEFT JOIN budget_pending_reservations r ON r.id::text = a.budget_reservation_id OR r.task_ref = a.budget_reservation_id
+ WHERE a.invocation_id LIKE $1 AND a.host_ref = ANY($2::text[]) AND a.provider = 'grok-subscription'`, [`${options.runId}-%`, options.bindings.map((binding: any) => binding.hostRef)])
+ const value = rows.rows[0]
+ return { total: value.total, active: value.active, activeTickets: value.active_tickets, reservations: value.reservations }
+ }
+ async function hello(input: any) {
+ options = input
+ if (!/^pr806-memory-[a-f0-9]{12}$/.test(options.runId) || !Array.isArray(options.bindings) || options.bindings.length !== 2) throw new Error('invalid run binding')
+ process.env.LOG_LEVEL = 'silent'
+ prod = { config: require('./dist/config.js').config, db: require('./dist/db.js'), jwt: require('./dist/utils/auth/mcpHostJwtToken.js'),
+ connections: require('./dist/services/grokSubscriptionConnection.js'),
+ policy: require('./dist/middleware/llmProviderAttemptAdmissionLimits.js').LLM_PROVIDER_ATTEMPT_ADMISSION_POLICY,
+ grok: require('@clerum/grok-provider-attempt-contract'), finalize: require('./dist/services/grokProviderAttemptFinalization.js').finalizeGrokProviderAttempt,
+ opaque: require('./dist/services/llmProviderAttemptRedemption.js').opaqueAttemptReceipt }
+ if (!prod.config.grokSubscriptionEnabled || !prod.config.codexSubscriptionEnabled) throw new Error('both actual provider validators must be enabled')
+ inspector = await connectInspector(options.inspectorPort)
+ const server = await inspector.snapshot()
+ const flags = [...server.execArgv, ...server.nodeOptions.split(/\s+/).filter(Boolean)]
+ if (server.cwd !== process.cwd() || !server.argv[1]?.endsWith('/dist/main.js') ||
+ !flags.includes(`--max-old-space-size=${options.candidate.heapSizeMiB}`) || !flags.includes(`--inspect=127.0.0.1:${options.inspectorPort}`) ||
+ flags.some(flag => !/^--(?:max-old-space-size=\d+|inspect=127\.0\.0\.1:\d+|enable-source-maps)$/.test(flag))) throw new Error('actual server source/argv mismatch')
+ const expected = { maxInFlight: options.candidate.concurrency, readDeadlineMs: options.candidate.readDeadlineMs, workDeadlineMs: options.candidate.workDeadlineMs, closeGraceMs: options.candidate.closeGraceMs }
+ if (JSON.stringify(prod.policy) !== JSON.stringify(expected)) throw new Error('actual compiled admission policy differs from candidate')
+ if (!options.cookie) {
+ if (!options.operatorPassword || !options.operatorUser) throw new Error('operator material unavailable')
+ const login = await ordinary('POST', '/api/v1/admin/auth/login', { username: options.operatorUser, password: options.operatorPassword }, '')
+ const raw = (login.headers['set-cookie'] ?? []).find((value: string) => value.startsWith(`${cookieName}=`))
+ if (login.status !== 200 || !raw) throw new Error('operator login refused')
+ options.cookie = raw.split(';')[0].slice(cookieName.length + 1); options.operatorPassword = undefined
+ }
+ const me = await ordinary('GET', '/api/v1/admin/auth/me')
+ if (me.status !== 200 || me.body?.me?.username !== options.operatorUser) throw new Error('operator identity mismatch')
+ const parent = await ordinary('GET', `/api/v1/gfs/proxy/v1/resources/${options.gfsParentRid}`)
+ if (parent.status !== 200 || parent.body?.ok !== true || parent.body.data.kind !== 'directory' || !parent.body.data.name.includes(options.fixtureRunId)) throw new Error('GFS parent ownership unproved')
+ options.gfsParentResourceId = parent.body.data.resourceId
+ const bindings = []
+ for (const binding of options.bindings) {
+ if (!binding.connectionKey.includes(options.fixtureRunId)) throw new Error('connection is not fixture-owned')
+ const connection = await prod.connections.getSafeGrokSubscriptionConnection(prod.db.pool, binding.connectionKey)
+ if (!connection || connection.status !== 'connected' || connection.catalogStatus !== 'ready') throw new Error('actual connection unavailable')
+ const model = await prod.db.pool.query('SELECT enabled, stale FROM grok_catalog_models WHERE connection_id = $1 AND model = $2', [connection.id, 'grok-4.6'])
+ if (model.rows.length !== 1 || !model.rows[0].enabled || model.rows[0].stale) throw new Error('actual model not admitted')
+ const budget = await prod.db.pool.query('SELECT name FROM token_budgets WHERE id = $1', [binding.budgetId])
+ if (budget.rows.length !== 1 || !budget.rows[0].name.includes(options.fixtureRunId)) throw new Error('budget not fixture-owned')
+ const policyHash = prod.grok.computeGrokPolicyHash({ model: 'grok-4.6', catalogRevision: connection.catalogRevision, credentialRevision: connection.credentialRevision, connectionKey: binding.connectionKey })
+ bindings.push({ ...binding, connectionId: connection.id, policyRevision: connection.catalogRevision, policyHash })
+ }
+ options.bindings = bindings
+ if ((await counts()).total !== 0) throw new Error('run id is not fresh')
+ return { bindings, policy: prod.policy, compiledPolicySha256: hash(fs.readFileSync('./dist/middleware/llmProviderAttemptAdmissionLimits.js')),
+ server: { ...server, nodeOptions: undefined }, cgroup: cgroup(), operatorSessionValidated: true, gfsParentResourceId: options.gfsParentResourceId,
+ auxiliary: { pid: process.pid, memory: process.memoryUsage(), inputHighWaterMark: process.stdin.readableHighWaterMark } }
+ }
+ async function unlock() {
+ if (lockClient) { const client = lockClient; lockClient = undefined; try { await client.query('ROLLBACK') } finally { client.release() } }
+ return { released: true }
+ }
+ function open(input: any) {
+ if (sockets.size >= 8 || sockets.has(input.requestId) || !Number.isSafeInteger(input.length) || input.length < 1 || input.length > 36 * 1024 * 1024) throw new Error('invalid bounded request')
+ let route: string; const headers: Record<string, string> = { 'content-type': 'application/json', 'content-length': String(input.length) }
+ if (input.route === 'authorize') {
+ const binding = options.bindings.find((value: any) => value.hostRef === input.hostRef)
+ if (!binding) throw new Error('Host not owned')
+ const issued = prod.jwt.issueMcpHostAccessJwt(prod.config.hostsNamespace, 'standalone', [binding.hostRef], { workflowControlScopes: ['llm:grok:execute', 'llm:codex:execute'], hccCredential: { hostUid: binding.hostUid } })
+ headers.authorization = `Bearer ${issued.token}`; route = '/api/v1/mcp-host/llm/provider-attempts/authorize'
+ } else if (input.route === 'gfs') {
+ if (!input.name?.startsWith(`${options.runId}-`)) throw new Error('GFS filename not owned')
+ headers.cookie = `${cookieName}=${options.cookie}`; route = `/api/v1/gfs/proxy/v1/resources/${options.gfsParentRid}/children`
+ } else throw new Error('invalid route')
+ const state: any = { input, acceptedWriteBytes: 0, maxWritableBytes: 0, closedByClient: false }
+ const request = http.request({ hostname: '127.0.0.1', port: prod.config.port, method: 'POST', path: route, headers }, response => {
+ const chunks: Buffer[] = []; let bytes = 0
+ response.on('data', chunk => { bytes += chunk.length; if (bytes > 65536) request.destroy(new Error('response bound')); else chunks.push(chunk) })
+ response.on('end', () => {
+ try {
+ const body = bytes ? JSON.parse(Buffer.concat(chunks).toString()) : null
+ let data: any = { status: response.statusCode, acceptedWriteBytes: state.acceptedWriteBytes, maxWritableBytes: state.maxWritableBytes, closedByClient: state.closedByClient, error: knownErrors.has(body?.error) ? body.error : undefined }
+ if (input.route === 'authorize' && response.statusCode === 200) data = { ...data, providerAttemptId: body.providerAttemptId, requestHash: body.requestHash }
+ if (input.route === 'gfs' && response.statusCode === 201 && body?.ok === true) {
+ const value = body.data
+ if (value.name !== input.name || value.parentResourceId !== options.gfsParentResourceId || value.bytes !== input.decodedBytes || value.kind !== 'file') throw new Error('GFS receipt mismatch')
+ created.set(value.rid, { rid: value.rid, resourceId: value.resourceId, name: value.name, bytes: value.bytes, parentResourceId: value.parentResourceId }); data.resource = created.get(value.rid)
+ }
+ emit({ event: 'response', requestId: input.requestId, data }); sockets.delete(input.requestId)
+ } catch { emit({ event: 'response', requestId: input.requestId, data: { failed: true, code: 'INVALID_BUSINESS_RESPONSE' } }); sockets.delete(input.requestId) }
+ })
+ })
+ state.request = request; sockets.set(input.requestId, state)
+ request.on('error', () => { sockets.delete(input.requestId); emit({ event: 'response', requestId: input.requestId, data: { transportClosed: true, acceptedWriteBytes: state.acceptedWriteBytes, closedByClient: state.closedByClient } }) })
+ request.setTimeout(options.candidate.workDeadlineMs + options.candidate.readDeadlineMs + 5000, () => request.destroy(new Error('HTTP deadline')))
+ request.flushHeaders(); return { opened: true }
+ }
+ async function download(rid: string) {
+ const owned = created.get(rid); if (!owned) throw new Error('download not owned')
+ return new Promise((resolve, reject) => {
+ const digest = createHash('sha256'); let bytes = 0
+ const request = http.get({ hostname: '127.0.0.1', port: prod.config.port, path: `/api/v1/gfs/proxy/v1/resources/${rid}/content`, headers: { cookie: `${cookieName}=${options.cookie}` } }, response => {
+ if (response.statusCode !== 200) { response.resume(); reject(new Error('GFS read refused')); return }
+ response.on('data', chunk => { bytes += chunk.length; digest.update(chunk); if (bytes > owned.bytes) request.destroy(new Error('GFS overflow')) })
+ response.on('end', () => bytes === owned.bytes ? resolve({ rid, bytes, sha256: digest.digest('hex') }) : reject(new Error('GFS incomplete')))
+ }); request.on('error', reject); request.setTimeout(15000, () => request.destroy(new Error('GFS deadline')))
+ })
+ }
+ async function cleanup() {
+ for (const state of sockets.values()) { state.closedByClient = true; state.request.destroy() }
+ await unlock()
+ const rows = await prod.db.pool.query(`SELECT a.id, a.request_hash, a.connection_id, t.jti::text FROM llm_provider_attempts a JOIN llm_provider_attempt_tickets t ON t.provider_attempt_id = a.id WHERE a.invocation_id LIKE $1 AND a.host_ref = ANY($2::text[]) AND a.provider = 'grok-subscription'`, [`${options.runId}-%`, options.bindings.map((value: any) => value.hostRef)])
+ for (const row of rows.rows) {
+ if (!options.bindings.some((value: any) => value.connectionId === row.connection_id)) throw new Error('attempt owner changed')
+ const opaque = prod.opaque({ jti: row.jti, providerAttemptId: row.id, requestHash: row.request_hash })
+ await prod.finalize({ attemptReceipt: opaque, receipt: { schemaVersion: 'grok-attempt-receipt.v1', providerAttemptId: row.id, requestHash: row.request_hash, outcome: 'canceled' } })
+ }
+ for (const [rid, expected] of created) {
+ const current = await ordinary('GET', `/api/v1/gfs/proxy/v1/resources/${rid}`)
+ if (current.status !== 200 || current.body?.data?.resourceId !== expected.resourceId || current.body.data.name !== expected.name || current.body.data.parentResourceId !== expected.parentResourceId) throw new Error('GFS owner changed')
+ const deleted = await ordinary('DELETE', `/api/v1/gfs/proxy/v1/resources/${rid}`)
+ if (deleted.status !== 200 || deleted.body?.data?.deleted !== true) throw new Error('GFS cleanup failed')
+ created.delete(rid)
+ }
+ const after = await counts()
+ if (after.active || after.activeTickets || after.reservations) throw new Error('terminal lifetime incomplete')
+ return { ...after, ownedGfsRemaining: created.size, retainedFinalizedAuditRows: rows.rows.length }
+ }
+ async function command(input: any): Promise<unknown> {
+ if (input.kind === 'hello') return hello(input.options)
+ if (!prod) throw new Error('runtime admission required')
+ if (input.kind === 'open') return open(input)
+ if (input.kind === 'end') { sockets.get(input.requestId)?.request.end(); return { ended: true } }
+ if (input.kind === 'close') { const state = sockets.get(input.requestId); if (state) { state.closedByClient = true; state.request.destroy() }; return { clientClosed: true } }
+ if (input.kind === 'counts') return counts()
+ if (input.kind === 'lock') {
+ if (lockClient) throw new Error('lock already held')
+ lockClient = await prod.db.pool.connect(); await lockClient.query('BEGIN'); await lockClient.query('SET LOCAL statement_timeout = 60000')
+ await lockClient.query('SELECT id FROM token_budgets WHERE id = ANY($1::uuid[]) ORDER BY id FOR UPDATE', [options.bindings.map((value: any) => value.budgetId)])
+ return { locked: true }
+ }
+ if (input.kind === 'unlock') return unlock()
+ if (input.kind === 'waiting') {
+ if (!lockClient) return { count: 0 }
+ const pid = (await lockClient.query('SELECT pg_backend_pid() AS pid')).rows[0].pid
+ return (await prod.db.pool.query("SELECT COUNT(*)::int AS count FROM pg_stat_activity WHERE $1::int = ANY(pg_blocking_pids(pid)) AND wait_event_type = 'Lock' AND usename = current_user", [pid])).rows[0]
+ }
+ if (input.kind === 'sample') return { server: await inspector.snapshot(), cgroup: cgroup() }
+ if (input.kind === 'gc') return { gc: await inspector.forceGc(), cgroup: cgroup() }
+ if (input.kind === 'coverage-start') { await inspector.startCoverage(); return { started: true } }
+ if (input.kind === 'coverage') return inspector.coverage()
+ if (input.kind === 'coverage-stop') { await inspector.stopCoverage(); return { stopped: true } }
+ if (input.kind === 'download') return download(input.rid)
+ if (input.kind === 'health') return { status: (await ordinary('GET', '/health', undefined, '')).status }
+ if (input.kind === 'cleanup') return cleanup()
+ throw new Error('unknown command')
+ }
+ let header = Buffer.alloc(0), frame: Frame, remaining = 0
+ try {
+ for await (const chunk of process.stdin) {
+ let offset = 0
+ while (offset < chunk.length) {
+ if (remaining) {
+ const length = Math.min(remaining, chunk.length - offset), state = sockets.get(frame.requestId!)
+ if (!state) throw new Error('request closed before streaming completed')
+ state.acceptedWriteBytes += length
+ const writable = state.request.write(chunk.subarray(offset, offset + length))
+ state.maxWritableBytes = Math.max(state.maxWritableBytes, state.request.writableLength)
+ if (!writable) await once(state.request, 'drain')
+ offset += length; remaining -= length
+ if (!remaining) emit({ callId: frame.callId, data: { streamedBytes: frame.length } })
+ } else {
+ const newline = chunk.indexOf(10, offset), end = newline < 0 ? chunk.length : newline
+ header = Buffer.concat([header, chunk.subarray(offset, end)])
+ if (header.length > 32768) throw new Error('header bound exceeded')
+ offset = newline < 0 ? end : end + 1
+ if (newline < 0) continue
+ frame = JSON.parse(header.toString()); header = Buffer.alloc(0)
+ if (frame.kind === 'write') {
+ if (!Number.isSafeInteger(frame.length) || frame.length! < 1 || frame.length! > 65536) throw new Error('body frame bound exceeded')
+ remaining = frame.length!
+ } else { try { emit({ callId: frame.callId, data: await command(frame) }) } catch { emit({ callId: frame.callId, failed: true, code: 'REAL_RUNTIME_COMMAND_FAILED' }) } }
+ }
+ }
+ }
+ if (remaining || header.length) throw new Error('incomplete frame')
+ } catch { emit({ fatal: true, code: 'COMPANION_PROTOCOL_FAILED' }); process.exitCode = 1 }
+ finally { for (const state of sockets.values()) state.request.destroy(); await unlock().catch(() => {}); inspector?.close(); if (prod) await prod.db.pool.end().catch(() => {}) }
+}
diff --git a/scripts/tests/lib/control-api-authorize-memory-fixtures.mjs b/scripts/tests/lib/control-api-authorize-memory-fixtures.mjs
new file mode 100644
index 000000000..f2efce45e
--- /dev/null
+++ b/scripts/tests/lib/control-api-authorize-memory-fixtures.mjs
@@ -0,0 +1,115 @@
+/** Physical synthetic request shapes for the real HTTP/PG memory experiment. No runtime or credential access. */
+import { createHash } from 'node:crypto'
+import { createRequire } from 'node:module'
+const require = createRequire(import.meta.url)
+const grok = require('../../../packages/grok-provider-attempt-contract/index.cjs')
+const codex = require('../../../packages/llm-provider-attempt-contract/index.cjs')
+const imageContainers = require('../../../packages/llm-provider-attempt-contract/testImageFixtures.cjs')
+const nativeRequire = createRequire(new URL('../../../mcp-host/package.json', import.meta.url))
+export const MIB = 1024 * 1024
+export const SHAPES = ['visual-35mib', 'worst-structure', 'max-containers', 'max-members', 'max-elements', 'wide-strings']
+export const sha256 = value => createHash('sha256').update(value).digest('hex')
+let imageBase64
+function physicalPng() {
+ if (!imageBase64) {
+ const { createCanvas } = nativeRequire('@napi-rs/canvas')
+ const canvas = createCanvas(8, 8), context = canvas.getContext('2d')
+ context.fillStyle = '#2480ca'; context.fillRect(0, 0, 8, 8)
+ const bytes = imageContainers.padPngToSize(canvas.toBuffer('image/png'), grok.GROK_VISUAL_LIMITS.maxTotalImageBytes)
+ imageBase64 = bytes.toString('base64')
+ }
+ return imageBase64
+}
+export function structureOf(root) {
+ const pending = [[root, 1]]
+ let containers = 0, members = 0, elements = 0, maxDepth = 0, arrayElements = 0
+ while (pending.length) {
+ const [value, depth] = pending.pop(); elements++
+ if (!value || typeof value !== 'object') continue
+ containers++; maxDepth = Math.max(maxDepth, depth)
+ const values = Array.isArray(value) ? value : Object.values(value)
+ if (Array.isArray(value)) arrayElements += values.length
+ else members += Object.keys(value).length
+ for (const child of values) pending.push([child, depth + 1])
+ }
+ return { containers, members, elements, arrayElements, maxDepth }
+}
+function stressTree(request, shape) {
+ if (!['worst-structure', 'max-containers', 'max-members', 'max-elements'].includes(shape)) return
+ const parameters = { type: 'object', properties: { stress: { type: 'array', items: {}, default: [] } } }
+ request.tools = [{ name: 'memory_fixture_shape', description: 'Synthetic valid schema used only by the local memory experiment', parameters }]
+ const values = parameters.properties.stress.default
+ const base = structureOf(request)
+ if (shape === 'max-members') {
+ parameters.properties.stress.type = 'object'
+ delete parameters.properties.stress.items
+ parameters.properties.stress.default = {}
+ const target = grok.LIMITS.maxRequestMembers - structureOf(request).members
+ for (let index = 0; index < target; index++) parameters.properties.stress.default[`m${index}`] = 0
+ } else if (shape === 'max-elements') {
+ for (let index = 0; index < grok.LIMITS.maxRequestElements - base.elements; index++) values.push(0)
+ } else {
+ const count = grok.LIMITS.maxRequestContainers - base.containers
+ for (let index = 0; index < count; index++) values.push({})
+ if (shape === 'worst-structure') {
+ const members = grok.LIMITS.maxRequestMembers - structureOf(request).members
+ if (members > values.length) throw new Error('Fixture structure cannot reach member target')
+ for (let index = 0; index < members; index++) values[index].a = 0
+ const elements = grok.LIMITS.maxRequestElements - structureOf(request).elements
+ for (let index = 0; index < elements; index++) values.push(0)
+ }
+ }
+}
+export function buildAuthorizeFixture({ shape, binding, invocationId, small = false }) {
+ if (!SHAPES.includes(shape) || !binding?.policyHash || !Number.isSafeInteger(binding.policyRevision) || !/^[A-Za-z0-9-]{1,128}$/.test(invocationId)) throw new Error('Invalid physical fixture binding')
+ const request = { schemaVersion: 'grok-completion-request.v2', requestId: invocationId, idempotencyKey: invocationId,
+ provider: 'grok-subscription', model: 'grok-4.6', messages: [{ role: 'user', content: '', contentParts: [{ type: 'text', text: '' }] }] }
+ if (!small) request.messages[0].contentParts.push({ type: 'image', mimeType: 'image/png', data: physicalPng(), source: { kind: 'tool', attachmentId: invocationId, toolCallId: invocationId } })
+ stressTree(request, small ? 'visual-35mib' : shape)
+ const envelope = { request, invocationId, attemptGeneration: 1, providerAttemptIndex: 1, policyRevision: binding.policyRevision, policyHash: binding.policyHash }
+ if (small) {
+ request.messages[0].content = 'memory experiment small control'
+ request.messages[0].contentParts[0].text = request.messages[0].content
+ } else {
+ const target = grok.LIMITS.maxVisualRequestBodyBytes - 4096
+ const room = target - Buffer.byteLength(JSON.stringify(envelope))
+ if (room <= 0) throw new Error('Physical structure already exceeds target body size')
+ const unit = shape === 'wide-strings' ? 'Ā' : 'x'
+ const count = Math.floor(room / (2 * Buffer.byteLength(unit)))
+ const text = unit.repeat(count)
+ request.messages[0].content = text; request.messages[0].contentParts[0].text = text
+ }
+ const serialized = Buffer.from(JSON.stringify(envelope))
+ const parsed = grok.parseGrokCompletionRequest(request)
+ if (!parsed.ok) throw new Error(`Physical synthetic ${shape} fixture is invalid: ${parsed.code}: ${parsed.message}`)
+ const scan = grok.scanJsonStructure(serialized, grok.BODY_STRUCTURE_LIMITS)
+ if (!Number.isSafeInteger(scan.containers) || !Number.isSafeInteger(scan.elements)) throw new Error('Physical scanner observations missing')
+ if (grok.measureNonImageAuthorizeBytes(envelope) > grok.LIMITS.maxRequestBodyBytes) throw new Error('Physical fixture exceeds actual authorize non-image budget')
+ const counts = structureOf(request)
+ if (!small && (serialized.length < grok.LIMITS.maxVisualRequestBodyBytes - 8192 || serialized.length > grok.LIMITS.maxVisualRequestBodyBytes)) throw new Error('Physical visual fixture is underfilled or oversized')
+ if (shape === 'worst-structure' && !small && (counts.containers !== grok.LIMITS.maxRequestContainers || counts.members !== grok.LIMITS.maxRequestMembers || counts.elements !== grok.LIMITS.maxRequestElements)) throw new Error('Worst structure fixture misses an enforced maximum')
+ return { bytes: serialized, request, envelope, metadata: { shape: small ? 'small-control' : shape, bytes: serialized.length,
+ sha256: sha256(serialized), requestHash: grok.hashGrokCompletionRequest(request), nonImageAuthorizeBytes: grok.measureNonImageAuthorizeBytes(envelope),
+ decodedImageBytes: small ? 0 : grok.GROK_VISUAL_LIMITS.maxTotalImageBytes, imageSha256: small ? [] : [sha256(Buffer.from(physicalPng(), 'base64'))], structure: counts } }
+}
+export function buildRejectedFixture(accepted, kind) {
+ const copy = structuredClone(accepted.envelope)
+ if (kind === 'codex-cap') {
+ copy.request.provider = 'codex-subscription'; copy.request.schemaVersion = 'codex-completion-request.v2'
+ const parsed = codex.parseCodexCompletionRequest(copy.request)
+ if (parsed.ok || parsed.code !== 'limit') throw new Error('Lower provider cap fixture does not actually exceed Codex bounds')
+ } else if (kind === 'elements') {
+ const request = copy.request
+ request.messages[0].content = ''; request.messages[0].contentParts[0].text = ''
+ request.tools = [{ name: 'memory_fixture_rejection', description: 'Synthetic structure refusal control', parameters: { type: 'array', default: Array(grok.BODY_STRUCTURE_LIMITS.maxElements + 1).fill(0) } }]
+ } else throw new Error('Unknown physical rejection shape')
+ const bytes = Buffer.from(JSON.stringify(copy))
+ return { bytes, envelope: copy, metadata: { shape: `rejected-${kind}`, bytes: bytes.length, sha256: sha256(bytes), expectedStatus: 413, expectedError: 'payload_too_large' } }
+}
+export function buildGfsFixture({ name, sequence }) {
+ if (!/^[A-Za-z0-9.-]{1,200}$/.test(name) || !Number.isSafeInteger(sequence) || sequence < 0 || sequence > 255) throw new Error('Invalid GFS fixture binding')
+ const content = Buffer.alloc(16 * MIB, sequence)
+ const bytes = Buffer.from(JSON.stringify({ name, kind: 'file', contentBase64: content.toString('base64') }))
+ if (bytes.length >= 24 * MIB) throw new Error('GFS fixture exceeds the actual GFSC parser cap')
+ return { bytes, metadata: { name, decodedBytes: content.length, serializedBytes: bytes.length, sha256: sha256(content), serializedSha256: sha256(bytes), route: '/api/v1/gfs/proxy/v1/resources/{parentRid}/children' } }
+}
diff --git a/scripts/tests/lib/control-api-authorize-memory-inspector.mjs b/scripts/tests/lib/control-api-authorize-memory-inspector.mjs
new file mode 100644
index 000000000..393758f3f
--- /dev/null
+++ b/scripts/tests/lib/control-api-authorize-memory-inspector.mjs
@@ -0,0 +1,87 @@
+/** Native loopback inspector reader. It creates no production endpoint and never retains request bodies. */
+export class InspectorClient {
+ constructor(socket, timeoutMs = 5000) {
+ this.socket = socket; this.timeoutMs = timeoutMs; this.nextId = 1; this.pending = new Map()
+ socket.addEventListener('message', event => {
+ let reply
+ try { reply = JSON.parse(String(event.data)) } catch { this.fail(new Error('Inspector returned invalid JSON')); return }
+ if (!Number.isSafeInteger(reply.id)) return
+ const entry = this.pending.get(reply.id); if (!entry) return
+ this.pending.delete(reply.id); clearTimeout(entry.timer)
+ if (reply.error) entry.reject(new Error('Inspector rejected a required command'))
+ else entry.resolve(reply.result)
+ })
+ socket.addEventListener('close', () => this.fail(new Error('Inspector closed before observations completed')))
+ socket.addEventListener('error', () => this.fail(new Error('Inspector transport failed')))
+ }
+ fail(error) { for (const item of this.pending.values()) { clearTimeout(item.timer); item.reject(error) }; this.pending.clear() }
+ call(method, params = {}) {
+ const id = this.nextId++
+ return new Promise((resolve, reject) => {
+ const timer = setTimeout(() => { this.pending.delete(id); reject(new Error(`Inspector deadline: ${method}`)) }, this.timeoutMs)
+ this.pending.set(id, { resolve, reject, timer })
+ try { this.socket.send(JSON.stringify({ id, method, params })) } catch { this.pending.delete(id); clearTimeout(timer); reject(new Error('Inspector send failed')) }
+ })
+ }
+ async snapshot() {
+ const reply = await this.call('Runtime.evaluate', { expression: "JSON.stringify({pid:process.pid,nodeVersion:process.versions.node,argv:process.argv,execArgv:process.execArgv,nodeOptions:process.env.NODE_OPTIONS||'',cwd:process.cwd(),at:Date.now(),memory:process.memoryUsage()})", returnByValue: true })
+ if (reply?.exceptionDetails || typeof reply?.result?.value !== 'string') throw new Error('Actual server metrics unavailable')
+ const data = JSON.parse(reply.result.value)
+ for (const name of ['rss', 'heapUsed', 'heapTotal', 'external', 'arrayBuffers']) if (!Number.isSafeInteger(data.memory?.[name]) || data.memory[name] < 0) throw new Error('Actual server memory metrics incomplete')
+ if (!Number.isSafeInteger(data.pid) || !Number.isSafeInteger(data.at) || !/^24\./.test(data.nodeVersion)) throw new Error('Actual server process identity is invalid')
+ return data
+ }
+ async forceGc() {
+ const before = await this.snapshot()
+ await this.call('HeapProfiler.enable')
+ await this.call('HeapProfiler.collectGarbage')
+ const after = await this.snapshot()
+ if (before.pid !== after.pid || after.at < before.at) throw new Error('GC metrics belong to another process/window')
+ return { kind: 'native-HeapProfiler.collectGarbage', forced: true, before, after }
+ }
+ async startCoverage() { await this.call('Profiler.enable'); await this.call('Profiler.startPreciseCoverage', { callCount: true, detailed: false }) }
+ async coverage() {
+ const reply = await this.call('Profiler.takePreciseCoverage')
+ if (!Array.isArray(reply?.result)) throw new Error('Native coverage unavailable')
+ const names = [
+ { key: 'parser', suffix: '/body-parser/lib/types/json.js', name: 'jsonParser' },
+ { key: 'authorizer', suffix: '/services/llmProviderAttemptAuthorizer.js', name: 'authorizeLlmProviderAttempt' },
+ ]
+ const observed = {}
+ for (const target of names) {
+ const scripts = reply.result.filter(script => String(script.url).endsWith(target.suffix))
+ const functions = scripts.flatMap(script => script.functions).filter(fn => fn.functionName === target.name)
+ if (!functions.length || functions.some(fn => !Number.isSafeInteger(fn.ranges?.[0]?.count))) throw new Error(`Actual ${target.key} coverage function is missing`)
+ observed[target.key] = functions.reduce((sum, fn) => sum + fn.ranges[0].count, 0)
+ }
+ return observed
+ }
+ async stopCoverage() { await this.call('Profiler.stopPreciseCoverage'); await this.call('Profiler.disable') }
+ close() { this.fail(new Error('Inspector disposed')); this.socket.close() }
+}
+export async function connectInspector(port) {
+ if (!Number.isSafeInteger(port) || port < 1024 || port > 65535) throw new Error('Invalid loopback inspector port')
+ const endpoint = new URL('http://127.0.0.1'); endpoint.port = String(port); endpoint.pathname = '/json/list'
+ const response = await fetch(endpoint, { signal: AbortSignal.timeout(5000) })
+ if (!response.ok) throw new Error('Loopback inspector discovery failed')
+ const list = await response.json()
+ if (!Array.isArray(list) || list.length !== 1) throw new Error('Loopback inspector target is ambiguous')
+ const url = new URL(list[0].webSocketDebuggerUrl)
+ if (url.protocol !== 'ws:' || url.hostname !== '127.0.0.1' || Number(url.port) !== port) throw new Error('Inspector is exposed outside its loopback boundary')
+ const socket = new WebSocket(url)
+ await new Promise((resolve, reject) => {
+ const timer = setTimeout(() => { socket.close(); reject(new Error('Inspector connection deadline')) }, 5000)
+ socket.addEventListener('open', () => { clearTimeout(timer); resolve() }, { once: true })
+ socket.addEventListener('error', () => { clearTimeout(timer); reject(new Error('Inspector connection failed')) }, { once: true })
+ })
+ return new InspectorClient(socket)
+}
+export function parseCgroup(raw) {
+ const number = value => { if (!/^\d+$/.test(String(value).trim())) throw new Error('Cgroup metric is unavailable'); const parsed = Number(String(value).trim()); if (!Number.isSafeInteger(parsed) || parsed < 0) throw new Error('Cgroup metric is invalid'); return parsed }
+ const current = number(raw.current), peak = number(raw.peak), limit = number(raw.limit)
+ if (limit !== 768 * 1024 * 1024 || peak < current) throw new Error('Actual cgroup is not the required 768Mi limit/window')
+ const events = {}
+ for (const line of String(raw.events).trim().split('\n')) { const [key, value] = line.trim().split(/\s+/); events[key] = number(value) }
+ for (const key of ['max', 'oom', 'oom_kill']) if (!Number.isSafeInteger(events[key])) throw new Error('Cgroup events are incomplete')
+ return { current, peak, limit, events }
+}
diff --git a/scripts/tests/measure-control-api-authorize-memory.test.mjs b/scripts/tests/measure-control-api-authorize-memory.test.mjs
new file mode 100644
index 000000000..6af465aa6
--- /dev/null
+++ b/scripts/tests/measure-control-api-authorize-memory.test.mjs
@@ -0,0 +1,79 @@
+import assert from 'node:assert/strict'
+import test from 'node:test'
+import { createRequire, stripTypeScriptTypes } from 'node:module'
+import fs from 'node:fs'
+import { buildAuthorizeFixture, buildRejectedFixture, buildGfsFixture, MIB } from './lib/control-api-authorize-memory-fixtures.mjs'
+import { InspectorClient, parseCgroup } from './lib/control-api-authorize-memory-inspector.mjs'
+
+const require = createRequire(import.meta.url)
+const grok = require('../../packages/grok-provider-attempt-contract/index.cjs')
+const binding = { policyRevision: 1, policyHash: 'a'.repeat(64) }
+
+test('physical visual/wide/worst fixtures reach35MiB under actual authorizer8MiB share and structure bounds', () => {
+ for (const shape of ['visual-35mib', 'wide-strings', 'worst-structure']) {
+ const fixture = buildAuthorizeFixture({ shape, binding, invocationId: `unit-${shape}` })
+ assert(fixture.bytes.length >= 35 * MIB - 8192)
+ assert(fixture.bytes.length <= 35 * MIB)
+ assert(fixture.metadata.nonImageAuthorizeBytes <= 8 * MIB)
+ assert.equal(fixture.metadata.decodedImageBytes, 20 * MIB)
+ assert.match(fixture.metadata.sha256, /^[a-f0-9]{64}$/)
+ assert.equal(grok.parseGrokCompletionRequest(fixture.request).ok, true)
+ if (shape === 'worst-structure') {
+ assert.equal(fixture.metadata.structure.containers, grok.LIMITS.maxRequestContainers)
+ assert.equal(fixture.metadata.structure.members, grok.LIMITS.maxRequestMembers)
+ assert.equal(fixture.metadata.structure.elements, grok.LIMITS.maxRequestElements)
+ }
+ }
+})
+test('rejected fixtures violate the intended physical bound rather than an invented model or malformed container', () => {
+ const valid = buildAuthorizeFixture({ shape: 'visual-35mib', binding, invocationId: 'unit-rejection' })
+ assert.equal(buildRejectedFixture(valid, 'codex-cap').metadata.expectedStatus, 413)
+ const elements = buildRejectedFixture(valid, 'elements')
+ assert.throws(() => grok.scanJsonStructure(elements.bytes, grok.BODY_STRUCTURE_LIMITS))
+})
+test('legacy GFS is actual16MiB contentBase64 below GFSC24MiB and reports decoded/serialized digests', () => {
+ const fixture = buildGfsFixture({ name: 'unit-owned.bin', sequence: 37 })
+ const parsed = JSON.parse(fixture.bytes.toString())
+ assert.equal(Buffer.from(parsed.contentBase64, 'base64').length, 16 * MIB)
+ assert(fixture.bytes.length < 24 * MIB)
+ assert.equal(fixture.metadata.decodedBytes, 16 * MIB)
+ assert.notEqual(fixture.metadata.sha256, fixture.metadata.serializedSha256)
+})
+test('cgroup requires actual768Mi/current/peak/events and never invents zeros for unavailable fields', () => {
+ const raw = { limit: String(768 * MIB), current: '100', peak: '200', events: 'low 0\nhigh 0\nmax 0\noom 0\noom_kill 0' }
+ assert.equal(parseCgroup(raw).peak, 200)
+ for (const patch of [{ limit: 'max' }, { peak: undefined }, { events: 'low 0' }, { current: '300' }]) assert.throws(() => parseCgroup({ ...raw, ...patch }))
+})
+class UnitSocket extends EventTarget {
+ constructor() { super(); this.commands = []; this.heap = 100 }
+ send(raw) {
+ const request = JSON.parse(raw); this.commands.push(request.method)
+ let result = {}
+ if (request.method === 'Runtime.evaluate') result = { result: { value: JSON.stringify({ pid: 4, nodeVersion: '24.18.0', at: this.commands.length, memory: { rss: 200, heapUsed: this.heap, heapTotal: 120, external: 20, arrayBuffers: 10 } }) } }
+ if (request.method === 'HeapProfiler.collectGarbage') this.heap = 70
+ queueMicrotask(() => this.dispatchEvent(new MessageEvent('message', { data: JSON.stringify({ id: request.id, result }) })))
+ }
+ close() { this.dispatchEvent(new Event('close')) }
+}
+test('forced-GC receipt requires native collectGarbage acknowledgement between actual process snapshots', async () => {
+ const socket = new UnitSocket(), inspector = new InspectorClient(socket)
+ const result = await inspector.forceGc()
+ assert.equal(result.kind, 'native-HeapProfiler.collectGarbage')
+ assert.equal(result.after.memory.heapUsed, 70)
+ assert.deepEqual(socket.commands, ['Runtime.evaluate', 'HeapProfiler.enable', 'HeapProfiler.collectGarbage', 'Runtime.evaluate'])
+ inspector.close()
+})
+test('missing nativeGC/metrics and incomplete coverage are failures, never post-quiescence substituted as forcedGC', async () => {
+ const socket = new UnitSocket(), inspector = new InspectorClient(socket)
+ await assert.rejects(() => inspector.coverage(), /coverage unavailable/)
+ socket.send = raw => { const request = JSON.parse(raw); queueMicrotask(() => socket.dispatchEvent(new MessageEvent('message', { data: JSON.stringify({ id: request.id, error: { code: -1 } }) }))) }
+ await assert.rejects(() => inspector.forceGc(), /rejected/)
+ inspector.close()
+})
+test('companion is valid native Node24 TypeScript source; it contains no role elevation or delete of audit rows', () => {
+ const source = fs.readFileSync(new URL('./lib/control-api-authorize-memory-companion.ts', import.meta.url), 'utf8')
+ const compiled = stripTypeScriptTypes(source)
+ assert(compiled.includes('issueMcpHostAccessJwt'))
+ assert(compiled.includes('finalizeGrokProviderAttempt'))
+ assert(!/DELETE FROM llm_provider_attempt|ALTER ROLE|DROP TABLE/.test(compiled))
+})