return rest def evaluate_iip0042_alignment(side_contract_text: str, protocol_md_text: str) -> dict[str, Any]: """Executable INDEPENDENT alignment auditor for IIP-0042 side-contract (typed JSON fields) AND GATE-30 companion protocol (authored Markdown section ### 2.2 checked separately). Enforces: - Exact complete set of ALL 4 mandatory on-target analytes + typed mandatory_on_target_spr_analyte_specs on JSON (rejecting omission/optional wording in any analyte entry!) AND full mandatory-all-four clause on Markdown (rejecting 'only one optional complex' or omission wording!). - Exact thresholds, directions, logical operators (AND on PASS; OR on FAIL), N_eff >= 60, both nonzero cytotoxicity hold states, 6-level fail-first precedence, and pre-adjudication ValueError on non-finite inputs on BOTH surfaces (rejecting e.g. L95 > 50.0% in Markdown!). - Typed 1JK8/WT15/6DFX peptide separation and anti-conflation check on BOTH surfaces. """ checks: dict[str, bool] = {} # --- PART A: Typed JSON validation on IIP-0042 side contract ONLY --- try: sc_obj = load_strict_json_text(side_contract_text, "iip0042_side_contract") except Exception: sc_obj = None gate_spr = ( sc_obj.get("executable_stage0_and_conditional_gates", {}).get("Gate_30_SHIELD_SPR") if isinstance(sc_obj, dict) else None ) sc_low = side_contract_text.lower() if isinstance(gate_spr, dict): reg = gate_spr.get("registered_cocktail_identity", {}) ctrl = gate_spr.get("mandatory_component2_bench_controls_iip0044", {}) c1_analytes = [str(x) for x in ctrl.get("control_1_off_target_spr_analytes", [])] c2_sec = str(ctrl.get("control_2_secmals_dls_monomer_and_coformulation", "")) c3_blk = ctrl.get("control_3_parallel_comparator_and_register_screen", {}) c3_comps = [str(x) for x in c3_blk.get("parallel_comparators", [])] c3_regs_str = " ".join(str(x) for x in c3_blk.get("register_controls", [])) on_target_raw = gate_spr.get("mandatory_on_target_spr_analytes", []) on_target_list = [str(x) for x in on_target_raw] if isinstance(on_target_raw, list) else [] analyte_specs = gate_spr.get("mandatory_on_target_spr_analyte_specs", []) qc_str = str(gate_spr.get("mandatory_reagent_qc", "")) qc_low = qc_str.lower() reg_spec = gate_spr.get("register_and_peptide_identity_spec", {}) reg_spec_str = json.dumps(reg_spec, ensure_ascii=False) eps = {ep.get("endpoint_id"): ep for ep in gate_spr.get("endpoints_and_3state_rules", []) if isinstance(ep, dict)} ep_aff = eps.get("SHIELD_AFFINITY_AND_SELECTIVITY", {}) ep_blk = eps.get("SHIELD_HUMAN_TCELL_BLOCKADE", {}) prec_tbl = gate_spr.get("decision_precedence_table", {}) cyto_unit = gate_spr.get("cytotoxicity_confidence_unit_specification", {}) typed_ev = gate_spr.get("typed_evidence_contract", {}) checks["sc_typed_gate_present"] = True checks["sc_stale_v13_hold_note_retired"] = ("hold for v1.3 affinity maturation" not in sc_low) checks["sc_post_spr_v14_maturation_present"] = ("v1.4" in str(ep_aff.get("hold_rule", "")).lower()) checks["sc_comp1_qf09_pi2_lead"] = (reg.get("component_1_lead_id") == "IsletSHIELD_QF09_pI2") checks["sc_comp2_t006_lead"] = (reg.get("component_2_lead_id") == "IsletSHIELD_Comp2_T006") checks["sc_two_component_cocktail"] = ("2-component" in str(reg.get("architecture", "")).lower()) # Exact complete canonical 4-analyte set + typed analyte_specs (rejects omission/optional wording!) exact_4_strings_ok = ( len(on_target_list) == 4 and set(on_target_list) == set(_EXPECTED_MANDATORY_ANALYTE_STRINGS) and all(_RE_OMISSION_OR_OPTIONAL.search(s) is None for s in on_target_list) ) typed_specs_ok = False if isinstance(analyte_specs, list) and len(analyte_specs) == 4: spec_by_id = { str(s.get("analyte_id") or ""): s for s in analyte_specs if isinstance(s, dict) } if set(spec_by_id.keys()) == set(_EXPECTED_MANDATORY_ANALYTE_SPEC_IDS): typed_specs_ok = all( s.get("required") is True and s.get("min_replicates_n") == 3 and s.get("ic50_pass_u95_max_nm") == 100.0 and s.get("ic50_fail_l95_gt_nm") == 500.0 and s.get("selectivity_pass_l95_min_fold") == 50.0 and s.get("selectivity_fail_u95_lt_fold") == 10.0 for s in spec_by_id.values() ) and spec_by_id["HLA-DQ8 + native WT InsB:9-23"].get("peptide_sequence") == "SHLVEALYLVCGERG" aff_spec = ep_aff.get("thresholds_spec") aff_spec_ok = ( isinstance(aff_spec, dict) and aff_spec.get("ic50_pass_u95_max_nm") == 100.0 and aff_spec.get("ic50_fail_l95_gt_nm") == 500.0 and aff_spec.get("selectivity_pass_l95_min_fold") == 50.0 and aff_spec.get("selectivity_fail_u95_lt_fold") == 10.0 and "u95(ic50) <= 100.0 nm" in str(ep_aff.get("pass_rule", "")).lower() and "50.0x" in str(ep_aff.get("pass_rule", "")).lower() and "l95(ic50) > 500.0 nm" in str(ep_aff.get("fail_rule", "")).lower() and "u95(selectivity) < 10.0x" in str(ep_aff.get("fail_rule", "")).lower() ) comp1_tgt_low = str(reg.get("component_1_target", "")).lower() checks["sc_all_4_mandatory_on_target_analytes_and_qc"] = ( exact_4_strings_ok and typed_specs_ok and aff_spec_ok and isinstance(typed_ev, dict) and typed_ev.get("scalar_summaries_without_typed_records_rejected") is True and "hla-dq2.5" in comp1_tgt_low and "hla-dq8-trans" in comp1_tgt_low and "hla-dq2.2" in comp1_tgt_low and _RE_OMISSION_OR_OPTIONAL.search(comp1_tgt_low) is None and "hla-dq2.5" in qc_low and "hla-dq8-trans" in qc_low and "hla-dq2.2" in qc_low and "shlvealylvcgerg" in qc_low ) checks["sc_dq22_in_target_analytes_and_qc"] = ( "HLA-DQ2.2 + HIP11" in on_target_list and typed_specs_ok and "hla-dq2.2" in comp1_tgt_low and "hla-dq2.2" in qc_low ) checks["sc_ctrl1_all_3_offtarget_analytes"] = ( "HLA-DQ8 + CLIP1" in c1_analytes and "HLA-DQ8 + DQ8-glia-gamma1a" in c1_analytes and "HLA-DQ8-trans + WT InsB:9-23" in c1_analytes ) comp1_off_list = [str(x) for x in gate_spr.get("mandatory_component1_off_target_spr_analytes_iip0044", [])] checks["sc_comp1_all_4_offtarget_analytes"] = all( a in comp1_off_list for a in _SHIELD_MANDATORY_COMP1_OFF_TARGET_CONTROLS ) sec_thr = typed_ev.get("sec_mals_dls_quantitative_pass_thresholds", {}) if isinstance(typed_ev, dict) else {} hla_allow = typed_ev.get("qualifying_t1d_donor_hla_dq_allowlist", []) if isinstance(typed_ev, dict) else [] p1jk8_pol = str(typed_ev.get("physical_1jk8_reagent_policy", "")).lower() if isinstance(typed_ev, dict) else "" gy18_rule = str(cyto_unit.get("gy018_denominator_compliance_rule", "")).lower() if isinstance(cyto_unit, dict) else "" checks["sc_r05_quant_secmals_batch_hla_gy018_optional_1jk8"] = ( isinstance(typed_ev, dict) and typed_ev.get("expression_batch_linkage_required_every_assay_block") is True and typed_ev.get("construct_bound_expression_batch_provenance_enforced") is True and typed_ev.get("empty_construct_batches_rejected") is True and typed_ev.get("invalid_or_unregistered_spr_rows_excluded_from_bio_fail") is True and typed_ev.get("order_independent_canonical_key_uniqueness_enforced") is True and typed_ev.get("mandatory_typed_t1d_donor_eligibility_enforced") is True and typed_ev.get("required_donor_clinical_cohort") == _SHIELD_REQUIRED_DONOR_CLINICAL_COHORT and typed_ev.get("min_distinct_referenced_expression_batches_n") == 3 and typed_ev.get("min_distinct_linked_comp1_batches_n") == 3 and typed_ev.get("min_distinct_linked_comp2_batches_n") == 3 and isinstance(sec_thr, dict) and sec_thr.get("comp1_monomer_pct_min") == 95.0 and sec_thr.get("comp2_monomer_pct_min") == 95.0 and sec_thr.get("cocktail_1to1_monomer_pct_min") == 95.0 and sec_thr.get("dls_pdi_max") == 0.15 and sec_thr.get("no_aggregates_or_precipitation_required") is True and all(h in hla_allow for h in ("HLA-DQ2.5", "HLA-DQ8", "HLA-DQ8-trans", "HLA-DQ2.2")) and "optional" in p1jk8_pol and "null" in p1jk8_pol and "1jk8" in p1jk8_pol and "gy-018" in gy18_rule and "design_effect_deff == 1.0" in gy18_rule and cyto_unit.get("exact_deff_equality_zero_tolerance") is True and cyto_unit.get("exact_deff_preserved_as_decimal_from_json") is True and cyto_unit.get("invalid_donors_excluded_from_confidence_bounds") is True and cyto_unit.get("cytotoxicity_estimand") == _SHIELD_CYTO_ESTIMAND and cyto_unit.get("simultaneous_fwer_alpha") == 0.05 and cyto_unit.get("per_donor_bonferroni_alpha_formula") == "0.05 / N_roster" and cyto_unit.get("fixed_preexclusion_family_denominator_enforced") is True and cyto_unit.get("preregistered_donor_roster_mandatory") is True and cyto_unit.get("preregistered_donor_roster_sha256_verified") is True and cyto_unit.get("gy018_independence_justification_mandatory") is True and cyto_unit.get("suppression_estimand") == _SHIELD_SUPPRESSION_ESTIMAND and cyto_unit.get("suppression_inference_method") == _SHIELD_SUPPRESSION_INFERENCE_METHOD and cyto_unit.get("suppression_student_t_authorizing") is False and cyto_unit.get("cohort_t_lower_authorizing") is False and typed_ev.get("preregistered_donor_family_roster_mandatory") is True and typed_ev.get("preregistered_donor_family_roster_sha256_verified") is True and typed_ev.get("gy018_independence_justification_mandatory") is True and typed_ev.get("suppression_student_t_authorizing") is False and typed_ev.get("suppression_confirmatory_min_donors_n") == _SHIELD_MIN_CONFIRMATORY_DONORS_N ) checks["sc_ctrl2_secmals_dls_homodimer_06691"] = ( "sec-mals" in c2_sec.lower() and "dls" in c2_sec.lower() and "homodimer" in c2_sec.lower() and "0.6691" in c2_sec and "IsletSHIELD_Comp2_T006" in c2_sec and "IsletSHIELD_QF09_pI2" in c2_sec ) checks["sc_ctrl3_all_6_parallel_comparators"] = all( tok in c3_comps for tok in ("IsletSHIELD_Comp2_T006", "SHIELD2_QQ", "R3_shv2_stk1", "SH2_MP01", "SH2_MP06", "SH2_MP08") ) typed_peps_ok = isinstance(reg_spec, dict) and all( isinstance(reg_spec.get(rk), dict) and all(reg_spec[rk].get(fk) == fv for fk, fv in exp.items()) for rk, exp in EXPECTED_INSB_REFERENCE_PEPTIDES.items() ) sc_decoded_blob = json.dumps(sc_obj, ensure_ascii=False) checks["sc_register_1jk8_wt15_6dfx_separated"] = ( typed_peps_ok and _find_unnegated_1jk8_conflation(side_contract_text) is None and _find_unnegated_1jk8_conflation(sc_decoded_blob) is None and "LVEALYLVCGERGG" in reg_spec_str and "SHLVEALYLVCGERG" in reg_spec_str and "VEELYLVAGEEG" in reg_spec_str and "LVEALYLVCGERGG" in c3_regs_str and "SHLVEALYLVCGERG" in c3_regs_str and "VEELYLVAGEEG" in c3_regs_str ) # Exact AND conjunction + 6-level fail-first precedence + N_eff >= 60 + both nonzero holds in JSON blk_pass = str(ep_blk.get("pass_rule", "")).lower() blk_fail = str(ep_blk.get("fail_rule", "")).lower() blk_hold = str(ep_blk.get("hold_rule", "")).lower() pass_spec = ep_blk.get("pass_rule_spec") pass_spec_ok = ( isinstance(pass_spec, dict) and pass_spec.get("logical_operator") == "AND" and pass_spec.get("fail_logical_operator") == "OR" and pass_spec.get("suppression_estimand") == _SHIELD_SUPPRESSION_ESTIMAND and pass_spec.get("suppression_inference_method") == _SHIELD_SUPPRESSION_INFERENCE_METHOD and pass_spec.get("suppression_student_t_authorizing") is False and pass_spec.get("suppression_confirmatory_min_donors_n") == _SHIELD_MIN_CONFIRMATORY_DONORS_N and pass_spec.get("suppression_responder_l95_clopper_pearson_min_pct") == 70.0 and pass_spec.get("suppression_anti_enhancement_min_pct") == 0.0 and pass_spec.get("suppression_cohort_mean_min_pct") == 70.0 and pass_spec.get("observed_offtarget_apc_cytotoxicity_exact_pct") == 0.0 and pass_spec.get("offtarget_apc_cytotoxicity_u95_max_pct") == 5.0 and pass_spec.get("min_n_eff_per_donor") == 60 and pass_spec.get("fail_suppression_nonresponder_lt_40pct_l95_clopper_pearson_gt_pct") == 60.0 and pass_spec.get("fail_offtarget_apc_cytotoxicity_l95_gt_pct") == 5.0 and pass_spec.get("preregistered_donor_roster_mandatory") is True and pass_spec.get("per_donor_bonferroni_alpha_formula") == "0.05 / N_roster" and pass_spec.get("nonzero_u95_gt_5pct_hold_state") == "HOLD_CYTOTOXICITY_U95_EXCEEDS_5PCT" and pass_spec.get("nonzero_cytotoxicity_hold_state") == "HOLD_OBSERVED_NONZERO_CYTOTOXICITY" ) pass_prose_and_ok = ( bool( re.search( r"l95_cp\(k_ge_70,\s*n_roster\)\s*>=\s*70\.0%.*?\band\b.*?min_donor_suppression\s*>=\s*0\.0%.*?\band\b.*?0%\s+observed\s+off-target\s+apc\s+cytotoxicity\s+and\s+(?:off-target\s+apc\s+cytotoxicity\s+)?u95\s*<=\s*5\.0%", blk_pass, ) ) and "l95_t >=" not in blk_pass and not bool(re.search(r"\bor\b", blk_pass)) and bool( re.search( r"l95_cp\(k_lt_40,\s*n_roster\)\s*>\s*60\.0%.*\bor\b.*simultaneous_worst_donor_bonferroni_lower_pct\s*>\s*5\.0%", blk_fail, ) ) and "u95_t < 40.0%" not in blk_fail and "cohort_mean_cytotoxicity_l975_pct" not in blk_fail and _RE_MD_CONTRADICTORY_CYTO_FAIL.search(blk_fail) is None ) checks["sc_unified_cytotoxicity_pass_and_hold_tiebreak"] = (