mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 10:34:23 +01:00
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
317 lines
16 KiB
Python
317 lines
16 KiB
Python
"""Resumable execution, final verification and publication of an edit sequence.
|
|
|
|
Layout under ``<store>/sequences/<id>/``:
|
|
|
|
* ``plan.json``: the frozen plan (``sequence``);
|
|
* ``steps/NN/artifact/``: the step's stack (``sequence_steps``); ``steps/NN/evaluation/``
|
|
its generations; ``steps/NN/step.json`` the step record, written last. A step
|
|
directory without ``step.json`` is an interrupted step: it was never verified
|
|
and is discarded when the sequence resumes;
|
|
* ``baseline.json``: the reference completions and state of the first session;
|
|
every later session must reproduce them;
|
|
* ``verification/verification.json``: the final verification;
|
|
* ``published.json``: written once, only after every step and the final
|
|
verification passed. Only a published sequence is ``complete`` and selected.
|
|
|
|
A step that fails its measurement marks the sequence ``failed`` for good: it is
|
|
never resumed, published or selected. A crash is not a failure: ``resume``
|
|
continues from the last completed, verified step.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from contextlib import contextmanager
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import shutil
|
|
|
|
from obliteratus.analysis import dossier_refs as refs
|
|
from obliteratus.concepts.dataset import identity
|
|
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_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
|
|
|
|
STEP = "step.json"
|
|
STEP_SCHEMA = "obliteratus.edit-sequence-step"
|
|
VERIFICATION_SCHEMA = "obliteratus.edit-sequence-verification"
|
|
PUBLISHED = "published.json"
|
|
RECOVERY = ("a failed step marks the sequence failed for good; recover by restoring a known "
|
|
"artifact with rollback (the selected reference or an earlier verified step), "
|
|
"digest-checked and re-measured; an inverse edit is never a recovery path")
|
|
|
|
|
|
@contextmanager
|
|
def _lock(path):
|
|
try:
|
|
import fcntl
|
|
except ImportError: # pragma: no cover - non-POSIX
|
|
yield
|
|
return
|
|
fd = os.open(Path(path) / ".lock", os.O_CREAT | os.O_RDWR)
|
|
try:
|
|
try:
|
|
fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
|
except OSError as error:
|
|
raise SequenceRefused(f"Another process is running sequence {Path(path).name}") \
|
|
from error
|
|
yield
|
|
finally:
|
|
os.close(fd)
|
|
|
|
|
|
def _read(path):
|
|
try:
|
|
return json.loads(Path(path).read_text(encoding="utf-8"))
|
|
except (OSError, ValueError):
|
|
return None
|
|
|
|
|
|
def step_records(seq_dir):
|
|
"""``{position: record}`` of steps whose record was written."""
|
|
found = {}
|
|
for file in sorted((Path(seq_dir) / "steps").glob(f"*/{STEP}")):
|
|
record = _read(file)
|
|
if record is None or record.get("id") != identity(
|
|
{k: v for k, v in record.items() if k != "id"}):
|
|
raise SequenceRefused(f"Step record {file} was edited after it was written")
|
|
found[record["position"]] = record
|
|
return found
|
|
|
|
|
|
def sequence_state(seq_dir, plan):
|
|
"""``planned``, ``incomplete``, ``failed``, ``complete`` (selected) or ``invalid``."""
|
|
try:
|
|
records = step_records(seq_dir)
|
|
except SequenceRefused as error:
|
|
return {"state": "invalid", "selected": False, "reason": str(error)}
|
|
verification = _read(Path(seq_dir) / "verification" / "verification.json")
|
|
base = {"steps_completed": sorted(p for p, r in records.items() if r["outcome"] == "passed")}
|
|
if any(r["outcome"] != "passed" for r in records.values()) or (
|
|
verification is not None and verification["outcome"] != "passed"):
|
|
return {"state": "failed", "selected": False, **base}
|
|
published = _read(Path(seq_dir) / PUBLISHED)
|
|
if published is not None:
|
|
problems = _published_problems(seq_dir, plan, published)
|
|
if problems:
|
|
return {"state": "invalid", "selected": False, "reason": "; ".join(problems), **base}
|
|
return {"state": "complete", "selected": True, **base,
|
|
"final_output": published["final_output"]}
|
|
return {"state": "incomplete" if records else "planned", "selected": False, **base}
|
|
|
|
|
|
def _published_problems(seq_dir, plan, published):
|
|
problems = []
|
|
for entry in published["steps"]:
|
|
file = Path(seq_dir) / entry["path"]
|
|
if not file.is_file() or refs.file_sha256(file) != entry["sha256"]:
|
|
problems.append(f"step record {entry['path']} changed")
|
|
for position, record in step_records(seq_dir).items():
|
|
if cio.tree_digests(Path(seq_dir) / record["output"]["artifact_path"]) != \
|
|
record["output"]["artifact_files"]:
|
|
problems.append(f"step {position} artifact changed")
|
|
if published["sequence"] != plan["id"]:
|
|
problems.append("published record names another sequence")
|
|
return problems
|
|
|
|
|
|
def publish(store, sequence):
|
|
"""Write ``published.json``; refused unless every step and the final verification passed."""
|
|
plan, seq_dir = seq.read_plan(store, sequence)
|
|
records = step_records(seq_dir)
|
|
verification_file = seq_dir / "verification" / "verification.json"
|
|
verification = _read(verification_file)
|
|
if len(records) != plan["frozen"]["length"] or any(
|
|
r["outcome"] != "passed" for r in records.values()):
|
|
raise SequenceRefused(f"Sequence {plan['id']} is not complete: steps "
|
|
f"{sorted(records)} of {plan['frozen']['length']} recorded; a "
|
|
"partial sequence is never published")
|
|
if verification is None or verification["outcome"] != "passed":
|
|
raise SequenceRefused(f"Sequence {plan['id']} has no passing final verification")
|
|
if (seq_dir / PUBLISHED).exists():
|
|
raise SequenceRefused(f"Sequence {plan['id']} is already published")
|
|
entry = {"sequence": plan["id"], "final_output": records[len(records)]["output"]["identity"],
|
|
"steps": [{"position": p, "path": f"steps/{steps.step_name(p)}/{STEP}",
|
|
"sha256": refs.file_sha256(seq_dir / "steps" / steps.step_name(p) / STEP)}
|
|
for p in sorted(records)],
|
|
"verification": {"path": "verification/verification.json",
|
|
"sha256": refs.file_sha256(verification_file)}, "at": now()}
|
|
atomic_json(seq_dir / PUBLISHED, entry)
|
|
return entry
|
|
|
|
|
|
def _check_resumable(seq_dir, plan, records):
|
|
for name in sorted((seq_dir / "steps").glob("*")) if (seq_dir / "steps").is_dir() else []:
|
|
if not (name / STEP).is_file():
|
|
shutil.rmtree(name) # an interrupted, never-verified step
|
|
if cio.tree_digests(plan["frozen"]["reference"]["source"]) != \
|
|
plan["frozen"]["reference"]["files"]:
|
|
raise SequenceRefused("The selected reference changed since the plan was written")
|
|
parent = steps.reference_identity(plan)
|
|
for position in sorted(records):
|
|
record = records[position]
|
|
if record["input"]["identity"] != parent or position != len(
|
|
[p for p in records if p <= position]):
|
|
raise SequenceRefused(f"Step {position} does not continue the recorded chain")
|
|
if cio.tree_digests(seq_dir / record["output"]["artifact_path"]) != \
|
|
record["output"]["artifact_files"]:
|
|
raise SequenceRefused(f"Step {position} artifact changed after it was verified")
|
|
parent = record["output"]["identity"]
|
|
|
|
|
|
def _baseline(seq_dir, session):
|
|
file, found = seq_dir / "baseline.json", {"completions": session["baseline"],
|
|
"loaded_state_sha256": session["baseline_state"]}
|
|
recorded = _read(file)
|
|
if recorded is None:
|
|
atomic_json(file, found)
|
|
elif recorded != found:
|
|
raise SequenceRefused("The reference no longer reproduces the recorded baseline")
|
|
|
|
|
|
def _run_step(seq_dir, session, position, parent, model_io, actor):
|
|
plan = session["plan"]
|
|
component = plan["frozen"]["components"][position - 1]
|
|
folder = seq_dir / "steps" / steps.step_name(position)
|
|
started = now()
|
|
stack = steps.write_step(seq_dir, plan, position, parent["stack"], component,
|
|
session["targets"][position - 1], model_io)
|
|
artifact = f"steps/{steps.step_name(position)}/{steps.ARTIFACT}"
|
|
files = cio.tree_digests(seq_dir / artifact)
|
|
found, logits, state = ev.measure_stack(session, seq_dir, stack, model_io,
|
|
folder / "evaluation" / "step")
|
|
temp, temp_logits, restored = ev.temporary(session, position, folder / "evaluation" /
|
|
"temporary")
|
|
checks, comparison, cards = ev.judge(session, position, found, temp, logits, temp_logits,
|
|
restored, parent["completions"])
|
|
passed = all(c["passed"] for c in checks)
|
|
record = {"schema": STEP_SCHEMA, "schema_version": seq.SCHEMA_VERSION,
|
|
"sequence": plan["id"], "position": position,
|
|
"order": [c["signature"]["dossier"] + "/" + c["signature"]["estimate"] + ":"
|
|
+ c["signature"]["operation"]["kind"]
|
|
for c in plan["frozen"]["components"]],
|
|
"component": {k: component[k] for k in ("position", "signature", "source",
|
|
"registration", "target_task")},
|
|
"input": {"identity": parent["identity"], "stack": parent["stack"]},
|
|
"output": {"identity": steps.output_identity(parent["identity"], position, component,
|
|
cio.tree_sha256(files), state),
|
|
"stack": stack, "artifact_path": artifact, "artifact_files": files,
|
|
"artifact_sha256": cio.tree_sha256(files), "loaded_state_sha256": state,
|
|
"stack_files": steps.stack_files(seq_dir, plan, stack)},
|
|
"checks": checks, "outcome": "passed" if passed else "failed",
|
|
"comparison": comparison, "scorecards": cards, "completions": found,
|
|
"dataset": session["data"], "independence": session["independence"],
|
|
"recovery": {"rule": RECOVERY, "restore_points": ["reference"] + [
|
|
p for p in range(1, position)]},
|
|
"started_at": started, "completed_at": now(), "actor": actor_name(actor),
|
|
"tool": tool()}
|
|
record["id"] = identity(record)
|
|
atomic_json(folder / STEP, record)
|
|
return record
|
|
|
|
|
|
def _verify(seq_dir, session, record, model_io):
|
|
out = seq_dir / "verification"
|
|
if out.exists():
|
|
shutil.rmtree(out) # an interrupted verification; never published
|
|
out.mkdir()
|
|
length = session["plan"]["frozen"]["length"]
|
|
stack = record["output"]["stack"]
|
|
found, logits, state = ev.measure_stack(session, seq_dir, stack, model_io, out / "final")
|
|
temp, temp_logits, restored = ev.temporary(session, length, out / "temporary")
|
|
checks, comparison, cards = ev.judge(session, length, found, temp, logits, temp_logits,
|
|
restored, session["baseline"])
|
|
files = steps.stack_files(seq_dir, session["plan"], stack)
|
|
checks += [
|
|
ev._check("artifact_digests", files == record["output"]["stack_files"],
|
|
"every file the final stack loads matches the step record"),
|
|
ev._check("reproduces_recorded_step", found == record["completions"]
|
|
and state == record["output"]["loaded_state_sha256"],
|
|
"independent reload reproduces the last step's completions and loaded state"),
|
|
ev._check("reference_preserved", cio.tree_digests(session["plan"]["frozen"]["reference"][
|
|
"source"]) == session["plan"]["frozen"]["reference"]["files"],
|
|
"reference files identical to the plan")]
|
|
passed = all(c["passed"] for c in checks)
|
|
verification = {"schema": VERIFICATION_SCHEMA, "schema_version": seq.SCHEMA_VERSION,
|
|
"sequence": session["plan"]["id"], "final_output": record["output"]["identity"],
|
|
"checks": checks, "outcome": "passed" if passed else "failed",
|
|
"comparison": comparison, "scorecards": cards, "at": now()}
|
|
atomic_json(out / "verification.json", verification)
|
|
return verification
|
|
|
|
|
|
def run_sequence(store, sequence, *, model_io=None, pack_resolver=None, resume=False,
|
|
actor=None):
|
|
"""Execute (``resume=False``) or continue (``resume=True``) a planned sequence."""
|
|
plan, seq_dir = seq.read_plan(store, sequence)
|
|
state = sequence_state(seq_dir, plan)
|
|
if state["state"] in ("failed", "invalid"):
|
|
raise SequenceRefused(f"Sequence {plan['id']} is {state['state']}; a failed sequence "
|
|
"is never resumed or selected")
|
|
if state["state"] == "complete":
|
|
return show(store, sequence)
|
|
started = (seq_dir / "steps").is_dir() or (seq_dir / "baseline.json").exists()
|
|
if started and not resume:
|
|
raise SequenceRefused(f"Sequence {plan['id']} was already started; use resume")
|
|
if resume and not started:
|
|
raise SequenceRefused(f"Sequence {plan['id']} has nothing to resume; use run")
|
|
model_io = model_io or cio.TransformersIO()
|
|
with _lock(seq_dir):
|
|
records = step_records(seq_dir)
|
|
_check_resumable(seq_dir, plan, records)
|
|
sessions = seq_dir / "sessions"
|
|
sessions.mkdir(exist_ok=True)
|
|
out = sessions / f"{len(list(sessions.iterdir())) + 1:04d}"
|
|
session = ev.open_session(plan, model_io, pack_resolver, out)
|
|
with cleanup_on_exit(session["handle"]):
|
|
_baseline(seq_dir, session)
|
|
parent = {"identity": steps.reference_identity(plan), "stack": {
|
|
"base": steps.REFERENCE, "adapters": []}, "completions": session["baseline"]}
|
|
for position in range(1, plan["frozen"]["length"] + 1):
|
|
record = records.get(position)
|
|
if record is None:
|
|
record = _run_step(seq_dir, session, position, parent, model_io, actor)
|
|
if record["outcome"] != "passed":
|
|
return show(store, sequence)
|
|
parent = {"identity": record["output"]["identity"],
|
|
"stack": record["output"]["stack"], "completions": record["completions"]}
|
|
if _verify(seq_dir, session, record, model_io)["outcome"] == "passed":
|
|
publish(store, sequence)
|
|
return show(store, sequence)
|
|
|
|
|
|
def show(store, sequence=None):
|
|
"""One sequence (plan, state, steps, verification) or a summary of every sequence."""
|
|
if sequence is not None:
|
|
plan, seq_dir = seq.read_plan(store, sequence)
|
|
state = sequence_state(seq_dir, plan)
|
|
try:
|
|
records = step_records(seq_dir)
|
|
except SequenceRefused:
|
|
records = {}
|
|
return {"plan": plan, "state": state,
|
|
"steps": [{k: r[k] for k in ("position", "component", "input", "output", "checks",
|
|
"outcome", "comparison", "recovery")}
|
|
for _, r in sorted(records.items())],
|
|
"verification": _read(seq_dir / "verification" / "verification.json"),
|
|
"published": _read(seq_dir / PUBLISHED)}
|
|
root, rows = Path(store) / seq.SEQUENCE_DIR, []
|
|
for item in sorted(root.glob(f"*/{seq.PLAN}")) if root.is_dir() else []:
|
|
try:
|
|
plan, seq_dir = seq.read_plan(store, item.parent.name)
|
|
except SequenceRefused as error:
|
|
rows.append({"id": item.parent.name, "state": "invalid", "reason": str(error)})
|
|
continue
|
|
rows.append({"id": plan["id"], "order": plan["frozen"]["interaction"]["order"],
|
|
"scope": plan["frozen"]["scope"], **sequence_state(seq_dir, plan)})
|
|
return {"sequences": rows}
|
|
|
|
|
|
__all__ = ["PUBLISHED", "RECOVERY", "publish", "run_sequence", "sequence_state", "show",
|
|
"step_records"]
|