On master: wip
This commit is contained in:
+86
-17
@@ -341,6 +341,24 @@ class StatisticalEvaluator:
|
||||
|
||||
return metrics, failures
|
||||
|
||||
@staticmethod
|
||||
def _load_selection_config(config_path: Optional[Path] = None) -> Dict[str, Any]:
|
||||
"""Loads gate, cost, and macro weight configurations from config/selection.yaml."""
|
||||
import yaml
|
||||
paths = [
|
||||
config_path,
|
||||
Path("config/selection.yaml"),
|
||||
Path("data_private/config/selection.yaml")
|
||||
]
|
||||
for p in paths:
|
||||
if p and Path(p).exists():
|
||||
try:
|
||||
with open(p, "r", encoding="utf-8") as f:
|
||||
return yaml.safe_load(f) or {}
|
||||
except Exception:
|
||||
pass
|
||||
return {}
|
||||
|
||||
def decide_acceptance(
|
||||
self,
|
||||
hypothesis: ProposalHypothesis,
|
||||
@@ -349,15 +367,20 @@ class StatisticalEvaluator:
|
||||
baseline_selection: Optional[EvaluationMetrics] = None,
|
||||
candidate_selection: Optional[EvaluationMetrics] = None,
|
||||
template_groups: Optional[Dict[str, str]] = None,
|
||||
git_base_ref: Optional[str] = None
|
||||
git_base_ref: Optional[str] = None,
|
||||
config_path: Optional[Path] = None,
|
||||
pruning_recommendations: Optional[List[str]] = None,
|
||||
ledger_breakage_explanations: Optional[Dict[str, str]] = None
|
||||
) -> DecisionRecord:
|
||||
"""
|
||||
Selection Gate Engine (Fail-Closed):
|
||||
Selection Gate Engine (Fail-Closed, driven by config/selection.yaml):
|
||||
1. Raises ValueError if baseline_selection or candidate_selection is absent.
|
||||
2. Retrieves candidate diff via `git diff <base>..HEAD`; rejects empty diff as 'no change'.
|
||||
3. Paired Cluster Bootstrap CI on macro gain (lower bound > 0 and mean gain > 2x noise SD).
|
||||
4. Case-level regression check.
|
||||
5. Cost-aware utility penalty via cost_lambda.
|
||||
4. Per-field no-regression rule: rejects if any field accuracy regresses beyond noise.
|
||||
5. Case-level regression check: listed and requires net gain survival + ledger justification.
|
||||
6. Cost-aware utility penalty via cost_lambda and vision fallback call enforcement.
|
||||
7. Pruning recommendations wired to decision record.
|
||||
"""
|
||||
# 1. Require Selection-Split Metrics
|
||||
if baseline_selection is None or candidate_selection is None:
|
||||
@@ -365,6 +388,18 @@ class StatisticalEvaluator:
|
||||
"SELECTION GATE ERROR: Selection-split metrics are required; refusing to decide on dev set alone."
|
||||
)
|
||||
|
||||
cfg = self._load_selection_config(config_path)
|
||||
gate_cfg = cfg.get("gate", {})
|
||||
cost_cfg = cfg.get("cost", {})
|
||||
|
||||
noise_multiplier = float(gate_cfg.get("noise_multiplier", 2.0))
|
||||
min_ci_lower = float(gate_cfg.get("min_macro_gain_ci_lower", 0.0))
|
||||
allow_field_regression = bool(gate_cfg.get("allow_field_regression", False))
|
||||
max_dev_selection_gap = float(gate_cfg.get("max_dev_selection_gap", 0.05))
|
||||
|
||||
cost_lambda = float(cost_cfg.get("cost_lambda", self.cost_lambda))
|
||||
max_vision_calls = int(cost_cfg.get("max_vision_fallback_calls", 0))
|
||||
|
||||
rejection_reasons: List[str] = []
|
||||
process_outcome = ProcessOutcome.ACCEPTED
|
||||
|
||||
@@ -405,43 +440,77 @@ class StatisticalEvaluator:
|
||||
f"Baseline ({baseline_selection.macro_accuracy}%). Delta: {delta_acc:.2f}%."
|
||||
)
|
||||
process_outcome = ProcessOutcome.INSIGNIFICANT
|
||||
elif ci_lower <= 0.0:
|
||||
elif ci_lower <= min_ci_lower:
|
||||
rejection_reasons.append(
|
||||
f"NOT STATISTICALLY SIGNIFICANT: 95% paired bootstrap CI lower bound ({ci_lower:.4f}) <= 0."
|
||||
f"NOT STATISTICALLY SIGNIFICANT: 95% paired bootstrap CI lower bound ({ci_lower:.4f}) <= {min_ci_lower}."
|
||||
)
|
||||
process_outcome = ProcessOutcome.INSIGNIFICANT
|
||||
elif mean_delta <= (2.0 * noise_sd) and noise_sd > 0.0:
|
||||
elif mean_delta <= (noise_multiplier * noise_sd) and noise_sd > 0.0:
|
||||
rejection_reasons.append(
|
||||
f"BELOW 2X NOISE SD: Mean gain ({mean_delta:.4f}) <= 2x noise standard deviation ({2.0 * noise_sd:.4f})."
|
||||
f"BELOW {noise_multiplier}X NOISE SD: Mean gain ({mean_delta:.4f}) <= {noise_multiplier}x noise SD ({noise_multiplier * noise_sd:.4f})."
|
||||
)
|
||||
process_outcome = ProcessOutcome.INSIGNIFICANT
|
||||
|
||||
# 4. Case-Level Regression Tracking
|
||||
# 4. Per-Field No-Regression Check
|
||||
field_regressions: List[str] = []
|
||||
if not allow_field_regression:
|
||||
for f_name, b_fm in baseline_selection.per_field_metrics.items():
|
||||
c_fm = candidate_selection.per_field_metrics.get(f_name)
|
||||
if c_fm and c_fm.accuracy < b_fm.accuracy:
|
||||
f_drop = b_fm.accuracy - c_fm.accuracy
|
||||
field_regressions.append(f"Field '{f_name}' regressed by {f_drop:.2f}% (from {b_fm.accuracy}% to {c_fm.accuracy}%)")
|
||||
|
||||
if field_regressions:
|
||||
rejection_reasons.append(
|
||||
f"PER-FIELD REGRESSION DETECTED: {len(field_regressions)} field(s) regressed: {'; '.join(field_regressions[:3])}."
|
||||
)
|
||||
process_outcome = ProcessOutcome.REGRESSION_DETECTED
|
||||
|
||||
# 5. Case-Level Regression Tracking
|
||||
case_regressions: List[str] = []
|
||||
unexplained_case_regressions: List[str] = []
|
||||
ledger_explanations = ledger_breakage_explanations or {}
|
||||
|
||||
for doc_id, b_doc in baseline_selection.doc_results.items():
|
||||
c_doc = candidate_selection.doc_results.get(doc_id)
|
||||
if c_doc:
|
||||
for f_name, b_val in b_doc.field_outcomes.items():
|
||||
c_val = c_doc.field_outcomes.get(f_name, False)
|
||||
if b_val and not c_val:
|
||||
case_key = f"{doc_id}:{f_name}"
|
||||
case_regressions.append(f"Doc '{doc_id}' field '{f_name}' regressed from True to False")
|
||||
if case_key not in ledger_explanations and doc_id not in ledger_explanations:
|
||||
unexplained_case_regressions.append(case_key)
|
||||
|
||||
if case_regressions:
|
||||
rejection_reasons.append(
|
||||
f"CASE REGRESSIONS DETECTED: {len(case_regressions)} previously correct fields broken in candidate."
|
||||
)
|
||||
process_outcome = ProcessOutcome.REGRESSION_DETECTED
|
||||
if unexplained_case_regressions:
|
||||
rejection_reasons.append(
|
||||
f"CASE REGRESSIONS DETECTED: {len(case_regressions)} previously correct fields broken without ledger explanation "
|
||||
f"(Unexplained: {', '.join(unexplained_case_regressions[:3])})."
|
||||
)
|
||||
process_outcome = ProcessOutcome.REGRESSION_DETECTED
|
||||
elif ci_lower <= 0.0:
|
||||
rejection_reasons.append(
|
||||
f"CASE REGRESSIONS DETECTED: Breakage was explained in ledger, but net gain did not survive paired CI test (CI low: {ci_lower:.4f})."
|
||||
)
|
||||
process_outcome = ProcessOutcome.REGRESSION_DETECTED
|
||||
|
||||
# 5. Cost-Aware Acceptance
|
||||
# 6. Cost-Aware Acceptance & Vision Fallback Enforcement
|
||||
latency_delta = round(candidate_selection.runtime_median_seconds - baseline_selection.runtime_median_seconds, 2)
|
||||
cost_penalty = self.cost_lambda * max(0.0, latency_delta)
|
||||
cost_penalty = cost_lambda * max(0.0, latency_delta)
|
||||
net_utility = delta_acc - (cost_penalty * 100.0)
|
||||
cost_accepted = net_utility >= 0.0
|
||||
|
||||
if not cost_accepted:
|
||||
rejection_reasons.append(
|
||||
f"COST UTILITY NEGATIVE: Net utility ({net_utility:.2f}%) < 0 after latency penalty "
|
||||
f"(+{latency_delta}s @ lambda={self.cost_lambda})."
|
||||
f"(+{latency_delta}s @ lambda={cost_lambda})."
|
||||
)
|
||||
process_outcome = ProcessOutcome.COST_REJECTED
|
||||
|
||||
if candidate_selection.vision_fallback_calls > max_vision_calls:
|
||||
rejection_reasons.append(
|
||||
f"EXCESSIVE VISION FALLBACK CALLS: {candidate_selection.vision_fallback_calls} calls exceed configured limit ({max_vision_calls})."
|
||||
)
|
||||
process_outcome = ProcessOutcome.COST_REJECTED
|
||||
|
||||
@@ -509,7 +578,7 @@ class StatisticalEvaluator:
|
||||
vision_fallback_calls=candidate_selection.vision_fallback_calls,
|
||||
case_regressions=case_regressions,
|
||||
rejection_reasons=rejection_reasons,
|
||||
pruning_recommendations=[],
|
||||
pruning_recommendations=pruning_recommendations or [],
|
||||
failure_deltas=failure_deltas,
|
||||
provenance=hypothesis.provenance,
|
||||
eval_run_id=str(results_file.as_posix()),
|
||||
|
||||
+454
-106
@@ -1,16 +1,229 @@
|
||||
# app/rrsi/screening.py
|
||||
"""
|
||||
Selection-Side Verification: Leakage Screening & Pruning Analyzer.
|
||||
1. Leakage Screening: Audits proposed code changes to detect test-data memorization,
|
||||
hardcoded coordinates, specific vendor strings, or fragile heuristics.
|
||||
2. Pruning Analyzer: Identifies dead, redundant, or superseded heuristics in the OCR pipeline.
|
||||
Implements:
|
||||
- B1: Forbidden-set runtime store using salted hashes + char-8-gram hashes.
|
||||
No real PII / tokens in source.
|
||||
- B2: AST scan of added lines of candidate diff (forbidden literals,
|
||||
concatenations, f-strings, coordinate comparisons including box[1] > 840,
|
||||
literal-int slices, vendor-conditional branches >= 6 chars). Whitelists
|
||||
kernel sizes and color constants via config/selection.yaml.
|
||||
- B4: Paired-bootstrap comparison of dev gain vs selection gain.
|
||||
- B5: Rule hit counter analysis requiring minimum corpus (>=40 docs, >=10 templates)
|
||||
to identify ablation candidates, and ablation acceptance evaluation.
|
||||
"""
|
||||
|
||||
import ast
|
||||
import re
|
||||
import json
|
||||
import hashlib
|
||||
from pathlib import Path
|
||||
from typing import Dict, Any, List, Tuple
|
||||
from typing import Dict, Any, List, Tuple, Set, Optional
|
||||
import yaml
|
||||
|
||||
|
||||
class LeakageASTVisitor(ast.NodeVisitor):
|
||||
"""
|
||||
AST Visitor to scan added lines of code for data leakage,
|
||||
coordinate hardcoding, and fragile vendor-conditional gating.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
salt: str = "",
|
||||
forbidden_hashes: Optional[Set[str]] = None,
|
||||
forbidden_ngrams: Optional[Set[str]] = None,
|
||||
whitelist_numbers: Optional[Set[float]] = None
|
||||
):
|
||||
self.salt = salt
|
||||
self.forbidden_hashes = forbidden_hashes or set()
|
||||
self.forbidden_ngrams = forbidden_ngrams or set()
|
||||
self.whitelist_numbers = whitelist_numbers or {0, 1, 3, 5, 7, 255, 1000}
|
||||
self.findings: List[str] = []
|
||||
|
||||
def _normalize_token(self, val: str) -> str:
|
||||
return re.sub(r"[^a-zA-Z0-9]", "", str(val)).lower()
|
||||
|
||||
def _check_sensitive_string(self, text: str, node: ast.AST):
|
||||
if not self.salt or not (self.forbidden_hashes or self.forbidden_ngrams):
|
||||
return
|
||||
|
||||
norm = self._normalize_token(text)
|
||||
if not norm or len(norm) < 3:
|
||||
return
|
||||
|
||||
candidates = [norm]
|
||||
# If numeric, also check integer representation and 2-decimal representation
|
||||
try:
|
||||
f_val = float(text)
|
||||
candidates.append(str(int(f_val)))
|
||||
candidates.append(f"{f_val:.2f}".replace(".", ""))
|
||||
except (ValueError, TypeError):
|
||||
pass
|
||||
|
||||
for cand in candidates:
|
||||
# 1. Exact salted hash check
|
||||
h_exact = hashlib.sha256((self.salt + cand).encode("utf-8")).hexdigest()
|
||||
if h_exact in self.forbidden_hashes:
|
||||
self.findings.append(
|
||||
f"DATA LEAKAGE DETECTED: Literal matches forbidden entity hash: '{text}' (Line {getattr(node, 'lineno', '?')})"
|
||||
)
|
||||
return
|
||||
|
||||
# 2. Salted char-8-gram check
|
||||
if len(cand) >= 8:
|
||||
for i in range(len(cand) - 7):
|
||||
ngram = cand[i:i+8]
|
||||
h_ngram = hashlib.sha256((self.salt + ngram).encode("utf-8")).hexdigest()
|
||||
if h_ngram in self.forbidden_ngrams:
|
||||
self.findings.append(
|
||||
f"DATA LEAKAGE DETECTED: Literal substring matches forbidden 8-gram: '{ngram}' (Line {getattr(node, 'lineno', '?')})"
|
||||
)
|
||||
return
|
||||
|
||||
def visit_Constant(self, node: ast.Constant):
|
||||
if isinstance(node.value, str):
|
||||
self._check_sensitive_string(node.value, node)
|
||||
elif isinstance(node.value, (int, float)):
|
||||
# Check numbers if represented as string in ground truth
|
||||
self._check_sensitive_string(str(node.value), node)
|
||||
self.generic_visit(node)
|
||||
|
||||
def visit_BinOp(self, node: ast.BinOp):
|
||||
"""Detect constant-folded string concatenations like 'WMP/B/' + '04531'"""
|
||||
if isinstance(node.op, ast.Add):
|
||||
try:
|
||||
# If both are string constants, evaluate combined string
|
||||
if isinstance(node.left, ast.Constant) and isinstance(node.left.value, str) and \
|
||||
isinstance(node.right, ast.Constant) and isinstance(node.right.value, str):
|
||||
combined = node.left.value + node.right.value
|
||||
self._check_sensitive_string(combined, node)
|
||||
except Exception:
|
||||
pass
|
||||
self.generic_visit(node)
|
||||
|
||||
def visit_JoinedStr(self, node: ast.JoinedStr):
|
||||
"""Detect f-strings containing concatenated sensitive parts"""
|
||||
try:
|
||||
parts = []
|
||||
for val in node.values:
|
||||
if isinstance(val, ast.Constant) and isinstance(val.value, str):
|
||||
parts.append(val.value)
|
||||
if parts:
|
||||
combined = "".join(parts)
|
||||
self._check_sensitive_string(combined, node)
|
||||
except Exception:
|
||||
pass
|
||||
self.generic_visit(node)
|
||||
|
||||
def visit_Call(self, node: ast.Call):
|
||||
"""Check regex and function calls for sensitive arguments or benign patterns"""
|
||||
func_name = ""
|
||||
if isinstance(node.func, ast.Name):
|
||||
func_name = node.func.id
|
||||
elif isinstance(node.func, ast.Attribute):
|
||||
func_name = node.func.attr
|
||||
|
||||
# Check regex calls
|
||||
if func_name in ("compile", "search", "match", "findall", "sub"):
|
||||
for arg in node.args:
|
||||
if isinstance(arg, ast.Constant) and isinstance(arg.value, str):
|
||||
self._check_sensitive_string(arg.value, node)
|
||||
|
||||
# Allow benign calls like cv2.resize(..., (1000, 1000))
|
||||
if func_name == "resize":
|
||||
return # skip inspecting arguments of cv2.resize
|
||||
|
||||
self.generic_visit(node)
|
||||
|
||||
def _is_coordinate_node(self, node: ast.AST) -> bool:
|
||||
"""Determines if an AST node represents a coordinate or bounding box attribute/subscript."""
|
||||
# 1. Attribute access: box.ymin, item.x, bbox.top, line.bottom, etc.
|
||||
if isinstance(node, ast.Attribute):
|
||||
coord_attrs = {"x", "y", "top", "left", "bottom", "right", "ymin", "ymax", "xmin", "xmax", "bbox", "box"}
|
||||
if node.attr.lower() in coord_attrs:
|
||||
return True
|
||||
|
||||
# 2. Simple Name: ymin, xmax, top, left, x, y
|
||||
if isinstance(node, ast.Name):
|
||||
coord_names = {"x", "y", "top", "left", "bottom", "right", "ymin", "ymax", "xmin", "xmax"}
|
||||
if node.id.lower() in coord_names:
|
||||
return True
|
||||
|
||||
# 3. Subscript: box[1], bbox[0], box[i], point[1]
|
||||
if isinstance(node, ast.Subscript):
|
||||
val = node.value
|
||||
if isinstance(val, ast.Name) and val.id.lower() in ("box", "bbox", "pt", "point", "coords", "points"):
|
||||
return True
|
||||
if isinstance(val, ast.Attribute) and val.attr.lower() in ("box", "bbox", "pt", "point", "coords"):
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def visit_Compare(self, node: ast.Compare):
|
||||
"""
|
||||
Flag:
|
||||
1. Coordinate hardcoding: x/y/top/left/box[i]/bbox compared with >=50 (including box[1] > 840)
|
||||
2. Vendor-conditional branches: comparisons, in, startswith against string literals >=6 chars
|
||||
"""
|
||||
left = node.left
|
||||
|
||||
# Check for coordinate hardcoding
|
||||
is_left_coord = self._is_coordinate_node(left)
|
||||
for comparator in node.comparators:
|
||||
is_right_coord = self._is_coordinate_node(comparator)
|
||||
|
||||
# Left is coord, right is number
|
||||
if is_left_coord and isinstance(comparator, ast.Constant) and isinstance(comparator.value, (int, float)):
|
||||
val = float(comparator.value)
|
||||
if val >= 50.0 and val not in self.whitelist_numbers:
|
||||
self.findings.append(
|
||||
f"COORDINATE OVERFITTING DETECTED: Coordinate comparison with literal {comparator.value} "
|
||||
f"(Line {getattr(node, 'lineno', '?')}). Heuristics must use relative ratios."
|
||||
)
|
||||
|
||||
# Right is coord, left is number
|
||||
elif is_right_coord and isinstance(left, ast.Constant) and isinstance(left.value, (int, float)):
|
||||
val = float(left.value)
|
||||
if val >= 50.0 and val not in self.whitelist_numbers:
|
||||
self.findings.append(
|
||||
f"COORDINATE OVERFITTING DETECTED: Coordinate comparison with literal {left.value} "
|
||||
f"(Line {getattr(node, 'lineno', '?')}). Heuristics must use relative ratios."
|
||||
)
|
||||
|
||||
# Check vendor-conditional gating: string literal >= 6 chars compared against variable
|
||||
for op in node.ops:
|
||||
if isinstance(op, (ast.Eq, ast.NotEq, ast.In, ast.NotIn)):
|
||||
# Either operand is a string literal >= 6 chars
|
||||
literal_str = None
|
||||
if isinstance(comparator, ast.Constant) and isinstance(comparator.value, str):
|
||||
literal_str = comparator.value
|
||||
elif isinstance(left, ast.Constant) and isinstance(left.value, str):
|
||||
literal_str = left.value
|
||||
|
||||
if literal_str and len(literal_str) >= 6:
|
||||
norm = self._normalize_token(literal_str)
|
||||
if norm and not literal_str.isupper() and not literal_str.startswith("GST"):
|
||||
# Check if not a common standard keyword (like INVOICE, TOTAL, TAX)
|
||||
common_keywords = {"invoice", "taxable", "subtotal", "balance", "consignee", "discount"}
|
||||
if norm not in common_keywords:
|
||||
self.findings.append(
|
||||
f"VENDOR-CONDITIONAL BRANCH DETECTED: Branch comparison against specific string '{literal_str}' "
|
||||
f"(Line {getattr(node, 'lineno', '?')}). Logic must be generalized across templates."
|
||||
)
|
||||
|
||||
self.generic_visit(node)
|
||||
|
||||
def visit_Slice(self, node: ast.Slice):
|
||||
"""Flag literal-int slices >= 50, e.g. crop[:850]"""
|
||||
for bound in (node.lower, node.upper):
|
||||
if isinstance(bound, ast.Constant) and isinstance(bound.value, int):
|
||||
if bound.value >= 50 and bound.value not in self.whitelist_numbers:
|
||||
self.findings.append(
|
||||
f"COORDINATE OVERFITTING DETECTED: Hardcoded pixel slice index [{bound.value}] "
|
||||
f"(Line {getattr(node, 'lineno', '?')})."
|
||||
)
|
||||
self.generic_visit(node)
|
||||
|
||||
|
||||
class LeakageScreening:
|
||||
@@ -19,139 +232,274 @@ class LeakageScreening:
|
||||
rather than memorizing the test dataset.
|
||||
"""
|
||||
|
||||
# Known sample-specific test tokens (invoice numbers / synthetic tokens)
|
||||
SUSPICIOUS_SAMPLE_TOKENS = {
|
||||
"wmp/b/04531", "bms/26-27/196", "cc-1415", "170588", "c/3930"
|
||||
}
|
||||
|
||||
GSTIN_REGEX = re.compile(r"\b\d{2}[A-Z]{5}\d{4}[A-Z][1-9A-Z]Z[0-9A-Z]\b")
|
||||
PHONE_REGEX = re.compile(r"\b(?:\+91[\-\s]?)?[6-9]\d{9}\b|\b0\d{2,4}[\-\s]?\d{6,8}\b")
|
||||
SYNTHETIC_WHITELIST = {
|
||||
"27AAPFU0939F1ZV", "27AAPFU0939F1ZW", "AAPFU0939F", "020-00000000", "9999999999"
|
||||
}
|
||||
|
||||
# Patterns indicating coordinate hardcoding (e.g. ymin == 145, box[1] > 840)
|
||||
HARDCODED_COORDINATE_PATTERNS = [
|
||||
r"\b(?:box|bbox|ymin|ymax|xmin|xmax)\s*(?:==|>=|<=|>|<)\s*\d{3,4}\b",
|
||||
r"\[\s*\d{3,4}\s*,\s*\d{3,4}\s*,\s*\d{3,4}\s*,\s*\d{3,4}\s*\]",
|
||||
r"\b(?:crop|slice)\s*\[\s*\d{3,4}\s*:\s*\d{3,4}\s*\]"
|
||||
]
|
||||
@classmethod
|
||||
def _load_config_whitelist(cls, config_path: Optional[Path] = None) -> Set[float]:
|
||||
paths = [
|
||||
config_path,
|
||||
Path("config/selection.yaml"),
|
||||
Path("data_private/config/selection.yaml")
|
||||
]
|
||||
for p in paths:
|
||||
if p and p.exists():
|
||||
try:
|
||||
with open(p, "r", encoding="utf-8") as f:
|
||||
data = yaml.safe_load(f)
|
||||
nums = data.get("whitelist", {}).get("allowed_numeric_literals", [])
|
||||
return {float(n) for n in nums}
|
||||
except Exception:
|
||||
pass
|
||||
return {0.0, 1.0, 3.0, 5.0, 7.0, 255.0, 1000.0}
|
||||
|
||||
@classmethod
|
||||
def _load_forbidden_hashes(cls) -> Tuple[str, set]:
|
||||
def _load_forbidden_hashes(cls) -> Tuple[str, Set[str], Set[str]]:
|
||||
for p in [Path("data_private/config/forbidden_hashes.json"), Path("config/forbidden_hashes.json")]:
|
||||
if p.exists():
|
||||
try:
|
||||
with open(p, "r", encoding="utf-8") as f:
|
||||
data = json.load(f)
|
||||
return data.get("salt", ""), set(data.get("hashes", []))
|
||||
salt = data.get("salt", "")
|
||||
hashes = set(data.get("hashes", []))
|
||||
ngrams = set(data.get("ngrams", []))
|
||||
return salt, hashes, ngrams
|
||||
except Exception:
|
||||
pass
|
||||
return "", set()
|
||||
return "", set(), set()
|
||||
|
||||
@classmethod
|
||||
def audit_code_changes(cls, code_or_diff: str) -> Tuple[bool, List[str]]:
|
||||
"""
|
||||
Scans code diff or candidate implementation for leakage and memorization.
|
||||
Returns:
|
||||
(passed: bool, findings: List[str])
|
||||
"""
|
||||
findings = []
|
||||
lower_code = code_or_diff.lower()
|
||||
|
||||
# 1. Check for specific test tokens / sample memorization
|
||||
for token in cls.SUSPICIOUS_SAMPLE_TOKENS:
|
||||
if re.search(r"['\"]" + re.escape(token) + r"['\"]", lower_code):
|
||||
findings.append(
|
||||
f"DATA LEAKAGE DETECTED: Code contains hardcoded test token '{token}'. "
|
||||
f"Production heuristics must be generalized, not tailored to specific sample entities."
|
||||
)
|
||||
|
||||
# 1b. Check for literal GSTINs and Indian phone numbers in code
|
||||
for match in cls.GSTIN_REGEX.findall(code_or_diff):
|
||||
if match not in cls.SYNTHETIC_WHITELIST:
|
||||
findings.append(
|
||||
f"DATA LEAKAGE DETECTED: Code contains hardcoded GSTIN '{match}'."
|
||||
)
|
||||
|
||||
for match in cls.PHONE_REGEX.findall(code_or_diff):
|
||||
if match not in cls.SYNTHETIC_WHITELIST:
|
||||
findings.append(
|
||||
f"DATA LEAKAGE DETECTED: Code contains hardcoded phone number '{match}'."
|
||||
)
|
||||
|
||||
# 1c. Check for salted forbidden entity names
|
||||
salt, forbidden_hashes = cls._load_forbidden_hashes()
|
||||
if salt and forbidden_hashes:
|
||||
tokens = [t.lower().strip(" \t\r\n'\",;:.()[]{}") for t in code_or_diff.split() if len(t.strip()) >= 4]
|
||||
for token in tokens:
|
||||
thash = hashlib.sha256((salt + token).encode("utf-8")).hexdigest()
|
||||
if thash in forbidden_hashes:
|
||||
findings.append("DATA LEAKAGE DETECTED: Code contains sensitive entity name matching forbidden hash.")
|
||||
break
|
||||
|
||||
# 2. Check for absolute coordinate hardcoding
|
||||
for pattern in cls.HARDCODED_COORDINATE_PATTERNS:
|
||||
matches = re.findall(pattern, code_or_diff)
|
||||
if matches:
|
||||
findings.append(
|
||||
f"COORDINATE OVERFITTING WARNING: Found hardcoded pixel coordinates: {matches[:3]}. "
|
||||
f"Heuristics must use relative ratios (0.0 to 1.0) or dynamic spatial clustering."
|
||||
)
|
||||
|
||||
passed = len(findings) == 0
|
||||
return passed, findings
|
||||
|
||||
@classmethod
|
||||
def audit_split_generalization(
|
||||
def build_runtime_forbidden_store(
|
||||
cls,
|
||||
calibration_delta: float,
|
||||
holdout_delta: float,
|
||||
min_holdout_retention: float = 0.5
|
||||
ground_truths: List[Dict[str, Any]],
|
||||
salt: str = "RRSI_SALT_SECURE_2026_"
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
B1: Builds the forbidden set at runtime from selection and locked-test ground truth.
|
||||
Normalizes: casefold, alphanumeric only.
|
||||
Stores only salted hashes and salted char-8-gram hashes.
|
||||
No real vendor, GSTIN, phone, or invoice value is stored in plaintext.
|
||||
"""
|
||||
salted_hashes = set()
|
||||
char_8grams = set()
|
||||
|
||||
def add_token(val: Any):
|
||||
if val is None:
|
||||
return
|
||||
s_val = str(val).strip()
|
||||
norm = re.sub(r"[^a-zA-Z0-9]", "", s_val).lower()
|
||||
if not norm or len(norm) < 3:
|
||||
return
|
||||
|
||||
# Full token hash
|
||||
h = hashlib.sha256((salt + norm).encode("utf-8")).hexdigest()
|
||||
salted_hashes.add(h)
|
||||
|
||||
# Char-8-grams
|
||||
if len(norm) >= 8:
|
||||
for i in range(len(norm) - 7):
|
||||
ng = norm[i:i+8]
|
||||
h_ng = hashlib.sha256((salt + ng).encode("utf-8")).hexdigest()
|
||||
char_8grams.add(h_ng)
|
||||
|
||||
for gt in ground_truths:
|
||||
add_token(gt.get("invoice_number") or gt.get("invoice_id"))
|
||||
add_token(gt.get("seller_name"))
|
||||
add_token(gt.get("seller_gstin"))
|
||||
add_token(gt.get("customer_name"))
|
||||
add_token(gt.get("customer_gstin"))
|
||||
add_token(gt.get("total_amount") or gt.get("invoice_amount"))
|
||||
for item in gt.get("items", []):
|
||||
add_token(item.get("description"))
|
||||
add_token(item.get("hsn"))
|
||||
add_token(item.get("total_price") or item.get("amount"))
|
||||
|
||||
return {
|
||||
"salt": salt,
|
||||
"hashes": sorted(list(salted_hashes)),
|
||||
"ngrams": sorted(list(char_8grams))
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def extract_added_lines_from_diff(cls, diff_or_code: str) -> str:
|
||||
"""Extracts only the added lines (+) from a git diff string."""
|
||||
if "diff --git" not in diff_or_code and "@@" not in diff_or_code:
|
||||
return diff_or_code
|
||||
|
||||
added_lines = []
|
||||
for line in diff_or_code.splitlines():
|
||||
if line.startswith("+") and not line.startswith("+++"):
|
||||
added_lines.append(line[1:])
|
||||
return "\n".join(added_lines)
|
||||
|
||||
@classmethod
|
||||
def audit_code_changes(
|
||||
cls,
|
||||
code_or_diff: str,
|
||||
config_path: Optional[Path] = None,
|
||||
forbidden_hashes: Optional[Set[str]] = None,
|
||||
forbidden_ngrams: Optional[Set[str]] = None,
|
||||
salt: Optional[str] = None
|
||||
) -> Tuple[bool, List[str]]:
|
||||
"""
|
||||
B2: AST scan of ONLY the added lines of the candidate diff.
|
||||
Flags:
|
||||
- String and numeric literals matching forbidden hashes or n-grams.
|
||||
- Concatenations ('WMP/B/' + '04531'), f-string parts, regex compile arguments.
|
||||
- Comparisons, in, startswith against literal strings >=6 chars gating extraction.
|
||||
- Numeric literals >=50 compared against coordinates (x, y, top, left, box[i], bbox).
|
||||
- Literal-int slices.
|
||||
- Whitelists kernel sizes and color constants.
|
||||
"""
|
||||
findings: List[str] = []
|
||||
added_code = cls.extract_added_lines_from_diff(code_or_diff)
|
||||
|
||||
# 1. Regex checks for un-whitelisted GSTIN and phone numbers
|
||||
for match in cls.GSTIN_REGEX.findall(added_code):
|
||||
if match not in cls.SYNTHETIC_WHITELIST:
|
||||
findings.append(f"DATA LEAKAGE DETECTED: Hardcoded GSTIN '{match}' found in diff.")
|
||||
|
||||
for match in cls.PHONE_REGEX.findall(added_code):
|
||||
if match not in cls.SYNTHETIC_WHITELIST:
|
||||
findings.append(f"DATA LEAKAGE DETECTED: Hardcoded phone number '{match}' found in diff.")
|
||||
|
||||
# 2. Load config whitelist & forbidden hashes
|
||||
whitelist_numbers = cls._load_config_whitelist(config_path)
|
||||
if salt is None or forbidden_hashes is None:
|
||||
loaded_salt, loaded_hashes, loaded_ngrams = cls._load_forbidden_hashes()
|
||||
salt = salt or loaded_salt
|
||||
forbidden_hashes = forbidden_hashes or loaded_hashes
|
||||
forbidden_ngrams = forbidden_ngrams or loaded_ngrams
|
||||
|
||||
# 3. Parse AST of added lines
|
||||
tree = None
|
||||
# Try parsing directly
|
||||
try:
|
||||
tree = ast.parse(added_code)
|
||||
except SyntaxError:
|
||||
# If diff contains indented lines or partial statements, wrap in function body
|
||||
try:
|
||||
indented = "\n".join(f" {line}" for line in added_code.splitlines())
|
||||
tree = ast.parse(f"def _temp_diff_func():\n{indented}")
|
||||
except SyntaxError:
|
||||
# If still syntax error, line-by-line fallback
|
||||
pass
|
||||
|
||||
if tree is not None:
|
||||
visitor = LeakageASTVisitor(
|
||||
salt=salt or "",
|
||||
forbidden_hashes=forbidden_hashes or set(),
|
||||
forbidden_ngrams=forbidden_ngrams or set(),
|
||||
whitelist_numbers=whitelist_numbers
|
||||
)
|
||||
visitor.visit(tree)
|
||||
findings.extend(visitor.findings)
|
||||
else:
|
||||
# Regex fallback if AST completely fails
|
||||
box_pat = re.compile(r"\b(?:box|bbox)\[\d+\]\s*(?:==|>=|<=|>|<)\s*(\d+)\b")
|
||||
for m in box_pat.finditer(added_code):
|
||||
val = float(m.group(1))
|
||||
if val >= 50.0 and val not in whitelist_numbers:
|
||||
findings.append(f"COORDINATE OVERFITTING DETECTED: Hardcoded coordinate comparison '{m.group(0)}'.")
|
||||
|
||||
# Deduplicate findings while preserving order
|
||||
unique_findings = []
|
||||
for f in findings:
|
||||
if f not in unique_findings:
|
||||
unique_findings.append(f)
|
||||
|
||||
passed = (len(unique_findings) == 0)
|
||||
return passed, unique_findings
|
||||
|
||||
@classmethod
|
||||
def compare_dev_vs_selection_gain(
|
||||
cls,
|
||||
dev_gain: float,
|
||||
selection_gain: float,
|
||||
selection_ci_lower: float,
|
||||
max_gap: float = 0.05
|
||||
) -> Tuple[bool, str]:
|
||||
"""
|
||||
Validates that performance gains on the calibration/training set generalize to the holdout set.
|
||||
Rejects candidates that overfit to calibration data while regressing or stagnating on holdout data.
|
||||
B4: Replaces audit_split_generalization with paired-bootstrap comparison
|
||||
of dev gain vs selection gain.
|
||||
Rejects when:
|
||||
- Selection CI lower bound <= 0
|
||||
- Dev-selection gap widens beyond max_gap limit.
|
||||
Does NOT reject on a point estimate below 0 that is inside the CI.
|
||||
"""
|
||||
if holdout_delta < 0.0:
|
||||
return False, f"GENERALIZATION FAILURE: Candidate regressed on holdout set by {holdout_delta:.2f}% despite gain on calibration set ({calibration_delta:.2f}%)."
|
||||
|
||||
if calibration_delta > 2.0 and holdout_delta < (calibration_delta * min_holdout_retention):
|
||||
if selection_ci_lower <= 0.0:
|
||||
return False, (
|
||||
f"LEAKAGE/OVERFITTING DETECTED: Disproportionate calibration gain (+{calibration_delta:.2f}%) "
|
||||
f"vs holdout retention (+{holdout_delta:.2f}%). Fails the {min_holdout_retention*100:.0f}% generalization retention threshold."
|
||||
f"GENERALIZATION REJECTED: Selection 95% CI lower bound ({selection_ci_lower:.4f}) <= 0. "
|
||||
f"Candidate did not demonstrate statistically significant improvement on selection split."
|
||||
)
|
||||
|
||||
return True, "Split generalization verified."
|
||||
gap = dev_gain - selection_gain
|
||||
if gap > max_gap:
|
||||
return False, (
|
||||
f"GENERALIZATION REJECTED: Dev-selection gap ({gap:.4f}) exceeds allowable limit ({max_gap:.4f}). "
|
||||
f"Dev gain (+{dev_gain:.4f}) disproportionate to selection gain (+{selection_gain:.4f})."
|
||||
)
|
||||
|
||||
return True, f"Split generalization verified (Dev: +{dev_gain:.4f}, Selection: +{selection_gain:.4f}, CI low: {selection_ci_lower:.4f})."
|
||||
|
||||
|
||||
class PruningAnalyzer:
|
||||
"""
|
||||
Identifies redundant, dead, or superseded heuristics in the OCR pipeline.
|
||||
Recommends removal of clutter to keep pipeline lean, maintainable, and fast.
|
||||
B5: Instruments rules, regexes, and keywords with hit counters.
|
||||
Uses zero-hit rules only as ablation CANDIDATES.
|
||||
Requires minimum corpus (>=40 docs, >=10 templates); below it returns INSUFFICIENT_EVIDENCE.
|
||||
Removal requires an ablation showing a within-noise score change and a cost drop.
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def analyze_regex_redundancy(patterns: List[str]) -> List[str]:
|
||||
"""Detects whether any regex pattern is completely shadowed or duplicated by another."""
|
||||
recommendations = []
|
||||
cleaned = [p.strip() for p in patterns if p.strip()]
|
||||
def audit_rule_ablation_candidates(
|
||||
rule_hit_counts: Dict[str, int],
|
||||
corpus_doc_count: int,
|
||||
corpus_template_count: int,
|
||||
min_docs: int = 40,
|
||||
min_templates: int = 10
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Audits rule hit counters across the corpus.
|
||||
Returns INSUFFICIENT_EVIDENCE if corpus size is below minimums.
|
||||
"""
|
||||
if corpus_doc_count < min_docs or corpus_template_count < min_templates:
|
||||
return {
|
||||
"status": "INSUFFICIENT_EVIDENCE",
|
||||
"message": (
|
||||
f"Corpus insufficient ({corpus_doc_count}/{min_docs} docs, "
|
||||
f"{corpus_template_count}/{min_templates} templates). "
|
||||
f"Never propose removal below minimum evidence threshold."
|
||||
),
|
||||
"candidates": []
|
||||
}
|
||||
|
||||
for i, p1 in enumerate(cleaned):
|
||||
for j, p2 in enumerate(cleaned):
|
||||
if i != j and p1 == p2:
|
||||
recommendations.append(f"PRUNING OPPORTUNITY: Exact duplicate regex pattern '{p1}' found at index {i} and {j}.")
|
||||
elif i != j and len(p1) < len(p2) and p1 in p2 and p1.startswith(r".*") and p1.endswith(r".*"):
|
||||
recommendations.append(f"PRUNING OPPORTUNITY: Pattern '{p2}' may be shadowed by broader pattern '{p1}'.")
|
||||
|
||||
return list(set(recommendations))
|
||||
zero_hit_candidates = [rule for rule, count in rule_hit_counts.items() if count == 0]
|
||||
return {
|
||||
"status": "CANDIDATES_IDENTIFIED" if zero_hit_candidates else "NO_DEAD_RULES",
|
||||
"message": f"Identified {len(zero_hit_candidates)} zero-hit rule(s) as ablation candidates.",
|
||||
"candidates": zero_hit_candidates
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def audit_keyword_list(keywords: set, sample_occurrences: Dict[str, int]) -> List[str]:
|
||||
"""Identifies keywords that never activate across any real-world invoice samples."""
|
||||
recommendations = []
|
||||
for kw in sorted(keywords):
|
||||
count = sample_occurrences.get(kw.lower(), 0)
|
||||
if count == 0:
|
||||
recommendations.append(f"PRUNING CANDIDATE: Keyword '{kw}' has 0 activations across dataset. Consider deprecating or archiving.")
|
||||
return recommendations
|
||||
def evaluate_ablation_acceptance(
|
||||
delta_accuracy: float,
|
||||
noise_sd: float,
|
||||
cost_delta: float
|
||||
) -> Tuple[bool, str]:
|
||||
"""
|
||||
Evaluates whether an ablation experiment should be accepted.
|
||||
Criteria:
|
||||
1. Within-noise score change (|delta_accuracy| <= noise_sd)
|
||||
2. Cost or complexity drop (cost_delta < 0)
|
||||
"""
|
||||
if abs(delta_accuracy) > noise_sd and delta_accuracy < 0.0:
|
||||
return False, f"ABLATION REJECTED: Accuracy dropped by {delta_accuracy:.4f}%, exceeding noise SD ({noise_sd:.4f}%)."
|
||||
|
||||
if cost_delta >= 0.0:
|
||||
return False, f"ABLATION REJECTED: Cost or latency did not decrease (cost_delta={cost_delta:.4f})."
|
||||
|
||||
return True, "Ablation accepted: Score change is within noise floor and cost/complexity reduced."
|
||||
|
||||
Reference in New Issue
Block a user