scripts/verify_iip0043_wholegraft_gate0v_and_calibration.py
Use this file in any AI chat (paste-only or attachment-capable):
Pinned raw file: https://openislet.org/files/gen-eb955f8266035aba/scripts/verify_iip0043_wholegraft_gate0v_and_calibration.py · SHA-256: 20706180ed1ed6a2…
if pe not in holds:
holds.append(pe)
if prior_qual_auth.get("archived_bank_payload_verified") is not True:
if "HOLD_MISSING_OR_INVALID_ARCHIVED_BANK_PAYLOAD" not in holds:
holds.append("HOLD_MISSING_OR_INVALID_ARCHIVED_BANK_PAYLOAD")
if "HOLD_UNVERIFIED_EXTERNAL_APPROVAL_STORE" not in holds:
holds.append("HOLD_UNVERIFIED_EXTERNAL_APPROVAL_STORE")
elif qual_tier == "AUTOLOGOUS_D1B_PROCESS_QUAL":
pq_ok, pq_errs, pq_meta = resolve_and_verify_trusted_process_qualification(
trusted_qualification_store,
expected_sha256=payload.get("process_qualification_ref_sha256"),
expected_product_variant_id=canonical_pvid,
inline_record_if_supplied=payload.get("process_qualification_record"),
openislet_root=resolved_root,
hold_prefix="HOLD_GATE0V_AUTOLOGOUS_PROC_QUAL",
)
prior_qual_auth = {"required": True, **pq_meta}
for pe in pq_errs:
if pe not in holds:
holds.append(pe)
if not _is_valid_sha256_hex(payload.get("adventitious_agent_panel_evidence_sha256")):
holds.append("HOLD_GATE0V_AUTOLOGOUS_D1B_MISSING_ADVENTITIOUS_AGENT_PANEL_SHA256")
# R11-B1 + R13-B3: Strictly validate `require_trusted_nw08_ocr_record` (must be `bool` if present),
# reject `require_trusted_nw08_ocr_record = True` on `BANK_QUALIFICATION` at direct admission
# (`HOLD_NW08_REQUIRED_OCR_NOT_PERMITTED_ON_BANK_QUALIFICATION`), and fail closed with
# `HOLD_NW08_REQUIRED_OCR_RECORD_MISSING` on uncoupled standalone clinical Gate-0V runs whenever
# required OCR is not adjudicated externally by the coupled composite wrapper!
req_trusted_ocr_flag = False
if "require_trusted_nw08_ocr_record" in payload:
rt_ocr_raw = payload.get("require_trusted_nw08_ocr_record")
if not _is_strict_bool(rt_ocr_raw):
holds.append("HOLD_NW08_COUPLED_INVALID_REQUIRE_TRUSTED_OCR_FLAG")
else:
req_trusted_ocr_flag = bool(rt_ocr_raw)
if prior_qual_auth.get("record_require_trusted_nw08_ocr_record") is True:
req_trusted_ocr_flag = True
if req_trusted_ocr_flag and qual_tier == "BANK_QUALIFICATION":
if "HOLD_NW08_REQUIRED_OCR_NOT_PERMITTED_ON_BANK_QUALIFICATION" not in holds:
holds.append("HOLD_NW08_REQUIRED_OCR_NOT_PERMITTED_ON_BANK_QUALIFICATION")
elif req_trusted_ocr_flag and not _nw08_ocr_adjudicated_externally:
if "HOLD_NW08_REQUIRED_OCR_RECORD_MISSING" not in holds:
holds.append("HOLD_NW08_REQUIRED_OCR_RECORD_MISSING")
lot_binding = {
"lot_id": lot_id if isinstance(lot_id, str) else None,
"mcb_id": mcb_id if isinstance(mcb_id, str) else None,
"supplied_product_variant_id": pvid_raw if isinstance(pvid_raw, str) else None,
"canonical_product_variant_id": canonical_pvid,
"canonical_product_tier_family": (
canonical_product_def.get("product_tier_family") if canonical_product_def is not None else None
),
"allowed_qualification_tiers_for_product": (
canonical_product_def.get("allowed_qualification_tiers") if canonical_product_def is not None else None
),
"qualification_tier_allowed_for_product_family": (
bool(tier_allowed_for_product) if (canonical_product_def is not None and qual_tier_valid) else False
),
"canonical_product_registry_source": (
canonical_product_def.get("registry_source") if canonical_product_def is not None else None
),
"canonical_product_execution_supported": (
canonical_product_def.get("execution_supported") if canonical_product_def is not None else None
),
"qualification_tier": qual_tier if isinstance(qual_tier, str) else None,
"supplied_payload_sha256": supplied_sha if isinstance(supplied_sha, str) else None,
"computed_payload_sha256": computed_sha,
"payload_sha256_verified": bool(_is_valid_sha256_hex(supplied_sha) and supplied_sha == computed_sha),
"require_trusted_nw08_ocr_record": req_trusted_ocr_flag,
"prior_qualification_authentication": prior_qual_auth,
}
# ------------------------------------------------------------------
# 0B. Clustering, Independence & Per-Estimand Sampling-Unit Specification (R11-B3 + R12-B1 + R13-B1/M2)
# ------------------------------------------------------------------
clust = payload.get("clustering_and_independence_spec")
deff: Decimal | None = None
raw_deff_val: Decimal | None = None
submitted_deff_unvalidated_safe: str | None = None
indep_units: int | None = None
indep_units_valid = False
sampling_unit_class: str | None = None
sampling_unit_class_valid = False
unit_def: Any = None
unit_def_valid = False
sampling_spec_valid = False
if not isinstance(clust, dict):
holds.append("HOLD_GATE0V_MISSING_CLUSTERING_AND_INDEPENDENCE_SPEC")
else:
raw_units = clust.get("independent_biological_units_n")
deff_raw = clust.get("clustering_design_effect_deff")
if deff_raw is not None:
if isinstance(deff_raw, Decimal) and deff_raw.is_finite():
submitted_deff_unvalidated_safe = format(deff_raw, "f")
elif isinstance(deff_raw, bool):
submitted_deff_unvalidated_safe = str(deff_raw)
elif isinstance(deff_raw, (int, float)) and math.isfinite(deff_raw):
submitted_deff_unvalidated_safe = str(deff_raw)
elif isinstance(deff_raw, str):
submitted_deff_unvalidated_safe = deff_raw
else:
submitted_deff_unvalidated_safe = repr(deff_raw)
deff_val = _to_exact_decimal_nonneg(deff_raw)
raw_deff_val = deff_val
unit_def = clust.get("unit_definition")
raw_unit_class = clust.get("independent_sampling_unit_class")
if _is_strict_int(raw_units) and raw_units >= MIN_INDEPENDENT_BIOLOGICAL_UNITS_N:
indep_units = int(raw_units)
indep_units_valid = True
else:
if _is_strict_int(raw_units):
indep_units = int(raw_units)
holds.append("HOLD_INSUFFICIENT_INDEPENDENT_BIOLOGICAL_UNITS")
if isinstance(raw_unit_class, str) and raw_unit_class in ALLOWED_INDEPENDENT_SAMPLING_UNIT_CLASSES:
sampling_unit_class = raw_unit_class
sampling_unit_class_valid = True
else:
holds.append("HOLD_GATE0V_INVALID_OR_MISSING_INDEPENDENT_SAMPLING_UNIT_CLASS")
if _is_valid_identifier(unit_def):
unit_def_valid = True
else:
holds.append("HOLD_GATE0V_MISSING_INDEPENDENCE_UNIT_DEFINITION")
deff_valid = bool(deff_val is not None and Decimal("1.0") <= deff_val <= Decimal("1000"))
if not deff_valid:
deff = None
if _is_negative_zero(deff_raw) and "INVALID_NEGATIVE_OR_SIGNED_ZERO_BOUNDARY_VALUE" not in holds:
holds.append("INVALID_NEGATIVE_OR_SIGNED_ZERO_BOUNDARY_VALUE")
holds.append("HOLD_GATE0V_INVALID_CLUSTERING_DESIGN_EFFECT_DEFF")
else:
deff = deff_val
if deff > Decimal("1.0") and "HOLD_CLUSTERED_DEFF_GT_1_REQUIRES_CLUSTER_LEVEL_INFERENCE" not in holds:
holds.append("HOLD_CLUSTERED_DEFF_GT_1_REQUIRES_CLUSTER_LEVEL_INFERENCE")
sampling_spec_valid = bool(deff_valid and indep_units_valid and sampling_unit_class_valid and unit_def_valid)
if not sampling_spec_valid:
# R12-B1 + R13-M2: NEVER retain any unvalidated numeric `deff` (neither 1.0 nor 2.0!)
# when `clustering_and_independence_spec` fails validation!
deff = None
sampling_tracker: Dict[str, Any] = {
"max_binomial_denominator_n": 0,
"partitions_checked": 0,
"partitions_reconciled": 0,
"all_partitions_reconciled": True,
}
# R13-B1: Dedicated pre-schema adverse `per_unit_counts` scanner runs UNCONDITIONALLY across
# EVERY arm (required AND supplemental stress regimes, pathogens, CD46/CD55 addenda, extra keys)
# in BOTH single-cell AND multi-well modes (and even when `clustering_and_independence_spec` is
# missing/invalid!) BEFORE any schema, class, or row-ID gating!
_req_kill_regimes_pre: Tuple[str, ...] = (
STRESS_AP1903_REGIMES
if qual_tier == "BANK_QUALIFICATION"
else ("normoxia_baseline", "hypoxia_1pct_plus_cytokines")
)
_puc_prescan = _scan_all_submitted_per_unit_counts_adverse(
payload,
raw_deff_dec=raw_deff_val,
hard_stops=hard_stops,
required_kill_regimes=_req_kill_regimes_pre,
)
if (
sampling_unit_class == "INDEPENDENT_SINGLE_CELL_OR_LIMITING_DILUTION_UNIT"
and _puc_prescan["any_per_unit_counts_present"]
):
sampling_tracker["all_partitions_reconciled"] = False
if "HOLD_UNEXPECTED_PER_UNIT_COUNTS_IN_SINGLE_CELL_MODE" not in holds:
holds.append("HOLD_UNEXPECTED_PER_UNIT_COUNTS_IN_SINGLE_CELL_MODE")
def _check_estimand_sampling_unit_basis(
raw_partition: Any,
*,
k_val: Any,
n_val: Any,
label: str,
zero_tolerance_hard_stop_token: str | None = None,
out_unit_maps: Dict[str, Dict[str, Tuple[int, int]]] | None = None,
) -> bool:
if _is_strict_int(n_val) and n_val >= 1:
if n_val > sampling_tracker["max_binomial_denominator_n"]:
sampling_tracker["max_binomial_denominator_n"] = int(n_val)
if sampling_unit_class == "RECONCILED_MULTI_WELL_CELL_ASSAY_WITH_UNIT_PARTITION":
sampling_tracker["partitions_checked"] += 1
p_info = _inspect_per_unit_counts(
raw_partition,
expected_units_n=indep_units,
expected_k=k_val if _is_strict_int(k_val) else None,
expected_n=n_val if _is_strict_int(n_val) else None,
)
if zero_tolerance_hard_stop_token is not None and p_info["any_pos_k"]:
if "HARD_STOP_ADVERSE_PER_UNIT_VIRAL_PERSISTENCE" not in hard_stops:
hard_stops.append("HARD_STOP_ADVERSE_PER_UNIT_VIRAL_PERSISTENCE")
if zero_tolerance_hard_stop_token not in hard_stops:
hard_stops.append(zero_tolerance_hard_stop_token)
if p_info["reconciled_ok"]:
if out_unit_maps is not None:
out_unit_maps[label] = p_info["unit_map"]
sampling_tracker["partitions_reconciled"] += 1
return True
sampling_tracker["all_partitions_reconciled"] = False
if "HOLD_SAMPLING_UNIT_PARTITION_MISMATCH_OR_INSUFFICIENT_UNITS" not in holds:
holds.append("HOLD_SAMPLING_UNIT_PARTITION_MISMATCH_OR_INSUFFICIENT_UNITS")
tok = f"HOLD_GATE0V_SAMPLING_UNIT_PARTITION_MISMATCH_{label}_{p_info['reason_code']}"
if tok not in holds:
holds.append(tok)
return False
if sampling_unit_class == "INDEPENDENT_SINGLE_CELL_OR_LIMITING_DILUTION_UNIT":
if raw_partition is not None:
sampling_tracker["all_partitions_reconciled"] = False
if "HOLD_UNEXPECTED_PER_UNIT_COUNTS_IN_SINGLE_CELL_MODE" not in holds:
holds.append("HOLD_UNEXPECTED_PER_UNIT_COUNTS_IN_SINGLE_CELL_MODE")
return False
if not (_is_strict_int(k_val) and _is_strict_int(n_val) and n_val >= 1 and 0 <= k_val <= n_val):
sampling_tracker["all_partitions_reconciled"] = False
if "HOLD_SAMPLING_UNIT_PARTITION_MISMATCH_OR_INSUFFICIENT_UNITS" not in holds:
holds.append("HOLD_SAMPLING_UNIT_PARTITION_MISMATCH_OR_INSUFFICIENT_UNITS")
return False
if not (indep_units_valid and indep_units is not None and indep_units >= n_val):
sampling_tracker["all_partitions_reconciled"] = False
if "HOLD_SAMPLING_UNIT_PARTITION_MISMATCH_OR_INSUFFICIENT_UNITS" not in holds:
holds.append("HOLD_SAMPLING_UNIT_PARTITION_MISMATCH_OR_INSUFFICIENT_UNITS")
return False
return True
# Missing/invalid unit class: still scan submitted partition for hidden positive viral events
if raw_partition is not None and zero_tolerance_hard_stop_token is not None:
p_info = _inspect_per_unit_counts(
raw_partition,
expected_units_n=indep_units,
expected_k=k_val if _is_strict_int(k_val) else None,
expected_n=n_val if _is_strict_int(n_val) else None,
)
if p_info["any_pos_k"]:
if "HARD_STOP_ADVERSE_PER_UNIT_VIRAL_PERSISTENCE" not in hard_stops:
hard_stops.append("HARD_STOP_ADVERSE_PER_UNIT_VIRAL_PERSISTENCE")
if zero_tolerance_hard_stop_token not in hard_stops:
hard_stops.append(zero_tolerance_hard_stop_token)
sampling_tracker["all_partitions_reconciled"] = False
return False
# ------------------------------------------------------------------
# 0C. Strict CXADR KO Boolean + Genotyping SHA-256 Digest
# ------------------------------------------------------------------
if "cxadr_ko_present" not in payload or not _is_strict_bool(payload.get("cxadr_ko_present")):
holds.append("HOLD_GATE0V_MISSING_OR_NONBOOL_CXADR_KO_FLAG")
elif payload["cxadr_ko_present"] is True: