OpenIslet

scripts/verify_iip0041_clinical_h1_cd64t_siglec.py

EXP-028 (CAND-031 / Target IIP-0041 — Bucket 2A) Remediated Verification Suite

script Release v0.8.0 gen-eb955f8266035aba 984 KB · 69 sections

Use this file in any AI chat (paste-only or attachment-capable):

Download as text (.txt) Download section (.txt)

Pinned raw file: https://openislet.org/files/gen-eb955f8266035aba/scripts/verify_iip0041_clinical_h1_cd64t_siglec.py · SHA-256: f004cf6d8259b8b4…

            "failure_reasons": failure_reasons,
        }

    # INDEPENDENT EXTERNAL trusted_clinical_store RESOLUTION (ZERO SELF-ATTESTATION!)
    self_attested_inline_rejected = any(
        k_in in cohort_payload and cohort_payload[k_in] is not None
        for k_in in (
            "prespecification_attestation",
            "source_verification_attestation",
            "verified_lot_to_bank_manifest",
            "parallel_cmc_readiness_ledger",
        )
    )
    payload_cmc_sha, cmc_sha_errs = _resolve_payload_cmc_ledger_sha256(cohort_payload)
    failure_reasons.extend(cmc_sha_errs)

    refs_raw = cohort_payload.get("clinical_store_refs")
    if refs_raw is not None and not isinstance(refs_raw, dict):
        failure_reasons.append("clinical_store_refs must be a dict mapping reference keys to 64-hex SHA-256 digests.")
        store_refs: dict[str, Any] = {}
    else:
        store_refs = dict(refs_raw or {})

    prespec_verified = False
    source_verified = False
    store_manifest_verified = False
    store_wcb_verified: bool | None = (False if wcb_required_or_present else None)
    cmc_ledger_verified = False
    store_manifest_failed_hierarchy = False
    cmc_ancestry_conflict = False
    st_mcb: dict[str, str] | None = None
    st_eff_wcb: dict[str, str] = {}
    trusted_store_source: str | None = None
    authenticated_store_digests: dict[str, str] = {}
    authenticated_store_issuers: dict[str, str] = {}
    parallel_cmc_summary: dict[str, Any] | None = None

    if trusted_clinical_store is None:
        failure_reasons.append(
            "Independent external trusted_clinical_store is required to certify lot/bank hierarchy and "
            "authorize Gate 28-A promotion (self-attested inline caller dicts and bare booleans can NEVER "
            "certify hierarchy, emit DEFF/n_eff/k_eff, or authorize promotion)."
        )
        store_manifest_failed_hierarchy = True
    else:
        # 1. Resolve PRESPECIFICATION_ATTESTATION strictly from trusted_clinical_store
        p_ref = store_refs.get("prespecification_attestation_sha256")
        if p_ref is None:
            failure_reasons.append("Missing clinical_store_refs.prespecification_attestation_sha256 for trusted_clinical_store lookup.")
        else:
            prec, perr, psrc = _lookup_gate28a_trusted_record(
                trusted_clinical_store,
                category="prespecification_attestations",
                expected_sha256=p_ref,
                allowed_issuers=GATE_28A_ALLOWED_ATTESTATION_AUTHORITIES,
            )
            if psrc is not None:
                trusted_store_source = psrc
            if perr is not None or prec is None:
                failure_reasons.append(f"Trusted clinical store resolution failed for PRESPECIFICATION_ATTESTATION: {perr}.")
            else:
                p_errs: list[str] = []
                p_recomp = compute_gate28a_record_sha256(prec)
                if not _is_valid_sha256_hex(prec.get("record_sha256")) or p_recomp != prec.get("record_sha256") or p_recomp != p_ref:
                    p_errs.append("PRESPECIFICATION_ATTESTATION SHA-256 verification failed.")
                if prec.get("attestation_sha256") is not None and prec.get("attestation_sha256") != p_recomp:
                    p_errs.append("PRESPECIFICATION_ATTESTATION attestation_sha256 contradicts record_sha256.")
                if prec.get("record_type") != "PRESPECIFICATION_ATTESTATION":
                    p_errs.append(f"Unexpected PRESPECIFICATION_ATTESTATION record_type={prec.get('record_type')!r}.")
                if cohort_id is not None and prec.get("cohort_id") != cohort_id:
                    p_errs.append(f"PRESPECIFICATION_ATTESTATION cohort_id={prec.get('cohort_id')!r} != {cohort_id!r}.")
                if prec.get("cohort_canonical_sha256") != cohort_canonical_sha256:
                    p_errs.append(
                        "PRESPECIFICATION_ATTESTATION cohort_canonical_sha256 does not match the full canonical cohort digest "
                        "(post-attestation mutation of compatibility, overlap, ICC, manifest, CMC hash, or recipients detected)."
                    )
                if prec.get("cohort_ledger_sha256") is not None and prec.get("cohort_ledger_sha256") != cohort_canonical_sha256:
                    p_errs.append("PRESPECIFICATION_ATTESTATION cohort_ledger_sha256 does not match full canonical cohort digest.")
                if _strict_literal_id(prec.get("protocol_or_sap_id")) is None:
                    p_errs.append("PRESPECIFICATION_ATTESTATION missing literal protocol_or_sap_id.")
                if prec.get("prespecified_before_unblinding") is not True or type(prec.get("prespecified_before_unblinding")) is not bool:
                    p_errs.append("PRESPECIFICATION_ATTESTATION prespecified_before_unblinding must be strict bool True.")
                if prec.get("single_compatible_cohort_confirmed") is not True or type(prec.get("single_compatible_cohort_confirmed")) is not bool:
                    p_errs.append("PRESPECIFICATION_ATTESTATION single_compatible_cohort_confirmed must be strict bool True.")
                p_issuer = _strict_literal_id(prec.get("issuer_authority_id"))
                if p_issuer not in GATE_28A_ALLOWED_ATTESTATION_AUTHORITIES:
                    p_errs.append(f"PRESPECIFICATION_ATTESTATION issuer_authority_id={prec.get('issuer_authority_id')!r} not in allowlist.")
                inline_p = cohort_payload.get("prespecification_attestation")
                if isinstance(inline_p, dict) and compute_gate28a_record_sha256(inline_p) != p_recomp:
                    p_errs.append("Inline prespecification_attestation contradicts trusted_clinical_store record.")
                if p_errs:
                    failure_reasons.extend(p_errs)
                else:
                    prespec_verified = True
                    authenticated_store_digests["prespecification_attestation"] = p_recomp
                    if p_issuer is not None:
                        authenticated_store_issuers["prespecification_attestation"] = p_issuer

        # 2. Resolve SOURCE_VERIFICATION_ATTESTATION strictly from trusted_clinical_store
        s_ref = store_refs.get("source_verification_attestation_sha256")
        source_att_cmc_sha: str | None = None
        if s_ref is None:
            failure_reasons.append("Missing clinical_store_refs.source_verification_attestation_sha256 for trusted_clinical_store lookup.")
        else:
            srec, serr, ssrc = _lookup_gate28a_trusted_record(
                trusted_clinical_store,
                category="source_verification_attestations",
                expected_sha256=s_ref,
                allowed_issuers=GATE_28A_ALLOWED_ATTESTATION_AUTHORITIES,
            )
            if ssrc is not None:
                trusted_store_source = ssrc
            if serr is not None or srec is None:
                failure_reasons.append(f"Trusted clinical store resolution failed for SOURCE_VERIFICATION_ATTESTATION: {serr}.")
            else:
                s_errs: list[str] = []
                s_recomp = compute_gate28a_record_sha256(srec)
                if not _is_valid_sha256_hex(srec.get("record_sha256")) or s_recomp != srec.get("record_sha256") or s_recomp != s_ref:
                    s_errs.append("SOURCE_VERIFICATION_ATTESTATION SHA-256 verification failed.")
                if srec.get("attestation_sha256") is not None and srec.get("attestation_sha256") != s_recomp:
                    s_errs.append("SOURCE_VERIFICATION_ATTESTATION attestation_sha256 contradicts record_sha256.")
                if srec.get("record_type") != "SOURCE_VERIFICATION_ATTESTATION":
                    s_errs.append(f"Unexpected SOURCE_VERIFICATION_ATTESTATION record_type={srec.get('record_type')!r}.")
                if cohort_id is not None and srec.get("cohort_id") != cohort_id:
                    s_errs.append(f"SOURCE_VERIFICATION_ATTESTATION cohort_id={srec.get('cohort_id')!r} != {cohort_id!r}.")
                if srec.get("cohort_canonical_sha256") != cohort_canonical_sha256:
                    s_errs.append(
                        "SOURCE_VERIFICATION_ATTESTATION cohort_canonical_sha256 does not match the full canonical cohort digest."
                    )
                if srec.get("cohort_ledger_sha256") is not None and srec.get("cohort_ledger_sha256") != cohort_canonical_sha256:
                    s_errs.append("SOURCE_VERIFICATION_ATTESTATION cohort_ledger_sha256 does not match full canonical cohort digest.")
                if _strict_literal_id(srec.get("primary_source_citation_id")) is None:
                    s_errs.append("SOURCE_VERIFICATION_ATTESTATION missing literal primary_source_citation_id.")
                if srec.get("primary_peer_reviewed_full_text_verified") is not True or type(srec.get("primary_peer_reviewed_full_text_verified")) is not bool:
                    s_errs.append("SOURCE_VERIFICATION_ATTESTATION primary_peer_reviewed_full_text_verified must be strict bool True.")
                elif "primary_peer_reviewed_full_text_verified" in cohort_payload:
                    pl_pdf = cohort_payload["primary_peer_reviewed_full_text_verified"]
                    if type(pl_pdf) is not bool or pl_pdf is not True:
                        s_errs.append(
                            f"SOURCE_VERIFICATION_ATTESTATION primary_peer_reviewed_full_text_verified=True contradicts "
                            f"cohort payload primary_peer_reviewed_full_text_verified={pl_pdf!r}."
                        )
                if srec.get("parallel_cmc_readiness_ledger_disclosed") is not True or type(srec.get("parallel_cmc_readiness_ledger_disclosed")) is not bool:
                    s_errs.append("SOURCE_VERIFICATION_ATTESTATION parallel_cmc_readiness_ledger_disclosed must be strict bool True.")
                elif "parallel_cmc_readiness_ledger_disclosed" in cohort_payload:
                    pl_cmc = cohort_payload["parallel_cmc_readiness_ledger_disclosed"]
                    if type(pl_cmc) is not bool or pl_cmc is not True:
                        s_errs.append(
                            f"SOURCE_VERIFICATION_ATTESTATION parallel_cmc_readiness_ledger_disclosed=True contradicts "
                            f"cohort payload parallel_cmc_readiness_ledger_disclosed={pl_cmc!r}."
                        )
                s_cmc = srec.get("parallel_cmc_readiness_ledger_sha256")
                if not _is_valid_sha256_hex(s_cmc):
                    s_errs.append("SOURCE_VERIFICATION_ATTESTATION missing valid 64-hex parallel_cmc_readiness_ledger_sha256.")
                else:
                    source_att_cmc_sha = str(s_cmc)
                    if payload_cmc_sha is not None and payload_cmc_sha != source_att_cmc_sha:
                        s_errs.append(
                            f"SOURCE_VERIFICATION_ATTESTATION parallel_cmc_readiness_ledger_sha256={source_att_cmc_sha} "
                            f"!= payload {payload_cmc_sha}."
                        )
                if srec.get("parallel_cmc_ledger_sha256") is not None and srec.get("parallel_cmc_ledger_sha256") != s_cmc:
                    s_errs.append("SOURCE_VERIFICATION_ATTESTATION conflicting parallel_cmc_ledger_sha256 alias.")
                s_issuer = _strict_literal_id(srec.get("issuer_authority_id"))
                if s_issuer not in GATE_28A_ALLOWED_ATTESTATION_AUTHORITIES:
                    s_errs.append(f"SOURCE_VERIFICATION_ATTESTATION issuer_authority_id={srec.get('issuer_authority_id')!r} not in allowlist.")
                inline_s = cohort_payload.get("source_verification_attestation")
                if isinstance(inline_s, dict) and compute_gate28a_record_sha256(inline_s) != s_recomp:
                    s_errs.append("Inline source_verification_attestation contradicts trusted_clinical_store record.")
                if s_errs:
                    failure_reasons.extend(s_errs)
                else:
                    source_verified = True
                    authenticated_store_digests["source_verification_attestation"] = s_recomp
                    if s_issuer is not None:
                        authenticated_store_issuers["source_verification_attestation"] = s_issuer

        # 3. Resolve VERIFIED_LOT_TO_BANK_MANIFEST strictly from trusted_clinical_store
        m_ref = store_refs.get("verified_lot_to_bank_manifest_sha256")
        if m_ref is None:
            failure_reasons.append("Missing clinical_store_refs.verified_lot_to_bank_manifest_sha256 for trusted_clinical_store lookup.")
            store_manifest_failed_hierarchy = True
        else:
            mrec, merr, msrc = _lookup_gate28a_trusted_record(
                trusted_clinical_store,
                category="verified_lot_to_bank_manifests",
                expected_sha256=m_ref,
                allowed_issuers=GATE_28A_ALLOWED_ATTESTATION_AUTHORITIES,
            )
            if msrc is not None:
                trusted_store_source = msrc
            if merr is not None or mrec is None:
                failure_reasons.append(f"Trusted clinical store resolution failed for VERIFIED_LOT_TO_BANK_MANIFEST: {merr}.")
                store_manifest_failed_hierarchy = True
            else:
                m_errs: list[str] = []
                m_recomp = compute_gate28a_record_sha256(mrec)
                if not _is_valid_sha256_hex(mrec.get("record_sha256")) or m_recomp != mrec.get("record_sha256") or m_recomp != m_ref:
                    m_errs.append("VERIFIED_LOT_TO_BANK_MANIFEST SHA-256 verification failed.")
                if mrec.get("record_type") != "VERIFIED_LOT_TO_BANK_MANIFEST":
                    m_errs.append(f"Unexpected VERIFIED_LOT_TO_BANK_MANIFEST record_type={mrec.get('record_type')!r}.")
                if cohort_id is not None and mrec.get("cohort_id") != cohort_id:
                    m_errs.append(f"VERIFIED_LOT_TO_BANK_MANIFEST cohort_id={mrec.get('cohort_id')!r} != {cohort_id!r}.")
                if _strict_literal_id(mrec.get("manifest_source_id")) is None:
                    m_errs.append("VERIFIED_LOT_TO_BANK_MANIFEST missing literal manifest_source_id.")
                m_issuer = _strict_literal_id(mrec.get("issuer_authority_id"))
                if m_issuer not in GATE_28A_ALLOWED_ATTESTATION_AUTHORITIES:
                    m_errs.append(f"VERIFIED_LOT_TO_BANK_MANIFEST issuer_authority_id={mrec.get('issuer_authority_id')!r} not in allowlist.")
                st_mcb, st_eff_wcb, st_wcb_present, st_man_errs = _merge_and_validate_bank_manifests(
                    mrec.get("lot_to_mcb_manifest"),
                    mrec.get("lot_to_wcb_manifest"),
                    mrec.get("lot_to_bank_manifest"),
                    context="VERIFIED_LOT_TO_BANK_MANIFEST",
                    extra_container=mrec,
                )
                m_errs.extend(st_man_errs)
                if st_mcb is not None and manifest_mcb_norm is not None:
                    if st_mcb != manifest_mcb_norm:
                        m_errs.append("VERIFIED_LOT_TO_BANK_MANIFEST lot_to_mcb_manifest does not match cohort payload manifest.")
                    for lid, obs_mcb in verified_lot_to_mcb.items():
                        if st_mcb.get(lid) != obs_mcb:
                            m_errs.append(
                                f"VERIFIED_LOT_TO_BANK_MANIFEST mismatch for lot_id={lid!r}: store={st_mcb.get(lid)!r} vs cohort={obs_mcb!r}."
                            )
                if wcb_required_or_present or st_wcb_present or st_eff_wcb:
                    if not st_eff_wcb:
                        m_errs.append("VERIFIED_LOT_TO_BANK_MANIFEST missing required lot_to_wcb_manifest for declared wcb_id ancestry.")
                    elif st_eff_wcb != effective_wcb_manifest:
                        m_errs.append("VERIFIED_LOT_TO_BANK_MANIFEST lot_to_wcb_manifest does not match cohort payload WCB manifest.")
                if m_errs:
                    failure_reasons.extend(m_errs)
                    store_manifest_failed_hierarchy = True
                else:
                    store_manifest_verified = True
                    store_wcb_verified = True if (wcb_required_or_present or st_wcb_present or st_eff_wcb) else None
                    authenticated_store_digests["verified_lot_to_bank_manifest"] = m_recomp
                    if m_issuer is not None:
                        authenticated_store_issuers["verified_lot_to_bank_manifest"] = m_issuer

        # 4. Resolve ACTUAL PARALLEL_CMC_READINESS_LEDGER payload bytes, verify SHA-256, run CMC evaluator, and reconcile recipients!
        target_cmc_sha = payload_cmc_sha or source_att_cmc_sha
        if target_cmc_sha is None:
            failure_reasons.append("Missing parallel_cmc_readiness_ledger_sha256 for trusted_clinical_store CMC ledger lookup.")
        else:
            cmc_bytes, cmc_err, csrc = _lookup_gate28a_trusted_cmc_ledger_bytes(
                trusted_clinical_store,
                expected_sha256=target_cmc_sha,
                allowed_issuers=GATE_28A_ALLOWED_ATTESTATION_AUTHORITIES,
            )
            if csrc is not None: