} min_obs = {k: v for k, v in safe_obs.items() if k in s9_only_keys} return evaluate_stage0_decision_tree(min_obs, root=root_path), True def _pick_headline_s11_fail_iip0031(fail_codes: list[str] | set[str]) -> str: """Deterministic S11 headline failure selector: substantive stops outrank schema-only unrecognized-key errors.""" substantive = sorted(r for r in fail_codes if r != 'FAIL_S11_UNRECOGNIZED_RECORD_KEYS') if substantive: return substantive[0] return sorted(fail_codes)[0] def _evaluate_all_s11_containers_iip0031( base_env: dict[str, Any], containers: tuple[dict[str, Any], ...] | list[dict[str, Any]], *, root_path: Path, product_def: dict[str, Any] | None = None, ) -> dict[str, Any]: """ Evaluate both nested `iip0031_locus2_record` panels AND flat Rule S11 observations across all supported Stage-0 containers (`top`, `scope`, `iip0026_molecular_observations`, `measurements`) without letting a `None`, malformed, or conflicting sibling erase an independently attributable `FAIL_S11_*` stop (Findings #3, #16, #46; Patch v11). """ env_clean = { k: v for k, v in base_env.items() if isinstance(k, str) and k not in ('scope', 'measurements', 'iip0026_molecular_observations', 'iip0031_locus2_record') } present_vals: list[Any] = [ c['iip0031_locus2_record'] for c in containers if isinstance(c, dict) and 'iip0031_locus2_record' in c ] _s11_rep_alias_keys = ('obs_stage0_independent_lot_replicates_n', 'replicates_n', 's11_replicate_n') def _is_s11_domain_key(k: str) -> bool: return bool( k.startswith(('obs_l2_v3_', 'obs_ciita_', 'obs_edited_72h_', 'obs_wt_lineage_', 'obs_cis_', 'obs_16p13_')) or k in _ALL_S11_RECOGNIZED_EVIDENCE_KEYS or k in ('l2_v3_donor_id', 'donor_id') ) def _is_s11_flat_key(k: str) -> bool: return bool( _is_s11_domain_key(k) or k in _s11_rep_alias_keys or k == 'background_id' ) flat_s11_keys: list[str] = [] seen_flat_k: set[str] = set() any_s11_domain_key_present = False for c in containers: if not isinstance(c, dict): continue for k in c: if not isinstance(k, str) or k in seen_flat_k: continue if _is_s11_flat_key(k): seen_flat_k.add(k) flat_s11_keys.append(k) if _is_s11_domain_key(k): any_s11_domain_key_present = True flat_s11_conflicted_keys: set[str] = set() for k in flat_s11_keys: vals = [c[k] for c in containers if isinstance(c, dict) and k in c] has_null = any(v is None or v == 'UNAVAILABLE' for v in vals) non_null = [v for v in vals if v is not None and v != 'UNAVAILABLE'] seen_sigs: set[str] = set() malformed_val = False for v in non_null: if _is_strict_int_iip0024(v) and abs(v).bit_length() > 53: malformed_val = True continue try: sv = json.dumps(v, sort_keys=True, separators=(',', ':'), allow_nan=False) except (TypeError, ValueError, OverflowError, RecursionError): malformed_val = True continue seen_sigs.add(sv) if malformed_val or (has_null and bool(non_null)) or len(seen_sigs) > 1: flat_s11_conflicted_keys.add(k) rep_vals_all = [ c[rk] for c in containers if isinstance(c, dict) for rk in _s11_rep_alias_keys if rk in c ] rep_has_null = any(v is None or v == 'UNAVAILABLE' for v in rep_vals_all) rep_non_null = [v for v in rep_vals_all if v is not None and v != 'UNAVAILABLE'] rep_sigs: set[str] = set() rep_malformed = False for rv in rep_non_null: if not _is_strict_int_iip0024(rv) or abs(rv).bit_length() > 53: rep_malformed = True continue rep_sigs.add(str(rv)) s11_rep_cross_conflict = bool( rep_vals_all and (rep_malformed or (rep_has_null and bool(rep_non_null)) or len(rep_sigs) > 1) ) is_l2_product_def = bool( isinstance(product_def, dict) and isinstance(product_def.get('staged_locus2_modeb_donor_binding'), dict) ) has_flat_s11_domain_conflict = any(_is_s11_domain_key(k) for k in flat_s11_conflicted_keys) has_flat_s11_conflict = bool( has_flat_s11_domain_conflict or (s11_rep_cross_conflict and (is_l2_product_def or any_s11_domain_key_present or bool(present_vals))) or ('background_id' in flat_s11_conflicted_keys and (is_l2_product_def or any_s11_domain_key_present or bool(present_vals))) ) flat_probe_envs: list[dict[str, Any]] = [] if has_flat_s11_conflict: for c in containers: if not isinstance(c, dict): continue cand = dict(env_clean) c_contributed = False for k in flat_s11_conflicted_keys: if k in c and c[k] is not None and c[k] != 'UNAVAILABLE': if _is_strict_int_iip0024(c[k]) and abs(c[k]).bit_length() > 53: continue cand[k] = c[k] c_contributed = True c_rep_keys = [rk for rk in _s11_rep_alias_keys if rk in c and c[rk] is not None and c[rk] != 'UNAVAILABLE'] if s11_rep_cross_conflict and c_rep_keys: for rk in _s11_rep_alias_keys: cand.pop(rk, None) for rk in c_rep_keys: if not (_is_strict_int_iip0024(c[rk]) and abs(c[rk]).bit_length() > 53): cand[rk] = c[rk] c_contributed = True if not c_contributed: continue # Never cross-breed mean from container A with SE from container B when means disagree: for mk, sk in _S11_MEAN_SE_PAIRS_IIP0031: if mk not in flat_s11_conflicted_keys and sk not in flat_s11_conflicted_keys: continue if mk in cand and sk not in c: if _is_strict_finite_number_iip0024(cand[mk]): m_f = float(cand[mk]) matching_ses = [ c_o[sk] for c_o in containers if isinstance(c_o, dict) and mk in c_o and _is_strict_finite_number_iip0024(c_o[mk]) and float(c_o[mk]) == m_f and sk in c_o and _is_strict_finite_number_iip0024(c_o[sk]) and float(c_o[sk]) > 0.0 ] mean_free_ses = [ c_o[sk] for c_o in containers if isinstance(c_o, dict) and mk not in c_o and sk in c_o and _is_strict_finite_number_iip0024(c_o[sk]) and float(c_o[sk]) > 0.0 ] if matching_ses: cand[sk] = max(matching_ses, key=float) elif mean_free_ses: cand[sk] = max(mean_free_ses, key=float) else: cand.pop(sk, None) else: cand.pop(sk, None) elif sk in cand and mk not in cand and mk in flat_s11_conflicted_keys: cand.pop(sk, None) flat_probe_envs.append(cand) if not present_vals and not flat_probe_envs: return evaluate_locus2_ciita_modeb_challenger_v3_gate(env_clean, root=root_path, product_def=product_def) has_null_or_non_dict = any(v is None or not isinstance(v, dict) for v in present_vals) dict_panels: list[dict[str, Any]] = [] seen_panel_sigs: set[str] = set() for v in present_vals: if isinstance(v, dict): try: sig = json.dumps(v, sort_keys=True, separators=(',', ':'), allow_nan=False) except (TypeError, ValueError, OverflowError, RecursionError): has_null_or_non_dict = True dict_panels.append(v) continue if sig not in seen_panel_sigs: seen_panel_sigs.add(sig) dict_panels.append(v) has_conflicting_dicts = len(dict_panels) > 1 evals: list[dict[str, Any]] = [] if dict_panels: for dp in dict_panels: evals.append( evaluate_locus2_ciita_modeb_challenger_v3_gate( {**env_clean, 'iip0031_locus2_record': dp}, root=root_path, product_def=product_def, ) ) for f_env in flat_probe_envs: evals.append( evaluate_locus2_ciita_modeb_challenger_v3_gate( {**f_env, 'iip0031_locus2_record': dp}, root=root_path, product_def=product_def, ) ) elif present_vals: # Only null / non-dict values supplied: still probe flat S11 fields for architectural/quantitative stops, # then enforce HOLD_S11_MALFORMED_RECORD if no FAIL_S11_* fired! flat_ev = evaluate_locus2_ciita_modeb_challenger_v3_gate( {**env_clean, 'iip0031_locus2_record': '__MALFORMED_NON_DICT__'}, root=root_path, product_def=product_def, ) flat_only_ev = evaluate_locus2_ciita_modeb_challenger_v3_gate( env_clean, root=root_path, product_def=product_def, ) evals.extend((flat_only_ev, flat_ev)) else: evals.append( evaluate_locus2_ciita_modeb_challenger_v3_gate( env_clean, root=root_path, product_def=product_def, ) ) for f_env in flat_probe_envs: evals.append( evaluate_locus2_ciita_modeb_challenger_v3_gate( f_env, root=root_path, product_def=product_def, ) ) all_fails: list[str] = [] all_holds: list[str] = []