index.ts 117 KB

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