Files
OBLITERATUS/obliteratus/experiments/intervention_runner.py
Joseph MaglyandClaude Opus 5.5 77d788b22c feat(experiments): confirmation and maturation registry
Add the confirmation registry (F15): freeze what decides a confirmation
before its results are unlocked, track exposure of confirmation data,
and compute maturity from referenced evidence.

- Append-only registry under <run>/registry with a schema file as
  authority. A registration freezes, by digest, the dossier binding,
  the full intervention spec, scorers, threshold, direction, margins,
  decision dose, target task, seeds and the confirmation row set (text
  digests only). Edited registrations fail their id; changes need an
  amendment, counted as a new comparison.
- Unlocking requires a matching frozen registration: the result must
  come from the frozen spec after registration, on the frozen rows, with
  an unchanged dossier, and its verdict is recomputed from its records.
- Every evaluation and unlock is logged as an exposure. Exposed rows are
  refused as inputs to discovery, example selection and causal maps,
  and for any later confirmation of any target. Runs without a registry
  are unchanged; an optional registry_dir covers outputs outside the
  run.
- Replications must reuse the recipe with different seeds and disjoint
  rows. Non-supported verdicts are negative entries that mark the target
  for re-investigation and require fresh data.
- Maturity per (target, method, model revision, task, backend) is
  computed: hypothesis, candidate, causal, replicated, persistent-pilot
  eligible (which also needs a current dossier). Fixture results never
  count by default; claims above the computed level are refused.
- The pain pack's declared protocol registers as the first candidate
  after reference and prompt-digest verification; with no answer keys it
  stays at hypothesis, marked descriptive only.
- `obliteratus experiments registry register|unlock|replicate|status`,
  a Python API and an Explorer section.

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

259 lines
13 KiB
Python

"""Controlled intervention runner: four arms, dose ladder, restoration after every arm.
``run_intervention`` runs one spec (see ``intervention_spec``) for one ready
dossier estimate and stores it beside the dossier's run as
``<run>/interventions/<spec-digest>/attempts/NNNN/`` using the observation
store's attempt lifecycle (``pending -> running -> complete | failed |
cancelled``). Every refusal (dossier, estimate, geometry, independence) happens
before the attempt moves to ``running``, i.e. before any forward pass.
Arms on the same examples, in this order: (1) unchanged baseline; then for each
declared coefficient (2) the intended operation on the estimate and (3) the
same operation along a norm/rank-matched random direction orthogonal to it;
finally (4) a fresh-baseline replay. Restoration (parameter/buffer digest, hook
counts, probe logits) is checked after every arm; the replay must reproduce the
baseline completions exactly. A failed check stops the attempt.
"""
from contextlib import ExitStack
from copy import deepcopy
from dataclasses import replace
from pathlib import Path
import torch
from obliteratus.analysis import dossier_interventions as verdicts
from obliteratus.analysis import dossier_refs as refs
from obliteratus.concepts import restoration
from obliteratus.concepts.scorecard import build_baseline_scorecard, build_scorecard
from obliteratus.experiments import generation as _generation
from obliteratus.experiments import inputs as _inputs
from obliteratus.experiments import intervention_checks as checks
from obliteratus.experiments import intervention_controls as controls
from obliteratus.experiments.intervention_ops import OPERATIONS, hook_record
from obliteratus.experiments.intervention_spec import load_intervention_spec, observation_view
from obliteratus.experiments.registry import guard_evaluation
from obliteratus.experiments.runner import _Cancel, _tool
from obliteratus.experiments.store import (
TERMINAL, ObservationStore, StoreError, atomic_json, now, read_json,
)
INTERVENTION_DIR = "interventions"
class RestorationFailed(RuntimeError):
"""The model was not restored after an arm; discard and reload it."""
def __init__(self, arm, coefficient, details):
super().__init__(f"Model not restored after the {arm} arm (coefficient {coefficient}): "
+ ", ".join(k for k in ("weights_restored", "hooks_restored",
"outputs_restored", "identical_to_baseline")
if details.get(k) is False))
self.restoration = {"arm": arm, "coefficient": coefficient, **details}
def run_intervention(spec, run_dir, *, model_factory=None, pack_resolver=None,
check_cancelled=None, rerun=False, operations=None):
"""Run (or return the completed result of) one controlled experiment spec.
``model_factory(model_section)`` returns an object with ``model``, ``tokenizer``
and optional ``cleanup()``. ``operations`` replaces the operation registry
(testing only). Exceptions from ``check_cancelled`` cancel the attempt.
"""
spec = load_intervention_spec(spec)
run = ObservationStore(Path(run_dir) / INTERVENTION_DIR).open(spec)
latest = run.latest_complete()
if latest is not None and not rerun:
return load_intervention_result(run.attempt_path(latest))
cancel, state = _Cancel(check_cancelled), {}
with run.lock():
attempt = run.new_attempt()
try:
result = _session(spec, Path(run_dir), run, attempt, model_factory, pack_resolver,
cancel, state, operations or OPERATIONS)
verdicts.validate_result(result)
atomic_json(attempt.path / "result.json", result)
attempt.transition("complete")
except BaseException as error:
if attempt.state not in TERMINAL:
cancelled = cancel.fired or isinstance(error, KeyboardInterrupt)
attempt.transition("cancelled" if cancelled else "failed",
reason=f"{type(error).__name__}: {error}"[:2000],
error_type=type(error).__name__,
hooks_removed=state.get("hooks_removed"),
forward_passes=state.get("running", False),
restoration=getattr(error, "restoration", None))
raise
return load_intervention_result(attempt.path)
def _session(spec, base, run, attempt, model_factory, pack_resolver, cancel, state, operations):
started = now()
prepared = checks.prepare(spec, base)
_, tasks, data = _inputs.resolve_dataset(observation_view(spec), pack_resolver)
margin = prepared["declared"]["collateral_margin"]
tasks = [replace(task, margin=margin, min_pairs=None) for task in tasks]
handle = (model_factory or _inputs.default_model_factory)(deepcopy(spec["model"]))
try:
model, tokenizer = handle.model, handle.tokenizer
adapter = _inputs.require_unmodified(model)
target = checks.verify_model(prepared, spec, model, tokenizer, adapter)
independence = checks.check_evaluation_independence(prepared, data, tasks, tokenizer)
guard_evaluation(base, spec, run.digest, tasks)
ctx = {"spec": spec, "model": model, "tokenizer": tokenizer, "tasks": tasks,
"target": target, "attempt": attempt, "run": run, "cancel": cancel,
"operations": operations, "prepared": prepared,
"device": target["identity"]["device"]}
if spec["kind"] == "controlled_intervention":
plan, execute = _plan_controlled(ctx), _controlled
else:
from obliteratus.experiments import patching
plan, execute = patching.plan(ctx), patching.execute
cancel()
state["hooks"] = restoration.hook_counts(model)
attempt.transition("running")
state["running"] = True
try:
body = execute(ctx, plan)
finally:
state["hooks_removed"] = restoration.hook_counts(model) == state["hooks"]
finally:
cleanup = getattr(handle, "cleanup", None)
if callable(cleanup):
cleanup()
dossier, estimate = prepared["dossier"], prepared["estimate"]
model_ids = [spec["model"]["id"], dossier["provenance"]["model"]["model_id"]]
return {"schema": verdicts.RESULT_SCHEMA, "schema_version": verdicts.RESULT_VERSION,
"kind": spec["kind"], "spec_digest": run.digest, "attempt": attempt.number,
**({"family": spec["family"]} if spec.get("family") else {}),
"state": "complete", "started_at": started, "completed_at": now(),
"evidence_scope": verdicts.evidence_scope(spec["model"]["model_class"], model_ids),
"dossier": {"path": refs.stored_path(prepared["path"], base), "name": dossier["name"],
"created_at": dossier["created_at"],
"hypothesis_sha256": verdicts.hypothesis_digest(dossier["hypothesis"]),
"declared": prepared["declared"]},
"estimate": {"id": estimate["id"], "type": estimate["type"], "site": estimate["site"],
"coordinate": estimate["coordinate"]},
"inputs": {"dataset": data, "model": target["identity"]},
"independence": independence, "geometry": target["geometry"],
"target": target["record"],
"restoration_rule": f"after every arm: {restoration.RULE}; otherwise stop",
**body, "tool": _tool()}
def _probe(ctx):
model, device, tasks = ctx["model"], ctx["device"], ctx["tasks"]
name = ctx["spec"].get("target_task") or ctx["spec"]["patching"]["target_task"]
examples = next(t for t in tasks if t.name == name).examples[:ctx["spec"]["probes"]]
inputs = [_inputs.encode(ctx["tokenizer"], e.prompt) for e in examples]
def probe():
with torch.inference_mode():
return [model(**{k: v.to(device) for k, v in batch.items()}).logits[0, -1].float().cpu()
for batch in inputs]
return probe
def _plan_controlled(ctx):
intended = ctx["target"]["vector"]
span = ctx["target"].get("span")
vector, control = controls.build_control(intended, ctx["spec"]["control"]["seed"], span)
controls.check_matched(intended, vector, control, span)
return {"control_vector": vector, "control": control, "probe": _probe(ctx)}
def _generate(ctx, out):
spec, settings = ctx["spec"], {"chat_template": False, "system": None}
decoding = {"decoding": {"strategy": "greedy", **spec["decoding"]}, "seed": spec["seed"]}
completions, paths = {}, {}
for task in ctx["tasks"]:
target = out / task.name
target.mkdir(parents=True, exist_ok=False)
prompts = _generation.render_prompts(ctx["tokenizer"], task.examples, settings)
_generation.generate_task(ctx["model"], ctx["tokenizer"], task, prompts, decoding,
ctx["cancel"], target, ctx["device"])
rows = read_json(target / "generations.json")["rows"]
if [r["example_id"] for r in rows] != [e.example_id for e in task.examples]:
raise StoreError(f"Generations for {task.name} do not match its examples")
completions[task.name] = [r["completion"] for r in rows]
paths[task.name] = (target / "generations.json").relative_to(ctx["run"].path).as_posix()
return completions, paths
def _controlled(ctx, plan):
spec, model, tasks, target = ctx["spec"], ctx["model"], ctx["tasks"], ctx["target"]
operation, probe = spec["operation"], plan["probe"]
apply = ctx["operations"][operation["kind"]]
pristine = restoration.snapshot(model, probe)
arms = []
def arm(name, coefficient, vector):
record = {"order": len(arms), "arm": name, "coefficient": coefficient, "hook": None,
"scorecard": None, "identical_to_baseline": None}
out = ctx["attempt"].path / "arms" / f"{record['order']:02d}-{name}"
with ExitStack() as stack:
if vector is not None:
stack.enter_context(apply(model, target["layer"], vector, coefficient, operation))
intended = name == "intended"
record["hook"] = hook_record(
target, operation, coefficient, vector, "estimate" if intended else "random",
target["record"]["orthogonalization"] if intended
else plan["control"].get("orthogonalization"))
completions, record["generations"] = _generate(ctx, out)
record["restoration"] = restoration.check(model, pristine, probe)
arms.append(record)
if not record["restoration"]["restored"]:
raise RestorationFailed(name, coefficient, record["restoration"])
return record, [completions[t.name] for t in tasks]
_, baseline = arm("baseline", None, None)
ladder = []
for coefficient in operation["coefficients"]:
pair = {}
for name, vector in (("intended", target["vector"]),
("random_control", plan["control_vector"])):
record, completions = arm(name, coefficient, vector)
record["scorecard"] = build_scorecard(tasks, baseline, completions)
pair[name] = record
ladder.append({"coefficient": coefficient,
"intended_status": pair["intended"]["scorecard"]["status"],
"control_status": pair["random_control"]["scorecard"]["status"],
"comparison": verdicts.compare_arms(
pair["intended"], pair["random_control"], spec["target_task"],
ctx["prepared"]["declared"]["effect_direction"],
ctx["prepared"]["declared"]["useful_effect_threshold"], coefficient),
"role": ("decision dose (pre-declared)"
if coefficient == operation["decision_coefficient"]
else "descriptive dose; never used for the verdict")})
record, completions = arm("replay", None, None)
record["scorecard"] = build_scorecard(tasks, baseline, completions)
record["identical_to_baseline"] = completions == baseline
if not record["identical_to_baseline"]:
raise RestorationFailed("replay", None, {**record["restoration"],
"identical_to_baseline": False})
body = {"endpoints": {"target_task": spec["target_task"],
"collateral_tasks": [t.name for t in tasks
if t.name != spec["target_task"]]},
"operation": {**operation, "module": target["module"],
"vector_sha256": checks.vector_sha256(target["vector"])},
"control": plan["control"], "baseline": build_baseline_scorecard(tasks, baseline),
"arms": arms, "dose_ladder": ladder}
body["verdict"] = verdicts.compute_verdict(body, ctx["prepared"]["declared"])
return body
def load_intervention_result(source):
"""Load a result from ``result.json`` or an attempt directory."""
path = Path(source)
if path.is_dir():
path = path / "result.json"
result = read_json(path)
if not isinstance(result, dict) or result.get("schema") != verdicts.RESULT_SCHEMA:
raise StoreError(f"{path} is not an intervention result")
result["location"] = {"result": str(path), "run_dir": str(path.parents[4])}
return result
__all__ = ["INTERVENTION_DIR", "RestorationFailed", "load_intervention_result",
"run_intervention"]