Files
OBLITERATUS/obliteratus/experiments/dossier_cli.py
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

117 lines
6.1 KiB
Python

"""``obliteratus experiments dossier build|show|check`` (target dossiers beside a run)."""
import json
from pathlib import Path
_INPUT_KEYS = {"name", "hypothesis", "discovery", "estimates", "contrast", "confirmation",
"subspace", "confound_resolutions", "coverage_notes", "rejected_alternatives",
"unresolved_questions", "claimed_label"}
def add_dossier(commands):
parser = commands.add_parser(
"dossier", help="Target dossiers: hypothesis, estimates and evidence by reference")
sub = parser.add_subparsers(dest="dossier_command", required=True)
build = sub.add_parser("build", help="Build <run>/dossiers/<name>.json from an input file")
build.add_argument("input", help="Dossier input JSON (name, hypothesis, discovery report, "
"estimates, ...)")
build.add_argument("--run", required=True,
help="Run directory; report paths in the input resolve against it")
build.add_argument("--overwrite", action="store_true", help="Replace an existing dossier")
show = sub.add_parser("show", help="Show a dossier with its recomputed readiness checklist")
show.add_argument("dossier", help="Dossier JSON file")
show.add_argument("--allow-fixture-results", action="store_true",
help="Let software-contract intervention results on fixture models raise "
"the computed label (never a model finding)")
check = sub.add_parser(
"check", help="Check whether a dossier is stale; exit 1 when it is")
check.add_argument("dossier", help="Dossier JSON file")
check.add_argument("--current", action="store_true",
help="Also compare the run's current model weights and pack dataset "
"revision (from <run>/spec.json)")
check.add_argument("--pack", help="Compare the dataset revision of this pack")
check.add_argument("--model", help="Compare the weights of this model id")
check.add_argument("--dtype", default="float32")
check.add_argument("--revision")
check.add_argument("--device", default="cpu")
def _read_input(path):
value = json.loads(Path(path).read_text(encoding="utf-8"))
if not isinstance(value, dict):
raise ValueError("Dossier input must be a JSON object")
unknown = set(value) - _INPUT_KEYS
if unknown:
raise ValueError(f"Unknown dossier input fields: {sorted(unknown)}")
for key in ("name", "hypothesis", "discovery"):
if key not in value:
raise ValueError(f"Dossier input requires {key!r}")
return value
def build(input_path, run, *, overwrite=False):
from obliteratus.analysis.target_dossier import build_dossier, write_dossier
spec = _read_input(input_path)
options = {k: spec[k] for k in spec if k not in {"name", "hypothesis", "discovery"}}
options.setdefault("estimates", [])
try:
dossier = build_dossier(spec["name"], run, hypothesis=spec["hypothesis"],
discovery=spec["discovery"], **options)
except KeyError as error:
raise ValueError(f"Dossier input entry is missing field {error}") from error
path = write_dossier(dossier, run, overwrite=overwrite)
return {"dossier": str(path), "evidence_label": dossier["evidence_label"],
"ready": dossier["readiness"]["ready"], "missing": dossier["readiness"]["missing"]}
def show(path, *, allow_fixture_results=False):
from obliteratus.analysis import dossier_readiness
from obliteratus.analysis.target_dossier import read_dossier
dossier, base = read_dossier(path)
status = dossier_readiness.check(dossier, base, allow_fixture_results=allow_fixture_results)
readiness = (dossier_readiness.assess(dossier, base)["readiness"] if not status["stale"]
else {"ready": False, "missing": status["reasons"]})
return {"name": dossier["name"], "hypothesis": dossier["hypothesis"],
"datasets": dossier["datasets"],
"estimates": [{"id": e["id"], "type": e["type"], "site": e["site"]}
for e in dossier["estimates"]],
"evidence_label": status["label"], "claimed_label":
dossier["evidence_label"]["claimed"], "stale": status["stale"],
"readiness": readiness, "counterexamples": len(dossier["counterexamples"]),
"rejected_alternatives": dossier["rejected_alternatives"],
"unresolved_questions": dossier["unresolved_questions"],
"interventions": status["interventions"]}
def check(path, *, current=False, pack=None, model=None, dtype="float32", revision=None,
device="cpu", model_factory=None, pack_resolver=None):
from obliteratus.analysis import dossier_readiness
from obliteratus.analysis.target_dossier import read_dossier
from obliteratus.experiments import inputs as _inputs
from obliteratus.experiments.spec import load_spec
dossier, base = read_dossier(path)
section = None if model is None else {"id": model, "dtype": dtype, "revision": revision,
"device": device}
if current:
spec = load_spec(base / "spec.json")
pack, section = pack or spec["dataset"]["pack"], section or dict(spec["model"])
resolved = None if pack is None else (pack_resolver or _inputs.default_pack_resolver)(pack)
handle = None if section is None else (
model_factory or _inputs.default_model_factory)(section)
try:
return dossier_readiness.check(dossier, base, pack=resolved,
model=None if handle is None else handle.model)
finally:
cleanup = getattr(handle, "cleanup", None)
if callable(cleanup):
cleanup()
def run_dossier_command(args):
if args.dossier_command == "build":
return build(args.input, args.run, overwrite=args.overwrite)
if args.dossier_command == "show":
return show(args.dossier, allow_fixture_results=args.allow_fixture_results)
return check(args.dossier, current=args.current, pack=args.pack, model=args.model,
dtype=args.dtype, revision=args.revision, device=args.device)