@aburi/diff 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../src/components.ts","../src/errors.ts","../src/delta.ts","../src/signature.ts","../src/similarity.ts","../src/match.ts","../src/status.ts","../src/diff.ts"],"sourcesContent":["import type { Component, ComponentDiff, Dependency, DependencyDiff } from \"@aburi/types\"\n\n/**\n * §6.1 — Component diff. Component identity is `id`; when a Component is present in both\n * base and head with the same id, the three delta booleans describe which axes actually\n * moved (roots reshuffle, public API globs, or framework hint list). `modified` deltas\n * are intentionally absent from this diff (§5.2.3): fields are reported as before/after\n * pairs via the `changed[]` entries so consumers can render them without stringifying\n * arrays.\n */\nexport function diffComponents(\n base: readonly Component[],\n head: readonly Component[],\n): ComponentDiff {\n const baseById = new Map<string, Component>()\n for (const c of base) baseById.set(c.id, c)\n const headById = new Map<string, Component>()\n for (const c of head) headById.set(c.id, c)\n\n const added: Component[] = []\n const removed: Component[] = []\n const changed: ComponentDiff[\"changed\"] = []\n\n for (const [id, headComp] of headById) {\n const baseComp = baseById.get(id)\n if (baseComp === undefined) {\n added.push(headComp)\n continue\n }\n const rootsChanged = !stringArraysEqual(baseComp.roots, headComp.roots)\n const publicApiChanged = !stringArraysEqual(baseComp.publicApi ?? [], headComp.publicApi ?? [])\n const frameworksChanged = !stringArraysEqual(\n baseComp.frameworks ?? [],\n headComp.frameworks ?? [],\n )\n if (rootsChanged || publicApiChanged || frameworksChanged) {\n changed.push({\n before: baseComp,\n after: headComp,\n delta: { rootsChanged, publicApiChanged, frameworksChanged },\n })\n }\n }\n for (const [id, baseComp] of baseById) {\n if (!headById.has(id)) removed.push(baseComp)\n }\n sortById(added)\n sortById(removed)\n changed.sort((a, b) => (a.after.id < b.after.id ? -1 : a.after.id > b.after.id ? 1 : 0))\n return { added, removed, changed }\n}\n\n/**\n * §6.2 — Dependency diff. Identity is the composite `(from, to, via)` triple; direction\n * and effect changes are surfaced as an added + removed pair so `modified` is not part of\n * the schema (§6.2 tail).\n */\nexport function diffDependencies(\n base: readonly Dependency[],\n head: readonly Dependency[],\n): DependencyDiff {\n const baseKeys = new Map<string, Dependency>()\n for (const d of base) baseKeys.set(dependencyKey(d), d)\n const headKeys = new Map<string, Dependency>()\n for (const d of head) headKeys.set(dependencyKey(d), d)\n const added: Dependency[] = []\n const removed: Dependency[] = []\n for (const [key, dep] of headKeys) {\n const b = baseKeys.get(key)\n if (b === undefined) {\n added.push(dep)\n continue\n }\n if (b.direction !== dep.direction || (b.effect ?? null) !== (dep.effect ?? null)) {\n removed.push(b)\n added.push(dep)\n }\n }\n for (const [key, dep] of baseKeys) {\n if (!headKeys.has(key)) removed.push(dep)\n }\n added.sort(compareDependencies)\n removed.sort(compareDependencies)\n return { added, removed }\n}\n\nfunction dependencyKey(d: Dependency): string {\n return `${d.from}::${d.to}::${d.via}`\n}\n\nfunction compareDependencies(a: Dependency, b: Dependency): number {\n const keyA = dependencyKey(a)\n const keyB = dependencyKey(b)\n return keyA < keyB ? -1 : keyA > keyB ? 1 : 0\n}\n\nfunction stringArraysEqual(a: readonly string[], b: readonly string[]): boolean {\n if (a.length !== b.length) return false\n for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false\n return true\n}\n\nfunction sortById<T extends { id: string }>(items: T[]): void {\n items.sort((a, b) => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0))\n}\n","/**\n * Coded error class for every @aburi/diff failure. Consumers branch on `code` without\n * parsing message text. Codes are stable and additions are non-breaking; renames are not.\n */\n\nexport type DiffErrorCode =\n /** `base.$schema` and `head.$schema` disagree; a diff across schema versions cannot be trusted (§9.1). */\n | \"schema-mismatch\"\n /** `config.diff.lineFuzz` was outside the documented [0, 10] range (diff-algorithm.md §5.2.1). */\n | \"invalid-line-fuzz\"\n /**\n * `baseIR` or `headIR` failed the top-level shape check that `buildDiff` enforces\n * before touching any Symbol. Missing / non-array `symbols`, `components`,\n * `dependencies`, or an absent `$schema` — anything that would otherwise crash with a\n * cryptic `TypeError: undefined is not iterable` deep in a matching stage.\n */\n | \"ir-shape-invalid\"\n\nexport interface DiffErrorDetail {\n code: DiffErrorCode\n /** Offending value (schema id, fuzz value, etc.) when applicable. */\n value?: string\n}\n\nexport class DiffError extends Error {\n readonly code: DiffErrorCode\n readonly value: string | undefined\n\n constructor(message: string, detail: DiffErrorDetail, options?: { cause?: unknown }) {\n super(message, options)\n this.name = \"DiffError\"\n this.code = detail.code\n this.value = detail.value\n }\n}\n","import type {\n ArrayDelta,\n Call,\n Decorator,\n Effect,\n Symbol as IRSymbol,\n Rule,\n Signature,\n SignatureDelta,\n SymbolDelta,\n} from \"@aburi/types\"\nimport { DiffError } from \"./errors\"\n\nexport const DEFAULT_LINE_FUZZ = 2\nexport const MAX_LINE_FUZZ = 10\nexport const MIN_LINE_FUZZ = 0\n\nexport interface DeltaOptions {\n /**\n * §5.2.1 line fuzz for rule/call identity. Must be an integer in\n * `[MIN_LINE_FUZZ, MAX_LINE_FUZZ]` (`0..10`); anything outside — or a non-finite value —\n * throws `DiffError({ code: \"invalid-line-fuzz\" })`. Setting `0` disables fuzz; omitting\n * the field falls back to `DEFAULT_LINE_FUZZ` (2).\n */\n lineFuzz?: number\n}\n\n/**\n * §5 — Compute the full per-Symbol delta between two paired Symbols. The three axis\n * booleans (`apiChanged` / `logicChanged` / `syntaxChanged`) come from fingerprint\n * comparison; the array deltas (`rules` / `effects` / `calls` / `decorators`) come from\n * identity-preserving diff with configurable line fuzz.\n */\nexport function computeSymbolDelta(\n base: IRSymbol,\n head: IRSymbol,\n options: DeltaOptions = {},\n): SymbolDelta {\n const fuzz = validateLineFuzz(options.lineFuzz ?? DEFAULT_LINE_FUZZ)\n const delta: SymbolDelta = {\n apiChanged: base.fingerprint.api !== head.fingerprint.api,\n logicChanged: base.fingerprint.logic !== head.fingerprint.logic,\n syntaxChanged: base.fingerprint.syntax !== head.fingerprint.syntax,\n componentChanged: (base.component ?? null) !== (head.component ?? null),\n visibilityChanged: base.visibility !== head.visibility,\n }\n delta.rules = diffRules(base.rules, head.rules, fuzz)\n delta.effects = diffEffects(base.effects, head.effects)\n delta.calls = diffCalls(base.calls, head.calls, fuzz)\n delta.decorators = diffDecorators(base.decorators, head.decorators, fuzz)\n const sigDelta = diffSignature(base.signature ?? null, head.signature ?? null)\n delta.signature = sigDelta\n return delta\n}\n\n/**\n * §5.2.1 range check. Loud on violation rather than silently clamping so a config typo\n * (`lineFuzz: 999`) or an upstream numeric bug (`NaN`, `Infinity`) surfaces at the diff\n * boundary instead of quietly rounding into a range that produces the wrong deltas.\n */\nfunction validateLineFuzz(value: number): number {\n if (!Number.isFinite(value) || !Number.isInteger(value)) {\n throw new DiffError(\n `config.diff.lineFuzz must be an integer in [${MIN_LINE_FUZZ}, ${MAX_LINE_FUZZ}]; got ${String(value)}.`,\n { code: \"invalid-line-fuzz\", value: String(value) },\n )\n }\n if (value < MIN_LINE_FUZZ || value > MAX_LINE_FUZZ) {\n throw new DiffError(\n `config.diff.lineFuzz must be within [${MIN_LINE_FUZZ}, ${MAX_LINE_FUZZ}]; got ${value}.`,\n { code: \"invalid-line-fuzz\", value: String(value) },\n )\n }\n return value\n}\n\ninterface Identified<T> {\n item: T\n key: string\n line: number\n}\n\n/**\n * Two-index diff: build key-indexed maps for both sides, then classify each key into\n * `added` / `removed` / `modified`. Line-fuzzed identities are matched within tolerance\n * so cosmetic line shifts do not produce added/removed noise. `modified` only fires when\n * the identity key stays the same but the content differs.\n */\nfunction differentiate<T>(\n base: readonly Identified<T>[],\n head: readonly Identified<T>[],\n isEqual: (a: T, b: T) => boolean,\n lineFuzz: number,\n): ArrayDelta {\n const added: T[] = []\n const removed: T[] = []\n const modified: T[] = []\n const consumedBase = new Set<number>()\n for (const h of head) {\n let matchIdx = -1\n for (let j = 0; j < base.length; j++) {\n if (consumedBase.has(j)) continue\n const b = base[j]\n if (b === undefined) continue\n if (b.key !== h.key) continue\n if (Math.abs(b.line - h.line) > lineFuzz) continue\n matchIdx = j\n break\n }\n if (matchIdx === -1) {\n added.push(h.item)\n continue\n }\n consumedBase.add(matchIdx)\n const bmatch = base[matchIdx]\n if (bmatch === undefined) continue\n if (!isEqual(bmatch.item, h.item)) modified.push(h.item)\n }\n for (let j = 0; j < base.length; j++) {\n if (consumedBase.has(j)) continue\n const b = base[j]\n if (b === undefined) continue\n removed.push(b.item)\n }\n return { added, removed, modified }\n}\n\nfunction diffRules(base: readonly Rule[], head: readonly Rule[], lineFuzz: number): ArrayDelta {\n const mapper = (r: Rule): Identified<Rule> => ({\n item: r,\n key: r.type,\n line: r.line,\n })\n return differentiate(base.map(mapper), head.map(mapper), rulesEqual, lineFuzz)\n}\n\nfunction rulesEqual(a: Rule, b: Rule): boolean {\n return (\n a.type === b.type &&\n (a.condition ?? null) === (b.condition ?? null) &&\n (a.what ?? null) === (b.what ?? null) &&\n (a.expr ?? null) === (b.expr ?? null) &&\n (a.loopKind ?? null) === (b.loopKind ?? null)\n )\n}\n\nfunction diffEffects(base: readonly Effect[], head: readonly Effect[]): ArrayDelta {\n const mapper = (e: Effect): Identified<Effect> => ({\n item: e,\n key: `${e.id}::${e.target}`,\n line: e.line,\n })\n return differentiate(base.map(mapper), head.map(mapper), effectsEqual, Number.POSITIVE_INFINITY)\n}\n\nfunction effectsEqual(a: Effect, b: Effect): boolean {\n return (\n a.id === b.id && a.target === b.target && a.plugin === b.plugin && a.confidence === b.confidence\n )\n}\n\nfunction diffCalls(base: readonly Call[], head: readonly Call[], lineFuzz: number): ArrayDelta {\n const mapper = (c: Call): Identified<Call> => ({\n item: c,\n key: c.target,\n line: c.line,\n })\n return differentiate(base.map(mapper), head.map(mapper), callsEqual, lineFuzz)\n}\n\nfunction callsEqual(a: Call, b: Call): boolean {\n return a.target === b.target && (a.resolved ?? null) === (b.resolved ?? null)\n}\n\n/**\n * §5.2.2 — decorator diff. Identity is `name`; when the same-named decorator is present\n * on both sides within the line-fuzz window, the argument list determines whether the\n * entry is a modified pair or an implicit no-op.\n */\nfunction diffDecorators(\n base: readonly Decorator[],\n head: readonly Decorator[],\n lineFuzz: number,\n): ArrayDelta {\n const mapper = (d: Decorator): Identified<Decorator> => ({\n item: d,\n key: d.name,\n line: d.line,\n })\n return differentiate(base.map(mapper), head.map(mapper), decoratorsEqual, lineFuzz)\n}\n\nfunction decoratorsEqual(a: Decorator, b: Decorator): boolean {\n if (a.name !== b.name) return false\n if (a.arguments.length !== b.arguments.length) return false\n for (let i = 0; i < a.arguments.length; i++) {\n if (a.arguments[i] !== b.arguments[i]) return false\n }\n return true\n}\n\n/**\n * §5.3 — signature delta. Three cases:\n * - both sides `null` → `null` (nothing to compare)\n * - exactly one side `null` → the present side is emitted verbatim as\n * `added` (when only head has a signature) or `removed` (when only base has one);\n * the three axis booleans compare the missing side against `false` defaults\n * - both sides non-null → per-list sub-deltas:\n * - `inputs`: strict positional compare via `differentiate(..., lineFuzz: 0)` — index\n * used as the line key so ordered parameter lists match position-for-position\n * - `outputs`: positional compare (added/removed emitted when the arrays diverge at\n * an index), no `modified` category\n * - `throws`: unordered set diff — duplicates collapse per Set semantics\n * - `async` / `generator` / `typeParameters` change flags follow the raw fields\n */\nfunction diffSignature(base: Signature | null, head: Signature | null): SignatureDelta | null {\n if (base === null && head === null) return null\n if (base === null || head === null) {\n const empty: ArrayDelta = { added: [], removed: [], modified: [] }\n const present = base ?? head\n if (present === null) return null\n const isBaseNull = base === null\n const inputs: ArrayDelta = isBaseNull\n ? { added: [...present.inputs], removed: [], modified: [] }\n : { added: [], removed: [...present.inputs], modified: [] }\n const outputs: ArrayDelta = isBaseNull\n ? { added: [...present.outputs], removed: [], modified: [] }\n : { added: [], removed: [...present.outputs], modified: [] }\n const throws: ArrayDelta = isBaseNull\n ? { added: [...present.throws], removed: [], modified: [] }\n : { added: [], removed: [...present.throws], modified: [] }\n return {\n inputs,\n outputs,\n throws,\n asyncChanged: (base?.async ?? false) !== (head?.async ?? false),\n generatorChanged: (base?.generator ?? false) !== (head?.generator ?? false),\n typeParametersChanged: !arraysEqual(\n base?.typeParameters ?? empty.added.map(String),\n head?.typeParameters ?? empty.added.map(String),\n ),\n }\n }\n const inputMapper = (\n input: { name: string; type: string },\n index: number,\n ): Identified<{ name: string; type: string }> => ({\n item: input,\n key: `${index}:${input.name}`,\n line: index,\n })\n const inputs = differentiate(\n base.inputs.map(inputMapper),\n head.inputs.map(inputMapper),\n (a, b) => a.name === b.name && a.type === b.type,\n 0,\n )\n const outputs = diffStringList(base.outputs, head.outputs)\n const throws = diffStringSet(base.throws, head.throws)\n return {\n inputs,\n outputs,\n throws,\n asyncChanged: base.async !== head.async,\n generatorChanged: base.generator !== head.generator,\n typeParametersChanged: !arraysEqual(base.typeParameters, head.typeParameters),\n }\n}\n\nfunction diffStringList(base: readonly string[], head: readonly string[]): ArrayDelta {\n const added: string[] = []\n const removed: string[] = []\n const max = Math.max(base.length, head.length)\n for (let i = 0; i < max; i++) {\n const b = i < base.length ? base[i] : undefined\n const h = i < head.length ? head[i] : undefined\n if (b === undefined && h !== undefined) added.push(h)\n else if (h === undefined && b !== undefined) removed.push(b)\n else if (b !== h) {\n if (b !== undefined) removed.push(b)\n if (h !== undefined) added.push(h)\n }\n }\n return { added, removed, modified: [] }\n}\n\nfunction diffStringSet(base: readonly string[], head: readonly string[]): ArrayDelta {\n const setB = new Set(base)\n const setH = new Set(head)\n const added: string[] = []\n const removed: string[] = []\n for (const h of setH) if (!setB.has(h)) added.push(h)\n for (const b of setB) if (!setH.has(b)) removed.push(b)\n return { added, removed, modified: [] }\n}\n\nfunction arraysEqual(a: readonly string[], b: readonly string[]): boolean {\n if (a.length !== b.length) return false\n for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false\n return true\n}\n","import type { Signature } from \"@aburi/types\"\n\n/**\n * §3.4.2 signatureSimilarity — averaged type-similarity across `inputs`, `outputs`, and\n * `throws`. Follows diff-algorithm.md §3.4.2:\n * - null + null → 1.0 (both symbols are signature-less; nothing to compare)\n * - null + non-null (either side) → 0.0\n * - both non-null: mean of three subscores\n *\n * Subscores:\n * - inputs: ordered type-string equality rate (`i-th type equal` counts as 1, else 0)\n * - outputs: same shape, but outputs is an array of type strings\n * - throws: unordered set intersection over union\n *\n * Empty subarrays on both sides count as 1.0 (they are trivially equal); on one side only\n * they count as 0.0 (asymmetric information).\n */\nexport function signatureSimilarity(\n base: Signature | null | undefined,\n head: Signature | null | undefined,\n): number {\n const baseSig = base ?? null\n const headSig = head ?? null\n if (baseSig === null && headSig === null) return 1\n if (baseSig === null || headSig === null) return 0\n const inputsScore = compareOrderedTypes(\n baseSig.inputs.map((i) => i.type),\n headSig.inputs.map((i) => i.type),\n )\n const outputsScore = compareOrderedTypes(baseSig.outputs, headSig.outputs)\n const throwsScore = compareUnorderedSet(baseSig.throws, headSig.throws)\n return (inputsScore + outputsScore + throwsScore) / 3\n}\n\nfunction compareOrderedTypes(a: readonly string[], b: readonly string[]): number {\n if (a.length === 0 && b.length === 0) return 1\n if (a.length !== b.length) return 0\n let matches = 0\n for (let i = 0; i < a.length; i++) {\n if (a[i] === b[i]) matches++\n }\n return matches / a.length\n}\n\nfunction compareUnorderedSet(a: readonly string[], b: readonly string[]): number {\n if (a.length === 0 && b.length === 0) return 1\n const setA = new Set(a)\n const setB = new Set(b)\n let intersection = 0\n for (const t of setA) if (setB.has(t)) intersection++\n const union = setA.size + setB.size - intersection\n return union === 0 ? 1 : intersection / union\n}\n","/**\n * String similarity utilities dedicated to the diff engine. Kept in one file so the\n * name/signature/owner formulas are auditable side by side against diff-algorithm.md §3.4.\n */\n\n/**\n * Tokenise a qualified name into normalised lowercase word segments. Splits on:\n * - camelCase / PascalCase boundaries (`fooBar` → `foo`, `Bar`)\n * - snake_case underscores (`foo_bar` → `foo`, `bar`)\n * - dotted namespaces (`ns.Class` → `ns`, `Class`)\n * - `::` static-method separator (`Class::method` → `Class`, `method`)\n * - runs of digits (`get2Users` → `get`, `2`, `Users`)\n *\n * The result is de-duplicated so `InvoiceService.createInvoice` collapses to\n * `[\"invoice\", \"service\", \"create\"]` — Jaccard similarity relies on set semantics, so\n * repeated tokens must not double-count.\n */\nexport function tokenizeName(input: string): string[] {\n const seen = new Set<string>()\n const out: string[] = []\n for (const raw of input.split(/[.:_\\-\\s/]+/)) {\n if (raw.length === 0) continue\n for (const piece of splitCamel(raw)) {\n const norm = piece.toLowerCase()\n if (norm.length === 0 || seen.has(norm)) continue\n seen.add(norm)\n out.push(norm)\n }\n }\n return out\n}\n\nfunction splitCamel(word: string): string[] {\n const chunks: string[] = []\n let current = \"\"\n for (const ch of word) {\n if (current === \"\") {\n current = ch\n continue\n }\n const prev = current.slice(-1)\n const boundary = isCamelBoundary(prev, ch)\n if (boundary) {\n chunks.push(current)\n current = ch\n } else {\n current += ch\n }\n }\n if (current.length > 0) chunks.push(current)\n return chunks\n}\n\nfunction isCamelBoundary(prev: string, curr: string): boolean {\n const prevLower = prev >= \"a\" && prev <= \"z\"\n const prevUpper = prev >= \"A\" && prev <= \"Z\"\n const prevDigit = prev >= \"0\" && prev <= \"9\"\n const currLower = curr >= \"a\" && curr <= \"z\"\n const currUpper = curr >= \"A\" && curr <= \"Z\"\n const currDigit = curr >= \"0\" && curr <= \"9\"\n if (prevLower && currUpper) return true\n if ((prevLower || prevUpper) && currDigit) return true\n if (prevDigit && (currLower || currUpper)) return true\n return false\n}\n\n/**\n * Jaccard similarity between two token multisets. Formula: |A ∩ B| / |A ∪ B|.\n * Empty on both sides returns 1.0 (both are \"no tokens\", which are equivalent for the\n * purpose of §3.4.1). Empty on one side only returns 0.0.\n */\nexport function jaccard(a: readonly string[], b: readonly string[]): number {\n if (a.length === 0 && b.length === 0) return 1\n if (a.length === 0 || b.length === 0) return 0\n const setA = new Set(a)\n const setB = new Set(b)\n let intersection = 0\n for (const t of setA) if (setB.has(t)) intersection++\n const union = setA.size + setB.size - intersection\n return intersection / union\n}\n\n/** name-token Jaccard shortcut used by nameSimilarity and ownerSimilarity call sites. */\nexport function jaccardTokens(a: string, b: string): number {\n return jaccard(tokenizeName(a), tokenizeName(b))\n}\n\n/**\n * §3.4.1 — Jaccard over the tokens of the full qualified name. The last segment is not\n * given extra weight; the entire path contributes uniformly because the intersection is\n * what the score should reward.\n */\nexport function nameSimilarity(baseName: string, headName: string): number {\n return jaccardTokens(baseName, headName)\n}\n\n/**\n * §3.4.6 — owner similarity: same as nameSimilarity but restricted to everything **before**\n * the last segment. Compensates for the R-8 same-name-different-class collision, where\n * `UserRepo.getUser` should not be pair-swapped with `AdminRepo.getUser`.\n *\n * Owner extraction:\n * - `Class::method` → `Class`\n * - `A.B.C.method` → `A.B.C`\n * - `topLevel` → `` (empty)\n *\n * When both owners are empty (top-level functions), returns 1.0 because both live in the\n * same \"no-owner\" bucket. When one is empty and the other is not, returns 0.0 because the\n * two Symbols live in structurally different scopes.\n */\nexport function ownerSimilarity(baseName: string, headName: string): number {\n const baseOwner = extractOwner(baseName)\n const headOwner = extractOwner(headName)\n if (baseOwner === \"\" && headOwner === \"\") return 1\n if (baseOwner === \"\" || headOwner === \"\") return 0\n return jaccardTokens(baseOwner, headOwner)\n}\n\nfunction extractOwner(qname: string): string {\n const staticIdx = qname.indexOf(\"::\")\n if (staticIdx >= 0) return qname.slice(0, staticIdx)\n const lastDot = qname.lastIndexOf(\".\")\n if (lastDot >= 0) return qname.slice(0, lastDot)\n return \"\"\n}\n\n/** §3.4.1 tail note — used by drop-4.5 weak matcher and threshold lookup. */\nexport function lastSegment(qname: string): string {\n const staticIdx = qname.indexOf(\"::\")\n if (staticIdx >= 0) return qname.slice(staticIdx + 2)\n const lastDot = qname.lastIndexOf(\".\")\n if (lastDot >= 0) return qname.slice(lastDot + 1)\n return qname\n}\n","import type { Symbol as IRSymbol, MatchRationale } from \"@aburi/types\"\nimport { signatureSimilarity } from \"./signature\"\nimport { lastSegment, nameSimilarity, ownerSimilarity, tokenizeName } from \"./similarity\"\n\n/**\n * A concluded pairing of one base Symbol with one head Symbol, along with the\n * `MatchRationale` that placed them in the same pair. Stage 4 uses this shape both to\n * carry results out and to feed the delta / status classifier downstream.\n */\nexport interface SymbolPair {\n base: IRSymbol\n head: IRSymbol\n rationale: MatchRationale\n}\n\n/** Optional map returned by git for stage 2 (`old path` → `new path`). */\nexport type GitRenameMap = ReadonlyMap<string, string>\n\n/** Fingerprint for the 12-hex-char \"everything zero\" (i.e. dropped) canonical hash. */\nconst ZERO_LOGIC_FP = \"000000000000\"\n\n/**\n * §3.1 — segments Symbols with identical `id` into paired matches. Runs first because it\n * is the highest-confidence signal (no heuristics, just hash lookup).\n */\nexport function matchStageId(\n base: readonly IRSymbol[],\n head: readonly IRSymbol[],\n): {\n matched: SymbolPair[]\n remainingBase: IRSymbol[]\n remainingHead: IRSymbol[]\n} {\n const headById = new Map<string, IRSymbol>()\n for (const s of head) headById.set(s.id, s)\n const matched: SymbolPair[] = []\n const remainingBase: IRSymbol[] = []\n const usedHead = new Set<string>()\n for (const b of base) {\n const h = headById.get(b.id)\n if (h !== undefined) {\n matched.push({ base: b, head: h, rationale: \"id-match\" })\n usedHead.add(b.id)\n } else {\n remainingBase.push(b)\n }\n }\n const remainingHead = head.filter((h) => !usedHead.has(h.id))\n return { matched, remainingBase, remainingHead }\n}\n\n/**\n * §3.2 — git rename detection. When a rename map is available (typical of ref-driven\n * scans), rewrite the base id with the head-side file path and look for a hit.\n *\n * The rewriter only touches the `file` portion of the id — the `<language>:` prefix and\n * the trailing `#<qualified-name>` segment stay unchanged so a moved file with the same\n * qualified name is picked up correctly. When a Symbol's file is not in the rename map,\n * it is left for later stages.\n */\nexport function matchStageGitRename(\n remainingBase: readonly IRSymbol[],\n remainingHead: readonly IRSymbol[],\n renameMap: GitRenameMap | null,\n): {\n matched: SymbolPair[]\n remainingBase: IRSymbol[]\n remainingHead: IRSymbol[]\n} {\n if (renameMap === null || renameMap.size === 0) {\n return { matched: [], remainingBase: [...remainingBase], remainingHead: [...remainingHead] }\n }\n const headById = new Map<string, IRSymbol>()\n for (const h of remainingHead) headById.set(h.id, h)\n const matched: SymbolPair[] = []\n const usedHead = new Set<string>()\n const remaining: IRSymbol[] = []\n for (const b of remainingBase) {\n const newPath = renameMap.get(b.source.file)\n if (newPath === undefined) {\n remaining.push(b)\n continue\n }\n const expectedId = rewriteIdFile(b.id, b.source.file, newPath)\n const h = headById.get(expectedId)\n if (h === undefined || usedHead.has(h.id)) {\n remaining.push(b)\n continue\n }\n matched.push({ base: b, head: h, rationale: \"git-rename\" })\n usedHead.add(h.id)\n }\n const remainingHeadOut = remainingHead.filter((h) => !usedHead.has(h.id))\n return { matched, remainingBase: remaining, remainingHead: remainingHeadOut }\n}\n\nfunction rewriteIdFile(id: string, oldPath: string, newPath: string): string {\n const colon = id.indexOf(\":\")\n const hash = id.indexOf(\"#\")\n if (colon < 0 || hash < 0 || hash < colon) return id\n const language = id.slice(0, colon)\n const filePart = id.slice(colon + 1, hash)\n if (filePart !== oldPath) return id\n const rest = id.slice(hash)\n return `${language}:${newPath}${rest}`\n}\n\n/**\n * §3.3 — group base Symbols by `fingerprint.logic` and match head Symbols whose logic\n * fingerprint is in the map. Two disambiguation branches:\n * - single candidate → paired with `logic-fingerprint`\n * - multiple candidates → best `nameSimilarity` wins if ≥ 0.85; otherwise leave both\n * sides in remaining for stage 4 to re-evaluate\n *\n * Dropped symbols are excluded (§3.3 tail) — their logic fingerprint is the sentinel\n * `\"000000000000\"` and would collide with every other dropped Symbol in the workspace.\n * They flow to the stage-4.5 weak matcher instead.\n */\nexport function matchStageLogicFingerprint(\n remainingBase: readonly IRSymbol[],\n remainingHead: readonly IRSymbol[],\n): {\n matched: SymbolPair[]\n remainingBase: IRSymbol[]\n remainingHead: IRSymbol[]\n} {\n const logicMap = new Map<string, IRSymbol[]>()\n const carryBase: IRSymbol[] = []\n for (const b of remainingBase) {\n if (b.dropped || b.fingerprint.logic === ZERO_LOGIC_FP) {\n carryBase.push(b)\n continue\n }\n const bucket = logicMap.get(b.fingerprint.logic) ?? []\n bucket.push(b)\n logicMap.set(b.fingerprint.logic, bucket)\n }\n const matched: SymbolPair[] = []\n const carryHead: IRSymbol[] = []\n const usedBaseIds = new Set<string>()\n for (const h of remainingHead) {\n if (h.dropped || h.fingerprint.logic === ZERO_LOGIC_FP) {\n carryHead.push(h)\n continue\n }\n const candidates = logicMap.get(h.fingerprint.logic)\n if (candidates === undefined || candidates.length === 0) {\n carryHead.push(h)\n continue\n }\n if (candidates.length === 1) {\n const only = candidates[0]\n if (only === undefined) continue\n matched.push({ base: only, head: h, rationale: \"logic-fingerprint\" })\n usedBaseIds.add(only.id)\n logicMap.delete(h.fingerprint.logic)\n continue\n }\n let best: { symbol: IRSymbol; score: number } | null = null\n for (const c of candidates) {\n const score = nameSimilarity(c.name, h.name)\n if (best === null || score > best.score) best = { symbol: c, score }\n }\n if (best !== null && best.score >= 0.85) {\n matched.push({\n base: best.symbol,\n head: h,\n rationale: \"logic-fingerprint+name-disambiguation\",\n })\n usedBaseIds.add(best.symbol.id)\n const remainingBucket = candidates.filter((c) => c.id !== best?.symbol.id)\n if (remainingBucket.length === 0) logicMap.delete(h.fingerprint.logic)\n else logicMap.set(h.fingerprint.logic, remainingBucket)\n } else {\n carryHead.push(h)\n }\n }\n const finalBase: IRSymbol[] = [...carryBase]\n for (const bucket of logicMap.values()) {\n for (const b of bucket) {\n if (!usedBaseIds.has(b.id)) finalBase.push(b)\n }\n }\n return { matched, remainingBase: finalBase, remainingHead: carryHead }\n}\n\n/**\n * §3.4 — name + signature similarity with `(kind, signatureNullness)` bucket pre-filter.\n * `signatureNullness` distinguishes symbols with no signature (`interface`, `type`,\n * `class`) from callable ones so that a class body is never paired against a function.\n *\n * The composite score:\n * 0.5 * nameSimilarity + 0.3 * signatureSimilarity + 0.2 * ownerSimilarity\n *\n * §3.4.3 threshold table:\n * - 1-token name → 1.0 (unreachable → never paired; false-positive shield)\n * - 2-token name → 0.95 (strict, avoids `getUser` vs `getUsers`)\n * - default → 0.85\n *\n * §3.4.3 tail — if both sides are signature-less, we skip pairing entirely because\n * signatureSimilarity always returns 1.0 for `null + null` and would flood the bucket with\n * false pairings.\n */\nexport function matchStageNameSignature(\n remainingBase: readonly IRSymbol[],\n remainingHead: readonly IRSymbol[],\n): {\n matched: SymbolPair[]\n remainingBase: IRSymbol[]\n remainingHead: IRSymbol[]\n} {\n type BucketKey = string\n const buckets = new Map<BucketKey, IRSymbol[]>()\n for (const b of remainingBase) {\n if (b.dropped) continue\n const key = bucketKey(b)\n const bucket = buckets.get(key) ?? []\n bucket.push(b)\n buckets.set(key, bucket)\n }\n const matched: SymbolPair[] = []\n const remainingHeadOut: IRSymbol[] = []\n const usedBaseIds = new Set<string>()\n for (const h of remainingHead) {\n if (h.dropped) {\n remainingHeadOut.push(h)\n continue\n }\n const key = bucketKey(h)\n const bucket = buckets.get(key)\n if (bucket === undefined || bucket.length === 0) {\n remainingHeadOut.push(h)\n continue\n }\n const headSigNull = h.signature === null || h.signature === undefined\n if (headSigNull && bucket.every((c) => c.signature === null || c.signature === undefined)) {\n remainingHeadOut.push(h)\n continue\n }\n let best: { symbol: IRSymbol; score: number } | null = null\n for (const c of bucket) {\n const score =\n 0.5 * nameSimilarity(c.name, h.name) +\n 0.3 * signatureSimilarity(c.signature ?? null, h.signature ?? null) +\n 0.2 * ownerSimilarity(c.name, h.name)\n if (best === null || score > best.score) best = { symbol: c, score }\n }\n const threshold = thresholdFor(h.name)\n if (best !== null && best.score >= threshold) {\n matched.push({ base: best.symbol, head: h, rationale: \"name-signature\" })\n usedBaseIds.add(best.symbol.id)\n buckets.set(\n key,\n bucket.filter((c) => c.id !== best?.symbol.id),\n )\n } else {\n remainingHeadOut.push(h)\n }\n }\n const remainingBaseOut: IRSymbol[] = []\n for (const b of remainingBase) {\n if (b.dropped) {\n remainingBaseOut.push(b)\n continue\n }\n if (!usedBaseIds.has(b.id)) remainingBaseOut.push(b)\n }\n return { matched, remainingBase: remainingBaseOut, remainingHead: remainingHeadOut }\n}\n\nfunction bucketKey(s: IRSymbol): string {\n const sig = s.signature === null || s.signature === undefined ? \"no-sig\" : \"has-sig\"\n return `${s.kind}::${sig}`\n}\n\nfunction thresholdFor(qname: string): number {\n const tokens = tokenizeName(lastSegment(qname)).length\n if (tokens <= 1) return 1\n if (tokens === 2) return 0.95\n return 0.85\n}\n\n/**\n * §3.4.5 — dropped-only weak matcher. For dropped Symbols the fingerprint is zeroed and\n * name/signature are the only remaining signals. Score is a coarse \"did the last name\n * segment or file basename survive?\" — if either half matches we pair (threshold 0.5).\n *\n * This is deliberately lax; diff-algorithm.md §3.4.5 already accepts that dropped Symbols live\n * outside the main IR review surface, so occasional false pairings only affect the\n * \"Drop-rule variation\" fold-out section in the Markdown projection.\n */\nexport function matchStageDroppedWeak(\n remainingBase: readonly IRSymbol[],\n remainingHead: readonly IRSymbol[],\n): {\n matched: SymbolPair[]\n remainingBase: IRSymbol[]\n remainingHead: IRSymbol[]\n} {\n const droppedBase = remainingBase.filter((s) => s.dropped)\n const nonDroppedBase = remainingBase.filter((s) => !s.dropped)\n const droppedHead = remainingHead.filter((s) => s.dropped)\n const nonDroppedHead = remainingHead.filter((s) => !s.dropped)\n const matched: SymbolPair[] = []\n const usedBaseIds = new Set<string>()\n const carryHead: IRSymbol[] = []\n for (const h of droppedHead) {\n let best: { symbol: IRSymbol; score: number } | null = null\n for (const b of droppedBase) {\n if (b.kind !== h.kind) continue\n if (usedBaseIds.has(b.id)) continue\n const nameHit = lastSegment(b.name) === lastSegment(h.name) ? 1 : 0\n const fileHit = basename(b.source.file) === basename(h.source.file) ? 1 : 0\n const score = 0.5 * nameHit + 0.5 * fileHit\n if (best === null || score > best.score) best = { symbol: b, score }\n }\n if (best !== null && best.score >= 0.5) {\n matched.push({ base: best.symbol, head: h, rationale: \"dropped-weak-match\" })\n usedBaseIds.add(best.symbol.id)\n } else {\n carryHead.push(h)\n }\n }\n const carryBase: IRSymbol[] = [\n ...nonDroppedBase,\n ...droppedBase.filter((b) => !usedBaseIds.has(b.id)),\n ]\n return {\n matched,\n remainingBase: carryBase,\n remainingHead: [...nonDroppedHead, ...carryHead],\n }\n}\n\nfunction basename(path: string): string {\n const slash = path.lastIndexOf(\"/\")\n return slash >= 0 ? path.slice(slash + 1) : path\n}\n","import type { Symbol as IRSymbol } from \"@aburi/types\"\n\nexport type SymbolStatus = \"unchanged\" | \"moved\" | \"changed\" | \"moved+changed\" | \"dropped-toggled\"\n\n/**\n * §4 — determine which of the five statuses a matched pair falls into. `dropped-toggled`\n * has absolute priority: whenever `dropped` differs between base and head, every other\n * signal is ignored because the underlying fingerprint change is a consequence of the\n * drop-rule flip, not a genuine code edit (§4.1 rationale).\n *\n * \"pathChanged\" catches both physical file relocation AND in-file rename: any Symbol id\n * mismatch between base and head means the identifier moved, and DF9 (\"method 名 rename\n * (同 file、同 logic) → moved\") relies on this. Stage-1 pairs cannot trigger the id branch\n * (they match by exact id), so this only affects stage-2+ pairings where the pair was\n * discovered via fingerprint or name/signature similarity.\n */\nexport function classifyStatus(base: IRSymbol, head: IRSymbol): SymbolStatus {\n const droppedToggled = base.dropped !== head.dropped\n if (droppedToggled) return \"dropped-toggled\"\n const pathChanged = base.source.file !== head.source.file || base.id !== head.id\n const fingerprintChanged =\n base.fingerprint.api !== head.fingerprint.api ||\n base.fingerprint.logic !== head.fingerprint.logic ||\n base.fingerprint.syntax !== head.fingerprint.syntax\n if (pathChanged && fingerprintChanged) return \"moved+changed\"\n if (pathChanged) return \"moved\"\n if (fingerprintChanged) return \"changed\"\n return \"unchanged\"\n}\n\n/** Direction of a `dropped-toggled` transition (§7.1 SymbolDroppedToggled). */\nexport type DropDirection = \"to-dropped\" | \"to-kept\"\n\nexport function dropDirection(head: IRSymbol): DropDirection {\n return head.dropped ? \"to-dropped\" : \"to-kept\"\n}\n","import { type SerializeOptions, serializeCanonical } from \"@aburi/core\"\nimport type { DiffResult, IR, IRRef, Summary, SymbolChange } from \"@aburi/types\"\nimport { diffComponents, diffDependencies } from \"./components\"\nimport { computeSymbolDelta, type DeltaOptions } from \"./delta\"\nimport { DiffError } from \"./errors\"\nimport {\n type GitRenameMap,\n matchStageDroppedWeak,\n matchStageGitRename,\n matchStageId,\n matchStageLogicFingerprint,\n matchStageNameSignature,\n type SymbolPair,\n} from \"./match\"\nimport { classifyStatus, dropDirection } from \"./status\"\n\nconst DIFF_SCHEMA = \"https://aburi.dev/schema/aburi.diff.v1.json\"\n\nexport interface DiffInput {\n baseIR: IR\n headIR: IR\n /** IR reference metadata (git ref / file path) for provenance. */\n base: IRRef\n head: IRRef\n /** Generator record for the diff output. Defaults to `{name: \"aburi\", version: \"0.0.0\"}`. */\n generator?: { name: string; version: string }\n /** Optional stage-2 rename table. When null/undefined stage 2 is skipped. */\n gitRenames?: GitRenameMap | null\n /** Passed through to computeSymbolDelta (§5.2.1 line fuzz). */\n delta?: DeltaOptions\n}\n\nconst DEFAULT_GENERATOR = { name: \"aburi\", version: \"0.0.0\" }\n\n/**\n * Top-level entry: run the 5-stage matcher, classify each pair, produce array deltas,\n * fold in Component / Dependency diffs, and assemble the `aburi.diff.v1` JSON projection.\n * The function is pure; write-to-disk is delegated to `writeCanonicalDiff` so callers can\n * pipe the result through additional steps (Markdown projection, `--fail-on` gate) before\n * serialisation.\n */\nexport function buildDiff(input: DiffInput): DiffResult {\n assertIRShape(input.baseIR, \"baseIR\")\n assertIRShape(input.headIR, \"headIR\")\n ensureSchemasAgree(input.baseIR, input.headIR)\n const stage1 = matchStageId(input.baseIR.symbols, input.headIR.symbols)\n const stage2 = matchStageGitRename(\n stage1.remainingBase,\n stage1.remainingHead,\n input.gitRenames ?? null,\n )\n const stage3 = matchStageLogicFingerprint(stage2.remainingBase, stage2.remainingHead)\n const stage4 = matchStageNameSignature(stage3.remainingBase, stage3.remainingHead)\n const stage4_5 = matchStageDroppedWeak(stage4.remainingBase, stage4.remainingHead)\n\n const pairs: SymbolPair[] = [\n ...stage1.matched,\n ...stage2.matched,\n ...stage3.matched,\n ...stage4.matched,\n ...stage4_5.matched,\n ]\n\n const summary: Summary = {\n added: 0,\n removed: 0,\n moved: 0,\n movedChanged: 0,\n changed: 0,\n droppedToggled: 0,\n unchanged: 0,\n droppedAdded: 0,\n droppedRemoved: 0,\n componentsAdded: 0,\n componentsRemoved: 0,\n componentsChanged: 0,\n depsAdded: 0,\n depsRemoved: 0,\n }\n\n const symbols: SymbolChange[] = []\n for (const pair of pairs) {\n const status = classifyStatus(pair.base, pair.head)\n if (status === \"unchanged\") {\n summary.unchanged++\n continue\n }\n if (status === \"dropped-toggled\") {\n summary.droppedToggled++\n symbols.push({\n status: \"dropped-toggled\",\n before: pair.base,\n after: pair.head,\n direction: dropDirection(pair.head),\n })\n continue\n }\n if (status === \"moved\") {\n summary.moved++\n symbols.push({\n status: \"moved\",\n before: pair.base,\n after: pair.head,\n rationale: pair.rationale,\n })\n continue\n }\n if (status === \"changed\") {\n summary.changed++\n symbols.push({\n status: \"changed\",\n before: pair.base,\n after: pair.head,\n delta: computeSymbolDelta(pair.base, pair.head, input.delta ?? {}),\n })\n continue\n }\n // moved+changed\n summary.movedChanged++\n symbols.push({\n status: \"moved+changed\",\n before: pair.base,\n after: pair.head,\n rationale: pair.rationale,\n delta: computeSymbolDelta(pair.base, pair.head, input.delta ?? {}),\n })\n }\n\n const finalRemainingHead = stage4_5.remainingHead\n const finalRemainingBase = stage4_5.remainingBase\n\n for (const h of finalRemainingHead) {\n if (h.dropped) summary.droppedAdded++\n else {\n summary.added++\n symbols.push({ status: \"added\", symbol: h })\n }\n }\n for (const b of finalRemainingBase) {\n if (b.dropped) summary.droppedRemoved++\n else {\n summary.removed++\n symbols.push({ status: \"removed\", symbol: b })\n }\n }\n\n const components = diffComponents(input.baseIR.components, input.headIR.components)\n summary.componentsAdded = components.added.length\n summary.componentsRemoved = components.removed.length\n summary.componentsChanged = components.changed.length\n\n const dependencies = diffDependencies(input.baseIR.dependencies, input.headIR.dependencies)\n summary.depsAdded = dependencies.added.length\n summary.depsRemoved = dependencies.removed.length\n\n symbols.sort(compareSymbolChange)\n\n return {\n $schema: DIFF_SCHEMA,\n generator: input.generator ?? DEFAULT_GENERATOR,\n base: input.base,\n head: input.head,\n summary,\n symbols,\n components,\n dependencies,\n }\n}\n\n/** §9.1 — refuse to diff across schema versions. */\nfunction ensureSchemasAgree(base: IR, head: IR): void {\n if (base.$schema !== head.$schema) {\n throw new DiffError(\n `Base IR schema \"${base.$schema}\" does not match head IR schema \"${head.$schema}\"; a diff across schema versions is not supported.`,\n { code: \"schema-mismatch\", value: base.$schema },\n )\n }\n}\n\n/**\n * Top-level shape check. Refuses to enter the 5-stage matcher when a caller hands in an\n * IR-shaped object that is missing one of the collections the matcher requires as an\n * array. Without this, a malformed `baseIR = { symbols: undefined, ... }` would crash\n * deep inside `matchStageId` with `TypeError: undefined is not iterable`, leaving the\n * root cause obscured. Not a full schema validation — just an entry-point smoke test.\n */\nfunction assertIRShape(ir: IR, name: \"baseIR\" | \"headIR\"): void {\n if (ir === null || typeof ir !== \"object\") {\n throw new DiffError(`${name} must be an IR object; got ${typeof ir}.`, {\n code: \"ir-shape-invalid\",\n value: name,\n })\n }\n if (typeof ir.$schema !== \"string\" || ir.$schema.length === 0) {\n throw new DiffError(`${name}.$schema must be a non-empty schema URL.`, {\n code: \"ir-shape-invalid\",\n value: `${name}.$schema`,\n })\n }\n for (const field of [\"symbols\", \"components\", \"dependencies\"] as const) {\n if (!Array.isArray(ir[field])) {\n throw new DiffError(`${name}.${field} must be an array.`, {\n code: \"ir-shape-invalid\",\n value: `${name}.${field}`,\n })\n }\n }\n}\n\n/**\n * Deterministic ordering of the `symbols[]` output:\n * 1. by status (alphabetical) — pins added/changed/moved sections\n * 2. by the \"reference\" id (`after` for change/move/toggle, otherwise `symbol`)\n *\n * The result is stable byte-for-byte for equal inputs, matching the canonicalisation\n * guarantee the diff JSON must uphold when persisted to `out/diff.json`.\n */\nfunction compareSymbolChange(a: SymbolChange, b: SymbolChange): number {\n if (a.status !== b.status) return a.status < b.status ? -1 : 1\n const idA = referenceId(a)\n const idB = referenceId(b)\n return idA < idB ? -1 : idA > idB ? 1 : 0\n}\n\nfunction referenceId(change: SymbolChange): string {\n if (change.status === \"added\" || change.status === \"removed\") return change.symbol.id\n return change.after.id\n}\n\n/**\n * Byte-deterministic serialiser for a DiffResult. Delegates to `@aburi/core`\n * `serializeCanonical` so the sort order, NFC normalisation, and codepoint key sort are\n * shared with the IR side.\n */\nexport function writeCanonicalDiff(diff: DiffResult, options: SerializeOptions = {}): string {\n return serializeCanonical(diff, options)\n}\n"],"mappings":";;;;;;;;;;;AAUA,SAAgB,eACd,MACA,MACe;CACf,MAAM,2BAAW,IAAI,IAAuB;CAC5C,KAAK,MAAM,KAAK,MAAM,SAAS,IAAI,EAAE,IAAI,CAAC;CAC1C,MAAM,2BAAW,IAAI,IAAuB;CAC5C,KAAK,MAAM,KAAK,MAAM,SAAS,IAAI,EAAE,IAAI,CAAC;CAE1C,MAAM,QAAqB,CAAC;CAC5B,MAAM,UAAuB,CAAC;CAC9B,MAAM,UAAoC,CAAC;CAE3C,KAAK,MAAM,CAAC,IAAI,aAAa,UAAU;EACrC,MAAM,WAAW,SAAS,IAAI,EAAE;EAChC,IAAI,aAAa,QAAW;GAC1B,MAAM,KAAK,QAAQ;GACnB;EACF;EACA,MAAM,eAAe,CAAC,kBAAkB,SAAS,OAAO,SAAS,KAAK;EACtE,MAAM,mBAAmB,CAAC,kBAAkB,SAAS,aAAa,CAAC,GAAG,SAAS,aAAa,CAAC,CAAC;EAC9F,MAAM,oBAAoB,CAAC,kBACzB,SAAS,cAAc,CAAC,GACxB,SAAS,cAAc,CAAC,CAC1B;EACA,IAAI,gBAAgB,oBAAoB,mBACtC,QAAQ,KAAK;GACX,QAAQ;GACR,OAAO;GACP,OAAO;IAAE;IAAc;IAAkB;GAAkB;EAC7D,CAAC;CAEL;CACA,KAAK,MAAM,CAAC,IAAI,aAAa,UAC3B,IAAI,CAAC,SAAS,IAAI,EAAE,GAAG,QAAQ,KAAK,QAAQ;CAE9C,SAAS,KAAK;C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UAC/B,MAAM,IAAI,UAAU,GAAG,KAAK,6BAA6B,OAAO,GAAG,IAAI;EACrE,MAAM;EACN,OAAO;CACT,CAAC;CAEH,IAAI,OAAO,GAAG,YAAY,YAAY,GAAG,QAAQ,WAAW,GAC1D,MAAM,IAAI,UAAU,GAAG,KAAK,2CAA2C;EACrE,MAAM;EACN,OAAO,GAAG,KAAK;CACjB,CAAC;CAEH,KAAK,MAAM,SAAS;EAAC;EAAW;EAAc;CAAc,GAC1D,IAAI,CAAC,MAAM,QAAQ,GAAG,MAAM,GAC1B,MAAM,IAAI,UAAU,GAAG,KAAK,GAAG,MAAM,qBAAqB;EACxD,MAAM;EACN,OAAO,GAAG,KAAK,GAAG;CACpB,CAAC;AAGP;;;;;;;;;AAUA,SAAS,oBAAoB,GAAiB,GAAyB;CACrE,IAAI,EAAE,WAAW,EAAE,QAAQ,OAAO,EAAE,SAAS,EAAE,SAAS,KAAK;CAC7D,MAAM,MAAM,YAAY,CAAC;CACzB,MAAM,MAAM,YAAY,CAAC;CACzB,OAAO,MAAM,MAAM,KAAK,MAAM,MAAM,IAAI;AAC1C;AAEA,SAAS,YAAY,QAA8B;CACjD,IAAI,OAAO,WAAW,WAAW,OAAO,WAAW,WAAW,OAAO,OAAO,OAAO;CACnF,OAAO,OAAO,MAAM;AACtB;;;;;;AAOA,SAAgB,mBAAmB,MAAkB,UAA4B,CAAC,GAAW;CAC3F,OAAO,mBAAmB,MAAM,OAAO;AACzC"}
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@aburi/diff",
3
+ "version": "0.1.0",
4
+ "description": "Aburi diff engine: 5-stage semantic matcher (id / git-rename / logic fingerprint / name+signature / dropped weak) followed by status classification and per-Symbol delta computation, emitting aburi.diff.v1-conformant output for PR-review workflows",
5
+ "license": "MIT",
6
+ "author": "kage1020",
7
+ "homepage": "https://github.com/kage1020/Aburi/tree/main/packages/diff#readme",
8
+ "bugs": {
9
+ "url": "https://github.com/kage1020/Aburi/issues"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/kage1020/Aburi.git",
14
+ "directory": "packages/diff"
15
+ },
16
+ "type": "module",
17
+ "main": "./dist/index.mjs",
18
+ "types": "./dist/index.d.mts",
19
+ "exports": {
20
+ ".": {
21
+ "types": "./dist/index.d.mts",
22
+ "default": "./dist/index.mjs"
23
+ },
24
+ "./package.json": "./package.json"
25
+ },
26
+ "files": [
27
+ "dist",
28
+ "src",
29
+ "README.md"
30
+ ],
31
+ "sideEffects": false,
32
+ "publishConfig": {
33
+ "access": "public"
34
+ },
35
+ "dependencies": {
36
+ "@aburi/core": "0.1.0",
37
+ "@aburi/types": "0.1.0"
38
+ },
39
+ "devDependencies": {
40
+ "@types/node": "^26.0.1",
41
+ "tsdown": "^0.22.3",
42
+ "typescript": "^6.0.3",
43
+ "vitest": "^4.1.9"
44
+ },
45
+ "scripts": {
46
+ "build": "tsdown",
47
+ "typecheck": "tsc -p tsconfig.json --noEmit",
48
+ "test": "vitest run"
49
+ }
50
+ }
@@ -0,0 +1,105 @@
1
+ import type { Component, ComponentDiff, Dependency, DependencyDiff } from "@aburi/types"
2
+
3
+ /**
4
+ * §6.1 — Component diff. Component identity is `id`; when a Component is present in both
5
+ * base and head with the same id, the three delta booleans describe which axes actually
6
+ * moved (roots reshuffle, public API globs, or framework hint list). `modified` deltas
7
+ * are intentionally absent from this diff (§5.2.3): fields are reported as before/after
8
+ * pairs via the `changed[]` entries so consumers can render them without stringifying
9
+ * arrays.
10
+ */
11
+ export function diffComponents(
12
+ base: readonly Component[],
13
+ head: readonly Component[],
14
+ ): ComponentDiff {
15
+ const baseById = new Map<string, Component>()
16
+ for (const c of base) baseById.set(c.id, c)
17
+ const headById = new Map<string, Component>()
18
+ for (const c of head) headById.set(c.id, c)
19
+
20
+ const added: Component[] = []
21
+ const removed: Component[] = []
22
+ const changed: ComponentDiff["changed"] = []
23
+
24
+ for (const [id, headComp] of headById) {
25
+ const baseComp = baseById.get(id)
26
+ if (baseComp === undefined) {
27
+ added.push(headComp)
28
+ continue
29
+ }
30
+ const rootsChanged = !stringArraysEqual(baseComp.roots, headComp.roots)
31
+ const publicApiChanged = !stringArraysEqual(baseComp.publicApi ?? [], headComp.publicApi ?? [])
32
+ const frameworksChanged = !stringArraysEqual(
33
+ baseComp.frameworks ?? [],
34
+ headComp.frameworks ?? [],
35
+ )
36
+ if (rootsChanged || publicApiChanged || frameworksChanged) {
37
+ changed.push({
38
+ before: baseComp,
39
+ after: headComp,
40
+ delta: { rootsChanged, publicApiChanged, frameworksChanged },
41
+ })
42
+ }
43
+ }
44
+ for (const [id, baseComp] of baseById) {
45
+ if (!headById.has(id)) removed.push(baseComp)
46
+ }
47
+ sortById(added)
48
+ sortById(removed)
49
+ changed.sort((a, b) => (a.after.id < b.after.id ? -1 : a.after.id > b.after.id ? 1 : 0))
50
+ return { added, removed, changed }
51
+ }
52
+
53
+ /**
54
+ * §6.2 — Dependency diff. Identity is the composite `(from, to, via)` triple; direction
55
+ * and effect changes are surfaced as an added + removed pair so `modified` is not part of
56
+ * the schema (§6.2 tail).
57
+ */
58
+ export function diffDependencies(
59
+ base: readonly Dependency[],
60
+ head: readonly Dependency[],
61
+ ): DependencyDiff {
62
+ const baseKeys = new Map<string, Dependency>()
63
+ for (const d of base) baseKeys.set(dependencyKey(d), d)
64
+ const headKeys = new Map<string, Dependency>()
65
+ for (const d of head) headKeys.set(dependencyKey(d), d)
66
+ const added: Dependency[] = []
67
+ const removed: Dependency[] = []
68
+ for (const [key, dep] of headKeys) {
69
+ const b = baseKeys.get(key)
70
+ if (b === undefined) {
71
+ added.push(dep)
72
+ continue
73
+ }
74
+ if (b.direction !== dep.direction || (b.effect ?? null) !== (dep.effect ?? null)) {
75
+ removed.push(b)
76
+ added.push(dep)
77
+ }
78
+ }
79
+ for (const [key, dep] of baseKeys) {
80
+ if (!headKeys.has(key)) removed.push(dep)
81
+ }
82
+ added.sort(compareDependencies)
83
+ removed.sort(compareDependencies)
84
+ return { added, removed }
85
+ }
86
+
87
+ function dependencyKey(d: Dependency): string {
88
+ return `${d.from}::${d.to}::${d.via}`
89
+ }
90
+
91
+ function compareDependencies(a: Dependency, b: Dependency): number {
92
+ const keyA = dependencyKey(a)
93
+ const keyB = dependencyKey(b)
94
+ return keyA < keyB ? -1 : keyA > keyB ? 1 : 0
95
+ }
96
+
97
+ function stringArraysEqual(a: readonly string[], b: readonly string[]): boolean {
98
+ if (a.length !== b.length) return false
99
+ for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false
100
+ return true
101
+ }
102
+
103
+ function sortById<T extends { id: string }>(items: T[]): void {
104
+ items.sort((a, b) => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0))
105
+ }
package/src/delta.ts ADDED
@@ -0,0 +1,301 @@
1
+ import type {
2
+ ArrayDelta,
3
+ Call,
4
+ Decorator,
5
+ Effect,
6
+ Symbol as IRSymbol,
7
+ Rule,
8
+ Signature,
9
+ SignatureDelta,
10
+ SymbolDelta,
11
+ } from "@aburi/types"
12
+ import { DiffError } from "./errors"
13
+
14
+ export const DEFAULT_LINE_FUZZ = 2
15
+ export const MAX_LINE_FUZZ = 10
16
+ export const MIN_LINE_FUZZ = 0
17
+
18
+ export interface DeltaOptions {
19
+ /**
20
+ * §5.2.1 line fuzz for rule/call identity. Must be an integer in
21
+ * `[MIN_LINE_FUZZ, MAX_LINE_FUZZ]` (`0..10`); anything outside — or a non-finite value —
22
+ * throws `DiffError({ code: "invalid-line-fuzz" })`. Setting `0` disables fuzz; omitting
23
+ * the field falls back to `DEFAULT_LINE_FUZZ` (2).
24
+ */
25
+ lineFuzz?: number
26
+ }
27
+
28
+ /**
29
+ * §5 — Compute the full per-Symbol delta between two paired Symbols. The three axis
30
+ * booleans (`apiChanged` / `logicChanged` / `syntaxChanged`) come from fingerprint
31
+ * comparison; the array deltas (`rules` / `effects` / `calls` / `decorators`) come from
32
+ * identity-preserving diff with configurable line fuzz.
33
+ */
34
+ export function computeSymbolDelta(
35
+ base: IRSymbol,
36
+ head: IRSymbol,
37
+ options: DeltaOptions = {},
38
+ ): SymbolDelta {
39
+ const fuzz = validateLineFuzz(options.lineFuzz ?? DEFAULT_LINE_FUZZ)
40
+ const delta: SymbolDelta = {
41
+ apiChanged: base.fingerprint.api !== head.fingerprint.api,
42
+ logicChanged: base.fingerprint.logic !== head.fingerprint.logic,
43
+ syntaxChanged: base.fingerprint.syntax !== head.fingerprint.syntax,
44
+ componentChanged: (base.component ?? null) !== (head.component ?? null),
45
+ visibilityChanged: base.visibility !== head.visibility,
46
+ }
47
+ delta.rules = diffRules(base.rules, head.rules, fuzz)
48
+ delta.effects = diffEffects(base.effects, head.effects)
49
+ delta.calls = diffCalls(base.calls, head.calls, fuzz)
50
+ delta.decorators = diffDecorators(base.decorators, head.decorators, fuzz)
51
+ const sigDelta = diffSignature(base.signature ?? null, head.signature ?? null)
52
+ delta.signature = sigDelta
53
+ return delta
54
+ }
55
+
56
+ /**
57
+ * §5.2.1 range check. Loud on violation rather than silently clamping so a config typo
58
+ * (`lineFuzz: 999`) or an upstream numeric bug (`NaN`, `Infinity`) surfaces at the diff
59
+ * boundary instead of quietly rounding into a range that produces the wrong deltas.
60
+ */
61
+ function validateLineFuzz(value: number): number {
62
+ if (!Number.isFinite(value) || !Number.isInteger(value)) {
63
+ throw new DiffError(
64
+ `config.diff.lineFuzz must be an integer in [${MIN_LINE_FUZZ}, ${MAX_LINE_FUZZ}]; got ${String(value)}.`,
65
+ { code: "invalid-line-fuzz", value: String(value) },
66
+ )
67
+ }
68
+ if (value < MIN_LINE_FUZZ || value > MAX_LINE_FUZZ) {
69
+ throw new DiffError(
70
+ `config.diff.lineFuzz must be within [${MIN_LINE_FUZZ}, ${MAX_LINE_FUZZ}]; got ${value}.`,
71
+ { code: "invalid-line-fuzz", value: String(value) },
72
+ )
73
+ }
74
+ return value
75
+ }
76
+
77
+ interface Identified<T> {
78
+ item: T
79
+ key: string
80
+ line: number
81
+ }
82
+
83
+ /**
84
+ * Two-index diff: build key-indexed maps for both sides, then classify each key into
85
+ * `added` / `removed` / `modified`. Line-fuzzed identities are matched within tolerance
86
+ * so cosmetic line shifts do not produce added/removed noise. `modified` only fires when
87
+ * the identity key stays the same but the content differs.
88
+ */
89
+ function differentiate<T>(
90
+ base: readonly Identified<T>[],
91
+ head: readonly Identified<T>[],
92
+ isEqual: (a: T, b: T) => boolean,
93
+ lineFuzz: number,
94
+ ): ArrayDelta {
95
+ const added: T[] = []
96
+ const removed: T[] = []
97
+ const modified: T[] = []
98
+ const consumedBase = new Set<number>()
99
+ for (const h of head) {
100
+ let matchIdx = -1
101
+ for (let j = 0; j < base.length; j++) {
102
+ if (consumedBase.has(j)) continue
103
+ const b = base[j]
104
+ if (b === undefined) continue
105
+ if (b.key !== h.key) continue
106
+ if (Math.abs(b.line - h.line) > lineFuzz) continue
107
+ matchIdx = j
108
+ break
109
+ }
110
+ if (matchIdx === -1) {
111
+ added.push(h.item)
112
+ continue
113
+ }
114
+ consumedBase.add(matchIdx)
115
+ const bmatch = base[matchIdx]
116
+ if (bmatch === undefined) continue
117
+ if (!isEqual(bmatch.item, h.item)) modified.push(h.item)
118
+ }
119
+ for (let j = 0; j < base.length; j++) {
120
+ if (consumedBase.has(j)) continue
121
+ const b = base[j]
122
+ if (b === undefined) continue
123
+ removed.push(b.item)
124
+ }
125
+ return { added, removed, modified }
126
+ }
127
+
128
+ function diffRules(base: readonly Rule[], head: readonly Rule[], lineFuzz: number): ArrayDelta {
129
+ const mapper = (r: Rule): Identified<Rule> => ({
130
+ item: r,
131
+ key: r.type,
132
+ line: r.line,
133
+ })
134
+ return differentiate(base.map(mapper), head.map(mapper), rulesEqual, lineFuzz)
135
+ }
136
+
137
+ function rulesEqual(a: Rule, b: Rule): boolean {
138
+ return (
139
+ a.type === b.type &&
140
+ (a.condition ?? null) === (b.condition ?? null) &&
141
+ (a.what ?? null) === (b.what ?? null) &&
142
+ (a.expr ?? null) === (b.expr ?? null) &&
143
+ (a.loopKind ?? null) === (b.loopKind ?? null)
144
+ )
145
+ }
146
+
147
+ function diffEffects(base: readonly Effect[], head: readonly Effect[]): ArrayDelta {
148
+ const mapper = (e: Effect): Identified<Effect> => ({
149
+ item: e,
150
+ key: `${e.id}::${e.target}`,
151
+ line: e.line,
152
+ })
153
+ return differentiate(base.map(mapper), head.map(mapper), effectsEqual, Number.POSITIVE_INFINITY)
154
+ }
155
+
156
+ function effectsEqual(a: Effect, b: Effect): boolean {
157
+ return (
158
+ a.id === b.id && a.target === b.target && a.plugin === b.plugin && a.confidence === b.confidence
159
+ )
160
+ }
161
+
162
+ function diffCalls(base: readonly Call[], head: readonly Call[], lineFuzz: number): ArrayDelta {
163
+ const mapper = (c: Call): Identified<Call> => ({
164
+ item: c,
165
+ key: c.target,
166
+ line: c.line,
167
+ })
168
+ return differentiate(base.map(mapper), head.map(mapper), callsEqual, lineFuzz)
169
+ }
170
+
171
+ function callsEqual(a: Call, b: Call): boolean {
172
+ return a.target === b.target && (a.resolved ?? null) === (b.resolved ?? null)
173
+ }
174
+
175
+ /**
176
+ * §5.2.2 — decorator diff. Identity is `name`; when the same-named decorator is present
177
+ * on both sides within the line-fuzz window, the argument list determines whether the
178
+ * entry is a modified pair or an implicit no-op.
179
+ */
180
+ function diffDecorators(
181
+ base: readonly Decorator[],
182
+ head: readonly Decorator[],
183
+ lineFuzz: number,
184
+ ): ArrayDelta {
185
+ const mapper = (d: Decorator): Identified<Decorator> => ({
186
+ item: d,
187
+ key: d.name,
188
+ line: d.line,
189
+ })
190
+ return differentiate(base.map(mapper), head.map(mapper), decoratorsEqual, lineFuzz)
191
+ }
192
+
193
+ function decoratorsEqual(a: Decorator, b: Decorator): boolean {
194
+ if (a.name !== b.name) return false
195
+ if (a.arguments.length !== b.arguments.length) return false
196
+ for (let i = 0; i < a.arguments.length; i++) {
197
+ if (a.arguments[i] !== b.arguments[i]) return false
198
+ }
199
+ return true
200
+ }
201
+
202
+ /**
203
+ * §5.3 — signature delta. Three cases:
204
+ * - both sides `null` → `null` (nothing to compare)
205
+ * - exactly one side `null` → the present side is emitted verbatim as
206
+ * `added` (when only head has a signature) or `removed` (when only base has one);
207
+ * the three axis booleans compare the missing side against `false` defaults
208
+ * - both sides non-null → per-list sub-deltas:
209
+ * - `inputs`: strict positional compare via `differentiate(..., lineFuzz: 0)` — index
210
+ * used as the line key so ordered parameter lists match position-for-position
211
+ * - `outputs`: positional compare (added/removed emitted when the arrays diverge at
212
+ * an index), no `modified` category
213
+ * - `throws`: unordered set diff — duplicates collapse per Set semantics
214
+ * - `async` / `generator` / `typeParameters` change flags follow the raw fields
215
+ */
216
+ function diffSignature(base: Signature | null, head: Signature | null): SignatureDelta | null {
217
+ if (base === null && head === null) return null
218
+ if (base === null || head === null) {
219
+ const empty: ArrayDelta = { added: [], removed: [], modified: [] }
220
+ const present = base ?? head
221
+ if (present === null) return null
222
+ const isBaseNull = base === null
223
+ const inputs: ArrayDelta = isBaseNull
224
+ ? { added: [...present.inputs], removed: [], modified: [] }
225
+ : { added: [], removed: [...present.inputs], modified: [] }
226
+ const outputs: ArrayDelta = isBaseNull
227
+ ? { added: [...present.outputs], removed: [], modified: [] }
228
+ : { added: [], removed: [...present.outputs], modified: [] }
229
+ const throws: ArrayDelta = isBaseNull
230
+ ? { added: [...present.throws], removed: [], modified: [] }
231
+ : { added: [], removed: [...present.throws], modified: [] }
232
+ return {
233
+ inputs,
234
+ outputs,
235
+ throws,
236
+ asyncChanged: (base?.async ?? false) !== (head?.async ?? false),
237
+ generatorChanged: (base?.generator ?? false) !== (head?.generator ?? false),
238
+ typeParametersChanged: !arraysEqual(
239
+ base?.typeParameters ?? empty.added.map(String),
240
+ head?.typeParameters ?? empty.added.map(String),
241
+ ),
242
+ }
243
+ }
244
+ const inputMapper = (
245
+ input: { name: string; type: string },
246
+ index: number,
247
+ ): Identified<{ name: string; type: string }> => ({
248
+ item: input,
249
+ key: `${index}:${input.name}`,
250
+ line: index,
251
+ })
252
+ const inputs = differentiate(
253
+ base.inputs.map(inputMapper),
254
+ head.inputs.map(inputMapper),
255
+ (a, b) => a.name === b.name && a.type === b.type,
256
+ 0,
257
+ )
258
+ const outputs = diffStringList(base.outputs, head.outputs)
259
+ const throws = diffStringSet(base.throws, head.throws)
260
+ return {
261
+ inputs,
262
+ outputs,
263
+ throws,
264
+ asyncChanged: base.async !== head.async,
265
+ generatorChanged: base.generator !== head.generator,
266
+ typeParametersChanged: !arraysEqual(base.typeParameters, head.typeParameters),
267
+ }
268
+ }
269
+
270
+ function diffStringList(base: readonly string[], head: readonly string[]): ArrayDelta {
271
+ const added: string[] = []
272
+ const removed: string[] = []
273
+ const max = Math.max(base.length, head.length)
274
+ for (let i = 0; i < max; i++) {
275
+ const b = i < base.length ? base[i] : undefined
276
+ const h = i < head.length ? head[i] : undefined
277
+ if (b === undefined && h !== undefined) added.push(h)
278
+ else if (h === undefined && b !== undefined) removed.push(b)
279
+ else if (b !== h) {
280
+ if (b !== undefined) removed.push(b)
281
+ if (h !== undefined) added.push(h)
282
+ }
283
+ }
284
+ return { added, removed, modified: [] }
285
+ }
286
+
287
+ function diffStringSet(base: readonly string[], head: readonly string[]): ArrayDelta {
288
+ const setB = new Set(base)
289
+ const setH = new Set(head)
290
+ const added: string[] = []
291
+ const removed: string[] = []
292
+ for (const h of setH) if (!setB.has(h)) added.push(h)
293
+ for (const b of setB) if (!setH.has(b)) removed.push(b)
294
+ return { added, removed, modified: [] }
295
+ }
296
+
297
+ function arraysEqual(a: readonly string[], b: readonly string[]): boolean {
298
+ if (a.length !== b.length) return false
299
+ for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false
300
+ return true
301
+ }