// Supplied synthetic snapshots only: no model execution, release or network. const fields = ['record_id', 'tenant_id', 'amount_minor', 'currency', 'evidence_id', 'decision']; const forbiddenKeys = new Set(['__proto__', 'prototype', 'constructor']); const text = (value, maximum = 200) => typeof value === 'string' && value.trim().length > 0 && value.length <= maximum; const closed = (value, keys) => value !== null && typeof value === 'object' && !Array.isArray(value) && Object.keys(value).length === keys.length && keys.every(key => Object.hasOwn(value, key)); const amount = value => Number.isSafeInteger(value) && value >= 0; const currency = value => typeof value === 'string' && /^[A-Z]{3}$/.test(value); function jsonSafe(value, ancestors = new Set()) { if (value === null || typeof value === 'string' || typeof value === 'boolean') return true; if (typeof value === 'number') return Number.isFinite(value); if (typeof value !== 'object' || ancestors.has(value)) return false; const array = Array.isArray(value); if (!array && Object.getPrototypeOf(value) !== Object.prototype && Object.getPrototypeOf(value) !== null) return false; const keys = Reflect.ownKeys(value); if (keys.some(key => typeof key !== 'string' || forbiddenKeys.has(key))) return false; if (array && (keys.length !== value.length + 1 || !Array.from({ length: value.length }, (_, index) => Object.hasOwn(value, String(index))).every(Boolean))) return false; ancestors.add(value); for (const key of keys) { if (array && key === 'length') continue; const descriptor = Object.getOwnPropertyDescriptor(value, key); if (!descriptor.enumerable || !Object.hasOwn(descriptor, 'value') || !jsonSafe(descriptor.value, ancestors)) { ancestors.delete(value); return false; } } ancestors.delete(value); return true; } function encode(value) { if (value === null || typeof value !== 'object') return JSON.stringify(value); if (Array.isArray(value)) return '[' + value.map(encode).join(',') + ']'; return '{' + Object.keys(value).sort().map(key => JSON.stringify(key) + ':' + encode(value[key])).join(',') + '}'; } export function canonical(value) { if (!jsonSafe(value)) throw new TypeError('Expected finite JSON values without cycles or dangerous keys.'); return encode(value); } export async function sha256(value) { if (typeof value !== 'string') throw new TypeError('SHA-256 input must be a string.'); const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(value)); return Array.from(new Uint8Array(digest), byte => byte.toString(16).padStart(2, '0')).join(''); } function outputShape(value) { return closed(value, fields) && ['record_id', 'tenant_id', 'evidence_id', 'decision'].every(key => text(value[key])) && amount(value.amount_minor) && currency(value.currency); } function wrapper(value) { if (!closed(value, ['prompt', 'evaluation_input', 'policy_revision', 'baseline', 'candidate'])) return false; if (!closed(value.prompt, ['prompt_id', 'text']) || !text(value.prompt.prompt_id) || !text(value.prompt.text, 4000)) return false; if (!outputShape(value.evaluation_input) || value.evaluation_input.decision !== 'HOLD' || value.policy_revision !== 'synthetic-control-v1') return false; return [value.baseline, value.candidate].every(snapshot => closed(snapshot, ['model_id', 'model_revision', 'prompt_sha256', 'evaluation_sha256', 'output']) && text(snapshot.model_id) && text(snapshot.model_revision) && typeof snapshot.prompt_sha256 === 'string' && /^[a-f0-9]{64}$/.test(snapshot.prompt_sha256) && typeof snapshot.evaluation_sha256 === 'string' && /^[a-f0-9]{64}$/.test(snapshot.evaluation_sha256)); } function emptyResult() { return { comparison_code: 'HOLD_INVALID_INPUT', baseline_code: 'NOT_CHECKED', candidate_code: 'NOT_CHECKED', reasons: ['Input must satisfy the closed synthetic wrapper, scalar limits and finite JSON safety rules.'], prompt_sha256: null, evaluation_sha256: null, baseline_output_sha256: null, candidate_output_sha256: null, model_execution_status: 'NOT_RUN_SYNTHETIC_OUTPUTS', action_executed: false, release_eligible: false }; } function assess(output, expected) { if (!outputShape(output)) return 'HOLD_OUTPUT_SCHEMA'; const codes = ['HOLD_RECORD_SCOPE', 'HOLD_TENANT_SCOPE', 'HOLD_AMOUNT', 'HOLD_CURRENCY', 'HOLD_EVIDENCE', 'HOLD_DECISION']; for (let index = 0; index < fields.length; index++) if (output[fields[index]] !== expected[fields[index]]) return codes[index]; return 'PASS_IN_FIXTURE'; } export async function evaluate(value) { const result = emptyResult(); let input; try { if (!jsonSafe(value)) return result; const serialized = JSON.stringify(value); if (serialized.length > 64000 || !wrapper(value)) return result; // Snapshot before asynchronous hashing so caller edits cannot change this run. input = JSON.parse(serialized); } catch { return result; } [result.prompt_sha256, result.evaluation_sha256, result.baseline_output_sha256, result.candidate_output_sha256] = await Promise.all([ sha256(input.prompt.text), sha256(canonical(input.evaluation_input)), sha256(canonical(input.baseline.output)), sha256(canonical(input.candidate.output)) ]); const held = (code, reason) => { result.comparison_code = code; result.reasons = [reason]; return result; }; if ([input.baseline, input.candidate].some(snapshot => snapshot.prompt_sha256 !== result.prompt_sha256)) return held('HOLD_PROMPT_BINDING', 'At least one snapshot does not bind to the exact supplied prompt bytes.'); if ([input.baseline, input.candidate].some(snapshot => snapshot.evaluation_sha256 !== result.evaluation_sha256)) return held('HOLD_EVAL_BINDING', 'At least one snapshot does not bind to the canonical fixed evaluation input.'); if (input.baseline.model_id === input.candidate.model_id && input.baseline.model_revision === input.candidate.model_revision) return held('HOLD_MODEL_IDENTITY', 'The supplied model identity and revision tuples are identical.'); result.baseline_code = assess(input.baseline.output, input.evaluation_input); result.candidate_code = assess(input.candidate.output, input.evaluation_input); if (result.baseline_code !== 'PASS_IN_FIXTURE') return held('HOLD_BASELINE_FAILURE', 'The supplied baseline fails an exact-field control. The candidate was independently assessed.'); if (result.candidate_code !== 'PASS_IN_FIXTURE') return held('CONTROL_REGRESSION', 'The supplied baseline passes but the candidate fails an exact-field control. No inference or real release was performed.'); return held('NO_REGRESSION_IN_FIXTURE', 'Both supplied outputs pass the fixed exact-field controls. This does not validate a model or authorize a production release.'); } export function createSession({ evaluator = evaluate } = {}) { let inputText = '', version = 0, result = null; return { setInputText(value) { inputText = value; version++; result = null; }, async run() { const token = ++version, captured = inputText; let input; try { input = JSON.parse(captured); } catch { input = undefined; } const computed = await evaluator(input); if (token !== version) return null; result = structuredClone(computed); return structuredClone(result); }, get result() { return structuredClone(result); } }; }