"""Persistent candidates: maturity gate, preserved reference, reload verification, provenance. The fixture model is the planted yes/no model of ``tests/test_controlled_interventions.py`` with its running sum moved before the layers and layer 0 a plain identity linear map (zero bias), so the site module ``layers.0`` is a linear map writing the site output and weight-space representations apply. It is saved to a reference directory and every load builds a fresh object from those bytes. Two keys reach ``persistent-pilot eligible`` under the test-only fixture allowance: an all-token steer of ``channel:layer.0:2`` (flips control rows to "yes") and an all-token ablation of it (flips positive rows to "no"). Verification uses a second pack revision whose rows were never seen by discovery or any registered confirmation. """ from __future__ import annotations import json import shutil from pathlib import Path from types import SimpleNamespace import pytest import torch from safetensors.torch import load_file, save_file from torch import nn from obliteratus.analysis.target_dossier import build_dossier, write_dossier from obliteratus.concepts.pack import ConceptPack from obliteratus.experiments import candidate as cand from obliteratus.experiments import candidate_edits as edits from obliteratus.experiments import candidate_status as status from obliteratus.experiments import registry as reg from obliteratus.experiments.candidate_edits import CandidateRefused from obliteratus.experiments.candidate_io import tree_digests from obliteratus.experiments.candidate_verify import verify_candidate from obliteratus.experiments.intervention_runner import run_intervention from obliteratus.experiments.registry_store import Registry from tests.test_confirmation_registry import _partition from tests.test_controlled_interventions import ( AnswerModel, AnswerTokenizer, _run, make_spec, ) from tests.test_labeled_activations import WIDTH, build_pack from tests.test_target_dossier import HYPOTHESIS, LEXICAL, REPORT pytestmark = pytest.mark.cpu ABLATE = {"useful_effect_threshold": 0.5, "effect_direction": "decrease", "collateral_margin": 0.2, "decision_coefficient": 1.0, "target_task": "keep"} class SumAnswerModel(AnswerModel): """The running sum moves before the layers; layer 0 is a plain identity ``nn.Linear``.""" def __init__(self): super().__init__() layer = nn.Linear(WIDTH, WIDTH) with torch.no_grad(): layer.weight.copy_(torch.eye(WIDTH)) layer.bias.zero_() self.layers[0] = layer self.eval() def forward(self, input_ids, attention_mask=None): self.forward_calls += 1 hidden = self.embed(input_ids).cumsum(1) for layer in self.layers: hidden = layer(hidden) return SimpleNamespace(logits=self.head(hidden)) class FixtureIO: """Saves a state dict; every load zeroes a fresh object and fills it from the file.""" name = "fixture" def __init__(self): self.loads = [] def load(self, path, section): self.loads.append(str(path)) model = SumAnswerModel() with torch.no_grad(): for tensor in model.state_dict().values(): tensor.zero_() model.load_state_dict(load_file(str(Path(path) / "model.safetensors"))) return SimpleNamespace(model=model, tokenizer=AnswerTokenizer()) def save(self, model, tokenizer, path): path = Path(path) path.mkdir(parents=True, exist_ok=False) save_file({k: v.contiguous() for k, v in model.state_dict().items()}, str(path / "model.safetensors")) (path / "config.json").write_text(json.dumps({"model_type": "planted-test"})) def _fresh(layout): """Same layout, texts shifted to numbers never paired with their class word.""" _partition(layout) for path, rows in layout.items(): if path.startswith("battery/"): continue for row in rows: word, _, number = row["text"].rpartition(" ") row["text"] = f"{word} {(int(number) + 80) % 400}" def steer(env, role="calibrate", seed=0, control=7): dataset = {"role": role, **({"allow_locked_test": True} if role == "test" else {})} return make_spec(env["pack"], dataset=dataset, seed=seed, control={"seed": control}, operation={"token_scope": "all_tokens"}) def ablate(env, role="calibrate", seed=0, control=7): spec = steer(env, role, seed, control) spec.update(dossier="dossiers/abl.json", target_task="keep", operation={"kind": "ablate", "site": "layer.0", "token_scope": "all_tokens", "units": "projection_fraction", "coefficients": [0.5, 1.0], "decision_coefficient": 1.0}) return spec def _intervene(env, run, spec): factory = lambda section: SimpleNamespace(model=env["model"], # noqa: E731 tokenizer=AnswerTokenizer()) return run_intervention(spec, run, model_factory=factory, pack_resolver=ConceptPack.load) def make_eligible(env, run, make, replicate=True): document = reg.register(run, make(env), pack_resolver=ConceptPack.load) result = _intervene(env, run, make(env)) assert reg.unlock(run, document["id"], result["location"]["result"])["outcome"] == "supported" if replicate: again = make(env, "test", seed=3, control=9) copy = reg.replicate(run, document["id"], again, pack_resolver=ConceptPack.load) result = _intervene(env, run, again) assert reg.unlock(run, copy["id"], result["location"]["result"])["outcome"] == "supported" return document["id"] @pytest.fixture(scope="module") def env(tmp_path_factory): root = tmp_path_factory.mktemp("candidates") pack = build_pack(root / "pack", _partition) pack.location = root / "pack" fresh = build_pack(root / "fresh", _fresh) model, io = SumAnswerModel(), FixtureIO() io.save(model, None, root / "reference") env = {"root": root, "pack": pack, "model": model, "io": io, "source": root / "reference", "fresh": {"pack": str(root / "fresh"), "revision": fresh.dataset().revision, "role": "select"}} base = _run(root, pack, "base", model, "local/answers") steer_run = shutil.copytree(base, root / "steer") env["steer"] = (steer_run, make_eligible(env, steer_run, steer)) ablate_run = shutil.copytree(base, root / "ablate") write_dossier(build_dossier("abl", ablate_run, hypothesis={**HYPOTHESIS, **ABLATE}, discovery=REPORT, estimates=["channel:layer.0:2"], confound_resolutions=[LEXICAL]), ablate_run) env["ablate"] = (ablate_run, make_eligible(env, ablate_run, ablate)) causal = shutil.copytree(base, root / "causal") env["causal"] = (causal, make_eligible(env, causal, steer, replicate=False)) env["source_bytes"] = (root / "reference" / "model.safetensors").read_bytes() return env def build(env, key, **options): run, registration = env[key] options = {"model_io": env["io"], "allow_fixture_results": True, **options} return cand.build_candidate(run, registration, env["source"], **options) def verify(env, key, manifest): return verify_candidate(env[key][0], manifest["id"], env["fresh"], model_io=env["io"], pack_resolver=ConceptPack.load) def checks(record): return {c["check"]: c["passed"] for c in record["checks"]} def test_build_is_refused_below_persistent_pilot_eligible(env): run, registration = env["causal"] with pytest.raises(CandidateRefused, match="'causal'.*persistent-pilot eligible"): cand.build_candidate(run, registration, env["source"], model_io=env["io"], allow_fixture_results=True) run, registration = env["steer"] # Fixture (software-contract) results never count without the test-only allowance. with pytest.raises(CandidateRefused, match="'candidate'"): cand.build_candidate(run, registration, env["source"], model_io=env["io"]) replication = next(d["id"] for d in Registry(run).registrations() if d["kind"] == "replication") with pytest.raises(CandidateRefused, match="is a replication"): cand.build_candidate(run, replication, env["source"], model_io=env["io"], allow_fixture_results=True) with pytest.raises(CandidateRefused, match="outside the candidate store"): cand.build_candidate(run, registration, run, model_io=env["io"], allow_fixture_results=True) assert env["io"].loads == [] and not (run / cand.CANDIDATE_DIR).exists() @pytest.mark.parametrize("key,representation", [("steer", "auto"), ("steer", "weight_edit"), ("ablate", "lora"), ("ablate", "weight_edit")]) def test_reloaded_candidate_reproduces_the_effect_and_preserves_the_reference( env, key, representation): before = tree_digests(env["source"]) manifest = build(env, key, representation=representation) expected = "activation_adapter" if representation == "auto" else representation assert manifest["edit"]["representation"] == expected assert manifest["scope"]["scope"] == "software-contract" assert tree_digests(env["source"]) == before == manifest["parent"]["files"] record = verify(env, key, manifest) assert record["outcome"] == "passed", [c for c in record["checks"] if not c["passed"]] assert record["selected"] is True and all(checks(record).values()) comparison = record["comparison"] assert comparison["candidate_minus_temporary"] == 0.0 assert comparison["completion_agreement"] == 1.0 assert comparison["max_probe_logit_difference"] <= 1e-4 effect = comparison["effect_vs_baseline"]["candidate"] assert (effect >= 0.5) if key == "steer" else (effect <= -0.5) assert set(comparison["collateral"].values()) == {"PASS"} assert record["independence"]["basis"] == "different dataset revision" # The reference artifact is byte-identical after build and reload verification. assert (env["source"] / "model.safetensors").read_bytes() == env["source_bytes"] state = status.show(env[key][0], manifest["id"])["state"] assert state == {"state": "passed", "selected": True, "verifications": 1} path = Path(manifest["location"]) assert (path / "verifications" / "0001" / "candidate" / "keep" / "generations.json").is_file() if expected == "weight_edit": changed = manifest["edit"]["changed_parameters"] assert changed == (["layers.0.bias"] if key == "steer" else ["layers.0.weight"]) def test_provenance_manifest_is_complete_and_every_digest_resolves(env): from obliteratus.analysis import dossier_refs as refs from obliteratus.analysis.dossier_interventions import hypothesis_digest from obliteratus.analysis.target_dossier import read_dossier run, registration = env["steer"] manifest = build(env, "steer", representation="weight_edit") stored, path = cand.read_candidate(run, manifest["id"]) assert stored["id"] == cand.manifest_id(stored) == manifest["id"] for field in ("schema", "schema_version", "created_at", "actor", "tool", "scope", "registration", "parent", "recipe", "edit", "selection", "compatibility", "artifact", "tolerances", "equivalence"): assert stored[field] not in (None, "", {}), field document = Registry(run).get(stored["registration"]["id"]) assert document["frozen"]["recipe"]["spec_digest"] == stored["recipe"]["spec_digest"] assert stored["registration"]["target_key"] == document["frozen"]["target_key"] dossier, _ = read_dossier(refs.resolve_path(stored["recipe"]["dossier"]["path"], run)) assert hypothesis_digest(dossier["hypothesis"]) == \ stored["recipe"]["dossier"]["hypothesis_sha256"] parent = stored["parent"] assert tree_digests(parent["source"]) == parent["files"] assert parent["weights_full"]["mode"] == "full" and parent["model_id"] == "local/answers" assert tree_digests(path / "artifact") == stored["artifact"]["files"] assert cand.artifact_problems(stored, path) == [] edit = stored["edit"] assert edit["vector_sha256"] == stored["recipe"]["target"]["vector_sha256"] reloaded = env["io"].load(path / "artifact", None).model digests = cand.tensor_digests(reloaded) reference = cand.tensor_digests(env["io"].load(env["source"], None).model) assert sorted(n for n in digests if digests[n] != reference[n]) == edit["changed_parameters"] assert all(digests[t["name"]] == t["after_sha256"] and reference[t["name"]] == t["before_sha256"] for t in edit["tensors"]) assert all(c["passed"] for c in stored["compatibility"]) options = stored["selection"]["options"] assert options["lora"]["feasible"] is False and options["activation_adapter"]["feasible"] # An edited manifest no longer reproduces its id. text = (path / "candidate.json").read_text() (path / "candidate.json").write_text(text.replace('"weight_edit"', '"lora"', 1)) with pytest.raises(CandidateRefused, match="edited after it was written"): cand.read_candidate(run, manifest["id"]) (path / "candidate.json").write_text(text) def test_broken_candidates_fail_verification_and_are_never_selected(env, monkeypatch): run = env["steer"][0] # 1. A corrupted edit whose files and manifest agree: only the measurement catches it. real = edits.weight_edit monkeypatch.setattr(edits, "weight_edit", lambda module, op, vector, c: real(module, op, vector, c / 12)) weak = build(env, "steer", representation="weight_edit") monkeypatch.setattr(edits, "weight_edit", real) record = verify(env, "steer", weak) found = checks(record) assert record["outcome"] == "failed" and record["selected"] is False assert found["artifact_digests"] and found["direction_binding"] assert not found["reproduces_temporary_effect"] and not found["intended_effect"] # 2. A wrong direction is refused by the binding to the registered recipe. from obliteratus.experiments import intervention_checks as checks_module verify_model = checks_module.verify_model def wrong(*args): target = verify_model(*args) vector = torch.zeros_like(target["vector"]) vector[5] = 1 record = {**target["record"], "vector_sha256": checks_module.vector_sha256(vector)} return {**target, "vector": vector, "record": record} monkeypatch.setattr(checks_module, "verify_model", wrong) misdirected = build(env, "steer") monkeypatch.setattr(checks_module, "verify_model", verify_model) record = verify(env, "steer", misdirected) assert record["outcome"] == "failed" and not checks(record)["direction_binding"] # 3. Tampered artifact bytes fail provenance before any model is loaded. good = build(env, "steer") adapter = Path(good["location"]) / "artifact" / edits.ADAPTER_FILE original = adapter.read_bytes() save_file({"vectors": torch.ones(WIDTH)}, str(adapter)) loads = len(env["io"].loads) record = verify(env, "steer", good) assert record["outcome"] == "failed" and not checks(record)["artifact_digests"] assert len(env["io"].loads) == loads and record["comparison"] is None for broken in (weak, misdirected, good): assert status.show(run, broken["id"])["state"]["selected"] is False with pytest.raises(CandidateRefused, match="not selected"): status.attach(run, broken["id"]) # A failed candidate stays failed even if a later attempt passes. adapter.write_bytes(original) again = verify(env, "steer", good) assert again["outcome"] == "passed" and again["selected"] is False assert status.show(run, good["id"])["state"] == {"state": "failed", "selected": False, "verifications": 2} assert (env["source"] / "model.safetensors").read_bytes() == env["source_bytes"] def test_verification_rows_must_be_independent(env): run = env["steer"][0] manifest = build(env, "steer") calibrate = {"pack": str(env["pack"].location), "revision": env["pack"].dataset().revision, "role": "calibrate"} with pytest.raises(CandidateRefused, match="registered confirmation sets"): verify_candidate(run, manifest["id"], calibrate, model_io=env["io"], pack_resolver=ConceptPack.load) with pytest.raises(ValueError, match="confirmation data|not independent"): verify_candidate(run, manifest["id"], {**calibrate, "role": "select"}, model_io=env["io"], pack_resolver=ConceptPack.load) assert status.show(run, manifest["id"])["state"]["state"] == "built" def test_cli_build_verify_show_and_registry_attachment(env, monkeypatch, capsys): from obliteratus.cli import main monkeypatch.setattr("obliteratus.experiments.candidate_io.TransformersIO", FixtureIO) monkeypatch.setattr("obliteratus.experiments.inputs.default_pack_resolver", ConceptPack.load) run, registration = env["causal"] with pytest.raises(SystemExit) as refused: main(["experiments", "candidate", "build", registration, "--run", str(run), "--source", str(env["source"])]) assert refused.value.code == 4 and "persistent-pilot eligible" in capsys.readouterr().err run, registration = env["ablate"] main(["experiments", "candidate", "build", registration, "--run", str(run), "--source", str(env["source"]), "--representation", "lora", "--agreement", "0.9", "--allow-fixture-results"]) built = json.loads(capsys.readouterr().out) assert built["representation"] == "lora" and built["tolerances"]["agreement"] == 0.9 fresh = env["fresh"] main(["experiments", "candidate", "verify", built["id"], "--run", str(run), "--pack", fresh["pack"], "--revision", fresh["revision"], "--role", "select", "--attach"]) verified = json.loads(capsys.readouterr().out) assert verified["outcome"] == "passed" and verified["attachment"]["candidate"] == built["id"] main(["experiments", "candidate", "show", "--run", str(run)]) shown = json.loads(capsys.readouterr().out) row = next(r for r in shown["candidates"] if r["id"] == built["id"]) assert row["selected"] is True and row["representation"] == "lora" [attached] = [a for a in shown["attachments"] if a["candidate"] == built["id"]] assert attached["valid"] is True and "persistent-pilot evidence" in attached["note"] main(["experiments", "candidate", "show", built["id"], "--run", str(run)]) assert json.loads(capsys.readouterr().out)["state"]["selected"] is True def test_unexpressible_representations_and_tolerances_are_refused(env): run = env["steer"][0] with pytest.raises(CandidateRefused, match="cannot express.*projection, not a translation"): build(env, "steer", representation="lora") with pytest.raises(CandidateRefused, match="Unknown tolerances"): build(env, "steer", tolerances={"effect": 0.1}) with pytest.raises(CandidateRefused, match="Unknown representation"): build(env, "steer", representation="merged") assert not list((run / cand.CANDIDATE_DIR).glob(".staging-*")) def test_changed_attachment_no_longer_counts(env): run = env["ablate"][0] manifest = build(env, "ablate") assert manifest["edit"]["representation"] == "activation_adapter" assert verify(env, "ablate", manifest)["selected"] is True entry = status.attach(run, manifest["id"], actor="tester") assert entry["actor"] == "tester" and entry["scope"] == "software-contract" target = run / entry["verification"]["path"] target.write_text(target.read_text() + " ") [row] = [r for r in status.registry_evidence(run) if r["candidate"] == manifest["id"]] assert row["valid"] is False and "no longer verifies" in row["note"] (Path(manifest["location"]) / "candidate.json").write_text("{}") assert any(r["state"] == "invalid" for r in status.show(run)["candidates"])