mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 11:04:06 +01:00
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>
299 lines
15 KiB
Python
299 lines
15 KiB
Python
"""Causal targeting map: a bounded, resumable grid of controlled interventions.
|
|
|
|
``run_map`` measures one ready dossier estimate over sites x token scopes x
|
|
doses. Each runnable row (the estimate's own site, one token scope) is one
|
|
call of the controlled intervention harness (``run_intervention``) with the
|
|
grid's doses as its ladder: baseline, intended and matched random-control arms
|
|
per dose, a fresh-baseline replay and restoration checks after every arm. The
|
|
map stores only references to those results (path, SHA-256, JSON pointer) in
|
|
``<run>/maps/<map-digest>/map.json`` and recomputes every cell from them.
|
|
|
|
Sites other than the estimate's own site would apply a transferred coordinate:
|
|
a channel index has no guaranteed meaning at another layer, and the harness
|
|
refuses them. This version records them as untested exploratory cells with the
|
|
reason (and the capture width, where known); they never reach a dossier.
|
|
|
|
The cell cap bounds the number of tested cells. A resumed map reuses completed
|
|
rows whose stored result still has the expected row-spec digest and SHA-256,
|
|
and refuses a map file or directory written for a different spec.
|
|
"""
|
|
|
|
from pathlib import Path
|
|
|
|
from obliteratus.analysis import causal_map_cells as cells_mod
|
|
from obliteratus.analysis import dossier_interventions as verdicts
|
|
from obliteratus.analysis import dossier_refs as refs
|
|
from obliteratus.experiments import causal_map_spec as mapspec
|
|
from obliteratus.experiments import intervention_checks as checks
|
|
from obliteratus.experiments.intervention_runner import (
|
|
RestorationFailed, load_intervention_result, run_intervention,
|
|
)
|
|
from obliteratus.experiments.intervention_spec import load_intervention_spec
|
|
from obliteratus.experiments.registry_store import assert_unexposed, spec_rows
|
|
from obliteratus.experiments.spec import SpecError, spec_digest
|
|
from obliteratus.experiments.store import ObservationStore, atomic_json, now, read_json
|
|
|
|
MAP_DIR = "maps"
|
|
MAP_FILE = "map.json"
|
|
MAP_SCHEMA = "obliteratus.causal-map"
|
|
MAP_VERSION = 1
|
|
TRANSFER_RULE = (
|
|
"cells at sites other than the estimate's own site would apply a transferred coordinate "
|
|
"(a channel index or residual vector has no guaranteed meaning at another layer); this "
|
|
"version does not run them: they are untested, labelled exploratory (transferred "
|
|
"coordinate) and can never be attached to a dossier or change its label")
|
|
SCOPE_NOTE = (
|
|
"Results hold only under the tested conditions: this model, dossier estimate, sites, "
|
|
"token scopes, doses, operation and evaluation rows. They are not general mechanistic "
|
|
"claims about the model.")
|
|
|
|
|
|
class MapRefused(ValueError):
|
|
"""The map cannot run, resume or attach as asked."""
|
|
|
|
|
|
def _estimate(spec, run_dir):
|
|
from obliteratus.analysis.target_dossier import read_dossier
|
|
dossier, _ = read_dossier(refs.resolve_path(spec["dossier"], run_dir))
|
|
estimate = next((e for e in dossier["estimates"] if e["id"] == spec["estimate"]), None)
|
|
if estimate is None:
|
|
raise MapRefused(f"Estimate {spec['estimate']!r} is not in the dossier")
|
|
return dossier, estimate
|
|
|
|
|
|
def _row_digest(spec, site, scope):
|
|
return spec_digest(load_intervention_spec(mapspec.row_spec(spec, site, scope)))
|
|
|
|
|
|
def _widths(spec, run_dir, site, others):
|
|
"""Capture width of each transferred site, or why it is unknown."""
|
|
if not others:
|
|
return {}
|
|
try:
|
|
prepared = checks.prepare(mapspec.row_spec(spec, site, spec["grid"]["token_scopes"][0]),
|
|
Path(run_dir))
|
|
except checks.InterventionRefused as error:
|
|
return {s: f"unknown ({error})" for s in others}
|
|
from safetensors import safe_open
|
|
shards = prepared["capture"]["shards"]
|
|
captured = {s["name"] for s in prepared["capture"]["identity"]["sites"]}
|
|
found = {}
|
|
for name in [site, *others]:
|
|
if name not in captured or not shards:
|
|
found[name] = None
|
|
continue
|
|
with safe_open(str(prepared["capture_dir"] / shards[0]["file"]), framework="pt") as h:
|
|
found[name] = h.get_slice(f"values/{name}").get_shape()[-1]
|
|
own = found[site]
|
|
return {s: ("unknown (site not in the estimate's capture)" if found[s] is None
|
|
else f"width {found[s]} matches the estimate width {own}" if found[s] == own
|
|
else f"width {found[s]} differs from the estimate width {own}")
|
|
for s in others}
|
|
|
|
|
|
def _verified_row(entry, expected, run_dir):
|
|
"""The stored result of a completed row after identity and digest checks."""
|
|
if entry.get("row_spec_digest") != expected:
|
|
raise MapRefused(f"Map row {entry['site']}/{entry['token_scope']} was measured with a "
|
|
"different row spec; refusing to mix cells from another spec")
|
|
path = refs.resolve_path(entry["result"]["path"], run_dir)
|
|
if not path.is_file() or refs.file_sha256(path) != entry["result"]["sha256"]:
|
|
raise MapRefused(f"Stored result {entry['result']['path']} is missing or changed")
|
|
result = load_intervention_result(path)
|
|
if result["spec_digest"] != expected:
|
|
raise MapRefused(f"Stored result {entry['result']['path']} belongs to another spec")
|
|
return result
|
|
|
|
|
|
def _previous(map_path, digest, resume_from):
|
|
if resume_from is not None:
|
|
other = read_json(resume_from)
|
|
if not isinstance(other, dict) or other.get("spec_digest") != digest:
|
|
raise MapRefused(f"{resume_from} was written for a different map spec "
|
|
f"({other.get('spec_digest') if isinstance(other, dict) else None}"
|
|
f", expected {digest}); refusing to mix cells")
|
|
if not map_path.is_file():
|
|
return {}
|
|
stored = read_json(map_path)
|
|
if stored.get("schema") != MAP_SCHEMA or stored.get("spec_digest") != digest:
|
|
raise MapRefused(f"{map_path} holds a different map; refusing to mix cells")
|
|
return {(r["site"], r["token_scope"]): r for r in stored["rows"]
|
|
if r["state"] == "complete"}
|
|
|
|
|
|
def _assemble(spec, digest, rows, dossier, estimate, run_dir):
|
|
grid, cells, scopes = spec["grid"], [], set()
|
|
for row in rows:
|
|
relation = "estimate_site" if row["site"] == estimate["site"] else "transferred"
|
|
if row["state"] == "complete":
|
|
result = load_intervention_result(refs.resolve_path(row["result"]["path"], run_dir))
|
|
cells.extend(cells_mod.tested_cells(result, row["result"], run_dir))
|
|
scopes.add(result["evidence_scope"]["scope"])
|
|
continue
|
|
for dose in grid["doses"]:
|
|
role = "decision" if dose == spec["decision_coefficient"] else "descriptive"
|
|
cells.append(cells_mod.untested(row["site"], row["token_scope"], dose, role=role,
|
|
relation=relation, reason=row["reason"]))
|
|
correction = cells_mod.apply_correction(cells)
|
|
counts = {}
|
|
for cell in cells:
|
|
counts[cell["status"]] = counts.get(cell["status"], 0) + 1
|
|
return {"schema": MAP_SCHEMA, "schema_version": MAP_VERSION, "kind": "causal_targeting_map",
|
|
"spec_digest": digest, "spec": spec, "updated_at": now(),
|
|
"dossier": {"path": spec["dossier"], "name": dossier["name"],
|
|
"created_at": dossier["created_at"],
|
|
"hypothesis_sha256": verdicts.hypothesis_digest(dossier["hypothesis"])},
|
|
"estimate": {"id": estimate["id"], "type": estimate["type"],
|
|
"site": estimate["site"]},
|
|
"grid": grid, "decision_coefficient": spec["decision_coefficient"],
|
|
"target_task": spec["target_task"], "budget": spec["budget"], "rows": rows,
|
|
"cells": cells, "correction": correction, "counts": counts,
|
|
"supported": [c["id"] for c in cells if c["status"] == cells_mod.SUPPORTED],
|
|
"evidence_scope": sorted(scopes), "site_transfer_rule": TRANSFER_RULE,
|
|
"interval_rule": cells_mod.INTERVAL_RULE, "scope_note": SCOPE_NOTE}
|
|
|
|
|
|
def run_map(spec, run_dir, *, model_factory=None, pack_resolver=None, check_cancelled=None,
|
|
max_cells=None, resume_from=None, registry_dir=None):
|
|
"""Run (or resume) a causal targeting map; returns the map document.
|
|
|
|
``registry_dir`` names another confirmation registry to consult besides
|
|
``<run_dir>/registry``.
|
|
"""
|
|
spec = mapspec.load_map_spec(spec)
|
|
if max_cells is not None:
|
|
spec["budget"] = {"max_cells": int(max_cells)}
|
|
run_dir = Path(run_dir)
|
|
digest = mapspec.map_identity(spec)
|
|
dossier, estimate = _estimate(spec, run_dir)
|
|
assert_unexposed(lambda: spec_rows(spec, pack_resolver), run_dir, registry_dir)
|
|
record = ObservationStore(run_dir / MAP_DIR).open(mapspec.identity_view(spec))
|
|
map_path = record.path / MAP_FILE
|
|
grid, own = spec["grid"], estimate["site"]
|
|
others = [s for s in grid["sites"] if s != own]
|
|
with record.lock(recover=False):
|
|
previous = _previous(map_path, digest, resume_from)
|
|
widths = _widths(spec, run_dir, own, others)
|
|
cap, tested, rows = spec["budget"]["max_cells"], 0, []
|
|
per_row = len(grid["doses"])
|
|
|
|
def save():
|
|
doc = _assemble(spec, digest, rows, dossier, estimate, run_dir)
|
|
atomic_json(map_path, doc)
|
|
return doc
|
|
|
|
for site in grid["sites"]:
|
|
for scope in grid["token_scopes"]:
|
|
entry = {"site": site, "token_scope": scope, "state": "untested",
|
|
"reason": None, "row_spec_digest": None, "result": None,
|
|
"reused": False}
|
|
rows.append(entry)
|
|
if site != own:
|
|
entry["reason"] = (f"{cells_mod.EXPLORATORY}: not run in this version "
|
|
f"({widths[site]})")
|
|
continue
|
|
expected = entry["row_spec_digest"] = _row_digest(spec, site, scope)
|
|
if (site, scope) in previous:
|
|
_verified_row(previous[(site, scope)], expected, run_dir)
|
|
entry.update(state="complete", result=previous[(site, scope)]["result"],
|
|
reused=True)
|
|
tested += per_row
|
|
continue
|
|
if tested + per_row > cap:
|
|
entry["reason"] = f"cell cap reached (budget.max_cells={cap})"
|
|
continue
|
|
try:
|
|
result = run_intervention(
|
|
mapspec.row_spec(spec, site, scope), run_dir,
|
|
model_factory=model_factory, pack_resolver=pack_resolver,
|
|
check_cancelled=check_cancelled)
|
|
except (checks.InterventionRefused, SpecError) as error:
|
|
entry["reason"] = f"refused before any forward pass: {error}"
|
|
except RestorationFailed as error:
|
|
entry["reason"] = f"restoration failed (reload the model): {error}"
|
|
except BaseException as error:
|
|
entry["reason"] = f"interrupted: {type(error).__name__}: {error}"[:2000]
|
|
save()
|
|
raise
|
|
else:
|
|
path = Path(result["location"]["result"])
|
|
entry.update(state="complete", result={
|
|
"path": refs.stored_path(path, run_dir),
|
|
"sha256": refs.file_sha256(path)})
|
|
tested += per_row
|
|
save()
|
|
doc = save()
|
|
doc["location"] = {"map": str(map_path), "run_dir": str(run_dir)}
|
|
return doc
|
|
|
|
|
|
def load_map(path, *, verify=True):
|
|
"""Read a stored map; with ``verify`` recompute every cell from its referenced results."""
|
|
path = Path(path)
|
|
stored = read_json(path)
|
|
if not isinstance(stored, dict) or stored.get("schema") != MAP_SCHEMA:
|
|
raise MapRefused(f"{path} is not a causal targeting map")
|
|
run_dir = path.parents[2]
|
|
if verify:
|
|
spec = mapspec.load_map_spec(stored["spec"])
|
|
if mapspec.map_identity(spec) != stored["spec_digest"]:
|
|
raise MapRefused(f"{path} does not match its own spec digest")
|
|
for row in stored["rows"]:
|
|
if row["state"] == "complete":
|
|
_verified_row(row, _row_digest(spec, row["site"], row["token_scope"]), run_dir)
|
|
dossier, estimate = _estimate(spec, run_dir)
|
|
fresh = _assemble(spec, stored["spec_digest"], stored["rows"], dossier, estimate,
|
|
run_dir)
|
|
if [(c["id"], c["status"]) for c in fresh["cells"]] != [
|
|
(c["id"], c["status"]) for c in stored["cells"]]:
|
|
raise MapRefused(f"Stored cell statuses of {path} do not follow from its results")
|
|
stored = {**fresh, "updated_at": stored["updated_at"]}
|
|
stored["location"] = {"map": str(path), "run_dir": str(run_dir)}
|
|
return stored
|
|
|
|
|
|
def attach_cell(map_path, cell, dossier_path):
|
|
"""Attach the controlled comparison behind one decision cell to its dossier.
|
|
|
|
Exploratory (transferred-coordinate), untested and descriptive cells are
|
|
refused, and so is a row result whose uncorrected harness verdict is
|
|
supported while the corrected map cell is not (attaching it would let the
|
|
dossier read an uncorrected verdict selected from the map's family).
|
|
"""
|
|
document = load_map(map_path)
|
|
found = next((c for c in document["cells"] if c["id"] == cell), None)
|
|
if found is None:
|
|
raise MapRefused(f"Map has no cell {cell!r}")
|
|
if found["site_relation"] != "estimate_site" or found["label"] == cells_mod.EXPLORATORY:
|
|
raise MapRefused(f"Cell {cell} is {cells_mod.EXPLORATORY}; it can never be attached to "
|
|
"a dossier or change its label")
|
|
if found["status"] == cells_mod.UNTESTED:
|
|
raise MapRefused(f"Cell {cell} is untested: {found['reason']}")
|
|
if found["role"] != "decision":
|
|
raise MapRefused(f"Cell {cell} is at a descriptive dose; only the pre-registered "
|
|
"decision dose can be attached")
|
|
if not found["attachable"]:
|
|
raise MapRefused(f"Cell {cell}: {found['reason']}; its row result is not attachable")
|
|
run_dir = document["location"]["run_dir"]
|
|
result = refs.resolve_path(found["comparison"]["path"], run_dir)
|
|
family = {"map": refs.Sources(run_dir).ref(Path(map_path), ""), "cell": cell,
|
|
"p_adjusted": found["target"]["p_adjusted"]}
|
|
return verdicts.attach_result(dossier_path, result, family=family)
|
|
|
|
|
|
def summary(document):
|
|
keep = ("spec_digest", "estimate", "counts", "supported", "correction", "evidence_scope",
|
|
"location")
|
|
out = {key: document[key] for key in keep if key in document}
|
|
out["cells"] = [{"id": c["id"], "status": c["status"], "role": c["role"],
|
|
"site_relation": c["site_relation"], "reason": c["reason"],
|
|
"difference": (c["target"] or {}).get("difference"),
|
|
"p_raw": (c["target"] or {}).get("p_raw"),
|
|
"p_adjusted": (c["target"] or {}).get("p_adjusted")}
|
|
for c in document["cells"]]
|
|
return out
|
|
|
|
|
|
__all__ = ["MAP_DIR", "MAP_FILE", "MAP_SCHEMA", "MapRefused", "SCOPE_NOTE", "TRANSFER_RULE",
|
|
"attach_cell", "load_map", "run_map", "summary"]
|