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…

        cases.append({"case": "b2_gate28a_skewed_20_plus_1_single_bank_21of21_fails", "passed": True, "n_eff": g28a_skew["n_eff"]})

        # 13. B2 balanced 3-lot 21/21 from a SINGLE bank STILL FAILS (single bank + n_eff < 21)!
        single_bank_21 = [
            {
                "recipient_id": f"SB-{i:02d}",
                "lot_id": f"LOT-{(i - 1) // 7 + 1}",
                "mcb_id": "MCB-ONLY",
                "followup_months": 12.0,
                "met_c2b_criteria": True,
                "intercurrent_event": "NONE",
            }
            for i in range(1, 22)
        ]
        p_sb, _ = build_valid_gate28a_reference_bundle(clin_store_json, single_bank_21, cohort_id="COHORT-SB-21")
        g28a_sb = evaluate_gate28a_h1_promotion(p_sb, trusted_clinical_store=clin_store_json)
        if g28a_sb["promotion_authorized"] is not False:
            raise ValueError(f"B2 single-bank 21/21 MUST fail closed: {g28a_sb}")
        cases.append({"case": "b2_gate28a_single_bank_3_lots_21of21_fails", "passed": True, "n_eff": g28a_sb["n_eff"]})

        # 14. B2 consistent single-bank 30/30 across 6 lots holds at n_eff=16.853933!
        single_bank_30 = [
            {
                "recipient_id": f"SB30-{i:02d}",
                "lot_id": f"LOT-{(i - 1) // 5 + 1}",
                "mcb_id": "MCB-SINGLE",
                "followup_months": 12.0,
                "met_c2b_criteria": True,
                "intercurrent_event": "NONE",
            }
            for i in range(1, 31)
        ]
        p_sb30, _ = build_valid_gate28a_reference_bundle(clin_store_json, single_bank_30, cohort_id="COHORT-SB-30")
        g28a_sb30 = evaluate_gate28a_h1_promotion(p_sb30, trusted_clinical_store=clin_store_json)
        if g28a_sb30["promotion_authorized"] is not False or g28a_sb30["distinct_mcb_n"] != 1 or abs(g28a_sb30["n_eff"] - 16.853933) > 1e-5:
            raise ValueError(f"B2 single-bank 30/30 check failed: {g28a_sb30}")
        cases.append({"case": "b2_gate28a_single_bank_6_lots_30of30_holds", "passed": True, "n_eff": g28a_sb30["n_eff"]})

        # 15. B2 well-replicated 45/45 across 15 lots (3 each) and 5 MCBs (9 each) WITH independent trusted_clinical_store
        #     (clears ALL THREE prespecified ICC scenarios) PROMOTES! (good_30 6-lot/3-MCB kept for moderate-ICC HOLD & negative controls.)
        good_30 = [
            {
                "recipient_id": f"G-{i:02d}",
                "lot_id": f"LOT-{(i - 1) // 5 + 1}",
                "mcb_id": f"MCB-{(i - 1) // 10 + 1}",
                "followup_months": 12.0,
                "met_c2b_criteria": True,
                "intercurrent_event": "NONE",
            }
            for i in range(1, 31)
        ]
        robust_45 = [
            {
                "recipient_id": f"G45-{i:02d}",
                "lot_id": f"LOT-{(i - 1) // 3 + 1}",
                "mcb_id": f"MCB-{(i - 1) // 9 + 1}",
                "followup_months": 12.0,
                "met_c2b_criteria": True,
                "intercurrent_event": "NONE",
            }
            for i in range(1, 46)
        ]
        p_good_json, good_recs = build_valid_gate28a_reference_bundle(clin_store_json, robust_45, cohort_id="COHORT-ROBUST-45")
        g28a_ok = evaluate_gate28a_h1_promotion(p_good_json, trusted_clinical_store=clin_store_json)
        if not (
            g28a_ok["promotion_authorized"] is True
            and g28a_ok["status"] == "PROMOTE_H1_88_TO_90"
            and g28a_ok["external_clinical_store_verified"] is True
            and g28a_ok["lot_bank_hierarchy_valid"] is True
            and g28a_ok["lot_to_mcb_manifest_verified"] is True
            and g28a_ok["parallel_cmc_ledger_verified"] is True
            and g28a_ok["icc_sensitivity_report_attached"] is True
            and g28a_ok["icc_sensitivity_review_status"] == "ROBUST_ACROSS_ALL_THREE_PRESPECIFIED_ICC_SCENARIOS"
        ):
            raise ValueError(f"B2 well-replicated 45/45 multi-bank cohort with trusted store should promote: {g28a_ok}")
        cases.append({"case": "b2_gate28a_replicated_45of45_15lots_5mcbs_promotes_with_trusted_store", "passed": True, "n_eff": g28a_ok["n_eff"]})

        # 16. R06 Blocking 2: Directory-layout trusted_clinical_store promotes identically to JSON registry store!
        write_gate28a_trusted_clinical_store(
            clin_store_dir,
            prespecification_attestations=[good_recs["prespecification_attestation"]],
            source_verification_attestations=[good_recs["source_verification_attestation"]],
            verified_lot_to_bank_manifests=[good_recs["verified_lot_to_bank_manifest"]],
            parallel_cmc_readiness_ledgers=[good_recs["parallel_cmc_readiness_ledger_bytes"]],
        )
        g28a_ok_dir = evaluate_gate28a_h1_promotion(p_good_json, trusted_clinical_store=clin_store_dir)
        if not (g28a_ok_dir["promotion_authorized"] is True and g28a_ok_dir["status"] == "PROMOTE_H1_88_TO_90" and g28a_ok_dir["external_clinical_store_verified"] is True):
            raise ValueError(f"R06 B2 directory trusted_clinical_store should promote: {g28a_ok_dir}")
        cases.append({"case": "b2_r06_gate28a_directory_store_promotes_identically", "passed": True})

        # 17. R06 Blocking 2: Self-attested inline records WITHOUT trusted_clinical_store MUST FAIL CLOSED!
        p_inline_only = build_valid_gate28a_cohort_payload(good_30, cohort_id="COHORT-INLINE-ONLY")
        g28a_inline = evaluate_gate28a_h1_promotion(p_inline_only, trusted_clinical_store=None)
        if not (
            g28a_inline["promotion_authorized"] is False
            and g28a_inline["status"] == "HOLD_H1_AT_88PCT"
            and g28a_inline["external_clinical_store_verified"] is False
            and g28a_inline["self_attested_inline_records_rejected"] is True
        ):
            raise ValueError(f"R06 B2 inline-only self-attested Gate 28-A MUST fail closed: {g28a_inline}")
        cases.append({"case": "b2_r06_gate28a_reject_inline_only_without_trusted_clinical_store", "passed": True})

        # 18. R06 Blocking 2: Post-attestation mutation of compatibility, overlap, ICC, or CMC hash MUST FAIL CLOSED!
        for mut_lbl, mut_key, mut_val in (
            ("transplant_site", "transplant_site", "ABDOMINAL_ANTERIOR_RECTUS_SHEATH"),
            ("includes_reichman", "includes_reichman", True),
            ("rho_lot", "rho_lot", 0.06),
            ("rho_bank", "rho_bank", 0.03),
            ("cmc_sha", "parallel_cmc_readiness_ledger_sha256", "0" * 64),
        ):
            p_mut = copy.deepcopy(p_good_json)
            p_mut[mut_key] = mut_val
            r_mut = evaluate_gate28a_h1_promotion(p_mut, trusted_clinical_store=clin_store_json)
            if r_mut["promotion_authorized"] is not False or r_mut["external_clinical_store_verified"] is not False:
                raise ValueError(f"R06 B2 post-attestation mutation ({mut_lbl}) MUST fail closed: {r_mut}")
        cases.append({"case": "b2_r06_gate28a_reject_post_attestation_mutation_of_declarations_and_icc", "passed": True})

        # 19. R06 Blocking 2: Missing, tampered, or unreconciled PARALLEL_CMC_READINESS_LEDGER bytes MUST FAIL CLOSED!
        # (a) Tampered bytes in store -> SHA-256 mismatch
        tampered_store = tmp_dir_p / "tampered_cmc_store.json"
        shutil_doc = load_strict_json_bytes(clin_store_json.read_bytes(), "copy_store")
        csha = good_recs["parallel_cmc_readiness_ledger_sha256"]
        shutil_doc["parallel_cmc_readiness_ledgers"][csha]["raw_payload_utf8"] += " "
        tampered_store.write_text(json.dumps(shutil_doc, indent=2, sort_keys=True) + "\n", encoding="utf-8")
        r_tamp_cmc = evaluate_gate28a_h1_promotion(p_good_json, trusted_clinical_store=tampered_store)
        # (b) Valid CMC bytes whose recipient list drops 1 clinical recipient -> reconciliation fails!
        unrec_store = tmp_dir_p / "unreconciled_cmc_store.json"
        partial_cmc = _derive_reconciling_cmc_lot_ledger_from_recipients(
            good_30[:-1], {f"LOT-{j}": f"MCB-{(j - 1) // 2 + 1}" for j in range(1, 7)}
        )
        p_unrec, _ = build_valid_gate28a_reference_bundle(
            unrec_store, good_30, cohort_id="COHORT-UNREC-CMC", parallel_cmc_lot_ledger=partial_cmc
        )
        r_unrec_cmc = evaluate_gate28a_h1_promotion(p_unrec, trusted_clinical_store=unrec_store)
        if not (
            r_tamp_cmc["promotion_authorized"] is False
            and r_tamp_cmc["parallel_cmc_ledger_verified"] is False
            and r_unrec_cmc["promotion_authorized"] is False
            and r_unrec_cmc["parallel_cmc_ledger_verified"] is False
        ):
            raise ValueError(f"R06 B2 tampered/unreconciled CMC ledger bytes MUST fail closed: {r_tamp_cmc} / {r_unrec_cmc}")
        cases.append({"case": "b2_r06_gate28a_reject_tampered_or_unreconciled_cmc_ledger_bytes", "passed": True})

        # 20. B2 adversarial counterexample: conflicting mcb_id inside same lot -> MUST HOLD BEFORE DEFF/n_eff!
        conflicting_mcb_30 = [
            {
                "recipient_id": f"CM-{i:02d}",
                "lot_id": f"LOT-{(i - 1) // 5 + 1}",
                "mcb_id": f"MCB-{(i - 1) % 3 + 1}",
                "followup_months": 12.0,
                "met_c2b_criteria": True,
                "intercurrent_event": "NONE",
            }
            for i in range(1, 31)
        ]
        g28a_conf_mcb = evaluate_gate28a_h1_promotion(
            build_valid_gate28a_cohort_payload(
                conflicting_mcb_30,
                cohort_id="COHORT-CONFLICT-MCB-30",
                lot_to_mcb_manifest={f"LOT-{j}": "MCB-1" for j in range(1, 7)},
            )
        )
        if (
            g28a_conf_mcb["promotion_authorized"] is not False
            or g28a_conf_mcb["status"] != "HOLD_H1_AT_88PCT"
            or g28a_conf_mcb["lot_bank_hierarchy_valid"] is not False
            or g28a_conf_mcb["deff"] is not None
            or g28a_conf_mcb["n_eff"] is not None
        ):
            raise ValueError(f"B2 conflicting lot->MCB cohort MUST fail closed before DEFF/n_eff: {g28a_conf_mcb}")
        cases.append({"case": "b2_gate28a_reject_conflicting_mcb_within_same_lot_before_deff", "passed": True})

        # 21. B2 adversarial: conflicting wcb_id within the same lot_id MUST fail closed before DEFF/n_eff!
        conflicting_wcb_30 = copy.deepcopy(good_30)
        for idx_r, rec in enumerate(conflicting_wcb_30):
            rec["wcb_id"] = "WCB-ALT" if idx_r == 1 else f"WCB-FOR-{rec['lot_id']}"
        g28a_conf_wcb = evaluate_gate28a_h1_promotion(
            build_valid_gate28a_cohort_payload(
                conflicting_wcb_30,
                cohort_id="COHORT-CONFLICT-WCB-30",
                lot_to_mcb_manifest={f"LOT-{j}": f"MCB-{(j - 1) // 2 + 1}" for j in range(1, 7)},
                lot_to_wcb_manifest={f"LOT-{j}": f"WCB-FOR-LOT-{j}" for j in range(1, 7)},
            )
        )
        if (
            g28a_conf_wcb["promotion_authorized"] is not False
            or g28a_conf_wcb["lot_bank_hierarchy_valid"] is not False
            or g28a_conf_wcb["deff"] is not None
            or g28a_conf_wcb["n_eff"] is not None
        ):
            raise ValueError(f"B2 conflicting lot->WCB cohort MUST fail closed before DEFF/n_eff: {g28a_conf_wcb}")
        cases.append({"case": "b2_gate28a_reject_conflicting_wcb_within_same_lot_before_deff", "passed": True})

        # 22. R06 Blocking 3a: Omitted or mismatched lot_to_mcb_manifest MUST return lot_bank_hierarchy_valid=False, deff=None, n_eff=None!
        p_no_manifest = copy.deepcopy(p_good_json)
        p_no_manifest.pop("lot_to_mcb_manifest")
        g28a_noman = evaluate_gate28a_h1_promotion(p_no_manifest, trusted_clinical_store=clin_store_json)
        g28a_noman_nostore = evaluate_gate28a_h1_promotion(p_no_manifest, trusted_clinical_store=None)
        p_bad_manifest = build_valid_gate28a_cohort_payload(
            good_30,
            cohort_id="COHORT-BADMAN",
            lot_to_mcb_manifest={f"LOT-{j}": ("MCB-WRONG" if j == 1 else f"MCB-{(j - 1) // 2 + 1}") for j in range(1, 7)},
        )
        g28a_badman = evaluate_gate28a_h1_promotion(p_bad_manifest)
        for lbl_m, res_m in (("no_manifest_store", g28a_noman), ("no_manifest_nostore", g28a_noman_nostore), ("bad_manifest", g28a_badman)):
            if not (
                res_m["promotion_authorized"] is False
                and res_m["status"] == "HOLD_H1_AT_88PCT"
                and res_m["lot_bank_hierarchy_valid"] is False
                and res_m["deff"] is None
                and res_m["n_eff"] is None
                and res_m["k_eff"] is None
            ):
                raise ValueError(f"R06 B3 {lbl_m} MUST return lot_bank_hierarchy_valid=False & null deff/n_eff/k_eff: {res_m}")
        cases.append({"case": "b3_r06_gate28a_missing_or_mismatched_mcb_manifest_returns_null_cluster_outputs", "passed": True})

        # 23. R06 Blocking 3b: Recipients declaring wcb_id without matching lot_to_wcb_manifest return null cluster outputs; valid store-verified WCB promotes!
        wcb_good_30 = copy.deepcopy(good_30)
        for rec in wcb_good_30:
            rec["wcb_id"] = f"WCB-FOR-{rec['lot_id']}"
        wcb_robust_45 = copy.deepcopy(robust_45)
        for rec in wcb_robust_45:
            rec["wcb_id"] = f"WCB-FOR-{rec['lot_id']}"
        p_wcb_ok, _ = build_valid_gate28a_reference_bundle(clin_store_json, wcb_robust_45, cohort_id="COHORT-WCB-OK-45")
        g28a_wcb_ok = evaluate_gate28a_h1_promotion(p_wcb_ok, trusted_clinical_store=clin_store_json)
        p_wcb_missing = copy.deepcopy(p_wcb_ok)
        p_wcb_missing.pop("lot_to_wcb_manifest")
        g28a_wcb_miss = evaluate_gate28a_h1_promotion(p_wcb_missing, trusted_clinical_store=clin_store_json)
        p_wcb_mismatch = copy.deepcopy(p_wcb_ok)
        p_wcb_mismatch["lot_to_wcb_manifest"]["LOT-1"] = "WCB-WRONG"
        g28a_wcb_mis = evaluate_gate28a_h1_promotion(p_wcb_mismatch, trusted_clinical_store=clin_store_json)
        if not (
            g28a_wcb_ok["promotion_authorized"] is True
            and g28a_wcb_ok["lot_bank_hierarchy_valid"] is True
            and g28a_wcb_ok["lot_to_wcb_manifest_verified"] is True
            and g28a_wcb_miss["lot_bank_hierarchy_valid"] is False
            and g28a_wcb_miss["deff"] is None
            and g28a_wcb_miss["n_eff"] is None
            and g28a_wcb_mis["lot_bank_hierarchy_valid"] is False
            and g28a_wcb_mis["deff"] is None
            and g28a_wcb_mis["n_eff"] is None
        ):
            raise ValueError(f"R06 B3 WCB provenance checks failed: ok={g28a_wcb_ok}, miss={g28a_wcb_miss}, mis={g28a_wcb_mis}")
        cases.append({"case": "b3_r06_gate28a_wcb_provenance_enforced_and_missing_returns_null_outputs", "passed": True})

        # 24. B2 adversarial: bare booleans without attestations OR attestation bound to wrong cohort/digest MUST fail closed!
        p_bare_bools = {
            "cohort_id": "COHORT-BARE",
            **GATE_28A_COMPATIBILITY_TARGET,
            "includes_reichman": False,