Skip to content

armgit / ODS

Public Imported from GitHub
0

ODS V3 Pre-Release: Public testing and refinement ahead of the official V3 launch. Turn your PC, Mac, or Linux box into a private AI server.

6.0 KB
Raw Blame
#!/usr/bin/env python3
"""Every curated catalog download must name an immutable Hugging Face revision.

A mutable ref such as ``resolve/main`` lets an upstream rewrite change or
delete the file behind a catalog entry without any ODS change. On 2026-07-16
ggml-org deleted its Gemma 4 26B-A4B and 31B Q4_K_M files from ``main``, and
both catalog downloads failed with HTTP 404 until they were repointed.
Pinning a 40-hex commit keeps ``gguf_url``, ``gguf_sha256`` and ``size_bytes``
describing one exact file.

vLLM entries (``"runtime": "vllm"``) have no download URL: they pin the
whole snapshot by ``hf_repo`` and a 40-hex ``revision`` and give every file a
sha256 (LFS) or git blob sha1 (small files). They are checked here for that
pin; tests/test-model-library-vllm.py checks the rest of their schema.

Network-free: this checks the catalog's shape, not Hugging Face. Verify a new
pin against the Hub's LFS metadata (``lfs.oid`` / ``lfs.size``) before adding it.
"""

from __future__ import annotations

import json
import re
from pathlib import Path


ROOT = Path(__file__).resolve().parents[1]
CATALOG = ROOT / "config" / "model-library.json"

PINNED_URL = re.compile(
    r"^https://huggingface\.co/"
    r"(?P<repo>[^/\s]+/[^/\s]+)/resolve/(?P<revision>[0-9a-f]{40})/(?P<path>[^\s]+\.gguf)$"
)
SHA256 = re.compile(r"^[0-9a-f]{64}$")
COMMIT = re.compile(r"^[0-9a-f]{40}$")
HF_REPO = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._-]*/[A-Za-z0-9][A-Za-z0-9._-]*$")


def _artifacts(model: dict) -> list[dict]:
    parts = model.get("gguf_parts")
    if isinstance(parts, list) and parts:
        return [
            {"file": part.get("file"), "url": part.get("url"), "sha256": part.get("sha256")}
            for part in parts
        ]
    return [{
        "file": model.get("gguf_file"),
        "url": model.get("gguf_url"),
        "sha256": model.get("gguf_sha256"),
    }]


def _catalog_models() -> list[dict]:
    return json.loads(CATALOG.read_text(encoding="utf-8"))["models"]


def _models() -> list[dict]:
    """The llama.cpp (GGUF) entries; vLLM entries pin a snapshot instead."""
    return [model for model in _catalog_models() if model.get("runtime") != "vllm"]


def test_every_vllm_snapshot_pins_a_commit_and_every_file_digest() -> None:
    offenders = []
    vllm = [model for model in _catalog_models() if model.get("runtime") == "vllm"]
    for model in vllm:
        if not HF_REPO.match(str(model.get("hf_repo") or "")) or not COMMIT.match(str(model.get("revision") or "")):
            offenders.append(f"{model['id']}: hf_repo {model.get('hf_repo')!r} @ {model.get('revision')!r}")
        for item in model.get("files") or [{}]:
            digest_ok = (
                SHA256.match(str(item.get("sha256") or "")) if "sha256" in item
                else COMMIT.match(str(item.get("git_blob_sha1") or ""))
            )
            if not digest_ok or not isinstance(item.get("size"), int):
                offenders.append(f"{model['id']}: {item.get('path') or '<missing path>'}")
    assert vllm, "the catalog's vLLM entries went missing"
    assert not offenders, (
        "vLLM entries must pin <owner>/<repo> at a 40-hex commit and give every file a size and "
        "a sha256 or git_blob_sha1:\n  " + "\n  ".join(offenders)
    )


def test_every_download_url_pins_a_commit() -> None:
    offenders = []
    for model in _models():
        for artifact in _artifacts(model):
            url = str(artifact.get("url") or "")
            if not PINNED_URL.match(url):
                offenders.append(f"{model['id']}: {url or '<missing url>'}")
    assert not offenders, (
        "catalog download URLs must use resolve/<40-hex commit>, not a branch or tag:\n  "
        + "\n  ".join(offenders)
    )


def test_every_download_declares_a_sha256() -> None:
    offenders = [
        f"{model['id']}: {artifact.get('file')}"
        for model in _models()
        for artifact in _artifacts(model)
        if not SHA256.match(str(artifact.get("sha256") or ""))
    ]
    assert not offenders, "catalog downloads need a lowercase 64-hex sha256:\n  " + "\n  ".join(offenders)


def test_split_parts_share_one_revision() -> None:
    for model in _models():
        parts = model.get("gguf_parts")
        if not isinstance(parts, list) or not parts:
            continue
        sources = {
            (match.group("repo"), match.group("revision"))
            for match in (PINNED_URL.match(str(part.get("url") or "")) for part in parts)
            if match
        }
        assert len(sources) == 1, f"{model['id']}: split parts come from {sorted(sources)}"


def test_declared_source_matches_download_url() -> None:
    for model in _models():
        match = PINNED_URL.match(str(model.get("gguf_url") or ""))
        if not match:
            continue
        if model.get("source_repo"):
            assert model["source_repo"] == match.group("repo"), model["id"]
        if model.get("source_revision"):
            assert model["source_revision"] == match.group("revision"), model["id"]


def test_guard_rejects_mutable_refs() -> None:
    for url in (
        "https://huggingface.co/org/repo-GGUF/resolve/main/model-Q4_K_M.gguf",
        "https://huggingface.co/org/repo-GGUF/resolve/v1.0/model-Q4_K_M.gguf",
        "https://huggingface.co/org/repo-GGUF/resolve/c099eb48e663/model-Q4_K_M.gguf",
        "https://huggingface.co/org/repo-GGUF/resolve/"
        "C099EB48E663FD284577B04978A94FFCCB261841/model-Q4_K_M.gguf",
    ):
        assert not PINNED_URL.match(url), url
    assert PINNED_URL.match(
        "https://huggingface.co/org/repo-GGUF/resolve/"
        "c099eb48e663fd284577b04978a94ffccb261841/Q4_K_M/model-Q4_K_M-00001-of-00002.gguf"
    )


def main() -> int:
    test_guard_rejects_mutable_refs()
    test_every_download_url_pins_a_commit()
    test_every_download_declares_a_sha256()
    test_split_parts_share_one_revision()
    test_declared_source_matches_download_url()
    test_every_vllm_snapshot_pins_a_commit_and_every_file_digest()
    print("[PASS] Every catalog download pins a Hugging Face commit and declares a sha256")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())