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

Foxhunt

Production HFT trading system in Rust.

Architecture

The workspace contains 32 crates organized as follows:

Core Libraries (16)

Crate Purpose
trading_engine Order processing, FIX 4.4, IB TWS, SIMD, RDTSC timing
risk VaR, Kelly, circuit breakers, kill switches, compliance
risk-data Risk data types and shared structures
trading-data Trading data types
ml DQN Rainbow, PPO, TFT, Mamba2, ensemble inference
ml-data ML data types and feature definitions
data Market data ingestion and storage
backtesting Replay engine, strategy tester
adaptive-strategy Ensemble execution, microstructure analysis
common Shared types, resilience, error handling
storage S3 and local model storage
model_loader Model serialization and loading
market-data Market data feed handlers
database PostgreSQL access layer (SQLx)
config Configuration management
tli CLI commands and tooling

Services (8)

Service Purpose
backtesting_service gRPC backtesting service
broker_gateway_service FIX routing, broker connectivity
trading_service Core trading operations
ml_training_service Model training orchestration
data_acquisition_service Market data acquisition
trading_agent_service Autonomous trading agents
api_gateway gRPC API gateway with auth
web-gateway Axum REST + WebSocket gateway

Frontend

web-dashboard/ -- React 19 + TypeScript + Vite + TradingView charts.

Building

# Check compilation (no PostgreSQL required)
SQLX_OFFLINE=true cargo check --workspace

# Run tests for a specific crate
SQLX_OFFLINE=true cargo test -p <crate> --lib

# Clippy
SQLX_OFFLINE=true cargo clippy --workspace

ML Models

Four production model architectures on Candle v0.9.1 with CUDA:

  • DQN Rainbow -- Deep Q-Network with prioritized replay, dueling heads, noisy nets
  • PPO -- Proximal Policy Optimization with GAE and LSTM policies
  • TFT -- Temporal Fusion Transformer for multi-horizon forecasting
  • Mamba2 -- State space model for sequence prediction

Each model has a standalone trainer and a UnifiedTrainable adapter for the hyperopt pipeline.

Infrastructure

  • Git: Gitea at git.fxhnt.ai (Tailscale-only), Scaleway DEV1-S
  • Observability: OpenTelemetry OTLP (env OTEL_EXPORTER_OTLP_ENDPOINT)
  • Database: PostgreSQL with SQLx offline mode for CI

License

Proprietary. All rights reserved.

Description
No description provided
Readme 849 MiB
Languages
Rust 88.2%
Cuda 7.7%
Python 1.3%
Shell 1.1%
PLpgSQL 0.8%
Other 0.8%