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…
d1_u = u_t; act_d1_u = act_u
if abs(hf_v - 336.0) <= 1e-6:
d14_l = l_t; act_d14_l = act_l
if abs(hf_v - 840.0) <= 1e-6:
d35_u = u_t; act_d35_u = act_u
act_hrs = [h for h in hrs1 if 24.0 - 1e-9 <= h <= 336.0 + 1e-9]
if (
not tp1_ok or hrs1[0] > 6.0 + 1e-9 or d1_u is None or d14_l is None or d35_u is None
or len(act_hrs) < 5
or any((act_hrs[i] - act_hrs[i - 1]) > 72.0 + 1e-9 for i in range(1, len(act_hrs)))
):
fails.append(f"FAIL_{tag}_LOCAL_FREE_PARENT_SCHEDULE_GAP_OR_MALFORMED"); ok_el = False
elif zero_disp_a1:
holds.append(f"HOLD_{tag}_LOCAL_PK_ZERO_DISPERSION_UNESTIMABLE"); ok_el = False
else:
env_a1 = ev.get("a1_local_pk_envelope")
exp_sc1 = str(ev.get("evidence_scope", "bench_unedited"))
if env_a1 is None:
holds.append(f"HOLD_{tag}_MISSING_CONTINUOUS_LOCAL_PK_ENVELOPE"); ok_el = False
elif isinstance(env_a1, Mapping) and env_a1.get("dense_pilot_trace") is None:
holds.append(f"HOLD_{tag}_MISSING_DENSE_PILOT_ENVELOPE_EVIDENCE"); ok_el = False
else:
e1_keys = frozenset({
"k_elim_active_max_per_h", "k_rise_active_max_per_h", "k_elim_washout_max_per_h",
"coating_article_id", "evidence_scope", "compartment_id",
"envelope_qualification_method", "pilot_sampling_max_gap_h",
"burst_phase_covered", "washout_phase_covered", "source_study_ids",
"dense_pilot_trace",
"envelope_model_sha256", "envelope_receipt_sha256",
})
if (
not isinstance(env_a1, Mapping) or set(env_a1.keys()) != e1_keys
or not is_valid_sha256_hex(env_a1.get("envelope_receipt_sha256"))
or sha256_bytes(canonical_json_bytes({k: v for k, v in env_a1.items() if k != "envelope_receipt_sha256"})) != str(env_a1["envelope_receipt_sha256"])
or not is_valid_sha256_hex(env_a1.get("envelope_model_sha256"))
or not all(is_finite_num(env_a1.get(k)) and 0.0 < float(env_a1[k]) <= 0.5 for k in ("k_elim_active_max_per_h", "k_rise_active_max_per_h", "k_elim_washout_max_per_h"))
or not (is_strict_bool(env_a1.get("burst_phase_covered")) and env_a1["burst_phase_covered"])
or not (is_strict_bool(env_a1.get("washout_phase_covered")) and env_a1["washout_phase_covered"])
or not (isinstance(env_a1.get("source_study_ids"), list) and len(env_a1["source_study_ids"]) >= 1 and all(is_nonempty_str(x) for x in env_a1["source_study_ids"]))
):
fails.append(f"FAIL_{tag}_INVALID_LOCAL_PK_ENVELOPE"); ok_el = False
elif (
(coat_id is not None and env_a1.get("coating_article_id") != coat_id.get("coating_article_id"))
or env_a1.get("evidence_scope") != exp_sc1
or env_a1.get("compartment_id") != "BENCH_ELUTION_LOCAL_PERI_GRAFT"
or env_a1.get("envelope_qualification_method") not in ("DENSE_PILOT_TIMECOURSE_BOUND", "PHYSICAL_DEPOT_MASS_BALANCE_UPPER_BOUND")
or not (is_finite_num(env_a1.get("pilot_sampling_max_gap_h")) and 0.0 < float(env_a1["pilot_sampling_max_gap_h"]) <= 2.0)
):
fails.append(f"FAIL_{tag}_LOCAL_PK_ENVELOPE_ARTICLE_PHASE_OR_GAP_MISMATCH"); ok_el = False
else:
ke_a1 = float(env_a1["k_elim_active_max_per_h"])
kr_a1 = float(env_a1["k_rise_active_max_per_h"])
ke_w1 = float(env_a1["k_elim_washout_max_per_h"])
dpt1 = env_a1.get("dense_pilot_trace")
dpt1_keys = frozenset({
"pilot_coating_lot_id", "pilot_compartment_id",
"burst_segment_hours_h", "burst_segment_free_parent_uM",
"washout_segment_hours_h", "washout_segment_free_parent_uM",
"pilot_trace_sha256",
})
exp_lot_a1 = str(coat_id.get("coating_lot_id", "")).strip().upper() if coat_id is not None else ""
dpt1_ok = False
if isinstance(dpt1, Mapping) and set(dpt1.keys()) == dpt1_keys:
p1sha = dpt1.get("pilot_trace_sha256")
p1lot = str(dpt1.get("pilot_coating_lot_id") or "").strip().upper()
bh1, bc1 = dpt1.get("burst_segment_hours_h"), dpt1.get("burst_segment_free_parent_uM")
wh1, wc1 = dpt1.get("washout_segment_hours_h"), dpt1.get("washout_segment_free_parent_uM")
if (
is_valid_sha256_hex(p1sha)
and sha256_bytes(canonical_json_bytes({k: v for k, v in dpt1.items() if k != "pilot_trace_sha256"})) == str(p1sha)
and str(env_a1.get("envelope_model_sha256", "")) == str(p1sha)
and is_nonempty_str(dpt1.get("pilot_coating_lot_id"))
and (not exp_lot_a1 or p1lot == exp_lot_a1 or p1lot.startswith(f"{exp_lot_a1}-"))
and dpt1.get("pilot_compartment_id") == "BENCH_ELUTION_LOCAL_PERI_GRAFT"
and isinstance(bh1, list) and isinstance(bc1, list) and len(bh1) >= 4 and len(bh1) == len(bc1)
and isinstance(wh1, list) and isinstance(wc1, list) and len(wh1) >= 4 and len(wh1) == len(wc1)
and all(is_finite_num(x) and float(x) > 0.0 for x in bh1 + bc1 + wh1 + wc1)
):
bh1f, bc1f = [float(x) for x in bh1], [float(x) for x in bc1]
wh1f, wc1f = [float(x) for x in wh1], [float(x) for x in wc1]
pm1 = float(env_a1["pilot_sampling_max_gap_h"])
if (
bh1f[0] <= 2.0 + 1e-9 and wh1f[0] >= 336.0 - 1e-9
and all(bh1f[i] > bh1f[i - 1] and (bh1f[i] - bh1f[i - 1]) <= pm1 + 1e-9 for i in range(1, len(bh1f)))
and all(wh1f[i] > wh1f[i - 1] and (wh1f[i] - wh1f[i - 1]) <= pm1 + 1e-9 for i in range(1, len(wh1f)))
):
okr1 = max((math.log(bc1f[i] / bc1f[i - 1]) / (bh1f[i] - bh1f[i - 1]) for i in range(1, len(bh1f))), default=0.0)
oke1 = max((-math.log(bc1f[i] / bc1f[i - 1]) / (bh1f[i] - bh1f[i - 1]) for i in range(1, len(bh1f))), default=0.0)
okw1 = max((-math.log(wc1f[i] / wc1f[i - 1]) / (wh1f[i] - wh1f[i - 1]) for i in range(1, len(wh1f))), default=0.0)
if kr_a1 + 1e-9 < 1.10 * max(0.0, okr1) or ke_a1 + 1e-9 < 1.10 * max(0.0, oke1) or ke_w1 + 1e-9 < 1.10 * max(0.0, okw1):
fails.append(f"FAIL_{tag}_LOCAL_PK_ENVELOPE_RATE_UNDERSTATES_DENSE_PILOT"); ok_el = False
else:
dpt1_ok = True
else:
fails.append(f"FAIL_{tag}_DENSE_PILOT_TRACE_SPACING_OR_PHASE_INVALID"); ok_el = False
else:
fails.append(f"FAIL_{tag}_INVALID_DENSE_PILOT_TRACE"); ok_el = False
else:
fails.append(f"FAIL_{tag}_INVALID_DENSE_PILOT_TRACE"); ok_el = False
if dpt1_ok:
slope_ok1 = True
for idx_e in range(1, len(hrs1)):
dt_e = hrs1[idx_e] - hrs1[idx_e - 1]
if dt_e <= 0.0:
continue
ke_use = ke_a1 if hrs1[idx_e] <= 336.0 + 1e-9 else ke_w1
if u1_ser[idx_e - 1] > 0.0 and u1_ser[idx_e] > 0.0:
su = math.log(u1_ser[idx_e] / u1_ser[idx_e - 1]) / dt_e
if (-su) > ke_use + 1e-6 or su > kr_a1 + 1e-6:
slope_ok1 = False; break
if hrs1[idx_e] <= 336.0 + 1e-9 and l1_ser[idx_e - 1] > 0.0 and l1_ser[idx_e] > 0.0:
sl = math.log(l1_ser[idx_e] / l1_ser[idx_e - 1]) / dt_e
if (-sl) > ke_a1 + 1e-6 or sl > kr_a1 + 1e-6:
slope_ok1 = False; break
if not slope_ok1:
fails.append(f"FAIL_{tag}_LOCAL_PK_ENVELOPE_RATE_CONTRADICTS_OBSERVED_SLOPE"); ok_el = False
else:
cont_pk1, cont_tr1 = _compute_pfd_continuous_envelope_24_336(
hrs1, u1_ser, l1_ser, ke_a1, kr_a1, ke_w1,
)
cont_pk1_act, cont_tr1_act = _compute_pfd_continuous_envelope_24_336(
hrs1, act_u1_ser, act_l1_ser, ke_a1, kr_a1, ke_w1,
)
u_act_max = max(u_act_max, cont_pk1)
l_act_min = min(l_act_min, cont_tr1)
act_lot_u_max = max(act_lot_u_max, cont_pk1_act)
act_lot_l_min = min(act_lot_l_min, cont_tr1_act)
eff_floor_a1_tp = ic50_u95_op if ic50_u95_op is not None else ic50_u95_val
wash_floor_a1_tp = ic50_u95_wash_op if ic50_u95_wash_op is not None else ic50_u95_val
if (all4_ceiling_val is not None and act_lot_u_max >= all4_ceiling_val) or (eff_floor_a1_tp is not None and act_lot_l_min < eff_floor_a1_tp):
fails.append(f"FAIL_{tag}_ACTIVE_LOT_EXPOSURE_OUT_OF_WINDOW"); ok_el = False
if eff_floor_a1_tp is not None and min(l_act_min, act_lot_l_min) < eff_floor_a1_tp:
fails.append(f"FAIL_{tag}_DAYS1_14_INTERIOR_FREE_PARENT_DIP_BELOW_IC50_U95"); ok_el = False
if all4_ceiling_val is not None and max(u_act_max, act_lot_u_max) >= all4_ceiling_val:
fails.append(f"FAIL_{tag}_DAYS1_14_LOCAL_FREE_PARENT_EXCEEDS_CEILING"); ok_el = False
if wash_floor_a1_tp is not None and max(d35_u, act_d35_u if act_d35_u is not None else d35_u) >= 0.10 * wash_floor_a1_tp:
fails.append(f"FAIL_{tag}_DAY35_LOCAL_FREE_PARENT_U95_NOT_EXHAUSTED"); ok_el = False
if is_finite_num(d1_fr) and float(d1_fr) + 0.5 < max(d1_u, act_d1_u if act_d1_u is not None else d1_u):
fails.append(f"FAIL_{tag}_DAY1_SCALAR_UNDERSTATES_RECOMPUTED_LOCAL_U95"); ok_el = False
if is_finite_num(d14_fr) and float(d14_fr) - 0.5 > min(d14_l, act_d14_l if act_d14_l is not None else d14_l):
fails.append(f"FAIL_{tag}_DAY14_SCALAR_OVERSTATES_RECOMPUTED_LOCAL_L95"); ok_el = False
# Days 0-35 time-resolved local pH verification (7.25-7.45 across full 840-h elution window per Draft A s5)!
a1_ph_tps = ev.get("a1_local_ph_timepoints")
if a1_ph_tps is None:
holds.append(f"HOLD_{tag}_MISSING_35D_TIME_RESOLVED_PH"); ok_el = False
elif not isinstance(a1_ph_tps, list) or len(a1_ph_tps) < 6:
fails.append(f"FAIL_{tag}_LOCAL_PH_OUTSIDE_7P25_7P45_OR_INCOMPLETE_35D"); ok_el = False
else:
_ph1_rk = frozenset({"hour_h", "coating_lot_id", "ph_min", "ph_max"})
_ph1_prev = -1.0
_ph1_ok = True
_exp_clot1 = str(coat_id.get("coating_lot_id", "")).strip() if coat_id is not None else ""
for _pr1 in a1_ph_tps:
if not (
isinstance(_pr1, Mapping) and set(_pr1.keys()) == _ph1_rk
and is_finite_num(_pr1.get("hour_h")) and float(_pr1["hour_h"]) > _ph1_prev and float(_pr1["hour_h"]) > 0.0
and is_nonempty_str(_pr1.get("coating_lot_id"))
and (not _exp_clot1 or str(_pr1["coating_lot_id"]).strip() == _exp_clot1)
and is_finite_num(_pr1.get("ph_min")) and is_finite_num(_pr1.get("ph_max"))
and 7.25 <= float(_pr1["ph_min"]) <= float(_pr1["ph_max"]) <= 7.45
):
_ph1_ok = False; break
if _ph1_prev >= 0.0 and (float(_pr1["hour_h"]) - _ph1_prev) > 168.0 + 1e-9:
_ph1_ok = False; break
_ph1_prev = float(_pr1["hour_h"])
if (
not _ph1_ok
or float(a1_ph_tps[0]["hour_h"]) > 24.0 + 1e-9
or float(a1_ph_tps[-1]["hour_h"]) < 840.0 - 1e-9
):
fails.append(f"FAIL_{tag}_LOCAL_PH_OUTSIDE_7P25_7P45_OR_INCOMPLETE_35D"); ok_el = False
return ok_el
a1_extras = frozenset({
"initial_pirfenidone_loading_mg", "day35_residual_parent_mg", "day35_degradant_mass_mg",
"elution_started_at_utc", "elution_t50_days", "day14_usable_delivery_fraction_pct",
"day35_depot_exhaustion_pct", "day35_residual_parent_pct",
"day35_terminal_mass_recovery_pct", "day14_elution_verified",
"elution_time_hours", "cumulative_intact_parent_released_mg",
"assumed_human_t_half_h", "assumed_human_vd_l",
"day1_intact_parent_free_uM", "day14_intact_parent_free_uM", "day35_intact_parent_free_uM",
"peak_daily_eluted_mg", "assumed_human_clearance_l_per_day",
"a1_local_free_parent_timepoints", "a1_local_pk_envelope",
"a1_local_ph_timepoints",
"manufacturing_lot_release_records",
})
a1_bench_ev = _validate_content_receipt(
rec.get("a1_bench_evidence"),
expected_stage="A-1-BENCH", expected_prod=prod_s, expected_bank=bank_s,
expected_lot=lot_s, expected_route=route_s, expected_scope="bench_unedited",
expected_coating_identity=coat_id,
expected_parent=a0b_ev, allowed_extra_keys=a1_extras, fails=fails, holds=holds,
missing_hold="HOLD_GATE_A1_BENCH_ELUTION_MISSING",
fail_code="FAIL_GATE_A1_BENCH_ELUTION_FAILED",
mismatch_code="FAIL_GATE_A1_BENCH_SCOPE_MISMATCH",
)
a1_bench_ok = a1_bench_ev is not None and a1_bench_ev.get("verdict") == "PASS"
if a1_bench_ev is not None:
t_el_st = parse_utc_iso_ts(a1_bench_ev.get("elution_started_at_utc"))
t_a1b = parse_utc_iso_ts(a1_bench_ev.get("recorded_at_utc"))
t_a0b = parse_utc_iso_ts(a0b_ev.get("recorded_at_utc")) if a0b_ev is not None else None
if t_el_st is None or t_a1b is None:
fails.append("FAIL_GATE_A1_BENCH_MISSING_ELUTION_START_UTC"); a1_bench_ok = False
else:
if t_a0b is not None and t_el_st < t_a0b:
fails.append("FAIL_GATE_A1_BENCH_ELUTION_STARTED_BEFORE_A0B"); a1_bench_ok = False
if (t_a1b - t_el_st) < timedelta(days=35):
fails.append("FAIL_GATE_A1_BENCH_ELAPSED_ELUTION_DURATION_LT_35_DAYS"); a1_bench_ok = False
if not _validate_a1_elution_fields(a1_bench_ev, "GATE_A1_BENCH"):
a1_bench_ok = False
if phase == "BENCH_UNEDITED":
verdict = "FAIL" if fails else ("HOLD" if holds else "PASS_BENCH_ONLY_UNEDITED_PRE_MATERIALIZATION")
return {
"gate": "GATE-40", "phase": phase, "scope_track": scope_track, "verdict": verdict,
"release_authorizing": False, "live_ledger_authenticated": False,
"upstream_archive_verification": OFFLINE_ARCHIVE_NOTE,
"a0a_operative_limits_uM": {**a0a_op_payload, "all4_ceiling_fu_adjusted_uM": all4_ceiling_val},
"product_id": rec.get("product_id"), "required_switch": spec["required_switch"] if spec else None,
"failures": fails, "holds": holds,
}
a1_prereqs: list[tuple[str, Optional[Mapping[str, Any]]]] = [("BANK_MATERIALIZATION", mat_ev)]
if g31_ev is not None:
a1_prereqs.append(("GATE-31", g31_ev))
a1_ev = _validate_content_receipt(
rec.get("a1_evidence"),
expected_stage="A-1", expected_prod=prod_s, expected_bank=bank_s,
expected_lot=lot_s, expected_route=route_s, expected_scope="exact_product",
expected_coating_identity=coat_id,
expected_parent=a1_bench_ev,
min_prereq_receipts=a1_prereqs,
allowed_extra_keys=a1_extras, fails=fails, holds=holds,
missing_hold="HOLD_GATE_A1_ELUTION_MISSING",
fail_code="FAIL_GATE_A1_ELUTION_FAILED",
mismatch_code="FAIL_GATE_A1_SCOPE_MISMATCH",
)
a1_ok = a1_ev is not None and a1_ev.get("verdict") == "PASS"
if a1_ev is not None:
t_el_st = parse_utc_iso_ts(a1_ev.get("elution_started_at_utc"))
t_a1 = parse_utc_iso_ts(a1_ev.get("recorded_at_utc"))
t_a1b = parse_utc_iso_ts(a1_bench_ev.get("recorded_at_utc")) if a1_bench_ev is not None else None
t_mat = parse_utc_iso_ts(mat_ev.get("recorded_at_utc")) if mat_ev is not None else None
if t_el_st is None or t_a1 is None:
fails.append("FAIL_GATE_A1_MISSING_ELUTION_START_UTC"); a1_ok = False
else:
if t_a1b is not None and t_el_st < t_a1b:
fails.append("FAIL_GATE_A1_EXACT_STARTED_BEFORE_BENCH_A1_COMPLETION"); a1_ok = False
if t_mat is not None and t_el_st < t_mat: