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.
Python agentaianthropicclaude-codecompressioncontext-engineeringcontext-windowcursorfastapilangchainllmmcpopenaiprompt-engineeringproxypythonragtoken-optimizationtokenstypescript
fix(relevance): gate ONNX embedding backend behind AVX2 to avoid SIGILL (#1723) (#1765)
Parideboy728b33088b25
diff --git a/README.md b/README.mdindex ce7b7655a..d1f67ed18 100644--- a/README.md+++ b/README.md@@ -340,6 +340,12 @@ pipx install --python python3.13 "headroom-ai[all]"→ [Installation guide](https://headroom-docs.vercel.app/docs/installation) — Docker tags, persistent service, PowerShell, devcontainers.+> **CPU requirement (x86/x86_64):** the ONNX-backed features — Magika content+> detection and embedding relevance — use a precompiled ONNX Runtime that needs+> **AVX2**. On x86 hosts without AVX2 (some Docker/QEMU setups and older cloud+> VMs) Headroom automatically falls back to its non-ONNX paths (BM25 relevance,+> heuristic detection) rather than crashing. `arm64`/Apple Silicon needs no AVX2.+### Updating```bashdiff --git a/crates/headroom-core/src/lib.rs b/crates/headroom-core/src/lib.rsindex 5b4634f5a..753fcdc76 100644--- a/crates/headroom-core/src/lib.rs+++ b/crates/headroom-core/src/lib.rs@@ -4,6 +4,7 @@ pub mod auth_mode;pub mod cache_control;pub mod ccr;pub mod compression_policy;+mod onnx_cpu;pub mod relevance;pub mod signals;pub mod tokenizer;diff --git a/crates/headroom-core/src/onnx_cpu.rs b/crates/headroom-core/src/onnx_cpu.rsnew file mode 100644index 000000000..16eb7ea59--- /dev/null+++ b/crates/headroom-core/src/onnx_cpu.rs@@ -0,0 +1,29 @@+//! Shared CPU-capability guard for the precompiled ONNX Runtime binary.+//!+//! Both ONNX entry points in this crate — Magika content detection+//! ([`crate::transforms::magika_detector`]) and the embedding relevance+//! scorer ([`crate::relevance::EmbeddingScorer`]) — link the same+//! precompiled ONNX Runtime shipped by `ort-sys` (pulled in via+//! fastembed's `ort-download-binaries*` feature).+//!+//! On x86/x86_64 that binary contains AVX2-family instructions. Executing+//! it on a CPU without AVX2 (common inside Docker / QEMU / older cloud VMs)+//! traps with `SIGILL` — a hardware fault that native code cannot turn into+//! a catchable exception, so the whole host process dies (issue #1723).+//!+//! Call this up front and skip the ONNX path when it returns `false`, so+//! callers fall back to non-ONNX behavior instead of crashing.++/// `true` if this CPU can run the precompiled ONNX Runtime binary.+///+/// On x86/x86_64 this requires AVX2. On non-x86 targets the AVX2 gate does+/// not apply and this always returns `true`.+#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]+pub(crate) fn onnx_runtime_supported_by_cpu() -> bool {+ std::is_x86_feature_detected!("avx2")+}++#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]+pub(crate) fn onnx_runtime_supported_by_cpu() -> bool {+ true+}diff --git a/crates/headroom-core/src/relevance/embedding.rs b/crates/headroom-core/src/relevance/embedding.rsindex aebdf3331..3c734ecb8 100644--- a/crates/headroom-core/src/relevance/embedding.rs+++ b/crates/headroom-core/src/relevance/embedding.rs@@ -93,6 +93,15 @@ impl EmbeddingScorer {/// quality/speed tradeoff for compression-relevance scoring on/// short snippets.pub fn try_new_with_model(model_kind: EmbeddingModel) -> Result<Self, String> {+ // fastembed links the precompiled ONNX Runtime binary, which contains+ // AVX2 instructions on x86. Loading/running it on a non-AVX2 CPU traps+ // with SIGILL (issue #1723) — an uncatchable native fault. Bail early so+ // callers fall back to the BM25/stub path instead of killing the process.+ if !crate::onnx_cpu::onnx_runtime_supported_by_cpu() {+ return Err("EmbeddingScorer: ONNX Runtime backend requires AVX2 on \+ this x86 CPU; embedding relevance disabled (falling back to BM25)"+ .to_string());+ }let name = format!("{:?}", model_kind);let model = TextEmbedding::try_new(InitOptions::new(model_kind)).map_err(|e| format!("EmbeddingScorer model load failed: {}", e))?;@@ -310,6 +319,31 @@ mod tests {assert!(r.is_empty());}+ // ---------- AVX2 CPU guard (issue #1723) ----------++ #[test]+ fn onnx_guard_matches_cpu_features() {+ let supported = crate::onnx_cpu::onnx_runtime_supported_by_cpu();+ #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]+ assert_eq!(supported, std::is_x86_feature_detected!("avx2"));+ #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]+ assert!(supported);+ }++ #[test]+ fn try_new_errors_on_unsupported_cpu_instead_of_sigill() {+ // On a no-AVX2 host the guard must turn the SIGILL into a plain Err+ // so callers fall back to BM25. On AVX2 CI runners the guard passes and+ // there is nothing to assert (loading the model would need network).+ if crate::onnx_cpu::onnx_runtime_supported_by_cpu() {+ return;+ }+ match EmbeddingScorer::try_new() {+ Err(err) => assert!(err.contains("AVX2"), "unexpected error: {err}"),+ Ok(_) => panic!("ONNX backend must not load on a no-AVX2 CPU"),+ }+ }+// ---------- model-backed tests (gated on RUN_FASTEMBED_TESTS) ----------#[test]diff --git a/crates/headroom-core/src/transforms/magika_detector.rs b/crates/headroom-core/src/transforms/magika_detector.rsindex 90c71d9c7..1fb1d3dbf 100644--- a/crates/headroom-core/src/transforms/magika_detector.rs+++ b/crates/headroom-core/src/transforms/magika_detector.rs@@ -54,14 +54,11 @@ use crate::transforms::content_detector::ContentType;/// instead of crashing.////// On non-x86 targets, this x86-specific AVX2 gate is not applied.-#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]-pub(crate) fn magika_onnx_runtime_supported_by_cpu() -> bool {- std::is_x86_feature_detected!("avx2")-}--#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]+///+/// Delegates to the shared [`crate::onnx_cpu`] guard so magika and the+/// embedding scorer agree on a single CPU-support source of truth.pub(crate) fn magika_onnx_runtime_supported_by_cpu() -> bool {- true+ crate::onnx_cpu::onnx_runtime_supported_by_cpu()}/// Errors from the magika detector. Wraps the underlying `magika::Error`