Files
OBLITERATUS/tests/test_distributed_profile_boundaries.py

97 lines
4.3 KiB
Python

"""Closed profile and control-plane contracts reject before execution."""
from dataclasses import replace
from types import SimpleNamespace
from unittest.mock import Mock
import pytest
from obliteratus.distributed import consensus, launcher, model
from obliteratus.distributed.contracts import ContractError, RuntimeContractError
from tests.test_distributed_rank_local import rank_config
@pytest.mark.parametrize("changes,message", [
({"rank_storage": None}, "exact rank-local two-host TP2"),
({"world_size": 4}, "exact rank-local two-host TP2"),
({"local_world_size": 2}, "exact rank-local two-host TP2"),
({"tensor_parallel_size": 1}, "exact rank-local two-host TP2"),
({"coordinator_rank": 1}, "exact rank-local two-host TP2"),
({"device_kind": "cuda"}, "explicit physical tier"),
({"schema_version": 2}, "requires schema version 3"),
])
def test_profile_geometry_and_evidence_cannot_be_relabelled(tmp_path, changes, message):
config = rank_config(tmp_path)
assert config.validate() is config
with pytest.raises(ContractError, match=message):
replace(config, **changes).validate()
@pytest.fixture
def control(monkeypatch):
state = SimpleNamespace(available=True, initialized=True, backend="gloo", size=2)
state.gather = Mock(side_effect=AssertionError("invalid membership must not gather"))
monkeypatch.setattr(consensus, "dist", SimpleNamespace(
is_available=lambda: state.available, is_initialized=lambda: state.initialized,
get_backend=lambda _: state.backend, get_rank=lambda _: 0,
get_world_size=lambda _: state.size, all_gather=state.gather,
))
return state
@pytest.mark.parametrize("field,value,message", [
("available", False, "must be initialized"), ("initialized", False, "must be initialized"),
("backend", "nccl", "requires the Gloo backend"),
("size", 1, "world_size must be between"),
("size", consensus.MAX_WORLD_SIZE + 1, "world_size must be between"),
])
def test_consensus_refuses_wrong_or_missing_process_state(control, field, value, message):
setattr(control, field, value)
with pytest.raises(ContractError, match=message):
consensus.gloo_all_gather_records({"ready": True})
control.gather.assert_not_called()
@pytest.mark.parametrize("capacity", [True, 0, -1, 1.5, consensus.MAX_CONSENSUS_BYTES + 1])
def test_consensus_rejects_invalid_wire_capacity_before_collective(control, capacity):
with pytest.raises(ContractError, match="capacity must be between"):
consensus.gloo_all_gather_records({"ready": True}, capacity=capacity)
control.gather.assert_not_called()
@pytest.mark.parametrize("payload", [b"{", b"\xff"])
def test_corrupt_preparation_vote_never_becomes_acceptance(monkeypatch, payload):
monkeypatch.setattr(consensus, "gloo_all_gather_records", lambda *args, **kwargs: (payload,))
with pytest.raises(ContractError, match="preparation vote is not JSON") as failure:
consensus.gather_failure_codes(1, None)
assert isinstance(failure.value.__cause__, (UnicodeError, ValueError))
def test_non_posix_diagnostics_do_not_start_posix_resource_tracker(monkeypatch):
ensure = Mock()
monkeypatch.setattr(launcher, "os", SimpleNamespace(name="nt"))
monkeypatch.setattr(launcher, "resource_tracker", SimpleNamespace(ensure_running=ensure))
launcher._prepare_diagnostic_helpers()
ensure.assert_not_called()
def test_model_digest_refuses_oversized_canonical_configuration():
assert len(model.model_config_digest({"vocab_size": 32})) == 64
with pytest.raises(ContractError, match="bounded digest contract"):
model.model_config_digest({"unbounded": "x" * (8 << 20)})
def test_model_preparation_vote_failure_prevents_next_operation(monkeypatch):
error = OSError("private transport failure")
gather = Mock(side_effect=error)
monkeypatch.setattr(model, "gather_failure_codes", gather)
reached = []
with pytest.raises(RuntimeContractError, match="model preparation vote failed") as failure:
with model._local_step(77, "control", code="LMS_LOAD_MANIFEST_INVALID"):
reached.append("prepared")
reached.append("next collective")
assert failure.value.code == "LMS_LOAD_MANIFEST_INVALID"
assert "private" not in str(failure.value)
assert reached == ["prepared"]
gather.assert_called_once_with(77, None, group="control")