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; /** Only already admitted canonical rows may be claimed; no implicit creation. */ startCanonical?(request: CanonicalRunRequest): Promise; loadCanonicalDataset?( runId: string, attemptId: string, requestedHash: string, signal?: AbortSignal ): Promise; completeCanonical?(result: CanonicalRunResult, attemptId: string): Promise; load(runId: string): Promise<{ status: string; config: unknown } | null>; rejectPending(runId: string, error: string): Promise; heartbeat(runId: string, attemptId: string): Promise<{ cancelRequested: boolean }>; loadDataset( runId: string, attemptId: string, requestedHash?: string, signal?: AbortSignal ): Promise; freezeDataset( runId: string, attemptId: string, dataset: HistoricalDataset, signal?: AbortSignal ): Promise; complete(result: BacktestRunResult, attemptId: string): Promise; updateProgress( runId: string, progress: number, currentDate: string | undefined, attemptId: string ): Promise; fail(runId: string, error: string, attemptId: string): Promise; cancel(runId: string, attemptId: string): Promise; updateControl( runId: string, update: { status?: 'running' | 'paused'; speedMultiplier?: number | null }, attemptId: string ): Promise; } 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 { return this.unavailable(); } async load(_runId: string): Promise { return this.unavailable(); } async rejectPending(_runId: string, _error: string): Promise { this.unavailable(); } async heartbeat(_runId: string, _attemptId: string): Promise<{ cancelRequested: boolean }> { return this.unavailable(); } async loadDataset(): Promise { return this.unavailable(); } async freezeDataset(): Promise { return this.unavailable(); } async complete(_result: BacktestRunResult, _attemptId: string): Promise { this.unavailable(); } async updateProgress(): Promise { this.unavailable(); } async updateControl(): Promise { this.unavailable(); } async fail(_runId: string, _error: string, _attemptId: string): Promise { this.unavailable(); } async cancel(_runId: string, _attemptId: string): Promise { this.unavailable(); } }