mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 18:44:41 +01:00
203 lines
11 KiB
Python
203 lines
11 KiB
Python
"""Export authority is rechecked against exact receipts; no hardware is exercised."""
|
|
|
|
import json
|
|
from dataclasses import replace
|
|
from types import SimpleNamespace
|
|
from unittest.mock import Mock
|
|
|
|
import pytest
|
|
import torch
|
|
|
|
import obliteratus.distributed.export as export
|
|
import obliteratus.distributed.preflight as preflight
|
|
from obliteratus.distributed.contracts import ContractError, RuntimeContractError
|
|
from obliteratus.distributed.launcher import TorchrunEnvironment
|
|
from obliteratus.distributed.model_topology import TP2DeploymentLayout
|
|
from tests.test_distributed_export_gloo import _direct_runtime, _export_config
|
|
from tests.test_distributed_export_units import _live_session
|
|
from tests.test_distributed_rank_local import install_review, rank_config, review_record
|
|
from tests.test_distributed_rank_local_gloo import launch_for, snapshot_for
|
|
|
|
|
|
@pytest.fixture
|
|
def authority(tmp_path, monkeypatch):
|
|
def build(*, physical=False):
|
|
profile_root, runtime_root = tmp_path / "profile", tmp_path / "runtime"
|
|
profile_root.mkdir()
|
|
runtime_root.mkdir()
|
|
config = rank_config(profile_root)
|
|
if physical:
|
|
config = replace(config, device_kind="cuda", evidence_tier="candidate_preflight",
|
|
rank_storage=replace(config.rank_storage, scope="physical_support", tier="LMS-E3"))
|
|
config.validate()
|
|
source = preflight.SourceIdentity(
|
|
config.source_digest, config.model_digest, config.tokenizer_digest, 1, 1,
|
|
)
|
|
snapshots = tuple(replace(snapshot_for(config, rank, source), device_kind=config.device_kind)
|
|
for rank in range(2))
|
|
records = tuple(preflight._attestation(config, launch_for(config, rank), snapshots[rank])
|
|
for rank in range(2))
|
|
review = review_record(config, records)
|
|
if physical:
|
|
# Synthetic registry/observations test validation only, never qualification.
|
|
review["evidence"] = {key: "e" * 64 for key in ("LMS-E2", "LMS-E3", "LMS-E4")}
|
|
install_review(monkeypatch, review)
|
|
monkeypatch.setattr(TorchrunEnvironment, "from_environ",
|
|
staticmethod(lambda env, cfg: launch_for(cfg, 0)))
|
|
monkeypatch.setattr(preflight.SystemProbes, "collect", lambda self, cfg, launch: snapshots[launch.rank])
|
|
runtime = _direct_runtime(runtime_root)
|
|
root = config.staging_path.stat()
|
|
runtime = replace(runtime, rank_local_config=config, rank_local_attestations=records,
|
|
deployment_layout=TP2DeploymentLayout.TWO_HOST_TP2,
|
|
rank_local_root_identity=(root.st_dev, root.st_ino),
|
|
tensor_device=torch.device(config.device_kind),
|
|
manifest=replace(runtime.manifest, adapter_id=config.model.adapter_id),
|
|
source_identity=source, runtime_config_digest=config.digest,
|
|
hf_config_digest=config.model.hf_config_digest,
|
|
**{name: getattr(config, name) for name in (
|
|
"run_id", "source_path", "staging_path", "storage_digest", "code_digest",
|
|
"commit_sha", "placement_plan_digest", "max_source_files", "max_source_bytes",
|
|
"max_source_file_bytes", "source_timeout_seconds",
|
|
)})
|
|
assert export._rank_local_authorized(runtime) is True
|
|
return runtime, snapshots[0]
|
|
return build
|
|
|
|
|
|
@pytest.mark.parametrize("field,value,message", [
|
|
("deployment_layout", TP2DeploymentLayout.LOCAL_TP2, "layout differs"),
|
|
("runtime_config_digest", "f" * 64, "export identity differs"),
|
|
("rank_local_attestations", (), "hardware evidence is unavailable"),
|
|
("hf_config_digest", "f" * 64, "immutable model bindings differ"),
|
|
("rank_local_root_identity", (0, 0), "root changed"),
|
|
])
|
|
def test_export_refuses_changed_authority_after_positive_control(authority, field, value, message):
|
|
runtime, _ = authority()
|
|
with pytest.raises(ContractError, match=message):
|
|
export._rank_local_authorized(replace(runtime, **{field: value}))
|
|
|
|
|
|
@pytest.mark.parametrize("field", ["max_source_files", "max_source_bytes", "max_source_file_bytes", "source_timeout_seconds"])
|
|
def test_export_cannot_expand_reviewed_source_inspection_bounds(authority, field):
|
|
runtime, _ = authority()
|
|
with pytest.raises(ContractError, match="reviewed resource bounds"):
|
|
export._rank_local_authorized(replace(runtime, **{field: getattr(runtime, field) + 1}))
|
|
|
|
|
|
def test_export_refuses_rank_local_config_without_storage_mapping(authority):
|
|
runtime, _ = authority()
|
|
unowned = replace(runtime.rank_local_config, rank_storage=None)
|
|
with pytest.raises(ContractError, match="layout differs"):
|
|
export._rank_local_authorized(replace(runtime, rank_local_config=unowned))
|
|
|
|
|
|
@pytest.mark.parametrize("change", ["adapter", "launch", "device-observation", "root-replacement"])
|
|
def test_physical_authority_rechecks_exact_observed_identity_on_cpu(authority, monkeypatch, change):
|
|
runtime, snapshot = authority(physical=True)
|
|
if change == "adapter":
|
|
runtime = replace(runtime, manifest=replace(runtime.manifest, adapter_id="unreviewed"))
|
|
message = "model adapter differs"
|
|
elif change == "launch":
|
|
monkeypatch.setattr(TorchrunEnvironment, "from_environ",
|
|
staticmethod(lambda env, cfg: launch_for(cfg, 1)))
|
|
message = "launch identity differs"
|
|
elif change == "device-observation":
|
|
monkeypatch.setattr(preflight.SystemProbes, "collect",
|
|
lambda *args: replace(snapshot, device_identity="replacement-device"))
|
|
message = "runtime observation differs"
|
|
else:
|
|
runtime.staging_path.rename(runtime.staging_path.with_name("preserved-root"))
|
|
runtime.staging_path.mkdir(mode=0o700)
|
|
(runtime.staging_path / "foreign").write_text("preserve")
|
|
message = "root changed"
|
|
with pytest.raises(ContractError, match=message):
|
|
export._rank_local_authorized(runtime)
|
|
if change == "root-replacement":
|
|
assert (runtime.staging_path / "foreign").read_text() == "preserve"
|
|
|
|
|
|
def test_physical_authority_allows_only_free_capacity_to_vary(authority, monkeypatch):
|
|
runtime, snapshot = authority(physical=True)
|
|
current = replace(snapshot, free_device_memory_bytes=1,
|
|
free_host_memory_bytes=2, free_staging_bytes=3)
|
|
monkeypatch.setattr(preflight.SystemProbes, "collect", lambda *args: current)
|
|
assert export._rank_local_authorized(runtime) is True
|
|
# Authority does not replace the later resource admission check.
|
|
changed = replace(current, total_host_memory_bytes=snapshot.total_host_memory_bytes + 1)
|
|
monkeypatch.setattr(preflight.SystemProbes, "collect", lambda *args: changed)
|
|
with pytest.raises(ContractError, match="runtime observation differs"):
|
|
export._rank_local_authorized(runtime)
|
|
|
|
|
|
@pytest.mark.parametrize("receipt", [
|
|
{"prior": True, "entries": 0}, {"prior": -1, "entries": 0},
|
|
{"prior": 0, "entries": False}, {"prior": 0, "entries": -1},
|
|
{"prior": 0}, {"prior": 0, "entries": 0, "unowned": 1},
|
|
])
|
|
def test_corrupt_peer_capacity_cannot_admit_export(authority, monkeypatch, receipt):
|
|
runtime, _ = authority()
|
|
monkeypatch.setattr(export, "unanimous_vote", lambda _, accepted, **kwargs: accepted)
|
|
monkeypatch.setattr(export, "gloo_all_gather_records", lambda record, **kwargs: (
|
|
json.dumps(record).encode(), json.dumps(receipt).encode(),
|
|
))
|
|
output = runtime.staging_path / "canonical-output"
|
|
with pytest.raises(RuntimeContractError) as failure:
|
|
export._resource_admission(runtime, _export_config(), export._assignments(runtime.manifest), (), output)
|
|
assert failure.value.code == "LMS_EXPORT_RESOURCE_DENIED"
|
|
assert not output.exists()
|
|
assert list(runtime.staging_path.iterdir()) == []
|
|
|
|
|
|
def test_live_rank_local_entry_revalidates_review_before_dispatch(authority, tmp_path, monkeypatch):
|
|
runtime, _ = authority()
|
|
session_root = tmp_path / "live-session"
|
|
session_root.mkdir()
|
|
session, verified, _ = _live_session(session_root)
|
|
object.__setattr__(session, "config", runtime.rank_local_config)
|
|
session.handle.plan_digest = runtime.placement_plan_digest
|
|
session.handle.source_identity = runtime.source_identity
|
|
session.handle.config.config_digest = runtime.hf_config_digest
|
|
session.preflight.source_identity = runtime.source_identity
|
|
session.preflight.attestations = runtime.rank_local_attestations
|
|
session.preflight.rank_local_root_identities = (runtime.rank_local_root_identity,) * 2
|
|
monkeypatch.setattr(export, "classify_tp2_topology", lambda *args: SimpleNamespace(
|
|
layout=TP2DeploymentLayout.TWO_HOST_TP2))
|
|
monkeypatch.setattr(export, "unanimous_vote", lambda _, accepted, **kwargs: accepted)
|
|
dispatch = Mock(return_value=object())
|
|
monkeypatch.setattr(export, "run_distributed_export", dispatch)
|
|
result = export.run_live_distributed_export(
|
|
session, verified, _export_config(), semantic_reload_validator=lambda *args: None,
|
|
)
|
|
assert result is dispatch.return_value
|
|
capability = dispatch.call_args.args[0]
|
|
assert capability.rank_local_config is runtime.rank_local_config
|
|
assert capability.rank_local_attestations == runtime.rank_local_attestations
|
|
assert capability.rank_local_root_identity == runtime.rank_local_root_identity
|
|
assert capability.deployment_layout is TP2DeploymentLayout.TWO_HOST_TP2
|
|
assert capability.model is session.handle.model
|
|
|
|
|
|
def test_failed_cleanup_vote_without_verified_capability_preserves_prior_output(tmp_path, monkeypatch):
|
|
runtime = _direct_runtime(tmp_path)
|
|
output = runtime.staging_path / "canonical-output"
|
|
output.mkdir()
|
|
(output / "prior.txt").write_text("prior")
|
|
votes = []
|
|
|
|
def vote(sequence, accepted, **kwargs):
|
|
votes.append((sequence, accepted))
|
|
# A peer can report incomplete cleanup even when this rank has no
|
|
# transaction or staging directory to retire.
|
|
return False if sequence == 23_999_999 else accepted
|
|
|
|
monkeypatch.setattr(export, "unanimous_vote", vote)
|
|
reload = Mock(side_effect=AssertionError("invalid capability must not reload"))
|
|
with pytest.raises(RuntimeContractError) as failure:
|
|
export.run_distributed_export(runtime, object(), _export_config(), semantic_reload_validator=reload)
|
|
assert failure.value.code == "LMS_EXPORT_CLEANUP_FAILED"
|
|
reload.assert_not_called()
|
|
assert votes == [(17_000_000, False), (23_999_999, True)]
|
|
assert (output / "prior.txt").read_text() == "prior"
|
|
assert set(runtime.staging_path.iterdir()) == {output}
|
|
assert not (output / "export-manifest.json").exists()
|