scripts/verify_iip0057_foundry_v2_shield_invvax_insl82i_cuffv2.py
Use this file in any AI chat (paste-only or attachment-capable):
Pinned raw file: https://openislet.org/files/gen-eb955f8266035aba/scripts/verify_iip0057_foundry_v2_shield_invvax_insl82i_cuffv2.py · SHA-256: 1c63dcf649c42be4…
allowed_auth_decisions: frozenset[str],
fail_reasons: list[str],
allow_partial_hold_decisions: frozenset[str] = frozenset(),
) -> bool:
"""Enforce coherent (decision, authority_scope, bank_qualified, promotion_authorized) tuples (Audit R4-01 / Round-14 live GATE-32 partial-hold support). Returns True if incoherent or unauthorized."""
rec_tier = parsed.get("evidence_tier")
dec = parsed.get("decision")
scope = parsed.get("authority_scope")
bq = parsed.get("bank_qualified")
pa = parsed.get("promotion_authorized")
incoherent = False
if pa is True and bq is not True:
incoherent = True
if pa is True and scope is not None and scope != "CLINICAL_LOT_RELEASE":
incoherent = True
if pa is True and dec is not None and dec not in allowed_auth_decisions:
incoherent = True
if scope == "CLINICAL_LOT_RELEASE":
if rec_tier is not None and rec_tier != "AUTHENTICATED_ARCHIVED_WETLAB":
incoherent = True
if bq is False or pa is False or (dec is not None and dec not in allowed_auth_decisions):
incoherent = True
elif scope in (
"BANK_QUALIFICATION_ONLY",
"STAGE_LIMITED_SORT_ONLY",
"COMPONENT_SORT_QUALIFICATION_ONLY",
"WAVE7_RESERVE_ONLY",
):
if pa is True:
incoherent = True
if scope == "BANK_QUALIFICATION_ONLY" and bq is False:
incoherent = True
elif scope == "REJECTED_OR_HELD":
if bq is True or pa is True:
incoherent = True
elif scope == "SYNTHETIC_SELF_TEST":
if rec_tier is not None and rec_tier != "SYNTHETIC_FIXTURE":
incoherent = True
if bq is True or pa is True or (dec is not None and dec != "SYNTHETIC_SELF_TEST_PASS"):
incoherent = True
if dec is not None and (
dec.startswith("FAIL") or dec.startswith("HOLD") or dec.startswith("REJECT") or dec.startswith("STAGE_LIMITED")
):
if pa is True or scope == "CLINICAL_LOT_RELEASE":
incoherent = True
if incoherent:
fail_reasons.append(
f"{tag}_INCOHERENT_AUTHORITY_TUPLE:tier={rec_tier},decision={dec},scope={scope},bank_qualified={bq},promotion_authorized={pa}"
)
is_coherent_partial_hold = (
rec_tier == "AUTHENTICATED_ARCHIVED_WETLAB"
and dec is not None
and dec in allow_partial_hold_decisions
and scope == "REJECTED_OR_HELD"
and bq is False
and pa is False
)
synthetic_invalid = False
if rec_tier == "AUTHENTICATED_ARCHIVED_WETLAB":
if not is_coherent_partial_hold:
if dec is not None and dec not in allowed_auth_decisions:
fail_reasons.append(f"{tag}_WETLAB_DECISION_NOT_AUTHORIZED:{dec}")
if scope is not None and scope != "CLINICAL_LOT_RELEASE":
fail_reasons.append(f"{tag}_AUTHORITY_SCOPE_NOT_CLINICAL_LOT_RELEASE:{scope}")
if bq is not None and bq is not True:
fail_reasons.append(f"{tag}_BANK_NOT_QUALIFIED")
if pa is not None and pa is not True:
fail_reasons.append(f"{tag}_PROMOTION_NOT_AUTHORIZED")
elif rec_tier == "SYNTHETIC_FIXTURE":
if dec is not None and dec != "SYNTHETIC_SELF_TEST_PASS":
fail_reasons.append(f"{tag}_SYNTHETIC_DECISION_INVALID:{dec}")
synthetic_invalid = True
if scope is not None and scope != "SYNTHETIC_SELF_TEST":
fail_reasons.append(f"{tag}_SYNTHETIC_AUTHORITY_SCOPE_INVALID:{scope}")
synthetic_invalid = True
if bq is not None and bq is not False:
fail_reasons.append(f"{tag}_SYNTHETIC_CANNOT_QUALIFY_BANK")
synthetic_invalid = True
if pa is not None and pa is not False:
fail_reasons.append(f"{tag}_SYNTHETIC_CANNOT_AUTHORIZE_PROMOTION")
synthetic_invalid = True
return incoherent or synthetic_invalid
def _parse_parent_identity_and_tier(
obs: dict[str, Any],
arm_entry: dict[str, Any] | None,
fail_reasons: list[str],
missing_fields: list[str],
) -> tuple[dict[str, str | None], bool]:
"""Require and verify parent observation identity, manifest digests, producer flags, and evidence_tier."""
out: dict[str, str | None] = {
"evidence_tier": None,
"product_id": None,
"lot_id": None,
"bank_id": None,
"bank_origin": None,
"construct_manifest_sha256": None,
}
parent_env_ok = True
for fld in ("evidence_tier", "product_id", "lot_id", "bank_id", "bank_origin", "construct_manifest_sha256"):
if fld not in obs or obs[fld] is None:
missing_fields.append(fld)
parent_env_ok = False
else:
out[fld] = _require_nonempty_str(obs[fld], fld)
ev_tier = out["evidence_tier"]
if ev_tier is not None and ev_tier not in ALLOWED_EVIDENCE_TIERS:
fail_reasons.append(f"INVALID_PARENT_EVIDENCE_TIER:{ev_tier}")
parent_env_ok = False
if _check_contradictory_producer_flags(
obs, tag="PARENT_OBS", field_name="obs", evidence_tier=ev_tier, fail_reasons=fail_reasons
):
parent_env_ok = False
borig = out["bank_origin"]
if borig is not None and borig not in ALLOWED_BANK_ORIGINS:
fail_reasons.append(f"INVALID_PARENT_BANK_ORIGIN:{borig}")
parent_env_ok = False
csha = out["construct_manifest_sha256"]
if csha is not None:
if not _is_valid_nonzero_sha256(csha):
fail_reasons.append("INVALID_OR_ZERO_PARENT_CONSTRUCT_MANIFEST_SHA256")
parent_env_ok = False
elif csha == EXPECTED_ARM_MANIFEST_SHA256:
fail_reasons.append("PARENT_CONSTRUCT_MANIFEST_DIGEST_CONFLATED_WITH_ARM_MANIFEST")
parent_env_ok = False
if arm_entry is not None:
if out["product_id"] is not None and out["product_id"] != arm_entry["product_id"]:
fail_reasons.append(f"PARENT_PRODUCT_ID_MISMATCH:{out['product_id']}!={arm_entry['product_id']}")
parent_env_ok = False
if borig is not None and borig != arm_entry["cell_origin"]:
fail_reasons.append(f"PARENT_BANK_ORIGIN_MISMATCH:{borig}!={arm_entry['cell_origin']}")
parent_env_ok = False
return out, parent_env_ok
def _validate_evidence_record_v2(
rec_raw: Any,
*,
field_name: str,
expected_gate_source: str,
allowed_auth_decisions: frozenset[str],
parent_id: dict[str, str | None],
arm_entry: dict[str, Any] | None,
fail_reasons: list[str],
missing_fields: list[str],
allow_partial_hold_decisions: frozenset[str] = frozenset(),
) -> tuple[dict[str, Any] | None, bool]:
"""Validate all 14 required fields, complete canonical body digest, producer flags, and authority tuple coherence of a v2_round5_unified record."""
if rec_raw is None:
missing_fields.append(field_name)
return None, False
if not isinstance(rec_raw, dict):
raise ValueError(f"{field_name} must be dict")
tag = field_name.upper()
parsed: dict[str, Any] = {}
all_present = True
integrity_ok = True
for fld in UPSTREAM_EVIDENCE_REQUIRED_FIELDS:
if fld not in rec_raw or rec_raw[fld] is None:
missing_fields.append(f"{field_name}.{fld}")
all_present = False
integrity_ok = False
elif fld in UPSTREAM_EVIDENCE_BOOL_FIELDS:
parsed[fld] = _require_exact_bool(rec_raw[fld], f"{field_name}.{fld}")
else:
parsed[fld] = _require_nonempty_str(rec_raw[fld], f"{field_name}.{fld}")
if "gate_source" in parsed and parsed["gate_source"] != expected_gate_source:
if (
field_name == "upstream_lot_evidence_record"
and parsed["gate_source"] == CANONICAL_LIVE_GATE32_DEBRIS_RECALL_SOURCE
):
fail_reasons.append(
f"{tag}_LIVE_GATE32_ROLE_COLLISION_REJECTED:GATE-32_IS_LIVE_DEC130_DEBRIS_RECALL_NOT_W6_C1_GATE34"
)
fail_reasons.append(f"{tag}_GATE_SOURCE_MISMATCH:{parsed['gate_source']}!={expected_gate_source}")
integrity_ok = False
rec_tier = parsed.get("evidence_tier")
if rec_tier is not None:
if rec_tier not in ALLOWED_EVIDENCE_TIERS:
fail_reasons.append(f"{tag}_INVALID_EVIDENCE_TIER:{rec_tier}")
integrity_ok = False
elif parent_id["evidence_tier"] in ALLOWED_EVIDENCE_TIERS and rec_tier != parent_id["evidence_tier"]:
fail_reasons.append(f"EVIDENCE_TIER_MISMATCH:{field_name}={rec_tier}!=parent={parent_id['evidence_tier']}")
integrity_ok = False
if _check_contradictory_producer_flags(
rec_raw, tag=tag, field_name=field_name, evidence_tier=rec_tier, fail_reasons=fail_reasons
):
integrity_ok = False
if "bank_origin" in parsed and parsed["bank_origin"] not in ALLOWED_BANK_ORIGINS:
fail_reasons.append(f"{tag}_INVALID_BANK_ORIGIN:{parsed['bank_origin']}")
integrity_ok = False
if "authority_scope" in parsed and parsed["authority_scope"] not in ALLOWED_AUTHORITY_SCOPES:
fail_reasons.append(f"{tag}_INVALID_AUTHORITY_SCOPE:{parsed['authority_scope']}")
integrity_ok = False
if "canonical_arm_manifest_sha256" in parsed and parsed["canonical_arm_manifest_sha256"] != EXPECTED_ARM_MANIFEST_SHA256:
fail_reasons.append(f"{tag}_CANONICAL_ARM_MANIFEST_SHA256_MISMATCH")
integrity_ok = False
if "construct_manifest_sha256" in parsed:
rcsha = parsed["construct_manifest_sha256"]
if not _is_valid_nonzero_sha256(rcsha):
fail_reasons.append(f"{tag}_INVALID_OR_ZERO_CONSTRUCT_MANIFEST_SHA256")
integrity_ok = False
elif rcsha == EXPECTED_ARM_MANIFEST_SHA256:
fail_reasons.append(f"{tag}_CONSTRUCT_MANIFEST_DIGEST_CONFLATED_WITH_ARM_MANIFEST")
integrity_ok = False
elif parent_id["construct_manifest_sha256"] is not None and rcsha != parent_id["construct_manifest_sha256"]:
fail_reasons.append(f"{tag}_CONSTRUCT_MANIFEST_MISMATCH_WITH_PARENT")
integrity_ok = False
if "archive_sha256" in parsed:
if not _is_valid_nonzero_sha256(parsed["archive_sha256"]):
fail_reasons.append(f"{tag}_INVALID_OR_ZERO_ARCHIVE_SHA256")
integrity_ok = False
else:
recomp_sha = compute_canonical_record_sha256(rec_raw)
if recomp_sha != parsed["archive_sha256"]:
fail_reasons.append(f"{tag}_ARCHIVE_BODY_DIGEST_MISMATCH")
integrity_ok = False
if arm_entry is not None:
if "product_id" in parsed and parsed["product_id"] != arm_entry["product_id"]:
fail_reasons.append(f"{tag}_CROSS_PRODUCT_MISMATCH:{parsed['product_id']}!={arm_entry['product_id']}")
integrity_ok = False
if "bank_origin" in parsed and parsed["bank_origin"] != arm_entry["cell_origin"]:
fail_reasons.append(f"{tag}_BANK_ORIGIN_MISMATCH:{parsed['bank_origin']}!={arm_entry['cell_origin']}")
integrity_ok = False
if parent_id["product_id"] is not None and "product_id" in parsed and parsed["product_id"] != parent_id["product_id"]:
fail_reasons.append(f"{tag}_PARENT_PRODUCT_MISMATCH:{parsed['product_id']}!={parent_id['product_id']}")
integrity_ok = False
if parent_id["lot_id"] is not None and "lot_id" in parsed and parsed["lot_id"] != parent_id["lot_id"]: