refactor(orchestrator): require canonical research contracts (CL14)

This commit is contained in:
Boki 2026-10-26 19:22:01 -04:00
parent bc2b8a89da
commit 21666242f4
37 changed files with 151 additions and 319 deletions

View file

@ -82,15 +82,8 @@ describe('canonical production workflow and legacy retirement', () => {
}
);
test('legacy adapter and worker envelopes refuse before mutable storage or child process access', async () => {
let reads = 0;
const engine = new RustBacktestAdapter({
logger,
storage: {
async getHistoricalBars() {
reads++;
return [];
},
},
});
await expect(engine.runBacktest(legacy)).rejects.toThrow(LEGACY_EXECUTION_RETIRED);
const request = { type: 'run', config: legacy, data: [] };
@ -98,7 +91,6 @@ describe('canonical production workflow and legacy retirement', () => {
await expect(
new NativeBacktestWorker().execute(request as never, new AbortController().signal, () => {})
).rejects.toThrow();
expect(reads).toBe(0);
expect(engine.getStatus().isRunning).toBe(false);
await engine.shutdown();
});

View file

@ -1,9 +1,9 @@
import { describe, expect, test } from 'bun:test';
import { BacktestCancelledError } from '../../apps/stock/orchestrator/src/backtest/execution-engine';
import {
BacktestCancelledError,
type BacktestExecutionEngine,
} from '../../apps/stock/orchestrator/src/backtest/execution-engine';
import { BacktestLeaseLostError } from '../../apps/stock/orchestrator/src/backtest/run-repository';
BacktestLeaseLostError,
UnavailableBacktestRunRepository,
} from '../../apps/stock/orchestrator/src/backtest/run-repository';
import { deferred, canonicalRunnerFixture as setup } from '../fixtures/canonical-runner';
describe('canonical jobs share the durable runner lifecycle', () => {
@ -47,31 +47,20 @@ describe('canonical jobs share the durable runner lifecycle', () => {
expect(f.events).toEqual([]);
await f.runner.shutdown();
});
test('a runner without canonical execution fails without preparation or legacy/provider fallback', async () => {
test('unavailable canonical storage refuses before any execution or provider fallback', async () => {
const f = setup();
const engine: BacktestExecutionEngine = f.engine;
delete engine.runCanonical;
const fail = f.repo.fail.bind(f.repo);
let savedFailure: string | undefined;
f.repo.fail = async (runId, error, attemptId) => {
savedFailure = error;
await fail(runId, error, attemptId);
};
try {
await expect(f.runner.runQueued(f.request.runId)).rejects.toThrow(
'Canonical execution capabilities are unavailable'
);
expect(savedFailure).toBe('Canonical execution capabilities are unavailable');
expect(f.events).toEqual(['claim', 'heartbeat', 'fail']);
expect(f.result).toBeUndefined();
expect(await f.runner.runQueued(f.request.runId)).toEqual({
runId: f.request.runId,
status: 'failed',
});
expect(f.events).toEqual(['claim', 'heartbeat', 'fail']);
} finally {
await f.runner.shutdown();
}
const unavailable = new UnavailableBacktestRunRepository();
f.repo.startCanonical = unavailable.startCanonical.bind(unavailable);
await expect(f.runner.runQueued(f.request.runId)).rejects.toThrow(
'Backtest execution requires a PostgreSQL run repository'
);
expect(f.events).toEqual([]);
expect(f.result).toBeUndefined();
await expect(unavailable.loadCanonicalDataset()).rejects.toThrow('PostgreSQL run repository');
await expect(unavailable.completeCanonical({} as never, 'attempt')).rejects.toThrow(
'PostgreSQL run repository'
);
await f.runner.shutdown();
});
test('cancel waits for execution to finish before committing cancellation', async () => {
const f = setup(),

View file

@ -95,16 +95,9 @@ test('canonical adapter cancellation during retained dataset reads prevents a la
const f = canonicalRunnerFixture(),
entered = deferred<void>(),
release = deferred<void>();
let providerReads = 0,
progress = 0;
let progress = 0;
const engine = new RustBacktestAdapter({
logger: { error() {} },
storage: {
async getHistoricalBars() {
providerReads++;
return [];
},
},
});
engine.on('progress', () => progress++);
const result = engine
@ -119,7 +112,6 @@ test('canonical adapter cancellation during retained dataset reads prevents a la
expect(await result).toBeInstanceOf(BacktestCancelledError);
release.resolve();
await Promise.resolve();
expect(providerReads).toBe(0);
expect(progress).toBe(0);
expect(engine.getStatus().isRunning).toBe(false);
await engine.shutdown();
@ -130,11 +122,6 @@ test('canonical adapter timeout covers retained input loading and shutdown close
const engine = new RustBacktestAdapter(
{
logger: { error() {} },
storage: {
async getHistoricalBars() {
throw new Error('provider must not run');
},
},
},
{ timeoutMs: 20 }
);

View file

@ -341,9 +341,7 @@ describe('deterministic ingestion → storage → native backtest', () => {
});
test('retired provider-at-execution entry refuses before storage or native strategy resolution', async () => {
const { mongodb } = memoryStore();
const storage = new StorageService({ logger }, mongodb, null, null);
const engine = new RustBacktestAdapter({ logger, storage });
const engine = new RustBacktestAdapter({ logger });
const config = BacktestConfigSchema.parse({
mode: 'backtest',
symbols: ['FIXTURE.TEST'],

View file

@ -181,7 +181,7 @@ describe('EOD intraday ingestion → research storage', () => {
expect(validateDataset(JSON.parse(JSON.stringify(legacy))).manifest.hash).toBe(
legacy.manifest.hash
);
const engine = new RustBacktestAdapter({ logger, storage });
const engine = new RustBacktestAdapter({ logger });
await expect(engine.runBacktest(config(), async () => legacy)).rejects.toThrow(
'Legacy backtest execution is retired'
);

View file

@ -2,10 +2,7 @@ import { randomUUID } from 'node:crypto';
import { afterEach, describe, expect, test } from 'bun:test';
import { Hono } from 'hono';
import { createBacktestRoutes } from '../../apps/stock/orchestrator/src/api/rest/backtest';
import {
BacktestCancelledError,
type BacktestExecutionEngine,
} from '../../apps/stock/orchestrator/src/backtest/execution-engine';
import { BacktestCancelledError } from '../../apps/stock/orchestrator/src/backtest/execution-engine';
import { ResearchBacktestPool } from '../../apps/stock/orchestrator/src/backtest/ResearchBacktestPool';
import { canonicalRunnerFixture, deferred } from '../fixtures/canonical-runner';
import { logger } from '../fixtures/execution';
@ -300,25 +297,26 @@ describe('run-scoped controls and event streams', () => {
f.slots[0]!.release.resolve();
await work[0];
});
test('exactly one active run retains no-ID pause/resume/speed controls', async () => {
test('one active run preserves explicit rejection of unsupported playback controls', async () => {
const f = fixture(1),
work = await started(f),
controls: unknown[] = [],
slot = f.slots[0]!;
slot.engine.pauseBacktest = async () => {
controls.push('pause');
};
slot.engine.resumeBacktest = async () => {
controls.push('resume');
};
(slot.engine as BacktestExecutionEngine).setSpeedMultiplier = speed => {
controls.push(speed);
};
for (const action of ['pause', 'resume']) {
expect((await post(f.app, action)).status).toBe(200);
for (const [action, message] of [
['pause', 'Native backtests do not support pause'],
['resume', 'Native backtests do not support resume'],
['speed', 'Native backtests run at maximum speed'],
] as const) {
for (const runId of [undefined, slot.request.runId]) {
const response = await post(f.app, action, {
...(runId ? { runId } : {}),
...(action === 'speed' ? { speed: null } : {}),
});
expect(response.status).toBe(500);
expect(await response.json()).toMatchObject({ error: message });
}
}
expect((await post(f.app, 'speed', { speed: null })).status).toBe(200);
expect(controls).toEqual(['pause', 'resume', null]);
expect(slot.runner.getStatus().isPaused).toBe(false);
expect(slot.events).not.toContain('complete');
slot.release.resolve();
await work[0];
});