OpenIslet

scripts/verify_iip0041_clinical_h1_cd64t_siglec.py

EXP-028 (CAND-031 / Target IIP-0041 — Bucket 2A) Remediated Verification Suite

script Release v0.8.0 gen-eb955f8266035aba 984 KB · 69 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_iip0041_clinical_h1_cd64t_siglec.py · SHA-256: f004cf6d8259b8b4…

                            f"lot_id={c_lid!r} with non-infused disposition={disp_raw_r!r} (lot_type={lt_raw_r!r}); "
                            "ancestry verification and cluster statistics nullified fail-closed."
                        )
                        conflict = True
                    elif lt_r not in valid_primary_lot_types:
                        errs.append(
                            f"CLINICAL_PRIMARY_LOT_CMC_DISPOSITION_CONFLICT: clinical recipient {rid!r} claims "
                            f"infused primary lot_id={c_lid!r}, but raw PARALLEL_CMC_READINESS_LEDGER records "
                            f"lot_id={c_lid!r} with non-primary lot_type={lt_raw_r!r} (disposition={disp_raw_r!r}); "
                            "ancestry verification and cluster statistics nullified fail-closed."
                        )
                        conflict = True
                    elif type(inf_raw_r) is not int or inf_raw_r < 1:
                        errs.append(
                            f"CLINICAL_PRIMARY_LOT_CMC_DISPOSITION_CONFLICT: clinical recipient {rid!r} claims "
                            f"infused primary lot_id={c_lid!r}, but raw PARALLEL_CMC_READINESS_LEDGER records "
                            f"lot_id={c_lid!r} with invalid/zero infused_doses_n={inf_raw_r!r} "
                            f"(disposition={disp_raw_r!r}); ancestry verification and cluster statistics nullified fail-closed."
                        )
                        conflict = True
                    else:
                        has_valid_infused_primary_row = True
                        has_explicit_binding = (od_set_r is not None) or (rr_set_r is not None)
                        missing_in_supplied = (
                            (not has_explicit_binding)
                            or (od_pres_r and (od_set_r is None or rid not in od_set_r))
                            or (rr_pres_r and (rr_set_r is None or rid not in rr_set_r))
                            or (ri_pres_r and (ri_set_r is None or rid not in ri_set_r))
                        )
                        if missing_in_supplied:
                            any_row_missing_rid = True
                        else:
                            any_row_binds_rid_completely = True
                if has_valid_infused_primary_row and (
                    not any_row_binds_rid_completely or any_row_missing_rid
                ):
                    errs.append(
                        f"CLINICAL_PRIMARY_RECIPIENT_MISSING_FROM_CMC_LOT: clinical recipient {rid!r} claims "
                        f"infused primary lot_id={c_lid!r}, but raw PARALLEL_CMC_READINESS_LEDGER row for "
                        f"lot_id={c_lid!r} does not record recipient {rid!r} as infused across all supplied recipient bindings; "
                        "ancestry verification and cluster statistics nullified fail-closed."
                    )
                    conflict = True

    for rlid, rdisp, r_rid, r_prim in raw_rescue_rows:
        if r_prim is not None and r_prim != rlid:
            if r_prim not in raw_primary_lot_rids:
                errs.append(
                    f"CMC_ORPHAN_RESCUE_PRIMARY_LOT: raw PARALLEL_CMC_READINESS_LEDGER "
                    f"RESCUE_REPEAT_LOT {rlid!r} (disposition={rdisp!r}) declares rescued_primary_lot_id={r_prim!r}, "
                    "which does not match any infused initial primary lot in PARALLEL_CMC_READINESS_LEDGER; "
                    "ancestry verification and cluster statistics nullified fail-closed."
                )
                errs.append(
                    f"CLINICAL_CMC_RESCUE_IDENTITY_CONFLICT: raw PARALLEL_CMC_READINESS_LEDGER "
                    f"RESCUE_REPEAT_LOT {rlid!r} (disposition={rdisp!r}) declares orphan rescued_primary_lot_id={r_prim!r}; "
                    "ancestry verification and cluster statistics nullified fail-closed."
                )
                conflict = True
            elif r_rid is not None and r_rid not in raw_primary_lot_rids[r_prim]:
                errs.append(
                    f"CMC_ORPHAN_RESCUE_PRIMARY_LOT: raw PARALLEL_CMC_READINESS_LEDGER "
                    f"RESCUE_REPEAT_LOT {rlid!r} (disposition={rdisp!r}) declares rescued_recipient_id={r_rid!r} "
                    f"for rescued_primary_lot_id={r_prim!r}, but that recipient is absent from primary lot "
                    f"{r_prim!r} explicit recipient outcomes/records; ancestry verification and cluster statistics nullified fail-closed."
                )
                errs.append(
                    f"CLINICAL_CMC_RESCUE_IDENTITY_CONFLICT: raw PARALLEL_CMC_READINESS_LEDGER "
                    f"RESCUE_REPEAT_LOT {rlid!r} (disposition={rdisp!r}) declares rescued_recipient_id={r_rid!r} "
                    f"absent from linked primary lot {r_prim!r}; ancestry verification and cluster statistics nullified fail-closed."
                )
                conflict = True

    for wid_t, mcbs_t in sorted(tree_wcb_to_mcbs.items()):
        if len(mcbs_t) > 1:
            errs.append(
                f"CMC_ROW_ANCESTRY_CONFLICTS_WITH_VERIFIED_MANIFEST: NON_UNIQUE_WCB_PARENTAGE "
                f"across verified manifest and PARALLEL_CMC_READINESS_LEDGER: wcb_id={wid_t!r} "
                f"maps to multiple distinct MCBs {sorted(mcbs_t)}."
            )
            if wid_t in rescue_wcbs_seen:
                errs.append(
                    f"CLINICAL_CMC_RESCUE_IDENTITY_CONFLICT: rescue-linked wcb_id={wid_t!r} maps to "
                    f"multiple distinct MCBs {sorted(mcbs_t)} across verified manifest and PARALLEL_CMC_READINESS_LEDGER; "
                    "ancestry verification and cluster statistics nullified fail-closed."
                )
            conflict = True

    return sorted(dict.fromkeys(errs)), conflict


def _reconcile_raw_clinical_and_cmc_rescues(
    lot_ledger_raw: Any,
    *,
    seen_rids: set[str],
    clinical_recipient_meta: dict[str, dict[str, Any]],
    raw_ancestry_claims_by_rid: dict[str, list[Any]],
    raw_rescue_claims_by_rid: dict[str, list[dict[str, Any]]],
    duplicate_rids_set: set[str],
    duplicate_rescue_errors_by_rid: dict[str, list[str]],
    ref_mcb_map: dict[str, str],
    ref_wcb_map: dict[str, str],
) -> tuple[list[str], bool]:
    """
    Single-source-of-truth unconditional bidirectional raw clinical-vs-CMC rescue identity,
    cardinality, and bank-lineage reconciler executed BEFORE evaluate_cmc_attempted_lot_readiness
    so unrelated CMC shape errors (such as invalid split-lot planned_doses_n) can NEVER mask
    rescue-identity mismatches or bypass full fail-closed ancestry nullification.
    Returns (rescue_errors, has_rescue_ancestry_conflict).
    """
    if not isinstance(lot_ledger_raw, list) or len(lot_ledger_raw) == 0:
        return [], False

    errs: list[str] = []
    ancestry_conflict = False
    cmc_rescue_lots_by_rid: dict[str, list[str]] = {}
    cmc_rescue_lineages_by_rid: dict[str, list[tuple[str, str | None, str | None]]] = {}
    non_cohort_cmc_rescues: list[tuple[str, str]] = []
    malformed_cmc_rescue_errs: list[str] = []

    valid_disps = {"FAILED_RELEASE", "RELEASED_UNINFUSED", "RELEASED_INFUSED"}
    ordered_ledger_raw = sorted(lot_ledger_raw, key=_canonical_cmc_lot_sort_key)
    for entry in ordered_ledger_raw:
        if not isinstance(entry, dict):
            continue
        lt = _strict_literal_id(entry.get("lot_type"))
        if lt != "RESCUE_REPEAT_LOT":
            continue
        disp = _strict_literal_id(entry.get("disposition"))
        lid = _strict_literal_id(entry.get("lot_id"))
        mid = _strict_literal_id(entry.get("mcb_id"))
        wid_raw = entry.get("wcb_id") if entry.get("wcb_id") is not None else entry.get("working_cell_bank_id")
        wid = _strict_literal_id(wid_raw) if wid_raw is not None else None
        resc_rid = _strict_literal_id(entry.get("rescued_recipient_id"))
        if lid is None:
            ancestry_conflict = True
            continue
        if disp not in valid_disps:
            malformed_cmc_rescue_errs.append(
                f"CMC_RESCUE_RECIPIENT_BINDING_CONFLICT / CLINICAL_CMC_RESCUE_IDENTITY_CONFLICT: "
                f"raw PARALLEL_CMC_READINESS_LEDGER RESCUE_REPEAT_LOT {lid!r} has invalid "
                f"disposition={entry.get('disposition')!r}; ancestry verification and cluster statistics nullified fail-closed."
            )
            ancestry_conflict = True
            continue

        raw_out_dict = entry.get("recipient_outcomes")
        raw_out_recs = entry.get("recipient_records")
        raw_rids = entry.get("recipient_ids")

        if disp != "RELEASED_INFUSED":
            if raw_out_dict is not None and (not isinstance(raw_out_dict, dict) or len(raw_out_dict) > 0):
                malformed_cmc_rescue_errs.append(
                    f"CMC_RESCUE_RECIPIENT_BINDING_CONFLICT: raw PARALLEL_CMC_READINESS_LEDGER uninfused/failed "
                    f"RESCUE_REPEAT_LOT {lid!r} (disposition={disp!r}) must have empty recipient_outcomes; "
                    "ancestry verification and cluster statistics nullified fail-closed."
                )
                ancestry_conflict = True
            if raw_out_recs is not None and (not isinstance(raw_out_recs, list) or len(raw_out_recs) > 0):
                malformed_cmc_rescue_errs.append(
                    f"CMC_RESCUE_RECIPIENT_BINDING_CONFLICT: raw PARALLEL_CMC_READINESS_LEDGER uninfused/failed "
                    f"RESCUE_REPEAT_LOT {lid!r} (disposition={disp!r}) must have empty recipient_records; "
                    "ancestry verification and cluster statistics nullified fail-closed."
                )
                ancestry_conflict = True
            if raw_rids is not None and (not isinstance(raw_rids, list) or len(raw_rids) > 0):
                malformed_cmc_rescue_errs.append(
                    f"CMC_RESCUE_RECIPIENT_BINDING_CONFLICT: raw PARALLEL_CMC_READINESS_LEDGER uninfused/failed "
                    f"RESCUE_REPEAT_LOT {lid!r} (disposition={disp!r}) must have empty recipient_ids; "
                    "ancestry verification and cluster statistics nullified fail-closed."
                )
                ancestry_conflict = True
            continue

        # disp == "RELEASED_INFUSED": unconditionally reconcile resc_rid AND ALL recipient/outcome binding fields!
        bound_sets: list[tuple[str, list[str]]] = []
        if raw_out_dict is None and raw_out_recs is None:
            malformed_cmc_rescue_errs.append(
                f"CMC_RESCUE_RECIPIENT_BINDING_CONFLICT / CLINICAL_CMC_RESCUE_IDENTITY_CONFLICT: "
                f"raw PARALLEL_CMC_READINESS_LEDGER infused RESCUE_REPEAT_LOT {lid!r} is missing explicit "
                f"recipient_outcomes or recipient_records binding for rescued_recipient_id={resc_rid!r}; "
                "ancestry verification and cluster statistics nullified fail-closed."
            )
            ancestry_conflict = True

        if raw_out_dict is not None:
            if not isinstance(raw_out_dict, dict):
                malformed_cmc_rescue_errs.append(
                    f"CMC_RESCUE_RECIPIENT_BINDING_CONFLICT / CLINICAL_CMC_RESCUE_IDENTITY_CONFLICT: "
                    f"raw PARALLEL_CMC_READINESS_LEDGER infused RESCUE_REPEAT_LOT {lid!r} recipient_outcomes must be a dict; "
                    "ancestry verification and cluster statistics nullified fail-closed."
                )
                ancestry_conflict = True
            else:
                valid_od_keys: list[str] = []
                bad_od_keys: list[Any] = []
                for rk in sorted(raw_out_dict.keys(), key=repr):
                    lr = _strict_literal_id(rk)
                    if lr is None:
                        bad_od_keys.append(rk)
                    else:
                        valid_od_keys.append(lr)
                if bad_od_keys:
                    malformed_cmc_rescue_errs.append(
                        f"CMC_RESCUE_RECIPIENT_BINDING_CONFLICT / CLINICAL_CMC_RESCUE_IDENTITY_CONFLICT: "
                        f"raw PARALLEL_CMC_READINESS_LEDGER infused RESCUE_REPEAT_LOT {lid!r} recipient_outcomes "
                        f"contains invalid literal recipient_id key(s) {bad_od_keys!r}; "
                        "ancestry verification and cluster statistics nullified fail-closed."
                    )
                    ancestry_conflict = True
                else:
                    valid_od_keys_sorted = sorted(valid_od_keys)
                    bound_sets.append(("recipient_outcomes", valid_od_keys_sorted))
                    if resc_rid is not None and valid_od_keys_sorted != [resc_rid]:
                        malformed_cmc_rescue_errs.append(
                            f"CMC_RESCUE_RECIPIENT_BINDING_CONFLICT / CLINICAL_CMC_RESCUE_IDENTITY_CONFLICT: "
                            f"raw PARALLEL_CMC_READINESS_LEDGER infused RESCUE_REPEAT_LOT {lid!r} declares "
                            f"recipient_outcomes keys={valid_od_keys_sorted!r}, contradicting "
                            f"rescued_recipient_id={resc_rid!r}; ancestry verification and cluster statistics nullified fail-closed."
                        )
                        ancestry_conflict = True
                    elif resc_rid is None and len(valid_od_keys_sorted) != 1:
                        malformed_cmc_rescue_errs.append(
                            f"CMC_RESCUE_RECIPIENT_BINDING_CONFLICT / CLINICAL_CMC_RESCUE_IDENTITY_CONFLICT: "
                            f"raw PARALLEL_CMC_READINESS_LEDGER infused RESCUE_REPEAT_LOT {lid!r} declares "
                            f"recipient_outcomes keys={valid_od_keys_sorted!r} (expected exactly 1 rescued recipient); "
                            "ancestry verification and cluster statistics nullified fail-closed."
                        )
                        ancestry_conflict = True

        if raw_out_recs is not None:
            if not isinstance(raw_out_recs, list):
                malformed_cmc_rescue_errs.append(
                    f"CMC_RESCUE_RECIPIENT_BINDING_CONFLICT / CLINICAL_CMC_RESCUE_IDENTITY_CONFLICT: "
                    f"raw PARALLEL_CMC_READINESS_LEDGER infused RESCUE_REPEAT_LOT {lid!r} recipient_records must be a list; "
                    "ancestry verification and cluster statistics nullified fail-closed."
                )
                ancestry_conflict = True
            else:
                valid_rr_ids: list[str] = []
                rr_malformed = False
                for r_obj in raw_out_recs:
                    if not isinstance(r_obj, dict):
                        rr_malformed = True
                        continue
                    lr = _strict_literal_id(r_obj.get("recipient_id"))
                    if lr is None:
                        rr_malformed = True
                    else:
                        valid_rr_ids.append(lr)