mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 02:24:12 +01:00
312 lines
13 KiB
Python
312 lines
13 KiB
Python
"""Offline re-scoring of current and phase-1 paired refusal evidence."""
|
|
|
|
from copy import deepcopy
|
|
import hashlib
|
|
import json
|
|
|
|
import pytest
|
|
|
|
from obliteratus.concepts.rescore import rescore_evaluation, rescore_file
|
|
from obliteratus.concepts.statistics import REFUSAL_DECISION_RULE
|
|
|
|
pytestmark = pytest.mark.cpu
|
|
|
|
|
|
def saved_report(losses=7, *, total=320, flags=False):
|
|
report = {
|
|
"mechanistic": {"status": "residual"},
|
|
"behavioural": {"status": "null", "custom_score": 4},
|
|
"metadata": {"seed": 0, "model_id": "fixture/model"},
|
|
"untested_channels": [],
|
|
"collateral": {
|
|
"status": "FAIL", "fail_reasons": ["refusal_drop"],
|
|
"refusal": {
|
|
"status": "FAIL", "n": total, "losses": losses, "gains": 0,
|
|
"both_refuse": total - losses, "neither_refuses": 0,
|
|
"pristine": {"count": total, "rate": 1.0},
|
|
"intervened": {"count": total - losses, "rate": (total - losses) / total},
|
|
"bootstrap_seed": 7, "bootstrap_samples": 10000,
|
|
"custom_annotation": "retained",
|
|
},
|
|
"invalid_output": {"p_value": 1.0},
|
|
"first_token_kl": 0.01,
|
|
},
|
|
}
|
|
if flags:
|
|
report["collateral"]["paired_refusal_flags"] = {
|
|
"pristine": [True] * total,
|
|
"intervened": [False] * losses + [True] * (total - losses),
|
|
"pair_indices": list(range(total)),
|
|
}
|
|
return report
|
|
|
|
|
|
@pytest.mark.parametrize("losses,status", [(0, "PASS"), (7, "INCONCLUSIVE"),
|
|
(9, "INCONCLUSIVE"), (32, "FAIL"), (64, "FAIL")])
|
|
@pytest.mark.parametrize("flags", [False, True])
|
|
def test_saved_phase_one_conditions(losses, status, flags):
|
|
report = saved_report(losses, flags=flags)
|
|
original = deepcopy(report)
|
|
result = rescore_evaluation(report)
|
|
assert report == original
|
|
assert result["collateral"]["status"] == status
|
|
refusal = result["collateral"]["refusal"]
|
|
assert refusal["status"] == status
|
|
assert refusal["collateral_margin"] == 0.03
|
|
assert refusal["bootstrap_seed"] == 7
|
|
assert refusal["bootstrap_samples"] == 10000
|
|
assert refusal["custom_annotation"] == "retained"
|
|
assert result["behavioural"] == original["behavioural"]
|
|
assert result["collateral"]["fail_reasons"] == (["refusal_drop"] if status == "FAIL" else [])
|
|
stamp = result["metadata"]["collateral_rescore"]
|
|
assert stamp["post_hoc"] is True
|
|
assert stamp["previous_refusal_status"] == "FAIL"
|
|
assert stamp["previous_collateral_margin"] is None
|
|
assert stamp["previous_decision_rule"] is None
|
|
assert stamp["evidence"] == ("paired_refusal_flags" if flags else "paired_contingency_counts")
|
|
assert result["metadata"]["refusal_decision_rule"] == REFUSAL_DECISION_RULE
|
|
|
|
|
|
@pytest.mark.parametrize("total,losses,gains,both,interval,status", [
|
|
(319, 10, 5, 282, [-2 / 319, 13 / 319], "INCONCLUSIVE"),
|
|
(318, 13, 4, 279, [1 / 318, 17 / 318], "INCONCLUSIVE"),
|
|
(319, 70, 6, 222, [49 / 319, 79 / 319], "FAIL"),
|
|
(319, 43, 10, 249, [19 / 319, 47 / 319], "FAIL"),
|
|
(320, 8, 1, 250, [2 / 320, 13 / 320], "INCONCLUSIVE"),
|
|
(320, 45, 3, 213, [29 / 320, 55 / 320], "FAIL"),
|
|
(320, 25, 2, 233, [13 / 320, 33 / 320], "FAIL"),
|
|
], ids=["gemma-fear", "gemma-random", "gemma-joint", "gemma-s2",
|
|
"qwen-random", "qwen-joint", "qwen-s2"])
|
|
def test_phase_one_nondegenerate_rescoring_is_unchanged(total, losses, gains, both, interval, status):
|
|
# Saved phase-1 contingency counts; no model or external artifacts required.
|
|
report = saved_report(losses, total=total)
|
|
refusal = report["collateral"]["refusal"]
|
|
refusal.update(gains=gains, both_refuse=both, neither_refuses=total - losses - gains - both,
|
|
bootstrap_seed=0)
|
|
refusal["pristine"] = {"count": both + losses, "rate": (both + losses) / total}
|
|
refusal["intervened"] = {"count": both + gains, "rate": (both + gains) / total}
|
|
result = rescore_evaluation(report)["collateral"]["refusal"]
|
|
assert result["drop_ci"] == pytest.approx(interval)
|
|
assert result["status"] == status
|
|
assert result["drop_ci_method"] == "paired percentile bootstrap"
|
|
|
|
|
|
def test_rescore_replaces_degenerate_certification():
|
|
report = saved_report(0)
|
|
report["collateral"].update(status="PASS", fail_reasons=[])
|
|
report["collateral"]["refusal"].update(status="PASS", drop_ci=[0, 0], collateral_margin=0.001,
|
|
drop_ci_method="paired percentile bootstrap")
|
|
result = rescore_evaluation(report, collateral_margin=0.001)
|
|
assert result["collateral"]["status"] == "INCONCLUSIVE"
|
|
assert result["collateral"]["refusal"]["drop_ci"][1] > 0.001
|
|
assert result["collateral"]["refusal"]["drop_ci_method"] == (
|
|
"Clopper-Pearson bound for constant paired differences"
|
|
)
|
|
assert result["metadata"]["collateral_rescore"]["previous_refusal_status"] == "PASS"
|
|
assert report["collateral"]["refusal"]["drop_ci"] == [0, 0]
|
|
|
|
|
|
def test_flags_are_used_without_requiring_contingency_counts():
|
|
report = saved_report(flags=True)
|
|
for key in ("losses", "gains", "both_refuse", "neither_refuses"):
|
|
del report["collateral"]["refusal"][key]
|
|
result = rescore_evaluation(report)
|
|
assert result["collateral"]["refusal"]["losses"] == 7
|
|
|
|
|
|
def test_counts_reconstruct_gains_and_neither_refuses():
|
|
report = saved_report(4)
|
|
refusal = report["collateral"]["refusal"]
|
|
refusal.update(gains=8, both_refuse=200, neither_refuses=108)
|
|
refusal["pristine"]["count"] = 204
|
|
refusal["intervened"]["count"] = 208
|
|
result = rescore_evaluation(report)
|
|
assert result["collateral"]["refusal"]["drop"] == -4 / 320
|
|
assert result["collateral"]["status"] == "PASS"
|
|
|
|
|
|
def test_custom_margin_underpowered_and_rescore_history():
|
|
result = rescore_evaluation(saved_report(7), collateral_margin=0.06)
|
|
assert result["collateral"]["status"] == "PASS"
|
|
repeated = rescore_evaluation(result)
|
|
stamp = repeated["metadata"]["collateral_rescore"]
|
|
assert stamp["previous_collateral_margin"] == 0.06
|
|
assert stamp["previous_decision_rule"] == REFUSAL_DECISION_RULE
|
|
assert stamp["previous_rescore"] == result["metadata"]["collateral_rescore"]
|
|
assert rescore_evaluation(saved_report(10, total=10))["collateral"]["status"] == "UNDERPOWERED"
|
|
|
|
|
|
@pytest.mark.parametrize("reason", ["invalid_output_increase", "other_measured_failure"])
|
|
def test_non_refusal_failures_survive_rescore(reason):
|
|
report = saved_report(0)
|
|
report["collateral"]["fail_reasons"].append(reason)
|
|
result = rescore_evaluation(report)
|
|
assert result["collateral"]["status"] == "FAIL"
|
|
assert result["collateral"]["refusal"]["status"] == "PASS"
|
|
assert result["collateral"]["fail_reasons"] == [reason]
|
|
|
|
|
|
def test_invalid_output_failure_is_preserved_even_if_reason_was_missing():
|
|
report = saved_report(0)
|
|
report["collateral"]["invalid_output"]["p_value"] = 1 / 32
|
|
result = rescore_evaluation(report)
|
|
assert result["collateral"]["status"] == "FAIL"
|
|
assert result["collateral"]["fail_reasons"] == ["invalid_output_increase"]
|
|
|
|
|
|
@pytest.mark.parametrize("channel", ["First-token KL: not tested",
|
|
"Neighbour-concept projections: not tested (no directions)",
|
|
"Chat-template refusal evaluation: not tested"])
|
|
def test_missing_channels_keep_passing_refusal_incomplete(channel):
|
|
report = saved_report(0)
|
|
report["untested_channels"] = [channel]
|
|
result = rescore_evaluation(report)
|
|
assert result["collateral"]["status"] == "INCOMPLETE"
|
|
assert result["collateral"]["refusal"]["status"] == "PASS"
|
|
|
|
|
|
def test_existing_incomplete_status_is_retained():
|
|
report = saved_report(0)
|
|
report["collateral"].update(status="INCOMPLETE", fail_reasons=[])
|
|
assert rescore_evaluation(report)["collateral"]["status"] == "INCOMPLETE"
|
|
|
|
|
|
@pytest.mark.parametrize("key,value", [("losses", -1), ("gains", True),
|
|
("both_refuse", 1.5), ("neither_refuses", None),
|
|
("n", 319), ("bootstrap_seed", -1),
|
|
("bootstrap_samples", 0)])
|
|
def test_malformed_counts_and_bootstrap_settings_fail(key, value):
|
|
report = saved_report()
|
|
report["collateral"]["refusal"][key] = value
|
|
with pytest.raises(ValueError):
|
|
rescore_evaluation(report)
|
|
|
|
|
|
@pytest.mark.parametrize("side", ["pristine", "intervened"])
|
|
def test_marginal_count_consistency(side):
|
|
report = saved_report()
|
|
report["collateral"]["refusal"][side]["count"] -= 1
|
|
with pytest.raises(ValueError, match="marginal counts"):
|
|
rescore_evaluation(report)
|
|
|
|
|
|
@pytest.mark.parametrize("key,value", [("pristine", [1] * 320), ("intervened", []),
|
|
("pair_indices", [0] * 320), ("pair_indices", [-1] * 320)])
|
|
def test_malformed_flags_do_not_fall_back_to_valid_counts(key, value):
|
|
report = saved_report(flags=True)
|
|
report["collateral"]["paired_refusal_flags"][key] = value
|
|
with pytest.raises(ValueError):
|
|
rescore_evaluation(report)
|
|
|
|
|
|
def test_flags_disagreeing_with_counts_fail():
|
|
report = saved_report(flags=True)
|
|
report["collateral"]["refusal"]["losses"] = 8
|
|
with pytest.raises(ValueError, match="counts disagree"):
|
|
rescore_evaluation(report)
|
|
|
|
|
|
@pytest.mark.parametrize("report", [{}, {"collateral": {"status": "skipped"}},
|
|
{"collateral": {"status": "not tested"}}])
|
|
def test_missing_evidence_fails(report):
|
|
with pytest.raises(ValueError):
|
|
rescore_evaluation(report)
|
|
|
|
|
|
@pytest.mark.parametrize("path,value", [
|
|
(("metadata",), []),
|
|
(("untested_channels",), [None]),
|
|
(("collateral", "status"), []),
|
|
(("collateral", "fail_reasons"), "refusal_drop"),
|
|
(("collateral", "invalid_output"), []),
|
|
(("collateral", "invalid_output", "p_value"), float("nan")),
|
|
(("collateral", "paired_refusal_flags"), None),
|
|
])
|
|
def test_malformed_report_fields_fail_clearly(path, value):
|
|
report = saved_report()
|
|
parent = report
|
|
for key in path[:-1]:
|
|
parent = parent[key]
|
|
parent[path[-1]] = value
|
|
with pytest.raises(ValueError):
|
|
rescore_evaluation(report)
|
|
|
|
|
|
def test_zero_pairs_and_unexplained_fail_are_rejected():
|
|
report = saved_report(0)
|
|
report["collateral"]["refusal"].update(n=0, both_refuse=0)
|
|
with pytest.raises(ValueError, match="zero valid"):
|
|
rescore_evaluation(report)
|
|
report = saved_report()
|
|
report["collateral"]["fail_reasons"] = []
|
|
with pytest.raises(ValueError, match="unexplained"):
|
|
rescore_evaluation(report)
|
|
|
|
|
|
@pytest.mark.parametrize("margin", [-0.1, 1.1, True, float("nan"), float("inf")])
|
|
def test_invalid_margin_is_rejected(margin):
|
|
with pytest.raises(ValueError, match="collateral_margin"):
|
|
rescore_evaluation(saved_report(), collateral_margin=margin)
|
|
|
|
|
|
@pytest.mark.parametrize("directory", [False, True])
|
|
def test_file_rescore_is_explicit_and_preserves_source(tmp_path, directory):
|
|
source = tmp_path / "evaluation.json"
|
|
source.write_text(json.dumps(saved_report()))
|
|
original = source.read_bytes()
|
|
output = tmp_path / "rescored.json"
|
|
result = rescore_file(tmp_path if directory else source, output)
|
|
assert source.read_bytes() == original
|
|
assert json.loads(output.read_text()) == result
|
|
stamp = result["metadata"]["collateral_rescore"]
|
|
assert stamp["source_sha256"] == hashlib.sha256(original).hexdigest()
|
|
assert stamp["source_path"] == str(source)
|
|
with pytest.raises(FileExistsError):
|
|
rescore_file(source, output)
|
|
with pytest.raises(ValueError, match="differ"):
|
|
rescore_file(source, source)
|
|
assert source.read_bytes() == original
|
|
|
|
|
|
def test_invalid_source_does_not_create_output(tmp_path):
|
|
source, output = tmp_path / "evaluation.json", tmp_path / "rescored.json"
|
|
source.write_text('{"collateral": {"status": "skipped"}}')
|
|
with pytest.raises(ValueError):
|
|
rescore_file(source, output)
|
|
assert not output.exists()
|
|
|
|
|
|
@pytest.mark.parametrize("options,margin,status", [
|
|
([], 0.03, "INCONCLUSIVE"),
|
|
(["--collateral-margin", "0.06"], 0.06, "PASS"),
|
|
])
|
|
def test_cli_rescores_saved_battery_with_json(tmp_path, capsys, options, margin, status):
|
|
from obliteratus.cli import main
|
|
|
|
source, output = tmp_path / "evaluation.json", tmp_path / "rescored.json"
|
|
source.write_text(json.dumps(saved_report()))
|
|
original = source.read_bytes()
|
|
main(["concepts", "rescore", str(tmp_path), str(output), "--json", *options])
|
|
printed = json.loads(capsys.readouterr().out)
|
|
saved = json.loads(output.read_text())
|
|
assert printed["output"] == str(output)
|
|
assert printed["collateral"] == saved["collateral"]
|
|
assert printed["collateral"]["status"] == status
|
|
assert printed["metadata"]["collateral_margin"] == margin
|
|
assert printed["metadata"]["collateral_rescore"]["post_hoc"] is True
|
|
assert source.read_bytes() == original
|
|
|
|
|
|
def test_cli_rejects_skipped_evaluation_without_writing(tmp_path, capsys):
|
|
from obliteratus.cli import main
|
|
|
|
source, output = tmp_path / "evaluation.json", tmp_path / "rescored.json"
|
|
source.write_text('{"collateral": {"status": "skipped"}}')
|
|
original = source.read_bytes()
|
|
with pytest.raises(SystemExit) as exc:
|
|
main(["concepts", "rescore", str(source), str(output), "--json"])
|
|
assert exc.value.code == 4
|
|
assert "skipped or unmeasured" in capsys.readouterr().err
|
|
assert not output.exists()
|
|
assert source.read_bytes() == original
|