Files
foxhunt/docs
jgrusewski 89789c8f00 fix(dqn-v2): Plan 4 Task 2c.3c.4 followup — replace stub-return defensive guards with proper invariants + signal escalation
Three classes of cleanup, prompted by the pre-commit stub-return
heuristic surfacing in the 2c.3c.4 commit:

(1) **Unreachable defensive null/bounds-guards → debug_assert + delete**
    `read_isv_signal_at`, `read_atom_utilization`, `compute_q_spectral_gap`
    each guarded `is_null()` on `isv_signals_pinned` / `q_readback_pinned`,
    yet both pointers are unconditionally allocated by the constructor
    (`cuMemAllocHost_v2` with `assert_eq!` on success) and NEVER reassigned
    after construction (greppable: zero `self.isv_signals_pinned =` /
    `self.q_readback_pinned =` outside Drop). The guards were dead code
    masquerading as safety. Removing the early-return removes the ambiguity
    between "buffer never initialised" and "buffer holds a real-zero ISV
    value" that downstream consumers of `read_isv_signal_at(IDX) == 0.0`
    cannot otherwise distinguish.

    `read_isv_signal_at`'s `index >= ISV_TOTAL_DIM` early-return is similarly
    a stub: every caller passes a named constant from the ISV slot table
    (`GAMMA_DIR_EFF_INDEX`, `TAU_EFF_INDEX`, `EPSILON_EFF_INDEX`, ...), so
    bounds violation is a programming error not a runtime condition.
    `compute_q_spectral_gap`'s `n_cols == 0` early-return is dead for the
    same reason — `total_actions()` is the sum of branch sizes which the
    trainer config requires non-zero.

    All four converted to `debug_assert!(...)` so dev/test builds catch
    invariant violations with a clear message; release builds inherit the
    UB on the unsafe deref of a null pointer (loud crash) rather than the
    silent stub return that previously made misconfiguration look fine.

(2) **Silent training-instability mask → warn-log + collapse sentinel**
    `compute_q_spectral_gap` previously returned 1.0 (= "balanced/healthy"
    in the calibration's eyes) on the first non-finite Q-value. NaN/Inf
    Q-values indicate gradient corruption — masking them with the same
    return value as a healthy network defeats HEALTH_DIAG's purpose. New
    behaviour: scan all samples, mark the non-finite case, then emit a
    `tracing::warn!` and return 100.0 (the same collapse sentinel emitted
    when lambda_2 falls below the numerical floor). Operators see the
    failure mode in logs rather than chasing a phantom balanced spectral
    gap that contradicts NaN losses elsewhere in the same epoch.

(3) **Genuine domain encodings → annotated for the linter**
    Three returns in `compute_q_spectral_gap` (lambda_2 < 1e-12, sigma2
    < 1e-6) and `power_iteration_largest` (norm < 1e-20) ARE meaningful
    mathematical degenerate-case answers, not stubs:
    - lambda_2 ≈ 0 / sigma2 ≈ 0: deflated Gram is rank-1, sigma1/sigma2
      diverges — calibrated collapse sentinel 100.0.
    - norm ≈ 0 in power iteration: M·v vanishes, so the eigenvalue is 0.
    Each got an explanatory comment + `// ok: domain encoding` to make
    the intent legible to both readers and the heuristic linter.
    `power_iteration_largest`'s `n == 0` early-return is unreachable
    from the spectral_gap caller (debug_asserted upstream); removed in
    favour of `debug_assert!(n > 0)` at the helper entry.

No behaviour change in production runs: the unreachable guards weren't
firing and the new invariants match the constructor's contract. The
non-finite Q-value path now surfaces what was previously silent — a
strict improvement.

cargo check clean at 11 warnings (baseline preserved).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-25 15:17:05 +02:00
..