Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

ci: fix smart_crusher branch CI failures + add make ci-precheck pre-push gate

chopratejasd6a00ee89c27
diff --git a/.commitlintrc.json b/.commitlintrc.json
index e3b5a6673..d42e91489 100644
--- a/.commitlintrc.json
+++ b/.commitlintrc.json
@@ -1,6 +1,26 @@
{
"extends": ["@commitlint/config-conventional"],
"rules": {
- "body-max-line-length": [2, "always", 200]
+ "body-max-line-length": [2, "always", 200],
+ "footer-leading-blank": [0],
+ "subject-case": [0],
+ "type-enum": [
+ 2,
+ "always",
+ [
+ "build",
+ "chore",
+ "ci",
+ "docs",
+ "feat",
+ "fix",
+ "parity",
+ "perf",
+ "refactor",
+ "revert",
+ "style",
+ "test"
+ ]
+ ]
}
}
\ No newline at end of file
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 1708331d0..63a48bd40 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -122,6 +122,39 @@ jobs:
python -m pip install --upgrade pip
pip install -e ".[dev,relevance]"
+ # `tests/test_relevance.py::TestSmartCrusherIntegration` constructs
+ # a `SmartCrusher`, which is a hard import of `headroom._core` since
+ # the Python implementation was retired in Stage 3c.1b. Without the
+ # extension built, those tests `ModuleNotFoundError`. Build + install
+ # the wheel and symlink the `.so` into the in-tree `headroom/` so
+ # the editable install resolves it (same pattern as the main `test`
+ # job above).
+ - name: Install Rust toolchain
+ uses: dtolnay/rust-toolchain@stable
+
+ - name: Cache cargo registry + build
+ uses: Swatinem/rust-cache@v2
+ with:
+ workspaces: ". -> target"
+
+ - name: Install maturin
+ run: pip install 'maturin>=1.5,<2.0'
+
+ - name: Build Rust extension (headroom._core)
+ run: |
+ set -euo pipefail
+ maturin build --release -m crates/headroom-py/Cargo.toml --out dist
+ pip install --force-reinstall --no-deps dist/headroom_core_py-*.whl
+ SITE_PACKAGES=$(python -c "import site; print(site.getsitepackages()[0])")
+ SO_FILE=$(find "$SITE_PACKAGES/headroom" -maxdepth 1 -name "_core.cpython-*.so" -print -quit 2>/dev/null)
+ if [[ -z "$SO_FILE" ]]; then
+ echo "error: could not find _core.cpython-*.so under $SITE_PACKAGES/headroom/" >&2
+ ls -la "$SITE_PACKAGES/headroom/" || true
+ exit 1
+ fi
+ ln -sf "$SO_FILE" "headroom/$(basename "$SO_FILE")"
+ python -c "from headroom._core import SmartCrusher; print('headroom._core OK:', SmartCrusher)"
+
- name: Run relevance tests
run: |
pytest tests/test_relevance.py -v
diff --git a/.github/workflows/eval.yml b/.github/workflows/eval.yml
index 2224a9a83..fb6a470cc 100644
--- a/.github/workflows/eval.yml
+++ b/.github/workflows/eval.yml
@@ -22,6 +22,36 @@ jobs:
python-version: "3.11"
- name: Install dependencies
run: pip install -e ".[all]"
+
+ # `compression_only.evaluate_ccr_lossless` constructs a SmartCrusher
+ # which now hard-imports `headroom._core` (Stage 3c.1b). Build the
+ # Rust extension before running the smoke test or every call raises
+ # `ModuleNotFoundError`. Mirrors the main CI `test` job pattern.
+ - name: Install Rust toolchain
+ uses: dtolnay/rust-toolchain@stable
+
+ - name: Cache cargo registry + build
+ uses: Swatinem/rust-cache@v2
+ with:
+ workspaces: ". -> target"
+
+ - name: Install maturin
+ run: pip install 'maturin>=1.5,<2.0'
+
+ - name: Build Rust extension (headroom._core)
+ run: |
+ set -euo pipefail
+ maturin build --release -m crates/headroom-py/Cargo.toml --out dist
+ pip install --force-reinstall --no-deps dist/headroom_core_py-*.whl
+ SITE_PACKAGES=$(python -c "import site; print(site.getsitepackages()[0])")
+ SO_FILE=$(find "$SITE_PACKAGES/headroom" -maxdepth 1 -name "_core.cpython-*.so" -print -quit 2>/dev/null)
+ if [[ -z "$SO_FILE" ]]; then
+ echo "error: could not find _core.cpython-*.so under $SITE_PACKAGES/headroom/" >&2
+ exit 1
+ fi
+ ln -sf "$SO_FILE" "headroom/$(basename "$SO_FILE")"
+ python -c "from headroom._core import SmartCrusher; print('headroom._core OK:', SmartCrusher)"
+
- name: Run CCR round-trip (zero cost)
run: |
python -c "
diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml
index ee0cb7196..1b3e9732f 100644
--- a/.github/workflows/rust.yml
+++ b/.github/workflows/rust.yml
@@ -67,9 +67,14 @@ jobs:
- os: macos-14
target: aarch64-apple-darwin
maturin-target: aarch64-apple-darwin
- - os: macos-15-intel
- target: x86_64-apple-darwin
- maturin-target: x86_64-apple-darwin
+ # macOS x86_64 (Intel) is NOT in this matrix.
+ # `fastembed` → `ort` → `ort-sys` does not publish prebuilt ONNX
+ # Runtime binaries for `x86_64-apple-darwin`; building from source
+ # in CI is a multi-hour cmake job. Apple Silicon has been the
+ # default macOS target since 2020 and is sufficient for the wheels
+ # we ship. If a customer needs Intel macOS, build from source
+ # locally (the toolchain works; only prebuilt distribution skips
+ # this target).
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
diff --git a/.gitignore b/.gitignore
index cdf391e73..eead22449 100644
--- a/.gitignore
+++ b/.gitignore
@@ -20,6 +20,7 @@ scripts/*
!scripts/fixtures/*.json
!scripts/record_fixtures.py
!scripts/build_rust_extension.sh
+!scripts/install-git-hooks.sh
# Rust / Cargo build artifacts
/target/
diff --git a/Makefile b/Makefile
index b0cb44958..cc02bc05d 100644
--- a/Makefile
+++ b/Makefile
@@ -7,19 +7,26 @@ MATURIN ?= maturin
PYTHON ?= python3
FIXTURES ?= tests/parity/fixtures
-.PHONY: help test test-parity bench build-proxy build-wheel fmt fmt-check lint clippy clean
+.PHONY: help test test-parity bench build-proxy build-wheel fmt fmt-check lint clippy clean ci-precheck ci-precheck-rust ci-precheck-python ci-precheck-commitlint install-git-hooks
help:
@echo "Headroom Rust targets:"
- @echo " make test - cargo test --workspace"
- @echo " make test-parity - maturin develop + parity-run against fixtures"
- @echo " make bench - cargo bench --workspace"
- @echo " make build-proxy - release build + strip headroom-proxy, print size"
- @echo " make build-wheel - release wheel for headroom-py"
- @echo " make fmt - cargo fmt --all"
- @echo " make fmt-check - cargo fmt --all -- --check"
- @echo " make lint - cargo clippy --workspace -- -D warnings"
- @echo " make clean - cargo clean"
+ @echo " make test - cargo test --workspace"
+ @echo " make test-parity - maturin develop + parity-run against fixtures"
+ @echo " make bench - cargo bench --workspace"
+ @echo " make build-proxy - release build + strip headroom-proxy, print size"
+ @echo " make build-wheel - release wheel for headroom-py"
+ @echo " make fmt - cargo fmt --all"
+ @echo " make fmt-check - cargo fmt --all -- --check"
+ @echo " make lint - cargo clippy --workspace -- -D warnings"
+ @echo " make clean - cargo clean"
+ @echo ""
+ @echo "Pre-push verification (run BEFORE git push to catch CI failures locally):"
+ @echo " make ci-precheck - run all CI gates (rust + python + commitlint)"
+ @echo " make ci-precheck-rust - cargo fmt --check + clippy + test"
+ @echo " make ci-precheck-python - smart_crusher-affected python tests"
+ @echo " make ci-precheck-commitlint - lint commits since origin/main"
+ @echo " make install-git-hooks - install a pre-push hook that runs ci-precheck"
test:
$(CARGO) test --workspace
@@ -56,3 +63,66 @@ clippy lint:
clean:
$(CARGO) clean
+
+# ─── Pre-push CI gate ──────────────────────────────────────────────────────
+#
+# These targets run the same checks GitHub Actions runs, locally. The intent
+# is: if `make ci-precheck` is green, `git push` will not turn red. The
+# 2026-04-27 push surfaced five CI breaks (cargo fmt drift, x86_64-apple-
+# darwin wheel, headroom._core not built in test-extras + smoke-test,
+# commitlint footer-leading-blank). The first three are caught by the gates
+# below; the last two are caught by the workflow fixes themselves.
+#
+# Run before EVERY `git push`. Install the git hook (one-time) with:
+# make install-git-hooks
+
+ci-precheck: ci-precheck-rust ci-precheck-python ci-precheck-commitlint
+ @echo ""
+ @echo "✅ ci-precheck PASSED — safe to push."
+
+ci-precheck-rust:
+ @echo "── ci-precheck-rust ────────────────────────────────────────────"
+ $(CARGO) fmt --all -- --check
+ $(CARGO) clippy --workspace -- -D warnings
+ $(CARGO) test --workspace
+
+# Mirrors the smart_crusher-affected test files we expect green on every
+# push. Builds the Rust extension first because most of these tests
+# instantiate `SmartCrusher`, which hard-imports `headroom._core`.
+ci-precheck-python:
+ @echo "── ci-precheck-python ─────────────────────────────────────────"
+ @if [ -z "$$VIRTUAL_ENV" ]; then \
+ echo "error: activate a venv first (e.g. source .venv/bin/activate)"; \
+ exit 1; \
+ fi
+ bash scripts/build_rust_extension.sh
+ $(PYTHON) -m pytest -q \
+ tests/test_transforms/test_smart_crusher_bugs.py \
+ tests/test_transforms/test_smart_crusher_rust_parity.py \
+ tests/test_transforms/test_diff_compressor.py \
+ tests/test_transforms/test_diff_compressor_rust_parity.py \
+ tests/test_relevance.py \
+ tests/test_relevance_extra.py \
+ tests/test_ccr.py \
+ tests/test_acceptance.py \
+ tests/test_critical_fixes.py \
+ tests/test_quality_retention.py \
+ tests/test_toin_integration.py
+
+# Lint commits since `origin/main`. Requires npx (Node 18+) on PATH.
+# Skips silently if npx is unavailable; install nodejs to enable.
+ci-precheck-commitlint:
+ @echo "── ci-precheck-commitlint ─────────────────────────────────────"
+ @if ! command -v npx >/dev/null 2>&1; then \
+ echo "skip: npx not on PATH (install node 18+ to enable commitlint pre-check)"; \
+ exit 0; \
+ fi
+ @if ! git rev-parse --verify origin/main >/dev/null 2>&1; then \
+ echo "skip: origin/main not fetched (run 'git fetch origin main')"; \
+ exit 0; \
+ fi
+ npx --yes --package=@commitlint/cli --package=@commitlint/config-conventional -- \
+ commitlint --from origin/main --to HEAD --config .commitlintrc.json
+
+install-git-hooks:
+ @scripts/install-git-hooks.sh
diff --git a/crates/headroom-core/src/relevance/base.rs b/crates/headroom-core/src/relevance/base.rs
index 36674bc3e..d5b2bedaf 100644
--- a/crates/headroom-core/src/relevance/base.rs
+++ b/crates/headroom-core/src/relevance/base.rs
@@ -72,7 +72,10 @@ pub fn default_batch_score<S: RelevanceScorer>(
items: &[&str],
context: &str,
) -> Vec<RelevanceScore> {
- items.iter().map(|item| scorer.score(item, context)).collect()
+ items
+ .iter()
+ .map(|item| scorer.score(item, context))
+ .collect()
}
#[cfg(test)]
diff --git a/crates/headroom-core/src/relevance/bm25.rs b/crates/headroom-core/src/relevance/bm25.rs
index 883f3bd3b..1a722c157 100644
--- a/crates/headroom-core/src/relevance/bm25.rs
+++ b/crates/headroom-core/src/relevance/bm25.rs
@@ -172,7 +172,12 @@ impl RelevanceScorer for BM25Scorer {
n => {
let preview: Vec<&str> = matched.iter().take(3).map(|s| s.as_str()).collect();
let suffix = if n > 3 { "..." } else { "" };
- format!("BM25: matched {} terms ({}{})", n, preview.join(", "), suffix)
+ format!(
+ "BM25: matched {} terms ({}{})",
+ n,
+ preview.join(", "),
+ suffix
+ )
}
};
@@ -275,7 +280,10 @@ mod tests {
#[test]
fn score_no_match_returns_zero() {
- let s = scorer().score(r#"{"id": 1, "name": "alice"}"#, "completely unrelated query");
+ let s = scorer().score(
+ r#"{"id": 1, "name": "alice"}"#,
+ "completely unrelated query",
+ );
assert_eq!(s.score, 0.0);
assert_eq!(s.reason, "BM25: no term matches");
assert!(s.matched_terms.is_empty());
@@ -284,10 +292,7 @@ mod tests {
#[test]
fn score_uuid_match_gets_long_token_bonus() {
let item = r#"{"id": "550e8400-e29b-41d4-a716-446655440000", "name": "Alice"}"#;
- let s = scorer().score(
- item,
- "find record 550e8400-e29b-41d4-a716-446655440000",
- );
+ let s = scorer().score(item, "find record 550e8400-e29b-41d4-a716-446655440000");
// Long-match bonus is +0.3, applied after normalization.
// Even a low raw score should clear 0.3 with the bonus.
assert!(
diff --git a/crates/headroom-core/src/relevance/embedding.rs b/crates/headroom-core/src/relevance/embedding.rs
index bd3b93f14..aebdf3331 100644
--- a/crates/headroom-core/src/relevance/embedding.rs
+++ b/crates/headroom-core/src/relevance/embedding.rs
@@ -182,11 +182,7 @@ impl RelevanceScorer for EmbeddingScorer {
.take(items.len())
.map(|emb| {
let sim = cosine_similarity(emb, &context_emb);
- RelevanceScore::new(
- sim,
- format!("Embedding: {:.2}", sim),
- Vec::new(),
- )
+ RelevanceScore::new(sim, format!("Embedding: {:.2}", sim), Vec::new())
})
.collect()
}
diff --git a/crates/headroom-core/src/relevance/hybrid.rs b/crates/headroom-core/src/relevance/hybrid.rs
index 6f6e62659..7a253b41b 100644
--- a/crates/headroom-core/src/relevance/hybrid.rs
+++ b/crates/headroom-core/src/relevance/hybrid.rs
@@ -199,7 +199,10 @@ impl RelevanceScorer for HybridScorer {
let bm25_results = self.bm25.score_batch(items, context);
if !self.embedding_available {
- return bm25_results.iter().map(|r| self.boost_bm25_only(r)).collect();
+ return bm25_results
+ .iter()
+ .map(|r| self.boost_bm25_only(r))
+ .collect();
}
let emb_results = self.embedding.score_batch(items, context);
diff --git a/crates/headroom-core/src/relevance/mod.rs b/crates/headroom-core/src/relevance/mod.rs
index c867f628f..59f002ee6 100644
--- a/crates/headroom-core/src/relevance/mod.rs
+++ b/crates/headroom-core/src/relevance/mod.rs
@@ -50,8 +50,10 @@ pub fn create_scorer(tier: &str) -> Result<Box<dyn RelevanceScorer + Send + Sync
if s.is_available() {
Ok(Box::new(s))
} else {
- Err("EmbeddingScorer requires the ONNX backend (not yet implemented in Rust)"
- .to_string())
+ Err(
+ "EmbeddingScorer requires the ONNX backend (not yet implemented in Rust)"
+ .to_string(),
+ )
}
}
other => Err(format!(
diff --git a/crates/headroom-core/src/transforms/adaptive_sizer.rs b/crates/headroom-core/src/transforms/adaptive_sizer.rs
index 4d88797fe..63c7beb90 100644
--- a/crates/headroom-core/src/transforms/adaptive_sizer.rs
+++ b/crates/headroom-core/src/transforms/adaptive_sizer.rs
@@ -51,12 +51,7 @@ use std::io::Write;
/// harder).
/// - `min_k`: lower bound on the return value.
/// - `max_k`: upper bound; `None` means "no cap" (i.e. up to `items.len()`).
-pub fn compute_optimal_k(
- items: &[&str],
- bias: f64,
- min_k: usize,
- max_k: Option<usize>,
-) -> usize {
+pub fn compute_optimal_k(items: &[&str], bias: f64, min_k: usize, max_k: Option<usize>) -> usize {
let n = items.len();
let effective_max = max_k.unwrap_or(n);
@@ -279,12 +274,7 @@ pub fn count_unique_simhash(items: &[&str], threshold: u32) -> usize {
///
/// `tolerance` is the maximum allowed ratio difference (Python default
/// 0.15 = 15%).
-pub fn validate_with_zlib(
- items: &[&str],
- k: usize,
- max_k: usize,
- tolerance: f64,
-) -> usize {
+pub fn validate_with_zlib(items: &[&str], k: usize, max_k: usize, tolerance: f64) -> usize {
if k >= items.len() || k >= max_k {
return k;
}
@@ -539,7 +529,12 @@ mod tests {
// 20 diverse items with similar per-item compressibility — full
// and subset get similar ratios → no bump.
let many: Vec<String> = (0..20)
- .map(|i| format!("entry id={} payload=item value with content for item number {}", i, i))
+ .map(|i| {
+ format!(
+ "entry id={} payload=item value with content for item number {}",
+ i, i
+ )
+ })
.collect();
let items: Vec<&str> = many.iter().map(|s| s.as_str()).collect();
let result = validate_with_zlib(&items, 10, 100, 0.15);
@@ -599,7 +594,17 @@ mod tests {
let k_low = compute_optimal_k(&refs, 0.7, 3, None);
let k_mid = compute_optimal_k(&refs, 1.0, 3, None);
let k_high = compute_optimal_k(&refs, 1.5, 3, None);
- assert!(k_low <= k_mid, "bias 0.7 → {} should be ≤ bias 1.0 → {}", k_low, k_mid);
- assert!(k_mid <= k_high, "bias 1.0 → {} should be ≤ bias 1.5 → {}", k_mid, k_high);
+ assert!(
+ k_low <= k_mid,
+ "bias 0.7 → {} should be ≤ bias 1.0 → {}",
+ k_low,
+ k_mid
+ );
+ assert!(
+ k_mid <= k_high,
+ "bias 1.0 → {} should be ≤ bias 1.5 → {}",
+ k_mid,
+ k_high
+ );
}
}
diff --git a/crates/headroom-core/src/transforms/anchor_selector.rs b/crates/headroom-core/src/transforms/anchor_selector.rs
index ff6dbfc20..bc1a94f14 100644
--- a/crates/headroom-core/src/transforms/anchor_selector.rs
+++ b/crates/headroom-core/src/transforms/anchor_selector.rs
@@ -890,10 +890,7 @@ mod tests {
// Python ensure_ascii=True: 'café' → '\\u00e9' for é.
// Reference verified via: json.dumps({"k": "café"}, sort_keys=True)
let v = json!({"k": "café"});
- assert_eq!(
- python_json_dumps_sort_keys(&v),
- "{\"k\": \"caf\\u00e9\"}"
- );
+ assert_eq!(python_json_dumps_sort_keys(&v), "{\"k\": \"caf\\u00e9\"}");
}
#[test]
@@ -941,10 +938,7 @@ mod tests {
fn compute_item_hash_matches_python_with_unicode() {
// Reference: hashlib.md5(json.dumps({"k":"café"}, sort_keys=True).encode())
// .hexdigest()[:16] = "6761da28ed7eb489"
- assert_eq!(
- compute_item_hash(&json!({"k": "café"})),
- "6761da28ed7eb489"
- );
+ assert_eq!(compute_item_hash(&json!({"k": "café"})), "6761da28ed7eb489");
}
#[test]
diff --git a/crates/headroom-core/src/transforms/smart_crusher/analyzer.rs b/crates/headroom-core/src/transforms/smart_crusher/analyzer.rs
index f525d6aac..acbdf642e 100644
--- a/crates/headroom-core/src/transforms/smart_crusher/analyzer.rs
+++ b/crates/headroom-core/src/transforms/smart_crusher/analyzer.rs
@@ -37,13 +37,9 @@ use serde_json::Value;
use std::collections::{BTreeMap, BTreeSet};
use super::config::SmartCrusherConfig;
-use super::field_detect::{
- detect_id_field_statistically, detect_score_field_statistically,
-};
+use super::field_detect::{detect_id_field_statistically, detect_score_field_statistically};
use super::stats_math::{mean, sample_stdev, sample_variance};
-use super::types::{
- ArrayAnalysis, CompressionStrategy, CrushabilityAnalysis, FieldStats,
-};
+use super::types::{ArrayAnalysis, CompressionStrategy, CrushabilityAnalysis, FieldStats};
/// Statistical analyzer for compression decisions.
///
@@ -111,7 +107,8 @@ impl SmartAnalyzer {
let crushability = self.analyze_crushability(items, &field_stats);
- let strategy = self.select_strategy(&field_stats, &pattern, items.len(), Some(&crushability));
+ let strategy =
+ self.select_strategy(&field_stats, &pattern, items.len(), Some(&crushability));
let reduction = if strategy == CompressionStrategy::Skip {
0.0
@@ -349,9 +346,7 @@ impl SmartAnalyzer {
let avg_len = stats.avg_length.unwrap_or(0.0);
if stats.unique_ratio > 0.5 && avg_len > 20.0 {
has_message_like = true;
- } else if stats.unique_ratio < 0.1
- && (2..=10).contains(&stats.unique_count)
- {
+ } else if stats.unique_ratio < 0.1 && (2..=10).contains(&stats.unique_count) {
has_level_like = true;
}
}
@@ -405,8 +400,7 @@ impl SmartAnalyzer {
// which is falsy for 0; we mirror by checking `mn != 0` to
// match Python's behavior (very unlikely range edge but pinned).
let unix_seconds = (1_000_000_000.0..=2_000_000_000.0).contains(&mn);
- let unix_millis =
- (1_000_000_000_000.0..=2_000_000_000_000.0).contains(&mn);
+ let unix_millis = (1_000_000_000_000.0..=2_000_000_000_000.0).contains(&mn);
if unix_seconds || unix_millis {
return true;
}
@@ -429,9 +423,7 @@ impl SmartAnalyzer {
items: &[Value],
field_stats: &BTreeMap<String, FieldStats>,
) -> CrushabilityAnalysis {
- use super::outliers::{
- detect_error_items_for_preservation, detect_structural_outliers,
- };
+ use super::outliers::{detect_error_items_for_preservation, detect_structural_outliers};
let mut signals_present: Vec<String> = Vec::new();
let mut signals_absent: Vec<String> = Vec::new();
@@ -505,8 +497,12 @@ impl SmartAnalyzer {
}
let threshold = self.config.variance_threshold * std;
for (i, item) in items.iter().enumerate() {
- let Some(obj) = item.as_object() else { continue };
- let Some(v) = obj.get(&stats.name) else { continue };
+ let Some(obj) = item.as_object() else {
+ continue;
+ };
+ let Some(v) = obj.get(&stats.name) else {
+ continue;
+ };
if let Some(num) = v.as_f64() {
if !num.is_nan() && (num - mean_val).abs() > threshold {
anomaly_indices.insert(i);
@@ -546,8 +542,7 @@ impl SmartAnalyzer {
};
let max_uniqueness = avg_string_uniqueness.max(id_uniqueness).max(0.0);
- let non_id_content_uniqueness =
- avg_string_uniqueness.max(avg_non_id_numeric_uniqueness);
+ let non_id_content_uniqueness = avg_string_uniqueness.max(avg_non_id_numeric_uniqueness);
// 6. Change points.
let has_change_points = field_stats
@@ -1120,9 +1115,7 @@ mod tests {
#[test]
fn crushability_repetitive_content_with_ids_crushes() {
// Unique ID + constant content field → repetitive_content path.
- let items: Vec<Value> = (0..20)
- .map(|i| json!({"id": i, "status": "ok"}))
- .collect();
+ let items: Vec<Value> = (0..20).map(|i| json!({"id": i, "status": "ok"})).collect();
let a = analyzer();
let mut fs: BTreeMap<String, FieldStats> = BTreeMap::new();
for k in ["id", "status"] {
diff --git a/crates/headroom-core/src/transforms/smart_crusher/anchors.rs b/crates/headroom-core/src/transforms/smart_crusher/anchors.rs
index 1a86f3936..69aa42a32 100644
--- a/crates/headroom-core/src/transforms/smart_crusher/anchors.rs
+++ b/crates/headroom-core/src/transforms/smart_crusher/anchors.rs
@@ -311,10 +311,7 @@ mod tests {
fn item_matches_anchor_in_key() {
let anchors: HashSet<String> = ["status".to_string()].into_iter().collect();
// The anchor "status" appears in the JSON-serialized key.
- assert!(item_matches_anchors(
- &json!({"status": "ok"}),
- &anchors
- ));
+ assert!(item_matches_anchors(&json!({"status": "ok"}), &anchors));
}
#[test]
@@ -359,10 +356,7 @@ mod tests {
// below would fail.
let v = json!({"name": "Alice", "ok": true, "count": 5, "val": null});
let r = python_repr(&v);
- assert_eq!(
- r,
- "{'name': 'Alice', 'ok': True, 'count': 5, 'val': None}"
- );
+ assert_eq!(r, "{'name': 'Alice', 'ok': True, 'count': 5, 'val': None}");
}
#[test]
diff --git a/crates/headroom-core/src/transforms/smart_crusher/crusher.rs b/crates/headroom-core/src/transforms/smart_crusher/crusher.rs
index fa8ded62d..ad7b4ce19 100644
--- a/crates/headroom-core/src/transforms/smart_crusher/crusher.rs
+++ b/crates/headroom-core/src/transforms/smart_crusher/crusher.rs
@@ -38,9 +38,7 @@ use serde_json::Value;
use super::analyzer::SmartAnalyzer;
use super::classifier::{classify_array, ArrayType};
use super::config::SmartCrusherConfig;
-use super::crushers::{
- compute_k_split, crush_number_array, crush_object, crush_string_array,
-};
+use super::crushers::{compute_k_split, crush_number_array, crush_object, crush_string_array};
use super::planning::SmartCrusherPlanner;
use super::types::{CompressionPlan, CompressionStrategy, CrushResult};
use crate::relevance::{HybridScorer, RelevanceScorer};
@@ -211,23 +209,24 @@ impl SmartCrusher {
match arr_type {
ArrayType::DictArray => {
let result = self.crush_array(arr, query_context, bias);
- info_parts
- .push(format!("{}({}->{})", result.strategy_info, n, result.items.len()));
+ info_parts.push(format!(
+ "{}({}->{})",
+ result.strategy_info,
+ n,
+ result.items.len()
+ ));
return (Value::Array(result.items), info_parts.join(","));
}
ArrayType::StringArray => {
- let strs: Vec<&str> =
- arr.iter().filter_map(|v| v.as_str()).collect();
- let (crushed, strategy) =
- crush_string_array(&strs, &self.config, bias);
+ let strs: Vec<&str> = arr.iter().filter_map(|v| v.as_str()).collect();
+ let (crushed, strategy) = crush_string_array(&strs, &self.config, bias);
info_parts.push(format!("{}({}->{})", strategy, n, crushed.len()));
let crushed_values: Vec<Value> =
crushed.into_iter().map(Value::String).collect();
return (Value::Array(crushed_values), info_parts.join(","));
}
ArrayType::NumberArray => {
- let (crushed, strategy) =
- crush_number_array(arr, &self.config, bias);
+ let (crushed, strategy) = crush_number_array(arr, &self.config, bias);
info_parts.push(format!("{}({}->{})", strategy, n, crushed.len()));
return (Value::Array(crushed), info_parts.join(","));
}
@@ -246,8 +245,7 @@ impl SmartCrusher {
// Below threshold or not crushable → recurse into items.
let mut processed: Vec<Value> = Vec::with_capacity(n);
for item in arr {
- let (p_item, p_info) =
- self.process_value(item, depth + 1, query_context, bias);
+ let (p_item, p_info) = self.process_value(item, depth + 1, query_context, bias);
processed.push(p_item);
if !p_info.is_empty() {
info_parts.push(p_info);
@@ -259,8 +257,7 @@ impl SmartCrusher {
// First pass: recurse into values to compress nested arrays.
let mut processed = serde_json::Map::new();
for (k, v) in map {
- let (p_val, p_info) =
- self.process_value(v, depth + 1, query_context, bias);
+ let (p_val, p_info) = self.process_value(v, depth + 1, query_context, bias);
processed.insert(k.clone(), p_val);
if !p_info.is_empty() {
info_parts.push(p_info);
@@ -401,8 +398,7 @@ impl SmartCrusher {
groups.push(group_key(item), i, item.clone());
}
- let mut keep_indices: std::collections::BTreeSet<usize> =
- std::collections::BTreeSet::new();
+ let mut keep_indices: std::collections::BTreeSet<usize> = std::collections::BTreeSet::new();
let mut strategy_parts: Vec<String> = Vec::new();
for (type_key, indices, values) in groups.into_iter() {
@@ -414,17 +410,14 @@ impl SmartCrusher {
match type_key {
"dict" => {
- let CrushArrayResult {
- items: crushed, ..
- } = self.crush_array(&values, query_context, bias);
+ let CrushArrayResult { items: crushed, .. } =
+ self.crush_array(&values, query_context, bias);
// Find which original indices survived by matching
// canonical-JSON serialization. Mirrors Python's
// `json.dumps(c, sort_keys=True, default=str)`-keyed
// set match.
- let crushed_keys: std::collections::HashSet<String> = crushed
- .iter()
- .map(canonical_json_for_match)
- .collect();
+ let crushed_keys: std::collections::HashSet<String> =
+ crushed.iter().map(canonical_json_for_match).collect();
for (i, idx) in indices.iter().enumerate() {
if crushed_keys.contains(&canonical_json_for_match(&values[i])) {
keep_indices.insert(*idx);
@@ -450,21 +443,15 @@ impl SmartCrusher {
// Python: just adaptive sampling + outlier detection
// (no summary prefix). Keeps first/last by index
// and items >variance_threshold σ from mean.
- let item_strings: Vec<String> =
- values.iter().map(|v| v.to_string()).collect();
- let item_refs: Vec<&str> =
- item_strings.iter().map(|s| s.as_str()).collect();
+ let item_strings: Vec<String> = values.iter().map(|v| v.to_string()).collect();
+ let item_refs: Vec<&str> = item_strings.iter().map(|s| s.as_str()).collect();
let (_kt, kf, kl, _) = compute_k_split(&item_refs, &self.config, bias);
let kf = kf.min(values.len());
let kl = kl.min(values.len().saturating_sub(kf));
let first_idx: Vec<usize> = indices.iter().take(kf).copied().collect();
- let last_idx: Vec<usize> = indices
- .iter()
- .rev()
- .take(kl)
- .copied()
- .collect::<Vec<_>>();
+ let last_idx: Vec<usize> =
+ indices.iter().rev().take(kl).copied().collect::<Vec<_>>();
keep_indices.extend(&first_idx);
keep_indices.extend(&last_idx);
@@ -474,18 +461,12 @@ impl SmartCrusher {
.filter_map(|v| v.as_f64().filter(|f| f.is_finite()))
.collect();
if finite.len() > 1 {
- if let Some(mean_v) =
- super::stats_math::mean(&finite)
- {
- if let Some(std_v) =
- super::stats_math::sample_stdev(&finite)
- {
+ if let Some(mean_v) = super::stats_math::mean(&finite) {
+ if let Some(std_v) = super::stats_math::sample_stdev(&finite) {
if std_v > 0.0 {
let threshold = self.config.variance_threshold * std_v;
for (i, val) in values.iter().enumerate() {
- if let Some(num) =
- val.as_f64().filter(|f| f.is_finite())
- {
+ if let Some(num) = val.as_f64().filter(|f| f.is_finite()) {
if (num - mean_v).abs() > threshold {
keep_indices.insert(indices[i]);
}
@@ -662,25 +643,21 @@ mod tests {
let c = crusher();
let items: Vec<Value> = (0..30).map(|_| json!({"status": "ok"})).collect();
let result = c.crush_array(&items, "", 1.0);
- assert!(
- result.items.len() <= 30,
- "should not exceed original count"
- );
+ assert!(result.items.len() <= 30, "should not exceed original count");
}
#[test]
fn crush_array_keeps_error_items() {
let c = crusher();
- let mut items: Vec<Value> = (0..30)
- .map(|i| json!({"id": i, "status": "ok"}))
- .collect();
+ let mut items: Vec<Value> = (0..30).map(|i| json!({"id": i, "status": "ok"})).collect();
items.push(json!({"id": 30, "status": "error", "msg": "FATAL"}));
let result = c.crush_array(&items, "", 1.0);
// Whatever path is taken, the error item should survive.
assert!(
- result.items.iter().any(|item| {
- item.get("status").and_then(|v| v.as_str()) == Some("error")
- }),
+ result
+ .items
+ .iter()
+ .any(|item| { item.get("status").and_then(|v| v.as_str()) == Some("error") }),
"error item must survive crush_array"
);
}
@@ -709,9 +686,7 @@ mod tests {
fn crush_mixed_groups_and_compresses_dicts() {
let c = crusher();
// 25 dicts (large group → gets crushed) + 5 strings (small group → all kept).
- let mut items: Vec<Value> = (0..25)
- .map(|i| json!({"id": i, "status": "ok"}))
- .collect();
+ let mut items: Vec<Value> = (0..25).map(|i| json!({"id": i, "status": "ok"})).collect();
for i in 0..5 {
items.push(json!(format!("string_{}", i)));
}
@@ -844,5 +819,4 @@ mod tests {
let result = c.crush_array(&items, "anything", 1.0);
assert!(result.items.len() <= 30);
}
-
}
diff --git a/crates/headroom-core/src/transforms/smart_crusher/crushers.rs b/crates/headroom-core/src/transforms/smart_crusher/crushers.rs
index e60477cf1..43c50498a 100644
--- a/crates/headroom-core/src/transforms/smart_crusher/crushers.rs
+++ b/crates/headroom-core/src/transforms/smart_crusher/crushers.rs
@@ -403,7 +403,13 @@ pub fn crush_object(
let keys: Vec<&String> = obj.keys().collect();
let kv_strings: Vec<String> = keys
.iter()
- .map(|k| format!("{}: {}", k, serde_json::to_string(&obj[k.as_str()]).unwrap_or_default()))
+ .map(|k| {
+ format!(
+ "{}: {}",
+ k,
+ serde_json::to_string(&obj[k.as_str()]).unwrap_or_default()
+ )
+ })
.collect();
let kv_refs: Vec<&str> = kv_strings.iter().map(|s| s.as_str()).collect();
@@ -421,7 +427,9 @@ pub fn crush_object(
// Always keep: error-keyword values.
let mut keep_keys: HashSet<String> = HashSet::new();
for (key, val) in obj {
- let val_str = serde_json::to_string(val).unwrap_or_default().to_lowercase();
+ let val_str = serde_json::to_string(val)
+ .unwrap_or_default()
+ .to_lowercase();
if ERROR_KEYWORDS.iter().any(|kw| val_str.contains(kw)) {
keep_keys.insert(key.clone());
}
@@ -534,7 +542,11 @@ fn format_number_repr(x: f64) -> String {
return "nan".to_string();
}
if x.is_infinite() {
- return if x > 0.0 { "inf".to_string() } else { "-inf".to_string() };
+ return if x > 0.0 {
+ "inf".to_string()
+ } else {
+ "-inf".to_string()
+ };
}
if x.fract() == 0.0 && x.abs() < 1e16 {
return format!("{}", x as i64);
@@ -630,7 +642,13 @@ mod tests {
#[test]
fn string_array_keeps_error_strings() {
let items: Vec<&str> = (0..30)
- .map(|i| if i == 15 { "FATAL: out of memory" } else { "ok" })
+ .map(|i| {
+ if i == 15 {
+ "FATAL: out of memory"
+ } else {
+ "ok"
+ }
+ })
.collect();
let (out, strat) = crush_string_array(&items, &cfg(), 1.0);
// Error item at index 15 must survive.
@@ -773,7 +791,10 @@ mod tests {
);
}
let (out, _) = crush_object(&obj, &cfg(), 1.0);
- assert!(out.contains_key("tiny"), "tiny key (small value) must survive");
+ assert!(
+ out.contains_key("tiny"),
+ "tiny key (small value) must survive"
+ );
}
#[test]
diff --git a/crates/headroom-core/src/transforms/smart_crusher/error_keywords.rs b/crates/headroom-core/src/transforms/smart_crusher/error_keywords.rs
index 7661b8b2c..74d305342 100644
--- a/crates/headroom-core/src/transforms/smart_crusher/error_keywords.rs
+++ b/crates/headroom-core/src/transforms/smart_crusher/error_keywords.rs
@@ -44,7 +44,11 @@ mod tests {
#[test]
fn all_lowercase_invariant() {
for &kw in ERROR_KEYWORDS {
- assert_eq!(kw, kw.to_lowercase(), "ERROR_KEYWORDS must all be lowercase");
+ assert_eq!(
+ kw,
+ kw.to_lowercase(),
+ "ERROR_KEYWORDS must all be lowercase"
+ );
}
}
@@ -53,8 +57,18 @@ mod tests {
// Pin the exact set so accidental edits surface in CI rather
// than silently changing item-preservation behavior.
let expected = [
- "error", "exception", "failed", "failure", "critical", "fatal", "crash", "panic",
- "abort", "timeout", "denied", "rejected",
+ "error",
+ "exception",
+ "failed",
+ "failure",
+ "critical",
+ "fatal",
+ "crash",
+ "panic",
+ "abort",
+ "timeout",
+ "denied",
+ "rejected",
];
let actual: std::collections::BTreeSet<&str> = ERROR_KEYWORDS.iter().copied().collect();
let expected: std::collections::BTreeSet<&str> = expected.iter().copied().collect();
diff --git a/crates/headroom-core/src/transforms/smart_crusher/field_detect.rs b/crates/headroom-core/src/transforms/smart_crusher/field_detect.rs
index 34d91b023..83bb8f73c 100644
--- a/crates/headroom-core/src/transforms/smart_crusher/field_detect.rs
+++ b/crates/headroom-core/src/transforms/smart_crusher/field_detect.rs
@@ -44,11 +44,7 @@ pub fn detect_id_field_statistically(stats: &FieldStats, values: &[Value]) -> (b
// First 20 string-typed values for sampling. Python: `values[:20]`
// then filters by `isinstance(v, str)` — order-preserving slice
// before filter, so we mirror that.
- let sample_values: Vec<&str> = values
- .iter()
- .take(20)
- .filter_map(|v| v.as_str())
- .collect();
+ let sample_values: Vec<&str> = values.iter().take(20).filter_map(|v| v.as_str()).collect();
if !sample_values.is_empty() {
let uuid_count = sample_values.iter().filter(|s| is_uuid_format(s)).count();
@@ -180,10 +176,7 @@ pub fn detect_score_field_statistically(stats: &FieldStats, items: &[Value]) ->
if values_in_order.len() >= 5 {
let num_pairs = values_in_order.len() - 1;
- let descending_count = values_in_order
- .windows(2)
- .filter(|w| w[0] >= w[1])
- .count();
+ let descending_count = values_in_order.windows(2).filter(|w| w[0] >= w[1]).count();
if num_pairs > 0 && (descending_count as f64 / num_pairs as f64) > 0.7 {
confidence += 0.3;
}
diff --git a/crates/headroom-core/src/transforms/smart_crusher/mod.rs b/crates/headroom-core/src/transforms/smart_crusher/mod.rs
index 3accbe556..760d850b9 100644
--- a/crates/headroom-core/src/transforms/smart_crusher/mod.rs
+++ b/crates/headroom-core/src/transforms/smart_crusher/mod.rs
@@ -57,15 +57,11 @@ pub use crushers::{compute_k_split, crush_number_array, crush_object, crush_stri
pub use error_keywords::ERROR_KEYWORDS;
pub use field_detect::{detect_id_field_statistically, detect_score_field_statistically};
pub use hashing::hash_field_name;
-pub use orchestration::{
- deduplicate_indices_by_content, fill_remaining_slots, prioritize_indices,
-};
-pub use planning::{
- item_has_preserve_field_match, map_to_anchor_pattern, SmartCrusherPlanner,
-};
+pub use orchestration::{deduplicate_indices_by_content, fill_remaining_slots, prioritize_indices};
pub use outliers::{
detect_error_items_for_preservation, detect_rare_status_values, detect_structural_outliers,
};
+pub use planning::{item_has_preserve_field_match, map_to_anchor_pattern, SmartCrusherPlanner};
pub use statistics::{calculate_string_entropy, detect_sequential_pattern, is_uuid_format};
pub use stats_math::{format_g, mean, median, sample_stdev, sample_variance};
pub use types::{
diff --git a/crates/headroom-core/src/transforms/smart_crusher/orchestration.rs b/crates/headroom-core/src/transforms/smart_crusher/orchestration.rs
index e2b8679a7..cb9c3ec3b 100644
--- a/crates/headroom-core/src/transforms/smart_crusher/orchestration.rs
+++ b/crates/headroom-core/src/transforms/smart_crusher/orchestration.rs
@@ -176,12 +176,12 @@ pub fn prioritize_indices(
// Over budget — apply critical-items-first prioritization.
// Errors (keyword-detected — preservation guarantee).
- let error_indices: BTreeSet<usize> =
- detect_error_items_for_preservation(items, None).into_iter().collect();
+ let error_indices: BTreeSet<usize> = detect_error_items_for_preservation(items, None)
+ .into_iter()
+ .collect();
// Structural outliers (statistical — rare fields, rare statuses).
- let outlier_indices: BTreeSet<usize> =
- detect_structural_outliers(items).into_iter().collect();
+ let outlier_indices: BTreeSet<usize> = detect_structural_outliers(items).into_iter().collect();
// Numeric anomalies (>variance_threshold σ from per-field mean).
let anomaly_indices = numeric_anomaly_indices(config, items, analysis);
@@ -260,7 +260,9 @@ fn numeric_anomaly_indices(
}
let threshold = config.variance_threshold * std;
for (i, item) in items.iter().enumerate() {
- let Some(obj) = item.as_object() else { continue };
+ let Some(obj) = item.as_object() else {
+ continue;
+ };
let Some(v) = obj.get(field_name) else {
continue;
};
@@ -276,9 +278,7 @@ fn numeric_anomaly_indices(
}
fn is_numeric_field_with_variance(stats: &FieldStats) -> bool {
- stats.field_type == "numeric"
- && stats.mean_val.is_some()
- && stats.variance.unwrap_or(0.0) > 0.0
+ stats.field_type == "numeric" && stats.mean_val.is_some() && stats.variance.unwrap_or(0.0) > 0.0
}
/// Hash function used by all three orchestration helpers.
@@ -345,11 +345,7 @@ mod tests {
#[test]
fn dedup_all_distinct_unchanged() {
- let items = vec![
- json!({"id": 1}),
- json!({"id": 2}),
- json!({"id": 3}),
- ];
+ let items = vec![json!({"id": 1}), json!({"id": 2}), json!({"id": 3})];
let kept = idx_set(&[0, 1, 2]);
let result = deduplicate_indices_by_content(&kept, &items);
assert_eq!(result, idx_set(&[0, 1, 2]));
@@ -367,10 +363,7 @@ mod tests {
fn dedup_key_order_independent() {
// {"b":2, "a":1} and {"a":1, "b":2} must hash to the same value
// because we serialize with sort_keys=True.
- let items = vec![
- json!({"b": 2, "a": 1}),
- json!({"a": 1, "b": 2}),
- ];
+ let items = vec![json!({"b": 2, "a": 1}), json!({"a": 1, "b": 2})];
let kept = idx_set(&[0, 1]);
let result = deduplicate_indices_by_content(&kept, &items);
assert_eq!(result.len(), 1);
diff --git a/crates/headroom-core/src/transforms/smart_crusher/outliers.rs b/crates/headroom-core/src/transforms/smart_crusher/outliers.rs
index d09150c0e..00fa0fd31 100644
--- a/crates/headroom-core/src/transforms/smart_crusher/outliers.rs
+++ b/crates/headroom-core/src/transforms/smart_crusher/outliers.rs
@@ -91,7 +91,9 @@ pub fn detect_structural_outliers(items: &[Value]) -> Vec<usize> {
// 1. Rare-field outliers.
for (i, item) in items.iter().enumerate() {
- let Some(obj) = item.as_object() else { continue };
+ let Some(obj) = item.as_object() else {
+ continue;
+ };
let has_rare = obj.keys().any(|k| rare_fields.contains(k.as_str()));
if has_rare {
outlier_set.insert(i);
@@ -212,8 +214,12 @@ pub fn detect_rare_status_values(items: &[Value], common_fields: &HashSet<String
// Items with values NOT in top_k_values are outliers.
for (i, item) in items.iter().enumerate() {
- let Some(obj) = item.as_object() else { continue };
- let Some(field_value) = obj.get(field_name) else { continue };
+ let Some(obj) = item.as_object() else {
+ continue;
+ };
+ let Some(field_value) = obj.get(field_name) else {
+ continue;
+ };
let item_value = if matches!(field_value, Value::Null) {
"__none__".to_string()
} else {
@@ -362,8 +368,10 @@ mod tests {
.collect();
let common: HashSet<String> = ["code".to_string()].into_iter().collect();
let outliers = detect_rare_status_values(&items, &common);
- assert!(outliers.is_empty(),
- "uniform distribution must not produce rare-status outliers");
+ assert!(
+ outliers.is_empty(),
+ "uniform distribution must not produce rare-status outliers"
+ );
}
#[test]
@@ -461,10 +469,7 @@ mod tests {
#[test]
fn error_keywords_no_match() {
- let items: Vec<Value> = vec![
- json!({"name": "alice"}),
- json!({"count": 5}),
- ];
+ let items: Vec<Value> = vec![json!({"name": "alice"}), json!({"count": 5})];
let errs = detect_error_items_for_preservation(&items, None);
assert!(errs.is_empty());
}
diff --git a/crates/headroom-core/src/transforms/smart_crusher/planning.rs b/crates/headroom-core/src/transforms/smart_crusher/planning.rs
index 2dfd4137e..7816d6e21 100644
--- a/crates/headroom-core/src/transforms/smart_crusher/planning.rs
+++ b/crates/headroom-core/src/transforms/smart_crusher/planning.rs
@@ -165,7 +165,13 @@ impl<'a> SmartCrusherPlanner<'a> {
// 3. Numeric anomalies (>variance_threshold σ from per-field mean).
for (name, stats) in &analysis.field_stats {
- for_each_anomaly(name, stats, items, self.config.variance_threshold, &mut keep);
+ for_each_anomaly(
+ name,
+ stats,
+ items,
+ self.config.variance_threshold,
+ &mut keep,
+ );
}
// 4. Items around change points (window of ±1).
@@ -247,9 +253,7 @@ impl<'a> SmartCrusherPlanner<'a> {
(i, score)
})
.collect();
- scored.sort_by(|a, b| {
- b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal)
- });
+ scored.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
let top_count = max_items.saturating_sub(3);
for (idx, _) in scored.iter().take(top_count) {
@@ -678,11 +682,7 @@ mod tests {
let item = json!({"customer_id": "user-12345-alice"});
let h = hash_field_name("customer_id");
let fields = vec![h];
- assert!(item_has_preserve_field_match(
- &item,
- &fields,
- "alice"
- ));
+ assert!(item_has_preserve_field_match(&item, &fields, "alice"));
}
#[test]
diff --git a/crates/headroom-core/src/transforms/smart_crusher/statistics.rs b/crates/headroom-core/src/transforms/smart_crusher/statistics.rs
index b8f63fd4e..214e319f1 100644
--- a/crates/headroom-core/src/transforms/smart_crusher/statistics.rs
+++ b/crates/headroom-core/src/transforms/smart_crusher/statistics.rs
@@ -116,9 +116,8 @@ fn python_int_parse(s: &str) -> Option<i64> {
// Reject patterns Python rejects: leading/trailing underscore,
// double underscores. Otherwise strip them out.
let bytes = trimmed.as_bytes();
- let starts_or_ends = bytes[0] == b'_'
- || *bytes.last().unwrap() == b'_'
- || trimmed.contains("__");
+ let starts_or_ends =
+ bytes[0] == b'_' || *bytes.last().unwrap() == b'_' || trimmed.contains("__");
if starts_or_ends {
return None;
}
@@ -216,10 +215,7 @@ pub fn detect_sequential_pattern(values: &[Value], check_order: bool) -> bool {
// Sort and compute pairwise diffs.
let mut sorted_nums = nums.clone();
sorted_nums.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
- let diffs: Vec<f64> = sorted_nums
- .windows(2)
- .map(|w| w[1] - w[0])
- .collect();
+ let diffs: Vec<f64> = sorted_nums.windows(2).map(|w| w[1] - w[0]).collect();
if diffs.is_empty() {
return false;
}
@@ -239,10 +235,7 @@ pub fn detect_sequential_pattern(values: &[Value], check_order: bool) -> bool {
if check_order {
// Python: ascending count over original (not-sorted) sequence.
// IDs ascend in array order; scores typically descend.
- let ascending_count = nums
- .windows(2)
- .filter(|w| w[0] <= w[1])
- .count();
+ let ascending_count = nums.windows(2).filter(|w| w[0] <= w[1]).count();
let n_pairs = nums.len() - 1;
let is_ascending = ascending_count as f64 / n_pairs as f64 > 0.7;
return is_ascending;
@@ -389,14 +382,7 @@ mod tests {
// field that has BOTH genuine ints AND string-encoded ints
// should still be detected (the unambiguous ints dominate the
// signal).
- let v = vec![
- json!(1),
- json!(2),
- json!("3"),
- json!(4),
- json!(5),
- json!(6),
- ];
+ let v = vec![json!(1), json!(2), json!("3"), json!(4), json!(5), json!(6)];
assert!(detect_sequential_pattern(&v, true));
}
@@ -426,13 +412,7 @@ mod tests {
// Floats with non-integer values but constant unit step. avg_diff
// = 1.0, all diffs in [0.5, 2.0], should be sequential. (Suggestion
// S6 in code review — pins float arithmetic doesn't drift.)
- let v: Vec<Value> = vec![
- json!(1.5),
- json!(2.5),
- json!(3.5),
- json!(4.5),
- json!(5.5),
- ];
+ let v: Vec<Value> = vec![json!(1.5), json!(2.5), json!(3.5), json!(4.5), json!(5.5)];
assert!(detect_sequential_pattern(&v, true));
}
diff --git a/crates/headroom-core/src/transforms/smart_crusher/stats_math.rs b/crates/headroom-core/src/transforms/smart_crusher/stats_math.rs
index 1e8ff1d9e..e3e5de4f2 100644
--- a/crates/headroom-core/src/transforms/smart_crusher/stats_math.rs
+++ b/crates/headroom-core/src/transforms/smart_crusher/stats_math.rs
@@ -100,7 +100,11 @@ pub fn format_g(x: f64) -> String {
return "nan".to_string();
}
if x.is_infinite() {
- return if x > 0.0 { "inf".to_string() } else { "-inf".to_string() };
+ return if x > 0.0 {
+ "inf".to_string()
+ } else {
+ "-inf".to_string()
+ };
}
if x == 0.0 {
return "0".to_string();
@@ -133,7 +137,10 @@ fn normalize_scientific_exp(s: &str) -> String {
let exp_part = &rest[1..];
let exp_num: i32 = exp_part.parse().unwrap_or(0);
let mantissa_clean = if mantissa.contains('.') {
- mantissa.trim_end_matches('0').trim_end_matches('.').to_string()
+ mantissa
+ .trim_end_matches('0')
+ .trim_end_matches('.')
+ .to_string()
} else {
mantissa.to_string()
};
diff --git a/crates/headroom-parity/examples/diff_fixture.rs b/crates/headroom-parity/examples/diff_fixture.rs
index 18a80906c..c0e82752b 100644
--- a/crates/headroom-parity/examples/diff_fixture.rs
+++ b/crates/headroom-parity/examples/diff_fixture.rs
@@ -37,21 +37,25 @@ fn main() -> Result<()> {
} else {
println!("\n=== DIFFER ===");
// Field-by-field for objects
- if let (Some(exp_obj), Some(act_obj)) =
- (fixture.output.as_object(), actual.as_object())
- {
- for key in exp_obj.keys().chain(act_obj.keys()).collect::<std::collections::BTreeSet<_>>() {
+ if let (Some(exp_obj), Some(act_obj)) = (fixture.output.as_object(), actual.as_object()) {
+ for key in exp_obj
+ .keys()
+ .chain(act_obj.keys())
+ .collect::<std::collections::BTreeSet<_>>()
+ {
let e = exp_obj.get(key);
let a = act_obj.get(key);
if e != a {
println!(" field {key}:");
println!(
" expected: {}",
- e.map(|v| serde_json::to_string(v).unwrap()).unwrap_or_default()
+ e.map(|v| serde_json::to_string(v).unwrap())
+ .unwrap_or_default()
);
println!(
" actual : {}",
- a.map(|v| serde_json::to_string(v).unwrap()).unwrap_or_default()
+ a.map(|v| serde_json::to_string(v).unwrap())
+ .unwrap_or_default()
);
}
}
diff --git a/crates/headroom-parity/src/lib.rs b/crates/headroom-parity/src/lib.rs
index 8a19a9230..bcc4906ad 100644
--- a/crates/headroom-parity/src/lib.rs
+++ b/crates/headroom-parity/src/lib.rs
@@ -302,14 +302,8 @@ impl TransformComparator for SmartCrusherComparator {
.get("content")
.and_then(|v| v.as_str())
.context("smart_crusher fixture input.content must be a JSON string")?;
- let query = input
- .get("query")
- .and_then(|v| v.as_str())
- .unwrap_or("");
- let bias = input
- .get("bias")
- .and_then(|v| v.as_f64())
- .unwrap_or(1.0);
+ let query = input.get("query").and_then(|v| v.as_str()).unwrap_or("");
+ let bias = input.get("bias").and_then(|v| v.as_f64()).unwrap_or(1.0);
let defaults = SmartCrusherConfig::default();
let cfg = SmartCrusherConfig {
diff --git a/crates/headroom-py/src/lib.rs b/crates/headroom-py/src/lib.rs
index 93d43983d..e0ce42f4b 100644
--- a/crates/headroom-py/src/lib.rs
+++ b/crates/headroom-py/src/lib.rs
@@ -581,9 +581,7 @@ impl PySmartCrusher {
#[new]
#[pyo3(signature = (config = None))]
fn new(config: Option<&PySmartCrusherConfig>) -> Self {
- let cfg = config
- .map(|c| c.inner.clone())
- .unwrap_or_default();
+ let cfg = config.map(|c| c.inner.clone()).unwrap_or_default();
Self {
inner: RustSmartCrusher::new(cfg),
}
@@ -603,12 +601,7 @@ impl PySmartCrusher {
/// `smart_crush_tool_output` convenience function and direct
/// callers that want the tuple form.
#[pyo3(signature = (content, query = "", bias = 1.0))]
- fn smart_crush_content(
- &self,
- content: &str,
- query: &str,
- bias: f64,
- ) -> (String, bool, String) {
+ fn smart_crush_content(&self, content: &str, query: &str, bias: f64) -> (String, bool, String) {
self.inner.smart_crush_content(content, query, bias)
}
}
diff --git a/scripts/install-git-hooks.sh b/scripts/install-git-hooks.sh
new file mode 100755
index 000000000..699fed1cf
--- /dev/null
+++ b/scripts/install-git-hooks.sh
@@ -0,0 +1,72 @@
+#!/usr/bin/env bash
+# Install a pre-push git hook that runs `make ci-precheck` before every push.
+#
+# Why: the 2026-04-27 push hit five CI failures that could all have been
+# caught locally — cargo fmt drift, an x86_64-apple-darwin wheel that the
+# project doesn't actually need, missing Rust extension in two CI lanes,
+# and a commitlint warning treated as an error. The fixes are committed;
+# this hook ensures we don't repeat the same dance.
+#
+# Idempotent. Re-running is safe — it overwrites the hook file with the
+# current desired contents. Skips installation if `.git/hooks/` is missing
+# (e.g. running outside a git checkout).
+
+set -euo pipefail
+
+cd "$(dirname "$0")/.."
+
+if [[ ! -d .git/hooks ]]; then
+ echo "error: .git/hooks/ not found — run from a git checkout root" >&2
+ exit 1
+fi
+
+HOOK_PATH=".git/hooks/pre-push"
+
+cat > "$HOOK_PATH" <<'HOOK_EOF'
+#!/usr/bin/env bash
+# Headroom pre-push hook — runs `make ci-precheck` so CI never finds a
+# bug a local check could have caught.
+#
+# Skip with: `git push --no-verify`. Use sparingly — every skip is a roll
+# of the dice on a CI break.
+
+set -euo pipefail
+
+# Skip the hook entirely when push goes to a ref that is not on the main
+# tracking branches we gate. Adjust the pattern below if more branches
+# need gating.
+remote="$1"
+url="$2"
+
+while IFS=' ' read -r local_ref local_sha remote_ref remote_sha; do
+ # Empty local_sha means a delete; nothing to verify.
+ if [[ "$local_sha" == "0000000000000000000000000000000000000000" ]]; then
+ continue
+ fi
+ echo "── pre-push: running 'make ci-precheck' before pushing $local_ref → $remote_ref"
+done
+
+if [[ -z "${VIRTUAL_ENV:-}" ]]; then
+ if [[ -f .venv/bin/activate ]]; then
+ # shellcheck disable=SC1091
+ source .venv/bin/activate
+ else
+ echo "warn: no VIRTUAL_ENV set and no .venv/ found — python checks may use the wrong interpreter" >&2
+ fi
+fi
+
+if make ci-precheck; then
+ exit 0
+else
+ echo ""
+ echo "❌ pre-push: 'make ci-precheck' failed. Fix the issues above before pushing."
+ echo " To bypass (NOT recommended): git push --no-verify"
+ exit 1
+fi
+HOOK_EOF
+
+chmod +x "$HOOK_PATH"
+
+echo "✅ installed: $HOOK_PATH"
+echo " Runs 'make ci-precheck' before every git push."
+echo " Bypass (use sparingly): git push --no-verify"