docs: explain strategy replay and durable execution code
This commit is contained in:
parent
8587c71cf3
commit
dd7ac53c51
19 changed files with 494 additions and 41 deletions
|
|
@ -1,6 +1,11 @@
|
|||
import type { Logger, PostgresClient, Queue } from '@stock-bot/services';
|
||||
|
||||
/** PostgreSQL is the source of truth; Redis jobs are repeatable delivery notifications. */
|
||||
/**
|
||||
* Bridges committed backtest_dispatch rows to BullMQ and recovers expired leases.
|
||||
* PostgreSQL is the source of truth; Redis jobs are repeatable notifications.
|
||||
* Duplicate delivery is safe because BacktestRunner must claim the SQL run before
|
||||
* executing it. Queue retries and durable attempt limits are separate mechanisms.
|
||||
*/
|
||||
export class BacktestDispatcher {
|
||||
private timer?: ReturnType<typeof setInterval>;
|
||||
private inFlight?: Promise<void>;
|
||||
|
|
@ -27,6 +32,7 @@ export class BacktestDispatcher {
|
|||
);
|
||||
}
|
||||
|
||||
/** Timer/manual ticks share one in-flight pass; a slow publish cannot overlap itself. */
|
||||
tick(): Promise<void> {
|
||||
if (this.closing) {
|
||||
return Promise.resolve();
|
||||
|
|
@ -44,6 +50,11 @@ export class BacktestDispatcher {
|
|||
await this.inFlight;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recover SQL ownership first, reserve due notifications second, publish last.
|
||||
* next_publish_at is a retry delay, not a delivery acknowledgement: a crash
|
||||
* after reservation leaves a durable intent that becomes due again.
|
||||
*/
|
||||
private async dispatch() {
|
||||
// Recovery is independent of Redis availability. Every terminal write fences the old owner.
|
||||
await this.postgres.transaction(async client => {
|
||||
|
|
|
|||
|
|
@ -158,6 +158,12 @@ export class BacktestRetentionError extends Error {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Application service for editable templates and immutable run inputs/results.
|
||||
* Submission writes a PostgreSQL outbox row for BacktestDispatcher; execution
|
||||
* belongs to the orchestrator. Historical result readers remain available even
|
||||
* though new submissions require the canonical Rust research path.
|
||||
*/
|
||||
export class BacktestServiceV2 {
|
||||
private container: IServiceContainer & { postgres: NonNullable<IServiceContainer['postgres']> };
|
||||
private wsHandler: WebSocketHandler | null = null;
|
||||
|
|
@ -394,7 +400,12 @@ export class BacktestServiceV2 {
|
|||
});
|
||||
}
|
||||
|
||||
// Run operations
|
||||
/**
|
||||
* Admit one logical run, saving its request and dispatch intent atomically.
|
||||
* A principal/key identifies the submission, not the template's current contents:
|
||||
* the same retry returns the original run even after a template edit. Conflicting
|
||||
* reuse fails. No queue operation occurs inside this transaction.
|
||||
*/
|
||||
async createRun(request: RunRequest, submission?: RunSubmission): Promise<Run> {
|
||||
const runId = uuidv4();
|
||||
const multiplier = request.speedMultiplier ?? null;
|
||||
|
|
@ -561,6 +572,11 @@ export class BacktestServiceV2 {
|
|||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new run from original retained inputs, not today's editable template.
|
||||
* Current review and retained-build checks still apply. Only execution/account
|
||||
* identity and the current review reference are rebound; economics stay pinned.
|
||||
*/
|
||||
async reproduceRun(originalRunId: string, submission: RunSubmission): Promise<Run> {
|
||||
if (
|
||||
!submission ||
|
||||
|
|
@ -819,7 +835,12 @@ export class BacktestServiceV2 {
|
|||
// The runner, not the API client, commits acknowledged state changes.
|
||||
}
|
||||
|
||||
// Get run results
|
||||
/**
|
||||
* Read saved results and verify canonical provenance and derived analytics
|
||||
* against the frozen request. Recomputing statistics checks integrity; it does
|
||||
* not simulate fills again. Missing canonical data must not fall back to a
|
||||
* legacy result projection, which has different financial semantics.
|
||||
*/
|
||||
async getRunResults(
|
||||
runId: string,
|
||||
database: BacktestResultQuery = this.container.postgres
|
||||
|
|
@ -883,6 +904,7 @@ export class BacktestServiceV2 {
|
|||
return mapStoredBacktestResult(row);
|
||||
}
|
||||
|
||||
/** Preview admission for the UI without saving a run; submission must recheck it. */
|
||||
async canonicalCapability(id: string): Promise<CanonicalRunCapability> {
|
||||
try {
|
||||
const execution = await this.container.postgres.transaction(async client => {
|
||||
|
|
|
|||
|
|
@ -1,3 +1,10 @@
|
|||
/**
|
||||
* Converts editable workbench choices into a fully bound canonical request.
|
||||
* Admission validates retained data/code and freezes financial policies/build
|
||||
* identity; it neither runs a strategy nor writes the run/dispatch rows.
|
||||
* Submission services own persistence; previews can discard the returned request.
|
||||
* The worker verifies inputs again before execution.
|
||||
*/
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import type { PoolClient } from 'pg';
|
||||
import type { IServiceContainer } from '@stock-bot/services';
|
||||
|
|
@ -57,6 +64,10 @@ export function canonicalDatasetUnavailableReason(dataset: HistoricalDatasetV2):
|
|||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Revalidate the stored payload and hash before trusting a catalog selection.
|
||||
* A syntactically valid hash or manifest alone is not execution eligibility.
|
||||
*/
|
||||
export async function readCanonicalDataset(
|
||||
database: DatasetCatalogQuery,
|
||||
hash: unknown
|
||||
|
|
@ -112,7 +123,11 @@ export async function canonicalDatasetEntry(
|
|||
}
|
||||
}
|
||||
|
||||
/** Separate v2 boundary: never round exchange-session times through the v1 daily parser. */
|
||||
/**
|
||||
* Require template symbols, interval and trading bounds to match retained input
|
||||
* exactly. Warmup remains part of that dataset rather than an implicit new fetch.
|
||||
* This v2 boundary avoids rounding session timestamps through the v1 daily parser.
|
||||
*/
|
||||
export async function validateCanonicalSetup(
|
||||
database: DatasetCatalogQuery,
|
||||
template: CanonicalTemplate
|
||||
|
|
@ -157,7 +172,17 @@ export async function validateCanonicalSetup(
|
|||
return { settings, dataset, execution, series, initial };
|
||||
}
|
||||
|
||||
/** Called under the backtest lock; this also serializes with family review/archive. */
|
||||
/**
|
||||
* Build the immutable request while the caller holds the backtest row lock.
|
||||
* Family locks also serialize admission with review/archive changes. Candidate
|
||||
* overrides validate both the granted baseline and the replacement; catalog
|
||||
* approval never turns candidate code into trusted research callbacks.
|
||||
*
|
||||
* The returned request pins a retained package, dataset hash, execution profile,
|
||||
* Rust/worker build and policies. Account identity derives from runId; preview
|
||||
* callers may use a placeholder and discard the request. Execution callers must
|
||||
* persist the request and dispatch intent together, using a new research run ID.
|
||||
*/
|
||||
export async function admitCanonicalRequest(
|
||||
client: PoolClient,
|
||||
container: IServiceContainer,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue