Skip to content

armgit / Ix

Public Imported from GitHub
0

Understand any codebase instantly. System intelligence for codebases, built for humans and AI.

feat: refine TypeScript parser, improve ingest performance, and adjust map output

Joseph Mikhail7258e0d9cd2c
diff --git a/core-ingestion/dist/index.d.ts b/core-ingestion/dist/index.d.ts
index d24611fc..7ec62b6b 100644
--- a/core-ingestion/dist/index.d.ts
+++ b/core-ingestion/dist/index.d.ts
@@ -85,8 +85,7 @@ 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 {
+export declare function resolveEdges(results: FileParseResult[], stats?: {
importLookups: number;
transitiveLookups: number;
globalFallbacks: number;
@@ -97,8 +96,6 @@ export interface ResolveStats {
resolvedQualifier: number;
skippedSameFile: number;
skippedAmbiguous: number;
-}
-export declare function resolveEdges(results: FileParseResult[]): ResolvedEdge[];
-export declare function resolveEdges(results: FileParseResult[], statsOut: ResolveStats): ResolvedEdge[];
+}): 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.js
index 33d01b76..fca896cb 100644
--- a/core-ingestion/dist/index.js
+++ b/core-ingestion/dist/index.js
@@ -76,7 +76,7 @@ const DEFINITION_KIND_MAP = {
'definition.property': 'function',
'definition.const': 'function',
'definition.static': 'function',
- 'definition.macro': 'function',
+ 'definition.macro': 'macro',
'definition.union': 'class',
'definition.typedef': 'class',
'definition.template': 'class',
@@ -168,6 +168,7 @@ export function isGrammarSupported(filePath) {
// Parser instance (reused across calls)
// ---------------------------------------------------------------------------
let _parser = null;
+let _currentGrammar = null;
const _queryCache = new Map();
function getParser() {
if (!_parser)
@@ -182,6 +183,36 @@ function getCachedQuery(language, grammar, querySource) {
_queryCache.set(language, { grammar, query });
return query;
}
+function normalizeCapturedImport(rawValue, language) {
+ const trimmed = rawValue.trim().replace(/\\\\/g, '/');
+ const unwrapped = trimmed
+ .replace(/^["'`<]/, '')
+ .replace(/[>"'`]$/, '');
+ if (language === SupportedLanguages.C || language === SupportedLanguages.CPlusPlus) {
+ return unwrapped.replace(/^\.\/+/, '');
+ }
+ const rawMod = unwrapped.split('/').filter((s) => s !== '*').pop() ?? unwrapped;
+ return rawMod.replace(/^\.+/, '');
+}
+function fileEntityName(filePath) {
+ return nodePath.basename(filePath);
+}
+function looksLikeCppHeader(source) {
+ return /\bclass\s+[A-Za-z_]\w*\b/.test(source)
+ || /\bnamespace\s+[A-Za-z_]\w*\b/.test(source)
+ || /\btemplate\s*</.test(source)
+ || /\busing\s+namespace\b/.test(source)
+ || /\b(public|private|protected)\s*:/.test(source)
+ || /\b(virtual|constexpr|typename|friend|operator)\b/.test(source)
+ || /\b[A-Za-z_]\w*::[A-Za-z_]\w*/.test(source);
+}
+function detectLanguageForSource(filePath, source) {
+ const language = languageFromPath(filePath);
+ if (language === SupportedLanguages.C && filePath.toLowerCase().endsWith('.h') && looksLikeCppHeader(source)) {
+ return SupportedLanguages.CPlusPlus;
+ }
+ return language;
+}
// ---------------------------------------------------------------------------
// Rust: cfg macro unwrapping
// ---------------------------------------------------------------------------
@@ -193,43 +224,57 @@ function getCachedQuery(language, grammar, querySource) {
* preserving every character position and line number so that entity line
* numbers remain accurate.
*/
+// Pre-compiled for blanking macro headers (preserves newlines, replaces all else with space)
+const _blankNonNewline = /[^\n]/g;
function unwrapRustCfgMacros(source) {
const re = /\bcfg_(?:rt_multi_thread|not_rt|rt|io|time|sync|net|fs|process|signal)!\s*\{/g;
- const chars = source.split('');
+ // Collect edit regions before mutating anything
+ const edits = []; // [macroStart, openBrace, closeBrace]
let m;
while ((m = re.exec(source)) !== null) {
- const openBrace = m.index + m[0].length - 1; // position of the `{`
- // Find matching closing brace by counting depth
+ const openBrace = m.index + m[0].length - 1;
let depth = 0;
let closeBrace = -1;
for (let i = openBrace; i < source.length; i++) {
if (source[i] === '{')
depth++;
else if (source[i] === '}') {
- depth--;
- if (depth === 0) {
+ if (--depth === 0) {
closeBrace = i;
break;
}
}
}
- if (closeBrace === -1)
- continue;
- // Blank out everything from start of macro name up to and including `{`
- for (let i = m.index; i <= openBrace; i++) {
- if (chars[i] !== '\n')
- chars[i] = ' ';
- }
- // Blank out the matching closing `}`
- chars[closeBrace] = ' ';
+ if (closeBrace !== -1)
+ edits.push([m.index, openBrace, closeBrace]);
}
- return chars.join('');
+ if (edits.length === 0)
+ return source;
+ // Build result from slices — avoids O(n) split('') allocation of N small string objects
+ const parts = [];
+ let pos = 0;
+ for (const [macroStart, openBrace, closeBrace] of edits) {
+ if (macroStart > pos)
+ parts.push(source.slice(pos, macroStart));
+ // Blank macro name + opening brace (preserve newlines)
+ parts.push(source.slice(macroStart, openBrace + 1).replace(_blankNonNewline, ' '));
+ pos = openBrace + 1;
+ // Keep everything inside the braces unchanged
+ if (closeBrace > pos)
+ parts.push(source.slice(pos, closeBrace));
+ // Blank closing brace
+ parts.push(' ');
+ pos = closeBrace + 1;
+ }
+ if (pos < source.length)
+ parts.push(source.slice(pos));
+ return parts.join('');
}
// ---------------------------------------------------------------------------
// Main parse function
// ---------------------------------------------------------------------------
export function parseFile(filePath, source) {
- const language = languageFromPath(filePath);
+ const language = detectLanguageForSource(filePath, source);
if (!language)
return null;
// TypeScript TSX uses a separate grammar
@@ -242,7 +287,10 @@ export function parseFile(filePath, source) {
return null;
try {
const parser = getParser();
- parser.setLanguage(grammar);
+ if (grammar !== _currentGrammar) {
+ parser.setLanguage(grammar);
+ _currentGrammar = grammar;
+ }
// tree-sitter-java cannot parse array-type annotations on varargs params
// (e.g. `@Nullable Object @Nullable ... args`). The second annotation causes
// error recovery to truncate the enclosing class_declaration, orphaning all
@@ -263,6 +311,25 @@ export function parseFile(filePath, source) {
const cacheKey = isTsx ? 'tsx' : language;
const query = getCachedQuery(cacheKey, grammar, queries);
const matches = query.matches(tree.rootNode);
+ // Pre-partition matches so each pass only iterates its relevant subset.
+ // Pass 1: definitions, heritage, Python typed-param/assign annotations.
+ // Pass 2: imports, calls, type references.
+ // Without this, every definition match does 5 useless find() calls in pass 2,
+ // and every call match does 4 useless find() calls in pass 1.
+ const pass1Matches = [];
+ const pass2Matches = [];
+ for (const m of matches) {
+ let isPass1 = false;
+ for (const c of m.captures) {
+ const n = c.name;
+ if (n.startsWith('definition.') || n.startsWith('heritage.') ||
+ n === '_typed_param_scope' || n === '_assign_scope') {
+ isPass1 = true;
+ break;
+ }
+ }
+ (isPass1 ? pass1Matches : pass2Matches).push(m);
+ }
const fileName = nodePath.basename(filePath);
let sourceLineCount = 1;
for (let i = 0; i < source.length; i++) {
@@ -286,7 +353,7 @@ export function parseFile(filePath, source) {
// Python: map function name → (variable name → assigned type) for untyped-param qualifier substitution
const assignTypeMap = new Map();
// --- First pass: collect definitions ---
- for (const match of matches) {
+ for (const match of pass1Matches) {
// Definition captures: name + definition.*
const defCapture = match.captures.find((c) => c.name.startsWith('definition.'));
const nameCapture = match.captures.find((c) => c.name === 'name');
@@ -403,7 +470,7 @@ export function parseFile(filePath, source) {
}
}
// --- Second pass: calls and imports ---
- for (const match of matches) {
+ for (const match of pass2Matches) {
// Full import statement captures (Scala: reconstructs dotted package paths)
const importStmt = match.captures.find((c) => c.name === 'import.stmt');
if (importStmt) {
@@ -438,12 +505,8 @@ export function parseFile(filePath, source) {
// Import captures (JS/TS/Python path-based)
const importSource = match.captures.find((c) => c.name === 'import.source');
if (importSource) {
- let importPath = importSource.node.text
- .replace(/^["'`]|["'`]$/g, '') // strip quotes
- .replace(/\\\\/g, '/'); // normalise backslashes
- if (importPath.length > 0 && importPath !== '*') {
- const rawMod = importPath.split('/').filter((s) => s !== '*').pop() ?? importPath;
- const modName = rawMod.replace(/^\.+/, ''); // strip leading dots: '.models' → 'models'
+ const modName = normalizeCapturedImport(importSource.node.text, language);
+ if (modName.length > 0 && modName !== '*') {
if (!modName)
continue; // skip bare '.' relative imports
entities.push({ name: modName, kind: 'module', lineStart: importSource.node.startPosition.row + 1, lineEnd: importSource.node.startPosition.row + 1, language });
@@ -698,8 +761,7 @@ export function resolveEdges(results, stats) {
for (let i = parts.length - 1; i >= parts.length - maxDepth; i--) {
const pkg = parts.slice(i).join('.');
const list = packageToFiles.get(pkg) ?? [];
- if (!list.includes(r.filePath))
- list.push(r.filePath);
+ list.push(r.filePath);
packageToFiles.set(pkg, list);
}
}
@@ -714,8 +776,7 @@ export function resolveEdges(results, stats) {
continue;
const dirName = nodePath.basename(nodePath.dirname(r.filePath));
const list = goPkgDirToFiles.get(dirName) ?? [];
- if (!list.includes(r.filePath))
- list.push(r.filePath);
+ list.push(r.filePath);
goPkgDirToFiles.set(dirName, list);
}
// ── Helpers ────────────────────────────────────────────────────────
@@ -732,6 +793,12 @@ export function resolveEdges(results, stats) {
const noExt = modName.replace(/\.(js|ts|mjs|cjs|jsx|tsx|py|scala|java|c|cc|cpp|cxx|h|hh|hpp|hxx)$/, '');
if (noExt !== modName && noExt && !candidates.includes(noExt))
candidates.push(noExt);
+ const pathBasename = nodePath.posix.basename(modName);
+ if (pathBasename && !candidates.includes(pathBasename))
+ candidates.push(pathBasename);
+ const pathBasenameNoExt = nodePath.posix.basename(noExt);
+ if (pathBasenameNoExt && !candidates.includes(pathBasenameNoExt))
+ candidates.push(pathBasenameNoExt);
// For dotted paths (Scala/Java: 'ix.memory.model.Edge'), also try last segment
const lastDot = modName.lastIndexOf('.');
if (lastDot !== -1) {
@@ -817,6 +884,21 @@ export function resolveEdges(results, stats) {
if (canonicalMatches.length === 1)
return canonicalMatches;
}
+ if (srcLanguage === SupportedLanguages.Java) {
+ const srcParts = srcFilePath.replace(/\\/g, '/').split('/');
+ const withProximity = matches.map(fp => {
+ const fpParts = fp.replace(/\\/g, '/').split('/');
+ let common = 0;
+ while (common < srcParts.length && common < fpParts.length && srcParts[common] === fpParts[common]) {
+ common++;
+ }
+ return { fp, common };
+ });
+ const maxCommon = Math.max(...withProximity.map(x => x.common));
+ const proximityMatches = withProximity.filter(x => x.common === maxCommon).map(x => x.fp);
+ if (proximityMatches.length === 1)
+ return proximityMatches;
+ }
if (srcLanguage !== SupportedLanguages.C && srcLanguage !== SupportedLanguages.CPlusPlus)
return matches;
let narrowed = matches;
@@ -892,26 +974,44 @@ export function resolveEdges(results, stats) {
// name ("ClaimId"), find files registered under that package, then find
// the one that defines the entity.
if (rel.predicate === 'IMPORTS') {
- if (result.language !== SupportedLanguages.Scala && result.language !== SupportedLanguages.Java)
- continue;
- const dstName = rel.dstName;
- const lastDot = dstName.lastIndexOf('.');
- if (lastDot === -1)
- continue; // not a dotted class import
- const entityName = dstName.slice(lastDot + 1);
- if (!entityName || entityName === '_')
- continue; // wildcard import
- const pkgPath = dstName.slice(0, lastDot); // e.g. "ix.memory.model"
- // Find files in the declared package that define this entity
- const pkgFiles = packageToFiles.get(pkgPath) ?? [];
- const matchFiles = pkgFiles.filter(fp => fp !== srcFilePath && fileHasSymbol.get(fp)?.has(entityName));
- if (matchFiles.length !== 1)
- continue; // ambiguous or not found
- const fp = matchFiles[0];
- 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 });
+ if (result.language === SupportedLanguages.Scala || result.language === SupportedLanguages.Java) {
+ const dstName = rel.dstName;
+ const lastDot = dstName.lastIndexOf('.');
+ if (lastDot !== -1) {
+ const entityName = dstName.slice(lastDot + 1);
+ if (entityName && entityName !== '_') {
+ const pkgPath = dstName.slice(0, lastDot);
+ const pkgFiles = packageToFiles.get(pkgPath) ?? [];
+ const matchFiles = pkgFiles.filter(fp => fp !== srcFilePath && fileHasSymbol.get(fp)?.has(entityName));
+ if (matchFiles.length === 1) {
+ const fp = matchFiles[0];
+ const dstQualifiedKey = bestQKey(fileQKeys, fp, entityName);
+ if (dstQualifiedKey !== null) {
+ resolved.push({ srcFilePath, srcName: rel.srcName, dstFilePath: fp, dstName, dstQualifiedKey, predicate: 'IMPORTS', confidence: 0.9 });
+ stats.resolvedImport++;
+ continue;
+ }
+ }
+ }
+ }
+ }
+ const importMatches = modNameToFiles(rel.dstName, srcFilePath);
+ if (importMatches.length === 1) {
+ const fp = importMatches[0];
+ resolved.push({
+ srcFilePath,
+ srcName: rel.srcName,
+ dstFilePath: fp,
+ dstName: rel.dstName,
+ dstQualifiedKey: fileEntityName(fp),
+ predicate: 'IMPORTS',
+ confidence: 0.9,
+ });
+ stats.resolvedImport++;
+ }
+ else if (importMatches.length > 1) {
+ stats.skippedAmbiguous++;
+ }
continue;
}
const dstName = rel.dstName;
@@ -991,6 +1091,13 @@ export function resolveEdges(results, stats) {
}
// Tier 3: global fallback (confidence 0.5) — uses inverted symbol index
// instead of scanning all files.
+ // Skip for C/C++ REFERENCES: struct/type references that don't resolve via the
+ // import chain come from system headers (e.g. <net/if.h>) and must not be
+ // linked to an in-repo definition of the same name.
+ if (rel.predicate === 'REFERENCES' &&
+ (srcLanguage === SupportedLanguages.C || srcLanguage === SupportedLanguages.CPlusPlus)) {
+ continue;
+ }
stats.globalFallbacks++;
const candidates = symbolToFiles.get(dstName) ?? [];
let globalMatches = candidates.filter(fp => fp !== srcFilePath && fileLanguage.get(fp) === srcLanguage);
diff --git a/core-ingestion/dist/queries.d.ts b/core-ingestion/dist/queries.d.ts
index a6b93dd6..45eef491 100644
--- a/core-ingestion/dist/queries.d.ts
+++ b/core-ingestion/dist/queries.d.ts
@@ -1,7 +1,7 @@
import { SupportedLanguages } from './languages.js';
export declare const TYPESCRIPT_QUERIES = "\n(class_declaration\n name: (type_identifier) @name) @definition.class\n\n(interface_declaration\n name: (type_identifier) @name) @definition.interface\n\n(function_declaration\n name: (identifier) @name) @definition.function\n\n(method_definition\n name: (property_identifier) @name) @definition.method\n\n(lexical_declaration\n (variable_declarator\n name: (identifier) @name\n value: (arrow_function))) @definition.function\n\n(lexical_declaration\n (variable_declarator\n name: (identifier) @name\n value: (function_expression))) @definition.function\n\n(export_statement\n declaration: (lexical_declaration\n (variable_declarator\n name: (identifier) @name\n value: (arrow_function)))) @definition.function\n\n(export_statement\n declaration: (lexical_declaration\n (variable_declarator\n name: (identifier) @name\n value: (function_expression)))) @definition.function\n\n(import_statement\n source: (string) @import.source) @import\n\n; Re-export statements: export { X } from './y'\n(export_statement\n source: (string) @import.source) @import\n\n; Dynamic import with string literal: import('./foo') or await import('./foo')\n(call_expression\n function: (import)\n arguments: (arguments (string) @import.source)) @import\n\n(call_expression\n function: (identifier) @call.name) @call\n\n(call_expression\n function: (member_expression\n property: (property_identifier) @call.name)) @call\n\n; this.method() \u2014 capture this as qualifier so index.ts can substitute the enclosing class.\n; Analogous to Python's self/cls substitution.\n(call_expression\n function: (member_expression\n object: (this) @_qualifier\n property: (property_identifier) @call.name)) @call\n\n; Constructor calls: new Foo()\n(new_expression\n constructor: (identifier) @call.name) @call\n\n; Heritage queries - class extends\n(class_declaration\n name: (type_identifier) @heritage.class\n (class_heritage\n (extends_clause\n value: (identifier) @heritage.extends))) @heritage\n\n; Heritage queries - class implements interface\n(class_declaration\n name: (type_identifier) @heritage.class\n (class_heritage\n (implements_clause\n (type_identifier) @heritage.implements))) @heritage.impl\n\n; Heritage queries - interface extends interface\n(interface_declaration\n name: (type_identifier) @heritage.class\n (extends_type_clause\n (type_identifier) @heritage.extends)) @heritage\n\n; Type references \u2014 captures types used in annotations/parameters/return types\n(type_annotation (type_identifier) @reference.type)\n";
export declare const JAVASCRIPT_QUERIES = "\n(class_declaration\n name: (identifier) @name) @definition.class\n\n(function_declaration\n name: (identifier) @name) @definition.function\n\n(method_definition\n name: (property_identifier) @name) @definition.method\n\n(lexical_declaration\n (variable_declarator\n name: (identifier) @name\n value: (arrow_function))) @definition.function\n\n(lexical_declaration\n (variable_declarator\n name: (identifier) @name\n value: (function_expression))) @definition.function\n\n; var foo = function() {} (CommonJS var declarations)\n(variable_declaration\n (variable_declarator\n name: (identifier) @name\n value: (function_expression))) @definition.function\n\n; var foo = () => {} (CommonJS var arrow functions)\n(variable_declaration\n (variable_declarator\n name: (identifier) @name\n value: (arrow_function))) @definition.function\n\n; obj.method = function name() {} (prototype/mixin method assignments)\n(expression_statement\n (assignment_expression\n left: (member_expression\n property: (property_identifier) @name)\n right: (function_expression))) @definition.function\n\n(export_statement\n declaration: (lexical_declaration\n (variable_declarator\n name: (identifier) @name\n value: (arrow_function)))) @definition.function\n\n(export_statement\n declaration: (lexical_declaration\n (variable_declarator\n name: (identifier) @name\n value: (function_expression)))) @definition.function\n\n(import_statement\n source: (string) @import.source) @import\n\n; Re-export statements: export { X } from './y'\n(export_statement\n source: (string) @import.source) @import\n\n; Dynamic import with string literal: import('./foo') or await import('./foo')\n(call_expression\n function: (import)\n arguments: (arguments (string) @import.source)) @import\n\n; CommonJS require() \u2192 IMPORTS\n(call_expression\n function: (identifier) @_req\n arguments: (arguments (string) @import.source)\n (#eq? @_req \"require\")) @import\n\n(call_expression\n function: (identifier) @call.name) @call\n\n(call_expression\n function: (member_expression\n property: (property_identifier) @call.name)) @call\n\n; this.method() \u2014 capture this as qualifier so index.ts can substitute the enclosing class.\n; Analogous to Python's self/cls substitution.\n(call_expression\n function: (member_expression\n object: (this) @_qualifier\n property: (property_identifier) @call.name)) @call\n\n; Constructor calls: new Foo()\n(new_expression\n constructor: (identifier) @call.name) @call\n\n; Heritage queries - class extends (JavaScript uses different AST than TypeScript)\n; In tree-sitter-javascript, class_heritage directly contains the parent identifier\n(class_declaration\n name: (identifier) @heritage.class\n (class_heritage\n (identifier) @heritage.extends)) @heritage\n";
-export declare const PYTHON_QUERIES = "\n(class_definition\n name: (identifier) @name) @definition.class\n\n(function_definition\n name: (identifier) @name) @definition.function\n\n(import_statement\n name: (dotted_name) @import.source) @import\n\n(import_from_statement\n module_name: (dotted_name) @import.source) @import\n\n(import_from_statement\n module_name: (relative_import) @import.source) @import\n\n; from . import utils / from .models import User \u2014 also capture the imported symbol names\n(import_from_statement\n module_name: (relative_import)\n name: (dotted_name (identifier) @import.name)) @import.names\n\n(call\n function: (identifier) @call.name) @call\n\n(call\n function: (attribute\n attribute: (identifier) @call.name)) @call\n\n; Attribute calls with object captured as qualifier so Tier-1b resolution can use\n; class/module name to break ties (e.g. Session.execute \u2192 resolves to Session in\n; the importing file). Emitted alongside the bare-name pattern above so that\n; bare-name global fallback still fires when qualifier resolution fails.\n(call\n function: (attribute\n object: (identifier) @_qualifier\n attribute: (identifier) @call.name)) @call\n\n; Django ORM manager pattern: Model.objects.method(...)\n; Unwinds the intermediate accessor (objects/related_manager/etc.) and captures\n; the model class as qualifier, e.g. User.objects.filter(...) \u2192 CALLS User.filter.\n; This creates cross-file edges between view/form code and model definitions.\n(call\n function: (attribute\n object: (attribute\n object: (identifier) @_qualifier\n attribute: (identifier))\n attribute: (identifier) @call.name)) @call\n\n; Chained call pattern: qualifier.method1(...).method2(...)\n; Propagates the qualifier from the inner call to the outer, e.g.\n; User.filter(...).order_by(...) \u2192 CALLS User.order_by.\n(call\n function: (attribute\n object: (call\n function: (attribute\n object: (identifier) @_qualifier\n attribute: (identifier)))\n attribute: (identifier) @call.name)) @call\n\n; Heritage queries - Python class inheritance\n(class_definition\n name: (identifier) @heritage.class\n superclasses: (argument_list\n (identifier) @heritage.extends)) @heritage\n\n; Type references \u2014 captures types used in type annotations / hints\n(typed_parameter type: (type (identifier) @reference.type))\n(typed_default_parameter type: (type (identifier) @reference.type))\n\n; Typed parameters: capture name + type together so qualifier substitution can\n; map a local variable name to its declared class (e.g. query: Query \u2192 'query' \u2192 'Query').\n(function_definition\n parameters: (parameters\n (typed_parameter\n (identifier) @_typed_param_name\n type: (type (identifier) @_typed_param_type)))) @_typed_param_scope\n\n(function_definition\n parameters: (parameters\n (typed_default_parameter\n (identifier) @_typed_param_name\n type: (type (identifier) @_typed_param_type)))) @_typed_param_scope\n\n; Assignment tracking: x = SomeClass() or x = SomeClass(args)\n; Maps local variable name \u2192 constructor type for untyped-parameter qualifier substitution.\n; Only PascalCase RHS names are tracked in TypeScript (constructor convention).\n(function_definition\n body: (block\n (expression_statement\n (assignment\n left: (identifier) @_assign_lhs\n right: (call\n function: (identifier) @_assign_rhs_type))))) @_assign_scope\n\n; Assignment tracking: x = Model.objects.method()\n; Captures the outermost identifier (model class) for ORM manager patterns.\n(function_definition\n body: (block\n (expression_statement\n (assignment\n left: (identifier) @_assign_lhs\n right: (call\n function: (attribute\n object: (attribute\n object: (identifier) @_assign_rhs_type\n attribute: (identifier))\n attribute: (identifier))))))) @_assign_scope\n";
+export declare const PYTHON_QUERIES = "\n(class_definition\n name: (identifier) @name) @definition.class\n\n(function_definition\n name: (identifier) @name) @definition.function\n\n(import_statement\n name: (dotted_name) @import.source) @import\n\n(import_from_statement\n module_name: (dotted_name) @import.source) @import\n\n(import_from_statement\n module_name: (relative_import) @import.source) @import\n\n; from . import utils / from .models import User \u2014 also capture the imported symbol names\n(import_from_statement\n module_name: (relative_import)\n name: (dotted_name (identifier) @import.name)) @import.names\n\n(call\n function: (identifier) @call.name) @call\n\n(call\n function: (attribute\n attribute: (identifier) @call.name)) @call\n\n; Attribute calls with object captured as qualifier so Tier-1b resolution can use\n; class/module name to break ties (e.g. Session.execute \u2192 resolves to Session in\n; the importing file). Emitted alongside the bare-name pattern above so that\n; bare-name global fallback still fires when qualifier resolution fails.\n(call\n function: (attribute\n object: (identifier) @_qualifier\n attribute: (identifier) @call.name)) @call\n\n; Django ORM manager pattern: Model.objects.method(...)\n; Unwinds the intermediate accessor (objects/related_manager/etc.) and captures\n; the model class as qualifier, e.g. User.objects.filter(...) \u2192 CALLS User.filter.\n; This creates cross-file edges between view/form code and model definitions.\n(call\n function: (attribute\n object: (attribute\n object: (identifier) @_qualifier\n attribute: (identifier))\n attribute: (identifier) @call.name)) @call\n\n; Chained call pattern: qualifier.method1(...).method2(...)\n; Propagates the qualifier from the inner call to the outer, e.g.\n; User.filter(...).order_by(...) \u2192 CALLS User.order_by.\n(call\n function: (attribute\n object: (call\n function: (attribute\n object: (identifier) @_qualifier\n attribute: (identifier)))\n attribute: (identifier) @call.name)) @call\n\n; Heritage queries - Python class inheritance\n(class_definition\n name: (identifier) @heritage.class\n superclasses: (argument_list\n (identifier) @heritage.extends)) @heritage\n\n; Type references \u2014 captures types used in type annotations / hints\n(typed_parameter type: (type (identifier) @reference.type))\n(typed_default_parameter type: (type (identifier) @reference.type))\n\n; Class/function references passed as keyword argument values\n; e.g. LoginView.as_view(authentication_form=AdminAuthenticationForm, ...)\n; Emits a CALLS edge from the enclosing function to the referenced identifier.\n(call\n arguments: (argument_list\n (keyword_argument\n value: (identifier) @call.name)))\n\n; Typed parameters: capture name + type together so qualifier substitution can\n; map a local variable name to its declared class (e.g. query: Query \u2192 'query' \u2192 'Query').\n(function_definition\n parameters: (parameters\n (typed_parameter\n (identifier) @_typed_param_name\n type: (type (identifier) @_typed_param_type)))) @_typed_param_scope\n\n(function_definition\n parameters: (parameters\n (typed_default_parameter\n (identifier) @_typed_param_name\n type: (type (identifier) @_typed_param_type)))) @_typed_param_scope\n\n; Assignment tracking: x = SomeClass() or x = SomeClass(args)\n; Maps local variable name \u2192 constructor type for untyped-parameter qualifier substitution.\n; Only PascalCase RHS names are tracked in TypeScript (constructor convention).\n(function_definition\n body: (block\n (expression_statement\n (assignment\n left: (identifier) @_assign_lhs\n right: (call\n function: (identifier) @_assign_rhs_type))))) @_assign_scope\n\n; Assignment tracking: x = Model.objects.method()\n; Captures the outermost identifier (model class) for ORM manager patterns.\n(function_definition\n body: (block\n (expression_statement\n (assignment\n left: (identifier) @_assign_lhs\n right: (call\n function: (attribute\n object: (attribute\n object: (identifier) @_assign_rhs_type\n attribute: (identifier))\n attribute: (identifier))))))) @_assign_scope\n";
export declare const JAVA_QUERIES = "\n; Classes, Interfaces, Enums, Annotations\n(class_declaration name: (identifier) @name) @definition.class\n(interface_declaration name: (identifier) @name) @definition.interface\n(enum_declaration name: (identifier) @name) @definition.enum\n(annotation_type_declaration name: (identifier) @name) @definition.annotation\n\n; Methods & Constructors\n(method_declaration name: (identifier) @name) @definition.method\n(constructor_declaration name: (identifier) @name) @definition.constructor\n\n; Imports - capture any import declaration child as source\n(import_declaration (_) @import.source) @import\n\n; Calls\n(method_invocation name: (identifier) @call.name) @call\n(method_invocation object: (_) name: (identifier) @call.name) @call\n\n; Constructor calls: new Foo()\n(object_creation_expression type: (type_identifier) @call.name) @call\n\n; Heritage - extends class (plain: extends Foo)\n(class_declaration name: (identifier) @heritage.class\n (superclass (type_identifier) @heritage.extends)) @heritage\n\n; Heritage - extends class with generics (extends Foo<T>)\n(class_declaration name: (identifier) @heritage.class\n (superclass (generic_type (type_identifier) @heritage.extends))) @heritage\n\n; Heritage - implements interfaces (plain: implements Foo)\n(class_declaration name: (identifier) @heritage.class\n (super_interfaces (type_list (type_identifier) @heritage.implements))) @heritage.impl\n\n; Heritage - implements interfaces with generics (implements Foo<T>)\n(class_declaration name: (identifier) @heritage.class\n (super_interfaces (type_list (generic_type (type_identifier) @heritage.implements)))) @heritage.impl\n\n; Heritage - interface extends interfaces (plain: extends Foo)\n(interface_declaration name: (identifier) @heritage.class\n (extends_interfaces (type_list (type_identifier) @heritage.extends))) @heritage\n\n; Heritage - interface extends interfaces with generics (extends Foo<T>)\n(interface_declaration name: (identifier) @heritage.class\n (extends_interfaces (type_list (generic_type (type_identifier) @heritage.extends)))) @heritage\n\n; Type references \u2014 captures types used in field/param/return-type positions\n(field_declaration type: (type_identifier) @reference.type)\n(formal_parameter type: (type_identifier) @reference.type)\n(local_variable_declaration type: (type_identifier) @reference.type)\n(method_declaration type: (type_identifier) @reference.type)\n";
export declare const C_QUERIES = "\n; Functions (direct declarator)\n(function_definition declarator: (function_declarator declarator: (identifier) @name)) @definition.function\n(declaration declarator: (function_declarator declarator: (identifier) @name)) @definition.function\n\n; Functions returning pointers (pointer_declarator wraps function_declarator)\n(function_definition declarator: (pointer_declarator declarator: (function_declarator declarator: (identifier) @name))) @definition.function\n(declaration declarator: (pointer_declarator declarator: (function_declarator declarator: (identifier) @name))) @definition.function\n\n; Functions returning double pointers (nested pointer_declarator)\n(function_definition declarator: (pointer_declarator declarator: (pointer_declarator declarator: (function_declarator declarator: (identifier) @name)))) @definition.function\n\n; Structs, Unions, Enums, Typedefs\n; Only match struct/union with a body to avoid treating forward declarations as definitions\n(struct_specifier name: (type_identifier) @name body: (field_declaration_list)) @definition.struct\n(union_specifier name: (type_identifier) @name body: (field_declaration_list)) @definition.union\n(enum_specifier name: (type_identifier) @name) @definition.enum\n(type_definition declarator: (type_identifier) @name) @definition.typedef\n\n; Macros\n(preproc_function_def name: (identifier) @name) @definition.macro\n(preproc_def name: (identifier) @name) @definition.macro\n\n; Includes\n(preproc_include path: (_) @import.source) @import\n\n; Calls\n(call_expression function: (identifier) @call.name) @call\n(call_expression function: (field_expression field: (field_identifier) @call.name)) @call\n\n; Type references \u2014 captures types used in declaration/parameter positions\n(declaration type: (type_identifier) @reference.type)\n(parameter_declaration type: (type_identifier) @reference.type)\n; struct_specifier forms: e.g. \"const struct Curl_protocol foo = {...}\"\n(declaration type: (struct_specifier name: (type_identifier) @reference.type))\n(parameter_declaration type: (struct_specifier name: (type_identifier) @reference.type))\n";
export declare const GO_QUERIES = "\n; Functions\n(function_declaration name: (identifier) @name) @definition.function\n\n; Methods \u2014 non-pointer receiver: func (r ReceiverType) Name(...)\n; The receiver.type capture lets the parser set container = ReceiverType so that\n; qualifiedKey becomes \"ReceiverType.Name\" instead of bare \"Name\".\n(method_declaration\n receiver: (parameter_list (parameter_declaration\n type: (type_identifier) @receiver.type))\n name: (field_identifier) @name) @definition.method\n\n; Methods \u2014 pointer receiver: func (r *ReceiverType) Name(...)\n(method_declaration\n receiver: (parameter_list (parameter_declaration\n type: (pointer_type (type_identifier) @receiver.type)))\n name: (field_identifier) @name) @definition.method\n\n; Types\n(type_declaration (type_spec name: (type_identifier) @name type: (struct_type))) @definition.struct\n(type_declaration (type_spec name: (type_identifier) @name type: (interface_type))) @definition.interface\n\n; Interface method signatures \u2014 emits each method inside an interface_type as a method entity.\n; @heritage.class = interface name (used as container in index.ts via heritageClassCapture).\n; node type is method_elem in tree-sitter-go v0.21+.\n(type_declaration\n (type_spec\n name: (type_identifier) @heritage.class\n type: (interface_type\n (method_elem\n name: (field_identifier) @name)))) @definition.method\n\n; Imports\n(import_declaration (import_spec path: (interpreted_string_literal) @import.source)) @import\n(import_declaration (import_spec_list (import_spec path: (interpreted_string_literal) @import.source))) @import\n\n; Struct embedding \u2014 value: type Foo struct { Bar }\n; NOTE: no @definition.* capture here \u2014 if it were present, defCapture fires first and\n; the heritage block is never reached (early-exit in the first pass).\n; The . anchors require the type_identifier to be the FIRST and ONLY named child of\n; field_declaration, distinguishing anonymous embedded fields from named fields like:\n; w Writer (named field \u2014 field_identifier_list precedes type, not matched)\n(type_declaration\n (type_spec\n name: (type_identifier) @heritage.class\n type: (struct_type\n (field_declaration_list\n (field_declaration .\n (type_identifier) @heritage.extends .)))))\n\n; Struct embedding \u2014 pointer: type Foo struct { *Bar }\n; The . anchor requires pointer_type to be the first named child (anonymous embedded).\n(type_declaration\n (type_spec\n name: (type_identifier) @heritage.class\n type: (struct_type\n (field_declaration_list\n (field_declaration .\n (pointer_type (type_identifier) @heritage.extends) .)))))\n\n; Calls\n(call_expression function: (identifier) @call.name) @call\n; Qualified calls: pkg.Func() or localVar.Method() where the operand is a simple\n; identifier \u2014 capture it as @_qualifier so cross-file resolution uses the fully\n; qualified name \"scrape.NewManager\" instead of bare \"NewManager\", preventing\n; ambiguous same-named symbols in different packages from being collapsed.\n(call_expression function: (selector_expression\n operand: (identifier) @_qualifier\n field: (field_identifier) @call.name)) @call\n; Chained/complex calls: a.b.Method(), call().Method(), index[i].Method() \u2014 the\n; operand is not a simple identifier, so emit without qualifier (bare method name).\n(call_expression function: (selector_expression\n operand: (selector_expression)\n field: (field_identifier) @call.name)) @call\n\n; Struct literal construction: User{Name: \"Alice\"}\n(composite_literal type: (type_identifier) @call.name) @call\n\n; Type references \u2014 captures types used in field/param/return-type positions\n(field_declaration type: (type_identifier) @reference.type)\n(parameter_declaration type: (type_identifier) @reference.type)\n; Return types: in tree-sitter-go, return type is a direct type_identifier child of function/method\n(function_declaration result: (type_identifier) @reference.type)\n(method_declaration result: (type_identifier) @reference.type)\n\n; Pointer-type variants: e.g. opts *Options, func(*Options) *Result\n(field_declaration type: (pointer_type (type_identifier) @reference.type))\n(parameter_declaration type: (pointer_type (type_identifier) @reference.type))\n(function_declaration result: (pointer_type (type_identifier) @reference.type))\n(method_declaration result: (pointer_type (type_identifier) @reference.type))\n";
diff --git a/core-ingestion/dist/queries.js b/core-ingestion/dist/queries.js
index 0b2b03c5..c06a53d6 100644
--- a/core-ingestion/dist/queries.js
+++ b/core-ingestion/dist/queries.js
@@ -260,6 +260,14 @@ export const PYTHON_QUERIES = `
(typed_parameter type: (type (identifier) @reference.type))
(typed_default_parameter type: (type (identifier) @reference.type))
+; Class/function references passed as keyword argument values
+; e.g. LoginView.as_view(authentication_form=AdminAuthenticationForm, ...)
+; Emits a CALLS edge from the enclosing function to the referenced identifier.
+(call
+ arguments: (argument_list
+ (keyword_argument
+ value: (identifier) @call.name)))
+
; Typed parameters: capture name + type together so qualifier substitution can
; map a local variable name to its declared class (e.g. query: Query → 'query' → 'Query').
(function_definition
diff --git a/core-ingestion/src/__tests__/patchBuilder.resolution.test.ts b/core-ingestion/src/__tests__/patchBuilder.resolution.test.ts
index 0a36cabb..1a4aae1e 100644
--- a/core-ingestion/src/__tests__/patchBuilder.resolution.test.ts
+++ b/core-ingestion/src/__tests__/patchBuilder.resolution.test.ts
@@ -2,7 +2,7 @@ import * as crypto from 'node:crypto';
import { describe, expect, it } from 'vitest';
-import type { FileParseResult, ParsedEntity, ParsedRelationship, ResolvedEdge } from '../index.js';
+import { parseFile, resolveEdges, type FileParseResult, type ParsedEntity, type ParsedRelationship, type ResolvedEdge } from '../index.js';
import { SupportedLanguages } from '../languages.js';
import { buildPatchWithResolution } from '../patch-builder.js';
@@ -108,4 +108,45 @@ describe('buildPatchWithResolution', () => {
dst: nodeId('/repo/compaction_job.cc', 'CompactionJob.Run'),
}));
});
+
+ it('rewrites resolved C++ imports to the imported file node', () => {
+ const importer = parseFile(
+ '/repo/include/KimeraRPGO/outlier/Pcm.h',
+ `
+#include "KimeraRPGO/utils/GraphUtils.h"
+
+class Pcm {};
+ `,
+ );
+ const imported = parseFile(
+ '/repo/include/KimeraRPGO/utils/GraphUtils.h',
+ `
+struct Trajectory {};
+ `,
+ );
+
+ expect(importer).not.toBeNull();
+ expect(imported).not.toBeNull();
+
+ const resolvedEdges = resolveEdges([importer!, imported!]);
+ expect(resolvedEdges).toContainEqual({
+ srcFilePath: '/repo/include/KimeraRPGO/outlier/Pcm.h',
+ srcName: 'Pcm.h',
+ dstFilePath: '/repo/include/KimeraRPGO/utils/GraphUtils.h',
+ dstName: 'KimeraRPGO/utils/GraphUtils.h',
+ dstQualifiedKey: 'GraphUtils.h',
+ predicate: 'IMPORTS',
+ confidence: 0.9,
+ });
+
+ const patch = buildPatchWithResolution(importer!, 'test-hash', resolvedEdges);
+ const importEdge = patch.ops.find(
+ (op) => op.type === 'UpsertEdge' && op.predicate === 'IMPORTS',
+ );
+
+ expect(importEdge).toEqual(expect.objectContaining({
+ src: nodeId('/repo/include/KimeraRPGO/outlier/Pcm.h', 'Pcm.h'),
+ dst: nodeId('/repo/include/KimeraRPGO/utils/GraphUtils.h', 'GraphUtils.h'),
+ }));
+ });
});
diff --git a/core-ingestion/src/__tests__/queries.c.test.ts b/core-ingestion/src/__tests__/queries.c.test.ts
index 3a81de31..dedd8ac4 100644
--- a/core-ingestion/src/__tests__/queries.c.test.ts
+++ b/core-ingestion/src/__tests__/queries.c.test.ts
@@ -106,4 +106,35 @@ static int smtp_connect(struct Curl_easy *data, struct connectdata *conn) {
.map(r => r.dstName);
expect(importTargets).toContain('curl.h');
});
+
+ // BUG-1: #define macros must be classified as 'macro', not 'function'
+ it('classifies #define macros as kind macro, not function', () => {
+ const result = parseFile(
+ '/repo/curl.h',
+ `
+#ifndef CURLINC_CURL_H
+#define CURLINC_CURL_H
+
+#define CURL_STRICTER
+#define CURL_EXTERN extern
+#define enquote(x) #x
+#define expand(x) enquote(x)
+
+void curl_global_init(void);
+ `,
+ );
+
+ expect(result).not.toBeNull();
+ const macroEntities = result!.entities.filter(e =>
+ ['CURLINC_CURL_H', 'CURL_STRICTER', 'CURL_EXTERN', 'enquote', 'expand'].includes(e.name),
+ );
+ expect(macroEntities.length).toBeGreaterThan(0);
+ for (const e of macroEntities) {
+ expect(e.kind).toBe('macro');
+ }
+
+ // Real function must still be 'function'
+ const fn = result!.entities.find(e => e.name === 'curl_global_init');
+ expect(fn?.kind).toBe('function');
+ });
});
diff --git a/core-ingestion/src/__tests__/queries.python.test.ts b/core-ingestion/src/__tests__/queries.python.test.ts
index 9e1dc0a1..a7114b38 100644
--- a/core-ingestion/src/__tests__/queries.python.test.ts
+++ b/core-ingestion/src/__tests__/queries.python.test.ts
@@ -95,6 +95,25 @@ def my_view(request):
});
});
+ it('emits REFERENCES edge for class passed as keyword argument value', () => {
+ const result = parseFile(
+ '/repo/admin/sites.py',
+ `
+class AdminSite:
+ def login(self, request):
+ return LoginView.as_view(authentication_form=AdminAuthenticationForm, extra_context=context)
+ `,
+ );
+
+ expect(result).not.toBeNull();
+ // AdminAuthenticationForm passed as kwarg value → CALLS edge from login
+ expect(result!.relationships).toContainEqual({
+ srcName: 'AdminSite.login',
+ dstName: 'AdminAuthenticationForm',
+ predicate: 'CALLS',
+ });
+ });
+
it('resolves qualifier from Model.objects.method() assignment (Pattern 2)', () => {
const result = parseFile(
'/repo/views.py',
diff --git a/core-ingestion/src/__tests__/resolveEdges.test.ts b/core-ingestion/src/__tests__/resolveEdges.test.ts
index cb1e3961..c46ad0fb 100644
--- a/core-ingestion/src/__tests__/resolveEdges.test.ts
+++ b/core-ingestion/src/__tests__/resolveEdges.test.ts
@@ -336,4 +336,32 @@ describe('resolveEdges', () => {
expect(resolveEdges([ambiguousGlobal, helperA, helperB])).toEqual([]);
});
+
+ // BUG-2: struct references in C that come from system headers (<net/if.h>)
+ // must not be linked to an in-repo definition of the same struct name via
+ // global tier-3 fallback.
+ it('does not create a false REFERENCES edge for a C struct from a system header', () => {
+ // CurlTests.c: includes system <net/if.h> (not in batch) and uses struct ifreq
+ const curlTests = fileResult(
+ '/repo/CMake/CurlTests.c',
+ SupportedLanguages.C,
+ [],
+ [
+ { srcName: 'CurlTests.c', dstName: 'net/if.h', predicate: 'IMPORTS' },
+ { srcName: 'CurlTests.c', dstName: 'ifreq', predicate: 'REFERENCES' },
+ ],
+ );
+
+ // if2ip.h: defines its own struct ifreq as a platform shim
+ const if2ip = fileResult(
+ '/repo/lib/if2ip.c',
+ SupportedLanguages.C,
+ [entity('ifreq', SupportedLanguages.C, 'class')],
+ [],
+ );
+
+ const resolved = resolveEdges([curlTests, if2ip]);
+ const refEdges = resolved.filter(e => e.predicate === 'REFERENCES');
+ expect(refEdges).toEqual([]);
+ });
});
diff --git a/core-ingestion/src/index.ts b/core-ingestion/src/index.ts
index dd12532c..e8477f6a 100644
--- a/core-ingestion/src/index.ts
+++ b/core-ingestion/src/index.ts
@@ -122,7 +122,7 @@ const DEFINITION_KIND_MAP: Record<string, string> = {
'definition.property': 'function',
'definition.const': 'function',
'definition.static': 'function',
- 'definition.macro': 'function',
+ 'definition.macro': 'macro',
'definition.union': 'class',
'definition.typedef': 'class',
'definition.template': 'class',
@@ -221,6 +221,7 @@ export function isGrammarSupported(filePath: string): boolean {
// ---------------------------------------------------------------------------
let _parser: Parser | null = null;
+let _currentGrammar: any = null;
const _queryCache = new Map<SupportedLanguages | 'tsx', { grammar: any; query: any }>();
function getParser(): Parser {
@@ -240,6 +241,42 @@ function getCachedQuery(
return query;
}
+function normalizeCapturedImport(rawValue: string, language: SupportedLanguages): string {
+ const trimmed = rawValue.trim().replace(/\\\\/g, '/');
+ const unwrapped = trimmed
+ .replace(/^["'`<]/, '')
+ .replace(/[>"'`]$/, '');
+
+ if (language === SupportedLanguages.C || language === SupportedLanguages.CPlusPlus) {
+ return unwrapped.replace(/^\.\/+/, '');
+ }
+
+ const rawMod = unwrapped.split('/').filter((s: string) => s !== '*').pop() ?? unwrapped;
+ return rawMod.replace(/^\.+/, '');
+}
+
+function fileEntityName(filePath: string): string {
+ return nodePath.basename(filePath);
+}
+
+function looksLikeCppHeader(source: string): boolean {
+ return /\bclass\s+[A-Za-z_]\w*\b/.test(source)
+ || /\bnamespace\s+[A-Za-z_]\w*\b/.test(source)
+ || /\btemplate\s*</.test(source)
+ || /\busing\s+namespace\b/.test(source)
+ || /\b(public|private|protected)\s*:/.test(source)
+ || /\b(virtual|constexpr|typename|friend|operator)\b/.test(source)
+ || /\b[A-Za-z_]\w*::[A-Za-z_]\w*/.test(source);
+}
+
+function detectLanguageForSource(filePath: string, source: string): SupportedLanguages | null {
+ const language = languageFromPath(filePath);
+ if (language === SupportedLanguages.C && filePath.toLowerCase().endsWith('.h') && looksLikeCppHeader(source)) {
+ return SupportedLanguages.CPlusPlus;
+ }
+ return language;
+}
+
// ---------------------------------------------------------------------------
// Rust: cfg macro unwrapping
// ---------------------------------------------------------------------------
@@ -252,31 +289,44 @@ function getCachedQuery(
* preserving every character position and line number so that entity line
* numbers remain accurate.
*/
+// Pre-compiled for blanking macro headers (preserves newlines, replaces all else with space)
+const _blankNonNewline = /[^\n]/g;
+
function unwrapRustCfgMacros(source: string): string {
const re = /\bcfg_(?:rt_multi_thread|not_rt|rt|io|time|sync|net|fs|process|signal)!\s*\{/g;
- const chars = source.split('');
+ // Collect edit regions before mutating anything
+ const edits: Array<[number, number, number]> = []; // [macroStart, openBrace, closeBrace]
let m: RegExpExecArray | null;
while ((m = re.exec(source)) !== null) {
- const openBrace = m.index + m[0].length - 1; // position of the `{`
- // Find matching closing brace by counting depth
+ const openBrace = m.index + m[0].length - 1;
let depth = 0;
let closeBrace = -1;
for (let i = openBrace; i < source.length; i++) {
if (source[i] === '{') depth++;
else if (source[i] === '}') {
- depth--;
- if (depth === 0) { closeBrace = i; break; }
+ if (--depth === 0) { closeBrace = i; break; }
}
}
- if (closeBrace === -1) continue;
- // Blank out everything from start of macro name up to and including `{`
- for (let i = m.index; i <= openBrace; i++) {
- if (chars[i] !== '\n') chars[i] = ' ';
- }
- // Blank out the matching closing `}`
- chars[closeBrace] = ' ';
+ if (closeBrace !== -1) edits.push([m.index, openBrace, closeBrace]);
}
- return chars.join('');
+ if (edits.length === 0) return source;
+
+ // Build result from slices — avoids O(n) split('') allocation of N small string objects
+ const parts: string[] = [];
+ let pos = 0;
+ for (const [macroStart, openBrace, closeBrace] of edits) {
+ if (macroStart > pos) parts.push(source.slice(pos, macroStart));
+ // Blank macro name + opening brace (preserve newlines)
+ parts.push(source.slice(macroStart, openBrace + 1).replace(_blankNonNewline, ' '));
+ pos = openBrace + 1;
+ // Keep everything inside the braces unchanged
+ if (closeBrace > pos) parts.push(source.slice(pos, closeBrace));
+ // Blank closing brace
+ parts.push(' ');
+ pos = closeBrace + 1;
+ }
+ if (pos < source.length) parts.push(source.slice(pos));
+ return parts.join('');
}
// ---------------------------------------------------------------------------
@@ -284,7 +334,7 @@ function unwrapRustCfgMacros(source: string): string {
// ---------------------------------------------------------------------------
export function parseFile(filePath: string, source: string): FileParseResult | null {
- const language = languageFromPath(filePath);
+ const language = detectLanguageForSource(filePath, source);
if (!language) return null;
// TypeScript TSX uses a separate grammar
@@ -297,7 +347,10 @@ export function parseFile(filePath: string, source: string): FileParseResult | n
try {
const parser = getParser();
- parser.setLanguage(grammar);
+ if (grammar !== _currentGrammar) {
+ parser.setLanguage(grammar);
+ _currentGrammar = grammar;
+ }
// tree-sitter-java cannot parse array-type annotations on varargs params
// (e.g. `@Nullable Object @Nullable ... args`). The second annotation causes
// error recovery to truncate the enclosing class_declaration, orphaning all
@@ -320,6 +373,26 @@ export function parseFile(filePath: string, source: string): FileParseResult | n
const query = getCachedQuery(cacheKey, grammar, queries);
const matches = query.matches(tree.rootNode);
+ // Pre-partition matches so each pass only iterates its relevant subset.
+ // Pass 1: definitions, heritage, Python typed-param/assign annotations.
+ // Pass 2: imports, calls, type references.
+ // Without this, every definition match does 5 useless find() calls in pass 2,
+ // and every call match does 4 useless find() calls in pass 1.
+ const pass1Matches: any[] = [];
+ const pass2Matches: any[] = [];
+ for (const m of matches) {
+ let isPass1 = false;
+ for (const c of m.captures) {
+ const n = c.name;
+ if (n.startsWith('definition.') || n.startsWith('heritage.') ||
+ n === '_typed_param_scope' || n === '_assign_scope') {
+ isPass1 = true;
+ break;
+ }
+ }
+ (isPass1 ? pass1Matches : pass2Matches).push(m);
+ }
+
const fileName = nodePath.basename(filePath);
let sourceLineCount = 1;
for (let i = 0; i < source.length; i++) {
@@ -354,7 +427,7 @@ export function parseFile(filePath: string, source: string): FileParseResult | n
const assignTypeMap = new Map<string, Map<string, string>>();
// --- First pass: collect definitions ---
- for (const match of matches) {
+ for (const match of pass1Matches) {
// Definition captures: name + definition.*
const defCapture = match.captures.find((c: any) =>
c.name.startsWith('definition.')
@@ -487,7 +560,7 @@ export function parseFile(filePath: string, source: string): FileParseResult | n
}
// --- Second pass: calls and imports ---
- for (const match of matches) {
+ for (const match of pass2Matches) {
// Full import statement captures (Scala: reconstructs dotted package paths)
const importStmt = match.captures.find((c: any) => c.name === 'import.stmt');
if (importStmt) {
@@ -521,12 +594,8 @@ export function parseFile(filePath: string, source: string): FileParseResult | n
// Import captures (JS/TS/Python path-based)
const importSource = match.captures.find((c: any) => c.name === 'import.source');
if (importSource) {
- let importPath = importSource.node.text
- .replace(/^["'`]|["'`]$/g, '') // strip quotes
- .replace(/\\\\/g, '/'); // normalise backslashes
- if (importPath.length > 0 && importPath !== '*') {
- const rawMod = importPath.split('/').filter((s: string) => s !== '*').pop() ?? importPath;
- const modName = rawMod.replace(/^\.+/, ''); // strip leading dots: '.models' → 'models'
+ const modName = normalizeCapturedImport(importSource.node.text, language);
+ if (modName.length > 0 && modName !== '*') {
if (!modName) continue; // skip bare '.' relative imports
entities.push({ name: modName, kind: 'module', lineStart: importSource.node.startPosition.row + 1, lineEnd: importSource.node.startPosition.row + 1, language });
relationships.push({ srcName: fileName, dstName: modName, predicate: 'IMPORTS' });
@@ -822,7 +891,7 @@ export function resolveEdges(results: FileParseResult[], stats?: {
for (let i = parts.length - 1; i >= parts.length - maxDepth; i--) {
const pkg = parts.slice(i).join('.');
const list = packageToFiles.get(pkg) ?? [];
- if (!list.includes(r.filePath)) list.push(r.filePath);
+ list.push(r.filePath);
packageToFiles.set(pkg, list);
}
}
@@ -837,7 +906,7 @@ export function resolveEdges(results: FileParseResult[], stats?: {
if (nodePath.extname(r.filePath) !== '.go') continue;
const dirName = nodePath.basename(nodePath.dirname(r.filePath));
const list = goPkgDirToFiles.get(dirName) ?? [];
- if (!list.includes(r.filePath)) list.push(r.filePath);
+ list.push(r.filePath);
goPkgDirToFiles.set(dirName, list);
}
@@ -854,6 +923,10 @@ export function resolveEdges(results: FileParseResult[], stats?: {
// (TS/JS ESM imports use .js extensions that map to .ts source files)
const noExt = modName.replace(/\.(js|ts|mjs|cjs|jsx|tsx|py|scala|java|c|cc|cpp|cxx|h|hh|hpp|hxx)$/, '');
if (noExt !== modName && noExt && !candidates.includes(noExt)) candidates.push(noExt);
+ const pathBasename = nodePath.posix.basename(modName);
+ if (pathBasename && !candidates.includes(pathBasename)) candidates.push(pathBasename);
+ const pathBasenameNoExt = nodePath.posix.basename(noExt);
+ if (pathBasenameNoExt && !candidates.includes(pathBasenameNoExt)) candidates.push(pathBasenameNoExt);
// For dotted paths (Scala/Java: 'ix.memory.model.Edge'), also try last segment
const lastDot = modName.lastIndexOf('.');
if (lastDot !== -1) {
@@ -947,6 +1020,21 @@ export function resolveEdges(results: FileParseResult[], stats?: {
if (canonicalMatches.length === 1) return canonicalMatches;
}
+ if (srcLanguage === SupportedLanguages.Java) {
+ const srcParts = srcFilePath.replace(/\\/g, '/').split('/');
+ const withProximity = matches.map(fp => {
+ const fpParts = fp.replace(/\\/g, '/').split('/');
+ let common = 0;
+ while (common < srcParts.length && common < fpParts.length && srcParts[common] === fpParts[common]) {
+ common++;
+ }
+ return { fp, common };
+ });
+ const maxCommon = Math.max(...withProximity.map(x => x.common));
+ const proximityMatches = withProximity.filter(x => x.common === maxCommon).map(x => x.fp);
+ if (proximityMatches.length === 1) return proximityMatches;
+ }
+
if (srcLanguage !== SupportedLanguages.C && srcLanguage !== SupportedLanguages.CPlusPlus) return matches;
let narrowed = matches;
@@ -1025,21 +1113,44 @@ export function resolveEdges(results: FileParseResult[], stats?: {
// name ("ClaimId"), find files registered under that package, then find
// the one that defines the entity.
if (rel.predicate === 'IMPORTS') {
- if (result.language !== SupportedLanguages.Scala && result.language !== SupportedLanguages.Java) continue;
- const dstName = rel.dstName;
- const lastDot = dstName.lastIndexOf('.');
- if (lastDot === -1) continue; // not a dotted class import
- const entityName = dstName.slice(lastDot + 1);
- if (!entityName || entityName === '_') continue; // wildcard import
- const pkgPath = dstName.slice(0, lastDot); // e.g. "ix.memory.model"
- // Find files in the declared package that define this entity
- const pkgFiles = packageToFiles.get(pkgPath) ?? [];
- const matchFiles = pkgFiles.filter(fp => fp !== srcFilePath && fileHasSymbol.get(fp)?.has(entityName));
- if (matchFiles.length !== 1) continue; // ambiguous or not found
- const fp = matchFiles[0];
- 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 });
+ if (result.language === SupportedLanguages.Scala || result.language === SupportedLanguages.Java) {
+ const dstName = rel.dstName;
+ const lastDot = dstName.lastIndexOf('.');
+ if (lastDot !== -1) {
+ const entityName = dstName.slice(lastDot + 1);
+ if (entityName && entityName !== '_') {
+ const pkgPath = dstName.slice(0, lastDot);
+ const pkgFiles = packageToFiles.get(pkgPath) ?? [];
+ const matchFiles = pkgFiles.filter(fp => fp !== srcFilePath && fileHasSymbol.get(fp)?.has(entityName));
+ if (matchFiles.length === 1) {
+ const fp = matchFiles[0];
+ const dstQualifiedKey = bestQKey(fileQKeys, fp, entityName);
+ if (dstQualifiedKey !== null) {
+ resolved.push({ srcFilePath, srcName: rel.srcName, dstFilePath: fp, dstName, dstQualifiedKey, predicate: 'IMPORTS', confidence: 0.9 });
+ stats.resolvedImport++;
+ continue;
+ }
+ }
+ }
+ }
+ }
+
+ const importMatches = modNameToFiles(rel.dstName, srcFilePath);
+ if (importMatches.length === 1) {
+ const fp = importMatches[0];
+ resolved.push({
+ srcFilePath,
+ srcName: rel.srcName,
+ dstFilePath: fp,
+ dstName: rel.dstName,
+ dstQualifiedKey: fileEntityName(fp),
+ predicate: 'IMPORTS',
+ confidence: 0.9,
+ });
+ stats.resolvedImport++;
+ } else if (importMatches.length > 1) {
+ stats.skippedAmbiguous++;
+ }
continue;
}
@@ -1119,6 +1230,13 @@ export function resolveEdges(results: FileParseResult[], stats?: {
}
// Tier 3: global fallback (confidence 0.5) — uses inverted symbol index
// instead of scanning all files.
+ // Skip for C/C++ REFERENCES: struct/type references that don't resolve via the
+ // import chain come from system headers (e.g. <net/if.h>) and must not be
+ // linked to an in-repo definition of the same name.
+ if (rel.predicate === 'REFERENCES' &&
+ (srcLanguage === SupportedLanguages.C || srcLanguage === SupportedLanguages.CPlusPlus)) {
+ continue;
+ }
stats.globalFallbacks++;
const candidates = symbolToFiles.get(dstName) ?? [];
let globalMatches = candidates.filter(fp => fp !== srcFilePath && fileLanguage.get(fp) === srcLanguage);
diff --git a/core-ingestion/src/queries.ts b/core-ingestion/src/queries.ts
index 959bcf12..a65fe764 100644
--- a/core-ingestion/src/queries.ts
+++ b/core-ingestion/src/queries.ts
@@ -264,6 +264,14 @@ export const PYTHON_QUERIES = `
(typed_parameter type: (type (identifier) @reference.type))
(typed_default_parameter type: (type (identifier) @reference.type))
+; Class/function references passed as keyword argument values
+; e.g. LoginView.as_view(authentication_form=AdminAuthenticationForm, ...)
+; Emits a CALLS edge from the enclosing function to the referenced identifier.
+(call
+ arguments: (argument_list
+ (keyword_argument
+ value: (identifier) @call.name)))
+
; Typed parameters: capture name + type together so qualifier substitution can
; map a local variable name to its declared class (e.g. query: Query → 'query' → 'Query').
(function_definition
diff --git a/core-ingestion/tsconfig.json b/core-ingestion/tsconfig.json
index 77af2577..3c32c63f 100644
--- a/core-ingestion/tsconfig.json
+++ b/core-ingestion/tsconfig.json
@@ -11,5 +11,6 @@
"skipLibCheck": true,
"types": ["node"]
},
- "include": ["src"]
+ "include": ["src"],
+ "exclude": ["src/__tests__"]
}
diff --git a/ix-cli/package-lock.json b/ix-cli/package-lock.json
index e92ca000..be5f49f1 100644
--- a/ix-cli/package-lock.json
+++ b/ix-cli/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "@ix/cli",
- "version": "0.4.0",
+ "version": "0.4.4",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@ix/cli",
- "version": "0.3.0",
+ "version": "0.4.4",
"dependencies": {
"chalk": "^5.3.0",
"commander": "^12.0.0",
diff --git a/ix-cli/src/cli/__tests__/search-ranking.test.ts b/ix-cli/src/cli/__tests__/search-ranking.test.ts
index 19eac811..5cfc9444 100644
--- a/ix-cli/src/cli/__tests__/search-ranking.test.ts
+++ b/ix-cli/src/cli/__tests__/search-ranking.test.ts
@@ -1,5 +1,5 @@
import { describe, it, expect, vi } from "vitest";
-import { resolveEntityFull } from "../resolve.js";
+import { resolveEntityFull, scoreCandidate } from "../resolve.js";
/**
* Verify that search ranking correctly incorporates backend _search_weight.
@@ -8,8 +8,8 @@ import { resolveEntityFull } from "../resolve.js";
* without needing a running backend.
*/
-/** Minimal scoreCandidate replica for testing (exact name = 0, prefix = 15, fuzzy = 30). */
-function scoreCandidate(node: any, term: string, opts?: { kind?: string; path?: string }): number {
+/** Minimal scoreCandidate replica for rankScore tests (exact name = 0, prefix = 15, fuzzy = 30). */
+function scoreCandidateSimple(node: any, term: string, opts?: { kind?: string; path?: string }): number {
const name: string = (node.name || "").toLowerCase();
const kind: string = (node.kind || "").toLowerCase();
const symbolLower = term.toLowerCase();
@@ -32,7 +32,7 @@ function rankScore(
): { tier: number; score: number; matchSource: string } {
// Weight comes through attrs (embedded by AQL into attrs JSON)
const backendWeight: number = node.attrs?._search_weight ?? node._search_weight ?? 0;
- const resolverScore = scoreCandidate(node, term, { kind: requestedKind, path: pathFilter });
+ const resolverScore = scoreCandidateSimple(node, term, { kind: requestedKind, path: pathFilter });
if (backendWeight >= 100) {
if (resolverScore <= -3) return { tier: 0, score: -backendWeight + resolverScore, matchSource: "name_exact" };
@@ -116,6 +116,83 @@ describe("search ranking with backend weight", () => {
});
});
+describe("scoreCandidate: structural kinds outrank chunks", () => {
+ // BUG-1 / BUG-2 regression: for PascalCase names, chunks must never outscore
+ // function/method entities. Previously a +3 penalty on method/function for
+ // looksTypeLikeSymbol names let chunks (score=0) beat them (score=2).
+
+ it("method outscores chunk for PascalCase name (Apply)", () => {
+ const method = { name: "Apply", kind: "method", provenance: { sourceUri: "etcd/apply/backend.go" } };
+ const chunk = { name: "Apply", kind: "chunk", provenance: { sourceUri: "etcd/apply/backend.go" } };
+ expect(scoreCandidate(method, "Apply")).toBeLessThan(scoreCandidate(chunk, "Apply"));
+ });
+
+ it("function outscores chunk for PascalCase name (StartEtcd)", () => {
+ const fn = { name: "StartEtcd", kind: "function", provenance: { sourceUri: "etcd/embed/etcd.go" } };
+ const chunk = { name: "StartEtcd", kind: "chunk", provenance: { sourceUri: "etcd/embed/etcd.go" } };
+ expect(scoreCandidate(fn, "StartEtcd")).toBeLessThan(scoreCandidate(chunk, "StartEtcd"));
+ });
+
+ it("class still outscores function for PascalCase type-like name", () => {
+ const cls = { name: "EtcdServer", kind: "class", provenance: { sourceUri: "etcd/server.go" } };
+ const fn = { name: "EtcdServer", kind: "function", provenance: { sourceUri: "etcd/server.go" } };
+ expect(scoreCandidate(cls, "EtcdServer")).toBeLessThan(scoreCandidate(fn, "EtcdServer"));
+ });
+});
+
+describe("resolveEntityFull: method preferred over chunk for PascalCase names", () => {
+ // BUG-1 regression: resolving "Apply" should return the method entity, not a chunk.
+ it("resolves to method entity, not chunk, when both have the same name", async () => {
+ const method = {
+ id: "method-apply-id",
+ name: "Apply",
+ kind: "method",
+ provenance: { sourceUri: "etcd/apply/backend.go" },
+ };
+ const chunk = {
+ id: "chunk-apply-id",
+ name: "Apply",
+ kind: "chunk",
+ provenance: { sourceUri: "etcd/apply/backend.go" },
+ };
+ const mockClient = {
+ search: vi.fn().mockResolvedValue([chunk, method]),
+ } as any;
+
+ const result = await resolveEntityFull(mockClient, "Apply", ["method"], { path: "etcd" });
+ expect(result.resolved).toBe(true);
+ if (result.resolved) {
+ expect(result.entity.id).toBe("method-apply-id");
+ expect(result.entity.kind).toBe("method");
+ }
+ });
+
+ it("resolves StartEtcd to function entity, not chunk (BUG-2 regression)", async () => {
+ const fn = {
+ id: "fn-startetcd-id",
+ name: "StartEtcd",
+ kind: "function",
+ provenance: { sourceUri: "etcd/embed/etcd.go" },
+ };
+ const chunk = {
+ id: "chunk-startetcd-id",
+ name: "StartEtcd",
+ kind: "chunk",
+ provenance: { sourceUri: "etcd/embed/etcd.go" },
+ };
+ const mockClient = {
+ search: vi.fn().mockResolvedValue([chunk, fn]),
+ } as any;
+
+ const result = await resolveEntityFull(mockClient, "StartEtcd", ["function"], { path: "etcd" });
+ expect(result.resolved).toBe(true);
+ if (result.resolved) {
+ expect(result.entity.id).toBe("fn-startetcd-id");
+ expect(result.entity.kind).toBe("function");
+ }
+ });
+});
+
describe("resolveEntityFull --path hard exclusion", () => {
it("excludes out-of-scope candidates even on exact name match", async () => {
// Simulates Bug 1: etcd has 'Notifier' but prometheus does not.
diff --git a/ix-cli/src/cli/commands/ingest.ts b/ix-cli/src/cli/commands/ingest.ts
index 8769f524..3613f1ce 100644
--- a/ix-cli/src/cli/commands/ingest.ts
+++ b/ix-cli/src/cli/commands/ingest.ts
@@ -2,7 +2,9 @@ import * as nodePath from 'node:path';
import * as fs from 'node:fs';
import * as crypto from 'node:crypto';
import * as os from 'node:os';
+import { fileURLToPath } from 'node:url';
import type { Command } from 'commander';
+import { ParsePool } from './parse-pool.js';
import chalk from 'chalk';
import { IxClient } from '../../client/api.js';
import type { GraphPatchPayload } from '../../client/types.js';
@@ -135,7 +137,8 @@ const PROG_LINE_WIDTH = 72;
function renderProgressLine(phase: string, current: number, total: number): string {
if (total === 0) {
- return ` ${phase}...`.padEnd(PROG_LINE_WIDTH);
+ const bar = chalk.dim('░'.repeat(PROG_BAR_WIDTH));
+ return ` ${phase.padEnd(8)} ${bar} 0%`.padEnd(PROG_LINE_WIDTH);
}
const pct = Math.min(current / total, 1);
const filled = Math.round(pct * PROG_BAR_WIDTH);
@@ -204,20 +207,13 @@ export async function ingestFiles(
const client = new IxClient(getEndpoint());
const start = performance.now();
- let progressPhase = 'Scanning';
+ let progressPhase = 'Parsing';
let progressCurrent = 0;
let progressTotal = 0;
let progressStart = performance.now();
const interval = opts.format === 'text' ? setInterval(() => {
- const elapsed = performance.now() - progressStart;
- const tau = progressPhase === 'Saving' ? 3000 : 2000;
- let display = progressCurrent;
- if (progressTotal > 0 && progressCurrent < progressTotal) {
- const simulated = Math.floor((1 - Math.exp(-elapsed / tau)) * 0.88 * progressTotal);
- display = Math.max(display, simulated);
- }
- process.stderr.write('\r' + renderProgressLine(progressPhase, display, progressTotal));
+ process.stderr.write('\r' + renderProgressLine(progressPhase, progressCurrent, progressTotal));
}, 80) : null;
let filesDiscovered = 0;
@@ -237,6 +233,13 @@ export async function ingestFiles(
resolvedGlobal: 0, resolvedQualifier: 0, skippedSameFile: 0, skippedAmbiguous: 0,
};
+ const workerPath = nodePath.resolve(
+ nodePath.dirname(fileURLToPath(import.meta.url)),
+ '../../../../core-ingestion/dist/parse-worker.js',
+ );
+ const pool = new ParsePool(workerPath, Math.max(1, os.cpus().length - 1));
+ pool.init();
+
try {
// Phase: discover files
const langFilter = opts.lang ? parseLangs(opts.lang) : null;
@@ -257,9 +260,8 @@ export async function ingestFiles(
timings.discoverMs = Math.round(discovered - start);
progressPhase = 'Parsing';
- progressTotal = filePaths.length;
+ progressTotal = 0;
progressCurrent = 0;
- progressStart = performance.now();
// Phase: mtime pre-filter — skip readFileSync+sha256 for files whose mtime
// is unchanged since the last successful ingest (common "ix map" re-run case).
@@ -340,6 +342,7 @@ export async function ingestFiles(
const flushBatch = async (batch: ParsedFile[]): Promise<void> => {
if (batch.length === 0) return;
filesChanged += batch.length;
+ await new Promise<void>(r => setImmediate(r));
const resolvedEdges = resolveEdgesFn!(batch.map(f => f.parsed), resolveStats);
const batchEdgesByFile = new Map<string, any[]>();
@@ -349,6 +352,7 @@ export async function ingestFiles(
arr.push(edge);
}
+ const subChunks: GraphPatchPayload[][] = [];
for (let i = 0; i < batch.length; i += COMMIT_CHUNK_SIZE) {
const end = Math.min(i + COMMIT_CHUNK_SIZE, batch.length);
const patchChunk: GraphPatchPayload[] = [];
@@ -361,7 +365,9 @@ export async function ingestFiles(
process.stderr.write(`\n [patch build error] ${p.filePath}: ${err}\n`);
}
}
- if (patchChunk.length === 0) continue;
+ if (patchChunk.length > 0) subChunks.push(patchChunk);
+ }
+ await Promise.all(subChunks.map(async (patchChunk) => {
try {
const result = await client.commitPatchBulk(patchChunk);
if (result.rev > latestRev) latestRev = result.rev;
@@ -379,14 +385,16 @@ export async function ingestFiles(
}
}
}
- }
+ }));
};
if ((knownHashes.size > 0 || mtimeChangedPaths.length === 0) && !opts.force) {
// Path A: has baseline or all mtime-clean → pre-scan to detect changes before loading modules.
// If nothing changed, module load is skipped entirely.
const changedPaths: Array<{ filePath: string; bytes: Buffer; hash: string; previousHash: string | undefined }> = [];
- for (const filePath of mtimeChangedPaths) {
+ for (let k = 0; k < mtimeChangedPaths.length; k++) {
+ if (k > 0 && k % 200 === 0) await new Promise<void>(r => setImmediate(r));
+ const filePath = mtimeChangedPaths[k];
try {
const bytes = fs.readFileSync(filePath);
const hash = sha256(bytes);
@@ -405,25 +413,30 @@ export async function ingestFiles(
timings.moduleLoadMs = Math.round(performance.now() - moduleStart);
resolveEdgesFn = ingestion.resolveEdges as Function;
buildPatchFn = patchBuilder.buildPatchWithResolution as Function;
-
- let batch: ParsedFile[] = [];
- for (let i = 0; i < changedPaths.length; i++) {
- const { filePath, bytes, hash, previousHash } = changedPaths[i];
- try {
- const parsed = (ingestion.parseFile as Function)(filePath, bytes.toString('utf-8'));
- if (!parsed) { filesSkipped++; progressCurrent++; continue; }
+ progressPhase = 'Parsing';
+ progressTotal = changedPaths.length;
+ progressCurrent = 0;
+ if (interval) process.stderr.write('\r' + renderProgressLine(progressPhase, progressCurrent, progressTotal));
+
+ let pendingFlush: Promise<void> = Promise.resolve();
+ for (let i = 0; i < changedPaths.length; i += PARSE_STREAM_CHUNK) {
+ const chunk = changedPaths.slice(i, i + PARSE_STREAM_CHUNK);
+ const parseResults = await Promise.all(
+ chunk.map(({ filePath, bytes }) =>
+ pool.parse(filePath, bytes.toString('utf-8')).then(r => { progressCurrent++; return r; }),
+ ),
+ );
+ const batch: ParsedFile[] = [];
+ for (let j = 0; j < chunk.length; j++) {
+ const parsed = parseResults[j] as any;
+ if (!parsed) { filesSkipped++; continue; }
entitiesParsed += parsed.entities.length;
- batch.push({ filePath, parsed, hash, previousHash });
- progressCurrent++;
- } catch (err) {
- parseErrors++;
- progressCurrent++;
- process.stderr.write(`\n [parse error] ${filePath}: ${err}\n`);
+ batch.push({ filePath: chunk[j].filePath, parsed, hash: chunk[j].hash, previousHash: chunk[j].previousHash });
}
- if ((i + 1) % YIELD_EVERY === 0) await new Promise<void>(resolve => setImmediate(resolve));
- if (batch.length >= PARSE_STREAM_CHUNK) { await flushBatch(batch); batch = []; }
+ await pendingFlush;
+ pendingFlush = flushBatch(batch);
}
- await flushBatch(batch);
+ await pendingFlush;
}
} else {
// Path B: no baseline (first ingest) or --force → load modules, then stream parse + commit.
@@ -432,32 +445,52 @@ export async function ingestFiles(
timings.moduleLoadMs = Math.round(performance.now() - moduleStart);
resolveEdgesFn = ingestion.resolveEdges as Function;
buildPatchFn = patchBuilder.buildPatchWithResolution as Function;
-
- let batch: ParsedFile[] = [];
- for (let i = 0; i < filePaths.length; i++) {
- const filePath = filePaths[i];
- try {
- const fileSize = fs.statSync(filePath).size;
- if (fileSize === 0) { filesSkipped++; progressCurrent++; continue; }
- if (fileSize > MAX_FILE_BYTES) { tooLarge++; progressCurrent++; continue; }
- const bytes = fs.readFileSync(filePath);
- const hash = sha256(bytes);
- if (!opts.force && knownHashes.get(filePath) === hash) { filesSkipped++; progressCurrent++; continue; }
- const previousHash = knownHashes.get(filePath);
- const parsed = (ingestion.parseFile as Function)(filePath, bytes.toString('utf-8'));
- if (!parsed) { filesSkipped++; progressCurrent++; continue; }
+ progressPhase = 'Parsing';
+ progressTotal = filePaths.length;
+ progressCurrent = 0;
+ if (interval) process.stderr.write('\r' + renderProgressLine(progressPhase, progressCurrent, progressTotal));
+
+ let pendingFlush: Promise<void> = Promise.resolve();
+ for (let i = 0; i < filePaths.length; i += PARSE_STREAM_CHUNK) {
+ const chunk = filePaths.slice(i, i + PARSE_STREAM_CHUNK);
+ type FileData = { filePath: string; bytes: Buffer; hash: string; previousHash: string | undefined };
+ const fileData: Array<FileData | null> = [];
+ for (const filePath of chunk) {
+ try {
+ const fileSize = fs.statSync(filePath).size;
+ if (fileSize === 0) { filesSkipped++; fileData.push(null); continue; }
+ if (fileSize > MAX_FILE_BYTES) { tooLarge++; fileData.push(null); continue; }
+ const bytes = fs.readFileSync(filePath);
+ const hash = sha256(bytes);
+ if (!opts.force && knownHashes.get(filePath) === hash) { filesSkipped++; fileData.push(null); continue; }
+ const previousHash = knownHashes.get(filePath);
+ fileData.push({ filePath, bytes, hash, previousHash: previousHash !== hash ? previousHash : undefined });
+ } catch (err) {
+ parseErrors++;
+ fileData.push(null);
+ process.stderr.write(`\n [read error] ${filePath}: ${err}\n`);
+ }
+ }
+ const parseResults = await Promise.all(
+ fileData.map(f =>
+ f
+ ? pool.parse(f.filePath, f.bytes.toString('utf-8')).then(r => { progressCurrent++; return r; })
+ : Promise.resolve(null),
+ ),
+ );
+ const batch: ParsedFile[] = [];
+ for (let j = 0; j < fileData.length; j++) {
+ const f = fileData[j];
+ if (!f) continue;
+ const parsed = parseResults[j] as any;
+ if (!parsed) { filesSkipped++; continue; }
entitiesParsed += parsed.entities.length;
- batch.push({ filePath, parsed, hash, previousHash: previousHash !== hash ? previousHash : undefined });
- progressCurrent++;
- } catch (err) {
- parseErrors++;
- progressCurrent++;
- process.stderr.write(`\n [parse error] ${filePath}: ${err}\n`);
+ batch.push({ filePath: f.filePath, parsed, hash: f.hash, previousHash: f.previousHash });
}
- if ((i + 1) % YIELD_EVERY === 0) await new Promise<void>(resolve => setImmediate(resolve));
- if (batch.length >= PARSE_STREAM_CHUNK) { await flushBatch(batch); batch = []; }
+ await pendingFlush;
+ pendingFlush = flushBatch(batch);
}
- await flushBatch(batch);
+ await pendingFlush;
}
const parsed = performance.now();
@@ -476,6 +509,7 @@ export async function ingestFiles(
saveMtimeCache(projectRoot, currentMtimes);
}
} finally {
+ await pool.destroy();
if (interval) {
clearInterval(interval);
if (progressTotal > 0) {
diff --git a/ix-cli/src/cli/commands/map.ts b/ix-cli/src/cli/commands/map.ts
index 3388ba71..5c2aae11 100644
--- a/ix-cli/src/cli/commands/map.ts
+++ b/ix-cli/src/cli/commands/map.ts
@@ -170,7 +170,8 @@ Examples:
if (mapInterval) { clearInterval(mapInterval); process.stderr.write('\r' + ' '.repeat(60) + '\r'); }
const mapMs = Math.round(performance.now() - mapStart);
if (opts.format !== "json") {
- process.stderr.write(chalk.dim(` ingest ${ingestMs}ms · map ${mapMs}ms\n`));
+ const mapSec = (mapMs / 1000).toFixed(1);
+ process.stderr.write(chalk.dim(` Mapped in ${mapSec}s\n`));
}
const minConf = parseFloat(opts.minConfidence ?? "0");
diff --git a/ix-cli/src/cli/commands/trace.ts b/ix-cli/src/cli/commands/trace.ts
index 0a79d258..75d5625a 100644
--- a/ix-cli/src/cli/commands/trace.ts
+++ b/ix-cli/src/cli/commands/trace.ts
@@ -117,7 +117,7 @@ async function buildTraceTree(
// ── Path search (BFS) ────────────────────────────────────────────────
-async function findPath(
+export async function findPath(
client: IxClient,
fromId: string,
toId: string,
@@ -134,9 +134,12 @@ async function findPath(
const { id, path } = entry;
if (path.length >= maxDepth) continue;
- const result = await client.expand(id, { direction: "out", predicates, hops: 1 });
+ const [outResult, inResult] = await Promise.all([
+ client.expand(id, { direction: "out", predicates, hops: 1 }),
+ client.expand(id, { direction: "in", predicates, hops: 1 }),
+ ]);
- for (const n of result.nodes) {
+ for (const n of [...outResult.nodes, ...inResult.nodes]) {
const name = n.name || n.attrs?.name || n.id.slice(0, 8);
if (!nodeMap.has(n.id)) {
nodeMap.set(n.id, { name, kind: n.kind ?? "unknown" });
@@ -441,7 +444,7 @@ export function registerTraceCommand(program: Command): void {
for (const child of children) {
nodes.push({ id: child.id, name: child.name, kind: child.kind });
// downstream = in edges (child → parent), upstream = out edges (parent → child)
- edges.push({ from: doUpstream ? parentId : child.id, to: doUpstream ? child.id : parentId, kind: relKind });
+ edges.push({ from: doUpstream ? child.id : parentId, to: doUpstream ? parentId : child.id, kind: relKind });
collectJson(child.id, child.children);
}
}
diff --git a/ix-cli/src/cli/commands/upgrade.ts b/ix-cli/src/cli/commands/upgrade.ts
index 0b6ad72f..ead6cf63 100644
--- a/ix-cli/src/cli/commands/upgrade.ts
+++ b/ix-cli/src/cli/commands/upgrade.ts
@@ -108,6 +108,7 @@ export async function checkForUpdate(): Promise<void> {
}
function printUpdateNotice(current: string, latest: string): void {
+ process.stderr.write('\r' + ' '.repeat(80) + '\r');
console.error("");
console.error(
chalk.yellow(` Update available: ${current} → ${latest}`)
diff --git a/ix-cli/src/cli/explain/role-inference.ts b/ix-cli/src/cli/explain/role-inference.ts
index 90ff4b5b..45eaf0d6 100644
--- a/ix-cli/src/cli/explain/role-inference.ts
+++ b/ix-cli/src/cli/explain/role-inference.ts
@@ -68,7 +68,8 @@ export function inferRole(facts: EntityFacts): RoleInference {
// ── 1. Test ──────────────────────────────────────────────────────────
if (
- nameOrPathContains(facts, "test", "spec", "__tests__") ||
+ /[/\\](tests?|spec|__tests__)[/\\]/i.test(facts.path ?? "") ||
+ /\.(test|spec)\.[a-z]+$/i.test(facts.path ?? "") ||
tokens.includes("test") ||
tokens.includes("spec")
) {
diff --git a/ix-cli/src/cli/explain/system-meaning.ts b/ix-cli/src/cli/explain/system-meaning.ts
index 6e135788..1802d6b4 100644
--- a/ix-cli/src/cli/explain/system-meaning.ts
+++ b/ix-cli/src/cli/explain/system-meaning.ts
@@ -27,7 +27,7 @@ function domainNounFromName(name: string): string {
.toLowerCase()
.split(/\s+/);
// Drop common suffixes that aren't the domain noun
- const drop = new Set(["kind", "type", "api", "result", "config", "service", "interface"]);
+ const drop = new Set(["kind", "type", "api", "result", "config", "service", "interface", "i"]);
const domainWords = words.filter((w) => !drop.has(w));
return domainWords.length > 0 ? domainWords[0] : words[0];
}
diff --git a/ix-cli/src/cli/resolve.ts b/ix-cli/src/cli/resolve.ts
index 3582fff0..c656676f 100644
--- a/ix-cli/src/cli/resolve.ts
+++ b/ix-cli/src/cli/resolve.ts
@@ -35,6 +35,10 @@ const STRUCTURAL_KINDS = new Set([
...CONTAINER_KINDS, "function", "method",
]);
+function looksTypeLikeSymbol(symbol: string): boolean {
+ return /^[A-Z][A-Za-z0-9_]*$/.test(symbol);
+}
+
// ── Scoring ───────────────────────────────────────────────────────────────
/**
@@ -77,9 +81,20 @@ export function scoreCandidate(
score -= 3; // containers are high-value resolution targets
} else if (STRUCTURAL_KINDS.has(kind)) {
score -= 1; // methods/functions are useful but lower than containers
+ } else if (kind === "chunk") {
+ score += 5; // chunks are retrieval units, not useful as trace starting points
}
// non-structural kinds (config_entry, doc, decision, etc.) get no boost
+ if (looksTypeLikeSymbol(symbol) && name === symbolLower) {
+ if (kind === "class" || kind === "interface" || kind === "trait" || kind === "object") {
+ score -= 4;
+ }
+ // No penalty for method/function: the +3 that was here caused chunks (score=0)
+ // to outscore structural entities (function/method net score=2) for PascalCase
+ // names like "Apply", "StartEtcd", "Range". Chunks are now penalised explicitly above.
+ }
+
// ── Path match ──────────────────────────────────────────────────────
if (opts?.path) {
const pathLower = opts.path.toLowerCase();
@@ -129,7 +144,11 @@ export async function resolveEntityFull(
opts?: { kind?: string; path?: string; pick?: number; includeTests?: boolean; testsOnly?: boolean; searchLimit?: number }
): Promise<ResolveResult> {
const kindFilter = opts?.kind;
- const nodes = await client.search(symbol, { limit: opts?.searchLimit ?? 20, kind: kindFilter, nameOnly: true });
+ const nodes = await client.search(symbol, {
+ limit: opts?.searchLimit ?? (looksTypeLikeSymbol(symbol) ? 50 : 20),
+ kind: kindFilter,
+ nameOnly: true,
+ });
if (nodes.length === 0) {
stderr(`No entity found matching "${symbol}".`);
@@ -202,8 +221,6 @@ function pickBest(
preferredKinds: string[],
opts?: { kind?: string; path?: string }
): ResolveResult | null {
- // Here is a test
- // LEt's see if it worked
// Score all candidates
const scored = candidates.map(n => ({
node: n,
@@ -351,7 +368,6 @@ function nodeToResolved(node: any, symbol: string, mode: ResolutionMode): Resolv
}
function buildAmbiguous(nodes: any[], scores?: number[]): AmbiguousResult {
- //this is a test
const seen = new Set<string>();
const candidates: AmbiguousResult["candidates"] = [];
let rank = 0;