mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 18:44:41 +01:00
235 lines
7.8 KiB
Python
235 lines
7.8 KiB
Python
"""Redacted, atomic distributed-preflight evidence tests."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
import stat
|
|
from dataclasses import replace
|
|
|
|
import pytest
|
|
|
|
from obliteratus.distributed.contracts import (
|
|
ContractError,
|
|
RuntimeStage,
|
|
StageMessage,
|
|
Vote,
|
|
)
|
|
import obliteratus.distributed.evidence as evidence_module
|
|
from obliteratus.distributed.evidence import (
|
|
PreflightEvidence,
|
|
PrivateRecordPending,
|
|
read_evidence,
|
|
read_stage_message,
|
|
write_evidence,
|
|
write_stage_message,
|
|
)
|
|
|
|
|
|
def test_evidence_is_allowlisted_redacted_and_private(tmp_path):
|
|
hostile = "Bearer hf_secret password=/private/model 10.10.0.10 host.internal"
|
|
evidence = PreflightEvidence.failure(
|
|
run_id="1" * 32,
|
|
config_digest="2" * 64,
|
|
code="LMS_DIAGNOSTIC_REDACTION_FAILED",
|
|
world_size=2,
|
|
evidence_tier="protocol_cpu",
|
|
detail=hostile,
|
|
)
|
|
path = tmp_path / "evidence.json"
|
|
write_evidence(path, evidence)
|
|
raw = path.read_text(encoding="utf-8")
|
|
parsed = json.loads(raw)
|
|
assert parsed["result"] == "failed"
|
|
assert parsed["error_code"] == "LMS_DIAGNOSTIC_REDACTION_FAILED"
|
|
assert hostile not in raw
|
|
assert "10.10.0.10" not in raw
|
|
assert stat.S_IMODE(path.stat().st_mode) == 0o600
|
|
assert read_evidence(path) == evidence
|
|
|
|
|
|
def test_cleanup_failure_is_quarantined():
|
|
evidence = PreflightEvidence.failure(
|
|
run_id="1" * 32,
|
|
config_digest="2" * 64,
|
|
code="LMS_CLEANUP_INCOMPLETE",
|
|
world_size=2,
|
|
evidence_tier="candidate_preflight",
|
|
detail="secret",
|
|
)
|
|
assert evidence.result == "quarantined"
|
|
assert evidence.error_code == "LMS_CLEANUP_INCOMPLETE"
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("overrides", "message"),
|
|
[
|
|
({"schema_version": 2}, "schema_version"),
|
|
({"result": "unknown"}, "result is invalid"),
|
|
({"run_id": "bad"}, "run_id is invalid"),
|
|
({"config_digest": "bad"}, "config_digest is invalid"),
|
|
({"world_size": True}, "world_size is invalid"),
|
|
({"accepted_ranks": True}, "accepted_ranks is invalid"),
|
|
({"evidence_tier": "unknown"}, "tier is invalid"),
|
|
({"identity_digest": "bad"}, "identity_digest is invalid"),
|
|
({"error_code": "UNKNOWN"}, "error_code is invalid"),
|
|
({"accepted_ranks": 1}, "complete accepted world"),
|
|
({"error_code": "LMS_STAGE_TIMEOUT"}, "cannot contain an error"),
|
|
],
|
|
)
|
|
def test_success_evidence_rejects_every_invalid_public_field(overrides, message):
|
|
evidence = PreflightEvidence.success(
|
|
run_id="1" * 32,
|
|
config_digest="2" * 64,
|
|
world_size=2,
|
|
evidence_tier="protocol_cpu",
|
|
identity_digest="3" * 64,
|
|
)
|
|
with pytest.raises(ContractError, match=message):
|
|
replace(evidence, **overrides)
|
|
|
|
|
|
def test_failure_evidence_requires_stable_error_code():
|
|
evidence = PreflightEvidence.failure(
|
|
run_id="1" * 32,
|
|
config_digest="2" * 64,
|
|
code="LMS_STAGE_TIMEOUT",
|
|
world_size=2,
|
|
evidence_tier="protocol_cpu",
|
|
)
|
|
with pytest.raises(ContractError, match="requires a stable error code"):
|
|
replace(evidence, error_code=None)
|
|
|
|
|
|
def test_evidence_never_overwrites_an_existing_attempt_record(tmp_path):
|
|
evidence = PreflightEvidence.failure(
|
|
run_id="1" * 32,
|
|
config_digest="2" * 64,
|
|
code="LMS_EVIDENCE_UNAVAILABLE",
|
|
world_size=2,
|
|
evidence_tier="candidate_preflight",
|
|
)
|
|
path = tmp_path / "evidence.json"
|
|
write_evidence(path, evidence)
|
|
with pytest.raises(ContractError, match="already exists"):
|
|
write_evidence(path, evidence)
|
|
|
|
|
|
def test_evidence_parent_must_be_private_and_cannot_be_a_symlink(tmp_path):
|
|
evidence = PreflightEvidence.failure(
|
|
run_id="1" * 32,
|
|
config_digest="2" * 64,
|
|
code="LMS_EVIDENCE_UNAVAILABLE",
|
|
world_size=2,
|
|
evidence_tier="candidate_preflight",
|
|
)
|
|
public = tmp_path / "public"
|
|
public.mkdir(mode=0o755)
|
|
# mkdir's mode is filtered by the caller's umask; this fixture must be public.
|
|
public.chmod(0o755)
|
|
assert public.stat().st_mode & 0o777 == 0o755
|
|
with pytest.raises(ContractError, match="private"):
|
|
write_evidence(public / "evidence.json", evidence)
|
|
private = tmp_path / "private"
|
|
private.mkdir(mode=0o700)
|
|
alias = tmp_path / "alias"
|
|
alias.symlink_to(private, target_is_directory=True)
|
|
with pytest.raises(ContractError):
|
|
write_evidence(alias / "evidence.json", evidence)
|
|
|
|
|
|
def test_evidence_decoder_rejects_noncanonical_and_duplicate_json():
|
|
duplicate = b'{"schema_version":1,"schema_version":1,"result":"failed"}\n'
|
|
with pytest.raises(ContractError, match="duplicate"):
|
|
PreflightEvidence.from_bytes(duplicate)
|
|
with pytest.raises(ContractError, match="byte envelope"):
|
|
PreflightEvidence.from_bytes(b"{}")
|
|
with pytest.raises(ContractError, match="strict UTF-8 JSON"):
|
|
PreflightEvidence.from_bytes(b"\xff\n")
|
|
with pytest.raises(ContractError, match="closed schema"):
|
|
PreflightEvidence.from_bytes(b"{}\n")
|
|
|
|
evidence = PreflightEvidence.failure(
|
|
run_id="1" * 32,
|
|
config_digest="2" * 64,
|
|
code="LMS_STAGE_TIMEOUT",
|
|
world_size=2,
|
|
evidence_tier="protocol_cpu",
|
|
)
|
|
noncanonical = json.dumps(json.loads(evidence.to_bytes()), indent=2).encode() + b"\n"
|
|
with pytest.raises(ContractError, match="not canonical"):
|
|
PreflightEvidence.from_bytes(noncanonical)
|
|
|
|
|
|
def test_lifecycle_receipt_uses_a_distinct_nonterminal_schema(tmp_path):
|
|
message = StageMessage(
|
|
run_id="1" * 32,
|
|
identity_digest="2" * 64,
|
|
rank=0,
|
|
sequence=1,
|
|
stage=RuntimeStage.PREFLIGHTED,
|
|
vote=Vote.PREPARED,
|
|
)
|
|
path = tmp_path / "prepared.stage.json"
|
|
write_stage_message(path, message)
|
|
assert read_stage_message(path) == message
|
|
with pytest.raises(ContractError):
|
|
read_evidence(path)
|
|
|
|
|
|
def test_record_observed_inside_its_publication_window_is_pending_not_published(
|
|
tmp_path, monkeypatch
|
|
):
|
|
"""Issue #305: a reader racing the link->unlink publication step."""
|
|
|
|
message = StageMessage(
|
|
run_id="1" * 32,
|
|
identity_digest="2" * 64,
|
|
rank=1,
|
|
sequence=2,
|
|
stage=RuntimeStage.ABORTED,
|
|
vote=Vote.ABORT,
|
|
error_code="LMS_RESOURCE_ADMISSION_DENIED",
|
|
)
|
|
path = tmp_path / ".rank-1.terminal.stage.json"
|
|
observed: list[BaseException] = []
|
|
real_link = os.link
|
|
|
|
def link_then_race_a_reader(*args, **kwargs):
|
|
real_link(*args, **kwargs)
|
|
# The target is now visible while the temporary name still exists.
|
|
with pytest.raises(PrivateRecordPending) as caught:
|
|
read_stage_message(path)
|
|
observed.append(caught.value)
|
|
|
|
monkeypatch.setattr(evidence_module.os, "link", link_then_race_a_reader)
|
|
write_stage_message(path, message)
|
|
monkeypatch.undo()
|
|
|
|
assert len(observed) == 1
|
|
# One-shot readers still refuse an unsettled record as a contract error.
|
|
assert isinstance(observed[0], ContractError)
|
|
assert read_stage_message(path) == message
|
|
assert [item.name for item in tmp_path.iterdir()] == [path.name]
|
|
|
|
|
|
def test_a_record_with_a_persistent_extra_link_is_never_accepted(tmp_path):
|
|
evidence = PreflightEvidence.failure(
|
|
run_id="1" * 32,
|
|
config_digest="2" * 64,
|
|
code="LMS_EVIDENCE_UNAVAILABLE",
|
|
world_size=2,
|
|
evidence_tier="protocol_cpu",
|
|
)
|
|
path = tmp_path / "evidence.json"
|
|
write_evidence(path, evidence)
|
|
os.link(path, tmp_path / "alias.json")
|
|
with pytest.raises(PrivateRecordPending, match="has not settled"):
|
|
read_evidence(path)
|
|
(tmp_path / "alias.json").unlink()
|
|
assert read_evidence(path) == evidence
|
|
path.chmod(0o644)
|
|
with pytest.raises(ContractError, match="not private and regular") as caught:
|
|
read_evidence(path)
|
|
assert not isinstance(caught.value, PrivateRecordPending)
|