def _check_part( raw_part: Any, *, expected_k: Any, expected_n: Any, zero_tol: bool, zt_tokens: Tuple[str, ...] = (), check_agg_exceed: bool = False, ap1903_cap: Decimal | None = None, kill_cap_token: str | None = None, ) -> None: info = _inspect_per_unit_counts( raw_part, expected_units_n=None, expected_k=expected_k if _is_strict_int(expected_k) else None, expected_n=expected_n if _is_strict_int(expected_n) else None, ) if zero_tol and info["any_pos_k"]: for tok in zt_tokens: _append_hs(tok) if info["any_k_gt_n"]: _append_hs("HARD_STOP_ADVERSE_PER_UNIT_CONTRADICTS_AGGREGATE_OR_BREACHES_KILL_FLOOR") if ( check_agg_exceed and info["interpretable_rows_n"] > 0 and _is_strict_int(expected_k) and info["sum_interp_k"] > int(expected_k) ): _append_hs("HARD_STOP_ADVERSE_PER_UNIT_CONTRADICTS_AGGREGATE_OR_BREACHES_KILL_FLOOR") if ap1903_cap is not None and info["sum_interp_k"] >= 1: den_n = max( int(info["sum_interp_n"]), int(expected_n) if (_is_strict_int(expected_n) and int(expected_n) >= 1) else 0, ) if den_n >= 1: if int(info["sum_interp_k"]) > den_n: _append_hs("HARD_STOP_ADVERSE_PER_UNIT_CONTRADICTS_AGGREGATE_OR_BREACHES_KILL_FLOOR") elif raw_deff_dec == Decimal("1.0"): l_ideal = exact_clopper_pearson_lower_calibrant( int(info["sum_interp_k"]), den_n, Decimal("0.025"), prec=60 ) if l_ideal > ap1903_cap: _append_hs("HARD_STOP_ADVERSE_PER_UNIT_CONTRADICTS_AGGREGATE_OR_BREACHES_KILL_FLOOR") if kill_cap_token: _append_hs(kill_cap_token) if not isinstance(payload, dict): return {"any_per_unit_counts_present": False} # 1. Gate 0V-Kill (ALL stress regimes — required, supplemental, and unrecognized; ALL lineages!) ap = payload.get("gate0v_kill_stress_ap1903") if isinstance(ap, dict): for reg, rb in ap.items(): if not isinstance(rb, dict): continue is_sup = bool(required_kill_regimes and reg not in required_kill_regimes) sup_hs = "SUPPLEMENTAL_" if is_sup else "" reg_tag = str(reg).upper() for lin, lb in rb.items(): if not isinstance(lb, dict) or "per_unit_counts" not in lb: continue any_puc_present = True visited_containers.add(id(lb)) lin_tag = str(lin).upper() puc = lb.get("per_unit_counts") if isinstance(puc, dict): for sub_k, sub_raw in puc.items(): sk_low = str(sub_k).lower() if sub_k == "veh_24h": _check_part( sub_raw, expected_k=lb.get("veh_24h_viable_n"), expected_n=lb.get("veh_24h_input_n"), zero_tol=False, check_agg_exceed=False, ) elif sub_k == "veh_72h": _check_part( sub_raw, expected_k=lb.get("veh_72h_viable_n"), expected_n=lb.get("veh_72h_input_n"), zero_tol=False, check_agg_exceed=False, ) elif sub_k == "direct_infected_veh_72h": _check_part( sub_raw, expected_k=lb.get("pretreat_infected_veh_survivor_n"), expected_n=lb.get("veh_72h_input_n"), zero_tol=False, check_agg_exceed=False, ) elif sub_k == "spike_calibrant": _check_part( sub_raw, expected_k=lb.get("spike_calibrant_detected_k"), expected_n=lb.get("spike_calibrant_total_n"), zero_tol=False, check_agg_exceed=False, ) elif sub_k == "fragile_infected_spike_calibrant": _check_part( sub_raw, expected_k=lb.get("fragile_infected_spike_detected_k"), expected_n=lb.get("fragile_infected_spike_total_n"), zero_tol=False, check_agg_exceed=False, ) elif sub_k == "ap1903_24h": _check_part( sub_raw, expected_k=lb.get("ap1903_24h_viable_survivors_k"), expected_n=lb.get("ap1903_24h_input_n"), zero_tol=False, check_agg_exceed=True, ap1903_cap=Decimal("0.001"), kill_cap_token=f"FAIL_HARD_STOP_{sup_hs}AP1903_24H_KILL_BELOW_99P9PCT_{reg_tag}_{lin_tag}", ) elif sub_k == "ap1903_72h": _check_part( sub_raw, expected_k=lb.get("ap1903_72h_viable_survivors_k"), expected_n=lb.get("ap1903_72h_input_n"), zero_tol=False, check_agg_exceed=True, ap1903_cap=Decimal("0.0001"), kill_cap_token=f"FAIL_HARD_STOP_{sup_hs}AP1903_72H_PK_KILL_BELOW_4LOG10_{reg_tag}_{lin_tag}", ) elif ( sub_k == "infected_viral_pos_72h" or "viral_pos" in sk_low or "post_ap1903_viral" in sk_low or "viral_escape" in sk_low ): _check_part( sub_raw, expected_k=lb.get("post_ap1903_viable_viral_pos_k"), expected_n=lb.get("pre_ap1903_lineage_input_cells_n"), zero_tol=True, zt_tokens=( "HARD_STOP_ADVERSE_PER_UNIT_POST_AP1903_VIRAL_ESCAPE", f"FAIL_HARD_STOP_{sup_hs}POST_AP1903_VIABLE_VIRAL_SURVIVOR_{reg_tag}_{lin_tag}", ), check_agg_exceed=True, ) else: # Unknown extra sub-key inside Gate 0V-Kill per_unit_counts is_viral_extra = ("viral" in sk_low) or ("escape" in sk_low) _check_part( sub_raw, expected_k=0 if is_viral_extra else None, expected_n=None, zero_tol=is_viral_extra, zt_tokens=( "HARD_STOP_ADVERSE_PER_UNIT_POST_AP1903_VIRAL_ESCAPE", f"FAIL_HARD_STOP_{sup_hs}POST_AP1903_VIABLE_VIRAL_SURVIVOR_{reg_tag}_{lin_tag}", ) if is_viral_extra else (), check_agg_exceed=is_viral_extra, ) elif isinstance(puc, list): # Flat list directly under lblock["per_unit_counts"] info_flat = _inspect_per_unit_counts(puc, expected_units_n=None, expected_k=None, expected_n=None) if info_flat["any_k_gt_n"]: _append_hs("HARD_STOP_ADVERSE_PER_UNIT_CONTRADICTS_AGGREGATE_OR_BREACHES_KILL_FLOOR") if ( info_flat["any_pos_k"] and lb.get("ap1903_24h_viable_survivors_k") == 0 and lb.get("ap1903_72h_viable_survivors_k") == 0 and lb.get("post_ap1903_viable_viral_pos_k", 0) == 0 ): _append_hs("HARD_STOP_ADVERSE_PER_UNIT_CONTRADICTS_AGGREGATE_OR_BREACHES_KILL_FLOOR") # 2. Gate 0V-B pathogens (ALL pathogens — required, supplemental, and unrecognized; ALL lineages & targets!) inf = payload.get("gate0v_b_infectious_challenge") if isinstance(inf, dict) and isinstance(inf.get("pathogens"), dict): for pid, pb in inf["pathogens"].items(): if not isinstance(pb, dict): continue p_tag = str(pid).upper() lat = pb.get("day28_latent_and_defective_genome_arm") if isinstance(lat, dict): if "per_unit_counts" in lat: any_puc_present = True visited_containers.add(id(lat)) _check_part( lat.get("per_unit_counts"), expected_k=lat.get("ddpcr_positive_events_k"), expected_n=lat.get("ddpcr_cell_equivalents_screened_n"), zero_tol=True, zt_tokens=( "HARD_STOP_ADVERSE_PER_UNIT_VIRAL_PERSISTENCE", f"FAIL_HARD_STOP_GATE0V_B_LATENT_OR_DEFECTIVE_GENOME_DETECTED_{p_tag}_K_UNIT_POS", ), check_agg_exceed=True, ) tgts = lat.get("ddpcr_targets") if isinstance(tgts, dict): for tname, te in tgts.items(): if isinstance(te, dict) and "per_unit_counts" in te: any_puc_present = True visited_containers.add(id(te)) t_tag = str(tname).upper() _check_part( te.get("per_unit_counts"), expected_k=te.get("k"), expected_n=te.get("n"), zero_tol=True, zt_tokens=( "HARD_STOP_ADVERSE_PER_UNIT_VIRAL_PERSISTENCE", f"FAIL_HARD_STOP_GATE0V_B_LATENT_OR_DEFECTIVE_GENOME_DETECTED_{p_tag}_{t_tag}_K_UNIT_POS", ), check_agg_exceed=True, ) unass = pb.get("unassigned_fraction_viral_readout") if isinstance(unass, dict) and "per_unit_counts" in unass: any_puc_present = True visited_containers.add(id(unass)) _check_part( unass.get("per_unit_counts"), expected_k=unass.get("stage2_confirmed_infectious_k"), expected_n=unass.get("unassigned_cells_screened_n"), zero_tol=True, zt_tokens=( "HARD_STOP_ADVERSE_PER_UNIT_VIRAL_PERSISTENCE", f"FAIL_HARD_STOP_GATE0V_CONFIRMED_VIRAL_POSITIVE_UNASSIGNED_{p_tag}_K_UNIT_POS", ), check_agg_exceed=True, ) lin_map = pb.get("day28_persistence_by_lineage") if isinstance(lin_map, dict): for lin_name, le in lin_map.items(): if not isinstance(le, dict): continue l_tag = str(lin_name).upper() if "per_unit_counts" in le: any_puc_present = True visited_containers.add(id(le)) _check_part( le.get("per_unit_counts"), expected_k=le.get("stage2_confirmed_infectious_k"), expected_n=le.get("cells_scored_n"), zero_tol=True, zt_tokens=( "HARD_STOP_ADVERSE_PER_UNIT_VIRAL_PERSISTENCE", f"FAIL_HARD_STOP_GATE0V_CONFIRMED_VIRAL_POSITIVE_{p_tag}_{l_tag}_K_UNIT_POS", ), check_agg_exceed=True, ) for aux_puc_key, aux_k_key, aux_n_key in (