OpenIslet

scripts/verify_iip0056_route4_omentum_and_exvivo_rest.py

OpenIslet Wave-6 Round-14 Prospective Candidate W6-C2 (Staged Slot IIP-0047 -> Provisional

script Release v0.8.0 gen-eb955f8266035aba 466 KB · 37 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_iip0056_route4_omentum_and_exvivo_rest.py · SHA-256: 25e12d578ba80b55…

                    fail_reasons.append(f"R4O_05_NHP_PORTAL_GRADIENT_ALIAS_POST_PROCEDURE_ALIAS_FAIL:{akey}:{pakey}")
                    adverse = True
    return adverse


def _validate_gate5_counts_and_u95(
    src: Mapping[str, Any],
    *,
    fail_reasons: List[str],
    hold_reasons: List[str],
) -> Optional[float]:
    """Validate live Blueprint Section 7 Gate 5 OQ-012 (k==0, n, f, eta>=0.30, n_eff>=299,573,228, U95<=1e-8)
    via exact context-independent 75-digit Decimal Clopper-Pearson arithmetic (ZERO float() before policy decision).
    """
    oq012_ok = src.get("oq012_clonogenic_regrowth_pass")
    if oq012_ok is None:
        hold_reasons.append("MISSING_UPSTREAM_GATE5_OQ012_CLONOGENIC_PASS")
    elif not _is_strict_bool(oq012_ok) or not oq012_ok:
        fail_reasons.append("GATE5_OQ012_CLONOGENIC_REGROWTH_FAILED")

    k_surv = src.get("survivor_count_k")
    n_test = src.get("tested_cells_n")
    f_val = src.get("clonogenic_fraction_f")
    eta_val = src.get("spike_in_detection_efficiency_eta")
    n_eff = src.get("effective_clonogenic_trials_n_eff")
    u95_v = src.get("u95_exact")

    k_ok = False
    n_ok = False
    n_eff_ok = False
    f_dec: Optional[Decimal] = None
    eta_dec: Optional[Decimal] = None
    u95_dec: Optional[Decimal] = None

    if k_surv is None:
        hold_reasons.append("MISSING_UPSTREAM_GATE5_FIELD:survivor_count_k")
    elif not _is_strict_int(k_surv) or k_surv < 0:
        fail_reasons.append("INVALID_UPSTREAM_GATE5_SURVIVOR_COUNT_K")
    else:
        k_ok = True
        if k_surv > 0:
            fail_reasons.append("FAIL_GATE5_OQ012_NONZERO_SURVIVOR_OR_U95_GT_1E8")

    if n_test is None:
        hold_reasons.append("MISSING_UPSTREAM_GATE5_FIELD:tested_cells_n")
    elif not _is_strict_int(n_test) or n_test < MIN_GATE5_TESTED_CELLS or n_test > MAX_CLONOGENIC_CELLS:
        fail_reasons.append("INVALID_UPSTREAM_GATE5_TESTED_CELLS_N")
    else:
        n_ok = True

    if k_ok and n_ok and k_surv > n_test:
        fail_reasons.append("INVALID_UPSTREAM_GATE5_SURVIVORS_EXCEED_TESTED_CELLS")
        k_ok = False

    if f_val is None:
        hold_reasons.append("HOLD_GATE5_OQ012_INCOMPLETE_RECOVERY_DENOMINATOR")
        hold_reasons.append("MISSING_UPSTREAM_GATE5_FIELD:clonogenic_fraction_f")
    elif not _is_finite_number(f_val):
        fail_reasons.append("INVALID_UPSTREAM_GATE5_CLONOGENIC_FRACTION_F")
    else:
        cand_f = _to_exact_decimal(f_val)
        if cand_f <= ZERO_DEC or cand_f > ONE_DEC:
            fail_reasons.append("INVALID_UPSTREAM_GATE5_CLONOGENIC_FRACTION_F")
        else:
            f_dec = cand_f

    if eta_val is None:
        hold_reasons.append("HOLD_GATE5_OQ012_INCOMPLETE_RECOVERY_DENOMINATOR")
        hold_reasons.append("MISSING_UPSTREAM_GATE5_FIELD:spike_in_detection_efficiency_eta")
    elif not _is_finite_number(eta_val):
        fail_reasons.append("INVALID_UPSTREAM_GATE5_SPIKE_IN_DETECTION_EFFICIENCY_ETA")
    else:
        cand_eta = _to_exact_decimal(eta_val)
        if cand_eta <= ZERO_DEC or cand_eta > ONE_DEC:
            fail_reasons.append("INVALID_UPSTREAM_GATE5_SPIKE_IN_DETECTION_EFFICIENCY_ETA")
        else:
            eta_dec = cand_eta
            if eta_dec < MIN_GATE5_DETECTION_EFFICIENCY_ETA_DEC:
                fail_reasons.append("FAIL_GATE5_OQ012_DETECTION_EFFICIENCY_ETA_LT_0P30")

    if n_eff is None:
        hold_reasons.append("HOLD_GATE5_OQ012_INCOMPLETE_RECOVERY_DENOMINATOR")
        hold_reasons.append("MISSING_UPSTREAM_GATE5_FIELD:effective_clonogenic_trials_n_eff")
    elif not _is_strict_int(n_eff) or n_eff < 1 or n_eff > MAX_CLONOGENIC_CELLS:
        fail_reasons.append("INVALID_UPSTREAM_GATE5_EFFECTIVE_CLONOGENIC_TRIALS_N_EFF")
    else:
        n_eff_ok = True
        if k_ok and k_surv > n_eff:
            fail_reasons.append("INVALID_UPSTREAM_GATE5_SURVIVORS_EXCEED_EFFECTIVE_TRIALS")
            k_ok = False
        if n_ok and f_dec is not None and eta_dec is not None:
            # For integer n_eff, n_eff <= floor(n_test * f * eta) iff Decimal(n_eff) <= exact n_test * f * eta
            max_n_eff_dec = _dec_mul(_dec_mul(Decimal(n_test), f_dec), eta_dec)
            if Decimal(n_eff) > max_n_eff_dec:
                fail_reasons.append("FAIL_GATE5_OQ012_EFFECTIVE_TRIALS_EXCEEDS_FLOOR_N_F_ETA")

    if u95_v is None:
        hold_reasons.append("MISSING_UPSTREAM_GATE5_FIELD:u95_exact")
    elif not _is_finite_number(u95_v):
        fail_reasons.append("GATE5_U95_EXACT_EXCEEDS_LIMIT_OR_INVALID")
    else:
        cand_u95 = _to_exact_decimal(u95_v)
        if cand_u95 <= ZERO_DEC or cand_u95 > ONE_DEC:
            fail_reasons.append("GATE5_U95_EXACT_EXCEEDS_LIMIT_OR_INVALID")
        else:
            u95_dec = cand_u95

    u95_recomputed: Optional[float] = None
    if k_ok and n_eff_ok:
        u95_recomp_dec = clopper_pearson_u95_decimal(k_surv, n_eff, alpha=Decimal("0.05"))
        if k_surv == 0:
            if n_eff < MIN_GATE5_EFFECTIVE_TRIALS:
                hold_reasons.append("HOLD_GATE5_OQ012_UNDERPOWERED_BELOW_299573228_EFFECTIVE_TRIALS")
            else:
                if u95_recomp_dec > MAX_GATE5_OQ012_U95_DEC:
                    fail_reasons.append("FAIL_GATE5_OQ012_NONZERO_SURVIVOR_OR_U95_GT_1E8")
                if u95_dec is not None and u95_dec > MAX_GATE5_OQ012_U95_DEC:
                    fail_reasons.append("GATE5_U95_EXACT_EXCEEDS_LIMIT_OR_INVALID")
        else:
            if u95_recomp_dec > MAX_GATE5_OQ012_U95_DEC and "FAIL_GATE5_OQ012_NONZERO_SURVIVOR_OR_U95_GT_1E8" not in fail_reasons:
                fail_reasons.append("FAIL_GATE5_OQ012_NONZERO_SURVIVOR_OR_U95_GT_1E8")
        if u95_dec is not None:
            tol_dec = min(Decimal("1e-12"), _dec_mul(Decimal("1e-4"), u95_recomp_dec))
            if _dec_diff_exceeds(u95_recomp_dec, u95_dec, tol_dec):
                fail_reasons.append("GATE5_UNDERSTATED_OR_FORGED_U95_EXACT")
        # Convert to display float ONLY after exact Decimal policy decisions complete
        if u95_recomp_dec >= ONE_DEC:
            u95_recomputed = 1.0
        else:
            u95_f_disp = _dec_to_display_float(u95_recomp_dec)
            if u95_f_disp is not None:
                if Decimal.from_float(u95_f_disp) < u95_recomp_dec:
                    u95_f_disp = math.nextafter(u95_f_disp, math.inf)
                u95_recomputed = min(1.0, u95_f_disp)
    elif k_ok and n_ok and u95_dec is not None:
        # Even if n_eff is omitted (HOLD), check u95_dec is not forged strictly below the optimistic lower bound U95(k, n_test)
        u95_opt_dec = clopper_pearson_u95_decimal(k_surv, n_test, alpha=Decimal("0.05"))
        tol_opt = min(Decimal("1e-12"), _dec_mul(Decimal("1e-4"), u95_opt_dec))
        if u95_dec < u95_opt_dec and _dec_diff_exceeds(u95_opt_dec, u95_dec, tol_opt):
            fail_reasons.append("GATE5_UNDERSTATED_OR_FORGED_U95_EXACT")
    return u95_recomputed


def _validate_upstream_lot_record(
    rec: Any,
    *,
    tag: str,
    expected_gate_source: str,
    expected_product_id: Optional[str],
    expected_lot_id: Optional[str],
    expected_bank_id: Optional[str],
    expected_bank_origin: Optional[str],
    expected_evidence_tier: Optional[str],
    expected_dual_switch_required: Optional[bool],
    trusted_evidence_store: Optional[Mapping[str, Mapping[str, Any]]],
    fail_reasons: List[str],
    hold_reasons: List[str],
) -> Dict[str, Any]:
    """Validate one v2_round5_unified 14-field upstream record against manifest, bank, scope, flags & trusted store."""
    empty: Dict[str, Any] = {
        "construct_sha": None,
        "bank_id": None,
        "record_id": None,
        "archive_sha256": None,
        "prerequisite_archive_sha256s": None,
        "recomputed_gate5_u95": None,
        "stored": None,
    }
    if rec is None:
        hold_reasons.append(f"MISSING_UPSTREAM_{tag}_RECORD")
        return empty
    if not isinstance(rec, dict):
        fail_reasons.append(f"INVALID_UPSTREAM_{tag}_RECORD")
        return empty

    _check_no_contradictory_authority_flags(rec, f"UPSTREAM_{tag}", fail_reasons)

    missing_or_invalid = False
    for field in REQUIRED_UPSTREAM_SCHEMA_V2_STRING_FIELDS:
        val = rec.get(field)
        if val is None:
            hold_reasons.append(f"MISSING_UPSTREAM_{tag}_FIELD:{field}")
            missing_or_invalid = True
        elif not _is_literal_nonempty_str(val):
            fail_reasons.append(f"INVALID_UPSTREAM_{tag}_FIELD:{field}")
            missing_or_invalid = True

    for field in REQUIRED_UPSTREAM_SCHEMA_V2_BOOL_FIELDS:
        val = rec.get(field)
        if val is None:
            hold_reasons.append(f"MISSING_UPSTREAM_{tag}_FIELD:{field}")
            missing_or_invalid = True
        elif not _is_strict_bool(val):
            fail_reasons.append(f"INVALID_UPSTREAM_{tag}_FIELD:{field}")
            missing_or_invalid = True

    if missing_or_invalid:
        return empty

    record_id = rec["record_id"]
    gate_source = rec["gate_source"]
    rec_prod = rec["product_id"]
    rec_lot = rec["lot_id"]
    rec_bank_id = rec["bank_id"]
    rec_bank_origin = rec["bank_origin"]
    rec_arm_manifest_sha = rec["canonical_arm_manifest_sha256"]
    rec_construct_sha = rec["construct_manifest_sha256"]
    rec_tier = rec["evidence_tier"]
    rec_decision = rec["decision"]
    rec_authority_scope = rec["authority_scope"]
    rec_bank_qualified = rec["bank_qualified"]
    rec_promo_auth = rec["promotion_authorized"]
    rec_archive_sha = rec["archive_sha256"]

    if gate_source != expected_gate_source:
        if expected_gate_source == "GATE-34" and gate_source == "GATE-32":
            fail_reasons.append("UPSTREAM_GATE34_LIVE_GATE32_ROLE_COLLISION_REJECTED")
        fail_reasons.append(f"UPSTREAM_{tag}_GATE_SOURCE_MISMATCH")
    if expected_product_id is not None and rec_prod != expected_product_id:
        fail_reasons.append(f"UPSTREAM_{tag}_PRODUCT_ID_MISMATCH")
    if expected_lot_id is not None and rec_lot != expected_lot_id:
        fail_reasons.append(f"UPSTREAM_{tag}_LOT_ID_MISMATCH")
    if expected_bank_id is not None and rec_bank_id != expected_bank_id:
        fail_reasons.append(f"UPSTREAM_{tag}_BANK_ID_MISMATCH")
    if rec_bank_origin not in ALLOWED_BANK_ORIGINS:
        fail_reasons.append(f"UPSTREAM_{tag}_INVALID_BANK_ORIGIN")
    elif expected_bank_origin is not None and rec_bank_origin != expected_bank_origin:
        fail_reasons.append(f"UPSTREAM_{tag}_BANK_ORIGIN_MISMATCH")
    if rec_arm_manifest_sha != EXPECTED_CANONICAL_MANIFEST_SHA256:
        fail_reasons.append(f"UPSTREAM_{tag}_CANONICAL_ARM_MANIFEST_SHA256_MISMATCH")

    valid_construct_sha: Optional[str] = None
    if not _is_non_all_zero_sha256_hex(rec_construct_sha) or rec_construct_sha == EXPECTED_CANONICAL_MANIFEST_SHA256:
        fail_reasons.append(f"UPSTREAM_{tag}_CONSTRUCT_MANIFEST_SHA256_INVALID_OR_CONFLATED")
    else:
        valid_construct_sha = rec_construct_sha

    if not _is_non_all_zero_sha256_hex(rec_archive_sha):
        fail_reasons.append(f"UPSTREAM_{tag}_INVALID_OR_ZERO_ARCHIVE_SHA256")
    if rec_tier not in ALLOWED_EVIDENCE_TIERS:
        fail_reasons.append(f"UPSTREAM_{tag}_INVALID_EVIDENCE_TIER")
    elif expected_evidence_tier is not None and rec_tier != expected_evidence_tier:
        fail_reasons.append(f"UPSTREAM_{tag}_EVIDENCE_TIER_MISMATCH")

    if rec_authority_scope not in ALLOWED_AUTHORITY_SCOPES:
        fail_reasons.append(f"UPSTREAM_{tag}_INVALID_AUTHORITY_SCOPE")

    if rec_tier == "AUTHENTICATED_ARCHIVED_WETLAB":
        if rec_authority_scope != "CLINICAL_LOT_RELEASE":
            fail_reasons.append(f"UPSTREAM_{tag}_AUTHORITY_SCOPE_NOT_CLINICAL_LOT_RELEASE")