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

@ -80,6 +80,7 @@ async function fixture() {
'10-durable-backtest-dispatch.sql',
'12-historical-datasets.sql',
'14-backtest-submission.sql',
'17-research-retention.sql',
]) {
await pool.query(
readFileSync(new URL(`../../database/postgres/init/${filename}`, import.meta.url), 'utf8')
@ -558,12 +559,46 @@ describe.skipIf(!url || !redisUrl)('durable backtests on real PostgreSQL and Red
const config = await f.snapshot(run.id);
const claim = await f.repository.start(config);
const dataset = await loadDataset(f.storage, config);
await f.repository.freezeDataset(run.id, claim.attemptId, dataset);
await f.pool.query(
"UPDATE backtest_datasets SET payload = jsonb_set(payload, '{bars,0,volume}', '0'::jsonb) WHERE hash = $1",
[dataset.manifest.hash]
);
// Model corrupt historical storage at initial insertion; retained evidence cannot be edited.
const corrupt = structuredClone(dataset);
corrupt.bars[0]!.volume = 0;
await f.pool.query('INSERT INTO backtest_datasets(hash,payload) VALUES ($1,$2)', [
dataset.manifest.hash,
corrupt,
]);
await f.pool.query('UPDATE runs SET dataset_hash=$2 WHERE id=$1', [
run.id,
dataset.manifest.hash,
]);
const reads = f.history.reads;
f.history.unavailable = true;
await expect(f.repository.loadDataset(run.id, claim.attemptId)).rejects.toThrow('hash/content');
await f.expire(run.id);
await f.dispatcher.tick();
await expect(f.runner.runQueued(run.id)).rejects.toThrow('hash/content');
expect((await f.api.getRun(run.id))?.status).toBe('failed');
expect(await f.api.getRunResults(run.id)).toBeNull();
expect(f.history.reads).toBe(reads);
});
test('a missing retained dataset fails a historical request without provider fallback', async () => {
const f = await fixture();
const runId = randomUUID();
const snapshot = backtestRequestFromDefinition(
{ ...f.definition, config: { ...f.definition.config, datasetHash: 'f'.repeat(64) } },
{ runId, backtestId: f.definition.id }
);
// Imported history can reference unavailable storage; retain its request and report the loss.
await f.pool.query(
'INSERT INTO runs(id,backtest_id,run_number,request_snapshot) VALUES ($1,$2,1,$3)',
[runId, f.definition.id, snapshot]
);
await f.pool.query('INSERT INTO backtest_dispatch(run_id) VALUES ($1)', [runId]);
f.history.unavailable = true;
await expect(f.runner.runQueued(runId)).rejects.toThrow('Frozen dataset not found');
expect(f.history.reads).toBe(0);
expect((await f.api.getRun(runId))?.status).toBe('failed');
expect(await f.api.getRunResults(runId)).toBeNull();
});
test('an explicit dataset replay rejects a different listing or date range', async () => {
@ -880,8 +915,13 @@ describe.skipIf(!url || !redisUrl)('durable backtests on real PostgreSQL and Red
test('invalid persisted snapshots become failed instead of remaining stranded pending', async () => {
const f = await fixture();
const run = await f.api.createRun({ backtestId: f.definition.id });
await f.pool.query("UPDATE runs SET request_snapshot = '{}' WHERE id = $1", [run.id]);
const run = { id: randomUUID() };
// A legacy malformed snapshot is inserted as found, not fabricated by editing retained evidence.
await f.pool.query(
"INSERT INTO runs(id,backtest_id,run_number,request_snapshot) VALUES ($1,$2,1,'{}')",
[run.id, f.definition.id]
);
await f.pool.query('INSERT INTO backtest_dispatch(run_id) VALUES ($1)', [run.id]);
await expect(f.runner.runQueued(run.id)).rejects.toThrow();
expect((await f.api.getRun(run.id))?.status).toBe('failed');
expect((await f.api.getRun(run.id))?.attemptNumber).toBe(0);
@ -889,12 +929,14 @@ describe.skipIf(!url || !redisUrl)('durable backtests on real PostgreSQL and Red
test('migration replay preserves leased owners, backfills legacy pending snapshots, and fails ownerless legacy runs', async () => {
const f = await fixture();
const pending = await f.api.createRun({ backtestId: f.definition.id });
const pending = { id: randomUUID() };
await f.pool.query('INSERT INTO runs(id,backtest_id,run_number) VALUES ($1,$2,1)', [
pending.id,
f.definition.id,
]);
const active = await f.api.createRun({ backtestId: f.definition.id });
const oldActive = await f.api.createRun({ backtestId: f.definition.id });
const claim = await f.repository.start(await f.snapshot(active.id));
await f.pool.query('DELETE FROM backtest_dispatch WHERE run_id = $1', [pending.id]);
await f.pool.query('UPDATE runs SET request_snapshot = NULL WHERE id = $1', [pending.id]);
await f.pool.query("UPDATE runs SET status = 'running' WHERE id = $1", [oldActive.id]);
const migration = readFileSync(
new URL('../../database/postgres/init/10-durable-backtest-dispatch.sql', import.meta.url),