minutes icon indicating copy to clipboard operation
minutes copied to clipboard

Isolate the sherpa stack behind its own namespace so engine-sherpa and diarize can share a macOS binary

Open silverstein opened this issue 3 months ago • 2 comments

Successor to #683, which is being closed by the compile guard that refuses the broken combination. This issue tracks the real fix: making the combination work.

Why in-process linking cannot work today

sherpa-onnx's static macOS bundle and pyannote's dependency tree each vendor two of the same native libraries:

library sherpa's copy pyannote's copy
ONNX Runtime 1.17.1 (bundled in the static tarball) 1.22.0 (ort-sys 2.0.0-rc.10)
kaldi-native-fbank libkaldi-native-fbank-core.a from sherpa-onnx v1.12.9 same archive name, vendored by knf-rs-sys 0.3.2

A Mach-O image keeps one definition per symbol, so whichever copy wins the link serves both consumers. Measured on macOS arm64 (iMac, commit be2cad55), every possible resolution breaks one side:

ORT winner kaldi winner pyannote sherpa
1.17.1 sherpa's ✗ API version [22] is not available ✓
1.22.0 sherpa's ✗ SIGABRT entering feature extraction ✓
1.22.0 knf-rs's ✓ enrollment completes ✗ SIGABRT

Row 1 is the #683 report. Rows 2 and 3 come from substituting archives via SHERPA_LIB_PATH: ORT can be unified upward because its C API is backward compatible (sherpa transcription runs fine against 1.22.0), but kaldi-native-fbank has no ABI contract. Identical mangled names over divergent struct layouts are undefined behavior; the SIGABRTs we observed are the lucky failure mode.

Linux and Windows do not have this problem because sherpa links dynamically there and its shared library resolves against its own bundled dependencies, not the executable's.

Candidate designs

A. dlopen plugin (preferred). A new cdylib crate (minutes-sherpa-plugin) statically embeds the whole sherpa stack, including its own ORT and kaldi, and exports a minimal C API (create engine, transcribe samples, destroy). sherpa_engine.rs grows a libloading backend that dlopens it. macOS two-level namespaces bind the dylib's internals to its own copies, so the main binary keeps its pyannote stack untouched.

  • No IPC and no audio crossing a process boundary, so the secure-byte-transport requirement that parked the parakeet sidecar (docs/architecture/parakeet.md) does not apply.
  • The dylib is delivered by minutes setup --sherpa into ~/.minutes/lib/, alongside the ~670MB model download that step already performs. The main binary stays self-contained; sherpa remains opt-in.
  • Missing plugin degrades exactly like a missing model does today: transcription falls back to Whisper, never breaks a recording.

B. Worker process. Follow the apple_speech_worker bounded-worker pattern with sherpa in a separately linked child. Full memory isolation, but audio crosses a process boundary (transport and supervision machinery), and the worker binary must itself be built without pyannote, which means shipping a second binary. Strictly more moving parts than A for the same outcome.

What to undo when this lands

  • The compile guard in crates/core/src/lib.rs (#683)
  • The CI step asserting the guard fires (ci.yml, macOS test job)
  • engine-sherpa back into release-cli.yml (macOS CLI) and release-macos.yml (CLI + Tauri app)
  • README and docs/architecture/sherpa-engine.md build instructions

Context

Enabling sherpa in release builds was agreed after the WER benchmarking showed parakeet-tdt-0.6b-v3 quality; #683 (broken voice enrollment) was reported by traoredev from a source build days before the first release would have shipped the combination. v0.24.0 is unaffected.

silverstein avatar Aug 09 '26 18:08 silverstein

The dlopen design is validated on real hardware. I built the smallest thing that could disprove it before writing any of the real plumbing.

The spike. Two crates. A cdylib depending on sherpa-rs with static + download-binaries, so the whole sherpa stack including its own ONNX Runtime 1.17.1 is embedded in the dylib. A host binary depending on pyannote-rs and ort = "=2.0.0-rc.10", which is the ORT 1.22.0 that requests API 22 and is exactly what #683 breaks. The host initializes pyannote, dlopens the plugin, transcribes, then re-initializes pyannote to catch anything sherpa corrupted on the way through.

Result on macOS arm64 (silverbook, parakeet-tdt-0.6b-v3-int8 and the real demo wav):

=== which ORT versions are inside the plugin ===
1.17.1
=== which ORT versions are inside the host ===
1.22.0
HOST: initializing pyannote (ort 1.22) ...
HOST: pyannote embedding extractor OK
HOST: dlopening the sherpa plugin ...
PLUGIN: transcribed 170013 samples -> " Matt and Wesley are review the Minute's Apple speech benchm"
HOST: plugin returned 0
HOST: re-initializing pyannote after sherpa ...
HOST: pyannote still OK after sherpa
SPIKE RESULT: both stacks worked in one process

Two ONNX Runtimes, both doing real work, in one process. That is the combination #683 proved impossible when both are statically linked into the same image.

Why it holds, and the check that matters most:

=== does the plugin export ORT symbols that could interpose? ===
0

The cdylib exports no OrtGetApiBase. Rust cdylibs export only their own #[no_mangle] surface, so the embedded ORT stays internal, and macOS two-level namespaces bind each image's calls to its own copy. There is nothing for the loader to unify, which is the property flat-namespace platforms would not give us for free.

What this does not yet prove, and should be covered as the real implementation lands:

  • kaldi-native-fbank was exercised only through whatever the transducer path touches; the same isolation argument applies, but a diarization run and a sherpa run interleaved repeatedly would test it harder than one call each.
  • Only one dlopen/close cycle, and the plugin was never unloaded. Repeated load/unload, and concurrent use from multiple threads, are untested.
  • Memory footprint of two ORT copies resident at once is unmeasured.

Ordering from here: promote the spike into a real minutes-sherpa-plugin crate with a narrow C API, add a libloading backend to sherpa_engine.rs behind the existing engine-sherpa feature, deliver the dylib through minutes setup --sherpa next to the model it already downloads, then remove the compile guard and put engine-sherpa back into the macOS release builds. A missing plugin degrades exactly like a missing model does today: fall back to Whisper, never break a recording.

Unrelated but worth flagging since I hit it: the iMac is at 100% disk with about 1Gi free, which is why the spike ran on silverbook. I cleaned up my own artifacts there but left the 8G ~/Sites/minutes/target and the 1.3G sherpa cache alone.

silverstein avatar Aug 10 '26 22:08 silverstein

Implementation is up as #725. This comment records the packaging half, which I deliberately did not build, and why.

What #725 delivers. sherpa runs from crates/sherpa-plugin, a cdylib the core dlopens, so engine-sherpa coexists with diarize and vad-ort again. The #683 compile guard is gone, replaced by CI asserting the inverse. Verified on arm64 in one binary: enroll through ORT 1.22, transcribe through the plugin's ORT 1.17, then both together with transcribe --diarize.

What it does not deliver. Release builds still omit engine-sherpa, because shipping an engine that cannot load would be worse than not shipping it. A source build works today by copying the dylib beside the binary or setting MINUTES_SHERPA_PLUGIN.

Why packaging is its own piece of work, and needs Mat. The obvious plan (build the plugin in release-macos.yml, ship it as an asset, have setup --sherpa install it) runs straight into the signing pipeline:

  1. The dylib has to be signed with the Developer ID identity and notarized. An unsigned or ad-hoc-signed dylib loaded by a notarized, hardened-runtime app is refused by Gatekeeper. That means the plugin either joins the app's inside-out signing sequence, or is signed and notarized as a standalone artifact for the CLI. I cannot verify either without the maintainer's certificate; a CI-green run that skips notarization would prove nothing, which is the exact trap this codebase has been bitten by before.
  2. release-macos.yml is hash-gated as packaging authority by check_graph_worker_packaging.mjs, so any change there is a deliberate review, not a drive-by.
  3. There is a delivery-shape decision I should not make alone: bundle the plugin inside Minutes.app (simplest signing story, but every user carries ~22 MB whether or not they enable sherpa), or ship it as a separate download that setup --sherpa fetches next to the model it already pulls (keeps the default build small, but adds a second signed artifact and a checksum path).
  4. The CLI has the same shape as the Windows runtime DLLs solved in #657: the tarball needs the dylib beside the executable, or the binary alone silently lacks the engine.

My recommendation is the separate download: sherpa is opt-in and its model is already a 670 MB fetch, so adding 22 MB to that path is invisible, while adding it to every desktop download is not. But that is a product call.

One correctness note for whoever picks this up. setup --sherpa currently installs only the model. The plugin loader deliberately does not name that command in its error text, because pointing at it would send someone round a loop that cannot fix the problem. When packaging lands, that message and the two doc references should start naming it again.

silverstein avatar Aug 11 '26 00:08 silverstein