scripts/verify_iip0041_clinical_h1_cd64t_siglec.py
Use this file in any AI chat (paste-only or attachment-capable):
Pinned raw file: https://openislet.org/files/gen-eb955f8266035aba/scripts/verify_iip0041_clinical_h1_cd64t_siglec.py · SHA-256: f004cf6d8259b8b4…
# Exact rescue-lot identity is REQUIRED on every clinically declared rescue repeat infusion!
err_miss_id = (
f"CLINICAL_RESCUE_MISSING_EXPLICIT_LOT_ID: recipient {rid!r} declares rescue repeat infusion "
f"(intercurrent_event={rclaim_chk['intercurrent_event']!r}) without an explicit clinical "
"rescue_lot_id / rescue_lot_ids (exact rescue-lot identity is required for CMC reconciliation)."
)
if err_miss_id not in errs:
errs.append(err_miss_id)
if len(cmc_rescue_lots) == 0:
err_om2 = (
f"CLINICAL_RESCUE_OMITTED_FROM_CMC_LEDGER / RESCUE_LOT_CARDINALITY_MISMATCH: "
f"recipient {rid!r} has clinically declared rescue repeat infusion "
f"(intercurrent_event={rclaim_chk['intercurrent_event']!r}) with no matching infused "
"RESCUE_REPEAT_LOT record in PARALLEL_CMC_READINESS_LEDGER."
)
if err_om2 not in errs:
errs.append(err_om2)
elif len(cmc_rescue_lots) != 1:
err_ex2 = (
f"CMC_EXTRA_RESCUE_LOT_OMITTED_FROM_CLINICAL_COHORT / RESCUE_LOT_CARDINALITY_MISMATCH: "
f"recipient {rid!r} declares unindexed clinical rescue while raw "
f"PARALLEL_CMC_READINESS_LEDGER reports {len(cmc_rescue_lots)} infused rescue lots "
f"{cmc_rescue_lots!r}; rescue events MUST declare exact matching clinical rescue_lot_id / rescue_lot_ids."
)
if err_ex2 not in errs:
errs.append(err_ex2)
ancestry_conflict = True
return errs, ancestry_conflict
def _audit_attested_raw_cmc_ledger_for_early_hold(
cohort_payload: dict[str, Any],
*,
cohort_id: str | None,
trusted_clinical_store: Any,
manifest_mcb_norm: dict[str, str] | None,
effective_wcb_manifest: dict[str, str],
observed_wcb_to_mcbs: dict[str, set[str]],
seen_rids: set[str],
clinical_recipient_meta: dict[str, dict[str, Any]],
raw_ancestry_claims_by_rid: dict[str, list[tuple[str | None, tuple[str, ...], tuple[str, ...], bool]]],
raw_rescue_claims_by_rid: dict[str, list[dict[str, Any]]],
duplicate_rids_set: set[str],
duplicate_rescue_errors_by_rid: dict[str, list[str]],
) -> list[str]:
"""
Resolve and audit any attested raw PARALLEL_CMC_READINESS_LEDGER in `trusted_clinical_store`
against EVERY distinct Pass-0 primary-lot claim (and rescue claims / CMC validation) BEFORE
returning on clinical hierarchy errors (such as CONFLICTING_DUPLICATE_RECIPIENT_ANCESTRY), so
contradictory duplicate recipient rows can NEVER suppress CLINICAL_PRIMARY_LOT_CMC_DISPOSITION_CONFLICT
or related raw CMC diagnostics.
"""
if trusted_clinical_store is None:
return []
audit_errs: list[str] = []
payload_cmc_sha, cmc_sha_errs = _resolve_payload_cmc_ledger_sha256(cohort_payload)
for err_s in cmc_sha_errs:
if err_s not in audit_errs:
audit_errs.append(err_s)
refs_raw = cohort_payload.get("clinical_store_refs")
store_refs: dict[str, Any] = dict(refs_raw) if isinstance(refs_raw, dict) else {}
source_att_cmc_sha: str | None = None
s_ref = store_refs.get("source_verification_attestation_sha256")
if s_ref is not None:
srec, serr, _ssrc = _lookup_gate28a_trusted_record(
trusted_clinical_store,
category="source_verification_attestations",
expected_sha256=s_ref,
allowed_issuers=GATE_28A_ALLOWED_ATTESTATION_AUTHORITIES,
)
if serr is None and srec is not None:
s_recomp = compute_gate28a_record_sha256(srec)
s_cmc = srec.get("parallel_cmc_readiness_ledger_sha256")
s_issuer = _strict_literal_id(srec.get("issuer_authority_id"))
if (
_is_valid_sha256_hex(srec.get("record_sha256"))
and s_recomp == srec.get("record_sha256")
and s_recomp == s_ref
and srec.get("record_type") == "SOURCE_VERIFICATION_ATTESTATION"
and (cohort_id is None or srec.get("cohort_id") == cohort_id)
and s_issuer in GATE_28A_ALLOWED_ATTESTATION_AUTHORITIES
and _is_valid_sha256_hex(s_cmc)
):
source_att_cmc_sha = str(s_cmc)
if payload_cmc_sha is not None and payload_cmc_sha != source_att_cmc_sha:
msg_sc = (
f"SOURCE_VERIFICATION_ATTESTATION parallel_cmc_readiness_ledger_sha256={source_att_cmc_sha} "
f"!= payload {payload_cmc_sha}."
)
if msg_sc not in audit_errs:
audit_errs.append(msg_sc)
st_mcb: dict[str, str] | None = None
st_eff_wcb: dict[str, str] = {}
m_ref = store_refs.get("verified_lot_to_bank_manifest_sha256")
if m_ref is not None:
mrec, merr, _msrc = _lookup_gate28a_trusted_record(
trusted_clinical_store,
category="verified_lot_to_bank_manifests",
expected_sha256=m_ref,
allowed_issuers=GATE_28A_ALLOWED_ATTESTATION_AUTHORITIES,
)
if merr is None and mrec is not None:
m_recomp = compute_gate28a_record_sha256(mrec)
m_issuer = _strict_literal_id(mrec.get("issuer_authority_id"))
if (
_is_valid_sha256_hex(mrec.get("record_sha256"))
and m_recomp == mrec.get("record_sha256")
and m_recomp == m_ref
and mrec.get("record_type") == "VERIFIED_LOT_TO_BANK_MANIFEST"
and (cohort_id is None or mrec.get("cohort_id") == cohort_id)
and m_issuer in GATE_28A_ALLOWED_ATTESTATION_AUTHORITIES
):
cand_mcb, cand_wcb, _cand_wcb_pres, cand_man_errs = _merge_and_validate_bank_manifests(
mrec.get("lot_to_mcb_manifest"),
mrec.get("lot_to_wcb_manifest"),
mrec.get("lot_to_bank_manifest"),
context="VERIFIED_LOT_TO_BANK_MANIFEST",
extra_container=mrec,
)
if cand_mcb is not None and not cand_man_errs:
st_mcb = cand_mcb
st_eff_wcb = cand_wcb
target_cmc_sha = payload_cmc_sha or source_att_cmc_sha
if target_cmc_sha is None:
return audit_errs
cmc_bytes, cmc_err, _csrc = _lookup_gate28a_trusted_cmc_ledger_bytes(
trusted_clinical_store,
expected_sha256=target_cmc_sha,
allowed_issuers=GATE_28A_ALLOWED_ATTESTATION_AUTHORITIES,
)
if cmc_err is not None or cmc_bytes is None:
msg_ce = f"Trusted clinical store resolution failed for PARALLEL_CMC_READINESS_LEDGER: {cmc_err}."
if msg_ce not in audit_errs:
audit_errs.append(msg_ce)
return audit_errs
c_errs: list[str] = []
try:
cmc_doc = load_strict_json_bytes(cmc_bytes, "PARALLEL_CMC_READINESS_LEDGER")
except Exception as exc:
cmc_doc = None
c_errs.append(
f"PARALLEL_CMC_READINESS_LEDGER failed strict JSON decode (CMC_LEDGER_ANCESTRY_UNVERIFIABLE): {exc}"
)
if not isinstance(cmc_doc, dict):
c_errs.append(
"PARALLEL_CMC_READINESS_LEDGER payload must be a JSON object (CMC_LEDGER_ANCESTRY_UNVERIFIABLE)."
)
else:
if cmc_doc.get("record_type") != "PARALLEL_CMC_READINESS_LEDGER":
c_errs.append(f"Unexpected PARALLEL_CMC_READINESS_LEDGER record_type={cmc_doc.get('record_type')!r}.")
if cohort_id is not None and cmc_doc.get("cohort_id") != cohort_id:
c_errs.append(f"PARALLEL_CMC_READINESS_LEDGER cohort_id={cmc_doc.get('cohort_id')!r} != {cohort_id!r}.")
c_issuer = _strict_literal_id(cmc_doc.get("issuer_authority_id"))
if c_issuer not in GATE_28A_ALLOWED_ATTESTATION_AUTHORITIES:
c_errs.append(
f"PARALLEL_CMC_READINESS_LEDGER issuer_authority_id={cmc_doc.get('issuer_authority_id')!r} not in allowlist."
)
lot_ledger_raw = cmc_doc.get("lot_ledger")
ref_mcb_map: dict[str, str] = st_mcb if st_mcb is not None else (manifest_mcb_norm or {})
ref_wcb_map: dict[str, str] = st_eff_wcb if st_mcb is not None else effective_wcb_manifest
raw_anc_errs, _raw_anc_conflict = _audit_raw_cmc_ledger_ancestry(
lot_ledger_raw,
ref_mcb_map=ref_mcb_map,
ref_wcb_map=ref_wcb_map,
observed_wcb_to_mcbs=observed_wcb_to_mcbs,
clinical_recipient_meta=clinical_recipient_meta,
raw_ancestry_claims_by_rid=raw_ancestry_claims_by_rid,
)
for aerr in raw_anc_errs:
if aerr not in c_errs:
c_errs.append(aerr)
raw_resc_errs, _raw_resc_conflict = _reconcile_raw_clinical_and_cmc_rescues(
lot_ledger_raw,
seen_rids=seen_rids,
clinical_recipient_meta=clinical_recipient_meta,
raw_ancestry_claims_by_rid=raw_ancestry_claims_by_rid,
raw_rescue_claims_by_rid=raw_rescue_claims_by_rid,
duplicate_rids_set=duplicate_rids_set,
duplicate_rescue_errors_by_rid=duplicate_rescue_errors_by_rid,
ref_mcb_map=ref_mcb_map,
ref_wcb_map=ref_wcb_map,
)
for rerr in raw_resc_errs:
if rerr not in c_errs:
c_errs.append(rerr)
try:
evaluate_cmc_attempted_lot_readiness(lot_ledger_raw)
except CMCAncestryValidationError as exc:
c_errs.append(
f"PARALLEL_CMC_READINESS_LEDGER failed evaluate_cmc_attempted_lot_readiness "
f"({exc.error_code}): {exc}"
)
except Exception as exc:
c_errs.append(f"PARALLEL_CMC_READINESS_LEDGER failed evaluate_cmc_attempted_lot_readiness: {exc}")
for cerr_item in c_errs:
if cerr_item not in audit_errs:
audit_errs.append(cerr_item)
return audit_errs
GATE_28A_KNOWN_MANIFEST_RELATED_KEYS = frozenset({
"lot_to_mcb_manifest",
"lot_to_wcb_manifest",
"lot_to_bank_manifest",
"verified_lot_to_bank_manifest",
"manifest_source_id",
})
GATE_28A_ALLOWED_MANIFEST_ENTRY_KEYS = frozenset({
"mcb_id",
"master_cell_bank_id",
"bank_id",
"wcb_id",
"working_cell_bank_id",
})
def _find_extra_manifest_alias_keys(container: dict[str, Any] | None) -> dict[str, Any]:
"""Detect any unrecognized extra manifest/bank-map alias keys on a cohort payload or store manifest record."""
if not isinstance(container, dict):
return {}
found: dict[str, Any] = {}
for k, v in sorted(container.items(), key=lambda kv: str(kv[0])):
if not isinstance(k, str) or v is None:
continue
if k in GATE_28A_KNOWN_MANIFEST_RELATED_KEYS or k.endswith("_sha256"):
continue
kl = k.lower()
if "manifest" in kl or kl.endswith(("_bank_map", "_mcb_map", "_wcb_map", "_bank_mapping", "_mcb_mapping", "_wcb_mapping")):
found[k] = v
return found
def _normalize_lot_to_mcb_manifest(
manifest_raw: Any,
*,
field_name: str = "lot_to_mcb_manifest",
) -> tuple[dict[str, str] | None, dict[str, str], list[str]]:
"""