{"record":{"id":"5f28678d9cc842da","repo":"can1357/oh-my-pi","slug":"union-intersection-invariant-failed","errorCode":null,"errorMessage":"union intersection invariant failed","messagePattern":"union intersection invariant failed","errorType":"exception","errorClass":"OmpTypeError","httpStatus":null,"severity":"error","filePath":"packages/omptype/src/type.ts","lineNumber":2026,"sourceCode":"\t\tif (a.fn !== b.fn || a.out !== b.out) {\n\t\t\tthrow new OmpTypeError(\"intersection of distinct morphs is indeterminate\");\n\t\t}\n\t\treturn { ...a, input: intersect(a.input, b.input) };\n\t}\n\tif (a.k === \"morph\") return { ...a, input: intersect(a.input, b) };\n\tif (b.k === \"morph\") return { ...b, input: intersect(a, b.input) };\n\tif (a.k === \"sub\" && a.schema.hasSteps) {\n\t\tif (b.k === \"sub\" && b.schema.hasSteps) {\n\t\t\tif (a.schema === b.schema) return a;\n\t\t\tthrow new OmpTypeError(\"intersection of distinct morphs is indeterminate\");\n\t\t}\n\t\tconst schema = a.schema as InternalType;\n\t\treturn embed(makeType(intersect(schema.ir, b), schema[kSteps], metaOf(schema)));\n\t}\n\tif (b.k === \"sub\" && b.schema.hasSteps) return intersect(b, a);\n\tif (a.k === \"union\" || b.k === \"union\") {\n\t\tconst union = a.k === \"union\" ? a : b.k === \"union\" ? b : undefined;\n\t\tif (union === undefined) throw new OmpTypeError(\"union intersection invariant failed\");\n\t\tconst branches = union.members;\n\t\tconst other = a.k === \"union\" ? b : a;\n\t\tconst members: IR[] = [];\n\t\tfor (const branch of branches) {\n\t\t\ttry {\n\t\t\t\tmembers.push(intersect(branch, other));\n\t\t\t} catch (error) {\n\t\t\t\tif (!(error instanceof OmpTypeError)) throw error;\n\t\t\t}\n\t\t}\n\t\tif (members.length === 0) throw new OmpTypeError(\"intersection has no satisfiable branches\");\n\t\treturn members.length === 1 ? members[0] : { k: \"union\", members };\n\t}\n\tif (a.k === \"lit\") {\n\t\tif (walk(b, a.v) instanceof OmpErrors) throw new OmpTypeError(\"literal is excluded by the intersection\");\n\t\treturn a;\n\t}\n\tif (b.k === \"lit\") return intersect(b, a);","sourceCodeStart":2008,"sourceCodeEnd":2044,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/omptype/src/type.ts#L2008-L2044","documentation":"In intersectResolved(), after detecting that at least one operand is a union, the code recomputes which operand was the union; if neither branch actually has k === 'union' despite the earlier guard, an internal invariant is broken and omptype throws. This is effectively an assertion — hitting it means corrupt or non-normalized IR reached the intersection (e.g. a hand-built IR or a failed alias resolution).","triggerScenarios":"Feeding a custom/foreign IR object into embed/intersect; an alias resolving to something mutated after construction; a bug or version mismatch inside omptype's IR normalization.","commonSituations":"Patching IR internals or monkey-patching schema objects; mixing omptype versions where IR shapes changed (e.g. two copies of the package in node_modules); deserializing schemas across versions.","solutions":["Deduplicate omptype so only one version exists (bun pm ls | grep omptype; fix duplicate installs)","Never construct or mutate IR objects directly — only use public builders","Rebuild the offending schema with current public API instead of copying cached/deserialized IR","If reproducible with public API only, report it as a bug with a minimal repro"],"exampleFix":"// before\n// manually mutated schema.ir to a union-like object\nschema.ir = { k: 'union', members: [...] }\n// after\nconst T = union(memberA, memberB) // build via API\nintersect(T, other)","handlingStrategy":"try-catch","validationCode":"// ensure operands come from the public API and one package version:\n// bun pm ls | grep omptype  → expect exactly one version\nfunction isWellFormedIR(ir) {\n  return ir && typeof ir.k === 'string' && (ir.k !== 'union' || Array.isArray(ir.members));\n}","typeGuard":"function isUnionIR(ir): ir is { k: 'union'; members: unknown[] } {\n  return typeof ir === 'object' && ir !== null && ir.k === 'union' && Array.isArray(ir.members);\n}","tryCatchPattern":"try {\n  return intersect(a, b);\n} catch (err) {\n  if (err instanceof OmpTypeError && err.message.includes('union intersection invariant')) {\n    logger.error('omptype IR invariant violated — likely version skew or manual IR mutation');\n    throw err; // do not swallow; fix the duplicate-install or rebuild the schema\n  }\n  throw err;\n}","preventionTips":["Never construct or mutate IR objects manually","Deduplicate omptype in node_modules to avoid mixed IR shapes","If it reproduces with only public API, file a bug with a minimal repro"],"tags":["omptype","union","intersection","internal-invariant"],"backgroundTag":"internal-invariant-violation","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}