index.ts 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314
  1. /**
  2. * Reference Resolution Orchestrator
  3. *
  4. * Coordinates all reference resolution strategies.
  5. */
  6. import * as fs from 'fs';
  7. import * as path from 'path';
  8. import { Language, Node, UnresolvedReference, Edge } from '../types';
  9. import { QueryBuilder } from '../db/queries';
  10. import {
  11. UnresolvedRef,
  12. ResolvedRef,
  13. ResolutionResult,
  14. ResolutionContext,
  15. FrameworkResolver,
  16. ImportMapping,
  17. } from './types';
  18. import { matchReference, matchFunctionRef, matchDottedCallChain, matchScopedCallChain, matchMethodCall, sameLanguageFamily, crossesKnownFamily, dumpNameMatcherProfile } from './name-matcher';
  19. import { resolveViaImport, resolveJvmImport, extractImportMappings, extractReExports, loadCppIncludeDirs, isPhpIncludePathRef, isCobolCopybookRef, isNixPathImportRef, clearImportResolverMemos } from './import-resolver';
  20. import { ResolverPool, minRefsForPool } from './resolver-pool';
  21. import { detectFrameworks } from './frameworks';
  22. import { synthesizeCallbackEdges } from './callback-synthesizer';
  23. import { createYielder, type MaybeYield } from './cooperative-yield';
  24. import { loadProjectAliases, type AliasMap } from './path-aliases';
  25. import { loadGoModule, type GoModule } from './go-module';
  26. import { loadWorkspacePackages, type WorkspacePackages } from './workspace-packages';
  27. import { logDebug } from '../errors';
  28. import type { ReExport } from './types';
  29. import { LRUCache } from './lru-cache';
  30. /** Node kinds that can declare supertypes (extends/implements). */
  31. const SUPERTYPE_BEARING_KINDS = new Set<Node['kind']>([
  32. 'class', 'struct', 'interface', 'trait', 'protocol', 'enum',
  33. ]);
  34. /**
  35. * Languages whose chained static-factory/fluent calls defer to the conformance
  36. * second pass. Dotted-receiver languages resolve via matchDottedCallChain; the
  37. * `::`-receiver ones (Rust) via matchScopedCallChain.
  38. */
  39. const CHAIN_LANGUAGES = new Set(['java', 'kotlin', 'csharp', 'swift', 'rust', 'go', 'scala', 'dart', 'objc', 'pascal']);
  40. const SCOPED_CHAIN_LANGUAGES = new Set(['rust']);
  41. /** The extractor's chained-receiver encoding: `<inner>().<method>`. */
  42. const CHAIN_SHAPE = /^(.+)\(\)\.(\w+)$/;
  43. /** PHP `$this->prop->method()` encoded as `this->prop.method` — no `()`, so CHAIN_SHAPE misses it. */
  44. const PHP_PROP_SHAPE = /^this->\w+\.\w+$/;
  45. /**
  46. * Cache size limits. Each per-resolver cache is bounded so memory
  47. * stays flat on large codebases (20k+ files). Sizes were chosen to
  48. * cover the working set for typical resolution batches without
  49. * exceeding a few hundred MB worst-case. Override via the env var
  50. * `CODEGRAPH_RESOLVER_CACHE_SIZE` (single integer applied to all
  51. * caches) when tuning for very large or very small projects.
  52. */
  53. const DEFAULT_CACHE_LIMIT = 5_000;
  54. function resolveCacheLimit(): number {
  55. const raw = process.env.CODEGRAPH_RESOLVER_CACHE_SIZE;
  56. if (!raw) return DEFAULT_CACHE_LIMIT;
  57. const parsed = Number.parseInt(raw, 10);
  58. if (Number.isFinite(parsed) && parsed > 0) return parsed;
  59. return DEFAULT_CACHE_LIMIT;
  60. }
  61. // Re-export types
  62. export * from './types';
  63. // Pre-built Sets for O(1) built-in lookups (allocated once, shared across all instances)
  64. const JS_BUILT_INS = new Set([
  65. 'console', 'window', 'document', 'global', 'process',
  66. 'Promise', 'Array', 'Object', 'String', 'Number', 'Boolean',
  67. 'Date', 'Math', 'JSON', 'RegExp', 'Error', 'Map', 'Set',
  68. 'setTimeout', 'setInterval', 'clearTimeout', 'clearInterval',
  69. 'fetch', 'require', 'module', 'exports', '__dirname', '__filename',
  70. ]);
  71. const REACT_HOOKS = new Set([
  72. 'useState', 'useEffect', 'useContext', 'useReducer', 'useCallback',
  73. 'useMemo', 'useRef', 'useLayoutEffect', 'useImperativeHandle', 'useDebugValue',
  74. ]);
  75. const PYTHON_BUILT_INS = new Set([
  76. 'print', 'len', 'range', 'str', 'int', 'float', 'list', 'dict', 'set', 'tuple',
  77. 'open', 'input', 'type', 'isinstance', 'hasattr', 'getattr', 'setattr',
  78. 'super', 'self', 'cls', 'None', 'True', 'False',
  79. ]);
  80. const PYTHON_BUILT_IN_TYPES = new Set([
  81. 'list', 'dict', 'set', 'tuple', 'str', 'int', 'float', 'bool',
  82. 'bytes', 'bytearray', 'frozenset', 'object', 'super',
  83. ]);
  84. const PYTHON_BUILT_IN_METHODS = new Set([
  85. 'append', 'extend', 'insert', 'remove', 'pop', 'clear', 'sort', 'reverse', 'copy',
  86. 'update', 'keys', 'values', 'items', 'get',
  87. 'add', 'discard', 'union', 'intersection', 'difference',
  88. 'split', 'join', 'strip', 'lstrip', 'rstrip', 'replace', 'lower', 'upper',
  89. 'startswith', 'endswith', 'find', 'index', 'count', 'encode', 'decode',
  90. 'format', 'isdigit', 'isalpha', 'isalnum',
  91. 'read', 'write', 'readline', 'readlines', 'close', 'flush', 'seek',
  92. ]);
  93. const GO_STDLIB_PACKAGES = new Set([
  94. 'fmt', 'os', 'io', 'net', 'http', 'log', 'math', 'sort', 'sync',
  95. 'time', 'path', 'bytes', 'strings', 'strconv', 'errors', 'context',
  96. 'json', 'xml', 'csv', 'html', 'template', 'regexp', 'reflect',
  97. 'runtime', 'testing', 'flag', 'bufio', 'crypto', 'encoding',
  98. 'filepath', 'hash', 'mime', 'rand', 'signal', 'sql', 'syscall',
  99. 'unicode', 'unsafe', 'atomic', 'binary', 'debug', 'exec', 'heap',
  100. 'ring', 'scanner', 'tar', 'zip', 'gzip', 'zlib', 'tls', 'url',
  101. 'user', 'pprof', 'trace', 'ast', 'build', 'parser', 'printer',
  102. 'token', 'types', 'cgo', 'plugin', 'race', 'ioutil',
  103. // Kubernetes-common stdlib aliases
  104. 'utilruntime', 'utilwait', 'utilnet',
  105. ]);
  106. const GO_BUILT_INS = new Set([
  107. 'make', 'new', 'len', 'cap', 'append', 'copy', 'delete', 'close',
  108. 'panic', 'recover', 'print', 'println', 'complex', 'real', 'imag',
  109. 'error', 'nil', 'true', 'false', 'iota',
  110. 'int', 'int8', 'int16', 'int32', 'int64',
  111. 'uint', 'uint8', 'uint16', 'uint32', 'uint64', 'uintptr',
  112. 'float32', 'float64', 'complex64', 'complex128',
  113. 'string', 'bool', 'byte', 'rune', 'any',
  114. ]);
  115. const PASCAL_UNIT_PREFIXES = [
  116. 'System.', 'Winapi.', 'Vcl.', 'Fmx.', 'Data.', 'Datasnap.',
  117. 'Soap.', 'Xml.', 'Web.', 'REST.', 'FireDAC.', 'IBX.',
  118. 'IdHTTP', 'IdTCP', 'IdSSL',
  119. ];
  120. const PASCAL_BUILT_INS = new Set([
  121. 'System', 'SysUtils', 'Classes', 'Types', 'Variants', 'StrUtils',
  122. 'Math', 'DateUtils', 'IOUtils', 'Generics.Collections', 'Generics.Defaults',
  123. 'Rtti', 'TypInfo', 'SyncObjs', 'RegularExpressions',
  124. 'SysInit', 'Windows', 'Messages', 'Graphics', 'Controls', 'Forms',
  125. 'Dialogs', 'StdCtrls', 'ExtCtrls', 'ComCtrls', 'Menus', 'ActnList',
  126. 'WriteLn', 'Write', 'ReadLn', 'Read', 'Inc', 'Dec', 'Ord', 'Chr',
  127. 'Length', 'SetLength', 'High', 'Low', 'Assigned', 'FreeAndNil',
  128. 'Format', 'IntToStr', 'StrToInt', 'FloatToStr', 'StrToFloat',
  129. 'Trim', 'UpperCase', 'LowerCase', 'Pos', 'Copy', 'Delete', 'Insert',
  130. 'Now', 'Date', 'Time', 'DateToStr', 'StrToDate',
  131. 'Raise', 'Exit', 'Break', 'Continue', 'Abort',
  132. 'True', 'False', 'nil', 'Self', 'Result',
  133. 'Create', 'Destroy', 'Free',
  134. 'TObject', 'TComponent', 'TPersistent', 'TInterfacedObject',
  135. 'TList', 'TStringList', 'TStrings', 'TStream', 'TMemoryStream', 'TFileStream',
  136. 'Exception', 'EAbort', 'EConvertError', 'EAccessViolation',
  137. 'IInterface', 'IUnknown',
  138. ]);
  139. const C_BUILT_INS = new Set([
  140. // Standard C library functions
  141. 'printf', 'fprintf', 'sprintf', 'snprintf', 'scanf', 'fscanf', 'sscanf',
  142. 'malloc', 'calloc', 'realloc', 'free',
  143. 'memcpy', 'memmove', 'memset', 'memcmp', 'memchr',
  144. 'strlen', 'strcpy', 'strncpy', 'strcat', 'strncat', 'strcmp', 'strncmp',
  145. 'strstr', 'strchr', 'strrchr', 'strtok', 'strdup',
  146. 'fopen', 'fclose', 'fread', 'fwrite', 'fgets', 'fputs', 'fputc', 'fgetc',
  147. 'feof', 'ferror', 'fflush', 'fseek', 'ftell', 'rewind',
  148. 'exit', 'abort', 'atexit', 'atoi', 'atol', 'atof', 'strtol', 'strtoul', 'strtod',
  149. 'qsort', 'bsearch',
  150. 'abs', 'labs', 'rand', 'srand',
  151. 'sin', 'cos', 'tan', 'sqrt', 'pow', 'log', 'log10', 'exp', 'ceil', 'floor', 'fabs',
  152. 'time', 'clock', 'difftime', 'mktime', 'localtime', 'gmtime', 'strftime', 'asctime',
  153. 'assert', 'errno',
  154. 'perror', 'remove', 'rename', 'tmpfile', 'tmpnam',
  155. 'getenv', 'system',
  156. 'signal', 'raise',
  157. 'setjmp', 'longjmp',
  158. 'va_start', 'va_end', 'va_arg', 'va_copy',
  159. 'NULL', 'EOF', 'BUFSIZ', 'FILENAME_MAX', 'RAND_MAX', 'EXIT_SUCCESS', 'EXIT_FAILURE',
  160. 'size_t', 'ptrdiff_t', 'wchar_t', 'intptr_t', 'uintptr_t',
  161. 'int8_t', 'int16_t', 'int32_t', 'int64_t',
  162. 'uint8_t', 'uint16_t', 'uint32_t', 'uint64_t',
  163. 'FILE',
  164. // POSIX additions commonly seen
  165. 'stat', 'lstat', 'fstat', 'open', 'close', 'read', 'write', 'pipe',
  166. 'fork', 'exec', 'waitpid', 'getpid', 'getppid', 'kill', 'sleep', 'usleep',
  167. 'pthread_create', 'pthread_join', 'pthread_mutex_lock', 'pthread_mutex_unlock',
  168. 'dlopen', 'dlsym', 'dlclose',
  169. ]);
  170. const CPP_BUILT_INS = new Set([
  171. // iostream objects (often used without std:: prefix via using)
  172. 'cout', 'cin', 'cerr', 'clog', 'endl', 'flush', 'ws',
  173. 'std', // the namespace itself when used as std::something
  174. // Common C++ keywords that leak as references
  175. 'nullptr', 'true', 'false', 'this', 'sizeof', 'alignof', 'typeid',
  176. 'static_cast', 'dynamic_cast', 'reinterpret_cast', 'const_cast',
  177. 'make_unique', 'make_shared', 'make_pair',
  178. 'move', 'forward', 'swap',
  179. ]);
  180. /**
  181. * Reference Resolver
  182. *
  183. * Orchestrates reference resolution using multiple strategies.
  184. */
  185. export class ReferenceResolver {
  186. private projectRoot: string;
  187. private queries: QueryBuilder;
  188. private context: ResolutionContext;
  189. private frameworks: FrameworkResolver[] = [];
  190. // Chained static-factory/fluent call refs the first pass couldn't resolve,
  191. // collected in-memory (the batched resolver deletes unresolved refs from the
  192. // DB, so they can't be re-read). Drained by resolveChainedCallsViaConformance
  193. // once implements/extends edges exist, to resolve methods on a supertype the
  194. // receiver conforms to (#750).
  195. private deferredChainRefs: UnresolvedRef[] = [];
  196. // `this.<member>` function-as-value refs whose member is NOT on the
  197. // enclosing class itself — possibly inherited. Collected in-memory for the
  198. // same reason as deferredChainRefs and drained by
  199. // resolveDeferredThisMemberRefs once implements/extends edges exist (#808).
  200. private deferredThisMemberRefs: UnresolvedRef[] = [];
  201. // Per-`.razor`/`.cshtml`-file `@using` namespace set (own directives + folder
  202. // `_Imports.razor`, cascading to the project root). Used to disambiguate a
  203. // markup type ref to the right C# namespace.
  204. private razorUsingsCache = new Map<string, string[]>();
  205. // All per-resolver caches are LRU-bounded. Previously these were
  206. // unbounded Maps that grew with every distinct lookup and OOM'd on
  207. // codebases with 20k+ files (see issue: unbounded cache growth).
  208. private nodeCache: LRUCache<string, Node[]>; // per-file node cache
  209. private fileCache: LRUCache<string, string | null>; // per-file content cache
  210. private importMappingCache: LRUCache<string, ImportMapping[]>;
  211. private reExportCache: LRUCache<string, ReExport[]>;
  212. private nameCache: LRUCache<string, Node[]>; // name → nodes cache
  213. private lowerNameCache: LRUCache<string, Node[]>; // lower(name) → nodes cache
  214. private qualifiedNameCache: LRUCache<string, Node[]>; // qualified_name → nodes cache
  215. private fileLinesCache: LRUCache<string, string[] | null>; // file → split lines cache
  216. private methodMatchCache: LRUCache<string, Node[]>; // lang\0Type::method → matching method nodes
  217. // Node kinds are a small fixed set (~24), so this is a plain Map, not an LRU.
  218. // getNodesByKind returns the FULL node list for a kind; it was previously
  219. // uncached — a per-ref `SELECT * FROM nodes WHERE kind=?` + row-mapping. Called
  220. // for every dotted call ref by the Spring resolver (constants) and every
  221. // `hook_` ref by the Drupal resolver (functions), that scan dominated
  222. // resolution on large repos (#1180). The node set is stable within a
  223. // resolution pass (same lifetime assumption as nameCache); clearCaches() resets
  224. // it between passes. Callers must treat the returned array as read-only.
  225. private nodesByKindCache = new Map<Node['kind'], Node[]>();
  226. private knownNames: Set<string> | null = null; // all known symbol names for fast pre-filtering
  227. private knownFiles: Set<string> | null = null;
  228. private cachesWarmed = false;
  229. // tsconfig/jsconfig path-alias map. `undefined` = not yet computed,
  230. // `null` = computed and absent. Treated as immutable for the
  231. // resolver's lifetime; callers re-create the resolver if config changes.
  232. private projectAliases: AliasMap | null | undefined = undefined;
  233. // go.mod module path. Same lazy/immutable convention as projectAliases.
  234. private goModule: GoModule | null | undefined = undefined;
  235. // Monorepo workspace member packages. Same lazy/immutable convention.
  236. private workspacePackages: WorkspacePackages | null | undefined = undefined;
  237. constructor(projectRoot: string, queries: QueryBuilder) {
  238. this.projectRoot = projectRoot;
  239. this.queries = queries;
  240. const limit = resolveCacheLimit();
  241. // The content cache is heavier (full file text), so we give it a
  242. // smaller budget than the metadata caches.
  243. const contentLimit = Math.max(64, Math.floor(limit / 5));
  244. this.nodeCache = new LRUCache(limit);
  245. this.fileCache = new LRUCache(contentLimit);
  246. this.importMappingCache = new LRUCache(limit);
  247. this.reExportCache = new LRUCache(limit);
  248. this.nameCache = new LRUCache(limit);
  249. this.lowerNameCache = new LRUCache(limit);
  250. this.qualifiedNameCache = new LRUCache(limit);
  251. // Split-lines arrays are heavier than content strings; refs arrive
  252. // file-ordered, so a small cache still hits nearly always.
  253. this.fileLinesCache = new LRUCache(contentLimit);
  254. this.methodMatchCache = new LRUCache(limit);
  255. this.context = this.createContext();
  256. }
  257. /**
  258. * Initialize the resolver (detect frameworks, etc.)
  259. */
  260. initialize(): void {
  261. this.frameworks = detectFrameworks(this.context);
  262. this.clearCaches();
  263. }
  264. /**
  265. * Run each framework resolver's cross-file finalization pass and persist
  266. * the returned node updates. Idempotent — safe to call after every indexAll
  267. * and every incremental sync. Returns the number of nodes updated.
  268. *
  269. * Caches are cleared before/after so the post-extract pass sees fresh DB
  270. * state and downstream queries see the updated names.
  271. */
  272. runPostExtract(): number {
  273. let updated = 0;
  274. this.clearCaches();
  275. for (const fw of this.frameworks) {
  276. if (!fw.postExtract) continue;
  277. try {
  278. const nodes = fw.postExtract(this.context);
  279. for (const node of nodes) {
  280. this.queries.updateNode(node);
  281. updated++;
  282. }
  283. } catch (err) {
  284. logDebug(`Framework '${fw.name}' postExtract failed`, {
  285. error: err instanceof Error ? err.message : String(err),
  286. });
  287. }
  288. }
  289. if (updated > 0) this.clearCaches();
  290. return updated;
  291. }
  292. /**
  293. * Pre-build lightweight caches for resolution.
  294. * Node lookups are now handled by indexed SQLite queries instead of
  295. * loading all nodes into memory (which caused OOM on large codebases).
  296. * We cache the set of known symbol names for fast pre-filtering.
  297. */
  298. warmCaches(): void {
  299. if (this.cachesWarmed) return;
  300. // Only cache the set of known file paths (lightweight string set)
  301. this.knownFiles = new Set(this.queries.getAllFilePaths());
  302. // Cache all distinct symbol names for fast pre-filtering (just strings, not full nodes)
  303. this.knownNames = new Set(this.queries.getAllNodeNames());
  304. this.cachesWarmed = true;
  305. }
  306. /**
  307. * warmCaches for the async resolution entry points: streams the distinct
  308. * name set with periodic yields instead of one synchronous `.all()`. On a
  309. * multi-million-node index the DISTINCT scan is a solid multi-second block
  310. * (measured up to 28s inside `codegraph sync` on the Linux kernel index),
  311. * long enough to matter to the #850 watchdog on slower hardware. Same
  312. * result, same memory — only the event loop keeps turning.
  313. */
  314. async warmCachesYielding(onYield: MaybeYield): Promise<void> {
  315. if (this.cachesWarmed) return;
  316. this.knownFiles = new Set(this.queries.getAllFilePaths());
  317. const names = new Set<string>();
  318. let scanned = 0;
  319. for (const name of this.queries.iterateNodeNames()) {
  320. names.add(name);
  321. if ((++scanned & 8191) === 0) await onYield();
  322. }
  323. this.knownNames = names;
  324. this.cachesWarmed = true;
  325. }
  326. /**
  327. * Clear internal caches
  328. */
  329. clearCaches(): void {
  330. this.nodeCache.clear();
  331. this.fileCache.clear();
  332. this.importMappingCache.clear();
  333. this.reExportCache.clear();
  334. this.nameCache.clear();
  335. this.lowerNameCache.clear();
  336. this.qualifiedNameCache.clear();
  337. this.fileLinesCache.clear();
  338. this.methodMatchCache.clear();
  339. this.nodesByKindCache.clear();
  340. this.knownNames = null;
  341. this.knownFiles = null;
  342. this.cachesWarmed = false;
  343. // The import-resolver's per-context memos assume the same stable window
  344. // as the caches above — drop them together.
  345. if (this.context) clearImportResolverMemos(this.context);
  346. }
  347. /** `readFile` through the LRU content cache (null = read failed, also cached). */
  348. private readFileCached(filePath: string): string | null {
  349. if (this.fileCache.has(filePath)) {
  350. return this.fileCache.get(filePath)!;
  351. }
  352. const fullPath = path.join(this.projectRoot, filePath);
  353. try {
  354. const content = fs.readFileSync(fullPath, 'utf-8');
  355. this.fileCache.set(filePath, content);
  356. return content;
  357. } catch (error) {
  358. logDebug('Failed to read file for resolution', { filePath, error: String(error) });
  359. this.fileCache.set(filePath, null);
  360. return null;
  361. }
  362. }
  363. /**
  364. * Create the resolution context
  365. */
  366. private createContext(): ResolutionContext {
  367. return {
  368. getNodesInFile: (filePath: string) => {
  369. if (!this.nodeCache.has(filePath)) {
  370. this.nodeCache.set(filePath, this.queries.getNodesByFile(filePath));
  371. }
  372. return this.nodeCache.get(filePath)!;
  373. },
  374. getNodesByName: (name: string) => {
  375. const cached = this.nameCache.get(name);
  376. if (cached !== undefined) return cached;
  377. const result = this.queries.getNodesByName(name);
  378. this.nameCache.set(name, result);
  379. return result;
  380. },
  381. getMethodMatches: (typeName: string, methodName: string, language: Language) => {
  382. const key = `${language} ${typeName}::${methodName}`;
  383. const cached = this.methodMatchCache.get(key);
  384. if (cached !== undefined) return cached;
  385. let candidates = this.nameCache.get(methodName);
  386. if (candidates === undefined) {
  387. candidates = this.queries.getNodesByName(methodName);
  388. this.nameCache.set(methodName, candidates);
  389. }
  390. const want = `${typeName}::${methodName}`;
  391. const matches: Node[] = [];
  392. for (const m of candidates) {
  393. if (m.kind !== 'method') continue;
  394. if (m.language !== language) continue;
  395. const qn = m.qualifiedName;
  396. if (qn === want || qn.endsWith(`::${want}`)) matches.push(m);
  397. }
  398. this.methodMatchCache.set(key, matches);
  399. return matches;
  400. },
  401. getNodesByQualifiedName: (qualifiedName: string) => {
  402. const cached = this.qualifiedNameCache.get(qualifiedName);
  403. if (cached !== undefined) return cached;
  404. const result = this.queries.getNodesByQualifiedNameExact(qualifiedName);
  405. this.qualifiedNameCache.set(qualifiedName, result);
  406. return result;
  407. },
  408. getNodesByKind: (kind: Node['kind']) => {
  409. const cached = this.nodesByKindCache.get(kind);
  410. if (cached !== undefined) return cached;
  411. const result = this.queries.getNodesByKind(kind);
  412. this.nodesByKindCache.set(kind, result);
  413. return result;
  414. },
  415. // Streamed, uncached — synthesizers scan-and-filter whole kinds, and
  416. // both the materialized array AND the per-kind cache retention are
  417. // O(nodes) memory (#1212). Per-ref resolvers keep the cached array
  418. // variant above.
  419. iterateNodesByKind: (kind: Node['kind']) => this.queries.iterateNodesByKind(kind),
  420. fileExists: (filePath: string) => {
  421. // Check pre-built known files set first (O(1))
  422. if (this.knownFiles) {
  423. const normalized = filePath.replace(/\\/g, '/');
  424. if (this.knownFiles.has(filePath) || this.knownFiles.has(normalized)) {
  425. return true;
  426. }
  427. }
  428. // Fall back to filesystem for files not yet indexed
  429. const fullPath = path.join(this.projectRoot, filePath);
  430. try {
  431. return fs.existsSync(fullPath);
  432. } catch (error) {
  433. logDebug('Error checking file existence', { filePath, error: String(error) });
  434. return false;
  435. }
  436. },
  437. readFile: (filePath: string) => this.readFileCached(filePath),
  438. getFileLines: (filePath: string) => {
  439. const cached = this.fileLinesCache.get(filePath);
  440. if (cached !== undefined) return cached;
  441. const source = this.readFileCached(filePath);
  442. const lines = source === null ? null : source.split(/\r?\n/);
  443. this.fileLinesCache.set(filePath, lines);
  444. return lines;
  445. },
  446. getProjectRoot: () => this.projectRoot,
  447. getAllFiles: () => {
  448. return this.queries.getAllFilePaths();
  449. },
  450. listDirectories: (relativePath: string) => {
  451. const target = relativePath === '.' || relativePath === ''
  452. ? this.projectRoot
  453. : path.join(this.projectRoot, relativePath);
  454. try {
  455. return fs
  456. .readdirSync(target, { withFileTypes: true })
  457. .filter((entry) => entry.isDirectory())
  458. .map((entry) => entry.name);
  459. } catch (error) {
  460. logDebug('Failed to list directory for resolution', {
  461. relativePath,
  462. error: String(error),
  463. });
  464. return [];
  465. }
  466. },
  467. getNodesByLowerName: (lowerName: string) => {
  468. const cached = this.lowerNameCache.get(lowerName);
  469. if (cached !== undefined) return cached;
  470. const result = this.queries.getNodesByLowerName(lowerName);
  471. this.lowerNameCache.set(lowerName, result);
  472. return result;
  473. },
  474. getNodeById: (id: string) => {
  475. return this.queries.getNodeById(id);
  476. },
  477. getSupertypes: (typeName: string, language) => {
  478. // Union the `implements`/`extends` targets of every same-named type node.
  479. // Matching by simple name (not id) reconciles a type declared in one node
  480. // (`KF::Builder`) with conformance declared in a separate extension node
  481. // (`KF.Builder: KFOptionSetter`) — both have name `Builder`.
  482. const typeNodes = this.context
  483. .getNodesByName(typeName)
  484. .filter((n) => SUPERTYPE_BEARING_KINDS.has(n.kind) && n.language === language);
  485. if (typeNodes.length === 0) return [];
  486. const supertypes = new Set<string>();
  487. for (const tn of typeNodes) {
  488. for (const edge of this.queries.getOutgoingEdges(tn.id, ['implements', 'extends'])) {
  489. const target = this.queries.getNodeById(edge.target);
  490. if (target?.name && target.name !== typeName) supertypes.add(target.name);
  491. }
  492. }
  493. return [...supertypes];
  494. },
  495. getImportMappings: (filePath: string, language) => {
  496. const cacheKey = filePath;
  497. const cached = this.importMappingCache.get(cacheKey);
  498. if (cached) return cached;
  499. const content = this.context.readFile(filePath);
  500. if (!content) {
  501. this.importMappingCache.set(cacheKey, []);
  502. return [];
  503. }
  504. const mappings = extractImportMappings(filePath, content, language);
  505. this.importMappingCache.set(cacheKey, mappings);
  506. return mappings;
  507. },
  508. getProjectAliases: () => {
  509. if (this.projectAliases === undefined) {
  510. this.projectAliases = loadProjectAliases(this.projectRoot);
  511. }
  512. return this.projectAliases;
  513. },
  514. getGoModule: () => {
  515. if (this.goModule === undefined) {
  516. this.goModule = loadGoModule(this.projectRoot);
  517. }
  518. return this.goModule;
  519. },
  520. getWorkspacePackages: () => {
  521. if (this.workspacePackages === undefined) {
  522. this.workspacePackages = loadWorkspacePackages(this.projectRoot);
  523. }
  524. return this.workspacePackages;
  525. },
  526. getReExports: (filePath: string, language) => {
  527. const cached = this.reExportCache.get(filePath);
  528. if (cached) return cached;
  529. const content = this.context.readFile(filePath);
  530. if (!content) {
  531. this.reExportCache.set(filePath, []);
  532. return [];
  533. }
  534. // Re-exports are a JS/TS-only construct, and what matters is the
  535. // BARREL file's own language — not the consuming reference's. A
  536. // `.svelte`/`.vue` consumer threads its own language down the
  537. // re-export chase, which would make extractReExports() bail on a
  538. // `.ts` index barrel and silently break the chain (#629). Re-key
  539. // the parse on the barrel's extension so the chase works no matter
  540. // what kind of file imports through it.
  541. const isJsFamily = /\.(?:d\.ts|[cm]?tsx?|[cm]?jsx?|ets)$/i.test(filePath);
  542. const reExports = extractReExports(content, isJsFamily ? 'typescript' : language);
  543. this.reExportCache.set(filePath, reExports);
  544. return reExports;
  545. },
  546. getCppIncludeDirs: () => {
  547. return loadCppIncludeDirs(this.projectRoot);
  548. },
  549. };
  550. }
  551. /**
  552. * Resolve all unresolved references
  553. */
  554. resolveAll(
  555. unresolvedRefs: UnresolvedReference[],
  556. onProgress?: (current: number, total: number) => void
  557. ): ResolutionResult {
  558. // Pre-load all nodes into memory for fast lookups
  559. this.warmCaches();
  560. const resolved: ResolvedRef[] = [];
  561. const unresolved: UnresolvedRef[] = [];
  562. const byMethod: Record<string, number> = {};
  563. // Convert to our internal format, using denormalized fields when available
  564. const refs: UnresolvedRef[] = unresolvedRefs.map((ref) => ({
  565. fromNodeId: ref.fromNodeId,
  566. referenceName: ref.referenceName,
  567. referenceKind: ref.referenceKind,
  568. line: ref.line,
  569. column: ref.column,
  570. filePath: ref.filePath || this.getFilePathFromNodeId(ref.fromNodeId),
  571. language: ref.language || this.getLanguageFromNodeId(ref.fromNodeId),
  572. rowId: ref.rowId,
  573. }));
  574. const total = refs.length;
  575. let lastReportedPercent = -1;
  576. for (let i = 0; i < refs.length; i++) {
  577. const ref = refs[i]!; // Array index is guaranteed to be in bounds
  578. const result = this.resolveOneTimed(ref);
  579. if (result) {
  580. resolved.push(result);
  581. byMethod[result.resolvedBy] = (byMethod[result.resolvedBy] || 0) + 1;
  582. } else {
  583. unresolved.push(ref);
  584. }
  585. // Report progress every 1% to avoid too many updates
  586. if (onProgress) {
  587. const currentPercent = Math.floor((i / total) * 100);
  588. if (currentPercent > lastReportedPercent) {
  589. lastReportedPercent = currentPercent;
  590. onProgress(i + 1, total);
  591. }
  592. }
  593. }
  594. // Final progress report
  595. if (onProgress && total > 0) {
  596. onProgress(total, total);
  597. }
  598. return {
  599. resolved,
  600. unresolved,
  601. stats: {
  602. total: refs.length,
  603. resolved: resolved.length,
  604. unresolved: unresolved.length,
  605. byMethod,
  606. },
  607. };
  608. }
  609. /**
  610. * Check if a reference name has any possible match in the codebase.
  611. * Uses the pre-built knownNames set to skip expensive resolution
  612. * for names that definitely don't exist as symbols.
  613. */
  614. private hasAnyPossibleMatch(name: string): boolean {
  615. if (!this.knownNames) return true; // no pre-filter available
  616. // Direct name match
  617. if (this.knownNames.has(name)) return true;
  618. // For qualified names like "obj.method" or "Class::method", check the parts
  619. const dotIdx = name.indexOf('.');
  620. if (dotIdx > 0) {
  621. const receiver = name.substring(0, dotIdx);
  622. const member = name.substring(dotIdx + 1);
  623. if (this.knownNames.has(receiver) || this.knownNames.has(member)) return true;
  624. // Also check capitalized receiver (instance-method resolution)
  625. const capitalized = receiver.charAt(0).toUpperCase() + receiver.slice(1);
  626. if (this.knownNames.has(capitalized)) return true;
  627. // JVM FQN: `com.example.foo.Bar` — the only useful segment is the
  628. // last one (`Bar`); the earlier check finds `example.foo.Bar` which
  629. // never matches a node name.
  630. const lastDot = name.lastIndexOf('.');
  631. if (lastDot > dotIdx) {
  632. const tail = name.substring(lastDot + 1);
  633. if (tail && this.knownNames.has(tail)) return true;
  634. }
  635. }
  636. const colonIdx = name.indexOf('::');
  637. if (colonIdx > 0) {
  638. const receiver = name.substring(0, colonIdx);
  639. const member = name.substring(colonIdx + 2);
  640. if (this.knownNames.has(receiver) || this.knownNames.has(member)) return true;
  641. // Multi-segment path `a::b::c` (a Rust/C++ module call like
  642. // `database::profiles::find`) — the only segment that names a symbol is
  643. // the last (`c`); `member` above is `b::c`, which never matches a node
  644. // name, so without this the pre-filter drops the ref before the Rust path
  645. // resolver ever sees it. Mirror the dotted-name leaf check above.
  646. const lastColon = name.lastIndexOf('::');
  647. if (lastColon > colonIdx) {
  648. const tail = name.substring(lastColon + 2);
  649. if (tail && this.knownNames.has(tail)) return true;
  650. }
  651. }
  652. // Lua/Luau method calls use a single `:` (`lg:log`); R uses `$` (`lg$log`).
  653. // Check the member (and receiver) around these separators too, so the ref
  654. // isn't dropped here before the method-call resolver ever sees it. The `:`
  655. // case is skipped when the name actually contains `::` (handled above).
  656. for (const sep of [':', '$']) {
  657. if (sep === ':' && name.includes('::')) continue;
  658. const sepIdx = name.indexOf(sep);
  659. if (sepIdx > 0) {
  660. const receiver = name.substring(0, sepIdx);
  661. const member = name.substring(sepIdx + 1);
  662. if (this.knownNames.has(member) || this.knownNames.has(receiver)) return true;
  663. const capitalized = receiver.charAt(0).toUpperCase() + receiver.slice(1);
  664. if (this.knownNames.has(capitalized)) return true;
  665. }
  666. }
  667. // For path-like references (e.g., "snippets/drawer-menu.liquid"), check the filename
  668. const slashIdx = name.lastIndexOf('/');
  669. if (slashIdx > 0) {
  670. const fileName = name.substring(slashIdx + 1);
  671. if (this.knownNames.has(fileName)) return true;
  672. }
  673. return false;
  674. }
  675. /**
  676. * Does `ref.referenceName` match an import declared in its containing
  677. * file? Used as a pre-filter escape so re-export chain resolution
  678. * still gets a chance when the name has no project-wide declaration.
  679. */
  680. private matchesAnyImport(ref: UnresolvedRef): boolean {
  681. const imports = this.context.getImportMappings(ref.filePath, ref.language);
  682. if (imports.length === 0) return false;
  683. for (const imp of imports) {
  684. if (
  685. imp.localName === ref.referenceName ||
  686. ref.referenceName.startsWith(imp.localName + '.')
  687. ) {
  688. return true;
  689. }
  690. }
  691. return false;
  692. }
  693. /**
  694. * Resolve a single reference
  695. */
  696. resolveOne(ref: UnresolvedRef): ResolvedRef | null {
  697. // Skip built-in/external references
  698. if (this.isBuiltInOrExternal(ref)) {
  699. return null;
  700. }
  701. // CFML component paths in inheritance (#1152): `extends="coldbox.system.web.
  702. // Controller"` names the supertype by its dot-separated path (or `extends=
  703. // "../base"` by relative file path) — the graph indexes the class under its
  704. // final segment only, so these die at the fast pre-filter below and never
  705. // resolved. Handled by a dedicated path-corroborated matcher, gated to
  706. // inheritance refs only (a dotted `calls` ref is a member-access chain, not
  707. // a component path). No fallthrough on miss: the full path string can only
  708. // ever mis-match downstream, and an unresolvable supertype usually lives in
  709. // an out-of-repo library (mxunit, testbox) — silent beats wrong.
  710. if (
  711. (ref.language === 'cfml' || ref.language === 'cfscript') &&
  712. (ref.referenceKind === 'extends' || ref.referenceKind === 'implements') &&
  713. (ref.referenceName.includes('.') || ref.referenceName.includes('/'))
  714. ) {
  715. return this.resolveCfmlComponentPath(ref);
  716. }
  717. // Fast pre-filter: skip if no symbol with this name exists anywhere
  718. // AND the name doesn't match a local import. The import escape is
  719. // necessary because re-export rename chains (`import { login }
  720. // from './barrel'` where the barrel has `export { signIn as login }
  721. // from './auth'`) intentionally call a name that has no
  722. // declaration anywhere — only the renamed upstream symbol does.
  723. // ArkTS chained-attribute refs carry a leading dot (`.titleStyle`) that
  724. // routes them to the decorator-gated matcher; the symbol itself is
  725. // indexed under the bare name, so the existence check strips the dot.
  726. // Nix static path imports (`import ./x.nix`) name a FILE, not a symbol —
  727. // they bypass the symbol-existence check and resolve via resolveViaImport.
  728. const existenceName =
  729. ref.language === 'arkts' && ref.referenceName.startsWith('.')
  730. ? ref.referenceName.slice(1)
  731. : ref.referenceName;
  732. const tPre = this.profileStages ? process.hrtime.bigint() : 0n;
  733. const preFilterPass =
  734. isNixPathImportRef(ref) ||
  735. this.hasAnyPossibleMatch(existenceName) ||
  736. this.matchesAnyImport(ref) ||
  737. this.frameworks.some((f) => f.claimsReference?.(ref.referenceName));
  738. if (this.profileStages) this.stageAdd('preFilter', ref, preFilterPass, tPre);
  739. if (!preFilterPass) {
  740. return null;
  741. }
  742. // Function-as-value refs (#756) get a dedicated, strictly-gated path:
  743. // import-based resolution first (an imported callback resolves through its
  744. // import, the most precise cross-file signal), then matchFunctionRef
  745. // (same-file first, unique-only cross-file, function/method targets only).
  746. // They never reach the framework or fuzzy strategies below.
  747. if (ref.referenceKind === 'function_ref') {
  748. // `this.<member>` values (TS/JS) resolve ONLY against the enclosing
  749. // class's own members — never a same-named symbol elsewhere.
  750. if (ref.referenceName.startsWith('this.')) {
  751. return this.gateLanguage(this.resolveThisMemberFnRef(ref), ref);
  752. }
  753. const viaImport = this.gateLanguage(resolveViaImport(ref, this.context), ref);
  754. if (viaImport) {
  755. const target = this.queries.getNodeById(viaImport.targetNodeId);
  756. if (target && (target.kind === 'function' || target.kind === 'method')) {
  757. return viaImport;
  758. }
  759. }
  760. return this.gateLanguage(matchFunctionRef(ref, this.context), ref);
  761. }
  762. // JVM FQN imports skip framework/name-matcher: `import com.example.Bar`
  763. // resolves directly through the qualifiedName index, which is unambiguous
  764. // even when several `Bar` classes exist in different packages.
  765. const tJvm = this.profileStages ? process.hrtime.bigint() : 0n;
  766. const jvmImport = resolveJvmImport(ref, this.context);
  767. if (this.profileStages) this.stageAdd('jvmImport', ref, !!jvmImport, tJvm);
  768. if (jvmImport) return jvmImport;
  769. // Razor/Blazor: a markup or `@code` type ref resolves through the file's
  770. // `@using` namespaces (incl. folder `_Imports.razor`). This precisely
  771. // disambiguates a simple name that exists in several namespaces — e.g.
  772. // `CatalogBrand` resolving to `BlazorShared.Models::CatalogBrand` (the DTO,
  773. // which the `.razor` `@using`s) rather than the same-named domain entity.
  774. if (ref.language === 'razor') {
  775. const razorResult = this.resolveRazorUsing(ref);
  776. if (razorResult) return razorResult;
  777. }
  778. const candidates: ResolvedRef[] = [];
  779. // Strategy 1: Try framework-specific resolution. Cross-language bridges
  780. // are deliberately preserved (Drupal `routing.yml` → PHP controller, RN
  781. // JS → native `calls`) — `gateFrameworkLanguage` only drops a type/import
  782. // edge between two KNOWN families (see its doc), never a `calls` bridge or
  783. // a config↔code edge.
  784. const tFw = this.profileStages ? process.hrtime.bigint() : 0n;
  785. let fwEarly: ResolvedRef | null = null;
  786. for (const framework of this.frameworks) {
  787. const result = this.gateFrameworkLanguage(framework.resolve(ref, this.context), ref);
  788. if (result) {
  789. if (result.confidence >= 0.9) {
  790. fwEarly = result; // High confidence, return immediately (below)
  791. break;
  792. }
  793. candidates.push(result);
  794. }
  795. }
  796. if (this.profileStages) this.stageAdd('frameworks', ref, fwEarly !== null, tFw);
  797. if (fwEarly) return fwEarly;
  798. // Strategy 2: Try import-based resolution
  799. const tImp = this.profileStages ? process.hrtime.bigint() : 0n;
  800. const importResult = this.gateLanguage(resolveViaImport(ref, this.context), ref);
  801. if (this.profileStages) this.stageAdd('viaImport', ref, !!importResult, tImp);
  802. if (importResult) {
  803. if (importResult.confidence >= 0.9) return importResult;
  804. candidates.push(importResult);
  805. }
  806. // PHP include/require paths resolve to files via import resolution only.
  807. // If that didn't find the file, do NOT fall back to the symbol
  808. // name-matcher — it would mis-connect e.g. "inc/db.php" to an unrelated
  809. // db.php elsewhere in the tree (a wrong edge is worse than none, #660).
  810. // Terraform refs are directory-scoped by language semantics — the
  811. // framework resolver IS the whole rulebook (`var.X` can never legally
  812. // bind outside its module directory), so the name-matcher's
  813. // qualified-name fallback would only ever add wrong cross-module edges.
  814. // Nix static path imports are file references for the same reason —
  815. // falling through would let "./x.nix" name-match an unrelated node.
  816. if (isPhpIncludePathRef(ref) || isCobolCopybookRef(ref) || isNixPathImportRef(ref) || ref.language === 'terraform') {
  817. return candidates.length > 0
  818. ? candidates.reduce((best, curr) =>
  819. curr.confidence > best.confidence ? curr : best
  820. )
  821. : null;
  822. }
  823. // Strategy 3: Try name matching
  824. const tName = this.profileStages ? process.hrtime.bigint() : 0n;
  825. let nameResult = this.gateLanguage(matchReference(ref, this.context), ref);
  826. if (this.profileStages) this.stageAdd('nameMatch', ref, !!nameResult, tName);
  827. // Nix has no ambient cross-file namespace — a callee binds lexically
  828. // (same file) or through explicit import/callPackage wiring (the import
  829. // path above). A cross-file name match is wrong by construction: every
  830. // module `inherit (lib) mkOption`s the same nixpkgs helpers, so the
  831. // matcher would link each `mkOption` call to whichever file's inherit
  832. // binding it happened to pick. Same-file matches only.
  833. if (nameResult) {
  834. const target = this.queries.getNodeById(nameResult.targetNodeId);
  835. if (ref.language === 'nix') {
  836. if (!target || target.filePath !== ref.filePath) {
  837. nameResult = null;
  838. }
  839. } else if (target && target.language === 'nix') {
  840. // The reverse direction is just as impossible: no other language can
  841. // symbolically call into a .nix binding (interop is eval/CLI, never a
  842. // linkable symbol) — without this, a Python script's `split()` lands
  843. // on some module's `split = ...` binding as a low-confidence match.
  844. nameResult = null;
  845. }
  846. }
  847. if (nameResult) {
  848. candidates.push(nameResult);
  849. }
  850. if (candidates.length === 0) {
  851. // Defer a chained static-factory/fluent call the first pass couldn't
  852. // resolve — its method may live on a supertype the receiver conforms to,
  853. // resolvable once implements/extends edges exist (the conformance pass).
  854. if (
  855. ref.referenceKind === 'calls' &&
  856. CHAIN_LANGUAGES.has(ref.language) &&
  857. CHAIN_SHAPE.test(ref.referenceName)
  858. ) {
  859. this.deferredChainRefs.push(ref);
  860. } else if (
  861. // PHP `$this->prop->method()` (encoded `this->prop.method`): its method
  862. // may live on the property's declared supertype, resolvable only once
  863. // implements/extends edges exist — defer to the same conformance pass.
  864. ref.referenceKind === 'calls' &&
  865. ref.language === 'php' &&
  866. PHP_PROP_SHAPE.test(ref.referenceName)
  867. ) {
  868. this.deferredChainRefs.push(ref);
  869. }
  870. return null;
  871. }
  872. // Return highest confidence candidate
  873. return candidates.reduce((best, curr) =>
  874. curr.confidence > best.confidence ? curr : best
  875. );
  876. }
  877. /**
  878. * Create edges from resolved references
  879. */
  880. createEdges(resolved: ResolvedRef[]): Edge[] {
  881. return resolved.map((ref) => {
  882. // `function_ref` (#756) is internal-only: it persists as a `references`
  883. // edge (the registration site depends on the callback), distinguishable
  884. // by metadata.resolvedBy === 'function-ref'. callers/impact already
  885. // traverse `references`, so registration sites surface with no
  886. // graph-layer changes.
  887. let kind: Edge['kind'] =
  888. ref.original.referenceKind === 'function_ref' ? 'references' : ref.original.referenceKind;
  889. // Promote "extends" to "implements" when a class/struct targets an interface
  890. if (kind === 'extends') {
  891. const targetNode = this.queries.getNodeById(ref.targetNodeId);
  892. if (targetNode && (targetNode.kind === 'interface' || targetNode.kind === 'protocol')) {
  893. const sourceNode = this.queries.getNodeById(ref.original.fromNodeId);
  894. if (sourceNode && sourceNode.kind !== 'interface' && sourceNode.kind !== 'protocol') {
  895. kind = 'implements';
  896. }
  897. }
  898. }
  899. // Promote "calls" to "instantiates" when the resolved target is a
  900. // class/struct. Languages without a `new` keyword (Python, Ruby)
  901. // express instantiation as `Foo()` — extraction can't tell that
  902. // apart from a function call without symbol info, but resolution
  903. // can: if `Foo` resolves to a class, the call IS an instantiation.
  904. if (kind === 'calls') {
  905. const targetNode = this.queries.getNodeById(ref.targetNodeId);
  906. if (targetNode && (targetNode.kind === 'class' || targetNode.kind === 'struct')) {
  907. kind = 'instantiates';
  908. }
  909. }
  910. return {
  911. source: ref.original.fromNodeId,
  912. target: ref.targetNodeId,
  913. kind,
  914. line: ref.original.line,
  915. column: ref.original.column,
  916. metadata: {
  917. confidence: ref.confidence,
  918. resolvedBy: ref.resolvedBy,
  919. // The ORIGINAL reference text (and kind, when edge-kind promotion
  920. // rewrote it — calls→instantiates, extends→implements,
  921. // function_ref→references). If this edge's target is later removed
  922. // by a re-index, the edge is resurrected as exactly this ref and
  923. // re-resolved (#1240 removal case) — a faithful resurrection, so
  924. // re-resolution can never bind anywhere a full re-index wouldn't.
  925. // Reconstruction from the target node's name instead would strip
  926. // receiver/qualifier context (`h.greet` → `greet`) and risk a
  927. // wrong rebind; edges without refName (pre-#1240, synthesized) are
  928. // deliberately NOT resurrected for the same reason.
  929. refName: ref.original.referenceName,
  930. ...(ref.original.referenceKind !== kind ? { refKind: ref.original.referenceKind } : {}),
  931. // Uniform marker for function-as-value edges (#756), regardless of
  932. // which strategy resolved them (import vs matchFunctionRef) — lets
  933. // tooling label "callback registration" and lets validation diff
  934. // exactly the edges this feature added.
  935. ...(ref.original.referenceKind === 'function_ref' ? { fnRef: true } : {}),
  936. },
  937. };
  938. });
  939. }
  940. /**
  941. * Split resolved refs into rows deletable by id and hand-built refs that
  942. * must fall back to the key-tuple delete. Rows loaded from the database
  943. * carry their row id and are deleted by exactly that id; the key tuple
  944. * omits line/col, so it also removes SIBLING rows — the same caller calling
  945. * the same callee at other lines — that a later batch hadn't attempted yet:
  946. * when a batch boundary split a caller's same-named call sites, the later
  947. * sites' edges were silently never created (#1269).
  948. */
  949. private static partitionResolvedCleanup(resolved: ResolvedRef[]): {
  950. rowIds: number[];
  951. legacyKeys: Array<{ fromNodeId: string; referenceName: string; referenceKind: string }>;
  952. } {
  953. const rowIds: number[] = [];
  954. const legacyKeys: Array<{ fromNodeId: string; referenceName: string; referenceKind: string }> = [];
  955. for (const r of resolved) {
  956. if (r.original.rowId != null) rowIds.push(r.original.rowId);
  957. else legacyKeys.push({
  958. fromNodeId: r.original.fromNodeId,
  959. referenceName: r.original.referenceName,
  960. referenceKind: r.original.referenceKind,
  961. });
  962. }
  963. return { rowIds, legacyKeys };
  964. }
  965. /**
  966. * Same row-id precision for parking unresolvable refs as status='failed'
  967. * (#1240): the key-tuple fallback would flip same-key sibling rows in later
  968. * batches to 'failed' before they were ever attempted, and resolution
  969. * outcome can differ per call site (receiver-type inference reads the
  970. * ref's line), so a sibling must not inherit this row's failure (#1269).
  971. */
  972. private static partitionFailedCleanup(unresolved: UnresolvedRef[]): {
  973. byRowId: Array<{ rowId: number; referenceName: string }>;
  974. legacyKeys: Array<{ fromNodeId: string; referenceName: string; referenceKind: string }>;
  975. } {
  976. const byRowId: Array<{ rowId: number; referenceName: string }> = [];
  977. const legacyKeys: Array<{ fromNodeId: string; referenceName: string; referenceKind: string }> = [];
  978. for (const r of unresolved) {
  979. if (r.rowId != null) byRowId.push({ rowId: r.rowId, referenceName: r.referenceName });
  980. else legacyKeys.push({
  981. fromNodeId: r.fromNodeId,
  982. referenceName: r.referenceName,
  983. referenceKind: r.referenceKind,
  984. });
  985. }
  986. return { byRowId, legacyKeys };
  987. }
  988. /**
  989. * Resolve and persist edges to database
  990. */
  991. resolveAndPersist(
  992. unresolvedRefs: UnresolvedReference[],
  993. onProgress?: (current: number, total: number) => void
  994. ): ResolutionResult {
  995. const result = this.resolveAll(unresolvedRefs, onProgress);
  996. // Create edges from resolved references
  997. const edges = this.createEdges(result.resolved);
  998. // Insert edges into database
  999. if (edges.length > 0) {
  1000. this.queries.insertEdges(edges);
  1001. }
  1002. // Clean up resolved refs from unresolved_refs table so metrics are accurate
  1003. if (result.resolved.length > 0) {
  1004. const { rowIds, legacyKeys } = ReferenceResolver.partitionResolvedCleanup(result.resolved);
  1005. this.queries.deleteReferencesByRowIds(rowIds);
  1006. this.queries.deleteSpecificResolvedReferences(legacyKeys);
  1007. }
  1008. // Park unresolvable refs as status='failed' — parity with
  1009. // resolveAndPersistBatched. Deleting them was wrong (#1240): a ref whose
  1010. // own file never changes is otherwise gone forever, so when a DIFFERENT
  1011. // file later gains the export/symbol that would satisfy it, no sync can
  1012. // recreate the edge — only a full re-index. Failed rows are excluded from
  1013. // the pending readers, which preserves the #1187 orphan sweep's
  1014. // invariant in status form: after a COMPLETED pass nothing it processed
  1015. // is still 'pending', so any pending row at rest belongs to an
  1016. // interrupted run and the sweep can key off the pending count.
  1017. if (result.unresolved.length > 0) {
  1018. const { byRowId, legacyKeys } = ReferenceResolver.partitionFailedCleanup(result.unresolved);
  1019. this.queries.markReferencesFailedByRowIds(byRowId);
  1020. this.queries.markReferencesFailed(legacyKeys);
  1021. }
  1022. return result;
  1023. }
  1024. /**
  1025. * Yielding counterpart of {@link resolveAndPersist} for a caller-supplied
  1026. * ref list — used by sync's failed-ref retry pass (#1240). Same persistence
  1027. * semantics: resolved refs become edges and their rows are deleted;
  1028. * still-unresolvable refs are (re-)marked failed (a no-op for rows already
  1029. * in that status). Yields per-ref because sync can run on the daemon's
  1030. * liveness-watchdog thread (#850/#1091) and a retry set is unbounded when
  1031. * a large edit lands many popular symbol names at once.
  1032. */
  1033. async resolveAndPersistListYielding(refs: UnresolvedReference[]): Promise<ResolutionResult> {
  1034. const maybeYield = createYielder();
  1035. const result = await this.resolveBatchYielding(refs, maybeYield);
  1036. const PERSIST_CHUNK = 1000;
  1037. const edges = this.createEdges(result.resolved);
  1038. for (let i = 0; i < edges.length; i += PERSIST_CHUNK) {
  1039. this.queries.insertEdges(edges.slice(i, i + PERSIST_CHUNK));
  1040. await maybeYield();
  1041. }
  1042. const resolvedCleanup = ReferenceResolver.partitionResolvedCleanup(result.resolved);
  1043. for (let i = 0; i < resolvedCleanup.rowIds.length; i += PERSIST_CHUNK) {
  1044. this.queries.deleteReferencesByRowIds(resolvedCleanup.rowIds.slice(i, i + PERSIST_CHUNK));
  1045. await maybeYield();
  1046. }
  1047. for (let i = 0; i < resolvedCleanup.legacyKeys.length; i += PERSIST_CHUNK) {
  1048. this.queries.deleteSpecificResolvedReferences(resolvedCleanup.legacyKeys.slice(i, i + PERSIST_CHUNK));
  1049. await maybeYield();
  1050. }
  1051. const failedCleanup = ReferenceResolver.partitionFailedCleanup(result.unresolved);
  1052. for (let i = 0; i < failedCleanup.byRowId.length; i += PERSIST_CHUNK) {
  1053. this.queries.markReferencesFailedByRowIds(failedCleanup.byRowId.slice(i, i + PERSIST_CHUNK));
  1054. await maybeYield();
  1055. }
  1056. for (let i = 0; i < failedCleanup.legacyKeys.length; i += PERSIST_CHUNK) {
  1057. this.queries.markReferencesFailed(failedCleanup.legacyKeys.slice(i, i + PERSIST_CHUNK));
  1058. await maybeYield();
  1059. }
  1060. return result;
  1061. }
  1062. /**
  1063. * Second resolution pass for chained static-factory / fluent calls whose
  1064. * chained method is defined on a SUPERTYPE the receiver's type conforms to —
  1065. * a protocol-extension / inherited / default-interface method (#750). The
  1066. * first pass can't resolve these because `implements`/`extends` edges aren't
  1067. * built yet; this runs AFTER edges are persisted, so `context.getSupertypes`
  1068. * (and the conformance fallback in resolveMethodOnType) can walk them.
  1069. *
  1070. * Operates only on the leftover unresolved refs that have the `inner().method`
  1071. * chain shape, for the dotted-chain languages — a small set — and is idempotent
  1072. * (re-resolving an already-resolved ref is a no-op since it's been deleted).
  1073. * Returns the number of newly-created edges.
  1074. */
  1075. async resolveChainedCallsViaConformance(): Promise<number> {
  1076. const deferred = this.deferredChainRefs;
  1077. this.deferredChainRefs = [];
  1078. if (deferred.length === 0) return 0;
  1079. // Read fresh edges (the main pass built the implements/extends edges after
  1080. // these refs were deferred). matchDottedCallChain now resolves a method on a
  1081. // supertype via context.getSupertypes -> resolveMethodOnType's conformance walk.
  1082. this.clearCaches();
  1083. // This post-pass runs synchronously on the indexer's main thread; yield
  1084. // periodically so the #850 liveness watchdog heartbeat can fire on a repo
  1085. // with many deferred chained calls (#1091).
  1086. const maybeYield = createYielder();
  1087. const resolved: ResolvedRef[] = [];
  1088. for (const ref of deferred) {
  1089. // PHP `this->prop.method` resolves via matchMethodCall (declared-type
  1090. // inference + resolveMethodOnType conformance walk); `::`-receiver
  1091. // languages (Rust) split on `::` (matchScopedCallChain); other
  1092. // dotted-receiver languages on `.` (matchDottedCallChain).
  1093. const chainMatch = (ref.language === 'php' && PHP_PROP_SHAPE.test(ref.referenceName))
  1094. ? matchMethodCall(ref, this.context)
  1095. : SCOPED_CHAIN_LANGUAGES.has(ref.language)
  1096. ? matchScopedCallChain(ref, this.context)
  1097. : matchDottedCallChain(ref, this.context);
  1098. const match = this.gateLanguage(chainMatch, ref);
  1099. if (match) resolved.push(match);
  1100. await maybeYield();
  1101. }
  1102. if (resolved.length === 0) return 0;
  1103. const edges = this.createEdges(resolved);
  1104. if (edges.length > 0) {
  1105. this.queries.insertEdges(edges);
  1106. this.clearCaches();
  1107. }
  1108. return edges.length;
  1109. }
  1110. /**
  1111. * Resolve one batch with a yield checkpoint between EVERY ref so the #850
  1112. * liveness heartbeat can fire on a slow/dense batch (#1091). The checkpoint
  1113. * granularity is per-ref — not per-N-refs — because per-ref cost is unbounded
  1114. * in the worst case (a collision-heavy method name whose candidate set misses
  1115. * the LRU re-fetches tens of thousands of rows): any fixed N multiplies that
  1116. * worst case into the watchdog window, which is how v1.2.0 still got killed
  1117. * at "Resolving refs" on large Java monorepos (#1122). `maybeYield()` is a
  1118. * ~ns time check when under budget, so per-ref checkpoints cost nothing.
  1119. * Behaviourally identical to `resolveAll(batch)`: `warmCaches()` is
  1120. * idempotent (guarded) and `resolveOne` is independent per ref, so yielding
  1121. * between refs changes only timing, never which edges get created.
  1122. */
  1123. private async resolveBatchYielding(
  1124. batch: UnresolvedReference[],
  1125. maybeYield: MaybeYield
  1126. ): Promise<ResolutionResult> {
  1127. this.warmCaches();
  1128. const resolved: ResolvedRef[] = [];
  1129. const unresolved: UnresolvedRef[] = [];
  1130. const byMethod: Record<string, number> = {};
  1131. for (const raw of batch) {
  1132. const ref: UnresolvedRef = {
  1133. fromNodeId: raw.fromNodeId,
  1134. referenceName: raw.referenceName,
  1135. referenceKind: raw.referenceKind,
  1136. line: raw.line,
  1137. column: raw.column,
  1138. filePath: raw.filePath || this.getFilePathFromNodeId(raw.fromNodeId),
  1139. language: raw.language || this.getLanguageFromNodeId(raw.fromNodeId),
  1140. rowId: raw.rowId,
  1141. };
  1142. const result = this.resolveOneTimed(ref);
  1143. if (result) {
  1144. resolved.push(result);
  1145. byMethod[result.resolvedBy] = (byMethod[result.resolvedBy] || 0) + 1;
  1146. } else {
  1147. unresolved.push(ref);
  1148. }
  1149. // Fast-path the per-ref yield check: awaiting the async no-op costs a
  1150. // microtask hop per ref, which dominates at ~10⁵ refs (see MaybeYield).
  1151. const y = maybeYield();
  1152. if (y) await y;
  1153. }
  1154. return {
  1155. resolved,
  1156. unresolved,
  1157. stats: {
  1158. total: batch.length,
  1159. resolved: resolved.length,
  1160. unresolved: unresolved.length,
  1161. byMethod,
  1162. },
  1163. };
  1164. }
  1165. /**
  1166. * Resolve a list of refs and return everything the ADMISSION side needs to
  1167. * persist the outcome: resolutions, failures, the deferred post-pass refs
  1168. * this run produced (drained, so the caller owns routing them), and stats.
  1169. * This is the resolver-worker entry point — it runs the exact per-ref loop
  1170. * of resolveBatchYielding, minus the main-thread yields (worker threads have
  1171. * no watchdog heartbeat to starve). Results are in input order.
  1172. */
  1173. /**
  1174. * CODEGRAPH_RESOLVE_PROFILE=1: per-outcome wall-clock histogram of
  1175. * resolveOne, keyed by the winning strategy (`resolvedBy`) or
  1176. * `fail:<referenceKind>` — the §7a.2 "profile the per-ref path" probe. The
  1177. * kernel-scale batch loop is ~430s and CORE-INVARIANT (835.9s pooled-4-on-8
  1178. * ≈ 812.5s sequential-on-2 for the whole superphase), so the next lever is
  1179. * which CLASS of ref the time belongs to, not more parallelism. Off by
  1180. * default: the hrtime pair costs ~100ns/ref only when the env is set.
  1181. */
  1182. private resolveProfile: Map<string, { n: number; ns: bigint }> | null =
  1183. process.env.CODEGRAPH_RESOLVE_PROFILE ? new Map() : null;
  1184. /**
  1185. * CODEGRAPH_RESOLVE_PROFILE=2 additionally attributes time to the
  1186. * STRATEGIES inside resolveOne (`stage:<name>|<refKind>|hit/miss` rows in
  1187. * the same histogram) — i.e. WHICH machinery a failing class of refs pays
  1188. * for, not just that it fails. =1 keeps the per-outcome rows only.
  1189. */
  1190. private profileStages: boolean = process.env.CODEGRAPH_RESOLVE_PROFILE === '2';
  1191. private stageAdd(stage: string, ref: UnresolvedRef, hit: boolean, t0: bigint): void {
  1192. if (!this.resolveProfile) return;
  1193. const dt = process.hrtime.bigint() - t0;
  1194. const key = `stage:${stage}|${ref.referenceKind}|${hit ? 'hit' : 'miss'}`;
  1195. const slot = this.resolveProfile.get(key);
  1196. if (slot) {
  1197. slot.n++;
  1198. slot.ns += dt;
  1199. } else {
  1200. this.resolveProfile.set(key, { n: 1, ns: dt });
  1201. }
  1202. }
  1203. private resolveOneTimed(ref: UnresolvedRef): ResolvedRef | null {
  1204. if (!this.resolveProfile) return this.resolveOne(ref);
  1205. const t0 = process.hrtime.bigint();
  1206. const result = this.resolveOne(ref);
  1207. const dt = process.hrtime.bigint() - t0;
  1208. const key = result ? result.resolvedBy : `fail:${ref.referenceKind}`;
  1209. const slot = this.resolveProfile.get(key);
  1210. if (slot) {
  1211. slot.n++;
  1212. slot.ns += dt;
  1213. } else {
  1214. this.resolveProfile.set(key, { n: 1, ns: dt });
  1215. }
  1216. return result;
  1217. }
  1218. /** Dump the CODEGRAPH_RESOLVE_PROFILE histogram to stderr (no-op when off). */
  1219. dumpResolveProfile(label: string): void {
  1220. if (!this.resolveProfile || this.resolveProfile.size === 0) return;
  1221. const rows = [...this.resolveProfile.entries()]
  1222. .map(([k, v]) => ({ k, n: v.n, ms: Number(v.ns / 1_000_000n) }))
  1223. .sort((a, b) => b.ms - a.ms);
  1224. for (const r of rows) {
  1225. console.error(
  1226. `[resolve-profile] ${label} ${r.k}: n=${r.n} total=${(r.ms / 1000).toFixed(1)}s avg=${((r.ms * 1000) / Math.max(1, r.n)).toFixed(0)}µs`
  1227. );
  1228. }
  1229. // =2 only: this thread's matchReference sub-stage table rides along.
  1230. dumpNameMatcherProfile(label);
  1231. }
  1232. resolveListForAdmission(refs: UnresolvedReference[]): {
  1233. resolved: ResolvedRef[];
  1234. unresolved: UnresolvedRef[];
  1235. deferredChain: UnresolvedRef[];
  1236. deferredThisMember: UnresolvedRef[];
  1237. byMethod: Record<string, number>;
  1238. } {
  1239. this.warmCaches();
  1240. const resolved: ResolvedRef[] = [];
  1241. const unresolved: UnresolvedRef[] = [];
  1242. const byMethod: Record<string, number> = {};
  1243. for (const raw of refs) {
  1244. const ref: UnresolvedRef = {
  1245. fromNodeId: raw.fromNodeId,
  1246. referenceName: raw.referenceName,
  1247. referenceKind: raw.referenceKind,
  1248. line: raw.line,
  1249. column: raw.column,
  1250. filePath: raw.filePath || this.getFilePathFromNodeId(raw.fromNodeId),
  1251. language: raw.language || this.getLanguageFromNodeId(raw.fromNodeId),
  1252. rowId: raw.rowId,
  1253. };
  1254. const result = this.resolveOneTimed(ref);
  1255. if (result) {
  1256. resolved.push(result);
  1257. byMethod[result.resolvedBy] = (byMethod[result.resolvedBy] || 0) + 1;
  1258. } else {
  1259. unresolved.push(ref);
  1260. }
  1261. }
  1262. return {
  1263. resolved,
  1264. unresolved,
  1265. deferredChain: this.deferredChainRefs.splice(0),
  1266. deferredThisMember: this.deferredThisMemberRefs.splice(0),
  1267. byMethod,
  1268. };
  1269. }
  1270. /**
  1271. * The resolver's live ResolutionContext — resolver-pool workers use it to
  1272. * run synthesis passes against their own read-only connection.
  1273. */
  1274. getResolutionContext(): ResolutionContext {
  1275. return this.context;
  1276. }
  1277. /**
  1278. * Re-queue deferred post-pass refs produced by resolver workers, preserving
  1279. * their admission order so resolveChainedCallsViaConformance /
  1280. * resolveDeferredThisMemberRefs process them exactly as the sequential path
  1281. * would have.
  1282. */
  1283. appendDeferredFromWorkers(deferredChain: UnresolvedRef[], deferredThisMember: UnresolvedRef[]): void {
  1284. this.deferredChainRefs.push(...deferredChain);
  1285. this.deferredThisMemberRefs.push(...deferredThisMember);
  1286. }
  1287. /**
  1288. * Resolve and persist in batches to keep memory bounded.
  1289. * Processes unresolved references in chunks, persisting edges and cleaning
  1290. * up resolved refs after each batch to avoid accumulating large arrays.
  1291. */
  1292. async resolveAndPersistBatched(
  1293. onProgress?: (current: number, total: number) => void,
  1294. batchSize: number = 5000,
  1295. onSynthesisProgress?: (done: number, total: number) => void,
  1296. // When provided, big batches fan out across a read-only resolver-worker
  1297. // pool with results admitted in canonical order (see resolver-pool.ts).
  1298. // Sequential fallback on any pool failure. CODEGRAPH_NO_PARALLEL_RESOLVE=1
  1299. // disables entirely. bulkEdgeLoad hooks (when provided) bracket the batch
  1300. // loop with drop/recreate of the non-unique edge indexes on big runs —
  1301. // see DatabaseConnection.beginBulkEdgeLoad. backpressure (when provided)
  1302. // is the WAL valve's writer-side backstop (WalCheckpointValve.backpressure):
  1303. // called at pool-idle boundaries so a full backfill can actually complete —
  1304. // the valve's timer-driven passive passes stay perpetually partial against
  1305. // the pool's continuous reads, which is how a kernel-scale resolution grew
  1306. // a 22GB WAL on a 4.6GB DB (migration plan §7a.1).
  1307. parallel?: {
  1308. dbPath: string;
  1309. bulkEdgeLoad?: { begin: () => void; end: () => void | Promise<void> };
  1310. /** unresolved_refs index window for the batched loop — the loop only
  1311. * reads the status index + PK; dropping the sync-path ref indexes cuts
  1312. * each per-batch DELETE's B-tree work (DatabaseConnection.beginBulkRefLoad). */
  1313. refIndexLoad?: { begin: () => void; end: () => void | Promise<void> };
  1314. backpressure?: () => Promise<void> | null;
  1315. }
  1316. ): Promise<ResolutionResult> {
  1317. // Resolution runs on the indexer's MAIN thread, and the #850 liveness
  1318. // watchdog SIGKILLs a process whose event loop stalls past its window (60s
  1319. // by default). A single dense batch's resolveAll — or the synthesis pass
  1320. // below — can exceed that on a large repo, killing a VALID in-progress index
  1321. // (#1091). A shared yielder lets both give the watchdog heartbeat a regular
  1322. // window to fire; see ./cooperative-yield.
  1323. const maybeYield = createYielder();
  1324. if (process.env.CODEGRAPH_SYNTH_TIMINGS) {
  1325. console.error(`[pool-timing] backpressure hook: ${parallel?.backpressure ? 'present' : 'absent'}`);
  1326. }
  1327. // CODEGRAPH_RESOLVE_PROFILE loop-stage attribution: the §7a.2 kernel-scale
  1328. // histogram showed resolveOne owns only ~93s of the ~436s batch loop —
  1329. // these counters name where the other ~340s goes (reads, edge build+insert,
  1330. // deletes/marks, the per-batch count guard).
  1331. const loopProf: Record<string, number> | null = process.env.CODEGRAPH_RESOLVE_PROFILE
  1332. ? { read: 0, settle: 0, backpressure: 0, recycle: 0, createEdges: 0, insertEdges: 0, deletes: 0, marks: 0, countGuard: 0 }
  1333. : null;
  1334. const lp = (k: string, t0: number): void => { if (loopProf) loopProf[k] = (loopProf[k] ?? 0) + (Date.now() - t0); };
  1335. let tLp = 0;
  1336. await this.warmCachesYielding(maybeYield);
  1337. const total = this.queries.getUnresolvedReferencesCount();
  1338. let processed = 0;
  1339. const aggregateStats = {
  1340. total: 0,
  1341. resolved: 0,
  1342. unresolved: 0,
  1343. byMethod: {} as Record<string, number>,
  1344. };
  1345. // Parallel pool, started immediately but never awaited up front: early
  1346. // batches run sequentially while the workers boot (module load + readonly
  1347. // DB open + framework detect + cache warm ≈ hundreds of ms), and the loop
  1348. // switches to fan-out the moment the pool reports ready — so pool boot
  1349. // costs zero wall-clock. Any failure downgrades to sequential permanently.
  1350. let pool: ResolverPool | null = null;
  1351. let poolReady = false;
  1352. const tPoolStart = Date.now();
  1353. if (parallel && total >= minRefsForPool()) {
  1354. pool = ResolverPool.tryCreate(parallel.dbPath, this.projectRoot);
  1355. pool?.ready().then(
  1356. () => {
  1357. poolReady = true;
  1358. if (process.env.CODEGRAPH_SYNTH_TIMINGS) console.error(`[pool-timing] pool ready after ${Date.now() - tPoolStart}ms`);
  1359. },
  1360. () => { void pool?.destroy().catch(() => undefined); pool = null; }
  1361. );
  1362. }
  1363. // Process in PIPELINED batches (double-buffer). The enumeration is the
  1364. // head of the pending set in rowid order; every ref a persisted batch
  1365. // processed leaves the pending set (resolved rows are deleted,
  1366. // unresolvable ones flip to status='failed'), shifting the remaining
  1367. // pending rows forward.
  1368. let prevRemaining = Number.POSITIVE_INFINITY;
  1369. // Cadence for the worker connection recycling below — ~8 batches
  1370. // ≈ 40k refs between recycles keeps the WAL shallow at kernel scale
  1371. // while a small sync never recycles at all. (25 recovered only half
  1372. // the write tax — the WAL re-deepened between recycles; reopens are
  1373. // sub-millisecond so the shorter cadence is ~free.)
  1374. const RECYCLE_EVERY_BATCHES = 8;
  1375. let batchesSinceRecycle = 0;
  1376. // Fan-out result of ResolverPool.resolveBatch, settled (never rejecting)
  1377. // so a fan-out begun before the previous batch's persist can't produce an
  1378. // unhandled rejection while it waits to be awaited.
  1379. type PoolSettled =
  1380. | { ok: true; out: Awaited<ReturnType<ResolverPool['resolveBatch']>> }
  1381. | { ok: false; err: unknown };
  1382. type InFlight = { mode: 'pool'; settled: Promise<PoolSettled> } | { mode: 'seq' };
  1383. // Begin one batch: fan out to the pool when it's ready and the batch is
  1384. // big enough — workers then resolve batch k+1 WHILE the main thread
  1385. // persists batch k (persist measured at ~58% of resolution wall on a
  1386. // 255k-ref repo, all of it previously spent with the pool idle).
  1387. // Sequential batches stay lazy: they run on the main thread at settle
  1388. // time, where an early start would only contend with the persist.
  1389. const beginBatch = (batch: UnresolvedReference[]): InFlight => {
  1390. if (pool && poolReady && ResolverPool.worthParallel(batch.length)) {
  1391. return {
  1392. mode: 'pool',
  1393. settled: pool.resolveBatch(batch).then(
  1394. (out) => ({ ok: true as const, out }),
  1395. (err: unknown) => ({ ok: false as const, err })
  1396. ),
  1397. };
  1398. }
  1399. return { mode: 'seq' };
  1400. };
  1401. // Settle an in-flight batch to a ResolutionResult. Deferred post-pass refs
  1402. // are appended HERE, in loop order — never inside the fan-out promise — so
  1403. // admission order stays exactly the sequential order even while a later
  1404. // batch resolves concurrently. A pool failure downgrades to sequential
  1405. // permanently and re-resolves this batch on the main thread.
  1406. const settleBatch = async (
  1407. inFlight: InFlight,
  1408. batch: UnresolvedReference[]
  1409. ): Promise<ResolutionResult> => {
  1410. if (inFlight.mode === 'pool') {
  1411. const settled = await inFlight.settled;
  1412. if (settled.ok) {
  1413. this.appendDeferredFromWorkers(settled.out.deferredChain, settled.out.deferredThisMember);
  1414. return {
  1415. resolved: settled.out.resolved,
  1416. unresolved: settled.out.unresolved,
  1417. stats: {
  1418. total: batch.length,
  1419. resolved: settled.out.resolved.length,
  1420. unresolved: settled.out.unresolved.length,
  1421. byMethod: settled.out.byMethod,
  1422. },
  1423. };
  1424. }
  1425. logDebug('Parallel resolution failed; falling back to sequential', {
  1426. error: settled.err instanceof Error ? settled.err.message : String(settled.err),
  1427. });
  1428. if (pool) await pool.destroy().catch(() => undefined);
  1429. pool = null;
  1430. }
  1431. return this.resolveBatchYielding(batch, maybeYield);
  1432. };
  1433. // Bulk edge load: on big runs, drop the non-unique edge indexes for the
  1434. // duration of the batch loop (the identity index stays — OR IGNORE dedup
  1435. // and the source-keyed supertype-walk reads both live on it). Recreated in
  1436. // the inner finally BEFORE synthesis, whose passes read kind-keyed.
  1437. // Measured on a 224k-edge resolution set: insert 2.8s → 1.1s + 0.3s
  1438. // recreate. Same ref-count gate as the pool so small syncs never pay the
  1439. // recreate cost.
  1440. let bulkEdgesActive = false;
  1441. if (parallel?.bulkEdgeLoad && total >= minRefsForPool()) {
  1442. try {
  1443. parallel.bulkEdgeLoad.begin();
  1444. bulkEdgesActive = true;
  1445. } catch { /* keep the indexes; inserts just pay the per-row maintenance */ }
  1446. }
  1447. // Same gate for the ref-index window: the loop's deletes stop maintaining
  1448. // the five sync-path unresolved_refs indexes, and the end-of-loop rebuild
  1449. // is near-free (only failed refs survive the loop).
  1450. let bulkRefsActive = false;
  1451. if (parallel?.refIndexLoad && total >= minRefsForPool()) {
  1452. try {
  1453. parallel.refIndexLoad.begin();
  1454. bulkRefsActive = true;
  1455. } catch { /* keep the indexes; deletes just pay the per-row maintenance */ }
  1456. }
  1457. try {
  1458. try {
  1459. tLp = Date.now();
  1460. let batch = this.queries.getUnresolvedReferencesBatchAfter(0, batchSize);
  1461. lp('read', tLp);
  1462. let inFlight: InFlight | null = batch.length > 0 ? beginBatch(batch) : null;
  1463. while (batch.length > 0 && inFlight) {
  1464. // Prefetch the NEXT batch before this one persists: this batch's rows
  1465. // are still pending (nothing has mutated the table since they were
  1466. // read), so seeking past this batch's last row id in the same rowid
  1467. // enumeration yields the following batch (keyset — OFFSET re-walked the
  1468. // accumulated failed prefix every read, 54.6s at kernel scale, §7a.2).
  1469. tLp = Date.now();
  1470. const nextBatch = this.queries.getUnresolvedReferencesBatchAfter(batch[batch.length - 1]!.rowId!, batchSize);
  1471. lp('read', tLp);
  1472. const tBatch = Date.now();
  1473. const result = await settleBatch(inFlight, batch);
  1474. if (process.env.CODEGRAPH_SYNTH_TIMINGS) console.error(`[pool-timing] batch ${inFlight.mode}: ${batch.length} refs in ${Date.now() - tBatch}ms`);
  1475. lp('settle', tBatch);
  1476. // WAL-valve backstop at the ONE pool-idle boundary of the double-buffer
  1477. // (this batch settled, the next not yet fanned out): past the hard cap
  1478. // the writer parks for a full backfill here, where the pool's readers
  1479. // are all between statements — so the backfill completes, readers
  1480. // re-enter at SQLite's backfilled mark, and the next persist commit
  1481. // WRAPS the WAL instead of growing it. No-op (one fstat) under the cap.
  1482. tLp = Date.now();
  1483. const bp = parallel?.backpressure?.();
  1484. if (bp) await bp;
  1485. lp('backpressure', tLp);
  1486. // Recycle the workers' read connections periodically at this same
  1487. // worker-idle boundary (batch k settled, batch k+1 not yet fanned
  1488. // out): a long-lived reader pins WAL checkpoint progress, and the
  1489. // deep WAL that accumulates behind it taxes the writer's OWN page
  1490. // operations — the §7a.6 writes-under-readers finding (deletes
  1491. // 42.6s → 118.8s from 0 to 4 attached readers; an aggressive valve
  1492. // recovered the writes but paid +129s in full-park folds). Releasing
  1493. // the read marks every ~25 batches lets the existing checkpoints
  1494. // advance instead, at ~milliseconds of reopen cost. A failed recycle
  1495. // downgrades to sequential permanently, same as a failed fan-out.
  1496. if (pool && poolReady && ++batchesSinceRecycle >= RECYCLE_EVERY_BATCHES) {
  1497. batchesSinceRecycle = 0;
  1498. tLp = Date.now();
  1499. try {
  1500. await pool.recycleWorkers();
  1501. } catch (err) {
  1502. logDebug('Worker connection recycle failed; falling back to sequential', {
  1503. error: err instanceof Error ? err.message : String(err),
  1504. });
  1505. await pool.destroy().catch(() => undefined);
  1506. pool = null;
  1507. }
  1508. lp('recycle', tLp);
  1509. }
  1510. // Persist in bounded sub-transactions with yields between: a whole
  1511. // batch's edge insert / keyed deletes are otherwise one solid
  1512. // synchronous span each on a multi-GB index, sitting BETWEEN the
  1513. // per-ref yields — the last unyielded stretch of the resolution loop.
  1514. // Crash semantics are unchanged (already several transactions): edges
  1515. // land before their refs are deleted, so a kill mid-way re-resolves
  1516. // the remainder idempotently on the next run/sweep (#1187).
  1517. const PERSIST_CHUNK = 1000;
  1518. const tPersist = Date.now();
  1519. // Persist edges BEFORE fanning out the next batch: later batches read
  1520. // this batch's edges — resolveMethodOnType walks supertype chains over
  1521. // `extends`/`implements` edges that earlier batches resolved, so a
  1522. // receiver typed as a subclass only reaches a method declared on its
  1523. // base class if those edges are visible. (Validated on dubbo: fanning
  1524. // out first downgraded exactly those supertype-method resolutions from
  1525. // the 0.9 typed-receiver path to the 0.65 word-overlap fallback.)
  1526. tLp = Date.now();
  1527. const edges = this.createEdges(result.resolved);
  1528. lp('createEdges', tLp);
  1529. tLp = Date.now();
  1530. for (let i = 0; i < edges.length; i += PERSIST_CHUNK) {
  1531. this.queries.insertEdges(edges.slice(i, i + PERSIST_CHUNK));
  1532. await maybeYield();
  1533. }
  1534. lp('insertEdges', tLp);
  1535. // NOW fan the next batch out — workers see exactly the edge state the
  1536. // sequential baseline would (every batch ≤ this one committed), while
  1537. // the main thread spends the REST of the persist (ref deletes + failed
  1538. // parking below) overlapped with their resolution — the double-buffer.
  1539. const nextInFlight = nextBatch.length > 0 ? beginBatch(nextBatch) : null;
  1540. // Clean up resolved refs so they don't appear in the next batch —
  1541. // by row id, so a same-key sibling ref in a LATER batch (same caller
  1542. // calling the same callee at another line) is left pending for its own
  1543. // attempt instead of being swept out with this batch's rows (#1269).
  1544. tLp = Date.now();
  1545. let removedThisBatch = 0;
  1546. const resolvedCleanup = ReferenceResolver.partitionResolvedCleanup(result.resolved);
  1547. for (let i = 0; i < resolvedCleanup.rowIds.length; i += PERSIST_CHUNK) {
  1548. removedThisBatch += this.queries.deleteReferencesByRowIds(resolvedCleanup.rowIds.slice(i, i + PERSIST_CHUNK));
  1549. await maybeYield();
  1550. }
  1551. for (let i = 0; i < resolvedCleanup.legacyKeys.length; i += PERSIST_CHUNK) {
  1552. removedThisBatch += this.queries.deleteSpecificResolvedReferences(resolvedCleanup.legacyKeys.slice(i, i + PERSIST_CHUNK));
  1553. await maybeYield();
  1554. }
  1555. lp('deletes', tLp);
  1556. // Park unresolvable refs from this batch as status='failed' so they
  1557. // leave the pending set (the batch reader and non-progress guard below
  1558. // only see pending rows) but stay retryable when a later sync adds a
  1559. // symbol that could satisfy them (#1240).
  1560. tLp = Date.now();
  1561. const failedCleanup = ReferenceResolver.partitionFailedCleanup(result.unresolved);
  1562. for (let i = 0; i < failedCleanup.byRowId.length; i += PERSIST_CHUNK) {
  1563. removedThisBatch += this.queries.markReferencesFailedByRowIds(failedCleanup.byRowId.slice(i, i + PERSIST_CHUNK));
  1564. await maybeYield();
  1565. }
  1566. for (let i = 0; i < failedCleanup.legacyKeys.length; i += PERSIST_CHUNK) {
  1567. removedThisBatch += this.queries.markReferencesFailed(failedCleanup.legacyKeys.slice(i, i + PERSIST_CHUNK));
  1568. await maybeYield();
  1569. }
  1570. lp('marks', tLp);
  1571. if (process.env.CODEGRAPH_SYNTH_TIMINGS) console.error(`[pool-timing] batch persist: ${Date.now() - tPersist}ms`);
  1572. // Aggregate stats
  1573. aggregateStats.total += result.stats.total;
  1574. aggregateStats.resolved += result.stats.resolved;
  1575. aggregateStats.unresolved += result.stats.unresolved;
  1576. for (const [method, count] of Object.entries(result.stats.byMethod)) {
  1577. aggregateStats.byMethod[method] = (aggregateStats.byMethod[method] || 0) + count;
  1578. }
  1579. processed += batch.length;
  1580. onProgress?.(processed, total);
  1581. // Yield so progress UI can render between batches
  1582. await new Promise(resolve => setImmediate(resolve));
  1583. // NOTE: there used to be an extra early break here when a batch resolved
  1584. // nothing (`result.unresolved.length === batch.length`). That was wrong:
  1585. // an all-unresolvable batch still DELETES its rows (progress), yet the
  1586. // break abandoned every batch after it in the same run — on a repo whose
  1587. // first 5000 refs are all external/stdlib calls, resolution stopped at
  1588. // batch one and left the rest of the table as permanent orphans (#1187).
  1589. // The count-based guard below catches the true no-progress case.
  1590. // Non-progress guard (defense-in-depth). Each iteration enumerates from
  1591. // the head of the pending set, so the PENDING population MUST shrink
  1592. // every iteration — resolved refs are deleted and unresolvable ones are
  1593. // marked failed above, and both leave the pending set the batch reader
  1594. // sees. If it didn't shrink, a resolver returned a match whose
  1595. // `original.referenceName` differs from the stored row, so the keyed
  1596. // delete/update no-ops, and we'd re-read + re-resolve + re-insert the
  1597. // same rows forever (the runaway that grew a 99-file repo to 5M edges /
  1598. // 1.4 GB before the Go-fallback fix). Stop rather than grow the graph
  1599. // without bound. (An in-flight prefetched batch is abandoned unsettled —
  1600. // fan-out has no side effects until settleBatch appends its results.)
  1601. // Non-progress signal, now O(1): `changes` summed across this batch's
  1602. // deletes + failed-parks is the DIRECT evidence the guard's old count
  1603. // diff inferred — a resolver returning a mismatched name makes the keyed
  1604. // cleanup no-op, which shows up here as zero removals. The per-batch
  1605. // COUNT(*) it replaces walked every remaining pending row — O(N²/batch)
  1606. // over a run, 93.9s of the kernel-scale batch loop (§7a.2). A REAL count
  1607. // runs only on the suspicious path (claimed-work batch removed nothing —
  1608. // e.g. every row was a sibling a legacy-key sweep already consumed),
  1609. // where it arbitrates stop-vs-continue exactly as before.
  1610. if (removedThisBatch <= 0 && batch.length > 0) {
  1611. tLp = Date.now();
  1612. const remaining = this.queries.getUnresolvedReferencesCount();
  1613. lp('countGuard', tLp);
  1614. if (remaining >= prevRemaining) break;
  1615. prevRemaining = remaining;
  1616. }
  1617. // Advance the pipeline: the prefetched batch (already fanned out when
  1618. // the pool is on) becomes the current one.
  1619. batch = nextBatch;
  1620. inFlight = nextInFlight;
  1621. }
  1622. } finally {
  1623. // Recreate the edge indexes BEFORE synthesis (kind-keyed reads) and on
  1624. // any error path. A crash before this line is healed by the next
  1625. // DatabaseConnection open (schema.sql re-applies IF NOT EXISTS).
  1626. if (bulkRefsActive) {
  1627. const tRef = Date.now();
  1628. await parallel!.refIndexLoad!.end();
  1629. if (process.env.CODEGRAPH_SYNTH_TIMINGS) console.error(`[phase-timing] ref-index-recreate: ${Date.now() - tRef}ms`);
  1630. }
  1631. if (bulkEdgesActive) {
  1632. const tIdx = Date.now();
  1633. await parallel!.bulkEdgeLoad!.end();
  1634. if (process.env.CODEGRAPH_SYNTH_TIMINGS) console.error(`[phase-timing] edge-index-recreate: ${Date.now() - tIdx}ms`);
  1635. // The recreate just wrote every non-unique edge index into the WAL
  1636. // (multi-GB at kernel scale) with the pool idle — fold before the
  1637. // synthesis passes pin readers against it for minutes.
  1638. const bp = parallel?.backpressure?.();
  1639. if (bp) await bp;
  1640. }
  1641. }
  1642. // Dynamic-edge synthesis: now that all base `calls` edges are persisted,
  1643. // synthesize observer/callback dispatch edges (dispatcher → registered
  1644. // callbacks) that static parsing leaves out. Best-effort — never fail the
  1645. // index on it. The pool (when it survived resolution) is REUSED to fan the
  1646. // independent passes across its read-only workers — that's why its destroy
  1647. // lives in the finally below, after synthesis, not at the end of the batch
  1648. // loop. See docs/design/callback-edge-synthesis.md.
  1649. const tSynth = Date.now();
  1650. try {
  1651. aggregateStats.byMethod['callback-synthesis'] = await synthesizeCallbackEdges(
  1652. this.queries,
  1653. this.context,
  1654. onSynthesisProgress,
  1655. pool,
  1656. parallel?.backpressure
  1657. );
  1658. } catch {
  1659. // synthesis is additive and optional; ignore failures
  1660. }
  1661. if (process.env.CODEGRAPH_SYNTH_TIMINGS) console.error(`[phase-timing] callback-synthesis: ${Date.now() - tSynth}ms`);
  1662. } finally {
  1663. if (pool) await pool.destroy().catch(() => undefined);
  1664. }
  1665. if (loopProf) {
  1666. const parts = Object.entries(loopProf).map(([k, v]) => `${k}=${(v / 1000).toFixed(1)}s`).join(' ');
  1667. console.error(`[resolve-profile] loop-stages ${parts}`);
  1668. }
  1669. this.dumpResolveProfile('main');
  1670. return {
  1671. resolved: [],
  1672. unresolved: [],
  1673. stats: aggregateStats,
  1674. };
  1675. }
  1676. /**
  1677. * Get detected frameworks
  1678. */
  1679. getDetectedFrameworks(): string[] {
  1680. return this.frameworks.map((f) => f.name);
  1681. }
  1682. /**
  1683. * Check if reference is to a built-in or external symbol
  1684. */
  1685. private isBuiltInOrExternal(ref: UnresolvedRef): boolean {
  1686. const name = ref.referenceName;
  1687. const isJsTs = ref.language === 'typescript' || ref.language === 'javascript'
  1688. || ref.language === 'tsx' || ref.language === 'jsx' || ref.language === 'arkts';
  1689. // JavaScript/TypeScript built-ins
  1690. if (isJsTs && JS_BUILT_INS.has(name)) {
  1691. return true;
  1692. }
  1693. // ArkTS resource-reference intrinsics — `$r('app.string.x')` /
  1694. // `$rawfile('x.png')` are framework-provided and appear dozens of times
  1695. // per UI file; without this they can resolve to a stray same-named
  1696. // symbol (e.g. a checked-in hvigor wrapper's `$r`).
  1697. if (ref.language === 'arkts' && (name === '$r' || name === '$rawfile')) {
  1698. return true;
  1699. }
  1700. // Common JS/TS library calls (console.log, Math.floor, JSON.parse)
  1701. if (isJsTs && (name.startsWith('console.') || name.startsWith('Math.') || name.startsWith('JSON.'))) {
  1702. return true;
  1703. }
  1704. // React hooks from React itself
  1705. if (isJsTs && REACT_HOOKS.has(name)) {
  1706. return true;
  1707. }
  1708. // Python built-ins (bare calls only — dotted calls like console.print are method calls)
  1709. if (ref.language === 'python' && PYTHON_BUILT_INS.has(name)) {
  1710. return true;
  1711. }
  1712. // Python built-in method calls (e.g., list.extend, dict.update)
  1713. if (ref.language === 'python') {
  1714. const dotIdx = name.indexOf('.');
  1715. if (dotIdx > 0) {
  1716. const receiver = name.substring(0, dotIdx);
  1717. const method = name.substring(dotIdx + 1);
  1718. // Filter calls on built-in types (list.append, dict.update, etc.)
  1719. if (PYTHON_BUILT_IN_TYPES.has(receiver)) {
  1720. return true;
  1721. }
  1722. // Filter built-in methods on non-class receivers
  1723. // (e.g., items.append where items is a local list variable)
  1724. // But allow if the capitalized receiver matches a known codebase class
  1725. if (PYTHON_BUILT_IN_METHODS.has(method)) {
  1726. const capitalized = receiver.charAt(0).toUpperCase() + receiver.slice(1);
  1727. if (!this.knownNames?.has(capitalized)) {
  1728. return true;
  1729. }
  1730. }
  1731. }
  1732. // A bare name colliding with a builtin method (index, get, update, count…)
  1733. // is only a builtin when NOTHING in the codebase declares it. A declared
  1734. // symbol with that exact name — e.g. a Flask/FastAPI view `def index()` or
  1735. // `def get()` — is a real reference target. Mirrors the knownNames guard on
  1736. // the dotted branch above; without it, every handler named after a builtin
  1737. // method silently loses its route→handler edge.
  1738. if (PYTHON_BUILT_IN_METHODS.has(name) && !this.knownNames?.has(name)) {
  1739. return true;
  1740. }
  1741. }
  1742. // Go standard library packages — refs like "fmt.Println", "http.ListenAndServe", etc.
  1743. if (ref.language === 'go') {
  1744. const dotIdx = name.indexOf('.');
  1745. if (dotIdx > 0) {
  1746. const pkg = name.substring(0, dotIdx);
  1747. if (GO_STDLIB_PACKAGES.has(pkg)) {
  1748. return true;
  1749. }
  1750. }
  1751. if (GO_BUILT_INS.has(name)) {
  1752. return true;
  1753. }
  1754. }
  1755. // Pascal/Delphi built-ins and standard library units
  1756. if (ref.language === 'pascal') {
  1757. if (PASCAL_UNIT_PREFIXES.some((p) => name.startsWith(p))) {
  1758. return true;
  1759. }
  1760. if (PASCAL_BUILT_INS.has(name)) {
  1761. return true;
  1762. }
  1763. }
  1764. // C/C++ standard library symbols (printf, malloc, std::vector, etc.).
  1765. // Names that collide with user-defined symbols are NOT filtered —
  1766. // C and C++ projects routinely shadow stdlib names (custom allocators
  1767. // define `malloc`/`free`, stream wrappers define `read`/`write`/`open`,
  1768. // containers define `move`/`swap`, logging libs wrap `printf`). Killing
  1769. // those resolutions makes the graph wrong, not cleaner. We only filter
  1770. // when there's no user node with this name — then name-matching would
  1771. // produce zero edges anyway and the filter just short-circuits work.
  1772. if (ref.language === 'c' || ref.language === 'cpp') {
  1773. // C++ std:: namespace prefix — safe to filter unconditionally,
  1774. // since `std::foo` is never a user-defined qualified name in
  1775. // tree-sitter output.
  1776. if (name.startsWith('std::')) return true;
  1777. if (C_BUILT_INS.has(name) || CPP_BUILT_INS.has(name)) {
  1778. return !this.hasAnyPossibleMatch(name);
  1779. }
  1780. }
  1781. return false;
  1782. }
  1783. /**
  1784. * Get file path from node ID
  1785. */
  1786. private getFilePathFromNodeId(nodeId: string): string {
  1787. const node = this.queries.getNodeById(nodeId);
  1788. return node?.filePath || '';
  1789. }
  1790. /**
  1791. * Get language from node ID
  1792. */
  1793. private getLanguageFromNodeId(nodeId: string): UnresolvedRef['language'] {
  1794. const node = this.queries.getNodeById(nodeId);
  1795. return node?.language || 'unknown';
  1796. }
  1797. /**
  1798. * Drop an import/name-strategy resolution that crosses a language family.
  1799. * Two regimes (mirrors `applyLanguageGate`'s candidate filter):
  1800. * - `references` (type usage): STRICT — a `Type.member` static read names a
  1801. * same-family type, never a coincidentally same-named symbol in another
  1802. * language. Drops any non-same-family target.
  1803. * - `imports` (import binding / `#include`): both-known — a C++ `#include
  1804. * "X.h"` must not resolve to a same-named ObjC header on another platform
  1805. * (basename collision), but a singleton-family / SFC language (`vue` →
  1806. * `.ts`) importing across is left alone.
  1807. * Applies to the import (strategy 2) + name-match (strategy 3) results.
  1808. */
  1809. /**
  1810. * Collect the `@using` namespaces in scope for a `.razor`/`.cshtml` file: its
  1811. * own `@using` directives plus every `_Imports.razor` from the file's folder up
  1812. * to the project root (Razor `_Imports` cascade). Cached per file.
  1813. */
  1814. private getRazorUsings(filePath: string): string[] {
  1815. const cached = this.razorUsingsCache.get(filePath);
  1816. if (cached) return cached;
  1817. const usings = new Set<string>();
  1818. const addFrom = (src: string | null): void => {
  1819. if (!src) return;
  1820. for (const m of src.matchAll(/^\s*@using\s+(?:static\s+)?([A-Za-z_][\w.]*)/gm)) usings.add(m[1]!);
  1821. };
  1822. addFrom(this.context.readFile(filePath));
  1823. let dir = filePath.includes('/') ? filePath.slice(0, filePath.lastIndexOf('/')) : '';
  1824. // Walk up to the project root, reading each level's _Imports.razor.
  1825. for (;;) {
  1826. addFrom(this.context.readFile(dir ? `${dir}/_Imports.razor` : '_Imports.razor'));
  1827. if (!dir) break;
  1828. const slash = dir.lastIndexOf('/');
  1829. dir = slash >= 0 ? dir.slice(0, slash) : '';
  1830. }
  1831. const arr = [...usings];
  1832. this.razorUsingsCache.set(filePath, arr);
  1833. return arr;
  1834. }
  1835. /**
  1836. * Resolve a Razor/Blazor simple type ref through the file's `@using`
  1837. * namespaces: `CatalogBrand` + `@using BlazorShared.Models` → the node whose
  1838. * qualified name is `BlazorShared.Models::CatalogBrand`. Only resolves when the
  1839. * `@using` set yields exactly ONE type (otherwise it stays ambiguous and falls
  1840. * through to name-matching).
  1841. */
  1842. private resolveRazorUsing(ref: UnresolvedRef): ResolvedRef | null {
  1843. if (ref.referenceName.includes('.') || ref.referenceName.includes('::')) return null;
  1844. const usings = this.getRazorUsings(ref.filePath);
  1845. if (usings.length === 0) return null;
  1846. const found = new Map<string, Node>();
  1847. for (const ns of usings) {
  1848. for (const cand of this.context.getNodesByQualifiedName(`${ns}::${ref.referenceName}`)) {
  1849. found.set(cand.id, cand);
  1850. }
  1851. }
  1852. if (found.size !== 1) return null;
  1853. const target = found.values().next().value!;
  1854. return { original: ref, targetNodeId: target.id, confidence: 0.9, resolvedBy: 'import' };
  1855. }
  1856. /**
  1857. * Resolve a CFML inheritance reference written as a component path (#1152).
  1858. * Two forms exist in real code:
  1859. *
  1860. * - Dotted: `extends="coldbox.system.web.Controller"` — dots are directory
  1861. * separators from the webroot or a CFML mapping. Mappings live in server
  1862. * config / Application.cfc, so the leading segments may not exist in the
  1863. * repo at all (in the coldbox repo itself the path is `system/web/
  1864. * Controller.cfc` — the `coldbox.` root IS the repo). Matched by final
  1865. * segment (the class), corroborated right-to-left against the candidate's
  1866. * parent directories.
  1867. * - Relative: `extends="../base"` / `extends="./base"` (the FW/1 style) —
  1868. * resolved against the referencing file's own directory.
  1869. *
  1870. * Conservative by design: a candidate needs at least one corroborating
  1871. * directory segment (a dotted path whose only same-named class sits in an
  1872. * unrelated directory is almost always an out-of-repo library supertype —
  1873. * mxunit/testbox/coldbox-as-dependency), and a corroboration tie yields no
  1874. * edge. Directory comparison is case-insensitive (CFML path resolution is);
  1875. * the class segment itself is matched exactly, which real code satisfies —
  1876. * dotted paths are written to match the on-disk file name.
  1877. */
  1878. private resolveCfmlComponentPath(ref: UnresolvedRef): ResolvedRef | null {
  1879. const cfmlCandidates = (name: string): Node[] =>
  1880. this.context
  1881. .getNodesByName(name)
  1882. .filter(
  1883. (n) =>
  1884. (n.kind === 'class' || n.kind === 'interface') &&
  1885. (n.language === 'cfml' || n.language === 'cfscript')
  1886. );
  1887. const norm = (p: string): string => p.replace(/\\/g, '/').toLowerCase();
  1888. // Relative-path form: `../base`, `./base`, `sub/thing` — resolve against
  1889. // the referencing file's directory and require an exact (case-insensitive)
  1890. // file match.
  1891. if (ref.referenceName.includes('/')) {
  1892. const rel = ref.referenceName.replace(/\.cfc$/i, '');
  1893. const fromDir = ref.filePath.replace(/\\/g, '/').split('/').slice(0, -1);
  1894. const parts = [...fromDir];
  1895. for (const seg of rel.split('/')) {
  1896. if (seg === '' || seg === '.') continue;
  1897. if (seg === '..') {
  1898. if (parts.length === 0) return null; // escapes the project root
  1899. parts.pop();
  1900. } else {
  1901. parts.push(seg);
  1902. }
  1903. }
  1904. const wantPath = norm(parts.join('/') + '.cfc');
  1905. const className = parts[parts.length - 1];
  1906. if (!className) return null;
  1907. const target = cfmlCandidates(className).find((c) => norm(c.filePath) === wantPath);
  1908. return target
  1909. ? { original: ref, targetNodeId: target.id, confidence: 0.95, resolvedBy: 'file-path' }
  1910. : null;
  1911. }
  1912. // Dotted form.
  1913. const segments = ref.referenceName.split('.').map((s) => s.trim()).filter(Boolean);
  1914. if (segments.length < 2) return null;
  1915. const className = segments[segments.length - 1]!;
  1916. const dirSegments = segments.slice(0, -1);
  1917. let best: Node | null = null;
  1918. let bestScore = 0;
  1919. let tie = false;
  1920. for (const cand of cfmlCandidates(className)) {
  1921. const dirs = cand.filePath.replace(/\\/g, '/').split('/').slice(0, -1);
  1922. // Count matching directory segments right-to-left: for
  1923. // `coldbox.system.web.Controller` vs `system/web/Controller.cfc`,
  1924. // `web` and `system` match, then the repo root ends the run → score 2.
  1925. let score = 0;
  1926. while (
  1927. score < dirSegments.length &&
  1928. score < dirs.length &&
  1929. dirSegments[dirSegments.length - 1 - score]!.toLowerCase() ===
  1930. dirs[dirs.length - 1 - score]!.toLowerCase()
  1931. ) {
  1932. score++;
  1933. }
  1934. if (score > bestScore) {
  1935. best = cand;
  1936. bestScore = score;
  1937. tie = false;
  1938. } else if (score === bestScore && score > 0) {
  1939. tie = true;
  1940. }
  1941. }
  1942. if (!best || bestScore === 0 || tie) return null;
  1943. return { original: ref, targetNodeId: best.id, confidence: 0.9, resolvedBy: 'qualified-name' };
  1944. }
  1945. /**
  1946. * Resolve a `this.<member>` function-as-value reference (#756/#808) to the
  1947. * ENCLOSING CLASS's own member — never a same-named symbol elsewhere. The
  1948. * registration idiom (`btn.on('click', this.handleClick)`) names a member
  1949. * of the class being defined, so the only valid target shares the
  1950. * from-symbol's qualified-name scope. Function/method targets only — a
  1951. * property (a data field, post-#808 classification) yields no edge — same
  1952. * file required, no fallback of any kind.
  1953. */
  1954. private resolveThisMemberFnRef(ref: UnresolvedRef): ResolvedRef | null {
  1955. const member = ref.referenceName.slice('this.'.length);
  1956. if (!member) return null;
  1957. const fromNode = this.queries.getNodeById(ref.fromNodeId);
  1958. if (!fromNode) return null;
  1959. // A hook declared at class-body level (Ruby `before_action :authenticate`)
  1960. // attributes to the CLASS node itself — its qualified name IS the scope.
  1961. // For members, strip the member segment.
  1962. let classPrefix: string;
  1963. if (SUPERTYPE_BEARING_KINDS.has(fromNode.kind) || fromNode.kind === 'module') {
  1964. classPrefix = fromNode.qualifiedName;
  1965. } else {
  1966. const sep = fromNode.qualifiedName.lastIndexOf('::');
  1967. if (sep <= 0) return null; // not inside a class scope
  1968. classPrefix = fromNode.qualifiedName.slice(0, sep);
  1969. }
  1970. const candidates = this.context
  1971. .getNodesByQualifiedName(`${classPrefix}::${member}`)
  1972. .filter(
  1973. (n) =>
  1974. (n.kind === 'function' || n.kind === 'method') &&
  1975. n.filePath === ref.filePath &&
  1976. n.id !== ref.fromNodeId
  1977. );
  1978. if (candidates.length === 0) {
  1979. // Not on the class itself — possibly INHERITED. implements/extends
  1980. // edges don't exist yet in this pass, so retry in the supertype pass
  1981. // (resolveDeferredThisMemberRefs) instead of giving up.
  1982. this.deferredThisMemberRefs.push(ref);
  1983. return null;
  1984. }
  1985. const target = candidates.reduce((a, b) => (a.startLine <= b.startLine ? a : b));
  1986. return {
  1987. original: ref,
  1988. targetNodeId: target.id,
  1989. confidence: 0.95,
  1990. resolvedBy: 'function-ref',
  1991. };
  1992. }
  1993. /**
  1994. * Second pass for `this.<member>` refs whose member wasn't on the enclosing
  1995. * class itself (#808): once implements/extends edges exist, walk the
  1996. * class's supertypes (transitively, depth-capped) and resolve the member on
  1997. * the nearest one that declares it — `this.handleSubmit` registered in a
  1998. * subclass resolves to `FormBase::handleSubmit`. Validated targets only
  1999. * (function/method kind, same language family); no match → no edge.
  2000. * Mirrors resolveChainedCallsViaConformance's lifecycle. Returns the number
  2001. * of newly-created edges.
  2002. */
  2003. async resolveDeferredThisMemberRefs(): Promise<number> {
  2004. const deferred = this.deferredThisMemberRefs;
  2005. this.deferredThisMemberRefs = [];
  2006. if (deferred.length === 0) return 0;
  2007. this.clearCaches();
  2008. // Synchronous main-thread post-pass with a per-ref supertype BFS — yield
  2009. // periodically so the #850 liveness watchdog heartbeat can fire (#1091).
  2010. const maybeYield = createYielder();
  2011. const resolved: ResolvedRef[] = [];
  2012. for (const ref of deferred) {
  2013. await maybeYield();
  2014. const member = ref.referenceName.slice('this.'.length);
  2015. const fromNode = this.queries.getNodeById(ref.fromNodeId);
  2016. if (!fromNode || !member) continue;
  2017. // Class-body-level hooks (Ruby) attribute to the CLASS node itself.
  2018. let className: string;
  2019. if (SUPERTYPE_BEARING_KINDS.has(fromNode.kind) || fromNode.kind === 'module') {
  2020. className = fromNode.name;
  2021. } else {
  2022. const sep = fromNode.qualifiedName.lastIndexOf('::');
  2023. if (sep <= 0) continue;
  2024. const classPrefix = fromNode.qualifiedName.slice(0, sep);
  2025. className = classPrefix.includes('::')
  2026. ? classPrefix.slice(classPrefix.lastIndexOf('::') + 2)
  2027. : classPrefix;
  2028. }
  2029. // NODE-anchored BFS up the supertype graph: start from the class node
  2030. // in the ref's own file (never a same-named class elsewhere — rails has
  2031. // a dozen `Engine`s), follow implements/extends EDGES to supertype
  2032. // NODES, and look members up through `contains` edges. No name-based
  2033. // unions anywhere — a name-keyed getSupertypes('Engine') merged every
  2034. // Engine's parents and produced a cross-class wrong edge on rails.
  2035. let frontierNodes = this.context
  2036. .getNodesByName(className)
  2037. .filter(
  2038. (n) =>
  2039. SUPERTYPE_BEARING_KINDS.has(n.kind) &&
  2040. n.filePath === ref.filePath
  2041. );
  2042. if (frontierNodes.length === 0) {
  2043. // The class itself may be declared in another file (partial/reopened
  2044. // classes); fall back to same-family nodes of that name.
  2045. frontierNodes = this.context
  2046. .getNodesByName(className)
  2047. .filter(
  2048. (n) =>
  2049. SUPERTYPE_BEARING_KINDS.has(n.kind) &&
  2050. sameLanguageFamily(n.language, ref.language)
  2051. );
  2052. }
  2053. const seenNodes = new Set<string>(frontierNodes.map((n) => n.id));
  2054. let target: Node | null = null;
  2055. for (let depth = 0; depth < 5 && frontierNodes.length > 0 && !target; depth++) {
  2056. const next: Node[] = [];
  2057. for (const typeNode of frontierNodes) {
  2058. for (const edge of this.queries.getOutgoingEdges(typeNode.id, ['implements', 'extends'])) {
  2059. const superNode = this.queries.getNodeById(edge.target);
  2060. if (!superNode || seenNodes.has(superNode.id)) continue;
  2061. seenNodes.add(superNode.id);
  2062. if (!SUPERTYPE_BEARING_KINDS.has(superNode.kind)) continue;
  2063. // Member lookup anchored on the supertype's contains edges.
  2064. for (const c of this.queries.getOutgoingEdges(superNode.id, ['contains'])) {
  2065. const m = this.queries.getNodeById(c.target);
  2066. if (
  2067. m &&
  2068. m.name === member &&
  2069. (m.kind === 'function' || m.kind === 'method') &&
  2070. sameLanguageFamily(m.language, ref.language)
  2071. ) {
  2072. target = m;
  2073. break;
  2074. }
  2075. }
  2076. if (target) break;
  2077. next.push(superNode);
  2078. }
  2079. if (target) break;
  2080. }
  2081. frontierNodes = next;
  2082. }
  2083. if (target) {
  2084. resolved.push({
  2085. original: ref,
  2086. targetNodeId: target.id,
  2087. confidence: 0.85,
  2088. resolvedBy: 'function-ref',
  2089. });
  2090. }
  2091. }
  2092. if (resolved.length === 0) return 0;
  2093. const edges = this.createEdges(resolved);
  2094. if (edges.length > 0) {
  2095. this.queries.insertEdges(edges);
  2096. this.clearCaches();
  2097. }
  2098. return edges.length;
  2099. }
  2100. private gateLanguage(result: ResolvedRef | null, ref: UnresolvedRef): ResolvedRef | null {
  2101. if (!result) return result;
  2102. const tgt = this.getLanguageFromNodeId(result.targetNodeId);
  2103. if (!tgt || !ref.language) return result;
  2104. if ((ref.referenceKind === 'references' || ref.referenceKind === 'function_ref') && !sameLanguageFamily(tgt, ref.language)) return null;
  2105. if (ref.referenceKind === 'imports' && crossesKnownFamily(tgt, ref.language)) return null;
  2106. return result;
  2107. }
  2108. /**
  2109. * Drop a FRAMEWORK-strategy resolution that crosses two *known* language
  2110. * families for a type-usage (`references`) or import-binding (`imports`)
  2111. * edge. The framework strategy is intentionally ungated for cross-language
  2112. * bridges, but those legitimate bridges are either `calls` edges (RN/Expo
  2113. * JS → native) or config↔code edges whose config side (`yaml`/`blade`/…) is
  2114. * not a known programming-language family. A `references`/`imports` edge
  2115. * between two *known* families is always a coincidental name collision — the
  2116. * React/Svelte/Vue PascalCase component resolvers name-match `getNodesByName`
  2117. * without a language check, so a TS `<TestRunner>` ref happily matched a
  2118. * Kotlin `class TestRunner`. Gating only the both-known-cross-family case
  2119. * lets config bridges and `calls` bridges through untouched.
  2120. */
  2121. private gateFrameworkLanguage(result: ResolvedRef | null, ref: UnresolvedRef): ResolvedRef | null {
  2122. if (!result) return result;
  2123. if (ref.referenceKind !== 'references' && ref.referenceKind !== 'imports') return result;
  2124. const tgt = this.getLanguageFromNodeId(result.targetNodeId);
  2125. if (tgt && ref.language && crossesKnownFamily(tgt, ref.language)) return null;
  2126. return result;
  2127. }
  2128. }
  2129. /**
  2130. * Create a reference resolver instance
  2131. */
  2132. export function createResolver(projectRoot: string, queries: QueryBuilder): ReferenceResolver {
  2133. const resolver = new ReferenceResolver(projectRoot, queries);
  2134. resolver.initialize();
  2135. return resolver;
  2136. }