Files
OBLITERATUS/tests/test_intervention_directions.py
Joseph MaglyandClaude Opus 5.5 5f0eafe14b feat(experiments): controlled interventions on direction and subspace estimates
Extend the controlled intervention harness beyond single channels.

- Fixed directions (rows of the pack's stored basis) and rank-k
  subspaces load from their direction artifact before the model does.
  The artifact must match the file, tensor and metadata digests that the
  dossier's source report recorded; discovery and subspace reports now
  record that binding, and reports without it are refused. Refitted
  difference-of-means directions stay refused.
- Subspace ablation removes the dose fraction of the projection onto the
  whole span. Steering a rank > 1 span needs one declared direction
  inside it (within 1e-4).
- Random controls are rank- and norm-matched: a seeded rank-k span
  orthogonal to the target span, and for a declared steering direction a
  rank-1 vector orthogonal to the whole span. Mismatched controls are
  refused before the arms run and at attachment.
- Each arm records target kind, rank, digests and orthogonalization.
  Evaluation rows on the artifact's own fit role and revision are
  refused. Channel specs and digests are unchanged.

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

376 lines
21 KiB
Python

"""Controlled interventions on fixed directions and subspaces with rank-matched controls.
The planted model of ``tests/test_controlled_interventions.py`` answers "yes" when
channel 2 of layer 0 exceeds 2. The direction artifact stores a rotated rank-2
basis spanning channels 2 and 5 (neither row is a standard-basis axis): row 0
is a fixed pack-basis direction found by candidate discovery, and the full span
is a labeled-subspace estimate. Both reports record the artifact's digests, and
the intervention must use exactly those bytes. Steering along row 0 flips
control rows; ablating the span silences positive rows; random controls
orthogonal to the target (rank 1 for the direction, a random rank-2 span for the
subspace, a rank-1 direction orthogonal to the whole span for a declared
steering direction) do neither.
"""
from __future__ import annotations
import copy
import json
import shutil
from types import SimpleNamespace
import pytest
import torch
from obliteratus.analysis import dossier_interventions as di
from obliteratus.analysis import dossier_readiness
from obliteratus.analysis.dossier_refs import file_sha256
from obliteratus.analysis.labeled_subspace import artifact_binding, labeled_subspace_report
from obliteratus.analysis.labeled_subspace import write_report as write_subspace
from obliteratus.analysis.target_dossier import build_dossier, read_dossier, write_dossier
from obliteratus.concepts.direction import ConceptDirection
from obliteratus.experiments.discover import run_discover
from obliteratus.experiments.intervention_checks import InterventionRefused
from obliteratus.experiments.intervention_controls import (
SPAN_ORTHOGONAL_RULE, check_matched, random_span, span_orthogonal,
)
from obliteratus.experiments.intervention_spec import load_intervention_spec
from obliteratus.experiments.intervention_targets import (
TargetRefused, _identity, bind, check_basis_independence, orthonormalize,
)
from obliteratus.experiments.spec import SpecError, spec_digest
from tests.test_controlled_interventions import (
CONTROLLED, DECLARED, VALIDATED, AnswerModel, AnswerTokenizer, _answers, _refused, _run_spec,
make_spec,
)
from tests.test_labeled_activations import build_pack, load
from tests.test_target_dossier import FAST, HYPOTHESIS, LEXICAL, REPORT
pytestmark = pytest.mark.cpu
EYE = torch.eye(10)
BASIS = torch.stack([(2 * EYE[2] + EYE[5]) / 5 ** 0.5, (EYE[2] - 2 * EYE[5]) / 5 ** 0.5])
ARTIFACT = "artifacts/direction.safetensors"
PACK0, DOM = "direction:layer.0:pack0", "direction:layer.0:dom"
SPAN = "subspace:layer.0:user/planted:rank2"
SPAN_DECLARED = {**DECLARED, "decision_coefficient": 1.0, "target_task": "keep",
"effect_direction": "decrease"}
ABLATE = {"kind": "ablate", "units": "projection_fraction", "coefficients": [0.5, 1.0],
"decision_coefficient": 1.0, "token_scope": "all_tokens"}
RESOLVED_FIT = {"flag": "heldout_fit_on_evaluation_data", "estimate": PACK0,
"reason": "fixture: the stored basis was fitted on the fit rows used for "
"discovery; confirmation on select rows is fit-free"}
SUBSPACE_CONFOUNDS = {"flag": "confound_baselines_not_assessed", "estimate": SPAN,
"reason": "fixture design: the class word is the only differing token"}
# Channel spec digests computed on the harness before fixed-direction support.
CHANNEL_DIGESTS = ("sha256:be7573cbc60eb2b8f211500f62f7907f6d2b3cb2e3811af34abd5139b53426a9",
"sha256:22450e68c5a2f9741ac89a86ad74cd86a7e3e192aa3856c7edd86c7af1b332b9")
def make_direction(pack, *, direction=None, basis=BASIS, **changes):
provenance = {"id": pack.manifest["id"], "sha256": pack.sha256,
"dataset_access": {"revision": pack.dataset().revision, "role": "fit",
"locked_test_acknowledged": False}}
options = {"pack_provenance": provenance, "concept_id": "user/planted",
"model_id": "local/answers", **changes}
return ConceptDirection(directions={0: basis[0] if direction is None else direction},
subspaces={0: basis}, extraction_layers=[0], intervention_layers=[0],
**options)
def save(direction, run, name):
path = run / "artifacts" / f"{name}.safetensors"
path.parent.mkdir(parents=True, exist_ok=True)
direction.save(path)
return {"path": path.relative_to(run).as_posix(), "sha256": file_sha256(path)}
def _subspace_report(labeled, direction, out, source=None):
report = labeled_subspace_report(labeled, direction, "layer.0", roles=["select"],
sets=["positive", "control:neutral", "control:other"],
direction_source=source, n_bootstrap=5, n_null=30)
return write_subspace(report, out)
@pytest.fixture(scope="module")
def env(tmp_path_factory):
root = tmp_path_factory.mktemp("directions")
pack = build_pack(root / "pack", _answers)
pack.location = root / "pack"
model, run = AnswerModel(), root / "run"
# Artifacts exist before discovery, so every report records their digests.
stored = make_direction(pack)
artifact = save(stored, run, "direction")
outside_direction = make_direction(pack, direction=EYE[3]) # same metadata, other vector
outside = save(outside_direction, run, "outside")
run_discover(pack, model, AnswerTokenizer(), 0, run / "candidates", model_id="local/answers",
options=FAST, direction=stored, direction_source=run / ARTIFACT)
labeled_dir = run / "candidates" / "labeled-layer.0-fit+select"
labeled = load(model, pack, labeled_dir, tokenizer=AnswerTokenizer(), model_id="local/answers")
subspace = _subspace_report(labeled, stored, run / "candidates", run / ARTIFACT)
# Further subspace reports beside copies of the same labeled set: one bound to the
# out-of-span artifact, one written before reports recorded artifact digests.
extra = {}
for name, direction, source in (("outside", outside_direction, run / outside["path"]),
("legacy", stored, None)):
shutil.copytree(labeled_dir, run / name / labeled_dir.name)
extra[name] = _subspace_report(labeled, direction, run / name, source)
span_options = {"estimates": [], "confound_resolutions": [SUBSPACE_CONFOUNDS]}
for name, hypothesis, options in (
("direction", DECLARED, {"estimates": [PACK0, DOM, "channel:layer.0:2"],
"confound_resolutions": [LEXICAL, RESOLVED_FIT]}),
("span", SPAN_DECLARED, {**span_options, "subspace": [subspace]}),
("steer", DECLARED, {**span_options, "subspace": [subspace]}),
("outside", DECLARED, {**span_options, "subspace": [extra["outside"]]}),
("legacy", SPAN_DECLARED, {**span_options, "subspace": [extra["legacy"]]})):
write_dossier(build_dossier(name, run, hypothesis={**HYPOTHESIS, **hypothesis},
discovery=REPORT, **options), run)
return {"pack": pack, "planted": model, "planted_run": run, "artifact": artifact,
"outside": outside, "subspace": subspace, "stored": stored}
def spec_for(env, dossier, estimate, **changes):
"""Spec on a fixture dossier; ``direction_artifact`` is optional (the report records it)."""
return make_spec(env["pack"], dossier=f"dossiers/{dossier}.json", estimate=estimate,
**changes)
def _span_spec(env, dossier="span", **changes):
return spec_for(env, dossier, SPAN, target_task="keep", operation=ABLATE, **changes)
def _schema_valid(result):
from jsonschema import Draft202012Validator
Draft202012Validator(di.result_schema()).validate(
{k: v for k, v in result.items() if k != "location"})
def test_fixture_estimates_are_ready(env):
for name, ready in (("direction", {PACK0, DOM, "channel:layer.0:2"}), ("span", {SPAN}),
("outside", {SPAN}), ("legacy", {SPAN})):
dossier, base = read_dossier(env["planted_run"] / "dossiers" / f"{name}.json")
readiness = dossier_readiness.assess(dossier, base)["readiness"]
assert readiness["ready"] and ready <= set(readiness["ready_estimates"])
report = json.loads((env["planted_run"] / REPORT).read_text())
binding = report["pack_direction"]["artifact"]
assert binding["file_sha256"] == binding["tensor_sha256"] == env["artifact"]["sha256"]
assert binding["path"] == (env["planted_run"] / ARTIFACT).resolve().as_posix()
assert json.loads(env["subspace"].read_text())["basis"]["artifact"] == binding
assert json.loads((env["planted_run"] / "legacy" / env["subspace"].name).read_text())[
"basis"]["artifact"] is None
with pytest.raises(ValueError, match="does not hold the directions"):
artifact_binding(env["planted_run"] / ARTIFACT,
make_direction(env["pack"], direction=EYE[3]))
def test_planted_direction_beats_rank_matched_control_and_attaches(env):
spec = spec_for(env, "direction", PACK0)
result = _run_spec(env, spec)
_schema_valid(result)
target = result["target"]
assert (target["kind"], target["rank"], target["row"]) == ("fixed_direction", 1, 0)
assert target["artifact"]["sha256"] == env["artifact"]["sha256"]
assert target["artifact"]["path"] == ARTIFACT
assert target["artifact"]["recorded"]["file_sha256"] == env["artifact"]["sha256"]
assert target["vector_sha256"] == result["operation"]["vector_sha256"]
assert result["estimate"]["type"] == "residual_direction"
assert "channel" not in result["geometry"] and result["geometry"]["rank"] == 1
control = result["control"]
assert control["rank"] == 1 and abs(control["cosine_with_intended"]) < 1e-6
assert control["norm"] == pytest.approx(control["intended_norm"])
intended, random = result["arms"][3], result["arms"][4]
assert intended["hook"]["rank"] == random["hook"]["rank"] == 1
assert intended["hook"]["vector_sha256"] == target["vector_sha256"]
assert result["independence"]["artifact_fit"]["receipt"]["role"] == "fit"
verdict = result["verdict"]
assert verdict["outcome"] == "supported", verdict["reasons"]
assert verdict["endpoint"]["difference"] >= 0.5
path = env["planted_run"] / "dossiers" / "direction.json"
attached = di.attach_result(path, result["location"]["result"])
assert attached["outcome"] == "supported"
assert attached["evidence_label"]["computed"] == VALIDATED
dossier, base = read_dossier(path)
allowed = dossier_readiness.assess(dossier, base, allow_fixture_results=True)
assert allowed["label"] == CONTROLLED and allowed["interventions"][0]["grants_label"]
def test_planted_span_ablation_beats_random_rank2_span_and_rank1_is_not_matched(env):
result = _run_spec(env, _span_spec(env, direction_artifact=env["artifact"]))
_schema_valid(result)
target, control = result["target"], result["control"]
assert (target["kind"], target["rank"], target["span_rank"]) == ("subspace", 2, 2)
ortho = target["orthogonalization"]
assert ortho["rank"] == 2 and ortho["max_abs_gram_error_after"] < 1e-12
assert ortho["span_preserved_error"] < 1e-12
assert control["rank"] == 2 and max(control["principal_cosines"]) < 1e-6
assert control["row_norms"] == pytest.approx(control["intended_row_norms"])
assert control["construction"].startswith("seeded standard-normal draw") and "k columns" in \
control["construction"]
hooks = [a["hook"] for a in result["arms"] if a["hook"]]
assert {h["rank"] for h in hooks} == {2}
assert hooks[1]["orthogonalization"] == control["orthogonalization"]
assert hooks[0]["orthogonalization"] == ortho
verdict = result["verdict"]
assert verdict["outcome"] == "supported", verdict["reasons"]
endpoint = verdict["endpoint"]
assert endpoint["control_rate"] == endpoint["baseline_rate"] > 0.8
assert endpoint["intended_rate"] < 0.2
path = env["planted_run"] / "dossiers" / "span.json"
attached = di.attach_result(path, result["location"]["result"])
assert attached["outcome"] == "supported"
dossier, base = read_dossier(path)
assert dossier_readiness.assess(dossier, base, allow_fixture_results=True)["label"] == CONTROLLED
# A rank-1 control is never accepted for a rank-2 span.
stored = json.loads((env["planted_run"] / dossier["interventions"][0]["result"]["path"])
.read_text())
rank_one = copy.deepcopy(stored)
rank_one["control"]["rank"] = 1
with pytest.raises(ValueError, match="not a matched control"):
di.verify_result(dossier, rank_one)
overlapping = copy.deepcopy(stored)
overlapping["control"]["cosine_with_intended"] = 0.3
with pytest.raises(ValueError, match="overlaps"):
di.verify_result(dossier, overlapping)
rows, _ = orthonormalize(BASIS)
with pytest.raises(InterventionRefused, match="rank 1"):
check_matched(rows, EYE[7], {"rank": 1})
def test_refitted_direction_is_refused_with_reason(env):
_refused(env, spec_for(env, "direction", DOM), "refitted inside every fold")
def test_tampered_or_missing_artifact_is_refused_before_any_forward_pass(env):
run = env["planted_run"]
original = run / ARTIFACT
tampered = run / "artifacts" / "tampered.safetensors"
shutil.copy(original, tampered)
shutil.copy(original.with_suffix(".json"), tampered.with_suffix(".json"))
data = bytearray(tampered.read_bytes())
data[-1] ^= 0x01
tampered.write_bytes(bytes(data))
declared = {"path": "artifacts/tampered.safetensors", "sha256": env["artifact"]["sha256"]}
_refused(env, spec_for(env, "direction", PACK0, direction_artifact=declared), "digest mismatch")
# Declaring the tampered bytes' own digest does not help: it is not the report's.
redeclared = {**declared, "sha256": file_sha256(tampered)}
_refused(env, spec_for(env, "direction", PACK0, direction_artifact=redeclared),
"differs from the digest recorded by the estimate's source report")
_refused(env, spec_for(env, "direction", PACK0,
direction_artifact={**declared, "path": "artifacts/none.safetensors"}),
"is missing")
_refused(env, spec_for(env, "direction", "channel:layer.0:2",
direction_artifact=env["artifact"]), "takes no direction_artifact")
def test_matching_metadata_with_other_vectors_and_prebinding_reports_are_refused(env):
run, pack = env["planted_run"], env["pack"]
# The out-of-span artifact has the planted artifact's metadata but another vector.
_refused(env, _span_spec(env, direction_artifact=env["outside"]),
"differs from the digest recorded")
wide = save(make_direction(pack, basis=torch.eye(12)[[2, 5]]), run, "wide")
_refused(env, spec_for(env, "direction", PACK0, direction_artifact=wide),
"differs from the digest recorded")
# Same tensor bytes, other sidecar metadata: caught by the recorded sidecar digest.
concept = save(make_direction(pack, concept_id="user/other"), run, "concept")
assert concept["sha256"] == env["artifact"]["sha256"]
_refused(env, spec_for(env, "direction", PACK0, direction_artifact=concept),
"metadata_sha256 [0-9a-f]{64} differs from the report's")
_refused(env, _span_spec(env, "legacy"), "predates artifact digests")
_refused(env, _span_spec(env, "legacy", direction_artifact=env["artifact"]),
"rerun discovery/subspace with --direction")
dossier = {"provenance": {"model": {"model_id": "local/answers", "dtype": "float32"}}}
for direction, match in ((make_direction(pack, model_id="org/other"), "extracted from org"),
(make_direction(pack, dtype="float16"), "dtype float16")):
with pytest.raises(TargetRefused, match=match):
_identity(direction, dossier)
with pytest.raises(TargetRefused, match="width 12 differs from the model hidden size 10"):
bind({"width": 12, "operated": torch.ones(12)}, 10, torch.zeros(1))
def test_subspace_steering_needs_one_declared_direction_in_the_span(env):
_refused(env, spec_for(env, "steer", SPAN), "name a single declared direction")
declared = spec_for(env, "steer", SPAN, operation={"steer_direction": {"source": "direction"}})
result = _run_spec(env, declared)
_schema_valid(result)
steer = result["target"]["steer_direction"]
assert steer["source"] == "direction" and steer["span_residual"] < 1e-6
assert result["target"]["rank"] == 1 and result["control"]["rank"] == 1
control = result["control"]
assert control["construction"] == SPAN_ORTHOGONAL_RULE
assert control["orthogonal_to"] == "estimate span (rank 2)"
assert len(control["span_row_cosines"]) == 2 and max(control["span_row_cosines"]) < 1e-6
assert control["norm"] == pytest.approx(control["intended_norm"])
assert result["verdict"]["outcome"] == "supported", result["verdict"]["reasons"]
# The out-of-span artifact's own subspace report records its digests (built before
# discovery), so only the span check can refuse its declared direction.
_refused(env, spec_for(env, "outside", SPAN,
operation={"steer_direction": {"source": "direction"}}),
"does not lie in the estimate's span")
_refused(env, spec_for(env, "steer", SPAN,
operation={"steer_direction": {"source": "basis_row", "index": 4}}),
"outside the rank-2 basis")
_refused(env, spec_for(env, "direction", PACK0,
operation={"steer_direction": {"source": "basis_row", "index": 0}}),
"Only subspace estimates")
for operation, match in (({**ABLATE, "steer_direction": {"source": "direction"}}, "steer only"),
({"steer_direction": {"source": "basis_row"}}, "needs an index")):
_refused(env, spec_for(env, "steer", SPAN, operation=operation), match, error=SpecError)
def test_patching_a_span_needs_site_scope(env):
spec = _span_spec(env)
for key in ("target_task", "operation", "control", "decoding"):
spec.pop(key)
spec.update(kind="activation_patching", patching={
"target_task": "keep", "source_task": "flip", "site": "layer.0",
"corruption": "mean_ablation", "scope": "estimate",
"metric": {"kind": "logit_difference", "target_token": "yes", "foil_token": "no"}})
_refused(env, spec, "use scope site")
def test_construction_helpers():
rows, details = orthonormalize(BASIS * torch.tensor([[2.0], [0.5]]))
assert torch.allclose(rows @ rows.T, torch.eye(2, dtype=torch.float64), atol=1e-12)
assert details["stored_row_norms"] == pytest.approx([2.0, 0.5])
for bad, match in ((torch.stack([BASIS[0], 2 * BASIS[0]]), "not linearly independent"),
(torch.stack([BASIS[0], torch.zeros(10)]), "zero row"),
(torch.full((2, 10), float("nan")), "finite")):
with pytest.raises(TargetRefused, match=match):
orthonormalize(bad)
scaled = BASIS * torch.tensor([[3.0], [1.0]])
control, info = random_span(scaled, 5)
assert torch.equal(random_span(scaled, 5)[0], control)
assert not torch.equal(random_span(scaled, 6)[0], control)
assert info["row_norms"] == pytest.approx([3.0, 1.0]) and info["rank"] == 2
assert float((control.double() @ scaled.double().T).abs().max()) < 1e-5
assert check_matched(scaled, control, info)
with pytest.raises(InterventionRefused, match="row norms"):
check_matched(scaled, control * 2, info)
with pytest.raises(InterventionRefused, match="overlaps"):
check_matched(scaled, torch.stack([scaled[0], control[1]]), info)
with pytest.raises(InterventionRefused, match="No room"):
random_span(torch.eye(3), 0)
span, _ = orthonormalize(BASIS)
steered, record = span_orthogonal(BASIS[0] * 3, span, 4)
assert float((span @ steered.double()).abs().max()) < 1e-6 and record["rank"] == 1
assert float(steered.norm()) == pytest.approx(3.0)
assert check_matched(BASIS[0] * 3, steered, record, span)
leaky = random_span(BASIS[:1], 4)[0][0] * 3 # orthogonal to row 0 only
with pytest.raises(InterventionRefused, match="overlaps the estimate span"):
check_matched(BASIS[0] * 3, leaky, {"rank": 1}, span)
with pytest.raises(InterventionRefused, match="No room"):
span_orthogonal(torch.ones(2), torch.eye(2, dtype=torch.float64), 0)
loaded = {"receipt": {"revision": "sha256:" + "a" * 64, "role": "calibrate"}}
with pytest.raises(TargetRefused, match="fitted on role 'calibrate'"):
check_basis_independence(loaded, {"revision": "sha256:" + "a" * 64, "role": "calibrate"})
assert check_basis_independence(loaded, {"revision": "sha256:" + "a" * 64, "role": "select"})
def test_channel_spec_digests_are_unchanged():
pack = SimpleNamespace(location="/fixed/pack",
dataset=lambda: SimpleNamespace(revision="sha256:" + "a" * 64))
ablate = {"kind": "ablate", "units": "projection_fraction", "coefficients": [1.0],
"decision_coefficient": 1.0}
found = tuple(spec_digest(load_intervention_spec(make_spec(pack, **changes)))
for changes in ({}, {"operation": ablate}))
assert found == CHANNEL_DIGESTS