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…

        f_nested_dup = td_r09_p / "r09_nested_dup.json"
        f_nested_dup.write_text(
            r09_raw_bank.replace(
                '"gate0v_a_poly_ic_sensing": {',
                '"gate0v_a_poly_ic_sensing": {"poly_ic_dose_ng_ml": 0, ',
                1,
            ),
            encoding="utf-8",
        )
        f_uni_dup = td_r09_p / "r09_uni_dup.json"
        f_uni_dup.write_text(
            '{"lot_\\u0069d": "FAKE-LOT", ' + r09_raw_bank[1:],
            encoding="utf-8",
        )
        r09_dup_ok = True
        r09_dup_holds: List[str] = []
        for dup_p in (f_top_dup, f_nested_dup, f_uni_dup):
            res_dp, rc_dp = evaluate_wholegraft_gate0v_lot_file(dup_p, openislet_root=resolved_root)
            r09_dup_holds = res_dp.get("holds") or []
            if not (
                rc_dp == 2
                and res_dp.get("verdict") == "HOLD_INCOMPLETE_OR_UNDERPOWERED"
                and "HOLD_GATE0V_AMBIGUOUS_OR_NONCANONICAL_LOT_JSON:DUPLICATE_JSON_KEY" in r09_dup_holds
            ):
                r09_dup_ok = False
        cases.append({
            "case": "r09_b1_standalone_lot_file_top_nested_unicode_duplicate_keys_hold_exit_2",
            "expected_verdict": "HOLD_INCOMPLETE_OR_UNDERPOWERED",
            "actual_verdict": "HOLD_INCOMPLETE_OR_UNDERPOWERED" if r09_dup_ok else "UNEXPECTED_ACCEPT",
            "expected_token": "HOLD_GATE0V_AMBIGUOUS_OR_NONCANONICAL_LOT_JSON:DUPLICATE_JSON_KEY",
            "hard_stops": [],
            "holds": r09_dup_holds[:3],
            "passed": r09_dup_ok,
        })

        # 44. R09-B1 (b): Standalone lot file intake rejects NaN, Infinity, -Infinity with structured HOLD exit 2!
        r09_const_ok = True
        r09_const_holds: List[str] = []
        for idx_c, tok_c in enumerate(("NaN", "Infinity", "-Infinity")):
            f_c = td_r09_p / f"r09_const_{idx_c}.json"
            f_c.write_text(f'{{"lot_id": "BANK-R09-C", "bad_val": {tok_c}}}', encoding="utf-8")
            res_c, rc_c = evaluate_wholegraft_gate0v_lot_file(f_c, openislet_root=resolved_root)
            r09_const_holds = res_c.get("holds") or []
            if not (
                rc_c == 2
                and res_c.get("verdict") == "HOLD_INCOMPLETE_OR_UNDERPOWERED"
                and "HOLD_GATE0V_AMBIGUOUS_OR_NONCANONICAL_LOT_JSON:NON_STANDARD_JSON_CONSTANT" in r09_const_holds
            ):
                r09_const_ok = False
        cases.append({
            "case": "r09_b1_standalone_lot_file_nan_infinity_constants_hold_exit_2",
            "expected_verdict": "HOLD_INCOMPLETE_OR_UNDERPOWERED",
            "actual_verdict": "HOLD_INCOMPLETE_OR_UNDERPOWERED" if r09_const_ok else "UNEXPECTED_ACCEPT",
            "expected_token": "HOLD_GATE0V_AMBIGUOUS_OR_NONCANONICAL_LOT_JSON:NON_STANDARD_JSON_CONSTANT",
            "hard_stops": [],
            "holds": r09_const_holds[:3],
            "passed": r09_const_ok,
        })

        # 45. R09-B1 (c): External trusted qualification store rejects duplicate keys AND NaN across
        #     registry JSON, directory bank record JSON, directory bank archive JSON, and directory
        #     autologous process-qual archive JSON!
        r09_brec = build_authenticated_bank_qualification_record(
            mcb_id="MCB-ISLET1-A-R09",
            product_variant_id="ISLET1_A4_ARM1B",
            bank_lot_id="BANK-R09-001",
            bank_payload_sha256=r09_g0v_bank["payload_sha256"],
            antiviral_genotype_stratum=r09_g0v_bank["antiviral_genotype_stratum"],
        )
        r09_surr = build_passing_reference_payload(
            lot_id="LOT-R09-SURR-001",
            mcb_id="MCB-ISLET1-A-R09",
            product_variant_id="ISLET1_A4_ARM1B",
            qualification_tier="LOT_RELEASE_SURROGATE",
            bridged_mcb_qualification_sha256=r09_brec["sha256"],
            openislet_root=resolved_root,
        )
        reg_dup_r09 = td_r09_p / "r09_store_reg_dup.json"
        write_record_to_trusted_qualification_store(
            reg_dup_r09, r09_brec, category="bank_qualifications", archived_payload=r09_g0v_bank
        )
        reg_dup_r09.write_text(
            reg_dup_r09.read_text(encoding="utf-8").replace(
                '"operational_tier": "BANK_QUALIFICATION"',
                '"operational_tier": "LOT_RELEASE_SURROGATE", "operational_tier": "BANK_QUALIFICATION"',
                1,
            ),
            encoding="utf-8",
        )
        dir_rec_dup_r09 = td_r09_p / "r09_store_dir_rec_dup"
        write_record_to_trusted_qualification_store(
            dir_rec_dup_r09, r09_brec, category="bank_qualifications", archived_payload=r09_g0v_bank
        )
        rec_f_r09 = dir_rec_dup_r09 / "bank_qualifications" / f"{r09_brec['sha256']}.json"
        rec_f_r09.write_text(
            '{"verdict": "HOLD_FAKE", ' + rec_f_r09.read_text(encoding="utf-8").lstrip()[1:],
            encoding="utf-8",
        )
        dir_arch_dup_r09 = td_r09_p / "r09_store_dir_arch_dup"
        write_record_to_trusted_qualification_store(
            dir_arch_dup_r09, r09_brec, category="bank_qualifications", archived_payload=r09_g0v_bank
        )
        arch_f_r09 = dir_arch_dup_r09 / "archived_payloads" / f"{r09_g0v_bank['payload_sha256']}.json"
        arch_f_r09.write_text(
            '{"qualification_tier": "LOT_RELEASE_SURROGATE", ' + arch_f_r09.read_text(encoding="utf-8").lstrip()[1:],
            encoding="utf-8",
        )
        dir_arch_nan_r09 = td_r09_p / "r09_store_dir_arch_nan"
        write_record_to_trusted_qualification_store(
            dir_arch_nan_r09, r09_brec, category="bank_qualifications", archived_payload=r09_g0v_bank
        )
        arch_nan_f = dir_arch_nan_r09 / "archived_payloads" / f"{r09_g0v_bank['payload_sha256']}.json"
        arch_nan_f.write_text(
            '{"nan_probe": NaN, ' + arch_nan_f.read_text(encoding="utf-8").lstrip()[1:],
            encoding="utf-8",
        )
        r09_store_ok = True
        r09_store_holds: List[str] = []
        for st_r09 in (reg_dup_r09, dir_rec_dup_r09, dir_arch_dup_r09, dir_arch_nan_r09):
            res_st = evaluate_wholegraft_gate0v_and_stress_ap1903(
                r09_surr, trusted_qualification_store=st_r09, openislet_root=resolved_root
            )
            r09_store_holds = res_st.get("holds") or []
            pqa_st = (res_st.get("lot_binding") or {}).get("prior_qualification_authentication") or {}
            if not (
                res_st.get("verdict") == "HOLD_INCOMPLETE_OR_UNDERPOWERED"
                and "HOLD_UNVERIFIED_EXTERNAL_APPROVAL_STORE" in r09_store_holds
                and pqa_st.get("archived_bank_payload_verified") is False
                and pqa_st.get("archived_bank_payload_live_revalidation_passed") is False
                and any("AMBIGUOUS_OR_NONCANONICAL_JSON" in h for h in r09_store_holds)
            ):
                r09_store_ok = False
        # Also test autologous process-qualification directory archive with duplicate key!
        proc_rec_r09 = build_authenticated_process_qualification_record(
            product_variant_id="ISLET1_D1_AUTO_V2_L1B_CIITA_CD64T_IL1RN",
            process_qual_id="PQ-R09-001",
            process_qual_payload_sha256=r09_g0v_bank["payload_sha256"],
        )
        dir_proc_dup = td_r09_p / "r09_store_proc_arch_dup"
        write_record_to_trusted_qualification_store(
            dir_proc_dup, proc_rec_r09, category="process_qualifications", archived_payload=r09_g0v_bank
        )
        proc_arch_f = dir_proc_dup / "archived_payloads" / f"{r09_g0v_bank['payload_sha256']}.json"
        proc_arch_f.write_text(
            '{"lot_id": "FAKE", ' + proc_arch_f.read_text(encoding="utf-8").lstrip()[1:],
            encoding="utf-8",
        )
        pq_proc_ok, pq_proc_errs, _pq_proc_meta = resolve_and_verify_trusted_process_qualification(
            dir_proc_dup,
            expected_sha256=proc_rec_r09["sha256"],
            expected_product_variant_id="ISLET1_D1_AUTO_V2_L1B_CIITA_CD64T_IL1RN",
            openislet_root=resolved_root,
        )
        if not (pq_proc_ok is False and "HOLD_UNVERIFIED_EXTERNAL_APPROVAL_STORE" in pq_proc_errs):
            r09_store_ok = False
        cases.append({
            "case": "r09_b1_external_store_registry_record_bank_and_proc_archives_dup_key_nan_hold",
            "expected_verdict": "HOLD_INCOMPLETE_OR_UNDERPOWERED",
            "actual_verdict": "HOLD_INCOMPLETE_OR_UNDERPOWERED" if r09_store_ok else "UNEXPECTED_ACCEPT",
            "expected_token": "HOLD_UNVERIFIED_EXTERNAL_APPROVAL_STORE",
            "hard_stops": [],
            "holds": r09_store_holds[:4],
            "passed": r09_store_ok,
        })

    # 46. R09-B2 + R15-M1: Live Gate 0V-Kill arm with 90% drug (900/1,000) vs 70% vehicle (700/1,000)
    #     survival at component tails alpha_treated=alpha_veh=0.025 reports valid unclamped ordered
    #     directional limits (L ~ 1.20793 <= U ~ 1.36889, negative kill L95) and FAIL_HARD_STOP!
    p_90_70 = copy.deepcopy(r09_g0v_bank)
    arm_90_70 = p_90_70["gate0v_kill_stress_ap1903"]["normoxia_baseline"]["alpha"]
    arm_90_70["veh_24h_viable_n"] = 700
    arm_90_70["veh_24h_input_n"] = 1000
    arm_90_70["ap1903_24h_input_n"] = 1000
    arm_90_70["ap1903_24h_viable_survivors_k"] = 900
    reconcile_fixture_per_unit_counts(p_90_70)
    _seal(p_90_70)
    r_90_70 = evaluate_wholegraft_gate0v_and_stress_ap1903(p_90_70, openislet_root=resolved_root)
    det_90_70 = r_90_70["stress_ap1903_regimes"]["normoxia_baseline"]["lineages"]["alpha"]
    l_90_70_d = Decimal(det_90_70["recomputed_l_ratio_24h_joint_lower"])
    u_90_70_d = Decimal(det_90_70["recomputed_u_ratio_24h_joint_upper"])
    ok_90_70_live = bool(
        r_90_70["verdict"] == "FAIL_HARD_STOP"
        and l_90_70_d <= u_90_70_d
        and u_90_70_d > Decimal("1.36")
        and l_90_70_d > Decimal("1.20")
        and det_90_70.get("u_ratio_clamped_to_unity_24h") is False
        and det_90_70.get("ratio_estimand_domain_24h") == "[0, +infinity)"
        and det_90_70.get("treated_survival_exceeds_vehicle_survival_24h") is True
        and det_90_70.get("ratio_bounds_ordered_l_le_u_24h") is True
        and det_90_70.get("recomputed_kill_24h_pct_joint_l95", 0.0) < -36.0
    )
    cases.append({
        "case": "r09_b2_live_gate0v_kill_90pct_vs_70pct_unclamped_ordered_bounds_and_hard_stop",
        "expected_verdict": "FAIL_HARD_STOP",
        "actual_verdict": r_90_70["verdict"],
        "expected_token": "FAIL_HARD_STOP_AP1903_24H_KILL_BELOW_99P9PCT_NORMOXIA_BASELINE_ALPHA",
        "hard_stops": r_90_70["hard_stops"][:3],
        "holds": r_90_70["holds"][:3],
        "passed": ok_90_70_live,
    })

    # 47. R09-B1 (d): In-memory Python dict with float('nan') fails closed cleanly to HOLD (no traceback)!
    p_nan_mem = copy.deepcopy(r09_g0v_bank)
    p_nan_mem["nonfinite_probe"] = float("nan")
    r_nan_mem = evaluate_wholegraft_gate0v_and_stress_ap1903(p_nan_mem, openislet_root=resolved_root)
    ok_nan_mem = bool(
        r_nan_mem["verdict"] == "HOLD_INCOMPLETE_OR_UNDERPOWERED"
        and "HOLD_GATE0V_NONCANONICAL_OR_NONFINITE_PAYLOAD" in r_nan_mem["holds"]
    )
    cases.append({
        "case": "r09_b1_in_memory_nonfinite_float_payload_fails_closed_hold",
        "expected_verdict": "HOLD_INCOMPLETE_OR_UNDERPOWERED",
        "actual_verdict": r_nan_mem["verdict"],
        "expected_token": "HOLD_GATE0V_NONCANONICAL_OR_NONFINITE_PAYLOAD",
        "hard_stops": r_nan_mem["hard_stops"][:2],
        "holds": r_nan_mem["holds"][:3],
        "passed": ok_nan_mem,
    })

    # 48. R10-B2 (a): Schema-invalid unhashable list/dict values (e.g., qualification_tier: ["BANK_QUALIFICATION"])
    #     in standalone --evaluate-lot intake return structured JSON HOLD exit 2 (NEVER TypeError traceback)!
    with tempfile.TemporaryDirectory() as td_r10:
        td_r10_p = Path(td_r10)
        unhash_cases = (
            ("list_tier", {"qualification_tier": ["BANK_QUALIFICATION"]}),
            ("dict_tier", {"qualification_tier": {"tier": "BANK_QUALIFICATION"}}),
            ("list_geno", {"qualification_tier": "BANK_QUALIFICATION", "antiviral_genotype_stratum": ["TYK2_WT__IFIH1_WT_OR_A946T"]}),
            ("list_lot_id", {"lot_id": ["LOT-1"], "qualification_tier": "BANK_QUALIFICATION"}),
        )
        r10_unhash_ok = True
        r10_unhash_holds: List[str] = []
        for u_tag, u_doc in unhash_cases:
            uf = td_r10_p / f"r10_unhash_{u_tag}.json"
            uf.write_text(json.dumps(u_doc), encoding="utf-8")
            u_res, u_rc = evaluate_wholegraft_gate0v_lot_file(uf, openislet_root=resolved_root)
            r10_unhash_holds = u_res.get("holds") or []
            if not (u_rc == 2 and u_res.get("verdict") == "HOLD_INCOMPLETE_OR_UNDERPOWERED" and len(r10_unhash_holds) >= 1):
                r10_unhash_ok = False
        cases.append({
            "case": "r10_b2_unhashable_list_and_dict_types_standalone_lot_file_structured_hold_exit_2",
            "expected_verdict": "HOLD_INCOMPLETE_OR_UNDERPOWERED",
            "actual_verdict": "HOLD_INCOMPLETE_OR_UNDERPOWERED" if r10_unhash_ok else "UNHANDLED_ERROR_OR_WRONG_RC",
            "expected_token": "HOLD_GATE0V_MISSING_OR_INVALID_QUALIFICATION_TIER",
            "hard_stops": [],
            "holds": r10_unhash_holds[:3],
            "passed": r10_unhash_ok,
        })

        # 49. R10-B2 (b): Out-of-float64-range finite JSON integers (10**400, -10**400) and overflowing float (1e500)
        #     at standalone --evaluate-lot intake return structured JSON HOLD exit 2 (NEVER OverflowError traceback)!