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…

            )
        )
    cmc_raw_bytes, cmc_sha = build_gate28a_parallel_cmc_ledger_bytes(
        cohort_id=cohort_id,
        lot_ledger=cmc_ledger_list,
        issuer_authority_id=issuer_authority_id,
    )

    payload: dict[str, Any] = {
        "cohort_id": cohort_id,
        **GATE_28A_COMPATIBILITY_TARGET,
        "includes_reichman": False,
        "rho_lot": GATE_28A_RHO_LOT_MIN,
        "rho_bank": GATE_28A_RHO_BANK_MIN,
        "primary_peer_reviewed_full_text_verified": True,
        "parallel_cmc_readiness_ledger_disclosed": True,
        "parallel_cmc_readiness_ledger_sha256": cmc_sha,
        "lot_to_mcb_manifest": payload_mcb_manifest,
        "recipients": copy.deepcopy(recipients),
    }
    if payload_wcb_manifest is not None:
        payload["lot_to_wcb_manifest"] = payload_wcb_manifest
    payload.update(overrides)

    canon_sha = compute_gate28a_cohort_canonical_sha256(payload)
    prespec_rec = build_gate28a_prespecification_attestation(
        cohort_id=str(payload["cohort_id"]),
        cohort_canonical_sha256=canon_sha,
        protocol_or_sap_id=protocol_or_sap_id,
        issuer_authority_id=issuer_authority_id,
    )
    source_rec = build_gate28a_source_verification_attestation(
        cohort_id=str(payload["cohort_id"]),
        cohort_canonical_sha256=canon_sha,
        parallel_cmc_readiness_ledger_sha256=cmc_sha,
        primary_source_citation_id=primary_source_citation_id,
        issuer_authority_id=issuer_authority_id,
    )
    manifest_rec = build_gate28a_verified_lot_to_bank_manifest(
        cohort_id=str(payload["cohort_id"]),
        lot_to_mcb_manifest=mcb_map,
        lot_to_wcb_manifest=payload_wcb_manifest,
        manifest_source_id=manifest_source_id,
        issuer_authority_id=issuer_authority_id,
    )

    write_gate28a_trusted_clinical_store(
        store_path,
        prespecification_attestations=[prespec_rec],
        source_verification_attestations=[source_rec],
        verified_lot_to_bank_manifests=[manifest_rec],
        parallel_cmc_readiness_ledgers=[cmc_raw_bytes],
        store_authority_id=store_authority_id,
    )

    payload["clinical_store_refs"] = {
        "prespecification_attestation_sha256": prespec_rec["record_sha256"],
        "source_verification_attestation_sha256": source_rec["record_sha256"],
        "verified_lot_to_bank_manifest_sha256": manifest_rec["record_sha256"],
        "parallel_cmc_readiness_ledger_sha256": cmc_sha,
    }
    store_records = {
        "prespecification_attestation": prespec_rec,
        "source_verification_attestation": source_rec,
        "verified_lot_to_bank_manifest": manifest_rec,
        "parallel_cmc_readiness_ledger_bytes": cmc_raw_bytes,
        "parallel_cmc_readiness_ledger_sha256": cmc_sha,
        "cohort_canonical_sha256": canon_sha,
    }
    return payload, store_records


def build_valid_gate28a_cohort_payload(
    recipients: list[dict[str, Any]],
    *,
    cohort_id: str = "COHORT-GATE28A-REF-001",
    lot_to_mcb_manifest: dict[str, Any] | None = None,
    lot_to_wcb_manifest: dict[str, str] | None = None,
    protocol_or_sap_id: str = "SAP-GATE28A-2026-V1",
    primary_source_citation_id: str = "PMID:99999999",
    parallel_cmc_ledger_sha256: str = "a1b2c3d4e5f60718293a4b5c6d7e8f90123456789abcdef0123456789abcdef0",
    issuer_authority_id: str = "OPENISLET_CLINICAL_ADJUDICATION_BOARD_V1",
    **overrides: Any,
) -> dict[str, Any]:
    """
    Build a full-digest-bound inline Gate 28-A payload (useful for diagnostic inspection and negative
    self-attested-inline rejection tests; CANNOT authorize promotion without trusted_clinical_store!).
    """
    if lot_to_mcb_manifest is None:
        manifest: dict[str, Any] = {}
        derived_wcb: dict[str, str] = {}
        for rec in recipients:
            lid = rec["lot_id"]
            mid = rec["mcb_id"]
            if lid not in manifest:
                manifest[lid] = mid
            wid = rec.get("wcb_id") if rec.get("wcb_id") is not None else rec.get("working_cell_bank_id")
            if wid is not None and lid not in derived_wcb:
                derived_wcb[lid] = wid
        wcb_manifest = copy.deepcopy(lot_to_wcb_manifest) if lot_to_wcb_manifest is not None else (derived_wcb or None)
    else:
        manifest = copy.deepcopy(lot_to_mcb_manifest)
        wcb_manifest = copy.deepcopy(lot_to_wcb_manifest) if lot_to_wcb_manifest is not None else None

    payload: dict[str, Any] = {
        "cohort_id": cohort_id,
        **GATE_28A_COMPATIBILITY_TARGET,
        "includes_reichman": False,
        "rho_lot": GATE_28A_RHO_LOT_MIN,
        "rho_bank": GATE_28A_RHO_BANK_MIN,
        "primary_peer_reviewed_full_text_verified": True,
        "parallel_cmc_readiness_ledger_disclosed": True,
        "parallel_cmc_readiness_ledger_sha256": parallel_cmc_ledger_sha256,
        "lot_to_mcb_manifest": manifest,
        "recipients": copy.deepcopy(recipients),
    }
    if wcb_manifest is not None:
        payload["lot_to_wcb_manifest"] = wcb_manifest
    payload.update(overrides)

    canon_sha = compute_gate28a_cohort_canonical_sha256(payload)
    payload["prespecification_attestation"] = build_gate28a_prespecification_attestation(
        cohort_id=str(payload["cohort_id"]),
        cohort_canonical_sha256=canon_sha,
        protocol_or_sap_id=protocol_or_sap_id,
        issuer_authority_id=issuer_authority_id,
    )
    payload["source_verification_attestation"] = build_gate28a_source_verification_attestation(
        cohort_id=str(payload["cohort_id"]),
        cohort_canonical_sha256=canon_sha,
        parallel_cmc_readiness_ledger_sha256=str(payload.get("parallel_cmc_readiness_ledger_sha256") or parallel_cmc_ledger_sha256),
        primary_source_citation_id=primary_source_citation_id,
        issuer_authority_id=issuer_authority_id,
    )
    return payload


def evaluate_gate28a_h1_promotion(
    cohort_payload: dict[str, Any],
    *,
    trusted_clinical_store: pathlib.Path | str | None = None,
) -> dict[str, Any]:
    """
    Executable bank/lot-aware Gate 28-A clinical H1 promotion evaluator (fail-closed):
      1. Enforces the exact prespecified single compatibility target (allogeneic GMP SC-islet,
         intraportal hepatic site, T1D, de novo chronic glucocorticoid-free systemic IS,
         full-dose single infusion). Full-dose split-lot recipients are eligible 1:1 with single-lot
         ancestry; pooled multi-lot courses are excluded from primary single-lot promotion.
      2. Enforces 1:1 unique recipients and STRICT functional lot -> MCB (and verified lot_to_wcb_manifest
         whenever ANY recipient declares wcb_id) hierarchy with ZERO fallback to unverified recipient rows:
         missing, empty, invalid, incomplete, conflicting, or mismatched manifests IMMEDIATELY return
         lot_bank_hierarchy_valid=False, deff=None, n_eff=None, k_eff=None, promotion_authorized=False.
      3. Requires resolving ALL FOUR authenticated records from an independent external `trusted_clinical_store`
         (NEVER trusting self-attested inline caller dicts or bare booleans; operational deployment dependency:
         independent QA/clinical governance custody, verified external store provenance, write-restricted
         access policy, and separation of duties):
           (a) PRESPECIFICATION_ATTESTATION bound to exact cohort_id + full cohort_canonical_sha256
               (binding cohort_id, all 5 compatibility declarations, includes_reichman, rho_lot, rho_bank,
               manifests, parallel_cmc_readiness_ledger_sha256, and full canonical recipients),
           (b) SOURCE_VERIFICATION_ATTESTATION bound to exact cohort_id + full cohort_canonical_sha256 +
               parallel_cmc_readiness_ledger_sha256,
           (c) VERIFIED_LOT_TO_BANK_MANIFEST matching payload MCB (and WCB) manifests,
           (d) Raw PARALLEL_CMC_READINESS_LEDGER bytes whose SHA-256 matches, passes
               evaluate_cmc_attempted_lot_readiness(...), contains zero pooled multi-lot courses, and
               reconciles infused clinical recipient IDs/lots/MCBs/rescue status/outcomes against the cohort.
      4. Enforces ONE unified deterministic intercurrent-event adjudication table (rescue repeat infusion = 0,
         prohibited peri-infusion systemic glucocorticoids = 0 in the primary glucocorticoid-free H1 estimand,
         missingness/LTFU = 0, pre-landmark death = 0, primary graft non-function = 0).
      5. Enforces structural manufacturing diversity floors (distinct_gmp_lots_n >= 3, distinct_mcb_n >= 2,
         max_single_lot_share <= 0.40 via exact context-independent Fraction) AND evaluates BOTH non-overlapping
         posteriors (Uniform Beta(1,1) P(H1>=0.90) >= 0.90 AND non-Reichman anchored Beta(8.8,1.2)
         P(H1>=0.90) >= 0.85) on cluster-adjusted (k_eff, n_eff).
    """
    if not isinstance(cohort_payload, dict):
        return {
            "status": "HOLD_H1_AT_88PCT",
            "promotion_authorized": False,
            "external_clinical_store_verified": False,
            "lot_bank_hierarchy_valid": False,
            "lot_to_mcb_manifest_verified": False,
            "lot_to_wcb_manifest_verified": False,
            "prespecification_attestation_verified": False,
            "source_verification_attestation_verified": False,
            "parallel_cmc_ledger_verified": False,
            "icc_sensitivity_report_attached": False,
            "icc_sensitivity_report": None,
            "boundary_sensitive_under_high_icc": False,
            "icc_sensitivity_review_status": None,
            "deff": None,
            "n_eff": None,
            "k_eff": None,
            "trusted_clinical_store_deployment_dependency": TRUSTED_CLINICAL_STORE_DEPLOYMENT_DEPENDENCY,
            "failure_reasons": ["cohort_payload must be a dict"],
        }

    failure_reasons: list[str] = []
    cohort_canonical_sha256 = compute_gate28a_cohort_canonical_sha256(cohort_payload)

    cohort_id = _strict_literal_id(cohort_payload.get("cohort_id"))
    if cohort_id is None:
        failure_reasons.append(f"Missing or invalid literal cohort_id={cohort_payload.get('cohort_id')!r}.")

    for field, expected in GATE_28A_COMPATIBILITY_TARGET.items():
        actual = _strict_literal_id(cohort_payload.get(field))
        if actual != expected:
            failure_reasons.append(f"Compatibility target mismatch for {field}: got {cohort_payload.get(field)!r}, expected {expected!r}.")

    includes_reichman = cohort_payload.get("includes_reichman")
    if type(includes_reichman) is not bool:
        failure_reasons.append("includes_reichman must be explicit strict bool.")
    elif includes_reichman is True:
        failure_reasons.append("Cohort includes Reichman 2025 recipients; non-overlapping Beta(8.8, 1.2) requires a non-Reichman compatible cohort.")

    if "primary_peer_reviewed_full_text_verified" in cohort_payload:
        pdf_ok = cohort_payload.get("primary_peer_reviewed_full_text_verified")
        if type(pdf_ok) is not bool or pdf_ok is not True:
            failure_reasons.append("primary_peer_reviewed_full_text_verified must be strict bool True when supplied.")

    if "parallel_cmc_readiness_ledger_disclosed" in cohort_payload:
        cmc_ok = cohort_payload.get("parallel_cmc_readiness_ledger_disclosed")
        if type(cmc_ok) is not bool or cmc_ok is not True:
            failure_reasons.append("parallel_cmc_readiness_ledger_disclosed must be strict bool True when supplied.")

    icc_domain_invalid = False
    rho_lot_raw = cohort_payload.get("rho_lot", GATE_28A_RHO_LOT_MIN)
    rho_bank_raw = cohort_payload.get("rho_bank", GATE_28A_RHO_BANK_MIN)
    rho_lot_dec = _strict_decimal_in_range(rho_lot_raw, min_val=Decimal("0"), max_val=Decimal("1"))
    rho_bank_dec = _strict_decimal_in_range(rho_bank_raw, min_val=Decimal("0"), max_val=Decimal("1"))
    if rho_lot_dec is None or Fraction(rho_lot_dec) < Fraction(Decimal(str(GATE_28A_RHO_LOT_MIN))):
        failure_reasons.append(
            f"INVALID_ICC_DOMAIN: rho_lot={rho_lot_raw!r} invalid or below prespecified conservative floor {GATE_28A_RHO_LOT_MIN}."
        )
        rho_lot = GATE_28A_RHO_LOT_MIN
        icc_domain_invalid = True
    else:
        rho_lot = float(rho_lot_dec)
    if rho_bank_dec is None or Fraction(rho_bank_dec) < Fraction(Decimal(str(GATE_28A_RHO_BANK_MIN))):
        failure_reasons.append(
            f"INVALID_ICC_DOMAIN: rho_bank={rho_bank_raw!r} invalid or below prespecified conservative floor {GATE_28A_RHO_BANK_MIN}."
        )
        rho_bank = GATE_28A_RHO_BANK_MIN
        icc_domain_invalid = True
    else:
        rho_bank = float(rho_bank_dec)
    if (
        rho_lot_dec is not None
        and rho_bank_dec is not None
        and (Fraction(rho_lot_dec) + Fraction(rho_bank_dec)) > Fraction(1, 1)
    ):
        failure_reasons.append(