Files
OBLITERATUS/tests/test_distributed_model_evidence_contracts.py
Joseph MaglyandClaude Opus 5.5 bdd7abaeba test(distributed): cover model admission, manifest, evidence, session and identity contracts (#304)
Single-process CPU tests (no Gloo/NCCL/CUDA) for the distributed
model-loading boundary: Llama config admission, runtime/capability
binding, safetensors metadata admission against a tiny immutable BF16
checkpoint, bounded load deadline, caller mesh validation, loader
result handling, post-load hook/replica/manifest validation, closed
evidence records and private sinks, session ownership primitives,
binding derivation, consensus record parsing, error translation, and
supply-chain identity digests.

Release-lane missing lines: model.py 175->1, model_session.py 86->0,
model_evidence.py 75->2, model_manifest.py 60->2, model_identity.py 28->0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 17:03:03 -04:00

520 lines
19 KiB
Python

"""Closed-schema, canonical-bytes and private-sink contracts for model-load evidence."""
from __future__ import annotations
import json
import os
from collections.abc import Mapping
from dataclasses import asdict, replace
import pytest
import obliteratus.distributed.model_evidence as evidence_module
from obliteratus.distributed.contracts import ContractError
from obliteratus.distributed.model_evidence import (
MAX_JSON_DEPTH,
LoadEvidenceSummary,
build_rank_load_evidence,
normalize_rank_load_evidence,
publish_rank_load_evidence,
read_rank_load_evidence,
validate_loaded_summary_set,
verify_rank_load_evidence,
)
from tests.test_distributed_model_contracts import _load_bindings, _loaded_evidence
pytestmark = pytest.mark.cpu
RUN_ID = "1" * 32
def _summary(**overrides: object) -> LoadEvidenceSummary:
values: dict[str, object] = {
"schema_version": 1,
"record_type": "lms_rank_load_summary",
"result": "loaded",
"run_id": RUN_ID,
"rank": 0,
"world_size": 2,
"evidence_tier": "protocol_cpu",
"bindings_digest": "a" * 64,
"private_record_digest": "b" * 64,
"manifest_digest": "c" * 64,
"parameter_count": 12,
"buffer_count": 0,
"hook_count": 1,
"error_code": None,
}
values.update(overrides)
return LoadEvidenceSummary(**values)
def _failed_summary(**overrides: object) -> LoadEvidenceSummary:
values: dict[str, object] = {
"result": "failed",
"manifest_digest": None,
"parameter_count": 0,
"hook_count": 0,
"error_code": "LMS_MODEL_LOAD_FAILED",
}
values.update(overrides)
return _summary(**values)
def _private_directory(tmp_path):
directory = tmp_path / "private"
directory.mkdir(mode=0o700)
return directory
def _record(**overrides: object) -> dict[str, object]:
record, _manifest = _loaded_evidence()
record = dict(record)
record.update(overrides)
return record
# ---------------------------------------------------------------------------
# Bounded JSON normalization
# ---------------------------------------------------------------------------
@pytest.mark.parametrize(
("value", "message"),
[
("", "non-empty text"),
(7, "non-empty text"),
("\ud800", "strict UTF-8"),
("x" * 513, "text bound"),
("has space", "invalid format"),
],
)
def test_text_fields_are_bounded_strict_and_patterned(value, message):
with pytest.raises(ContractError, match=message):
evidence_module._require_text(value, "field", pattern=evidence_module._SAFE_TEXT_RE)
class _DuplicateKeys(Mapping):
"""A mapping whose iteration yields one key twice, as a hostile adapter might."""
def __getitem__(self, key):
return 1
def __iter__(self):
return iter(("name", "name"))
def __len__(self):
return 2
def test_json_normalization_refuses_unsafe_or_unbounded_values(monkeypatch):
nested: object = 1
for _ in range(MAX_JSON_DEPTH + 1):
nested = [nested]
with pytest.raises(ContractError, match="depth bound"):
evidence_module._normalize_json(nested)
with pytest.raises(ContractError, match="duplicate field"):
evidence_module._normalize_json(_DuplicateKeys())
with pytest.raises(ContractError, match="non-JSON value"):
evidence_module._normalize_json({"name": b"raw-bytes"})
with pytest.raises(ContractError, match="non-JSON value"):
evidence_module._normalize_json({"name": object()})
with pytest.raises(ContractError, match="integer between"):
evidence_module._normalize_json({"name": 2**63})
monkeypatch.setattr(evidence_module, "MAX_JSON_ITEMS", 3)
with pytest.raises(ContractError, match="item bound"):
evidence_module._normalize_json([1, 2, 3])
def test_canonical_bytes_require_an_object_within_its_byte_bound():
with pytest.raises(ContractError, match="must be a JSON object"):
evidence_module._canonical_bytes([1], maximum=64)
with pytest.raises(ContractError, match="byte bound"):
evidence_module._canonical_bytes({"name": "x" * 64}, maximum=16)
assert evidence_module._canonical_bytes({"b": 1, "a": True}, maximum=64) == (
b'{"a":true,"b":1}\n'
)
@pytest.mark.parametrize(
("payload", "message"),
[
("text", "byte envelope"),
(b"", "byte envelope"),
(b'{"a":1}', "byte envelope"),
(b'{"a":1,"a":1}\n', "duplicate field"),
(b'{"a":12345678901234567890}\n', "integer exceeds"),
(b'{"a":NaN}\n', "non-finite"),
(b'{"a":\xff}\n', "strict UTF-8 JSON"),
(b'{"a":\n', "strict UTF-8 JSON"),
(b"[1]\n", "must be a JSON object"),
(b'{"b":1,"a":1}\n', "not canonical"),
(b'{"a": 1}\n', "not canonical"),
],
)
def test_strict_json_accepts_only_canonical_bounded_objects(payload, message):
with pytest.raises(ContractError, match=message):
evidence_module._strict_json(payload, maximum=128)
def test_strict_json_round_trips_canonical_bytes():
payload = b'{"a":[1,"x"],"b":null}\n'
assert evidence_module._strict_json(payload, maximum=128) == {"a": [1, "x"], "b": None}
# ---------------------------------------------------------------------------
# Private per-rank record schema
# ---------------------------------------------------------------------------
@pytest.mark.parametrize(
("overrides", "message"),
[
({"schema_version": 2}, "schema_version must be 1"),
({"record_type": "other"}, "record_type is invalid"),
({"result": "partial"}, "result is invalid"),
({"run_id": "A" * 32}, "invalid format"),
({"world_size": 1}, "integer between"),
({"rank": 2}, "integer between"),
({"evidence_tier": "production"}, "tier is invalid"),
({"error_code": "LMS_MODEL_LOAD_FAILED"}, "cannot contain an error code"),
({"error_code": "not-a-code"}, "invalid format"),
({"manifest": None}, "requires a manifest object"),
({"bindings": {}}, "bindings do not match"),
({"bindings": {**_load_bindings(), "source_digest": "F" * 64}}, "invalid format"),
],
)
def test_private_record_envelope_is_closed(overrides, message):
with pytest.raises(ContractError, match=message):
normalize_rank_load_evidence(_record(**overrides))
def test_private_record_requires_exact_field_set():
record = _record()
record.pop("error_code")
with pytest.raises(ContractError, match="fields do not match"):
normalize_rank_load_evidence(record)
with pytest.raises(ContractError, match="fields do not match"):
normalize_rank_load_evidence(["not", "a", "mapping"])
@pytest.mark.parametrize(
("mutation", "message"),
[
(lambda value: value.update(schema_version=2), "schema_version must be 1"),
(lambda value: value.update(adapter_id="bad id"), "invalid format"),
(lambda value: value.update(parameters={}), "parameters must be a list"),
(lambda value: value.update(hooks="layer"), "hooks must be a list"),
(lambda value: value.update(buffers={}), "buffers must be a list"),
(lambda value: value.update(extra=1), "fields do not match"),
],
)
def test_manifest_schema_is_closed(mutation, message):
_record_value, manifest = _loaded_evidence()
changed = manifest.to_dict()
mutation(changed)
with pytest.raises(ContractError, match=message):
build_rank_load_evidence(
result="loaded",
run_id=RUN_ID,
rank=0,
world_size=2,
evidence_tier="protocol_cpu",
bindings=_load_bindings(),
manifest=changed,
)
def test_manifest_record_count_is_bounded(monkeypatch):
monkeypatch.setattr(evidence_module, "MAX_MANIFEST_RECORDS", 3)
with pytest.raises(ContractError, match="record-count bound"):
_loaded_evidence()
def test_failed_record_round_trips_without_a_manifest(tmp_path):
directory = _private_directory(tmp_path)
record = build_rank_load_evidence(
result="quarantined",
run_id=RUN_ID,
rank=1,
world_size=2,
evidence_tier="candidate_local_gpu",
bindings=_load_bindings(),
error_code="LMS_CLEANUP_INCOMPLETE",
)
summary = publish_rank_load_evidence(directory, record)
assert summary.result == "quarantined"
assert summary.manifest_digest is None
assert (summary.parameter_count, summary.buffer_count, summary.hook_count) == (0, 0, 0)
assert summary.error_code == "LMS_CLEANUP_INCOMPLETE"
assert verify_rank_load_evidence(directory, summary) == record
assert LoadEvidenceSummary.from_bytes(summary.to_bytes()) == summary
# ---------------------------------------------------------------------------
# Compact summaries
# ---------------------------------------------------------------------------
@pytest.mark.parametrize(
("overrides", "message"),
[
({"schema_version": 2}, "schema identity"),
({"record_type": "lms_private_rank_load_evidence"}, "schema identity"),
({"result": "partial"}, "result is invalid"),
({"evidence_tier": "production"}, "tier is invalid"),
({"parameter_count": 16_384, "hook_count": 1}, "exceed the manifest bound"),
({"manifest_digest": None}, "requires a manifest digest"),
({"error_code": "LMS_MODEL_LOAD_FAILED"}, "cannot contain an error code"),
],
)
def test_loaded_summary_is_closed(overrides, message):
with pytest.raises(ContractError, match=message):
_summary(**overrides)
@pytest.mark.parametrize(
("overrides", "message"),
[
({"manifest_digest": "c" * 64}, "partial manifest"),
({"parameter_count": 1}, "partial manifest"),
({"error_code": None}, "stable load error code"),
({"error_code": "LMS_UNREGISTERED"}, "stable load error code"),
({"error_code": "LMS_LOAD_STATE_UNCERTAIN"}, "quarantine state"),
({"result": "quarantined"}, "quarantine state"),
],
)
def test_unsuccessful_summary_never_describes_partial_success(overrides, message):
with pytest.raises(ContractError, match=message):
_failed_summary(**overrides)
def test_summary_bytes_are_strict_closed_and_canonical():
summary = _failed_summary()
assert LoadEvidenceSummary.from_bytes(summary.to_bytes()) == summary
value = asdict(summary)
value["extra"] = 1
payload = (json.dumps(value, sort_keys=True, separators=(",", ":")) + "\n").encode()
with pytest.raises(ContractError, match="fields do not match"):
LoadEvidenceSummary.from_bytes(payload)
value = asdict(_summary())
value["error_code"] = "lowercase"
payload = (json.dumps(value, sort_keys=True, separators=(",", ":")) + "\n").encode()
with pytest.raises(ContractError, match="invalid format"):
LoadEvidenceSummary.from_bytes(payload)
# ---------------------------------------------------------------------------
# Private sink and readback
# ---------------------------------------------------------------------------
@pytest.mark.parametrize("directory", ["relative/path", "/tmp/../etc"])
def test_private_directory_must_be_absolute_and_normalized(directory):
with pytest.raises(ContractError, match="absolute normalized"):
publish_rank_load_evidence(directory, _record())
def test_private_directory_must_exist_be_a_directory_and_be_owned(tmp_path):
with pytest.raises(ContractError, match="sink is unavailable"):
publish_rank_load_evidence(tmp_path / "missing", _record())
regular = tmp_path / "regular"
regular.write_bytes(b"x")
with pytest.raises(ContractError, match="sink is unavailable"):
publish_rank_load_evidence(regular, _record())
def test_private_directory_root_open_failure_is_contained(monkeypatch):
real_open = os.open
def fail_root(path, flags, *args, **kwargs):
if path == "/":
raise PermissionError("denied")
return real_open(path, flags, *args, **kwargs)
monkeypatch.setattr(evidence_module.os, "open", fail_root)
with pytest.raises(ContractError, match="sink is unavailable"):
evidence_module._open_private_directory("/private")
def test_private_directory_rejects_a_non_directory_descriptor(tmp_path, monkeypatch):
directory = _private_directory(tmp_path)
real_fstat = os.fstat
def regular(descriptor):
values = list(real_fstat(descriptor))
values[0] = 0o100600
return os.stat_result(values)
monkeypatch.setattr(evidence_module.os, "fstat", regular)
with pytest.raises(ContractError, match="must be a directory"):
evidence_module._open_private_directory(directory)
def test_published_record_requires_bounded_private_readback(tmp_path):
directory = _private_directory(tmp_path)
with pytest.raises(ContractError, match="unavailable"):
read_rank_load_evidence(directory, run_id=RUN_ID, rank=0)
path = directory / evidence_module._record_filename(RUN_ID, 0)
path.write_bytes(b"{}\n")
path.chmod(0o644)
with pytest.raises(ContractError, match="not private"):
read_rank_load_evidence(directory, run_id=RUN_ID, rank=0)
path.chmod(0o600)
os.link(path, directory / "second-link")
with pytest.raises(ContractError, match="not private"):
read_rank_load_evidence(directory, run_id=RUN_ID, rank=0)
def test_readback_refuses_symlinked_records(tmp_path):
directory = _private_directory(tmp_path)
target = tmp_path / "target.json"
target.write_bytes(b"{}\n")
(directory / evidence_module._record_filename(RUN_ID, 0)).symlink_to(target)
with pytest.raises(ContractError, match="sink is unavailable"):
read_rank_load_evidence(directory, run_id=RUN_ID, rank=0)
@pytest.mark.parametrize("change", ["truncated", "grew", "metadata"])
def test_readback_detects_records_changed_while_reading(tmp_path, monkeypatch, change):
directory = _private_directory(tmp_path)
summary = publish_rank_load_evidence(directory, _record())
real_read, real_fstat = os.read, os.fstat
reads: list[int] = []
stats: list[int] = []
def read(descriptor, size):
reads.append(size)
if change == "truncated":
return b""
if change == "grew" and size == 1:
return b"x"
return real_read(descriptor, size)
def fstat(descriptor):
stats.append(descriptor)
result = real_fstat(descriptor)
if change == "metadata" and len(stats) == 3: # directory, before, after
values = list(result)
values[1] += 1
return os.stat_result(values)
return result
monkeypatch.setattr(evidence_module.os, "read", read)
monkeypatch.setattr(evidence_module.os, "fstat", fstat)
expected = {"truncated": "truncated", "grew": "grew", "metadata": "changed"}[change]
with pytest.raises(ContractError, match=expected):
verify_rank_load_evidence(directory, summary)
def test_exclusive_write_contains_os_failures_and_cleans_its_temporary(tmp_path, monkeypatch):
directory = _private_directory(tmp_path)
real_link = os.link
def failing_link(*_args, **_kwargs):
raise PermissionError("link denied")
monkeypatch.setattr(evidence_module.os, "link", failing_link)
with pytest.raises(ContractError, match="sink is unavailable"):
publish_rank_load_evidence(directory, _record())
assert list(directory.iterdir()) == []
monkeypatch.setattr(evidence_module.os, "link", real_link)
def test_exclusive_write_requires_progress(tmp_path, monkeypatch):
directory = _private_directory(tmp_path)
monkeypatch.setattr(evidence_module.os, "write", lambda _descriptor, _payload: 0)
with pytest.raises(ContractError, match="no progress"):
publish_rank_load_evidence(directory, _record())
assert list(directory.iterdir()) == []
def test_exclusive_write_refuses_a_non_private_temporary(tmp_path, monkeypatch):
directory = _private_directory(tmp_path)
real_fstat = os.fstat
calls: list[int] = []
def shared(descriptor):
calls.append(descriptor)
values = list(real_fstat(descriptor))
if len(calls) == 2: # directory first, then the temporary file
values[3] = 2 # st_nlink
return os.stat_result(values)
monkeypatch.setattr(evidence_module.os, "fstat", shared)
with pytest.raises(ContractError, match="not private and regular"):
publish_rank_load_evidence(directory, _record())
assert list(directory.iterdir()) == []
def test_publish_refuses_readback_that_differs_from_the_written_payload(tmp_path, monkeypatch):
directory = _private_directory(tmp_path)
monkeypatch.setattr(evidence_module, "_read_stable", lambda _fd, _name: b"{}\n")
with pytest.raises(ContractError, match="readback disagrees"):
publish_rank_load_evidence(directory, _record())
def test_verify_requires_a_typed_summary(tmp_path):
with pytest.raises(ContractError, match="invalid type"):
verify_rank_load_evidence(_private_directory(tmp_path), asdict(_summary()))
# ---------------------------------------------------------------------------
# Loaded summary set
# ---------------------------------------------------------------------------
def _loaded_pair():
first = _summary(private_record_digest="d" * 64)
second = replace(first, rank=1, private_record_digest="e" * 64)
return first, second
@pytest.mark.parametrize(
("summaries", "message"),
[
("loaded", "fixed-rank sequence"),
((), "invalid record"),
((object(),), "invalid record"),
],
)
def test_summary_set_requires_a_typed_sequence(summaries, message):
with pytest.raises(ContractError, match=message):
validate_loaded_summary_set(summaries)
def test_summary_set_requires_one_fixed_world_in_rank_order():
first, second = _loaded_pair()
assert len(validate_loaded_summary_set((first, second))) == 64
with pytest.raises(ContractError, match="rank order"):
validate_loaded_summary_set((second, first))
with pytest.raises(ContractError, match="rank order"):
validate_loaded_summary_set((first,))
@pytest.mark.parametrize(
"drift",
[
{"bindings_digest": "f" * 64},
{"evidence_tier": "candidate_local_gpu"},
{"hook_count": 2},
],
)
def test_summary_set_must_unanimously_describe_one_loaded_world(drift):
first, second = _loaded_pair()
with pytest.raises(ContractError, match="unanimously"):
validate_loaded_summary_set((first, replace(second, **drift)))
def test_summary_set_refuses_failed_members():
first, _second = _loaded_pair()
failed = _failed_summary(rank=1)
with pytest.raises(ContractError, match="unanimously"):
validate_loaded_summary_set((first, failed))