Skip to content

The agent that grows with you

feat(pm): resolve llama.cpp updates from the llama.app installer bucket

ethernet3cbd410d4b02
diff --git a/pm/packages.py b/pm/packages.py
index 1e766db44bd..5e63c51f0b4 100644
--- a/pm/packages.py
+++ b/pm/packages.py
@@ -23,6 +23,8 @@ from pm.store import ALL_TARGETS, Store, flatten_single_dir, merge_tree
from pm.update import (
btbn_versions,
github_release_tags,
+ llama_app_bucket_versions,
+ llama_app_latest,
martin_riedl_versions,
node_latest_versions,
npm_dist_tags,
@@ -772,9 +774,17 @@ class LlamaCpp(BinaryPackage):
return self.fetch_urls(version, target)[0]
def latest_versions(self, target: str, locked=None) -> list[str]:
- # llama.cpp tags are b<build> (b10362); the lock version is the
- # bare build number (10362).
- return github_release_tags("ggml-org/llama.cpp", strip_prefix="b")
+ # Resolve from the llama.app installer bucket (the installer's own
+ # updater pointer + version index — no API token, no rate limit):
+ # the `latest` pointer is the authoritative "next version"; the
+ # bucket tree supplies the full candidate list. Artifacts still
+ # come from the llama.cpp GitHub releases (1:1 tag correspondence).
+ latest = llama_app_latest()
+ if latest is not None:
+ return [latest, *llama_app_bucket_versions()]
+ return llama_app_bucket_versions() or github_release_tags(
+ "ggml-org/llama.cpp", strip_prefix="b"
+ )
def known_sha256(self, version: str, url: str) -> Optional[str]:
"""GitHub's release API serves every asset's digest, so pinning a
diff --git a/pm/update.py b/pm/update.py
index 380c450c749..5c7f34ac2f2 100644
--- a/pm/update.py
+++ b/pm/update.py
@@ -182,13 +182,86 @@ def _get_json(url: str) -> dict | list:
return json.load(resp)
-def _get_text(url: str) -> str:
+def _get_text(url: str, headers: Optional[dict] = None) -> str:
+ hdrs = dict(_UA)
+ if headers:
+ hdrs.update(headers)
with urllib.request.urlopen(
- urllib.request.Request(url, headers=_UA), timeout=60
+ urllib.request.Request(url, headers=hdrs), timeout=60
) as resp:
return resp.read().decode("utf-8", "replace")
+# ── llama.app installer bucket (ggml-org/install.sh) ──────────────────────
+# The llama-install.sh installer resolves its build version from
+# `.../resolve/latest` (a short tag like b10679) and downloads prebuilt
+# llama-app binaries from the HF bucket tree {ARCH}/{OS}/{backend}/{CONFIG}/
+# llama-app[.exe].zst. pm does NOT fetch those binaries (CONFIG codes are
+# hardware-probe-derived, not precomputable, and the llama.cpp GitHub
+# releases are our artifact source) — but the bucket's version index is a
+# better "what should we be on" signal than scraping every GitHub release:
+# it is the installer's own updater pointer, needs no API token, and is
+# not rate-limited. We resolve versions from it and still fetch artifacts
+# from the llama.cpp GitHub releases (every bucket tag corresponds 1:1 to
+# a GitHub release tag, so a bump always has our per-target assets).
+
+_LLAMA_BUCKET = "ggml-org/install.sh"
+_LLAMA_BUCKET_API = f"https://huggingface.co/api/buckets/{_LLAMA_BUCKET}"
+_LLAMA_BUCKET_RESOLVE = f"https://huggingface.co/buckets/{_LLAMA_BUCKET}/resolve"
+
+
+def _hf_headers() -> dict:
+ """Optional bearer auth for HF bucket fetches (HF_TOKEN), like the
+ installer's own requests."""
+ headers = {}
+ token = os.environ.get("HF_TOKEN")
+ if token:
+ headers["Authorization"] = f"Bearer {token}"
+ return headers
+
+
+def llama_app_latest() -> Optional[str]:
+ """The build tag the llama.app installer's `latest` pointer currently
+ resolves to — the updater's "next version". Returns the bare build
+ number ("10679") or None when unreachable."""
+ try:
+ text = _get_text(f"{_LLAMA_BUCKET_RESOLVE}/latest", headers=_hf_headers())
+ except Exception:
+ return None
+ # The tag is b<digits> (b10679) — the first digit run is the build
+ # number. A \b boundary would fail between the 'b' and the digits.
+ m = re.search(r"(\d+)", text)
+ return m.group(1) if m else None
+
+
+def llama_app_bucket_versions() -> list[str]:
+ """Build numbers visible in the llama.app bucket, newest-first.
+
+ The HF bucket tree API IGNORES the offset param (verified: every
+ offset returns the same first page — the oldest ~1000 paths, sorted
+ by path ascending). So the tree can only ever enumerate the OLDEST
+ builds, never the newest — the `latest` pointer (llama_app_latest)
+ is the authoritative "next version" source. This helper returns what
+ the tree CAN see (deduped, sorted by build number descending) as a
+ bounded supplement; callers should put llama_app_latest() first.
+ """
+ versions = []
+ seen = set()
+ try:
+ data = _get_json(f"{_LLAMA_BUCKET_API}/tree?limit=1000&offset=0")
+ except Exception:
+ return []
+ for entry in data or []:
+ m = re.match(r"^(b\d+)/", entry.get("path", ""))
+ if m:
+ tag = m.group(1)[1:] # strip the leading 'b'
+ if tag not in seen:
+ seen.add(tag)
+ versions.append(int(tag))
+ versions.sort(reverse=True)
+ return [str(v) for v in versions]
+
+
def github_release_tags(repo: str, *, strip_prefix: str = "") -> list[str]:
"""Newest-first release tag names for a GitHub repo (releases, not all
tags — no drafts/prereleases, and a rolling 'latest' pseudo-release is
diff --git a/tests/pm/test_update.py b/tests/pm/test_update.py
index 547bc6f6a84..00b76e238f1 100644
--- a/tests/pm/test_update.py
+++ b/tests/pm/test_update.py
@@ -182,3 +182,93 @@ def test_resolve_package_calls_latest_versions_per_target():
assert isinstance(r, Resolved)
assert r.changed
assert r.version == "26.8.1"
+
+
+# ── llama.app installer bucket helpers (pure, monkeypatched) ───────────────
+
+
+def test_llama_app_latest_parses_build_number(monkeypatch):
+ from pm import update as u
+
+ calls = []
+ monkeypatch.setattr(
+ u,
+ "_get_text",
+ lambda url, headers=None: (calls.append((url, headers)) or "b10679\n"),
+ )
+ assert u.llama_app_latest() == "10679"
+ url, headers = calls[0]
+ assert url.endswith("/resolve/latest")
+ assert headers == {} # no HF_TOKEN → no auth header
+
+
+def test_llama_app_latest_honors_hf_token(monkeypatch):
+ from pm import update as u
+
+ monkeypatch.setenv("HF_TOKEN", "hf-secret")
+ seen = {}
+ monkeypatch.setattr(u, "_get_text", lambda url, headers=None: (seen.update(headers) or "b10612\n"))
+ assert u.llama_app_latest() == "10612"
+ assert seen.get("Authorization") == "Bearer hf-secret"
+
+
+def test_llama_app_latest_unreachable_returns_none(monkeypatch):
+ from pm import update as u
+
+ monkeypatch.setattr(u, "_get_text", lambda url, headers=None: (_ for _ in ()).throw(OSError("down")))
+ assert u.llama_app_latest() is None
+
+
+def test_llama_app_bucket_versions_dedupes_and_sorts(monkeypatch):
+ from pm import update as u
+
+ # The HF tree API ignores offset and returns only the FIRST page —
+ # the oldest ~1000 paths sorted by path ascending. The helper must
+ # dedupe (the same build appears on many paths) and sort by build
+ # number descending so the newest of what the tree can see comes first.
+ page = [
+ {"path": "b10326/aarch64/linux/cpu/kk/llama-app.zst"},
+ {"path": "b10098/x86_64/linux/vulkan/kk/llama-app.zst"},
+ {"path": "b10326/x86_64/windows/cuda/13/kk/llama-app.exe.zst"}, # dup
+ {"path": "b9733/aarch64/macos/metal/kk/llama-app.zst"},
+ {"path": "latest/whatever"}, # non-build path must be ignored
+ ]
+ monkeypatch.setattr(u, "_get_json", lambda url: page)
+ assert u.llama_app_bucket_versions() == ["10326", "10098", "9733"]
+
+
+def test_llama_app_bucket_versions_unreachable_returns_empty(monkeypatch):
+ from pm import update as u
+
+ monkeypatch.setattr(u, "_get_json", lambda url: (_ for _ in ()).throw(OSError("down")))
+ assert u.llama_app_bucket_versions() == []
+
+
+def test_llamacpp_latest_versions_prefers_bucket(monkeypatch):
+ """The real LlamaCppCpu class: latest from the llama.app `latest`
+ pointer first, then the bucket tree, GitHub only as a fallback.
+ Patching targets the from-imported names in pm.packages (a from-import
+ copies the reference at import time — patching pm.update would miss)."""
+ import pm.packages as pkgs
+
+ monkeypatch.setattr(pkgs, "llama_app_latest", lambda: "10679")
+ monkeypatch.setattr(pkgs, "llama_app_bucket_versions", lambda: ["10679", "10612", "10362"])
+ called = []
+ monkeypatch.setattr(pkgs, "github_release_tags", lambda *a, **k: (called.append(a) or ["99999"]))
+
+ pkg = pkgs.LlamaCppCpu()
+ versions = pkg.latest_versions("linux-x64")
+ assert versions == ["10679", "10679", "10612", "10362"]
+ assert called == [] # GitHub never consulted when the bucket answered
+
+
+def test_llamacpp_latest_versions_github_fallback(monkeypatch):
+ """Bucket unreachable → fall back to the GitHub releases tags."""
+ import pm.packages as pkgs
+
+ monkeypatch.setattr(pkgs, "llama_app_latest", lambda: None)
+ monkeypatch.setattr(pkgs, "llama_app_bucket_versions", lambda: [])
+ monkeypatch.setattr(pkgs, "github_release_tags", lambda *a, **k: ["10362", "10217"])
+
+ pkg = pkgs.LlamaCppCpu()
+ assert pkg.latest_versions("linux-x64") == ["10362", "10217"]