Files
OBLITERATUS/tests/test_distributed_model_session_contracts.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

1171 lines
43 KiB
Python

"""Single-process fail-closed contracts for the distributed model-session orchestrator.
Complements tests/test_distributed_model_session.py: every closed-schema refusal,
bounded ownership primitive (watchdog, supervisor, NCCL teardown), binding derivation,
consensus record validation and error translation is exercised with injected process
groups so no real Gloo/NCCL group or CUDA device is required.
"""
from __future__ import annotations
import json
import os
import threading
from contextlib import contextmanager, nullcontext
from dataclasses import asdict, replace
from types import SimpleNamespace
import pytest
import torch
import obliteratus.distributed.model_session as session_module
from obliteratus.distributed.contracts import (
AgreedTopologyRefusal,
ContractError,
RuntimeContractError,
StageMessage,
canonical_record,
contract_digest,
)
from obliteratus.distributed.model_evidence import LoadEvidenceSummary
from obliteratus.distributed.model_session import (
DataPlane,
EXTERNAL_LOAD_BINDING_FIELDS,
ExactLoadProfile,
LiveModelSession,
ModelSessionResult,
ProcessHardWatchdog,
execute_model_session,
exact_nccl_data_plane,
live_model_session,
supervise_model_session_entry,
translate_model_session_error,
)
from obliteratus.distributed.model_topology import TP2DeploymentLayout
from obliteratus.distributed.preflight import SourceIdentity, SourceMountAttestation
from tests.test_distributed_model_session import (
DIGEST,
RUN_ID,
_Manifest,
_bindings,
_digest,
_external_bindings,
_protocol_mount,
)
pytestmark = pytest.mark.cpu
@pytest.fixture(autouse=True)
def _local_votes(monkeypatch):
monkeypatch.setattr(session_module, "unanimous_vote", lambda _sequence, accepted: accepted)
monkeypatch.setattr(
session_module, "gather_failure_codes", lambda _sequence, code: (code,) if code else (),
)
@pytest.mark.parametrize("local_failure", [False, True])
@pytest.mark.parametrize(("failure", "code"), [
(TimeoutError("private timeout"), "LMS_LOAD_TIMEOUT"),
(RuntimeContractError("LMS_SOURCE_CHANGED", "private source error"), "LMS_SOURCE_CHANGED"),
])
def test_session_preparation_preserves_asymmetric_refusal(monkeypatch, local_failure, failure, code):
def vote(sequence, local_code):
assert sequence == 40
assert local_code == (code if local_failure else None)
return (code,)
monkeypatch.setattr(session_module, "gather_failure_codes", vote)
with pytest.raises(RuntimeContractError) as error:
with session_module._local_step(40, code="LMS_LOAD_PROFILE_MISMATCH"):
if local_failure:
raise failure
assert error.value.code == code
assert "private" not in str(error.value)
def _exact(**overrides: object) -> ExactLoadProfile:
values: dict[str, object] = {
"schema_version": 1,
"evidence_tier": "protocol_cpu",
"profile_state": "candidate_unaccepted",
"residual_risk_accepted": False,
"transport_security": "none_claimed",
"session_timeout_seconds": 5,
"bindings": _external_bindings(),
"source_mount": _protocol_mount(),
}
values.update(overrides)
return ExactLoadProfile(**values)
def _gpu_mount(tier: str = "candidate_local_gpu") -> SourceMountAttestation:
return SourceMountAttestation(
schema_version=1,
evidence_tier=tier,
mechanism="read_only_content_addressed",
source_digest=DIGEST,
mount_digest=DIGEST,
content_root_digest=DIGEST,
external_evidence_digest=DIGEST,
read_only=True,
content_addressed=True,
production_accepted=False,
)
# ---------------------------------------------------------------------------
# Closed value objects
# ---------------------------------------------------------------------------
@pytest.mark.parametrize(
("overrides", "message"),
[
({"schema_version": 2}, "schema_version must be 1"),
({"evidence_tier": "production"}, "evidence tier is invalid"),
({"session_timeout_seconds": True}, "timeout is outside"),
({"session_timeout_seconds": 0}, "timeout is outside"),
({"session_timeout_seconds": 21_601}, "timeout is outside"),
({"source_mount": object()}, "mount attestation is invalid"),
({"evidence_tier": "candidate_local_gpu"}, "tiers disagree"),
({"bindings": _external_bindings(api_digest="A" * 64)}, "invalid digest"),
({"bindings": ["api_digest"]}, "closed schema"),
],
)
def test_exact_profile_refuses_out_of_contract_fields(overrides, message):
with pytest.raises(ContractError, match=message):
_exact(**overrides)
def test_exact_profile_freezes_a_detached_copy_of_its_bindings():
source = _external_bindings()
profile = _exact(bindings=source)
source["api_digest"] = "0" * 64
assert profile.bindings["api_digest"] == _digest("api_digest")
with pytest.raises(TypeError):
profile.bindings["api_digest"] = "1" * 64 # type: ignore[index]
def test_data_plane_and_session_result_require_digests():
with pytest.raises(ContractError, match="data plane digest"):
DataPlane(object(), object(), "not-a-digest")
with pytest.raises(ContractError, match="invalid digest"):
ModelSessionResult(
preflight=SimpleNamespace(),
summary_set_digest=DIGEST,
summaries=(),
loaded_messages=(),
logical_manifest_digest="X" * 64,
local_manifest_digest=DIGEST,
)
def _live_session(**overrides: object) -> LiveModelSession:
values: dict[str, object] = {
"preflight": SimpleNamespace(),
"config": SimpleNamespace(),
"launch": SimpleNamespace(),
"handle": object(),
"control_group": object(),
"data_plane": DataPlane(object(), object(), DIGEST),
"bindings": _bindings(),
"summary_set_digest": DIGEST,
"summaries": (),
"loaded_messages": (),
}
values.update(overrides)
return LiveModelSession(**values)
def test_live_session_capability_requires_complete_digest_bindings():
session = _live_session()
with pytest.raises(TypeError):
session.bindings["api_digest"] = DIGEST # type: ignore[index]
with pytest.raises(ContractError, match="invalid summary digest"):
_live_session(summary_set_digest="short")
bindings = _bindings()
bindings.pop("api_digest")
with pytest.raises(ContractError, match="do not match the evidence schema"):
_live_session(bindings=bindings)
with pytest.raises(ContractError, match="invalid binding digest"):
_live_session(bindings=_bindings(api_digest="zz"))
# ---------------------------------------------------------------------------
# Watchdog, supervisor and teardown ownership primitives
# ---------------------------------------------------------------------------
@pytest.mark.parametrize("timeout", [True, 0, -1, 21_601, "5"])
def test_watchdog_timeout_is_bounded(timeout):
with pytest.raises(ContractError, match="outside its bound"):
ProcessHardWatchdog(timeout)
def test_watchdog_is_single_use_and_quiet_when_work_finishes_in_time():
exits: list[int] = []
watchdog = ProcessHardWatchdog(5, exit_process=exits.append)
with watchdog:
pass
assert watchdog.fired is False
assert exits == []
with pytest.raises(ContractError, match="cannot be reused"):
watchdog.__enter__()
def test_watchdog_exits_hard_when_its_supervisor_thread_cannot_be_joined():
exits: list[int] = []
watchdog = ProcessHardWatchdog(5, exit_process=exits.append)
watchdog.__enter__()
real_thread = watchdog._thread
watchdog._thread = SimpleNamespace(join=lambda _timeout: None, is_alive=lambda: True)
try:
with pytest.raises(RuntimeContractError) as failure:
watchdog.__exit__(None, None, None)
finally:
real_thread.join(1)
assert failure.value.code == "LMS_CLEANUP_INCOMPLETE"
assert exits == [70]
assert not real_thread.is_alive()
class _Exited(BaseException):
def __init__(self, code: int) -> None:
super().__init__(code)
self.code = code
@pytest.mark.parametrize(
("entrypoint", "code"),
[
(lambda: None, 0),
(lambda: (_ for _ in ()).throw(RuntimeError("secret traceback text")), 70),
],
)
def test_supervised_child_exit_code_never_carries_exception_text(monkeypatch, entrypoint, code):
def hard_exit(value):
raise _Exited(value)
monkeypatch.setattr(session_module.os, "_exit", hard_exit)
with pytest.raises(_Exited) as exited:
session_module._run_supervised_entry(entrypoint)
assert exited.value.code == code
@pytest.mark.parametrize(
("entrypoint", "kwargs", "message"),
[
(lambda: None, {"timeout_seconds": 0}, "supervisor timeout"),
(lambda: None, {"timeout_seconds": True}, "supervisor timeout"),
(
lambda: None,
{"timeout_seconds": 1, "termination_timeout_seconds": 61},
"supervisor termination timeout",
),
("not-callable", {"timeout_seconds": 1}, "must be callable"),
],
)
def test_supervisor_arguments_are_bounded(entrypoint, kwargs, message):
with pytest.raises(ContractError, match=message):
supervise_model_session_entry(entrypoint, **kwargs)
class _FakeProcess:
def __init__(self, *, alive_after_terminate=False, exitcode=0, events=None):
self.events = [] if events is None else events
self.alive_after_terminate = alive_after_terminate
self.exitcode = exitcode
self.hung = alive_after_terminate
def start(self):
self.events.append("start")
def join(self, timeout):
self.events.append(("join", timeout))
def is_alive(self):
if not self.hung:
return False
return "kill" not in self.events
def terminate(self):
self.events.append("terminate")
def kill(self):
self.events.append("kill")
def _install_process(monkeypatch, process):
monkeypatch.setattr(
session_module.multiprocessing,
"get_context",
lambda method: SimpleNamespace(Process=lambda **_kwargs: process),
)
def test_supervisor_kills_a_worker_that_ignores_termination(monkeypatch):
process = _FakeProcess(alive_after_terminate=True)
_install_process(monkeypatch, process)
with pytest.raises(RuntimeContractError) as failure:
supervise_model_session_entry(
lambda: None, timeout_seconds=2, termination_timeout_seconds=3
)
assert failure.value.code == "LMS_LOAD_STATE_UNCERTAIN"
assert process.events == [
"start", ("join", 2.0), "terminate", ("join", 3.0), "kill", ("join", 3.0),
]
def test_supervisor_reports_unclean_or_clean_worker_exit(monkeypatch):
_install_process(monkeypatch, _FakeProcess(exitcode=70))
with pytest.raises(RuntimeContractError, match="did not exit cleanly"):
supervise_model_session_entry(lambda: None, timeout_seconds=1)
clean = _FakeProcess(exitcode=0)
_install_process(monkeypatch, clean)
assert supervise_model_session_entry(lambda: None, timeout_seconds=1) is None
assert clean.events == ["start", ("join", 1.0)]
def test_supervisor_contains_spawn_failures(monkeypatch):
def broken(_method):
raise OSError("spawn failed: /private/socket")
monkeypatch.setattr(session_module.multiprocessing, "get_context", broken)
with pytest.raises(RuntimeContractError) as failure:
supervise_model_session_entry(lambda: None, timeout_seconds=1)
assert failure.value.code == "LMS_LOAD_STATE_UNCERTAIN"
assert "private" not in str(failure.value)
def test_data_group_teardown_failure_is_cleanup_incomplete(monkeypatch):
import torch.distributed as dist
def failing(_group):
raise RuntimeError("NCCL communicator abort")
monkeypatch.setattr(dist, "destroy_process_group", failing)
exits: list[int] = []
with pytest.raises(RuntimeContractError, match="teardown failed") as failure:
session_module._bounded_destroy_data_group(
object(), timeout_seconds=1, exit_process=exits.append
)
assert failure.value.code == "LMS_CLEANUP_INCOMPLETE"
assert exits == []
def test_hung_data_group_teardown_exits_hard(monkeypatch):
import torch.distributed as dist
release = threading.Event()
monkeypatch.setattr(dist, "destroy_process_group", lambda _group: release.wait(5))
exits: list[int] = []
try:
with pytest.raises(RuntimeContractError, match="exceeded its bound"):
session_module._bounded_destroy_data_group(
object(), timeout_seconds=0.05, exit_process=exits.append
)
finally:
release.set()
assert exits == [70]
# ---------------------------------------------------------------------------
# NCCL data-plane and Gloo control-group identity (injected torch.distributed)
# ---------------------------------------------------------------------------
def _nccl_config():
return SimpleNamespace(
device_kind="cuda",
world_size=2,
local_world_size=2,
tensor_parallel_size=2,
coordinator_rank=0,
teardown_timeout_seconds=1,
)
def _nccl_launch(rank=0):
return SimpleNamespace(
rank=rank, local_rank=rank, world_size=2, local_world_size=2, group_rank=0
)
@pytest.fixture
def fake_dist(monkeypatch):
import torch.distributed as dist
import torch.distributed.device_mesh as device_mesh_module
state = SimpleNamespace(
initialized=True,
backend="gloo",
group_backend="nccl",
ranks=[0, 1],
destroyed=[],
group=object(),
mesh=None,
init_error=None,
)
class Mesh:
ndim = 1
device_type = "cuda"
mesh = torch.tensor([0, 1])
def size(self):
return 2
def get_group(self, _name):
return state.group
def get_local_rank(self, _name):
return 0
def initialize(*_args, **_kwargs):
if state.init_error is not None:
raise state.init_error
state.mesh = Mesh()
return state.mesh
monkeypatch.setattr(dist, "is_initialized", lambda: state.initialized)
monkeypatch.setattr(
dist,
"get_backend",
lambda selected=None: state.backend if selected is None else state.group_backend,
)
monkeypatch.setattr(dist, "get_process_group_ranks", lambda _group: list(state.ranks))
monkeypatch.setattr(dist, "destroy_process_group", state.destroyed.append)
monkeypatch.setattr(device_mesh_module, "init_device_mesh", initialize)
return state
def test_data_plane_refuses_unclosed_topology_before_any_group_work(fake_dist):
config = replace_namespace(_nccl_config(), world_size=4)
with pytest.raises(RuntimeContractError) as failure:
with exact_nccl_data_plane(config, _nccl_launch(), exit_process=lambda _c: None):
pytest.fail("refused topology must not yield")
assert failure.value.code == "LMS_LOAD_PROFILE_MISMATCH"
assert fake_dist.mesh is None and fake_dist.destroyed == []
def test_data_plane_requires_the_live_gloo_control_group(fake_dist, monkeypatch):
votes = []
monkeypatch.setattr(
session_module, "gather_failure_codes",
lambda sequence, code: votes.append((sequence, code is None)) or ((code,) if code else ()),
)
fake_dist.backend = "nccl"
with pytest.raises(RuntimeContractError) as failure:
with exact_nccl_data_plane(_nccl_config(), _nccl_launch(), exit_process=lambda _c: None):
pytest.fail("must not yield")
assert failure.value.code == "LMS_LOAD_PROFILE_MISMATCH"
assert str(failure.value) == "distributed model session failed"
assert votes == [(50, False)]
assert fake_dist.mesh is None and fake_dist.destroyed == []
def test_data_plane_identity_drift_is_refused_and_the_group_destroyed(fake_dist, monkeypatch):
votes = []
monkeypatch.setattr(
session_module, "gather_failure_codes",
lambda sequence, code: votes.append((sequence, code is None)) or ((code,) if code else ()),
)
fake_dist.ranks = [1, 0]
with pytest.raises(RuntimeContractError) as failure:
with exact_nccl_data_plane(_nccl_config(), _nccl_launch(), exit_process=lambda _c: None):
pytest.fail("must not yield")
assert failure.value.code == "LMS_LOAD_PROFILE_MISMATCH"
assert str(failure.value) == "distributed model session failed"
assert votes == [(50, True), (51, False)]
assert fake_dist.destroyed == [fake_dist.group]
@pytest.mark.parametrize("sequence", [50, 51])
def test_data_plane_peer_refusal_stops_preparation_and_cleans_up(fake_dist, monkeypatch, sequence):
votes = []
def vote(current, code):
votes.append((current, code is None))
return ("LMS_LOAD_PROFILE_MISMATCH",) if current == sequence else ()
monkeypatch.setattr(session_module, "gather_failure_codes", vote)
with pytest.raises(RuntimeContractError) as failure:
with exact_nccl_data_plane(_nccl_config(), _nccl_launch(), exit_process=lambda _c: None):
pytest.fail("must not yield")
assert failure.value.code == "LMS_LOAD_PROFILE_MISMATCH"
if sequence == 50:
assert votes == [(50, True)]
assert fake_dist.mesh is None and fake_dist.destroyed == []
else:
assert votes == [(50, True), (51, True)]
assert fake_dist.destroyed == [fake_dist.group]
def test_data_plane_initialization_failure_is_redacted(fake_dist):
fake_dist.init_error = RuntimeError("NCCL WARN /dev/shm/private")
with pytest.raises(RuntimeContractError) as failure:
with exact_nccl_data_plane(_nccl_config(), _nccl_launch(), exit_process=lambda _c: None):
pytest.fail("must not yield")
assert failure.value.code == "LMS_MODEL_LOAD_FAILED"
assert "private" not in str(failure.value)
assert fake_dist.destroyed == []
def test_data_plane_digest_binds_the_layout(fake_dist):
with exact_nccl_data_plane(
_nccl_config(), _nccl_launch(), exit_process=lambda _c: None
) as plane:
digest = plane.digest
assert digest == contract_digest(
{
"backend": "nccl",
"device_type": "cuda",
"mesh_dim_names": ["tp"],
"ranks": [0, 1],
"world_size": 2,
"deployment_layout": TP2DeploymentLayout.LOCAL_TP2.value,
}
)
assert fake_dist.destroyed == [fake_dist.group]
def replace_namespace(namespace, **changes):
return SimpleNamespace(**{**vars(namespace), **changes})
@pytest.fixture
def control_dist(monkeypatch):
import torch.distributed as dist
state = SimpleNamespace(initialized=True, backend="gloo", rank=1, world=2, ranks=[0, 1])
monkeypatch.setattr(dist, "is_initialized", lambda: state.initialized)
monkeypatch.setattr(dist, "get_backend", lambda *_args: state.backend)
monkeypatch.setattr(dist, "get_rank", lambda *_args: state.rank)
monkeypatch.setattr(dist, "get_world_size", lambda *_args: state.world)
monkeypatch.setattr(dist, "get_process_group_ranks", lambda _group: list(state.ranks))
return state
def test_control_group_identity_binds_the_gloo_world(control_dist):
import torch.distributed as dist
group, digest = session_module._control_group_identity(SimpleNamespace(rank=1, world_size=2))
assert group is dist.group.WORLD
assert digest == contract_digest({"backend": "gloo", "ranks": [0, 1], "world_size": 2})
@pytest.mark.parametrize(
("change", "message"),
[
({"initialized": False}, "identity changed"),
({"backend": "nccl"}, "identity changed"),
({"rank": 0}, "identity changed"),
({"world": 3}, "identity changed"),
({"ranks": [1, 0]}, "rank order changed"),
],
)
def test_control_group_identity_refuses_drift(control_dist, change, message):
for name, value in change.items():
setattr(control_dist, name, value)
with pytest.raises(RuntimeContractError, match=message) as failure:
session_module._control_group_identity(SimpleNamespace(rank=1, world_size=2))
assert failure.value.code == "LMS_LOAD_PROFILE_MISMATCH"
# ---------------------------------------------------------------------------
# Binding derivation
# ---------------------------------------------------------------------------
def _preflight(source=None):
source = source or SourceIdentity(DIGEST, _digest("model"), _digest("tokenizer"), 2, 128)
return SimpleNamespace(
topology={"backend": "gloo", "ranks": [0, 1]},
attestations=(
SimpleNamespace(rank=0, device_config_digest=_digest("dc0"),
device_profile_digest=_digest("dp0")),
SimpleNamespace(rank=1, device_config_digest=_digest("dc1"),
device_profile_digest=_digest("dp1")),
),
identity_digest=_digest("identity"),
lifecycle_identity_digest=_digest("lifecycle"),
source_identity=source,
)
def _session_config(**overrides: object):
selection = SimpleNamespace(hf_config_digest=_digest("model-config"))
values: dict[str, object] = {
"run_id": RUN_ID,
"world_size": 2,
"device_kind": "cpu",
"evidence_tier": "protocol_cpu",
"source_digest": DIGEST,
"code_digest": _digest("code"),
"digest": _digest("config"),
"placement_plan_digest": _digest("plan"),
"teardown_timeout_seconds": 1,
"require_model": lambda: selection,
}
values.update(overrides)
return SimpleNamespace(**values)
def _preload(config, exact, preflight=None):
return session_module._validate_preload_bindings(
config,
SimpleNamespace(rank=0),
preflight or _preflight(),
exact,
source_mount_digest=_digest("mount"),
control_group_digest=_digest("control"),
)
def test_protocol_preload_bindings_are_derived_from_preflight():
preflight = _preflight()
bindings = _preload(_session_config(), _exact(), preflight)
assert bindings["deployment_layout_digest"] == contract_digest(
{"layout": "protocol_cpu_noncoverage", "topology": preflight.topology}
)
assert bindings["hardware_digest"] == session_module._hardware_digest(
preflight, _digest("mount")
)
assert bindings["model_digest"] == preflight.source_identity.model_digest
assert bindings["topology_digest"] == contract_digest(preflight.topology)
def test_protocol_profile_refuses_a_gpu_preflight():
with pytest.raises(RuntimeContractError, match="protocol evidence tier disagrees"):
_preload(_session_config(device_kind="cuda"), _exact())
def test_gpu_profile_refuses_a_cpu_preflight():
exact = _exact(evidence_tier="candidate_local_gpu", source_mount=_gpu_mount())
with pytest.raises(RuntimeContractError, match="GPU evidence tier disagrees with preflight"):
_preload(_session_config(), exact)
def test_gpu_profile_requires_an_observable_runtime_identity(monkeypatch):
def unavailable():
raise ContractError("critical API module origin is unavailable")
monkeypatch.setattr(session_module, "observed_external_runtime_bindings", unavailable)
exact = _exact(evidence_tier="candidate_local_gpu", source_mount=_gpu_mount())
config = _session_config(device_kind="cuda", evidence_tier="candidate_preflight")
with pytest.raises(RuntimeContractError) as failure:
_preload(config, exact)
assert failure.value.code == "LMS_DEPENDENCY_DRIFT"
@pytest.mark.parametrize(
("tier", "layout", "accepted"),
[
("candidate_local_gpu", TP2DeploymentLayout.LOCAL_TP2, True),
("candidate_two_host_gpu", TP2DeploymentLayout.TWO_HOST_TP2, True),
("candidate_local_gpu", TP2DeploymentLayout.TWO_HOST_TP2, False),
("candidate_two_host_gpu", TP2DeploymentLayout.LOCAL_TP2, False),
],
)
def test_gpu_profile_tier_must_match_the_classified_layout(monkeypatch, tier, layout, accepted):
external = _external_bindings()
monkeypatch.setattr(
session_module,
"observed_external_runtime_bindings",
lambda: {name: external[name] for name in ("api_digest", "package_digest")},
)
topology = SimpleNamespace(layout=layout, digest=_digest(f"topology-{layout.value}"))
monkeypatch.setattr(session_module, "classify_tp2_topology", lambda *_args: topology)
exact = _exact(evidence_tier=tier, source_mount=_gpu_mount(tier), bindings=external)
config = _session_config(device_kind="cuda", evidence_tier="candidate_preflight")
if accepted:
assert _preload(config, exact)["deployment_layout_digest"] == topology.digest
else:
with pytest.raises(RuntimeContractError, match="disagrees with TP2 layout"):
_preload(config, exact)
def _handle(preflight, *, manifest=None, **overrides):
manifest = manifest or _Manifest()
values = {
"manifest": manifest,
"manifest_digest": _digest("logical-manifest"),
"local_manifest_digest": manifest.local_digest,
"plan_digest": _digest("plan"),
"source_identity": preflight.source_identity,
"config": SimpleNamespace(config_digest=_digest("model-config")),
"profile": SimpleNamespace(digest=_digest("adapter-profile")),
}
values.update(overrides)
return SimpleNamespace(**values)
def test_handle_bindings_are_derived_from_the_loaded_handle():
preflight = _preflight()
plane = DataPlane(object(), object(), _digest("data"))
bindings = session_module._validate_handle_bindings(
_session_config(), SimpleNamespace(rank=0, world_size=2), preflight,
_handle(preflight), plane,
)
assert bindings == {
"data_group_digest": plane.digest,
"logical_manifest_digest": _digest("logical-manifest"),
"model_config_digest": _digest("model-config"),
"plan_digest": _digest("plan"),
"profile_digest": _digest("adapter-profile"),
}
@pytest.mark.parametrize(
("change", "code"),
[
({"manifest": None}, "LMS_LOAD_MANIFEST_INVALID"),
({"local_manifest_digest": _digest("other-local")}, "LMS_LOAD_MANIFEST_INVALID"),
({"config": SimpleNamespace(config_digest=_digest("drift"))}, "LMS_LOAD_MANIFEST_INVALID"),
(
{"source_identity": SourceIdentity(DIGEST, DIGEST, DIGEST, 1, 1)},
"LMS_LOAD_MANIFEST_INVALID",
),
],
)
def test_handle_bindings_refuse_incomplete_or_drifted_handles(change, code):
preflight = _preflight()
handle = _handle(preflight)
for name, value in change.items():
setattr(handle, name, value)
with pytest.raises(RuntimeContractError) as failure:
session_module._validate_handle_bindings(
_session_config(), SimpleNamespace(rank=0, world_size=2), preflight, handle,
DataPlane(object(), object(), DIGEST),
)
assert failure.value.code == code
def test_handle_for_another_rank_is_refused():
preflight = _preflight()
with pytest.raises(RuntimeContractError, match="identity disagrees"):
session_module._validate_handle_bindings(
_session_config(), SimpleNamespace(rank=1, world_size=2), preflight,
_handle(preflight), DataPlane(object(), object(), DIGEST),
)
def test_evidence_bindings_merge_only_known_agreeing_complete_digests():
exact = _exact()
derived = {
name: value for name, value in _bindings().items()
if name not in EXTERNAL_LOAD_BINDING_FIELDS
}
merged = session_module._complete_evidence_bindings(exact, derived, {"code_digest": derived["code_digest"]})
assert dict(merged) == {**dict(exact.bindings), **derived}
with pytest.raises(RuntimeContractError, match="unknown binding"):
session_module._complete_evidence_bindings(exact, derived, {"hostname": DIGEST})
with pytest.raises(RuntimeContractError, match="derivations disagree"):
session_module._complete_evidence_bindings(exact, derived, {"code_digest": DIGEST})
partial = dict(derived)
partial.pop("code_digest")
with pytest.raises(RuntimeContractError, match="incomplete"):
session_module._complete_evidence_bindings(exact, partial)
with pytest.raises(RuntimeContractError, match="incomplete"):
session_module._complete_evidence_bindings(exact, {**derived, "code_digest": "x"})
# ---------------------------------------------------------------------------
# Consensus record parsing
# ---------------------------------------------------------------------------
def _loaded_summary(rank=0, **overrides):
values = {
"schema_version": 1,
"record_type": "lms_rank_load_summary",
"result": "loaded",
"run_id": RUN_ID,
"rank": rank,
"world_size": 2,
"evidence_tier": "protocol_cpu",
"bindings_digest": _digest("bindings"),
"private_record_digest": _digest(f"private-{rank}"),
"manifest_digest": _digest("manifest"),
"parameter_count": 1,
"buffer_count": 0,
"hook_count": 1,
"error_code": None,
}
values.update(overrides)
return LoadEvidenceSummary(**values)
def test_summary_parser_round_trips_canonical_records():
summary = _loaded_summary()
assert session_module._summary_from_bytes(canonical_record(summary)) == summary
@pytest.mark.parametrize(
("payload", "message"),
[
(b'{"rank":0,"rank":0}', "duplicate field"),
(b"\xff", "strict canonical JSON"),
(b"{", "strict canonical JSON"),
(b"[]", "closed schema"),
(b'{"rank":0}', "closed schema"),
],
)
def test_summary_parser_refuses_malformed_payloads(payload, message):
with pytest.raises(ContractError, match=message):
session_module._summary_from_bytes(payload)
def test_summary_parser_refuses_invalid_or_non_canonical_fields():
value = asdict(_loaded_summary())
value["result"] = "partial"
with pytest.raises(ContractError, match="fields are invalid"):
session_module._summary_from_bytes(json.dumps(value).encode())
spaced = json.dumps(asdict(_loaded_summary()), indent=1).encode()
with pytest.raises(ContractError, match="not canonical"):
session_module._summary_from_bytes(spaced)
def test_summary_gather_requires_one_fixed_loaded_world(monkeypatch):
local = _loaded_summary()
monkeypatch.setattr(
session_module, "gloo_all_gather_records",
lambda record: (canonical_record(record), canonical_record(record)),
)
with pytest.raises(RuntimeContractError) as failure:
session_module._gather_load_summaries(local)
assert failure.value.code == "LMS_LOAD_MANIFEST_INVALID"
def _emit(monkeypatch, gather, *, vote=True):
monkeypatch.setattr(session_module, "gloo_all_gather_records", gather)
monkeypatch.setattr(
session_module, "unanimous_vote", lambda sequence, accepted: vote if sequence == 3 else accepted,
)
return session_module._emit_loaded_messages(
SimpleNamespace(run_id=RUN_ID, world_size=2),
SimpleNamespace(rank=0),
SimpleNamespace(lifecycle_identity_digest=_digest("lifecycle")),
_digest("summary-set"),
)
def _peer(record, **changes):
return canonical_record(replace(record, rank=1, **changes))
def test_loaded_messages_are_committed_by_every_fixed_rank(monkeypatch):
messages = _emit(monkeypatch, lambda record: (canonical_record(record), _peer(record)))
assert [message.rank for message in messages] == [0, 1]
assert len({message.identity_digest for message in messages}) == 1
@pytest.mark.parametrize(
"gather",
[
lambda record: (canonical_record(record), b"not-json"),
lambda record: (canonical_record(record), _peer(record, sequence=3)),
lambda record: (canonical_record(record), _peer(record, identity_digest=DIGEST)),
lambda record: (_peer(record), canonical_record(record)),
],
)
def test_loaded_messages_refuse_invalid_or_disagreeing_peers(monkeypatch, gather):
with pytest.raises(RuntimeContractError) as failure:
_emit(monkeypatch, gather)
assert failure.value.code == "LMS_LOAD_MANIFEST_INVALID"
def test_loaded_messages_require_a_unanimous_commit_vote(monkeypatch):
with pytest.raises(RuntimeContractError, match="not unanimous"):
_emit(monkeypatch, lambda record: (canonical_record(record), _peer(record)), vote=False)
# ---------------------------------------------------------------------------
# Evidence directory and error translation
# ---------------------------------------------------------------------------
def test_rank_zero_creates_a_private_evidence_directory(monkeypatch, tmp_path):
votes: list[int] = []
monkeypatch.setattr(
torch.distributed, "all_reduce", lambda outcome, *, op: votes.append(outcome.item())
)
path = session_module._prepare_evidence_directory(tmp_path, SimpleNamespace(rank=0))
assert path == tmp_path / "model-load-evidence"
assert path.stat().st_mode & 0o777 == 0o700
assert votes == [1]
@pytest.mark.parametrize("failed_close", [None, "child", "parent", "both"])
def test_evidence_directory_closes_both_descriptors_before_voting(
monkeypatch, tmp_path, failed_close
):
opened: dict[str, int] = {}
closed: list[int] = []
votes: list[tuple[int, bool]] = []
original_open = os.open
original_close = os.close
def open_descriptor(path, *args, **kwargs):
descriptor = original_open(path, *args, **kwargs)
opened["child" if path == "model-load-evidence" else "parent"] = descriptor
return descriptor
def close_descriptor(descriptor):
closed.append(descriptor)
original_close(descriptor)
if failed_close == "both" or descriptor == opened.get(failed_close):
raise OSError("private descriptor close failure")
def vote(sequence, code):
if sequence == 44:
assert closed == [opened["child"], opened["parent"]]
else:
assert sequence == 52
assert closed == []
votes.append((sequence, code is None))
return (code,) if code else ()
def reduce(_outcome, *, op):
assert votes == [(52, True)]
monkeypatch.setattr(torch.distributed, "all_reduce", reduce)
monkeypatch.setattr(session_module.os, "open", open_descriptor)
monkeypatch.setattr(session_module.os, "close", close_descriptor)
monkeypatch.setattr(session_module, "gather_failure_codes", vote)
if failed_close is None:
assert session_module._prepare_evidence_directory(
tmp_path, SimpleNamespace(rank=0)
) == tmp_path / "model-load-evidence"
else:
with pytest.raises(RuntimeContractError) as failure:
session_module._prepare_evidence_directory(tmp_path, SimpleNamespace(rank=0))
assert failure.value.code == "LMS_LOAD_EVIDENCE_UNAVAILABLE"
assert "private" not in str(failure.value)
assert votes == [(52, True), (44, failed_close is None)]
for descriptor in opened.values():
with pytest.raises(OSError):
os.fstat(descriptor)
def test_shared_evidence_directory_is_refused(monkeypatch, tmp_path):
(tmp_path / "model-load-evidence").mkdir(mode=0o755)
(tmp_path / "model-load-evidence").chmod(0o755)
monkeypatch.setattr(torch.distributed, "all_reduce", lambda _outcome, *, op: None)
with pytest.raises(RuntimeContractError) as failure:
session_module._prepare_evidence_directory(tmp_path, SimpleNamespace(rank=1))
assert failure.value.code == "LMS_LOAD_EVIDENCE_UNAVAILABLE"
def test_missing_attempt_directory_still_votes_and_fails(monkeypatch, tmp_path):
votes: list[int] = []
monkeypatch.setattr(
torch.distributed, "all_reduce", lambda outcome, *, op: votes.append(outcome.item())
)
with pytest.raises(RuntimeContractError) as failure:
session_module._prepare_evidence_directory(tmp_path / "absent", SimpleNamespace(rank=1))
assert failure.value.code == "LMS_LOAD_EVIDENCE_UNAVAILABLE"
assert votes == [0]
@pytest.mark.parametrize(
("error", "code"),
[
(RuntimeContractError("LMS_SOURCE_CHANGED", "x"), "LMS_SOURCE_CHANGED"),
(RuntimeContractError("LMS_STAGE_TIMEOUT", "x"), "LMS_LOAD_TIMEOUT"),
(RuntimeContractError("LMS_IDENTITY_MISMATCH", "x"), "LMS_LOAD_PROFILE_MISMATCH"),
(RuntimeContractError("LMS_SOMETHING_NEW", "x"), "LMS_MODEL_LOAD_FAILED"),
(TimeoutError("private"), "LMS_LOAD_TIMEOUT"),
(KeyboardInterrupt(), "LMS_MODEL_LOAD_FAILED"),
(ValueError("private"), "LMS_MODEL_LOAD_FAILED"),
],
)
def test_error_translation_keeps_only_closed_codes(error, code):
translated = translate_model_session_error(error)
assert translated.code == code
assert str(translated) == "distributed model session failed"
def test_topology_errors_translate_to_an_explicit_unsupported_message():
translated = translate_model_session_error(
RuntimeContractError("LMS_TOPOLOGY_UNSUPPORTED", "private detail")
)
assert translated.code == "LMS_TP_PLAN_UNSUPPORTED"
assert "two-host TP2" in str(translated)
refusal = AgreedTopologyRefusal()
assert translate_model_session_error(refusal) is refusal
# ---------------------------------------------------------------------------
# live_model_session adapter selection and normal completion
# ---------------------------------------------------------------------------
def test_live_session_requires_an_exact_profile():
with pytest.raises(RuntimeContractError, match="exact model-load profile is unavailable"):
with live_model_session(_session_config(), SimpleNamespace(), object()):
pytest.fail("must not yield")
@pytest.mark.parametrize(
"injection",
[
{"protocol_loader": lambda *a, **k: None},
{"protocol_data_plane": lambda *a, **k: nullcontext()},
{"exit_process": lambda _code: None},
],
)
def test_gpu_candidate_session_refuses_test_injection(injection):
exact = _exact(evidence_tier="candidate_local_gpu", source_mount=_gpu_mount())
with pytest.raises(RuntimeContractError, match="cannot use protocol test injection"):
with live_model_session(_session_config(), SimpleNamespace(), exact, **injection):
pytest.fail("must not yield")
def test_protocol_session_requires_both_explicit_adapters():
with pytest.raises(RuntimeContractError, match="explicit non-production test adapters"):
with live_model_session(
_session_config(), SimpleNamespace(), _exact(), protocol_loader=lambda *a, **k: None
):
pytest.fail("must not yield")
def _isolate_session_ownership(monkeypatch, preflight, events):
monkeypatch.setattr(session_module, "ProcessHardWatchdog", lambda *a, **k: nullcontext())
monkeypatch.setattr(session_module, "runtime_diagnostic_guard", lambda **k: nullcontext())
@contextmanager
def control(_config, _launch):
events.append("gloo-live")
try:
yield
finally:
events.append("gloo-destroyed")
monkeypatch.setattr(session_module, "control_group", control)
monkeypatch.setattr(session_module, "run_live_preflight", lambda *_a, **_k: preflight)
monkeypatch.setattr(
session_module, "verify_source_mount_attestation", lambda *_args: _digest("mount")
)
monkeypatch.setattr(
session_module, "_control_group_identity", lambda _launch: (object(), _digest("control"))
)
def test_gpu_candidate_session_uses_the_production_nccl_plane_and_rejects_bad_adapters(
monkeypatch,
):
events: list[str] = []
preflight = _preflight()
_isolate_session_ownership(monkeypatch, preflight, events)
monkeypatch.setattr(
session_module, "_validate_preload_bindings", lambda *_a, **_k: {}
)
planes: list[object] = []
@contextmanager
def production_plane(_config, _launch, *, exit_process):
planes.append(exit_process)
yield "not-a-data-plane"
monkeypatch.setattr(session_module, "exact_nccl_data_plane", production_plane)
monkeypatch.setattr(
session_module,
"load_distributed_model",
lambda *_a, **_k: pytest.fail("an invalid data plane must not reach the loader"),
)
exact = _exact(evidence_tier="candidate_local_gpu", source_mount=_gpu_mount())
with pytest.raises(RuntimeContractError) as failure:
execute_model_session(_session_config(), SimpleNamespace(rank=0), exact)
assert failure.value.code == "LMS_LOAD_PROFILE_MISMATCH"
assert planes == [os._exit]
assert events == ["gloo-live", "gloo-destroyed"]
def test_protocol_session_completes_and_tears_down_before_returning(monkeypatch, tmp_path):
attempt = tmp_path / RUN_ID
attempt.mkdir(mode=0o700)
preflight = _preflight()
preflight.attempt_path = attempt
config = _session_config()
launch = SimpleNamespace(rank=0, world_size=2)
events: list[str] = []
_isolate_session_ownership(monkeypatch, preflight, events)
handle = _handle(preflight)
data_digest = _digest("data-group")
derived = session_module._validate_preload_bindings(
config, launch, preflight, _exact(),
source_mount_digest=_digest("mount"), control_group_digest=_digest("control"),
)
derived.update(
data_group_digest=data_digest,
logical_manifest_digest=handle.manifest_digest,
profile_digest=handle.profile.digest,
)
exact = _exact(
bindings={name: _bindings(**derived)[name] for name in EXTERNAL_LOAD_BINDING_FIELDS}
)
@contextmanager
def protocol_plane(_config, _launch, **_kwargs):
events.append("data-live")
try:
yield DataPlane(object(), object(), data_digest)
finally:
events.append("data-destroyed")
def gather(record):
if isinstance(record, LoadEvidenceSummary):
other = replace(record, rank=1, private_record_digest=_digest("rank-one"))
return canonical_record(record), canonical_record(other)
assert isinstance(record, StageMessage)
return canonical_record(record), canonical_record(replace(record, rank=1))
monkeypatch.setattr(session_module, "gloo_all_gather_records", gather)
monkeypatch.setattr(session_module, "unanimous_vote", lambda sequence, accepted: accepted)
monkeypatch.setattr(torch.distributed, "all_reduce", lambda _tensor, *, op: None)
result = execute_model_session(
config,
launch,
exact,
protocol_loader=lambda *_a, **_k: handle,
protocol_data_plane=protocol_plane,
exit_process=lambda code: pytest.fail(f"unexpected hard exit {code}"),
)
assert events == ["gloo-live", "data-live", "data-destroyed", "gloo-destroyed"]
assert isinstance(result, ModelSessionResult)
assert result.logical_manifest_digest == handle.manifest_digest
assert result.local_manifest_digest == handle.local_manifest_digest
assert [summary.rank for summary in result.summaries] == [0, 1]
assert result.summary_set_digest == session_module.validate_loaded_summary_set(
result.summaries
)
def test_pre_yield_native_timeouts_are_translated_and_owned_groups_torn_down(monkeypatch):
preflight = _preflight()
events: list[str] = []
_isolate_session_ownership(monkeypatch, preflight, events)
def hung(*_args, **_kwargs):
raise TimeoutError("private native hang detail")
monkeypatch.setattr(session_module, "_validate_preload_bindings", hung)
with pytest.raises(RuntimeContractError) as failure:
with live_model_session(
_session_config(), SimpleNamespace(rank=0), _exact(),
protocol_loader=lambda *a, **k: None,
protocol_data_plane=lambda *a, **k: nullcontext(),
):
pytest.fail("must not yield")
assert failure.value.code == "LMS_LOAD_TIMEOUT"
assert "private" not in str(failure.value)
assert events == ["gloo-live", "gloo-destroyed"]