On master: wip

This commit is contained in:
omkarande14
2026-09-29 10:38:26 +05:30
parent 10bd585f83
commit 673d181e68
8 changed files with 788 additions and 162 deletions
+66
View File
@@ -90,3 +90,69 @@ def test_selection_gate_requires_selection_split():
baseline_selection=None,
candidate_selection=None
)
def test_selection_gate_per_field_regression_rejection():
evaluator = StatisticalEvaluator()
hypo = ProposalHypothesis(
proposal_id="PROP-102",
area=PipelineArea.ENTITY_EXTRACTION,
title="Field regression",
description="Testing per-field regression",
rationale="Unit test"
)
# Baseline selection has 100% on both fields
gt = [
{"invoice_id": "1", "invoice_number": "A", "invoice_date": "2026-08-01"},
{"invoice_id": "2", "invoice_number": "B", "invoice_date": "2026-08-02"}
]
base_pred = [
{"invoice_id": "1", "invoice_number": "A", "invoice_date": "2026-08-01"},
{"invoice_id": "2", "invoice_number": "B", "invoice_date": "2026-08-02"}
]
# Candidate breaks invoice_date on invoice 2
cand_pred = [
{"invoice_id": "1", "invoice_number": "A", "invoice_date": "2026-08-01"},
{"invoice_id": "2", "invoice_number": "B", "invoice_date": "wrong_date"}
]
m_base, _ = evaluator.evaluate_predictions(base_pred, gt)
m_cand, _ = evaluator.evaluate_predictions(cand_pred, gt)
rec = evaluator.decide_acceptance(
hypothesis=hypo,
baseline_dev=m_base,
candidate_dev=m_cand,
baseline_selection=m_base,
candidate_selection=m_cand
)
assert rec.status == ExperimentStatus.REJECTED
assert any("PER-FIELD REGRESSION DETECTED" in r for r in rec.rejection_reasons)
def test_selection_gate_case_level_regression_without_explanation():
evaluator = StatisticalEvaluator()
hypo = ProposalHypothesis(
proposal_id="PROP-103",
area=PipelineArea.ENTITY_EXTRACTION,
title="Case regression",
description="Testing case-level regression",
rationale="Unit test"
)
gt = [{"invoice_id": "doc_1", "invoice_number": "A"}]
base_pred = [{"invoice_id": "doc_1", "invoice_number": "A"}]
cand_pred = [{"invoice_id": "doc_1", "invoice_number": "WRONG"}]
m_base, _ = evaluator.evaluate_predictions(base_pred, gt)
m_cand, _ = evaluator.evaluate_predictions(cand_pred, gt)
rec = evaluator.decide_acceptance(
hypothesis=hypo,
baseline_dev=m_base,
candidate_dev=m_cand,
baseline_selection=m_base,
candidate_selection=m_cand,
ledger_breakage_explanations=None # no explanation
)
assert rec.status == ExperimentStatus.REJECTED
assert any("CASE REGRESSIONS DETECTED" in r for r in rec.rejection_reasons)
+41 -15
View File
@@ -11,6 +11,7 @@ Comprehensive Unit Tests for the RRSI Framework:
import tempfile
import pytest
import hashlib
from pathlib import Path
from app.rrsi import (
@@ -145,8 +146,18 @@ def test_leakage_screening_detects_hardcoded_sample_tokens():
assert passed
assert len(findings) == 0
leaky_code = "if 'WMP/B/04531' in text: return 'WMP/B/04531'"
passed, findings = LeakageScreening.audit_code_changes(leaky_code)
# Synthetic forbidden token passed via runtime forbidden hashes
salt = "TEST_SALT_MOCK_"
token = "SYNTH_LEAKY_TOKEN_01"
norm = "synthleakytoken01"
h = hashlib.sha256((salt + norm).encode("utf-8")).hexdigest()
leaky_code = f"if '{token}' in text: return '{token}'"
passed, findings = LeakageScreening.audit_code_changes(
leaky_code,
salt=salt,
forbidden_hashes={h}
)
assert not passed
assert any("DATA LEAKAGE DETECTED" in f for f in findings)
@@ -155,30 +166,45 @@ def test_leakage_screening_detects_coordinate_overfitting():
overfitted_code = "if ymin >= 145 and ymax <= 320: return crop[120:850]"
passed, findings = LeakageScreening.audit_code_changes(overfitted_code)
assert not passed
assert any("COORDINATE OVERFITTING WARNING" in f for f in findings)
assert any("COORDINATE OVERFITTING DETECTED" in f for f in findings)
def test_generalization_split_screening():
# Healthy generalization
passed, _ = LeakageScreening.audit_split_generalization(calibration_delta=3.5, holdout_delta=2.8)
passed, _ = LeakageScreening.compare_dev_vs_selection_gain(
dev_gain=0.035, selection_gain=0.028, selection_ci_lower=0.015
)
assert passed
# Regression on holdout set
passed, msg = LeakageScreening.audit_split_generalization(calibration_delta=4.0, holdout_delta=-1.2)
# Regression / CI lower bound <= 0
passed, msg = LeakageScreening.compare_dev_vs_selection_gain(
dev_gain=0.040, selection_gain=-0.012, selection_ci_lower=-0.025
)
assert not passed
assert "GENERALIZATION FAILURE" in msg
assert "GENERALIZATION REJECTED" in msg
# Disproportionate overfit (calibration high, holdout negligible)
passed, msg = LeakageScreening.audit_split_generalization(calibration_delta=8.0, holdout_delta=0.5)
# Disproportionate overfit (dev high, selection gain gap > max_gap)
passed, msg = LeakageScreening.compare_dev_vs_selection_gain(
dev_gain=0.080, selection_gain=0.005, selection_ci_lower=0.001, max_gap=0.05
)
assert not passed
assert "LEAKAGE/OVERFITTING DETECTED" in msg
assert "Dev-selection gap" in msg
def test_pruning_analyzer_identifies_regex_redundancy():
patterns = [r"INV-[0-9]+", r".*invoice.*", r"INV-[0-9]+"]
recommendations = PruningAnalyzer.analyze_regex_redundancy(patterns)
assert len(recommendations) > 0
assert any("PRUNING OPPORTUNITY" in r for r in recommendations)
def test_pruning_analyzer_identifies_rule_ablation_candidates():
rule_counts = {"rule_legacy_tax": 0, "rule_modern_gst": 100}
# Below corpus size -> insufficient evidence
res_insufficient = PruningAnalyzer.audit_rule_ablation_candidates(
rule_counts, corpus_doc_count=10, corpus_template_count=2
)
assert res_insufficient["status"] == "INSUFFICIENT_EVIDENCE"
# Sufficient corpus -> identifies zero-hit candidates
res_sufficient = PruningAnalyzer.audit_rule_ablation_candidates(
rule_counts, corpus_doc_count=50, corpus_template_count=15
)
assert res_sufficient["status"] == "CANDIDATES_IDENTIFIED"
assert "rule_legacy_tax" in res_sufficient["candidates"]
# -----------------------------------------------------------------------------