Understand any codebase instantly. System intelligence for codebases, built for humans and AI.
TypeScript aiai-memoryclaude-codeclicode-analysiscode-mappingcode-memorycodebase-understandingcodexdeveloper-toolsllmopenclawpersistent-memoryprogram-analysissoftware-architecturesystem-intelligencesystem-mapping
fix: deduplicate deterministic patch edges (#411)
Joseph Mikhail8d1bc85be21d
diff --git a/core-ingestion/src/__tests__/patchBuilder.test.ts b/core-ingestion/src/__tests__/patchBuilder.test.tsindex 745c450c..2c8ff0da 100644--- a/core-ingestion/src/__tests__/patchBuilder.test.ts+++ b/core-ingestion/src/__tests__/patchBuilder.test.ts@@ -1,6 +1,9 @@import { describe, expect, it } from 'vitest';+import { parseFile } from '../index.js';import {+ buildPatch,+ buildPatchWithResolution,buildDeletionPatch,extractorName,PREVIOUS_EXTRACTORS,@@ -48,3 +51,78 @@ describe('patch-builder extractor policy', () => {expect(patch.replaces).toEqual(candidates);});});++describe('patch-builder edge identity', () => {+ it.each([+ ['buildPatch', (result: NonNullable<ReturnType<typeof parseFile>>) => buildPatch(result, 'source-hash')],+ ['buildPatchWithResolution', (result: NonNullable<ReturnType<typeof parseFile>>) =>+ buildPatchWithResolution(result, 'source-hash', '', [])],+ ])('emits each deterministic edge once for Python overloads via %s', (_name, build) => {+ const result = parseFile('backend/tests/unit/test_exir_adapter.py', `+from typing import overload++class Adapter:+ @overload+ def convert(self, value: int) -> int: ...++ @overload+ def convert(self, value: str) -> str: ...++ def convert(self, value):+ return value+`);++ expect(result).not.toBeNull();+ const patch = build(result!);+ const edgeIds = patch.ops+ .filter(op => op.type === 'UpsertEdge')+ .map(op => op.id);+ const chunkNodeIds = patch.ops+ .filter(op => op.type === 'UpsertNode' && (op.kind === 'chunk' || op.kind === 'section'))+ .map(op => op.id);+ const containsChunkEdges = patch.ops.filter(+ op => op.type === 'UpsertEdge' && op.predicate === 'CONTAINS_CHUNK',+ );+ const definesEdges = patch.ops.filter(+ op => op.type === 'UpsertEdge' && op.predicate === 'DEFINES',+ );++ expect(edgeIds.length).toBeGreaterThan(0);+ expect(edgeIds).toHaveLength(new Set(edgeIds).size);+ expect(result!.chunks.every(chunk => chunk.name !== null)).toBe(true);+ expect(containsChunkEdges).toHaveLength(result!.chunks.length);+ expect(definesEdges).toHaveLength(result!.chunks.length);+ expect(new Set(containsChunkEdges.map(edge => edge.dst))).toEqual(new Set(chunkNodeIds));+ expect(new Set(definesEdges.map(edge => edge.src))).toEqual(new Set(chunkNodeIds));+ });++ it.each([+ ['buildPatch', (result: NonNullable<ReturnType<typeof parseFile>>) => buildPatch(result, 'source-hash')],+ ['buildPatchWithResolution', (result: NonNullable<ReturnType<typeof parseFile>>) =>+ buildPatchWithResolution(result, 'source-hash', '', [])],+ ])('keeps distinct NEXT edges between top-level overload chunks via %s', (_name, build) => {+ const result = parseFile('adapter.py', `+from typing import overload++@overload+def convert(value: int) -> int: ...++@overload+def convert(value: str) -> str: ...++def convert(value):+ return value+`);++ expect(result).not.toBeNull();+ const patch = build(result!);+ const nextEdges = patch.ops.filter(+ op => op.type === 'UpsertEdge' && op.predicate === 'NEXT',+ );++ expect(result!.chunks).toHaveLength(3);+ expect(nextEdges).toHaveLength(2);+ expect(new Set(nextEdges.map(edge => edge.id)).size).toBe(2);+ expect(nextEdges[0].dst).toBe(nextEdges[1].src);+ });+});diff --git a/core-ingestion/src/patch-builder.ts b/core-ingestion/src/patch-builder.tsindex ee2e9d2d..bf5db089 100644--- a/core-ingestion/src/patch-builder.ts+++ b/core-ingestion/src/patch-builder.ts@@ -88,12 +88,49 @@ function sourceType(filePath: string): string {return 'code';}+/**+ * Keep each logical edge once per patch. Parsers can emit the same relationship+ * more than once (for example, every declaration in a Python @overload group).+ * Edge ids intentionally collapse those declarations to one graph edge, so+ * forwarding every occurrence would send duplicate physical keys to ArangoDB.+ * A repeated id with different endpoints is an identity bug, not a duplicate;+ * fail before commit rather than silently disconnecting part of the graph.+ */+function deduplicateUpsertEdges(ops: PatchOp[]): PatchOp[] {+ const deduplicated: PatchOp[] = [];+ const edgeIndexById = new Map<string, number>();+ for (const op of ops) {+ if (op.type !== 'UpsertEdge') {+ deduplicated.push(op);+ continue;+ }+ const edgeId = op.id;+ if (typeof edgeId !== 'string') {+ deduplicated.push(op);+ continue;+ }+ const existingIndex = edgeIndexById.get(edgeId);+ if (existingIndex === undefined) {+ edgeIndexById.set(edgeId, deduplicated.length);+ deduplicated.push(op);+ continue;+ }+ const existing = deduplicated[existingIndex];+ if (existing.src !== op.src || existing.dst !== op.dst || existing.predicate !== op.predicate) {+ throw new Error(`Conflicting UpsertEdge identity for deterministic id ${edgeId}`);+ }+ // Exact logical duplicate: retain the last operation's attributes.+ deduplicated[existingIndex] = op;+ }+ return deduplicated;+}+export function extractorName(): string {- return `tree-sitter/1.24`;+ return `tree-sitter/1.25`;}/** Previous extractor versions — their patches are superseded when re-ingesting. */-export const PREVIOUS_EXTRACTORS = ['tree-sitter/1.23', 'tree-sitter/1.22', 'tree-sitter/1.21', 'tree-sitter/1.20', 'tree-sitter/1.19', 'tree-sitter/1.18', 'tree-sitter/1.17', 'tree-sitter/1.16', 'tree-sitter/1.15', 'tree-sitter/1.14', 'tree-sitter/1.13', 'tree-sitter/1.12', 'tree-sitter/1.11', 'tree-sitter/1.10', 'tree-sitter/1.9', 'tree-sitter/1.8', 'tree-sitter/1.7', 'tree-sitter/1.6', 'tree-sitter/1.5', 'tree-sitter/1.4', 'tree-sitter/1.3', 'tree-sitter/1.2', 'tree-sitter/1.1'];+export const PREVIOUS_EXTRACTORS = ['tree-sitter/1.24', 'tree-sitter/1.23', 'tree-sitter/1.22', 'tree-sitter/1.21', 'tree-sitter/1.20', 'tree-sitter/1.19', 'tree-sitter/1.18', 'tree-sitter/1.17', 'tree-sitter/1.16', 'tree-sitter/1.15', 'tree-sitter/1.14', 'tree-sitter/1.13', 'tree-sitter/1.12', 'tree-sitter/1.11', 'tree-sitter/1.10', 'tree-sitter/1.9', 'tree-sitter/1.8', 'tree-sitter/1.7', 'tree-sitter/1.6', 'tree-sitter/1.5', 'tree-sitter/1.4', 'tree-sitter/1.3', 'tree-sitter/1.2', 'tree-sitter/1.1'];/** Patch ids that may own the active graph entities for a stored source hash. */export function sourcePatchIdCandidates(@@ -248,7 +285,7 @@ export function buildPatch(// File -[CONTAINS]-> Chunkops.push({type: 'UpsertEdge',- id: edgeId(idPath, 'file', chunkName, 'CONTAINS_CHUNK'),+ id: edgeId(idPath, fileNodeId, cid, 'CONTAINS_CHUNK'),src: fileNodeId,dst: cid,predicate: 'CONTAINS_CHUNK',@@ -260,7 +297,7 @@ export function buildPatch(const symbolNid = nodeId(idPath, symbolKey);ops.push({type: 'UpsertEdge',- id: edgeId(idPath, chunkName, symbolKey, 'DEFINES'),+ id: edgeId(idPath, cid, symbolNid, 'DEFINES'),src: cid,dst: symbolNid,predicate: 'DEFINES',@@ -277,11 +314,9 @@ export function buildPatch(if (a.container == null && b.container == null) {const aid = chunkId(idPath, a.chunkKind, a.name, a.lineStart);const bid = chunkId(idPath, b.chunkKind, b.name, b.lineStart);- const aName = a.name ?? `file_body:${a.lineStart}`;- const bName = b.name ?? `file_body:${b.lineStart}`;ops.push({type: 'UpsertEdge',- id: edgeId(idPath, aName, bName, 'NEXT'),+ id: edgeId(idPath, aid, bid, 'NEXT'),src: aid,dst: bid,predicate: 'NEXT',@@ -367,7 +402,7 @@ export function buildPatch(...(multiRepo?.repoId ? { repoId: multiRepo.repoId } : {}),},baseRev: 0,- ops,+ ops: deduplicateUpsertEdges(ops),replaces,intent: `Parsed ${nodePath.basename(filePath)}`,};@@ -543,7 +578,7 @@ export function buildPatchWithResolution(});ops.push({type: 'UpsertEdge',- id: edgeId(idPath, 'file', chunkName, 'CONTAINS_CHUNK'),+ id: edgeId(idPath, fileNodeId2, cid, 'CONTAINS_CHUNK'),src: fileNodeId2,dst: cid,predicate: 'CONTAINS_CHUNK',@@ -554,7 +589,7 @@ export function buildPatchWithResolution(const symbolNid = nodeId(idPath, symbolKey);ops.push({type: 'UpsertEdge',- id: edgeId(idPath, chunkName, symbolKey, 'DEFINES'),+ id: edgeId(idPath, cid, symbolNid, 'DEFINES'),src: cid,dst: symbolNid,predicate: 'DEFINES',@@ -569,11 +604,9 @@ export function buildPatchWithResolution(if (a.container == null && b.container == null) {const aid = chunkId(idPath, a.chunkKind, a.name, a.lineStart);const bid = chunkId(idPath, b.chunkKind, b.name, b.lineStart);- const aName = a.name ?? `file_body:${a.lineStart}`;- const bName = b.name ?? `file_body:${b.lineStart}`;ops.push({type: 'UpsertEdge',- id: edgeId(idPath, aName, bName, 'NEXT'),+ id: edgeId(idPath, aid, bid, 'NEXT'),src: aid,dst: bid,predicate: 'NEXT',@@ -659,7 +692,7 @@ export function buildPatchWithResolution(...(multiRepo?.repoId ? { repoId: multiRepo.repoId } : {}),},baseRev: 0,- ops,+ ops: deduplicateUpsertEdges(ops),replaces,intent: `Parsed ${nodePath.basename(filePath)}`,};diff --git a/ix-cli/src/cli/__tests__/ingest-reconcile.test.ts b/ix-cli/src/cli/__tests__/ingest-reconcile.test.tsindex 54fddc27..fdbef60d 100644--- a/ix-cli/src/cli/__tests__/ingest-reconcile.test.ts+++ b/ix-cli/src/cli/__tests__/ingest-reconcile.test.ts@@ -16,7 +16,7 @@ function patchWith(ops: GraphPatchPayload["ops"]): GraphPatchPayload {source: {uri: "src/example.ts",sourceHash: "next-hash",- extractor: "tree-sitter/1.24",+ extractor: "tree-sitter/1.25",sourceType: "code",workspaceId: "deadbeef",},