Files
OBLITERATUS/tests/test_distributed_model_session.py
T
Joseph MaglyandClaude Opus 5.5 1b936c1dfc feat(distributed): add deterministic probe distill stages
(cherry picked from commit f9a289e267889c297cefcdc67eea92c4b8eb8d4d)

Ported for #244 (part 2/6). Internal-only content from the archived
commit was dropped: .aiwg records and the internal security-docs test.
Marked experimental in user docs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 14:00:29 -04:00

622 lines
22 KiB
Python

"""Closed orchestration and security-boundary tests for issue 60."""
from __future__ import annotations
import hashlib
import logging
import os
import time
import warnings
from contextlib import contextmanager
from dataclasses import replace
from types import SimpleNamespace
import pytest
import torch
import obliteratus.distributed.model_session as session_module
import obliteratus.distributed.preflight as preflight_module
from obliteratus.distributed.contracts import (
ContractError,
RuntimeContractError,
RuntimeStage,
StageMessage,
Vote,
canonical_record,
contract_digest,
)
from obliteratus.distributed.launcher import runtime_diagnostic_guard
from obliteratus.distributed.model_evidence import (
LOAD_EVIDENCE_BINDING_FIELDS,
LoadEvidenceSummary,
)
from obliteratus.distributed.model_session import (
DataPlane,
EXTERNAL_LOAD_BINDING_FIELDS,
ExactLoadProfile,
ProcessHardWatchdog,
exact_nccl_data_plane,
execute_model_session,
live_model_session,
supervise_model_session_entry,
)
from obliteratus.distributed.preflight import (
SourceIdentity,
SourceMountAttestation,
verify_source_mount_attestation,
)
RUN_ID = "a" * 32
DIGEST = "b" * 64
def _digest(label: str) -> str:
return hashlib.sha256(label.encode("ascii")).hexdigest()
def _bindings(**overrides: str) -> dict[str, str]:
values = {name: _digest(name) for name in LOAD_EVIDENCE_BINDING_FIELDS}
values.update(overrides)
return values
def _external_bindings(**overrides: str) -> dict[str, str]:
values = {name: _digest(name) for name in EXTERNAL_LOAD_BINDING_FIELDS}
values.update(overrides)
return values
def _protocol_mount(source_digest: str = DIGEST) -> SourceMountAttestation:
return SourceMountAttestation(
schema_version=1,
evidence_tier="protocol_cpu",
mechanism="protocol_test_injected",
source_digest=source_digest,
mount_digest=_digest("protocol-mount-noncoverage"),
content_root_digest=_digest("protocol-content-noncoverage"),
external_evidence_digest=_digest("protocol-external-noncoverage"),
read_only=False,
content_addressed=False,
production_accepted=False,
)
def test_exact_profile_is_closed_and_cannot_accept_security_or_support_claims():
profile = ExactLoadProfile(
schema_version=1,
evidence_tier="protocol_cpu",
profile_state="candidate_unaccepted",
residual_risk_accepted=False,
transport_security="none_claimed",
session_timeout_seconds=30,
bindings=_external_bindings(),
source_mount=_protocol_mount(),
)
assert set(profile.bindings) == set(EXTERNAL_LOAD_BINDING_FIELDS)
with pytest.raises(ContractError):
replace(profile, profile_state="supported")
with pytest.raises(ContractError):
replace(profile, residual_risk_accepted=True)
with pytest.raises(ContractError):
replace(profile, transport_security="tls")
with pytest.raises(ContractError):
replace(profile, bindings={**dict(profile.bindings), "version": DIGEST})
def test_protocol_mount_attestation_cannot_authorize_candidate_gpu():
preflight = SimpleNamespace(
source_identity=SourceIdentity(DIGEST, _digest("model"), _digest("tokenizer"), 1, 1)
)
config = SimpleNamespace(source_digest=DIGEST, device_kind="cuda")
with pytest.raises(RuntimeContractError) as failure:
verify_source_mount_attestation(config, preflight, _protocol_mount())
assert failure.value.code == "LMS_MODEL_ARTIFACT_UNSUPPORTED"
def test_candidate_mount_requires_observed_read_only_exact_content_root(monkeypatch, tmp_path):
mount_digest = _digest("observed-mount")
attestation = SourceMountAttestation(
schema_version=1,
evidence_tier="candidate_local_gpu",
mechanism="read_only_content_addressed",
source_digest=DIGEST,
mount_digest=mount_digest,
content_root_digest=DIGEST,
external_evidence_digest=_digest("external-mount-proof"),
read_only=True,
content_addressed=True,
production_accepted=False,
)
preflight = SimpleNamespace(
source_identity=SourceIdentity(DIGEST, _digest("model"), _digest("tokenizer"), 1, 1)
)
config = SimpleNamespace(source_digest=DIGEST, source_path=tmp_path, device_kind="cuda")
monkeypatch.setattr(
preflight_module,
"source_mount_observation",
lambda _path: (mount_digest, True, True),
)
assert verify_source_mount_attestation(config, preflight, attestation) == attestation.digest
monkeypatch.setattr(
preflight_module,
"source_mount_observation",
lambda _path: (mount_digest, False, True),
)
with pytest.raises(RuntimeContractError) as failure:
verify_source_mount_attestation(config, preflight, attestation)
assert failure.value.code == "LMS_MODEL_ARTIFACT_UNSUPPORTED"
def test_candidate_preload_recomputes_external_runtime_bindings(monkeypatch):
source = SourceIdentity(DIGEST, _digest("model"), _digest("tokenizer"), 1, 1)
preflight = SimpleNamespace(
topology={"world_size": 2},
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"),
source_identity=source,
)
selection = SimpleNamespace(hf_config_digest=_digest("model-config"))
config = SimpleNamespace(
device_kind="cuda",
evidence_tier="candidate_preflight",
code_digest=_digest("code"),
digest=_digest("config"),
placement_plan_digest=_digest("plan"),
require_model=lambda: selection,
)
external = _external_bindings()
exact = SimpleNamespace(evidence_tier="candidate_local_gpu", bindings=external)
monkeypatch.setattr(
session_module,
"observed_external_runtime_bindings",
lambda: {
"api_digest": _digest("different-api"),
"package_digest": external["package_digest"],
"runtime_digest": external["runtime_digest"],
},
)
with pytest.raises(RuntimeContractError) as failure:
session_module._validate_preload_bindings(
config,
SimpleNamespace(),
preflight,
exact,
source_mount_digest=_digest("mount"),
control_group_digest=_digest("control"),
)
assert failure.value.code == "LMS_DEPENDENCY_DRIFT"
def test_live_preflight_reuses_gloo_without_destroying_it(monkeypatch):
config = SimpleNamespace(run_id=RUN_ID, world_size=2)
launch = SimpleNamespace(rank=0, world_size=2)
result = SimpleNamespace(lifecycle_identity_digest=_digest("lifecycle"))
lifecycle = object()
observed: list[object] = []
monkeypatch.setattr(preflight_module.dist, "is_available", lambda: True)
monkeypatch.setattr(preflight_module.dist, "is_initialized", lambda: True)
monkeypatch.setattr(preflight_module.dist, "get_backend", lambda: "gloo")
monkeypatch.setattr(preflight_module.dist, "get_rank", lambda: 0)
monkeypatch.setattr(preflight_module.dist, "get_world_size", lambda: 2)
monkeypatch.setattr(preflight_module, "_begin_lifecycle", lambda *_args: lifecycle)
def run(_config, _launch, *, probes, lifecycle):
observed.extend((probes, lifecycle))
return result
def gather(record):
return canonical_record(record), canonical_record(replace(record, rank=1))
monkeypatch.setattr(preflight_module, "run_preflight", run)
monkeypatch.setattr(preflight_module, "gloo_all_gather_records", gather)
monkeypatch.setattr(
preflight_module.dist,
"destroy_process_group",
lambda *_args: pytest.fail("live preflight must not destroy caller-owned Gloo"),
)
assert preflight_module.run_live_preflight(config, launch) is result
assert observed == [None, lifecycle]
@pytest.mark.parametrize("descriptor", [1, 2])
def test_runtime_guard_contains_native_fd_output_and_fails_closed(capfd, descriptor):
secret = b"native-secret-canary"
with pytest.raises(RuntimeContractError) as failure:
with runtime_diagnostic_guard(teardown_timeout_seconds=1, exit_process=lambda _code: None):
os.write(descriptor, secret)
assert failure.value.code == "LMS_DIAGNOSTIC_REDACTION_FAILED"
captured = capfd.readouterr()
assert secret.decode() not in captured.out
assert secret.decode() not in captured.err
@pytest.mark.parametrize("kind", ["warning", "logging"])
def test_runtime_guard_contains_python_diagnostics_and_fails_closed(capfd, kind):
secret = "python-secret-canary"
with pytest.raises(RuntimeContractError) as failure:
with runtime_diagnostic_guard(teardown_timeout_seconds=1, exit_process=lambda _code: None):
if kind == "warning":
warnings.showwarning(secret, UserWarning, __file__, 1)
else:
logging.getLogger("obliteratus.test").warning(secret)
assert failure.value.code == "LMS_DIAGNOSTIC_REDACTION_FAILED"
captured = capfd.readouterr()
assert secret not in captured.out
assert secret not in captured.err
def test_process_hard_watchdog_quarantines_when_deadline_fires():
exits: list[int] = []
with pytest.raises(RuntimeContractError) as failure:
with ProcessHardWatchdog(0.01, exit_process=exits.append):
time.sleep(0.05)
assert exits == [70]
assert failure.value.code == "LMS_LOAD_STATE_UNCERTAIN"
def test_process_supervisor_terminates_a_hung_worker(monkeypatch):
events: list[str] = []
class Process:
exitcode = None
def __init__(self, **kwargs):
assert kwargs["target"] is session_module._run_supervised_entry
assert kwargs["daemon"] is False
def start(self):
events.append("start")
def join(self, _timeout):
events.append("join")
def is_alive(self):
return "terminate" not in events
def terminate(self):
events.append("terminate")
def kill(self):
pytest.fail("a terminated worker must not need a second kill")
context = SimpleNamespace(Process=lambda **kwargs: Process(**kwargs))
monkeypatch.setattr(session_module.multiprocessing, "get_context", lambda method: context)
with pytest.raises(RuntimeContractError) as failure:
supervise_model_session_entry(lambda: None, timeout_seconds=1)
assert failure.value.code == "LMS_LOAD_STATE_UNCERTAIN"
assert events == ["start", "join", "terminate", "join"]
def test_exact_nccl_mesh_is_verified_and_destroyed(monkeypatch):
import torch.distributed as dist
import torch.distributed.device_mesh as device_mesh_module
group = object()
destroyed: list[object] = []
launch = SimpleNamespace(
rank=0,
local_rank=0,
world_size=2,
local_world_size=2,
group_rank=0,
)
config = SimpleNamespace(
device_kind="cuda",
world_size=2,
local_world_size=2,
tensor_parallel_size=2,
teardown_timeout_seconds=1,
)
class Mesh:
ndim = 1
device_type = "cuda"
mesh = torch.tensor([0, 1])
def size(self):
return 2
def get_group(self, name):
assert name == "tp"
return group
def get_local_rank(self, name):
assert name == "tp"
return 0
def initialize(device_type, shape, **kwargs):
assert (device_type, shape) == ("cuda", (2,))
assert kwargs == {"mesh_dim_names": ("tp",), "backend_override": {"tp": "nccl"}}
return Mesh()
monkeypatch.setattr(dist, "is_initialized", lambda: True)
monkeypatch.setattr(
dist, "get_backend", lambda selected=None: "gloo" if selected is None else "nccl"
)
monkeypatch.setattr(dist, "get_process_group_ranks", lambda selected: [0, 1])
monkeypatch.setattr(dist, "destroy_process_group", destroyed.append)
monkeypatch.setattr(device_mesh_module, "init_device_mesh", initialize)
with exact_nccl_data_plane(config, launch, exit_process=lambda _code: None) as data:
assert data.group is group
assert data.device_mesh.device_type == "cuda"
assert destroyed == [group]
class _Manifest:
local_digest = _digest("rank-zero-local-manifest")
def to_dict(self):
return {
"schema_version": 1,
"adapter_id": "llama_bf16_tp2_v1",
"rank": 0,
"world_size": 2,
"parameters": [{"name": "model.weight"}],
"buffers": [],
"hooks": [{"name": "layer_boundary", "placement": "replicated_layer_boundary"}],
}
def test_live_protocol_session_publishes_consensus_before_yield_and_tears_down_after(
monkeypatch, tmp_path
):
attempt = tmp_path / RUN_ID
attempt.mkdir(mode=0o700)
source = SourceIdentity(DIGEST, _digest("model"), _digest("tokenizer"), 2, 128)
preflight = 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"),
attempt_path=attempt,
source_identity=source,
)
selection = SimpleNamespace(hf_config_digest=_digest("model-config"))
config = SimpleNamespace(
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,
)
launch = SimpleNamespace(rank=0, world_size=2)
control_digest = _digest("control-group")
data_digest = _digest("data-group")
source_mount = _protocol_mount()
manifest = _Manifest()
handle = SimpleNamespace(
manifest=manifest,
manifest_digest=_digest("logical-manifest"),
local_manifest_digest=manifest.local_digest,
plan_digest=config.placement_plan_digest,
source_identity=source,
config=SimpleNamespace(config_digest=selection.hf_config_digest),
profile=SimpleNamespace(digest=_digest("adapter-profile")),
)
bindings = _bindings(
code_digest=config.code_digest,
config_digest=config.digest,
control_group_digest=control_digest,
data_group_digest=data_digest,
hardware_digest=session_module._hardware_digest(preflight, source_mount.digest),
identity_digest=preflight.identity_digest,
logical_manifest_digest=handle.manifest_digest,
model_config_digest=selection.hf_config_digest,
model_digest=source.model_digest,
plan_digest=config.placement_plan_digest,
profile_digest=handle.profile.digest,
source_digest=source.source_digest,
tokenizer_digest=source.tokenizer_digest,
topology_digest=contract_digest(preflight.topology),
)
exact = ExactLoadProfile(
schema_version=1,
evidence_tier="protocol_cpu",
profile_state="candidate_unaccepted",
residual_risk_accepted=False,
transport_security="none_claimed",
session_timeout_seconds=5,
bindings={name: bindings[name] for name in EXTERNAL_LOAD_BINDING_FIELDS},
source_mount=source_mount,
)
events: list[str] = []
@contextmanager
def no_control(_config, _launch):
events.append("gloo-live")
try:
yield
finally:
events.append("gloo-destroyed")
@contextmanager
def protocol_plane(_config, _launch, **_kwargs):
events.append("nccl-live")
try:
yield DataPlane(object(), object(), data_digest)
finally:
events.append("nccl-destroyed")
def loader(_config, _launch, _preflight, **_kwargs):
events.append("loaded")
return handle
def gather(record):
if isinstance(record, LoadEvidenceSummary):
events.append("summary-consensus")
other = replace(
record,
rank=1,
private_record_digest=_digest("rank-one-private-record"),
)
return canonical_record(record), canonical_record(other)
assert isinstance(record, StageMessage)
events.append("loaded-consensus")
return canonical_record(record), canonical_record(replace(record, rank=1))
monkeypatch.setattr(session_module, "control_group", no_control)
monkeypatch.setattr(session_module, "run_live_preflight", lambda *_args, **_kwargs: preflight)
monkeypatch.setattr(
session_module, "_control_group_identity", lambda _launch: (object(), control_digest)
)
monkeypatch.setattr(session_module, "gloo_all_gather_records", gather)
monkeypatch.setattr(
session_module, "unanimous_vote", lambda sequence, accepted: sequence == 3 and accepted
)
monkeypatch.setattr(
preflight_module,
"source_mount_observation",
lambda _path: (_digest("unused"), False, False),
)
monkeypatch.setattr(torch.distributed, "all_reduce", lambda _tensor, *, op: None)
with pytest.raises(RuntimeContractError) as downstream:
with live_model_session(
config,
launch,
exact,
protocol_loader=loader,
protocol_data_plane=protocol_plane,
exit_process=lambda code: pytest.fail(f"unexpected hard exit {code}"),
) as session:
assert session.config is config
assert session.launch is launch
assert session.handle is handle
assert session.control_group is not None
assert session.data_plane.digest == data_digest
assert dict(session.bindings) == bindings
assert session.summary_set_digest == session_module.validate_loaded_summary_set(
session.summaries
)
assert [item.rank for item in session.summaries] == [0, 1]
assert all(
item.stage is RuntimeStage.LOADED and item.vote is Vote.COMMITTED
for item in session.loaded_messages
)
assert events[-2:] == ["summary-consensus", "loaded-consensus"]
assert "nccl-destroyed" not in events
assert "gloo-destroyed" not in events
raise RuntimeContractError("LMS_PROBE_CANCELLED", "private downstream detail")
assert downstream.value.code == "LMS_PROBE_CANCELLED"
assert str(downstream.value) == "distributed downstream stage failed"
assert (attempt / "model-load-evidence" / f"load-evidence-{RUN_ID}-rank-0000.json").is_file()
assert events.index("summary-consensus") < events.index("loaded-consensus")
assert events[-2:] == ["nccl-destroyed", "gloo-destroyed"]
def test_execute_model_session_returns_only_after_live_capability_is_closed(monkeypatch):
events: list[str] = []
session = SimpleNamespace(
preflight=SimpleNamespace(),
summary_set_digest=_digest("summary-set"),
summaries=(),
loaded_messages=(),
handle=SimpleNamespace(
manifest_digest=_digest("logical-manifest"),
local_manifest_digest=_digest("local-manifest"),
),
)
@contextmanager
def fake_live(*_args, **_kwargs):
events.append("live")
yield session
events.append("closed")
monkeypatch.setattr(session_module, "live_model_session", fake_live)
result = execute_model_session(object(), object(), object())
assert events == ["live", "closed"]
assert result.logical_manifest_digest == session.handle.manifest_digest
assert result.local_manifest_digest == session.handle.local_manifest_digest
def test_evidence_failure_never_emits_loaded(monkeypatch):
emitted: list[object] = []
monkeypatch.setattr(session_module, "gloo_all_gather_records", emitted.append)
with pytest.raises(RuntimeContractError) as failure:
try:
raise ContractError("private sink failed")
except ContractError as error:
raise session_module.translate_model_session_error(error) from None
assert failure.value.code == "LMS_MODEL_LOAD_FAILED"
assert emitted == []
@pytest.mark.parametrize("rank", [0, 1])
def test_evidence_mkdir_failure_votes_before_raising_and_closes_descriptor(
monkeypatch, tmp_path, rank
):
(tmp_path / "model-load-evidence").mkdir(mode=0o700)
closed: list[int] = []
close = os.close
def close_descriptor(descriptor):
closed.append(descriptor)
close(descriptor)
def failed_vote(outcome, *, op):
assert op is torch.distributed.ReduceOp.MIN
assert outcome.item() == rank # Rank 0's mkdir failed; rank 1 opened its parent.
outcome.fill_(0)
monkeypatch.setattr(torch.distributed, "all_reduce", failed_vote)
monkeypatch.setattr(session_module.os, "close", close_descriptor)
with pytest.raises(RuntimeContractError) as failure:
session_module._prepare_evidence_directory(tmp_path, SimpleNamespace(rank=rank))
assert failure.value.code == "LMS_LOAD_EVIDENCE_UNAVAILABLE"
assert len(closed) == 1
with pytest.raises(OSError):
os.fstat(closed[0])
def test_loaded_message_validator_rejects_missing_fixed_rank(monkeypatch):
config = SimpleNamespace(run_id=RUN_ID, world_size=2)
launch = SimpleNamespace(rank=0)
preflight = SimpleNamespace(lifecycle_identity_digest=_digest("lifecycle"))
def only_local(record):
return (canonical_record(record),)
monkeypatch.setattr(session_module, "gloo_all_gather_records", only_local)
with pytest.raises(RuntimeContractError) as failure:
session_module._emit_loaded_messages(
config,
launch,
preflight,
_digest("summary-set"),
)
assert failure.value.code == "LMS_LOAD_MANIFEST_INVALID"