"""Calibrated screening study (O06) on synthetic candidate fixtures. Each candidate has a recorded per-row outcome table (development battery, task battery and one paired collateral endpoint) and stage-0 diagnostics. Synthetic fixtures: not evidence for real models. """ from __future__ import annotations import json import random import pytest from obliteratus.analysis.screening_report import ( auc, predictor_quality, run_screening_study, spearman, ) from obliteratus.analysis.screening_spec import ( ScreeningRefused, TableEvaluator, resolve_spec, ) from obliteratus.cli import main from obliteratus.experiments.registry_store import ExposedRows, Registry, text_sha256 pytestmark = pytest.mark.cpu DEV, TASK, COLL = 48, 40, 20 KINDS = { # (dev rate, task rate, collateral loss rate, stage-0 auc) "good": (0.85, 0.85, 0.0, 0.9), "bad": (0.1, 0.1, 0.0, 0.55), "misleading": (0.1, 0.1, 0.0, 0.99), "hidden": (0.9, 0.95, 0.0, 0.5), "harmful": (0.85, 0.85, 0.5, 0.9), } def _flags(rng, n, rate): k = round(rate * n) flags = [True] * k + [False] * (n - k) rng.shuffle(flags) return flags def build(counts, **overrides): rows = {"development": [{"id": f"d{i}", "text": f"dev prompt {i}"} for i in range(DEV)], "task": [{"id": f"t{i}", "text": f"task prompt {i}"} for i in range(TASK)], "collateral": {"general": [{"id": f"g{i}", "text": f"general prompt {i}"} for i in range(COLL)]}} candidates, table = [], {} for kind, n in counts.items(): dev, task, loss, score = KINDS[kind] for j in range(n): cid = f"{kind}-{j:02d}" rng = random.Random(cid) candidates.append({"id": cid, "stage0": {"auc": score + 0.003 * j, "kl": 1 - score}}) losses = _flags(rng, COLL, loss) table[cid] = { "development": dict(zip((r["id"] for r in rows["development"]), map(int, _flags(rng, DEV, dev)))), "task": dict(zip((r["id"] for r in rows["task"]), map(int, _flags(rng, TASK, task)))), "collateral": {"general": {r["id"]: [1, int(not lost)] for r, lost in zip(rows["collateral"]["general"], losses)}}} spec = {"schema": "obliteratus.screening-spec/1", "candidates": candidates, "rows": rows, "stage0": {"score": "auc", "threshold": 0.7}, "usefulness": {"min_task_rate": 0.6, "collateral_margin": 0.2}, "costs": {"stage0_forwards_per_candidate": 10, "forwards_per_generation": 8}} for key, value in overrides.items(): spec[key] = {**spec.get(key, {}), **value} if isinstance(value, dict) else value return spec, {"schema": "obliteratus.screening-outcomes/1", "candidates": table} def study(counts, **overrides): spec, outcomes = build(counts, **overrides) return run_screening_study(spec, TableEvaluator(outcomes)) def test_calibrated_ladder_saves_cost_and_is_deterministic(): report = study({"good": 30, "bad": 30}) ladder = report["arms"]["ladder"] assert ladder["verdict"] == {"verdict": "improvement", "reasons": []} assert report["recommendation"] == "geometry ladder" assert ladder["cost_saving"]["min"] > 0.2 rule = report["predictor_quality"]["stage0_rule"] assert rule["role"] == "earned" and rule["prediction"] == "adequate" for seed in ladder["seeds"]: assert seed["false_rejections"] == [] and seed["confirmation_candidate_unchanged"] assert seed["audit"]["audited"] >= 1 and seed["endpoints_complete"] for run in ladder["runs"]: assert len(run["exclusions"]) == 30 and all("reason" in e for e in run["exclusions"]) assert report["spec"]["stage1"]["method"] == "mixture-martingale" assert "dev prompt" not in json.dumps(report) # row texts never written again = study({"good": 30, "bad": 30}) assert again["report_sha256"] == report["report_sha256"] def test_misleading_geometry_is_not_promoted_and_prediction_is_weak(): report = study({"good": 10, "bad": 10, "misleading": 10}) misleading = {f"misleading-{j:02d}" for j in range(10)} for name in ("development", "ladder"): for run in report["arms"][name]["runs"]: assert not misleading & set(run["finalists"]) assert not any(report["reference"]["candidates"][c]["useful"] for c in misleading) rule = report["predictor_quality"]["stage0_rule"] assert rule["prediction"] == "weak" and rule["role"] == "not earned" assert abs(rule["spearman"]) < 0.2 ladder = report["arms"]["ladder"]["verdict"] assert ladder["verdict"] == "no improvement" assert any("did not earn its role" in r for r in ladder["reasons"]) def test_good_candidate_with_poor_stage0_score_is_caught_by_audit_and_accounting(): report = study({"good": 10, "bad": 10, "hidden": 1}, audit={"fraction": 1.0}) rule = report["predictor_quality"]["stage0_rule"] assert rule["useful_failing_threshold"] == ["hidden-00"] for seed in report["arms"]["ladder"]["seeds"]: assert seed["false_rejections"] == ["hidden-00"] assert seed["rescued_by_audit"] == ["hidden-00"] and seed["missed"] == [] assert seed["audit"]["useful_found"] == 1 assert seed["audit"]["estimated_false_rejections"] == pytest.approx(1.0) # The development-only arm screens on task behaviour and keeps it. assert all(s["false_rejections"] == [] for s in report["arms"]["development"]["seeds"]) # Without an audit it is missed, the confirmation candidate changes, and the verdict says so. blind = study({"good": 10, "bad": 10, "hidden": 1}, audit={"fraction": 0.0, "min_candidates": 0}) seeds = blind["arms"]["ladder"]["seeds"] assert all(s["missed"] == ["hidden-00"] and not s["confirmation_candidate_unchanged"] for s in seeds) assert blind["arms"]["ladder"]["verdict"]["verdict"] == "no improvement" def test_collateral_harm_blocks_usefulness_and_endpoints_are_never_skipped(): report = study({"good": 4, "harmful": 2, "bad": 4}) reference = report["reference"]["candidates"] assert not reference["harmful-00"]["useful"] assert reference["harmful-00"]["collateral"]["general"]["status"] == "FAIL" for name in ("development", "ladder"): for run in report["arms"][name]["runs"]: for cid in run["finalists"]: assert reference[cid]["endpoints_evaluated"] == ["task", "general"] spec, outcomes = build({"good": 3}) del outcomes["candidates"]["good-01"]["collateral"]["general"]["g3"] with pytest.raises(ScreeningRefused, match="final endpoints are never skipped"): run_screening_study(spec, TableEvaluator(outcomes)) def test_verdict_is_conservative(): few = study({"good": 30, "bad": 30}, seeds=[0]) assert few["arms"]["ladder"]["verdict"]["verdict"] == "no improvement" assert few["recommendation"] == "no improvement" small = study({"good": 3, "bad": 3}) # too few useful candidates to bound false rejection reasons = small["arms"]["ladder"]["verdict"]["reasons"] assert any("false-rejection upper bound" in r for r in reasons) none = study({"bad": 6}) assert any("no useful candidate" in r for r in none["arms"]["development"]["verdict"]["reasons"]) def test_confirmation_rows_and_results_are_refused(tmp_path): spec, outcomes = build({"good": 2, "bad": 2}) run = tmp_path / "run" registry = Registry(run) registry.create() digest = text_sha256("task prompt 5") frozen = {"target_key": {"target": "t", "method": "m", "model": "x", "task": "a", "backend": "transformers"}, "amends": None, "parent": None, "hypothesis": None, "recipe": None, "scorers": [], "thresholds": None, "seeds": None, "protocol": None, "confirmation": {"pack": "p", "pack_sha256": "0" * 64, "revision": None, "role": "calibrate", "content_digest": None, "partition_digest": None, "tasks": [], "n_rows": 1, "rows_sha256": "sha256:" + "1" * 64, "record_ids_sha256": "sha256:" + "2" * 64, "row_digests": [digest]}} document = registry.write("confirmation", frozen, "tester") run_screening_study(spec, TableEvaluator(outcomes), registry_dirs=[run]) # not yet exposed registry.append("evaluate", document["id"], "tester") with pytest.raises(ExposedRows, match="never feeds screening"): run_screening_study(spec, TableEvaluator(outcomes), registry_dirs=[run / "registry"]) locked, _ = build({"good": 1}) locked["rows"]["development"][0]["role"] = "test" with pytest.raises(ScreeningRefused, match="locked role"): resolve_spec(locked) leaked = json.loads(json.dumps(outcomes)) leaked["candidates"]["good-00"]["confirmation"] = {"outcome": "supported"} with pytest.raises(ScreeningRefused, match="confirmation results never feed"): TableEvaluator(leaked) def test_stage_rules_are_declared_and_digest_bound(): spec, outcomes = build({"good": 2, "bad": 2}) report = run_screening_study(spec, TableEvaluator(outcomes)) run_screening_study(spec, TableEvaluator(outcomes), expected_spec_sha256=report["spec_sha256"]) changed, _ = build({"good": 2, "bad": 2}, stage0={"threshold": 0.6}) with pytest.raises(ScreeningRefused, match="changed after declaration"): run_screening_study(changed, TableEvaluator(outcomes), expected_spec_sha256=report["spec_sha256"]) bad_specs = [ ({"stage1": {"method": "naive-peeking"}}, "does not keep its error guarantee"), ({"stage1": {"method": "bayes"}}, "stage1.method"), ({"stage1": {"alpha": 0.7}}, "alpha"), ({"stage1": {"batch": 0}}, "batch"), ({"stage0": {"threshold": None}}, "declared together"), ({"seeds": [1, 1]}, "distinct"), ({"audit": {"fraction": 2}}, "audit.fraction"), ({"stage1": {"surprise": 1}}, "Unknown"), ({"max_candidates": 2}, "bounded reference"), ] for override, message in bad_specs: with pytest.raises(ScreeningRefused, match=message): resolve_spec(build({"good": 2, "bad": 2}, **override)[0]) overlap, _ = build({"good": 1}) overlap["rows"]["task"][0]["text"] = "dev prompt 0" with pytest.raises(ScreeningRefused, match="overlap"): resolve_spec(overlap) for broken, message in (({"candidates": []}, "candidates"), ({"schema": "other"}, "schema"), ({"rows": {"development": [{"id": "x"}]}}, "sha256")): with pytest.raises(ScreeningRefused, match=message): resolve_spec({**spec, **broken}) def test_fixed_sample_method_and_development_budget(): report = study({"good": 12, "bad": 12}, stage1={"method": "fixed-sample", "max_rows": 16}) run = report["arms"]["development"]["runs"][0] assert all(s["rows"] == 16 for s in run["stage1"].values()) assert report["sequential_method"] == "fixed-sample" capped = study({"good": 12, "bad": 12}, stage1={"max_total_rows": 40}) run = capped["arms"]["development"]["runs"][0] assert run["cost"]["development_generations"] <= 40 assert any(s.get("reason") == "unresolved within the stage-1 budget" for s in run["stage1"].values()) def test_rank_helpers_and_predictor_without_stage0_rule(): assert spearman([1, 2, 3, 4], [10, 20, 30, 40]) == pytest.approx(1.0) assert spearman([1, 1, 1], [1, 2, 3]) is None and spearman([1, 2], [1, 2]) is None assert auc([0.9, 0.1], [True, False]) == 1.0 and auc([0.5], [True]) is None spec, outcomes = build({"good": 3, "bad": 3}, stage0={"score": None, "threshold": None}) report = run_screening_study(spec, TableEvaluator(outcomes)) assert report["predictor_quality"]["stage0_rule"] is None assert set(report["predictor_quality"]["diagnostics"]) == {"auc", "kl"} lower = resolve_spec(build({"good": 3, "bad": 3}, stage0={"score": "kl", "threshold": 0.3, "higher_is_better": False})[0]) found = predictor_quality(lower, report["reference"]["candidates"]) assert found["diagnostics"]["kl"]["orientation"] == "lower is better" assert found["stage0_rule"]["spearman"] > 0.5 def test_evaluator_contract_refusals(): evaluator = TableEvaluator(build({"good": 1})[1]) with pytest.raises(ScreeningRefused, match="No outcomes"): evaluator.full("missing") with pytest.raises(ScreeningRefused, match="no development outcome"): evaluator.development("good-00", ["nope"]) with pytest.raises(ScreeningRefused, match="Outcomes schema"): TableEvaluator({"schema": "x", "candidates": {}}) class Short(TableEvaluator): def development(self, candidate, row_ids): return super().development(candidate, row_ids)[:-1] spec, outcomes = build({"good": 2}) with pytest.raises(ScreeningRefused, match="development outcomes"): run_screening_study(spec, Short(outcomes)) def test_cli_run_and_simulate_null(tmp_path, capsys): spec, outcomes = build({"good": 30, "bad": 30}) (tmp_path / "spec.json").write_text(json.dumps(spec)) (tmp_path / "outcomes.json").write_text(json.dumps(outcomes)) argv = ["experiments", "screening", "run", "--spec", str(tmp_path / "spec.json"), "--outcomes", str(tmp_path / "outcomes.json"), "--out", str(tmp_path / "out")] main(argv) printed = json.loads(capsys.readouterr().out) first = (tmp_path / "out" / "screening.json").read_bytes() assert printed["recommendation"] == "geometry ladder" assert printed["verdicts"] == {"development": "improvement", "ladder": "improvement"} main(argv + ["--expect-spec-sha256", printed["spec_sha256"]]) capsys.readouterr() assert (tmp_path / "out" / "screening.json").read_bytes() == first with pytest.raises(SystemExit) as refused: main(argv + ["--expect-spec-sha256", "sha256:" + "0" * 64]) assert refused.value.code == 4 and "changed after declaration" in capsys.readouterr().err main(["experiments", "screening", "simulate-null", "--method", "naive-peeking", "--repetitions", "300"]) found = json.loads(capsys.readouterr().out) assert found["method"] == "naive-peeking" and found["rejection_rate"] > 0.05