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…

    parsed_up, up_env_ok = _validate_evidence_record_v2(
        up_raw,
        field_name="upstream_lot_evidence_record",
        expected_gate_source=CANONICAL_UPSTREAM_GATE34_SOURCE,
        allowed_auth_decisions=ALLOWED_GATE34_UPSTREAM_AUTH_DECISIONS,
        parent_id=parent_id,
        arm_entry=arm_entry,
        fail_reasons=fail_reasons,
        missing_fields=missing_fields,
    )
    parsed_cur, cur_env_ok = _validate_evidence_record_v2(
        cur_raw,
        field_name="current_assay_evidence_record",
        expected_gate_source=expected_module_gate_source,
        allowed_auth_decisions=allowed_cur_decisions,
        parent_id=parent_id,
        arm_entry=arm_entry,
        fail_reasons=fail_reasons,
        missing_fields=missing_fields,
    )

    g32_raw: Any = None
    parsed_g32: dict[str, Any] | None = None
    g32_env_ok = True
    if require_live_gate32_debris_recall:
        g32_raw = obs.get("live_gate32_debris_recall_record") if "live_gate32_debris_recall_record" in obs else None
        if g32_raw is None:
            missing_fields.append("live_gate32_debris_recall_record")
            hold_reasons.append("HOLD_MISSING_LIVE_GATE32_WHOLE_DEBRIS_RECALL")
            g32_env_ok = False
        else:
            parsed_g32, g32_env_ok = _validate_evidence_record_v2(
                g32_raw,
                field_name="live_gate32_debris_recall_record",
                expected_gate_source=CANONICAL_LIVE_GATE32_DEBRIS_RECALL_SOURCE,
                allowed_auth_decisions=ALLOWED_LIVE_GATE32_DEBRIS_FULL_CREDIT_DECISIONS,
                parent_id=parent_id,
                arm_entry=arm_entry,
                fail_reasons=fail_reasons,
                missing_fields=missing_fields,
                allow_partial_hold_decisions=ALLOWED_LIVE_GATE32_DEBRIS_PARTIAL_HOLD_DECISIONS,
            )
            if isinstance(g32_raw, dict):
                _evaluate_live_gate32_debris_recall_fields(
                    g32_raw,
                    parsed_g32=parsed_g32,
                    fail_reasons=fail_reasons,
                    missing_fields=missing_fields,
                    hold_reasons=hold_reasons,
                )

    if isinstance(cur_raw, dict) and module_binding_checker is not None:
        module_binding_checker(cur_raw, fail_reasons, missing_fields, hold_reasons)

    records_unconflated = True
    if parsed_up is not None and parsed_cur is not None:
        if parsed_up["record_id"] == parsed_cur["record_id"]:
            fail_reasons.append("UPSTREAM_AND_CURRENT_ASSAY_RECORD_ID_CONFLATED")
            records_unconflated = False
        if parsed_up["archive_sha256"] == parsed_cur["archive_sha256"]:
            fail_reasons.append("UPSTREAM_AND_CURRENT_ASSAY_ARCHIVE_DIGEST_CONFLATED")
            records_unconflated = False
        if parsed_up["evidence_tier"] != parsed_cur["evidence_tier"]:
            fail_reasons.append(
                f"EVIDENCE_TIER_MISMATCH:upstream={parsed_up['evidence_tier']}!=current_assay={parsed_cur['evidence_tier']}"
            )
            records_unconflated = False
    if require_live_gate32_debris_recall and parsed_g32 is not None:
        for other_name, other_rec in (("upstream", parsed_up), ("current_assay", parsed_cur)):
            if other_rec is not None:
                if parsed_g32["record_id"] == other_rec["record_id"]:
                    fail_reasons.append(f"LIVE_GATE32_AND_{other_name.upper()}_RECORD_ID_CONFLATED")
                    records_unconflated = False
                if parsed_g32["archive_sha256"] == other_rec["archive_sha256"]:
                    fail_reasons.append(f"LIVE_GATE32_AND_{other_name.upper()}_ARCHIVE_DIGEST_CONFLATED")
                    records_unconflated = False
                if parsed_g32["evidence_tier"] != other_rec["evidence_tier"]:
                    fail_reasons.append(
                        f"EVIDENCE_TIER_MISMATCH:live_gate32={parsed_g32['evidence_tier']}!={other_name}={other_rec['evidence_tier']}"
                    )
                    records_unconflated = False

    ev_tier = parent_id["evidence_tier"]
    all_tiers = {
        parent_id["evidence_tier"],
        parsed_up["evidence_tier"] if parsed_up is not None else None,
        parsed_cur["evidence_tier"] if parsed_cur is not None else None,
    }
    if require_live_gate32_debris_recall:
        all_tiers.add(parsed_g32["evidence_tier"] if parsed_g32 is not None else None)

    has_required_parsed = (
        parsed_up is not None
        and parsed_cur is not None
        and (not require_live_gate32_debris_recall or parsed_g32 is not None)
    )
    is_pure_synthetic = (
        ev_tier == "SYNTHETIC_FIXTURE"
        and has_required_parsed
        and all_tiers == {"SYNTHETIC_FIXTURE"}
    )
    is_pure_wetlab = (
        ev_tier == "AUTHENTICATED_ARCHIVED_WETLAB"
        and has_required_parsed
        and all_tiers == {"AUTHENTICATED_ARCHIVED_WETLAB"}
    )

    # Always resolve trusted_evidence_store BEFORE building official statistics so unauthenticated,
    # missing-store, or tampered-store records NEVER leak verified numbers on FAIL or HOLD (Audit R10-03)!
    up_status: str | None = None
    up_errs: list[str] = []
    up_store_integrity_ok = False
    up_store_integrity_errs: list[str] = []

    cur_status: str | None = None
    cur_errs: list[str] = []
    cur_store_integrity_ok = False
    cur_store_integrity_errs: list[str] = []

    g32_status: str | None = None
    g32_errs: list[str] = []
    g32_store_integrity_ok = not require_live_gate32_debris_recall
    g32_store_integrity_errs: list[str] = []

    if is_pure_wetlab and isinstance(up_raw, dict) and isinstance(cur_raw, dict):
        up_status, up_errs, up_store_integrity_ok, up_store_integrity_errs = _lookup_and_verify_store_record(
            trusted_evidence_store,
            field_name="upstream_lot_evidence_record",
            rec_raw=up_raw,
            parsed=parsed_up,
            expected_gate_source=CANONICAL_UPSTREAM_GATE34_SOURCE,
            allowed_auth_decisions=ALLOWED_GATE34_UPSTREAM_AUTH_DECISIONS,
        )
        cur_status, cur_errs, cur_store_integrity_ok, cur_store_integrity_errs = _lookup_and_verify_store_record(
            trusted_evidence_store,
            field_name="current_assay_evidence_record",
            rec_raw=cur_raw,
            parsed=parsed_cur,
            expected_gate_source=expected_module_gate_source,
            allowed_auth_decisions=allowed_cur_decisions,
        )
        if require_live_gate32_debris_recall and isinstance(g32_raw, dict) and parsed_g32 is not None:
            g32_status, g32_errs, g32_store_integrity_ok, g32_store_integrity_errs = _lookup_and_verify_store_record(
                trusted_evidence_store,
                field_name="live_gate32_debris_recall_record",
                rec_raw=g32_raw,
                parsed=parsed_g32,
                expected_gate_source=CANONICAL_LIVE_GATE32_DEBRIS_RECALL_SOURCE,
                allowed_auth_decisions=ALLOWED_LIVE_GATE32_DEBRIS_FULL_CREDIT_DECISIONS,
                allow_partial_hold_decisions=ALLOWED_LIVE_GATE32_DEBRIS_PARTIAL_HOLD_DECISIONS,
            )

    fail_store_errs: list[str] = []
    hold_store_errs: list[str] = []
    for st, errs in ((up_status, up_errs), (cur_status, cur_errs), (g32_status, g32_errs)):
        if st in ("FAIL_CORRUPT", "FAIL_MISMATCH"):
            fail_store_errs.extend(errs)
        elif st == "HOLD_UNRESOLVED":
            hold_store_errs.extend(errs)
    store_tamper_errs = up_store_integrity_errs + cur_store_integrity_errs + g32_store_integrity_errs

    manifest_ok = (
        verified_manifest is not None
        and arm_entry is not None
        and obs.get("manifest_sha256") == EXPECTED_ARM_MANIFEST_SHA256
        and (
            "canonical_arm_manifest_sha256" not in obs
            or obs["canonical_arm_manifest_sha256"] in (None, EXPECTED_ARM_MANIFEST_SHA256)
        )
    )
    envelope_chain_ok = (
        manifest_ok
        and parent_env_ok
        and has_required_parsed
        and up_env_ok
        and cur_env_ok
        and g32_env_ok
        and records_unconflated
    )
    if fail_reasons:
        final_verdict = "FAIL"
    elif store_tamper_errs and (missing_fields or hold_reasons):
        final_verdict = "FAIL"
    elif missing_fields or hold_reasons:
        final_verdict = "HOLD"
    elif "SYNTHETIC_FIXTURE" in all_tiers:
        final_verdict = "SYNTHETIC_SELF_TEST_PASS"
    elif fail_store_errs:
        final_verdict = "FAIL"
    elif hold_store_errs:
        final_verdict = "HOLD"
    else:
        final_verdict = "PASS"

    auth_ctx: dict[str, Any] = {
        "evidence_tier": ev_tier,
        "is_synthetic": is_pure_synthetic,
        "final_verdict": final_verdict,
        "archive_authenticated": bool(
            envelope_chain_ok
            and is_pure_wetlab
            and up_store_integrity_ok
            and cur_store_integrity_ok
            and g32_store_integrity_ok
        ),
        "synthetic_archive_sealed": bool(envelope_chain_ok and is_pure_synthetic),
        "up_store_status": up_status,
        "cur_store_status": cur_status,
        "g32_store_status": g32_status,
        "up_store_integrity_ok": up_store_integrity_ok,
        "cur_store_integrity_ok": cur_store_integrity_ok,
        "g32_store_integrity_ok": g32_store_integrity_ok,
    }
    extra = extra_builder(auth_ctx) if extra_builder is not None else (extra_out or {})

    def _finish(result: dict[str, Any]) -> dict[str, Any]:
        check_strictly_finite(result, f"gate36_{expected_module_gate_source}_result")
        # Defense-in-depth invariant (Audit R12-07): FAIL/FAIL_* NEVER pairs with *_HOLD_* derivation source,
        # HOLD/HOLD_* NEVER pairs with *_WITH_INDEPENDENT_FAIL, and every emitted source MUST be declared.
        verdict_val = str(result.get("verdict", ""))
        decision_val = str(result.get("decision", ""))
        for src_key, allowed_sources in (
            ("shield_statistics_derivation_source", SHIELD_STATISTICS_DERIVATION_SOURCES),
            ("ins_statistics_derivation_source", INS_STATISTICS_DERIVATION_SOURCES),
        ):
            if src_key in result:
                src_val = result[src_key]
                require_cond(
                    isinstance(src_val, str) and src_val in allowed_sources,
                    f"Internal invariant violation: undeclared {src_key}={src_val!r}",
                )
                if verdict_val.startswith("FAIL") or decision_val.startswith("FAIL"):
                    require_cond(
                        "_HOLD_" not in src_val and not src_val.startswith("UNVERIFIED_HOLD"),
                        f"Internal invariant violation: FAIL verdict/decision ({verdict_val}/{decision_val}) "
                        f"paired with HOLD source label {src_key}={src_val!r}",
                    )
                if verdict_val.startswith("HOLD") or decision_val.startswith("HOLD"):
                    require_cond(
                        not src_val.endswith("_WITH_INDEPENDENT_FAIL"),
                        f"Internal invariant violation: HOLD verdict/decision ({verdict_val}/{decision_val}) "
                        f"paired with FAIL source label {src_key}={src_val!r}",
                    )
                if verdict_val in ("PASS", "SYNTHETIC_SELF_TEST_PASS"):
                    require_cond(
                        "_HOLD_" not in src_val and not src_val.startswith("WITHHELD_"),
                        f"Internal invariant violation: {verdict_val} paired with non-verified "
                        f"source label {src_key}={src_val!r}",
                    )