Files
OBLITERATUS/tests/test_regression_library.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

293 lines
15 KiB
Python

"""Regression library over confirmed, verified and qualified evidence (CPU contracts).
Reuses the planted fixture candidates of ``tests/test_persistent_candidates.py``:
the steer key is confirmed, replicated, saved as an activation adapter, verified
after reload and qualified on CPU float32 (qualified) and CPU bfloat16 (failed).
The library references that evidence by digest; dependency changes are
supplied as observed snapshots or made to files of a copied run.
"""
from __future__ import annotations
import hashlib
import json
import shutil
from pathlib import Path
import pytest
import torch
from obliteratus.analysis import dossier_refs as refs
from obliteratus.concepts.dataset import identity
from obliteratus.concepts.pack import ConceptPack
from obliteratus.experiments import regression as reg
from obliteratus.experiments import regression_cases as rc
from obliteratus.experiments.regression_status import status
from obliteratus.experiments.regression_store import LibraryError
from tests.test_backend_qualification import BackendIO, run as qualify_run, selected # noqa: F401
from tests.test_persistent_candidates import FixtureIO, build, env, verify # noqa: F401
pytestmark = pytest.mark.cpu
CPU32 = {"device": "cpu", "dtype": "float32", "cache": "cached"}
BF16 = {"device": "cpu", "dtype": "bfloat16", "cache": "cached"}
@pytest.fixture(scope="module")
def evidence(env, selected): # noqa: F811
qualify_run(env, selected, "adapter", CPU32)
qualify_run(env, selected, "adapter", BF16)
return selected["adapter"]
@pytest.fixture
def run(evidence, tmp_path):
copy = Path(shutil.copytree(evidence[0], tmp_path / "run"))
reg.build(copy)
return copy
def files(root):
return {p.relative_to(root).as_posix(): hashlib.sha256(p.read_bytes()).hexdigest()
for p in sorted(Path(root).rglob("*")) if p.is_file() and p.name != ".lock"}
def by_kind(report):
found = {}
for case in report["cases"]:
key = case["kind"] if case["kind"] != "qualification" else \
"qualification:" + case["condition"]["backend"].split("/")[1]
found[key] = case
return found
def states(report):
return {k: c["status"] for k, c in by_kind(report).items()}
def test_cases_reference_historical_evidence_by_digest_with_reproduction_inputs(run):
suites = reg.build(run)
assert suites["unchanged"] is True and suites["version"] == 1 and suites["skipped"] == []
names = {n: sorted(c["kind"] for c in s["cases"]) for n, s in suites["suites"].items()}
assert names == {"confirmed": ["confirmation", "qualification", "replication",
"verification"],
"failures": ["qualification"], "surprises": []}
for _, _, case in reg.cases_of(suites).values():
assert case["id"] == identity({k: v for k, v in case.items() if k != "id"})
path = refs.resolve_path(case["evidence"]["path"], run)
assert refs.file_sha256(path) == case["evidence"]["sha256"]
# The expected value is read at the pointer of the evidence, never re-typed.
stored = refs.resolve_pointer(json.loads(path.read_text()), case["evidence"]["pointer"])
assert stored == case["expected"]["value"]
reproduce = case["reproduce"]
assert len(reproduce["model"]["weights_sha256"]) == 64
assert reproduce["model"]["tokenizer_vocab_sha256"]
assert reproduce["dataset"]["revision"].startswith("sha256:")
assert reproduce["dataset"]["rows_sha256"].startswith("sha256:")
assert {s["id"] for s in reproduce["scorers"]} == {"exact_answer"}
assert reproduce["backend"]["label"] and reproduce["recipe"]["spec_digest"]
kinds = {d["kind"] for d in case["dependencies"]}
assert {"model", "tokenizer", "dataset", "scorer", "backend", "direction"} <= kinds
failed = suites["suites"]["failures"]["cases"][0]
assert failed["expected"]["outcome"] == "failed"
assert failed["reproduce"]["backend"]["descriptor"]["dtype"] == "bfloat16"
assert failed["reproduce"]["backend"]["runtime"].startswith("torch ")
report = status(run)
assert set(states(report).values()) == {"current"}
assert report["graph"]["nodes"] and len(report["graph"]["edges"]) >= 5 * 6
@pytest.mark.parametrize("snapshot,stale", [
({"backends": {"cpu/bfloat16/none/cached/default": "torch 9.9"}}, {"qualification:bfloat16"}),
({"backends": {"transformers/cpu/float32": "torch 9.9"}},
{"confirmation", "replication", "verification"}),
({"scorers": {"exact_answer": 2}}, "all"),
({"models": {"local/answers": {"tokenizer_vocab_sha256": "0" * 64}}}, "all"),
({"models": {"other/model": "1" * 64}, "scorers": {"other_scorer": 3}}, set()),
])
def test_dependency_change_marks_exactly_the_affected_evidence_stale(run, snapshot, stale):
report = status(run, snapshot=snapshot)
found = states(report)
expected = set(found) if stale == "all" else stale
assert {k for k, v in found.items() if v == "stale"} == expected
assert {k for k, v in found.items() if v == "current"} == set(found) - expected
for key in expected:
assert any("changed" in reason for reason in by_kind(report)[key]["reasons"])
def test_dataset_artifact_and_feature_changes_are_computed_not_edited(run):
before = files(run)
cases = by_kind(status(run))
fresh = cases["verification"]["condition"]["domain"]
pack = next(d["key"] for d in reg.cases_of(reg.build(run))[cases["verification"]["case"]][2]
["dependencies"] if d["kind"] == "dataset")
assert Path(pack).name == fresh.split("/")[0]
report = status(run, snapshot={"datasets": {pack: "sha256:" + "2" * 64}})
assert {k for k, v in states(report).items() if v == "stale"} == {
"verification", "qualification:float32", "qualification:bfloat16"}
assert files(run) == before
# Feature report (direction dictionary) changed on disk: only evidence citing it.
feature = run / "candidates" / "layer.0-binary-fit.json"
feature.write_bytes(feature.read_bytes() + b" ")
assert {k for k, v in states(status(run)).items() if v == "stale"} == {
"confirmation", "replication"}
# Saved candidate artifact changed: the verification and both qualifications.
artifact = next((run / "persistent-candidates").glob("*/artifact/adapter_config.json"))
artifact.write_bytes(artifact.read_bytes() + b" ")
assert {k for k, v in states(status(run)).items() if v == "stale"} == set(states(status(run)))
def test_same_model_name_with_new_weights_inherits_nothing(run):
new = "f" * 64
reg.observe(run, {"models": {"local/answers": new}})
report = status(run)
assert set(states(report).values()) == {"stale"}
rows = report["dashboard"]["rows"]
untested = [r for r in rows if r["status"] == "untested"]
assert untested and all(r["condition"]["model"] == f"local/answers@weights:{new[:12]}"
for r in untested)
assert all("not inherited by name" in r["reasons"][0] for r in untested)
# Nothing on the new weights is qualified or current: the old rows are stale.
assert not [r for r in rows if r["condition"]["model"].endswith(new[:12])
and r["status"] != "untested"]
backends = {r["condition"]["backend"] for r in untested}
assert {"cpu/float32/none/cached/default", "cpu/bfloat16/none/cached/default"} <= backends
# Observing the recorded fingerprint again makes the evidence current.
recorded = by_kind(report)["confirmation"]["expected"]
weights = reg.cases_of(reg.build(run))[by_kind(report)["confirmation"]["case"]][2][
"reproduce"]["model"]["weights_sha256"]
reg.observe(run, {"models": {"local/answers": weights}})
assert set(states(status(run)).values()) == {"current"} and recorded["outcome"] == "supported"
def test_declared_backend_without_attempt_is_untested(run):
declared = {"device": "cpu", "dtype": "float16", "cache": "cached"}
report = status(run, snapshot={"declared_backends": [declared, CPU32]})
[row] = [r for r in report["dashboard"]["rows"] if r["status"] == "untested"]
assert row["condition"]["backend"] == "cpu/float16/none/cached/default"
assert row["capability"] == "unknown" and row["artifact"].startswith("sha256:")
assert report["dashboard"]["counts"]["untested"] == 1
class DriftIO(BackendIO):
"""Every backend-side load perturbs the head: an inference regression the descriptor
does not name."""
def load(self, path, section):
handle = super().load(path, section)
if len(self.models) % 2 == 0:
with torch.no_grad():
handle.model.head.bias.add_(0.5)
return handle
def test_reruns_append_history_and_regressions_link_to_reproducible_cases(run, env): # noqa: F811
suites = reg.build(run)
cases = by_kind(status(run))
historical = {c["evidence"]["path"]: files(run)[c["evidence"]["path"]]
for _, _, c in reg.cases_of(suites).values()}
suite_bytes = (run / "regression" / "suites" / "0001.json").read_bytes()
first = reg.run_case(run, cases["verification"]["case"], model_io=FixtureIO(),
pack_resolver=ConceptPack.load)
assert first["status"] == "pass" and first["seq"] == 1
log = (run / "regression" / "results.jsonl").read_bytes()
second = reg.run_case(run, cases["qualification:float32"]["case"], model_io=DriftIO(),
pack_resolver=ConceptPack.load)
assert second["status"] == "regression" and second["observed"]["outcome"] == "failed"
# Append-only: earlier lines, suite version and historical evidence unchanged.
assert (run / "regression" / "results.jsonl").read_bytes().startswith(log)
assert (run / "regression" / "suites" / "0001.json").read_bytes() == suite_bytes
assert {p: files(run)[p] for p in historical} == historical
# The regression links to its case; the case's evidence digest verifies.
link = second["case_link"]
document = json.loads((run / "regression" / link["suite_file"]).read_text())
case = refs.resolve_pointer(document, link["pointer"])
assert case["id"] == second["case"] == cases["qualification:float32"]["case"]
assert refs.file_sha256(run / case["evidence"]["path"]) == case["evidence"]["sha256"]
report = status(run)
row = by_kind(report)["qualification:float32"]
assert row["status"] == "regressed" and row["regression"]["case_link"] == link
assert [h["status"] for h in row["history"]] == ["regression"]
assert by_kind(report)["verification"]["status"] == "current"
# A rebuild does not turn re-run evidence into new historical cases.
assert reg.build(run)["unchanged"] is True
plan = reg.revalidate(run)
assert [c["case"] for c in plan["cases"]] == [second["case"]]
assert plan["suites"] == [{"name": "confirmed", "id": suites["suites"]["confirmed"]["id"]}]
assert (run / "regression" / "plans" / f"{plan['id'][7:]}.json").is_file()
assert reg.revalidate(run)["id"] == plan["id"]
def test_run_refusals_and_edited_history(run):
cases = by_kind(status(run))
case = cases["verification"]
with pytest.raises(rc.CaseError, match="historical evidence"):
reg.run_case(run, case["case"], result=case["validated_by"]["historical_evidence"]["path"])
other = cases["qualification:bfloat16"]["validated_by"]["historical_evidence"]["path"]
with pytest.raises(rc.CaseError, match="not a re-run"):
reg.run_case(run, cases["qualification:float32"]["case"], result=other)
with pytest.raises(LibraryError, match="No case"):
reg.run_case(run, "sha256:" + "0" * 64, result=other)
with pytest.raises(LibraryError, match="No regression library"):
status(run / "nowhere")
with pytest.raises(ValueError, match="Unknown snapshot fields"):
reg.observe(run, {"weights": {}})
reg.observe(run, {"backends": {"x": "1"}})
reg.observe(run, {"backends": {"x": "2"}})
log = run / "regression" / "dependencies.jsonl"
lines = log.read_bytes().split(b"\n")
log.write_bytes(b"\n".join([lines[1], lines[0], *lines[2:]]))
with pytest.raises(LibraryError, match="append-only chain"):
status(run)
log.write_bytes(b"\n".join(lines[:2]) + b"\n" + b'{"torn"')
assert status(run)["cases"]
with pytest.raises(LibraryError, match="torn line"):
reg.observe(run, {})
suite = run / "regression" / "suites" / "0001.json"
suite.write_text(suite.read_text().replace('"confirmed"', '"Confirmed"', 1))
with pytest.raises(LibraryError, match="edited"):
status(run)
def test_evidence_changed_after_recording_is_invalid(run):
case = by_kind(status(run))["confirmation"]
path = run / case["validated_by"]["historical_evidence"]["path"]
path.write_bytes(path.read_bytes() + b" ")
found = by_kind(status(run))["confirmation"]
assert found["status"] == "invalid" and "changed after" in found["reasons"][0]
assert by_kind(status(run))["replication"]["status"] == "current"
def test_cli_build_observe_status_revalidate_run(run, tmp_path, capsys):
from obliteratus.cli import main
main(["experiments", "regression", "build", "--run", str(run), "--tolerance", "0.1"])
built = json.loads(capsys.readouterr().out)
assert built["version"] == 2 and built["unchanged"] is False
snapshot = tmp_path / "snapshot.json"
snapshot.write_text(json.dumps({"backends": {"cpu/bfloat16/none/cached/default": "x"}}))
main(["experiments", "regression", "observe", str(snapshot), "--run", str(run)])
assert json.loads(capsys.readouterr().out)["seq"] == 1
main(["experiments", "regression", "status", "--run", str(run)])
report = json.loads(capsys.readouterr().out)
assert report["dashboard"]["counts"]["stale"] == 1
conditions = report["conditions"]["alpha/channel:layer.0:2|steer/all_tokens/raw"]
assert conditions["aggregate"] is None and len(conditions["conditions"]) == 4
assert {(c["condition"]["backend"], c["status"], c["capability"])
for c in conditions["conditions"]} == {
("transformers/cpu/float32", "current", "works"),
("cpu/float32/none/cached/default", "current", "works"),
("cpu/bfloat16/none/cached/default", "stale", "fails")}
main(["experiments", "regression", "revalidate", "--run", str(run)])
plan = json.loads(capsys.readouterr().out)
assert [c["status"] for c in plan["cases"]] == ["stale"]
case = plan["cases"][0]["case"]
original = by_kind(status(run))["qualification:bfloat16"]
result = original["validated_by"]["historical_evidence"]["path"]
with pytest.raises(SystemExit) as refused:
main(["experiments", "regression", "run", "--run", str(run), "--case", case,
"--result", result])
assert refused.value.code == 4 and "historical evidence" in capsys.readouterr().err
with pytest.raises(SystemExit) as refused:
main(["experiments", "regression", "run", "--run", str(run), "--plan", plan["id"],
"--result", result])
assert refused.value.code == 4