Files
OBLITERATUS/tests/test_distributed_preflight_boundaries.py

236 lines
11 KiB
Python

"""CPU protocol receipts never stand in for live membership or peer teardown."""
import json
from contextlib import contextmanager
from dataclasses import replace
from types import SimpleNamespace
import pytest
import obliteratus.distributed.preflight as preflight
import obliteratus.distributed.rank_local as local
from obliteratus.distributed.contracts import ContractError, RuntimeContractError, canonical_record, contract_digest
from obliteratus.distributed.evidence import read_evidence
from tests.test_distributed_rank_local import install_review, rank_config, review_record
from tests.test_distributed_rank_local_gloo import launch_for, snapshot_for
@pytest.fixture
def rank_world(tmp_path, monkeypatch):
"""Real records/directories, with explicit in-process transport observations."""
config = rank_config(tmp_path)
launch = launch_for(config, 0)
source = preflight.SourceIdentity(
config.source_digest, config.model_digest, config.tokenizer_digest, 1, 1,
)
snapshots = tuple(snapshot_for(config, rank, source) for rank in range(2))
records = tuple(preflight._attestation(config, launch_for(config, rank), snapshots[rank])
for rank in range(2))
install_review(monkeypatch, review_record(config, records))
peer_attempt = local.create_attempt(config.for_rank(1).staging_path, config.run_id, 1)
state = SimpleNamespace(config=config, launch=launch, records=records, corrupt=None,
fail_at=None, events=[], probes=SimpleNamespace(collect=lambda *_: snapshots[0]))
monkeypatch.setattr(preflight.dist, "is_available", lambda: True)
monkeypatch.setattr(preflight.dist, "is_initialized", lambda: True)
monkeypatch.setattr(preflight.dist, "get_backend", lambda: "gloo")
monkeypatch.setattr(preflight.dist, "get_rank", lambda: 0)
monkeypatch.setattr(preflight.dist, "get_world_size", lambda: 2)
monkeypatch.setattr(preflight, "gather_failure_codes", lambda _, code: (code,) if code else ())
monkeypatch.setattr(preflight, "unanimous_vote", lambda _, accepted: accepted)
monkeypatch.setattr(preflight, "require_record_consensus", contract_digest)
monkeypatch.setattr(preflight, "require_consensus_digest", lambda digest: digest)
def gather(record):
values = records if isinstance(record, preflight.RankAttestation) else (
replace(record, rank=0), replace(record, rank=1),
)
return tuple(canonical_record(item) for item in values)
def exchange(run_id, identity, rank, stage, sequence, payload):
state.events.append(stage)
assert (run_id, rank) == (config.run_id, 0)
if state.fail_at == stage:
raise RuntimeContractError("LMS_CONSENSUS_RECORD_INVALID", "private transport error")
peer = dict(payload)
if stage == "PREFLIGHTED":
root_stat, attempt_stat = config.for_rank(1).staging_path.stat(), peer_attempt.stat()
peer.update(attestation_digest=contract_digest(records[1]),
root_identity=[root_stat.st_dev, root_stat.st_ino],
attempt_identity=[attempt_stat.st_dev, attempt_stat.st_ino])
if state.corrupt:
state.corrupt(peer)
return ({"payload": payload}, {"payload": peer})
@contextmanager
def group(*_):
state.events.append("group-open")
try:
yield
finally:
state.events.append("group-closed")
if state.fail_at == "teardown":
raise RuntimeContractError("LMS_CLEANUP_INCOMPLETE", "private teardown error")
monkeypatch.setattr(preflight, "gloo_all_gather_records", gather)
monkeypatch.setattr(local, "exchange_summary", exchange)
monkeypatch.setattr(preflight, "control_group", group)
return state
def execute(world):
return preflight.execute_preflight(world.config, world.launch, probes=world.probes)
def test_rank_local_terminal_success_binds_owned_attempt_and_waits_for_teardown(rank_world):
result = execute(rank_world)
config = rank_world.config
assert result.result == "preflighted"
assert result.evidence_tier == "protocol_cpu"
assert read_evidence(config.evidence_path) == result
assert rank_world.events == ["group-open", "PREFLIGHTED", "PREPARED", "group-closed"]
attempt = config.evidence_path.parent
prepared = json.loads((attempt / "prepared-summary.json").read_text())
assert prepared == {"rank": 0, "run_id": config.run_id,
"identity_digest": result.identity_digest, "result": "preflighted"}
assert not (attempt / "quarantined-summary.json").exists()
@pytest.mark.parametrize("field,value", [
("attestation_digest", "f" * 64), ("mapping_digest", "f" * 64),
("root_identity", [True, 1]), ("root_identity", [-1, 1]),
("root_identity", [1]), ("root_identity", "1,2"),
("attempt_identity", [1, False]), ("attempt_identity", [1, -1]),
("attempt_identity", [1, 2, 3]), ("attempt_identity", "1,2"),
("unrecognized", "extra"),
])
def test_corrupt_peer_receipt_cannot_publish_preflight(rank_world, field, value):
rank_world.corrupt = lambda payload: payload.update({field: value})
with pytest.raises(RuntimeContractError, match="peer termination is unknown") as failure:
execute(rank_world)
assert failure.value.code == "LMS_CONSENSUS_RECORD_INVALID"
assert rank_world.events == ["group-open", "PREFLIGHTED", "group-closed"]
assert not rank_world.config.evidence_path.exists()
assert not list(rank_world.config.staging_path.rglob("prepared-summary.json"))
@pytest.mark.parametrize("missing", ["root_identity", "attempt_identity", "attestation_digest"])
def test_missing_receipt_fields_are_not_inferred(rank_world, missing):
rank_world.corrupt = lambda payload: payload.pop(missing)
with pytest.raises(RuntimeContractError) as failure:
execute(rank_world)
assert failure.value.code == "LMS_CONSENSUS_RECORD_INVALID"
assert not rank_world.config.evidence_path.exists()
@pytest.mark.parametrize("boundary", ["wire", "malformed", "closed-parser", "lifecycle"])
def test_attestation_and_lifecycle_failures_cannot_become_terminal_success(rank_world, monkeypatch, boundary):
gather = preflight.gloo_all_gather_records
def fail(record):
if boundary == "lifecycle" and getattr(record, "sequence", None) == 1:
raise ContractError("private lifecycle transport error")
if isinstance(record, preflight.RankAttestation):
if boundary == "wire":
raise ContractError("private malformed transport record")
if boundary == "malformed":
return (b'{"unexpected":true}',) * 2
return gather(record)
if boundary == "closed-parser":
def unavailable(payload):
raise RuntimeContractError("LMS_CONSENSUS_RECORD_INVALID", "typed parser failure")
monkeypatch.setattr(preflight.RankAttestation, "from_bytes", staticmethod(unavailable))
monkeypatch.setattr(preflight, "gloo_all_gather_records", fail)
with pytest.raises(RuntimeContractError) as failure:
execute(rank_world)
assert failure.value.code == "LMS_CONSENSUS_RECORD_INVALID"
assert "private" not in str(failure.value)
assert not rank_world.config.evidence_path.exists()
assert "PREPARED" not in rank_world.events
def test_rank_mapping_must_match_even_with_live_control_group(rank_world):
rank_world.launch = replace(rank_world.launch, group_rank=1)
with pytest.raises(RuntimeContractError) as failure:
execute(rank_world)
assert failure.value.code == "LMS_TOPOLOGY_UNSUPPORTED"
assert not rank_world.config.evidence_path.parent.exists()
@pytest.mark.parametrize("replace_path", ["root", "attempt"])
def test_replaced_owned_directory_never_receives_terminal_evidence(rank_world, replace_path):
collect = rank_world.probes.collect
target = (rank_world.config.staging_path if replace_path == "root"
else rank_world.config.evidence_path.parent)
def replace_during_observation(*args):
snapshot = collect(*args)
target.rename(target.with_name("preserved-original"))
target.mkdir(mode=0o700)
(target / "foreign").write_text("preserve")
return snapshot
rank_world.probes.collect = replace_during_observation
with pytest.raises(RuntimeContractError) as failure:
execute(rank_world)
assert failure.value.code == "LMS_STORAGE_PROFILE_MISMATCH"
assert (target / "foreign").read_text() == "preserve"
assert not rank_world.config.evidence_path.exists()
def test_same_host_cannot_supply_two_distinct_host_groups(rank_world):
records = (rank_world.records[0], replace(rank_world.records[1], host_digest=rank_world.records[0].host_digest))
with pytest.raises(RuntimeContractError, match="local ranks must be unique within each host") as failure:
preflight._validate_preflight_attestations(rank_world.config, rank_world.launch, records)
assert failure.value.code == "LMS_RANK_DEVICE_CONFLICT"
@pytest.mark.parametrize("available,initialized,rank,size,backend", [
(False, False, 0, 2, "gloo"), (True, False, 0, 2, "gloo"),
(True, True, 1, 2, "gloo"), (True, True, 0, 1, "gloo"),
(True, True, 0, 2, "nccl"),
])
def test_missing_or_wrong_process_group_cannot_create_local_attempt(
rank_world, monkeypatch, available, initialized, rank, size, backend,
):
for name, value in {"is_available": available, "is_initialized": initialized,
"get_rank": rank, "get_world_size": size, "get_backend": backend}.items():
monkeypatch.setattr(preflight.dist, name, lambda value=value: value)
with pytest.raises(RuntimeContractError) as failure:
execute(rank_world)
assert failure.value.code == "LMS_LOAD_PROFILE_MISMATCH"
assert rank_world.events == ["group-open", "group-closed"]
assert not rank_world.config.evidence_path.parent.exists()
@pytest.mark.parametrize("failure_at,code", [
("PREPARED", "LMS_CONSENSUS_RECORD_INVALID"), ("teardown", "LMS_CLEANUP_INCOMPLETE"),
])
@pytest.mark.parametrize("quarantine_write_fails", [False, True])
def test_late_failure_never_publishes_success_or_claims_peer_teardown(
rank_world, monkeypatch, failure_at, code, quarantine_write_fails,
):
rank_world.fail_at = failure_at
writer = local.write_owned_json
def write(path, payload):
if quarantine_write_fails and path.name == "quarantined-summary.json":
raise OSError("private quarantine failure")
return writer(path, payload)
monkeypatch.setattr(local, "write_owned_json", write)
with pytest.raises(RuntimeContractError, match="peer termination is unknown") as failure:
execute(rank_world)
assert failure.value.code == code
assert "private" not in str(failure.value)
assert rank_world.events[-1] == "group-closed"
assert not rank_world.config.evidence_path.exists()
quarantine = rank_world.config.evidence_path.parent / "quarantined-summary.json"
if quarantine_write_fails:
assert not quarantine.exists()
else:
assert json.loads(quarantine.read_text()) == {
"rank": 0, "run_id": rank_world.config.run_id,
"peer_termination": "unknown", "result": "quarantined",
}