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,4 +1,8 @@
/** Trusted synchronous strategy author contract. No execution or native capabilities. */
/**
* Strategy-facing types for synchronous decisions on the canonical engine.
* StrategyController supplies these values; the native replay host owns time,
* order admission and accounting. Implementations return intent, not broker calls.
*/
import type { DecisionIdentity, FinancialStrategyIntent } from '@stock-bot/types';
import type {
DecisionContext,
@ -13,7 +17,11 @@ export type DeepReadonly<T> = T extends string | number | boolean | null | undef
? { readonly [Key in keyof T]: DeepReadonly<T[Key]> }
: T;
/** Copied native admission outcomes, not a later fill-event stream. */
/**
* Order outcomes copied when the previous decision batch was admitted.
* These are not later fill notifications: inspect current positions and the
* account view to learn what has executed since that decision.
*/
export interface StrategyAdmissionFeedback {
decision: DecisionIdentity;
revision: number;
@ -25,23 +33,35 @@ export interface StrategyAdmissionFeedback {
}[];
}
/** Read-only input to one owner at an engine-defined decision barrier. */
export interface StrategyContext<State = unknown> {
readonly ownerId: string;
readonly strategyId: string;
readonly strategyVersion: string;
/** Monotonic per-owner identity; advances after confirmed batch admission. */
readonly decisionSequence: number;
/** Engine availability time in milliseconds, never the host's wall clock. */
readonly now: number;
/** Due owners, account revision and freshness of the required input series. */
readonly barrier: DeepReadonly<DecisionContext['barrier']>;
/** The exact same read-only account view is shared by all due callbacks. */
readonly portfolio: DeepReadonly<DecisionContext['view']>;
/** Already filtered to this owner; portfolio contains the account-wide view. */
readonly positions: DeepReadonly<DecisionContext['view']['portfolio']['positions']>;
/** Published since this owner's last callback, buffered across slower cadences. */
readonly observations: readonly DeepReadonly<PublishedObservation>[];
readonly previousAdmission: DeepReadonly<StrategyAdmissionFeedback> | null;
/** Last confirmed JSON state, or initialState before the first decision. */
readonly state: DeepReadonly<State>;
/** Valid only during this synchronous callback; never use Math.random for decisions. */
readonly random: () => number;
}
/**
* One strategy instance bound to one configured financial owner and intent mode.
* Keep decision history in explicit state so a fresh attempt can recompute it.
* Native risk rules still apply to every returned intent.
*/
export interface StrategyProgram<State = unknown> {
readonly ownerId: string;
readonly strategyId: string;
@ -50,12 +70,19 @@ export interface StrategyProgram<State = unknown> {
readonly seed: number;
/** Finite plain JSON, checked and detached before the native attempt starts. */
readonly initialState: State;
/**
* Propose the next JSON state and target holdings or explicit order actions.
* A target quantity is this owner's desired total, not an incremental buy.
* Return synchronously: state and seeded RNG advances become confirmed only
* after Rust acknowledges the whole decision batch, including any rejections.
*/
decide(context: StrategyContext<State>): {
nextState: State;
intent: FinancialStrategyIntent;
};
}
/** Confirmed callback state for inspection; it does not contain the native ledger. */
export interface StrategyDiagnostic {
ownerId: string;
strategyId: string;