scripts/verify_iip0044_foundry_v1_3_biologics.py
Use this file in any AI chat (paste-only or attachment-capable):
Pinned raw file: https://openislet.org/files/gen-eb955f8266035aba/scripts/verify_iip0044_foundry_v1_3_biologics.py · SHA-256: 1d6c53a0817f6254…
# register_control_screens covering BOTH mandatory register analytes — native 15-aa WT InsB:9-23 [WT15]
# AND engineered 12-aa 6DFX p8E9E [6DFX] — bound to construct-specific batches, validated register_qc_records
# reagent lots/sequences, replicates_n >= 3, and unique evidence_record_id; optional physical 1JK8 allowed as
# omitted or JSON null and strictly validated if non-null!)
_allowed_comp_row_keys = frozenset({
"comparator_id",
"screened",
"replicates_n",
"expression_batch_ids",
"register_control_screens",
})
_allowed_comp_reg_keys = frozenset({
"native_wt_insb9_23_15aa",
"engineered_6dfx_p8e9e_12aa",
"crystal_ref_1jk8_14aa",
})
_required_comp_cond_keys = frozenset({
"screened",
"peptide_sequence",
"peptide_reagent_lot_id",
"expression_batch_ids",
"replicates_n",
"evidence_record_id",
})
_required_comp_1jk8_keys = frozenset({
"screened",
"peptide_sequence",
"expression_batch_ids",
"replicates_n",
"evidence_record_id",
})
_allowed_comp_1jk8_keys = _required_comp_1jk8_keys | frozenset({"peptide_reagent_lot_id"})
comps_raw = observation.get("comp2_comparators_screened")
seen_comps: set[str] = set()
conflicted_comps: set[str] = set()
conflicted_comp_ev_ids: set[str] = set()
verified_comparators_both_registers: set[str] = set()
comparator_shared_batches_by_id: dict[str, list[str]] = {}
if not isinstance(comps_raw, list) or len(comps_raw) == 0:
_escalate("HOLD_INVALID_OR_MISSING_INPUT", "Missing typed comp2_comparators_screened list (all 6 comparators required)")
else:
comp_counts: dict[str, int] = {}
comp_ev_counts: dict[str, int] = {}
for rec in comps_raw:
if isinstance(rec, dict):
cid = rec.get("comparator_id")
if type(cid) is str and cid.strip():
cid_p = _shield_canonicalize_construct_id(cid)
comp_counts[cid_p] = comp_counts.get(cid_p, 0) + 1
rc_screens = rec.get("register_control_screens")
if isinstance(rc_screens, dict):
for cond_v in rc_screens.values():
if isinstance(cond_v, dict):
raw_ev = cond_v.get("evidence_record_id")
if type(raw_ev) is str and raw_ev.strip():
ev_norm = raw_ev.strip()
comp_ev_counts[ev_norm] = comp_ev_counts.get(ev_norm, 0) + 1
conflicted_comps = {k for k, cnt in comp_counts.items() if cnt > 1}
for ccid in sorted(conflicted_comps):
_escalate(
"HOLD_INVALID_OR_MISSING_INPUT",
f"Duplicate/conflicting comparator canonical key: {ccid!r} (count={comp_counts[ccid]}); all occurrences excluded",
)
conflicted_comp_ev_ids = {k for k, cnt in comp_ev_counts.items() if cnt > 1}
for cev in sorted(conflicted_comp_ev_ids):
_escalate(
"HOLD_INVALID_OR_MISSING_INPUT",
f"Duplicate/conflicting comparator register-screen evidence_record_id: {cev!r} (count={comp_ev_counts[cev]}); "
f"all affected comparator rows excluded from coverage and batch-linkage credit",
)
for idx, rec in enumerate(comps_raw):
if not isinstance(rec, dict):
_escalate("HOLD_INVALID_OR_MISSING_INPUT", f"comp2_comparators_screened[{idx}] must be an object")
continue
cid = rec.get("comparator_id")
if type(cid) is not str or not cid.strip():
_escalate("HOLD_INVALID_OR_MISSING_INPUT", f"comp2_comparators_screened[{idx}] missing comparator_id")
continue
cid_c = _shield_canonicalize_construct_id(cid)
seen_comps.add(cid_c)
if cid_c in conflicted_comps:
continue
if cid_c not in _SHIELD_MANDATORY_COMP2_COMPARATORS:
_escalate("HOLD_INVALID_OR_MISSING_INPUT", f"Unregistered comp2_comparator record rejected: {cid_c!r}")
continue
comp_row_valid = True
extra_comp_keys = sorted(set(rec.keys()) - _allowed_comp_row_keys)
if extra_comp_keys:
comp_row_valid = False
_escalate(
"HOLD_INVALID_OR_MISSING_INPUT",
f"comparator[{cid_c}] contains unexpected/untyped keys {extra_comp_keys}; "
f"strict closed schema with typed register_control_screens required",
)
tmp_ref_c: set[str] = set()
tmp_c2_c: set[str] = set()
b_err = _shield_validate_batch_ids(
rec.get("expression_batch_ids"),
f"comparator[{cid_c}]",
passing_batch_constructs,
tmp_ref_c,
required_construct_all=cid_c,
role_comp2_batches=tmp_c2_c if cid_c == _SHIELD_COMP2_LEAD else None,
)
if b_err is not None:
comp_row_valid = False
_escalate("HOLD_INVALID_OR_MISSING_INPUT", b_err)
elif cid_c == _SHIELD_COMP2_LEAD:
if not tmp_c2_c or not tmp_c2_c.issubset(rqc_comp2_batches):
comp_row_valid = False
_escalate(
"HOLD_INVALID_OR_MISSING_INPUT",
f"comparator[{cid_c}]: Comp2 expression_batch_ids={sorted(tmp_c2_c)} are not covered by "
f"parent register_qc_records Comp2 batches={sorted(rqc_comp2_batches)}",
)
if not tmp_c2_c or not tmp_c2_c.issubset(sec_comp2_qualified_batches):
comp_row_valid = False
_escalate(
"HOLD_INVALID_OR_MISSING_INPUT",
f"comparator[{cid_c}]: Comp2 expression_batch_ids={sorted(tmp_c2_c)} lack passing Comp2 "
f"SEC-MALS/DLS physicochemical material qualification "
f"(qualified_comp2_batches={sorted(sec_comp2_qualified_batches)})",
)
if rec.get("screened") is not True:
comp_row_valid = False
_escalate("HOLD_INVALID_OR_MISSING_INPUT", f"Comparator {cid_c!r} not marked screened=True")
_, rep_err = _shield_check_strict_int(
rec.get("replicates_n"), f"comparator[{cid_c}].replicates_n", min_val=_SHIELD_MIN_SPR_REPLICATES_N
)
if rep_err is not None:
comp_row_valid = False
_escalate("HOLD_INVALID_OR_MISSING_INPUT", rep_err)
reg_screens = rec.get("register_control_screens")
cond_wt_batches: set[str] = set()
cond_6dfx_batches: set[str] = set()
if not isinstance(reg_screens, dict):
comp_row_valid = False
_escalate(
"HOLD_INVALID_OR_MISSING_INPUT",
f"comparator[{cid_c}] missing mandatory typed register_control_screens object "
f"(must explicitly cover BOTH native_wt_insb9_23_15aa [WT15] AND engineered_6dfx_p8e9e_12aa [6DFX])",
)
else:
extra_reg_keys = sorted(set(reg_screens.keys()) - _allowed_comp_reg_keys)
if extra_reg_keys:
comp_row_valid = False
_escalate(
"HOLD_INVALID_OR_MISSING_INPUT",
f"comparator[{cid_c}].register_control_screens contains unexpected register keys {extra_reg_keys}",
)
for cond_key, exp_seq, exp_lot, cond_rqc_ok, out_batches in (
("native_wt_insb9_23_15aa", "SHLVEALYLVCGERG", rqc_wt_lot, wt15_register_qc_valid, cond_wt_batches),
("engineered_6dfx_p8e9e_12aa", "VEELYLVAGEEG", rqc_6dfx_lot, r6dfx_qc_valid, cond_6dfx_batches),
):
cond_obj = reg_screens.get(cond_key)
cond_lbl = f"comparator[{cid_c}].register_control_screens.{cond_key}"
if not isinstance(cond_obj, dict):
comp_row_valid = False
_escalate(
"HOLD_INVALID_OR_MISSING_INPUT",
f"{cond_lbl} missing mandatory per-comparator register screen record",
)
continue
extra_ckeys = sorted(set(cond_obj.keys()) - _required_comp_cond_keys)
missing_ckeys = sorted(_required_comp_cond_keys - set(cond_obj.keys()))
if extra_ckeys or missing_ckeys:
comp_row_valid = False
_escalate(
"HOLD_INVALID_OR_MISSING_INPUT",
f"{cond_lbl} closed-schema key violation (missing={missing_ckeys}, extra={extra_ckeys})",
)
if cond_obj.get("screened") is not True:
comp_row_valid = False
_escalate("HOLD_INVALID_OR_MISSING_INPUT", f"{cond_lbl}.screened must be literal boolean True")
if cond_obj.get("peptide_sequence") != exp_seq:
comp_row_valid = False
_escalate(
"HOLD_INVALID_OR_MISSING_INPUT",
f"{cond_lbl}.peptide_sequence={cond_obj.get('peptide_sequence')!r} != expected {exp_seq!r}",
)
raw_c_lot = cond_obj.get("peptide_reagent_lot_id")
cond_lot_ok = (
type(raw_c_lot) is str
and bool(raw_c_lot)
and raw_c_lot == raw_c_lot.strip()
and bool(_SHIELD_STRICT_ID_RE.match(raw_c_lot))
)
if not cond_lot_ok:
comp_row_valid = False
_escalate(
"HOLD_INVALID_OR_MISSING_INPUT",
f"{cond_lbl} missing valid unpadded peptide_reagent_lot_id (got {raw_c_lot!r})",
)
elif not cond_rqc_ok or exp_lot is None:
comp_row_valid = False
_escalate(
"HOLD_INVALID_OR_MISSING_INPUT",
f"{cond_lbl} excluded because mandatory register_qc_records.{cond_key} QC or parent batch linkage failed",
)
elif raw_c_lot != exp_lot:
comp_row_valid = False
_escalate(
"HOLD_INVALID_OR_MISSING_INPUT",
f"{cond_lbl}.peptide_reagent_lot_id={raw_c_lot!r} != register_qc_records.{cond_key} lot {exp_lot!r}",
)
_, crep_err = _shield_check_strict_int(
cond_obj.get("replicates_n"), f"{cond_lbl}.replicates_n", min_val=_SHIELD_MIN_SPR_REPLICATES_N
)
if crep_err is not None:
comp_row_valid = False
_escalate("HOLD_INVALID_OR_MISSING_INPUT", crep_err)
raw_cev = cond_obj.get("evidence_record_id")
if (
type(raw_cev) is not str
or not raw_cev
or raw_cev != raw_cev.strip()
or not _SHIELD_EVIDENCE_REF_RE.match(raw_cev)
):
comp_row_valid = False
_escalate(
"HOLD_INVALID_OR_MISSING_INPUT",
f"{cond_lbl}.evidence_record_id must be an unpadded audit ref matching "
f"{_SHIELD_EVIDENCE_REF_RE.pattern} (got {raw_cev!r})",
)
elif raw_cev in conflicted_comp_ev_ids:
comp_row_valid = False
cb_err = _shield_validate_batch_ids(
cond_obj.get("expression_batch_ids"),
cond_lbl,
passing_batch_constructs,
out_batches,
required_construct_all=cid_c,
)
if cb_err is not None:
comp_row_valid = False
_escalate("HOLD_INVALID_OR_MISSING_INPUT", cb_err)
elif b_err is None and not out_batches.issubset(tmp_ref_c):
comp_row_valid = False
_escalate(
"HOLD_INVALID_OR_MISSING_INPUT",
f"{cond_lbl}.expression_batch_ids={sorted(out_batches)} must be a subset of "
f"comparator[{cid_c}].expression_batch_ids={sorted(tmp_ref_c)}",
)
elif cid_c == _SHIELD_COMP2_LEAD:
if not out_batches.issubset(rqc_comp2_batches):
comp_row_valid = False
_escalate(
"HOLD_INVALID_OR_MISSING_INPUT",