Files
OBLITERATUS/tests/test_distributed_surgery_gloo.py
Joseph MaglyandClaude Opus 5.5 cdde845a99 Restore original collective limits; size reapers for unchecked intervals
Review r2: "stage budgets + one stalled collective" only holds where a
cooperative deadline is checked. PROBE's DISTILL tail, surgery's
post-EXCISE restoration and VERIFY's rollback run unchecked.

Restore the pre-PR process-group timeouts (VERIFY 4 s; PROBE, surgery and
model 5 s). The observed flake was the 5 s EXCISE stage budget, and the
original collective limits held under the same load. Each reaper is now
checked stage budgets + (collectives outside stage clocks + each unchecked
interval + each stage's longest overshoot interval) x collective timeout +
one stalled collective + exit. Counts are named constants with production
source pointers: model 45 s, PROBE 520 s, surgery 695 s, VERIFY 914 s,
VERIFY rank-exit setup 638 s. The short oracles are unchanged from r2.

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

782 lines
31 KiB
Python

"""Two-rank CPU/Gloo transaction, rollback, and fault oracle for the experimental TP2 stack (#244)."""
from __future__ import annotations
import hashlib
import multiprocessing as mp
import os
import time
from contextlib import contextmanager
from dataclasses import replace
from datetime import timedelta
from pathlib import Path
from queue import Empty
from types import SimpleNamespace
from unittest.mock import patch
import pytest
import torch
import torch.distributed as dist
import torch.nn as nn
from obliteratus.analysis.numerical_contracts import project_weight_against_direction
from obliteratus.distributed import surgery as surgery_module
from obliteratus.distributed.model_manifest import (
ManifestPlacement,
ParameterManifest,
ParameterManifestEntry,
ParameterShard,
RuntimeStorageKind,
SurgeryRole,
)
from obliteratus.distributed.probe_contracts import (
LayerProbeArtifact,
ProbeDistillResult,
ProbeDistillSummary,
tensor_digest,
)
from obliteratus.distributed.surgery import (
SurgeryRuntime,
rollback_distributed_excise,
run_distributed_excise,
)
from obliteratus.distributed.surgery_contracts import ExciseConfig
pytestmark = pytest.mark.gloo
WORLD_SIZE = 2
DIGEST = "a" * 64
SITE = "model.layers.0"
NAMES = (
"model.layers.0.self_attn.q_proj.weight",
"model.layers.0.self_attn.o_proj.weight",
"model.layers.0.mlp.gate_proj.weight",
"model.layers.0.mlp.down_proj.weight",
"model.layers.0.replicated_proj.weight",
)
# Budget policy: keep the original short collective failure detectors; the
# observed load flake was the EXCISE stage budget, so stage budgets get headroom.
# Reaper = checked stage budgets + (unchecked-interval collectives + longest
# collective interval between cooperative checks per stage) * group timeout
# + one stalled collective + exit margin. Collectives can pace just below their
# timeout: this is a hang-guard envelope, not an expected duration. Explicit
# timeout/rank-loss/hang oracles keep their own short limits.
_STAGE_TIMEOUT_SECONDS = 60.0
_COLLECTIVE_TIMEOUT_SECONDS = 5
_RANK_EXIT_COLLECTIVE_TIMEOUT_SECONDS = 5
_COLD_START_TIMEOUT_SECONDS = 60.0 # readiness wait; outside the operation reaper
_WORKER_EXIT_TIMEOUT_SECONDS = 30.0
# surgery.py:run_distributed_excise / _project_detached — two sharded directions.
_STAGE_OVERSHOOT_COLLECTIVES = 29 + 3 + 1 + 2 * (1 + 21 + 1) # 79
# surgery.py:_restore_transaction — five parameter proofs, lifecycle and cleanup.
_RESTORATION_COLLECTIVES = 2 + len(NAMES) * 6 + 2 + 6 + 1 # unchecked: 41
# 60 + (79 overshoot + 41 unchecked restoration + 1 stalled) * 5 + 30 = 695.
# Journal preparation (16 + 5*len(NAMES) = 41) and final summary (6) are shorter.
_WORKER_TIMEOUT_SECONDS = (
_STAGE_TIMEOUT_SECONDS
+ (_STAGE_OVERSHOOT_COLLECTIVES + _RESTORATION_COLLECTIVES + 1)
* _COLLECTIVE_TIMEOUT_SECONDS
+ _WORKER_EXIT_TIMEOUT_SECONDS
)
class _Block(nn.Module):
def __init__(self) -> None:
super().__init__()
self.self_attn = nn.Module()
self.self_attn.q_proj = nn.Linear(4, 2, bias=False, dtype=torch.float64)
self.self_attn.o_proj = nn.Linear(2, 4, bias=False, dtype=torch.float64)
self.mlp = nn.Module()
self.mlp.gate_proj = nn.Linear(4, 2, bias=False, dtype=torch.float64)
self.mlp.down_proj = nn.Linear(2, 4, bias=False, dtype=torch.float64)
self.replicated_proj = nn.Linear(4, 4, bias=False, dtype=torch.float64)
class _TinyShardedModel(nn.Module):
def __init__(self, rank: int) -> None:
super().__init__()
self.model = nn.Module()
self.model.layers = nn.ModuleList([_Block()])
parameters = dict(self.named_parameters(remove_duplicate=False))
with torch.no_grad():
for name, parameter in parameters.items():
full = _full_weight(name)
if name.endswith("q_proj.weight") or name.endswith("gate_proj.weight"):
value = full[rank * 2 : (rank + 1) * 2]
elif not name.endswith("replicated_proj.weight"):
value = full[:, rank * 2 : (rank + 1) * 2]
else:
value = full
parameter.copy_(value)
def _full_weight(name: str) -> torch.Tensor:
offset = NAMES.index(name) * 3.0
values = torch.arange(1, 17, dtype=torch.float64).reshape(4, 4)
return (values - 8.5) / 7.0 + offset
def _rank_shards(shard_dim: int) -> tuple[ParameterShard, ...]:
result = []
for rank in range(2):
shape = [4, 4]
shape[shard_dim] = 2
result.append(ParameterShard(rank, rank * 2, (rank + 1) * 2, tuple(shape)))
return tuple(result)
def _manifest(model: nn.Module, rank: int, mode: str) -> ParameterManifest:
parameters = dict(model.named_parameters(remove_duplicate=False))
roles = (
SurgeryRole.ATTENTION_INPUT,
SurgeryRole.ATTENTION_OUTPUT,
SurgeryRole.MLP_INPUT,
SurgeryRole.MLP_OUTPUT,
SurgeryRole.ATTENTION_INPUT,
)
entries = []
for name, role in zip(NAMES, roles, strict=True):
replicated = name.endswith("replicated_proj.weight")
shard_dim = (
None
if replicated
else 0
if role in {SurgeryRole.ATTENTION_INPUT, SurgeryRole.MLP_INPUT}
else 1
)
shards = (
tuple(ParameterShard(owner, 0, 0, (4, 4)) for owner in range(2))
if replicated
else _rank_shards(shard_dim)
)
local = shards[rank]
parameter = parameters[name]
digest = tensor_digest(parameter)
if mode == "manifest_digest_drift" and rank == 1 and name == NAMES[0]:
digest = "f" * 64
tie_id = (
"unexpected-tie" if mode == "tie_drift" and rank == 1 and name == NAMES[0] else None
)
surgery_role = SurgeryRole.IMMUTABLE if mode == "unsupported_role" and rank == 1 else role
local_shape = local.local_shape
shard_end = local.end
if mode == "uneven_layout" and rank == 1 and name == NAMES[0]:
local_shape = (1, 4)
shard_end = 3
entries.append(
ParameterManifestEntry(
name=name,
global_shape=(4, 4),
local_shape=local_shape,
dtype="float64",
placement=(ManifestPlacement.REPLICATE if replicated else ManifestPlacement.SHARD),
shard_dim=shard_dim,
shard_start=local.start,
shard_end=shard_end,
owner_ranks=(0, 1),
tie_id=tie_id,
local_value_digest=digest,
replicated_value_digest=digest if replicated else None,
hook_site=SITE,
surgery_role=surgery_role,
runtime_storage_kind=RuntimeStorageKind.LOCAL_TENSOR_PARAMETER,
rank_shards=shards,
)
)
return ParameterManifest(1, "tiny-llama-tp2", rank, 2, tuple(entries), (SITE,))
def _directions(mode: str, rank: int) -> torch.Tensor:
value = torch.tensor(
((1.0, 2.0, -1.0, 0.5), (-0.5, 1.0, 2.0, 1.0)),
dtype=torch.float32,
)
value = value / value.norm(dim=1, keepdim=True)
if mode == "direction_drift" and rank == 1:
value = value.clone()
value[0] = torch.tensor((1.0, 0.0, 0.0, 0.0))
return value
def _distilled(mode: str, rank: int, run_id: str) -> ProbeDistillResult:
directions = _directions(mode, rank)
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(4, dtype=torch.float32),
harmless_mean=torch.zeros(4, dtype=torch.float32),
directions=directions,
)
layer_summary = artifact.summary_record()
summary = ProbeDistillSummary(
schema_version=1,
run_id=run_id,
loaded_identity_digest="c" * 64,
config_digest="d" * 64,
corpus_digest="e" * 64,
sample_ids_digest="f" * 64,
tokenizer_contract_digest="1" * 64,
tokenization_digest="2" * 64,
harmful_count=2,
harmless_count=2,
input_width=4,
activation_atol_hex=(0.0).hex(),
activation_rtol_hex=(0.0).hex(),
selected_sites=(SITE,),
layers=(layer_summary,),
)
return ProbeDistillResult(summary, (artifact,), (), ())
def _config(mode: str) -> ExciseConfig:
config = ExciseConfig(
norm_preserve=True,
regularization=0.2,
projection_row_fraction=0.5,
max_norm_ratio=1.1,
max_journal_bytes=1024 * 1024,
max_update_bytes=1024 * 1024,
safety_margin_bytes=0,
stage_timeout_seconds=_STAGE_TIMEOUT_SECONDS,
)
if mode == "timeout":
return replace(config, stage_timeout_seconds=1e-9)
if mode == "config_drift" and os.environ.get("RANK") == "1":
return replace(config, regularization=0.3)
if mode == "without_norm_preservation":
return replace(config, norm_preserve=False)
if mode == "without_regularization":
return replace(config, regularization=0.0)
if mode == "without_selective_projection":
return replace(config, projection_row_fraction=1.0)
if mode == "role_subset":
return replace(
config,
roles=(SurgeryRole.ATTENTION_INPUT, SurgeryRole.MLP_OUTPUT),
)
return config
def _model_digest(model: nn.Module) -> str:
digest = hashlib.sha256()
for name, parameter in model.named_parameters(remove_duplicate=False):
digest.update(name.encode("ascii"))
digest.update(bytes.fromhex(tensor_digest(parameter)))
return digest.hexdigest()
def _fault(mode: str, rank: int):
def inject(point: str, sequence: int) -> None:
matches = {
"journal_failure": ("journal_before", -1),
"journal_after_failure": ("journal_after", -1),
"prepare_before_failure": ("prepare_before", 0),
"prepare_failure": ("prepare_after", 0),
"commit_before_failure": ("commit_before", 0),
"commit_after_failure": ("commit_after", 0),
"rollback_set_digest_failure": ("commit_after", 0),
"rollback_loaded_failure": ("rollback_loaded", -1),
"rollback_before_failure": ("rollback_before", -1),
"rollback_failure": ("rollback_copy", 0),
"rollback_after_failure": ("rollback_after", -1),
"cleanup_before_failure": ("cleanup_before", -1),
"cleanup_failure": ("cleanup_after", -1),
}
if mode == "rank_exit" and rank == 1 and (point, sequence) == ("commit_before", 0):
os._exit(17)
if rank == 1 and matches.get(mode) == (point, sequence):
raise RuntimeError("private injected payload must never escape")
return inject
def _preparation_fault(mode: str, rank: int):
original_prepare = surgery_module._project_detached
original_clone = torch.Tensor.clone
original_to = torch.Tensor.to
original_projection = surgery_module.distributed_project_weight
def prepare(runtime, operation, parameter, artifact, config):
if rank != 1 or operation.sequence != 1:
return original_prepare(runtime, operation, parameter, artifact, config)
def clone(tensor, *args, **kwargs):
if tensor.data_ptr() == parameter.data_ptr():
raise RuntimeError("private injected clone failure")
return original_clone(tensor, *args, **kwargs)
def copy_direction(tensor, *args, **kwargs):
if tensor.data_ptr() == artifact.directions[1].data_ptr():
raise RuntimeError("private injected direction copy failure")
return original_to(tensor, *args, **kwargs)
def project(*args, **kwargs):
result = original_projection(*args, **kwargs)
return replace(result, weight=torch.full_like(result.weight, float("nan")))
if mode == "prepare_clone_failure":
with patch.object(torch.Tensor, "clone", clone):
return original_prepare(runtime, operation, parameter, artifact, config)
if mode == "prepare_copy_failure":
with patch.object(torch.Tensor, "to", copy_direction):
return original_prepare(runtime, operation, parameter, artifact, config)
if mode == "prepare_nonfinite_result":
with patch.object(surgery_module, "distributed_project_weight", project):
return original_prepare(runtime, operation, parameter, artifact, config)
return original_prepare(runtime, operation, parameter, artifact, config)
return prepare
@contextmanager
def _stage_preparation_fault(mode: str, rank: int, runtime: SurgeryRuntime):
def fail(*_args, **_kwargs):
raise MemoryError("private injected preparation failure")
if mode == "live_runtime_failure":
def narrow(*_args, **_kwargs):
if rank == 1:
fail()
return runtime
with patch.object(surgery_module, "runtime_from_live_session", narrow):
yield
return
if rank != 1:
yield
return
target = surgery_module
if mode == "config_digest_failure":
target, name, replacement = ExciseConfig, "digest", property(fail)
elif mode == "operation_plan_digest_failure":
name, replacement = "operation_plan_digest", fail
elif mode == "transaction_identity_failure":
original_digest = surgery_module.contract_digest
def transaction_digest(value):
if isinstance(value, dict) and "pristine_set_digest" in value:
fail()
return original_digest(value)
name, replacement = "contract_digest", transaction_digest
elif mode == "postcondition_digest_failure":
original_set_digest = surgery_module.excise_digest_set
def postcondition_digest(values):
if not isinstance(values, tuple):
fail()
return original_set_digest(values)
name, replacement = "excise_digest_set", postcondition_digest
elif mode == "rollback_set_digest_failure":
original_set_digest = surgery_module.excise_digest_set
calls = 0
def rollback_digest(values):
nonlocal calls
calls += 1
if calls == 2:
fail()
return original_set_digest(values)
name, replacement = "excise_digest_set", rollback_digest
elif mode == "summary_digest_failure":
target, name, replacement = surgery_module.ExciseSummary, "digest", property(fail)
elif mode == "decision_frame_failure":
name, replacement = "encode_frame", fail
elif mode == "transaction_construction_failure":
name, replacement = "ExciseTransaction", fail
elif mode == "result_construction_failure":
name, replacement = "ExciseResult", fail
else:
yield
return
with patch.object(target, name, replacement):
yield
def _worker(rank: int, rendezvous: str, root: str, mode: str, queue: mp.Queue, ready) -> None:
model = None
before = None
committed_sequences = []
votes = []
try:
os.environ["RANK"] = str(rank)
dist.init_process_group(
"gloo",
init_method=f"file://{rendezvous}",
rank=rank,
world_size=WORLD_SIZE,
timeout=timedelta(
seconds=(
_RANK_EXIT_COLLECTIVE_TIMEOUT_SECONDS
if mode == "rank_exit" else _COLLECTIVE_TIMEOUT_SECONDS
)
),
)
ready.set()
model = _TinyShardedModel(rank)
if mode == "unregistered_storage_alias" and rank == 1:
model.model.layers[0].mlp.gate_proj.weight = torch.nn.Parameter(
model.model.layers[0].self_attn.q_proj.weight.detach()
)
if mode == "nonfinite_parameter" and rank == 1:
with torch.no_grad():
dict(model.named_parameters())[NAMES[0]][0, 0] = float("nan")
run_id = hashlib.sha256(mode.encode("ascii")).hexdigest()[:32]
distilled = _distilled(mode, rank, run_id)
if mode == "identical_direction_tampering":
# Preserve the DISTILLED summary and change the live tensor on both ranks.
distilled.layers[0].directions[0] = torch.tensor((1.0, 0.0, 0.0, 0.0))
manifest = _manifest(model, rank, mode)
available = 1 if mode == "resource_failure" and rank == 1 else 1 << 30
runtime = SurgeryRuntime(
run_id=run_id,
distilled_digest=distilled.digest,
rank=rank,
world_size=2,
model=model,
manifest=manifest,
control_group=dist.group.WORLD,
data_group=dist.group.WORLD,
tensor_device=torch.device("cpu"),
available_host_bytes=available,
available_staging_bytes=available,
available_device_bytes=available,
journal_parent=Path(root),
)
before = _model_digest(model)
inject = _fault(mode, rank)
def record_fault(point, sequence):
if point == "commit_after":
committed_sequences.append(sequence)
inject(point, sequence)
original_vote = surgery_module.unanimous_vote
def record_vote(sequence, accepted, *, group):
votes.append((sequence, accepted))
return original_vote(sequence, accepted, group=group)
config = _config(mode)
with (
patch.object(surgery_module, "unanimous_vote", record_vote),
patch.object(surgery_module, "_project_detached", _preparation_fault(mode, rank)),
_stage_preparation_fault(mode, rank, runtime),
):
if mode == "live_runtime_failure":
result = surgery_module.run_live_distributed_excise(
SimpleNamespace(control_group=runtime.control_group), distilled, config
)
else:
result = run_distributed_excise(
runtime,
distilled,
config,
cancellation_requested=lambda: mode == "cancelled" and rank == 1,
fault_injector=record_fault,
)
committed = {
name: parameter.detach().tolist()
for name, parameter in model.named_parameters(remove_duplicate=False)
}
journal_private = (
result.transaction.journal.active
and result.transaction.journal.root.stat().st_mode & 0o077 == 0
and all(
(result.transaction.journal.root / item.relative_name).stat().st_mode & 0o177 == 0
for item in result.transaction.journal.entries
)
)
rollback_distributed_excise(
result,
fault_injector=_fault(mode, rank),
)
queue.put(
(
rank,
"ok",
{
"summary_digest": result.digest,
"operation_count": result.summary.operation_count,
"prepared": tuple(item.stage.value for item in result.prepared_messages),
"mutating": tuple(item.stage.value for item in result.mutating_messages),
"committed": committed,
"restored": _model_digest(model) == before,
"journal_private": journal_private,
"journal_removed": not result.transaction.journal.root.exists(),
"summary": repr(result.summary),
"result_repr": repr(result),
},
)
)
except BaseException as exc:
queue.put(
(
rank,
"error",
(
getattr(exc, "code", None),
str(exc),
model is not None and before is not None and _model_digest(model) == before,
tuple(committed_sequences),
tuple(votes),
),
)
)
finally:
if dist.is_available() and dist.is_initialized():
try:
dist.destroy_process_group()
except BaseException:
pass
def _run(tmp_path: Path, mode: str, *, timeout: float = _WORKER_TIMEOUT_SECONDS):
os.chmod(tmp_path, 0o700)
context = mp.get_context("spawn")
queue = context.Queue()
ready = [context.Event() for _rank in range(WORLD_SIZE)]
rendezvous = tmp_path / f"{mode}.rendezvous"
processes = [
context.Process(
target=_worker,
args=(rank, str(rendezvous), str(tmp_path), mode, queue, ready[rank]),
)
for rank in range(WORLD_SIZE)
]
for process in processes:
process.start()
cold_deadline = time.monotonic() + _COLD_START_TIMEOUT_SECONDS
for event in ready:
if not event.wait(max(0.0, cold_deadline - time.monotonic())):
for process in processes:
if process.is_alive():
process.terminate()
process.join(3)
pytest.fail(f"{mode} workers did not initialize within the cold-start bound")
deadline = time.monotonic() + timeout
for process in processes:
process.join(max(0.0, deadline - time.monotonic()))
if process.is_alive():
process.terminate()
process.join(3)
pytest.fail(f"{mode} worker did not terminate within the operation bound")
results = []
while len(results) < WORLD_SIZE:
try:
results.append(queue.get(timeout=2))
except Empty:
break
queue.close()
queue.join_thread()
return sorted(results), tuple(process.exitcode for process in processes)
def _reference(name: str, mode: str) -> torch.Tensor:
value = _full_weight(name)
role = {
NAMES[0]: SurgeryRole.ATTENTION_INPUT,
NAMES[1]: SurgeryRole.ATTENTION_OUTPUT,
NAMES[2]: SurgeryRole.MLP_INPUT,
NAMES[3]: SurgeryRole.MLP_OUTPUT,
NAMES[4]: SurgeryRole.ATTENTION_INPUT,
}[name]
config = _config(mode)
if role not in config.roles:
return value
direction_axis = 1 if role in {SurgeryRole.ATTENTION_INPUT, SurgeryRole.MLP_INPUT} else 0
for direction in _directions("success", 0):
result = project_weight_against_direction(
value,
direction,
direction_axis=direction_axis,
norm_preserve=config.norm_preserve,
regularization=config.regularization,
projection_row_fraction=config.projection_row_fraction,
max_norm_ratio=config.max_norm_ratio,
)
assert result.projected
value = result.weight
return value
@pytest.mark.parametrize(
("mode", "operation_count"),
[
("success", 5),
("without_norm_preservation", 5),
("without_regularization", 5),
("without_selective_projection", 5),
("role_subset", 3),
],
)
def test_two_rank_excise_matches_complete_tensor_and_exactly_rolls_back(
tmp_path, mode, operation_count
):
results, exits = _run(tmp_path, mode)
assert exits == (0, 0)
assert [status for _rank, status, _payload in results] == ["ok", "ok"], results
payloads = [payload for _rank, _status, payload in results]
assert len({item["summary_digest"] for item in payloads}) == 1
assert {item["operation_count"] for item in payloads} == {operation_count}
assert all(item["prepared"] == ("prepared", "prepared") for item in payloads)
assert all(item["mutating"] == ("mutating", "mutating") for item in payloads)
assert all(item["restored"] and item["journal_private"] for item in payloads)
assert all(item["journal_removed"] for item in payloads)
assert all("safetensors" not in item["summary"] for item in payloads)
assert all("safetensors" not in item["result_repr"] for item in payloads)
assert all("tensor(" not in item["result_repr"] for item in payloads)
for name in NAMES:
shards = [torch.tensor(item["committed"][name], dtype=torch.float64) for item in payloads]
if name.endswith("replicated_proj.weight"):
torch.testing.assert_close(shards[0], shards[1], rtol=0, atol=0)
actual = shards[0]
else:
concat_dim = (
0 if name.endswith("q_proj.weight") or name.endswith("gate_proj.weight") else 1
)
actual = torch.cat(shards, dim=concat_dim)
torch.testing.assert_close(
actual,
_reference(name, mode),
rtol=1e-10,
atol=1e-10,
msg=lambda message, logical_name=name: f"{logical_name}: {message}",
)
@pytest.mark.parametrize(
("mode", "code"),
[
("config_drift", "LMS_EXCISE_CONFIG_INVALID"),
("direction_drift", "LMS_EXCISE_CONFIG_INVALID"),
("manifest_digest_drift", "LMS_EXCISE_MANIFEST_INVALID"),
("tie_drift", "LMS_EXCISE_CONFIG_INVALID"),
("unregistered_storage_alias", "LMS_EXCISE_MANIFEST_INVALID"),
("unsupported_role", "LMS_EXCISE_CONFIG_INVALID"),
("uneven_layout", "LMS_EXCISE_MANIFEST_INVALID"),
("nonfinite_parameter", "LMS_EXCISE_MANIFEST_INVALID"),
("resource_failure", "LMS_EXCISE_RESOURCE_DENIED"),
("journal_failure", "LMS_EXCISE_JOURNAL_FAILED"),
("journal_after_failure", "LMS_EXCISE_JOURNAL_FAILED"),
("prepare_before_failure", "LMS_EXCISE_PREPARE_FAILED"),
("prepare_failure", "LMS_EXCISE_PREPARE_FAILED"),
("prepare_clone_failure", "LMS_EXCISE_PREPARE_FAILED"),
("prepare_copy_failure", "LMS_EXCISE_PREPARE_FAILED"),
("prepare_nonfinite_result", "LMS_EXCISE_PREPARE_FAILED"),
("identical_direction_tampering", "LMS_EXCISE_MANIFEST_INVALID"),
("commit_before_failure", "LMS_EXCISE_COMMIT_FAILED"),
("commit_after_failure", "LMS_EXCISE_COMMIT_FAILED"),
("cancelled", "LMS_EXCISE_CANCELLED"),
("timeout", "LMS_EXCISE_TIMEOUT"),
],
)
def test_all_preparation_commit_and_input_faults_fail_closed_on_every_rank(tmp_path, mode, code):
results, exits = _run(tmp_path, mode)
assert exits == (0, 0)
assert len(results) == 2
assert [status for _rank, status, _payload in results] == ["error", "error"], results
assert {payload[0] for _rank, _status, payload in results} == {code}
assert all("private injected" not in payload[1] for _rank, _status, payload in results)
if mode in {
"prepare_clone_failure", "prepare_copy_failure", "prepare_nonfinite_result",
"identical_direction_tampering",
}:
assert all(payload[2] for _rank, _status, payload in results), results
expected_commits = () if mode == "identical_direction_tampering" else (0,)
assert all(payload[3] == expected_commits for _rank, _status, payload in results), results
assert not any(path.name.endswith("published") for path in tmp_path.iterdir())
assert not any(path.name.startswith(".lms-journal-") for path in tmp_path.iterdir())
@pytest.mark.parametrize(
("mode", "sequence", "code", "committed"),
[
("config_digest_failure", 63_002, "LMS_EXCISE_CONFIG_INVALID", False),
("operation_plan_digest_failure", 63_001, "LMS_EXCISE_MANIFEST_INVALID", False),
("transaction_identity_failure", 63_040, "LMS_EXCISE_PREPARE_FAILED", False),
("transaction_construction_failure", 63_042, "LMS_EXCISE_PREPARE_FAILED", False),
("decision_frame_failure", 63_050, "LMS_EXCISE_COLLECTIVE_FAILED", False),
("postcondition_digest_failure", 66_001, "LMS_EXCISE_COMMIT_FAILED", True),
("summary_digest_failure", 66_001, "LMS_EXCISE_COMMIT_FAILED", True),
("result_construction_failure", 66_001, "LMS_EXCISE_COMMIT_FAILED", True),
("live_runtime_failure", 63_003, "LMS_EXCISE_CONFIG_INVALID", False),
],
)
def test_rank_local_stage_preparation_is_voted_before_the_next_collective(
tmp_path, mode, sequence, code, committed
):
results, exits = _run(tmp_path, mode)
assert exits == (0, 0)
assert [status for _rank, status, _payload in results] == ["error", "error"], results
assert {payload[0] for _rank, _status, payload in results} == {code}
expected_commits = tuple(range(len(NAMES))) if committed else ()
for rank, _status, payload in results:
assert "private injected" not in payload[1]
assert payload[2], results
assert payload[3] == expected_commits, results
assert [accepted for step, accepted in payload[4] if step == sequence] == [rank == 0]
assert [step for step, _accepted in results[0][2][4]] == [
step for step, _accepted in results[1][2][4]
]
assert not any(path.name.startswith(".lms-journal-") for path in tmp_path.iterdir())
assert not any(path.name.endswith("published") for path in tmp_path.iterdir())
@pytest.mark.parametrize(
("mode", "codes"),
[
("rollback_loaded_failure", {"LMS_EXCISE_ROLLBACK_FAILED"}),
("rollback_before_failure", {"LMS_EXCISE_ROLLBACK_FAILED"}),
("rollback_failure", {"LMS_EXCISE_ROLLBACK_FAILED"}),
("rollback_after_failure", {"LMS_EXCISE_ROLLBACK_FAILED"}),
("cleanup_before_failure", {"LMS_EXCISE_JOURNAL_FAILED"}),
("cleanup_failure", {"LMS_EXCISE_JOURNAL_FAILED"}),
],
)
def test_rollback_and_cleanup_failure_never_return_a_usable_result(tmp_path, mode, codes):
results, exits = _run(tmp_path, mode)
assert exits == (0, 0)
assert len(results) == 2
assert [status for _rank, status, _payload in results] == ["error", "error"]
assert {payload[0] for _rank, _status, payload in results} <= codes
assert all("private injected" not in payload[1] for _rank, _status, payload in results)
def test_rank_loss_makes_commit_state_uncertain_and_cannot_report_success(tmp_path):
started = time.monotonic()
results, exits = _run(tmp_path, "rank_exit", timeout=15)
assert time.monotonic() - started < 15
assert exits[1] == 17
assert len(results) == 1
assert results[0][1] == "error"
assert results[0][2][0] in {
"LMS_EXCISE_COLLECTIVE_FAILED",
"LMS_EXCISE_STATE_UNCERTAIN",
}
def test_rollback_set_digest_failure_votes_before_consensus_and_quarantines(tmp_path):
results, exits = _run(tmp_path, "rollback_set_digest_failure")
assert exits == (0, 0)
assert [status for _rank, status, _payload in results] == ["error", "error"], results
assert {payload[0] for _rank, _status, payload in results} == {"LMS_EXCISE_STATE_UNCERTAIN"}
for rank, _status, payload in results:
assert payload[2], results
assert payload[3] == (0,)
assert [accepted for step, accepted in payload[4] if step == 67_002] == [rank == 0]
assert [step for step, _accepted in results[0][2][4]] == [
step for step, _accepted in results[1][2][4]
]
assert len(list(tmp_path.glob(".lms-journal-*"))) == 2
assert not any(path.name.endswith("published") for path in tmp_path.iterdir())