mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 10:34:23 +01:00
Integrate bounded admission, UI ownership and controls, opt-in durable MCP execution, rank-local TP2 groundwork, regression hardening and operator guides. Physical larger-memory and two-host qualification remain separate.
444 lines
19 KiB
Python
444 lines
19 KiB
Python
"""Synthetic rank-local authority, identity, framing and crash-recovery contracts."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import json
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
from dataclasses import replace
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
import obliteratus.distributed.rank_local as local
|
|
from obliteratus.distributed.config import DistributedModelConfig
|
|
from obliteratus.distributed.config import DistributedPreflightConfig
|
|
from obliteratus.distributed.contracts import ContractError
|
|
from obliteratus.distributed.model_registry import LLAMA_BF16_TP2_PROFILE
|
|
from tests.test_distributed_launcher import _profile
|
|
|
|
|
|
def rank_config(root: Path, sources: tuple[Path, Path] | None = None):
|
|
control = root / "control"
|
|
control.mkdir()
|
|
config = _profile(control)
|
|
ranks = []
|
|
for rank in range(2):
|
|
directory = root / f"rank-{rank}"
|
|
directory.mkdir(mode=0o700)
|
|
source = sources[rank] if sources else directory / "source"
|
|
if sources is None:
|
|
source.mkdir()
|
|
staging = directory / "staging"
|
|
staging.mkdir(mode=0o700)
|
|
from obliteratus.distributed.preflight import storage_mount_digest
|
|
ranks.append(local.RankStorage(rank, source, staging, storage_mount_digest(staging)))
|
|
mapping = local.RankLocalStorage(1, "synthetic-tp2", "qualification", "LMS-E1", tuple(ranks))
|
|
config = replace(config, schema_version=3,
|
|
model=DistributedModelConfig(LLAMA_BF16_TP2_PROFILE.adapter_id, "a" * 40, "b" * 64, 5),
|
|
rank_storage=mapping, device_kind="cpu", device_name="cpu", compute_capability="none",
|
|
evidence_tier="protocol_cpu", source_path=ranks[0].source, staging_path=ranks[0].staging,
|
|
storage_digest=ranks[0].storage_digest,
|
|
evidence_path=ranks[0].staging / f"{config.run_id}.rank-0" / "preflight.json")
|
|
return config.validate()
|
|
|
|
|
|
def review_record(config, attestations=()):
|
|
bindings = local.review_bindings(config, attestations or None)
|
|
bindings.setdefault("hardware_digest", "0" * 64)
|
|
storage = config.rank_storage
|
|
return {"profile_id": storage.profile_id, "scope": storage.scope, "tier": storage.tier,
|
|
"mapping_digest": storage.mapping_digest, "policy_digest": local.policy_digest(),
|
|
"bindings": bindings, "evidence": {},
|
|
"review": "https://github.com/elder-plinius/OBLITERATUS/pull/287"}
|
|
|
|
|
|
def install_review(monkeypatch, record):
|
|
monkeypatch.setattr(local, "read_review_registry", lambda: {
|
|
"schema_version": 1, "owner": "OBLITERATUS maintainers", "profiles": [record]})
|
|
|
|
|
|
def test_mapping_selection_and_exclusive_attempts(tmp_path):
|
|
config = rank_config(tmp_path)
|
|
first = config.for_rank(0)
|
|
second = config.for_rank(1)
|
|
assert first.digest == second.digest
|
|
assert first.staging_path != second.staging_path
|
|
assert first.evidence_path != second.evidence_path
|
|
paths = [local.create_attempt(config.for_rank(rank).staging_path, config.run_id, rank) for rank in range(2)]
|
|
assert paths[0] != paths[1]
|
|
for rank, path in enumerate(paths):
|
|
assert path.stat().st_mode & 0o777 == 0o700
|
|
assert json.loads((path / "owner.json").read_text())["rank"] == rank
|
|
with pytest.raises(FileExistsError):
|
|
local.create_attempt(path.parent, config.run_id, rank)
|
|
with pytest.raises(ContractError):
|
|
config.for_rank(True)
|
|
with pytest.raises(ContractError):
|
|
replace(config, source_path=second.source_path).validate()
|
|
|
|
|
|
def test_v3_profile_parser_is_closed_and_preserves_v2_fields(tmp_path):
|
|
config = rank_config(tmp_path)
|
|
payload = json.loads((tmp_path / "control" / "profile.json").read_text())
|
|
payload["schema_version"] = 3
|
|
payload["rank_storage"] = {"schema_version": 1, "profile_id": "synthetic-tp2",
|
|
"scope": "qualification", "tier": "LMS-E1", "ranks": [
|
|
{"rank": row.rank, "source": str(row.source), "staging": str(row.staging),
|
|
"storage_digest": row.storage_digest} for row in config.rank_storage.ranks]}
|
|
payload["source"]["path"] = str(config.source_path)
|
|
payload["staging"] = {"path": str(config.staging_path), "storage_digest": config.storage_digest}
|
|
payload["evidence"]["path"] = str(config.evidence_path)
|
|
payload["execution"].update(device_kind="cpu", device_name="cpu", compute_capability="none", evidence_tier="protocol_cpu")
|
|
payload["model"] = {"adapter_id": config.model.adapter_id, "artifact_revision": config.model.artifact_revision,
|
|
"hf_config_digest": config.model.hf_config_digest, "load_timeout_seconds": 5}
|
|
path = tmp_path / "profile-v3.json"
|
|
path.write_text(json.dumps(payload))
|
|
parsed = DistributedPreflightConfig.from_file(path)
|
|
assert parsed.rank_storage == config.rank_storage
|
|
assert parsed.for_rank(1).require_model() == config.model
|
|
payload["rank_storage"]["ranks"][0]["peer_path"] = "/forbidden"
|
|
path.write_text(json.dumps(payload))
|
|
with pytest.raises(ContractError):
|
|
DistributedPreflightConfig.from_file(path)
|
|
|
|
|
|
@pytest.mark.parametrize("mutation", ("order", "duplicate", "relative", "overlap", "version", "tier", "scope"))
|
|
def test_mapping_rejects_incomplete_and_claimed_support(tmp_path, mutation):
|
|
config = rank_config(tmp_path)
|
|
storage = config.rank_storage
|
|
with pytest.raises(ContractError):
|
|
if mutation == "order":
|
|
replace(storage, ranks=tuple(reversed(storage.ranks)))
|
|
elif mutation == "duplicate":
|
|
replace(storage, ranks=(storage.ranks[0], storage.ranks[0]))
|
|
elif mutation == "relative":
|
|
replace(storage.ranks[0], staging=Path("relative"))
|
|
elif mutation == "overlap":
|
|
replace(storage.ranks[0], staging=storage.ranks[0].source / "nested")
|
|
elif mutation == "version":
|
|
replace(storage, schema_version=True)
|
|
elif mutation == "tier":
|
|
replace(storage, tier="LMS-E0")
|
|
else:
|
|
replace(storage, scope="physical_support")
|
|
|
|
|
|
def test_closed_registry_empty_and_exact_review(tmp_path, monkeypatch):
|
|
config = rank_config(tmp_path)
|
|
with pytest.raises(ContractError, match="no maintainer approval"):
|
|
local.config_review(config)
|
|
record = review_record(config)
|
|
install_review(monkeypatch, record)
|
|
assert len(local.config_review(config)) == 64
|
|
for key in ("code_digest", "software_digest", "adapter_digest", "execution_digest"):
|
|
original = record["bindings"][key]
|
|
record["bindings"][key] = "f" * 64
|
|
with pytest.raises(ContractError):
|
|
local.config_review(config)
|
|
record["bindings"][key] = original
|
|
record["policy_digest"] = "f" * 64
|
|
with pytest.raises(ContractError, match="policy"):
|
|
local.config_review(config)
|
|
|
|
|
|
def test_physical_review_never_uses_e1(tmp_path, monkeypatch):
|
|
config = rank_config(tmp_path)
|
|
storage = replace(config.rank_storage, scope="physical_support", tier="LMS-E3")
|
|
config = replace(config, rank_storage=storage, device_kind="cuda", evidence_tier="candidate_preflight")
|
|
record = review_record(config)
|
|
install_review(monkeypatch, record)
|
|
with pytest.raises(ContractError, match="E2, E3 and E4"):
|
|
local.config_review(config)
|
|
record["evidence"] = {"LMS-E1": "a" * 64, "LMS-E2": "b" * 64, "LMS-E3": "c" * 64, "LMS-E4": "d" * 64}
|
|
with pytest.raises(ContractError, match="synthetic evidence"):
|
|
local.config_review(config)
|
|
record["evidence"].pop("LMS-E1")
|
|
assert local.config_review(config)
|
|
with pytest.raises(ContractError, match="CPU protocol"):
|
|
replace(config, device_kind="cpu").validate()
|
|
|
|
|
|
def test_cpu_runtime_cannot_assert_a_physical_profile(tmp_path):
|
|
import torch
|
|
from types import SimpleNamespace
|
|
from obliteratus.distributed.export import _rank_local_authorized
|
|
from obliteratus.distributed.model_topology import TP2DeploymentLayout
|
|
config = rank_config(tmp_path)
|
|
config = replace(config, device_kind="cuda", evidence_tier="candidate_preflight",
|
|
rank_storage=replace(config.rank_storage, tier="LMS-E3"))
|
|
runtime = SimpleNamespace(rank=0, rank_local_config=config, tensor_device=torch.device("cpu"),
|
|
deployment_layout=TP2DeploymentLayout.TWO_HOST_TP2)
|
|
with pytest.raises(ContractError, match="protocol device"):
|
|
_rank_local_authorized(runtime)
|
|
|
|
|
|
@pytest.mark.parametrize("field", ("offset", "size", "total", "identity", "file_digest", "chunk_digest", "schema_version"))
|
|
def test_chunk_rejects_corruption_replay_and_wrong_identity(field):
|
|
data = b"exact bytes"
|
|
digest = hashlib.sha256(data).hexdigest()
|
|
record = {"schema_version": 1, "identity": "a" * 64, "file_digest": digest,
|
|
"offset": 0, "size": len(data), "total": len(data), "chunk_digest": digest}
|
|
local.validate_chunk(record, identity="a" * 64, file_digest=digest, offset=0,
|
|
size=len(data), total=len(data), data=data)
|
|
record[field] = 1 if field in {"offset", "size", "total", "schema_version"} else "b" * 64
|
|
if field == "schema_version":
|
|
record[field] = 2
|
|
with pytest.raises(ContractError):
|
|
local.validate_chunk(record, identity="a" * 64, file_digest=digest, offset=0,
|
|
size=len(data), total=len(data), data=data)
|
|
with pytest.raises(ContractError):
|
|
local.validate_chunk({}, identity="a" * 64, file_digest=digest, offset=0,
|
|
size=len(data), total=len(data), data=data[:-1])
|
|
|
|
|
|
def test_owned_root_replacement_and_privacy(tmp_path):
|
|
root = tmp_path / "root"
|
|
root.mkdir(mode=0o700)
|
|
pinned = local.PinnedDirectory(root)
|
|
try:
|
|
root.rename(tmp_path / "old")
|
|
root.mkdir(mode=0o700)
|
|
with pytest.raises(ContractError, match="replaced"):
|
|
pinned.check()
|
|
finally:
|
|
pinned.close()
|
|
root.chmod(0o755)
|
|
with pytest.raises(ContractError, match="private"):
|
|
local.PinnedDirectory(root)
|
|
|
|
|
|
def test_durable_publication_concurrent_publisher_and_owned_cleanup(tmp_path):
|
|
tmp_path.chmod(0o700)
|
|
prior = tmp_path / "output"
|
|
prior.mkdir()
|
|
(prior / "prior").write_text("old")
|
|
publisher = local.DurablePublication(tmp_path, "output", "1" * 32)
|
|
other = local.DurablePublication(tmp_path, "output", "2" * 32)
|
|
try:
|
|
staging = publisher.__enter__()
|
|
(staging / "new").write_text("new")
|
|
with pytest.raises(ContractError, match="completed promotion"):
|
|
publisher.decide("a" * 64)
|
|
with pytest.raises(FileExistsError):
|
|
other.__enter__()
|
|
publisher.promote(lambda path: None)
|
|
assert (prior / "new").read_text() == "new"
|
|
assert local.recovery_inventory(tmp_path, "output", "1" * 32)["outcome"] == "uncertain"
|
|
with pytest.raises(ContractError, match="durable commit decision"):
|
|
publisher.acknowledge()
|
|
assert (tmp_path / publisher.backup_name / "prior").read_text() == "old"
|
|
publisher.decide("a" * 64)
|
|
publisher.acknowledge()
|
|
assert local.recovery_inventory(tmp_path, "output", "1" * 32)["outcome"] == "complete"
|
|
assert not list(tmp_path.glob("*.rank-local-backup-*"))
|
|
finally:
|
|
publisher.close()
|
|
other.close()
|
|
|
|
|
|
def test_post_decision_uncertainty_never_adopts_a_competing_publishers_fence(tmp_path):
|
|
tmp_path.chmod(0o700)
|
|
publisher = local.DurablePublication(tmp_path, "output", "1" * 32)
|
|
competitor = local.DurablePublication(tmp_path, "output", "2" * 32)
|
|
try:
|
|
staging = publisher.__enter__()
|
|
(staging / "new").write_text("new")
|
|
publisher.promote(lambda path: None)
|
|
publisher.decide("a" * 64)
|
|
publisher.acknowledge()
|
|
competitor.__enter__()
|
|
competing_identity = competitor.identities[competitor.lock]
|
|
publisher.abort()
|
|
state = local.recovery_inventory(tmp_path, "output", "1" * 32)
|
|
assert state["outcome"] == "uncertain" and state["decision"] == "commit"
|
|
competitor._check_owned(competitor.lock)
|
|
assert competitor.identities[competitor.lock] == competing_identity
|
|
competitor.abort()
|
|
finally:
|
|
publisher.close()
|
|
competitor.close()
|
|
|
|
|
|
@pytest.mark.parametrize("phase", ("PREPARED", "STAGED", "PROMOTION_INTENT", "INTENT_LINKED", "BACKUP_RENAMED", "BACKUP_SYNCED", "BACKED_UP", "RENAMED", "DESTINATION_SYNCED", "PROMOTED", "COMMIT_DECIDED", "ACKNOWLEDGED", "BACKUP_REMOVED", "COMPLETE"))
|
|
def test_hard_crash_windows_are_fenced_and_recoverable(tmp_path, phase):
|
|
tmp_path.chmod(0o700)
|
|
prior = tmp_path / "output"
|
|
prior.mkdir()
|
|
(prior / "prior").write_text("prior")
|
|
script = """
|
|
import os, sys
|
|
from pathlib import Path
|
|
from obliteratus.distributed.rank_local import DurablePublication
|
|
root = Path(sys.argv[1])
|
|
phase = sys.argv[2]
|
|
def fault(point):
|
|
if point == phase:
|
|
os._exit(23)
|
|
publisher = DurablePublication(root, 'output', '1' * 32, fault)
|
|
staging = publisher.__enter__()
|
|
(staging / 'new').write_text('new')
|
|
publisher.promote(lambda path: None)
|
|
publisher.decide('a' * 64)
|
|
publisher.acknowledge()
|
|
"""
|
|
completed = subprocess.run([sys.executable, "-c", script, str(tmp_path), phase], check=False, timeout=20)
|
|
assert completed.returncode == 23
|
|
inventory = local.recovery_inventory(tmp_path, "output", "1" * 32)
|
|
assert inventory["outcome"] == ("not_published" if phase in {"PREPARED", "STAGED"} else "complete" if phase == "COMPLETE" else "uncertain")
|
|
assert inventory["fence_present"]
|
|
if phase == "BACKED_UP":
|
|
assert not prior.exists()
|
|
assert (tmp_path / f".output.rank-local-backup-{'1' * 32}" / "prior").read_text() == "prior"
|
|
publisher = local.DurablePublication(tmp_path, "output", "2" * 32)
|
|
try:
|
|
with pytest.raises(FileExistsError):
|
|
publisher.__enter__()
|
|
finally:
|
|
publisher.close()
|
|
|
|
|
|
@pytest.mark.parametrize("failure_phase", ("initial_link", "intent_link"))
|
|
def test_parent_sync_failure_prevents_every_destructive_rename(tmp_path, monkeypatch, failure_phase):
|
|
tmp_path.chmod(0o700)
|
|
output = tmp_path / "output"
|
|
output.mkdir()
|
|
(output / "prior").write_text("prior")
|
|
publisher = local.DurablePublication(tmp_path, "output", "1" * 32)
|
|
original_sync = os.fsync
|
|
original_rename = os.rename
|
|
renames = []
|
|
armed = [failure_phase == "initial_link"]
|
|
|
|
def sync(descriptor):
|
|
if descriptor == publisher.root.fd and armed[0]:
|
|
armed[0] = False
|
|
raise OSError("injected parent-root durability failure")
|
|
return original_sync(descriptor)
|
|
|
|
def rename(source, destination, **kwargs):
|
|
renames.append((source, destination))
|
|
return original_rename(source, destination, **kwargs)
|
|
|
|
monkeypatch.setattr(os, "fsync", sync)
|
|
monkeypatch.setattr(os, "rename", rename)
|
|
try:
|
|
if failure_phase == "initial_link":
|
|
with pytest.raises(OSError, match="durability failure"):
|
|
publisher.__enter__()
|
|
else:
|
|
staging = publisher.__enter__()
|
|
(staging / "new").write_text("new")
|
|
armed[0] = True
|
|
with pytest.raises(OSError, match="durability failure"):
|
|
publisher.promote(lambda path: None)
|
|
assert renames == []
|
|
assert (output / "prior").read_text() == "prior"
|
|
assert not (tmp_path / publisher.backup_name).exists()
|
|
assert (tmp_path / publisher.lock).is_dir()
|
|
finally:
|
|
publisher.close()
|
|
|
|
|
|
def test_intent_and_fence_are_parent_synced_before_backup_rename(tmp_path, monkeypatch):
|
|
tmp_path.chmod(0o700)
|
|
output = tmp_path / "output"
|
|
output.mkdir()
|
|
(output / "prior").write_text("prior")
|
|
events = []
|
|
publisher = local.DurablePublication(tmp_path, "output", "1" * 32, events.append)
|
|
original_sync = os.fsync
|
|
original_rename = os.rename
|
|
|
|
def sync(descriptor):
|
|
if descriptor == publisher.root.fd:
|
|
events.append("ROOT_SYNC")
|
|
return original_sync(descriptor)
|
|
|
|
def rename(source, destination, **kwargs):
|
|
if source == "output":
|
|
events.append("OLD_OUTPUT_RENAME")
|
|
elif source == publisher.staging_name:
|
|
events.append("STAGING_RENAME")
|
|
return original_rename(source, destination, **kwargs)
|
|
|
|
monkeypatch.setattr(os, "fsync", sync)
|
|
monkeypatch.setattr(os, "rename", rename)
|
|
try:
|
|
staging = publisher.__enter__()
|
|
(staging / "new").write_text("new")
|
|
publisher.promote(lambda path: None)
|
|
publisher.decide("a" * 64)
|
|
publisher.acknowledge()
|
|
assert events.index("ROOT_SYNC") < events.index("PREPARED")
|
|
intent = events.index("PROMOTION_INTENT")
|
|
assert intent < events.index("ROOT_SYNC", intent) < events.index("OLD_OUTPUT_RENAME")
|
|
backup = events.index("BACKUP_RENAMED")
|
|
assert backup < events.index("ROOT_SYNC", backup) < events.index("STAGING_RENAME")
|
|
destination = events.index("STAGING_RENAME")
|
|
assert destination < events.index("ROOT_SYNC", destination) < events.index("PROMOTED")
|
|
decision = events.index("COMMIT_DECIDED")
|
|
assert decision < events.index("ROOT_SYNC", decision) < events.index("BACKUP_REMOVED")
|
|
finally:
|
|
publisher.close()
|
|
|
|
|
|
def test_foreign_staging_is_never_cleaned(tmp_path):
|
|
tmp_path.chmod(0o700)
|
|
publisher = local.DurablePublication(tmp_path, "output", "1" * 32)
|
|
try:
|
|
staging = publisher.__enter__()
|
|
staging.rename(tmp_path / "owned-retained")
|
|
staging.mkdir(mode=0o700)
|
|
(staging / "foreign").write_text("preserve")
|
|
with pytest.raises(ContractError, match="replaced"):
|
|
publisher.abort()
|
|
assert (staging / "foreign").read_text() == "preserve"
|
|
finally:
|
|
publisher.close()
|
|
|
|
|
|
def test_replaced_destination_is_preserved_and_publication_quarantined(tmp_path):
|
|
tmp_path.chmod(0o700)
|
|
output = tmp_path / "output"
|
|
output.mkdir()
|
|
(output / "prior").write_text("prior")
|
|
publisher = local.DurablePublication(tmp_path, "output", "1" * 32)
|
|
try:
|
|
staging = publisher.__enter__()
|
|
(staging / "new").write_text("new")
|
|
output.rename(tmp_path / "retained-prior")
|
|
output.mkdir()
|
|
(output / "foreign").write_text("preserve")
|
|
with pytest.raises(ContractError, match="replaced"):
|
|
publisher.promote(lambda path: None)
|
|
publisher.abort()
|
|
assert (output / "foreign").read_text() == "preserve"
|
|
assert local.recovery_inventory(tmp_path, "output", "1" * 32)["outcome"] == "uncertain"
|
|
finally:
|
|
publisher.close()
|
|
|
|
|
|
def test_optimized_python_guards_survive():
|
|
script = """
|
|
from pathlib import Path
|
|
from obliteratus.distributed.rank_local import RankLocalStorage, RankStorage, validate_chunk, validate_host_capacity
|
|
from obliteratus.distributed.contracts import ContractError
|
|
checks = [lambda: RankStorage(True, Path('/source'), Path('/stage'), 'a'*64),
|
|
lambda: RankLocalStorage(1, 'test', 'physical_support', 'LMS-E1', ()),
|
|
lambda: validate_chunk({}, identity='a'*64, file_digest='b'*64,
|
|
offset=0, size=1, total=1, data=b'x'),
|
|
lambda: validate_host_capacity({'schema_version': 1, 'rank': 0, 'host_digest': 'a'*64,
|
|
'domain': 'operator-estimate', 'total_host_memory_bytes': 1},
|
|
0, 'a'*64)]
|
|
for check in checks:
|
|
try:
|
|
check()
|
|
except ContractError:
|
|
continue
|
|
raise SystemExit('guard was bypassed')
|
|
"""
|
|
subprocess.run([sys.executable, "-O", "-c", script], check=True, timeout=20)
|