OpenIslet

scripts/verify_iip0035_d1b_auto_and_portfolio.py

Deterministic Autologous D1b-Auto-C v2 Mode-B Registry, 45-Cell Stage-0 Contract,

script Release v0.8.0 gen-eb955f8266035aba 288 KB · 23 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_iip0035_d1b_auto_and_portfolio.py · SHA-256: fedb990aeaedd116…

                            and comp_hlac_cp.get("reported_u95_meets_exact_bound") is False
                        ):
                            comp_reasons.append("FAIL_D1_08_REPORTED_U95_BELOW_EXACT_CLOPPER_PEARSON_BOUND")
                    else:
                        if hlac03_t_c is None:
                            comp_reasons.append("HOLD_D1_08_HLAC0304_REPORTED_U95_REQUIRED")
                        elif hlac03_t_c > hlac03_cap_c:
                            comp_reasons.append("HOLD_D1_08_HLAC0304_REPORTED_U95_ABOVE_GATE_LIMIT")
                        elif hlac03_t_c <= Decimal(0) or comp_hlac_cp.get("reported_u95_meets_exact_bound") is False:
                            comp_reasons.append("FAIL_D1_08_REPORTED_U95_BELOW_EXACT_CLOPPER_PEARSON_BOUND")
                        elif comp_hlac_cp.get("reported_u95_meets_exact_bound") is None:
                            comp_reasons.append("HOLD_D1_08_REPORTED_U95_EXACT_FLOOR_INCONCLUSIVE")
            elif carries_c is False:
                comp_d1_08_branch = "L2_COMPONENT_NON_HLAC0304_MATCHED_ALLELE_CD8_SUBGATE"
                ma_pc_c = observations.get("d1_08_matched_allele_positive_control_valid")
                ma_ok_c = observations.get("d1_08_non_c0304_matched_allele_screen_passed")
                if isinstance(ma_pc_c, str) and ma_pc_c == "UNAVAILABLE":
                    explicitly_unavailable.append("d1_08_matched_allele_positive_control_valid")
                    ma_pc_c = None
                elif ma_pc_c is not None and not isinstance(ma_pc_c, bool):
                    invalid_or_oob_keys.append("d1_08_matched_allele_positive_control_valid")
                    ma_pc_c = None
                elif ma_pc_c is False:
                    comp_reasons.append("FAIL_D1_08_MATCHED_ALLELE_POSITIVE_CONTROL_INVALID")
                if ma_pc_c is None:
                    comp_reasons.append("HOLD_D1_08_MATCHED_ALLELE_POSITIVE_CONTROL_VALIDITY_REQUIRED")
                if isinstance(ma_ok_c, str) and ma_ok_c == "UNAVAILABLE":
                    explicitly_unavailable.append("d1_08_non_c0304_matched_allele_screen_passed")
                    ma_ok_c = None
                elif ma_ok_c is not None and not isinstance(ma_ok_c, bool):
                    invalid_or_oob_keys.append("d1_08_non_c0304_matched_allele_screen_passed")
                    ma_ok_c = None
                elif ma_ok_c is False:
                    comp_reasons.append("FAIL_D1_08_MATCHED_ALLELE_CD8_SCREEN_FAILED")
                if ma_ok_c is None:
                    comp_reasons.append("HOLD_D1_08_MATCHED_ALLELE_CD8_SCREEN_REQUIRED")
                if hlac03_k_c is not None and hlac03_k_c >= 1:
                    comp_reasons.append("FAIL_HARD_STOP_D1_08_HLAC0304_LYSIS_EVENT_DETECTED")
                if _l2_hlac_cal["sampling_independence_conflict"]:
                    comp_reasons.append("HOLD_D1_08_HLAC0304_HOLD_CONFLICTING_SAMPLING_INDEPENDENCE_DECLARATIONS")
                elif _l2_hlac_cal["sampling_malformed"]:
                    comp_reasons.append("HOLD_D1_08_HLAC0304_HOLD_MALFORMED_SAMPLING_INDEPENDENCE_DECLARATION")
                elif _l2_hlac_cal["clustered_sampling"]:
                    comp_reasons.append("HOLD_D1_08_HLAC0304_HOLD_NON_INDEPENDENT_OR_CLUSTERED_SAMPLING_REJECTED")
            else:
                comp_d1_08_branch = "L2_COMPONENT_UNSPECIFIED_HLAC0304_CARRIER_STATUS_HOLD"
                comp_reasons.append("HOLD_D1_08_HLAC0304_CARRIER_STATUS_REQUIRED")
                if hlac03_k_c is not None and hlac03_k_c >= 1:
                    comp_reasons.append("FAIL_HARD_STOP_D1_08_HLAC0304_LYSIS_EVENT_DETECTED")
                if _l2_hlac_cal["sampling_independence_conflict"]:
                    comp_reasons.append("HOLD_D1_08_HLAC0304_HOLD_CONFLICTING_SAMPLING_INDEPENDENCE_DECLARATIONS")
                elif _l2_hlac_cal["sampling_malformed"]:
                    comp_reasons.append("HOLD_D1_08_HLAC0304_HOLD_MALFORMED_SAMPLING_INDEPENDENCE_DECLARATION")
                elif _l2_hlac_cal["clustered_sampling"]:
                    comp_reasons.append("HOLD_D1_08_HLAC0304_HOLD_NON_INDEPENDENT_OR_CLUSTERED_SAMPLING_REJECTED")
        # Never drop any reported cross-gate adverse safety hard stop (k >= 1) on any component donor path!
        _comp_ap_k_adv = _strict_int("obs_d1_04_ap1903_survivor_count_k")
        if _comp_ap_k_adv is not None and _comp_ap_k_adv >= 1:
            comp_reasons.append("FAIL_HARD_STOP_D1_04_AP1903_SURVIVOR_DETECTED")
        _comp_hlac_k_adv = _strict_int("obs_d1_08_hlac0304_lysed_events_k")
        if _comp_hlac_k_adv is not None and _comp_hlac_k_adv >= 1:
            comp_reasons.append("FAIL_HARD_STOP_D1_08_HLAC0304_LYSIS_EVENT_DETECTED")
        _comp_c5b9_k_adv = _strict_int("obs_d1_08_t1d_serum_c5b9_pos_events_k")
        if _comp_c5b9_k_adv is not None and _comp_c5b9_k_adv >= 1:
            comp_reasons.append("FAIL_HARD_STOP_D1_08_T1D_SERUM_C5B9_EVENT_DETECTED")
        comp_out_of_scope_g0v_status: str | None = None
        comp_g0v_standalone_status = "NOT_APPLICABLE_COMPONENT_DONOR"
        if g0v_any_positive_k:
            comp_reasons.append("FAIL_HARD_STOP_OUT_OF_SCOPE_COMPONENT_GLOBAL_GATE0V_DAY28_VP1_IE1_POSITIVE_EVENT")
            comp_out_of_scope_g0v_status = "FAIL_HARD_STOP_OUT_OF_SCOPE_ADVERSE_GATE0V_RECORD_RECEIVED"
            comp_g0v_standalone_status = "FAIL_HARD_STOP_OUT_OF_SCOPE_ADVERSE_GATE0V_RECORD_RECEIVED"
        if g0v_conflict:
            comp_reasons.append("FAIL_D1_CONFLICTING_GLOBAL_GATE0V_DECLARATIONS")
            if comp_out_of_scope_g0v_status is None:
                comp_out_of_scope_g0v_status = "FAIL_D1_CONFLICTING_GLOBAL_GATE0V_DECLARATIONS"
                comp_g0v_standalone_status = "FAIL_D1_CONFLICTING_GLOBAL_GATE0V_DECLARATIONS"
        # Inspect ALL supplied nested Gate-0V records on component donors for lot-alias conflicts,
        # mismatches against evaluated_lot_id, malformed lot aliases, non-dict records, or explicit FAIL verdicts!
        for _comp_g0v_rec in g0v_supplied_records:
            if _comp_g0v_rec is None:
                continue
            if isinstance(_comp_g0v_rec, bool) or not isinstance(_comp_g0v_rec, dict):
                comp_reasons.append("HOLD_D1_COMPONENT_OUT_OF_SCOPE_MALFORMED_GATE0V_RECORD")
                if comp_out_of_scope_g0v_status is None:
                    comp_out_of_scope_g0v_status = "HOLD_D1_COMPONENT_OUT_OF_SCOPE_MALFORMED_GATE0V_RECORD"
                    comp_g0v_standalone_status = "HOLD_D1_COMPONENT_OUT_OF_SCOPE_MALFORMED_GATE0V_RECORD"
                continue
            if _inspect_adverse_gate0v_failure(_comp_g0v_rec) is not None:
                comp_reasons.append("FAIL_OUT_OF_SCOPE_COMPONENT_GLOBAL_GATE0V_VERDICT_FAIL")
                if comp_out_of_scope_g0v_status is None:
                    comp_out_of_scope_g0v_status = "FAIL_OUT_OF_SCOPE_COMPONENT_GLOBAL_GATE0V_VERDICT_FAIL"
                    comp_g0v_standalone_status = "FAIL_OUT_OF_SCOPE_COMPONENT_GLOBAL_GATE0V_VERDICT_FAIL"
            _cn_lot_vals: List[str] = []
            _cn_lot_bad = False
            for _cnlk in _LOT_ALIAS_KEYS:
                if _cnlk not in _comp_g0v_rec:
                    continue
                _cnlv = _comp_g0v_rec[_cnlk]
                if _cnlv is None or not _is_canonical_identifier(_cnlv):
                    _cn_lot_bad = True
                elif _cnlv not in _cn_lot_vals:
                    _cn_lot_vals.append(_cnlv)
            if _cn_lot_bad:
                comp_reasons.append("HOLD_D1_INVALID_OR_MISSING_EVALUATED_LOT_ID")
                if comp_out_of_scope_g0v_status is None:
                    comp_out_of_scope_g0v_status = "HOLD_D1_INVALID_OR_MISSING_EVALUATED_LOT_ID"
                    comp_g0v_standalone_status = "HOLD_D1_INVALID_OR_MISSING_EVALUATED_LOT_ID"
            if len(_cn_lot_vals) > 1 or (lot_id_eff is not None and any(_v != lot_id_eff for _v in _cn_lot_vals)):
                comp_reasons.append("HOLD_D1_CONFLICTING_LOT_ID_BINDING")
                if comp_out_of_scope_g0v_status is None:
                    comp_out_of_scope_g0v_status = "HOLD_D1_CONFLICTING_LOT_ID_BINDING"
                    comp_g0v_standalone_status = "HOLD_D1_CONFLICTING_LOT_ID_BINDING"
        if invalid_or_oob_keys:
            comp_reasons.append("FAIL_D1_INVALID_OR_OUT_OF_RANGE_OBSERVATION")
        if explicitly_unavailable:
            comp_reasons.append("HOLD_D1_OBSERVATION_EXPLICITLY_UNAVAILABLE")
        all_c = list(dict.fromkeys(reasons + comp_reasons))
        non_excl_reasons = [r for r in all_c if r != "HOLD_D1_COMPONENT_DONOR_EXCLUDED_FROM_FULLSTACK_CLEARANCE"]
        comp_is_canonical_eval = bool(
            require_canonical_sha
            and contract["staged_registry_verified"]
            and contract["canonical_sha_verified"]
            and contract["playbook_canonical_sha_verified"]
        )
        comp_raw_subgates_passed = len(non_excl_reasons) == 0
        comp_subgates_passed = bool(comp_raw_subgates_passed and comp_is_canonical_eval)
        if not comp_is_canonical_eval:
            all_c.append("HOLD_D1_NON_CANONICAL_DIAGNOSTIC_CONTRACT_UNPINNED")
        ord_c = (
            [r for r in all_c if r.startswith("FAIL_HARD_STOP_")]
            + [r for r in all_c if r.startswith("FAIL_") and not r.startswith("FAIL_HARD_STOP_")]
            + [r for r in all_c if not r.startswith("FAIL_") and r != "HOLD_D1_COMPONENT_DONOR_EXCLUDED_FROM_FULLSTACK_CLEARANCE"]
            + [r for r in all_c if r == "HOLD_D1_COMPONENT_DONOR_EXCLUDED_FROM_FULLSTACK_CLEARANCE"]
        )
        comp_cal_summary: Dict[str, Any] = {}
        comp_sampling_prov: Dict[str, Any] = {}
        if pvid == "ISLET1_D1_AUTO_V2_L3_AAVS1_VTCN1_UCOE2S_ICASP9":
            comp_cal_summary["d1_04_ap1903"] = _cal_meta_from_cp(comp_ap_cp, comp_samp_meta.get("d1_04_ap1903"))
            comp_sampling_prov["d1_04_ap1903"] = (comp_samp_meta.get("d1_04_ap1903") or {}).get("sampling_independence_provenance")
        elif pvid == "ISLET1_D1_AUTO_V2_L2_CXCL10_SERPINB9_PDL1":
            comp_cal_summary["d1_08_hlac0304"] = _cal_meta_from_cp(comp_hlac_cp, comp_samp_meta.get("d1_08_hlac0304"))
            comp_sampling_prov["d1_08_hlac0304"] = (comp_samp_meta.get("d1_08_hlac0304") or {}).get("sampling_independence_provenance")
        comp_cov_regime = _aggregate_panel_coverage_regime(comp_cal_summary)
        comp_assumption_flags = _collect_assumption_only_flags(comp_cal_summary)
        comp_top_status = (
            "NON_CANONICAL_DIAGNOSTIC_COMPONENT_SUBGATES_MET_UNPINNED_EXCLUDED_FROM_FULLSTACK"
            if (not comp_is_canonical_eval and comp_raw_subgates_passed)
            else ord_c[0]
        )
        comp_emp_status = (
            "HOLD_COMPONENT_DONOR_EXCLUDED_FROM_FULLSTACK_CLEARANCE"
            if comp_is_canonical_eval
            else "NON_CANONICAL_DIAGNOSTIC_COMPONENT_UNPINNED_NO_RELEASE"
        )
        return {
            "gate_id": "STAGE0_GATE_AUTOLOGOUS_D1B_AUTO_C_V2_D101_D109_IIP_0035",
            "decision_id": "DEC-114",
            "contribution_id": "IIP-0035",
            "controlling_contract_block_id": contract["gate_id"],
            "controlling_contract_schema_version": contract["schema_version"],
            "controlling_contract_sha256": contract["sha256"],
            "controlling_staged_registry_verified": contract["staged_registry_verified"],
            "controlling_contract_canonical_sha_verified": contract["canonical_sha_verified"],
            "controlling_contract_on_disk_verified": contract["on_disk_decision_tree_verified"],
            "controlling_contract_in_memory_matches_canonical_sha": contract["in_memory_block_matches_canonical_sha"],
            "controlling_playbook_sec4120_sha256": contract["playbook_sec4120_sha256"],
            "controlling_playbook_footer_verified": contract["playbook_footer_verified"],
            "controlling_playbook_canonical_sha_verified": contract["playbook_canonical_sha_verified"],
            "controlling_contract_cells_loaded": contract["cells_loaded"],
            "controlling_contract_external_refs": contract["external_refs"],
            "evaluation_mode": "CANONICAL_PINNED" if comp_is_canonical_eval else "NON_CANONICAL_DIAGNOSTIC_UNPINNED",
            "evaluation_kind": "COMPONENT_SUBGATE_REFERENCE_COMPUTATION_EXCLUDED_FROM_FULLSTACK_CLEARANCE",
            "reference_scenario_scope": "SINGLE_LOCUS_COMPONENT_DONOR_EXCLUDED_FROM_FULLSTACK",
            "assembled_multilocus_manifest_bound": False,
            "clinical_or_batch_release_authorized": False,
            "canonical_product_variant_id": pvid,
            "effective_locus1_donor_arm": None,
            "evaluated_lot_id": lot_id_eff if _is_canonical_identifier(lot_id_eff) else None,
            "d1_08_allele_branch": comp_d1_08_branch,
            "d1_08_non_c0304_screen_attestation_scope": (
                "EXTERNALLY_ADJUDICATED_CALLER_ATTESTATION_PENDING_NORMATIVE_PROTOCOL"
                if comp_d1_08_branch == "L2_COMPONENT_NON_HLAC0304_MATCHED_ALLELE_CD8_SUBGATE"
                else None
            ),
            "global_gate0v_standalone_subcheck_status": comp_g0v_standalone_status,
            "global_gate0v_out_of_scope_adverse_record_status": comp_out_of_scope_g0v_status,
            "global_gate0v_lims_ledger_verified": None,
            "panel_coverage_scope": "PER_ASSAY_INDIVIDUAL_COVERAGE_NOT_SIMULTANEOUS_PANEL_FAMILYWISE_GUARANTEE",
            "coverage_regime": comp_cov_regime,
            "rare_event_calibration_summary": comp_cal_summary,
            "sampling_design_provenance_by_assay": comp_sampling_prov,
            "assumption_only_path_flags": comp_assumption_flags,
            "component_subgate_evaluation": {
                "applicable_gates": comp_gates,
                "evaluation_mode": "CANONICAL_PINNED" if comp_is_canonical_eval else "NON_CANONICAL_DIAGNOSTIC_UNPINNED",
                "component_subgates_passed": comp_subgates_passed,
                "component_subgates_diagnostic_passed": comp_raw_subgates_passed,
                "component_reasons": comp_reasons,
            },
            "d1_04_ap1903_u95_str": comp_ap_cp.get("u95_exact_sound_decimal_str") if comp_ap_cp else None,
            "d1_08_hlac0304_u95_pct_str": comp_hlac_cp.get("u95_exact_sound_decimal_pct_str") if comp_hlac_cp else None,
            "d1_08_t1d_serum_c5b9_u95_pct_str": comp_t1d_cp.get("u95_exact_sound_decimal_pct_str") if comp_t1d_cp else None,
            "empirical_clearance_status": comp_emp_status,
            "reference_criteria_met": False,
            "status": comp_top_status,
            "reasons": ord_c,
        }

    # Full-panel / unresolved-product evaluation path
    g0v_kw = {
        "day14_min": thr.get("g0v_day14_min", Decimal("3.0")),
        "day14_l95_min": thr.get("g0v_day14_l95_min", Decimal("3.0")),
        "early_72h_max": thr.get("g0v_72h_max", Decimal("2.0")),
        "day28_u95_max": thr.get("g0v_day28_u95_max", Decimal("0.0005")),
        "day28_n_min": int(thr.get("g0v_day28_n_min", 5990)),
        "panel_observations": observations,
    }
    _hs_g0v_l1a_or_unres = (
        "FAIL_HARD_STOP_D1_UNRESOLVED_PRODUCT_GLOBAL_GATE0V_DAY28_VP1_IE1_POSITIVE_EVENT"
        if (has_pvid_conflict or not is_fullstack)
        else "FAIL_HARD_STOP_D1_L1A_GLOBAL_GATE0V_DAY28_VP1_IE1_POSITIVE_EVENT"
    )
    _fail_g0v_l1a_or_unres = (
        "FAIL_D1_UNRESOLVED_PRODUCT_GLOBAL_GATE0V_VIRAL_SANCTUARY_FAILED"
        if (has_pvid_conflict or not is_fullstack)
        else "FAIL_D1_L1A_GLOBAL_GATE0V_VIRAL_SANCTUARY_FAILED"
    )
    _g0v_adverse_fail_reasons = sorted({
        r_f
        for rec in g0v_supplied_records
        if (r_f := _inspect_adverse_gate0v_failure(
            rec,
            day14_min=g0v_kw["day14_min"],
            day14_l95_min=g0v_kw["day14_l95_min"],
            early_72h_max=g0v_kw["early_72h_max"],
        )) is not None
    })
    if is_fullstack and not has_pvid_conflict and effective_l1_arm == "D1AUTO_V2_L1B_CD46WT_CD64T_GATE0V_COMPARATOR":
        if lot_conflict or lot_invalid:
            _lot_block_reason = "HOLD_D1_CONFLICTING_LOT_ID_BINDING" if lot_conflict else "HOLD_D1_INVALID_OR_MISSING_EVALUATED_LOT_ID"
            if g0v_any_positive_k:
                reasons.append("FAIL_HARD_STOP_D1_L1B_GATE0V_DAY28_VP1_IE1_POSITIVE_EVENT_SWITCH_TO_L1A_IL1RN")
                g0v = {
                    "cleared": False,
                    "failed": True,
                    "hard_stop": True,
                    "lot_binding_invalid": True,
                    "reason": "FAIL_HARD_STOP_D1_L1B_GATE0V_DAY28_VP1_IE1_POSITIVE_EVENT_SWITCH_TO_L1A_IL1RN",
                    "lot_hold_reason": _lot_block_reason,
                    "adverse_event_pending_lot_attribution": True,