min_ratio = min(min_ratio, b_l / p_u) if min_ratio == float("inf"): min_ratio = 0.0 return min_ratio, max_ratio, unbounded def _validate_mixture_requal_receipt( mix_raw: Any, *, expected_prod: str, expected_bank: str, expected_lot: str, expected_route: str, expected_scope: str, expected_coating_identity: Optional[Mapping[str, str]], parent_recorded_at_utc: Optional[datetime], observed_bmt_to_parent_ratio_min: float, observed_bmt_to_parent_ratio_max: float, unbounded_ratio_under_parent_blq: bool = False, fails: list[str], holds: list[str], prefix: str, ) -> tuple[bool, Optional[float], Optional[float]]: """ Validate phase-scoped content-hashed parent+BMT-153261 mixture requalification receipt: - Enforces matching scope ("bench_unedited" on B-2b-bench, "exact_product" on B-2b-exact / B-4) - Enforces tested_bmt_to_parent_ratio_min <= observed_free_ratio_min <= observed_free_ratio_max <= tested_bmt_to_parent_ratio_max - Returns (ok, mixture_ec50_eff_u95_free_nM, mixture_c_safe_max_l95_free_nM) so downstream exposure bounds are DIRECTLY adjudicated against the mixture-specific therapeutic window! """ mix_ev = _validate_content_receipt( mix_raw, expected_stage="MIXTURE_REQUALIFICATION", expected_prod=expected_prod, expected_bank=expected_bank, expected_lot=expected_lot, expected_route=expected_route, expected_scope=expected_scope, expected_coating_identity=expected_coating_identity, allowed_extra_keys=frozenset({ "analysis_record_sha256", "tested_bmt_to_parent_ratio_min", "tested_bmt_to_parent_ratio_max", "mixture_ec50_eff_u95_free_nM", "mixture_c_safe_max_l95_free_nM", "mixture_dose_response_evidence", "metabolite_dominant_unbounded_ratio_qualified", "bmt_alone_qualification_evidence", }), fails=fails, holds=holds, missing_hold=f"HOLD_{prefix}_METABOLITE_TRIGGERED_MIXTURE_REQUAL_REQUIRED", fail_code=f"FAIL_{prefix}_MIXTURE_REQUAL_RECEIPT_FAILED", mismatch_code=f"FAIL_{prefix}_MIXTURE_REQUAL_SCOPE_MISMATCH", ) if mix_ev is None: return False, None, None t_m = parse_utc_iso_ts(mix_ev.get("recorded_at_utc")) if parent_recorded_at_utc is not None and t_m is not None and t_m > parent_recorded_at_utc: fails.append(f"FAIL_{prefix}_MIXTURE_REQUAL_POSTDATES_PARENT"); return False, None, None e50_m = mix_ev.get("mixture_ec50_eff_u95_free_nM") cs_m = mix_ev.get("mixture_c_safe_max_l95_free_nM") r_lo = mix_ev.get("tested_bmt_to_parent_ratio_min") r_hi = mix_ev.get("tested_bmt_to_parent_ratio_max") if not ( is_valid_sha256_hex(mix_ev.get("analysis_record_sha256")) and is_finite_num(r_lo) and is_finite_num(r_hi) and 0.0 <= float(r_lo) <= float(r_hi) and float(r_hi) > 0.0 and is_finite_num(e50_m) and float(e50_m) > 0.0 and is_finite_num(cs_m) and float(cs_m) >= 2.0 * float(e50_m) ): fails.append(f"FAIL_{prefix}_MIXTURE_REQUAL_MARGIN_BELOW_2X_OR_INVALID"); return False, None, None mix_safe_specs = ( ("sprouting_pct", 90.0), ("nkg2a_pct", 85.0), ("cd94_pct", 85.0), ("il10_retention_pct", 80.0), ) mix_safe_keys = frozenset(k for k, _ in mix_safe_specs) mix_eff_keys = frozenset({ "beta_cytokine_protection_pct", "nk_cytokine_suppression_pct", "iec_ibmir_marker_suppression_pct", }) def _eval_7ep_panel_curve( grid_f: Sequence[float], s_rows: Any, e_rows: Any, *, c_safe_claim: float, ec50_claim: float, alpha_safe: float, alpha_eff: float, ) -> tuple[str, Optional[str]]: if not ( isinstance(s_rows, list) and len(s_rows) == len(grid_f) and isinstance(e_rows, list) and len(e_rows) == len(grid_f) and all(isinstance(r, Mapping) and set(r.keys()) == mix_safe_keys for r in s_rows) and all(isinstance(r, Mapping) and set(r.keys()) == mix_eff_keys for r in e_rows) ): return "FAIL", "SCHEMA" for sr in s_rows: for sk in mix_safe_keys: arr = sr[sk] if not (isinstance(arr, list) and len(arr) >= 3 and all(is_finite_num(x) and 0.0 <= float(x) <= 150.0 for x in arr)): return "FAIL", "SCHEMA" for er in e_rows: for ek in mix_eff_keys: arr = er[ek] if not (isinstance(arr, list) and len(arr) >= 3 and all(is_finite_num(x) and -20.0 <= float(x) <= 120.0 for x in arr)): return "FAIL", "SCHEMA" prefix_ok = True safe_reached = False tox_above = False eff_at_ec50_ok = False zero_eff_ok = all((sum(float(x) for x in e_rows[0][ek]) / len(e_rows[0][ek])) < 20.0 for ek in mix_eff_keys) zero_disp = False idx_le_ec = [i for i, cv in enumerate(grid_f) if cv <= ec50_claim + 1e-9] target_ec_idx = idx_le_ec[-1] if idx_le_ec else -1 for idx_g, (c_val, sr, er) in enumerate(zip(grid_f, s_rows, e_rows)): all_safe_pass = True any_safe_fail = False for sk, sfloor in mix_safe_specs: rf = [float(x) for x in sr[sk]] sb = sample_t_one_sided_bounds(rf, alpha=alpha_safe) if sb is None: zero_disp = True all_safe_pass = False else: if sb[1] < sfloor: all_safe_pass = False any_safe_fail = True all_eff_pass = True for ek in mix_eff_keys: rf = [float(x) for x in er[ek]] eb = sample_t_one_sided_bounds(rf, alpha=alpha_eff) if eb is None: zero_disp = True all_eff_pass = False elif eb[1] < 50.0: all_eff_pass = False if c_val <= c_safe_claim + 1e-9: if not all_safe_pass: prefix_ok = False if c_val >= c_safe_claim - 1e-9 and all_safe_pass: safe_reached = True elif any_safe_fail: tox_above = True if idx_g == target_ec_idx and all_eff_pass: eff_at_ec50_ok = True if zero_disp: return "HOLD", "ZERO_DISP" if not (prefix_ok and safe_reached and tox_above and eff_at_ec50_ok and zero_eff_ok): return "HOLD", "WINDOW" return "OK", None if unbounded_ratio_under_parent_blq: if not (is_strict_bool(mix_ev.get("metabolite_dominant_unbounded_ratio_qualified")) and mix_ev["metabolite_dominant_unbounded_ratio_qualified"]): holds.append(f"HOLD_{prefix}_UNBOUNDED_BMT_TO_PARENT_RATIO_UNDER_PARENT_BLQ"); return False, None, None bmt_ev = mix_ev.get("bmt_alone_qualification_evidence") bmt_req = frozenset({ "analysis_method", "tested_product_id", "tested_free_bmt_conc_grid_nM", "bmt_alone_safety_replicates_by_conc", "bmt_alone_efficacy_replicates_by_conc", "bmt_alone_c_safe_max_l95_free_nM", "bmt_alone_ec50_eff_u95_free_nM", "evidence_sha256", }) if bmt_ev is None: holds.append(f"HOLD_{prefix}_MISSING_BMT_ALONE_BIOLOGICAL_QUALIFICATION_EVIDENCE"); return False, None, None if ( not isinstance(bmt_ev, Mapping) or set(bmt_ev.keys()) != bmt_req or bmt_ev.get("analysis_method") != "SIMULTANEOUS_BONFERRONI_GRID_BRACKET_STUDENT_T" or (expected_scope == "exact_product" and bmt_ev.get("tested_product_id") != expected_prod) or (expected_scope != "exact_product" and not is_nonempty_str(bmt_ev.get("tested_product_id"))) or not is_valid_sha256_hex(bmt_ev.get("evidence_sha256")) or sha256_bytes(canonical_json_bytes({k: v for k, v in bmt_ev.items() if k != "evidence_sha256"})) != str(bmt_ev["evidence_sha256"]) ): fails.append(f"FAIL_{prefix}_INVALID_BMT_ALONE_QUALIFICATION_EVIDENCE"); return False, None, None bg = bmt_ev.get("tested_free_bmt_conc_grid_nM") bsr = bmt_ev.get("bmt_alone_safety_replicates_by_conc") ber = bmt_ev.get("bmt_alone_efficacy_replicates_by_conc") b_cs = bmt_ev.get("bmt_alone_c_safe_max_l95_free_nM") b_ec = bmt_ev.get("bmt_alone_ec50_eff_u95_free_nM") if not ( isinstance(bg, list) and len(bg) >= 5 and all(is_finite_num(x) and float(x) >= 0.0 for x in bg) and float(bg[0]) == 0.0 and all(float(bg[i]) < float(bg[i + 1]) for i in range(len(bg) - 1)) and is_finite_num(b_cs) and is_finite_num(b_ec) and float(b_ec) > 0.0 and float(b_cs) >= 2.0 * float(b_ec) and float(b_cs) >= float(cs_m) - 1e-9 and float(b_ec) <= float(e50_m) + 1e-9 ): fails.append(f"FAIL_{prefix}_BMT_ALONE_WINDOW_INVALID_OR_WEAKER_THAN_MIXTURE"); return False, None, None bg_f = [float(x) for x in bg] st_bmt, why_bmt = _eval_7ep_panel_curve( bg_f, bsr, ber, c_safe_claim=float(b_cs), ec50_claim=float(b_ec), alpha_safe=0.05 / float(4 * len(bg_f)), alpha_eff=0.05 / float(3 * len(bg_f)), ) if st_bmt == "FAIL": fails.append(f"FAIL_{prefix}_INVALID_BMT_ALONE_QUALIFICATION_EVIDENCE"); return False, None, None if st_bmt == "HOLD": code = f"HOLD_{prefix}_BMT_ALONE_ZERO_DISPERSION_UNESTIMABLE" if why_bmt == "ZERO_DISP" else f"HOLD_{prefix}_BMT_ALONE_REPLICATES_DO_NOT_SUBSTANTIATE_WINDOW" holds.append(code); return False, None, None # ALWAYS enforce compact metabolite-fraction f_BMT = BMT/(parent+BMT) in [0, 1] composition coverage # (NEVER skipped under parent BLQ! When parent is BLQ, compatible fraction range extends to f_obs_max = 1.0 # and MUST be densely covered across high-finite intermediate mixtures with max_f_gap <= 0.25 up to 1.0!). if observed_bmt_to_parent_ratio_max > 0.0 or not unbounded_ratio_under_parent_blq: if not (float(r_lo) - 1e-9 <= observed_bmt_to_parent_ratio_min and observed_bmt_to_parent_ratio_max <= float(r_hi) + 1e-9): fails.append(f"FAIL_{prefix}_MIXTURE_REQUAL_RATIO_DOES_NOT_COVER_OBSERVED_BMT_PARENT"); return False, None, None f_obs_min = 0.0 if observed_bmt_to_parent_ratio_min <= 0.0 else (observed_bmt_to_parent_ratio_min / (1.0 + observed_bmt_to_parent_ratio_min)) f_obs_max = 1.0 if unbounded_ratio_under_parent_blq else (0.0 if observed_bmt_to_parent_ratio_max <= 0.0 else (observed_bmt_to_parent_ratio_max / (1.0 + observed_bmt_to_parent_ratio_max))) mdr = mix_ev.get("mixture_dose_response_evidence") mdr_req = frozenset({ "analysis_method", "tested_product_id", "tested_bmt_to_parent_ratio_min", "tested_bmt_to_parent_ratio_max", "tested_bmt_fraction_min", "tested_bmt_fraction_max", "composition_interpolation_rule", "mixture_composition_curves", "evidence_sha256", }) if mdr is None: holds.append(f"HOLD_{prefix}_MISSING_MIXTURE_DOSE_RESPONSE_EVIDENCE"); return False, None, None if ( not isinstance(mdr, Mapping) or set(mdr.keys()) != mdr_req or mdr.get("analysis_method") != "SIMULTANEOUS_BONFERRONI_GRID_BRACKET_STUDENT_T" or (expected_scope == "exact_product" and mdr.get("tested_product_id") != expected_prod) or (expected_scope != "exact_product" and not is_nonempty_str(mdr.get("tested_product_id"))) or not is_valid_sha256_hex(mdr.get("evidence_sha256")) or sha256_bytes(canonical_json_bytes({k: v for k, v in mdr.items() if k != "evidence_sha256"})) != str(mdr["evidence_sha256"]) ): fails.append(f"FAIL_{prefix}_INVALID_MIXTURE_DOSE_RESPONSE_EVIDENCE"); return False, None, None mr_lo, mr_hi = mdr.get("tested_bmt_to_parent_ratio_min"), mdr.get("tested_bmt_to_parent_ratio_max") mf_lo, mf_hi = mdr.get("tested_bmt_fraction_min"), mdr.get("tested_bmt_fraction_max") interp_rule = mdr.get("composition_interpolation_rule") curves = mdr.get("mixture_composition_curves") curve_req = frozenset({ "measured_bmt_fraction", "measured_bmt_to_parent_ratio", "tested_free_combined_conc_grid_nM", "mixture_safety_replicates_by_conc", "mixture_efficacy_replicates_by_conc", }) min_curves = 2 if (float(r_hi) > float(r_lo) + 1e-9 or unbounded_ratio_under_parent_blq) else 1 if not ( is_finite_num(mr_lo) and is_finite_num(mr_hi) and is_finite_num(mf_lo) and is_finite_num(mf_hi) and 0.0 <= float(mf_lo) <= float(mf_hi) <= 1.0 and abs(float(mr_lo) - float(r_lo)) <= 1e-9 and abs(float(mr_hi) - float(r_hi)) <= 1e-9