Files
OBLITERATUS/obliteratus/experiments/sequence_rollback.py
Joseph Magly 3c7a6ca920 fix(models): harden ModelHandle cleanup sites and messages
Moves cleanup_on_exit/cleanup_during_error to the light
obliteratus.handle_cleanup module (re-exported from the loader) and imports
them at module top, so no import runs after a handle exists. Stores the closed
policy before detaching the restore point, names the path to remove in
handle-surfaced SnapshotErrors, guards post-load work in candidate,
smaller_change and the sequence loaders, keeps observer-close and neuron-lab
cleanup failures from replacing the primary error or skipping state reset, and
adds failure-plus-failing-close tests for every listed site.

Fixes #669
2026-11-25 10:06:29 -05:00

115 lines
6.0 KiB
Python

"""Rollback of an edit sequence by restoring a known artifact.
``rollback`` copies a known artifact into a new directory and tests the copy:
* ``reference``: the selected reference's files, which must be byte-identical
(every file digest) to the plan's record;
* a step position: a step that passed, restored as its stack (base checkpoint
and ordered adapters), every file digest-checked against the step record.
The restored copy is then loaded from disk and measured on the plan's rows: its
loaded parameter/buffer state must equal the recorded state and its completions
must equal the recorded completions. Applying an inverse edit is never a
rollback: under finite precision it need not restore anything, so it is not
offered. Each attempt is stored as ``rollbacks/NNNN.json``; the sequence's
state is not changed by a rollback.
"""
from __future__ import annotations
import json
from pathlib import Path
import shutil
from obliteratus.experiments import candidate_io as cio
from obliteratus.experiments import sequence as seq
from obliteratus.experiments import sequence_eval as ev
from obliteratus.experiments import sequence_run as srun
from obliteratus.experiments import sequence_steps as steps
from obliteratus.experiments.registry_store import actor_name, now, tool
from obliteratus.experiments.sequence import SequenceRefused
from obliteratus.experiments.store import atomic_json
from obliteratus.handle_cleanup import cleanup_on_exit
ROLLBACK_SCHEMA = "obliteratus.edit-sequence-rollback"
RULE = ("restore known bytes (digest-checked), reload and compare the loaded state and outputs "
"with the recorded ones; mathematical invertibility is not a substitute")
def _restore(seq_dir, plan, target, dest):
"""Copy the known artifact into ``dest``; returns ``(stack, expected files, recorded)``."""
baseline = json.loads((seq_dir / "baseline.json").read_text(encoding="utf-8")) \
if (seq_dir / "baseline.json").is_file() else None
if target == "reference":
shutil.copytree(plan["frozen"]["reference"]["source"], dest)
stack = {"base": {"kind": "step", "path": "."}, "adapters": []}
expected = {"base": plan["frozen"]["reference"]["files"]}
return stack, expected, baseline
position = int(target)
record = srun.step_records(seq_dir).get(position)
if record is None or record["outcome"] != "passed":
raise SequenceRefused(f"Step {position} has no verified artifact to restore")
original = record["output"]["stack"]
dest.mkdir()
base = (plan["frozen"]["reference"]["source"] if original["base"] == steps.REFERENCE
else seq_dir / original["base"]["path"])
shutil.copytree(base, dest / "base")
adapters = []
for index, adapter in enumerate(original["adapters"], 1):
shutil.copytree(seq_dir / adapter, dest / "adapters" / f"{index:02d}")
adapters.append(f"adapters/{index:02d}")
stack = {"base": {"kind": "step", "path": "base"}, "adapters": adapters}
expected = {"base": record["output"]["stack_files"]["base"],
**{new: record["output"]["stack_files"][old]
for new, old in zip(adapters, original["adapters"])}}
(dest / steps.STACK).write_text(json.dumps(stack, indent=2, sort_keys=True) + "\n",
encoding="utf-8")
return stack, expected, {"completions": record["completions"],
"loaded_state_sha256": record["output"]["loaded_state_sha256"]}
def rollback(store, sequence, target, dest, *, model_io=None, pack_resolver=None, actor=None):
"""Restore ``reference`` or a verified step into ``dest`` and test it; returns the record."""
plan, seq_dir = seq.read_plan(store, sequence)
dest = Path(dest).resolve()
if dest.exists():
raise SequenceRefused(f"{dest} exists; a rollback writes a new directory")
if cio.overlaps(dest, seq_dir) or cio.overlaps(dest, plan["frozen"]["reference"]["source"]):
raise SequenceRefused("Restore outside the sequence store and the reference")
stack, expected, recorded = _restore(seq_dir, plan, str(target), dest)
if recorded is None:
shutil.rmtree(dest, ignore_errors=True)
raise SequenceRefused("The sequence has no recorded baseline to compare with; run it")
if str(target) == "reference":
restored_files = {"base": cio.tree_digests(dest)}
else:
restored_files = steps.stack_files(dest, plan, stack)
model_io = model_io or cio.TransformersIO()
folder = seq_dir / "rollbacks"
folder.mkdir(exist_ok=True)
attempt = len(list(folder.glob("*.json"))) + 1
session = ev.open_session(plan, model_io, pack_resolver,
folder / f"{attempt:04d}" / "session")
with cleanup_on_exit(session["handle"]):
found, _, state = ev.measure_stack(session, dest, stack, model_io,
folder / f"{attempt:04d}" / "restored")
checks = [
ev._check("bytes_restored", restored_files == expected,
"every restored file has its recorded SHA-256"),
ev._check("state_restored", state == recorded["loaded_state_sha256"],
"loaded parameter and buffer state equals the recorded state"),
ev._check("outputs_restored", found == recorded["completions"],
"completions on the plan's rows equal the recorded completions"),
ev._check("reference_preserved", cio.tree_digests(plan["frozen"]["reference"]["source"])
== plan["frozen"]["reference"]["files"], "reference files unchanged")]
record = {"schema": ROLLBACK_SCHEMA, "schema_version": seq.SCHEMA_VERSION,
"sequence": plan["id"], "attempt": attempt, "target": str(target),
"destination": str(dest), "files": restored_files, "checks": checks,
"outcome": "passed" if all(c["passed"] for c in checks) else "failed",
"rule": RULE, "at": now(), "actor": actor_name(actor), "tool": tool()}
atomic_json(folder / f"{attempt:04d}.json", record)
return record
__all__ = ["ROLLBACK_SCHEMA", "rollback"]