"""Conservative field-specific accept/retry/abstain policy.""" from .contracts import RankedCandidate, SemanticClass from .reliability import AGREEMENT_CLASSES, CHECKSUM_CLASSES, MIN_RELIABILITY BEAM_MARGIN_FLOOR = 0.05 RECOVERY_MARK = "nid_recovery" # Character floors stay as a second screen after the reliability bound. # They are not calibration. THRESHOLDS: dict[SemanticClass, float] = { "nid": 97.0, "pan": 97.0, "registration_number": 94.0, "tax_id": 94.0, "account_number": 94.0, "date": 92.0, "amount": 92.0, "percentage": 90.0, "generic_numeric": 92.0, } def decide( candidate: RankedCandidate | None, semantic_class: SemanticClass, *, agreement: bool = False, reliability_lower_bound: float = 0.0, ) -> str: if candidate is None or not candidate.schema_valid or candidate.checksum_valid is False: return "abstain" if candidate.beam_margin < BEAM_MARGIN_FLOOR: return "retry" if RECOVERY_MARK in candidate.preprocessing_variant: return "retry" if reliability_lower_bound < MIN_RELIABILITY: return "retry" if candidate.min_character_confidence < THRESHOLDS[semantic_class]: return "retry" if semantic_class in CHECKSUM_CLASSES: return "accepted" if candidate.checksum_valid is True else "retry" if semantic_class in AGREEMENT_CLASSES: return "accepted" if agreement else "retry" return "retry"