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

455 lines
22 KiB
Python

"""Controlled intervention harness: four arms, restoration, refusals and dossier labels.
The fixture model extends the planted model of ``tests/test_labeled_activations.py``
with a yes/no head. In the planted variant the head answers "yes" when channel 2
of layer 0 exceeds 2, so steering the dossier's ``channel:layer.0:2`` estimate flips
control rows to "yes" while a norm/rank-matched random direction (orthogonal to
channel 2) cannot. In the null variant the head ignores the residual stream.
"""
from __future__ import annotations
import copy
import json
from contextlib import contextmanager
from pathlib import Path
from types import SimpleNamespace
import pytest
import torch
from torch import nn
from obliteratus.analysis import dossier_interventions as di
from obliteratus.analysis import dossier_readiness
from obliteratus.analysis.target_dossier import build_dossier, read_dossier, write_dossier
from obliteratus.concepts.pack import ConceptPack
from obliteratus.concepts.restoration import hook_counts
from obliteratus.experiments import ObservationCancelled, require_unmodified
from obliteratus.experiments.discover import run_discover
from obliteratus.experiments.intervention_checks import InterventionRefused, random_control
from obliteratus.experiments.intervention_ops import apply_operation
from obliteratus.experiments.intervention_runner import (
INTERVENTION_DIR, RestorationFailed, run_intervention,
)
from obliteratus.experiments.spec import SpecError
from obliteratus.experiments.store import ObservationStore
from tests.test_candidate_discovery import channel_two
from tests.test_labeled_activations import WIDTH, PlantedModel, RunningSum, WordTokenizer, build_pack
from tests.test_target_dossier import FAST, HYPOTHESIS, LEXICAL, REPORT
pytestmark = pytest.mark.cpu
DECLARED = {"useful_effect_threshold": 0.5, "effect_direction": "increase",
"collateral_margin": 0.2, "decision_coefficient": 6.0, "target_task": "flip"}
CONTROLLED = "controlled intervention result"
VALIDATED = "independently validated predictor"
class AnswerTokenizer(WordTokenizer):
def __init__(self):
super().__init__("local/answers")
size = len(self.vocab)
self.vocab.update(yes=size, no=size + 1)
def decode(self, ids, skip_special_tokens=True):
inverse = {v: k for k, v in self.vocab.items()}
return " ".join(inverse[int(i)] for i in ids if int(i) > 2)
class AnswerModel(PlantedModel):
"""Planted layer-0 geometry with a yes/no head; counts forward calls."""
def __init__(self, reads_channel=True):
super().__init__(planted=channel_two())
vocab = AnswerTokenizer().vocab
old = self.embed.weight.detach().clone()
self.embed = nn.Embedding(len(vocab), WIDTH)
self.head = nn.Linear(WIDTH, len(vocab))
with torch.no_grad():
self.embed.weight.zero_()
self.embed.weight[:len(old)] = old
self.head.weight.zero_()
self.head.bias.fill_(-100.0)
self.head.bias[vocab["no"]] = 0.0
if reads_channel:
self.head.weight[vocab["yes"], 2] = 4.0
self.head.bias[vocab["yes"]] = -8.0
else:
self.head.bias[vocab["yes"]] = -1.0
self.forward_calls = 0
self.eval()
def forward(self, input_ids, attention_mask=None):
self.forward_calls += 1
return super().forward(input_ids, attention_mask)
def generate(self, input_ids, attention_mask=None, max_new_tokens=1, **_options):
ids = input_ids
for _ in range(max_new_tokens):
ids = torch.cat([ids, self(ids).logits[:, -1].argmax(-1, keepdim=True)], 1)
return ids
def _answers(layout):
for path, rows in layout.items():
for row in rows:
row["answer"] = "yes"
# Controlled interventions must be evaluated on rows independent of both
# discovery (fit) and confirmation (select): move the upper half of each
# set's select rows to calibrate. 44 is even, so no family pair is split.
index = int("".join(c for c in row["id"] if c.isdigit()) or -1)
if not path.startswith("battery/") and row["role"] == "select" and index >= 44:
row["role"] = "calibrate"
def _run(root, pack, name, model, model_id):
run = root / name
run_discover(pack, model, AnswerTokenizer(), 0, run / "candidates", model_id=model_id,
options=FAST)
dossier = build_dossier("alpha", run, hypothesis={**HYPOTHESIS, **DECLARED},
discovery=REPORT, estimates=["channel:layer.0:2"],
confound_resolutions=[LEXICAL])
write_dossier(dossier, run)
return run
def build_env(root):
"""Pack, planted and null models with a ready dossier each under ``root``."""
pack = build_pack(root / "pack", _answers)
pack.location = root / "pack"
planted, null = AnswerModel(), AnswerModel(reads_channel=False)
return {"root": root, "pack": pack, "planted": planted, "null": null,
"planted_run": _run(root, pack, "planted", planted, "local/answers"),
"null_run": _run(root, pack, "null", null, "fixture/answers-null")}
@pytest.fixture(scope="module")
def env(tmp_path_factory):
return build_env(tmp_path_factory.mktemp("interventions"))
def make_spec(pack, **changes):
spec = {"schema_version": "1", "kind": "controlled_intervention",
"dossier": "dossiers/alpha.json", "estimate": "channel:layer.0:2",
"model": {"id": "local/answers", "dtype": "float32", "model_class": "fixture"},
"dataset": {"pack": str(pack.location), "revision": pack.dataset().revision,
"role": "calibrate"},
"tasks": [{"name": "flip", "scorer": {"id": "exact_answer", "version": 1},
"target_field": "answer", "path": "controls/neutral.jsonl"},
{"name": "keep", "scorer": {"id": "exact_answer", "version": 1},
"target_field": "answer", "path": "positive.jsonl"}],
"target_task": "flip", "decoding": {"max_new_tokens": 1},
"operation": {"kind": "steer", "site": "layer.0", "token_scope": "last_token",
"units": "raw", "coefficients": [2.0, 6.0],
"decision_coefficient": 6.0},
"control": {"seed": 7}}
for key, value in changes.items():
spec[key] = {**spec[key], **value} if isinstance(value, dict) and key in spec else value
return spec
def _run_spec(env, spec, model=None, run=None, **options):
model = env["planted"] if model is None else model
factory = lambda section: SimpleNamespace(model=model, tokenizer=AnswerTokenizer()) # noqa: E731
return run_intervention(spec, run or env["planted_run"], model_factory=factory,
pack_resolver=ConceptPack.load, **options)
def _history(run, spec):
from obliteratus.experiments.intervention_spec import load_intervention_spec
from obliteratus.experiments.spec import spec_digest
record = ObservationStore(run / INTERVENTION_DIR).get(spec_digest(load_intervention_spec(spec)))
return record.history()
def test_planted_effect_beats_matched_control_and_upgrades_only_with_fixture_allowance(env):
from jsonschema import Draft202012Validator
from obliteratus.experiments.intervention_spec import schema as spec_schema
spec = make_spec(env["pack"])
Draft202012Validator(spec_schema()).validate(spec)
result = _run_spec(env, spec)
Draft202012Validator(di.result_schema()).validate(
{k: v for k, v in result.items() if k != "location"})
assert [(a["arm"], a["coefficient"]) for a in result["arms"]] == [
("baseline", None), ("intended", 2.0), ("random_control", 2.0), ("intended", 6.0),
("random_control", 6.0), ("replay", None)]
assert all(a["restoration"]["restored"] for a in result["arms"])
replay = result["arms"][-1]
assert replay["identical_to_baseline"] is True and replay["scorecard"]["status"] == "PASS"
hook = result["arms"][3]["hook"]
assert hook["site"] == "layer.0" and hook["module"] == "layers.0"
assert (hook["token_scope"], hook["units"], hook["operation"]) == ("last_token", "raw", "steer")
assert result["arms"][4]["hook"]["direction"] == "random"
control = result["control"]
assert abs(control["cosine_with_intended"]) < 1e-12 and control["norm"] == pytest.approx(1.0)
verdict = result["verdict"]
assert verdict["outcome"] == "supported", verdict["reasons"]
endpoint = verdict["endpoint"]
assert endpoint["difference"] >= 0.5 and endpoint["p_value"] < 0.05
assert endpoint["control_rate"] == endpoint["baseline_rate"] < 0.2
assert verdict["collateral"]["statuses"] == {"keep": "PASS", "flip:invalid_output": "PASS"}
assert [d["role"].startswith("decision") for d in result["dose_ladder"]] == [False, True]
assert result["evidence_scope"]["scope"] == di.SOFTWARE_CONTRACT
assert result["independence"]["basis"] == "disjoint roles of one dataset revision"
assert result["independence"]["overlaps_confirmation"] is False
assert hook_counts(env["planted"]) == [0] * len(hook_counts(env["planted"]))
# A completed spec is returned, not rerun.
calls = env["planted"].forward_calls
assert _run_spec(env, spec)["attempt"] == result["attempt"]
assert env["planted"].forward_calls == calls
dossier_path = env["planted_run"] / "dossiers" / "alpha.json"
attached = di.attach_result(dossier_path, result["location"]["result"])
assert attached["outcome"] == "supported" and attached["evidence_scope"] == di.SOFTWARE_CONTRACT
assert attached["evidence_label"] == {"computed": VALIDATED, "claimed": None}
dossier, base = read_dossier(dossier_path)
[entry] = dossier["interventions"]
assert entry["result"]["pointer"] == "/verdict"
assert entry["result"]["path"].startswith("interventions/")
default = dossier_readiness.assess(dossier, base)
assert default["label"] == VALIDATED
assert "software-contract" in default["interventions"][0]["reasons"][-1]
allowed = dossier_readiness.assess(dossier, base, allow_fixture_results=True)
assert allowed["label"] == CONTROLLED and allowed["interventions"][0]["grants_label"]
with pytest.raises(ValueError, match="already attached"):
di.attach_result(dossier_path, result["location"]["result"])
stored = json.loads(Path(result["location"]["result"]).read_text())
tampered = copy.deepcopy(stored)
tampered["verdict"]["endpoint"]["difference"] = 0.99
with pytest.raises(ValueError, match="does not follow"):
di.verify_result(dossier, tampered)
moved = {**dossier, "hypothesis": {**dossier["hypothesis"], "useful_effect_threshold": 0.1}}
with pytest.raises(ValueError, match="changed after"):
di.verify_result(moved, stored)
late = {**dossier, "created_at": "2999-01-01T00:00:00+00:00"}
with pytest.raises(ValueError, match="different dossier"):
di.verify_result(late, stored)
early = copy.deepcopy(stored)
early["started_at"] = "2000-01-01T00:00:00+00:00"
with pytest.raises(ValueError, match="before the dossier"):
di.verify_result(dossier, early)
# A changed result file makes the dossier stale; its label falls back.
path = env["planted_run"] / entry["result"]["path"]
original = path.read_text()
path.write_text(original.replace('"state": "complete"', '"state": "complete" ', 1))
try:
assert dossier_readiness.check(dossier, base)["stale"] is True
finally:
path.write_text(original)
assert dossier_readiness.check(dossier, base, allow_fixture_results=True)["label"] == CONTROLLED
def test_null_effect_is_recorded_negative_and_never_upgrades(env):
spec = make_spec(env["pack"], model={"id": "fixture/answers-null"},
operation={"coefficients": [6.0]})
result = _run_spec(env, spec, model=env["null"], run=env["null_run"])
assert result["verdict"]["outcome"] == "not_supported"
assert "below the useful-effect threshold" in result["verdict"]["reasons"][0]
assert result["evidence_scope"] == {"scope": di.SOFTWARE_CONTRACT,
"reason": "spec declares model.model_class fixture"}
dossier_path = env["null_run"] / "dossiers" / "alpha.json"
attached = di.attach_result(dossier_path, result["location"]["result"])
assert attached["outcome"] == "not_supported"
dossier, base = read_dossier(dossier_path)
allowed = dossier_readiness.assess(dossier, base, allow_fixture_results=True)
assert allowed["label"] == VALIDATED
assert allowed["interventions"][0]["grants_label"] is False
with pytest.raises(ValueError, match="exceeds the computed level"):
from obliteratus.analysis.evidence_levels import check_claim
check_claim(CONTROLLED, allowed["label"], intervention_results=dossier["interventions"])
def test_injected_exception_and_cancellation_remove_hooks_and_record_the_attempt(env):
model = env["planted"]
@contextmanager
def exploding(model_, layer, vector, coefficient, operation):
def boom(*_args):
raise RuntimeError("injected failure")
with apply_operation(model_, layer, vector, coefficient, operation):
handle = layer.register_forward_hook(boom)
try:
yield
finally:
handle.remove()
spec = make_spec(env["pack"], control={"seed": 11})
with pytest.raises(RuntimeError, match="injected failure"):
_run_spec(env, spec, operations={"steer": exploding})
require_unmodified(model)
last = _history(env["planted_run"], spec)[-1]
assert last["state"] == "failed" and last["error_type"] == "RuntimeError"
assert last["events"][-1]["hooks_removed"] is True
assert last["events"][-1]["forward_passes"] is True
seen, calls = {}, {"n": 0}
def cancel_inside_intended_arm():
calls["n"] += 1
if calls["n"] == 130:
seen["hooks"] = sum(hook_counts(model))
raise ObservationCancelled("stop")
cancelled = make_spec(env["pack"], control={"seed": 12})
with pytest.raises(ObservationCancelled):
_run_spec(env, cancelled, check_cancelled=cancel_inside_intended_arm)
assert seen["hooks"] == 1, "the intended arm's hook was active when cancelled"
require_unmodified(model)
last = _history(env["planted_run"], cancelled)[-1]
assert last["state"] == "cancelled" and last["events"][-1]["hooks_removed"] is True
def test_leaky_operations_fail_restoration(env):
model = copy.deepcopy(env["planted"])
@contextmanager
def edits_weight(model_, layer, vector, coefficient, operation):
with torch.no_grad():
model_.head.bias[0] += 1.0
yield
leaked = []
@contextmanager
def leaves_hook(model_, layer, vector, coefficient, operation):
leaked.append(layer.register_forward_hook(lambda *_args: None))
yield
spec = make_spec(env["pack"], operation={"coefficients": [6.0]}, control={"seed": 21})
with pytest.raises(RestorationFailed, match="weights_restored"):
_run_spec(env, spec, model=model, operations={"steer": edits_weight}, rerun=True)
event = _history(env["planted_run"], spec)[-1]["events"][-1]
assert event["restoration"]["arm"] == "intended" and event["restoration"]["weights_restored"] is False
model = copy.deepcopy(env["planted"])
with pytest.raises(RestorationFailed, match="hooks_restored"):
_run_spec(env, spec, model=model, operations={"steer": leaves_hook}, rerun=True)
event = _history(env["planted_run"], spec)[-1]["events"][-1]
assert event["restoration"]["hooks_restored"] is False and event["hooks_removed"] is False
assert event["restoration"]["outputs_restored"] is True, "a no-op hook keeps outputs"
leaked[0].remove()
def _refused(env, spec, match, model=None, run=None, error=InterventionRefused):
model = env["planted"] if model is None else model
calls = model.forward_calls
with pytest.raises(error, match=match):
_run_spec(env, spec, model=model, run=run, rerun=True)
assert model.forward_calls == calls, "refused after a forward pass"
def test_incompatible_geometry_is_refused_before_any_forward_pass(env):
pack = env["pack"]
_refused(env, make_spec(pack, operation={"site": "layer.1"}), "differs from the estimate site")
_refused(env, make_spec(pack, model={"dtype": "float16"}), "dtype float16 differs")
_refused(env, make_spec(pack, model={"id": "local/other"}), "not the dossier's model")
_refused(env, make_spec(pack, model={"device": "cuda:0"}), "Model refused")
wide = copy.deepcopy(env["planted"])
wide.config.hidden_size = WIDTH + 2
_refused(env, make_spec(pack), "width 10 differs from the model hidden size 12", model=wide)
class OtherSum(RunningSum):
pass
swapped = copy.deepcopy(env["planted"])
swapped.layers[0] = OtherSum()
_refused(env, make_spec(pack), "not the module the coordinate", model=swapped)
edited = copy.deepcopy(env["planted"])
with torch.no_grad():
edited.layers[1].weight[3, 3] += 1e-3
_refused(env, make_spec(pack), "weight fingerprint", model=edited)
_refused(env, make_spec(pack, estimate="channel:layer.0:0"), "not a ready estimate")
run = env["planted_run"]
plain = build_dossier("plain", run, hypothesis=HYPOTHESIS, discovery=REPORT,
estimates=["channel:layer.0:2"], confound_resolutions=[LEXICAL])
write_dossier(plain, run, overwrite=True)
_refused(env, make_spec(pack, dossier="dossiers/plain.json"), "does not pre-declare")
last = _history(run, make_spec(pack, dossier="dossiers/plain.json"))[-1]
assert last["state"] == "failed" and last["events"][-1]["forward_passes"] is False
for changes, match in (({"target_task": "nope"}, "not a declared task"),
({"operation": {"decision_coefficient": 3.0}}, "decision_coefficient"),
({"operation": {"coefficients": [6.0, 6.0]}}, "unique"),
({"operation": {"kind": "ablate"}}, "unsupported value"),
({"tasks": make_spec(pack)["tasks"][:1]}, "collateral task")):
_refused(env, make_spec(pack, **changes), match, error=SpecError)
def test_evaluation_rows_overlapping_discovery_are_refused(env):
run = env["planted_run"]
dossier, _ = read_dossier(run / "dossiers" / "alpha.json")
overlap = copy.deepcopy(dossier)
overlap["name"] = "overlap"
overlap["interventions"] = []
overlap["datasets"][0]["roles"] = ["fit", "calibrate"]
write_dossier(overlap, run, overwrite=True)
_refused(env, make_spec(env["pack"], dossier="dossiers/overlap.json"), "not independent")
# A different dataset revision passes the role check, but the captured discovery
# rows (fit and select) share tokenized inputs with the evaluation rows.
renamed = copy.deepcopy(overlap)
renamed["name"] = "renamed"
renamed["datasets"][0]["roles"] = ["fit", "select"]
renamed["datasets"][0]["dataset_revision"] = "sha256:" + "e" * 64
write_dossier(renamed, run, overwrite=True)
_refused(env, make_spec(env["pack"], dossier="dossiers/renamed.json",
dataset={"role": "select"}),
"same tokenized input as discovery rows")
def test_ablation_dose_and_scope_rules(env):
run = env["planted_run"]
dossier, _ = read_dossier(run / "dossiers" / "alpha.json")
ablation = copy.deepcopy(dossier)
ablation["name"] = "ablation"
ablation["interventions"] = []
ablation["hypothesis"]["decision_coefficient"] = 1.0
write_dossier(ablation, run, overwrite=True)
spec = make_spec(env["pack"], dossier="dossiers/ablation.json",
operation={"kind": "ablate", "units": "projection_fraction",
"coefficients": [1.0], "decision_coefficient": 1.0,
"token_scope": "all_tokens"})
result = _run_spec(env, spec)
assert result["arms"][1]["hook"]["operation"] == "ablate"
assert result["arms"][1]["hook"]["position"] == "all"
assert result["verdict"]["outcome"] in {"not_supported", "inconclusive"}
assert all(a["restoration"]["restored"] for a in result["arms"])
vector = torch.zeros(4)
vector[1] = 2.0
control, info = random_control(vector, 3)
assert control[1] == 0 and info["norm"] == pytest.approx(2.0) and info["rank"] == 1
assert torch.equal(random_control(vector, 3)[0], control)
assert di.evidence_scope("pretrained", ["org/model"])["scope"] == di.MODEL_FINDING
assert di.evidence_scope("pretrained", ["org/model", "local/x"])["scope"] == di.SOFTWARE_CONTRACT
with pytest.raises(ValueError, match="pre-declare"):
di.declared_thresholds(HYPOTHESIS)
with pytest.raises(ValueError, match="useful_effect_threshold"):
build_dossier("bad", env["planted_run"], hypothesis={**HYPOTHESIS,
"useful_effect_threshold": 0},
discovery=REPORT, estimates=["channel:layer.0:2"])
def test_schemas_are_shipped_and_valid():
from jsonschema import Draft202012Validator
from obliteratus.experiments.intervention_spec import SCHEMA_PATH, schema
Draft202012Validator.check_schema(schema())
Draft202012Validator.check_schema(di.result_schema())
for path in (SCHEMA_PATH, di.RESULT_SCHEMA_PATH):
assert path.parent.name == "schema" and path.suffix == ".json"
def test_decision_dose_and_target_task_must_be_preregistered(env):
"""The comparison that decides the result is fixed in the dossier before the run."""
_refused(env, make_spec(env["pack"], operation={"coefficients": [2.0, 6.0, 9.0],
"decision_coefficient": 9.0}),
"pre-registered decision_coefficient=6.0")
_refused(env, make_spec(env["pack"], target_task="keep"), "pre-registered target_task='flip'")
run = env["planted_run"]
dossier, _ = read_dossier(run / "dossiers" / "alpha.json")
unregistered = copy.deepcopy(dossier)
unregistered["name"] = "unregistered"
unregistered["interventions"] = []
del unregistered["hypothesis"]["decision_coefficient"]
write_dossier(unregistered, run, overwrite=True)
_refused(env, make_spec(env["pack"], dossier="dossiers/unregistered.json"),
"does not pre-register decision_coefficient")
def test_confirmation_rows_cannot_be_reused_for_the_causal_test(env):
"""The estimate was selected partly on its confirmation rows; they are refused."""
spec = make_spec(env["pack"], dataset={"role": "select"})
_refused(env, spec, "confirmation data")