skip_missing_in_a: bool = False, skip_missing_in_b: bool = False, ) -> bool: """Compare normalized GP2 per-animal tuples (raw_id, lot_id, challenge_dose_cells, followup_weeks, teratoma_detected) order-insensitively by canonical animal ID (Audit R10-06 / R10-07 / R11-06). When skip_missing_in_a=True and/or skip_missing_in_b=True, absent (None) fields in that representation are NOT treated as a contradiction against a present value in the other representation (preserving governed HOLD for missing evidence), while ALL overlapping present values & canonical/raw IDs are strictly reconciled so any genuine contradiction between present values FAILS closed.""" ids_a = set(rec_a.keys()) ids_b = set(rec_b.keys()) matched = True if ids_a != ids_b: matched = False if mismatch_code and mismatch_code not in failures: failures.append(mismatch_code) if alias_conflict_code and alias_conflict_code not in failures: failures.append(alias_conflict_code) raw_diff_found = False field_diff_found = False for cid in sorted(ids_a & ids_b): ra = rec_a[cid] rb = rec_b[cid] if ra["raw_id"] != rb["raw_id"]: raw_diff_found = True la, lb = ra["lot_id"], rb["lot_id"] if (skip_missing_in_a and la is None) or (skip_missing_in_b and lb is None): lot_diff = False else: lot_diff = la != lb da, db = ra["challenge_dose_cells"], rb["challenge_dose_cells"] if (skip_missing_in_a and da is None) or (skip_missing_in_b and db is None): dose_diff = False else: dose_diff = ((da is None) != (db is None)) or (da is not None and db is not None and abs(da - db) > 1e-12) wa, wb = ra["followup_weeks"], rb["followup_weeks"] if (skip_missing_in_a and wa is None) or (skip_missing_in_b and wb is None): wk_diff = False else: wk_diff = ((wa is None) != (wb is None)) or (wa is not None and wb is not None and abs(wa - wb) > 1e-12) oa, ob = ra["teratoma_detected"], rb["teratoma_detected"] if (skip_missing_in_a and oa is None) or (skip_missing_in_b and ob is None): out_diff = False else: out_diff = oa != ob if lot_diff or dose_diff or wk_diff or out_diff: field_diff_found = True if raw_diff_found: matched = False if "FAIL_NONUNIQUE_CANONICAL_EQUIVALENCE_ID" not in failures: failures.append("FAIL_NONUNIQUE_CANONICAL_EQUIVALENCE_ID") if mismatch_code and mismatch_code not in failures: failures.append(mismatch_code) if alias_conflict_code and alias_conflict_code not in failures: failures.append(alias_conflict_code) if field_diff_found: matched = False if mismatch_code and mismatch_code not in failures: failures.append(mismatch_code) if alias_conflict_code and alias_conflict_code not in failures: failures.append(alias_conflict_code) return matched _gp2_extra_roster_aliases: tuple[str, ...] = ("per_animal_teratoma_outcomes", "teratoma_animal_outcomes") min_animal_dose: Optional[int] = None obs_events = 0 parent_unk = 0 parent_distinct_animals: Optional[int] = None parent_animal_records: dict[str, dict[str, Any]] = {} parent_id_set: Optional[set[str]] = None parent_raw_map: dict[str, str] = {} parent_roster_integrity_fail = False parent_roster_incomplete = False caller_gp2_extra_rosters: list[tuple[str, dict[str, dict[str, Any]], set[str], dict[str, str]]] = [] caller_gp2_id_lists: list[tuple[str, int, set[str], dict[str, str]]] = [] caller_gp2_id_list_fail = False if per_animals is None: holds.append("MISSING_per_animal_exposures") parent_roster_incomplete = True else: ( min_animal_dose, obs_events, parent_unk, parent_distinct_animals, parent_animal_records, parent_id_set, parent_raw_map, parent_roster_integrity_fail, parent_roster_incomplete, ) = _audit_gp2_animal_list( per_animals, expected_n=t_n, expected_ev=t_ev, expected_lot=valid_lot, prefix="", ) for rk in _gp2_extra_roster_aliases: if rk in payload: if payload[rk] is None: failures.append("FAIL_GP2_CALLER_ROSTER_ALIAS_NULL") caller_gp2_id_list_fail = True continue _, _, rk_unk, _, rk_recs, rk_ids, rk_raw, rk_int_fail, rk_miss = _audit_gp2_animal_list( payload[rk], expected_n=t_n, expected_ev=t_ev, expected_lot=valid_lot, prefix="CALLER_ALIAS_", ) caller_gp2_extra_rosters.append((rk, rk_recs, rk_ids, rk_raw)) if rk_int_fail or rk_miss or rk_unk > 0: caller_gp2_id_list_fail = True if parent_animal_records: if not _reconcile_gp2_animal_records( parent_animal_records, rk_recs, mismatch_code="FAIL_GP2_CALLER_ROSTER_ALIAS_CONFLICT", alias_conflict_code="FAIL_GP2_CALLER_ROSTER_ALIAS_CONFLICT", skip_missing_in_a=True, skip_missing_in_b=True, ): caller_gp2_id_list_fail = True if len(caller_gp2_extra_rosters) >= 2: for i_ce in range(len(caller_gp2_extra_rosters)): for j_ce in range(i_ce + 1, len(caller_gp2_extra_rosters)): if not _reconcile_gp2_animal_records( caller_gp2_extra_rosters[i_ce][1], caller_gp2_extra_rosters[j_ce][1], mismatch_code="FAIL_GP2_CALLER_ROSTER_ALIAS_CONFLICT", alias_conflict_code="FAIL_GP2_CALLER_ROSTER_ALIAS_CONFLICT", skip_missing_in_a=True, skip_missing_in_b=True, ): caller_gp2_id_list_fail = True # Audit any optional caller ID-list aliases under strict R7-04/R8-07 grammar & require exact identity + 1-to-1 raw match for opt_id_k in ("animal_ids", "teratoma_animal_ids"): if opt_id_k in payload: if payload[opt_id_k] is None: failures.append("FAIL_GP2_CALLER_ANIMAL_ID_ALIAS_NULL") caller_gp2_id_list_fail = True continue cid_fails: list[str] = [] opt_n, opt_set, opt_raw = _audit_canonical_id_list( payload[opt_id_k], min_distinct=12, expected_count=t_n, label_prefix="CALLER_GP2_ANIMAL", failures=cid_fails, ) for cf in cid_fails: if cf == "FAIL_NONUNIQUE_CANONICAL_EQUIVALENCE_ID" and cf in failures: continue failures.append(cf) if cid_fails: caller_gp2_id_list_fail = True if opt_set is not None: caller_gp2_id_lists.append((opt_id_k, opt_n, opt_set, opt_raw)) if parent_id_set is not None and opt_set is not None: if opt_set != parent_id_set: failures.append("FAIL_GP2_CALLER_ANIMAL_ID_ALIAS_CONFLICT") caller_gp2_id_list_fail = True elif any(opt_raw.get(cid) != parent_raw_map.get(cid) for cid in opt_set): if "FAIL_NONUNIQUE_CANONICAL_EQUIVALENCE_ID" not in failures: failures.append("FAIL_NONUNIQUE_CANONICAL_EQUIVALENCE_ID") failures.append("FAIL_GP2_CALLER_ANIMAL_ID_ALIAS_CONFLICT") caller_gp2_id_list_fail = True if len(caller_gp2_id_lists) >= 2: ref_cg_ids, ref_cg_raw = caller_gp2_id_lists[0][2], caller_gp2_id_lists[0][3] for _, _, cg_ids, cg_raw in caller_gp2_id_lists[1:]: if cg_ids != ref_cg_ids: failures.append("FAIL_GP2_CALLER_ANIMAL_ID_ALIAS_CONFLICT") caller_gp2_id_list_fail = True elif any(cg_raw.get(cid) != ref_cg_raw.get(cid) for cid in ref_cg_ids): if "FAIL_NONUNIQUE_CANONICAL_EQUIVALENCE_ID" not in failures: failures.append("FAIL_NONUNIQUE_CANONICAL_EQUIVALENCE_ID") failures.append("FAIL_GP2_CALLER_ANIMAL_ID_ALIAS_CONFLICT") caller_gp2_id_list_fail = True all_caller_gp2_rosters: list[dict[str, dict[str, Any]]] = ( ([parent_animal_records] if parent_animal_records else []) + [ce[1] for ce in caller_gp2_extra_rosters] ) if _reconcile_study_followup_against_animal_floor( study_res=caller_wk_res, animal_rosters=all_caller_gp2_rosters, role="CALLER", gate_tag="GP2", failures=failures, ): caller_gp2_id_list_fail = True # Compute provisional aggregate-only U95 estimate (strictly isolated unless verified archived per_animal_exposures is complete) provisional_gp2_agg_u95_pct: Optional[float] = None gp2_u95_derivation_source: str = "UNVERIFIED_AGGREGATE_HOLD_ONLY" gp2_caller_reconciliation_status: str = "NO_AUTHENTICATED_ARCHIVE" gp2_caller_reported_unverified_u95_pct: Optional[float] = None gp2_unverified_archive_dedup_animals: Optional[int] = None gp2_unverified_archive_observed_events: Optional[int] = None verified_gp2_distinct_animals: Optional[int] = None verified_gp2_min_dose: Optional[int] = None if _is_strict_int(t_ev) and _is_strict_int(t_n) and 0 < t_n <= MAX_CLONOGENIC_CELLS and t_ev >= 0 and t_ev <= t_n: provisional_gp2_agg_u95_pct = _safe_round_finite(clopper_pearson_u95(t_ev, t_n) * 100.0, 4) arch_gp2 = _verify_upstream_evidence_record( payload.get("lot_evidence_record"), "lot_evidence_record", expected_gate_source="GATE-37-GP2", expected_product_id=valid_product, expected_lot_id=valid_lot, expected_bank_id=valid_bank, expected_bank_origin=valid_origin, expected_construct_manifest_sha256=valid_construct_sha, parent_evidence_tier=valid_tier, allowed_wetlab_decisions=frozenset({"PASS_GP2_INTERMEDIATE_BANK_QUALIFIED", "PASS_GP2_MCB_QUALIFICATION", "PASS"}), allowed_wetlab_scopes=frozenset({"BANK_QUALIFICATION_ONLY"}), trusted_evidence_store=trusted_evidence_store, holds=holds, failures=failures, ) arch_calc_input_gp2_pct: Optional[float] = None arch_calc_cond_recovery_pct: Optional[float] = None arch_calc_gpi_pct: Optional[float] = None arch_calc_purity_pct: Optional[float] = None arch_min_animal_dose: Optional[int] = None arch_distinct_animals: Optional[int] = None caller_teratoma_conflict = False if arch_gp2 is not None: if valid_tier == "AUTHENTICATED_ARCHIVED_WETLAB": if arch_gp2.get("bank_qualified") is not True: failures.append("FAIL_GP2_ARCHIVE_NOT_BANK_QUALIFIED") if arch_gp2.get("promotion_authorized") is not False: failures.append("FAIL_GP2_ARCHIVE_MUST_NOT_AUTHORIZE_CLINICAL_PROMOTION") # Audit R5-02 (P0): Require ALL governing GP2 release measurements inside the verified GATE-35-GP2 # archive, derive ALL threshold decisions strictly from archived measurements & counts/denominators, # reject ANY parent-vs-archive mismatch (1e-12 exact tolerance), and HOLD on any missing archived field! def _audit_arch_gp2_pct( key: str, parent_val: Optional[float], *, min_pass: Optional[float] = None, max_pass: Optional[float] = None, fail_code: str, tag: str, ) -> Optional[float]: raw = arch_gp2.get(key) if raw is None: holds.append(f"HOLD_GP2_ARCHIVE_MISSING_{tag}") return None if not _is_num(raw): failures.append(f"FAIL_GP2_ARCHIVE_INVALID_{tag}") return None