File size: 4,667 Bytes
f45837e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
"""The split and the leakage guard.

These are the tests that matter most in this repo. Every claim about the
rubrics being untainted reduces to `assert_minable` being correct and being
called, so both are tested directly rather than assumed.
"""

from __future__ import annotations

import json
import sys
from pathlib import Path

import pytest

ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT))

import split as split_mod


@pytest.fixture(scope="module")
def sp():
    return split_mod.Split(split_mod.load_manifest())


def test_uses_the_frozen_development_split(sp):
    assert len(sp.source) == 5
    assert len(sp.evaluation) == 15


def test_split_is_disjoint(sp):
    assert not set(sp.source) & set(sp.evaluation)


def test_source_fraction_is_one_quarter(sp):
    assert sp.train_fraction == pytest.approx(0.25)


def test_every_tier_is_represented_in_train():
    """A train set missing a whole difficulty class would silently bias the
    rubrics toward whatever complexity survived the sample."""
    manifest = split_mod.load_manifest()
    tiers = {e["tier"] for e in manifest["source_competitions"]}
    assert tiers == {"low", "medium", "high"}


def test_eval_competition_is_refused(sp):
    victim = sp.evaluation[0]
    with pytest.raises(SystemExit, match="LEAKAGE"):
        sp.assert_minable(victim)


def test_every_eval_competition_is_refused(sp):
    """Not a spot check. All held-out development competitions are unreachable."""
    for comp in sp.evaluation:
        with pytest.raises(SystemExit):
            sp.assert_minable(comp)


def test_every_train_competition_is_allowed(sp):
    for comp in sp.source:
        sp.assert_minable(comp)


def test_unknown_competition_is_refused(sp):
    """Allow-list, not deny-list: a typo or a renamed slug must fail closed."""
    with pytest.raises(SystemExit, match="LEAKAGE GUARD"):
        sp.assert_minable("titanic")
    with pytest.raises(SystemExit, match="LEAKAGE GUARD"):
        sp.assert_minable("dog-breed-identificatio")     # one char short


def test_guard_normalizes_paths_and_case(sp):
    comp = sp.source[0]
    sp.assert_minable(comp.upper())
    sp.assert_minable(f"  {comp}  ")
    sp.assert_minable(f"/some/data/dir/{comp}/")
    victim = sp.evaluation[0]
    with pytest.raises(SystemExit):
        sp.assert_minable(f"/data/{victim.upper()}/prepared")


def test_overlapping_manifest_is_rejected():
    """A broken manifest must fail loudly at construction, not mine quietly."""
    bad = {"source_competitions": [{"id": "a"}, {"id": "b"}],
           "evaluation_competitions": [{"id": "b"}], "frozen": True}
    with pytest.raises(SystemExit, match="SPLIT IS BROKEN"):
        split_mod.Split(bad)


def test_unfrozen_manifest_is_refused_in_strict_mode(tmp_path, monkeypatch):
    manifest = split_mod.load_manifest()
    manifest["frozen"] = False
    path = tmp_path / "thawed.json"
    path.write_text(json.dumps(manifest))
    monkeypatch.setattr(split_mod, "MANIFEST", path)
    monkeypatch.setattr(split_mod, "_SPLIT", None)
    with pytest.raises(SystemExit, match="not frozen"):
        split_mod.get(strict=True)


def test_default_manifest_is_the_canonical_experiment_manifest():
    from shared.protocol import SPLIT_MANIFEST
    assert split_mod.MANIFEST.resolve() == SPLIT_MANIFEST.resolve()


# ------------------------------------------- the mirror guard, for measuring
def test_train_competition_is_refused_by_the_measurement_guard(sp):
    """Scoring rubrics on the competitions they were mined from would report
    memorization as transfer -- and it would look like a great result."""
    for comp in sp.source:
        with pytest.raises(SystemExit, match="CONTAMINATED MEASUREMENT"):
            sp.assert_evaluation_only(comp)


def test_every_eval_competition_is_allowed_for_measurement(sp):
    for comp in sp.evaluation:
        sp.assert_evaluation_only(comp)


def test_unknown_competition_is_refused_by_both_guards(sp):
    with pytest.raises(SystemExit):
        sp.assert_evaluation_only("titanic")
    with pytest.raises(SystemExit):
        sp.assert_minable("titanic")


def test_the_two_guards_are_exact_complements(sp):
    """No competition may pass both, and none of the 75 may fail both."""
    for comp in sp.source + sp.evaluation:
        passed_mine = passed_eval = False
        try:
            sp.assert_minable(comp); passed_mine = True
        except SystemExit:
            pass
        try:
            sp.assert_evaluation_only(comp); passed_eval = True
        except SystemExit:
            pass
        assert passed_mine != passed_eval, f"{comp} passed {passed_mine=} {passed_eval=}"