botp
/

File size: 7,227 Bytes
1d2de8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
"""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