"""Real CPU Gloo ranks refuse corrupt export receipts without replacing output.""" from dataclasses import replace import pytest import obliteratus.distributed.export as export import obliteratus.distributed.rank_local as local from obliteratus.distributed.contracts import RuntimeContractError from tests import test_distributed_export_contracts as fixtures from tests.test_distributed_export_units import _assert_failed from tests.test_distributed_rank_local_gloo import _candidate_worker, run_candidate mocked_export_world = fixtures.mocked_export_world def _receipt_worker(rank, rendezvous, config, identities, mode, queue): patches = pytest.MonkeyPatch() broadcast = export._broadcast_record gather = export._gather_rank_evidence description_count = 0 def corrupt_description(runtime, source, record): nonlocal description_count value = broadcast(runtime, source, record) if rank == 1 and mode == "staging-type" and "staging_name" in value: return {**value, "staging_name": 7} if set(value) == {"size", "digest"}: description_count += 1 target = 1 if mode == "index-size" else 2 if rank == 1 and mode in {"index-size", "manifest-size"} and description_count == target: return {**value, "size": True} return value def corrupt_writer(runtime, local, assignments): records = gather(runtime, local, assignments) if rank == 1 and mode == "reload-binding": records = (replace(records[0], reload_evidence_digest="f" * 64), records[1]) return records patches.setattr(export, "_broadcast_record", corrupt_description) patches.setattr(export, "_gather_rank_evidence", corrupt_writer) try: _candidate_worker(rank, rendezvous, config, identities, "success", queue) finally: patches.undo() @pytest.mark.gloo @pytest.mark.parametrize("mode,code", [ ("staging-type", "LMS_EXPORT_COLLECTIVE_FAILED"), ("index-size", "LMS_EXPORT_INDEX_INVALID"), ("manifest-size", "LMS_EXPORT_MANIFEST_INVALID"), ("reload-binding", "LMS_EXPORT_MANIFEST_INVALID"), ]) def test_corrupt_one_rank_receipt_rejects_both_and_preserves_prior_output(tmp_path, mode, code): config, rows, exits = run_candidate(tmp_path, mode, worker=_receipt_worker) assert exits == [0, 0] assert [row[1:] for row in rows] == [("error", code)] * 2 output = config.staging_path / "canonical-output" assert sorted(path.name for path in output.iterdir()) == ["prior.txt"] assert (output / "prior.txt").read_text() == "prior" assert not (config.for_rank(1).staging_path / "canonical-output").exists() for rank in range(2): root = config.for_rank(rank).staging_path assert not list(root.glob(".*rank-local-stage-*")) assert not list(root.glob(".*rank-local-validation-*")) def test_missing_writer_reconstruction_aborts_both_ranks_without_publication(mocked_export_world, monkeypatch): reconstruct = export._reconstruct_tensor def missing(runtime, *args): tensor = reconstruct(runtime, *args) return None if runtime.rank == 0 else tensor monkeypatch.setattr(export, "_reconstruct_tensor", missing) case = mocked_export_world results = case.run() _assert_failed(case, results, "LMS_EXPORT_WRITE_FAILED") assert not list(case.staging.glob(".*staging-*")) def test_authority_failure_is_a_shared_refusal_before_writing(mocked_export_world, monkeypatch): def unauthorized(runtime): if runtime.rank == 1: raise RuntimeContractError("LMS_EXPORT_TOPOLOGY_UNSUPPORTED", "authority revoked") return False # Both ranks describe two-host placement without an authorized local mapping. # Classify the exact runtime at entry rather than substituting publication. original = export._group_identity def two_host(runtime): original(runtime) object.__setattr__(runtime, "deployment_layout", export.TP2DeploymentLayout.TWO_HOST_TP2) monkeypatch.setattr(export, "_group_identity", two_host) monkeypatch.setattr(export, "_rank_local_authorized", unauthorized) case = mocked_export_world _assert_failed(case, case.run(), "LMS_EXPORT_TOPOLOGY_UNSUPPORTED") assert not list(case.staging.glob(".*staging-*")) def _abort_failure_worker(rank, rendezvous, config, identities, mode, queue): original = local.DurablePublication.abort def unavailable(publisher): if publisher.promoting: raise OSError("private publication abort failure") return original(publisher) patches = pytest.MonkeyPatch() patches.setattr(local.DurablePublication, "abort", unavailable) try: _candidate_worker(rank, rendezvous, config, identities, "promotion_ack_lost", queue) finally: patches.undo() @pytest.mark.gloo def test_lost_promotion_ack_and_failed_abort_remain_uncertain(tmp_path): config, rows, exits = run_candidate(tmp_path, "abort-failure", worker=_abort_failure_worker) assert exits == [0, 0] assert [row[1:] for row in rows] == [("error", "LMS_EXPORT_STATE_UNCERTAIN")] * 2 assert local.recovery_inventory(config.staging_path, "canonical-output", "1" * 32)["outcome"] == "uncertain" backup = config.staging_path / f".canonical-output.rank-local-backup-{'1' * 32}" assert (backup / "prior.txt").read_text() == "prior"