mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 18:44:41 +01:00
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>
177 lines
8.9 KiB
Python
177 lines
8.9 KiB
Python
"""Interaction statistics, protection geometry, storage, dossier links, CLI and Explorer."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import shutil
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
import torch
|
|
|
|
from obliteratus.analysis import interaction_stats as stats
|
|
from obliteratus.analysis.dossier_interactions import attach_interaction, describe, ledger_path
|
|
from obliteratus.analysis.target_dossier import build_dossier, read_dossier, write_dossier
|
|
from obliteratus.concepts.pack import ConceptPack
|
|
from obliteratus.experiments import interaction_protect as protection
|
|
from obliteratus.experiments import registry as reg
|
|
from obliteratus.experiments.intervention_checks import InterventionRefused
|
|
from obliteratus.experiments.interaction_runner import load_interaction_result
|
|
from obliteratus.experiments.store import StoreError
|
|
from tests.test_controlled_interventions import DECLARED
|
|
from tests.test_controlled_interventions import make_spec as controlled_spec
|
|
from tests.test_interaction_experiments import CH1, CH2, build_env, component, make_spec, run_rule
|
|
from tests.test_target_dossier import HYPOTHESIS, LEXICAL, REPORT
|
|
|
|
pytestmark = pytest.mark.cpu
|
|
SETTINGS = {"bootstrap_samples": 300, "seed": 0, "confidence": 0.95}
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def env(tmp_path_factory):
|
|
return build_env(tmp_path_factory.mktemp("interaction-reports"))
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def stored(env):
|
|
spec = make_spec(env["pack"], component(CH1, 10.0), component(CH2, 10.0))
|
|
return run_rule(env, spec, "and")
|
|
|
|
|
|
def test_statistics_edge_cases():
|
|
assert stats.sign_test(0, 0) is None
|
|
assert stats.sign_test(5, 0) == pytest.approx(2 / 32)
|
|
assert stats.sign_test(3, 3) == 1.0
|
|
assert stats.paired_interval([], samples=10, seed=0, confidence=0.9) is None
|
|
empty = stats.summarize([], SETTINGS, excluded=2)
|
|
assert empty["estimate"] is None and stats.classify(empty, "x", "y") == stats.INCONCLUSIVE
|
|
# Interval excludes zero but too few pairs for the sign test: inconclusive, never detected.
|
|
small = stats.summarize([1, 1], SETTINGS, excluded=0)
|
|
assert small["interval"][0] > 0 and small["p_value"] == 0.5
|
|
assert stats.classify(small, "found", "missing") == stats.INCONCLUSIVE
|
|
arm = lambda values: {"t": dict(enumerate(values))} # noqa: E731
|
|
found = stats.interaction(arm([False, False, None]), arm([False, True, True]),
|
|
arm([False, True, True]), arm([True, True, True]), "t", SETTINGS)
|
|
assert found["n"] == 2 and found["excluded"] == 1
|
|
assert found["estimate"] == 0.0 # (1-0-0+0 + 1-1-1+0) / 2
|
|
assert stats.geometry((torch.ones(4), torch.ones(4)), ("layer.0", "layer.1"))[
|
|
"comparable"] is False
|
|
same = stats.geometry((torch.tensor([1.0, 0, 0]), torch.tensor([1.0, 1.0, 0])),
|
|
("layer.0", "layer.0"))
|
|
assert same["orthogonal"] is False and same["principal_cosines"][0] == pytest.approx(2 ** -0.5)
|
|
|
|
|
|
def test_protection_geometry():
|
|
span = protection.orthonormal_rows(torch.tensor([1.0, 1.0, 0.0, 0.0]))
|
|
vector = torch.tensor([1.0, 0.0, 0.0, 0.0])
|
|
protected, record = protection.protect(vector, span, same_site=True)
|
|
assert record["status"] == "supported" and record["residual_fraction"] == pytest.approx(
|
|
2 ** -0.5)
|
|
assert float((protected.double() @ span.T).abs().max()) < 1e-12
|
|
inside, record = protection.protect(torch.tensor([2.0, 2.0, 0, 0]), span, same_site=True)
|
|
assert inside is None and record["status"] == "unsupported"
|
|
other, record = protection.protect(vector, span, same_site=False)
|
|
assert other is None and "different sites" in record["reason"]
|
|
basis = torch.tensor([[1.0, 0, 0, 0], [0, 0, 1.0, 0]])
|
|
rows, record = protection.protect(basis, span, same_site=True)
|
|
assert rows.shape == (2, 4) and record["status"] == "supported"
|
|
assert float((rows.double() @ span.T).abs().max()) < 1e-12
|
|
before = [torch.tensor([1.0]), torch.tensor([2.0])]
|
|
assert protection.projection_change(before, before)["preserved"] is True
|
|
assert protection.projection_change([torch.tensor([1.5]), before[1]], before)[
|
|
"preserved"] is False
|
|
|
|
|
|
def test_stored_result_is_reverified_and_tampering_is_refused(env, stored, tmp_path):
|
|
again = run_rule(env, make_spec(env["pack"], component(CH1, 10.0), component(CH2, 10.0)),
|
|
"and")
|
|
assert again["attempt"] == stored["attempt"]
|
|
copy = tmp_path / "interactions" / "d" / "attempts" / "0001"
|
|
copy.mkdir(parents=True)
|
|
document = json.loads(Path(stored["location"]["result"]).read_text())
|
|
document["analysis"]["interactions"]["flip"]["A>B"]["target"]["estimate"] = 0.0
|
|
(copy / "result.json").write_text(json.dumps(document))
|
|
with pytest.raises(ValueError, match="do not follow"):
|
|
load_interaction_result(copy, verify=True)
|
|
document["spec"]["control"]["seed"] = 99
|
|
(copy / "result.json").write_text(json.dumps(document))
|
|
with pytest.raises(StoreError, match="spec digest"):
|
|
load_interaction_result(copy, verify=True)
|
|
(copy / "result.json").write_text(json.dumps({"schema": "other"}))
|
|
with pytest.raises(StoreError):
|
|
load_interaction_result(copy)
|
|
|
|
|
|
def test_dossier_link_is_descriptive_and_reverified(env, stored, tmp_path):
|
|
run = tmp_path / "run"
|
|
shutil.copytree(env["run"], run)
|
|
result = run / stored["location"]["result"].split("/run/", 1)[1]
|
|
dossier = run / "dossiers" / "alpha.json"
|
|
label = read_dossier(dossier)[0]["evidence_label"]
|
|
attached = attach_interaction(dossier, result)
|
|
assert attached["attached"]["grants_label"] is False
|
|
assert attached["attached"]["components"] == ["A", "B"]
|
|
assert read_dossier(dossier)[0]["evidence_label"] == label
|
|
with pytest.raises(ValueError, match="already attached"):
|
|
attach_interaction(dossier, result)
|
|
[entry] = describe(dossier)["entries"]
|
|
assert entry["status"] == "verified" and entry["headline"][0]["outcome"] == stats.DETECTED
|
|
other = build_dossier("beta", run, hypothesis={**HYPOTHESIS, **DECLARED}, discovery=REPORT,
|
|
estimates=[CH1], confound_resolutions=[LEXICAL])
|
|
write_dossier(other, run)
|
|
with pytest.raises(ValueError, match="not run against"):
|
|
attach_interaction(run / "dossiers" / "beta.json", result)
|
|
assert describe(run / "dossiers" / "beta.json")["entries"] == []
|
|
result.write_text(result.read_text() + "\n")
|
|
[entry] = describe(dossier)["entries"]
|
|
assert entry["status"] == "invalid" and entry["grants_label"] is False
|
|
assert ledger_path(run, "alpha").is_file()
|
|
|
|
|
|
def test_registered_confirmation_rows_are_refused(env, tmp_path):
|
|
run = tmp_path / "run"
|
|
shutil.copytree(env["run"], run)
|
|
reg.register(run, controlled_spec(env["pack"], estimate=CH2),
|
|
pack_resolver=ConceptPack.load, actor="tester")
|
|
spec = make_spec(env["pack"], component(CH1, 10.0), component(CH2, 10.0),
|
|
control={"seed": 3})
|
|
env["model"].rule = "and"
|
|
with pytest.raises(InterventionRefused, match="registered confirmation set"):
|
|
run_rule(dict(env, run=run), spec, "and")
|
|
|
|
|
|
def test_not_ready_estimate_is_refused_before_forward_passes(env):
|
|
spec = make_spec(env["pack"], component("channel:layer.0:7", 1.0), component(CH2, 1.0))
|
|
with pytest.raises(InterventionRefused, match="not a ready estimate"):
|
|
run_rule(env, spec, "second")
|
|
|
|
|
|
def test_cli_show_attach_list_and_explorer_rows(env, stored, capsys, tmp_path):
|
|
from obliteratus import ui_evidence_explorer as explorer
|
|
from obliteratus.cli import main
|
|
from obliteratus.ui_evidence_interactions import INTERACTION_COLUMNS, interaction_rows
|
|
main(["experiments", "interact", "show", stored["location"]["result"]])
|
|
shown = json.loads(capsys.readouterr().out)
|
|
assert shown["headline"][0]["outcome"] == stats.DETECTED
|
|
assert shown["label_effect"].startswith("none") and shown["geometry"]["orthogonal"]
|
|
run = tmp_path / "run"
|
|
shutil.copytree(env["run"], run)
|
|
result = run / stored["location"]["result"].split("/run/", 1)[1]
|
|
dossier = str(run / "dossiers" / "alpha.json")
|
|
main(["experiments", "interact", "attach", str(result), "--dossier", dossier])
|
|
assert json.loads(capsys.readouterr().out)["attached"]["role"] == "descriptive"
|
|
main(["experiments", "interact", "list", "--dossier", dossier])
|
|
assert json.loads(capsys.readouterr().out)["entries"][0]["status"] == "verified"
|
|
rows = interaction_rows(run)
|
|
assert rows and all(len(row) == len(INTERACTION_COLUMNS) for row in rows)
|
|
flip = [row for row in rows if row[1] == "flip"]
|
|
assert {row[2] for row in flip} == {"A>B", "B>A"}
|
|
assert all(row[11] == stats.DETECTED and "not evidence of independence" in row[14]
|
|
for row in flip)
|
|
assert explorer.interaction_rows({"run_dir": run}) == rows
|
|
result.write_text("{}")
|
|
assert interaction_rows(run)[0][1].startswith("unavailable")
|
|
with pytest.raises(SystemExit) as refused:
|
|
main(["experiments", "interact", "show", str(result)])
|
|
assert refused.value.code == 4
|