Commit Graph

5910 Commits

Author SHA1 Message Date
jgrusewski
8c7ce02da9 feat(rl): B-10 policy-quality cascade diagnostic
alpha-rl-8gtk2 (B-7+B-8+B-9 run at SHA 87a8259c6) exposed a degenerate
equilibrium at step 6478 (~32% through 20k train): l_pi diverged 15×
from best (stale 478 steps), l_q + l_v bit-flat (stale 524/615 steps),
π near-uniform (entropy 2.36 / ln(11)=2.40 max, Hold-dominant 32%),
Kelly fraction floored at 0.025 with consistently-negative EV.

Three lit-confirmed candidate root causes (perplexity 2026-06-01):
  α Q-collapse via adaptive C51 EWMA atom support — Bellemare projection
    collapses target distribution to one-hot at center atom under
    EWMA-narrowed [V_MIN_eff, V_MAX_eff], yielding near-zero CE without
    Q having any information content.
  β Q→π distill amplification — softmax(Q/τ) is uniform for ANY τ when
    Q is uniform across actions, so high-τ distill = pure max-entropy
    regularization regardless of τ value.
  γ Heavy-tailed advantage normalization — per-batch standardization
    (Schulman 2017 canonical, computed in `rl_advantage_normalize.cu`,
    publishes var to slot 612) produces 5-10σ outliers under B-7's
    unclamped reward distribution → PPO surrogate magnitude explodes.

B-10 ships PURE OBSERVABILITY across 4 groups (G1 Q-dist informativeness,
G2 Q-distill effectiveness, G3 PPO advantage normalization, G4 PPO
surrogate decomposition). No controller, no behavior change, no atom-span
modification. The falsification criteria predetermine the B-11 path the
next cluster run motivates.

Changes:
- 13 new ISV slots (730-742) in isv_slots.rs; `RL_SLOTS_END = 743`.
- 13 bootstrap entries; fixed-size array `[(usize, f32); 217]` → 230 at
  integrated.rs:3394.
- New kernel `rl_q_distribution_stats.cu` — two entry points
  (`rl_q_distribution_per_batch` + `rl_q_distribution_reduce`) computing
  online Q distribution entropy + E_Q range + |E_Q| max via single-block
  tree-reduce (B-9 pattern). Reads online `q_logits_d` + `atom_supports_d`.
- New kernel `rl_ppo_diagnostic_stats_reduce.cu` — cross-batch reducer
  over 4 per-batch scratches (`ppo_a_norm_pb`, `ppo_ratio_dev_pb`,
  `ppo_ratio_clipped_pb`, `ppo_surrogate_pb`) written by surrogate_fwd.
  Reads slot 612 (var_pre_norm) for σ_used = sqrt(var); reconstructs
  |A_unnorm| stats from |A_norm| × σ_used.
- Modified `ppo_clipped_surrogate.cu` — 4 new pointer params for the
  scratches; writes happen in the same `act == 0` branch that already
  owns per-batch reductions, alongside existing loss_pi_per_b. Loss
  reduce path (`ppo_loss_reduce_b.cu`) and ratio path
  (`ppo_log_ratio_abs_max_b.cu`) untouched.
- Modified `rl_q_pi_distill_grad.cu` — adds 2 inline emits (slot 733
  target entropy, 734 target-π entropy diff) in the existing batch-0
  diagnostic block; controller writes (slot 486 λ, slot 487 τ) untouched.
- 5 new `[B] f32` scratch buffers + 2 new function handles in
  `PolicyHead` + 2 new function handles in `DqnHead`. New launch methods
  `launch_q_distribution_stats` (DqnHead) and
  `launch_ppo_diagnostic_stats_reduce` (PolicyHead) following B-9 pattern.
- Trainer wires: G1 launch after `dqn_head.forward(h_t)` at line 6615;
  G3+G4 reducer call right after `surrogate_forward` at line 5211
  (single call site verified — `surrogate_forward` is invoked once per
  step_with_lobsim_gpu pass).
- 14 new diag leaves under `policy_diagnostic` block: 13 from new slots
  + 1 from existing slot 407 (`rl_q_pi_agree_b` was writing every step
  but never reached diag; per B-10 spec §6 Open Decision 4 audit).
- `EXPECTED_LEAVES` 657 → 671 in `eval_diag_emission.rs`.

Local validation (RTX 3050 Ti, 200+50 b=16 fold-1 smoke):
  * Build clean, release binary 1m 43s.
  * Schema parity test path: train = eval = 671 leaves ✓
  * 11/14 new diag fields populating correctly:
    - q_distill_target_entropy = 2.395 (near-uniform ⇒ G2 working)
    - q_pi_agree_ema evolving 0.41 → -0.80 → 0.36 across training
    - PPO scratches show non-zero ppo_a_norm_mean=0.65 at step 50
      (when advantages have signal) and 0 elsewhere (correct: A=0 at
      most steps with replay still warming).
  * 3/14 fields (G1 q_dist_*) return 0 locally — possible sm_86-specific
    runtime issue with the new `rl_q_distribution_stats` kernel; will
    debug on the next cluster run (H100 sm_90) which is the canonical
    diagnostic environment for B-10's intended cluster validation.

Spec: docs/superpowers/specs/2026-06-01-b10-policy-quality-cascade-diagnostic.md

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-01 12:20:42 +02:00
jgrusewski
033906f213 docs(rl): fix stale C51 V_MAX/V_MIN "ratchet" claims + B-9 test bug
While alpha-rl-8gtk2 (the B-7+B-8+B-9 20k+5k cluster run) was training,
B-9's saturation diag exposed V_MAX_eff dropping from 856.82 (step 482)
to 464.61 (step 817) within the same run. The kernel docstrings and slot
doc-comments uniformly claimed "ratchet (monotone-grow)" semantics —
contradicting the observation by 392 units.

Root cause: wwcsz followup 2026-05-24 (commit landed in
rl_reward_clamp_controller.cu Step 5 only) REPLACED the original ratchet
with a slow symmetric EWMA (α=0.001, half-life ~700 steps) on
`win_bound`/`loss_bound`. Static ratchet was wasting atom resolution on
rare tails (avg rewards in [-5,+5] with span at [-60,+20] → Δz=4, Q
couldn't distinguish "slightly winning" from "slightly losing"). The
EWMA refocuses atom resolution on the ACTIVE range.

The controller's own header was correctly updated at the time. The
documentation drift was in 3 other places — fixed here:

- bellman_target_projection.cu header (lines 47-49): "ratchet
  (monotone-grow)" → accurate EWMA description with cross-references
  + pearl_c51_v_max_freeze_required_for_surfer warning (V_MAX in
  100-200 → trend-follower; past 1000 → degraded).

- rl_atom_support_update.cu line 4: "Companion to the C51 atom-span
  ratchet" → "Companion to the C51 atom-span EWMA".

- isv_slots.rs slot allocation table line 43: "C51 atom-span ratchet
  slots" → "C51 atom-span EWMA slots (α=0.001)".

- isv_slots.rs RL_C51_V_MAX_INDEX / RL_C51_V_MIN_INDEX doc-comments
  (lines 649-668): replaced with accurate EWMA description, observed
  857 → 465 drop example, and asymmetric tracking note (V_MIN_eff
  EWMAs -loss_bound NOT -V_MAX_eff — they only coincide when ratio≈1).

Latent test bug also surfaced + fixed: c51_atom_saturation_diagnostic
assumed `V_MIN_eff = -V_MAX_eff` (line 136). With observed Kelly EMAs
avg_loss=$436 vs avg_win=$872 → ratio ≈ 0.5 → V_MIN_eff ≈ -0.5 ×
V_MAX_eff, the test's bot-rate consistency check would false-fail if
saturation ever became non-zero. Dormant so far (sat=0% in both smoke
and full run). Relaxed the bot-rate assertion to use the v_bound_floor
(-1.0) as the necessary lower bound — correct for any asymmetric ratio
and catches gross drift without false-failing on legitimate adaptation.

No behavior change; pure docstring drift + dormant test bug repair per
feedback_trust_code_not_docs. Compile clean.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-01 11:31:31 +02:00
jgrusewski
87a8259c6e fix(build): ml-backtesting honors CUDA_COMPUTE_CAP for sm_90 on H100
alpha-rl-mbg2n on H100 failed at LobSimCuda::new with
`CUDA_ERROR_NO_BINARY_FOR_GPU` loading book_update.cubin. Root cause:
ml-backtesting/build.rs read only `FOXHUNT_CUDA_ARCH` (set by
lob-backtest-sweep-template) but NOT `CUDA_COMPUTE_CAP` (set by
alpha-rl-template from `nvidia-smi --query-gpu=compute_cap` inside the
compile pod). On H100 it silently fell through to default sm_86; the
sm_86 cubin contains no PTX → no JIT path on sm_90 device.

The docstring claimed "Mirrors crates/ml-alpha/build.rs" — it didn't
(ml-alpha reads CUDA_COMPUTE_CAP). Completing the mirror now: detect_arch
returns sm_<NN> honoring (in order):
  1. CUDA_COMPUTE_CAP numeric env (alpha-rl-template)
  2. FOXHUNT_CUDA_ARCH sm_-prefixed env (lob-backtest-sweep-template)
  3. nvidia-smi --query-gpu=compute_cap at build time
  4. Default sm_86 (RTX 3050 Ti local dev)

Both production argo templates now produce sm_90 cubins on H100 and
sm_89 on L40S without further changes.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-01 11:00:53 +02:00
jgrusewski
29b5acad55 feat(rl): B-9 C51 Bellman-target saturation observability
Under B-7 (clamp default-disabled), per-transition rewards in
bellman_target_projection / bellman_fused_select_project can exceed the
adaptive atom support [V_MIN_eff, V_MAX_eff]; each t_z overshoot is
silently clamped before being mapped onto the discrete support. B-9
publishes the per-step saturation rate + pre-clamp t_z extremes so we
can decide if the atom span has become a bottleneck — without
re-attempting the reverted Fix F atom-widening (pearl_c51_v_max_freeze
_required_for_surfer).

Changes:
- 4 new ISV slots (726-729): top/bot saturation rate + max/min pre-proj.
- bellman_target_projection.cu: both entry points (bellman_target_projection
  AND bellman_fused_select_project per feedback_no_partial_refactor) gain
  4 new [B] f32 pointer params; thread-0 sequential reduction over
  Q_N_ATOMS=21 (odd count rules out symmetric tree-reduce — matches the
  kernel's existing softmax max/sum pattern at lines 157/171).
- New cross-batch reducer cuda/rl_bellman_target_saturation_reduce.cu:
  single block, grid-stride gather + power-of-2 tree reduce, no atomicAdd
  per feedback_no_atomicadd.
- dqn.rs: load reducer cubin, add saturation_reduce_fn handle,
  launch_saturation_reduce method, 4 scratch pointer params on both
  bellman methods.
- integrated.rs: allocate 4 [B] f32 scratch buffers; pass through both
  fused_select_and_project_bellman call sites + launch reducer after each.
  4 new bootstrap entries (213 → 217 fixed-size array).
- build.rs: register new kernel.
- 4 new diag leaves under risk_stack.atom_calibration.target_*. Comment
  distinguishes them from popart.max_abs_reward_ema (different signal:
  Bellman target = r + γ·atom_value, can exceed reward by γ·V_MAX_eff).
- EXPECTED_LEAVES 653 → 657.
- tests/c51_atom_saturation_diagnostic.rs: GPU-oracle test asserts
  4 invariants over 249 rows — rates in [0,1], max≥min, rate>0 ⇒
  overshoot exists, top+bot ≤ 1.

Validation:
- 200+50 b=16 fold-1 smoke clean. Locally V_MAX_eff adapts to ~19.3
  (atom support is ISV-driven via rl_atom_support_update), so saturation
  is 0% in the smoke; both diag leaves emit + invariants hold.
- popart_disaggregation_invariants: still passes (249 rows, identity).
- eval_diag_emission: train = eval = 657 leaves.
- Determinism preserved: kernel adds shared-mem reduction over per-thread
  t_z values that were already computed; target_dist output unchanged.

Spec: docs/superpowers/specs/2026-06-01-b9-c51-atom-saturation-diagnostic.md

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-01 10:31:18 +02:00
jgrusewski
1739d9c173 feat(rl): B-8 popart σ_welford disaggregation + identity invariant
Under B-7 (clamp default-disabled), popart's Welford state now updates
against unclamped magnitudes; σ_effective = max(σ_welford, env.max) can
spike from either source but diag only emitted the combined value. This
blocks attribution of any future σ shocks.

Changes:
- RL_POPART_SIGMA_WELFORD_INDEX (slot 725): Welford-only σ, BEFORE the
  F4 envelope floor at rl_popart_normalize.cu:156. Pure observability —
  no computation change.
- rl_popart_normalize.cu: insert one ISV write between σ_welford
  computation (line 141) and envelope-floor application (line 156).
  Mirrors the existing #define-local-then-write pattern (POPART_SIGMA_INDEX).
- build_diag_value: new leaf popart.sigma_welford in the canonical
  popart block. NOT duplicating max_abs_reward_ema (already emitted at
  risk_stack.regime.popart_envelope.max_abs_reward_ema per
  feedback_single_source_of_truth_no_duplicates).
- EXPECTED_LEAVES 652 → 653.
- Bootstrap array [(usize, f32); 212] → 213 with sentinel 0.0
  (overwritten every step by popart kernel).
- tests/popart_disaggregation_invariants.rs: GPU-oracle test asserts
  identity popart.sigma == max(σ_welford, env.max) across 249 rows
  (200 train + 50 eval), skipping step 0 bootstrap.
- tests/eval_diag_emission.rs: migrate fold-idx 0/n_folds 2 → 1/3
  (the n_folds=2 split picks the first 4 files which don't satisfy
  loader's 1033-snapshot minimum after test_data grew from 2 → 9 files).

Validation:
- popart_disaggregation_invariants passes locally (249 rows OK).
- eval_diag_emission passes locally: train=653 eval=653 leaves.
- B-9 spec at docs/superpowers/specs/2026-06-01-b9-c51-atom-saturation-diagnostic.md
  builds on slots 726-729 next (uncommitted, awaiting implementation).

Spec: docs/superpowers/specs/2026-06-01-b8-popart-calibration-observability-and-floor.md

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-01 10:15:36 +02:00
jgrusewski
55d049ecf4 feat(rl): B-7 ISV-toggle reward clamp (default disabled)
apply_reward_scale.cu's post-scale [-3,+1] clamp was masking ~99.99% of
realized tail magnitudes from the agent's reward signal: local b=16 smoke
showed train pnl_cum_usd −$0.63 (clamp-truncated) vs realized_pnl_cum_usd
−$8,574.63 (raw raw_rewards sum), a 13,600× compression. Per van Hasselt
2016, popart standardization + F4 envelope are designed to handle tail
magnitudes; the clamp fights them.

Changes:
- RL_REWARD_CLAMP_ENABLED_INDEX (slot 724): default 0 (disabled). When 0
  the kernel skips the asymmetric clamp; scaled rewards pass through to
  rewards[b] unchanged. Legacy behavior restored by setting to 1.
- DiagInputs.realized_pnl_cum_usd: parallel counter computed from
  raw_rewards (pre-scale, pre-clamp shaped pnl). Compare against
  trading.pnl_cum_usd to surface clamp-truncation gaps.
- Trainer accumulates realized_pnl_cum_usd in both train + eval loops
  per closed-trade done-step (same pattern as pnl_cum_usd).
- EXPECTED_LEAVES 651 → 652 for the new diag leaf.

Validation:
- 200+100 b=16 fold-1 smoke clean; train leaves = eval leaves = 652;
  realized_pnl_cum_usd diverges from pnl_cum_usd as expected when clamp
  disabled (the reward-hacking gap is now observable).
- compute-sanitizer pending (cluster).

B-8 (popart σ_welford disaggregation) and B-9 (C51 Bellman-target
saturation observability) specs at docs/superpowers/specs/2026-06-01-*
build on this slot allocation (725, 726-729 next).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-01 10:05:59 +02:00
jgrusewski
912f33c6fc test(rl): B-6 invariant regression test — asymmetric Wiener-α / Bayesian shrinkage
GPU-oracle test (per `feedback_no_cpu_test_fallbacks`) validating:

1. Cold-start EMA values match spec defaults (avg_w=1, avg_l=1, wr_ema=0.5
   from B-3 cold_start bootstrap)
2. ISV slot 721/722/723 defaults exposed in diag (α_slow_min=0.001,
   n_full_threshold=30000, cv_gain=1.0)
3. Asymmetric direction holds at cold-start: avg_loss EMA grows ~50× faster
   than avg_win EMA per equivalent observation (because α_fast/α_slow_min
   = 0.05/0.001 = 50). Verified locally: avg_l=$425 vs avg_w=$4.36 at
   train_end (100× empirical ratio — matches expected math under volatile
   batch=16 data)
4. Boundary reset: at eval[1], avg_w=avg_l=1.0 and wr_ema=0.5 (cold_start
   resumed via reset_session_state)

Test result locally:
  cold-start: avg_w=1 avg_l=1 wr_ema=0.5
  ISV slots:  alpha_slow_min=0.001 trust_full=30000 cv_gain=1
  train_end:  avg_w=4.36 avg_l=425.39 dones=131
  eval[1]:    avg_w=1.00 avg_l=1.00 wr_ema=0.500 dones=0
  TEST PASS

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-01 01:19:52 +02:00
jgrusewski
bc9eaac89d fix(rl): B-6 — ISV-driven adaptive asymmetric Wiener-α (Bayesian shrinkage)
B-5 (asymmetric α with static α_slow=0.001) revealed the static parameter
problem: provably bounds cascades (avg_win peak $2k vs B-4's $40k) BUT
over-conservative in train (dckcc step 800: avg_l > avg_w → Kelly says
don't trade → model can't discover edges; wr_ema crashed to 0.145).

The fundamental tension: static α_slow can't satisfy BOTH
  - Train convergence: asymmetry must FADE so model learns from real data
  - Boundary safety: asymmetry must ENGAGE at every fold to prevent cascade

B-6 RESOLVES this via Bayesian shrinkage:

  trust(n)   = min(1, cum_dones / n_full_threshold)           [Phase 1]
  stability  = exp(-CV × cv_gain)                              [Phase 2]
  trust_eff  = trust(n) × stability
  α_slow_eff = α_slow_min + (α_fast − α_slow_min) × trust_eff

Phase 1 (data-quantity): trust grows with cum_dones; reset_session_state
zeroes cum_dones → asymmetry RESUMES at every boundary. Math: at n=0
α_slow_eff = α_slow_min = 0.001 (full skepticism). At n=n_full = 30k
trades: α_slow_eff = α_fast = 0.05 (full standard Wiener).

Phase 2 (data-quality): Welford CV of reward magnitude gates trust. Stable
signal (CV→0): stability=1, trust opens normally. Volatile signal (CV high):
stability→0, asymmetry persists. cv_gain=0 disables Phase 2.

Per-EMA asymmetry direction encodes Kelly safety semantics:
  avg_win:  slow-up (skeptical of wins),    fast-down
  avg_loss: fast-up (admit losses),         slow-down (slow forget)
  wr_ema:   slow-up (skeptical of high WR), fast-down

ISV slots (all signal-derived from cum_dones + Welford):
  721 RL_EMA_ALPHA_SLOW_MIN_INDEX           = 0.001
  722 RL_EMA_TRUST_FULL_THRESHOLD_INDEX     = 30000
  723 RL_EMA_CV_GAIN_INDEX                  = 1.0

Threshold calibration (n_full=30k):
- Train: ~1000 cluster steps for trust to fully open → asymmetry active
  during cold-start (first 30 steps, cascade prevention) then fades.
- Eval: 30 dones/step × 500 eval steps = 15k dones → trust climbs to 0.5
  by eval end → partial protection throughout eval.

Composes:
- B-3 Kelly fractional-trust (Kelly SIZING gated by cum_dones)
- B-6 EMA asymmetric-α (Kelly INPUTS biased conservative by cum_dones)
Both fade as data accumulates; both reset at boundary.

Spec: docs/superpowers/specs/2026-06-01-ema-asymmetric-trust-with-cv-gain.md

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-01 01:13:27 +02:00
jgrusewski
d725b77031 fix(rl): B-5 — asymmetric Wiener-α replaces B-4 caps (math gap fix)
B-4 multiplicative cap (1.5/step) and additive cap (0.05) failed math gap:
1. Multiplicative cap: 1.5^N grows unboundedly over many steps. zh96b
   confirmed avg_win peak still $40,911 (B-4) vs $44,821 (B-3) — only 9%
   reduction at peak despite 12× reduction at early steps.
2. Additive cap on wr_ema: 0.05 > natural Wiener step at α=0.05 (max 0.035
   from p=0.3 to obs=1.0). Cap NEVER engages → no-op.

Fundamental math: per-step rate-caps CANNOT bound EMA convergence to E[X].
For sustained observations, ema → X regardless of any per-step rate-cap
(EMA's natural property).

B-5 ASYMMETRIC WIENER-α — provably biased estimator for Kelly safety:

  avg_win:  alpha = (step_avg > prev) ? α_slow : α_fast
  avg_loss: alpha = (step_avg > prev) ? α_fast : α_slow   // mirror
  wr_ema:   alpha = (step_wr > prev) ? α_slow : α_fast

With α_slow=0.001, α_fast=0.05:

  EMA_N (sustained X in slow direction) = X × (1 - 0.999^N)
  N=100: ema = 9.5% × X
  N=200: 18%
  N=500: 39%
  N=1000: 63%

For avg_win: $40k sustained → ema reaches only $3,800 by step 100 (vs
B-4's $40k peak). Provably biased low → Kelly's b̂ underestimated →
smaller f_safe by construction.

For wr_ema: 100% wr sustained from prev=0.3 → reaches 0.87 in N=694 steps
(vs prior cascade in 140 steps).

Asymmetry direction chosen per-EMA for Kelly safety:
- avg_win:  slow up (skeptical of wins), fast down (correct quickly)
- avg_loss: fast up (admit losses), slow down (slow to forget pessimism)
- wr_ema:   slow up (skeptical of high WR), fast down

Single new ISV slot: RL_EMA_ALPHA_SLOW_INDEX = 0.001. Replaces B-4's
RL_WR_EMA_MAX_DELTA_INDEX (same slot 721, repurposed).

Removed: B-4 cap logic from both kernels (multiplicative + additive).
Single uniform asymmetric-alpha rule. Cleaner than B-4 patchwork.

Math validation prediction: avg_win peak should be ≤ $5k (vs B-4's $40k);
wr_ema peak should be ≤ 0.50 (vs B-4's 0.88).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-01 00:57:58 +02:00
jgrusewski
cbe4869375 fix(rl): B-4 — extend Winsorization rate-cap to Kelly input EMAs
Deep JSONL analysis of x56wn revealed the B-2/B-3 cold-start fixes still
left Kelly inputs (avg_win/loss, wr_ema) vulnerable to Wiener-α cascade:
  - avg_win_ema spiked to $44,820 by step 219 (vs current $1.6k stable)
  - wr_ema oscillated 0.32 ↔ 0.60 across 500-step windows
  - Per-step wr stable 0.29-0.33 but EMA admitted 27-percentage-point swings

Root cause: B-3 fixed the cold_start=1.0 anti-pattern, but Wiener-α=0.05
still admits 5% of arbitrarily large observations into the EMA. From
cold_start=1, a single $45k tail-win lifts ema by 5% × $45k = $2250 in
ONE step. Heavy-tailed magnitude → unbounded EMA variance.

B-4 fix — apply Winsorization rate-caps with EMA-type-appropriate form:

ISSUE A — avg_win/loss EMAs (heavy-tailed positive magnitudes)
==============================================================
Math: Hoeffding bound requires bounded support; heavy-tailed sample mean
is unboundedly biased. Winsorize observations at c × prev. Same multiplicative
rate-cap as pos_max_ema (B-2). REUSES slot 718 — single source of truth
for "magnitude EMA growth cap".

  ema_raw = (1-α) × prev + α × step_avg
  ema_new = min(ema_raw, prev × growth_cap)   // growth_cap = isv[718]

At growth_cap=1.225 (B-2 default): adapts from cold_start=1.0 to 10000×
in ~50 steps. Single $45k observation now caps at $1.225 first step.

ISSUE B — wr_ema (proportion [0,1])
====================================
Multiplicative cap wrong for bounded proportion. Use ADDITIVE cap:
  |ema_new - prev| ≤ max_delta   (default 0.05)

Math (Hoeffding): at batch=1024, σ_binomial = √(p(1-p)/b) ≈ 0.014 for
p=0.3. Cap 0.05 = 3.5σ → P(admit | IID) ≈ 1.2%. Steady-state EMA noise
std ≈ 0.004 (α=0.05) so cap = 12σ_EMA — never noise-triggered.

ISIV-driven: new slot 721 RL_WR_EMA_MAX_DELTA_INDEX = 0.05 (tunable).

Also REMOVED first-observation bootstrap branch in win_rate_ema_update
(was: if prev==SENTINEL → ema = step_wr direct). SENTINEL=0.5 is a
conservative neutral; Wiener-α blend from it is safe.

Generalized principle (deeper than B-2/B-3): every adaptive EMA that
gates a magnitude-sensitive downstream controller needs a rate-limit
on per-step change. Form depends on domain:
  - Unbounded ℝ⁺ (magnitudes): multiplicative cap (Winsorize at c × prev)
  - Bounded proportion [0,1]: additive cap (|Δ| ≤ ε)
  - Signed unbounded: additive cap scaled by EMA magnitude

Validation: cargo check clean. Will validate at cluster against x56wn
(same SHA except B-4 additions) for direct comparison.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-01 00:32:06 +02:00
jgrusewski
42b4898239 fix(rl): B-3 — Kelly fractional-trust schedule + avg_win/loss cold-start
alpha-rl-4xmxm eval analysis revealed the residual -$100M loss was OVERCONFIDENT
SIZING, not σ-explosion. At eval[1]: wr_ema=0.575, avg_win=$1180, avg_loss=$117
(b=10:1), kelly_fraction=1.0 (warmup gate). The controller was sizing at 53% of
capital based on n=1 sample — Hoeffding ε at n=1 is √(ln(40)/2) = 1.36, so the
empirical win-rate has 95% CI half-width of 100%+. Kelly is mathematically
unidentified from this sample.

ISSUE A — binary warmup gate at f=1.0
=====================================
rl_kelly_fraction_controller.cu:45 + rl_fused_controllers.cu:858:
```
if (cumulative_dones < min_trades) kelly = 1.0;  // MAX SIZE during warmup
```
Intent was anti-trade-death (pearl_kelly_trade_stream_death). But forces
maximum Kelly at every fold boundary where parent fix resets cum_dones=0.

ISSUE B — avg_win/loss bootstrap to first observation
=====================================================
rl_avg_win_loss_ema_update.cu:61-65,71-75:
```
if (prev == 0.0f) ema_new = step_avg;  // bootstrap = first observation
```
Same anti-pattern as pos_max_ema (B-2). At eval[1] the first trade pair's
INSTANCE ratio (b=10:1) became the EMA, making Kelly's b̂ wildly biased.

B-3 FIX — fractional-trust schedule with bounded floor:
  trust(n) = max(f_floor, min(1, n / N_full))
  f_safe   = max(f_floor·safety, f_kelly · trust · safety)

Where:
  N_full = 200 trades (Hoeffding-derived: ε=0.10 at 95% confidence)
  f_floor = 0.05 (5% of full Kelly, prevents trade-death)
  safety  = 0.5 (existing fractional-Kelly multiplier)

At n=0: f_safe = 0.05 × 0.5 = 0.025 (2.5% of capital — 21× smaller than
                                       the prior f=1.0 catastrophe)
At n=N_full: f_safe = f_kelly · safety (asymptotic optimality)

Plus B-2 extension: avg_win, avg_loss cold-start to 1.0 (was 0), in both
`with_controllers_bootstrapped` AND `reset_session_state`. Single uniform
Wiener-α update — no first-observation branch.

New ISV slot 720: RL_KELLY_BOOTSTRAP_FLOOR_INDEX = 0.05.
Mirror change to fused_controllers.cu Kelly block (twice-implementation rule).

Local validation (b=16 800+200 fold-1):
- eval[1]: avg_win=1.0, avg_loss=1.0, wr_ema=0.5 (NEUTRAL — NOT bootstrapped
  to first-observation $1180/$117 ratio)
- eval[100]: avg_win=153, avg_loss=12 (controller smoothly adapted via Wiener-α)
- eval pnl: -$169k (similar to prior smokes; local scale doesn't trigger
  the b=1024 catastrophe pattern)

Cluster prediction: Kelly's max sizing during eval should be ~2.5% during
first ~200 trades, then asymptote to safety × kelly. Expected eval pnl
improvement vs 4xmxm's -$100M: at least 5×, target 10×.

Spec: docs/superpowers/specs/2026-05-31-pos-max-ema-cold-start-redesign.md
      (B-3 follow-up appended; full Hoeffding math in spec body)

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-31 23:35:20 +02:00
jgrusewski
16cf9f260c fix(rl): B-2 — pos_max_ema cold-start cascade eliminated, ISV-driven cap
The addendum's rate-cap (B-1) only protected the Wiener-α path; the first-
observation bootstrap branch let cold-start fat-tail events seed pos_max_ema
unbounded. At alpha-rl-4xmxm step 5, a single $947 scaled reward bootstrapped
pos_max_ema=879 directly, cascading through clamp_win → unclamped subsequent
rewards → env.max=11375 by step 37 (1500× the eventual steady-state σ).

B-2 fix per docs/superpowers/specs/2026-05-31-pos-max-ema-cold-start-redesign.md:

1. Bootstrap RL_POS/NEG_SCALED_REWARD_MAX_EMA_INDEX to MIN_WIN=1.0 (was 0)
   in `with_controllers_bootstrapped`. Conservative neutral value → clamp_win
   starts at MARGIN × 1.0 = 1.5 → rewards heavily clipped until adaptation.

2. Remove the `if (ema_prev == 0.0f) ema_new = pos_max;` branch from
   `rl_reward_clamp_controller.cu`. Single uniform update rule (Wiener-α +
   rate-cap) applies from cold-start onward. Mirror change for neg_max_ema.

3. Replace `#define POS_MAX_EMA_MAX_GROWTH_PER_STEP 1.5f` with ISV-driven
   reads (per feedback_isv_for_adaptive_bounds). Three new slots:
     717 RL_POS_MAX_EMA_COLD_START_INDEX             = 1.0
     718 RL_POS_MAX_EMA_GROWTH_CAP_BASE_INDEX        = 1.225 (√1.5 for
                                                       twice-per-step inv)
     719 RL_POS_MAX_EMA_GROWTH_CAP_CV_GAIN_INDEX     = 0.0   (adaptive layer
                                                       disabled by default)

4. Adaptive growth_cap from Welford CV of reward magnitude (slot 615-617)
   when cv_gain > 0: stable regime → tight cap, volatile regime → loose.
   Disabled by default; opt-in via ISV tuning.

Local smoke validation (800+200 fold-1 b=16):
- step 1:  pos_ema=1.0 (initialized, NOT bootstrapped from observation)
- step 5:  pos_ema=1.0 (no observation yet, sparse-skip working)
- step 25: pos_ema=63.8 (vs 1314 without B-2 — 21× reduction)
- step 37: pos_ema=32.5 (vs 7074 without B-2 — 218× reduction)
- env.max @ step 37: 126 (vs 11375 without B-2 — 90× reduction)

Generalizes the pattern: NORMALIZATION EMAs that gate signal magnitudes
should bootstrap to CONSERVATIVE neutral values, NEVER to first observation.
Cross-references pearl_first_observation_bootstrap which needs revision.

Phase 4 audit (other controllers with same anti-pattern) deferred to
follow-up — see spec §4 Phase 4 for the grep target list.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-31 23:09:59 +02:00
jgrusewski
1aa92f57f0 fix(rl): eval-boundary addendum — reward_scale warmed_flag + pos_max_ema rate-cap
The parent eval-boundary fix (72684ed3e) preserved env.max/clamp EMAs but
left the σ explosion intact. Diagnosis from alpha-rl-jnct8 (10k+2500 eval,
fold-1, -$106M eval pnl, σ → 4451 by eval step 5) revealed two pre-existing
mechanisms compounding the eval shock:

ISSUE A — reward_scale snaps to 0.10 at boundary
================================================
rl_fused_controllers' reward_scale block had a bootstrap-fraction-floor
gate: `(cumulative_dones < min_trades) → boot_floor = 0.10`. The intent
was cold-start protection (prevent scale crash before any closed-trade
ground truth). But cumulative_dones (slot 660) is reset by
reset_session_state for Kelly's PREDICTIVE-warmup purpose. At fold
boundary the gate fires again, clamping the preserved scale (0.0046 in
jnct8) UP to 0.10 — a 22× upward jump. Eval rewards are then 22× larger,
overwhelming env.max preservation; σ explodes regardless.

FIX A — decouple via monotonic warmed_flag (slot 716, never reset).
Set ONCE when cumulative_dones first crosses min_trades; persists across
all subsequent fold boundaries. boot_floor reads the flag instead of
re-evaluating trade_count. Math: at cold-start flag=0 → boot_floor=0.10
(original protection preserved). Post-warmup flag=1 → boot_floor=scale_min
(~1e-4) → preserved scale survives boundaries.

ISSUE B — pos_max_ema growth unbounded (train-phase fat-tail spike)
====================================================================
Pre-existing fat-tail behavior: at alpha-rl-jnct8 step 3895 a single
account had pre_clamp scaled reward 724. The clamp's Wiener-α EMA
(α=0.4 floor) admitted 40% of the observation, jumping pos_max_ema
112 → 834 in 5 steps. clamp_win = MARGIN × pos_max_ema followed
magnitude up rather than bounding it; env.max captured the unclamped
reward (121 → 1306). Recovery via slow-decay over 600 steps, but
during the spike PPO gradients were mis-scaled.

FIX B — asymmetric per-step growth cap on pos_max_ema (1.5× max).
Same Schulman-bounded-step pattern as reward_scale's 2% per-step
movement clamp. Decreases unbounded (allows fast recovery from spike).
Bootstrap path unchanged (ema_prev=0 → ema_new=pos_max, no cap).
Math: 5-step max growth = 1.5^5 ≈ 7.6× vs prior uncapped 7.4× in
practice — similar steady-state, bounded transient.

Local validation (b=16, 800+200 fold-1):
- σ preserved across boundary (51.9 → 51.4 at eval[1])
- σ stays bounded in 23-57 range across full eval phase (no 60× explosion)
- scale=0.10 stable across boundary
- warmed_flag stays 0 at b=16 (cumulative_dones never crosses min_trades
  at this scale — flip behavior tested at cluster b=1024)

Spec: docs/superpowers/specs/2026-05-31-eval-boundary-addendum-reward-scale-and-train-spike.md

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-31 22:25:24 +02:00
jgrusewski
72684ed3ef fix(rl): preserve normalization EMAs across eval boundary
reset_session_state was resetting four EMAs that calibrate signal
scale rather than predict behavior:
  - RL_POS_SCALED_REWARD_MAX_EMA_INDEX (clamp scale anchor)
  - RL_NEG_SCALED_REWARD_MAX_EMA_INDEX (clamp scale anchor)
  - RL_REWARD_CLAMP_CLIP_RATE_EMA_INDEX (clamp feedback)
  - RL_POPART_MAX_ABS_REWARD_EMA_INDEX (F4 envelope)

At the train→eval fold boundary this disabled the reward clamp
(cap = pos_max_ema × ratio = 0) and let eval's first ~10 trade
outliers blow up the popart envelope σ 1500× (57 → 4471) over
~14 steps. PPO surrogate (A_unnorm = σ × A_norm) ran with
catastrophically mis-scaled gradients for the next ~1000 eval
steps, accounting for the bulk of the -$185M eval loss at
alpha-rl-6kghr fold 1.

Refines pearl_adaptive_carryover_discipline: RESET PREDICTIVE
EMAs (Kelly, dd, recency) but PRESERVE NORMALIZATION EMAs.

Spec: docs/superpowers/specs/2026-05-31-eval-boundary-normalization-preservation-design.md
Pearl: pearl_popart_reset_at_eval_boundary_shocks_normalization.md

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-31 21:10:59 +02:00
jgrusewski
22e6ddbcac fix(rl): eval_summary aggregates across all b_size accounts (E.4-E.5)
Replaces the broken single-account + scale-mismatch slicing block in
alpha_rl_train.rs eval phase with proper per-account aggregation.

Pre-fix (cluster v11 alpha-rl-8ll7j, b=1024):
  head_before_eval = sim.read_total_trade_count()  // aggregate=1,203,376
  all_records      = sim.read_trade_records(0)     // backtest 0 only, ≤1024
  // 1.2M > 1024 → wrap branch fires → eval_records = ALL 1024 records
  //   from account 0, mixing train+eval trades
  eval_summary: n_trades=1024 pnl=$61,513 wr=0.217 — MEANINGLESS

Post-fix (this commit, b=16 local smoke):
  head_before_per_b = sim.read_per_backtest_trade_counts()
  all_per_b         = sim.read_trade_records_all()
  // for each backtest: slice ring by per-account head_before vs head_after,
  //   handle wrap correctly, aggregate eval-only records
  eval_summary: n_trades=226 pnl=$-41,137 wr=0.376
  n_eval_trades_seen=226 (= captured) n_dropped=0 n_pre_eval_wrapped=0

Local smoke validation (b=16, 1k train + 250 eval, fold 1 ES.FUT all):
  - n_trades jumped 58 → 226 (4× — confirms aggregation across 16 accts)
  - per-account pre-eval heads: min=16 max=59 sum=596 (correct scale)
  - n_eval_trades_seen == n_trades (no wrap at this scale)
  - eval_summary.json gained 4 new fields:
      n_eval_trades_seen, n_eval_trades_dropped,
      n_pre_eval_trades_wrapped, b_size

Existing keys (n_trades, total_pnl_usd, profit_factor, sharpe_ann,
max_drawdown_usd, win_rate) preserved for downstream G8-gate / Argo
aggregator consumers (per spec §3).

Cluster validation pending Phase A cluster (alpha-rl-jz48s) completion
to avoid alpha_rl_train.rs branch conflict. After Phase A merges,
this fix can submit alongside.

Spec: docs/superpowers/specs/2026-05-31-eval-summary-trade-aggregation-design.md
Plan: docs/superpowers/plans/2026-05-31-eval-summary-trade-aggregation.md

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-31 18:09:16 +02:00
jgrusewski
fa0858f0b1 feat(lobsim): per-backtest trade-count read + 4× TRADE_LOG_CAP (E.1-E.3)
Adds two new public methods to LobSimCuda + bumps TRADE_LOG_CAP to
prevent eval-phase ring-buffer wrap at cluster scale.

- read_per_backtest_trade_counts() -> Vec<u32>: cumulative trade
  counters per backtest (length n_backtests). Replaces the broken
  pattern in alpha_rl_train.rs where head_before_eval = aggregate
  across batch was compared against all_records = single-account ring.

- read_trade_records_all() -> Vec<Vec<TradeRecord>>: all backtests'
  rings in one call. Mapped-pinned staging per
  feedback_no_htod_htoh_only_mapped_pinned: allocate
  MappedRecordBuffer<u8> for the payload + MappedRecordBuffer<u32>
  for heads, DtoD copy from device buffers into mapped-pinned
  dev_ptrs, sync, read host_ptrs.

- TRADE_LOG_CAP 1024 → 4096: cluster v11 (alpha-rl-8ll7j) showed
  ~342 eval trades/account mean with peaks toward 1000. 4096 gives
  4× headroom; memory cost b=1024 × cap × 40 B = 167 MB (was 41 MB),
  comfortable on L40S 48GB / H100 80GB.

Both methods synchronize after DtoD so host reads see the data.
E.4 (alpha_rl_train.rs aggregation block replacement) lands
separately after Phase A cluster validates to avoid alpha_rl_train.rs
conflict.

See spec docs/superpowers/specs/2026-05-31-eval-summary-trade-aggregation-design.md
and plan docs/superpowers/plans/2026-05-31-eval-summary-trade-aggregation.md.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-31 18:01:16 +02:00
jgrusewski
ad16e9d941 fix(trainer): address Phase A code review minors (8 fixes)
Per `feedback_always_fix_minor_review_findings`, all 8 minor review
findings from Phase A (eval-diag emission) are applied:

1. Restore aliasing comment for trail_fired_step / conf_gate_step
   (both read RL_CONF_GATE_FIRED_COUNT_INDEX deliberately).
2. Replace act_hist[7/8/9/10] magic indices with `Action::*` enum
   variants in both `IntegratedTrainer::build_diag_value` and the
   train/eval loops in `alpha_rl_train.rs`.
3. Rewrite `recursion_limit = "256"` comment in `lib.rs` to describe
   macro recursion DEPTH (~30 nested object blocks), not leaf count.
4. Move `RL_CONF_GATE_FIRED_COUNT_INDEX`, `RL_PYRAMID_ADD_COUNT_INDEX`,
   `RL_FRD_GATE_FIRED_COUNT_INDEX`, `RL_HEAT_CAP_FIRED_COUNT_INDEX`
   to the top-of-file `use` block (each appears ≥2× as a fully-
   qualified path); 10 call sites switched to bare names.
5. Harmonise leaf-count documentation to 643 (642 scalars + 1 bool
   `pyramid.max_units_reached`) across `build_diag_value` docstring,
   `--eval-diag-jsonl` arg docs, eval-phase comment, and the test
   module-level docstring.
6. Drop `_host` suffix from every `DiagInputs` field (14 fields).
   The struct's docstring already states all slices are host-side;
   the suffix was redundant. `DiagStaging.*_host_ptr` fields are
   NOT renamed — those still refer to actual host pointers.
   28 internal trainer references and 28 call-site references
   updated in lockstep.
7. Align eval_diag_emission test default data dir with foxhunt
   convention: `test_data/futures-baseline/ES.FUT` (resolved from
   `CARGO_MANIFEST_DIR`) instead of `/tmp/rl-smoke-lpi-diag/data`.
   Switch `--instrument-mode` from `front-month` to `all` to match
   the all-instrument predecoded files at that path. Env override
   `FOXHUNT_EVAL_DIAG_DATA` still wins.
8. Add eval-step monotone assertion: verify `eval[0].step == n_steps`
   and `eval[N-1].step == n_steps + n_eval_steps - 1`. Catches a
   regression where the eval loop would reuse the training step
   counter instead of continuing past it.

Verification: `SQLX_OFFLINE=true cargo check -p ml-alpha` clean +
`cargo test --release -p ml-alpha --test eval_diag_emission --
--ignored --nocapture` PASS with curated data (100 train + 50 eval
lines, 643 leaves both phases, schema parity, step axis 100..=149).
2026-05-31 17:49:23 +02:00
jgrusewski
210794626a feat(rl): emit eval-phase per-step diag to eval_diag.jsonl
Cluster run alpha-rl-8ll7j ended with +$61,513 pnl and max_dd -$444,512
but the eval phase emitted ZERO per-step diag, leaving the drawdown
trajectory invisible. Phase A of the 2026-05-31 checkpoints+eval-diag
plan wires the eval loop into the same diag pipeline as train using
the builder extracted in the previous commit.

Changes:
  * `--eval-diag-jsonl <PATH>` CLI flag (defaults to
    `<out>/eval_diag.jsonl`).
  * Eval loop now calls `diag_staging.sync_and_swap` +
    `snapshot_async` after every `step_with_lobsim_gpu`, builds a
    `DiagInputs` from the staging reads, and writes a JSONL line via
    the same `IntegratedTrainer::build_diag_value` the train loop
    uses. Step indices continue past the train phase
    (`cli.n_steps + eval_step`) so post-hoc tooling can concatenate
    train + eval JSONL into a monotone step axis.
  * Eval-phase running counters (pnl_cum_usd, trades, gates, …) are
    independent of train counters so the eval JSONL reflects the
    eval window only — mirrors the trade-record checkpoint that
    eval_summary.json uses.
  * New integration test `eval_diag_emission` validates schema
    parity: same 643 leaf paths in `diag.jsonl` and `eval_diag.jsonl`,
    correct line counts (n_steps / n_eval_steps). Ignored by default
    because it requires CUDA + the pre-built release binary.

Verification (locally on RTX 3050 Ti):
  100 train + 50 eval @ b=16, n_folds=2 →
  `diff <(head -1 diag.jsonl | jq 'paths(scalars)|sort')
        <(head -1 eval_diag.jsonl | jq 'paths(scalars)|sort')`
  returns empty (schema parity confirmed).
2026-05-31 17:26:21 +02:00
jgrusewski
8a93a77adc refactor(trainer): extract json!{} into IntegratedTrainer::build_diag_value
The per-step diag JSONL `json!{...}` block had grown to 642 leaf paths
across ~30 nested objects, duplicating every ISV slot read into the
example binary. Phase A of the 2026-05-31 checkpoints+eval-diag plan
extracts it into a single builder on the trainer so the eval phase
can reuse it (next commit) without duplicating the schema.

Changes:
  * `IntegratedTrainer::build_diag_value(step, elapsed_s, &DiagInputs)
    -> Result<serde_json::Value>` — same 642-leaf schema as before,
    bit-equivalent ISV reads (all from `self.isv_host_slice()`).
  * `DiagInputs<'a>` struct bundles the host-side per-step state the
    trainer doesn't own (DiagStaging reads + running counters +
    windowed act histogram), so the call site stays a 1-liner.
  * Train loop in `alpha_rl_train.rs` swaps the inline json! for the
    builder call; the ~140-slot ISV-imports wall collapses to four
    slots still read by the stderr ticker.
  * `#![recursion_limit = "256"]` moves from the example into
    `ml-alpha/src/lib.rs` since the builder now lives in the library.

Schema parity verified: `head -1 diag.jsonl | jq 'paths(scalars)|sort'`
yields the same 642 keys as before this refactor (no schema drift).
2026-05-31 17:25:47 +02:00
jgrusewski
13d8ed76da docs: checkpoints + eval-diag spec + plans (v1 superseded by v2) 2026-05-31 16:57:43 +02:00
jgrusewski
7b3309edcc fix(rl): eliminate atomicAdd in PPO+DQN loss reduction (F4.1)
Replaces atomicAdd accumulators in `ppo_clipped_surrogate_fwd` and
`dqn_distributional_q_bwd` with per-batch [B] outputs + a dedicated
single-block tree-reduce kernel. Per feedback_no_atomicadd.

Root cause: `ss_pi_loss_dev_ptr` was zero-init at trainer construction
but never reset between steps; the PPO atomicAdd accumulated across
every step since startup. Result: `loss.pi` = step_count × mean_per_step,
bit-exact step-count fingerprints:
  - local 1k × ~9 ≈ 9080 ✓
  - cluster 20k × ~1200 ≈ 24M ✓

The DQN distributional Q kernel had the same atomicAdd pattern. Its
trainer caller happened to memset between launches in dqn_replay_step
so the symptom was masked, but the anti-pattern was identical. Fixed
in the same commit per the user's "atomicAdd should not be used at all"
reminder.

Local smoke (RTX 3050, b=16, 1k steps, seed=16962):

  step | l_pi (pre → post)  | l_q (pre → post)
   100 |   27.5 → 0.56      |  18.1 → 0.19
   500 |  743.1 → 4.59      |  93.3 → 0.19
   999 | 9080.5 → 65.1      | 186.1 → 0.19

l_q now bit-flat at per-step mean (~0.19) — no step-count fingerprint.
l_pi grows organically with policy excursion (PPO surrogate magnitude
when ratio→clamp_max under Q-distillation-driven policy updates),
which is the legitimate diagnostic the prior staleness was burying.

Changes:
  - ppo_clipped_surrogate_fwd: scalar [1] outputs → per-batch [B]
  - dqn_distributional_q_bwd: remove atomicAdd; per_batch[B] only
  - ppo_loss_reduce_b.cu (NEW): two block tree-reduce kernels
      • ppo_loss_reduce_b (dual: PPO loss + entropy loss)
      • mean_reduce_b_f32 (single, reused by DQN head)
  - PolicyHead + DqnHead: load reducer cubin, add reduce_loss_to_scalar
  - Trainer: allocate ss_pi_loss_per_b_d + ss_pi_loss_entropy_per_b_d,
    invoke reducer after each backward (PPO + DQN replay paths)

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-31 14:40:58 +02:00
jgrusewski
30982963ef feat(rl): IQN τ tail-recency consumer + G24/G25 invariants (F5)
When TAIL_EVENT_RECENCY < N_window (default 100), boost τ_min by
factor (default 1.5) — agent uses more pessimistic action selection
during tail-recent regimes.

Defense-in-depth alongside Kelly resurrection (F2): F2 catches the
sizing-layer absorbing state; F5 makes action selection more
risk-averse right after a shock.

Tests:
- G24: TAIL_EVENT_RECENCY < 100 → τ_action ≥ τ_min × 1.5
- G25: TAIL_EVENT_RECENCY ≥ 100 → τ_action behavior unchanged

22 risk_stack_invariants now pass on RTX 3050 Ti.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-31 12:59:48 +02:00
jgrusewski
6dacde95ef feat(rl): popart per-account max-magnitude envelope (F4) — Problem 3 fix
Adds envelope-detector floor on popart_sigma so single-account tail events
within a batch are captured instead of diluted by the batch-mean variance.

Kernel changes (rl_popart_normalize.cu):
- warp_reduce_max helper alongside existing warp_reduce_sum
- Pass 1 extension: per-thread local_max_abs via fmaxf(fabsf(r))
- 3rd shared-mem bank for max_abs reduction (s_max_abs[])
- envelope-detector inside tid==0 block: fast-up, slow-down (α_d=0.01)
- new_sigma = fmaxf(new_sigma, max_r_ema) before write
- CRITICAL (Issue β): Pass 3 broadcast slots moved block_dim*2 → block_dim*3

Trainer launch update: smem_bytes = (block_x * 3 + 3) * sizeof(f32).

Per Theorem 6 (spec v3): at Run #8 step 7377 with r_tail=-19.45, the
envelope captures 19.45 instantly via fast-up path; sigma jumps from
2.327 → 19.45 in one step. The popart_v_correct kernel handles the
σ discontinuity affinely so V regression doesn't destabilize.

Decay phase: α_d=0.01 (69-step half-life); max_r_ema returns to typical
batch-max baseline over ~200 steps after a single shock.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-31 12:50:31 +02:00
jgrusewski
cfe40f2f3f test(rl): G10-G12 back-compat + G15-G21 Kelly resurrection (F2.3+F2.4)
G10/G11/G12: explicit dead-zone disable (DEAD_ZONE_FLAG=0, TIMEOUT_FLAG=0)
before Kelly kernel launch — verifies analytic-Kelly path stays unmodified.

New invariants:
- G15: DEAD_ZONE_FLAG = 1 on composite kelly=0 ∧ all-flat ∧ no-cooldown
- G16: DEAD_ZONE_FLAG = 0 when any condition violated
- G17: Kelly resurrection sets kelly_f = ε_recovery_live when flag set
- G18: Kelly retains analytic value when flag NOT set
- G19: ε_recovery_live ramps linearly from ε_min at T=0 to ε_max at T≥N
- G20: TIMEOUT_FLAG fires when DURATION > MAX_DURATION
- G21: Kelly resurrection NOT triggered when TIMEOUT_FLAG = 1

20 risk_stack_invariants now pass on RTX 3050 Ti.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-31 12:42:16 +02:00
jgrusewski
ad05ffeb2a feat(rl): Kelly resurrection override (F2) — Problem 1 fix
When DEAD_ZONE_FLAG fires (kelly_f=0 ∧ all-flat ∧ no-cooldown), override
Kelly with ε_recovery_live (regime_observer-computed value ramping from
ε_min=0.05 to ε_max=0.50 as TAIL_EVENT_RECENCY grows). TIMEOUT_FLAG
gates resurrection off after MAX_DURATION=1000 steps (safety net).

Per Theorem 1 (spec v3): exit probability from absorbing state per step is
> 1 - 10^-23 given π(any Open) ≥ 0.05 over 1024 accounts. The Kelly
absorbing state no longer exists in the state graph (except the
operator-intended TIMEOUT terminal).

Block appended to end of rl_kelly_fraction_controller.cu (analytic Kelly
clamp first, then conditional override) and mirrored in Layer-4 branch
of rl_fused_controllers.cu.
2026-05-31 12:33:13 +02:00
jgrusewski
bf214d1a09 refactor(rl): v9 slot rename — RL_EVAL_WARMUP_* → RL_REGIME_TRANSITION_* (F3)
Theorem 2 (v9 behavior preserved): pure constant identifier rename across 3 files;
no physical migration. Slots 685/686/687 keep their addresses; v9 kernel,
trainer reset_session_state, and diag emit all reference the same memory.

- crates/ml-alpha/src/trainer/integrated.rs: 4 references renamed
- crates/ml-alpha/cuda/rl_eval_warmup_decay.cu: 3 `#define`s renamed
- crates/ml-alpha/examples/alpha_rl_train.rs: ~12 references renamed (import + diag emit)

Unblocks the 4 pre-existing errors that F1.1 introduced. Branch now compiles
cleanly with regime_observer foundation (F1) + v9 cleanup (F3).
2026-05-31 12:26:54 +02:00
jgrusewski
b09feaf9e4 feat(rl): regime_observer diag emit (F1.8)
Adds 'regime' block to risk_stack diag JSON, surfacing 5 nested groups:
- dead_zone: flag, duration, timeout_flag, max_duration
- tail: recency, session_pnl_variance_ema, sigma_threshold, welford_count
- kelly_eps_recovery: factor, live, min, max, n_recovery
- popart_envelope: max_abs_reward_ema, decay_alpha
- iqn_tau_boost: factor, n_window

All values read directly from ISV slots (drift-free per spec issue #9).
F3 will add the 'transition' group when v9 slot renames land in the diag.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-31 12:17:13 +02:00
jgrusewski
0a9ff73b1f feat(rl): reset_session_state regime boundary policy (F1.7)
Extends reset_session_state with 8 new regime_observer slot resets per
spec section "Regime observer reset policy at fold/eval boundaries":

- Transient state (FLAG/DURATION/TIMEOUT) → 0
- RECOVERY_FACTOR → 1.0
- TAIL_EVENT_RECENCY → 1e6 sentinel
- KELLY_EPS_RECOVERY_LIVE → 0.50 (= ε_max)
- PREV_WORST_PNL → 0 (CRITICAL — prevents spurious 3σ tail at boundary)
- POPART_MAX_ABS_REWARD_EMA → 0 (F4 envelope reset)

Welford state (M2/MEAN/COUNT) intentionally NOT reset — variance of
session_pnl_change is regime-invariant per pearl_adaptive_carryover_discipline.

Issue γ fix: without PREV_WORST_PNL reset, the first regime_observer call
after a fold boundary would see Δworst = 0 - (-\$15k_train) = +\$15k, fire
a spurious 3σ tail event, and peg ε_recovery at ε_min for 100 steps.
2026-05-31 12:15:34 +02:00
jgrusewski
67f862191f feat(rl): regime_observer bootstrap entries (F1.6) — array 179→199
Initial values for the 20 new regime_observer slots per spec v3:
- Transient state: DEAD_ZONE_FLAG/DURATION/TIMEOUT = 0, RECOVERY_FACTOR = 1
- Sentinels: TAIL_EVENT_RECENCY = 1e6, PREV_WORST_PNL = 0, MAX_ABS_REWARD_EMA = 0
- Welford state: M2/MEAN/COUNT = 0 (bootstrap via first-observation, gated count>=10)
- Configs: ε_recovery_min=0.05, ε_recovery_max=0.50, N_recovery=100
- Configs: TAIL_SIGMA_THRESHOLD=3.0, MAX_DURATION=1000, popart α_d=0.01
- IQN τ tail boost: factor=1.5, window=100
2026-05-31 12:14:30 +02:00
jgrusewski
9e97c2acaf feat(rl): wire regime_observer into step_with_lobsim_reward_and_train (F1.5)
Inserts flat_count helper + regime_observer kernel launches BEFORE the
risk-stack controllers (CMDP/IQN/Inventory/Kelly). Uses prev_position_lots_d
(current-step snapshot, just-updated by LobSim per existing pipeline) as
the input to flat_count.

One-step lag on kelly_f/cooldown/worst_pnl reads is benign per Theorem 1:
absorbing state persists across step boundaries, detection at T+1 still
fires before harm escalates.

No consumer changes yet — F2/F3/F4/F5 land independently.
2026-05-31 12:12:46 +02:00
jgrusewski
79b8a43491 style(rl): F1.4 code review fixes — borrow + naming + format
Code-quality review found 1 Important + 2 Minor; applied all 3:
- Important: launch_rl_regime_flat_count(&mut self) → (&self)
  GPU writes through device pointer are invisible to borrow checker;
  matches pattern of launch_rl_win_rate_ema_update (&self) at adjacent line.
- Minor: smem_bytes → smem (matches convention of 11 other launchers)
- Minor: removed column-padding alignment on 5 new struct fields + changed
  `///` doc comment to `//` for private field flat_count_d (matches surrounding
  private fields).

Zero functional change.
2026-05-31 12:11:02 +02:00
jgrusewski
6e0c9abff2 feat(rl): regime_observer trainer wiring — loaders + struct + launchers (F1.4)
Adds:
- include_bytes! for rl_regime_flat_count + rl_regime_observer cubins
- IntegratedTrainer struct fields: 2 modules + 2 functions + flat_count_d helper buffer
- new() cubin loading + struct field assignment
- launch_rl_regime_flat_count(): Grid=(1), Block=(256), smem=256*sizeof(int)
- launch_rl_regime_observer(): Grid=(1), Block=(1), smem=0

Does NOT wire launchers into step_with_lobsim (that's F1.5).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-31 12:05:49 +02:00
jgrusewski
6ded2c55c2 style(rl): F1.3 code review fixes — comments + spec annotations
Code-quality review found 1 Important + 4 Minor; applied all 5:
- I1: restored `(issue #9, drift-free)` cross-link in Phase 4 header
- M1: added explicit launch profile in header `Grid=(1,1,1), Block=(1,1,1), smem=0`
- M2: comment explaining `count` reuse in Phase 3 (Welford state retained in registers)
- M3: inline rationale for `kelly == 0.0f` and `cooldown == 0.0f` exact equality
- M4: extended Welford-bootstrap comment with statistical floor justification
       (exempt from ISV-promotion per "physics/math constants" rule)

Zero functional change — comments only; cubin rebuilds identical bytes.
2026-05-31 12:01:50 +02:00
jgrusewski
b8272221db feat(rl): rl_regime_observer kernel (F1.3)
Single-thread single-block kernel emitting 4 surface signals
+ internal Welford state + drift-free recovery_factor/eps_live.

Spec: docs/superpowers/specs/2026-05-31-regime-observer-design.md v3
Pearls: [[pearl_kelly_trade_stream_death]], [[pearl_dead_signal_resurrection_discipline]]

4 phases:
- 1. Dead-zone detection (composite kelly=0 ∧ all-flat ∧ no-cooldown)
- 2. Welford variance on Δworst_pnl (skips bootstrap, gated count >= 10)
- 3. 3σ tail event detection → resets TAIL_EVENT_RECENCY
- 4. Linear ramp recovery_factor + eps_recovery_live for F2 Kelly consumer

Per feedback_no_atomicadd: single-thread, no atomics.
Per feedback_cpu_is_read_only: pure device kernel.
2026-05-31 11:58:02 +02:00
jgrusewski
8ba9417837 style(rl): F1.2 code review fixes — comment + const + power-of-2 doc
Code-quality review found 1 Important + 3 Minor; applied all 4:
- comment: "warp-aware shared-mem reduction" → "shared-mem block reduction"
  (no __shfl_down_sync used; claim was misleading per reviewer)
- const-correctness: tid is now `const int tid = threadIdx.x` (matches sibling kernels)
- power-of-2 assumption: documented via `#define BLOCK_X 256` + comment
- removed unused `#include <stdint.h>` (kernel uses plain int only)

Zero functional change — kernel emit is identical; cubin rebuilds and passes.
2026-05-31 11:55:06 +02:00
jgrusewski
c76d960645 feat(rl): rl_regime_flat_count kernel (F1.2)
Block-reduce per-account lots[b] → flat_count single int.
Per feedback_no_atomicadd: shared-mem tree-reduce, no atomics.

Foundation for regime_observer (F1.3) which reads flat_count
to compose the dead-zone detection (kelly=0 ∧ all-flat ∧ no-cooldown).

Launch profile (for trainer in F1.4):
  Grid=(1), Block=(256), smem=256*sizeof(int)=1024 bytes.
2026-05-31 11:51:29 +02:00
jgrusewski
6353deed15 style(rl): F1.1 code review fixes — separator + comments
Code-quality review found 2 Important + 2 Minor issues; applied all 4:
- separator style: ────── → ============ (file convention)
- restored split sub-headers: outputs (6) / Welford state (4)
- restored full safety-net comment with issue #4 reference
- added inline value hints on every constant (per spec block)

Zero functional change — comments and whitespace only.
2026-05-31 11:49:37 +02:00
jgrusewski
c1a0143311 feat(rl): regime_observer ISV slot allocation (F1.1)
20 new slots (696-715) + 3 v9 slot renames (685-687).
Per spec docs/superpowers/specs/2026-05-31-regime-observer-design.md v3.

Slot map:
- 696-705: regime_observer outputs + internal Welford state
- 706-709: Kelly resurrection config + live value
- 710: dead-zone timeout config
- 711-712: IQN τ tail-boost config
- 713: tail σ threshold
- 714-715: popart max envelope (F4)

v9 renames are zero-move (same physical addresses):
- 685 RL_EVAL_WARMUP_REMAINING → RL_REGIME_TRANSITION_REMAINING
- 686 RL_EVAL_WARMUP_STEPS_CONFIG → RL_REGIME_TRANSITION_STEPS_CONFIG
- 687 RL_EVAL_WARMUP_DECAY_STEPS_CONFIG → RL_REGIME_TRANSITION_DECAY_STEPS_CONFIG

RL_SLOTS_END: 696 → 716

Downstream sites (eval_warmup_decay.cu, integrated.rs) will show
"cannot find" errors against the renamed v9 constants until F3 updates them.
2026-05-31 11:44:35 +02:00
jgrusewski
baf971ba54 diag(rl): emit v9 eval_warmup state to JSONL + cleanup lints
Diag: surfaces `risk_stack.eval_warmup.{remaining,active,blend,
floor_*,target_*}` so the v9 defensive-warmup window is observable
in diag.jsonl. `remaining` is the counter; `blend` is the
defensive-vs-normal mix coefficient (1.0 = full defensive, 0.0 =
normal); `floor_*` reflect the LIVE override values (read AFTER the
warmup kernel ran). Pre-warmup the kernel is a no-op (remaining=-1),
so v9 train-phase diag is bit-identical to v8.

Cleanup: tightens unreachable_pub items in tests/behavioral/* and
tests/sp5_producer_unit_tests.rs (pub → pub(crate)), removes
unused_mut on 6 sp5 scratch buffers, renames unused `step` loop
counter in alpha_baseline example, and explicitly discards an
intentionally-no-op `Command::assert` in cli_integration_test.
Reduces lint count by ~25; remaining 3 dead_code warnings flag
SP15 Phase 2A behavioral scaffolding (Phase 2B never landed —
deliberate signal, not noise).

Pre-existing pearl per feedback_no_hiding: do NOT suppress these
with #[allow]; the warnings ARE the design call surface.
2026-05-31 02:12:04 +02:00
jgrusewski
0c8cb6ad5b fix(rl): v9 defensive eval-boundary calibration
Implements docs/superpowers/specs/2026-05-31-v9-defensive-eval-boundary-calibration.md.
Closes the [[pearl_adaptive_carryover_discipline]] gap: every adaptive
EMA must reset OR re-bootstrap at regime boundaries, never selectively
preserve train-acquired statistics.

Layer 1 — full EMA reset in `reset_session_state`:
  - win_rate, avg_win/loss EMAs → neutral sentinels
  - cumulative_dones → 0 (re-enter Kelly bootstrap)
  - inventory_beta + inventory_variance EMAs → 0
  - reward_clamp pos/neg max EMAs + clip_rate EMA → 0
  - Kelly fraction → 1.0 (bootstrap)
  Lifts Fix D's deliberate carryover (commit 7064c9269), which was
  diagnosed in v8 fold-1 eval as the primary -$507k driver: agent
  entered eval with train wr=0.34 EMA and took aggressively-sized
  losing trades while EMA decayed to true eval wr=0.25.

Layer 2 — defensive warmup window:
  - New `rl_eval_warmup_decay` CUDA kernel: single-thread single-block,
    no atomics, mapped-pinned only. Runs after fused controllers each
    step. Overrides 4 risk-sizing floors (Kelly safety_frac, IQN τ_min,
    entropy_coef_min, PPO clip ε_min) with conservative defensive
    values for the first 500 steps post-boundary, with linear decay
    over the final 200 steps back to normal targets.
  - 11 new ISV slots (685-695): remaining counter, configured
    durations, defensive overrides (0.25/0.30/0.05/0.10), normal
    targets (0.50/0.10/0.01/0.05).
  - Sentinel counter = -1 at boot → kernel is no-op until reset arms it.

All boundary semantics live in ISV; no hardcoded constants in the
kernel. Build verified, all 13 risk-stack invariants + 5 controller
adaptive-floor tests + integrated-trainer smoke pass on RTX 3050.
2026-05-31 01:52:29 +02:00
jgrusewski
6c4945fe16 docs(spec): v9 defensive eval-boundary calibration (completes adaptive principle)
Per the newly-saved memory pearl pearl_adaptive_carryover_discipline,
diagnoses Fix D's "intentionally preserve train Kelly + inventory EMAs
into eval" as the dominant cause of v8's eval-phase regression.

Design — 3 layers at the train→eval boundary:

  Layer 1: Reset every adaptive EMA (Kelly wr/avg-win/avg-loss/
  cumulative_dones, inventory β/variance, reward-clamp pos/neg/clip
  EMAs) to neutral sentinels. Lets the controllers re-bootstrap from
  eval-distribution observations rather than carrying poisoned train
  state.

  Layer 2: Defensive warmup window (500 steps) overrides risk-sizing
  controllers — Kelly safety_frac 0.5→0.25, IQN τ_min 0.10→0.30,
  entropy coef floor 0.01→0.05, PPO ε floor 0.05→0.10. Linear decay
  back to normal over additional 200 steps.

  Layer 3: 2× LR multiplier during warmup window. Network weights
  adapt fast to new-regime statistics; this is the slowest-adapting
  layer of the agent.

Adds 8 new ISV slots (686-693), bumps RL_SLOTS_END 686→694. New
small kernel rl_eval_warmup_decay applies overrides each step during
warmup. Pure additive design — no kernel logic changes outside the
new warmup-decay kernel.

Validation requires running ALL 3 folds (vs v8's single fold-1) per
pearl_single_window_oos_is_not_oos. Success criterion: mean eval pnl
across 3 folds > v8 mean.

No code change in this commit — design document only. Implementation
deferred until decision is made on whether to ship v9 or first
collect v8's fold-0 + fold-2 baseline for cleaner comparison.
2026-05-31 01:31:46 +02:00
jgrusewski
ad5b29e652 diag(rl): emit atom-span calibration signals (no behavior change)
Per docs/superpowers/specs/2026-05-31-c51-atom-span-math-validation.md,
the binding constraint on atom_max during training is the dynamic
Bellman bound `atom_max ≥ WIN + γ × atom_max`, not the overstated
fixed-point `WIN/(1-γ)`. The math validation against local b=128
smoke confirmed atom_max can be 4.5× the fixed-point bound yet train
cleanly (qpa=+0.969 at step 999).

This commit adds derived diag fields under
`risk_stack.atom_calibration` to expose the bound directly:

  - win_bound, atom_max, gamma         (inputs)
  - dynamic_bound = WIN + γ × atom_max (the binding constraint)
  - atom_max_headroom (=atom_max - dynamic_bound; >0 = self-consistent)
  - popart_sigma, v_target_max_3sigma  (statistical V_target estimate)
  - atom_max_over_3sigma                (resolution waste ratio)

These let future cluster runs measure CURRENT design's over-sizing
empirically (smoke step 999 showed atom_max ~5× larger than 3σ of
V_target requires — wasteful but safe). Future iterations can use
this data to safely tighten atom_max anchor toward V_target_max
without speculating about which design works.

Pure additive diag — no kernel changes, no behavior change. Pulls
values from existing ISV slots (RL_REWARD_CLAMP_WIN_INDEX,
RL_C51_V_MAX_INDEX, RL_GAMMA_INDEX, RL_POPART_SIGMA_INDEX).

Validates the math from 2026-05-31-c51-atom-span-math-validation.md
empirically in every cluster run going forward.
2026-05-31 01:20:37 +02:00
jgrusewski
82572ff3bd docs(spec): C51 atom span math validation + empirical proof
Formal proof of why the current `atom_max = EWMA(WIN_bound)` design
trains successfully despite empirical violations of the "structural
minimum atom_max ≥ WIN/(1-γ)" claim made in 2026-05-30-c51-atom-
resolution-design-alternatives.md.

Key findings (proven by local smoke + cluster v8 trajectory):

1. The fixed-point bound `WIN/(1-γ)` is overstated as a structural
   constraint. Empirical: smoke step 999 atom_max = 4.5× the bound,
   system trains healthy (qpa=+0.969).

2. The actually-binding constraint is the dynamic bound:
   `atom_max ≥ max V_target_observed ≤ WIN + γ × V_max_observed`
   Self-consistent and satisfied with margin 23 at smoke step 999.

3. Both speculative alternatives (Fix F = mean_abs_pnl anchor,
   Fix F-v2 = V_target_observed anchor) have closed-form instability
   at γ(1+ε) ≥ 1 — for γ=0.99 only ε ≤ 0.01 stable, no resolution
   benefit over current design.

4. Current design is *conservative* (atom_max ~5× larger than V_target
   3σ requires) but *correct* — slow EWMA hysteresis absorbs WIN
   transients and keeps Q-V Bellman self-consistent.

Includes empirical trajectory data:
- Local smoke (b=128, HEAD d57bee054, 1000 steps): atom_max 1798→1468,
  qpa +0.65→+0.97, clip_rate 5-11%, dynamic bound satisfied with
  margin 23 at convergence.
- Cluster v8 (b=1024, alpha-rl-vxbpq @ 6d4a962e5): popart_σ collapses
  56→1.69 from step 100→16830, qpa stays >+0.93, pnl_cum $164M.

Saves pearl_atom_span_dynamic_vs_fixed_point for future sessions.

Diagnostic emit of V_target_max_observed proposed (§5) but deferred
— current signals (WIN + γ × atom_max) already provide the bound
indirectly via JSONL. Real-time V_target_max is a nice-to-have for
future calibration work, not required to validate the current design.
2026-05-31 01:13:47 +02:00
jgrusewski
d57bee0542 docs(spec): atom resolution design alternatives (post Fix F failure)
Documents why Fix F crashed at cluster scale (qpa: +0.898 → -0.976
between step 371 and step 500 in alpha-rl-qrgjr v7), the structural
Bellman self-consistency constraint atom_max >= WIN/(1-γ) that makes
the resolution trade-off fundamental, and the design alternatives
considered (non-uniform atoms, adaptive γ, two-headed Q). Recommends
shipping A+B+C+D+E only — the pre-Fix-F design implements the
structurally-correct atom span via WIN-tracking EWMA.

No code change in this commit — design document only. Empirical
result from v8 (alpha-rl-vxbpq, SHA 6d4a962e5) will determine whether
deeper atom-resolution work (Options 4.C or 4.E) is justified.
2026-05-31 00:10:05 +02:00
jgrusewski
6d4a962e5c Revert "fix(rl): decouple C51 atom span from reward-clamp ceiling"
This reverts commit 0fe825a8c5.
2026-05-31 00:01:19 +02:00
jgrusewski
0fe825a8c5 fix(rl): decouple C51 atom span from reward-clamp ceiling
Spec: docs/superpowers/specs/2026-05-30-c51-atom-span-decouple-from-clamp.md

Diagnosed in cluster v5 (alpha-rl-rjsjq SHA 7064c9269) at step 371:
c51_v_max ratcheted up 1 → 24 → 53 → 859 → 1938 while WIN clamp swung
4910 → 568 → 7540 → 44 (volatile from MARGIN saturation). The slow
EWMA tracking win_bound time-averaged the early spikes, locking
atom span at extreme-value magnitude. Δz reached ~184 per atom — Q
could no longer discriminate +$500 from +$5000 wins.

Root cause: atom-span target was anchored on `MARGIN × pos_max_ema`,
where pos_max is the EXTREME-VALUE statistic across b=1024 batch
elements. The clamp bound (rightly fat-tail) and the atom span (should
be typical-magnitude) were treating two distinct concerns as one.

Fix F: anchor atom span on `atom_margin × mean_abs_pnl_ema` — true
central-tendency signal — while leaving the reward clamp on
pos_max_ema. The clamp can still admit fat-tail wins ($42k → +245)
and V regression (scalar head) retains the magnitude for PPO advantage,
but Q's CATEGORICAL distributional support now sizes itself for the
trades where most learning happens.

New ISV slot 685 (RL_C51_ATOM_MARGIN_INDEX, bootstrap 10.0).
RL_SLOTS_END 685 → 686.

Validation (local b=128 1k smoke):
  metric          | pre-Fix-F      | with Fix F
  v_max final      | 1929           | 86.6        (22× tighter)
  Δz per atom      | 184            | 8.7         (resolution restored)
  qpa final        | +0.957         | +0.958      (Q-π alignment preserved)
  mean_active      | +$47k          | +$34k       (within smoke variance)

13/13 risk_stack_invariants + 20/20 trade_management_kernels +
integrated_trainer_smoke pass.

Tradeoff documented in spec: Q loses fat-tail categorical magnitude
(rewards beyond ±87 project to top/bottom atom), but V regression
preserves it. Acceptable for the lottery-ticket strategy where typical-
magnitude resolution matters more than fat-tail magnitude precision.
2026-05-30 23:52:43 +02:00
jgrusewski
1a05af803d fix(cuda): force-close existing position on DD trip / cooldown entry
Cluster v5 alpha-rl-rjsjq step 371 revealed worst-account session_pnl
growing monotonically across cooldown windows (-$3.7k → -$9.3k from
step 100 → 371). Root cause: when DD triggers, actions_to_market_targets
forces {side=2, size=0} — a no-op that suppresses EVERY action type
including trail-stop-driven closes. If the account was holding a
losing position when DD fired, that position bleeds mark-to-market for
the entire 500-step cooldown with no exit path.

Fix: on first DD/cooldown step, emit a closing market order matched to
the current position (side=1 sell for long, side=0 buy for short, size
= |position_lots|). Once flat, subsequent cooldown steps emit the
existing no-op. Net effect: account closes its position at the moment
of DD trip, then sits flat until its recovery clock expires.

Validation: 13/13 risk_stack_invariants pass, 20/20 trade_management
pass, integrated_trainer_smoke passes. Local b=128 1k smoke:
  qpa: +0.95 (best result of session)
  mean_active: +$47k
  worst: stabilizes at -$11.8k (vs unbounded growth pre-fix); residual
  loss is from fat-tail single-step market moves that cross dd_limit
  before the controller can react — out of scope for this fix.
2026-05-30 23:35:54 +02:00
jgrusewski
7064c9269e fix(rl): un-freeze adaptive WIN/LOSS clamp + reset per-batch state on fold
Two compounding bugs found via cluster diag at alpha-rl-gwmkf step 4083:

Fix C — reward-clamp writeback un-freeze:
  rewards.clip_rate_ema = 0.9999 — essentially every win clamped to +1.
  With R-multiple 43 trade outcomes ($7k avg win / $163 avg loss), V
  regression saw identical targets for +$500 wins and +$42k wins; PPO
  advantage lost magnitude signal; policy stagnated at qpa = -0.95.

  Root cause: a 2026-05-24 "G.2" patch in rl_reward_clamp_controller.cu
  added `(void) margin;` and froze WIN=1.0, LOSS=3.0 over concern that
  simultaneously adapting clamp bounds + atom span could create a
  positive feedback loop. Empirically refuted — MARGIN is bounded
  [1.0, 5.0] and pos_max_ema is bounded by reward_scale × raw_PnL, so
  WIN has natural structural ceiling. The freeze was over-conservative.

  Restore the writeback: WIN = max(MIN_WIN, MARGIN × pos_max_ema),
  LOSS = max(MIN_WIN, WIN × RATIO). Atom span Step 5 then follows via
  the slow EWMA already in place.

  Local b=128 1k smoke confirms:
    clip_rate_ema: 0.9999 → 0.094  (target 5%, near hit)
    WIN clamp:     1.0    → 245.6
    V_max atom:    1.0    → 1929
    qpa:           -0.95  → +0.71  (Q and π aligned!)
    mean_active:   +$47k stays positive

Fix D — reset_session_state extends to per-batch buffers:
  The 39efacf77 per-batch CMDP refactor added 4 device buffers but
  reset_session_state() was only zeroing the 4 ISV summary slots. On
  train→eval fold transition, eval inherited all the accumulated
  dead/cooldown accounts from training — eval started with a poisoned
  fleet.

  reset_session_state now also memsets session_pnl_per_batch_d,
  session_dd_triggered_per_batch_d, consec_loss_per_batch_d, and
  cooldown_remaining_per_batch_d to zero. Plus also resets the new
  RL_SESSION_PNL_WORST_INDEX slot.

Validation: 13/13 risk_stack_invariants pass, 20/20 trade_management
pass, integrated_trainer_smoke end-to-end passes.
2026-05-30 23:19:20 +02:00
jgrusewski
5e4c2e62b6 fix(rl): CMDP DD recovery + IQN τ reads mean-of-active (not worst)
Diagnosed via the diag-emit added in 6e0f56816 — alpha-rl-d6d8d step 2000
showed worst-account at -$18k, IQN τ pinned at floor 0.1, popart σ
collapsing 1.00 → 0.47. Root cause: dead-account accumulation in a
sticky-DD fleet.

The per-batch CMDP from 39efacf77 fixed the GLOBAL lockout but left
two failure modes:

  1. Once an account hit `dd_limit`, `session_dd_triggered_per_batch[b]`
     stayed sticky for the fold. `actions_to_market_targets` forced
     those accounts to no-op → V_target ≈ γ·V(s'_unchanged) → popart
     σ leaked variance from accumulating dead-weight.

  2. ISV[RL_SESSION_PNL_USD_INDEX] exposed worst-account pnl, which
     IQN-τ consumes for `drawdown_frac`. A single broken account
     dragged τ to its floor for the entire fold — defensive action
     selection for a fleet that was, on average, doing fine.

Fix A: DD recovery on cooldown expiry. When `dd_limit` trips, also
start a recovery cooldown clock. When the clock decrements to 0,
clear `dd_triggered` + reset `session_pnl` to 0. Account rejoins the
active fleet. Matches surfer trading philosophy: accept the wipeout,
take the forced break, get back on the board.

Fix B: IQN-τ signal split. CMDP now writes mean-of-active-accounts
to RL_SESSION_PNL_USD_INDEX (slot 662, IQN-τ consumer) and mirrors
worst per-batch pnl to a new slot RL_SESSION_PNL_WORST_INDEX (684,
diag only). τ now tracks fleet-typical drawdown instead of a single
catastrophic outlier.

Subtle kernel detail caught by G1: `cool_prev` must be snapshotted
BEFORE the DD-trip section, otherwise a fresh cooldown set this step
gets immediately decremented by 1 in the same launch.

Validation (local b=128 1k smoke):
  metric            | pre-fix (HEAD) | with A+B
  worst pnl          | -$34,016       | -$7,183     (79% less bleed)
  mean active pnl    | n/a            | +$40,343    (typical account profitable)
  IQN τ              | 0.10 (floored) | 0.50        (neutral)
  popart σ           | 0.47→collapsing| 0.84        (variance preserved)
  win rate           | 0.21           | 0.56        (positive edge)

13/13 risk_stack_invariants pass (G1 updated, G1b NEW for cooldown
recovery). 20/20 trade_management_kernels pass. integrated_trainer_smoke
end-to-end passes.

Slot 684 added: RL_SESSION_PNL_WORST_INDEX. RL_SLOTS_END 684 → 685.
2026-05-30 22:40:36 +02:00
jgrusewski
39efacf77d fix(rl): CMDP gates per-batch (one independent session per b)
Each batch element is an independent backtest session with its own
$35k starting capital. The CMDP kernel had been summing per-batch
rewards into a single ISV slot — at b=1024 the accumulator grew
b_size× faster than the -$3500 single-account DD limit, tripping
session_dd_triggered globally in ~250 steps (cluster alpha-rl-2j9k9
step 320: session_pnl_usd=-$5775, all 1024 sessions locked out).
Same flaw on consec_loss_count: any 10 losses across the batch in a
single step triggered cooldown for the entire fleet.

Refactor — every Layer-1 state shard is per-batch:
  session_pnl_per_batch_d            [b_size] f32
  session_dd_triggered_per_batch_d   [b_size] f32
  consec_loss_per_batch_d            [b_size] f32
  cooldown_remaining_per_batch_d     [b_size] f32

`actions_to_market_targets` now reads per-batch DD-triggered + cooldown,
so one account hitting its limit does not lock out the other 1023.

Summary ISV slots (RL_SESSION_PNL_USD, RL_CONSEC_LOSS_COUNT,
RL_SESSION_DD_TRIGGERED, RL_COOLDOWN_REMAINING_STEPS) are now
kernel-OUT only — they expose worst-account / any-triggered / max
aggregates for diag + the single IQN-τ consumer ("be pessimistic
when ANY session is in trouble").

3 raw `actions_to_market_targets_fn.cu_function()` launch sites in
integrated.rs were updated atomically per `feedback_no_partial_refactor`
— public wrapper + 2 internal step_with_lobsim sites. The first
attempt hit CUDA_ERROR_INVALID_VALUE in integrated_trainer_smoke
because only the public wrapper had been updated.

Validation:
- 12/12 risk_stack_invariants pass on per-batch semantics (G1-G4
  rewritten to seed per-batch buffers via write_slice_f32_d_pub).
- 20/20 trade_management_kernels still pass (actions gating intact).
- integrated_trainer_smoke passes end-to-end.
- Local b=128 1k smoke: 1000/1000 steps clean, no NaN. CMDP behavior
  matches design: worst-of-128 hit DD (-$34k > $3500 limit), other 127
  unaffected. IQN τ floored at 0.1 (worst-account drawdown >>10%),
  Kelly = 0.0 (observed edge negative: wr=0.21, R=1.57).
2026-05-30 22:06:22 +02:00