"""Verified export continuation with local storage and tiny CPU models.""" from __future__ import annotations from dataclasses import replace import hashlib import json from unittest.mock import Mock import weakref import pytest import torch from safetensors.torch import save_file from transformers import LlamaConfig, LlamaForCausalLM from obliteratus.models import checkpoint_export as export from obliteratus.persistence_contracts import atomic_checkpoint_directory pytestmark = pytest.mark.cpu CANDIDATE = hashlib.sha256(b"verified immutable live-weight manifest").hexdigest() @pytest.fixture def model_plan(): torch.manual_seed(71) model = LlamaForCausalLM(LlamaConfig( vocab_size=16, hidden_size=8, intermediate_size=16, num_hidden_layers=2, num_attention_heads=2, num_key_value_heads=2, tie_word_embeddings=False, )) plan = export.plan_native_export(model, 2000) assert plan is not None and len(plan.shards) >= 3 return model, plan class Store: def __init__(self): self.files = {} self.receipts = {} self.restored = [] def record(self, path, receipt): self.files[receipt.filename] = path.read_bytes() self.receipts[receipt.filename] = receipt def restore(self, receipt, path): self.restored.append(receipt.filename) path.write_bytes(self.files[receipt.filename]) def continuation(self, **kwargs): return replace(export.ExportContinuation( CANDIDATE, tuple(self.receipts.values()), self.restore, self.record, ), **kwargs) def write(model, plan, destination, **kwargs): with atomic_checkpoint_directory(destination) as staging: export.write_native_checkpoint(model, staging, plan, **kwargs) def snapshot(directory): return {p.name: p.read_bytes() for p in directory.iterdir() if p.is_file()} def test_interrupted_export_reuses_only_verified_shards(tmp_path, monkeypatch, model_plan): model, plan = model_plan store = Store() def interrupted_record(path, receipt): store.record(path, receipt) if len(store.receipts) == 2: raise OSError("storage interrupted after durable commit") with pytest.raises(OSError, match="storage interrupted"): write(model, plan, tmp_path / "interrupted", continuation=store.continuation( record_shard=interrupted_record, )) assert not (tmp_path / "interrupted").exists() assert len(store.receipts) == 2 fresh = tmp_path / "fresh" write(model, plan, fresh) materialize = Mock(wraps=export._materialize_tensor) monkeypatch.setattr(export, "_materialize_tensor", materialize) budget = Mock(return_value=10**12) events, finalized = [], [] resumed = tmp_path / "resumed" write(model, plan, resumed, continuation=store.continuation(), host_budget=budget, on_shard_progress=events.append, on_shard_finalized=lambda path, name: finalized.append((name, path.exists()))) assert snapshot(resumed) == snapshot(fresh) assert store.restored == [shard.filename for shard in plan.shards[:2]] skipped = {name for shard in plan.shards[:2] for name in shard.tensor_names} assert {call.args[1] for call in materialize.call_args_list} == set(plan.tensors) - skipped assert budget.call_count == len(plan.shards) - 2 assert finalized == [(shard.filename, True) for shard in plan.shards] complete = [event for event in events if event.event == "complete"] assert [event.reused for event in complete] == [True, True] + [False] * (len(plan.shards) - 2) assert [event.index for event in complete] == list(range(1, len(plan.shards) + 1)) assert all(event.total == len(plan.shards) for event in events) assert all(event.completed_bytes > 0 for event in complete) assert json.loads((resumed / "model.safetensors.index.json").read_text()) == plan.index reloaded = LlamaForCausalLM.from_pretrained(resumed, local_files_only=True) for name, value in model.state_dict().items(): torch.testing.assert_close(reloaded.state_dict()[name], value, rtol=0, atol=0) def test_all_restored_shards_skip_staging_admission(tmp_path, monkeypatch, model_plan): model, plan = model_plan store = Store() write(model, plan, tmp_path / "first", continuation=store.continuation()) forbidden = Mock(side_effect=AssertionError("restoration materialized weights")) monkeypatch.setattr(export, "_materialize_tensor", forbidden) write(model, plan, tmp_path / "second", continuation=store.continuation(), host_budget=forbidden) forbidden.assert_not_called() assert snapshot(tmp_path / "second") == snapshot(tmp_path / "first") def test_receipt_publication_occurs_after_tensor_release(tmp_path, monkeypatch, model_plan): model, plan = model_plan materialize = export._materialize_tensor tensors = [] calls = [] def track(*args): tensor = materialize(*args) tensors.append(weakref.ref(tensor)) return tensor def record(path, receipt): assert all(ref() is None for ref in tensors) assert hashlib.sha256(path.read_bytes()).hexdigest() == receipt.sha256 assert path.stat().st_size == receipt.size_bytes calls.append(receipt) monkeypatch.setattr(export, "_materialize_tensor", track) write(model, plan, tmp_path / "checkpoint", continuation=export.ExportContinuation( CANDIDATE, record_shard=record, )) assert len(calls) == len(plan.shards) @pytest.mark.parametrize("change,match", [ ({"candidate_sha256": "f" * 64}, "candidate differs"), ({"plan_sha256": "f" * 64}, "plan differs"), ({"filename": "unknown.safetensors"}, "unknown or duplicated"), ({"filename": "../escape.safetensors"}, "unknown or duplicated"), ({"size_bytes": 0}, "content identity"), ({"size_bytes": True}, "content identity"), ({"sha256": "bad"}, "content identity"), ]) def test_invalid_receipts_are_rejected_before_writes(tmp_path, monkeypatch, model_plan, change, match): model, plan = model_plan receipt = export.ExportShardReceipt( CANDIDATE, export.native_export_plan_sha256(plan), plan.shards[0].filename, 123, "a" * 64, ) forbidden = Mock(side_effect=AssertionError("invalid receipt reached write")) monkeypatch.setattr(export, "_materialize_tensor", forbidden) with pytest.raises(ValueError, match=match): export.write_native_checkpoint(model, tmp_path, plan, continuation=export.ExportContinuation( CANDIDATE, (replace(receipt, **change),), forbidden, )) forbidden.assert_not_called() assert not list(tmp_path.iterdir()) @pytest.mark.parametrize("case", ["duplicate", "missing_restore", "invalid_record", "invalid_restore", "invalid_candidate", "mutable_receipts", "invalid_receipt", "invalid_type"]) def test_continuation_contract_rejects_invalid_inputs(tmp_path, model_plan, case): model, plan = model_plan receipt = export.ExportShardReceipt( CANDIDATE, export.native_export_plan_sha256(plan), plan.shards[0].filename, 123, "a" * 64, ) continuation = export.ExportContinuation(CANDIDATE, (receipt,), lambda *_: None) changes = { "duplicate": {"receipts": (receipt, receipt)}, "missing_restore": {"restore_shard": None}, "invalid_record": {"record_shard": 1}, "invalid_restore": {"restore_shard": 1}, "invalid_candidate": {"candidate_sha256": "model-name"}, "mutable_receipts": {"receipts": [receipt]}, "invalid_receipt": {"receipts": (object(),)}, } continuation = object() if case == "invalid_type" else replace(continuation, **changes[case]) with pytest.raises((TypeError, ValueError)): export.write_native_checkpoint(model, tmp_path, plan, continuation=continuation) assert not list(tmp_path.iterdir()) @pytest.mark.parametrize("case", ["truncated", "payload", "keys", "shape", "dtype", "symlink", "missing", "directory"]) def test_restored_content_must_match_receipt_and_tensor_layout(tmp_path, model_plan, case): model, plan = model_plan store = Store() write(model, plan, tmp_path / "first", continuation=store.continuation()) shard = plan.shards[0] receipt = store.receipts[shard.filename] if case == "truncated": store.files[shard.filename] = store.files[shard.filename][:-1] elif case == "payload": content = bytearray(store.files[shard.filename]) content[-1] ^= 1 store.files[shard.filename] = bytes(content) elif case in {"keys", "shape", "dtype"}: tensors = {name: model.get_parameter_or_buffer(name).detach().clone() for name in shard.tensor_names} name = shard.tensor_names[0] if case == "keys": tensors["different"] = tensors.pop(name) elif case == "shape": tensors[name] = tensors[name].flatten() else: tensors[name] = tensors[name].to(torch.float64) altered = tmp_path / "altered.safetensors" save_file(tensors, altered) content = altered.read_bytes() store.files[shard.filename] = content store.receipts[shard.filename] = replace( receipt, size_bytes=len(content), sha256=hashlib.sha256(content).hexdigest(), ) def restore(receipt, path): if case == "symlink": path.symlink_to(tmp_path / "first" / receipt.filename) elif case == "directory": path.mkdir() elif case != "missing": store.restore(receipt, path) with pytest.raises(ValueError): write(model, plan, tmp_path / "resumed", continuation=store.continuation(restore_shard=restore)) assert not (tmp_path / "resumed").exists() assert not list(tmp_path.glob(".resumed.staging-*")) def test_placement_change_preserves_identity_but_layout_change_does_not(model_plan): _, plan = model_plan moved = replace(plan, tensors={name: replace(spec, offload_owner="moved") for name, spec in plan.tensors.items()}, shards=tuple(replace(shard, host_demand_bytes=0) for shard in plan.shards), ) assert export.native_export_plan_sha256(moved) == export.native_export_plan_sha256(plan) changed = replace(plan, shards=tuple(reversed(plan.shards))) assert export.native_export_plan_sha256(changed) != export.native_export_plan_sha256(plan) @pytest.mark.parametrize("where", ["start", "hash", "complete", "restore", "record", "cancel"]) def test_callbacks_and_cancellation_propagate_with_atomic_rollback(tmp_path, model_plan, where): model, plan = model_plan store = Store() write(model, plan, tmp_path / "first", continuation=store.continuation()) destination = tmp_path / "destination" destination.mkdir() (destination / "keep.txt").write_text("existing") def fail(*_args): raise RuntimeError("caller failed") def progress(event): if event.event == where: fail() kwargs = {"on_shard_progress": progress} continuation = store.continuation() if where == "cancel": kwargs["check_cancelled"] = fail elif where in {"restore", "record"}: continuation = replace(continuation, **{f"{where}_shard": fail}) with pytest.raises(RuntimeError, match="caller failed"): write(model, plan, destination, continuation=continuation, **kwargs) assert snapshot(destination) == {"keep.txt": b"existing"} def test_no_continuation_keeps_default_writer_without_hash_pass(tmp_path, monkeypatch, model_plan): model, plan = model_plan forbidden = Mock(side_effect=AssertionError("default writer hashed output")) monkeypatch.setattr(export, "_hash_shard", forbidden) write(model, plan, tmp_path / "checkpoint") forbidden.assert_not_called() def test_progress_rejected_before_write(tmp_path, model_plan): model, plan = model_plan with pytest.raises(TypeError, match="on_shard_progress"): export.write_native_checkpoint(model, tmp_path, plan, on_shard_progress=object()) assert not list(tmp_path.iterdir()) @pytest.mark.parametrize("filename", ["../escape.safetensors", "nested/file.safetensors", ".hidden.safetensors", "nested\\file.safetensors", "weights.bin"]) def test_unsafe_plan_filenames_rejected_before_write(tmp_path, model_plan, filename): model, plan = model_plan invalid = replace(plan, shards=(replace(plan.shards[0], filename=filename),)) with pytest.raises(ValueError, match="safe shard filenames"): export.write_native_checkpoint(model, tmp_path, invalid, continuation=export.ExportContinuation(CANDIDATE)) assert not list(tmp_path.iterdir()) def test_existing_restore_target_is_not_overwritten(tmp_path, model_plan): model, plan = model_plan store = Store() write(model, plan, tmp_path / "first", continuation=store.continuation()) target = tmp_path / plan.shards[0].filename target.write_bytes(b"existing") with pytest.raises(ValueError, match="already exists"): export.write_native_checkpoint(model, tmp_path, plan, continuation=store.continuation()) assert target.read_bytes() == b"existing" assert store.restored == [] def test_existing_restore_temporary_is_not_followed_or_removed(tmp_path, model_plan): model, plan = model_plan store = Store() write(model, plan, tmp_path / "first", continuation=store.continuation()) outside = tmp_path / "keep.txt" outside.write_text("preserve") temporary = tmp_path / f".{plan.shards[0].filename}.restore.tmp" temporary.symlink_to(outside) with pytest.raises(ValueError, match="temporary path already exists"): export.write_native_checkpoint(model, tmp_path, plan, continuation=store.continuation()) assert temporary.is_symlink() assert outside.read_text() == "preserve" assert store.restored == [] def test_cancellation_during_hash_does_not_commit_partial_restore(tmp_path, model_plan): model, plan = model_plan store = Store() write(model, plan, tmp_path / "first", continuation=store.continuation()) hashing = False completed = [] def progress(event): nonlocal hashing hashing |= event.event == "hash" if event.event == "complete": completed.append(event) def cancel(): if hashing: raise RuntimeError("cancelled during hash") with pytest.raises(RuntimeError, match="cancelled during hash"): write(model, plan, tmp_path / "resumed", continuation=store.continuation(), on_shard_progress=progress, check_cancelled=cancel) assert not (tmp_path / "resumed").exists() assert completed == []