feat(research): run trusted SMA strategies through native workers
This commit is contained in:
parent
4f6e44a853
commit
ca5abdde0e
16 changed files with 1682 additions and 28 deletions
|
|
@ -1,7 +1,13 @@
|
|||
import { BacktestEngine } from '@stock-bot/engine';
|
||||
import { executeReplayTrace } from './native-replay';
|
||||
import type { StrategyReplayResult } from '../contracts/strategy-replay';
|
||||
import {
|
||||
createBuiltinStrategy,
|
||||
validateBuiltinStrategyBindings,
|
||||
} from '../strategies/canonical-sma';
|
||||
import { executeReplayTrace, prepareReplayLoad } from './native-replay';
|
||||
import { resolveNativeStrategies } from './native-strategies';
|
||||
import { nativeWorkerRequestSchema, type NativeWorkerMessage } from './native-worker-protocol';
|
||||
import { runStrategyReplay } from './strategy-runtime';
|
||||
|
||||
/** One process, one native engine, one request. No database or service-container access. */
|
||||
function send(message: NativeWorkerMessage): Promise<void> {
|
||||
|
|
@ -17,6 +23,36 @@ function send(message: NativeWorkerMessage): Promise<void> {
|
|||
process.once('message', async (input: unknown) => {
|
||||
try {
|
||||
const request = nativeWorkerRequestSchema.parse(input);
|
||||
if (request.type === 'runStrategyReplay') {
|
||||
const load = prepareReplayLoad(request.dataset, request.config);
|
||||
validateBuiltinStrategyBindings(request.strategies, load);
|
||||
const programs = request.strategies.map(selection => {
|
||||
const program = createBuiltinStrategy(selection);
|
||||
if (
|
||||
program.ownerId !== selection.ownerId ||
|
||||
program.strategyId !== selection.strategyId ||
|
||||
program.strategyVersion !== selection.strategyVersion ||
|
||||
program.seed !== selection.seed ||
|
||||
program.mode !== 'target'
|
||||
) {
|
||||
throw new Error('Trusted strategy constructor identity mismatch');
|
||||
}
|
||||
return program;
|
||||
});
|
||||
await send({ type: 'progress', progress: 0, message: 'Canonical strategy replay started' });
|
||||
const { result } = runStrategyReplay(load, programs, {
|
||||
eventBudget: request.eventBudget,
|
||||
resultFormat: request.resultFormat,
|
||||
});
|
||||
const envelope: StrategyReplayResult = {
|
||||
type: 'strategyReplayResult',
|
||||
version: 1,
|
||||
strategies: request.strategies,
|
||||
result,
|
||||
};
|
||||
await send({ type: 'result', result: JSON.stringify(envelope) });
|
||||
return;
|
||||
}
|
||||
if (request.type === 'runReplayTrace') {
|
||||
await send({ type: 'progress', progress: 0, message: 'Canonical replay trace started' });
|
||||
// One load and a local pull loop; no per-observation IPC. The supervisor
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue