@optave/codegraph 3.13.0 → 3.15.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 (458) hide show
  1. package/README.md +35 -34
  2. package/dist/ast-analysis/engine.d.ts.map +1 -1
  3. package/dist/ast-analysis/engine.js +38 -40
  4. package/dist/ast-analysis/engine.js.map +1 -1
  5. package/dist/ast-analysis/rules/b2.d.ts +7 -0
  6. package/dist/ast-analysis/rules/b2.d.ts.map +1 -0
  7. package/dist/ast-analysis/rules/b2.js +240 -0
  8. package/dist/ast-analysis/rules/b2.js.map +1 -0
  9. package/dist/ast-analysis/rules/b3.d.ts +6 -0
  10. package/dist/ast-analysis/rules/b3.d.ts.map +1 -0
  11. package/dist/ast-analysis/rules/b3.js +105 -0
  12. package/dist/ast-analysis/rules/b3.js.map +1 -0
  13. package/dist/ast-analysis/rules/b4.d.ts +9 -0
  14. package/dist/ast-analysis/rules/b4.d.ts.map +1 -0
  15. package/dist/ast-analysis/rules/b4.js +361 -0
  16. package/dist/ast-analysis/rules/b4.js.map +1 -0
  17. package/dist/ast-analysis/rules/b5.d.ts +4 -0
  18. package/dist/ast-analysis/rules/b5.d.ts.map +1 -0
  19. package/dist/ast-analysis/rules/b5.js +52 -0
  20. package/dist/ast-analysis/rules/b5.js.map +1 -0
  21. package/dist/ast-analysis/rules/c.d.ts +4 -0
  22. package/dist/ast-analysis/rules/c.d.ts.map +1 -0
  23. package/dist/ast-analysis/rules/c.js +143 -0
  24. package/dist/ast-analysis/rules/c.js.map +1 -0
  25. package/dist/ast-analysis/rules/index.d.ts.map +1 -1
  26. package/dist/ast-analysis/rules/index.js +34 -0
  27. package/dist/ast-analysis/rules/index.js.map +1 -1
  28. package/dist/ast-analysis/rules/javascript.d.ts.map +1 -1
  29. package/dist/ast-analysis/rules/javascript.js +3 -0
  30. package/dist/ast-analysis/rules/javascript.js.map +1 -1
  31. package/dist/ast-analysis/shared.d.ts.map +1 -1
  32. package/dist/ast-analysis/shared.js +2 -0
  33. package/dist/ast-analysis/shared.js.map +1 -1
  34. package/dist/ast-analysis/visitor-utils.d.ts +1 -0
  35. package/dist/ast-analysis/visitor-utils.d.ts.map +1 -1
  36. package/dist/ast-analysis/visitor-utils.js +5 -0
  37. package/dist/ast-analysis/visitor-utils.js.map +1 -1
  38. package/dist/ast-analysis/visitor.d.ts.map +1 -1
  39. package/dist/ast-analysis/visitor.js +60 -47
  40. package/dist/ast-analysis/visitor.js.map +1 -1
  41. package/dist/ast-analysis/visitors/cfg-visitor.d.ts.map +1 -1
  42. package/dist/ast-analysis/visitors/cfg-visitor.js +126 -76
  43. package/dist/ast-analysis/visitors/cfg-visitor.js.map +1 -1
  44. package/dist/ast-analysis/visitors/complexity-visitor.d.ts.map +1 -1
  45. package/dist/ast-analysis/visitors/complexity-visitor.js +27 -15
  46. package/dist/ast-analysis/visitors/complexity-visitor.js.map +1 -1
  47. package/dist/ast-analysis/visitors/dataflow-visitor.d.ts.map +1 -1
  48. package/dist/ast-analysis/visitors/dataflow-visitor.js +54 -21
  49. package/dist/ast-analysis/visitors/dataflow-visitor.js.map +1 -1
  50. package/dist/cli/commands/config.d.ts.map +1 -1
  51. package/dist/cli/commands/config.js +137 -134
  52. package/dist/cli/commands/config.js.map +1 -1
  53. package/dist/cli/commands/roles.d.ts.map +1 -1
  54. package/dist/cli/commands/roles.js +6 -1
  55. package/dist/cli/commands/roles.js.map +1 -1
  56. package/dist/db/better-sqlite3.d.ts +2 -1
  57. package/dist/db/better-sqlite3.d.ts.map +1 -1
  58. package/dist/db/better-sqlite3.js.map +1 -1
  59. package/dist/db/connection.d.ts +7 -1
  60. package/dist/db/connection.d.ts.map +1 -1
  61. package/dist/db/connection.js +20 -5
  62. package/dist/db/connection.js.map +1 -1
  63. package/dist/db/index.d.ts +1 -1
  64. package/dist/db/index.d.ts.map +1 -1
  65. package/dist/db/index.js +1 -1
  66. package/dist/db/index.js.map +1 -1
  67. package/dist/db/migrations.d.ts.map +1 -1
  68. package/dist/db/migrations.js +68 -0
  69. package/dist/db/migrations.js.map +1 -1
  70. package/dist/db/repository/build-stmts.d.ts.map +1 -1
  71. package/dist/db/repository/build-stmts.js +18 -0
  72. package/dist/db/repository/build-stmts.js.map +1 -1
  73. package/dist/db/repository/dataflow.d.ts +5 -0
  74. package/dist/db/repository/dataflow.d.ts.map +1 -1
  75. package/dist/db/repository/dataflow.js +14 -0
  76. package/dist/db/repository/dataflow.js.map +1 -1
  77. package/dist/db/repository/index.d.ts +1 -1
  78. package/dist/db/repository/index.d.ts.map +1 -1
  79. package/dist/db/repository/index.js +1 -1
  80. package/dist/db/repository/index.js.map +1 -1
  81. package/dist/db/repository/native-repository.d.ts.map +1 -1
  82. package/dist/db/repository/native-repository.js +47 -34
  83. package/dist/db/repository/native-repository.js.map +1 -1
  84. package/dist/domain/analysis/context.d.ts +2 -2
  85. package/dist/domain/analysis/dependencies.d.ts +2 -2
  86. package/dist/domain/analysis/diff-impact.d.ts +2 -2
  87. package/dist/domain/analysis/fn-impact.d.ts +3 -1
  88. package/dist/domain/analysis/fn-impact.d.ts.map +1 -1
  89. package/dist/domain/analysis/fn-impact.js +4 -0
  90. package/dist/domain/analysis/fn-impact.js.map +1 -1
  91. package/dist/domain/analysis/implementations.d.ts +2 -2
  92. package/dist/domain/analysis/module-map.d.ts.map +1 -1
  93. package/dist/domain/analysis/module-map.js +32 -5
  94. package/dist/domain/analysis/module-map.js.map +1 -1
  95. package/dist/domain/analysis/roles.d.ts +7 -1
  96. package/dist/domain/analysis/roles.d.ts.map +1 -1
  97. package/dist/domain/analysis/roles.js +16 -0
  98. package/dist/domain/analysis/roles.js.map +1 -1
  99. package/dist/domain/analysis/symbol-lookup.d.ts +4 -4
  100. package/dist/domain/graph/builder/call-resolver.d.ts +17 -5
  101. package/dist/domain/graph/builder/call-resolver.d.ts.map +1 -1
  102. package/dist/domain/graph/builder/call-resolver.js +85 -220
  103. package/dist/domain/graph/builder/call-resolver.js.map +1 -1
  104. package/dist/domain/graph/builder/context.d.ts +1 -0
  105. package/dist/domain/graph/builder/context.d.ts.map +1 -1
  106. package/dist/domain/graph/builder/context.js.map +1 -1
  107. package/dist/domain/graph/builder/helpers.d.ts +16 -1
  108. package/dist/domain/graph/builder/helpers.d.ts.map +1 -1
  109. package/dist/domain/graph/builder/helpers.js +162 -72
  110. package/dist/domain/graph/builder/helpers.js.map +1 -1
  111. package/dist/domain/graph/builder/incremental.d.ts.map +1 -1
  112. package/dist/domain/graph/builder/incremental.js +166 -97
  113. package/dist/domain/graph/builder/incremental.js.map +1 -1
  114. package/dist/domain/graph/builder/pipeline.d.ts.map +1 -1
  115. package/dist/domain/graph/builder/pipeline.js +10 -4
  116. package/dist/domain/graph/builder/pipeline.js.map +1 -1
  117. package/dist/domain/graph/builder/stages/build-edges.d.ts.map +1 -1
  118. package/dist/domain/graph/builder/stages/build-edges.js +496 -250
  119. package/dist/domain/graph/builder/stages/build-edges.js.map +1 -1
  120. package/dist/domain/graph/builder/stages/collect-files.d.ts.map +1 -1
  121. package/dist/domain/graph/builder/stages/collect-files.js +10 -7
  122. package/dist/domain/graph/builder/stages/collect-files.js.map +1 -1
  123. package/dist/domain/graph/builder/stages/detect-changes.d.ts.map +1 -1
  124. package/dist/domain/graph/builder/stages/detect-changes.js +2 -1
  125. package/dist/domain/graph/builder/stages/detect-changes.js.map +1 -1
  126. package/dist/domain/graph/builder/stages/native-orchestrator.d.ts.map +1 -1
  127. package/dist/domain/graph/builder/stages/native-orchestrator.js +895 -545
  128. package/dist/domain/graph/builder/stages/native-orchestrator.js.map +1 -1
  129. package/dist/domain/graph/resolver/points-to.d.ts.map +1 -1
  130. package/dist/domain/graph/resolver/points-to.js +105 -57
  131. package/dist/domain/graph/resolver/points-to.js.map +1 -1
  132. package/dist/domain/graph/resolver/strategy.d.ts +61 -0
  133. package/dist/domain/graph/resolver/strategy.d.ts.map +1 -0
  134. package/dist/domain/graph/resolver/strategy.js +222 -0
  135. package/dist/domain/graph/resolver/strategy.js.map +1 -0
  136. package/dist/domain/graph/watcher.d.ts.map +1 -1
  137. package/dist/domain/graph/watcher.js +16 -9
  138. package/dist/domain/graph/watcher.js.map +1 -1
  139. package/dist/domain/parser.d.ts +12 -0
  140. package/dist/domain/parser.d.ts.map +1 -1
  141. package/dist/domain/parser.js +12 -2
  142. package/dist/domain/parser.js.map +1 -1
  143. package/dist/domain/queries.d.ts +1 -1
  144. package/dist/domain/queries.d.ts.map +1 -1
  145. package/dist/domain/queries.js +1 -1
  146. package/dist/domain/queries.js.map +1 -1
  147. package/dist/domain/wasm-worker-entry.js +3 -0
  148. package/dist/domain/wasm-worker-entry.js.map +1 -1
  149. package/dist/domain/wasm-worker-pool.d.ts.map +1 -1
  150. package/dist/domain/wasm-worker-pool.js +24 -5
  151. package/dist/domain/wasm-worker-pool.js.map +1 -1
  152. package/dist/domain/wasm-worker-protocol.d.ts +7 -0
  153. package/dist/domain/wasm-worker-protocol.d.ts.map +1 -1
  154. package/dist/extractors/dart.js +48 -3
  155. package/dist/extractors/dart.js.map +1 -1
  156. package/dist/extractors/groovy.js +62 -3
  157. package/dist/extractors/groovy.js.map +1 -1
  158. package/dist/extractors/helpers.d.ts +4 -2
  159. package/dist/extractors/helpers.d.ts.map +1 -1
  160. package/dist/extractors/helpers.js +5 -1
  161. package/dist/extractors/helpers.js.map +1 -1
  162. package/dist/extractors/java.js +77 -1
  163. package/dist/extractors/java.js.map +1 -1
  164. package/dist/extractors/javascript.d.ts.map +1 -1
  165. package/dist/extractors/javascript.js +549 -163
  166. package/dist/extractors/javascript.js.map +1 -1
  167. package/dist/extractors/kotlin.js +58 -3
  168. package/dist/extractors/kotlin.js.map +1 -1
  169. package/dist/extractors/objc.js +25 -2
  170. package/dist/extractors/objc.js.map +1 -1
  171. package/dist/extractors/scala.js +62 -2
  172. package/dist/extractors/scala.js.map +1 -1
  173. package/dist/extractors/swift.js +52 -3
  174. package/dist/extractors/swift.js.map +1 -1
  175. package/dist/features/audit.js +26 -23
  176. package/dist/features/audit.js.map +1 -1
  177. package/dist/features/boundaries.d.ts.map +1 -1
  178. package/dist/features/boundaries.js +12 -9
  179. package/dist/features/boundaries.js.map +1 -1
  180. package/dist/features/cfg.d.ts.map +1 -1
  181. package/dist/features/cfg.js +25 -18
  182. package/dist/features/cfg.js.map +1 -1
  183. package/dist/features/check.d.ts.map +1 -1
  184. package/dist/features/check.js +18 -5
  185. package/dist/features/check.js.map +1 -1
  186. package/dist/features/communities.d.ts +4 -2
  187. package/dist/features/communities.d.ts.map +1 -1
  188. package/dist/features/communities.js +6 -4
  189. package/dist/features/communities.js.map +1 -1
  190. package/dist/features/dataflow.d.ts +60 -0
  191. package/dist/features/dataflow.d.ts.map +1 -1
  192. package/dist/features/dataflow.js +530 -6
  193. package/dist/features/dataflow.js.map +1 -1
  194. package/dist/features/manifesto.d.ts.map +1 -1
  195. package/dist/features/manifesto.js +59 -72
  196. package/dist/features/manifesto.js.map +1 -1
  197. package/dist/features/sequence.d.ts.map +1 -1
  198. package/dist/features/sequence.js +27 -22
  199. package/dist/features/sequence.js.map +1 -1
  200. package/dist/features/snapshot.d.ts.map +1 -1
  201. package/dist/features/snapshot.js +36 -28
  202. package/dist/features/snapshot.js.map +1 -1
  203. package/dist/features/structure.d.ts.map +1 -1
  204. package/dist/features/structure.js +150 -62
  205. package/dist/features/structure.js.map +1 -1
  206. package/dist/features/triage.d.ts.map +1 -1
  207. package/dist/features/triage.js +18 -11
  208. package/dist/features/triage.js.map +1 -1
  209. package/dist/graph/algorithms/bfs.d.ts +1 -1
  210. package/dist/graph/algorithms/bfs.d.ts.map +1 -1
  211. package/dist/graph/algorithms/bfs.js +14 -13
  212. package/dist/graph/algorithms/bfs.js.map +1 -1
  213. package/dist/graph/algorithms/tarjan.d.ts.map +1 -1
  214. package/dist/graph/algorithms/tarjan.js +5 -0
  215. package/dist/graph/algorithms/tarjan.js.map +1 -1
  216. package/dist/graph/builders/dependency.js +28 -22
  217. package/dist/graph/builders/dependency.js.map +1 -1
  218. package/dist/graph/classifiers/roles.d.ts +10 -1
  219. package/dist/graph/classifiers/roles.d.ts.map +1 -1
  220. package/dist/graph/classifiers/roles.js +60 -6
  221. package/dist/graph/classifiers/roles.js.map +1 -1
  222. package/dist/infrastructure/config.d.ts +10 -0
  223. package/dist/infrastructure/config.d.ts.map +1 -1
  224. package/dist/infrastructure/config.js +31 -3
  225. package/dist/infrastructure/config.js.map +1 -1
  226. package/dist/infrastructure/registry.d.ts +0 -7
  227. package/dist/infrastructure/registry.d.ts.map +1 -1
  228. package/dist/infrastructure/registry.js +29 -13
  229. package/dist/infrastructure/registry.js.map +1 -1
  230. package/dist/infrastructure/update-check.d.ts.map +1 -1
  231. package/dist/infrastructure/update-check.js +49 -31
  232. package/dist/infrastructure/update-check.js.map +1 -1
  233. package/dist/mcp/server.d.ts +2 -10
  234. package/dist/mcp/server.d.ts.map +1 -1
  235. package/dist/mcp/server.js.map +1 -1
  236. package/dist/mcp/tools/ast-query.d.ts +1 -1
  237. package/dist/mcp/tools/ast-query.d.ts.map +1 -1
  238. package/dist/mcp/tools/audit.d.ts +1 -1
  239. package/dist/mcp/tools/audit.d.ts.map +1 -1
  240. package/dist/mcp/tools/batch-query.d.ts +1 -1
  241. package/dist/mcp/tools/batch-query.d.ts.map +1 -1
  242. package/dist/mcp/tools/branch-compare.d.ts +1 -1
  243. package/dist/mcp/tools/branch-compare.d.ts.map +1 -1
  244. package/dist/mcp/tools/brief.d.ts +1 -1
  245. package/dist/mcp/tools/brief.d.ts.map +1 -1
  246. package/dist/mcp/tools/cfg.d.ts +1 -1
  247. package/dist/mcp/tools/cfg.d.ts.map +1 -1
  248. package/dist/mcp/tools/check.d.ts +1 -1
  249. package/dist/mcp/tools/check.d.ts.map +1 -1
  250. package/dist/mcp/tools/co-changes.d.ts +1 -1
  251. package/dist/mcp/tools/co-changes.d.ts.map +1 -1
  252. package/dist/mcp/tools/code-owners.d.ts +1 -1
  253. package/dist/mcp/tools/code-owners.d.ts.map +1 -1
  254. package/dist/mcp/tools/communities.d.ts +1 -1
  255. package/dist/mcp/tools/communities.d.ts.map +1 -1
  256. package/dist/mcp/tools/complexity.d.ts +1 -1
  257. package/dist/mcp/tools/complexity.d.ts.map +1 -1
  258. package/dist/mcp/tools/context.d.ts +1 -1
  259. package/dist/mcp/tools/context.d.ts.map +1 -1
  260. package/dist/mcp/tools/dataflow.d.ts +1 -1
  261. package/dist/mcp/tools/dataflow.d.ts.map +1 -1
  262. package/dist/mcp/tools/diff-impact.d.ts +1 -1
  263. package/dist/mcp/tools/diff-impact.d.ts.map +1 -1
  264. package/dist/mcp/tools/execution-flow.d.ts +1 -1
  265. package/dist/mcp/tools/execution-flow.d.ts.map +1 -1
  266. package/dist/mcp/tools/export-graph.d.ts +1 -1
  267. package/dist/mcp/tools/export-graph.d.ts.map +1 -1
  268. package/dist/mcp/tools/file-deps.d.ts +1 -1
  269. package/dist/mcp/tools/file-deps.d.ts.map +1 -1
  270. package/dist/mcp/tools/file-exports.d.ts +1 -1
  271. package/dist/mcp/tools/file-exports.d.ts.map +1 -1
  272. package/dist/mcp/tools/find-cycles.d.ts +1 -1
  273. package/dist/mcp/tools/find-cycles.d.ts.map +1 -1
  274. package/dist/mcp/tools/fn-impact.d.ts +1 -1
  275. package/dist/mcp/tools/fn-impact.d.ts.map +1 -1
  276. package/dist/mcp/tools/impact-analysis.d.ts +1 -1
  277. package/dist/mcp/tools/impact-analysis.d.ts.map +1 -1
  278. package/dist/mcp/tools/implementations.d.ts +1 -1
  279. package/dist/mcp/tools/implementations.d.ts.map +1 -1
  280. package/dist/mcp/tools/index.d.ts +2 -5
  281. package/dist/mcp/tools/index.d.ts.map +1 -1
  282. package/dist/mcp/tools/index.js.map +1 -1
  283. package/dist/mcp/tools/interfaces.d.ts +1 -1
  284. package/dist/mcp/tools/interfaces.d.ts.map +1 -1
  285. package/dist/mcp/tools/list-functions.d.ts +1 -1
  286. package/dist/mcp/tools/list-functions.d.ts.map +1 -1
  287. package/dist/mcp/tools/list-repos.d.ts +1 -1
  288. package/dist/mcp/tools/list-repos.d.ts.map +1 -1
  289. package/dist/mcp/tools/module-map.d.ts +1 -1
  290. package/dist/mcp/tools/module-map.d.ts.map +1 -1
  291. package/dist/mcp/tools/node-roles.d.ts +1 -1
  292. package/dist/mcp/tools/node-roles.d.ts.map +1 -1
  293. package/dist/mcp/tools/path.d.ts +1 -1
  294. package/dist/mcp/tools/path.d.ts.map +1 -1
  295. package/dist/mcp/tools/query.d.ts +1 -1
  296. package/dist/mcp/tools/query.d.ts.map +1 -1
  297. package/dist/mcp/tools/semantic-search.d.ts +1 -1
  298. package/dist/mcp/tools/semantic-search.d.ts.map +1 -1
  299. package/dist/mcp/tools/sequence.d.ts +1 -1
  300. package/dist/mcp/tools/sequence.d.ts.map +1 -1
  301. package/dist/mcp/tools/structure.d.ts +1 -1
  302. package/dist/mcp/tools/structure.d.ts.map +1 -1
  303. package/dist/mcp/tools/symbol-children.d.ts +1 -1
  304. package/dist/mcp/tools/symbol-children.d.ts.map +1 -1
  305. package/dist/mcp/tools/triage.d.ts +1 -1
  306. package/dist/mcp/tools/triage.d.ts.map +1 -1
  307. package/dist/mcp/tools/where.d.ts +1 -1
  308. package/dist/mcp/tools/where.d.ts.map +1 -1
  309. package/dist/mcp/types.d.ts +19 -0
  310. package/dist/mcp/types.d.ts.map +1 -0
  311. package/dist/mcp/types.js +6 -0
  312. package/dist/mcp/types.js.map +1 -0
  313. package/dist/presentation/queries-cli/index.d.ts +1 -1
  314. package/dist/presentation/queries-cli/index.d.ts.map +1 -1
  315. package/dist/presentation/queries-cli/index.js +1 -1
  316. package/dist/presentation/queries-cli/index.js.map +1 -1
  317. package/dist/presentation/queries-cli/overview.d.ts +1 -0
  318. package/dist/presentation/queries-cli/overview.d.ts.map +1 -1
  319. package/dist/presentation/queries-cli/overview.js +20 -1
  320. package/dist/presentation/queries-cli/overview.js.map +1 -1
  321. package/dist/presentation/queries-cli.d.ts +1 -1
  322. package/dist/presentation/queries-cli.d.ts.map +1 -1
  323. package/dist/presentation/queries-cli.js +1 -1
  324. package/dist/presentation/queries-cli.js.map +1 -1
  325. package/dist/presentation/viewer.d.ts.map +1 -1
  326. package/dist/presentation/viewer.js +45 -32
  327. package/dist/presentation/viewer.js.map +1 -1
  328. package/dist/shared/constants.d.ts +21 -0
  329. package/dist/shared/constants.d.ts.map +1 -1
  330. package/dist/shared/constants.js +25 -0
  331. package/dist/shared/constants.js.map +1 -1
  332. package/dist/shared/normalize.d.ts.map +1 -1
  333. package/dist/shared/normalize.js +12 -22
  334. package/dist/shared/normalize.js.map +1 -1
  335. package/dist/shared/paginate.d.ts +4 -17
  336. package/dist/shared/paginate.d.ts.map +1 -1
  337. package/dist/shared/paginate.js.map +1 -1
  338. package/dist/types.d.ts +76 -1
  339. package/dist/types.d.ts.map +1 -1
  340. package/grammars/tree-sitter-erlang.wasm +0 -0
  341. package/package.json +7 -7
  342. package/src/ast-analysis/engine.ts +43 -63
  343. package/src/ast-analysis/rules/b2.ts +263 -0
  344. package/src/ast-analysis/rules/b3.ts +127 -0
  345. package/src/ast-analysis/rules/b4.ts +378 -0
  346. package/src/ast-analysis/rules/b5.ts +65 -0
  347. package/src/ast-analysis/rules/c.ts +157 -0
  348. package/src/ast-analysis/rules/index.ts +34 -0
  349. package/src/ast-analysis/rules/javascript.ts +3 -0
  350. package/src/ast-analysis/shared.ts +2 -0
  351. package/src/ast-analysis/visitor-utils.ts +5 -0
  352. package/src/ast-analysis/visitor.ts +82 -52
  353. package/src/ast-analysis/visitors/cfg-visitor.ts +198 -84
  354. package/src/ast-analysis/visitors/complexity-visitor.ts +44 -16
  355. package/src/ast-analysis/visitors/dataflow-visitor.ts +68 -29
  356. package/src/cli/commands/config.ts +184 -184
  357. package/src/cli/commands/roles.ts +6 -1
  358. package/src/db/better-sqlite3.ts +5 -4
  359. package/src/db/connection.ts +23 -5
  360. package/src/db/index.ts +1 -0
  361. package/src/db/migrations.ts +68 -0
  362. package/src/db/repository/build-stmts.ts +30 -0
  363. package/src/db/repository/dataflow.ts +16 -0
  364. package/src/db/repository/index.ts +1 -1
  365. package/src/db/repository/native-repository.ts +56 -40
  366. package/src/domain/analysis/fn-impact.ts +4 -0
  367. package/src/domain/analysis/module-map.ts +38 -6
  368. package/src/domain/analysis/roles.ts +23 -0
  369. package/src/domain/graph/builder/call-resolver.ts +112 -232
  370. package/src/domain/graph/builder/context.ts +1 -0
  371. package/src/domain/graph/builder/helpers.ts +190 -72
  372. package/src/domain/graph/builder/incremental.ts +249 -120
  373. package/src/domain/graph/builder/pipeline.ts +11 -5
  374. package/src/domain/graph/builder/stages/build-edges.ts +696 -296
  375. package/src/domain/graph/builder/stages/collect-files.ts +12 -6
  376. package/src/domain/graph/builder/stages/detect-changes.ts +3 -1
  377. package/src/domain/graph/builder/stages/native-orchestrator.ts +1102 -590
  378. package/src/domain/graph/resolver/points-to.ts +182 -59
  379. package/src/domain/graph/resolver/strategy.ts +265 -0
  380. package/src/domain/graph/watcher.ts +19 -9
  381. package/src/domain/parser.ts +12 -2
  382. package/src/domain/queries.ts +1 -1
  383. package/src/domain/wasm-worker-entry.ts +3 -0
  384. package/src/domain/wasm-worker-pool.ts +28 -4
  385. package/src/domain/wasm-worker-protocol.ts +4 -0
  386. package/src/extractors/dart.ts +48 -3
  387. package/src/extractors/groovy.ts +62 -2
  388. package/src/extractors/helpers.ts +5 -2
  389. package/src/extractors/java.ts +80 -1
  390. package/src/extractors/javascript.ts +566 -161
  391. package/src/extractors/kotlin.ts +57 -3
  392. package/src/extractors/objc.ts +25 -1
  393. package/src/extractors/scala.ts +63 -1
  394. package/src/extractors/swift.ts +46 -3
  395. package/src/features/audit.ts +43 -34
  396. package/src/features/boundaries.ts +17 -9
  397. package/src/features/cfg.ts +31 -22
  398. package/src/features/check.ts +21 -5
  399. package/src/features/communities.ts +28 -19
  400. package/src/features/dataflow.ts +755 -6
  401. package/src/features/manifesto.ts +76 -75
  402. package/src/features/sequence.ts +29 -23
  403. package/src/features/snapshot.ts +36 -25
  404. package/src/features/structure.ts +185 -55
  405. package/src/features/triage.ts +28 -15
  406. package/src/graph/algorithms/bfs.ts +13 -12
  407. package/src/graph/algorithms/tarjan.ts +5 -0
  408. package/src/graph/builders/dependency.ts +35 -23
  409. package/src/graph/classifiers/roles.ts +74 -7
  410. package/src/infrastructure/config.ts +32 -3
  411. package/src/infrastructure/registry.ts +44 -20
  412. package/src/infrastructure/update-check.ts +55 -33
  413. package/src/mcp/server.ts +2 -8
  414. package/src/mcp/tools/ast-query.ts +1 -1
  415. package/src/mcp/tools/audit.ts +1 -1
  416. package/src/mcp/tools/batch-query.ts +1 -1
  417. package/src/mcp/tools/branch-compare.ts +1 -1
  418. package/src/mcp/tools/brief.ts +1 -1
  419. package/src/mcp/tools/cfg.ts +1 -1
  420. package/src/mcp/tools/check.ts +1 -1
  421. package/src/mcp/tools/co-changes.ts +1 -1
  422. package/src/mcp/tools/code-owners.ts +1 -1
  423. package/src/mcp/tools/communities.ts +1 -1
  424. package/src/mcp/tools/complexity.ts +1 -1
  425. package/src/mcp/tools/context.ts +1 -1
  426. package/src/mcp/tools/dataflow.ts +1 -1
  427. package/src/mcp/tools/diff-impact.ts +1 -1
  428. package/src/mcp/tools/execution-flow.ts +1 -1
  429. package/src/mcp/tools/export-graph.ts +1 -1
  430. package/src/mcp/tools/file-deps.ts +1 -1
  431. package/src/mcp/tools/file-exports.ts +1 -1
  432. package/src/mcp/tools/find-cycles.ts +1 -1
  433. package/src/mcp/tools/fn-impact.ts +1 -1
  434. package/src/mcp/tools/impact-analysis.ts +1 -1
  435. package/src/mcp/tools/implementations.ts +1 -1
  436. package/src/mcp/tools/index.ts +2 -5
  437. package/src/mcp/tools/interfaces.ts +1 -1
  438. package/src/mcp/tools/list-functions.ts +1 -1
  439. package/src/mcp/tools/list-repos.ts +1 -1
  440. package/src/mcp/tools/module-map.ts +1 -1
  441. package/src/mcp/tools/node-roles.ts +1 -1
  442. package/src/mcp/tools/path.ts +1 -1
  443. package/src/mcp/tools/query.ts +1 -1
  444. package/src/mcp/tools/semantic-search.ts +1 -1
  445. package/src/mcp/tools/sequence.ts +1 -1
  446. package/src/mcp/tools/structure.ts +1 -1
  447. package/src/mcp/tools/symbol-children.ts +1 -1
  448. package/src/mcp/tools/triage.ts +1 -1
  449. package/src/mcp/tools/where.ts +1 -1
  450. package/src/mcp/types.ts +21 -0
  451. package/src/presentation/queries-cli/index.ts +1 -1
  452. package/src/presentation/queries-cli/overview.ts +35 -1
  453. package/src/presentation/queries-cli.ts +1 -0
  454. package/src/presentation/viewer.ts +98 -87
  455. package/src/shared/constants.ts +26 -0
  456. package/src/shared/normalize.ts +13 -22
  457. package/src/shared/paginate.ts +4 -18
  458. package/src/types.ts +86 -1
@@ -124,7 +124,331 @@ function insertDataflowEdges(insert, data, resolveNode) {
124
124
  }
125
125
  return edgeCount;
126
126
  }
127
+ // ── P1: dataflow_vertices + intra def_use edges ───────────────────────────────
128
+ function prepareVertexStmts(db) {
129
+ try {
130
+ return {
131
+ insertVertex: db.prepare(`INSERT INTO dataflow_vertices (func_id, kind, name, param_index, line, node_id)
132
+ VALUES (?, ?, ?, ?, ?, ?)`),
133
+ insertIntraEdge: db.prepare(`INSERT INTO dataflow
134
+ (source_id, target_id, kind, source_vertex, target_vertex, scope, expression, line, confidence)
135
+ VALUES (?, ?, 'def_use', ?, ?, 'intra', ?, ?, 1.0)`),
136
+ available: true,
137
+ };
138
+ }
139
+ catch {
140
+ return {
141
+ insertVertex: db.prepare('SELECT 1'),
142
+ insertIntraEdge: db.prepare('SELECT 1'),
143
+ available: false,
144
+ };
145
+ }
146
+ }
147
+ /**
148
+ * Build dataflow_vertices, intra def_use edges, and summaries for one file.
149
+ * Called alongside insertDataflowEdges in the same transaction.
150
+ *
151
+ * Returns stitch candidates and return captures for the P2 inter-procedural
152
+ * post-pass (run after all files are processed).
153
+ */
154
+ function buildDataflowVerticesAndEdges(db, vstmts, data, resolveNode) {
155
+ const empty = {
156
+ candidates: [],
157
+ captures: [],
158
+ };
159
+ if (!vstmts.available)
160
+ return empty;
161
+ const params = data.parameters;
162
+ const returns = data.returns;
163
+ const assignments = data.assignments;
164
+ const argFlows = data.argFlows;
165
+ const mutations = data.mutations;
166
+ // 1. param vertices
167
+ const paramVertexIds = new Map(); // "funcName:paramName" → vertex id
168
+ const paramIndexByFuncAndIndex = new Map(); // "funcId:paramIndex" → vertex id
169
+ for (const p of params) {
170
+ const fn = resolveNode(p.funcName);
171
+ if (!fn)
172
+ continue;
173
+ const result = vstmts.insertVertex.run(fn.id, 'param', p.paramName, p.paramIndex, p.line, null);
174
+ const vid = result.lastInsertRowid;
175
+ paramVertexIds.set(`${p.funcName}:${p.paramName}`, vid);
176
+ paramIndexByFuncAndIndex.set(`${fn.id}:${p.paramIndex}`, vid);
177
+ }
178
+ // 2. return vertices (one per function that has a return statement)
179
+ const returnVertexIds = new Map(); // funcName → vertex id
180
+ const returnFuncsSeen = new Set();
181
+ for (const r of returns) {
182
+ if (returnFuncsSeen.has(r.funcName))
183
+ continue;
184
+ returnFuncsSeen.add(r.funcName);
185
+ const fn = resolveNode(r.funcName);
186
+ if (!fn)
187
+ continue;
188
+ const result = vstmts.insertVertex.run(fn.id, 'return', null, null, r.line, null);
189
+ returnVertexIds.set(r.funcName, result.lastInsertRowid);
190
+ }
191
+ // 3. local vertices (from call-return assignments)
192
+ const localVertexIds = new Map(); // "funcName:varName" → vertex id
193
+ const localsSeen = new Set();
194
+ for (const a of assignments) {
195
+ const key = `${a.callerFunc}:${a.varName}`;
196
+ if (localsSeen.has(key))
197
+ continue;
198
+ localsSeen.add(key);
199
+ const fn = resolveNode(a.callerFunc);
200
+ if (!fn)
201
+ continue;
202
+ const result = vstmts.insertVertex.run(fn.id, 'local', a.varName, null, a.line, null);
203
+ localVertexIds.set(key, result.lastInsertRowid);
204
+ }
205
+ // 4. intra def_use edges: param/local → return
206
+ for (const r of returns) {
207
+ const fn = resolveNode(r.funcName);
208
+ if (!fn)
209
+ continue;
210
+ const returnVid = returnVertexIds.get(r.funcName);
211
+ if (!returnVid)
212
+ continue;
213
+ for (const name of r.referencedNames) {
214
+ const paramVid = paramVertexIds.get(`${r.funcName}:${name}`);
215
+ if (paramVid) {
216
+ vstmts.insertIntraEdge.run(fn.id, fn.id, paramVid, returnVid, r.expression, r.line);
217
+ }
218
+ const localVid = localVertexIds.get(`${r.funcName}:${name}`);
219
+ if (localVid) {
220
+ vstmts.insertIntraEdge.run(fn.id, fn.id, localVid, returnVid, r.expression, r.line);
221
+ }
222
+ }
223
+ }
224
+ // 5. summaries: flows_to_return = direct def_use from param to function's return
225
+ const checkDefUse = db.prepare(`SELECT 1 FROM dataflow WHERE source_vertex = ? AND target_vertex = ? AND kind = 'def_use' LIMIT 1`);
226
+ const insertSummary = db.prepare(`INSERT OR REPLACE INTO dataflow_summary (func_id, param_index, flows_to_return, is_mutated) VALUES (?, ?, ?, ?)`);
227
+ for (const p of params) {
228
+ const fn = resolveNode(p.funcName);
229
+ if (!fn)
230
+ continue;
231
+ const paramVid = paramVertexIds.get(`${p.funcName}:${p.paramName}`);
232
+ if (!paramVid)
233
+ continue;
234
+ const returnVid = returnVertexIds.get(p.funcName);
235
+ const flowsToReturn = returnVid ? (checkDefUse.get(paramVid, returnVid) ? 1 : 0) : 0;
236
+ const isMutated = mutations.some((m) => m.funcName === p.funcName &&
237
+ m.binding?.type === 'param' &&
238
+ m.binding?.index === p.paramIndex)
239
+ ? 1
240
+ : 0;
241
+ insertSummary.run(fn.id, p.paramIndex, flowsToReturn, isMutated);
242
+ }
243
+ // 6. collect stitch candidates for P2 inter-procedural post-pass
244
+ const candidates = [];
245
+ for (const af of argFlows) {
246
+ const callerFn = resolveNode(af.callerFunc);
247
+ const calleeFn = resolveNode(af.calleeName);
248
+ if (!callerFn || !calleeFn)
249
+ continue;
250
+ candidates.push({
251
+ callerFuncId: callerFn.id,
252
+ calleeFuncId: calleeFn.id,
253
+ argIndex: af.argIndex,
254
+ bindingType: af.binding.type,
255
+ bindingIndex: af.binding.index,
256
+ argName: af.argName,
257
+ expression: af.expression,
258
+ line: af.line,
259
+ confidence: af.confidence,
260
+ });
261
+ }
262
+ // 7. collect return captures (locals that hold a callee's return value)
263
+ const captures = [];
264
+ for (const a of assignments) {
265
+ const callerFn = resolveNode(a.callerFunc);
266
+ const calleeFn = resolveNode(a.sourceCallName);
267
+ if (!callerFn || !calleeFn)
268
+ continue;
269
+ captures.push({ callerFuncId: callerFn.id, calleeFuncId: calleeFn.id, varName: a.varName });
270
+ }
271
+ return { candidates, captures };
272
+ }
273
+ // ── P2: interprocedural stitching ─────────────────────────────────────────────
274
+ /**
275
+ * Core stitch logic — must be called inside an already-open transaction.
276
+ *
277
+ * All callers (buildDataflowVerticesFromMap, buildDataflowEdges,
278
+ * buildDataflowP4ForNative) manage their own outer transaction and call this
279
+ * directly to avoid nested transactions, which better-sqlite3 does not support.
280
+ */
281
+ function runInterproceduralStitch(db, candidates, captures) {
282
+ if (candidates.length === 0)
283
+ return 0;
284
+ const getParamVertex = db.prepare(`SELECT id FROM dataflow_vertices WHERE func_id = ? AND kind = 'param' AND param_index = ? LIMIT 1`);
285
+ const getLocalVertex = db.prepare(`SELECT id FROM dataflow_vertices WHERE func_id = ? AND kind = 'local' AND name = ? LIMIT 1`);
286
+ const getReturnVertex = db.prepare(`SELECT id FROM dataflow_vertices WHERE func_id = ? AND kind = 'return' LIMIT 1`);
287
+ const getCallEdge = db.prepare(`SELECT id FROM edges WHERE source_id = ? AND target_id = ? AND kind = 'calls' LIMIT 1`);
288
+ const getSummary = db.prepare(`SELECT flows_to_return FROM dataflow_summary WHERE func_id = ? AND param_index = ?`);
289
+ const insertInterEdge = db.prepare(`INSERT INTO dataflow
290
+ (source_id, target_id, kind, source_vertex, target_vertex, scope, call_edge_id, expression, line, confidence)
291
+ VALUES (?, ?, ?, ?, ?, 'inter', ?, ?, ?, ?)`);
292
+ // Build capture map: "callerFuncId:calleeFuncId" → varName (first match wins)
293
+ const captureMap = new Map();
294
+ for (const cap of captures) {
295
+ const key = `${cap.callerFuncId}:${cap.calleeFuncId}`;
296
+ if (!captureMap.has(key))
297
+ captureMap.set(key, cap.varName);
298
+ }
299
+ let count = 0;
300
+ for (const cand of candidates) {
301
+ // Resolve call edge for this site
302
+ const callEdge = getCallEdge.get(cand.callerFuncId, cand.calleeFuncId);
303
+ const callEdgeId = callEdge?.id ?? null;
304
+ // Find source vertex x in caller
305
+ let srcVertexId = null;
306
+ if (cand.bindingType === 'param' && cand.bindingIndex != null) {
307
+ const v = getParamVertex.get(cand.callerFuncId, cand.bindingIndex);
308
+ srcVertexId = v?.id ?? null;
309
+ }
310
+ else if (cand.bindingType === 'local') {
311
+ const v = getLocalVertex.get(cand.callerFuncId, cand.argName);
312
+ srcVertexId = v?.id ?? null;
313
+ }
314
+ if (!srcVertexId)
315
+ continue;
316
+ // Find callee's param[argIndex] vertex
317
+ const calleeParam = getParamVertex.get(cand.calleeFuncId, cand.argIndex);
318
+ if (!calleeParam)
319
+ continue;
320
+ // arg_in: A's source → B.param[j]
321
+ insertInterEdge.run(cand.callerFuncId, cand.calleeFuncId, 'arg_in', srcVertexId, calleeParam.id, callEdgeId, cand.expression, cand.line, cand.confidence);
322
+ count++;
323
+ // return_out: if B.param[j] reaches B's return, emit B.return → A's capture
324
+ const summary = getSummary.get(cand.calleeFuncId, cand.argIndex);
325
+ if (summary?.flows_to_return) {
326
+ const calleeReturn = getReturnVertex.get(cand.calleeFuncId);
327
+ if (calleeReturn) {
328
+ const captureVarName = captureMap.get(`${cand.callerFuncId}:${cand.calleeFuncId}`);
329
+ const captureVertex = captureVarName
330
+ ? getLocalVertex.get(cand.callerFuncId, captureVarName)
331
+ : null;
332
+ if (captureVertex) {
333
+ insertInterEdge.run(cand.calleeFuncId, cand.callerFuncId, 'return_out', calleeReturn.id, captureVertex.id, callEdgeId, cand.expression, cand.line, cand.confidence);
334
+ count++;
335
+ }
336
+ }
337
+ }
338
+ }
339
+ return count;
340
+ }
127
341
  // ── buildDataflowEdges ──────────────────────────────────────────────────────
342
+ // ── P4 helpers ───────────────────────────────────────────────────────────────
343
+ /** Return IDs of all function/method nodes in the given relative file paths. */
344
+ export function collectFuncIdsForFiles(db, relPaths) {
345
+ const stmt = db.prepare(`SELECT id FROM nodes WHERE file = ? AND kind IN ('function', 'method')`);
346
+ const ids = [];
347
+ for (const p of relPaths) {
348
+ for (const row of stmt.all(p))
349
+ ids.push(row.id);
350
+ }
351
+ return ids;
352
+ }
353
+ /**
354
+ * P4: Re-collect stitch candidates from caller files that were NOT in the
355
+ * changed set but contain calls to functions that WERE changed.
356
+ *
357
+ * During an incremental build the changed files' param vertices are purged
358
+ * and recreated, but the callers' files are never re-parsed — so their
359
+ * arg_in edges (pointing to the old param vertices) are deleted and never
360
+ * replaced. This function reads those caller files from disk and rebuilds
361
+ * the StitchCandidate list so runInterproceduralStitch can reconnect them.
362
+ */
363
+ export async function collectCallerStitchCandidates(db, changedFuncIds, changedRelPaths, rootDir, extToLang, parsers, getParserFn) {
364
+ if (changedFuncIds.length === 0)
365
+ return { candidates: [], captures: [] };
366
+ // Find distinct caller files that have flows_to edges targeting any changed
367
+ // function and are NOT already in the changed file set (those are handled by
368
+ // the main per-file loop).
369
+ //
370
+ // Chunk the query to avoid exceeding SQLite's SQLITE_MAX_VARIABLE_NUMBER
371
+ // (999 on older builds, 32766 on SQLite ≥ 3.32). 500 is a safe batch size
372
+ // that works across all SQLite versions.
373
+ const CHUNK_SIZE = 500;
374
+ const callerFileSet = new Set();
375
+ for (let i = 0; i < changedFuncIds.length; i += CHUNK_SIZE) {
376
+ const chunk = changedFuncIds.slice(i, i + CHUNK_SIZE);
377
+ const placeholders = chunk.map(() => '?').join(',');
378
+ const rows = db
379
+ .prepare(`SELECT DISTINCT n.file AS caller_file
380
+ FROM dataflow d
381
+ JOIN nodes n ON n.id = d.source_id
382
+ WHERE d.target_id IN (${placeholders})
383
+ AND d.kind = 'flows_to'`)
384
+ .all(...chunk);
385
+ for (const r of rows)
386
+ callerFileSet.add(r.caller_file);
387
+ }
388
+ const callerFileRows = [...callerFileSet].map((f) => ({ caller_file: f }));
389
+ const callerFiles = callerFileRows
390
+ .map((r) => r.caller_file)
391
+ .filter((f) => !changedRelPaths.has(f));
392
+ if (callerFiles.length === 0)
393
+ return { candidates: [], captures: [] };
394
+ // Ensure parsers are available — the main loop may have skipped loading them
395
+ // if all changed files came through the native bulk-insert path.
396
+ let activeParsers = parsers;
397
+ let activeGetParserFn = getParserFn;
398
+ if (!activeGetParserFn) {
399
+ const { createParsers, getParser } = await import('../domain/parser.js');
400
+ activeParsers = await createParsers();
401
+ activeGetParserFn = getParser;
402
+ }
403
+ const changedFuncIdSet = new Set(changedFuncIds);
404
+ const stmts = prepareNodeResolvers(db);
405
+ const candidates = [];
406
+ const captures = [];
407
+ for (const callerFile of callerFiles) {
408
+ // Read the caller file from disk without touching its existing DB rows.
409
+ // definitions: [] is an intentional stub — P4 only needs argFlow/assignment
410
+ // data from the visitor, not pre-loaded symbol definitions. extractDataflow
411
+ // does not currently use _definitions, so this is safe. If that changes,
412
+ // the stub must be replaced with the actual symbol list for the caller file.
413
+ const stub = { definitions: [], _langId: null, _tree: null };
414
+ const data = getDataflowForFile(stub, callerFile, rootDir, extToLang, activeParsers, activeGetParserFn);
415
+ if (!data)
416
+ continue;
417
+ const resolver = makeNodeResolver(stmts, callerFile);
418
+ const argFlows = data.argFlows;
419
+ const assignments = data.assignments;
420
+ for (const af of argFlows) {
421
+ const callerFn = resolver(af.callerFunc);
422
+ const calleeFn = resolver(af.calleeName);
423
+ if (!callerFn || !calleeFn)
424
+ continue;
425
+ if (!changedFuncIdSet.has(calleeFn.id))
426
+ continue; // only re-stitch calls to changed callees
427
+ candidates.push({
428
+ callerFuncId: callerFn.id,
429
+ calleeFuncId: calleeFn.id,
430
+ argIndex: af.argIndex,
431
+ bindingType: af.binding.type,
432
+ bindingIndex: af.binding.index,
433
+ argName: af.argName,
434
+ expression: af.expression,
435
+ line: af.line,
436
+ confidence: af.confidence,
437
+ });
438
+ }
439
+ for (const a of assignments) {
440
+ const callerFn = resolver(a.callerFunc);
441
+ const calleeFn = resolver(a.sourceCallName);
442
+ if (!callerFn || !calleeFn)
443
+ continue;
444
+ if (!changedFuncIdSet.has(calleeFn.id))
445
+ continue;
446
+ captures.push({ callerFuncId: callerFn.id, calleeFuncId: calleeFn.id, varName: a.varName });
447
+ }
448
+ }
449
+ debug(`Dataflow P4: re-stitched ${candidates.length} candidate(s) from ${callerFiles.length} caller file(s)`);
450
+ return { candidates, captures };
451
+ }
128
452
  function prepareNodeResolvers(db) {
129
453
  return {
130
454
  getNodeByNameAndFile: db.prepare(`SELECT id, name, kind, file, line FROM nodes
@@ -135,12 +459,24 @@ function prepareNodeResolvers(db) {
135
459
  };
136
460
  }
137
461
  function makeNodeResolver(stmts, relPath) {
462
+ // Memoise per (relPath, funcName). buildDataflowVerticesAndEdges resolves the
463
+ // same handful of function names many times per file — once per param, return,
464
+ // assignment, argFlow, summary row, and capture — and each miss costs one or
465
+ // two `nodes` table queries. The nodes table is never mutated during the P6
466
+ // vertex pass (only dataflow* tables are written), so the lookup is stable for
467
+ // the lifetime of the resolver; caching collapses tens of thousands of
468
+ // redundant queries on a full build into one per distinct name (#perf).
469
+ const cache = new Map();
138
470
  return (funcName) => {
471
+ const cached = cache.get(funcName);
472
+ if (cached !== undefined)
473
+ return cached;
139
474
  const local = stmts.getNodeByNameAndFile.all(funcName, relPath);
140
- if (local.length > 0)
141
- return local[0];
142
- const global = stmts.getNodeByName.all(funcName);
143
- return global.length > 0 ? global[0] : null;
475
+ const resolved = local.length > 0
476
+ ? local[0]
477
+ : (stmts.getNodeByName.all(funcName)[0] ?? null);
478
+ cache.set(funcName, resolved);
479
+ return resolved;
144
480
  };
145
481
  }
146
482
  function collectNativeEdges(data, resolveNode, edges) {
@@ -189,6 +525,99 @@ function collectNativeEdges(data, resolveNode, edges) {
189
525
  }
190
526
  }
191
527
  }
528
+ /**
529
+ * P6 vertex-only pass for the native orchestrator path.
530
+ *
531
+ * When the Rust orchestrator runs with analysisComplete=true it inserts
532
+ * flows_to/returns/mutates edges directly into the DB but never writes to
533
+ * dataflow_vertices or dataflow_summary. This function takes pre-extracted
534
+ * DataflowResult objects (from native.extractDataflowAnalysis) and builds
535
+ * the missing vertex rows and inter-procedural edges — without touching the
536
+ * already-correct function-level edges.
537
+ */
538
+ export function buildDataflowVerticesFromMap(db, dataflowMap, extraCandidates, extraCaptures) {
539
+ const vstmts = prepareVertexStmts(db);
540
+ if (!vstmts.available || dataflowMap.size === 0)
541
+ return 0;
542
+ const stmts = prepareNodeResolvers(db);
543
+ // Vertex writes and inter-procedural stitch are a single atomic unit: if the
544
+ // stitch throws or the process is killed between the two, the DB would be left
545
+ // with dataflow_vertices rows but no arg_in/return_out edges. Wrapping both
546
+ // under one transaction boundary prevents that half-written state.
547
+ let stitchCount = 0;
548
+ const tx = db.transaction(() => {
549
+ const allCandidates = [];
550
+ const allCaptures = [];
551
+ for (const [relPath, data] of dataflowMap) {
552
+ const resolver = makeNodeResolver(stmts, relPath);
553
+ const { candidates, captures } = buildDataflowVerticesAndEdges(db, vstmts, data, resolver);
554
+ allCandidates.push(...candidates);
555
+ allCaptures.push(...captures);
556
+ }
557
+ // P4: merge in stitch candidates from unchanged caller files if provided.
558
+ if (extraCandidates && extraCandidates.length > 0)
559
+ allCandidates.push(...extraCandidates);
560
+ if (extraCaptures && extraCaptures.length > 0)
561
+ allCaptures.push(...extraCaptures);
562
+ stitchCount = runInterproceduralStitch(db, allCandidates, allCaptures);
563
+ });
564
+ tx();
565
+ return stitchCount;
566
+ }
567
+ /**
568
+ * P4 re-stitch pass for the native engine path.
569
+ *
570
+ * On incremental builds the changed files' param vertices are purged and
571
+ * recreated by the P6 vertex pass, but unchanged caller files are never
572
+ * re-parsed — so their arg_in edges (which pointed to the old param vertex
573
+ * IDs) are deleted and not replaced. This function re-parses those caller
574
+ * files and rebuilds the arg_in edges for any call that targets a function
575
+ * in one of the changed callee files.
576
+ *
577
+ * Called by the native orchestrator after buildDataflowVerticesFromMap.
578
+ * Safe to call on full builds — the guard below exits early when
579
+ * changedFiles covers all distinct files in the DB.
580
+ */
581
+ export async function buildDataflowP4ForNative(db, changedFiles, rootDir) {
582
+ if (changedFiles.length === 0)
583
+ return;
584
+ // Deduplicate upfront so the full-build guard uses unique-file count, not
585
+ // raw array length (the orchestrator may emit the same path more than once).
586
+ const changedRelPaths = new Set(changedFiles);
587
+ // Skip on full builds — all files were in the changed set, so there are no
588
+ // unchanged callers to re-stitch.
589
+ const totalFilesInDb = db.prepare(`SELECT COUNT(DISTINCT file) AS n FROM nodes`).get().n;
590
+ if (changedRelPaths.size >= totalFilesInDb)
591
+ return;
592
+ const extToLang = buildExtToLangMap();
593
+ const changedFuncIds = collectFuncIdsForFiles(db, changedRelPaths);
594
+ if (changedFuncIds.length === 0)
595
+ return;
596
+ // parsers=null, getParserFn=null → collectCallerStitchCandidates initialises
597
+ // WASM parsers lazily for the caller files it needs to re-parse.
598
+ const { candidates, captures } = await collectCallerStitchCandidates(db, changedFuncIds, changedRelPaths, rootDir, extToLang, null, null);
599
+ if (candidates.length > 0) {
600
+ let count = 0;
601
+ // Wrap the DELETE + stitch in a single transaction so that a crash between
602
+ // the purge and the re-insert cannot leave arg_in edges permanently removed
603
+ // but never replaced.
604
+ db.transaction(() => {
605
+ // Purge any existing arg_in edges targeting the changed callees from unchanged
606
+ // caller files. These edges were inserted by a previous P4 pass. Without this
607
+ // guard, repeated calls to buildDataflowP4ForNative insert duplicates because
608
+ // the dataflow table has no UNIQUE constraint on (source_vertex, target_vertex).
609
+ const placeholders = changedFuncIds.map(() => '?').join(',');
610
+ db.prepare(`DELETE FROM dataflow
611
+ WHERE kind = 'arg_in'
612
+ AND target_id IN (${placeholders})
613
+ AND source_id NOT IN (${placeholders})`).run(...changedFuncIds, ...changedFuncIds);
614
+ count = runInterproceduralStitch(db, candidates, captures);
615
+ })();
616
+ if (count > 0) {
617
+ info(`Dataflow (native P4): ${count} inter-procedural edges re-stitched`);
618
+ }
619
+ }
620
+ }
192
621
  export async function buildDataflowEdges(db, fileSymbols, rootDir, engineOpts) {
193
622
  const extToLang = buildExtToLangMap();
194
623
  // ── Native bulk-insert fast path ──────────────────────────────────────
@@ -219,6 +648,61 @@ export async function buildDataflowEdges(db, fileSymbols, rootDir, engineOpts) {
219
648
  }
220
649
  info(`Dataflow (native bulk): ${inserted} edges inserted`);
221
650
  }
651
+ // P6: vertex extraction on the native path.
652
+ // Rust DataflowResult already contains parameters/returns — no re-parse needed.
653
+ const vstmts = prepareVertexStmts(db);
654
+ if (vstmts.available) {
655
+ // P4: Incremental re-stitch — unchanged caller files are not in
656
+ // fileSymbols so their arg_in edges to the old param vertices were
657
+ // deleted by the purge and never recreated. Re-collect stitch
658
+ // candidates from those caller files by parsing them from disk.
659
+ //
660
+ // Skip on full builds: fileSymbols covers every file in the DB, so
661
+ // there are no unchanged callers to re-stitch.
662
+ //
663
+ // This async disk-read step must happen BEFORE the transaction opens
664
+ // (SQLite transactions are synchronous; async work inside them is not
665
+ // supported by better-sqlite3).
666
+ const totalFilesInDb = db.prepare(`SELECT COUNT(DISTINCT file) AS n FROM nodes`).get().n;
667
+ let p4CallerCount = 0;
668
+ const p4Candidates = [];
669
+ const p4Captures = [];
670
+ if (fileSymbols.size < totalFilesInDb) {
671
+ const changedRelPaths = new Set(fileSymbols.keys());
672
+ const changedFuncIds = collectFuncIdsForFiles(db, changedRelPaths);
673
+ const extra = await collectCallerStitchCandidates(db, changedFuncIds, changedRelPaths, rootDir, extToLang, null, null);
674
+ p4Candidates.push(...extra.candidates);
675
+ p4Captures.push(...extra.captures);
676
+ p4CallerCount = extra.candidates.length;
677
+ }
678
+ // Vertex writes and inter-procedural stitch are a single atomic unit:
679
+ // if the stitch throws or the process is killed between the two, the DB
680
+ // would be left with dataflow_vertices rows but no arg_in/return_out
681
+ // edges. Wrapping both under one transaction boundary prevents that
682
+ // half-written state.
683
+ let interCount = 0;
684
+ const txVertex = db.transaction(() => {
685
+ const allCandidates = [];
686
+ const allCaptures = [];
687
+ for (const [relPath, symbols] of fileSymbols) {
688
+ if (!symbols.dataflow)
689
+ continue;
690
+ const ext = path.extname(relPath).toLowerCase();
691
+ if (!DATAFLOW_EXTENSIONS.has(ext))
692
+ continue;
693
+ const resolver = makeNodeResolver(stmts, relPath);
694
+ const { candidates, captures } = buildDataflowVerticesAndEdges(db, vstmts, symbols.dataflow, resolver);
695
+ allCandidates.push(...candidates);
696
+ allCaptures.push(...captures);
697
+ }
698
+ // Merge in the P4 candidates collected above before opening the tx.
699
+ allCandidates.push(...p4Candidates);
700
+ allCaptures.push(...p4Captures);
701
+ interCount = runInterproceduralStitch(db, allCandidates, allCaptures);
702
+ });
703
+ txVertex();
704
+ info(`Dataflow (native): ${interCount} inter-procedural edges inserted${p4CallerCount > 0 ? ` (P4: ${p4CallerCount} re-stitch candidate(s) from unchanged callers)` : ''}`);
705
+ }
222
706
  return;
223
707
  }
224
708
  debug('Dataflow: some files lack pre-computed data — falling back to JS');
@@ -228,8 +712,39 @@ export async function buildDataflowEdges(db, fileSymbols, rootDir, engineOpts) {
228
712
  const insert = db.prepare(`INSERT INTO dataflow (source_id, target_id, kind, param_index, expression, line, confidence)
229
713
  VALUES (?, ?, ?, ?, ?, ?, ?)`);
230
714
  const stmts = prepareNodeResolvers(db);
715
+ const vstmts = prepareVertexStmts(db);
716
+ // P4: Incremental re-stitch — if only a subset of files changed, callers of
717
+ // the changed functions were not in fileSymbols, so their arg_in edges were
718
+ // deleted by the purge but never reconstructed. Re-collect stitch candidates
719
+ // from those caller files now (read from disk, no DB writes).
720
+ //
721
+ // Skip P4 on full builds: when fileSymbols covers every file in the DB there
722
+ // are no unchanged callers, and collectFuncIdsForFiles would issue one SELECT
723
+ // per file for nothing. A single COUNT query is cheaper than N per-file SELECTs.
724
+ //
725
+ // This async disk-read step must happen BEFORE the transaction opens
726
+ // (SQLite transactions are synchronous; async work inside them is not
727
+ // supported by better-sqlite3).
728
+ const totalFilesInDb = db.prepare(`SELECT COUNT(DISTINCT file) AS n FROM nodes`).get().n;
729
+ const p4Candidates = [];
730
+ const p4Captures = [];
731
+ if (vstmts.available && fileSymbols.size < totalFilesInDb) {
732
+ const changedRelPaths = new Set(fileSymbols.keys());
733
+ const changedFuncIds = collectFuncIdsForFiles(db, changedRelPaths);
734
+ const extra = await collectCallerStitchCandidates(db, changedFuncIds, changedRelPaths, rootDir, extToLang, parsers, getParserFn);
735
+ p4Candidates.push(...extra.candidates);
736
+ p4Captures.push(...extra.captures);
737
+ }
738
+ // Edge writes, vertex writes, and inter-procedural stitch are a single
739
+ // atomic unit: if the stitch throws or the process is killed between vertex
740
+ // writes and the stitch, the DB would be left with dataflow_vertices rows
741
+ // but no arg_in/return_out edges. Wrapping all three under one transaction
742
+ // boundary prevents that half-written state.
231
743
  let totalEdges = 0;
744
+ let interCount = 0;
232
745
  const tx = db.transaction(() => {
746
+ const allCandidates = [];
747
+ const allCaptures = [];
233
748
  for (const [relPath, symbols] of fileSymbols) {
234
749
  const ext = path.extname(relPath).toLowerCase();
235
750
  if (!DATAFLOW_EXTENSIONS.has(ext))
@@ -237,11 +752,20 @@ export async function buildDataflowEdges(db, fileSymbols, rootDir, engineOpts) {
237
752
  const data = getDataflowForFile(symbols, relPath, rootDir, extToLang, parsers, getParserFn);
238
753
  if (!data)
239
754
  continue;
240
- totalEdges += insertDataflowEdges(insert, data, makeNodeResolver(stmts, relPath));
755
+ const resolver = makeNodeResolver(stmts, relPath);
756
+ totalEdges += insertDataflowEdges(insert, data, resolver);
757
+ const { candidates, captures } = buildDataflowVerticesAndEdges(db, vstmts, data, resolver);
758
+ allCandidates.push(...candidates);
759
+ allCaptures.push(...captures);
241
760
  }
761
+ // Merge in the P4 candidates collected above before opening the tx.
762
+ allCandidates.push(...p4Candidates);
763
+ allCaptures.push(...p4Captures);
764
+ // P2: inter-procedural stitch — runs after all per-file vertices + summaries written
765
+ interCount = vstmts.available ? runInterproceduralStitch(db, allCandidates, allCaptures) : 0;
242
766
  });
243
767
  tx();
244
- info(`Dataflow: ${totalEdges} edges inserted`);
768
+ info(`Dataflow: ${totalEdges} fn-level edges, ${interCount} inter-procedural edges inserted`);
245
769
  }
246
770
  function prepareDataflowStmts(db) {
247
771
  return {