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

@ -57,7 +57,6 @@ export class BacktestRunner extends EventEmitter {
constructor(
private readonly dependencies: {
engine: BacktestExecutionEngine;
engineName: Exclude<BacktestRunResult['engine'], 'legacy'>;
repository: BacktestRunRepository;
logger: Pick<Logger, 'error'>;
heartbeatMs?: number;
@ -222,9 +221,6 @@ export class BacktestRunner extends EventEmitter {
};
this.dependencies.engine.on('progress', onProgress);
try {
if (!this.dependencies.repository.startCanonical) {
throw new Error('Canonical run repository is unavailable');
}
this.active = await this.dependencies.repository.startCanonical(config);
if (
this.active.executionDeadlineAt !== undefined &&
@ -283,18 +279,13 @@ export class BacktestRunner extends EventEmitter {
{
const engine = this.dependencies.engine;
const repository = this.dependencies.repository;
const loadCanonicalDataset = repository.loadCanonicalDataset;
if (!engine.runCanonical || !loadCanonicalDataset || !repository.completeCanonical) {
throw new Error('Canonical execution capabilities are unavailable');
}
const claim = this.active;
// Resolve retained data through the owning attempt. The adapter supplies its
// abort signal; neither this callback nor the worker fetches fresh provider bars.
const report = await engine.runCanonical(
executionConfig,
signal =>
loadCanonicalDataset.call(
repository,
repository.loadCanonicalDataset(
claim.runId,
claim.attemptId,
executionConfig.datasetHash,
@ -324,12 +315,8 @@ export class BacktestRunner extends EventEmitter {
if (this.leaseFailure) {
throw this.leaseFailure;
}
const completeCanonical = this.dependencies.repository.completeCanonical;
if (!completeCanonical) {
throw new Error('Canonical result persistence is unavailable');
}
checkDeadline();
await completeCanonical.call(this.dependencies.repository, result, this.active.attemptId);
await this.dependencies.repository.completeCanonical(result, this.active.attemptId);
// Observers cannot turn a committed success into a failed run.
this.publish('complete', result);
return result;
@ -450,36 +437,21 @@ export class BacktestRunner extends EventEmitter {
await this.dependencies.engine.shutdown();
}
}
/** Compatibility control; the engine must accept it before SQL records a pause. */
async pauseBacktest(runId?: string) {
const active = this.requireActive(runId);
await this.dependencies.engine.pauseBacktest();
await this.dependencies.repository.updateControl(
active.runId,
{ status: 'paused' },
active.attemptId
);
/** Retained control routes reject playback operations without mutating durable state. */
async pauseBacktest(runId?: string): Promise<void> {
this.requireActive(runId);
throw new Error('Native backtests do not support pause');
}
async resumeBacktest(runId?: string) {
const active = this.requireActive(runId);
await this.dependencies.engine.resumeBacktest();
await this.dependencies.repository.updateControl(
active.runId,
{ status: 'running' },
active.attemptId
);
async resumeBacktest(runId?: string): Promise<void> {
this.requireActive(runId);
throw new Error('Native backtests do not support resume');
}
async setSpeedMultiplier(speed: number | null, runId?: string) {
async setSpeedMultiplier(speed: number | null, runId?: string): Promise<void> {
if (speed !== null && (!Number.isFinite(speed) || speed <= 0)) {
throw new Error('Backtest speed must be positive or null');
}
const active = this.requireActive(runId);
this.dependencies.engine.setSpeedMultiplier(speed);
await this.dependencies.repository.updateControl(
active.runId,
{ speedMultiplier: speed },
active.attemptId
);
this.requireActive(runId);
throw new Error('Native backtests run at maximum speed');
}
/** Local worker projection; persisted run status remains authoritative after restart. */
getStatus() {