if expected_product_variant_id is not None and pv_leg_res["product_variant_id"] != expected_product_variant_id: _add_hold("HOLD_COMPOSITE_TOP_PRODUCT_VARIANT_ALIAS_MISMATCH") if not (_is_valid_sha256_hex(top_sha) and top_sha == computed_comp_sha): _add_hold("HOLD_COMPOSITE_PAYLOAD_SHA256_MISMATCH") legacy_obs = composite_doc.get("legacy_stage0_observations") gate0v_obs = composite_doc.get("gate0v_wholegraft_payload") if not isinstance(legacy_obs, dict): _add_hold("HOLD_COMPOSITE_MISSING_LEGACY_STAGE0_OBSERVATIONS") if not isinstance(gate0v_obs, dict): _add_hold("HOLD_COMPOSITE_MISSING_GATE0V_WHOLEGRAFT_PAYLOAD") leg_pvid_canon: str | None = None if isinstance(legacy_obs, dict): leg_lot_keys_found: List[Tuple[str, Any]] = [] for lk in ("current_lot_id", "lot_id", "batch_id"): if lk in legacy_obs and legacy_obs.get(lk) is not None: leg_lot_keys_found.append((lk, legacy_obs.get(lk))) if ( not leg_lot_keys_found or any(val != top_lot for _, val in leg_lot_keys_found) or (expected_lot_id is not None and any(val != expected_lot_id for _, val in leg_lot_keys_found)) ): _add_hold("HOLD_COMPOSITE_LEGACY_LOT_ID_MISMATCH") if legacy_obs.get("mcb_id") != top_mcb or (expected_mcb_id is not None and legacy_obs.get("mcb_id") != expected_mcb_id): _add_hold("HOLD_COMPOSITE_LEGACY_MCB_ID_MISMATCH") leg_pvid_raw = legacy_obs.get("product_variant_id") if not _is_clean_nonempty_str(leg_pvid_raw): _add_hold("HOLD_COMPOSITE_LEGACY_MISSING_PRODUCT_VARIANT_ID") else: leg_pvid_res = resolve_canonical_product_variant(leg_pvid_raw, openislet_root=openislet_root) if leg_pvid_res is None: _add_hold("HOLD_COMPOSITE_LEGACY_UNREGISTERED_PRODUCT_VARIANT_ID") else: leg_pvid_canon = leg_pvid_res["product_variant_id"] if (canonical_pvid is not None and leg_pvid_canon != canonical_pvid) or ( expected_product_variant_id is not None and leg_pvid_canon != expected_product_variant_id ): _add_hold("HOLD_COMPOSITE_LEGACY_PRODUCT_VARIANT_ID_MISMATCH") if "product_variant" in legacy_obs and legacy_obs.get("product_variant") is not None: lpv_res = resolve_canonical_product_variant(legacy_obs.get("product_variant"), openislet_root=openislet_root) if ( lpv_res is None or (canonical_pvid is not None and lpv_res["product_variant_id"] != canonical_pvid) or (expected_product_variant_id is not None and lpv_res["product_variant_id"] != expected_product_variant_id) ): _add_hold("HOLD_COMPOSITE_LEGACY_PRODUCT_VARIANT_ALIAS_MISMATCH") qual_tier: str | None = None g0v_pvid_canon: str | None = None if isinstance(gate0v_obs, dict): qual_tier = gate0v_obs.get("qualification_tier") if isinstance(gate0v_obs.get("qualification_tier"), str) else None if gate0v_obs.get("lot_id") != top_lot or (expected_lot_id is not None and gate0v_obs.get("lot_id") != expected_lot_id): _add_hold("HOLD_COMPOSITE_GATE0V_LOT_ID_MISMATCH") if gate0v_obs.get("mcb_id") != top_mcb or (expected_mcb_id is not None and gate0v_obs.get("mcb_id") != expected_mcb_id): _add_hold("HOLD_COMPOSITE_GATE0V_MCB_ID_MISMATCH") g0v_pvid_raw = gate0v_obs.get("product_variant_id") if not _is_clean_nonempty_str(g0v_pvid_raw): _add_hold("HOLD_COMPOSITE_GATE0V_MISSING_PRODUCT_VARIANT_ID") else: g0v_pvid_res = resolve_canonical_product_variant(g0v_pvid_raw, openislet_root=openislet_root) if g0v_pvid_res is None: _add_hold("HOLD_COMPOSITE_GATE0V_UNREGISTERED_PRODUCT_VARIANT_ID") else: g0v_pvid_canon = g0v_pvid_res["product_variant_id"] if (canonical_pvid is not None and g0v_pvid_canon != canonical_pvid) or ( expected_product_variant_id is not None and g0v_pvid_canon != expected_product_variant_id ): _add_hold("HOLD_COMPOSITE_GATE0V_PRODUCT_VARIANT_ID_MISMATCH") if "product_variant" in gate0v_obs and gate0v_obs.get("product_variant") is not None: gpv_res = resolve_canonical_product_variant(gate0v_obs.get("product_variant"), openislet_root=openislet_root) if ( gpv_res is None or (canonical_pvid is not None and gpv_res["product_variant_id"] != canonical_pvid) or (expected_product_variant_id is not None and gpv_res["product_variant_id"] != expected_product_variant_id) ): _add_hold("HOLD_COMPOSITE_GATE0V_PRODUCT_VARIANT_ALIAS_MISMATCH") if not (isinstance(qual_tier, str) and qual_tier in ALLOWED_QUALIFICATION_TIERS): _add_hold("HOLD_COMPOSITE_MISSING_OR_INVALID_QUALIFICATION_TIER") # R08-B1: Exhaustively reconcile EVERY supplied tier field across top-level, Gate-0V, and legacy # sections independently (NEVER short-circuit `qualification_tier or operational_tier`!). target_tier = expected_tier if expected_tier is not None else qual_tier if target_tier is not None: if expected_tier is not None and qual_tier != expected_tier: _add_hold("HOLD_COMPOSITE_OPERATIONAL_TIER_MISMATCH") if "operational_tier" in composite_doc and composite_doc.get("operational_tier") != target_tier: _add_hold("HOLD_COMPOSITE_OPERATIONAL_TIER_MISMATCH") if "qualification_tier" in composite_doc and composite_doc.get("qualification_tier") != target_tier: _add_hold("HOLD_COMPOSITE_OPERATIONAL_TIER_MISMATCH") if isinstance(gate0v_obs, dict) and "operational_tier" in gate0v_obs and gate0v_obs.get("operational_tier") != target_tier: _add_hold("HOLD_COMPOSITE_OPERATIONAL_TIER_MISMATCH") if isinstance(legacy_obs, dict): if "operational_tier" in legacy_obs and legacy_obs.get("operational_tier") != target_tier: _add_hold("HOLD_COMPOSITE_OPERATIONAL_TIER_MISMATCH") if "qualification_tier" in legacy_obs and legacy_obs.get("qualification_tier") != target_tier: _add_hold("HOLD_COMPOSITE_OPERATIONAL_TIER_MISMATCH") # Reconcile optional top-level antiviral_genotype_stratum against nested gate0v_wholegraft_payload if ( "antiviral_genotype_stratum" in composite_doc and isinstance(gate0v_obs, dict) and composite_doc.get("antiviral_genotype_stratum") != gate0v_obs.get("antiviral_genotype_stratum") ): _add_hold("HOLD_UNVERIFIED_OR_MISMATCHED_BANK_GENOTYPE_STRATUM") # R10-B1: Extract & reconcile any lot-declared planned NW-08 geometry route & evidence constraints # across top-level composite envelope and nested gate0v_wholegraft_payload (ZERO unhashable lookups!). declared_routes: List[str] = [] planned_route_errors: List[str] = [] for container in (composite_doc, gate0v_obs if isinstance(gate0v_obs, dict) else None): if container is None: continue for rk in _LOT_PLANNED_ROUTE_KEYS: if rk in container and container.get(rk) is not None: rv = container.get(rk) if not (_is_clean_nonempty_str(rv) and rv in CANONICAL_NW08_ROUTE_IDS): if "HOLD_NW08_COUPLED_INVALID_LOT_PLANNED_ROUTE" not in planned_route_errors: planned_route_errors.append("HOLD_NW08_COUPLED_INVALID_LOT_PLANNED_ROUTE") _add_hold("HOLD_NW08_COUPLED_INVALID_LOT_PLANNED_ROUTE") else: declared_routes.append(rv) planned_nw08_route_id: str | None = None if declared_routes: if len(set(declared_routes)) > 1: if "HOLD_NW08_COUPLED_CONFLICTING_LOT_PLANNED_ROUTE" not in planned_route_errors: planned_route_errors.append("HOLD_NW08_COUPLED_CONFLICTING_LOT_PLANNED_ROUTE") _add_hold("HOLD_NW08_COUPLED_CONFLICTING_LOT_PLANNED_ROUTE") elif not planned_route_errors: planned_nw08_route_id = declared_routes[0] declared_ev_shas: List[str] = [] for container in (composite_doc, gate0v_obs if isinstance(gate0v_obs, dict) else None): if container is None: continue if "nw08_measurement_evidence_sha256" in container and container.get("nw08_measurement_evidence_sha256") is not None: ev_v = container.get("nw08_measurement_evidence_sha256") if not _is_valid_sha256_hex(ev_v): if "HOLD_NW08_COUPLED_INVALID_LOT_MEASUREMENT_EVIDENCE_SHA256" not in planned_route_errors: planned_route_errors.append("HOLD_NW08_COUPLED_INVALID_LOT_MEASUREMENT_EVIDENCE_SHA256") _add_hold("HOLD_NW08_COUPLED_INVALID_LOT_MEASUREMENT_EVIDENCE_SHA256") else: declared_ev_shas.append(ev_v) planned_ev_sha: str | None = None if declared_ev_shas: if len(set(declared_ev_shas)) > 1: if "HOLD_NW08_COUPLED_CONFLICTING_LOT_MEASUREMENT_EVIDENCE_SHA256" not in planned_route_errors: planned_route_errors.append("HOLD_NW08_COUPLED_CONFLICTING_LOT_MEASUREMENT_EVIDENCE_SHA256") _add_hold("HOLD_NW08_COUPLED_CONFLICTING_LOT_MEASUREMENT_EVIDENCE_SHA256") elif "HOLD_NW08_COUPLED_INVALID_LOT_MEASUREMENT_EVIDENCE_SHA256" not in planned_route_errors: planned_ev_sha = declared_ev_shas[0] planned_not_before_utc: str | None = None if "nw08_measurement_not_before_utc" in composite_doc and composite_doc.get("nw08_measurement_not_before_utc") is not None: nb_raw = composite_doc.get("nw08_measurement_not_before_utc") if _parse_strict_utc_timestamp(nb_raw) is None: planned_route_errors.append("HOLD_NW08_COUPLED_INVALID_LOT_NOT_BEFORE_TIMESTAMP_UTC") _add_hold("HOLD_NW08_COUPLED_INVALID_LOT_NOT_BEFORE_TIMESTAMP_UTC") else: planned_not_before_utc = nb_raw # R11-B1: Strictly validate `require_trusted_nw08_ocr_record` as `bool` whenever present in # top-level composite envelope OR nested `gate0v_wholegraft_payload` (non-bool including None/int/str # or conflicting True/False fails closed to HOLD!). req_trusted_ocr = False declared_req_ocr: List[bool] = [] for container in (composite_doc, gate0v_obs if isinstance(gate0v_obs, dict) else None): if container is None: continue if "require_trusted_nw08_ocr_record" in container: rt_raw = container.get("require_trusted_nw08_ocr_record") if not _is_strict_bool(rt_raw): if "HOLD_NW08_COUPLED_INVALID_REQUIRE_TRUSTED_OCR_FLAG" not in planned_route_errors: planned_route_errors.append("HOLD_NW08_COUPLED_INVALID_REQUIRE_TRUSTED_OCR_FLAG") _add_hold("HOLD_NW08_COUPLED_INVALID_REQUIRE_TRUSTED_OCR_FLAG") else: declared_req_ocr.append(bool(rt_raw)) if declared_req_ocr: if len(set(declared_req_ocr)) > 1: if "HOLD_NW08_COUPLED_CONFLICTING_REQUIRE_TRUSTED_OCR_FLAG" not in planned_route_errors: planned_route_errors.append("HOLD_NW08_COUPLED_CONFLICTING_REQUIRE_TRUSTED_OCR_FLAG") _add_hold("HOLD_NW08_COUPLED_CONFLICTING_REQUIRE_TRUSTED_OCR_FLAG") req_trusted_ocr = True elif "HOLD_NW08_COUPLED_INVALID_REQUIRE_TRUSTED_OCR_FLAG" not in planned_route_errors: req_trusted_ocr = declared_req_ocr[0] # R13-B3: Subtractive policy enforced identically at direct admission AND archived replay — # NW-08 measured-OCR release gating applies strictly to clinical release lots # (`LOT_RELEASE_SURROGATE` / `AUTOLOGOUS_D1B_PROCESS_QUAL`), NEVER to `BANK_QUALIFICATION`! if req_trusted_ocr and (qual_tier == "BANK_QUALIFICATION" or expected_tier == "BANK_QUALIFICATION"): if "HOLD_NW08_REQUIRED_OCR_NOT_PERMITTED_ON_BANK_QUALIFICATION" not in planned_route_errors: planned_route_errors.append("HOLD_NW08_REQUIRED_OCR_NOT_PERMITTED_ON_BANK_QUALIFICATION") _add_hold("HOLD_NW08_REQUIRED_OCR_NOT_PERMITTED_ON_BANK_QUALIFICATION") tier_allowed_for_product = False if canonical_product_def is not None and isinstance(qual_tier, str) and qual_tier in ALLOWED_QUALIFICATION_TIERS: tier_allowed_for_product, tier_family_errs = is_qualification_tier_allowed_for_product( canonical_product_def, qual_tier ) for te in tier_family_errs: _add_hold(te) return { "valid": len(holds) == 0, "holds": holds, "top_lot": top_lot, "top_mcb": top_mcb, "top_pvid_raw": top_pvid_raw, "top_sha": top_sha, "computed_comp_sha": computed_comp_sha, "canonical_product_def": canonical_product_def, "canonical_pvid": canonical_pvid, "legacy_obs": legacy_obs, "gate0v_obs": gate0v_obs, "leg_pvid_canon": leg_pvid_canon, "g0v_pvid_canon": g0v_pvid_canon, "qual_tier": qual_tier, "tier_allowed_for_product": tier_allowed_for_product, "planned_nw08_route_id": planned_nw08_route_id, "planned_nw08_route_errors": planned_route_errors, "planned_nw08_measurement_evidence_sha256": planned_ev_sha, "planned_nw08_measurement_not_before_utc": planned_not_before_utc, "require_trusted_nw08_ocr_record": req_trusted_ocr, } def resolve_and_verify_trusted_bank_qualification( trusted_qualification_store: Path | str | None, *, expected_sha256: Any, expected_mcb_id: Any, expected_product_variant_id: Any, expected_genotype_stratum: Any | None = None, inline_record_if_supplied: Any = None, openislet_root: Path | None = None, hold_prefix: str = "HOLD_GATE0V_PRIOR_BANK_QUAL", legacy_evaluator: Any = None, composite_bank_revalidator: Any = None, _revalidating_archived_bank: bool = False, ) -> Tuple[bool, List[str], Dict[str, Any]]: """ Resolve and verify prior MCB BANK_QUALIFICATION approval SOLELY from an independently controlled external trusted qualification store (B1 / R04-B2 / R05-B1 / R06-B4 / R07-B1 / R08-B1). Self-attested records embedded inside the caller's lot bundle are NEVER trusted as proof of prior approval, and a complete, canonical, self-digest-verified, envelope-aligned, nested-identity-reconciled, and live-evaluator-reverified archived original BANK_QUALIFICATION payload is MANDATORY in BOTH store layouts (embedded registry JSON and file-backed directory store):