Files
OBLITERATUS/obliteratus/experiments/dossier_cli.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.1 KiB
Python

"""``obliteratus experiments dossier build|show|check`` (target dossiers beside a run)."""
import json
from pathlib import Path
from obliteratus.handle_cleanup import cleanup_on_exit
_INPUT_KEYS = {"name", "hypothesis", "discovery", "estimates", "contrast", "confirmation",
"subspace", "confound_resolutions", "coverage_notes", "rejected_alternatives",
"unresolved_questions", "claimed_label"}
def add_dossier(commands):
parser = commands.add_parser(
"dossier", help="Target dossiers: hypothesis, estimates and evidence by reference")
sub = parser.add_subparsers(dest="dossier_command", required=True)
build = sub.add_parser("build", help="Build <run>/dossiers/<name>.json from an input file")
build.add_argument("input", help="Dossier input JSON (name, hypothesis, discovery report, "
"estimates, ...)")
build.add_argument("--run", required=True,
help="Run directory; report paths in the input resolve against it")
build.add_argument("--overwrite", action="store_true", help="Replace an existing dossier")
show = sub.add_parser("show", help="Show a dossier with its recomputed readiness checklist")
show.add_argument("dossier", help="Dossier JSON file")
show.add_argument("--allow-fixture-results", action="store_true",
help="Let software-contract intervention results on fixture models raise "
"the computed label (never a model finding)")
check = sub.add_parser(
"check", help="Check whether a dossier is stale; exit 1 when it is")
check.add_argument("dossier", help="Dossier JSON file")
check.add_argument("--current", action="store_true",
help="Also compare the run's current model weights and pack dataset "
"revision (from <run>/spec.json)")
check.add_argument("--pack", help="Compare the dataset revision of this pack")
check.add_argument("--model", help="Compare the weights of this model id")
check.add_argument("--dtype", default="float32")
check.add_argument("--revision")
check.add_argument("--device", default="cpu")
def _read_input(path):
value = json.loads(Path(path).read_text(encoding="utf-8"))
if not isinstance(value, dict):
raise ValueError("Dossier input must be a JSON object")
unknown = set(value) - _INPUT_KEYS
if unknown:
raise ValueError(f"Unknown dossier input fields: {sorted(unknown)}")
for key in ("name", "hypothesis", "discovery"):
if key not in value:
raise ValueError(f"Dossier input requires {key!r}")
return value
def build(input_path, run, *, overwrite=False):
from obliteratus.analysis.target_dossier import build_dossier, write_dossier
spec = _read_input(input_path)
options = {k: spec[k] for k in spec if k not in {"name", "hypothesis", "discovery"}}
options.setdefault("estimates", [])
try:
dossier = build_dossier(spec["name"], run, hypothesis=spec["hypothesis"],
discovery=spec["discovery"], **options)
except KeyError as error:
raise ValueError(f"Dossier input entry is missing field {error}") from error
path = write_dossier(dossier, run, overwrite=overwrite)
return {"dossier": str(path), "evidence_label": dossier["evidence_label"],
"ready": dossier["readiness"]["ready"], "missing": dossier["readiness"]["missing"]}
def show(path, *, allow_fixture_results=False):
from obliteratus.analysis import dossier_readiness
from obliteratus.analysis.target_dossier import read_dossier
dossier, base = read_dossier(path)
status = dossier_readiness.check(dossier, base, allow_fixture_results=allow_fixture_results)
readiness = (dossier_readiness.assess(dossier, base)["readiness"] if not status["stale"]
else {"ready": False, "missing": status["reasons"]})
return {"name": dossier["name"], "hypothesis": dossier["hypothesis"],
"datasets": dossier["datasets"],
"estimates": [{"id": e["id"], "type": e["type"], "site": e["site"]}
for e in dossier["estimates"]],
"evidence_label": status["label"], "claimed_label":
dossier["evidence_label"]["claimed"], "stale": status["stale"],
"readiness": readiness, "counterexamples": len(dossier["counterexamples"]),
"rejected_alternatives": dossier["rejected_alternatives"],
"unresolved_questions": dossier["unresolved_questions"],
"interventions": status["interventions"]}
def check(path, *, current=False, pack=None, model=None, dtype="float32", revision=None,
device="cpu", model_factory=None, pack_resolver=None):
from obliteratus.analysis import dossier_readiness
from obliteratus.analysis.target_dossier import read_dossier
from obliteratus.experiments import inputs as _inputs
from obliteratus.experiments.spec import load_spec
dossier, base = read_dossier(path)
section = None if model is None else {"id": model, "dtype": dtype, "revision": revision,
"device": device}
if current:
spec = load_spec(base / "spec.json")
pack, section = pack or spec["dataset"]["pack"], section or dict(spec["model"])
resolved = None if pack is None else (pack_resolver or _inputs.default_pack_resolver)(pack)
handle = None if section is None else (
model_factory or _inputs.default_model_factory)(section)
with cleanup_on_exit(handle):
return dossier_readiness.check(dossier, base, pack=resolved,
model=None if handle is None else handle.model)
def run_dossier_command(args):
if args.dossier_command == "build":
return build(args.input, args.run, overwrite=args.overwrite)
if args.dossier_command == "show":
return show(args.dossier, allow_fixture_results=args.allow_fixture_results)
return check(args.dossier, current=args.current, pack=args.pack, model=args.model,
dtype=args.dtype, revision=args.revision, device=args.device)