feat(research): read verified frozen datasets (RP28)

This commit is contained in:
Boki 2026-10-28 00:03:15 -04:00
parent 04892a59fe
commit 4b9c642817
9 changed files with 263 additions and 18 deletions

View file

@ -12,6 +12,7 @@ import { BacktestRunner } from '../../apps/stock/orchestrator/src/backtest/Backt
import { PostgresBacktestRunRepository } from '../../apps/stock/orchestrator/src/backtest/PostgresBacktestRunRepository';
import { RustBacktestAdapter } from '../../apps/stock/orchestrator/src/backtest/RustBacktestAdapter';
import { createBacktestV2Routes } from '../../apps/stock/web-api/src/routes/backtests-v2.routes';
import { createDatasetRoutes } from '../../apps/stock/web-api/src/routes/datasets.routes';
import { createExperimentRoutes } from '../../apps/stock/web-api/src/routes/experiments.routes';
import { createResearchAgentRoutes } from '../../apps/stock/web-api/src/routes/research-agents.routes';
import { createResearchRoutes } from '../../apps/stock/web-api/src/routes/research.routes';
@ -118,6 +119,7 @@ async function fixture() {
app.route('/', createResearchRoutes(container));
app.route('/', createExperimentRoutes(container));
app.route('/', createBacktestV2Routes(container));
app.route('/', createDatasetRoutes(container));
const engine = new RustBacktestAdapter({
logger,
});
@ -258,6 +260,71 @@ async function fixture() {
}
describe.skipIf(!url)('AI research workflow through authenticated APIs and Rust', () => {
test('research credentials export known immutable snapshots with exact read-only scope', async () => {
const f = await fixture();
const hash = f.dataset.manifest.hash;
const totals = async () =>
(
await f.pool.query(`SELECT
(SELECT count(*)::int FROM runs) AS runs,
(SELECT count(*)::int FROM research_experiments) AS experiments,
(SELECT count(*)::int FROM backtest_datasets) AS datasets`)
).rows[0];
const before = await totals();
expect(await f.client.canonicalDataset(hash)).toEqual(f.dataset);
const head = await f.transport(
`http://127.0.0.1:2003/api/v2/datasets/canonical/${hash}/export`,
{
method: 'HEAD',
headers: { Authorization: `Bearer ${f.provisioned.token}` },
}
);
expect(head.status).toBe(200);
expect(head.headers.get('cache-control')).toBe('no-store');
expect(head.headers.get('x-content-type-options')).toBe('nosniff');
expect(head.headers.get('content-disposition')).toBe(
`attachment; filename="dataset-${hash}.json"`
);
expect(await head.text()).toBe('');
for (const [path, method] of [
['/datasets/canonical', 'GET'],
['/datasets/stored-eod/coverage', 'GET'],
['/datasets/canonical/preview', 'POST'],
['/datasets/canonical/retain', 'POST'],
['/datasets/canonical/stored-source', 'POST'],
['/datasets/stored-eod/inspect', 'POST'],
[`/datasets/canonical/${hash}/export`, 'POST'],
]) {
const response = await f.transport(`http://127.0.0.1:2003/api/v2${path}`, {
method,
headers: { Authorization: `Bearer ${f.provisioned.token}` },
});
expect(response.status).toBe(403);
}
await expect(f.client.canonicalDataset('0'.repeat(64))).rejects.toMatchObject({
status: 404,
uncertain: false,
});
expect(await totals()).toEqual(before);
const badHash = 'f'.repeat(64);
await f.pool.query('INSERT INTO backtest_datasets(hash,payload) VALUES($1,$2::jsonb)', [
badHash,
f.dataset,
]);
const afterFixtureInsert = await totals();
await expect(f.client.canonicalDataset(badHash)).rejects.toMatchObject({
status: 409,
uncertain: false,
});
expect(await totals()).toEqual(afterFixtureInsert);
await f.agents.update(f.provisioned.agent.id, { expectedRevision: 1, enabled: false });
await expect(f.client.canonicalDataset(hash)).rejects.toMatchObject({
status: 401,
uncertain: false,
});
expect(f.executionErrors).toEqual([]);
});
test('defaulted candidate validation accepts, reopens and replays the exact original input', async () => {
const f = await fixture();
const plan = f.suggestions();
@ -607,6 +674,34 @@ describe.skipIf(!url)('AI research workflow through authenticated APIs and Rust'
expect((await f.pool.query('SELECT count(*)::int AS count FROM runs')).rows[0].count).toBe(
before
);
const original = await f.client.canonicalRunResult(reference.runId);
const snapshot = await f.client.canonicalDataset(original.request.datasetHash);
expect(snapshot).toEqual(
(
await f.pool.query('SELECT payload FROM backtest_datasets WHERE hash=$1', [
original.request.datasetHash,
])
).rows[0].payload
);
const template = await f.api.getBacktest(original.backtestId);
if (!template) {
throw new Error('Missing original template');
}
await f.api.updateBacktest(original.backtestId, {
startDate: new Date(f.dataset.manifest.bounds.tradeStart).toISOString(),
endDate: new Date(f.dataset.manifest.bounds.tradeEnd).toISOString(),
symbols: f.dataset.manifest.executionSeries.map(series => series.instrumentId),
config: {
...template.config,
datasetHash: f.dataset.manifest.hash,
dataFrequency: f.dataset.manifest.series[0]!.interval,
},
});
expect(await f.client.canonicalDataset(original.request.datasetHash)).toEqual(snapshot);
expect(await f.client.canonicalRunResult(reference.runId)).toEqual(original);
expect((await f.pool.query('SELECT count(*)::int AS count FROM runs')).rows[0].count).toBe(
before
);
await f.agents.update(f.provisioned.agent.id, {
expectedRevision: f.provisioned.agent.revision,
enabled: false,