feat(research): run retained strategies through durable canonical engine
This commit is contained in:
parent
d929db707a
commit
618fb9e33c
58 changed files with 6596 additions and 779 deletions
|
|
@ -1,4 +1,5 @@
|
|||
import { fork, type ChildProcess } from 'node:child_process';
|
||||
import { realpathSync } from 'node:fs';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import {
|
||||
parseReplayProjectionResult,
|
||||
|
|
@ -12,19 +13,20 @@ import {
|
|||
parseStrategyReplayResult,
|
||||
strategyReplayRequestSchema,
|
||||
} from '../contracts/strategy-replay';
|
||||
import { nativeWorkerMessageSchema, type NativeWorkerRequest } from './native-worker-protocol';
|
||||
import { canonicalWorkerUrl } from './canonical-build';
|
||||
import {
|
||||
nativeWorkerMessageSchema,
|
||||
parseRetainedStrategyResult,
|
||||
retainedStrategyRequestSchema,
|
||||
type NativeWorkerRequest,
|
||||
} from './native-worker-protocol';
|
||||
import { BacktestConflictError } from './run-repository';
|
||||
|
||||
/** Hard cancellation requires a process boundary: a thread cannot interrupt native Rust. */
|
||||
export class NativeBacktestWorker {
|
||||
private active: ChildProcess | undefined;
|
||||
|
||||
constructor(
|
||||
private readonly workerUrl = new URL(
|
||||
import.meta.url.endsWith('.ts') ? './native-worker.ts' : './native-worker.js',
|
||||
import.meta.url
|
||||
)
|
||||
) {}
|
||||
constructor(private readonly workerUrl?: URL) {}
|
||||
|
||||
async execute(
|
||||
request: NativeWorkerRequest,
|
||||
|
|
@ -37,11 +39,30 @@ export class NativeBacktestWorker {
|
|||
}
|
||||
// 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 retainedRequest =
|
||||
request.type === 'runRetainedStrategy'
|
||||
? structuredClone(retainedStrategyRequestSchema.parse(request))
|
||||
: undefined;
|
||||
const strategyRequest =
|
||||
request.type === 'runStrategyReplay'
|
||||
? structuredClone(strategyReplayRequestSchema.parse(request))
|
||||
: undefined;
|
||||
const submitted = strategyRequest ?? request;
|
||||
const submitted = retainedRequest ?? strategyRequest ?? request;
|
||||
if (
|
||||
retainedRequest &&
|
||||
this.workerUrl &&
|
||||
realpathSync(fileURLToPath(this.workerUrl)) !==
|
||||
realpathSync(fileURLToPath(canonicalWorkerUrl))
|
||||
) {
|
||||
throw new Error('Retained strategy execution requires the canonical bundled worker');
|
||||
}
|
||||
const workerUrl = retainedRequest
|
||||
? canonicalWorkerUrl
|
||||
: (this.workerUrl ??
|
||||
new URL(
|
||||
import.meta.url.endsWith('.ts') ? './native-worker.ts' : './native-worker.js',
|
||||
import.meta.url
|
||||
));
|
||||
// Capture expected identity/policy before asynchronous execution. A caller
|
||||
// mutating its request cannot change which completed child result is valid.
|
||||
const expectedReplay =
|
||||
|
|
@ -56,7 +77,7 @@ export class NativeBacktestWorker {
|
|||
format: replayResultFormatSchema.parse(submitted.resultFormat ?? 'trace'),
|
||||
}
|
||||
: undefined;
|
||||
const child = fork(fileURLToPath(this.workerUrl), [], {
|
||||
const child = fork(fileURLToPath(workerUrl), [], {
|
||||
execPath: process.execPath,
|
||||
execArgv: [],
|
||||
serialization: 'json',
|
||||
|
|
@ -94,6 +115,14 @@ export class NativeBacktestWorker {
|
|||
if (message.type === 'error') {
|
||||
terminate(new Error(message.message));
|
||||
} else if (message.type === 'result') {
|
||||
if (retainedRequest) {
|
||||
try {
|
||||
parseRetainedStrategyResult(message.result, retainedRequest.request);
|
||||
} catch (error) {
|
||||
terminate(error instanceof Error ? error : new Error(String(error)));
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (expectedReplay) {
|
||||
try {
|
||||
if (strategyRequest) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue