OpenIslet

scripts/verify_iip0057_foundry_v2_shield_invvax_insl82i_cuffv2.py

W6-C3 (Provisional Next-Free IIP-0057 / DEC-138 / GATE-36 / GY-035; staging slot iip0057,

script Release v0.8.0 gen-eb955f8266035aba 412 KB · 32 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_iip0057_foundry_v2_shield_invvax_insl82i_cuffv2.py · SHA-256: 1c63dcf649c42be4…

                    **official_stats,
                    "ins_statistics_derivation_source": src_label,
                    "caller_roster_reconciliation_status": "MATCHED",
                    **null_caller,
                }

            # State 4: Authenticated archive contradicted by caller -> official stats strictly from archive, caller isolated!
            src_label = (
                "AUTHENTICATED_ARCHIVE_ONLY_CALLER_CONFLICT"
                if is_auth_wetlab
                else "SYNTHETIC_FIXTURE_ARCHIVE_ONLY_CALLER_CONFLICT"
            )
            return {
                **official_stats,
                "ins_statistics_derivation_source": src_label,
                "caller_roster_reconciliation_status": "CONFLICT_CALLER_MISMATCH",
                **_caller_ins_unverified(),
            }

        return _verify_upstream_evidence_and_finalize(
            obs,
            CANONICAL_GATE34_MODULE_SOURCES["ins"],
            None,
            arm_entry,
            trusted_evidence_store,
            fail_reasons,
            missing_fields,
            hold_reasons,
            verified_manifest=verified_manifest,
            extra_builder=_build_ins_extra,
        )
    except ValueError as exc:
        return _malformed_rejection(exc, empty_ins)


def _evaluate_cuff_measurements(
    src: dict[str, Any],
    *,
    is_archive: bool,
    fail_reasons: list[str],
    missing_fields: list[str],
    hold_reasons: list[str],
) -> dict[str, Any]:
    """Validate and enforce ALL governing Cuff v2.0 measurements & cutoffs directly on `src` (ZERO threshold tolerance)."""
    pfx = "current_assay_evidence_record." if is_archive else ""
    rtag = "ARCHIVE_" if is_archive else ""

    def _opt(key: str) -> tuple[bool, Any]:
        if key not in src or src[key] is None:
            missing_fields.append(f"{pfx}{key}")
            return False, None
        return True, src[key]

    has_study, study_raw = _opt("study_id")
    study_id = _require_canonical_ascii_id(study_raw, f"{pfx}study_id", rtag=rtag, fail_reasons=fail_reasons) if has_study else None

    # Exact 3-protein core assembly manifest: {"sCR1_v2", "THBD-M406L", "CD200-ECD"}
    has_core, core_raw = _opt("core_cuff_proteins")
    parsed_core: list[str] | None = None
    if has_core:
        if not isinstance(core_raw, (list, tuple)):
            raise ValueError(f"{pfx}core_cuff_proteins must be list of strings")
        parsed_core = [_require_canonical_ascii_id(x, f"{pfx}core_cuff_protein", rtag=rtag, fail_reasons=fail_reasons) for x in core_raw]
        if len(parsed_core) != 3 or set(parsed_core) != CANONICAL_CORE_CUFF_PROTEINS:
            fail_reasons.append(f"{rtag}CORE_3_PROTEIN_CUFF_MANIFEST_MISMATCH:{parsed_core}")
    if "core_proteins" in src and src["core_proteins"] is not None:
        cp_alias_raw = src["core_proteins"]
        if not isinstance(cp_alias_raw, (list, tuple)):
            raise ValueError(f"{pfx}core_proteins must be list of strings")
        cp_alias = [_require_canonical_ascii_id(x, f"{pfx}core_proteins[]", rtag=rtag, fail_reasons=fail_reasons) for x in cp_alias_raw]
        if len(cp_alias) != 3 or set(cp_alias) != CANONICAL_CORE_CUFF_PROTEINS:
            fail_reasons.append(f"{rtag}CORE_PROTEINS_ALIAS_MANIFEST_MISMATCH:{cp_alias}")
        if parsed_core is not None and set(cp_alias) != set(parsed_core):
            fail_reasons.append(f"{rtag}CORE_PROTEINS_ALIAS_MISMATCH:{cp_alias}!={parsed_core}")

    # Lot-linked per-protein conjugation yield, PEG tier, exact 0 unpaired ECD Cys, & free thiol
    parsed_conj: dict[str, dict[str, Any]] = {}
    has_conj, conj_raw = _opt("per_protein_conjugation_records")
    if has_conj:
        if not isinstance(conj_raw, dict):
            raise ValueError(f"{pfx}per_protein_conjugation_records must be dict")
        peg_rules = {
            "sCR1_v2": ("max", 5.0),
            "THBD-M406L": ("max", 5.0),
            "CD200-ECD": ("min", 20.0),
        }
        for prot in ("sCR1_v2", "THBD-M406L", "CD200-ECD"):
            if prot not in conj_raw or conj_raw[prot] is None:
                missing_fields.append(f"{pfx}per_protein_conjugation_records.{prot}")
                continue
            prec = conj_raw[prot]
            if not isinstance(prec, dict):
                raise ValueError(f"{pfx}per_protein_conjugation_records.{prot} must be dict")
            pp: dict[str, Any] = {}
            if "protein_lot_id" not in prec or prec["protein_lot_id"] is None:
                missing_fields.append(f"{pfx}per_protein_conjugation_records.{prot}.protein_lot_id")
            else:
                pp["protein_lot_id"] = _require_canonical_ascii_id(prec["protein_lot_id"], f"{pfx}conj[{prot}].protein_lot_id", rtag=rtag, fail_reasons=fail_reasons)
            if "conjugation_yield_pct" not in prec or prec["conjugation_yield_pct"] is None:
                missing_fields.append(f"{pfx}per_protein_conjugation_records.{prot}.conjugation_yield_pct")
            else:
                cy = _require_bounded_float(prec["conjugation_yield_pct"], f"{pfx}conj[{prot}].conjugation_yield_pct", lo=0.0, hi=100.0)
                pp["conjugation_yield_pct"] = cy
                if cy < 60.0:
                    fail_reasons.append(f"{rtag}CONJUGATION_YIELD_FAIL:{prot}={cy}%")
                elif cy < 80.0:
                    hold_reasons.append(f"{rtag}CONJUGATION_YIELD_INTERMEDIATE:{prot}={cy}%")
            if "peg_kda" not in prec or prec["peg_kda"] is None:
                missing_fields.append(f"{pfx}per_protein_conjugation_records.{prot}.peg_kda")
            else:
                pkda = _require_bounded_float(prec["peg_kda"], f"{pfx}conj[{prot}].peg_kda", lo=0.0, hi=1e6, strict_lo=True)
                pp["peg_kda"] = pkda
                mode, bound = peg_rules[prot]
                if (mode == "max" and pkda > bound) or (mode == "min" and pkda < bound):
                    fail_reasons.append(f"{rtag}PEG_TIER_VIOLATION:{prot}={pkda}kDa")
            if "unpaired_ecd_cys" not in prec or prec["unpaired_ecd_cys"] is None:
                missing_fields.append(f"{pfx}per_protein_conjugation_records.{prot}.unpaired_ecd_cys")
            else:
                uc = _require_exact_int(prec["unpaired_ecd_cys"], f"{pfx}conj[{prot}].unpaired_ecd_cys", lo=0)
                pp["unpaired_ecd_cys"] = uc
                if uc > 0:
                    fail_reasons.append(f"{rtag}UNPAIRED_ECD_CYS_FAIL:{prot}={uc}")
            if "free_thiol_mol_per_mol" not in prec or prec["free_thiol_mol_per_mol"] is None:
                missing_fields.append(f"{pfx}per_protein_conjugation_records.{prot}.free_thiol_mol_per_mol")
            else:
                ftm = _require_bounded_float(prec["free_thiol_mol_per_mol"], f"{pfx}conj[{prot}].free_thiol_mol_per_mol", lo=0.0, hi=1e6)
                pp["free_thiol_mol_per_mol"] = ftm
                if ftm > 0.20:
                    fail_reasons.append(f"{rtag}FREE_THIOL_MOL_FAIL:{prot}={ftm}")
                elif ftm > 0.05:
                    hold_reasons.append(f"{rtag}FREE_THIOL_MOL_INTERMEDIATE:{prot}={ftm}")
            parsed_conj[prot] = pp

    has_ftu, ftu_raw = _opt("free_thiol_um")
    free_thiol_um = _require_bounded_float(ftu_raw, f"{pfx}free_thiol_um", lo=0.0, hi=1e6) if has_ftu else None

    # Co-immobilized CD200R accessibility alongside sCR1_v2
    has_cd200r, cd200r_raw = _opt("coimmobilized_cd200r_accessibility_verified")
    cd200r_acc = _require_exact_bool(cd200r_raw, f"{pfx}coimmobilized_cd200r_accessibility_verified") if has_cd200r else None
    if cd200r_acc is False:
        fail_reasons.append(f"{rtag}COIMMOBILIZED_CD200R_ACCESSIBILITY_FAILED")

    has_cd200b, cd200b_raw = _opt("coimmobilized_cd200r_binding_pct_of_unhindered")
    cd200r_bind = _require_bounded_float(cd200b_raw, f"{pfx}coimmobilized_cd200r_binding_pct_of_unhindered", lo=0.0, hi=200.0) if has_cd200b else None
    if cd200r_bind is not None:
        if cd200r_bind < 50.0:
            fail_reasons.append(f"{rtag}COIMMOBILIZED_CD200R_BINDING_FAIL:{cd200r_bind}%")
        elif cd200r_bind < 75.0:
            hold_reasons.append(f"{rtag}COIMMOBILIZED_CD200R_BINDING_INTERMEDIATE:{cd200r_bind}%")
    if "cd200r_binding_pct" in src and src["cd200r_binding_pct"] is not None:
        b_alias = _require_bounded_float(src["cd200r_binding_pct"], f"{pfx}cd200r_binding_pct", lo=0.0, hi=200.0)
        if b_alias < 50.0:
            fail_reasons.append(f"{rtag}CD200R_BINDING_ALIAS_FAIL:{b_alias}%")
        elif b_alias < 75.0:
            hold_reasons.append(f"{rtag}CD200R_BINDING_ALIAS_INTERMEDIATE:{b_alias}%")
        if cd200r_bind is not None and b_alias != cd200r_bind:
            fail_reasons.append(f"{rtag}CD200R_BINDING_ALIAS_MISMATCH:{b_alias}!={cd200r_bind}")

    has_fasl, fasl_raw = _opt("fasl_present")
    fasl_present = _require_exact_bool(fasl_raw, f"{pfx}fasl_present") if has_fasl else None
    if fasl_present is True:
        fail_reasons.append(f"{rtag}GY013_RETIRED_ADIPOQ_HEXA_FASL_PRESENT")

    has_fllod, fllod_raw = _opt("fasl_lcms_below_llod")
    fasl_below = _require_exact_bool(fllod_raw, f"{pfx}fasl_lcms_below_llod") if has_fllod else None
    if fasl_below is False:
        fail_reasons.append(f"{rtag}FASL_LCMS_DETECTED_ABOVE_LLOD")

    has_cfh, cfh_raw = _opt("retired_cfh_19_445_absent")
    cfh_absent = _require_exact_bool(cfh_raw, f"{pfx}retired_cfh_19_445_absent") if has_cfh else None
    if cfh_absent is False:
        fail_reasons.append(f"{rtag}RETIRED_CFH_19_445_PRESENT_IN_PRIMARY_CUFF")

    has_mono, mono_raw = _opt("monomer_pct")
    mono = _require_bounded_float(mono_raw, f"{pfx}monomer_pct", lo=0.0, hi=100.0) if has_mono else None
    if mono is not None:
        if mono < 90.0:
            fail_reasons.append(f"{rtag}CUFF_MONOMER_FAIL:{mono}%")
        elif mono < 95.0:
            hold_reasons.append(f"{rtag}CUFF_MONOMER_INTERMEDIATE:{mono}%")

    has_ret, ret_raw = _opt("retention_30d_pct")
    ret30 = _require_bounded_float(ret_raw, f"{pfx}retention_30d_pct", lo=0.0, hi=100.0) if has_ret else None
    if ret30 is not None:
        if ret30 < 85.0:
            fail_reasons.append(f"{rtag}RETENTION_30D_FAIL:{ret30}%")
        elif ret30 < 95.0:
            hold_reasons.append(f"{rtag}RETENTION_30D_INTERMEDIATE:{ret30}%")

    has_lch, lch_raw = _opt("leaching_30d_serum_pct")
    lch = _require_bounded_float(lch_raw, f"{pfx}leaching_30d_serum_pct", lo=0.0, hi=100.0) if has_lch else None
    if lch is not None:
        if lch > 5.0:
            fail_reasons.append(f"{rtag}LEACHING_30D_FAIL:{lch}%")
        elif lch > 2.0:
            hold_reasons.append(f"{rtag}LEACHING_30D_INTERMEDIATE:{lch}%")

    # Paired intact-serum vs IgG-attributable CD16+ ADCC U95 (GY-023)
    has_pctrl, pctrl_raw = _opt("paired_igg_depleted_control_included")
    pctrl = _require_exact_bool(pctrl_raw, f"{pfx}paired_igg_depleted_control_included") if has_pctrl else None
    if pctrl is False:
        fail_reasons.append(f"{rtag}GY023_PAIRED_IGG_DEPLETED_CONTROL_MISSING")

    has_adn, adn_raw = _opt("adcc_donor_n")
    adn = _require_exact_int(adn_raw, f"{pfx}adcc_donor_n", lo=0) if has_adn else None
    if adn is not None:
        if adn == 0:
            fail_reasons.append(f"{rtag}GY023_ZERO_ADCC_DONORS")
        elif adn < 6:
            hold_reasons.append(f"{rtag}GY023_INSUFFICIENT_ADCC_DONORS:{adn}<6")
    _validate_optional_canonical_id_list(
        src,
        keys=("adcc_donor_ids", "donor_ids"),
        expected_count=adn,
        min_unique=6,
        pfx=pfx,
        rtag=rtag,
        module_tag="CUFF",
        fail_reasons=fail_reasons,
    )

    has_intact, intact_raw = _opt("intact_serum_total_killing_u95_pct")
    intact_u95 = _require_bounded_float(intact_raw, f"{pfx}intact_serum_total_killing_u95_pct", lo=0.0, hi=100.0) if has_intact else None
    if intact_u95 is not None:
        if intact_u95 > 15.0:
            fail_reasons.append(f"{rtag}GY023_INTACT_SERUM_KILLING_U95_FAIL:{intact_u95}%")
        elif intact_u95 > 5.0:
            hold_reasons.append(f"{rtag}GY023_INTACT_SERUM_KILLING_U95_INTERMEDIATE:{intact_u95}%")
    if "serum_killing_u95_pct" in src and src["serum_killing_u95_pct"] is not None:
        sk_alias = _require_bounded_float(src["serum_killing_u95_pct"], f"{pfx}serum_killing_u95_pct", lo=0.0, hi=100.0)
        if sk_alias > 15.0:
            fail_reasons.append(f"{rtag}SERUM_KILLING_ALIAS_U95_FAIL:{sk_alias}%")
        elif sk_alias > 5.0:
            hold_reasons.append(f"{rtag}SERUM_KILLING_ALIAS_U95_INTERMEDIATE:{sk_alias}%")
        if intact_u95 is not None and sk_alias != intact_u95:
            fail_reasons.append(f"{rtag}SERUM_KILLING_ALIAS_MISMATCH:{sk_alias}!={intact_u95}")

    has_padcc, padcc_raw = _opt("paired_igg_attributable_cd16_adcc_u95_pct")
    paired_u95 = _require_bounded_float(padcc_raw, f"{pfx}paired_igg_attributable_cd16_adcc_u95_pct", lo=0.0, hi=100.0) if has_padcc else None
    if paired_u95 is not None:
        if paired_u95 > 15.0:
            fail_reasons.append(f"{rtag}GY023_PAIRED_IGG_CD16_ADCC_U95_FAIL:{paired_u95}%")
        elif paired_u95 > 5.0:
            hold_reasons.append(f"{rtag}GY023_PAIRED_IGG_CD16_ADCC_U95_INTERMEDIATE:{paired_u95}%")
    if "cd16_adcc_u95_pct" in src and src["cd16_adcc_u95_pct"] is not None:
        adcc_alias = _require_bounded_float(src["cd16_adcc_u95_pct"], f"{pfx}cd16_adcc_u95_pct", lo=0.0, hi=100.0)
        if adcc_alias > 15.0:
            fail_reasons.append(f"{rtag}CD16_ADCC_ALIAS_U95_FAIL:{adcc_alias}%")
        elif adcc_alias > 5.0:
            hold_reasons.append(f"{rtag}CD16_ADCC_ALIAS_U95_INTERMEDIATE:{adcc_alias}%")