"""Closed-schema edge contracts for distributed profiles and canonical records (#304).""" from __future__ import annotations import json from dataclasses import dataclass, replace import pytest from obliteratus.distributed import config as config_module from obliteratus.distributed.config import DistributedModelConfig, DistributedPreflightConfig from obliteratus.distributed.contracts import ( MAX_TEXT_BYTES, ContractError, LogicalPlacement, PlacementKind, RunIdentity, RuntimeStage, StageMessage, Vote, canonical_record, streamed_contract_digest, ) from tests.test_distributed_launcher import _profile pytestmark = pytest.mark.cpu _MODEL = { "adapter_id": "llama-bf16-tp2", "artifact_revision": "a" * 40, "hf_config_digest": "b" * 64, "load_timeout_seconds": 60, } def _write_profile(tmp_path, **changes) -> object: _profile(tmp_path) path = tmp_path / "profile.json" payload = json.loads(path.read_text(encoding="utf-8")) payload.update(changes) path.write_text(json.dumps(payload), encoding="utf-8") return path # --- DistributedPreflightConfig / DistributedModelConfig ------------------------------- def test_schema_v2_profile_loads_and_exposes_its_model_adapter(tmp_path): path = _write_profile(tmp_path, schema_version=2, model=dict(_MODEL)) config = DistributedPreflightConfig.from_file(path) assert config.schema_version == 2 model = config.require_model() assert model == DistributedModelConfig(**_MODEL) # The v2 digest binds the model section. changed = _write_profile( tmp_path, schema_version=2, model={**_MODEL, "load_timeout_seconds": 61} ) assert DistributedPreflightConfig.from_file(changed).digest != config.digest def test_schema_v1_profile_refuses_model_loading(tmp_path): config = _profile(tmp_path) with pytest.raises(ContractError, match="requires schema version 2"): config.require_model() @pytest.mark.parametrize( ("changes", "message"), [ ({"schema_version": 2}, "required profile section"), ({"schema_version": 2, "model": {**_MODEL, "extra": 1}}, "unknown profile field in model"), ({"schema_version": 2, "model": {**_MODEL, "load_timeout_seconds": 0}}, "between"), ({"schema_version": 2, "model": {**_MODEL, "artifact_revision": "x"}}, "invalid format"), ({"schema_version": 2, "model": {**_MODEL, "adapter_id": "bad id!"}}, "invalid format"), ({"schema_version": 3}, "required profile section"), ({"schema_version": 4}, "schema_version must be 1, 2 or 3"), ({"unexpected": {}}, "unknown profile field"), ({"model": dict(_MODEL)}, "unknown profile field"), ], ) def test_profile_schema_version_and_model_section_are_closed(tmp_path, changes, message): with pytest.raises(ContractError, match=message): DistributedPreflightConfig.from_file(_write_profile(tmp_path, **changes)) @pytest.mark.parametrize( ("field", "value", "message"), [ ("adapter_id", "-leading-dash", "adapter_id has an invalid format"), ("artifact_revision", "A" * 40, "artifact_revision has an invalid format"), ("hf_config_digest", "c" * 63, "hf_config_digest has an invalid format"), ("load_timeout_seconds", True, "must be an integer"), ("load_timeout_seconds", 3601, "between 1 and 3600"), ], ) def test_model_config_validates_every_field_on_direct_construction(field, value, message): with pytest.raises(ContractError, match=message): DistributedModelConfig(**{**_MODEL, field: value}) def test_direct_replacement_rechecks_schema_and_model_pairing(tmp_path): v1 = _profile(tmp_path) model = DistributedModelConfig(**_MODEL) with pytest.raises(ContractError, match="schema_version must be 1, 2 or 3"): replace(v1, schema_version=0).validate() with pytest.raises(ContractError, match="version 1 cannot declare"): replace(v1, model=model).validate() with pytest.raises(ContractError, match="version 2 requires a model"): replace(v1, schema_version=2).validate() with pytest.raises(ContractError, match="version 2 requires a model"): replace(v1, schema_version=2).require_model() assert replace(v1, schema_version=2, model=model).require_model() is model def test_profile_that_changes_while_read_is_rejected(tmp_path, monkeypatch): path = _write_profile(tmp_path) original_read = config_module.os.read def truncated_read(descriptor, size): return original_read(descriptor, size)[:-1] monkeypatch.setattr(config_module.os, "read", truncated_read) with pytest.raises(ContractError, match="changed while it was read"): DistributedPreflightConfig.from_file(path) # --- contracts.py primitive validators --------------------------------------------------- def _placement(**changes) -> LogicalPlacement: values = { "logical_name": "model.layers.0.weight", "global_shape": (4, 4), "dtype": "float32", "kind": PlacementKind.COLUMN_WISE, "rank": 0, "world_size": 2, "direction_axis": 1, "shard_dim": 0, "shard_start": 0, "shard_end": 2, } values.update(changes) return LogicalPlacement(**values) @pytest.mark.parametrize( ("changes", "message"), [ ({"logical_name": ""}, "logical_name must be non-empty text"), ({"logical_name": "\ud800"}, "logical_name must be valid UTF-8"), ({"logical_name": "a" * (MAX_TEXT_BYTES + 1)}, f"exceeds {MAX_TEXT_BYTES} UTF-8 bytes"), ({"global_shape": (4, 0)}, "positive two-dimensional"), ({"global_shape": (4, True)}, "positive two-dimensional"), ({"global_shape": [4, 4]}, "positive two-dimensional"), ({"kind": "column_wise"}, "kind must be a PlacementKind"), ], ) def test_text_shape_and_kind_validators_fail_closed(changes, message): with pytest.raises(ContractError, match=message): _placement(**changes) def test_text_validator_accepts_exact_byte_bound(): assert _placement(logical_name="a" * MAX_TEXT_BYTES).logical_name == "a" * MAX_TEXT_BYTES with pytest.raises(ContractError, match="run_id must be non-empty text"): RunIdentity("", "a" * 64, "b" * 64, "c" * 64, "d" * 64, "e" * 40, 2) @pytest.mark.parametrize( ("stage", "vote", "message"), [ ("loaded", None, "stage must be a RuntimeStage"), (RuntimeStage.LOADED, "committed", "vote must be a Vote"), ], ) def test_stage_message_requires_enum_typed_stage_and_vote(stage, vote, message): with pytest.raises(ContractError, match=message): StageMessage("1" * 32, "a" * 64, 0, 1, stage, vote) @pytest.mark.parametrize( "payload", [ b"\xff", b"{", b'{"error_code":null,"identity_digest":"' + b"a" * 64 + b'","rank":0,"run_id":"' + b"1" * 32 + b'","sequence":1,"stage":"unknown","vote":null}', b'{"error_code":null,"identity_digest":"' + b"a" * 64 + b'","rank":"0","run_id":"' + b"1" * 32 + b'","sequence":1,"stage":"loaded","vote":null}', ], ) def test_stage_message_decoder_rejects_non_json_and_wrongly_typed_values(payload): with pytest.raises(ContractError): StageMessage.from_bytes(payload) def test_stage_message_decoder_round_trips_committed_vote(): message = StageMessage("1" * 32, "a" * 64, 1, 3, RuntimeStage.VERIFIED, Vote.COMMITTED) assert StageMessage.from_bytes(message.to_bytes()) == message with pytest.raises(ContractError, match="closed schema"): StageMessage.from_bytes(b'{"rank":1}') @dataclass class _Node: child: object = None def test_canonical_record_rejects_dataclass_and_list_reference_cycles(): node = _Node() node.child = node with pytest.raises(ContractError, match="reference cycle"): canonical_record(node) items: list[object] = [] items.append(items) with pytest.raises(ContractError, match="reference cycle"): canonical_record(items) # Shared, non-cyclic references are legal and serialize twice. shared = [1] assert canonical_record({"a": shared, "b": shared}) == b'{"a":[1],"b":[1]}' @pytest.mark.parametrize( ("value", "message"), [ ({"k" * 17: 1}, "exceeds 16 bytes"), ({"\ud800": 1}, "key must be valid UTF-8"), (["\ud800"], "string must be valid UTF-8"), ({"a": "x" * 6, "b": "y" * 6}, "exceeds 16 bytes"), ], ) def test_canonical_record_bounds_keys_strings_and_total_size(value, message): with pytest.raises(ContractError, match=message): canonical_record(value, max_bytes=16) @pytest.mark.parametrize( ("prefix", "domain", "message"), [ (b"", "records", "prefix"), ("v1", "records", "prefix"), (b"x" * 129, "records", "prefix"), (b"v1", "Records", "domain"), (b"v1", "", "domain"), (b"v1", 7, "domain"), ], ) def test_streamed_digest_rejects_unbounded_prefix_or_domain(prefix, domain, message): with pytest.raises(ContractError, match=message): streamed_contract_digest((), prefix=prefix, domain=domain) def test_streamed_digest_preserves_record_boundaries_and_domains(): first = streamed_contract_digest(({"a": 1}, {"b": 2}), prefix=b"v1", domain="records") assert first == streamed_contract_digest(({"a": 1}, {"b": 2}), prefix=b"v1", domain="records") assert first != streamed_contract_digest(({"b": 2}, {"a": 1}), prefix=b"v1", domain="records") assert first != streamed_contract_digest(({"a": 1}, {"b": 2}), prefix=b"v1", domain="other") assert first != streamed_contract_digest(({"a": 1}, {"b": 2}), prefix=b"v2", domain="records")