@codragraph/cli 1.6.2

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 (909) hide show
  1. package/README.md +341 -0
  2. package/dist/_shared/graph/types.d.ts +81 -0
  3. package/dist/_shared/graph/types.d.ts.map +1 -0
  4. package/dist/_shared/graph/types.js +8 -0
  5. package/dist/_shared/graph/types.js.map +1 -0
  6. package/dist/_shared/index.d.ts +55 -0
  7. package/dist/_shared/index.d.ts.map +1 -0
  8. package/dist/_shared/index.js +39 -0
  9. package/dist/_shared/index.js.map +1 -0
  10. package/dist/_shared/language-detection.d.ts +23 -0
  11. package/dist/_shared/language-detection.d.ts.map +1 -0
  12. package/dist/_shared/language-detection.js +139 -0
  13. package/dist/_shared/language-detection.js.map +1 -0
  14. package/dist/_shared/languages.d.ts +26 -0
  15. package/dist/_shared/languages.d.ts.map +1 -0
  16. package/dist/_shared/languages.js +27 -0
  17. package/dist/_shared/languages.js.map +1 -0
  18. package/dist/_shared/lbug/schema-constants.d.ts +16 -0
  19. package/dist/_shared/lbug/schema-constants.d.ts.map +1 -0
  20. package/dist/_shared/lbug/schema-constants.js +67 -0
  21. package/dist/_shared/lbug/schema-constants.js.map +1 -0
  22. package/dist/_shared/mro-strategy.d.ts +41 -0
  23. package/dist/_shared/mro-strategy.d.ts.map +1 -0
  24. package/dist/_shared/mro-strategy.js +2 -0
  25. package/dist/_shared/mro-strategy.js.map +1 -0
  26. package/dist/_shared/pipeline.d.ts +16 -0
  27. package/dist/_shared/pipeline.d.ts.map +1 -0
  28. package/dist/_shared/pipeline.js +5 -0
  29. package/dist/_shared/pipeline.js.map +1 -0
  30. package/dist/_shared/scope-resolution/def-index.d.ts +36 -0
  31. package/dist/_shared/scope-resolution/def-index.d.ts.map +1 -0
  32. package/dist/_shared/scope-resolution/def-index.js +51 -0
  33. package/dist/_shared/scope-resolution/def-index.js.map +1 -0
  34. package/dist/_shared/scope-resolution/evidence-weights.d.ts +69 -0
  35. package/dist/_shared/scope-resolution/evidence-weights.d.ts.map +1 -0
  36. package/dist/_shared/scope-resolution/evidence-weights.js +84 -0
  37. package/dist/_shared/scope-resolution/evidence-weights.js.map +1 -0
  38. package/dist/_shared/scope-resolution/finalize-algorithm.d.ts +139 -0
  39. package/dist/_shared/scope-resolution/finalize-algorithm.d.ts.map +1 -0
  40. package/dist/_shared/scope-resolution/finalize-algorithm.js +479 -0
  41. package/dist/_shared/scope-resolution/finalize-algorithm.js.map +1 -0
  42. package/dist/_shared/scope-resolution/language-classification.d.ts +26 -0
  43. package/dist/_shared/scope-resolution/language-classification.d.ts.map +1 -0
  44. package/dist/_shared/scope-resolution/language-classification.js +44 -0
  45. package/dist/_shared/scope-resolution/language-classification.js.map +1 -0
  46. package/dist/_shared/scope-resolution/method-dispatch-index.d.ts +80 -0
  47. package/dist/_shared/scope-resolution/method-dispatch-index.d.ts.map +1 -0
  48. package/dist/_shared/scope-resolution/method-dispatch-index.js +79 -0
  49. package/dist/_shared/scope-resolution/method-dispatch-index.js.map +1 -0
  50. package/dist/_shared/scope-resolution/module-scope-index.d.ts +46 -0
  51. package/dist/_shared/scope-resolution/module-scope-index.d.ts.map +1 -0
  52. package/dist/_shared/scope-resolution/module-scope-index.js +58 -0
  53. package/dist/_shared/scope-resolution/module-scope-index.js.map +1 -0
  54. package/dist/_shared/scope-resolution/origin-priority.d.ts +14 -0
  55. package/dist/_shared/scope-resolution/origin-priority.d.ts.map +1 -0
  56. package/dist/_shared/scope-resolution/origin-priority.js +21 -0
  57. package/dist/_shared/scope-resolution/origin-priority.js.map +1 -0
  58. package/dist/_shared/scope-resolution/parsed-file.d.ts +76 -0
  59. package/dist/_shared/scope-resolution/parsed-file.d.ts.map +1 -0
  60. package/dist/_shared/scope-resolution/parsed-file.js +54 -0
  61. package/dist/_shared/scope-resolution/parsed-file.js.map +1 -0
  62. package/dist/_shared/scope-resolution/position-index.d.ts +62 -0
  63. package/dist/_shared/scope-resolution/position-index.d.ts.map +1 -0
  64. package/dist/_shared/scope-resolution/position-index.js +134 -0
  65. package/dist/_shared/scope-resolution/position-index.js.map +1 -0
  66. package/dist/_shared/scope-resolution/qualified-name-index.d.ts +44 -0
  67. package/dist/_shared/scope-resolution/qualified-name-index.d.ts.map +1 -0
  68. package/dist/_shared/scope-resolution/qualified-name-index.js +75 -0
  69. package/dist/_shared/scope-resolution/qualified-name-index.js.map +1 -0
  70. package/dist/_shared/scope-resolution/reference-site.d.ts +75 -0
  71. package/dist/_shared/scope-resolution/reference-site.d.ts.map +1 -0
  72. package/dist/_shared/scope-resolution/reference-site.js +24 -0
  73. package/dist/_shared/scope-resolution/reference-site.js.map +1 -0
  74. package/dist/_shared/scope-resolution/registries/class-registry.d.ts +27 -0
  75. package/dist/_shared/scope-resolution/registries/class-registry.d.ts.map +1 -0
  76. package/dist/_shared/scope-resolution/registries/class-registry.js +30 -0
  77. package/dist/_shared/scope-resolution/registries/class-registry.js.map +1 -0
  78. package/dist/_shared/scope-resolution/registries/context.d.ts +69 -0
  79. package/dist/_shared/scope-resolution/registries/context.d.ts.map +1 -0
  80. package/dist/_shared/scope-resolution/registries/context.js +44 -0
  81. package/dist/_shared/scope-resolution/registries/context.js.map +1 -0
  82. package/dist/_shared/scope-resolution/registries/evidence.d.ts +56 -0
  83. package/dist/_shared/scope-resolution/registries/evidence.d.ts.map +1 -0
  84. package/dist/_shared/scope-resolution/registries/evidence.js +150 -0
  85. package/dist/_shared/scope-resolution/registries/evidence.js.map +1 -0
  86. package/dist/_shared/scope-resolution/registries/field-registry.d.ts +26 -0
  87. package/dist/_shared/scope-resolution/registries/field-registry.d.ts.map +1 -0
  88. package/dist/_shared/scope-resolution/registries/field-registry.js +31 -0
  89. package/dist/_shared/scope-resolution/registries/field-registry.js.map +1 -0
  90. package/dist/_shared/scope-resolution/registries/lookup-core.d.ts +81 -0
  91. package/dist/_shared/scope-resolution/registries/lookup-core.d.ts.map +1 -0
  92. package/dist/_shared/scope-resolution/registries/lookup-core.js +332 -0
  93. package/dist/_shared/scope-resolution/registries/lookup-core.js.map +1 -0
  94. package/dist/_shared/scope-resolution/registries/lookup-qualified.d.ts +33 -0
  95. package/dist/_shared/scope-resolution/registries/lookup-qualified.d.ts.map +1 -0
  96. package/dist/_shared/scope-resolution/registries/lookup-qualified.js +56 -0
  97. package/dist/_shared/scope-resolution/registries/lookup-qualified.js.map +1 -0
  98. package/dist/_shared/scope-resolution/registries/method-registry.d.ts +36 -0
  99. package/dist/_shared/scope-resolution/registries/method-registry.d.ts.map +1 -0
  100. package/dist/_shared/scope-resolution/registries/method-registry.js +32 -0
  101. package/dist/_shared/scope-resolution/registries/method-registry.js.map +1 -0
  102. package/dist/_shared/scope-resolution/registries/tie-breaks.d.ts +43 -0
  103. package/dist/_shared/scope-resolution/registries/tie-breaks.d.ts.map +1 -0
  104. package/dist/_shared/scope-resolution/registries/tie-breaks.js +60 -0
  105. package/dist/_shared/scope-resolution/registries/tie-breaks.js.map +1 -0
  106. package/dist/_shared/scope-resolution/resolve-type-ref.d.ts +53 -0
  107. package/dist/_shared/scope-resolution/resolve-type-ref.d.ts.map +1 -0
  108. package/dist/_shared/scope-resolution/resolve-type-ref.js +126 -0
  109. package/dist/_shared/scope-resolution/resolve-type-ref.js.map +1 -0
  110. package/dist/_shared/scope-resolution/scope-id.d.ts +43 -0
  111. package/dist/_shared/scope-resolution/scope-id.d.ts.map +1 -0
  112. package/dist/_shared/scope-resolution/scope-id.js +46 -0
  113. package/dist/_shared/scope-resolution/scope-id.js.map +1 -0
  114. package/dist/_shared/scope-resolution/scope-tree.d.ts +61 -0
  115. package/dist/_shared/scope-resolution/scope-tree.d.ts.map +1 -0
  116. package/dist/_shared/scope-resolution/scope-tree.js +186 -0
  117. package/dist/_shared/scope-resolution/scope-tree.js.map +1 -0
  118. package/dist/_shared/scope-resolution/shadow/aggregate.d.ts +63 -0
  119. package/dist/_shared/scope-resolution/shadow/aggregate.d.ts.map +1 -0
  120. package/dist/_shared/scope-resolution/shadow/aggregate.js +122 -0
  121. package/dist/_shared/scope-resolution/shadow/aggregate.js.map +1 -0
  122. package/dist/_shared/scope-resolution/shadow/diff.d.ts +59 -0
  123. package/dist/_shared/scope-resolution/shadow/diff.d.ts.map +1 -0
  124. package/dist/_shared/scope-resolution/shadow/diff.js +79 -0
  125. package/dist/_shared/scope-resolution/shadow/diff.js.map +1 -0
  126. package/dist/_shared/scope-resolution/symbol-definition.d.ts +34 -0
  127. package/dist/_shared/scope-resolution/symbol-definition.d.ts.map +1 -0
  128. package/dist/_shared/scope-resolution/symbol-definition.js +12 -0
  129. package/dist/_shared/scope-resolution/symbol-definition.js.map +1 -0
  130. package/dist/_shared/scope-resolution/types.d.ts +356 -0
  131. package/dist/_shared/scope-resolution/types.d.ts.map +1 -0
  132. package/dist/_shared/scope-resolution/types.js +17 -0
  133. package/dist/_shared/scope-resolution/types.js.map +1 -0
  134. package/dist/cli/ai-context.d.ts +27 -0
  135. package/dist/cli/ai-context.js +270 -0
  136. package/dist/cli/analyze.d.ts +43 -0
  137. package/dist/cli/analyze.js +312 -0
  138. package/dist/cli/augment.d.ts +13 -0
  139. package/dist/cli/augment.js +33 -0
  140. package/dist/cli/clean.d.ts +10 -0
  141. package/dist/cli/clean.js +78 -0
  142. package/dist/cli/config.d.ts +27 -0
  143. package/dist/cli/config.js +106 -0
  144. package/dist/cli/eval-server.d.ts +37 -0
  145. package/dist/cli/eval-server.js +398 -0
  146. package/dist/cli/graphstore.d.ts +40 -0
  147. package/dist/cli/graphstore.js +639 -0
  148. package/dist/cli/group.d.ts +2 -0
  149. package/dist/cli/group.js +306 -0
  150. package/dist/cli/index-repo.d.ts +15 -0
  151. package/dist/cli/index-repo.js +120 -0
  152. package/dist/cli/index.d.ts +2 -0
  153. package/dist/cli/index.js +236 -0
  154. package/dist/cli/lazy-action.d.ts +6 -0
  155. package/dist/cli/lazy-action.js +18 -0
  156. package/dist/cli/list.d.ts +6 -0
  157. package/dist/cli/list.js +40 -0
  158. package/dist/cli/mcp.d.ts +8 -0
  159. package/dist/cli/mcp.js +36 -0
  160. package/dist/cli/remove.d.ts +30 -0
  161. package/dist/cli/remove.js +99 -0
  162. package/dist/cli/serve.d.ts +4 -0
  163. package/dist/cli/serve.js +37 -0
  164. package/dist/cli/setup.d.ts +8 -0
  165. package/dist/cli/setup.js +543 -0
  166. package/dist/cli/skill-gen.d.ts +26 -0
  167. package/dist/cli/skill-gen.js +555 -0
  168. package/dist/cli/status.d.ts +6 -0
  169. package/dist/cli/status.js +36 -0
  170. package/dist/cli/tool.d.ts +43 -0
  171. package/dist/cli/tool.js +168 -0
  172. package/dist/cli/wiki.d.ts +21 -0
  173. package/dist/cli/wiki.js +579 -0
  174. package/dist/config/ignore-service.d.ts +35 -0
  175. package/dist/config/ignore-service.js +436 -0
  176. package/dist/config/supported-languages.d.ts +13 -0
  177. package/dist/config/supported-languages.js +13 -0
  178. package/dist/core/augmentation/engine.d.ts +26 -0
  179. package/dist/core/augmentation/engine.js +252 -0
  180. package/dist/core/embeddings/ast-utils.d.ts +22 -0
  181. package/dist/core/embeddings/ast-utils.js +105 -0
  182. package/dist/core/embeddings/character-chunk.d.ts +12 -0
  183. package/dist/core/embeddings/character-chunk.js +43 -0
  184. package/dist/core/embeddings/chunker.d.ts +14 -0
  185. package/dist/core/embeddings/chunker.js +239 -0
  186. package/dist/core/embeddings/embedder.d.ts +65 -0
  187. package/dist/core/embeddings/embedder.js +320 -0
  188. package/dist/core/embeddings/embedding-pipeline.d.ts +62 -0
  189. package/dist/core/embeddings/embedding-pipeline.js +486 -0
  190. package/dist/core/embeddings/http-client.d.ts +31 -0
  191. package/dist/core/embeddings/http-client.js +179 -0
  192. package/dist/core/embeddings/index.d.ts +10 -0
  193. package/dist/core/embeddings/index.js +10 -0
  194. package/dist/core/embeddings/line-index.d.ts +7 -0
  195. package/dist/core/embeddings/line-index.js +42 -0
  196. package/dist/core/embeddings/server-mapping.d.ts +15 -0
  197. package/dist/core/embeddings/server-mapping.js +33 -0
  198. package/dist/core/embeddings/structural-extractor.d.ts +15 -0
  199. package/dist/core/embeddings/structural-extractor.js +58 -0
  200. package/dist/core/embeddings/text-generator.d.ts +31 -0
  201. package/dist/core/embeddings/text-generator.js +208 -0
  202. package/dist/core/embeddings/types.d.ts +207 -0
  203. package/dist/core/embeddings/types.js +200 -0
  204. package/dist/core/git-staleness.d.ts +31 -0
  205. package/dist/core/git-staleness.js +137 -0
  206. package/dist/core/graph/graph.d.ts +2 -0
  207. package/dist/core/graph/graph.js +173 -0
  208. package/dist/core/graph/types.d.ts +36 -0
  209. package/dist/core/graph/types.js +1 -0
  210. package/dist/core/graphstore/index.d.ts +46 -0
  211. package/dist/core/graphstore/index.js +80 -0
  212. package/dist/core/graphstore/lbug-row-source.d.ts +19 -0
  213. package/dist/core/graphstore/lbug-row-source.js +141 -0
  214. package/dist/core/group/bridge-db.d.ts +82 -0
  215. package/dist/core/group/bridge-db.js +460 -0
  216. package/dist/core/group/bridge-schema.d.ts +27 -0
  217. package/dist/core/group/bridge-schema.js +55 -0
  218. package/dist/core/group/config-parser.d.ts +7 -0
  219. package/dist/core/group/config-parser.js +100 -0
  220. package/dist/core/group/contract-extractor.d.ts +7 -0
  221. package/dist/core/group/contract-extractor.js +1 -0
  222. package/dist/core/group/cross-impact.d.ts +41 -0
  223. package/dist/core/group/cross-impact.js +441 -0
  224. package/dist/core/group/extractors/fs-utils.d.ts +10 -0
  225. package/dist/core/group/extractors/fs-utils.js +24 -0
  226. package/dist/core/group/extractors/grpc-extractor.d.ts +25 -0
  227. package/dist/core/group/extractors/grpc-extractor.js +401 -0
  228. package/dist/core/group/extractors/grpc-patterns/go.d.ts +2 -0
  229. package/dist/core/group/extractors/grpc-patterns/go.js +97 -0
  230. package/dist/core/group/extractors/grpc-patterns/index.d.ts +19 -0
  231. package/dist/core/group/extractors/grpc-patterns/index.js +46 -0
  232. package/dist/core/group/extractors/grpc-patterns/java.d.ts +2 -0
  233. package/dist/core/group/extractors/grpc-patterns/java.js +173 -0
  234. package/dist/core/group/extractors/grpc-patterns/node.d.ts +4 -0
  235. package/dist/core/group/extractors/grpc-patterns/node.js +290 -0
  236. package/dist/core/group/extractors/grpc-patterns/proto.d.ts +9 -0
  237. package/dist/core/group/extractors/grpc-patterns/proto.js +134 -0
  238. package/dist/core/group/extractors/grpc-patterns/python.d.ts +2 -0
  239. package/dist/core/group/extractors/grpc-patterns/python.js +67 -0
  240. package/dist/core/group/extractors/grpc-patterns/types.d.ts +50 -0
  241. package/dist/core/group/extractors/grpc-patterns/types.js +1 -0
  242. package/dist/core/group/extractors/http-patterns/go.d.ts +2 -0
  243. package/dist/core/group/extractors/http-patterns/go.js +215 -0
  244. package/dist/core/group/extractors/http-patterns/index.d.ts +17 -0
  245. package/dist/core/group/extractors/http-patterns/index.js +44 -0
  246. package/dist/core/group/extractors/http-patterns/java.d.ts +2 -0
  247. package/dist/core/group/extractors/http-patterns/java.js +253 -0
  248. package/dist/core/group/extractors/http-patterns/node.d.ts +4 -0
  249. package/dist/core/group/extractors/http-patterns/node.js +484 -0
  250. package/dist/core/group/extractors/http-patterns/php.d.ts +2 -0
  251. package/dist/core/group/extractors/http-patterns/php.js +178 -0
  252. package/dist/core/group/extractors/http-patterns/python.d.ts +2 -0
  253. package/dist/core/group/extractors/http-patterns/python.js +133 -0
  254. package/dist/core/group/extractors/http-patterns/types.d.ts +61 -0
  255. package/dist/core/group/extractors/http-patterns/types.js +1 -0
  256. package/dist/core/group/extractors/http-route-extractor.d.ts +21 -0
  257. package/dist/core/group/extractors/http-route-extractor.js +421 -0
  258. package/dist/core/group/extractors/manifest-extractor.d.ts +54 -0
  259. package/dist/core/group/extractors/manifest-extractor.js +292 -0
  260. package/dist/core/group/extractors/topic-extractor.d.ts +8 -0
  261. package/dist/core/group/extractors/topic-extractor.js +97 -0
  262. package/dist/core/group/extractors/topic-patterns/go.d.ts +2 -0
  263. package/dist/core/group/extractors/topic-patterns/go.js +120 -0
  264. package/dist/core/group/extractors/topic-patterns/index.d.ts +14 -0
  265. package/dist/core/group/extractors/topic-patterns/index.js +38 -0
  266. package/dist/core/group/extractors/topic-patterns/java.d.ts +2 -0
  267. package/dist/core/group/extractors/topic-patterns/java.js +80 -0
  268. package/dist/core/group/extractors/topic-patterns/node.d.ts +4 -0
  269. package/dist/core/group/extractors/topic-patterns/node.js +155 -0
  270. package/dist/core/group/extractors/topic-patterns/python.d.ts +2 -0
  271. package/dist/core/group/extractors/topic-patterns/python.js +116 -0
  272. package/dist/core/group/extractors/topic-patterns/types.d.ts +25 -0
  273. package/dist/core/group/extractors/topic-patterns/types.js +10 -0
  274. package/dist/core/group/extractors/tree-sitter-scanner.d.ts +113 -0
  275. package/dist/core/group/extractors/tree-sitter-scanner.js +94 -0
  276. package/dist/core/group/group-path-utils.d.ts +17 -0
  277. package/dist/core/group/group-path-utils.js +40 -0
  278. package/dist/core/group/matching.d.ts +13 -0
  279. package/dist/core/group/matching.js +198 -0
  280. package/dist/core/group/normalization.d.ts +3 -0
  281. package/dist/core/group/normalization.js +115 -0
  282. package/dist/core/group/resolve-at-member.d.ts +10 -0
  283. package/dist/core/group/resolve-at-member.js +31 -0
  284. package/dist/core/group/service-boundary-detector.d.ts +8 -0
  285. package/dist/core/group/service-boundary-detector.js +155 -0
  286. package/dist/core/group/service.d.ts +55 -0
  287. package/dist/core/group/service.js +394 -0
  288. package/dist/core/group/storage.d.ts +9 -0
  289. package/dist/core/group/storage.js +91 -0
  290. package/dist/core/group/sync.d.ts +21 -0
  291. package/dist/core/group/sync.js +196 -0
  292. package/dist/core/group/types.d.ts +160 -0
  293. package/dist/core/group/types.js +1 -0
  294. package/dist/core/ingestion/ast-cache.d.ts +26 -0
  295. package/dist/core/ingestion/ast-cache.js +47 -0
  296. package/dist/core/ingestion/binding-accumulator.d.ts +212 -0
  297. package/dist/core/ingestion/binding-accumulator.js +336 -0
  298. package/dist/core/ingestion/call-extractors/configs/c-cpp.d.ts +3 -0
  299. package/dist/core/ingestion/call-extractors/configs/c-cpp.js +8 -0
  300. package/dist/core/ingestion/call-extractors/configs/csharp.d.ts +2 -0
  301. package/dist/core/ingestion/call-extractors/configs/csharp.js +6 -0
  302. package/dist/core/ingestion/call-extractors/configs/dart.d.ts +2 -0
  303. package/dist/core/ingestion/call-extractors/configs/dart.js +5 -0
  304. package/dist/core/ingestion/call-extractors/configs/go.d.ts +2 -0
  305. package/dist/core/ingestion/call-extractors/configs/go.js +5 -0
  306. package/dist/core/ingestion/call-extractors/configs/jvm.d.ts +3 -0
  307. package/dist/core/ingestion/call-extractors/configs/jvm.js +51 -0
  308. package/dist/core/ingestion/call-extractors/configs/php.d.ts +2 -0
  309. package/dist/core/ingestion/call-extractors/configs/php.js +5 -0
  310. package/dist/core/ingestion/call-extractors/configs/python.d.ts +2 -0
  311. package/dist/core/ingestion/call-extractors/configs/python.js +5 -0
  312. package/dist/core/ingestion/call-extractors/configs/ruby.d.ts +2 -0
  313. package/dist/core/ingestion/call-extractors/configs/ruby.js +5 -0
  314. package/dist/core/ingestion/call-extractors/configs/rust.d.ts +2 -0
  315. package/dist/core/ingestion/call-extractors/configs/rust.js +5 -0
  316. package/dist/core/ingestion/call-extractors/configs/swift.d.ts +2 -0
  317. package/dist/core/ingestion/call-extractors/configs/swift.js +5 -0
  318. package/dist/core/ingestion/call-extractors/configs/typescript-javascript.d.ts +3 -0
  319. package/dist/core/ingestion/call-extractors/configs/typescript-javascript.js +8 -0
  320. package/dist/core/ingestion/call-extractors/generic.d.ts +5 -0
  321. package/dist/core/ingestion/call-extractors/generic.js +59 -0
  322. package/dist/core/ingestion/call-processor.d.ts +235 -0
  323. package/dist/core/ingestion/call-processor.js +2639 -0
  324. package/dist/core/ingestion/call-routing.d.ts +55 -0
  325. package/dist/core/ingestion/call-routing.js +95 -0
  326. package/dist/core/ingestion/call-types.d.ts +135 -0
  327. package/dist/core/ingestion/call-types.js +2 -0
  328. package/dist/core/ingestion/class-extractors/configs/c-cpp.d.ts +3 -0
  329. package/dist/core/ingestion/class-extractors/configs/c-cpp.js +11 -0
  330. package/dist/core/ingestion/class-extractors/configs/csharp.d.ts +2 -0
  331. package/dist/core/ingestion/class-extractors/configs/csharp.js +21 -0
  332. package/dist/core/ingestion/class-extractors/configs/dart.d.ts +2 -0
  333. package/dist/core/ingestion/class-extractors/configs/dart.js +7 -0
  334. package/dist/core/ingestion/class-extractors/configs/go.d.ts +2 -0
  335. package/dist/core/ingestion/class-extractors/configs/go.js +20 -0
  336. package/dist/core/ingestion/class-extractors/configs/jvm.d.ts +3 -0
  337. package/dist/core/ingestion/class-extractors/configs/jvm.js +35 -0
  338. package/dist/core/ingestion/class-extractors/configs/php.d.ts +2 -0
  339. package/dist/core/ingestion/class-extractors/configs/php.js +7 -0
  340. package/dist/core/ingestion/class-extractors/configs/python.d.ts +2 -0
  341. package/dist/core/ingestion/class-extractors/configs/python.js +7 -0
  342. package/dist/core/ingestion/class-extractors/configs/ruby.d.ts +2 -0
  343. package/dist/core/ingestion/class-extractors/configs/ruby.js +7 -0
  344. package/dist/core/ingestion/class-extractors/configs/rust.d.ts +2 -0
  345. package/dist/core/ingestion/class-extractors/configs/rust.js +7 -0
  346. package/dist/core/ingestion/class-extractors/configs/swift.d.ts +2 -0
  347. package/dist/core/ingestion/class-extractors/configs/swift.js +18 -0
  348. package/dist/core/ingestion/class-extractors/configs/typescript-javascript.d.ts +4 -0
  349. package/dist/core/ingestion/class-extractors/configs/typescript-javascript.js +28 -0
  350. package/dist/core/ingestion/class-extractors/generic.d.ts +2 -0
  351. package/dist/core/ingestion/class-extractors/generic.js +135 -0
  352. package/dist/core/ingestion/class-types.d.ts +34 -0
  353. package/dist/core/ingestion/class-types.js +1 -0
  354. package/dist/core/ingestion/cluster-enricher.d.ts +38 -0
  355. package/dist/core/ingestion/cluster-enricher.js +168 -0
  356. package/dist/core/ingestion/cobol/cobol-copy-expander.d.ts +57 -0
  357. package/dist/core/ingestion/cobol/cobol-copy-expander.js +392 -0
  358. package/dist/core/ingestion/cobol/cobol-preprocessor.d.ts +210 -0
  359. package/dist/core/ingestion/cobol/cobol-preprocessor.js +1715 -0
  360. package/dist/core/ingestion/cobol/jcl-parser.d.ts +68 -0
  361. package/dist/core/ingestion/cobol/jcl-parser.js +217 -0
  362. package/dist/core/ingestion/cobol/jcl-processor.d.ts +33 -0
  363. package/dist/core/ingestion/cobol/jcl-processor.js +229 -0
  364. package/dist/core/ingestion/cobol-processor.d.ts +54 -0
  365. package/dist/core/ingestion/cobol-processor.js +1232 -0
  366. package/dist/core/ingestion/community-processor.d.ts +39 -0
  367. package/dist/core/ingestion/community-processor.js +318 -0
  368. package/dist/core/ingestion/constants.d.ts +16 -0
  369. package/dist/core/ingestion/constants.js +16 -0
  370. package/dist/core/ingestion/emit-references.d.ts +88 -0
  371. package/dist/core/ingestion/emit-references.js +229 -0
  372. package/dist/core/ingestion/entry-point-scoring.d.ts +58 -0
  373. package/dist/core/ingestion/entry-point-scoring.js +380 -0
  374. package/dist/core/ingestion/export-detection.d.ts +57 -0
  375. package/dist/core/ingestion/export-detection.js +233 -0
  376. package/dist/core/ingestion/field-extractor.d.ts +29 -0
  377. package/dist/core/ingestion/field-extractor.js +25 -0
  378. package/dist/core/ingestion/field-extractors/configs/c-cpp.d.ts +3 -0
  379. package/dist/core/ingestion/field-extractors/configs/c-cpp.js +104 -0
  380. package/dist/core/ingestion/field-extractors/configs/csharp.d.ts +8 -0
  381. package/dist/core/ingestion/field-extractors/configs/csharp.js +116 -0
  382. package/dist/core/ingestion/field-extractors/configs/dart.d.ts +8 -0
  383. package/dist/core/ingestion/field-extractors/configs/dart.js +78 -0
  384. package/dist/core/ingestion/field-extractors/configs/go.d.ts +11 -0
  385. package/dist/core/ingestion/field-extractors/configs/go.js +60 -0
  386. package/dist/core/ingestion/field-extractors/configs/helpers.d.ts +53 -0
  387. package/dist/core/ingestion/field-extractors/configs/helpers.js +158 -0
  388. package/dist/core/ingestion/field-extractors/configs/jvm.d.ts +3 -0
  389. package/dist/core/ingestion/field-extractors/configs/jvm.js +118 -0
  390. package/dist/core/ingestion/field-extractors/configs/php.d.ts +8 -0
  391. package/dist/core/ingestion/field-extractors/configs/php.js +65 -0
  392. package/dist/core/ingestion/field-extractors/configs/python.d.ts +12 -0
  393. package/dist/core/ingestion/field-extractors/configs/python.js +91 -0
  394. package/dist/core/ingestion/field-extractors/configs/ruby.d.ts +16 -0
  395. package/dist/core/ingestion/field-extractors/configs/ruby.js +76 -0
  396. package/dist/core/ingestion/field-extractors/configs/rust.d.ts +9 -0
  397. package/dist/core/ingestion/field-extractors/configs/rust.js +52 -0
  398. package/dist/core/ingestion/field-extractors/configs/swift.d.ts +8 -0
  399. package/dist/core/ingestion/field-extractors/configs/swift.js +65 -0
  400. package/dist/core/ingestion/field-extractors/configs/typescript-javascript.d.ts +3 -0
  401. package/dist/core/ingestion/field-extractors/configs/typescript-javascript.js +56 -0
  402. package/dist/core/ingestion/field-extractors/generic.d.ts +49 -0
  403. package/dist/core/ingestion/field-extractors/generic.js +117 -0
  404. package/dist/core/ingestion/field-extractors/typescript.d.ts +77 -0
  405. package/dist/core/ingestion/field-extractors/typescript.js +291 -0
  406. package/dist/core/ingestion/field-types.d.ts +61 -0
  407. package/dist/core/ingestion/field-types.js +2 -0
  408. package/dist/core/ingestion/filesystem-walker.d.ts +28 -0
  409. package/dist/core/ingestion/filesystem-walker.js +91 -0
  410. package/dist/core/ingestion/finalize-orchestrator.d.ts +63 -0
  411. package/dist/core/ingestion/finalize-orchestrator.js +139 -0
  412. package/dist/core/ingestion/framework-detection.d.ts +150 -0
  413. package/dist/core/ingestion/framework-detection.js +786 -0
  414. package/dist/core/ingestion/heritage-extractors/configs/go.d.ts +13 -0
  415. package/dist/core/ingestion/heritage-extractors/configs/go.js +20 -0
  416. package/dist/core/ingestion/heritage-extractors/configs/ruby.d.ts +18 -0
  417. package/dist/core/ingestion/heritage-extractors/configs/ruby.js +65 -0
  418. package/dist/core/ingestion/heritage-extractors/generic.d.ts +23 -0
  419. package/dist/core/ingestion/heritage-extractors/generic.js +47 -0
  420. package/dist/core/ingestion/heritage-processor.d.ts +54 -0
  421. package/dist/core/ingestion/heritage-processor.js +360 -0
  422. package/dist/core/ingestion/heritage-types.d.ts +73 -0
  423. package/dist/core/ingestion/heritage-types.js +2 -0
  424. package/dist/core/ingestion/import-processor.d.ts +23 -0
  425. package/dist/core/ingestion/import-processor.js +373 -0
  426. package/dist/core/ingestion/import-resolvers/configs/c-cpp.d.ts +7 -0
  427. package/dist/core/ingestion/import-resolvers/configs/c-cpp.js +14 -0
  428. package/dist/core/ingestion/import-resolvers/configs/csharp.d.ts +8 -0
  429. package/dist/core/ingestion/import-resolvers/configs/csharp.js +27 -0
  430. package/dist/core/ingestion/import-resolvers/configs/dart.d.ts +17 -0
  431. package/dist/core/ingestion/import-resolvers/configs/dart.js +54 -0
  432. package/dist/core/ingestion/import-resolvers/configs/go.d.ts +8 -0
  433. package/dist/core/ingestion/import-resolvers/configs/go.js +26 -0
  434. package/dist/core/ingestion/import-resolvers/configs/jvm.d.ts +13 -0
  435. package/dist/core/ingestion/import-resolvers/configs/jvm.js +68 -0
  436. package/dist/core/ingestion/import-resolvers/configs/php.d.ts +8 -0
  437. package/dist/core/ingestion/import-resolvers/configs/php.js +15 -0
  438. package/dist/core/ingestion/import-resolvers/configs/python.d.ts +12 -0
  439. package/dist/core/ingestion/import-resolvers/configs/python.js +41 -0
  440. package/dist/core/ingestion/import-resolvers/configs/ruby.d.ts +8 -0
  441. package/dist/core/ingestion/import-resolvers/configs/ruby.js +16 -0
  442. package/dist/core/ingestion/import-resolvers/configs/rust.d.ts +8 -0
  443. package/dist/core/ingestion/import-resolvers/configs/rust.js +54 -0
  444. package/dist/core/ingestion/import-resolvers/configs/swift.d.ts +8 -0
  445. package/dist/core/ingestion/import-resolvers/configs/swift.js +29 -0
  446. package/dist/core/ingestion/import-resolvers/configs/typescript-javascript.d.ts +9 -0
  447. package/dist/core/ingestion/import-resolvers/configs/typescript-javascript.js +23 -0
  448. package/dist/core/ingestion/import-resolvers/csharp.d.ts +18 -0
  449. package/dist/core/ingestion/import-resolvers/csharp.js +115 -0
  450. package/dist/core/ingestion/import-resolvers/go.d.ts +17 -0
  451. package/dist/core/ingestion/import-resolvers/go.js +46 -0
  452. package/dist/core/ingestion/import-resolvers/jvm.d.ts +27 -0
  453. package/dist/core/ingestion/import-resolvers/jvm.js +106 -0
  454. package/dist/core/ingestion/import-resolvers/php.d.ts +24 -0
  455. package/dist/core/ingestion/import-resolvers/php.js +77 -0
  456. package/dist/core/ingestion/import-resolvers/python.d.ts +22 -0
  457. package/dist/core/ingestion/import-resolvers/python.js +72 -0
  458. package/dist/core/ingestion/import-resolvers/resolver-factory.d.ts +24 -0
  459. package/dist/core/ingestion/import-resolvers/resolver-factory.js +33 -0
  460. package/dist/core/ingestion/import-resolvers/ruby.d.ts +14 -0
  461. package/dist/core/ingestion/import-resolvers/ruby.js +17 -0
  462. package/dist/core/ingestion/import-resolvers/rust.d.ts +17 -0
  463. package/dist/core/ingestion/import-resolvers/rust.js +75 -0
  464. package/dist/core/ingestion/import-resolvers/standard.d.ts +30 -0
  465. package/dist/core/ingestion/import-resolvers/standard.js +142 -0
  466. package/dist/core/ingestion/import-resolvers/types.d.ts +68 -0
  467. package/dist/core/ingestion/import-resolvers/types.js +6 -0
  468. package/dist/core/ingestion/import-resolvers/utils.d.ts +35 -0
  469. package/dist/core/ingestion/import-resolvers/utils.js +149 -0
  470. package/dist/core/ingestion/import-target-adapter.d.ts +73 -0
  471. package/dist/core/ingestion/import-target-adapter.js +95 -0
  472. package/dist/core/ingestion/language-config.d.ts +52 -0
  473. package/dist/core/ingestion/language-config.js +181 -0
  474. package/dist/core/ingestion/language-provider.d.ts +410 -0
  475. package/dist/core/ingestion/language-provider.js +24 -0
  476. package/dist/core/ingestion/languages/c-cpp.d.ts +12 -0
  477. package/dist/core/ingestion/languages/c-cpp.js +329 -0
  478. package/dist/core/ingestion/languages/cobol.d.ts +1 -0
  479. package/dist/core/ingestion/languages/cobol.js +26 -0
  480. package/dist/core/ingestion/languages/csharp/accessor-unwrap.d.ts +21 -0
  481. package/dist/core/ingestion/languages/csharp/accessor-unwrap.js +56 -0
  482. package/dist/core/ingestion/languages/csharp/arity-metadata.d.ts +26 -0
  483. package/dist/core/ingestion/languages/csharp/arity-metadata.js +46 -0
  484. package/dist/core/ingestion/languages/csharp/arity.d.ts +23 -0
  485. package/dist/core/ingestion/languages/csharp/arity.js +37 -0
  486. package/dist/core/ingestion/languages/csharp/cache-stats.d.ts +15 -0
  487. package/dist/core/ingestion/languages/csharp/cache-stats.js +26 -0
  488. package/dist/core/ingestion/languages/csharp/captures.d.ts +19 -0
  489. package/dist/core/ingestion/languages/csharp/captures.js +249 -0
  490. package/dist/core/ingestion/languages/csharp/import-decomposer.d.ts +19 -0
  491. package/dist/core/ingestion/languages/csharp/import-decomposer.js +93 -0
  492. package/dist/core/ingestion/languages/csharp/import-target.d.ts +25 -0
  493. package/dist/core/ingestion/languages/csharp/import-target.js +123 -0
  494. package/dist/core/ingestion/languages/csharp/index.d.ts +82 -0
  495. package/dist/core/ingestion/languages/csharp/index.js +82 -0
  496. package/dist/core/ingestion/languages/csharp/interpret.d.ts +15 -0
  497. package/dist/core/ingestion/languages/csharp/interpret.js +132 -0
  498. package/dist/core/ingestion/languages/csharp/merge-bindings.d.ts +27 -0
  499. package/dist/core/ingestion/languages/csharp/merge-bindings.js +55 -0
  500. package/dist/core/ingestion/languages/csharp/namespace-siblings.d.ts +50 -0
  501. package/dist/core/ingestion/languages/csharp/namespace-siblings.js +374 -0
  502. package/dist/core/ingestion/languages/csharp/query.d.ts +35 -0
  503. package/dist/core/ingestion/languages/csharp/query.js +515 -0
  504. package/dist/core/ingestion/languages/csharp/receiver-binding.d.ts +31 -0
  505. package/dist/core/ingestion/languages/csharp/receiver-binding.js +135 -0
  506. package/dist/core/ingestion/languages/csharp/scope-resolver.d.ts +10 -0
  507. package/dist/core/ingestion/languages/csharp/scope-resolver.js +63 -0
  508. package/dist/core/ingestion/languages/csharp/simple-hooks.d.ts +53 -0
  509. package/dist/core/ingestion/languages/csharp/simple-hooks.js +76 -0
  510. package/dist/core/ingestion/languages/csharp.d.ts +8 -0
  511. package/dist/core/ingestion/languages/csharp.js +152 -0
  512. package/dist/core/ingestion/languages/dart.d.ts +12 -0
  513. package/dist/core/ingestion/languages/dart.js +102 -0
  514. package/dist/core/ingestion/languages/go.d.ts +11 -0
  515. package/dist/core/ingestion/languages/go.js +44 -0
  516. package/dist/core/ingestion/languages/index.d.ts +39 -0
  517. package/dist/core/ingestion/languages/index.js +64 -0
  518. package/dist/core/ingestion/languages/java.d.ts +9 -0
  519. package/dist/core/ingestion/languages/java.js +44 -0
  520. package/dist/core/ingestion/languages/kotlin.d.ts +9 -0
  521. package/dist/core/ingestion/languages/kotlin.js +123 -0
  522. package/dist/core/ingestion/languages/php.d.ts +8 -0
  523. package/dist/core/ingestion/languages/php.js +240 -0
  524. package/dist/core/ingestion/languages/python/arity-metadata.d.ts +24 -0
  525. package/dist/core/ingestion/languages/python/arity-metadata.js +45 -0
  526. package/dist/core/ingestion/languages/python/arity.d.ts +22 -0
  527. package/dist/core/ingestion/languages/python/arity.js +38 -0
  528. package/dist/core/ingestion/languages/python/cache-stats.d.ts +17 -0
  529. package/dist/core/ingestion/languages/python/cache-stats.js +28 -0
  530. package/dist/core/ingestion/languages/python/captures.d.ts +19 -0
  531. package/dist/core/ingestion/languages/python/captures.js +106 -0
  532. package/dist/core/ingestion/languages/python/import-decomposer.d.ts +15 -0
  533. package/dist/core/ingestion/languages/python/import-decomposer.js +112 -0
  534. package/dist/core/ingestion/languages/python/import-target.d.ts +21 -0
  535. package/dist/core/ingestion/languages/python/import-target.js +99 -0
  536. package/dist/core/ingestion/languages/python/index.d.ts +80 -0
  537. package/dist/core/ingestion/languages/python/index.js +80 -0
  538. package/dist/core/ingestion/languages/python/interpret.d.ts +15 -0
  539. package/dist/core/ingestion/languages/python/interpret.js +191 -0
  540. package/dist/core/ingestion/languages/python/merge-bindings.d.ts +16 -0
  541. package/dist/core/ingestion/languages/python/merge-bindings.js +44 -0
  542. package/dist/core/ingestion/languages/python/query.d.ts +9 -0
  543. package/dist/core/ingestion/languages/python/query.js +267 -0
  544. package/dist/core/ingestion/languages/python/receiver-binding.d.ts +21 -0
  545. package/dist/core/ingestion/languages/python/receiver-binding.js +116 -0
  546. package/dist/core/ingestion/languages/python/scope-resolver.d.ts +16 -0
  547. package/dist/core/ingestion/languages/python/scope-resolver.js +53 -0
  548. package/dist/core/ingestion/languages/python/simple-hooks.d.ts +23 -0
  549. package/dist/core/ingestion/languages/python/simple-hooks.js +35 -0
  550. package/dist/core/ingestion/languages/python.d.ts +12 -0
  551. package/dist/core/ingestion/languages/python.js +91 -0
  552. package/dist/core/ingestion/languages/ruby.d.ts +9 -0
  553. package/dist/core/ingestion/languages/ruby.js +210 -0
  554. package/dist/core/ingestion/languages/rust.d.ts +12 -0
  555. package/dist/core/ingestion/languages/rust.js +132 -0
  556. package/dist/core/ingestion/languages/swift.d.ts +12 -0
  557. package/dist/core/ingestion/languages/swift.js +244 -0
  558. package/dist/core/ingestion/languages/typescript.d.ts +11 -0
  559. package/dist/core/ingestion/languages/typescript.js +184 -0
  560. package/dist/core/ingestion/languages/vue.d.ts +13 -0
  561. package/dist/core/ingestion/languages/vue.js +77 -0
  562. package/dist/core/ingestion/markdown-processor.d.ts +17 -0
  563. package/dist/core/ingestion/markdown-processor.js +124 -0
  564. package/dist/core/ingestion/method-extractors/configs/c-cpp.d.ts +3 -0
  565. package/dist/core/ingestion/method-extractors/configs/c-cpp.js +387 -0
  566. package/dist/core/ingestion/method-extractors/configs/csharp.d.ts +2 -0
  567. package/dist/core/ingestion/method-extractors/configs/csharp.js +287 -0
  568. package/dist/core/ingestion/method-extractors/configs/dart.d.ts +2 -0
  569. package/dist/core/ingestion/method-extractors/configs/dart.js +376 -0
  570. package/dist/core/ingestion/method-extractors/configs/go.d.ts +2 -0
  571. package/dist/core/ingestion/method-extractors/configs/go.js +176 -0
  572. package/dist/core/ingestion/method-extractors/configs/jvm.d.ts +3 -0
  573. package/dist/core/ingestion/method-extractors/configs/jvm.js +336 -0
  574. package/dist/core/ingestion/method-extractors/configs/php.d.ts +2 -0
  575. package/dist/core/ingestion/method-extractors/configs/php.js +304 -0
  576. package/dist/core/ingestion/method-extractors/configs/python.d.ts +2 -0
  577. package/dist/core/ingestion/method-extractors/configs/python.js +309 -0
  578. package/dist/core/ingestion/method-extractors/configs/ruby.d.ts +2 -0
  579. package/dist/core/ingestion/method-extractors/configs/ruby.js +286 -0
  580. package/dist/core/ingestion/method-extractors/configs/rust.d.ts +2 -0
  581. package/dist/core/ingestion/method-extractors/configs/rust.js +195 -0
  582. package/dist/core/ingestion/method-extractors/configs/swift.d.ts +2 -0
  583. package/dist/core/ingestion/method-extractors/configs/swift.js +277 -0
  584. package/dist/core/ingestion/method-extractors/configs/typescript-javascript.d.ts +3 -0
  585. package/dist/core/ingestion/method-extractors/configs/typescript-javascript.js +338 -0
  586. package/dist/core/ingestion/method-extractors/generic.d.ts +11 -0
  587. package/dist/core/ingestion/method-extractors/generic.js +204 -0
  588. package/dist/core/ingestion/method-types.d.ts +90 -0
  589. package/dist/core/ingestion/method-types.js +2 -0
  590. package/dist/core/ingestion/model/field-registry.d.ts +18 -0
  591. package/dist/core/ingestion/model/field-registry.js +22 -0
  592. package/dist/core/ingestion/model/heritage-map.d.ts +105 -0
  593. package/dist/core/ingestion/model/heritage-map.js +260 -0
  594. package/dist/core/ingestion/model/index.d.ts +20 -0
  595. package/dist/core/ingestion/model/index.js +43 -0
  596. package/dist/core/ingestion/model/method-registry.d.ts +71 -0
  597. package/dist/core/ingestion/model/method-registry.js +134 -0
  598. package/dist/core/ingestion/model/registration-table.d.ts +138 -0
  599. package/dist/core/ingestion/model/registration-table.js +224 -0
  600. package/dist/core/ingestion/model/resolution-context.d.ts +93 -0
  601. package/dist/core/ingestion/model/resolution-context.js +337 -0
  602. package/dist/core/ingestion/model/resolve.d.ts +61 -0
  603. package/dist/core/ingestion/model/resolve.js +381 -0
  604. package/dist/core/ingestion/model/scope-resolution-indexes.d.ts +59 -0
  605. package/dist/core/ingestion/model/scope-resolution-indexes.js +42 -0
  606. package/dist/core/ingestion/model/semantic-model.d.ts +150 -0
  607. package/dist/core/ingestion/model/semantic-model.js +175 -0
  608. package/dist/core/ingestion/model/symbol-table.d.ts +200 -0
  609. package/dist/core/ingestion/model/symbol-table.js +206 -0
  610. package/dist/core/ingestion/model/type-registry.d.ts +39 -0
  611. package/dist/core/ingestion/model/type-registry.js +62 -0
  612. package/dist/core/ingestion/mro-processor.d.ts +46 -0
  613. package/dist/core/ingestion/mro-processor.js +597 -0
  614. package/dist/core/ingestion/named-bindings/csharp.d.ts +3 -0
  615. package/dist/core/ingestion/named-bindings/csharp.js +37 -0
  616. package/dist/core/ingestion/named-bindings/java.d.ts +3 -0
  617. package/dist/core/ingestion/named-bindings/java.js +29 -0
  618. package/dist/core/ingestion/named-bindings/kotlin.d.ts +3 -0
  619. package/dist/core/ingestion/named-bindings/kotlin.js +36 -0
  620. package/dist/core/ingestion/named-bindings/php.d.ts +3 -0
  621. package/dist/core/ingestion/named-bindings/php.js +61 -0
  622. package/dist/core/ingestion/named-bindings/python.d.ts +3 -0
  623. package/dist/core/ingestion/named-bindings/python.js +49 -0
  624. package/dist/core/ingestion/named-bindings/rust.d.ts +3 -0
  625. package/dist/core/ingestion/named-bindings/rust.js +66 -0
  626. package/dist/core/ingestion/named-bindings/types.d.ts +16 -0
  627. package/dist/core/ingestion/named-bindings/types.js +6 -0
  628. package/dist/core/ingestion/named-bindings/typescript.d.ts +3 -0
  629. package/dist/core/ingestion/named-bindings/typescript.js +58 -0
  630. package/dist/core/ingestion/parsing-processor.d.ts +40 -0
  631. package/dist/core/ingestion/parsing-processor.js +576 -0
  632. package/dist/core/ingestion/pipeline-phases/cobol.d.ts +16 -0
  633. package/dist/core/ingestion/pipeline-phases/cobol.js +45 -0
  634. package/dist/core/ingestion/pipeline-phases/communities.d.ts +16 -0
  635. package/dist/core/ingestion/pipeline-phases/communities.js +62 -0
  636. package/dist/core/ingestion/pipeline-phases/cross-file-impl.d.ts +17 -0
  637. package/dist/core/ingestion/pipeline-phases/cross-file-impl.js +156 -0
  638. package/dist/core/ingestion/pipeline-phases/cross-file.d.ts +37 -0
  639. package/dist/core/ingestion/pipeline-phases/cross-file.js +63 -0
  640. package/dist/core/ingestion/pipeline-phases/index.d.ts +22 -0
  641. package/dist/core/ingestion/pipeline-phases/index.js +23 -0
  642. package/dist/core/ingestion/pipeline-phases/markdown.d.ts +17 -0
  643. package/dist/core/ingestion/pipeline-phases/markdown.js +33 -0
  644. package/dist/core/ingestion/pipeline-phases/mro.d.ts +18 -0
  645. package/dist/core/ingestion/pipeline-phases/mro.js +36 -0
  646. package/dist/core/ingestion/pipeline-phases/orm-extraction.d.ts +22 -0
  647. package/dist/core/ingestion/pipeline-phases/orm-extraction.js +92 -0
  648. package/dist/core/ingestion/pipeline-phases/orm.d.ts +15 -0
  649. package/dist/core/ingestion/pipeline-phases/orm.js +74 -0
  650. package/dist/core/ingestion/pipeline-phases/parse-impl.d.ts +58 -0
  651. package/dist/core/ingestion/pipeline-phases/parse-impl.js +458 -0
  652. package/dist/core/ingestion/pipeline-phases/parse.d.ts +74 -0
  653. package/dist/core/ingestion/pipeline-phases/parse.js +33 -0
  654. package/dist/core/ingestion/pipeline-phases/processes.d.ts +16 -0
  655. package/dist/core/ingestion/pipeline-phases/processes.js +143 -0
  656. package/dist/core/ingestion/pipeline-phases/routes.d.ts +21 -0
  657. package/dist/core/ingestion/pipeline-phases/routes.js +243 -0
  658. package/dist/core/ingestion/pipeline-phases/runner.d.ts +22 -0
  659. package/dist/core/ingestion/pipeline-phases/runner.js +203 -0
  660. package/dist/core/ingestion/pipeline-phases/scan.d.ts +21 -0
  661. package/dist/core/ingestion/pipeline-phases/scan.js +46 -0
  662. package/dist/core/ingestion/pipeline-phases/structure.d.ts +27 -0
  663. package/dist/core/ingestion/pipeline-phases/structure.js +35 -0
  664. package/dist/core/ingestion/pipeline-phases/tools.d.ts +20 -0
  665. package/dist/core/ingestion/pipeline-phases/tools.js +79 -0
  666. package/dist/core/ingestion/pipeline-phases/types.d.ts +79 -0
  667. package/dist/core/ingestion/pipeline-phases/types.js +37 -0
  668. package/dist/core/ingestion/pipeline-phases/wildcard-synthesis.d.ts +70 -0
  669. package/dist/core/ingestion/pipeline-phases/wildcard-synthesis.js +312 -0
  670. package/dist/core/ingestion/pipeline.d.ts +36 -0
  671. package/dist/core/ingestion/pipeline.js +89 -0
  672. package/dist/core/ingestion/process-processor.d.ts +51 -0
  673. package/dist/core/ingestion/process-processor.js +317 -0
  674. package/dist/core/ingestion/registry-primary-flag.d.ts +86 -0
  675. package/dist/core/ingestion/registry-primary-flag.js +111 -0
  676. package/dist/core/ingestion/resolve-references.d.ts +63 -0
  677. package/dist/core/ingestion/resolve-references.js +175 -0
  678. package/dist/core/ingestion/route-extractors/expo.d.ts +1 -0
  679. package/dist/core/ingestion/route-extractors/expo.js +36 -0
  680. package/dist/core/ingestion/route-extractors/middleware.d.ts +47 -0
  681. package/dist/core/ingestion/route-extractors/middleware.js +167 -0
  682. package/dist/core/ingestion/route-extractors/nextjs.d.ts +3 -0
  683. package/dist/core/ingestion/route-extractors/nextjs.js +76 -0
  684. package/dist/core/ingestion/route-extractors/php.d.ts +7 -0
  685. package/dist/core/ingestion/route-extractors/php.js +22 -0
  686. package/dist/core/ingestion/route-extractors/response-shapes.d.ts +20 -0
  687. package/dist/core/ingestion/route-extractors/response-shapes.js +294 -0
  688. package/dist/core/ingestion/scope-extractor-bridge.d.ts +32 -0
  689. package/dist/core/ingestion/scope-extractor-bridge.js +44 -0
  690. package/dist/core/ingestion/scope-extractor.d.ts +86 -0
  691. package/dist/core/ingestion/scope-extractor.js +758 -0
  692. package/dist/core/ingestion/scope-resolution/contract/scope-resolver.d.ts +372 -0
  693. package/dist/core/ingestion/scope-resolution/contract/scope-resolver.js +212 -0
  694. package/dist/core/ingestion/scope-resolution/graph-bridge/edges.d.ts +43 -0
  695. package/dist/core/ingestion/scope-resolution/graph-bridge/edges.js +79 -0
  696. package/dist/core/ingestion/scope-resolution/graph-bridge/ids.d.ts +57 -0
  697. package/dist/core/ingestion/scope-resolution/graph-bridge/ids.js +112 -0
  698. package/dist/core/ingestion/scope-resolution/graph-bridge/imports-to-edges.d.ts +17 -0
  699. package/dist/core/ingestion/scope-resolution/graph-bridge/imports-to-edges.js +46 -0
  700. package/dist/core/ingestion/scope-resolution/graph-bridge/method-dispatch.d.ts +19 -0
  701. package/dist/core/ingestion/scope-resolution/graph-bridge/method-dispatch.js +30 -0
  702. package/dist/core/ingestion/scope-resolution/graph-bridge/node-lookup.d.ts +37 -0
  703. package/dist/core/ingestion/scope-resolution/graph-bridge/node-lookup.js +113 -0
  704. package/dist/core/ingestion/scope-resolution/graph-bridge/references-to-edges.d.ts +38 -0
  705. package/dist/core/ingestion/scope-resolution/graph-bridge/references-to-edges.js +73 -0
  706. package/dist/core/ingestion/scope-resolution/passes/compound-receiver.d.ts +42 -0
  707. package/dist/core/ingestion/scope-resolution/passes/compound-receiver.js +198 -0
  708. package/dist/core/ingestion/scope-resolution/passes/free-call-fallback.d.ts +27 -0
  709. package/dist/core/ingestion/scope-resolution/passes/free-call-fallback.js +131 -0
  710. package/dist/core/ingestion/scope-resolution/passes/imported-return-types.d.ts +48 -0
  711. package/dist/core/ingestion/scope-resolution/passes/imported-return-types.js +130 -0
  712. package/dist/core/ingestion/scope-resolution/passes/mro.d.ts +42 -0
  713. package/dist/core/ingestion/scope-resolution/passes/mro.js +99 -0
  714. package/dist/core/ingestion/scope-resolution/passes/overload-narrowing.d.ts +26 -0
  715. package/dist/core/ingestion/scope-resolution/passes/overload-narrowing.js +61 -0
  716. package/dist/core/ingestion/scope-resolution/passes/receiver-bound-calls.d.ts +46 -0
  717. package/dist/core/ingestion/scope-resolution/passes/receiver-bound-calls.js +327 -0
  718. package/dist/core/ingestion/scope-resolution/pipeline/phase.d.ts +47 -0
  719. package/dist/core/ingestion/scope-resolution/pipeline/phase.js +130 -0
  720. package/dist/core/ingestion/scope-resolution/pipeline/reconcile-ownership.d.ts +68 -0
  721. package/dist/core/ingestion/scope-resolution/pipeline/reconcile-ownership.js +125 -0
  722. package/dist/core/ingestion/scope-resolution/pipeline/registry.d.ts +17 -0
  723. package/dist/core/ingestion/scope-resolution/pipeline/registry.js +21 -0
  724. package/dist/core/ingestion/scope-resolution/pipeline/run.d.ts +66 -0
  725. package/dist/core/ingestion/scope-resolution/pipeline/run.js +157 -0
  726. package/dist/core/ingestion/scope-resolution/scope/namespace-targets.d.ts +36 -0
  727. package/dist/core/ingestion/scope-resolution/scope/namespace-targets.js +52 -0
  728. package/dist/core/ingestion/scope-resolution/scope/walkers.d.ts +127 -0
  729. package/dist/core/ingestion/scope-resolution/scope/walkers.js +349 -0
  730. package/dist/core/ingestion/scope-resolution/workspace-index.d.ts +52 -0
  731. package/dist/core/ingestion/scope-resolution/workspace-index.js +61 -0
  732. package/dist/core/ingestion/shadow-harness.d.ts +113 -0
  733. package/dist/core/ingestion/shadow-harness.js +148 -0
  734. package/dist/core/ingestion/structure-processor.d.ts +2 -0
  735. package/dist/core/ingestion/structure-processor.js +36 -0
  736. package/dist/core/ingestion/tree-sitter-queries.d.ts +16 -0
  737. package/dist/core/ingestion/tree-sitter-queries.js +1338 -0
  738. package/dist/core/ingestion/type-env.d.ts +86 -0
  739. package/dist/core/ingestion/type-env.js +1128 -0
  740. package/dist/core/ingestion/type-extractors/c-cpp.d.ts +7 -0
  741. package/dist/core/ingestion/type-extractors/c-cpp.js +532 -0
  742. package/dist/core/ingestion/type-extractors/csharp.d.ts +2 -0
  743. package/dist/core/ingestion/type-extractors/csharp.js +583 -0
  744. package/dist/core/ingestion/type-extractors/dart.d.ts +15 -0
  745. package/dist/core/ingestion/type-extractors/dart.js +369 -0
  746. package/dist/core/ingestion/type-extractors/go.d.ts +2 -0
  747. package/dist/core/ingestion/type-extractors/go.js +513 -0
  748. package/dist/core/ingestion/type-extractors/jvm.d.ts +3 -0
  749. package/dist/core/ingestion/type-extractors/jvm.js +856 -0
  750. package/dist/core/ingestion/type-extractors/php.d.ts +2 -0
  751. package/dist/core/ingestion/type-extractors/php.js +534 -0
  752. package/dist/core/ingestion/type-extractors/python.d.ts +2 -0
  753. package/dist/core/ingestion/type-extractors/python.js +474 -0
  754. package/dist/core/ingestion/type-extractors/ruby.d.ts +2 -0
  755. package/dist/core/ingestion/type-extractors/ruby.js +377 -0
  756. package/dist/core/ingestion/type-extractors/rust.d.ts +2 -0
  757. package/dist/core/ingestion/type-extractors/rust.js +515 -0
  758. package/dist/core/ingestion/type-extractors/shared.d.ts +131 -0
  759. package/dist/core/ingestion/type-extractors/shared.js +796 -0
  760. package/dist/core/ingestion/type-extractors/swift.d.ts +2 -0
  761. package/dist/core/ingestion/type-extractors/swift.js +484 -0
  762. package/dist/core/ingestion/type-extractors/types.d.ts +172 -0
  763. package/dist/core/ingestion/type-extractors/types.js +1 -0
  764. package/dist/core/ingestion/type-extractors/typescript.d.ts +2 -0
  765. package/dist/core/ingestion/type-extractors/typescript.js +661 -0
  766. package/dist/core/ingestion/utils/ast-helpers.d.ts +89 -0
  767. package/dist/core/ingestion/utils/ast-helpers.js +535 -0
  768. package/dist/core/ingestion/utils/call-analysis.d.ts +75 -0
  769. package/dist/core/ingestion/utils/call-analysis.js +574 -0
  770. package/dist/core/ingestion/utils/env.d.ts +10 -0
  771. package/dist/core/ingestion/utils/env.js +10 -0
  772. package/dist/core/ingestion/utils/event-loop.d.ts +5 -0
  773. package/dist/core/ingestion/utils/event-loop.js +5 -0
  774. package/dist/core/ingestion/utils/graph-sort.d.ts +58 -0
  775. package/dist/core/ingestion/utils/graph-sort.js +100 -0
  776. package/dist/core/ingestion/utils/max-file-size.d.ts +20 -0
  777. package/dist/core/ingestion/utils/max-file-size.js +52 -0
  778. package/dist/core/ingestion/utils/method-props.d.ts +32 -0
  779. package/dist/core/ingestion/utils/method-props.js +147 -0
  780. package/dist/core/ingestion/utils/ruby-self-call.d.ts +52 -0
  781. package/dist/core/ingestion/utils/ruby-self-call.js +59 -0
  782. package/dist/core/ingestion/utils/verbose.d.ts +1 -0
  783. package/dist/core/ingestion/utils/verbose.js +7 -0
  784. package/dist/core/ingestion/variable-extractors/configs/c-cpp.d.ts +3 -0
  785. package/dist/core/ingestion/variable-extractors/configs/c-cpp.js +81 -0
  786. package/dist/core/ingestion/variable-extractors/configs/csharp.d.ts +9 -0
  787. package/dist/core/ingestion/variable-extractors/configs/csharp.js +63 -0
  788. package/dist/core/ingestion/variable-extractors/configs/dart.d.ts +2 -0
  789. package/dist/core/ingestion/variable-extractors/configs/dart.js +94 -0
  790. package/dist/core/ingestion/variable-extractors/configs/go.d.ts +2 -0
  791. package/dist/core/ingestion/variable-extractors/configs/go.js +83 -0
  792. package/dist/core/ingestion/variable-extractors/configs/jvm.d.ts +18 -0
  793. package/dist/core/ingestion/variable-extractors/configs/jvm.js +115 -0
  794. package/dist/core/ingestion/variable-extractors/configs/php.d.ts +14 -0
  795. package/dist/core/ingestion/variable-extractors/configs/php.js +58 -0
  796. package/dist/core/ingestion/variable-extractors/configs/python.d.ts +2 -0
  797. package/dist/core/ingestion/variable-extractors/configs/python.js +101 -0
  798. package/dist/core/ingestion/variable-extractors/configs/ruby.d.ts +11 -0
  799. package/dist/core/ingestion/variable-extractors/configs/ruby.js +52 -0
  800. package/dist/core/ingestion/variable-extractors/configs/rust.d.ts +2 -0
  801. package/dist/core/ingestion/variable-extractors/configs/rust.js +76 -0
  802. package/dist/core/ingestion/variable-extractors/configs/swift.d.ts +2 -0
  803. package/dist/core/ingestion/variable-extractors/configs/swift.js +88 -0
  804. package/dist/core/ingestion/variable-extractors/configs/typescript-javascript.d.ts +3 -0
  805. package/dist/core/ingestion/variable-extractors/configs/typescript-javascript.js +83 -0
  806. package/dist/core/ingestion/variable-extractors/generic.d.ts +5 -0
  807. package/dist/core/ingestion/variable-extractors/generic.js +80 -0
  808. package/dist/core/ingestion/variable-types.d.ts +82 -0
  809. package/dist/core/ingestion/variable-types.js +2 -0
  810. package/dist/core/ingestion/vue-sfc-extractor.d.ts +44 -0
  811. package/dist/core/ingestion/vue-sfc-extractor.js +94 -0
  812. package/dist/core/ingestion/workers/parse-worker.d.ts +198 -0
  813. package/dist/core/ingestion/workers/parse-worker.js +1928 -0
  814. package/dist/core/ingestion/workers/worker-pool.d.ts +16 -0
  815. package/dist/core/ingestion/workers/worker-pool.js +126 -0
  816. package/dist/core/lbug/csv-generator.d.ts +33 -0
  817. package/dist/core/lbug/csv-generator.js +459 -0
  818. package/dist/core/lbug/lbug-adapter.d.ts +173 -0
  819. package/dist/core/lbug/lbug-adapter.js +1188 -0
  820. package/dist/core/lbug/pool-adapter.d.ts +93 -0
  821. package/dist/core/lbug/pool-adapter.js +543 -0
  822. package/dist/core/lbug/schema.d.ts +62 -0
  823. package/dist/core/lbug/schema.js +484 -0
  824. package/dist/core/run-analyze.d.ts +72 -0
  825. package/dist/core/run-analyze.js +315 -0
  826. package/dist/core/search/bm25-index.d.ts +41 -0
  827. package/dist/core/search/bm25-index.js +209 -0
  828. package/dist/core/search/hybrid-search.d.ts +49 -0
  829. package/dist/core/search/hybrid-search.js +118 -0
  830. package/dist/core/search/phase-timer.d.ts +72 -0
  831. package/dist/core/search/phase-timer.js +106 -0
  832. package/dist/core/tree-sitter/parser-loader.d.ts +8 -0
  833. package/dist/core/tree-sitter/parser-loader.js +84 -0
  834. package/dist/core/wiki/cursor-client.d.ts +31 -0
  835. package/dist/core/wiki/cursor-client.js +122 -0
  836. package/dist/core/wiki/generator.d.ts +129 -0
  837. package/dist/core/wiki/generator.js +898 -0
  838. package/dist/core/wiki/graph-queries.d.ts +84 -0
  839. package/dist/core/wiki/graph-queries.js +244 -0
  840. package/dist/core/wiki/html-viewer.d.ts +10 -0
  841. package/dist/core/wiki/html-viewer.js +303 -0
  842. package/dist/core/wiki/llm-client.d.ts +63 -0
  843. package/dist/core/wiki/llm-client.js +234 -0
  844. package/dist/core/wiki/prompts.d.ts +53 -0
  845. package/dist/core/wiki/prompts.js +181 -0
  846. package/dist/lib/utils.d.ts +1 -0
  847. package/dist/lib/utils.js +3 -0
  848. package/dist/mcp/compatible-stdio-transport.d.ts +25 -0
  849. package/dist/mcp/compatible-stdio-transport.js +200 -0
  850. package/dist/mcp/core/embedder.d.ts +27 -0
  851. package/dist/mcp/core/embedder.js +122 -0
  852. package/dist/mcp/core/lbug-adapter.d.ts +5 -0
  853. package/dist/mcp/core/lbug-adapter.js +5 -0
  854. package/dist/mcp/local/graphstore-handler.d.ts +214 -0
  855. package/dist/mcp/local/graphstore-handler.js +272 -0
  856. package/dist/mcp/local/local-backend.d.ts +347 -0
  857. package/dist/mcp/local/local-backend.js +3218 -0
  858. package/dist/mcp/resources.d.ts +62 -0
  859. package/dist/mcp/resources.js +696 -0
  860. package/dist/mcp/server.d.ts +23 -0
  861. package/dist/mcp/server.js +533 -0
  862. package/dist/mcp/staleness.d.ts +5 -0
  863. package/dist/mcp/staleness.js +4 -0
  864. package/dist/mcp/tools.d.ts +27 -0
  865. package/dist/mcp/tools.js +823 -0
  866. package/dist/server/analyze-job.d.ts +55 -0
  867. package/dist/server/analyze-job.js +150 -0
  868. package/dist/server/analyze-worker.d.ts +13 -0
  869. package/dist/server/analyze-worker.js +59 -0
  870. package/dist/server/api.d.ts +47 -0
  871. package/dist/server/api.js +1727 -0
  872. package/dist/server/git-clone.d.ts +26 -0
  873. package/dist/server/git-clone.js +184 -0
  874. package/dist/server/mcp-http.d.ts +13 -0
  875. package/dist/server/mcp-http.js +100 -0
  876. package/dist/storage/git.d.ts +80 -0
  877. package/dist/storage/git.js +190 -0
  878. package/dist/storage/repo-manager.d.ts +458 -0
  879. package/dist/storage/repo-manager.js +766 -0
  880. package/dist/types/pipeline.d.ts +18 -0
  881. package/dist/types/pipeline.js +1 -0
  882. package/hooks/claude/codragraph-hook.cjs +268 -0
  883. package/hooks/claude/pre-tool-use.sh +79 -0
  884. package/hooks/claude/session-start.sh +42 -0
  885. package/package.json +127 -0
  886. package/scripts/bench-scope-resolution.ts +134 -0
  887. package/scripts/build-tree-sitter-proto.cjs +82 -0
  888. package/scripts/build.js +90 -0
  889. package/scripts/ci-list-migrated-languages.ts +24 -0
  890. package/scripts/patch-tree-sitter-swift.cjs +78 -0
  891. package/skills/codragraph-cli.md +82 -0
  892. package/skills/codragraph-debugging.md +89 -0
  893. package/skills/codragraph-exploring.md +78 -0
  894. package/skills/codragraph-guide.md +64 -0
  895. package/skills/codragraph-impact-analysis.md +97 -0
  896. package/skills/codragraph-pr-review.md +163 -0
  897. package/skills/codragraph-refactoring.md +121 -0
  898. package/vendor/leiden/index.cjs +355 -0
  899. package/vendor/leiden/utils.cjs +392 -0
  900. package/vendor/tree-sitter-proto/binding.gyp +30 -0
  901. package/vendor/tree-sitter-proto/bindings/node/binding.cc +20 -0
  902. package/vendor/tree-sitter-proto/bindings/node/index.d.ts +28 -0
  903. package/vendor/tree-sitter-proto/bindings/node/index.js +7 -0
  904. package/vendor/tree-sitter-proto/package.json +12 -0
  905. package/vendor/tree-sitter-proto/src/node-types.json +1145 -0
  906. package/vendor/tree-sitter-proto/src/parser.c +10149 -0
  907. package/vendor/tree-sitter-proto/src/tree_sitter/alloc.h +54 -0
  908. package/vendor/tree-sitter-proto/src/tree_sitter/array.h +291 -0
  909. package/vendor/tree-sitter-proto/src/tree_sitter/parser.h +266 -0
@@ -0,0 +1,3218 @@
1
+ /**
2
+ * Local Backend (Multi-Repo)
3
+ *
4
+ * Provides tool implementations using local .codragraph/ indexes.
5
+ * Supports multiple indexed repositories via a global registry.
6
+ * LadybugDB connections are opened lazily per repo on first query.
7
+ */
8
+ import fs from 'fs/promises';
9
+ import path from 'path';
10
+ import { initLbug, executeQuery, executeParameterized, closeLbug, isLbugReady, isWriteQuery, } from '../../core/lbug/pool-adapter.js';
11
+ export { isWriteQuery };
12
+ // Embedding imports are lazy (dynamic import) to avoid loading onnxruntime-node
13
+ // at MCP server startup — crashes on unsupported Node ABI versions (#89)
14
+ // git utilities available if needed
15
+ // import { isGitRepo, getCurrentCommit, getGitRoot } from '../../storage/git.js';
16
+ import { parseDiffHunks } from '../../storage/git.js';
17
+ import { listRegisteredRepos, cleanupOldKuzuFiles, } from '../../storage/repo-manager.js';
18
+ import { GroupService } from '../../core/group/service.js';
19
+ import { resolveAtGroupMemberRepoPath } from '../../core/group/resolve-at-member.js';
20
+ import { collectBestChunks } from '../../core/embeddings/types.js';
21
+ import { EMBEDDING_TABLE_NAME, EMBEDDING_INDEX_NAME } from '../../core/lbug/schema.js';
22
+ import { PhaseTimer } from '../../core/search/phase-timer.js';
23
+ import { checkStaleness, checkCwdMatch } from '../../core/git-staleness.js';
24
+ // AI context generation is CLI-only (codragraph analyze)
25
+ // import { generateAIContextFiles } from '../../cli/ai-context.js';
26
+ /**
27
+ * Quick test-file detection for filtering impact results.
28
+ * Matches common test file patterns across all supported languages.
29
+ */
30
+ export function isTestFilePath(filePath) {
31
+ const p = filePath.toLowerCase().replace(/\\/g, '/');
32
+ return (p.includes('.test.') ||
33
+ p.includes('.spec.') ||
34
+ p.includes('__tests__/') ||
35
+ p.includes('__mocks__/') ||
36
+ p.includes('/test/') ||
37
+ p.includes('/tests/') ||
38
+ p.includes('/testing/') ||
39
+ p.includes('/fixtures/') ||
40
+ p.endsWith('_test.go') ||
41
+ p.endsWith('_test.py') ||
42
+ p.endsWith('_spec.rb') ||
43
+ p.endsWith('_test.rb') ||
44
+ p.includes('/spec/') ||
45
+ p.includes('/test_') ||
46
+ p.includes('/conftest.'));
47
+ }
48
+ /** Valid LadybugDB node labels for safe Cypher query construction */
49
+ export const VALID_NODE_LABELS = new Set([
50
+ 'File',
51
+ 'Folder',
52
+ 'Function',
53
+ 'Class',
54
+ 'Interface',
55
+ 'Method',
56
+ 'CodeElement',
57
+ 'Community',
58
+ 'Process',
59
+ 'Struct',
60
+ 'Enum',
61
+ 'Macro',
62
+ 'Typedef',
63
+ 'Union',
64
+ 'Namespace',
65
+ 'Trait',
66
+ 'Impl',
67
+ 'TypeAlias',
68
+ 'Const',
69
+ 'Static',
70
+ 'Property',
71
+ 'Record',
72
+ 'Delegate',
73
+ 'Annotation',
74
+ 'Constructor',
75
+ 'Template',
76
+ 'Module',
77
+ 'Route',
78
+ 'Tool',
79
+ ]);
80
+ /** Valid relation types for impact analysis filtering */
81
+ export const VALID_RELATION_TYPES = new Set([
82
+ 'CALLS',
83
+ 'IMPORTS',
84
+ 'EXTENDS',
85
+ 'IMPLEMENTS',
86
+ 'HAS_METHOD',
87
+ 'HAS_PROPERTY',
88
+ 'METHOD_OVERRIDES',
89
+ 'OVERRIDES', // Legacy alias — dual-read for pre-rename indexes
90
+ 'METHOD_IMPLEMENTS',
91
+ 'ACCESSES',
92
+ 'HANDLES_ROUTE',
93
+ 'FETCHES',
94
+ 'HANDLES_TOOL',
95
+ 'ENTRY_POINT_OF',
96
+ 'WRAPS',
97
+ ]);
98
+ /**
99
+ * Per-relation-type confidence floor for impact analysis.
100
+ *
101
+ * When the graph stores a relation with a confidence value, that stored
102
+ * value is used as-is (it reflects resolution-tier accuracy from analysis
103
+ * time). This map provides the floor for each edge type when no stored
104
+ * confidence is available, and is also used for display / tooltip hints.
105
+ *
106
+ * Rationale:
107
+ * CALLS / IMPORTS – direct, strongly-typed references → 0.9
108
+ * EXTENDS – class hierarchy, statically verifiable → 0.85
109
+ * IMPLEMENTS – interface contract, statically verifiable → 0.85
110
+ * METHOD_OVERRIDES – method override, statically verifiable → 0.85
111
+ * METHOD_IMPLEMENTS – interface method implementation, statically verifiable → 0.85
112
+ * HAS_METHOD – structural containment → 0.95
113
+ * HAS_PROPERTY – structural containment → 0.95
114
+ * ACCESSES – field read/write, may be indirect → 0.8
115
+ * CONTAINS – folder/file containment → 0.95
116
+ * (unknown type) – conservative fallback → 0.5
117
+ */
118
+ export const IMPACT_RELATION_CONFIDENCE = {
119
+ CALLS: 0.9,
120
+ IMPORTS: 0.9,
121
+ EXTENDS: 0.85,
122
+ IMPLEMENTS: 0.85,
123
+ METHOD_OVERRIDES: 0.85,
124
+ METHOD_IMPLEMENTS: 0.85,
125
+ HAS_METHOD: 0.95,
126
+ HAS_PROPERTY: 0.95,
127
+ ACCESSES: 0.8,
128
+ CONTAINS: 0.95,
129
+ };
130
+ /**
131
+ * Return the confidence floor for a given relation type.
132
+ * Falls back to 0.5 for unknown types so they are not silently elevated.
133
+ */
134
+ const confidenceForRelType = (relType) => IMPACT_RELATION_CONFIDENCE[relType ?? ''] ?? 0.5;
135
+ /** Structured error logging for query failures — replaces empty catch blocks */
136
+ function logQueryError(context, err) {
137
+ const msg = err instanceof Error ? err.message : String(err);
138
+ console.error(`CodraGraph [${context}]: ${msg}`);
139
+ }
140
+ /**
141
+ * Structured per-query latency log for production aggregation (#553).
142
+ *
143
+ * Emitted on stderr — NOT stdout — because the MCP stdio transport uses
144
+ * stdout exclusively for JSON-RPC responses (#324), and the CLI e2e test
145
+ * `tool output goes to stdout via fd 1` asserts that stdout parses cleanly
146
+ * as JSON. Any `console.log` from inside a tool handler would corrupt the
147
+ * protocol. Matches the existing `logQueryError` convention above, which
148
+ * uses stderr for the same reason.
149
+ *
150
+ * The `CodraGraph [query:timing] …` prefix keeps lines greppable; the
151
+ * `phases` payload is JSON so log-scraping pipelines can parse it
152
+ * without custom format knowledge.
153
+ */
154
+ function logQueryTiming(query, phases) {
155
+ const totalMs = phases.wall ?? Object.values(phases).reduce((a, b) => a + b, 0);
156
+ const truncated = query.length > 80 ? `${query.slice(0, 80)}…` : query;
157
+ console.error(`CodraGraph [query:timing] query=${JSON.stringify(truncated)} totalMs=${totalMs} phases=${JSON.stringify(phases)}`);
158
+ }
159
+ export class LocalBackend {
160
+ repos = new Map();
161
+ contextCache = new Map();
162
+ initializedRepos = new Set();
163
+ reinitPromises = new Map();
164
+ lastStalenessCheck = new Map();
165
+ groupToolSvc = null;
166
+ /**
167
+ * One-shot stderr warnings for sibling-clone drift, keyed by
168
+ * `${repoId}|${cwdGitRoot}`. Without this guard every tool call
169
+ * from inside a sibling clone would print the same warning,
170
+ * making MCP stderr unreadable.
171
+ */
172
+ warnedSiblingDrift = new Set();
173
+ /**
174
+ * Cross-repo group tools (CLI). Shares logic with MCP `group_*` handlers.
175
+ */
176
+ getGroupService() {
177
+ if (!this.groupToolSvc) {
178
+ const port = {
179
+ resolveRepo: (p) => this.resolveRepo(p),
180
+ impact: (r, p) => this.impact(r, p),
181
+ query: (r, p) => this.query(r, p),
182
+ impactByUid: (id, uid, d, o) => this.impactByUid(id, uid, d, o),
183
+ context: (r, p) => this.context(r, p),
184
+ };
185
+ this.groupToolSvc = new GroupService(port);
186
+ }
187
+ return this.groupToolSvc;
188
+ }
189
+ /** Close all pooled LadybugDB connections (CLI one-shot; optional for long-lived MCP). */
190
+ async dispose() {
191
+ await closeLbug();
192
+ }
193
+ // ─── Initialization ──────────────────────────────────────────────
194
+ /**
195
+ * Initialize from the global registry.
196
+ * Returns true if at least one repo is available.
197
+ */
198
+ async init() {
199
+ await this.refreshRepos();
200
+ return this.repos.size > 0;
201
+ }
202
+ /**
203
+ * Re-read the global registry and update the in-memory repo map.
204
+ * New repos are added, existing repos are updated, removed repos are pruned.
205
+ * LadybugDB connections for removed repos are NOT closed (they idle-timeout naturally).
206
+ */
207
+ async refreshRepos() {
208
+ const entries = await listRegisteredRepos({ validate: true });
209
+ const freshIds = new Set();
210
+ for (const entry of entries) {
211
+ const id = this.repoId(entry.name, entry.path);
212
+ freshIds.add(id);
213
+ const storagePath = entry.storagePath;
214
+ const lbugPath = path.join(storagePath, 'lbug');
215
+ // Clean up any leftover KuzuDB files from before the LadybugDB migration.
216
+ // If kuzu exists but lbug doesn't, warn so the user knows to re-analyze.
217
+ const kuzu = await cleanupOldKuzuFiles(storagePath);
218
+ if (kuzu.found && kuzu.needsReindex) {
219
+ console.error(`CodraGraph: "${entry.name}" has a stale KuzuDB index. Run: codragraph analyze ${entry.path}`);
220
+ }
221
+ const handle = {
222
+ id,
223
+ name: entry.name,
224
+ repoPath: entry.path,
225
+ storagePath,
226
+ lbugPath,
227
+ indexedAt: entry.indexedAt,
228
+ lastCommit: entry.lastCommit,
229
+ remoteUrl: entry.remoteUrl,
230
+ stats: entry.stats,
231
+ };
232
+ this.repos.set(id, handle);
233
+ // Build lightweight context (no LadybugDB needed)
234
+ const s = entry.stats || {};
235
+ this.contextCache.set(id, {
236
+ projectName: entry.name,
237
+ stats: {
238
+ fileCount: s.files || 0,
239
+ functionCount: s.nodes || 0,
240
+ communityCount: s.communities || 0,
241
+ processCount: s.processes || 0,
242
+ },
243
+ });
244
+ }
245
+ // Prune repos that no longer exist in the registry
246
+ for (const id of this.repos.keys()) {
247
+ if (!freshIds.has(id)) {
248
+ this.repos.delete(id);
249
+ this.contextCache.delete(id);
250
+ this.initializedRepos.delete(id);
251
+ }
252
+ }
253
+ }
254
+ /**
255
+ * Generate a stable repo ID from name + path.
256
+ * If names collide, append a hash of the path.
257
+ */
258
+ repoId(name, repoPath) {
259
+ const base = name.toLowerCase();
260
+ // Check for name collision with a different path
261
+ for (const [id, handle] of this.repos) {
262
+ if (id === base && handle.repoPath !== path.resolve(repoPath)) {
263
+ // Collision — use path hash
264
+ const hash = Buffer.from(repoPath).toString('base64url').slice(0, 6);
265
+ return `${base}-${hash}`;
266
+ }
267
+ }
268
+ return base;
269
+ }
270
+ // ─── Repo Resolution ─────────────────────────────────────────────
271
+ /**
272
+ * Resolve which repo to use.
273
+ * - If repoParam is given, match by name or path
274
+ * - If only 1 repo, use it
275
+ * - If 0 or multiple without param, throw with helpful message
276
+ *
277
+ * On a miss, re-reads the registry once in case a new repo was indexed
278
+ * while the MCP server was running.
279
+ */
280
+ async resolveRepo(repoParam) {
281
+ const result = this.resolveRepoFromCache(repoParam);
282
+ if (result) {
283
+ // Issue: silent graph drift across sibling clones.
284
+ // If the caller's cwd lives in a *different* on-disk clone of
285
+ // the same repo (matched by `remoteUrl`), warn once per
286
+ // (repo, cwd) pair on stderr. We do not fail or refuse to
287
+ // serve — the index is still the best answer we have — but
288
+ // the operator/agent has to know the answer may be stale.
289
+ this.maybeWarnSiblingDrift(result).catch(() => {
290
+ /* best-effort; never throw from resolveRepo */
291
+ });
292
+ return result;
293
+ }
294
+ // Miss — refresh registry and try once more
295
+ await this.refreshRepos();
296
+ const retried = this.resolveRepoFromCache(repoParam);
297
+ if (retried) {
298
+ this.maybeWarnSiblingDrift(retried).catch(() => { });
299
+ return retried;
300
+ }
301
+ // Still no match — throw with helpful message
302
+ if (this.repos.size === 0) {
303
+ throw new Error('No indexed repositories. Run: codragraph analyze');
304
+ }
305
+ // Build a disambiguated "Available: …" list (#829). When two handles
306
+ // share a name, annotate each colliding label with its path so the
307
+ // caller can actually pick the right one. Single-name entries render
308
+ // identically to pre-#829 output.
309
+ const nameCounts = new Map();
310
+ for (const h of this.repos.values()) {
311
+ const key = h.name.toLowerCase();
312
+ nameCounts.set(key, (nameCounts.get(key) ?? 0) + 1);
313
+ }
314
+ const labels = [...this.repos.values()].map((h) => (nameCounts.get(h.name.toLowerCase()) ?? 0) > 1 ? `${h.name} (${h.repoPath})` : h.name);
315
+ if (repoParam) {
316
+ throw new Error(`Repository "${repoParam}" not found. Available: ${labels.join(', ')}`);
317
+ }
318
+ throw new Error(`Multiple repositories indexed. Specify which one with the "repo" parameter. Available: ${labels.join(', ')}`);
319
+ }
320
+ /**
321
+ * Try to resolve a repo from the in-memory cache. Returns null on miss.
322
+ */
323
+ resolveRepoFromCache(repoParam) {
324
+ if (this.repos.size === 0)
325
+ return null;
326
+ if (repoParam) {
327
+ const paramLower = repoParam.toLowerCase();
328
+ // Match by id
329
+ if (this.repos.has(paramLower))
330
+ return this.repos.get(paramLower);
331
+ // Match by name (case-insensitive)
332
+ for (const handle of this.repos.values()) {
333
+ if (handle.name.toLowerCase() === paramLower)
334
+ return handle;
335
+ }
336
+ // Match by path (substring)
337
+ const resolved = path.resolve(repoParam);
338
+ for (const handle of this.repos.values()) {
339
+ if (handle.repoPath === resolved)
340
+ return handle;
341
+ }
342
+ // Match by partial name
343
+ for (const handle of this.repos.values()) {
344
+ if (handle.name.toLowerCase().includes(paramLower))
345
+ return handle;
346
+ }
347
+ return null;
348
+ }
349
+ if (this.repos.size === 1) {
350
+ return this.repos.values().next().value;
351
+ }
352
+ return null; // Multiple repos, no param — ambiguous
353
+ }
354
+ // ─── Lazy LadybugDB Init ────────────────────────────────────────────
355
+ async ensureInitialized(repoId) {
356
+ // If a reinit is already in progress for this repo, wait for it
357
+ const pending = this.reinitPromises.get(repoId);
358
+ if (pending)
359
+ return pending;
360
+ const handle = this.repos.get(repoId);
361
+ if (!handle)
362
+ throw new Error(`Unknown repo: ${repoId}`);
363
+ // Check if the index was rebuilt since we opened the connection (#297).
364
+ // Throttle staleness checks to at most once per 5 seconds per repo to
365
+ // avoid an fs.readFile round-trip on every tool invocation.
366
+ if (this.initializedRepos.has(repoId) && isLbugReady(repoId)) {
367
+ const now = Date.now();
368
+ const lastCheck = this.lastStalenessCheck.get(repoId) ?? 0;
369
+ if (now - lastCheck < 5000)
370
+ return; // Checked recently — skip
371
+ this.lastStalenessCheck.set(repoId, now);
372
+ try {
373
+ const metaPath = path.join(handle.storagePath, 'meta.json');
374
+ const metaRaw = await fs.readFile(metaPath, 'utf-8');
375
+ const meta = JSON.parse(metaRaw);
376
+ if (meta.indexedAt && meta.indexedAt !== handle.indexedAt) {
377
+ // Index was rebuilt — close stale connection and re-init.
378
+ // Wrap in reinitPromises to prevent TOCTOU race where concurrent
379
+ // callers both detect staleness and double-close the pool.
380
+ const reinit = (async () => {
381
+ try {
382
+ await closeLbug(repoId);
383
+ this.initializedRepos.delete(repoId);
384
+ handle.indexedAt = meta.indexedAt;
385
+ await initLbug(repoId, handle.lbugPath);
386
+ this.initializedRepos.add(repoId);
387
+ }
388
+ finally {
389
+ this.reinitPromises.delete(repoId);
390
+ }
391
+ })();
392
+ this.reinitPromises.set(repoId, reinit);
393
+ return reinit;
394
+ }
395
+ else {
396
+ return; // Pool is current
397
+ }
398
+ }
399
+ catch {
400
+ return; // Can't read meta — assume pool is fine
401
+ }
402
+ }
403
+ try {
404
+ await initLbug(repoId, handle.lbugPath);
405
+ this.initializedRepos.add(repoId);
406
+ }
407
+ catch (err) {
408
+ // If lock error, mark as not initialized so next call retries
409
+ this.initializedRepos.delete(repoId);
410
+ throw err;
411
+ }
412
+ }
413
+ // ─── Public Getters ──────────────────────────────────────────────
414
+ /**
415
+ * Get context for a specific repo (or the single repo if only one).
416
+ */
417
+ getContext(repoId) {
418
+ if (repoId && this.contextCache.has(repoId)) {
419
+ return this.contextCache.get(repoId);
420
+ }
421
+ if (this.repos.size === 1) {
422
+ return this.contextCache.values().next().value ?? null;
423
+ }
424
+ return null;
425
+ }
426
+ /**
427
+ * List all registered repos with their metadata.
428
+ * Re-reads the global registry so newly indexed repos are discovered
429
+ * without restarting the MCP server.
430
+ *
431
+ * Each entry includes:
432
+ * - `staleness`: if the indexed clone's own HEAD has moved past
433
+ * the recorded `lastCommit` (option D in the issue's fix list).
434
+ * - `siblings`: other registered entries sharing the same
435
+ * `remoteUrl` (option B's payoff: callers can see at a glance
436
+ * that another clone of the same logical repo is registered).
437
+ * - `remoteUrl`: the canonical origin URL recorded at index time.
438
+ */
439
+ async listRepos() {
440
+ await this.refreshRepos();
441
+ const handles = [...this.repos.values()];
442
+ // Pre-group registered handles by `remoteUrl` so the sibling
443
+ // lookup is O(1) per handle. We reuse the in-memory `this.repos`
444
+ // (already populated by `refreshRepos`) instead of doing a fresh
445
+ // `readRegistry()` per entry — that would be N file reads for N
446
+ // registered repos.
447
+ const isWin = process.platform === 'win32';
448
+ const norm = (p) => (isWin ? path.resolve(p).toLowerCase() : path.resolve(p));
449
+ const byRemote = new Map();
450
+ for (const h of handles) {
451
+ if (!h.remoteUrl)
452
+ continue;
453
+ const list = byRemote.get(h.remoteUrl) ?? [];
454
+ list.push(h);
455
+ byRemote.set(h.remoteUrl, list);
456
+ }
457
+ return handles.map((h) => {
458
+ const stale = checkStaleness(h.repoPath, h.lastCommit);
459
+ const selfNorm = norm(h.repoPath);
460
+ const siblings = h.remoteUrl
461
+ ? (byRemote.get(h.remoteUrl) ?? []).filter((e) => norm(e.repoPath) !== selfNorm)
462
+ : [];
463
+ return {
464
+ name: h.name,
465
+ path: h.repoPath,
466
+ indexedAt: h.indexedAt,
467
+ lastCommit: h.lastCommit,
468
+ remoteUrl: h.remoteUrl,
469
+ stats: h.stats,
470
+ staleness: stale.isStale
471
+ ? { commitsBehind: stale.commitsBehind, hint: stale.hint }
472
+ : undefined,
473
+ siblings: siblings.length > 0
474
+ ? siblings.map((s) => ({
475
+ name: s.name,
476
+ path: s.repoPath,
477
+ lastCommit: s.lastCommit,
478
+ }))
479
+ : undefined,
480
+ };
481
+ });
482
+ }
483
+ /**
484
+ * Best-effort sibling-clone drift warning.
485
+ *
486
+ * When the resolved index has a `remoteUrl` recorded and the caller's
487
+ * `process.cwd()` is inside a *different* clone of the same repo, emit
488
+ * one stderr line per (repo, cwd) pair so the operator knows the
489
+ * graph may be stale relative to what's actually on disk under their
490
+ * cwd. Silent on path matches and on repos without a remote URL.
491
+ *
492
+ * Limitation: in MCP stdio server mode `process.cwd()` is the
493
+ * server's CWD at start time, *not* the agent client's CWD. The
494
+ * warning therefore only fires when the MCP server itself was
495
+ * launched from inside a sibling clone (typical for `npx codragraph
496
+ * serve` from a polecat workspace). Surfacing the client's CWD
497
+ * would require a per-tool-call `cwd` parameter — out of scope for
498
+ * the current MCP contract.
499
+ *
500
+ * Pure side-effect (stderr); never affects the returned handle.
501
+ * After the first computation for a given (repo, cwd) pair the
502
+ * result is cached so subsequent `resolveRepo()` calls don't
503
+ * re-shell-out to git.
504
+ */
505
+ async maybeWarnSiblingDrift(handle) {
506
+ if (!handle.remoteUrl)
507
+ return;
508
+ let cwd;
509
+ try {
510
+ cwd = process.cwd();
511
+ }
512
+ catch {
513
+ return;
514
+ }
515
+ // Early-exit cache: keyed on (repo, cwd) BEFORE any git shellout.
516
+ // After the first call for a given cwd, this short-circuits the
517
+ // up-to-four `execSync`/`execFileSync` calls inside `checkCwdMatch`
518
+ // — important for MCP-server mode where `process.cwd()` is constant
519
+ // and `resolveRepo` runs on every tool call.
520
+ const cacheKey = `${handle.id}|${cwd}`;
521
+ if (this.warnedSiblingDrift.has(cacheKey))
522
+ return;
523
+ const match = await checkCwdMatch(cwd);
524
+ if (match.match !== 'sibling-by-remote' ||
525
+ !match.entry ||
526
+ !match.cwdGitRoot ||
527
+ match.entry.path !== handle.repoPath ||
528
+ !match.hint) {
529
+ // Cache "nothing to warn about" outcomes too — `checkCwdMatch`
530
+ // is deterministic for a fixed (registry, cwd) pair, so re-running
531
+ // it yields nothing new.
532
+ this.warnedSiblingDrift.add(cacheKey);
533
+ return;
534
+ }
535
+ this.warnedSiblingDrift.add(cacheKey);
536
+ console.error(`CodraGraph: ${match.hint}`);
537
+ }
538
+ // ─── Tool Dispatch ───────────────────────────────────────────────
539
+ async callTool(method, params) {
540
+ if (method === 'list_repos') {
541
+ return this.listRepos();
542
+ }
543
+ if (method.startsWith('group_')) {
544
+ return this.handleGroupTool(method, params || {});
545
+ }
546
+ const p = params && typeof params === 'object' ? params : {};
547
+ if ((method === 'impact' || method === 'query' || method === 'context') &&
548
+ typeof p.repo === 'string' &&
549
+ p.repo.startsWith('@')) {
550
+ return this.callToolAtGroupRepo(method, p);
551
+ }
552
+ // Resolve repo from optional param (re-reads registry on miss)
553
+ const repo = await this.resolveRepo(params?.repo);
554
+ switch (method) {
555
+ case 'query':
556
+ return this.query(repo, params);
557
+ case 'cypher': {
558
+ const raw = await this.cypher(repo, params);
559
+ return this.formatCypherAsMarkdown(raw);
560
+ }
561
+ case 'context':
562
+ return this.context(repo, params);
563
+ case 'impact':
564
+ return this.impact(repo, params);
565
+ case 'detect_changes':
566
+ return this.detectChanges(repo, params);
567
+ case 'rename':
568
+ return this.rename(repo, params);
569
+ // Legacy aliases for backwards compatibility
570
+ case 'search':
571
+ return this.query(repo, params);
572
+ case 'explore':
573
+ return this.context(repo, { name: params?.name, ...params });
574
+ case 'overview':
575
+ return this.overview(repo, params);
576
+ case 'route_map':
577
+ return this.routeMap(repo, params);
578
+ case 'shape_check':
579
+ return this.shapeCheck(repo, params);
580
+ case 'tool_map':
581
+ return this.toolMap(repo, params);
582
+ case 'api_impact':
583
+ return this.apiImpact(repo, params);
584
+ case 'harness_swarm_run': {
585
+ // Same lazy-import dance as harness_run (see comments below) — keeps
586
+ // codragraph-harness optional and avoids a circular build-time dep.
587
+ const harnessModuleId = '@codragraph/harness/mcp/handler';
588
+ const dynImport = (id) => import(/* @vite-ignore */ id);
589
+ let handler;
590
+ try {
591
+ const mod = (await dynImport(harnessModuleId));
592
+ handler = mod.handleHarnessSwarmRun;
593
+ }
594
+ catch (err) {
595
+ throw new Error('harness_swarm_run requires the `@codragraph/harness` package. ' +
596
+ `Underlying error: ${err instanceof Error ? err.message : String(err)}`);
597
+ }
598
+ if (!handler) {
599
+ throw new Error('@codragraph/harness/dist/mcp/handler.js does not export handleHarnessSwarmRun');
600
+ }
601
+ return handler(params);
602
+ }
603
+ case 'graphstore_log':
604
+ case 'graphstore_branches':
605
+ case 'graphstore_diff':
606
+ case 'graphstore_semantic_diff':
607
+ case 'graphstore_merge':
608
+ case 'graphstore_gc':
609
+ case 'graphstore_blame_symbol': {
610
+ const handler = await import('./graphstore-handler.js');
611
+ const storagePath = repo.storagePath;
612
+ const args = params ?? {};
613
+ switch (method) {
614
+ case 'graphstore_log':
615
+ return handler.handleGraphstoreLog({
616
+ storagePath,
617
+ from: typeof args['from'] === 'string' ? args['from'] : undefined,
618
+ limit: typeof args['limit'] === 'number' ? args['limit'] : undefined,
619
+ });
620
+ case 'graphstore_branches':
621
+ return handler.handleGraphstoreBranches({ storagePath });
622
+ case 'graphstore_diff': {
623
+ const from = typeof args['from'] === 'string' ? args['from'] : '';
624
+ const to = typeof args['to'] === 'string' ? args['to'] : '';
625
+ if (!from || !to) {
626
+ throw new Error('graphstore_diff requires both `from` and `to`');
627
+ }
628
+ return handler.handleGraphstoreDiff({ storagePath, from, to });
629
+ }
630
+ case 'graphstore_semantic_diff': {
631
+ const from = typeof args['from'] === 'string' ? args['from'] : '';
632
+ const to = typeof args['to'] === 'string' ? args['to'] : '';
633
+ if (!from || !to) {
634
+ throw new Error('graphstore_semantic_diff requires both `from` and `to`');
635
+ }
636
+ return handler.handleGraphstoreSemanticDiff({ storagePath, from, to });
637
+ }
638
+ case 'graphstore_merge': {
639
+ const source = typeof args['source'] === 'string' ? args['source'] : '';
640
+ if (!source)
641
+ throw new Error('graphstore_merge requires `source`');
642
+ return handler.handleGraphstoreMerge({
643
+ storagePath,
644
+ source,
645
+ into: typeof args['into'] === 'string' ? args['into'] : undefined,
646
+ message: typeof args['message'] === 'string' ? args['message'] : undefined,
647
+ dryRun: typeof args['dryRun'] === 'boolean' ? args['dryRun'] : undefined,
648
+ });
649
+ }
650
+ case 'graphstore_gc':
651
+ return handler.handleGraphstoreGc({
652
+ storagePath,
653
+ dryRun: typeof args['dryRun'] === 'boolean' ? args['dryRun'] : undefined,
654
+ });
655
+ case 'graphstore_blame_symbol': {
656
+ const symbolId = typeof args['symbolId'] === 'string' ? args['symbolId'] : '';
657
+ if (!symbolId) {
658
+ throw new Error('graphstore_blame_symbol requires `symbolId`');
659
+ }
660
+ return handler.handleGraphstoreBlameSymbol({
661
+ storagePath,
662
+ symbolId,
663
+ table: typeof args['table'] === 'string' ? args['table'] : undefined,
664
+ limit: typeof args['limit'] === 'number' ? args['limit'] : undefined,
665
+ });
666
+ }
667
+ }
668
+ // Should not be reached; fall through to default for safety.
669
+ throw new Error(`Unhandled graphstore method: ${method}`);
670
+ }
671
+ case 'harness_recipes_list':
672
+ case 'harness_recipes_lookup': {
673
+ // Phase 4 × Phase 3 moat tools — same lazy-import dance as the
674
+ // other harness handlers so codragraph-harness stays optional.
675
+ const harnessModuleId = '@codragraph/harness/mcp/handler';
676
+ const dynImport = (id) => import(/* @vite-ignore */ id);
677
+ let mod;
678
+ try {
679
+ mod = (await dynImport(harnessModuleId));
680
+ }
681
+ catch (err) {
682
+ throw new Error(`${method} requires the \`codragraph-harness\` package. ` +
683
+ `Underlying error: ${err instanceof Error ? err.message : String(err)}`);
684
+ }
685
+ const handler = method === 'harness_recipes_list'
686
+ ? mod.handleHarnessRecipesList
687
+ : mod.handleHarnessRecipesLookup;
688
+ if (!handler) {
689
+ throw new Error(`@codragraph/harness/dist/mcp/handler.js does not export the handler for ${method}`);
690
+ }
691
+ return handler(params);
692
+ }
693
+ case 'harness_run': {
694
+ // Lazy dynamic import — keeps codragraph-harness optional at runtime
695
+ // and avoids a build-time circular dep (harness depends on codragraph,
696
+ // so static import here would create a cycle). The import id is
697
+ // routed through a `string` variable so TS module-resolution does
698
+ // not try to resolve the path at compile time — the harness package's
699
+ // .d.ts files do not exist while codragraph itself is being built
700
+ // for the first time during workspace install.
701
+ const harnessModuleId = '@codragraph/harness/mcp/handler';
702
+ const dynImport = (id) => import(/* @vite-ignore */ id);
703
+ let handler;
704
+ try {
705
+ const mod = (await dynImport(harnessModuleId));
706
+ handler = mod.handleHarnessRun;
707
+ }
708
+ catch (err) {
709
+ throw new Error('harness_run requires the `@codragraph/harness` package. ' +
710
+ `Install it (npm i codragraph-harness) and retry. ` +
711
+ `Underlying error: ${err instanceof Error ? err.message : String(err)}`);
712
+ }
713
+ if (!handler) {
714
+ throw new Error('@codragraph/harness/dist/mcp/handler.js does not export handleHarnessRun');
715
+ }
716
+ return handler(params);
717
+ }
718
+ default:
719
+ throw new Error(`Unknown tool: ${method}`);
720
+ }
721
+ }
722
+ // ─── Tool Implementations ────────────────────────────────────────
723
+ /**
724
+ * Query tool — process-grouped search.
725
+ *
726
+ * 1. Hybrid search (BM25 + semantic) to find matching symbols
727
+ * 2. Trace each match to its process(es) via STEP_IN_PROCESS
728
+ * 3. Group by process, rank by aggregate relevance + internal cluster cohesion
729
+ * 4. Return: { processes, process_symbols, definitions }
730
+ */
731
+ async query(repo, params) {
732
+ if (!params.query?.trim()) {
733
+ return { error: 'query parameter is required and cannot be empty.' };
734
+ }
735
+ await this.ensureInitialized(repo.id);
736
+ const processLimit = params.limit || 5;
737
+ const maxSymbolsPerProcess = params.max_symbols || 10;
738
+ const includeContent = params.include_content ?? false;
739
+ const searchQuery = params.query.trim();
740
+ // Per-phase timing instrumentation (#553). Records wall time for each
741
+ // observable sub-step of the search pipeline so production latency can
742
+ // be aggregated offline for Pareto analysis and bottleneck detection.
743
+ // Overhead is <0.1 ms per phase; the timer is passive and never alters
744
+ // query behaviour.
745
+ const timer = new PhaseTimer();
746
+ const wallStart = performance.now();
747
+ // Step 1: Run hybrid search to get matching symbols. BM25 and vector
748
+ // search run concurrently via Promise.all — use `timer.time()` for
749
+ // each so both get independent wall-time records without fighting
750
+ // over a single `current` phase slot.
751
+ const searchLimit = processLimit * maxSymbolsPerProcess; // fetch enough raw results
752
+ const [bm25SearchResult, semanticResults] = await Promise.all([
753
+ timer.time('bm25', this.bm25Search(repo, searchQuery, searchLimit)),
754
+ timer.time('vector', this.semanticSearch(repo, searchQuery, searchLimit)),
755
+ ]);
756
+ const bm25Results = bm25SearchResult.results;
757
+ const ftsUsed = bm25SearchResult.ftsUsed;
758
+ // Merge via reciprocal rank fusion
759
+ timer.start('merge');
760
+ const scoreMap = new Map();
761
+ for (let i = 0; i < bm25Results.length; i++) {
762
+ const result = bm25Results[i];
763
+ const key = result.nodeId || result.filePath;
764
+ const rrfScore = 1 / (60 + i);
765
+ const existing = scoreMap.get(key);
766
+ if (existing) {
767
+ existing.score += rrfScore;
768
+ }
769
+ else {
770
+ scoreMap.set(key, { score: rrfScore, data: result });
771
+ }
772
+ }
773
+ for (let i = 0; i < semanticResults.length; i++) {
774
+ const result = semanticResults[i];
775
+ const key = result.nodeId || result.filePath;
776
+ const rrfScore = 1 / (60 + i);
777
+ const existing = scoreMap.get(key);
778
+ if (existing) {
779
+ existing.score += rrfScore;
780
+ }
781
+ else {
782
+ scoreMap.set(key, { score: rrfScore, data: result });
783
+ }
784
+ }
785
+ const merged = Array.from(scoreMap.entries())
786
+ .sort((a, b) => b[1].score - a[1].score)
787
+ .slice(0, searchLimit);
788
+ timer.stop(); // merge
789
+ // Step 2: For each match with a nodeId, trace to process(es)
790
+ timer.start('symbol_lookup');
791
+ const processMap = new Map();
792
+ const definitions = []; // standalone symbols not in any process
793
+ for (const [_, item] of merged) {
794
+ const sym = item.data;
795
+ if (!sym.nodeId) {
796
+ // File-level results go to definitions
797
+ definitions.push({
798
+ name: sym.name,
799
+ type: sym.type || 'File',
800
+ filePath: sym.filePath,
801
+ });
802
+ continue;
803
+ }
804
+ // Find processes this symbol participates in
805
+ let processRows = [];
806
+ try {
807
+ processRows = await executeParameterized(repo.id, `
808
+ MATCH (n {id: $nodeId})-[r:CodeRelation {type: 'STEP_IN_PROCESS'}]->(p:Process)
809
+ RETURN p.id AS pid, p.label AS label, p.heuristicLabel AS heuristicLabel, p.processType AS processType, p.stepCount AS stepCount, r.step AS step
810
+ `, { nodeId: sym.nodeId });
811
+ }
812
+ catch (e) {
813
+ logQueryError('query:process-lookup', e);
814
+ }
815
+ // Get cluster membership + cohesion (cohesion used as internal ranking signal)
816
+ let cohesion = 0;
817
+ let module;
818
+ try {
819
+ const cohesionRows = await executeParameterized(repo.id, `
820
+ MATCH (n {id: $nodeId})-[:CodeRelation {type: 'MEMBER_OF'}]->(c:Community)
821
+ RETURN c.cohesion AS cohesion, c.heuristicLabel AS module
822
+ LIMIT 1
823
+ `, { nodeId: sym.nodeId });
824
+ if (cohesionRows.length > 0) {
825
+ cohesion = (cohesionRows[0].cohesion ?? cohesionRows[0][0]) || 0;
826
+ module = cohesionRows[0].module ?? cohesionRows[0][1];
827
+ }
828
+ }
829
+ catch (e) {
830
+ logQueryError('query:cluster-info', e);
831
+ }
832
+ // Optionally fetch content
833
+ let content;
834
+ if (includeContent) {
835
+ try {
836
+ const contentRows = await executeParameterized(repo.id, `
837
+ MATCH (n {id: $nodeId})
838
+ RETURN n.content AS content
839
+ `, { nodeId: sym.nodeId });
840
+ if (contentRows.length > 0) {
841
+ content = contentRows[0].content ?? contentRows[0][0];
842
+ }
843
+ }
844
+ catch (e) {
845
+ logQueryError('query:content-fetch', e);
846
+ }
847
+ }
848
+ const symbolEntry = {
849
+ id: sym.nodeId,
850
+ name: sym.name,
851
+ type: sym.type,
852
+ filePath: sym.filePath,
853
+ startLine: sym.startLine,
854
+ endLine: sym.endLine,
855
+ ...(module ? { module } : {}),
856
+ ...(includeContent && content ? { content } : {}),
857
+ };
858
+ if (processRows.length === 0) {
859
+ // Symbol not in any process — goes to definitions
860
+ definitions.push(symbolEntry);
861
+ }
862
+ else {
863
+ // Add to each process it belongs to
864
+ for (const row of processRows) {
865
+ const pid = row.pid ?? row[0];
866
+ const label = row.label ?? row[1];
867
+ const hLabel = row.heuristicLabel ?? row[2];
868
+ const pType = row.processType ?? row[3];
869
+ const stepCount = row.stepCount ?? row[4];
870
+ const step = row.step ?? row[5];
871
+ if (!processMap.has(pid)) {
872
+ processMap.set(pid, {
873
+ id: pid,
874
+ label,
875
+ heuristicLabel: hLabel,
876
+ processType: pType,
877
+ stepCount,
878
+ totalScore: 0,
879
+ cohesionBoost: 0,
880
+ symbols: [],
881
+ });
882
+ }
883
+ const proc = processMap.get(pid);
884
+ proc.totalScore += item.score;
885
+ proc.cohesionBoost = Math.max(proc.cohesionBoost, cohesion);
886
+ proc.symbols.push({
887
+ ...symbolEntry,
888
+ process_id: pid,
889
+ step_index: step,
890
+ });
891
+ }
892
+ }
893
+ }
894
+ timer.stop(); // symbol_lookup
895
+ // Step 3: Rank processes by aggregate score + internal cohesion boost
896
+ timer.start('ranking');
897
+ const rankedProcesses = Array.from(processMap.values())
898
+ .map((p) => ({
899
+ ...p,
900
+ priority: p.totalScore + p.cohesionBoost * 0.1, // cohesion as subtle ranking signal
901
+ }))
902
+ .sort((a, b) => b.priority - a.priority)
903
+ .slice(0, processLimit);
904
+ timer.stop(); // ranking
905
+ // Step 4: Build response
906
+ timer.start('formatting');
907
+ const processes = rankedProcesses.map((p) => ({
908
+ id: p.id,
909
+ summary: p.heuristicLabel || p.label,
910
+ priority: Math.round(p.priority * 1000) / 1000,
911
+ symbol_count: p.symbols.length,
912
+ process_type: p.processType,
913
+ step_count: p.stepCount,
914
+ }));
915
+ const processSymbols = rankedProcesses.flatMap((p) => p.symbols.slice(0, maxSymbolsPerProcess).map((s) => ({
916
+ ...s,
917
+ // remove internal fields
918
+ })));
919
+ // Deduplicate process_symbols by id
920
+ const seen = new Set();
921
+ const dedupedSymbols = processSymbols.filter((s) => {
922
+ if (seen.has(s.id))
923
+ return false;
924
+ seen.add(s.id);
925
+ return true;
926
+ });
927
+ timer.stop(); // formatting
928
+ // End-to-end wall time — deliberately a separate mark so callers can
929
+ // compare sum(phases) vs wall to see how much Promise.all concurrency
930
+ // saved. Must come before summary() so it's included.
931
+ timer.mark('wall', performance.now() - wallStart);
932
+ const timing = timer.summary();
933
+ logQueryTiming(searchQuery, timing);
934
+ return {
935
+ processes,
936
+ process_symbols: dedupedSymbols,
937
+ definitions: definitions.slice(0, 20), // cap standalone definitions
938
+ timing,
939
+ ...(!ftsUsed && {
940
+ warning: 'FTS extension unavailable - keyword search degraded. Run: codragraph analyze --force to rebuild indexes.',
941
+ }),
942
+ };
943
+ }
944
+ /**
945
+ * BM25 keyword search helper - uses LadybugDB FTS for always-fresh results
946
+ */
947
+ async bm25Search(repo, query, limit) {
948
+ const { searchFTSFromLbug } = await import('../../core/search/bm25-index.js');
949
+ let bm25Results;
950
+ try {
951
+ bm25Results = await searchFTSFromLbug(query, limit, repo.id);
952
+ }
953
+ catch (err) {
954
+ console.error('CodraGraph: BM25/FTS search failed (FTS indexes may not exist) -', err.message);
955
+ return { results: [], ftsUsed: false };
956
+ }
957
+ const ftsUsed = bm25Results.length === 0 || bm25Results[0]?.ftsUsed !== false;
958
+ const results = [];
959
+ for (const bm25Result of bm25Results) {
960
+ const fullPath = bm25Result.filePath;
961
+ try {
962
+ // Prefer direct nodeId lookup (exact FTS-matched nodes) over filePath fallback.
963
+ // Without this, LIMIT 3 on filePath returns arbitrary symbols rather than
964
+ // the nodes that actually scored highest in the BM25 index.
965
+ const nodeIds = bm25Result.nodeIds?.length ? bm25Result.nodeIds : null;
966
+ const symbols = nodeIds
967
+ ? await executeParameterized(repo.id, `
968
+ MATCH (n)
969
+ WHERE n.id IN $nodeIds
970
+ RETURN n.id AS id, n.name AS name, labels(n)[0] AS type, n.filePath AS filePath, n.startLine AS startLine, n.endLine AS endLine
971
+ `, { nodeIds })
972
+ : await executeParameterized(repo.id, `
973
+ MATCH (n)
974
+ WHERE n.filePath = $filePath
975
+ RETURN n.id AS id, n.name AS name, labels(n)[0] AS type, n.filePath AS filePath, n.startLine AS startLine, n.endLine AS endLine
976
+ LIMIT 3
977
+ `, { filePath: fullPath });
978
+ if (symbols.length > 0) {
979
+ for (const sym of symbols) {
980
+ results.push({
981
+ nodeId: sym.id || sym[0],
982
+ name: sym.name || sym[1],
983
+ type: sym.type || sym[2],
984
+ filePath: sym.filePath || sym[3],
985
+ startLine: sym.startLine || sym[4],
986
+ endLine: sym.endLine || sym[5],
987
+ bm25Score: bm25Result.score,
988
+ });
989
+ }
990
+ }
991
+ else {
992
+ const fileName = fullPath.split('/').pop() || fullPath;
993
+ results.push({
994
+ name: fileName,
995
+ type: 'File',
996
+ filePath: bm25Result.filePath,
997
+ bm25Score: bm25Result.score,
998
+ });
999
+ }
1000
+ }
1001
+ catch {
1002
+ const fileName = fullPath.split('/').pop() || fullPath;
1003
+ results.push({
1004
+ name: fileName,
1005
+ type: 'File',
1006
+ filePath: bm25Result.filePath,
1007
+ bm25Score: bm25Result.score,
1008
+ });
1009
+ }
1010
+ }
1011
+ return { results, ftsUsed };
1012
+ }
1013
+ /**
1014
+ * Semantic vector search helper
1015
+ */
1016
+ async semanticSearch(repo, query, limit) {
1017
+ try {
1018
+ // Check if embedding table exists before loading the model (avoids heavy model init when embeddings are off)
1019
+ const tableCheck = await executeQuery(repo.id, `MATCH (e:${EMBEDDING_TABLE_NAME}) RETURN COUNT(*) AS cnt LIMIT 1`);
1020
+ if (!tableCheck.length || (tableCheck[0].cnt ?? tableCheck[0][0]) === 0)
1021
+ return [];
1022
+ const { embedQuery, getEmbeddingDims } = await import('../core/embedder.js');
1023
+ const queryVec = await embedQuery(query);
1024
+ const dims = getEmbeddingDims();
1025
+ const queryVecStr = `[${queryVec.join(',')}]`;
1026
+ const bestChunks = await collectBestChunks(limit, async (fetchLimit) => {
1027
+ const vectorQuery = `
1028
+ CALL QUERY_VECTOR_INDEX('${EMBEDDING_TABLE_NAME}', '${EMBEDDING_INDEX_NAME}',
1029
+ CAST(${queryVecStr} AS FLOAT[${dims}]), ${fetchLimit})
1030
+ YIELD node AS emb, distance
1031
+ WITH emb, distance
1032
+ WHERE distance < 0.6
1033
+ RETURN emb.nodeId AS nodeId, emb.chunkIndex AS chunkIndex,
1034
+ emb.startLine AS startLine, emb.endLine AS endLine, distance
1035
+ ORDER BY distance
1036
+ `;
1037
+ const embResults = await executeQuery(repo.id, vectorQuery);
1038
+ return embResults.map((row) => ({
1039
+ nodeId: row.nodeId ?? row[0],
1040
+ chunkIndex: row.chunkIndex ?? row[1] ?? 0,
1041
+ startLine: row.startLine ?? row[2] ?? 0,
1042
+ endLine: row.endLine ?? row[3] ?? 0,
1043
+ distance: row.distance ?? row[4],
1044
+ }));
1045
+ });
1046
+ if (bestChunks.size === 0)
1047
+ return [];
1048
+ const results = [];
1049
+ for (const [nodeId, chunk] of Array.from(bestChunks.entries()).slice(0, limit)) {
1050
+ const labelEndIdx = nodeId.indexOf(':');
1051
+ const label = labelEndIdx > 0 ? nodeId.substring(0, labelEndIdx) : 'Unknown';
1052
+ // Validate label against known node types to prevent Cypher injection
1053
+ if (!VALID_NODE_LABELS.has(label))
1054
+ continue;
1055
+ try {
1056
+ const nodeQuery = label === 'File'
1057
+ ? `MATCH (n:File {id: $nodeId}) RETURN n.name AS name, n.filePath AS filePath`
1058
+ : `MATCH (n:\`${label}\` {id: $nodeId}) RETURN n.name AS name, n.filePath AS filePath`;
1059
+ const nodeRows = await executeParameterized(repo.id, nodeQuery, { nodeId });
1060
+ if (nodeRows.length > 0) {
1061
+ const nodeRow = nodeRows[0];
1062
+ results.push({
1063
+ nodeId,
1064
+ name: nodeRow.name ?? nodeRow[0] ?? '',
1065
+ type: label,
1066
+ filePath: nodeRow.filePath ?? nodeRow[1] ?? '',
1067
+ distance: chunk.distance,
1068
+ startLine: chunk.startLine,
1069
+ endLine: chunk.endLine,
1070
+ });
1071
+ }
1072
+ }
1073
+ catch { }
1074
+ }
1075
+ return results;
1076
+ }
1077
+ catch {
1078
+ // Expected when embeddings are disabled — silently fall back to BM25-only
1079
+ return [];
1080
+ }
1081
+ }
1082
+ async executeCypher(repoName, query) {
1083
+ const repo = await this.resolveRepo(repoName);
1084
+ return this.cypher(repo, { query });
1085
+ }
1086
+ async cypher(repo, params) {
1087
+ await this.ensureInitialized(repo.id);
1088
+ if (!isLbugReady(repo.id)) {
1089
+ return { error: 'LadybugDB not ready. Index may be corrupted.' };
1090
+ }
1091
+ // Block write operations (defense-in-depth — DB is already read-only)
1092
+ if (isWriteQuery(params.query)) {
1093
+ return {
1094
+ error: 'Write operations (CREATE, DELETE, SET, MERGE, REMOVE, DROP, ALTER, COPY, DETACH) are not allowed. The knowledge graph is read-only.',
1095
+ };
1096
+ }
1097
+ try {
1098
+ const result = await executeQuery(repo.id, params.query);
1099
+ return result;
1100
+ }
1101
+ catch (err) {
1102
+ return { error: err.message || 'Query failed' };
1103
+ }
1104
+ }
1105
+ /**
1106
+ * Format raw Cypher result rows as a markdown table for LLM readability.
1107
+ * Falls back to raw result if rows aren't tabular objects.
1108
+ */
1109
+ formatCypherAsMarkdown(result) {
1110
+ if (!Array.isArray(result) || result.length === 0)
1111
+ return result;
1112
+ const firstRow = result[0];
1113
+ if (typeof firstRow !== 'object' || firstRow === null)
1114
+ return result;
1115
+ const keys = Object.keys(firstRow);
1116
+ if (keys.length === 0)
1117
+ return result;
1118
+ const header = '| ' + keys.join(' | ') + ' |';
1119
+ const separator = '| ' + keys.map(() => '---').join(' | ') + ' |';
1120
+ const dataRows = result.map((row) => '| ' +
1121
+ keys
1122
+ .map((k) => {
1123
+ const v = row[k];
1124
+ if (v === null || v === undefined)
1125
+ return '';
1126
+ if (typeof v === 'object')
1127
+ return JSON.stringify(v);
1128
+ return String(v);
1129
+ })
1130
+ .join(' | ') +
1131
+ ' |');
1132
+ return {
1133
+ markdown: [header, separator, ...dataRows].join('\n'),
1134
+ row_count: result.length,
1135
+ };
1136
+ }
1137
+ /**
1138
+ * Aggregate same-named clusters: group by heuristicLabel, sum symbols,
1139
+ * weighted-average cohesion, filter out tiny clusters (<5 symbols).
1140
+ * Raw communities stay intact in LadybugDB for Cypher queries.
1141
+ */
1142
+ aggregateClusters(clusters) {
1143
+ const groups = new Map();
1144
+ for (const c of clusters) {
1145
+ const label = c.heuristicLabel || c.label || 'Unknown';
1146
+ const symbols = c.symbolCount || 0;
1147
+ const cohesion = c.cohesion || 0;
1148
+ const existing = groups.get(label);
1149
+ if (!existing) {
1150
+ groups.set(label, {
1151
+ ids: [c.id],
1152
+ totalSymbols: symbols,
1153
+ weightedCohesion: cohesion * symbols,
1154
+ largest: c,
1155
+ });
1156
+ }
1157
+ else {
1158
+ existing.ids.push(c.id);
1159
+ existing.totalSymbols += symbols;
1160
+ existing.weightedCohesion += cohesion * symbols;
1161
+ if (symbols > (existing.largest.symbolCount || 0)) {
1162
+ existing.largest = c;
1163
+ }
1164
+ }
1165
+ }
1166
+ return Array.from(groups.entries())
1167
+ .map(([label, g]) => ({
1168
+ id: g.largest.id,
1169
+ label,
1170
+ heuristicLabel: label,
1171
+ symbolCount: g.totalSymbols,
1172
+ cohesion: g.totalSymbols > 0 ? g.weightedCohesion / g.totalSymbols : 0,
1173
+ subCommunities: g.ids.length,
1174
+ }))
1175
+ .filter((c) => c.symbolCount >= 5)
1176
+ .sort((a, b) => b.symbolCount - a.symbolCount);
1177
+ }
1178
+ async overview(repo, params) {
1179
+ await this.ensureInitialized(repo.id);
1180
+ const limit = params.limit || 20;
1181
+ const result = {
1182
+ repo: repo.name,
1183
+ repoPath: repo.repoPath,
1184
+ stats: repo.stats,
1185
+ indexedAt: repo.indexedAt,
1186
+ lastCommit: repo.lastCommit,
1187
+ };
1188
+ if (params.showClusters !== false) {
1189
+ try {
1190
+ // Fetch more raw communities than the display limit so aggregation has enough data
1191
+ const rawLimit = Math.max(limit * 5, 200);
1192
+ const clusters = await executeQuery(repo.id, `
1193
+ MATCH (c:Community)
1194
+ RETURN c.id AS id, c.label AS label, c.heuristicLabel AS heuristicLabel, c.cohesion AS cohesion, c.symbolCount AS symbolCount
1195
+ ORDER BY c.symbolCount DESC
1196
+ LIMIT ${rawLimit}
1197
+ `);
1198
+ const rawClusters = clusters.map((c) => ({
1199
+ id: c.id || c[0],
1200
+ label: c.label || c[1],
1201
+ heuristicLabel: c.heuristicLabel || c[2],
1202
+ cohesion: c.cohesion || c[3],
1203
+ symbolCount: c.symbolCount || c[4],
1204
+ }));
1205
+ result.clusters = this.aggregateClusters(rawClusters).slice(0, limit);
1206
+ }
1207
+ catch {
1208
+ result.clusters = [];
1209
+ }
1210
+ }
1211
+ if (params.showProcesses !== false) {
1212
+ try {
1213
+ const processes = await executeQuery(repo.id, `
1214
+ MATCH (p:Process)
1215
+ RETURN p.id AS id, p.label AS label, p.heuristicLabel AS heuristicLabel, p.processType AS processType, p.stepCount AS stepCount
1216
+ ORDER BY p.stepCount DESC
1217
+ LIMIT ${limit}
1218
+ `);
1219
+ result.processes = processes.map((p) => ({
1220
+ id: p.id || p[0],
1221
+ label: p.label || p[1],
1222
+ heuristicLabel: p.heuristicLabel || p[2],
1223
+ processType: p.processType || p[3],
1224
+ stepCount: p.stepCount || p[4],
1225
+ }));
1226
+ }
1227
+ catch {
1228
+ result.processes = [];
1229
+ }
1230
+ }
1231
+ return result;
1232
+ }
1233
+ /**
1234
+ * Patch the `type` field on candidates whose `labels(n)[0]` projection
1235
+ * came back empty — a known LadybugDB behaviour for several node types.
1236
+ *
1237
+ * Uses one scoped UNION query across the five priority labels rather
1238
+ * than per-candidate round-trips, so cost is a single DB call regardless
1239
+ * of how many candidates need enrichment. No-op when every candidate
1240
+ * already has a non-empty type.
1241
+ *
1242
+ * Failures are swallowed: label enrichment is an optimisation for
1243
+ * downstream scoring and #480 Class/Interface BFS seeding; if it fails
1244
+ * the symbol still resolves, just without the kind-priority bonus.
1245
+ */
1246
+ async enrichCandidateLabels(repo, candidates) {
1247
+ const ids = candidates.filter((c) => c.type === '' && c.id).map((c) => c.id);
1248
+ if (ids.length === 0)
1249
+ return;
1250
+ try {
1251
+ const rows = await executeParameterized(repo.id, `
1252
+ MATCH (n:\`Class\`) WHERE n.id IN $ids RETURN n.id AS id, 'Class' AS label
1253
+ UNION ALL
1254
+ MATCH (n:\`Interface\`) WHERE n.id IN $ids RETURN n.id AS id, 'Interface' AS label
1255
+ UNION ALL
1256
+ MATCH (n:\`Function\`) WHERE n.id IN $ids RETURN n.id AS id, 'Function' AS label
1257
+ UNION ALL
1258
+ MATCH (n:\`Method\`) WHERE n.id IN $ids RETURN n.id AS id, 'Method' AS label
1259
+ UNION ALL
1260
+ MATCH (n:\`Constructor\`) WHERE n.id IN $ids RETURN n.id AS id, 'Constructor' AS label
1261
+ `, { ids });
1262
+ const labelById = new Map();
1263
+ for (const r of rows) {
1264
+ const id = (r.id ?? r[0]);
1265
+ const label = (r.label ?? r[1]);
1266
+ if (id && label && !labelById.has(id))
1267
+ labelById.set(id, label);
1268
+ }
1269
+ for (const c of candidates) {
1270
+ if (c.type === '' && labelById.has(c.id))
1271
+ c.type = labelById.get(c.id);
1272
+ }
1273
+ }
1274
+ catch {
1275
+ /* best-effort — downstream resolvers still work without the label */
1276
+ }
1277
+ }
1278
+ /**
1279
+ * Score a symbol candidate for disambiguation ranking.
1280
+ *
1281
+ * Deterministic, no DB round-trip:
1282
+ * - base 0.50
1283
+ * - +0.40 when file_path hint matches (substring, case-insensitive)
1284
+ * - +0.20 when kind hint exactly matches the candidate's kind
1285
+ * - when no kind hint, a small priority bonus (Class > Interface >
1286
+ * Function > Method > Constructor) to preserve the intuition that
1287
+ * class-level names are usually what the user wanted.
1288
+ *
1289
+ * Capped at 1.0. Intentionally simple and inspectable — a future v2 can
1290
+ * plug in BM25/embedding signals here without changing the surrounding
1291
+ * resolver shape.
1292
+ */
1293
+ scoreCandidate(c, hints) {
1294
+ let s = 0.5;
1295
+ if (hints.file_path && c.filePath && typeof c.filePath === 'string') {
1296
+ if (c.filePath.toLowerCase().includes(hints.file_path.toLowerCase())) {
1297
+ s += 0.4;
1298
+ }
1299
+ }
1300
+ if (hints.kind && c.kind === hints.kind) {
1301
+ s += 0.2;
1302
+ }
1303
+ if (!hints.kind) {
1304
+ const priority = {
1305
+ Class: 5,
1306
+ Interface: 4,
1307
+ Function: 3,
1308
+ Method: 2,
1309
+ Constructor: 1,
1310
+ };
1311
+ s += (priority[c.kind] ?? 0) * 0.02;
1312
+ }
1313
+ return Math.min(1.0, s);
1314
+ }
1315
+ /**
1316
+ * Shared symbol resolver used by `context` and `impact`.
1317
+ *
1318
+ * Returns one of:
1319
+ * - `{ kind: 'ok', symbol, resolvedLabel }` — single confident match
1320
+ * (either direct UID, only one candidate after filtering, Class/
1321
+ * Constructor collapse, or a top-scoring candidate with a clear gap
1322
+ * to the runner-up).
1323
+ * - `{ kind: 'ambiguous', candidates }` — multiple viable matches,
1324
+ * sorted by score desc. Each candidate carries a relevance score.
1325
+ * - `{ kind: 'not_found' }` — no matches at all.
1326
+ *
1327
+ * Preserves the #480 Class/Constructor preference: when the only
1328
+ * ambiguity is between a Class and its own Constructor (same name,
1329
+ * same filePath), the Class wins silently.
1330
+ */
1331
+ async resolveSymbolCandidates(repo, query, hints) {
1332
+ const { uid, name, include_content } = query;
1333
+ const selectClause = `n.id AS id, n.name AS name, labels(n)[0] AS type, n.filePath AS filePath, n.startLine AS startLine, n.endLine AS endLine${include_content ? ', n.content AS content' : ''}`;
1334
+ // Direct UID — zero-ambiguity path.
1335
+ if (uid) {
1336
+ const rows = await executeParameterized(repo.id, `MATCH (n {id: $uid}) RETURN ${selectClause} LIMIT 1`, { uid });
1337
+ if (rows.length === 0)
1338
+ return { kind: 'not_found' };
1339
+ const r = rows[0];
1340
+ const symbol = {
1341
+ id: (r.id ?? r[0]),
1342
+ name: (r.name ?? r[1]),
1343
+ type: (r.type ?? r[2] ?? ''),
1344
+ filePath: (r.filePath ?? r[3]),
1345
+ startLine: (r.startLine ?? r[4]),
1346
+ endLine: (r.endLine ?? r[5]),
1347
+ ...(include_content ? { content: (r.content ?? r[6]) } : {}),
1348
+ };
1349
+ // Same LadybugDB label-enrichment as the name-based path: a UID
1350
+ // pointing at a Class must still surface `type: 'Class'` so impact's
1351
+ // Class/Interface BFS seed fires. No-op when type is already set.
1352
+ await this.enrichCandidateLabels(repo, [symbol]);
1353
+ return { kind: 'ok', symbol, resolvedLabel: symbol.type };
1354
+ }
1355
+ if (!name)
1356
+ return { kind: 'not_found' };
1357
+ const isQualified = name.includes('/') || name.includes(':');
1358
+ let whereClause;
1359
+ const queryParams = { symName: name };
1360
+ if (hints.file_path) {
1361
+ whereClause = `WHERE n.name = $symName AND n.filePath CONTAINS $filePath`;
1362
+ queryParams.filePath = hints.file_path;
1363
+ }
1364
+ else if (isQualified) {
1365
+ whereClause = `WHERE n.id = $symName OR n.name = $symName`;
1366
+ }
1367
+ else {
1368
+ whereClause = `WHERE n.name = $symName`;
1369
+ }
1370
+ // LIMIT 20 (was 10) — scoring is the point now, so give the ranker
1371
+ // headroom instead of arbitrary truncation.
1372
+ const rows = await executeParameterized(repo.id, `MATCH (n) ${whereClause} RETURN ${selectClause} LIMIT 20`, queryParams);
1373
+ if (rows.length === 0)
1374
+ return { kind: 'not_found' };
1375
+ // Normalise row shape across object / tuple returns from LadybugDB.
1376
+ const normalized = rows.map((r) => ({
1377
+ id: (r.id ?? r[0]),
1378
+ name: (r.name ?? r[1]),
1379
+ type: (r.type ?? r[2] ?? ''),
1380
+ filePath: (r.filePath ?? r[3]),
1381
+ startLine: (r.startLine ?? r[4]),
1382
+ endLine: (r.endLine ?? r[5]),
1383
+ ...(include_content ? { content: (r.content ?? r[6]) } : {}),
1384
+ }));
1385
+ // Enrich labels for any candidates where `labels(n)[0]` came back empty.
1386
+ // LadybugDB returns an empty string for that projection on certain node
1387
+ // types (notably Class), which left downstream consumers (impact's
1388
+ // Class/Interface BFS seed, the kind-priority scoring bonus) unable to
1389
+ // distinguish a Class target from "unknown kind". One scoped UNION
1390
+ // across the five priority labels patches the type in-place without
1391
+ // per-candidate round-trips.
1392
+ await this.enrichCandidateLabels(repo, normalized);
1393
+ // Preserve #480 Class/Constructor collapse: if we have exactly one
1394
+ // Class (or Interface) candidate and one Constructor sharing name +
1395
+ // filePath, fold into the Class. This used to require a follow-up
1396
+ // label query because LadybugDB sometimes returns an empty labels()[0]
1397
+ // for Class nodes — enrichment above handles the empty-type case, but
1398
+ // the `type === 'Constructor'` gate still correctly triggers when a
1399
+ // Class and its Constructor share the name.
1400
+ if (!hints.kind && normalized.length > 1) {
1401
+ const ambiguousType = normalized.some((s) => s.type === '' || s.type === 'Constructor');
1402
+ if (ambiguousType) {
1403
+ const candidateIds = normalized.map((s) => s.id).filter(Boolean);
1404
+ for (const label of ['Class', 'Interface']) {
1405
+ const labelRows = await executeParameterized(repo.id, `MATCH (n:\`${label}\`) WHERE n.id IN $candidateIds RETURN n.id AS id LIMIT 1`, { candidateIds }).catch(() => []);
1406
+ if (labelRows.length > 0) {
1407
+ const preferredId = labelRows[0].id ?? labelRows[0][0];
1408
+ const preferred = normalized.find((s) => s.id === preferredId);
1409
+ if (preferred) {
1410
+ return {
1411
+ kind: 'ok',
1412
+ symbol: preferred,
1413
+ resolvedLabel: label,
1414
+ };
1415
+ }
1416
+ }
1417
+ }
1418
+ }
1419
+ }
1420
+ if (normalized.length === 1) {
1421
+ return {
1422
+ kind: 'ok',
1423
+ symbol: normalized[0],
1424
+ resolvedLabel: '',
1425
+ };
1426
+ }
1427
+ // Score, sort desc, stable tiebreak on shorter filePath then lex uid.
1428
+ const scored = normalized.map((s) => ({
1429
+ ...s,
1430
+ score: this.scoreCandidate({ kind: s.type, filePath: s.filePath || '' }, hints),
1431
+ }));
1432
+ scored.sort((a, b) => {
1433
+ if (b.score !== a.score)
1434
+ return b.score - a.score;
1435
+ const fpA = (a.filePath || '').length;
1436
+ const fpB = (b.filePath || '').length;
1437
+ if (fpA !== fpB)
1438
+ return fpA - fpB;
1439
+ return String(a.id).localeCompare(String(b.id));
1440
+ });
1441
+ // Confident single-result: top score ≥ 0.95 AND beats runner-up by a
1442
+ // clear margin. This lets a very strong file_path/kind hint resolve
1443
+ // cleanly instead of forcing the caller through a disambiguation
1444
+ // round-trip.
1445
+ //
1446
+ // The gap threshold uses `> 0.09` rather than `>= 0.10` on purpose:
1447
+ // IEEE754 addition of the scoring terms (0.50 + 0.40 + 0.20 - 0.90
1448
+ // yields 0.09999999999999998, not exactly 0.10) would otherwise break
1449
+ // the comparison for legitimate "top is 1.00, runner is 0.90" cases.
1450
+ // The intent is a clearly-dominant winner; 0.09 is a large enough
1451
+ // margin to mean that unambiguously.
1452
+ //
1453
+ // The `scored.length >= 2` guard is defensive. The `normalized.length === 1`
1454
+ // early return above already handles the single-candidate path, so in
1455
+ // practice `scored` always has at least two elements by the time we get
1456
+ // here — keeping the guard means changes to the upstream early-return
1457
+ // logic cannot accidentally index out of bounds at `scored[1]`.
1458
+ if (scored.length >= 2 && scored[0].score >= 0.95 && scored[0].score - scored[1].score > 0.09) {
1459
+ return { kind: 'ok', symbol: scored[0], resolvedLabel: scored[0].type };
1460
+ }
1461
+ return { kind: 'ambiguous', candidates: scored };
1462
+ }
1463
+ /**
1464
+ * Context tool — 360-degree symbol view with categorized refs.
1465
+ * Disambiguation (ranked) when multiple symbols share a name.
1466
+ * UID-based direct lookup. No cluster in output.
1467
+ */
1468
+ async context(repo, params) {
1469
+ await this.ensureInitialized(repo.id);
1470
+ const { name, uid, file_path, kind, include_content } = params;
1471
+ if (!name && !uid) {
1472
+ return { error: 'Either "name" or "uid" parameter is required.' };
1473
+ }
1474
+ const outcome = await this.resolveSymbolCandidates(repo, { uid, name, include_content }, { file_path, kind });
1475
+ if (outcome.kind === 'not_found') {
1476
+ return { error: `Symbol '${name || uid}' not found` };
1477
+ }
1478
+ if (outcome.kind === 'ambiguous') {
1479
+ return {
1480
+ status: 'ambiguous',
1481
+ message: `Found ${outcome.candidates.length} symbols matching '${name}'. Use uid, file_path, or kind to disambiguate.`,
1482
+ candidates: outcome.candidates.map((c) => ({
1483
+ uid: c.id,
1484
+ name: c.name,
1485
+ kind: c.type,
1486
+ filePath: c.filePath,
1487
+ line: c.startLine,
1488
+ score: Number(c.score.toFixed(2)),
1489
+ })),
1490
+ };
1491
+ }
1492
+ // Step 3: Build full context
1493
+ const sym = outcome.symbol;
1494
+ const resolvedLabel = outcome.resolvedLabel;
1495
+ const symId = sym.id;
1496
+ // Categorized incoming refs
1497
+ const incomingRows = await executeParameterized(repo.id, `
1498
+ MATCH (caller)-[r:CodeRelation]->(n {id: $symId})
1499
+ WHERE r.type IN ['CALLS', 'IMPORTS', 'EXTENDS', 'IMPLEMENTS', 'HAS_METHOD', 'HAS_PROPERTY', 'METHOD_OVERRIDES', 'OVERRIDES', 'METHOD_IMPLEMENTS', 'ACCESSES']
1500
+ RETURN r.type AS relType, caller.id AS uid, caller.name AS name, caller.filePath AS filePath, labels(caller)[0] AS kind
1501
+ LIMIT 30
1502
+ `, { symId });
1503
+ // Fix #480: Class/Interface nodes have no direct CALLS/IMPORTS edges —
1504
+ // those point to Constructor and File nodes respectively. Fetch those
1505
+ // extra incoming refs and merge them in so context() shows real callers.
1506
+ //
1507
+ // Determine if this is a Class/Interface node. If resolvedLabel was set
1508
+ // during disambiguation (Step 2), use it directly — no extra round-trip.
1509
+ // Otherwise fall back to a single label check only when the type field is
1510
+ // empty (LadybugDB labels(n)[0] limitation).
1511
+ const symRawType = sym.type || sym[2] || '';
1512
+ let isClassLike = resolvedLabel === 'Class' || resolvedLabel === 'Interface';
1513
+ if (!isClassLike && symRawType === '') {
1514
+ try {
1515
+ // Single UNION query instead of two serial round-trips.
1516
+ const typeCheck = await executeParameterized(repo.id, `
1517
+ MATCH (n:Class) WHERE n.id = $symId RETURN 'Class' AS label LIMIT 1
1518
+ UNION ALL
1519
+ MATCH (n:Interface) WHERE n.id = $symId RETURN 'Interface' AS label LIMIT 1
1520
+ `, { symId });
1521
+ isClassLike = typeCheck.length > 0;
1522
+ }
1523
+ catch {
1524
+ /* not a Class/Interface node */
1525
+ }
1526
+ }
1527
+ else if (!isClassLike) {
1528
+ isClassLike = symRawType === 'Class' || symRawType === 'Interface';
1529
+ }
1530
+ if (isClassLike) {
1531
+ try {
1532
+ // Run both incoming-ref queries in parallel — they are independent.
1533
+ const [ctorIncoming, fileIncoming] = await Promise.all([
1534
+ executeParameterized(repo.id, `
1535
+ MATCH (n)-[hm:CodeRelation]->(ctor:Constructor)
1536
+ WHERE n.id = $symId AND hm.type = 'HAS_METHOD'
1537
+ MATCH (caller)-[r:CodeRelation]->(ctor)
1538
+ WHERE r.type IN ['CALLS', 'IMPORTS', 'EXTENDS', 'IMPLEMENTS', 'ACCESSES']
1539
+ RETURN r.type AS relType, caller.id AS uid, caller.name AS name, caller.filePath AS filePath, labels(caller)[0] AS kind
1540
+ LIMIT 30
1541
+ `, { symId }),
1542
+ executeParameterized(repo.id, `
1543
+ MATCH (f:File)-[rel:CodeRelation]->(n)
1544
+ WHERE n.id = $symId AND rel.type = 'DEFINES'
1545
+ MATCH (caller)-[r:CodeRelation]->(f)
1546
+ WHERE r.type IN ['CALLS', 'IMPORTS']
1547
+ RETURN r.type AS relType, caller.id AS uid, caller.name AS name, caller.filePath AS filePath, labels(caller)[0] AS kind
1548
+ LIMIT 30
1549
+ `, { symId }),
1550
+ ]);
1551
+ // Deduplicate by (relType, uid) — a caller can have multiple relation
1552
+ // types to the same target (e.g. both IMPORTS and CALLS), and each
1553
+ // must be preserved so every category appears in the output.
1554
+ const seenKeys = new Set(incomingRows.map((r) => `${r.relType || r[0]}:${r.uid || r[1]}`));
1555
+ for (const r of [...ctorIncoming, ...fileIncoming]) {
1556
+ const key = `${r.relType || r[0]}:${r.uid || r[1]}`;
1557
+ if (!seenKeys.has(key)) {
1558
+ seenKeys.add(key);
1559
+ incomingRows.push(r);
1560
+ }
1561
+ }
1562
+ }
1563
+ catch (e) {
1564
+ logQueryError('context:class-incoming-expansion', e);
1565
+ }
1566
+ }
1567
+ // Categorized outgoing refs
1568
+ const outgoingRows = await executeParameterized(repo.id, `
1569
+ MATCH (n {id: $symId})-[r:CodeRelation]->(target)
1570
+ WHERE r.type IN ['CALLS', 'IMPORTS', 'EXTENDS', 'IMPLEMENTS', 'HAS_METHOD', 'HAS_PROPERTY', 'METHOD_OVERRIDES', 'OVERRIDES', 'METHOD_IMPLEMENTS', 'ACCESSES']
1571
+ RETURN r.type AS relType, target.id AS uid, target.name AS name, target.filePath AS filePath, labels(target)[0] AS kind
1572
+ LIMIT 30
1573
+ `, { symId });
1574
+ // Process participation
1575
+ let processRows = [];
1576
+ try {
1577
+ processRows = await executeParameterized(repo.id, `
1578
+ MATCH (n {id: $symId})-[r:CodeRelation {type: 'STEP_IN_PROCESS'}]->(p:Process)
1579
+ RETURN p.id AS pid, p.heuristicLabel AS label, r.step AS step, p.stepCount AS stepCount
1580
+ `, { symId });
1581
+ }
1582
+ catch (e) {
1583
+ logQueryError('context:process-participation', e);
1584
+ }
1585
+ // Helper to categorize refs
1586
+ const categorize = (rows) => {
1587
+ const cats = {};
1588
+ for (const row of rows) {
1589
+ const relType = (row.relType || row[0] || '').toLowerCase();
1590
+ const entry = {
1591
+ uid: row.uid || row[1],
1592
+ name: row.name || row[2],
1593
+ filePath: row.filePath || row[3],
1594
+ kind: row.kind || row[4],
1595
+ };
1596
+ if (!cats[relType])
1597
+ cats[relType] = [];
1598
+ cats[relType].push(entry);
1599
+ }
1600
+ return cats;
1601
+ };
1602
+ // Method/Function/Constructor enrichment: fetch method-specific properties
1603
+ const symKind = isClassLike ? resolvedLabel || 'Class' : sym.type || sym[2];
1604
+ const isMethodLike = symKind === 'Method' || symKind === 'Function' || symKind === 'Constructor';
1605
+ let methodMetadata;
1606
+ if (isMethodLike) {
1607
+ try {
1608
+ const metaRows = await executeParameterized(repo.id, `
1609
+ MATCH (n {id: $symId})
1610
+ RETURN n.visibility AS visibility, n.isStatic AS isStatic, n.isAbstract AS isAbstract,
1611
+ n.isFinal AS isFinal, n.isVirtual AS isVirtual, n.isOverride AS isOverride,
1612
+ n.isAsync AS isAsync, n.isPartial AS isPartial, n.returnType AS returnType,
1613
+ n.parameterCount AS parameterCount, n.isVariadic AS isVariadic,
1614
+ n.requiredParameterCount AS requiredParameterCount,
1615
+ n.parameterTypes AS parameterTypes, n.annotations AS annotations
1616
+ LIMIT 1
1617
+ `, { symId });
1618
+ if (metaRows.length > 0) {
1619
+ const row = metaRows[0];
1620
+ const meta = {};
1621
+ // Only include defined properties to distinguish "not applicable" from "not enriched"
1622
+ for (const key of Object.keys(row)) {
1623
+ const val = row[key];
1624
+ if (val !== null && val !== undefined)
1625
+ meta[key] = val;
1626
+ }
1627
+ if (Object.keys(meta).length > 0)
1628
+ methodMetadata = meta;
1629
+ }
1630
+ }
1631
+ catch {
1632
+ /* method metadata unavailable — omit silently */
1633
+ }
1634
+ }
1635
+ return {
1636
+ status: 'found',
1637
+ symbol: {
1638
+ uid: sym.id || sym[0],
1639
+ name: sym.name || sym[1],
1640
+ kind: symKind,
1641
+ filePath: sym.filePath || sym[3],
1642
+ startLine: sym.startLine || sym[4],
1643
+ endLine: sym.endLine || sym[5],
1644
+ ...(include_content && (sym.content || sym[6]) ? { content: sym.content || sym[6] } : {}),
1645
+ ...(methodMetadata ? { methodMetadata } : {}),
1646
+ },
1647
+ incoming: categorize(incomingRows),
1648
+ outgoing: categorize(outgoingRows),
1649
+ processes: processRows.map((r) => ({
1650
+ id: r.pid || r[0],
1651
+ name: r.label || r[1],
1652
+ step_index: r.step || r[2],
1653
+ step_count: r.stepCount || r[3],
1654
+ })),
1655
+ };
1656
+ }
1657
+ /**
1658
+ * Legacy explore — kept for backwards compatibility with resources.ts.
1659
+ * Routes cluster/process types to direct graph queries.
1660
+ */
1661
+ async explore(repo, params) {
1662
+ await this.ensureInitialized(repo.id);
1663
+ const { name, type } = params;
1664
+ if (type === 'symbol') {
1665
+ return this.context(repo, { name });
1666
+ }
1667
+ if (type === 'cluster') {
1668
+ const clusters = await executeParameterized(repo.id, `
1669
+ MATCH (c:Community)
1670
+ WHERE c.label = $clusterName OR c.heuristicLabel = $clusterName
1671
+ RETURN c.id AS id, c.label AS label, c.heuristicLabel AS heuristicLabel, c.cohesion AS cohesion, c.symbolCount AS symbolCount
1672
+ `, { clusterName: name });
1673
+ if (clusters.length === 0)
1674
+ return { error: `Cluster '${name}' not found` };
1675
+ const rawClusters = clusters.map((c) => ({
1676
+ id: c.id || c[0],
1677
+ label: c.label || c[1],
1678
+ heuristicLabel: c.heuristicLabel || c[2],
1679
+ cohesion: c.cohesion || c[3],
1680
+ symbolCount: c.symbolCount || c[4],
1681
+ }));
1682
+ let totalSymbols = 0, weightedCohesion = 0;
1683
+ for (const c of rawClusters) {
1684
+ const s = c.symbolCount || 0;
1685
+ totalSymbols += s;
1686
+ weightedCohesion += (c.cohesion || 0) * s;
1687
+ }
1688
+ const members = await executeParameterized(repo.id, `
1689
+ MATCH (n)-[:CodeRelation {type: 'MEMBER_OF'}]->(c:Community)
1690
+ WHERE c.label = $clusterName OR c.heuristicLabel = $clusterName
1691
+ RETURN DISTINCT n.name AS name, labels(n)[0] AS type, n.filePath AS filePath
1692
+ LIMIT 30
1693
+ `, { clusterName: name });
1694
+ return {
1695
+ cluster: {
1696
+ id: rawClusters[0].id,
1697
+ label: rawClusters[0].heuristicLabel || rawClusters[0].label,
1698
+ heuristicLabel: rawClusters[0].heuristicLabel || rawClusters[0].label,
1699
+ cohesion: totalSymbols > 0 ? weightedCohesion / totalSymbols : 0,
1700
+ symbolCount: totalSymbols,
1701
+ subCommunities: rawClusters.length,
1702
+ },
1703
+ members: members.map((m) => ({
1704
+ name: m.name || m[0],
1705
+ type: m.type || m[1],
1706
+ filePath: m.filePath || m[2],
1707
+ })),
1708
+ };
1709
+ }
1710
+ if (type === 'process') {
1711
+ const processes = await executeParameterized(repo.id, `
1712
+ MATCH (p:Process)
1713
+ WHERE p.label = $processName OR p.heuristicLabel = $processName
1714
+ RETURN p.id AS id, p.label AS label, p.heuristicLabel AS heuristicLabel, p.processType AS processType, p.stepCount AS stepCount
1715
+ LIMIT 1
1716
+ `, { processName: name });
1717
+ if (processes.length === 0)
1718
+ return { error: `Process '${name}' not found` };
1719
+ const proc = processes[0];
1720
+ const procId = proc.id || proc[0];
1721
+ const steps = await executeParameterized(repo.id, `
1722
+ MATCH (n)-[r:CodeRelation {type: 'STEP_IN_PROCESS'}]->(p {id: $procId})
1723
+ RETURN n.name AS name, labels(n)[0] AS type, n.filePath AS filePath, r.step AS step
1724
+ ORDER BY r.step
1725
+ `, { procId });
1726
+ return {
1727
+ process: {
1728
+ id: procId,
1729
+ label: proc.label || proc[1],
1730
+ heuristicLabel: proc.heuristicLabel || proc[2],
1731
+ processType: proc.processType || proc[3],
1732
+ stepCount: proc.stepCount || proc[4],
1733
+ },
1734
+ steps: steps.map((s) => ({
1735
+ step: s.step || s[3],
1736
+ name: s.name || s[0],
1737
+ type: s.type || s[1],
1738
+ filePath: s.filePath || s[2],
1739
+ })),
1740
+ };
1741
+ }
1742
+ return { error: 'Invalid type. Use: symbol, cluster, or process' };
1743
+ }
1744
+ /**
1745
+ * Detect changes — git-diff based impact analysis.
1746
+ * Maps changed lines to indexed symbols, then finds affected processes.
1747
+ */
1748
+ async detectChanges(repo, params) {
1749
+ await this.ensureInitialized(repo.id);
1750
+ const scope = params.scope || 'unstaged';
1751
+ const { execFileSync } = await import('child_process');
1752
+ // Build git diff args based on scope (using execFileSync to avoid shell injection)
1753
+ let diffArgs;
1754
+ switch (scope) {
1755
+ case 'staged':
1756
+ diffArgs = ['diff', '--staged', '-U0'];
1757
+ break;
1758
+ case 'all':
1759
+ diffArgs = ['diff', 'HEAD', '-U0'];
1760
+ break;
1761
+ case 'compare':
1762
+ if (!params.base_ref)
1763
+ return { error: 'base_ref is required for "compare" scope' };
1764
+ diffArgs = ['diff', params.base_ref, '-U0'];
1765
+ break;
1766
+ case 'unstaged':
1767
+ default:
1768
+ diffArgs = ['diff', '-U0'];
1769
+ break;
1770
+ }
1771
+ let diffOutput;
1772
+ try {
1773
+ // maxBuffer raised from Node's 1MB default to 256MB to avoid ENOBUFS on
1774
+ // repos with large unstaged/untracked diffs (e.g. unignored build folders).
1775
+ // See issue: spawnSync git ENOBUFS in detect_changes(scope="unstaged").
1776
+ diffOutput = execFileSync('git', diffArgs, {
1777
+ cwd: repo.repoPath,
1778
+ encoding: 'utf-8',
1779
+ maxBuffer: 256 * 1024 * 1024,
1780
+ });
1781
+ }
1782
+ catch (err) {
1783
+ return { error: `Git diff failed: ${err.message}` };
1784
+ }
1785
+ const fileDiffs = parseDiffHunks(diffOutput);
1786
+ if (fileDiffs.length === 0) {
1787
+ return {
1788
+ summary: {
1789
+ changed_count: 0,
1790
+ affected_count: 0,
1791
+ risk_level: 'none',
1792
+ message: 'No changes detected.',
1793
+ },
1794
+ changed_symbols: [],
1795
+ affected_processes: [],
1796
+ };
1797
+ }
1798
+ // Map diff hunks to indexed symbols via range overlap
1799
+ const changedSymbols = [];
1800
+ for (const fileDiff of fileDiffs) {
1801
+ if (fileDiff.hunks.length === 0)
1802
+ continue;
1803
+ // Build range overlap conditions for all hunks in this file
1804
+ const overlapConditions = fileDiff.hunks
1805
+ .map((_, i) => `(n.startLine <= $hunkEnd${i} AND n.endLine >= $hunkStart${i})`)
1806
+ .join(' OR ');
1807
+ const queryParams = { filePath: fileDiff.filePath };
1808
+ fileDiff.hunks.forEach((hunk, i) => {
1809
+ queryParams[`hunkStart${i}`] = hunk.startLine;
1810
+ queryParams[`hunkEnd${i}`] = hunk.endLine;
1811
+ });
1812
+ const symbolQuery = `
1813
+ MATCH (n) WHERE n.filePath ENDS WITH $filePath
1814
+ AND n.startLine IS NOT NULL AND n.endLine IS NOT NULL
1815
+ AND (${overlapConditions})
1816
+ RETURN n.id AS id, n.name AS name, labels(n)[0] AS type,
1817
+ n.filePath AS filePath, n.startLine AS startLine, n.endLine AS endLine
1818
+ `;
1819
+ try {
1820
+ const rows = await executeParameterized(repo.id, symbolQuery, queryParams);
1821
+ for (const sym of rows) {
1822
+ changedSymbols.push({
1823
+ id: sym.id || sym[0],
1824
+ name: sym.name || sym[1],
1825
+ type: sym.type || sym[2],
1826
+ filePath: sym.filePath || sym[3],
1827
+ change_type: 'touched',
1828
+ });
1829
+ }
1830
+ }
1831
+ catch (e) {
1832
+ logQueryError('detect-changes:file-symbols', e);
1833
+ }
1834
+ }
1835
+ // Find affected processes -- single batched query instead of N+1
1836
+ const affectedProcesses = new Map();
1837
+ if (changedSymbols.length > 0) {
1838
+ const symIds = changedSymbols.map((s) => s.id);
1839
+ const symNameById = new Map(changedSymbols.map((s) => [s.id, s.name]));
1840
+ try {
1841
+ const procs = await executeParameterized(repo.id, `
1842
+ MATCH (n)-[r:CodeRelation {type: 'STEP_IN_PROCESS'}]->(p:Process)
1843
+ WHERE n.id IN $ids
1844
+ RETURN n.id AS nodeId, p.id AS pid, p.heuristicLabel AS label,
1845
+ p.processType AS processType, p.stepCount AS stepCount, r.step AS step
1846
+ `, { ids: symIds });
1847
+ for (const proc of procs) {
1848
+ const nodeId = proc.nodeId || proc[0];
1849
+ const pid = proc.pid || proc[1];
1850
+ if (!affectedProcesses.has(pid)) {
1851
+ affectedProcesses.set(pid, {
1852
+ id: pid,
1853
+ name: proc.label || proc[2],
1854
+ process_type: proc.processType || proc[3],
1855
+ step_count: proc.stepCount || proc[4],
1856
+ changed_steps: [],
1857
+ });
1858
+ }
1859
+ affectedProcesses.get(pid).changed_steps.push({
1860
+ symbol: symNameById.get(nodeId) ?? nodeId,
1861
+ step: proc.step || proc[5],
1862
+ });
1863
+ }
1864
+ }
1865
+ catch (e) {
1866
+ logQueryError('detect-changes:process-lookup', e);
1867
+ }
1868
+ }
1869
+ const processCount = affectedProcesses.size;
1870
+ const risk = processCount === 0
1871
+ ? 'low'
1872
+ : processCount <= 5
1873
+ ? 'medium'
1874
+ : processCount <= 15
1875
+ ? 'high'
1876
+ : 'critical';
1877
+ return {
1878
+ summary: {
1879
+ changed_count: changedSymbols.length,
1880
+ affected_count: processCount,
1881
+ changed_files: fileDiffs.length,
1882
+ risk_level: risk,
1883
+ },
1884
+ changed_symbols: changedSymbols,
1885
+ affected_processes: Array.from(affectedProcesses.values()),
1886
+ };
1887
+ }
1888
+ /**
1889
+ * Rename tool — multi-file coordinated rename using graph + text search.
1890
+ * Graph refs are tagged "graph" (high confidence).
1891
+ * Additional refs found via text search are tagged "text_search" (lower confidence).
1892
+ */
1893
+ async rename(repo, params) {
1894
+ await this.ensureInitialized(repo.id);
1895
+ const { new_name, file_path } = params;
1896
+ const dry_run = params.dry_run ?? true;
1897
+ if (!params.symbol_name && !params.symbol_uid) {
1898
+ return { error: 'Either symbol_name or symbol_uid is required.' };
1899
+ }
1900
+ /** Guard: ensure a file path resolves within the repo root (prevents path traversal) */
1901
+ const assertSafePath = (filePath) => {
1902
+ const full = path.resolve(repo.repoPath, filePath);
1903
+ if (!full.startsWith(repo.repoPath + path.sep) && full !== repo.repoPath) {
1904
+ throw new Error(`Path traversal blocked: ${filePath}`);
1905
+ }
1906
+ return full;
1907
+ };
1908
+ // Step 1: Find the target symbol (reuse context's lookup)
1909
+ const lookupResult = await this.context(repo, {
1910
+ name: params.symbol_name,
1911
+ uid: params.symbol_uid,
1912
+ file_path,
1913
+ });
1914
+ if (lookupResult.status === 'ambiguous') {
1915
+ return lookupResult; // pass disambiguation through
1916
+ }
1917
+ if (lookupResult.error) {
1918
+ return lookupResult;
1919
+ }
1920
+ const sym = lookupResult.symbol;
1921
+ const oldName = sym.name;
1922
+ if (oldName === new_name) {
1923
+ return { error: 'New name is the same as the current name.' };
1924
+ }
1925
+ // Step 2: Collect edits from graph (high confidence)
1926
+ const changes = new Map();
1927
+ const addEdit = (filePath, line, oldText, newText, confidence) => {
1928
+ if (!changes.has(filePath)) {
1929
+ changes.set(filePath, { file_path: filePath, edits: [] });
1930
+ }
1931
+ changes.get(filePath).edits.push({ line, old_text: oldText, new_text: newText, confidence });
1932
+ };
1933
+ // The definition itself
1934
+ if (sym.filePath && sym.startLine) {
1935
+ try {
1936
+ const content = await fs.readFile(assertSafePath(sym.filePath), 'utf-8');
1937
+ const lines = content.split('\n');
1938
+ const lineIdx = sym.startLine - 1;
1939
+ if (lineIdx >= 0 && lineIdx < lines.length && lines[lineIdx].includes(oldName)) {
1940
+ const defRegex = new RegExp(`\\b${oldName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\b`, 'g');
1941
+ addEdit(sym.filePath, sym.startLine, lines[lineIdx].trim(), lines[lineIdx].replace(defRegex, new_name).trim(), 'graph');
1942
+ }
1943
+ }
1944
+ catch (e) {
1945
+ logQueryError('rename:read-definition', e);
1946
+ }
1947
+ }
1948
+ // All incoming refs from graph (callers, importers, etc.)
1949
+ const allIncoming = [
1950
+ ...(lookupResult.incoming.calls || []),
1951
+ ...(lookupResult.incoming.imports || []),
1952
+ ...(lookupResult.incoming.extends || []),
1953
+ ...(lookupResult.incoming.implements || []),
1954
+ ];
1955
+ let graphEdits = changes.size > 0 ? 1 : 0; // count definition edit
1956
+ for (const ref of allIncoming) {
1957
+ if (!ref.filePath)
1958
+ continue;
1959
+ try {
1960
+ const content = await fs.readFile(assertSafePath(ref.filePath), 'utf-8');
1961
+ const lines = content.split('\n');
1962
+ for (let i = 0; i < lines.length; i++) {
1963
+ if (lines[i].includes(oldName)) {
1964
+ addEdit(ref.filePath, i + 1, lines[i].trim(), lines[i]
1965
+ .replace(new RegExp(`\\b${oldName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\b`, 'g'), new_name)
1966
+ .trim(), 'graph');
1967
+ graphEdits++;
1968
+ break; // one edit per file from graph refs
1969
+ }
1970
+ }
1971
+ }
1972
+ catch (e) {
1973
+ logQueryError('rename:read-ref', e);
1974
+ }
1975
+ }
1976
+ // Step 3: Text search for refs the graph might have missed
1977
+ let astSearchEdits = 0;
1978
+ const graphFiles = new Set([sym.filePath, ...allIncoming.map((r) => r.filePath)].filter(Boolean));
1979
+ // Simple text search across the repo for the old name (in files not already covered by graph)
1980
+ try {
1981
+ const { execFileSync } = await import('child_process');
1982
+ const rgArgs = [
1983
+ '-l',
1984
+ '--type-add',
1985
+ 'code:*.{ts,tsx,js,jsx,py,go,rs,java,c,h,cpp,cc,cxx,hpp,hxx,hh,cs,php,swift}',
1986
+ '-t',
1987
+ 'code',
1988
+ `\\b${oldName}\\b`,
1989
+ '.',
1990
+ ];
1991
+ const output = execFileSync('rg', rgArgs, {
1992
+ cwd: repo.repoPath,
1993
+ encoding: 'utf-8',
1994
+ timeout: 5000,
1995
+ // Avoid ENOBUFS on large repos: rg -l can list many files.
1996
+ maxBuffer: 256 * 1024 * 1024,
1997
+ });
1998
+ const files = output
1999
+ .trim()
2000
+ .split('\n')
2001
+ .filter((f) => f.length > 0);
2002
+ for (const file of files) {
2003
+ const normalizedFile = file.replace(/\\/g, '/').replace(/^\.\//, '');
2004
+ if (graphFiles.has(normalizedFile))
2005
+ continue; // already covered by graph
2006
+ try {
2007
+ const content = await fs.readFile(assertSafePath(normalizedFile), 'utf-8');
2008
+ const lines = content.split('\n');
2009
+ const regex = new RegExp(`\\b${oldName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\b`, 'g');
2010
+ for (let i = 0; i < lines.length; i++) {
2011
+ regex.lastIndex = 0;
2012
+ if (regex.test(lines[i])) {
2013
+ regex.lastIndex = 0;
2014
+ addEdit(normalizedFile, i + 1, lines[i].trim(), lines[i].replace(regex, new_name).trim(), 'text_search');
2015
+ astSearchEdits++;
2016
+ }
2017
+ }
2018
+ }
2019
+ catch (e) {
2020
+ logQueryError('rename:text-search-read', e);
2021
+ }
2022
+ }
2023
+ }
2024
+ catch (e) {
2025
+ logQueryError('rename:ripgrep', e);
2026
+ }
2027
+ // Step 4: Apply or preview
2028
+ const allChanges = Array.from(changes.values());
2029
+ const totalEdits = allChanges.reduce((sum, c) => sum + c.edits.length, 0);
2030
+ if (!dry_run) {
2031
+ // Apply edits to files
2032
+ for (const change of allChanges) {
2033
+ try {
2034
+ const fullPath = assertSafePath(change.file_path);
2035
+ let content = await fs.readFile(fullPath, 'utf-8');
2036
+ const regex = new RegExp(`\\b${oldName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\b`, 'g');
2037
+ content = content.replace(regex, new_name);
2038
+ await fs.writeFile(fullPath, content, 'utf-8');
2039
+ }
2040
+ catch (e) {
2041
+ logQueryError('rename:apply-edit', e);
2042
+ }
2043
+ }
2044
+ }
2045
+ return {
2046
+ status: 'success',
2047
+ old_name: oldName,
2048
+ new_name,
2049
+ files_affected: allChanges.length,
2050
+ total_edits: totalEdits,
2051
+ graph_edits: graphEdits,
2052
+ text_search_edits: astSearchEdits,
2053
+ changes: allChanges,
2054
+ applied: !dry_run,
2055
+ };
2056
+ }
2057
+ async impact(repo, params) {
2058
+ try {
2059
+ return await this._impactImpl(repo, params);
2060
+ }
2061
+ catch (err) {
2062
+ // Return structured error instead of crashing (#321)
2063
+ return {
2064
+ error: (err instanceof Error ? err.message : String(err)) || 'Impact analysis failed',
2065
+ target: { name: params.target },
2066
+ direction: params.direction,
2067
+ impactedCount: 0,
2068
+ risk: 'UNKNOWN',
2069
+ suggestion: 'The graph query failed — try codragraph context <symbol> as a fallback',
2070
+ };
2071
+ }
2072
+ }
2073
+ async _impactImpl(repo, params) {
2074
+ await this.ensureInitialized(repo.id);
2075
+ const { target, direction } = params;
2076
+ const maxDepth = params.maxDepth || 3;
2077
+ // Map legacy relation type names before filtering (backward compat for OVERRIDES → METHOD_OVERRIDES)
2078
+ const mappedRelTypes = params.relationTypes?.flatMap((t) => t === 'OVERRIDES' ? ['OVERRIDES', 'METHOD_OVERRIDES'] : [t]);
2079
+ const rawRelTypes = mappedRelTypes && mappedRelTypes.length > 0
2080
+ ? mappedRelTypes.filter((t) => VALID_RELATION_TYPES.has(t))
2081
+ : [
2082
+ 'CALLS',
2083
+ 'IMPORTS',
2084
+ 'EXTENDS',
2085
+ 'IMPLEMENTS',
2086
+ 'METHOD_OVERRIDES',
2087
+ 'OVERRIDES',
2088
+ 'METHOD_IMPLEMENTS',
2089
+ ];
2090
+ const relationTypes = rawRelTypes.length > 0
2091
+ ? rawRelTypes
2092
+ : [
2093
+ 'CALLS',
2094
+ 'IMPORTS',
2095
+ 'EXTENDS',
2096
+ 'IMPLEMENTS',
2097
+ 'METHOD_OVERRIDES',
2098
+ 'OVERRIDES',
2099
+ 'METHOD_IMPLEMENTS',
2100
+ ];
2101
+ const includeTests = params.includeTests ?? false;
2102
+ const minConfidence = params.minConfidence ?? 0;
2103
+ // Resolve target via the shared symbol resolver. When the caller passes
2104
+ // target_uid we skip the name lookup entirely (zero-ambiguity). Otherwise
2105
+ // we rank candidates (#470) and either proceed with a confident single
2106
+ // match, or return a structured ambiguous response instead of silently
2107
+ // picking the wrong symbol.
2108
+ //
2109
+ // The resolver preserves the #480 Class/Constructor preference heuristic:
2110
+ // when a Class and its Constructor share name + filePath, the Class is
2111
+ // selected silently.
2112
+ const outcome = await this.resolveSymbolCandidates(repo, { uid: params.target_uid, name: target }, { file_path: params.file_path, kind: params.kind });
2113
+ if (outcome.kind === 'not_found') {
2114
+ const missing = params.target_uid ?? target;
2115
+ return {
2116
+ error: `Target '${missing}' not found`,
2117
+ target: { name: target },
2118
+ direction,
2119
+ impactedCount: 0,
2120
+ risk: 'UNKNOWN',
2121
+ };
2122
+ }
2123
+ if (outcome.kind === 'ambiguous') {
2124
+ return {
2125
+ status: 'ambiguous',
2126
+ message: `Found ${outcome.candidates.length} symbols matching '${target}'. Use target_uid, file_path, or kind to disambiguate.`,
2127
+ target: { name: target },
2128
+ direction,
2129
+ impactedCount: 0,
2130
+ risk: 'UNKNOWN',
2131
+ candidates: outcome.candidates.map((c) => ({
2132
+ uid: c.id,
2133
+ name: c.name,
2134
+ kind: c.type,
2135
+ filePath: c.filePath,
2136
+ line: c.startLine,
2137
+ score: Number(c.score.toFixed(2)),
2138
+ })),
2139
+ };
2140
+ }
2141
+ const sym = {
2142
+ id: outcome.symbol.id,
2143
+ name: outcome.symbol.name,
2144
+ filePath: outcome.symbol.filePath,
2145
+ };
2146
+ const symType = outcome.resolvedLabel || outcome.symbol.type || '';
2147
+ return this._runImpactBFS(repo, sym, symType, direction, {
2148
+ maxDepth,
2149
+ relationTypes,
2150
+ includeTests,
2151
+ minConfidence,
2152
+ });
2153
+ }
2154
+ /**
2155
+ * Shared BFS traversal for impact analysis (name-resolved or UID-resolved symbol).
2156
+ */
2157
+ async _runImpactBFS(repo, sym, symType, direction, opts) {
2158
+ const { maxDepth, relationTypes, includeTests, minConfidence } = opts;
2159
+ const relTypeFilter = relationTypes.map((t) => `'${t}'`).join(', ');
2160
+ const confidenceFilter = minConfidence > 0 ? ` AND r.confidence >= ${minConfidence}` : '';
2161
+ const symId = sym.id || sym[0];
2162
+ const impacted = [];
2163
+ const visited = new Set([symId]);
2164
+ let frontier = [symId];
2165
+ let traversalComplete = true;
2166
+ // Fix #480: For Java (and other JVM) Class/Interface nodes, CALLS edges
2167
+ // point to Constructor nodes and IMPORTS edges point to File nodes — not
2168
+ // the Class/Interface itself. Seed the frontier with the Constructor(s)
2169
+ // and owning File so the BFS traversal finds those edges naturally.
2170
+ // The owning File is kept only as an internal seed (frontier/visited) and
2171
+ // is NOT added to impacted — it is the definition container, not an
2172
+ // upstream dependent. The BFS will discover IMPORTS edges on it naturally.
2173
+ if (symType === 'Class' || symType === 'Interface') {
2174
+ try {
2175
+ // Run both seed queries in parallel — they are independent.
2176
+ const [ctorRows, fileRows] = await Promise.all([
2177
+ executeParameterized(repo.id, `
2178
+ MATCH (n)-[hm:CodeRelation]->(c:Constructor)
2179
+ WHERE n.id = $symId AND hm.type = 'HAS_METHOD'
2180
+ RETURN c.id AS id, c.name AS name, labels(c)[0] AS type, c.filePath AS filePath
2181
+ `, { symId }),
2182
+ // Restrict to DEFINES edges only — other File->Class edge types (if
2183
+ // any) should not be treated as the owning file relationship.
2184
+ executeParameterized(repo.id, `
2185
+ MATCH (f:File)-[rel:CodeRelation]->(n)
2186
+ WHERE n.id = $symId AND rel.type = 'DEFINES'
2187
+ RETURN f.id AS id, f.name AS name, labels(f)[0] AS type, f.filePath AS filePath
2188
+ `, { symId }),
2189
+ ]);
2190
+ for (const r of ctorRows) {
2191
+ const rid = r.id || r[0];
2192
+ if (rid && !visited.has(rid)) {
2193
+ visited.add(rid);
2194
+ frontier.push(rid);
2195
+ }
2196
+ }
2197
+ for (const r of fileRows) {
2198
+ const rid = r.id || r[0];
2199
+ if (rid && !visited.has(rid)) {
2200
+ visited.add(rid);
2201
+ frontier.push(rid);
2202
+ }
2203
+ }
2204
+ }
2205
+ catch (e) {
2206
+ logQueryError('impact:class-node-expansion', e);
2207
+ }
2208
+ }
2209
+ for (let depth = 1; depth <= maxDepth && frontier.length > 0; depth++) {
2210
+ const nextFrontier = [];
2211
+ // Batch frontier nodes into a single Cypher query per depth level
2212
+ const idList = frontier.map((id) => `'${id.replace(/'/g, "''")}'`).join(', ');
2213
+ const query = direction === 'upstream'
2214
+ ? `MATCH (caller)-[r:CodeRelation]->(n) WHERE n.id IN [${idList}] AND r.type IN [${relTypeFilter}]${confidenceFilter} RETURN n.id AS sourceId, caller.id AS id, caller.name AS name, labels(caller)[0] AS type, caller.filePath AS filePath, r.type AS relType, r.confidence AS confidence`
2215
+ : `MATCH (n)-[r:CodeRelation]->(callee) WHERE n.id IN [${idList}] AND r.type IN [${relTypeFilter}]${confidenceFilter} RETURN n.id AS sourceId, callee.id AS id, callee.name AS name, labels(callee)[0] AS type, callee.filePath AS filePath, r.type AS relType, r.confidence AS confidence`;
2216
+ try {
2217
+ const related = await executeQuery(repo.id, query);
2218
+ for (const rel of related) {
2219
+ const relId = rel.id || rel[1];
2220
+ const filePath = rel.filePath || rel[4] || '';
2221
+ if (!includeTests && isTestFilePath(filePath))
2222
+ continue;
2223
+ if (!visited.has(relId)) {
2224
+ visited.add(relId);
2225
+ nextFrontier.push(relId);
2226
+ const storedConfidence = rel.confidence ?? rel[6];
2227
+ const relationType = rel.relType || rel[5];
2228
+ // Prefer the stored confidence from the graph (set at analysis time);
2229
+ // fall back to the per-type floor for edges without a stored value.
2230
+ const effectiveConfidence = typeof storedConfidence === 'number' && storedConfidence > 0
2231
+ ? storedConfidence
2232
+ : confidenceForRelType(relationType);
2233
+ impacted.push({
2234
+ depth,
2235
+ id: relId,
2236
+ name: rel.name || rel[2],
2237
+ type: rel.type || rel[3],
2238
+ filePath,
2239
+ relationType,
2240
+ confidence: effectiveConfidence,
2241
+ });
2242
+ }
2243
+ }
2244
+ }
2245
+ catch (e) {
2246
+ logQueryError('impact:depth-traversal', e);
2247
+ // Break out of depth loop on query failure but return partial results
2248
+ // collected so far, rather than silently swallowing the error (#321)
2249
+ traversalComplete = false;
2250
+ break;
2251
+ }
2252
+ frontier = nextFrontier;
2253
+ }
2254
+ const grouped = {};
2255
+ for (const item of impacted) {
2256
+ if (!grouped[item.depth])
2257
+ grouped[item.depth] = [];
2258
+ grouped[item.depth].push(item);
2259
+ }
2260
+ // ── Enrichment: affected processes, modules, risk ──────────────
2261
+ const directCount = (grouped[1] || []).length;
2262
+ let affectedProcesses = [];
2263
+ let affectedModules = [];
2264
+ if (impacted.length > 0) {
2265
+ const CHUNK_SIZE = 100;
2266
+ // Max number of chunks to process to avoid unbounded DB round-trips.
2267
+ // Configurable via env IMPACT_MAX_CHUNKS, default 10 => max items = 1000
2268
+ const MAX_CHUNKS = parseInt(process.env.IMPACT_MAX_CHUNKS || '10', 10);
2269
+ // ── Process enrichment: batched chunking (bounded by MAX_CHUNKS) ─
2270
+ // Uses merged Cypher query (WITH + OPTIONAL MATCH) to fetch
2271
+ // process + entry point info in 1 round-trip per chunk. Converted to
2272
+ // parameterized queries to avoid manual string escaping and long query strings.
2273
+ const entryPointMap = new Map();
2274
+ // Map process id -> entryPointId to allow fixing missing minStep values later
2275
+ const processToEntryPoint = new Map();
2276
+ // Collect process ids where MIN(r.step) returned null so we can retry in batch
2277
+ const processesMissingMinStep = new Set();
2278
+ let chunksProcessed = 0;
2279
+ for (let i = 0; i < impacted.length && chunksProcessed < MAX_CHUNKS; i += CHUNK_SIZE, chunksProcessed++) {
2280
+ const chunk = impacted.slice(i, i + CHUNK_SIZE);
2281
+ const ids = chunk.map((item) => String(item.id ?? ''));
2282
+ try {
2283
+ // Use parameterized list to avoid building long query strings
2284
+ const rows = await executeParameterized(repo.id, `
2285
+ MATCH (s)-[r:CodeRelation {type: 'STEP_IN_PROCESS'}]->(p:Process)
2286
+ WHERE s.id IN $ids
2287
+ WITH p, COUNT(DISTINCT s.id) AS hits, MIN(r.step) AS minStep
2288
+ OPTIONAL MATCH (ep {id: p.entryPointId})
2289
+ RETURN p.id AS pId, p.heuristicLabel AS name, p.processType AS processType,
2290
+ p.entryPointId AS entryPointId, hits, minStep, p.stepCount AS stepCount,
2291
+ ep.name AS epName, labels(ep)[0] AS epType, ep.filePath AS epFilePath
2292
+ `, { ids }).catch(() => []);
2293
+ for (const row of rows) {
2294
+ const pId = row.pId ?? row[0];
2295
+ const epId = row.entryPointId ?? row[3] ?? row.pId ?? row[0];
2296
+ // Track mapping from process -> entryPoint so we can backfill missing minStep
2297
+ if (pId)
2298
+ processToEntryPoint.set(String(pId), String(epId));
2299
+ // Normalize epName: prefer epName, fall back to other columns, and
2300
+ // ensure we don't keep an empty string (labels(...) can return "").
2301
+ const epNameRaw = row.epName ?? row[7] ?? row.name ?? row[1] ?? 'unknown';
2302
+ const epName = typeof epNameRaw === 'string' && epNameRaw.trim().length > 0
2303
+ ? epNameRaw.trim()
2304
+ : 'unknown';
2305
+ // Normalize epType: labels(ep)[0] can return an empty string in
2306
+ // some DBs (LadybugDB). Using nullish coalescing (??) preserves
2307
+ // empty strings, which results in empty `type` values being
2308
+ // propagated. Treat empty-string labels as missing and fall back
2309
+ // to the next candidate or a sensible default.
2310
+ const epTypeRaw = row.epType ?? row[8] ?? '';
2311
+ const epType = typeof epTypeRaw === 'string' && epTypeRaw.trim().length > 0
2312
+ ? epTypeRaw.trim()
2313
+ : 'Function';
2314
+ const epFilePath = row.epFilePath ?? row[9] ?? '';
2315
+ const hits = row.hits ?? row[4] ?? 0;
2316
+ const minStep = row.minStep ?? row[5];
2317
+ // If the DB returned null for minStep, note the process id so we
2318
+ // can run a follow-up query using a different aggregation strategy.
2319
+ if (minStep === null || minStep === undefined) {
2320
+ if (pId)
2321
+ processesMissingMinStep.add(String(pId));
2322
+ }
2323
+ if (!entryPointMap.has(epId)) {
2324
+ entryPointMap.set(epId, {
2325
+ name: epName,
2326
+ type: epType,
2327
+ filePath: epFilePath,
2328
+ affected_process_count: 0,
2329
+ total_hits: 0,
2330
+ earliest_broken_step: Infinity,
2331
+ });
2332
+ }
2333
+ const ep = entryPointMap.get(epId);
2334
+ ep.affected_process_count += 1;
2335
+ ep.total_hits += hits;
2336
+ ep.earliest_broken_step = Math.min(ep.earliest_broken_step, minStep ?? Infinity);
2337
+ }
2338
+ }
2339
+ catch (e) {
2340
+ logQueryError('impact:process-chunk', e);
2341
+ }
2342
+ }
2343
+ // If some processes returned null minStep, try a batched follow-up query
2344
+ // using the full impacted id set. This handles older indexes or DBs
2345
+ // where MIN(r.step) can come back null even when step properties exist.
2346
+ if (processesMissingMinStep.size > 0) {
2347
+ try {
2348
+ const pIds = Array.from(processesMissingMinStep);
2349
+ const allImpactedIds = impacted.map((it) => String(it.id ?? ''));
2350
+ const missingRows = await executeParameterized(repo.id, `
2351
+ MATCH (s)-[r:CodeRelation {type: 'STEP_IN_PROCESS'}]->(p:Process)
2352
+ WHERE p.id IN $pIds AND s.id IN $ids
2353
+ RETURN p.id AS pid, MIN(r.step) AS minStep
2354
+ `, { pIds, ids: allImpactedIds }).catch(() => []);
2355
+ for (const mr of missingRows) {
2356
+ const pid = mr.pid ?? mr[0];
2357
+ const minStep = mr.minStep ?? mr[1];
2358
+ const epId = processToEntryPoint.get(String(pid));
2359
+ if (!epId)
2360
+ continue;
2361
+ const ep = entryPointMap.get(epId);
2362
+ if (!ep)
2363
+ continue;
2364
+ if (typeof minStep === 'number') {
2365
+ ep.earliest_broken_step = Math.min(ep.earliest_broken_step, minStep);
2366
+ }
2367
+ }
2368
+ }
2369
+ catch (e) {
2370
+ logQueryError('impact:process-chunk-backfill', e);
2371
+ }
2372
+ }
2373
+ // If we capped chunks, mark traversal incomplete so caller knows results are partial
2374
+ if (chunksProcessed * CHUNK_SIZE < impacted.length) {
2375
+ traversalComplete = false;
2376
+ }
2377
+ affectedProcesses = Array.from(entryPointMap.values())
2378
+ .map((ep) => ({
2379
+ ...ep,
2380
+ earliest_broken_step: ep.earliest_broken_step === Infinity ? null : ep.earliest_broken_step,
2381
+ }))
2382
+ .sort((a, b) => b.total_hits - a.total_hits);
2383
+ // ── Module enrichment: use same cap as process enrichment and parameterized queries
2384
+ const maxItems = Math.min(impacted.length, MAX_CHUNKS * CHUNK_SIZE);
2385
+ const cappedImpacted = impacted.slice(0, maxItems);
2386
+ const allIdsArr = cappedImpacted.map((i) => String(i.id ?? ''));
2387
+ const d1Items = (grouped[1] || []).slice(0, maxItems);
2388
+ const d1IdsArr = d1Items.map((i) => String(i.id ?? ''));
2389
+ // Chunked module enrichment: run the MEMBER_OF queries in chunks
2390
+ // to avoid large single queries or concurrent Kuzu calls that can
2391
+ // crash (SIGSEGV) on arm64 macOS; behavior preserves existing maxItems cap and returns equivalent aggregated results.
2392
+ const moduleHitsMap = new Map();
2393
+ const directModuleSet = new Set();
2394
+ // Helper to run a single module chunk and accumulate hits by name
2395
+ const runModuleChunk = async (idsChunk) => {
2396
+ if (!idsChunk || idsChunk.length === 0)
2397
+ return;
2398
+ try {
2399
+ const rows = await executeParameterized(repo.id, `
2400
+ MATCH (s)-[:CodeRelation {type: 'MEMBER_OF'}]->(c:Community)
2401
+ WHERE s.id IN $ids
2402
+ RETURN c.heuristicLabel AS name, COUNT(DISTINCT s.id) AS hits
2403
+ ORDER BY hits DESC
2404
+ LIMIT 20
2405
+ `, { ids: idsChunk }).catch(() => []);
2406
+ for (const r of rows) {
2407
+ const name = r.name ?? r[0] ?? null;
2408
+ const hits = (r.hits ?? r[1]) || 0;
2409
+ if (!name)
2410
+ continue;
2411
+ moduleHitsMap.set(name, (moduleHitsMap.get(name) || 0) + hits);
2412
+ }
2413
+ }
2414
+ catch (e) {
2415
+ logQueryError('impact:module-chunk', e);
2416
+ }
2417
+ };
2418
+ // Run module query chunks sequentially (safe on arm64 macOS)
2419
+ for (let i = 0; i < allIdsArr.length; i += CHUNK_SIZE) {
2420
+ const chunkIds = allIdsArr.slice(i, i + CHUNK_SIZE);
2421
+ await runModuleChunk(chunkIds);
2422
+ }
2423
+ // Run direct module query similarly (distinct heuristic labels for depth-1 items)
2424
+ const runDirectModuleChunk = async (idsChunk) => {
2425
+ if (!idsChunk || idsChunk.length === 0)
2426
+ return;
2427
+ try {
2428
+ const rows = await executeParameterized(repo.id, `
2429
+ MATCH (s)-[:CodeRelation {type: 'MEMBER_OF'}]->(c:Community)
2430
+ WHERE s.id IN $ids
2431
+ RETURN DISTINCT c.heuristicLabel AS name
2432
+ `, { ids: idsChunk }).catch(() => []);
2433
+ for (const r of rows) {
2434
+ const name = r.name ?? r[0] ?? null;
2435
+ if (name)
2436
+ directModuleSet.add(name);
2437
+ }
2438
+ }
2439
+ catch (e) {
2440
+ logQueryError('impact:direct-module-chunk', e);
2441
+ }
2442
+ };
2443
+ for (let i = 0; i < d1IdsArr.length; i += CHUNK_SIZE) {
2444
+ const chunkIds = d1IdsArr.slice(i, i + CHUNK_SIZE);
2445
+ await runDirectModuleChunk(chunkIds);
2446
+ }
2447
+ // Build final moduleRows array from aggregated hits map, sorted & limited
2448
+ const moduleRows = Array.from(moduleHitsMap.entries())
2449
+ .map(([name, hits]) => ({ name, hits }))
2450
+ .sort((a, b) => b.hits - a.hits)
2451
+ .slice(0, 20);
2452
+ const directModuleRows = Array.from(directModuleSet).map((name) => ({ name }));
2453
+ // Build affectedModules in the same shape as original implementation
2454
+ const directModuleNameSet = new Set(directModuleRows.map((r) => r.name || r[0]));
2455
+ affectedModules = moduleRows.map((r) => {
2456
+ const name = r.name ?? r[0];
2457
+ const hits = r.hits ?? r[1] ?? 0;
2458
+ return {
2459
+ name,
2460
+ hits,
2461
+ impact: directModuleNameSet.has(name) ? 'direct' : 'indirect',
2462
+ };
2463
+ });
2464
+ }
2465
+ // Risk scoring
2466
+ const processCount = affectedProcesses.length;
2467
+ const moduleCount = affectedModules.length;
2468
+ let risk = 'LOW';
2469
+ if (directCount >= 30 || processCount >= 5 || moduleCount >= 5 || impacted.length >= 200) {
2470
+ risk = 'CRITICAL';
2471
+ }
2472
+ else if (directCount >= 15 ||
2473
+ processCount >= 3 ||
2474
+ moduleCount >= 3 ||
2475
+ impacted.length >= 100) {
2476
+ risk = 'HIGH';
2477
+ }
2478
+ else if (directCount >= 5 || impacted.length >= 30) {
2479
+ risk = 'MEDIUM';
2480
+ }
2481
+ return {
2482
+ target: {
2483
+ id: symId,
2484
+ name: sym.name || sym[1],
2485
+ type: symType,
2486
+ filePath: sym.filePath || sym[2],
2487
+ },
2488
+ direction,
2489
+ impactedCount: impacted.length,
2490
+ risk,
2491
+ ...(!traversalComplete && { partial: true }),
2492
+ summary: {
2493
+ direct: directCount,
2494
+ processes_affected: processCount,
2495
+ modules_affected: moduleCount,
2496
+ },
2497
+ affected_processes: affectedProcesses,
2498
+ affected_modules: affectedModules,
2499
+ byDepth: grouped,
2500
+ };
2501
+ }
2502
+ /**
2503
+ * UID-based impact for cross-repo fan-out. Same result shape as `impact`.
2504
+ * Returns null if the repo is unknown, the UID is missing, or analysis fails.
2505
+ */
2506
+ async impactByUid(repoId, uid, direction, opts) {
2507
+ try {
2508
+ await this.refreshRepos();
2509
+ await this.ensureInitialized(repoId);
2510
+ }
2511
+ catch {
2512
+ return null;
2513
+ }
2514
+ const repo = this.repos.get(repoId);
2515
+ if (!repo)
2516
+ return null;
2517
+ const dir = direction === 'downstream' ? 'downstream' : 'upstream';
2518
+ let rows;
2519
+ try {
2520
+ rows = await executeParameterized(repoId, `MATCH (n) WHERE n.id = $uid
2521
+ RETURN n.id AS id, n.name AS name, n.filePath AS filePath, labels(n)[0] AS type
2522
+ LIMIT 1`, { uid });
2523
+ }
2524
+ catch {
2525
+ return null;
2526
+ }
2527
+ if (!rows?.length)
2528
+ return null;
2529
+ const sym = rows[0];
2530
+ const labelRaw = sym.type ?? sym[3];
2531
+ const symType = typeof labelRaw === 'string' && labelRaw.trim().length > 0 ? labelRaw.trim() : '';
2532
+ // Map legacy relation type names (backward compat for OVERRIDES → METHOD_OVERRIDES)
2533
+ const mappedRelTypes = opts.relationTypes?.flatMap((t) => t === 'OVERRIDES' ? ['OVERRIDES', 'METHOD_OVERRIDES'] : [t]);
2534
+ const rawRelTypes = mappedRelTypes && mappedRelTypes.length > 0
2535
+ ? mappedRelTypes.filter((t) => VALID_RELATION_TYPES.has(t))
2536
+ : [
2537
+ 'CALLS',
2538
+ 'IMPORTS',
2539
+ 'EXTENDS',
2540
+ 'IMPLEMENTS',
2541
+ 'METHOD_OVERRIDES',
2542
+ 'OVERRIDES',
2543
+ 'METHOD_IMPLEMENTS',
2544
+ ];
2545
+ const relationTypes = rawRelTypes.length > 0
2546
+ ? rawRelTypes
2547
+ : [
2548
+ 'CALLS',
2549
+ 'IMPORTS',
2550
+ 'EXTENDS',
2551
+ 'IMPLEMENTS',
2552
+ 'METHOD_OVERRIDES',
2553
+ 'OVERRIDES',
2554
+ 'METHOD_IMPLEMENTS',
2555
+ ];
2556
+ try {
2557
+ return await this._runImpactBFS(repo, sym, symType, dir, {
2558
+ maxDepth: opts.maxDepth,
2559
+ relationTypes,
2560
+ includeTests: opts.includeTests,
2561
+ minConfidence: opts.minConfidence,
2562
+ });
2563
+ }
2564
+ catch {
2565
+ return null;
2566
+ }
2567
+ }
2568
+ handleGroupTool(method, params) {
2569
+ switch (method) {
2570
+ case 'group_list':
2571
+ return this.groupList(params);
2572
+ case 'group_sync':
2573
+ return this.groupSync(params);
2574
+ default:
2575
+ throw new Error(`Unknown group tool: ${method}. Removed tools: use repo "@<groupName>" on impact, query, or context (optional "/<memberPath>"), or MCP resources.`);
2576
+ }
2577
+ }
2578
+ /**
2579
+ * Dispatch impact/query/context when `repo` is `@groupName` or `@groupName/memberPath`
2580
+ * (group mode — not the global indexed-repo `repo` parameter).
2581
+ */
2582
+ async callToolAtGroupRepo(method, params) {
2583
+ await this.refreshRepos();
2584
+ if (params.service !== undefined &&
2585
+ params.service !== null &&
2586
+ String(params.service).trim() === '') {
2587
+ return { error: 'service must not be an empty string' };
2588
+ }
2589
+ const raw = String(params.repo).slice(1);
2590
+ const slash = raw.indexOf('/');
2591
+ const groupName = (slash === -1 ? raw : raw.slice(0, slash)).trim();
2592
+ const memberRest = slash === -1 ? undefined : raw.slice(slash + 1).trim() || undefined;
2593
+ const resolved = await resolveAtGroupMemberRepoPath(groupName, memberRest);
2594
+ if (resolved.ok === false)
2595
+ return { error: resolved.error };
2596
+ const svc = this.getGroupService();
2597
+ if (method === 'impact') {
2598
+ const impactArgs = {
2599
+ name: groupName,
2600
+ repo: resolved.repoPath,
2601
+ target: params.target,
2602
+ direction: params.direction,
2603
+ };
2604
+ if (params.maxDepth !== undefined)
2605
+ impactArgs.maxDepth = params.maxDepth;
2606
+ if (params.crossDepth !== undefined)
2607
+ impactArgs.crossDepth = params.crossDepth;
2608
+ if (params.relationTypes !== undefined)
2609
+ impactArgs.relationTypes = params.relationTypes;
2610
+ if (params.includeTests !== undefined)
2611
+ impactArgs.includeTests = params.includeTests;
2612
+ if (params.minConfidence !== undefined)
2613
+ impactArgs.minConfidence = params.minConfidence;
2614
+ if (params.service !== undefined && params.service !== null)
2615
+ impactArgs.service = params.service;
2616
+ if (typeof params.subgroup === 'string')
2617
+ impactArgs.subgroup = params.subgroup;
2618
+ if (params.timeoutMs !== undefined)
2619
+ impactArgs.timeoutMs = params.timeoutMs;
2620
+ if (params.timeout !== undefined)
2621
+ impactArgs.timeout = params.timeout;
2622
+ return svc.groupImpact(impactArgs);
2623
+ }
2624
+ if (method === 'query') {
2625
+ const queryArgs = {
2626
+ name: groupName,
2627
+ query: params.query,
2628
+ };
2629
+ if (typeof params.task_context === 'string')
2630
+ queryArgs.task_context = params.task_context;
2631
+ if (typeof params.goal === 'string')
2632
+ queryArgs.goal = params.goal;
2633
+ if (typeof params.limit === 'number')
2634
+ queryArgs.limit = params.limit;
2635
+ if (typeof params.max_symbols === 'number')
2636
+ queryArgs.max_symbols = params.max_symbols;
2637
+ if (params.include_content !== undefined)
2638
+ queryArgs.include_content = params.include_content;
2639
+ if (params.service !== undefined && params.service !== null)
2640
+ queryArgs.service = params.service;
2641
+ if (memberRest !== undefined) {
2642
+ queryArgs.subgroup = memberRest;
2643
+ queryArgs.subgroupExact = true;
2644
+ }
2645
+ return svc.groupQuery(queryArgs);
2646
+ }
2647
+ if (method === 'context') {
2648
+ const targetSym = typeof params.target === 'string' && params.target.trim() !== ''
2649
+ ? params.target.trim()
2650
+ : typeof params.name === 'string' && params.name.trim() !== ''
2651
+ ? params.name.trim()
2652
+ : undefined;
2653
+ const contextArgs = {
2654
+ name: groupName,
2655
+ target: targetSym,
2656
+ };
2657
+ if (typeof params.uid === 'string')
2658
+ contextArgs.uid = params.uid;
2659
+ if (typeof params.file_path === 'string')
2660
+ contextArgs.file_path = params.file_path;
2661
+ if (params.include_content !== undefined)
2662
+ contextArgs.include_content = params.include_content;
2663
+ if (params.service !== undefined && params.service !== null)
2664
+ contextArgs.service = params.service;
2665
+ if (memberRest !== undefined) {
2666
+ contextArgs.subgroup = memberRest;
2667
+ contextArgs.subgroupExact = true;
2668
+ }
2669
+ return svc.groupContext(contextArgs);
2670
+ }
2671
+ throw new Error(`Internal: unsupported group-repo tool ${method}`);
2672
+ }
2673
+ async groupList(params) {
2674
+ return this.getGroupService().groupList(params);
2675
+ }
2676
+ async groupSync(params) {
2677
+ return this.getGroupService().groupSync(params);
2678
+ }
2679
+ /**
2680
+ * MCP resource body for `codragraph://group/{name}/contracts` (Issue #794).
2681
+ */
2682
+ async readGroupContractsResource(groupName, filter) {
2683
+ try {
2684
+ const params = { name: groupName };
2685
+ if (filter.type !== undefined)
2686
+ params.type = filter.type;
2687
+ if (filter.repo !== undefined)
2688
+ params.repo = filter.repo;
2689
+ if (filter.unmatchedOnly === true)
2690
+ params.unmatchedOnly = true;
2691
+ const raw = await this.getGroupService().groupContracts(params);
2692
+ return LocalBackend.formatGroupResourcePayload(raw);
2693
+ }
2694
+ catch (e) {
2695
+ return `error: ${e instanceof Error ? e.message : String(e)}`;
2696
+ }
2697
+ }
2698
+ /**
2699
+ * MCP resource body for `codragraph://group/{name}/status` (Issue #794).
2700
+ */
2701
+ async readGroupStatusResource(groupName) {
2702
+ try {
2703
+ const raw = await this.getGroupService().groupStatus({ name: groupName });
2704
+ return LocalBackend.formatGroupResourcePayload(raw);
2705
+ }
2706
+ catch (e) {
2707
+ return `error: ${e instanceof Error ? e.message : String(e)}`;
2708
+ }
2709
+ }
2710
+ static formatGroupResourcePayload(raw) {
2711
+ if (raw && typeof raw === 'object' && 'error' in raw) {
2712
+ const err = raw.error;
2713
+ if (typeof err === 'string' && err.length > 0) {
2714
+ return `error: ${err}`;
2715
+ }
2716
+ }
2717
+ return JSON.stringify(raw, null, 2);
2718
+ }
2719
+ /**
2720
+ * Fetch Route nodes with their consumers in a single query.
2721
+ * Shared by routeMap and shapeCheck to avoid N+1 query patterns.
2722
+ */
2723
+ async fetchRoutesWithConsumers(repoId, routeFilter, params) {
2724
+ const rows = await executeParameterized(repoId, `
2725
+ MATCH (n:Route)
2726
+ WHERE n.id STARTS WITH 'Route:' ${routeFilter}
2727
+ OPTIONAL MATCH (consumer)-[r:CodeRelation]->(n)
2728
+ WHERE r.type = 'FETCHES'
2729
+ RETURN n.id AS routeId, n.name AS routeName, n.filePath AS handlerFile,
2730
+ n.responseKeys AS responseKeys, n.errorKeys AS errorKeys, n.middleware AS middleware,
2731
+ consumer.name AS consumerName, consumer.filePath AS consumerFile,
2732
+ r.reason AS fetchReason
2733
+ `, params);
2734
+ // Strip wrapping quotes from DB array elements — CSV COPY stores ['key'] which
2735
+ // LadybugDB may return as "'key'" rather than "key"
2736
+ const stripQuotes = (keys) => keys ? keys.map((k) => k.replace(/^['"]|['"]$/g, '')) : null;
2737
+ const routeMap = new Map();
2738
+ for (const row of rows) {
2739
+ const id = row.routeId ?? row[0];
2740
+ const name = row.routeName ?? row[1];
2741
+ const filePath = row.handlerFile ?? row[2];
2742
+ const responseKeys = stripQuotes(row.responseKeys ?? row[3] ?? null);
2743
+ const errorKeys = stripQuotes(row.errorKeys ?? row[4] ?? null);
2744
+ const middleware = stripQuotes(row.middleware ?? row[5] ?? null);
2745
+ const consumerName = row.consumerName ?? row[6];
2746
+ const consumerFile = row.consumerFile ?? row[7];
2747
+ const fetchReason = row.fetchReason ?? row[8] ?? null;
2748
+ if (!routeMap.has(id)) {
2749
+ routeMap.set(id, {
2750
+ id,
2751
+ name,
2752
+ filePath,
2753
+ responseKeys,
2754
+ errorKeys,
2755
+ middleware,
2756
+ consumers: [],
2757
+ });
2758
+ }
2759
+ if (consumerName && consumerFile) {
2760
+ // Parse accessed keys from reason field: "fetch-url-match|keys:data,pagination|fetches:3"
2761
+ let accessedKeys;
2762
+ let fetchCount;
2763
+ if (fetchReason) {
2764
+ const keysMatch = fetchReason.match(/\|keys:([^|]+)/);
2765
+ if (keysMatch) {
2766
+ accessedKeys = keysMatch[1].split(',').filter((k) => k.length > 0);
2767
+ }
2768
+ const fetchesMatch = fetchReason.match(/\|fetches:(\d+)/);
2769
+ if (fetchesMatch) {
2770
+ fetchCount = parseInt(fetchesMatch[1], 10);
2771
+ }
2772
+ }
2773
+ routeMap.get(id).consumers.push({
2774
+ name: consumerName,
2775
+ filePath: consumerFile,
2776
+ ...(accessedKeys ? { accessedKeys } : {}),
2777
+ ...(fetchCount && fetchCount > 1 ? { fetchCount } : {}),
2778
+ });
2779
+ }
2780
+ }
2781
+ return [...routeMap.values()];
2782
+ }
2783
+ /**
2784
+ * Batch-fetch execution flows linked to a set of Route or Tool nodes.
2785
+ * Single query instead of N+1.
2786
+ */
2787
+ async fetchLinkedFlowsBatch(repoId, nodeIds) {
2788
+ const result = new Map();
2789
+ if (nodeIds.length === 0)
2790
+ return result;
2791
+ try {
2792
+ // Use list_contains to filter at DB level instead of fetching all and filtering in memory
2793
+ const rows = await executeParameterized(repoId, `
2794
+ MATCH (source)-[r:CodeRelation]->(proc:Process)
2795
+ WHERE r.type = 'ENTRY_POINT_OF'
2796
+ AND list_contains($nodeIds, source.id)
2797
+ RETURN source.id AS sourceId, proc.label AS name
2798
+ `, { nodeIds });
2799
+ for (const row of rows) {
2800
+ const sourceId = row.sourceId ?? row[0];
2801
+ const name = row.name ?? row[1];
2802
+ if (!name)
2803
+ continue;
2804
+ let list = result.get(sourceId);
2805
+ if (!list) {
2806
+ list = [];
2807
+ result.set(sourceId, list);
2808
+ }
2809
+ list.push(name);
2810
+ }
2811
+ }
2812
+ catch {
2813
+ /* no ENTRY_POINT_OF edges yet */
2814
+ }
2815
+ return result;
2816
+ }
2817
+ async routeMap(repo, params) {
2818
+ await this.ensureInitialized(repo.id);
2819
+ const routeFilter = params.route ? `AND n.name CONTAINS $route` : '';
2820
+ const queryParams = params.route ? { route: params.route } : {};
2821
+ const routes = await this.fetchRoutesWithConsumers(repo.id, routeFilter, queryParams);
2822
+ if (routes.length === 0) {
2823
+ return {
2824
+ routes: [],
2825
+ total: 0,
2826
+ message: params.route
2827
+ ? `No routes matching "${params.route}"`
2828
+ : 'No routes found in this project.',
2829
+ };
2830
+ }
2831
+ const flowMap = await this.fetchLinkedFlowsBatch(repo.id, routes.map((r) => r.id));
2832
+ return {
2833
+ routes: routes.map((r) => ({
2834
+ route: r.name,
2835
+ handler: r.filePath,
2836
+ middleware: r.middleware || [],
2837
+ consumers: r.consumers,
2838
+ flows: flowMap.get(r.id) || [],
2839
+ })),
2840
+ total: routes.length,
2841
+ };
2842
+ }
2843
+ async shapeCheck(repo, params) {
2844
+ await this.ensureInitialized(repo.id);
2845
+ const routeFilter = params.route ? `AND n.name CONTAINS $route` : '';
2846
+ const queryParams = params.route ? { route: params.route } : {};
2847
+ const allRoutes = await this.fetchRoutesWithConsumers(repo.id, routeFilter, queryParams);
2848
+ const results = allRoutes
2849
+ .filter((r) => ((r.responseKeys && r.responseKeys.length > 0) ||
2850
+ (r.errorKeys && r.errorKeys.length > 0)) &&
2851
+ r.consumers.length > 0)
2852
+ .map((r) => {
2853
+ // Keys already normalized by fetchRoutesWithConsumers (quotes stripped)
2854
+ const responseKeys = r.responseKeys ?? [];
2855
+ const errorKeys = r.errorKeys ?? [];
2856
+ // Combined set: consumer accessing either success or error keys is valid
2857
+ const allKnownKeys = new Set([...responseKeys, ...errorKeys]);
2858
+ // Check each consumer's accessed keys against the route's response shape
2859
+ const responseKeySet = new Set(responseKeys);
2860
+ const consumers = r.consumers.map((c) => {
2861
+ if (!c.accessedKeys || c.accessedKeys.length === 0) {
2862
+ return { name: c.name, filePath: c.filePath };
2863
+ }
2864
+ const mismatched = c.accessedKeys.filter((k) => !allKnownKeys.has(k));
2865
+ // Keys in allKnownKeys but not in responseKeys — error-path access (e.g., .error from errorKeys)
2866
+ const errorPathKeys = c.accessedKeys.filter((k) => allKnownKeys.has(k) && !responseKeySet.has(k));
2867
+ const isMultiFetch = (c.fetchCount ?? 1) > 1;
2868
+ return {
2869
+ name: c.name,
2870
+ filePath: c.filePath,
2871
+ accessedKeys: c.accessedKeys,
2872
+ ...(mismatched.length > 0
2873
+ ? {
2874
+ mismatched,
2875
+ mismatchConfidence: isMultiFetch ? 'low' : 'high',
2876
+ }
2877
+ : {}),
2878
+ ...(errorPathKeys.length > 0 ? { errorPathKeys } : {}),
2879
+ ...(isMultiFetch
2880
+ ? {
2881
+ attributionNote: `This file fetches ${c.fetchCount} routes — accessed keys may belong to a different route.`,
2882
+ }
2883
+ : {}),
2884
+ };
2885
+ });
2886
+ const hasMismatches = consumers.some((c) => 'mismatched' in c && c.mismatched.length > 0);
2887
+ return {
2888
+ route: r.name,
2889
+ handler: r.filePath,
2890
+ ...(responseKeys.length > 0 ? { responseKeys } : {}),
2891
+ ...(errorKeys.length > 0 ? { errorKeys } : {}),
2892
+ consumers,
2893
+ ...(hasMismatches ? { status: 'MISMATCH' } : {}),
2894
+ };
2895
+ });
2896
+ const mismatchCount = results.filter((r) => r.status === 'MISMATCH').length;
2897
+ return {
2898
+ routes: results,
2899
+ total: results.length,
2900
+ routesWithShapes: results.length,
2901
+ ...(mismatchCount > 0 ? { mismatches: mismatchCount } : {}),
2902
+ message: results.length === 0
2903
+ ? 'No routes with both response shapes and consumers found.'
2904
+ : mismatchCount > 0
2905
+ ? `Found ${results.length} route(s) with response shape data. ${mismatchCount} route(s) have consumer/shape mismatches.`
2906
+ : `Found ${results.length} route(s) with response shape data and consumers.`,
2907
+ };
2908
+ }
2909
+ async toolMap(repo, params) {
2910
+ await this.ensureInitialized(repo.id);
2911
+ const toolFilter = params.tool ? `AND n.name CONTAINS $tool` : '';
2912
+ const queryParams = params.tool ? { tool: params.tool } : {};
2913
+ const rows = await executeParameterized(repo.id, `
2914
+ MATCH (n:Tool)
2915
+ WHERE n.id STARTS WITH 'Tool:' ${toolFilter}
2916
+ RETURN n.id AS id, n.name AS name, n.filePath AS filePath, n.description AS description
2917
+ `, queryParams);
2918
+ if (rows.length === 0) {
2919
+ return {
2920
+ tools: [],
2921
+ total: 0,
2922
+ message: params.tool ? `No tools matching "${params.tool}"` : 'No tool definitions found.',
2923
+ };
2924
+ }
2925
+ const toolIds = rows.map((r) => r.id ?? r[0]);
2926
+ const flowMap = await this.fetchLinkedFlowsBatch(repo.id, toolIds);
2927
+ return {
2928
+ tools: rows.map((r) => {
2929
+ const id = r.id ?? r[0];
2930
+ return {
2931
+ name: r.name ?? r[1],
2932
+ filePath: r.filePath ?? r[2],
2933
+ description: (r.description ?? r[3] ?? '').slice(0, 200),
2934
+ flows: flowMap.get(id) || [],
2935
+ };
2936
+ }),
2937
+ total: rows.length,
2938
+ };
2939
+ }
2940
+ async apiImpact(repo, params) {
2941
+ await this.ensureInitialized(repo.id);
2942
+ if (!params.route && !params.file) {
2943
+ return { error: 'Either "route" or "file" parameter is required.' };
2944
+ }
2945
+ // If file is provided but route is not, look up the route by file path
2946
+ let routeFilter = '';
2947
+ const queryParams = {};
2948
+ if (params.route) {
2949
+ routeFilter = `AND n.name CONTAINS $route`;
2950
+ queryParams.route = params.route;
2951
+ }
2952
+ else if (params.file) {
2953
+ routeFilter = `AND n.filePath CONTAINS $file`;
2954
+ queryParams.file = params.file;
2955
+ }
2956
+ const routes = await this.fetchRoutesWithConsumers(repo.id, routeFilter, queryParams);
2957
+ if (routes.length === 0) {
2958
+ const target = params.route || params.file;
2959
+ return { error: `No routes found matching "${target}".` };
2960
+ }
2961
+ const flowMap = await this.fetchLinkedFlowsBatch(repo.id, routes.map((r) => r.id));
2962
+ // Count how many routes share the same handler file (for middleware partial detection)
2963
+ const routeCountByHandler = new Map();
2964
+ for (const r of routes) {
2965
+ if (r.filePath) {
2966
+ routeCountByHandler.set(r.filePath, (routeCountByHandler.get(r.filePath) ?? 0) + 1);
2967
+ }
2968
+ }
2969
+ const results = routes.map((r) => {
2970
+ // Keys already normalized by fetchRoutesWithConsumers (quotes stripped)
2971
+ const responseKeys = r.responseKeys ?? [];
2972
+ const errorKeys = r.errorKeys ?? [];
2973
+ const allKnownKeys = new Set([...responseKeys, ...errorKeys]);
2974
+ // Build consumer list with mismatch detection
2975
+ const consumers = r.consumers.map((c) => ({
2976
+ name: c.name,
2977
+ file: c.filePath,
2978
+ accesses: c.accessedKeys ?? [],
2979
+ ...(c.fetchCount && c.fetchCount > 1
2980
+ ? {
2981
+ attributionNote: `This file fetches ${c.fetchCount} routes — accessed keys may belong to a different route.`,
2982
+ }
2983
+ : {}),
2984
+ }));
2985
+ // Detect mismatches: consumer accesses keys not in response shape
2986
+ const mismatches = [];
2987
+ if (allKnownKeys.size > 0) {
2988
+ for (const c of r.consumers) {
2989
+ if (!c.accessedKeys)
2990
+ continue;
2991
+ const isMultiFetch = (c.fetchCount ?? 1) > 1;
2992
+ for (const key of c.accessedKeys) {
2993
+ if (!allKnownKeys.has(key)) {
2994
+ mismatches.push({
2995
+ consumer: c.filePath,
2996
+ field: key,
2997
+ reason: 'accessed but not in response shape',
2998
+ confidence: isMultiFetch ? 'low' : 'high',
2999
+ });
3000
+ }
3001
+ }
3002
+ }
3003
+ }
3004
+ const flows = flowMap.get(r.id) || [];
3005
+ const consumerCount = r.consumers.length;
3006
+ // Risk level heuristic
3007
+ let riskLevel;
3008
+ if (consumerCount >= 10) {
3009
+ riskLevel = 'HIGH';
3010
+ }
3011
+ else if (consumerCount >= 4) {
3012
+ riskLevel = 'MEDIUM';
3013
+ }
3014
+ else {
3015
+ riskLevel = 'LOW';
3016
+ }
3017
+ // Bump up one level if mismatches exist
3018
+ if (mismatches.length > 0) {
3019
+ if (riskLevel === 'LOW')
3020
+ riskLevel = 'MEDIUM';
3021
+ else if (riskLevel === 'MEDIUM')
3022
+ riskLevel = 'HIGH';
3023
+ }
3024
+ const warning = consumerCount > 0
3025
+ ? `Changing response shape will affect ${consumerCount} component${consumerCount === 1 ? '' : 's'}`
3026
+ : undefined;
3027
+ // Flag when middleware was detected but handler exports multiple HTTP methods
3028
+ // (middleware chain may only reflect one export)
3029
+ const middlewareArr = r.middleware || [];
3030
+ const handlerRouteCount = r.filePath ? (routeCountByHandler.get(r.filePath) ?? 1) : 1;
3031
+ const middlewarePartial = middlewareArr.length > 0 && handlerRouteCount > 1;
3032
+ return {
3033
+ route: r.name,
3034
+ handler: r.filePath,
3035
+ responseShape: {
3036
+ success: responseKeys,
3037
+ error: errorKeys,
3038
+ },
3039
+ middleware: middlewareArr,
3040
+ ...(middlewarePartial
3041
+ ? {
3042
+ middlewareDetection: 'partial',
3043
+ middlewareNote: 'Middleware captured from first HTTP method export only — other methods in this handler may use different middleware chains.',
3044
+ }
3045
+ : {}),
3046
+ consumers,
3047
+ ...(mismatches.length > 0 ? { mismatches } : {}),
3048
+ executionFlows: flows,
3049
+ impactSummary: {
3050
+ directConsumers: consumerCount,
3051
+ affectedFlows: flows.length,
3052
+ riskLevel,
3053
+ ...(warning ? { warning } : {}),
3054
+ },
3055
+ };
3056
+ });
3057
+ // If a single route was targeted, return it directly (not wrapped in array)
3058
+ if (results.length === 1) {
3059
+ return results[0];
3060
+ }
3061
+ return { routes: results, total: results.length };
3062
+ }
3063
+ // ─── Direct Graph Queries (for resources.ts) ────────────────────
3064
+ /**
3065
+ * Query clusters (communities) directly from graph.
3066
+ * Used by getClustersResource — avoids legacy overview() dispatch.
3067
+ */
3068
+ async queryClusters(repoName, limit = 100) {
3069
+ const repo = await this.resolveRepo(repoName);
3070
+ await this.ensureInitialized(repo.id);
3071
+ try {
3072
+ const rawLimit = Math.max(limit * 5, 200);
3073
+ const clusters = await executeQuery(repo.id, `
3074
+ MATCH (c:Community)
3075
+ RETURN c.id AS id, c.label AS label, c.heuristicLabel AS heuristicLabel, c.cohesion AS cohesion, c.symbolCount AS symbolCount
3076
+ ORDER BY c.symbolCount DESC
3077
+ LIMIT ${rawLimit}
3078
+ `);
3079
+ const rawClusters = clusters.map((c) => ({
3080
+ id: c.id || c[0],
3081
+ label: c.label || c[1],
3082
+ heuristicLabel: c.heuristicLabel || c[2],
3083
+ cohesion: c.cohesion || c[3],
3084
+ symbolCount: c.symbolCount || c[4],
3085
+ }));
3086
+ return { clusters: this.aggregateClusters(rawClusters).slice(0, limit) };
3087
+ }
3088
+ catch {
3089
+ return { clusters: [] };
3090
+ }
3091
+ }
3092
+ /**
3093
+ * Query processes directly from graph.
3094
+ * Used by getProcessesResource — avoids legacy overview() dispatch.
3095
+ */
3096
+ async queryProcesses(repoName, limit = 50) {
3097
+ const repo = await this.resolveRepo(repoName);
3098
+ await this.ensureInitialized(repo.id);
3099
+ try {
3100
+ const processes = await executeQuery(repo.id, `
3101
+ MATCH (p:Process)
3102
+ RETURN p.id AS id, p.label AS label, p.heuristicLabel AS heuristicLabel, p.processType AS processType, p.stepCount AS stepCount
3103
+ ORDER BY p.stepCount DESC
3104
+ LIMIT ${limit}
3105
+ `);
3106
+ return {
3107
+ processes: processes.map((p) => ({
3108
+ id: p.id || p[0],
3109
+ label: p.label || p[1],
3110
+ heuristicLabel: p.heuristicLabel || p[2],
3111
+ processType: p.processType || p[3],
3112
+ stepCount: p.stepCount || p[4],
3113
+ })),
3114
+ };
3115
+ }
3116
+ catch {
3117
+ return { processes: [] };
3118
+ }
3119
+ }
3120
+ /**
3121
+ * Query cluster detail (members) directly from graph.
3122
+ * Used by getClusterDetailResource.
3123
+ */
3124
+ async queryClusterDetail(name, repoName) {
3125
+ const repo = await this.resolveRepo(repoName);
3126
+ await this.ensureInitialized(repo.id);
3127
+ const clusters = await executeParameterized(repo.id, `
3128
+ MATCH (c:Community)
3129
+ WHERE c.label = $clusterName OR c.heuristicLabel = $clusterName
3130
+ RETURN c.id AS id, c.label AS label, c.heuristicLabel AS heuristicLabel, c.cohesion AS cohesion, c.symbolCount AS symbolCount
3131
+ `, { clusterName: name });
3132
+ if (clusters.length === 0)
3133
+ return { error: `Cluster '${name}' not found` };
3134
+ const rawClusters = clusters.map((c) => ({
3135
+ id: c.id || c[0],
3136
+ label: c.label || c[1],
3137
+ heuristicLabel: c.heuristicLabel || c[2],
3138
+ cohesion: c.cohesion || c[3],
3139
+ symbolCount: c.symbolCount || c[4],
3140
+ }));
3141
+ let totalSymbols = 0, weightedCohesion = 0;
3142
+ for (const c of rawClusters) {
3143
+ const s = c.symbolCount || 0;
3144
+ totalSymbols += s;
3145
+ weightedCohesion += (c.cohesion || 0) * s;
3146
+ }
3147
+ const members = await executeParameterized(repo.id, `
3148
+ MATCH (n)-[:CodeRelation {type: 'MEMBER_OF'}]->(c:Community)
3149
+ WHERE c.label = $clusterName OR c.heuristicLabel = $clusterName
3150
+ RETURN DISTINCT n.name AS name, labels(n)[0] AS type, n.filePath AS filePath
3151
+ LIMIT 30
3152
+ `, { clusterName: name });
3153
+ return {
3154
+ cluster: {
3155
+ id: rawClusters[0].id,
3156
+ label: rawClusters[0].heuristicLabel || rawClusters[0].label,
3157
+ heuristicLabel: rawClusters[0].heuristicLabel || rawClusters[0].label,
3158
+ cohesion: totalSymbols > 0 ? weightedCohesion / totalSymbols : 0,
3159
+ symbolCount: totalSymbols,
3160
+ subCommunities: rawClusters.length,
3161
+ },
3162
+ members: members.map((m) => ({
3163
+ name: m.name || m[0],
3164
+ type: m.type || m[1],
3165
+ filePath: m.filePath || m[2],
3166
+ })),
3167
+ };
3168
+ }
3169
+ /**
3170
+ * Query process detail (steps) directly from graph.
3171
+ * Used by getProcessDetailResource.
3172
+ */
3173
+ async queryProcessDetail(name, repoName) {
3174
+ const repo = await this.resolveRepo(repoName);
3175
+ await this.ensureInitialized(repo.id);
3176
+ const processes = await executeParameterized(repo.id, `
3177
+ MATCH (p:Process)
3178
+ WHERE p.label = $processName OR p.heuristicLabel = $processName
3179
+ RETURN p.id AS id, p.label AS label, p.heuristicLabel AS heuristicLabel, p.processType AS processType, p.stepCount AS stepCount
3180
+ LIMIT 1
3181
+ `, { processName: name });
3182
+ if (processes.length === 0)
3183
+ return { error: `Process '${name}' not found` };
3184
+ const proc = processes[0];
3185
+ const procId = proc.id || proc[0];
3186
+ const steps = await executeParameterized(repo.id, `
3187
+ MATCH (n)-[r:CodeRelation {type: 'STEP_IN_PROCESS'}]->(p {id: $procId})
3188
+ RETURN n.name AS name, labels(n)[0] AS type, n.filePath AS filePath, r.step AS step
3189
+ ORDER BY r.step
3190
+ `, { procId });
3191
+ return {
3192
+ process: {
3193
+ id: procId,
3194
+ label: proc.label || proc[1],
3195
+ heuristicLabel: proc.heuristicLabel || proc[2],
3196
+ processType: proc.processType || proc[3],
3197
+ stepCount: proc.stepCount || proc[4],
3198
+ },
3199
+ steps: steps.map((s) => ({
3200
+ step: s.step || s[3],
3201
+ name: s.name || s[0],
3202
+ type: s.type || s[1],
3203
+ filePath: s.filePath || s[2],
3204
+ })),
3205
+ };
3206
+ }
3207
+ async disconnect() {
3208
+ await closeLbug(); // close all connections
3209
+ // Note: we intentionally do NOT call disposeEmbedder() here.
3210
+ // ONNX Runtime's native cleanup segfaults on macOS and some Linux configs,
3211
+ // and importing the embedder module on Node v24+ crashes if onnxruntime
3212
+ // was never loaded during the session. Since process.exit(0) follows
3213
+ // immediately after disconnect(), the OS reclaims everything. See #38, #89.
3214
+ this.repos.clear();
3215
+ this.contextCache.clear();
3216
+ this.initializedRepos.clear();
3217
+ }
3218
+ }