Validate strict 1-to-1 lot_id -> mcb_id (and optional embedded wcb_id) manifest mapping. Returns (lot_to_mcb_or_None, embedded_lot_to_wcb, errors). """ errs: list[str] = [] if not isinstance(manifest_raw, dict) or len(manifest_raw) == 0: return None, {}, [f"{field_name} must be a non-empty dict mapping every observed lot_id uniquely to its single mcb_id."] out_mcb: dict[str, str] = {} out_wcb: dict[str, str] = {} for k, v in manifest_raw.items(): lid = _strict_literal_id(k) if lid is None: errs.append(f"{field_name} contains invalid lot_id key={k!r}.") continue if isinstance(v, str): mid = _strict_literal_id(v) if mid is None: errs.append(f"{field_name}[{lid!r}] has invalid mcb_id={v!r}.") continue out_mcb[lid] = mid elif isinstance(v, dict): extra_entry_keys = sorted(str(ek) for ek in v.keys() if ek not in GATE_28A_ALLOWED_MANIFEST_ENTRY_KEYS) if extra_entry_keys: errs.append( f"UNRECOGNIZED_BANK_MANIFEST_ENTRY_KEY: {field_name}[{lid!r}] contains unsupported key(s) {extra_entry_keys!r}." ) mid = _strict_literal_id(v.get("mcb_id")) if mid is None: errs.append(f"{field_name}[{lid!r}] missing valid literal mcb_id.") continue for mcb_alias in ("master_cell_bank_id", "bank_id"): if mcb_alias in v and v[mcb_alias] is not None: amid = _strict_literal_id(v[mcb_alias]) if amid is None or amid != mid: errs.append( f"CONFLICTING_MCB_MANIFEST_ALIAS: {field_name}[{lid!r}].{mcb_alias}={v[mcb_alias]!r} " f"contradicts mcb_id={mid!r}." ) wcb_raw = v.get("wcb_id") wcb_alias_raw = v.get("working_cell_bank_id") wid: str | None = None if wcb_raw is not None: wid = _strict_literal_id(wcb_raw) if wid is None: errs.append(f"{field_name}[{lid!r}] has invalid literal wcb_id={wcb_raw!r}.") continue if wcb_alias_raw is not None: waid = _strict_literal_id(wcb_alias_raw) if waid is None: errs.append(f"{field_name}[{lid!r}] has invalid literal working_cell_bank_id={wcb_alias_raw!r}.") continue if wid is not None and waid != wid: errs.append( f"CONFLICTING_WCB_MANIFEST_ALIAS: {field_name}[{lid!r}].working_cell_bank_id={waid!r} " f"contradicts wcb_id={wid!r}." ) else: wid = waid if wid is not None: out_wcb[lid] = wid out_mcb[lid] = mid else: errs.append(f"{field_name}[{lid!r}] must be a literal mcb_id string or dict with mcb_id.") if errs: return None, {}, errs return out_mcb, out_wcb, [] def _normalize_lot_to_wcb_manifest( wcb_manifest_raw: Any, ) -> tuple[dict[str, str] | None, list[str]]: """ Validate optional top-level lot_to_wcb_manifest mapping lot_id -> strict literal wcb_id. Returns (None, []) when omitted (None); returns (None, errs) if non-dict, empty, or invalid; returns (normalized_dict, []) when valid. """ if wcb_manifest_raw is None: return None, [] if not isinstance(wcb_manifest_raw, dict) or len(wcb_manifest_raw) == 0: return None, ["lot_to_wcb_manifest must be a non-empty dict mapping lot_id uniquely to its literal wcb_id when supplied."] errs: list[str] = [] out: dict[str, str] = {} for k, v in wcb_manifest_raw.items(): lid = _strict_literal_id(k) if lid is None: errs.append(f"lot_to_wcb_manifest contains invalid lot_id key={k!r}.") continue wid = _strict_literal_id(v) if wid is None: errs.append(f"lot_to_wcb_manifest[{lid!r}] has invalid literal wcb_id={v!r}.") continue out[lid] = wid if errs: return None, errs return out, [] def _merge_and_validate_bank_manifests( mcb_raw: Any, wcb_raw: Any, bank_alias_raw: Any = None, *, context: str, extra_container: dict[str, Any] | None = None, ) -> tuple[dict[str, str] | None, dict[str, str], bool, list[str]]: """ Unified manifest-alias normalizer and hierarchy validator across ALL supported manifest fields (lot_to_mcb_manifest, optional top-level lot_to_wcb_manifest, optional lot_to_bank_manifest alias, and detection of unrecognized extra manifest alias keys): 1. Rejects unrecognized extra manifest alias keys fail-closed (UNRECOGNIZED_BANK_MANIFEST_ALIAS). 2. Enforces exact agreement across overlapping MCB and WCB declarations BEFORE merging (CONFLICTING_BANK_MANIFEST_ALIASES / CONFLICTING_MCB_MANIFEST_ALIAS / CONFLICTING_WCB_MANIFEST_ALIAS). 3. Rejects widening or partial/extra lot or WCB claims present solely in lot_to_bank_manifest that contradict or widen canonical lot_to_mcb_manifest / lot_to_wcb_manifest. 4. Enforces strict global uniqueness of wcb_id -> mcb_id parentage across the COMBINED declarations from ALL aliases (every distinct literal wcb_id MUST map to at most ONE mcb_id; every lot with a WCB MUST have a known MCB parent). Returns (mcb_norm_or_None, effective_wcb_dict, any_wcb_present, errors). """ errs: list[str] = [] extra_aliases = _find_extra_manifest_alias_keys(extra_container) for ek in sorted(extra_aliases): errs.append( f"UNRECOGNIZED_BANK_MANIFEST_ALIAS: {context} contains unsupported manifest alias key {ek!r}; " "only canonical lot_to_mcb_manifest, lot_to_wcb_manifest, and exact-matching lot_to_bank_manifest are allowed." ) mcb_norm, emb_wcb, mcb_errs = _normalize_lot_to_mcb_manifest(mcb_raw, field_name="lot_to_mcb_manifest") errs.extend(mcb_errs) wcb_norm, wcb_errs = _normalize_lot_to_wcb_manifest(wcb_raw) errs.extend(wcb_errs) alt_mcb: dict[str, str] | None = None alt_wcb: dict[str, str] = {} if bank_alias_raw is not None: alt_mcb, alt_wcb, alt_errs = _normalize_lot_to_mcb_manifest(bank_alias_raw, field_name="lot_to_bank_manifest") errs.extend(alt_errs) any_wcb_present = bool(emb_wcb) or (wcb_raw is not None) or bool(alt_wcb) canonical_wcb: dict[str, str] = dict(emb_wcb) # Pre-merge alias conflict check: inspect BOTH embedded and top-level WCB maps BEFORE merging! if wcb_norm is not None: for lid in sorted(wcb_norm): wid = wcb_norm[lid] if lid in canonical_wcb and canonical_wcb[lid] != wid: errs.append( f"CONFLICTING_WCB_MANIFEST_ALIAS: {context} has contradictory wcb_id for lot_id={lid!r}: " f"embedded lot_to_mcb_manifest={canonical_wcb[lid]!r} vs top-level lot_to_wcb_manifest={wid!r}." ) else: canonical_wcb[lid] = wid effective_wcb: dict[str, str] = dict(canonical_wcb) # Unified lot_to_bank_manifest alias reconciliation: exact agreement on MCBs and zero widening/contradiction on WCBs! if bank_alias_raw is not None: if alt_mcb is not None and mcb_norm is not None: for lid in sorted(set(alt_mcb.keys()) & set(mcb_norm.keys())): if alt_mcb[lid] != mcb_norm[lid]: errs.append( f"CONFLICTING_BANK_MANIFEST_ALIASES / CONFLICTING_MCB_MANIFEST_ALIAS: {context} " f"lot_to_bank_manifest[{lid!r}]={alt_mcb[lid]!r} contradicts lot_to_mcb_manifest[{lid!r}]={mcb_norm[lid]!r}." ) extra_mcb_lots = sorted(set(alt_mcb.keys()) - set(mcb_norm.keys())) missing_mcb_lots = sorted(set(mcb_norm.keys()) - set(alt_mcb.keys())) if extra_mcb_lots or missing_mcb_lots: errs.append( f"CONFLICTING_BANK_MANIFEST_ALIASES: {context} lot_to_bank_manifest lot set contradicts or widens " f"lot_to_mcb_manifest (extra={extra_mcb_lots!r}, missing={missing_mcb_lots!r})." ) if alt_wcb: for lid_a in sorted(alt_wcb): wid_a = alt_wcb[lid_a] if lid_a in canonical_wcb and canonical_wcb[lid_a] != wid_a: errs.append( f"CONFLICTING_BANK_MANIFEST_ALIASES / CONFLICTING_WCB_MANIFEST_ALIAS: {context} " f"lot_to_bank_manifest[{lid_a!r}] wcb_id={wid_a!r} contradicts canonical WCB={canonical_wcb[lid_a]!r}." ) elif lid_a not in canonical_wcb: errs.append( f"CONFLICTING_BANK_MANIFEST_ALIASES / CONFLICTING_WCB_MANIFEST_ALIAS: {context} " f"lot_to_bank_manifest declares extra WCB entry lot_id={lid_a!r} -> wcb_id={wid_a!r} " "absent from canonical lot_to_mcb_manifest / lot_to_wcb_manifest." ) effective_wcb[lid_a] = wid_a omitted_wcb_lots = sorted(set(canonical_wcb.keys()) - set(alt_wcb.keys())) if omitted_wcb_lots: errs.append( f"CONFLICTING_BANK_MANIFEST_ALIASES / CONFLICTING_WCB_MANIFEST_ALIAS: {context} " f"lot_to_bank_manifest declares partial WCB entries omitting canonical WCB lots {omitted_wcb_lots!r}." ) # Combined global WCB-to-MCB parentage uniqueness across ALL aliases (lot_to_mcb_manifest, # lot_to_wcb_manifest, AND lot_to_bank_manifest): every distinct literal wcb_id MUST descend # from at most ONE mcb_id across the combined declarations! combined_lot_to_mcbs: dict[str, set[str]] = {} for m_src in (mcb_norm, alt_mcb): if m_src is not None: for lid, mid in sorted(m_src.items()): combined_lot_to_mcbs.setdefault(lid, set()).add(mid) if combined_lot_to_mcbs and (emb_wcb or wcb_norm or alt_wcb): wcb_to_mcbs: dict[str, set[str]] = {} seen_pairs: set[tuple[str, str]] = set() for src_map in (emb_wcb, wcb_norm or {}, alt_wcb): for lid, wid in sorted(src_map.items()): if (lid, wid) in seen_pairs: continue seen_pairs.add((lid, wid)) if lid not in combined_lot_to_mcbs: errs.append( f"WCB_ORPHAN_LOT_WITHOUT_MCB_PARENT: {context} assigns wcb_id={wid!r} to lot_id={lid!r}, " "which is absent from lot_to_mcb_manifest." ) else: wcb_to_mcbs.setdefault(wid, set()).update(combined_lot_to_mcbs[lid]) for wid, mcbs in sorted(wcb_to_mcbs.items()): if len(mcbs) > 1: errs.append( f"NON_UNIQUE_WCB_PARENTAGE: {context} assigns literal wcb_id={wid!r} across multiple distinct " f"MCBs {sorted(mcbs)} in combined manifest declarations (each distinct literal wcb_id must map to at most ONE mcb_id)." ) return mcb_norm, effective_wcb, any_wcb_present, errs _GATE28A_UNSET = object() def _canonicalize_gate28a_value(val: Any) -> Any: """Injectively canonicalize values for Gate 28-A digest computation (equates int/float/Decimal numerically while preserving exact type distinctions across bool/None/str/num/dict/list).""" if type(val) is bool: return ["bool", val] if val is None: return ["null"] if isinstance(val, str): return ["str", val] if isinstance(val, (int, float, Decimal, Fraction)): d = _strict_decimal_in_range(val) if d is not None: return ["num", str(Fraction(d))] return ["nonfinite_num", type(val).__name__, repr(val)] if isinstance(val, dict): return [ "dict", [[str(k), _canonicalize_gate28a_value(v)] for k, v in sorted(val.items(), key=lambda kv: str(kv[0]))], ] if isinstance(val, list): return ["list", [_canonicalize_gate28a_value(x) for x in val]]