Files
OBLITERATUS/obliteratus/experiments/interaction_runner.py
Joseph MaglyandClaude Opus 5.5 10812efc0e feat(experiments): interaction and preservation experiments
Add two-estimate interaction experiments (F14): measure whether two
ready dossier estimates interact when applied together, in which order,
and under which conversation history, without treating orthogonality as
independence.

- Arms: baseline, A, B, each declared order, matched random controls
  for each, optional protected arms (one component restricted to the
  complement of the other's span) and a baseline replay; restoration is
  checked after every arm and a failure stops the attempt.
- Orders and history mode (single turn, matched context, diverged) are
  part of the spec digest. Components pass the harness's model and
  independence checks; confirmation-locked rows are refused.
- Interaction is the per-example difference-in-differences of task
  success with a seeded paired bootstrap interval and an exact sign
  test, compared against the random-control sequence; order sensitivity
  is reported. "Independent" is never an outcome, and geometry never
  feeds one.
- Protection reports projection preservation and behaviour preservation
  separately.
- Results link to dossiers by reference as descriptive evidence only,
  re-verified on read, never granting labels. `obliteratus experiments
  interact` and an Explorer interaction view.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 09:26:34 -05:00

322 lines
16 KiB
Python

"""Interaction and preservation experiments between two ready dossier estimates.
``run_interaction`` runs one interaction spec (``interaction_spec``) and stores
it beside the dossiers' run as ``<run>/interactions/<spec-digest>/attempts/NNNN/``
with the observation store's attempt lifecycle. Both components pass every
pre-forward check of the controlled intervention harness (dossier, estimate,
artifact, geometry, evaluation independence); evaluation rows registered or
exposed in a confirmation registry are refused.
Arms on the same examples, in this order: baseline; A, its matched random
control, B, its matched random control; for each declared order the sequence
(``A>B`` / ``B>A``) and the same sequence of the two random controls; the
protected-span arms when ``protection`` is declared and supported; a
fresh-baseline replay that must reproduce the baseline completions exactly.
Restoration is checked after every arm. Controls are seeded (A: ``control.seed``,
B: ``control.seed + 1``), norm/rank-matched and, at a shared site, orthogonal to
both components. Results are descriptive and never change a dossier label.
"""
from __future__ import annotations
from copy import deepcopy
from pathlib import Path
import torch
from obliteratus.analysis import dossier_interventions as verdicts
from obliteratus.analysis import dossier_refs as refs
from obliteratus.analysis import interaction_report as report
from obliteratus.analysis import interaction_stats as stats
from obliteratus.concepts import restoration
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 import interaction_protect as protection
from obliteratus.experiments import interaction_spec as ispec
from obliteratus.experiments.interaction_arms import ArmRunner
from obliteratus.experiments.intervention_ops import OPERATIONS
from obliteratus.experiments.intervention_runner import RestorationFailed
from obliteratus.experiments.registry import guard_evaluation
from obliteratus.experiments.registry_store import assert_unexposed
from obliteratus.experiments.runner import _Cancel, _tool
from obliteratus.experiments.store import TERMINAL, ObservationStore, StoreError, atomic_json, \
now, read_json
INTERACTION_DIR = "interactions"
RESULT_SCHEMA = "obliteratus.interaction-result"
RESULT_VERSION = 1
LABEL_EFFECT = ("none: interaction results are descriptive evidence; they never grant or change "
"a dossier evidence label (only a controlled intervention result can)")
REQUIRED = ("spec", "spec_digest", "components", "arms", "analysis", "evidence_scope",
"inputs", "restoration_rule")
def run_interaction(spec, run_dir, *, model_factory=None, pack_resolver=None,
check_cancelled=None, rerun=False, operations=None, registry_dir=None):
"""Run (or return the completed result of) one interaction spec.
``model_factory(model_section)`` returns an object with ``model``, ``tokenizer``
and optional ``cleanup()``. ``operations`` replaces the operation registry
(testing only). ``registry_dir`` names another confirmation registry to consult.
"""
spec = ispec.load_interaction_spec(spec)
run = ObservationStore(Path(run_dir) / INTERACTION_DIR).open(spec)
latest = run.latest_complete()
if latest is not None and not rerun:
return load_interaction_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, registry_dir)
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_interaction_result(attempt.path)
def _components(spec, base):
out = {}
for name in ispec.COMPONENTS:
prepared = checks.prepare(ispec.check_view(spec, name), base)
try:
verdicts.preregistered_decision(prepared["dossier"]["hypothesis"],
ispec.component_view(spec, name)["operation"],
spec["target_task"])
matches = True
except ValueError:
matches = False
out[name] = {"prepared": prepared, "matches_preregistered_decision": matches}
return out
def _session(spec, base, run, attempt, model_factory, pack_resolver, cancel, state, operations,
registry_dir):
started = now()
parts = _components(spec, base)
_, tasks, data = _inputs.resolve_dataset(ispec.observation_view(spec), pack_resolver)
examples = [e for t in tasks for e in t.examples]
assert_unexposed(examples, base, registry_dir)
guard_evaluation(base, spec, run.digest, tasks)
handle = (model_factory or _inputs.default_model_factory)(deepcopy(spec["model"]))
try:
model, tokenizer = handle.model, handle.tokenizer
adapter = _inputs.require_unmodified(model)
targets, independence = {}, {}
for name, part in parts.items():
view = ispec.check_view(spec, name)
targets[name] = checks.verify_model(part["prepared"], view, model, tokenizer, adapter)
independence[name] = checks.check_evaluation_independence(
part["prepared"], data, tasks, tokenizer)
ctx = {"spec": spec, "model": model, "tokenizer": tokenizer, "tasks": tasks,
"targets": targets, "attempt": attempt, "run": run, "cancel": cancel,
"operations": operations, "device": targets["A"]["identity"]["device"]}
plan = _plan(ctx)
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()
components, model_ids = {}, [spec["model"]["id"]]
for name, part in parts.items():
prepared = part["prepared"]
dossier, estimate = prepared["dossier"], prepared["estimate"]
model_ids.append(dossier["provenance"]["model"]["model_id"])
components[name] = {
"dossier": {"path": refs.stored_path(prepared["path"], base),
"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"], "coordinate": estimate["coordinate"]},
"operation": {**spec["components"][name]["operation"],
"module": targets[name]["module"]},
"target": targets[name]["record"], "control": plan["controls"][name],
"independence": independence[name],
"matches_preregistered_decision": part["matches_preregistered_decision"]}
return {"schema": RESULT_SCHEMA, "schema_version": RESULT_VERSION,
"kind": "interaction_experiment", "spec_digest": run.digest, "spec": spec,
"attempt": attempt.number, "state": "complete", "started_at": started,
"completed_at": now(),
"evidence_scope": verdicts.evidence_scope(spec["model"]["model_class"], model_ids),
"label_effect": LABEL_EFFECT, "components": components,
"orders": spec["orders"], "history_mode": spec["history"]["mode"],
"inputs": {"dataset": data, "model": targets["A"]["identity"]},
"restoration_rule": f"after every arm: {restoration.RULE}; otherwise stop",
**body, "tool": _tool()}
def _basis(*tensors):
rows = torch.cat([t.detach().double().cpu().reshape(-1, t.shape[-1]) for t in tensors
if t is not None])
_, singular, vh = torch.linalg.svd(rows, full_matrices=False)
return vh[singular > 1e-8 * singular[0]].contiguous()
def _probe(ctx):
task = next(t for t in ctx["tasks"] if t.name == ctx["spec"]["target_task"])
inputs = [_inputs.encode(ctx["tokenizer"], e.prompt)
for e in task.examples[:ctx["spec"]["probes"]]]
model, device = ctx["model"], ctx["device"]
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, inputs
def _plan(ctx):
targets, spec = ctx["targets"], ctx["spec"]
same_site = targets["A"]["geometry"]["site"] == targets["B"]["geometry"]["site"]
vectors = {name: targets[name]["vector"] for name in ispec.COMPONENTS}
spans = {name: _basis(vectors[name], targets[name].get("span")) for name in vectors}
details = {name: targets[name]["record"]["orthogonalization"] for name in vectors}
records = {}
for offset, name in enumerate(ispec.COMPONENTS):
seed, vector = spec["control"]["seed"] + offset, vectors[name]
span = None
if vector.ndim == 1:
others = [targets[name].get("span")] + ([vectors[o] for o in vectors if o != name]
if same_site else [])
span = _basis(vector, *others) if (targets[name].get("span") is not None
or same_site) else None
control, record = controls.build_control(vector, seed, span)
controls.check_matched(vector, control, record, span)
record["orthogonal_to"] = ("both components (shared site)" if same_site and span is not None
else "its own component only")
vectors[f"random:{name}"], records[name] = control, record
details[f"random:{name}"] = record.get("orthogonalization")
probe, probe_inputs = _probe(ctx)
plan = {"vectors": vectors, "details": details, "controls": records, "probe": probe,
"probe_inputs": probe_inputs, "same_site": same_site, "spans": spans,
"coverage": None}
if spec.get("protection") is not None:
coverage = {}
for name in ispec.COMPONENTS:
other = "B" if name == "A" else "A"
tensor, record = protection.protect(vectors[name], spans[other], same_site=same_site)
record["protected_against"] = other
coverage[name] = record
if tensor is not None:
key = report.protected_name(name)
vectors[key], details[key] = tensor, {"method": "protected complement",
**record}
plan["coverage"] = coverage
return plan
def _steps(*pairs):
return [{"component": c, "vector": v, "direction": d} for c, v, d in pairs]
def _execute(ctx, plan):
spec, runner = ctx["spec"], ArmRunner(ctx, plan)
coverage = plan["coverage"]
measured = coverage is not None and plan["same_site"]
def measure(*names):
if not measured:
return None
return {n: (ctx["targets"][n]["layer"], plan["spans"]["B" if n == "A" else "A"])
for n in names}
baseline = runner.run("baseline", "baseline", [], measure=measure("A", "B"))
for name in ispec.COMPONENTS:
runner.run(name, "single", _steps((name, name, "estimate")), measure=measure(name))
runner.run(f"random:{name}", "single_control",
_steps((name, f"random:{name}", "random")))
for order in spec["orders"]:
first, second = ispec.ORDERS[order]
runner.run(order, "sequence", _steps((first, first, "estimate"),
(second, second, "estimate")))
runner.run(f"random:{order}", "sequence_control",
_steps((first, f"random:{first}", "random"),
(second, f"random:{second}", "random")))
for name in ispec.COMPONENTS if coverage else ():
key = report.protected_name(name)
if key in plan["vectors"]:
runner.run(key, "protected", _steps((name, key, "protected")),
measure=measure(name))
replay = runner.run("replay", "replay", [])
replay["identical_to_baseline"] = (replay["completions"] == baseline["completions"]
and replay["history"].get("turn_1")
== baseline["history"].get("turn_1"))
if not replay["identical_to_baseline"]:
raise RestorationFailed("replay", None, {**replay["restoration"],
"identical_to_baseline": False})
analysis = report.analyze(spec, runner.arms)
analysis["geometry"] = stats.geometry((plan["vectors"]["A"], plan["vectors"]["B"]),
(ctx["targets"]["A"]["geometry"]["site"],
ctx["targets"]["B"]["geometry"]["site"]))
if coverage is not None:
analysis["protection"] = _coverage(coverage, runner, measured)
return {"arms": runner.arms, "analysis": analysis}
def _coverage(coverage, runner, measured):
out = {}
for name, record in coverage.items():
entry = dict(record)
if measured:
before = runner.projections["baseline"][name]
entry["projected_quantity"] = {"unprotected": protection.projection_change(
runner.projections[name][name], before)}
key = report.protected_name(name)
if key in runner.projections:
entry["projected_quantity"]["protected"] = protection.projection_change(
runner.projections[key][name], before)
out[name] = entry
return {"rule": protection.RULE, "components": out}
def validate_result(result):
if not isinstance(result, dict) or result.get("schema") != RESULT_SCHEMA \
or result.get("schema_version") != RESULT_VERSION:
raise StoreError("Not an interaction result")
missing = [key for key in REQUIRED if key not in result]
if missing:
raise StoreError(f"Interaction result is missing {', '.join(missing)}")
return result
def load_interaction_result(source, *, verify=False):
"""Load a result from ``result.json`` or an attempt directory.
With ``verify`` the spec digest and the statistics are recomputed from the
stored spec and per-example records.
"""
path = Path(source)
if path.is_dir():
path = path / "result.json"
result = validate_result(read_json(path))
if verify:
from obliteratus.experiments.spec import spec_digest
if spec_digest(ispec.load_interaction_spec(result["spec"])) != result["spec_digest"]:
raise StoreError(f"{path} does not match its own spec digest")
report.verify_analysis(result)
result["location"] = {"result": str(path), "run_dir": str(path.parents[4])}
return result
__all__ = ["INTERACTION_DIR", "LABEL_EFFECT", "RESULT_SCHEMA", "load_interaction_result",
"run_interaction", "validate_result"]