Bound backtest dataset preparation and preserve replay identity

This commit is contained in:
Boki 2026-10-21 23:18:23 -04:00
parent cca5d71e6e
commit 6bd671d92a
20 changed files with 1420 additions and 155 deletions

View file

@ -1,5 +1,15 @@
import { EventEmitter } from 'node:events';
import { resolveDataset, type DatasetResolver } from '../data/historical-dataset';
import {
datasetLimits,
PREPARATION_BATCH_SIZE,
preparationCheckpoint,
type DatasetLimits,
} from '../data/dataset-limits';
import {
assertNativeDatasetBudget,
resolveDataset,
type DatasetResolver,
} from '../data/historical-dataset';
import { StorageService } from '../services/StorageService';
import { BacktestConfigSchema, type BacktestConfig, type OrchestratorServices } from '../types';
import {
@ -19,6 +29,7 @@ export class RustBacktestAdapter extends EventEmitter implements BacktestExecuti
private readonly storageService: StorageService;
private readonly worker: NativeBacktestWorker;
private readonly timeoutMs: number;
private readonly datasetLimits: Readonly<DatasetLimits>;
private active: { controller: AbortController; finished: Promise<void> } | undefined;
private closing = false;
private progress = 0;
@ -26,9 +37,10 @@ export class RustBacktestAdapter extends EventEmitter implements BacktestExecuti
constructor(
private readonly container: OrchestratorServices,
options: { timeoutMs?: number; workerUrl?: URL } = {}
options: { timeoutMs?: number; workerUrl?: URL; datasetLimits?: Partial<DatasetLimits> } = {}
) {
super();
this.datasetLimits = datasetLimits(options.datasetLimits);
this.timeoutMs = options.timeoutMs ?? 300_000;
if (
!Number.isInteger(this.timeoutMs) ||
@ -116,9 +128,19 @@ export class RustBacktestAdapter extends EventEmitter implements BacktestExecuti
resolver?: DatasetResolver
): Promise<NativeWorkerRequest['data']> {
signal.throwIfAborted();
const dataset = await resolveDataset(this.storageService, config, resolver);
const options = { limits: this.datasetLimits, signal };
const dataset = await resolveDataset(this.storageService, config, resolver, options);
signal.throwIfAborted();
return dataset.bars.map(bar => ({ ...bar, type: 'bar' }));
assertNativeDatasetBudget(dataset, config, options);
const data: NativeWorkerRequest['data'] = [];
for (const bar of dataset.bars) {
data.push({ ...bar, type: 'bar' });
if (data.length % PREPARATION_BATCH_SIZE === 0) {
await preparationCheckpoint(signal);
}
}
signal.throwIfAborted();
return data;
}
private publish(event: string, data: unknown): void {