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…

    if rec_arm_manifest_sha != EXPECTED_CANONICAL_MANIFEST_SHA256:
        fail_reasons.append("CURRENT_ASSAY_GATE35_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("CURRENT_ASSAY_GATE35_CONSTRUCT_MANIFEST_SHA256_INVALID_OR_CONFLATED")
    else:
        valid_construct_sha = rec_construct_sha
        if expected_construct_sha is not None and rec_construct_sha != expected_construct_sha:
            fail_reasons.append("CURRENT_ASSAY_GATE35_CONSTRUCT_MANIFEST_SHA256_MISMATCH")

    if not _is_non_all_zero_sha256_hex(rec_archive_sha):
        fail_reasons.append("CURRENT_ASSAY_GATE35_INVALID_OR_ZERO_ARCHIVE_SHA256")
    if rec_tier not in ALLOWED_EVIDENCE_TIERS:
        fail_reasons.append("CURRENT_ASSAY_GATE35_INVALID_EVIDENCE_TIER")
    elif expected_evidence_tier is not None and rec_tier != expected_evidence_tier:
        fail_reasons.append("CURRENT_ASSAY_GATE35_EVIDENCE_TIER_MISMATCH")
    if rec_authority_scope not in ALLOWED_AUTHORITY_SCOPES:
        fail_reasons.append("CURRENT_ASSAY_GATE35_INVALID_AUTHORITY_SCOPE")

    if record_id in [x for x in upstream_record_ids if x is not None]:
        fail_reasons.append("CURRENT_ASSAY_GATE35_RECORD_ID_CONFLATED_WITH_UPSTREAM")
    if rec_archive_sha in [x for x in upstream_archive_shas if x is not None]:
        fail_reasons.append("CURRENT_ASSAY_GATE35_ARCHIVE_SHA256_CONFLATED_WITH_UPSTREAM")

    cur_scale = cur_rec.get("scale_mode")
    needs_clinical_authority = is_promo and (cur_scale != "ANIMAL_PRECLINICAL_SCALE")

    if rec_tier == "AUTHENTICATED_ARCHIVED_WETLAB":
        if needs_clinical_authority:
            if rec_authority_scope != "CLINICAL_LOT_RELEASE":
                fail_reasons.append("CURRENT_ASSAY_GATE35_AUTHORITY_SCOPE_NOT_CLINICAL_LOT_RELEASE")
            if rec_promo_auth is not True:
                fail_reasons.append("CURRENT_ASSAY_GATE35_PROMOTION_AUTHORIZATION_WITHHELD")
            if rec_bank_qualified is not True:
                fail_reasons.append("CURRENT_ASSAY_GATE35_BANK_NOT_QUALIFIED")
            if rec_decision not in ALLOWED_CURRENT_ASSAY_CLINICAL_DECISIONS:
                fail_reasons.append("CURRENT_ASSAY_GATE35_UNAPPROVED_DECISION")
        else:
            if rec_authority_scope not in ("CLINICAL_LOT_RELEASE", "BANK_QUALIFICATION_ONLY"):
                fail_reasons.append("CURRENT_ASSAY_GATE35_AUTHORITY_SCOPE_REJECTED")
            if rec_decision not in ALLOWED_CURRENT_ASSAY_NONPROMO_DECISIONS:
                fail_reasons.append("CURRENT_ASSAY_GATE35_UNAPPROVED_DECISION")
    elif rec_tier == "SYNTHETIC_FIXTURE":
        if rec_authority_scope != "SYNTHETIC_SELF_TEST":
            fail_reasons.append("CURRENT_ASSAY_GATE35_SYNTHETIC_AUTHORITY_SCOPE_MISMATCH")
        if rec_promo_auth is not False:
            fail_reasons.append("CURRENT_ASSAY_GATE35_SYNTHETIC_CANNOT_AUTHORIZE_PROMOTION")
        if rec_decision not in ALLOWED_CURRENT_ASSAY_SYNTHETIC_DECISIONS:
            fail_reasons.append("CURRENT_ASSAY_GATE35_UNAPPROVED_DECISION")
        try:
            recomp_synth = _canonical_json_sha256({k: v for k, v in cur_rec.items() if k != "archive_sha256"})
        except (TypeError, ValueError, OverflowError, DecimalException):
            fail_reasons.append("CURRENT_ASSAY_GATE35_SUBMITTED_BODY_UNSERIALIZABLE")
        else:
            if recomp_synth != rec_archive_sha:
                fail_reasons.append("CURRENT_ASSAY_GATE35_SUBMITTED_BODY_DIGEST_MISMATCH")

    verified_body: Dict[str, Any] = dict(cur_rec)
    if rec_tier == "AUTHENTICATED_ARCHIVED_WETLAB":
        if trusted_evidence_store is None:
            hold_reasons.append("MISSING_TRUSTED_EVIDENCE_STORE_FOR_CURRENT_ASSAY_GATE35")
        elif not isinstance(trusted_evidence_store, Mapping):
            fail_reasons.append("INVALID_TRUSTED_EVIDENCE_STORE")
        else:
            stored_dict = _resolve_store_entry_with_alias_consistency(
                rec=cur_rec,
                record_id=record_id,
                rec_archive_sha=rec_archive_sha,
                prefix="CURRENT_ASSAY_GATE35",
                trusted_evidence_store=trusted_evidence_store,
                fail_reasons=fail_reasons,
            )
            if stored_dict is not None:
                verified_body = stored_dict
                _check_no_contradictory_authority_flags(stored_dict, "CURRENT_ASSAY_GATE35_TRUSTED_STORE", fail_reasons)
                for field in REQUIRED_UPSTREAM_SCHEMA_V2_ALL_FIELDS:
                    s_val = stored_dict.get(field)
                    r_val = cur_rec.get(field)
                    if type(s_val) is not type(r_val) or s_val != r_val:
                        fail_reasons.append(f"CURRENT_ASSAY_GATE35_TRUSTED_STORE_FIELD_MISMATCH:{field}")
                if stored_dict.get("evidence_tier") != "AUTHENTICATED_ARCHIVED_WETLAB":
                    fail_reasons.append("CURRENT_ASSAY_GATE35_TRUSTED_STORE_NOT_WETLAB_TIER")
                if stored_dict.get("gate_source") != "GATE-35":
                    if stored_dict.get("gate_source") == "GATE-33":
                        fail_reasons.append("CURRENT_ASSAY_GATE35_LIVE_GATE33_ROLE_COLLISION_REJECTED")
                    fail_reasons.append("CURRENT_ASSAY_GATE35_TRUSTED_STORE_GATE_SOURCE_MISMATCH")
                s_scale = stored_dict.get("scale_mode")
                s_needs_clin = is_promo and (s_scale != "ANIMAL_PRECLINICAL_SCALE")
                if s_needs_clin:
                    if stored_dict.get("authority_scope") != "CLINICAL_LOT_RELEASE":
                        fail_reasons.append("CURRENT_ASSAY_GATE35_TRUSTED_STORE_AUTHORITY_SCOPE_MISMATCH")
                    if stored_dict.get("promotion_authorized") is not True:
                        fail_reasons.append("CURRENT_ASSAY_GATE35_TRUSTED_STORE_PROMOTION_WITHHELD")
                    if stored_dict.get("bank_qualified") is not True:
                        fail_reasons.append("CURRENT_ASSAY_GATE35_TRUSTED_STORE_BANK_NOT_QUALIFIED")
                    if stored_dict.get("decision") not in ALLOWED_CURRENT_ASSAY_CLINICAL_DECISIONS:
                        fail_reasons.append("CURRENT_ASSAY_GATE35_TRUSTED_STORE_DECISION_REJECTED")
                else:
                    if stored_dict.get("authority_scope") not in ("CLINICAL_LOT_RELEASE", "BANK_QUALIFICATION_ONLY"):
                        fail_reasons.append("CURRENT_ASSAY_GATE35_TRUSTED_STORE_AUTHORITY_SCOPE_MISMATCH")
                    if stored_dict.get("decision") not in ALLOWED_CURRENT_ASSAY_NONPROMO_DECISIONS:
                        fail_reasons.append("CURRENT_ASSAY_GATE35_TRUSTED_STORE_DECISION_REJECTED")

    return verified_body, valid_construct_sha


def _extract_areal_loading_with_alias(
    source: Mapping[str, Any],
    *,
    fail_reasons: List[str],
    mismatch_code: str,
) -> Any:
    """Resolve areal loading from areal_loading_ieq_cm2 / areal_loading_ieq_per_cm2 enforcing exact Decimal alias consistency."""
    v1 = source.get("areal_loading_ieq_cm2")
    v2 = source.get("areal_loading_ieq_per_cm2")
    if v1 is not None and v2 is not None:
        if not _is_finite_number(v1) or not _is_finite_number(v2) or _to_exact_decimal(v1) != _to_exact_decimal(v2):
            fail_reasons.append(mismatch_code)
    return v1 if v1 is not None else v2


def _reconcile_parent_against_current_archive(
    payload: Mapping[str, Any],
    verified_archive: Mapping[str, Any],
    *,
    is_promo: bool,
    fail_reasons: List[str],
) -> bool:
    """Enforce exact strict equality between parent fields and verified current_assay_evidence_record (Review #4).

    Returns True if ANY discrepancy was detected between parent and verified archive.
    """
    any_diff = False
    arch_site = verified_archive.get("deployment_site")
    parent_site = payload.get("deployment_site")
    arch_exv = verified_archive.get("exvivo_rest_applied")
    parent_exv = payload.get("exvivo_rest_applied")

    required_fields: List[str] = list(CURRENT_ASSAY_ALWAYS_BOUND_FIELDS)
    if is_promo:
        required_fields.extend(CURRENT_ASSAY_STAGE1_COMMON_BOUND_FIELDS)
        if arch_site == "R4_OMENTUM_LEAD" or parent_site == "R4_OMENTUM_LEAD":
            required_fields.extend(CURRENT_ASSAY_STAGE1_OMENTUM_BOUND_FIELDS)
    if arch_exv is True or parent_exv is True:
        required_fields.extend(CURRENT_ASSAY_EXVIVO_REST_STAGE0_BOUND_FIELDS)
        if is_promo:
            required_fields.extend(CURRENT_ASSAY_EXVIVO_REST_STAGE1_BOUND_FIELDS)

    for fname in required_fields:
        p_val = payload.get(fname)
        a_val = verified_archive.get(fname)
        if p_val is None and a_val is None:
            continue
        if a_val is None and p_val is not None:
            fail_reasons.append(f"CURRENT_ASSAY_ARCHIVE_MISSING_REQUIRED_FIELD:{fname}")
            any_diff = True
        elif p_val is None and a_val is not None:
            fail_reasons.append(f"CURRENT_ASSAY_PARENT_MISSING_ARCHIVED_FIELD:{fname}")
            any_diff = True
        elif not _strict_json_equal(p_val, a_val):
            fail_reasons.append(f"CURRENT_ASSAY_ARCHIVE_FIELD_MISMATCH:{fname}")
            any_diff = True

    # Alias-aware areal loading reconciliation (areal_loading_ieq_cm2 / areal_loading_ieq_per_cm2)
    p_load = _extract_areal_loading_with_alias(
        payload, fail_reasons=fail_reasons, mismatch_code="INCONSISTENT_AREAL_LOADING_ALIAS_FIELD",
    )
    a_load = _extract_areal_loading_with_alias(
        verified_archive, fail_reasons=fail_reasons, mismatch_code="CURRENT_ASSAY_ARCHIVE_INCONSISTENT_AREAL_LOADING_ALIAS",
    )
    if p_load is None and a_load is None:
        pass
    elif a_load is None and p_load is not None:
        fail_reasons.append("CURRENT_ASSAY_ARCHIVE_MISSING_REQUIRED_FIELD:areal_loading_ieq_per_cm2")
        any_diff = True
    elif p_load is None and a_load is not None:
        fail_reasons.append("CURRENT_ASSAY_PARENT_MISSING_ARCHIVED_FIELD:areal_loading_ieq_cm2")
        any_diff = True
    elif not _strict_json_equal(p_load, a_load):
        fail_reasons.append("CURRENT_ASSAY_ARCHIVE_FIELD_MISMATCH:areal_loading_ieq_cm2")
        any_diff = True

    # Optional measured_bed_po2_mmhg consistency when present in either parent or archive
    p_leg_bed = payload.get("measured_bed_po2_mmhg")
    a_leg_bed = verified_archive.get("measured_bed_po2_mmhg")
    if (p_leg_bed is not None or a_leg_bed is not None) and not _strict_json_equal(p_leg_bed, a_leg_bed):
        fail_reasons.append("CURRENT_ASSAY_ARCHIVE_FIELD_MISMATCH:measured_bed_po2_mmhg")
        any_diff = True

    return any_diff


def _evaluate_gate33_measurements(
    obs: Mapping[str, Any],
    *,
    is_promo: bool,
    valid_lot_id: Optional[str],
    arm_spec: Optional[Dict[str, Any]],
    fail_reasons: List[str],
    hold_reasons: List[str],
) -> Dict[str, Any]:
    """Evaluate all Gate-33 CMC, physical geometry, per-leaf reconciliation, actual-leaf steric packing,
    scope-bound scale_mode, Stage-1 porcine/NHP cohorts, and ex-vivo rest cutoffs on the given observation source.
    """
    site = obs.get("deployment_site")
    valid_site: Optional[str] = None
    if site is None:
        hold_reasons.append("MISSING_DEPLOYMENT_SITE")
    elif not _is_literal_nonempty_str(site) or site not in ("R4_OMENTUM_LEAD", "R4_SARS_FREE_BACKUP"):
        fail_reasons.append("INVALID_OR_UNREGISTERED_DEPLOYMENT_SITE")
    else:
        valid_site = site

    # 4. Clinical matrix, sterility, mycoplasma, PPCN quarantine, needle-track ban, and strict allowlist Cuff safety
    matrix = obs.get("clinical_matrix_id")
    if matrix is None:
        hold_reasons.append("MISSING_CLINICAL_MATRIX_ID")
    elif not _is_literal_nonempty_str(matrix):
        fail_reasons.append("INVALID_CLINICAL_MATRIX_ID")
    elif matrix == "PPCN_CITRATE_PNIPAAM":
        fail_reasons.append("PPCN_QUARANTINED_STAGE0_RESEARCH_COMPARATOR_ONLY")
    elif valid_site == "R4_OMENTUM_LEAD" and matrix not in ALLOWED_CLINICAL_MATRICES_OMENTUM:
        fail_reasons.append("OMENTUM_CLINICAL_LEAD_REQUIRES_GMP_PLASMA_THROMBIN_OR_FIBRIN")
    elif valid_site == "R4_SARS_FREE_BACKUP" and matrix not in ALLOWED_CLINICAL_MATRICES_SARS_BACKUP:
        fail_reasons.append("SARS_BACKUP_REQUIRES_BIODEGRADABLE_THIN_PLANAR_CARRIER_NO_NEEDLE_TRACKS")

    needle_tracks = obs.get("needle_microtracks_used")
    if needle_tracks is None:
        hold_reasons.append("MISSING_NEEDLE_MICROTRACKS_BOOL")
    elif not _is_strict_bool(needle_tracks):
        fail_reasons.append("INVALID_NEEDLE_MICROTRACKS_BOOL")
    elif needle_tracks:
        fail_reasons.append("GY028_3_NEEDLE_MICROTRACK_BOLUS_PROHIBITED")

    cuff_agents = obs.get("coformulated_immune_cuff_agents")
    if cuff_agents is None:
        hold_reasons.append("MISSING_CUFF_AGENT_MANIFEST")
    elif not isinstance(cuff_agents, list) or not all(_is_literal_nonempty_str(x) for x in cuff_agents):
        fail_reasons.append("INVALID_CUFF_AGENT_MANIFEST")
    else:
        for ag in cuff_agents:
            if _is_fasl_or_cd95_token(ag):
                fail_reasons.append(f"R4O_02_HARD_VETO_FASL_PROHIBITED:{ag}")
            elif ag not in ALLOWED_CUFF_COMPONENTS_ALLOWLIST:
                fail_reasons.append(f"UNREGISTERED_CUFF_COMPONENT_REJECTED:{ag}")
        if arm_spec is not None and arm_spec.get("cuff_v2_eligible") is False and len(cuff_agents) > 0:
            fail_reasons.append("DESIGN_A_STRICTLY_PROHIBITS_IMMOBILIZED_CUFF_BIOLOGICS")

    polyglactin_used = obs.get("polyglactin_sutures_used")