Share backtest request contracts and isolate stored result mapping

This commit is contained in:
Boki 2026-10-21 23:57:48 -04:00
parent 007e35c747
commit 420c47704e
21 changed files with 777 additions and 244 deletions

View file

@ -1,12 +1,13 @@
import { randomUUID } from 'node:crypto';
import { EventEmitter } from 'node:events';
import { defined } from '@stock-bot/invariants';
import { parseBacktestRequest } from '@stock-bot/stock-config/backtest';
import { DataManager } from '../data/DataManager';
import { resolveDataset, type DatasetResolver } from '../data/historical-dataset';
import { amount, decimal } from '../execution/model';
import type { StorageService } from '../services/StorageService';
import type { StrategyManager } from '../strategies/StrategyManager';
import { BacktestConfigSchema, type BacktestConfig, type OrchestratorServices } from '../types';
import type { BacktestConfig, OrchestratorServices } from '../types';
import { BacktestMarket } from './BacktestMarket';
import { BacktestPlayback, type PlaybackProgress } from './BacktestPlayback';
import { BacktestReport } from './BacktestReport';
@ -39,7 +40,7 @@ export class BacktestEngine extends EventEmitter implements BacktestExecutionEng
this.closing ? 'Backtest engine is shutting down' : 'Backtest already running'
);
}
const config = BacktestConfigSchema.parse(input);
const config = parseBacktestRequest(input);
const playback = new BacktestPlayback(Date.parse(config.startDate), speeds[config.speed]);
this.playback = playback;
this.completedReport = undefined;

View file

@ -1,10 +1,9 @@
import { EventEmitter } from 'node:events';
import { z } from 'zod';
import type { Logger } from '@stock-bot/services';
import { parseBacktestRequest } from '@stock-bot/stock-config/backtest';
import type { BacktestRunResult, HistoricalDatasetManifest } from '@stock-bot/types';
import { validateHistoricalDataRequest } from '@stock-bot/types';
import { dataPolicy, prepareDataset } from '../data/historical-dataset';
import { BacktestConfigSchema, type BacktestConfig } from '../types';
import { prepareDataset } from '../data/historical-dataset';
import type { BacktestConfig } from '../types';
import { BacktestCancelledError, type BacktestExecutionEngine } from './execution-engine';
import { normalizeBacktestResult } from './result';
import {
@ -14,17 +13,7 @@ import {
type RunClaim,
} from './run-repository';
export function parseBacktestRequest(input: unknown): BacktestConfig {
const config = BacktestConfigSchema.parse(input);
validateHistoricalDataRequest(config);
dataPolicy(config);
if (new Date(config.endDate) <= new Date(config.startDate)) {
throw new z.ZodError([
{ code: 'custom', path: ['endDate'], message: 'Backtest endDate must be after startDate' },
]);
}
return config;
}
export { parseBacktestRequest };
/** One application workflow for HTTP, jobs, and both execution backends. */
export class BacktestRunner extends EventEmitter {

View file

@ -2,9 +2,10 @@ import { randomUUID } from 'node:crypto';
import type { PoolClient } from 'pg';
import type { TransactionCallback } from '@stock-bot/postgres';
import type { PostgresClient } from '@stock-bot/services';
import { parseBacktestRequest } from '@stock-bot/stock-config/backtest';
import type { BacktestRunResult, HistoricalDataset } from '@stock-bot/types';
import { prepareDataset } from '../data/historical-dataset';
import { BacktestConfigSchema, type BacktestConfig } from '../types';
import type { BacktestConfig } from '../types';
import { BacktestCancelledError } from './execution-engine';
import {
BacktestConflictError,
@ -54,7 +55,7 @@ export class PostgresBacktestRunRepository implements BacktestRunRepository {
if (!run) {
throw new BacktestConflictError('Run is missing, already claimed, or terminal');
}
const snapshot = BacktestConfigSchema.parse(run.request_snapshot);
const snapshot = parseBacktestRequest(run.request_snapshot);
return {
runId: String(run.id),
backtestId: String(run.backtest_id),

View file

@ -1,8 +1,9 @@
import { EventEmitter } from 'node:events';
import { parseBacktestRequest } from '@stock-bot/stock-config/backtest';
import { resolveDataset, type DatasetResolver } from '../data/historical-dataset';
import { PREPARATION_BATCH_SIZE, preparationCheckpoint } from '../data/preparation';
import { StorageService } from '../services/StorageService';
import { BacktestConfigSchema, type BacktestConfig, type OrchestratorServices } from '../types';
import type { BacktestConfig, OrchestratorServices } from '../types';
import {
BacktestCancelledError,
BacktestTimeoutError,
@ -58,7 +59,7 @@ export class RustBacktestAdapter extends EventEmitter implements BacktestExecuti
this.closing ? 'Native backtest engine is shutting down' : 'Backtest already running'
);
}
const config = BacktestConfigSchema.parse(input);
const config = parseBacktestRequest(input);
const controller = new AbortController();
let finished!: () => void;
this.active = {