Files
OBLITERATUS/tests/test_causal_map.py
Joseph MaglyandClaude Opus 5.5 4a6938e63a fix(experiments): attach causal-map cells only through the map's correction
A map runs one controlled intervention per row, and each row result was
attachable on its own with `intervene attach`, which would let a single
cell be picked from the grid without the map's Holm correction.

- Map rows set an optional `family` (kind causal_map, map digest) in
  their intervention spec; the runner copies it into the result.
- `attach_result` refuses a result carrying a family unless the caller
  supplies the map reference, cell and Holm-adjusted p; `map attach`
  does. The family is stored in the dossier entry.
- Every assessment re-reads the map, checks its digest, and confirms the
  cell is still supported with the recorded adjusted p; an edited or
  missing family makes the attachment invalid.

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

420 lines
21 KiB
Python

"""Causal targeting maps: grid of controlled interventions, correction, resume, Explorer.
Fixtures reuse the controlled-intervention harness fixtures
(``tests/test_controlled_interventions.py``). The planted model mean-pools the
residual stream over positions before its yes/no head and answers "yes" only
when the pooled channel 2 exceeds 4. Steering the dossier's
``channel:layer.0:2`` estimate at every token by the decision dose (6) flips
control rows to "yes"; steering only the last token adds 6/L to the pooled
value and does not, and the descriptive dose (2) does not either. So exactly
one cell (layer.0, all_tokens, 6.0) carries the effect. The null model's head
reads every channel except 2, so the estimate never moves it (its random
controls can, by a seed-dependent amount).
"""
from __future__ import annotations
import copy
import json
from pathlib import Path
from types import SimpleNamespace
import pytest
import torch
from obliteratus.analysis import causal_map_cells as cm
from obliteratus.analysis import dossier_refs as refs
from obliteratus.analysis.causal_map_rank import rank_candidates
from obliteratus.concepts.pack import ConceptPack
from obliteratus.experiments import causal_map
from obliteratus.experiments.causal_map import MapRefused, attach_cell, load_map, run_map
from obliteratus.experiments.causal_map_spec import load_map_spec, map_identity
from obliteratus.experiments.spec import SpecError
from obliteratus.experiments.store import StoreError
from tests.test_controlled_interventions import (
AnswerModel, AnswerTokenizer, _answers, _run, make_spec,
)
from tests.test_labeled_activations import build_pack
pytestmark = pytest.mark.cpu
PLANTED = "layer.0|all_tokens|6.0"
YES = AnswerTokenizer().vocab["yes"]
class PooledModel(AnswerModel):
"""Planted geometry; the head reads the running mean of the residual stream."""
def __init__(self):
super().__init__()
with torch.no_grad():
self.head.bias[YES] = -16.0
def forward(self, input_ids, attention_mask=None):
self.forward_calls += 1
hidden = self.embed(input_ids)
for layer in self.layers:
hidden = layer(hidden)
steps = torch.arange(1, hidden.shape[1] + 1, dtype=hidden.dtype).view(1, -1, 1)
return SimpleNamespace(logits=self.head(hidden.cumsum(1) / steps))
class OtherChannelModel(AnswerModel):
"""Null: the head reads every channel except 2, so the estimate never moves it;
each seed's random control moves it by a different amount."""
def __init__(self):
super().__init__()
with torch.no_grad():
self.head.weight[YES].fill_(-2.0)
self.head.weight[YES, 2] = 0.0
self.head.bias[YES] = -8.0
@pytest.fixture(scope="module")
def env(tmp_path_factory):
root = tmp_path_factory.mktemp("maps")
pack = build_pack(root / "pack", _answers)
pack.location = root / "pack"
planted, null = PooledModel(), OtherChannelModel()
return {"pack": pack, "planted": planted, "null": null,
"planted_run": _run(root, pack, "pooled", planted, "local/answers-pooled"),
"null_run": _run(root, pack, "other", null, "local/answers-other")}
def map_spec(pack, model_id="local/answers-pooled", **changes):
base = make_spec(pack, model={"id": model_id})
spec = {k: base[k] for k in ("schema_version", "dossier", "estimate", "model", "dataset",
"tasks", "target_task", "decoding", "control")}
spec.update(kind="causal_targeting_map", operation={"kind": "steer", "units": "raw"},
grid={"sites": ["layer.0", "layer.1"],
"token_scopes": ["last_token", "all_tokens"], "doses": [2.0, 6.0]},
decision_coefficient=6.0)
spec.update(changes)
return spec
def _map(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_map(spec, run or env["planted_run"], model_factory=factory,
pack_resolver=ConceptPack.load, **options)
def _cells(document):
return {c["id"]: c for c in document["cells"]}
def test_planted_cell_is_the_only_supported_cell_after_correction(env):
from jsonschema import Draft202012Validator
from obliteratus.experiments.causal_map_spec import schema
Draft202012Validator.check_schema(schema())
spec = map_spec(env["pack"])
Draft202012Validator(schema()).validate(spec)
document = _map(env, spec)
cells = _cells(document)
assert len(cells) == 8 and document["supported"] == [PLANTED]
assert document["counts"] == {"descriptive": 2, "not_supported": 1, "supported": 1,
"untested": 4}
correction = document["correction"]
assert correction["m"] == 2 and correction["tested_cells"] == 4
assert correction["family"] == ["layer.0|last_token|6.0", PLANTED]
planted = cells[PLANTED]
target = planted["target"]
assert target["difference"] >= target["useful_effect_threshold"] == 0.5
assert target["p_adjusted"] == pytest.approx(2 * target["p_raw"]) and target["p_adjusted"] < 0.05
assert target["interval"][0] > 0 and target["interval"][0] <= target["difference"]
assert planted["collateral"]["passed"] and planted["restoration"]["passed"]
assert planted["independence"]["overlaps_confirmation"] is False
assert planted["evidence_scope"] == "software-contract" and planted["attachable"]
other = cells["layer.0|last_token|6.0"]
assert other["status"] == "not_supported" and other["target"]["difference"] == 0
for dose_two in ("layer.0|last_token|2.0", "layer.0|all_tokens|2.0"):
assert cells[dose_two]["status"] == "descriptive"
assert cells[dose_two]["target"]["p_adjusted"] is None
for cell_id, cell in cells.items():
if cell["site"] == "layer.1":
assert cell["status"] == "untested" and cell["label"] == cm.EXPLORATORY
assert cell["target"] is None and cell["comparison"] is None
assert "transferred coordinate" in cell["reason"] and not cell["attachable"]
continue
# Every tested cell links to a controlled comparison with restoration passed.
link = cell["comparison"]
path = env["planted_run"] / link["path"]
assert refs.file_sha256(path) == link["sha256"]
result = json.loads(path.read_text())
step = refs.resolve_pointer(result, link["pointer"])
assert step["coefficient"] == cell["dose"]
assert refs.resolve_pointer(result, link["control_arm"])["arm"] == "random_control"
assert all(a["restoration"]["restored"] for a in result["arms"])
assert result["operation"]["token_scope"] == cell["token_scope"]
generations = cell["failure_examples"]["generations"]
assert refs.file_sha256(env["planted_run"] / generations["path"]) == generations["sha256"]
assert cells["layer.0|last_token|6.0"]["failure_examples"]["count"] > 0
assert cells[PLANTED]["invalid_output_rate"]["flip"] == 0
assert load_map(document["location"]["map"])["supported"] == [PLANTED]
from obliteratus import ui_evidence_explorer as explorer
from obliteratus.ui_evidence_maps import MAP_COLUMNS
page = explorer.map_html({"run_dir": env["planted_run"]})
assert page.count("<table>") == 2 and page.count("<em>untested</em>") == 4
assert "exploratory (transferred coordinate)" in page and "#/dose_ladder/1" in page
untested_cells = [part for part in page.split("<td") if "untested</em>" in part]
assert all("background" not in part and "Δ" not in part for part in untested_cells)
[row] = explorer.map_rows({"run_dir": env["planted_run"]})
assert len(row) == len(MAP_COLUMNS) and row[2] == PLANTED and row[4] == 4
def test_null_model_has_no_supported_cell_across_seeds(env):
seen = []
for seed in (1, 2, 3):
spec = map_spec(env["pack"], "local/answers-other", control={"seed": seed},
grid={"sites": ["layer.0"], "token_scopes": ["last_token", "all_tokens"],
"doses": [2.0, 6.0]})
document = _map(env, spec, model=env["null"], run=env["null_run"])
assert document["supported"] == [] and document["correction"]["m"] == 2
for cell in document["cells"]:
assert cell["status"] != "supported"
assert cell["target"]["difference"] <= 0
if cell["role"] == "decision":
assert cell["target"]["p_adjusted"] >= cell["target"]["p_raw"]
seen.append(sorted(c["target"]["difference"] for c in document["cells"]))
assert len({tuple(s) for s in seen}) > 1, "the random controls differ between seeds"
def test_cap_resume_and_refusal_to_mix_specs(env):
spec = map_spec(env["pack"], control={"seed": 8},
grid={"sites": ["layer.0"], "token_scopes": ["last_token", "all_tokens"],
"doses": [2.0, 6.0]})
first = _map(env, spec, max_cells=3)
cells = _cells(first)
assert [cells[k]["status"] for k in ("layer.0|all_tokens|2.0", PLANTED)] == ["untested"] * 2
assert "cell cap reached (budget.max_cells=3)" in cells[PLANTED]["reason"]
assert cells[PLANTED]["target"] is None and first["supported"] == []
assert first["correction"]["m"] == 1
model = env["planted"]
calls = model.forward_calls
second = _map(env, spec, max_cells=4, resume_from=first["location"]["map"])
assert second["location"]["map"] == first["location"]["map"]
assert [r["reused"] for r in second["rows"]] == [True, False]
assert second["supported"] == [PLANTED] and model.forward_calls > calls
calls = model.forward_calls
third = _map(env, spec)
assert all(r["reused"] for r in third["rows"]) and model.forward_calls == calls
assert map_identity(load_map_spec(spec)) == third["spec_digest"]
other = map_spec(env["pack"], control={"seed": 9},
grid={"sites": ["layer.0"], "token_scopes": ["last_token"],
"doses": [6.0]})
with pytest.raises(MapRefused, match="different map spec"):
_map(env, other, resume_from=first["location"]["map"])
assert model.forward_calls == calls
map_path = Path(first["location"]["map"])
original = map_path.read_text()
tampered = json.loads(original)
tampered["rows"][0]["row_spec_digest"] = "sha256:" + "0" * 64
map_path.write_text(json.dumps(tampered))
try:
with pytest.raises(MapRefused, match="different row spec"):
_map(env, spec)
tampered = json.loads(original)
tampered["spec_digest"] = "sha256:" + "1" * 64
map_path.write_text(json.dumps(tampered))
with pytest.raises(MapRefused, match="holds a different map"):
_map(env, spec)
result = env["planted_run"] / tampered["rows"][0]["result"]["path"]
map_path.write_text(original)
saved = result.read_text()
result.write_text(saved + " ")
try:
with pytest.raises(MapRefused, match="missing or changed"):
load_map(map_path)
finally:
result.write_text(saved)
finally:
map_path.write_text(original)
spec_file = map_path.parent / "spec.json"
stored = spec_file.read_text()
spec_file.write_text(stored.replace('"seed": 8', '"seed": 18'))
try:
with pytest.raises(StoreError, match="refusing to mix"):
_map(env, spec)
finally:
spec_file.write_text(stored)
assert model.forward_calls == calls
def test_refusals_are_untested_with_reasons_and_spec_rules(env):
spec = map_spec(env["pack"], target_task="keep",
grid={"sites": ["layer.0"], "token_scopes": ["last_token"], "doses": [6.0]})
document = _map(env, spec)
[cell] = document["cells"]
assert cell["status"] == "untested" and "refused before any forward pass" in cell["reason"]
assert "pre-registered target_task='flip'" in cell["reason"]
assert document["correction"]["m"] == 0 and document["supported"] == []
pack = env["pack"]
for changes, match in (({"decision_coefficient": 3.0}, "must contain the decision"),
({"grid": {"sites": ["layer.0", "layer.0"],
"token_scopes": ["last_token"], "doses": [6.0]}}, "unique"),
({"kind": "other"}, "unsupported value"),
({"tasks": map_spec(pack)["tasks"][:1]}, "collateral task")):
with pytest.raises(SpecError, match=match):
load_map_spec(map_spec(pack, **changes))
with pytest.raises(MapRefused, match="not in the dossier"):
_map(env, map_spec(pack, estimate="channel:layer.0:9"))
def test_holm_interval_and_corrected_statuses():
assert cm.holm([0.01, 0.04, 0.03]) == pytest.approx([0.03, 0.06, 0.06])
assert cm.holm([]) == []
assert cm.paired_interval(0, 0, 10) is None
low, high = cm.paired_interval(8, 0, 10)
assert 0 < low < high == pytest.approx(0.8)
# Two cells supported by the harness at raw p 0.03 and 0.04: Holm turns both
# inconclusive, and neither row result may be attached.
cells = []
for index, p in enumerate((0.03, 0.04, 0.5)):
cells.append({"id": f"c{index}", "role": "decision", "status": "x",
"site_relation": "estimate_site", "reason": None,
"harness_outcome": "supported" if p < 0.05 else "not_supported",
"target": {"p_raw": p, "p_adjusted": None}})
cells.append(cm.untested("layer.1", "last_token", 6.0, role="decision",
relation="transferred", reason="r"))
correction = cm.apply_correction(cells)
assert correction["m"] == 3 and correction["untested_cells"] == 1
assert [c["status"] for c in cells[:3]] == ["inconclusive", "inconclusive", "not_supported"]
assert [c["attachable"] for c in cells[:3]] == [False, False, True]
assert "Holm-adjusted" in cells[0]["reason"]
def test_rank_cli_and_attachment_rules(env, capsys, monkeypatch):
from obliteratus.cli import main
run = env["planted_run"]
paths = sorted(str(p) for p in run.glob("maps/*/map.json"))
maps = [load_map(p) for p in paths]
[planted] = [m for m in maps if len(m["cells"]) == 8]
ranked = rank_candidates(maps, dossier=run / "dossiers" / "alpha.json")
assert ranked["rows"][0]["status"] == "supported" and ranked["rows"][0]["rank"] == 1
assert ranked["family_size"] == sum(m["correction"]["m"] for m in maps)
assert ranked["untested_estimates"] == [] and "tested conditions" in ranked["note"]
assert all(r["p_adjusted_family"] >= r["p_raw"] for r in ranked["rows"])
foreign = copy.deepcopy(planted)
foreign["dossier"]["name"] = "other"
with pytest.raises(ValueError, match="different dossiers"):
rank_candidates([planted, foreign])
with pytest.raises(ValueError, match="No maps"):
rank_candidates([])
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_file = run / "map-spec.json"
spec_file.write_text(json.dumps(map_spec(env["pack"])))
main(["experiments", "map", "run", str(spec_file), "--run", str(run)])
out = json.loads(capsys.readouterr().out)
assert out["supported"] == [PLANTED] and len(out["cells"]) == 8
map_path = out["location"]["map"]
main(["experiments", "map", "show", map_path])
assert json.loads(capsys.readouterr().out)["supported"] == [PLANTED]
main(["experiments", "map", "rank", *paths])
assert json.loads(capsys.readouterr().out)["rows"][0]["cell"] == PLANTED
dossier = str(run / "dossiers" / "alpha.json")
for cell, match in (("layer.1|all_tokens|6.0", "transferred coordinate"),
("layer.0|all_tokens|2.0", "descriptive dose"),
("layer.9|all_tokens|6.0", "no cell")):
with pytest.raises(MapRefused, match=match):
attach_cell(map_path, cell, dossier)
with pytest.raises(SystemExit) as refused:
main(["experiments", "map", "attach", map_path, "--cell", "layer.1|last_token|6.0",
"--dossier", dossier])
assert refused.value.code == 4 and "never be attached" in capsys.readouterr().err
before = json.loads(Path(dossier).read_text())["interventions"]
assert before == []
main(["experiments", "map", "attach", map_path, "--cell", PLANTED, "--dossier", dossier])
attached = json.loads(capsys.readouterr().out)
assert attached["outcome"] == "supported" and attached["evidence_scope"] == "software-contract"
entries = json.loads(Path(dossier).read_text())["interventions"]
assert [e["result"]["path"] for e in entries] == [_cells(planted)[PLANTED]["comparison"]["path"]]
assert causal_map.summary(planted)["supported"] == [PLANTED]
class LeakyModel(PooledModel):
"""Outputs drift with every forward pass (state outside the weights), so the
probe logits are never restored."""
def forward(self, input_ids, attention_mask=None):
out = super().forward(input_ids, attention_mask)
return SimpleNamespace(logits=out.logits + 1e-3 * self.forward_calls)
def test_interrupted_and_unrestored_rows_are_untested_then_resumable(env):
from obliteratus.experiments import ObservationCancelled
spec = map_spec(env["pack"], control={"seed": 10},
grid={"sites": ["layer.0"], "token_scopes": ["all_tokens"], "doses": [6.0]})
def cancel():
raise ObservationCancelled("stop")
with pytest.raises(ObservationCancelled):
_map(env, spec, check_cancelled=cancel)
[path] = [p for p in env["planted_run"].glob("maps/*/map.json")
if json.loads(p.read_text())["spec"]["control"]["seed"] == 10]
[cell] = json.loads(path.read_text())["cells"]
assert cell["status"] == "untested" and cell["reason"].startswith("interrupted: Observation")
with pytest.raises(MapRefused, match="untested"):
attach_cell(path, PLANTED, env["planted_run"] / "dossiers" / "alpha.json")
assert _map(env, spec)["supported"] == [PLANTED]
leaky = map_spec(env["pack"], control={"seed": 11},
grid={"sites": ["layer.0"], "token_scopes": ["all_tokens"], "doses": [6.0]})
[cell] = _map(env, leaky, model=LeakyModel())["cells"]
assert cell["status"] == "untested", cell
assert cell["reason"].startswith("restoration failed (reload the model)")
assert "outputs_restored" in cell["reason"]
def test_map_row_results_reach_a_dossier_only_through_the_corrected_map_path(env):
"""A grid cell's result cannot be attached alone: that would skip the Holm correction."""
from obliteratus.analysis import dossier_interventions as verdicts
from obliteratus.analysis import dossier_readiness
from obliteratus.analysis.target_dossier import read_dossier, write_dossier
run = env["planted_run"]
[map_path] = [p for p in run.glob("maps/*/map.json") if len(load_map(p)["cells"]) == 8]
planted = load_map(map_path)
cell = _cells(planted)[PLANTED]
result_path = refs.resolve_path(cell["comparison"]["path"], run)
result = json.loads(Path(result_path).read_text())
assert result["family"] == {"kind": "causal_map", "map_digest": planted["spec_digest"]}
# Results are bound to the dossier they ran against, so clear alpha's
# attachments for this test and restore them afterwards.
target = run / "dossiers" / "alpha.json"
saved = target.read_bytes()
original, _ = read_dossier(target)
fresh = copy.deepcopy(original)
fresh["interventions"] = []
write_dossier(fresh, run, overwrite=True)
try:
with pytest.raises(ValueError, match="skip the map's multiple-comparison correction"):
verdicts.attach_result(target, result_path)
attach_cell(map_path, PLANTED, target)
stored, _ = read_dossier(target)
finally:
target.write_bytes(saved)
[entry] = stored["interventions"]
assert entry["family"]["cell"] == PLANTED
assert entry["family"]["p_adjusted"] == cell["target"]["p_adjusted"] < 0.05
assessed = dossier_readiness.assess(stored, run, allow_fixture_results=True)
assert [a["outcome"] for a in assessed["interventions"]] == ["supported"]
# Editing the recorded corrected p (or dropping the family) invalidates the attachment.
tampered = copy.deepcopy(stored)
tampered["interventions"][0]["family"]["p_adjusted"] = 0.0
assert [a["outcome"] for a in dossier_readiness.assess(
tampered, run, allow_fixture_results=True)["interventions"]] == ["invalid"]
dropped = copy.deepcopy(stored)
del dropped["interventions"][0]["family"]
assert [a["outcome"] for a in dossier_readiness.assess(
dropped, run, allow_fixture_results=True)["interventions"]] == ["invalid"]