or not is_nonempty_str(il10_assay.get("assay_id")) or str(il10_assay.get("lot_id", "")).strip() != lot_s or (blank_veh_40 is not None and str(il10_assay.get("matched_blank_vehicle_lot_id", "")).strip() != blank_veh_40) or t_iexp is None or t_iwash is None or t_irecov is None or t_irec is None or not (t_iexp < t_iwash < t_irecov <= t_irec) or (t_b3_r is not None and t_irec > t_b3_r) or (t_b2be_r is not None and t_iexp < t_b2be_r) or not isinstance(il10_donors, list) or len(il10_donors) < 3 ): fails.append("FAIL_GATE_B3_IL10_WASHOUT_RECOVERY_ASSAY_INVALID"); b3_ok = False elif not ( is_finite_num(il10_assay.get("exposure_measured_free_parent_nM")) and float(il10_assay["exposure_measured_free_parent_nM"]) > 0.0 and (b2be_p_lo is None or float(il10_assay["exposure_measured_free_parent_nM"]) >= b2be_p_lo - 1e-9) and (b2be_j_hi is None or float(il10_assay["exposure_measured_free_parent_nM"]) <= b2be_j_hi + 1e-9) ): fails.append("FAIL_GATE_B3_IL10_WASHOUT_EXPOSURE_UNLINKED_TO_B2B_EXACT"); b3_ok = False else: wash_h = (t_irecov - t_iwash).total_seconds() / 3600.0 if not (70.0 <= wash_h <= 76.0): fails.append("FAIL_GATE_B3_IL10_WASHOUT_INTERVAL_NOT_72H"); b3_ok = False d_req_k = frozenset({ "donor_id", "raw_record_id", "treated_post_washout_il10_suppression_pct", "matched_vehicle_il10_suppression_pct", "record_sha256", }) seen_id_d: set[str] = set() seen_id_r: set[str] = set() rec_pcts: list[float] = [] donors_valid = True for drow in il10_donors: if not isinstance(drow, Mapping) or set(drow.keys()) != d_req_k: donors_valid = False; break did = drow.get("donor_id") rid = drow.get("raw_record_id") tr_p = drow.get("treated_post_washout_il10_suppression_pct") vh_p = drow.get("matched_vehicle_il10_suppression_pct") dsha = drow.get("record_sha256") if ( not is_nonempty_str(did) or not is_nonempty_str(rid) or not (is_finite_num(tr_p) and float(tr_p) >= 0.0 and is_finite_num(vh_p) and float(vh_p) > 0.0) or not is_valid_sha256_hex(dsha) or sha256_bytes(canonical_json_bytes({k: v for k, v in drow.items() if k != "record_sha256"})) != str(dsha) ): donors_valid = False; break dn = str(did).strip().upper(); rn = str(rid).strip().upper() if dn in seen_id_d or rn in seen_id_r: donors_valid = False; break seen_id_d.add(dn); seen_id_r.add(rn) rec_pcts.append(100.0 * float(tr_p) / float(vh_p)) if not donors_valid or len(rec_pcts) < 3: fails.append("FAIL_GATE_B3_IL10_WASHOUT_RECOVERY_ASSAY_INVALID"); b3_ok = False else: recomp_il10_mean = sum(rec_pcts) / len(rec_pcts) if min(rec_pcts) < 80.0 or recomp_il10_mean < 90.0: fails.append("FAIL_GATE_B3_POST_WASHOUT_72H_IL10_RECOVERY_LT_90PCT"); b3_ok = False if is_finite_num(il10_rec) and abs(recomp_il10_mean - float(il10_rec)) > 0.1: fails.append("FAIL_GATE_B3_IL10_RECOVERY_MISMATCH_VS_RAW_DONORS"); b3_ok = False if not (is_finite_num(nkg_mfi) and 85.0 <= float(nkg_mfi) <= 150.0 and is_finite_num(cd94_mfi) and 85.0 <= float(cd94_mfi) <= 150.0): fails.append("FAIL_GATE_B3_NKG2A_CD94_MFI_BELOW_85PCT"); b3_ok = False if not _validate_readout3b_nk( b3_ev.get("readout3b_evidence"), fails=fails, prefix="GATE_B3", require_sirpa_low=True, expected_vehicle_lot_id=blank_veh_40, max_vehicle_delta_pp=2.0, require_hla_e_spr=bool(spec["hla_e_engineered"]) if spec is not None else True, ): b3_ok = False if not _validate_readout2_gsis( b3_ev.get("readout2_evidence"), fails=fails, holds=holds, prefix="GATE_B3", expected_comparator_id=blank_veh_40, min_comparator_output_ratio=0.90, parent_recorded_at_utc=parse_utc_iso_ts(b3_ev.get("recorded_at_utc")), min_exposure_start_utc=parse_utc_iso_ts(b2be_ev.get("recorded_at_utc")) if b2be_ev is not None else None, expected_prod=prod_s, expected_bank=bank_s, expected_lot=lot_s, expected_route=route_s, ): b3_ok = False if spec is not None and not _validate_lineage_hour0_density( spec, b3_ev.get("hour0_density_by_lineage"), required_lineages=REQUIRED_HOUR0_LINEAGES_FULL, fails=fails, prefix="GATE_B3", ): b3_ok = False # Gate B-4: max inter-sample gap <= 72 h across [0, 240 h] (HOLD if gappy!) + matched-vehicle switch parity data! b4_extras = frozenset({ "invivo_study_started_at_utc", "invivo_time_resolved_pk_gate_retained", "b4_local_compartment_calibration", "b4_systemic_compartment_calibration", "serial_pk_schedule_hours", "serial_pk_local_parent_free_nM", "serial_pk_local_bmt_free_nM", "serial_pk_systemic_parent_total_nM", "serial_pk_systemic_bmt_total_nM", "timepoint_replicate_records", "censoring_audit_passed", "blq_imputation_rule", "alpha_conc", "alpha_fu_parent", "alpha_fu_bmt", "intersample_peak_envelope_justified", "intersample_pk_envelope", "invivo_u95_joint_peak_free_nM", "invivo_days0_10_parent_lower_free_nM", "invivo_systemic_plasma_u95_nM", "matched_control_switch_parity_evidence", "mixture_requalification_receipt", }) b4_ev = _validate_content_receipt( rec.get("b4_evidence"), expected_stage="B-4", expected_prod=prod_s, expected_bank=bank_s, expected_lot=lot_s, expected_route=route_s, expected_scope="exact_product", expected_coating_identity=coat_id, expected_parent=b3_ev, min_prereq_receipts=[("BANK_MATERIALIZATION", mat_ev), ("NATIVE_SAFETY_SWITCH", sw_ev), ("GATE-0V-KILL", g0v_ev)], allowed_extra_keys=b4_extras, fails=fails, holds=holds, missing_hold="HOLD_PRE_INVIVO_B4_PENDING", fail_code="FAIL_GATE_B4_INVIVO_PK_SWITCH_FAILED", mismatch_code="FAIL_GATE_B4_SCOPE_MISMATCH", ) if b4_ev is not None: if not (is_strict_bool(b4_ev.get("invivo_time_resolved_pk_gate_retained")) and b4_ev["invivo_time_resolved_pk_gate_retained"]): fails.append("FAIL_MANDATORY_INVIVO_PK_GATE_WAIVED") t_st = parse_utc_iso_ts(b4_ev.get("invivo_study_started_at_utc")) t_b4 = parse_utc_iso_ts(b4_ev.get("recorded_at_utc")) t_b3 = parse_utc_iso_ts(b3_ev.get("recorded_at_utc")) if b3_ev is not None else None t_sw = parse_utc_iso_ts(sw_ev.get("recorded_at_utc")) if sw_ev is not None else None t_g0 = parse_utc_iso_ts(g0v_ev.get("recorded_at_utc")) if g0v_ev is not None else None if t_st is None or t_b4 is None: fails.append("FAIL_GATE_B4_MISSING_INVIVO_STUDY_START_UTC") else: for lbl, pt in (("B3", t_b3), ("SWITCH", t_sw), ("GATE0V", t_g0)): if pt is not None and t_st < pt: fails.append(f"FAIL_GATE_B4_STUDY_STARTED_BEFORE_{lbl}") if (t_b4 - t_st) < timedelta(days=21): fails.append("FAIL_GATE_B4_ELAPSED_STUDY_DURATION_LT_21_DAYS") hrs = b4_ev.get("serial_pk_schedule_hours") lp_arr = b4_ev.get("serial_pk_local_parent_free_nM") lb_arr = b4_ev.get("serial_pk_local_bmt_free_nM") sp_arr = b4_ev.get("serial_pk_systemic_parent_total_nM") sb_arr = b4_ev.get("serial_pk_systemic_bmt_total_nM") a_c = b4_ev.get("alpha_conc") a_fp = b4_ev.get("alpha_fu_parent") a_fb = b4_ev.get("alpha_fu_bmt") b4_alpha_ok = ( is_finite_num(a_c) and is_finite_num(a_fp) and is_finite_num(a_fb) and float(a_c) > 0.0 and float(a_fp) > 0.0 and float(a_fb) > 0.0 and (float(a_c) + float(a_fp) + float(a_fb)) <= 0.05 + 1e-12 ) iv_pk = b4_ev.get("invivo_u95_joint_peak_free_nM") iv_lo = b4_ev.get("invivo_days0_10_parent_lower_free_nM") iv_sys = b4_ev.get("invivo_systemic_plasma_u95_nM") if not (is_strict_bool(b4_ev.get("censoring_audit_passed")) and b4_ev["censoring_audit_passed"]): fails.append("FAIL_GATE_B4_CENSORING_AUDIT_MISSING") if b4_ev.get("blq_imputation_rule") != "LLOQ_TIMES_FU_U_ALLOC_SAFETY_AND_ZERO_EFFICACY": fails.append("FAIL_GATE_B4_BLQ_IMPUTATION_RULE_INVALID") b4_eff_csafe = csafe_op_val b4_eff_ec50 = ec50_u95 if not ( isinstance(hrs, list) and len(hrs) >= 7 and all(isinstance(arr, list) and len(arr) == len(hrs) for arr in (lp_arr, lb_arr, sp_arr, sb_arr)) and all(is_finite_num(x) for x in hrs + lp_arr + lb_arr + sp_arr + sb_arr) ): fails.append("FAIL_GATE_B4_INVALID_OR_INCOMPLETE_SERIAL_PK_ARRAYS") else: hf = [float(x) for x in hrs] lpf = [float(x) for x in lp_arr] lbf = [float(x) for x in lb_arr] spf = [float(x) for x in sp_arr] sbf = [float(x) for x in sb_arr] if any(hf[i] >= hf[i + 1] for i in range(len(hf) - 1)) or hf[0] <= 0.0: fails.append("FAIL_GATE_B4_SERIAL_PK_TIMESTAMPS_NOT_STRICTLY_INCREASING") if t_st is not None and t_b4 is not None: elapsed_h = (t_b4 - t_st).total_seconds() / 3600.0 if hf[-1] > elapsed_h + 1e-9: fails.append("FAIL_GATE_B4_SERIAL_PK_SAMPLE_POSTDATES_RECEIPT") day21_indices = [i for i, h in enumerate(hf) if 504.0 <= h <= 528.0] if hf[0] > 6.0 or not any(6.0 < h <= 24.0 for h in hf) or not any(abs(h - 240.0) <= 12.0 for h in hf) or not day21_indices: fails.append("FAIL_GATE_B4_SERIAL_PK_WINDOW_MISSES_BURST_DAY10_OR_DAY21_WINDOW") # Enforce maximum inter-sample gap <= 72 h across the ENTIRE Day 0-21 window [0, 504 h] (zero post-Day-10 blind spots!) all_gaps = [hf[i + 1] - hf[i] for i in range(len(hf) - 1)] if all_gaps and max(all_gaps) > 72.0 + 1e-9: holds.append("HOLD_GATE_B4_INSUFFICIENT_TEMPORAL_COVERAGE_GAP_GT_72H") if min(lpf + lbf + spf + sbf) < 0.0: fails.append("FAIL_GATE_B4_NEGATIVE_SERIAL_PK_CONCENTRATION") local_comb = [lpf[i] + lbf[i] for i in range(len(hf))] sys_comb = [spf[i] + sbf[i] for i in range(len(hf))] if is_finite_num(iv_pk) and max(local_comb) > float(iv_pk) + 1e-9: fails.append("FAIL_GATE_B4_COMBINED_LOCAL_PEAK_EXCEEDS_REPORTED_U95_JOINT_PEAK") if is_finite_num(iv_sys) and max(sys_comb) > float(iv_sys) + 1e-9: fails.append("FAIL_GATE_B4_COMBINED_SYSTEMIC_PEAK_EXCEEDS_REPORTED_U95_SYSTEMIC") d0_10_lp = [lpf[i] for i in range(len(hf)) if hf[i] <= 240.0 + 1e-9] if d0_10_lp and is_finite_num(iv_lo) and float(iv_lo) > min(d0_10_lp) + 1e-9: fails.append("FAIL_GATE_B4_REPORTED_LOWER_BOUND_EXCEEDS_OBSERVED_MIN") check_wash_idxs = set(day21_indices) | {len(hf) - 1} if any(local_comb[idx] >= 2.0 or (ec50_u95 is not None and local_comb[idx] >= ec50_u95) for idx in check_wash_idxs): fails.append("FAIL_GATE_B4_DAY21_WASHOUT_NOT_EXHAUSTED") b4_tps = b4_ev.get("timepoint_replicate_records") loc_cal = b4_ev.get("b4_local_compartment_calibration") sys_cal = b4_ev.get("b4_systemic_compartment_calibration") b4_raw_bmt_trig = False b4_unbounded_bmt_ratio = False b4_obs_free_min = float("inf") b4_obs_free_max = 0.0 u_local4_series: list[float] = [] u_bmt4_free_series: list[float] = [] l_bmt4_free_series: list[float] = [] l_parent4_all_s: list[float] = [] u_parent4_all_s: list[float] = [] u_sys4_series: list[float] = [] day21_u_joint4: Optional[float] = None b4_tp_ok = False loc_cal_keys = frozenset({ "compartment_matrix_id", "concentration_basis", "pre_submission_recovery_correction_applied", "species_id", "analytical_method_id", "lloq_parent_nM", "lloq_bmt_nM", "probe_or_extraction_recovery_pct", "spike_accuracy_pct", "method_validated", "fu_parent_replicates", "fu_bmt_replicates", "calibration_sha256", }) sys_cal_plasma_keys = frozenset({ "compartment_matrix_id", "species_id", "analytical_method_id", "lloq_parent_nM", "lloq_bmt_nM", "spike_accuracy_pct", "method_validated", "fu_parent_replicates", "fu_bmt_replicates", "calibration_sha256", }) sys_cal_wb_keys = sys_cal_plasma_keys | frozenset({ "kbp_assay_method_id", "blood_to_plasma_ratio_parent_replicates", "blood_to_plasma_ratio_bmt_replicates", "alpha_kbp_parent", "alpha_kbp_bmt", }) is_sys_wb = isinstance(sys_cal, Mapping) and sys_cal.get("compartment_matrix_id") == "SYSTEMIC_WHOLE_BLOOD" wb_cal_valid = (not is_sys_wb) or ( set(sys_cal.keys()) == sys_cal_wb_keys and is_nonempty_str(sys_cal.get("kbp_assay_method_id")) and is_finite_num(sys_cal.get("alpha_kbp_parent")) and float(sys_cal["alpha_kbp_parent"]) > 0.0 and is_finite_num(sys_cal.get("alpha_kbp_bmt")) and float(sys_cal["alpha_kbp_bmt"]) > 0.0 and all( isinstance(sys_cal.get(k), list) and len(sys_cal[k]) >= 3 and all(is_finite_num(x) and 0.05 <= float(x) <= 20.0 for x in sys_cal[k]) for k in ("blood_to_plasma_ratio_parent_replicates", "blood_to_plasma_ratio_bmt_replicates") ) ) cal_ok = ( isinstance(loc_cal, Mapping) and set(loc_cal.keys()) == loc_cal_keys and isinstance(sys_cal, Mapping) and ((sys_cal.get("compartment_matrix_id") == "SYSTEMIC_PLASMA" and set(sys_cal.keys()) == sys_cal_plasma_keys) or (is_sys_wb and wb_cal_valid)) and loc_cal.get("compartment_matrix_id") in ("PERI_GRAFT_MICRODIALYSIS_DIALYSATE", "PERI_GRAFT_INTERSTITIAL_FLUID", "EXPLANT_TISSUE_HOMOGENATE") and is_strict_bool(loc_cal.get("pre_submission_recovery_correction_applied")) and loc_cal["pre_submission_recovery_correction_applied"] and is_nonempty_str(loc_cal.get("species_id")) and loc_cal.get("species_id") == sys_cal.get("species_id") and is_nonempty_str(loc_cal.get("analytical_method_id")) and is_nonempty_str(sys_cal.get("analytical_method_id")) and is_strict_bool(loc_cal.get("method_validated")) and loc_cal["method_validated"] and is_strict_bool(sys_cal.get("method_validated")) and sys_cal["method_validated"] and is_finite_num(loc_cal.get("probe_or_extraction_recovery_pct")) and 70.0 <= float(loc_cal["probe_or_extraction_recovery_pct"]) <= 115.0 and is_finite_num(loc_cal.get("spike_accuracy_pct")) and 80.0 <= float(loc_cal["spike_accuracy_pct"]) <= 120.0 and is_finite_num(sys_cal.get("spike_accuracy_pct")) and 80.0 <= float(sys_cal["spike_accuracy_pct"]) <= 120.0 and all(is_finite_num(c.get(k)) and 0.0 < float(c[k]) <= 0.2 for c in (loc_cal, sys_cal) for k in ("lloq_parent_nM", "lloq_bmt_nM")) and all(isinstance(c.get(k), list) and len(c[k]) >= 3 and all(is_finite_num(x) and 0.0 < float(x) <= 1.0 for x in c[k]) for c in (loc_cal, sys_cal) for k in ("fu_parent_replicates", "fu_bmt_replicates")) and all(is_valid_sha256_hex(c.get("calibration_sha256")) and sha256_bytes(canonical_json_bytes({k: v for k, v in c.items() if k != "calibration_sha256"})) == str(c["calibration_sha256"]) for c in (loc_cal, sys_cal)) and ( ( loc_cal.get("compartment_matrix_id") == "PERI_GRAFT_MICRODIALYSIS_DIALYSATE"