"""Single-process unit tests for distributed preflight validation and failure paths.""" from __future__ import annotations import os import signal import threading from contextlib import contextmanager from pathlib import Path from types import SimpleNamespace import pytest import obliteratus.distributed.preflight as preflight from obliteratus.checkpoint_errors import CheckpointContractError from obliteratus.distributed.contracts import ( ContractError, RuntimeContractError, RuntimeStage, StageMessage, Vote, contract_digest, ) from obliteratus.distributed.evidence import PreflightEvidence, write_stage_message from obliteratus.distributed.preflight import ( SourceIdentity, SourceMountAttestation, verify_source_mount_attestation, ) from tests.test_distributed_preflight import _attestation, _validate RUN_ID = "1" * 32 IDENTITY = "2" * 64 @pytest.fixture(autouse=True) def _local_votes(monkeypatch): monkeypatch.setattr(preflight, "unanimous_vote", lambda _sequence, accepted: accepted) monkeypatch.setattr( preflight, "gather_failure_codes", lambda _sequence, code: (code,) if code else (), ) def _digest(char: str) -> str: return char * 64 def test_source_inventory_digest_streams_large_bounded_records(): records = [ {"path": f"shard-{index:05d}.safetensors", "size_bytes": index + 1, "sha256": "a" * 64} for index in range(1024) ] with pytest.raises(ContractError): contract_digest(records) digest = preflight._source_records_digest(records) assert len(digest) == 64 assert digest == preflight._source_records_digest(list(records)) assert digest != preflight._source_records_digest(list(reversed(records))) assert digest != preflight._source_records_digest(records[:-1]) changed = [*records[:-1], {**records[-1], "sha256": "b" * 64}] assert digest != preflight._source_records_digest(changed) def test_source_inventory_digest_preserves_small_identities_and_record_bounds(): records = [{"path": "model.safetensors", "size_bytes": 1, "sha256": "a" * 64}] assert preflight._source_records_digest(records) == contract_digest(records) with pytest.raises(ContractError): preflight._source_records_digest([{**records[0], "path": "x" * 65536}]) class _FakeOs: """Delegate to ``os`` while overriding selected functions for one module.""" def __init__(self, **overrides): self._overrides = overrides def __getattr__(self, name): if name in self._overrides: return self._overrides[name] return getattr(os, name) def _mount_values(**overrides): values = { "schema_version": 1, "evidence_tier": "candidate_local_gpu", "mechanism": "read_only_content_addressed", "source_digest": _digest("a"), "mount_digest": _digest("b"), "content_root_digest": _digest("a"), "external_evidence_digest": _digest("c"), "read_only": True, "content_addressed": True, "production_accepted": False, } values.update(overrides) return values # --- SourceMountAttestation ------------------------------------------------- @pytest.mark.parametrize( ("overrides", "message"), ( ({"schema_version": 2}, "schema_version must be 1"), ({"production_accepted": True}, "cannot accept a production profile"), ( { "evidence_tier": "protocol_cpu", "mechanism": "protocol_test_injected", "read_only": True, "content_addressed": False, }, "explicit noncoverage", ), ({"evidence_tier": "production"}, "tier is invalid"), ), ) def test_source_mount_attestation_rejects_unsupported_claims(overrides, message): with pytest.raises(ContractError, match=message): SourceMountAttestation(**_mount_values(**overrides)) def test_source_mount_attestation_accepts_protocol_noncoverage_record(): record = SourceMountAttestation( **_mount_values( evidence_tier="protocol_cpu", mechanism="protocol_test_injected", read_only=False, content_addressed=False, ) ) assert len(record.digest) == 64 def _verify_context(device_kind="cuda", source_digest=None): source = SourceIdentity(_digest("a"), _digest("d"), _digest("e"), 1, 1) result = SimpleNamespace(source_identity=source) config = SimpleNamespace( source_digest=source_digest or _digest("a"), device_kind=device_kind, source_path=Path("/nonexistent-source"), ) return config, result def test_verify_source_mount_attestation_requires_a_typed_record(): config, result = _verify_context() with pytest.raises(RuntimeContractError, match="unavailable") as error: verify_source_mount_attestation(config, result, _mount_values()) assert error.value.code == "LMS_MODEL_ARTIFACT_UNSUPPORTED" def test_verify_source_mount_attestation_rejects_another_source(): config, result = _verify_context(source_digest=_digest("f")) with pytest.raises(RuntimeContractError, match="does not bind") as error: verify_source_mount_attestation(config, result, SourceMountAttestation(**_mount_values())) assert error.value.code == "LMS_SOURCE_CHANGED" def test_verify_source_mount_attestation_protocol_tier_only_authorizes_cpu(): config, result = _verify_context(device_kind="cpu") protocol = SourceMountAttestation( **_mount_values( evidence_tier="protocol_cpu", mechanism="protocol_test_injected", read_only=False, content_addressed=False, ) ) assert verify_source_mount_attestation(config, result, protocol) == protocol.digest def test_verify_source_mount_attestation_gpu_tier_refuses_cpu_device(monkeypatch): config, result = _verify_context(device_kind="cpu") monkeypatch.setattr( preflight, "source_mount_observation", lambda _path: pytest.fail("cpu device must refuse before mount observation"), ) with pytest.raises(RuntimeContractError, match="tier is inconsistent") as error: verify_source_mount_attestation(config, result, SourceMountAttestation(**_mount_values())) assert error.value.code == "LMS_MODEL_ARTIFACT_UNSUPPORTED" # --- _stage_deadline -------------------------------------------------------- def test_stage_deadline_refuses_non_main_threads(): errors = [] def worker(): try: with preflight._stage_deadline(1, "unused"): pass except RuntimeContractError as error: errors.append(error.code) thread = threading.Thread(target=worker) thread.start() thread.join() assert errors == ["LMS_RUNTIME_PROFILE_MISMATCH"] def test_stage_deadline_refuses_a_foreign_active_timer(): previous = signal.signal(signal.SIGALRM, lambda *_args: None) signal.setitimer(signal.ITIMER_REAL, 30) try: with pytest.raises(RuntimeContractError, match="conflicts with an active") as error: with preflight._stage_deadline(1, "unused"): pass assert error.value.code == "LMS_RUNTIME_PROFILE_MISMATCH" finally: signal.setitimer(signal.ITIMER_REAL, 0) signal.signal(signal.SIGALRM, previous) def test_nested_stage_deadline_restores_the_outer_timer(): with preflight._stage_deadline(30, "outer"): with preflight._stage_deadline(5, "inner"): inner_remaining = signal.getitimer(signal.ITIMER_REAL)[0] outer_remaining = signal.getitimer(signal.ITIMER_REAL)[0] assert 0 < inner_remaining <= 5 assert 5 < outer_remaining <= 30 assert signal.getitimer(signal.ITIMER_REAL)[0] == 0 assert preflight._ACTIVE_DEADLINE_DEPTH == 0 # --- source inspection ------------------------------------------------------ def _immutable_source(tmp_path: Path, files: dict[str, bytes], name: str = "source") -> Path: source = tmp_path / name source.mkdir() for relative, payload in files.items(): path = source / relative path.write_bytes(payload) path.chmod(0o444) source.chmod(0o555) return source def test_inspect_source_normalizes_plain_contract_errors(tmp_path, monkeypatch): def broken(*_args, **_kwargs): raise ContractError("private inspector detail") monkeypatch.setattr(preflight, "_inspect_source", broken) with pytest.raises(RuntimeContractError, match="bounded immutable input") as error: preflight.inspect_source(tmp_path) assert error.value.code == "LMS_SOURCE_BOUNDARY_VIOLATION" assert "private" not in str(error.value) def _inspect(path, **overrides): values = { "max_files": 10, "max_total_bytes": 1 << 20, "max_file_bytes": 1 << 20, "timeout_seconds": 60, } values.update(overrides) return preflight._inspect_source(path, **values) def test_inspect_source_rejects_mutable_nested_directories(tmp_path): source = tmp_path / "source" nested = source / "nested" nested.mkdir(parents=True) (nested / "model.safetensors").write_bytes(b"x") source.chmod(0o555) try: with pytest.raises(RuntimeContractError, match="every source directory") as error: _inspect(source) assert error.value.code == "LMS_SOURCE_BOUNDARY_VIOLATION" finally: source.chmod(0o755) def test_inspect_source_rejects_non_regular_files(tmp_path): source = tmp_path / "source" source.mkdir() os.mkfifo(source / "pipe.json") source.chmod(0o555) try: with pytest.raises(RuntimeContractError, match="only regular files") as error: _inspect(source) assert error.value.code == "LMS_SOURCE_BOUNDARY_VIOLATION" finally: source.chmod(0o755) class _Report: def __init__(self, files, *, total_bytes=None, primary="hf_safetensors", decision="canonical_hf_ready", formats=("hf_safetensors",)): self.primary_format = primary self.support_decision = decision self._files = files self._total = ( sum(item["size_bytes"] for item in files) if total_bytes is None else total_bytes ) self._formats = formats def to_dict(self): return { "components": [{"format": item} for item in self._formats], "source_inventory": {"files": self._files, "total_bytes": self._total}, } def _inventory(source: Path): return [ {"relative_path": path.name, "size_bytes": path.stat().st_size, "sha256": _digest("0")} for path in sorted(source.iterdir()) ] @pytest.mark.parametrize( ("code", "detail", "expected"), ( ("DCI_SOURCE_CHANGED", "changed", "LMS_SOURCE_CHANGED"), ("DCI_RESOURCE_LIMIT", "deadline", "LMS_STAGE_TIMEOUT"), ("DCI_RESOURCE_LIMIT", "max_files", "LMS_SOURCE_BOUNDARY_VIOLATION"), ), ) def test_inspect_source_maps_structural_inspector_refusals( tmp_path, monkeypatch, code, detail, expected, ): source = _immutable_source(tmp_path, {"model.safetensors": b"x", "tokenizer.json": b"{}"}) def refuse(*_args, **_kwargs): raise CheckpointContractError(code, detail=detail) monkeypatch.setattr(preflight, "inspect_checkpoint", refuse) with pytest.raises(RuntimeContractError) as error: _inspect(source) assert error.value.code == expected def test_inspect_source_rejects_inspection_finishing_after_the_deadline(tmp_path, monkeypatch): source = _immutable_source(tmp_path, {"model.safetensors": b"x", "tokenizer.json": b"{}"}) clock = iter((0.0, 0.0, 120.0)) monkeypatch.setattr(preflight.time, "monotonic", lambda: next(clock)) monkeypatch.setattr( preflight, "inspect_checkpoint", lambda *_a, **_k: _Report(_inventory(source)) ) with pytest.raises(RuntimeContractError, match="explicit timeout") as error: _inspect(source) assert error.value.code == "LMS_STAGE_TIMEOUT" @pytest.mark.parametrize( "report_kwargs", ( {"primary": "gguf"}, {"decision": "needs_conversion"}, {"formats": ("hf_safetensors", "peft_adapter")}, ), ) def test_inspect_source_requires_canonical_hf_safetensors(tmp_path, monkeypatch, report_kwargs): source = _immutable_source(tmp_path, {"model.safetensors": b"x", "tokenizer.json": b"{}"}) monkeypatch.setattr( preflight, "inspect_checkpoint", lambda *_a, **_k: _Report(_inventory(source), **report_kwargs), ) with pytest.raises(RuntimeContractError, match="canonical Hugging Face") as error: _inspect(source) assert error.value.code == "LMS_SOURCE_BOUNDARY_VIOLATION" @pytest.mark.parametrize("mismatch", ("count", "bytes")) def test_inspect_source_detects_inventory_drift(tmp_path, monkeypatch, mismatch): source = _immutable_source(tmp_path, {"model.safetensors": b"x", "tokenizer.json": b"{}"}) inventory = _inventory(source) report = ( _Report(inventory[:1]) if mismatch == "count" else _Report(inventory, total_bytes=999) ) monkeypatch.setattr(preflight, "inspect_checkpoint", lambda *_a, **_k: report) with pytest.raises(RuntimeContractError, match="changed during") as error: _inspect(source) assert error.value.code == "LMS_SOURCE_CHANGED" @pytest.mark.parametrize( ("files", "message"), ( ({"config.json": b"{}", "tokenizer.json": b"{}"}, "at least one safetensors"), ({"model.safetensors": b"x", "config.json": b"{}"}, "tokenizer metadata"), ), ) def test_inspect_source_requires_model_and_tokenizer_records( tmp_path, monkeypatch, files, message, ): source = _immutable_source(tmp_path, files) monkeypatch.setattr( preflight, "inspect_checkpoint", lambda *_a, **_k: _Report(_inventory(source)) ) with pytest.raises(RuntimeContractError, match=message) as error: _inspect(source) assert error.value.code == "LMS_SOURCE_BOUNDARY_VIOLATION" # --- checkout identity ------------------------------------------------------ def test_bounded_text_file_rejects_short_reads_and_invalid_utf8(tmp_path, monkeypatch): text = tmp_path / "HEAD" text.write_bytes(b"\xff\xfe\xfd") with pytest.raises(ContractError, match="unavailable"): preflight._bounded_text_file(text) text.write_bytes(b"abcdef") monkeypatch.setattr( preflight, "os", _FakeOs(read=lambda descriptor, _size: os.read(descriptor, 2)) ) with pytest.raises(ContractError, match="changed while it was read"): preflight._bounded_text_file(text) def test_checkout_commit_rejects_a_reference_with_a_malformed_commit(tmp_path): git_dir = tmp_path / ".git" (git_dir / "refs" / "heads").mkdir(parents=True) (git_dir / "HEAD").write_text("ref: refs/heads/main\n", encoding="utf-8") (git_dir / "refs" / "heads" / "main").write_text("not-a-commit\n", encoding="utf-8") with pytest.raises(ContractError, match="commit has an invalid format"): preflight.checkout_commit(tmp_path) def test_checkout_code_digest_enforces_its_byte_bound(tmp_path, monkeypatch): package = tmp_path / "obliteratus" package.mkdir() (package / "a.py").write_text("x = 1\n", encoding="utf-8") monkeypatch.setattr( preflight, "_hash_file", lambda *_a, **_k: ((64 << 20) + 1, _digest("0")) ) with pytest.raises(RuntimeContractError, match="byte bound") as error: preflight.checkout_code_digest(tmp_path) assert error.value.code == "LMS_RESOURCE_ADMISSION_DENIED" # --- mount identity --------------------------------------------------------- def _mountinfo(*lines: str): payload = ("\n".join(lines) + "\n").encode() def read(_descriptor, size): nonlocal payload chunk, payload = payload[:size], payload[size:] return chunk return read def test_storage_mount_digest_refuses_symlinked_staging(tmp_path): target = tmp_path / "target" target.mkdir() link = tmp_path / "link" link.symlink_to(target, target_is_directory=True) with pytest.raises(RuntimeContractError, match="non-symlink") as error: preflight.storage_mount_digest(link) assert error.value.code == "LMS_STORAGE_PROFILE_MISMATCH" def test_storage_mount_digest_bounds_the_mount_inventory(tmp_path, monkeypatch): monkeypatch.setattr( preflight, "os", _FakeOs(read=lambda _d, size: b"x" * size) ) with pytest.raises(RuntimeContractError, match="byte bound") as error: preflight.storage_mount_digest(tmp_path) assert error.value.code == "LMS_RESOURCE_ADMISSION_DENIED" def test_storage_mount_digest_skips_malformed_lines_and_requires_a_mount(tmp_path, monkeypatch): monkeypatch.setattr( preflight, "os", _FakeOs(read=_mountinfo( "no separator here", "1 2 3:4 / - ext4", "1 2 3:4 / /unrelated-mount rw - ext4 /dev/x rw", )), ) with pytest.raises(RuntimeContractError, match="mount identity is unavailable") as error: preflight.storage_mount_digest(tmp_path) assert error.value.code == "LMS_STORAGE_PROFILE_MISMATCH" def test_source_mount_observation_reads_the_real_mount_table(tmp_path): digest, read_only, exact_root = preflight.source_mount_observation(tmp_path) assert len(digest) == 64 assert isinstance(read_only, bool) assert exact_root is False assert preflight.source_mount_observation(tmp_path)[0] == digest def test_source_mount_observation_selects_the_deepest_exact_read_only_mount( tmp_path, monkeypatch, ): escaped = str(tmp_path).replace(" ", "\\040") monkeypatch.setattr( preflight, "os", _FakeOs(read=_mountinfo( "garbage", "1 0 8:1 / / rw shared:1 - ext4 /dev/root rw", "short 1 - ext4", f"7 1 8:2 /data {escaped} ro,nosuid - squashfs /dev/loop0 ro", "9 1 8:3 / /elsewhere rw - tmpfs tmpfs rw", )), ) digest, read_only, exact_root = preflight.source_mount_observation(tmp_path) assert read_only is True and exact_root is True monkeypatch.setattr( preflight, "os", _FakeOs(read=_mountinfo("1 0 8:1 / / rw shared:1 - ext4 /dev/root rw")), ) root_digest, root_read_only, root_exact = preflight.source_mount_observation(tmp_path) assert root_digest != digest assert root_read_only is False and root_exact is False def test_source_mount_observation_failure_paths(tmp_path, monkeypatch): target = tmp_path / "target" target.mkdir() link = tmp_path / "link" link.symlink_to(target, target_is_directory=True) with pytest.raises(RuntimeContractError, match="exact local non-symlink") as error: preflight.source_mount_observation(link) assert error.value.code == "LMS_SOURCE_BOUNDARY_VIOLATION" with pytest.raises(RuntimeContractError, match="mount identity is unavailable"): preflight.source_mount_observation(tmp_path / "missing") monkeypatch.setattr(preflight, "os", _FakeOs(read=lambda _d, size: b"x" * size)) with pytest.raises(RuntimeContractError, match="byte bound") as error: preflight.source_mount_observation(target) assert error.value.code == "LMS_RESOURCE_ADMISSION_DENIED" monkeypatch.setattr(preflight, "os", _FakeOs(read=_mountinfo("1 0 8:1 / /other rw - x y z"))) with pytest.raises(RuntimeContractError, match="mount identity is unavailable"): preflight.source_mount_observation(target) # --- system probes ---------------------------------------------------------- def test_system_probes_propagate_runtime_checkout_refusals(tmp_path, monkeypatch): source = SourceIdentity("1" * 64, "2" * 64, "3" * 64, 2, 128) monkeypatch.setattr(preflight, "inspect_source", lambda *_args, **_kwargs: source) monkeypatch.setattr(preflight.shutil, "disk_usage", lambda _path: SimpleNamespace(free=4096)) monkeypatch.setattr(preflight, "validate_network_interface", lambda *_a, **_k: None) monkeypatch.setattr(preflight, "_host_memory", lambda: (8192, 4096)) monkeypatch.setattr(preflight, "runtime_software_versions", lambda: (("python", "3"),)) monkeypatch.setattr(preflight, "checkout_commit", lambda: "b" * 40) def refuse(): raise RuntimeContractError("LMS_RESOURCE_ADMISSION_DENIED", "too much source") monkeypatch.setattr(preflight, "checkout_code_digest", refuse) config = SimpleNamespace( source_path=tmp_path, max_source_files=1, max_source_bytes=1, max_source_file_bytes=1, source_timeout_seconds=1, staging_path=tmp_path, network_interface="lo", allowed_master_cidrs=(), master_addr="127.0.0.1", coordinator_rank=0, device_kind="cpu", ) with pytest.raises(RuntimeContractError) as error: preflight.SystemProbes().collect(config, SimpleNamespace(rank=0, local_rank=0)) assert error.value.code == "LMS_RESOURCE_ADMISSION_DENIED" # --- attempt directory and shared staging probe ------------------------------ def _staging_config(tmp_path: Path, **overrides): values = { "staging_path": tmp_path, "run_id": RUN_ID, "coordinator_rank": 0, "rendezvous_id": "3" * 32, "world_size": 2, } values.update(overrides) return SimpleNamespace(**values) def test_prepare_attempt_directory_refuses_reuse(tmp_path, monkeypatch): monkeypatch.setattr(preflight.dist, "barrier", lambda: None) (tmp_path / RUN_ID).mkdir() with pytest.raises(RuntimeContractError, match="cannot be reused") as error: preflight._prepare_attempt_directory(_staging_config(tmp_path), SimpleNamespace(rank=0)) assert error.value.code == "LMS_ATTEMPT_IDENTITY_CONFLICT" def test_prepare_attempt_directory_requires_a_private_directory(tmp_path, monkeypatch): monkeypatch.setattr(preflight.dist, "barrier", lambda: None) (tmp_path / RUN_ID).mkdir(mode=0o755) (tmp_path / RUN_ID).chmod(0o755) with pytest.raises(RuntimeContractError, match="not private") as error: preflight._prepare_attempt_directory(_staging_config(tmp_path), SimpleNamespace(rank=1)) assert error.value.code == "LMS_STORAGE_PROFILE_MISMATCH" def test_prepare_attempt_directory_maps_missing_staging(tmp_path, monkeypatch): monkeypatch.setattr(preflight.dist, "barrier", lambda: None) config = _staging_config(tmp_path / "missing") with pytest.raises(RuntimeContractError, match="preflight preparation failed") as error: preflight._prepare_attempt_directory(config, SimpleNamespace(rank=0)) assert error.value.code == "LMS_STORAGE_PROFILE_MISMATCH" def test_prepare_attempt_directory_succeeds_for_coordinator_and_peer(tmp_path, monkeypatch): votes = [] monkeypatch.setattr( preflight, "gather_failure_codes", lambda sequence, code: votes.append((sequence, code is None)) or ((code,) if code else ()), ) config = _staging_config(tmp_path) attempt = preflight._prepare_attempt_directory(config, SimpleNamespace(rank=0)) assert attempt == tmp_path / RUN_ID and attempt.is_dir() assert preflight._prepare_attempt_directory(config, SimpleNamespace(rank=1)) == attempt assert votes == [(10, True), (11, True), (28, True)] * 2 def test_shared_staging_probe_requires_a_visible_attempt(tmp_path, monkeypatch): monkeypatch.setattr(preflight.dist, "barrier", lambda: None) with pytest.raises(RuntimeContractError, match="not visible to every rank") as error: preflight._probe_shared_staging(_staging_config(tmp_path), SimpleNamespace(rank=1)) assert error.value.code == "LMS_STORAGE_PROFILE_MISMATCH" def test_shared_staging_probe_round_trips_and_cleans_up(tmp_path, monkeypatch): monkeypatch.setattr(preflight.dist, "barrier", lambda: None) (tmp_path / RUN_ID).mkdir(mode=0o700) config = _staging_config(tmp_path) assert preflight._probe_shared_staging(config, SimpleNamespace(rank=0)) == tmp_path / RUN_ID assert list((tmp_path / RUN_ID).iterdir()) == [] def test_shared_staging_probe_detects_incomplete_writes(tmp_path, monkeypatch): monkeypatch.setattr(preflight.dist, "barrier", lambda: None) (tmp_path / RUN_ID).mkdir(mode=0o700) monkeypatch.setattr(preflight, "os", _FakeOs(write=lambda _d, data: len(data) - 1)) with pytest.raises(RuntimeContractError, match="write was incomplete") as error: preflight._probe_shared_staging(_staging_config(tmp_path), SimpleNamespace(rank=0)) assert error.value.code == "LMS_STORAGE_PROFILE_MISMATCH" assert list((tmp_path / RUN_ID).iterdir()) == [] def test_shared_staging_probe_detects_foreign_nonce(tmp_path, monkeypatch): attempt = tmp_path / RUN_ID attempt.mkdir(mode=0o700) (attempt / ".shared-probe.ready").write_bytes(b"another-run") monkeypatch.setattr(preflight.dist, "barrier", lambda: None) with pytest.raises(RuntimeContractError, match="identity disagrees") as error: preflight._probe_shared_staging(_staging_config(tmp_path), SimpleNamespace(rank=1)) assert error.value.code == "LMS_STORAGE_PROFILE_MISMATCH" def test_shared_staging_probe_maps_missing_marker_to_storage_failure(tmp_path, monkeypatch): (tmp_path / RUN_ID).mkdir(mode=0o700) monkeypatch.setattr(preflight.dist, "barrier", lambda: None) with pytest.raises(RuntimeContractError, match="preflight preparation failed") as error: preflight._probe_shared_staging(_staging_config(tmp_path), SimpleNamespace(rank=1)) assert error.value.code == "LMS_STORAGE_PROFILE_MISMATCH" def test_shared_staging_probe_reports_cleanup_failures(tmp_path, monkeypatch): (tmp_path / RUN_ID).mkdir(mode=0o700) monkeypatch.setattr(preflight.dist, "barrier", lambda: None) calls = {"fsync": 0} def unlink(name, *, dir_fd): if name == ".shared-probe.pending": raise PermissionError("denied") return os.unlink(name, dir_fd=dir_fd) def fsync(descriptor): calls["fsync"] += 1 if calls["fsync"] > 3: raise OSError("fsync failed") return os.fsync(descriptor) monkeypatch.setattr(preflight, "os", _FakeOs(unlink=unlink, fsync=fsync)) with pytest.raises(RuntimeContractError, match="cleanup failed") as error: preflight._probe_shared_staging(_staging_config(tmp_path), SimpleNamespace(rank=0)) assert error.value.code == "LMS_CLEANUP_INCOMPLETE" # --- lifecycle, consensus and failure codes --------------------------------- def test_begin_lifecycle_maps_identity_disagreement(monkeypatch): def disagree(_record): raise ContractError("digests differ") monkeypatch.setattr(preflight, "require_record_consensus", disagree) monkeypatch.setattr( preflight, "_run_identity", lambda _config: {"run_id": RUN_ID}, ) with pytest.raises(RuntimeContractError, match="identities disagree") as error: preflight._begin_lifecycle(SimpleNamespace(run_id=RUN_ID), SimpleNamespace(rank=0)) assert error.value.code == "LMS_IDENTITY_MISMATCH" def _run_preflight_config(**overrides): values = { "validate": lambda: None, "source_timeout_seconds": 5, "collective_timeout_seconds": 5, "run_id": RUN_ID, "world_size": 2, "coordinator_rank": 0, "placement_plan_digest": _digest("c"), } values.update(overrides) return SimpleNamespace(**values) def test_run_preflight_maps_profile_validation_errors(): def invalid(): raise ContractError("profile detail") with pytest.raises(RuntimeContractError, match="profile validation failed") as error: preflight.run_preflight(_run_preflight_config(validate=invalid), SimpleNamespace()) assert error.value.code == "LMS_RUNTIME_PROFILE_MISMATCH" def _stub_run_preflight(monkeypatch, tmp_path, *, gather=None, probe=None, vote=True): lifecycle = ({"run_id": RUN_ID}, IDENTITY, ()) monkeypatch.setattr(preflight, "_require_live_staging_layout", lambda *_a: None) monkeypatch.setattr(preflight, "_prepare_attempt_directory", lambda *_a: tmp_path) monkeypatch.setattr(preflight, "_attestation", lambda *_a: _attestation(0)) monkeypatch.setattr( preflight, "gloo_all_gather_records", gather or (lambda record: (record.to_bytes() if hasattr(record, "to_bytes") else b"",)), ) monkeypatch.setattr( preflight.RankAttestation, "from_bytes", classmethod(lambda cls, _b: _attestation(0)), ) monkeypatch.setattr(preflight, "validate_attestations", lambda *_a, **_k: None) monkeypatch.setattr(preflight, "_probe_shared_staging", probe or (lambda *_a: tmp_path)) monkeypatch.setattr( preflight, "unanimous_vote", lambda sequence, accepted: vote if sequence == 0 else accepted, ) monkeypatch.setattr(preflight, "_opaque_identity", lambda *_a: _digest("d")) snapshot = SimpleNamespace(source=SourceIdentity("1" * 64, "2" * 64, "3" * 64, 1, 1)) probes = SimpleNamespace(collect=lambda *_a: snapshot) config = _run_preflight_config( tensor_parallel_size=2, dimension_divisors=(2,), device_kind="cpu", device_name="cpu", compute_capability="none", software_digest=_digest("5"), source_digest=_digest("7"), model_digest=_digest("8"), tokenizer_digest=_digest("9"), digest=_digest("a"), commit_sha="b" * 40, code_digest=_digest("f"), storage_digest=_digest("6"), network_interface="lo", local_world_size=1, min_free_device_memory_bytes=1, min_free_host_memory_bytes=1, min_free_staging_bytes=1, ) launch = SimpleNamespace(rank=0, rendezvous_id="3" * 32) return config, launch, probes, lifecycle def test_run_preflight_propagates_runtime_gather_failures(monkeypatch, tmp_path): def gather(_record): raise RuntimeContractError("LMS_COLLECTIVE_FAILED", "peer vanished") config, launch, probes, lifecycle = _stub_run_preflight(monkeypatch, tmp_path, gather=gather) with pytest.raises(RuntimeContractError) as error: preflight.run_preflight(config, launch, probes=probes, lifecycle=lifecycle) assert error.value.code == "LMS_COLLECTIVE_FAILED" def test_run_preflight_rejects_a_changed_shared_attempt(monkeypatch, tmp_path): config, launch, probes, lifecycle = _stub_run_preflight( monkeypatch, tmp_path, probe=lambda *_a: tmp_path / "other", ) with pytest.raises(RuntimeContractError, match="attempt identity changed") as error: preflight.run_preflight(config, launch, probes=probes, lifecycle=lifecycle) assert error.value.code == "LMS_STORAGE_PROFILE_MISMATCH" def test_run_preflight_requires_a_unanimous_admission_vote(monkeypatch, tmp_path): config, launch, probes, lifecycle = _stub_run_preflight(monkeypatch, tmp_path, vote=False) with pytest.raises(ContractError, match="not unanimous"): preflight.run_preflight(config, launch, probes=probes, lifecycle=lifecycle) def test_run_preflight_returns_a_bound_capability(monkeypatch, tmp_path): config, launch, probes, lifecycle = _stub_run_preflight(monkeypatch, tmp_path) result = preflight.run_preflight(config, launch, probes=probes, lifecycle=lifecycle) assert result.attempt_path == tmp_path assert result.lifecycle_identity_digest == IDENTITY assert result.topology.world_size == 2 assert len(result.identity_digest) == 64 def test_run_live_preflight_requires_the_exact_gloo_group(monkeypatch): monkeypatch.setattr(preflight.dist, "is_available", lambda: True) monkeypatch.setattr(preflight.dist, "is_initialized", lambda: True) monkeypatch.setattr(preflight.dist, "get_backend", lambda: "nccl") monkeypatch.setattr( preflight, "run_preflight", lambda *_a, **_k: pytest.fail("must refuse first"), ) with pytest.raises(RuntimeContractError, match="exact Gloo control group") as error: preflight.run_live_preflight(SimpleNamespace(), SimpleNamespace(rank=0, world_size=2)) assert error.value.code == "LMS_LOAD_PROFILE_MISMATCH" def test_run_live_preflight_maps_lifecycle_disagreement(monkeypatch): votes = [] messages = [] monkeypatch.setattr( preflight, "gather_failure_codes", lambda sequence, code: votes.append((sequence, code is None)) or ((code,) if code else ()), ) 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, "run_preflight", lambda *_a, **_k: SimpleNamespace(lifecycle_identity_digest=IDENTITY), ) monkeypatch.setattr(preflight, "gloo_all_gather_records", lambda message: (message.to_bytes(),)) def disagree(*_args, **_kwargs): messages.extend(_args[0]) raise ContractError("private lifecycle detail") monkeypatch.setattr(preflight, "_validate_stage_messages", disagree) config = SimpleNamespace(run_id=RUN_ID, world_size=2) with pytest.raises(RuntimeContractError) as error: preflight.run_live_preflight(config, SimpleNamespace(rank=0, world_size=2)) assert error.value.code == "LMS_CONSENSUS_RECORD_INVALID" assert str(error.value) == "preflight preparation failed" assert votes == [(22, True), (24, False)] assert len(messages) == 1 assert messages[0].stage is RuntimeStage.PREFLIGHTED assert messages[0].identity_digest == IDENTITY @pytest.mark.parametrize( ("error", "code"), ( (RuntimeContractError("LMS_SOURCE_CHANGED", "x"), "LMS_SOURCE_CHANGED"), (KeyboardInterrupt(), "LMS_ATTEMPT_CANCELLED"), (TimeoutError(), "LMS_STAGE_TIMEOUT"), (ContractError("x"), "LMS_LIFECYCLE_INVALID"), (ValueError("x"), "LMS_COLLECTIVE_FAILED"), ), ) def test_failure_code_classification(error, code): assert preflight._failure_code(error) == code @pytest.mark.parametrize("local_failure", [False, True]) @pytest.mark.parametrize(("failure", "code"), [ (TimeoutError("private timeout"), "LMS_STAGE_TIMEOUT"), (KeyboardInterrupt(), "LMS_ATTEMPT_CANCELLED"), (RuntimeContractError("LMS_RESOURCE_ADMISSION_DENIED", "resource refused"), "LMS_RESOURCE_ADMISSION_DENIED"), (ValueError("private failure"), "LMS_RUNTIME_PROFILE_MISMATCH"), ]) def test_local_preflight_step_preserves_asymmetric_failure_code( monkeypatch, local_failure, failure, code, ): def vote(sequence, local_code): assert sequence == 16 assert local_code == (code if local_failure else None) return (code,) monkeypatch.setattr(preflight, "gather_failure_codes", vote) with pytest.raises(RuntimeContractError) as error: with preflight._local_step(16, code="LMS_RUNTIME_PROFILE_MISMATCH"): if local_failure: raise failure assert error.value.code == code assert "private" not in str(error.value) def test_local_preflight_step_arbitrates_only_actual_failures(monkeypatch): monkeypatch.setattr( preflight, "gather_failure_codes", lambda _sequence, _code: ("LMS_STAGE_TIMEOUT", "LMS_RESOURCE_ADMISSION_DENIED"), ) with pytest.raises(RuntimeContractError) as error: with preflight._local_step(16, code="LMS_RUNTIME_PROFILE_MISMATCH"): raise TimeoutError() assert error.value.code == "LMS_RESOURCE_ADMISSION_DENIED" def test_local_preflight_step_vote_failure_remains_a_stable_refusal(monkeypatch): def vote(*_args): raise RuntimeError("private transport error") monkeypatch.setattr(preflight, "gather_failure_codes", vote) with pytest.raises(RuntimeContractError) as error: with preflight._local_step(16, code="LMS_RUNTIME_PROFILE_MISMATCH"): pass assert error.value.code == "LMS_RUNTIME_PROFILE_MISMATCH" assert "private" not in str(error.value) def test_failure_code_selection_prefers_priority_and_has_a_fallback(): assert preflight._select_failure_code( {"LMS_STAGE_TIMEOUT", "LMS_CLEANUP_INCOMPLETE"} ) == "LMS_CLEANUP_INCOMPLETE" assert preflight._select_failure_code({"LMS_UNLISTED_CODE"}) == "LMS_COLLECTIVE_FAILED" assert preflight._select_failure_code(set()) == "LMS_COLLECTIVE_FAILED" def _abort(rank: int, *, code="LMS_STAGE_TIMEOUT", run_id=RUN_ID, sequence=1): return StageMessage( run_id=run_id, identity_digest=IDENTITY, rank=rank, sequence=sequence, stage=RuntimeStage.ABORTING, vote=Vote.ABORT, error_code=code, ) def _terminal(rank: int, *, code="LMS_STAGE_TIMEOUT", stage=RuntimeStage.ABORTED): return StageMessage( run_id=RUN_ID, identity_digest=IDENTITY, rank=rank, sequence=2, stage=stage, vote=Vote.ABORT, error_code=code, ) @pytest.mark.parametrize( ("records", "code"), ( ((_abort(0),), "LMS_MEMBERSHIP_INVALID"), ((_abort(0), _abort(1, run_id="9" * 32)), "LMS_LIFECYCLE_INVALID"), ), ) def test_collective_aborting_validates_peer_records(monkeypatch, records, code): monkeypatch.setattr( preflight, "gloo_all_gather_records", lambda _m: tuple(r.to_bytes() for r in records) ) config = SimpleNamespace(run_id=RUN_ID, world_size=2) with pytest.raises(RuntimeContractError) as error: preflight._collective_aborting(config, SimpleNamespace(rank=0), IDENTITY, "LMS_STAGE_TIMEOUT") assert error.value.code == code def test_collective_aborting_selects_the_highest_priority_peer_code(monkeypatch): records = (_abort(0), _abort(1, code="LMS_SOURCE_CHANGED")) monkeypatch.setattr( preflight, "gloo_all_gather_records", lambda _m: tuple(r.to_bytes() for r in records) ) config = SimpleNamespace(run_id=RUN_ID, world_size=2) code, messages = preflight._collective_aborting( config, SimpleNamespace(rank=0), IDENTITY, "LMS_STAGE_TIMEOUT", ) assert code == "LMS_SOURCE_CHANGED" assert messages == records def _write_receipts(attempt: Path, aborting, terminal): for rank, (before, after) in enumerate(zip(aborting, terminal, strict=True)): write_stage_message(attempt / f".rank-{rank}.aborting.stage.json", before) write_stage_message(attempt / f".rank-{rank}.terminal.stage.json", after) def test_read_terminal_receipts_round_trips_and_rejects_disagreement(tmp_path): config = SimpleNamespace(run_id=RUN_ID, world_size=2) good = tmp_path / "good" good.mkdir(mode=0o700) _write_receipts(good, (_abort(0), _abort(1)), (_terminal(0), _terminal(1))) aborting, terminal = preflight._read_terminal_receipts( good, config=config, identity_digest=IDENTITY, ) assert [item.rank for item in terminal] == [0, 1] swapped = tmp_path / "swapped" swapped.mkdir(mode=0o700) _write_receipts(swapped, (_abort(1), _abort(0)), (_terminal(1), _terminal(0))) with pytest.raises(RuntimeContractError, match="omit a fixed rank") as error: preflight._read_terminal_receipts(swapped, config=config, identity_digest=IDENTITY) assert error.value.code == "LMS_MEMBERSHIP_INVALID" changed = tmp_path / "changed" changed.mkdir(mode=0o700) _write_receipts( changed, (_abort(0), _abort(1)), (_terminal(0), _terminal(1, code="LMS_SOURCE_CHANGED")), ) with pytest.raises(RuntimeContractError, match="receipts disagree") as error: preflight._read_terminal_receipts(changed, config=config, identity_digest=IDENTITY) assert error.value.code == "LMS_LIFECYCLE_INVALID" def test_ensure_failure_attempt_paths(tmp_path, monkeypatch): config = SimpleNamespace(staging_path=tmp_path, run_id=RUN_ID) created = preflight._ensure_failure_attempt(config) assert created.is_dir() assert preflight._ensure_failure_attempt(config) == created def racing_mkdir(*_args, **_kwargs): raise FileExistsError("peer created it") race = SimpleNamespace(staging_path=tmp_path, run_id="4" * 32) monkeypatch.setattr(preflight, "os", _FakeOs(mkdir=racing_mkdir)) assert preflight._ensure_failure_attempt(race) == tmp_path / ("4" * 32) missing = SimpleNamespace(staging_path=tmp_path / "missing", run_id=RUN_ID) with pytest.raises(RuntimeContractError, match="could not be established") as error: preflight._ensure_failure_attempt(missing) assert error.value.code == "LMS_EVIDENCE_UNAVAILABLE" def test_wait_for_evidence_times_out_and_write_failures_are_mapped(tmp_path, monkeypatch): clock = iter((0.0, 0.5, 2.0)) monkeypatch.setattr(preflight.time, "monotonic", lambda: next(clock)) monkeypatch.setattr(preflight.time, "sleep", lambda _seconds: None) with pytest.raises(RuntimeContractError, match="not published within") as error: preflight._wait_for_evidence(tmp_path / "evidence.json", timeout_seconds=1) assert error.value.code == "LMS_EVIDENCE_UNAVAILABLE" evidence = PreflightEvidence.failure( run_id=RUN_ID, config_digest=_digest("a"), code="LMS_STAGE_TIMEOUT", world_size=2, evidence_tier="protocol_cpu", ) with pytest.raises(RuntimeContractError, match="could not be persisted") as error: preflight._write_terminal_evidence(tmp_path / "missing" / "evidence.json", evidence) assert error.value.code == "LMS_EVIDENCE_UNAVAILABLE" # --- attestation inventory -------------------------------------------------- def test_validate_attestations_requires_one_record_per_rank(): with pytest.raises(RuntimeContractError, match="exactly one attestation") as error: _validate((_attestation(0),)) assert error.value.code == "LMS_MEMBERSHIP_INVALID" def test_validate_attestations_requires_group_ranks_to_cover_hosts(): records = (_attestation(0), _attestation(1, group_rank=2)) with pytest.raises(RuntimeContractError, match="group ranks do not cover") as error: _validate(records) assert error.value.code == "LMS_MEMBERSHIP_INVALID" def test_validate_attestations_requires_rank_to_group_mapping(): records = (_attestation(0, group_rank=1), _attestation(1, group_rank=0)) with pytest.raises(RuntimeContractError, match="fixed host-group mapping") as error: _validate(records) assert error.value.code == "LMS_MEMBERSHIP_INVALID" def test_validate_attestations_requires_tensor_parallel_world(): with pytest.raises(RuntimeContractError, match="tensor_parallel_size") as error: preflight.validate_attestations( (_attestation(0), _attestation(1)), world_size=2, tensor_parallel_size=1, dimension_divisors=(2,), expected_device_kind="cuda", expected_device_config_digest=_digest("0"), expected_software_digest=_digest("5"), expected_source_digest=_digest("7"), expected_model_digest=_digest("8"), expected_tokenizer_digest=_digest("9"), expected_config_digest=_digest("a"), expected_commit_sha="b" * 40, expected_code_digest=_digest("f"), expected_placement_plan_digest=_digest("c"), expected_storage_digest=_digest("6"), expected_network_interface_digest=_digest("d"), local_world_size=1, min_free_device_memory_bytes=1, min_free_host_memory_bytes=1, min_free_staging_bytes=1, ) assert error.value.code == "LMS_TOPOLOGY_UNSUPPORTED" def test_checkout_commit_rejects_a_foreign_git_pointer(tmp_path): (tmp_path / ".git").write_text("not a gitdir pointer\n", encoding="utf-8") with pytest.raises(ContractError, match="checkout identity is unavailable"): preflight.checkout_commit(tmp_path) @pytest.mark.parametrize("file_count", (10_000, 10_001)) def test_checkout_code_digest_enforces_its_file_bound(tmp_path, monkeypatch, file_count): package = tmp_path / "obliteratus" package.mkdir() module = package / "a.py" module.write_text("x = 1\n", encoding="utf-8") hashed = [] def fake_hash(path, **_kwargs): hashed.append(path) return 1, _digest("0") monkeypatch.setattr(preflight, "_hash_file", fake_hash) monkeypatch.setattr(type(package), "rglob", lambda self, _pattern: [module] * file_count) if file_count == 10_000: assert len(preflight.checkout_code_digest(tmp_path)) == 64 else: with pytest.raises(RuntimeContractError, match="file bound") as error: preflight.checkout_code_digest(tmp_path) assert error.value.code == "LMS_RESOURCE_ADMISSION_DENIED" assert len(hashed) == 10_000 @pytest.mark.parametrize("entry", ("begin", "run", "live")) def test_runtime_contract_errors_pass_through_unchanged(monkeypatch, entry): refusal = RuntimeContractError("LMS_SECRET_INPUT_REJECTED", "exact refusal") def refuse(*_args, **_kwargs): raise refusal with pytest.raises(RuntimeContractError) as error: if entry == "begin": monkeypatch.setattr(preflight, "require_record_consensus", refuse) monkeypatch.setattr(preflight, "_run_identity", lambda _config: {"run_id": RUN_ID}) preflight._begin_lifecycle(SimpleNamespace(run_id=RUN_ID), SimpleNamespace(rank=0)) elif entry == "run": preflight.run_preflight(_run_preflight_config(validate=refuse), SimpleNamespace()) else: for name, value in ( ("is_available", True), ("is_initialized", True), ("get_backend", "gloo"), ("get_rank", 0), ("get_world_size", 2), ): monkeypatch.setattr(preflight.dist, name, lambda value=value: value) monkeypatch.setattr( preflight, "run_preflight", lambda *_a, **_k: SimpleNamespace(lifecycle_identity_digest=IDENTITY), ) monkeypatch.setattr(preflight, "gloo_all_gather_records", lambda m: (m.to_bytes(),)) monkeypatch.setattr(preflight, "_validate_stage_messages", refuse) preflight.run_live_preflight( SimpleNamespace(run_id=RUN_ID, world_size=2), SimpleNamespace(rank=0, world_size=2), ) assert error.value is refusal # --- execute_preflight terminal handling (single coordinator, simulated peer) --- def _execute_harness(monkeypatch, tmp_path, *, fail=None, rank=0): config = SimpleNamespace( run_id=RUN_ID, digest=_digest("a"), source_digest=_digest("7"), model_digest=_digest("8"), tokenizer_digest=_digest("9"), commit_sha="b" * 40, world_size=2, coordinator_rank=0, staging_path=tmp_path, evidence_path=tmp_path / "evidence.json", evidence_tier="protocol_cpu", teardown_timeout_seconds=0.05, ) launch = SimpleNamespace(rank=rank) attempt = tmp_path / RUN_ID @contextmanager def control_group(_config, _launch): yield def run_preflight(_config, _launch, *, probes=None): if fail is not None: raise RuntimeContractError(fail, "private failure") attempt.mkdir(mode=0o700, exist_ok=True) return SimpleNamespace( lifecycle_identity_digest=IDENTITY, identity_digest=_digest("4"), attempt_path=attempt, ) def gather(message): return tuple( StageMessage(**{**message.__dict__, "rank": peer}).to_bytes() for peer in (0, 1) ) monkeypatch.setattr(preflight, "control_group", control_group) monkeypatch.setattr(preflight, "run_preflight", run_preflight) monkeypatch.setattr(preflight, "gloo_all_gather_records", gather) monkeypatch.setattr(preflight, "unanimous_vote", lambda _sequence, accepted: accepted) monkeypatch.setattr(preflight.dist, "barrier", lambda: None) return config, launch, attempt def _peer_ack(attempt: Path, terminal_identity: str, **overrides): values = { "run_id": RUN_ID, "identity_digest": terminal_identity, "rank": 1, "sequence": 2, "stage": RuntimeStage.PREFLIGHTED, "vote": Vote.COMMITTED, } values.update(overrides) write_stage_message(attempt / ".rank-1.teardown.stage.json", StageMessage(**values)) def _terminal_identity(monkeypatch): captured = {} original = PreflightEvidence.success def success(**kwargs): captured["identity"] = kwargs["identity_digest"] return original(**kwargs) monkeypatch.setattr(preflight.PreflightEvidence, "success", success) return captured def test_execute_preflight_success_requires_every_peer_acknowledgement(monkeypatch, tmp_path): config, launch, attempt = _execute_harness(monkeypatch, tmp_path) captured = _terminal_identity(monkeypatch) original_write = preflight.write_stage_message def write_and_ack(path, message): original_write(path, message) if path.name == ".rank-0.teardown.stage.json": _peer_ack(attempt, captured["identity"]) monkeypatch.setattr(preflight, "write_stage_message", write_and_ack) evidence = preflight.execute_preflight(config, launch) assert evidence.result == "preflighted" assert preflight.read_evidence(config.evidence_path) == evidence @pytest.mark.parametrize("peer", ("disagreeing", "corrupt", "missing")) def test_execute_preflight_unacknowledged_success_is_cleanup_incomplete( monkeypatch, tmp_path, peer, ): config, launch, attempt = _execute_harness(monkeypatch, tmp_path) captured = _terminal_identity(monkeypatch) original_write = preflight.write_stage_message def write_and_ack(path, message): original_write(path, message) if path.name != ".rank-0.teardown.stage.json": return if peer == "disagreeing": _peer_ack(attempt, _digest("e")) elif peer == "corrupt": (attempt / ".rank-1.teardown.stage.json").write_bytes(b"{") monkeypatch.setattr(preflight, "write_stage_message", write_and_ack) with pytest.raises(RuntimeContractError) as error: preflight.execute_preflight(config, launch) assert error.value.code == "LMS_CLEANUP_INCOMPLETE" assert "identity" in captured terminal = preflight.read_evidence(config.evidence_path) assert terminal.result == "quarantined" and terminal.error_code == "LMS_CLEANUP_INCOMPLETE" def test_execute_preflight_failed_teardown_acknowledgement_is_terminal(monkeypatch, tmp_path): config, launch, _attempt = _execute_harness(monkeypatch, tmp_path) original_write = preflight.write_stage_message def refuse_teardown(path, message): if path.name.endswith(".teardown.stage.json"): raise OSError("disk full") original_write(path, message) monkeypatch.setattr(preflight, "write_stage_message", refuse_teardown) with pytest.raises(RuntimeContractError) as error: preflight.execute_preflight(config, launch) assert error.value.code == "LMS_CLEANUP_INCOMPLETE" def _abort_harness(monkeypatch, tmp_path, *, code="LMS_SOURCE_CHANGED", rank=0): config, launch, attempt = _execute_harness(monkeypatch, tmp_path, fail=code, rank=rank) monkeypatch.setattr(preflight, "_collective_aborting", lambda *_a: (code, ())) return config, launch, attempt def test_execute_preflight_failure_with_quarantined_peer_is_cleanup_incomplete( monkeypatch, tmp_path, ): config, launch, _attempt = _abort_harness(monkeypatch, tmp_path) original = preflight._write_local_terminal_receipts def write_with_quarantined_peer(path, **kwargs): original(path, **kwargs) original(path, **{**kwargs, "launch": SimpleNamespace(rank=1), "quarantined": True}) monkeypatch.setattr(preflight, "_write_local_terminal_receipts", write_with_quarantined_peer) with pytest.raises(RuntimeContractError) as error: preflight.execute_preflight(config, launch) assert error.value.code == "LMS_CLEANUP_INCOMPLETE" assert preflight.read_evidence(config.evidence_path).error_code == "LMS_CLEANUP_INCOMPLETE" @pytest.mark.parametrize("peer", ("corrupt", "missing")) def test_execute_preflight_failure_without_peer_receipts_is_cleanup_incomplete( monkeypatch, tmp_path, peer, ): config, launch, _attempt = _abort_harness(monkeypatch, tmp_path) original = preflight._write_local_terminal_receipts def write_with_corrupt_peer(path, **kwargs): original(path, **kwargs) if peer == "corrupt": (path / ".rank-1.aborting.stage.json").write_bytes(b"{") monkeypatch.setattr(preflight, "_write_local_terminal_receipts", write_with_corrupt_peer) with pytest.raises(RuntimeContractError) as error: preflight.execute_preflight(config, launch) assert error.value.code == "LMS_CLEANUP_INCOMPLETE" terminal = preflight.read_evidence(config.evidence_path) assert terminal.error_code == "LMS_CLEANUP_INCOMPLETE" def test_execute_preflight_reports_unavailable_failure_attempt(monkeypatch, tmp_path): config, launch, _attempt = _abort_harness(monkeypatch, tmp_path) def unavailable(_config): raise RuntimeContractError("LMS_EVIDENCE_UNAVAILABLE", "no attempt") def receipts_fail(*_args, **_kwargs): raise OSError("attempt directory is missing") monkeypatch.setattr(preflight, "_ensure_failure_attempt", unavailable) monkeypatch.setattr(preflight, "_write_local_terminal_receipts", receipts_fail) with pytest.raises(RuntimeContractError) as error: preflight.execute_preflight(config, launch) # Receipt persistence failure dominates the evidence-directory failure. assert error.value.code == "LMS_CLEANUP_INCOMPLETE" assert preflight.read_evidence(config.evidence_path).error_code == "LMS_CLEANUP_INCOMPLETE" def test_execute_preflight_peer_waits_for_the_coordinator_attempt(monkeypatch, tmp_path): config, launch, attempt = _abort_harness(monkeypatch, tmp_path, rank=1) written = [] monkeypatch.setattr( preflight, "_write_local_terminal_receipts", lambda path, **kwargs: written.append((path, kwargs["code"])), ) evidence = PreflightEvidence.failure( run_id=RUN_ID, config_digest=config.digest, code="LMS_SOURCE_CHANGED", world_size=2, evidence_tier="protocol_cpu", ) preflight.write_evidence(config.evidence_path, evidence) with pytest.raises(RuntimeContractError) as error: preflight.execute_preflight(config, launch) assert error.value.code == "LMS_SOURCE_CHANGED" assert written == [(attempt, "LMS_SOURCE_CHANGED")] assert not attempt.exists()