feat(research): reproduce retained canonical runs with economic comparison

This commit is contained in:
Boki 2026-10-24 10:02:21 -04:00
parent e3bd708838
commit 30accfcf43
31 changed files with 2969 additions and 28 deletions

View file

@ -292,6 +292,7 @@ export async function fixture(browser: Browser, native = false) {
'18-backtest-strategy-versions.sql',
'19-canonical-research.sql',
'20-research-workspace.sql',
'21-run-reproduction.sql',
]) {
await pool.query(
readFileSync(new URL(`../../database/postgres/init/${name}`, import.meta.url), 'utf8')

View file

@ -1049,6 +1049,114 @@ test('research_project_to_finding_browser_with_canonical_strategy_setup_run_and_
await f.page.waitForURL(completedUrl);
await visible(f.page, '9969.30000000');
expect(await f.request(`/api/v2/runs/${run.id}/results`)).toEqual(result);
// Original reproduction is independent of subsequent mutable template edits.
await f.request(`/api/v2/backtests/${backtestId}`, 'PUT', {
name: 'Changed current settings',
initialCapital: 20000,
strategyParameters: { fastPeriod: 1, slowPeriod: 2 },
config: { ...sent.config, canonical: { ...sent.config.canonical, feeRate: '0.02', seed: 7 } },
});
await f.page.reload();
await visible(f.page, 'Canonical research result');
await f.page.getByRole('button', { name: 'Reproduce original', exact: true }).click();
const reproductionDialog = f.page.getByRole('dialog', {
name: 'Reproduce original',
exact: true,
});
const originalInputs = reproductionDialog.getByRole('region', {
name: 'Original retained inputs',
exact: true,
});
await originalInputs.waitFor({ state: 'visible' });
expect(await originalInputs.textContent()).toContain(retained.hash);
expect(await originalInputs.textContent()).toContain(selected.id);
await reproductionDialog
.getByText('Exact parameters, financial policies and build', { exact: true })
.click();
expect(await originalInputs.locator('pre').textContent()).toContain(
'"initialCapital": "10000.00000000"'
);
expect(await originalInputs.locator('pre').textContent()).toContain('"fastPeriod": 2');
const reproducing = f.page.waitForResponse(
response =>
response.request().method() === 'POST' &&
response.url().endsWith(`/runs/${run.id}/reproduce`)
);
await reproductionDialog
.getByRole('button', { name: 'Confirm reproduction', exact: true })
.click();
const reproducedResponse = await reproducing;
expect(reproducedResponse.status()).toBe(202);
expect(reproducedResponse.request().headers()['idempotency-key']).toBeTruthy();
expect(reproducedResponse.request().postDataJSON()).toEqual({});
const reproducedRun = await reproducedResponse.json();
expect(reproducedRun.originalRunId).toBe(run.id);
expect(reproducedRun.id).not.toBe(run.id);
expect(reproducedResponse.headers().location).toBe(`/api/v2/runs/${reproducedRun.id}`);
await f.page.waitForURL(`${f.origin}/backtests/${backtestId}/run/${reproducedRun.id}`);
const duplicate = await f.page.evaluate(
async ({ sourceId, key }) => {
const response = await fetch(`/api/v2/runs/${sourceId}/reproduce`, {
method: 'POST',
credentials: 'include',
headers: { 'Content-Type': 'application/json', 'Idempotency-Key': key },
body: '{}',
});
return { status: response.status, run: await response.json() };
},
{ sourceId: run.id, key: reproducedResponse.request().headers()['idempotency-key']! }
);
expect(duplicate.status).toBe(202);
expect(duplicate.run.id).toBe(reproducedRun.id);
expect((await f.pool.query('SELECT * FROM runs')).rowCount).toBe(2);
await visible(f.page, 'Economic results match the original.');
const comparison = await f.request(`/api/v2/runs/${reproducedRun.id}/reproduction-comparison`);
expect(comparison.status).toBe('match');
expect(comparison.originalHash).toMatch(/^[a-f0-9]{64}$/);
expect(comparison.reproducedHash).toBe(comparison.originalHash);
const reproducedResult = (await f.request(
`/api/v2/runs/${reproducedRun.id}/results`
)) as CanonicalRunResult;
expect(reproducedResult.request.strategy.selection).toEqual(result.request.strategy.selection);
expect(reproducedResult.request.build).toEqual(result.request.build);
expect(reproducedResult.request.datasetHash).toBe(result.request.datasetHash);
expect(String(reproducedResult.request.policy.matching.risk.ledger.initialCapital)).toBe(
'10000.00000000'
);
expect(reproducedResult.financial.projection).toEqual(result.financial.projection);
expect(reproducedResult.analytics).toEqual(result.analytics);
await f.page.reload();
await visible(f.page, 'Economic results match the original.');
await f.page.getByRole('link', { name: 'Open original run', exact: true }).click();
await f.page.waitForURL(completedUrl);
await visible(f.page, '9969.30000000');
// Ordinary submission still freezes the current settings and has no reproduction lineage.
await f.page.getByRole('button', { name: 'Runs', exact: true }).click();
const currentResponse = await submitInBrowser(f.page);
expect(currentResponse.status()).toBe(202);
const currentRun = await currentResponse.json();
expect(currentRun.originalRunId).toBeNull();
await visible(f.page, 'Canonical research result');
const currentResult = (await f.request(
`/api/v2/runs/${currentRun.id}/results`
)) as CanonicalRunResult;
expect(String(currentResult.request.policy.matching.risk.ledger.initialCapital)).toBe(
'20000.00000000'
);
expect(currentResult.request.strategy.selection.parameters).toEqual({
fastPeriod: 1,
slowPeriod: 2,
});
expect(currentResult.request.strategy.selection.seed).toBe(7);
expect(String(currentResult.request.policy.matching.risk.feeRate)).toBe('0.02000000');
expect(await f.request(`/api/v2/runs/${run.id}/results`)).toEqual(result);
expect(
(await f.pool.query('SELECT original_run_id FROM runs WHERE id=$1', [reproducedRun.id]))
.rows[0].original_run_id
).toBe(run.id);
expect((await f.pool.query('SELECT * FROM backtest_results')).rowCount).toBe(3);
expect(f.historicalDataReads).toEqual([]);
expect(f.unexpectedNetwork).toEqual([]);
} finally {