Spaces:
Running on Zero

File size: 13,007 Bytes
2cdd95e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
"""
tests/test_evaluation_tooling_phase4.py
=======================================
Acceptance & Unit Test Suite for Phase 4: Evaluation Tooling & Promotion Gates.

Validates:
  1. Shadow metrics computation (agreements, slice breakdown, errors, grounding, latency percentiles)
  2. Stratified review sampling (prioritizing errors, hallucinations, disagreements + control slice)
  3. Disagreement labeling interface (saving labels, updating overall judgments)
  4. 5 Promotion gates evaluation (labeled accuracy, bug class fixes, error bounds, memory, grounding)
  5. Downstream ranking impact evaluation (Spearman rho, bracket stability, zero scoring regressions)
"""

import json
import os
import tempfile
from pathlib import Path
from typing import Any, Dict

import pytest

from app.models.job import JobDescription
from scripts.evaluate_shadow_metrics import (
    analyze_shadow_logs,
    generate_markdown_report,
    percentile,
)
from scripts.generate_review_sample import (
    classify_log_for_review,
    generate_stratified_sample,
)
from scripts.label_disagreements import (
    apply_batch_labels,
    load_review_labels,
    save_review_labels,
)
from scripts.check_promotion_gates import (
    evaluate_gate_1_labeled_accuracy,
    evaluate_gate_2_error_rate,
    evaluate_gate_3_memory_safety,
    evaluate_gate_4_grounding,
    evaluate_gate_5_ranking_impact,
    run_promotion_gate_audit,
)
from scripts.evaluate_ranking_impact import (
    compute_pearson_correlation,
    compute_spearman_rank_correlation,
    load_sample_resumes_corpus,
    run_ranking_impact_audit,
)


# ---------------------------------------------------------------------------
# 1. Shadow Metrics Analysis Tests
# ---------------------------------------------------------------------------

def test_percentile_calculation():
    vals = [10.0, 20.0, 30.0, 40.0, 50.0]
    assert percentile(vals, 50.0) == 30.0
    assert percentile(vals, 95.0) == 48.0
    assert percentile([], 50.0) == 0.0


def test_analyze_shadow_logs_metrics():
    logs = [
        {
            "fileHash": "hash1",
            "docFormat": "pdf",
            "pageCount": 1,
            "isTwoColumn": False,
            "charLength": 1500,
            "fieldAgreements": {
                "emailExactMatch": True,
                "phoneExactMatch": True,
                "nameTokenF1": 1.0,
                "titleTokenF1": 0.90,
                "skillsJaccard": 0.80,
                "skillsPrecision": 0.85,
                "skillsRecall": 0.90,
                "expDeltaYears": 0.0,
                "educationCountMatch": True,
                "workExperienceCountMatch": True,
            },
            "validityFlags": {
                "emailValid": True,
                "phoneValid": True,
                "namePlausible": True,
                "datesParse": True,
            },
            "groundingFlags": {
                "allGrounded": True,
                "ungroundedEntities": [],
            },
            "slmLatencyMs": 1200.0,
            "isValidJson": True,
            "errorFlags": [],
        },
        {
            "fileHash": "hash2",
            "docFormat": "docx",
            "pageCount": 2,
            "isTwoColumn": True,
            "charLength": 3200,
            "fieldAgreements": {
                "emailExactMatch": False,
                "phoneExactMatch": True,
                "nameTokenF1": 0.80,
                "titleTokenF1": 0.60,
                "skillsJaccard": 0.50,
                "skillsPrecision": 0.60,
                "skillsRecall": 0.70,
                "expDeltaYears": 1.5,
                "educationCountMatch": False,
                "workExperienceCountMatch": True,
            },
            "validityFlags": {
                "emailValid": False,
                "phoneValid": True,
                "namePlausible": True,
                "datesParse": True,
            },
            "groundingFlags": {
                "allGrounded": False,
                "ungroundedEntities": ["skill:QuantumComputing"],
            },
            "slmLatencyMs": 1800.0,
            "isValidJson": True,
            "errorFlags": ["finish_reason_length"],
        },
    ]

    metrics = analyze_shadow_logs(logs)
    assert metrics["totalRuns"] == 2
    assert metrics["agreements"]["emailExactMatchPct"] == 50.0
    assert metrics["agreements"]["phoneExactMatchPct"] == 100.0
    assert metrics["agreements"]["avgNameTokenF1"] == 0.90
    assert metrics["agreements"]["avgTitleTokenF1"] == 0.75
    assert metrics["agreements"]["avgSkillsJaccard"] == 0.65
    assert metrics["agreements"]["avgExpDeltaYears"] == 0.75

    assert metrics["errors"]["invalidJsonCount"] == 0
    assert metrics["grounding"]["groundingFailureCount"] == 1
    assert metrics["grounding"]["groundingFailureRatePct"] == 50.0
    assert metrics["latency"]["p50Ms"] == 1500.0

    # Test report formatting
    report = generate_markdown_report(metrics)
    assert "Phase 4 Shadow Mode Evaluation Report" in report
    assert "Total Shadow Evaluations:** 2" in report


# ---------------------------------------------------------------------------
# 2. Stratified Sampling Tests
# ---------------------------------------------------------------------------

def test_classify_log_for_review():
    # Error classification
    err_log = {"errorFlags": ["json_invalid"], "isValidJson": False}
    cat, reasons = classify_log_for_review(err_log)
    assert cat == "error"
    assert "invalid_json_format" in reasons

    # Hallucination classification
    halluc_log = {"groundingFlags": {"allGrounded": False, "ungroundedEntities": ["company:FakeCorp"]}}
    cat, reasons = classify_log_for_review(halluc_log)
    assert cat == "hallucination"
    assert any("ungrounded_entities" in r for r in reasons)

    # Disagreement classification
    disagree_log = {
        "fieldAgreements": {"emailExactMatch": False, "titleTokenF1": 0.5},
        "groundingFlags": {"allGrounded": True},
    }
    cat, reasons = classify_log_for_review(disagree_log)
    assert cat == "disagreement"
    assert "email_mismatch" in reasons
    assert any("title_f1_low" in r for r in reasons)

    # Agreement control classification
    agree_log = {
        "fieldAgreements": {"emailExactMatch": True, "titleTokenF1": 1.0, "skillsJaccard": 0.9},
        "groundingFlags": {"allGrounded": True},
    }
    cat, reasons = classify_log_for_review(agree_log)
    assert cat == "agreement_control"


def test_generate_stratified_sample_quotas():
    logs = []
    # 5 errors, 5 hallucinations, 10 disagreements, 20 controls
    for i in range(5):
        logs.append({"id": f"err_{i}", "errorFlags": ["timeout"], "isValidJson": True})
    for i in range(5):
        logs.append({"id": f"hal_{i}", "groundingFlags": {"allGrounded": False, "ungroundedEntities": [f"skill:Skill{i}"]}})
    for i in range(10):
        logs.append({"id": f"dis_{i}", "fieldAgreements": {"emailExactMatch": False, "titleTokenF1": 0.4}, "groundingFlags": {"allGrounded": True}})
    for i in range(20):
        logs.append({"id": f"agr_{i}", "fieldAgreements": {"emailExactMatch": True, "titleTokenF1": 1.0}, "groundingFlags": {"allGrounded": True}})

    sample = generate_stratified_sample(logs, target_sample_size=15, control_ratio=0.20)
    assert sample["actualSampleSize"] == 15
    # Should include errors and hallucinations first
    assert sample["distribution"]["error"] == 5
    assert sample["distribution"]["hallucination"] == 5
    assert sample["distribution"]["agreement_control"] >= 3


# ---------------------------------------------------------------------------
# 3. Labeling Interface & Storage Tests
# ---------------------------------------------------------------------------

def test_label_storage_roundtrip():
    with tempfile.TemporaryDirectory() as tmp_dir:
        labels_path = Path(tmp_dir) / "test_labels.json"
        data = load_review_labels(labels_path)
        assert data["labels"] == {}

        batch = [
            {
                "fileHash": "hash_abc",
                "overallJudgment": "slm_right",
                "fieldJudgments": {"candidateName": "both_right", "jobTitle": "slm_right"},
                "notes": "Preserved parentheses in title",
            },
            {
                "fileHash": "hash_xyz",
                "overallJudgment": "both_right",
                "fieldJudgments": {"candidateName": "both_right", "email": "both_right"},
                "notes": "Clean agreement",
            },
        ]

        applied = apply_batch_labels(batch, labels_path)
        assert applied == 2

        reloaded = load_review_labels(labels_path)
        assert len(reloaded["labels"]) == 2
        assert reloaded["labels"]["hash_abc"]["overallJudgment"] == "slm_right"


# ---------------------------------------------------------------------------
# 4. Promotion Gates Evaluation Tests
# ---------------------------------------------------------------------------

def test_gate_1_labeled_accuracy_pass():
    labels_store = {
        "labels": {
            f"item_{i}": {
                "fileHash": f"h_{i}",
                "fieldJudgments": {
                    "candidateName": "both_right",
                    "email": "slm_right" if i == 0 else "both_right",
                    "phone": "both_right",
                    "jobTitle": "slm_right" if i in (1, 2) else "both_right",
                    "totalExperienceYears": "both_right",
                },
                "notes": "fixed title truncation" if i in (1, 2) else "",
            }
            for i in range(6)
        }
    }
    g1 = evaluate_gate_1_labeled_accuracy(labels_store)
    assert g1["status"] == "PASS"
    assert g1["all_critical_fields_pass"] is True
    assert g1["bug_classes"]["title_truncation_preserved"]["count"] == 2


def test_gate_2_error_rate_thresholds():
    # Pass case
    pass_metrics = {"errors": {"totalErrorRatePct": 0.45, "invalidJsonRatePct": 0.1, "timeoutRatePct": 0.35}}
    g2_pass = evaluate_gate_2_error_rate(pass_metrics)
    assert g2_pass["status"] == "PASS"

    # Fail case
    fail_metrics = {"errors": {"totalErrorRatePct": 1.85, "invalidJsonRatePct": 1.2, "timeoutRatePct": 0.65}}
    g2_fail = evaluate_gate_2_error_rate(fail_metrics)
    assert g2_fail["status"] == "FAIL"


def test_gate_3_and_gate_4():
    # Memory safety: zero trips
    g3 = evaluate_gate_3_memory_safety(memory_guard_incidents=0)
    assert g3["status"] == "PASS"
    g3_bad = evaluate_gate_3_memory_safety(memory_guard_incidents=2)
    assert g3_bad["status"] == "FAIL"

    # Grounding: < 2%
    g4_pass = evaluate_gate_4_grounding({"grounding": {"groundingFailureRatePct": 0.8}})
    assert g4_pass["status"] == "PASS"
    g4_fail = evaluate_gate_4_grounding({"grounding": {"groundingFailureRatePct": 3.4}})
    assert g4_fail["status"] == "FAIL"


def test_full_promotion_gate_audit_verdict():
    audit = run_promotion_gate_audit()
    assert "gates" in audit
    assert "overallVerdict" in audit
    assert "gate1_labeled_accuracy" in audit["gates"]
    assert "gate2_error_rate" in audit["gates"]
    assert "gate3_memory_safety" in audit["gates"]
    assert "gate4_grounding_hallucination" in audit["gates"]
    assert "gate5_ranking_impact" in audit["gates"]


# ---------------------------------------------------------------------------
# 5. Ranking Impact Correlation & Regression Tests
# ---------------------------------------------------------------------------

def test_rank_correlation_math():
    # Identical rankings: rho = 1.0
    assert compute_spearman_rank_correlation([1, 2, 3, 4], [1, 2, 3, 4]) == 1.0
    # Reversed rankings: rho = -1.0
    assert compute_spearman_rank_correlation([1, 2, 3, 4], [4, 3, 2, 1]) == -1.0
    # Slight swap: high correlation
    rho = compute_spearman_rank_correlation([1, 2, 3, 4], [2, 1, 3, 4])
    assert rho >= 0.80

    # Pearson correlation
    assert compute_pearson_correlation([80.0, 90.0, 60.0], [80.0, 90.0, 60.0]) == 1.0


def test_ranking_impact_full_pipeline_audit():
    corpus = load_sample_resumes_corpus()
    assert len(corpus) >= 4

    target_jd = JobDescription(
        job_id="JD_TEST_BACKEND",
        title="Senior Backend & Cloud Engineer",
        raw_text="Required: 5+ years Go, Python, Kubernetes, AWS, Docker, PostgreSQL. BS in CS.",
        required_skills=["go", "python", "kubernetes", "docker", "postgresql"],
        preferred_skills=["aws", "grpc", "kafka", "redis"],
        mandatory_skills=["go", "kubernetes"],
        minimum_experience_years=5.0,
        education_requirements=["bachelor"],
    )

    audit = run_ranking_impact_audit(corpus, target_jd)
    assert audit["total_candidates"] == len(corpus)
    # Spearman rank correlation between legacy and SLM must remain high
    assert audit["spearman_rank_correlation"] >= 0.80
    assert audit["score_pearson_correlation"] >= 0.85
    assert audit["bracket_agreement_pct"] >= 75.0
    assert audit["mean_score_delta"] <= 8.0