Files
OBLITERATUS/obliteratus/experiments/regression.py
Joseph MaglyandClaude Opus 5.5 189311e5d0 feat(experiments): regression library and dependency-triggered revalidation
Add F22: versioned regression suites built from confirmed cases,
failures and interaction surprises, with computed staleness when a
dependency changes and append-only re-run history.

- Cases point at the original evidence by path, digest and pointer
  (never copying scores) and record what reproduces them: model and
  weight fingerprint, tokenizer, dataset revision and rows digest,
  scorer versions, backend descriptor and recipe, plus the expected
  outcome with tolerance. Suite versions are write-once and chained.
- A dependency graph covers weights, tokenizer, dataset, scorer,
  backend runtime, directions, report files and artifacts. Staleness is
  computed on read; models match by weight fingerprint, so a same-name
  model with new weights inherits no qualification.
- Re-runs go through each feature's own run function and append to a
  hash-chained results log; regressions link to their case, and history
  stays byte-identical.
- Status and the conditions table report per intervention and condition
  (task, domain, backend, model), with no single aggregate.
- `obliteratus experiments regression build|observe|status|revalidate|run`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 13:32:01 -05:00

215 lines
11 KiB
Python

"""Regression library: versioned suites of historical cases, revalidation and re-runs.
``build`` turns a run's evidence into suites (``confirmed``, ``failures``,
``surprises``) of cases that reference the evidence by digest. ``observe``
records the current dependency values; ``status`` (regression_status) computes
stale, regressed and untested evidence from them; ``revalidate`` writes the
plan of cases to re-run; ``run`` measures a new result for a case and appends
it to the results log. Historical evidence, suite versions and earlier log
lines are never rewritten.
"""
from __future__ import annotations
from pathlib import Path
from obliteratus.analysis import dossier_refs as refs
from obliteratus.concepts.dataset import identity
from obliteratus.experiments import regression_cases as rc
from obliteratus.experiments import regression_deps as deps
from obliteratus.experiments.regression_store import (
RESULTS, SNAPSHOTS, Library, LibraryError,
)
from obliteratus.experiments.registry_store import actor_name, now, tool
SUITES = {"confirmed": "confirmed", "failure": "failures", "surprise": "surprises"}
def _registry_evidence(run_dir, tolerance, skipped):
from obliteratus.experiments.registry_store import Registry
registry = Registry(run_dir)
if not registry.exists():
return []
documents = {d["id"]: d for d in registry.registrations()}
found = []
for event in registry.events():
if event["event"] != "unlock":
continue
reference = event["detail"].get("result") or {}
try:
observations = rc.measure(run_dir, documents[event["registration"]]["kind"],
reference["path"], source=documents[event["registration"]],
tolerance=tolerance)
if observations[0]["evidence"]["sha256"] != reference["sha256"]:
raise rc.CaseError("unlocked result changed after it was unlocked")
found += observations
except (OSError, ValueError, KeyError, StopIteration) as error:
skipped.append({"evidence": reference.get("path"), "reason": str(error)})
return found
def _glob_evidence(run_dir, kind, pattern, tolerance, skipped):
found = []
for file in sorted(Path(run_dir).glob(pattern)):
stored = refs.stored_path(file, run_dir)
try:
found += rc.measure(run_dir, kind, stored, tolerance=tolerance)
except (OSError, ValueError, KeyError, StopIteration) as error:
skipped.append({"evidence": stored, "reason": str(error)})
return found
def collect(run_dir, *, tolerance=rc.DEFAULT_TOLERANCE, exclude=()):
"""``(cases, skipped)`` of every measurable piece of evidence in ``run_dir``."""
skipped = []
observations = _registry_evidence(run_dir, tolerance, skipped)
observations += _glob_evidence(run_dir, "verification",
"persistent-candidates/*/verifications/*/verification.json",
tolerance, skipped)
observations += _glob_evidence(run_dir, "qualification",
"backend-qualification/*/*/*/qualification.json",
tolerance, skipped)
observations += _glob_evidence(run_dir, "interaction",
"interactions/*/attempts/*/result.json", tolerance, skipped)
excluded = set(exclude)
cases = [rc.make_case(o) for o in observations if o["evidence"]["sha256"] not in excluded]
return sorted(cases, key=lambda c: c["id"]), skipped
def build(run_dir, *, library=None, tolerance=rc.DEFAULT_TOLERANCE, actor=None):
"""Write a new suite version when the cases changed; otherwise return the latest."""
lib = Library(run_dir, library)
with lib.lock():
reruns = {r["evidence"]["sha256"] for r in lib.read_log(RESULTS)}
cases, skipped = collect(lib.run_dir, tolerance=tolerance, exclude=reruns)
suites = {}
for name in SUITES.values():
members = [c for c in cases if SUITES[c["category"]] == name]
suites[name] = {"id": identity([c["id"] for c in members]), "cases": members}
latest = lib.latest_suites()
suite_id = identity({"suites": suites})
if latest is not None and latest["id"] == suite_id:
return {**latest, "unchanged": True}
document = {"schema": "obliteratus.regression-suites", "schema_version": 1,
"id": suite_id, "version": (latest["version"] + 1) if latest else 1,
"previous": latest["id"] if latest else None, "created_at": now(),
"actor": actor_name(actor), "tool": tool(), "suites": suites,
"skipped": skipped,
"rule": "cases reference evidence by path, SHA-256 and JSON pointer; "
"scores are never copied into or overwritten by the library"}
lib.write_suites(document)
return {**document, "unchanged": False}
def cases_of(document):
"""``{case id: (suite name, index, case)}`` of a suite version."""
return {c["id"]: (name, i, c) for name, suite in document["suites"].items()
for i, c in enumerate(suite["cases"])}
def observe(run_dir, snapshot, *, library=None, actor=None):
"""Append one observed dependency snapshot (current model/tokenizer/... values)."""
lib = Library(run_dir, library)
normalized = deps.normalize_snapshot(snapshot)
with lib.lock():
return lib.append(SNAPSHOTS, {"schema": "obliteratus.regression-snapshot",
"schema_version": 1, "observed_at": now(),
"actor": actor_name(actor), "snapshot": normalized,
"snapshot_sha256": identity(normalized)})
def model_snapshot(model, tokenizer, model_id):
"""Snapshot entry for a loaded model: sampled weight fingerprint and vocabulary digest."""
from obliteratus.analysis.capture_identity import capture_tokenizer_identity, weight_fingerprint
return {"models": {model_id: {
"weights_sha256": weight_fingerprint(model)["sha256"],
"tokenizer_vocab_sha256": capture_tokenizer_identity(tokenizer)["vocab_sha256"]}}}
def _matches_recipe(case, observation):
recipe, new = case["reproduce"]["recipe"], observation["recipe"]
fields = {"confirmation": ("spec_digest", "registration"),
"replication": ("spec_digest", "registration"),
"verification": ("candidate", "dataset"),
"qualification": ("candidate", "backend_id", "dataset"),
"interaction": ("spec_digest",)}[case["kind"]]
return [f for f in fields if recipe.get(f) != new.get(f)]
def run_case(run_dir, case_id, *, result=None, library=None, executor=None, actor=None,
**harness):
"""Measure a new result for one case and append it; history is never rewritten.
``result`` names an already produced result file; otherwise ``executor(run_dir, case,
**harness)`` (default: ``regression_exec.execute``) re-runs the recorded recipe.
"""
lib = Library(run_dir, library).require()
suites = lib.latest_suites()
if suites is None or case_id not in cases_of(suites):
raise LibraryError(f"No case {case_id} in the latest suite version")
name, index, case = cases_of(suites)[case_id]
if result is None:
from obliteratus.experiments.regression_exec import execute
result = (executor or execute)(lib.run_dir, case, **harness)
stored = refs.stored_path(refs.resolve_path(str(result), lib.run_dir), lib.run_dir)
source = None
if case["kind"] in ("confirmation", "replication"):
from obliteratus.experiments.registry_store import Registry
source = Registry(lib.run_dir).get(case["source"])
found = [o for o in rc.measure(lib.run_dir, case["kind"], stored, source=source,
tolerance=case["expected"]["tolerance"], every=True)
if o["slot"] == case["slot"]]
if not found:
raise rc.CaseError(f"{stored} has no {case['slot']} measurement")
observation = found[0]
mismatched = _matches_recipe(case, observation)
if mismatched:
raise rc.CaseError(f"{stored} is not a re-run of case {case_id}: {mismatched} differ")
with lib.lock():
history = lib.read_log(RESULTS)
digest = observation["evidence"]["sha256"]
if digest == case["evidence"]["sha256"]:
raise rc.CaseError("That is the case's historical evidence, not a new result")
if any(r["case"] == case_id and r["evidence"]["sha256"] == digest for r in history):
raise rc.CaseError("That result is already recorded for this case")
within, reasons = rc.compare(case["expected"], observation["observed"])
record = {"schema": "obliteratus.regression-result", "schema_version": 1,
"case": case_id, "kind": case["kind"], "intervention": case["intervention"],
"case_link": {"suite_version": suites["version"], "suite_file":
f"suites/{suites['version']:04d}.json",
"pointer": refs.pointer("suites", name, "cases", index),
"suite": name, "case_evidence": case["evidence"],
"reproduce": case["reproduce"]},
"evidence": observation["evidence"], "observed": observation["observed"],
"expected": case["expected"], "condition": observation["condition"],
"dependencies": observation["edges"], "within_tolerance": within,
"reasons": reasons, "status": "pass" if within else "regression"}
record["id"] = identity(record)
return lib.append(RESULTS, {**record, "recorded_at": now(), "actor": actor_name(actor)})
def revalidate(run_dir, *, library=None, snapshot=None):
"""Write (once) the plan of suites and cases to re-run after dependency changes."""
from obliteratus.experiments.regression_status import status
lib = Library(run_dir, library).require()
report = status(run_dir, library=library, snapshot=snapshot)
affected = [c for c in report["cases"] if c["status"] in ("stale", "regressed", "invalid")]
suites = sorted({(c["suite"], report["suites"][c["suite"]]) for c in affected})
plan = {"schema": "obliteratus.regression-plan", "schema_version": 1,
"suite_version": report["suite_version"], "snapshot_sha256": report["snapshot_sha256"],
"suites": [{"name": n, "id": i} for n, i in suites],
"cases": [{"case": c["case"], "status": c["status"], "reasons": c["reasons"],
"kind": c["kind"], "intervention": c["intervention"],
"command": f"obliteratus experiments regression run --run "
f"{lib.run_dir} --case {c['case']}"} for c in affected],
"rule": "re-running appends a new result; the historical case and its evidence "
"are kept unchanged"}
plan["id"] = identity(plan)
from obliteratus.experiments.regression_store import write_once
write_once(lib.path / "plans" / f"{plan['id'].removeprefix('sha256:')}.json", plan)
return plan
__all__ = ["build", "cases_of", "collect", "model_snapshot", "observe", "revalidate",
"run_case"]