feat(research): run durable backtests in a bounded worker pool

This commit is contained in:
Boki 2026-10-24 17:41:00 -04:00
parent bbf0dd4453
commit 26b97265c6
15 changed files with 1730 additions and 129 deletions

View file

@ -2,7 +2,8 @@ import { asFunction, asValue, createContainer as createDiContainer } from 'awili
import { afterEach, describe, expect, test } from 'bun:test';
import { ServiceContainerBuilder, type ServiceCradle } from '@stock-bot/di';
import type { StockAppConfig } from '@stock-bot/stock-config';
import { RustBacktestAdapter } from '../../apps/stock/orchestrator/src/backtest/RustBacktestAdapter';
import { stockAppSchema } from '../../apps/stock/config/src/schemas/stock-app.schema';
import { ResearchBacktestPool } from '../../apps/stock/orchestrator/src/backtest/ResearchBacktestPool';
import { registerOrchestratorServices } from '../../apps/stock/orchestrator/src/container';
import { createRoutes } from '../../apps/stock/orchestrator/src/routes/create-routes';
import { createContainer } from '../../apps/stock/orchestrator/src/simple-container';
@ -15,7 +16,10 @@ afterEach(async () => {
await close();
}
});
async function compose(options: Parameters<typeof registerOrchestratorServices>[1] = {}) {
async function compose(
options: Parameters<typeof registerOrchestratorServices>[1] = {},
queues?: unknown[]
) {
const container = await new ServiceContainerBuilder()
.withOptions({
enableCache: false,
@ -28,6 +32,11 @@ async function compose(options: Parameters<typeof registerOrchestratorServices>[
})
.build();
const services: OrchestratorServices = { logger };
if (queues) {
Object.defineProperty(services, 'queueManager', {
value: { getQueue: (name: string, options: unknown) => queues.push({ name, options }) },
});
}
container.register({ serviceContainer: asValue(services) });
await registerOrchestratorServices(container, options);
cleanup.push(async () => {
@ -88,8 +97,9 @@ describe('canonical research-only default composition', () => {
'default and explicit research compose one canonical engine without any account/feed/mode gateway',
async options => {
const { services, container } = await compose(options);
expect(services.custom!.BacktestEngine).toBeInstanceOf(RustBacktestAdapter);
expect(services.custom!.BacktestRunner).toBeInstanceOf(ResearchBacktestPool);
for (const key of [
'BacktestEngine',
'ModeManager',
'ExecutionService',
'MarketDataService',
@ -118,6 +128,37 @@ describe('canonical research-only default composition', () => {
});
}
);
test.each([1, 2, 4] as const)(
'composition uses one BullMQ consumer with concurrency %s',
async maxConcurrent => {
const calls: unknown[] = [];
const { services } = await compose({ maxConcurrent }, calls);
expect(calls).toEqual([
{ name: 'orchestrator', options: { workers: 1, concurrency: maxConcurrent } },
]);
expect(services.custom!.BacktestRunner!.getStatus().isRunning).toBe(false);
}
);
test('default composition and schema select one slot; invalid values fail before resolving services/connections', async () => {
const calls: unknown[] = [];
await compose({}, calls);
expect(calls).toEqual([{ name: 'orchestrator', options: { workers: 1, concurrency: 1 } }]);
const schema = stockAppSchema.shape.services
.unwrap()
.shape.orchestrator.unwrap()
.shape.backtesting.unwrap();
expect(schema.parse({}).maxConcurrent).toBe(1);
for (const maxConcurrent of [null, 0, 3, 5, 1.5, '2', NaN, Infinity]) {
expect(schema.safeParse({ maxConcurrent }).success).toBe(false);
const container = createDiContainer<ServiceCradle>();
await expect(
registerOrchestratorServices(container, { maxConcurrent } as never)
).rejects.toThrow('1, 2, or 4');
await expect(
createContainer({ services: { orchestrator: { backtesting: { maxConcurrent } } } } as never)
).rejects.toThrow('1, 2, or 4');
}
});
test('cannot overlay the canonical graph on a legacy account runtime', async () => {
const container = createDiContainer<ServiceCradle>();
container.register({ serviceContainer: asValue({ logger, custom: { ExecutionService: {} } }) });