import { executeReplayTrace } from '../../apps/stock/orchestrator/src/backtest/native-replay'; import { nativeWorkerRequestSchema, type NativeWorkerMessage, } from '../../apps/stock/orchestrator/src/backtest/native-worker-protocol'; function send(message: NativeWorkerMessage | { invalid: boolean }): Promise { return new Promise((resolve, reject) => { process.send!(message, (error: Error | null) => (error ? reject(error) : resolve())); }); } process.once('message', async (input: unknown) => { const request = nativeWorkerRequestSchema.parse(input); if (request.type !== 'runReplayTrace') { throw new Error('Canonical replay lifecycle fixture only'); } const { config } = request; // Fixture controls are run labels, not unsupported production strategy parameters. const behavior = config.matching.risk.ledger.sessionId.startsWith('fixture:') ? config.matching.risk.ledger.sessionId.slice('fixture:'.length) : undefined; if (!behavior) { // The same adapter can recover and run the actual Rust engine after a failed worker. await import('../../apps/stock/orchestrator/src/backtest/native-worker'); process.emit('message', input, undefined); return; } await send({ type: 'progress', progress: 0, message: `Fixture worker ready:${process.pid}` }); switch (behavior) { case 'block': // Intentionally block this process. Messages, timers, and cooperative stop cannot run. while (true) { Math.sqrt(Math.random()); } case 'crash': process.kill(process.pid, 'SIGKILL'); break; case 'malformed': await send({ invalid: true }); break; case 'invalid-result': await send({ type: 'result', result: '{"not":"a backtest result"}' }); process.disconnect?.(); break; case 'reported-error': await send({ type: 'error', message: 'Fixture native failure' }); break; case 'no-result': process.exit(0); return; case 'result-then-block': await send({ type: 'result', result: JSON.stringify(executeReplayTrace(request).result) }); while (true) { Math.sqrt(Math.random()); } } });