OpenIslet

scripts/verify_iip0043_wholegraft_gate0v_and_calibration.py

IIP-0043 / DEC-122 (CAND-034 / EXP-031 — Bucket 1 Closure, Stage S3-R2 Remediated)

script Release v0.8.0 gen-eb955f8266035aba 1008 KB · 76 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_iip0043_wholegraft_gate0v_and_calibration.py · SHA-256: 20706180ed1ed6a2…

        bad_allo_proc_rec = build_authenticated_process_qualification_record(
            product_variant_id="ISLET1_A4_ARM1B",
            process_qual_id="PROCQUAL-MISAPPLIED-ARM1B",
        )
        write_record_to_trusted_qualification_store(store_json, bad_allo_proc_rec, category="process_qualifications")
        p_allo_on_auto = build_passing_reference_payload(
            product_variant_id="ISLET1_A4_ARM1B",
            qualification_tier="AUTOLOGOUS_D1B_PROCESS_QUAL",
            process_qualification_ref_sha256=bad_allo_proc_rec["sha256"],
            openislet_root=resolved_root,
        )
        _record(
            "r04_b1_allogeneic_arm1b_on_autologous_tier_with_matching_qa_record_held",
            "HOLD_INCOMPLETE_OR_UNDERPOWERED",
            p_allo_on_auto,
            "HOLD_INAPPLICABLE_QUALIFICATION_TIER_FOR_PRODUCT_FAMILY",
            store_path=store_json,
        )

        # 20H-R04-B1b. Autologous D1b submitted on BANK_QUALIFICATION or LOT_RELEASE_SURROGATE MUST fail closed!
        p_d1b_on_bank = build_passing_reference_payload(
            product_variant_id="ISLET1_D1_AUTO_V2_L1A_CIITA_CD64T_IL1RN",
            qualification_tier="BANK_QUALIFICATION",
            openislet_root=resolved_root,
        )
        _record(
            "r04_b1_autologous_d1b_on_bank_qualification_tier_held",
            "HOLD_INCOMPLETE_OR_UNDERPOWERED",
            p_d1b_on_bank,
            "HOLD_INAPPLICABLE_QUALIFICATION_TIER_FOR_PRODUCT_FAMILY",
            store_path=store_json,
        )

        # 20I-R04-B2. Exact Round-03 counterexample: external QA bank record omitting antiviral_genotype_stratum
        # + archived WT bank payload used for protective-LOF current lot -> MUST HOLD!
        archived_wt_bank = build_passing_reference_payload(
            lot_id="BANK-WT-ARCH-001",
            mcb_id="MCB-ISLET1-A-001",
            product_variant_id="ISLET1_A4_ARM1B",
            qualification_tier="BANK_QUALIFICATION",
            antiviral_genotype_stratum="TYK2_WT__IFIH1_WT_OR_A946T",
            openislet_root=resolved_root,
        )
        no_geno_bank_rec = build_authenticated_bank_qualification_record(
            mcb_id="MCB-ISLET1-A-001",
            product_variant_id="ISLET1_A4_ARM1B",
            bank_lot_id="BANK-WT-ARCH-001",
            bank_payload_sha256=archived_wt_bank["payload_sha256"],
        )
        no_geno_bank_rec.pop("antiviral_genotype_stratum", None)
        no_geno_bank_rec["sha256"] = compute_canonical_qualification_artifact_sha256(no_geno_bank_rec)
        write_record_to_trusted_qualification_store(
            store_json, no_geno_bank_rec, category="bank_qualifications", archived_payload=archived_wt_bank
        )
        p_lof_from_nogeno = build_passing_reference_payload(
            lot_id="LOT-LOF-SURR-001",
            mcb_id="MCB-ISLET1-A-001",
            product_variant_id="ISLET1_A4_ARM1B",
            qualification_tier="LOT_RELEASE_SURROGATE",
            bridged_mcb_qualification_sha256=no_geno_bank_rec["sha256"],
            antiviral_genotype_stratum="TYK2_WT__IFIH1_PROTECTIVE_LOF",
            openislet_root=resolved_root,
        )
        _record(
            "r04_b2_missing_bank_stratum_with_archived_wt_for_lof_lot_held",
            "HOLD_INCOMPLETE_OR_UNDERPOWERED",
            p_lof_from_nogeno,
            "HOLD_UNVERIFIED_OR_MISMATCHED_BANK_GENOTYPE_STRATUM",
            store_path=store_json,
        )

        # 20I-R04-B2b. Bank record claiming LOF while archived bank payload is WT -> MUST HOLD!
        mismatched_arch_rec = build_authenticated_bank_qualification_record(
            mcb_id="MCB-ISLET1-A-001",
            product_variant_id="ISLET1_A4_ARM1B",
            bank_lot_id="BANK-WT-ARCH-001",
            bank_payload_sha256=archived_wt_bank["payload_sha256"],
            antiviral_genotype_stratum="TYK2_WT__IFIH1_PROTECTIVE_LOF",
        )
        write_record_to_trusted_qualification_store(
            store_json, mismatched_arch_rec, category="bank_qualifications", archived_payload=archived_wt_bank
        )
        p_lof_from_mismatched_arch = build_passing_reference_payload(
            lot_id="LOT-LOF-SURR-002",
            mcb_id="MCB-ISLET1-A-001",
            product_variant_id="ISLET1_A4_ARM1B",
            qualification_tier="LOT_RELEASE_SURROGATE",
            bridged_mcb_qualification_sha256=mismatched_arch_rec["sha256"],
            antiviral_genotype_stratum="TYK2_WT__IFIH1_PROTECTIVE_LOF",
            openislet_root=resolved_root,
        )
        _record(
            "r04_b2_bank_record_stratum_contradicts_archived_wt_payload_held",
            "HOLD_INCOMPLETE_OR_UNDERPOWERED",
            p_lof_from_mismatched_arch,
            "HOLD_UNVERIFIED_OR_MISMATCHED_BANK_GENOTYPE_STRATUM",
            store_path=store_json,
        )

    # 20J-R04-B3a. Extra non-canonical lineage key inside required stress regime (clean counts -> HOLD; adverse counts -> FAIL_HARD_STOP!)
    p_extra_lin_clean = build_passing_reference_payload(openislet_root=resolved_root)
    p_extra_lin_clean["gate0v_kill_stress_ap1903"]["hypoxia_1pct_plus_cytokines"]["ductal_noncanonical"] = copy.deepcopy(
        p_extra_lin_clean["gate0v_kill_stress_ap1903"]["hypoxia_1pct_plus_cytokines"]["beta"]
    )
    _record(
        "r04_b3_extra_noncanonical_lineage_key_clean_counts_held",
        "HOLD_INCOMPLETE_OR_UNDERPOWERED",
        _seal(p_extra_lin_clean),
        "HOLD_UNRECOGNIZED_LINEAGE_KEY",
    )

    p_extra_lin_adv = copy.deepcopy(p_extra_lin_clean)
    p_extra_lin_adv["gate0v_kill_stress_ap1903"]["hypoxia_1pct_plus_cytokines"]["ductal_noncanonical"]["ap1903_24h_viable_survivors_k"] = 7000
    reconcile_fixture_per_unit_counts(p_extra_lin_adv)
    _record(
        "r04_b3_extra_noncanonical_lineage_key_adverse_raw_counts_hard_stop",
        "FAIL_HARD_STOP",
        _seal(p_extra_lin_adv),
        "AP1903_24H_KILL_BELOW_99P9PCT_HYPOXIA_1PCT_PLUS_CYTOKINES_DUCTAL_NONCANONICAL",
    )

    # 20J-R04-B3b. Zero-killing arm paired with poorly surviving vehicle control (veh_24h_viable_n=50,
    # ap1903_24h_viable_survivors_k=10000) MUST preserve FAIL_HARD_STOP (NOT return HOLD-only)!
    p_zero_kill_low_veh = build_passing_reference_payload(openislet_root=resolved_root)
    p_zero_kill_low_veh["gate0v_kill_stress_ap1903"]["er_stress_tg_or_tm"]["alpha"]["veh_24h_viable_n"] = 50
    p_zero_kill_low_veh["gate0v_kill_stress_ap1903"]["er_stress_tg_or_tm"]["alpha"]["ap1903_24h_viable_survivors_k"] = 10000
    reconcile_fixture_per_unit_counts(p_zero_kill_low_veh)
    _record(
        "r04_b3_zero_killing_with_low_vehicle_survival_preserves_hard_stop",
        "FAIL_HARD_STOP",
        _seal(p_zero_kill_low_veh),
        "AP1903_24H_KILL_BELOW_99P9PCT_ER_STRESS_TG_OR_TM_ALPHA",
    )

    # 20J-R04-B3c. Extra non-canonical lineage key inside pathogen day28_persistence_by_lineage with k2=1 -> FAIL_HARD_STOP!
    p_extra_path_lin = build_passing_reference_payload(openislet_root=resolved_root)
    extra_path_entry = copy.deepcopy(p_extra_path_lin["gate0v_b_infectious_challenge"]["pathogens"]["cvb4_e2"]["day28_persistence_by_lineage"]["beta"])
    extra_path_entry["stage1_screen_positive_k"] = 1
    extra_path_entry["stage2_sorted_for_confirmation_n"] = 1
    extra_path_entry["stage2_confirmed_infectious_k"] = 1
    p_extra_path_lin["gate0v_b_infectious_challenge"]["pathogens"]["cvb4_e2"]["day28_persistence_by_lineage"]["ductal_noncanonical"] = extra_path_entry
    _record(
        "r04_b3_extra_noncanonical_pathogen_lineage_viral_pos_hard_stop",
        "FAIL_HARD_STOP",
        _seal(p_extra_path_lin),
        "CONFIRMED_VIRAL_POSITIVE_CVB4_E2_DUCTAL_NONCANONICAL_K_1",
    )

    # 21. R11-B3: Clustering design effect deff = 2.0 routes to HOLD_CLUSTERED_DEFF_GT_1_REQUIRES_CLUSTER_LEVEL_INFERENCE
    #     (and also flags reduced effective n INSUFFICIENT_DENOMINATOR diagnostically; exact_directional_confidence_certified=False)!
    p_deff = build_passing_reference_payload(openislet_root=resolved_root)
    p_deff["clustering_and_independence_spec"]["clustering_design_effect_deff"] = "2.0"
    r_deff_21 = _record(
        "clustering_deff_2p0_reduces_neff_held",
        "HOLD_CLUSTERED_DEFF_GT_1_REQUIRES_CLUSTER_LEVEL_INFERENCE",
        _seal(p_deff),
        "INSUFFICIENT_DENOMINATOR",
    )
    if not (
        r_deff_21.get("exact_directional_confidence_certified") is False
        and r_deff_21.get("release_authorizing") is False
        and "HOLD_CLUSTERED_DEFF_GT_1_REQUIRES_CLUSTER_LEVEL_INFERENCE" in r_deff_21.get("holds", [])
    ):
        cases[-1]["passed"] = False

    # 22. Exact 3-pathogen boundary (15,949 PASS vs 15,948 HOLD at 29/29)
    alpha_3p = _alpha_3pathogen_global_floor(prec=120)
    eta_3p_29 = exact_clopper_pearson_lower_calibrant(29, 29, alpha_3p, prec=80)
    n_3p_29 = min_n_for_zero_event_cap(Decimal("0.0005"), alpha_3p, eta_3p_29, prec=80)["min_n_pass"]
    p_3p_pass = build_passing_reference_payload(openislet_root=resolved_root)
    p_3p_pass["gate0v_b_infectious_challenge"]["pathogens"]["cvb3"]["day28_persistence_by_lineage"]["plvap_iec"]["cells_scored_n"] = n_3p_29
    reconcile_fixture_per_unit_counts(p_3p_pass)
    _record(f"exact_3pathogen_boundary_pass_n_{n_3p_29}", "PASS_WHOLEGRAFT_BANK_QUALIFICATION", _seal(p_3p_pass))

    p_3p_hold = build_passing_reference_payload(openislet_root=resolved_root)
    p_3p_hold["gate0v_b_infectious_challenge"]["pathogens"]["cvb3"]["day28_persistence_by_lineage"]["plvap_iec"]["cells_scored_n"] = n_3p_29 - 1
    reconcile_fixture_per_unit_counts(p_3p_hold)
    _record(f"exact_3pathogen_boundary_hold_n_{n_3p_29 - 1}", "HOLD_INCOMPLETE_OR_UNDERPOWERED", _seal(p_3p_hold), "INSUFFICIENT_DENOMINATOR_CVB3_PLVAP_IEC")

    # 23. TYK2_P1104A_HET paired non-inferiority with >= 6 replicates passes, < 6 holds
    p_tyk2_ok = build_passing_reference_payload(
        antiviral_genotype_stratum="TYK2_P1104A_HET__IFIH1_WT",
        openislet_root=resolved_root,
    )
    _record("tyk2_het_noninferiority_n6_passes", "PASS_WHOLEGRAFT_BANK_QUALIFICATION", p_tyk2_ok)

    p_tyk2_lown = copy.deepcopy(p_tyk2_ok)
    p_tyk2_lown["gate0v_b_infectious_challenge"]["pathogens"]["cvb4_e2"]["paired_noninferiority_replicates_n"] = 2
    _record("tyk2_het_noninferiority_n2_held", "HOLD_INCOMPLETE_OR_UNDERPOWERED", _seal(p_tyk2_lown), "UNDERPOWERED_NONINFERIORITY_REPLICATES")

    # 24. Hostile ambient Decimal prec=4, ROUND_FLOOR, Inexact trap ENABLED on FULL evaluator!
    p_ctx = build_passing_reference_payload(openislet_root=resolved_root)
    res_clean = evaluate_wholegraft_gate0v_and_stress_ap1903(p_ctx, openislet_root=resolved_root)
    ctx_trapped = False
    res_polluted = None
    _clear_math_caches()
    with localcontext() as ctx:
        ctx.prec = 4
        ctx.rounding = ROUND_FLOOR
        ctx.traps[decimal.Inexact] = True
        try:
            res_polluted = evaluate_wholegraft_gate0v_and_stress_ap1903(p_ctx, openislet_root=resolved_root)
        except decimal.Inexact:
            ctx_trapped = True
    ctx_identical = (not ctx_trapped) and (json.dumps(res_clean, sort_keys=True) == json.dumps(res_polluted, sort_keys=True))
    cases.append({
        "case": "ambient_decimal_prec4_floor_inexact_trap_full_evaluator_immunity",
        "expected_verdict": "IDENTICAL_BYTE_EXACT_OUTPUT",
        "actual_verdict": "IDENTICAL_BYTE_EXACT_OUTPUT" if ctx_identical else f"DRIFT_OR_TRAP(trapped={ctx_trapped})",
        "expected_token": None,
        "hard_stops": [],
        "holds": [],
        "passed": ctx_identical,
    })

    # 25. Speculative CXADR KO True -> FAIL_HARD_STOP
    p_cx = build_passing_reference_payload(openislet_root=resolved_root)
    p_cx["cxadr_ko_present"] = True
    _record("speculative_cxadr_ko_barred", "FAIL_HARD_STOP", _seal(p_cx), "SPECULATIVE_CXADR_KO_BARRED")

    # 26. Raw 24-h AP1903 survivors causing kill_24h < 99.9% -> FAIL_HARD_STOP
    p_24f = build_passing_reference_payload(openislet_root=resolved_root)
    p_24f["gate0v_kill_stress_ap1903"]["hypoxia_1pct_plus_cytokines"]["alpha"]["ap1903_24h_viable_survivors_k"] = 30
    reconcile_fixture_per_unit_counts(p_24f)
    _record("raw_24h_kill_drop_below_99p9pct_hard_stop", "FAIL_HARD_STOP", _seal(p_24f), "AP1903_24H_KILL_BELOW_99P9PCT_HYPOXIA_1PCT_PLUS_CYTOKINES_ALPHA")

    # 27. rk = 0 fragile spike calibrant -> deterministic HOLD, zero crash
    p_rk0 = build_passing_reference_payload(openislet_root=resolved_root)
    p_rk0["gate0v_b_infectious_challenge"]["pathogens"]["cvb4_e2"]["day28_persistence_by_lineage"]["pericyte"]["fragile_infected_spike_detected_k"] = 0
    reconcile_fixture_per_unit_counts(p_rk0)
    _record("rk0_fragile_calibrant_deterministic_hold", "HOLD_INCOMPLETE_OR_UNDERPOWERED", _seal(p_rk0), "CALIBRANT_SENSITIVITY_BELOW_0P80_CVB4_E2_PERICYTE")

    # 28. Legacy beta-only payload rejected
    p_beta_only = build_passing_reference_payload(openislet_root=resolved_root)
    p_beta_only["gate0v_b_infectious_challenge"]["pathogens"]["cvb4_e2"]["day28_persistence_by_lineage"] = {
        "beta": p_beta_only["gate0v_b_infectious_challenge"]["pathogens"]["cvb4_e2"]["day28_persistence_by_lineage"]["beta"]
    }
    _record("legacy_beta_only_rejected", "HOLD_INCOMPLETE_OR_UNDERPOWERED", _seal(p_beta_only), "MISSING_LINEAGE_CVB4_E2_ALPHA")

    # ------------------------------------------------------------------
    # Round-06 Final-Closure Adversarial Regression Suite (B1-B4 & M2-M3)
    # ------------------------------------------------------------------
    # 29. R06-B1a: Indirect-only marginal conditional escape NEVER issues PASS -> HOLD_INDIRECT_CONDITIONAL_ESCAPE_UNSUPPORTED_FOR_RELEASE!
    p_ind_only = build_passing_reference_payload(use_direct_infected_veh_survivor_mode=False, openislet_root=resolved_root)
    _record(
        "r06_b1_indirect_only_conditional_escape_held_never_pass",
        "HOLD_INCOMPLETE_OR_UNDERPOWERED",
        p_ind_only,
        "HOLD_INDIRECT_CONDITIONAL_ESCAPE_UNSUPPORTED_FOR_RELEASE",
    )