scripts/verify_wave7_cellgemma_2p24_rebase.py
Use this file in any AI chat (paste-only or attachment-capable):
Pinned raw file: https://openislet.org/files/gen-eb955f8266035aba/scripts/verify_wave7_cellgemma_2p24_rebase.py · SHA-256: 5f31a6369f25031d…
c2_sp_s = str(c2_ev.get("specimen_id")).strip().upper() if is_nonempty_str(c2_ev.get("specimen_id")) else None
if (
not is_valid_sha256_hex(sp_sha)
or sha256_bytes(canonical_json_bytes({k: v for k, v in surv_panel.items() if k != "panel_receipt_sha256"})) != str(sp_sha)
or not all(is_nonempty_str(surv_panel.get(k)) for k in ("specimen_id", "specimen_lot_id", "comparator_nt_lot_id", "assay_record_id"))
or (c2_sp_s is not None and str(surv_panel["specimen_id"]).strip().upper() != c2_sp_s)
or (lot_s and str(surv_panel["specimen_lot_id"]).strip().upper() != lot_s.upper())
or (is_nonempty_str(nt_lot_id) and str(surv_panel["comparator_nt_lot_id"]).strip().upper() != str(nt_lot_id).strip().upper())
):
fails.append("FAIL_GATE_C2_RESTING_SURVIVAL_PROTEIN_PANEL_MISMATCH")
elif not all(
is_finite_num(surv_panel.get(k)) and 80.0 <= float(surv_panel[k]) <= 200.0
for k in ("bcl2l1_pct_ctrl", "mcl1_pct_ctrl", "cd59_pct_ctrl")
):
fails.append("FAIL_GATE_C2_RESTING_SURVIVAL_PROTEINS_BELOW_80PCT")
prolif_lots = c2_ev.get("c2_per_lot_proliferation_records")
if not isinstance(prolif_lots, list) or len(prolif_lots) < 3:
fails.append("FAIL_GATE_C2_REQUIRES_AT_LEAST_3_PER_LOT_PROLIFERATION_RECORDS")
else:
seen_pl: set[str] = set()
seen_pl_nt: set[str] = set()
seen_pl_samples: set[str] = set()
seen_all_assay_ids: set[str] = set()
pl_allowed = frozenset({
"lot_id", "product_id", "candidate_gene", "comparator_nt_lot_id",
"treated_sample_id", "matched_control_sample_id",
"lot_record_sha256", "resting_assay_record_id", "post_recovery_assay_record_id",
"resting_ki67_fold_vs_ctrl", "resting_edu_fold_vs_ctrl",
"post_recovery_ki67_fold_vs_ctrl", "post_recovery_edu_fold_vs_ctrl",
"ki67_edu_referred",
})
for idx, lrec in enumerate(prolif_lots):
if not isinstance(lrec, Mapping):
fails.append(f"FAIL_GATE_C2_PROLIF_LOT_MALFORMED:{idx}"); continue
_check_allowed_keys(lrec, pl_allowed, fails, f"GATE_C2_PROLIF_{idx}")
if not is_nonempty_str(lrec.get("lot_id")):
fails.append("FAIL_GATE_C2_PROLIF_DUPLICATE_OR_EMPTY_LOT_ID"); continue
lid = str(lrec["lot_id"]).strip()
lid_n = lid.upper()
if lid_n in seen_pl:
fails.append("FAIL_GATE_C2_PROLIF_DUPLICATE_OR_EMPTY_LOT_ID"); continue
seen_pl.add(lid_n)
l_prod = str(lrec.get("product_id", "")).strip()
l_gene = str(lrec.get("candidate_gene", "")).strip().upper()
l_nt = lrec.get("comparator_nt_lot_id")
if l_prod != prod_s or l_gene != target:
fails.append(f"FAIL_GATE_C2_PROLIF_LOT_CANDIDATE_OR_PRODUCT_MISMATCH:{lid}")
if not is_nonempty_str(l_nt):
fails.append(f"FAIL_GATE_C2_PROLIF_LOT_COMPARATOR_MISMATCH:{lid}")
else:
l_nt_s = str(l_nt).strip()
l_nt_n = l_nt_s.upper()
if l_nt_n == lid_n or l_nt_n in seen_pl_nt or (lid_n == lot_s.upper() and l_nt_s != nt_lot_id):
fails.append(f"FAIL_GATE_C2_PROLIF_LOT_COMPARATOR_MISMATCH:{lid}")
seen_pl_nt.add(l_nt_n)
tsid = lrec.get("treated_sample_id")
csid = lrec.get("matched_control_sample_id")
if not (is_nonempty_str(tsid) and is_nonempty_str(csid)):
fails.append(f"FAIL_GATE_C2_PROLIF_SAMPLE_IDS_INVALID:{lid}")
else:
tsn, csn = str(tsid).strip().upper(), str(csid).strip().upper()
if tsn == csn or tsn in seen_pl_samples or csn in seen_pl_samples:
fails.append(f"FAIL_GATE_C2_PROLIF_SAMPLE_IDS_INVALID:{lid}")
seen_pl_samples.add(tsn); seen_pl_samples.add(csn)
lsha = lrec.get("lot_record_sha256")
if not is_valid_sha256_hex(lsha) or sha256_bytes(canonical_json_bytes({k: v for k, v in lrec.items() if k != "lot_record_sha256"})) != lsha:
fails.append(f"FAIL_GATE_C2_PROLIF_LOT_RECORD_HASH_INVALID:{lid}")
rid = lrec.get("resting_assay_record_id")
pid = lrec.get("post_recovery_assay_record_id")
if not (is_nonempty_str(rid) and is_nonempty_str(pid)):
fails.append(f"FAIL_GATE_C2_PROLIF_ASSAY_RECORDS_MISSING_OR_IDENTICAL:{lid}")
else:
rn, pn = str(rid).strip().upper(), str(pid).strip().upper()
if rn == pn or rn in seen_all_assay_ids or pn in seen_all_assay_ids:
fails.append(f"FAIL_GATE_C2_PROLIF_ASSAY_RECORDS_MISSING_OR_IDENTICAL:{lid}")
seen_all_assay_ids.add(rn); seen_all_assay_ids.add(pn)
rk = lrec.get("resting_ki67_fold_vs_ctrl")
re_ = lrec.get("resting_edu_fold_vs_ctrl")
pk = lrec.get("post_recovery_ki67_fold_vs_ctrl")
pe = lrec.get("post_recovery_edu_fold_vs_ctrl")
if not all(is_finite_num(x) and float(x) > 0.0 for x in (rk, re_, pk, pe)):
fails.append(f"FAIL_GATE_C2_MISSING_PER_LOT_PROLIFERATION:{lid}")
elif max(float(rk), float(re_), float(pk), float(pe)) > 1.0:
if not (is_strict_bool(lrec.get("ki67_edu_referred")) and lrec["ki67_edu_referred"]):
fails.append(f"FAIL_GATE_C2_UNREFERRED_PROLIFERATION_ELEVATION:{lid}")
else:
holds.append(f"HOLD_GATE_C2_PROLIFERATION_ELEVATION_REFERRED:{lid}")
if not seen_pl.isdisjoint(seen_pl_nt):
fails.append("FAIL_GATE_C2_PROLIF_LOT_TREATED_COMPARATOR_OVERLAP")
if lot_s and lot_s.upper() not in seen_pl:
fails.append("FAIL_GATE_C2_ACTIVE_LOT_MISSING_FROM_PROLIFERATION_RECORDS")
c3_prereqs: list[tuple[str, Optional[Mapping[str, Any]]]] = [
("BANK_MATERIALIZATION", mat_ev), ("DRUG_FREE_EXPOSURE", df_ev),
("NATIVE_SAFETY_SWITCH", sw_ev), ("GATE-0V-KILL", g0v_ev),
]
if g31_ev is not None:
c3_prereqs.append(("GATE-31", g31_ev))
c3_nk = _validate_content_receipt(
rec.get("c3_nk_evidence"),
expected_stage="C-3-NK", expected_prod=prod_s, expected_bank=bank_s,
expected_lot=lot_s, expected_route=route_s, expected_scope="exact_product",
expected_candidate_gene=target, expected_comparator_nt_lot_id=nt_lot_id,
expected_parent=c2_ev, min_prereq_receipts=c3_prereqs,
allowed_extra_keys=frozenset({"assay_started_at_utc", "assay_completed_at_utc", "specimen_id", "readout3b_evidence", "transient_target_engagement_record"}), fails=fails, holds=holds,
missing_hold="HOLD_GATE_C3_NK_MISSING",
fail_code="FAIL_GATE_C3_NK_FAILED",
mismatch_code="FAIL_GATE_C3_NK_SCOPE_MISMATCH",
)
if c3_nk is not None:
_validate_readout3b_nk(
c3_nk.get("readout3b_evidence"), fails=fails, holds=holds, prefix="GATE_C3_NK",
require_sirpa_low=True,
require_hla_e_spr=bool(spec["hla_e_engineered"]) if spec is not None else True,
expected_specimen_id=str(c3_nk.get("specimen_id")).strip() if is_nonempty_str(c3_nk.get("specimen_id")) else None,
expected_lot_id=lot_s or None,
expected_cohort_id=c1_cohort_id,
)
c3_gsis = _validate_content_receipt(
rec.get("c3_gsis_evidence"),
expected_stage="C-3-GSIS", expected_prod=prod_s, expected_bank=bank_s,
expected_lot=lot_s, expected_route=route_s, expected_scope="exact_product",
expected_candidate_gene=target, expected_comparator_nt_lot_id=nt_lot_id,
expected_parent=c3_nk, min_prereq_receipts=c3_prereqs,
allowed_extra_keys=frozenset({"assay_started_at_utc", "assay_completed_at_utc", "specimen_id", "readout2_evidence", "transient_target_engagement_record"}), fails=fails, holds=holds,
missing_hold="HOLD_GATE_C3_GSIS_MISSING",
fail_code="FAIL_GATE_C3_GSIS_FAILED",
mismatch_code="FAIL_GATE_C3_GSIS_SCOPE_MISMATCH",
)
if c2_ev is not None:
t_c2 = parse_utc_iso_ts(c2_ev.get("recorded_at_utc"))
c2_sha = c2_ev.get("receipt_sha256")
for sw_lbl, sw_rec in (("NATIVE_SAFETY_SWITCH", sw_ev), ("GATE-0V-KILL", g0v_ev)):
if sw_rec is not None:
t_swr = parse_utc_iso_ts(sw_rec.get("recorded_at_utc"))
if t_c2 is not None and t_swr is not None and t_swr < t_c2:
fails.append(f"FAIL_GATE42_CHRONOLOGY_{sw_lbl}_PREDATES_C2")
# Cryptographic C-2 prerequisite digest pin inside BOTH safety switch receipts!
sw_pr = sw_rec.get("prerequisite_receipt_sha256s")
if not isinstance(sw_pr, Mapping) or sw_pr.get("C-2") != c2_sha:
fails.append(f"FAIL_GATE42_MISSING_C2_PREREQ_DIGEST_IN_{sw_lbl}")
if c3_gsis is not None:
_gs_eng_m = c3_gsis.get("transient_target_engagement_record")
_gs_meas_s = parse_utc_iso_ts(_gs_eng_m.get("start_measured_at_utc")) if isinstance(_gs_eng_m, Mapping) else None
_gs_meas_e = parse_utc_iso_ts(_gs_eng_m.get("end_measured_at_utc")) if isinstance(_gs_eng_m, Mapping) else None
_gs_acq_s = parse_utc_iso_ts(c3_gsis.get("assay_started_at_utc"))
_gs_acq_e = parse_utc_iso_ts(c3_gsis.get("assay_completed_at_utc"))
_gs_eff_s = max([t for t in (_gs_meas_s, _gs_acq_s) if t is not None], default=None)
_gs_eff_e = min([t for t in (_gs_meas_e, _gs_acq_e) if t is not None], default=None)
_validate_readout2_gsis(
c3_gsis.get("readout2_evidence"), fails=fails, holds=holds, prefix="GATE_C3_GSIS",
expected_comparator_id=nt_lot_id or sentinel_nt, min_comparator_output_ratio=0.90,
parent_recorded_at_utc=parse_utc_iso_ts(c3_gsis.get("recorded_at_utc")),
min_exposure_start_utc=parse_utc_iso_ts(c2_ev.get("recorded_at_utc")) if c2_ev is not None else None,
expected_prod=prod_s, expected_bank=bank_s, expected_lot=lot_s, expected_route=route_s,
expected_specimen_id=str(c3_gsis.get("specimen_id")).strip() if is_nonempty_str(c3_gsis.get("specimen_id")) else None,
expected_cohort_id=c1_cohort_id,
expected_candidate_gene=target if target in C1_FAMILY_GENES else None,
expected_engagement_window_utc=(_gs_eff_s, _gs_eff_e),
)
# Bind producer-acquired (assay_started_at_utc, assay_completed_at_utc, specimen_id)
# on EVERY downstream assay, enforce strict inter-stage acquisition chronology, AND verify active transient knockdown
# spans the ENTIRE [assay_started_at_utc, assay_completed_at_utc] measurement window on the exact specimen!
t_c1_acq_ts = parse_utc_iso_ts(c1_ev.get("assay_acquired_at_utc")) if c1_ev is not None else None
t_c1_rec_ts = parse_utc_iso_ts(c1_ev.get("recorded_at_utc")) if c1_ev is not None else None
t_r2_harv_ts = None
if c3_gsis is not None and isinstance(c3_gsis.get("readout2_evidence"), Mapping) and isinstance(c3_gsis["readout2_evidence"].get("dual_clocks_utc"), Mapping):
t_r2_harv_ts = parse_utc_iso_ts(c3_gsis["readout2_evidence"]["dual_clocks_utc"].get("assay_harvest_utc"))
stage_acq: dict[str, tuple[Optional[datetime], Optional[datetime], Optional[str]]] = {}
sw_cov_ts: dict[str, list[datetime]] = {}
for _st_tag, _st_rec in (
("C-2", c2_ev), ("NATIVE_SAFETY_SWITCH", sw_ev), ("GATE-0V-KILL", g0v_ev),
("C-3-NK", c3_nk), ("C-3-GSIS", c3_gsis),
):
if _st_rec is None:
stage_acq[_st_tag] = (None, None, None)
continue
_t_st = parse_utc_iso_ts(_st_rec.get("assay_started_at_utc"))
_t_en = parse_utc_iso_ts(_st_rec.get("assay_completed_at_utc"))
_t_rc = parse_utc_iso_ts(_st_rec.get("recorded_at_utc"))
_sp_id = _st_rec.get("specimen_id")
if _t_st is None or _t_en is None or not is_nonempty_str(_sp_id):
holds.append(f"HOLD_GATE_C_MISSING_ASSAY_ACQUISITION_WINDOW_OR_SPECIMEN:{_st_tag}")
stage_acq[_st_tag] = (None, None, None)
elif _t_st > _t_en or (_t_rc is not None and _t_en > _t_rc):
fails.append(f"FAIL_GATE_C_INVALID_ASSAY_ACQUISITION_WINDOW:{_st_tag}")
stage_acq[_st_tag] = (_t_st, _t_en, str(_sp_id).strip())
else:
stage_acq[_st_tag] = (_t_st, _t_en, str(_sp_id).strip())
if _st_tag in ("NATIVE_SAFETY_SWITCH", "GATE-0V-KILL"):
_t_24 = parse_utc_iso_ts(_st_rec.get("assay_24h_collection_utc"))
_t_72 = parse_utc_iso_ts(_st_rec.get("assay_72h_collection_utc"))
if _t_24 is None or _t_72 is None:
holds.append(f"HOLD_GATE_C_MISSING_ASSAY_ACQUISITION_WINDOW_OR_SPECIMEN:{_st_tag}")
else:
h24 = (_t_24 - _t_st).total_seconds() / 3600.0
h72 = (_t_72 - _t_st).total_seconds() / 3600.0
span_h = (_t_en - _t_st).total_seconds() / 3600.0
if not (23.0 <= h24 <= 25.0 and 71.0 <= h72 <= 73.5 and _t_72 <= _t_en and span_h >= 72.0 - 1e-6):
fails.append(f"FAIL_GATE_C_SAFETY_SWITCH_24H_72H_ENDPOINT_TIMING_INVALID:{_st_tag}")
sw_cov_ts[_st_tag] = [_t_st, _t_24, _t_72, _t_en]
# True physical acquisition chronology: C-1 <= C-2 <= {SWITCH, GATE-0V} <= C-3-NK <= C-3-GSIS (with R2 harvest inside C-3-GSIS window)!
t_c2_st, t_c2_en, sp_c2 = stage_acq["C-2"]
t_sw_st, t_sw_en, sp_sw = stage_acq["NATIVE_SAFETY_SWITCH"]
t_gv_st, t_gv_en, sp_gv = stage_acq["GATE-0V-KILL"]
t_nk_st, t_nk_en, sp_nk = stage_acq["C-3-NK"]
t_gs_st, t_gs_en, sp_gs = stage_acq["C-3-GSIS"]
for _p_lbl, _p_ts, _c_lbl, _c_ts in (
("C-1", t_c1_acq_ts, "C-2", t_c2_st),
("C-2", t_c2_en, "NATIVE_SAFETY_SWITCH", t_sw_st),
("C-2", t_c2_en, "GATE-0V-KILL", t_gv_st),
("NATIVE_SAFETY_SWITCH", t_sw_en, "C-3-NK", t_nk_st),
("GATE-0V-KILL", t_gv_en, "C-3-NK", t_nk_st),
("C-3-NK", t_nk_en, "C-3-GSIS", t_gs_st),
):
if _p_ts is not None and _c_ts is not None and _c_ts < _p_ts:
fails.append(f"FAIL_GATE_C_ACQUISITION_CHRONOLOGY_VIOLATION:{_p_lbl}_AFTER_{_c_lbl}")
if t_r2_harv_ts is not None and t_gs_st is not None and t_gs_en is not None:
if not (t_gs_st <= t_r2_harv_ts <= t_gs_en):
fails.append("FAIL_GATE_C_C3_GSIS_READOUT2_HARVEST_OUTSIDE_ASSAY_WINDOW")
t_c2_rec_ts = parse_utc_iso_ts(c2_ev.get("recorded_at_utc")) if c2_ev is not None else None
t_c3nk_rec_ts = parse_utc_iso_ts(c3_nk.get("recorded_at_utc")) if c3_nk is not None else None
t_safety_max_end = max([t for t in (t_sw_en, t_gv_en) if t is not None], default=t_c2_rec_ts)
for _st_tag, _st_rec, _min_ts, _req_st_ts, _req_end_ts, _exp_sp in (
("C-2", c2_ev, t_c1_rec_ts, t_c2_st, t_c2_en, sp_c2),
("NATIVE_SAFETY_SWITCH", sw_ev, t_c2_rec_ts, t_sw_st, t_sw_en, sp_sw),
("GATE-0V-KILL", g0v_ev, t_c2_rec_ts, t_gv_st, t_gv_en, sp_gv),
("C-3-NK", c3_nk, t_safety_max_end, t_nk_st, t_nk_en, sp_nk),
("C-3-GSIS", c3_gsis, t_c3nk_rec_ts, t_gs_st, max([t for t in (t_gs_en, t_r2_harv_ts) if t is not None], default=None), sp_gs),
):
_is_sw_st = _st_tag in ("NATIVE_SAFETY_SWITCH", "GATE-0V-KILL")
_cov_list = sw_cov_ts.get(_st_tag, [])
_eff_req_end = max([t for t in (_req_end_ts, *_cov_list) if t is not None], default=None)
_validate_transient_engagement_record(
_st_rec, target_gene=target, expected_cohort_id=c1_cohort_id,
expected_lot_id=lot_s, expected_specimen_id=_exp_sp,
min_stage_ts=_min_ts, required_start_ts=_req_st_ts, required_end_ts=_eff_req_end,
stage_tag=_st_tag, fails=fails, holds=holds,
require_longitudinal_mode=_is_sw_st,
required_covering_ts=_cov_list,
)
_pilot_downstream_missing_holds = frozenset({
"HOLD_GATE_C2_BASAL_SHIELD_GRANULE_MISSING",
"HOLD_GATE_C3_NK_MISSING",
"HOLD_GATE_C3_GSIS_MISSING",