"""Single-process EXCISE adapter tests with a faked two-rank control plane (#304). The multi-process Gloo suite proves the real collectives. These tests replace only the collective primitives imported by ``obliteratus.distributed.surgery`` with a deterministic in-process two-rank mirror so that validation, rollback, and fail-closed branches can be exercised quickly and exhaustively. """ from __future__ import annotations import json import os from dataclasses import replace from pathlib import Path from types import SimpleNamespace import pytest import torch from obliteratus.distributed import surgery as surgery_module from obliteratus.distributed.contracts import ( ContractError, PlacementKind, RuntimeContractError, RuntimeStage, canonical_record, contract_digest, ) from obliteratus.distributed.journal import RankJournal from obliteratus.distributed.model_manifest import ManifestPlacement from obliteratus.distributed.model_session import LiveModelSession from obliteratus.distributed.probe_contracts import ( LayerProbeArtifact, ProbeDistillResult, ) from obliteratus.distributed.surgery import ( ExciseResult, ExciseTransactionState, SurgeryRuntime, excise_host_memory_bytes, rollback_distributed_excise, run_distributed_excise, run_live_distributed_excise, runtime_from_live_session, ) from tests.test_distributed_surgery_gloo import ( NAMES, SITE, _TinyShardedModel, _config, _distilled, _manifest, _model_digest, ) RUN_ID = "0123456789abcdef0123456789abcdef" COEFFICIENT = 0.25 class _FakeWorld: """Mirror rank-local collective inputs into a deterministic two-rank world.""" def __init__(self, monkeypatch: pytest.MonkeyPatch, *, rank: int = 0) -> None: self.rank = rank self.backend = "gloo" self.gather_hook = None self.consensus_hook = None self.all_gather_hook = None self.broadcast_hook = None self.votes: list[tuple[int, bool]] = [] self.consensus: list[str] = [] fake_dist = SimpleNamespace( get_backend=lambda group=None: self.backend, get_rank=lambda group=None: self.rank, get_world_size=lambda group=None: 2, all_gather=self._all_gather, broadcast=self._broadcast, ) monkeypatch.setattr(surgery_module, "dist", fake_dist) monkeypatch.setattr(surgery_module, "unanimous_vote", self._vote) monkeypatch.setattr(surgery_module, "require_consensus_digest", self._consensus) monkeypatch.setattr(surgery_module, "gloo_all_gather_records", self._gather) def _vote(self, sequence, accepted, *, group=None): self.votes.append((sequence, accepted)) return bool(accepted) def _consensus(self, digest, *, group=None): self.consensus.append(digest) if self.consensus_hook is not None: self.consensus_hook(digest) return digest def _gather(self, record, *, group=None): value = json.loads(canonical_record(record).decode("utf-8")) result = [] for peer in range(2): item = dict(value, rank=peer) if self.gather_hook is not None: item = self.gather_hook(item, peer) result.append(canonical_record(item)) return tuple(result) def _all_gather(self, output, tensor, group=None): for peer, item in enumerate(output): item.copy_(tensor) if self.all_gather_hook is not None: self.all_gather_hook(item, peer) def _broadcast(self, frame, src=0, group=None): if self.broadcast_hook is not None: self.broadcast_hook(frame) def _runtime(tmp_path: Path, *, rank: int = 0, mode: str = "ok", **changes): os.chmod(tmp_path, 0o700) model = _TinyShardedModel(rank) distilled = _distilled(mode, rank, RUN_ID) runtime = SurgeryRuntime( run_id=RUN_ID, distilled_digest=distilled.digest, rank=rank, world_size=2, model=model, manifest=_manifest(model, rank, mode), control_group=object(), data_group=object(), tensor_device=torch.device("cpu"), available_host_bytes=1 << 30, available_staging_bytes=1 << 30, available_device_bytes=1 << 30, journal_parent=tmp_path, ) return (replace(runtime, **changes) if changes else runtime), distilled @pytest.fixture def projected(monkeypatch): """Replace the real data-plane projection with a deterministic detached update.""" def project(runtime, operation, parameter, artifact, config): return parameter.detach().clone() * 0.5, COEFFICIENT monkeypatch.setattr(surgery_module, "_project_detached", project) def _parameters(model) -> dict[str, torch.Tensor]: return { name: parameter.detach().clone() for name, parameter in model.named_parameters(remove_duplicate=False) } # --------------------------------------------------------------------------- # SurgeryRuntime and live-session narrowing @pytest.mark.parametrize( ("changes", "message"), [ ({"run_id": "not-a-run-id"}, "run_id"), ({"distilled_digest": "A" * 64}, "distilled identity"), ({"world_size": 3}, "exact TP2 world"), ({"rank": 2}, "exact TP2 world"), ({"manifest": object()}, "manifest is invalid"), ({"rank": 1}, "rank identity disagree"), ({"tensor_device": torch.device("meta")}, "tensor device"), ({"tensor_device": "cpu"}, "tensor device"), ({"available_host_bytes": True}, "resource inventory"), ({"available_staging_bytes": -1}, "resource inventory"), ({"available_device_bytes": 1.0}, "resource inventory"), ({"journal_parent": Path("relative")}, "journal parent"), ({"journal_parent": "/absolute/string"}, "journal parent"), ], ) def test_surgery_runtime_rejects_invalid_identity_and_inventory(tmp_path, changes, message): runtime, _ = _runtime(tmp_path) with pytest.raises(ContractError, match=message): replace(runtime, **changes) def test_current_host_available_multiplies_page_size_by_available_pages(monkeypatch): values = {"SC_PAGE_SIZE": 4096, "SC_AVPHYS_PAGES": 7} monkeypatch.setattr(surgery_module.os, "sysconf", lambda name: values[name]) assert surgery_module._current_host_available() == 4096 * 7 def test_runtime_from_live_session_requires_live_capabilities(tmp_path): _, distilled = _runtime(tmp_path) with pytest.raises(RuntimeContractError) as caught: runtime_from_live_session(SimpleNamespace(), distilled) assert caught.value.code == "LMS_EXCISE_CONFIG_INVALID" assert "LOADED and DISTILLED" in str(caught.value) def _bare_session(**fields) -> LiveModelSession: session = object.__new__(LiveModelSession) for name, value in fields.items(): object.__setattr__(session, name, value) return session def test_runtime_from_live_session_rejects_incomplete_capability(tmp_path): _, distilled = _runtime(tmp_path) with pytest.raises(RuntimeContractError) as caught: runtime_from_live_session(_bare_session(), distilled) assert caught.value.code == "LMS_EXCISE_CONFIG_INVALID" assert "incomplete" in str(caught.value) def test_runtime_from_live_session_rejects_non_cuda_or_foreign_profile(tmp_path): runtime, distilled = _runtime(tmp_path) attestation = SimpleNamespace( rank=0, free_host_memory_bytes=1 << 30, free_staging_bytes=1 << 30, free_device_memory_bytes=1 << 30, ) session = _bare_session( handle=SimpleNamespace( manifest=runtime.manifest, model=runtime.model, profile=surgery_module.LLAMA_BF16_TP2_PROFILE, manifest_digest=runtime.manifest.digest, ), preflight=SimpleNamespace( attestations=(attestation,), identity=SimpleNamespace(run_id=RUN_ID), ), config=SimpleNamespace(staging_path=tmp_path), launch=SimpleNamespace(local_rank=0), ) # The fixture model is CPU-resident, so the exact CUDA profile must refuse it. with pytest.raises(RuntimeContractError) as caught: runtime_from_live_session(session, distilled) assert caught.value.code == "LMS_EXCISE_CONFIG_INVALID" assert "outside the exact profile" in str(caught.value) def test_run_live_distributed_excise_narrows_session_before_running(monkeypatch, tmp_path): _FakeWorld(monkeypatch) runtime, distilled = _runtime(tmp_path) config = _config("ok") calls = [] sentinel = object() monkeypatch.setattr( surgery_module, "runtime_from_live_session", lambda session, value: calls.append(("narrow", session, value)) or runtime, ) def run(value, result, cfg, *, cancellation_requested): calls.append(("run", value, result, cfg, cancellation_requested())) return sentinel monkeypatch.setattr(surgery_module, "run_distributed_excise", run) session = SimpleNamespace(control_group=object()) assert ( run_live_distributed_excise( session, distilled, config, cancellation_requested=lambda: True ) is sentinel ) assert calls == [("narrow", session, distilled), ("run", runtime, distilled, config, True)] # --------------------------------------------------------------------------- # Collective helpers def test_group_identity_fails_closed_when_backend_is_unavailable(monkeypatch, tmp_path): _FakeWorld(monkeypatch) runtime, _ = _runtime(tmp_path) def broken(group=None): raise RuntimeError("group destroyed") monkeypatch.setattr(surgery_module.dist, "get_backend", broken) with pytest.raises(RuntimeContractError) as caught: surgery_module._group_identity(runtime) assert caught.value.code == "LMS_EXCISE_COLLECTIVE_FAILED" def test_group_identity_rejects_wrong_data_backend_or_rank(monkeypatch, tmp_path): world = _FakeWorld(monkeypatch) runtime, _ = _runtime(tmp_path) surgery_module._group_identity(runtime) world.backend = "nccl" with pytest.raises(RuntimeContractError) as caught: surgery_module._group_identity(runtime) assert caught.value.code == "LMS_EXCISE_CONFIG_INVALID" world.backend = "gloo" world.rank = 1 with pytest.raises(RuntimeContractError, match="identity changed"): surgery_module._group_identity(runtime) def test_collective_accept_translates_vote_transport_failure(monkeypatch, tmp_path): _FakeWorld(monkeypatch) runtime, _ = _runtime(tmp_path) def broken(*_args, **_kwargs): raise RuntimeError("peer lost") monkeypatch.setattr(surgery_module, "unanimous_vote", broken) with pytest.raises(RuntimeContractError) as caught: surgery_module._collective_accept(runtime, 1, True, code="X", message="unused") assert caught.value.code == "LMS_EXCISE_COLLECTIVE_FAILED" def test_check_continue_treats_failing_cancellation_probe_as_cancelled(monkeypatch, tmp_path): _FakeWorld(monkeypatch) runtime, _ = _runtime(tmp_path) def probe() -> bool: raise RuntimeError("operator channel lost") with pytest.raises(RuntimeContractError) as caught: surgery_module._check_continue( runtime, _config("ok"), started=surgery_module.time.monotonic(), sequence=5, cancellation_requested=probe, ) assert caught.value.code == "LMS_EXCISE_CANCELLED" def test_check_continue_rejects_gather_failure_and_sequence_disagreement(monkeypatch, tmp_path): world = _FakeWorld(monkeypatch) runtime, _ = _runtime(tmp_path) kwargs = { "started": surgery_module.time.monotonic(), "sequence": 9, "cancellation_requested": lambda: False, } surgery_module._check_continue(runtime, _config("ok"), **kwargs) def disagree(item, peer): if peer == 1: item[0] = 10 world.all_gather_hook = disagree with pytest.raises(RuntimeContractError, match="sequence disagrees"): surgery_module._check_continue(runtime, _config("ok"), **kwargs) def broken(item, peer): raise RuntimeError("transport failure") world.all_gather_hook = broken with pytest.raises(RuntimeContractError, match="cancellation consensus failed"): surgery_module._check_continue(runtime, _config("ok"), **kwargs) def test_check_continue_reports_timeout_after_its_bound(monkeypatch, tmp_path): _FakeWorld(monkeypatch) runtime, _ = _runtime(tmp_path) with pytest.raises(RuntimeContractError) as caught: surgery_module._check_continue( runtime, _config("ok"), started=surgery_module.time.monotonic() - 3600, sequence=1, cancellation_requested=lambda: False, ) assert caught.value.code == "LMS_EXCISE_TIMEOUT" def test_emit_stage_rejects_records_from_a_wrong_rank_set(monkeypatch, tmp_path): world = _FakeWorld(monkeypatch) runtime, _ = _runtime(tmp_path) kwargs = { "current": RuntimeStage.DISTILLED, "requested": RuntimeStage.PREPARED, "sequence": 5, "identity_digest": "e" * 64, } messages = surgery_module._emit_stage(runtime, **kwargs) assert tuple(item.rank for item in messages) == (0, 1) world.gather_hook = lambda item, peer: dict(item, rank=0) with pytest.raises(RuntimeContractError, match="lifecycle records disagree"): surgery_module._emit_stage(runtime, **kwargs) world.gather_hook = None with pytest.raises(RuntimeContractError, match="lifecycle consensus failed"): surgery_module._emit_stage( runtime, **dict(kwargs, requested=RuntimeStage.PUBLISHED) ) def test_broadcast_decision_enforces_coordinator_ownership(monkeypatch, tmp_path): world = _FakeWorld(monkeypatch) runtime, _ = _runtime(tmp_path) assert surgery_module._broadcast_decision(runtime, {"action": "commit"}) == { "action": "commit" } with pytest.raises(RuntimeContractError, match="decision broadcast failed"): surgery_module._broadcast_decision(runtime, None) world.rank = 1 follower, _ = _runtime(tmp_path, rank=1) with pytest.raises(RuntimeContractError, match="decision broadcast failed"): surgery_module._broadcast_decision(follower, {"action": "commit"}) def deliver_coordinator_frame(frame): frame.copy_(surgery_module.encode_frame(canonical_record({"action": "commit"}))) world.broadcast_hook = deliver_coordinator_frame assert surgery_module._broadcast_decision(follower, None) == {"action": "commit"} def test_broadcast_decision_rejects_a_non_record_payload(monkeypatch, tmp_path): world = _FakeWorld(monkeypatch) runtime, _ = _runtime(tmp_path) def deliver_list(frame): frame.copy_(surgery_module.encode_frame(b"[1,2]")) world.broadcast_hook = deliver_list with pytest.raises(RuntimeContractError, match="not a bounded record"): surgery_module._broadcast_decision(runtime, {"action": "commit"}) def test_logical_digest_rejects_malformed_or_divergent_records(monkeypatch, tmp_path): world = _FakeWorld(monkeypatch) runtime, _ = _runtime(tmp_path) digest = "1" * 64 expected = contract_digest((digest, digest)) assert ( surgery_module._logical_digest(runtime, sequence=3, local_digest=digest, replicated=True) == expected ) world.gather_hook = lambda item, peer: dict(item, sequence=4) if peer else item with pytest.raises(RuntimeContractError, match="logical digest records disagree"): surgery_module._logical_digest(runtime, sequence=3, local_digest=digest, replicated=False) world.gather_hook = lambda item, peer: dict(item, local_digest="2" * 64) if peer else item sharded = surgery_module._logical_digest( runtime, sequence=3, local_digest=digest, replicated=False ) assert sharded == contract_digest((digest, "2" * 64)) with pytest.raises(RuntimeContractError) as caught: surgery_module._logical_digest(runtime, sequence=3, local_digest=digest, replicated=True) assert caught.value.code == "LMS_EXCISE_MANIFEST_INVALID" def broken(item, peer): raise RuntimeError("transport failure") world.gather_hook = broken with pytest.raises(RuntimeContractError, match="aggregation failed"): surgery_module._logical_digest(runtime, sequence=3, local_digest=digest, replicated=False) def test_journal_set_digest_requires_both_rank_summaries(monkeypatch, tmp_path): world = _FakeWorld(monkeypatch) runtime, _ = _runtime(tmp_path) local = surgery_module.RankJournalSummary( schema_version=1, rank=0, operation_count=2, tensor_bytes=64, payload_bytes=128, entry_root_digest="3" * 64, ) peer_summary = replace(local, rank=1) assert surgery_module._journal_set_digest(runtime, local) == contract_digest( (local.digest, peer_summary.digest) ) world.gather_hook = lambda item, peer: dict(item, operation_count=1) if peer else item with pytest.raises(RuntimeContractError, match="journal summary set is incomplete"): surgery_module._journal_set_digest(runtime, local) world.gather_hook = lambda item, peer: dict(item, unexpected=True) with pytest.raises(RuntimeContractError, match="journal summary consensus failed"): surgery_module._journal_set_digest(runtime, local) def test_entry_placement_maps_admitted_layouts_and_rejects_others(): shard = ManifestPlacement.SHARD assert ( surgery_module._entry_placement( SimpleNamespace(placement=ManifestPlacement.REPLICATE, shard_dim=None) ) is PlacementKind.REPLICATED ) assert ( surgery_module._entry_placement(SimpleNamespace(placement=shard, shard_dim=0)) is PlacementKind.COLUMN_WISE ) assert ( surgery_module._entry_placement(SimpleNamespace(placement=shard, shard_dim=1)) is PlacementKind.ROW_WISE ) with pytest.raises(ContractError, match="placement is not admitted"): surgery_module._entry_placement(SimpleNamespace(placement=shard, shard_dim=2)) def test_translate_error_maps_failure_families(): owned = RuntimeContractError("LMS_EXCISE_CANCELLED", "cancelled") assert surgery_module._translate_error(owned) is owned cases = { TimeoutError(): "LMS_EXCISE_TIMEOUT", ContractError("bad manifest"): "LMS_EXCISE_MANIFEST_INVALID", RuntimeContractError("LMS_FOREIGN", "x"): "LMS_EXCISE_MANIFEST_INVALID", ValueError("other"): "LMS_EXCISE_FAILED", } for error, code in cases.items(): translated = surgery_module._translate_error(error) assert translated.code == code assert "distributed EXCISE failed" in str(translated) # --------------------------------------------------------------------------- # Operation-plan admission def _distilled_namespace(runtime, distilled, **summary_changes): summary = SimpleNamespace( run_id=distilled.summary.run_id, selected_sites=distilled.summary.selected_sites, layers=distilled.summary.layers, ) for name, value in summary_changes.items(): setattr(summary, name, value) return SimpleNamespace( digest=runtime.distilled_digest, summary=summary, layers=distilled.layers, ) def test_operation_plan_admits_every_selected_role_in_canonical_order(tmp_path): runtime, distilled = _runtime(tmp_path) operations, parameters, artifacts = surgery_module._build_operation_plan( runtime, distilled, _config("ok") ) # Layer, then role order, then name: the replicated input sorts before q_proj. assert [item.logical_name for item in operations] == [ NAMES[4], NAMES[0], NAMES[1], NAMES[2], NAMES[3], ] assert [item.sequence for item in operations] == list(range(5)) assert set(parameters) == set(NAMES) assert set(artifacts) == {SITE} replicated = [item for item in operations if item.placement is PlacementKind.REPLICATED] assert [item.logical_name for item in replicated] == [NAMES[4]] def test_operation_plan_rejects_changed_distilled_capability(tmp_path): runtime, distilled = _runtime(tmp_path) config = _config("ok") with pytest.raises(ContractError, match="capability identity changed"): surgery_module._build_operation_plan( replace(runtime, distilled_digest="0" * 64), distilled, config ) incomplete = _distilled_namespace(runtime, distilled) incomplete.layers = () with pytest.raises(ContractError, match="selected artifacts are incomplete"): surgery_module._build_operation_plan(runtime, incomplete, config) drifted = _distilled_namespace( runtime, distilled, layers=(replace(distilled.summary.layers[0], direction_digest="0" * 64),), ) with pytest.raises(ContractError, match="direction identity changed"): surgery_module._build_operation_plan(runtime, drifted, config) class _ParameterList: def __init__(self, items): self.items = items def named_parameters(self, remove_duplicate=True): return iter(self.items) def test_operation_plan_rejects_model_api_and_storage_drift(monkeypatch, tmp_path): runtime, distilled = _runtime(tmp_path) config = _config("ok") with pytest.raises(ContractError, match="exact parameter API"): surgery_module._build_operation_plan(replace(runtime, model=object()), distilled, config) named = list(runtime.model.named_parameters(remove_duplicate=False)) with pytest.raises(ContractError, match="no longer match the manifest"): surgery_module._build_operation_plan( replace(runtime, model=_ParameterList(named[:-1])), distilled, config ) plain = [(name, value.detach().clone()) for name, value in named] with pytest.raises(ContractError, match="storage is unsupported"): surgery_module._build_operation_plan( replace(runtime, model=_ParameterList(plain)), distilled, config ) def unavailable(self): raise RuntimeError("storage released") monkeypatch.setattr(torch.nn.Parameter, "untyped_storage", unavailable, raising=False) with pytest.raises(ContractError, match="storage identity is unavailable"): surgery_module._build_operation_plan(runtime, distilled, config) @pytest.mark.parametrize( ("mode", "message"), [ ("unsupported_role", "operation plan is empty"), ("tie_drift", "unsupported mutable parameter"), ("manifest_digest_drift", "pristine digest changed"), ], ) def test_operation_plan_rejects_unsupported_manifest_entries(tmp_path, mode, message): runtime, distilled = _runtime(tmp_path, rank=1, mode=mode) with pytest.raises(ContractError, match=message): surgery_module._build_operation_plan(runtime, distilled, _config("ok")) def test_operation_plan_rejects_direction_width_mismatch(tmp_path): runtime, distilled = _runtime(tmp_path) narrow = torch.tensor(((1.0, 0.0, 0.0),), dtype=torch.float32) artifact = LayerProbeArtifact( site=SITE, layer_index=0, activation_digest="b" * 64, max_abs_error=0.0, max_rel_error=0.0, score=1.0, harmful_mean=torch.ones(3, dtype=torch.float32), harmless_mean=torch.zeros(3, dtype=torch.float32), directions=narrow, ) summary = replace( distilled.summary, input_width=3, layers=(artifact.summary_record(),) ) mismatched = ProbeDistillResult(summary, (artifact,), (), ()) with pytest.raises(ContractError, match="direction width"): surgery_module._build_operation_plan( replace(runtime, distilled_digest=mismatched.digest), mismatched, _config("ok"), ) def test_excise_host_memory_reserves_cpu_projection_working_set(tmp_path): runtime, distilled = _runtime(tmp_path) config = _config("ok") operations, _, artifacts = surgery_module._build_operation_plan(runtime, distilled, config) journal_bound = sum(item.local_bytes for item in operations) + 3 * max( item.local_bytes for item in operations ) direction_bytes = max( item.directions.numel() * item.directions.element_size() for item in artifacts.values() ) largest = max(item.local_bytes for item in operations) assert excise_host_memory_bytes(runtime, distilled, config) == max( journal_bound, 8 * largest + 4 * direction_bytes ) # --------------------------------------------------------------------------- # End-to-end transaction in the faked two-rank world def test_excise_commits_and_rolls_back_in_mirrored_world(monkeypatch, tmp_path, projected): world = _FakeWorld(monkeypatch) runtime, distilled = _runtime(tmp_path) before = _parameters(runtime.model) result = run_distributed_excise(runtime, distilled, _config("ok")) assert result.digest == result.summary.digest assert result.summary.operation_count == 5 assert [item.stage for item in result.prepared_messages] == [RuntimeStage.PREPARED] * 2 assert [item.stage for item in result.mutating_messages] == [RuntimeStage.MUTATING] * 2 assert result.transaction.state is ExciseTransactionState.COMMITTED for name, value in runtime.model.named_parameters(remove_duplicate=False): torch.testing.assert_close(value.detach(), before[name] * 0.5, rtol=0, atol=0) assert all(accepted for _sequence, accepted in world.votes) messages = rollback_distributed_excise(result) assert [item.stage for item in messages] == [RuntimeStage.ABORTING] * 2 + [ RuntimeStage.ABORTED ] * 2 assert result.transaction.state is ExciseTransactionState.ROLLED_BACK assert not result.transaction.journal.root.exists() for name, value in runtime.model.named_parameters(remove_duplicate=False): assert torch.equal(value.detach(), before[name]) with pytest.raises(RuntimeContractError) as caught: rollback_distributed_excise(result) assert caught.value.code == "LMS_EXCISE_ROLLBACK_FAILED" def test_rollback_quarantines_when_restored_identity_is_not_pristine( monkeypatch, tmp_path, projected ): world = _FakeWorld(monkeypatch) runtime, distilled = _runtime(tmp_path) result = run_distributed_excise(runtime, distilled, _config("ok")) def peer_not_restored(item, peer): if peer == 1 and set(item) == {"rank", "sequence", "local_digest"}: if item["sequence"] == 67_101: return dict(item, local_digest="9" * 64) return item world.gather_hook = peer_not_restored with pytest.raises(RuntimeContractError) as caught: rollback_distributed_excise(result) assert caught.value.code == "LMS_EXCISE_ROLLBACK_FAILED" assert "not pristine" in str(caught.value) assert result.transaction.state is ExciseTransactionState.QUARANTINED # A quarantined transaction keeps its journal for out-of-band recovery. assert result.transaction.journal.root.exists() def test_rollback_rejects_a_non_result(monkeypatch, tmp_path): with pytest.raises(RuntimeContractError) as caught: rollback_distributed_excise(SimpleNamespace(transaction=None)) assert caught.value.code == "LMS_EXCISE_ROLLBACK_FAILED" assert "active committed transaction" in str(caught.value) def test_excise_votes_on_invalid_inputs_before_identity_consensus(monkeypatch, tmp_path): world = _FakeWorld(monkeypatch) runtime, distilled = _runtime(tmp_path) with pytest.raises(RuntimeContractError) as caught: run_distributed_excise(runtime, distilled, object()) assert caught.value.code == "LMS_EXCISE_CONFIG_INVALID" assert world.votes == [(63_002, False)] and world.consensus == [] def test_excise_rejects_disagreeing_rank_identities(monkeypatch, tmp_path): world = _FakeWorld(monkeypatch) runtime, distilled = _runtime(tmp_path) def disagree(digest): raise ContractError("rank digests disagree") world.consensus_hook = disagree with pytest.raises(RuntimeContractError) as caught: run_distributed_excise(runtime, distilled, _config("ok")) assert caught.value.code == "LMS_EXCISE_CONFIG_INVALID" assert not any(tmp_path.iterdir()) def test_excise_votes_down_a_locally_invalid_operation_plan(monkeypatch, tmp_path): world = _FakeWorld(monkeypatch) runtime, distilled = _runtime(tmp_path, rank=1, mode="tie_drift") world.rank = 1 before = _model_digest(runtime.model) with pytest.raises(RuntimeContractError) as caught: run_distributed_excise(runtime, distilled, _config("ok")) assert caught.value.code == "LMS_EXCISE_MANIFEST_INVALID" assert (63_001, False) in world.votes assert _model_digest(runtime.model) == before assert not any(tmp_path.iterdir()) def test_excise_rejects_disagreeing_operation_plans(monkeypatch, tmp_path): world = _FakeWorld(monkeypatch) runtime, distilled = _runtime(tmp_path) identities = {_config("ok").digest, runtime.distilled_digest, runtime.manifest.digest} def plan_disagrees(digest): if digest not in identities: raise ContractError("rank digests disagree") world.consensus_hook = plan_disagrees with pytest.raises(RuntimeContractError) as caught: run_distributed_excise(runtime, distilled, _config("ok")) assert caught.value.code == "LMS_EXCISE_MANIFEST_INVALID" assert "operation plans disagree" in str(caught.value) def test_excise_restores_when_live_state_drifts_after_journaling( monkeypatch, tmp_path, projected ): world = _FakeWorld(monkeypatch) runtime, distilled = _runtime(tmp_path) before = _parameters(runtime.model) target = dict(runtime.model.named_parameters())[NAMES[0]] def drift(point, sequence): if point == "journal_after": with torch.no_grad(): target.add_(1.0) with pytest.raises(RuntimeContractError) as caught: run_distributed_excise(runtime, distilled, _config("ok"), fault_injector=drift) assert caught.value.code == "LMS_EXCISE_PREPARE_FAILED" assert "before the first commit" in str(caught.value) for name, value in runtime.model.named_parameters(remove_duplicate=False): assert torch.equal(value.detach(), before[name]) assert not any(tmp_path.iterdir()) assert (67_900, True) in world.votes def test_excise_restores_when_projection_statistics_disagree( monkeypatch, tmp_path, projected ): world = _FakeWorld(monkeypatch) runtime, distilled = _runtime(tmp_path) before = _model_digest(runtime.model) coefficient = contract_digest({"coefficient_norm_sq_hex": float(COEFFICIENT).hex()}) def statistics_disagree(digest): if digest == coefficient: raise ContractError("rank digests disagree") world.consensus_hook = statistics_disagree with pytest.raises(RuntimeContractError) as caught: run_distributed_excise(runtime, distilled, _config("ok")) assert caught.value.code == "LMS_EXCISE_PREPARE_FAILED" assert "projection statistics" in str(caught.value) assert _model_digest(runtime.model) == before def test_excise_restores_when_logical_postcondition_differs(monkeypatch, tmp_path, projected): world = _FakeWorld(monkeypatch) runtime, distilled = _runtime(tmp_path) before = _model_digest(runtime.model) def peer_post_differs(item, peer): if peer == 1 and set(item) == {"rank", "sequence", "local_digest"}: # Sequence 1 is the row-sharded q_proj, so only the logical root differs. if item["sequence"] == 64_601: return dict(item, local_digest="9" * 64) return item world.gather_hook = peer_post_differs with pytest.raises(RuntimeContractError) as caught: run_distributed_excise(runtime, distilled, _config("ok")) assert caught.value.code == "LMS_EXCISE_COMMIT_FAILED" assert "logical postcondition" in str(caught.value) assert _model_digest(runtime.model) == before assert not any(tmp_path.iterdir()) def test_excise_reports_uncertain_state_when_rollback_cannot_load( monkeypatch, tmp_path, projected ): _FakeWorld(monkeypatch) runtime, distilled = _runtime(tmp_path) def fail(point, sequence): if point in {"prepare_before", "rollback_loaded"}: raise RuntimeError(point) with pytest.raises(RuntimeContractError) as caught: run_distributed_excise(runtime, distilled, _config("ok"), fault_injector=fail) assert caught.value.code == "LMS_EXCISE_STATE_UNCERTAIN" assert "rollback did not reach certain global state" in str(caught.value) def test_excise_reports_uncertain_state_when_failed_journal_cleanup_fails( monkeypatch, tmp_path, projected ): world = _FakeWorld(monkeypatch) runtime, distilled = _runtime(tmp_path) cleanups = [] def summary_gather_fails(item, peer): if "entry_root_digest" in item: raise RuntimeError("peer lost during journal consensus") return item def cleanup_fails(self): cleanups.append(self.root) raise OSError("cleanup interrupted") world.gather_hook = summary_gather_fails monkeypatch.setattr(RankJournal, "cleanup", cleanup_fails) with pytest.raises(RuntimeContractError) as caught: run_distributed_excise(runtime, distilled, _config("ok")) assert caught.value.code == "LMS_EXCISE_STATE_UNCERTAIN" assert "journal cleanup did not finish" in str(caught.value) assert len(cleanups) == 1 assert (67_901, False) in world.votes def test_excise_cleans_journal_after_post_creation_failure(monkeypatch, tmp_path, projected): world = _FakeWorld(monkeypatch) runtime, distilled = _runtime(tmp_path) def summary_gather_fails(item, peer): if "entry_root_digest" in item: raise RuntimeError("peer lost during journal consensus") return item world.gather_hook = summary_gather_fails with pytest.raises(RuntimeContractError) as caught: run_distributed_excise(runtime, distilled, _config("ok")) assert caught.value.code == "LMS_EXCISE_COLLECTIVE_FAILED" assert (67_901, True) in world.votes assert not any(tmp_path.iterdir()) def test_excise_result_is_not_forgeable_from_summary_alone(monkeypatch, tmp_path, projected): _FakeWorld(monkeypatch) runtime, distilled = _runtime(tmp_path) result = run_distributed_excise(runtime, distilled, _config("ok")) forged = ExciseResult( summary=result.summary, prepared_messages=result.prepared_messages, mutating_messages=result.mutating_messages, transaction=replace(result.transaction, state=ExciseTransactionState.ROLLED_BACK), ) with pytest.raises(RuntimeContractError): rollback_distributed_excise(forged) assert "transaction" not in repr(result) rollback_distributed_excise(result) def test_standalone_operation_plan_finite_checks_use_bounded_chunks(monkeypatch, tmp_path): import obliteratus.distributed.probe_contracts as contracts runtime, distilled = _runtime(tmp_path) monkeypatch.setattr(contracts, "TENSOR_DIGEST_CHUNK_BYTES", 64) monkeypatch.setattr(contracts, "TENSOR_DIGEST_WORKING_BYTES", 128) original = torch.isfinite sizes = [] def finite(value): assert value.numel() * (value.element_size() + 3) + 64 <= 128 sizes.append(value.numel()) return original(value) monkeypatch.setattr(torch, "isfinite", finite) surgery_module._build_operation_plan(runtime, distilled, _config("ok")) assert len(sizes) > len(NAMES) assert sum(sizes) == sum(parameter.numel() for parameter in runtime.model.parameters())