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…

                "promotion_authorized": False,
                "evaluation_context": "INVALID_ARM_MANIFEST",
                "evidence_tier": None,
                "fail_reasons": ["FAIL_CALLER_SUPPLIED_ARM_MANIFEST_HASH_MISMATCH"],
                "hold_reasons": [],
                "stage_limited_reasons": [],
                "reasons": ["FAIL_CALLER_SUPPLIED_ARM_MANIFEST_HASH_MISMATCH"],
                "upstream_lot_evidence_record_v1": rec_v1,
            }

    fail_reasons: List[str] = []
    hold_reasons: List[str] = []
    stage_limited_reasons: List[str] = []
    resolved_upstream_tiers: List[str] = []
    computed_u95_exact: Dict[str, Optional[float]] = {
        "gate5": None,
        "hypoxic": None,
        "normoxic": None,
        "secondary_cdk1_hsv_tk": None,
    }

    def _add_fail(code: str) -> None:
        if code not in fail_reasons:
            fail_reasons.append(code)

    def _add_hold(code: str) -> None:
        if code not in hold_reasons:
            hold_reasons.append(code)

    def _add_stage_limited(code: str) -> None:
        if code not in stage_limited_reasons:
            stage_limited_reasons.append(code)

    def req_bool(key: str, *, expected: Optional[bool] = None, fail_code: str = "", hold_code: str = "") -> Optional[bool]:
        if key not in payload or payload[key] is None:
            hold_reasons.append(hold_code or f"HOLD_MISSING_{key.upper()}")
            return None
        v = payload[key]
        if not _is_strict_bool(v):
            fail_reasons.append(f"FAIL_INVALID_BOOL_TYPE_{key.upper()}")
            return None
        if expected is not None and v is not expected:
            fail_reasons.append(fail_code or f"FAIL_POLICY_VIOLATION_{key.upper()}")
        return v

    def req_int(key: str, *, lo: Optional[int] = None, hi: Optional[int] = None, exact: Optional[int] = None, fail_code: str = "", domain_code: str = "") -> Optional[int]:
        if key not in payload or payload[key] is None:
            hold_reasons.append(f"HOLD_MISSING_{key.upper()}")
            return None
        v = payload[key]
        if not _is_strict_int(v):
            fail_reasons.append(domain_code or f"FAIL_NON_INTEGER_OR_OVERFLOW_{key.upper()}")
            return None
        if exact is not None and v != exact:
            fail_reasons.append(fail_code or f"FAIL_INVALID_{key.upper()}")
        if lo is not None and v < lo:
            fail_reasons.append(domain_code or fail_code or f"FAIL_OUT_OF_DOMAIN_{key.upper()}")
        if hi is not None and v > hi:
            fail_reasons.append(fail_code or f"FAIL_EXCEEDED_{key.upper()}")
        return v

    def req_pct(key: str, *, max_allowed: float, fail_code: str) -> Optional[float]:
        if key not in payload or payload[key] is None:
            hold_reasons.append(f"HOLD_MISSING_{key.upper()}")
            return None
        v = payload[key]
        if not _is_finite_number(v):
            fail_reasons.append(f"FAIL_INVALID_OR_OVERFLOW_NUMBER_{key.upper()}")
            return None
        fv = float(v)
        if fv < 0.0 or fv > 100.0:
            fail_reasons.append(f"FAIL_OUT_OF_DOMAIN_PCT_{key.upper()}")
            return None
        if fv > max_allowed:
            fail_reasons.append(fail_code)
        return fv

    # 0. Submission context, evidence tier, evaluation scope, & canonical arm/lot/bank/manifest binding
    sub_type_raw = payload.get("submission_type")
    if sub_type_raw is None:
        hold_reasons.append("HOLD_MISSING_SUBMISSION_TYPE")
        eval_ctx = "UNSPECIFIED"
    elif not _is_clean_str(sub_type_raw) or sub_type_raw not in VALID_SUBMISSION_TYPES:
        fail_reasons.append("FAIL_INVALID_SUBMISSION_TYPE")
        eval_ctx = "INVALID"
    else:
        eval_ctx = sub_type_raw

    ev_tier_raw = payload.get("evidence_tier")
    evidence_tier_str: Optional[str] = None
    if ev_tier_raw is None:
        hold_reasons.append("HOLD_MISSING_EVIDENCE_TIER")
    elif not _is_clean_str(ev_tier_raw) or ev_tier_raw not in ALLOWED_EVIDENCE_TIERS:
        fail_reasons.append("FAIL_INVALID_EVIDENCE_TIER")
    else:
        evidence_tier_str = ev_tier_raw

    if eval_ctx == "SYNTHETIC_SELF_TEST_FIXTURE" and evidence_tier_str == "AUTHENTICATED_ARCHIVED_WETLAB":
        fail_reasons.append("FAIL_CONTRADICTORY_SYNTHETIC_SUBMISSION_WITH_WETLAB_EVIDENCE_TIER")

    scope_raw = payload.get("evaluation_scope", "FULL_CLINICAL_PROMOTION")
    eval_scope_str = "FULL_CLINICAL_PROMOTION"
    if not _is_clean_str(scope_raw) or scope_raw not in VALID_EVALUATION_SCOPES:
        fail_reasons.append("FAIL_INVALID_EVALUATION_SCOPE")
    else:
        eval_scope_str = scope_raw

    # Safe string extraction BEFORE any dict/set membership lookup (R2-18)
    arm_id_raw = payload.get("arm_id")
    arm_id_str: Optional[str] = None
    arm_cfg: Optional[Dict[str, Any]] = None
    if arm_id_raw is None:
        hold_reasons.append("HOLD_MISSING_ARM_ID")
    elif not _is_clean_str(arm_id_raw) or arm_id_raw not in arm_manifest["arms"]:
        fail_reasons.append("FAIL_UNKNOWN_OR_INVALID_ARM_ID")
    else:
        arm_id_str = arm_id_raw
        arm_cfg = arm_manifest["arms"][arm_id_str]

    product_id_raw = payload.get("product_id")
    product_id_str: Optional[str] = None
    if product_id_raw is None:
        hold_reasons.append("HOLD_MISSING_PRODUCT_ID")
    elif not _is_clean_str(product_id_raw):
        fail_reasons.append("FAIL_INVALID_PRODUCT_ID")
    else:
        product_id_str = product_id_raw
        if arm_cfg is not None and arm_id_str is not None:
            if arm_id_str == "A":
                if product_id_str == arm_cfg["product_id"]:
                    # Governed v0.8.0 ref (AAVS1) cannot silently pass v0.9.0 Gate 34 switch relocation
                    fail_reasons.append("FAIL_DESIGN_A_V080_REF_REQUIRES_SEPARATE_V090_TBP_CANDIDATE_ID")
                elif product_id_str != arm_cfg.get("v090_hygiene_candidate_id"):
                    fail_reasons.append("FAIL_PRODUCT_ID_CANONICAL_MANIFEST_MISMATCH")
            elif product_id_str != arm_cfg["product_id"]:
                fail_reasons.append("FAIL_PRODUCT_ID_CANONICAL_MANIFEST_MISMATCH")

    lot_id_raw = payload.get("lot_id")
    lot_id_str: Optional[str] = None
    if lot_id_raw is None:
        hold_reasons.append("HOLD_MISSING_LOT_ID")
    elif not _is_clean_str(lot_id_raw):
        fail_reasons.append("FAIL_INVALID_LOT_ID")
    else:
        lot_id_str = lot_id_raw

    bank_id_raw = payload.get("bank_id")
    bank_id_str: Optional[str] = None
    if bank_id_raw is None:
        hold_reasons.append("HOLD_MISSING_BANK_ID")
    elif not _is_clean_str(bank_id_raw):
        fail_reasons.append("FAIL_INVALID_BANK_ID")
    else:
        bank_id_str = bank_id_raw

    bank_origin_raw = payload.get("bank_origin")
    bank_origin_str: Optional[str] = None
    if bank_origin_raw is None:
        hold_reasons.append("HOLD_MISSING_BANK_ORIGIN")
    elif not _is_clean_str(bank_origin_raw):
        fail_reasons.append("FAIL_INVALID_BANK_ORIGIN")
    else:
        bank_origin_str = bank_origin_raw
        if arm_id_str is not None and bank_origin_str not in ALLOWED_BANK_ORIGINS_BY_ARM[arm_id_str]:
            fail_reasons.append("FAIL_BANK_ORIGIN_ARM_MANIFEST_MISMATCH")

    c_sha_raw = payload.get("construct_manifest_sha256")
    construct_sha_str: Optional[str] = None
    if c_sha_raw is None:
        hold_reasons.append("HOLD_MISSING_CONSTRUCT_MANIFEST_SHA256")
    elif not _is_sha256_hex(c_sha_raw):
        fail_reasons.append("FAIL_INVALID_CONSTRUCT_MANIFEST_SHA256")
    elif c_sha_raw == ZERO_SHA256:
        fail_reasons.append("FAIL_PLACEHOLDER_ALL_ZERO_CONSTRUCT_MANIFEST_SHA256")
    elif c_sha_raw in (EXPECTED_ARM_MANIFEST_SHA256, EXPECTED_ARM_MANIFEST_CANONICAL_SHA256):
        fail_reasons.append("FAIL_CONSTRUCT_MANIFEST_SHA256_CONFLATED_WITH_ARM_MANIFEST_SHA256")
    else:
        construct_sha_str = c_sha_raw

    m_sha_raw = payload.get("canonical_arm_manifest_sha256")
    if m_sha_raw is None:
        hold_reasons.append("HOLD_MISSING_CANONICAL_ARM_MANIFEST_SHA256")
    elif not _is_nonzero_sha256_hex(m_sha_raw) or m_sha_raw != EXPECTED_ARM_MANIFEST_SHA256:
        fail_reasons.append("FAIL_CANONICAL_ARM_MANIFEST_SHA256_MISMATCH")

    # Unified trusted evidence-archive resolver (v2_round5_unified — all 14 required fields,
    # alias-consistency lookup, adverse producer flag rejection, & coherent decision/scope tuples!)
    def _resolve_archived_record_obj(
        raw_rec: Any,
        *,
        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]:
        if raw_rec is None:
            hold_reasons.append(f"HOLD_MISSING_{prefix}")
            return None, False
        if not isinstance(raw_rec, dict):
            fail_reasons.append(f"FAIL_INVALID_{prefix}_TYPE")
            return None, False

        arch_sha = raw_rec.get("archive_sha256")
        legacy_sha = raw_rec.get("record_sha256")
        if arch_sha is None:
            if legacy_sha is not None:
                if _is_sha256_hex(legacy_sha) and legacy_sha == ZERO_SHA256:
                    fail_reasons.append(f"FAIL_{prefix}_PLACEHOLDER_ALL_ZERO_SHA256")
                else:
                    hold_reasons.append(f"HOLD_MISSING_{prefix}_ARCHIVE_SHA256")
            else:
                hold_reasons.append(f"HOLD_MISSING_{prefix}_ARCHIVE_SHA256")
            return None, False
        if not _is_sha256_hex(arch_sha):
            fail_reasons.append(f"FAIL_{prefix}_INVALID_ARCHIVE_SHA256")
            return None, False
        if arch_sha == ZERO_SHA256:
            fail_reasons.append(f"FAIL_{prefix}_PLACEHOLDER_ALL_ZERO_SHA256")
            return None, False
        if legacy_sha is not None and legacy_sha != arch_sha:
            fail_reasons.append(f"FAIL_{prefix}_ARCHIVE_VS_RECORD_SHA256_MISMATCH")
            return None, False

        if trusted_evidence_store is None:
            hold_reasons.append(f"HOLD_UNRESOLVED_{prefix}_NO_TRUSTED_EVIDENCE_STORE")
            return None, False
        raw_rid = raw_rec.get("record_id")
        if raw_rid is not None and not _is_clean_str(raw_rid):
            fail_reasons.append(f"FAIL_{prefix}_INVALID_RECORD_ID")
            return None, False
        archived, lookup_status = _lookup_in_trusted_store(trusted_evidence_store, arch_sha, record_id=raw_rid)
        if lookup_status == "CONFLICTING_INDEX_ALIASES":
            _add_fail("FAIL_CONFLICTING_TRUSTED_STORE_INDEX_ALIASES")
            _add_fail(f"FAIL_{prefix}_CONFLICTING_TRUSTED_STORE_INDEX_ALIASES")
            return None, False
        if archived is None:
            hold_reasons.append(f"HOLD_UNRESOLVED_{prefix}_NOT_IN_TRUSTED_EVIDENCE_STORE")
            return None, False
        # Even if raw_rec omitted record_id, re-verify alias consistency against archived['record_id']!
        if raw_rid is None and _is_clean_str(archived.get("record_id")):
            archived_alias, alias_status = _lookup_in_trusted_store(
                trusted_evidence_store, arch_sha, record_id=archived["record_id"]
            )
            if alias_status == "CONFLICTING_INDEX_ALIASES" or archived_alias is None:
                _add_fail("FAIL_CONFLICTING_TRUSTED_STORE_INDEX_ALIASES")
                _add_fail(f"FAIL_{prefix}_CONFLICTING_TRUSTED_STORE_INDEX_ALIASES")
                return None, False