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…
'obs_ciita_ko_ex4_ex6_aberrant_skip_pct',
'obs_ciita_ko_ex4_ex6_aberrant_skip_se',
})
_S11_KIKI_ONLY_FIELDS: frozenset[str] = frozenset({
'obs_ciita_ki_allele1_informative_reads_n',
'obs_ciita_ki_allele2_informative_reads_n',
'obs_ciita_allele1_informative_reads_n',
'obs_ciita_allele2_informative_reads_n',
'obs_ciita_ki_allele1_ex4_ex6_aberrant_skip_reads_k',
'obs_ciita_ki_allele2_ex4_ex6_aberrant_skip_reads_k',
'obs_ciita_allele1_ex4_ex6_aberrant_skip_reads_k',
'obs_ciita_allele2_ex4_ex6_aberrant_skip_reads_k',
'obs_ciita_ki_allele1_ex4_ex6_aberrant_skip_pct',
'obs_ciita_ki_allele2_ex4_ex6_aberrant_skip_pct',
'obs_ciita_allele1_ex4_ex6_aberrant_skip_pct',
'obs_ciita_allele2_ex4_ex6_aberrant_skip_pct',
'obs_ciita_ki_allele1_ex4_ex6_aberrant_skip_se',
'obs_ciita_ki_allele2_ex4_ex6_aberrant_skip_se',
'obs_ciita_allele1_ex4_ex6_aberrant_skip_se',
'obs_ciita_allele2_ex4_ex6_aberrant_skip_se',
})
def evaluate_locus2_ciita_modeb_challenger_v3_gate(
payload: dict[str, Any],
*,
root: Path | str | None = None,
product_def: dict[str, Any] | None = None,
_trusted_p_mod: Any = None,
) -> dict[str, Any]:
"""
Fail-closed Locus-2 CIITA Mode-B v3 & Rule S11 evaluator (IIP-0031; DEC-110).
"""
root_path = Path(root) if root is not None else Path(__file__).resolve().parents[1]
p_mod = _trusted_p_mod if _trusted_p_mod is not None else _load_product_contract_module(root_path)
payload_malformed = not isinstance(payload, dict)
if payload_malformed:
payload = {}
raw_rec_obj = payload.get('iip0031_locus2_record')
rec_malformed = payload_malformed or (raw_rec_obj is not None and not isinstance(raw_rec_obj, dict))
rec_in: dict[str, Any] = dict(raw_rec_obj) if isinstance(raw_rec_obj, dict) else {}
outer_only = {k: v for k, v in payload.items() if isinstance(k, str) and k != 'iip0031_locus2_record' and v is not None}
pvid_supplied = ('product_variant_id' in payload) or ('product_variant_id' in rec_in)
pvid_outer_raw = payload.get('product_variant_id') if 'product_variant_id' in payload else rec_in.get('product_variant_id')
if not pvid_supplied and isinstance(product_def, dict) and _trusted_p_mod is not None:
pvid_outer_raw = product_def.get('product_variant_id')
pvid_supplied = True
pvid_canon, pdef_loaded, registry_ok = _canonicalize_product_id_detailed_iip0031(
pvid_outer_raw,
root_path,
_trusted_p_mod=p_mod,
)
# Never let an unverified caller `product_def` override the authoritative registry binding (#14):
if pdef_loaded is not None:
if isinstance(product_def, dict) and hasattr(p_mod, 'validate_product_definition_dict'):
try:
pdef = p_mod.validate_product_definition_dict(product_def, root=root_path)
if pdef.get('product_variant_id') != pdef_loaded.get('product_variant_id'):
pdef = pdef_loaded
except ValueError:
pdef = pdef_loaded
else:
pdef = pdef_loaded
else:
pdef = None
l2_binding = pdef.get('staged_locus2_modeb_donor_binding') if isinstance(pdef, dict) else None
expected_donor_id = str(l2_binding.get('locus2_donor_id')) if isinstance(l2_binding, dict) and l2_binding.get('locus2_donor_id') else None
expected_donor_sha = str(l2_binding.get('locus2_donor_sha256')) if isinstance(l2_binding, dict) and l2_binding.get('locus2_donor_sha256') else None
expected_bg_id = str(l2_binding.get('background_id')) if isinstance(l2_binding, dict) and l2_binding.get('background_id') else None
supplied_donor_ids = [
v for v in (
outer_only.get('l2_v3_donor_id'),
outer_only.get('donor_id'),
rec_in.get('l2_v3_donor_id'),
rec_in.get('donor_id'),
)
if v is not None
]
supplied_bg_ids = [
v for v in (outer_only.get('background_id'), rec_in.get('background_id'))
if v is not None
]
donor_id = (
expected_donor_id
if expected_donor_id is not None
else (supplied_donor_ids[0] if supplied_donor_ids and isinstance(supplied_donor_ids[0], str) else None)
)
_all_candidate_donors = {
d for d in ([expected_donor_id] + supplied_donor_ids) if isinstance(d, str) and d.strip()
}
_all_candidate_bgs = [
b.strip() for b in ([expected_bg_id] + supplied_bg_ids) if isinstance(b, str) and b.strip()
]
_bg_outer = outer_only.get('background_id')
_bg_nested = rec_in.get('background_id')
bg_id_raw = (
_bg_outer.strip() if isinstance(_bg_outer, str) and _bg_outer.strip()
else (expected_bg_id or (_bg_nested.strip() if isinstance(_bg_nested, str) else ''))
)
bg_id = expected_bg_id if expected_bg_id is not None else bg_id_raw
is_l2_variant = bool(pvid_canon in getattr(p_mod, 'STAGED_LOCUS2_V3_PRODUCT_IDS', frozenset())) or bool(donor_id) or bool(supplied_donor_ids)
merged = {**rec_in, **outer_only}
def _out(status: str, gate_pass: bool, reasons: list[str], **extra: Any) -> dict[str, Any]:
return {
'rule_id': RULE_S11_GATE_ID_IIP0031,
'decision_id': 'DEC-110',
'contribution_id': 'IIP-0031',
'product_variant_id': pvid_canon,
'l2_v3_donor_id': donor_id,
'status': status,
'gate_pass': gate_pass,
'reasons': reasons,
'execution_supported': False,
**(
{'barrier_intervention_classification': IIP0031_BARRIER_CHS4_250_CLASSIFICATION}
if donor_id == 'L2_V3_BARRIER_CHS4_250' or pvid_canon == 'ISLET1_A4_L2_V3_BARRIER_CHS4_250'
else {}
),
**extra,
}
if not registry_ok:
return _out(
'HOLD_S11_UNVERIFIED_OR_CORRUPTED_PRODUCT_REGISTRY',
False,
['HOLD_S11_UNVERIFIED_OR_CORRUPTED_PRODUCT_REGISTRY'],
)
has_any_s11_fields = bool(rec_in) or any(
isinstance(k, str) and (
k.startswith(('obs_l2_v3_', 'obs_ciita_'))
or k in _ALL_S11_RECOGNIZED_EVIDENCE_KEYS
)
for k in outer_only
)
# 1. Hard architectural compatibility blocks (evaluated uniformly across flat & nested inputs, #12, #14):
if isinstance(pvid_canon, str) and pvid_canon.startswith('ISLET1_B_SCT_') and (
donor_id is not None
or bool(supplied_donor_ids)
or has_any_s11_fields
or rec_malformed
or merged.get('require_full_iip0031_locus2_and_tier6b_panel') is True
):
return _out(
'FAIL_S11_TRACK2_BSCT_INCOMPATIBLE_AT_LOCUS_2',
False,
['FAIL_S11_TRACK2_BSCT_INCOMPATIBLE_AT_LOCUS_2', 'INCOMPATIBLE_AT_LOCUS_2'],
)
is_design_y_any = bool(
(isinstance(pvid_canon, str) and pvid_canon.startswith('ISLET1_Y_'))
or (isinstance(pvid_outer_raw, str) and pvid_outer_raw == pvid_outer_raw.strip() and pvid_outer_raw.startswith('ISLET1_Y_'))
or any(b.upper().startswith('DESIGN_Y') for b in _all_candidate_bgs)
)
has_syn5_cd200_any = bool(
_all_candidate_donors.intersection({
'L2_V3_CO_PRIMARY_CD200',
'L2_V3_DESIGN_B_CD200',
'L2_V3_CTRL_NT_SCRAMBLE',
'L2_V3_BARRIER_CHS4_250',
})
or (isinstance(l2_binding, dict) and l2_binding.get('unit2_promoter') == 'Syn_5xISRE_minP' and l2_binding.get('unit2_cargo') == 'CD200')
or any(c.get('syn_5xisre_cd200_in_design_y') is True for c in (outer_only, rec_in))
)
if is_design_y_any and has_syn5_cd200_any:
return _out('FAIL_S11_SYN5XISRE_CD200_BARRED_IN_DESIGN_Y_Y03', False, ['FAIL_S11_SYN5XISRE_CD200_BARRED_IN_DESIGN_Y_Y03'])
has_mir_active = bool(
(isinstance(l2_binding, dict) and l2_binding.get('has_intronic_mir') is True and l2_binding.get('mir_class') == 'ACTIVE_CD276_PVR')
or any(c.get('has_intronic_mir_cd276_pvr') is True for c in (outer_only, rec_in))
or bool(_all_candidate_donors.intersection({
'L2_V3_CO_PRIMARY_CD200',
'L2_V3_CO_PRIMARY_MANF',
'L2_V3_ALT2_CCL22',
'L2_V3_BARRIER_CHS4_250',
'L2_V3_CD274P_ZERO_TATATA',
}))
)
is_design_b_bg = bool(
(isinstance(pdef, dict) and pdef.get('family') == 'B' and not str(pvid_canon or '').startswith('ISLET1_B_SCT_'))
or any(b.upper().startswith('DESIGN_B') for b in _all_candidate_bgs)
)
if is_design_b_bg and has_mir_active:
return _out('FAIL_S11_MIR_CD276_PVR_BARRED_ON_DESIGN_B_HLAC_INTACT', False, ['FAIL_S11_MIR_CD276_PVR_BARRED_ON_DESIGN_B_HLAC_INTACT'])
if pvid_supplied and pdef_loaded is None:
return _out(
'HOLD_S11_UNREGISTERED_OR_INVALID_PRODUCT_VARIANT_ID',
False,
['HOLD_S11_UNREGISTERED_OR_INVALID_PRODUCT_VARIANT_ID'],
)
if not pvid_supplied and not is_l2_variant and not has_any_s11_fields and not rec_malformed:
return _out(
'HOLD_S11_UNREGISTERED_OR_INVALID_PRODUCT_VARIANT_ID',
False,
['HOLD_S11_UNREGISTERED_OR_INVALID_PRODUCT_VARIANT_ID'],
)
if (
pdef_loaded is not None
and not is_l2_variant
and not has_any_s11_fields
and not rec_malformed
and merged.get('require_full_iip0031_locus2_and_tier6b_panel') is not True
):
return _out('NOT_APPLICABLE_NON_LOCUS2_MODEB_VARIANT', True, [])
if rec_malformed:
return _out('HOLD_S11_MALFORMED_RECORD', False, ['HOLD_S11_MALFORMED_RECORD'])
# 2. Enforce LOT_SCOPE, strict Donor-ID + SHA-256 + Background Registry Binding, & forbid nested provenance overrides:
for sc_check_v in (outer_only.get('scope_type'), rec_in.get('scope_type')):
if sc_check_v is not None and sc_check_v != 'LOT_SCOPE':
return _out('HOLD_S11_PANEL_REQUIRES_LOT_SCOPE', False, ['HOLD_S11_PANEL_REQUIRES_LOT_SCOPE'])
if isinstance(l2_binding, dict):
if expected_donor_id is not None:
for sup_did in supplied_donor_ids:
if not isinstance(sup_did, str) or sup_did != expected_donor_id:
return _out('HOLD_S11_DONOR_MISMATCH', False, ['HOLD_S11_DONOR_MISMATCH'])
if expected_donor_sha is not None:
for sup_sha in (outer_only.get('construct_sha256'), rec_in.get('construct_sha256')):
if sup_sha is not None and sup_sha != expected_donor_sha:
return _out('HOLD_S11_DONOR_MISMATCH', False, ['HOLD_S11_DONOR_MISMATCH'])
if expected_bg_id is not None:
for sup_bg in supplied_bg_ids:
if not isinstance(sup_bg, str) or sup_bg != expected_bg_id:
return _out('HOLD_S11_BACKGROUND_MISMATCH', False, ['HOLD_S11_BACKGROUND_MISMATCH'])
if 'product_variant_id' in outer_only and 'product_variant_id' in rec_in and rec_in['product_variant_id'] is not None:
nested_pvid_c, _, _ = _canonicalize_product_id_detailed_iip0031(rec_in['product_variant_id'], root_path, _trusted_p_mod=p_mod)
if nested_pvid_c != pvid_canon:
return _out('FAIL_S11_NESTED_PROVENANCE_SCOPE_MISMATCH', False, ['FAIL_S11_NESTED_PROVENANCE_SCOPE_MISMATCH'])
for id_k in ('construct_sha256', 'background_id', 'donor_seedline_id', 'seed_line_id', 'mcb_id', 'lot_id', 'current_lot_id', 'sample_id', 'sample_unit_id', 'l2_v3_donor_id', 'donor_id'):
if id_k in outer_only and id_k in rec_in and rec_in[id_k] is not None and rec_in[id_k] != outer_only[id_k]:
return _out('FAIL_S11_NESTED_PROVENANCE_SCOPE_MISMATCH', False, ['FAIL_S11_NESTED_PROVENANCE_SCOPE_MISMATCH'])
_all_s11_sids = [
v for v in (
outer_only.get('sample_id'),
outer_only.get('sample_unit_id'),
rec_in.get('sample_id'),
rec_in.get('sample_unit_id'),