@optave/codegraph 3.11.0 → 3.11.1

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 (223) hide show
  1. package/README.md +38 -31
  2. package/dist/ast-analysis/engine.d.ts.map +1 -1
  3. package/dist/ast-analysis/engine.js +91 -60
  4. package/dist/ast-analysis/engine.js.map +1 -1
  5. package/dist/ast-analysis/visitor-utils.d.ts +3 -0
  6. package/dist/ast-analysis/visitor-utils.d.ts.map +1 -1
  7. package/dist/ast-analysis/visitor-utils.js +83 -49
  8. package/dist/ast-analysis/visitor-utils.js.map +1 -1
  9. package/dist/ast-analysis/visitors/ast-store-visitor.d.ts.map +1 -1
  10. package/dist/ast-analysis/visitors/ast-store-visitor.js +78 -62
  11. package/dist/ast-analysis/visitors/ast-store-visitor.js.map +1 -1
  12. package/dist/ast-analysis/visitors/dataflow-visitor.d.ts.map +1 -1
  13. package/dist/ast-analysis/visitors/dataflow-visitor.js +61 -42
  14. package/dist/ast-analysis/visitors/dataflow-visitor.js.map +1 -1
  15. package/dist/cli/commands/embed.d.ts.map +1 -1
  16. package/dist/cli/commands/embed.js +49 -4
  17. package/dist/cli/commands/embed.js.map +1 -1
  18. package/dist/domain/analysis/dependencies.d.ts.map +1 -1
  19. package/dist/domain/analysis/dependencies.js +106 -80
  20. package/dist/domain/analysis/dependencies.js.map +1 -1
  21. package/dist/domain/analysis/fn-impact.d.ts.map +1 -1
  22. package/dist/domain/analysis/fn-impact.js +77 -52
  23. package/dist/domain/analysis/fn-impact.js.map +1 -1
  24. package/dist/domain/analysis/module-map.d.ts.map +1 -1
  25. package/dist/domain/analysis/module-map.js +132 -121
  26. package/dist/domain/analysis/module-map.js.map +1 -1
  27. package/dist/domain/graph/builder/helpers.d.ts +4 -4
  28. package/dist/domain/graph/builder/helpers.d.ts.map +1 -1
  29. package/dist/domain/graph/builder/helpers.js +47 -33
  30. package/dist/domain/graph/builder/helpers.js.map +1 -1
  31. package/dist/domain/graph/builder/incremental.d.ts +6 -0
  32. package/dist/domain/graph/builder/incremental.d.ts.map +1 -1
  33. package/dist/domain/graph/builder/incremental.js +142 -76
  34. package/dist/domain/graph/builder/incremental.js.map +1 -1
  35. package/dist/domain/graph/builder/pipeline.d.ts +1 -44
  36. package/dist/domain/graph/builder/pipeline.d.ts.map +1 -1
  37. package/dist/domain/graph/builder/pipeline.js +10 -766
  38. package/dist/domain/graph/builder/pipeline.js.map +1 -1
  39. package/dist/domain/graph/builder/stages/build-edges.d.ts.map +1 -1
  40. package/dist/domain/graph/builder/stages/build-edges.js +133 -96
  41. package/dist/domain/graph/builder/stages/build-edges.js.map +1 -1
  42. package/dist/domain/graph/builder/stages/build-structure.d.ts.map +1 -1
  43. package/dist/domain/graph/builder/stages/build-structure.js +82 -65
  44. package/dist/domain/graph/builder/stages/build-structure.js.map +1 -1
  45. package/dist/domain/graph/builder/stages/detect-changes.d.ts.map +1 -1
  46. package/dist/domain/graph/builder/stages/detect-changes.js +84 -56
  47. package/dist/domain/graph/builder/stages/detect-changes.js.map +1 -1
  48. package/dist/domain/graph/builder/stages/finalize.d.ts.map +1 -1
  49. package/dist/domain/graph/builder/stages/finalize.js +60 -51
  50. package/dist/domain/graph/builder/stages/finalize.js.map +1 -1
  51. package/dist/domain/graph/builder/stages/insert-nodes.d.ts +8 -6
  52. package/dist/domain/graph/builder/stages/insert-nodes.d.ts.map +1 -1
  53. package/dist/domain/graph/builder/stages/insert-nodes.js +107 -122
  54. package/dist/domain/graph/builder/stages/insert-nodes.js.map +1 -1
  55. package/dist/domain/graph/builder/stages/native-db-lifecycle.d.ts +14 -0
  56. package/dist/domain/graph/builder/stages/native-db-lifecycle.d.ts.map +1 -0
  57. package/dist/domain/graph/builder/stages/native-db-lifecycle.js +77 -0
  58. package/dist/domain/graph/builder/stages/native-db-lifecycle.js.map +1 -0
  59. package/dist/domain/graph/builder/stages/native-orchestrator.d.ts +62 -0
  60. package/dist/domain/graph/builder/stages/native-orchestrator.d.ts.map +1 -0
  61. package/dist/domain/graph/builder/stages/native-orchestrator.js +747 -0
  62. package/dist/domain/graph/builder/stages/native-orchestrator.js.map +1 -0
  63. package/dist/domain/graph/cycles.d.ts +6 -4
  64. package/dist/domain/graph/cycles.d.ts.map +1 -1
  65. package/dist/domain/graph/cycles.js +50 -55
  66. package/dist/domain/graph/cycles.js.map +1 -1
  67. package/dist/domain/graph/journal.d.ts.map +1 -1
  68. package/dist/domain/graph/journal.js +89 -70
  69. package/dist/domain/graph/journal.js.map +1 -1
  70. package/dist/domain/graph/watcher.d.ts.map +1 -1
  71. package/dist/domain/graph/watcher.js +5 -2
  72. package/dist/domain/graph/watcher.js.map +1 -1
  73. package/dist/domain/parser.d.ts +12 -23
  74. package/dist/domain/parser.d.ts.map +1 -1
  75. package/dist/domain/parser.js +126 -79
  76. package/dist/domain/parser.js.map +1 -1
  77. package/dist/domain/search/generator.d.ts +3 -1
  78. package/dist/domain/search/generator.d.ts.map +1 -1
  79. package/dist/domain/search/generator.js +68 -45
  80. package/dist/domain/search/generator.js.map +1 -1
  81. package/dist/domain/search/models.d.ts +2 -0
  82. package/dist/domain/search/models.d.ts.map +1 -1
  83. package/dist/domain/search/models.js +37 -3
  84. package/dist/domain/search/models.js.map +1 -1
  85. package/dist/domain/search/search/hybrid.d.ts.map +1 -1
  86. package/dist/domain/search/search/hybrid.js +49 -40
  87. package/dist/domain/search/search/hybrid.js.map +1 -1
  88. package/dist/domain/search/search/semantic.d.ts.map +1 -1
  89. package/dist/domain/search/search/semantic.js +69 -49
  90. package/dist/domain/search/search/semantic.js.map +1 -1
  91. package/dist/domain/wasm-worker-entry.js +201 -136
  92. package/dist/domain/wasm-worker-entry.js.map +1 -1
  93. package/dist/extractors/elixir.js +95 -71
  94. package/dist/extractors/elixir.js.map +1 -1
  95. package/dist/extractors/gleam.d.ts.map +1 -1
  96. package/dist/extractors/gleam.js +23 -31
  97. package/dist/extractors/gleam.js.map +1 -1
  98. package/dist/extractors/helpers.d.ts +79 -1
  99. package/dist/extractors/helpers.d.ts.map +1 -1
  100. package/dist/extractors/helpers.js +137 -0
  101. package/dist/extractors/helpers.js.map +1 -1
  102. package/dist/extractors/java.d.ts.map +1 -1
  103. package/dist/extractors/java.js +37 -49
  104. package/dist/extractors/java.js.map +1 -1
  105. package/dist/extractors/javascript.d.ts.map +1 -1
  106. package/dist/extractors/javascript.js +44 -44
  107. package/dist/extractors/javascript.js.map +1 -1
  108. package/dist/extractors/julia.js +27 -34
  109. package/dist/extractors/julia.js.map +1 -1
  110. package/dist/extractors/r.d.ts.map +1 -1
  111. package/dist/extractors/r.js +33 -58
  112. package/dist/extractors/r.js.map +1 -1
  113. package/dist/extractors/solidity.d.ts.map +1 -1
  114. package/dist/extractors/solidity.js +38 -61
  115. package/dist/extractors/solidity.js.map +1 -1
  116. package/dist/features/boundaries.d.ts.map +1 -1
  117. package/dist/features/boundaries.js +49 -39
  118. package/dist/features/boundaries.js.map +1 -1
  119. package/dist/features/cfg.d.ts.map +1 -1
  120. package/dist/features/cfg.js +90 -63
  121. package/dist/features/cfg.js.map +1 -1
  122. package/dist/features/check.d.ts.map +1 -1
  123. package/dist/features/check.js +43 -34
  124. package/dist/features/check.js.map +1 -1
  125. package/dist/features/cochange.d.ts.map +1 -1
  126. package/dist/features/cochange.js +68 -56
  127. package/dist/features/cochange.js.map +1 -1
  128. package/dist/features/complexity.d.ts.map +1 -1
  129. package/dist/features/complexity.js +105 -75
  130. package/dist/features/complexity.js.map +1 -1
  131. package/dist/features/dataflow.d.ts.map +1 -1
  132. package/dist/features/dataflow.js +37 -29
  133. package/dist/features/dataflow.js.map +1 -1
  134. package/dist/features/flow.d.ts.map +1 -1
  135. package/dist/features/flow.js +31 -22
  136. package/dist/features/flow.js.map +1 -1
  137. package/dist/features/graph-enrichment.d.ts.map +1 -1
  138. package/dist/features/graph-enrichment.js +77 -70
  139. package/dist/features/graph-enrichment.js.map +1 -1
  140. package/dist/features/owners.d.ts +17 -26
  141. package/dist/features/owners.d.ts.map +1 -1
  142. package/dist/features/owners.js +120 -109
  143. package/dist/features/owners.js.map +1 -1
  144. package/dist/features/sequence.d.ts.map +1 -1
  145. package/dist/features/sequence.js +59 -54
  146. package/dist/features/sequence.js.map +1 -1
  147. package/dist/features/structure-query.d.ts.map +1 -1
  148. package/dist/features/structure-query.js +60 -60
  149. package/dist/features/structure-query.js.map +1 -1
  150. package/dist/features/structure.js +28 -36
  151. package/dist/features/structure.js.map +1 -1
  152. package/dist/graph/algorithms/leiden/optimiser.d.ts.map +1 -1
  153. package/dist/graph/algorithms/leiden/optimiser.js +100 -69
  154. package/dist/graph/algorithms/leiden/optimiser.js.map +1 -1
  155. package/dist/graph/classifiers/roles.d.ts.map +1 -1
  156. package/dist/graph/classifiers/roles.js +63 -59
  157. package/dist/graph/classifiers/roles.js.map +1 -1
  158. package/dist/infrastructure/config.d.ts +1 -1
  159. package/dist/infrastructure/config.d.ts.map +1 -1
  160. package/dist/infrastructure/config.js +1 -1
  161. package/dist/infrastructure/config.js.map +1 -1
  162. package/dist/presentation/cfg.d.ts.map +1 -1
  163. package/dist/presentation/cfg.js +44 -29
  164. package/dist/presentation/cfg.js.map +1 -1
  165. package/dist/presentation/flow.d.ts.map +1 -1
  166. package/dist/presentation/flow.js +58 -38
  167. package/dist/presentation/flow.js.map +1 -1
  168. package/dist/types.d.ts +1 -1
  169. package/dist/types.d.ts.map +1 -1
  170. package/package.json +7 -7
  171. package/src/ast-analysis/engine.ts +145 -61
  172. package/src/ast-analysis/visitor-utils.ts +86 -46
  173. package/src/ast-analysis/visitors/ast-store-visitor.ts +104 -69
  174. package/src/ast-analysis/visitors/dataflow-visitor.ts +86 -47
  175. package/src/cli/commands/embed.ts +54 -4
  176. package/src/domain/analysis/dependencies.ts +166 -85
  177. package/src/domain/analysis/fn-impact.ts +120 -50
  178. package/src/domain/analysis/module-map.ts +175 -140
  179. package/src/domain/graph/builder/helpers.ts +85 -76
  180. package/src/domain/graph/builder/incremental.ts +217 -90
  181. package/src/domain/graph/builder/pipeline.ts +19 -957
  182. package/src/domain/graph/builder/stages/build-edges.ts +198 -140
  183. package/src/domain/graph/builder/stages/build-structure.ts +115 -82
  184. package/src/domain/graph/builder/stages/detect-changes.ts +107 -64
  185. package/src/domain/graph/builder/stages/finalize.ts +72 -70
  186. package/src/domain/graph/builder/stages/insert-nodes.ts +154 -120
  187. package/src/domain/graph/builder/stages/native-db-lifecycle.ts +74 -0
  188. package/src/domain/graph/builder/stages/native-orchestrator.ts +942 -0
  189. package/src/domain/graph/cycles.ts +51 -49
  190. package/src/domain/graph/journal.ts +84 -69
  191. package/src/domain/graph/watcher.ts +8 -2
  192. package/src/domain/parser.ts +143 -66
  193. package/src/domain/search/generator.ts +132 -74
  194. package/src/domain/search/models.ts +39 -3
  195. package/src/domain/search/search/hybrid.ts +53 -42
  196. package/src/domain/search/search/semantic.ts +105 -65
  197. package/src/domain/wasm-worker-entry.ts +235 -152
  198. package/src/extractors/elixir.ts +91 -64
  199. package/src/extractors/gleam.ts +33 -37
  200. package/src/extractors/helpers.ts +205 -1
  201. package/src/extractors/java.ts +42 -45
  202. package/src/extractors/javascript.ts +44 -43
  203. package/src/extractors/julia.ts +28 -35
  204. package/src/extractors/r.ts +38 -56
  205. package/src/extractors/solidity.ts +43 -71
  206. package/src/features/boundaries.ts +64 -46
  207. package/src/features/cfg.ts +145 -74
  208. package/src/features/check.ts +60 -43
  209. package/src/features/cochange.ts +95 -72
  210. package/src/features/complexity.ts +134 -79
  211. package/src/features/dataflow.ts +57 -34
  212. package/src/features/flow.ts +48 -24
  213. package/src/features/graph-enrichment.ts +105 -70
  214. package/src/features/owners.ts +186 -146
  215. package/src/features/sequence.ts +99 -69
  216. package/src/features/structure-query.ts +94 -79
  217. package/src/features/structure.ts +56 -56
  218. package/src/graph/algorithms/leiden/optimiser.ts +142 -87
  219. package/src/graph/classifiers/roles.ts +64 -54
  220. package/src/infrastructure/config.ts +1 -1
  221. package/src/presentation/cfg.ts +48 -32
  222. package/src/presentation/flow.ts +100 -52
  223. package/src/types.ts +1 -1
@@ -88,78 +88,118 @@ export function extractParams(
88
88
  return result;
89
89
  }
90
90
 
91
- /** Extract names from a rest parameter (e.g. `...args`). */
92
- function extractRestParamNames(node: TreeSitterNode, rules: LanguageRules): string[] {
93
- const nameNode = node.childForFieldName('name');
94
- if (nameNode) return [nameNode.text];
95
- for (const child of node.namedChildren) {
96
- if (child.type === rules.paramIdentifier) return [child.text];
97
- }
98
- return [];
99
- }
100
-
101
- /** Extract names from an object destructuring pattern (e.g. `{ a, b: c }`). */
102
- function extractObjectDestructNames(node: TreeSitterNode, rules: LanguageRules): string[] {
103
- const names: string[] = [];
104
- for (const child of node.namedChildren) {
105
- if (rules.shorthandPropPattern && child.type === rules.shorthandPropPattern) {
106
- names.push(child.text);
107
- } else if (rules.pairPatternType && child.type === rules.pairPatternType) {
108
- const value = child.childForFieldName('value');
109
- if (value) names.push(...extractParamNames(value, rules));
110
- } else if (rules.restParamType && child.type === rules.restParamType) {
111
- names.push(...extractParamNames(child, rules));
112
- }
113
- }
114
- return names;
115
- }
116
-
117
- /** Extract names from an array destructuring pattern (e.g. `[a, b]`). */
118
- function extractArrayDestructNames(node: TreeSitterNode, rules: LanguageRules): string[] {
119
- const names: string[] = [];
120
- for (const child of node.namedChildren) {
121
- names.push(...extractParamNames(child, rules));
122
- }
123
- return names;
124
- }
125
-
126
91
  /**
127
- * Extract parameter names from a single parameter node.
92
+ * Resolve a single parameter node to either a direct list of names (base case)
93
+ * or a list of child nodes that still need processing. Returns `null` if the
94
+ * node yields nothing.
95
+ *
96
+ * This base case keeps destructuring helpers from recursing back into
97
+ * `extractParamNames`, breaking the 3-node mutual recursion cycle between
98
+ * `extractParamNames`, `extractObjectDestructNames`, and `extractArrayDestructNames`.
128
99
  */
129
- export function extractParamNames(node: TreeSitterNode | null, rules: LanguageRules): string[] {
130
- if (!node) return [];
100
+ function resolveParamNode(
101
+ node: TreeSitterNode,
102
+ rules: LanguageRules,
103
+ ): { names?: string[]; next?: TreeSitterNode[] } | null {
131
104
  const t = node.type;
132
105
 
133
106
  if (rules.extractParamName) {
134
107
  const result = rules.extractParamName(node);
135
- if (result) return result;
108
+ if (result) return { names: result };
136
109
  }
137
110
 
138
- if (t === rules.paramIdentifier) return [node.text];
111
+ if (t === rules.paramIdentifier) return { names: [node.text] };
139
112
 
140
113
  if (rules.paramWrapperTypes.has(t)) {
141
114
  const pattern = node.childForFieldName('pattern') || node.childForFieldName('name');
142
- return pattern ? extractParamNames(pattern, rules) : [];
115
+ return pattern ? { next: [pattern] } : null;
143
116
  }
144
117
 
145
118
  if (rules.defaultParamType && t === rules.defaultParamType) {
146
119
  const left = node.childForFieldName('left') || node.childForFieldName('name');
147
- return left ? extractParamNames(left, rules) : [];
120
+ return left ? { next: [left] } : null;
148
121
  }
149
122
 
150
123
  if (rules.restParamType && t === rules.restParamType) {
151
- return extractRestParamNames(node, rules);
124
+ const nameNode = node.childForFieldName('name');
125
+ if (nameNode) return { names: [nameNode.text] };
126
+ for (const child of node.namedChildren) {
127
+ if (child.type === rules.paramIdentifier) return { names: [child.text] };
128
+ }
129
+ return null;
152
130
  }
153
131
 
154
132
  if (rules.objectDestructType && t === rules.objectDestructType) {
155
- return extractObjectDestructNames(node, rules);
133
+ return { next: collectObjectDestructChildren(node, rules) };
156
134
  }
157
135
 
158
136
  if (rules.arrayDestructType && t === rules.arrayDestructType) {
159
- return extractArrayDestructNames(node, rules);
137
+ return { next: [...node.namedChildren] };
138
+ }
139
+
140
+ return null;
141
+ }
142
+
143
+ /**
144
+ * Collect child nodes from an object destructuring pattern that should be
145
+ * processed for further name extraction. Returns nodes (not names) so the
146
+ * caller drives traversal via a worklist instead of recursion.
147
+ */
148
+ function collectObjectDestructChildren(
149
+ node: TreeSitterNode,
150
+ rules: LanguageRules,
151
+ ): TreeSitterNode[] {
152
+ const next: TreeSitterNode[] = [];
153
+ for (const child of node.namedChildren) {
154
+ if (rules.shorthandPropPattern && child.type === rules.shorthandPropPattern) {
155
+ // Shorthand prop is a direct identifier — handled by the shorthand
156
+ // guard in the `extractParamNames` worklist loop (before `resolveParamNode`).
157
+ next.push(child);
158
+ } else if (rules.pairPatternType && child.type === rules.pairPatternType) {
159
+ const value = child.childForFieldName('value');
160
+ if (value) next.push(value);
161
+ } else if (rules.restParamType && child.type === rules.restParamType) {
162
+ next.push(child);
163
+ }
160
164
  }
165
+ return next;
166
+ }
161
167
 
162
- return [];
168
+ /**
169
+ * Extract parameter names from a single parameter node.
170
+ *
171
+ * Uses an iterative worklist to handle nested destructuring (objects, arrays,
172
+ * defaults, rest, wrappers) without mutual recursion through helper functions.
173
+ */
174
+ export function extractParamNames(node: TreeSitterNode | null, rules: LanguageRules): string[] {
175
+ if (!node) return [];
176
+
177
+ const names: string[] = [];
178
+ const stack: TreeSitterNode[] = [node];
179
+
180
+ while (stack.length > 0) {
181
+ const current = stack.pop();
182
+ if (!current) continue;
183
+
184
+ // Shorthand identifier inside an object destructuring is just the node's text.
185
+ if (rules.shorthandPropPattern && current.type === rules.shorthandPropPattern) {
186
+ names.push(current.text);
187
+ continue;
188
+ }
189
+
190
+ const resolved = resolveParamNode(current, rules);
191
+ if (!resolved) continue;
192
+ if (resolved.names) names.push(...resolved.names);
193
+ if (resolved.next) {
194
+ // Push in reverse so traversal order matches the previous recursive order.
195
+ for (let i = resolved.next.length - 1; i >= 0; i--) {
196
+ const child = resolved.next[i];
197
+ if (child) stack.push(child);
198
+ }
199
+ }
200
+ }
201
+
202
+ return names;
163
203
  }
164
204
 
165
205
  /**
@@ -181,46 +181,17 @@ function newTypesFor(astTypeMap: Record<string, string>): Set<string> {
181
181
  return s;
182
182
  }
183
183
 
184
- export function createAstStoreVisitor(
185
- astTypeMap: Record<string, string>,
186
- defs: Definition[],
187
- relPath: string,
188
- nodeIdMap: Map<string, number>,
189
- stringConfig: AstStringConfig = DEFAULT_STRING_CONFIG,
190
- stopRecurseKinds: ReadonlySet<string> = new Set(),
191
- ): Visitor {
192
- const rows: AstStoreRow[] = [];
193
- const matched = new Set<number>();
194
- const newTypes = newTypesFor(astTypeMap);
195
- // When nodeIdMap is empty, parentNodeId resolution is wasted work — the
196
- // worker passes an empty map and the main thread re-resolves against its
197
- // own DB-populated map in features/ast.ts::collectFileAstRows. Skip the
198
- // findParentDef linear scan in that case.
199
- const skipParentLookup = nodeIdMap.size === 0;
200
-
201
- function findParentDef(line: number): Definition | null {
202
- let best: Definition | null = null;
203
- for (const def of defs) {
204
- if (def.line <= line && (def.endLine == null || def.endLine >= line)) {
205
- if (!best || (def.endLine ?? 0) - def.line < (best.endLine ?? 0) - best.line) {
206
- best = def;
207
- }
208
- }
209
- }
210
- return best;
211
- }
212
-
213
- function resolveParentNodeId(line: number): number | null {
214
- if (skipParentLookup) return null;
215
- const parentDef = findParentDef(line);
216
- if (!parentDef) return null;
217
- return nodeIdMap.get(`${parentDef.name}|${parentDef.kind}|${parentDef.line}`) || null;
218
- }
184
+ type NameTextResult = { name: string | null | undefined; text: string | null; skip?: boolean };
185
+ type KindHandler = (node: TreeSitterNode) => NameTextResult;
219
186
 
220
- type NameTextResult = { name: string | null | undefined; text: string | null; skip?: boolean };
221
- type KindHandler = (node: TreeSitterNode) => NameTextResult;
187
+ const DEFAULT_NAME_TEXT_RESULT: NameTextResult = { name: undefined, text: null };
222
188
 
223
- const kindHandlers: Record<string, KindHandler> = {
189
+ /** Build the per-kind resolver map for name/text extraction. */
190
+ function buildKindHandlers(
191
+ newTypes: Set<string>,
192
+ stringConfig: AstStringConfig,
193
+ ): Record<string, KindHandler> {
194
+ return {
224
195
  new: (node) => ({ name: extractConstructorName(node), text: truncate(node.text) }),
225
196
  throw: (node) => ({
226
197
  name: extractThrowName(node, newTypes),
@@ -234,31 +205,102 @@ export function createAstStoreVisitor(
234
205
  },
235
206
  regex: (node) => ({ name: node.text || '?', text: truncate(node.text) }),
236
207
  };
237
- const defaultResult: NameTextResult = { name: undefined, text: null };
208
+ }
238
209
 
239
- function resolveNameAndText(node: TreeSitterNode, kind: string): NameTextResult {
240
- const handler = kindHandlers[kind];
241
- return handler ? handler(node) : defaultResult;
210
+ /** Find the innermost definition whose line range contains `line`. */
211
+ function findParentDef(line: number, defs: Definition[]): Definition | null {
212
+ let best: Definition | null = null;
213
+ for (const def of defs) {
214
+ if (def.line <= line && (def.endLine == null || def.endLine >= line)) {
215
+ if (!best || (def.endLine ?? 0) - def.line < (best.endLine ?? 0) - best.line) {
216
+ best = def;
217
+ }
218
+ }
242
219
  }
220
+ return best;
221
+ }
222
+
223
+ /** Resolve the parent definition's node id for a given source line. */
224
+ function resolveParentNodeId(
225
+ line: number,
226
+ defs: Definition[],
227
+ nodeIdMap: Map<string, number>,
228
+ skipParentLookup: boolean,
229
+ ): number | null {
230
+ if (skipParentLookup) return null;
231
+ const parentDef = findParentDef(line, defs);
232
+ if (!parentDef) return null;
233
+ return nodeIdMap.get(`${parentDef.name}|${parentDef.kind}|${parentDef.line}`) || null;
234
+ }
243
235
 
244
- function collectNode(node: TreeSitterNode, kind: string): void {
245
- if (matched.has(node.id)) return;
236
+ interface CollectCtx {
237
+ rows: AstStoreRow[];
238
+ matched: Set<number>;
239
+ relPath: string;
240
+ defs: Definition[];
241
+ nodeIdMap: Map<string, number>;
242
+ skipParentLookup: boolean;
243
+ kindHandlers: Record<string, KindHandler>;
244
+ }
246
245
 
247
- const resolved = resolveNameAndText(node, kind);
248
- if (resolved.skip) return;
246
+ function collectNode(ctx: CollectCtx, node: TreeSitterNode, kind: string): void {
247
+ if (ctx.matched.has(node.id)) return;
248
+
249
+ const handler = ctx.kindHandlers[kind];
250
+ const resolved = handler ? handler(node) : DEFAULT_NAME_TEXT_RESULT;
251
+ if (resolved.skip) return;
252
+
253
+ const line = node.startPosition.row + 1;
254
+ ctx.rows.push({
255
+ file: ctx.relPath,
256
+ line,
257
+ kind,
258
+ name: resolved.name,
259
+ text: resolved.text,
260
+ receiver: null,
261
+ parentNodeId: resolveParentNodeId(line, ctx.defs, ctx.nodeIdMap, ctx.skipParentLookup),
262
+ });
263
+
264
+ ctx.matched.add(node.id);
265
+ }
249
266
 
250
- rows.push({
251
- file: relPath,
252
- line: node.startPosition.row + 1,
253
- kind,
254
- name: resolved.name,
255
- text: resolved.text,
256
- receiver: null,
257
- parentNodeId: resolveParentNodeId(node.startPosition.row + 1),
258
- });
267
+ /**
268
+ * Resolve the kind for a tree-sitter node, or `null` if the node should be ignored.
269
+ *
270
+ * Gate with `hasOwn` because plain-object lookup walks Object.prototype:
271
+ * tree-sitter node types like `constructor` (Haskell sum-types: Left,
272
+ * Right) would otherwise resolve to `Object.prototype.constructor` (the
273
+ * Object() function), which then crashes the worker boundary with
274
+ * "function Object() { [native code] } could not be cloned" when the
275
+ * resulting astNodes row is structured-cloned back to the main thread.
276
+ */
277
+ function resolveAstKind(node: TreeSitterNode, astTypeMap: Record<string, string>): string | null {
278
+ if (!Object.hasOwn(astTypeMap, node.type)) return null;
279
+ return astTypeMap[node.type] || null;
280
+ }
259
281
 
260
- matched.add(node.id);
261
- }
282
+ export function createAstStoreVisitor(
283
+ astTypeMap: Record<string, string>,
284
+ defs: Definition[],
285
+ relPath: string,
286
+ nodeIdMap: Map<string, number>,
287
+ stringConfig: AstStringConfig = DEFAULT_STRING_CONFIG,
288
+ stopRecurseKinds: ReadonlySet<string> = new Set(),
289
+ ): Visitor {
290
+ const newTypes = newTypesFor(astTypeMap);
291
+ // When nodeIdMap is empty, parentNodeId resolution is wasted work — the
292
+ // worker passes an empty map and the main thread re-resolves against its
293
+ // own DB-populated map in features/ast.ts::collectFileAstRows. Skip the
294
+ // findParentDef linear scan in that case.
295
+ const ctx: CollectCtx = {
296
+ rows: [],
297
+ matched: new Set<number>(),
298
+ relPath,
299
+ defs,
300
+ nodeIdMap,
301
+ skipParentLookup: nodeIdMap.size === 0,
302
+ kindHandlers: buildKindHandlers(newTypes, stringConfig),
303
+ };
262
304
 
263
305
  return {
264
306
  name: 'ast-store',
@@ -267,19 +309,12 @@ export function createAstStoreVisitor(
267
309
  // Guard: skip re-collection but do NOT skipChildren — node.id (memory address)
268
310
  // can be reused by tree-sitter, so a collision would incorrectly suppress an
269
311
  // unrelated subtree. The parent call's skipChildren handles the intended case.
270
- if (matched.has(node.id)) return;
271
-
272
- // Gate with `hasOwn` because plain-object lookup walks Object.prototype:
273
- // tree-sitter node types like `constructor` (Haskell sum-types: Left,
274
- // Right) would otherwise resolve to `Object.prototype.constructor` (the
275
- // Object() function), which then crashes the worker boundary with
276
- // "function Object() { [native code] } could not be cloned" when the
277
- // resulting astNodes row is structured-cloned back to the main thread.
278
- if (!Object.hasOwn(astTypeMap, node.type)) return;
279
- const kind = astTypeMap[node.type];
312
+ if (ctx.matched.has(node.id)) return;
313
+
314
+ const kind = resolveAstKind(node, astTypeMap);
280
315
  if (!kind) return;
281
316
 
282
- collectNode(node, kind);
317
+ collectNode(ctx, node, kind);
283
318
 
284
319
  // Mirror the native walker's recursion policy. In JS/TS, the native
285
320
  // javascript.rs walker returns after collecting `new` or `throw` to
@@ -293,7 +328,7 @@ export function createAstStoreVisitor(
293
328
  },
294
329
 
295
330
  finish(): AstStoreRow[] {
296
- return rows;
331
+ return ctx.rows;
297
332
  },
298
333
  };
299
334
  }
@@ -405,6 +405,79 @@ function handleReturn(
405
405
  }
406
406
  }
407
407
 
408
+ /** Collect parameter entries for a function and push a new scope onto the stack. */
409
+ function enterFunctionScope(
410
+ funcNode: TreeSitterNode,
411
+ rules: AnyRules,
412
+ scopeStack: ScopeEntry[],
413
+ parameters: DataflowParam[],
414
+ ): void {
415
+ const name = functionName(funcNode, rules);
416
+ const paramsNode = funcNode.childForFieldName(rules.paramListField);
417
+ const paramList = extractParams(paramsNode, rules);
418
+ const paramMap = new Map<string, number>();
419
+ for (const p of paramList) {
420
+ paramMap.set(p.name, p.index);
421
+ if (name) {
422
+ parameters.push({
423
+ funcName: name,
424
+ paramName: p.name,
425
+ paramIndex: p.index,
426
+ line: (paramsNode?.startPosition?.row ?? funcNode.startPosition.row) + 1,
427
+ });
428
+ }
429
+ }
430
+ scopeStack.push({ funcName: name, funcNode, params: paramMap, locals: new Map() });
431
+ }
432
+
433
+ interface DataflowDispatchCtx {
434
+ rules: AnyRules;
435
+ scopeStack: ScopeEntry[];
436
+ returns: DataflowReturnEntry[];
437
+ assignments: DataflowAssignment[];
438
+ argFlows: DataflowArgFlow[];
439
+ mutations: DataflowMutation[];
440
+ isCallNode: (t: string) => boolean;
441
+ }
442
+
443
+ /**
444
+ * Route a node to the appropriate dataflow handler based on its type.
445
+ * Function-definition nodes are short-circuited with an early return.
446
+ */
447
+ function dispatchDataflowNode(ctx: DataflowDispatchCtx, node: TreeSitterNode): void {
448
+ const { rules } = ctx;
449
+ const t = node.type;
450
+
451
+ if (rules.functionNodes.has(t)) return;
452
+
453
+ if (rules.returnNode && t === rules.returnNode) {
454
+ handleReturn(node, rules, ctx.scopeStack, ctx.returns);
455
+ return;
456
+ }
457
+
458
+ if (
459
+ (rules.varDeclaratorNode && t === rules.varDeclaratorNode) ||
460
+ rules.varDeclaratorNodes?.has(t)
461
+ ) {
462
+ handleVarDeclarator(node, rules, ctx.scopeStack, ctx.assignments, ctx.isCallNode);
463
+ return;
464
+ }
465
+
466
+ if (ctx.isCallNode(t)) {
467
+ handleCallExpr(node, rules, ctx.scopeStack, ctx.argFlows);
468
+ return;
469
+ }
470
+
471
+ if (rules.assignmentNode && t === rules.assignmentNode) {
472
+ handleAssignment(node, rules, ctx.scopeStack, ctx.assignments, ctx.mutations, ctx.isCallNode);
473
+ return;
474
+ }
475
+
476
+ if (rules.expressionStmtNode && t === rules.expressionStmtNode) {
477
+ handleExprStmtMutation(node, rules, ctx.scopeStack, ctx.mutations, ctx.isCallNode);
478
+ }
479
+ }
480
+
408
481
  export function createDataflowVisitor(rules: AnyRules): Visitor {
409
482
  const isCallNode: (t: string) => boolean = rules.callNodes
410
483
  ? (t: string) => rules.callNodes.has(t)
@@ -417,6 +490,16 @@ export function createDataflowVisitor(rules: AnyRules): Visitor {
417
490
  const mutations: DataflowMutation[] = [];
418
491
  const scopeStack: ScopeEntry[] = [];
419
492
 
493
+ const dispatchCtx: DataflowDispatchCtx = {
494
+ rules,
495
+ scopeStack,
496
+ returns,
497
+ assignments,
498
+ argFlows,
499
+ mutations,
500
+ isCallNode,
501
+ };
502
+
420
503
  return {
421
504
  name: 'dataflow',
422
505
  functionNodeTypes: rules.functionNodes,
@@ -426,22 +509,7 @@ export function createDataflowVisitor(rules: AnyRules): Visitor {
426
509
  _funcName: string | null,
427
510
  _context: VisitorContext,
428
511
  ): void {
429
- const name = functionName(funcNode, rules);
430
- const paramsNode = funcNode.childForFieldName(rules.paramListField);
431
- const paramList = extractParams(paramsNode, rules);
432
- const paramMap = new Map<string, number>();
433
- for (const p of paramList) {
434
- paramMap.set(p.name, p.index);
435
- if (name) {
436
- parameters.push({
437
- funcName: name,
438
- paramName: p.name,
439
- paramIndex: p.index,
440
- line: (paramsNode?.startPosition?.row ?? funcNode.startPosition.row) + 1,
441
- });
442
- }
443
- }
444
- scopeStack.push({ funcName: name, funcNode, params: paramMap, locals: new Map() });
512
+ enterFunctionScope(funcNode, rules, scopeStack, parameters);
445
513
  },
446
514
 
447
515
  exitFunction(
@@ -453,37 +521,8 @@ export function createDataflowVisitor(rules: AnyRules): Visitor {
453
521
  },
454
522
 
455
523
  enterNode(node: TreeSitterNode, _context: VisitorContext): EnterNodeResult | undefined {
456
- const t = node.type;
457
-
458
- if (rules.functionNodes.has(t)) return;
459
-
460
- if (rules.returnNode && t === rules.returnNode) {
461
- handleReturn(node, rules, scopeStack, returns);
462
- return;
463
- }
464
-
465
- if (rules.varDeclaratorNode && t === rules.varDeclaratorNode) {
466
- handleVarDeclarator(node, rules, scopeStack, assignments, isCallNode);
467
- return;
468
- }
469
- if (rules.varDeclaratorNodes?.has(t)) {
470
- handleVarDeclarator(node, rules, scopeStack, assignments, isCallNode);
471
- return;
472
- }
473
-
474
- if (isCallNode(t)) {
475
- handleCallExpr(node, rules, scopeStack, argFlows);
476
- return;
477
- }
478
-
479
- if (rules.assignmentNode && t === rules.assignmentNode) {
480
- handleAssignment(node, rules, scopeStack, assignments, mutations, isCallNode);
481
- return;
482
- }
483
-
484
- if (rules.expressionStmtNode && t === rules.expressionStmtNode) {
485
- handleExprStmtMutation(node, rules, scopeStack, mutations, isCallNode);
486
- }
524
+ dispatchDataflowNode(dispatchCtx, node);
525
+ return undefined;
487
526
  },
488
527
 
489
528
  finish(): DataflowResultInternal {
@@ -1,7 +1,37 @@
1
1
  import path from 'node:path';
2
- import { buildEmbeddings, DEFAULT_MODEL, EMBEDDING_STRATEGIES } from '../../domain/search/index.js';
2
+ import { openReadonlyOrFail } from '../../db/index.js';
3
+ import { getEmbeddingMeta } from '../../db/repository/embeddings.js';
4
+ import {
5
+ buildEmbeddings,
6
+ DEFAULT_MODEL,
7
+ EMBEDDING_STRATEGIES,
8
+ MODELS,
9
+ } from '../../domain/search/index.js';
10
+ import { info, warn } from '../../infrastructure/logger.js';
3
11
  import type { CommandDefinition } from '../types.js';
4
12
 
13
+ function resolveStickyModel(dbPath: string | undefined): string | null {
14
+ try {
15
+ const db = openReadonlyOrFail(dbPath);
16
+ try {
17
+ const storedName = getEmbeddingMeta(db, 'model');
18
+ if (!storedName) return null;
19
+ for (const [key, cfg] of Object.entries(MODELS)) {
20
+ if (cfg.name === storedName) return key;
21
+ }
22
+ warn(
23
+ `Stored embedding model "${storedName}" is no longer recognised — falling back to default. ` +
24
+ 'Embeddings will be rebuilt with the new model.',
25
+ );
26
+ return null;
27
+ } finally {
28
+ db.close();
29
+ }
30
+ } catch {
31
+ return null;
32
+ }
33
+ }
34
+
5
35
  export const command: CommandDefinition = {
6
36
  name: 'embed [dir]',
7
37
  description:
@@ -9,7 +39,7 @@ export const command: CommandDefinition = {
9
39
  options: [
10
40
  [
11
41
  '-m, --model <name>',
12
- 'Embedding model (default from config or minilm). Run `codegraph models` for details',
42
+ 'Embedding model. Defaults to config, then the model used by existing embeddings, then the built-in default. Run `codegraph models` for options',
13
43
  ],
14
44
  [
15
45
  '-s, --strategy <name>',
@@ -25,8 +55,28 @@ export const command: CommandDefinition = {
25
55
  },
26
56
  async execute([dir], opts, ctx) {
27
57
  const root = path.resolve(dir || '.');
58
+ const dbPath = opts.db as string | undefined;
28
59
  const embeddingsConfig = ctx.config.embeddings;
29
- const model = (opts.model as string) || (embeddingsConfig?.model as string) || DEFAULT_MODEL;
30
- await buildEmbeddings(root, model, opts.db as string | undefined, { strategy: opts.strategy });
60
+ const flagModel = opts.model as string | undefined;
61
+ const configModel = (embeddingsConfig?.model as string | null | undefined) ?? null;
62
+
63
+ let model: string;
64
+ if (flagModel) {
65
+ model = flagModel;
66
+ } else if (configModel) {
67
+ model = configModel;
68
+ } else {
69
+ const sticky = resolveStickyModel(dbPath);
70
+ if (sticky) {
71
+ info(
72
+ `Reusing previously-stored embedding model "${sticky}". Pass --model to switch, or set embeddings.model in your config.`,
73
+ );
74
+ model = sticky;
75
+ } else {
76
+ model = DEFAULT_MODEL;
77
+ }
78
+ }
79
+
80
+ await buildEmbeddings(root, model, dbPath, { strategy: opts.strategy });
31
81
  },
32
82
  };