}) | (COATING_IDENTITY_KEYS if expected_coating_identity is not None else frozenset()) _check_allowed_keys(ev, base_keys | allowed_extra_keys, fails, expected_stage.replace("-", "_")) rsha = ev.get("receipt_sha256") if not is_valid_sha256_hex(rsha): fails.append(f"FAIL_INVALID_RECEIPT_SHA256:{expected_stage}") else: if compute_receipt_sha256(ev) != rsha: fails.append(f"FAIL_RECEIPT_CONTENT_HASH_TAMPER:{expected_stage}") ts = parse_utc_iso_ts(ev.get("recorded_at_utc")) if ts is None: fails.append(f"FAIL_INVALID_RECORDED_AT_UTC:{expected_stage}") if ev.get("stage") != expected_stage: fails.append(f"{mismatch_code}_STAGE") if expected_scope is not None and ev.get("evidence_scope") != expected_scope: fails.append(f"FAIL_EVIDENCE_SCOPE_MISMATCH:{expected_stage}") if expected_prod is not None and ev.get("product_id") != expected_prod: fails.append(f"{mismatch_code}_PRODUCT") if expected_bank is not None and ev.get("bank_id") != expected_bank: fails.append(f"{mismatch_code}_BANK") if expected_lot is not None and ev.get("lot_id") != expected_lot: fails.append(f"{mismatch_code}_LOT") if forbid_route: if "route_id" in ev and ev["route_id"] is not None: fails.append(f"FAIL_ROUTE_AGNOSTIC_RECEIPT_MUST_NOT_BIND_ROUTE:{expected_stage}") elif expected_route is not None and str(ev.get("route_id", "")).strip().upper() != expected_route: fails.append(f"{mismatch_code}_ROUTE") if expected_candidate_gene is not None and ev.get("candidate_gene") != expected_candidate_gene: fails.append(f"FAIL_INTERVENTION_CANDIDATE_MISMATCH:{expected_stage}") if expected_comparator_nt_lot_id is not None: if not is_nonempty_str(ev.get("comparator_nt_lot_id")) or str(ev["comparator_nt_lot_id"]).strip() != expected_comparator_nt_lot_id: fails.append(f"FAIL_COMPARATOR_NT_LOT_MISMATCH:{expected_stage}") if expected_coating_identity is not None: _extract_and_validate_coating_identity(ev, expected=expected_coating_identity, fails=fails, stage=expected_stage) if expected_parent is not None: p_sha = expected_parent.get("receipt_sha256") if ev.get("parent_receipt_sha256") != p_sha: fails.append(f"FAIL_PARENT_RECEIPT_CHAIN_MISMATCH:{expected_stage}") p_ts = parse_utc_iso_ts(expected_parent.get("recorded_at_utc")) if ts is not None and p_ts is not None and ts < p_ts: fails.append(f"FAIL_CHRONOLOGICAL_TIMESTAMP_ORDER_VIOLATION:{expected_stage}") if min_prereq_receipts: prereq_map = ev.get("prerequisite_receipt_sha256s") if not isinstance(prereq_map, Mapping): fails.append(f"FAIL_MISSING_PREREQUISITE_RECEIPT_DIGEST_MAP:{expected_stage}") else: for pr_name, pr_ev in min_prereq_receipts: if pr_ev is None: continue if prereq_map.get(pr_name) != pr_ev.get("receipt_sha256"): fails.append(f"FAIL_PREREQ_RECEIPT_DIGEST_BINDING_MISMATCH:{pr_name}_IN_{expected_stage}") pr_ts = parse_utc_iso_ts(pr_ev.get("recorded_at_utc")) if ts is not None and pr_ts is not None and ts < pr_ts: fails.append(f"FAIL_CHRONOLOGICAL_PREREQ_POSTDATES_STAGE:{pr_name}_AFTER_{expected_stage}") v = ev.get("verdict") if v == "FAIL": fails.append(fail_code) elif v == "HOLD": holds.append(f"HOLD_{expected_stage}_IN_HOLD") elif v != "PASS": fails.append(f"{mismatch_code}_INVALID_VERDICT") return ev def _validate_readout2_gsis( r2: Any, *, fails: list[str], holds: list[str], prefix: str, expected_comparator_id: Optional[str] = None, min_comparator_output_ratio: Optional[float] = None, parent_recorded_at_utc: Optional[datetime] = None, min_exposure_start_utc: Optional[datetime] = None, expected_prod: Optional[str] = None, expected_bank: Optional[str] = None, expected_lot: Optional[str] = None, expected_route: Optional[str] = None, expected_specimen_id: Optional[str] = None, expected_cohort_id: Optional[str] = None, expected_candidate_gene: Optional[str] = None, expected_engagement_window_utc: Optional[tuple[Optional[datetime], Optional[datetime]]] = None, ) -> bool: """ Full canonical Playbook Readout-2 + separate Gate 0M + canonical Rule S8 + Gate 0S receipt + Gate Resp-1 + DEC-134 washout (docs/STAGE-0-EXECUTION-PLAYBOOK.md:122-126, 339-340; scripts/wetlab_contract.py:12826-12867): - Binds dual_clocks_utc.assay_harvest_utc <= parent_recorded_at_utc (rejects future-dated GSIS!) - Separates ordinary Readout-2 (t50 <= 3.0 min, 0 < t50 < t90) from Gate 0M (t90 <= 3.0 min when gate0m_applicable/Rule S8 active) - Enforces canonical Rule S8 locked analysis plan, explicit window_selection_design, Bonferroni alpha/k or disjoint pilot blocks, & immutable comparator receipt SHA-256 - Enforces content-hashed scope-matched gate0s_escalation_receipt whenever stored insulin > 138.3 IU (rejects bare boolean bypass!) - Recomputes Student-t L95(Z1/Z2/RefAdult/Z2-0.5*RefAdult), U95(tau), & paired Fieller matched-comparator ratio """ if not isinstance(r2, Mapping): fails.append(f"FAIL_{prefix}_READOUT2_MISSING_OR_MALFORMED") return False canonical_mixtures: dict[str, dict[str, Any]] = { "REF_60_30_10": {"beta": 0.60, "alpha": 0.30, "delta": 0.10, "is_reference": True}, "DEC084_M1_70_20_10": {"beta": 0.70, "alpha": 0.20, "delta": 0.10, "is_reference": False}, "DEC084_M2_70_25_5": {"beta": 0.70, "alpha": 0.25, "delta": 0.05, "is_reference": False}, "DEC084_M3_80_15_5": {"beta": 0.80, "alpha": 0.15, "delta": 0.05, "is_reference": False}, "DEC084_M4_80_10_10": {"beta": 0.80, "alpha": 0.10, "delta": 0.10, "is_reference": False}, } allowed_r2 = frozenset({ "canonical_readout2_producer_sha256", "shared_measurement_record_id", "independent_blocks_n", "is_non_reference_mixture", "registered_mixture_id", "beta_cell_fraction", "alpha_cell_fraction", "delta_cell_fraction", "dec084_matched_age_mixture_receipt", "preparation_process_mode", "sorting_performed", "dual_clocks_utc", "alk5i_free_maturation_applied", "stage7_t3_10nM_and_nac_1mM_retained", "post_sort_endocrine_zm447439_regimen", "post_sort_zm447439_uM", "vascular_iec_coassembled", "pre_coassembly_iec_vascular_function_verified", "zm447439_washout_lcms_uM", "zm447439_wash_cycles", "zm447439_spike_recovery_pct", "eta_sort_viable_recovery_pct", "implanted_dose_ieq", "mature_insulin_inventory_iu_per_dose", "insulin_inventory_assay_specificity", "assay_cumulative_secreted_iu_equiv", "stored_insulin_per_lot_iu", "proinsulin_to_cpeptide_content_ratio_per_lot", "within_lot_technical_var_z2", "stored_insulin_u95_iu", "stored_insulin_max_lot_iu", "gate0s_escalation_cleared", "gate0s_escalation_receipt", "z_increment_units", "z_min_floor", "matched_comparator_id", "gate0m_applicable", "rule_s8_extended_clock_applicable", "rule_s8_locked_analysis_plan_id", "rule_s8_candidate_windows_k", "rule_s8_window_selection_design", "rule_s8_pilot_block_ids", "rule_s8_comparator_receipt_sha256", "rule_s8_earlier_window_records", "o2_40_mmhg", "o2_100_mmhg", "gate_resp1_inlet_po2_mmhg", "gate_resp1_flow_ul_min", "gate_resp1_dose_ieq", "gate_resp1_ocr_pmol_min_ieq", "gate_resp1_flow_dose_record_sha256", "gate_resp1_snr", "gate_resp1_po2_drop_mmhg", "gate_resp1_blank_drift_mmhg", "gate_resp1_dna_recovery_pct", "gate_resp1_ocr_ratio", "gate_resp1_ocr_cv_pct", "gate_resp1_replicates_n", "gate_resp1_whole_prep_ocr_record", "gate_resp1_pure_endocrine_ocr_record", "cluster_surface_o2_and_morphometry_record", "dec134_washout_time_min_series", "dec134_washout_remaining_peak_pct_series", "dec134_washout_raw_trace_sha256", "dec134_washout_trace_span_min", "dec134_washout_tail_remaining_pct", "dec134_washout_mass_recovery_pct", "dec134_washout_t50_min", "dec134_washout_mean_transit_min", "dec134_washout_t90_min", }) require_r2_spec_prov = any(x is not None for x in (expected_specimen_id, expected_cohort_id, expected_candidate_gene)) if require_r2_spec_prov: allowed_r2 = allowed_r2 | frozenset({"readout2_specimen_provenance"}) _check_allowed_keys(r2, allowed_r2, fails, f"{prefix}_READOUT2") ok = True if not is_valid_sha256_hex(r2.get("canonical_readout2_producer_sha256")): fails.append(f"FAIL_{prefix}_READOUT2_MISSING_CANONICAL_PRODUCER_SHA256"); ok = False if not is_nonempty_str(r2.get("shared_measurement_record_id")): fails.append(f"FAIL_{prefix}_READOUT2_MISSING_SHARED_MEASUREMENT_RECORD_ID"); ok = False # Live wetlab_contract.py:12754-12816 Registered Mixture + Measured Composition + DEC-084 Linkage! is_mix = r2.get("is_non_reference_mixture") reg_mix_id = r2.get("registered_mixture_id") if not is_strict_bool(is_mix): fails.append(f"FAIL_{prefix}_READOUT2_MISSING_MIXTURE_BOOL"); ok = False mix_spec = canonical_mixtures.get(str(reg_mix_id)) if is_nonempty_str(reg_mix_id) else None if mix_spec is None: fails.append(f"FAIL_{prefix}_READOUT2_UNREGISTERED_OR_MISSING_MIXTURE_ID"); ok = False else: expected_non_ref = not bool(mix_spec["is_reference"]) if is_strict_bool(is_mix) and is_mix != expected_non_ref: fails.append(f"FAIL_{prefix}_READOUT2_MIXTURE_ID_VS_NONREF_BOOL_MISMATCH"); ok = False bf_c, af_c, df_c = r2.get("beta_cell_fraction"), r2.get("alpha_cell_fraction"), r2.get("delta_cell_fraction") if not ( all(is_finite_num(x) for x in (bf_c, af_c, df_c)) and 0.0 < float(bf_c) <= 1.0 and 0.0 <= float(af_c) <= 1.0 and 0.0 <= float(df_c) <= 1.0 and 0.95 <= (float(bf_c) + float(af_c) + float(df_c)) <= 1.0 + 1e-6 ): fails.append(f"FAIL_{prefix}_READOUT2_INVALID_MEASURED_ENDOCRINE_COMPOSITION"); ok = False else: in_band = ( abs(float(bf_c) - float(mix_spec["beta"])) <= 0.05 + 1e-9 and abs(float(af_c) - float(mix_spec["alpha"])) <= 0.05 + 1e-9 and abs(float(df_c) - float(mix_spec["delta"])) <= 0.03 + 1e-9 ) if mix_spec["is_reference"]: ad_rat = float(af_c) / max(1e-9, float(df_c)) if not in_band or not (2.0 <= ad_rat <= 4.5): holds.append(f"HOLD_{prefix}_READOUT2_COMPOSITION_DRIFT_REQUIRES_DEC084"); ok = False elif not in_band: holds.append(f"HOLD_{prefix}_READOUT2_COMPOSITION_OUTSIDE_DEC084_TOLERANCE"); ok = False if expected_non_ref: d84_ev = _validate_content_receipt( r2.get("dec084_matched_age_mixture_receipt"), expected_stage="DEC-084-MIXTURE", expected_prod=expected_prod, expected_bank=expected_bank, expected_lot=expected_lot, expected_route=expected_route, forbid_route=(expected_route is None), expected_scope="exact_product", allowed_extra_keys=frozenset({ "registered_mixture_id", "canonical_dec084_producer_sha256", "canonical_dec084_verdict", }), fails=fails, holds=holds, missing_hold=f"HOLD_{prefix}_READOUT2_DEC084_MATCHED_AGE_MIXTURE_RECEIPT_MISSING", fail_code=f"FAIL_{prefix}_READOUT2_DEC084_MIXTURE_RECEIPT_FAILED", mismatch_code=f"FAIL_{prefix}_READOUT2_DEC084_MIXTURE_SCOPE_MISMATCH", ) if d84_ev is None: ok = False else: t_d84 = parse_utc_iso_ts(d84_ev.get("recorded_at_utc")) if parent_recorded_at_utc is not None and t_d84 is not None and t_d84 > parent_recorded_at_utc: fails.append(f"FAIL_{prefix}_READOUT2_DEC084_RECEIPT_POSTDATES_PARENT"); ok = False if ( d84_ev.get("registered_mixture_id") != reg_mix_id or not is_valid_sha256_hex(d84_ev.get("canonical_dec084_producer_sha256")) or d84_ev.get("canonical_dec084_verdict") != "PASS" ): fails.append(f"FAIL_{prefix}_READOUT2_DEC084_MIXTURE_VERDICT_OR_ID_MISMATCH"); ok = False n_blk = r2.get("independent_blocks_n") min_blk = 8 if (is_mix is True or (mix_spec is not None and not mix_spec["is_reference"])) else 6 if not is_strict_int(n_blk) or int(n_blk) < min_blk: fails.append(f"FAIL_{prefix}_READOUT2_INDEPENDENT_BLOCKS_LT_{min_blk}"); ok = False n_expected = int(n_blk) if is_strict_int(n_blk) and int(n_blk) >= min_blk else min_blk # Live wetlab_contract.py:12512-12513, 13043-13085, & 13232-13234 Process Mode, Dual-Clock Minima & Extended Classifier! proc_mode = r2.get("preparation_process_mode") sorting_perf = r2.get("sorting_performed") if proc_mode not in ("SORTED_REAGGREGATED", "UNSORTED_CONTROL") or not is_strict_bool(sorting_perf): holds.append(f"HOLD_{prefix}_READOUT2_MISSING_OR_INVALID_PROCESS_SORTING_MODE"); ok = False elif (proc_mode == "SORTED_REAGGREGATED" and sorting_perf is not True) or ( proc_mode == "UNSORTED_CONTROL" and sorting_perf is not False ): fails.append(f"FAIL_{prefix}_READOUT2_SORTING_MODE_BOOLEAN_CONTRADICTION"); ok = False clks = r2.get("dual_clocks_utc") extended_clock_detected = False t_hv: Optional[datetime] = None if not isinstance(clks, Mapping) or set(clks.keys()) != {"stage7_start_utc", "sort_reaggregation_utc", "assay_harvest_utc"}: fails.append(f"FAIL_{prefix}_READOUT2_MISSING_DUAL_CLOCKS_UTC"); ok = False else: t_s7 = parse_utc_iso_ts(clks.get("stage7_start_utc")) t_sr = parse_utc_iso_ts(clks.get("sort_reaggregation_utc")) t_hv = parse_utc_iso_ts(clks.get("assay_harvest_utc")) if t_s7 is None or t_sr is None or t_hv is None or not (t_s7 <= t_sr <= t_hv): fails.append(f"FAIL_{prefix}_READOUT2_INVALID_DUAL_CLOCKS_ORDER"); ok = False else: if parent_recorded_at_utc is not None and t_hv > parent_recorded_at_utc: fails.append(f"FAIL_{prefix}_READOUT2_ASSAY_HARVEST_POSTDATES_RECEIPT"); ok = False if min_exposure_start_utc is not None and t_hv < min_exposure_start_utc: fails.append(f"FAIL_{prefix}_READOUT2_ASSAY_HARVEST_PREDATES_EXPOSURE_START"); ok = False s7_total_days = (t_hv - t_s7).total_seconds() / 86400.0