Files
OBLITERATUS/tests/test_intervention_patching_cli.py
T
Joseph MaglyandClaude Opus 5.5 878796adb2 feat(experiments): controlled intervention harness with matched controls and restoration checks
Start milestone 3 (F12): run a temporary intervention on a ready
dossier estimate and record the result as the only route to a
"controlled intervention result" label.

- Versioned intervention spec (own schema; baseline observation specs
  unchanged) for steering or ablating a channel estimate at its site
  through the existing hook manager. Four arms on the same examples:
  baseline, intended, a seeded norm- and rank-matched random control
  orthogonal to the target, and a fresh-baseline replay that must
  reproduce the baseline exactly. Each arm records site, module, hook,
  token scope, units, dose and ordering.
- Dose ladders with paired scorecards against the baseline. The deciding
  dose and target task must equal the values pre-registered in the
  dossier hypothesis, alongside its effect threshold, direction and
  collateral margin.
- Restoration (parameter/buffer digest, hook counts, probe logits) is
  checked after every arm through a helper shared with pack evaluation.
  Exceptions and cancellation remove hooks and leave a recorded attempt.
- Refused before any forward pass: stale or unready dossiers, geometry
  mismatches (site, width, model, dtype, fingerprint, tokenizer,
  readout, device), evaluation rows overlapping discovery inputs, and
  evaluation on the dossier's confirmation rows.
- A minimal last-token activation-patching experiment with an explicit
  corruption strategy and logit-difference metric (descriptive only).
- Dossiers attach results by reference and recompute the verdict from
  per-example records on every assessment. The label is granted only if
  the intended arm beats the control by the threshold (one-sided exact
  McNemar), collateral endpoints pass and restoration held. Fixture or
  local models are software-contract results that never upgrade a label
  by default.
- `obliteratus experiments intervene` and a Python API.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 18:09:42 -05:00

96 lines
4.9 KiB
Python

"""Activation-patching experiments and the ``experiments intervene`` CLI.
Fixtures (planted and null yes/no models, their dossiers and specs) come from
``tests/test_controlled_interventions.py``.
"""
from __future__ import annotations
import copy
import json
from types import SimpleNamespace
import pytest
from obliteratus.analysis import dossier_interventions as di
from obliteratus.analysis.target_dossier import build_dossier, write_dossier
from obliteratus.cli import main
from obliteratus.concepts.pack import ConceptPack
from obliteratus.experiments import require_unmodified
from obliteratus.experiments.intervention_runner import INTERVENTION_DIR
from tests.test_controlled_interventions import (
CONTROLLED, DECLARED, VALIDATED, AnswerTokenizer, _refused, _run_spec, build_env, make_spec,
)
from tests.test_target_dossier import HYPOTHESIS, LEXICAL, REPORT
pytestmark = pytest.mark.cpu
@pytest.fixture(scope="module")
def env(tmp_path_factory):
return build_env(tmp_path_factory.mktemp("patching"))
def test_activation_patching_records_corruption_and_metric(env):
from jsonschema import Draft202012Validator
spec = make_spec(env["pack"], kind="activation_patching")
for key in ("target_task", "operation", "control", "decoding"):
spec.pop(key)
spec["patching"] = {"target_task": "flip", "source_task": "keep", "site": "layer.0",
"corruption": "source_swap", "scope": "estimate",
"metric": {"kind": "logit_difference", "target_token": "yes",
"foil_token": "no"}}
result = _run_spec(env, spec)
Draft202012Validator(di.result_schema()).validate(
{k: v for k, v in result.items() if k != "location"})
patching = result["patching"]
assert patching["summary"]["n"] == 30 and patching["summary"]["positive"] >= 27
assert patching["summary"]["mean_effect"] > 8
assert patching["records"][0]["source_example"] is not None
assert [a["arm"] for a in result["arms"]] == ["clean", "source_capture", "patched"]
assert all(a["restoration"]["restored"] for a in result["arms"])
require_unmodified(env["planted"])
mean = copy.deepcopy(spec)
mean["patching"].update(corruption="mean_ablation", scope="site")
summary = _run_spec(env, mean)["patching"]
assert summary["records"][0]["source_example"] is None and summary["summary"]["mean_effect"] > 8
with pytest.raises(ValueError, match="Only controlled-intervention"):
di.attach_result(env["planted_run"] / "dossiers" / "alpha.json",
result["location"]["result"])
bad = copy.deepcopy(spec)
bad["patching"]["metric"]["target_token"] = "yes no"
_refused(env, bad, "is 2 tokens")
def test_cli_run_attach_and_show(env, tmp_path, capsys, monkeypatch):
import shutil
run = tmp_path / "run"
shutil.copytree(env["planted_run"], run, ignore=shutil.ignore_patterns(INTERVENTION_DIR))
(run / "dossiers" / "alpha.json").unlink()
write_dossier(build_dossier("alpha", run, hypothesis={**HYPOTHESIS, **DECLARED},
discovery=REPORT, estimates=["channel:layer.0:2"],
confound_resolutions=[LEXICAL]), run)
monkeypatch.setattr("obliteratus.experiments.inputs.default_model_factory",
lambda section: SimpleNamespace(model=env["planted"],
tokenizer=AnswerTokenizer()))
monkeypatch.setattr("obliteratus.experiments.inputs.default_pack_resolver", ConceptPack.load)
spec = tmp_path / "spec.json"
spec.write_text(json.dumps(make_spec(env["pack"], operation={"coefficients": [6.0]})))
main(["experiments", "intervene", "run", str(spec), "--run", str(run)])
out = json.loads(capsys.readouterr().out)
assert out["verdict"]["outcome"] == "supported" and out["state"] == "complete"
dossier = str(run / "dossiers" / "alpha.json")
main(["experiments", "intervene", "attach", out["location"]["result"], "--dossier", dossier])
assert json.loads(capsys.readouterr().out)["evidence_label"]["computed"] == VALIDATED
main(["experiments", "dossier", "show", dossier])
shown = json.loads(capsys.readouterr().out)
assert shown["evidence_label"] == VALIDATED and shown["interventions"][0]["outcome"] == "supported"
main(["experiments", "dossier", "show", dossier, "--allow-fixture-results"])
assert json.loads(capsys.readouterr().out)["evidence_label"] == CONTROLLED
main(["experiments", "dossier", "check", dossier])
assert json.loads(capsys.readouterr().out)["interventions"][0]["grants_label"] is False
spec.write_text(json.dumps(make_spec(env["pack"], operation={"site": "layer.1"})))
with pytest.raises(SystemExit) as refused:
main(["experiments", "intervene", "run", str(spec), "--run", str(run)])
assert refused.value.code == 4 and "differs from the estimate site" in capsys.readouterr().err