Remove dataset input caps and simplify preparation

This commit is contained in:
Boki 2026-10-21 23:34:10 -04:00
parent 6bd671d92a
commit 007e35c747
17 changed files with 175 additions and 620 deletions

View file

@ -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');
}