mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 11:04:06 +01:00
Add F18: recovery runs that start from a verified persistent candidate and produce a separate recovered candidate, assessed against both the edited candidate and the pristine reference. - Only selected, digest-verified, persistent-pilot eligible parents are accepted. The output is a write-once removable recovery adapter with learning curves; parent and reference are only read and re-checked. - Recovery data lineage, training configuration and teacher provenance (model, revision, weight fingerprint) are recorded and re-hashed before assessment. - Recovery and assessment rows that overlap discovery, dossier confirmation data, registered confirmation sets, the parent's verification rows or each other are refused before training. - A reload assessment compares pristine, edited and recovered arms on independent rows. Restoring the unwanted target behaviour beyond tolerance fails the run permanently; passing needs persisting target effect, retained capability versus pristine, no new damage and a useful gain. - Efficiency or freed-capacity claims need matched random-target and pristine same-steps controls and must beat both; otherwise they are unsupported. Loss curves are never reported as mechanism. - `obliteratus experiments recovery run|verify|show`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
260 lines
14 KiB
Python
260 lines
14 KiB
Python
"""Recovery tuning of a selected persistent candidate: contamination, provenance, paired assessment.
|
|
|
|
The parent is the steer candidate of ``tests/test_persistent_candidates.py``: an
|
|
all-token steer of ``channel:layer.0:2`` that flips control rows ("beta") to
|
|
"yes" (the target task ``flip``). It also damages a retained capability the
|
|
candidate was never verified on: rows starting ``present present`` (channel 0)
|
|
should be answered "no", and the edited candidate answers "yes". A recovery
|
|
adapter on the head can restore that capability by reading channel 0 while the
|
|
target effect (read from channel 2) persists, when its data also contains rows
|
|
that keep the edited target behaviour. Self-distilling the pristine model on
|
|
target-like rows instead restores the unwanted target behaviour and fails.
|
|
Recovery and assessment rows come from pack revisions whose texts were never
|
|
used by discovery, confirmation or the candidate's verification.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import shutil
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from obliteratus.concepts.pack import ConceptPack
|
|
from obliteratus.experiments import candidate as cand
|
|
from obliteratus.experiments import recovery as rec
|
|
from obliteratus.experiments import recovery_status as rstatus
|
|
from obliteratus.experiments.candidate_io import tree_digests
|
|
from obliteratus.experiments.candidate_verify import verify_candidate
|
|
from obliteratus.experiments.recovery_spec import RecoveryRefused, load_recovery_spec
|
|
from tests.test_confirmation_registry import _partition
|
|
from tests.test_controlled_interventions import _run
|
|
from tests.test_labeled_activations import build_pack
|
|
from tests.test_persistent_candidates import (
|
|
FixtureIO, SumAnswerModel, _fresh, make_eligible, steer,
|
|
)
|
|
|
|
pytestmark = pytest.mark.cpu
|
|
TRAINING = {"seed": 0, "steps": 20, "learning_rate": 0.1, "batch_size": 8, "rank": 2,
|
|
"alpha": 2, "target_modules": ["head"]}
|
|
|
|
|
|
def _shifted(shift):
|
|
"""Texts shifted by ``shift``; neighbour rows become ``present`` rows answered "no"."""
|
|
def mutate(layout):
|
|
_partition(layout)
|
|
for path, rows in layout.items():
|
|
if path.startswith("battery/"):
|
|
continue
|
|
for row in rows:
|
|
word, _, number = row["text"].rpartition(" ")
|
|
if path == "neighbours/near.jsonl":
|
|
word, row["answer"] = "present present sample", "no"
|
|
row["text"] = f"{word} {(int(number) + shift) % 400}"
|
|
return mutate
|
|
|
|
|
|
def _dataset(pack):
|
|
return {"pack": str(pack.location), "revision": pack.dataset().revision, "role": "select"}
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def env(tmp_path_factory):
|
|
root = tmp_path_factory.mktemp("recovery")
|
|
pack = build_pack(root / "pack", _partition)
|
|
pack.location = root / "pack"
|
|
fresh = build_pack(root / "fresh", _fresh)
|
|
packs = {}
|
|
for name, shift in (("recovery", 160), ("assessment", 240)):
|
|
packs[name] = build_pack(root / name, _shifted(shift))
|
|
packs[name].location = root / name
|
|
model, io = SumAnswerModel(), FixtureIO()
|
|
io.save(model, None, root / "reference")
|
|
env = {"root": root, "pack": pack, "model": model, "io": io, "source": root / "reference",
|
|
"packs": packs, "fresh": {"pack": str(root / "fresh"),
|
|
"revision": fresh.dataset().revision, "role": "select"}}
|
|
base = _run(root, pack, "base", model, "local/answers")
|
|
run = shutil.copytree(base, root / "steer")
|
|
registration = make_eligible(env, run, steer)
|
|
manifest = cand.build_candidate(run, registration, env["source"], model_io=io,
|
|
allow_fixture_results=True)
|
|
assert verify_candidate(run, manifest["id"], env["fresh"], model_io=io,
|
|
pack_resolver=ConceptPack.load)["selected"]
|
|
env.update(run=run, candidate=manifest, registration=registration)
|
|
return env
|
|
|
|
|
|
def make_spec(env, *, sets=("neighbours/near.jsonl", "controls/neutral.jsonl"), data=None,
|
|
teacher=None, controls=(), **assessment):
|
|
return {"schema_version": "1", "kind": "candidate_recovery",
|
|
"candidate": env["candidate"]["id"],
|
|
"data": {**(data or _dataset(env["packs"]["recovery"])),
|
|
"sets": [{"path": p, "target_field": "answer"} for p in sets]},
|
|
"teacher": teacher, "training": TRAINING, "controls": list(controls),
|
|
"assessment": {"dataset": _dataset(env["packs"]["assessment"]),
|
|
"retained_tasks": [{"name": "present", "path": "neighbours/near.jsonl",
|
|
"target_field": "answer"}],
|
|
"useful_gain": 0.5, "target_restoration_tolerance": 0.1,
|
|
"retained_margin": 0.3, **assessment}}
|
|
|
|
|
|
def run(env, spec):
|
|
return rec.run_recovery(env["run"], spec, model_io=env["io"], pack_resolver=ConceptPack.load)
|
|
|
|
|
|
def checks(record):
|
|
return {c["check"]: c["passed"] for c in record["checks"]}
|
|
|
|
|
|
def parent_bytes(env):
|
|
path = Path(env["candidate"]["location"])
|
|
return (path / cand.MANIFEST).read_bytes(), tree_digests(path / "artifact")
|
|
|
|
|
|
def test_useful_recovery_passes_with_paired_results_and_preserves_the_parent(env):
|
|
before, reference = parent_bytes(env), tree_digests(env["source"])
|
|
found = run(env, make_spec(env))
|
|
record, manifest = found["assessment"], found["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())
|
|
rates = record["comparison"]["rates"]
|
|
# Editing damaged the retained capability; recovery restores it, the target effect persists.
|
|
assert rates["pristine"]["present"] >= 0.9 and rates["edited"]["present"] <= 0.1
|
|
assert rates["recovered"]["present"] >= 0.9
|
|
assert rates["pristine"]["flip"] <= 0.1 and rates["edited"]["flip"] >= 0.9
|
|
assert record["comparison"]["target_restoration"] <= 0.1
|
|
assert set(record["scorecards"]) == {"edited_vs_pristine", "recovered_vs_pristine",
|
|
"recovered_vs_edited"}
|
|
# Efficiency claims are withheld without matched controls.
|
|
assert record["claims"]["status"] == "unsupported" and "missing" in record["claims"]["reason"]
|
|
assert "never reported as an explanatory mechanism" in record["claims"]["note"]
|
|
# A new artifact: parent candidate and reference are byte-identical.
|
|
assert parent_bytes(env) == before and tree_digests(env["source"]) == reference
|
|
path = Path(manifest["location"])
|
|
assert path.parent.name == rstatus.RECOVERED_DIR and path.name != Path(
|
|
env["candidate"]["location"]).name
|
|
training = manifest["training"]
|
|
for key in ("seed", "steps", "learning_rate", "optimizer", "trainable_parameters"):
|
|
assert training[key] not in (None, "", {}), key
|
|
curve = json.loads((path / training["curve_file"]).read_text())
|
|
assert [p["step"] for p in curve] == list(range(1, TRAINING["steps"] + 1))
|
|
assert training["curve"]["final_loss"] < training["curve"]["initial_loss"]
|
|
shown = rstatus.show(env["run"], manifest["id"])
|
|
assert shown["state"] == {"state": "passed", "selected": True, "assessments": 1,
|
|
"parent_selected": True}
|
|
assert any(r["id"] == manifest["id"] for r in rstatus.show(env["run"])["recovered"])
|
|
|
|
|
|
def test_provenance_resolves_teacher_model_and_data_and_target_restoration_fails(env):
|
|
teacher = {"source": str(env["source"]), "model_id": "local/answers", "revision": None}
|
|
found = run(env, make_spec(env, teacher=teacher))
|
|
record, manifest = found["assessment"], found["manifest"]
|
|
# The pristine teacher relabels target-like rows "no": the unwanted behaviour returns.
|
|
assert record["outcome"] == "failed" and record["selected"] is False
|
|
assert not checks(record)["target_restoration_within_tolerance"]
|
|
assert record["comparison"]["target_restoration"] > 0.1
|
|
state = rstatus.show(env["run"], manifest["id"])["state"]
|
|
assert state["state"] == "failed" and state["selected"] is False
|
|
# Every dependency resolves: teacher files and weights, parent, reference, data lineage.
|
|
provenance = rec.provenance(env["run"], manifest["id"], pack_resolver=ConceptPack.load)
|
|
assert all(c["passed"] for c in provenance), provenance
|
|
stored = manifest["teacher"]
|
|
assert tree_digests(stored["source"]) == stored["files"]
|
|
assert stored["weights_full"]["mode"] == "full" and stored["answers_sha256"]
|
|
data = manifest["data"]
|
|
assert data["revision"] == env["packs"]["recovery"].dataset().revision
|
|
assert data["role"] == "select" and data["pack"]["sha256"]
|
|
assert manifest["parent"]["candidate"] == env["candidate"]["id"]
|
|
# A failed recovery stays failed even when a later attempt passes its checks.
|
|
again = rec.verify_recovered(env["run"], manifest["id"], model_io=env["io"],
|
|
pack_resolver=ConceptPack.load)
|
|
assert again["selected"] is False
|
|
# A changed recovered file breaks provenance before any model is loaded.
|
|
curve = Path(manifest["location"]) / manifest["training"]["curve_file"]
|
|
curve.write_text(curve.read_text() + " ")
|
|
loads = len(env["io"].loads)
|
|
broken = rec.verify_recovered(env["run"], manifest["id"], model_io=env["io"],
|
|
pack_resolver=ConceptPack.load)
|
|
assert broken["outcome"] == "failed" and not checks(broken)["recovered_files"]
|
|
assert len(env["io"].loads) == loads
|
|
|
|
|
|
def test_matched_controls_are_required_before_efficiency_claims(env):
|
|
found = run(env, make_spec(env, controls=("random_targets", "pristine_same_steps")))
|
|
record, manifest = found["assessment"], found["manifest"]
|
|
claims = record["claims"]
|
|
# Random-target training of the same size does not restore the capability.
|
|
assert claims["status"] == "supported"
|
|
assert any(v is not None and v > 0 for v in claims["gain_beyond_pristine_control"].values())
|
|
assert claims["gain_beyond_random_control"]["present"] >= 0.5
|
|
assert claims["matched"]["steps"] == TRAINING["steps"]
|
|
assert set(record["controls"]) == {"random_targets", "pristine_same_steps"}
|
|
for control in manifest["controls"].values():
|
|
assert control["training"]["steps"] == TRAINING["steps"]
|
|
assert control["training"]["rows"] == manifest["training"]["rows"]
|
|
assert "never reported as an explanatory mechanism" in claims["note"]
|
|
|
|
|
|
@pytest.mark.parametrize("case,match", [
|
|
("confirmation", "registered confirmation sets"),
|
|
("verification", "verification rows"),
|
|
("assessment", "overlap the recovery rows"),
|
|
("discovery", "confirmation data|not independent"),
|
|
])
|
|
def test_contaminated_recovery_rows_are_refused(env, case, match):
|
|
pack, fresh = env["pack"], env["fresh"]
|
|
data = {"confirmation": {**_dataset(pack), "role": "calibrate"},
|
|
"verification": {k: fresh[k] for k in ("pack", "revision", "role")},
|
|
"discovery": _dataset(pack)}.get(case)
|
|
spec = make_spec(env, data=data)
|
|
if case == "assessment":
|
|
spec["assessment"]["dataset"] = _dataset(env["packs"]["recovery"])
|
|
with pytest.raises(RecoveryRefused, match=match):
|
|
run(env, spec)
|
|
assert not list((env["run"] / rstatus.RECOVERED_DIR).glob(".staging-*"))
|
|
|
|
|
|
def test_recovery_needs_a_selected_parent_and_a_valid_spec(env, tmp_path):
|
|
with pytest.raises(RecoveryRefused, match="retained_tasks"):
|
|
load_recovery_spec({**make_spec(env), "assessment": {"useful_gain": 0.5}})
|
|
with pytest.raises(RecoveryRefused, match="controls"):
|
|
load_recovery_spec({**make_spec(env), "controls": ["magic"]})
|
|
with pytest.raises(RecoveryRefused, match="learning_rate"):
|
|
load_recovery_spec({**make_spec(env), "training": {**TRAINING, "learning_rate": -1}})
|
|
with pytest.raises(RecoveryRefused, match="Unreadable"):
|
|
load_recovery_spec(tmp_path / "missing.json")
|
|
unverified = cand.build_candidate(env["run"], env["registration"], env["source"],
|
|
model_io=env["io"], representation="weight_edit",
|
|
allow_fixture_results=True)
|
|
spec = {**make_spec(env), "candidate": unverified["id"]}
|
|
with pytest.raises(RecoveryRefused, match="parent candidate is built"):
|
|
run(env, spec)
|
|
# Once verified, the same weight-edit candidate is a valid parent.
|
|
assert verify_candidate(env["run"], unverified["id"], env["fresh"], model_io=env["io"],
|
|
pack_resolver=ConceptPack.load)["selected"]
|
|
found = run(env, spec)
|
|
assert found["assessment"]["outcome"] == "passed"
|
|
assert found["manifest"]["parent"]["representation"] == "weight_edit"
|
|
|
|
|
|
def test_cli_run_verify_and_show(env, monkeypatch, capsys, tmp_path):
|
|
from obliteratus.cli import main
|
|
monkeypatch.setattr("obliteratus.experiments.candidate_io.TransformersIO", FixtureIO)
|
|
monkeypatch.setattr("obliteratus.experiments.inputs.default_pack_resolver",
|
|
ConceptPack.load)
|
|
spec = tmp_path / "recovery.json"
|
|
spec.write_text(json.dumps(make_spec(env)))
|
|
main(["experiments", "recovery", "run", str(spec), "--run", str(env["run"])])
|
|
done = json.loads(capsys.readouterr().out)
|
|
assert done["outcome"] == "passed" and done["claims"]["status"] == "unsupported"
|
|
main(["experiments", "recovery", "verify", done["id"], "--run", str(env["run"])])
|
|
assert json.loads(capsys.readouterr().out)["attempt"] == 2
|
|
main(["experiments", "recovery", "show", done["id"], "--run", str(env["run"])])
|
|
assert json.loads(capsys.readouterr().out)["state"]["assessments"] == 2
|
|
bad = tmp_path / "bad.json"
|
|
bad.write_text(json.dumps({**make_spec(env), "kind": "other"}))
|
|
with pytest.raises(SystemExit) as refused:
|
|
main(["experiments", "recovery", "run", str(bad), "--run", str(env["run"])])
|
|
assert refused.value.code == 4 and "kind" in capsys.readouterr().err
|