Skip to content

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

TypeScript

test(codex): cover workflow receipts and discovery continuity

Alfredo Lopez1f1dbc4c9d44
diff --git a/codex-llm-proxy/test/approvedToolsUpstream.test.ts b/codex-llm-proxy/test/approvedToolsUpstream.test.ts
index a7e294f2b..44e6b0612 100644
--- a/codex-llm-proxy/test/approvedToolsUpstream.test.ts
+++ b/codex-llm-proxy/test/approvedToolsUpstream.test.ts
@@ -135,10 +135,13 @@ describe('approved-tools isolated upstream boundary', () => {
expect(simulator.evidence().finalResponses).toBe(2)
})
- it('rediscovers after a new user turn and returns the actual empty catalog after revocation', async () => {
+ it('recovers after an interrupted user task 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 before cancellation' })
+ const canceled = await request(simulator, input)
+ input.push(canceled.event.item) // Genuine cancellation leaves no execution result.
input.push({ role: 'user', content: 'verification receipt again' })
const search = await request(simulator, input)
expect(search.event.item.name).toBe(bridges[0])
@@ -151,12 +154,142 @@ describe('approved-tools isolated upstream boundary', () => {
const denied = await request(simulator, input)
expect(JSON.parse(denied.event.delta)).toEqual(empty)
expect(simulator.evidence()).toMatchObject({
- businessCalls: 1,
+ businessCalls: 2,
finalResponses: 1,
deniedResponses: 1,
})
})
+ it('reuses an earlier real schema for an ordinary repeated request', async () => {
+ const simulator = createApprovedToolsUpstream()
+ const input: Entry[] = [{ role: 'user', content: 'Show my verification receipt.' }]
+ const receipt = await complete(simulator, input, 'reuse')
+ const before = simulator.evidence()
+ input.push({ role: 'user', content: 'Show my verification receipt again.' })
+ const call = await request(simulator, input)
+ expect(call.event.item.name).toBe(bridges[2])
+ // Persisted JSON may reorder object keys without changing the call.
+ call.event.item.arguments = JSON.stringify({ arguments: {}, name: target })
+ input.push(call.event.item, {
+ type: 'function_call_output',
+ call_id: call.event.item.call_id,
+ output: `<tool_output name="${target}" sanitized="false">\n${JSON.stringify(receipt)}\n</tool_output>`,
+ })
+ const final = await request(simulator, input)
+ expect(JSON.parse(final.event.delta)).toEqual(receipt)
+ expect(simulator.evidence().searchCalls).toBe(before.searchCalls)
+ expect(simulator.evidence().describeCalls).toBe(before.describeCalls)
+ expect(
+ simulator
+ .evidence()
+ .requests.slice(before.requests.length)
+ .map(row => row.stage)
+ ).toEqual([bridges[2], 'final'])
+ })
+
+ it('approval denial returns the real denial and never requests another business call', async () => {
+ const simulator = createApprovedToolsUpstream()
+ const input: Entry[] = [{ role: 'user', content: 'verification receipt' }]
+ await complete(simulator, input, 'denial')
+ input.push({ role: 'user', content: 'Please read my verification receipt.' })
+ const call = await request(simulator, input)
+ input.push(call.event.item, {
+ type: 'function_call_output',
+ call_id: call.event.item.call_id,
+ output: 'Error: Approval denied by user',
+ })
+ const final = await request(simulator, input)
+ expect(final.event.delta).toContain('Approval denied by user')
+ expect(simulator.evidence().businessCalls).toBe(2)
+ expect(simulator.evidence().deniedResponses).toBe(1)
+ input.push({ role: 'user', content: 'verification receipt please' })
+ expect((await request(simulator, input)).event.item.name).toBe(bridges[0])
+ })
+
+ it('revalidates an obsolete cached target through real search after a catalog denial', async () => {
+ const simulator = createApprovedToolsUpstream()
+ const input: Entry[] = [{ role: 'user', content: 'verification receipt' }]
+ await complete(simulator, input, 'obsolete')
+ input.push({ role: 'user', content: 'verification receipt again' })
+ const call = await request(simulator, input)
+ input.push(call.event.item, {
+ type: 'function_call_output',
+ call_id: call.event.item.call_id,
+ output: 'Error: Tool not found in current catalog',
+ })
+ const search = await request(simulator, input)
+ expect(search.event.item.name).toBe(bridges[0])
+ input.push(search.event.item, {
+ type: 'function_call_output',
+ call_id: search.event.item.call_id,
+ output: JSON.stringify({ found: 0, results: [] }),
+ })
+ expect((await request(simulator, input)).event.delta).toContain('"found":0')
+ })
+
+ it.each(['running', 'completed'])(
+ 'uses the native workflow result after %s status, never a fabricated receipt',
+ async phase => {
+ const simulator = createApprovedToolsUpstream()
+ const workflow = 'approved-receipt-workflow'
+ const nativeNames = [
+ 'workflow_list',
+ 'workflow_trigger',
+ 'workflow_status',
+ 'workflow_result',
+ ]
+ const definitions = [
+ ...tools,
+ ...nativeNames.map(name => ({ type: 'function', name, parameters: { type: 'object' } })),
+ ]
+ const input: Entry[] = [
+ {
+ role: 'user',
+ content: `Run my ${workflow} workflow, check that it finishes, and show the business ID from its result artifact.`,
+ },
+ ]
+ const businessId = randomUUID()
+ const outputs = [
+ {
+ items: [
+ { name: 'other-workflow' },
+ {
+ name: workflow,
+ requiresInput: false,
+ targets: [{ kind: 'user', label: 'Personal' }],
+ },
+ ],
+ },
+ { workflowName: workflow, phase: 'pending' },
+ { name: workflow, latestRun: { phase } },
+ {
+ workflowName: workflow,
+ artifactAvailable: true,
+ result: { businessId, tool: 'workitem_read_receipt' },
+ },
+ ]
+ for (let index = 0; index < nativeNames.length; index++) {
+ const call = await request(simulator, input, definitions)
+ expect(call.response.status).toBe(200)
+ expect(call.event.item.name).toBe(nativeNames[index])
+ if (index > 0)
+ expect(JSON.parse(call.event.item.arguments)).toEqual({
+ name: workflow,
+ targetLabel: 'Personal',
+ })
+ input.push(call.event.item, {
+ type: 'function_call_output',
+ call_id: call.event.item.call_id,
+ output: `<tool_output name="${nativeNames[index]}" sanitized="false">\n${JSON.stringify(outputs[index])}\n</tool_output>`,
+ })
+ }
+ const final = await request(simulator, input, definitions)
+ expect(JSON.parse(final.event.delta)).toEqual(outputs[3])
+ expect(simulator.evidence().requests.map(row => row.stage)).toEqual([...nativeNames, 'final'])
+ expect(simulator.evidence().businessCalls).toBe(0)
+ }
+ )
+
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' }
diff --git a/codex-llm-proxy/test/approvedToolsUpstream.ts b/codex-llm-proxy/test/approvedToolsUpstream.ts
index 730d7293c..a82fbba83 100644
--- a/codex-llm-proxy/test/approvedToolsUpstream.ts
+++ b/codex-llm-proxy/test/approvedToolsUpstream.ts
@@ -1,5 +1,6 @@
/** External model boundary for the isolated approved-tools E2E image only. */
import { randomUUID } from 'node:crypto'
+import { isDeepStrictEqual } from 'node:util'
type Row = Record<string, unknown>
const COMPLETIONS = 'https://chatgpt.com/backend-api/codex/responses'
@@ -30,8 +31,11 @@ function textResult(value: unknown, expectedName: string): Row {
}
value = wrapped[2]
}
+ // Real tool failures can be plain sanitized text; preserve that failure as
+ // data rather than inventing a successful result or retrying an approval.
+ if (typeof value === 'string' && !value.trim().startsWith('{')) return { error: value }
const result = parse(value)
- if (result.isError === true || result.is_error === true) throw new Error('tool_failed')
+ if (result.isError === true || result.is_error === true) return result
if (Array.isArray(result.content)) {
const blocks = result.content.map(record)
if (blocks.length !== 1 || blocks[0]?.type !== 'text')
@@ -41,6 +45,229 @@ function textResult(value: unknown, expectedName: string): Row {
return result
}
+type Exchange = { call: Row; args: Row; result: Row }
+type Decision = { stage: string; args?: Row; result?: Row }
+
+function expectedCall(exchange: Exchange, decision: Decision): void {
+ if (
+ exchange.call.name !== decision.stage ||
+ !isDeepStrictEqual(exchange.args, decision.args)
+ ) {
+ throw new Error('unexpected_fixture_call')
+ }
+}
+
+function exchanges(rows: Row[]): Exchange[] {
+ const calls = rows.filter(row => row.type === 'function_call')
+ const outputs = rows.filter(row => row.type === 'function_call_output')
+ if (calls.length > 12 || calls.length !== outputs.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')
+ return calls.map(call => {
+ if (typeof call.call_id !== 'string' || typeof call.name !== 'string')
+ throw new Error('invalid_call')
+ const matched = outputs.filter(output => output.call_id === call.call_id)
+ if (matched.length !== 1 || rows.indexOf(matched[0]!) < rows.indexOf(call))
+ throw new Error('uncorrelated_result')
+ const args = parse(call.arguments)
+ const expectedName = call.name === BRIDGES[2] ? args.name : call.name
+ if (typeof expectedName !== 'string') throw new Error('invalid_envelope_target')
+ return { call, args, result: textResult(matched[0]!.output, expectedName) }
+ })
+}
+
+function failed(result: Row): boolean {
+ return result.isError === true || result.is_error === true || Boolean(result.error)
+}
+
+function described(exchange: Exchange): string {
+ if (
+ exchange.call.name !== BRIDGES[1] ||
+ exchange.result.found !== true ||
+ exchange.result.name !== exchange.args.name ||
+ typeof exchange.args.name !== 'string'
+ )
+ throw new Error('description_target_mismatch')
+ const schema = record(exchange.result.parameters)
+ if (schema.type !== 'object' || (Array.isArray(schema.required) && schema.required.length))
+ throw new Error('fixture_requires_empty_arguments')
+ return exchange.args.name
+}
+
+function receipt(exchange: Exchange, candidate: string): Row {
+ if (
+ exchange.call.name !== BRIDGES[2] ||
+ exchange.args.name !== candidate ||
+ Object.keys(record(exchange.args.arguments)).length
+ )
+ throw new Error('invocation_target_mismatch')
+ const value = exchange.result
+ if (
+ typeof value.businessId !== 'string' ||
+ !value.businessId ||
+ typeof value.runId !== 'string' ||
+ typeof value.callId !== 'string' ||
+ typeof value.tool !== 'string' ||
+ !candidate.endsWith(`__${value.tool}`)
+ )
+ throw new Error('missing_business_result')
+ return value
+}
+
+function reusableDescription(history: Row[]): string | undefined {
+ let previousUser = history.length - 1
+ while (previousUser >= 0 && history[previousUser]?.role !== 'user') previousUser--
+ if (previousUser < 0) return undefined
+ try {
+ // A canceled or denied previous task invalidates reuse; an ordinary new
+ // request must recover via discovery, not continue its interrupted call.
+ const previous = exchanges(history.slice(previousUser + 1))
+ const last = previous.at(-1)
+ if (
+ !last ||
+ last.call.name !== BRIDGES[2] ||
+ failed(last.result) ||
+ typeof last.args.name !== 'string'
+ )
+ return undefined
+ receipt(last, last.args.name)
+ const allCalls = history.filter(row => row.type === 'function_call' && row.name === BRIDGES[1])
+ for (const call of allCalls.reverse()) {
+ const result = history.find(
+ row => row.type === 'function_call_output' && row.call_id === call.call_id
+ )
+ if (!result) continue
+ const candidate = described(exchanges([call, result])[0]!)
+ if (candidate === last.args.name) return candidate
+ }
+ } catch {
+ // Historical partial results are expected after genuine user cancellation.
+ // They cannot supply a reusable schema, but never block the new task.
+ }
+ return undefined
+}
+
+function receiptDecision(current: Exchange[], known?: string): Decision {
+ const last = current.at(-1)
+ if (!last)
+ return known
+ ? { stage: BRIDGES[2]!, args: { name: known, arguments: {} } }
+ : { stage: BRIDGES[0]!, args: { query: FIXTURE_QUERY, limit: 5 } }
+ expectedCall(last, receiptDecision(current.slice(0, -1), known))
+ if (failed(last.result)) {
+ // An obsolete cached target can be rediscovered. Approval denial/cancel
+ // remains terminal and must never cause an automatic business retry.
+ if (
+ last.call.name === BRIDGES[2] &&
+ /not found|not available|unavailable|not .*catalog/i.test(JSON.stringify(last.result)) &&
+ !/approval|denied|cancel|reject/i.test(JSON.stringify(last.result))
+ )
+ return { stage: BRIDGES[0]!, args: { query: FIXTURE_QUERY, limit: 5 } }
+ return { stage: 'denied', result: last.result }
+ }
+ if (last.call.name === BRIDGES[0]) {
+ if (last.args.query !== FIXTURE_QUERY || !Array.isArray(last.result.results))
+ throw new Error('invalid_search_result')
+ if (last.result.found === 0 && last.result.results.length === 0)
+ return { stage: 'denied', result: last.result }
+ if (last.result.results.length !== 1) throw new Error('search_must_find_one_target')
+ const match = record(last.result.results[0])
+ if (
+ typeof match.name !== 'string' ||
+ BRIDGES.includes(match.name) ||
+ 'parameters' in match ||
+ 'inputSchema' in match
+ )
+ throw new Error('invalid_search_target')
+ return { stage: BRIDGES[1]!, args: { name: match.name } }
+ }
+ if (last.call.name === BRIDGES[1]) {
+ if (last.result.found === false)
+ return { stage: BRIDGES[0]!, args: { query: FIXTURE_QUERY, limit: 5 } }
+ const candidate = described(last)
+ const search = current.at(-2)
+ if (
+ !search ||
+ search.call.name !== BRIDGES[0] ||
+ !Array.isArray(search.result.results) ||
+ record(search.result.results[0]).name !== candidate
+ )
+ throw new Error('description_without_search')
+ return { stage: BRIDGES[2]!, args: { name: candidate, arguments: {} } }
+ }
+ if (last.call.name === BRIDGES[2]) {
+ const prior = current.at(-2)
+ const candidate = prior?.call.name === BRIDGES[1] ? described(prior) : known
+ if (!candidate) throw new Error('invocation_without_schema')
+ return { stage: 'final', result: receipt(last, candidate) }
+ }
+ throw new Error('unsupported_receipt_transition')
+}
+
+function workflowDecision(current: Exchange[], prompt: string): Decision {
+ if (!current.length) return { stage: 'workflow_list', args: {} }
+ expectedCall(current.at(-1)!, workflowDecision(current.slice(0, -1), prompt))
+ const listed = current[0]!
+ if (
+ listed.call.name !== 'workflow_list' ||
+ failed(listed.result) ||
+ !Array.isArray(listed.result.items)
+ )
+ throw new Error('workflow_list_required')
+ const matches = listed.result.items
+ .map(record)
+ .filter(item => typeof item.name === 'string' && prompt.includes(item.name))
+ if (matches.length !== 1 || matches[0]!.requiresInput === true)
+ throw new Error('workflow_not_uniquely_resolved')
+ const selected = matches[0]!
+ const args: Row = { name: selected.name }
+ if (Array.isArray(selected.targets)) {
+ const targets = selected.targets.map(record)
+ const matching =
+ targets.length === 1
+ ? targets
+ : targets.filter(
+ target => typeof target.label === 'string' && prompt.includes(target.label)
+ )
+ if (matching.length !== 1 || typeof matching[0]!.label !== 'string')
+ throw new Error('ambiguous_workflow_target')
+ args.targetLabel = matching[0]!.label
+ }
+ const last = current.at(-1)!
+ if (failed(last.result)) return { stage: 'denied', result: last.result }
+ if (current.length === 1) return { stage: 'workflow_trigger', args }
+ for (const item of current.slice(1)) {
+ if (item.args.name !== selected.name) throw new Error('workflow_target_mismatch')
+ }
+ if (last.call.name === 'workflow_trigger' && current.length === 2) {
+ if (last.result.workflowName !== selected.name) throw new Error('trigger_binding_missing')
+ return { stage: 'workflow_status', args }
+ }
+ if (last.call.name === 'workflow_status' && current.length === 3) {
+ if (last.result.name !== selected.name) throw new Error('status_binding_missing')
+ const latest = last.result.latestRun ? record(last.result.latestRun) : undefined
+ if (
+ ['failed', 'cancelled', 'canceled'].includes(
+ String(latest?.phase ?? last.result.workflowPhase)
+ )
+ )
+ return { stage: 'denied', result: last.result }
+ // workflow_result performs the real bounded artifact-readiness polling.
+ return { stage: 'workflow_result', args }
+ }
+ if (last.call.name === 'workflow_result' && current.length === 4) {
+ if (
+ last.result.workflowName !== selected.name ||
+ last.result.artifactAvailable !== true ||
+ typeof record(last.result.result).businessId !== 'string'
+ )
+ throw new Error('workflow_artifact_missing')
+ return { stage: 'final', result: last.result }
+ }
+ throw new Error('unsupported_workflow_transition')
+}
+
function stream(event: Row): Response {
return new Response(
[event, { type: 'response.completed' }].map(row => `data: ${JSON.stringify(row)}\n\n`).join(''),
@@ -97,67 +324,14 @@ export function createApprovedToolsUpstream() {
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))
+ const isWorkflow = /\bworkflow\b/i.test(user.content)
+ if (!isWorkflow && !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')
+ const current = exchanges(history.slice(lastUser + 1))
+ const decision = isWorkflow
+ ? workflowDecision(current, user.content)
+ : receiptDecision(current, reusableDescription(history.slice(0, lastUser)))
+ const { stage } = decision
if (evidence.requests.length >= 512) throw new Error('evidence_capacity_exceeded')
evidence.completions++
// This fixture task needs one receipt tool. Other fixture catalog names
@@ -178,21 +352,22 @@ export function createApprovedToolsUpstream() {
leakedSchema: connectorDefinitionCount > 0 || fixtureNames.size > 1,
stage,
})
- if (denied) {
+ if (stage === 'denied') {
evidence.deniedResponses++
- return stream({ type: 'response.output_text.delta', delta: JSON.stringify(results[0]) })
+ return stream({
+ type: 'response.output_text.delta',
+ delta: JSON.stringify(decision.result),
+ })
}
if (stage === 'final') {
evidence.finalResponses++
- return stream({ type: 'response.output_text.delta', delta: JSON.stringify(results[2]) })
+ return stream({
+ type: 'response.output_text.delta',
+ delta: JSON.stringify(decision.result),
+ })
}
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: {} }
+ const args = decision.args
if (stage === BRIDGES[0]) evidence.searchCalls++
if (stage === BRIDGES[1]) evidence.describeCalls++
if (stage === BRIDGES[2]) evidence.businessCalls++
diff --git a/tests/e2e/fixtures/codex-subscription/approved-tools-workflow/Dockerfile b/tests/e2e/fixtures/codex-subscription/approved-tools-workflow/Dockerfile
new file mode 100644
index 000000000..b514985d0
--- /dev/null
+++ b/tests/e2e/fixtures/codex-subscription/approved-tools-workflow/Dockerfile
@@ -0,0 +1,7 @@
+# Harness supplies the exact branch-owned image with the real Workflow SDK.
+ARG WORKFLOW_CUSTOM_SDK_IMAGE
+FROM ${WORKFLOW_CUSTOM_SDK_IMAGE}
+LABEL ai.evenfire.test-only="approved-tools-workflow"
+WORKDIR /app
+COPY tests/e2e/fixtures/codex-subscription/approved-tools-workflow/index.js ./index.js
+CMD ["node", "index.js"]
diff --git a/tests/e2e/fixtures/codex-subscription/approved-tools-workflow/index.js b/tests/e2e/fixtures/codex-subscription/approved-tools-workflow/index.js
new file mode 100644
index 000000000..e9c3dd76e
--- /dev/null
+++ b/tests/e2e/fixtures/codex-subscription/approved-tools-workflow/index.js
@@ -0,0 +1,146 @@
+// Isolated custom-coordinator fixture. The SDK owns WRC identity and status.
+const fs = require('node:fs/promises')
+const { randomUUID } = require('node:crypto')
+
+const ARTIFACT_NAME = 'approved-tools-workflow-result.json'
+const ARTIFACT_PATH = `/output/${ARTIFACT_NAME}`
+
+function receiptEndpoint(spec) {
+ if (!Array.isArray(spec.mcpServers) || spec.mcpServers.length !== 1) {
+ throw new Error('exactly_one_declared_receipt_service_required')
+ }
+ const endpoint = new URL(spec.mcpServers[0].endpoint)
+ if (
+ endpoint.protocol !== 'http:' ||
+ endpoint.username ||
+ endpoint.password ||
+ endpoint.search ||
+ endpoint.hash ||
+ endpoint.pathname !== '/mcp' ||
+ !/^[a-z0-9-]+\.mcp-server\.svc\.cluster\.local$/.test(endpoint.hostname) ||
+ !endpoint.port ||
+ Number(endpoint.port) < 1024
+ )
+ throw new Error('receipt_service_must_be_declared_local_mcp_fixture')
+ return endpoint.href
+}
+
+async function readReceipt(endpoint, fetchImpl = fetch) {
+ const rpc = async (method, params, notification = false) => {
+ const id = notification ? undefined : randomUUID()
+ const response = await fetchImpl(endpoint, {
+ method: 'POST',
+ redirect: 'error',
+ headers: {
+ 'content-type': 'application/json',
+ accept: 'application/json, text/event-stream',
+ 'mcp-protocol-version': '2025-03-26',
+ },
+ body: JSON.stringify({ jsonrpc: '2.0', ...(id && { id }), method, params }),
+ signal: AbortSignal.timeout(15000),
+ })
+ if (!response.ok) throw new Error('receipt_mcp_request_failed')
+ if (notification) return undefined
+ const text = await response.text()
+ if (Buffer.byteLength(text) > 1048576) throw new Error('receipt_mcp_response_too_large')
+ const body = JSON.parse(text)
+ if (body.jsonrpc !== '2.0' || body.id !== id || body.error || !body.result)
+ throw new Error('invalid_receipt_mcp_response')
+ return body.result
+ }
+ await rpc('initialize', {
+ protocolVersion: '2025-03-26',
+ capabilities: {},
+ clientInfo: { name: 'approved-tools-workflow-fixture', version: '1.0.0' },
+ })
+ await rpc('notifications/initialized', {}, true)
+ const catalog = await rpc('tools/list', {})
+ if (!Array.isArray(catalog.tools)) throw new Error('receipt_catalog_missing')
+ const matches = catalog.tools.filter(
+ tool =>
+ typeof tool.description === 'string' && tool.description.includes('verification receipt')
+ )
+ if (
+ matches.length !== 1 ||
+ matches[0].inputSchema?.type !== 'object' ||
+ matches[0].inputSchema?.required?.length
+ )
+ throw new Error('receipt_tool_not_uniquely_resolved')
+ const selected = matches[0].name
+ // Exactly one business invocation; never retry a request after uncertain execution.
+ const result = await rpc('tools/call', { name: selected, arguments: {} })
+ if (
+ result.isError ||
+ !Array.isArray(result.content) ||
+ result.content.length !== 1 ||
+ result.content[0].type !== 'text'
+ )
+ throw new Error('invalid_receipt_result')
+ const receipt = JSON.parse(result.content[0].text)
+ if (
+ receipt.tool !== selected ||
+ typeof receipt.businessId !== 'string' ||
+ typeof receipt.runId !== 'string' ||
+ typeof receipt.callId !== 'string'
+ )
+ throw new Error('receipt_binding_missing')
+ return receipt
+}
+
+async function main() {
+ if (!process.env.CLERUM_WORKFLOW_RUN_ID?.trim())
+ throw new Error('triggered_workflow_run_required')
+ const { WorkflowSDK, emitLog } = require('@clerum/workflow-sdk')
+ const sdk = await WorkflowSDK.fromEnvironment()
+ try {
+ const spec = await sdk.config.getSpec()
+ if (spec.steps?.length !== 1 || spec.steps[0].id !== 'receipt')
+ throw new Error('receipt_step_required')
+ const endpoint = receiptEndpoint(spec)
+ sdk.updatePhase('running')
+ await sdk.status.reportWorkflowStatus('running')
+ sdk.updateStepState('receipt', { phase: 'running' })
+ await sdk.status.reportStepStatus('receipt', 'running', {
+ executor: 'custom',
+ startedAt: new Date().toISOString(),
+ })
+ const receipt = await readReceipt(endpoint)
+ await fs.mkdir('/output', { recursive: true })
+ await fs.writeFile(ARTIFACT_PATH, JSON.stringify(receipt), { flag: 'wx' })
+ const { size } = await fs.stat(ARTIFACT_PATH)
+ const output = {
+ artifacts: [
+ {
+ name: ARTIFACT_NAME,
+ format: 'json',
+ sizeBytes: size,
+ path: ARTIFACT_PATH,
+ createdAt: new Date().toISOString(),
+ },
+ ],
+ }
+ sdk.updateStepState('receipt', { phase: 'completed', output })
+ await sdk.status.reportStepStatus('receipt', 'completed', {
+ executor: 'custom',
+ output,
+ completedAt: new Date().toISOString(),
+ })
+ sdk.updatePhase('completed')
+ await sdk.status.reportWorkflowStatus('completed', { completedAt: new Date().toISOString() })
+ emitLog('info', 'Approved-tools workflow receipt completed')
+ } catch {
+ sdk.updatePhase('failed')
+ await sdk.status.reportWorkflowStatus('failed', {
+ failureReason: 'approved_tools_receipt_failed',
+ })
+ process.exitCode = 1
+ } finally {
+ await sdk.shutdown()
+ }
+}
+
+module.exports = { receiptEndpoint, readReceipt }
+if (require.main === module)
+ main().catch(() => {
+ process.exitCode = 1
+ })
diff --git a/tests/e2e/fixtures/codex-subscription/approved-tools-workflow/index.test.mjs b/tests/e2e/fixtures/codex-subscription/approved-tools-workflow/index.test.mjs
new file mode 100644
index 000000000..4f28457a9
--- /dev/null
+++ b/tests/e2e/fixtures/codex-subscription/approved-tools-workflow/index.test.mjs
@@ -0,0 +1,89 @@
+import assert from 'node:assert/strict'
+import { spawnSync } from 'node:child_process'
+import { once } from 'node:events'
+import { test } from 'node:test'
+import { fileURLToPath } from 'node:url'
+import { createApprovedToolsFixture } from '../approved-tools/server.mjs'
+import coordinator from './index.js'
+
+test('coordinator refuses eager execution without a triggered workflow run', () => {
+ const result = spawnSync(
+ process.execPath,
+ [fileURLToPath(new URL('./index.js', import.meta.url))],
+ { env: {}, encoding: 'utf8' }
+ )
+ assert.equal(result.status, 1)
+ assert.equal(result.stdout, '')
+ assert.equal(result.stderr, '')
+})
+
+test('coordinator receipt client executes the selected real local MCP once', async t => {
+ const runId = 'workflow-unit-contract'
+ const server = createApprovedToolsFixture({ catalogSize: 83, 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}`
+ const receipt = await coordinator.readReceipt(`${base}/mcp`)
+ const evidence = await fetch(`${base}/evidence?runId=${runId}`).then(response => response.json())
+ assert.equal(receipt.runId, runId)
+ assert.equal(receipt.tool, 'workitem_read_receipt')
+ assert.match(receipt.businessId, /^[a-f0-9-]{36}$/)
+ assert.deepEqual(evidence.calls, [receipt])
+})
+
+test('coordinator accepts only one declared cluster-local fixture endpoint', () => {
+ const valid = 'http://approved-tools-workflow.mcp-server.svc.cluster.local:8080/mcp'
+ assert.equal(
+ coordinator.receiptEndpoint({ mcpServers: [{ id: 'receipt', endpoint: valid }] }),
+ valid
+ )
+ for (const endpoint of [
+ 'https://example.com/mcp',
+ 'http://127.0.0.1:8080/mcp',
+ valid + '?override=x',
+ valid.replace('/mcp', '/other'),
+ ]) {
+ assert.throws(() => coordinator.receiptEndpoint({ mcpServers: [{ id: 'receipt', endpoint }] }))
+ }
+ assert.throws(() => coordinator.receiptEndpoint({ mcpServers: [] }))
+ assert.throws(() =>
+ coordinator.receiptEndpoint({ mcpServers: [{ endpoint: valid }, { endpoint: valid }] })
+ )
+})
+
+test('an uncertain business invocation is not retried', async () => {
+ let calls = 0
+ await assert.rejects(
+ coordinator.readReceipt('http://fixture/mcp', async (_input, init) => {
+ const request = JSON.parse(init.body)
+ if (request.method === 'notifications/initialized') return new Response(null, { status: 202 })
+ if (request.method === 'tools/call') {
+ calls++
+ throw new Error('connection_lost')
+ }
+ return Response.json({
+ jsonrpc: '2.0',
+ id: request.id,
+ result:
+ request.method === 'tools/list'
+ ? {
+ tools: [
+ {
+ name: 'receipt',
+ description: 'verification receipt',
+ inputSchema: { type: 'object' },
+ },
+ ],
+ }
+ : { protocolVersion: '2025-03-26' },
+ })
+ })
+ )
+ assert.equal(calls, 1)
+})
diff --git a/tests/e2e/fixtures/codex-subscription/approved-tools-workflow/package.json b/tests/e2e/fixtures/codex-subscription/approved-tools-workflow/package.json
new file mode 100644
index 000000000..927afbed6
--- /dev/null
+++ b/tests/e2e/fixtures/codex-subscription/approved-tools-workflow/package.json
@@ -0,0 +1,5 @@
+{
+ "name": "approved-tools-workflow-fixture",
+ "private": true,
+ "type": "commonjs"
+}
diff --git a/tests/e2e/fixtures/package-lock.json b/tests/e2e/fixtures/package-lock.json
index 07b427e25..334f53d1e 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.72",
+ "version": "1.0.73",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@clerum/e2e-test-fixtures",
- "version": "1.0.72",
+ "version": "1.0.73",
"dependencies": {
"@kubernetes/client-node": "^1.0.0"
},
diff --git a/tests/e2e/fixtures/package.json b/tests/e2e/fixtures/package.json
index cb4361eaa..65ca97022 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.72",
+ "version": "1.0.73",
"description": "E2E test fixtures and utilities for Clerum testing",
"type": "module",
"main": "index.ts",