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

117 lines
5.4 KiB
Python

"""Versioned interaction experiment specs (``interaction-spec.v1.schema.json``).
An interaction experiment names two ready dossier estimates, ``A`` and ``B``,
each with one operation (kind, site, token scope, units and a single
coefficient). The ordered sequences to run (``orders``: ``A>B``, ``B>A``) and
the conversation-history mode are part of the spec, so they are part of the
run identity: the canonical digest of the default-filled spec.
The shared sections (model, dataset, tasks, decoding) and each component's
operation are validated through the controlled-intervention schema by building
one intervention-spec view per component (``component_view``).
"""
from copy import deepcopy
from functools import lru_cache
import json
from pathlib import Path
from obliteratus.concepts.validation import PackError, _check
from obliteratus.experiments.spec import SpecError, _read
SCHEMA_PATH = (Path(__file__).resolve().parents[1] / "concepts" / "schema"
/ "interaction-spec.v1.schema.json")
COMPONENTS = ("A", "B")
ORDERS = {"A>B": ("A", "B"), "B>A": ("B", "A")}
HISTORY_MODES = ("single_turn", "matched_context", "diverged")
DEFAULT_STATISTICS = {"bootstrap_samples": 2000, "seed": 0, "confidence": 0.95}
DEFAULT_PROBES = 4
DEFAULT_MARGIN = 0.1
@lru_cache(maxsize=1)
def schema():
return json.loads(SCHEMA_PATH.read_text(encoding="utf-8"))
def load_interaction_spec(source):
"""Validate a spec (mapping or JSON file) and return its default-filled copy."""
spec = deepcopy(_read(source))
try:
_check(spec, schema(), schema(), "interaction_spec")
except PackError as exc:
raise SpecError(str(exc)) from exc
spec.setdefault("seed", 0)
spec.setdefault("probes", DEFAULT_PROBES)
spec.setdefault("orders", list(ORDERS))
spec["orders"] = [order for order in ORDERS if order in spec["orders"]]
spec.setdefault("history", {"mode": "single_turn"})
spec["statistics"] = {**DEFAULT_STATISTICS, **spec.get("statistics", {})}
history = spec["history"]
if (history["mode"] == "single_turn") == ("follow_up" in history):
raise SpecError("history.follow_up is required for the matched_context and diverged "
"modes and not allowed for single_turn")
a, b = (spec["components"][name] for name in COMPONENTS)
if (a["dossier"], a["estimate"], a["operation"]) == (b["dossier"], b["estimate"],
b["operation"]):
raise SpecError("Components A and B are the same estimate and operation")
from obliteratus.experiments.intervention_spec import load_intervention_spec
views = {name: load_intervention_spec(component_view(spec, name)) for name in COMPONENTS}
spec["model"], spec["dataset"] = views["A"]["model"], views["A"]["dataset"]
spec["tasks"] = views["A"]["tasks"]
protection = spec.get("protection")
if protection is not None:
protection.setdefault("margin", DEFAULT_MARGIN)
if protection["preserve_task"] not in {t["name"] for t in spec["tasks"]}:
raise SpecError(f"protection.preserve_task {protection['preserve_task']!r} is not a "
"declared task")
return spec
def component_view(spec, name):
"""The controlled-intervention spec view of one component (validation and checks).
The view's dose ladder is the component's single coefficient. It is used to
validate the shared sections and to run the harness's pre-forward checks;
it is never run as a controlled intervention and its result never exists.
"""
component = spec["components"][name]
operation = component["operation"]
view = {"schema_version": "1", "kind": "controlled_intervention",
"dossier": component["dossier"], "estimate": component["estimate"],
"model": deepcopy(spec["model"]), "dataset": deepcopy(spec["dataset"]),
"tasks": deepcopy(spec["tasks"]), "target_task": spec["target_task"],
"decoding": deepcopy(spec["decoding"]), "seed": spec.get("seed", 0),
"probes": spec.get("probes", DEFAULT_PROBES),
"operation": {key: deepcopy(operation[key]) for key in
("kind", "site", "token_scope", "units", "steer_direction")
if key in operation},
"control": {"seed": spec["control"]["seed"]}}
view["operation"].update(coefficients=[operation["coefficient"]],
decision_coefficient=operation["coefficient"])
if "direction_artifact" in component:
view["direction_artifact"] = deepcopy(component["direction_artifact"])
return view
def check_view(spec, name):
"""The view passed to the harness's dossier/estimate/geometry checks.
It uses the harness's descriptive-experiment path (as activation patching
does): every dossier, estimate, artifact and geometry refusal applies, but
no pre-registered decision dose is required, because an interaction result
never decides a dossier label.
"""
view = component_view(spec, name)
view["kind"] = "activation_patching"
view["patching"] = {"site": view["operation"]["site"]}
return view
def observation_view(spec):
return {"dataset": spec["dataset"], "tasks": spec["tasks"], "model": spec["model"]}
__all__ = ["COMPONENTS", "HISTORY_MODES", "ORDERS", "SCHEMA_PATH", "check_view",
"component_view", "load_interaction_spec", "observation_view", "schema"]