feat(research): inspect complete canonical run results (RP27)

This commit is contained in:
Boki 2026-10-27 23:29:39 -04:00
parent 65c6594879
commit 66af201dad
9 changed files with 269 additions and 20 deletions

View file

@ -11,6 +11,7 @@ import type { StrategyVersionPackage } from '@stock-bot/types';
import { BacktestRunner } from '../../apps/stock/orchestrator/src/backtest/BacktestRunner';
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 { 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';
@ -116,6 +117,7 @@ async function fixture() {
app.route('/', createStrategyRoutes(container));
app.route('/', createResearchRoutes(container));
app.route('/', createExperimentRoutes(container));
app.route('/', createBacktestV2Routes(container));
const engine = new RustBacktestAdapter({
logger,
});
@ -406,11 +408,16 @@ describe.skipIf(!url)('AI research workflow through authenticated APIs and Rust'
reopenedFinding.latestRevision,
]);
for (const evidence of finding.latestRevision.evidence) {
const saved = await f.api.getRunResults(evidence.runId);
const saved = await f.client.canonicalRunResult(evidence.runId);
if (!saved || !('kind' in saved) || saved.kind !== 'canonical-result/v1') {
throw new Error('Missing native result');
}
expect(saved.strategyExecution).toBe('typescript-isolated-candidate/v1');
const retained = await f.api.getRunResults(evidence.runId);
if (!retained || !('kind' in retained) || retained.kind !== 'canonical-result/v1') {
throw new Error('Missing retained canonical result');
}
expect(saved).toEqual(retained);
expect(saved.request.strategy.versionId).toBe(outcome.versionId);
expect(saved.request.strategy.package.artifact.sha256).toHaveLength(64);
expect(evidence.datasetHash).toBe(saved.request.datasetHash);
@ -436,6 +443,13 @@ describe.skipIf(!url)('AI research workflow through authenticated APIs and Rust'
).toBeTrue();
expect(broken.findings).toEqual([]);
expect(broken.stoppedReason).toContain('failed');
await expect(
f.client.canonicalRunResult(broken.screening!.trials[0]!.runId!)
).rejects.toMatchObject({ status: 404, uncertain: false });
await expect(f.client.canonicalRunResult(randomUUID())).rejects.toMatchObject({
status: 404,
uncertain: false,
});
expect(result.comparison?.rows).toHaveLength(4);
expect(result.comparison?.selection.leftId).toBe(result.outcomes[0]!.costs!.id);
expect(result.comparison?.selection.rightId).toBe(result.outcomes[1]!.costs!.id);
@ -504,7 +518,7 @@ describe.skipIf(!url)('AI research workflow through authenticated APIs and Rust'
expect(Object.hasOwn(savedInput, 'source')).toBe(source === null);
expect(savedInput.evidence).toEqual(comparisonInput.evidence);
for (const reference of finding.latestRevision.evidence) {
const native = await f.api.getRunResults(reference.runId);
const native = await f.client.canonicalRunResult(reference.runId);
if (!native || !('kind' in native) || native.kind !== 'canonical-result/v1') {
throw new Error('Missing native comparison evidence');
}
@ -601,6 +615,10 @@ describe.skipIf(!url)('AI research workflow through authenticated APIs and Rust'
status: 401,
uncertain: false,
});
await expect(f.client.canonicalRunResult(reference.runId)).rejects.toMatchObject({
status: 401,
uncertain: false,
});
await expect(f.client.artifactRevisions(note.id)).rejects.toMatchObject({
status: 401,
uncertain: false,

View file

@ -552,6 +552,14 @@ describe.skipIf(!url)('retained original-run reproduction', () => {
const f = await fixture();
const { run, result } = await completed(f);
await f.pool.query('ALTER TABLE runs DISABLE TRIGGER USER');
// A valid but different SQL snapshot must not be replaced by the embedded request.
await f.pool.query('UPDATE runs SET request_snapshot=$1 WHERE id=$2', [
{ ...result.request, name: 'Different trusted snapshot' },
run.id,
]);
await expect(f.api.getRunResults(run.id)).rejects.toThrow('does not match');
await f.pool.query('UPDATE runs SET request_snapshot=NULL WHERE id=$1', [run.id]);
await expect(f.api.getRunResults(run.id)).rejects.toThrow();
await f.pool.query('UPDATE runs SET request_snapshot=$1 WHERE id=$2', [
{ ...result.request, policy: {} },
run.id,