Files
OBLITERATUS/tests/test_distributed_journal_edges.py
Joseph MaglyandClaude Opus 5.5 07929a33c2 test(distributed): cover config, contract, journal, launcher and VERIFY edges (#304)
Add single-process unit tests for fail-closed validation and error paths in
the distributed stack, using fakes, monkeypatching and private tmp dirs
instead of new Gloo tests:

- config/contracts: schema v2 model profiles, schema/model pairing,
  mid-read profile changes, text/shape/kind validators, StageMessage
  decoding, canonical-record cycles and byte bounds, streamed digests.
- journal: argument admission, storage mismatches, serialization and
  payload bounds, short writes, non-private entries, close failures,
  tampered or swapped payloads, restore/copy failures, cleanup refusals.
- launcher: private allowlisted endpoints, membership mismatches,
  interface address filtering, init failure codes, and fd/diagnostic
  guard failures (pipe/dup/dup2/read/flush errors, reader overrun).
- verification(_contracts): closed summary/evidence records, tokenizer
  output validation, chat-template tokenization, and one-rank fakes for
  every collective guard (broadcast, logit error, state roots, evaluate,
  metrics, lifecycle, state transition, revalidation).
- mocked two-rank world: baseline and VERIFY summary consensus failures.

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

317 lines
11 KiB
Python

"""Fail-closed admission, restoration, and cleanup edges of the TP2 rollback journal (#304)."""
from __future__ import annotations
import hashlib
import os
import stat
from dataclasses import replace
from pathlib import Path
from types import SimpleNamespace
import pytest
import torch
from safetensors.torch import save as save_safetensors
from obliteratus.distributed import journal as journal_module
from obliteratus.distributed.contracts import ContractError, RuntimeContractError
from obliteratus.distributed.journal import RankJournal
from tests.test_distributed_surgery_contracts import _operation
pytestmark = pytest.mark.cpu
RUN_ID = "c" * 32
@pytest.fixture
def parent(tmp_path):
os.chmod(tmp_path, 0o700)
return tmp_path
def _create(parent, *, run_id=RUN_ID, tensor=None, **overrides):
operation = _operation()
values = {
"run_id": run_id,
"rank": 0,
"operations": (operation,),
"parameters": {
operation.logical_name: torch.arange(8, dtype=torch.float32).reshape(2, 4)
if tensor is None
else tensor
},
"max_payload_bytes": 4096,
}
values.update(overrides)
return RankJournal.create(parent, **values)
def _journal_dir(parent, run_id=RUN_ID) -> Path:
return parent / f".lms-journal-{run_id}-rank-0"
@pytest.mark.parametrize(
("overrides", "message"),
[
({"run_id": "short"}, "run_id is invalid"),
({"run_id": 7}, "run_id is invalid"),
({"rank": 2}, "outside the exact TP2 world"),
({"operations": ()}, "empty or out of sequence"),
({"operations": (_operation(sequence=1),)}, "empty or out of sequence"),
({"parameters": {}}, "parameter set does not match"),
({"max_payload_bytes": True}, "payload bound is invalid"),
({"max_payload_bytes": 0}, "payload bound is invalid"),
({"max_payload_bytes": 4096.0}, "payload bound is invalid"),
],
)
def test_create_rejects_invalid_arguments_before_touching_disk(parent, overrides, message):
with pytest.raises(ContractError, match=message):
_create(parent, **overrides)
assert list(parent.iterdir()) == []
@pytest.mark.parametrize("candidate", [Path("relative/parent"), "/absolute/but/text"])
def test_create_requires_an_absolute_path_object(candidate):
with pytest.raises(ContractError, match="absolute Path"):
_create(candidate)
@pytest.mark.parametrize(
"tensor",
[
torch.zeros(3, 4),
torch.zeros(2, 4, dtype=torch.float64),
torch.zeros(2, 4, dtype=torch.int32),
torch.zeros(2, 4).to_sparse(),
[[0.0] * 4] * 2,
],
)
def test_create_rejects_parameter_storage_that_disagrees_with_plan(parent, tensor):
with pytest.raises(RuntimeContractError) as raised:
_create(parent, tensor=tensor)
assert raised.value.code == "LMS_EXCISE_MANIFEST_INVALID"
assert not _journal_dir(parent).exists()
def test_serialization_failure_is_reported_and_cleaned_up(parent, monkeypatch):
def fail_save(_tensors):
raise RuntimeError("private tensor values")
monkeypatch.setattr(journal_module, "save_safetensors", fail_save)
with pytest.raises(RuntimeContractError) as raised:
_create(parent)
assert raised.value.code == "LMS_EXCISE_JOURNAL_FAILED"
assert "private" not in str(raised.value)
assert not _journal_dir(parent).exists()
def test_serialized_payload_overhead_counts_against_the_bound(parent):
# 2x4 float32 is exactly 32 tensor bytes; the safetensors header must not fit.
with pytest.raises(RuntimeContractError) as raised:
_create(parent, max_payload_bytes=32)
assert raised.value.code == "LMS_EXCISE_RESOURCE_DENIED"
assert not _journal_dir(parent).exists()
def test_short_write_aborts_creation_and_removes_partial_entry(parent, monkeypatch):
monkeypatch.setattr(journal_module.os, "write", lambda _descriptor, _payload: 0)
with pytest.raises(RuntimeContractError) as raised:
_create(parent)
assert raised.value.code == "LMS_EXCISE_JOURNAL_FAILED"
assert not _journal_dir(parent).exists()
def test_entry_file_that_is_not_private_is_rejected(parent, monkeypatch):
original_fstat = os.fstat
def shared_link_count(descriptor):
item = original_fstat(descriptor)
if stat.S_ISREG(item.st_mode):
return SimpleNamespace(
st_mode=item.st_mode, st_uid=item.st_uid, st_nlink=2, st_size=item.st_size
)
return item
monkeypatch.setattr(journal_module.os, "fstat", shared_link_count)
with pytest.raises(RuntimeContractError) as raised:
_create(parent)
assert raised.value.code == "LMS_EXCISE_JOURNAL_FAILED"
assert "not private" in str(raised.value)
assert not _journal_dir(parent).exists()
@pytest.mark.parametrize("cleanup_fails", [False, True])
def test_directory_close_failure_after_creation_never_returns_a_journal(
parent, monkeypatch, cleanup_fails
):
original_open = os.open
original_close = os.close
journal_descriptors: list[int] = []
cleanups: list[bool] = []
def record_open(path, flags, *args, **kwargs):
descriptor = original_open(path, flags, *args, **kwargs)
if kwargs.get("dir_fd") is not None and str(path).startswith(".lms-journal-"):
journal_descriptors.append(descriptor)
return descriptor
def fail_journal_close(descriptor):
original_close(descriptor)
if descriptor in journal_descriptors:
journal_descriptors.remove(descriptor)
raise OSError("injected close failure")
original_cleanup = RankJournal.cleanup
def cleanup(self):
cleanups.append(True)
if cleanup_fails:
raise RuntimeContractError("LMS_EXCISE_JOURNAL_FAILED", "injected cleanup failure")
original_cleanup(self)
monkeypatch.setattr(journal_module.os, "open", record_open)
monkeypatch.setattr(journal_module.os, "close", fail_journal_close)
monkeypatch.setattr(RankJournal, "cleanup", cleanup)
with pytest.raises(RuntimeContractError) as raised:
_create(parent)
assert raised.value.code == "LMS_EXCISE_STATE_UNCERTAIN"
assert cleanups == [True]
assert _journal_dir(parent).exists() is cleanup_fails
def test_inactive_journal_has_no_summary_and_cannot_restore(parent):
journal = _create(parent)
assert journal.summary.operation_count == 1
journal.cleanup()
with pytest.raises(ContractError, match="no longer active"):
journal.summary
with pytest.raises(RuntimeContractError) as raised:
journal.load_pristine()
assert raised.value.code == "LMS_EXCISE_ROLLBACK_FAILED"
def test_read_exact_rejects_truncated_payload(tmp_path):
path = tmp_path / "payload"
path.write_bytes(b"abcd")
descriptor = os.open(path, os.O_RDONLY)
try:
assert journal_module._read_exact(descriptor, 4) == b"abcd"
os.lseek(descriptor, 0, os.SEEK_SET)
with pytest.raises(RuntimeContractError, match="incomplete"):
journal_module._read_exact(descriptor, 10)
finally:
os.close(descriptor)
def test_entry_modified_during_read_is_refused(parent, monkeypatch):
journal = _create(parent)
entry_path = journal.root / journal.entries[0].relative_name
original_read_exact = journal_module._read_exact
def touch_after_read(descriptor, size):
payload = original_read_exact(descriptor, size)
os.utime(entry_path, ns=(1, 1))
return payload
monkeypatch.setattr(journal_module, "_read_exact", touch_after_read)
with pytest.raises(RuntimeContractError, match="changed while read"):
journal.load_pristine()
def test_entry_replaced_by_symlink_is_unavailable(parent, tmp_path_factory):
journal = _create(parent)
entry_path = journal.root / journal.entries[0].relative_name
target = tmp_path_factory.mktemp("elsewhere") / "payload"
target.write_bytes(entry_path.read_bytes())
entry_path.unlink()
entry_path.symlink_to(target)
with pytest.raises(RuntimeContractError, match="file is unavailable"):
journal.load_pristine()
def _rewrite_entry(journal, payload: bytes) -> None:
entry = journal.entries[0]
path = journal.root / entry.relative_name
os.chmod(path, 0o600)
path.write_bytes(payload)
journal.entries = (
replace(
entry,
payload_digest=hashlib.sha256(payload).hexdigest(),
payload_bytes=len(payload),
),
)
@pytest.mark.parametrize(
("payload", "message"),
[
(b"not a safetensors payload", "payload is invalid"),
(save_safetensors({"other": torch.zeros(2, 4)}), "tensor set changed"),
(save_safetensors({"tensor": torch.zeros(2, 4)}), "tensor identity changed"),
(save_safetensors({"tensor": torch.zeros(4, 2)}), "tensor identity changed"),
],
)
def test_digest_consistent_but_semantically_wrong_payload_is_refused(parent, payload, message):
journal = _create(parent)
_rewrite_entry(journal, payload)
with pytest.raises(RuntimeContractError, match=message):
journal.load_pristine()
def test_restore_refuses_changed_target_set_or_storage(parent):
journal = _create(parent)
name = journal.entries[0].logical_name
with pytest.raises(RuntimeContractError, match="target set changed"):
journal.restore({})
with pytest.raises(RuntimeContractError, match="target storage changed"):
journal.restore({name: torch.zeros(4, 2)})
with pytest.raises(RuntimeContractError, match="target storage changed"):
journal.restore({name: torch.zeros(2, 4, dtype=torch.float64)})
def test_restore_reports_failed_copy_without_backend_detail(parent):
journal = _create(parent)
overlapping = torch.zeros(1, 4).expand(2, 4)
with pytest.raises(RuntimeContractError) as raised:
journal.restore({journal.entries[0].logical_name: overlapping})
assert raised.value.code == "LMS_EXCISE_ROLLBACK_FAILED"
assert str(raised.value).endswith("rollback copy failed")
def test_restore_requires_post_copy_pristine_identity(parent, monkeypatch):
journal = _create(parent)
target = torch.zeros(2, 4)
original_digest = journal_module.tensor_digest
monkeypatch.setattr(
journal_module,
"tensor_digest",
lambda tensor: "0" * 64 if tensor is target else original_digest(tensor),
)
with pytest.raises(RuntimeContractError, match="did not restore pristine identity"):
journal.restore({journal.entries[0].logical_name: target})
assert torch.equal(target, torch.arange(8, dtype=torch.float32).reshape(2, 4))
def test_cleanup_refuses_unknown_files_and_stays_active(parent):
journal = _create(parent)
sentinel = journal.root / "operator-file"
sentinel.touch(mode=0o600)
with pytest.raises(RuntimeContractError, match="unexpected files"):
journal.cleanup()
assert journal.active
assert sentinel.exists()
sentinel.unlink()
journal.cleanup()
assert not journal.active and not journal.root.exists()
def test_cleanup_refuses_a_journal_directory_that_became_shared(parent):
journal = _create(parent)
os.chmod(journal.root, 0o755)
with pytest.raises(RuntimeContractError, match="not private"):
journal.cleanup()
assert journal.active
assert (journal.root / journal.entries[0].relative_name).exists()