OpenIslet

scripts/verify_iip0055_hygiene_safecell_minifh_orthogonality.py

OpenIslet Wave-6 W6-C1 (Provisional IIP-0055 / DEC-136 / GATE-34 / GY-033;

script Release v0.8.0 gen-eb955f8266035aba 417 KB · 30 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_iip0055_hygiene_safecell_minifh_orthogonality.py · SHA-256: d5b98e2ba1310333…

                h2h_run_id = h2h.get("run_id")
                if h2h_run_id is None:
                    _add_hold("HOLD_MISSING_HEAD_TO_HEAD_RUN_ID")
                elif not _is_clean_str(h2h_run_id):
                    _add_fail("FAIL_HEAD_TO_HEAD_INVALID_RUN_ID")

                h2h_lot_id = h2h.get("lot_id")
                if h2h_lot_id is None:
                    _add_hold("HOLD_MISSING_HEAD_TO_HEAD_LOT_ID")
                elif not _is_clean_str(h2h_lot_id) or (lot_id_str is not None and h2h_lot_id != lot_id_str):
                    _add_fail("FAIL_HEAD_TO_HEAD_LOT_ID_MISMATCH")

                h2h_arch_rec, h2h_arch_ok = _resolve_archived_record_obj(
                    h2h.get("archived_benchmark_record"),
                    prefix="H2H_BENCHMARK_RECORD",
                    expected_gate_source=H2H_BENCHMARK_GATE_SOURCE,
                    allowed_decisions=frozenset({"PASS", "SYNTHETIC_SELF_TEST_PASS"}),
                    role="supporting",
                    wetlab_allowed_decisions=frozenset({"PASS"}),
                    synthetic_allowed_decisions=frozenset({"SYNTHETIC_SELF_TEST_PASS"}),
                )
                arch_obs_map: Optional[Dict[str, Any]] = None
                if h2h_arch_rec is not None:
                    if _is_clean_str(h2h_run_id) and h2h_arch_rec.get("run_id") != h2h_run_id:
                        _add_fail("FAIL_H2H_BENCHMARK_ARCHIVE_RUN_ID_MISMATCH")
                        h2h_arch_ok = False
                    if h2h_arch_rec.get("same_1pct_o2_cytokine_run") is not True:
                        _add_fail("FAIL_H2H_BENCHMARK_ARCHIVE_SAME_RUN_FLAG_INVALID")
                        h2h_arch_ok = False
                    arch_arms = h2h_arch_rec.get("comparator_arms_benchmarked")
                    arch_arms_clean_list = _is_clean_str_list(arch_arms)
                    if not arch_arms_clean_list:
                        _add_fail("FAIL_H2H_BENCHMARK_ARCHIVE_INVALID_ARMS_LIST")
                        h2h_arch_ok = False
                    elif not REQUIRED_H2H_COMPARATOR_ARMS.issubset(set(arch_arms)):
                        _add_fail("FAIL_H2H_BENCHMARK_ARCHIVE_INCOMPLETE_GOVERNED_3WAY_SET")
                        h2h_arch_ok = False
                    elif carms_clean_list and set(arch_arms) != set(carms):
                        _add_fail("FAIL_H2H_BENCHMARK_INLINE_VS_ARCHIVE_ARMS_MISMATCH")
                        h2h_arch_ok = False

                    raw_arch_obs = h2h_arch_rec.get("per_arm_observations")
                    raw_arch_arms_alias = h2h_arch_rec.get("arms")
                    if raw_arch_obs is not None and not isinstance(raw_arch_obs, dict):
                        _add_fail("FAIL_H2H_BENCHMARK_ARCHIVE_INVALID_OBSERVATIONS_TYPE")
                        h2h_arch_ok = False
                    if raw_arch_arms_alias is not None and not isinstance(raw_arch_arms_alias, dict):
                        _add_fail("FAIL_H2H_BENCHMARK_ARCHIVE_INVALID_ARMS_MAP_TYPE")
                        h2h_arch_ok = False
                    if isinstance(raw_arch_obs, dict) and isinstance(raw_arch_arms_alias, dict):
                        if not _strict_json_equal(raw_arch_obs, raw_arch_arms_alias):
                            _add_fail("FAIL_H2H_BENCHMARK_ARCHIVE_ARMS_VS_OBSERVATIONS_MISMATCH")
                            h2h_arch_ok = False
                        arch_obs_map = raw_arch_obs
                    elif isinstance(raw_arch_obs, dict):
                        arch_obs_map = raw_arch_obs
                    elif isinstance(raw_arch_arms_alias, dict):
                        arch_obs_map = raw_arch_arms_alias
                    else:
                        _add_fail("FAIL_H2H_BENCHMARK_ARCHIVE_MISSING_OBSERVATIONS")
                        h2h_arch_ok = False

                per_arm = h2h.get("per_arm_observations")
                if per_arm is None:
                    _add_hold("HOLD_MISSING_HEAD_TO_HEAD_PER_ARM_OBSERVATIONS")
                    h2h_arch_ok = False
                elif not isinstance(per_arm, dict):
                    _add_fail("FAIL_INVALID_HEAD_TO_HEAD_PER_ARM_OBSERVATIONS_TYPE")
                    h2h_arch_ok = False
                else:
                    if arch_obs_map is not None and not _strict_json_equal(arch_obs_map, per_arm):
                        _add_fail("FAIL_H2H_BENCHMARK_INLINE_VS_ARCHIVE_OBSERVATIONS_MISMATCH")
                        h2h_arch_ok = False

                # Evaluate benchmark observations directly from verified archive (and also inspect inline per_arm if it differs)
                obs_maps_to_check: List[Dict[str, Any]] = []
                if isinstance(arch_obs_map, dict):
                    obs_maps_to_check.append(arch_obs_map)
                if isinstance(per_arm, dict) and (arch_obs_map is None or not _strict_json_equal(arch_obs_map, per_arm)):
                    obs_maps_to_check.append(per_arm)
                if obs_maps_to_check:
                    # Select active comparator observation STRICTLY from verified lot construct switch_registry_by_id!
                    active_cand_switch = registered_primary_benchmark_arm if registered_primary_switch_verified else None
                    if active_cand_switch is None:
                        if construct_rec is None:
                            _add_hold("HOLD_HEAD_TO_HEAD_PENDING_VERIFIED_CONSTRUCT_SWITCH_REGISTRY")
                        else:
                            _add_fail("FAIL_HEAD_TO_HEAD_UNRESOLVED_REGISTERED_PRIMARY_SWITCH")
                        h2h_arch_ok = False
                    for eval_obs_map in obs_maps_to_check:
                        for req_arm_name in sorted(REQUIRED_H2H_COMPARATOR_ARMS):
                            obs = eval_obs_map.get(req_arm_name)
                            if obs is None:
                                _add_fail(f"FAIL_HEAD_TO_HEAD_MISSING_OBSERVATION_{req_arm_name.upper()}")
                                h2h_arch_ok = False
                                continue
                            if not isinstance(obs, dict):
                                _add_fail(f"FAIL_HEAD_TO_HEAD_INVALID_OBSERVATION_{req_arm_name.upper()}")
                                h2h_arch_ok = False
                                continue
                            if not _is_clean_str(obs.get("run_id")) or (_is_clean_str(h2h_run_id) and obs.get("run_id") != h2h_run_id):
                                _add_fail(f"FAIL_HEAD_TO_HEAD_RUN_ID_MISMATCH_{req_arm_name.upper()}")
                                h2h_arch_ok = False
                            if not _is_clean_str(obs.get("lot_id")) or (lot_id_str is not None and obs.get("lot_id") != lot_id_str):
                                _add_fail(f"FAIL_HEAD_TO_HEAD_LOT_ID_MISMATCH_{req_arm_name.upper()}")
                                h2h_arch_ok = False
                            obs_o2 = obs.get("hypoxia_o2_pct")
                            if not _is_finite_number(obs_o2) or abs(float(obs_o2) - 1.0) > 1.0e-9:
                                _add_fail(f"FAIL_HEAD_TO_HEAD_O2_PCT_MUST_EQUAL_1P0_{req_arm_name.upper()}")
                                h2h_arch_ok = False
                            obs_n = obs.get("cells_assayed")
                            obs_k = obs.get("survivors_observed")
                            obs_u95 = obs.get("escape_u95")
                            obs_cl = obs.get("caspase_cleavage_fold_vs_vehicle")
                            if not _is_strict_int(obs_n) or obs_n < 100_000 or obs_n > MAX_CLONOGENIC_CELLS or not _is_strict_int(obs_k) or obs_k < 0 or obs_k > obs_n:
                                _add_fail(f"FAIL_HEAD_TO_HEAD_INVALID_COUNTS_{req_arm_name.upper()}")
                                h2h_arch_ok = False
                            elif not _is_finite_number(obs_u95) or float(obs_u95) <= 0.0 or float(obs_u95) > 1.0:
                                _add_fail(f"FAIL_HEAD_TO_HEAD_INVALID_U95_{req_arm_name.upper()}")
                                h2h_arch_ok = False
                            else:
                                obs_u95_dec = clopper_pearson_u95_decimal(obs_k, obs_n, alpha=0.05)
                                if Decimal.from_float(float(obs_u95)) < obs_u95_dec - Decimal("1e-15"):
                                    _add_fail(f"FAIL_HEAD_TO_HEAD_UNDERSTATED_U95_{req_arm_name.upper()}")
                                    h2h_arch_ok = False
                                if req_arm_name == active_cand_switch and (obs_u95_dec > Decimal("1e-4") or float(obs_u95) > 1.0e-4):
                                    _add_fail("FAIL_HEAD_TO_HEAD_CANDIDATE_SWITCH_U95_EXCEEDS_1E_MINUS_4")
                                    h2h_arch_ok = False
                            if not _is_finite_number(obs_cl) or float(obs_cl) <= 0.0:
                                _add_fail(f"FAIL_HEAD_TO_HEAD_INVALID_CLEAVAGE_{req_arm_name.upper()}")
                                h2h_arch_ok = False
                            elif req_arm_name == active_cand_switch and float(obs_cl) < 5.0:
                                _add_fail("FAIL_HEAD_TO_HEAD_CANDIDATE_SWITCH_CLEAVAGE_LT_5X")
                                h2h_arch_ok = False
                h2h_archive_verified = h2h_arch_ok

    # HARD SYNTHETIC FIXTURE ISOLATION + BANK-VS-CLINICAL SCOPE SEPARATION + TRUSTED-ARCHIVE AUTHORIZATION GATE
    all_required_archives_verified = bool(
        construct_verified
        and g0v_verified
        and g5_verified
        and chs4_barrier_verified
        and cbx3_barrier_verified
        and h2h_archive_verified
        and func_arch_verified
    )
    any_synthetic = (
        evidence_tier_str == "SYNTHETIC_FIXTURE"
        or eval_ctx == "SYNTHETIC_SELF_TEST_FIXTURE"
        or any(t == "SYNTHETIC_FIXTURE" for t in resolved_upstream_tiers)
    )

    stage0_other_limited = bool(stage_limited_reasons)
    bank_only_limited = False
    if g0v_is_bank_only and eval_scope_str == "FULL_CLINICAL_PROMOTION":
        bank_only_limited = True
        _add_stage_limited("STAGE_LIMITED_GATE0V_BANK_QUALIFICATION_ONLY_NOT_LOT_RELEASE")
    elif eval_scope_str == "BANK_QUALIFICATION_ONLY":
        bank_only_limited = True
        _add_stage_limited("BANK_QUALIFICATION_ONLY_SCOPE_WITHHOLDS_CLINICAL_PROMOTION")
    elif eval_scope_str == "STAGE0_ONLY_NO_CLINICAL_PROMOTION":
        stage0_other_limited = True
        if not stage_limited_reasons:
            _add_stage_limited("STAGE0_ONLY_SCOPE_CANNOT_AUTHORIZE_CLINICAL_PROMOTION")

    if fail_reasons:
        decision = "FAIL"
        authority_scope = "REJECTED_OR_HELD"
        bank_qualified = False
        promotion_authorized = False
    elif hold_reasons:
        decision = "HOLD"
        authority_scope = "REJECTED_OR_HELD"
        bank_qualified = False
        promotion_authorized = False
    elif bank_only_limited and not stage0_other_limited:
        decision = "STAGE_LIMITED_BANK_QUALIFIED"
        authority_scope = "BANK_QUALIFICATION_ONLY"
        bank_qualified = bool(not any_synthetic and all_required_archives_verified)
        promotion_authorized = False
    elif stage_limited_reasons:
        decision = "STAGE_LIMITED_PASS"
        authority_scope = "REJECTED_OR_HELD"
        bank_qualified = False
        promotion_authorized = False
    elif any_synthetic:
        decision = "SYNTHETIC_SELF_TEST_PASS"
        authority_scope = "SYNTHETIC_SELF_TEST"
        bank_qualified = False
        promotion_authorized = False
    elif (
        evidence_tier_str == "AUTHENTICATED_ARCHIVED_WETLAB"
        and eval_ctx == "EXTERNAL_LOT_SUBMISSION"
        and eval_scope_str == "FULL_CLINICAL_PROMOTION"
        and all_required_archives_verified
    ):
        decision = "PASS"
        authority_scope = "CLINICAL_LOT_RELEASE"
        bank_qualified = True
        promotion_authorized = True
    else:
        decision = "HOLD"
        authority_scope = "REJECTED_OR_HELD"
        bank_qualified = False
        promotion_authorized = False
        _add_hold("HOLD_PROMOTION_PREREQUISITES_NOT_SATISFIED")

    dual_switch_lot_qualified = bool(
        dual_req is True
        and registered_primary_switch_verified
        and registered_secondary_switch_verified
        and secondary_switch_assay_verified
        and decision in ("PASS", "SYNTHETIC_SELF_TEST_PASS")
    )
    g0v_arch_sha = (
        g0v_rec["archive_sha256"]
        if (g0v_rec is not None and g0v_verified and _is_nonzero_sha256_hex(g0v_rec.get("archive_sha256")))
        else None
    )
    g5_arch_sha = (
        g5_rec["archive_sha256"]
        if (g5_rec is not None and g5_verified and _is_nonzero_sha256_hex(g5_rec.get("archive_sha256")))
        else None
    )

    emitted_receipt_v1 = _build_gate32_emitted_receipt(
        arm_id=arm_id_str,
        product_id=product_id_str,
        lot_id=lot_id_str,
        bank_id=bank_id_str,
        bank_origin=bank_origin_str,
        construct_manifest_sha256=construct_sha_str,
        evidence_tier=evidence_tier_str,
        decision=decision,
        authority_scope=authority_scope,
        bank_qualified=bank_qualified,
        promotion_authorized=promotion_authorized,
        dual_switch_lot_qualified=dual_switch_lot_qualified,
        primary_switch_id=registered_primary_switch_id if registered_primary_switch_verified else None,
        secondary_switch_id=registered_secondary_switch_id if dual_switch_lot_qualified else None,
        g0v_archive_sha256=g0v_arch_sha,
        g5_archive_sha256=g5_arch_sha,
    )

    return {
        "gate_id": GATE_ID,
        "gate_source": GATE_ID,
        "gate_role": GATE_ROLE,
        "verdict": decision,