## Two architectural cleanups, both surfaced by the wgdc8 experiment ### Part 1: volume_bar_size in cache key Mirrors the imbalance_bar_threshold/ewma_alpha fix from `f7718b376`. The volume bar size constant (100 contracts/bar) was previously hardcoded and not in the fxcache key. Tuning it would have hit the same fossilization bug as imbalance_bar_threshold did pre-fix. Changes: - `Hyperparams.volume_bar_size: u64` field added (default 100, matches `DEFAULT_VOLUME_BAR_SIZE` for backwards compat). - TrainingProfile loader reads `volume_bar_size` TOML key. - `calculate_dbn_cache_key_full` signature 7 → 8 args. Hashed via `to_le_bytes()`. Test `test_cache_key_includes_volume_bar_size` added; passes alongside the 5 existing tests. - 4 callers updated atomically (per `feedback_no_partial_refactor`): `discover_and_load`, `data_loading.rs:146`, `train_baseline_rl.rs:599`, `precompute_features.rs:259,720`. - `data_loading.rs:279` now passes `self.hyperparams.volume_bar_size` to `build_volume_bars` instead of the hardcoded `DEFAULT_VOLUME_BAR_SIZE`. - New `--volume-bar-size` CLI arg on both binaries (default 100). - New Argo workflow params `volume-bar-size` (default "100") and `data-source` (default "mbp10") on both `train-template.yaml` and `train-multi-seed-template.yaml`. Threaded into precompute + trainer invocations. - `scripts/argo-train.sh` exposes `--volume-bar-size <n>` and `--data-source <s>` for ad-hoc overrides. ### Part 2: OFI front-month filter (latent bug fix) `crates/ml/examples/precompute_features.rs:539-557` (the OFI/VPIN/Kyle's Lambda computation branch when MBP-10 + trades data is available) was loading trades unfiltered for per-bar microstructure feature computation. The volume bar formation path filters front-month per-file (line 354), but the OFI path did not. Effect pre-fix: during contract rollover windows (e.g., ESZ24 → ESH25), OFI per-bar microstructure features included trades from BOTH contracts simultaneously, distorting VPIN, Kyle's Lambda, and trade imbalance signals. Severity in production: small (front-month dominates ES.FUT volume by 10-100×) but real and present in every prior MBP-10+trades production run. Fix: mirror the per-file `filter_front_month` call from the volume bar path. Volume bar formation and OFI computation now both see the same in-month trade tape. Added log line shows raw vs filtered count per file for transparency. ## Why bundled Both fixes touch trade-data plumbing in `precompute_features.rs` and the fxcache key contract. Per `feedback_no_partial_refactor`, related architectural cleanups land atomically. Both surfaced from the same wgdc8 audit; bundling avoids two cache-key-invalidating commits in sequence (each would force full fxcache regen). ## Compatibility - `volume_bar_size` defaults to 100 → existing wgdc7-equivalent runs reproduce, but with a *new* fxcache key (the f7718b376-era cache file is unreachable; harmless, can GC manually). - OFI fix is strictly more correct; no opt-out needed. Existing models trained on contaminated OFI features may show slight feature distribution drift on first cache regen — expected, not a regression. - `data_source = "ohlcv"` Argo param now possible; routes precompute through volume bar branch directly. wgdc8 experiment uses this to test bar resolution sensitivity at volume_bar_size=500 (5× DEFAULT). Tests: 6/6 feature_cache tests pass. Workspace + examples compile clean. Audit-doc: `docs/dqn-wire-up-audit.md` updated. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
176 lines
7.4 KiB
Rust
176 lines
7.4 KiB
Rust
//! Feature Cache Key Utilities
|
|
//!
|
|
//! Provides cache key generation for `.fxcache` auto-discovery.
|
|
//! The flat-binary `.fxcache` format is the only cache path; Parquet
|
|
//! and old binary caches have been removed.
|
|
|
|
use anyhow::{Context, Result};
|
|
use sha2::{Digest, Sha256};
|
|
use std::path::{Path, PathBuf};
|
|
|
|
/// Calculate a cache key for a DBN data directory.
|
|
///
|
|
/// Hashes every `.dbn` and `.dbn.zst` file found recursively under `data_dir`
|
|
/// by (canonical path, size, mtime). Any change to the directory contents
|
|
/// (add / remove / modify) produces a different key.
|
|
///
|
|
/// Production defaults for the bar-formation params (matches `dqn-production.toml`):
|
|
/// `imbalance_bar_threshold = 0.5`, `imbalance_bar_ewma_alpha = 0.1`,
|
|
/// `volume_bar_size = 100` (matches `DEFAULT_VOLUME_BAR_SIZE`).
|
|
pub fn calculate_dbn_cache_key(data_dir: &Path) -> Result<String> {
|
|
calculate_dbn_cache_key_full(data_dir, None, None, "ES.FUT", "mbp10", 0.5, 0.1, 100)
|
|
}
|
|
|
|
/// Extended cache key that includes MBP-10 and trades directories AND bar
|
|
/// formation parameters. The cache is invalidated when ANY data source
|
|
/// changes — OHLCV, MBP-10, trades — OR when bar-formation params change.
|
|
///
|
|
/// Bar-formation params (`imbalance_bar_threshold`, `imbalance_bar_ewma_alpha`,
|
|
/// `volume_bar_size`) are hashed in regardless of `data_source` because:
|
|
/// (a) they uniquely identify the bar stream the cache contains, (b) downstream
|
|
/// features (e.g. log-returns, targets) depend on bar boundaries, and
|
|
/// (c) without this, two runs configured with different params will silently
|
|
/// share the same cache file (canonical 2026-05-09 audit: 14 prior production
|
|
/// runs all collided on `a3f933aa...` / `c07c960a...` keys regardless of TOML
|
|
/// threshold value because the bar params were not in the hash).
|
|
pub fn calculate_dbn_cache_key_full(
|
|
data_dir: &Path,
|
|
mbp10_dir: Option<&Path>,
|
|
trades_dir: Option<&Path>,
|
|
symbol: &str,
|
|
data_source: &str,
|
|
imbalance_bar_threshold: f64,
|
|
imbalance_bar_ewma_alpha: f64,
|
|
volume_bar_size: u64,
|
|
) -> Result<String> {
|
|
let mut hasher = Sha256::new();
|
|
|
|
// Include symbol and data_source in hash — different instruments/modes get different keys
|
|
hasher.update(symbol.as_bytes());
|
|
hasher.update(b"|");
|
|
hasher.update(data_source.as_bytes());
|
|
hasher.update(b"|");
|
|
|
|
// Include bar-formation params — different bar streams get different keys
|
|
hasher.update(&imbalance_bar_threshold.to_le_bytes());
|
|
hasher.update(b"|");
|
|
hasher.update(&imbalance_bar_ewma_alpha.to_le_bytes());
|
|
hasher.update(b"|");
|
|
hasher.update(&volume_bar_size.to_le_bytes());
|
|
hasher.update(b"|");
|
|
|
|
// Hash only file contents (size + mtime), not paths.
|
|
// This makes the key independent of CWD, relative/absolute paths,
|
|
// and directory naming — only actual data changes invalidate it.
|
|
let mut files: Vec<_> = collect_dbn_files_for_hash(data_dir);
|
|
if let Some(dir) = mbp10_dir {
|
|
files.extend(collect_dbn_files_for_hash(dir));
|
|
}
|
|
if let Some(dir) = trades_dir {
|
|
files.extend(collect_dbn_files_for_hash(dir));
|
|
}
|
|
// Sort by filename only (strip parent dirs) for deterministic ordering
|
|
files.sort_by(|a, b| {
|
|
a.file_name().cmp(&b.file_name())
|
|
});
|
|
|
|
if files.is_empty() {
|
|
return Err(anyhow::anyhow!(
|
|
"No .dbn or .dbn.zst files found in {:?} — cannot build cache key",
|
|
data_dir
|
|
));
|
|
}
|
|
|
|
for path in &files {
|
|
// Hash filename (not full path) + size.
|
|
// NOT mtime: PVC remounts and node reboots change file timestamps
|
|
// without changing data, causing cache misses on every new pod.
|
|
if let Some(name) = path.file_name() {
|
|
hasher.update(name.to_string_lossy().as_bytes());
|
|
}
|
|
let meta = path
|
|
.metadata()
|
|
.with_context(|| format!("Failed to stat {:?}", path))?;
|
|
hasher.update(&meta.len().to_le_bytes());
|
|
}
|
|
|
|
Ok(format!("{:x}", hasher.finalize()))
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn test_cache_key_includes_symbol() {
|
|
let dir = Path::new("test_data/futures-baseline");
|
|
if !dir.exists() { return; }
|
|
let key_es = calculate_dbn_cache_key_full(dir, None, None, "ES.FUT", "mbp10", 0.5, 0.1, 100).unwrap();
|
|
let key_nq = calculate_dbn_cache_key_full(dir, None, None, "NQ.FUT", "mbp10", 0.5, 0.1, 100).unwrap();
|
|
assert_ne!(key_es, key_nq, "Different symbols must produce different cache keys");
|
|
}
|
|
|
|
#[test]
|
|
fn test_cache_key_includes_data_source() {
|
|
let dir = Path::new("test_data/futures-baseline");
|
|
if !dir.exists() { return; }
|
|
let key_ohlcv = calculate_dbn_cache_key_full(dir, None, None, "ES.FUT", "ohlcv", 0.5, 0.1, 100).unwrap();
|
|
let key_mbp10 = calculate_dbn_cache_key_full(dir, None, None, "ES.FUT", "mbp10", 0.5, 0.1, 100).unwrap();
|
|
assert_ne!(key_ohlcv, key_mbp10, "Different data sources must produce different cache keys");
|
|
}
|
|
|
|
#[test]
|
|
fn test_cache_key_includes_bar_threshold() {
|
|
let dir = Path::new("test_data/futures-baseline");
|
|
if !dir.exists() { return; }
|
|
let key_a = calculate_dbn_cache_key_full(dir, None, None, "ES.FUT", "mbp10", 0.5, 0.1, 100).unwrap();
|
|
let key_b = calculate_dbn_cache_key_full(dir, None, None, "ES.FUT", "mbp10", 2.5, 0.1, 100).unwrap();
|
|
assert_ne!(key_a, key_b, "Different imbalance_bar_threshold must produce different cache keys");
|
|
}
|
|
|
|
#[test]
|
|
fn test_cache_key_includes_bar_alpha() {
|
|
let dir = Path::new("test_data/futures-baseline");
|
|
if !dir.exists() { return; }
|
|
let key_a = calculate_dbn_cache_key_full(dir, None, None, "ES.FUT", "mbp10", 0.5, 0.1, 100).unwrap();
|
|
let key_b = calculate_dbn_cache_key_full(dir, None, None, "ES.FUT", "mbp10", 0.5, 1.0, 100).unwrap();
|
|
assert_ne!(key_a, key_b, "Different imbalance_bar_ewma_alpha must produce different cache keys");
|
|
}
|
|
|
|
#[test]
|
|
fn test_cache_key_includes_volume_bar_size() {
|
|
let dir = Path::new("test_data/futures-baseline");
|
|
if !dir.exists() { return; }
|
|
let key_a = calculate_dbn_cache_key_full(dir, None, None, "ES.FUT", "ohlcv", 0.5, 0.1, 100).unwrap();
|
|
let key_b = calculate_dbn_cache_key_full(dir, None, None, "ES.FUT", "ohlcv", 0.5, 0.1, 500).unwrap();
|
|
assert_ne!(key_a, key_b, "Different volume_bar_size must produce different cache keys");
|
|
}
|
|
|
|
#[test]
|
|
fn test_cache_key_stable() {
|
|
let dir = Path::new("test_data/futures-baseline");
|
|
if !dir.exists() { return; }
|
|
let key1 = calculate_dbn_cache_key_full(dir, None, None, "ES.FUT", "mbp10", 0.5, 0.1, 100).unwrap();
|
|
let key2 = calculate_dbn_cache_key_full(dir, None, None, "ES.FUT", "mbp10", 0.5, 0.1, 100).unwrap();
|
|
assert_eq!(key1, key2, "Same inputs must produce same key");
|
|
}
|
|
}
|
|
|
|
/// Collect all .dbn and .dbn.zst paths recursively (for hashing purposes).
|
|
fn collect_dbn_files_for_hash(dir: &Path) -> Vec<PathBuf> {
|
|
let mut out = Vec::new();
|
|
if let Ok(entries) = std::fs::read_dir(dir) {
|
|
for entry in entries.flatten() {
|
|
let path = entry.path();
|
|
if path.is_dir() {
|
|
out.extend(collect_dbn_files_for_hash(&path));
|
|
} else if path.extension().and_then(|s| s.to_str()) == Some("dbn")
|
|
|| path.to_string_lossy().ends_with(".dbn.zst")
|
|
{
|
|
out.push(path);
|
|
}
|
|
}
|
|
}
|
|
out
|
|
}
|