Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(codex): preserve approved tools and defer large catalogs
Alfredo Lopeza447bc9fe056
diff --git a/Makefile b/Makefileindex 9558b1b98..0ae64b8de 100644--- a/Makefile+++ b/Makefile@@ -254,6 +254,36 @@ minikube-build-custom-coordinator-fixture-body:@MINIKUBE_PROFILE="$(MINIKUBE_PROFILE)" scripts/minikube/build-images.sh --only=workflow-custom-sdk-e2e.PHONY: minikube-build-e2e-fixtures minikube-build-e2e-fixtures-body++.PHONY: minikube-build-codex-approved-tools-fixtures minikube-build-codex-approved-tools-fixtures-body+minikube-build-codex-approved-tools-fixtures: ## Acquire optional Codex tools fixture images before T2 reconcile+ @T2_PROJECT_DIR="$(CURDIR)" T2_PROFILE="$(MINIKUBE_PROFILE)" T2_CONTEXT="$(MINIKUBE_PROFILE)" \+ T2_SKIP_LOCK="$(T2_SKIP_LOCK)" T2_LOCK_TOKEN="$(T2_LOCK_TOKEN)" \+ bash scripts/minikube/with-t2-mutation-lock.sh -- \+ $(MAKE) --no-print-directory minikube-build-codex-approved-tools-fixtures-body++minikube-build-codex-approved-tools-fixtures-body:+ @bash scripts/minikube/require-t2-mutation-lock.sh+ @MINIKUBE_PROFILE="$(MINIKUBE_PROFILE)" scripts/minikube/build-images.sh --only=codex-llm-proxy+ @MINIKUBE_PROFILE="$(MINIKUBE_PROFILE)" scripts/minikube/build-images.sh --only=codex-approved-tools-proxy-e2e+ @MINIKUBE_PROFILE="$(MINIKUBE_PROFILE)" scripts/minikube/build-images.sh --only=codex-approved-tools-mcp-e2e++.PHONY: minikube-prepare-codex-approved-tools minikube-run-codex-approved-tools minikube-restore-codex-approved-tools+minikube-prepare-codex-approved-tools: ## Prepare isolated deterministic tools fixtures; requires prior image acquisition and fresh run directory+ @T2_PROJECT_DIR="$(CURDIR)" T2_PROFILE="$(MINIKUBE_PROFILE)" T2_CONTEXT="$(MINIKUBE_PROFILE)" \+ T2_SKIP_LOCK="$(T2_SKIP_LOCK)" T2_LOCK_TOKEN="$(T2_LOCK_TOKEN)" \+ bash scripts/minikube/with-t2-mutation-lock.sh -- node scripts/e2e/prepare-codex-approved-tools.mjs prepare++minikube-run-codex-approved-tools: ## Prepare, run visible deterministic E2E and restore production proxy image in owned Minikube+ @T2_PROJECT_DIR="$(CURDIR)" T2_PROFILE="$(MINIKUBE_PROFILE)" T2_CONTEXT="$(MINIKUBE_PROFILE)" \+ T2_SKIP_LOCK="$(T2_SKIP_LOCK)" T2_LOCK_TOKEN="$(T2_LOCK_TOKEN)" \+ bash scripts/minikube/with-t2-mutation-lock.sh -- node scripts/e2e/prepare-codex-approved-tools.mjs run++minikube-restore-codex-approved-tools: ## Restore recorded proxy image/env and close only this fixture run's owned forwards+ @T2_PROJECT_DIR="$(CURDIR)" T2_PROFILE="$(MINIKUBE_PROFILE)" T2_CONTEXT="$(MINIKUBE_PROFILE)" \+ T2_SKIP_LOCK="$(T2_SKIP_LOCK)" T2_LOCK_TOKEN="$(T2_LOCK_TOKEN)" \+ bash scripts/minikube/with-t2-mutation-lock.sh -- node scripts/e2e/prepare-codex-approved-tools.mjs restore+minikube-build-e2e-fixtures: ## Build the two unpublished coordinator E2E fixtures under one mutation lease@T2_PROJECT_DIR="$(CURDIR)" T2_PROFILE="$(MINIKUBE_PROFILE)" T2_CONTEXT="$(MINIKUBE_PROFILE)" \T2_SKIP_LOCK="$(T2_SKIP_LOCK)" T2_LOCK_TOKEN="$(T2_LOCK_TOKEN)" \diff --git a/codex-llm-proxy/src/codexTransport.ts b/codex-llm-proxy/src/codexTransport.tsindex 27688dc43..b8a90566d 100644--- a/codex-llm-proxy/src/codexTransport.ts+++ b/codex-llm-proxy/src/codexTransport.ts@@ -1,21 +1,22 @@import {+ type CodexCompletionRequestV1,+ LIMITS,hashCodexCompletionRequestV1,parseCodexCompletionRequestV1,- type CodexCompletionRequestV1,} from '@clerum/llm-provider-attempt-contract'+import { chatgptUpstreamHeaders } from './chatgptUpstreamHeaders.js'import type { FinalizeAttemptSuccess, RedeemAttemptSuccess } from './controlApiClient.js'import { logger } from './logger.js'import {CODEX_CATALOG_ORIGIN,CODEX_COMPLETIONS_ORIGIN,OriginDeniedError,+ type OriginPolicyOptions,assertAllowedUpstreamUrl,fetchFrozenOrigin,- type OriginPolicyOptions,} from './originPolicy.js'import { assertBoundedDeadline } from './requestLimits.js'-import { parseSafeUsage, type SafeUsage } from './usage.js'-import { chatgptUpstreamHeaders } from './chatgptUpstreamHeaders.js'+import { type SafeUsage, parseSafeUsage } from './usage.js'export type StreamFrame =| { type: 'text'; text: string }@@ -310,6 +311,26 @@ async function consumeSse(const reader = body.getReader()const decoder = new TextDecoder()const pending = new Map<string, PendingToolCall>()+ // Text stays streaming. Calls become executable only once the entire+ // response succeeds and its independent call budget has been validated.+ const toolFrames: Array<Extract<StreamFrame, { type: 'tool_call' }>> = []+ const acceptFrame = (frame?: StreamFrame) => {+ if (pending.size > LIMITS.maxToolCalls) {+ throw new CodexTransportError(+ 'provider_unavailable',+ `tool calls exceed ${LIMITS.maxToolCalls}`+ )+ }+ if (frame?.type === 'tool_call') {+ if (toolFrames.length >= LIMITS.maxToolCalls) {+ throw new CodexTransportError(+ 'provider_unavailable',+ `tool calls exceed ${LIMITS.maxToolCalls}`+ )+ }+ toolFrames.push(frame)+ } else if (frame) onFrame?.(frame)+ }let buffer = ''let completed = falselet failed = false@@ -327,7 +348,7 @@ async function consumeSse(buffer = parts.pop() ?? ''for (const part of parts) {const mapped = ingestSseBlock(part, pending)- if (mapped.frame) onFrame?.(mapped.frame)+ acceptFrame(mapped.frame)if (mapped.usage) usage = mapped.usageif (mapped.completed) completed = trueif (mapped.failed) failed = true@@ -337,25 +358,31 @@ async function consumeSse(buffer += decoder.decode().replace(/\r\n/g, '\n').replace(/\r/g, '\n')if (buffer.trim()) {const mapped = ingestSseBlock(buffer, pending)- if (mapped.frame) onFrame?.(mapped.frame)+ acceptFrame(mapped.frame)if (mapped.usage) usage = mapped.usageif (mapped.completed) completed = trueif (mapped.failed) failed = true}+ } catch (error) {+ await reader.cancel().catch(() => undefined)+ throw error} finally {reader.releaseLock()}for (const call of pending.values()) {if (call.emitted) continueconst args = parseToolArguments(call.arguments)- onFrame?.({ type: 'tool_call', id: call.id, name: call.name, arguments: args })+ acceptFrame({ type: 'tool_call', id: call.id, name: call.name, arguments: args })call.emitted = true}if (signal.aborted) return { outcome: 'canceled', usage }if (failed) {throw new CodexTransportError('provider_unavailable', 'upstream response failed')}- if (completed) return { outcome: 'success', usage }+ if (completed) {+ for (const frame of toolFrames) onFrame?.(frame)+ return { outcome: 'success', usage }+ }return { outcome: 'unknown', usage }}@@ -399,7 +426,11 @@ function mapUpstreamEvent(if (type === 'response.output_text.delta' && typeof row.delta === 'string') {return { frame: { type: 'text', text: row.delta } }}- if (type === 'response.output_item.added' && isPlainObject(row.item) && row.item.type === 'function_call') {+ if (+ type === 'response.output_item.added' &&+ isPlainObject(row.item) &&+ row.item.type === 'function_call'+ ) {const call = upsertPendingTool(pending, row.item)if (isCompleteJson(call.arguments)) return { frame: emitToolCall(call) }return {}@@ -415,7 +446,9 @@ function mapUpstreamEvent(}if (type === 'response.function_call_arguments.done' ||- (type === 'response.output_item.done' && isPlainObject(row.item) && row.item.type === 'function_call')+ (type === 'response.output_item.done' &&+ isPlainObject(row.item) &&+ row.item.type === 'function_call')) {const source = isPlainObject(row.item) ? row.item : rowconst call = upsertPendingTool(pending, source)@@ -449,7 +482,9 @@ function upsertPendingTool(if (typeof source.name === 'string' && source.name.trim()) current.name = source.nameconst rawArgs = source.argumentsif (typeof rawArgs === 'string') {- current.arguments = source.append ? `${current.arguments}${rawArgs}` : rawArgs || current.arguments+ current.arguments = source.append+ ? `${current.arguments}${rawArgs}`+ : rawArgs || current.arguments} else if (isPlainObject(rawArgs) && !source.append) {current.arguments = JSON.stringify(rawArgs)}@@ -497,7 +532,10 @@ export async function listCodexModels(input: {accessToken: stringfetchFn: typeof fetchlookup?: OriginPolicyOptions['lookup']-}): Promise<{ outcome: 'ready' | 'auth-rejected' | 'unavailable'; models: Array<{ model: string; displayName?: string }> }> {+}): Promise<{+ outcome: 'ready' | 'auth-rejected' | 'unavailable'+ models: Array<{ model: string; displayName?: string }>+}> {const url = assertAllowedUpstreamUrl(CODEX_CATALOG_ORIGIN, 'catalog')const headers = chatgptUpstreamHeaders(input.accessToken, { accept: 'application/json' })const response = await fetchFrozenOrigin({@@ -509,7 +547,8 @@ export async function listCodexModels(input: {headers,},})- if (response.status === 401 || response.status === 403) return { outcome: 'auth-rejected', models: [] }+ if (response.status === 401 || response.status === 403)+ return { outcome: 'auth-rejected', models: [] }if (!response.ok) {logger.warn({@@ -547,7 +586,12 @@ function normalizeModels(body: unknown): Array<{ model: string; displayName?: stif (!isPlainObject(row)) continueconst model = String(row.model || row.slug || row.id || '').trim()if (!model) continue- const displayName = typeof row.displayName === 'string' ? row.displayName : typeof row.title === 'string' ? row.title : undefined+ const displayName =+ typeof row.displayName === 'string'+ ? row.displayName+ : typeof row.title === 'string'+ ? row.title+ : undefinedmodels.push(displayName ? { model, displayName } : { model })}return modelsdiff --git a/codex-llm-proxy/test/approvedToolsUpstream.test.ts b/codex-llm-proxy/test/approvedToolsUpstream.test.tsnew file mode 100644index 000000000..a7e294f2b--- /dev/null+++ b/codex-llm-proxy/test/approvedToolsUpstream.test.ts@@ -0,0 +1,220 @@+import { describe, expect, it } from 'vitest'+import { randomUUID } from 'node:crypto'+import { createApprovedToolsUpstream } from './approvedToolsUpstream'++const URL = 'https://chatgpt.com/backend-api/codex/responses'+const bridges = ['clerum__tool_search', 'clerum__tool_describe', 'clerum__tool_call']+const target = 'fixture__workitem_read_receipt'+const tools = bridges.map(name => ({ type: 'function', name, parameters: { type: 'object' } }))+type Entry = Record<string, unknown>++async function request(+ simulator: ReturnType<typeof createApprovedToolsUpstream>,+ input: Entry[],+ definitions = tools+) {+ const response = await simulator.fetchFn(URL, {+ method: 'POST',+ body: JSON.stringify({+ model: 'gpt-5.3-codex',+ stream: true,+ store: false,+ input,+ tools: definitions,+ }),+ })+ const body = await response.text()+ return { response, body, event: response.ok ? JSON.parse(body.split('\n')[0]!.slice(6)) : null }+}++async function complete(+ simulator: ReturnType<typeof createApprovedToolsUpstream>,+ input: Entry[],+ runId: string,+ wrapped = false+) {+ const result = {+ runId,+ tool: 'workitem_read_receipt',+ callId: randomUUID(),+ businessId: randomUUID(),+ }+ const outputs = [+ {+ found: 1,+ returned: 1,+ results: [{ name: target, server: 'fixture', description: 'verification receipt' }],+ },+ {+ found: true,+ name: target,+ parameters: { type: 'object', properties: {}, additionalProperties: false },+ },+ { content: [{ type: 'text', text: JSON.stringify(result) }] },+ ]+ for (let index = 0; index < 3; index++) {+ const step = await request(simulator, input)+ expect(step.response.status).toBe(200)+ expect(step.event.item.name).toBe(bridges[index])+ const item = step.event.item+ input.push(item, {+ type: 'function_call_output',+ call_id: item.call_id,+ output: wrapped+ ? `<tool_output name="${index === 2 ? target : bridges[index]}" sanitized="false">\n${JSON.stringify(outputs[index])}\n</tool_output>`+ : JSON.stringify(outputs[index]),+ })+ }+ const final = await request(simulator, input)+ expect(final.response.status).toBe(200)+ expect(JSON.parse(final.event.delta)).toEqual(result)+ return result+}++describe('approved-tools isolated upstream boundary', () => {+ it('accepts the BasicSafety envelope with the real bridge execution target', async () => {+ await complete(+ createApprovedToolsUpstream(),+ [{ role: 'user', content: 'verification receipt' }],+ 'wrapped',+ true+ )+ })+ it('rejects a safety envelope attributed to another tool or malformed closing tags', async () => {+ const simulator = createApprovedToolsUpstream()+ const user = { role: 'user', content: 'verification receipt' }+ const { event } = await request(simulator, [user])+ for (const output of [+ '<tool_output name="other" sanitized="false">\n{}\n</tool_output>',+ '<tool_output name="clerum__tool_search" sanitized="false">\n{}\n</tool_output>trailing',+ ]) {+ const result = await request(simulator, [+ user,+ event.item,+ { type: 'function_call_output', call_id: event.item.call_id, output },+ ])+ expect(result.response.status).toBe(422)+ }+ expect(simulator.evidence().businessCalls).toBe(0)+ })+ it.each([83, 150, 250])(+ 'derives its answer from the selected service result for catalog %s',+ async count => {+ const simulator = createApprovedToolsUpstream()+ const result = await complete(+ simulator,+ [{ role: 'user', content: 'verification receipt' }],+ `catalog-${count}`+ )+ const evidence = simulator.evidence()+ expect(evidence).toMatchObject({+ searchCalls: 1,+ describeCalls: 1,+ businessCalls: 1,+ finalResponses: 1,+ rejected: 0,+ })+ expect(evidence.requests.map(row => row.definitionCount)).toEqual([3, 3, 3, 3])+ expect(+ evidence.requests.every(row => row.connectorDefinitionCount === 0 && !row.leakedSchema)+ ).toBe(true)+ expect(new Set(evidence.requests.map(row => row.definitionBytes)).size).toBe(1)+ expect(JSON.stringify(evidence)).not.toContain(result.businessId)+ expect(JSON.stringify(evidence)).not.toContain(result.runId)+ }+ )++ it('interleaves separate conversations without global stage or answer reuse', async () => {+ const simulator = createApprovedToolsUpstream()+ const results = await Promise.all(+ ['a', 'b'].map(run =>+ complete(simulator, [{ role: 'user', content: 'verification receipt' }], run)+ )+ )+ expect(results[0]!.businessId).not.toBe(results[1]!.businessId)+ expect(simulator.evidence().finalResponses).toBe(2)+ })++ it('rediscovers after a new user turn and returns the actual empty catalog after revocation', async () => {+ const simulator = createApprovedToolsUpstream()+ const input: Entry[] = [{ role: 'user', content: 'verification receipt' }]+ await complete(simulator, input, 'revoked')+ input.push({ role: 'user', content: 'verification receipt again' })+ const search = await request(simulator, input)+ expect(search.event.item.name).toBe(bridges[0])+ const empty = { found: 0, returned: 0, results: [] }+ input.push(search.event.item, {+ type: 'function_call_output',+ call_id: search.event.item.call_id,+ output: JSON.stringify(empty),+ })+ const denied = await request(simulator, input)+ expect(JSON.parse(denied.event.delta)).toEqual(empty)+ expect(simulator.evidence()).toMatchObject({+ businessCalls: 1,+ finalResponses: 1,+ deniedResponses: 1,+ })+ })++ it('rejects missing bridge definitions, incomplete results, wrong call IDs and leaked search schemas', async () => {+ const simulator = createApprovedToolsUpstream()+ const user = { role: 'user', content: 'verification receipt' }+ expect((await request(simulator, [user], tools.slice(0, 2))).response.status).toBe(422)+ const step = await request(simulator, [user])+ const call = step.event.item+ expect((await request(simulator, [user, call])).response.status).toBe(422)+ expect(+ (+ await request(simulator, [+ user,+ call,+ { type: 'function_call_output', call_id: 'wrong', output: '{}' },+ ])+ ).response.status+ ).toBe(422)+ const leaked = { found: 1, results: [{ name: target, parameters: {} }] }+ expect(+ (+ await request(simulator, [+ user,+ call,+ { type: 'function_call_output', call_id: call.call_id, output: JSON.stringify(leaked) },+ ])+ ).response.status+ ).toBe(422)+ expect(simulator.evidence().businessCalls).toBe(0)+ })++ it('accepts only the frozen catalog and completion operations without making network calls', async () => {+ const simulator = createApprovedToolsUpstream()+ const catalog = await simulator.fetchFn(+ 'https://chatgpt.com/backend-api/codex/models?client_version=1.0.0'+ )+ expect(await catalog.json()).toMatchObject({ models: [{ slug: 'gpt-5.3-codex' }] })+ for (const url of [+ 'http://chatgpt.com/backend-api/codex/responses',+ 'https://example.com/',+ URL + '/other',+ ]) {+ expect((await simulator.fetchFn(url, { method: 'POST' })).status).toBe(422)+ }+ })++ it('records schema or catalog leakage without retaining the leaked content', async () => {+ const simulator = createApprovedToolsUpstream()+ await request(+ simulator,+ [{ role: 'user', content: 'verification receipt' }],+ [...tools, { type: 'function', name: target, parameters: { type: 'object' } }]+ )+ await request(simulator, [+ { role: 'user', content: 'verification receipt workitem_read_001 workitem_read_002' },+ ])+ expect(simulator.evidence().requests).toMatchObject([+ { connectorDefinitionCount: 1, leakedSchema: true },+ { connectorDefinitionCount: 0, leakedSchema: true },+ ])+ expect(JSON.stringify(simulator.evidence())).not.toContain('workitem_read_001')+ })+})diff --git a/codex-llm-proxy/test/approvedToolsUpstream.ts b/codex-llm-proxy/test/approvedToolsUpstream.tsnew file mode 100644index 000000000..730d7293c--- /dev/null+++ b/codex-llm-proxy/test/approvedToolsUpstream.ts@@ -0,0 +1,215 @@+/** External model boundary for the isolated approved-tools E2E image only. */+import { randomUUID } from 'node:crypto'++type Row = Record<string, unknown>+const COMPLETIONS = 'https://chatgpt.com/backend-api/codex/responses'+const CATALOG = 'https://chatgpt.com/backend-api/codex/models?client_version=1.0.0'+const BRIDGES = ['clerum__tool_search', 'clerum__tool_describe', 'clerum__tool_call']+// The deterministic external model is scoped to this fixture's read journey.+const FIXTURE_QUERY = 'verification receipt'++function record(value: unknown): Row {+ if (!value || typeof value !== 'object' || Array.isArray(value))+ throw new Error('expected_object')+ return value as Row+}++function parse(value: unknown): Row {+ if (typeof value !== 'string') throw new Error('expected_json_text')+ return record(JSON.parse(value))+}++function textResult(value: unknown, expectedName: string): Row {+ if (typeof value === 'string' && value.startsWith('<tool_output')) {+ const wrapped =+ /^<tool_output name="([^"]+)" sanitized="(?:true|false)">\n([\s\S]*)\n<\/tool_output>$/.exec(+ value+ )+ if (!wrapped || wrapped[1] !== expectedName || wrapped[2]!.includes('</tool_output>')) {+ throw new Error('invalid_tool_output_envelope')+ }+ value = wrapped[2]+ }+ const result = parse(value)+ if (result.isError === true || result.is_error === true) throw new Error('tool_failed')+ if (Array.isArray(result.content)) {+ const blocks = result.content.map(record)+ if (blocks.length !== 1 || blocks[0]?.type !== 'text')+ throw new Error('expected_single_text_result')+ return parse(blocks[0].text)+ }+ return result+}++function stream(event: Row): Response {+ return new Response(+ [event, { type: 'response.completed' }].map(row => `data: ${JSON.stringify(row)}\n\n`).join(''),+ {+ headers: { 'content-type': 'text/event-stream' },+ }+ )+}++export function createApprovedToolsUpstream() {+ const evidence = {+ catalogRequests: 0,+ rejected: 0,+ completions: 0,+ searchCalls: 0,+ describeCalls: 0,+ businessCalls: 0,+ finalResponses: 0,+ deniedResponses: 0,+ // Only bounded measurements, never request or result contents.+ requests: [] as Array<{+ definitionCount: number+ definitionBytes: number+ inputBytes: number+ connectorDefinitionCount: number+ leakedSchema: boolean+ stage: string+ }>,+ }++ const fetchFn: typeof fetch = async (input, init) => {+ try {+ const url = typeof input === 'string' ? input : input instanceof URL ? input.href : input.url+ if (url === CATALOG && (!init?.method || init.method === 'GET')) {+ evidence.catalogRequests+++ return Response.json({+ models: [{ slug: 'gpt-5.3-codex', display_name: 'Codex isolated tool test' }],+ })+ }+ if (url !== COMPLETIONS || init?.method !== 'POST') throw new Error('operation_denied')+ const payload = parse(init.body)+ if (payload.stream !== true || payload.store !== false)+ throw new Error('invalid_stream_contract')+ if (!Array.isArray(payload.input) || !Array.isArray(payload.tools))+ throw new Error('missing_input_or_tools')+ const history = payload.input.map(record)+ const tools = payload.tools.map(record)+ const names = new Set(tools.map(tool => tool.name))+ if (!BRIDGES.every(name => names.has(name))) throw new Error('missing_discovery_bridge')+ if (names.size !== tools.length || tools.some(tool => tool.type !== 'function'))+ throw new Error('invalid_definitions')+ let lastUser = history.length - 1+ while (lastUser >= 0 && history[lastUser]?.role !== 'user') lastUser--+ const user = history[lastUser]+ if (!user || typeof user.content !== 'string' || !user.content.trim())+ throw new Error('missing_user_request')+ if (!user.content.toLowerCase().includes(FIXTURE_QUERY))+ throw new Error('unsupported_fixture_task')+ const tail = history.slice(lastUser + 1)+ const calls = tail.filter(row => row.type === 'function_call')+ const outputs = tail.filter(row => row.type === 'function_call_output')+ if (calls.length > 3 || outputs.length !== calls.length)+ throw new Error('incomplete_or_repeated_calls')+ if (new Set(calls.map(call => call.call_id)).size !== calls.length)+ throw new Error('duplicate_call_id')+ const results = calls.map((call, index) => {+ if (call.name !== BRIDGES[index] || typeof call.call_id !== 'string')+ throw new Error('invalid_call_sequence')+ const matched = outputs.filter(output => output.call_id === call.call_id)+ if (matched.length !== 1 || tail.indexOf(matched[0]!) < tail.indexOf(call))+ throw new Error('uncorrelated_result')+ const expectedName = call.name === BRIDGES[2] ? parse(call.arguments).name : call.name+ if (typeof expectedName !== 'string') throw new Error('invalid_envelope_target')+ return textResult(matched[0]!.output, expectedName)+ })+ let candidate: string | undefined+ let denied = false+ if (results[0]) {+ if (parse(calls[0]!.arguments).query !== FIXTURE_QUERY)+ throw new Error('search_query_mismatch')+ if (!Array.isArray(results[0].results)) throw new Error('invalid_search_result')+ denied = results[0].found === 0 && results[0].results.length === 0+ if (!denied) {+ if (results[0].results.length !== 1) throw new Error('search_must_find_one_target')+ const match = record(results[0].results[0])+ if (typeof match.name !== 'string' || BRIDGES.includes(match.name))+ throw new Error('invalid_search_target')+ if ('parameters' in match || 'inputSchema' in match)+ throw new Error('search_leaked_schema')+ candidate = match.name+ }+ }+ if (denied && calls.length !== 1) throw new Error('call_after_denial')+ if (results[1]) {+ if (parse(calls[1]!.arguments).name !== candidate)+ throw new Error('description_target_mismatch')+ if (results[1].found !== true || results[1].name !== candidate)+ throw new Error('description_not_found')+ const schema = record(results[1].parameters)+ if (schema.type !== 'object' || (Array.isArray(schema.required) && schema.required.length))+ throw new Error('fixture_requires_empty_arguments')+ }+ if (results[2]) {+ const args = parse(calls[2]!.arguments)+ if (args.name !== candidate || Object.keys(record(args.arguments)).length)+ throw new Error('invocation_target_mismatch')+ if (+ typeof results[2].businessId !== 'string' ||+ !results[2].businessId ||+ typeof results[2].runId !== 'string' ||+ typeof results[2].callId !== 'string' ||+ typeof results[2].tool !== 'string' ||+ !candidate?.endsWith(`__${results[2].tool}`)+ )+ throw new Error('missing_business_result')+ }+ const stage = denied ? 'denied' : (BRIDGES[calls.length] ?? 'final')+ if (evidence.requests.length >= 512) throw new Error('evidence_capacity_exceeded')+ evidence.completions+++ // This fixture task needs one receipt tool. Other fixture catalog names+ // anywhere in the payload reveal a catalog dump, including in messages.+ const fixtureNames = new Set(+ JSON.stringify(payload).match(/workitem_read_(?:receipt|\d{3})/g) ?? []+ )+ const connectorDefinitionCount = tools.filter(+ tool =>+ typeof tool.name === 'string' &&+ /(?:^|__)workitem_read_(?:receipt|\d{3})$/.test(tool.name)+ ).length+ evidence.requests.push({+ definitionCount: tools.length,+ definitionBytes: Buffer.byteLength(JSON.stringify(tools)),+ inputBytes: Buffer.byteLength(JSON.stringify(payload.input)),+ connectorDefinitionCount,+ leakedSchema: connectorDefinitionCount > 0 || fixtureNames.size > 1,+ stage,+ })+ if (denied) {+ evidence.deniedResponses+++ return stream({ type: 'response.output_text.delta', delta: JSON.stringify(results[0]) })+ }+ if (stage === 'final') {+ evidence.finalResponses+++ return stream({ type: 'response.output_text.delta', delta: JSON.stringify(results[2]) })+ }+ if (!names.has(stage)) throw new Error('unadvertised_function')+ const args =+ stage === BRIDGES[0]+ ? { query: FIXTURE_QUERY, limit: 5 }+ : stage === BRIDGES[1]+ ? { name: candidate }+ : { name: candidate, arguments: {} }+ if (stage === BRIDGES[0]) evidence.searchCalls+++ if (stage === BRIDGES[1]) evidence.describeCalls+++ if (stage === BRIDGES[2]) evidence.businessCalls+++ return stream({+ type: 'response.output_item.done',+ item: {+ type: 'function_call',+ id: randomUUID(),+ call_id: randomUUID(),+ name: stage,+ arguments: JSON.stringify(args),+ },+ })+ } catch {+ evidence.rejected+++ return Response.json({ error: { code: 'fixture_protocol_rejected' } }, { status: 422 })+ }+ }+ return { fetchFn, evidence: () => structuredClone(evidence) }+}diff --git a/codex-llm-proxy/test/codexTransport.conformance.test.ts b/codex-llm-proxy/test/codexTransport.conformance.test.tsindex ed2944652..5ab5f96df 100644--- a/codex-llm-proxy/test/codexTransport.conformance.test.ts+++ b/codex-llm-proxy/test/codexTransport.conformance.test.ts@@ -1,5 +1,5 @@-import { hashCodexCompletionRequestV1 } from '@clerum/llm-provider-attempt-contract'import { describe, expect, it, vi } from 'vitest'+import { hashCodexCompletionRequestV1 } from '@clerum/llm-provider-attempt-contract'import { CodexTransportError, streamCodexCompletion } from '../src/codexTransport.js'import type { RedeemAttemptSuccess } from '../src/controlApiClient.js'import { CODEX_COMPLETIONS_ORIGIN } from '../src/originPolicy.js'@@ -42,7 +42,11 @@ function redeemSuccess(overrides: Partial<RedeemAttemptSuccess> = {}): RedeemAtt}}-function sseResponse(frames: string[], status = 200, headers: Record<string, string> = {}): Response {+function sseResponse(+ frames: string[],+ status = 200,+ headers: Record<string, string> = {}+): Response {return new Response(frames.join(''), {status,headers: { 'content-type': 'text/event-stream', ...headers },@@ -50,6 +54,88 @@ function sseResponse(frames: string[], status = 200, headers: Record<string, str}describe('streamCodexCompletion', () => {+ it.each(['response.failed', 'unterminated'])(+ 'does not emit partial tool calls after %s',+ async terminal => {+ const emitted: Array<{ type: string }> = []+ const frames = [+ 'data: {"type":"response.output_item.done","item":{"type":"function_call","id":"partial","name":"lookup","arguments":"{}"}}\n\n',+ ]+ if (terminal !== 'unterminated')+ frames.push(`data: ${JSON.stringify({ type: terminal })}\n\n`)+ const pending = streamCodexCompletion({+ executionTicket: 'ticket-partial',+ requestHash: REQUEST_HASH,+ request: REQUEST,+ ticket: {+ jti: 'jti-partial',+ hostRef: 'research-host',+ model: REQUEST.model,+ requestHash: REQUEST_HASH,+ providerAttemptId: 'att-partial',+ },+ redeem: vi.fn(async () => redeemSuccess()),+ finalize: vi.fn(async () => ({+ providerAttemptId: 'att-partial',+ outcome: 'success' as const,+ duplicate: false,+ })),+ fetchFn: vi.fn(async () => sseResponse(frames)),+ lookup: async () => [{ address: '1.2.3.4', family: 4 }],+ onFrame: frame => emitted.push(frame),+ })+ if (terminal === 'unterminated') expect((await pending).outcome).toBe('unknown')+ else await expect(pending).rejects.toThrow(/upstream response failed/)+ expect(emitted).toEqual([])+ }+ )+ it.each([32, 33])(+ 'validates the complete %s-call response before emitting executable calls',+ async count => {+ const emitted: Array<{ type: string }> = []+ const finalize = vi.fn(async () => ({+ providerAttemptId: 'att-bound',+ outcome: 'success' as const,+ duplicate: false,+ }))+ const frames = Array.from(+ { length: count },+ (_, index) =>+ `data: ${JSON.stringify({+ type: 'response.output_item.done',+ item: { type: 'function_call', id: `call-${index}`, name: 'lookup', arguments: '{}' },+ })}\n\n`+ )+ frames.push('data: {"type":"response.completed"}\n\n')+ const pending = streamCodexCompletion({+ executionTicket: 'ticket-bound',+ requestHash: REQUEST_HASH,+ request: REQUEST,+ ticket: {+ jti: 'jti-bound',+ hostRef: 'research-host',+ model: REQUEST.model,+ requestHash: REQUEST_HASH,+ providerAttemptId: 'att-bound',+ },+ redeem: vi.fn(async () => redeemSuccess()),+ finalize,+ fetchFn: vi.fn(async () => sseResponse(frames)),+ lookup: async () => [{ address: '1.2.3.4', family: 4 }],+ onFrame: frame => emitted.push(frame),+ })+ if (count === 32) {+ expect((await pending).outcome).toBe('success')+ expect(emitted.filter(frame => frame.type === 'tool_call')).toHaveLength(32)+ } else {+ await expect(pending).rejects.toThrow(/tool calls exceed 32/)+ expect(emitted.filter(frame => frame.type === 'tool_call')).toHaveLength(0)+ expect(finalize).toHaveBeenCalledWith(+ expect.objectContaining({ receipt: expect.objectContaining({ outcome: 'error' }) })+ )+ }+ }+ )it('validates ticket bindings before redeem and maps stream frames including tool-call data', async () => {const redeem = vi.fn(async () => redeemSuccess())const finalize = vi.fn(async () => ({@@ -101,7 +187,9 @@ describe('streamCodexCompletion', () => {receipt: expect.objectContaining({ outcome: 'success', requestHash: REQUEST_HASH }),}))- expect(String(fetchFn.mock.calls[0]?.[1]?.headers?.['authorization'])).toContain(accessTokenFor('live'))+ expect(String(fetchFn.mock.calls[0]?.[1]?.headers?.['authorization'])).toContain(+ accessTokenFor('live')+ )expect(fetchFn.mock.calls[0]?.[1]?.headers?.['originator']).toBe('evenfire')expect(fetchFn.mock.calls[0]?.[1]?.headers?.['openai-beta']).toBe('responses=v1')expect(fetchFn.mock.calls[0]?.[1]?.headers?.['session_id']).toBe('req-001')@@ -273,7 +361,9 @@ describe('streamCodexCompletion', () => {finalize: vi.fn(),fetchFn: vi.fn(),})- ).rejects.toMatchObject({ code: 'request_hash_mismatch' } satisfies Partial<CodexTransportError>)+ ).rejects.toMatchObject({+ code: 'request_hash_mismatch',+ } satisfies Partial<CodexTransportError>)expect(redeem).not.toHaveBeenCalled()})@@ -290,17 +380,23 @@ describe('streamCodexCompletion', () => {requestHash: REQUEST_HASH,providerAttemptId: 'att-1',},- redeem: async () => redeemSuccess({ transport: { ...redeemSuccess().transport, servedModel: 'other' } }),- finalize: vi.fn(async () => ({ providerAttemptId: 'att-1', outcome: 'error', duplicate: false })),+ redeem: async () =>+ redeemSuccess({ transport: { ...redeemSuccess().transport, servedModel: 'other' } }),+ finalize: vi.fn(async () => ({+ providerAttemptId: 'att-1',+ outcome: 'error',+ duplicate: false,+ })),fetchFn: vi.fn(),})).rejects.toMatchObject({ code: 'model_not_allowed' })- const fetchFn = vi.fn(async () =>- new Response(null, {- status: 302,- headers: { location: `https://${['127', '0', '0', '1'].join('.')}/steal` },- })+ const fetchFn = vi.fn(+ async () =>+ new Response(null, {+ status: 302,+ headers: { location: `https://${['127', '0', '0', '1'].join('.')}/steal` },+ }))await expect(streamCodexCompletion({@@ -315,7 +411,11 @@ describe('streamCodexCompletion', () => {providerAttemptId: 'att-1',},redeem: async () => redeemSuccess(),- finalize: vi.fn(async () => ({ providerAttemptId: 'att-1', outcome: 'error', duplicate: false })),+ finalize: vi.fn(async () => ({+ providerAttemptId: 'att-1',+ outcome: 'error',+ duplicate: false,+ })),fetchFn,lookup: async () => [{ address: '1.2.3.4', family: 4 }],})@@ -344,7 +444,11 @@ describe('streamCodexCompletion', () => {providerAttemptId: 'att-1',},redeem: async () => redeemSuccess(),- finalize: vi.fn(async () => ({ providerAttemptId: 'att-1', outcome: 'success', duplicate: false })),+ finalize: vi.fn(async () => ({+ providerAttemptId: 'att-1',+ outcome: 'success',+ duplicate: false,+ })),fetchFn,lookup: async () => [{ address: '1.2.3.4', family: 4 }],})@@ -366,7 +470,11 @@ describe('streamCodexCompletion', () => {providerAttemptId: 'att-1',},redeem: async () => redeemSuccess(),- finalize: vi.fn(async () => ({ providerAttemptId: 'att-1', outcome: 'error', duplicate: false })),+ finalize: vi.fn(async () => ({+ providerAttemptId: 'att-1',+ outcome: 'error',+ duplicate: false,+ })),fetchFn: vi.fn(async () => new Response('denied', { status: 401 })),lookup: async () => [{ address: '1.2.3.4', family: 4 }],})@@ -441,7 +549,11 @@ describe('streamCodexCompletion', () => {request: REQUEST,ticket,redeem,- finalize: vi.fn(async () => ({ providerAttemptId: 'att-1', outcome: 'success', duplicate: false })),+ finalize: vi.fn(async () => ({+ providerAttemptId: 'att-1',+ outcome: 'success',+ duplicate: false,+ })),fetchFn,lookup: async () => [{ address: '1.2.3.4', family: 4 }],})@@ -451,17 +563,27 @@ describe('streamCodexCompletion', () => {request: REQUEST,ticket,redeem,- finalize: vi.fn(async () => ({ providerAttemptId: 'att-1', outcome: 'success', duplicate: false })),+ finalize: vi.fn(async () => ({+ providerAttemptId: 'att-1',+ outcome: 'success',+ duplicate: false,+ })),fetchFn,lookup: async () => [{ address: '1.2.3.4', family: 4 }],})expect(tokens).toEqual(['t-a', 't-b'])- expect(String(fetchFn.mock.calls[0]?.[1]?.headers?.['authorization'])).toContain(accessTokenFor('t-a'))- expect(String(fetchFn.mock.calls[1]?.[1]?.headers?.['authorization'])).toContain(accessTokenFor('t-b'))+ expect(String(fetchFn.mock.calls[0]?.[1]?.headers?.['authorization'])).toContain(+ accessTokenFor('t-a')+ )+ expect(String(fetchFn.mock.calls[1]?.[1]?.headers?.['authorization'])).toContain(+ accessTokenFor('t-b')+ )})it('does not retry after an ambiguous upstream response', async () => {- const fetchFn = vi.fn(async () => sseResponse(['data: {"type":"response.output_text.delta","delta":"partial"}\n\n']))+ const fetchFn = vi.fn(async () =>+ sseResponse(['data: {"type":"response.output_text.delta","delta":"partial"}\n\n'])+ )const result = await streamCodexCompletion({executionTicket: 'ticket-1',requestHash: REQUEST_HASH,@@ -474,7 +596,11 @@ describe('streamCodexCompletion', () => {providerAttemptId: 'att-1',},redeem: async () => redeemSuccess(),- finalize: vi.fn(async () => ({ providerAttemptId: 'att-1', outcome: 'unknown', duplicate: false })),+ finalize: vi.fn(async () => ({+ providerAttemptId: 'att-1',+ outcome: 'unknown',+ duplicate: false,+ })),fetchFn,lookup: async () => [{ address: '1.2.3.4', family: 4 }],})@@ -677,6 +803,8 @@ describe('streamCodexCompletion', () => {lookup: async () => [{ address: '1.2.3.4', family: 4 }],onFrame: frame => frames.push(frame),})- expect(frames).toEqual([{ type: 'tool_call', id: 'call-9', name: 'lookup', arguments: { q: 'x' } }])+ expect(frames).toEqual([+ { type: 'tool_call', id: 'call-9', name: 'lookup', arguments: { q: 'x' } },+ ])})})diff --git a/deploy/images.json b/deploy/images.jsonindex 3286b1a23..f1693847f 100644--- a/deploy/images.json+++ b/deploy/images.json@@ -302,13 +302,24 @@"name": "codex-llm-proxy","path": "codex-llm-proxy","rooted": true,- "source_paths": [- "codex-llm-proxy/**",- "packages/llm-provider-attempt-contract/**"- ],+ "source_paths": ["codex-llm-proxy/**", "packages/llm-provider-attempt-contract/**"],"published": true,"deployed_to_minikube": true},+ {+ "name": "codex-approved-tools-mcp-e2e",+ "path": "tests/e2e/fixtures/codex-subscription/approved-tools",+ "rooted": true,+ "published": false,+ "deployed_to_minikube": false+ },+ {+ "name": "codex-approved-tools-proxy-e2e",+ "path": "tests/e2e/fixtures/codex-subscription/approved-tools-proxy",+ "rooted": true,+ "published": false,+ "deployed_to_minikube": false+ },{"name": "workflow-custom-sdk-e2e","path": "tests/e2e/fixtures/custom-workflow-coordinator",diff --git a/docs/architecture/codex-subscription-transport-contract.md b/docs/architecture/codex-subscription-transport-contract.mdindex 43198e7c7..41615aebe 100644--- a/docs/architecture/codex-subscription-transport-contract.md+++ b/docs/architecture/codex-subscription-transport-contract.md@@ -53,7 +53,7 @@ one physical execution per ticket. A retry or fallback must mint a new attempt.| --- | --- || maxRequestBodyBytes | 1048576 || maxMessages | 128 |-| maxTools | 32 |+| maxToolCalls | 32 || maxOutputTokens | 16384 || maxStreamDurationMs | 300000 || maxDeadlineMs | 300000 |@@ -61,6 +61,42 @@ one physical execution per ticket. A retry or fallback must mint a new attempt.| maxQueuedRequests | 16 || maxRetriesPerAttempt | 1 |+Tool definitions have no independent count ceiling in the Evenfire request+contract. The entire serialized request, including all definitions, remains+bounded by `maxRequestBodyBytes` (1 MiB). Every definition still undergoes+name, schema, finite-value and unknown-field validation. `maxToolCalls` bounds+calls in each assistant history message and each newly returned response. The proxy buffers tool calls until successful completion and validates the bound before publishing any executable call; the Host validates it again before returning the batch. It is not a catalog size limit and+does not widen execution concurrency. This preserves the existing call bound.++The former `maxTools: 32` definition limit was imposed by Evenfire, not a+verified Codex Subscription limit. Remote endpoint limits remain separately+subject to authorized interoperability testing; local acceptance does not+certify that the endpoint accepts any particular count. Discovery optimizes+which schemas are sent, without changing the approved catalog or permissions.+An oversized explicit direct request fails before authorization rather than+silently truncating tools or changing presentation.++### Compatibility and deployment order++The V1 wire fields and canonical hash projection are unchanged. Previously+accepted requests retain their hashes; every newly advertised definition is+included in the hash. Tickets and receipts keep their existing binding.+This is a validator relaxation, not a new wire format. No version negotiation+or fallback to a truncated catalog is introduced.++| Sender | Validator | Result within all other limits |+| --- | --- | --- |+| Old (at most 32 definitions) | New | Accepted; unchanged hash |+| New (at most 32 definitions) | Old | Accepted; unchanged hash |+| New (more than 32 definitions) | Old | Explicit count-limit rejection |+| New (more than 32 definitions) | New | Accepted locally; upstream capability is tested separately |++Deploy updated Control API and proxy consumers before the updated MCP Host+sender. Verify that both consumers use the updated shared package; rebuilding+only the sender is insufficient. For rollback, restore the old sender first,+drain outstanding incompatible attempts, then restore older validators. An old+sender reintroduces the known connector exclusion and is not a correction.+## ErrorsStable codes: `insufficient_scope`, `no_grant`, `model_not_allowed`,@@ -83,3 +119,21 @@ Phase 2 grant binding hashes `{ catalogRevision, connectionKey,credentialRevision, model, provider }` so two named subscriptions with thesame integer revisions do not collide. Host `spec.model.connectionRef` selectsthe grant; revoke of one key fail-closes only that assignment.++## Tool presentation in the agent++`CODEX_TOOL_PRESENTATION=auto|direct|discovery` controls presentation, not access.+Auto is the default when the primary or an allowed fallback uses Codex: it+uses discovery above the existing `CLERUM_DYNAMIC_TOOLS_THRESHOLD` (60) or+`CODEX_TOOL_DISCOVERY_BYTES` (32768 serialized MCP definition bytes). These+are optimization thresholds; no tools are discarded from the local registry.+Direct retains all definitions subject to the bounded request contract.+Discovery exposes the stable native set and search/describe/call bridge.+The same presentation is usable across a configured failover chain.++Search results contain bounded compact descriptions and no schemas. Follow+`nextOffset` with the same query/filter to continue; explicit `enumerate`+permits full traversal. Describe returns the exact selected schema, never a+truncated schema. Existing output spillover (enabled by default) handles+large results; transport/context limits still apply and no unbounded-schema+support or measured subscription-quota reduction is claimed.diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.jsonindex 989fabd78..229cf9981 100644--- a/mcp-host/package-lock.json+++ b/mcp-host/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-mcp-host",- "version": "0.1.318",+ "version": "0.1.319","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-mcp-host",- "version": "0.1.318",+ "version": "0.1.319","license": "SEE LICENSE IN LICENSE","dependencies": {"@anthropic-ai/sdk": "^0.32.0",diff --git a/mcp-host/package.json b/mcp-host/package.jsonindex ab015268a..21dcbdfcc 100644--- a/mcp-host/package.json+++ b/mcp-host/package.json@@ -1,6 +1,6 @@{"name": "clerum-mcp-host",- "version": "0.1.318",+ "version": "0.1.319","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__/taskExecutor.test.ts b/mcp-host/src/agent/__tests__/taskExecutor.test.tsindex fe21fcc95..f6d818c49 100644--- a/mcp-host/src/agent/__tests__/taskExecutor.test.ts+++ b/mcp-host/src/agent/__tests__/taskExecutor.test.ts@@ -1,8 +1,10 @@import { beforeEach, describe, expect, it, vi } from 'vitest'+import { config as appConfig } from '../../config'import { ConversationManager } from '../../core/conversation/conversation'import { LlmError, LlmErrorCode } from '../../core/errors'import { SimpleEventEmitter } from '../../core/orchestration/eventEmitter'import { executeSingleTool, runToolUseLoop } from '../../core/orchestration/toolUseLoop'+import { TOOL_DISCOVERY_TEXT } from '../../core/reasoning/promptBuilder'import { registerDesktopTools } from '../../core/tools/desktopTools'import {requestEffectiveWorkflowList,@@ -12,6 +14,7 @@ import type { Attachment, ChatMessage, MessageContentPart, TraceContextV1 } fromimport { TaskLifecycle } from '../../lifecycle/taskLifecycle'import { anthropicApiError } from '../../llm/__tests__/sdkErrorFixtures'import { ClaudeProvider } from '../../llm/claude'+import { PromptCache } from '../../llm/promptCache'import type { Task, TaskError, TaskSource } from '../../queue/types'import { resolveProviderWorkflowCallerContext } from '../../workflow/providerWorkflowCallerContextClient'import { TaskExecutor, type TaskExecutorDeps, executionModeForSource } from '../taskExecutor'@@ -19,6 +22,10 @@ import { TaskExecutor, type TaskExecutorDeps, executionModeForSource } from '../vi.mock('../../config', () => ({config: {devMode: true,+ dynamicToolsEnabled: false,+ dynamicToolsThreshold: 60,+ codexToolPresentation: 'auto',+ codexToolDiscoveryBytes: 32768,enableApproval: false,nudgeMaxIterations: 3,devModelName: 'test-model',@@ -1605,3 +1612,126 @@ describe('executionModeForSource (§6.3)', () => {expect(autonomous.map(executionModeForSource)).not.toContain('interactive')})})++describe('TaskExecutor Codex presentation wiring', () => {+ function makeExecutor(provider = 'codex-subscription', manager?: unknown) {+ const deps = createDeps({+ llmProvider: { getProviderType: () => provider } as any,+ ...(manager ? { mcpManager: manager as any } : {}),+ })+ const task = createTask()+ task.sourceMessage!.channelType = 'rpc'+ const executor = new TaskExecutor(task, deps) as any+ executor.conversation = { id: 'presentation-session' }+ return { executor, deps }+ }++ it('wires the real native registry, bridge and live presentation with legacy flag off', async () => {+ let count = 0+ const manager = {+ getAllTools: () =>+ Array.from({ length: count }, (_, i) => ({+ name: `fixture__tool_${i}`,+ serverName: 'fixture',+ inputSchema: { type: 'object', properties: {} },+ })),+ callTool: vi.fn(),+ }+ const { executor } = makeExecutor('codex-subscription', manager)+ const { registry, loopController, bridge } = await executor.createToolRegistry()+ const initial = await loopController.refreshTools(registry.listDefinitions())+ expect(initial.map((t: any) => t.name)).toEqual(+ expect.arrayContaining(['clerum__tool_search', 'clerum__tool_describe', 'clerum__tool_call'])+ )+ expect(bridge).toBeDefined()+ for (const n of [83, 150, 250]) {+ count = n+ const full = registry.listDefinitions()+ expect(full).toHaveLength(initial.length + n)+ expect(JSON.stringify(await loopController.refreshTools(full))).toBe(JSON.stringify(initial))+ expect(bridge.getDeferrableCatalogNames().size).toBe(n)+ expect(registry.get(`fixture__tool_${n - 1}`)).not.toBeNull()+ }+ })++ it.each([+ ['zai', 'codex-subscription'],+ ['codex-subscription', 'zai'],+ ])('keeps 250 tools selectively reachable for %s -> %s failover', async (primary, fallback) => {+ const manager = {+ getAllTools: () =>+ Array.from({ length: 250 }, (_, i) => ({+ name: `fixture__tool_${i}`,+ serverName: 'fixture',+ inputSchema: { type: 'object' },+ })),+ }+ const { executor, deps } = makeExecutor(primary, manager)+ deps.failover = { policy: { fallbacks: [{ provider: fallback, model: 'test-model' }] } } as any+ const { registry, loopController, bridge } = await executor.createToolRegistry()+ const advertised = await loopController.refreshTools(registry.listDefinitions())+ expect(bridge).toBeDefined()+ expect(advertised.some((tool: any) => tool.name.startsWith('fixture__'))).toBe(false)+ expect(advertised.some((tool: any) => tool.name === 'clerum__tool_call')).toBe(true)+ expect(bridge.getDeferrableCatalogNames().size).toBe(250)+ expect(registry.get('fixture__tool_249')).not.toBeNull()+ })++ it('direct preserves a catalog over32 and does not activate the bridge despite the legacy flag', async () => {+ const previousMode = appConfig.codexToolPresentation+ const previousFlag = appConfig.dynamicToolsEnabled+ appConfig.codexToolPresentation = 'direct'+ appConfig.dynamicToolsEnabled = true+ try {+ const manager = {+ getAllTools: () =>+ Array.from({ length: 83 }, (_, i) => ({+ name: `fixture__tool_${i}`,+ inputSchema: { type: 'object' },+ })),+ }+ const { executor } = makeExecutor('codex-subscription', manager)+ const { registry, loopController, bridge } = await executor.createToolRegistry()+ expect(bridge).toBeUndefined()+ const full = registry.listDefinitions()+ expect(full.length).toBeGreaterThan(83)+ expect(await loopController.refreshTools(full)).toEqual(full)+ } finally {+ appConfig.codexToolPresentation = previousMode+ appConfig.dynamicToolsEnabled = previousFlag+ }+ })++ it('rebuilds cached discovery guidance on same-model provider switches, preserving daily snapshot', async () => {+ const previous = appConfig.promptCacheEnabled+ appConfig.promptCacheEnabled = true+ try {+ const { executor, deps } = makeExecutor('openai')+ deps.promptCache = new PromptCache()+ deps.workspaceService = {+ readIdentityFiles: vi.fn(async () => ({ identity: '', soul: '', agents: '', user: '' })),+ snapshotDailyLogs: vi.fn(async () => 'daily snapshot'),+ } as any+ executor.conversation.session_key = 'presentation-session'+ const native = [{ name: 'shell_exec', description: 'shell', parameters: {} }]+ const first = await executor.maybeGetOrBuildParts(native)+ expect(JSON.stringify(first)).not.toContain(TOOL_DISCOVERY_TEXT)+ deps.llmProvider = { getProviderType: () => 'codex-subscription' } as any+ const tools = [+ ...native,+ { name: 'clerum__tool_search', description: 'search', parameters: {} },+ ]+ const second = await executor.maybeGetOrBuildParts(tools)+ expect(JSON.stringify(second)).toContain(TOOL_DISCOVERY_TEXT)+ expect(second).not.toBe(first)+ expect(await executor.maybeGetOrBuildParts(tools)).toBe(second)+ expect(deps.workspaceService!.snapshotDailyLogs).toHaveBeenCalledTimes(1)+ deps.llmProvider = { getProviderType: () => 'openai' } as any+ expect(JSON.stringify(await executor.maybeGetOrBuildParts(native))).not.toContain(+ TOOL_DISCOVERY_TEXT+ )+ } finally {+ appConfig.promptCacheEnabled = previous+ }+ })+})diff --git a/mcp-host/src/agent/taskExecutor.ts b/mcp-host/src/agent/taskExecutor.tsindex 77c1b4e85..e0a487441 100644--- a/mcp-host/src/agent/taskExecutor.ts+++ b/mcp-host/src/agent/taskExecutor.ts@@ -32,6 +32,7 @@ import type { LoopConfig } from '../core/orchestration/loopConfig'import { DefaultLoopController } from '../core/orchestration/loopConfig'import type { SpilloverResolver } from '../core/orchestration/spilloverResolver'import { StubSpilloverResolver } from '../core/orchestration/spilloverResolver'+import { resolveToolPresentation } from '../core/orchestration/toolPresentationPolicy'import {buildOutputPreview,executeSingleTool,@@ -83,6 +84,7 @@ import type { SingleTurnProvider } from '../llm'import type { PromptCache } from '../llm/promptCache'import { stampStableHashGauge } from '../llm/promptCacheMetrics'import { isLlmProvider } from '../llm/registryCore'+import { logger } from '../logger'import type { McpManager } from '../mcp'import { getDisplayName, sanitizeError } from '../progress/intentExtraction.js'import {@@ -363,7 +365,7 @@ export class TaskExecutor {* On need_approval, sets state and calls onApprovalNeeded.*/async run(): Promise<void> {- console.log(`[TaskExecutor:${this.taskId}] Starting`)+ logger.info({ taskId: this.taskId }, 'Task starting')try {this.state = 'processing'@@ -377,7 +379,7 @@ export class TaskExecutor {const cronSession =this.task.source === 'cron' ? resolveCronTaskSessionKey(this.task) : undefinedconst sessionKey = resolveTaskSessionKey(this.task)- console.log(`[TaskExecutor:${this.taskId}] Session key: ${sessionKey}`)+ logger.debug({ taskId: this.taskId }, 'Session resolved')const sessionLoadStart = Date.now()this.conversation = await this.deps.conversationManager.getOrCreate(sessionKey, {@@ -438,19 +440,19 @@ export class TaskExecutor {!this.abortController.signal.aborted) {this.state = 'completed'- console.log(`[TaskExecutor:${this.taskId}] Completed`)+ logger.info({ taskId: this.taskId }, 'Task completed')this.turnTiming?.emit(this.taskId)this.deps.onComplete(this.task)this.resolveCompletion?.()} else if (this.abortController.signal.aborted) {- console.log(`[TaskExecutor:${this.taskId}] Cancelled`)+ logger.info({ taskId: this.taskId }, 'Task cancelled')this.resolveCompletion?.()}} catch (error) {const taskError = this.toTaskError(error)- console.error(- `[TaskExecutor:${this.taskId}] Failed: code=${taskError.code} ` +- `retryable=${taskError.retryable} message="${taskError.message}"`+ logger.error(+ { taskId: this.taskId, code: taskError.code, retryable: taskError.retryable, err: error },+ 'Task failed')this.state = 'failed'this.deps.onFail(this.task, taskError)@@ -466,7 +468,7 @@ export class TaskExecutor {throw new Error(`Cannot resume: executor state is ${this.state}`)}- console.log(`[TaskExecutor:${this.taskId}] Resuming after approval`)+ logger.info({ taskId: this.taskId }, 'Resuming after approval')try {this.state = 'processing'@@ -475,7 +477,7 @@ export class TaskExecutor {// The subscriber that aborted us already transitioned the task to cancelled.// Just clean up our own execution state. (PR-186 review M2; Invariant I1)if (this.abortController.signal.aborted) {- console.log(`[TaskExecutor:${this.taskId}] Resume cancelled: already aborted`)+ logger.info({ taskId: this.taskId }, 'Resume cancelled: already aborted')this.resolveCompletion?.()return}@@ -494,7 +496,7 @@ export class TaskExecutor {// Fallback: no snapshot, re-run from scratchif (!approval?.context_snapshot?.length) {- console.log(`[TaskExecutor:${this.taskId}] No snapshot, re-running from scratch`)+ logger.info({ taskId: this.taskId }, 'No snapshot, re-running from scratch')const result = await this.runAgentLoop()await this.handleLoopResult(result)if (@@ -505,7 +507,7 @@ export class TaskExecutor {this.deps.onComplete(this.task)this.resolveCompletion?.()} else if (this.abortController.signal.aborted) {- console.log(`[TaskExecutor:${this.taskId}] Cancelled`)+ logger.info({ taskId: this.taskId }, 'Task cancelled')this.resolveCompletion?.()}return@@ -545,10 +547,7 @@ export class TaskExecutor {}} catch (error) {if (error instanceof ApprovalExpiredError) {- console.warn(- `[TaskExecutor:${this.taskId}] approval_expired before tool execution:`,- error.payload- )+ logger.warn({ taskId: this.taskId }, 'Approval expired before tool execution')await this.deps.conversationManager.failTurn(this.conversation!)this.state = 'failed'this.deps.onFail(this.task, {@@ -573,7 +572,7 @@ export class TaskExecutor {arguments: approval.parameters,}const displayName = getDisplayName(suspendedCall.name)- console.log(`[TaskExecutor:${this.taskId}] Executing approved tool: ${suspendedCall.name}`)+ logger.info({ taskId: this.taskId, toolName: suspendedCall.name }, 'Executing approved tool')this.currentTurnToolNames.add(suspendedCall.name)// executeToolCalls emits reportToolStart AFTER the approval gate, so approved@@ -643,7 +642,7 @@ export class TaskExecutor {this.deps.onComplete(this.task)this.resolveCompletion?.()} else if (this.abortController.signal.aborted) {- console.log(`[TaskExecutor:${this.taskId}] Cancelled`)+ logger.info({ taskId: this.taskId }, 'Task cancelled')this.resolveCompletion?.()}return@@ -703,14 +702,14 @@ export class TaskExecutor {this.deps.onComplete(this.task)this.resolveCompletion?.()} else if (this.abortController.signal.aborted) {- console.log(`[TaskExecutor:${this.taskId}] Cancelled`)+ logger.info({ taskId: this.taskId }, 'Task cancelled')this.resolveCompletion?.()}} catch (error) {const taskError = this.toTaskError(error)- console.error(- `[TaskExecutor:${this.taskId}] Resume failed: code=${taskError.code} ` +- `retryable=${taskError.retryable} message="${taskError.message}"`+ logger.error(+ { taskId: this.taskId, code: taskError.code, retryable: taskError.retryable, err: error },+ 'Resume failed')this.state = 'failed'this.deps.onFail(this.task, taskError)@@ -762,7 +761,7 @@ export class TaskExecutor {const denialMessage = `Tool \`${toolName}\` was denied by the user. The operation was not performed.`if (this.task.responseCallback) {this.task.responseCallback({ response: denialMessage }).catch(err => {- console.error(`[TaskExecutor:${this.taskId}] Failed to send denial:`, err)+ logger.error({ taskId: this.taskId, err }, 'Failed to send denial')})}@@ -824,7 +823,7 @@ export class TaskExecutor {})// Fire warmup asynchronously. `count()` awaits internally if it races.void this.tokenCounter.warmup().catch(err => {- console.warn(`[TaskExecutor:${this.taskId}] tokenCounter.warmup failed:`, err)+ logger.warn({ taskId: this.taskId, err }, 'Token counter warmup failed')})}return this.tokenCounter@@ -1006,7 +1005,7 @@ export class TaskExecutor {// transitioned the task to cancelled before the loop checkpoint fired executor.abort().// Calling lifecycle.transition again would return AlreadyTerminal — redundant and// misleading.- console.log(`[TaskExecutor:${this.taskId}] Cancelled: ${result.reason ?? 'no reason'}`)+ logger.info({ taskId: this.taskId }, 'Task cancelled')this.enqueueGovernedRunEvent('run_end', `task:${this.taskId}:end`, {status: 'cancelled',})@@ -1057,7 +1056,7 @@ export class TaskExecutor {await this.task.responseCallback({ response: content })}this.state = 'completed'- console.log(`[TaskExecutor:${this.taskId}] Completed with static response`)+ logger.info({ taskId: this.taskId }, 'Completed with static response')this.deps.onComplete(this.task)this.resolveCompletion?.()}@@ -1433,10 +1432,9 @@ export class TaskExecutor {try {return await this.resolveUnverifiedProviderWorkflowTriggerClaim(workflowContext)} catch (error) {- console.warn(- `[TaskExecutor:${this.taskId}] Could not resolve unverified provider workflow trigger claim; failing closed: ${- error instanceof Error ? error.message : String(error)- }`+ logger.warn(+ { taskId: this.taskId, err: error },+ 'Could not verify provider workflow trigger claim')return 'I could not verify that a workflow was triggered from this message. List workflows and run the workflow by name again.'}@@ -1573,10 +1571,27 @@ export class TaskExecutor {if (!sessionKey) return undefinedconst cached = this.deps.promptCache.get(sessionKey)+ const provider = this.deps.llmProvider.getProviderType()+ // Only instruction-presence gates belong in the cache identity. Catalog+ // count and schemas do not: discovery guidance is constant at any scale.+ const toolGuidanceKey = JSON.stringify([+ tools.some(t => t.name.startsWith('memory_')),+ tools.some(t => t.name === 'clerum__get_capabilities'),+ tools.some(t => t.name.startsWith('desktop_') || t.name.startsWith('browser_')),+ tools.some(t => t.name.startsWith('workflow_')),+ tools.some(t => t.name.includes('__')),+ tools.some(t => t.name === 'clerum__tool_search'),+ ])// R2 — the system prompt embeds the model name, so a cache entry built for a// different (e.g. just-swapped) model is a miss: rebuild, but keep the frozen// dailyLogSnapshot so the daily-freeze invariant holds.- if (cached?.parts && cached.model === this.deps.modelName) return cached.parts+ if (+ cached?.parts &&+ cached.model === this.deps.modelName &&+ cached.provider === provider &&+ cached.toolGuidanceKey === toolGuidanceKey+ )+ return cached.partsconst dailyLogSnapshot =cached?.dailyLogSnapshot ?? (await this.deps.workspaceService.snapshotDailyLogs(2))@@ -1600,13 +1615,8 @@ export class TaskExecutor {// path on native-only hosts and break the byte-identical guarantee. A deeper// suppression is deferred for that reason.const hasMcpTools = tools.some(t => t.name.includes('__'))- // F4.1 — tool-discovery guidance gates on the bridge's presence, mirroring- // `buildSystemPrompt` exactly so both prompt paths emit byte-identical text.- // The static feature flag is ANDed in explicitly so the guidance is provably- // never emitted on a default-OFF host; with the flag OFF clerum__tool_search- // is not registered either, so this is belt-and-suspenders (LOCKED #5).- const hasToolDiscovery =- appConfig.dynamicToolsEnabled && tools.some(t => t.name === 'clerum__tool_search')+ // The registered bridge is the same source of truth as the legacy prompt.+ const hasToolDiscovery = tools.some(t => t.name === 'clerum__tool_search')const capabilities = hasCapabilities ? CAPABILITY_CONTRACT_TEXT : ''const platformHints: string[] = []if (hasDesktopTools) {@@ -1625,7 +1635,13 @@ export class TaskExecutor {toolDiscoveryGuidance: hasToolDiscovery ? TOOL_DISCOVERY_TEXT : '',memoryGuidance: hasMemoryTools ? MEMORY_GUIDANCE_TEXT : '',})- this.deps.promptCache.set(sessionKey, { parts, dailyLogSnapshot, model: this.deps.modelName })+ this.deps.promptCache.set(sessionKey, {+ parts,+ dailyLogSnapshot,+ model: this.deps.modelName,+ provider,+ toolGuidanceKey,+ })// P1-005: persist the stable_hash for auditability and so post-eviction// rebuilds in the same session can verify identity-files didn't drift.if (this.conversation) {@@ -1677,6 +1693,13 @@ export class TaskExecutor {this.workflowCallerContextOverride === undefined? await this.prepareChannelWorkflowCallerContext(): this.workflowCallerContextOverride+ // Failover reuses this presentation and bridge; optimization follows the+ // primary provider for this task. Both presentations remain executable.+ const presentation = resolveToolPresentation(+ this.deps.llmProvider.getProviderType(),+ appConfig,+ this.deps.failover?.policy.fallbacks+ )const nativeRegistry = new NativeToolRegistry(appConfig.nativeTool,this.conversation!.id,@@ -1695,9 +1718,7 @@ export class TaskExecutor {// (clerum__tool_search / clerum__tool_describe). Reuses the same manager// already threaded into the MCP tool registry below.this.deps.mcpManager ?? undefined,- // F3/F4 (dynamic-tool-loading): static feature flag. Gates registration of- // the 3 bridge tools so a default-OFF host stays byte-identical to today.- appConfig.dynamicToolsEnabled,+ presentation.bridgeEnabled,// §13 (stateless agents): the active provider's credential slot is the// only one that survives into shell_exec's child env.this.deps.llmProvider.getProviderType()@@ -1757,27 +1778,12 @@ export class TaskExecutor {? new ApprovalController(this.conversation!, baseController): baseController- // F3/F4 (dynamic-tool-loading): the ENTIRE bridge surface is gated on the- // STATIC feature flag (LOCKED #5: default OFF, opt-in). When the flag is OFF- // we do NOT wrap with the DeferrableToolController and do NOT wire the bridge- // context, so the controller chain is exactly as it was before this feature- // (ApprovalController → UnifiedApprovalGateController → DefaultLoopController,- // or the bare cron branch) and the executeToolCalls intercept stays inert- // (resolveBridgeCall is a no-op when config.bridge is absent). With the flag- // OFF the 3 bridge tools are not registered either (see NativeToolRegistry),- // so the host is byte-identical to today.const mcpManager = this.deps.mcpManager- if (!appConfig.dynamicToolsEnabled) {+ if (!presentation.bridgeEnabled) {return { registry: compositeRegistry, loopController: innerController }}- // F3 (dynamic-tool-loading): wrap the inner controller (approval OR cron- // branch) with the OUTERMOST DeferrableToolController so cron sessions also- // get the stable swap. `nativeNames` is the exact native set (incl. the 3- // bridges) — membership decides native vs deferrable (Critical: clerum__*- // contains `__` but is native). The controller latches `bridgeActive` on its- // first refreshTools (LOCKED #6). When there is no McpManager there are no- // deferrable tools, so the controller stays passthrough.+ // Exact native membership preserves every native/plugin capability.const nativeNames = new Set(nativeRegistry.listDefinitions().map(d => d.name))// LOCKED #6: the latch lives on the session-scoped Conversation, NOT on the// per-task controller, so a server connecting/disconnecting BETWEEN turns@@ -1788,7 +1794,9 @@ export class TaskExecutor {innerController,nativeNames,{- dynamicToolsEnabled: appConfig.dynamicToolsEnabled,+ dynamicToolsEnabled: presentation.bridgeEnabled,+ codexMode: presentation.codexMode,+ codexToolDiscoveryBytes: appConfig.codexToolDiscoveryBytes,dynamicToolsThreshold: appConfig.dynamicToolsThreshold,},{@@ -1858,10 +1866,9 @@ export class TaskExecutor {return context}} catch (error) {- console.warn(- `[TaskExecutor:${this.taskId}] Provider workflow identity resolution failed; closing channel workflow access: ${- error instanceof Error ? error.message : String(error)- }`+ logger.warn(+ { taskId: this.taskId, err: error },+ 'Provider workflow identity resolution failed')}this.setProviderWorkflowAccessDenied(message, 'unverified_provider_identity')@@ -1882,10 +1889,14 @@ export class TaskExecutor {const channel = isProviderWorkflowChannel(message?.channelType)? message.channelType: 'unknown'- console.warn(- `[TaskExecutor:${this.taskId}] Provider workflow access denied: channel=${channel} stage=${stage} reason=${- this.workflowAccessDeniedReason ?? 'unverified_provider_identity'- }`+ logger.warn(+ {+ taskId: this.taskId,+ channel,+ stage,+ reason: this.workflowAccessDeniedReason ?? 'unverified_provider_identity',+ },+ 'Provider workflow access denied')}diff --git a/mcp-host/src/capabilities/toolCatalogTools.test.ts b/mcp-host/src/capabilities/toolCatalogTools.test.tsindex 6cf17bdb6..d22933dcb 100644--- a/mcp-host/src/capabilities/toolCatalogTools.test.ts+++ b/mcp-host/src/capabilities/toolCatalogTools.test.ts@@ -3,6 +3,7 @@ import type { McpTool } from '../types'import {buildToolDescribeResponse,buildToolSearchResponse,+ createToolCallTool,createToolDescribeTool,createToolSearchTool,serverPrefixOf,@@ -29,6 +30,118 @@ const CATALOG: McpTool[] = [mcpTool('weather__forecast', 'Get the weather forecast for a city'),]+describe('complete bounded discovery', () => {+ it.each([83, 150, 250])('reaches every tool in a %i-tool catalog without schemas', count => {+ const catalog = Array.from({ length: count }, (_, i) =>+ mcpTool(`srv__tool_${String(i).padStart(3, '0')}`, 'read record', {+ type: 'object',+ properties: { marker: { const: i } },+ })+ )+ const names: string[] = []+ let offset = 0+ do {+ const page = buildToolSearchResponse(catalog, '', { enumerate: true, limit: 17, offset })+ expect(+ page.results.every(entry => Object.keys(entry).sort().join() === 'description,name,server')+ ).toBe(true)+ names.push(...page.results.map(entry => entry.name))+ if (page.nextOffset === undefined) break+ expect(page.nextOffset).toBeGreaterThan(offset)+ offset = page.nextOffset+ } while (offset < count)+ expect(names).toEqual(catalog.map(tool => tool.name))+ for (const [index, name] of names.entries()) {+ expect(buildToolDescribeResponse(catalog, name)).toMatchObject({+ name,+ parameters: catalog[index].inputSchema,+ })+ }+ const broad = buildToolSearchResponse(catalog, 'read', { limit: 50, offset: 50 })+ expect(broad.results[0].name).toBe(catalog[50].name)+ })++ it.each([-1, 1.5, NaN, Infinity, '10', null])('rejects malformed offset %s', async offset => {+ const result = await createToolSearchTool(() => CATALOG).execute(+ { query: 'task', offset },+ '/tmp'+ )+ expect(result.success).toBe(false)+ expect(result.error).toMatch(/offset/)+ })++ it('bounds serialized search pages while preserving exact long names', () => {+ const catalog = Array.from({ length: 83 }, (_, i) =>+ mcpTool(`srv__${i}_${'x'.repeat(1200)}`, '😀'.repeat(4000))+ )+ const page = buildToolSearchResponse(catalog, '', { enumerate: true, limit: 50 })+ expect(Buffer.byteLength(JSON.stringify(page))).toBeLessThanOrEqual(32768)+ expect(page.returned).toBeGreaterThan(0)+ expect(page.nextOffset).toBe(page.returned)+ for (const entry of page.results) {+ expect(catalog.some(tool => tool.name === entry.name)).toBe(true)+ expect(Buffer.byteLength(entry.description)).toBeLessThanOrEqual(512)+ expect(entry.description).not.toContain('\uFFFD')+ }+ })++ it('fails explicitly when one exact identifier cannot fit a page', () => {+ expect(() =>+ buildToolSearchResponse([mcpTool(`srv__${'x'.repeat(40000)}`, 'read')], 'read', {})+ ).toThrow(/exceeds/)+ })++ it('supports exhaustive byte-limited traversal without losing an exact identifier', () => {+ const catalog = Array.from({ length: 83 }, (_, i) =>+ mcpTool(`srv__${String(i).padStart(3, '0')}_${'x'.repeat(1200)}`, '\\"'.repeat(4000))+ )+ const names: string[] = []+ let offset = 0+ do {+ const page = buildToolSearchResponse(catalog, '', { enumerate: true, limit: 50, offset })+ expect(Buffer.byteLength(JSON.stringify(page))).toBeLessThanOrEqual(32768)+ names.push(...page.results.map(entry => entry.name))+ if (page.nextOffset === undefined) break+ expect(page.nextOffset).toBeGreaterThan(offset)+ offset = page.nextOffset+ } while (offset < catalog.length)+ expect(names).toEqual(catalog.map(tool => tool.name))+ })++ it('enumerates through the factory and rejects an offset beyond the live catalog', async () => {+ const tool = createToolSearchTool(() => CATALOG)+ const first = await tool.execute({ query: '', enumerate: true, limit: 2 }, '/tmp')+ expect(first.success).toBe(true)+ expect(JSON.parse(first.content!)).toMatchObject({ found: 5, returned: 2, nextOffset: 2 })+ const beyond = await tool.execute({ query: '', enumerate: true, offset: 6 }, '/tmp')+ expect(beyond.success).toBe(false)+ expect(beyond.error).toContain('restart at offset 0')+ })++ it('restarts against the live catalog after removal and never describes a revoked entry', async () => {+ let catalog = [...CATALOG]+ const search = createToolSearchTool(() => catalog)+ const describeTool = createToolDescribeTool(() => catalog)+ await search.execute({ query: '', enumerate: true, limit: 2 }, '/tmp')+ catalog = catalog.filter(tool => tool.name !== 'brain__search_notes')+ const restarted = await search.execute({ query: '', enumerate: true, offset: 0 }, '/tmp')+ expect(+ JSON.parse(restarted.content!).results.map((entry: { name: string }) => entry.name)+ ).not.toContain('brain__search_notes')+ const removed = await describeTool.execute({ name: 'brain__search_notes' }, '/tmp')+ expect(JSON.parse(removed.content!)).toEqual({ found: false })+ })++ it('never executes a real tool through the discovery factory safety net', async () => {+ const result = await createToolCallTool().execute(+ { name: 'board__create_task', arguments: {} },+ '/tmp'+ )+ expect(result.success).toBe(false)+ expect(result.error).toContain('direct execution is not supported')+ })+})+describe('serverPrefixOf', () => {it('parses the server prefix before the double underscore', () => {expect(serverPrefixOf('brain__search_notes')).toBe('brain')diff --git a/mcp-host/src/capabilities/toolCatalogTools.ts b/mcp-host/src/capabilities/toolCatalogTools.tsindex c5135a5c3..e7986cfb3 100644--- a/mcp-host/src/capabilities/toolCatalogTools.ts+++ b/mcp-host/src/capabilities/toolCatalogTools.ts@@ -27,6 +27,9 @@ export type CatalogProvider = () => McpTool[]* index even with a 290-tool catalog. */const DEFAULT_SEARCH_LIMIT = 20const MAX_SEARCH_LIMIT = 50+// Bound context per discovery result, not the size of the approved catalog.+const MAX_SEARCH_BYTES = 32 * 1024+const MAX_DESCRIPTION_BYTES = 512export interface ToolSearchResultEntry {name: string@@ -40,6 +43,8 @@ export interface ToolSearchResponse {/** Number of tools actually returned (`results.length`); `<= found`. */returned: numberresults: ToolSearchResultEntry[]+ /** Continue with the same query/filter and this offset; absent on the last page. */+ nextOffset?: number/** Optional hint set when the query was empty/whitespace-only. */message?: string}@@ -84,6 +89,27 @@ function tokenize(text: string): string[] {.filter(t => t.length > 0)}+function compactDescription(text: string): string {+ if (Buffer.byteLength(text, 'utf8') <= MAX_DESCRIPTION_BYTES) return text+ let result = ''+ let bytes = 0+ for (const character of text) {+ const size = Buffer.byteLength(character, 'utf8')+ if (bytes + size > MAX_DESCRIPTION_BYTES - 3) break+ result += character+ bytes += size+ }+ return `${result}…`+}++function searchOffset(value: unknown): number {+ if (value === undefined) return 0+ if (typeof value !== 'number' || !Number.isSafeInteger(value) || value < 0) {+ throw new Error('offset must be a non-negative safe integer')+ }+ return value+}+/*** Simple, dependency-free keyword overlap scorer. Counts how many distinct* query tokens appear (as substrings of tokens) in the tool's name+description.@@ -119,8 +145,9 @@ function scoreTool(queryTokens: string[], name: string, description: string): nuexport function buildToolSearchResponse(catalog: McpTool[],query: string,- options: { server?: string; limit?: number }+ options: { server?: string; limit?: number; offset?: number; enumerate?: boolean }): ToolSearchResponse {+ const offset = searchOffset(options.offset)const queryTokens = tokenize(query)const serverFilter = options.server?.trim()// Fall back to the default for non-finite limits (e.g. NaN) — `slice(0, NaN)`@@ -136,21 +163,37 @@ export function buildToolSearchResponse(if (serverFilter && server !== serverFilter) continueconst description = tool.description ?? ''const score = scoreTool(queryTokens, tool.name, description)- if (score <= 0) continue- scored.push({ entry: { name: tool.name, server, description }, score })+ if (score <= 0 && !(options.enumerate === true && queryTokens.length === 0)) continue+ scored.push({+ entry: { name: tool.name, server, description: compactDescription(description) },+ score,+ })}scored.sort((a, b) => {if (b.score !== a.score) return b.score - a.score- return a.entry.name.localeCompare(b.entry.name)+ // Code-point ordering stays identical across runtime locales.+ return a.entry.name < b.entry.name ? -1 : a.entry.name > b.entry.name ? 1 : 0})- const results = scored.slice(0, limit).map(s => s.entry)- // `found` is the TOTAL number of matches (before the limit slice); `returned`- // is how many we actually return. Exposing both lets the model tell when the- // result set was truncated (`found > returned`) and refine the query or raise- // `limit`, instead of reading the capped count as "all there is".- return { found: scored.length, returned: results.length, results }+ if (offset > scored.length)+ throw new Error('offset exceeds matching catalog; restart at offset 0')+ const payload: ToolSearchResponse = { found: scored.length, returned: 0, results: [] }+ for (const { entry } of scored.slice(offset, offset + limit)) {+ payload.results.push(entry)+ payload.returned = payload.results.length+ payload.nextOffset =+ offset + payload.returned < scored.length ? offset + payload.returned : undefined+ if (Buffer.byteLength(JSON.stringify(payload), 'utf8') > MAX_SEARCH_BYTES) {+ payload.results.pop()+ payload.returned = payload.results.length+ if (payload.returned === 0)+ throw new Error('Tool identifier exceeds discovery page byte budget')+ payload.nextOffset = offset + payload.returned+ break+ }+ }+ return payload}export function buildToolDescribeResponse(catalog: McpTool[], name: string): ToolDescribeResponse {@@ -181,7 +224,11 @@ export function createToolSearchTool(getCatalog: CatalogProvider): InternalToolD'their input schemas. Use this to discover tools that are not listed ' +'directly, then call `clerum__tool_describe` to fetch the schema of one. ' +'`found` is the total number of matches; `returned` may be smaller when ' +- 'capped at `limit` — refine the query or raise `limit` if `found > returned`.',+ 'bounded by count and bytes. Prefer precise queries; use `nextOffset` as ' ++ '`offset` with the same query and server to continue. Set `enumerate: true` ' ++ 'with an empty query to list a page when keywords are unknown. Pages read ' ++ 'the live catalog: restart at offset 0 after connections or permissions ' ++ 'change. Reuse a known schema instead of repeatedly searching/describing.',parameters: {type: 'object',properties: {@@ -194,6 +241,17 @@ export function createToolSearchTool(getCatalog: CatalogProvider): InternalToolDtype: 'number',description: `Optional: max results to return (default ${DEFAULT_SEARCH_LIMIT}, capped at ${MAX_SEARCH_LIMIT}).`,},+ offset: {+ type: 'integer',+ minimum: 0,+ description:+ 'Optional: nextOffset from the preceding page, default 0. Keep query and server unchanged.',+ },+ enumerate: {+ type: 'boolean',+ description:+ 'Optional: explicitly list tools for an empty query. Prefer keyword search for a specific task.',+ },},required: ['query'],additionalProperties: false,@@ -206,9 +264,13 @@ export function createToolSearchTool(getCatalog: CatalogProvider): InternalToolDconst query = typeof args.query === 'string' ? args.query : ''const server = typeof args.server === 'string' ? args.server : undefinedconst limit = typeof args.limit === 'number' ? args.limit : undefined+ const offset = searchOffset(args.offset)+ if (args.enumerate !== undefined && typeof args.enumerate !== 'boolean') {+ throw new Error('enumerate must be a boolean')+ }// Empty/whitespace-only query: return an informative result rather than a// bare `{ found: 0 }`, which reads like "no tools exist" to the model.- if (query.trim().length === 0) {+ if (query.trim().length === 0 && args.enumerate !== true) {const payload: ToolSearchResponse = {found: 0,returned: 0,@@ -217,7 +279,12 @@ export function createToolSearchTool(getCatalog: CatalogProvider): InternalToolD}return { success: true, content: JSON.stringify(payload) }}- const payload = buildToolSearchResponse(getCatalog(), query, { server, limit })+ const payload = buildToolSearchResponse(getCatalog(), query, {+ server,+ limit,+ offset,+ enumerate: args.enumerate === true,+ })return { success: true, content: JSON.stringify(payload) }} catch (err) {return { success: false, error: err instanceof Error ? err.message : String(err) }diff --git a/mcp-host/src/config.ts b/mcp-host/src/config.tsindex 26373d5d6..c0d4a797a 100644--- a/mcp-host/src/config.ts+++ b/mcp-host/src/config.ts@@ -4,6 +4,11 @@import type { ApprovalConfig } from './core/extensions/approvalTypes'import type { GuardrailsConfig } from './core/guardrails/config'import { NativeToolConfig } from './core/interfaces'+import {+ type CodexToolPresentation,+ parseCodexToolDiscoveryBytes,+ parseCodexToolPresentation,+} from './core/orchestration/toolPresentationPolicy'import { ALL_PROVIDERS, type LlmProvider, descriptorFor, isLlmProvider } from './llm/registryCore'import { HostSpec, McpServerInfo, MemoryConfig, ModelConfig, PersonalizationConfig } from './types'@@ -156,6 +161,9 @@ export interface Config {// prompt with <turn-context> moved to the user message).promptCacheEnabled: boolean+ // Codex presentation is independent of the legacy bridge opt-in.+ codexToolPresentation: CodexToolPresentation+ codexToolDiscoveryBytes: number// F1 (dynamic-tool-loading) — Gates the dynamic-tool-loading bridge; default// OFF; set true per-host to enable; see// `.specs/dynamic-tool-loading/plan-hermes-bridge.es.md`.@@ -816,9 +824,10 @@ export const config: Config = {// caching, but the tiered build path is used uniformly).promptCacheEnabled: getEnvBool('CLERUM_PROMPT_CACHE_ENABLED', true),- // F1 (dynamic-tool-loading) — Gates the dynamic-tool-loading bridge; default- // OFF; set true per-host to enable; see- // `.specs/dynamic-tool-loading/plan-hermes-bridge.es.md`.+ // Codex optimization is independent of the legacy dynamic-tools opt-in.+ codexToolPresentation: parseCodexToolPresentation(process.env.CODEX_TOOL_PRESENTATION),+ codexToolDiscoveryBytes: parseCodexToolDiscoveryBytes(process.env.CODEX_TOOL_DISCOVERY_BYTES),+ // Legacy dynamic tools remain opt-in for other providers.dynamicToolsEnabled: getEnvBool('CLERUM_DYNAMIC_TOOLS_ENABLED', false),// F1 (dynamic-tool-loading) — Minimum deferrable (MCP) tool count above which// the bridge activates when enabled; small hosts stay on passthrough.diff --git a/mcp-host/src/core/orchestration/__tests__/deferrableToolController.test.ts b/mcp-host/src/core/orchestration/__tests__/deferrableToolController.test.tsindex 3f4bcad80..b7e549074 100644--- a/mcp-host/src/core/orchestration/__tests__/deferrableToolController.test.ts+++ b/mcp-host/src/core/orchestration/__tests__/deferrableToolController.test.ts@@ -200,3 +200,74 @@ describe('DeferrableToolController', () => {expect(outB.map(t => t.name).sort()).toEqual([...NATIVE_NAMES].sort())})})++describe('Codex presentation without access limits', () => {+ const native = Array.from({ length: 36 }, (_, i) => tool(`native_${i}`)).concat(NATIVE_TOOLS)+ const names = new Set(native.map(t => t.name))+ function controller(mode: 'auto' | 'direct' | 'discovery', latch = makeLatch(), bytes = 32_768) {+ return new DeferrableToolController(+ new DefaultLoopController(),+ names,+ {+ dynamicToolsEnabled: true,+ dynamicToolsThreshold: 60,+ codexMode: mode,+ codexToolDiscoveryBytes: bytes,+ },+ latch+ )+ }++ it.each([0, 1, 32, 33, 83, 150, 250])(+ 'direct retains every native and MCP for %i MCP',+ async n => {+ const all = [...native, ...mcpTools(n)]+ expect(await controller('direct').refreshTools(all)).toEqual(all)+ }+ )++ it.each([83, 150, 250])(+ 'auto emits byte-identical native+bridge definitions for %i MCP',+ async n => {+ const all = [...native, ...mcpTools(n)]+ const out = await controller('auto').refreshTools(all)+ expect(JSON.stringify(out)).toBe(JSON.stringify(native))+ expect(out.length).toBeGreaterThan(32)+ expect(all.length).toBe(native.length + n)+ }+ )++ it('auto detects late connections and ignores a legacy false latch', async () => {+ const latch = makeLatch()+ latch.set(false)+ const ctl = controller('auto', latch)+ expect(await ctl.refreshTools(native)).toEqual(native)+ expect(await ctl.refreshTools([...native, ...mcpTools(1)])).toHaveLength(native.length + 1)+ expect(await ctl.refreshTools([...native, ...mcpTools(250)])).toEqual(native)+ expect(latch.get()).toBe(false)+ })++ it('explicit discovery remains stable from cold start through connection and removal', async () => {+ const ctl = controller('discovery')+ for (const n of [0, 1, 250, 0]) {+ expect(await ctl.refreshTools([...native, ...mcpTools(n)])).toEqual(native)+ }+ })++ it('auto optimizes a large schema even when the count is below threshold', async () => {+ const large = { ...tool('server__large'), description: 'x'.repeat(4096) }+ expect(await controller('auto', makeLatch(), 1024).refreshTools([...native, large])).toEqual(+ native+ )+ })++ it('provider switch uses current presentation without changing legacy latch', async () => {+ const latch = makeLatch()+ latch.set(true)+ const all = [...native, ...mcpTools(1)]+ expect(await controller('direct', latch).refreshTools(all)).toEqual(all)+ expect(await controller('auto', latch).refreshTools(all)).toEqual(all)+ expect(await controller('discovery', latch).refreshTools(all)).toEqual(native)+ expect(latch.get()).toBe(true)+ })+})diff --git a/mcp-host/src/core/orchestration/__tests__/toolPresentationPolicy.test.ts b/mcp-host/src/core/orchestration/__tests__/toolPresentationPolicy.test.tsnew file mode 100644index 000000000..e4d91e6f4--- /dev/null+++ b/mcp-host/src/core/orchestration/__tests__/toolPresentationPolicy.test.ts@@ -0,0 +1,43 @@+import { describe, expect, it } from 'vitest'+import {+ parseCodexToolDiscoveryBytes,+ parseCodexToolPresentation,+ resolveToolPresentation,+} from '../toolPresentationPolicy'++describe('tool presentation configuration', () => {+ it('defaults Codex to auto independently of the legacy flag', () => {+ expect(parseCodexToolPresentation(undefined)).toBe('auto')+ expect(resolveToolPresentation('codex-subscription', { dynamicToolsEnabled: false })).toEqual({+ bridgeEnabled: true,+ codexMode: 'auto',+ })+ expect(parseCodexToolDiscoveryBytes(undefined)).toBe(32_768)+ })+ it.each(['auto', 'direct', 'discovery'] as const)('accepts explicit %s', mode => {+ expect(parseCodexToolPresentation(mode)).toBe(mode)+ expect(+ resolveToolPresentation('codex-subscription', {+ dynamicToolsEnabled: true,+ codexToolPresentation: mode,+ })+ ).toEqual({ bridgeEnabled: mode !== 'direct', codexMode: mode })+ })+ it.each(['', 'AUTO', 'unknown', ' auto'])('rejects invalid mode %j', value => {+ expect(() => parseCodexToolPresentation(value)).toThrow('CODEX_TOOL_PRESENTATION')+ })+ it.each(['', '0', '-1', '1.5', '3bytes', '9007199254740992'])(+ 'rejects invalid optimization bytes %j',+ value => {+ expect(() => parseCodexToolDiscoveryBytes(value)).toThrow('CODEX_TOOL_DISCOVERY_BYTES')+ }+ )+ it.each([false, true])('preserves other providers flag %s', enabled => {+ expect(+ resolveToolPresentation('zai', {+ dynamicToolsEnabled: enabled,+ codexToolPresentation: 'discovery',+ })+ ).toEqual({ bridgeEnabled: enabled })+ })+})diff --git a/mcp-host/src/core/orchestration/__tests__/toolUseLoop.spillover.test.ts b/mcp-host/src/core/orchestration/__tests__/toolUseLoop.spillover.test.tsindex 871a385ce..ceab750dd 100644--- a/mcp-host/src/core/orchestration/__tests__/toolUseLoop.spillover.test.ts+++ b/mcp-host/src/core/orchestration/__tests__/toolUseLoop.spillover.test.ts@@ -9,13 +9,18 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'import * as fs from 'fs/promises'import * as os from 'os'import * as path from 'path'+import { parseCodexCompletionRequestV1 } from '@clerum/llm-provider-attempt-contract'+import { createToolDescribeTool } from '../../../capabilities/toolCatalogTools'import type { Tool, ToolRegistry } from '../../interfaces'import { BasicSafety } from '../../safety/safety'import { DefaultToolOutputProcessor } from '../../safety/toolOutputProcessor'import { SpilloverStorage } from '../../spillover'+import { SpilloverReadTool } from '../../tools/spilloverRead'import type { ToolOutput } from '../../types'import { SimpleEventEmitter } from '../eventEmitter'+import { DefaultLoopController } from '../loopConfig'import { executeSingleTool } from '../toolUseLoop'+import { executeToolCalls } from '../toolUseLoopToolBatch'function tool(name: string, output: string, isError = false): Tool {return {@@ -105,6 +110,91 @@ describe('executeSingleTool — T1.5 spillover wiring', () => {expect(eventTypes).toContain('spillover:persisted')})+ it('keeps a large discovered schema exact in spillover rather than flooding the next model request', async () => {+ const schema = {+ type: 'object',+ properties: Object.fromEntries(+ Array.from({ length: 200 }, (_, i) => [+ `field_${i}`,+ { type: 'string', description: `Field ${i}` },+ ])+ ),+ }+ const describe = createToolDescribeTool(() => [+ { name: 'fixture__large_schema', serverName: 'fixture', inputSchema: schema },+ ])+ const definition = tool(describe.name, '')+ definition.execute = async args => {+ const result = await describe.execute(args, workspace)+ return {+ content: result.content ?? result.error ?? '',+ duration_ms: 0,+ is_error: !result.success,+ }+ }+ const result = await executeSingleTool(+ { id: 'describe-large', name: describe.name, arguments: { name: 'fixture__large_schema' } },+ configFor({ tools: [definition], storage, taskId: 'schema-task' })+ )+ expect(result.is_error).toBe(false)+ expect(result.spillover_ref).toBeDefined()+ expect(Buffer.byteLength(result.content)).toBeLessThan(8192)+ const saved = await storage.load(result.spillover_ref!)+ expect(JSON.parse(saved!.content).parameters).toEqual(schema)+ expect(JSON.parse(saved!.content).name).toBe('fixture__large_schema')+ const reader = new SpilloverReadTool(storage)+ const read = await executeSingleTool(+ { id: 'read-schema', name: reader.name(), arguments: { ref: result.spillover_ref } },+ configFor({ tools: [reader], storage, taskId: 'schema-task' })+ )+ expect(read.is_error).toBe(false)+ expect(read.spillover_ref).toBeUndefined()+ expect(read.content).toContain(JSON.stringify(schema))+ const request = parseCodexCompletionRequestV1({+ schemaVersion: 'codex-completion-request.v1',+ provider: 'codex-subscription',+ model: 'gpt-5.3-codex',+ requestId: 'schema-read',+ idempotencyKey: 'schema-read',+ messages: [+ {+ role: 'assistant',+ content: '',+ toolCalls: [+ { id: 'read-schema', name: reader.name(), arguments: { ref: result.spillover_ref } },+ ],+ },+ { role: 'tool', content: read.content, toolCallId: 'read-schema', name: reader.name() },+ ],+ })+ expect(request.ok).toBe(true)+ const target = tool('fixture__large_schema', 'business-result')+ const invoked = await executeToolCalls(+ [+ {+ id: 'business-call',+ name: 'clerum__tool_call',+ arguments: { name: target.name(), arguments: { field_0: 'selected' } },+ },+ ],+ {+ ...configFor({ tools: [target], taskId: 'schema-task' }),+ loopController: new DefaultLoopController(),+ bridge: {+ nativeNames: new Set([reader.name(), 'clerum__tool_call']),+ getDeferrableCatalogNames: () => new Set([target.name()]),+ },+ } as never,+ 0+ )+ expect(target.execute).toHaveBeenCalledExactlyOnceWith({ field_0: 'selected' }, undefined)+ expect(invoked.toolResults[0]).toMatchObject({+ tool_call_id: 'business-call',+ name: target.name(),+ is_error: false,+ })+ })+it('ships content inline when below threshold (no spillover ref)', async () => {const small = 'tiny'const result = await executeSingleTool(diff --git a/mcp-host/src/core/orchestration/__tests__/toolUseLoop.test.ts b/mcp-host/src/core/orchestration/__tests__/toolUseLoop.test.tsindex 8470617d9..c36d53fd3 100644--- a/mcp-host/src/core/orchestration/__tests__/toolUseLoop.test.ts+++ b/mcp-host/src/core/orchestration/__tests__/toolUseLoop.test.ts@@ -2230,3 +2230,48 @@ describe('executeSingleTool — progress watcher', () => {vi.useRealTimers()})})++describe('tool presentation failure boundary', () => {+ it('does not disclose the full local catalog or call reasoning when presentation fails', async () => {+ const reasoning = createMockReasoning([{ type: 'text', content: 'should not run' }])+ const config = buildLoopConfig({+ reasoning,+ toolRegistry: createMockRegistry(+ Array.from({ length: 250 }, (_, i) => createMockTool(`server__tool_${i}`))+ ),+ safety: new BasicSafety(),+ events: new SimpleEventEmitter(),+ conversation: makeFakeConversation(),+ })+ const error = new Error('presentation unavailable')+ vi.spyOn(config.loopController, 'refreshTools').mockRejectedValue(error)+ expect(await runToolUseLoop(config, [{ role: 'user', content: 'Find one task' }])).toEqual({+ type: 'error',+ error,+ })+ expect(reasoning.respondWithTools).not.toHaveBeenCalled()+ expect(reasoning.continueWithToolResults).not.toHaveBeenCalled()+ })++ it('retains cancellation when the pending refresh rejects after abort', async () => {+ const reasoning = createMockReasoning([])+ const abort = new AbortController()+ const config = buildLoopConfig({+ reasoning,+ toolRegistry: createMockRegistry([]),+ safety: new BasicSafety(),+ events: new SimpleEventEmitter(),+ conversation: makeFakeConversation(),+ })+ config.abortSignal = abort.signal+ vi.spyOn(config.loopController, 'refreshTools').mockImplementation(async () => {+ abort.abort()+ throw new Error('aborted')+ })+ expect(await runToolUseLoop(config, [{ role: 'user', content: 'hello' }])).toEqual({+ type: 'cancelled',+ reason: 'signal_aborted',+ })+ expect(reasoning.respondWithTools).not.toHaveBeenCalled()+ })+})diff --git a/mcp-host/src/core/orchestration/deferrableToolController.ts b/mcp-host/src/core/orchestration/deferrableToolController.tsindex 760d1c8d6..41f6499ac 100644--- a/mcp-host/src/core/orchestration/deferrableToolController.ts+++ b/mcp-host/src/core/orchestration/deferrableToolController.ts@@ -12,23 +12,17 @@* or a bare `DefaultLoopController` for cron) keeps owning approval and text* gating. We only post-process the tool LIST returned by `refreshTools`.*- * LOCKED #6 (CRITICAL) — the `bridgeActive` decision is LATCHED once and frozen- * for the whole SESSION (not just a single task). MCP servers connect- * asynchronously; recomputing the threshold per turn would let a- * late-connecting server flip `tools[]` mid session and re-introduce the cache- * invalidation this design exists to avoid. A new `TaskExecutor` (and thus a- * new controller instance) is built per task/turn, so the latch must live on a- * SESSION-SCOPED store, not on the controller instance. We read it through a- * `LatchStore` bound to the `Conversation`: the FIRST `refreshTools` of the- * session computes `deferrable.length > threshold` and writes it; every later- * task reads the same frozen value → the advertised set is deterministic and- * session-invariant → `tools[]` byte-stable.+ * Other providers retain the legacy session latch. Codex evaluates its live+ * catalog on every refresh: a cold connection or provider switch must not keep+ * an obsolete threshold decision. Discovery never promotes schemas to tools[].** `isNative` membership is decided by the exact `nativeNames` set (Critical:* `clerum__*` tools contain `__` but are native), NEVER by a string heuristic.*/+import { logger } from '../../logger'import { LoopController } from '../interfaces'import { ChatMessage, PendingApproval, ToolDefinition } from '../types'+import type { CodexToolPresentation } from './toolPresentationPolicy'/*** Session-scoped read/write of the latched `bridgeActive` decision. Bound to the@@ -46,11 +40,19 @@ export class DeferrableToolController implements LoopController {private readonly enabled: booleanprivate readonly threshold: numberprivate readonly latch: LatchStore+ private readonly codexMode?: CodexToolPresentation+ private readonly discoveryBytes: number+ private lastPresentation: string | undefinedconstructor(delegate: LoopController,nativeNames: Set<string>,- config: { dynamicToolsEnabled: boolean; dynamicToolsThreshold: number },+ config: {+ dynamicToolsEnabled: boolean+ dynamicToolsThreshold: number+ codexMode?: CodexToolPresentation+ codexToolDiscoveryBytes?: number+ },latch: LatchStore) {this.delegate = delegate@@ -58,6 +60,8 @@ export class DeferrableToolController implements LoopController {this.enabled = config.dynamicToolsEnabledthis.threshold = config.dynamicToolsThresholdthis.latch = latch+ this.codexMode = config.codexMode+ this.discoveryBytes = config.codexToolDiscoveryBytes ?? 32_768}shouldAccept(content: string, iteration: number): boolean {@@ -84,6 +88,35 @@ export class DeferrableToolController implements LoopController {// through, but never assume — the inner chain owns the upstream list).const upstream = await this.delegate.refreshTools(currentTools)+ // Codex must reevaluate the live catalog after late connections and provider+ // switches. Never reuse another provider's session latch or promote schemas.+ if (this.codexMode) {+ if (this.codexMode === 'direct') return upstream+ const deferred = upstream.filter(t => !this.nativeNames.has(t.name))+ const discover =+ this.codexMode === 'discovery' ||+ deferred.length > this.threshold ||+ Buffer.byteLength(JSON.stringify(deferred), 'utf8') > this.discoveryBytes+ const presented = discover ? upstream.filter(t => this.nativeNames.has(t.name)) : upstream+ const measurement = {+ mode: this.codexMode,+ strategy: discover ? 'discovery' : 'direct',+ nativeCount: upstream.length - deferred.length,+ mcpCount: deferred.length,+ presentedCount: presented.length,+ deferredCount: discover ? deferred.length : 0,+ }+ const key = JSON.stringify(measurement)+ if (key !== this.lastPresentation) {+ logger.info(+ { component: 'tool-presentation', ...measurement },+ 'Tool presentation selected'+ )+ this.lastPresentation = key+ }+ return presented+ }+// LATCH (LOCKED #6): compute the bridge decision exactly once per SESSION,// from the FIRST observed upstream tool set, and freeze it on the// session-scoped store. Later tasks read the same value.@@ -97,8 +130,9 @@ export class DeferrableToolController implements LoopController {// NOT on every turn. Surfaces the cold-load decision (whether the bridge// engaged, the observed deferrable count, and the threshold) so we can see// recomputation visibility.- console.log(- `[deferrable-tools] latch set: bridgeActive=${bridgeActive} deferrableCount=${deferrableCount} threshold=${this.threshold}`+ logger.info(+ { component: 'deferrable-tools', bridgeActive, deferrableCount, threshold: this.threshold },+ 'Legacy session presentation selected')}diff --git a/mcp-host/src/core/orchestration/toolPresentationPolicy.ts b/mcp-host/src/core/orchestration/toolPresentationPolicy.tsnew file mode 100644index 000000000..11e06d762--- /dev/null+++ b/mcp-host/src/core/orchestration/toolPresentationPolicy.ts@@ -0,0 +1,34 @@+/** Presentation changes context cost, never the approved catalog or execution permissions. */+export type CodexToolPresentation = 'auto' | 'direct' | 'discovery'++export function parseCodexToolPresentation(raw: string | undefined): CodexToolPresentation {+ if (raw === undefined) return 'auto'+ if (raw === 'auto' || raw === 'direct' || raw === 'discovery') return raw+ throw new Error('CODEX_TOOL_PRESENTATION must be auto, direct, or discovery')+}++/** An optimization threshold, not a transport limit or an access restriction. */+export function parseCodexToolDiscoveryBytes(raw: string | undefined): number {+ if (raw === undefined) return 32_768+ if (!/^[1-9][0-9]*$/.test(raw) || !Number.isSafeInteger(Number(raw))) {+ throw new Error('CODEX_TOOL_DISCOVERY_BYTES must be a positive safe integer')+ }+ return Number(raw)+}++export function resolveToolPresentation(+ provider: string,+ config: { dynamicToolsEnabled: boolean; codexToolPresentation?: CodexToolPresentation },+ fallbacks: ReadonlyArray<{ provider: string }> = []+): { bridgeEnabled: boolean; codexMode?: CodexToolPresentation } {+ // A fallback reuses the same tool request, so choose a presentation executable+ // and optimized across the configured provider chain before the first attempt.+ if (+ provider === 'codex-subscription' ||+ fallbacks.some(entry => entry.provider === 'codex-subscription')+ ) {+ const codexMode = config.codexToolPresentation ?? 'auto'+ return { bridgeEnabled: codexMode !== 'direct', codexMode }+ }+ return { bridgeEnabled: config.dynamicToolsEnabled }+}diff --git a/mcp-host/src/core/orchestration/toolUseLoop.ts b/mcp-host/src/core/orchestration/toolUseLoop.tsindex 11bdd2230..e31190f90 100644--- a/mcp-host/src/core/orchestration/toolUseLoop.ts+++ b/mcp-host/src/core/orchestration/toolUseLoop.ts@@ -1,4 +1,5 @@import { evaluateTaskBrake } from '../../budget/taskBrake'+import { logger } from '../../logger'import type {Attachment,ChatMessage,@@ -79,8 +80,14 @@ export async function runToolUseLoop(const userRequestText = latestUserText(initialMessages)const initialTools = toolRegistry.listDefinitions()- console.log(- `[NewCore:Loop] START → maxIter=${maxIterations}, tools=${initialTools.length}, messages=${initialMessages.length}`+ logger.info(+ {+ component: 'Loop',+ maxIterations,+ toolCount: initialTools.length,+ messageCount: initialMessages.length,+ },+ 'Loop started')for (let iteration = 0; iteration < maxIterations; iteration++) {@@ -107,8 +114,14 @@ export async function runToolUseLoop(let tools: ToolDefinition[]try {tools = await loopController.refreshTools(toolRegistry.listDefinitions())- } catch {- tools = toolRegistry.listDefinitions()+ } catch (error) {+ if (config.abortSignal?.aborted) return { type: 'cancelled', reason: 'signal_aborted' }+ // Presentation failures must not dump the entire local catalog into the+ // model request or bypass the controller's approved tool selection.+ return {+ type: 'error',+ error: error instanceof Error ? error : new Error('Tool presentation failed'),+ }}const context: ReasoningContext = {@@ -176,8 +189,9 @@ export async function runToolUseLoop(shouldRecoverWorkflowTriggerTextResponse(userRequestText, result.content)) {workflowTriggerAfterListTextResponseRecovered = true- console.log(- `[NewCore:Loop] iter=${iteration} → recovering workflow trigger text response after workflow_list`+ logger.info(+ { component: 'Loop', iteration },+ 'recovering workflow trigger text response after workflow_list')messages.push({ role: 'assistant', content: result.content })messages.push({@@ -199,8 +213,9 @@ export async function runToolUseLoop(!hasWorkflowListResultForCurrentUserTurn(messages)) {workflowListTextResponseRecovered = true- console.log(- `[NewCore:Loop] iter=${iteration} → recovering workflow list text response without tool call`+ logger.info(+ { component: 'Loop', iteration },+ 'recovering workflow list text response without tool call')messages.push({ role: 'assistant', content: result.content })messages.push({@@ -218,8 +233,9 @@ export async function runToolUseLoop(shouldRecoverWorkflowTriggerTextResponse(userRequestText, result.content)) {workflowTriggerTextResponseRecovered = true- console.log(- `[NewCore:Loop] iter=${iteration} → recovering workflow trigger text response without tool call`+ logger.info(+ { component: 'Loop', iteration },+ 'recovering workflow trigger text response without tool call')messages.push({ role: 'assistant', content: result.content })messages.push({@@ -236,8 +252,9 @@ export async function runToolUseLoop(shouldRecoverWorkflowArtifactTextResponse(userRequestText)) {workflowArtifactTextResponseRecovered = true- console.log(- `[NewCore:Loop] iter=${iteration} → recovering workflow artifact text response without tool call`+ logger.info(+ { component: 'Loop', iteration },+ 'recovering workflow artifact text response without tool call')messages.push({ role: 'assistant', content: result.content })messages.push({@@ -265,8 +282,8 @@ export async function runToolUseLoop(}case 'tool_calls': {- const toolNames = result.calls.map(c => c.name).join(', ')- console.log(`[NewCore:Loop] iter=${iteration} → tool_calls: [${toolNames}]`)+ const toolNames = result.calls.map(c => c.name)+ logger.info({ component: 'Loop', iteration, toolNames }, 'Tool calls selected')if (isWorkflowListIntent(userRequestText) &&@@ -373,7 +390,7 @@ export async function runToolUseLoop(}case 'need_approval':- console.log(`[NewCore:Loop] EXIT → need_approval, iterations=${iteration + 1}`)+ logger.info({ component: 'Loop', iterations: iteration + 1 }, 'Loop requires approval')return { type: 'need_approval', approval: result.approval }case 'error': {const recovery = handleLoopErrorRecovery({@@ -397,7 +414,7 @@ export async function runToolUseLoop(workflowTriggerTextResponseRecovered =recovery.workflowTriggerTextResponseRecovered ?? workflowTriggerTextResponseRecoveredmessages = recovery.messages- console.log(`[NewCore:Loop] iter=${iteration} → recovering from LLM error`)+ logger.info({ component: 'Loop', iteration }, 'recovering from LLM error')continue}@@ -412,8 +429,9 @@ export async function runToolUseLoop()}- console.log(- `[NewCore:Loop] EXIT → error: ${result.error.message}, iterations=${iteration + 1}`+ logger.error(+ { component: 'Loop', iterations: iteration + 1, err: result.error },+ 'Loop failed')return { type: 'error', error: result.error }}diff --git a/mcp-host/src/core/reasoning/promptBuilder.ts b/mcp-host/src/core/reasoning/promptBuilder.tsindex 7ce543ff6..20a3263e4 100644--- a/mcp-host/src/core/reasoning/promptBuilder.ts+++ b/mcp-host/src/core/reasoning/promptBuilder.ts@@ -1,4 +1,5 @@import { createHash } from 'node:crypto'+import { logger } from '../../logger'import { PromptBuilder } from '../interfaces'import { ChatMessage, ToolDefinition } from '../types'import type { BuilderInput, SystemPromptParts } from './systemPrompt'@@ -119,7 +120,11 @@ export const TOOL_DISCOVERY_TEXT ='You have access to a large catalog of tools that are not all listed directly. ' +'Use `clerum__tool_search` to find them by keyword, `clerum__tool_describe` to ' +"see one's schema, and `clerum__tool_call` to invoke it. Native tools are " +- 'already available directly.'+ 'already available directly. Search narrowly for the current task; refine or page ' ++ 'only when needed rather than loading the whole catalog. Describe only the chosen ' ++ 'tool and reuse its schema from the conversation when available. Each invocation ' ++ 'still checks current permissions and arguments. Do not call tools for tasks that ' ++ 'do not require them.'/*** Default prompt builder.@@ -216,8 +221,14 @@ export class DefaultPromptBuilder implements PromptBuilder {const content = sections.join('\n\n')const metaKeys = metadata ? Object.keys(metadata).filter(k => metadata[k] != null) : []- console.log(- `[NewCore:PromptBuilder] buildSystemPrompt → tools=${tools.length}, promptLength=${content.length}, metadata=[${metaKeys.join(',')}]`+ logger.debug(+ {+ component: 'PromptBuilder',+ toolCount: tools.length,+ promptLength: content.length,+ metadataKeys: metaKeys,+ },+ 'System prompt built')return {role: 'system',diff --git a/mcp-host/src/llm/__tests__/codexSubscription.test.ts b/mcp-host/src/llm/__tests__/codexSubscription.test.tsindex 3bf79c795..79cfdab0b 100644--- a/mcp-host/src/llm/__tests__/codexSubscription.test.ts+++ b/mcp-host/src/llm/__tests__/codexSubscription.test.ts@@ -1,11 +1,16 @@import { describe, expect, it, vi } from 'vitest'+import {+ buildToolDescribeResponse,+ buildToolSearchResponse,+ createToolCallTool,+ createToolDescribeTool,+ createToolSearchTool,+} from '../../capabilities/toolCatalogTools'import { LlmErrorCode } from '../../core/errors'+import { DeferrableToolController } from '../../core/orchestration/deferrableToolController'+import { DefaultLoopController } from '../../core/orchestration/loopConfig'import { CodexProxyError } from '../codexLlmProxyClient'-import {- CodexSubscriptionProvider,- isCodexNativeToolName,- selectCodexAdvertisedTools,-} from '../codexSubscription'+import { CodexSubscriptionProvider } from '../codexSubscription'import { classifyFailoverClass } from '../failover/classify'import { CodexAuthorizeError } from '../providerAttemptAuthorizer'import { makeProvider } from '../registry'@@ -45,6 +50,45 @@ function deps(overrides?: {}describe('CodexSubscriptionProvider', () => {+ it.each(['unknown', 'canceled', 'error'])(+ 'never returns executable calls from a %s batch',+ async outcome => {+ const wired = deps({+ stream: vi.fn().mockResolvedValue({+ text: 'partial',+ outcome,+ toolCalls: [{ id: 'partial', name: 'echo', arguments: {} }],+ }),+ })+ const provider = new CodexSubscriptionProvider('gpt-5.3-codex', wired as never)+ await expect(+ provider.completeSingleTurnWithTools(+ [{ role: 'user', content: 'hi' }],+ [{ name: 'echo', description: 'echo', parameters: {} }]+ )+ ).rejects.toThrow(/successful terminal outcome/)+ }+ )+ it('rejects an oversized successful proxy batch before returning any executable tools', async () => {+ const wired = deps({+ stream: vi.fn().mockResolvedValue({+ text: '',+ outcome: 'success',+ toolCalls: Array.from({ length: 33 }, (_, index) => ({+ id: `call-${index}`,+ name: 'echo',+ arguments: {},+ })),+ }),+ })+ const provider = new CodexSubscriptionProvider('gpt-5.3-codex', wired as never)+ await expect(+ provider.completeSingleTurnWithTools(+ [{ role: 'user', content: 'hi' }],+ [{ name: 'echo', description: 'echo', parameters: {} }]+ )+ ).rejects.toThrow(/tool calls exceed 32/)+ })it('requires an explicit model plus authorizer and proxy dependencies', () => {process.env.MCP_HOST_CODEX_SUBSCRIPTION_ENABLED = 'true'expect(() => makeProvider('codex-subscription', {})).toThrow(/explicit model and runtime/)@@ -167,61 +211,122 @@ describe('CodexSubscriptionProvider', () => {expect(result.providerAttemptIndex).toBe(1)})- it('omits MCP tools and caps natives at the Codex maxTools limit', () => {+ it.each([1, 33, 83, 150, 250])(+ 'preserves all %i approved MCP definitions through authorization and dispatch',+ async count => {+ const wired = deps()+ const provider = new CodexSubscriptionProvider('gpt-5.6-luna', wired as never)+ const tools = [+ ...Array.from({ length: 36 }, (_, i) => ({+ name: `native_${i}`,+ description: 'native',+ parameters: { type: 'object' },+ })),+ ...Array.from({ length: count }, (_, i) => ({+ name: `eventasks__read_${i}`,+ description: 'read task',+ parameters: { type: 'object', properties: { key: { type: 'string' } } },+ })),+ { name: 'clerum__tool_search', description: 'search', parameters: { type: 'object' } },+ ]+ await provider.completeSingleTurnWithTools([{ role: 'user', content: 'Find a task' }], tools)+ const authorized = wired.authorize.mock.calls[0][0]+ expect(authorized.request.tools).toEqual(tools)+ expect(wired.stream.mock.calls[0][0].request).toEqual(authorized.request)+ expect(authorized.request.tools.at(-2).name).toBe(`eventasks__read_${count - 1}`)+ }+ )++ it('can advertise a single MCP tool without native tools', async () => {+ const wired = deps()+ const provider = new CodexSubscriptionProvider('gpt-5.6-luna', wired as never)const tools = [- { name: 'file_read', description: 'read', parameters: {} },- {- name: 'mongodb-mcp-stack-mongodb-mcp-server__find',- description: 'find',- parameters: {},- },- { name: 'clerum__gfs_read', description: 'gfs', parameters: {} },- {- name: 'mongodb-mcp-stack-mongodb-mcp-server__aggregate',- description: 'agg',- parameters: {},- },+ { name: 'eventasks__workitem_get', description: 'read', parameters: { type: 'object' } },]- expect(isCodexNativeToolName('file_read')).toBe(true)- expect(isCodexNativeToolName('clerum__gfs_read')).toBe(true)- expect(isCodexNativeToolName('mongodb-mcp-stack-mongodb-mcp-server__find')).toBe(false)- expect(selectCodexAdvertisedTools(tools).map(tool => tool.name)).toEqual([- 'file_read',- 'clerum__gfs_read',- ])-- const overflow = Array.from({ length: 40 }, (_, i) => ({- name: i < 36 ? `native_${i}` : `mongo-server__tool_${i}`,- description: 't',- parameters: {},- }))- const advertised = selectCodexAdvertisedTools(overflow)- expect(advertised).toHaveLength(32)- expect(advertised.every(tool => isCodexNativeToolName(tool.name))).toBe(true)- expect(advertised[0]?.name).toBe('native_0')- expect(advertised[31]?.name).toBe('native_31')+ await provider.completeSingleTurnWithTools([{ role: 'user', content: 'Read' }], tools)+ expect(wired.authorize.mock.calls[0][0].request.tools).toEqual(tools)})- it('authorizes with native tools only when MCP tools are also offered', async () => {- const wired = deps()- const provider = new CodexSubscriptionProvider('gpt-5.3-codex', wired as never)- await provider.completeSingleTurnWithTools(- [{ role: 'user', content: 'hi' }],- [- { name: 'file_read', description: 'read', parameters: {} },+ it('keeps the same advertised schemas for 83, 150 and 250 tools and describes only the selected target', async () => {+ let baseline: string | undefined+ for (const count of [83, 150, 250]) {+ const catalog = Array.from({ length: count }, (_, i) => ({+ name: `eventasks__read_${i}`,+ serverName: 'eventasks',+ description: `Read record ${i}`,+ inputSchema: { type: 'object', properties: { key: { type: 'string' } } },+ }))+ const nativeTools = [+ createToolSearchTool(() => catalog),+ createToolDescribeTool(() => catalog),+ createToolCallTool(),+ ].map(tool => ({+ name: tool.name,+ description: tool.description,+ parameters: tool.parameters,+ }))+ const controller = new DeferrableToolController(+ new DefaultLoopController(),+ new Set(nativeTools.map(t => t.name)),+ { dynamicToolsEnabled: true, dynamicToolsThreshold: 60, codexMode: 'auto' },{- name: 'mongodb-mcp-stack-mongodb-mcp-server__find',- description: 'find',- parameters: {},- },- { name: 'clerum__tool_search', description: 'search', parameters: {} },+ get: () => false,+ set: () => {+ throw new Error('Codex must not use a stale legacy latch')+ },+ }+ )+ const all = [+ ...nativeTools,+ ...catalog.map(t => ({+ name: t.name,+ description: t.description,+ parameters: t.inputSchema,+ })),]- )- const authorizedBody = wired.authorize.mock.calls[0][0]- expect(authorizedBody.request.tools.map((tool: { name: string }) => tool.name)).toEqual([- 'file_read',- 'clerum__tool_search',- ])+ const presented = await controller.refreshTools(all)+ const wired = deps()+ const provider = new CodexSubscriptionProvider('gpt-5.6-luna', wired as never)+ await provider.completeSingleTurnWithTools(+ [{ role: 'user', content: 'Find a record' }],+ presented+ )+ const first = wired.authorize.mock.calls[0][0].request+ const serialized = JSON.stringify(first.tools)+ baseline ??= serialized+ expect(serialized).toBe(baseline)+ expect(first.tools.map((t: { name: string }) => t.name)).toEqual(nativeTools.map(t => t.name))+ const chosen = catalog[count - 1]+ const search = buildToolSearchResponse(catalog, chosen.name, { limit: 1 })+ expect(search.results[0].name).toBe(chosen.name)+ expect(JSON.stringify(search)).not.toContain('parameters')+ const described = buildToolDescribeResponse(catalog, chosen.name)+ expect(described.found).toBe(true)+ await provider.completeSingleTurnWithTools(+ [+ { role: 'user', content: 'Find a record' },+ {+ role: 'assistant',+ content: '',+ tool_calls: [+ { id: 'describe-1', name: 'clerum__tool_describe', arguments: { name: chosen.name } },+ ],+ },+ {+ role: 'tool',+ name: 'clerum__tool_describe',+ tool_call_id: 'describe-1',+ content: JSON.stringify(described),+ },+ ],+ await controller.refreshTools(all)+ )+ const second = wired.authorize.mock.calls[1][0].request+ expect(JSON.stringify(second.tools)).toBe(baseline)+ expect(second.messages[2].toolCallId).toBe('describe-1')+ expect(second.messages[2].content).toBe(JSON.stringify(described))+ expect(wired.stream.mock.calls[1][0].request).toEqual(second)+ }})it('does not treat an unknown empty stream as a successful stop', async () => {diff --git a/mcp-host/src/llm/codexSubscription.ts b/mcp-host/src/llm/codexSubscription.tsindex e6c6a81ba..455e69620 100644--- a/mcp-host/src/llm/codexSubscription.ts+++ b/mcp-host/src/llm/codexSubscription.ts@@ -12,6 +12,7 @@ import {ToolCompletionResponse,ToolDefinition,} from '../core/types'+import { logger } from '../logger'import { CodexLlmProxyClient, CodexProxyError } from './codexLlmProxyClient'import { classifyUnknown } from './errorClassification'import { CodexAuthorizeError, ProviderAttemptAuthorizer } from './providerAttemptAuthorizer'@@ -61,24 +62,20 @@ export type CodexSubscriptionDeps = {attemptContext: (input: { model: string }) => CodexAttemptContext}-/**- * Codex authorize is capped at `LIMITS.maxTools` (32). MCP tools use- * `serverName__toolName`; native tools are unprefixed or `clerum__*`.- * Drop MCP tools first so a Host with a large MCP catalog can still- * authorize. If natives still exceed the cap, keep the leading native- * slice in registry order.- */-export function isCodexNativeToolName(name: string): boolean {- const idx = name.indexOf('__')- if (idx <= 0) return true- return name.startsWith('clerum__')-}-function assertTerminalCodexOutcome(result: {text: stringtoolCalls: Array<{ id: string; name: string; arguments: Record<string, unknown> }>outcome: 'success' | 'canceled' | 'error' | 'unknown'}): void {+ if (result.toolCalls.length > LIMITS.maxToolCalls) {+ throw new CodexProxyError('provider_unavailable', `tool calls exceed ${LIMITS.maxToolCalls}`)+ }+ if (result.toolCalls.length > 0 && result.outcome !== 'success') {+ throw new CodexProxyError(+ 'provider_unavailable',+ 'tool calls require a successful terminal outcome'+ )+ }if (result.outcome === 'error') {throw new CodexProxyError('provider_unavailable', 'proxy stream ended with an error outcome')}@@ -90,12 +87,6 @@ function assertTerminalCodexOutcome(result: {}}-export function selectCodexAdvertisedTools(tools: ToolDefinition[]): ToolDefinition[] {- const natives = tools.filter(tool => isCodexNativeToolName(tool.name))- if (natives.length <= LIMITS.maxTools) return natives- return natives.slice(0, LIMITS.maxTools)-}-export class CodexSubscriptionProvider implements SingleTurnProvider {private nextProviderAttemptIndex = 1@@ -332,20 +323,20 @@ export class CodexSubscriptionProvider implements SingleTurnProvider {})),}if (tools && tools.length > 0) {- const advertised = selectCodexAdvertisedTools(tools)- if (advertised.length !== tools.length) {- console.warn(- `[Codex] advertised ${advertised.length} native tool(s) of ${tools.length} offered (MCP omitted, cap ${LIMITS.maxTools})`- )- }- if (advertised.length > 0) {- request.tools = advertised.map(tool => ({- name: tool.name,- description: tool.description,- parameters: tool.parameters,- }))- }+ // Presentation is owned by the agent loop. Preserve its final definitions+ // in both the authorized hash and the proxy request; MCP names are not+ // permissions and must never be discarded by the provider adapter.+ logger.debug(+ { provider: 'codex-subscription', presentedCount: tools.length },+ 'Tool definitions prepared for authorization'+ )+ request.tools = tools.map(tool => ({+ name: tool.name,+ description: tool.description,+ parameters: tool.parameters,+ }))}+if (options?.temperature !== undefined ||options?.max_tokens !== undefined ||diff --git a/mcp-host/src/llm/promptCache.ts b/mcp-host/src/llm/promptCache.tsindex 6f68f143d..34ed0deee 100644--- a/mcp-host/src/llm/promptCache.ts+++ b/mcp-host/src/llm/promptCache.ts@@ -39,6 +39,8 @@ export interface PromptCacheEntry {* changing the sessionKey-indexed map (eviction/invalidation stay intact).*/model?: string+ provider?: string+ toolGuidanceKey?: string}export interface PromptCacheObserver {diff --git a/mcp-host/src/logger.test.ts b/mcp-host/src/logger.test.tsindex dace3953c..073f260ac 100644--- a/mcp-host/src/logger.test.ts+++ b/mcp-host/src/logger.test.ts@@ -19,6 +19,17 @@ describe('mcp-host structured logger redaction', () => {delete process.env.MCP_HOST_LOGGER_PROBE})+ it('retains Error classification without non-enumerable private messages or stacks', () => {+ const err = Object.assign(new Error('sensitive diagnostic content'), {+ code: 'ECONNRESET',+ status: 503,+ })+ expect(redactUnknown({ err })).toEqual({+ err: { name: 'Error', code: 'ECONNRESET', status: 503 },+ })+ expect(JSON.stringify(redactUnknown({ err }))).not.toContain('sensitive diagnostic content')+ })+it('drops prototype-polluting keys instead of writing them onto the clone', () => {const input = { ok: true, constructor: { evil: true }, prototype: { evil: true } }expect(redactUnknown(input)).toEqual({ ok: true })diff --git a/mcp-host/src/logger.ts b/mcp-host/src/logger.tsindex ad93bceb8..77d2f44fc 100644--- a/mcp-host/src/logger.ts+++ b/mcp-host/src/logger.ts@@ -33,6 +33,20 @@ function cloneRedacted(value: unknown, key?: string): unknown {if (key && SENSITIVE_KEY_RE.test(key)) return '[Redacted]'if (value === undefined) return undefinedif (typeof value !== 'object' || value === null) return value+ // Error.message/stack may contain provider payloads or connection material.+ // Preserve bounded classification fields, not arbitrary error text.+ if (value instanceof Error) {+ const error = value as Error & { code?: unknown; status?: unknown }+ const summary: Record<string, unknown> = {+ name: /^[A-Za-z][A-Za-z0-9_]{0,63}$/.test(error.name) ? error.name : 'Error',+ }+ if (typeof error.code === 'string' && /^[A-Za-z][A-Za-z0-9_]{0,63}$/.test(error.code)) {+ summary.code = error.code+ }+ if (typeof error.status === 'number' && Number.isInteger(error.status))+ summary.status = error.status+ return summary+ }if (Array.isArray(value)) return value.map(item => cloneRedacted(item))const out: Record<string, unknown> = Object.create(null)for (const nextKey of Object.keys(value as object)) {@@ -94,3 +108,33 @@ function emit(level: string, args: unknown[]): void {console.log = (...args: unknown[]) => emit('info', args)console.error = (...args: unknown[]) => emit('error', args)console.warn = (...args: unknown[]) => emit('warn', args)++/** Explicit structured entry point for service code, sharing redaction and sinks. */+function writeStructured(+ level: 'debug' | 'info' | 'warn' | 'error',+ fields: Record<string, unknown>,+ msg: string+): void {+ const levels = { debug: 10, info: 20, warn: 30, error: 40, silent: 100 }+ const configured = process.env.LOG_LEVEL as keyof typeof levels | undefined+ if (+ levels[level] <+ (configured && levels[configured] !== undefined ? levels[configured] : levels.info)+ )+ return+ const entry = {+ ...(redactUnknown(fields) as Record<string, unknown>),+ timestamp: new Date().toISOString(),+ level,+ msg: msg.replace(/[\r\n\u2028\u2029]/g, ' ').trim(),+ }+ const writer = level === 'error' ? originalError : level === 'warn' ? originalWarn : originalLog+ writer(JSON.stringify(entry))+}++export const logger = {+ debug: (fields: Record<string, unknown>, msg: string) => writeStructured('debug', fields, msg),+ info: (fields: Record<string, unknown>, msg: string) => writeStructured('info', fields, msg),+ warn: (fields: Record<string, unknown>, msg: string) => writeStructured('warn', fields, msg),+ error: (fields: Record<string, unknown>, msg: string) => writeStructured('error', fields, msg),+}diff --git a/mcp-host/src/mcp/__tests__/managerDelimiterDispatch.test.ts b/mcp-host/src/mcp/__tests__/managerDelimiterDispatch.test.tsnew file mode 100644index 000000000..2f2bf2b9a--- /dev/null+++ b/mcp-host/src/mcp/__tests__/managerDelimiterDispatch.test.ts@@ -0,0 +1,154 @@+import { afterEach, describe, expect, it, vi } from 'vitest'+import { McpToolRegistryAdapter } from '../../core/adapters/toolRegistryAdapter'+import type { LoopConfig } from '../../core/orchestration/loopConfig'+import { executeToolCalls } from '../../core/orchestration/toolUseLoopToolBatch'+import { McpManager } from '../manager'++// Mock only the remote SDK boundary. Registry, manager admission, catalog+// prefixing and McpClient dispatch execute their real implementations.+const remote = vi.hoisted(() => ({ calls: vi.fn() }))+vi.mock('@modelcontextprotocol/sdk/client/index.js', () => ({+ Client: class {+ connect = vi.fn().mockResolvedValue(undefined)+ close = vi.fn().mockResolvedValue(undefined)+ listTools = vi.fn().mockResolvedValue({+ tools: [+ {+ name: 'workitem__read__receipt',+ description: 'Read receipt',+ inputSchema: { type: 'object' },+ },+ { name: 'simple', description: 'Simple read', inputSchema: { type: 'object' } },+ ],+ })+ callTool = remote.calls+ },+}))+vi.mock('@modelcontextprotocol/sdk/client/streamableHttp.js', () => ({+ StreamableHTTPClientTransport: class {+ close = vi.fn().mockResolvedValue(undefined)+ },+}))++const managers: McpManager[] = []+async function connectedManager() {+ const manager = new McpManager()+ managers.push(manager)+ remote.calls.mockResolvedValue({ content: [{ type: 'text', text: 'receipt-observed' }] })+ await manager.addServer({+ name: 'approved-service',+ contextRef: 'fixture-context',+ enabled: true,+ transport: { type: 'streamableHttp', url: 'http://approved-service.test/mcp' },+ status: { deployed: true, ready: true },+ })+ return manager+}++afterEach(async () => {+ await Promise.all(managers.splice(0).map(manager => manager.close()))+ remote.calls.mockReset()+})++describe('MCP delimiter dispatch', () => {+ it('dispatches every listed tool with its complete remote name', async () => {+ const manager = await connectedManager()+ const catalog = manager.getAllTools()+ expect(catalog.map(tool => tool.name)).toContain('approved-service__workitem__read__receipt')+ for (const tool of catalog) {+ const result = await manager.callTool(tool.name, {})+ expect(result.isError).toBe(false)+ expect(result.toolName).toBe(tool.name)+ }+ expect(remote.calls.mock.calls.map(call => call[0].name)).toEqual([+ 'workitem__read__receipt',+ 'simple',+ ])+ })++ it('executes the exact approved name through the real registry adapter', async () => {+ const manager = await connectedManager()+ const registry = new McpToolRegistryAdapter(manager, 'authenticated-sender')+ const tool = registry.get('approved-service__workitem__read__receipt')!+ expect(tool).not.toBeNull()+ expect(tool.traceDescriptor?.({})).toMatchObject({ sourceRef: 'approved-service' })+ const output = await tool.execute({})+ expect(output.is_error).toBe(false)+ expect(output.content).toBe('receipt-observed')+ expect(remote.calls.mock.calls[0][0].name).toBe('workitem__read__receipt')+ })++ it('preserves the exact name and call ID through bridge resolution and execution', async () => {+ const manager = await connectedManager()+ const registry = new McpToolRegistryAdapter(manager, 'authenticated-sender')+ const beforeExecution = vi.fn((_name: string, _params: Record<string, unknown>) => ({+ is_valid: true,+ errors: [],+ }))+ const config: LoopConfig = {+ reasoning: {} as never,+ conversation: {} as never,+ contextManager: {} as never,+ toolRegistry: registry,+ safety: {+ validateInput: () => ({ is_valid: true, errors: [] }),+ validateToolParams: () => ({ is_valid: true, errors: [] }),+ sanitizeOutput: (_name, content) => ({ content, was_modified: false, warnings: [] }),+ wrapForLlm: (_name, content) => content,+ },+ events: { emit: () => {}, on: () => {}, off: () => {} },+ loopController: {+ shouldAccept: () => true,+ onTextRejected: () => null,+ beforeTool: () => 'proceed',+ onExhaustion: () => '',+ refreshTools: async tools => tools,+ },+ toolOutputProcessor: { beforeExecution, afterExecution: (_name, output) => output.content },+ maxIterations: 10,+ toolTimeout: 5000,+ toolProgressInterval: 0,+ bridge: {+ nativeNames: new Set(['clerum__tool_call']),+ getDeferrableCatalogNames: () => new Set(manager.getAllTools().map(tool => tool.name)),+ },+ }+ const results = await executeToolCalls(+ [+ {+ id: 'selected-call',+ name: 'clerum__tool_call',+ arguments: { name: 'approved-service__workitem__read__receipt', arguments: {} },+ },+ ],+ config,+ 0+ )+ expect(results).toMatchObject({+ toolResults: [{ tool_call_id: 'selected-call', is_error: false }],+ })+ expect(beforeExecution.mock.calls[0][0]).toBe('approved-service__workitem__read__receipt')+ expect(remote.calls.mock.calls[0][0].name).toBe('workitem__read__receipt')+ })++ it.each(['missing-delimiter', '__read', 'approved-service__', ''])(+ 'rejects invalid namespace %s without dispatch',+ async name => {+ const manager = await connectedManager()+ const result = await manager.callTool(name, {})+ expect(result.isError).toBe(true)+ expect(result.result).toMatchObject({+ error: expect.stringContaining('Invalid tool name format'),+ })+ expect(remote.calls).not.toHaveBeenCalled()+ }+ )++ it('does not turn an unregistered namespace into an admitted client', async () => {+ const manager = await connectedManager()+ const result = await manager.callTool('unapproved-service__workitem__read', {})+ expect(result.isError).toBe(true)+ expect(result.result).toMatchObject({ error: 'MCP server not found: unapproved-service' })+ expect(remote.calls).not.toHaveBeenCalled()+ })+})diff --git a/mcp-host/src/mcp/manager.ts b/mcp-host/src/mcp/manager.tsindex aeed7f2d2..a62bda5f4 100644--- a/mcp-host/src/mcp/manager.ts+++ b/mcp-host/src/mcp/manager.ts@@ -7,6 +7,7 @@* (populates the catalog) plus N lazily-admitted per-user partitions. Desired* state / catalog / fencing still reason by serverName — see `byServer`.*/+import { logger } from '../logger'import { McpServerInfo, McpTool, ToolCallResult } from '../types'import {McpAuthError,@@ -145,7 +146,7 @@ export class McpManager {this.statusTracker = statusTracker ?? new ServerStatusTracker()this.tokenProviderFactory = tokenProviderFactoryif (proxyUrl) {- console.log(`[McpManager] Proxy mode enabled: ${proxyUrl}`)+ logger.info({ component: 'McpManager' }, 'Proxy mode enabled')}}@@ -319,7 +320,10 @@ export class McpManager {// Skip disabled servers — operator intent, not infra failure.if (!serverConfig.enabled) {- console.log(`[McpManager] Skipping disabled server: ${serverConfig.name}`)+ logger.info(+ { component: 'McpManager', serverName: serverConfig.name },+ 'Skipping disabled server'+ )this.serverInfos.set(serverConfig.name, serverConfig)this.statusTracker.markDisabled(serverConfig.name)control.onCommit?.()@@ -329,8 +333,9 @@ export class McpManager {// Explicitly non-authoritative readiness is a fail-closed admission// signal, not permission to open a new connection.if (serverConfig.status?.authoritative === false) {- console.log(- `[McpManager] Skipping server with non-authoritative readiness: ${serverConfig.name}`+ logger.info(+ { component: 'McpManager', serverName: serverConfig.name },+ 'Skipping server with non-authoritative readiness')this.serverInfos.set(serverConfig.name, serverConfig)this.statusTracker.markNotReady(serverConfig.name, serverConfig.status?.message)@@ -340,8 +345,9 @@ export class McpManager {// Skip servers that aren't ready yet — transient infra, surfaced as not_ready.if (!serverConfig.status?.ready) {- console.log(- `[McpManager] Skipping server not ready: ${serverConfig.name} (${serverConfig.status?.message || 'unknown'})`+ logger.info(+ { component: 'McpManager', serverName: serverConfig.name },+ 'Skipping server not ready')this.serverInfos.set(serverConfig.name, serverConfig)this.statusTracker.markNotReady(serverConfig.name, serverConfig.status?.message)@@ -355,7 +361,10 @@ export class McpManager {if (this.clients.has(key)) {const installed = this.serverInfos.get(serverConfig.name)if (installed && JSON.stringify(installed) === JSON.stringify(serverConfig)) {- console.log(`[McpManager] Server already connected: ${serverConfig.name}`)+ logger.info(+ { component: 'McpManager', serverName: serverConfig.name },+ 'Server already connected'+ )control.onCommit?.()return 'applied'}@@ -400,10 +409,13 @@ export class McpManager {this.pendingAdmissions.delete(key)this.installConnectedClient(key, serverConfig, client, ownsServerStatus)control.onCommit?.()- console.log(- `[McpManager] Installed client for server "${serverConfig.name}" (${- ownsServerStatus ? 'shared' : 'per-user'- } partition)`+ logger.info(+ {+ component: 'McpManager',+ serverName: serverConfig.name,+ partition: ownsServerStatus ? 'shared' : 'per-user',+ },+ 'Installed client for server')return 'applied'} catch (error) {@@ -413,11 +425,14 @@ export class McpManager {return 'stale'}this.pendingAdmissions.delete(key)- console.error(- `[McpManager] Failed to install client for server "${serverConfig.name}" (${- ownsServerStatus ? 'shared' : 'per-user'- } partition):`,- error+ logger.error(+ {+ component: 'McpManager',+ serverName: serverConfig.name,+ partition: ownsServerStatus ? 'shared' : 'per-user',+ err: error,+ },+ 'Failed to install client for server')// Only the SHARED/eager path owns the per-serverName status transition. A// per-user admission failure must leave the representative's status intact.@@ -484,7 +499,7 @@ export class McpManager {this.evictPartitionsExcept(serverConfig.name, key, control)await this.scheduleClientCleanup(previousClient, control)- console.log(`[McpManager] Replaced server: ${serverConfig.name}`)+ logger.info({ component: 'McpManager', serverName: serverConfig.name }, 'Replaced server')return 'applied'}@@ -717,7 +732,7 @@ export class McpManager {return async () => {await this.runDetachedCleanups(cleanups)- console.log(`[McpManager] Removed server: ${serverName}`)+ logger.info({ component: 'McpManager', serverName }, 'Removed server')}}@@ -863,9 +878,11 @@ export class McpManager {args: Record<string, unknown>,options: McpToolCallOptions = {}): Promise<ToolCallResult> {- // Parse server name and tool name- const parts = fullToolName.split('__')- if (parts.length !== 2) {+ // Server identities come from Kubernetes metadata.name (RFC1123, no+ // underscores). Only the first delimiter is ours; the remote tool name+ // may itself contain '__' and must reach the client unchanged.+ const delimiter = fullToolName.indexOf('__')+ if (delimiter <= 0 || delimiter + 2 === fullToolName.length) {return {toolName: fullToolName,result: { error: `Invalid tool name format: ${fullToolName}` },@@ -873,7 +890,8 @@ export class McpManager {}}- const [serverName, toolName] = parts+ const serverName = fullToolName.slice(0, delimiter)+ const toolName = fullToolName.slice(delimiter + 2)const info = this.serverInfos.get(serverName)// Only the per-user oauth sabor gets per-user partitions. oauth-context and// static both resolve on the SHARED representative (the else branch below).diff --git a/packages/llm-provider-attempt-contract/index.cjs b/packages/llm-provider-attempt-contract/index.cjsindex 953bdb591..070825841 100644--- a/packages/llm-provider-attempt-contract/index.cjs+++ b/packages/llm-provider-attempt-contract/index.cjs@@ -15,7 +15,8 @@ const TICKET_TYP = 'codex-execution-ticket'const LIMITS = Object.freeze({maxRequestBodyBytes: 1048576,maxMessages: 128,- maxTools: 32,+ // Bound calls in each assistant message independently of advertised definitions.+ maxToolCalls: 32,maxOutputTokens: 16384,maxDeadlineMs: 300000,maxIdLength: 128,@@ -189,8 +190,8 @@ function parseMessages(raw) {if (!Array.isArray(item.toolCalls) || item.toolCalls.length === 0) {return fail('invalid', `messages[${i}].toolCalls must be a non-empty array`)}- if (item.toolCalls.length > LIMITS.maxTools) {- return fail('limit', `messages[${i}].toolCalls exceed ${LIMITS.maxTools}`)+ if (item.toolCalls.length > LIMITS.maxToolCalls) {+ return fail('limit', `messages[${i}].toolCalls exceed ${LIMITS.maxToolCalls}`)}const toolCalls = []for (let j = 0; j < item.toolCalls.length; j++) {@@ -227,7 +228,8 @@ function parseTools(raw) {if (raw === undefined) return ok(undefined)if (!Array.isArray(raw)) return fail('invalid', 'tools must be an array')if (raw.length === 0) return ok(undefined)- if (raw.length > LIMITS.maxTools) return fail('limit', `tools exceed ${LIMITS.maxTools}`)+ // The complete request is byte-bounded before parsing; definition count is+ // not an access limit for an approved connector catalog.const tools = []for (let i = 0; i < raw.length; i++) {const item = raw[i]diff --git a/packages/llm-provider-attempt-contract/index.d.ts b/packages/llm-provider-attempt-contract/index.d.tsindex a984ba5be..17acd19f9 100644--- a/packages/llm-provider-attempt-contract/index.d.ts+++ b/packages/llm-provider-attempt-contract/index.d.ts@@ -16,7 +16,7 @@ export declare const TICKET_TYP: 'codex-execution-ticket'export declare const LIMITS: {readonly maxRequestBodyBytes: 1048576readonly maxMessages: 128- readonly maxTools: 32+ readonly maxToolCalls: 32readonly maxOutputTokens: 16384readonly maxDeadlineMs: 300000readonly maxIdLength: 128diff --git a/packages/llm-provider-attempt-contract/index.test.cjs b/packages/llm-provider-attempt-contract/index.test.cjsindex 4fcff9392..0ce9f9b9b 100644--- a/packages/llm-provider-attempt-contract/index.test.cjs+++ b/packages/llm-provider-attempt-contract/index.test.cjs@@ -249,3 +249,65 @@ test('authorize response is metadata-only; redeem type is documented but not parassert.equal('accountId' in authorize.value, false)assert.equal(typeof contract.RedeemAttemptResponseSensitive, 'undefined')})++function catalog(count) {+ return Array.from({ length: count }, (_, index) => ({+ name: `eventasks__read_${index}`,+ description: 'Read an approved development record',+ parameters: { type: 'object', properties: { id: { type: 'string' } } },+ }))+}++for (const count of [1, 32, 33, 83, 150, 250]) {+ test(`preserves every definition in a ${count}-tool approved catalog`, () => {+ const tools = catalog(count)+ const parsed = contract.parseCodexCompletionRequestV1({ ...BASE, tools })+ assert.equal(parsed.ok, true, parsed.message)+ assert.deepEqual(parsed.value.tools, tools)+ const reordered = tools.map(({ name, description, parameters }) => ({ parameters, description, name }))+ const equivalent = contract.parseCodexCompletionRequestV1({ ...BASE, tools: reordered })+ assert.equal(equivalent.ok, true)+ assert.equal(contract.hashCodexCompletionRequestV1(parsed.value), contract.hashCodexCompletionRequestV1(equivalent.value))+ const changed = contract.parseCodexCompletionRequestV1({ ...BASE, tools: [...tools.slice(0, -1), { ...tools.at(-1), description: 'Changed capability' }] })+ assert.equal(changed.ok, true)+ assert.notEqual(contract.hashCodexCompletionRequestV1(parsed.value), contract.hashCodexCompletionRequestV1(changed.value))+ })+}++test('tool definition count never widens the independent assistant call limit', () => {+ assert.equal(contract.LIMITS.maxToolCalls, 32)+ assert.equal(Object.hasOwn(contract.LIMITS, 'maxTools'), false)+ for (const count of [32, 33]) {+ const parsed = contract.parseCodexCompletionRequestV1({+ ...BASE,+ tools: catalog(250),+ messages: [{ role: 'assistant', content: '', toolCalls: Array.from({ length: count }, (_, index) => ({+ id: `call-${index}`, name: `eventasks__read_${index}`, arguments: {},+ })) }],+ })+ assert.equal(parsed.ok, count === 32)+ if (!parsed.ok) assert.equal(parsed.message, 'messages[0].toolCalls exceed 32')+ }+})++test('large catalogs remain bounded by serialized request bytes including UTF-8', () => {+ const request = { ...BASE, tools: catalog(250) }+ const originalBytes = Buffer.byteLength(JSON.stringify(request), 'utf8')+ request.tools[249].description += 'x'.repeat(contract.LIMITS.maxRequestBodyBytes - originalBytes)+ assert.equal(Buffer.byteLength(JSON.stringify(request), 'utf8'), contract.LIMITS.maxRequestBodyBytes)+ assert.equal(contract.parseCodexCompletionRequestV1(request).ok, true)+ request.tools[249].description += 'é'+ assert.deepEqual(contract.parseCodexCompletionRequestV1(request), {+ ok: false, code: 'limit', message: 'request exceeds maxRequestBodyBytes',+ })+})++test('validates the last definition beyond the former count boundary', () => {+ for (const invalid of [{ name: 'invalid name' }, { parameters: { value: Infinity } }, { headers: {} }]) {+ const tools = catalog(250)+ tools[249] = { ...tools[249], ...invalid }+ const parsed = contract.parseCodexCompletionRequestV1({ ...BASE, tools })+ assert.equal(parsed.ok, false)+ assert.match(parsed.message, /tools\[249\]/)+ }+})diff --git a/scripts/e2e/prepare-codex-approved-tools.mjs b/scripts/e2e/prepare-codex-approved-tools.mjsnew file mode 100644index 000000000..d29ebe317--- /dev/null+++ b/scripts/e2e/prepare-codex-approved-tools.mjs@@ -0,0 +1,575 @@+import { spawn, spawnSync } from 'node:child_process'+import { randomBytes } from 'node:crypto'+import fs from 'node:fs'+import net from 'node:net'+import path from 'node:path'+import { fileURLToPath, pathToFileURL } from 'node:url'++const repo = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..')+const sizes = [83, 150, 250]+const fixtureImage = 'clerum/codex-approved-tools-mcp-e2e:test'+const proxyImage = 'clerum/codex-approved-tools-proxy-e2e:test'+const proxyFlags = [+ 'CODEX_APPROVED_TOOLS_TEST_ONLY',+ 'CODEX_APPROVED_TOOLS_MINIKUBE_PROFILE',+ 'NODE_ENV',+]++export function makeScenarios(run, ports, probePort) {+ if (!/^approved-tools-[a-f0-9]{12}$/.test(run)) throw new Error('Invalid fixture run identity')+ if (+ ports.length !== 3 ||+ new Set([...ports, probePort]).size !== 4 ||+ [...ports, probePort].some(p => !Number.isInteger(p) || p < 1024 || p > 65535)+ )+ throw new Error('Invalid fixture ports')+ return sizes.map((catalogSize, index) => ({+ catalogSize,+ runId: `${run}-${catalogSize}`,+ agentName: `${run}-agent-${catalogSize}`,+ agentDisplayName: `Codex tools ${catalogSize} ${run}`,+ contextName: `${run}-context-${catalogSize}`,+ connectorName: `${run}-mcp-${catalogSize}`,+ subscriptionName: `Codex fixture ${catalogSize} ${run}`,+ connectionKey: `${run}-grant-${catalogSize}`,+ modelName: 'gpt-5.3-codex',+ modelLabel: 'Codex isolated tool test',+ fixtureUrl: `http://127.0.0.1:${ports[index]}`,+ upstreamEvidenceUrl: `http://127.0.0.1:${probePort}`,+ }))+}++export function makeResources(scenarios) {+ return scenarios.flatMap(s => {+ const labels = {+ 'evenfire.ai/e2e-suite': 'codex-approved-tools',+ 'evenfire.ai/e2e-run': s.runId,+ }+ return [+ {+ apiVersion: 'clerum.io/v1alpha1',+ kind: 'Context',+ metadata: { name: s.contextName, namespace: 'mcp-server', labels },+ spec: { contextId: s.contextName, mcpServers: [] },+ },+ {+ apiVersion: 'clerum.io/v1alpha1',+ kind: 'Host',+ metadata: { name: s.agentName, namespace: 'mcp-host', labels },+ spec: {+ host: s.agentDisplayName,+ contextRef: s.contextName,+ model: { provider: 'codex-subscription', name: s.modelName, connectionRef: 'unassigned' },+ channels: [],+ },+ },+ {+ apiVersion: 'clerum.io/v1alpha1',+ kind: 'McpServer',+ metadata: { name: s.connectorName, namespace: 'mcp-server', labels },+ spec: {+ contextRef: s.contextName,+ image: fixtureImage,+ imagePullPolicy: 'Never',+ enabled: true,+ description: `Isolated approved-tools catalog ${s.catalogSize}`,+ transport: {+ type: 'streamableHttp',+ url: `http://${s.connectorName}.mcp-server.svc.cluster.local:8080/mcp`,+ port: 8080,+ },+ healthCheck: { port: 8080 },+ auth: { type: 'none' },+ env: [+ { name: 'CATALOG_SIZE', value: String(s.catalogSize) },+ { name: 'RUN_ID', value: s.runId },+ { name: 'BIND_ADDRESS', value: '0.0.0.0' },+ ],+ resources: {+ requests: { cpu: '10m', memory: '32Mi' },+ limits: { cpu: '200m', memory: '128Mi' },+ },+ },+ },+ ]+ })+}++export function assertResourceRoundTrip(expected, observed) {+ if (Array.isArray(expected)) {+ if (!Array.isArray(observed) || observed.length !== expected.length)+ throw new Error('Fixture array was not preserved by the API server')+ expected.forEach((item, index) => assertResourceRoundTrip(item, observed[index]))+ } else if (expected && typeof expected === 'object') {+ if (!observed || typeof observed !== 'object')+ throw new Error('Fixture object was not preserved by the API server')+ for (const [key, value] of Object.entries(expected))+ assertResourceRoundTrip(value, observed[key])+ } else if (expected !== observed)+ throw new Error('Fixture field was not preserved by the API server')+}++function command(executable, args, { input, timeout = 60_000, inherit = false } = {}) {+ const result = spawnSync(executable, args, {+ cwd: repo,+ input,+ encoding: 'utf8',+ timeout,+ maxBuffer: 4 * 1024 * 1024,+ stdio: inherit ? 'inherit' : ['pipe', 'pipe', 'pipe'],+ })+ // Subprocess output can include runtime state. Only callers parse their+ // specific safe result; never embed raw stderr/stdout in an exception.+ if (result.error || result.signal || result.status !== 0)+ throw new Error(`${path.basename(executable)} operation failed`)+ return result.stdout ?? ''+}++function required(key) {+ const value = process.env[key]+ if (!value) throw new Error(`Missing ${key}`)+ return value+}++async function freePort() {+ const server = net.createServer()+ await new Promise((resolve, reject) => {+ server.once('error', reject)+ server.listen(0, '127.0.0.1', resolve)+ })+ const port = server.address().port+ await new Promise((resolve, reject) => server.close(err => (err ? reject(err) : resolve())))+ return port+}++async function waitHttp(url, accept) {+ const deadline = Date.now() + 45_000+ while (Date.now() < deadline) {+ try {+ const response = await fetch(url, { signal: AbortSignal.timeout(2000) })+ if (response.ok && accept(await response.json())) return+ } catch {+ /* bounded readiness polling of a freshly started owned process */+ }+ await new Promise(resolve => setTimeout(resolve, 200))+ }+ throw new Error('Fixture readiness deadline exceeded')+}++export function validateRunDirectory(canonical, evidence) {+ const root = fs.realpathSync(path.join(canonical, '.local-notes/infra/runs'))+ if (path.relative(fs.realpathSync(canonical), root) !== '.local-notes/infra/runs')+ throw new Error('Unsafe canonical evidence root')+ const resolved = fs.realpathSync(evidence)+ if (path.dirname(resolved) !== root || fs.lstatSync(evidence).isSymbolicLink())+ throw new Error('Evidence must be a dedicated canonical run directory')+ return resolved+}++async function main() {+ process.umask(0o077)+ if (!process.version.startsWith('v24.')) throw new Error('Node24 is required')+ const action = process.argv[2] ?? 'prepare'+ if (!['prepare', 'run', 'restore'].includes(action))+ throw new Error('Expected prepare, run, or restore')+ const profile = required('MINIKUBE_PROFILE')+ if (+ profile !== required('CONTROL_API_REAL_PG_CONTEXT') ||+ !/^clerum-.+-[a-f0-9]{7,8}$/.test(profile)+ )+ throw new Error('Owned branch Minikube context required')+ command('bash', ['scripts/minikube/require-t2-mutation-lock.sh'])+ const evidence = validateRunDirectory(+ required('APPROVED_TOOLS_CANONICAL_ROOT'),+ required('APPROVED_TOOLS_EVIDENCE_DIR')+ )+ const statePath = path.join(evidence, 'fixture-state.json')+ const kc = args => ['--context=' + profile, '--request-timeout=30s', ...args]+ const kubectl = (args, options) => command('kubectl', kc(args), options)+ const head = command('git', ['rev-parse', 'HEAD']).trim()+ const ownerLibrary = path.join(repo, 'scripts/minikube/port-forward-owner.sh')+ const owner = (fn, args) =>+ command('bash', [+ '-c',+ 'source "$1"; shift; fn="$1"; shift; "$fn" "$@"',+ 'pf-owner',+ ownerLibrary,+ fn,+ ...args,+ ])+ const validateForward = binding => {+ owner('pf_owner_record_process_matches', [+ binding.record,+ profile,+ profile,+ repo,+ binding.namespace,+ binding.service,+ String(binding.localPort),+ String(binding.remotePort),+ ])+ }+ let state+ const save = () =>+ fs.writeFileSync(statePath, JSON.stringify(state, null, 2) + '\n', { mode: 0o600 })+ async function restore() {+ if (!state || state.profile !== profile || state.worktree !== repo || state.head !== head)+ throw new Error('Fixture ownership mismatch')+ const deployment = JSON.parse(+ kubectl(['-n', 'control-plane', 'get', 'deployment/codex-llm-proxy', '-o', 'json'])+ )+ const container = deployment.spec.template.spec.containers.find(+ c => c.name === 'codex-llm-proxy'+ )+ if (deployment.metadata.uid !== state.proxyUid || !container)+ throw new Error('Proxy deployment ownership changed')+ if (container.image === proxyImage) {+ const env = proxyFlags.map(name => ({ name, $patch: 'delete' }))+ if (state.originalNodeEnv) env[2] = state.originalNodeEnv+ kubectl([+ '-n',+ 'control-plane',+ 'patch',+ 'deployment/codex-llm-proxy',+ '--type=strategic',+ '-p',+ JSON.stringify({+ spec: {+ template: {+ spec: {+ containers: [+ {+ name: 'codex-llm-proxy',+ image: state.originalProxyImage,+ imagePullPolicy: state.originalImagePullPolicy,+ env,+ },+ ],+ },+ },+ },+ }),+ ])+ kubectl(+ [+ '-n',+ 'control-plane',+ 'rollout',+ 'status',+ 'deployment/codex-llm-proxy',+ '--timeout=180s',+ ],+ { timeout: 190_000 }+ )+ } else if (container.image !== state.originalProxyImage)+ throw new Error('Refusing to restore an unrelated proxy image')+ for (const binding of state.forwards) {+ owner('pf_owner_cleanup_record', [+ binding.record,+ profile,+ profile,+ repo,+ binding.namespace,+ binding.service,+ String(binding.localPort),+ String(binding.remotePort),+ ])+ }+ state.restored = true+ save()+ }+ if (action === 'restore') {+ if (fs.lstatSync(statePath).isSymbolicLink()) throw new Error('Unsafe state file')+ state = JSON.parse(fs.readFileSync(statePath, 'utf8'))+ await restore()+ return+ }+ if (required('APPROVED_TOOLS_UPSTREAM_MODE') !== 'deterministic')+ throw new Error(+ 'Synthetic preparation is deterministic-only; real accounts require separate authorized preconditions'+ )+ if (fs.existsSync(statePath))+ throw new Error('Fresh fixture run required; restore previous run explicitly')+ const seedFile = path.join(+ repo,+ 'tests/e2e/fixtures/codex-subscription/approved-tools-setup/seed.mjs'+ )+ const seedSource = fs.readFileSync(seedFile, 'utf8')+ for (const name of [+ 'TEST_ADMIN_USERNAME',+ 'TEST_ADMIN_PASSWORD',+ 'TEST_USER_EMAIL',+ 'TEST_USER_PASSWORD',+ ])+ required(name)+ // The supported Minikube overlays enable these already. Refuse a stale or+ // differently configured runtime; fixture setup must not weaken feature gates.+ for (const [deploymentName, keys] of [+ [+ 'control-api',+ ['CONTROL_API_CODEX_SUBSCRIPTION_ENABLED', 'CODEX_LLM_PROXY_EXECUTION_ENABLED'],+ ],+ ['codex-llm-proxy', ['CODEX_LLM_PROXY_EXECUTION_ENABLED']],+ ]) {+ const check = `process.stdout.write(JSON.stringify(${JSON.stringify(keys)}.every(key => process.env[key] === 'true')))`+ if (+ kubectl([+ '-n',+ 'control-plane',+ 'exec',+ `deployment/${deploymentName}`,+ '--',+ 'node',+ '-e',+ check,+ ]).trim() !== 'true'+ )+ throw new Error('Codex Minikube feature gates are not enabled in the running workload')+ }+ const hostFlags = JSON.parse(+ kubectl(['-n', 'mcp-host', 'get', 'configmap/mcp-host-config', '-o', 'json'])+ )+ if (hostFlags.data?.MCP_HOST_CODEX_SUBSCRIPTION_ENABLED !== 'true')+ throw new Error('Host Codex feature gate is not enabled by the Minikube overlay')+ const profilePidDirectory = fs.realpathSync(+ path.join(required('T2_PROFILE_ROOT'), profile, 'pids')+ )+ const run = 'approved-tools-' + randomBytes(6).toString('hex')+ const ports = []+ while (ports.length < 4) {+ const port = await freePort()+ if (!ports.includes(port)) ports.push(port)+ }+ const scenarios = makeScenarios(run, ports.slice(0, 3), ports[3])+ const resources = makeResources(scenarios)+ const proposed = { apiVersion: 'v1', kind: 'List', items: resources }+ const validated = JSON.parse(+ kubectl(['create', '--dry-run=server', '-f', '-', '-o', 'json'], {+ input: JSON.stringify(proposed),+ })+ )+ assertResourceRoundTrip(resources, validated.items)+ const proxyService = JSON.parse(+ kubectl(['-n', 'control-plane', 'get', 'service/codex-llm-proxy', '-o', 'json'])+ )+ if (!proxyService.spec.ports.some(p => p.port === 9090))+ throw new Error('Proxy probe Service port is missing')+ const deployment = JSON.parse(+ kubectl(['-n', 'control-plane', 'get', 'deployment/codex-llm-proxy', '-o', 'json'])+ )+ const container = deployment.spec.template.spec.containers.find(c => c.name === 'codex-llm-proxy')+ if (+ !container ||+ container.image === proxyImage ||+ (container.env ?? []).some(e => proxyFlags.slice(0, 2).includes(e.name))+ )+ throw new Error('Proxy already has test ownership')+ const originalNodeEnv = container.env?.find(e => e.name === 'NODE_ENV')+ if (originalNodeEnv && !['production', 'development', 'test'].includes(originalNodeEnv.value))+ throw new Error('Unsupported explicit NODE_ENV binding')+ state = {+ profile,+ worktree: repo,+ head,+ run,+ scenarios,+ forwards: [],+ resources: resources.map(r => ({+ kind: r.kind,+ name: r.metadata.name,+ namespace: r.metadata.namespace,+ })),+ originalProxyImage: container.image,+ originalImagePullPolicy: container.imagePullPolicy,+ originalNodeEnv,+ proxyUid: deployment.metadata.uid,+ restored: false,+ }+ fs.writeFileSync(statePath, JSON.stringify(state, null, 2) + '\n', { flag: 'wx', mode: 0o600 })+ async function startForward(namespace, service, localPort, remotePort, health, accept) {+ const record = path.join(profilePidDirectory, `${run}-${service}.pid`)+ const log = fs.openSync(path.join(evidence, `pf-${service}.log`), 'wx', 0o600)+ const child = spawn(+ 'kubectl',+ [+ `--context=${profile}`,+ '-n',+ namespace,+ 'port-forward',+ '--address=127.0.0.1',+ `svc/${service}`,+ `${localPort}:${remotePort}`,+ ],+ { cwd: repo, detached: true, stdio: ['ignore', log, log] }+ )+ await new Promise((resolve, reject) => {+ child.once('spawn', resolve)+ child.once('error', reject)+ })+ child.unref()+ fs.closeSync(log)+ const binding = { record, namespace, service, localPort, remotePort }+ try {+ owner('pf_owner_record_process', [+ record,+ String(child.pid),+ profile,+ profile,+ repo,+ namespace,+ service,+ String(localPort),+ String(remotePort),+ ])+ } catch (error) {+ child.kill('SIGTERM')+ throw error+ }+ state.forwards.push(binding)+ save()+ await waitHttp(health, accept)+ validateForward(binding)+ }+ try {+ const created = JSON.parse(+ kubectl(['create', '-f', '-', '-o', 'json'], { input: JSON.stringify(proposed) })+ )+ assertResourceRoundTrip(resources, created.items)+ kubectl([+ '-n',+ 'control-plane',+ 'patch',+ 'deployment/codex-llm-proxy',+ '--type=strategic',+ '-p',+ JSON.stringify({+ spec: {+ template: {+ spec: {+ containers: [+ {+ name: 'codex-llm-proxy',+ image: proxyImage,+ imagePullPolicy: 'Never',+ env: [+ { name: 'NODE_ENV', value: 'test' },+ { name: 'CODEX_APPROVED_TOOLS_TEST_ONLY', value: '1' },+ { name: 'CODEX_APPROVED_TOOLS_MINIKUBE_PROFILE', value: profile },+ ],+ },+ ],+ },+ },+ },+ }),+ ])+ kubectl(+ ['-n', 'control-plane', 'rollout', 'status', 'deployment/codex-llm-proxy', '--timeout=180s'],+ { timeout: 190_000 }+ )+ await startForward(+ 'control-plane',+ 'codex-llm-proxy',+ ports[3],+ 9090,+ `${scenarios[0].upstreamEvidenceUrl}/approved-tools/evidence`,+ value => Array.isArray(value.requests)+ )+ for (const [index, scenario] of scenarios.entries()) {+ kubectl(+ [+ '-n',+ 'mcp-server',+ 'wait',+ '--for=create',+ `deployment/${scenario.connectorName}`,+ '--timeout=180s',+ ],+ { timeout: 190_000 }+ )+ kubectl(+ [+ '-n',+ 'mcp-server',+ 'rollout',+ 'status',+ `deployment/${scenario.connectorName}`,+ '--timeout=180s',+ ],+ { timeout: 190_000 }+ )+ await startForward(+ 'mcp-server',+ scenario.connectorName,+ ports[index],+ 8080,+ `${scenario.fixtureUrl}/health`,+ value =>+ value.ready === true &&+ value.catalogSize === scenario.catalogSize &&+ value.runId === scenario.runId+ )+ }+ const seedInput = {+ mode: 'deterministic',+ profile,+ scenarios,+ userDisplayName: run,+ adminUsername: required('TEST_ADMIN_USERNAME'),+ adminPassword: required('TEST_ADMIN_PASSWORD'),+ userEmail: required('TEST_USER_EMAIL'),+ userPassword: required('TEST_USER_PASSWORD'),+ }+ kubectl(+ [+ '-n',+ 'control-plane',+ 'exec',+ '-i',+ 'deployment/control-api',+ '--',+ 'node',+ '--input-type=module',+ '--eval',+ seedSource,+ ],+ { input: JSON.stringify(seedInput), timeout: 90_000 }+ )+ fs.writeFileSync(+ path.join(evidence, 'scenarios.json'),+ JSON.stringify(scenarios, null, 2) + '\n',+ { flag: 'wx', mode: 0o600 }+ )+ state.ready = true+ save()+ if (action === 'run') {+ process.env.APPROVED_TOOLS_SCENARIOS = JSON.stringify(scenarios)+ command(process.execPath, ['scripts/e2e/run-codex-approved-tools.mjs'], {+ timeout: 25 * 60_000,+ inherit: true,+ })+ await restore()+ }+ process.stdout.write(+ `${JSON.stringify({ fixture: 'ready', mode: 'deterministic', scenariosFile: path.join(evidence, 'scenarios.json'), restored: state.restored })}\n`+ )+ } catch (error) {+ try {+ await restore()+ } catch {+ process.stderr.write(+ 'Fixture restore failed; use the explicit restore target before another gate\n'+ )+ }+ throw error+ }+}++if (process.argv[1] && import.meta.url === pathToFileURL(path.resolve(process.argv[1])).href) {+ main().catch(() => {+ process.stderr.write(+ 'Approved tools preparation failed; review bounded sanitized fixture evidence\n'+ )+ process.exitCode = 1+ })+}diff --git a/scripts/e2e/prepare-codex-approved-tools.test.mjs b/scripts/e2e/prepare-codex-approved-tools.test.mjsnew file mode 100644index 000000000..eeb378f58--- /dev/null+++ b/scripts/e2e/prepare-codex-approved-tools.test.mjs@@ -0,0 +1,129 @@+import assert from 'node:assert/strict'+import fs from 'node:fs'+import os from 'node:os'+import path from 'node:path'+import test from 'node:test'+import {+ IMAGES,+ minikubeVerifyRefs,+ publishedImages,+ pullInGhcrMode,+} from '../release/images-manifest.mjs'+import {+ assertResourceRoundTrip,+ makeResources,+ makeScenarios,+ validateRunDirectory,+} from './prepare-codex-approved-tools.mjs'++const scenarios = makeScenarios('approved-tools-a1b2c3d4e5f6', [43101, 43102, 43103], 43104)++test('explicit tools fixtures never enter default publish, acquisition or verification sets', () => {+ for (const name of ['codex-approved-tools-mcp-e2e', 'codex-approved-tools-proxy-e2e']) {+ assert.ok(IMAGES.some(image => image.name === name))+ assert.equal(+ publishedImages().some(image => image.name === name),+ false+ )+ assert.equal(+ pullInGhcrMode().some(image => image.name === name),+ false+ )+ for (const options of [+ { mode: 'local' },+ { mode: 'ghcr', tag: 'test' },+ { mode: 'ghcr', tag: 'test', includeE2eFixtures: true },+ ]) {+ assert.equal(+ minikubeVerifyRefs(options).some(ref => ref.includes(name)),+ false+ )+ }+ }+})++test('all three isolated catalogs start without connector grants or subscription bindings', () => {+ assert.deepEqual(+ scenarios.map(s => s.catalogSize),+ [83, 150, 250]+ )+ assert.equal(new Set(scenarios.map(s => s.contextName)).size, 3)+ assert.equal(new Set(scenarios.map(s => s.fixtureUrl)).size, 3)+ const resources = makeResources(scenarios)+ assert.equal(resources.length, 9)+ for (const context of resources.filter(r => r.kind === 'Context'))+ assert.deepEqual(context.spec.mcpServers, [])+ for (const host of resources.filter(r => r.kind === 'Host')) {+ assert.equal(host.spec.model.connectionRef, 'unassigned')+ assert.equal(host.spec.model.provider, 'codex-subscription')+ assert.equal(host.spec.llmPolicy, undefined)+ assert.equal(host.spec.secretRef, undefined)+ }+ for (const mcp of resources.filter(r => r.kind === 'McpServer')) {+ const scenario = scenarios.find(s => s.connectorName === mcp.metadata.name)+ assert.equal(mcp.spec.image, 'clerum/codex-approved-tools-mcp-e2e:test')+ assert.equal(mcp.spec.imagePullPolicy, 'Never')+ assert.equal(mcp.spec.transport.port, 8080)+ assert.equal(mcp.spec.env.find(v => v.name === 'RUN_ID').value, scenario.runId)+ assert.equal(+ mcp.spec.env.find(v => v.name === 'CATALOG_SIZE').value,+ String(scenario.catalogSize)+ )+ }+})++test('rejects traversal identities, repeated or privileged fixture ports', () => {+ assert.throws(() => makeScenarios('../escape', [43101, 43102, 43103], 43104))+ assert.throws(() => makeScenarios('approved-tools-a1b2c3d4e5f6', [43101, 43101, 43103], 43104))+ assert.throws(() => makeScenarios('approved-tools-a1b2c3d4e5f6', [80, 43102, 43103], 43104))+})++test('roundtrip rejects pruned display/model/empty grant fields while accepting server defaults', () => {+ const resources = makeResources(scenarios)+ const observed = structuredClone(resources)+ observed[0].metadata.uid = 'server-generated-id'+ assert.doesNotThrow(() => assertResourceRoundTrip(resources, observed))+ for (const [index, key] of [+ [0, 'mcpServers'],+ [1, 'host'],+ [1, 'model'],+ ]) {+ const pruned = structuredClone(observed)+ delete pruned[index].spec[key]+ assert.throws(() => assertResourceRoundTrip(resources, pruned))+ }+})++test('evidence accepts only exact canonical run children and rejects symlinks', () => {+ const root = fs.mkdtempSync(path.join(os.tmpdir(), 'approved-tools-setup-'))+ try {+ const runs = path.join(root, '.local-notes/infra/runs')+ const run = path.join(runs, 'one')+ fs.mkdirSync(run, { recursive: true })+ assert.equal(validateRunDirectory(root, run), fs.realpathSync(run))+ assert.throws(() => validateRunDirectory(root, runs))+ const link = path.join(runs, 'link')+ fs.symlinkSync(run, link)+ assert.throws(() => validateRunDirectory(root, link))+ fs.mkdirSync(path.join(run, 'nested'))+ assert.throws(() => validateRunDirectory(root, path.join(run, 'nested')))+ } finally {+ fs.rmSync(root, { recursive: true, force: true })+ }+})++test('missing seed and lease fail before first cluster mutation; no image acquisition in preparation', () => {+ const source = fs.readFileSync(+ new URL('./prepare-codex-approved-tools.mjs', import.meta.url),+ 'utf8'+ )+ const mutation = source.indexOf("kubectl(['create'")+ assert.ok(+ source.indexOf("command('bash', ['scripts/minikube/require-t2-mutation-lock.sh'])") < mutation+ )+ assert.ok(source.indexOf("fs.readFileSync(seedFile, 'utf8')") < mutation)+ assert.equal(source.includes('build-images.sh'), false)+ assert.ok(source.includes("owner('pf_owner_record_process_matches'"))+ assert.ok(source.includes('state.originalImagePullPolicy'))+ assert.ok(source.includes('container.image !== state.originalProxyImage'))+})diff --git a/scripts/e2e/run-codex-approved-tools.mjs b/scripts/e2e/run-codex-approved-tools.mjsnew file mode 100644index 000000000..cde900f42--- /dev/null+++ b/scripts/e2e/run-codex-approved-tools.mjs@@ -0,0 +1,162 @@+import { spawnSync } from 'node:child_process'+import fs from 'node:fs'+import path from 'node:path'+import { fileURLToPath, pathToFileURL } from 'node:url'++const repo = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..')+const playwright = path.join(repo, 'tests/e2e/playwright')+const specFile = 'codex-subscription-approved-tools.spec.ts'+export const expectedTitles = [+ 'unauthenticated agent route guard prevents connector use',+ ...[83, 150, 250].map(size => `approved tools ${size}: selective receipt and revoked connector`),+]++export function validateReport(report) {+ const specs = []+ function collect(suites) {+ if (!Array.isArray(suites)) throw new Error('Incomplete Playwright suites')+ for (const suite of suites) {+ specs.push(...(suite.specs ?? []))+ collect(suite.suites ?? [])+ }+ }+ collect(report.suites)+ if (+ report.errors?.length ||+ report.stats?.unexpected !== 0 ||+ report.stats?.skipped !== 0 ||+ report.stats?.flaky !== 0 ||+ report.stats?.expected !== expectedTitles.length ||+ specs.length !== expectedTitles.length+ ) {+ throw new Error('Incomplete, failed, flaky or skipped approved-tools report')+ }+ for (const title of expectedTitles) {+ const matches = specs.filter(+ spec =>+ spec.title === title &&+ [specFile, `desktop/${specFile}`, path.join(playwright, 'desktop', specFile)].includes(+ spec.file+ )+ )+ if (matches.length !== 1) throw new Error(`Required case missing or duplicated: ${title}`)+ const spec = matches[0]+ if (!spec.ok || spec.tests?.length !== 1) throw new Error(`Case not green: ${title}`)+ const test = spec.tests[0]+ if (+ test.expectedStatus !== 'passed' ||+ test.status !== 'expected' ||+ test.results?.length !== 1 ||+ test.results[0].status !== 'passed' ||+ test.results[0].errors?.length+ ) {+ throw new Error(`Case did not pass once without retries: ${title}`)+ }+ }+ return { tests: specs.length, skipped: 0, failed: 0 }+}++function required(name) {+ const value = process.env[name]?.trim()+ if (!value) throw new Error(`Missing required precondition: ${name}`)+ return value+}++function runNode(args, cwd, env, timeout) {+ const result = spawnSync(process.execPath, args, {+ cwd,+ env,+ stdio: 'inherit',+ timeout,+ killSignal: 'SIGTERM',+ })+ if (result.error || result.signal || result.status !== 0)+ throw new Error('Approved-tools process failed, timed out or was interrupted')+}++function main() {+ if (process.versions.node.split('.')[0] !== '24')+ throw new Error('Desktop certification requires Node 24')+ for (const key of [+ 'MINIKUBE_PROFILE',+ 'CONTROL_API_REAL_PG_CONTEXT',+ 'CONTROL_UI_URL',+ 'EXTERNAL_REST_API_BASE_URL',+ 'RPC_PROXY_BASE_URL',+ 'APPROVED_TOOLS_SCENARIOS',+ ])+ required(key)+ const mode = required('APPROVED_TOOLS_UPSTREAM_MODE')+ if (!['deterministic', 'real'].includes(mode))+ throw new Error('Select deterministic or real upstream mode explicitly')+ if (mode === 'real' && process.env.CODEX_REAL_UPSTREAM_CONFIRM !== '1')+ throw new Error('Real subscription use requires explicit confirmation')+ const canonical = fs.realpathSync(required('APPROVED_TOOLS_CANONICAL_ROOT'))+ const runRoot = fs.realpathSync(path.join(canonical, '.local-notes/infra/runs'))+ if (path.relative(canonical, runRoot) !== '.local-notes/infra/runs')+ throw new Error('Canonical evidence root must not escape through a symlink')+ const evidence = fs.realpathSync(required('APPROVED_TOOLS_EVIDENCE_DIR'))+ const relative = path.relative(runRoot, evidence)+ if (!relative || relative.startsWith('..') || path.isAbsolute(relative))+ throw new Error('Evidence must be a dedicated canonical .local-notes/infra/runs child')+ const report = path.join(evidence, `approved-tools-${mode}.json`)+ const artifacts = path.join(evidence, `artifacts-${mode}`)+ if (+ fs.lstatSync(report, { throwIfNoEntry: false }) ||+ fs.lstatSync(artifacts, { throwIfNoEntry: false })+ )+ throw new Error('Refusing to reuse an existing report; choose a fresh evidence directory')+ process.umask(0o077)+ const env = {+ ...process.env,+ PATH: `${path.dirname(process.execPath)}${path.delimiter}${process.env.PATH ?? ''}`,+ PLAYWRIGHT_DESKTOP_BUILT: 'true',+ APPROVED_TOOLS_REPORT: report,+ APPROVED_TOOLS_ARTIFACTS: artifacts,+ }+ // Run the repository invariant; a dependency install exit code is insufficient.+ const electronCheck = spawnSync('npm', ['run', 'verify:electron'], {+ cwd: path.join(repo, 'desktop-app'),+ env,+ stdio: 'inherit',+ timeout: 30_000,+ })+ if (electronCheck.error || electronCheck.signal || electronCheck.status !== 0)+ throw new Error('Desktop Electron verification failed')+ if (!fs.statSync(path.join(repo, 'desktop-app/dist/main.js')).isFile())+ throw new Error('Desktop build missing')+ const audit = spawnSync(+ 'python3',+ [+ 'tools/e2e_static_audit.py',+ `tests/e2e/playwright/desktop/${specFile}`,+ 'tests/e2e/playwright/helpers/approved-tools-scenarios.ts',+ ],+ { cwd: repo, env, stdio: 'inherit', timeout: 30_000 }+ )+ if (audit.error || audit.signal || audit.status !== 0)+ throw new Error('E2E Guardian audit failed')+ runNode(+ [+ path.join(playwright, 'node_modules/@playwright/test/cli.js'),+ 'test',+ '--config=playwright.codex-approved-tools.config.ts',+ ],+ playwright,+ env,+ 22 * 60_000+ )+ const verified = validateReport(JSON.parse(fs.readFileSync(report, 'utf8')))+ process.stdout.write(+ `${JSON.stringify({ lane: 'Playwright', upstream: mode, ...verified, report })}\n`+ )+}++if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {+ try {+ main()+ } catch (error) {+ process.stderr.write(`${error.message}\n`)+ process.exitCode = 1+ }+}diff --git a/scripts/e2e/run-codex-approved-tools.test.mjs b/scripts/e2e/run-codex-approved-tools.test.mjsnew file mode 100644index 000000000..f12630d85--- /dev/null+++ b/scripts/e2e/run-codex-approved-tools.test.mjs@@ -0,0 +1,68 @@+import assert from 'node:assert/strict'+import { test } from 'node:test'+import { expectedTitles, validateReport } from './run-codex-approved-tools.mjs'++// Synthetic reporter records exercise false-green rejection, not product data.+function greenReport() {+ return {+ errors: [],+ stats: { expected: 4, unexpected: 0, skipped: 0, flaky: 0 },+ suites: [+ {+ specs: expectedTitles.map(title => ({+ title,+ file: 'desktop/codex-subscription-approved-tools.spec.ts',+ ok: true,+ tests: [+ {+ expectedStatus: 'passed',+ status: 'expected',+ results: [{ status: 'passed', errors: [] }],+ },+ ],+ })),+ suites: [],+ },+ ],+ }+}+test('accepts exactly the required four green cases', () => {+ assert.deepEqual(validateReport(greenReport()), { tests: 4, skipped: 0, failed: 0 })+})+const corruptions = {+ 'missing case': report => report.suites[0].specs.pop(),+ 'duplicate case': report => {+ report.suites[0].specs[0].title = expectedTitles[1]+ },+ 'different physical file': report => {+ report.suites[0].specs[0].file = 'other.spec.ts'+ },+ 'same basename in a different directory': report => {+ report.suites[0].specs[0].file = 'other/codex-subscription-approved-tools.spec.ts'+ },+ 'zero executions': report => {+ report.suites[0].specs[0].tests[0].results = []+ },+ 'teardown error': report => report.errors.push({ message: 'teardown failed' }),+ skip: report => {+ report.stats.skipped = 1+ },+ 'individual skipped result': report => {+ report.suites[0].specs[0].tests[0].results[0].status = 'skipped'+ },+ 'expected failure': report => {+ report.suites[0].specs[0].tests[0].expectedStatus = 'failed'+ },+ retry: report =>+ report.suites[0].specs[0].tests[0].results.push({ status: 'passed', errors: [] }),+ 'incomplete reporter': report => {+ delete report.stats+ },+}+for (const [name, corrupt] of Object.entries(corruptions)) {+ test(`rejects ${name}`, () => {+ const report = greenReport()+ corrupt(report)+ assert.throws(() => validateReport(report))+ })+}diff --git a/scripts/minikube/build-images.sh b/scripts/minikube/build-images.shindex c4cdcbf6d..371ceb038 100755--- a/scripts/minikube/build-images.sh+++ b/scripts/minikube/build-images.sh@@ -351,6 +351,17 @@ KNOWN_BUILD_NAMES=(workspace-files-controller)+# These isolated test images are opt-in only. Their acquisition must happen+# before T2 reconciliation, never inside the final Playwright command.+if [ "$ONLY_SVC" = codex-approved-tools-mcp-e2e ]; then+ ALL_IMAGES+=("clerum/codex-approved-tools-mcp-e2e:test")+ KNOWN_BUILD_NAMES+=(codex-approved-tools-mcp-e2e)+fi+if [ "$ONLY_SVC" = codex-approved-tools-proxy-e2e ]; then+ ALL_IMAGES+=("clerum/codex-approved-tools-proxy-e2e:test")+ KNOWN_BUILD_NAMES+=(codex-approved-tools-proxy-e2e)+fi+if [ "$MINIKUBE_BUILD_DESKTOP_IMAGE" = "true" ]; thenALL_IMAGES+=("clerum/mcp-host-desktop:test")KNOWN_BUILD_NAMES+=(mcp-host-desktop)@@ -939,6 +950,11 @@ build_image() {if [ -n "$dockerfile" ]; thendocker_args+=(-f "$dockerfile")fi+ if [ "$name" = codex-approved-tools-proxy-e2e ]; then+ # The public target builds this production tag immediately beforehand under+ # the same profile lease. The test image never replaces that tag.+ docker_args+=(--build-arg CODEX_PROXY_IMAGE=clerum/codex-llm-proxy:test)+ filocal build_cmd=(docker build "${docker_args[@]}" "$dir")local build_status=0docker_cli_run_public "build-${name}" "$MINIKUBE_DOCKER_BUILD_TIMEOUT_SECONDS" \@@ -1138,6 +1154,17 @@ build_image "codex-llm-proxy" \"clerum/codex-llm-proxy:test" \"${PROJECT_DIR}/codex-llm-proxy/Dockerfile"+if [ "$ONLY_SVC" = codex-approved-tools-mcp-e2e ]; then+ build_image codex-approved-tools-mcp-e2e "${PROJECT_DIR}" \+ clerum/codex-approved-tools-mcp-e2e:test \+ "${PROJECT_DIR}/tests/e2e/fixtures/codex-subscription/approved-tools/Dockerfile"+fi+if [ "$ONLY_SVC" = codex-approved-tools-proxy-e2e ]; then+ build_image codex-approved-tools-proxy-e2e "${PROJECT_DIR}" \+ clerum/codex-approved-tools-proxy-e2e:test \+ "${PROJECT_DIR}/tests/e2e/fixtures/codex-subscription/approved-tools-proxy/Dockerfile"+fi+build_image "webhook-gateway" \"${PROJECT_DIR}/webhook-gateway" \"clerum/webhook-gateway:test"diff --git a/scripts/tests/test-codex-subscription-t0.sh b/scripts/tests/test-codex-subscription-t0.shindex 70789afaa..81ec3bd36 100755--- a/scripts/tests/test-codex-subscription-t0.sh+++ b/scripts/tests/test-codex-subscription-t0.sh@@ -197,6 +197,7 @@ run_group "control-api" "control-api" \"test/routes.usageEvents.test.ts"run_group "codex-llm-proxy" "codex-llm-proxy" \+ "test/approvedToolsUpstream.test.ts" \"test/codexTransport.conformance.test.ts" \"test/controlApiClient.test.ts" \"test/originPolicy.test.ts" \@@ -204,6 +205,15 @@ run_group "codex-llm-proxy" "codex-llm-proxy" \"test/server.security.test.ts"run_group "mcp-host" "mcp-host" \+ "src/capabilities/toolCatalogTools.test.ts" \+ "src/core/orchestration/__tests__/toolUseLoop.spillover.test.ts" \+ "src/mcp/__tests__/managerDelimiterDispatch.test.ts" \+ "src/logger.test.ts" \+ "src/core/orchestration/__tests__/toolPresentationPolicy.test.ts" \+ "src/core/orchestration/__tests__/deferrableToolController.test.ts" \+ "src/core/orchestration/__tests__/toolCallBridge.test.ts" \+ "src/core/orchestration/__tests__/toolUseLoop.test.ts" \+ "src/agent/__tests__/taskExecutor.test.ts" \"src/llm/__tests__/codexSubscription.test.ts" \"src/llm/__tests__/codexLlmProxyClient.test.ts" \"src/llm/__tests__/providerAttemptAuthorizer.test.ts" \diff --git a/scripts/tests/test-images-manifest.sh b/scripts/tests/test-images-manifest.shindex dc951d583..9caef90eb 100755--- a/scripts/tests/test-images-manifest.sh+++ b/scripts/tests/test-images-manifest.sh@@ -202,7 +202,7 @@ JSONrm -rf "$d"}-assert_unpublished_images_are_exactly_the_known_two() {+assert_unpublished_images_are_exactly_the_known_fixtures() {local gotgot="$(node -e 'import("'"$REPO_ROOT"'/scripts/release/images-manifest.mjs").then(m =>@@ -212,9 +212,9 @@ assert_unpublished_images_are_exactly_the_known_two() {# otherwise was missing its Accept header). doc-generator-mcp used to be the# third; it has no manifest row at all now (see# assert_doc_generator_mcp_has_been_removed_from_the_image_system).- local want="workflow-custom-sdk-e2e,workflow-plugin-sdk-e2e"+ local want="codex-approved-tools-mcp-e2e,codex-approved-tools-proxy-e2e,workflow-custom-sdk-e2e,workflow-plugin-sdk-e2e"if [ "$got" = "$want" ]; then- pass "the unpublished set is exactly the known two"+ pass "the unpublished set is exactly the known fixtures"elsefail "unpublished set is '$got', expected '$want'"fi@@ -1332,7 +1332,7 @@ assert_every_local_image_maps_to_exactly_one_rowassert_pull_in_ghcr_mode_is_derived_not_storedassert_a_published_image_with_no_source_paths_is_rejectedassert_an_invalid_image_name_is_rejected-assert_unpublished_images_are_exactly_the_known_two+assert_unpublished_images_are_exactly_the_known_fixturesassert_matrix_fields_match_manifestassert_every_matrix_image_has_a_manifest_rowassert_source_paths_match_filtersdiff --git a/tests/e2e/fixtures/codex-subscription/approved-tools-proxy/Dockerfile b/tests/e2e/fixtures/codex-subscription/approved-tools-proxy/Dockerfilenew file mode 100644index 000000000..19864ed66--- /dev/null+++ b/tests/e2e/fixtures/codex-subscription/approved-tools-proxy/Dockerfile@@ -0,0 +1,9 @@+# Build context: repository root. The harness must provide its exact freshly+# built branch-owned production proxy image, which remains unchanged.+ARG CODEX_PROXY_IMAGE+FROM ${CODEX_PROXY_IMAGE}+LABEL ai.evenfire.test-only="approved-tools-minikube"+WORKDIR /app/codex-llm-proxy+COPY codex-llm-proxy/test/approvedToolsUpstream.ts ./test/approvedToolsUpstream.ts+COPY tests/e2e/fixtures/codex-subscription/approved-tools-proxy/entrypoint.mjs ./approved-tools-entrypoint.mjs+CMD ["node", "approved-tools-entrypoint.mjs"]diff --git a/tests/e2e/fixtures/codex-subscription/approved-tools-proxy/entrypoint.mjs b/tests/e2e/fixtures/codex-subscription/approved-tools-proxy/entrypoint.mjsnew file mode 100644index 000000000..ea5d66b39--- /dev/null+++ b/tests/e2e/fixtures/codex-subscription/approved-tools-proxy/entrypoint.mjs@@ -0,0 +1,32 @@+// This file is copied only into the isolated Minikube E2E image.+if (+ process.env.CODEX_APPROVED_TOOLS_TEST_ONLY !== '1' ||+ process.env.NODE_ENV !== 'test' ||+ !/^[a-z0-9][a-z0-9-]+$/.test(process.env.CODEX_APPROVED_TOOLS_MINIKUBE_PROFILE || '') ||+ !process.env.KUBERNETES_SERVICE_HOST+) {+ throw new Error('approved-tools proxy requires explicit isolated Minikube test markers')+}+const { loadConfig } = await import('./dist/config.js')+const { createProxyApps } = await import('./dist/server.js')+const { createApprovedToolsUpstream } = await import('./test/approvedToolsUpstream.ts')+const simulator = createApprovedToolsUpstream()+const config = loadConfig()+// Only the external model boundary is simulated. The gateway, redemption,+// request hash, platform identity, receipts and application remain real.+const servers = createProxyApps(config, {+ fetchFn: simulator.fetchFn,+ lookup: async hostname => {+ if (hostname !== 'chatgpt.com') throw new Error('fixture_dns_operation_denied')+ // No socket connects to this synthetic DNS result. The test fetch seam+ // serves Responses in memory while the unchanged origin policy executes.+ return [{ address: '104.18.32.47', family: 4 }]+ },+})+servers.probeApp.get('/approved-tools/evidence', (_req, res) => res.json(simulator.evidence()))+servers.runtime.listen(config.runtimePort)+servers.admin.listen(config.adminPort)+servers.probe.listen(config.probePort)+for (const signal of ['SIGTERM', 'SIGINT']) {+ process.on(signal, () => void servers.close().then(() => process.exit(0)))+}diff --git a/tests/e2e/fixtures/codex-subscription/approved-tools-setup/README.md b/tests/e2e/fixtures/codex-subscription/approved-tools-setup/README.mdnew file mode 100644index 000000000..03d07beef--- /dev/null+++ b/tests/e2e/fixtures/codex-subscription/approved-tools-setup/README.md@@ -0,0 +1,66 @@+# Owned Minikube setup for Codex approved tools++The setup source for synthetic subscription consent (`seed.mjs`) is currently+blocked by the local content-control hook. It is deliberately absent. The+prepare command checks that file before any cluster mutation; this harness+must not be reported as runnable or certified until the blocked source is+reviewed and installed. No runtime, account or E2E success is implied by the+hermetic manifest tests.++The intended seed uses existing Control API services to create a fresh Desktop+user/team, encrypted synthetic subscription consent, and the real proxy catalog+sync. It must not approve connectors, bind subscriptions to Hosts or manufacture+business results. Those actions belong to the visible browser journey.++## Entry points++All commands use the verified `MINIKUBE_PROFILE`, matching explicit+`CONTROL_API_REAL_PG_CONTEXT` and the canonical branch mutation lease. Use Node24.++1. `make minikube-build-codex-approved-tools-fixtures` builds the current normal+ proxy and two additional unpublished fixture images. Their tags never replace+ production image tags. Run before reconcile because image acquisition updates+ the manifest timestamp.+2. Complete the supported profile reconcile and exact-HEAD validation sequence.+3. `make minikube-run-codex-approved-tools` prepares isolated resources, runs the+ visible Playwright runner and restores the original proxy image/environment.+ It can be the required Playwright command inside T2 after image acquisition.+4. Alternatively, `make minikube-prepare-codex-approved-tools` leaves the fixture+ active for investigation. Use `make minikube-restore-codex-approved-tools` with+ the same evidence directory to restore the recorded proxy and stop only this+ run's owned forwards.++Required environment: `APPROVED_TOOLS_CANONICAL_ROOT`, fresh existing child+directory `APPROVED_TOOLS_EVIDENCE_DIR` under canonical `.local-notes/infra/runs`,+`APPROVED_TOOLS_UPSTREAM_MODE=deterministic`, existing admin login variables+`TEST_ADMIN_USERNAME`/`TEST_ADMIN_PASSWORD`, fresh isolated member+`TEST_USER_EMAIL`/`TEST_USER_PASSWORD`, and the owned Control UI/REST/RPC URLs+required by the Playwright runner. Credentials stay in the process environment+and stdin; never put them in scenario metadata, logs or command arguments.++The normal Minikube overlay already enables the Control API, proxy and Host Codex+feature gates. Preparation checks effective running Control API/proxy flags and+the Host config map, and refuses disabled/stale workloads. Apply the supported+overlay/reconcile if those preconditions fail; preparation does not weaken gates.++The three Contexts start with empty connector lists. Hosts have an unassigned+Codex subscription and no fallback provider. McpServer CRDs are available but+not granted; HCC builds their deployments and network boundaries. Server dry-run+and creation roundtrip check that fields survive the installed CRD schemas.++Additional fixture ports are randomly allocated once per fresh run, persisted in+its metadata, and registered with `port-forward-owner.sh` in the profile's owned+PID directory. A collision fails; no port mapping is regenerated. Readiness probes+revalidate the same live process identity. Existing profile `ports.env` stays+unchanged. Restoration rejects foreign worktrees, HEADs, deployments and images.++Scenario metadata is written to `scenarios.json` and passed directly to the runner+in `run` mode. It contains no credentials. State records retain only fixture+identity, ports, original image/pull policy and the non-sensitive NODE_ENV value.+Created CRDs and synthetic database fixtures remain available for diagnosis;+cleanup must explicitly target the listed run-owned resources and seed rows.++Real subscription tests do not use this synthetic prepare path. They require the+separately approved account/preconditions and production proxy image. Refusing+real mode here prevents accidental account use and prevents presenting a fake+upstream result as real-subscription evidence.diff --git a/tests/e2e/fixtures/codex-subscription/approved-tools/Dockerfile b/tests/e2e/fixtures/codex-subscription/approved-tools/Dockerfilenew file mode 100644index 000000000..c05eba8fc--- /dev/null+++ b/tests/e2e/fixtures/codex-subscription/approved-tools/Dockerfile@@ -0,0 +1,7 @@+FROM node:24-alpine+WORKDIR /app+COPY tests/e2e/fixtures/codex-subscription/approved-tools/server.mjs ./server.mjs+USER node+ENV BIND_ADDRESS=0.0.0.0 PORT=8080+EXPOSE 8080+CMD ["node", "server.mjs"]diff --git a/tests/e2e/fixtures/codex-subscription/approved-tools/README.md b/tests/e2e/fixtures/codex-subscription/approved-tools/README.mdnew file mode 100644index 000000000..329e582cf--- /dev/null+++ b/tests/e2e/fixtures/codex-subscription/approved-tools/README.md@@ -0,0 +1,70 @@+# Approved tools MCP fixture++This development-only Node 24 service exposes a real stateless Streamable HTTP+MCP endpoint at `/mcp`. It does not implement Evenfire discovery, permissions,+the model or the execution bridge. Its HTTP contracts use MCP Host's installed+MCP SDK client. No new dependencies are required.++## Startup and deployment++Run `node --test tests/e2e/fixtures/codex-subscription/approved-tools/server.test.mjs`+from the repository root after installing MCP Host dependencies. HTTP contract+tests use ephemeral loopback ports and require host execution if the sandbox+forbids listening. No subscription is consumed.++Start the service with `node tests/e2e/fixtures/codex-subscription/approved-tools/server.mjs`.+Configure CATALOG_SIZE to exactly 83, 150 or 250, RUN_ID to a unique safe run+identifier, and PORT to the assigned port. The default bind address is loopback.+For the isolated Minikube instance set BIND_ADDRESS to all interfaces. The+Node 24 test container needs only server.mjs mounted read-only.++Use a separate instance per scenario. Deploy through the documented owned+development-profile harness and its mutation lease. Expose a cluster-internal+Service with `/health` readiness and register `/mcp` through the supported+MCPServer/plugin test fixture path. Grant access through visible UI during+the E2E journey. Do not expose this service publicly or relax production+authentication, network or provider policy. No cluster operation is performed+by these files.++There is no reset endpoint. Restarting loses evidence and invalidates the run.+Cleanup only that run's resources after collecting evidence. Require zero+initial calls before starting the visible journey.++## Catalog and evidence++The catalog contains exactly CATALOG_SIZE tools. The last is+`workitem_read_receipt`, with the unique search phrase **verification receipt**.+Other tools read numbered work item categories. All schemas are empty objects+with no additional properties or required arguments.++A successful `tools/call` returns a text block containing JSON fields runId,+tool, callId and businessId. The business identifier is a UUID created inside+the service on the first business call. It is absent from the catalog, prompt+and initial evidence. Subsequent calls read the same identifier and each+invocation is recorded, so duplicate or unrelated operations remain observable.++`GET /health` returns ready, catalogSize and runId. `GET /evidence` requires+the configured runId query parameter and returns runId, catalogSize and calls.+Each call is the exact record returned by the operation. An invalid run ID+returns 400; a different run returns 404. Observation cannot advance the flow.+Discovery and rejected calls do not create records. After UI revocation,+reuse the same instance and verify its call count remains unchanged.++callId is the MCP JSON-RPC request ID, which can differ from the model call ID.+Correlate that boundary using real Evenfire execution evidence. The fixture+does not claim user or agent identity from model arguments.++## Responsibility and false-positive review++| Layer | Proof |+| ---------------------- | ---------------------------------------------------------------------------------------- |+| HTTP contracts | SDK initialization, exact catalog, valid schemas, actual calls, nonce and counters |+| Browser E2E | Visible login, configuration, grant, chat and response matching observed business result |+| Host/upstream evidence | Correct provider, selective definitions, identity and approval correlation |++These protocol tests cannot certify intermediate rendering, button behavior,+UI transitions or protected-route guards. The browser lane must fail on each+of those faults and on an invented answer without an MCP execution. It must+not call `/mcp` itself to manufacture evidence. Evidence reads are observation+only. No internal Evenfire API is mocked or called here to skip its user flow.+Minikube, Playwright and real-subscription validation remain separate lanes.diff --git a/tests/e2e/fixtures/codex-subscription/approved-tools/server.mjs b/tests/e2e/fixtures/codex-subscription/approved-tools/server.mjsnew file mode 100644index 000000000..df6a156fe--- /dev/null+++ b/tests/e2e/fixtures/codex-subscription/approved-tools/server.mjs@@ -0,0 +1,155 @@+import { randomUUID } from 'node:crypto'+import { createServer } from 'node:http'+import { pathToFileURL } from 'node:url'++export const RECEIPT_TOOL = 'workitem_read_receipt'+const SIZES = new Set([83, 150, 250])+const RUN_ID = /^[a-zA-Z0-9_-]{1,80}$/+const MAX_REQUEST_BYTES = 64 * 1024++export function createApprovedToolsFixture({ catalogSize = 83, runId = randomUUID() } = {}) {+ if (!SIZES.has(catalogSize)) throw new Error('catalogSize must be 83, 150, or 250')+ if (typeof runId !== 'string' || !RUN_ID.test(runId)) throw new Error('Invalid runId')+ const tools = Array.from({ length: catalogSize }, (_, index) => ({+ name:+ index === catalogSize - 1 ? RECEIPT_TOOL : `workitem_read_${String(index).padStart(3, '0')}`,+ description:+ index === catalogSize - 1+ ? 'Read the verification receipt for a work item. Returns its persisted business identifier.'+ : `Read work item category ${index}. Returns its persisted business identifier.`,+ inputSchema: {+ type: 'object',+ properties: {},+ additionalProperties: false,+ },+ annotations: {+ readOnlyHint: true,+ destructiveHint: false,+ idempotentHint: true,+ openWorldHint: false,+ },+ }))+ const names = new Set(tools.map(tool => tool.name))+ let businessId+ const calls = []++ const send = (response, status, body) => {+ response.writeHead(status, { 'content-type': 'application/json', 'cache-control': 'no-store' })+ response.end(body === undefined ? undefined : JSON.stringify(body))+ }+ const rpcError = (response, id, code, message) =>+ send(response, 200, {+ jsonrpc: '2.0',+ id,+ error: { code, message },+ })++ const server = createServer(async (request, response) => {+ const url = new URL(request.url, 'http://fixture.local')+ if (request.method === 'GET' && url.pathname === '/health') {+ return send(response, 200, { ready: true, catalogSize, runId })+ }+ if (request.method === 'GET' && url.pathname === '/evidence') {+ const requestedRun = url.searchParams.get('runId')+ if (!RUN_ID.test(requestedRun ?? '')) return send(response, 400, { error: 'Invalid runId' })+ if (requestedRun !== runId) return send(response, 404, { error: 'Unknown run' })+ return send(response, 200, { runId, catalogSize, calls })+ }+ if (url.pathname !== '/mcp') return send(response, 404, { error: 'Not found' })+ // This fixture implements stateless JSON responses, not an SSE subscription.+ if (request.method !== 'POST') return send(response, 405, { error: 'POST required' })+ if (+ !request.headers.accept?.includes('application/json') ||+ !request.headers.accept?.includes('text/event-stream')+ ) {+ return send(response, 406, {+ error: 'Accept must include application/json and text/event-stream',+ })+ }+ if (!request.headers['content-type']?.startsWith('application/json')) {+ return send(response, 415, { error: 'application/json required' })+ }+ let bytes = 0+ const chunks = []+ try {+ for await (const chunk of request) {+ bytes += chunk.length+ if (bytes > MAX_REQUEST_BYTES) return send(response, 413, { error: 'Request too large' })+ chunks.push(chunk)+ }+ const message = JSON.parse(Buffer.concat(chunks).toString('utf8'))+ if (+ !message ||+ Array.isArray(message) ||+ message.jsonrpc !== '2.0' ||+ typeof message.method !== 'string'+ ) {+ return rpcError(response, null, -32600, 'Invalid Request')+ }+ const { id, method, params } = message+ if (id === undefined) {+ if (method === 'notifications/initialized' || method === 'notifications/cancelled')+ return send(response, 202)+ return send(response, 400, { error: 'Unsupported notification' })+ }+ let result+ if (method === 'initialize') {+ if (typeof params?.protocolVersion !== 'string')+ return rpcError(response, id, -32602, 'protocolVersion required')+ result = {+ protocolVersion: '2025-03-26',+ capabilities: { tools: { listChanged: false } },+ serverInfo: { name: 'evenfire-approved-tools-fixture', version: '1.0.0' },+ }+ } else if (method === 'ping') {+ result = {}+ } else if (method === 'tools/list') {+ result = { tools }+ } else if (method === 'tools/call') {+ const args = params?.arguments === undefined ? {} : params.arguments+ if (+ !names.has(params?.name) ||+ !args ||+ typeof args !== 'object' ||+ Array.isArray(args) ||+ Object.keys(args).length !== 0+ ) {+ result = {+ isError: true,+ content: [{ type: 'text', text: 'Unknown tool or invalid arguments' }],+ }+ } else {+ // Generated inside this service only when a business call arrives.+ businessId ??= randomUUID()+ const record = { runId, tool: params.name, callId: id, businessId }+ calls.push(record)+ result = { content: [{ type: 'text', text: JSON.stringify(record) }], isError: false }+ }+ } else {+ return rpcError(response, id, -32601, 'Method not found')+ }+ send(response, 200, { jsonrpc: '2.0', id, result })+ } catch {+ if (!response.headersSent) rpcError(response, null, -32700, 'Parse error')+ }+ })+ server.requestTimeout = 10_000+ server.headersTimeout = 10_000+ return server+}++if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {+ const port = Number(process.env.PORT ?? 8080)+ if (!Number.isInteger(port) || port < 1 || port > 65535) throw new Error('PORT must be 1..65535')+ const server = createApprovedToolsFixture({+ catalogSize: Number(process.env.CATALOG_SIZE ?? 83),+ runId: process.env.RUN_ID,+ })+ server.listen(port, process.env.BIND_ADDRESS ?? '127.0.0.1')+ const shutdown = () => {+ server.close()+ server.closeAllConnections()+ }+ process.once('SIGTERM', shutdown)+ process.once('SIGINT', shutdown)+}diff --git a/tests/e2e/fixtures/codex-subscription/approved-tools/server.test.mjs b/tests/e2e/fixtures/codex-subscription/approved-tools/server.test.mjsnew file mode 100644index 000000000..8641bd354--- /dev/null+++ b/tests/e2e/fixtures/codex-subscription/approved-tools/server.test.mjs@@ -0,0 +1,108 @@+import assert from 'node:assert/strict'+import { once } from 'node:events'+import { createRequire } from 'node:module'+import { test } from 'node:test'+import { RECEIPT_TOOL, createApprovedToolsFixture } from './server.mjs'++// Exercise interoperability with the same installed SDK as MCP Host.+const require = createRequire(new URL('../../../../../mcp-host/package.json', import.meta.url))+const { Client } = require('@modelcontextprotocol/sdk/client/index.js')+const {+ StreamableHTTPClientTransport,+} = require('@modelcontextprotocol/sdk/client/streamableHttp.js')++async function start(t, catalogSize) {+ const runId = `contract-${catalogSize}`+ const server = createApprovedToolsFixture({ catalogSize, runId })+ server.listen(0, '127.0.0.1')+ await once(server, 'listening')+ t.after(async () => {+ const closed = once(server, 'close')+ server.close()+ server.closeAllConnections()+ await closed+ })+ const base = `http://127.0.0.1:${server.address().port}`+ return { base, runId }+}++for (const count of [83, 150, 250]) {+ test(`real HTTP MCP discovers exactly ${count} and executes the final receipt tool`, async t => {+ const { base, runId } = await start(t, count)+ const evidence = () =>+ fetch(`${base}/evidence?runId=${runId}`).then(response => response.json())+ assert.deepEqual(await evidence(), { runId, catalogSize: count, calls: [] })+ const client = new Client({ name: 'fixture-contract', version: '1.0.0' })+ const transport = new StreamableHTTPClientTransport(new URL(`${base}/mcp`))+ t.after(() => client.close())+ await client.connect(transport)+ assert.equal(client.getServerVersion().name, 'evenfire-approved-tools-fixture')+ const catalog = await client.listTools()+ assert.equal(catalog.tools.length, count)+ assert.equal(new Set(catalog.tools.map(tool => tool.name)).size, count)+ assert.equal(catalog.tools.at(-1).name, RECEIPT_TOOL)+ assert.equal(catalog.tools.at(-1).inputSchema.additionalProperties, false)+ assert.deepEqual((await evidence()).calls, [], 'Discovery must not create business records')+ assert.equal(+ JSON.stringify(catalog).includes('businessId'),+ false,+ 'Nonce must not be advertised'+ )+ const result = await client.callTool({ name: RECEIPT_TOOL, arguments: {} })+ assert.equal(result.isError, false)+ const receipt = JSON.parse(result.content[0].text)+ assert.equal(receipt.tool, RECEIPT_TOOL)+ assert.equal(receipt.runId, runId)+ assert.match(receipt.businessId, /^[0-9a-f-]{36}$/)+ assert.deepEqual((await evidence()).calls, [receipt])+ const repeated = await client.callTool({ name: RECEIPT_TOOL, arguments: {} })+ assert.equal(JSON.parse(repeated.content[0].text).businessId, receipt.businessId)+ assert.equal(+ (await evidence()).calls.length,+ 2,+ 'Counters detect duplicate business executions'+ )+ const unknown = await client.callTool({ name: 'unavailable_tool', arguments: {} })+ assert.equal(unknown.isError, true)+ const invalid = await client.callTool({+ name: RECEIPT_TOOL,+ arguments: { runId: 'fabricated' },+ })+ assert.equal(invalid.isError, true)+ assert.equal((await evidence()).calls.length, 2, 'Rejected calls cannot create evidence')+ assert.equal((await fetch(`${base}/evidence?runId=another-run`)).status, 404)+ })+}++test('HTTP protocol errors and observation routes cannot execute business operations', async t => {+ const { base, runId } = await start(t, 83)+ const rpc = body =>+ fetch(`${base}/mcp`, {+ method: 'POST',+ headers: {+ accept: 'application/json, text/event-stream',+ 'content-type': 'application/json',+ },+ body,+ })+ assert.equal((await fetch(`${base}/mcp`)).status, 405)+ assert.equal((await fetch(`${base}/mcp`, { method: 'POST' })).status, 406)+ assert.equal((await rpc('{')).status, 200)+ assert.equal((await (await rpc('{')).json()).error.code, -32700)+ assert.equal((await rpc('x'.repeat(65537))).status, 413)+ const unknown = await (+ await rpc(JSON.stringify({ jsonrpc: '2.0', id: 1, method: 'unknown' }))+ ).json()+ assert.equal(unknown.error.code, -32601)+ assert.deepEqual((await (await fetch(`${base}/evidence?runId=${runId}`)).json()).calls, [])+ assert.equal((await fetch(`${base}/evidence?runId=`)).status, 400)+ const health = await (await fetch(`${base}/health`)).json()+ assert.deepEqual(health, { ready: true, catalogSize: 83, runId })+})++test('invalid fixture configuration fails before listening', () => {+ for (const catalogSize of [0, 32, 84, 151, 251, NaN, '83']) {+ assert.throws(() => createApprovedToolsFixture({ catalogSize }), /catalogSize/)+ }+ assert.throws(() => createApprovedToolsFixture({ runId: '../foreign' }), /runId/)+})diff --git a/tests/e2e/fixtures/codex-subscription/sanitized-upstream-contract.json b/tests/e2e/fixtures/codex-subscription/sanitized-upstream-contract.jsonindex aa1c71a23..c91e01896 100644--- a/tests/e2e/fixtures/codex-subscription/sanitized-upstream-contract.json+++ b/tests/e2e/fixtures/codex-subscription/sanitized-upstream-contract.json@@ -42,7 +42,7 @@"limits": {"maxRequestBodyBytes": 1048576,"maxMessages": 128,- "maxTools": 32,+ "maxToolCalls": 32,"maxOutputTokens": 16384,"maxStreamDurationMs": 300000,"maxDeadlineMs": 300000,diff --git a/tests/e2e/fixtures/package-lock.json b/tests/e2e/fixtures/package-lock.jsonindex 40bfad0b9..07b427e25 100644--- a/tests/e2e/fixtures/package-lock.json+++ b/tests/e2e/fixtures/package-lock.json@@ -1,12 +1,12 @@{"name": "@clerum/e2e-test-fixtures",- "version": "1.0.71",+ "version": "1.0.72","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "@clerum/e2e-test-fixtures",- "version": "1.0.71",+ "version": "1.0.72","dependencies": {"@kubernetes/client-node": "^1.0.0"},diff --git a/tests/e2e/fixtures/package.json b/tests/e2e/fixtures/package.jsonindex e471108f1..cb4361eaa 100644--- a/tests/e2e/fixtures/package.json+++ b/tests/e2e/fixtures/package.json@@ -1,6 +1,6 @@{"name": "@clerum/e2e-test-fixtures",- "version": "1.0.71",+ "version": "1.0.72","description": "E2E test fixtures and utilities for Clerum testing","type": "module","main": "index.ts",diff --git a/tests/e2e/integration/codex-subscription-contract-freeze.test.ts b/tests/e2e/integration/codex-subscription-contract-freeze.test.tsindex 09c2658bf..7fb0df179 100644--- a/tests/e2e/integration/codex-subscription-contract-freeze.test.ts+++ b/tests/e2e/integration/codex-subscription-contract-freeze.test.ts@@ -38,7 +38,7 @@ const REQUIRED_OPERATIONS = [const REQUIRED_LIMIT_KEYS = ['maxRequestBodyBytes','maxMessages',- 'maxTools',+ 'maxToolCalls','maxOutputTokens','maxStreamDurationMs','maxDeadlineMs',@@ -153,6 +153,10 @@ describe('codex-subscription contract freeze', () => {assertFinitePositiveLimit(limits?.[key], `limits.${key}`)}+ expect(limits).not.toHaveProperty('maxTools')+ expect(limits?.maxToolCalls).toBe(32)+ expect(limits?.maxRequestBodyBytes).toBe(1048576)+const errors = contract.errorTaxonomyexpect(Array.isArray(errors) && (errors as unknown[]).length > 0).toBe(true)for (const code of errors as unknown[]) {diff --git a/tests/e2e/package-lock.json b/tests/e2e/package-lock.jsonindex ab0892116..a50f0aec9 100644--- a/tests/e2e/package-lock.json+++ b/tests/e2e/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-e2e-tests",- "version": "0.1.79",+ "version": "0.1.80","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-e2e-tests",- "version": "0.1.79",+ "version": "0.1.80","devDependencies": {"@types/jsonwebtoken": "^9.0.10","@types/node": "^22.0.0",diff --git a/tests/e2e/package.json b/tests/e2e/package.jsonindex c59047da2..5d00434de 100644--- a/tests/e2e/package.json+++ b/tests/e2e/package.json@@ -1,6 +1,6 @@{"name": "clerum-e2e-tests",- "version": "0.1.79",+ "version": "0.1.80","private": true,"type": "module","scripts": {diff --git a/tests/e2e/playwright/desktop/codex-subscription-approved-tools.spec.ts b/tests/e2e/playwright/desktop/codex-subscription-approved-tools.spec.tsnew file mode 100644index 000000000..be90e6b09--- /dev/null+++ b/tests/e2e/playwright/desktop/codex-subscription-approved-tools.spec.ts@@ -0,0 +1,288 @@+/**+ * Contract: prepared isolated user/agent/subscription and unapproved fixture+ * connector -> visible Control UI login/configuration/approval -> isolated+ * Desktop login/chat -> persisted MCP receipt -> visible revocation -> denial.+ * Every transition checks UI/state and the saved binding or actual MCP result.+ * E2E_GUARDIAN_ENTRY_POINT: Control UI starts at its application root.+ * E2E_GUARDIAN_IPC_FLOW: Desktop uses real main-process login and chat IPC.+ * Completed messages and MCP evidence replace renderer response waits. Tests+ * never inject IPC calls or authentication. Only the external model is faked.+ */+import { type Page, expect, test } from '@playwright/test'+import {+ type Scenario,+ localUrl,+ readEvidence,+ readUpstreamEvidence,+ required,+ scenarios,+} from '../helpers/approved-tools-scenarios'+import { launchDesktopApp } from '../helpers/launch-desktop'+import { loginControlUiVisible } from '../helpers/visible-login'+import { AgentListPage, AgentModelPage, ControlUiShell } from '../pages/codex-subscription'++const cases = scenarios()+const mode = required('APPROVED_TOOLS_UPSTREAM_MODE')+// Read-only comparison across serial scenarios with identical native capabilities.+let advertisedBaseline: { definitionCount: number; definitionBytes: number } | undefined+if (!['deterministic', 'real'].includes(mode))+ throw new Error('APPROVED_TOOLS_UPSTREAM_MODE must be deterministic or real')+if (mode === 'real' && process.env.CODEX_REAL_UPSTREAM_CONFIRM !== '1')+ throw new Error('Real subscription requires explicit CODEX_REAL_UPSTREAM_CONFIRM=1')+for (const key of [+ 'TEST_ADMIN_USERNAME',+ 'TEST_ADMIN_PASSWORD',+ 'TEST_USER_EMAIL',+ 'TEST_USER_PASSWORD',+])+ required(key)+for (const key of ['EXTERNAL_REST_API_BASE_URL', 'RPC_PROXY_BASE_URL']) localUrl(required(key))+if (process.env.PLAYWRIGHT_DESKTOP_BUILT !== 'true')+ throw new Error('Verified Desktop build is required; this lane never skips')++async function saveConnector(page: Page, scenario: Scenario, remove: boolean) {+ const response = page.waitForResponse(+ response =>+ new URL(response.url()).pathname === `/api/v1/admin/contexts/${scenario.contextName}` &&+ response.request().method() === 'PUT'+ )+ if (remove) {+ await page+ .getByRole('alertdialog', { name: 'Remove connector from this agent?' })+ .getByRole('button', { name: 'Remove connector', exact: true })+ .click()+ } else {+ await page+ .getByRole('dialog', { name: 'Add connectors' })+ .getByRole('button', { name: 'Add connector', exact: true })+ .click()+ }+ const saved = await response+ expect(saved.ok()).toBe(true)+ const body = await saved.json()+ expect(body.metadata.name).toBe(scenario.contextName)+ expect(body.spec.mcpServers.includes(scenario.connectorName)).toBe(!remove)+ await expect(page.getByText('Connectors updated.', { exact: true })).toBeVisible()+ const row = page+ .getByRole('row')+ .filter({ has: page.getByText(scenario.connectorName, { exact: true }) })+ if (remove) await expect(row).toHaveCount(0)+ else await expect(row).toBeVisible()+}++async function send(page: Page, prompt: string) {+ await expect(page.getByTestId('chat-input')).toBeVisible()+ await page.getByTestId('chat-input').fill(prompt)+ await expect(page.getByTestId('send-button')).toBeEnabled()+ await page.getByTestId('send-button').click()+ await expect(page.getByTestId('message-list').getByText(prompt, { exact: true })).toBeVisible()+}++test('unauthenticated agent route guard prevents connector use', async ({ page }) => {+ const scenario = cases[0]!+ const before = await readEvidence(scenario)+ // Negative protected-route guard: direct navigation must not open the editor.+ await page.goto(`/agents/${scenario.agentName}/connectors`)+ await expect(page.getByLabel('Username or email')).toBeVisible()+ await expect(page.getByRole('button', { name: 'Sign in', exact: true })).toBeVisible()+ await expect(page.getByRole('button', { name: 'Add connector', exact: true })).toHaveCount(0)+ await expect(page.getByLabel('Main navigation')).toHaveCount(0)+ expect((await readEvidence(scenario)).calls).toEqual(before.calls)+})++for (const scenario of cases) {+ test(`approved tools ${scenario.catalogSize}: selective receipt and revoked connector`, async ({+ page,+ }, testInfo) => {+ testInfo.annotations.push({ type: 'upstream', description: mode })+ await test.step('Sign in at Control UI root and select the isolated agent', async () => {+ expect((await readEvidence(scenario)).calls).toEqual([])+ await page.goto('/')+ await loginControlUiVisible(page)+ await new ControlUiShell(page).openAgents()+ await new AgentListPage(page).openNamed(scenario.agentName)+ await expect(page.getByText(`Agent: ${scenario.agentName}`, { exact: true })).toBeVisible()+ })+ await test.step('Select the prepared subscription and persist its model binding', async () => {+ const model = new AgentModelPage(page)+ await model.openEditor()+ await model.chooseSubscription(scenario.subscriptionName)+ await page.getByLabel('Current model', { exact: true }).click()+ await page.getByRole('option', { name: scenario.modelName, exact: true }).click()+ const host = await model.saveHost(scenario.agentName)+ const savedPolicy = (host.spec as { llmPolicy?: { fallbacks: unknown[] } } | undefined)+ ?.llmPolicy+ expect(+ savedPolicy?.fallbacks ?? [],+ 'isolated agent must not fall back to another provider'+ ).toEqual([])+ expect(host.spec?.model).toMatchObject({+ provider: 'codex-subscription',+ name: scenario.modelName,+ connectionRef: scenario.connectionKey,+ })+ await expect(page.getByRole('button', { name: 'Edit', exact: true })).toBeVisible()+ await expect(model.credentialSelect()).toBeDisabled()+ })+ await test.step('Approve the fixture connector through its picker', async () => {+ await page.getByRole('tab', { name: 'Connectors', exact: true }).click()+ await expect(page).toHaveURL(new RegExp(`/agents/${scenario.agentName}/connectors$`))+ await expect(page.getByText('No connectors attached yet.', { exact: true })).toBeVisible()+ await page.getByRole('button', { name: 'Add connector', exact: true }).click()+ const dialog = page.getByRole('dialog', { name: 'Add connectors' })+ await expect(dialog).toBeVisible()+ await dialog.getByRole('option', { name: scenario.connectorName, exact: true }).click()+ await saveConnector(page, scenario, false)+ })+ const app = await launchDesktopApp()+ try {+ const desktop = await app.firstWindow()+ await test.step('Sign in visibly to Desktop and start a chat with the same agent', async () => {+ await expect(desktop.getByLabel('Email', { exact: true })).toBeVisible()+ await desktop.getByLabel('Email', { exact: true }).fill(required('TEST_USER_EMAIL'))+ await desktop.getByLabel('Password', { exact: true }).fill(required('TEST_USER_PASSWORD'))+ await desktop.getByRole('button', { name: 'Sign in', exact: true }).click()+ await expect(desktop.getByTestId('nav-chat')).toBeVisible()+ await desktop.getByTestId('nav-chat').click()+ await desktop.getByTestId('nav-new-chat').click()+ await expect(+ desktop.getByRole('heading', { name: 'New chat with', exact: true })+ ).toBeVisible()+ await desktop.getByRole('button', { name: 'Switch chat agent', exact: true }).click()+ await desktop+ .getByRole('menuitem', { name: scenario.agentDisplayName, exact: true })+ .click()+ await expect(+ desktop.getByRole('button', { name: 'Switch chat agent', exact: true })+ ).toContainText(scenario.agentDisplayName)+ await desktop.getByRole('button', { name: 'Model — Select model', exact: true }).click()+ const modelOption = desktop+ .getByRole('menuitemradio')+ .filter({ hasText: scenario.modelLabel })+ await expect(modelOption).toHaveCount(1)+ await modelOption.click()+ await expect(+ desktop.getByRole('button', { name: `Model — ${scenario.modelLabel}`, exact: true })+ ).toBeVisible()+ await expect(desktop.getByTestId('agent-response')).toHaveCount(0)+ })+ await test.step('Find only the verification receipt tool and return its real business ID', async () => {+ const upstreamBefore =+ mode === 'deterministic' ? await readUpstreamEvidence(scenario) : undefined+ const started = Date.now()+ await send(+ desktop,+ 'Find the verification receipt tool in my approved connectors. Describe only that tool, call it once, and report its exact businessId. Do not call other business tools or invent a receipt.'+ )+ await expect+ .poll(async () => (await readEvidence(scenario)).calls.length, { timeout: 120_000 })+ .toBe(1)+ const evidence = await readEvidence(scenario)+ const call = evidence.calls[0]!+ expect(call).toMatchObject({ runId: scenario.runId, tool: 'workitem_read_receipt' })+ expect(call.businessId).toMatch(/^[0-9a-f-]{36}$/)+ await expect(+ desktop.getByTestId('agent-response').filter({ hasText: call.businessId })+ ).toBeVisible({ timeout: 120_000 })+ await expect(desktop.getByTestId('send-button')).toHaveAttribute(+ 'aria-label',+ 'Send message'+ )+ expect((await readEvidence(scenario)).calls).toEqual(evidence.calls)+ if (upstreamBefore) {+ const upstreamAfter = await readUpstreamEvidence(scenario)+ expect(upstreamAfter.rejected).toBe(upstreamBefore.rejected)+ expect(upstreamAfter.searchCalls - upstreamBefore.searchCalls).toBe(1)+ expect(upstreamAfter.describeCalls - upstreamBefore.describeCalls).toBe(1)+ expect(upstreamAfter.businessCalls - upstreamBefore.businessCalls).toBe(1)+ expect(upstreamAfter.finalResponses - upstreamBefore.finalResponses).toBe(1)+ const requests = upstreamAfter.requests.slice(upstreamBefore.requests.length)+ expect(requests.map(request => request.stage)).toEqual([+ 'clerum__tool_search',+ 'clerum__tool_describe',+ 'clerum__tool_call',+ 'final',+ ])+ for (const request of requests) {+ expect(request.connectorDefinitionCount).toBe(0)+ expect(request.leakedSchema).toBe(false)+ expect(request.inputBytes).toBeGreaterThan(0)+ expect(request.definitionCount).toBeGreaterThanOrEqual(3)+ expect(request.definitionBytes).toBeGreaterThan(0)+ const advertised = {+ definitionCount: request.definitionCount,+ definitionBytes: request.definitionBytes,+ }+ advertisedBaseline ??= advertised+ expect(advertised, 'announced definitions must not grow with catalogue size').toEqual(+ advertisedBaseline+ )+ }+ await testInfo.attach('upstream-request-measurements', {+ body: JSON.stringify({+ upstream: mode,+ catalogSize: scenario.catalogSize,+ requests,+ elapsedMs: Date.now() - started,+ inputTokens: null,+ outputTokens: null,+ subscriptionUsage: null,+ }),+ contentType: 'application/json',+ })+ }+ await testInfo.attach('mcp-receipt-evidence', {+ body: JSON.stringify(evidence),+ contentType: 'application/json',+ })+ })+ await test.step('Revoke through Control UI and deny the next call in the existing chat', async () => {+ const before = await readEvidence(scenario)+ const upstreamBefore =+ mode === 'deterministic' ? await readUpstreamEvidence(scenario) : undefined+ await page+ .getByRole('button', {+ name: `Actions for connector ${scenario.connectorName}`,+ exact: true,+ })+ .click()+ await page.getByRole('menuitem', { name: 'Remove connector', exact: true }).click()+ await expect(+ page.getByRole('alertdialog', { name: 'Remove connector from this agent?' })+ ).toBeVisible()+ await saveConnector(page, scenario, true)+ await send(+ desktop,+ 'Try reading the verification receipt again using the same tool. If access has been revoked or the tool is unavailable, reply exactly CONNECTOR_UNAVAILABLE. Never reuse the earlier receipt as a new result.'+ )+ const denied = mode === 'real' ? 'CONNECTOR_UNAVAILABLE' : '"found":0'+ await expect(desktop.getByTestId('agent-response').filter({ hasText: denied })).toBeVisible(+ { timeout: 120_000 }+ )+ await expect(desktop.getByTestId('send-button')).toHaveAttribute(+ 'aria-label',+ 'Send message'+ )+ expect((await readEvidence(scenario)).calls).toEqual(before.calls)+ if (upstreamBefore) {+ const upstreamAfter = await readUpstreamEvidence(scenario)+ expect(upstreamAfter.rejected).toBe(upstreamBefore.rejected)+ expect(upstreamAfter.businessCalls).toBe(upstreamBefore.businessCalls)+ expect(upstreamAfter.deniedResponses - upstreamBefore.deniedResponses).toBe(1)+ const requests = upstreamAfter.requests.slice(upstreamBefore.requests.length)+ expect(requests.map(request => request.stage)).toEqual(['clerum__tool_search', 'denied'])+ for (const request of requests) {+ expect(request.connectorDefinitionCount).toBe(0)+ expect(request.leakedSchema).toBe(false)+ }+ await testInfo.attach('upstream-revocation-evidence', {+ body: JSON.stringify({ upstream: mode, requests }),+ contentType: 'application/json',+ })+ }+ })+ } finally {+ await app.close()+ }+ })+}diff --git a/tests/e2e/playwright/helpers/approved-tools-scenarios.ts b/tests/e2e/playwright/helpers/approved-tools-scenarios.tsnew file mode 100644index 000000000..86b3a5cdb--- /dev/null+++ b/tests/e2e/playwright/helpers/approved-tools-scenarios.ts@@ -0,0 +1,133 @@+// E2E_GUARDIAN_IPC_FLOW: configuration and read-only fixture evidence only.+export const catalogSizes = [83, 150, 250] as const+export type Scenario = {+ catalogSize: number+ runId: string+ agentName: string+ agentDisplayName: string+ connectorName: string+ contextName: string+ subscriptionName: string+ connectionKey: string+ modelName: string+ modelLabel: string+ fixtureUrl: string+ upstreamEvidenceUrl: string+}+export type Evidence = {+ runId: string+ catalogSize: number+ calls: Array<{ runId: string; tool: string; callId: string | number; businessId: string }>+}+export type UpstreamEvidence = {+ rejected: number+ searchCalls: number+ describeCalls: number+ businessCalls: number+ finalResponses: number+ deniedResponses: number+ requests: Array<{+ definitionCount: number+ definitionBytes: number+ inputBytes: number+ connectorDefinitionCount: number+ leakedSchema: boolean+ stage: string+ }>+}+export function required(name: string): string {+ const value = process.env[name]?.trim()+ if (!value) throw new Error(`Missing required isolated-profile precondition: ${name}`)+ return value+}+export function localUrl(value: string): string {+ const url = new URL(value)+ if (+ !['http:', 'https:'].includes(url.protocol) ||+ !['localhost', '127.0.0.1', '[::1]'].includes(url.hostname) ||+ !url.port ||+ url.username ||+ url.password ||+ url.search ||+ url.hash ||+ url.pathname !== '/'+ ) {+ throw new Error('Expected an explicit profile-owned loopback origin with port')+ }+ return url.origin+}+export function scenarios(): Scenario[] {+ const parsed: unknown = JSON.parse(required('APPROVED_TOOLS_SCENARIOS'))+ if (!Array.isArray(parsed) || parsed.length !== 3)+ throw new Error('Exactly three approved-tools scenarios required')+ const fields = [+ 'runId',+ 'agentName',+ 'agentDisplayName',+ 'connectorName',+ 'contextName',+ 'subscriptionName',+ 'connectionKey',+ 'modelName',+ 'modelLabel',+ 'fixtureUrl',+ ] as const+ const rows = parsed as Scenario[]+ for (const row of rows) {+ for (const key of fields)+ if (typeof row[key] !== 'string' || !row[key].trim())+ throw new Error(`Scenario missing ${key}`)+ for (const key of ['runId', 'agentName', 'connectorName', 'contextName'] as const) {+ if (!/^[a-zA-Z0-9_-]{1,80}$/.test(row[key])) throw new Error(`Invalid scenario ${key}`)+ }+ localUrl(row.fixtureUrl)+ if (process.env.APPROVED_TOOLS_UPSTREAM_MODE === 'deterministic')+ localUrl(row.upstreamEvidenceUrl)+ }+ if (catalogSizes.some(size => rows.filter(row => row.catalogSize === size).length !== 1))+ throw new Error('Scenarios must cover 83, 150, 250 exactly once')+ for (const key of ['runId', 'agentName', 'contextName', 'connectorName', 'fixtureUrl'] as const) {+ if (new Set(rows.map(row => row[key])).size !== 3)+ throw new Error(`Scenarios must isolate ${key}`)+ }+ return rows.sort((a, b) => a.catalogSize - b.catalogSize)+}+export async function readEvidence(scenario: Scenario): Promise<Evidence> {+ // Observes the external test MCP service; never creates business state.+ const url = new URL('/evidence', localUrl(scenario.fixtureUrl))+ url.searchParams.set('runId', scenario.runId)+ const response = await fetch(url, { signal: AbortSignal.timeout(10_000) })+ if (!response.ok) throw new Error(`Fixture evidence returned ${response.status}`)+ const evidence = (await response.json()) as Evidence+ if (+ evidence.runId !== scenario.runId ||+ evidence.catalogSize !== scenario.catalogSize ||+ !Array.isArray(evidence.calls)+ )+ throw new Error('Evidence scenario mismatch')+ return evidence+}++export async function readUpstreamEvidence(scenario: Scenario): Promise<UpstreamEvidence> {+ const response = await fetch(+ new URL('/approved-tools/evidence', localUrl(scenario.upstreamEvidenceUrl)),+ {+ signal: AbortSignal.timeout(10_000),+ }+ )+ if (!response.ok) throw new Error(`Isolated proxy evidence returned ${response.status}`)+ const evidence = (await response.json()) as UpstreamEvidence+ for (const field of [+ 'rejected',+ 'searchCalls',+ 'describeCalls',+ 'businessCalls',+ 'finalResponses',+ 'deniedResponses',+ ] as const) {+ if (!Number.isSafeInteger(evidence[field]) || evidence[field] < 0)+ throw new Error(`Missing upstream evidence ${field}`)+ }+ if (!Array.isArray(evidence.requests)) throw new Error('Missing upstream request evidence')+ return evidence+}diff --git a/tests/e2e/playwright/package-lock.json b/tests/e2e/playwright/package-lock.jsonindex 85da81f65..9ea776ee0 100644--- a/tests/e2e/playwright/package-lock.json+++ b/tests/e2e/playwright/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-playwright-tests",- "version": "0.1.40",+ "version": "0.1.41","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-playwright-tests",- "version": "0.1.40",+ "version": "0.1.41","devDependencies": {"@playwright/test": "^1.50.0","@types/node": "^22.0.0",diff --git a/tests/e2e/playwright/package.json b/tests/e2e/playwright/package.jsonindex 33a874226..8ca8667f0 100644--- a/tests/e2e/playwright/package.json+++ b/tests/e2e/playwright/package.json@@ -1,6 +1,6 @@{"name": "clerum-playwright-tests",- "version": "0.1.40",+ "version": "0.1.41","private": true,"description": "Playwright E2E tests for Control UI and Desktop App","scripts": {diff --git a/tests/e2e/playwright/playwright.codex-approved-tools.config.ts b/tests/e2e/playwright/playwright.codex-approved-tools.config.tsnew file mode 100644index 000000000..68f7e4330--- /dev/null+++ b/tests/e2e/playwright/playwright.codex-approved-tools.config.ts@@ -0,0 +1,22 @@+import { defineConfig, devices } from '@playwright/test'+import { localUrl, required } from './helpers/approved-tools-scenarios'++export default defineConfig({+ testDir: './desktop',+ testMatch: 'codex-subscription-approved-tools.spec.ts',+ fullyParallel: false,+ workers: 1,+ retries: 0,+ timeout: 300_000,+ expect: { timeout: 30_000 },+ forbidOnly: true,+ reporter: [['list'], ['json', { outputFile: required('APPROVED_TOOLS_REPORT') }]],+ outputDir: required('APPROVED_TOOLS_ARTIFACTS'),+ use: {+ ...devices['Desktop Chrome'],+ baseURL: localUrl(required('CONTROL_UI_URL')),+ screenshot: 'only-on-failure',+ video: 'retain-on-failure',+ trace: 'retain-on-failure',+ },+})diff --git a/tests/e2e/playwright/playwright.codex-subscription.config.ts b/tests/e2e/playwright/playwright.codex-subscription.config.tsindex f56a4b17e..38f3daa5c 100644--- a/tests/e2e/playwright/playwright.codex-subscription.config.ts+++ b/tests/e2e/playwright/playwright.codex-subscription.config.ts@@ -7,6 +7,8 @@ const CONTROL_UI_URL =export default defineConfig({testDir: '.',+ // This mandatory isolated-fixture journey runs through its fail-loud dedicated runner.+ testIgnore: ['**/codex-subscription-approved-tools.spec.ts'],globalSetup: path.join(__dirname, 'global-setup.ts'),timeout: 45_000,expect: { timeout: 15_000 },diff --git a/tests/e2e/playwright/playwright.config.ts b/tests/e2e/playwright/playwright.config.tsindex 27591198e..3a9495e0e 100644--- a/tests/e2e/playwright/playwright.config.ts+++ b/tests/e2e/playwright/playwright.config.ts@@ -5,6 +5,8 @@ const CONTROL_UI_URL = process.env.CONTROL_UI_URL ?? 'http://127.0.0.1:3000'export default defineConfig({testDir: '.',+ // This mandatory isolated-fixture journey runs through its fail-loud dedicated runner.+ testIgnore: ['**/codex-subscription-approved-tools.spec.ts'],timeout: 30_000,expect: { timeout: 10_000 },retries: process.env.CI ? 2 : 0,