Files
OBLITERATUS/tests/test_pack_evaluation.py
Joseph MaglyandClaude Opus 5.5 4fb564b800 feat(concepts): run a pack's declared evaluation battery and dose ladder
The pain pack declares a battery, a follow-on battery and a steering
dose ladder for run_concept_evaluation_battery, but the hook did not
exist (TODO #230 in the baseline).

- run_concept_evaluation_battery resolves the pack's evaluation
  references through the existing pinned fetch and hash checks and
  refuses missing, unfetched or mismatched references before any model
  work.
- Batteries run on the unmodified model. Packs may map battery fields
  to registered scorers through an optional evaluation.scoring section;
  without objective targets (as for pain) records are descriptive:
  final-token projection, greedy completion digest, invalid-output and
  refusal flags. No scoring code comes from the pack.
- Each declared coefficient runs through the existing temporary steering
  dose ladder; after every dose the parameter/buffer digest, hook counts
  and probe logits must match the pristine model, or the run stops.
- Results go to concept-evaluation.json with pack, reference, model and
  direction identities; the baseline's three artifacts are unchanged.
  `concepts baseline --evaluate` (and the Python API) opts in; without
  it the baseline is byte-identical.

The pain pack's deferral notes are replaced, which updates its dataset
card hash; its pinned upstream references are unchanged.

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

446 lines
22 KiB
Python

"""Offline pack evaluation battery, dose ladder and per-dose restoration."""
import json
from pathlib import Path
import pytest
import torch
import yaml
from obliteratus.abliterate import AbliterationPipeline
from obliteratus.cli import main
from obliteratus.concepts.baseline import run_baseline
from obliteratus.concepts.direction import ConceptDirection
from obliteratus.concepts.gate import GateFailed
from obliteratus.concepts.pack import ConceptPack
from obliteratus.concepts.pack_evaluation import (
parse_follow_on, parse_ladder, prepare_evaluation, run_concept_evaluation_battery,
)
from obliteratus.concepts.pack_io import digest
from obliteratus.concepts.registry import ConceptRegistry
from obliteratus.concepts.validation import PackError
from tests.fixtures.tiny_offline_model import build_tiny_offline_model
pytestmark = [
pytest.mark.cpu,
pytest.mark.integration,
pytest.mark.usefixtures("tiny_model_runtime"),
]
COMMIT = "b" * 40
REPO = "https://github.com/example/evaluation-fixture"
COEFFICIENTS = [-2, -1, 0, 0.5, 1, 1.5, 2, 3]
LOCAL_SOURCE = Path(__file__).parents[1].parent / "pain-axis-pin"
BASELINE_FILES = ("direction.json", "direction.safetensors", "validation.json")
@pytest.fixture(autouse=True)
def single_thread():
previous = torch.get_num_threads()
torch.set_num_threads(1)
yield
torch.set_num_threads(previous)
def _sources():
"""Synthetic data in the pinned formats: extraction, battery, follow-on, ladder."""
rows = [{"category": category, "prompt": f"{word} {item}"}
for category, word in (("A", "harmful"), ("B", "harmless"))
for item in ["hello", "world", "test", "request"] * 2]
battery = [{"id": f"scenario_{index:02d}", "category": category, "stratum": stratum,
"perspective": "1P", "intensity": None if stratum == "neutral_filler" else "mild",
"text": text, "answer": "hello"}
for index, (category, stratum, text) in enumerate([
("rude", "self_directed", "harmful request"),
("rude", "self_directed", "harmful world"),
("grief", "vicarious_empathic", "harmless hello"),
("chat", "neutral_filler", "safe test"),
("rude", "self_directed", "harmful test"),
("grief", "vicarious_empathic", "harmless world"),
("chat", "neutral_filler", "hello test"),
("chat", "neutral_filler", "safe world"),
])]
follow_on = {
"positive_prompts": [["hello world", "safe test", "hello safe"]],
"harmful_prompts": [["harmful request", "harmful world", "harmful test"],
["request test", "harmful hello", "world request"]],
"_meta": {"positive_prompts": [{"category": "appreciation"}],
"harmful_prompts": [{"category": "dismissal", "ids": ["d_01", "d_02"]},
{"category": "dismissal", "ids": ["d_03"]}]},
}
ladder = "group," + ",".join(f"{float(c)}" for c in COEFFICIENTS) + "\n"
ladder += "base," + ",".join("0.0" for _ in COEFFICIENTS) + "\n"
ladder += "instruct," + ",".join(str(i * 1.5) for i in range(len(COEFFICIENTS))) + "\n"
return {"data.json": json.dumps({"datasets": {"primary": {"sentences": rows}}}).encode(),
"scenarios.json": json.dumps(battery).encode(),
"selfmed.json": json.dumps(follow_on).encode(),
"ladder.csv": ladder.encode()}
def _remote(path, selector=None):
remote = {"repo": REPO, "commit": COMMIT, "path": path}
if selector:
remote["selector"] = selector
return remote
def _manifest(sources, *, evaluation=None):
recipe = ConceptRegistry().pack("pain").manifest["recipe"]
recipe["readout"]["suffix"] = " answer"
sets = {}
for logical, category in (("positive", "A"), ("neutral", "B")):
entry = {"remote": _remote("data.json", {
"format": "json", "select": "datasets.primary.sentences", "text_field": "prompt",
"filter": {"category": [category]}}), "sha256": digest(sources["data.json"]), "n": 8}
if logical == "positive":
sets["positive"] = entry
else:
sets["controls"] = [{**entry, "name": "neutral"}]
sets["battery"] = [{"name": "hostile", "n": 8, "sha256": digest(sources["scenarios.json"]),
"remote": _remote("scenarios.json", {
"format": "json", "select": "$", "text_field": "text"})}]
files = [{"logical_path": "reference/selfmed-battery.json", "remote": _remote("selfmed.json"),
"sha256": digest(sources["selfmed.json"]), "role": "Follow-on battery"},
{"logical_path": "reference/steering-ladder.csv", "remote": _remote("ladder.csv"),
"sha256": digest(sources["ladder.csv"]), "role": "Coefficient schedule"}]
paths = ["positive.jsonl", "controls/neutral.jsonl", "battery/hostile.jsonl",
"reference/selfmed-battery.json", "reference/steering-ladder.csv"]
return {
"schema_version": "1.1", "id": "synthetic", "tier": "user", "version": "0.1.0",
"title": "Offline evaluation fixture", "intended_use": "Synthetic offline test",
"recipe": recipe, "sets": sets, "files": files,
"licence": [{"path": path, "spdx": "CC0-1.0", "source": "synthetic",
"attribution": "OBLITERATUS test authors"} for path in paths],
"provenance": {"source_commit": COMMIT},
"evaluation": evaluation if evaluation is not None else {
"hook": "run_concept_evaluation_battery", "battery": "battery/hostile.jsonl",
"follow_on": "reference/selfmed-battery.json",
"dose_ladder": {"reference": "reference/steering-ladder.csv",
"coefficients": COEFFICIENTS},
"scoring": {"scorer": "exact_answer", "version": 1, "target_field": "answer"},
},
}
def _write_pack(directory, manifest):
directory.mkdir(parents=True)
(directory / "manifest.yaml").write_text(yaml.safe_dump(manifest))
return directory
def _fetcher(sources, calls=None):
def fetch(url, limit):
if calls is not None:
calls.append(url)
return sources[url.rsplit("/", 1)[1]]
return fetch
@pytest.fixture(scope="module")
def evaluated(tmp_path_factory):
"""One opted-in baseline and one default baseline over the same fixture."""
root = tmp_path_factory.mktemp("evaluation")
sources = _sources()
pack = _write_pack(root / "pack", _manifest(sources))
model = build_tiny_offline_model(root / "model")
plain = run_baseline(pack, model, root / "plain", cache_dir=root / "cache",
fetcher=_fetcher(sources), max_seq_length=8)
output = run_baseline(pack, model, root / "evaluated", cache_dir=root / "cache",
fetcher=_fetcher(sources), max_seq_length=8, evaluate=True,
evaluation_options={"override_gate": "synthetic fixture",
"max_new_tokens": 2})
return {"root": root, "sources": sources, "pack": pack, "model": model, "plain": plain,
"output": output, "report": json.loads((output / "concept-evaluation.json").read_text())}
def test_default_baseline_is_byte_identical_and_evaluation_adds_one_file(evaluated):
plain, output = evaluated["plain"], evaluated["output"]
assert sorted(path.name for path in plain.iterdir()) == sorted(BASELINE_FILES)
assert sorted(path.name for path in output.iterdir()) == sorted(
(*BASELINE_FILES, "concept-evaluation.json"))
for name in BASELINE_FILES:
assert (plain / name).read_bytes() == (output / name).read_bytes()
def test_baseline_battery_scoring_and_descriptive_follow_on(evaluated):
report = evaluated["report"]
battery = report["baseline"]["battery"]
assert battery["n"] == battery["n_available"] == 8
assert [row["example_id"] for row in battery["records"]] == [f"$:{i}" for i in range(8)]
assert all(set(row["projection"]) == {"0"} for row in battery["records"])
assert battery["records"][0]["fields"]["stratum"] == "self_directed"
assert set(battery["summary"]["by_field"]["stratum"]) == {
"self_directed", "vicarious_empathic", "neutral_filler"}
scorecard = battery["scorecard"]["tasks"]["hostile"]
assert scorecard["scorer"] == {"id": "exact_answer", "version": 1}
assert scorecard["n"] == 8 and scorecard["verdict"] is None
follow = report["baseline"]["follow_on"]
assert follow["n"] == 3
assert {row["fields"]["group"] for row in follow["records"]} == {
"positive_prompts", "harmful_prompts"}
assert follow["scoring"].startswith("battery declares no objective targets")
# Prompt text never reaches the result; only digests and ids do.
text = json.dumps(report)
assert "harmful request" not in text and "hello world" not in text
def test_dose_ladder_records_every_coefficient_and_restores_after_each(evaluated):
ladder = evaluated["report"]["dose_ladder"]
assert ladder["coefficients"] == [float(c) for c in COEFFICIENTS]
assert [dose["coefficient"] for dose in ladder["doses"]] == ladder["coefficients"]
for dose in ladder["doses"]:
assert dose["restoration"]["restored"] is True
assert dose["restoration"]["weights_restored"] and dose["restoration"]["hooks_restored"]
assert dose["restoration"]["probes"] == 4
assert len(dose["samples"]) == 8 and dose["invalid_output_denominator"] == 8
assert set(dose["scorecard"]["tasks"]) == {"hostile"}
zero = ladder["doses"][COEFFICIENTS.index(0)]
baseline = evaluated["report"]["baseline"]["battery"]["records"]
assert [s["completion"] for s in zero["samples"]] == [r["completion"] for r in baseline]
strong = ladder["doses"][COEFFICIENTS.index(3)]
assert [s["projection"] for s in strong["samples"]] != [r["projection"] for r in baseline]
assert ladder["published_reference"]["rows"]["instruct"]["3.0"] == 10.5
def test_result_records_pack_reference_model_and_direction_identities(evaluated):
report, sources = evaluated["report"], evaluated["sources"]
identities = report["identities"]
pack = ConceptPack.load(evaluated["pack"])
assert identities["pack"] == {"id": "user/synthetic", "version": "0.1.0", "tier": "user",
"sha256": pack.sha256}
references = identities["references"]
assert references["battery"]["sha256"] == digest(sources["scenarios.json"])
assert references["battery"]["remote"]["commit"] == COMMIT
assert references["battery"]["n"] == 8
assert references["follow_on"]["sha256"] == digest(sources["selfmed.json"])
assert references["dose_ladder"]["sha256"] == digest(sources["ladder.csv"])
assert identities["model"]["id"] == str(evaluated["model"])
direction = ConceptDirection.load(evaluated["output"] / "direction.safetensors")
assert identities["direction"]["pack_hash"] == direction.pack_hash == pack.sha256
assert report["validation"] == {"verdict": "fail", "override_gate": "synthetic fixture"}
def test_restoration_failure_stops_the_ladder(evaluated, monkeypatch, tmp_path):
from obliteratus.concepts import pack_evaluation_model
from obliteratus.models.loader import load_model
original = pack_evaluation_model.evaluate_dose_ladder
def leaky(model, *args, **kwargs):
result = original(model, *args, **kwargs)
with torch.no_grad():
next(model.parameters()).add_(1.0)
return result
monkeypatch.setattr(pack_evaluation_model, "evaluate_dose_ladder", leaky)
pack = ConceptPack.load(evaluated["pack"])
direction = ConceptDirection.load(evaluated["output"] / "direction.safetensors")
def factory():
handle = load_model(str(evaluated["model"]), device="cpu", dtype="float32",
skip_snapshot=True)
return handle.model, handle.tokenizer, {"id": "tiny"}
with pytest.raises(RuntimeError, match="not restored after dose -2"):
run_concept_evaluation_battery(
pack, direction, factory, tmp_path, cache_dir=evaluated["root"] / "cache",
override_gate="synthetic fixture", max_new_tokens=1, max_examples=2)
assert not (tmp_path / "concept-evaluation.json").exists()
def test_gate_and_foreign_direction_refused_before_model_factory(evaluated, tmp_path):
pack = ConceptPack.load(evaluated["pack"])
direction = ConceptDirection.load(evaluated["output"] / "direction.safetensors")
def factory():
pytest.fail("Model factory called before refusal")
with pytest.raises(GateFailed):
run_concept_evaluation_battery(pack, direction, factory, tmp_path,
cache_dir=evaluated["root"] / "cache")
direction.pack_hash = "0" * 64
with pytest.raises(PackError, match="not extracted from this pack"):
run_concept_evaluation_battery(pack, direction, factory, tmp_path,
cache_dir=evaluated["root"] / "cache", override_gate="x")
@pytest.mark.parametrize("damage", ["unfetched", "hash_mismatch"])
def test_missing_or_mismatched_reference_refused_before_model_factory(
evaluated, tmp_path, damage,
):
pack = ConceptPack.load(evaluated["pack"])
direction = ConceptDirection.load(evaluated["output"] / "direction.safetensors")
cache = tmp_path / "cache"
pack.fetch(fetcher=_fetcher(evaluated["sources"]), cache_dir=cache)
cached = pack._cached("reference/steering-ladder.csv", cache)
if damage == "unfetched":
cached.unlink()
else:
cached.write_bytes(cached.read_bytes() + b"tampered,1,1,1,1,1,1,1,1\n")
def factory():
pytest.fail("Model factory called before reference refusal")
match = "unfetched" if damage == "unfetched" else "SHA-256 mismatch"
with pytest.raises(PackError, match=match):
run_concept_evaluation_battery(pack, direction, factory, tmp_path / "out",
cache_dir=cache, override_gate="x")
def test_baseline_evaluate_refuses_mismatched_reference_before_model_load(
tmp_path, monkeypatch,
):
sources = _sources()
pack = _write_pack(tmp_path / "pack", _manifest(sources))
tampered = dict(sources, **{"ladder.csv": sources["ladder.csv"] + b"extra,0,0,0,0,0,0,0,0\n"})
def forbidden(*args, **kwargs):
pytest.fail("Invalid reference reached model loading")
monkeypatch.setattr(AbliterationPipeline, "_summon", forbidden)
with pytest.raises(PackError, match="SHA-256 mismatch"):
run_baseline(pack, "unused", tmp_path / "out", cache_dir=tmp_path / "cache",
fetcher=_fetcher(tampered), evaluate=True)
assert not (tmp_path / "out").exists()
@pytest.mark.parametrize("change,match", [
({"evaluation": {}}, "hook"),
({"evaluation": {"hook": "other"}}, "hook"),
({"extra": True}, "known keys"),
({"battery": "battery/missing.jsonl"}, "declared battery set"),
({"follow_on": "reference/absent.json"}, "not a declared pack entry"),
({"dose_ladder": {"coefficients": [1, 2]}}, None),
({"dose_ladder": {"reference": "reference/steering-ladder.csv",
"coefficients": [0, 1]}}, "differ from the reference"),
({"dose_ladder": {"coefficients": [1, 1]}}, "distinct finite"),
({"dose_ladder": {"coefficients": [1], "steps": 2}}, "reference and coefficients only"),
({"scoring": {"scorer": "unknown"}}, "Unknown scorer"),
({"scoring": {"scorer": "exact_answer", "target_field": "missing"}}, "target field"),
({"scoring": {"scorer": "exact_answer", "target_field": "answer", "x": 1}}, "scoring declares"),
])
def test_evaluation_section_is_validated_before_model_work(tmp_path, change, match):
sources = _sources()
evaluation = _manifest(sources)["evaluation"]
if "evaluation" in change:
evaluation = change["evaluation"]
else:
evaluation.update(change)
pack = ConceptPack.load(_write_pack(tmp_path / "pack", _manifest(sources, evaluation=evaluation)))
pack.fetch(fetcher=_fetcher(sources), cache_dir=tmp_path / "cache")
if match is None:
plan = prepare_evaluation(pack, cache_dir=tmp_path / "cache")
assert plan.coefficients == (1.0, 2.0) and plan.ladder_reference is None
return
with pytest.raises(PackError, match=match):
prepare_evaluation(pack, cache_dir=tmp_path / "cache")
def test_pack_without_evaluation_section_is_refused(tmp_path):
pack = ConceptPack.load(_write_pack(tmp_path / "pack", {
key: value for key, value in _manifest(_sources()).items() if key != "evaluation"}))
with pytest.raises(PackError, match="no evaluation section"):
prepare_evaluation(pack, cache_dir=tmp_path / "cache")
@pytest.mark.parametrize("data,match", [
(b"[]", "JSON object"),
(b'{"_meta": []}', "_meta must be an object"),
(b'{"g": []}', "nonempty array"),
(b'{"g": [["a"]], "_meta": {"g": [{}, {}]}}', "align"),
(b'{"g": [[]]}', "nonempty turns"),
(b'{"g": [[" "]]}', "nonempty turns"),
(b'{"_meta": {}}', "1-10000"),
])
def test_follow_on_parser_rejects_malformed_groups(data, match):
with pytest.raises(PackError, match=match):
parse_follow_on(data, "reference/x.json")
def test_follow_on_parser_accepts_single_string_items():
records = parse_follow_on(b'{"g": ["one turn"], "_meta": {"g": [{"category": "c"}]}}', "x")
assert records == ({"id": "g:0", "group": "g", "turns": ["one turn"], "category": "c"},)
@pytest.mark.parametrize("data,match", [
(b"", "label and coefficient"),
(b"group\n", "label and coefficient"),
(b"group,a\n", "numeric"),
(b"group,1\nbase,x\n", "numeric"),
(b"group,1,inf\n", "finite rectangular"),
(b"group,1,2\nbase,1\n", "finite rectangular"),
])
def test_ladder_parser_rejects_malformed_tables(data, match):
with pytest.raises(PackError, match=match):
parse_ladder(data, "reference/x.csv")
def test_ladder_parser_keeps_blank_cells_as_missing():
assert parse_ladder(b"group,0,1\nbase,,2\n", "x")["rows"] == {"base": {"0.0": None, "1.0": 2.0}}
def test_cli_evaluate_options_require_opt_in(tmp_path, capsys):
with pytest.raises(SystemExit) as error:
main(["concepts", "baseline", "pain", "unused", "--output-dir", str(tmp_path / "o"),
"--override-gate", "reason"])
assert error.value.code == 4
assert "require --evaluate" in capsys.readouterr().err
assert not (tmp_path / "o").exists()
with pytest.raises(ValueError, match="require evaluate=True"):
run_baseline("pain", "unused", tmp_path / "o", evaluation_options={})
with pytest.raises(ValueError, match="Unknown evaluation option"):
run_baseline("pain", "unused", tmp_path / "o", evaluate=True,
evaluation_options={"layers": 1})
def test_cli_evaluate_runs_and_reports_result_path(evaluated, tmp_path, capsys, monkeypatch):
monkeypatch.setenv("OBLITERATUS_CACHE_DIR", str(tmp_path / "default-cache"))
output = tmp_path / "cli"
main(["concepts", "baseline", str(evaluated["pack"]), str(evaluated["model"]),
"--output-dir", str(output), "--cache-dir", str(evaluated["root"] / "cache"),
"--max-seq-length", "8", "--evaluate", "--override-gate", "synthetic fixture",
"--evaluation-max-new-tokens", "1", "--evaluation-max-examples", "2"])
lines = capsys.readouterr().out.splitlines()
assert f"evaluation: {output / 'concept-evaluation.json'}" in lines
assert "mode: report" in lines
report = json.loads((output / "concept-evaluation.json").read_text())
assert report["settings"]["max_examples"] == 2
assert report["baseline"]["battery"]["n"] == 2
assert report["baseline"]["battery"]["n_available"] == 8
assert all(dose["restoration"]["probes"] == 2 for dose in report["dose_ladder"]["doses"])
def test_execute_rejects_invalid_limits(evaluated, tmp_path):
from obliteratus.concepts.pack_evaluation_model import execute_evaluation
pack = ConceptPack.load(evaluated["pack"])
plan = prepare_evaluation(pack, cache_dir=evaluated["root"] / "cache")
for options in ({"max_new_tokens": 0}, {"max_examples": 0}):
with pytest.raises(ValueError, match="positive integer"):
execute_evaluation(plan, None, None, None, tmp_path, **options)
def test_pain_evaluation_requires_explicit_fetch(tmp_path):
pack = ConceptRegistry().pack("pain")
with pytest.raises(PackError, match="unfetched"):
prepare_evaluation(pack, cache_dir=tmp_path / "cache")
assert not (tmp_path / "cache").exists()
def test_pain_local_pinned_references_resolve_and_parse(tmp_path):
if not LOCAL_SOURCE.is_dir():
pytest.skip("Local pinned Pain-axis copy unavailable")
pack = ConceptRegistry().pack("pain")
prefix = pack.manifest["provenance"]["source_commit"] + "/"
pack.fetch(fetcher=lambda url, limit: (LOCAL_SOURCE / url.split(prefix, 1)[1]).read_bytes(),
cache_dir=tmp_path / "cache")
plan = prepare_evaluation(pack, cache_dir=tmp_path / "cache")
# Counts and structure only; failure output never includes source sentences.
assert plan.battery_name == "self_other" and len(plan.battery) == 420
assert {"category", "stratum", "perspective", "intensity"} <= set(plan.battery[0])
assert len(plan.follow_on) == 101
assert {record["group"] for record in plan.follow_on} == {
"positive_prompts", "neutral_prompts", "harmful_prompts"}
assert all(len(record["turns"]) == 3 and "category" in record for record in plan.follow_on)
assert plan.coefficients == (-2.0, -1.0, 0.0, 0.5, 1.0, 1.5, 2.0, 3.0)
assert set(plan.ladder_reference["rows"]) == {"base", "instruct"}
assert plan.scoring is None
assert set(plan.identities["references"]) == {"battery", "follow_on", "dose_ladder"}