stock-bot/design/development/architecture/decisions/004-strategy-boundary.md
2026-11-27 00:12:48 -05:00

3.1 KiB

ADR-004 — One pull-session boundary, not two strategy frameworks

Status: approved by owner, 2026-10-22 (explicit “yes” to ADR-002/003/004 as written). Session execution is implemented by F07/S01, not by approving this contract direction.

Selected approach

Reuse NAPI-RS in the existing research worker. TS drives a synchronous bounded pull loop, not a reentrant Rust-to-JS callback. Web/API processes never execute the native loop. IPC stays at worker supervision and, later, the untrusted-strategy boundary.

The first strategy path uses reviewed incremental TS calculations directly. A cache/feature DAG is not a prerequisite. Port one SMA strategy first; retain other strategies as explicit legacy artifacts until needed. Do not port every native built-in just to validate NAPI.

Minimal contract

advance(eventBudget) returns one of:

  • progress: no decision; allow cancellation/supervisor progress.
  • decisionBatch: barrier ID/revision, immutable prefix contexts for due strategy instances.
  • complete: canonical ledger/mark/result inputs.

At a decision barrier, collect all due strategies' intents/next states against the same portfolio revision. Submit the batch once; Rust validates owner identity, mode, IDs and admits in the pinned priority order. No early callback can mutate the next callback's view. Target versus explicit-order mode is fixed per strategy instance; duplicate targets consider existing owned commitments.

Each strategy has its own seed/RNG cursor and decision sequence. Stage next state/RNG until submission. An exception discards the staged barrier and fails the attempt; a risk rejection is a valid committed outcome, with feedback to the strategy. On an ambiguous process failure, discard/restart the attempt rather than reuse half-advanced resident state.

Determinism claim, precisely

Controlled clock/random inputs, prefix observations and pinned strategy/runtime/native artifacts produce repeatable fixture traces on the supported platform. Repeated conformance tests cannot prove arbitrary JavaScript deterministic. Trusted code can violate conventions; this is not a security sandbox. Economic replay of a recorded decision trace and recomputation of strategy decisions are separate tests.

A snapshot sufficient for decision recomputation includes ledger/open orders/reservations, replay cursor, decision IDs, strategy states and RNG cursors; any incremental indicators must be checkpointed or deterministically rebuilt from the recorded warmup prefix.

Alternatives and tradeoffs

NAPI already exists; callback registration explicitly fails. The existing native worker supplies killable process isolation. Keep it. No embedded JS runtime, generic transport adapter or mandatory per-observation IPC. A typed native plan remains deferred P06.

F07 introduces the session; S01 drives it; S02 ships one real strategy. F07/S01 extend P01 measurements for actual crossings, context bytes and decisions. P03 cross-trial resident data is optional; loading once per session is ordinary behavior.