Remove dataset input caps and simplify preparation
This commit is contained in:
parent
6bd671d92a
commit
007e35c747
17 changed files with 175 additions and 620 deletions
|
|
@ -2,7 +2,6 @@ import { randomUUID } from 'node:crypto';
|
|||
import { EventEmitter } from 'node:events';
|
||||
import { defined } from '@stock-bot/invariants';
|
||||
import { DataManager } from '../data/DataManager';
|
||||
import { datasetLimits, type DatasetLimits } from '../data/dataset-limits';
|
||||
import { resolveDataset, type DatasetResolver } from '../data/historical-dataset';
|
||||
import { amount, decimal } from '../execution/model';
|
||||
import type { StorageService } from '../services/StorageService';
|
||||
|
|
@ -25,16 +24,13 @@ export class BacktestEngine extends EventEmitter implements BacktestExecutionEng
|
|||
private active?: Promise<BacktestResult>;
|
||||
private completedReport?: BacktestReport;
|
||||
private closing = false;
|
||||
private readonly datasetLimits: Readonly<DatasetLimits>;
|
||||
|
||||
constructor(
|
||||
private readonly container: OrchestratorServices,
|
||||
private readonly storageService: StorageService,
|
||||
private readonly strategyManager: StrategyManager,
|
||||
options: { datasetLimits?: Partial<DatasetLimits> } = {}
|
||||
private readonly strategyManager: StrategyManager
|
||||
) {
|
||||
super();
|
||||
this.datasetLimits = datasetLimits(options.datasetLimits);
|
||||
}
|
||||
|
||||
async runBacktest(input: unknown, resolver?: DatasetResolver): Promise<BacktestResult> {
|
||||
|
|
@ -65,10 +61,7 @@ export class BacktestEngine extends EventEmitter implements BacktestExecutionEng
|
|||
const plan = backtestStrategies(config, 'typescript');
|
||||
this.container.logger.info('Starting TypeScript backtest', { symbols: config.symbols });
|
||||
const dataset = await playback.read(() =>
|
||||
resolveDataset(this.storageService, config, resolver, {
|
||||
limits: this.datasetLimits,
|
||||
signal: playback.signal,
|
||||
})
|
||||
resolveDataset(this.storageService, config, resolver, playback.signal)
|
||||
);
|
||||
playback.assertRunning();
|
||||
const dataManager = new DataManager(this.storageService);
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ import { z } from 'zod';
|
|||
import type { Logger } from '@stock-bot/services';
|
||||
import type { BacktestRunResult, HistoricalDatasetManifest } from '@stock-bot/types';
|
||||
import { validateHistoricalDataRequest } from '@stock-bot/types';
|
||||
import { datasetLimits, type DatasetLimits } from '../data/dataset-limits';
|
||||
import { dataPolicy, prepareDataset } from '../data/historical-dataset';
|
||||
import { BacktestConfigSchema, type BacktestConfig } from '../types';
|
||||
import { BacktestCancelledError, type BacktestExecutionEngine } from './execution-engine';
|
||||
|
|
@ -37,7 +36,6 @@ export class BacktestRunner extends EventEmitter {
|
|||
private closing = false;
|
||||
private phase: 'preparing' | 'executing' | 'persisting' = 'preparing';
|
||||
private execution: Promise<BacktestRunResult> | undefined;
|
||||
private readonly datasetLimits: Readonly<DatasetLimits>;
|
||||
|
||||
constructor(
|
||||
private readonly dependencies: {
|
||||
|
|
@ -47,12 +45,10 @@ export class BacktestRunner extends EventEmitter {
|
|||
logger: Pick<Logger, 'error'>;
|
||||
heartbeatMs?: number;
|
||||
heartbeatTimeoutMs?: number;
|
||||
datasetLimits?: Partial<DatasetLimits>;
|
||||
prepare?: (config: BacktestConfig) => Promise<void>;
|
||||
}
|
||||
) {
|
||||
super();
|
||||
this.datasetLimits = datasetLimits(dependencies.datasetLimits);
|
||||
}
|
||||
|
||||
async runBacktest(input: unknown): Promise<BacktestRunResult> {
|
||||
|
|
@ -213,10 +209,7 @@ export class BacktestRunner extends EventEmitter {
|
|||
executionConfig.datasetHash,
|
||||
signal
|
||||
);
|
||||
const dataset = await prepareDataset(saved ?? (await load()), executionConfig, {
|
||||
limits: this.datasetLimits,
|
||||
signal,
|
||||
});
|
||||
const dataset = await prepareDataset(saved ?? (await load()), executionConfig, signal);
|
||||
signal?.throwIfAborted();
|
||||
const frozen =
|
||||
saved ??
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ import type { PoolClient } from 'pg';
|
|||
import type { TransactionCallback } from '@stock-bot/postgres';
|
||||
import type { PostgresClient } from '@stock-bot/services';
|
||||
import type { BacktestRunResult, HistoricalDataset } from '@stock-bot/types';
|
||||
import { assertDatasetLimit, datasetLimits, type DatasetLimits } from '../data/dataset-limits';
|
||||
import { prepareDataset } from '../data/historical-dataset';
|
||||
import { BacktestConfigSchema, type BacktestConfig } from '../types';
|
||||
import { BacktestCancelledError } from './execution-engine';
|
||||
|
|
@ -16,13 +15,10 @@ import {
|
|||
|
||||
/** The attempt UUID is a fencing token. PostgreSQL time, not worker clocks, defines ownership. */
|
||||
export class PostgresBacktestRunRepository implements BacktestRunRepository {
|
||||
private readonly datasetLimits: Readonly<DatasetLimits>;
|
||||
constructor(
|
||||
private readonly postgres: Pick<PostgresClient, 'transaction'>,
|
||||
private readonly leaseMs = 30_000,
|
||||
limits?: Partial<DatasetLimits>
|
||||
private readonly leaseMs = 30_000
|
||||
) {
|
||||
this.datasetLimits = datasetLimits(limits);
|
||||
if (!Number.isInteger(leaseMs) || leaseMs < 100) {
|
||||
throw new Error('Invalid execution lease duration');
|
||||
}
|
||||
|
|
@ -177,7 +173,7 @@ export class PostgresBacktestRunRepository implements BacktestRunRepository {
|
|||
input: HistoricalDataset,
|
||||
signal?: AbortSignal
|
||||
): Promise<HistoricalDataset> {
|
||||
const dataset = await prepareDataset(input, undefined, { limits: this.datasetLimits, signal });
|
||||
const dataset = await prepareDataset(input, undefined, signal);
|
||||
return this.ownedTransaction(runId, async client => {
|
||||
const owned = await client.query(
|
||||
`SELECT dataset_hash FROM runs WHERE id = $1 AND attempt_id = $2
|
||||
|
|
@ -218,21 +214,14 @@ export class PostgresBacktestRunRepository implements BacktestRunRepository {
|
|||
signal?: AbortSignal
|
||||
): Promise<HistoricalDataset> {
|
||||
signal?.throwIfAborted();
|
||||
// Never transfer/parse an oversized JSONB document into the application process.
|
||||
const saved = await client.query(
|
||||
`SELECT CASE WHEN octet_length(payload::text) <= $2 THEN payload ELSE NULL END AS payload,
|
||||
octet_length(payload::text) AS payload_bytes FROM backtest_datasets WHERE hash = $1`,
|
||||
[hash, this.datasetLimits.maxStoredBytes]
|
||||
);
|
||||
const saved = await client.query('SELECT payload FROM backtest_datasets WHERE hash = $1', [
|
||||
hash,
|
||||
]);
|
||||
const row = saved.rows[0];
|
||||
if (!row) {
|
||||
throw new Error('Frozen dataset not found');
|
||||
}
|
||||
assertDatasetLimit(this.datasetLimits, 'maxStoredBytes', Number(row.payload_bytes));
|
||||
const dataset = await prepareDataset(row.payload, undefined, {
|
||||
limits: this.datasetLimits,
|
||||
signal,
|
||||
});
|
||||
const dataset = await prepareDataset(row.payload, undefined, signal);
|
||||
if (dataset.manifest.hash !== hash) {
|
||||
throw new Error('Stored dataset identity mismatch');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,15 +1,6 @@
|
|||
import { EventEmitter } from 'node:events';
|
||||
import {
|
||||
datasetLimits,
|
||||
PREPARATION_BATCH_SIZE,
|
||||
preparationCheckpoint,
|
||||
type DatasetLimits,
|
||||
} from '../data/dataset-limits';
|
||||
import {
|
||||
assertNativeDatasetBudget,
|
||||
resolveDataset,
|
||||
type DatasetResolver,
|
||||
} from '../data/historical-dataset';
|
||||
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 {
|
||||
|
|
@ -29,7 +20,6 @@ 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;
|
||||
|
|
@ -37,10 +27,9 @@ export class RustBacktestAdapter extends EventEmitter implements BacktestExecuti
|
|||
|
||||
constructor(
|
||||
private readonly container: OrchestratorServices,
|
||||
options: { timeoutMs?: number; workerUrl?: URL; datasetLimits?: Partial<DatasetLimits> } = {}
|
||||
options: { timeoutMs?: number; workerUrl?: URL } = {}
|
||||
) {
|
||||
super();
|
||||
this.datasetLimits = datasetLimits(options.datasetLimits);
|
||||
this.timeoutMs = options.timeoutMs ?? 300_000;
|
||||
if (
|
||||
!Number.isInteger(this.timeoutMs) ||
|
||||
|
|
@ -128,10 +117,8 @@ export class RustBacktestAdapter extends EventEmitter implements BacktestExecuti
|
|||
resolver?: DatasetResolver
|
||||
): Promise<NativeWorkerRequest['data']> {
|
||||
signal.throwIfAborted();
|
||||
const options = { limits: this.datasetLimits, signal };
|
||||
const dataset = await resolveDataset(this.storageService, config, resolver, options);
|
||||
const dataset = await resolveDataset(this.storageService, config, resolver, signal);
|
||||
signal.throwIfAborted();
|
||||
assertNativeDatasetBudget(dataset, config, options);
|
||||
const data: NativeWorkerRequest['data'] = [];
|
||||
for (const bar of dataset.bars) {
|
||||
data.push({ ...bar, type: 'bar' });
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue