("expected_canonical_manifest_sha256", eff_exp_manifest_sha), ("release_scope", eff_req_scope), ): if fval is not None and not isinstance(fval, str): malformed_anchor_reasons.append(f"MALFORMED_LOOKUP_ANCHOR_TYPE:{fname}:{type(fval).__name__}") if malformed_anchor_reasons: return _emit_fail( "FAIL_MALFORMED_ARCHIVED_MEASUREMENT", "REJECTED_MALFORMED_ARCHIVED_MEASUREMENT", "G0V-MALFORMED-FAIL", malformed_anchor_reasons, ) # Audit R7-07: If caller supplied expected_canonical_manifest_sha256, it MUST match governed manifest bytes! if eff_exp_manifest_sha is not None and eff_exp_manifest_sha != manifest_sha: return _emit_fail( "FAIL_CANONICAL_MANIFEST_SHA256_MISMATCH", "REJECTED_MANIFEST_HASH_MISMATCH", "G0V-MANIFEST-FAIL", [f"CALLER_CANONICAL_MANIFEST_SHA256_MISMATCH:{eff_exp_manifest_sha}!={manifest_sha}"], ) # Resolve separately governed on-disk store root STRICTLY against owner trust anchor (Audit R7-06 & Audit R8-03)! if governed_store_root is None: store_root = trust_ctx["authorized_store_raw_path"] elif isinstance(governed_store_root, (Path, str)): caller_sroot = Path(governed_store_root) if caller_sroot.is_symlink() or caller_sroot.resolve() != trust_ctx["authorized_store_resolved"]: return _emit_fail( "FAIL_UNTRUSTED_STORE_OR_MANIFEST_PATH", "REJECTED_UNTRUSTED_TRUST_ANCHOR", "G0V-TRUST-FAIL", ["CALLER_GOVERNED_STORE_ROOT_OUTSIDE_OWNER_TRUST_ANCHOR"], ) store_root = trust_ctx["authorized_store_raw_path"] else: return _emit_fail( "FAIL_MUTABLE_CALLER_STORE_FORBIDDEN", "REJECTED_MUTABLE_CALLER_STORE", "G0V-STORE-FAIL", [f"GOVERNED_STORE_ROOT_MUST_BE_PATH:{type(governed_store_root).__name__}"], ) hold_reasons: List[str] = [] if not _is_clean_nonempty_str(eff_dossier_id): hold_reasons.append("MISSING_LOOKUP_DOSSIER_ID") elif not _is_safe_store_id_v2_round8(eff_dossier_id): return _emit_fail( "FAIL_PHYSICAL_EVIDENCE_CUSTODY_MISMATCH", "REJECTED_CUSTODY_MISMATCH", "G0V-CUSTODY-FAIL", ["UNSAFE_LOOKUP_DOSSIER_ID"], ) if not _is_clean_nonempty_str(eff_qa_signoff_id): hold_reasons.append("MISSING_LOOKUP_QA_SIGNOFF_ID") elif not _is_safe_store_id_v2_round8(eff_qa_signoff_id): return _emit_fail( "FAIL_PHYSICAL_EVIDENCE_CUSTODY_MISMATCH", "REJECTED_CUSTODY_MISMATCH", "G0V-CUSTODY-FAIL", ["UNSAFE_LOOKUP_QA_SIGNOFF_ID"], ) if store_root.is_symlink() or not store_root.is_dir(): hold_reasons.append("GOVERNED_OWNER_STORE_ROOT_UNAVAILABLE") dossier_path = store_root / "physical_dossiers" / f"{eff_dossier_id}.json" if _is_safe_store_id_v2_round8(eff_dossier_id) else None qa_path = store_root / "qa_signoffs" / f"{eff_qa_signoff_id}.json" if _is_safe_store_id_v2_round8(eff_qa_signoff_id) else None issuer_reg_path = store_root / "qa_issuer_registry.json" # Audit R8-03: If the governed store directory exists, an absent qa_issuer_registry.json is a hard FAIL (never HOLD/PASS)! if not hold_reasons and not issuer_reg_path.is_file() and not issuer_reg_path.is_symlink(): return _emit_fail( "FAIL_EMPTY_OR_MISSING_QA_ISSUER_REGISTRY", "REJECTED_EMPTY_OR_MISSING_QA_ISSUER_REGISTRY", "G0V-REGISTRY-FAIL", ["ABSENT_QA_ISSUER_REGISTRY_IN_GOVERNED_STORE"], ) if not hold_reasons: if dossier_path is None or (not dossier_path.is_file() and not dossier_path.is_symlink()): hold_reasons.append("ABSENT_PHYSICAL_DOSSIER_IN_GOVERNED_STORE") if qa_path is None or (not qa_path.is_file() and not qa_path.is_symlink()): hold_reasons.append("ABSENT_QA_SIGNOFF_IN_GOVERNED_STORE") if hold_reasons: return _return_finalized( _make_fail_closed_gate0v_custody_receipt_v2_round7( decision="HOLD_PHYSICAL_EVIDENCE_UNAVAILABLE", authority_scope="UNQUALIFIED_PENDING_PHYSICAL_EVIDENCE", evidence_tier="UNAUTHENTICATED_HOLD", record_prefix="G0V-CUSTODY-HOLD", product_id=canonical_product_id, lot_id=_safe_str_v2_round8(eff_lot_id), bank_id=_safe_str_v2_round8(eff_bank_id), bank_origin=bank_origin, manifest_sha=manifest_sha, construct_manifest_sha256=_safe_str_v2_round8(eff_construct_sha), dossier_id=_safe_str_v2_round8(eff_dossier_id) or None, qa_signoff_id=_safe_str_v2_round8(eff_qa_signoff_id) or None, custody_failure_reasons=hold_reasons, ), 2, ) # Load immutable on-disk governed files cleanly (no uncaught JSON/symlink/type errors!) dossier_obj, d_load_err = _safe_load_governed_json_v2_round8(dossier_path) # type: ignore[arg-type] qa_obj, q_load_err = _safe_load_governed_json_v2_round8(qa_path) # type: ignore[arg-type] issuer_reg_raw_sha: str | None = None if issuer_reg_path.is_file() and not issuer_reg_path.is_symlink(): try: issuer_reg_raw_sha = hashlib.sha256(issuer_reg_path.read_bytes()).hexdigest() except OSError: issuer_reg_raw_sha = None issuer_reg_obj, reg_load_err = _safe_load_governed_json_v2_round8(issuer_reg_path) empty_registry_reasons: List[str] = [] registry_pin_reasons: List[str] = [] malformed_reasons: List[str] = [] missing_dossier_manifest_reasons: List[str] = [] missing_qa_manifest_reasons: List[str] = [] manifest_mismatch_reasons: List[str] = [] issuer_auth_reasons: List[str] = [] tier_rule_reasons: List[str] = [] adverse_qual_reasons: List[str] = [] custody_reasons: List[str] = [] if d_load_err is not None: malformed_reasons.append(f"DOSSIER_LOAD_ERROR:{d_load_err}") elif not isinstance(dossier_obj, dict): malformed_reasons.append(f"OWNER_DOSSIER_NOT_MAPPING:{type(dossier_obj).__name__}") if q_load_err is not None: malformed_reasons.append(f"QA_SIGNOFF_LOAD_ERROR:{q_load_err}") elif not isinstance(qa_obj, dict): malformed_reasons.append(f"OWNER_QA_SIGNOFF_NOT_MAPPING:{type(qa_obj).__name__}") if reg_load_err is not None: if reg_load_err == "FILE_NOT_FOUND": empty_registry_reasons.append(f"QA_ISSUER_REGISTRY_LOAD_ERROR:{reg_load_err}") elif reg_load_err == "SYMLINK_FORBIDDEN": registry_pin_reasons.append(f"QA_ISSUER_REGISTRY_LOAD_ERROR:{reg_load_err}") else: issuer_auth_reasons.append(f"QA_ISSUER_REGISTRY_LOAD_ERROR:{reg_load_err}") elif not isinstance(issuer_reg_obj, dict): empty_registry_reasons.append("QA_ISSUER_REGISTRY_NOT_MAPPING") # Validate caller identity anchors if not _is_clean_nonempty_str(eff_lot_id): custody_reasons.append("INVALID_LOT_ID") if not _is_clean_nonempty_str(eff_bank_id): custody_reasons.append("INVALID_BANK_ID") if not _is_clean_nonempty_str(eff_product_id): custody_reasons.append("MISSING_LOOKUP_PRODUCT_ID") elif eff_product_id != canonical_product_id: custody_reasons.append(f"PRODUCT_ID_MISMATCH:{eff_product_id}!={canonical_product_id}") if not _is_safe_sha256_v2_round8(eff_construct_sha): custody_reasons.append("INVALID_CONSTRUCT_MANIFEST_SHA256") # Validate QA issuer registry non-emptiness, owner trust pin, and self-digest (Audit R7-06 & Audit R8-03)! verified_issuers: Dict[str, Any] = {} if isinstance(issuer_reg_obj, dict): issuers_raw = issuer_reg_obj.get("issuers") if not isinstance(issuers_raw, dict) or len(issuers_raw) == 0: empty_registry_reasons.append( f"QA_ISSUER_REGISTRY_EMPTY_OR_INVALID_ISSUERS:{type(issuers_raw).__name__}:len={len(issuers_raw) if isinstance(issuers_raw, (dict, list)) else -1}" ) else: verified_issuers = issuers_raw if issuer_reg_raw_sha != trust_ctx["qa_issuer_registry_sha256"]: registry_pin_reasons.append( f"QA_ISSUER_REGISTRY_OWNER_TRUST_PIN_MISMATCH:{issuer_reg_raw_sha}!={trust_ctx['qa_issuer_registry_sha256']}" ) if issuer_reg_obj.get("schema_version") != _QA_ISSUER_REGISTRY_SCHEMA_V2_ROUND8: issuer_auth_reasons.append("QA_ISSUER_REGISTRY_SCHEMA_INVALID") reg_sha = issuer_reg_obj.get("registry_sha256") if not _is_safe_sha256_v2_round8(reg_sha): issuer_auth_reasons.append("QA_ISSUER_REGISTRY_SHA256_INVALID") else: recomp_reg_sha = _safe_canonical_sha256_v2_round8( {k: v for k, v in issuer_reg_obj.items() if k != "registry_sha256"} ) if recomp_reg_sha != reg_sha: issuer_auth_reasons.append("QA_ISSUER_REGISTRY_DIGEST_TAMPER_MISMATCH") # Validate archived physical dossier integrity, typed fields, quantitative thresholds & anchors dossier_sha = "" parsed_metrics: Dict[str, float] = {} release_scope_val: str | None = None if isinstance(dossier_obj, dict): d_sha_raw = dossier_obj.get("archive_sha256") if not _is_safe_sha256_v2_round8(d_sha_raw): custody_reasons.append("DOSSIER_ARCHIVE_SHA256_INVALID") else: dossier_sha = str(d_sha_raw) recomp_d = _safe_canonical_sha256_v2_round8( {k: v for k, v in dossier_obj.items() if k not in ("archive_sha256", "record_sha256")} ) if recomp_d is None: malformed_reasons.append("DOSSIER_NON_CANONICALIZABLE_ARCHIVE") elif recomp_d != dossier_sha: custody_reasons.append("DOSSIER_DIGEST_TAMPER_MISMATCH") if eff_exp_dossier_sha is not None and eff_exp_dossier_sha != dossier_sha: custody_reasons.append("DOSSIER_EXPECTED_SHA256_MISMATCH") if ( "composite_verdict" in dossier_obj or dossier_obj.get("modeled_qualification_only") is True or (isinstance(dossier_obj.get("authority_scope"), str) and dossier_obj.get("authority_scope") in _FORBIDDEN_MODELED_SCOPES_V2_ROUND8) or (isinstance(dossier_obj.get("evidence_tier"), str) and dossier_obj.get("evidence_tier") in _FORBIDDEN_MODELED_SCOPES_V2_ROUND8) or (isinstance(dossier_obj.get("qualification_scope"), str) and dossier_obj.get("qualification_scope") in _FORBIDDEN_MODELED_SCOPES_V2_ROUND8) ): custody_reasons.append("MODELED_DOSSIER_REJECTED_BY_PHYSICAL_CUSTODY") if dossier_obj.get("dossier_type") != "AUTHENTICATED_PHYSICAL_WETLAB_GATE0V_DOSSIER": custody_reasons.append("INVALID_PHYSICAL_DOSSIER_TYPE") if dossier_obj.get("evidence_tier") != "AUTHENTICATED_ARCHIVED_WETLAB": custody_reasons.append("INVALID_PHYSICAL_DOSSIER_EVIDENCE_TIER") if dossier_obj.get("physical_qualification_complete") is not True: if not isinstance(dossier_obj.get("physical_qualification_complete"), bool): malformed_reasons.append("MALFORMED_PHYSICAL_QUALIFICATION_COMPLETE_TYPE") else: custody_reasons.append("DOSSIER_PHYSICAL_QUALIFICATION_INCOMPLETE") if dossier_obj.get("whole_graft_stressed_tested") is not True: if not isinstance(dossier_obj.get("whole_graft_stressed_tested"), bool): malformed_reasons.append("MALFORMED_WHOLEGRAFT_STRESSED_TYPE") else: adverse_qual_reasons.append("FAIL_GATE0V_WHOLEGRAFT_STRESSED_NOT_TESTED") lin_ok, lin_err, lin_malformed = _safe_validate_5_lineages_v2_round8(dossier_obj.get("lineages_verified")) if not lin_ok: if lin_malformed: malformed_reasons.append(f"MALFORMED_LINEAGES:{lin_err}") else: adverse_qual_reasons.append(lin_err or "DOSSIER_5_LINEAGES_INCOMPLETE") raw_rscope = dossier_obj.get("release_scope") if not isinstance(raw_rscope, str): malformed_reasons.append(f"MALFORMED_RELEASE_SCOPE_TYPE:{type(raw_rscope).__name__}") elif raw_rscope not in ("CLINICAL_LOT_RELEASE", "BANK_QUALIFICATION_ONLY"): custody_reasons.append("DOSSIER_INVALID_RELEASE_SCOPE") else: release_scope_val = raw_rscope if eff_req_scope is not None and eff_req_scope != release_scope_val: custody_reasons.append(f"CALLER_RELEASE_SCOPE_MISMATCH:{eff_req_scope}!={release_scope_val}") # Audit R8-09: Mandatory canonical_arm_manifest_sha256 attestation on archived physical dossier! d_msha = dossier_obj.get("canonical_arm_manifest_sha256") if d_msha is None: missing_dossier_manifest_reasons.append("MISSING_DOSSIER_CANONICAL_ARM_MANIFEST_SHA256") elif not isinstance(d_msha, str): malformed_reasons.append("MALFORMED_DOSSIER_MANIFEST_SHA_TYPE") elif not _is_safe_sha256_v2_round8(d_msha): missing_dossier_manifest_reasons.append("INVALID_OR_EMPTY_DOSSIER_CANONICAL_ARM_MANIFEST_SHA256")