feat(research): cut over canonical runtime and enforce agent budgets

This commit is contained in:
Boki 2026-10-24 16:47:19 -04:00
parent ab08db82fd
commit bbf0dd4453
103 changed files with 6138 additions and 3112 deletions

View file

@ -12,6 +12,7 @@ import { loadDataset } from '../../apps/stock/orchestrator/src/data/historical-d
import { createBacktestV2Routes } from '../../apps/stock/web-api/src/routes/backtests-v2.routes';
import { BacktestServiceV2 } from '../../apps/stock/web-api/src/services/backtest-v2.service';
import { BacktestDispatcher } from '../../apps/stock/web-api/src/services/BacktestDispatcher';
import { seedHistoricalRun } from '../fixtures/historical-run';
setLoggerConfig({ logLevel: 'fatal', logConsole: false });
const logger = getLogger('research-retention-test');
@ -172,7 +173,9 @@ async function waitForBlocked(pool: Pool, schema: string) {
describe.skipIf(!url)('research evidence retention on real PostgreSQL', () => {
test('retained_run_delete_restricted_postgres blocks raw cascades, mutations and truncation while preserving runless legacy deletion', async () => {
const f = await fixture();
const run = await f.api.createRun(
const run = await seedHistoricalRun(
f.pool,
f.api,
{ backtestId: f.definition.id },
{ principal: 'operator', key: 'retained' }
);
@ -217,7 +220,7 @@ describe.skipIf(!url)('research evidence retention on real PostgreSQL', () => {
test('archive_preserves_replay hides discovery only and rejects new admission or edits until restored', async () => {
const f = await fixture();
const receipt = { principal: 'operator' as const, key: 'archive-retry' };
const run = await f.api.createRun({ backtestId: f.definition.id }, receipt);
const run = await seedHistoricalRun(f.pool, f.api, { backtestId: f.definition.id }, receipt);
const before = await f.snapshot(run.id);
const archived = await f.api.archiveBacktest(f.definition.id, true);
expect(archived?.archivedAt).toBeString();
@ -247,12 +250,15 @@ describe.skipIf(!url)('research evidence retention on real PostgreSQL', () => {
expect((await f.api.updateBacktest(f.definition.id, { name: 'Restored' }))?.name).toBe(
'Restored'
);
expect((await f.api.createRun({ backtestId: f.definition.id })).runNumber).toBe(2);
await expect(f.api.createRun({ backtestId: f.definition.id })).rejects.toThrow(
'Legacy backtest execution is retired'
);
expect(await f.api.listRuns(f.definition.id)).toHaveLength(1);
});
test('archive and admission use one parent lock, including direct repository inserts', async () => {
const f = await fixture();
const accepted = await f.api.createRun({ backtestId: f.definition.id });
const accepted = await seedHistoricalRun(f.pool, f.api, { backtestId: f.definition.id });
const blocker = await f.pool.connect();
await blocker.query('BEGIN');
await blocker.query('SELECT id FROM backtests WHERE id=$1 FOR UPDATE', [f.definition.id]);
@ -279,7 +285,7 @@ describe.skipIf(!url)('research evidence retention on real PostgreSQL', () => {
test('accepted archived work retains binding, lease recovery, controls, progress and terminal result writes', async () => {
const f = await fixture();
const run = await f.api.createRun({ backtestId: f.definition.id });
const run = await seedHistoricalRun(f.pool, f.api, { backtestId: f.definition.id });
await f.api.archiveBacktest(f.definition.id, true);
const claim = await f.repository.start(await f.snapshot(run.id));
const frozen = await f.repository.freezeDataset(
@ -319,7 +325,10 @@ describe.skipIf(!url)('research evidence retention on real PostgreSQL', () => {
await f.api.updateRunProgress(run.id, { progress: 90, attemptId: retry.attemptId });
await f.repository.complete(result(run.id, f.definition.id, frozen), retry.attemptId);
const saved = await f.api.getRunResults(run.id);
expect(saved?.config.dataset?.hash).toBe(frozen.manifest.hash);
if (!saved || !('config' in saved)) {
throw new Error('Expected preserved historical result');
}
expect(saved.config.dataset?.hash).toBe(frozen.manifest.hash);
expect((await f.api.getRun(run.id))?.attemptNumber).toBe(2);
expect((await f.api.getRun(run.id))?.status).toBe('completed');
await f.pool.query(retention);
@ -329,7 +338,9 @@ describe.skipIf(!url)('research evidence retention on real PostgreSQL', () => {
test('run identity and frozen inputs reject rewrites while legacy NULL snapshots can be populated once', async () => {
const f = await fixture();
const run = await f.api.createRun(
const run = await seedHistoricalRun(
f.pool,
f.api,
{ backtestId: f.definition.id },
{ principal: 'service', key: 'immutable' }
);
@ -381,7 +392,9 @@ describe.skipIf(!url)('research evidence retention on real PostgreSQL', () => {
test('pending and running cancellation/failure still terminate accepted archived requests', async () => {
const f = await fixture();
const runs = await Promise.all(
Array.from({ length: 4 }, () => f.api.createRun({ backtestId: f.definition.id }))
Array.from({ length: 4 }, () =>
seedHistoricalRun(f.pool, f.api, { backtestId: f.definition.id })
)
);
await f.api.archiveBacktest(f.definition.id, true);
await f.api.cancelRun(runs[0]!.id);
@ -405,7 +418,7 @@ describe.skipIf(!url)('research evidence retention on real PostgreSQL', () => {
test('preexisting corrupted datasets remain immutable and fail checksum verification on retrieval', async () => {
const f = await fixture();
const run = await f.api.createRun({ backtestId: f.definition.id });
const run = await seedHistoricalRun(f.pool, f.api, { backtestId: f.definition.id });
const claim = await f.repository.start(await f.snapshot(run.id));
const dataset = await f.dataset(run.id);
const corrupt = structuredClone(dataset);
@ -430,7 +443,7 @@ describe.skipIf(!url)('research evidence retention on real PostgreSQL', () => {
test('HTTP archive/list/delete errors are actionable and direct saved resources remain accessible', async () => {
const f = await fixture();
const run = await f.api.createRun({ backtestId: f.definition.id });
const run = await seedHistoricalRun(f.pool, f.api, { backtestId: f.definition.id });
const archived = await f.app.request(`/api/v2/backtests/${f.definition.id}/archive`, {
method: 'POST',
body: JSON.stringify({ archived: true }),