From f9afa2d8c474050fca89be4a8de51b45b2610e19 Mon Sep 17 00:00:00 2001 From: carlos-alm Date: Fri, 7 Aug 2026 22:45:38 -0600 Subject: [PATCH 1/2] fix(export): add id-based join fields to file-level exportJSON edges (#2144) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit File-level exportJSON edges use file path strings for source/target, which don't join against this same payload's nodes[].id the way function-level mode's numeric-id edges do. Fixing source/target directly would break existing codegraph export -f json consumers who read them as file paths today, so this adds sourceId/targetId (numeric, matching nodes[].id) alongside the existing fields instead — purely additive, giving consumers the same id-based join function-level mode already supports without changing the existing shape. docs check acknowledged. Impact: 1 functions changed, 2 affected --- src/features/export.ts | 18 ++++++++++++++++-- tests/graph/export.test.ts | 20 ++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/features/export.ts b/src/features/export.ts index c95de7904..a064344cd 100644 --- a/src/features/export.ts +++ b/src/features/export.ts @@ -343,15 +343,29 @@ export function exportJSON( .all() as Array<{ id: number; name: string; kind: string; file: string; line: number }>; if (noTests) nodes = nodes.filter((n) => !isTestFile(n.file)); + // sourceId/targetId (#2144): file-level edges' source/target are file + // path strings for backward compatibility with existing `codegraph + // export -f json` consumers, but that means they don't join against + // this same payload's nodes[].id the way function-level mode's edges + // do. sourceId/targetId are purely additive so a consumer that wants + // the same id-based join function-level mode already supports can use + // them without breaking anyone reading source/target as file paths. let edges = db .prepare(` - SELECT DISTINCT n1.file AS source, n2.file AS target, e.kind, e.confidence + SELECT DISTINCT n1.file AS source, n2.file AS target, n1.id AS sourceId, n2.id AS targetId, e.kind, e.confidence FROM edges e JOIN nodes n1 ON e.source_id = n1.id JOIN nodes n2 ON e.target_id = n2.id WHERE n1.file != n2.file AND e.confidence >= ? `) - .all(minConf) as Array<{ source: string; target: string; kind: string; confidence: number }>; + .all(minConf) as Array<{ + source: string; + target: string; + sourceId: number; + targetId: number; + kind: string; + confidence: number; + }>; if (noTests) edges = edges.filter((e) => !isTestFile(e.source) && !isTestFile(e.target)); const base = { nodes, edges }; diff --git a/tests/graph/export.test.ts b/tests/graph/export.test.ts index 13d2f2495..84a77da06 100644 --- a/tests/graph/export.test.ts +++ b/tests/graph/export.test.ts @@ -287,6 +287,26 @@ describe('exportJSON', () => { expect(functionLevel.nodes.some((n) => n.kind === 'function')).toBe(true); db.close(); }); + + it('file-level edges carry sourceId/targetId that join against nodes[].id (#2144)', () => { + const db = createTestDb(); + const a = insertNode(db, 'src/a.js', 'file', 'src/a.js', 0); + const b = insertNode(db, 'src/b.js', 'file', 'src/b.js', 0); + insertEdge(db, a, b, 'imports'); + + const data = exportJSON(db); + const nodeIds = new Set(data.nodes.map((n) => n.id)); + expect(data.edges.length).toBeGreaterThan(0); + for (const edge of data.edges) { + // source/target remain file path strings for backward compatibility — + // sourceId/targetId are the additive, node-id-based join. + expect(typeof edge.source).toBe('string'); + expect(typeof edge.target).toBe('string'); + expect(nodeIds.has(edge.sourceId)).toBe(true); + expect(nodeIds.has(edge.targetId)).toBe(true); + } + db.close(); + }); }); describe('exportGraphML', () => { From 694478a46d67c5c366d243cb4148b16384c6d2c6 Mon Sep 17 00:00:00 2001 From: carlos-alm Date: Fri, 7 Aug 2026 23:12:57 -0600 Subject: [PATCH 2/2] fix(export): resolve sourceId/targetId against file nodes, not edge endpoints (#2144 review) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The previous fix pulled sourceId/targetId from the edge query's own n1.id/n2.id — but for a cross-file calls/receiver/etc. edge, those are the edge's ACTUAL endpoints (function/class/... nodes), not the file nodes this payload's nodes[] array contains. That both failed to resolve against nodes[].id for the common case and widened this query's DISTINCT aggregation, since different function-pairs between the same two files would produce separate rows instead of collapsing into one file-level edge. Fix: look sourceId/targetId up from the already-fetched file-kind nodes list by file path, entirely outside the SQL query, so the existing DISTINCT aggregation is untouched. null (not omitted) when an endpoint is a directory (contains edges), since directories have no entry in this file-only nodes[] array to resolve against. docs check acknowledged. Impact: 1 functions changed, 2 affected --- src/features/export.ts | 49 +++++++++++++++++++++++++------------- tests/graph/export.test.ts | 49 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 17 deletions(-) diff --git a/src/features/export.ts b/src/features/export.ts index a064344cd..c4117e5ab 100644 --- a/src/features/export.ts +++ b/src/features/export.ts @@ -343,32 +343,47 @@ export function exportJSON( .all() as Array<{ id: number; name: string; kind: string; file: string; line: number }>; if (noTests) nodes = nodes.filter((n) => !isTestFile(n.file)); - // sourceId/targetId (#2144): file-level edges' source/target are file - // path strings for backward compatibility with existing `codegraph - // export -f json` consumers, but that means they don't join against - // this same payload's nodes[].id the way function-level mode's edges - // do. sourceId/targetId are purely additive so a consumer that wants - // the same id-based join function-level mode already supports can use - // them without breaking anyone reading source/target as file paths. let edges = db .prepare(` - SELECT DISTINCT n1.file AS source, n2.file AS target, n1.id AS sourceId, n2.id AS targetId, e.kind, e.confidence + SELECT DISTINCT n1.file AS source, n2.file AS target, e.kind, e.confidence FROM edges e JOIN nodes n1 ON e.source_id = n1.id JOIN nodes n2 ON e.target_id = n2.id WHERE n1.file != n2.file AND e.confidence >= ? `) - .all(minConf) as Array<{ - source: string; - target: string; - sourceId: number; - targetId: number; - kind: string; - confidence: number; - }>; + .all(minConf) as Array<{ source: string; target: string; kind: string; confidence: number }>; if (noTests) edges = edges.filter((e) => !isTestFile(e.source) && !isTestFile(e.target)); - const base = { nodes, edges }; + // sourceId/targetId (#2144): file-level edges' source/target are file + // path strings for backward compatibility with existing `codegraph + // export -f json` consumers, but that means they don't join against + // this same payload's nodes[].id the way function-level mode's edges + // do. sourceId/targetId are purely additive so a consumer that wants + // the same id-based join function-level mode already supports can use + // them without breaking anyone reading source/target as file paths. + // + // Looked up from the already-fetched file-kind `nodes` list (by file + // path) rather than from the edge query's own n1.id/n2.id — those are + // the edge's ACTUAL endpoints, which for a cross-file `calls` edge are + // function/class/etc. nodes, not the file nodes this payload's `nodes` + // array contains. Using them directly would both fail to resolve + // against `nodes[].id` and (worse) widen this query's DISTINCT + // aggregation, since two different function-to-function edges between + // the same file pair would then produce two distinct rows instead of + // collapsing into one. + // + // null (not omitted) when source/target is a directory rather than a + // file — `contains` edges include directory→file containment, and + // directories have no corresponding entry in this file-only `nodes` + // array to resolve against. + const fileNodeIdByPath = new Map(nodes.map((n) => [n.file, n.id])); + const edgesWithIds = edges.map((e) => ({ + ...e, + sourceId: fileNodeIdByPath.get(e.source) ?? null, + targetId: fileNodeIdByPath.get(e.target) ?? null, + })); + + const base = { nodes, edges: edgesWithIds }; return paginateResult(base, 'edges', { limit: opts.limit, offset: opts.offset }) as { nodes: unknown[]; edges: unknown[]; diff --git a/tests/graph/export.test.ts b/tests/graph/export.test.ts index 84a77da06..ba39308c4 100644 --- a/tests/graph/export.test.ts +++ b/tests/graph/export.test.ts @@ -307,6 +307,55 @@ describe('exportJSON', () => { } db.close(); }); + + it("resolves sourceId/targetId to file nodes (not the edge's own function-level endpoints) for a cross-file calls edge, and does not widen DISTINCT aggregation (#2144 review)", () => { + const db = createTestDb(); + const fileA = insertNode(db, 'src/a.js', 'file', 'src/a.js', 0); + const fileB = insertNode(db, 'src/b.js', 'file', 'src/b.js', 0); + // Two DIFFERENT function pairs between the same two files — a file-level + // `calls` edge's actual e.source_id/e.target_id point at these function + // nodes, never at fileA/fileB directly. + const fn1 = insertNode(db, 'doWork', 'function', 'src/a.js', 5); + const fn2 = insertNode(db, 'helper', 'function', 'src/b.js', 10); + const fn3 = insertNode(db, 'doOtherWork', 'function', 'src/a.js', 20); + const fn4 = insertNode(db, 'helper2', 'function', 'src/b.js', 30); + insertEdge(db, fn1, fn2, 'calls'); + insertEdge(db, fn3, fn4, 'calls'); + + const data = exportJSON(db); + const fileLevelEdges = data.edges.filter( + (e) => e.source === 'src/a.js' && e.target === 'src/b.js', + ); + // Both function-pair edges collapse into a single file-level row — + // asserting this BEFORE checking sourceId/targetId, since a regression + // that widens DISTINCT would otherwise still "pass" a naive per-edge check. + expect(fileLevelEdges).toHaveLength(1); + expect(fileLevelEdges[0]?.sourceId).toBe(fileA); + expect(fileLevelEdges[0]?.targetId).toBe(fileB); + // Must NOT be the raw function-node ids the edge itself points at. + expect(fileLevelEdges[0]?.sourceId).not.toBe(fn1); + expect(fileLevelEdges[0]?.sourceId).not.toBe(fn3); + expect(fileLevelEdges[0]?.targetId).not.toBe(fn2); + expect(fileLevelEdges[0]?.targetId).not.toBe(fn4); + db.close(); + }); + + it('sourceId is null (not a directory-node id) for a contains edge whose source is a directory (#2144 review)', () => { + const db = createTestDb(); + const dir = insertNode(db, 'src', 'directory', 'src', 0); + const fileA = insertNode(db, 'src/a.js', 'file', 'src/a.js', 0); + insertEdge(db, dir, fileA, 'contains'); + + const data = exportJSON(db); + const edge = data.edges.find((e) => e.source === 'src' && e.target === 'src/a.js'); + expect(edge).toBeDefined(); + // Directories have no entry in this file-only nodes[] array, so their + // id can never be a meaningful join target — null makes that explicit + // rather than silently resolving to something wrong (or being omitted). + expect(edge?.sourceId).toBeNull(); + expect(edge?.targetId).toBe(fileA); + db.close(); + }); }); describe('exportGraphML', () => {