feat(engine): expose canonical replay sessions through native workers
This commit is contained in:
parent
417a7bd21e
commit
e3635f23cf
26 changed files with 2693 additions and 33 deletions
|
|
@ -1,5 +1,10 @@
|
|||
import { fork, type ChildProcess } from 'node:child_process';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import {
|
||||
parseReplayResult,
|
||||
replayConfigSchema,
|
||||
replayIdentitySchema,
|
||||
} from '../contracts/native-replay';
|
||||
import { nativeWorkerMessageSchema, type NativeWorkerRequest } from './native-worker-protocol';
|
||||
import { BacktestConflictError } from './run-repository';
|
||||
|
||||
|
|
@ -23,6 +28,19 @@ export class NativeBacktestWorker {
|
|||
if (this.active) {
|
||||
throw new BacktestConflictError('A native backtest worker is already running');
|
||||
}
|
||||
// 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'
|
||||
? {
|
||||
identity: replayIdentitySchema.parse({
|
||||
version: 1,
|
||||
sessionId: request.config.matching.risk.ledger.sessionId,
|
||||
datasetHash: (request.dataset as { manifest?: { hash?: unknown } })?.manifest?.hash,
|
||||
}),
|
||||
policy: replayConfigSchema.parse(request.config),
|
||||
}
|
||||
: undefined;
|
||||
const child = fork(fileURLToPath(this.workerUrl), [], {
|
||||
execPath: process.execPath,
|
||||
execArgv: [],
|
||||
|
|
@ -61,6 +79,14 @@ export class NativeBacktestWorker {
|
|||
if (message.type === 'error') {
|
||||
terminate(new Error(message.message));
|
||||
} else if (message.type === 'result') {
|
||||
if (expectedReplay) {
|
||||
try {
|
||||
parseReplayResult(message.result, expectedReplay.identity, expectedReplay.policy);
|
||||
} catch (error) {
|
||||
terminate(error instanceof Error ? error : new Error(String(error)));
|
||||
return;
|
||||
}
|
||||
}
|
||||
result = message.result;
|
||||
} else {
|
||||
try {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue