feat(research): archive backtests and protect retained evidence

This commit is contained in:
Boki 2026-10-23 20:18:58 -04:00
parent 4a2e5ad362
commit 8cca0ee588
25 changed files with 1550 additions and 79 deletions

View file

@ -35,6 +35,7 @@ export interface BacktestRequest {
export interface Backtest {
id: string;
archivedAt: string | null;
name: string;
strategy: string;
symbols: string[];
@ -82,6 +83,19 @@ export class RunSubmissionConflictError extends Error {
}
}
export class BacktestRetentionError extends Error {
constructor(
public readonly code: 'backtest_archived' | 'retained_runs',
public readonly runCount?: number
) {
super(
code === 'backtest_archived'
? 'Backtest is archived; restore it before editing or submitting a new run'
: `Backtest has ${runCount} retained run(s); archive it to preserve results and reproducibility`
);
}
}
export class BacktestServiceV2 {
private container: IServiceContainer & { postgres: NonNullable<IServiceContainer['postgres']> };
private wsHandler: WebSocketHandler | null = null;
@ -160,6 +174,9 @@ export class BacktestServiceV2 {
if (!locked.rows[0]) {
return null;
}
if (locked.rows[0].archived_at) {
throw new BacktestRetentionError('backtest_archived');
}
const existing = this.mapBacktest(locked.rows[0]);
const next = { ...existing, ...request };
await assertSelectedDatasetMatchesRequest(client, backtestRequestFromDefinition(next));
@ -217,19 +234,54 @@ export class BacktestServiceV2 {
});
}
async listBacktests(params: { limit: number; offset: number }): Promise<Backtest[]> {
async listBacktests(params: {
limit: number;
offset: number;
includeArchived?: boolean;
}): Promise<Backtest[]> {
const result = await this.container.postgres.query(
`SELECT * FROM backtests
WHERE ($3::boolean OR archived_at IS NULL)
ORDER BY created_at DESC
LIMIT $1 OFFSET $2`,
[params.limit, params.offset]
[params.limit, params.offset, params.includeArchived === true]
);
return result.rows.map(row => this.mapBacktest(row));
}
async deleteBacktest(id: string): Promise<void> {
await this.container.postgres.query('DELETE FROM backtests WHERE id = $1', [id]);
await this.container.postgres.transaction(async client => {
const parent = await client.query('SELECT id FROM backtests WHERE id=$1 FOR UPDATE', [id]);
if (!parent.rows[0]) {
return;
}
const retained = await client.query(
'SELECT count(*)::int AS count FROM runs WHERE backtest_id=$1',
[id]
);
const count = Number(retained.rows[0]?.count ?? 0);
if (count > 0) {
throw new BacktestRetentionError('retained_runs', count);
}
await client.query('DELETE FROM backtests WHERE id=$1', [id]);
});
}
async archiveBacktest(id: string, archived: boolean): Promise<Backtest | null> {
return this.container.postgres.transaction(async client => {
const parent = await client.query('SELECT id FROM backtests WHERE id=$1 FOR UPDATE', [id]);
if (!parent.rows[0]) {
return null;
}
const updated = await client.query(
`UPDATE backtests
SET archived_at=CASE WHEN $2::boolean THEN COALESCE(archived_at,NOW()) ELSE NULL END,
updated_at=NOW() WHERE id=$1 RETURNING *`,
[id, archived]
);
return this.mapBacktest(at(updated.rows, 0));
});
}
// Run operations
@ -281,6 +333,9 @@ export class BacktestServiceV2 {
if (concurrent) {
return concurrent;
}
if (at(parent.rows, 0).archived_at) {
throw new BacktestRetentionError('backtest_archived');
}
const definition = this.mapBacktest(at(parent.rows, 0));
const snapshot = backtestRequestFromDefinition(definition, {
runId,
@ -434,6 +489,7 @@ export class BacktestServiceV2 {
private mapBacktest(row: QueryResultRow): Backtest {
return {
id: row.id,
archivedAt: row.archived_at ? new Date(row.archived_at).toISOString() : null,
name: row.name,
strategy: row.strategy,
symbols: row.symbols,