import { ServiceContainerBuilder } from '@stock-bot/di'; import type { StockAppConfig } from '@stock-bot/stock-config'; import { assertResearchConcurrency } from './backtest/ResearchBacktestPool'; import { paperConfigurationSchema } from '@stock-bot/stock-config/paper-runtime'; import { assertSupportedComposition, registerOrchestratorServices, resolvePaperComposition, type OrchestratorRole, } from './container'; import { ownOrchestratorRuntime } from './runtime'; import type { OrchestratorServices } from './types'; type OwnedServices = Awaited>; function buildSharedServices(config: StockAppConfig): Promise { return new ServiceContainerBuilder() .withConfig(config) .withOptions({ enableMongoDB: config.database?.mongodb?.enabled ?? false, enablePostgres: config.database?.postgres?.enabled ?? false, enableCache: config.database?.dragonfly?.enabled ?? false, enableQueue: config.queue?.enabled ?? false, enableQuestDB: config.database?.questdb?.enabled ?? false, enableBrowser: false, enableProxy: false, }) .buildOwned(); } /** Use the shared builder for connection setup and adapter ownership. */ export async function createContainer( config: StockAppConfig, options: { role?: OrchestratorRole } = {}, build: (config: StockAppConfig) => Promise = buildSharedServices ) { const settings = config.services?.orchestrator; if (settings?.defaultMode === 'live') { throw new Error('Live trading is disabled: no real broker adapter exists'); } const role = options.role === undefined ? settings?.defaultMode === 'paper' ? 'paper' : 'research-only' : options.role; const configuredEngine = config.services?.orchestrator?.backtesting?.useRustEngine; const useRustEngine = configuredEngine === undefined ? true : configuredEngine; // Fail before opening connections as well as before registering runtime services. assertSupportedComposition(role, useRustEngine); const maxConcurrent = settings?.backtesting?.maxConcurrent === undefined ? 1 : settings.backtesting.maxConcurrent; assertResearchConcurrency(maxConcurrent); const { canonicalPaper, paperDeploymentHost } = resolvePaperComposition( role, settings?.canonicalPaper === undefined ? undefined : paperConfigurationSchema.parse(settings.canonicalPaper), settings?.paperDeploymentHost ); if ((canonicalPaper || paperDeploymentHost) && !config.database?.postgres?.enabled) { throw new Error('Canonical paper requires durable PostgreSQL custody'); } const owned = await build(config); let runtime: ReturnType | undefined; try { const services = owned.container.resolve('serviceContainer') as OrchestratorServices; runtime = ownOrchestratorRuntime(services, owned.shutdown); await registerOrchestratorServices(owned.container, { role, useRustEngine, maxConcurrent, allowSyntheticData: config.services?.orchestrator?.backtesting?.allowSyntheticData ?? false, syntheticSeed: config.services?.orchestrator?.backtesting?.syntheticSeed ?? 42, nativeExecutionTimeoutMs: config.services?.orchestrator?.backtesting?.executionTimeoutMs, canonicalPaper, paperDeploymentHost, }); return runtime; } catch (error) { try { await (runtime ? runtime.shutdown() : owned.shutdown()); } catch (cleanupError) { runtime?.services.logger.error('Orchestrator composition cleanup failed', { cleanupError }); } throw error; } }