Files
OBLITERATUS/tests/test_phase_evaluation_admission.py
Joseph MaglyandClaude Opus 5.5 6f925caf1d feat(concepts): versioned task scorers and paired scorecards
The behavioural battery treated any greedy-text difference as an effect,
so a punctuation-only edit looked like a change and nothing measured
task success.

Add a local, versioned scorer registry with deterministic exact-answer
and structured-output (JSON + declared schema) scorers. Each completion
yields separate endpoints: task success, invalid output and abstention;
raw text change is reported as descriptive only. Paired summaries use a
one-sided exact McNemar test and Wilson bounds against a declared
per-task margin; min_pairs defaults to the smallest sample that could
pass that margin, and untested or underpowered endpoints never aggregate
to PASS. Every aggregate keeps its per-example records.

EvaluationBattery(scorers=...) writes behavioural["scorecard"]; without
scorers nothing changes. Pipeline admission refuses batteries that carry
scorers until their generation is modelled. Dataset-built examples read
select/calibrate rows; locked test still needs explicit acknowledgement.

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

203 lines
8.7 KiB
Python

"""Evaluation preflight mirrors the data-only ModelEvaluator call contracts."""
from types import SimpleNamespace
from unittest.mock import Mock
import pytest
import torch
from obliteratus.abliterate import AbliterationPipeline, _COHERENCE_ANCHORS
from obliteratus.concepts.evaluation_model import ModelEvaluator
from obliteratus.phase_admission import PhaseAdmissionError
class RecordingTokenizer:
chat_template = "configured"
def __init__(self):
self.calls = []
self.templates = []
def apply_chat_template(self, messages, **kwargs):
self.templates.append((messages, kwargs))
return "<chat>" + messages[0]["content"] + "</chat>"
def __call__(self, prompt, **kwargs):
self.calls.append((prompt, kwargs))
tokens = min(len(prompt.split()) + 1, kwargs.get("max_length", 4096))
return {"input_ids": torch.ones((1, tokens), dtype=torch.long)}
@pytest.fixture
def pipeline():
pipeline = object.__new__(AbliterationPipeline)
config = SimpleNamespace(
max_position_embeddings=64, use_cache=True, vocab_size=256,
text_config=SimpleNamespace(max_position_embeddings=4096, use_cache=True, vocab_size=256),
)
pipeline.handle = SimpleNamespace(
model=SimpleNamespace(
config=config,
generation_config=SimpleNamespace(use_cache=True),
_supports_logits_to_keep=lambda: True,
get_output_embeddings=lambda: SimpleNamespace(
weight=torch.empty(256, 16, device="meta", dtype=torch.float32),
),
),
tokenizer=RecordingTokenizer(),
)
pipeline.concept = SimpleNamespace(id="style", battery={})
pipeline._evaluation_battery = None
pipeline._evaluation_battery_supplied = False
pipeline._skip_collateral = False
pipeline._repair_enabled = False
pipeline._refusal_eval_size = 2
pipeline._refusal_eval_max_new_tokens = 4
pipeline.seed = 7
pipeline.max_seq_length = None
pipeline.harmless_prompts = []
pipeline._active_stage = "validate"
return pipeline
def rendered(prompts):
return ["<chat>" + prompt + "</chat>" for prompt in prompts]
@pytest.mark.parametrize("stage", ["validate", "excise"])
def test_named_pack_batteries_do_not_replace_measurement_anchors(pipeline, stage):
pipeline.concept.battery = {"choices": ["choice prompt"], "items": ["a named string group"]}
pipeline._skip_collateral = True
workloads, _ = pipeline._phase_forward_workloads(stage, 16, 256)
calls = pipeline.handle.tokenizer.calls
anchors = list(_COHERENCE_ANCHORS)
assert [prompt for prompt, _ in calls] == rendered(
anchors + ["choice prompt", "a named string group"],
)
assert all(options["max_length"] == 63 for _, options in calls[:len(anchors)])
assert all(options["max_length"] == 60 for _, options in calls[len(anchors):])
assert any(not workload.use_cache for workload in workloads)
assert any(workload.use_cache for workload in workloads)
def test_skip_collateral_keeps_readout_without_inventing_generation(pipeline):
pipeline._skip_collateral = True
workloads, _ = pipeline._phase_forward_workloads("validate", 16, 256)
assert [prompt for prompt, _ in pipeline.handle.tokenizer.calls] == rendered(_COHERENCE_ANCHORS)
assert workloads
assert all(not workload.use_cache for workload in workloads)
@pytest.mark.parametrize("stage", ["validate", "excise"])
def test_evaluation_uses_supplied_battery_metadata_and_bound_measure_prompts(pipeline, stage):
evaluator = object.__new__(ModelEvaluator)
evaluator.prompts = ["custom measurement anchor"]
pipeline._evaluation_battery = SimpleNamespace(
measure=evaluator.measure,
prompts=["supplied refusal prompt"],
decoding={"max_new_tokens": 7},
skip_collateral=False,
behavioural_battery=None,
)
pipeline._evaluation_battery_supplied = True
pipeline.concept.battery = {"unused": ["must not be generated"]}
pipeline._phase_forward_workloads(stage, 16, 256)
calls = pipeline.handle.tokenizer.calls
assert [prompt for prompt, _ in calls] == rendered(
evaluator.prompts + ["supplied refusal prompt"] + list(_COHERENCE_ANCHORS),
)
assert calls[0][1]["max_length"] == 63
assert all(options["max_length"] == 57 for _, options in calls[1:])
def test_supplied_battery_can_disable_measure_and_collateral(pipeline):
pipeline._evaluation_battery = SimpleNamespace(
measure=None, skip_collateral=True, behavioural_battery=None,
)
pipeline._evaluation_battery_supplied = True
workloads, _ = pipeline._phase_forward_workloads("validate", 16, 256)
assert workloads == []
assert pipeline.handle.tokenizer.calls == []
def test_default_refusal_ignores_previous_unsupplied_evaluation(pipeline):
pipeline.concept.id = "refusal"
pipeline._evaluation_battery = SimpleNamespace(prompts=["previous run"])
workloads, _ = pipeline._phase_forward_workloads("validate", 16, 256)
assert workloads == []
assert pipeline.handle.tokenizer.calls == []
def test_template_and_context_settings_match_model_evaluator(pipeline):
pipeline._skip_collateral = True
pipeline.concept.battery = {"long": ["token " * 100]}
workloads, _ = pipeline._phase_forward_workloads("validate", 16, 256)
assert all(not options["add_special_tokens"]
for _, options in pipeline.handle.tokenizer.calls)
assert all(options == {
"tokenize": False, "add_generation_prompt": True, "enable_thinking": False,
} for _, options in pipeline.handle.tokenizer.templates)
# ModelEvaluator uses the outer context (64), even when text_config says 4096.
assert any(workload.tokens == 60 and workload.use_cache for workload in workloads)
assert max(workload.cache_tokens or 0 for workload in workloads) == 64
def test_missing_template_uses_raw_prompts_and_special_tokens(pipeline):
pipeline._skip_collateral = True
pipeline.handle.tokenizer.chat_template = None
pipeline._phase_forward_workloads("validate", 16, 256)
assert [prompt for prompt, _ in pipeline.handle.tokenizer.calls] == list(_COHERENCE_ANCHORS)
assert all(options["add_special_tokens"] for _, options in pipeline.handle.tokenizer.calls)
assert pipeline.handle.tokenizer.templates == []
def test_evaluation_rejects_generation_that_leaves_no_prompt_context(pipeline):
pipeline._refusal_eval_max_new_tokens = 64
with pytest.raises(PhaseAdmissionError, match="leave room in the context window"):
pipeline._phase_forward_workloads("validate", 16, 256)
@pytest.mark.parametrize("callback_name", ["measure", "generate", "resteer_probe", "kv_probe",
"behavioural_battery"])
def test_opaque_evaluation_callback_rejects_before_execution(pipeline, callback_name):
callback = Mock(side_effect=AssertionError("must not run during admission"))
pipeline._evaluation_battery = SimpleNamespace(**{callback_name: callback})
pipeline._evaluation_battery_supplied = True
message = callback_name + ".*OBLITERATUS_PHASE_ADMISSION=off"
with pytest.raises(PhaseAdmissionError, match=message):
pipeline._phase_forward_workloads("validate", 16, 256)
callback.assert_not_called()
assert pipeline.handle.tokenizer.calls == []
def test_supplied_pack_callback_uses_its_actual_prompt_groups(pipeline):
from obliteratus.concepts.evaluation import pack_battery
pipeline._evaluation_battery = SimpleNamespace(
measure=None, skip_collateral=True,
behavioural_battery=pack_battery({"real": ["callback anchor"]}),
)
pipeline._evaluation_battery_supplied = True
pipeline.concept.battery = {"unused": ["unrelated large prompt " * 100]}
pipeline._phase_forward_workloads("validate", 16, 256)
calls = pipeline.handle.tokenizer.calls
assert [prompt for prompt, _ in calls] == rendered(["callback anchor"])
def test_supplied_battery_with_task_scorers_rejects_before_execution(pipeline):
from obliteratus.concepts.evaluation import EvaluationBattery
from obliteratus.concepts.scoring import ScorerSpec, ScoringExample, ScoringTask
generate = Mock(side_effect=AssertionError("must not run during admission"))
task = ScoringTask("answers", ScorerSpec("exact_answer", 1),
(ScoringExample("e0", "Capital of France?", "Paris"),))
pipeline._evaluation_battery = EvaluationBattery(
generate, skip_collateral=True, scorers=[task],
)
pipeline._evaluation_battery.generate = None
pipeline._evaluation_battery_supplied = True
with pytest.raises(PhaseAdmissionError, match="scorers.*OBLITERATUS_PHASE_ADMISSION=off"):
pipeline._phase_forward_workloads("validate", 16, 256)
generate.assert_not_called()
assert pipeline.handle.tokenizer.calls == []