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…

        # Recompute canonical JSON SHA-256 of archived record to defeat digest forgery/tampering
        try:
            recomp_sha = compute_canonical_record_sha256(archived)
        except (TypeError, ValueError, OverflowError):
            fail_reasons.append(f"FAIL_{prefix}_ARCHIVED_RECORD_UNSERIALIZABLE")
            return None, False
        if archived.get("archive_sha256") != arch_sha or recomp_sha != arch_sha:
            fail_reasons.append(f"FAIL_{prefix}_ARCHIVED_DIGEST_TAMPER_OR_MISMATCH")
            return None, False

        # Prevent inline caller payload from disagreeing (value or strict type) with the cryptographically archived record
        for k_in, v_in in raw_rec.items():
            if k_in == "record_sha256":
                continue
            if k_in not in archived or not _strict_json_equal(archived[k_in], v_in):
                fail_reasons.append(f"FAIL_{prefix}_INLINE_VS_ARCHIVED_MISMATCH")
                return None, False

        # Validate all 14 required fields of v2_round5_unified schema
        for req_f in UPSTREAM_RECEIPT_SCHEMA_V1_REQUIRED_FIELDS:
            if req_f not in archived:
                fail_reasons.append(f"FAIL_{prefix}_MISSING_OR_INVALID_SCHEMA_V1_{req_f.upper()}")
                return None, False
            if req_f in UPSTREAM_RECEIPT_BOOL_FIELDS:
                if not _is_strict_bool(archived[req_f]):
                    fail_reasons.append(f"FAIL_{prefix}_MISSING_OR_INVALID_SCHEMA_V1_{req_f.upper()}")
                    return None, False
            else:
                if not _is_clean_str(archived[req_f]):
                    fail_reasons.append(f"FAIL_{prefix}_MISSING_OR_INVALID_SCHEMA_V1_{req_f.upper()}")
                    return None, False

        rec_ok = True
        if archived["gate_source"] != expected_gate_source:
            fail_reasons.append(f"FAIL_{prefix}_GATE_SOURCE_MISMATCH")
            rec_ok = False
        if product_id_str is not None and archived["product_id"] != product_id_str:
            fail_reasons.append(f"FAIL_{prefix}_PRODUCT_ID_MISMATCH")
            rec_ok = False
        if lot_id_str is not None and archived["lot_id"] != lot_id_str:
            fail_reasons.append(f"FAIL_{prefix}_LOT_ID_MISMATCH")
            rec_ok = False
        if bank_id_str is not None and archived["bank_id"] != bank_id_str:
            fail_reasons.append(f"FAIL_{prefix}_BANK_ID_MISMATCH")
            rec_ok = False
        if archived["bank_origin"] not in ALLOWED_BANK_ORIGINS:
            fail_reasons.append(f"FAIL_{prefix}_INVALID_BANK_ORIGIN_ENUM")
            rec_ok = False
        if bank_origin_str is not None and archived["bank_origin"] != bank_origin_str:
            fail_reasons.append(f"FAIL_{prefix}_BANK_ORIGIN_MISMATCH")
            rec_ok = False
        if arm_id_str is not None and archived["bank_origin"] not in ALLOWED_BANK_ORIGINS_BY_ARM[arm_id_str]:
            fail_reasons.append(f"FAIL_{prefix}_BANK_ORIGIN_ARM_MISMATCH")
            rec_ok = False
        if not _is_nonzero_sha256_hex(archived["canonical_arm_manifest_sha256"]) or archived["canonical_arm_manifest_sha256"] != EXPECTED_ARM_MANIFEST_SHA256:
            fail_reasons.append(f"FAIL_{prefix}_CANONICAL_ARM_MANIFEST_SHA256_MISMATCH")
            rec_ok = False
        if not _is_nonzero_sha256_hex(archived["construct_manifest_sha256"]):
            fail_reasons.append(f"FAIL_{prefix}_INVALID_OR_ZERO_CONSTRUCT_MANIFEST_SHA256")
            rec_ok = False
        elif archived["construct_manifest_sha256"] in (EXPECTED_ARM_MANIFEST_SHA256, EXPECTED_ARM_MANIFEST_CANONICAL_SHA256):
            fail_reasons.append(f"FAIL_{prefix}_CONSTRUCT_MANIFEST_SHA256_CONFLATED_WITH_ARM_MANIFEST")
            rec_ok = False
        elif construct_sha_str is not None and archived["construct_manifest_sha256"] != construct_sha_str:
            fail_reasons.append(f"FAIL_{prefix}_CONSTRUCT_MANIFEST_SHA256_MISMATCH")
            rec_ok = False
        if archived["authority_scope"] not in ALLOWED_AUTHORITY_SCOPES:
            fail_reasons.append(f"FAIL_{prefix}_INVALID_AUTHORITY_SCOPE")
            rec_ok = False
        rec_tier = archived["evidence_tier"]
        if rec_tier not in ALLOWED_EVIDENCE_TIERS:
            fail_reasons.append(f"FAIL_{prefix}_INVALID_EVIDENCE_TIER")
            rec_ok = False
        else:
            resolved_upstream_tiers.append(rec_tier)
            if evidence_tier_str == "AUTHENTICATED_ARCHIVED_WETLAB" and rec_tier != "AUTHENTICATED_ARCHIVED_WETLAB":
                fail_reasons.append(f"FAIL_{prefix}_SYNTHETIC_UPSTREAM_IN_WETLAB_SUBMISSION")
                rec_ok = False
            if rec_tier == "SYNTHETIC_FIXTURE" and archived["promotion_authorized"] is True:
                fail_reasons.append(f"FAIL_{prefix}_SYNTHETIC_RECORD_CLAIMS_PROMOTION_AUTHORIZED")
                rec_ok = False
        if archived["decision"] not in allowed_decisions:
            fail_reasons.append(f"FAIL_{prefix}_DECISION_NOT_PASS")
            rec_ok = False

        # Reject contradictory adverse producer physical-qualification flags on EVERY archived record (Audit R4-01)
        if "physical_qualification_complete" in archived:
            if not _is_strict_bool(archived["physical_qualification_complete"]) or archived["physical_qualification_complete"] is not True:
                fail_reasons.append(f"FAIL_{prefix}_PHYSICAL_QUALIFICATION_INCOMPLETE")
                rec_ok = False
        if "release_authorizing" in archived:
            if not _is_strict_bool(archived["release_authorizing"]) or archived["release_authorizing"] is not True:
                fail_reasons.append(f"FAIL_{prefix}_RELEASE_AUTHORIZING_FALSE")
                rec_ok = False
        if "modeled_qualification_only" in archived:
            if not _is_strict_bool(archived["modeled_qualification_only"]) or archived["modeled_qualification_only"] is not False:
                fail_reasons.append(f"FAIL_{prefix}_MODELED_QUALIFICATION_ONLY_PROHIBITED")
                rec_ok = False
        if "qualification_scope" in archived:
            qscope = archived["qualification_scope"]
            if not _is_clean_str(qscope) or "MODELED" in qscope.upper() or "PENDING_PHYSICAL" in qscope.upper():
                fail_reasons.append(f"FAIL_{prefix}_MODELED_QUALIFICATION_SCOPE_PROHIBITED")
                rec_ok = False

        # Coherent (decision, authority_scope, bank_qualified, promotion_authorized) tuple check on supporting archives (Review #2)
        if role == "supporting":
            ascope = archived["authority_scope"]
            adec = archived["decision"]
            abq = archived["bank_qualified"]
            apa = archived["promotion_authorized"]
            if ascope in PROHIBITED_SUPPORTING_AUTHORITY_SCOPES:
                fail_reasons.append(f"FAIL_{prefix}_PROHIBITED_AUTHORITY_SCOPE_{ascope}")
                rec_ok = False
            if rec_tier == "AUTHENTICATED_ARCHIVED_WETLAB":
                if wetlab_allowed_decisions is not None and adec not in wetlab_allowed_decisions:
                    fail_reasons.append(f"FAIL_{prefix}_WETLAB_DECISION_INVALID")
                    rec_ok = False
                if ascope != "CLINICAL_LOT_RELEASE":
                    fail_reasons.append(f"FAIL_{prefix}_CLINICAL_LOT_RELEASE_SCOPE_REQUIRED")
                    rec_ok = False
                if abq is not True:
                    fail_reasons.append(f"FAIL_{prefix}_BANK_QUALIFIED_REQUIRED")
                    rec_ok = False
                if apa is not False:
                    fail_reasons.append(f"FAIL_{prefix}_SUPPORTING_ARCHIVE_CANNOT_SELF_GRANT_PROMOTION")
                    rec_ok = False
            elif rec_tier == "SYNTHETIC_FIXTURE":
                if synthetic_allowed_decisions is not None and adec not in synthetic_allowed_decisions:
                    fail_reasons.append(f"FAIL_{prefix}_SYNTHETIC_DECISION_INVALID")
                    rec_ok = False
                if ascope != "SYNTHETIC_SELF_TEST":
                    fail_reasons.append(f"FAIL_{prefix}_SYNTHETIC_AUTHORITY_SCOPE_REQUIRED")
                    rec_ok = False
                if abq is not False or apa is not False:
                    fail_reasons.append(f"FAIL_{prefix}_SYNTHETIC_AUTHORITY_FLAGS_MUST_BE_FALSE")
                    rec_ok = False
        return archived, rec_ok

    def _resolve_archived_record(
        rec_key: str,
        *,
        prefix: str,
        expected_gate_source: str,
        allowed_decisions: FrozenSet[str],
        role: str = "supporting",
        wetlab_allowed_decisions: Optional[FrozenSet[str]] = None,
        synthetic_allowed_decisions: Optional[FrozenSet[str]] = None,
    ) -> Tuple[Optional[Dict[str, Any]], bool]:
        return _resolve_archived_record_obj(
            payload.get(rec_key),
            prefix=prefix,
            expected_gate_source=expected_gate_source,
            allowed_decisions=allowed_decisions,
            role=role,
            wetlab_allowed_decisions=wetlab_allowed_decisions,
            synthetic_allowed_decisions=synthetic_allowed_decisions,
        )

    # Resolve archived construct manifest record FIRST (binds switch_registry_by_id, cHS4 identity, construct->digest map, & AAV delivery modality!)
    construct_rec, construct_verified = _resolve_archived_record(
        "construct_manifest_record",
        prefix="CONSTRUCT_MANIFEST_RECORD",
        expected_gate_source="CONSTRUCT_MANIFEST_REGISTRY",
        allowed_decisions=frozenset({"REGISTERED"}),
        role="supporting",
        wetlab_allowed_decisions=frozenset({"REGISTERED"}),
        synthetic_allowed_decisions=frozenset({"REGISTERED"}),
    )
    construct_contains_chs4_fi = False
    construct_contains_cbx3_ucoe = False
    construct_is_aav_delivered: Optional[bool] = None
    construct_ids_verified: List[str] = []
    construct_seq_sha_map_verified: Dict[str, str] = {}
    registered_primary_switch_id: Optional[str] = None
    registered_primary_switch_locus: Optional[str] = None
    registered_primary_benchmark_arm: Optional[str] = None
    registered_primary_is_icasp8: Optional[bool] = None
    registered_primary_is_d315a: Optional[bool] = None
    registered_primary_switch_verified = False
    registered_secondary_switch_id: Optional[str] = None
    registered_secondary_switch_locus: Optional[str] = None
    registered_secondary_switch_seq_sha: Optional[str] = None
    registered_secondary_switch_verified = False

    if construct_rec is not None:
        cids = construct_rec.get("construct_ids")
        seq_map = construct_rec.get("sequence_sha256_by_construct_id")
        dmods = construct_rec.get("delivery_modalities")
        if not isinstance(cids, list) or len(cids) == 0 or any(not _is_clean_str(x) for x in cids):
            fail_reasons.append("FAIL_CONSTRUCT_MANIFEST_INVALID_CONSTRUCT_IDS")
            construct_verified = False
        elif not isinstance(seq_map, dict) or set(seq_map.keys()) != set(cids) or any(not _is_nonzero_sha256_hex(v) for v in seq_map.values()):
            fail_reasons.append("FAIL_CONSTRUCT_MANIFEST_INVALID_SEQUENCE_SHA256_MAP")
            construct_verified = False
        else:
            construct_ids_verified = list(cids)
            construct_seq_sha_map_verified = {str(k): str(v) for k, v in seq_map.items()}
            if CHS4_0C9_V090_SEQ_SHA256 in construct_seq_sha_map_verified.values() or any("chs4_0c9" in cid.lower() for cid in cids):
                construct_contains_chs4_fi = True
            if CBX3_700BP_UCOE_SEQ_SHA256 in construct_seq_sha_map_verified.values() or any("cbx3" in cid.lower() for cid in cids):
                construct_contains_cbx3_ucoe = True
        if not isinstance(dmods, list) or len(dmods) == 0 or any(not _is_clean_str(x) for x in dmods):
            fail_reasons.append("FAIL_CONSTRUCT_MANIFEST_INVALID_DELIVERY_MODALITIES")
            construct_verified = False
        else:
            construct_is_aav_delivered = any("aav" in m.lower() for m in dmods)

        # Require verified switch_registry_by_id inside construct_manifest_record (Review #1)
        sw_reg = construct_rec.get("switch_registry_by_id")
        if not isinstance(sw_reg, dict) or len(sw_reg) == 0:
            fail_reasons.append("FAIL_CONSTRUCT_MANIFEST_MISSING_OR_INVALID_SWITCH_REGISTRY")
            construct_verified = False
        else:
            primary_entries: List[Tuple[str, Dict[str, Any]]] = []
            secondary_entries: List[Tuple[str, Dict[str, Any]]] = []
            sw_entries_ok = True
            valid_seq_shas = set(construct_seq_sha_map_verified.values())
            for sw_id, sw_meta in sw_reg.items():
                if not _is_clean_str(sw_id) or not isinstance(sw_meta, dict):
                    fail_reasons.append("FAIL_CONSTRUCT_SWITCH_REGISTRY_MALFORMED_ENTRY")
                    sw_entries_ok = False
                    continue
                sw_locus = sw_meta.get("locus")
                sw_sha = sw_meta.get("sequence_sha256")
                sw_role = sw_meta.get("role")
                sw_cid = sw_meta.get("construct_id")
                sw_var = sw_meta.get("variant")
                if not _is_clean_str(sw_locus):
                    fail_reasons.append("FAIL_CONSTRUCT_SWITCH_REGISTRY_INVALID_LOCUS")
                    sw_entries_ok = False
                if not _is_nonzero_sha256_hex(sw_sha) or (valid_seq_shas and sw_sha not in valid_seq_shas):
                    fail_reasons.append("FAIL_CONSTRUCT_SWITCH_REGISTRY_UNREGISTERED_SEQUENCE_SHA256")
                    sw_entries_ok = False
                if sw_cid is not None:
                    if not _is_clean_str(sw_cid) or sw_cid not in construct_seq_sha_map_verified or construct_seq_sha_map_verified[sw_cid] != sw_sha:
                        fail_reasons.append("FAIL_CONSTRUCT_SWITCH_REGISTRY_CONSTRUCT_ID_MISMATCH")
                        sw_entries_ok = False
                if sw_var is not None and (not _is_clean_str(sw_var) or sw_var not in {"WT", "D315A"}):
                    fail_reasons.append("FAIL_CONSTRUCT_SWITCH_REGISTRY_INVALID_VARIANT")
                    sw_entries_ok = False
                if sw_role == "primary":
                    primary_entries.append((sw_id, sw_meta))
                elif sw_role == "secondary":
                    secondary_entries.append((sw_id, sw_meta))
                else:
                    fail_reasons.append("FAIL_CONSTRUCT_SWITCH_REGISTRY_INVALID_ROLE")
                    sw_entries_ok = False

            if not sw_entries_ok:
                construct_verified = False