fix(runtime): isolate legacy research from paper execution
This commit is contained in:
parent
ee670a3bd1
commit
43ffb547be
7 changed files with 485 additions and 9 deletions
|
|
@ -18,12 +18,29 @@ import { StrategyManager } from './strategies/StrategyManager';
|
|||
import type { StrategyRegistry } from './strategies/StrategyRegistry';
|
||||
import type { OrchestratorServices as IServiceContainer } from './types';
|
||||
|
||||
export type OrchestratorRole = 'paper' | 'research-only';
|
||||
|
||||
export function assertSupportedComposition(role: OrchestratorRole, useRustEngine: boolean): void {
|
||||
if (role !== 'paper' && role !== 'research-only') {
|
||||
throw new Error('Unsupported runtime role: expected paper or research-only');
|
||||
}
|
||||
if (typeof useRustEngine !== 'boolean') {
|
||||
throw new Error('Runtime useRustEngine option must be a boolean');
|
||||
}
|
||||
if (role !== 'research-only' && !useRustEngine) {
|
||||
throw new Error(
|
||||
'Unsupported runtime combination: TypeScript backtests require a research-only composition; paper requires the isolated native engine'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register orchestrator-specific services in the DI container
|
||||
*/
|
||||
export async function registerOrchestratorServices(
|
||||
container: AwilixContainer<ServiceDefinitions>,
|
||||
options: {
|
||||
role?: OrchestratorRole;
|
||||
initializeMode?: boolean;
|
||||
useRustEngine?: boolean;
|
||||
allowSyntheticData?: boolean;
|
||||
|
|
@ -32,6 +49,11 @@ export async function registerOrchestratorServices(
|
|||
strategyRegistry?: StrategyRegistry;
|
||||
} = {}
|
||||
): Promise<void> {
|
||||
const role = options.role === undefined ? 'paper' : options.role;
|
||||
const useRustEngine = options.useRustEngine === undefined ? true : options.useRustEngine;
|
||||
assertSupportedComposition(role, useRustEngine);
|
||||
const allowedMode = role === 'research-only' ? 'backtest' : 'paper';
|
||||
|
||||
// Create a service adapter that provides the expected interface
|
||||
const services = container.resolve('serviceContainer') as IServiceContainer;
|
||||
const custom = services.custom ?? {};
|
||||
|
|
@ -55,7 +77,7 @@ export async function registerOrchestratorServices(
|
|||
storage: storageService,
|
||||
drainStrategies: () => strategyManager.drain(),
|
||||
});
|
||||
const executionService = new ExecutionService(services, storageService);
|
||||
const executionService = new ExecutionService(services, storageService, { allowedMode });
|
||||
const analyticsService = new AnalyticsService(services);
|
||||
const registry = options.strategyRegistry ?? createBuiltinStrategyRegistry();
|
||||
const strategyManager: StrategyManager = new StrategyManager({
|
||||
|
|
@ -65,13 +87,14 @@ export async function registerOrchestratorServices(
|
|||
createStrategy: config => registry.create(config, { modeManager, execution: executionService }),
|
||||
});
|
||||
const modeManager: ModeManager = new ModeManager({
|
||||
allowedMode,
|
||||
logger: services.logger,
|
||||
marketData: marketDataService,
|
||||
execution: executionService,
|
||||
storage: storageService,
|
||||
strategies: strategyManager,
|
||||
});
|
||||
const backtestEngine = options.useRustEngine
|
||||
const backtestEngine = useRustEngine
|
||||
? new RustBacktestAdapter(
|
||||
{ logger: services.logger, storage: storageService },
|
||||
{
|
||||
|
|
@ -93,12 +116,12 @@ export async function registerOrchestratorServices(
|
|||
);
|
||||
const backtestRunner = new BacktestRunner({
|
||||
engine: backtestEngine,
|
||||
engineName: options.useRustEngine ? 'native' : 'typescript',
|
||||
engineName: useRustEngine ? 'native' : 'typescript',
|
||||
repository: services.postgres
|
||||
? new PostgresBacktestRunRepository(services.postgres)
|
||||
: new UnavailableBacktestRunRepository(),
|
||||
logger: services.logger,
|
||||
prepare: options.useRustEngine ? undefined : config => modeManager.initializeMode(config),
|
||||
prepare: useRustEngine ? undefined : config => modeManager.initializeMode(config),
|
||||
});
|
||||
|
||||
// Store custom services
|
||||
|
|
@ -150,7 +173,7 @@ export async function registerOrchestratorServices(
|
|||
strategyManager.setupEventListeners();
|
||||
|
||||
// Initialize mode manager with default paper trading mode
|
||||
if (options.initializeMode !== false) {
|
||||
if (role === 'paper' && options.initializeMode !== false) {
|
||||
await modeManager.initializeMode({ mode: 'paper', startingCapital: 100000 });
|
||||
}
|
||||
// Start job consumption only after the shared graph and initial mode are ready.
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ export class ModeManager extends EventEmitter {
|
|||
private isInitialized = false;
|
||||
constructor(
|
||||
private readonly dependencies: {
|
||||
allowedMode?: 'paper' | 'backtest';
|
||||
logger: Pick<Logger, 'info'>;
|
||||
marketData: Pick<MarketDataService, 'initialize' | 'shutdown'>;
|
||||
execution: Pick<ExecutionService, 'initialize' | 'shutdown'>;
|
||||
|
|
@ -37,6 +38,11 @@ export class ModeManager extends EventEmitter {
|
|||
if (config.mode === 'live') {
|
||||
throw new Error('Live trading is disabled: no real broker adapter exists');
|
||||
}
|
||||
if (this.dependencies.allowedMode && config.mode !== this.dependencies.allowedMode) {
|
||||
throw new Error(
|
||||
`Runtime composition only permits ${this.dependencies.allowedMode} mode; cannot initialize ${config.mode}`
|
||||
);
|
||||
}
|
||||
// Validate config based on mode
|
||||
switch (config.mode) {
|
||||
case 'backtest':
|
||||
|
|
|
|||
|
|
@ -60,7 +60,11 @@ export class ExecutionService extends EventEmitter {
|
|||
constructor(
|
||||
private readonly container: OrchestratorServices,
|
||||
_storage: StorageService,
|
||||
private readonly options: { repository?: ExecutionRepository; clock?: () => number } = {}
|
||||
private readonly options: {
|
||||
repository?: ExecutionRepository;
|
||||
clock?: () => number;
|
||||
allowedMode?: 'paper' | 'backtest';
|
||||
} = {}
|
||||
) {
|
||||
super();
|
||||
this.simulation = new SimulationExecution({
|
||||
|
|
@ -93,6 +97,11 @@ export class ExecutionService extends EventEmitter {
|
|||
'Live trading is disabled: no real broker adapter exists'
|
||||
);
|
||||
}
|
||||
if (this.options.allowedMode && config.mode !== this.options.allowedMode) {
|
||||
throw new UnsupportedExecutionError(
|
||||
`Runtime composition only permits ${this.options.allowedMode} mode; cannot initialize ${config.mode}`
|
||||
);
|
||||
}
|
||||
await this.tail;
|
||||
this.closing = false;
|
||||
this.mode = undefined;
|
||||
|
|
|
|||
|
|
@ -1,10 +1,22 @@
|
|||
import { ServiceContainerBuilder } from '@stock-bot/di';
|
||||
import type { StockAppConfig } from '@stock-bot/stock-config';
|
||||
import { registerOrchestratorServices } from './container';
|
||||
import {
|
||||
assertSupportedComposition,
|
||||
registerOrchestratorServices,
|
||||
type OrchestratorRole,
|
||||
} from './container';
|
||||
import type { OrchestratorServices } from './types';
|
||||
|
||||
/** Use the shared builder for connection setup and adapter ownership. */
|
||||
export async function createContainer(config: StockAppConfig) {
|
||||
export async function createContainer(
|
||||
config: StockAppConfig,
|
||||
options: { role?: OrchestratorRole } = {}
|
||||
) {
|
||||
const role = options.role === undefined ? 'paper' : 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 container = await new ServiceContainerBuilder()
|
||||
.withConfig(config)
|
||||
.withOptions({
|
||||
|
|
@ -18,7 +30,8 @@ export async function createContainer(config: StockAppConfig) {
|
|||
})
|
||||
.build();
|
||||
await registerOrchestratorServices(container, {
|
||||
useRustEngine: config.services?.orchestrator?.backtesting?.useRustEngine ?? true,
|
||||
role,
|
||||
useRustEngine,
|
||||
allowSyntheticData: config.services?.orchestrator?.backtesting?.allowSyntheticData ?? false,
|
||||
syntheticSeed: config.services?.orchestrator?.backtesting?.syntheticSeed ?? 42,
|
||||
nativeExecutionTimeoutMs: config.services?.orchestrator?.backtesting?.executionTimeoutMs,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue