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

110 lines
5.7 KiB
Python

"""``experiments select-examples``: the O02 example-selection study on a labeled set.
Captures (or reuses, after full verification) the pack's labeled sets for
roles ``fit`` and ``select`` (legacy packs: ``fit`` only, evaluated on a
group-disjoint holdout fold) at one decoder layer of an unmodified model,
derives hashed lexical features from the in-memory row text (the text is
never written), and writes ``<site>.json`` next to an
observation run (``<run>/example-selection/``) or into a chosen directory,
alongside the verified labeled set it used.
"""
import json
import time
from pathlib import Path
from obliteratus.analysis.example_selection import lexical_features
from obliteratus.analysis.example_selection_report import REPORT_DIR, write_report
from obliteratus.analysis.example_selection_study import run_selection_study
from obliteratus.analysis.labeled_activations import (
CAPTURE_DIR, MANIFEST, capture_labeled_set, load_labeled_set, plan_labeled_set,
)
from obliteratus.experiments import inputs as _inputs
from obliteratus.experiments.registry_store import assert_unexposed
from obliteratus.experiments.spec import SpecError, load_spec
from obliteratus.experiments.subspace import labeled_dir
def _capture_cost(target, reused):
try:
manifest = json.loads((target / CAPTURE_DIR / "manifest.json").read_text())
measured = manifest["measured"]
except (OSError, ValueError, KeyError) as error:
return {"measured": False, "reason": f"capture manifest unreadable: {error}"}
return {"measured": True, "reused_labeled_set": reused,
"wall_seconds": measured.get("wall_seconds"),
"bytes_written": measured.get("bytes_written"),
"rows": len(manifest.get("shards", [])),
"note": "measured when the labeled set was captured (all captured rows and sites)"}
def run_select_examples(pack, model, tokenizer, layer, out_dir, *, model_id, config=None,
device="cpu", budget_bytes=None, layers=None, cache_dir=None,
timings=True, registry_dir=None):
"""Capture-or-reuse the labeled set, run the study at ``layer`` and write the JSON.
``registry_dir`` names a run's confirmation registry to consult when ``out_dir``
is not inside that run (``<out>`` and its parent are always consulted).
"""
_inputs.require_unmodified(model)
layers = layers or _inputs.capture_sites(model, [layer])
if len(layers) != 1:
raise ValueError("Example selection reads exactly one site")
site = next(iter(layers))
roles = ["fit", "select"] if "dataset" in pack.manifest else ["fit"]
assert_unexposed(lambda: plan_labeled_set(pack, roles, cache_dir=cache_dir)["rows"],
out_dir, Path(out_dir).parent, registry_dir)
target = labeled_dir(out_dir, site, roles)
options = {"model_id": model_id, "cache_dir": cache_dir}
reused = (target / MANIFEST).is_file()
if not reused:
capture_labeled_set(model, tokenizer, layers, pack, target, roles=roles, device=device,
budget_bytes=budget_bytes, **options)
labeled = load_labeled_set(target, model, tokenizer, layers, pack, **options)
started = time.perf_counter()
lexical = lexical_features(plan_labeled_set(pack, roles, cache_dir=cache_dir)["rows"])
lexical_seconds = time.perf_counter() - started
report = run_selection_study(labeled, site, lexical=lexical, config=config,
capture_cost=_capture_cost(target, reused),
lexical_seconds=lexical_seconds, timings=timings)
path = write_report(report, out_dir)
return {"report": str(path), "labeled_set": str(target), "reused_labeled_set": reused,
"recommendation": report["recommendation"]["verdict"],
"report_sha256": report["report_sha256"]}
def resolve_and_run(*, layer, run=None, pack=None, model=None, dtype="float32", revision=None,
device="cpu", out=None, config=None, budget_bytes=None, timings=True,
model_factory=None, pack_resolver=None):
"""Resolve the pack and model from an observation run directory or explicit references."""
if run is not None:
if pack is not None or model is not None:
raise SpecError("Pass either a run directory or --pack and --model, not both")
spec = load_spec(Path(run) / "spec.json")
pack_ref, section = spec["dataset"]["pack"], dict(spec["model"])
out = out or Path(run) / REPORT_DIR
else:
if pack is None or model is None or out is None:
raise SpecError("Without a run directory, --pack, --model and --out are required")
spec, pack_ref = None, pack
section = {"id": model, "dtype": dtype, "revision": revision, "device": device}
if isinstance(config, (str, Path)):
config = json.loads(Path(config).read_text())
resolved = (pack_resolver or _inputs.default_pack_resolver)(pack_ref)
if spec is not None and "dataset" in resolved.manifest and (
resolved.dataset().revision != spec["dataset"]["revision"]):
raise SpecError("Pack dataset revision differs from the run's spec")
handle = (model_factory or _inputs.default_model_factory)(section)
try:
return run_select_examples(resolved, handle.model, handle.tokenizer, layer, out,
model_id=section["id"], config=config,
device=section.get("device", "cpu"),
budget_bytes=budget_bytes, timings=timings)
finally:
cleanup = getattr(handle, "cleanup", None)
if callable(cleanup):
cleanup()
__all__ = ["REPORT_DIR", "resolve_and_run", "run_select_examples"]