"""AT-CV-03: exact synthetic truth table and deterministic cached validation.""" from dataclasses import replace import json import pytest import torch from obliteratus.concepts import ConceptDirection, ExtractionRecipe from obliteratus.concepts.extraction import extract_directions from obliteratus.concepts.gate import ( ActivationCache, DEFAULT_THRESHOLDS, GateFailed, LIMITATIONS, discriminating_tokens, effective_thresholds, enforce_gate, validate_direction, write_validation, ) pytestmark = pytest.mark.cpu def test_discriminating_tokens_requires_controls(): with pytest.raises(ValueError, match="control set"): discriminating_tokens(["positive example"], {}) @pytest.fixture(autouse=True) def single_thread(): previous = torch.get_num_threads() torch.set_num_threads(1) yield torch.set_num_threads(previous) def fixture(kind="planted", count=64): generator = torch.Generator().manual_seed(228) # Orthogonal nuisance variance makes random directions weaker without # compromising the planted coordinate. Identical nuisance in both classes # removes small-sample drift from the underpowered truth-table cases. noise = torch.randn(count, 64, generator=generator) noise[:, 0] = 0 recipe = ExtractionRecipe() if 1 < count < 32: noise.zero_() noise[:, 1] = torch.linspace(-100, 100, count) recipe = replace(recipe, denoise={"control_pc_variance": 1.0}) positive, negative = noise.clone(), noise.clone() if kind != "null": positive[:, 0] = 1 negative[:, 0] = -1 pos, controls = {0: positive}, {"neutral": {0: negative}} direction = extract_directions(pos, controls, recipe, seed=228, concept_id="planted") cache = ActivationCache( pos, controls, readouts={"final_token": (pos, controls), "mean_pool": (pos, controls)}, masked=(pos, controls), texts=(["topic"] * count, {"neutral": ["topic"] * count}), ) if kind in {"lexical", "suffix"}: cache.texts = (["topic Calibration marker."] * count, {"neutral": ["topic"] * count}) cache.top_tokens = discriminating_tokens(*cache.texts) if kind == "lexical": cache.masked = ({0: noise.clone()}, {"neutral": {0: noise.clone()}}) if kind == "sign_flipped": direction.directions = {0: -direction.directions[0]} if kind == "random": vector = torch.randn(64, generator=torch.Generator().manual_seed(256)) direction.directions = {0: vector / vector.norm()} if kind == "neighbour": neighbour = negative.clone() neighbour[:, 0] += 1.8 neighbour[:, 1] += 2 * (1 - 0.9**2) ** 0.5 cache.neighbours = {"injury": {0: neighbour}} return direction, cache @pytest.mark.parametrize("count,statistical_verdict", [(64, "pass"), (16, "warn")]) @pytest.mark.parametrize("mode", ["report", "enforce"]) def test_recorded_token_policy_failure_overrides_statistical_verdict(count, statistical_verdict, mode): direction, cache = fixture(count=count) assert validate_direction(direction, cache, mode=mode)["verdict"] == statistical_verdict direction.activation_identity["special_token_observations"] = [{ "label": "harmful", "batch_start": 0, "issue_counts": {"bos_required": count}, "gate_mode": "report", "override_gate": None, }] report = validate_direction(direction, cache, mode=mode) assert report["verdict"] == "fail" failures = [reason for reason in report["reasons"] if reason["severity"] == "fail"] assert len(failures) == 1 assert failures[0]["check"] == "special_token_policy" assert "bos_required" in failures[0]["message"] if mode == "report": enforce_gate(report) report["mode"] = "enforce" with pytest.raises(GateFailed, match="bos_required.*override-gate"): enforce_gate(report) report["override_gate"] = "Inspect the tokenizer's actual BOS behavior" enforce_gate(report) @pytest.mark.parametrize( "kind,count,verdict,check", [ ("planted", 64, "pass", None), ("sign_flipped", 64, "pass", None), ("random", 64, "fail", "heldout_auc"), ("null", 64, "fail", "heldout_auc"), ("lexical", 64, "fail", "confound"), ("suffix", 64, "fail", "lexical_confound"), ("neighbour", 64, "fail", "neighbour"), ("planted", 32, "pass", None), ("planted", 16, "warn", "power"), ("planted", 15, "fail", "power"), ("planted", 10, "fail", "power"), ("planted", 7, "fail", "power"), ("planted", 1, "fail", "power"), ], ) def test_truth_table(kind, count, verdict, check): direction, cache = fixture(kind, count) report = validate_direction(direction, cache) assert report["verdict"] == verdict if check: assert any(reason["check"] == check for reason in report["reasons"]) row = report["metrics"]["0"] if kind == "planted" and count == 64: assert row["heldout_auc"] >= 0.85 assert row["random_percentile"] >= 99 if kind == "null": assert row["heldout_auc"] < 0.75 if kind == "lexical": assert cache.top_tokens[0]["token"] == "calibration" assert row["masked_auc_drop"] > 0.2 if kind == "suffix": assert row["masked_auc_drop"] == 0 assert report["lexical_baseline"]["heldout_auc"] == 1 if kind == "sign_flipped": assert row["orientation"] == -1 assert row["heldout_auc"] == 1 if kind == "random": assert row["supplied_vector_heldout_auc"] < 0.75 assert row["recipe_refit_cv_auc"] > 0.85 if kind == "neighbour": assert row["neighbours"]["injury"]["cosine"] == pytest.approx(0.9) assert any("injury" in reason["message"] for reason in report["reasons"]) def test_saved_direction_exact_revalidation_and_reference(tmp_path): direction, cache = fixture() report = validate_direction(direction, cache, reference=direction.directions) assert report["metrics"]["0"]["reference_cosine"] == 1 assert report["metrics"]["0"]["readout_aucs"] == {"final_token": 1.0, "mean_pool": 1.0} direction.validation = report path = tmp_path / "direction.safetensors" direction.save(path) restored = ConceptDirection.load(path) assert restored.validation == report assert validate_direction(restored, cache, reference=restored.directions) == report write_validation(report, tmp_path / "validation.json") assert json.loads((tmp_path / "validation.json").read_text()) == report assert report["what_validation_does_not_establish"] == LIMITATIONS assert report["thresholds"] == DEFAULT_THRESHOLDS assert report["cv_recipe"]["layer_select"] == {"strategy": "cv_auc", "folds": 5} def test_cv_refits_and_does_not_trust_saved_scores(): direction, cache = fixture("null") direction.cv_scores = {0: [1.0] * 5} report = validate_direction(direction, cache) assert report["metrics"]["0"]["heldout_auc"] < 0.75 def test_multiple_controls_neighbour_target_auc_and_missing_budget(): direction, cache = fixture() cache.controls["other"] = {0: cache.controls["neutral"][0].clone()} cache.texts[1]["other"] = list(cache.texts[1]["neutral"]) # All readout pairs share the controls dictionary. cache.neighbours = {"neutrality": cache.controls["neutral"]} report = validate_direction(direction, cache, expected_neighbours=["neutrality", "unmeasured"]) row = report["metrics"]["0"] assert row["semantic_controls"]["other"] == { "target_auc": 1.0, "control_vs_remaining_auc": 0.5, } assert row["neighbours"]["neutrality"]["target_auc"] == 0.5 assert report["verdict"] == "warn" assert any("unmeasured" in reason["message"] for reason in report["reasons"]) def test_unmeasured_checks_warn_not_pass(): direction, cache = fixture() cache.readouts.clear() cache.masked = None cache.texts = None report = validate_direction(direction, cache) assert report["verdict"] == "warn" assert {r["check"] for r in report["reasons"]} == {"confound", "readout", "lexical_confound"} @pytest.mark.parametrize( "mode,override,blocked", [ ("enforce", None, True), ("enforce", "deliberate experiment", False), ("report", None, False), ], ) def test_enforcement(mode, override, blocked): direction, cache = fixture("null") report = validate_direction(direction, cache, mode=mode, override_gate=override) assert report["verdict"] == "fail" assert report["override_gate"] == override if blocked: with pytest.raises(GateFailed, match="before EXCISE"): enforce_gate(report) else: enforce_gate(report) def test_explicit_refusal_cannot_be_enforced(): direction, cache = fixture("null") direction.concept_id = "refusal" report = validate_direction(direction, cache, mode="enforce") assert report["mode"] == "report" assert report["verdict"] == "fail" enforce_gate(report) @pytest.mark.parametrize( "overrides", [ {"missing": 1}, {"min_heldout_auc": float("nan")}, {"min_heldout_auc": True}, {"min_heldout_auc": 2}, {"min_heldout_auc": 0.9}, {"min_samples": 1.5}, ], ) def test_invalid_thresholds(overrides): with pytest.raises(ValueError): effective_thresholds(overrides) def test_pack_thresholds_and_core_policy(): direction, cache = fixture("neighbour") thresholds = {"max_neighbour_abs_cos": 0.95, "warn_neighbour_abs_cos": 0.92} report = validate_direction(direction, cache, thresholds=thresholds) assert report["verdict"] == "pass" assert report["thresholds"]["max_neighbour_abs_cos"] == 0.95 with pytest.raises(ValueError, match="only tighten"): validate_direction(direction, cache, thresholds=thresholds, tier="core") assert effective_thresholds({"min_heldout_auc": 0.8}, tier="core")["min_heldout_auc"] == 0.8 @pytest.mark.parametrize( "kwargs", [ {"override_gate": " "}, {"mode": "off"}, {"random_directions": 0}, ], ) def test_invalid_options(kwargs): direction, cache = fixture() with pytest.raises(ValueError): validate_direction(direction, cache, **kwargs) def test_cache_and_reference_validation(): direction, cache = fixture() with pytest.raises(ValueError, match="Reference missing"): validate_direction(direction, cache, reference={}) with pytest.raises(ValueError, match="dimensions"): validate_direction(direction, cache, reference={0: torch.ones(2)}) cache.masked = ({0: cache.positive[0][:2]}, cache.controls) with pytest.raises(ValueError, match="sample shapes"): validate_direction(direction, cache) cache.masked = None cache.positive = {1: cache.positive[0]} with pytest.raises(ValueError, match="match direction layers"): validate_direction(direction, cache) def test_denoising_is_fitted_inside_folds(): from tests.test_concept_extraction import cv_recipe, planted_cache positive, controls = planted_cache() direction = extract_directions(positive, controls, cv_recipe(), seed=227, concept_id="planted") cache = ActivationCache(positive, controls) report = validate_direction(direction, cache) # Double precision validation may differ slightly; it uses the same fold/recipe fit. for layer, scores in direction.cv_scores.items(): assert report["metrics"][str(layer)]["recipe_refit_fold_aucs"] == pytest.approx(scores) assert report["selected_layers"] == [2] assert not any(r["layer"] in {0, 1, 3} for r in report["reasons"]) def test_extraction_pin_keeps_validation_diagnostic_cv(): from tests.test_concept_extraction import cv_recipe, planted_cache positive, controls = planted_cache() recipe = replace(cv_recipe(), extraction_layer={"offline": 2}) direction = extract_directions(positive, controls, recipe, seed=227, concept_id="planted", model_id="offline") assert direction.cv_scores == {} report = validate_direction(direction, ActivationCache(positive, controls)) assert report["selected_layers"] == [2] assert "extraction_layer" not in report["cv_recipe"] assert len(report["metrics"]["2"]["recipe_refit_fold_aucs"]) == 4 assert direction.extraction_layer_source == "pinned" def test_reference_threshold_and_pooling(): direction, cache = fixture() direction.recipe = replace(direction.recipe, pool_controls="concat") report = validate_direction(direction, cache, reference={0: -direction.directions[0]}) assert report["verdict"] == "fail" assert any(r["check"] == "reference" for r in report["reasons"]) def test_control_named_positive_can_have_a_different_size(): direction, cache = fixture() cache.controls["positive"] = {0: cache.controls["neutral"][0][:40]} cache.texts[1]["positive"] = cache.texts[1]["neutral"][:40] report = validate_direction(direction, cache) assert report["sample_counts"]["control:positive"] == 40 assert report["sample_counts"]["positive"] == 64