scripts/wetlab_contract.py
Use this file in any AI chat (paste-only or attachment-capable):
Pinned raw file: https://openislet.org/files/gen-eb955f8266035aba/scripts/wetlab_contract.py · SHA-256: 6bb3955530f8f05d…
def _valid_domain_scalar_iip0028(val: Any, lo: float, hi: float) -> bool:
return _is_strict_finite_number_iip0024(val) and (lo <= float(val) <= hi)
def _valid_positive_se_iip0028(se: Any) -> bool:
return _is_strict_finite_number_iip0024(se) and (float(se) > 0.0)
def _valid_strict_unpadded_id_iip0028(val: Any, min_len: int = 4) -> bool:
return isinstance(val, str) and (val == val.strip()) and (len(val) >= min_len)
def build_valid_upstream_b2_record_iip_0028(
mcb_id: str = 'MCB-B2-001',
lot_id: str = 'LOT-B2-001',
) -> dict[str, Any]:
"""Build canonical upstream ISLET1_B2_5LOCUS_BASE research-comparator authorization record (IIP-0028; DEC-106)."""
return {
'product_variant_id': 'ISLET1_B2_5LOCUS_BASE',
'mcb_id': mcb_id,
'lot_id': lot_id,
'lifecycle_state': 'STAGE0_RESEARCH_COMPARATOR_AUTHORIZED_FOR_PAIRED_STUDY',
'execution_supported': False,
'design_b_fasta_sha256': DESIGN_B_FASTA_SHA256,
'bound_c1_c5_pair_sha256': DESIGN_B_C1_C5_PAIR_SHA256,
'verified_qc_atoms': {
'mcb_hlac_phased_allele_and_ab_ko_qc': {
'atom_id': f'QCATOM-MCB-HLAC-{mcb_id}',
'mcb_id': mcb_id,
'status': 'PASS',
'biallelic_hla_ab_ciita_ko_pct': 99.4,
'phased_hlac_allele': 'HLA-C*03:04:01',
},
'mcb_c1_c5_junction_and_5p3p_race_qc': {
'atom_id': f'QCATOM-MCB-C1C5-{mcb_id}',
'mcb_id': mcb_id,
'status': 'PASS',
'c1_sha256_prefix': '3392af83',
'c5_sha256_prefix': 'ba264b14',
'five_prime_three_prime_race_exact': True,
'biallelic_c1_c5_hdr_pct': 99.1,
},
'lot_hlac_dt9_and_hlae_3d12_specificity_qc': {
'atom_id': f'QCATOM-LOT-DT9-3D12-{lot_id}',
'lot_id': lot_id,
'status': 'PASS',
'dt9_hlac_fold_over_hlae_only_ctrl': 8.5,
'hlae_3d12_molecules': 28500.0,
},
},
}
def verify_upstream_b2_paired_study_authorization_iip_0028(
auth_rec: Any,
*,
expected_mcb_id: str,
expected_lot_id: str,
) -> tuple[bool, str]:
"""
Strict upstream registry and QC-atom verifier for ISLET1_B2_5LOCUS_BASE (IIP-0028; DEC-106):
- Never trusts a caller boolean;
- Binds DESIGN_B_FASTA_SHA256 (4bb20389...) and DESIGN_B_C1_C5_PAIR_SHA256 (82c351af...);
- Enforces execution_supported is False while authorizing paired Stage-0 ex-vivo responder
comparisons only when all three MCB/lot allele, c1+c5 RACE/junction, and DT9+3D12
HLA-E-controlled surface specificity QC atoms pass.
"""
if not isinstance(auth_rec, dict):
return (False, 'HOLD_B2_MISSING_UPSTREAM_AUTHORIZATION_RECORD')
if auth_rec.get('product_variant_id') != 'ISLET1_B2_5LOCUS_BASE':
return (False, 'HOLD_B2_UPSTREAM_PRODUCT_MISMATCH')
if auth_rec.get('mcb_id') != expected_mcb_id or auth_rec.get('lot_id') != expected_lot_id:
return (False, 'HOLD_B2_UPSTREAM_MCB_OR_LOT_MISMATCH')
if auth_rec.get('lifecycle_state') != 'STAGE0_RESEARCH_COMPARATOR_AUTHORIZED_FOR_PAIRED_STUDY':
return (False, 'HOLD_B2_LIFECYCLE_NOT_AUTHORIZED_FOR_PAIRED_STUDY')
if auth_rec.get('execution_supported') is not False:
return (False, 'HOLD_B2_EXECUTION_SUPPORTED_MUST_REMAIN_FALSE')
if auth_rec.get('design_b_fasta_sha256') != DESIGN_B_FASTA_SHA256:
return (False, 'HOLD_B2_FASTA_DIGEST_MISMATCH')
if auth_rec.get('bound_c1_c5_pair_sha256') != DESIGN_B_C1_C5_PAIR_SHA256:
return (False, 'HOLD_B2_C1_C5_PAIR_DIGEST_MISMATCH')
qc_atoms = auth_rec.get('verified_qc_atoms')
if not isinstance(qc_atoms, dict):
return (False, 'HOLD_B2_MISSING_UPSTREAM_QC_ATOMS')
a1 = qc_atoms.get('mcb_hlac_phased_allele_and_ab_ko_qc')
if (
not isinstance(a1, dict)
or not _valid_strict_unpadded_id_iip0028(a1.get('atom_id'))
or a1.get('mcb_id') != expected_mcb_id
or a1.get('status') != 'PASS'
or not _valid_domain_scalar_iip0028(a1.get('biallelic_hla_ab_ciita_ko_pct'), 98.0, 100.0)
or not _valid_strict_unpadded_id_iip0028(a1.get('phased_hlac_allele'), min_len=6)
):
return (False, 'HOLD_B2_UNVERIFIED_MCB_HLAC_ALLELE_AND_AB_KO_QC_ATOM')
a2 = qc_atoms.get('mcb_c1_c5_junction_and_5p3p_race_qc')
if (
not isinstance(a2, dict)
or not _valid_strict_unpadded_id_iip0028(a2.get('atom_id'))
or a2.get('mcb_id') != expected_mcb_id
or a2.get('status') != 'PASS'
or a2.get('c1_sha256_prefix') != '3392af83'
or a2.get('c5_sha256_prefix') != 'ba264b14'
or a2.get('five_prime_three_prime_race_exact') is not True
or not _valid_domain_scalar_iip0028(a2.get('biallelic_c1_c5_hdr_pct'), 98.0, 100.0)
):
return (False, 'HOLD_B2_UNVERIFIED_MCB_C1_C5_JUNCTION_AND_RACE_QC_ATOM')
a3 = qc_atoms.get('lot_hlac_dt9_and_hlae_3d12_specificity_qc')
if (
not isinstance(a3, dict)
or not _valid_strict_unpadded_id_iip0028(a3.get('atom_id'))
or a3.get('lot_id') != expected_lot_id
or a3.get('status') != 'PASS'
or not _valid_domain_scalar_iip0028(a3.get('dt9_hlac_fold_over_hlae_only_ctrl'), 2.0, 1000.0)
or not _valid_domain_scalar_iip0028(a3.get('hlae_3d12_molecules'), 15000.0, 1000000.0)
):
return (False, 'HOLD_B2_UNVERIFIED_LOT_HLAC_DT9_HLAE_3D12_SPECIFICITY_QC_ATOM')
return (True, 'AUTHORIZED_B2_STAGE0_PAIRED_STUDY_EXECUTION_SUPPORTED_FALSE')
def _read_se_alias_iip0028(obs: dict[str, Any], primary_se_key: str, *alias_se_keys: str) -> Any:
if primary_se_key in obs and obs[primary_se_key] is not None:
return obs[primary_se_key]
for ak in alias_se_keys:
if ak in obs and obs[ak] is not None:
return obs[ak]
return None
def evaluate_paired_adcc_and_postdepletion_gate_iip_0028(
obs: dict[str, Any],
*,
root: Path | str | None = None,
upstream_b2_authorization_record: dict[str, Any] | None = None,
) -> tuple[bool, str]:
"""
Fail-closed registry-bound Gate 0d + intact-and-increment CD16+ ADCC evaluator (IIP-0028; DEC-106):
- Rejects omitted, None, empty, whitespace-padded, unregistered, caller-overridden, or Track-2 B'-SCT IDs;
- Gates ISLET1_B2_5LOCUS_BASE on verified upstream 82c351af... c1+c5 authorization + QC atoms (execution_supported=False);
- Separates Arm 1 reference, Arm 2 post-depletion (preserving all 6 Arm-2 explicit-SE endpoints + contemporaneous
Arm-3 active-FasL control + unpooled washout/aging + segregated RETAINED_ACTIVE SA-FHL-1), and Arm 3 profiles;
- Caps BOTH intact-serum total cellular killing AND paired IgG-attributable CD16+ ADCC increment at U95_t <= 5.0%
(preventing high Fc-independent background injury such as 30% intact vs 29% depleted from clearing on a 1% delta),
alongside positive control L95_t >= 25.0% and isotype background U95_t <= 5.0%.
"""
if not isinstance(obs, dict):
return (False, 'HOLD_INVALID_OBSERVATION_PAYLOAD')
if any(k in obs for k in ('product_defs', 'override_product_defs', 'execution_supported', 'override_execution_supported')):
return (False, 'HOLD_CALLER_PRODUCT_DEF_OR_EXECUTION_OVERRIDE_REJECTED')
if 'product_variant_id' not in obs or obs['product_variant_id'] is None:
return (False, 'HOLD_MISSING_PRODUCT_VARIANT_ID')
prod_id = obs['product_variant_id']
if not isinstance(prod_id, str) or prod_id == '':
return (False, 'HOLD_MISSING_PRODUCT_VARIANT_ID')
if prod_id != prod_id.strip():
return (False, 'HOLD_WHITESPACE_PADDED_PRODUCT_VARIANT_ID')
if prod_id.startswith('ISLET1_B_SCT_'):
return (False, 'HOLD_UNSUPPORTED_TRACK2_BSCT_REQUIRES_SEPARATE_GATE0B_SCT')
root_path = Path(root) if root is not None else Path(__file__).resolve().parents[1]
p_mod = _load_product_contract_module(root_path)
prod_doc = p_mod.load_product_contracts(root_path)
reg_by_id = {str(p['product_variant_id']): p for p in prod_doc['products']}
reg_entry = reg_by_id.get(prod_id)
if reg_entry is None:
return (False, 'HOLD_UNREGISTERED_PRODUCT_VARIANT_ID')
mcb_id = obs.get('mcb_id', obs.get('mcb_clone_id'))
lot_id = obs.get('lot_id', obs.get('current_lot_id'))
if not _valid_strict_unpadded_id_iip0028(mcb_id) or not _valid_strict_unpadded_id_iip0028(lot_id):
return (False, 'HOLD_GATE0D_MISSING_OR_PADDED_MCB_OR_LOT_ID')
if prod_id == 'ISLET1_B2_5LOCUS_BASE':
if upstream_b2_authorization_record is None:
if (
obs.get('registered_and_qc_cleared') is True
or obs.get('comparator_registered_and_qc_cleared') is True
or any(str(k).endswith('_registered_and_qc_cleared') and obs.get(k) is True for k in obs)
):
return (False, 'HOLD_B2_CALLER_BOOLEAN_REJECTED_MISSING_UPSTREAM_REGISTRY_RECORD')
return (False, 'HOLD_B2_PENDING_UPSTREAM_COMPARATOR_REGISTRATION')
b2_ok, b2_code = verify_upstream_b2_paired_study_authorization_iip_0028(
upstream_b2_authorization_record,
expected_mcb_id=str(mcb_id),
expected_lot_id=str(lot_id),
)
if not b2_ok:
return (False, b2_code)
elif reg_entry.get('execution_supported') is False:
return (False, 'HOLD_NON_EXECUTABLE_RESEARCH_COMPARATOR_VARIANT')
arm = obs.get('adjunct_conditioning_history_arm')
if arm not in ('ARM1_NEVER_CONDITIONED', 'ARM2_CONDITIONED_DEPLETED', 'ARM3_CONTINUOUSLY_ACTIVE'):
return (False, 'HOLD_GATE0D_MISSING_EXPLICIT_HISTORY_ARM')
n_rep = obs.get('n_independent_blocks', obs.get('obs_stage0_independent_lot_replicates_n'))
if not _is_strict_int_iip0024(n_rep) or n_rep < 6:
return (False, 'HOLD_GATE0D_INSUFFICIENT_INDEPENDENT_BLOCKS_LT_6')
t_crit = student_t_ppf(0.95, n_rep - 1)
if arm == 'ARM1_NEVER_CONDITIONED':
for mk, sk1, sk2, lo, hi in (
('obs_arm1_unconditioned_cd4_si', 'obs_arm1_unconditioned_cd4_si_se', 'se_arm1_cd4_si', 0.1, 100.0),
('obs_arm1_unconditioned_annexin_v_pct', 'obs_arm1_unconditioned_annexin_v_se', 'se_arm1_annexin_v', 0.0, 100.0),
):
s_v = _read_se_alias_iip0028(obs, sk1, sk2)
if not _valid_domain_scalar_iip0028(obs.get(mk), lo, hi) or not _valid_positive_se_iip0028(s_v):
return (False, f'HOLD_GATE0D_ARM1_INVALID_OR_MISSING_CONTROL_{mk}')
return (False, 'HOLD_GATE0D_ARM1_NEVER_CONDITIONED_REFERENCE_CONTROL')
if not _valid_strict_unpadded_id_iip0028(obs.get('recipient_serum_pair_id')):
return (False, 'HOLD_ADCC_MISSING_OR_INVALID_RECIPIENT_SERUM_PAIR_ID')
intact_sid = obs.get('intact_serum_sample_id')
dep_sid = obs.get('igg_depleted_serum_sample_id')
if not _valid_strict_unpadded_id_iip0028(intact_sid) or not _valid_strict_unpadded_id_iip0028(dep_sid) or intact_sid == dep_sid:
return (False, 'HOLD_ADCC_MISSING_OR_DUPLICATE_PAIRED_SERUM_SAMPLE_IDS')
pos_cid = obs.get('positive_adcc_control_id')
iso_cid = obs.get('isotype_adcc_control_id')
if not _valid_strict_unpadded_id_iip0028(pos_cid) or not _valid_strict_unpadded_id_iip0028(iso_cid) or pos_cid == iso_cid:
return (False, 'HOLD_ADCC_MISSING_OR_DUPLICATE_POSITIVE_ISOTYPE_CONTROL_IDS')
if obs.get('igg_depleted_matched_serum_control_verified', obs.get('cd16_adcc_igg_depletion_paired_control_verified')) is not True:
return (False, 'HOLD_ADCC_MISSING_IGG_DEPLETED_MATCHED_SERUM_CONTROL')
if not _valid_domain_scalar_iip0028(obs.get('obs_igg_depletion_efficiency_pct'), 95.0, 100.0):
return (False, 'HOLD_ADCC_IGG_DEPLETION_EFFICIENCY_BELOW_95PCT')
if obs.get('fcr_effector_fcgr3a_158v_typed_verified') is not True:
return (False, 'HOLD_ADCC_UNVERIFIED_CD16_FCGR3A_EFFECTOR_COMPETENCE')
# Enforce BOTH intact-serum total cellular killing U95_t <= 5.0% AND paired IgG-attributable increment U95_t <= 5.0%:
intact_m = obs.get('obs_fcr_cd16_intact_serum_total_lysis_pct')
intact_s = _read_se_alias_iip0028(obs, 'obs_fcr_cd16_intact_serum_total_lysis_se', 'se_intact_adcc')
dep_m = obs.get('obs_fcr_cd16_igg_depleted_serum_lysis_pct')
if not _valid_domain_scalar_iip0028(intact_m, 0.0, 100.0) or not _valid_positive_se_iip0028(intact_s):
return (False, 'HOLD_MISSING_OR_INVALID_obs_fcr_cd16_intact_serum_total_lysis_pct')
if dep_m is not None:
if not _valid_domain_scalar_iip0028(dep_m, 0.0, 100.0):
return (False, 'HOLD_MISSING_OR_INVALID_obs_fcr_cd16_igg_depleted_serum_lysis_pct')
net_check = obs.get('obs_fcr_cd16_net_adcc_lysis_vs_igg_depleted_pct')
if _is_strict_finite_number_iip0024(net_check) and abs((float(intact_m) - float(dep_m)) - float(net_check)) > 1e-4:
return (False, 'HOLD_ADCC_INTACT_MINUS_DEPLETED_ARITHMETIC_MISMATCH')
if float(intact_m) + t_crit * float(intact_s) > 5.0:
return (False, 'HOLD_EXCEEDED_UPPER95_obs_fcr_cd16_intact_serum_total_lysis_pct')
pos_adcc_m = obs.get('obs_adcc_positive_control_lysis_pct', obs.get('obs_cd16_adcc_positive_control_lysis_pct'))
pos_adcc_s = _read_se_alias_iip0028(obs, 'obs_cd16_adcc_positive_control_lysis_se', 'se_pos_adcc')
if not _valid_domain_scalar_iip0028(pos_adcc_m, 0.0, 100.0) or not _valid_positive_se_iip0028(pos_adcc_s):