Files
OBLITERATUS/tests/test_ui_evidence_explorer.py
Joseph MaglyandClaude Opus 5.5 ca94280c9b feat(analysis): labeled activation sets and model-level subspace validation
Connect the full-subspace report to real captured activations (F08b)
and add the labeled-activation foundation that candidate discovery and
example selection build on.

- Labeled sets: capture a pack's positive, control, neighbour and
  optional battery sets for declared dataset roles. Labels (set, kind,
  role, group, family, record id, input digest) live in a sidecar whose
  digest, the capture identity and the dataset revision are bound in a
  labeled-set manifest; prompt text is never stored. Locked test needs
  explicit acknowledgement. Legacy packs are fit-only, bound to the pack
  bytes.
- Loader: recomputes rows and capture identity and refuses any
  mismatch, naming the field. Selection helpers pick rows by role, set
  and kind, and build deterministic group-disjoint folds. A capture that
  failed or was cancelled is replaced on retry; complete sets never are.
- Subspace report on a labeled set with the pack's extracted rank-k
  basis; groups come from dataset independence units, and the
  basis-fit overlap is read from the basis's dataset access receipt.
- `obliteratus experiments subspace` and a Python API write versioned
  reports next to the run; the Explorer lists them as a descriptive
  table.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 21:46:05 -05:00

436 lines
22 KiB
Python

"""Evidence explorer: scorecard drill-down, activations, text access, views and export.
Handlers and the layout's callbacks run without a browser against real runs of
the tiny offline model. The CPU lane has no Gradio, so the layout is built
against the recording stand-in from the Observation Runs tests; one
``operator_ui`` test constructs the real Blocks offline.
"""
from __future__ import annotations
import json
import os
from pathlib import Path
import shutil
import subprocess
import sys
import pytest
import torch
from obliteratus import ui_evidence_explorer as explorer
from obliteratus import ui_evidence_views as views
from obliteratus.concepts.pack import ConceptPack
from obliteratus.experiments import compare_results, run_observation
from obliteratus.ui_evidence_explorer_layout import build_evidence_explorer
from tests.test_concept_datasets import FIXTURE, alter
from tests.test_observation_runs import factory, make_spec, tiny_gpt2
from tests.test_ui_observation_runs import _FakeGradio
def pack_with(root, answers):
"""The F02 fixture pack with chosen ``answer`` targets by row id."""
files = dict(ConceptPack.load(FIXTURE).files)
def annotate(rows):
for index, row in enumerate(rows):
row.update(answer=answers.get(row["id"], f"amber {index}"),
alias=["kite", f"prism {index}"])
files = alter(files, "positive.jsonl", annotate)
for name, data in files.items():
(root / name).parent.mkdir(parents=True, exist_ok=True)
(root / name).write_bytes(data)
return root
def zero_gpt2():
"""All-zero weights: every decoder output is exactly zero and completions are empty."""
model = tiny_gpt2()
with torch.no_grad():
for parameter in model.parameters():
parameter.zero_()
return model
def run_dir(spec, store, **options):
return run_observation(spec, store, **options)["location"]["run_dir"]
@pytest.fixture(scope="module")
def runs(tmp_path_factory):
root = tmp_path_factory.mktemp("explorer")
probe = run_observation(make_spec(pack_with(root / "probe-pack", {}), profile=False),
root / "probe", model_factory=factory())
step = Path(probe["location"]["run_dir"]) / probe["steps"]["generate-answers"]["path"]
completions = {r["example_id"]: r["completion"]
for r in json.loads((step / "generations.json").read_text())["rows"]}
# One answer target equals the model's own completion, so the task mixes pass and fail.
pack = pack_with(root / "pack", {"p4": completions["p4"]})
spec = make_spec(pack, profile=False)
return {
"root": root, "spec": spec, "completions": completions,
"mixed": run_dir(spec, root / "store", model_factory=factory()),
"zero": run_dir(spec, root / "zero", model_factory=factory(model=zero_gpt2())),
"none": run_dir(make_spec(pack, profile=False, capture=None), root / "none",
model_factory=factory()),
"locked": run_dir(make_spec(pack, profile=False,
dataset={"role": "test", "allow_locked_test": True}),
root / "locked", model_factory=factory()),
}
def aggregate(state, task, endpoint):
return next(r for r in explorer.aggregates(state) if r[:2] == [task, endpoint])
@pytest.mark.cpu
def test_flow_from_failing_score_to_activations_and_replayed_export(runs, tmp_path):
state = explorer.load_run(runs["mixed"])
by_spec = runs["root"] / "spec.json"
by_spec.write_text(json.dumps(runs["spec"]))
assert explorer.load_run("", str(by_spec), str(runs["root"] / "store"))["run_dir"] == \
state["run_dir"]
assert aggregate(state, "answers", "task_success")[2].startswith("0.333 (1/3)")
assert "scorer `exact_answer` v1" in explorer.definitions(state)
# Every aggregate drills down to exactly its counted and denominator examples.
for task in state["tasks"]:
card = state["result"]["scorecard"]["tasks"][task]
for endpoint in ("task_success", "invalid_output", "abstention"):
assert len(explorer.filter_examples(state, explorer.drill(state, task, endpoint))) \
== card[endpoint]["count"]
assert len(explorer.filter_examples(
state, explorer.drill(state, task, endpoint, "denominator"))) == card[endpoint]["n"]
assert len(explorer.filter_examples(state, explorer.drill(state, task, "excluded"))) == \
len(card["excluded"])
for group, counts in card["groups"].items():
assert len(explorer.filter_examples(state, explorer.drill_group(state, task, group))) \
== counts["n"]
failing = explorer.filter_examples(state, {"task": "answers", "outcomes": ["failed"]})
assert [r["example_id"] for r in failing] == ["p3", "p5"]
assert [row[3] for row in explorer.example_rows(failing)] == ["failed", "failed"]
detail = explorer.example_detail(state, "answers", "p3")
assert detail["record"]["task_success"] == "False"
assert len(detail["record"]["completion_sha256"]) == 64
assert len(detail["record"]["capture_input_sha256"]) == 64
assert [row[0] for row in detail["activations"]] == ["layer.0", "layer.1"]
assert all(float(row[4]) > 0 for row in detail["activations"])
grid = explorer.heatmap(state, [r["example_id"] for r in failing])
assert grid["examples"] == ["p3", "p5"] and all(v > 0 for row in grid["cells"] for v in row)
assert "descriptive" in explorer.heatmap_html(grid)
exported = views.export_batch(state, tmp_path / "replay" / "spec.json",
{"task": "answers", "outcomes": ["failed"]},
{"task": "answers", "example_id": "p3"})
assert exported["spec"].read_bytes() == (state["run_dir"] / "spec.json").read_bytes()
sidecar = json.loads(exported["sidecar"].read_text())
assert sidecar["view"]["filters"]["outcomes"] == ["failed"]
assert exported["command"].startswith(f"obliteratus experiments run {exported['spec']} --store")
replay = run_observation(exported["spec"], tmp_path / "replay-store", model_factory=factory())
assert replay["spec_digest"] == exported["spec_digest"] == state["result"]["spec_digest"]
assert compare_results(state["result"]["location"]["result"], replay)["equal"]
assert views.compare_with(state, replay["location"]["run_dir"])["report"]["equal"]
with pytest.raises(views.ViewError, match="already exists"):
views.export_batch(state, exported["spec"], {})
with pytest.raises(views.ViewError, match=".json"):
views.export_batch(state, tmp_path / "spec.txt", {})
@pytest.mark.cpu
def test_missing_values_read_unavailable_and_measured_zero_reads_zero(runs, tmp_path):
zero = explorer.load_run(runs["zero"])
assert aggregate(zero, "answers", "task_success")[2].startswith(
"unavailable: denominator is 0 (0/0)")
assert aggregate(zero, "answers", "invalid_output")[2].startswith("1.000 (3/3)")
assert aggregate(zero, "answers", "excluded")[2] == "3"
row = explorer.example_rows(explorer.filter_examples(zero, {"outcomes": ["excluded"]}))[0]
assert row[3] == "invalid" and row[4] == "unavailable: excluded (invalid_output)"
assert row[6] == "unavailable: excluded (invalid_output)" and row[5] == "True"
detail = explorer.example_detail(zero, "answers", "p3")
assert detail["record"]["abstained"].startswith("unavailable")
# Excluded from scoring, but the prompt activations were measured: exactly zero.
assert [r[4:] for r in detail["activations"]] == [["0", "0", "0"], ["0", "0", "0"]]
grid = explorer.heatmap(zero, ["p3"])
assert grid["cells"] == [[0.0], [0.0]] and ">0</td>" in explorer.heatmap_html(grid)
mixed = explorer.load_run(runs["mixed"])
assert aggregate(mixed, "answers", "invalid_output")[2].startswith("0 (0/3)")
assert explorer.number(0.0) == "0" and explorer.number(-0.0) == "0"
none = explorer.load_run(runs["none"])
rows, reason = explorer.activation_rows(none, "p3")
assert rows == [] and reason == explorer.NO_CAPTURE
assert explorer.example_rows(none["rows"][:1])[0][7] == explorer.NO_CAPTURE
assert explorer.example_detail(none, "answers", "p3")["record"]["capture_input_sha256"] == \
explorer.NO_CAPTURE
assert explorer.NO_CAPTURE in explorer.heatmap_html(explorer.heatmap(none, ["p3"]))
assert explorer.filter_examples(none, {"evidence": ["capture unavailable"]}) == none["rows"]
assert "captured no activations" in explorer.summary(none)
broken = tmp_path / "broken"
shutil.copytree(Path(runs["mixed"]).parent, broken)
state = explorer.load_run(broken / Path(runs["mixed"]).name)
next((state["run_dir"] / state["result"]["capture"]["path"] / "raw").glob("*")).unlink()
rows, reason = explorer.activation_rows(state, "p3")
assert rows == [] and reason.startswith("unavailable: capture could not be loaded")
grid = explorer.heatmap(state, ["p3", "p4"])
assert grid["cells"] == [[None, None]] * 2
assert "<em>unavailable</em>" in explorer.heatmap_html(grid)
state["batches"]["ghost"] = 99
state.pop("_capture")
shutil.copytree(Path(runs["mixed"]) / "attempts", state["run_dir"] / "attempts",
dirs_exist_ok=True)
assert explorer.activation_rows(state, "ghost") == (
[], "unavailable: example is not in the capture")
@pytest.mark.cpu
def test_text_is_hidden_by_default_and_never_shown_for_locked_test(runs, tmp_path):
state = explorer.load_run(runs["mixed"])
assert explorer.example_text(state, "answers", "p3") == {"shown": False,
"message": explorer.TEXT_HIDDEN}
shown = explorer.example_text(state, "answers", "p3", show_text=True)
assert shown["shown"] and shown["completion"] == runs["completions"]["p3"]
assert shown["prompt"] == explorer.PROMPT_UNAVAILABLE and shown["prompt_tokens"] > 0
for name, detail in (("detail", explorer.example_detail(state, "answers", "p3")),
("rows", explorer.example_rows(state["rows"]))):
assert runs["completions"]["p3"] not in json.dumps(detail), name
locked = explorer.load_run(runs["locked"])
assert locked["locked"]
example = locked["rows"][0]
assert explorer.example_text(locked, example["task"], example["example_id"], True) == {
"shown": False, "message": explorer.TEXT_LOCKED}
copy = tmp_path / "tampered"
shutil.copytree(Path(runs["mixed"]).parent, copy)
tampered = explorer.load_run(copy / Path(runs["mixed"]).name)
path = tampered["run_dir"] / tampered["result"]["steps"]["generate-answers"]["path"] / \
"generations.json"
payload = json.loads(path.read_text())
payload["rows"][0]["completion"] = "edited"
path.write_text(json.dumps(payload))
assert "does not match" in explorer.example_text(tampered, "answers", "p3", True)["message"]
path.write_text("{}")
assert explorer.example_text(tampered, "answers", "p3", True)["message"].startswith(
"unavailable: no stored completion")
@pytest.mark.cpu
def test_saved_views_round_trip_and_malformed_views_are_refused(runs):
state = explorer.load_run(runs["mixed"])
filters = {"task": "answers", "group": "p-g3", "outcomes": ["failed", "failed"],
"evidence": ["captured"]}
path = views.save_view(state, "failing-g3", filters, {"task": "answers", "example_id": "p3"})
assert path == state["run_dir"] / "views" / "failing-g3.json"
assert "failing-g3" in views.list_views(state)
loaded = views.load_view(state, "failing-g3")
assert loaded["filters"] == {**filters, "outcomes": ["failed"]}
assert loaded["selection"] == {"task": "answers", "example_id": "p3"}
assert json.loads(path.read_text()) == loaded
assert explorer.filter_examples(state, loaded["filters"])[0]["example_id"] == "p3"
good = json.loads(path.read_text())
bad = {
"extra-key": {**good, "show_text": True},
"schema": {**good, "schema": "obliteratus.evidence-view/9"},
"digest": {**good, "spec_digest": "sha256:" + "0" * 64},
"attempt": {**good, "attempt": True},
"saved": {**good, "saved_at": 5},
"outcome": {**good, "filters": {**good["filters"], "outcomes": ["lost"]}},
"task": {**good, "filters": {**good["filters"], "task": "nope"}},
"group": {**good, "filters": {**good["filters"], "group": "nope"}},
"filter-key": {**good, "filters": {**good["filters"], "text": True}},
"selection": {**good, "selection": {"task": "answers", "example_id": "zz"}},
"selection-shape": {**good, "selection": ["answers", "p3"]},
}
for name, value in bad.items():
(state["run_dir"] / "views" / f"bad-{name}.json").write_text(json.dumps(value))
with pytest.raises(views.ViewError):
views.load_view(state, f"bad-{name}")
folder = state["run_dir"] / "views"
(folder / "bad-json.json").write_text("{")
(folder / "bad-nan.json").write_text('{"schema": NaN}')
(folder / "bad-big.json").write_text(" " * (views.MAX_VIEW_BYTES + 1))
for name, message in (("bad-json", "valid JSON"), ("bad-nan", "NaN"), ("bad-big", "64 KiB"),
("missing", "No saved view")):
with pytest.raises(views.ViewError, match=message):
views.load_view(state, name)
for name in ("../escape", ".hidden", "", "a/b"):
with pytest.raises(views.ViewError, match="View names"):
views.save_view(state, name, {})
@pytest.mark.cpu
def test_load_and_filter_errors_are_explicit(runs):
with pytest.raises(ValueError, match="Enter a result"):
explorer.load_run()
state = explorer.load_run(runs["mixed"])
for filters, message in (({"outcomes": ["lost"]}, "outcome"), ({"evidence": "x"}, "evidence"),
({"task": "nope"}, "task"), ({"bogus": 1}, "Filters")):
with pytest.raises(ValueError, match=message):
explorer.filter_examples(state, filters)
with pytest.raises(ValueError, match="No aggregate"):
explorer.drill(state, "answers", "raw_change")
with pytest.raises(ValueError, match="No example"):
explorer.example_detail(state, "answers", "zz")
@pytest.mark.cpu
def test_activation_labels_are_descriptive_without_causal_wording():
labels = [explorer.DESCRIPTIVE, *explorer.ACTIVATION_COLUMNS,
*(text for key, text in explorer.METRICS.items() if key.startswith("activation"))]
assert "Descriptive" in explorer.DESCRIPTIVE
for word in ("caus", "effect", "drives", "responsible"):
assert not any(word in label.lower() for label in labels), word
@pytest.fixture
def layout(monkeypatch):
fake = _FakeGradio()
monkeypatch.setitem(sys.modules, "gradio", fake)
build_evidence_explorer()
return {event[4].get("api_name"): event[1] for component in fake.components
for event in component.events}
@pytest.mark.cpu
def test_layout_callbacks_walk_the_flow(runs, layout, tmp_path):
assert {"explorer_load", "explorer_filter", "explorer_drill", "explorer_drill_group",
"explorer_inspect", "explorer_text", "explorer_save_view", "explorer_load_view",
"explorer_export", "explorer_compare"} <= set(layout)
assert layout["explorer_load"]("", "", "")[1].startswith("Load failed")
for name, args in (("explorer_filter", ("answers", "(all)", [], [])),
("explorer_drill", ("answers", "task_success", "count")),
("explorer_drill_group", ("answers", "p-g3")),
("explorer_inspect", ("answers", "p3", False)),
("explorer_save_view", ("v", "(all)", "(all)", [], [], "answers", "")),
("explorer_load_view", ("v",)),
("explorer_export", ("x.json", "(all)", "(all)", [], [], "answers", "")),
("explorer_compare", ("x",))):
outcome = layout[name](None, *args)
assert explorer.TEXT_HIDDEN in json.dumps(outcome) or "Load a run first" in json.dumps(
outcome), name
loaded = layout["explorer_load"](runs["mixed"], "", "")
state = loaded[0]
assert loaded[1] == "Run loaded." and loaded[6]["choices"] == ["answers", "aliases"]
assert loaded[-1] == [] and len(layout["explorer_load"]("", "", "")) == len(loaded)
examples, grid, message = layout["explorer_filter"](state, "answers", "(all)", ["failed"], [])
assert [row[1] for row in examples] == ["p3", "p5"] and message.startswith("2 matching")
assert layout["explorer_filter"](state, "nope", "(all)", [], [])[2].startswith("Filters not")
drilled = layout["explorer_drill"](state, "answers", "task_success", "count")
assert drilled[2] == ["passed"] and [row[1] for row in drilled[4]] == ["p4"]
assert layout["explorer_drill"](state, "answers", "raw", "count")[6].startswith("Drill-down")
assert len(layout["explorer_drill_group"](state, "answers", "p-g3")[4]) == 1
assert layout["explorer_drill_group"](state, "answers", "zz")[6].startswith("Drill-down")
record, activations, note, text = layout["explorer_inspect"](state, "answers", "p3", False)
assert record["example_id"] == "p3" and len(activations) == 2
assert note == explorer.DESCRIPTIVE and text == explorer.TEXT_HIDDEN
assert layout["explorer_inspect"](state, "answers", "zz", False)[2].startswith("Example un")
assert runs["completions"]["p3"] in layout["explorer_text"](state, "answers", "p3", True)
assert layout["explorer_text"](state, "answers", "zz", True).startswith("Text unavailable")
assert layout["explorer_text"](state, "answers", "", True) == explorer.TEXT_HIDDEN
saved = layout["explorer_save_view"](state, "ui-view", "answers", "(all)", ["failed"], [],
"answers", "p5")
assert saved[0].startswith("Saved view") and saved[1]["value"] == "ui-view"
assert layout["explorer_save_view"](state, "../x", "answers", "(all)", [], [], "answers",
"")[0].startswith("View not saved")
reloaded = layout["explorer_load_view"](state, "ui-view")
assert reloaded[2] == ["failed"] and reloaded[5] == "p5" and len(reloaded[6]) == 2
assert layout["explorer_load_view"](state, "absent")[8].startswith("View not loaded")
target = tmp_path / "ui" / "spec.json"
exported = layout["explorer_export"](state, str(target), "answers", "(all)", ["failed"], [],
"answers", "")
assert "obliteratus experiments run" in exported and target.is_file()
assert layout["explorer_export"](state, str(target), "(all)", "(all)", [], [], "answers",
"").startswith("Export failed")
summary, exact, _tolerance = layout["explorer_compare"](state, runs["mixed"])
assert summary.startswith("Equal") and exact
assert layout["explorer_compare"](state, str(tmp_path / "nowhere"))[0].startswith(
"Comparison unavailable")
@pytest.mark.operator_ui
def test_real_explorer_tab_and_app_registration_build_without_model_or_network(tmp_path):
pytest.importorskip("gradio", reason="install the locked spaces extra")
script = r'''
import asyncio
import socket
def refuse(*args, **kwargs):
raise RuntimeError("network access attempted")
socket.socket.connect = refuse
socket.create_connection = refuse
import gradio as gr
from obliteratus.ui_evidence_explorer_layout import build_evidence_explorer
from tests.conditional.test_operator_ui import _process_api
with gr.Blocks() as demo:
build_evidence_explorer()
config = demo.get_config_file()
dependencies = {dep["api_name"]: dep for dep in config["dependencies"]}
assert {"explorer_load", "explorer_filter", "explorer_drill", "explorer_inspect",
"explorer_text", "explorer_save_view", "explorer_load_view", "explorer_export",
"explorer_compare"} <= set(dependencies)
labels = {item["props"].get("label") for item in config["components"]}
assert "Show text (local only)" in labels
async def check():
result = await _process_api(demo, dependencies["explorer_load"]["id"], ["", "", ""],
state=None)
assert result["data"][1].startswith("Load failed"), result["data"]
asyncio.run(check())
import app
config = app.demo.get_config_file()
labels = {item["props"]["label"] for item in config["components"] if item["type"] == "tabitem"}
assert {"Observations", "Explorer"} <= labels
assert app.demo.local_url is None and app._state["model"] is None
print("explorer tab constructed offline")
'''
probe = subprocess.run(
[sys.executable, "-c", script], capture_output=True, text=True, timeout=180,
check=False, env={**os.environ, "GRADIO_ANALYTICS_ENABLED": "False",
"HF_HUB_OFFLINE": "1", "TRANSFORMERS_OFFLINE": "1",
"GRADIO_TEMP_DIR": str(tmp_path)})
assert probe.returncode == 0, probe.stdout + probe.stderr
assert probe.stdout.strip().endswith("explorer tab constructed offline")
@pytest.mark.cpu
@pytest.mark.parametrize("variable", ["SPACE_ID", "OBLITERATUS_PUBLIC_UI"])
def test_text_is_refused_on_public_deployments(runs, monkeypatch, variable):
state = explorer.load_run(runs["mixed"])
example = state["rows"][0]
monkeypatch.setenv(variable, "1")
shown = explorer.example_text(state, example["task"], example["record"]["example_id"],
show_text=True)
assert shown["shown"] is False and shown["message"] == explorer.TEXT_PUBLIC
monkeypatch.delenv(variable)
assert explorer.example_text(state, example["task"], example["record"]["example_id"],
show_text=True)["shown"] is True
@pytest.mark.cpu
def test_capture_order_is_recorded_by_the_runner_and_preferred(runs, tmp_path):
import json
import shutil
state = explorer.load_run(runs["mixed"])
recorded = state["result"]["capture"]["example_ids"]
assert recorded and len(recorded) == state["result"]["capture"]["batches"]
assert state["batches"] == {example_id: index for index, example_id in enumerate(recorded)}
# A result from before example ids were recorded falls back to the runner's order.
copy = tmp_path / "old"
shutil.copytree(Path(runs["mixed"]).parent, copy)
old_run = copy / Path(runs["mixed"]).name
result_path = Path(state["result"]["location"]["result"])
relative = result_path.relative_to(Path(runs["mixed"]))
old_result = old_run / relative
data = json.loads(old_result.read_text())
del data["capture"]["example_ids"]
old_result.write_text(json.dumps(data))
fallback = explorer.load_run(str(old_result))
assert fallback["batches"] == state["batches"]