Files
OBLITERATUS/tests/test_distributed_preflight.py
Joseph MaglyandClaude Opus 5.5 8dd6add995 fix(distributed): TP2 hardening before two-GPU qualification
Fixes eight reported defects in the native tensor-parallel path:

- #278 VERIFY admits host memory in a symmetric vote before allocating
  and broadcasting baseline input tensors.
- #280 require_record_consensus canonicalizes inside the poison-protected
  exchange and compares canonical payloads.
- #282 EXCISE votes on fallible digest preparation before the next
  collective.
- #285 preflight, model session, PROBE and EXCISE vote on rank-local
  preparation failures before the next collective, including the
  evidence-directory preparation that previously allocated before its
  all_reduce.
- #275 postcondition and VERIFY digest sets stream the full declared
  operation range (up to MAX_EXCISE_OPERATIONS) while preserving
  existing identities.
- #281 preflight source identity digests stream inventory records
  instead of using one 64 KiB control record.
- #286 the evidence checker requires an integer schema_version and
  checkout/source inventories are bounded and streamed.
- #276 standalone operation planning uses bounded chunked finite checks.

Each fix has CPU/Gloo regression tests, including one-rank fault cases.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-18 20:53:20 -04:00

1397 lines
56 KiB
Python

"""Admission and source-safety tests for the distributed preflight."""
from __future__ import annotations
import hashlib
import json
import os
import socket
import time
from contextlib import contextmanager, nullcontext
from dataclasses import replace
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import Mock
import pytest
import obliteratus.distributed.preflight as preflight
from obliteratus.distributed.contracts import (
AgreedTopologyRefusal,
ContractError,
RuntimeContractError,
RuntimeStage,
StageMessage,
canonical_record,
contract_digest,
)
from obliteratus.distributed.launcher import validate_network_interface
from obliteratus.distributed.preflight import (
_validate_stage_messages,
RankAttestation,
checkout_code_digest,
checkout_commit,
inspect_source,
storage_mount_digest,
validate_attestations,
)
from tests.test_distributed_launcher import _profile
@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 _mock_layout_collectives(monkeypatch, rank):
monkeypatch.setattr(preflight.dist, "get_backend", lambda: "gloo")
monkeypatch.setattr(preflight.dist, "get_world_size", lambda: 2)
monkeypatch.setattr(preflight.dist, "get_rank", lambda: rank)
monkeypatch.setattr(
preflight, "gloo_all_gather_records",
lambda record, **_kwargs: (canonical_record(record),) * 2,
)
def _attestation(rank: int = 0, **overrides) -> RankAttestation:
values = {
"rank": rank,
"local_rank": 0,
"local_world_size": 1,
"group_rank": rank,
"world_size": 2,
"host_digest": ("1" if rank == 0 else "2") * 64,
"device_digest": ("3" if rank == 0 else "4") * 64,
"device_profile_digest": "0" * 64,
"device_config_digest": contract_digest(
{"kind": "cuda", "name": "test-device", "compute_capability": "10.0"}
),
"device_kind": "cuda",
"total_device_memory_bytes": 8192,
"free_device_memory_bytes": 4096,
"total_host_memory_bytes": 16384,
"free_host_memory_bytes": 8192,
"free_staging_bytes": 8192,
"software_digest": "5" * 64,
"storage_digest": "6" * 64,
"source_digest": "7" * 64,
"model_digest": "8" * 64,
"tokenizer_digest": "9" * 64,
"config_digest": "a" * 64,
"commit_sha": "b" * 40,
"code_digest": "f" * 64,
"placement_plan_digest": "c" * 64,
"network_interface_digest": "d" * 64,
}
values.update(overrides)
return RankAttestation(**values)
def _validate(records):
validate_attestations(
tuple(records),
world_size=2,
tensor_parallel_size=2,
dimension_divisors=(2, 4),
expected_device_kind="cuda",
expected_device_config_digest=contract_digest(
{"kind": "cuda", "name": "test-device", "compute_capability": "10.0"}
),
expected_software_digest="5" * 64,
expected_source_digest="7" * 64,
expected_model_digest="8" * 64,
expected_tokenizer_digest="9" * 64,
expected_config_digest="a" * 64,
expected_commit_sha="b" * 40,
expected_code_digest="f" * 64,
expected_placement_plan_digest="c" * 64,
expected_storage_digest="6" * 64,
expected_network_interface_digest="d" * 64,
local_world_size=1,
min_free_device_memory_bytes=4096,
min_free_host_memory_bytes=8192,
min_free_staging_bytes=8192,
)
def test_complete_fixed_inventory_passes_exact_boundaries():
_validate((_attestation(0), _attestation(1)))
@pytest.mark.parametrize("rank", (0, 1))
def test_two_host_gpu_preflight_refuses_after_layout_agreement_before_staging(monkeypatch, rank):
events = []
config = SimpleNamespace(
device_kind="cuda", world_size=2, local_world_size=1,
validate=lambda: events.append("validate"),
)
launch = SimpleNamespace(
rank=rank, local_rank=0, group_rank=rank, world_size=2, local_world_size=1,
)
_mock_layout_collectives(monkeypatch, rank)
def consensus(observed_config, observed_launch):
assert observed_config is config and observed_launch is launch
events.append("consensus")
return (object(), "a" * 64, ())
def forbidden(*_args, **_kwargs):
pytest.fail("two-host refusal must precede filesystem access and probes")
monkeypatch.setattr(preflight, "_begin_lifecycle", consensus)
monkeypatch.setattr(preflight, "_prepare_attempt_directory", forbidden)
monkeypatch.setattr(preflight, "_probe_shared_staging", forbidden)
with pytest.raises(RuntimeContractError, match="live two-host TP2 preflight is refused") as error:
preflight.run_preflight(config, launch, probes=SimpleNamespace(collect=forbidden))
assert error.value.code == "LMS_TOPOLOGY_UNSUPPORTED"
assert events == ["validate"]
def _mock_preflight_ownership(monkeypatch, tmp_path, rank, teardown_error=None):
events = []
config = SimpleNamespace(
device_kind="cuda", world_size=2, local_world_size=1, coordinator_rank=0,
run_id="1" * 32, digest="2" * 64, source_digest="3" * 64,
model_digest="4" * 64, tokenizer_digest="5" * 64, commit_sha="6" * 40,
staging_path=tmp_path, evidence_path=tmp_path / "evidence.json",
evidence_tier="candidate_preflight", teardown_timeout_seconds=0.01,
validate=lambda: events.append("validate"),
)
launch = SimpleNamespace(
rank=rank, local_rank=0, group_rank=rank, world_size=2, local_world_size=1,
)
_mock_layout_collectives(monkeypatch, rank)
@contextmanager
def control_group(observed_config, observed_launch):
assert observed_config is config and observed_launch is launch
events.append("enter")
try:
yield
finally:
events.append("teardown")
if teardown_error is not None:
raise RuntimeContractError(teardown_error, "private teardown detail")
def consensus(observed_config, observed_launch):
assert observed_config is config and observed_launch is launch
events.append("consensus")
identity = preflight._run_identity(config)
return identity, contract_digest(identity), ()
monkeypatch.setattr(preflight, "control_group", control_group)
monkeypatch.setattr(preflight, "_begin_lifecycle", consensus)
return config, launch, events
@pytest.mark.parametrize("entrypoint", ("execute", "cli_json", "cli_human"))
@pytest.mark.parametrize("teardown_error", (None, "LMS_CLEANUP_INCOMPLETE"))
@pytest.mark.parametrize("gather_failure", (False, True))
def test_public_two_host_refusal_is_symmetric_and_storage_free(
monkeypatch, tmp_path, capsys, entrypoint, teardown_error, gather_failure,
):
from obliteratus import cli
import obliteratus.distributed.config as config_module
import obliteratus.distributed.launcher as launcher_module
forbidden_calls = []
def forbidden(*_args, **_kwargs):
forbidden_calls.append(True)
raise AssertionError("agreed refusal must not enter storage or abort handling")
for name in (
"_prepare_attempt_directory", "_probe_shared_staging", "SystemProbes",
"_collective_aborting", "_ensure_failure_attempt",
"_write_local_terminal_receipts", "_read_terminal_receipts",
"_write_terminal_evidence", "_wait_for_evidence",
"write_stage_message", "read_stage_message", "write_evidence", "read_evidence",
):
monkeypatch.setattr(preflight, name, forbidden)
outcomes = []
for rank in (0, 1):
staging = tmp_path / f"host-{rank}"
staging.mkdir()
sentinel = staging / "existing"
sentinel.write_bytes(b"untouched")
before = sorted(staging.rglob("*"))
config, launch, events = _mock_preflight_ownership(
monkeypatch, staging, rank, teardown_error,
)
if gather_failure:
monkeypatch.setattr(preflight, "gloo_all_gather_records", Mock(
side_effect=ContractError("private layout gather failure"),
))
if entrypoint == "execute":
with pytest.raises(RuntimeContractError) as error:
preflight.execute_preflight(config, launch)
outcomes.append((error.value.code, str(error.value)))
else:
monkeypatch.setattr(
config_module.DistributedPreflightConfig, "from_file", lambda _path: config,
)
monkeypatch.setattr(
launcher_module.TorchrunEnvironment, "from_environ", lambda *_args: launch,
)
argv = ["distributed", "preflight", str(staging / "private-profile.json")]
if entrypoint == "cli_json":
argv.append("--json")
with pytest.raises(SystemExit) as error:
cli.main(argv)
assert error.value.code == 2
output = capsys.readouterr().out
if entrypoint == "cli_json":
payload = json.loads(output)
assert payload == {
"schema_version": 1, "result": "failed",
"error_code": "LMS_TOPOLOGY_UNSUPPORTED",
}
assert "LMS_TOPOLOGY_UNSUPPORTED" in output
assert str(staging) not in output and "private" not in output
outcomes.append(output)
assert events == ["enter", "validate", "teardown"]
assert sorted(staging.rglob("*")) == before
assert sentinel.read_bytes() == b"untouched"
assert outcomes[0] == outcomes[1]
if entrypoint == "execute":
assert outcomes[0][0] == "LMS_TOPOLOGY_UNSUPPORTED"
assert forbidden_calls == []
@pytest.mark.parametrize(
("device_kind", "local_world_size", "code"),
(("cuda", 2, "LMS_RESOURCE_ADMISSION_DENIED"),
("cuda", 2, "LMS_TOPOLOGY_UNSUPPORTED"),
("cpu", 1, "LMS_RESOURCE_ADMISSION_DENIED"),
("cpu", 2, "LMS_RESOURCE_ADMISSION_DENIED")),
)
def test_supported_layout_failure_retains_receipts_and_evidence(
monkeypatch, tmp_path, device_kind, local_world_size, code,
):
config, launch, events = _mock_preflight_ownership(monkeypatch, tmp_path, 0)
config.device_kind = device_kind
config.local_world_size = local_world_size
launch.local_world_size = local_world_size
if device_kind == "cpu":
config.evidence_tier = "protocol_cpu"
def fail_before_probes(*_args):
raise RuntimeContractError(code, "private probe failure")
abort = Mock(return_value=(code, ()))
monkeypatch.setattr(preflight, "_prepare_attempt_directory", fail_before_probes)
monkeypatch.setattr(preflight, "_collective_aborting", abort)
original_write = preflight._write_local_terminal_receipts
def write_mocked_rank_receipts(attempt, **kwargs):
original_write(attempt, **kwargs)
peer = SimpleNamespace(rank=1)
original_write(attempt, **{**kwargs, "launch": peer})
helpers = {}
for name in (
"_ensure_failure_attempt", "_write_local_terminal_receipts",
"_read_terminal_receipts", "_write_terminal_evidence", "_wait_for_evidence",
):
implementation = (
write_mocked_rank_receipts if name == "_write_local_terminal_receipts"
else getattr(preflight, name)
)
helpers[name] = Mock(wraps=implementation)
monkeypatch.setattr(preflight, name, helpers[name])
with pytest.raises(RuntimeContractError) as error:
preflight.execute_preflight(config, launch)
assert error.value.code == code
abort.assert_called_once()
for helper in helpers.values():
helper.assert_called_once()
for rank in (0, 1):
receipt = tmp_path / config.run_id / f".rank-{rank}.terminal.stage.json"
assert preflight.read_stage_message(receipt).stage is RuntimeStage.ABORTED
terminal = preflight.read_evidence(config.evidence_path)
assert terminal.result == "failed" and terminal.error_code == code
assert "private probe failure" not in config.evidence_path.read_text()
assert events == ["enter", "validate", "consensus", "teardown"]
@pytest.mark.parametrize(
("device_kind", "local_world_size"), (("cuda", 2), ("cpu", 1), ("cpu", 2)),
)
def test_live_staging_layout_retains_local_gpu_and_protocol_admission(
monkeypatch, device_kind, local_world_size,
):
_mock_layout_collectives(monkeypatch, 0)
preflight._require_live_staging_layout(SimpleNamespace(
device_kind=device_kind, world_size=2, local_world_size=local_world_size,
), SimpleNamespace(
rank=0, local_rank=0, group_rank=0, world_size=2, local_world_size=local_world_size,
))
def _paired_layout_runtime(monkeypatch, configs, launches, backends=("gloo", "gloo")):
"""Replay each mocked rank against the same complete collective records."""
active = {"rank": 0}
events = []
monkeypatch.setattr(preflight.dist, "get_rank", lambda: active["rank"])
monkeypatch.setattr(preflight.dist, "get_world_size", lambda: 2)
monkeypatch.setattr(preflight.dist, "get_backend", lambda: backends[active["rank"]])
layouts = []
for rank in (0, 1):
active["rank"] = rank
layouts.append(preflight._live_staging_layout_record(configs[rank], launches[rank]))
payloads = tuple(canonical_record(record) for record in layouts)
identities = tuple(canonical_record(preflight._run_identity(config)) for config in configs)
def identity_consensus(identity):
assert canonical_record(identity) == identities[0] == identities[1]
events.append((active["rank"], "identity"))
return contract_digest(identity)
def gather(record, **kwargs):
rank = active["rank"]
if isinstance(record, dict):
assert record == layouts[rank]
assert record["sequence"] == 4
assert kwargs == {"capacity": 512}
assert max(map(len, payloads)) <= kwargs["capacity"]
events.append((rank, "layout"))
return payloads
if isinstance(record, StageMessage):
events.append((rank, record.stage.value))
return tuple(canonical_record(replace(record, rank=peer)) for peer in (0, 1))
assert isinstance(record, RankAttestation)
return tuple(canonical_record(item) for item in attestations)
@contextmanager
def control_group(config, launch):
rank = active["rank"]
assert config is configs[rank] and launch is launches[rank]
events.append((rank, "enter"))
try:
yield
finally:
events.append((rank, "teardown"))
# The lifecycle implementation remains real. Only transport and ownership
# are mocked: no Gloo, GPU, watchdog, or worker process is started.
monkeypatch.setattr(preflight, "require_record_consensus", identity_consensus)
monkeypatch.setattr(preflight, "gloo_all_gather_records", gather)
monkeypatch.setattr(preflight, "control_group", control_group)
attestations = tuple(
_attestation(
rank, local_rank=rank, local_world_size=2, group_rank=0,
host_digest="1" * 64,
device_kind=configs[rank].device_kind,
device_config_digest=contract_digest({
"kind": configs[rank].device_kind, "name": configs[rank].device_name,
"compute_capability": configs[rank].compute_capability,
}),
software_digest=configs[rank].software_digest,
source_digest=configs[rank].source_digest, model_digest=configs[rank].model_digest,
tokenizer_digest=configs[rank].tokenizer_digest, config_digest=configs[rank].digest,
commit_sha=configs[rank].commit_sha, code_digest=configs[rank].code_digest,
placement_plan_digest=configs[rank].placement_plan_digest,
storage_digest=configs[rank].storage_digest,
network_interface_digest=preflight._opaque_identity(
configs[rank].network_interface, configs[rank].rendezvous_id,
),
) for rank in (0, 1)
)
return active, events, layouts, attestations
def _one_host_pair(tmp_path):
config = _profile(tmp_path, run={
"run_id": "1" * 32, "rendezvous_id": "2" * 32,
"world_size": 2, "local_world_size": 2,
})
launches = tuple(SimpleNamespace(
rank=rank, local_rank=rank, group_rank=0, world_size=2, local_world_size=2,
rendezvous_id=config.rendezvous_id,
) for rank in (0, 1))
return (config, config), launches
@pytest.mark.parametrize("entrypoint", ("run_preflight", "execute_preflight"))
@pytest.mark.parametrize("layout", (
"stale_digest", "stale_digest_matching_launcher", "two_host",
"launcher_local_world_size", "launcher_world_size", "local_rank", "group_rank",
"device_kind", "backend", "world_size", "cpu_layout_disagreement",
))
def test_actual_layout_agreement_is_terminal_before_any_storage(
monkeypatch, tmp_path, entrypoint, layout,
):
configs, launches = _one_host_pair(tmp_path)
backends = ("gloo", "gloo")
if layout in {"stale_digest", "stale_digest_matching_launcher"}:
configs = (configs[0], replace(configs[1], local_world_size=1))
if layout == "stale_digest_matching_launcher":
launches[1].local_world_size = 1
launches[1].local_rank = 0
launches[1].group_rank = 1
elif layout == "two_host":
configs = tuple(replace(config, local_world_size=1) for config in configs)
for launch in launches:
launch.local_world_size = 1
launch.local_rank = 0
launch.group_rank = launch.rank
elif layout == "cpu_layout_disagreement":
configs = tuple(replace(
config, device_kind="cpu", evidence_tier="protocol_cpu",
local_world_size=2 - rank,
) for rank, config in enumerate(configs))
launches[1].local_world_size = 1
launches[1].local_rank = 0
launches[1].group_rank = 1
elif layout == "device_kind":
configs = (configs[0], replace(
configs[1], device_kind="cpu", evidence_tier="protocol_cpu",
))
elif layout == "backend":
backends = ("gloo", "nccl")
elif layout == "world_size":
configs = (configs[0], replace(configs[1], world_size=4, tensor_parallel_size=4))
else:
field = layout.removeprefix("launcher_")
setattr(launches[1], field, 1 if layout != "local_rank" else 0)
# Both real configurations validate with the same stored digest. Except
# for world_size (which the identity binds separately), their lifecycle
# identities also match, as in the dataclass-replacement counterexample.
for config in configs:
assert config.validate() is config
assert configs[0].digest == configs[1].digest
if layout != "world_size":
assert preflight._run_identity(configs[0]) == preflight._run_identity(configs[1])
active, events, layouts, _attestations = _paired_layout_runtime(
monkeypatch, configs, launches, backends,
)
if layout == "cpu_layout_disagreement":
assert all(record["admitted"] for record in layouts)
forbidden = Mock(side_effect=AssertionError("terminal layout refusal touched storage"))
for name in (
"_prepare_attempt_directory", "_probe_shared_staging", "SystemProbes",
"_collective_aborting", "_ensure_failure_attempt",
"_write_local_terminal_receipts", "_read_terminal_receipts",
"_write_terminal_evidence", "_wait_for_evidence",
"write_stage_message", "read_stage_message", "write_evidence", "read_evidence",
):
monkeypatch.setattr(preflight, name, forbidden)
sentinel = configs[0].staging_path / "existing"
sentinel.write_bytes(b"untouched")
before = sorted(tmp_path.rglob("*"))
outcomes = []
for rank in (0, 1):
active["rank"] = rank
with pytest.raises(AgreedTopologyRefusal) as error:
getattr(preflight, entrypoint)(
configs[rank], launches[rank], probes=SimpleNamespace(collect=forbidden),
)
outcomes.append((type(error.value), error.value.code, str(error.value)))
expected = ["layout"]
if entrypoint == "execute_preflight":
expected = ["enter", *expected, "teardown"]
assert [event for worker, event in events if worker == rank] == expected
assert outcomes[0] == outcomes[1]
assert outcomes[0][1] == "LMS_TOPOLOGY_UNSUPPORTED"
forbidden.assert_not_called()
assert sorted(tmp_path.rglob("*")) == before
assert sentinel.read_bytes() == b"untouched"
assert not (configs[0].staging_path / configs[0].run_id).exists()
assert not configs[0].evidence_path.exists()
@pytest.mark.parametrize("entrypoint", ("run_preflight", "execute_preflight"))
@pytest.mark.parametrize("failure", (
"gather", "gather_poison", "construction_0", "construction_1", "comparison",
))
def test_layout_admission_exceptions_are_symmetric_and_storage_free(
monkeypatch, tmp_path, entrypoint, failure,
):
import obliteratus.distributed.consensus as consensus
configs, launches = _one_host_pair(tmp_path)
configs = tuple(replace(config, local_world_size=1) for config in configs)
for launch in launches:
launch.local_world_size = 1
launch.local_rank = 0
launch.group_rank = launch.rank
active, events, layouts, _attestations = _paired_layout_runtime(
monkeypatch, configs, launches,
)
payloads = tuple(canonical_record(record) for record in layouts)
capacity = preflight._STAGING_LAYOUT_CAPACITY
failed_rank = int(failure[-1]) if failure.startswith("construction_") else None
frames = [consensus.encode_frame(payload, capacity=capacity) for payload in payloads]
if failed_rank is not None:
frames[failed_rank] = consensus.encode_frame(b"", capacity=capacity)
frames[failed_rank][:4] = 255
original_construct = preflight._live_staging_layout_record
def construct(config, launch):
if launch.rank == failed_rank:
raise MemoryError("private layout construction failure")
return original_construct(config, launch)
monkeypatch.setattr(preflight, "_live_staging_layout_record", construct)
# Replay real primitive serialization and poison handling on each mocked
# rank. The transport supplies the same fixed frames without a Gloo group.
monkeypatch.setattr(consensus, "_require_gloo", lambda _group: (active["rank"], 2))
transported = []
def transport(output, frame, *, group):
assert group is None
assert consensus.torch.equal(frame, frames[active["rank"]])
transported.append(active["rank"])
for target, source in zip(output, frames):
target.copy_(source)
monkeypatch.setattr(consensus.dist, "all_gather", transport)
primitive_errors = []
def gather(record, **kwargs):
rank = active["rank"]
events.append((rank, "layout"))
assert kwargs == {"capacity": capacity}
if failure == "gather":
raise RuntimeError("private layout transport failure")
if failure == "gather_poison":
raise ContractError("a peer rank failed to prepare its consensus payload")
if failure == "comparison":
return payloads
try:
return consensus.gloo_all_gather_records(record, **kwargs)
except ContractError as error:
primitive_errors.append((rank, str(error)))
raise
monkeypatch.setattr(preflight, "gloo_all_gather_records", gather)
if failure == "comparison":
monkeypatch.setattr(preflight, "canonical_record", Mock(
side_effect=RuntimeError("private layout comparison failure"),
))
forbidden = Mock(side_effect=AssertionError("failed layout admission touched storage"))
for name in (
"_begin_lifecycle", "_prepare_attempt_directory", "_probe_shared_staging", "SystemProbes",
"_collective_aborting", "_ensure_failure_attempt",
"_write_local_terminal_receipts", "_read_terminal_receipts",
"_write_terminal_evidence", "_wait_for_evidence",
"write_stage_message", "read_stage_message", "write_evidence", "read_evidence",
):
monkeypatch.setattr(preflight, name, forbidden)
sentinel = configs[0].staging_path / "existing"
sentinel.write_bytes(b"untouched")
before = sorted(tmp_path.rglob("*"))
outcomes = []
for rank in (0, 1):
active["rank"] = rank
with pytest.raises(AgreedTopologyRefusal) as error:
getattr(preflight, entrypoint)(
configs[rank], launches[rank], probes=SimpleNamespace(collect=forbidden),
)
outcomes.append((type(error.value), error.value.code, str(error.value)))
expected = ["layout"]
if entrypoint == "execute_preflight":
expected = ["enter", *expected, "teardown"]
assert [event for worker, event in events if worker == rank] == expected
assert outcomes[0] == outcomes[1]
assert outcomes[0][1] == "LMS_TOPOLOGY_UNSUPPORTED"
if failed_rank is not None:
assert transported == [0, 1]
assert len(primitive_errors) == 2
assert "unsupported type bytes" in dict(primitive_errors)[failed_rank]
assert "peer rank failed to prepare" in dict(primitive_errors)[1 - failed_rank]
forbidden.assert_not_called()
assert sorted(tmp_path.rglob("*")) == before
assert sentinel.read_bytes() == b"untouched"
assert not (configs[0].staging_path / configs[0].run_id).exists()
assert not configs[0].evidence_path.exists()
@pytest.mark.parametrize("entrypoint", ("run_preflight", "execute_preflight"))
@pytest.mark.parametrize("device_kind", ("cuda", "cpu"))
@pytest.mark.parametrize("failure_code", (
None, "LMS_RESOURCE_ADMISSION_DENIED", "LMS_TOPOLOGY_UNSUPPORTED",
))
def test_agreed_one_host_layout_keeps_success_and_ordinary_failure_paths(
monkeypatch, tmp_path, entrypoint, device_kind, failure_code,
):
configs, launches = _one_host_pair(tmp_path)
# Separate mocked host roots allow each worker's local storage behavior to
# be checked independently, with peer transport/receipts supplied below.
configs = tuple(replace(
config, device_kind=device_kind,
evidence_tier="protocol_cpu" if device_kind == "cpu" else "candidate_preflight",
staging_path=tmp_path / f"rank-{rank}",
evidence_path=tmp_path / f"rank-{rank}" / config.run_id / "preflight.json",
).validate() for rank, config in enumerate(configs))
for config in configs:
# Match the private attempt parent normally created by production;
# receipt and terminal-evidence writes retain their real checks below.
(config.staging_path / config.run_id).mkdir(mode=0o700, parents=True)
active, events, layouts, attestations = _paired_layout_runtime(
monkeypatch, configs, launches,
)
assert all(record["admitted"] for record in layouts)
assert layouts[0] == layouts[1]
def prepare(config, launch):
events.append((launch.rank, "prepare"))
if failure_code is not None:
raise RuntimeContractError(failure_code, "private probe failure")
return config.staging_path / config.run_id
source = preflight.SourceIdentity(
configs[0].source_digest, configs[0].model_digest, configs[0].tokenizer_digest, 1, 1,
)
collect = Mock(return_value=SimpleNamespace(source=source))
monkeypatch.setattr(preflight, "_prepare_attempt_directory", prepare)
monkeypatch.setattr(preflight, "_stage_deadline", lambda *_args: nullcontext())
monkeypatch.setattr(
preflight, "_attestation", lambda _config, launch, _snapshot: attestations[launch.rank],
)
monkeypatch.setattr(
preflight, "_probe_shared_staging", lambda config, _launch: config.staging_path / config.run_id,
)
votes = []
def vote(sequence, accepted):
votes.append((active["rank"], sequence, accepted))
return accepted
def failure_vote(sequence, code):
vote(sequence, code is None)
return (code,) if code else ()
monkeypatch.setattr(preflight, "unanimous_vote", vote)
monkeypatch.setattr(preflight, "gather_failure_codes", failure_vote)
monkeypatch.setattr(preflight.dist, "barrier", lambda: None)
original_write = preflight.write_stage_message
def write_with_peer(path, message):
original_write(path, message)
if path.name.startswith(".rank-"):
peer = 1 - message.rank
peer_path = path.with_name(path.name.replace(f".rank-{message.rank}.", f".rank-{peer}."))
original_write(peer_path, replace(message, rank=peer))
monkeypatch.setattr(preflight, "write_stage_message", write_with_peer)
original_read = preflight.read_stage_message
def read_with_coordinator(path):
if active["rank"] == 1 and path.name == ".preflight.prepared.stage.json":
path = configs[0].staging_path / configs[0].run_id / path.name
return original_read(path)
monkeypatch.setattr(preflight, "read_stage_message", read_with_coordinator)
coordinator_terminal = []
original_wait = preflight._wait_for_evidence
def wait_for_coordinator(path, **kwargs):
if active["rank"] == 0:
coordinator_terminal.append(original_wait(path, **kwargs))
return coordinator_terminal[0]
monkeypatch.setattr(preflight, "_wait_for_evidence", wait_for_coordinator)
abort = Mock(wraps=preflight._collective_aborting)
monkeypatch.setattr(preflight, "_collective_aborting", abort)
for rank in (0, 1):
active["rank"] = rank
config = configs[rank]
lifecycle_identity_digest = contract_digest(preflight._run_identity(config))
if failure_code is not None:
with pytest.raises(RuntimeContractError) as error:
getattr(preflight, entrypoint)(
config, launches[rank], probes=SimpleNamespace(collect=collect),
)
assert error.value.code == failure_code
assert not isinstance(error.value, AgreedTopologyRefusal)
else:
result = getattr(preflight, entrypoint)(
config, launches[rank], probes=SimpleNamespace(collect=collect),
)
if entrypoint == "run_preflight":
assert result.attempt_path == config.staging_path / config.run_id
assert result.attestations == attestations
assert result.lifecycle_identity_digest == lifecycle_identity_digest
else:
assert result.result == "preflighted"
receipt = config.staging_path / config.run_id / f".rank-{rank}.teardown.stage.json"
acknowledgement = preflight.read_stage_message(receipt)
assert acknowledgement.vote is preflight.Vote.COMMITTED
assert acknowledgement.identity_digest == result.identity_digest
rank_events = [event for worker, event in events if worker == rank]
assert rank_events.index("layout") < rank_events.index("identity") < rank_events.index("prepare")
if entrypoint == "execute_preflight":
assert rank_events[0] == "enter" and rank_events[-1] == "teardown"
if failure_code is not None:
assert "aborting" in rank_events
abort.assert_called_with(config, launches[rank], lifecycle_identity_digest, failure_code)
receipt = config.staging_path / config.run_id / f".rank-{rank}.terminal.stage.json"
message = preflight.read_stage_message(receipt)
assert message.stage is RuntimeStage.ABORTED
assert message.error_code == failure_code
assert message.identity_digest == lifecycle_identity_digest
if failure_code is None:
assert collect.call_count == 2
sequences = (9, 20, 21, 23, 16, 17, 19, 0, 18)
expected_votes = [(rank, sequence, True) for rank in (0, 1) for sequence in sequences]
if entrypoint == "execute_preflight":
expected_votes = [
(rank, sequence, True) for rank in (0, 1) for sequence in (27, *sequences, 25, 26, 2)
]
assert votes == expected_votes
else:
collect.assert_not_called()
sequences = (9, 20, 21, 23)
if entrypoint == "execute_preflight":
sequences = (27, *sequences)
assert votes == [(rank, sequence, True) for rank in (0, 1) for sequence in sequences]
if entrypoint == "execute_preflight":
assert abort.call_count == (2 if failure_code is not None else 0)
terminal = preflight.read_evidence(configs[0].evidence_path)
assert terminal.result == ("failed" if failure_code is not None else "preflighted")
assert terminal.error_code == failure_code
assert "private probe failure" not in configs[0].evidence_path.read_text()
else:
abort.assert_not_called()
assert all(not config.evidence_path.exists() for config in configs)
def test_rank_attestation_decoder_requires_exact_canonical_bytes():
record = _attestation()
assert RankAttestation.from_bytes(canonical_record(record)) == record
with pytest.raises(ContractError, match="not canonical"):
RankAttestation.from_bytes(b" " + canonical_record(record))
duplicate = canonical_record(record).replace(b'{"code_digest":', b'{"rank":0,"code_digest":', 1)
with pytest.raises(ContractError, match="duplicate"):
RankAttestation.from_bytes(duplicate)
def test_rank_attestation_rejects_invalid_memory_digest_and_json():
with pytest.raises(ContractError, match="free_host_memory_bytes cannot exceed"):
_attestation(total_host_memory_bytes=1, free_host_memory_bytes=2)
with pytest.raises(ContractError, match="free_staging_bytes is outside"):
_attestation(free_staging_bytes=-1)
with pytest.raises(ContractError, match="source_digest has an invalid format"):
_attestation(source_digest="bad")
assert _attestation().to_dict()["rank"] == 0
with pytest.raises(ContractError, match="strict UTF-8 JSON"):
RankAttestation.from_bytes(b"\xff")
with pytest.raises(ContractError, match="closed schema"):
RankAttestation.from_bytes(b"{}")
def test_stage_decoder_and_membership_errors_use_stable_runtime_codes(monkeypatch):
with pytest.raises(RuntimeContractError) as malformed:
preflight._stage_message_from_bytes(b"{}")
assert malformed.value.code == "LMS_CONSENSUS_RECORD_INVALID"
runtime_error = RuntimeContractError("LMS_STAGE_TIMEOUT", "timeout")
monkeypatch.setattr(
preflight.StageMessage,
"from_bytes",
lambda _payload: (_ for _ in ()).throw(runtime_error),
)
with pytest.raises(RuntimeContractError) as preserved:
preflight._stage_message_from_bytes(b"record")
assert preserved.value is runtime_error
with pytest.raises(RuntimeContractError) as membership:
_validate_stage_messages(
(),
run_id="1" * 32,
world_size=2,
stage=RuntimeStage.CREATED,
sequence=0,
identity_digest="2" * 64,
vote=None,
)
assert membership.value.code == "LMS_MEMBERSHIP_INVALID"
def test_lifecycle_validation_rejects_a_record_bound_to_another_run():
identity_digest = "a" * 64
records = (
StageMessage("1" * 32, identity_digest, 0, 0, RuntimeStage.CREATED),
StageMessage("2" * 32, identity_digest, 1, 0, RuntimeStage.CREATED),
)
with pytest.raises(RuntimeContractError) as error:
_validate_stage_messages(
records,
run_id="1" * 32,
world_size=2,
stage=RuntimeStage.CREATED,
sequence=0,
identity_digest=identity_digest,
vote=None,
)
assert error.value.code == "LMS_LIFECYCLE_INVALID"
@pytest.mark.parametrize(
("records", "message"),
[
((_attestation(0),), "exactly one attestation"),
((_attestation(0), _attestation(0)), "rank order"),
(
(_attestation(0), replace(_attestation(1), device_digest="3" * 64)),
"device identities",
),
(
(_attestation(0), replace(_attestation(1), device_profile_digest="e" * 64)),
"device profiles",
),
(
(_attestation(0), replace(_attestation(1), local_rank=0, host_digest="1" * 64)),
"local ranks",
),
(
(_attestation(0), replace(_attestation(1), software_digest="e" * 64)),
"software identities",
),
(
(_attestation(0), replace(_attestation(1), storage_digest="e" * 64)),
"storage identities",
),
(
(_attestation(0), replace(_attestation(1), source_digest="e" * 64)),
"source_digest",
),
(
(_attestation(0), replace(_attestation(1), free_device_memory_bytes=4095)),
"device memory headroom",
),
(
(_attestation(0), replace(_attestation(1), free_host_memory_bytes=8191)),
"host memory headroom",
),
(
(_attestation(0), replace(_attestation(1), free_staging_bytes=8191)),
"staging headroom",
),
],
)
def test_inventory_disagreement_fails_closed(records, message):
with pytest.raises(ContractError, match=message):
_validate(records)
def test_topology_dimensions_must_be_divisible():
with pytest.raises(ContractError, match="dimension divisor"):
validate_attestations(
(_attestation(0), _attestation(1)),
world_size=2,
tensor_parallel_size=2,
dimension_divisors=(3,),
expected_device_kind="cuda",
expected_device_config_digest=contract_digest(
{"kind": "cuda", "name": "test-device", "compute_capability": "10.0"}
),
expected_software_digest="5" * 64,
expected_source_digest="7" * 64,
expected_model_digest="8" * 64,
expected_tokenizer_digest="9" * 64,
expected_config_digest="a" * 64,
expected_commit_sha="b" * 40,
expected_code_digest="f" * 64,
expected_placement_plan_digest="c" * 64,
expected_storage_digest="6" * 64,
expected_network_interface_digest="d" * 64,
local_world_size=1,
min_free_device_memory_bytes=1,
min_free_host_memory_bytes=1,
min_free_staging_bytes=1,
)
def test_source_inspection_accepts_only_immutable_local_safetensors(tmp_path):
source = tmp_path / "source"
source.mkdir()
weights = source / "model.safetensors"
tokenizer = source / "tokenizer.json"
header = json.dumps(
{"weight": {"dtype": "F32", "shape": [1], "data_offsets": [0, 4]}},
separators=(",", ":"),
).encode()
weights.write_bytes(len(header).to_bytes(8, "little") + header + b"\0" * 4)
tokenizer.write_text('{"model":"fixture"}', encoding="utf-8")
weights.chmod(0o444)
tokenizer.chmod(0o444)
source.chmod(0o555)
first = inspect_source(source)
second = inspect_source(source)
assert first == second
assert first.file_count == 2
assert len({first.source_digest, first.model_digest, first.tokenizer_digest}) == 3
def test_source_inspection_accepts_inventory_larger_than_one_consensus_record(tmp_path):
source = tmp_path / "source"
source.mkdir()
header = json.dumps(
{"weight": {"dtype": "F32", "shape": [1], "data_offsets": [0, 4]}},
separators=(",", ":"),
).encode()
(source / "model.safetensors").write_bytes(
len(header).to_bytes(8, "little") + header + b"\0" * 4
)
for index in range(768):
(source / f"tokenizer-{index:05d}.json").write_text("{}", encoding="utf-8")
for item in source.iterdir():
item.chmod(0o444)
source.chmod(0o555)
try:
first = inspect_source(source, max_files=769)
assert first.file_count == 769
assert first == inspect_source(source, max_files=769)
finally:
source.chmod(0o755)
@pytest.mark.parametrize("name", (".staging-attempt", ".backup-prior"))
def test_source_inspection_rejects_transaction_directories(tmp_path, name):
source = tmp_path / name
source.mkdir()
with pytest.raises(RuntimeContractError, match="not loadable model sources") as error:
inspect_source(source)
assert error.value.code == "LMS_SOURCE_BOUNDARY_VIOLATION"
def test_source_inspection_rejects_arbitrary_bytes_with_safetensors_suffix(tmp_path):
source = tmp_path / "source"
source.mkdir()
weights = source / "model.safetensors"
tokenizer = source / "tokenizer.json"
weights.write_bytes(b"not-a-safetensors-file")
tokenizer.write_text("{}", encoding="utf-8")
weights.chmod(0o444)
tokenizer.chmod(0o444)
source.chmod(0o555)
with pytest.raises(ContractError, match="safe-structure inspection"):
inspect_source(source)
def test_source_inspection_rejects_executable_serialization(tmp_path):
source = tmp_path / "source"
source.mkdir()
weights = source / "model.safetensors"
pickle_file = source / "pytorch_model.bin"
weights.write_bytes(b"safe")
pickle_file.write_bytes(b"not-executed")
weights.chmod(0o444)
pickle_file.chmod(0o444)
source.chmod(0o555)
with pytest.raises(ContractError, match="outside the safetensors envelope"):
inspect_source(source)
def test_source_inspection_rejects_symlinked_directories(tmp_path):
outside = tmp_path / "outside"
outside.mkdir()
(outside / "tokenizer.json").write_text("{}", encoding="utf-8")
source = tmp_path / "source"
source.mkdir()
weights = source / "model.safetensors"
weights.write_bytes(b"safe")
(source / "linked").symlink_to(outside, target_is_directory=True)
weights.chmod(0o444)
source.chmod(0o555)
with pytest.raises(RuntimeContractError, match="symbolic links") as error:
inspect_source(source)
assert error.value.code == "LMS_SOURCE_BOUNDARY_VIOLATION"
def test_source_inspection_enforces_byte_bounds_before_hashing(tmp_path):
source = tmp_path / "source"
source.mkdir()
weights = source / "model.safetensors"
tokenizer = source / "tokenizer.json"
weights.write_bytes(b"12345")
tokenizer.write_bytes(b"{}")
weights.chmod(0o444)
tokenizer.chmod(0o444)
source.chmod(0o555)
with pytest.raises(RuntimeContractError, match="configured byte bound") as error:
inspect_source(source, max_file_bytes=4, max_total_bytes=10)
assert error.value.code == "LMS_RESOURCE_ADMISSION_DENIED"
def test_source_inspection_enforces_exact_file_and_total_bounds(tmp_path):
source = tmp_path / "source"
source.mkdir()
header = json.dumps(
{"weight": {"dtype": "F32", "shape": [1], "data_offsets": [0, 4]}},
separators=(",", ":"),
).encode()
weights = source / "model.safetensors"
tokenizer = source / "tokenizer.json"
weights.write_bytes(len(header).to_bytes(8, "little") + header + b"\0" * 4)
tokenizer.write_text("{}", encoding="utf-8")
total = weights.stat().st_size + tokenizer.stat().st_size
weights.chmod(0o444)
tokenizer.chmod(0o444)
source.chmod(0o555)
assert (
inspect_source(source, max_files=2, max_total_bytes=total, max_file_bytes=total).file_count
== 2
)
with pytest.raises(ContractError, match="file count"):
inspect_source(source, max_files=1, max_total_bytes=total, max_file_bytes=total)
with pytest.raises(ContractError, match="total-byte"):
inspect_source(
source,
max_files=2,
max_total_bytes=total - 1,
max_file_bytes=total - 1,
)
def test_source_inspection_timeout_is_deterministic_before_io(tmp_path, monkeypatch):
source = tmp_path / "source"
source.mkdir()
source.chmod(0o555)
moments = iter((0.0, 0.0, 2.0))
monkeypatch.setattr("obliteratus.distributed.preflight.time.monotonic", lambda: next(moments))
with pytest.raises(RuntimeContractError, match="explicit timeout") as error:
inspect_source(source, timeout_seconds=1)
assert error.value.code == "LMS_STAGE_TIMEOUT"
@pytest.mark.parametrize("timeout", [True, 0, 3601])
def test_source_inspection_rejects_invalid_timeout_contract(tmp_path, timeout):
with pytest.raises(RuntimeContractError, match="timeout_seconds"):
inspect_source(tmp_path, timeout_seconds=timeout)
def test_source_inspection_rejects_path_permissions_bounds_and_aliases(tmp_path):
missing = tmp_path / "missing"
with pytest.raises(RuntimeContractError, match="local non-symlink"):
preflight._inspect_source(
missing,
max_files=1,
max_total_bytes=1,
max_file_bytes=1,
timeout_seconds=1,
)
mutable = tmp_path / "mutable"
mutable.mkdir()
with pytest.raises(RuntimeContractError, match="directory must be immutable"):
preflight._inspect_source(
mutable,
max_files=1,
max_total_bytes=1,
max_file_bytes=1,
timeout_seconds=1,
)
empty = tmp_path / "empty"
empty.mkdir()
empty.chmod(0o555)
with pytest.raises(RuntimeContractError, match="file count"):
preflight._inspect_source(
empty,
max_files=1,
max_total_bytes=1,
max_file_bytes=1,
timeout_seconds=1,
)
aliased = tmp_path / "aliased"
aliased.mkdir()
first = aliased / "model.safetensors"
first.write_bytes(b"safe")
(aliased / "copy.safetensors").hardlink_to(first)
first.chmod(0o444)
aliased.chmod(0o555)
with pytest.raises(RuntimeContractError, match="hard-link aliases"):
preflight._inspect_source(
aliased,
max_files=2,
max_total_bytes=8,
max_file_bytes=4,
timeout_seconds=1,
)
@pytest.mark.parametrize(
("kwargs", "message"),
[
({"max_files": True}, "max_files"),
({"max_total_bytes": 0}, "max_total_bytes"),
({"max_file_bytes": 3, "max_total_bytes": 2}, "cannot exceed"),
],
)
def test_source_inspection_rejects_invalid_resource_bounds(tmp_path, kwargs, message):
source = tmp_path / "source"
source.mkdir()
source.chmod(0o555)
values = {
"max_files": 1,
"max_total_bytes": 1,
"max_file_bytes": 1,
"timeout_seconds": 1,
**kwargs,
}
with pytest.raises(RuntimeContractError, match=message):
preflight._inspect_source(source, **values)
def test_source_inspection_hard_deadline_interrupts_the_structural_inspector(tmp_path, monkeypatch):
source = tmp_path / "source"
source.mkdir()
weights = source / "model.safetensors"
tokenizer = source / "tokenizer.json"
weights.write_bytes(b"bounded")
tokenizer.write_text("{}", encoding="utf-8")
weights.chmod(0o444)
tokenizer.chmod(0o444)
source.chmod(0o555)
def block(*args, **kwargs):
time.sleep(5)
raise AssertionError("deadline did not interrupt the inspector")
monkeypatch.setattr("obliteratus.distributed.preflight.inspect_checkpoint", block)
started = time.monotonic()
with pytest.raises(RuntimeContractError) as error:
inspect_source(source, timeout_seconds=1)
assert error.value.code == "LMS_STAGE_TIMEOUT"
assert time.monotonic() - started < 2
def test_checkout_commit_resolves_a_worktree_reference_without_a_child_process(tmp_path):
checkout = tmp_path / "checkout"
git_dir = tmp_path / "common" / "worktrees" / "candidate"
common = tmp_path / "common"
reference = common / "refs" / "heads" / "candidate"
checkout.mkdir()
git_dir.mkdir(parents=True)
reference.parent.mkdir(parents=True)
(checkout / ".git").write_text(f"gitdir: {git_dir}\n", encoding="utf-8")
(git_dir / "HEAD").write_text("ref: refs/heads/candidate\n", encoding="utf-8")
(git_dir / "commondir").write_text("../..\n", encoding="utf-8")
reference.write_text("a" * 40 + "\n", encoding="utf-8")
assert checkout_commit(checkout) == "a" * 40
def test_checkout_commit_resolves_detached_and_packed_references(tmp_path):
detached = tmp_path / "detached"
git_dir = detached / ".git"
git_dir.mkdir(parents=True)
(git_dir / "HEAD").write_text("a" * 40 + "\n", encoding="utf-8")
assert checkout_commit(detached) == "a" * 40
packed_checkout = tmp_path / "packed"
packed_checkout.mkdir()
packed_git = tmp_path / "packed-git"
packed_git.mkdir()
(packed_checkout / ".git").write_text("gitdir: ../packed-git\n", encoding="utf-8")
(packed_git / "HEAD").write_text("ref: refs/heads/main\n", encoding="utf-8")
(packed_git / "packed-refs").write_text(
"# pack-refs with: peeled\n" + "b" * 40 + " refs/heads/main\n",
encoding="utf-8",
)
assert checkout_commit(packed_checkout) == "b" * 40
@pytest.mark.parametrize(
("head", "message"),
[
("detached-but-invalid", "HEAD has an invalid format"),
("ref: ../outside", "reference has an invalid format"),
],
)
def test_checkout_commit_rejects_invalid_head_forms(tmp_path, head, message):
checkout = tmp_path / "checkout"
git_dir = checkout / ".git"
git_dir.mkdir(parents=True)
(git_dir / "HEAD").write_text(head + "\n", encoding="utf-8")
with pytest.raises(ContractError, match=message):
checkout_commit(checkout)
def test_checkout_code_digest_rejects_empty_or_symlinked_package(tmp_path):
package = tmp_path / "obliteratus"
package.mkdir()
with pytest.raises(RuntimeContractError, match="inventory is empty"):
checkout_code_digest(tmp_path)
target = tmp_path / "target.py"
target.write_text("VALUE = 1\n", encoding="utf-8")
(package / "linked.py").symlink_to(target)
with pytest.raises(RuntimeContractError, match="regular Python files"):
checkout_code_digest(tmp_path)
def test_checkout_code_digest_changes_with_executable_source(tmp_path):
package = tmp_path / "obliteratus"
package.mkdir()
module = package / "module.py"
module.write_text("VALUE = 1\n", encoding="utf-8")
first = checkout_code_digest(tmp_path)
module.write_text("VALUE = 2\n", encoding="utf-8")
assert checkout_code_digest(tmp_path) != first
@pytest.mark.parametrize("file_count", (1, 600))
def test_checkout_code_digest_preserves_canonical_inventory_identity(tmp_path, file_count):
package = tmp_path / "obliteratus"
package.mkdir()
for index in reversed(range(file_count)):
(package / f"module_{index:04d}.py").write_bytes(b"x")
records = [
{
"path": f"obliteratus/module_{index:04d}.py",
"size_bytes": 1,
"sha256": hashlib.sha256(b"x").hexdigest(),
}
for index in range(file_count)
]
payload = json.dumps(records, sort_keys=True, separators=(",", ":")).encode("utf-8")
if file_count == 600:
assert len(payload) > 64 * 1024
first = checkout_code_digest(tmp_path)
assert first == hashlib.sha256(payload).hexdigest()
last = package / f"module_{file_count - 1:04d}.py"
last.write_bytes(b"y")
changed = checkout_code_digest(tmp_path)
assert changed != first
last.rename(package / "renamed.py")
assert checkout_code_digest(tmp_path) != changed
def test_storage_mount_digest_is_measured_and_stable(tmp_path):
assert storage_mount_digest(tmp_path) == storage_mount_digest(tmp_path)
def test_storage_mount_digest_rejects_non_directory(tmp_path):
with pytest.raises(RuntimeContractError, match="mount identity is unavailable"):
storage_mount_digest(tmp_path / "missing")
def test_revalidate_source_returns_exact_identity_and_rejects_change(monkeypatch):
expected = preflight.SourceIdentity("1" * 64, "2" * 64, "3" * 64, 2, 8)
config = SimpleNamespace(
source_path=Path("/source"),
max_source_files=2,
max_source_bytes=8,
max_source_file_bytes=4,
source_timeout_seconds=1,
)
monkeypatch.setattr(preflight, "inspect_source", lambda *_args, **_kwargs: expected)
assert preflight.revalidate_source(config, expected) is expected
changed = replace(expected, total_bytes=7)
monkeypatch.setattr(preflight, "inspect_source", lambda *_args, **_kwargs: changed)
with pytest.raises(RuntimeContractError, match="changed after") as error:
preflight.revalidate_source(config, expected)
assert error.value.code == "LMS_SOURCE_CHANGED"
def test_host_and_library_version_probes_are_bounded(monkeypatch):
total, free = preflight._host_memory()
assert total > 0 and free >= 0
monkeypatch.setattr(preflight.torch.cuda.nccl, "version", lambda: (2, 28, 3))
assert preflight._nccl_version() == "2.28.3"
monkeypatch.setattr(preflight.torch.cuda.nccl, "version", lambda: 22803)
assert preflight._nccl_version() == "22803"
monkeypatch.setattr(
preflight.torch.cuda.nccl,
"version",
lambda: (_ for _ in ()).throw(RuntimeError("unavailable")),
)
assert preflight._nccl_version() == "unavailable"
class DriverLibrary:
@staticmethod
def cuInit(_flags):
return 0
@staticmethod
def cuDriverGetVersion(value):
value._obj.value = 13000
return 0
monkeypatch.setattr(preflight.ctypes, "CDLL", lambda _name: DriverLibrary())
assert preflight._cuda_driver_version() == "13000"
def test_network_interface_requires_allowlisted_address_and_coordinator_binding(
monkeypatch,
):
addresses = {"eth0": [SimpleNamespace(family=socket.AF_INET, address="10.10.0.10")]}
monkeypatch.setattr("psutil.net_if_addrs", lambda: addresses)
validate_network_interface("eth0", ("10.10.0.0/24",), "10.10.0.10", coordinator=True)
with pytest.raises(ContractError, match="not bound"):
validate_network_interface("eth0", ("10.10.0.0/24",), "10.10.0.11", coordinator=True)
def _terminal_failure_evidence():
from obliteratus.distributed.evidence import PreflightEvidence
return PreflightEvidence.failure(
run_id="1" * 32,
config_digest="2" * 64,
code="LMS_RESOURCE_ADMISSION_DENIED",
world_size=2,
evidence_tier="protocol_cpu",
)
def test_noncoordinator_waits_through_an_unsettled_evidence_publication(tmp_path, monkeypatch):
"""Issue #305: a peer polling while the coordinator links then unlinks.
Before the fix the peer surfaced a raw ContractError without a code.
"""
from obliteratus.distributed.evidence import write_evidence
evidence = _terminal_failure_evidence()
path = tmp_path / "preflight.json"
write_evidence(path, evidence)
in_flight = tmp_path / ".preflight.json.tmp.publisher"
os.link(path, in_flight)
sleeps = []
def coordinator_finishes_publication(seconds):
sleeps.append(seconds)
in_flight.unlink(missing_ok=True)
monkeypatch.setattr(preflight.time, "sleep", coordinator_finishes_publication)
assert preflight._wait_for_evidence(path, timeout_seconds=5) == evidence
assert sleeps == [0.01]
def test_evidence_wait_fails_closed_with_a_stable_code(tmp_path):
from obliteratus.distributed.evidence import write_evidence
path = tmp_path / "preflight.json"
write_evidence(path, _terminal_failure_evidence())
os.link(path, tmp_path / "never-settles")
with pytest.raises(RuntimeContractError, match="within its bound") as pending:
preflight._wait_for_evidence(path, timeout_seconds=0)
assert pending.value.code == "LMS_EVIDENCE_UNAVAILABLE"
malformed = tmp_path / "malformed.json"
malformed.write_bytes(b"{}\n")
malformed.chmod(0o600)
with pytest.raises(RuntimeContractError, match="could not be read") as invalid:
preflight._wait_for_evidence(malformed, timeout_seconds=5)
assert invalid.value.code == "LMS_EVIDENCE_UNAVAILABLE"
assert invalid.value.__cause__ is None