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.

test(onnx): fix magika idle-thread test on macOS with ORT 1.31 (#4194)

Romin Parekh0a9aa66bff9c
diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml
index bd8c598c6..7e17b47be 100644
--- a/.github/workflows/rust.yml
+++ b/.github/workflows/rust.yml
@@ -65,11 +65,23 @@ jobs:
esac
test:
- name: test (ubuntu)
+ # `test (ubuntu)` keeps its exact check name, which branch protection may
+ # require. macOS runs only `cargo test`: the magika/ORT tests there take a
+ # different path (Core ML EP, `ps -M` thread counts, #4191) that Linux
+ # never exercises; fmt/clippy stay on Ubuntu.
+ name: test (${{ matrix.name }})
needs: rust-changes
if: needs.rust-changes.outputs.rust == 'true'
- runs-on: ubuntu-latest
+ runs-on: ${{ matrix.os }}
timeout-minutes: 30
+ strategy:
+ fail-fast: false
+ matrix:
+ include:
+ - name: ubuntu
+ os: ubuntu-latest
+ - name: macos
+ os: macos-26
steps:
- uses: actions/checkout@v7
- name: Install stable toolchain
@@ -139,11 +151,19 @@ jobs:
print(f"onnxruntime {version} -> {libs[0]}", file=sys.stderr)
PY
- name: cargo fmt --check
+ if: matrix.os == 'ubuntu-latest'
run: cargo fmt --all -- --check
- name: cargo clippy
+ if: matrix.os == 'ubuntu-latest'
run: cargo clippy --workspace -- -D warnings
- name: cargo test
run: cargo test --workspace
+ - name: cargo test (magika idle threads)
+ # Cargo stops at the first failing test binary, so a failure elsewhere
+ # in the suite would skip this macOS-specific check (#4191). Run it on
+ # its own even then; --nocapture keeps its thread counts in the log.
+ if: ${{ !cancelled() && matrix.os == 'macos-26' }}
+ run: cargo test -p headroom-core --test magika_idle_threads -- --nocapture
simulator-e2e:
name: simulator e2e (${{ matrix.os }})
diff --git a/crates/headroom-core/tests/ccr_backends.rs b/crates/headroom-core/tests/ccr_backends.rs
index 62e7f6ecf..9a3ecb732 100644
--- a/crates/headroom-core/tests/ccr_backends.rs
+++ b/crates/headroom-core/tests/ccr_backends.rs
@@ -162,14 +162,15 @@ fn backend_swap_byte_equal_keys() {
#[test]
fn in_memory_get_refreshes_idle_ttl() {
- let store = InMemoryCcrStore::with_capacity_and_ttl(10, Duration::from_millis(120));
+ // Wide margins: `sleep` overshoots by tens of ms on loaded macOS runners.
+ let store = InMemoryCcrStore::with_capacity_and_ttl(10, Duration::from_millis(1_000));
let hash = compute_key(b"hot entry");
store.put(&hash, "hot entry");
- // Touch the entry every 60ms for ~4 idle windows' worth of wall
- // clock. Wall-clock expiry would kill it at 120ms; a sliding idle
- // window keeps it alive because every hit restarts the clock.
- for _ in 0..8 {
- std::thread::sleep(Duration::from_millis(60));
+ // Touch the entry every 250ms for 1.5s. Wall-clock expiry would kill
+ // it at 1s; a sliding idle window keeps it alive because every hit
+ // restarts the clock.
+ for _ in 0..6 {
+ std::thread::sleep(Duration::from_millis(250));
assert_eq!(
store.get(&hash).as_deref(),
Some("hot entry"),
@@ -177,7 +178,7 @@ fn in_memory_get_refreshes_idle_ttl() {
);
}
// Now go idle past the window: the entry must expire.
- std::thread::sleep(Duration::from_millis(200));
+ std::thread::sleep(Duration::from_millis(1_500));
assert_eq!(
store.get(&hash),
None,
diff --git a/crates/headroom-core/tests/magika_idle_threads.rs b/crates/headroom-core/tests/magika_idle_threads.rs
index 8c6e61a44..9f38e2a7f 100644
--- a/crates/headroom-core/tests/magika_idle_threads.rs
+++ b/crates/headroom-core/tests/magika_idle_threads.rs
@@ -8,7 +8,7 @@
use std::time::Duration;
-use headroom_core::transforms::magika_detector::{magika_detect, MagikaDetectorError};
+use headroom_core::transforms::magika_detector::magika_detect;
#[cfg(target_os = "linux")]
fn os_thread_count() -> usize {
@@ -27,31 +27,54 @@ fn os_thread_count() -> usize {
String::from_utf8_lossy(&out.stdout).lines().count() - 1
}
+/// Thread count once short-lived threads (e.g. `magika-init`) have exited.
+fn settled_thread_count() -> usize {
+ std::thread::sleep(Duration::from_millis(200));
+ os_thread_count()
+}
+
#[test]
fn magika_session_does_not_spawn_intra_op_pool() {
- let before = os_thread_count();
- match magika_detect("fn main() { println!(\"hi\"); }") {
- Ok(_) => {}
- // With an explicit ORT_DYLIB_PATH (as in CI) the runtime is present,
- // so an init failure is a real session-build regression, not a skip.
- Err(MagikaDetectorError::Init(reason))
- if std::env::var("ORT_DYLIB_PATH").map_or(true, |v| v.trim().is_empty()) =>
- {
- eprintln!("skipping: magika runtime unavailable on this host: {reason}");
- return;
- }
- Err(e) => panic!("magika detection failed: {e}"),
- }
- // Let the short-lived `magika-init` thread finish exiting.
- std::thread::sleep(Duration::from_millis(200));
- let after = os_thread_count();
- // ORT and macOS may leave up to 2 idle background threads (ORT telemetry,
- // libdispatch); neither spins. A pool adds its workers on top (7 on a
- // 16-core Mac), but on very small hosts a 1-2 worker pool can hide inside
- // this allowance, so a pass there is not proof of no spinning.
+ let Some(ort_dylib) = std::env::var("ORT_DYLIB_PATH")
+ .ok()
+ .filter(|v| !v.trim().is_empty())
+ else {
+ eprintln!("skipping: set ORT_DYLIB_PATH to an onnxruntime library to run this test");
+ return;
+ };
+ ort::init_from(&ort_dylib)
+ .expect("load ONNX Runtime from ORT_DYLIB_PATH")
+ .commit();
+
+ // Sessions also start whatever their execution provider needs. ort's
+ // default policy (MaxEfficiency, i.e. prefer NPU) picks Core ML on the
+ // Apple Neural Engine once ORT 1.31 registers Core ML as a built-in EP,
+ // adding 5-7 idle ANE threads (#4191). The control goes through the same
+ // EP selection, so measure the allowance there instead of guessing it.
+ let start = settled_thread_count();
+ let control = magika::Session::builder()
+ .with_intra_threads(1)
+ .build()
+ .expect("build pool-free control session");
+ let control_alive = settled_thread_count();
+ let allowance = control_alive.saturating_sub(start);
+ drop(control);
+
+ let before = settled_thread_count();
+ magika_detect("fn main() { println!(\"hi\"); }").expect("magika detection failed");
+ let after = settled_thread_count();
+ // The allowance also covers threads the control leaves running for good
+ // (2-4 on macOS), so on a low-core host a pool that small can still hide
+ // inside it; a pass there is not proof of no spinning.
let extra = after.saturating_sub(before);
+ eprintln!(
+ "OS threads: start={start}, control alive={control_alive}, control dropped={before} \
+ ({} kept); magika session added {extra} (allowance {allowance})",
+ before.saturating_sub(start)
+ );
assert!(
- extra <= 2,
- "magika session left {extra} extra OS thread(s) running (before={before}, after={after})"
+ extra <= allowance,
+ "magika session left {extra} extra OS thread(s) running, a pool-free session \
+ adds {allowance} (before={before}, after={after})"
);
}