Files
OBLITERATUS/tests/test_target_dossier.py
Joseph MaglyandClaude Opus 5.5 c94ce6dfd1 feat(analysis): target dossiers with evidence provenance and experiment readiness
Add versioned target dossiers (F11), the readiness gate between target
discovery and controlled causal experiments.

- Schema-validated JSON separating the hypothesis (intended behavior,
  bounded task, falsification test) from one or more estimates
  (channel, residual direction or subspace at a site). Evidence is
  stored by reference (path, sha256, JSON pointer into the source
  report), never as copied numbers. Provenance covers the pack, dataset
  revision, model identity with weight fingerprint, labeled sets and
  report digests; confounds, coverage, counterexamples, rejected
  alternatives and open questions are recorded.
- Evidence labels follow the roadmap vocabulary, defined once in
  evidence_levels.py. "Independently validated predictor" requires
  independent confirmation beating the shuffled-label null; claims may
  not exceed the computed label, and causal or intervention labels are
  refused without an intervention result.
- Confirmation must be independent: overlapping roles of one revision,
  legacy packs, another model, or a new revision reusing discovery
  inputs are refused.
- Readiness is computed with a checklist of what is missing; accepted
  confounds stay visible with their reasons. Missing geometry research
  never blocks.
- Changed or missing sources, a different weight fingerprint or model
  revision, or a different dataset revision mark a dossier stale and
  its evidence unsupported.
- `obliteratus experiments dossier build|show|check` and a Python API;
  the Explorer shows each dossier with its readiness checklist.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 14:01:45 -05:00

405 lines
22 KiB
Python

"""Target dossiers: planted and shuffled fixtures, label refusal, invalidation, references.
The fixtures reuse the planted model of ``tests/test_labeled_activations.py``:
channel 2 of layer 0 carries the positive set, so a discovery on role ``fit``
validated on role ``select`` is an independently validated predictor, while
the same pipeline on shuffled set labels must record an honest negative.
"""
from __future__ import annotations
import copy
import hashlib
import json
import shutil
from dataclasses import replace
from pathlib import Path
import pytest
import torch
from obliteratus import ui_evidence_explorer as explorer
from obliteratus.analysis import dossier_readiness, evidence_levels
from obliteratus.analysis.candidate_discovery import discover_candidates
from obliteratus.analysis.candidate_report import write_report
from obliteratus.analysis.capture_identity import weight_fingerprint
from obliteratus.analysis.dossier_refs import SourceChanged, Sources, pointer, resolve_pointer
from obliteratus.analysis.labeled_subspace import labeled_subspace_report
from obliteratus.analysis.labeled_subspace import write_report as write_subspace
from obliteratus.analysis.target_dossier import (
SCHEMA_PATH, build_dossier, check_independence, list_dossiers, read_dossier, validate,
write_dossier,
)
from obliteratus.cli import main
from obliteratus.concepts.validation import PackError
from obliteratus.experiments.discover import run_discover
from obliteratus.experiments.dossier_cli import check as cli_check
from tests.test_candidate_discovery import channel_two, pack_direction
from tests.test_labeled_activations import PlantedModel, WordTokenizer, build_pack, capture, load
pytestmark = pytest.mark.cpu
FAST = {"n_permutations": 40, "n_bootstrap": 60, "n_random": 20}
REPORT = "candidates/layer.0-binary-fit.json"
LABELED = "candidates/labeled-layer.0-fit+select"
HYPOTHESIS = {"intended_behavior": "positive-set membership is linearly readable at layer 0",
"bounded_task": "binary positive vs control rows of the planted pack",
"falsification_test": "held-out AUC on role select at or below the shuffled-label "
"null q95, or a confound baseline matching it"}
LEXICAL = {"flag": "lexical_baseline_matches",
"reason": "fixture design: the class word is the only token differing between sets"}
def _run(root, name, pack):
from tests.test_observation_runs import make_spec
run = root / name
run.mkdir()
(run / "spec.json").write_text(json.dumps(make_spec(root / "pack", capture=None)))
return run
@pytest.fixture(scope="module")
def runs(tmp_path_factory):
root = tmp_path_factory.mktemp("dossier")
pack = build_pack(root / "pack")
model = PlantedModel(planted=channel_two())
planted = _run(root, "planted", pack)
run_discover(pack, model, WordTokenizer(), 0, planted / "candidates",
model_id="local/planted", options=FAST)
labeled = load(model, pack, planted / LABELED)
select_only = discover_candidates(labeled, "layer.0", discovery_roles=["select"],
validation_roles=(), **FAST)
write_report(select_only, planted / "candidates")
shuffled_run = _run(root, "shuffled", pack)
capture(model, pack, shuffled_run / LABELED, roles=["fit", "select"])
labeled_s = load(model, pack, shuffled_run / LABELED)
order = torch.randperm(len(labeled_s.sets), generator=torch.Generator().manual_seed(3))
shuffled = replace(labeled_s, sets=tuple(labeled_s.sets[i] for i in order.tolist()))
write_report(discover_candidates(shuffled, "layer.0", **FAST), shuffled_run / "candidates")
return {"root": root, "pack": pack, "model": model, "planted": planted,
"shuffled": shuffled_run, "labeled": labeled}
def _build(run, name="alpha", **changes):
options = {"hypothesis": HYPOTHESIS, "discovery": REPORT,
"estimates": ["channel:layer.0:2", "direction:layer.0:dom"],
"confound_resolutions": [LEXICAL],
"rejected_alternatives": [{"id": "channel:layer.0:0",
"reason": "interval overlaps chance"}],
"unresolved_questions": ["Does the signal survive paraphrased class words?"],
**changes}
return build_dossier(name, run, **options)
def test_planted_signal_is_an_independently_validated_predictor_and_ready(runs):
from jsonschema import Draft202012Validator
dossier = _build(runs["planted"])
schema = json.loads(SCHEMA_PATH.read_text())
Draft202012Validator.check_schema(schema)
Draft202012Validator(schema).validate(dossier)
assert dossier["evidence_label"] == {"computed": "independently validated predictor",
"claimed": None}
readiness = dossier["readiness"]
assert readiness["ready"] is True and readiness["missing"] == []
assert "channel:layer.0:2" in readiness["ready_estimates"]
assert readiness["geometry"].startswith("Not required")
discovery, confirmation = dossier["datasets"]
assert discovery["roles"] == ["fit"] and confirmation["roles"] == ["select"]
assert confirmation["independence"] == "disjoint roles of one dataset revision"
assert discovery["dataset_revision"] == runs["pack"].dataset().revision
model = dossier["provenance"]["model"]
assert model["model_id"] == "local/planted"
assert model["weights"]["sha256"] == weight_fingerprint(runs["model"])["sha256"]
roles = [s["role"] for s in dossier["provenance"]["sources"]]
assert roles == ["discovery_report", "discovery_report_labeled_set",
"discovery_report_capture"]
[channel, dom] = dossier["estimates"]
assert channel["type"] == "channel" and channel["site"] == "layer.0"
assert channel["evidence"]["confirmation"]["pointer"].endswith("/validation")
assert dom["type"] == "residual_direction"
assert dossier["rejected_alternatives"][0]["evidence"]["pointer"].startswith("/contrasts/0")
assert dossier["coverage"]["untested"] == {"sets": ["neighbour:near"],
"roles": ["calibrate", "test"]}
# The same evidence with the lexical flag unresolved, or no falsification test, is not ready.
flagged = _build(runs["planted"], confound_resolutions=[])
assert flagged["readiness"]["ready"] is False
assert any("lexical_baseline_matches" in m for m in flagged["readiness"]["missing"])
assert flagged["evidence_label"]["computed"] == "independently validated predictor"
untestable = _build(runs["planted"], hypothesis={**HYPOTHESIS, "falsification_test": ""})
assert [m.split(":")[0] for m in untestable["readiness"]["missing"]] == [
"falsification_test_stated"]
path = write_dossier(dossier, runs["planted"])
assert path == runs["planted"] / "dossiers" / "alpha.json"
with pytest.raises(ValueError, match="already exists"):
write_dossier(dossier, runs["planted"])
assert read_dossier(path)[1] == runs["planted"].resolve()
def test_shuffled_labels_record_an_honest_negative(runs):
dossier = _build(runs["shuffled"], name="shuffled",
estimates=[_top_channel(runs["shuffled"])])
assert dossier["evidence_label"]["computed"] == "descriptive association"
readiness = dossier["readiness"]
assert readiness["ready"] is False
[missing] = [m for m in readiness["missing"]
if m.startswith("independent_validation_beyond_nulls")]
assert "chance" in missing or "null" in missing
assert dossier["counterexamples"], "shuffled labels leave rest rows scoring like targets"
example = dossier["counterexamples"][0]
assert example["example"]["pointer"].split("/")[-2] == "rest"
def _top_channel(run):
report = json.loads((run / REPORT).read_text())
return report["contrasts"][0]["tables"]["channel"][0]["id"]
def test_causal_or_inflated_labels_are_refused(runs):
for claim in ("causal", "controlled intervention result", "deployment-tested result"):
with pytest.raises(ValueError, match="intervention result"):
_build(runs["planted"], claimed_label=claim)
shuffled = [_top_channel(runs["shuffled"])]
with pytest.raises(ValueError, match="exceeds the computed level"):
_build(runs["shuffled"], estimates=shuffled,
claimed_label="independently validated predictor")
lower = _build(runs["shuffled"], estimates=shuffled, claimed_label="hypothesis")
assert lower["evidence_label"]["claimed"] == "hypothesis"
with pytest.raises(ValueError, match="Unknown evidence label"):
evidence_levels.normalize("proven")
with pytest.raises(ValueError, match="must be a string"):
evidence_levels.normalize(3)
assert evidence_levels.check_claim(None, "hypothesis") is None
attached = evidence_levels.check_claim("causal", "controlled intervention result",
intervention_results=[{"path": "x"}])
assert attached == "controlled intervention result"
with pytest.raises(PackError):
validate({**lower, "interventions": [lower["estimates"][0]["coordinate"]]})
def test_model_weights_or_dataset_revision_change_makes_the_dossier_stale(runs, tmp_path):
dossier = _build(runs["planted"])
base = runs["planted"]
same = dossier_readiness.check(dossier, base, model=runs["model"], pack=runs["pack"])
assert same["stale"] is False and same["label"] == "independently validated predictor"
other = copy.deepcopy(runs["model"])
with torch.no_grad():
other.layers[1].weight[3, 3] += 1e-3 # one fully hashed element of layer 1
stale = dossier_readiness.check(dossier, base, model=other)
assert stale["stale"] is True and "weight fingerprint" in stale["reasons"][0]
assert stale["evidence"] == dossier_readiness.UNSUPPORTED and stale["ready"] is False
assert {e["label"] for e in stale["estimates"]} == {"hypothesis"}
assert stale["hypothesis"] == HYPOTHESIS
revised = build_pack(tmp_path / "revised", lambda layout: layout["positive.jsonl"][25].update(
text="alpha sample 399"))
moved = dossier_readiness.check(dossier, base, pack=revised)
assert moved["stale"] and "dataset revision" in moved["reasons"][0]
clone = tmp_path / "run"
shutil.copytree(base, clone)
report = clone / REPORT
report.write_text(report.read_text().replace('"rank": 1', '"rank": 1 ', 1))
changed = dossier_readiness.check(dossier, clone)
assert changed["stale"] and "digest mismatch" in changed["reasons"][0]
assert dossier_readiness.assess(dossier, clone)["readiness"]["ready"] is False
report.unlink()
assert "missing" in dossier_readiness.check(dossier, clone)["reasons"][0]
def test_every_reference_reconstructs_from_the_dossier_alone(runs, tmp_path):
path = write_dossier(_build(runs["planted"], name="reconstruct"), runs["planted"])
dossier = json.loads(path.read_text())
base = path.parent.parent
def load(stored, sha):
file = Path(stored) if Path(stored).is_absolute() else base / stored
data = file.read_bytes()
assert hashlib.sha256(data).hexdigest() == sha, stored
return json.loads(data)
def follow(ref):
value = load(ref["path"], ref["sha256"])
for token in ref["pointer"].split("/")[1:]:
token = token.replace("~1", "/").replace("~0", "~")
value = value[int(token)] if isinstance(value, list) else value[token]
return value
for source in dossier["provenance"]["sources"]:
load(source["path"], source["sha256"])
cited = [e["coordinate"] for e in dossier["estimates"]]
cited += [r for e in dossier["estimates"] for r in e["evidence"].values() if r]
cited += [c["example"] for c in dossier["counterexamples"]]
cited += [r["evidence"] for r in dossier["rejected_alternatives"] if r["evidence"]]
for ref in cited:
assert follow(ref) is not None
channel = dossier["estimates"][0]["evidence"]
assert follow(channel["discovery"])["auc"] > 0.95
assert follow(channel["confirmation"])["fit_on_evaluation_data"] is False
assert follow(dossier["estimates"][0]["coordinate"])["channel"] == 2
# Values are cited, never copied: no measured statistic appears in the dossier itself.
text = json.dumps(dossier)
for key in ('"auc"', '"smd"', '"quantile"', '"score"'):
assert key not in text
# Pointer helpers: escaping round-trips and missing values refuse.
assert pointer("a/b", "c~d") == "/a~1b/c~0d"
assert resolve_pointer({"a/b": {"c~d": 1}}, "/a~1b/c~0d") == 1
assert resolve_pointer([5], "") == [5]
for bad in ("x", "/9", "/a/b"):
with pytest.raises(SourceChanged):
resolve_pointer({"a": [1]}, bad)
(tmp_path / "x.json").write_text("{")
with pytest.raises(SourceChanged, match="not JSON"):
Sources(tmp_path).load("x.json")
assert Sources(tmp_path).get(None) is None
def test_confirmation_must_be_independent_of_discovery(runs):
with pytest.raises(ValueError, match="not independent"):
_build(runs["planted"], confirmation=REPORT)
separate = _build(runs["planted"], confirmation="candidates/layer.0-binary-select.json")
[_, confirmation] = separate["datasets"]
assert confirmation["roles"] == ["select"] and confirmation["source"].startswith("held-out")
channel, dom = separate["estimates"]
assert channel["evidence"]["confirmation"]["path"].endswith("binary-select.json")
assert dom["evidence"]["confirmation"] is None
assert "refitted direction" in dom["confirmation_unavailable_reason"]
assert separate["evidence_label"]["computed"] == "independently validated predictor"
fit, select = {"dataset_revision": "r1", "roles": ["fit"]}, {"dataset_revision": "r1",
"roles": ["select"]}
assert check_independence(fit, select) == "disjoint roles of one dataset revision"
assert check_independence(fit, {**fit, "dataset_revision": "r2"}) == (
"different dataset revision")
with pytest.raises(ValueError, match="not independent"):
check_independence({"dataset_revision": None, "roles": ["fit"]},
{"dataset_revision": None, "roles": ["select"]})
def test_subspace_estimates_and_build_refusals(runs):
base, pack, labeled = runs["planted"], runs["pack"], runs["labeled"]
sets = ["positive", "control:neutral", "control:other"]
options = {"n_bootstrap": 5, "n_null": 30}
report = labeled_subspace_report(labeled, pack_direction(pack), "layer.0", roles=["select"],
sets=sets, **options)
path = write_subspace(report, base / "candidates")
dossier = _build(base, estimates=["channel:layer.0:2"], subspace=[path])
sub = dossier["estimates"][1]
assert sub["id"] == "subspace:layer.0:user/planted:rank2" and sub["type"] == "subspace"
assert [d["id"] for d in dossier["datasets"]][-2:] == ["subspace-0-basis-fit",
"subspace-0-evaluation"]
src = Sources(base)
assessed = dossier_readiness.estimate_assessment(dossier, sub, src)
assert assessed["label"] == "independently validated predictor"
assert assessed["unresolved_confounds"] == ["confound_baselines_not_assessed"]
blind = labeled_subspace_report(labeled, pack_direction(pack, receipt=False), "layer.0",
roles=["fit"], sets=sets, **options)
blind_path = write_subspace(blind, base / "candidates")
unknown = _build(base, estimates=[], subspace=[blind_path])
[est] = unknown["estimates"]
assert "no dataset access receipt" in est["confirmation_unavailable_reason"]
assert unknown["evidence_label"]["computed"] == "descriptive association"
overlap = labeled_subspace_report(labeled, pack_direction(pack), "layer.0",
roles=["fit"], sets=sets, **options)
with pytest.raises(ValueError, match="not independent"):
_build(base, estimates=[], subspace=[write_subspace(overlap, base / "candidates")])
for changes, match in (({"estimates": []}, "at least one estimate"),
({"estimates": ["channel:layer.0:99"]}, "not in contrast"),
({"contrast": "nope"}, "Contrast 'nope'"),
({"discovery": str(path)}, "is not a"),
({"confound_resolutions": [{**LEXICAL, "estimate": "x"}]},
"unknown estimate")):
with pytest.raises(ValueError, match=match):
_build(base, **changes)
with pytest.raises(ValueError, match="Dossier name"):
_build(base, name="../escape")
stray = base / "stray"
stray.mkdir(exist_ok=True)
shutil.copy(base / REPORT, stray / "report.json")
with pytest.raises(ValueError, match="No labeled set"):
_build(base, discovery="stray/report.json")
def test_cli_build_show_check_and_explorer_rows(runs, tmp_path, capsys, monkeypatch):
run = tmp_path / "run"
shutil.copytree(runs["planted"], run, ignore=shutil.ignore_patterns("dossiers"))
assert explorer.dossier_rows({"run_dir": run}) == []
spec = tmp_path / "input.json"
spec.write_text(json.dumps({"name": "cli", "hypothesis": HYPOTHESIS, "discovery": REPORT,
"estimates": ["channel:layer.0:2"],
"confound_resolutions": [LEXICAL]}))
main(["experiments", "dossier", "build", str(spec), "--run", str(run)])
built = json.loads(capsys.readouterr().out)
assert built["ready"] is True and built["dossier"].endswith("dossiers/cli.json")
main(["experiments", "dossier", "show", built["dossier"]])
shown = json.loads(capsys.readouterr().out)
assert shown["evidence_label"] == "independently validated predictor"
assert shown["readiness"]["ready"] is True and shown["stale"] is False
main(["experiments", "dossier", "check", built["dossier"]])
assert json.loads(capsys.readouterr().out)["stale"] is False
def factory(model):
return lambda section: type("Handle", (), {
"model": model, "tokenizer": WordTokenizer(), "cleanup": lambda self: None})()
from obliteratus.concepts.pack import ConceptPack
current = cli_check(built["dossier"], current=True, model_factory=factory(runs["model"]),
pack_resolver=ConceptPack.load)
assert current["stale"] is False, current["reasons"]
monkeypatch.setattr("obliteratus.experiments.inputs.default_model_factory",
factory(PlantedModel(planted=channel_two(), seed=1)))
with pytest.raises(SystemExit) as stale:
main(["experiments", "dossier", "check", built["dossier"], "--model", "local/planted"])
assert stale.value.code == 1
assert "weight fingerprint" in json.loads(capsys.readouterr().out)["reasons"][0]
rows = explorer.dossier_rows({"run_dir": run})
assert len(rows) == 1 and len(rows[0]) == len(dossier_readiness.TABLE_COLUMNS)
assert rows[0][3] == "independently validated predictor" and rows[0][4] == "yes"
assert "falsification_test_stated: met" in rows[0][5] and rows[0][6] == "current"
(run / "dossiers" / "broken.json").write_text("{")
assert [p.name for p, _ in list_dossiers(run)] == ["cli.json"]
(run / REPORT).write_text("{}")
stale_row = explorer.dossier_rows({"run_dir": run})[0]
assert stale_row[6] == "stale" and stale_row[3] == "hypothesis" and "digest" in stale_row[5]
main(["experiments", "dossier", "show", built["dossier"]])
assert json.loads(capsys.readouterr().out)["readiness"]["ready"] is False
for content, match in (({"name": "x"}, "requires 'hypothesis'"), ([], "JSON object"),
({"name": "x", "hypothesis": {}, "discovery": REPORT, "colour": 1},
"Unknown dossier input"),
({"name": "cli", "hypothesis": HYPOTHESIS, "discovery": REPORT,
"estimates": ["channel:layer.0:2"],
"rejected_alternatives": [{"id": "a"}]}, "missing field")):
spec.write_text(json.dumps(content))
with pytest.raises(SystemExit) as refused:
main(["experiments", "dossier", "build", str(spec), "--run", str(runs["planted"])])
assert refused.value.code == 4 and match in capsys.readouterr().err
def test_schema_is_shipped_by_the_package_data_glob():
import re
text = (Path(__file__).parents[1] / "pyproject.toml").read_text()
line = re.search(r'^"obliteratus\.concepts" = \[(.*)\]$', text, re.MULTILINE).group(1)
globs = json.loads(f"[{line}]")
relative = SCHEMA_PATH.relative_to(SCHEMA_PATH.parents[1])
assert any(relative.match(pattern) for pattern in globs)
def test_a_new_dataset_revision_that_reuses_discovery_rows_is_not_independent(runs):
"""Different revision alone is not enough: shared tokenized inputs are refused."""
import json
source = runs["planted"] / "candidates" / "layer.0-binary-select.json"
report = json.loads(source.read_text())
report["labeled_set"]["dataset_revision"] = "sha256:" + "f" * 64
copy = runs["planted"] / "candidates" / "layer.0-binary-select-rev2.json"
copy.write_text(json.dumps(report))
try:
with pytest.raises(ValueError, match="same tokenized input"):
_build(runs["planted"], confirmation="candidates/layer.0-binary-select-rev2.json")
finally:
copy.unlink()
def test_accepted_confounds_stay_visible_with_their_reason(runs):
from obliteratus.analysis import dossier_readiness
dossier = _build(runs["planted"])
assessed = dossier_readiness.assess(dossier, runs["planted"])
channel = next(e for e in assessed["estimates"] if e["id"] == "channel:layer.0:2")
assert channel["unresolved_confounds"] == []
assert {"flag": LEXICAL["flag"], "reason": LEXICAL["reason"]} in channel["accepted_confounds"]