Skip to content

Automation foundation model for tiny devices: 2-bit, 8-29 MB, tool calls, ASR, structured extraction and embeddings on phones, wearables, smart homes, robots, cars and microcontrollers.

fix(fetch): refuse a machine no engine is built for instead of handing it x86_64 (#161)

kevinc6b0fc679531
diff --git a/needle/agent/fetch.py b/needle/agent/fetch.py
index 6fdaf96..aa605b8 100644
--- a/needle/agent/fetch.py
+++ b/needle/agent/fetch.py
@@ -101,14 +101,28 @@ def _is_musl():
return not libc
+# platform.machine() spellings of the two architectures the engine is built for.
+_ENGINE_ARCHES = {"x86_64": "x86_64", "amd64": "x86_64", "aarch64": "aarch64", "arm64": "aarch64"}
+
+
def _platform_tag():
machine = platform.machine().lower()
if sys.platform == "darwin":
arch = "arm64" if machine in ("arm64", "aarch64") else "x86_64"
return "macosx_11_0_" + arch
+ arch = _ENGINE_ARCHES.get(machine)
+ if arch is None:
+ # Any other machine (32-bit ARM or x86, riscv64, a 32-bit Python on
+ # Windows) used to be handed the x86_64 build, which then failed to load
+ # with a wrong-ELF-class or "not a valid Win32 application" error, after
+ # a second download of the other libc's x86_64 build on Linux.
+ system = "Windows" if sys.platform == "win32" else "Linux"
+ raise RuntimeError(
+ f"no Needle engine is published for {system} on {platform.machine() or 'this machine'}; "
+ "the engine builds are for x86_64 and arm64. Build libneedle for this machine "
+ "and point NEEDLE3_LIB_PATH (NEEDLE2_LIB_PATH for Needle 2) at it.")
if sys.platform == "win32":
- return "win_arm64" if machine in ("arm64", "aarch64") else "win_amd64"
- arch = "aarch64" if machine in ("aarch64", "arm64") else "x86_64"
+ return "win_arm64" if arch == "aarch64" else "win_amd64"
family = "musllinux_1_2_" if _is_musl() else "manylinux2014_"
return family + arch
diff --git a/tests/test_fetch.py b/tests/test_fetch.py
index 46c1a17..25b38db 100644
--- a/tests/test_fetch.py
+++ b/tests/test_fetch.py
@@ -236,6 +236,29 @@ def test_glibc_detected_from_maps(monkeypatch, tmp_path):
assert fetch._is_musl() is False
+def test_platform_tag_names_only_the_architectures_the_engine_is_built_for(monkeypatch):
+ import platform
+ from needle.agent import fetch
+
+ monkeypatch.setattr(fetch, "_is_musl", lambda: False)
+ for system, machine, tag in (("linux", "x86_64", "manylinux2014_x86_64"),
+ ("linux", "aarch64", "manylinux2014_aarch64"),
+ ("win32", "AMD64", "win_amd64"),
+ ("win32", "ARM64", "win_arm64"),
+ ("darwin", "arm64", "macosx_11_0_arm64")):
+ monkeypatch.setattr(sys, "platform", system)
+ monkeypatch.setattr(platform, "machine", lambda machine=machine: machine)
+ assert fetch._platform_tag() == tag
+
+ # Every other machine was handed the x86_64 build, which cannot load there.
+ for system, machine in (("linux", "armv7l"), ("linux", "riscv64"), ("linux", "i686"),
+ ("win32", "x86")):
+ monkeypatch.setattr(sys, "platform", system)
+ monkeypatch.setattr(platform, "machine", lambda machine=machine: machine)
+ with pytest.raises(RuntimeError, match=f"on {machine};.*NEEDLE3_LIB_PATH"):
+ fetch._platform_tag()
+
+
def test_other_libc_tag_swaps_families(monkeypatch):
from needle.agent import fetch