and target_g0v_payload.get("antiviral_genotype_stratum") in ALLOWED_ANTIVIRAL_GENOTYPES ) if not g0v_evidence_blocks_ok: arch_fail_reasons.append("archived_gate0v_missing_mandatory_bank_evidence_blocks") elif not arch_envelope_ok: # R08-B1: NEVER report positive live revalidation on an archived bank bundle whose # envelope fails canonical validation (prevents direct-vs-archived parity drift!). live_reval_ok = False arch_reval_verdict = "HOLD_ARCHIVED_BANK_ENVELOPE_MISMATCH" arch_fail_reasons.append("archived_bank_live_gate0v_revalidation_blocked_by_envelope_mismatch") else: # R06-B4: Live re-execution of evaluate_wholegraft_gate0v_and_stress_ap1903 on archived bank payload! reval_res = evaluate_wholegraft_gate0v_and_stress_ap1903( target_g0v_payload, trusted_qualification_store=None, openislet_root=openislet_root, _revalidating_archived_bank=True, ) arch_reval_verdict = str(reval_res.get("verdict") or "UNKNOWN") reval_lb = reval_res.get("lot_binding") or {} live_reval_ok = bool( arch_reval_verdict == "PASS_WHOLEGRAFT_BANK_QUALIFICATION" and len(reval_res.get("hard_stops") or []) == 0 and len(reval_res.get("holds") or []) == 0 and reval_lb.get("payload_sha256_verified") is True and reval_lb.get("canonical_product_variant_id") == expected_product_variant_id and reval_lb.get("mcb_id") == expected_mcb_id and reval_lb.get("lot_id") == rec.get("bank_lot_id") ) if not live_reval_ok: arch_fail_reasons.append(f"archived_bank_failed_live_gate0v_revalidation:{arch_reval_verdict}") _add_err(f"{hold_prefix}_ARCHIVED_PAYLOAD_FAILED_LIVE_GATE0V_REVALIDATION") arch_nested_ok = bool(top_identity_ok and arch_envelope_ok and nested_reconcile_ok and arch_geno_ok) legacy_replay_gate_ok = (arch_leg_reval_ok is True) if require_composite_legacy_replay else True comp_replay_gate_ok = (arch_comp_reval_ok is True) if (composite_bank_revalidator is not None and arch_format == "composite_bank_bundle") else ( False if (composite_bank_revalidator is not None) else True ) arch_ok = bool( arch_self_digest_ok and arch_envelope_ok and arch_nested_ok and g0v_evidence_blocks_ok and (live_reval_ok is True) and legacy_replay_gate_ok and comp_replay_gate_ok ) archived_verified = arch_ok if not arch_ok: _add_err("HOLD_MISSING_OR_INVALID_ARCHIVED_BANK_PAYLOAD") _add_err(f"{hold_prefix}_ARCHIVED_BANK_PAYLOAD_VERIFICATION_FAILED") # If caller embedded an inline copy that contradicts the trusted store record, reject! if isinstance(inline_record_if_supplied, dict): inline_sha = _safe_canonical_qualification_artifact_sha256(inline_record_if_supplied) if inline_sha is None or computed_sha is None or inline_sha != computed_sha: _add_err(f"{hold_prefix}_INLINE_RECORD_CONFLICTS_WITH_TRUSTED_STORE") if errs: errs = ["HOLD_UNVERIFIED_EXTERNAL_APPROVAL_STORE"] + [e for e in errs if e != "HOLD_UNVERIFIED_EXTERNAL_APPROVAL_STORE"] all_live_reval_ok = bool( archived_verified is True and arch_envelope_ok is True and (live_reval_ok is True) and ((arch_leg_reval_ok is True) if require_composite_legacy_replay else True) and ((arch_comp_reval_ok is True) if composite_bank_revalidator is not None else True) ) ok = bool(len(errs) == 0 and all_live_reval_ok) return ok, errs, { "authenticated": ok, "resolution_source": store_src, "self_attested_inline_record_ignored": inline_record_if_supplied is not None, "computed_sha256": computed_sha, "approved_by_authority_id": rec.get("approved_by_authority_id"), "record_mcb_id": rec.get("mcb_id"), "record_product_variant_id": rec_pvid_canon, "record_antiviral_genotype_stratum": rec_geno, "archived_antiviral_genotype_stratum": arch_geno, "record_operational_tier": rec.get("operational_tier"), "record_verdict": rec.get("verdict"), "record_legacy_tier": rec.get("legacy_tier"), "record_gate0v_verdict": rec.get("gate0v_verdict"), "record_require_trusted_nw08_ocr_record": rec_req_ocr, "archived_bank_payload_verified": archived_verified, "archived_bank_payload_envelope_verified": arch_envelope_ok, "archived_bank_payload_status": archive_status, "archived_bank_payload_format": arch_format, "archived_bank_payload_self_digest_verified": arch_self_digest_ok, "archived_bank_payload_nested_identities_reconciled": arch_nested_ok, "archived_bank_payload_live_revalidation_passed": all_live_reval_ok, "archived_bank_payload_live_g0v_revalidation_passed": bool(live_reval_ok), "archived_bank_payload_live_gate0v_revalidation_verdict": arch_reval_verdict, "archived_bank_payload_live_legacy_revalidation_required": require_composite_legacy_replay, "archived_bank_payload_live_legacy_revalidation_passed": arch_leg_reval_ok, "archived_bank_payload_live_composite_revalidation_passed": arch_comp_reval_ok, "archived_bank_payload_failure_reasons": arch_fail_reasons, "deployment_prerequisite": ( "Software record-content, self-digest, canonical envelope alignment, and live evaluator " "re-verification require independent QA operator administration, write-restricted storage " "control, and separation of duties over --trusted-qualification-store as deployment prerequisites." ), } def resolve_and_verify_trusted_process_qualification( trusted_qualification_store: Path | str | None, *, expected_sha256: Any, expected_product_variant_id: Any, inline_record_if_supplied: Any = None, openislet_root: Path | None = None, hold_prefix: str = "HOLD_GATE0V_AUTOLOGOUS_PROC_QUAL", ) -> Tuple[bool, List[str], Dict[str, Any]]: """ Resolve and verify prior AUTOLOGOUS_D1B_PROCESS_QUAL approval SOLELY from an independently controlled external trusted qualification store (B1). """ rec, archived_payload, lookup_errs, store_src, archive_status = ( _lookup_in_trusted_qualification_store_with_archive_status( trusted_qualification_store, category="process_qualifications", expected_sha256=expected_sha256, ) ) if lookup_errs or rec is None: return False, lookup_errs, { "authenticated": False, "resolution_source": store_src, "self_attested_inline_record_ignored": inline_record_if_supplied is not None, } errs: List[str] = [] rec_sha = rec.get("sha256") computed_sha = _safe_canonical_qualification_artifact_sha256(rec) if computed_sha is None or not _is_valid_sha256_hex(rec_sha) or rec_sha != computed_sha: errs.append(f"{hold_prefix}_RECORD_INTERNAL_SHA256_MISMATCH") elif rec_sha != expected_sha256: errs.append(f"{hold_prefix}_RECORD_SHA256_DOES_NOT_MATCH_REF_DIGEST") if not _is_valid_identifier(rec.get("approved_by_authority_id")): errs.append(f"{hold_prefix}_MISSING_APPROVED_BY_AUTHORITY_ID") if rec.get("operational_tier") != "AUTOLOGOUS_D1B_PROCESS_QUAL": errs.append(f"{hold_prefix}_WRONG_OPERATIONAL_TIER") if "qualification_tier" in rec and rec.get("qualification_tier") != "AUTOLOGOUS_D1B_PROCESS_QUAL": errs.append(f"{hold_prefix}_WRONG_OPERATIONAL_TIER") if rec.get("verdict") != "PASS_AUTOLOGOUS_D1B_PROCESS_QUALIFICATION": errs.append(f"{hold_prefix}_VERDICT_NOT_PASS") rec_pvid_raw = rec.get("product_variant_id") if not _is_clean_nonempty_str(rec_pvid_raw): errs.append(f"{hold_prefix}_MISSING_RECORD_PRODUCT_VARIANT_ID") rec_pvid_res = ( resolve_canonical_product_variant(rec_pvid_raw, openislet_root=openislet_root) if _is_clean_nonempty_str(rec_pvid_raw) else None ) rec_pvid_canon = rec_pvid_res["product_variant_id"] if rec_pvid_res is not None else rec_pvid_raw if rec_pvid_canon != expected_product_variant_id: errs.append(f"{hold_prefix}_PRODUCT_VARIANT_ID_MISMATCH") if "product_variant" in rec and rec.get("product_variant") is not None: rpv_res = resolve_canonical_product_variant(rec.get("product_variant"), openislet_root=openislet_root) if rpv_res is None or rpv_res["product_variant_id"] != expected_product_variant_id: errs.append(f"{hold_prefix}_PRODUCT_VARIANT_ID_MISMATCH") # R04-B1: AUTOLOGOUS_D1B_PROCESS_QUAL is strictly allowed ONLY for canonical autologous # D1b-Auto-C variants (ISLET1_D1_AUTO_*) — reject any allogeneic product EVEN IF its # external QA process-record digest and product_variant_id agree! exp_pvid_res = resolve_canonical_product_variant(expected_product_variant_id, openislet_root=openislet_root) if rec_pvid_res is None: errs.append(f"{hold_prefix}_UNREGISTERED_RECORD_PRODUCT_VARIANT_ID") else: tier_ok_rec, tier_errs_rec = is_qualification_tier_allowed_for_product( rec_pvid_res, "AUTOLOGOUS_D1B_PROCESS_QUAL" ) if not tier_ok_rec: errs.extend(tier_errs_rec) if exp_pvid_res is not None: tier_ok_exp, tier_errs_exp = is_qualification_tier_allowed_for_product( exp_pvid_res, "AUTOLOGOUS_D1B_PROCESS_QUAL" ) if not tier_ok_exp: for te in tier_errs_exp: if te not in errs: errs.append(te) # R06-M3: Align autologous process-qualification validation with canonical autologous family semantics # (NEVER accept allogeneic PASS_WHOLEGRAFT_BANK_QUALIFICATION as the underlying autologous process pass!) if rec.get("underlying_gate0v_verdict") not in ( "PASS_WHOLEGRAFT_AUTOLOGOUS_PROCESS_QUAL", "PASS_COMPOSITE_AUTOLOGOUS_PROCESS_QUAL", ): errs.append(f"{hold_prefix}_UNDERLYING_GATE0V_VERDICT_NOT_AUTOLOGOUS_PROCESS_QUAL_PASS") if rec.get("underlying_gate0v_passed") is not True: errs.append(f"{hold_prefix}_UNDERLYING_GATE0V_NOT_PASSED") if rec.get("underlying_hard_stops_count") != 0 or rec.get("underlying_holds_count") != 0: errs.append(f"{hold_prefix}_UNDERLYING_NONZERO_STOPS_OR_HOLDS") if not _is_valid_sha256_hex(rec.get("process_qual_payload_sha256")) or not _is_valid_identifier(rec.get("process_qual_id")): errs.append(f"{hold_prefix}_MISSING_PAYLOAD_DIGEST_OR_ID") rec_req_ocr = False if "require_trusted_nw08_ocr_record" in rec: if not _is_strict_bool(rec.get("require_trusted_nw08_ocr_record")): errs.append("HOLD_NW08_COUPLED_INVALID_REQUIRE_TRUSTED_OCR_FLAG") errs.append(f"{hold_prefix}_INVALID_REQUIRE_TRUSTED_OCR_FLAG") else: rec_req_ocr = bool(rec.get("require_trusted_nw08_ocr_record")) archived_verified: bool | None = None if archived_payload is not None: arch_sha = _safe_canonical_lot_sha256(archived_payload) arch_pvid_raw = archived_payload.get("product_variant_id") arch_pvid_present = _is_clean_nonempty_str(arch_pvid_raw) arch_pvid_res = ( resolve_canonical_product_variant(arch_pvid_raw, openislet_root=openislet_root) if arch_pvid_present else None ) arch_pvid_canon = arch_pvid_res["product_variant_id"] if arch_pvid_res is not None else None arch_alias_ok = True if archived_payload.get("product_variant") is not None: apv_res = resolve_canonical_product_variant(archived_payload.get("product_variant"), openislet_root=openislet_root) arch_alias_ok = bool(apv_res is not None and apv_res["product_variant_id"] == expected_product_variant_id) arch_tier_ok = bool( ("qualification_tier" not in archived_payload or archived_payload.get("qualification_tier") == "AUTOLOGOUS_D1B_PROCESS_QUAL") and ("operational_tier" not in archived_payload or archived_payload.get("operational_tier") == "AUTOLOGOUS_D1B_PROCESS_QUAL") ) arch_ok = bool( arch_sha is not None and arch_sha == rec.get("process_qual_payload_sha256") and arch_pvid_present and arch_pvid_canon == expected_product_variant_id and arch_alias_ok and arch_tier_ok ) archived_verified = arch_ok if not arch_ok: errs.append(f"{hold_prefix}_ARCHIVED_PROCESS_PAYLOAD_VERIFICATION_FAILED") elif archive_status.startswith(("non_object", "unreadable", "invalid_container", "ambiguous")): archived_verified = False errs.append(f"{hold_prefix}_ARCHIVED_PROCESS_PAYLOAD_INVALID") if isinstance(inline_record_if_supplied, dict): inline_sha = _safe_canonical_qualification_artifact_sha256(inline_record_if_supplied) if inline_sha is None or computed_sha is None or inline_sha != computed_sha: errs.append(f"{hold_prefix}_INLINE_RECORD_CONFLICTS_WITH_TRUSTED_STORE") if errs: errs = ["HOLD_UNVERIFIED_EXTERNAL_APPROVAL_STORE"] + errs