fail_reasons.append("FAIL_GATE5_WETLAB_DECISION_MUST_BE_OQ012_PASS") g5_verified = False if g5_scope != "CLINICAL_LOT_RELEASE" or g5_bq is not True or g5_pa is not True: fail_reasons.append("FAIL_GATE5_WETLAB_CLINICAL_AUTHORITY_REQUIRED") g5_verified = False else: if g5_dec != "SYNTHETIC_SELF_TEST_PASS": fail_reasons.append("FAIL_GATE5_SYNTHETIC_DECISION_INVALID") g5_verified = False if g5_scope != "SYNTHETIC_SELF_TEST" or g5_bq is True or g5_pa is True: fail_reasons.append("FAIL_GATE5_SYNTHETIC_AUTHORITY_INVALID") g5_verified = False oq012_pass = g5_rec.get("oq012_clonogenic_regrowth_pass") if oq012_pass is None: hold_reasons.append("HOLD_MISSING_GATE5_OQ012_CLONOGENIC_REGROWTH_PASS") g5_verified = False elif not _is_strict_bool(oq012_pass) or oq012_pass is not True: fail_reasons.append("FAIL_GATE5_OQ012_CLONOGENIC_REGROWTH_NOT_PASSED") g5_verified = False n_g5 = g5_rec.get("tested_cells_n") k_g5 = g5_rec.get("survivor_count_k") f_g5 = g5_rec.get("clonogenic_fraction_f") eta_g5 = g5_rec.get("spike_in_detection_efficiency_eta") n_eff_g5 = g5_rec.get("effective_clonogenic_trials_n_eff") u95_g5 = g5_rec.get("u95_exact") # Reject any contradicting legacy alias fields if co-supplied for canon_k, leg_k in ( ("tested_cells_n", "cells_assayed"), ("survivor_count_k", "survivors_observed"), ("u95_exact", "clonogenic_escape_u95"), ): if leg_k in g5_rec and canon_k in g5_rec and g5_rec[leg_k] != g5_rec[canon_k]: fail_reasons.append("FAIL_GATE5_LEGACY_ALIAS_CONTRADICTION") g5_verified = False # Live Blueprint Section 7 OQ-012 Gate 5 rare-escape qualification: # - strict survivor_count_k == 0 (ANY k >= 1 -> FAIL, NEVER clinical PASS) # - explicit tested_cells_n (int >= 1), clonogenic_fraction_f in (0, 1], # spike_in_detection_efficiency_eta in [0.30, 1.0] (< 0.30 -> FAIL), # effective_clonogenic_trials_n_eff <= floor(tested_cells_n * f * eta) (> floor -> FAIL) # - omitted recovery/effective-denominator fields -> governed HOLD # - k == 0 and n_eff < 299_573_228 (incl. 299_573_227 or acute 0/100_000 screen) -> governed HOLD # - clinical PASS requires oq012_clonogenic_regrowth_pass is True, k == 0, # n_eff >= 299_573_228, and exact 75-digit Decimal U95(0, n_eff) <= 1e-8 if n_g5 is None or k_g5 is None: hold_reasons.append("HOLD_MISSING_GATE5_TESTED_CELLS_OR_SURVIVOR_COUNT") g5_verified = False elif not _is_strict_int(n_g5) or n_g5 < 1 or n_g5 > MAX_CLONOGENIC_CELLS: fail_reasons.append("FAIL_GATE5_INSUFFICIENT_OR_INVALID_CELLS_ASSAYED") g5_verified = False elif not _is_strict_int(k_g5) or k_g5 < 0 or k_g5 > n_g5: fail_reasons.append("FAIL_GATE5_INVALID_SURVIVOR_COUNT") g5_verified = False else: if k_g5 >= 1: _add_fail("FAIL_GATE5_OQ012_NONZERO_SURVIVOR_OR_U95_GT_1E8") g5_verified = False f_dec: Optional[Decimal] = None eta_dec: Optional[Decimal] = None n_eff_valid: Optional[int] = None recovery_missing = False recovery_invalid = False if f_g5 is None: recovery_missing = True else: f_dec = _to_exact_decimal(f_g5) if f_dec is None or f_dec <= Decimal(0) or f_dec > Decimal(1): _add_fail("FAIL_GATE5_OQ012_INVALID_CLONOGENIC_FRACTION_F") recovery_invalid = True g5_verified = False if eta_g5 is None: recovery_missing = True else: eta_dec = _to_exact_decimal(eta_g5) if eta_dec is None or eta_dec <= Decimal(0) or eta_dec > Decimal(1): _add_fail("FAIL_GATE5_OQ012_INVALID_DETECTION_EFFICIENCY_ETA") recovery_invalid = True g5_verified = False elif eta_dec < GATE5_OQ012_MIN_DETECTION_EFFICIENCY_ETA: _add_fail("FAIL_GATE5_OQ012_DETECTION_EFFICIENCY_ETA_LT_0P30") recovery_invalid = True g5_verified = False if n_eff_g5 is None: recovery_missing = True elif not _is_strict_int(n_eff_g5) or n_eff_g5 < 1 or n_eff_g5 > MAX_CLONOGENIC_CELLS: _add_fail("FAIL_GATE5_OQ012_INVALID_EFFECTIVE_CLONOGENIC_TRIALS") recovery_invalid = True g5_verified = False else: n_eff_valid = n_eff_g5 if recovery_missing: _add_hold("HOLD_GATE5_OQ012_INCOMPLETE_RECOVERY_DENOMINATOR") g5_verified = False if k_g5 == 0 and n_g5 < GATE5_OQ012_MIN_EFFECTIVE_TRIALS: _add_hold("HOLD_GATE5_OQ012_UNDERPOWERED_BELOW_299573228_EFFECTIVE_TRIALS") u95_raw_dec = clopper_pearson_u95_decimal(k_g5, n_g5, alpha=0.05) u95_raw_exact = clopper_pearson_u95(k_g5, n_g5, alpha=0.05) computed_u95_exact["gate5"] = u95_raw_exact if u95_g5 is None: _add_hold("HOLD_MISSING_GATE5_SUBMITTED_U95") elif not _is_finite_number(u95_g5) or float(u95_g5) <= 0.0 or float(u95_g5) > 1.0: _add_fail("FAIL_GATE5_CLONOGENIC_ESCAPE_U95_EXCEEDED") elif Decimal.from_float(float(u95_g5)) < u95_raw_dec - Decimal("1e-18"): _add_fail("FAIL_GATE5_FORGED_OR_UNDERSTATED_CLOPPER_PEARSON_U95") elif not recovery_invalid and f_dec is not None and eta_dec is not None and n_eff_valid is not None: with localcontext() as ctx: ctx.prec = 75 max_n_eff_floor = int((Decimal(n_g5) * f_dec * eta_dec).to_integral_value(rounding=ROUND_FLOOR)) if n_eff_valid > max_n_eff_floor: _add_fail("FAIL_GATE5_OQ012_EFFECTIVE_TRIALS_EXCEEDS_RECOVERY_FLOOR") g5_verified = False if k_g5 > n_eff_valid: _add_fail("FAIL_GATE5_SURVIVOR_COUNT_EXCEEDS_EFFECTIVE_TRIALS") g5_verified = False else: u95_g5_dec = clopper_pearson_u95_decimal(k_g5, n_eff_valid, alpha=0.05) u95_g5_exact = clopper_pearson_u95(k_g5, n_eff_valid, alpha=0.05) computed_u95_exact["gate5"] = u95_g5_exact if k_g5 == 0 and n_eff_valid < GATE5_OQ012_MIN_EFFECTIVE_TRIALS: _add_hold("HOLD_GATE5_OQ012_UNDERPOWERED_BELOW_299573228_EFFECTIVE_TRIALS") g5_verified = False if u95_g5 is None: _add_hold("HOLD_MISSING_GATE5_SUBMITTED_U95") elif not _is_finite_number(u95_g5) or float(u95_g5) <= 0.0 or float(u95_g5) > 1.0: _add_fail("FAIL_GATE5_CLONOGENIC_ESCAPE_U95_EXCEEDED") elif Decimal.from_float(float(u95_g5)) < u95_g5_dec - Decimal("1e-18"): _add_fail("FAIL_GATE5_FORGED_OR_UNDERSTATED_CLOPPER_PEARSON_U95") else: if u95_g5_dec > GATE5_OQ012_MAX_U95_DEC or u95_g5_exact > 1.0e-8: _add_fail("FAIL_GATE5_OQ012_NONZERO_SURVIVOR_OR_U95_GT_1E8") g5_verified = False if u95_g5 is None: _add_hold("HOLD_MISSING_GATE5_SUBMITTED_U95") g5_verified = False elif not _is_finite_number(u95_g5) or float(u95_g5) <= 0.0 or float(u95_g5) > 1.0e-8: _add_fail("FAIL_GATE5_CLONOGENIC_ESCAPE_U95_EXCEEDED") g5_verified = False elif Decimal.from_float(float(u95_g5)) < u95_g5_dec - Decimal("1e-18"): _add_fail("FAIL_GATE5_FORGED_OR_UNDERSTATED_CLOPPER_PEARSON_U95") g5_verified = False # 6. Mandatory archived current functional assays (GATE-34-FUNCTIONAL) reconciled strictly # against parent functional_assays: exact 1.0% O2 hypoxic kill + cytokine + normoxic # comparator + 3-way governed head-to-head switch benchmark (WT + D315A + iCasp8) with # registered-switch archive selection, pyroptosis/cFLIP/MS, paired drop-ins, AAV vector # homogeneity, complement LC-MS topology, secondary CDK1::HSV-TK assay on dual-switch arms, # & G34-04 autologous D1b cohort endpoints func_arch_rec, func_arch_verified = _resolve_archived_record( "current_functional_assays_record", prefix="CURRENT_FUNCTIONAL_ASSAYS_RECORD", expected_gate_source=CURRENT_FUNCTIONAL_GATE_SOURCE, allowed_decisions=frozenset({"PASS", "SYNTHETIC_SELF_TEST_PASS"}), role="supporting", wetlab_allowed_decisions=frozenset({"PASS"}), synthetic_allowed_decisions=frozenset({"SYNTHETIC_SELF_TEST_PASS"}), ) arch_fa: Optional[Dict[str, Any]] = None if func_arch_rec is not None: raw_arch_fa = func_arch_rec.get("functional_assays") if not isinstance(raw_arch_fa, dict): _add_fail("FAIL_CURRENT_FUNCTIONAL_ASSAYS_ARCHIVE_MISSING_FUNCTIONAL_ASSAYS_DICT") func_arch_verified = False else: arch_fa = raw_arch_fa h2h_archive_verified = False secondary_switch_assay_verified = False fa = payload.get("functional_assays") if fa is None: _add_hold("HOLD_MISSING_FUNCTIONAL_ASSAYS") func_arch_verified = False elif not isinstance(fa, dict): _add_fail("FAIL_INVALID_FUNCTIONAL_ASSAYS_TYPE") func_arch_verified = False # Reconcile EVERY parent functional_assays section strictly against verified archived current_functional_assays_record (Audit R4-01) governed_fa_sections: Tuple[str, ...] = ( "hypoxic_kill_and_cleavage", "pyroptosis_and_cflip_clearance", "paired_dropin_functional_assays", "aav_vector_homogeneity", "complement_expression_and_topology", "d1b_g32_04_autologous_cohort", "secondary_switch_cdk1_hsv_tk_assay", ) if isinstance(fa, dict) and isinstance(arch_fa, dict): for sec_name in governed_fa_sections: in_parent = sec_name in fa in_arch = sec_name in arch_fa if in_parent != in_arch: _add_fail(f"FAIL_CURRENT_FUNCTIONAL_ASSAYS_PARENT_VS_ARCHIVE_MISMATCH_{sec_name.upper()}") _add_fail("FAIL_CURRENT_FUNCTIONAL_ASSAYS_PARENT_VS_ARCHIVE_MISMATCH") func_arch_verified = False continue if not in_parent: continue p_sec = fa[sec_name] a_sec = arch_fa[sec_name] if sec_name == "hypoxic_kill_and_cleavage" and isinstance(p_sec, dict) and isinstance(a_sec, dict): p_cmp = {k: v for k, v in p_sec.items() if k != "head_to_head_switch_comparator"} a_cmp = {k: v for k, v in a_sec.items() if k != "head_to_head_switch_comparator"} if not _strict_json_equal(p_cmp, a_cmp): _add_fail("FAIL_CURRENT_FUNCTIONAL_ASSAYS_PARENT_VS_ARCHIVE_MISMATCH_HYPOXIC_KILL_AND_CLEAVAGE") _add_fail("FAIL_CURRENT_FUNCTIONAL_ASSAYS_PARENT_VS_ARCHIVE_MISMATCH") func_arch_verified = False if "head_to_head_switch_comparator" in a_sec and not _strict_json_equal( p_sec.get("head_to_head_switch_comparator"), a_sec.get("head_to_head_switch_comparator") ): _add_fail("FAIL_CURRENT_FUNCTIONAL_ASSAYS_PARENT_VS_ARCHIVE_MISMATCH_HYPOXIC_KILL_AND_CLEAVAGE") _add_fail("FAIL_CURRENT_FUNCTIONAL_ASSAYS_PARENT_VS_ARCHIVE_MISMATCH") func_arch_verified = False elif not _strict_json_equal(p_sec, a_sec): _add_fail(f"FAIL_CURRENT_FUNCTIONAL_ASSAYS_PARENT_VS_ARCHIVE_MISMATCH_{sec_name.upper()}") _add_fail("FAIL_CURRENT_FUNCTIONAL_ASSAYS_PARENT_VS_ARCHIVE_MISMATCH") func_arch_verified = False def _evaluate_functional_assays_sections(fa_obj: Dict[str, Any], *, from_archive: bool) -> Tuple[bool, bool]: """Evaluate governed functional assays (6a-g, excluding H2H which has its own archive). Returns (all_refs_and_sections_ok, secondary_switch_passed). """ sec_all_ok = True sec_sw_ok = False # 6a. Hypoxic kill (strictly hypoxia_o2_pct == 1.0 + cytokines + matched normoxic comparator + exact Decimal CP U95) hk = fa_obj.get("hypoxic_kill_and_cleavage") if hk is None: _add_hold("HOLD_MISSING_HYPOXIC_KILL_AND_CLEAVAGE_ASSAY") sec_all_ok = False elif not isinstance(hk, dict): _add_fail("FAIL_INVALID_HYPOXIC_KILL_ASSAY_TYPE") sec_all_ok = False else: has_ho2 = "hypoxia_o2_pct" in hk and hk["hypoxia_o2_pct"] is not None has_o2 = "oxygen_pct" in hk and hk["oxygen_pct"] is not None if not has_ho2 and not has_o2: _add_hold("HOLD_MISSING_HYPOXIA_O2_PCT") sec_all_ok = False else: for o2_key in ("hypoxia_o2_pct", "oxygen_pct"): if o2_key in hk and hk[o2_key] is not None: v_o2 = hk[o2_key] if not _is_finite_number(v_o2) or abs(float(v_o2) - 1.0) > 1.0e-9: