botp
/

Solomon / mlx /tests /test_hub.py
orz99's picture ArcherHume's picture
Duplicate from DoccyHealth/Solomon
1d2de8a
Raw History Blame Contribute Delete
7.23 kB
"""Adapter-only downloads, immutable reuse, integrity failures and atomic assembly."""
import copy
import fcntl
import json
from pathlib import Path
import pytest
import solomon_mlx_hub.prepare as hub
from solomon_mlx.artifacts import sha256
REVISION = "a" * 40
@pytest.fixture
def release(tmp_path, monkeypatch):
source, base = tmp_path / "source", tmp_path / "base"
(source / "adapter").mkdir(parents=True)
(source / "mlx/bf16").mkdir(parents=True)
base.mkdir()
payloads = {"adapter.safetensors": b"trained adapter", "heads.npz": b"trained heads"}
for name, value in payloads.items():
(source / "adapter" / name).write_bytes(value)
for name in ("LICENSE", "NOTICE", "MODIFICATIONS.md"):
(source / "mlx/bf16" / name).write_text(name)
(base / "config.json").write_text('{"model_type":"qwen3_5"}')
manifest = tmp_path / "base-manifest.json"
manifest.write_text(
json.dumps(
{
"revision": hub.RELEASE["binding_contract"]["base_revision"],
"files": [
{
"name": "config.json",
"size": (base / "config.json").stat().st_size,
"sha256": sha256(base / "config.json"),
}
],
}
)
)
reference = {
**{name: sha256(source / "adapter" / name) for name in payloads},
**{name: sha256(source / "mlx/bf16" / name) for name in ("LICENSE", "NOTICE", "MODIFICATIONS.md")},
"backbone/config.json": sha256(base / "config.json"),
"backbone/model.safetensors": "historical-serialization-hash",
}
spec = copy.deepcopy(hub.RELEASE)
spec["reference_files"] = reference
spec["base_manifest_sha256"] = sha256(manifest)
spec["source_hashes"] = {"adapter/" + name: reference[name] for name in payloads}
spec["binding_contract"]["base_hashes"] = {"config.json": sha256(base / "config.json")}
monkeypatch.setattr(hub, "RELEASE", spec)
monkeypatch.setattr(hub, "MANIFEST", manifest)
calls = []
def convert(root, output):
assert Path(root) == base
calls.append("convert")
output.mkdir()
(output / "config.json").write_bytes((base / "config.json").read_bytes())
(output / "model.safetensors").write_bytes(b"new serialization")
monkeypatch.setattr(hub, "convert_bf16", convert)
return source, base, tmp_path / "model", calls
def test_adapter_only_hub_then_verified_offline_reuse(release, monkeypatch):
source, base, output, calls = release
def download(repo_id, **kwargs):
assert repo_id == "DoccyHealth/Solomon"
assert kwargs["revision"] == REVISION
assert "adapter/**" in kwargs["allow_patterns"]
assert not any(
"backbone" in pattern or pattern == "mlx/bf16/**" for pattern in kwargs["allow_patterns"]
)
calls.append("download")
return str(source)
monkeypatch.setattr(hub, "snapshot_download", download)
assert hub.prepare_from_hub(output, revision=REVISION, base_dir=base) == output
binding = hub.verify_prepared(output)
assert binding["hub_preparation"]["revision"] == REVISION
assert binding["adapter_scale"] == 2.0
assert binding["files"]["backbone/model.safetensors"] != "historical-serialization-hash"
original_binding = (output / "binding.json").read_bytes()
assert hub.prepare_from_hub(output, revision=REVISION) == output
assert calls == ["download", "convert"]
assert (output / "binding.json").read_bytes() == original_binding
(output / "adapter.safetensors").write_bytes(b"corrupt")
with pytest.raises(ValueError, match="checksum mismatch"):
hub.prepare_from_hub(output, revision=REVISION)
assert calls == ["download", "convert"]
def test_base_download_pinned_and_verified_before_conversion(release, monkeypatch):
source, _base, output, calls = release
def download(repo_id, **kwargs):
calls.append(repo_id)
if repo_id == "DoccyHealth/Solomon":
return source
assert repo_id == "Qwen/Qwen3.8-27B"
assert kwargs["revision"] == hub.RELEASE["binding_contract"]["base_revision"]
target = Path(kwargs["local_dir"])
target.mkdir(parents=True)
(target / "config.json").write_bytes(b"corrupt base")
monkeypatch.setattr(hub, "snapshot_download", download)
with pytest.raises(ValueError, match="Base size mismatch"):
hub.prepare_from_hub(output, revision=REVISION)
assert calls == ["DoccyHealth/Solomon", "Qwen/Qwen3.8-27B"]
assert not output.exists()
def test_bad_source_stops_before_base_or_conversion(release):
source, base, output, calls = release
(source / "adapter/heads.npz").write_bytes(b"corrupt")
with pytest.raises(ValueError, match="source checksum mismatch"):
hub.prepare_from_snapshot(source, output, revision=REVISION, base_dir=base)
assert not calls and not output.exists()
def test_converter_failure_is_atomic_and_retryable(release, monkeypatch):
source, base, output, calls = release
convert = hub.convert_bf16
def fail(*args):
args[1].mkdir()
(args[1] / "partial").write_bytes(b"partial output")
raise RuntimeError("conversion failed")
monkeypatch.setattr(hub, "convert_bf16", fail)
with pytest.raises(RuntimeError, match="conversion failed"):
hub.prepare_from_snapshot(source, output, revision=REVISION, base_dir=base)
assert not output.exists()
assert list(output.parent.glob(".model.*")) == [output.parent / ".model.prepare.lock"]
monkeypatch.setattr(hub, "convert_bf16", convert)
hub.prepare_from_snapshot(source, output, revision=REVISION, base_dir=base)
assert calls == ["convert"]
def test_mutable_revision_and_converter_drift_fail_closed(release, monkeypatch):
source, base, output, calls = release
for revision in (None, "main", "abc123", "A" * 40):
with pytest.raises(ValueError, match="commit SHA"):
hub.prepare_from_hub(output, revision=revision)
monkeypatch.setattr(hub, "code_identity", lambda: "changed source")
with pytest.raises(ValueError, match="Converter source identity"):
hub.prepare_from_snapshot(source, output, revision=REVISION, base_dir=base)
assert not calls
def test_bound_paths_cannot_escape_output(release):
source, base, output, _calls = release
hub.prepare_from_snapshot(source, output, revision=REVISION, base_dir=base)
external = output.parent / "external"
target = output / "backbone/model.safetensors"
target.rename(external)
target.symlink_to(external)
with pytest.raises(ValueError, match="checksum mismatch"):
hub.verify_prepared(output)
def test_simultaneous_preparations_fail_without_mutation(release):
source, base, output, calls = release
with (output.parent / ".model.prepare.lock").open("a") as handle:
fcntl.flock(handle, fcntl.LOCK_EX | fcntl.LOCK_NB)
with pytest.raises(RuntimeError, match="Another process"):
hub.prepare_from_snapshot(source, output, revision=REVISION, base_dir=base)
assert not output.exists() and not calls