stock-bot/apps/stock/orchestrator/src/backtest/run-repository.ts

107 lines
3.6 KiB
TypeScript

import type {
CanonicalRunRequest,
CanonicalRunResult,
} from '@stock-bot/stock-config/canonical-research';
import type { BacktestRunResult, HistoricalDataset, HistoricalDatasetV2 } from '@stock-bot/types';
import type { BacktestConfig } from '../types';
export interface RunIdentity {
runId: string;
backtestId: string;
}
export interface RunClaim extends RunIdentity {
attemptId: string;
config: BacktestConfig;
}
export interface CanonicalRunClaim extends RunIdentity {
/** Authoritative per-attempt research budget deadline; outside financial semantics. */
executionDeadlineAt?: number;
attemptId: string;
config: CanonicalRunRequest;
}
/** Every execution write must prove ownership of an unexpired attempt. */
export interface BacktestRunRepository {
start(config: BacktestConfig): Promise<RunClaim>;
/** Only already admitted canonical rows may be claimed; no implicit creation. */
startCanonical?(request: CanonicalRunRequest): Promise<CanonicalRunClaim>;
loadCanonicalDataset?(
runId: string,
attemptId: string,
requestedHash: string,
signal?: AbortSignal
): Promise<HistoricalDatasetV2>;
completeCanonical?(result: CanonicalRunResult, attemptId: string): Promise<void>;
load(runId: string): Promise<{ status: string; config: unknown } | null>;
rejectPending(runId: string, error: string): Promise<void>;
heartbeat(runId: string, attemptId: string): Promise<{ cancelRequested: boolean }>;
loadDataset(
runId: string,
attemptId: string,
requestedHash?: string,
signal?: AbortSignal
): Promise<HistoricalDataset | null>;
freezeDataset(
runId: string,
attemptId: string,
dataset: HistoricalDataset,
signal?: AbortSignal
): Promise<HistoricalDataset>;
complete(result: BacktestRunResult, attemptId: string): Promise<void>;
updateProgress(
runId: string,
progress: number,
currentDate: string | undefined,
attemptId: string
): Promise<void>;
fail(runId: string, error: string, attemptId: string): Promise<void>;
cancel(runId: string, attemptId: string): Promise<void>;
updateControl(
runId: string,
update: { status?: 'running' | 'paused'; speedMultiplier?: number | null },
attemptId: string
): Promise<void>;
}
export class BacktestConflictError extends Error {}
export class BacktestLeaseLostError extends BacktestConflictError {}
/** No database is never a license to execute an unowned job. */
export class UnavailableBacktestRunRepository implements BacktestRunRepository {
private unavailable(): never {
throw new Error('Backtest execution requires a PostgreSQL run repository');
}
async start(_config: BacktestConfig): Promise<RunClaim> {
return this.unavailable();
}
async load(_runId: string): Promise<null> {
return this.unavailable();
}
async rejectPending(_runId: string, _error: string): Promise<void> {
this.unavailable();
}
async heartbeat(_runId: string, _attemptId: string): Promise<{ cancelRequested: boolean }> {
return this.unavailable();
}
async loadDataset(): Promise<null> {
return this.unavailable();
}
async freezeDataset(): Promise<HistoricalDataset> {
return this.unavailable();
}
async complete(_result: BacktestRunResult, _attemptId: string): Promise<void> {
this.unavailable();
}
async updateProgress(): Promise<void> {
this.unavailable();
}
async updateControl(): Promise<void> {
this.unavailable();
}
async fail(_runId: string, _error: string, _attemptId: string): Promise<void> {
this.unavailable();
}
async cancel(_runId: string, _attemptId: string): Promise<void> {
this.unavailable();
}
}