Commit Graph

4705 Commits

Author SHA1 Message Date
jgrusewski
84de278dfe feat(sp14): B.2 — register 11 SP14 ISV slots for fold-boundary reset
Each EGF pearl EMA / state slot resets to its Pearl-A sentinel at fold
boundary, mirroring sp13_aux_dir_acc_short_ema / long_ema entries.
Atomic refactor (feedback_no_partial_refactor): both halves land
together — registry entry + reset_named_state dispatch arm.

Reset slots (11 total, sentinel in parens):
  - Q_DISAGREEMENT_SHORT/LONG_EMA (slots 383, 384) → 0.5
  - K_AUX_ADAPTIVE (385) → K_BASE_AUX = 20.0
  - K_Q_ADAPTIVE (386) → K_BASE_Q = 15.0
  - BETA_RATE_LIMITER_ADAPTIVE (387) → BETA_BASE = 0.5
  - AUX_DIR_ACC_VARIANCE_EMA, Q_DISAGREEMENT_VARIANCE_EMA,
    ALPHA_GRAD_RAW_VARIANCE_EMA (388, 389, 390) → 0.0
    (initial k = k_base, β = β_base via ISV-driven controllers)
  - GATE1_OPEN_STATE (391) → 0.0 (closed)
  - ALPHA_GRAD_SMOOTHED (393) → 0.0
  - AUX_DIR_ACC_POST_OPEN_MIN (394) → 1.0 (no min observed)

ALPHA_GRAD_RAW (slot 392, recomputed every step from variance EMAs)
and GRADIENT_HACK_LOCKOUT_REMAINING (slot 395, decays at epoch
boundary) are NOT in the fold-reset registry; both naturally
re-initialise without explicit reset.

Also corrects the isv-slots.md SP14 table: slots 392 and 395 were
incorrectly marked FoldReset in the B.1 entry; corrected to reflect
their actual reset semantics (NOT reset / epoch-boundary decay).

Producer + consumer wiring lands in subsequent tasks (B.3-B.12);
this commit is additive infrastructure only — no behavior change.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-05 18:51:00 +02:00
jgrusewski
d63cb7992e feat(sp14): B.1 — sp14_isv_slots.rs with 13 new ISV slot constants
Allocates ISV slots [383..396) for the Aux→Q Wire + Earned Gradient
Flow pearl (Layer B of SP14). Mirrors sp13_isv_slots.rs pattern.
The plan originally documented [381..394), but Phase 0 verification
found SP13 closeout added HOLD_RATE_TARGET_INDEX=381 and
HOLD_RATE_OBSERVED_EMA_INDEX=382 after the plan was written, so the
range shifts by +2.

Slots fall into 4 functional groups:
- Q-disagreement EMAs (short, long; K=4↔K=2 mapping with Hold/Flat masked)
- Adaptive controllers (k_aux, k_q, β; variance-driven)
- Welford variance EMAs (3, one per adaptive scalar)
- Schmitt state + α_grad outputs + circuit breaker

Plus 14 structural constants for numerical-stability anchors:
K_BASE_*, K_MIN, VARIANCE_REF_*, BETA_BASE, BETA_MAX, SCHMITT_BAND,
WARMUP_STEPS_FALLBACK, LOCKOUT_*, Q_DISAGREEMENT_BASELINE.

Per feedback_isv_for_adaptive_bounds: adaptive bounds (k_*, β,
post_open_min, lockout) live in ISV; numerical anchors live as
structural constants. Per pearl_first_observation_bootstrap: all
EMAs reset to sentinels and Pearl-A bootstraps on first observation.

Producer + consumer wiring lands in subsequent tasks (B.2-B.12);
this commit is additive infrastructure only — no behavior change.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-05 18:45:49 +02:00
jgrusewski
f75786fc5a fix(sp14): A.1 — C51 inv_a_std floor lift (1e-6 → 1e-3)
c51_grad_kernel.cu line 275: lift floor from 1e-6 to 1e-3 in
\`inv_a_std = 1.0f / (a_std + 1e-3f)\`, capping the magnitude-branch
gradient amplifier at 1000 instead of ~1e6 in the degenerate case.

Why: Smoke A produced 1109 GRAD_CLIP_OUTLIER events with C51 grad
reaching 9.5e6 — the SP7 budget controller saturated at the EPS_DIV
floor instead of rebalancing proportionally. Phase-0 verification
against the actual kernel found the amplifier is NOT the spec's
claimed −log(p)/p divide (which does not exist; the kernel uses
the CE-stable expf(lp) - proj form at line 81). The actual
amplifier is inv_a_std = 1/(a_std + 1e-6) at line 275, gated by
\`if (d == 1) grad_val *= inv_a_std\` at line 282. When magnitude
advantage logits collapse near-uniform (Smoke A: var_q=9e-10),
a_std → ~1e-9, so inv_a_std → ~1e6.

Per feedback_isv_for_adaptive_bounds, this is a numerical-stability
anchor (Invariant 1: prevent division-by-near-zero amplification),
not a behavioural bound. ISV-driven bounds govern behavior; the
existing 1e-12f floor on a_std at line 274 is also a structural
anchor — same class of fix.

Validation gate: Smoke A2-A GRAD_CLIP_OUTLIER count <100 in fold 2
(was 1109 pre-fix). The 3-order-of-magnitude reduction in worst-
case amplification should bring C51 grad spikes back under SP7
budget controller authority.

Audit doc: Fix 40 added (parallel to Fix 39 for A.2 and Fix 41 for
A.3); the stale "A.1 deferred" note was removed in the A.3 commit.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-05 18:09:21 +02:00
jgrusewski
1420383212 fix(sp14): A.3 — stagnation warmup gate at fold boundary
compute_aux_w_p0b's stagnation term was firing inappropriately at
fold reset because Pearl-A first-observation bootstrap forces both
EMAs equal:
  short_ema = sentinel 0.5 → first observation X
  long_ema  = sentinel 0.5 → first observation X (same update)
  improvement = max(0, short - long) = 0
  stagnation = (1 - 0/max(deficit, 0.005)) = 1.0
  aux_w *= (1 - 0.7) = 0.3 → spurious decay on a non-stagnation

Fix: add epochs_in_fold parameter; skip stagnation when < 1. Wait
one full epoch for the α=0.3 vs α=0.05 short/long EMA timescale
split to produce real improvement signal.

Atomic refactor (feedback_no_partial_refactor): all 5 callers
migrated — 1 production site (training_loop.rs:4257) plus 4
existing unit tests at trainers/dqn/trainer/tests.rs.

Test: aux_w_stagnation_warmup_gate_epoch_0 verifies:
  - Epoch 0: stagnation = 0, aux_w = base × deficit_amp = 0.625
  - Epoch 1: stagnation = 1.0, aux_w = floor 0.15

Combined with A.2 (clamp lift), Fold 2's aux_w should now hit
0.625 in epoch 0 (the controller's intended post-deficit-amp
value) instead of being collapsed to 0.164.

Audit doc: Fix 41 added; stale "A.1 deferred" note from a prior
killed agent was also removed (A.1 lands in the next commit, not
deferred per user direction).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-05 18:07:12 +02:00
jgrusewski
731cae4c80 fix(sp14): A.2 — lift set_aux_weight clamp to SP13 P0b range [0.15, 1.5]
The pre-fix clamp at gpu_dqn_trainer.rs:14722 was [0.05, 0.3] — the
SP11-era cap. P0b's controller computes aux_w in [0.15, 1.5] (base
0.5 × [0.3, 3.0]) but the setter silently chopped everything above
0.3, masking the deficit-amplification term entirely.

Smoke A trace confirmed:
  Fold 0/1: raw aux_w = 0.66-0.80 → clamped to 0.30 (deficit invisible)
  Fold 2:   raw aux_w = 0.164 (stagnation; below clamp) → 45% deficit

Post-fix: deficit-amp term `(1 + 5 × deficit)` actually expresses
through to the trainer. Fold 2 stagnation will get the designed
floor 0.15 instead of being capped at 0.3 — but the upper range
1.5 also opens, so deficit-amp can pull aux_w up when accuracy is
below target.

Constants imported from sp13_isv_slots.rs (AUX_W_BASE=0.5,
AUX_W_HARD_FLOOR_RATIO=0.3, AUX_W_HARD_CEIL_RATIO=3.0). No new
slots; existing constants exposed as the clamp bounds.

Test: set_aux_weight_clamp_range verifies constants resolve correctly.

A.1 (C51 atom-probability floor) deferred — Phase 0 verification
found the spec's stated `−log(p)/p · ∂p/∂z` divide does NOT exist
in c51_grad_kernel.cu at HEAD 037c24116; actual kernel uses the
numerically-stable `expf(lp) - proj`. The 1109 GRAD_CLIP_OUTLIER
events in Smoke A are real but their mechanism is different. Will
be re-spec'd as a separate task post-SP14.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-05 17:52:18 +02:00
jgrusewski
037c24116d plan(sp14): implementation plan for spec v3
Two-sub-project plan:
- Layer A (4 tasks, ~50 LOC): C51 atom-floor, set_aux_weight clamp lift,
  stagnation warmup gate, smoke validation
- Layer B (15 tasks, ~1180 LOC): 13 ISV slots, 4 kernels (q_disagreement,
  alpha_grad, gradient_hack_detect, dir_concat_qaux), forward wire,
  backward gradient gating, orchestrator wire-up, HEALTH_DIAG, tests, smoke

Each task has bite-sized TDD steps (write failing test, run fail,
implement, run pass, commit) per the writing-plans skill conventions.
Phase 0 verification tasks (A.0, B.0) anchor against current code at
HEAD 40e737a18+ before edits.

19 total tasks across 2 layers. Each layer commits independently as
an atomic feature; intermediate per-task commits during Layer B keep
the wire safety-protected (forward concat lands before backward gating
in B.9 → B.10 sequence).

8 explicit kill criteria for Smoke A2-B per spec B.7.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-05 17:36:56 +02:00
jgrusewski
40e737a181 docs(sp14): spec v3 — K=4 direction + ISV-adaptive β rate limiter
Two further user-flagged corrections from second-critical review:

1. Direction Q-head emits K=4 actions, NOT K=3.
   Authoritative source: state_layout.cuh:123-126
     #define DIR_SHORT 0   // open/maintain short
     #define DIR_HOLD  1   // keep current position (no-op)
     #define DIR_LONG  2   // open/maintain long
     #define DIR_FLAT  3   // close all to zero
   The "default: 3 — Short/Flat/Long" comment at gpu_dqn_trainer.rs:2438
   is stale pre-SP13. Production callsites all set branch_0_size: 4.
   SP13 added DIR_HOLD as a separate fourth direction action (Hold-pricing).
   The config default comment was never updated when DIR_HOLD landed.
   This is exactly the feedback_trust_code_not_docs failure mode — a
   single stale comment would have silently corrupted the q_disagreement
   signal (Hold/Flat being indices 1/3 instead of just Flat=1).

   Updates:
   - B.1 action-space context: 4 actions with Hold AND Flat both
     non-committal (Hold = keep position, Flat = exit to zero)
   - B.2.3 q_disagreement mapping: K=4↔K=2 with both Hold and Flat
     masked from disagreement signal (no new directional commitment
     to evaluate); only Short and Long picks contribute to disagreement
   - Edge case handling for all-Hold/all-Flat batches

2. Adaptive β rate limiter (was structural β=0.9).
   Per feedback_isv_for_adaptive_bounds, β should be signal-driven
   not hardcoded. v3 derives β from variance of α_grad_raw, mirroring
   the k_aux/k_q variance-driven steepness pattern in B.2.5.

   Formula:
     β = clip(β_base + variance_alpha_raw / variance_ref_alpha,
             [β_base, β_max])
     β_base = 0.5 (light smoothing baseline; ~2-step half-life)
     β_max  = 0.95 (heavy smoothing; ~20-step half-life)

   Stable α_grad_raw → β = β_base (preserves directional intent)
   Volatile α_grad_raw → β → β_max (dampens jitter)

   Adds 2 ISV slots:
     ALPHA_GRAD_RAW_VARIANCE_EMA_INDEX (Welford variance)
     BETA_RATE_LIMITER_ADAPTIVE_INDEX (current β value)

ISV slot count: 11 → 13 (net +2 for variance + adaptive β).
LOC estimate: ~1150 → ~1180 (negligible delta; 3 Welford
variances now in alpha_grad_compute_kernel instead of 2).

HEALTH_DIAG pearl_egf_diag emit updated to expose all three
adaptive scalars (α, β, k_aux/k_q) plus all three driving
variances (var_alpha, var_aux, var_q) for full observability.

Verified against current code at HEAD d243a6f08:
  - state_layout.cuh:123-126 (DIR_* enum truth source)
  - gpu_dqn_trainer.rs:2438 (stale K=3 comment confirmed)
  - branch_0_size: 4 in 5 production callsites
    (smoke_tests, gpu_iqn_head, gpu_backtest_evaluator)
  - DIR_HOLD usage in experience_kernels.cu:1298 + 14 other sites

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-05 17:21:17 +02:00
jgrusewski
d243a6f080 docs(sp14): spec v2 — critical review corrections
8 corrections from code-anchored critical review at HEAD eaf4adcb9:

1. Direction Q-head emits K=3 (Short/Flat/Long), not K=4. Aux head
   emits K=2 (down/up). Gate 2 q_disagreement now uses K=3↔K=2 mapping
   with Flat masking. Verified against gpu_dqn_trainer.rs:2438.

2. Forward launch order constraint added: aux forward must complete
   before direction Q-head forward (new serial dep). Cited
   pearl_canary_input_freshness_launch_order.

3. Adaptive sigmoid k formula fixed: v1 had unreachable k_max=50
   because formula caps k ≤ k_base. v2 uses max(..., k_min) with
   k_max = k_base implicit.

4. α_grad rate limiter promoted from nice-to-have to v1. Schmitt
   state-flip introduces sigmoid discontinuity. β=0.9 EMA smoothing
   added; new ALPHA_GRAD_SMOOTHED_INDEX slot.

5. q_disagreement_baseline drop: v1 had adaptive baseline as long-EMA
   (feedback loop risk). v2 uses structural 0.5 (analytic K=3-with-
   Flat-masked random alignment). Drop BASELINE_INDEX slot.

6. Backward gradient scaling clarified: α_grad scales dL/dx (input
   gradient flowing back to aux), NOT dL/dW (Q-head's weight grad).
   Q-head learns to use the wire freely; gate only controls upstream
   flow.

7. 4 hard rules added: feedback_no_hiding,
   feedback_no_htod_htoh_only_mapped_pinned,
   feedback_kill_runs_on_anomaly_quickly,
   pearl_canary_input_freshness_launch_order.

8. Smoke A2 explicit kill criteria table added (8 triggers).

Net ISV slot count unchanged (11), composition shifted: dropped
BASELINE, added SMOOTHED. Total impl cost ~1150 LOC (was ~1060).

Verified against current code:
  - TARGET_DIR_ACC_INDEX=372, AUX_DIR_ACC_SHORT_EMA_INDEX=373,
    AUX_DIR_PREDICTION_INDEX=375 (sp13_isv_slots.rs)
  - set_aux_weight clamp(0.05, 0.3) at gpu_dqn_trainer.rs:14722
    (confirms Bug 3 from Smoke A diagnostic)
  - mag_concat_qdir precedent at experience_kernels.cu:4560
    (direction-conditioning pattern; SP14's wire is the analog)
  - state_reset_registry pattern at lines 913-922 (canonical
    template for new EMA fold-reset entries)
  - branch_0_size = 3 in production config (the K=3 finding)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-05 17:11:04 +02:00
jgrusewski
eaf4adcb98 docs(sp14): spec — aux→Q wire + Earned Gradient Flow pearl
Designs the SP14 chain on top of SP13 Layer B (HEAD 6657e5626):

1. Sub-project A — stability fixes (3 small bugs found in Smoke A)
   - C51 atom-probability floor (ISV-driven from SP4 atom_pos_p99)
   - aux_w setter clamp lift [0.05, 0.3] → [0.15, 1.5]
   - Stagnation warmup gate at fold boundary

2. Sub-project B — the architectural piece (THIS spec)
   - Forward wire: aux_softmax_diff per-bar into direction Q-head input
     concat (in_dim+1, fingerprint bump, zero-init new column)
   - Earned Gradient Flow pearl — adaptive ISV-driven gradient gating:
     * Gate 1 (aux competence) — Schmitt-trigger hysteresis
     * Gate 2 (Q-head disagreement) — NEW signal, EMA per-step argmax
       mismatch
     * ISV-adaptive sigmoid steepness (variance-driven k_aux, k_q)
     * Per-epoch warmup ramp
     * Anti-gradient-hacking circuit breaker (mesa-opt defense)
   - 11 new ISV slots, 3 new GPU kernels, ~1060 LOC total
   - HEALTH_DIAG pearl_egf_diag observability line

3. Sub-project C — Adaptive LR (deferred until A+B effects measured)

Motivation from Smoke A diagnostic:
- aux_dir_acc reached 0.61 (signal extraction works)
- val_win_rate stuck 45-48% (no path to action selection)
- WR-flat-while-aux-varies = Q-head directional weights frozen
- 1109 GRAD_CLIP_OUTLIER events (chronic; not noise)

Three parallel diagnostic agents triangulated three interlocking root
causes:
- Slot 375 has zero readers (the wire was scoped but never built)
- C51 raw grad reaches 9.5e6, saturates SP7 budget controller
- aux_w controller muzzled by SP11-era [0.05, 0.3] clamp

The Earned Gradient Flow pearl is a new application of the codebase's
pearl pattern: ISV-driven adaptive controller, but applied to backward-
pass gradient flow instead of forward-pass features. The wire is one-
way (stop-gradient) by default; co-training is earned by both:
(a) aux head demonstrating label competence, AND
(b) Q-head showing it's actually fighting aux signal (informative
    disagreement above baseline).

Stability additions hardened against:
- Oscillation around target (Schmitt hysteresis)
- Numerical sigmoid saturation (argument clipping ±30)
- Stale variance EMAs across folds (state-reset-registry)
- Discontinuous warmup transitions (linear ramp)
- Mesa-optimization (gradient-hacking circuit breaker)
- Cold-start sentinel-state spurious gate openings (Pearl-A bootstrap)

Awaiting user review before invoking superpowers:writing-plans for
sub-project B (and a separate small plan for sub-project A).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-05 16:59:26 +02:00
jgrusewski
6657e56265 feat(sp13): B1.1b — producer kernel + replay direct path + experience collector
Final piece of the SP13 Layer B chain. B1.1a flipped the aux head from K=1
MSE regression to K=2 softmax CE classification but aux_nb_label_buf was
zero-init — model was training on "all bars are class 0 (down)". B1.1b
lands the producer kernel that fills i32 -1/0/1 labels from the 30-bar
price trajectory, the replay direct-path 8th gather that carries those
labels into the trainer, and the experience collector hoist that ensures
bar_indices_pinned is always populated (producer + hindsight relabel both
consume it). Aux head finally trains on real classification signal.

Recovery commit: this completes a B1.1b agent dispatch that crashed
mid-edit. The implementer had landed ~95% of the cascade (kernel file,
build.rs, replay buffer signature + direct path, fused_training getter,
trainer accessor, both training_loop.rs callers, kernel field + cubin
loader on the experience collector) before being killed. The missing
pieces (experience collector launch + bar_indices_pinned hoist + 6
producer tests + audit doc) were completed manually post-crash and
verified end-to-end.

Three contracts (atomic single commit per feedback_no_partial_refactor):

  1. NEW aux_sign_label_kernel.cu producer — pure per-thread O(1) map
     reading targets[bar*6+2] (raw_close column) at bar and bar+lookahead,
     writing -1 (skip if bar+lookahead >= total_bars), 0 (down/flat under
     strict greater-than tie-break), or 1 (up). Replaces B0 alloc_zeros.

  2. Replay direct-path 8th gather — set_trainer_buffers gains 8th arg
     trainer_aux_sign_labels_ptr; direct branch in sample_proportional
     adds gather_i32_scalar into the trainer ptr; fallback gather wrapped
     in if !direct_to_trainer (avoids wasted DtoD). Direct-mode
     GpuBatchPtrs return points aux_sign_labels_ptr at trainer ptr.

  3. Experience collector bar_indices_pinned cpu-fill hoist — moved out
     of if hindsight_fraction > 0.0 so producer + hindsight share it.

Files (9 total):
  - crates/ml/src/cuda_pipeline/aux_sign_label_kernel.cu (NEW)
  - crates/ml/build.rs (cubin registration)
  - crates/ml/src/cuda_pipeline/gpu_experience_collector.rs (kernel
    field + cubin loader + struct init + hoist + producer launch)
  - crates/ml-dqn/src/gpu_replay_buffer.rs (8th arg + direct gather +
    fallback skip + GpuBatchPtrs return)
  - crates/ml/src/cuda_pipeline/gpu_dqn_trainer.rs (aux_nb_label_buf_ptr
    accessor mirrors 6 existing trainer-buf accessors)
  - crates/ml/src/trainers/dqn/fused_training.rs
    (trainer_aux_sign_labels_buf_ptr getter)
  - crates/ml/src/trainers/dqn/trainer/training_loop.rs (both
    set_trainer_buffers callers updated)
  - crates/ml/tests/sp13_layer_b_oracle_tests.rs (6 NEW producer tests)
  - docs/dqn-wire-up-audit.md (B1.1b section)

Hard rules upheld:
  - feedback_no_partial_refactor: every consumer of the 3 contracts
    migrates atomically
  - feedback_no_atomicadd: producer is pure map; no reductions
  - feedback_cpu_is_read_only: producer GPU-only; only host work is
    pre-existing bar_indices_pinned cpu-fill (hoisted unchanged)
  - feedback_no_stubs: kernel output flows through real chain — ring
    buffer → direct gather → aux_nb_label_buf → CE consumer
  - feedback_no_legacy_aliases: 8-arg setter gets
    #[allow(clippy::too_many_arguments)] not an alias shim
  - feedback_no_htod_htoh_only_mapped_pinned: targets_buf and
    bar_indices_pinned both pre-existing mapped-pinned

Build + test:
  - cargo check --workspace clean (only pre-existing warnings)
  - cargo check --workspace --tests clean
  - 17 tests in sp13_layer_b_oracle_tests.rs:
    - 2 CPU-only (fingerprint bump + HEALTH_DIAG snap stable) pass
    - 15 GPU on RTX 3050 Ti pass (9 B1.1a + 6 new B1.1b producer):
        aux_sign_label_monotone_up_all_ones
        aux_sign_label_monotone_down_all_zeros
        aux_sign_label_flat_all_zeros_strict_gt
        aux_sign_label_last_30_bars_skip
        aux_sign_label_boundary_first_valid_last_skip
        aux_sign_label_multi_episode_per_episode_skip

Producer tests cover every edge case in the kernel:
  - Monotone trajectories (label=1 / label=0 across all valid bars)
  - Flat tie-break (strict greater-than means flat → 0)
  - Skip sentinel for last lookahead bars
  - First/last bar boundary (bar=0 valid, bar=L-1 skip)
  - Multi-episode global skip semantics

Next: Smoke A — L40S 5-epoch validation of full SP13 stack
(P0a + P0b + B0 + B0.1 + B1.0 + B1.1a + B1.1b). Expected: aux head
trains on real K=2 softmax CE labels; aux_dir_acc_short_ema rises above
0.5 within first epoch (vs B1.1a degraded baseline at 0.5);
HEALTH_DIAG aux_b1_diag emits per-epoch with n_down/n_up/n_skip/mask_frac.
If aux_dir_acc_short_ema > 0.55 by epoch 5, B1.1b is validated and the
chain merges to main.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-05 14:45:47 +02:00
jgrusewski
7d10ea8b3e feat(sp13): B1.1a — K=1→2 + softmax CE kernel rewrites + struct flips
Flips the aux next-bar head from K=1 MSE regression to K=2 softmax
cross-entropy classification. Kernel ABIs, struct fields, partial-buf
shapes, and per-step ISV producers all migrate atomically; the producer
that fills `aux_sign_labels` with real -1/0/1 from the price trajectory
lands separately in B1.1b.

Why split B1.1a from B1.1b: the original B1 brief was decomposed (B1.0
+ B1.1) after six full-B1 dispatches confirmed agent-session capacity
is the bottleneck, not cascade understanding. B1.1 itself is now further
split into B1.1a (kernel/struct/test cascade — this commit) and B1.1b
(producer kernel + replay direct path + experience collector hoist +
remaining tests + Smoke A). B1.1a is contract-consistent atomic
kernel-side; B1.1b lands the producer + smoke.

Why labels stay zero-init: B1.1a is producer-less by design. The
`aux_nb_label_buf: CudaSlice<i32>` is `alloc_zeros<i32>` so every
sample receives label 0 ("down"). The model converges on "predict
class 0 (down) everywhere" until B1.1b lands the producer kernel that
fills real -1/0/1 from the 30-bar price trajectory. This degraded
training behavior is intentional and known — the cascade is internally
consistent (every consumer of K-flip / softmax tile / CE / i32 label
migrates atomically per `feedback_no_partial_refactor`); the labels are
placeholder. Local unit tests (CE correctness, dir_acc correctness,
isv_tanh correctness, fingerprint bump) validate B1.1a in isolation;
no L40S smoke runs between B1.1a and B1.1b.

Four contracts (atomic in this commit):
  1. K_NB flip 1 → 2: AUX_NEXT_BAR_K constant, compute_param_sizes
     ([121]/[122] grow), fingerprint seed rename
     (PARAM_AUX_NB_W2/B2 → PARAM_AUX_NB_W2_K2/B2_K2 — bumps the hash),
     forward + backward kernels, partial-buf allocs (nb_w2 [B,H]→[B,K,H],
     nb_b2 [B]→[B,K]), saxpy spec table, max_aux_tensor_len,
     aux_nb_pred_buf renamed to aux_nb_logits_buf per
     feedback_no_legacy_aliases.
  2. Softmax tile: aux_next_bar_forward writes [B, K] softmax via
     in-kernel stable softmax (max-shift form, K=2 single-thread fanout
     mirrors regime kernel); 4 consumers read the tile (loss, backward,
     dir-acc, isv-tanh). NEW field aux_nb_softmax_buf [B, K].
  3. MSE → CE: aux_next_bar_loss_reduce reads softmax + i32 labels,
     masks -1, divides by B_valid (mean-over-valid-rows), writes loss +
     B_valid scalar. aux_next_bar_backward reads B_valid so loss + grad
     share the same divisor — derivatives of the same scalar function.
     NEW field aux_nb_valid_count_buf [1]. All-skip batch produces
     loss = 0 (no NaN; fmaxf(valid, 1) divisor) and zero gradients.
     Numerical floor 1e-30 prevents -log(0) = +inf in extreme-logit path.
  4. i32 label dtype: aux_nb_label_buf flipped f32 → i32; -1 mask
     sentinel handled across loss + backward + dir-acc + isv-tanh.
     The strided_gather of next_states[:, 0] retired entirely.

Cascade (atomic per feedback_no_partial_refactor):
- aux_heads_kernel.cu: aux_next_bar_forward gains K + softmax tile
  output (via in-kernel stable softmax); aux_next_bar_loss_reduce
  ABI flipped (softmax + i32 labels, mean-over-valid CE,
  valid_count_out); aux_next_bar_backward ABI flipped (softmax +
  i32 labels + valid_count, K-fanout d_logits, masked rows zero
  across the K-vector)
- aux_dir_acc_reduce_kernel.cu: read softmax + i32 labels, argmax
  over K, output grew 3 → 6 floats (added n_down/n_up/n_skip);
  shmem 4 → 6 int arrays
- aux_pred_to_isv_tanh_kernel.cu: read softmax tile, compute
  mean(softmax[:, 1] - softmax[:, 0]); tanh transcend retired
  (structural [-1, +1] bound via softmax components per
  pearl_bounded_modifier_outputs_require_structural_activation)
- gpu_aux_heads.rs: AUX_NEXT_BAR_K 1 → 2; forward_next_bar gains K +
  logits_out + softmax_out args; next_bar_loss_reduce gains K +
  valid_count_out; backward_next_bar gains softmax_in + labels_i32_in
  + valid_count_in + K
- gpu_dqn_trainer.rs: compute_param_sizes ([121]/[122]); fingerprint
  seed rename (W2/B2 → W2_K2/B2_K2); struct fields (logits, softmax,
  i32 label, valid_count); aux_dir_acc_buf 3 → 6 floats; partial-buf
  allocs grow; max_aux_tensor_len extended; saxpy spec table updated;
  orchestrator launchers (launch_aux_dir_acc_reduce,
  launch_aux_pred_to_isv_tanh, launch_sp13_aux_dir_metrics) gain K
  arg; strided_gather block deleted entirely
- training_loop.rs: aux_b1_diag HEALTH_DIAG line reads
  aux_dir_acc_buf [3..6] for n_down / n_up / n_skip + mask_frac;
  doc comment update for the per-step aux dir-metrics block
- tests/sp13_phase0_oracle_tests.rs: 6 dir_acc + 3 isv_tanh tests
  rewritten in-place to new ABI (no shadow tests, per
  feedback_no_legacy_aliases)
- tests/sp13_layer_b_oracle_tests.rs (NEW): 11 B1.1a tests — 5 CE
  loss/backward (single-row, batch-mixed, all-skip-NaN, backward
  single-row, backward batch-mixed); 2 dir_acc (handcrafted argmax,
  all-skip NaN-safe); 2 isv_tanh (bounded fuzz, mean handcrafted);
  2 layout regression (fingerprint bump, HEALTH_DIAG snap stable)
- docs/dqn-wire-up-audit.md: B1.1a section

Hard rules upheld:
- feedback_no_partial_refactor: every consumer of K-flip / softmax tile
  / CE / i32 labels migrates atomically — kernels + orchestrators +
  struct + diag + existing oracle tests
- feedback_no_atomicadd: block tree-reduce only; CE loss reduce uses
  2 parallel partial-reduction strips; CE backward uses existing
  per-sample partial → final aux_param_grad_reduce pattern
- feedback_cpu_is_read_only: aux_nb_label_buf is GPU-resident
  CudaSlice<i32>; HEALTH_DIAG aux_b1_diag reads via mapped-pinned
  aux_dir_acc_buf (no DtoH)
- feedback_no_stubs: every new buffer + kernel arg wired through to
  a real consumer; CE forward / loss / backward chain executes
  end-to-end against placeholder labels (degraded behavior, not stub)
- feedback_no_legacy_aliases: aux_nb_pred_buf renamed in-place
  (no shim); PARAM_AUX_NB_W2/B2 renamed to _W2_K2/_B2_K2 in seed
  (no _DEPRECATED alias); 9 existing oracle tests rewritten in-place
- feedback_no_cpu_test_fallbacks: 9 GPU tests gated #[ignore]; 2
  layout-regression tests are CPU-only (pub const + size_of)
- feedback_no_htod_htoh_only_mapped_pinned: every CPU↔GPU buffer
  in tests + production is MappedF32Buffer / MappedI32Buffer
- feedback_isv_for_adaptive_bounds: no hardcoded thresholds added
  (1e-30 numerical floor on log is stability epsilon, not tunable)
- feedback_trust_code_not_docs: 8/8 Phase 0 anchors verified at
  HEAD 75e94858c before editing
- pearl_bounded_modifier_outputs_require_structural_activation:
  softmax IS the structural activation; runtime tanh squash retired

Build: cargo check --workspace --tests clean.
Tests:
  - snapshot_size_is_stable passes at 149*4=596 bytes (B1.1a
    doesn't touch HEALTH_DIAG snap-words)
  - fingerprint_bumped_from_pre_b1_1a passes (proves W2/B2 rename
    flipped seed hash)
  - health_diag_snap_size_stable_at_149_floats passes
  - 9 GPU oracle tests in sp13_layer_b_oracle_tests pass on local
    RTX 3050 Ti (B=1/4)
  - 14 tests in sp13_phase0_oracle_tests pass (6 dir_acc + 3
    isv_tanh rewrites + 5 unchanged hold/EMA tests)

Pre-existing test failures (14) on `cargo test -p ml --lib` are
environmental (OFI test data missing, etc.) and reproduce identically
at HEAD 75e94858c (B1.0) — verified via git stash round-trip.

Net delta: 9 files (8 source + 1 audit doc), +1039 / −539 LOC.

Next: B1.1b — producer kernel + replay direct-path 8th gather +
experience collector hoist + remaining 7 tests (6 producer + 1
end-to-end round-trip) + Smoke A on L40S.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-05 14:09:48 +02:00
jgrusewski
75e94858c5 feat(sp13): B1.0 — ISV[117] retirement + scale-free MSE bridge
Retires ISV[117]=AUX_LABEL_SCALE_EMA_INDEX together with its producer
kernel (aux_label_scale_ema_update), launch site, backward pass-through,
StateResetRegistry entry, HEALTH_DIAG snapshot field, and unit test.

Why: labels at the data layer are z-normalised, so the
mean(|label|) EMA tracked by ISV[117] sits at ~1.0 empirically.
Dividing by max(scale, 1e-6) before the residual `(pred - label)`
reduces to `(pred - label)` within rounding. The divisor was a
defensive scaffold from when the data layer carried mixed-scale
labels (1e-3 log returns vs 5000 raw prices); z-normalisation made
that scaffold redundant.

This is a numerical bridge, NOT the final fix. B1.1 lands on top:
- Aux head 1→2 dim (next-bar regression → 2-class direction logit)
- MSE → CE loss flip
- aux_dir_acc reads softmax over the 2 logits
- aux_pred_to_isv_tanh rewrite as logit-diff
- Producer kernel that fills aux_sign_labels with real -1/0/1 from
  the 30-bar price trajectory (B0 plumbing currently zero-init)
- dqn_param_layout fingerprint bump (head dim changes)
- aux_b1_diag HEALTH_DIAG metric
- 17+ GPU oracle unit tests

Cascade (atomic per feedback_no_partial_refactor):
- aux_heads_kernel.cu: aux_next_bar_loss_reduce + aux_next_bar_backward
  drop `isv` + `isv_label_scale_index` params; residual is (pred - label)
- aux_heads_loss_ema_kernel.cu: aux_label_scale_ema_update kernel deleted
- gpu_aux_heads.rs: kernel field/loader + launch_label_scale_ema +
  isv_* args from next_bar_loss_reduce / backward_next_bar all dropped
- gpu_dqn_trainer.rs: Step 2b producer launch + ISV slot uses dropped;
  AUX_LABEL_SCALE_EMA=117 line retained in fingerprint seed
  (no fingerprint bump in B1.0; B1.1 will bump on head-dim flip)
- gpu_health_diag.rs + health_diag.rs: aux_label_scale snapshot field
  dropped; aux block 4→3 floats, downstream offsets shift down by 1,
  WORD_TOTAL 150→149, snapshot_size_is_stable test 150*4 → 149*4
- health_diag_kernel.cu: WORD_AUX_LABEL_SCALE removed, downstream
  offsets shift, static_assert(WORD_TOTAL == 149)
- state_reset_registry.rs: isv_aux_label_scale_ema FoldReset dropped
- training_loop.rs: reset_named_state arm + HEALTH_DIAG read +
  aux line label_scale field all dropped
- sp4_producer_unit_tests.rs: load_aux_label_scale_ema_kernel helper +
  sp4_aux_label_scale_ema_writes_step_obs_via_pearl_a_then_converges_pearl_d
  test dropped

Hard rules upheld:
- feedback_no_partial_refactor: every consumer of ISV[117] migrates
  atomically — kernel + Rust orchestrator + producer launch + backward
  + HEALTH_DIAG + reset registry + unit test all in this commit
- feedback_no_stubs: not a stub — divisor is removed at every site,
  not aliased through a 1.0_const shim
- feedback_no_legacy_aliases: no legacy AUX_LABEL_SCALE_EMA_INDEX → 1.0
  alias function
- feedback_no_hiding: doc comments forward to B1.1 explicitly; no
  underscore suppression or #[allow(dead_code)]

Build: cargo check --workspace --tests clean.
Tests: snapshot_size_is_stable passes at 149*4=596 bytes.
       cargo test -p ml --lib + cargo test -p ml-dqn --lib compile.

Net delta: 10 files, −288 LOC.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-05 10:52:13 +02:00
jgrusewski
6a869ad366 fix(sp13): B0 cascade gap — 5 unaudited insert_batch call sites
The B0 audit (commit 62ab8ed85) under-counted insert_batch test
callers as 2 (1 production + 1 in-file unit test). Surfaced during
B1.0 implementation when cargo check --workspace --tests failed
with 5 arity-mismatch errors after B0's signature change.

Root cause: B0 audit's grep filter was `grep -v test` and didn't
enumerate crates/ml/src/trainers/dqn/smoke_tests/ (compiled as
part of the lib's test binary, not behind #[cfg(test)]) nor
crates/ml/tests/.

Sites fixed (zero-init i32 alloc, threaded through):
  - crates/ml/src/trainers/dqn/smoke_tests/training_stability.rs:152, 196
  - crates/ml/src/trainers/dqn/smoke_tests/performance.rs:142
  - crates/ml/src/trainers/dqn/smoke_tests/gpu_residency.rs:75
  - crates/ml/tests/gpu_per_integration_test.rs:125

No behavior change — the column carries zero data and no consumer
reads it pre-B1.1. B1.1 lands the producer kernel that fills with
-1/0/1 from the 30-bar price trajectory.

Process correction documented in docs/dqn-wire-up-audit.md
"B0.1 cascade-gap fix-up" subsection: future B-series audits must
run cargo check --workspace --tests before claiming cardinality
completeness.

Build: cargo check --workspace --tests clean.
Tests: cargo test -p ml --lib compiles + passes (GPU tests
#[ignore]-gated).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-05 10:46:20 +02:00
jgrusewski
62ab8ed850 feat(sp13): B0 — replay buffer i32 ring + GpuBatchPtrs plumbing
Pure plumbing: threads a new i32 column (aux_sign_labels) through every
layer of the replay path so B1 can wire the aux head's CrossEntropy
classification target without touching any aggregator or batch-shape
contract on its own. No consumer reads the column yet — all labels are
zero-initialized; smoke between B0 and B1 should be bit-identical to
parent 0ad5b6fa4 modulo allocator entropy.

Why split B0 from B1: original Layer B Commit 1 bundled replay plumbing
with the aux head contract change (1->2 dim, MSE->CE, slot 117 retire,
fingerprint bump). Four implementer subagents in a row hit
NEEDS_CONTEXT on brief-accuracy issues — too large for a single
dispatch. Split keeps B0 mechanical and isolates B1 for fresh
implementer with audit-derived brief.

Wiring (data flow):
  1. scatter_insert_i32 kernel mirrors scatter_insert_u32 but preserves
     -1 sentinels (u32 would alias to 4294967295). gather_i32_scalar at
     #18c is symmetric.
  2. ReplayKernels.scatter_insert_i32 field + ld() loader.
  3. GpuReplayBuffer.aux_sign_labels: CudaSlice<i32> ring [capacity] +
     sample_aux_sign_labels: CudaSlice<i32> per-batch gather dst.
  4. insert_batch signature gains aux_sign: &CudaSlice<i32>. Body
     scatter-launches alongside actions/rewards/dones using same
     (ci, cpi, bsi) tuple.
  5. sample_proportional Step 3c launches gather_i32_scalar from ring
     into sample buffer. Both GpuBatchPtrs return paths set
     aux_sign_labels_ptr to sample buffer raw_ptr.
  6. GpuBatchPtrs.aux_sign_labels_ptr: u64 publicly exposed.
  7. GpuExperienceBatch.aux_sign_labels field — collect_experiences_gpu
     alloc_zeros total-sized i32 slice (B1 replaces with producer kernel).
  8. Single production caller training_loop.rs:2032 threads through.

Audit-verified call site cardinality (per implementer 4 finding):
  - 1 production insert_batch caller (training_loop.rs:2032)
  - 1 in-file unit test caller (signature updated)
  - 2 GpuBatchPtrs construction sites (both inside sample_proportional)
  - 1 GpuExperienceBatch construction site (collect_experiences_gpu)

Implementer 4 caught the over-counted 4 sites claim from the original
brief — the 2 extras were doc-comment references to
insert_batch_with_episode_ids (no separate function exists).

Hard rules upheld:
  - feedback_no_partial_refactor: every insert_batch consumer migrates
    atomically (1 prod + 1 test, both updated)
  - feedback_no_stubs: column carries real data through real kernels;
    zero values are valid i32 that B1 overwrites with -1/0/1
  - feedback_isv_for_adaptive_bounds: no new ISV slots; slot 117
    AUX_LABEL_SCALE_EMA retirement deferred to B1 atomic
  - feedback_no_atomicadd: no new producers/reductions
  - feedback_cpu_is_read_only: alloc_zeros + scatter + gather all GPU

Build: cargo check --workspace clean in 21s.

Audit: docs/dqn-wire-up-audit.md SP13 Layer B B0 section added with
wiring diagram, call site cardinality table, and B1 next-steps for
fresh-implementer dispatch.

Files: 92 LOC net across 4 source files + audit doc:
  - crates/ml-dqn/src/replay_buffer_kernels.cu (+21)
  - crates/ml-dqn/src/gpu_replay_buffer.rs (+54)
  - crates/ml/src/cuda_pipeline/gpu_experience_collector.rs (+17)
  - crates/ml/src/trainers/dqn/trainer/training_loop.rs (+1)
  - docs/dqn-wire-up-audit.md (B0 section)

Next: B1 — aux head 1->2 dim, MSE->CE loss, aux_dir_acc reads softmax,
aux_pred_to_isv_tanh logit-diff rewrite, slot 117 retirement,
dqn_param_layout fingerprint bump, kernel populates aux_sign_labels
with real -1/0/1 labels, aux_b1_diag HEALTH_DIAG, 17+ unit tests.
B1 dispatched to fresh implementer with this audit doc as truth-source.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-05 09:43:00 +02:00
jgrusewski
0ad5b6fa42 chore(sp13): script bug fix + Layer B implementation notes
While P0b smoke (train-sw4ws on bdc5cb8bb) runs, two prep items:

(A) scripts/argo-train.sh — add `ci-training` to the L40S sm_89 case
    arm. Bare `ci-training` is an L40S pool alias in some clusters;
    previously defaulted to sm_90 (Hopper), causing train-mnpf7 to
    deploy with wrong-arch cubins (terminated + resubmitted manually).
    Now both `*l40s*` and bare `ci-training` resolve correctly.

(C) docs/superpowers/plans/...sp13...md — Layer B section expanded
    with concrete codebase locations discovered during P0a:
    - aux_heads_kernel.cu, aux_heads_loss_ema_kernel.cu locations
    - aux_nb_label_buf populated as column 0 of next_states (log_return)
    - F1/F2 regression history note (don't alias the label buffer)
    - aux_pred_to_isv_tanh_kernel.cu is a P0a placeholder per its own
      header — Layer B should rewrite to read softmax logit-diff
    - dir_acc kernel + oracle tests need softmax-read updates
    - Layer B + P0b combined rationale post-P0a empirics

Saves the next implementer ~30 min of re-investigation.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-05 08:27:49 +02:00
jgrusewski
bdc5cb8bb2 feat(sp13): P0b — aux_w deficit+stagnation controller + Hold cost lift
P0a smoke (train-67gqb on f934ea171) returned PARTIAL: aux_dir_acc climbed
0.149 → 0.483 in 10 epochs (signal exists), but val_win_rate stuck at 0.4638
and observed_hold_rate climbed to 0.479 despite controller saturating at
2.4× base. Two findings drive P0b:

(1) aux_w=0.05 (SP11-era clamp) starves the aux head of gradient. Replace
    inverted formula at training_loop.rs SP11 site with deficit+stagnation:
        deficit  = max(0, target - short_ema)
        improve  = max(0, short_ema - long_ema)
        stag     = (deficit > 0.005) ? clamp(1 - improve/deficit, 0, 1) : 0
        aux_w    = base × (1 + 5 × deficit) × (1 - 0.7 × stag),
                   clamped [0.3×base, 3.0×base]
    base 0.05 → 0.5 (10× lift). Stagnation decay prevents permanent
    destabilisation in data-limited case. Formula extracted as host helper
    `compute_aux_w_p0b(target, short, long)` for unit-test coverage.

(2) HOLD_COST_BASE=0.001 was too weak — max cost 0.005/bar × 30-bar hold =
    0.15 cumulative vs ±5-10 reward range = <3% of magnitude. Lift to 0.005
    (max 0.025/bar × 30 bars = 0.75 cumulative ≈ 10-15% of capped reward).
    Genuinely deters lazy Hold without crippling MFT use. Constructor
    static-init unchanged: it still writes the (now-lifted) HOLD_COST_BASE
    constant to slot 380 at fold boundary.

Per pearl_event_driven_reward_density_alignment tension already addressed
in P0a spec; the lift doesn't change the architecture, just the calibration.
Per feedback_isv_for_adaptive_bounds: base/gain/decay/floor/ceil are
numerical anchors; target/short/long EMAs read from ISV.

Tests: 3 new unit tests for controller formula (aux_w_at_target_returns_base,
aux_w_stagnation_decays_to_floor, aux_w_improving_amplifies_above_base).
14 SP13 GPU oracle tests + 14 SP12 reward-math tests still green; no kernel
changes.

Audit-doc updated with P0b section per Invariant 7.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-05 08:10:35 +02:00
jgrusewski
f934ea1719 feat(sp13): P0a atomic — Hold-pricing + dir_acc instrumentation (additive)
Tests user's hypothesis (Hold being FREE is the bug, not Hold itself) by
pricing Hold via ISV-driven adaptive controller targeting 20% Hold-rate.

11 new SP13 ISV slots [372..383). 5 new GPU kernels:
- aux_dir_acc_reduce_kernel.cu (correct/pos_pred/pos_label/valid → 3 scalars)
- hold_rate_observer_kernel.cu (packed batch_actions decode, count(Hold)/B)
- apply_fixed_alpha_ema_kernel.cu (preserves short/long timescale split that
  Wiener-optimal apply_pearls_ad_kernel would collapse)
- aux_pred_to_isv_tanh_kernel.cu (mean(tanh(aux_pred)) → ISV[375])
- 3 reward-composition sites in experience_kernels.cu subtract isv[HOLD_COST]
  on Hold actions (segment_complete pre-asymmetric-cap, positioned-non-event
  per-bar, flat per-bar)

Host-side controller in training_loop.rs:
  excess = max(0, observed - target)
  hold_cost = HOLD_COST_BASE × (1 + 5 × excess), clamped [0.5×, 5.0×base]

Per-step observer + EMA chain in gpu_experience_collector.rs after
experience_action_select. Per-epoch HEALTH_DIAG emit:
  aux_dir_acc target/short/long/pred_tanh
  hold_pricing observed_rate/target/cost

4-way action space stays (ExposureLevel::Hold preserved). Replay buffer /
fxcache compatibility preserved. SP11 (11/11) + SP12 (14/14) tests no
regression. SP13 P0a oracle tests: 14/14 on RTX 3050 Ti.

Spec/plan: docs/superpowers/{specs,plans}/2026-05-04-sp13-redefine-success-for-predictive-skill.md (v3)
Audit: docs/dqn-wire-up-audit.md (SP13 P0a section appended)

v2 → v3 reframe: P0a.T3 v2 implementer's audit found DirectionAction enum
doesn't exist (codebase uses 8-variant fused ExposureLevel cascading through
77 files). v3 reframes from "eliminate Hold" (250 LOC + 32-test cascade) to
"price Hold" (additive, no contract change, no cross-crate cascade).

Tension with pearl_event_driven_reward_density_alignment acknowledged in spec
— per-bar Hold cost is exposure-NEGATIVE (pulls policy AWAY from Hold-default,
inverse of the pearl's failure mode), models real economic carry, ISV-bounded
by controller. Faithful reward modeling, not artificial shaping.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-05 00:52:54 +02:00
jgrusewski
ba83fcd1f5 docs(sp13): v3 spec + plan — Hold-pricing replaces Hold-elimination
P0a.T3 v2 implementer's audit revealed `DirectionAction` enum doesn't exist; the
codebase uses an 8-variant fused `ExposureLevel` (ShortSmall/Half/Full, Hold,
LongSmall/Half/Full, Flat) with cross-crate consumers across 77 files and 32+
test files pinning the 8-variant invariant. Atomic Hold elimination would
cascade massively.

User insight (2026-05-04): Hold being FREE is the bug, not Hold itself. MFT
trading legitimately needs multi-bar holds; we want the model to use them
deliberately, not as a CQL-bias lazy default. Holding isn't free in the real
world — broker fees, margin interest, opportunity cost.

v3 reframes as Hold-pricing:
- 4-way action space stays; ExposureLevel::Hold stays; no cross-crate cascade
- 3 new ISV slots (380-382): HOLD_COST_INDEX, HOLD_RATE_TARGET_INDEX,
  HOLD_RATE_OBSERVED_EMA_INDEX
- Hold-rate observer: small GPU kernel + Pearls A+D smoothing
- Hold-cost controller: 5-line deficit-driven formula
  (excess > target → cost rises 1×→5× base; observed ≤ target → relax)
- Per-bar reward subtraction at action == DIR_HOLD site
- 2 GPU oracle tests for the controller

P0a.T3 cuts from ~250 LOC + 32-test cascade → ~120 LOC additive. T1+T2
already-staged work unchanged. T4/T5/Layer B/C/D structure preserved.

Tension with pearl_event_driven_reward_density_alignment acknowledged in
spec — per-bar Hold cost is exposure-NEGATIVE (away from Hold), models real
economic carry, ISV-bounded by controller. Inverse of the pearl's failure
mode. Faithful reward modeling, not artificial shaping.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-04 23:18:58 +02:00
jgrusewski
0ca45ef61d docs(sp13): v2 spec + plan after critical review
Spec v2 supersedes v1 with five major fixes from the critical review:
- Drop alpha-vs-benchmark reward (mathematically tautological — Long trades had
  alpha = -costs always against always-long benchmark)
- Split Phase 0 into 0a (Hold-only, clean test of user hypothesis) + 0b (aux
  amplification probe, only if 0a partial), avoiding the v1 confounded experiment
- Bound direction-skill bonus relative to |alpha| (cap_ratio × |alpha|) to prevent
  reward gaming on small-alpha correct-direction losers; spec adds 8-quadrant
  worked-example matrix verifying the no-negative-EV invariant
- Replace 5-epoch absolute-threshold gate with 10-epoch trajectory criterion to
  avoid false-negatives from aux head underconvergence
- Aux_w controller adds dual-EMA stagnation detector (decays toward base when
  no improvement) — prevents permanent destabilization of Q-head in
  data-limited case

Plan v2 mirrors spec changes:
- Phase 0a (Hold elimination + dir_acc instrumentation, ~300 LOC, 1 atomic commit)
- Phase 0b (aux_w controller replacement, conditional, ~80 LOC)
- Layer B (aux head regression -> binary classification, ~120 LOC)
- Layer C (skill bonus + luck discount with calibrated bounds, ~150 LOC)
- Layer D (30-epoch validation + 3 new pearls)

ISV slot allocation [372..380): drops slot 371 (BENCHMARK_PNL_CUMULATIVE),
adds 374 (AUX_DIR_ACC_LONG_EMA — stagnation), 379 (SKILL_BONUS_CAP_RATIO).

Plan integrates Explore-agent touch-list (50-70 sites) with corrected enum
ordering: Short=0, Long=1, Flat=2 (preserves codebase Short-first convention,
avoids 30+ stale-comment churn). Adds direction-bias signal handling at
experience_kernels.cu:5159 (Hold's 0.5 softening gate vanishes -> [1, 1, 0]).

Three new pearls planned for Layer D close-out:
- pearl_redefine_success_for_predictive_skill
- pearl_skill_bonus_must_be_alpha_bounded (calibration lesson)
- pearl_reward_quadrant_audit_required (meta-pearl)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-04 22:24:16 +02:00
jgrusewski
a1681abc46 Revert "exp(sp13): aux_w=1.0 override + directional accuracy metric for data investigation"
This reverts commit d2a27a0042.
2026-05-04 22:23:53 +02:00
jgrusewski
d2a27a0042 exp(sp13): aux_w=1.0 override + directional accuracy metric for data investigation
One-shot diagnostic to answer the SP13 root question: does the data have
predictable directional signal at the bar level?

Wires the existing `aux_next_bar_loss_reduce` kernel (which already had a
4-strip shmem reduction emitting `[dir_acc, pos_pred_frac, pos_label_frac]`
into a 3-float output) end-to-end:
  - `gpu_aux_heads.rs`: launcher takes `dir_acc_out_ptr`, allocates 4×AUX_BLOCK
    shmem to back the four parallel reductions.
  - `gpu_dqn_trainer.rs`: adds `aux_nb_dir_acc_buf` (3 f32 device buffer),
    threads it through the loss-reduce launch, exposes `read_aux_dir_acc()`
    accessor for once-per-epoch DtoH readback.
  - `training_loop.rs`: pins `aux_w = 1.0` (instead of the ISV-driven 0.05–0.3
    clamp) so the supervised aux head dominates the loss; emits a new
    `HEALTH_DIAG[ep]: aux_dir_acc accuracy=… pos_pred_frac=… pos_label_frac=…`
    line per epoch.

Verdict thresholds:
  * dir_acc > 55% by ep 5 ⇒ data has signal, DQN failing to use it
  * dir_acc ≈ 50% throughout ⇒ data lacks signal at this timescale
  * dir_acc 60–70% ⇒ strong signal we're not using

EXPERIMENT BRANCH — revert this commit after the investigation reads back the
5-epoch table from smoke logs. All five touch points are tagged
"SP13 data-investigation" / "EXPERIMENT" for clean revert.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-04 21:11:16 +02:00
jgrusewski
1c645264e6 test(sp12): GPU oracle tests for the 3 reward math changes
Adds the GPU oracle test scaffold deferred from commit 17cfbb250.

Refactored 3 reward composition functions into device-inline functions
in trade_physics.cuh for testability without behavior drift:
  - compute_asymmetric_capped_pnl
  - compute_min_hold_penalty
  - compute_lump_sum_opp_cost

experience_kernels.cu's segment_complete branch now calls these helpers
in place of the inline math; the lump-sum opp_cost helper preserves the
production multiplication order via parens so the refactor is bit-
equivalent under f32 rounding to the inline computation it replaces.
The min-hold helper subsumes the original `if (hold_time < target)`
early-exit (returns 0 when at/past target — capped_pnl - shaping_scale
* 0 == capped_pnl).

New test kernel sp12_reward_math_test_kernel.cu exposes the device
functions for GPU oracle testing. The wrapper is single-thread / single-
block (the math is pure register arithmetic) and writes outputs to
mapped-pinned buffers with __threadfence_system() for host visibility,
matching the thompson_test_kernel / sp4_histogram_p99_test_kernel
pattern. Cubin registered in build.rs alongside the other test kernels.

New test module crates/ml/tests/sp12_reward_math_tests.rs covers all
three changes:

  Asymmetric cap (5 tests):
    - clips above pos_cap (+20 -> +5)
    - clips below neg_cap (-20 -> -10)
    - passes through zero
    - exact at pos_cap (+5 -> +5)
    - exact at neg_cap (-10 -> -10)

  Min-hold soft penalty (5 tests):
    - at target -> zero penalty
    - at hold=0,target=30,T=10,max=3 -> 2.25 (factor 30/40 = 0.75)
    - mid-deficit hold=15 -> 1.8 (factor 15/25 = 0.6)
    - past target -> no penalty (early-exit branch)
    - temperature smooths transition (T=10 -> 2.25 vs T=20 -> 1.8)

  Lump-sum opp_cost (4 tests):
    - exiting + position + hold_time -> -0.01
    - zero position -> zero cost
    - zero hold_time -> zero cost
    - negative position -> uses |position| (matches +0.5 magnitude)

Per feedback_no_cpu_test_fallbacks: GPU oracle pattern (synthetic
inputs through real device functions, verified against analytical
expected outputs). No CPU reference impl. Per
feedback_no_htod_htoh_only_mapped_pinned: every CPU<->GPU buffer is
a MappedF32Buffer; zero htod_copy / dtoh_sync_copy. Tests gated with
#[ignore = "requires GPU"] to match the existing sp4/sp5/sp11
producer-test convention.

Verified on RTX 3050 Ti (sm_86):
  SQLX_OFFLINE=true CUDA_COMPUTE_CAP=86 \
    cargo test -p ml --test sp12_reward_math_tests --features cuda \
      -- --ignored --nocapture
  -> 14 passed; 0 failed (3.32s).

Build: SQLX_OFFLINE=true cargo check -p ml --lib --features cuda clean.

Audit doc updated in docs/dqn-wire-up-audit.md (Invariant 7).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-04 19:32:18 +02:00
jgrusewski
17cfbb2503 fix(sp12): per-trade event-driven reward composition
Three architectural changes in one atomic commit per spec ecab584c3:

1. Asymmetric bounded cap (REWARD_NEG_CAP=-10, REWARD_POS_CAP=+5):
   restores prospect-theory loss aversion (2:1 ratio) erased by SP11
   symmetric cap. Per pearl_audit_unboundedness_for_implicit_asymmetry.

2. Min-hold soft penalty with temperature curriculum:
   patience requirement on voluntary exits via deficit/(deficit+T) soft
   factor. Temperature anneals 50->5 across ~100 epochs (curriculum,
   half-life 20 epochs). Trail-fire exits exempted (preserves stop-loss
   design). Penalty flows through r_popart so SP11 controller weighs it
   consistently with other voluntary-exit terms.

3. Zero per-bar shaping (Change 3):
   r_micro = 0 entirely on positioned-non-event bars (per-bar shaping is
   anti-pattern; Q-learning handles credit assignment via TD).
   Per-bar Flat opp_cost zeroed (was the symmetric counterpart to micro).
   r_opp_cost preserved as lump-sum at exit:
     r_opp_cost = -shaping_scale * holding_cost_rate * |position| * hold_time
   on segment_complete (both voluntary and trail-fire). Preserves
   carrying-cost economic concept without per-bar density bias.
   Per pearl_event_driven_reward_density_alignment.

Empirical motivation: train-multi-seed-pmbwn 50-epoch on 6a259942e
showed sharpe-gaming (PnL -30% over 8 epochs while sharpe held at 80).
Three causes: lost loss aversion (Change 1), per-bar gradient (Change
3), no commitment (Change 2). Unified per-trade event-driven design
fixes all three together.

Constants live in state_layout.cuh as Invariant-1 numerical anchors
(REWARD_POS_CAP, REWARD_NEG_CAP, MIN_HOLD_TARGET, MIN_HOLD_PENALTY_MAX,
MIN_HOLD_TEMPERATURE_{START,END,DECAY}). Min-hold temperature is
recomputed in Rust per epoch via min_hold_temperature_for_epoch in
training_loop.rs and passed as a launch scalar. New HEALTH_DIAG line
sp12_event_reward emits the constants per epoch alongside sp11_reward.

Phase 1 = constants only. Phase 2 (ISV adaptive bounds per
feedback_isv_for_adaptive_bounds) deferred until validation results
indicate adaptive need.

LOC: ~344 added (includes spec-required documentation comments)
across experience_kernels.cu, state_layout.cuh,
gpu_experience_collector.rs, training_loop.rs, dqn-wire-up-audit.md.

Build: SQLX_OFFLINE=true cargo check -p ml --lib clean.
Tests: SQLX_OFFLINE=true cargo test -p ml --lib — 938 passed,
13 failed (same 13 failures pre-existing on HEAD ecab584c3, none
related to SP12 changes).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-04 19:04:26 +02:00
jgrusewski
ecab584c32 spec(sp12): per-trade event-driven reward composition (v3)
Three architectural changes in one unified design to push the model from
HFT noise extraction (62% trade rate, sharpe-gaming) toward MFT alpha-hunting:

1. Asymmetric bounded cap (-10/+5) — restores loss aversion erased by
   SP11 symmetric cap. 2:1 ratio matches Kahneman/Tversky prospect theory.
   Anchor: pearl_audit_unboundedness_for_implicit_asymmetry.

2. Min-hold soft penalty with temperature curriculum — patience requirement
   at exit. Soft factor = deficit/(deficit+T), T anneals 50→5 over 50 epochs.
   Forces commitment without paralysis in early training.

3. Zero per-bar shaping (gate micro/opp_cost on events) — eliminates
   continuous-reward gradient that pulls toward continuous exposure.
   Anchor: pearl_event_driven_reward_density_alignment.

Combined: reward fires only on trade events with prospect-theory loss
aversion + commitment requirement. Pure per-trade event-driven Q-learning
properly aligned with per-trade P&L objective.

~50 LOC across 3-4 files. No new ISV slots in Phase 1 (constants only).
Cost ~€1.30 (€0.30 smoke + €1.00 30-epoch validation).

Empirical motivation: train-multi-seed-pmbwn 50-epoch on commit 6a259942e
showed sharpe-gaming pattern (PnL -30% over 8 epochs while sharpe held).
SP11 cap fix unmasked the per-bar shaping bias plus erased loss-aversion
that the unbounded loss path was implicitly providing.

Continues on sp11-reward-as-controlled-subsystem branch — SP12 is
architectural continuation of SP11, not separate work.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-04 18:41:57 +02:00
jgrusewski
6a259942e9 docs(sp11): close-out — pearl_symmetric_clamp_audit + audit doc
Documents the SP11 sharpe-degradation root cause investigation:
  - 35db31089: symmetric reward cap (THE bug)
  - 348f6078b: plan_isv symmetric clamp mirrors (4 sites)
  - b92dcc3df: diagnostic instrumentation cleanup
  - this commit: close-out

New memory pearl: pearl_symmetric_clamp_audit
  Distinct from pearl_bounded_modifier_outputs_require_structural_activation:
  - That pearl: model OUTPUTS need structural activation (sigmoid/tanh)
  - This pearl: intermediate kernel scalars need bilateral fminf/fmaxf
  Both share: bounded contracts must be enforced structurally.

Audit doc: cross-references bug, fix, validation smoke, both pearls.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-04 15:20:26 +02:00
jgrusewski
b92dcc3dfc chore(sp11): remove reward-chain diagnostic instrumentation
Instrumentation from 774d7552a served its purpose — empirically
identified the asymmetric reward cap at experience_kernels.cu:2788
as the inflater (popart min reaching -210336 by F0 ep3 pre-fix).
Fixed in 35db31089; validated by smoke-test-trk72 (PASSED).

Removed:
  - reward_chain_diag_reduce_kernel.cu
  - 12 per-sample diagnostic buffers in gpu_experience_collector
  - kernel parameter threading in experience_kernels.cu
  - launcher + reader + mapped-pinned output in gpu_dqn_trainer
  - wire-up site + HEALTH_DIAG emit in training_loop
  - audit doc section for the transient instrumentation

This brings the worktree back to its pre-instrumentation state on
the SP11 reward chain. Symmetric reward cap fix (35db31089) and
plan_isv symmetric clamps (Commit A) remain.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-04 15:14:54 +02:00
jgrusewski
348f6078b8 fix(sp11): plan_isv symmetric clamp — 4 sites mirror reward-cap bug
Implementer of 35db31089 (symmetric reward cap) flagged 4 additional
asymmetric-clamp sites in policy-input features (plan_isv slots),
mirroring the same bug class but on the feature side rather than
reward side:

  experience_kernels.cu:850   plan_isv[PNL_VS_TARGET] capped above only
  experience_kernels.cu:853   plan_isv[PNL_VS_STOP]   capped above only
  backtest_plan_kernel.cu:164 — mirror of 850
  backtest_plan_kernel.cu:168 — mirror of 853

unrealized P&L can be negative -> fminf(x, 2.0) leaves an unbounded
lower tail that produces feature jitter at the policy input, hurting
the network's state representation under losing-trade conditions.
Fix: symmetric clamp via fmaxf(-2.0, fminf(x, 2.0)).

Per pearl_symmetric_clamp_audit (added in close-out commit) and
pearl_bounded_modifier_outputs_require_structural_activation: any
spec-bounded scalar requires bilateral enforcement.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-04 15:07:08 +02:00
jgrusewski
35db310893 fix(sp11): symmetric reward cap — losses were unbounded
experience_kernels.cu:2788:
  float capped_pnl = fminf(base_reward, 10.0f);
                          ^^^^^^^^^^^^^ caps profits, NOT losses

Diagnostic instrumentation in smoke-test-k9drh on commit 774d7552a
captured the asymmetry empirically:
  ep1: r_popart min=-9186, max=+10
  ep2: r_popart min=-79089, max=+10 (growing)

`base_reward = 2.0f * vol_normalized_return` and
`vol_normalized_return = segment_return / vol_norm` where
`segment_return` has no structural lower bound (signed P&L). The
unilateral `fminf(base_reward, 10.0f)` capped the upper tail only,
so a single large adverse segment_return produced an arbitrarily
negative `capped_pnl` → r_popart → r_weighted →
reward_components[+0] → slot 63 (PopArt input EMA), inflating
C51/IQN/Bellman normalization scale and breaking Q-target
consistency across epochs. Empirical fingerprint matched the
within-fold sharpe degradation observed in smoke-test-gwfn8 on
commit fd24b5383 (10→4 within F0, 9→2.5 within F1).

Spec semantic: reward bounded in [-10, +10]. Fix:
  float capped_pnl = fmaxf(-10.0f, fminf(base_reward, 10.0f));

Audit findings (per task §2): all related fminf/fmaxf clamps in
experience_kernels.cu reviewed. Reward modifier chain (3260-3500)
verified bounded once r_popart is bilateral. Other bilateral
clamps already correct (515-517, 2174, 2191, 2227, 3300, 3452,
3729, 3763, 5076, 5210, 5213, 5461, 5535, 6353, 6693, 6694).
Intentional asymmetries verified at 1078, 1082-1085, 2564-2565,
2873, 2949, 4574, 5896 (each documented in the audit doc with
the structural reason the lower side is unbounded).

Latent finding flagged separately (NOT fixed here — feature-side
requires consumer audit per feedback_no_partial_refactor):
plan_isv[PNL_VS_TARGET] at line 850 and plan_isv[PNL_VS_STOP]
at line 853 are upper-clamped at 2.0 but lower-unbounded. These
feed assemble_state as policy features (not reward components),
so out of scope of this reward-chain fix. Mirrored in
backtest_plan_kernel.cu:164,168 (same pattern). Tracking as
follow-up.

Per pearl_bounded_modifier_outputs_require_structural_activation:
spec-bounded values require BILATERAL structural enforcement.
This bug was the asymmetric counterpart to the conviction sigmoid
(which IS correctly bounded structurally).

Diagnostic instrumentation (commit 774d7552a) NOT removed in this
commit — will be removed in a follow-up after the symmetric-cap
smoke validates the fix on L40S.

docs/dqn-wire-up-audit.md updated with Resolution (2026-05-04)
section reflecting root cause, fix, audit follow-through, and the
latent plan_isv finding (per Invariant 7).

Build: SQLX_OFFLINE=true cargo check -p ml --lib — clean.
Test: trainers::dqn::trainer::tests::test_reward_function_price_changes
      passes. (PPO test_reward_computation pre-existing failure on
      HEAD 774d7552a, unrelated — verified via stash+rerun.)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-04 14:42:15 +02:00
jgrusewski
774d7552a0 diag(sp11): instrument reward chain to find the 5000x inflater
Smoke smoke-test-gwfn8 on fd24b5383 showed mean(|reward|) hitting
5054 at F0 ep2 despite all known multiplicative modifiers being
structurally bounded (conviction in (0,1) via sigmoid at line 7579,
cf_flip in +/-1, drawdown in [-5*w_dd,0], shaping_scale in [0,1]). The
inflater is somewhere in the pre-composition or modifier chain that
isn't currently visible in HEALTH_DIAG.

Adds 12 per-sample diagnostic buffers + reduction kernel + HEALTH_DIAG
emit for min/mean/max at every checkpoint in the reward chain:
  - per-component (r_popart, r_trail, r_micro, r_opp_cost, r_bonus)
  - r_weighted (post-composition, pre-modifier)
  - post-modifier sequential (post_dd, post_inv, post_churn, post_conv)
  - sanity checks (position_abs, conviction)

Implementation:
  - 12 new per-sample CudaSlice<f32> buffers in gpu_experience_collector
    (alloc_episodes * alloc_timesteps each); zero-init at every (i,t)
    in the kernel entry block before any early-return; written at
    each checkpoint with NULL-tolerant guards.
  - new reward_chain_diag_reduce_kernel.cu: single-block 256-thread
    block-tree-reduce over the 12 buffers, three lockstep reductions
    (sum/min/max) per buffer in shared memory; outputs 36 floats
    (3 stats x 12 buffers) to a 36-slot mapped-pinned scratch buffer
    on the trainer; no atomicAdd per feedback_no_atomicadd, pure GPU
    compute per feedback_no_cpu_compute_strict, mapped-pinned host
    visibility per feedback_no_htod_htoh_only_mapped_pinned.
  - trainer: cubin load, mapped-pinned 36-f32 output, set_sp11_reward_
    chain_diag_bufs setter, launch_sp11_reward_chain_diag_reduce
    launcher, read_sp11_reward_chain_diag host accessor.
  - training_loop.rs: wires the 12 collector buffers post-construction
    (mirror of the popart-component wire-up); HEALTH_DIAG `reward_
    chain_diag` emit added immediately after `reward_split` — launches
    reduction kernel, syncs stream, reads the 36 floats.
  - build.rs: adds reward_chain_diag_reduce_kernel.cu to the cubin
    manifest.
  - docs/dqn-wire-up-audit.md: new section documenting the
    instrumentation scope, additions, exclusions (no ISV slots, no
    state-reset registry entries), and removal plan.

No state-reset registry entry: this is a transient diagnostic, not
persistent state — buffers reset to 0 every step via the kernel
entry-block default writes (same pattern as the other per-sample
diagnostic buffers like trail_triggered_per_sample). No ISV slots
are added: the host reads the mapped-pinned scratch directly to keep
this lightweight and avoid permanent ISV growth.

Will be removed in a follow-up commit once the inflater is identified
and properly fixed per pearl_bounded_modifier_outputs_require_
structural_activation.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-04 13:55:03 +02:00
jgrusewski
7a19c51522 docs(sp11): B1b smoke-recovery close-out — audit + 3 memory pearls
Layer C close-out for SP11 B1b smoke-recovery work. Two atomic commits
land the fix:
- b3b4d0278: z-score normalization for mag-ratio canary (Bug 5)
- fd24b5383: launch-order — reward_component_ema before canary (Bug 6)

Audit doc (docs/dqn-wire-up-audit.md) adds the close-out entry covering
all 6 bugs (slot 63 overload, stale rc[] init, cf_flip ordering,
cf-component feedback loop, magnitude-asymmetric ratios, launch-order),
the 6 new ISV variance EMA slots [361..367) producer/consumer wiring,
and validation evidence from the 3 smoke-test workflows
(smoke-test-6wd2c killed → smoke-test-4rbv9 killed → smoke-test-gwfn8
PASSED 15m11s on commit fd24b5383).

3 memory pearls written to user memory (not in repo):
- pearl_zscore_normalization_for_magnitude_asymmetric_signals
- pearl_canary_input_freshness_launch_order
- pearl_controller_amplifies_dominant_magnitude_trap

MEMORY.md index updated under Controllers and signals.

Open follow-up flagged: residual within-fold sharpe degradation
across epochs is a separate triage scoped to post-T10 validation,
not part of B1b smoke-recovery.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-04 13:26:55 +02:00
jgrusewski
fd24b53833 fix(sp11): B1b launch-order — reward_component_ema before mag-ratio canary
smoke-test-4rbv9 on b3b4d0278 (z-score implementation) showed bit-
identical w_pop=2.000 at ep1 to pre-z-score B1b smoke, proving the
z-score formula was structurally a no-op:

  z[c] = mag[c] / fmaxf(sqrtf(0), EPS_DIV) = mag[c] x 1e6
  ratio[c] = (mag[c] x 1e6) / (1e6 x sum(mag)) = mag[c] / sum(mag)  -> linear

Root cause: launch_reward_component_ema_inplace at line 3707 ran
AFTER launch_sp11_mag_ratio_compute at line 3465. So ISV[64..68]
and ISV[362..366] held sentinel-0 values at ep1's canary read
(ep0 had no segment_complete fires). z[c]=0 for c=1..5 -> popart
ratio collapsed to 1.0 -> controller saturated.

This was structurally the same bug that motivated adding
launch_sp11_popart_component_ema at line 3441 (B1b follow-up).
That fix-up addressed popart but left cf/trail/micro/opp_cost/
bonus stale.

Moved launch_reward_component_ema_inplace from line 3707 to before
launch_sp11_popart_component_ema. Other launches at the original
site (trade_attempt_rate_ema, plan_threshold_update, etc.) stay
where they were — different consumers, different timing constraints.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-04 12:26:18 +02:00
jgrusewski
b3b4d02789 fix(sp11): B1b smoke-recovery — z-score normalization for mag-ratio canary
Linear magnitude ratios in reward_component_mag_ratio_compute_kernel
amplified popart's intrinsic O(100) magnitude over the other 5
components' O(0.1-2) magnitudes, causing controller to saturate
w_pop toward MAX_WEIGHT regardless of actual signal quality.

Replaced with z-score: z[c] = mag[c] / max(sqrt(var[c]), EPS_DIV).
6 new ISV slots [361..367) for per-component variance EMAs computed
via Welford's online algorithm in extended popart_component_ema_kernel
and reward_component_ema_kernel.

Atomic per feedback_no_partial_refactor: slot allocation +
state-reset registry + 2 producer kernels + canary signature +
launcher Pearls A+D + tests.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-04 11:51:30 +02:00
jgrusewski
85069ba75e spec(sp11): amend B1b smoke-recovery — z-score normalization for mag-ratio canary
smoke-test-6wd2c on commit 61b2fa962 (B1b + 4 bug fix-ups) revealed a 5th
pathology not covered by the previous fix-ups: the mag-ratio canary's
linear-magnitude-ratio formula amplifies whichever component is
intrinsically largest, regardless of whether that's a useful signal.

Popart (trade P&L on segment_complete) is O(100) per fire while the
other 5 components (cf/trail/micro/opp_cost/bonus) are O(0.1-2). Even
with the slot 360 fix preventing total-reward contamination, popart's
intrinsic magnitude makes popart_mag / Σ ≈ 0.93. The controller blend
'winner_weight = ratio' then amplifies popart further. Smoke trajectory:
w_pop=2.0 → 2.44 → 2.57, curiosity_b=30 → 120 → 199, sharpe_ema=10.7 →
2.4 → 0.75 (cascading collapse).

Resolution: z-score normalization. Each component's magnitude divided
by its own running standard deviation before computing the ratio:

  popart_z = popart_mag_ema / max(sqrt(popart_var_ema), EPS_DIV) ≈ O(1)
  cf_z     = cf_mag_ema     / max(sqrt(cf_var_ema), EPS_DIV)     ≈ O(1)
  ...
  ratio[c] = component_z / Σ component_z   ≈ ~1/6 each when stable

Allocates 6 new ISV slots [361..367) for per-component variance EMAs.
Producers: extend popart_component_ema_kernel + reward_component_ema_kernel
to also emit variance via Welford's online algorithm (single-pass).
SP5_SLOT_END = 367, ISV_TOTAL_DIM = 367.

Carries forward main's slot 360 amendment (commit 52c0b7521 on main)
which the sp11 branch was missing, plus this z-score amendment.

Per-component gradient ratios (the original spec intent) don't fix this
either — in DQN there's no per-component gradient pathway; grad norm
scales with current_weight × magnitude, so it's the same bias. Z-score
normalization is the standard scale-invariant measure of significance
and matches what the SP11 controller is trying to express.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-04 11:30:52 +02:00
jgrusewski
61b2fa962b fix(sp11): B1b bug 3 — cf-component feedback loop in mag-ratio canary
Deep audit on b435d25be found a third post-B1b bug in same class as
the slot 63 overload: experience_env_step writes cf_reward_weighted
(= w_cf × cf_reward, post-controller-weight) to reward_components_
per_sample[+1] at line ~3696. SP4's reward_component_ema_kernel EMAs
this into ISV slot 64 (REWARD_CF_EMA_INDEX) which the SP11 mag-ratio
canary reads as ratio[1] = slot[64] / Σ.

Self-reinforcing loop:
  high w_cf → high cf_reward_weighted → high slot 64 →
  high ratio[1] → controller raises w_cf → tighter loop

Until mean=1 normalization saturates other components to floor.

The other 5 component slots correctly write RAW pre-weight values:
- rc[+0] = total_reward (intentional, PopArt input — post-composition)
- rc[+2..+5] = r_trail / r_micro / r_opp_cost / r_bonus (all raw, pre-Σ)
- Slot 360 (popart-component) fed by `popart_component_per_sample`
  which receives `r_popart` raw

Only rc[+1] was wrong. Fixed: write raw cf_reward to rc[+1] so the
canary tracks intrinsic cf magnitude. The replay-buffer cf-tuple
reward (out_rewards[cf_off]) still uses cf_reward_weighted — that's
correct, loss kernels train on controller-weighted signal.

This was the third bug in a class — pre-SP11 invariants exposed by
post-decomposition semantic. Trio: (1) slot 63 overload (5e16b67ca),
(2) stale rc[] init + cf_flip ordering (b435d25be), (3) cf-component
feedback loop (this commit).

cargo check + build clean; 6/6 SP11 GPU tests + 14/14 contract tests
still pass. After this fix-up, all known SP11 reward-system bugs
identified by deep audit are resolved. L40S smoke validates empirical
sharpe recovery.
2026-05-04 11:00:47 +02:00
jgrusewski
b435d25bec fix(sp11): B1b bug-hunt fix-up — stale rc[] init + cf_flip ordering
Bug-hunt review on 5e16b67ca found two real bugs in experience_env_step
post-B1b structural refactor — same class as the slot 63 overload
(pre-SP11 invariants exposed by post-decomposition semantic).

Bug 1 (Critical): reward_components_per_sample[+1..+5] not zero-init at
per-bar entry. Slots written only on execution paths that fire (rc[2]
inside segment_complete trail-trigger, rc[3] inside positioned-non-
complete, rc[4] inside flat-with-features, rc[5] inside multiple
bonus paths, rc[1] inside CF block). Non-firing paths leave stale
values from previous batch's same out_off, contaminating SP4
reward_component_ema_kernel -> slots 64..68 -> SP11 mag-ratio canary
-> wrong controller weights.

Fix: zero-init rc[0..6) at the same location where r_<component>
locals are zero-initialized, so locals and buffer reset together.
Eliminates the entire stale-rc class of bugs.

Bug 2 (Important): cf_flip applied BEFORE inventory/churn/conviction
modifiers. Spec section 3.4.4 requires cf_flip LAST so subtractive
penalties operate on the right sign and multiplicative scales
attenuate before direction is flipped. Pre-fix on flipped samples,
inventory/churn ADDED to negated reward (penalty became bonus) and
conviction scaled the wrong-signed value. Asymmetric gradient signals
between flipped/non-flipped -> contaminated SP11 mag-ratio canary at
the cf axis.

Fix: move cf_flip to after conviction_scale, making it the last
modifier before out_rewards write. CF-block contract preserved
(reward at cf_off is still the post-flipped final value; CF block
already does `do_flip ? -reward : reward` to recover unflipped base).

Stale doc-strings: state_reset_registry.rs (2 sites) and
training_loop.rs (1 site) referenced pre-fix-up SP5_PRODUCER_COUNT=186
/ wiener_buffer=771. Updated to formula form
`(71 + SP5_PRODUCER_COUNT) × 3` so they don't drift on the next SP;
current value (post-B1b fix-up slot 360) is SP5_PRODUCER_COUNT=187,
buffer=774 floats.

dqn-wire-up-audit.md: appended SP11 B1b bug-hunt fix-up section
documenting both bugs, the structural fix, the doc-string sweep,
and verification (cargo check/build/tests).

cargo check + build clean; 6/6 SP11 GPU tests + 10/10 sp5_isv_slots
contract tests + 4/4 state_reset_registry tests still pass. The two
bugs were latent in the local smoke (sharpe 3 vs B1a 30 likely
traceable to either or both); structural fix sound. L40S smoke on
this commit will validate the empirical recovery.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-04 10:51:55 +02:00
jgrusewski
5e16b67ca6 fix(sp11): B1b follow-up — add slot 360 for popart-component mag EMA
Per spec §4 amendment at 52c0b7521 on main: B1b smoke surfaced that
SP11 mag-ratio canary was reading slot 63 (REWARD_POPART_EMA_INDEX)
which is overloaded — pre-SP11 PopArt's normalization input (total
reward mag EMA) was the same value as popart-component magnitude
because composition was inline accumulation. B1b decomposition exposed
the overload; controller emitted w_pop ≈ 2.0 based on contaminated
ratio → 10× sharpe drop in smoke.

Resolution:
- Allocate ISV slot 360 = POPART_COMPONENT_MAG_EMA_INDEX
- Add popart_component_per_sample mapped-pinned buffer + write site
  in experience_env_step at the r_popart assignment
- New popart_component_ema_kernel.cu writes slot 360 (single-block
  block-tree-reduce per feedback_no_atomicadd)
- mag-ratio canary kernel signature changes from single
  popart_ema_base_slot to (popart_specific_slot, cf_others_base_slot)
  pair so it reads non-contiguous slot 360 + slots 64..68
- Reset registry: sp11_popart_component_mag_ema entry + dispatch arm
- Slot 63 (PopArt's input) UNCHANGED — pre-SP11 invariant preserved

ISV total: 360 → 361. SP5_SLOT_END = 361. SP5_PRODUCER_COUNT = 187.

cargo check + build clean; SP11 GPU oracle tests pass (6/6 including
updated mag_ratio test with 2 slot-index args); sp5_isv_slots layout
tests pass (10/10 with 185 unique slots / 187 linear span); state
reset registry tests pass (4/4 with new sp11_popart_component_mag_ema
entry + dispatch arm). Local multi_fold_convergence smoke gated on
data volume (175k bars on local fxcache vs 10-month walk-forward
requirement); validation deferred to L40S Argo run on PVC data per
the spec's pass criterion.
2026-05-04 10:29:47 +02:00
jgrusewski
034ba16801 feat(sp11): B1b — structural reward composition refactor (production flip)
Per spec §3.5.3 amended at 7ddaf9c51 on main: experience_env_step
reward composition decomposed from 8+ inline accumulation sites into
explicit per-component locals (r_popart, r_cf, r_trail, r_micro,
r_opp_cost, r_bonus), then composed as Σ w_i × r_i with controller
weights from ISV[340..346).

Trail reward extraction (§3.5.4): trail-fire P&L now flows through
r_trail (forced-exit signal) instead of r_popart (voluntary-exit
signal). REWARD_TRAIL_WEIGHT_INDEX has real signal — controller can
weight forced-exit vs voluntary-exit P&L differently. rc[2] (the
prior structural-placeholder slot) now carries trail magnitude.

Universal post-composition modifiers (§3.4.4): drawdown / capital-
floor / inventory / churn / conviction-scale / cf-flip apply AFTER
the weighted Σ, unweighted. They are risk constraints and structural
operators, NOT learning components — agent cannot weigh them away.

Mean=1 normalization (B0, §3.4.3): weights normalize to mean=1 so per-
bar `w_active × r_active` ≈ pre-SP11 absolute scale on average.

Sentinel-defense: experience_env_step runs at start of epoch, SP11
controller runs at end (training_loop.rs ~3475). At fold 0 epoch 0
step 0 the controller has not yet emitted, so ISV[340..346) hold
sentinel 0. Defense: fmaxf(w_raw, 0.01) — same Invariant-1 hard floor
the controller enforces post-renorm. Cold-start scale = 1% of
pre-SP11; Pearl A bootstrap on first emit replaces sentinel.

cf_reward path: out_rewards[cf_off] now writes controller-weighted
cf reward (w_cf × r_cf with sentinel-defense). Loss-kernel
cf_weight=0.3f at mse:318/c51:789 (structural Q-blend, NOT reward
weight) UNTOUCHED per §3.5 amendment.

Mutual exclusivity preserved (popart / trail / micro / opp_cost):
exactly one path fires per bar; others stay 0. The cascade scalar
`reward` mirrors per-component locals so C.4/D.4b bonus blocks that
read in-progress trade reward (Q-cap pattern from
pearl_one_unbounded_signal_per_reward) keep bit-identical compounding.
After cascade, `reward` is overwritten with r_weighted; post-
composition modifiers operate on r_weighted as before.

This is the production-flip commit. Trainer is now on the SP11
controller end-to-end (modulo replay-time curiosity which lands in
B1c after Layer C audit per §3.5.5).

Verification:
  - cargo check + build clean (1m32s release).
  - 6/6 SP11 GPU oracle tests pass (none exercise env_step directly).
  - 14/14 contract tests pass (sp5_isv_slots=10, state_reset_registry=4).
  - Local smoke (RTX 3050 Ti, 20-epoch magnitude_distribution) verifies
    HEALTH_DIAG sp11_reward weights drift epoch-over-epoch:
      epoch 0: w_pop=1.000 w_cf=1.000 w_tr=1.000 ...   (uniform sentinel-defense floor → mean=1)
      epoch 3: w_pop=1.991 w_cf=2.036 w_tr=0.493 ...   (controller redistributes)
      epoch 9: w_pop=1.823 w_cf=1.887 w_tr=0.572 ...   (mean ≈ 1.0 preserved, Σ ≈ 6)
    EVAL_DIST bit-identical to B1a baseline (eq=0.803 eh=0.197 ef=0.000)
    — pre-existing magnitude eval-collapse pathology
    (project_magnitude_eval_collapse_kelly_capped) unchanged by B1b.

Audit doc updated (Invariant 7): docs/isv-slots.md SP11 section now
reflects Layer B status with B0/B1a/B1b/B1c rollout timeline.
2026-05-04 09:46:53 +02:00
jgrusewski
d5e1214f25 fix(sp11): B1a — saboteur GPU multiplication + SimHash state_stride
Per feedback_cpu_is_read_only, saboteur effective scale now computed
on-device:
- saboteur_generate_params kernel signature gains isv ptr +
  saboteur_intensity_mult_slot parameter
- Kernel reads `mult = fmaxf(isv[saboteur_intensity_mult_slot],
  SABOTEUR_MIN)` (sentinel-0 defense for cold-start before A2's
  controller first runs) and applies `effective_scale = base × mult`
  to perturbation generation
- gpu_experience_collector.rs launcher updated; only one call site

SimHash state_stride parameter added to lookup + update kernels —
prepares for B1c replay-time curiosity wiring against trainer.
states_buf which is STATE_DIM_PADDED=128-strided. Kernel inner loop
reads `state[i × state_stride + d]` (was `i × 42 + d`). Proj-init
kernel unchanged (writes projection, doesn't read states).

Pre-requisite for B1c (curiosity wiring) and a small atomic step
toward full SP11 production behavior.

cargo check + build clean; 6/6 SP11 GPU tests + 14/14 contract tests
still pass.
2026-05-04 08:51:23 +02:00
jgrusewski
302992f63a fix(sp11): B0 — controller renorm Σ=1 → mean=1 (post-A2 spec amendment)
Per spec §3.4.3 amended at 7ddaf9c51 on main: A2's Σweights=1
renormalization caused 6× reward-magnitude collapse on mutually-
exclusive components in experience_env_step (popart/micro/opp_cost
paths fire at most one per bar; weight 1/6 per fired path averages
1/6 of pre-SP11 reward magnitude).

Amended: weights normalize to mean(weights) = 1 (i.e., Σ = N = 6).
Each weight in [WEIGHT_HARD_FLOOR=0.01, MAX_WEIGHT=3.0]. Default
uniform = 1.0 each. Preserves pre-SP11 absolute scale on average.

Code change: reward_subsystem_controller_kernel.cu renormalization
step changes from `weights[c] = blends[c] / blend_sum` to
`weights[c] = min(MAX_WEIGHT, blends[c] × N / blend_sum)`. Anchors
N_COMPONENTS=6.0f and MAX_WEIGHT=3.0f added to the Invariant-1 const
float block at the top of the kernel.

3 A2 controller unit-test assertions updated:
- z_score_at_zero: weight_sum 1.0→6.0; per-component 1/6→1.0
- weights_renormalize_after_floor: assertion strengthened to
  weight_sum ≤ N (cap binds in this pathological test where pre-cap
  dominant weight ≈ 4.18 > MAX_WEIGHT=3.0); added per-component
  ≤ MAX_WEIGHT envelope check; added explicit cap-binding assertion
  on dominant weight.
- saboteur_post_clamp_holds_min: weight assertions unaffected (this
  test asserts only on s[6]/s[7], saboteur+curiosity are independent
  of mean-vs-Σ choice).

Audit doc updated: docs/dqn-wire-up-audit.md gets a new
"SP11 B0 — controller renorm Σ=1 → mean=1 (2026-05-04)" section.

cargo check + release build clean. 6/6 SP11 GPU oracle tests pass on
RTX 3050 Ti. sp5_isv_slots (10/10) + state_reset_registry (4/4)
contract tests still pass.

Pre-requisite for B1b structural reward-composition refactor (§3.5.3)
which depends on the mean=1 semantic.
2026-05-04 08:40:33 +02:00
jgrusewski
44fb4531a8 fix(sp11): A2 follow-up — delete dead launchers + expand XOR-fold rationale
Code-quality review on 25eba79ad found two Important issues:

- launch_sp11_novelty_simhash_lookup + launch_sp11_novelty_simhash_update
  were defined with #[allow(dead_code)] since B1 hasn't wired them yet —
  feedback_no_hiding violation. Deleted both functions; kept the kernel
  handle fields + cubin loads + MappedF32Buffer storage. B1 will inline
  the launches at the actual call site (matching A0's deferral pattern
  for the novelty-hash registry entry). Field doc-refs at the kernel
  declarations updated so no stale name reference remains.

- Seed XOR-fold comment named what ("fold the high + low halves") but
  not why ("to preserve entropy; truncation would silently discard
  upper 32 bits"). Extended comment so a future reader switching to
  truncation gets the warning.

Verified: cargo check clean (18 unrelated warnings, zero errors); 6/6
SP11 GPU oracle tests build under --features cuda (GPU-gated, ignored
on CPU runners); 10/10 sp5_isv_slots + 4/4 state_reset_registry
contract tests pass; allow(dead_code) count is exactly the 12
pre-existing attributes (was 14 before this fix; A2 added 2).
2026-05-04 02:54:57 +02:00
jgrusewski
25eba79ad5 feat(sp11): A2 — controller kernel + SimHash novelty buffer
reward_subsystem_controller_kernel: 5 canaries → 10 outputs, true Z-score
(delta_ema/sqrt(var_ema)), sigmoid blending, weight renormalization to Σ=1,
saboteur post-clamp, curiosity permanent floor (0.2 × bound). Pearls A+D
chained on outputs per spec §3.4.1.

novelty_simhash_kernel: 42×16 random projection → 16-bit SimHash code,
1M-slot bucket count table for novelty signal `1/sqrt(1+count)`. Race-
tolerated update per feedback_no_atomicadd (under-counts bias novelty
UPWARD — safe direction).

novelty_simhash_proj_init_kernel: Philox-seeded GPU init for the
projection matrix (CPU is read-only per feedback_no_cpu_forwards).

HEALTH_DIAG `sp11_reward` line emits 10 outputs + improvement_z each
epoch. Reset registry: novelty hash table reset arm wired (closes the
A0 deferral); projection matrix is frozen at trainer init for run
lifetime, not reset.

All 20 SP11 slots populate every step. No consumer reads them yet —
training behavior unchanged from A1. 3 new GPU oracle tests pass on
RTX 3050 Ti (controller midpoint, weight renorm, saboteur clamp).

Spec: docs/superpowers/specs/2026-05-04-sp11-reward-as-controlled-subsystem.md §3.4 §3.5.2

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-04 02:26:05 +02:00
jgrusewski
66f5fd8f00 fix(sp11): A1 follow-up — remove let _ + correct shmem in tests
Code-quality review on 91b48bc7a found two issues in
sp11_producer_unit_tests.rs:

- :281 `let _ = REWARD_COMPONENT_MAG_RATIO_BASE` violated
  feedback_no_hiding (silent dead-code suppression). Removed the
  suppression and the unused import. The ISV landing slot for the
  mag-ratio producer is exercised by Pearls A+D unit tests, not here.

- :190 saboteur_engagement test passed shared_mem_bytes=1024 for a
  kernel that uses __shared__ (static, allocated at compile time),
  not extern __shared__ (dynamic). Set to 0 matching the other two
  test launches.

cargo check + all 3 GPU oracle tests still pass.
2026-05-04 02:04:14 +02:00
jgrusewski
91b48bc7a5 feat(sp11): A1 — three canary producer kernels (no behavior change)
Adds val_sharpe_delta + saboteur_engagement + reward_component_mag_ratio
GPU producers for the SP11 reward-as-controlled-subsystem chain. Each is
a single-block producer chained with apply_pearls_ad_kernel for Pearls
A+D smoothing per pearl_first_observation_bootstrap.md +
pearl_wiener_optimal_adaptive_alpha.md. All three write to slots in
[350..360) which no consumer reads yet — Layer A is additive; consumer
migration lands atomically in Layer B.

A1.1 — val_sharpe_delta_compute_kernel.cu
  Two-pass: writes raw delta + (delta - prev_delta_ema)^2 to scratch.
  Chained Pearls A+D (n_slots=2) → ISV[VAL_SHARPE_DELTA_EMA_INDEX=350,
  VAL_SHARPE_VAR_EMA_INDEX=351]. Host writes val_sharpe to mapped-pinned
  history[1]; rotation handled in training_loop.rs at val emit boundary
  (a literal already-computed value — no host-side compute, no htod_copy).

A1.2 — saboteur_engagement_compute_kernel.cu
  Per-bar |Δreward| > 0.01 × ISV[PNL_REWARD_MAGNITUDE_EMA_INDEX] check
  with block tree-reduce (no atomicAdd per feedback_no_atomicadd). The
  per-bar Δreward signal is produced by experience_env_step's saboteur
  perturbation site as `traded × |reward| × max(|eff_spread − 1|,
  |eff_slip − 1|)` — a structural proxy for the cost-differential the
  saboteur imposed on bars where the model traded. Single kernel-side
  emit (no parallel reward computation), per spec §3.3.1.
  Chained Pearls A+D → ISV[SABOTEUR_ENGAGEMENT_RATE_INDEX=358].

A1.3 — reward_component_mag_ratio_compute_kernel.cu
  Reads ISV[REWARD_POPART_EMA_INDEX..+6) (the SP4 reward-component
  magnitude EMAs), normalises to ratios, and mirrors popart magnitude
  into scratch[6] as a side-output. ONE non-pointer parameter
  (popart_ema_base_slot) — no _unused param per feedback_no_stubs.
  Two chained Pearls A+D launches:
    n_slots=6 → ISV[REWARD_COMPONENT_MAG_RATIO_BASE..+6)
    n_slots=1 → ISV[PNL_REWARD_MAGNITUDE_EMA_INDEX=359]
  (slots non-contiguous: 352..358 then 359.)

Wire-up (per feedback_wire_everything_up):
- 3 cubin entries appended to crates/ml/build.rs
- 3 kernel handles + val_sharpe_history_pinned (MappedF32Buffer[2]) +
  saboteur_delta_reward dev-ptr cache fields on GpuDqnTrainer
- 3 launchers (launch_sp11_*) + 1 setter (set_sp11_saboteur_delta_reward_buf)
- saboteur_delta_reward_per_sample buffer field on GpuExperienceCollector
- experience_env_step kernel signature extended with the new buffer arg;
  every call site in the same commit per feedback_no_partial_refactor
- training_loop.rs init wires collector→trainer setter; val emit boundary
  invokes launch_sp11_val_sharpe_delta_compute; per-epoch metrics block
  invokes launch_sp11_mag_ratio_compute then
  launch_sp11_saboteur_engagement_compute (mag_ratio first so the
  signal-relative threshold base is populated before the saboteur reader)
- SP5_SCRATCH_TOTAL grown 266 → 276 (10 new scratch slots: 2+1+7)
- docs/isv-slots.md SP11 section updated to reflect A1 producers

3 GPU oracle tests in crates/ml/tests/sp11_producer_unit_tests.rs
pass on RTX 3050 Ti via MappedF32Buffer fixtures (zero htod_copy /
dtoh_sync_copy / alloc_zeros — feedback_no_htod_htoh_only_mapped_pinned
compliant).

Note on Step 8a path: the plan offered two routes for the saboteur
Δreward producer — in-kernel diff emission OR a small dedicated
reader-of-existing-buffers. The existing reward path emits ONE reward
(not both with/without), so the dedicated-reader alternative was
infeasible. The in-kernel emission landed as a small write site at the
END of experience_env_step (after total_reward_per_sample is finalised),
threading saboteur_eff_spread/saboteur_eff_slip from the perturbation
site forward to the END via stack vars. Single new kernel parameter,
single new GPU-only buffer, single existing call site updated.

Spec: docs/superpowers/specs/2026-05-04-sp11-reward-as-controlled-subsystem.md §5
Plan: docs/superpowers/plans/2026-05-04-sp11-reward-as-controlled-subsystem.md (Task A1)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-04 01:40:38 +02:00
jgrusewski
201b59dfbc fix(sp11): A0 sweep — eliminate remaining stale wiener-buffer refs
A0 follow-up (1e5a65912) fixed two stale refs from code-quality review.
Implementer surfaced 3 more accumulated across SP4/SP5/Layer-D:

- :537 sp4_wiener_state — claimed 543 floats with growth chain 141→207→213→543
- :974 sp5_pnl_aggregation — claimed SP5_WIENER_TOTAL_FLOATS=573 (post-D2 stale)
- :989 sp5_health_composition — same =573 stale
- :1009 sp5_training_metrics_ema — claimed =582 (post-D3 stale)

All four converted to formula form (71 + SP5_PRODUCER_COUNT) × 3 matching
A0 fix-up pattern. Drops brittle growth chains in favor of a derivable
formula. Audit doc entry added per Invariant 7. cargo check + state_reset
_registry tests 4/4 pass.
2026-05-04 01:21:41 +02:00
jgrusewski
1e5a65912c fix(sp11): A0 follow-up — update stale wiener-buffer + isv-slots header
Code-quality review on bf3a32d63 found two stale references that need
SP11 numbers:

- training_loop.rs:6672 + state_reset_registry.rs:891 — sp5_wiener_state
  comments referenced the post-SP4/post-SP8 buffer sizes (543, 681);
  post-SP11 is (71 + SP5_PRODUCER_COUNT) × 3 = 771 floats. Replaced the
  literal sizes with formula form citing SP5_PRODUCER_COUNT directly so
  this drifts less in the future.

- docs/isv-slots.md header — "Current ISV_TOTAL_DIM" said 171 (post-SP4
  Task A1) while actual is 360. Updated header; SP11 section already
  appended at the end of the file.

No logic changes. Cargo check + sp5_isv_slots / state_reset_registry
tests still pass.
2026-05-04 01:11:11 +02:00
jgrusewski
bf3a32d63a feat(sp11): A0 — allocate 20 ISV slots [340..360) + 20 reset entries
Pure infrastructure. No producer kernels, no consumer reads. Existing
training paths trace identically because no consumer reads slots [340..360)
yet. Layout-fingerprint bumped to ISV_TOTAL_DIM=360.

Spec: docs/superpowers/specs/2026-05-04-sp11-reward-as-controlled-subsystem.md
2026-05-04 00:55:07 +02:00
jgrusewski
e0d3abd9d2 plan(sp11): patch all 5 violations + 2 gaps from review
feedback_no_htod_htoh_only_mapped_pinned (tests not exempt):
  - All test fixtures converted from htod_copy/dtoh_sync_copy to
    MappedF32Buffer with host_slice / host_slice_mut access.
  - Novelty hash buffer + projection matrix changed from
    cudarc::CudaSlice<f32> + alloc_zeros to MappedF32Buffer.

feedback_no_cpu_forwards (CPU is read-only):
  - Projection matrix initialization changed from host-side StdRng +
    host_slice_mut writes to a one-shot GPU init kernel
    (novelty_simhash_proj_init_kernel) using Philox seeded from
    config.seed. No host RNG, no host writes.

feedback_no_cpu_compute_strict (saboteur multiplication):
  - B1 step 5 reverted from Rust-side `read_isv_slot * scale` to
    GPU-side: pass base scale + ISV pointer + slot index to the
    saboteur perturbation kernel; multiplication happens on-device.

feedback_trust_code_not_docs (grad-ratio terminology):
  - Spec §3.3.1 "per-component grad EMA" was wrong — SP4 grad-balancer
    is per-branch (4 slots), not per-reward-component. Renamed:
      reward_component_grad_ratio_compute_kernel
        → reward_component_mag_ratio_compute_kernel
      REWARD_COMPONENT_GRAD_RATIO_BASE
        → REWARD_COMPONENT_MAG_RATIO_BASE
    Source: existing REWARD_POPART_EMA_INDEX..+6 (per-component reward
    magnitude EMAs from SP4 reward_component_ema_kernel). Semantic
    equivalent for the controller's exploit/diversify blend.

feedback_no_stubs (dead parameter):
  - Removed `eps_div_idx_unused` from mag_ratio kernel signature.

Saboteur engagement: missing producer specified
  - Spec §3.3.1's two-reward-arrays formulation replaced with single
    `saboteur_delta_reward_buf` produced by the saboteur perturbation
    kernel itself (single reward computation, diff emitted as side
    output). Engagement kernel signature simplified to one input array.

PNL_REWARD_MAGNITUDE_EMA_INDEX (slot 359): producer wired
  - mag-ratio kernel mirrors `isv[REWARD_POPART_EMA_INDEX]` to
    scratch_out[6]; chained apply_pearls_ad targets slot 359.

Replay sample kernel location specified
  - graph_utility_kernels.cu:71 (gather_f32_scalar). New sibling kernel
    `gather_replay_reward_with_curiosity` defined; replaces the existing
    scalar gather (no legacy alias per feedback_no_legacy_aliases).
    novelty_simhash_lookup runs before, novelty_simhash_update after.

A2 controller test placeholders → full GPU oracle assertions
  - Three controller tests (z=0 midpoint, weight renorm, saboteur clamp)
    have full mapped-pinned fixtures with assertions on weight sum,
    individual values, post-clamp bounds.

Plan now passes:
  - feedback_no_htod_htoh_only_mapped_pinned (tests + production)
  - feedback_no_cpu_forwards (CPU never writes/computes for GPU)
  - feedback_no_cpu_compute_strict (all multiplications GPU-side)
  - feedback_no_atomicadd (race-tolerated non-atomic, safety documented)
  - feedback_no_partial_refactor (Layer B atomic; saboteur kernel sig
    change touches all callers in the same commit)
  - feedback_no_stubs (no dead parameters)
  - feedback_trust_code_not_docs (corrected spec terminology)
  - feedback_wire_everything_up (every new field has init + producer)
  - feedback_no_legacy_aliases (old gather_f32_scalar replaced, deleted)

1447 lines, +268 from previous version.
2026-05-04 00:40:36 +02:00
jgrusewski
d8b44e0829 plan(sp11): implementation plan for reward-as-controlled-subsystem
Task-by-task plan for the SP11 spec at HEAD 9395b983c. Three layers:

Layer A (3 commits, additive infrastructure, no behavior change):
  A0 — 20 ISV slots [340..360) + 20 reset entries + novelty-hash arm
  A1 — 3 canary producers (val_sharpe_delta, saboteur_engagement,
       reward_component_grad_ratio) with Pearls A+D chained
  A2 — controller kernel + SimHash novelty buffer; HEALTH_DIAG sp11_reward

Layer B (1 atomic commit, ~750 LOC):
  B1 — every consumer migrates: cf_weight (mse + c51), audit-discovered
       shaping sites, saboteur multiplier, replay-time curiosity bonus,
       novelty hash lookup+update scheduled at replay

Layer C (validation + close-out):
  C1-C2 — local + L40S smoke; T10 3×3×50 full validation
  C3-C5 — Fix 39 audit doc, pearl_reward_as_controlled_subsystem, MEMORY.md
  C6 — merge to main

Plan provides exact file paths, kernel signatures, GPU oracle test
skeletons, commit messages, and validation pass criteria from spec
§9.1-9.3. ~1550 LOC total estimate; ~3 hours subagent work plus
validation wall-clock (smoke ~25 min, T10 ~4 hr).

Spec: docs/superpowers/specs/2026-05-04-sp11-reward-as-controlled-subsystem.md
2026-05-04 00:27:28 +02:00
jgrusewski
9395b983cc spec(sp11): fix all 13 review issues — straight-up implementation
Critical bugs fixed:
1. Z-score formula: was mean(Δ)/mean(|Δ|), bounded to [-1,+1] — sigmoid
   never saturated, controller stuck in [0.27, 0.73]. Now true Z-score:
   delta_ema / sqrt(delta_var_ema) with two-pass var. Renamed canary
   slot 351 from VAL_SHARPE_STD_EMA to VAL_SHARPE_VAR_EMA.
2. Component weight renormalization added — Σweights = 1 enforced
   post-floor so PopArt's reward-magnitude EMA stays uncontaminated.
3. SABOTEUR_MIN bound violation — engagement-floor (0.1) was silently
   pulling output below 0.5 stated minimum. Added post-multiplication
   clamp to [SABOTEUR_MIN, SABOTEUR_MAX].
4. ratios[] undeclared — now __shared__ float ratios[6] block-loaded
   once from ISV.
5. §3.6 slot count off-by-five (15 → 20). All 20 slots get FoldReset
   entries; cold-start window behavior specified explicitly (no NaN path).

Architectural fixes:
6. Q-overconfidence concern addressed in scope — controller's
   REWARD_CF_WEIGHT_INDEX covers CQL conservatism. No SP11′ deferral;
   if T10 fails, extend controller outputs (e.g., target-update τ).
7. Curiosity recompute-at-replay specified (§3.5.1) — replay buffer
   stores base reward only; curiosity added per-tuple at replay time
   against current visit-count and current ISV. Eliminates stale-signal
   replay contamination that would worsen ep1-overfitting.

Smaller fixes:
8. Novelty signal specified — SimHash 42×16 projection → 16-bit code,
   1M-slot per-bucket count table, 1/sqrt(1+count). Hash table lives
   in state-reset registry (cleared at fold boundary).
9. Saboteur engagement operationally defined — per-bar
   |reward_with_saboteur − reward_without_saboteur| > EPS_ENGAGEMENT,
   block tree-reduce, EMA'd. EPS_ENGAGEMENT = 0.01 × PnL_EMA.
10. Duplicate sentence in §7 component-starvation paragraph removed.
11. Validation criteria strengthened (§9): aggregation across 9
    trajectories specified; primary metric = median peak-epoch ≥ 10
    (baseline median peak-epoch = 1); secondary = drop-from-peak ≤ 5%
    by ep20; tertiary = ep20 mean ≥ baseline ep1 mean. §9.3 fix-forward
    response codified — no rollback.
12. Phases split per project pattern — Layer A additive (3 commits:
    slots, canaries, controller), Layer B atomic consumer migration
    (1 commit, including replay-time curiosity), Layer C validation +
    close-out. No falsification gate; smoke is validation only.
13. Pearls A+D applied to controller outputs (§3.4.1) — chained
    apply_pearls_ad_kernel after controller writes scratch, smoothed
    values land in ISV. Consumers read smoothed slots.

Constants surviving (Invariant-1 anchors only, all rate-not-regime):
EPS_DIV, WEIGHT_HARD_FLOOR, SABOTEUR_MIN/MAX, CURIOSITY_PERMANENT_FRACTION,
CURIOSITY_BOUND_FRACTION, WEIGHT_FLOOR_FRACTION, ENGAGEMENT_FLOOR.

Spec is now full straight-up implementation; smoke validates Layer A
infrastructure and Layer B consumers, T10 validates SP11 success metric.
~1550 LOC across 3 commits in Layer A + 1 atomic commit in Layer B +
close-out in Layer C.
2026-05-04 00:20:25 +02:00
jgrusewski
d49fbbe4e2 spec(sp11): reframe curiosity as feature + add permanent floor
User correction: curiosity is the *fix* for the ep1-peak overfitting
pathology, not a hazard to defend against. Reframed §7 from "Risks"
to "Design notes" — curiosity bound is a signal-relative scale, not
a defensive cap.

Fix the contradiction this exposed in the formula: previous
`curiosity_pressure = stagnant_or_worse * curiosity_bound` went to
zero when improving, which would cancel the always-on exploration
the §7 narrative now relies on. Replace with permanent-floor pattern
per pearl_blend_formulas_must_have_permanent_floor:

  curiosity_floor    = 0.2 * curiosity_bound  (CURIOSITY_PERMANENT_FRACTION)
  curiosity_dynamic  = stagnant_or_worse * curiosity_bound
  curiosity_pressure = max(curiosity_dynamic, curiosity_floor)

Now curiosity is always ≥ 20% of bound (anti-overfitting baseline)
and rises toward the bound when stagnant (stagnation breaker).
Updated unit-test guidance to assert pressure > 0 even at z=+10.

CURIOSITY_PERMANENT_FRACTION=0.2 added to Invariant-1 fraction list.
Saboteur-rising-with-improvement reframed as adversarial-load feature
rather than over-stress risk.
2026-05-04 00:08:39 +02:00