docs: explain strategy replay and durable execution code

This commit is contained in:
Boki 2026-10-25 11:31:23 -04:00
parent 8587c71cf3
commit dd7ac53c51
19 changed files with 494 additions and 41 deletions

View file

@ -1,3 +1,8 @@
/**
* Browser state for an editable backtest template and one selected durable run.
* The API owns execution status/results; this hook owns request lifetimes and
* ignores responses belonging to a previous page or run selection.
*/
import { useCallback, useEffect, useRef, useState } from 'react';
import type { CanonicalRunCapability } from '@stock-bot/stock-config/canonical-research';
import { catalogExecutionReason } from '@/features/strategies/components/StrategySelection';
@ -9,6 +14,7 @@ import type {
Run,
} from '../services/backtestApiV2';
/** Object identity is the ownership token for one status/result request chain. */
interface RunSelection {
id: string;
backtestId: string | null;
@ -23,6 +29,11 @@ const isActive = (run: Run) => ['pending', 'running', 'paused'].includes(run.sta
const messageOf = (error: unknown, fallback: string) =>
error instanceof Error ? error.message : fallback;
/**
* Connects BacktestDetailPageV2 to backtestApiV2. Template loading, run monitoring
* and submission have separate state because each can fail independently.
* WebSocket notifications call refreshRun; they never replace the saved result.
*/
export function useBacktestV2() {
const [backtest, setBacktest] = useState<Backtest | null>(null);
const [runs, setRuns] = useState<Run[]>([]);
@ -35,6 +46,9 @@ export function useBacktestV2() {
const [submissionError, setSubmissionError] = useState<string | null>(null);
const [isSubmitting, setIsSubmitting] = useState(false);
const [cancelRequested, setCancelRequested] = useState(false);
// Generations reject late responses even when an earlier request cannot be
// aborted. The selection generation also prevents submission from taking the
// user away from a different run they selected while admission was pending.
const mounted = useRef(true);
const backtestGeneration = useRef(0);
const loadedBacktestId = useRef<string | null>(null);
@ -92,6 +106,8 @@ export function useBacktestV2() {
});
return () => controller.abort();
}, [backtest, catalog, capabilityRetry]);
// Eligibility belongs to this exact loaded definition, not just its ID: an
// edit must invalidate an earlier successful capability check.
const currentCapability =
capabilityState?.definition === backtest && capabilityState?.retry === capabilityRetry
? capabilityState
@ -111,6 +127,7 @@ export function useBacktestV2() {
[]
);
/** Stop browser observation only; cancelling server execution is cancelRun's job. */
const stopMonitoring = useCallback(() => {
selectionGeneration.current++;
const selected = selection.current;
@ -228,6 +245,7 @@ export function useBacktestV2() {
[refreshSelection]
);
/** Reopening a run reads its recorded state; it never submits another backtest. */
const selectRun = useCallback(
(runId: string | undefined) => {
if (
@ -419,6 +437,12 @@ export function useBacktestV2() {
}
}, []);
/**
* Retain the same key/body after an ambiguous response so Retry can recover
* the accepted run. This receipt is in memory for this mounted hook; it is not
* a persistent browser recovery journal. New admissions recheck eligibility;
* receipt retries recover the original run before current eligibility checks.
*/
const createRun = useCallback(
async (_speedMultiplier: number | null = null) => {
if (!backtest || backtest.id !== loadedBacktestId.current || submission.current?.pending) {
@ -481,6 +505,7 @@ export function useBacktestV2() {
[backtest, catalog, canonicalCapability, capabilityError]
);
/** Record a cancellation request, then monitor until the server reports a terminal state. */
const cancelRun = useCallback(async () => {
const selected = selection.current;
if (