File size: 8,659 Bytes
75878d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
"""
tests/test_corpus_loop.py
========================
Automated smoke test for the corpus loop (Phase 3 completion).

Verifies the invariant the manual smoke run got wrong: a *source-disjoint*
split, where every source_id appears in exactly one of {train, val, hard_dev}
β€” not merely text-disjoint within a source.

Covers:
  * Full pipeline on the smoke fixture produces text- AND source-disjoint splits.
  * MANIFEST.json records split_policy / split_seed / source_counts /
    source_overlap / text_overlap, with every overlap count zero.
  * At least 3 independent source groups; experiment reaches FROZEN_CORPUS.
  * Deterministic: two clean runs produce the same corpus_hash.
  * Split rejects a corpus with too few source groups (< 3).
  * Corpus gate worker flags a nonzero source overlap and passes a clean one.
"""

from __future__ import annotations

import json
import os
import shutil
import sys
import tempfile
from pathlib import Path

_REPO_ROOT = Path(__file__).resolve().parents[1]

# Point the registry at a throwaway temp DB BEFORE importing the corpus loop so
# the module-level init_db() in corpus_loop never touches state/registry.db.
_TMP_REGISTRY = Path(tempfile.mkdtemp(prefix="corpus_test_registry_")) / "registry.db"
os.environ["TINYMETATRON_DB"] = str(_TMP_REGISTRY)
if str(_REPO_ROOT) not in sys.path:
    sys.path.insert(0, str(_REPO_ROOT))

import db  # noqa: E402
db.set_db_path(str(_TMP_REGISTRY))
db.init_db(str(_TMP_REGISTRY))

from loops import corpus_loop  # noqa: E402

import pytest  # noqa: E402

_FIXTURE = _REPO_ROOT / "tests" / "fixtures" / "corpus_smoke.jsonl"


# ── fixtures ──────────────────────────────────────────────────────────────────

@pytest.fixture
def clean_registry(tmp_path) -> str:
    """A fresh temp registry DB per test (clean test registry)."""
    p = str(tmp_path / "registry.db")
    db.set_db_path(p)
    db.init_db(p)
    return p


@pytest.fixture
def corpus_dir(tmp_path) -> Path:
    """Temp raw-corpus dir containing a copy of the smoke fixture."""
    d = tmp_path / "raw"
    d.mkdir()
    shutil.copy(_FIXTURE, d / "corpus_smoke.jsonl")
    return d


def _load_jsonl(path: Path) -> list[dict]:
    return [json.loads(l) for l in open(path, encoding="utf-8") if l.strip()]


def _texts(rows: list[dict]) -> set:
    return {r["text"] for r in rows}


def _sources(rows: list[dict]) -> set:
    return {r.get("source", r.get("domain", "unknown")) for r in rows}


# ── source-disjoint smoke ─────────────────────────────────────────────────────

def test_corpus_smoke_source_disjoint(clean_registry, corpus_dir, tmp_path) -> None:
    out = tmp_path / "out"
    summary = corpus_loop.run_corpus_pipeline({
        "exp_id": "exp-smoke-001",
        "corpus_dir": str(corpus_dir),
        "output_dir": str(out),
        "seed": 42,
        "train_pct": 0.80,
        "val_pct": 0.10,
    })

    corpus = out / "corpus"
    train = _load_jsonl(corpus / "train.jsonl")
    val = _load_jsonl(corpus / "val.jsonl")
    hard = _load_jsonl(corpus / "hard_dev.jsonl")

    tr_t, va_t, ha_t = _texts(train), _texts(val), _texts(hard)
    tr_s, va_s, ha_s = _sources(train), _sources(val), _sources(hard)

    # Every partition non-empty.
    assert train and val and hard, "all three splits must be non-empty"

    # Text-disjoint.
    assert tr_t.isdisjoint(va_t)
    assert tr_t.isdisjoint(ha_t)
    assert va_t.isdisjoint(ha_t)

    # Source-disjoint β€” the invariant the manual smoke run got wrong.
    assert tr_s.isdisjoint(va_s)
    assert tr_s.isdisjoint(ha_s)
    assert va_s.isdisjoint(ha_s)

    # At least 3 independent source groups overall.
    assert len(tr_s | va_s | ha_s) >= 3

    # MANIFEST.json records the split policy invariants.
    man = json.loads((out / "MANIFEST.json").read_text(encoding="utf-8"))
    assert man["split_policy"] == "source_disjoint_capped_v1"
    assert man["split_seed"] == 42
    assert man["source_counts"] == {
        "train": len(tr_s), "val": len(va_s), "hard_dev": len(ha_s),
    }
    for key in ("train_val", "train_hard_dev", "val_hard_dev"):
        assert man["source_overlap"][key] == 0, f"source_overlap[{key}] != 0"
        assert man["text_overlap"][key] == 0, f"text_overlap[{key}] != 0"
    assert sum(man["source_overlap"].values()) == 0
    assert sum(man["text_overlap"].values()) == 0

    # Gate passed β†’ experiment frozen.
    assert summary["corpus_hash"]
    exp = db.get_loop_experiment("exp-smoke-001")
    assert exp["state"] == "FROZEN_CORPUS"


# ── determinism ───────────────────────────────────────────────────────────────

def test_corpus_smoke_deterministic(clean_registry, corpus_dir, tmp_path) -> None:
    out1 = tmp_path / "out1"
    s1 = corpus_loop.run_corpus_pipeline({
        "exp_id": "exp-smoke-det-a",
        "corpus_dir": str(corpus_dir),
        "output_dir": str(out1),
        "seed": 42,
    })
    out2 = tmp_path / "out2"
    s2 = corpus_loop.run_corpus_pipeline({
        "exp_id": "exp-smoke-det-b",
        "corpus_dir": str(corpus_dir),
        "output_dir": str(out2),
        "seed": 42,
    })
    assert s1["corpus_hash"] == s2["corpus_hash"], (
        "deterministic split must produce identical corpus_hash across runs")


# ── too-few source groups rejected ────────────────────────────────────────────

def test_split_rejects_too_few_sources(clean_registry, tmp_path) -> None:
    raw = tmp_path / "raw"
    raw.mkdir()
    rows = [
        {"text": "Alpha source sentence one about network security fundamentals.",
         "source": "alpha"},
        {"text": "Alpha source sentence two about cryptographic hash functions.",
         "source": "alpha"},
        {"text": "Beta source sentence one about machine learning neural nets.",
         "source": "beta"},
        {"text": "Beta source sentence two about gradient descent optimization.",
         "source": "beta"},
    ]
    with open(raw / "in.jsonl", "w", encoding="utf-8") as f:
        for r in rows:
            f.write(json.dumps(r) + "\n")

    out = tmp_path / "out"
    with pytest.raises(RuntimeError, match="Worker error"):
        corpus_loop.run_corpus_pipeline({
            "exp_id": "exp-smoke-few",
            "corpus_dir": str(raw),
            "output_dir": str(out),
        })
    # Must not have been frozen.
    assert db.get_loop_experiment("exp-smoke-few")["state"] != "FROZEN_CORPUS"


# ── corpus gate worker: detects overlap, passes clean ─────────────────────────

def test_corpus_gate_detects_overlap(tmp_path) -> None:
    man = tmp_path / "MANIFEST.json"
    payload = {
        "split_policy": "source_disjoint_v1",
        "split_seed": 42,
        "source_counts": {"train": 2, "val": 2, "hard_dev": 1},
        "source_overlap": {"train_val": 1, "train_hard_dev": 0, "val_hard_dev": 0},
        "text_overlap": {"train_val": 0, "train_hard_dev": 0, "val_hard_dev": 0},
    }
    man.write_text(json.dumps(payload), encoding="utf-8")

    result = corpus_loop._run_worker(
        [sys.executable, "-m", "workers.corpus.corpus_gate", "--manifest", str(man)],
        tmp_path / "gate_art", 60,
    )
    assert result["metrics"]["source_overlap_total"] == 1
    # Gate pass_condition (source_overlap_total == 0) must evaluate to False.
    assert corpus_loop._eval_condition(
        result["metrics"]["source_overlap_total"], "==", 0) is False


def test_corpus_gate_passes_clean(tmp_path) -> None:
    man = tmp_path / "MANIFEST.json"
    payload = {
        "split_policy": "source_disjoint_v1",
        "split_seed": 42,
        "source_counts": {"train": 2, "val": 1, "hard_dev": 1},
        "source_overlap": {"train_val": 0, "train_hard_dev": 0, "val_hard_dev": 0},
        "text_overlap": {"train_val": 0, "train_hard_dev": 0, "val_hard_dev": 0},
    }
    man.write_text(json.dumps(payload), encoding="utf-8")

    result = corpus_loop._run_worker(
        [sys.executable, "-m", "workers.corpus.corpus_gate", "--manifest", str(man)],
        tmp_path / "gate_art", 60,
    )
    assert result["metrics"]["source_overlap_total"] == 0
    assert corpus_loop._eval_condition(
        result["metrics"]["source_overlap_total"], "==", 0) is True