/** This entrypoint runs ONLY inside the restricted image; never the API/native supervisor. */ import { sameIdentity } from '../contracts/native-replay'; import { importRetainedProgram } from '../strategies/retained-program'; import { StrategyController } from '../strategies/strategy-controller'; import { freeze } from '../strategies/strategy-decision'; import { candidateRequestSchema, readCandidateFrames, writeCandidateFrame, } from './candidate-protocol'; // Ordinary logs are diagnostics, not protocol. Deliberate raw stdout forgery remains untrusted. // eslint-disable-next-line no-console -- stdout belongs exclusively to the framed protocol. console.log = console.info = console.debug = (...values: unknown[]) => console.error(...values); let controller: StrategyController | undefined; let identity: ConstructorParameters[0] | undefined; let sequence = 0; try { for await (const raw of readCandidateFrames(process.stdin)) { const request = candidateRequestSchema.parse(raw); if (request.sequence !== sequence) { throw new Error('Candidate protocol sequence mismatch'); } const response = { version: 1, sequence }; if (request.type === 'transpile') { if (sequence !== 0 || controller) { throw new Error('Packaging must be the first and only host operation'); } const compiler = new Bun.Transpiler({ loader: 'ts', target: 'browser' }); if ( compiler.scanImports(request.source).length || /\b(?:import|require)\s*\(/.test(request.source) ) { throw new Error( 'Candidate packaging accepts self-contained TypeScript without import/require edges' ); } // Transformation only: top-level statements are data and are never imported or evaluated. const content = compiler.transformSync(request.source); if (compiler.scanImports(content).length) { throw new Error('Packaged candidate retained an import edge'); } await writeCandidateFrame(process.stdout, { ...response, type: 'transpiled', content, runtimeVersion: Bun.version, }); process.exit(0); } else if (request.type === 'init') { if (controller || sequence !== 0) { throw new Error('Candidate host already initialized'); } identity = freeze(request.identity); const program = await importRetainedProgram( request.package as Parameters[0], request.selection ); controller = new StrategyController(identity, [program], { matching: { owners: [{ ownerId: request.selection.ownerId, mode: 'Target' }] }, }); await writeCandidateFrame(process.stdout, { ...response, type: 'ready' }); } else { if (!controller || !identity) { throw new Error('Candidate host is not initialized'); } if (request.type === 'stage') { sameIdentity(request.published.identity, identity); if (request.published.step.type !== 'decisionBatch') { throw new Error('Candidate requires a decision barrier'); } freeze(request.published); const decisions = controller.stage(request.published.step, request.published.observations); await writeCandidateFrame(process.stdout, { ...response, type: 'decisions', decisions }); } else if (request.type === 'committed') { sameIdentity(request.admitted.identity, identity); freeze(request.admitted.outcome); controller.committed(request.admitted.outcome); await writeCandidateFrame(process.stdout, { ...response, type: 'ack' }); } else { await writeCandidateFrame(process.stdout, { ...response, type: 'finished' }); process.exit(0); } } sequence++; } throw new Error('Candidate supervisor disconnected before finish'); } catch (error) { try { await writeCandidateFrame(process.stdout, { version: 1, sequence, type: 'error', message: String(error).slice(0, 4096), }); } finally { process.exit(1); } }