gitnexus 1.5.2 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (207) hide show
  1. package/README.md +10 -0
  2. package/dist/_shared/graph/types.d.ts +1 -1
  3. package/dist/_shared/graph/types.d.ts.map +1 -1
  4. package/dist/_shared/index.d.ts +1 -0
  5. package/dist/_shared/index.d.ts.map +1 -1
  6. package/dist/_shared/language-detection.d.ts.map +1 -1
  7. package/dist/_shared/language-detection.js +2 -0
  8. package/dist/_shared/language-detection.js.map +1 -1
  9. package/dist/_shared/languages.d.ts +1 -0
  10. package/dist/_shared/languages.d.ts.map +1 -1
  11. package/dist/_shared/languages.js +1 -0
  12. package/dist/_shared/languages.js.map +1 -1
  13. package/dist/_shared/lbug/schema-constants.d.ts +1 -1
  14. package/dist/_shared/lbug/schema-constants.d.ts.map +1 -1
  15. package/dist/_shared/lbug/schema-constants.js +3 -1
  16. package/dist/_shared/lbug/schema-constants.js.map +1 -1
  17. package/dist/_shared/mro-strategy.d.ts +19 -0
  18. package/dist/_shared/mro-strategy.d.ts.map +1 -0
  19. package/dist/_shared/mro-strategy.js +2 -0
  20. package/dist/_shared/mro-strategy.js.map +1 -0
  21. package/dist/cli/ai-context.d.ts +1 -0
  22. package/dist/cli/ai-context.js +28 -4
  23. package/dist/cli/analyze.d.ts +2 -0
  24. package/dist/cli/analyze.js +2 -1
  25. package/dist/cli/group.d.ts +2 -0
  26. package/dist/cli/group.js +233 -0
  27. package/dist/cli/index.js +3 -0
  28. package/dist/cli/serve.js +4 -1
  29. package/dist/cli/setup.js +34 -3
  30. package/dist/cli/wiki.js +15 -44
  31. package/dist/config/ignore-service.js +8 -3
  32. package/dist/core/augmentation/engine.js +1 -1
  33. package/dist/core/git-staleness.d.ts +13 -0
  34. package/dist/core/git-staleness.js +29 -0
  35. package/dist/core/group/bridge-db.d.ts +82 -0
  36. package/dist/core/group/bridge-db.js +460 -0
  37. package/dist/core/group/bridge-schema.d.ts +27 -0
  38. package/dist/core/group/bridge-schema.js +55 -0
  39. package/dist/core/group/config-parser.d.ts +3 -0
  40. package/dist/core/group/config-parser.js +83 -0
  41. package/dist/core/group/contract-extractor.d.ts +7 -0
  42. package/dist/core/group/contract-extractor.js +1 -0
  43. package/dist/core/group/extractors/grpc-extractor.d.ts +16 -0
  44. package/dist/core/group/extractors/grpc-extractor.js +264 -0
  45. package/dist/core/group/extractors/http-route-extractor.d.ts +24 -0
  46. package/dist/core/group/extractors/http-route-extractor.js +428 -0
  47. package/dist/core/group/extractors/topic-extractor.d.ts +9 -0
  48. package/dist/core/group/extractors/topic-extractor.js +234 -0
  49. package/dist/core/group/matching.d.ts +13 -0
  50. package/dist/core/group/matching.js +198 -0
  51. package/dist/core/group/normalization.d.ts +3 -0
  52. package/dist/core/group/normalization.js +115 -0
  53. package/dist/core/group/service-boundary-detector.d.ts +8 -0
  54. package/dist/core/group/service-boundary-detector.js +155 -0
  55. package/dist/core/group/service.d.ts +46 -0
  56. package/dist/core/group/service.js +160 -0
  57. package/dist/core/group/storage.d.ts +9 -0
  58. package/dist/core/group/storage.js +91 -0
  59. package/dist/core/group/sync.d.ts +21 -0
  60. package/dist/core/group/sync.js +148 -0
  61. package/dist/core/group/types.d.ts +130 -0
  62. package/dist/core/group/types.js +1 -0
  63. package/dist/core/ingestion/binding-accumulator.d.ts +207 -0
  64. package/dist/core/ingestion/binding-accumulator.js +332 -0
  65. package/dist/core/ingestion/call-processor.d.ts +155 -24
  66. package/dist/core/ingestion/call-processor.js +1129 -247
  67. package/dist/core/ingestion/class-extractors/generic.d.ts +2 -0
  68. package/dist/core/ingestion/class-extractors/generic.js +135 -0
  69. package/dist/core/ingestion/class-types.d.ts +34 -0
  70. package/dist/core/ingestion/class-types.js +1 -0
  71. package/dist/core/ingestion/entry-point-scoring.d.ts +1 -0
  72. package/dist/core/ingestion/entry-point-scoring.js +1 -0
  73. package/dist/core/ingestion/field-extractors/configs/helpers.d.ts +5 -1
  74. package/dist/core/ingestion/field-extractors/configs/helpers.js +13 -3
  75. package/dist/core/ingestion/field-types.d.ts +2 -2
  76. package/dist/core/ingestion/filesystem-walker.js +8 -0
  77. package/dist/core/ingestion/framework-detection.d.ts +1 -0
  78. package/dist/core/ingestion/framework-detection.js +1 -0
  79. package/dist/core/ingestion/heritage-processor.d.ts +8 -15
  80. package/dist/core/ingestion/heritage-processor.js +15 -28
  81. package/dist/core/ingestion/import-processor.d.ts +1 -11
  82. package/dist/core/ingestion/import-processor.js +0 -12
  83. package/dist/core/ingestion/import-resolvers/utils.js +1 -0
  84. package/dist/core/ingestion/import-resolvers/vue.d.ts +8 -0
  85. package/dist/core/ingestion/import-resolvers/vue.js +9 -0
  86. package/dist/core/ingestion/language-provider.d.ts +6 -3
  87. package/dist/core/ingestion/languages/c-cpp.js +168 -1
  88. package/dist/core/ingestion/languages/csharp.js +20 -0
  89. package/dist/core/ingestion/languages/dart.js +26 -4
  90. package/dist/core/ingestion/languages/go.js +22 -0
  91. package/dist/core/ingestion/languages/index.d.ts +1 -0
  92. package/dist/core/ingestion/languages/index.js +2 -0
  93. package/dist/core/ingestion/languages/java.js +17 -0
  94. package/dist/core/ingestion/languages/kotlin.js +24 -1
  95. package/dist/core/ingestion/languages/php.js +23 -11
  96. package/dist/core/ingestion/languages/python.js +9 -0
  97. package/dist/core/ingestion/languages/ruby.js +28 -0
  98. package/dist/core/ingestion/languages/rust.js +38 -0
  99. package/dist/core/ingestion/languages/swift.js +31 -0
  100. package/dist/core/ingestion/languages/typescript.d.ts +1 -0
  101. package/dist/core/ingestion/languages/typescript.js +54 -1
  102. package/dist/core/ingestion/languages/vue.d.ts +13 -0
  103. package/dist/core/ingestion/languages/vue.js +81 -0
  104. package/dist/core/ingestion/method-extractors/configs/c-cpp.d.ts +3 -0
  105. package/dist/core/ingestion/method-extractors/configs/c-cpp.js +387 -0
  106. package/dist/core/ingestion/method-extractors/configs/csharp.js +5 -1
  107. package/dist/core/ingestion/method-extractors/configs/dart.d.ts +2 -0
  108. package/dist/core/ingestion/method-extractors/configs/dart.js +376 -0
  109. package/dist/core/ingestion/method-extractors/configs/go.d.ts +2 -0
  110. package/dist/core/ingestion/method-extractors/configs/go.js +176 -0
  111. package/dist/core/ingestion/method-extractors/configs/jvm.js +13 -4
  112. package/dist/core/ingestion/method-extractors/configs/php.d.ts +2 -0
  113. package/dist/core/ingestion/method-extractors/configs/php.js +304 -0
  114. package/dist/core/ingestion/method-extractors/configs/python.d.ts +2 -0
  115. package/dist/core/ingestion/method-extractors/configs/python.js +309 -0
  116. package/dist/core/ingestion/method-extractors/configs/ruby.d.ts +2 -0
  117. package/dist/core/ingestion/method-extractors/configs/ruby.js +285 -0
  118. package/dist/core/ingestion/method-extractors/configs/rust.d.ts +2 -0
  119. package/dist/core/ingestion/method-extractors/configs/rust.js +195 -0
  120. package/dist/core/ingestion/method-extractors/configs/swift.d.ts +2 -0
  121. package/dist/core/ingestion/method-extractors/configs/swift.js +277 -0
  122. package/dist/core/ingestion/method-extractors/configs/typescript-javascript.d.ts +3 -0
  123. package/dist/core/ingestion/method-extractors/configs/typescript-javascript.js +338 -0
  124. package/dist/core/ingestion/method-extractors/generic.js +38 -15
  125. package/dist/core/ingestion/method-types.d.ts +25 -0
  126. package/dist/core/ingestion/model/field-registry.d.ts +18 -0
  127. package/dist/core/ingestion/model/field-registry.js +22 -0
  128. package/dist/core/ingestion/model/heritage-map.d.ts +70 -0
  129. package/dist/core/ingestion/model/heritage-map.js +159 -0
  130. package/dist/core/ingestion/model/index.d.ts +20 -0
  131. package/dist/core/ingestion/model/index.js +41 -0
  132. package/dist/core/ingestion/model/method-registry.d.ts +62 -0
  133. package/dist/core/ingestion/model/method-registry.js +130 -0
  134. package/dist/core/ingestion/model/registration-table.d.ts +139 -0
  135. package/dist/core/ingestion/model/registration-table.js +224 -0
  136. package/dist/core/ingestion/model/resolution-context.d.ts +93 -0
  137. package/dist/core/ingestion/model/resolution-context.js +337 -0
  138. package/dist/core/ingestion/model/resolve.d.ts +56 -0
  139. package/dist/core/ingestion/model/resolve.js +242 -0
  140. package/dist/core/ingestion/model/semantic-model.d.ts +86 -0
  141. package/dist/core/ingestion/model/semantic-model.js +120 -0
  142. package/dist/core/ingestion/model/symbol-table.d.ts +222 -0
  143. package/dist/core/ingestion/model/symbol-table.js +206 -0
  144. package/dist/core/ingestion/model/type-registry.d.ts +39 -0
  145. package/dist/core/ingestion/model/type-registry.js +62 -0
  146. package/dist/core/ingestion/mro-processor.d.ts +4 -3
  147. package/dist/core/ingestion/mro-processor.js +310 -106
  148. package/dist/core/ingestion/parsing-processor.d.ts +5 -4
  149. package/dist/core/ingestion/parsing-processor.js +210 -85
  150. package/dist/core/ingestion/pipeline.d.ts +2 -0
  151. package/dist/core/ingestion/pipeline.js +192 -68
  152. package/dist/core/ingestion/tree-sitter-queries.d.ts +6 -6
  153. package/dist/core/ingestion/tree-sitter-queries.js +37 -0
  154. package/dist/core/ingestion/type-env.d.ts +15 -2
  155. package/dist/core/ingestion/type-env.js +163 -102
  156. package/dist/core/ingestion/type-extractors/csharp.js +17 -0
  157. package/dist/core/ingestion/type-extractors/jvm.js +11 -0
  158. package/dist/core/ingestion/type-extractors/php.js +0 -55
  159. package/dist/core/ingestion/type-extractors/ruby.js +0 -32
  160. package/dist/core/ingestion/type-extractors/swift.js +13 -0
  161. package/dist/core/ingestion/type-extractors/types.d.ts +8 -8
  162. package/dist/core/ingestion/type-extractors/typescript.js +66 -69
  163. package/dist/core/ingestion/utils/ast-helpers.d.ts +33 -43
  164. package/dist/core/ingestion/utils/ast-helpers.js +129 -565
  165. package/dist/core/ingestion/utils/method-props.d.ts +32 -0
  166. package/dist/core/ingestion/utils/method-props.js +147 -0
  167. package/dist/core/ingestion/vue-sfc-extractor.d.ts +44 -0
  168. package/dist/core/ingestion/vue-sfc-extractor.js +94 -0
  169. package/dist/core/ingestion/workers/parse-worker.d.ts +31 -19
  170. package/dist/core/ingestion/workers/parse-worker.js +463 -198
  171. package/dist/core/lbug/lbug-adapter.d.ts +6 -0
  172. package/dist/core/lbug/lbug-adapter.js +68 -3
  173. package/dist/core/lbug/pool-adapter.d.ts +76 -0
  174. package/dist/core/lbug/pool-adapter.js +522 -0
  175. package/dist/core/run-analyze.d.ts +2 -0
  176. package/dist/core/run-analyze.js +1 -1
  177. package/dist/core/search/bm25-index.js +1 -1
  178. package/dist/core/tree-sitter/parser-loader.js +1 -0
  179. package/dist/core/wiki/graph-queries.js +1 -1
  180. package/dist/core/wiki/html-viewer.js +6 -4
  181. package/dist/core/wiki/llm-client.js +4 -6
  182. package/dist/mcp/core/embedder.js +6 -5
  183. package/dist/mcp/core/lbug-adapter.d.ts +3 -63
  184. package/dist/mcp/core/lbug-adapter.js +3 -484
  185. package/dist/mcp/local/local-backend.d.ts +31 -2
  186. package/dist/mcp/local/local-backend.js +255 -46
  187. package/dist/mcp/resources.js +5 -4
  188. package/dist/mcp/staleness.d.ts +3 -13
  189. package/dist/mcp/staleness.js +2 -31
  190. package/dist/mcp/tools.js +80 -4
  191. package/dist/server/analyze-job.d.ts +2 -0
  192. package/dist/server/analyze-job.js +4 -0
  193. package/dist/server/api.d.ts +20 -1
  194. package/dist/server/api.js +306 -71
  195. package/dist/server/git-clone.d.ts +2 -1
  196. package/dist/server/git-clone.js +98 -5
  197. package/dist/storage/git.d.ts +13 -0
  198. package/dist/storage/git.js +25 -0
  199. package/dist/storage/repo-manager.js +1 -1
  200. package/package.json +8 -2
  201. package/scripts/patch-tree-sitter-swift.cjs +78 -0
  202. package/dist/core/ingestion/named-binding-processor.d.ts +0 -18
  203. package/dist/core/ingestion/named-binding-processor.js +0 -42
  204. package/dist/core/ingestion/resolution-context.d.ts +0 -58
  205. package/dist/core/ingestion/resolution-context.js +0 -135
  206. package/dist/core/ingestion/symbol-table.d.ts +0 -79
  207. package/dist/core/ingestion/symbol-table.js +0 -115
@@ -0,0 +1,337 @@
1
+ /**
2
+ * Resolution Context
3
+ *
4
+ * Single implementation of tiered name resolution.
5
+ *
6
+ * Resolution tiers (highest confidence first):
7
+ * 1. Same file (lookupExactAll — authoritative)
8
+ * 2a-named. Named binding chain (walkBindingChain via NamedImportMap)
9
+ * 2a. Import-scoped (iterate importedFiles with lookupExactAll per file)
10
+ * 2b. Package-scoped (iterate indexed files matching package dir with lookupExactAll)
11
+ * 3. Global (lookupClassByName + lookupImplByName + lookupCallableByName — consumers must check count)
12
+ *
13
+ * Each tier queries the minimum necessary scope directly:
14
+ * - Tier 2a iterates the caller's import set (O(imports) × O(1) lookupExactAll).
15
+ * - Tier 2b iterates all indexed files filtered by package dir
16
+ * (O(files) × O(1) lookupExactAll — avoids a global name scan).
17
+ * - Tier 3 combines lookupClassByName + lookupImplByName + lookupCallableByName
18
+ * (three O(1) index lookups with a narrow, type-specific result set).
19
+ */
20
+ import { createSemanticModel } from './semantic-model.js';
21
+ /**
22
+ * Check if a file path is directly inside a package directory identified by
23
+ * its suffix. Used by Tier 2b package-scoped resolution (Go / C#).
24
+ */
25
+ export function isFileInPackageDir(filePath, dirSuffix) {
26
+ // Prepend '/' so paths like "internal/auth/service.go" match suffix "/internal/auth/"
27
+ const normalized = '/' + filePath.replace(/\\/g, '/');
28
+ if (!normalized.includes(dirSuffix))
29
+ return false;
30
+ const afterDir = normalized.substring(normalized.indexOf(dirSuffix) + dirSuffix.length);
31
+ return !afterDir.includes('/');
32
+ }
33
+ /** Maximum re-export hops walkBindingChain will follow before giving up.
34
+ * A hard cap is needed to defend against pathological cycles that slip
35
+ * past the `visited` Set (e.g. a binding chain whose key is equal by
36
+ * string value but visits distinct modules). Five hops covers the
37
+ * common TypeScript monorepo pattern (component → pkg/index →
38
+ * packages/index → root/index → types/index). Chains longer than this
39
+ * fall through to Tier 2a-import / Tier 2b / Tier 3 resolution, which
40
+ * is a silent false-negative that the caller may or may not recover
41
+ * from. If a real repo hits this limit, raise it — there is no
42
+ * correctness reason to keep it at exactly 5. */
43
+ const MAX_BINDING_CHAIN_DEPTH = 5;
44
+ /**
45
+ * Walk a named-binding re-export chain through NamedImportMap.
46
+ *
47
+ * When file A imports { User } from B, and B re-exports { User } from C,
48
+ * the NamedImportMap for A points to B, but B has no User definition.
49
+ * This function follows the chain: A → B → C until a definition is found.
50
+ *
51
+ * Returns the definitions found at the end of the chain, or null if the
52
+ * chain breaks (missing binding, circular reference, or
53
+ * {@link MAX_BINDING_CHAIN_DEPTH} exceeded). Internal to
54
+ * resolution-context — not exported from the model barrel.
55
+ */
56
+ function walkBindingChain(name, currentFilePath, symbolTable, namedImportMap) {
57
+ // Fast exit: most files have no named imports at all. Skip the Set
58
+ // allocation + loop entry on the common empty-binding path so resolve()
59
+ // stays allocation-free for the typical call site.
60
+ const firstBindings = namedImportMap.get(currentFilePath);
61
+ if (!firstBindings)
62
+ return null;
63
+ const firstBinding = firstBindings.get(name);
64
+ if (!firstBinding)
65
+ return null;
66
+ let lookupFile = currentFilePath;
67
+ let lookupName = name;
68
+ const visited = new Set();
69
+ for (let depth = 0; depth < MAX_BINDING_CHAIN_DEPTH; depth++) {
70
+ const bindings = depth === 0 ? firstBindings : namedImportMap.get(lookupFile);
71
+ if (!bindings)
72
+ return null;
73
+ const binding = depth === 0 ? firstBinding : bindings.get(lookupName);
74
+ if (!binding)
75
+ return null;
76
+ const key = `${binding.sourcePath}:${binding.exportedName}`;
77
+ if (visited.has(key))
78
+ return null; // circular
79
+ visited.add(key);
80
+ const targetName = binding.exportedName;
81
+ const resolvedDefs = symbolTable.lookupExactAll(binding.sourcePath, targetName);
82
+ if (resolvedDefs.length > 0)
83
+ return resolvedDefs;
84
+ // No definition in source file → follow re-export chain
85
+ lookupFile = binding.sourcePath;
86
+ lookupName = targetName;
87
+ }
88
+ return null;
89
+ }
90
+ /** Confidence scores per resolution tier. */
91
+ export const TIER_CONFIDENCE = {
92
+ 'same-file': 0.95,
93
+ 'import-scoped': 0.9,
94
+ global: 0.5,
95
+ };
96
+ export const createResolutionContext = () => {
97
+ const model = createSemanticModel();
98
+ const symbols = model.symbols;
99
+ const importMap = new Map();
100
+ const packageMap = new Map();
101
+ const namedImportMap = new Map();
102
+ const moduleAliasMap = new Map();
103
+ // Inverted index: packageDirSuffix → Set<filePath>.
104
+ // Built lazily on first Tier 2b hit — one-time cost of O(totalFiles ×
105
+ // allUniqueDirSuffixes) isFileInPackageDir calls across the entire
106
+ // packageMap, amortized over the pipeline run. Subsequent Tier 2b
107
+ // resolutions are O(callerPackages × filesInPackage × O(1)).
108
+ let packageDirIndex = null;
109
+ // Per-file cache state
110
+ let cacheFile = null;
111
+ let cache = null;
112
+ let cacheHits = 0;
113
+ let cacheMisses = 0;
114
+ // Tier hit counters — replaces the lost fuzzyCallCount diagnostic
115
+ let tierSameFile = 0;
116
+ let tierImportScoped = 0;
117
+ let tierGlobal = 0;
118
+ let tierMiss = 0;
119
+ // --- Core resolution (single implementation of tier logic) ---
120
+ const resolveUncached = (name, fromFile) => {
121
+ // Tier 1: Same file — authoritative match (returns all overloads)
122
+ const localDefs = symbols.lookupExactAll(fromFile, name);
123
+ if (localDefs.length > 0) {
124
+ tierSameFile++;
125
+ return { candidates: localDefs, tier: 'same-file' };
126
+ }
127
+ // Tier 2a-named: Named binding chain (aliased / re-exported imports)
128
+ // Checked before import-scoped so that `import { User as U }` resolves
129
+ // correctly even when lookupExactAll on the alias name returns nothing.
130
+ const chainResult = walkBindingChain(name, fromFile, symbols, namedImportMap);
131
+ if (chainResult && chainResult.length > 0) {
132
+ tierImportScoped++;
133
+ return { candidates: chainResult, tier: 'import-scoped' };
134
+ }
135
+ // Tier 2a: Import-scoped — iterate the caller's imported files directly.
136
+ // O(importedFiles) × O(1) lookupExactAll — no global name scan needed.
137
+ const importedFiles = importMap.get(fromFile);
138
+ if (importedFiles) {
139
+ const importedDefs = [];
140
+ for (const file of importedFiles) {
141
+ importedDefs.push(...symbols.lookupExactAll(file, name));
142
+ }
143
+ if (importedDefs.length > 0) {
144
+ tierImportScoped++;
145
+ return { candidates: importedDefs, tier: 'import-scoped' };
146
+ }
147
+ }
148
+ // Tier 2b: Package-scoped — look up files in the caller's imported package
149
+ // directories via an inverted index (packageDirSuffix → Set<filePath>),
150
+ // then do O(1) lookupExactAll per file. The inverted index is built lazily
151
+ // on first Tier 2b hit by scanning symbols.getFiles() once, making
152
+ // subsequent Tier 2b resolutions O(packages × filesInPackage) instead of
153
+ // O(allFiles × packages).
154
+ const importedPackages = packageMap.get(fromFile);
155
+ if (importedPackages) {
156
+ // Lazily build the inverted index on first use. For each indexed file,
157
+ // test it against isFileInPackageDir for all known dirSuffixes collected
158
+ // from packageMap. This scans all files once (instead of per-resolution)
159
+ // and produces a dirSuffix → Set<filePath> map.
160
+ if (!packageDirIndex) {
161
+ // Collect all unique dir suffixes across the entire packageMap
162
+ const allDirSuffixes = new Set();
163
+ for (const dirs of packageMap.values()) {
164
+ for (const d of dirs)
165
+ allDirSuffixes.add(d);
166
+ }
167
+ packageDirIndex = new Map();
168
+ for (const file of symbols.getFiles()) {
169
+ for (const dirSuffix of allDirSuffixes) {
170
+ if (isFileInPackageDir(file, dirSuffix)) {
171
+ let files = packageDirIndex.get(dirSuffix);
172
+ if (!files) {
173
+ files = new Set();
174
+ packageDirIndex.set(dirSuffix, files);
175
+ }
176
+ files.add(file);
177
+ }
178
+ }
179
+ }
180
+ }
181
+ const packageDefs = [];
182
+ for (const dirSuffix of importedPackages) {
183
+ const filesInDir = packageDirIndex.get(dirSuffix);
184
+ if (filesInDir) {
185
+ for (const file of filesInDir) {
186
+ packageDefs.push(...symbols.lookupExactAll(file, name));
187
+ }
188
+ }
189
+ }
190
+ if (packageDefs.length > 0) {
191
+ tierImportScoped++;
192
+ return { candidates: packageDefs, tier: 'import-scoped' };
193
+ }
194
+ }
195
+ // Tier 3: Global — targeted O(1) index lookups for each symbol category.
196
+ // Class-like symbols (Class, Struct, Interface, Enum, Record, Trait) are
197
+ // covered by lookupClassByName; Rust impl blocks by lookupImplByName
198
+ // (separate to avoid polluting heritage resolution); free callables
199
+ // (Function, Macro, Delegate) by lookupCallableByName; owner-scoped
200
+ // methods and constructors by `model.methods.lookupMethodByName`.
201
+ //
202
+ // FREE_CALLABLE_TYPES excludes Method/Constructor, so strictly-labeled
203
+ // methods are disjoint between the two indexes.
204
+ //
205
+ // Partial-state caveat: Python/Rust/Kotlin class methods are emitted
206
+ // as Function + ownerId — `rawSymbols.add` routes them through both
207
+ // the Function callable index AND, via the dispatch-key normalization
208
+ // in `wrappedAdd`, the method registry. The same `SymbolDefinition`
209
+ // reference lands in both `callableDefs` and `methodDefs`, so the
210
+ // Set-based dedup below is required.
211
+ //
212
+ // Known exclusion: TypeAlias, Const, and Variable are NOT reachable at
213
+ // Tier 3 — they don't belong to any of the indexes. TypeAlias is not
214
+ // a call target; Const/Variable are resolved via import or same-file
215
+ // tiers. Macro (C/C++) and Delegate (C#) stay in the callable index
216
+ // since call-processor.ts treats them as callable targets.
217
+ const classDefs = model.types.lookupClassByName(name);
218
+ const implDefs = model.types.lookupImplByName(name);
219
+ const callableDefs = symbols.lookupCallableByName(name);
220
+ const methodDefs = model.methods.lookupMethodByName(name);
221
+ if (classDefs.length === 0 &&
222
+ implDefs.length === 0 &&
223
+ callableDefs.length === 0 &&
224
+ methodDefs.length === 0) {
225
+ tierMiss++;
226
+ return null;
227
+ }
228
+ // Fast path: if no `Function + ownerId` class method was ever
229
+ // registered into the method registry (the only source of
230
+ // cross-index duplication), the callable and method indexes are
231
+ // guaranteed disjoint and we can concat without dedup.
232
+ if (!model.methods.hasFunctionMethods) {
233
+ const globalDefs = [
234
+ ...classDefs,
235
+ ...implDefs,
236
+ ...callableDefs,
237
+ ...methodDefs,
238
+ ];
239
+ tierGlobal++;
240
+ return { candidates: globalDefs, tier: 'global' };
241
+ }
242
+ // Slow path: dedup by nodeId because the same SymbolDefinition
243
+ // reference can land in both `callableDefs` (via the Function
244
+ // callable-index gate) and `methodDefs` (via the dispatch-key
245
+ // normalization routing Function+ownerId into MethodRegistry).
246
+ // Dedup covers all four index reads so any nodeId overlap (even
247
+ // theoretical ones between classDefs/implDefs) is caught.
248
+ const globalDefs = [];
249
+ const seen = new Set();
250
+ const pushUnique = (pool) => {
251
+ for (const def of pool) {
252
+ if (seen.has(def.nodeId))
253
+ continue;
254
+ seen.add(def.nodeId);
255
+ globalDefs.push(def);
256
+ }
257
+ };
258
+ pushUnique(classDefs);
259
+ pushUnique(implDefs);
260
+ pushUnique(callableDefs);
261
+ pushUnique(methodDefs);
262
+ tierGlobal++;
263
+ return { candidates: globalDefs, tier: 'global' };
264
+ };
265
+ const resolve = (name, fromFile) => {
266
+ // Check cache (only when enabled AND fromFile matches cached file)
267
+ if (cache && cacheFile === fromFile) {
268
+ if (cache.has(name)) {
269
+ cacheHits++;
270
+ return cache.get(name);
271
+ }
272
+ cacheMisses++;
273
+ }
274
+ const result = resolveUncached(name, fromFile);
275
+ // Store in cache if active and file matches
276
+ if (cache && cacheFile === fromFile) {
277
+ cache.set(name, result);
278
+ }
279
+ return result;
280
+ };
281
+ // --- Cache lifecycle ---
282
+ const enableCache = (filePath) => {
283
+ cacheFile = filePath;
284
+ if (!cache)
285
+ cache = new Map();
286
+ else
287
+ cache.clear();
288
+ };
289
+ const clearCache = () => {
290
+ cacheFile = null;
291
+ // Reuse the Map instance — just clear entries to reduce GC pressure at scale.
292
+ cache?.clear();
293
+ // Note: packageDirIndex is NOT invalidated here. It is built lazily on
294
+ // first Tier 2b hit and remains valid across file boundaries because
295
+ // packageMap and the symbol file set are append-only during the calls
296
+ // phase (all parsing/import processing completes before resolution).
297
+ // Invalidating per-file would destroy the amortization benefit — the
298
+ // O(files × dirs) rebuild would run per-file instead of once.
299
+ // Full invalidation happens in clear() (pipeline reset).
300
+ };
301
+ const getStats = () => ({
302
+ ...symbols.getStats(),
303
+ cacheHits,
304
+ cacheMisses,
305
+ tierSameFile,
306
+ tierImportScoped,
307
+ tierGlobal,
308
+ tierMiss,
309
+ });
310
+ const clear = () => {
311
+ model.clear();
312
+ importMap.clear();
313
+ packageMap.clear();
314
+ namedImportMap.clear();
315
+ moduleAliasMap.clear();
316
+ packageDirIndex = null; // invalidate — will rebuild on next Tier 2b hit
317
+ clearCache();
318
+ cacheHits = 0;
319
+ cacheMisses = 0;
320
+ tierSameFile = 0;
321
+ tierImportScoped = 0;
322
+ tierGlobal = 0;
323
+ tierMiss = 0;
324
+ };
325
+ return {
326
+ resolve,
327
+ model,
328
+ importMap,
329
+ packageMap,
330
+ namedImportMap,
331
+ moduleAliasMap,
332
+ enableCache,
333
+ clearCache,
334
+ getStats,
335
+ clear,
336
+ };
337
+ };
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Deterministic Resolution Functions
3
+ *
4
+ * Pure functions that resolve methods across the inheritance hierarchy
5
+ * using only the SemanticModel registries and HeritageMap — NO dependency
6
+ * on resolution-context.ts (circular dependency risk).
7
+ */
8
+ import type { SymbolDefinition } from './symbol-table.js';
9
+ import type { SemanticModel } from './semantic-model.js';
10
+ import type { HeritageMap } from './heritage-map.js';
11
+ import type { MroStrategy } from '../../../_shared/index.js';
12
+ /**
13
+ * Gather all ancestor IDs in BFS / topological order.
14
+ * Returns the linearized list of ancestor IDs (excluding the class itself).
15
+ */
16
+ declare function gatherAncestors(classId: string, parentMap: Map<string, string[]>): string[];
17
+ /**
18
+ * Compute C3 linearization for a class given a parentMap.
19
+ * Returns an array of ancestor IDs in C3 order (excluding the class itself),
20
+ * or null if linearization fails (inconsistent or cyclic hierarchy).
21
+ *
22
+ * Used internally by `lookupMethodByOwnerWithMRO` for the Python MRO
23
+ * strategy and re-exported for mro-processor.ts (graph-level MRO emission).
24
+ */
25
+ export declare function c3Linearize(classId: string, parentMap: Map<string, string[]>, cache: Map<string, string[] | null>, inProgress?: Set<string>): string[] | null;
26
+ export { gatherAncestors };
27
+ /**
28
+ * Look up a method on an owner class, walking the parent chain via HeritageMap
29
+ * when the method isn't found on the direct owner.
30
+ *
31
+ * Respects the 5 per-language MRO strategies:
32
+ * - `first-wins`: BFS ancestor walk, first match wins (default)
33
+ * - `leftmost-base`: BFS ancestor walk, leftmost base in declaration order wins (C++);
34
+ * HeritageMap preserves insertion order matching source declaration,
35
+ * so BFS order is equivalent to leftmost-base semantics
36
+ * - `c3`: C3-linearized ancestor order, first match wins (Python)
37
+ * - `implements-split`: BFS ancestor walk, first match wins (Java/C#) —
38
+ * full ambiguity detection for multiple interface defaults
39
+ * is handled by computeMRO at graph level
40
+ * - `qualified-syntax`: No auto-resolution (Rust) — returns undefined
41
+ *
42
+ * Uses the `c3Linearize` defined in this file (also consumed by
43
+ * mro-processor.ts for graph-level MRO emission) for the `c3` strategy.
44
+ *
45
+ * Depends only on {@link SemanticModel} + {@link HeritageMap} + an
46
+ * {@link MroStrategy} literal — NO dependency on SymbolTable, the language
47
+ * registry, or resolution-context, which keeps the `model/` module free of
48
+ * cross-layer imports. Callers derive the strategy from their language
49
+ * provider before invoking this function.
50
+ *
51
+ * @internal This is the low-level MRO walker. Exported so call-processor's
52
+ * higher-level resolvers (and unit tests) can invoke it directly. Callers
53
+ * outside `core/ingestion/` should use the higher-level resolvers in
54
+ * call-processor.ts instead of depending on this function.
55
+ */
56
+ export declare const lookupMethodByOwnerWithMRO: (ownerNodeId: string, methodName: string, heritageMap: HeritageMap, model: SemanticModel, strategy: MroStrategy, argCount?: number) => SymbolDefinition | undefined;
@@ -0,0 +1,242 @@
1
+ /**
2
+ * Deterministic Resolution Functions
3
+ *
4
+ * Pure functions that resolve methods across the inheritance hierarchy
5
+ * using only the SemanticModel registries and HeritageMap — NO dependency
6
+ * on resolution-context.ts (circular dependency risk).
7
+ */
8
+ // ---------------------------------------------------------------------------
9
+ // MRO primitives.
10
+ //
11
+ // `c3Linearize` and its BFS helper `gatherAncestors` live here so the model
12
+ // layer stays a pure leaf — mro-processor.ts (graph-level MRO emission)
13
+ // imports `c3Linearize` from this file.
14
+ // ---------------------------------------------------------------------------
15
+ /**
16
+ * Gather all ancestor IDs in BFS / topological order.
17
+ * Returns the linearized list of ancestor IDs (excluding the class itself).
18
+ */
19
+ function gatherAncestors(classId, parentMap) {
20
+ const visited = new Set();
21
+ const order = [];
22
+ const queue = [...(parentMap.get(classId) ?? [])];
23
+ while (queue.length > 0) {
24
+ const id = queue.shift();
25
+ if (visited.has(id))
26
+ continue;
27
+ visited.add(id);
28
+ order.push(id);
29
+ const grandparents = parentMap.get(id);
30
+ if (grandparents) {
31
+ for (const gp of grandparents) {
32
+ if (!visited.has(gp))
33
+ queue.push(gp);
34
+ }
35
+ }
36
+ }
37
+ return order;
38
+ }
39
+ /**
40
+ * Compute C3 linearization for a class given a parentMap.
41
+ * Returns an array of ancestor IDs in C3 order (excluding the class itself),
42
+ * or null if linearization fails (inconsistent or cyclic hierarchy).
43
+ *
44
+ * Used internally by `lookupMethodByOwnerWithMRO` for the Python MRO
45
+ * strategy and re-exported for mro-processor.ts (graph-level MRO emission).
46
+ */
47
+ export function c3Linearize(classId, parentMap, cache, inProgress) {
48
+ if (cache.has(classId))
49
+ return cache.get(classId);
50
+ // Cycle detection: if we're already computing this class, the hierarchy is cyclic
51
+ const visiting = inProgress ?? new Set();
52
+ if (visiting.has(classId)) {
53
+ cache.set(classId, null);
54
+ return null;
55
+ }
56
+ visiting.add(classId);
57
+ const directParents = parentMap.get(classId);
58
+ if (!directParents || directParents.length === 0) {
59
+ visiting.delete(classId);
60
+ cache.set(classId, []);
61
+ return [];
62
+ }
63
+ // Compute linearization for each parent first
64
+ const parentLinearizations = [];
65
+ for (const pid of directParents) {
66
+ const pLin = c3Linearize(pid, parentMap, cache, visiting);
67
+ if (pLin === null) {
68
+ visiting.delete(classId);
69
+ cache.set(classId, null);
70
+ return null;
71
+ }
72
+ parentLinearizations.push([pid, ...pLin]);
73
+ }
74
+ // Add the direct parents list as the final sequence
75
+ const sequences = [...parentLinearizations, [...directParents]];
76
+ const result = [];
77
+ while (sequences.some((s) => s.length > 0)) {
78
+ // Find a good head: one that doesn't appear in the tail of any other sequence
79
+ let head = null;
80
+ for (const seq of sequences) {
81
+ if (seq.length === 0)
82
+ continue;
83
+ const candidate = seq[0];
84
+ const inTail = sequences.some((other) => other.length > 1 && other.indexOf(candidate, 1) !== -1);
85
+ if (!inTail) {
86
+ head = candidate;
87
+ break;
88
+ }
89
+ }
90
+ if (head === null) {
91
+ // Inconsistent hierarchy
92
+ visiting.delete(classId);
93
+ cache.set(classId, null);
94
+ return null;
95
+ }
96
+ result.push(head);
97
+ // Remove the chosen head from all sequences
98
+ for (const seq of sequences) {
99
+ if (seq.length > 0 && seq[0] === head) {
100
+ seq.shift();
101
+ }
102
+ }
103
+ }
104
+ visiting.delete(classId);
105
+ cache.set(classId, result);
106
+ return result;
107
+ }
108
+ // `gatherAncestors` is exported so mro-processor.ts can reuse the same
109
+ // BFS traversal for graph-level MRO emission.
110
+ export { gatherAncestors };
111
+ // ---------------------------------------------------------------------------
112
+ // C3 linearization cache (per HeritageMap, auto-drained via WeakMap)
113
+ // ---------------------------------------------------------------------------
114
+ /**
115
+ * Per-HeritageMap cache of C3 linearization results keyed by owner nodeId.
116
+ *
117
+ * HeritageMap instances are immutable after construction, so C3 output is
118
+ * stable for the lifetime of a HeritageMap. WeakMap lets the cache auto-drain
119
+ * when the HeritageMap is garbage collected (end of ingestion run), so we
120
+ * never need to manually invalidate it.
121
+ *
122
+ * `null` is a sentinel for "C3 failed for this owner" (cyclic or inconsistent
123
+ * hierarchy) so we don't re-run the expensive linearization repeatedly.
124
+ */
125
+ const c3LinearizationCache = new WeakMap();
126
+ const getCachedC3Linearization = (ownerNodeId, heritageMap) => {
127
+ let perHmCache = c3LinearizationCache.get(heritageMap);
128
+ if (!perHmCache) {
129
+ perHmCache = new Map();
130
+ c3LinearizationCache.set(heritageMap, perHmCache);
131
+ }
132
+ const cached = perHmCache.get(ownerNodeId);
133
+ if (cached !== undefined)
134
+ return cached;
135
+ const parentMap = buildParentMapFromHeritage(ownerNodeId, heritageMap);
136
+ const result = c3Linearize(ownerNodeId, parentMap, new Map()) ?? null;
137
+ perHmCache.set(ownerNodeId, result);
138
+ return result;
139
+ };
140
+ // ---------------------------------------------------------------------------
141
+ // Heritage → parentMap conversion
142
+ // ---------------------------------------------------------------------------
143
+ /**
144
+ * Build a parentMap from HeritageMap for use with c3Linearize.
145
+ * Traverses the parent chain starting from startNodeId, collecting all
146
+ * parent→children relationships into a Map<string, string[]>.
147
+ *
148
+ * Uses a head-pointer BFS (queue[head++]) instead of Array.shift() to avoid
149
+ * O(n) per-dequeue re-indexing. For wide/shallow hierarchies common in
150
+ * large Java/C# codebases this keeps the walk linear in ancestor count.
151
+ */
152
+ const buildParentMapFromHeritage = (startNodeId, heritageMap) => {
153
+ const parentMap = new Map();
154
+ const visited = new Set();
155
+ const queue = [startNodeId];
156
+ let head = 0;
157
+ while (head < queue.length) {
158
+ const nodeId = queue[head++];
159
+ if (visited.has(nodeId))
160
+ continue;
161
+ visited.add(nodeId);
162
+ const parents = heritageMap.getParents(nodeId);
163
+ if (parents.length > 0) {
164
+ parentMap.set(nodeId, parents);
165
+ for (const p of parents) {
166
+ if (!visited.has(p))
167
+ queue.push(p);
168
+ }
169
+ }
170
+ }
171
+ return parentMap;
172
+ };
173
+ // ---------------------------------------------------------------------------
174
+ // MRO-aware method lookup
175
+ // ---------------------------------------------------------------------------
176
+ /**
177
+ * Look up a method on an owner class, walking the parent chain via HeritageMap
178
+ * when the method isn't found on the direct owner.
179
+ *
180
+ * Respects the 5 per-language MRO strategies:
181
+ * - `first-wins`: BFS ancestor walk, first match wins (default)
182
+ * - `leftmost-base`: BFS ancestor walk, leftmost base in declaration order wins (C++);
183
+ * HeritageMap preserves insertion order matching source declaration,
184
+ * so BFS order is equivalent to leftmost-base semantics
185
+ * - `c3`: C3-linearized ancestor order, first match wins (Python)
186
+ * - `implements-split`: BFS ancestor walk, first match wins (Java/C#) —
187
+ * full ambiguity detection for multiple interface defaults
188
+ * is handled by computeMRO at graph level
189
+ * - `qualified-syntax`: No auto-resolution (Rust) — returns undefined
190
+ *
191
+ * Uses the `c3Linearize` defined in this file (also consumed by
192
+ * mro-processor.ts for graph-level MRO emission) for the `c3` strategy.
193
+ *
194
+ * Depends only on {@link SemanticModel} + {@link HeritageMap} + an
195
+ * {@link MroStrategy} literal — NO dependency on SymbolTable, the language
196
+ * registry, or resolution-context, which keeps the `model/` module free of
197
+ * cross-layer imports. Callers derive the strategy from their language
198
+ * provider before invoking this function.
199
+ *
200
+ * @internal This is the low-level MRO walker. Exported so call-processor's
201
+ * higher-level resolvers (and unit tests) can invoke it directly. Callers
202
+ * outside `core/ingestion/` should use the higher-level resolvers in
203
+ * call-processor.ts instead of depending on this function.
204
+ */
205
+ export const lookupMethodByOwnerWithMRO = (ownerNodeId, methodName, heritageMap, model, strategy, argCount) => {
206
+ // Direct lookup first (child override — no walk needed).
207
+ // argCount is threaded through so arity-differing overloads on the direct
208
+ // owner can be disambiguated before the MRO walk starts.
209
+ const direct = model.methods.lookupMethodByOwner(ownerNodeId, methodName, argCount);
210
+ if (direct)
211
+ return direct;
212
+ // Rust: requires qualified syntax (<Type as Trait>::method), no auto-resolution
213
+ if (strategy === 'qualified-syntax')
214
+ return undefined;
215
+ // Determine ancestor walk order based on MRO strategy.
216
+ // readonly to accept the cached (frozen) c3 linearization without copying.
217
+ let ancestors;
218
+ if (strategy === 'c3') {
219
+ // C3 linearization (memoized per HeritageMap
220
+ // so repeated calls for the same owner within an ingestion run reuse the
221
+ // linearization instead of rebuilding the parent map and re-running C3).
222
+ // c3Linearize returns ancestors only (excludes the owner itself),
223
+ // matching heritageMap.getAncestors() semantics.
224
+ const c3Result = getCachedC3Linearization(ownerNodeId, heritageMap);
225
+ // Fall back to BFS order if C3 fails (cyclic or inconsistent hierarchy).
226
+ // Note: BFS order may not preserve Python MRO semantics in these edge
227
+ // cases, but cyclic/inconsistent hierarchies are invalid in Python anyway.
228
+ ancestors = c3Result ?? heritageMap.getAncestors(ownerNodeId);
229
+ }
230
+ else {
231
+ // first-wins, leftmost-base, implements-split: BFS order via HeritageMap
232
+ ancestors = heritageMap.getAncestors(ownerNodeId);
233
+ }
234
+ // Walk ancestors in MRO order — first match wins.
235
+ // argCount narrows overloaded ancestors the same way as the direct lookup.
236
+ for (const ancestorId of ancestors) {
237
+ const method = model.methods.lookupMethodByOwner(ancestorId, methodName, argCount);
238
+ if (method)
239
+ return method;
240
+ }
241
+ return undefined;
242
+ };