77 lines
2.8 KiB
TypeScript
77 lines
2.8 KiB
TypeScript
import type {
|
|
CanonicalRunRequest,
|
|
CanonicalRunResult,
|
|
} from '@stock-bot/stock-config/canonical-research';
|
|
import type { HistoricalDatasetV2 } from '@stock-bot/types';
|
|
|
|
export interface RunIdentity {
|
|
runId: string;
|
|
backtestId: string;
|
|
}
|
|
export interface CanonicalRunClaim extends RunIdentity {
|
|
/** Authoritative per-attempt research budget deadline; outside financial semantics. */
|
|
executionDeadlineAt?: number;
|
|
attemptId: string;
|
|
config: CanonicalRunRequest;
|
|
}
|
|
|
|
/** Canonical execution only; every write proves ownership of an unexpired attempt.
|
|
* Historical writer adapters are outside this production contract. */
|
|
export interface BacktestRunRepository {
|
|
/** 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 }>;
|
|
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>;
|
|
}
|
|
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 startCanonical(_request: CanonicalRunRequest): Promise<CanonicalRunClaim> {
|
|
return this.unavailable();
|
|
}
|
|
async loadCanonicalDataset(): Promise<HistoricalDatasetV2> {
|
|
return this.unavailable();
|
|
}
|
|
async completeCanonical(_result: CanonicalRunResult, _attemptId: string): Promise<void> {
|
|
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 updateProgress(): 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();
|
|
}
|
|
}
|