if not _is_valid_sha256_hex(dd_sha): holds.append(f"HOLD_GATE0V_B_LATENT_ARM_MISSING_EVIDENCE_SHA256_{p_tag}") lat_ok = False if not (_is_strict_int(dd_k) and _is_strict_int(dd_n) and dd_n >= 1 and 0 <= dd_k <= dd_n): holds.append(f"HOLD_GATE0V_B_LATENT_ARM_INVALID_DDPCR_COUNTS_{p_tag}") lat_ok = False else: if all_targets_valid_ints and dd_k != sum_target_k: holds.append(f"HOLD_GATE0V_B_LATENT_ARM_AGGREGATE_VS_TARGET_K_MISMATCH_{p_tag}") lat_ok = False if dd_k >= 1: hard_stops.append(f"FAIL_HARD_STOP_GATE0V_B_LATENT_OR_DEFECTIVE_GENOME_DETECTED_{p_tag}_K_{dd_k}") lat_ok = False lat_arm_lbl = f"LATENT_ARM_{p_tag}" if not _check_estimand_sampling_unit_basis( lat_arm.get("per_unit_counts"), k_val=dd_k, n_val=dd_n, label=lat_arm_lbl, zero_tolerance_hard_stop_token=f"FAIL_HARD_STOP_GATE0V_B_LATENT_OR_DEFECTIVE_GENOME_DETECTED_{p_tag}_K_UNIT_POS", out_unit_maps=lat_unit_maps, ): lat_ok = False elif ( sampling_unit_class == "RECONCILED_MULTI_WELL_CELL_ASSAY_WITH_UNIT_PARTITION" and lat_arm_lbl in lat_unit_maps and all(f"LATENT_{p_tag}_{t}" in lat_unit_maps for t in required_targets) ): # R13-B2: Reconcile same unit_id set and per-unit target k sums across latent arm! arm_umap = lat_unit_maps[lat_arm_lbl] tgt_umaps = [lat_unit_maps[f"LATENT_{p_tag}_{t}"] for t in required_targets] lat_cross_ok = all(set(tu.keys()) == set(arm_umap.keys()) for tu in tgt_umaps) if lat_cross_ok: for u_k, (arm_k_u, _arm_n_u) in arm_umap.items(): if sum(tu[u_k][0] for tu in tgt_umaps) != arm_k_u: lat_cross_ok = False break if not lat_cross_ok: lat_ok = False sampling_tracker["all_partitions_reconciled"] = False if sampling_tracker["partitions_reconciled"] > 0: sampling_tracker["partitions_reconciled"] -= 1 if "HOLD_SAMPLING_UNIT_SUBSET_OR_ID_CONTRADICTION" not in holds: holds.append("HOLD_SAMPLING_UNIT_SUBSET_OR_ID_CONTRADICTION") holds.append(f"HOLD_GATE0V_B_LATENT_ARM_UNIT_ID_OR_SUM_CONTRADICTION_{p_tag}") if deff is None: lat_ok = False else: dd_n_indep = _exact_floor_div_by_decimal(dd_n, deff) if dd_n_indep < 1: holds.append(f"HOLD_GATE0V_B_LATENT_ARM_UNDERPOWERED_{p_tag}") lat_ok = False elif dd_k == 0: u_lat = sound_zero_event_clopper_pearson_upper(dd_n_indep, alpha_obs_tier, min_eta_cal, prec=80)["u_thinned_upper_dec"] lat_summary["u_latent_upper_80d"] = format(u_lat, "f")[:24] if u_lat > cap_frac_persist: holds.append(f"HOLD_GATE0V_B_LATENT_ARM_UNDERPOWERED_{p_tag}") lat_ok = False if path_id == "hcmv": react_app = lat_arm.get("reactivation_challenge_applied") react_sha = lat_arm.get("reactivation_protocol_evidence_sha256") react_pfu = _to_exact_decimal(lat_arm.get("reactivation_7d_outgrowth_pfu_ml")) react_vol = _to_exact_decimal(lat_arm.get("reactivation_assay_volume_ml")) react_lod = _to_exact_decimal(lat_arm.get("reactivation_lod_pfu_ml")) if react_app is not True or not _is_valid_sha256_hex(react_sha): holds.append("HOLD_GATE0V_B_HCMV_MISSING_REACTIVATION_CHALLENGE_OR_DIGEST") lat_ok = False if react_pfu is None or react_pfu < Decimal(0) or react_vol is None or react_vol < Decimal("1.0") or react_lod is None or react_lod <= Decimal(0) or react_lod > Decimal("1.0"): holds.append("HOLD_GATE0V_B_HCMV_INVALID_REACTIVATION_OUTGROWTH_OR_LOD") lat_ok = False elif react_pfu > Decimal(0): hard_stops.append("FAIL_HARD_STOP_GATE0V_B_HCMV_LATENT_REACTIVATION_OUTGROWTH") lat_ok = False else: if lat_arm.get("cvb_5prime_deletion_primers_verified") is not True: holds.append(f"HOLD_GATE0V_B_MISSING_CVB_5PRIME_DELETION_PRIMER_VERIFICATION_{p_tag}") lat_ok = False lat_summary["ddpcr_target_panel"] = dd_panel lat_summary["ddpcr_targets"] = target_details lat_summary["passed"] = lat_ok # 5-Lineage Two-Stage True-U95-Calibrated Surveillance + Reconciled Lineage Ledger (B3 + B5) lin_data = pblock.get("day28_persistence_by_lineage") lin_results: Dict[str, Any] = {} comp_weighted_u95 = Decimal(0) path_5lin_cal_ok = True sum_lineage_assigned_n = 0 all_assigned_counts_present = True if not isinstance(lin_data, dict): holds.append(f"HOLD_GATE0V_B_MISSING_5LINEAGE_TABLE_{p_tag}") path_5lin_cal_ok = False all_pathogens_ok = False all_assigned_counts_present = False else: for lin in GRAFT_LINEAGES: entry = lin_data.get(lin) if not isinstance(entry, dict): holds.append(f"HOLD_GATE0V_B_MISSING_LINEAGE_{p_tag}_{lin.upper()}") path_5lin_cal_ok = False all_pathogens_ok = False all_assigned_counts_present = False continue n_assigned = entry.get("cells_assigned_n") n_i = entry.get("cells_scored_n") k1_i = entry.get("stage1_screen_positive_k") n2_sorted = entry.get("stage2_sorted_for_confirmation_n") k2_conf = entry.get("stage2_confirmed_infectious_k") uc_k = entry.get("uninfected_ctrl_fp_k") uc_n = entry.get("uninfected_ctrl_scored_n") rk_i = entry.get("spike_calibrant_detected_k") rn_i = entry.get("spike_calibrant_total_n") frk_i = entry.get("fragile_infected_spike_detected_k") frn_i = entry.get("fragile_infected_spike_total_n") pre_dissoc = entry.get("pre_dissociation_spike_in_attested") spike_sha = entry.get("pre_dissociation_spike_sop_evidence_sha256") if not (_is_strict_int(n_assigned) and _is_strict_int(n_i) and n_i >= 1 and n_assigned >= n_i): holds.append(f"HOLD_GATE0V_B_MISSING_OR_INVALID_LINEAGE_ASSIGNED_COUNT_{p_tag}_{lin.upper()}") path_5lin_cal_ok = False all_pathogens_ok = False all_assigned_counts_present = False else: sum_lineage_assigned_n += n_assigned if not ( _is_strict_int(n_i) and n_i >= 1 and _is_strict_int(k1_i) and 0 <= k1_i <= n_i and _is_strict_int(n2_sorted) and 0 <= n2_sorted <= n_i and _is_strict_int(k2_conf) and 0 <= k2_conf <= n2_sorted ): holds.append(f"HOLD_GATE0V_B_INVALID_TWOSTAGE_COUNTS_{p_tag}_{lin.upper()}") sampling_tracker["all_partitions_reconciled"] = False path_5lin_cal_ok = False all_pathogens_ok = False continue # Hard-stop veto on ANY confirmed infectious cell (evaluated BEFORE power holds!) if k2_conf >= 1: hard_stops.append( f"FAIL_HARD_STOP_GATE0V_CONFIRMED_VIRAL_POSITIVE_{p_tag}_{lin.upper()}_K_{k2_conf}" ) # R12-B1 + R17-B1: Validate per-estimand sampling-unit basis for Day-28 viral # persistence, uninfected negative control, AND both recovery calibrants! lin_unit_maps: Dict[str, Dict[str, Tuple[int, int]]] = {} persist_lbl = f"PERSIST_{p_tag}_{lin.upper()}" lin_unit_basis_ok = _check_estimand_sampling_unit_basis( entry.get("per_unit_counts"), k_val=k2_conf, n_val=n_i, label=persist_lbl, zero_tolerance_hard_stop_token=f"FAIL_HARD_STOP_GATE0V_CONFIRMED_VIRAL_POSITIVE_{p_tag}_{lin.upper()}_K_UNIT_POS", out_unit_maps=lin_unit_maps, ) if not lin_unit_basis_ok: path_5lin_cal_ok = False all_pathogens_ok = False # 100% of Stage-1 screen hits must be routed to Stage-2 orthogonal confirmation if n2_sorted != k1_i: holds.append(f"HOLD_GATE0V_B_UNCONFIRMED_STAGE1_HITS_{p_tag}_{lin.upper()}") path_5lin_cal_ok = False all_pathogens_ok = False with _clean_dec_ctx(80, ROUND_CEILING): screen_frac = Decimal(k1_i) / Decimal(n_i) if screen_frac > max_stage1_screen_frac: holds.append(f"HOLD_GATE0V_B_STAGE1_SCREEN_BURDEN_EXCEEDS_0P2PCT_{p_tag}_{lin.upper()}") path_5lin_cal_ok = False all_pathogens_ok = False # B3 + R17-B1: Uninfected negative-control TRUE Clopper-Pearson U95 <= 0.15% gate # (n >= 20,000) WITH mandatory typed sampling-unit reconciliation! # At n = 20,000, alpha = 0.05: k = 20 -> U95 = 0.145277% <= 0.15% PASS; # k = 21 -> U95 = 0.151167% > 0.15% HOLD! fpr_u95_disp: float | None = None fpr_u95_pass = False neg_ctrl_unit_basis_ok = _check_estimand_sampling_unit_basis( entry.get("uninfected_ctrl_per_unit_counts"), k_val=uc_k, n_val=uc_n, label=f"NEG_CTRL_{p_tag}_{lin.upper()}", ) if not neg_ctrl_unit_basis_ok: path_5lin_cal_ok = False all_pathogens_ok = False if not (_is_strict_int(uc_k) and _is_strict_int(uc_n) and uc_n >= min_uninfected_ctrl_n and 0 <= uc_k <= uc_n): holds.append(f"HOLD_GATE0V_B_MISSING_OR_UNDERPOWERED_UNINFECTED_FPR_CTRL_{p_tag}_{lin.upper()}") path_5lin_cal_ok = False all_pathogens_ok = False else: fpr_u95 = exact_clopper_pearson_upper_general(uc_k, uc_n, alpha_05, prec=60) fpr_u95_disp = _dec_to_float_pct(fpr_u95, 6) if fpr_u95 > max_neg_ctrl_fpr_u95: holds.append(f"HOLD_GATE0V_B_UNINFECTED_CTRL_FPR_U95_EXCEEDS_0P15PCT_{p_tag}_{lin.upper()}") path_5lin_cal_ok = False all_pathogens_ok = False elif neg_ctrl_unit_basis_ok and sampling_spec_valid and deff == Decimal("1.0"): fpr_u95_pass = True if pre_dissoc is not True or not _is_valid_sha256_hex(spike_sha): holds.append(f"HOLD_GATE0V_B_PRE_DISSOCIATION_SPIKE_OR_DIGEST_MISSING_{p_tag}_{lin.upper()}") path_5lin_cal_ok = False all_pathogens_ok = False # R17-B1: Mandatory typed sampling-unit reconciliation for BOTH Day-28 recovery calibrants! cal_h_lbl = f"SPIKE_CAL_HEALTHY_{p_tag}_{lin.upper()}" cal_f_lbl = f"SPIKE_CAL_FRAGILE_{p_tag}_{lin.upper()}" cal_h_unit_basis_ok = _check_estimand_sampling_unit_basis( entry.get("spike_calibrant_per_unit_counts"), k_val=rk_i, n_val=rn_i, label=cal_h_lbl, out_unit_maps=lin_unit_maps, ) cal_f_unit_basis_ok = _check_estimand_sampling_unit_basis( entry.get("fragile_infected_spike_per_unit_counts"), k_val=frk_i, n_val=frn_i, label=cal_f_lbl, out_unit_maps=lin_unit_maps, ) if ( sampling_unit_class == "RECONCILED_MULTI_WELL_CELL_ASSAY_WITH_UNIT_PARTITION" and persist_lbl in lin_unit_maps ): ref_lin_uids = set(lin_unit_maps[persist_lbl].keys()) if ( cal_h_unit_basis_ok and cal_h_lbl in lin_unit_maps and set(lin_unit_maps[cal_h_lbl].keys()) != ref_lin_uids ): cal_h_unit_basis_ok = False sampling_tracker["all_partitions_reconciled"] = False if sampling_tracker["partitions_reconciled"] > 0: sampling_tracker["partitions_reconciled"] -= 1 if "HOLD_SAMPLING_UNIT_SUBSET_OR_ID_CONTRADICTION" not in holds: holds.append("HOLD_SAMPLING_UNIT_SUBSET_OR_ID_CONTRADICTION") holds.append(f"HOLD_GATE0V_B_SPIKE_CAL_HEALTHY_UNIT_ID_MISMATCH_{p_tag}_{lin.upper()}") if ( cal_f_unit_basis_ok and cal_f_lbl in lin_unit_maps and set(lin_unit_maps[cal_f_lbl].keys()) != ref_lin_uids ): cal_f_unit_basis_ok = False sampling_tracker["all_partitions_reconciled"] = False if sampling_tracker["partitions_reconciled"] > 0: sampling_tracker["partitions_reconciled"] -= 1 if "HOLD_SAMPLING_UNIT_SUBSET_OR_ID_CONTRADICTION" not in holds: