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

@ -1,4 +1,5 @@
import { asValue, type AwilixContainer } from 'awilix';
import { z } from 'zod';
import type { ServiceCradle as ServiceDefinitions } from '@stock-bot/di';
import { BacktestRunner } from './backtest/BacktestRunner';
import { PostgresBacktestRunRepository } from './backtest/PostgresBacktestRunRepository';
@ -10,6 +11,7 @@ import { PostgresCanonicalExecutionRepository } from './execution/PostgresCanoni
import { BacktestHandler } from './handlers/backtest/backtest.handler';
import { CanonicalPaperRuntime } from './paper/CanonicalPaperRuntime';
import { validatePaperConfiguration } from './paper/feed';
import { PaperDeploymentHost } from './paper/PaperDeploymentHost';
import { AnalyticsService } from './services/AnalyticsService';
import { StorageService } from './services/StorageService';
import { createBuiltinStrategy } from './strategies/canonical-builtins';
@ -29,6 +31,45 @@ export function assertSupportedComposition(role: OrchestratorRole, useRustEngine
}
}
/** Validate before opening resources as well as before registering runtime services. */
export function resolvePaperComposition(
role: OrchestratorRole,
canonicalInput?: PaperConfigurationInput,
deploymentInput?: unknown
) {
if ((canonicalInput !== undefined || deploymentInput !== undefined) && role !== 'paper') {
throw new Error('Canonical paper configuration requires a paper composition');
}
if (canonicalInput !== undefined && deploymentInput !== undefined) {
throw new Error('Choose canonicalPaper or paperDeploymentHost, never two account owners');
}
const canonicalPaper =
canonicalInput === undefined ? undefined : validatePaperConfiguration(canonicalInput);
if (canonicalPaper && canonicalPaper.version !== 1) {
throw new Error('Promoted paper strategies require an explicitly reviewed deployment host');
}
const paperDeploymentHost =
deploymentInput === undefined
? undefined
: z
.object({
enabled: z.literal(true),
deploymentId: z
.string()
.uuid()
.transform(id => id.toLowerCase())
.optional(),
})
.strict()
.parse(deploymentInput);
if (role === 'paper' && !canonicalPaper && !paperDeploymentHost) {
throw new Error(
'Paper composition requires explicit canonicalPaper or paperDeploymentHost configuration; accounts are never migrated automatically'
);
}
return { canonicalPaper, paperDeploymentHost };
}
/**
* Register orchestrator-specific services in the DI container
*/
@ -43,6 +84,7 @@ export async function registerOrchestratorServices(
nativeExecutionTimeoutMs?: number;
maxConcurrent?: number;
canonicalPaper?: PaperConfigurationInput;
paperDeploymentHost?: { enabled: true; deploymentId?: string };
} = {}
): Promise<void> {
const role = options.role === undefined ? 'research-only' : options.role;
@ -50,18 +92,11 @@ export async function registerOrchestratorServices(
assertSupportedComposition(role, useRustEngine);
const maxConcurrent = options.maxConcurrent === undefined ? 1 : options.maxConcurrent;
assertResearchConcurrency(maxConcurrent);
if (options.canonicalPaper !== undefined && role !== 'paper') {
throw new Error('Canonical paper configuration requires a paper composition');
}
const canonicalPaper =
options.canonicalPaper === undefined
? undefined
: validatePaperConfiguration(options.canonicalPaper);
if (role === 'paper' && !canonicalPaper) {
throw new Error(
'Paper composition requires explicit canonicalPaper configuration; accounts are never migrated automatically'
);
}
const { canonicalPaper, paperDeploymentHost } = resolvePaperComposition(
role,
options.canonicalPaper,
options.paperDeploymentHost
);
// Create a service adapter that provides the expected interface
const services = container.resolve('serviceContainer') as IServiceContainer;
@ -72,6 +107,7 @@ export async function registerOrchestratorServices(
if (
[
'CanonicalPaperRuntime',
'PaperDeploymentHost',
'ModeManager',
'ExecutionService',
'MarketDataService',
@ -93,7 +129,7 @@ export async function registerOrchestratorServices(
options
);
if (canonicalPaper && !services.postgres) {
if ((canonicalPaper || paperDeploymentHost) && !services.postgres) {
throw new Error('Canonical paper requires durable PostgreSQL custody');
}
const paper =
@ -103,6 +139,13 @@ export async function registerOrchestratorServices(
programs: canonicalPaper.strategies.map(createBuiltinStrategy),
})
: undefined;
const deploymentHost =
paperDeploymentHost && services.postgres
? new PaperDeploymentHost({
database: services.postgres,
repository: new PostgresCanonicalExecutionRepository(services.postgres),
})
: undefined;
const runner = new ResearchBacktestPool({
runners: Array.from(
{ length: maxConcurrent },
@ -129,12 +172,14 @@ export async function registerOrchestratorServices(
AnalyticsService: analytics,
BacktestRunner: runner,
...(paper ? { CanonicalPaperRuntime: paper } : {}),
...(deploymentHost ? { PaperDeploymentHost: deploymentHost } : {}),
});
const canonicalContainer = container as AwilixContainer<
ServiceDefinitions & {
StorageService: StorageService;
AnalyticsService: AnalyticsService;
CanonicalPaperRuntime: PaperRuntime;
PaperDeploymentHost: PaperDeploymentHost;
BacktestRunner: ResearchBacktestPool;
orchestratorServices: IServiceContainer;
}
@ -145,6 +190,7 @@ export async function registerOrchestratorServices(
BacktestRunner: asValue(runner),
orchestratorServices: asValue(services),
...(paper ? { CanonicalPaperRuntime: asValue(paper) } : {}),
...(deploymentHost ? { PaperDeploymentHost: asValue(deploymentHost) } : {}),
});
container
.resolve('handlerScanner')
@ -153,10 +199,14 @@ export async function registerOrchestratorServices(
if (paper && canonicalPaper && options.initializeMode !== false) {
await paper.initialize(canonicalPaper);
}
if (deploymentHost && paperDeploymentHost?.deploymentId && options.initializeMode !== false) {
await deploymentHost.bind(paperDeploymentHost.deploymentId);
}
services.queueManager?.getQueue('orchestrator', { workers: 1, concurrency: maxConcurrent });
} catch (error) {
await runner.shutdown().catch(() => {});
await paper?.shutdown().catch(() => {});
await deploymentHost?.shutdown().catch(() => {});
throw error;
}
}