if rec_promo_auth is not True: fail_reasons.append(f"UPSTREAM_{tag}_PROMOTION_AUTHORIZATION_WITHHELD") if rec_bank_qualified is not True: fail_reasons.append(f"UPSTREAM_{tag}_BANK_NOT_QUALIFIED") elif rec_tier == "SYNTHETIC_FIXTURE": if rec_authority_scope != "SYNTHETIC_SELF_TEST": fail_reasons.append(f"UPSTREAM_{tag}_SYNTHETIC_AUTHORITY_SCOPE_MISMATCH") if rec_promo_auth is not False: fail_reasons.append(f"UPSTREAM_{tag}_SYNTHETIC_CANNOT_AUTHORIZE_PROMOTION") if expected_gate_source == "GATE-0V": if rec_decision in GATE0V_BANK_ONLY_DECISIONS: fail_reasons.append("UPSTREAM_GATE0V_BANK_ONLY_DECISION_REJECTED_FOR_CLINICAL_PROMOTION") fail_reasons.append("UPSTREAM_GATE0V_UNAPPROVED_DECISION") else: if expected_bank_origin == "autologous_hiPSC": wet_allowed = GATE0V_AUTOLOGOUS_CLINICAL_DECISIONS opp_decisions = GATE0V_ALLOGENEIC_CLINICAL_DECISIONS elif expected_bank_origin in ("allogeneic_hiPSC", "allogeneic_EnSC"): wet_allowed = GATE0V_ALLOGENEIC_CLINICAL_DECISIONS opp_decisions = GATE0V_AUTOLOGOUS_CLINICAL_DECISIONS else: wet_allowed = ALLOWED_UPSTREAM_WETLAB_DECISIONS["GATE-0V"] opp_decisions = frozenset() if rec_decision in opp_decisions: fail_reasons.append("UPSTREAM_GATE0V_ORIGIN_DECISION_SCOPE_MISMATCH") fail_reasons.append("UPSTREAM_GATE0V_UNAPPROVED_DECISION") else: allowed_dec = wet_allowed if rec_tier == "AUTHENTICATED_ARCHIVED_WETLAB" else (wet_allowed | {"SYNTHETIC_SELF_TEST_PASS"}) if rec_decision not in allowed_dec: fail_reasons.append("UPSTREAM_GATE0V_UNAPPROVED_DECISION") else: allowed_dec = ( ALLOWED_UPSTREAM_WETLAB_DECISIONS[expected_gate_source] if rec_tier == "AUTHENTICATED_ARCHIVED_WETLAB" else ALLOWED_UPSTREAM_SYNTHETIC_DECISIONS[expected_gate_source] ) if rec_decision not in allowed_dec: fail_reasons.append(f"UPSTREAM_{tag}_UNAPPROVED_DECISION") gate_specific_keys: List[str] = [] valid_prereqs: Optional[Dict[str, str]] = None recomputed_g5_u95: Optional[float] = None if expected_gate_source == "GATE-34": gate_specific_keys.append("dual_switch_lot_qualified") ds_flag = rec.get("dual_switch_lot_qualified") if ds_flag is None: hold_reasons.append("MISSING_UPSTREAM_GATE34_DUAL_SWITCH_LOT_QUALIFIED") elif not _is_strict_bool(ds_flag): fail_reasons.append("INVALID_UPSTREAM_GATE34_DUAL_SWITCH_LOT_QUALIFIED") elif expected_dual_switch_required is not None and ds_flag != expected_dual_switch_required: fail_reasons.append("UPSTREAM_GATE34_DUAL_SWITCH_MISMATCH") prereqs = rec.get("prerequisite_archive_sha256s") if prereqs is None: if rec_tier == "AUTHENTICATED_ARCHIVED_WETLAB" or rec.get("schema_revision") == UPSTREAM_RECEIPT_SCHEMA_REVISION: fail_reasons.append("UPSTREAM_GATE34_MISSING_PREREQUISITE_ARCHIVE_SHA256S") else: gate_specific_keys.append("prerequisite_archive_sha256s") if not isinstance(prereqs, dict): fail_reasons.append("UPSTREAM_GATE34_INVALID_PREREQUISITE_ARCHIVE_SHA256S") else: p_g0v = prereqs.get("GATE-0V") p_g5 = prereqs.get("GATE-5") if not _is_non_all_zero_sha256_hex(p_g0v) or not _is_non_all_zero_sha256_hex(p_g5): fail_reasons.append("UPSTREAM_GATE34_INVALID_PREREQUISITE_ARCHIVE_SHA256S") else: valid_prereqs = {"GATE-0V": p_g0v, "GATE-5": p_g5} elif expected_gate_source == "GATE-0V": gate_specific_keys.extend(["lineages_verified", "whole_graft_stressed_tested"]) lin = rec.get("lineages_verified") if lin is None: hold_reasons.append("MISSING_UPSTREAM_GATE0V_LINEAGES_VERIFIED") elif ( not isinstance(lin, list) or not all(_is_literal_nonempty_str(x) for x in lin) or {x.lower() for x in lin} != REQUIRED_GY030_LINEAGES ): fail_reasons.append("GY030_INCOMPLETE_5LINEAGE_WHOLE_GRAFT_PANEL") stressed = rec.get("whole_graft_stressed_tested") if stressed is None: hold_reasons.append("MISSING_UPSTREAM_GATE0V_WHOLE_GRAFT_STRESSED_BOOL") elif not _is_strict_bool(stressed) or not stressed: fail_reasons.append("GY030_WHOLE_GRAFT_STRESSED_TEST_REQUIRED") elif expected_gate_source == "GATE-5": gate_specific_keys.extend([ "oq012_clonogenic_regrowth_pass", "survivor_count_k", "tested_cells_n", "clonogenic_fraction_f", "spike_in_detection_efficiency_eta", "effective_clonogenic_trials_n_eff", "u95_exact", ]) recomputed_g5_u95 = _validate_gate5_counts_and_u95(rec, fail_reasons=fail_reasons, hold_reasons=hold_reasons) stored_dict: Optional[Dict[str, Any]] = None if rec_tier == "AUTHENTICATED_ARCHIVED_WETLAB": if trusted_evidence_store is None: hold_reasons.append(f"MISSING_TRUSTED_EVIDENCE_STORE_FOR_{tag}") elif not isinstance(trusted_evidence_store, Mapping): fail_reasons.append("INVALID_TRUSTED_EVIDENCE_STORE") else: stored_dict = _resolve_store_entry_with_alias_consistency( rec=rec, record_id=record_id, rec_archive_sha=rec_archive_sha, prefix=f"UPSTREAM_{tag}", trusted_evidence_store=trusted_evidence_store, fail_reasons=fail_reasons, ) if stored_dict is not None: _check_no_contradictory_authority_flags(stored_dict, f"UPSTREAM_{tag}_TRUSTED_STORE", fail_reasons) for field in REQUIRED_UPSTREAM_SCHEMA_V2_ALL_FIELDS: s_val = stored_dict.get(field) r_val = rec.get(field) if type(s_val) is not type(r_val) or s_val != r_val: fail_reasons.append(f"UPSTREAM_{tag}_TRUSTED_STORE_FIELD_MISMATCH:{field}") for gfield in gate_specific_keys: s_val = stored_dict.get(gfield) r_val = rec.get(gfield) if gfield in ("clonogenic_fraction_f", "spike_in_detection_efficiency_eta", "effective_clonogenic_trials_n_eff"): if (gfield in stored_dict) != (gfield in rec) or not _strict_json_equal(s_val, r_val): fail_reasons.append(f"UPSTREAM_{tag}_TRUSTED_STORE_GATE_FIELD_MISMATCH:{gfield}") elif gfield not in stored_dict or not _strict_json_equal(s_val, r_val): fail_reasons.append(f"UPSTREAM_{tag}_TRUSTED_STORE_GATE_FIELD_MISMATCH:{gfield}") if stored_dict.get("evidence_tier") != "AUTHENTICATED_ARCHIVED_WETLAB": fail_reasons.append(f"UPSTREAM_{tag}_TRUSTED_STORE_NOT_WETLAB_TIER") if stored_dict.get("authority_scope") != "CLINICAL_LOT_RELEASE": fail_reasons.append(f"UPSTREAM_{tag}_TRUSTED_STORE_AUTHORITY_SCOPE_MISMATCH") if stored_dict.get("promotion_authorized") is not True: fail_reasons.append(f"UPSTREAM_{tag}_TRUSTED_STORE_PROMOTION_WITHHELD") if stored_dict.get("bank_qualified") is not True: fail_reasons.append(f"UPSTREAM_{tag}_TRUSTED_STORE_BANK_NOT_QUALIFIED") if expected_gate_source == "GATE-0V": s_lin = stored_dict.get("lineages_verified") if ( not isinstance(s_lin, list) or not all(_is_literal_nonempty_str(x) for x in s_lin) or {x.lower() for x in s_lin} != REQUIRED_GY030_LINEAGES ): fail_reasons.append("UPSTREAM_GATE0V_TRUSTED_STORE_LINEAGES_MISMATCH") if stored_dict.get("whole_graft_stressed_tested") is not True: fail_reasons.append("UPSTREAM_GATE0V_TRUSTED_STORE_STRESSED_MISMATCH") elif expected_gate_source == "GATE-5": if stored_dict.get("oq012_clonogenic_regrowth_pass") is not True: fail_reasons.append("UPSTREAM_GATE5_TRUSTED_STORE_OQ012_MISMATCH") s_u95 = _validate_gate5_counts_and_u95(stored_dict, fail_reasons=fail_reasons, hold_reasons=hold_reasons) if s_u95 is not None: recomputed_g5_u95 = s_u95 elif expected_gate_source == "GATE-34": if stored_dict.get("gate_source") == "GATE-32": fail_reasons.append("UPSTREAM_GATE34_LIVE_GATE32_ROLE_COLLISION_REJECTED") s_prereqs = stored_dict.get("prerequisite_archive_sha256s") if not isinstance(s_prereqs, dict) or not _is_non_all_zero_sha256_hex(s_prereqs.get("GATE-0V")) or not _is_non_all_zero_sha256_hex(s_prereqs.get("GATE-5")): fail_reasons.append("UPSTREAM_GATE34_TRUSTED_STORE_INVALID_PREREQUISITE_ARCHIVE_SHA256S") else: valid_prereqs = {"GATE-0V": s_prereqs["GATE-0V"], "GATE-5": s_prereqs["GATE-5"]} return { "construct_sha": valid_construct_sha, "bank_id": rec_bank_id, "record_id": record_id, "archive_sha256": rec_archive_sha, "prerequisite_archive_sha256s": valid_prereqs, "recomputed_gate5_u95": recomputed_g5_u95, "stored": stored_dict, } def _validate_current_assay_record( cur_rec: Any, *, is_promo: bool, expected_product_id: Optional[str], expected_lot_id: Optional[str], expected_bank_id: Optional[str], expected_bank_origin: Optional[str], expected_evidence_tier: Optional[str], expected_construct_sha: Optional[str], upstream_record_ids: Sequence[Optional[str]], upstream_archive_shas: Sequence[Optional[str]], trusted_evidence_store: Optional[Mapping[str, Mapping[str, Any]]], fail_reasons: List[str], hold_reasons: List[str], ) -> Tuple[Optional[Dict[str, Any]], Optional[str]]: """Validate mandatory GATE-35 current_assay_evidence_record in trusted_evidence_store (rejecting colliding live GATE-33).""" if cur_rec is None: fail_reasons.append("MISSING_CURRENT_ASSAY_EVIDENCE_RECORD") return None, None if not isinstance(cur_rec, dict): fail_reasons.append("INVALID_CURRENT_ASSAY_EVIDENCE_RECORD") return None, None _check_no_contradictory_authority_flags(cur_rec, "CURRENT_ASSAY_GATE35", fail_reasons) missing_or_invalid = False for field in REQUIRED_UPSTREAM_SCHEMA_V2_STRING_FIELDS: val = cur_rec.get(field) if val is None: fail_reasons.append(f"MISSING_CURRENT_ASSAY_GATE35_FIELD:{field}") missing_or_invalid = True elif not _is_literal_nonempty_str(val): fail_reasons.append(f"INVALID_CURRENT_ASSAY_GATE35_FIELD:{field}") missing_or_invalid = True for field in REQUIRED_UPSTREAM_SCHEMA_V2_BOOL_FIELDS: val = cur_rec.get(field) if val is None: fail_reasons.append(f"MISSING_CURRENT_ASSAY_GATE35_FIELD:{field}") missing_or_invalid = True elif not _is_strict_bool(val): fail_reasons.append(f"INVALID_CURRENT_ASSAY_GATE35_FIELD:{field}") missing_or_invalid = True if missing_or_invalid: return dict(cur_rec), None record_id = cur_rec["record_id"] gate_source = cur_rec["gate_source"] rec_prod = cur_rec["product_id"] rec_lot = cur_rec["lot_id"] rec_bank_id = cur_rec["bank_id"] rec_bank_origin = cur_rec["bank_origin"] rec_arm_manifest_sha = cur_rec["canonical_arm_manifest_sha256"] rec_construct_sha = cur_rec["construct_manifest_sha256"] rec_tier = cur_rec["evidence_tier"] rec_decision = cur_rec["decision"] rec_authority_scope = cur_rec["authority_scope"] rec_bank_qualified = cur_rec["bank_qualified"] rec_promo_auth = cur_rec["promotion_authorized"] rec_archive_sha = cur_rec["archive_sha256"] if gate_source != "GATE-35": if gate_source == "GATE-33": fail_reasons.append("CURRENT_ASSAY_GATE35_LIVE_GATE33_ROLE_COLLISION_REJECTED") fail_reasons.append("CURRENT_ASSAY_GATE35_GATE_SOURCE_MISMATCH") if expected_product_id is not None and rec_prod != expected_product_id: fail_reasons.append("CURRENT_ASSAY_GATE35_PRODUCT_ID_MISMATCH") if expected_lot_id is not None and rec_lot != expected_lot_id: fail_reasons.append("CURRENT_ASSAY_GATE35_LOT_ID_MISMATCH") if expected_bank_id is not None and rec_bank_id != expected_bank_id: fail_reasons.append("CURRENT_ASSAY_GATE35_BANK_ID_MISMATCH") if rec_bank_origin not in ALLOWED_BANK_ORIGINS: fail_reasons.append("CURRENT_ASSAY_GATE35_INVALID_BANK_ORIGIN") elif expected_bank_origin is not None and rec_bank_origin != expected_bank_origin: fail_reasons.append("CURRENT_ASSAY_GATE35_BANK_ORIGIN_MISMATCH")