def _pick_anchor(field_name: str, kw_val: Any) -> Any: rv = req_map.get(field_name) if rv is not None and kw_val is not None and rv != kw_val: return ("__CONFLICT__", rv, kw_val) return kw_val if kw_val is not None else rv eff_arm_id = _pick_anchor("arm_id", arm_id) eff_lot_id = _pick_anchor("lot_id", lot_id) eff_bank_id = _pick_anchor("bank_id", bank_id) eff_product_id = _pick_anchor("product_id", product_id) eff_construct_sha = _pick_anchor("construct_manifest_sha256", construct_manifest_sha256) eff_dossier_id = _pick_anchor("dossier_id", dossier_id) eff_qa_signoff_id = _pick_anchor("qa_signoff_id", qa_signoff_id) eff_exp_dossier_sha = _pick_anchor("expected_dossier_sha256", expected_dossier_sha256) eff_exp_qa_sha = _pick_anchor("expected_qa_signoff_sha256", expected_qa_signoff_sha256) eff_exp_manifest_sha = _pick_anchor("expected_canonical_manifest_sha256", expected_canonical_manifest_sha256) if eff_exp_manifest_sha is None and "canonical_arm_manifest_sha256" in req_map: eff_exp_manifest_sha = req_map.get("canonical_arm_manifest_sha256") eff_req_scope = _pick_anchor("release_scope", release_scope) # Check modeled non-substitution BEFORE anything else! is_modeled_attempt = ( "composite_verdict" in req_map or req_map.get("modeled_qualification_only") is True or (isinstance(req_map.get("authority_scope"), str) and req_map.get("authority_scope") in _FORBIDDEN_MODELED_SCOPES_V2_ROUND8) or (isinstance(req_map.get("evidence_tier"), str) and req_map.get("evidence_tier") in _FORBIDDEN_MODELED_SCOPES_V2_ROUND8) or (isinstance(req_map.get("qualification_scope"), str) and req_map.get("qualification_scope") in _FORBIDDEN_MODELED_SCOPES_V2_ROUND8) ) # Resolve governed manifest bytes FIRST (Audit R7-07: hash strictly derived from disk bytes!) if isinstance(eff_arm_id, tuple) and eff_arm_id and eff_arm_id[0] == "__CONFLICT__": return _return_finalized( _make_fail_closed_gate0v_custody_receipt_v2_round7( decision="FAIL_PHYSICAL_EVIDENCE_CUSTODY_MISMATCH", authority_scope="REJECTED_CUSTODY_MISMATCH", evidence_tier="UNAUTHENTICATED_FAIL", record_prefix="G0V-CUSTODY-FAIL", product_id=_safe_str_v2_round8(eff_product_id), lot_id=_safe_str_v2_round8(eff_lot_id), bank_id=_safe_str_v2_round8(eff_bank_id), bank_origin="", 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=["CONFLICTING_LOOKUP_ANCHOR:arm_id"], ), 3, ) if eff_arm_id is not None and not isinstance(eff_arm_id, str): return _return_finalized( _make_fail_closed_gate0v_custody_receipt_v2_round7( decision="FAIL_MALFORMED_ARCHIVED_MEASUREMENT", authority_scope="REJECTED_MALFORMED_ARCHIVED_MEASUREMENT", evidence_tier="UNAUTHENTICATED_FAIL", record_prefix="G0V-MALFORMED-FAIL", product_id=_safe_str_v2_round8(eff_product_id), lot_id=_safe_str_v2_round8(eff_lot_id), bank_id=_safe_str_v2_round8(eff_bank_id), bank_origin="", 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=[f"MALFORMED_ARM_ID_TYPE:{type(eff_arm_id).__name__}"], ), 3, ) if trust_ctx is None: is_inst_id_err = isinstance(trust_err, str) and trust_err.startswith("INSTALLATION_IDENTITY::") return _return_finalized( _make_fail_closed_gate0v_custody_receipt_v2_round7( decision="FAIL_UNTRUSTED_INSTALLATION_IDENTITY" if is_inst_id_err else "FAIL_UNTRUSTED_STORE_OR_MANIFEST_PATH", authority_scope="REJECTED_UNTRUSTED_INSTALLATION_IDENTITY" if is_inst_id_err else "REJECTED_UNTRUSTED_TRUST_ANCHOR", evidence_tier="UNAUTHENTICATED_FAIL", record_prefix="G0V-INSTID-FAIL" if is_inst_id_err else "G0V-TRUST-FAIL", product_id=_safe_str_v2_round8(eff_product_id), lot_id=_safe_str_v2_round8(eff_lot_id), bank_id=_safe_str_v2_round8(eff_bank_id), bank_origin="", 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=[f"OWNER_TRUST_CONFIG_REJECTED:{trust_err}"], ), 3, ) # Reject any caller-supplied canonical_manifest_path that is a symlink or diverges from configured owner anchor! if canonical_manifest_path is not None: if not isinstance(canonical_manifest_path, (Path, str)): return _return_finalized( _make_fail_closed_gate0v_custody_receipt_v2_round7( decision="FAIL_UNTRUSTED_STORE_OR_MANIFEST_PATH", authority_scope="REJECTED_UNTRUSTED_TRUST_ANCHOR", evidence_tier="UNAUTHENTICATED_FAIL", record_prefix="G0V-TRUST-FAIL", product_id=_safe_str_v2_round8(eff_product_id), lot_id=_safe_str_v2_round8(eff_lot_id), bank_id=_safe_str_v2_round8(eff_bank_id), bank_origin="", 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=[f"UNTRUSTED_MANIFEST_PATH_TYPE:{type(canonical_manifest_path).__name__}"], ), 3, ) caller_mpath = Path(canonical_manifest_path) if caller_mpath.is_symlink() or caller_mpath.resolve() != trust_ctx["canonical_manifest_resolved"]: return _return_finalized( _make_fail_closed_gate0v_custody_receipt_v2_round7( decision="FAIL_UNTRUSTED_STORE_OR_MANIFEST_PATH", authority_scope="REJECTED_UNTRUSTED_TRUST_ANCHOR", evidence_tier="UNAUTHENTICATED_FAIL", record_prefix="G0V-TRUST-FAIL", product_id=_safe_str_v2_round8(eff_product_id), lot_id=_safe_str_v2_round8(eff_lot_id), bank_id=_safe_str_v2_round8(eff_bank_id), bank_origin="", 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=["CALLER_CANONICAL_MANIFEST_PATH_OUTSIDE_OWNER_TRUST_ANCHOR"], ), 3, ) ctx, ctx_err = _resolve_governed_manifest_and_arm_v2_round8( arm_id=eff_arm_id, canonical_manifest_path=trust_ctx["canonical_manifest_raw_path"], openislet_root=resolved_root_pre, ) if ctx is None: is_hold_manifest = ctx_err in ("CANONICAL_MANIFEST_UNAVAILABLE",) return _return_finalized( _make_fail_closed_gate0v_custody_receipt_v2_round7( decision="HOLD_PHYSICAL_EVIDENCE_UNAVAILABLE" if is_hold_manifest else "FAIL_PHYSICAL_EVIDENCE_CUSTODY_MISMATCH", authority_scope="UNQUALIFIED_PENDING_PHYSICAL_EVIDENCE" if is_hold_manifest else "REJECTED_CUSTODY_MISMATCH", evidence_tier="UNAUTHENTICATED_HOLD" if is_hold_manifest else "UNAUTHENTICATED_FAIL", record_prefix="G0V-CUSTODY-HOLD" if is_hold_manifest else "G0V-CUSTODY-FAIL", product_id=_safe_str_v2_round8(eff_product_id), lot_id=_safe_str_v2_round8(eff_lot_id), bank_id=_safe_str_v2_round8(eff_bank_id), bank_origin="", 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=[ctx_err or "CANONICAL_MANIFEST_RESOLUTION_FAILED"], ), 2 if is_hold_manifest else 3, ) canonical_product_id = str(ctx["product_id"]) bank_origin = str(ctx["bank_origin"]) tier_family = str(ctx["tier_family"]) canonical_product_def = ctx["canonical_product_def"] manifest_sha = str(ctx["manifest_sha256"]) resolved_root: Path = ctx["resolved_root"] v38 = _get_v38_module() def _emit_fail(decision: str, scope: str, prefix: str, reasons: List[str], extra: Dict[str, Any] | None = None) -> Tuple[Dict[str, Any], int]: return _return_finalized( _make_fail_closed_gate0v_custody_receipt_v2_round7( decision=decision, authority_scope=scope, evidence_tier="UNAUTHENTICATED_FAIL", record_prefix=prefix, 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=reasons, extra_fields=extra, ), 3, ) # Audit R8-03: Configured canonical manifest bytes MUST match owner trust-config SHA-256 pin! if manifest_sha != trust_ctx["canonical_arm_manifest_sha256"]: return _emit_fail( "FAIL_CANONICAL_MANIFEST_SHA256_MISMATCH", "REJECTED_MANIFEST_HASH_MISMATCH", "G0V-MANIFEST-FAIL", [f"GOVERNED_MANIFEST_BYTES_MISMATCH_OWNER_TRUST_PIN:{manifest_sha}!={trust_ctx['canonical_arm_manifest_sha256']}"], ) if is_modeled_attempt: return _emit_fail( "FAIL_PHYSICAL_EVIDENCE_CUSTODY_MISMATCH", "REJECTED_CUSTODY_MISMATCH", "G0V-CUSTODY-FAIL", ["NON_SUBSTITUTION_ERROR:MODELED_OUTPUT_REJECTED_AS_PHYSICAL_EVIDENCE"], ) # Reject caller-supplied raw physical assay dicts (NEVER self-hash caller bodies!) forbidden_present = sorted(_FORBIDDEN_CALLER_PHYSICAL_ASSAY_KEYS.intersection(req_map.keys())) if forbidden_present: return _emit_fail( "FAIL_PHYSICAL_EVIDENCE_CUSTODY_MISMATCH", "REJECTED_CUSTODY_MISMATCH", "G0V-CUSTODY-FAIL", [f"FORBIDDEN_CALLER_SUPPLIED_ASSAY_BODY_KEYS:{','.join(forbidden_present)}"], ) # Check conflicting anchor arguments conflict_reasons: List[str] = [] for fname, fval in ( ("lot_id", eff_lot_id), ("bank_id", eff_bank_id), ("product_id", eff_product_id), ("construct_manifest_sha256", eff_construct_sha), ("dossier_id", eff_dossier_id), ("qa_signoff_id", eff_qa_signoff_id), ("expected_dossier_sha256", eff_exp_dossier_sha), ("expected_qa_signoff_sha256", eff_exp_qa_sha), ("expected_canonical_manifest_sha256", eff_exp_manifest_sha), ("release_scope", eff_req_scope), ): if isinstance(fval, tuple) and fval and fval[0] == "__CONFLICT__": conflict_reasons.append(f"CONFLICTING_LOOKUP_ANCHOR:{fname}") if conflict_reasons: return _emit_fail( "FAIL_PHYSICAL_EVIDENCE_CUSTODY_MISMATCH", "REJECTED_CUSTODY_MISMATCH", "G0V-CUSTODY-FAIL", conflict_reasons, ) # Audit R7-12: Typed check on lookup anchor types BEFORE any dict/path lookup (no TypeError on nested JSON!) malformed_anchor_reasons: List[str] = [] for fname, fval in ( ("lot_id", eff_lot_id), ("bank_id", eff_bank_id), ("product_id", eff_product_id), ("construct_manifest_sha256", eff_construct_sha), ("dossier_id", eff_dossier_id), ("qa_signoff_id", eff_qa_signoff_id), ("expected_dossier_sha256", eff_exp_dossier_sha), ("expected_qa_signoff_sha256", eff_exp_qa_sha),