feat(research): isolate candidates and add reviewed paper promotion

This commit is contained in:
Boki 2026-10-24 18:46:55 -04:00
parent 26b97265c6
commit 25b5cbf481
128 changed files with 13038 additions and 1388 deletions

View file

@ -2,6 +2,7 @@ import { randomUUID } from 'node:crypto';
import type { PoolClient, QueryResultRow } from 'pg';
import { z } from 'zod';
import { requireServices, type IServiceContainer } from '@stock-bot/services';
import { canonicalStrategyExecution } from '@stock-bot/stock-config/canonical-research';
import type {
CostSensitivityInput,
WorkbenchExperimentDetail,
@ -39,7 +40,11 @@ import {
PREVIEW_RUN_ID,
} from './experiment-input';
import { mapExperimentRetry, parseExperimentRetry, retryColumns } from './experiment-retry';
import { lockExperimentResearchAgent, lockResearchAgent } from './research-agent-budget';
import {
lockExperimentResearchAgent,
lockResearchAgent,
verifyCandidateGrant,
} from './research-agent-budget';
import { RunReproductionError, verifyReproductionInputs } from './run-reproduction';
import { StrategyCatalogError } from './strategy-catalog.service';
import { parseChronologicalValidation, prepareChronologicalValidation } from './validation-preview';
@ -118,32 +123,52 @@ export class ExperimentsService {
constructor(private readonly container: IServiceContainer) {
this.database = requireServices(container, 'postgres').postgres;
}
async preview(raw: unknown): Promise<ExperimentPreview> {
async preview(raw: unknown, principal = 'operator'): Promise<ExperimentPreview> {
const input = parseExperimentInput(raw);
return this.database.transaction(client => this.lockedPreview(client, input));
}
async previewCosts(raw: unknown) {
const input = parseCostSensitivityInput(raw);
return this.database.transaction(client => this.lockedCostPreview(client, input));
}
private async lockedCostPreview(client: PoolClient, input: CostSensitivityInput) {
const source = await this.lockedPreview(client, {
backtestId: input.backtestId,
name: input.name,
grid: {},
return this.database.transaction(async client => {
await lockResearchAgent(client, principal);
return this.lockedPreview(client, input, principal);
});
}
async previewCosts(raw: unknown, principal = 'operator') {
const input = parseCostSensitivityInput(raw);
return this.database.transaction(async client => {
await lockResearchAgent(client, principal);
return this.lockedCostPreview(client, input, principal);
});
}
private async lockedCostPreview(
client: PoolClient,
input: CostSensitivityInput,
principal = 'operator'
) {
const source = await this.lockedPreview(
client,
{
backtestId: input.backtestId,
name: input.name,
grid: {},
...(input.candidate ? { candidate: input.candidate } : {}),
},
principal
);
return buildCostSensitivityPreview(input, source);
}
async previewValidation(raw: unknown) {
async previewValidation(raw: unknown, principal = 'operator') {
const input = parseChronologicalValidation(raw);
const { recipe: _recipe, ...sourceInput } = input;
return this.database.transaction(async client => {
const source = await this.lockedPreview(client, sourceInput);
await lockResearchAgent(client, principal);
const source = await this.lockedPreview(client, sourceInput, principal);
const dataset = await readCanonicalDataset(client, source.request.datasetHash);
return prepareChronologicalValidation(input, source, dataset).preview;
});
}
async lockedPreview(client: PoolClient, input: ExperimentInput): Promise<ExperimentPreview> {
async lockedPreview(
client: PoolClient,
input: ExperimentInput,
principal = 'operator'
): Promise<ExperimentPreview> {
const parent = (
await client.query('SELECT * FROM backtests WHERE id=$1 FOR UPDATE', [input.backtestId])
).rows[0];
@ -172,7 +197,8 @@ export class ExperimentsService {
config: parent.config,
},
input.backtestId,
PREVIEW_RUN_ID
PREVIEW_RUN_ID,
input.candidate
);
} catch (cause) {
if (
@ -192,6 +218,7 @@ export class ExperimentsService {
}
throw cause;
}
await verifyCandidateGrant(client, request, principal);
return buildExperimentPreview(
input,
request,
@ -251,8 +278,8 @@ export class ExperimentsService {
}
const preview: WorkbenchExperimentPreview =
'scenarios' in input
? await this.lockedCostPreview(client, input)
: await this.lockedPreview(client, input);
? await this.lockedCostPreview(client, input, receipt.principal)
: await this.lockedPreview(client, input, receipt.principal);
if (preview.previewHash !== previewHash) {
throw new ExperimentError(
409,
@ -648,7 +675,7 @@ export class ExperimentsService {
run.result_run_id !== run.id ||
run.result_backtest_id !== expected.backtestId ||
run.financial_engine !== 'rust' ||
run.strategy_execution !== 'typescript-retained-builtin/v1' ||
run.strategy_execution !== canonicalStrategyExecution(expected) ||
!equal(run.result_request, expected) ||
run.analytics_version !== 1 ||
!equal(run.analytics_policy, expectedPolicy) ||