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
|
|
@ -8,6 +8,10 @@ import {
|
|||
replayIdentitySchema,
|
||||
replayResultFormatSchema,
|
||||
} from '../contracts/native-replay';
|
||||
import {
|
||||
parseStrategyReplayResult,
|
||||
strategyReplayRequestSchema,
|
||||
} from '../contracts/strategy-replay';
|
||||
import { nativeWorkerMessageSchema, type NativeWorkerRequest } from './native-worker-protocol';
|
||||
import { BacktestConflictError } from './run-repository';
|
||||
|
||||
|
|
@ -31,18 +35,25 @@ export class NativeBacktestWorker {
|
|||
if (this.active) {
|
||||
throw new BacktestConflictError('A native backtest worker is already running');
|
||||
}
|
||||
// Detach the entire normalized strategy request, including the frozen-data
|
||||
// payload. The caller must not mutate what is sent or what counts as success.
|
||||
const strategyRequest =
|
||||
request.type === 'runStrategyReplay'
|
||||
? structuredClone(strategyReplayRequestSchema.parse(request))
|
||||
: undefined;
|
||||
const submitted = strategyRequest ?? request;
|
||||
// Capture expected identity/policy before asynchronous execution. A caller
|
||||
// mutating its request cannot change which completed child result is valid.
|
||||
const expectedReplay =
|
||||
request.type === 'runReplayTrace'
|
||||
submitted.type === 'runReplayTrace' || submitted.type === 'runStrategyReplay'
|
||||
? {
|
||||
identity: replayIdentitySchema.parse({
|
||||
version: 1,
|
||||
sessionId: request.config.matching.risk.ledger.sessionId,
|
||||
datasetHash: (request.dataset as { manifest?: { hash?: unknown } })?.manifest?.hash,
|
||||
sessionId: submitted.config.matching.risk.ledger.sessionId,
|
||||
datasetHash: (submitted.dataset as { manifest?: { hash?: unknown } })?.manifest?.hash,
|
||||
}),
|
||||
policy: replayConfigSchema.parse(request.config),
|
||||
format: replayResultFormatSchema.parse(request.resultFormat ?? 'trace'),
|
||||
policy: replayConfigSchema.parse(submitted.config),
|
||||
format: replayResultFormatSchema.parse(submitted.resultFormat ?? 'trace'),
|
||||
}
|
||||
: undefined;
|
||||
const child = fork(fileURLToPath(this.workerUrl), [], {
|
||||
|
|
@ -85,13 +96,20 @@ export class NativeBacktestWorker {
|
|||
} else if (message.type === 'result') {
|
||||
if (expectedReplay) {
|
||||
try {
|
||||
const parse =
|
||||
expectedReplay.format === 'report'
|
||||
? parseReplayReportResult
|
||||
: expectedReplay.format === 'projection'
|
||||
? parseReplayProjectionResult
|
||||
: parseReplayResult;
|
||||
parse(message.result, expectedReplay.identity, expectedReplay.policy);
|
||||
if (strategyRequest) {
|
||||
parseStrategyReplayResult(message.result, {
|
||||
...expectedReplay,
|
||||
strategies: strategyRequest.strategies,
|
||||
});
|
||||
} else {
|
||||
const parse =
|
||||
expectedReplay.format === 'report'
|
||||
? parseReplayReportResult
|
||||
: expectedReplay.format === 'projection'
|
||||
? parseReplayProjectionResult
|
||||
: parseReplayResult;
|
||||
parse(message.result, expectedReplay.identity, expectedReplay.policy);
|
||||
}
|
||||
} catch (error) {
|
||||
terminate(error instanceof Error ? error : new Error(String(error)));
|
||||
return;
|
||||
|
|
@ -144,7 +162,7 @@ export class NativeBacktestWorker {
|
|||
return;
|
||||
}
|
||||
try {
|
||||
child.send(request, error => {
|
||||
child.send(submitted, error => {
|
||||
if (error) {
|
||||
terminate(error);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue