# Optional off_target_kd_u95_um interval coherence check: require L95 <= U95 kd_off_u95_um: float | None = None if "off_target_kd_u95_um" in src and src["off_target_kd_u95_um"] is not None: kd_off_u95_um = _require_bounded_float(src["off_target_kd_u95_um"], f"{pfx}off_target_kd_u95_um", lo=0.0, hi=1e9, strict_lo=True) if kd_off_l95_um is not None and kd_off_l95_um > kd_off_u95_um: fail_reasons.append(f"{rtag}OFF_TARGET_KD_INTERVAL_INVERTED_FAIL:l95={kd_off_l95_um}>u95={kd_off_u95_um}") internal_conflict = True has_sel, sel_raw = _opt("selectivity_ratio_l95") sel_l95 = _require_bounded_float(sel_raw, f"{pfx}selectivity_ratio_l95", lo=0.0, hi=1e15, strict_lo=True) if has_sel else None if sel_l95 is not None and sel_l95 < 15000.0: fail_reasons.append(f"{rtag}SELECTIVITY_RATIO_L95_BELOW_15000X_FAIL:{sel_l95!r}") # Conservative joint selectivity ratio: (off_target_kd_l95_um * 1000.0) / on_target_kd_u95_nm >= 15000.0 (exact!) recomputed_ratio: float | None = None if kd_off_l95_nm is not None and kd_on_nm is not None and kd_on_calibrated: ratio_candidate = kd_off_l95_nm / kd_on_nm if not math.isfinite(ratio_candidate): fail_reasons.append(f"{rtag}NONFINITE_RECOMPUTED_SELECTIVITY_RATIO_FAIL") internal_conflict = True else: recomputed_ratio = ratio_candidate if recomputed_ratio < 15000.0: fail_reasons.append(f"{rtag}CONSERVATIVE_SELECTIVITY_RATIO_BELOW_15000X_FAIL:{recomputed_ratio:.6f}") if sel_l95 is not None and sel_l95 > recomputed_ratio * (1.0 + 1e-12): fail_reasons.append(f"{rtag}SELECTIVITY_RATIO_L95_INCONSISTENT_EXCEEDS_CONSERVATIVE_KD_RATIO:{sel_l95}>{recomputed_ratio:.6f}") internal_conflict = True # Q84D strict gating if regimen_id == "Q84D": if kd_on_nm is not None and kd_on_nm > 1.0: fail_reasons.append(f"{rtag}Q84D_REQUIRES_KD_ON_LE_1NM_FAIL:{kd_on_nm!r}nM") if human_pk_verified is False: fail_reasons.append(f"{rtag}Q84D_REQUIRES_MEASURED_HUMAN_PK_FAIL") internal_conflict = True has_pk, pk_raw = _opt("peak_free_nm") peak_free_nm = _require_bounded_float(pk_raw, f"{pfx}peak_free_nm", lo=0.0, hi=1e9) if has_pk else None if peak_free_nm is not None and peak_free_nm <= 0.0: fail_reasons.append(f"{rtag}ZERO_PLASMA_PEAK_EXPOSURE_REJECTED") internal_conflict = True has_ptr, ptr_raw = _opt("plasma_trough_free_nm") plasma_trough_free_nm = _require_bounded_float(ptr_raw, f"{pfx}plasma_trough_free_nm", lo=0.0, hi=1e9) if has_ptr else None if plasma_trough_free_nm is not None and plasma_trough_free_nm <= 0.0: fail_reasons.append(f"{rtag}ZERO_PLASMA_TROUGH_EXPOSURE_REJECTED") internal_conflict = True has_dln, dln_raw = _opt("dln_trough_free_nm") dln_trough_free_nm = _require_bounded_float(dln_raw, f"{pfx}dln_trough_free_nm", lo=0.0, hi=1e9) if has_dln else None if dln_trough_free_nm is not None and dln_trough_free_nm <= 0.0: fail_reasons.append(f"{rtag}ZERO_DLN_TROUGH_EXPOSURE_REJECTED") internal_conflict = True # Strict physical compartment ordering: peak_free_nm >= plasma_trough_free_nm >= dln_trough_free_nm > 0 if peak_free_nm is not None and plasma_trough_free_nm is not None and peak_free_nm < plasma_trough_free_nm: fail_reasons.append(f"{rtag}EXPOSURE_ORDERING_INCONSISTENT_PEAK_LT_PLASMA_TROUGH:{peak_free_nm}<{plasma_trough_free_nm}") internal_conflict = True if plasma_trough_free_nm is not None and dln_trough_free_nm is not None and plasma_trough_free_nm < dln_trough_free_nm: fail_reasons.append(f"{rtag}EXPOSURE_ORDERING_INCONSISTENT_PLASMA_TROUGH_LT_DLN_TROUGH:{plasma_trough_free_nm}<{dln_trough_free_nm}") internal_conflict = True if peak_free_nm is not None and dln_trough_free_nm is not None and peak_free_nm < dln_trough_free_nm: fail_reasons.append(f"{rtag}EXPOSURE_ORDERING_INCONSISTENT_PEAK_LT_DLN_TROUGH:{peak_free_nm}<{dln_trough_free_nm}") internal_conflict = True has_rep_ro, rep_ro_raw = _opt("reported_off_target_peak_ro_pct") rep_off_ro = _require_bounded_float(rep_ro_raw, f"{pfx}reported_off_target_peak_ro_pct", lo=0.0, hi=100.0) if has_rep_ro else None if rep_off_ro is not None and rep_off_ro > 5.6: fail_reasons.append(f"{rtag}REPORTED_OFF_TARGET_PEAK_RO_EXCEEDS_5P6_FAIL:{rep_off_ro!r}%") has_adcc, adcc_raw = _opt("fc_adcc_cdc_pct") adcc_pct = _require_bounded_float(adcc_raw, f"{pfx}fc_adcc_cdc_pct", lo=0.0, hi=100.0) if has_adcc else None if adcc_pct is not None: if adcc_pct > 2.0: fail_reasons.append(f"{rtag}FC_EFFECTOR_CYTOTOXICITY_FAIL:{adcc_pct}%") elif adcc_pct > 1.0: hold_reasons.append(f"{rtag}FC_SILENCING_INTERMEDIATE:{adcc_pct}%") has_tcell, tcell_raw = _opt("tcell_inhibition_l95_pct") tcell_inh = _require_bounded_float(tcell_raw, f"{pfx}tcell_inhibition_l95_pct", lo=0.0, hi=100.0) if has_tcell else None if tcell_inh is not None: if tcell_inh < 50.0: fail_reasons.append(f"{rtag}FUNCTIONAL_TCELL_INHIBITION_FAIL:{tcell_inh}%") elif tcell_inh < 70.0: hold_reasons.append(f"{rtag}FUNCTIONAL_TCELL_INHIBITION_INTERMEDIATE:{tcell_inh}%") has_apc, apc_raw = _opt("apc_cytotoxicity_u95_pct") apc_tox = _require_bounded_float(apc_raw, f"{pfx}apc_cytotoxicity_u95_pct", lo=0.0, hi=100.0) if has_apc else None if apc_tox is not None and apc_tox > 5.0: fail_reasons.append(f"{rtag}APC_CYTOTOXICITY_FAIL:{apc_tox}%") if "off_target_apc_killing_u95_pct" in src and src["off_target_apc_killing_u95_pct"] is not None: apc_alias = _require_bounded_float(src["off_target_apc_killing_u95_pct"], f"{pfx}off_target_apc_killing_u95_pct", lo=0.0, hi=100.0) if apc_alias > 5.0: fail_reasons.append(f"{rtag}OFF_TARGET_APC_KILLING_FAIL:{apc_alias}%") if apc_tox is not None and apc_alias != apc_tox: fail_reasons.append(f"{rtag}APC_CYTOTOXICITY_ALIAS_MISMATCH:{apc_alias}!={apc_tox}") internal_conflict = True has_mono, mono_raw = _opt("sec_mals_monomer_pct") mono_pct = _require_bounded_float(mono_raw, f"{pfx}sec_mals_monomer_pct", lo=0.0, hi=100.0) if has_mono else None if mono_pct is not None: if mono_pct < 90.0: fail_reasons.append(f"{rtag}SEC_MALS_MONOMER_FAIL:{mono_pct}%") elif mono_pct < 95.0: hold_reasons.append(f"{rtag}SEC_MALS_MONOMER_INTERMEDIATE:{mono_pct}%") has_het, het_raw = _opt("ce_sds_heterodimer_pct") het_pct = _require_bounded_float(het_raw, f"{pfx}ce_sds_heterodimer_pct", lo=0.0, hi=100.0) if has_het else None if het_pct is not None: if het_pct < 90.0: fail_reasons.append(f"{rtag}CE_SDS_HETERODIMER_FAIL:{het_pct}%") elif het_pct < 95.0: hold_reasons.append(f"{rtag}CE_SDS_HETERODIMER_INTERMEDIATE:{het_pct}%") # Worst-case off-target healthy-APC occupancy using LOWER 95% bound off_target_kd_l95_um pan_dq_peak_ro: float | None = None pan_dq_plasma_trough_ro: float | None = None pan_dq_dln_trough_ro: float | None = None on_target_dln_trough_ro: float | None = None # Recompute peak off-target RO & enforce two-sided reported_off_target_peak_ro_pct agreement ONLY when # peak_free_nm is actually present (Audit R12-07: NEVER substitute plasma/dLN trough exposure when peak is # omitted, so omitting peak_free_nm alone returns governed HOLD instead of a false internal-conflict FAIL!). if kd_off_l95_nm is not None and peak_free_nm is not None: concs = [c for c in (peak_free_nm, plasma_trough_free_nm, dln_trough_free_nm) if c is not None] max_conc = max(concs) pan_dq_peak_ro = 100.0 * free_ligand_ro(max_conc, kd_off_l95_nm) if pan_dq_peak_ro > 5.6: fail_reasons.append(f"{rtag}PAN_DQ_PEAK_RO_L95_EXCEEDS_5P6_FAIL:{pan_dq_peak_ro:.4f}%") if rep_off_ro is not None and abs(rep_off_ro - pan_dq_peak_ro) > 0.05: fail_reasons.append( f"{rtag}REPORTED_OFF_TARGET_RO_DEVIATES_FROM_RECOMPUTED:{rep_off_ro:.4f}%!=recomputed:{pan_dq_peak_ro:.4f}%" ) internal_conflict = True if kd_off_l95_nm is not None and plasma_trough_free_nm is not None: pan_dq_plasma_trough_ro = 100.0 * free_ligand_ro(plasma_trough_free_nm, kd_off_l95_nm) if pan_dq_plasma_trough_ro > 5.6: fail_reasons.append(f"{rtag}PAN_DQ_PLASMA_TROUGH_RO_L95_EXCEEDS_5P6_FAIL:{pan_dq_plasma_trough_ro:.4f}%") if kd_off_l95_nm is not None and dln_trough_free_nm is not None: pan_dq_dln_trough_ro = 100.0 * free_ligand_ro(dln_trough_free_nm, kd_off_l95_nm) if pan_dq_dln_trough_ro > 1.1: fail_reasons.append(f"{rtag}PAN_DQ_DLN_TROUGH_RO_L95_EXCEEDS_1P1_FAIL:{pan_dq_dln_trough_ro:.4f}%") if kd_on_nm is not None and kd_on_calibrated and dln_trough_free_nm is not None: on_target_dln_trough_ro = 100.0 * free_ligand_ro(dln_trough_free_nm, kd_on_nm) if on_target_dln_trough_ro < 90.0: fail_reasons.append(f"{rtag}ON_TARGET_DLN_TROUGH_RO_BELOW_90_FAIL:{on_target_dln_trough_ro:.4f}%") fields_map = { "study_id": study_id, "format_id": fmt_id, "dosing_regimen": regimen_id, "timed_pk_day": timed_pk_day, "human_pk_qualified": human_pk_verified, "periodic_pk_converged": periodic_pk_converged, "gmp_lot_count": gmp_lots, "donor_hla_dq_panel_n": don_n, "off_target_negative_states_tested": neg_n, "on_target_kd_u95_nm": kd_on_nm, "off_target_kd_l95_um": kd_off_l95_um, "selectivity_ratio_l95": sel_l95, "peak_free_nm": peak_free_nm, "plasma_trough_free_nm": plasma_trough_free_nm, "dln_trough_free_nm": dln_trough_free_nm, "reported_off_target_peak_ro_pct": rep_off_ro, "fc_adcc_cdc_pct": adcc_pct, "tcell_inhibition_l95_pct": tcell_inh, "apc_cytotoxicity_u95_pct": apc_tox, "sec_mals_monomer_pct": mono_pct, "ce_sds_heterodimer_pct": het_pct, } archive_complete_ok = ( len(local_missing_fields) == 0 and all(v is not None for v in fields_map.values()) and periodic_pk_converged is True ) archive_internally_consistent_ok = not internal_conflict return { "fields": fields_map, "local_missing_fields": local_missing_fields, "archive_complete_ok": archive_complete_ok, "archive_internally_consistent_ok": archive_internally_consistent_ok, "off_target_kd_u95_um": kd_off_u95_um, "kd_on_calibrated": kd_on_calibrated, "internal_conflict": internal_conflict, "pan_dq_peak_ro": pan_dq_peak_ro, "pan_dq_plasma_trough_ro": pan_dq_plasma_trough_ro, "pan_dq_dln_trough_ro": pan_dq_dln_trough_ro, "on_target_dln_trough_ro": on_target_dln_trough_ro, "recomputed_ratio": recomputed_ratio, } def evaluate_gate34_shield_sel( obs: dict[str, Any], arm_manifest: dict[str, Any] | None = None, *, trusted_evidence_store: dict[str, dict[str, Any]] | None = None, ) -> dict[str, Any]: """Fail-closed Gate 34-SHIELD-SEL evaluator (Round-12 Audit R11-04: gate ALL official stats on complete authenticated archive + every internal consistency check; keep caller reconciliation separate).""" empty_ro = { "occupancy_convention": OCCUPANCY_CONVENTION, "pan_dq_peak_ro_pct": None, "pan_dq_plasma_trough_ro_pct": None, "pan_dq_dln_trough_ro_pct": None, "dln_trough_on_target_ro_pct": None, "recomputed_selectivity_ratio": None, "shield_statistics_derivation_source": "MALFORMED_INPUT", "caller_measurements_reconciliation_status": "MALFORMED_INPUT", "caller_reported_unverified_pan_dq_peak_ro_pct": None, "caller_reported_unverified_pan_dq_plasma_trough_ro_pct": None, "caller_reported_unverified_pan_dq_dln_trough_ro_pct": None, "caller_reported_unverified_dln_trough_on_target_ro_pct": None, "caller_reported_unverified_recomputed_selectivity_ratio": None, } try: if not isinstance(obs, dict): raise ValueError("obs must be dict") check_strictly_finite(obs, "gate34_shield_sel_obs") fail_reasons: list[str] = [] missing_fields: list[str] = [] hold_reasons: list[str] = [] verified_manifest = _verify_manifest_context(arm_manifest, obs, fail_reasons, missing_fields) _, arm_entry = _check_arm_and_design_a_invariants(obs, "shield_eligible", verified_manifest, fail_reasons, missing_fields) obs_eval = _evaluate_shield_measurements( obs, is_archive=False, fail_reasons=fail_reasons, missing_fields=missing_fields, hold_reasons=hold_reasons ) cur_raw = obs.get("current_assay_evidence_record") if "current_assay_evidence_record" in obs else None if cur_raw is not None and not isinstance(cur_raw, dict): raise ValueError("current_assay_evidence_record must be dict") cur_eval: dict[str, Any] | None = None caller_matches_archive = False if isinstance(cur_raw, dict): cur_eval = _evaluate_shield_measurements( cur_raw, is_archive=True, fail_reasons=fail_reasons, missing_fields=missing_fields, hold_reasons=hold_reasons ) caller_matches_archive = (not obs_eval["internal_conflict"]) and (len(obs_eval["local_missing_fields"]) == 0) # Enforce EXACT equality (ZERO numeric tolerance!) on ALL 21 governing fields between archive & parent! for fld, obs_v in obs_eval["fields"].items(): cur_v = cur_eval["fields"].get(fld) if obs_v is None or cur_v is None: caller_matches_archive = False elif type(obs_v) is bool or type(cur_v) is bool: if obs_v is not cur_v: