headroom/tests/test_transforms/test_ort_dylib.py
Rob Francis 32ce99e4b4
fix(build): enable Intel macOS pip installs via ort-load-dynamic (#1538)
## Description

Fix `pip install headroom-ai` on Intel Mac (`x86_64-apple-darwin`).
Source installs failed because `ort-sys 2.0.0-rc.12` (transitive via
`fastembed`) does not ship prebuilt ONNX Runtime binaries for that
target, causing maturin/cargo to exit during the wheel build.

This PR mirrors the existing Windows fix: build the Rust core with
`ort-load-dynamic`, pin `ORT_DYLIB_PATH` to the pip `onnxruntime` native
library at import time, and publish Intel macOS wheels from CI.

Closes #

## Type of Change

- [x] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [x] Documentation update
- [ ] Performance improvement
- [ ] Code refactoring (no functional changes)

## Changes Made

- `crates/headroom-core/Cargo.toml`: use `ort-load-dynamic` for
`x86_64-apple-darwin` instead of `ort-download-binaries-rustls-tls`.
- `headroom/_ort.py`: extend the ORT dylib pin hook to Intel macOS
(`darwin` + `x86_64`), resolving `libonnxruntime*.dylib` from the pip
`onnxruntime` package.
- `.github/workflows/release.yml` and `.github/workflows/rust.yml`: add
`macos-15-intel` / `x86_64-apple-darwin` wheel matrix entries.
- `tests/test_release_workflows.py` and
`tests/test_transforms/test_ort_dylib.py`: update/add coverage for the
new target and dylib pin behavior.
- `README.md`: note that prebuilt wheels are published for Intel macOS.

## Testing

- [x] Unit tests pass (`pytest`)
- [ ] Linting passes (`ruff check .`)
- [ ] Type checking passes (`mypy headroom`)
- [x] New tests added for new functionality
- [x] Manual testing performed

### Test Output

```text
$ pytest tests/test_transforms/test_ort_dylib.py \
    tests/test_release_workflows.py::test_fastembed_uses_dynamic_ort_on_windows \
    tests/test_release_workflows.py::test_build_wheels_matrix_includes_intel_macos_with_dynamic_ort -q

..........................                                           [100%]
10 passed in 0.18s

$ maturin build --release -o /tmp/headroom-dist
📦 Built wheel for abi3 Python ≥ 3.10 to /tmp/headroom-dist/headroom_ai-0.27.0-cp310-abi3-macosx_10_12_x86_64.whl

$ python3.11 -m venv /tmp/hr-venv && /tmp/hr-venv/bin/pip install .
Successfully built headroom-ai
Successfully installed headroom-ai-0.27.0

$ cd /tmp && /tmp/hr-venv/bin/python -c "import headroom; import headroom._core; print('ok')"
version 0.27.0
_core ok
```

## Real Behavior Proof

- Environment: macOS `x86_64-apple-darwin`, Python 3.11.5, Rust 1.95.0
- Exact command / steps: Reproduced the reported failure with `pip
install headroom-ai` (sdist build dies in `ort-sys` for
`x86_64-apple-darwin`); after this patch ran `maturin build --release`,
then `pip install .` in a clean venv, then `python -c "import
headroom._core"`.
- Observed result: Before fix, cargo/maturin exit 101 on missing ORT
prebuilts; after fix, wheel build succeeds and `headroom._core` imports
cleanly (`version 0.27.0`, `_core ok`).
- Not tested: `macos-15-intel` GitHub Actions wheel matrix row (will be
validated by CI after merge).

## Review Readiness

- [x] I have performed a self-review
- [x] This PR is ready for human review

## Checklist

- [x] My code follows the project's style guidelines
- [x] I have performed a self-review of my code
- [x] I have commented my code, particularly in hard-to-understand areas
- [x] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [ ] I have updated the CHANGELOG.md if applicable

## Additional Notes

- ML features (magika detection, fastembed embeddings) still require
`onnxruntime` at runtime on Intel Mac. Users should install
`headroom-ai[proxy]` or `pip install onnxruntime`; `_ort.py` auto-pins
`ORT_DYLIB_PATH` when that package is present.
- Apple Silicon (`aarch64-apple-darwin`) behavior is unchanged: it
continues to bundle ORT via `ort-download-binaries-rustls-tls`.
- Lint/mypy not re-run locally in this pass; targeted pytest +
maturin/pip install proof covers the changed surface.

---------

Co-authored-by: Bor <you@example.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-06 18:33:34 -05:00

130 lines
4.3 KiB
Python

"""Tests for headroom._ort — the ORT_DYLIB_PATH auto-pin.
The resolver guards the Rust core on platforms that use `ort-load-dynamic`
(Windows and Intel macOS). On Windows it avoids the System32 onnxruntime.dll
deadlock (Win11 24H2+, see headroom/_ort.py). Platform gates are
monkeypatched so the full logic runs on any CI OS.
"""
from __future__ import annotations
import sys
from types import SimpleNamespace
import pytest
import headroom._ort as _ort
@pytest.fixture(autouse=True)
def _fresh_resolver(monkeypatch):
"""Reset the module-level cache and scrub the env before every test."""
monkeypatch.setattr(_ort, "_pinned", _ort._UNSET)
monkeypatch.delenv("ORT_DYLIB_PATH", raising=False)
def _force_windows(monkeypatch):
monkeypatch.setattr(sys, "platform", "win32")
def _force_intel_macos(monkeypatch):
monkeypatch.setattr(sys, "platform", "darwin")
monkeypatch.setattr(_ort.platform, "machine", lambda: "x86_64")
def _fake_spec_for(monkeypatch, package_dir):
"""Make find_spec('onnxruntime') resolve to a fake package directory."""
spec = SimpleNamespace(origin=str(package_dir / "__init__.py"))
monkeypatch.setattr(
_ort.importlib.util,
"find_spec",
lambda name: spec if name == "onnxruntime" else None,
)
def test_noop_on_non_dynamic_platforms(monkeypatch):
monkeypatch.setattr(sys, "platform", "linux")
assert _ort.ensure_ort_dylib_pinned() is None
assert "ORT_DYLIB_PATH" not in _ort.os.environ
monkeypatch.setattr(sys, "platform", "darwin")
monkeypatch.setattr(_ort.platform, "machine", lambda: "arm64")
assert _ort.ensure_ort_dylib_pinned() is None
assert "ORT_DYLIB_PATH" not in _ort.os.environ
def test_respects_existing_env(monkeypatch):
_force_windows(monkeypatch)
monkeypatch.setenv("ORT_DYLIB_PATH", r"C:\custom\onnxruntime.dll")
assert _ort.ensure_ort_dylib_pinned() == r"C:\custom\onnxruntime.dll"
assert _ort.os.environ["ORT_DYLIB_PATH"] == r"C:\custom\onnxruntime.dll"
def test_pins_to_package_capi_dll(monkeypatch, tmp_path):
_force_windows(monkeypatch)
pkg = tmp_path / "onnxruntime"
capi = pkg / "capi"
capi.mkdir(parents=True)
dll = capi / "onnxruntime.dll"
dll.write_bytes(b"not really a dll")
_fake_spec_for(monkeypatch, pkg)
assert _ort.ensure_ort_dylib_pinned() == str(dll)
assert _ort.os.environ["ORT_DYLIB_PATH"] == str(dll)
def test_pins_to_package_capi_dylib_on_intel_macos(monkeypatch, tmp_path):
_force_intel_macos(monkeypatch)
pkg = tmp_path / "onnxruntime"
capi = pkg / "capi"
capi.mkdir(parents=True)
dylib = capi / "libonnxruntime.1.23.2.dylib"
dylib.write_bytes(b"not really a dylib")
_fake_spec_for(monkeypatch, pkg)
assert _ort.ensure_ort_dylib_pinned() == str(dylib)
assert _ort.os.environ["ORT_DYLIB_PATH"] == str(dylib)
def test_idempotent_after_first_resolution(monkeypatch, tmp_path):
_force_windows(monkeypatch)
pkg = tmp_path / "onnxruntime"
(pkg / "capi").mkdir(parents=True)
(pkg / "capi" / "onnxruntime.dll").write_bytes(b"x")
_fake_spec_for(monkeypatch, pkg)
first = _ort.ensure_ort_dylib_pinned()
# Resolution must not re-run: break find_spec and call again.
monkeypatch.setattr(
_ort.importlib.util,
"find_spec",
lambda name: pytest.fail("resolution ran twice"),
)
assert _ort.ensure_ort_dylib_pinned() == first
def test_no_pin_when_package_missing(monkeypatch):
_force_windows(monkeypatch)
monkeypatch.setattr(_ort.importlib.util, "find_spec", lambda name: None)
assert _ort.ensure_ort_dylib_pinned() is None
assert "ORT_DYLIB_PATH" not in _ort.os.environ
def test_no_pin_when_dll_file_absent(monkeypatch, tmp_path):
_force_windows(monkeypatch)
pkg = tmp_path / "onnxruntime"
pkg.mkdir() # package exists, but no capi/onnxruntime.dll inside
_fake_spec_for(monkeypatch, pkg)
assert _ort.ensure_ort_dylib_pinned() is None
assert "ORT_DYLIB_PATH" not in _ort.os.environ
def test_never_raises(monkeypatch):
_force_windows(monkeypatch)
def boom(name):
raise RuntimeError("synthetic find_spec failure")
monkeypatch.setattr(_ort.importlib.util, "find_spec", boom)
assert _ort.ensure_ort_dylib_pinned() is None
assert "ORT_DYLIB_PATH" not in _ort.os.environ