) payload: dict[str, Any] = { "planned_analysis_looks": int(planned_analysis_looks), "predeclared_per_donor_unit_counts": per_donor_map, "preregistration_timestamp_utc": str(preregistration_timestamp_utc), "roster_donor_ids": ids_list, "roster_id": str(roster_id), "single_look_fixed_horizon_plan_locked": bool(single_look_fixed_horizon_plan_locked), } if predeclared_per_donor_unit_count_n is not None: payload["predeclared_per_donor_unit_count_n"] = int(predeclared_per_donor_unit_count_n) raw = json.dumps(payload, sort_keys=True, ensure_ascii=False, separators=(",", ":")).encode("utf-8") return hashlib.sha256(raw).hexdigest() def _shield_build_preregistered_roster( roster_donor_ids: list[str] | tuple[str, ...], *, roster_id: str = "ROSTER_SHIELD_T1D_PREREG_V1", preregistration_timestamp_utc: str = "2026-11-28T00:00:00Z", predeclared_per_donor_unit_count_n: int | None = _SHIELD_MIN_CYTO_N_EFF_PER_DONOR, predeclared_per_donor_unit_counts: dict[str, int] | None = None, planned_analysis_looks: int = 1, single_look_fixed_horizon_plan_locked: bool = True, ) -> dict[str, Any]: """Construct a cryptographically hash-pinned outcome-blind pre-registered T1D donor family roster committing donor membership, fixed per-donor cytotoxicity sample size N, and single-look fixed horizon. """ ids_list = [str(x) for x in roster_donor_ids] out: dict[str, Any] = { "roster_id": str(roster_id), "preregistration_timestamp_utc": str(preregistration_timestamp_utc), "outcome_blind_lock_attestation": True, "single_look_fixed_horizon_plan_locked": bool(single_look_fixed_horizon_plan_locked), "planned_analysis_looks": int(planned_analysis_looks), "roster_donor_ids": ids_list, "planned_t1d_donor_family_n": len(ids_list), } if predeclared_per_donor_unit_counts is not None: counts_map = {str(did): int(predeclared_per_donor_unit_counts[did]) for did in ids_list} out["predeclared_per_donor_unit_counts"] = counts_map if predeclared_per_donor_unit_count_n is not None: out["predeclared_per_donor_unit_count_n"] = int(predeclared_per_donor_unit_count_n) out["roster_sha256"] = _shield_compute_donor_roster_sha256( roster_id, preregistration_timestamp_utc, ids_list, predeclared_per_donor_unit_count_n=predeclared_per_donor_unit_count_n, predeclared_per_donor_unit_counts=counts_map, planned_analysis_looks=planned_analysis_looks, single_look_fixed_horizon_plan_locked=single_look_fixed_horizon_plan_locked, ) else: fixed_n = int(_SHIELD_MIN_CYTO_N_EFF_PER_DONOR if predeclared_per_donor_unit_count_n is None else predeclared_per_donor_unit_count_n) out["predeclared_per_donor_unit_count_n"] = fixed_n out["roster_sha256"] = _shield_compute_donor_roster_sha256( roster_id, preregistration_timestamp_utc, ids_list, predeclared_per_donor_unit_count_n=fixed_n, planned_analysis_looks=planned_analysis_looks, single_look_fixed_horizon_plan_locked=single_look_fixed_horizon_plan_locked, ) return out def _shield_descriptive_quantiles(values: list[float]) -> tuple[float, float, float, float]: """Return (mean, median, min, iqr) using exact linear interpolation (overflow-immune pure stdlib).""" svals = sorted(float(v) for v in values) n = len(svals) scale_abs = max(abs(x) for x in svals) mean_v = 0.0 if scale_abs == 0.0 else (sum(x / scale_abs for x in svals) / float(n)) * scale_abs min_v = svals[0] def _pct(p: float) -> float: if n == 1: return svals[0] pos = p * float(n - 1) lo_i = int(math.floor(pos)) hi_i = int(math.ceil(pos)) frac = pos - float(lo_i) return (1.0 - frac) * svals[lo_i] + frac * svals[hi_i] med_v = _pct(0.5) q75 = _pct(0.75) q25 = _pct(0.25) iqr_v = q75 - q25 if math.isfinite(q75 - q25) else 0.0 return mean_v, med_v, min_v, iqr_v def _shield_connected_clusters( roster_ids: list[str], donor_group_sets: dict[str, set[str]], ) -> list[list[str]]: """Deterministic Union-Find partition of fixed roster_ids merging any two donors that share at least one batch or assay-run token into one conservative independent cluster. Submitted donors retain their validated batch/run dependency edges even when unrelated outcome or CI fields fail; pre-assay-excluded unmeasured donors remain singletons. """ parent: dict[str, str] = {did: did for did in roster_ids} def _find(x: str) -> str: while parent[x] != x: parent[x] = parent[parent[x]] x = parent[x] return x def _union(a: str, b: str) -> None: ra, rb = _find(a), _find(b) if ra != rb: if ra < rb: parent[rb] = ra else: parent[ra] = rb token_owner: dict[str, str] = {} for did in roster_ids: for tok in sorted(donor_group_sets.get(did, ())): if tok in token_owner: _union(did, token_owner[tok]) else: token_owner[tok] = did comps: dict[str, list[str]] = {} for did in roster_ids: comps.setdefault(_find(did), []).append(did) return [comps[k] for k in sorted(comps)] def build_valid_shield_spr_observation( *, n_eff_per_donor: int = 60, donor_k_obs: tuple[int, ...] | None = None, donor_suppressions: tuple[float, ...] | None = None, include_optional_1jk8: bool = True, planned_t1d_donor_family_n: int | None = None, include_preregistered_roster: bool = True, roster_donor_ids: list[str] | tuple[str, ...] | None = None, roster_id: str = "ROSTER_SHIELD_T1D_PREREG_V1", preregistration_timestamp_utc: str = "2026-11-28T00:00:00Z", excluded_donor_records: list[dict[str, Any]] | None = None, shared_expression_batches_n: int | None = None, shared_assay_runs_n: int | None = None, sampling_design_class: str = "SIMPLE_RANDOM_OR_CONSECUTIVE_INDEPENDENT_T1D_COHORT", separate_comp1_comp2_batches: bool = False, ) -> dict[str, Any]: """Build a complete typed Gate 30-SHIELD-SPR PASS record with all mandatory evidence blocks, mandatory hash-pinned outcome-blind preregistered_donor_family_roster committing donor membership, fixed per-donor cytotoxicity sample size N, and single-look fixed horizon (defaulting to 9 confirmatory T1D donors across 9 disjoint physical co-formulation mixtures MIX_SHIELD_01..09 and 9 disjoint assay_run_ids so cluster-conservative exact Clopper-Pearson responder L95(9/9) = 71.687116% >= 70.0% achieves calibrated PASS), typed donor_sampling_and_independence_design, construct-bound expression_batches (cocktail leads + dedicated comparator batches), full Comp1+Comp2 off-target controls, WT15 reagent-lot linkage, per-mixture quantitative SEC-MALS/DLS co-formulation qualification, typed T1D donor eligibility provenance, qualifying donor hla_genotype, authorization-grade typed GY-018 independence evidence record, and GY-018-compliant cytotoxicity denominators (exact deff == 1.0, n_eff == raw_independent_units_n >= 60). """ if donor_k_obs is None and donor_suppressions is None: k_seq: tuple[int, ...] = tuple(0 for _ in range(_SHIELD_MIN_CONFIRMATORY_DONORS_N)) sup_seq: tuple[float, ...] = (82.0, 84.0, 86.0, 81.0, 83.0, 85.0, 82.5, 84.5, 83.5) elif donor_k_obs is not None and donor_suppressions is None: k_seq = tuple(int(x) for x in donor_k_obs) sup_seq = tuple(84.0 for _ in range(len(k_seq))) elif donor_k_obs is None and donor_suppressions is not None: sup_seq = tuple(float(x) for x in donor_suppressions) k_seq = tuple(0 for _ in range(len(sup_seq))) else: k_seq = tuple(int(x) for x in donor_k_obs) sup_seq = tuple(float(x) for x in donor_suppressions) n_donors = max(len(k_seq), len(sup_seq)) n_lead_batches = ( int(shared_expression_batches_n) if shared_expression_batches_n is not None else max(_SHIELD_MIN_BATCHES_N, n_donors) ) n_assay_runs = ( int(shared_assay_runs_n) if shared_assay_runs_n is not None else max(1, n_donors) ) if separate_comp1_comp2_batches: c1_lead_batch_ids = [f"BATCH_C1_{i:02d}" for i in range(1, n_lead_batches + 1)] c2_lead_batch_ids = [f"BATCH_C2_{i:02d}" for i in range(1, n_lead_batches + 1)] spr_c1_batch_ids = c1_lead_batch_ids[: max(_SHIELD_MIN_BATCHES_N, min(3, len(c1_lead_batch_ids)))] spr_c2_batch_ids = c2_lead_batch_ids[: max(_SHIELD_MIN_BATCHES_N, min(3, len(c2_lead_batch_ids)))] sec_all_lead_batch_ids = c1_lead_batch_ids + c2_lead_batch_ids rqc_parent_batch_ids = spr_c1_batch_ids + spr_c2_batch_ids expression_batches: list[dict[str, Any]] = ( [{"batch_id": bid, "construct_ids": [_SHIELD_COMP1_LEAD], "qc_passed": True} for bid in c1_lead_batch_ids] + [{"batch_id": bid, "construct_ids": [_SHIELD_COMP2_LEAD], "qc_passed": True} for bid in c2_lead_batch_ids] ) else: lead_batch_ids = [f"BATCH_ECOLI_{i:02d}" for i in range(1, n_lead_batches + 1)] c1_lead_batch_ids = list(lead_batch_ids) c2_lead_batch_ids = list(lead_batch_ids) spr_c1_batch_ids = lead_batch_ids[: max(_SHIELD_MIN_BATCHES_N, min(3, len(lead_batch_ids)))] spr_c2_batch_ids = list(spr_c1_batch_ids) sec_all_lead_batch_ids = list(lead_batch_ids) rqc_parent_batch_ids = list(spr_c1_batch_ids) expression_batches = [ {"batch_id": bid, "construct_ids": list(_SHIELD_COCKTAIL_LEADS), "qc_passed": True} for bid in lead_batch_ids ] hip11_reagent_lot_id = "REAGENT_HIP11_EXCHANGE_LOT01" wt15_reagent_lot_id = "REAGENT_WT15_INSB9_23_LOT01" r6dfx_reagent_lot_id = "REAGENT_6DFX_P8E9E_LOT01" comparator_batch_map: dict[str, str] = { cid: f"BATCH_COMP_{cid}" for cid in _SHIELD_MANDATORY_COMP2_COMPARATORS if cid != _SHIELD_COMP2_LEAD } for cid, cbid in comparator_batch_map.items(): expression_batches.append( {"batch_id": cbid, "construct_ids": [cid], "qc_passed": True} ) qualified_mixtures: list[dict[str, Any]] = [ { "mixture_id": f"MIX_SHIELD_{m_idx + 1:02d}", "comp1_expression_batch_id": c1_lead_batch_ids[m_idx], "comp2_expression_batch_id": c2_lead_batch_ids[m_idx], "monomer_pct": 96.8, "dls_pdi": 0.08, "no_aggregates_or_precipitation": True, "qc_passed": True, "equimolar_1to1_molar_ratio_attested": True, "ratio_basis_attestation": "LAB_ATTESTED_1TO1_MOLAR_COFORMULATION_MIXING_PROTOCOL", } for m_idx in range(n_lead_batches) ] donors: list[dict[str, Any]] = [] for i in range(n_donors): k_i = k_seq[i] if i < len(k_seq) else 0 s_i = sup_seq[i] if i < len(sup_seq) else 84.0 run_idx = (i % n_assay_runs) + 1 mix_idx = i % n_lead_batches donor_batches = ( [c1_lead_batch_ids[mix_idx], c2_lead_batch_ids[mix_idx]] if separate_comp1_comp2_batches else [c1_lead_batch_ids[mix_idx]] ) donors.append( { "donor_id": f"T1D_DONOR_{i + 1:02d}", "t1d_diagnosis_confirmed": True, "donor_clinical_cohort": _SHIELD_REQUIRED_DONOR_CLINICAL_COHORT, "t1d_prequalification_ref": f"PREQUAL_T1D_DONOR_{i + 1:02d}",