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
perf: eliminate O(n²) bottlenecks in first-ingest pipeline
Tanner Kocherf1396a6d4e47
diff --git a/core-ingestion/dist/index.d.ts b/core-ingestion/dist/index.d.tsindex 5e611570..1a7c51d5 100644--- a/core-ingestion/dist/index.d.ts+++ b/core-ingestion/dist/index.d.ts@@ -68,6 +68,20 @@ export interface ResolvedEdge {* - Unambiguous class method: dstQualifiedKey === 'ClassName.method'* - Ambiguous (two entities share the same plain name): edge not emitted*/+/** Stats collected during resolution (exposed for debug instrumentation). */+export interface ResolveStats {+ importLookups: number;+ transitiveLookups: number;+ globalFallbacks: number;+ globalCandidateTotal: number;+ resolvedImport: number;+ resolvedTransitive: number;+ resolvedGlobal: number;+ resolvedQualifier: number;+ skippedSameFile: number;+ skippedAmbiguous: number;+}export declare function resolveEdges(results: FileParseResult[]): ResolvedEdge[];+export declare function resolveEdges(results: FileParseResult[], statsOut: ResolveStats): ResolvedEdge[];/** @deprecated Use resolveEdges instead. */export declare function resolveCallEdges(results: FileParseResult[]): ResolvedCallEdge[];diff --git a/core-ingestion/dist/index.js b/core-ingestion/dist/index.jsindex adfb2758..712b01de 100644--- a/core-ingestion/dist/index.js+++ b/core-ingestion/dist/index.js@@ -129,6 +129,17 @@ function getParser() {_parser = new Parser();return _parser;}+// Per-process cache for compiled tree-sitter queries, keyed by language.+// Avoids recompiling the same query string for every file of the same language.+const _queryCache = new Map();+function getCachedQuery(language, grammar, querySource) {+ const cached = _queryCache.get(language);+ if (cached && cached.grammar === grammar)+ return cached.query;+ const query = new Parser.Query(grammar, querySource);+ _queryCache.set(language, { grammar, query });+ return query;+}// ---------------------------------------------------------------------------// Main parse function// ---------------------------------------------------------------------------@@ -148,12 +159,18 @@ export function parseFile(filePath, source) {const parser = getParser();parser.setLanguage(grammar);const tree = parser.parse(source);- const query = new Parser.Query(grammar, queries);+ const cacheKey = isTsx ? 'tsx' : language;+ const query = getCachedQuery(cacheKey, grammar, queries);const matches = query.matches(tree.rootNode);const fileName = nodePath.basename(filePath);- const lines = source.split('\n');+ // Count newlines instead of allocating a full string array+ let lineCount = 1;+ for (let i = 0; i < source.length; i++) {+ if (source.charCodeAt(i) === 10)+ lineCount++;+ }const entities = [- { name: fileName, kind: 'file', lineStart: 1, lineEnd: lines.length, language },+ { name: fileName, kind: 'file', lineStart: 1, lineEnd: lineCount, language },];const relationships = [];// Track class ranges for containment: [name, startLine, endLine]@@ -259,7 +276,7 @@ export function parseFile(filePath, source) {.replace(/^["'`]|["'`]$/g, '') // strip quotes.replace(/\\\\/g, '/'); // normalise backslashesif (importPath.length > 0 && importPath !== '*') {- const rawMod = importPath.split('/').filter(s => s !== '*').pop() ?? importPath;+ const rawMod = importPath.split('/').filter((s) => s !== '*').pop() ?? importPath;const modName = rawMod.replace(/^\.+/, ''); // strip leading dots: '.models' → 'models'if (!modName)continue; // skip bare '.' relative imports@@ -350,33 +367,18 @@ function bestQKey(fileQKeys, filePath, plainName) {const qks = [...new Set(fileQKeys.get(filePath)?.get(plainName) ?? [])];return qks.length === 1 ? qks[0] : null;}-/**- * Resolves CALLS and EXTENDS relationships to their cross-file targets by- * building a symbol table and import map over the full batch of parsed files.- *- * Tiers (in priority order):- * 0.9 — import-scoped: dst is in a file the src explicitly imports- * 0.8 — transitive import-scoped: one re-export hop away- * 0.5 — global fallback: dst exists in exactly one other file (unambiguous)- *- * Same-file edges are already handled correctly by buildPatch and are not- * emitted here.- *- * Import resolution handles:- * - Stem-based: './payments' → 'payments.ts'- * - Path-aliased: '@/lib/payments' → 'payments.ts' (last segment)- * - Directory index: './services' → 'services/index.ts'- * - Bare aliases: '@components' → 'components.ts' (strip leading non-word chars)- * - Dotted paths: 'ix.memory.model.Edge' → stem 'Edge' → 'Edge.scala'- * (handles Scala/Java package imports where tree-sitter- * emits each identifier separately)- *- * Qualified-key resolution:- * - Module-level function/class: dstQualifiedKey === dstName- * - Unambiguous class method: dstQualifiedKey === 'ClassName.method'- * - Ambiguous (two entities share the same plain name): edge not emitted- */-export function resolveEdges(results) {+export function resolveEdges(results, statsOut) {+ const stats = statsOut ?? {+ importLookups: 0, transitiveLookups: 0, globalFallbacks: 0,+ globalCandidateTotal: 0, resolvedImport: 0, resolvedTransitive: 0,+ resolvedGlobal: 0, resolvedQualifier: 0, skippedSameFile: 0, skippedAmbiguous: 0,+ };+ // ── Index structures (built once) ──────────────────────────────────+ // Direct file lookup: filePath → FileParseResult (replaces results.find())+ const resultByPath = new Map();+ for (const r of results) {+ resultByPath.set(r.filePath, r);+ }// fileQKeys: filePath → (plainName → qualifiedKeys[])// Mirrors the entityQKey computation in buildPatch so nodeIds match exactly.const fileQKeys = new Map();@@ -397,6 +399,24 @@ export function resolveEdges(results) {for (const [fp, qkMap] of fileQKeys) {fileHasSymbol.set(fp, new Set(qkMap.keys()));}+ // Precompute language per file (avoids calling languageFromPath in hot loops)+ const fileLanguage = new Map();+ for (const r of results) {+ fileLanguage.set(r.filePath, r.language);+ }+ // Inverted symbol index: symbolName → Array<{filePath, language}>+ // Replaces the O(N) global fallback scan with O(candidates) lookup.+ const symbolToFiles = new Map();+ for (const [fp, symbols] of fileHasSymbol) {+ for (const sym of symbols) {+ let arr = symbolToFiles.get(sym);+ if (!arr) {+ arr = [];+ symbolToFiles.set(sym, arr);+ }+ arr.push(fp);+ }+ }// stemToFiles: basename-without-extension → filePath[]// Matches the last path segment of an import to files in the batch.const stemToFiles = new Map();@@ -438,6 +458,7 @@ export function resolveEdges(results) {packageToFiles.set(pkg, list);}}+ // ── Helpers ────────────────────────────────────────────────────────/** Resolve a module name to matching file paths in the batch. */function modNameToFiles(modName, excludeFp) {const fps = [];@@ -475,10 +496,12 @@ export function resolveEdges(results) {}return fps;}+ // ── Main resolution loop ───────────────────────────────────────────const resolved = [];for (const result of results) {const srcFilePath = result.filePath;const srcSymbols = fileHasSymbol.get(srcFilePath);+ const srcLanguage = result.language;// Build the set of file paths this file explicitly imports.const importedFilePaths = new Set();for (const rel of result.relationships) {@@ -492,7 +515,7 @@ export function resolveEdges(results) {// Handles re-exports: baz.ts → index.ts (re-exports from bar.ts) → bar.tsconst transitiveFilePaths = new Set();for (const fp of importedFilePaths) {- const fpResult = results.find(r => r.filePath === fp);+ const fpResult = resultByPath.get(fp); // O(1) instead of results.find()if (!fpResult)continue;for (const rel of fpResult.relationships) {@@ -533,6 +556,7 @@ export function resolveEdges(results) {if (dstQualifiedKey === null)continue;resolved.push({ srcFilePath, srcName: rel.srcName, dstFilePath: fp, dstName, dstQualifiedKey, predicate: 'IMPORTS', confidence: 0.9 });+ stats.resolvedImport++;continue;}const dstName = rel.dstName;@@ -559,22 +583,23 @@ export function resolveEdges(results) {if (dstQualifiedKey !== null) {// dstName must match rel.dstName so buildPatchWithResolution can look it upresolved.push({ srcFilePath, srcName, dstFilePath: qfp, dstName, dstQualifiedKey, predicate: rel.predicate, confidence: 0.9 });+ stats.resolvedQualifier++;}}continue;}- // Global qualifier fallback- const qualGlobalMatches = [];- for (const [fp, symbols] of fileHasSymbol) {- if (fp !== srcFilePath && symbols.has(qualifierPart))- qualGlobalMatches.push(fp);- }+ // Global qualifier fallback — use inverted index instead of scanning all files+ const qualCandidates = symbolToFiles.get(qualifierPart) ?? [];+ const qualGlobalMatches = qualCandidates.filter(fp => fp !== srcFilePath);+ stats.globalFallbacks++;+ stats.globalCandidateTotal += qualGlobalMatches.length;if (qualGlobalMatches.length === 1) {const qfp = qualGlobalMatches[0];if (fileHasSymbol.get(qfp)?.has(memberPart)) {const dstQualifiedKey = bestQKey(fileQKeys, qfp, memberPart);if (dstQualifiedKey !== null) {resolved.push({ srcFilePath, srcName, dstFilePath: qfp, dstName, dstQualifiedKey, predicate: rel.predicate, confidence: 0.7 });+ stats.resolvedQualifier++;}}}@@ -582,9 +607,12 @@ export function resolveEdges(results) {}}// Tier 1: same-file — already correct in buildPatch, skip here- if (srcSymbols.has(dstName))+ if (srcSymbols.has(dstName)) {+ stats.skippedSameFile++;continue;+ }// Tier 2: import-scoped (confidence 0.9)+ stats.importLookups++;const importMatches = [];for (const fp of importedFilePaths) {if (fileHasSymbol.get(fp)?.has(dstName))@@ -596,11 +624,15 @@ export function resolveEdges(results) {if (dstQualifiedKey === null)continue; // ambiguous — do not emit bad nodeIdresolved.push({ srcFilePath, srcName, dstFilePath: fp, dstName, dstQualifiedKey, predicate: rel.predicate, confidence: 0.9 });+ stats.resolvedImport++;continue;}- if (importMatches.length > 1)- continue; // ambiguous file — do not emit+ if (importMatches.length > 1) {+ stats.skippedAmbiguous++;+ continue;+ } // ambiguous file — do not emit// Tier 2.5: transitive import-scoped (confidence 0.8) — one re-export hop away+ stats.transitiveLookups++;const transitiveMatches = [];for (const fp of transitiveFilePaths) {if (fileHasSymbol.get(fp)?.has(dstName))@@ -612,24 +644,26 @@ export function resolveEdges(results) {if (dstQualifiedKey === null)continue;resolved.push({ srcFilePath, srcName, dstFilePath: fp, dstName, dstQualifiedKey, predicate: rel.predicate, confidence: 0.8 });+ stats.resolvedTransitive++;continue;}- if (transitiveMatches.length > 1)- continue; // ambiguous — do not emit- // Tier 3: global fallback (confidence 0.5) — exactly one other file defines it- // Only match files in the same language as the caller to avoid cross-language false positives.- const srcLanguage = result.language;- const globalMatches = [];- for (const [fp, symbols] of fileHasSymbol) {- if (fp !== srcFilePath && symbols.has(dstName) && languageFromPath(fp) === srcLanguage)- globalMatches.push(fp);- }+ if (transitiveMatches.length > 1) {+ stats.skippedAmbiguous++;+ continue;+ } // ambiguous — do not emit+ // Tier 3: global fallback (confidence 0.5) — uses inverted symbol index+ // instead of scanning all files.+ stats.globalFallbacks++;+ const candidates = symbolToFiles.get(dstName) ?? [];+ const globalMatches = candidates.filter(fp => fp !== srcFilePath && fileLanguage.get(fp) === srcLanguage);+ stats.globalCandidateTotal += globalMatches.length;if (globalMatches.length === 1) {const fp = globalMatches[0];const dstQualifiedKey = bestQKey(fileQKeys, fp, dstName);if (dstQualifiedKey === null)continue; // ambiguous — do not emit bad nodeIdresolved.push({ srcFilePath, srcName, dstFilePath: fp, dstName, dstQualifiedKey, predicate: rel.predicate, confidence: 0.5 });+ stats.resolvedGlobal++;}// 0 or >1 global matches — leave as dangling edge, do not emit}diff --git a/core-ingestion/dist/patch-builder.js b/core-ingestion/dist/patch-builder.jsindex 7e72642c..684c9d48 100644--- a/core-ingestion/dist/patch-builder.js+++ b/core-ingestion/dist/patch-builder.js@@ -166,14 +166,16 @@ export function buildPatch(result, sourceHash, previousSourceHash) {// ---------------------------------------------------------------------------export function buildPatchWithResolution(result, sourceHash, resolvedEdges, previousSourceHash) {// Build lookup: `${predicate}:${dstName}` → { dstFilePath, dstQualifiedKey }+ // Callers should pass only edges for this file (pre-grouped) for best performance,+ // but we still tolerate the full array for backward compatibility.const edgeResolution = new Map();for (const edge of resolvedEdges) {- if (edge.srcFilePath === result.filePath) {- edgeResolution.set(`${edge.predicate}:${edge.dstName}`, {- dstFilePath: edge.dstFilePath,- dstQualifiedKey: edge.dstQualifiedKey,- });- }+ if (edge.srcFilePath !== result.filePath)+ continue;+ edgeResolution.set(`${edge.predicate}:${edge.dstName}`, {+ dstFilePath: edge.dstFilePath,+ dstQualifiedKey: edge.dstQualifiedKey,+ });}const { filePath, entities, relationships } = result;const ops = [];diff --git a/core-ingestion/src/index.ts b/core-ingestion/src/index.tsindex 506c90ce..a541d383 100644--- a/core-ingestion/src/index.ts+++ b/core-ingestion/src/index.ts@@ -166,6 +166,18 @@ function getParser(): Parser {return _parser;}+// Per-process cache for compiled tree-sitter queries, keyed by language.+// Avoids recompiling the same query string for every file of the same language.+const _queryCache = new Map<SupportedLanguages | 'tsx', { grammar: any; query: any }>();++function getCachedQuery(language: SupportedLanguages | 'tsx', grammar: any, querySource: string): any {+ const cached = _queryCache.get(language);+ if (cached && cached.grammar === grammar) return cached.query;+ const query = new Parser.Query(grammar, querySource);+ _queryCache.set(language, { grammar, query });+ return query;+}+// ---------------------------------------------------------------------------// Main parse function// ---------------------------------------------------------------------------@@ -186,14 +198,17 @@ export function parseFile(filePath: string, source: string): FileParseResult | nconst parser = getParser();parser.setLanguage(grammar);const tree = parser.parse(source);- const query = new Parser.Query(grammar, queries);+ const cacheKey = isTsx ? 'tsx' as const : language;+ const query = getCachedQuery(cacheKey, grammar, queries);const matches = query.matches(tree.rootNode);const fileName = nodePath.basename(filePath);- const lines = source.split('\n');+ // Count newlines instead of allocating a full string array+ let lineCount = 1;+ for (let i = 0; i < source.length; i++) { if (source.charCodeAt(i) === 10) lineCount++; }const entities: ParsedEntity[] = [- { name: fileName, kind: 'file', lineStart: 1, lineEnd: lines.length, language },+ { name: fileName, kind: 'file', lineStart: 1, lineEnd: lineCount, language },];const relationships: ParsedRelationship[] = [];@@ -315,7 +330,7 @@ export function parseFile(filePath: string, source: string): FileParseResult | n.replace(/^["'`]|["'`]$/g, '') // strip quotes.replace(/\\\\/g, '/'); // normalise backslashesif (importPath.length > 0 && importPath !== '*') {- const rawMod = importPath.split('/').filter(s => s !== '*').pop() ?? importPath;+ const rawMod = importPath.split('/').filter((s: string) => s !== '*').pop() ?? importPath;const modName = rawMod.replace(/^\.+/, ''); // strip leading dots: '.models' → 'models'if (!modName) continue; // skip bare '.' relative importsentities.push({ name: modName, kind: 'module', lineStart: importSource.node.startPosition.row + 1, lineEnd: importSource.node.startPosition.row + 1, language });@@ -470,7 +485,37 @@ function bestQKey(* - Unambiguous class method: dstQualifiedKey === 'ClassName.method'* - Ambiguous (two entities share the same plain name): edge not emitted*/-export function resolveEdges(results: FileParseResult[]): ResolvedEdge[] {+/** Stats collected during resolution (exposed for debug instrumentation). */+export interface ResolveStats {+ importLookups: number;+ transitiveLookups: number;+ globalFallbacks: number;+ globalCandidateTotal: number;+ resolvedImport: number;+ resolvedTransitive: number;+ resolvedGlobal: number;+ resolvedQualifier: number;+ skippedSameFile: number;+ skippedAmbiguous: number;+}++export function resolveEdges(results: FileParseResult[]): ResolvedEdge[];+export function resolveEdges(results: FileParseResult[], statsOut: ResolveStats): ResolvedEdge[];+export function resolveEdges(results: FileParseResult[], statsOut?: ResolveStats): ResolvedEdge[] {+ const stats: ResolveStats = statsOut ?? {+ importLookups: 0, transitiveLookups: 0, globalFallbacks: 0,+ globalCandidateTotal: 0, resolvedImport: 0, resolvedTransitive: 0,+ resolvedGlobal: 0, resolvedQualifier: 0, skippedSameFile: 0, skippedAmbiguous: 0,+ };++ // ── Index structures (built once) ──────────────────────────────────++ // Direct file lookup: filePath → FileParseResult (replaces results.find())+ const resultByPath = new Map<string, FileParseResult>();+ for (const r of results) {+ resultByPath.set(r.filePath, r);+ }+// fileQKeys: filePath → (plainName → qualifiedKeys[])// Mirrors the entityQKey computation in buildPatch so nodeIds match exactly.const fileQKeys = new Map<string, Map<string, string[]>>();@@ -492,6 +537,23 @@ export function resolveEdges(results: FileParseResult[]): ResolvedEdge[] {fileHasSymbol.set(fp, new Set(qkMap.keys()));}+ // Precompute language per file (avoids calling languageFromPath in hot loops)+ const fileLanguage = new Map<string, SupportedLanguages | null>();+ for (const r of results) {+ fileLanguage.set(r.filePath, r.language);+ }++ // Inverted symbol index: symbolName → Array<{filePath, language}>+ // Replaces the O(N) global fallback scan with O(candidates) lookup.+ const symbolToFiles = new Map<string, Array<string>>();+ for (const [fp, symbols] of fileHasSymbol) {+ for (const sym of symbols) {+ let arr = symbolToFiles.get(sym);+ if (!arr) { arr = []; symbolToFiles.set(sym, arr); }+ arr.push(fp);+ }+ }+// stemToFiles: basename-without-extension → filePath[]// Matches the last path segment of an import to files in the batch.const stemToFiles = new Map<string, string[]>();@@ -534,6 +596,8 @@ export function resolveEdges(results: FileParseResult[]): ResolvedEdge[] {}}+ // ── Helpers ────────────────────────────────────────────────────────+/** Resolve a module name to matching file paths in the batch. */function modNameToFiles(modName: string, excludeFp: string): string[] {const fps: string[] = [];@@ -566,11 +630,14 @@ export function resolveEdges(results: FileParseResult[]): ResolvedEdge[] {return fps;}+ // ── Main resolution loop ───────────────────────────────────────────+const resolved: ResolvedEdge[] = [];for (const result of results) {const srcFilePath = result.filePath;const srcSymbols = fileHasSymbol.get(srcFilePath)!;+ const srcLanguage = result.language;// Build the set of file paths this file explicitly imports.const importedFilePaths = new Set<string>();@@ -585,7 +652,7 @@ export function resolveEdges(results: FileParseResult[]): ResolvedEdge[] {// Handles re-exports: baz.ts → index.ts (re-exports from bar.ts) → bar.tsconst transitiveFilePaths = new Set<string>();for (const fp of importedFilePaths) {- const fpResult = results.find(r => r.filePath === fp);+ const fpResult = resultByPath.get(fp); // O(1) instead of results.find()if (!fpResult) continue;for (const rel of fpResult.relationships) {if (rel.predicate !== 'IMPORTS') continue;@@ -619,6 +686,7 @@ export function resolveEdges(results: FileParseResult[]): ResolvedEdge[] {const dstQualifiedKey = bestQKey(fileQKeys, fp, entityName);if (dstQualifiedKey === null) continue;resolved.push({ srcFilePath, srcName: rel.srcName, dstFilePath: fp, dstName, dstQualifiedKey, predicate: 'IMPORTS', confidence: 0.9 });+ stats.resolvedImport++;continue;}@@ -646,21 +714,23 @@ export function resolveEdges(results: FileParseResult[]): ResolvedEdge[] {if (dstQualifiedKey !== null) {// dstName must match rel.dstName so buildPatchWithResolution can look it upresolved.push({ srcFilePath, srcName, dstFilePath: qfp, dstName, dstQualifiedKey, predicate: rel.predicate, confidence: 0.9 });+ stats.resolvedQualifier++;}}continue;}- // Global qualifier fallback- const qualGlobalMatches: string[] = [];- for (const [fp, symbols] of fileHasSymbol) {- if (fp !== srcFilePath && symbols.has(qualifierPart)) qualGlobalMatches.push(fp);- }+ // Global qualifier fallback — use inverted index instead of scanning all files+ const qualCandidates = symbolToFiles.get(qualifierPart) ?? [];+ const qualGlobalMatches = qualCandidates.filter(fp => fp !== srcFilePath);+ stats.globalFallbacks++;+ stats.globalCandidateTotal += qualGlobalMatches.length;if (qualGlobalMatches.length === 1) {const qfp = qualGlobalMatches[0];if (fileHasSymbol.get(qfp)?.has(memberPart)) {const dstQualifiedKey = bestQKey(fileQKeys, qfp, memberPart);if (dstQualifiedKey !== null) {resolved.push({ srcFilePath, srcName, dstFilePath: qfp, dstName, dstQualifiedKey, predicate: rel.predicate, confidence: 0.7 });+ stats.resolvedQualifier++;}}}@@ -669,9 +739,10 @@ export function resolveEdges(results: FileParseResult[]): ResolvedEdge[] {}// Tier 1: same-file — already correct in buildPatch, skip here- if (srcSymbols.has(dstName)) continue;+ if (srcSymbols.has(dstName)) { stats.skippedSameFile++; continue; }// Tier 2: import-scoped (confidence 0.9)+ stats.importLookups++;const importMatches: string[] = [];for (const fp of importedFilePaths) {if (fileHasSymbol.get(fp)?.has(dstName)) importMatches.push(fp);@@ -682,11 +753,13 @@ export function resolveEdges(results: FileParseResult[]): ResolvedEdge[] {const dstQualifiedKey = bestQKey(fileQKeys, fp, dstName);if (dstQualifiedKey === null) continue; // ambiguous — do not emit bad nodeIdresolved.push({ srcFilePath, srcName, dstFilePath: fp, dstName, dstQualifiedKey, predicate: rel.predicate, confidence: 0.9 });+ stats.resolvedImport++;continue;}- if (importMatches.length > 1) continue; // ambiguous file — do not emit+ if (importMatches.length > 1) { stats.skippedAmbiguous++; continue; } // ambiguous file — do not emit// Tier 2.5: transitive import-scoped (confidence 0.8) — one re-export hop away+ stats.transitiveLookups++;const transitiveMatches: string[] = [];for (const fp of transitiveFilePaths) {if (fileHasSymbol.get(fp)?.has(dstName)) transitiveMatches.push(fp);@@ -697,23 +770,24 @@ export function resolveEdges(results: FileParseResult[]): ResolvedEdge[] {const dstQualifiedKey = bestQKey(fileQKeys, fp, dstName);if (dstQualifiedKey === null) continue;resolved.push({ srcFilePath, srcName, dstFilePath: fp, dstName, dstQualifiedKey, predicate: rel.predicate, confidence: 0.8 });+ stats.resolvedTransitive++;continue;}- if (transitiveMatches.length > 1) continue; // ambiguous — do not emit-- // Tier 3: global fallback (confidence 0.5) — exactly one other file defines it- // Only match files in the same language as the caller to avoid cross-language false positives.- const srcLanguage = result.language;- const globalMatches: string[] = [];- for (const [fp, symbols] of fileHasSymbol) {- if (fp !== srcFilePath && symbols.has(dstName) && languageFromPath(fp) === srcLanguage) globalMatches.push(fp);- }+ if (transitiveMatches.length > 1) { stats.skippedAmbiguous++; continue; } // ambiguous — do not emit++ // Tier 3: global fallback (confidence 0.5) — uses inverted symbol index+ // instead of scanning all files.+ stats.globalFallbacks++;+ const candidates = symbolToFiles.get(dstName) ?? [];+ const globalMatches = candidates.filter(fp => fp !== srcFilePath && fileLanguage.get(fp) === srcLanguage);+ stats.globalCandidateTotal += globalMatches.length;if (globalMatches.length === 1) {const fp = globalMatches[0];const dstQualifiedKey = bestQKey(fileQKeys, fp, dstName);if (dstQualifiedKey === null) continue; // ambiguous — do not emit bad nodeIdresolved.push({ srcFilePath, srcName, dstFilePath: fp, dstName, dstQualifiedKey, predicate: rel.predicate, confidence: 0.5 });+ stats.resolvedGlobal++;}// 0 or >1 global matches — leave as dangling edge, do not emit}diff --git a/core-ingestion/src/patch-builder.ts b/core-ingestion/src/patch-builder.tsindex c49ca36b..09b5941b 100644--- a/core-ingestion/src/patch-builder.ts+++ b/core-ingestion/src/patch-builder.ts@@ -197,14 +197,15 @@ export function buildPatchWithResolution(previousSourceHash?: string,): GraphPatchPayload {// Build lookup: `${predicate}:${dstName}` → { dstFilePath, dstQualifiedKey }+ // Callers should pass only edges for this file (pre-grouped) for best performance,+ // but we still tolerate the full array for backward compatibility.const edgeResolution = new Map<string, { dstFilePath: string; dstQualifiedKey: string }>();for (const edge of resolvedEdges) {- if (edge.srcFilePath === result.filePath) {- edgeResolution.set(`${edge.predicate}:${edge.dstName}`, {- dstFilePath: edge.dstFilePath,- dstQualifiedKey: edge.dstQualifiedKey,- });- }+ if (edge.srcFilePath !== result.filePath) continue;+ edgeResolution.set(`${edge.predicate}:${edge.dstName}`, {+ dstFilePath: edge.dstFilePath,+ dstQualifiedKey: edge.dstQualifiedKey,+ });}const { filePath, entities, relationships } = result;diff --git a/ix-cli/src/cli/commands/ingest.ts b/ix-cli/src/cli/commands/ingest.tsindex aa0c866a..9802e011 100644--- a/ix-cli/src/cli/commands/ingest.ts+++ b/ix-cli/src/cli/commands/ingest.ts@@ -122,6 +122,11 @@ export async function ingestFiles(// Phase timing marksconst timings = { discoverMs: 0, hashMs: 0, parseMs: 0, resolveMs: 0, commitMs: 0 };+ const resolveStats = {+ importLookups: 0, transitiveLookups: 0, globalFallbacks: 0,+ globalCandidateTotal: 0, resolvedImport: 0, resolvedTransitive: 0,+ resolvedGlobal: 0, resolvedQualifier: 0, skippedSameFile: 0, skippedAmbiguous: 0,+ };try {// Phase: discover files@@ -179,15 +184,24 @@ export async function ingestFiles(timings.parseMs = Math.round(parsed - hashed);// Phase: cross-file CALLS + EXTENDS resolution over the full batch- const resolvedEdges = resolveEdges(parsedFiles.map(f => f.parsed));+ const resolvedEdges = (resolveEdges as Function)(parsedFiles.map(f => f.parsed), resolveStats);const resolved = performance.now();timings.resolveMs = Math.round(resolved - parsed);+ // Pre-group resolved edges by source file for O(1) lookup in patch building+ const edgesByFile = new Map<string, typeof resolvedEdges>();+ for (const edge of resolvedEdges) {+ let arr = edgesByFile.get(edge.srcFilePath);+ if (!arr) { arr = []; edgesByFile.set(edge.srcFilePath, arr); }+ arr.push(edge);+ }+ const emptyEdges: typeof resolvedEdges = [];+// Phase: build patches and bulk commitconst patches: GraphPatchPayload[] = [];for (const { parsed: p, hash, previousHash } of parsedFiles) {try {- patches.push(buildPatchWithResolution(p, hash, resolvedEdges, previousHash));+ patches.push(buildPatchWithResolution(p, hash, edgesByFile.get(p.filePath) ?? emptyEdges, previousHash));} catch (err) {parseErrors++;process.stderr.write(`\n [patch build error] ${p.filePath}: ${err}\n`);@@ -237,6 +251,7 @@ export async function ingestFiles(skipReasons: { unchanged: filesSkipped, emptyFile: 0, parseError: parseErrors, tooLarge },elapsedSeconds: parseFloat(elapsed),timings,+ resolveStats,}, null, 2));} else {console.log(chalk.bold('\nIngest summary'));@@ -261,6 +276,17 @@ export async function ingestFiles(console.log(chalk.dim(` parse: ${timings.parseMs}ms`));console.log(chalk.dim(` resolve: ${timings.resolveMs}ms`));console.log(chalk.dim(` commit: ${timings.commitMs}ms`));+ console.log(chalk.dim(`\n Resolve stats:`));+ console.log(chalk.dim(` import lookups: ${resolveStats.importLookups}`));+ console.log(chalk.dim(` transitive lookups: ${resolveStats.transitiveLookups}`));+ console.log(chalk.dim(` global fallbacks: ${resolveStats.globalFallbacks}`));+ console.log(chalk.dim(` avg candidates: ${resolveStats.globalFallbacks > 0 ? (resolveStats.globalCandidateTotal / resolveStats.globalFallbacks).toFixed(1) : '0'}`));+ console.log(chalk.dim(` resolved import: ${resolveStats.resolvedImport}`));+ console.log(chalk.dim(` resolved transitive:${resolveStats.resolvedTransitive}`));+ console.log(chalk.dim(` resolved global: ${resolveStats.resolvedGlobal}`));+ console.log(chalk.dim(` resolved qualifier: ${resolveStats.resolvedQualifier}`));+ console.log(chalk.dim(` skipped same-file: ${resolveStats.skippedSameFile}`));+ console.log(chalk.dim(` skipped ambiguous: ${resolveStats.skippedAmbiguous}`));}console.log();