Files
OBLITERATUS/tests/test_aspa_pareto_controller.py
Joseph MaglyandClaude Opus 5.5 ca622d290d fix(aspa): fail closed on unreadable Pareto controller evidence
load_json() caught every exception and returned None, so a truncated,
empty or undecodable ship-gate report looked like missing evidence: the
candidate silently left the leaderboard, the controller recommended the
wrong leader and still exited 0 (#518).

- load_json returns None only for a missing file; read, decode, JSON
  and non-object errors raise UnreadableEvidence with the path and
  reason.
- collect scans every evidence directory, then raises
  UnreadableEvidenceSet listing all unreadable files; corrupt
  interpolation metadata is treated the same way.
- main exits 2, reports each file on stderr and writes no frontier, so a
  ranking is never published from a partial evidence set. Unrelated
  valid JSON in an evidence directory is still ignored.
- Outcome tests cover truncated, empty, binary and non-object reports,
  multiple evidence kinds, missing files, unreadable directory entries
  and corrupt interpolation metadata; the script is registered in the
  test-risk map.

Reported-by: soarkit
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 10:25:46 -05:00

108 lines
4.3 KiB
Python

"""Unreadable evidence must never be mistaken for missing evidence (issue #518)."""
from __future__ import annotations
import json
from pathlib import Path
import pytest
from scripts import aspa_pareto_controller as controller
pytestmark = pytest.mark.cpu
def ship_report(model: str, score: float) -> dict:
return {
"model": model, "label": model, "ship_score": score, "passes_ship_gate": True,
"harmful": {"n": 120, "refusal_rate": 0.0, "heuristic_usable_rate": 0.9,
"repetition_rate": 0.01},
"benign": {"pass_rate": 0.9}, "style": {"pass_rate": 0.9}, "gates": {},
}
def evidence_dirs(tmp_path: Path) -> dict[str, Path]:
dirs = {name: tmp_path / name for name in ("ship", "cap", "kl", "com")}
for directory in dirs.values():
directory.mkdir()
(dirs["ship"] / "alpha.json").write_text(json.dumps(ship_report("alpha", 10.0)))
return dirs
def run(dirs: dict[str, Path], out: Path) -> int:
return controller.main([
"--ship-dir", str(dirs["ship"]), "--capability-dir", str(dirs["cap"]),
"--kl-dir", str(dirs["kl"]), "--community-dir", str(dirs["com"]), "--out", str(out),
])
def test_intact_reports_rank_the_highest_scoring_candidate(tmp_path, capsys):
dirs = evidence_dirs(tmp_path)
(dirs["ship"] / "zeta.json").write_text(json.dumps(ship_report("zeta", 999.0)))
out = tmp_path / "frontier.json"
assert run(dirs, out) == 0
result = json.loads(out.read_text())
assert result["n_candidates"] == 2
assert [row["model"] for row in result["leaderboard"]] == ["zeta", "alpha"]
assert capsys.readouterr().err == ""
@pytest.mark.parametrize("payload", [
json.dumps(ship_report("zeta", 999.0))[:-2].encode(), # interrupted write
b"", # empty file
b"\xff\xfe\x00\x81garbage", # not UTF-8
b"[1, 2, 3]", # not a JSON object
], ids=["truncated", "empty", "binary", "non-object"])
def test_unreadable_report_fails_closed_and_publishes_nothing(tmp_path, capsys, payload):
dirs = evidence_dirs(tmp_path)
corrupt = dirs["ship"] / "zeta.json"
corrupt.write_bytes(payload)
out = tmp_path / "frontier.json"
assert run(dirs, out) == 2
assert not out.exists()
err = capsys.readouterr().err
assert str(corrupt) in err and "partial evidence set" in err
def test_every_unreadable_file_is_reported_across_evidence_kinds(tmp_path, capsys):
dirs = evidence_dirs(tmp_path)
(dirs["ship"] / "a.json").write_text("{")
(dirs["kl"] / "b.json").write_text("not json")
(dirs["com"] / "c.json").write_bytes(b"\xff")
with pytest.raises(controller.UnreadableEvidenceSet) as raised:
controller.collect([dirs["ship"]], [dirs["cap"]], [dirs["kl"]], [dirs["com"]])
assert sorted(e.path.name for e in raised.value.errors) == ["a.json", "b.json", "c.json"]
assert run(dirs, tmp_path / "out.json") == 2
err = capsys.readouterr().err
assert all(name in err for name in ("a.json", "b.json", "c.json"))
def test_missing_file_is_absent_evidence_not_an_error(tmp_path):
assert controller.load_json(tmp_path / "absent.json") is None
def test_unreadable_directory_entry_is_an_error_not_absence(tmp_path):
directory = tmp_path / "report.json"
directory.mkdir()
with pytest.raises(controller.UnreadableEvidence) as raised:
controller.load_json(directory)
assert raised.value.path == directory and "Error" in raised.value.reason
def test_unrelated_valid_json_is_still_ignored(tmp_path):
dirs = evidence_dirs(tmp_path)
(dirs["ship"] / "notes.json").write_text(json.dumps({"comment": "not a report"}))
candidates = controller.collect([dirs["ship"]], [dirs["cap"]], [dirs["kl"]], [dirs["com"]])
assert list(candidates) == ["alpha"]
def test_corrupt_interpolation_metadata_fails_closed(tmp_path, capsys):
dirs = evidence_dirs(tmp_path)
model_dir = tmp_path / "model"
model_dir.mkdir()
(model_dir / "source_interpolation_metadata.json").write_text("{")
(dirs["ship"] / "interp.json").write_text(json.dumps(ship_report(str(model_dir), 50.0)))
out = tmp_path / "frontier.json"
assert run(dirs, out) == 2
assert not out.exists()
assert "source_interpolation_metadata.json" in capsys.readouterr().err