cgraphx 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (936) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +243 -0
  3. package/dist/.claude-template/commands/my-commit.md +9 -0
  4. package/dist/.claude-template/commands/my-query.md +4 -0
  5. package/dist/.claude-template/hooks/context-monitor/context-monitor.cjs +216 -0
  6. package/dist/.claude-template/plugins/claude-hud/dist/claude-config-dir.d.ts +4 -0
  7. package/dist/.claude-template/plugins/claude-hud/dist/claude-config-dir.d.ts.map +1 -0
  8. package/dist/.claude-template/plugins/claude-hud/dist/claude-config-dir.js +24 -0
  9. package/dist/.claude-template/plugins/claude-hud/dist/claude-config-dir.js.map +1 -0
  10. package/dist/.claude-template/plugins/claude-hud/dist/config-reader.d.ts +8 -0
  11. package/dist/.claude-template/plugins/claude-hud/dist/config-reader.d.ts.map +1 -0
  12. package/dist/.claude-template/plugins/claude-hud/dist/config-reader.js +204 -0
  13. package/dist/.claude-template/plugins/claude-hud/dist/config-reader.js.map +1 -0
  14. package/dist/.claude-template/plugins/claude-hud/dist/config.d.ts +46 -0
  15. package/dist/.claude-template/plugins/claude-hud/dist/config.d.ts.map +1 -0
  16. package/dist/.claude-template/plugins/claude-hud/dist/config.js +220 -0
  17. package/dist/.claude-template/plugins/claude-hud/dist/config.js.map +1 -0
  18. package/dist/.claude-template/plugins/claude-hud/dist/constants.d.ts +10 -0
  19. package/dist/.claude-template/plugins/claude-hud/dist/constants.d.ts.map +1 -0
  20. package/dist/.claude-template/plugins/claude-hud/dist/constants.js +10 -0
  21. package/dist/.claude-template/plugins/claude-hud/dist/constants.js.map +1 -0
  22. package/dist/.claude-template/plugins/claude-hud/dist/debug.d.ts +6 -0
  23. package/dist/.claude-template/plugins/claude-hud/dist/debug.d.ts.map +1 -0
  24. package/dist/.claude-template/plugins/claude-hud/dist/debug.js +15 -0
  25. package/dist/.claude-template/plugins/claude-hud/dist/debug.js.map +1 -0
  26. package/dist/.claude-template/plugins/claude-hud/dist/extra-cmd.d.ts +23 -0
  27. package/dist/.claude-template/plugins/claude-hud/dist/extra-cmd.d.ts.map +1 -0
  28. package/dist/.claude-template/plugins/claude-hud/dist/extra-cmd.js +103 -0
  29. package/dist/.claude-template/plugins/claude-hud/dist/extra-cmd.js.map +1 -0
  30. package/dist/.claude-template/plugins/claude-hud/dist/git.d.ts +16 -0
  31. package/dist/.claude-template/plugins/claude-hud/dist/git.d.ts.map +1 -0
  32. package/dist/.claude-template/plugins/claude-hud/dist/git.js +86 -0
  33. package/dist/.claude-template/plugins/claude-hud/dist/git.js.map +1 -0
  34. package/dist/.claude-template/plugins/claude-hud/dist/index.d.ts +24 -0
  35. package/dist/.claude-template/plugins/claude-hud/dist/index.d.ts.map +1 -0
  36. package/dist/.claude-template/plugins/claude-hud/dist/index.js +97 -0
  37. package/dist/.claude-template/plugins/claude-hud/dist/index.js.map +1 -0
  38. package/dist/.claude-template/plugins/claude-hud/dist/render/agents-line.d.ts +3 -0
  39. package/dist/.claude-template/plugins/claude-hud/dist/render/agents-line.d.ts.map +1 -0
  40. package/dist/.claude-template/plugins/claude-hud/dist/render/agents-line.js +44 -0
  41. package/dist/.claude-template/plugins/claude-hud/dist/render/agents-line.js.map +1 -0
  42. package/dist/.claude-template/plugins/claude-hud/dist/render/colors.d.ts +12 -0
  43. package/dist/.claude-template/plugins/claude-hud/dist/render/colors.d.ts.map +1 -0
  44. package/dist/.claude-template/plugins/claude-hud/dist/render/colors.js +58 -0
  45. package/dist/.claude-template/plugins/claude-hud/dist/render/colors.js.map +1 -0
  46. package/dist/.claude-template/plugins/claude-hud/dist/render/index.d.ts +3 -0
  47. package/dist/.claude-template/plugins/claude-hud/dist/render/index.d.ts.map +1 -0
  48. package/dist/.claude-template/plugins/claude-hud/dist/render/index.js +379 -0
  49. package/dist/.claude-template/plugins/claude-hud/dist/render/index.js.map +1 -0
  50. package/dist/.claude-template/plugins/claude-hud/dist/render/lines/environment.d.ts +3 -0
  51. package/dist/.claude-template/plugins/claude-hud/dist/render/lines/environment.d.ts.map +1 -0
  52. package/dist/.claude-template/plugins/claude-hud/dist/render/lines/environment.js +30 -0
  53. package/dist/.claude-template/plugins/claude-hud/dist/render/lines/environment.js.map +1 -0
  54. package/dist/.claude-template/plugins/claude-hud/dist/render/lines/identity.d.ts +3 -0
  55. package/dist/.claude-template/plugins/claude-hud/dist/render/lines/identity.d.ts.map +1 -0
  56. package/dist/.claude-template/plugins/claude-hud/dist/render/lines/identity.js +52 -0
  57. package/dist/.claude-template/plugins/claude-hud/dist/render/lines/identity.js.map +1 -0
  58. package/dist/.claude-template/plugins/claude-hud/dist/render/lines/index.d.ts +5 -0
  59. package/dist/.claude-template/plugins/claude-hud/dist/render/lines/index.d.ts.map +1 -0
  60. package/dist/.claude-template/plugins/claude-hud/dist/render/lines/index.js +5 -0
  61. package/dist/.claude-template/plugins/claude-hud/dist/render/lines/index.js.map +1 -0
  62. package/dist/.claude-template/plugins/claude-hud/dist/render/lines/project.d.ts +3 -0
  63. package/dist/.claude-template/plugins/claude-hud/dist/render/lines/project.d.ts.map +1 -0
  64. package/dist/.claude-template/plugins/claude-hud/dist/render/lines/project.js +74 -0
  65. package/dist/.claude-template/plugins/claude-hud/dist/render/lines/project.js.map +1 -0
  66. package/dist/.claude-template/plugins/claude-hud/dist/render/lines/usage.d.ts +3 -0
  67. package/dist/.claude-template/plugins/claude-hud/dist/render/lines/usage.d.ts.map +1 -0
  68. package/dist/.claude-template/plugins/claude-hud/dist/render/lines/usage.js +92 -0
  69. package/dist/.claude-template/plugins/claude-hud/dist/render/lines/usage.js.map +1 -0
  70. package/dist/.claude-template/plugins/claude-hud/dist/render/session-line.d.ts +7 -0
  71. package/dist/.claude-template/plugins/claude-hud/dist/render/session-line.d.ts.map +1 -0
  72. package/dist/.claude-template/plugins/claude-hud/dist/render/session-line.js +247 -0
  73. package/dist/.claude-template/plugins/claude-hud/dist/render/session-line.js.map +1 -0
  74. package/dist/.claude-template/plugins/claude-hud/dist/render/todos-line.d.ts +3 -0
  75. package/dist/.claude-template/plugins/claude-hud/dist/render/todos-line.d.ts.map +1 -0
  76. package/dist/.claude-template/plugins/claude-hud/dist/render/todos-line.js +25 -0
  77. package/dist/.claude-template/plugins/claude-hud/dist/render/todos-line.js.map +1 -0
  78. package/dist/.claude-template/plugins/claude-hud/dist/render/tools-line.d.ts +3 -0
  79. package/dist/.claude-template/plugins/claude-hud/dist/render/tools-line.d.ts.map +1 -0
  80. package/dist/.claude-template/plugins/claude-hud/dist/render/tools-line.js +43 -0
  81. package/dist/.claude-template/plugins/claude-hud/dist/render/tools-line.js.map +1 -0
  82. package/dist/.claude-template/plugins/claude-hud/dist/speed-tracker.d.ts +7 -0
  83. package/dist/.claude-template/plugins/claude-hud/dist/speed-tracker.d.ts.map +1 -0
  84. package/dist/.claude-template/plugins/claude-hud/dist/speed-tracker.js +62 -0
  85. package/dist/.claude-template/plugins/claude-hud/dist/speed-tracker.js.map +1 -0
  86. package/dist/.claude-template/plugins/claude-hud/dist/stdin.d.ts +9 -0
  87. package/dist/.claude-template/plugins/claude-hud/dist/stdin.d.ts.map +1 -0
  88. package/dist/.claude-template/plugins/claude-hud/dist/stdin.js +136 -0
  89. package/dist/.claude-template/plugins/claude-hud/dist/stdin.js.map +1 -0
  90. package/dist/.claude-template/plugins/claude-hud/dist/transcript.d.ts +3 -0
  91. package/dist/.claude-template/plugins/claude-hud/dist/transcript.d.ts.map +1 -0
  92. package/dist/.claude-template/plugins/claude-hud/dist/transcript.js +189 -0
  93. package/dist/.claude-template/plugins/claude-hud/dist/transcript.js.map +1 -0
  94. package/dist/.claude-template/plugins/claude-hud/dist/types.d.ts +79 -0
  95. package/dist/.claude-template/plugins/claude-hud/dist/types.d.ts.map +1 -0
  96. package/dist/.claude-template/plugins/claude-hud/dist/types.js +5 -0
  97. package/dist/.claude-template/plugins/claude-hud/dist/types.js.map +1 -0
  98. package/dist/.claude-template/plugins/claude-hud/dist/usage-api.d.ts +59 -0
  99. package/dist/.claude-template/plugins/claude-hud/dist/usage-api.d.ts.map +1 -0
  100. package/dist/.claude-template/plugins/claude-hud/dist/usage-api.js +733 -0
  101. package/dist/.claude-template/plugins/claude-hud/dist/usage-api.js.map +1 -0
  102. package/dist/.claude-template/skills/cgraphx/SKILL.md +143 -0
  103. package/dist/.claude-template/skills/cgraphx/agent-prompt.md +56 -0
  104. package/dist/.claude-template/skills/clarify-requirements/SKILL.md +425 -0
  105. package/dist/.claude-template/skills/code-impact-api/SKILL.md +143 -0
  106. package/dist/.claude-template/skills/code-impact-api/agent-prompt.md +51 -0
  107. package/dist/.claude-template/skills/code-impact-docgen/SKILL.md +366 -0
  108. package/dist/.claude-template/skills/code-impact-docgen/template-business-html.md +242 -0
  109. package/dist/.claude-template/skills/code-impact-docgen/template-business-md.md +107 -0
  110. package/dist/.claude-template/skills/code-impact-docgen/template-technical-html.md +205 -0
  111. package/dist/.claude-template/skills/code-impact-docgen/template-technical-md.md +155 -0
  112. package/dist/.claude-template/skills/code-impact-init/SKILL.md +800 -0
  113. package/dist/.claude-template/skills/code-impact-markdown/SKILL.md +345 -0
  114. package/dist/.claude-template/skills/code-impact-markdown/template-guide.md +68 -0
  115. package/dist/.claude-template/skills/code-impact-markdown/template-memory.md +82 -0
  116. package/dist/.claude-template/skills/code-impact-markdown/template-runbook.md +58 -0
  117. package/dist/.claude-template/skills/db-query/SKILL.md +166 -0
  118. package/dist/.claude-template/skills/db-query/agent-prompt.md +55 -0
  119. package/dist/.claude-template/skills/developer-timeline/SKILL.md +302 -0
  120. package/dist/.claude-template/skills/developer-timeline/demo-single-page-report.html +657 -0
  121. package/dist/.claude-template/skills/implementation/SKILL.md +136 -0
  122. package/dist/.claude-template/skills/subagent-implement/SKILL.md +225 -0
  123. package/dist/.claude-template/skills/subagent-implement/implementer-prompt.md +127 -0
  124. package/dist/.claude-template/skills/subagent-implement/quality-reviewer-prompt.md +130 -0
  125. package/dist/.claude-template/skills/subagent-implement/spec-reviewer-prompt.md +112 -0
  126. package/dist/.claude-template/skills/write-plan/SKILL.md +322 -0
  127. package/dist/.claude-template/skills/write-plan/plan-document-reviewer-prompt.md +134 -0
  128. package/dist/.claude-template/skills/write-prd/SKILL.md +242 -0
  129. package/dist/.claude-template/skills/write-spec/SKILL.md +278 -0
  130. package/dist/bin/codegraph.d.ts +26 -0
  131. package/dist/bin/codegraph.d.ts.map +1 -0
  132. package/dist/bin/codegraph.js +2014 -0
  133. package/dist/bin/codegraph.js.map +1 -0
  134. package/dist/bin/fatal-handler.d.ts +20 -0
  135. package/dist/bin/fatal-handler.d.ts.map +1 -0
  136. package/dist/bin/fatal-handler.js +118 -0
  137. package/dist/bin/fatal-handler.js.map +1 -0
  138. package/dist/bin/node-version-check.d.ts +51 -0
  139. package/dist/bin/node-version-check.d.ts.map +1 -0
  140. package/dist/bin/node-version-check.js +114 -0
  141. package/dist/bin/node-version-check.js.map +1 -0
  142. package/dist/bin/uninstall.d.ts +14 -0
  143. package/dist/bin/uninstall.d.ts.map +1 -0
  144. package/dist/bin/uninstall.js +36 -0
  145. package/dist/bin/uninstall.js.map +1 -0
  146. package/dist/context/formatter.d.ts +30 -0
  147. package/dist/context/formatter.d.ts.map +1 -0
  148. package/dist/context/formatter.js +263 -0
  149. package/dist/context/formatter.js.map +1 -0
  150. package/dist/context/index.d.ts +119 -0
  151. package/dist/context/index.d.ts.map +1 -0
  152. package/dist/context/index.js +1296 -0
  153. package/dist/context/index.js.map +1 -0
  154. package/dist/context/markers.d.ts +19 -0
  155. package/dist/context/markers.d.ts.map +1 -0
  156. package/dist/context/markers.js +22 -0
  157. package/dist/context/markers.js.map +1 -0
  158. package/dist/db/index.d.ts +122 -0
  159. package/dist/db/index.d.ts.map +1 -0
  160. package/dist/db/index.js +296 -0
  161. package/dist/db/index.js.map +1 -0
  162. package/dist/db/migrations.d.ts +44 -0
  163. package/dist/db/migrations.d.ts.map +1 -0
  164. package/dist/db/migrations.js +140 -0
  165. package/dist/db/migrations.js.map +1 -0
  166. package/dist/db/queries.d.ts +401 -0
  167. package/dist/db/queries.d.ts.map +1 -0
  168. package/dist/db/queries.js +1591 -0
  169. package/dist/db/queries.js.map +1 -0
  170. package/dist/db/schema.sql +152 -0
  171. package/dist/db/sqlite-adapter.d.ts +53 -0
  172. package/dist/db/sqlite-adapter.d.ts.map +1 -0
  173. package/dist/db/sqlite-adapter.js +117 -0
  174. package/dist/db/sqlite-adapter.js.map +1 -0
  175. package/dist/dbquery/cli.d.ts +17 -0
  176. package/dist/dbquery/cli.d.ts.map +1 -0
  177. package/dist/dbquery/cli.js +229 -0
  178. package/dist/dbquery/cli.js.map +1 -0
  179. package/dist/dbquery/config.d.ts +38 -0
  180. package/dist/dbquery/config.d.ts.map +1 -0
  181. package/dist/dbquery/config.js +244 -0
  182. package/dist/dbquery/config.js.map +1 -0
  183. package/dist/dbquery/constants.d.ts +40 -0
  184. package/dist/dbquery/constants.d.ts.map +1 -0
  185. package/dist/dbquery/constants.js +65 -0
  186. package/dist/dbquery/constants.js.map +1 -0
  187. package/dist/dbquery/drivers/mysql.d.ts +15 -0
  188. package/dist/dbquery/drivers/mysql.d.ts.map +1 -0
  189. package/dist/dbquery/drivers/mysql.js +102 -0
  190. package/dist/dbquery/drivers/mysql.js.map +1 -0
  191. package/dist/dbquery/drivers/postgres.d.ts +16 -0
  192. package/dist/dbquery/drivers/postgres.d.ts.map +1 -0
  193. package/dist/dbquery/drivers/postgres.js +105 -0
  194. package/dist/dbquery/drivers/postgres.js.map +1 -0
  195. package/dist/dbquery/errors.d.ts +40 -0
  196. package/dist/dbquery/errors.d.ts.map +1 -0
  197. package/dist/dbquery/errors.js +85 -0
  198. package/dist/dbquery/errors.js.map +1 -0
  199. package/dist/dbquery/executor.d.ts +30 -0
  200. package/dist/dbquery/executor.d.ts.map +1 -0
  201. package/dist/dbquery/executor.js +243 -0
  202. package/dist/dbquery/executor.js.map +1 -0
  203. package/dist/dbquery/format.d.ts +18 -0
  204. package/dist/dbquery/format.d.ts.map +1 -0
  205. package/dist/dbquery/format.js +174 -0
  206. package/dist/dbquery/format.js.map +1 -0
  207. package/dist/dbquery/index.d.ts +10 -0
  208. package/dist/dbquery/index.d.ts.map +1 -0
  209. package/dist/dbquery/index.js +23 -0
  210. package/dist/dbquery/index.js.map +1 -0
  211. package/dist/dbquery/init.d.ts +33 -0
  212. package/dist/dbquery/init.d.ts.map +1 -0
  213. package/dist/dbquery/init.js +125 -0
  214. package/dist/dbquery/init.js.map +1 -0
  215. package/dist/dbquery/logging.d.ts +22 -0
  216. package/dist/dbquery/logging.d.ts.map +1 -0
  217. package/dist/dbquery/logging.js +140 -0
  218. package/dist/dbquery/logging.js.map +1 -0
  219. package/dist/dbquery/mcp-tools.d.ts +29 -0
  220. package/dist/dbquery/mcp-tools.d.ts.map +1 -0
  221. package/dist/dbquery/mcp-tools.js +206 -0
  222. package/dist/dbquery/mcp-tools.js.map +1 -0
  223. package/dist/dbquery/queries.d.ts +31 -0
  224. package/dist/dbquery/queries.d.ts.map +1 -0
  225. package/dist/dbquery/queries.js +160 -0
  226. package/dist/dbquery/queries.js.map +1 -0
  227. package/dist/dbquery/safety.d.ts +35 -0
  228. package/dist/dbquery/safety.d.ts.map +1 -0
  229. package/dist/dbquery/safety.js +306 -0
  230. package/dist/dbquery/safety.js.map +1 -0
  231. package/dist/dbquery/types.d.ts +152 -0
  232. package/dist/dbquery/types.d.ts.map +1 -0
  233. package/dist/dbquery/types.js +10 -0
  234. package/dist/dbquery/types.js.map +1 -0
  235. package/dist/directory.d.ts +147 -0
  236. package/dist/directory.d.ts.map +1 -0
  237. package/dist/directory.js +523 -0
  238. package/dist/directory.js.map +1 -0
  239. package/dist/errors.d.ts +136 -0
  240. package/dist/errors.d.ts.map +1 -0
  241. package/dist/errors.js +219 -0
  242. package/dist/errors.js.map +1 -0
  243. package/dist/extraction/astro-extractor.d.ts +79 -0
  244. package/dist/extraction/astro-extractor.d.ts.map +1 -0
  245. package/dist/extraction/astro-extractor.js +320 -0
  246. package/dist/extraction/astro-extractor.js.map +1 -0
  247. package/dist/extraction/dfm-extractor.d.ts +31 -0
  248. package/dist/extraction/dfm-extractor.d.ts.map +1 -0
  249. package/dist/extraction/dfm-extractor.js +151 -0
  250. package/dist/extraction/dfm-extractor.js.map +1 -0
  251. package/dist/extraction/extraction-version.d.ts +25 -0
  252. package/dist/extraction/extraction-version.d.ts.map +1 -0
  253. package/dist/extraction/extraction-version.js +28 -0
  254. package/dist/extraction/extraction-version.js.map +1 -0
  255. package/dist/extraction/function-ref.d.ts +118 -0
  256. package/dist/extraction/function-ref.d.ts.map +1 -0
  257. package/dist/extraction/function-ref.js +727 -0
  258. package/dist/extraction/function-ref.js.map +1 -0
  259. package/dist/extraction/generated-detection.d.ts +30 -0
  260. package/dist/extraction/generated-detection.d.ts.map +1 -0
  261. package/dist/extraction/generated-detection.js +83 -0
  262. package/dist/extraction/generated-detection.js.map +1 -0
  263. package/dist/extraction/grammars.d.ts +114 -0
  264. package/dist/extraction/grammars.d.ts.map +1 -0
  265. package/dist/extraction/grammars.js +477 -0
  266. package/dist/extraction/grammars.js.map +1 -0
  267. package/dist/extraction/index.d.ts +175 -0
  268. package/dist/extraction/index.d.ts.map +1 -0
  269. package/dist/extraction/index.js +1887 -0
  270. package/dist/extraction/index.js.map +1 -0
  271. package/dist/extraction/languages/c-cpp.d.ts +12 -0
  272. package/dist/extraction/languages/c-cpp.d.ts.map +1 -0
  273. package/dist/extraction/languages/c-cpp.js +275 -0
  274. package/dist/extraction/languages/c-cpp.js.map +1 -0
  275. package/dist/extraction/languages/csharp.d.ts +25 -0
  276. package/dist/extraction/languages/csharp.d.ts.map +1 -0
  277. package/dist/extraction/languages/csharp.js +175 -0
  278. package/dist/extraction/languages/csharp.js.map +1 -0
  279. package/dist/extraction/languages/dart.d.ts +3 -0
  280. package/dist/extraction/languages/dart.d.ts.map +1 -0
  281. package/dist/extraction/languages/dart.js +374 -0
  282. package/dist/extraction/languages/dart.js.map +1 -0
  283. package/dist/extraction/languages/go.d.ts +3 -0
  284. package/dist/extraction/languages/go.d.ts.map +1 -0
  285. package/dist/extraction/languages/go.js +111 -0
  286. package/dist/extraction/languages/go.js.map +1 -0
  287. package/dist/extraction/languages/index.d.ts +10 -0
  288. package/dist/extraction/languages/index.d.ts.map +1 -0
  289. package/dist/extraction/languages/index.js +53 -0
  290. package/dist/extraction/languages/index.js.map +1 -0
  291. package/dist/extraction/languages/java.d.ts +3 -0
  292. package/dist/extraction/languages/java.d.ts.map +1 -0
  293. package/dist/extraction/languages/java.js +315 -0
  294. package/dist/extraction/languages/java.js.map +1 -0
  295. package/dist/extraction/languages/javascript.d.ts +3 -0
  296. package/dist/extraction/languages/javascript.d.ts.map +1 -0
  297. package/dist/extraction/languages/javascript.js +106 -0
  298. package/dist/extraction/languages/javascript.js.map +1 -0
  299. package/dist/extraction/languages/kotlin.d.ts +3 -0
  300. package/dist/extraction/languages/kotlin.d.ts.map +1 -0
  301. package/dist/extraction/languages/kotlin.js +379 -0
  302. package/dist/extraction/languages/kotlin.js.map +1 -0
  303. package/dist/extraction/languages/lua.d.ts +3 -0
  304. package/dist/extraction/languages/lua.d.ts.map +1 -0
  305. package/dist/extraction/languages/lua.js +150 -0
  306. package/dist/extraction/languages/lua.js.map +1 -0
  307. package/dist/extraction/languages/luau.d.ts +3 -0
  308. package/dist/extraction/languages/luau.d.ts.map +1 -0
  309. package/dist/extraction/languages/luau.js +37 -0
  310. package/dist/extraction/languages/luau.js.map +1 -0
  311. package/dist/extraction/languages/objc.d.ts +3 -0
  312. package/dist/extraction/languages/objc.d.ts.map +1 -0
  313. package/dist/extraction/languages/objc.js +175 -0
  314. package/dist/extraction/languages/objc.js.map +1 -0
  315. package/dist/extraction/languages/pascal.d.ts +3 -0
  316. package/dist/extraction/languages/pascal.d.ts.map +1 -0
  317. package/dist/extraction/languages/pascal.js +77 -0
  318. package/dist/extraction/languages/pascal.js.map +1 -0
  319. package/dist/extraction/languages/php.d.ts +3 -0
  320. package/dist/extraction/languages/php.d.ts.map +1 -0
  321. package/dist/extraction/languages/php.js +196 -0
  322. package/dist/extraction/languages/php.js.map +1 -0
  323. package/dist/extraction/languages/python.d.ts +3 -0
  324. package/dist/extraction/languages/python.d.ts.map +1 -0
  325. package/dist/extraction/languages/python.js +56 -0
  326. package/dist/extraction/languages/python.js.map +1 -0
  327. package/dist/extraction/languages/r.d.ts +3 -0
  328. package/dist/extraction/languages/r.d.ts.map +1 -0
  329. package/dist/extraction/languages/r.js +314 -0
  330. package/dist/extraction/languages/r.js.map +1 -0
  331. package/dist/extraction/languages/ruby.d.ts +3 -0
  332. package/dist/extraction/languages/ruby.d.ts.map +1 -0
  333. package/dist/extraction/languages/ruby.js +149 -0
  334. package/dist/extraction/languages/ruby.js.map +1 -0
  335. package/dist/extraction/languages/rust.d.ts +3 -0
  336. package/dist/extraction/languages/rust.d.ts.map +1 -0
  337. package/dist/extraction/languages/rust.js +142 -0
  338. package/dist/extraction/languages/rust.js.map +1 -0
  339. package/dist/extraction/languages/scala.d.ts +3 -0
  340. package/dist/extraction/languages/scala.d.ts.map +1 -0
  341. package/dist/extraction/languages/scala.js +209 -0
  342. package/dist/extraction/languages/scala.js.map +1 -0
  343. package/dist/extraction/languages/swift.d.ts +3 -0
  344. package/dist/extraction/languages/swift.d.ts.map +1 -0
  345. package/dist/extraction/languages/swift.js +152 -0
  346. package/dist/extraction/languages/swift.js.map +1 -0
  347. package/dist/extraction/languages/typescript.d.ts +16 -0
  348. package/dist/extraction/languages/typescript.d.ts.map +1 -0
  349. package/dist/extraction/languages/typescript.js +167 -0
  350. package/dist/extraction/languages/typescript.js.map +1 -0
  351. package/dist/extraction/liquid-extractor.d.ts +59 -0
  352. package/dist/extraction/liquid-extractor.d.ts.map +1 -0
  353. package/dist/extraction/liquid-extractor.js +357 -0
  354. package/dist/extraction/liquid-extractor.js.map +1 -0
  355. package/dist/extraction/mybatis-extractor.d.ts +48 -0
  356. package/dist/extraction/mybatis-extractor.d.ts.map +1 -0
  357. package/dist/extraction/mybatis-extractor.js +198 -0
  358. package/dist/extraction/mybatis-extractor.js.map +1 -0
  359. package/dist/extraction/parse-worker.d.ts +8 -0
  360. package/dist/extraction/parse-worker.d.ts.map +1 -0
  361. package/dist/extraction/parse-worker.js +97 -0
  362. package/dist/extraction/parse-worker.js.map +1 -0
  363. package/dist/extraction/razor-extractor.d.ts +42 -0
  364. package/dist/extraction/razor-extractor.d.ts.map +1 -0
  365. package/dist/extraction/razor-extractor.js +285 -0
  366. package/dist/extraction/razor-extractor.js.map +1 -0
  367. package/dist/extraction/svelte-extractor.d.ts +56 -0
  368. package/dist/extraction/svelte-extractor.d.ts.map +1 -0
  369. package/dist/extraction/svelte-extractor.js +275 -0
  370. package/dist/extraction/svelte-extractor.js.map +1 -0
  371. package/dist/extraction/tree-sitter-helpers.d.ts +28 -0
  372. package/dist/extraction/tree-sitter-helpers.d.ts.map +1 -0
  373. package/dist/extraction/tree-sitter-helpers.js +152 -0
  374. package/dist/extraction/tree-sitter-helpers.js.map +1 -0
  375. package/dist/extraction/tree-sitter-types.d.ts +239 -0
  376. package/dist/extraction/tree-sitter-types.d.ts.map +1 -0
  377. package/dist/extraction/tree-sitter-types.js +10 -0
  378. package/dist/extraction/tree-sitter-types.js.map +1 -0
  379. package/dist/extraction/tree-sitter.d.ts +647 -0
  380. package/dist/extraction/tree-sitter.d.ts.map +1 -0
  381. package/dist/extraction/tree-sitter.js +5592 -0
  382. package/dist/extraction/tree-sitter.js.map +1 -0
  383. package/dist/extraction/vue-extractor.d.ts +51 -0
  384. package/dist/extraction/vue-extractor.d.ts.map +1 -0
  385. package/dist/extraction/vue-extractor.js +254 -0
  386. package/dist/extraction/vue-extractor.js.map +1 -0
  387. package/dist/extraction/wasm/tree-sitter-c_sharp.wasm +0 -0
  388. package/dist/extraction/wasm/tree-sitter-lua.wasm +0 -0
  389. package/dist/extraction/wasm/tree-sitter-luau.wasm +0 -0
  390. package/dist/extraction/wasm/tree-sitter-pascal.wasm +0 -0
  391. package/dist/extraction/wasm/tree-sitter-r.wasm +0 -0
  392. package/dist/extraction/wasm/tree-sitter-scala.wasm +0 -0
  393. package/dist/extraction/wasm-runtime-flags.d.ts +38 -0
  394. package/dist/extraction/wasm-runtime-flags.d.ts.map +1 -0
  395. package/dist/extraction/wasm-runtime-flags.js +106 -0
  396. package/dist/extraction/wasm-runtime-flags.js.map +1 -0
  397. package/dist/graph/index.d.ts +8 -0
  398. package/dist/graph/index.d.ts.map +1 -0
  399. package/dist/graph/index.js +13 -0
  400. package/dist/graph/index.js.map +1 -0
  401. package/dist/graph/queries.d.ts +106 -0
  402. package/dist/graph/queries.d.ts.map +1 -0
  403. package/dist/graph/queries.js +339 -0
  404. package/dist/graph/queries.js.map +1 -0
  405. package/dist/graph/traversal.d.ts +127 -0
  406. package/dist/graph/traversal.d.ts.map +1 -0
  407. package/dist/graph/traversal.js +540 -0
  408. package/dist/graph/traversal.js.map +1 -0
  409. package/dist/index.d.ts +563 -0
  410. package/dist/index.d.ts.map +1 -0
  411. package/dist/index.js +1041 -0
  412. package/dist/index.js.map +1 -0
  413. package/dist/installer/claude-assets.d.ts +45 -0
  414. package/dist/installer/claude-assets.d.ts.map +1 -0
  415. package/dist/installer/claude-assets.js +144 -0
  416. package/dist/installer/claude-assets.js.map +1 -0
  417. package/dist/installer/config-writer.d.ts +28 -0
  418. package/dist/installer/config-writer.d.ts.map +1 -0
  419. package/dist/installer/config-writer.js +91 -0
  420. package/dist/installer/config-writer.js.map +1 -0
  421. package/dist/installer/index.d.ts +101 -0
  422. package/dist/installer/index.d.ts.map +1 -0
  423. package/dist/installer/index.js +692 -0
  424. package/dist/installer/index.js.map +1 -0
  425. package/dist/installer/instructions-template.d.ts +41 -0
  426. package/dist/installer/instructions-template.d.ts.map +1 -0
  427. package/dist/installer/instructions-template.js +53 -0
  428. package/dist/installer/instructions-template.js.map +1 -0
  429. package/dist/installer/targets/antigravity.d.ts +57 -0
  430. package/dist/installer/targets/antigravity.d.ts.map +1 -0
  431. package/dist/installer/targets/antigravity.js +308 -0
  432. package/dist/installer/targets/antigravity.js.map +1 -0
  433. package/dist/installer/targets/claude.d.ts +66 -0
  434. package/dist/installer/targets/claude.d.ts.map +1 -0
  435. package/dist/installer/targets/claude.js +564 -0
  436. package/dist/installer/targets/claude.js.map +1 -0
  437. package/dist/installer/targets/codex.d.ts +18 -0
  438. package/dist/installer/targets/codex.d.ts.map +1 -0
  439. package/dist/installer/targets/codex.js +185 -0
  440. package/dist/installer/targets/codex.js.map +1 -0
  441. package/dist/installer/targets/cursor.d.ts +35 -0
  442. package/dist/installer/targets/cursor.d.ts.map +1 -0
  443. package/dist/installer/targets/cursor.js +254 -0
  444. package/dist/installer/targets/cursor.js.map +1 -0
  445. package/dist/installer/targets/gemini.d.ts +26 -0
  446. package/dist/installer/targets/gemini.d.ts.map +1 -0
  447. package/dist/installer/targets/gemini.js +165 -0
  448. package/dist/installer/targets/gemini.js.map +1 -0
  449. package/dist/installer/targets/hermes.d.ts +18 -0
  450. package/dist/installer/targets/hermes.d.ts.map +1 -0
  451. package/dist/installer/targets/hermes.js +359 -0
  452. package/dist/installer/targets/hermes.js.map +1 -0
  453. package/dist/installer/targets/kiro.d.ts +27 -0
  454. package/dist/installer/targets/kiro.d.ts.map +1 -0
  455. package/dist/installer/targets/kiro.js +178 -0
  456. package/dist/installer/targets/kiro.js.map +1 -0
  457. package/dist/installer/targets/opencode.d.ts +38 -0
  458. package/dist/installer/targets/opencode.d.ts.map +1 -0
  459. package/dist/installer/targets/opencode.js +288 -0
  460. package/dist/installer/targets/opencode.js.map +1 -0
  461. package/dist/installer/targets/registry.d.ts +35 -0
  462. package/dist/installer/targets/registry.d.ts.map +1 -0
  463. package/dist/installer/targets/registry.js +91 -0
  464. package/dist/installer/targets/registry.js.map +1 -0
  465. package/dist/installer/targets/shared.d.ts +101 -0
  466. package/dist/installer/targets/shared.d.ts.map +1 -0
  467. package/dist/installer/targets/shared.js +264 -0
  468. package/dist/installer/targets/shared.js.map +1 -0
  469. package/dist/installer/targets/toml.d.ts +52 -0
  470. package/dist/installer/targets/toml.d.ts.map +1 -0
  471. package/dist/installer/targets/toml.js +147 -0
  472. package/dist/installer/targets/toml.js.map +1 -0
  473. package/dist/installer/targets/types.d.ts +108 -0
  474. package/dist/installer/targets/types.d.ts.map +1 -0
  475. package/dist/installer/targets/types.js +16 -0
  476. package/dist/installer/targets/types.js.map +1 -0
  477. package/dist/markdown/cli.d.ts +16 -0
  478. package/dist/markdown/cli.d.ts.map +1 -0
  479. package/dist/markdown/cli.js +533 -0
  480. package/dist/markdown/cli.js.map +1 -0
  481. package/dist/markdown/constants.d.ts +22 -0
  482. package/dist/markdown/constants.d.ts.map +1 -0
  483. package/dist/markdown/constants.js +71 -0
  484. package/dist/markdown/constants.js.map +1 -0
  485. package/dist/markdown/dedup.d.ts +20 -0
  486. package/dist/markdown/dedup.d.ts.map +1 -0
  487. package/dist/markdown/dedup.js +64 -0
  488. package/dist/markdown/dedup.js.map +1 -0
  489. package/dist/markdown/errors.d.ts +22 -0
  490. package/dist/markdown/errors.d.ts.map +1 -0
  491. package/dist/markdown/errors.js +45 -0
  492. package/dist/markdown/errors.js.map +1 -0
  493. package/dist/markdown/extractor.d.ts +43 -0
  494. package/dist/markdown/extractor.d.ts.map +1 -0
  495. package/dist/markdown/extractor.js +152 -0
  496. package/dist/markdown/extractor.js.map +1 -0
  497. package/dist/markdown/frontmatter-parser.d.ts +47 -0
  498. package/dist/markdown/frontmatter-parser.d.ts.map +1 -0
  499. package/dist/markdown/frontmatter-parser.js +199 -0
  500. package/dist/markdown/frontmatter-parser.js.map +1 -0
  501. package/dist/markdown/indexer.d.ts +34 -0
  502. package/dist/markdown/indexer.d.ts.map +1 -0
  503. package/dist/markdown/indexer.js +256 -0
  504. package/dist/markdown/indexer.js.map +1 -0
  505. package/dist/markdown/mcp-tools.d.ts +33 -0
  506. package/dist/markdown/mcp-tools.d.ts.map +1 -0
  507. package/dist/markdown/mcp-tools.js +300 -0
  508. package/dist/markdown/mcp-tools.js.map +1 -0
  509. package/dist/markdown/query.d.ts +108 -0
  510. package/dist/markdown/query.d.ts.map +1 -0
  511. package/dist/markdown/query.js +570 -0
  512. package/dist/markdown/query.js.map +1 -0
  513. package/dist/markdown/schema-bootstrap.d.ts +40 -0
  514. package/dist/markdown/schema-bootstrap.d.ts.map +1 -0
  515. package/dist/markdown/schema-bootstrap.js +85 -0
  516. package/dist/markdown/schema-bootstrap.js.map +1 -0
  517. package/dist/markdown/schema.sql +124 -0
  518. package/dist/markdown/store.d.ts +77 -0
  519. package/dist/markdown/store.d.ts.map +1 -0
  520. package/dist/markdown/store.js +194 -0
  521. package/dist/markdown/store.js.map +1 -0
  522. package/dist/markdown/summary-extractor.d.ts +22 -0
  523. package/dist/markdown/summary-extractor.d.ts.map +1 -0
  524. package/dist/markdown/summary-extractor.js +66 -0
  525. package/dist/markdown/summary-extractor.js.map +1 -0
  526. package/dist/markdown/types.d.ts +159 -0
  527. package/dist/markdown/types.d.ts.map +1 -0
  528. package/dist/markdown/types.js +9 -0
  529. package/dist/markdown/types.js.map +1 -0
  530. package/dist/markdown/validator.d.ts +44 -0
  531. package/dist/markdown/validator.d.ts.map +1 -0
  532. package/dist/markdown/validator.js +95 -0
  533. package/dist/markdown/validator.js.map +1 -0
  534. package/dist/mcp/daemon-manager.d.ts +42 -0
  535. package/dist/mcp/daemon-manager.d.ts.map +1 -0
  536. package/dist/mcp/daemon-manager.js +129 -0
  537. package/dist/mcp/daemon-manager.js.map +1 -0
  538. package/dist/mcp/daemon-paths.d.ts +46 -0
  539. package/dist/mcp/daemon-paths.d.ts.map +1 -0
  540. package/dist/mcp/daemon-paths.js +125 -0
  541. package/dist/mcp/daemon-paths.js.map +1 -0
  542. package/dist/mcp/daemon-registry.d.ts +47 -0
  543. package/dist/mcp/daemon-registry.d.ts.map +1 -0
  544. package/dist/mcp/daemon-registry.js +229 -0
  545. package/dist/mcp/daemon-registry.js.map +1 -0
  546. package/dist/mcp/daemon.d.ts +220 -0
  547. package/dist/mcp/daemon.d.ts.map +1 -0
  548. package/dist/mcp/daemon.js +637 -0
  549. package/dist/mcp/daemon.js.map +1 -0
  550. package/dist/mcp/dynamic-boundaries.d.ts +41 -0
  551. package/dist/mcp/dynamic-boundaries.d.ts.map +1 -0
  552. package/dist/mcp/dynamic-boundaries.js +359 -0
  553. package/dist/mcp/dynamic-boundaries.js.map +1 -0
  554. package/dist/mcp/engine.d.ts +105 -0
  555. package/dist/mcp/engine.d.ts.map +1 -0
  556. package/dist/mcp/engine.js +278 -0
  557. package/dist/mcp/engine.js.map +1 -0
  558. package/dist/mcp/index.d.ts +113 -0
  559. package/dist/mcp/index.d.ts.map +1 -0
  560. package/dist/mcp/index.js +499 -0
  561. package/dist/mcp/index.js.map +1 -0
  562. package/dist/mcp/liveness-watchdog.d.ts +18 -0
  563. package/dist/mcp/liveness-watchdog.d.ts.map +1 -0
  564. package/dist/mcp/liveness-watchdog.js +207 -0
  565. package/dist/mcp/liveness-watchdog.js.map +1 -0
  566. package/dist/mcp/ppid-watchdog.d.ts +44 -0
  567. package/dist/mcp/ppid-watchdog.d.ts.map +1 -0
  568. package/dist/mcp/ppid-watchdog.js +27 -0
  569. package/dist/mcp/ppid-watchdog.js.map +1 -0
  570. package/dist/mcp/proxy.d.ts +87 -0
  571. package/dist/mcp/proxy.d.ts.map +1 -0
  572. package/dist/mcp/proxy.js +641 -0
  573. package/dist/mcp/proxy.js.map +1 -0
  574. package/dist/mcp/server-instructions.d.ts +34 -0
  575. package/dist/mcp/server-instructions.d.ts.map +1 -0
  576. package/dist/mcp/server-instructions.js +106 -0
  577. package/dist/mcp/server-instructions.js.map +1 -0
  578. package/dist/mcp/session.d.ts +79 -0
  579. package/dist/mcp/session.d.ts.map +1 -0
  580. package/dist/mcp/session.js +330 -0
  581. package/dist/mcp/session.js.map +1 -0
  582. package/dist/mcp/stdin-teardown.d.ts +27 -0
  583. package/dist/mcp/stdin-teardown.d.ts.map +1 -0
  584. package/dist/mcp/stdin-teardown.js +49 -0
  585. package/dist/mcp/stdin-teardown.js.map +1 -0
  586. package/dist/mcp/tools.d.ts +547 -0
  587. package/dist/mcp/tools.d.ts.map +1 -0
  588. package/dist/mcp/tools.js +4122 -0
  589. package/dist/mcp/tools.js.map +1 -0
  590. package/dist/mcp/transport.d.ts +188 -0
  591. package/dist/mcp/transport.d.ts.map +1 -0
  592. package/dist/mcp/transport.js +359 -0
  593. package/dist/mcp/transport.js.map +1 -0
  594. package/dist/mcp/version.d.ts +19 -0
  595. package/dist/mcp/version.d.ts.map +1 -0
  596. package/dist/mcp/version.js +71 -0
  597. package/dist/mcp/version.js.map +1 -0
  598. package/dist/project-config.d.ts +36 -0
  599. package/dist/project-config.d.ts.map +1 -0
  600. package/dist/project-config.js +235 -0
  601. package/dist/project-config.js.map +1 -0
  602. package/dist/reasoning/config.d.ts +45 -0
  603. package/dist/reasoning/config.d.ts.map +1 -0
  604. package/dist/reasoning/config.js +171 -0
  605. package/dist/reasoning/config.js.map +1 -0
  606. package/dist/reasoning/credentials.d.ts +5 -0
  607. package/dist/reasoning/credentials.d.ts.map +1 -0
  608. package/dist/reasoning/credentials.js +83 -0
  609. package/dist/reasoning/credentials.js.map +1 -0
  610. package/dist/reasoning/login.d.ts +21 -0
  611. package/dist/reasoning/login.d.ts.map +1 -0
  612. package/dist/reasoning/login.js +85 -0
  613. package/dist/reasoning/login.js.map +1 -0
  614. package/dist/reasoning/reasoner.d.ts +43 -0
  615. package/dist/reasoning/reasoner.d.ts.map +1 -0
  616. package/dist/reasoning/reasoner.js +308 -0
  617. package/dist/reasoning/reasoner.js.map +1 -0
  618. package/dist/resolution/c-fnptr-synthesizer.d.ts +33 -0
  619. package/dist/resolution/c-fnptr-synthesizer.d.ts.map +1 -0
  620. package/dist/resolution/c-fnptr-synthesizer.js +352 -0
  621. package/dist/resolution/c-fnptr-synthesizer.js.map +1 -0
  622. package/dist/resolution/callback-synthesizer.d.ts +15 -0
  623. package/dist/resolution/callback-synthesizer.d.ts.map +1 -0
  624. package/dist/resolution/callback-synthesizer.js +2926 -0
  625. package/dist/resolution/callback-synthesizer.js.map +1 -0
  626. package/dist/resolution/frameworks/astro.d.ts +9 -0
  627. package/dist/resolution/frameworks/astro.d.ts.map +1 -0
  628. package/dist/resolution/frameworks/astro.js +169 -0
  629. package/dist/resolution/frameworks/astro.js.map +1 -0
  630. package/dist/resolution/frameworks/cargo-workspace.d.ts +18 -0
  631. package/dist/resolution/frameworks/cargo-workspace.d.ts.map +1 -0
  632. package/dist/resolution/frameworks/cargo-workspace.js +225 -0
  633. package/dist/resolution/frameworks/cargo-workspace.js.map +1 -0
  634. package/dist/resolution/frameworks/csharp.d.ts +8 -0
  635. package/dist/resolution/frameworks/csharp.d.ts.map +1 -0
  636. package/dist/resolution/frameworks/csharp.js +241 -0
  637. package/dist/resolution/frameworks/csharp.js.map +1 -0
  638. package/dist/resolution/frameworks/drupal.d.ts +51 -0
  639. package/dist/resolution/frameworks/drupal.d.ts.map +1 -0
  640. package/dist/resolution/frameworks/drupal.js +367 -0
  641. package/dist/resolution/frameworks/drupal.js.map +1 -0
  642. package/dist/resolution/frameworks/expo-modules.d.ts +3 -0
  643. package/dist/resolution/frameworks/expo-modules.d.ts.map +1 -0
  644. package/dist/resolution/frameworks/expo-modules.js +148 -0
  645. package/dist/resolution/frameworks/expo-modules.js.map +1 -0
  646. package/dist/resolution/frameworks/express.d.ts +8 -0
  647. package/dist/resolution/frameworks/express.d.ts.map +1 -0
  648. package/dist/resolution/frameworks/express.js +308 -0
  649. package/dist/resolution/frameworks/express.js.map +1 -0
  650. package/dist/resolution/frameworks/fabric.d.ts +3 -0
  651. package/dist/resolution/frameworks/fabric.d.ts.map +1 -0
  652. package/dist/resolution/frameworks/fabric.js +354 -0
  653. package/dist/resolution/frameworks/fabric.js.map +1 -0
  654. package/dist/resolution/frameworks/go.d.ts +8 -0
  655. package/dist/resolution/frameworks/go.d.ts.map +1 -0
  656. package/dist/resolution/frameworks/go.js +161 -0
  657. package/dist/resolution/frameworks/go.js.map +1 -0
  658. package/dist/resolution/frameworks/goframe.d.ts +41 -0
  659. package/dist/resolution/frameworks/goframe.d.ts.map +1 -0
  660. package/dist/resolution/frameworks/goframe.js +112 -0
  661. package/dist/resolution/frameworks/goframe.js.map +1 -0
  662. package/dist/resolution/frameworks/index.d.ts +50 -0
  663. package/dist/resolution/frameworks/index.d.ts.map +1 -0
  664. package/dist/resolution/frameworks/index.js +169 -0
  665. package/dist/resolution/frameworks/index.js.map +1 -0
  666. package/dist/resolution/frameworks/java.d.ts +8 -0
  667. package/dist/resolution/frameworks/java.d.ts.map +1 -0
  668. package/dist/resolution/frameworks/java.js +509 -0
  669. package/dist/resolution/frameworks/java.js.map +1 -0
  670. package/dist/resolution/frameworks/laravel.d.ts +13 -0
  671. package/dist/resolution/frameworks/laravel.d.ts.map +1 -0
  672. package/dist/resolution/frameworks/laravel.js +257 -0
  673. package/dist/resolution/frameworks/laravel.js.map +1 -0
  674. package/dist/resolution/frameworks/nestjs.d.ts +26 -0
  675. package/dist/resolution/frameworks/nestjs.d.ts.map +1 -0
  676. package/dist/resolution/frameworks/nestjs.js +698 -0
  677. package/dist/resolution/frameworks/nestjs.js.map +1 -0
  678. package/dist/resolution/frameworks/play.d.ts +19 -0
  679. package/dist/resolution/frameworks/play.d.ts.map +1 -0
  680. package/dist/resolution/frameworks/play.js +111 -0
  681. package/dist/resolution/frameworks/play.js.map +1 -0
  682. package/dist/resolution/frameworks/python.d.ts +10 -0
  683. package/dist/resolution/frameworks/python.d.ts.map +1 -0
  684. package/dist/resolution/frameworks/python.js +400 -0
  685. package/dist/resolution/frameworks/python.js.map +1 -0
  686. package/dist/resolution/frameworks/react-native.d.ts +3 -0
  687. package/dist/resolution/frameworks/react-native.d.ts.map +1 -0
  688. package/dist/resolution/frameworks/react-native.js +410 -0
  689. package/dist/resolution/frameworks/react-native.js.map +1 -0
  690. package/dist/resolution/frameworks/react.d.ts +8 -0
  691. package/dist/resolution/frameworks/react.d.ts.map +1 -0
  692. package/dist/resolution/frameworks/react.js +334 -0
  693. package/dist/resolution/frameworks/react.js.map +1 -0
  694. package/dist/resolution/frameworks/ruby.d.ts +8 -0
  695. package/dist/resolution/frameworks/ruby.d.ts.map +1 -0
  696. package/dist/resolution/frameworks/ruby.js +302 -0
  697. package/dist/resolution/frameworks/ruby.js.map +1 -0
  698. package/dist/resolution/frameworks/rust.d.ts +8 -0
  699. package/dist/resolution/frameworks/rust.d.ts.map +1 -0
  700. package/dist/resolution/frameworks/rust.js +304 -0
  701. package/dist/resolution/frameworks/rust.js.map +1 -0
  702. package/dist/resolution/frameworks/svelte.d.ts +9 -0
  703. package/dist/resolution/frameworks/svelte.d.ts.map +1 -0
  704. package/dist/resolution/frameworks/svelte.js +253 -0
  705. package/dist/resolution/frameworks/svelte.js.map +1 -0
  706. package/dist/resolution/frameworks/swift-objc.d.ts +37 -0
  707. package/dist/resolution/frameworks/swift-objc.d.ts.map +1 -0
  708. package/dist/resolution/frameworks/swift-objc.js +252 -0
  709. package/dist/resolution/frameworks/swift-objc.js.map +1 -0
  710. package/dist/resolution/frameworks/swift.d.ts +10 -0
  711. package/dist/resolution/frameworks/swift.d.ts.map +1 -0
  712. package/dist/resolution/frameworks/swift.js +400 -0
  713. package/dist/resolution/frameworks/swift.js.map +1 -0
  714. package/dist/resolution/frameworks/vue.d.ts +9 -0
  715. package/dist/resolution/frameworks/vue.d.ts.map +1 -0
  716. package/dist/resolution/frameworks/vue.js +303 -0
  717. package/dist/resolution/frameworks/vue.js.map +1 -0
  718. package/dist/resolution/go-module.d.ts +26 -0
  719. package/dist/resolution/go-module.d.ts.map +1 -0
  720. package/dist/resolution/go-module.js +78 -0
  721. package/dist/resolution/go-module.js.map +1 -0
  722. package/dist/resolution/goframe-synthesizer.d.ts +28 -0
  723. package/dist/resolution/goframe-synthesizer.d.ts.map +1 -0
  724. package/dist/resolution/goframe-synthesizer.js +158 -0
  725. package/dist/resolution/goframe-synthesizer.js.map +1 -0
  726. package/dist/resolution/import-resolver.d.ts +78 -0
  727. package/dist/resolution/import-resolver.d.ts.map +1 -0
  728. package/dist/resolution/import-resolver.js +1849 -0
  729. package/dist/resolution/import-resolver.js.map +1 -0
  730. package/dist/resolution/index.d.ts +196 -0
  731. package/dist/resolution/index.d.ts.map +1 -0
  732. package/dist/resolution/index.js +1328 -0
  733. package/dist/resolution/index.js.map +1 -0
  734. package/dist/resolution/lru-cache.d.ts +24 -0
  735. package/dist/resolution/lru-cache.d.ts.map +1 -0
  736. package/dist/resolution/lru-cache.js +62 -0
  737. package/dist/resolution/lru-cache.js.map +1 -0
  738. package/dist/resolution/name-matcher.d.ts +93 -0
  739. package/dist/resolution/name-matcher.d.ts.map +1 -0
  740. package/dist/resolution/name-matcher.js +1212 -0
  741. package/dist/resolution/name-matcher.js.map +1 -0
  742. package/dist/resolution/path-aliases.d.ts +68 -0
  743. package/dist/resolution/path-aliases.d.ts.map +1 -0
  744. package/dist/resolution/path-aliases.js +238 -0
  745. package/dist/resolution/path-aliases.js.map +1 -0
  746. package/dist/resolution/strip-comments.d.ts +27 -0
  747. package/dist/resolution/strip-comments.d.ts.map +1 -0
  748. package/dist/resolution/strip-comments.js +443 -0
  749. package/dist/resolution/strip-comments.js.map +1 -0
  750. package/dist/resolution/swift-objc-bridge.d.ts +134 -0
  751. package/dist/resolution/swift-objc-bridge.d.ts.map +1 -0
  752. package/dist/resolution/swift-objc-bridge.js +256 -0
  753. package/dist/resolution/swift-objc-bridge.js.map +1 -0
  754. package/dist/resolution/types.d.ts +233 -0
  755. package/dist/resolution/types.d.ts.map +1 -0
  756. package/dist/resolution/types.js +8 -0
  757. package/dist/resolution/types.js.map +1 -0
  758. package/dist/resolution/workspace-packages.d.ts +48 -0
  759. package/dist/resolution/workspace-packages.d.ts.map +1 -0
  760. package/dist/resolution/workspace-packages.js +208 -0
  761. package/dist/resolution/workspace-packages.js.map +1 -0
  762. package/dist/search/query-parser.d.ts +57 -0
  763. package/dist/search/query-parser.d.ts.map +1 -0
  764. package/dist/search/query-parser.js +177 -0
  765. package/dist/search/query-parser.js.map +1 -0
  766. package/dist/search/query-utils.d.ts +87 -0
  767. package/dist/search/query-utils.d.ts.map +1 -0
  768. package/dist/search/query-utils.js +449 -0
  769. package/dist/search/query-utils.js.map +1 -0
  770. package/dist/sync/git-hooks.d.ts +45 -0
  771. package/dist/sync/git-hooks.d.ts.map +1 -0
  772. package/dist/sync/git-hooks.js +225 -0
  773. package/dist/sync/git-hooks.js.map +1 -0
  774. package/dist/sync/index.d.ts +19 -0
  775. package/dist/sync/index.d.ts.map +1 -0
  776. package/dist/sync/index.js +35 -0
  777. package/dist/sync/index.js.map +1 -0
  778. package/dist/sync/watch-policy.d.ts +48 -0
  779. package/dist/sync/watch-policy.d.ts.map +1 -0
  780. package/dist/sync/watch-policy.js +124 -0
  781. package/dist/sync/watch-policy.js.map +1 -0
  782. package/dist/sync/watcher.d.ts +350 -0
  783. package/dist/sync/watcher.d.ts.map +1 -0
  784. package/dist/sync/watcher.js +811 -0
  785. package/dist/sync/watcher.js.map +1 -0
  786. package/dist/sync/worktree.d.ts +54 -0
  787. package/dist/sync/worktree.d.ts.map +1 -0
  788. package/dist/sync/worktree.js +137 -0
  789. package/dist/sync/worktree.js.map +1 -0
  790. package/dist/telemetry/index.d.ts +143 -0
  791. package/dist/telemetry/index.d.ts.map +1 -0
  792. package/dist/telemetry/index.js +541 -0
  793. package/dist/telemetry/index.js.map +1 -0
  794. package/dist/timeline/bash-semantics.d.ts +52 -0
  795. package/dist/timeline/bash-semantics.d.ts.map +1 -0
  796. package/dist/timeline/bash-semantics.js +376 -0
  797. package/dist/timeline/bash-semantics.js.map +1 -0
  798. package/dist/timeline/cli.d.ts +50 -0
  799. package/dist/timeline/cli.d.ts.map +1 -0
  800. package/dist/timeline/cli.js +367 -0
  801. package/dist/timeline/cli.js.map +1 -0
  802. package/dist/timeline/constants.d.ts +62 -0
  803. package/dist/timeline/constants.d.ts.map +1 -0
  804. package/dist/timeline/constants.js +73 -0
  805. package/dist/timeline/constants.js.map +1 -0
  806. package/dist/timeline/errors.d.ts +27 -0
  807. package/dist/timeline/errors.d.ts.map +1 -0
  808. package/dist/timeline/errors.js +51 -0
  809. package/dist/timeline/errors.js.map +1 -0
  810. package/dist/timeline/hook-runner.d.ts +36 -0
  811. package/dist/timeline/hook-runner.d.ts.map +1 -0
  812. package/dist/timeline/hook-runner.js +61 -0
  813. package/dist/timeline/hook-runner.js.map +1 -0
  814. package/dist/timeline/hooks.d.ts +45 -0
  815. package/dist/timeline/hooks.d.ts.map +1 -0
  816. package/dist/timeline/hooks.js +364 -0
  817. package/dist/timeline/hooks.js.map +1 -0
  818. package/dist/timeline/index.d.ts +12 -0
  819. package/dist/timeline/index.d.ts.map +1 -0
  820. package/dist/timeline/index.js +28 -0
  821. package/dist/timeline/index.js.map +1 -0
  822. package/dist/timeline/indexer.d.ts +37 -0
  823. package/dist/timeline/indexer.d.ts.map +1 -0
  824. package/dist/timeline/indexer.js +76 -0
  825. package/dist/timeline/indexer.js.map +1 -0
  826. package/dist/timeline/installer.d.ts +33 -0
  827. package/dist/timeline/installer.d.ts.map +1 -0
  828. package/dist/timeline/installer.js +255 -0
  829. package/dist/timeline/installer.js.map +1 -0
  830. package/dist/timeline/payload.d.ts +31 -0
  831. package/dist/timeline/payload.d.ts.map +1 -0
  832. package/dist/timeline/payload.js +58 -0
  833. package/dist/timeline/payload.js.map +1 -0
  834. package/dist/timeline/post-tool-summary.d.ts +29 -0
  835. package/dist/timeline/post-tool-summary.d.ts.map +1 -0
  836. package/dist/timeline/post-tool-summary.js +190 -0
  837. package/dist/timeline/post-tool-summary.js.map +1 -0
  838. package/dist/timeline/recorder.d.ts +36 -0
  839. package/dist/timeline/recorder.d.ts.map +1 -0
  840. package/dist/timeline/recorder.js +42 -0
  841. package/dist/timeline/recorder.js.map +1 -0
  842. package/dist/timeline/schema-bootstrap.d.ts +42 -0
  843. package/dist/timeline/schema-bootstrap.d.ts.map +1 -0
  844. package/dist/timeline/schema-bootstrap.js +81 -0
  845. package/dist/timeline/schema-bootstrap.js.map +1 -0
  846. package/dist/timeline/schema.sql +37 -0
  847. package/dist/timeline/store.d.ts +69 -0
  848. package/dist/timeline/store.d.ts.map +1 -0
  849. package/dist/timeline/store.js +429 -0
  850. package/dist/timeline/store.js.map +1 -0
  851. package/dist/timeline/types.d.ts +78 -0
  852. package/dist/timeline/types.d.ts.map +1 -0
  853. package/dist/timeline/types.js +9 -0
  854. package/dist/timeline/types.js.map +1 -0
  855. package/dist/types.d.ts +392 -0
  856. package/dist/types.d.ts.map +1 -0
  857. package/dist/types.js +81 -0
  858. package/dist/types.js.map +1 -0
  859. package/dist/ui/glyphs.d.ts +42 -0
  860. package/dist/ui/glyphs.d.ts.map +1 -0
  861. package/dist/ui/glyphs.js +78 -0
  862. package/dist/ui/glyphs.js.map +1 -0
  863. package/dist/ui/shimmer-progress.d.ts +11 -0
  864. package/dist/ui/shimmer-progress.d.ts.map +1 -0
  865. package/dist/ui/shimmer-progress.js +90 -0
  866. package/dist/ui/shimmer-progress.js.map +1 -0
  867. package/dist/ui/shimmer-worker.d.ts +2 -0
  868. package/dist/ui/shimmer-worker.d.ts.map +1 -0
  869. package/dist/ui/shimmer-worker.js +118 -0
  870. package/dist/ui/shimmer-worker.js.map +1 -0
  871. package/dist/ui/types.d.ts +17 -0
  872. package/dist/ui/types.d.ts.map +1 -0
  873. package/dist/ui/types.js +3 -0
  874. package/dist/ui/types.js.map +1 -0
  875. package/dist/upgrade/index.d.ts +132 -0
  876. package/dist/upgrade/index.d.ts.map +1 -0
  877. package/dist/upgrade/index.js +498 -0
  878. package/dist/upgrade/index.js.map +1 -0
  879. package/dist/utils.d.ts +224 -0
  880. package/dist/utils.d.ts.map +1 -0
  881. package/dist/utils.js +583 -0
  882. package/dist/utils.js.map +1 -0
  883. package/package.json +60 -0
  884. package/scripts/add-lang/bench.sh +60 -0
  885. package/scripts/add-lang/check-grammar.mjs +75 -0
  886. package/scripts/add-lang/dump-ast.mjs +103 -0
  887. package/scripts/add-lang/verify-extraction.mjs +70 -0
  888. package/scripts/agent-eval/ab-adoption.sh +91 -0
  889. package/scripts/agent-eval/ab-hook.sh +86 -0
  890. package/scripts/agent-eval/ab-impl.sh +78 -0
  891. package/scripts/agent-eval/ab-new-vs-baseline.sh +102 -0
  892. package/scripts/agent-eval/ab-sufficiency.sh +78 -0
  893. package/scripts/agent-eval/arms-F.sh +21 -0
  894. package/scripts/agent-eval/arms-matrix.sh +37 -0
  895. package/scripts/agent-eval/audit.sh +68 -0
  896. package/scripts/agent-eval/bench-readme.sh +28 -0
  897. package/scripts/agent-eval/bench-why-repo.sh +22 -0
  898. package/scripts/agent-eval/block-read-hook.sh +19 -0
  899. package/scripts/agent-eval/hook-settings.json +15 -0
  900. package/scripts/agent-eval/itrun.sh +120 -0
  901. package/scripts/agent-eval/offload-eval-3arm.sh +72 -0
  902. package/scripts/agent-eval/offload-eval-cost.mjs +133 -0
  903. package/scripts/agent-eval/offload-eval-effort.mjs +108 -0
  904. package/scripts/agent-eval/offload-eval-frontload-matrix.sh +25 -0
  905. package/scripts/agent-eval/offload-eval-frontload.sh +47 -0
  906. package/scripts/agent-eval/offload-eval-ground-truth.json +18 -0
  907. package/scripts/agent-eval/offload-eval-hook.mjs +84 -0
  908. package/scripts/agent-eval/offload-eval-judge.mjs +103 -0
  909. package/scripts/agent-eval/offload-eval-matrix.sh +20 -0
  910. package/scripts/agent-eval/offload-eval-metrics.mjs +94 -0
  911. package/scripts/agent-eval/offload-eval-refs1.sh +50 -0
  912. package/scripts/agent-eval/offload-eval-setup.sh +24 -0
  913. package/scripts/agent-eval/offload-eval-styles.sh +72 -0
  914. package/scripts/agent-eval/offload-eval-summarize.mjs +68 -0
  915. package/scripts/agent-eval/offload-eval.md +76 -0
  916. package/scripts/agent-eval/parse-arms.mjs +116 -0
  917. package/scripts/agent-eval/parse-bench-readme.mjs +84 -0
  918. package/scripts/agent-eval/parse-run.mjs +45 -0
  919. package/scripts/agent-eval/parse-session.mjs +93 -0
  920. package/scripts/agent-eval/probe-context.mjs +21 -0
  921. package/scripts/agent-eval/probe-explore.mjs +40 -0
  922. package/scripts/agent-eval/probe-node.mjs +20 -0
  923. package/scripts/agent-eval/probe-sweep.mjs +119 -0
  924. package/scripts/agent-eval/probe-trace.mjs +20 -0
  925. package/scripts/agent-eval/redirect-read-hook.sh +38 -0
  926. package/scripts/agent-eval/run-agent.sh +34 -0
  927. package/scripts/agent-eval/run-all.sh +69 -0
  928. package/scripts/agent-eval/run-arms.sh +56 -0
  929. package/scripts/agent-eval/seq-matrix.mjs +137 -0
  930. package/scripts/build-bundle.sh +118 -0
  931. package/scripts/extract-release-notes.mjs +130 -0
  932. package/scripts/local-install.sh +41 -0
  933. package/scripts/npm-sdk.js +75 -0
  934. package/scripts/npm-shim.js +246 -0
  935. package/scripts/pack-npm.sh +118 -0
  936. package/scripts/prepare-release.mjs +270 -0
@@ -0,0 +1,4122 @@
1
+ "use strict";
2
+ /**
3
+ * MCP Tool Definitions
4
+ *
5
+ * Defines the tools exposed by the CodeGraph MCP server.
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.ToolHandler = exports.tools = exports.PathRefusalError = exports.NotIndexedError = void 0;
9
+ exports.getExploreBudget = getExploreBudget;
10
+ exports.getExploreOutputBudget = getExploreOutputBudget;
11
+ exports.formatStaleBanner = formatStaleBanner;
12
+ exports.formatStaleFooter = formatStaleFooter;
13
+ exports.formatDegradedBanner = formatDegradedBanner;
14
+ exports.getStaticTools = getStaticTools;
15
+ const directory_1 = require("../directory");
16
+ // Lazy-load the heavy CodeGraph chain off the MCP startup path — see the same
17
+ // helper in engine.ts. ToolHandler must load to answer tools/list (static
18
+ // schemas), but it must NOT drag in sqlite/query layers before the daemon binds;
19
+ // CodeGraph is pulled in only when a tool actually opens a project. require() is
20
+ // sync + cached (CommonJS build).
21
+ const loadCodeGraph = () => require('../index').default;
22
+ const worktree_1 = require("../sync/worktree");
23
+ const query_utils_1 = require("../search/query-utils");
24
+ const fs_1 = require("fs");
25
+ const utils_1 = require("../utils");
26
+ const generated_detection_1 = require("../extraction/generated-detection");
27
+ const dynamic_boundaries_1 = require("./dynamic-boundaries");
28
+ /**
29
+ * An expected, recoverable "cgraphx can't serve this" condition — most
30
+ * importantly a project with no index. The dispatch catch converts these to
31
+ * SUCCESS-shaped responses (guidance text, NO isError): an `isError: true`
32
+ * early in a session teaches the agent the toolset is broken and it stops
33
+ * calling cgraphx entirely (observed repeatedly), which is exactly wrong
34
+ * for conditions the agent can simply work around (use built-in tools for
35
+ * that codebase / pass projectPath). isError is reserved for "stop trying"
36
+ * cases: security refusals ({@link PathRefusalError}) and genuine
37
+ * malfunctions.
38
+ */
39
+ class NotIndexedError extends Error {
40
+ }
41
+ exports.NotIndexedError = NotIndexedError;
42
+ /**
43
+ * A security refusal (sensitive system path). Stays `isError: true` WITHOUT
44
+ * retry guidance — abandoning this path is the desired agent reaction.
45
+ */
46
+ class PathRefusalError extends Error {
47
+ }
48
+ exports.PathRefusalError = PathRefusalError;
49
+ const path_1 = require("path");
50
+ /** Maximum output length to prevent context bloat (characters) */
51
+ const MAX_OUTPUT_LENGTH = 15000;
52
+ /**
53
+ * Maximum length for free-form string inputs (query, task, symbol).
54
+ * Bounds memory and CPU when a buggy or hostile MCP client sends a
55
+ * huge payload — without this an attacker could ship a 100MB string
56
+ * and force a full FTS5 scan / OOM the server. 10 000 characters is
57
+ * far beyond any realistic legitimate query.
58
+ */
59
+ const MAX_INPUT_LENGTH = 10_000;
60
+ /**
61
+ * Maximum length for path-like string inputs (projectPath, path
62
+ * filter, glob pattern). Paths beyond a few thousand chars are
63
+ * never legitimate and signal abuse or a bug upstream.
64
+ */
65
+ const MAX_PATH_LENGTH = 4_096;
66
+ /**
67
+ * Rust path roots that have no file-system equivalent — `crate` is the
68
+ * current crate, `super` is the parent module, `self` is the current
69
+ * module. Used by `matchesSymbol` to strip these before file-path
70
+ * matching so `crate::configurator::stage_apply::run` resolves the
71
+ * same as `configurator::stage_apply::run`.
72
+ */
73
+ const RUST_PATH_PREFIXES = new Set(['crate', 'super', 'self']);
74
+ /**
75
+ * Node kinds that contain other symbols. For these, `codegraph_node` with
76
+ * `includeCode=true` returns a structural outline (member names + signatures
77
+ * + line numbers) instead of the full body, which for a large class is a
78
+ * multi-thousand-character wall of source that bloats the agent's context.
79
+ */
80
+ const CONTAINER_NODE_KINDS = new Set([
81
+ 'class', 'struct', 'interface', 'trait', 'protocol', 'enum', 'namespace', 'module',
82
+ ]);
83
+ /** Last `::` / `.` / `/`-separated segment of a qualified symbol. */
84
+ function lastQualifierPart(symbol) {
85
+ const parts = symbol.split(/::|[./]/).filter((p) => p.length > 0);
86
+ return parts[parts.length - 1] ?? symbol;
87
+ }
88
+ /**
89
+ * Calculate the recommended number of codegraph_explore calls based on project size.
90
+ * Larger codebases need more exploration calls to cover their surface area,
91
+ * but smaller ones should use fewer to avoid unnecessary overhead.
92
+ */
93
+ function getExploreBudget(fileCount) {
94
+ if (fileCount < 500)
95
+ return 1;
96
+ if (fileCount < 5000)
97
+ return 2;
98
+ if (fileCount < 15000)
99
+ return 3;
100
+ if (fileCount < 25000)
101
+ return 4;
102
+ return 5;
103
+ }
104
+ function getExploreOutputBudget(fileCount) {
105
+ // Tiered budget, scaled to project size. The budget is a CEILING (relevance
106
+ // still gates WHAT is included), and it MUST stay under the agent's INLINE
107
+ // tool-result cap (~25K chars). Above that, the host externalizes the result
108
+ // to a file the agent then Reads back — re-introducing a read AND the
109
+ // cache-write cost — which is exactly what a 35K vscode explore did in the
110
+ // n=4 README A/B. So even large repos cap at ~24K: the answer is the handful
111
+ // of ~100-line flow windows the agent would have grep-located and read (it
112
+ // natively reads ~6–9 files, median 100-line ranges), NOT a sprawl of 12
113
+ // files. Concentration onto the flow emerges from this cap + the named-file-
114
+ // first sort dropping peripheral files. Invariant: a larger tier must never
115
+ // get a smaller `maxCharsPerFile` than a smaller tier.
116
+ if (fileCount < 150) {
117
+ return {
118
+ // ITER3: revert iter2's aggressive body shrink (forced Read fallback —
119
+ // the per-file 2.5K cap pushed the agent to Read instead of node).
120
+ // Back to the iter1 shape (13K/4/3.8K) but keep the test-file
121
+ // hard-exclude. The cost lever for this tier lives in steering the
122
+ // agent to stop after 1-2 calls, not in this budget.
123
+ maxOutputChars: 13000,
124
+ defaultMaxFiles: 4,
125
+ maxCharsPerFile: 3800,
126
+ gapThreshold: 7,
127
+ maxSymbolsInFileHeader: 5,
128
+ maxEdgesPerRelationshipKind: 4,
129
+ includeRelationships: false,
130
+ includeAdditionalFiles: false,
131
+ includeCompletenessSignal: false,
132
+ includeBudgetNote: false,
133
+ excludeLowValueFiles: true,
134
+ };
135
+ }
136
+ if (fileCount < 500) {
137
+ return {
138
+ // ITER3: same revert/keep-filter pattern as <150.
139
+ maxOutputChars: 18000,
140
+ defaultMaxFiles: 5,
141
+ maxCharsPerFile: 3800,
142
+ gapThreshold: 8,
143
+ maxSymbolsInFileHeader: 6,
144
+ maxEdgesPerRelationshipKind: 6,
145
+ includeRelationships: false,
146
+ includeAdditionalFiles: false,
147
+ includeCompletenessSignal: false,
148
+ includeBudgetNote: false,
149
+ excludeLowValueFiles: true,
150
+ };
151
+ }
152
+ if (fileCount < 5000) {
153
+ return {
154
+ // ~150-line per-file window (the native read unit) × ~6 files, capped at
155
+ // the ~24K inline ceiling so the response is never externalized. Per-file
156
+ // stays ≥ the <500 tier (3800) — monotonic.
157
+ maxOutputChars: 24000,
158
+ defaultMaxFiles: 8,
159
+ maxCharsPerFile: 6500,
160
+ gapThreshold: 12,
161
+ maxSymbolsInFileHeader: 10,
162
+ maxEdgesPerRelationshipKind: 10,
163
+ includeRelationships: true,
164
+ includeAdditionalFiles: true,
165
+ includeCompletenessSignal: true,
166
+ includeBudgetNote: true,
167
+ excludeLowValueFiles: false,
168
+ };
169
+ }
170
+ // Large + very-large repos: SAME ~24K inline ceiling (a bigger response just
171
+ // externalizes — see vscode). More files indexed → more CALLS via
172
+ // getExploreBudget, not a bigger single response. Per-file 7000 (≥ smaller
173
+ // tiers) gives the central file a ~180-line orientation window.
174
+ if (fileCount < 15000) {
175
+ return {
176
+ maxOutputChars: 24000,
177
+ defaultMaxFiles: 8,
178
+ maxCharsPerFile: 7000,
179
+ gapThreshold: 15,
180
+ maxSymbolsInFileHeader: 15,
181
+ maxEdgesPerRelationshipKind: 15,
182
+ includeRelationships: true,
183
+ includeAdditionalFiles: true,
184
+ includeCompletenessSignal: true,
185
+ includeBudgetNote: true,
186
+ excludeLowValueFiles: false,
187
+ };
188
+ }
189
+ return {
190
+ maxOutputChars: 24000,
191
+ defaultMaxFiles: 8,
192
+ maxCharsPerFile: 7000,
193
+ gapThreshold: 15,
194
+ maxSymbolsInFileHeader: 15,
195
+ maxEdgesPerRelationshipKind: 15,
196
+ includeRelationships: true,
197
+ includeAdditionalFiles: true,
198
+ includeCompletenessSignal: true,
199
+ includeBudgetNote: true,
200
+ excludeLowValueFiles: false,
201
+ };
202
+ }
203
+ /**
204
+ * Whether `codegraph_explore` should prefix source lines with their line
205
+ * numbers (cat -n style: `<num>\t<code>`).
206
+ *
207
+ * Line numbers let the agent cite `file:line` straight from the explore
208
+ * payload instead of re-Reading the file just to find a line number — the
209
+ * dominant residual cost on precise-tracing questions (#185 follow-up).
210
+ *
211
+ * Defaults ON. Set `CGRAPHX_EXPLORE_LINENUMS=0` to disable (used by the
212
+ * A/B harness to measure the payload-cost vs. read-savings tradeoff).
213
+ */
214
+ function exploreLineNumbersEnabled() {
215
+ return process.env.CGRAPHX_EXPLORE_LINENUMS !== '0';
216
+ }
217
+ /**
218
+ * Adaptive explore sizing (default ON). `codegraph_explore` skeletonizes OFF-SPINE
219
+ * polymorphic-sibling files — a file whose class is one of ≥3 interchangeable
220
+ * implementations of a shared interface (e.g. OkHttp's `: Interceptor` classes) —
221
+ * to class + member signatures (bodies elided), keeping the on-spine exemplar full.
222
+ * This sizes the response to the answer instead of the budget cap on sibling-heavy
223
+ * flows (OkHttp interceptor-chain explore 28.5k→16.6k, ~28% cheaper than native
224
+ * search, reads flat). It is PROVABLY INERT elsewhere: distinct pipeline steps (no
225
+ * ≥3-implementer supertype, e.g. Excalidraw's `renderStaticScene`) and on-spine
226
+ * files keep full source — output is byte-identical to shipped on excalidraw /
227
+ * tokio / django / vscode / gin. Set `CGRAPHX_ADAPTIVE_EXPLORE=0` to disable.
228
+ */
229
+ function adaptiveExploreEnabled() {
230
+ return process.env.CGRAPHX_ADAPTIVE_EXPLORE !== '0' && process.env.CGRAPHX_ADAPTIVE_EXPLORE !== 'false';
231
+ }
232
+ /**
233
+ * How long the FIRST tool call waits on the post-open catch-up reconcile before
234
+ * giving up and serving anyway (issue #905). On a normal repo the reconcile
235
+ * finishes in well under this, so the gate is fully honored and nothing changes.
236
+ * On a very large repo (~100k files) the reconcile takes minutes — blocking the
237
+ * first call on all of it presents as a multi-minute hang — so we wait briefly
238
+ * for a clean answer, then serve and let the reconcile finish in the background
239
+ * (it yields to the event loop, so a concurrent read still runs).
240
+ *
241
+ * `CGRAPHX_CATCHUP_GATE_TIMEOUT_MS` overrides the default; `0` restores the
242
+ * old unbounded-wait behavior (always block until the reconcile completes).
243
+ */
244
+ const DEFAULT_CATCHUP_GATE_TIMEOUT_MS = 3000;
245
+ function resolveCatchUpGateTimeoutMs() {
246
+ const raw = process.env.CGRAPHX_CATCHUP_GATE_TIMEOUT_MS;
247
+ if (raw === undefined || raw === '')
248
+ return DEFAULT_CATCHUP_GATE_TIMEOUT_MS;
249
+ const n = Number(raw);
250
+ if (!Number.isFinite(n) || n < 0)
251
+ return DEFAULT_CATCHUP_GATE_TIMEOUT_MS;
252
+ return Math.floor(n);
253
+ }
254
+ /**
255
+ * Prefix each line of a source slice with its 1-based line number, matching
256
+ * the Read tool's `cat -n` convention (number + tab) so the agent treats it
257
+ * the same way it treats Read output.
258
+ *
259
+ * @param slice contiguous source text (already extracted from the file)
260
+ * @param firstLineNumber the 1-based line number of the slice's first line
261
+ */
262
+ function numberSourceLines(slice, firstLineNumber) {
263
+ const out = [];
264
+ const split = slice.split('\n');
265
+ for (let i = 0; i < split.length; i++) {
266
+ out.push(`${firstLineNumber + i}\t${split[i]}`);
267
+ }
268
+ return out.join('\n');
269
+ }
270
+ /**
271
+ * Unique line-prefix for a per-file source section in codegraph_explore output.
272
+ * Issue #778: tool results dropped ATX headings (`####`, `##`, `###`) for bold
273
+ * labels so Markdown-rendering MCP clients (e.g. the Claude Code VSCode
274
+ * extension) stop blowing every header up to H1–H4. The path is bold + a code
275
+ * span so it still reads as a header, and the leading ``**` `` stays a UNIQUE,
276
+ * greppable marker — no other explore line begins with it — that the explore
277
+ * truncation boundary (`handleExplore`) and the offload chunker
278
+ * (`reasoning/reasoner.ts`) both key off to cut on whole file sections.
279
+ */
280
+ const FILE_SECTION_PREFIX = '**`';
281
+ function fileSectionHeader(filePath, suffix) {
282
+ return suffix
283
+ ? `${FILE_SECTION_PREFIX}${filePath}\`** — ${suffix}`
284
+ : `${FILE_SECTION_PREFIX}${filePath}\`**`;
285
+ }
286
+ /**
287
+ * Per-file staleness banner emitted at the top of a tool response when the
288
+ * file watcher has pending events for files referenced by the response.
289
+ * The agent uses this to fall back to Read for those specific files
290
+ * without waiting for the debounced sync (issue #403).
291
+ */
292
+ function formatStaleBanner(stale) {
293
+ const now = Date.now();
294
+ const lines = stale.map((p) => {
295
+ const ageMs = Math.max(0, now - p.lastSeenMs);
296
+ const label = p.indexing ? 'indexing in progress' : 'pending sync';
297
+ return ` - ${p.path} (edited ${ageMs}ms ago, ${label})`;
298
+ });
299
+ return ('⚠️ Some files referenced below were edited since the last index sync — ' +
300
+ 'their cgraphx entries may be stale:\n' +
301
+ lines.join('\n') +
302
+ '\nFor accurate content of those specific files, Read them directly. ' +
303
+ 'The rest of this response is fresh.');
304
+ }
305
+ /**
306
+ * Compact footer listing pending files that are NOT referenced in this
307
+ * response. Gives the agent a complete project-wide freshness picture
308
+ * without bloating the main banner.
309
+ */
310
+ function formatStaleFooter(stale) {
311
+ const MAX = 5;
312
+ const now = Date.now();
313
+ const shown = stale.slice(0, MAX);
314
+ const lines = shown.map((p) => {
315
+ const ageMs = Math.max(0, now - p.lastSeenMs);
316
+ return ` - ${p.path} (edited ${ageMs}ms ago)`;
317
+ });
318
+ const more = stale.length > MAX ? `\n - …and ${stale.length - MAX} more` : '';
319
+ return (`(Note: ${stale.length} file(s) elsewhere in this project are pending index ` +
320
+ `sync but were not referenced above:\n${lines.join('\n')}${more})`);
321
+ }
322
+ /**
323
+ * Whole-index degradation banner (issue #876). Emitted at the top of a read
324
+ * tool response when live watching has permanently stopped — at which point
325
+ * `getPendingFiles()` is empty, so the per-file banner above can't fire even
326
+ * though the index is now FROZEN and silently drifting stale. Leads with the
327
+ * agent-actionable instruction (Read directly) and carries the reason, which
328
+ * already names the operator remedy (`cgraphx sync` / git hooks).
329
+ */
330
+ function formatDegradedBanner(reason) {
331
+ return ('⚠️ cgraphx auto-sync is DISABLED — live file watching stopped, so the index is ' +
332
+ 'frozen and any file edited since then is stale here. Read files directly to confirm ' +
333
+ 'current content before relying on it.' +
334
+ (reason ? `\n Reason: ${reason}` : ''));
335
+ }
336
+ /**
337
+ * Common projectPath property for cross-project queries
338
+ */
339
+ const projectPathProperty = {
340
+ type: 'string',
341
+ description: 'Absolute path to the project to query (or any directory inside it) — codegraph uses the nearest .cgraphx/ index at or above that path. Omit to use this session\'s default project. Pass it to query a second codebase, or when the server root has no index of its own (e.g. a monorepo where only sub-projects are indexed, so there is no default project).',
342
+ };
343
+ /**
344
+ * All CodeGraph MCP tools
345
+ *
346
+ * Designed for minimal context usage - use codegraph_explore as the primary tool
347
+ * (one call usually answers the whole question), and only use other tools for
348
+ * targeted follow-up queries.
349
+ *
350
+ * All tools support cross-project queries via the optional `projectPath` parameter.
351
+ */
352
+ /**
353
+ * All CodeGraph MCP tools
354
+ *
355
+ * Designed for minimal context usage - use codegraph_explore as the primary tool
356
+ * (one call usually answers the whole question), and only use other tools for
357
+ * targeted follow-up queries.
358
+ *
359
+ * All tools support cross-project queries via the optional `projectPath` parameter.
360
+ *
361
+ * The markdown knowledge base (`cgraphx docs ...`) and dbquery (`cgraphx db ...`)
362
+ * subsystems are CLI-only — they're not registered as MCP tools. Agents reach them
363
+ * through the `code-impact-api` / `db-query` skills (see assets/claude-template/).
364
+ */
365
+ exports.tools = [
366
+ {
367
+ name: 'codegraph_search',
368
+ description: 'Quick symbol search by name. Returns locations only (no code). Use codegraph_explore instead to get the actual source / understand an area in one call.',
369
+ inputSchema: {
370
+ type: 'object',
371
+ properties: {
372
+ query: {
373
+ type: 'string',
374
+ description: 'Symbol name or partial name (e.g., "auth", "signIn", "UserService")',
375
+ },
376
+ kind: {
377
+ type: 'string',
378
+ description: 'Filter by node kind',
379
+ enum: ['function', 'method', 'class', 'interface', 'type', 'variable', 'route', 'component'],
380
+ },
381
+ limit: {
382
+ type: 'number',
383
+ description: 'Maximum results (default: 10)',
384
+ default: 10,
385
+ },
386
+ projectPath: projectPathProperty,
387
+ },
388
+ required: ['query'],
389
+ },
390
+ },
391
+ {
392
+ name: 'codegraph_callers',
393
+ description: 'List functions that call <symbol>. For the full flow, use codegraph_explore.',
394
+ inputSchema: {
395
+ type: 'object',
396
+ properties: {
397
+ symbol: {
398
+ type: 'string',
399
+ description: 'Name of the function, method, or class to find callers for',
400
+ },
401
+ file: {
402
+ type: 'string',
403
+ description: 'Narrow to the definition in this file (path or suffix) when several same-named symbols exist (e.g. one UserService per app in a monorepo)',
404
+ },
405
+ limit: {
406
+ type: 'number',
407
+ description: 'Maximum number of callers to return (default: 20)',
408
+ default: 20,
409
+ },
410
+ projectPath: projectPathProperty,
411
+ },
412
+ required: ['symbol'],
413
+ },
414
+ },
415
+ {
416
+ name: 'codegraph_callees',
417
+ description: 'List functions that <symbol> calls. For the full flow, use codegraph_explore.',
418
+ inputSchema: {
419
+ type: 'object',
420
+ properties: {
421
+ symbol: {
422
+ type: 'string',
423
+ description: 'Name of the function, method, or class to find callees for',
424
+ },
425
+ file: {
426
+ type: 'string',
427
+ description: 'Narrow to the definition in this file (path or suffix) when several same-named symbols exist',
428
+ },
429
+ limit: {
430
+ type: 'number',
431
+ description: 'Maximum number of callees to return (default: 20)',
432
+ default: 20,
433
+ },
434
+ projectPath: projectPathProperty,
435
+ },
436
+ required: ['symbol'],
437
+ },
438
+ },
439
+ {
440
+ name: 'codegraph_impact',
441
+ description: 'List symbols affected by changing <symbol>. Use before a refactor.',
442
+ inputSchema: {
443
+ type: 'object',
444
+ properties: {
445
+ symbol: {
446
+ type: 'string',
447
+ description: 'Name of the symbol to analyze impact for',
448
+ },
449
+ file: {
450
+ type: 'string',
451
+ description: 'Narrow to the definition in this file (path or suffix) when several same-named symbols exist',
452
+ },
453
+ depth: {
454
+ type: 'number',
455
+ description: 'How many levels of dependencies to traverse (default: 2)',
456
+ default: 2,
457
+ },
458
+ projectPath: projectPathProperty,
459
+ },
460
+ required: ['symbol'],
461
+ },
462
+ },
463
+ {
464
+ name: 'codegraph_node',
465
+ description: 'Two modes. (1) READ A FILE — use INSTEAD of the Read tool: pass `file` (a path or basename) with no `symbol` and it returns that file\'s current on-disk source with line numbers, exactly the shape Read gives you (`<n>\\t<line>`, safe to Edit from), narrowable with `offset`/`limit` just like Read — PLUS a one-line note of which files depend on it. Same bytes as Read, faster (served from the index), with the blast radius attached. Use it whenever you would Read a source file. (2) ONE SYMBOL you can name — its location, signature, verbatim source (includeCode=true) and caller/callee trail in one call, so before changing it you see what calls it and what your edit would break. For an AMBIGUOUS name it returns EVERY matching definition\'s body in one call (so you never Read a file to find the right overload); pass `file`/`line` to pin one. Use codegraph_explore for several related symbols or the full flow.',
466
+ inputSchema: {
467
+ type: 'object',
468
+ properties: {
469
+ symbol: {
470
+ type: 'string',
471
+ description: 'Name of the symbol to read (symbol mode). Omit it and pass `file` alone to read a whole file like Read.',
472
+ },
473
+ includeCode: {
474
+ type: 'boolean',
475
+ description: 'Symbol mode: include the symbol\'s full body (default: false). Ignored in file mode, which always returns source unless `symbolsOnly` is set.',
476
+ default: false,
477
+ },
478
+ file: {
479
+ type: 'string',
480
+ description: 'A file path or basename (e.g. "harness.rs", "src/auth/session.ts"). Pass it ALONE (no symbol) to READ the file like the Read tool — its full source with line numbers + which files depend on it. Or pass it WITH a symbol to disambiguate an overloaded name to the definition in this file.',
481
+ },
482
+ offset: {
483
+ type: 'number',
484
+ description: 'File mode: 1-based line to start reading from, exactly like Read\'s offset. Defaults to the start of the file.',
485
+ },
486
+ limit: {
487
+ type: 'number',
488
+ description: 'File mode: maximum number of lines to return, exactly like Read\'s limit. Defaults to the whole file (capped at 2000 lines, like Read).',
489
+ },
490
+ symbolsOnly: {
491
+ type: 'boolean',
492
+ description: 'File mode: return just the file\'s symbol map + dependents (a cheap structural overview) instead of its source.',
493
+ default: false,
494
+ },
495
+ line: {
496
+ type: 'number',
497
+ description: 'Symbol mode only: disambiguate to the definition at/around this line (use with the file:line a trail showed you).',
498
+ },
499
+ projectPath: projectPathProperty,
500
+ },
501
+ required: [],
502
+ },
503
+ },
504
+ {
505
+ name: 'codegraph_explore',
506
+ description: 'PRIMARY TOOL — call FIRST for almost any question OR before an edit: how does X work, architecture, a bug, where/what is X, surveying an area, or the symbols you are about to change. Returns the verbatim source of the relevant symbols grouped by file in ONE capped call (Read-equivalent — treat the shown source as already Read; do NOT re-open those files), plus the call path among them. Query can be a natural-language question OR a bag of symbol/file names. Usually the ONLY call you need — more accurate context, in far fewer tokens and round-trips than a search/Read/Grep loop.',
507
+ inputSchema: {
508
+ type: 'object',
509
+ properties: {
510
+ query: {
511
+ type: 'string',
512
+ description: 'Symbol names, file names, or short code terms to explore (e.g., "AuthService loginUser session-manager", "GraphTraverser BFS impact traversal.ts"). For a flow question, name the symbols spanning the flow (e.g. "mutateElement renderScene"). A natural-language question works too — no prior codegraph_search needed.',
513
+ },
514
+ maxFiles: {
515
+ type: 'number',
516
+ description: 'Maximum number of files to include source code from (default: 12)',
517
+ default: 12,
518
+ },
519
+ projectPath: projectPathProperty,
520
+ },
521
+ required: ['query'],
522
+ },
523
+ },
524
+ {
525
+ name: 'codegraph_status',
526
+ description: 'Index health check (files / nodes / edges). Skip unless debugging.',
527
+ inputSchema: {
528
+ type: 'object',
529
+ properties: {
530
+ projectPath: projectPathProperty,
531
+ },
532
+ },
533
+ },
534
+ {
535
+ name: 'codegraph_files',
536
+ description: 'Indexed file tree with language + symbol counts. Faster than Glob for project layout.',
537
+ inputSchema: {
538
+ type: 'object',
539
+ properties: {
540
+ path: {
541
+ type: 'string',
542
+ description: 'Filter to files under this directory path (e.g., "src/components"). Returns all files if not specified.',
543
+ },
544
+ pattern: {
545
+ type: 'string',
546
+ description: 'Filter files matching this glob pattern (e.g., "*.tsx", "**/*.test.ts")',
547
+ },
548
+ format: {
549
+ type: 'string',
550
+ description: 'Output format: "tree" (hierarchical, default), "flat" (simple list), "grouped" (by language)',
551
+ enum: ['tree', 'flat', 'grouped'],
552
+ default: 'tree',
553
+ },
554
+ includeMetadata: {
555
+ type: 'boolean',
556
+ description: 'Include file metadata like language and symbol count (default: true)',
557
+ default: true,
558
+ },
559
+ maxDepth: {
560
+ type: 'number',
561
+ description: 'Maximum directory depth to show (default: unlimited)',
562
+ },
563
+ projectPath: projectPathProperty,
564
+ },
565
+ },
566
+ },
567
+ ];
568
+ // NOTE: the markdown knowledge base subsystem (codegraph_docs_*) was moved
569
+ // out of the MCP surface — it's now CLI-only (`cgraphx docs ...`) plus the
570
+ // `codegraph-docs` skill that teaches agents when to reach for it. Keeping
571
+ // MCP pared to `codegraph_explore` alone matches cgraphx's "single high-
572
+ // saliency tool" philosophy (see DEFAULT_MCP_TOOLS). The docs indexer and
573
+ // query layer in src/markdown/ are unchanged and fully usable via CLI.
574
+ /**
575
+ * Allowlist-filtered tool definitions WITHOUT an engine — the static surface the
576
+ * proxy answers `tools/list` with before any project is open. Mirrors
577
+ * `ToolHandler.getTools()` in the no-CodeGraph case (the dynamic per-repo budget
578
+ * note in a description only adds once `cg` is loaded; the schemas are static).
579
+ */
580
+ function getStaticTools() {
581
+ const raw = process.env.CGRAPHX_MCP_TOOLS;
582
+ if (!raw || !raw.trim()) {
583
+ return exports.tools.filter(t => DEFAULT_MCP_TOOLS.has(t.name.replace(/^codegraph_/, '')));
584
+ }
585
+ const allow = new Set(raw.split(',').map(s => s.trim().replace(/^codegraph_/, '')).filter(Boolean));
586
+ return allow.size ? exports.tools.filter(t => allow.has(t.name.replace(/^codegraph_/, ''))) : exports.tools;
587
+ }
588
+ /**
589
+ * The MCP tools served by DEFAULT (short names). Pared to ONLY `codegraph_explore`
590
+ * — the single tool that reliably earns its place: one capped call returns the
591
+ * verbatim source of the relevant symbols grouped by file. Every other tool is a
592
+ * narrower slice of what explore already does, and presence itself steers
593
+ * mis-picks, so they are no longer LISTED to agents.
594
+ *
595
+ * The other defined tools (`node`, `search`, `callers`, plus callees/impact/files/
596
+ * status) remain fully functional — handlers stay, the library API and CLI are
597
+ * untouched, and `CGRAPHX_MCP_TOOLS=explore,node,...` re-enables any of them.
598
+ */
599
+ const DEFAULT_MCP_TOOLS = new Set([
600
+ 'explore',
601
+ ]);
602
+ /**
603
+ * Tool handler that executes tools against a CodeGraph instance
604
+ *
605
+ * Supports cross-project queries via the projectPath parameter.
606
+ * Other projects are opened on-demand and cached for performance.
607
+ */
608
+ class ToolHandler {
609
+ cg;
610
+ // Cache of opened CodeGraph instances for cross-project queries
611
+ projectCache = new Map();
612
+ // The directory the server last searched for a default project. Surfaced in
613
+ // the "not initialized" error so users can see why detection missed.
614
+ defaultProjectHint = null;
615
+ // Per-start-path cache of the git worktree/index mismatch (issue #155). The
616
+ // mismatch is a fixed property of (where the request came from → which
617
+ // .cgraphx/ it resolves to), so the up-to-two `git rev-parse` spawns run
618
+ // once and every later tool call reuses the result — never shelling out to
619
+ // git on the hot path. `undefined` = not computed yet; `null` = no mismatch.
620
+ worktreeMismatchCache = new Map();
621
+ // Gate that the MCP engine pokes after `cg.open()` so the first tool call
622
+ // blocks on the post-open filesystem reconcile (catch-up sync). Without
623
+ // this, a tool call that races past `catchUpSync()` serves rows for files
624
+ // that were deleted (or edited) while no MCP server was running — and the
625
+ // per-file staleness banner can't help, because `getPendingFiles()` is
626
+ // populated by the watcher, not by catch-up. The wait is time-boxed
627
+ // (see {@link resolveCatchUpGateTimeoutMs}) so a minutes-long reconcile on a
628
+ // huge repo can't hang the first call (#905); cleared on first await so
629
+ // subsequent calls don't pay any cost.
630
+ catchUpGate = null;
631
+ constructor(cg) {
632
+ this.cg = cg;
633
+ }
634
+ /**
635
+ * Update the default CodeGraph instance (e.g. after lazy initialization)
636
+ */
637
+ setDefaultCodeGraph(cg) {
638
+ this.cg = cg;
639
+ }
640
+ /**
641
+ * Engine-only: register the catch-up sync promise so the next `execute()`
642
+ * call awaits it before serving. The handler swallows rejections (the
643
+ * engine logs them) so a sync failure never propagates as a tool error;
644
+ * we still want to serve a best-effort result over the same potentially-
645
+ * stale data, which is what would have happened without the gate.
646
+ */
647
+ setCatchUpGate(p) {
648
+ this.catchUpGate = p;
649
+ }
650
+ /**
651
+ * Await the catch-up gate, but no longer than the configured timeout (#905).
652
+ * If the reconcile settles first, we got the fully-reconciled answer. If the
653
+ * timeout wins, we serve the call now and let the reconcile finish in the
654
+ * background — it yields to the event loop (see SYNC_RECONCILE_YIELD_INTERVAL),
655
+ * so a concurrent read still runs against the same connection. Never throws:
656
+ * a failed reconcile is logged by the engine, and we serve best-effort over
657
+ * the same potentially-stale data the un-gated path would have.
658
+ */
659
+ async awaitCatchUpGate(gate) {
660
+ const timeoutMs = resolveCatchUpGateTimeoutMs();
661
+ if (timeoutMs <= 0) {
662
+ // 0 = opt back into the original unbounded wait.
663
+ try {
664
+ await gate;
665
+ }
666
+ catch { /* engine already logged */ }
667
+ return;
668
+ }
669
+ let timer;
670
+ const timedOut = new Promise((resolve) => {
671
+ timer = setTimeout(() => resolve('timeout'), timeoutMs);
672
+ timer.unref?.();
673
+ });
674
+ try {
675
+ const outcome = await Promise.race([
676
+ gate.then(() => 'done', () => 'done'),
677
+ timedOut,
678
+ ]);
679
+ if (outcome === 'timeout') {
680
+ process.stderr.write(`[cgraphx MCP] Catch-up reconcile still running after ${timeoutMs}ms; serving this tool call now and finishing the reconcile in the background (#905). ` +
681
+ `Set CGRAPHX_CATCHUP_GATE_TIMEOUT_MS=0 to always wait for it.\n`);
682
+ }
683
+ }
684
+ finally {
685
+ if (timer)
686
+ clearTimeout(timer);
687
+ }
688
+ }
689
+ /**
690
+ * Record the directory the server tried to resolve the default project from.
691
+ * Used only to make the "no default project" error actionable.
692
+ */
693
+ setDefaultProjectHint(searchedPath) {
694
+ this.defaultProjectHint = searchedPath;
695
+ }
696
+ /**
697
+ * Whether a default CodeGraph instance is available
698
+ */
699
+ hasDefaultCodeGraph() {
700
+ return this.cg !== null;
701
+ }
702
+ /**
703
+ * Optional allowlist of exposed tools, parsed from the CGRAPHX_MCP_TOOLS
704
+ * env var (comma-separated short names, e.g. "trace,search,node,context").
705
+ * Unset/empty → every tool is exposed. Lets an operator (or an A/B harness)
706
+ * trim the tool surface without rebuilding the client config; the ablated
707
+ * tool is then truly absent from ListTools rather than merely denied on call.
708
+ * Matching is on the short form, so "node" and "codegraph_node" both work.
709
+ */
710
+ toolAllowlist() {
711
+ const raw = process.env.CGRAPHX_MCP_TOOLS;
712
+ if (!raw || !raw.trim())
713
+ return null;
714
+ const short = (s) => s.trim().replace(/^codegraph_/, '');
715
+ const set = new Set(raw.split(',').map(short).filter(Boolean));
716
+ return set.size ? set : null;
717
+ }
718
+ /** Whether a tool name passes the CGRAPHX_MCP_TOOLS allowlist (if any). */
719
+ isToolAllowed(name) {
720
+ const allow = this.toolAllowlist();
721
+ return !allow || allow.has(name.replace(/^codegraph_/, ''));
722
+ }
723
+ /**
724
+ * Get tool definitions with dynamic descriptions based on project size.
725
+ * The codegraph_explore tool description includes a budget recommendation
726
+ * scaled to the number of indexed files. Honors the CGRAPHX_MCP_TOOLS
727
+ * allowlist so a trimmed surface is reflected in ListTools.
728
+ */
729
+ getTools() {
730
+ const allow = this.toolAllowlist();
731
+ // No explicit allowlist → the default 4-tool surface (see
732
+ // DEFAULT_MCP_TOOLS for the evidence). An allowlist replaces the
733
+ // default entirely, so any defined tool can be re-enabled.
734
+ let visible = allow
735
+ ? exports.tools.filter(t => allow.has(t.name.replace(/^codegraph_/, '')))
736
+ : exports.tools.filter(t => DEFAULT_MCP_TOOLS.has(t.name.replace(/^codegraph_/, '')));
737
+ if (!this.cg)
738
+ return visible;
739
+ try {
740
+ const stats = this.cg.getStats();
741
+ const budget = getExploreBudget(stats.fileCount);
742
+ // Tiny-repo tool gating: on projects under TINY_REPO_FILE_THRESHOLD
743
+ // files, only expose the core trio (search, node, explore) — one
744
+ // below even the 4-tool default: at this scale callers, too, reduces
745
+ // to one grep. (Historical note: the audit below ran when context and
746
+ // trace still existed; its "5 core tools" are today's trio.)
747
+ //
748
+ // n=2 audits ruled out cutting below 5 tools:
749
+ // - 3-tool gate (search + context + trace): cost regressed on
750
+ // cobra/ky/sinatra. The agent fell back to raw Reads to cover
751
+ // what codegraph_node + codegraph_explore would have answered.
752
+ // - 1-tool gate (search only): catastrophic regression — express
753
+ // went from -43% WIN to +107% LOSS. With only search, the agent
754
+ // can't navigate the call graph structurally and reads everything.
755
+ //
756
+ // 5 is the empirical lower bound. Tools beyond search/context/
757
+ // node/explore/trace pay overhead that the agent doesn't recoup
758
+ // on tiny-repo flow questions.
759
+ // ITER4: raise threshold 150 → 500 so single-file frameworks
760
+ // (sinatra at 159, slim_framework around 200) also get the
761
+ // 5-tool surface. The empirical 5-tool floor was set on <150
762
+ // probes; iter3 measurement showed sinatra is structurally the
763
+ // SAME problem as cobra (single-file WITHOUT-arm Read wins),
764
+ // so it deserves the same gating.
765
+ const TINY_REPO_FILE_THRESHOLD = 500;
766
+ const TINY_REPO_CORE_TOOLS = new Set([
767
+ 'codegraph_explore',
768
+ 'codegraph_search',
769
+ 'codegraph_node',
770
+ ]);
771
+ if (stats.fileCount < TINY_REPO_FILE_THRESHOLD) {
772
+ visible = visible.filter(t => TINY_REPO_CORE_TOOLS.has(t.name));
773
+ }
774
+ return visible.map(tool => {
775
+ if (tool.name === 'codegraph_explore') {
776
+ return {
777
+ ...tool,
778
+ description: `${tool.description} Budget: make at most ${budget} calls for this project (${stats.fileCount.toLocaleString()} files indexed).`,
779
+ };
780
+ }
781
+ return tool;
782
+ });
783
+ }
784
+ catch {
785
+ return visible;
786
+ }
787
+ }
788
+ /**
789
+ * Get CodeGraph instance for a project
790
+ *
791
+ * If projectPath is provided, opens that project's CodeGraph (cached).
792
+ * Otherwise returns the default CodeGraph instance.
793
+ *
794
+ * Walks up parent directories to find the nearest .cgraphx/ folder,
795
+ * similar to how git finds .git/ directories.
796
+ */
797
+ getCodeGraph(projectPath) {
798
+ if (!projectPath) {
799
+ if (!this.cg) {
800
+ const searched = this.defaultProjectHint ?? process.cwd();
801
+ throw new NotIndexedError('No cgraphx project is loaded for this session.\n' +
802
+ `Searched for a .cgraphx/ directory starting from: ${searched}\n` +
803
+ 'Either the server root has no index of its own (e.g. a monorepo where only ' +
804
+ "sub-projects are indexed), or the MCP client launched the server outside your " +
805
+ 'project without reporting the workspace root. Either way, target the project ' +
806
+ 'explicitly:\n' +
807
+ ' • Pass projectPath to the tool call, e.g. projectPath: "/absolute/path/to/your/project" ' +
808
+ '(any project that has a .cgraphx/ — including a sub-project of a monorepo)\n' +
809
+ ' • Or add --path to the server\'s MCP config args: ["serve", "--mcp", "--path", "/absolute/path/to/your/project"]\n' +
810
+ 'If a project simply has no index, use your built-in tools (Read/Grep/Glob) for THAT ' +
811
+ "project (the user can run 'cgraphx init' there to enable it) — you can still query " +
812
+ 'other indexed projects by projectPath in the same session.');
813
+ }
814
+ return this.freshen(this.cg);
815
+ }
816
+ // Reject sensitive system directories before opening. Only validate a
817
+ // path that actually exists — a nested or not-yet-created sub-path of a
818
+ // real project must still be allowed to resolve UP to its .cgraphx/
819
+ // root below (issue #238), so we don't run the existence-checking
820
+ // validator on paths that are meant to walk up.
821
+ if ((0, fs_1.existsSync)(projectPath)) {
822
+ const pathError = (0, utils_1.validateProjectPath)(projectPath);
823
+ if (pathError) {
824
+ throw new PathRefusalError(pathError);
825
+ }
826
+ }
827
+ // Always RE-RESOLVE the nearest .cgraphx/ from the input path. The walk
828
+ // is cheap (a few existsSync up the tree) and is the only thing that
829
+ // notices a path whose index root CHANGED since it was first seen — most
830
+ // importantly a git worktree that gained its own .cgraphx/ after the
831
+ // (long-lived) server first resolved it up to the parent checkout. We used
832
+ // to short-circuit on a `projectCache[projectPath]` entry before resolving,
833
+ // which pinned that first resolution for the server's whole lifetime, so a
834
+ // worktree kept being served the parent checkout's index until restart
835
+ // (#926). The DB connection itself is still cached (by resolved root,
836
+ // below), so re-resolving costs only the stat walk, never a reopen.
837
+ const resolvedRoot = (0, directory_1.findNearestCodeGraphRoot)(projectPath);
838
+ if (!resolvedRoot) {
839
+ throw new NotIndexedError(`The project at ${projectPath} isn't indexed with cgraphx (no .cgraphx/ directory found ` +
840
+ 'walking up from it), so cgraphx cannot query it. Use your built-in tools (Read/Grep/Glob) ' +
841
+ "for that codebase instead, and don't call cgraphx for it again this session. " +
842
+ "Indexing is the user's decision — they can run 'cgraphx init' in that project to enable it.");
843
+ }
844
+ // If the path resolves to the default project, reuse the already-open
845
+ // default instance rather than opening a SECOND connection to the same DB.
846
+ // A duplicate connection serializes reads against the watcher's auto-sync
847
+ // writes; when WAL isn't in effect (e.g. a filesystem without shared-memory
848
+ // support) that surfaces as intermittent
849
+ // "database is locked" on concurrent tool calls. See issue #238. The
850
+ // default instance is owned/closed by the server, so it's never cached.
851
+ if (this.cg && this.cg.getProjectRoot() === resolvedRoot) {
852
+ return this.freshen(this.cg);
853
+ }
854
+ // Cache the open DB connection by RESOLVED ROOT only — never by the input
855
+ // path. One key per instance means closeAll() closes each exactly once, and
856
+ // a changed resolution maps to a different entry instead of a stale hit.
857
+ const cached = this.projectCache.get(resolvedRoot);
858
+ if (cached)
859
+ return this.freshen(cached);
860
+ const cg = loadCodeGraph().openSync(resolvedRoot);
861
+ this.projectCache.set(resolvedRoot, cg);
862
+ return cg;
863
+ }
864
+ /**
865
+ * Heal a long-lived connection whose `.cgraphx/` was removed and recreated
866
+ * at the same path (a worktree recreated, or `rm -rf .cgraphx` + re-init)
867
+ * before handing it to a tool. Otherwise the daemon keeps serving the
868
+ * pre-removal snapshot from its now-unlinked file handle until restart — and
869
+ * because the daemon registry is keyed by path, a same-path recreate routes
870
+ * new clients straight back to this same stale daemon (#925). The check is one
871
+ * stat() and a no-op unless the inode actually changed; it never throws into a
872
+ * tool call.
873
+ */
874
+ freshen(cg) {
875
+ try {
876
+ if (cg.reopenIfReplaced()) {
877
+ process.stderr.write('[cgraphx MCP] The index was replaced on disk (e.g. a git worktree ' +
878
+ 'recreated at the same path); reopened the live database in place.\n');
879
+ }
880
+ }
881
+ catch {
882
+ // Best-effort self-heal — a failed reopen must never break the tool call;
883
+ // the (still stale) handle keeps serving and the next call retries.
884
+ }
885
+ return cg;
886
+ }
887
+ /**
888
+ * Close all cached project connections
889
+ */
890
+ closeAll() {
891
+ for (const cg of this.projectCache.values()) {
892
+ cg.close();
893
+ }
894
+ this.projectCache.clear();
895
+ this.worktreeMismatchCache.clear();
896
+ }
897
+ /**
898
+ * Validate that a value is a non-empty string within length bounds.
899
+ *
900
+ * The `maxLength` cap protects against MCP clients that ship huge
901
+ * payloads (10MB+ query strings either by accident or maliciously).
902
+ * Without this, a single oversized input can pin the FTS5 index or
903
+ * exhaust memory before any real work runs.
904
+ */
905
+ validateString(value, name, maxLength = MAX_INPUT_LENGTH) {
906
+ if (typeof value !== 'string' || value.length === 0) {
907
+ return this.errorResult(`${name} must be a non-empty string`);
908
+ }
909
+ if (value.length > maxLength) {
910
+ return this.errorResult(`${name} exceeds maximum length of ${maxLength} characters (got ${value.length})`);
911
+ }
912
+ return value;
913
+ }
914
+ /**
915
+ * Validate an optional path-like string input. Returns the value if
916
+ * valid (or undefined), or a ToolResult with the error.
917
+ */
918
+ validateOptionalPath(value, name) {
919
+ if (value === undefined || value === null)
920
+ return undefined;
921
+ if (typeof value !== 'string') {
922
+ return this.errorResult(`${name} must be a string`);
923
+ }
924
+ if (value.length > MAX_PATH_LENGTH) {
925
+ return this.errorResult(`${name} exceeds maximum length of ${MAX_PATH_LENGTH} characters (got ${value.length})`);
926
+ }
927
+ return value;
928
+ }
929
+ /**
930
+ * Cached git worktree/index mismatch for a tool call's effective project.
931
+ *
932
+ * The "effective project" is what the request targets: an explicit
933
+ * `projectPath` arg, else the directory the server resolved its default
934
+ * project from (`defaultProjectHint`), else cwd. Memoized per start path —
935
+ * see `worktreeMismatchCache`. Best-effort: if the project can't be resolved
936
+ * (e.g. nothing initialized yet), it reports "no mismatch" so a tool is never
937
+ * broken by this check.
938
+ */
939
+ worktreeMismatchFor(projectPath) {
940
+ const startPath = projectPath ?? this.defaultProjectHint ?? process.cwd();
941
+ // The verdict depends on BOTH the start path AND the index root it resolves
942
+ // to, so the cache must be keyed on the pair. Resolve the index root first
943
+ // (cheap — getCodeGraph re-walks to the nearest .cgraphx/, no git), then
944
+ // key on `(startPath, indexRoot)`. The moment that root changes — most
945
+ // importantly when a git worktree gains its own index and the walk-up stops
946
+ // there instead of at the parent checkout — the key changes and the verdict
947
+ // is recomputed, instead of serving the stale "borrowed the parent's index"
948
+ // warning for the server's whole lifetime. Keying on startPath alone pinned
949
+ // that first verdict until restart (#926).
950
+ let indexRoot;
951
+ try {
952
+ indexRoot = this.getCodeGraph(projectPath).getProjectRoot();
953
+ }
954
+ catch {
955
+ // No resolvable project (or any other resolution error) → nothing to warn.
956
+ return null;
957
+ }
958
+ const cacheKey = `${startPath}\u0000${indexRoot}`;
959
+ const cached = this.worktreeMismatchCache.get(cacheKey);
960
+ if (cached !== undefined)
961
+ return cached;
962
+ const mismatch = (0, worktree_1.detectWorktreeIndexMismatch)(startPath, indexRoot);
963
+ this.worktreeMismatchCache.set(cacheKey, mismatch);
964
+ return mismatch;
965
+ }
966
+ /**
967
+ * Prefix a successful read-tool result with a compact worktree-mismatch
968
+ * notice when the resolved index belongs to a different git working tree than
969
+ * the caller's (issue #155). Without this, an agent in a nested worktree
970
+ * silently trusts main-branch results. No-op on error results and when there
971
+ * is no mismatch. `codegraph_status` is excluded — it embeds its own verbose
972
+ * warning — so it stays out of this path.
973
+ */
974
+ withWorktreeNotice(result, projectPath) {
975
+ if (result.isError)
976
+ return result;
977
+ const mismatch = this.worktreeMismatchFor(projectPath);
978
+ if (!mismatch)
979
+ return result;
980
+ const notice = (0, worktree_1.worktreeMismatchNotice)(mismatch);
981
+ const [first, ...rest] = result.content;
982
+ if (first && first.type === 'text') {
983
+ return { ...result, content: [{ type: 'text', text: `${notice}\n\n${first.text}` }, ...rest] };
984
+ }
985
+ return result;
986
+ }
987
+ /**
988
+ * Annotate a successful read-tool result with per-file staleness — the
989
+ * non-blocking answer to issue #403. The file watcher tracks every event
990
+ * it sees per path; here we intersect "files referenced in this response"
991
+ * against that pending set and prepend a compact banner so the agent can
992
+ * fall back to Read for those *specific* files without waiting for the
993
+ * debounced sync to fire. Other pending files in the project (not
994
+ * referenced by this response) get a small footer so the agent has a
995
+ * complete picture without bloating the banner.
996
+ *
997
+ * Cost when nothing is pending — the common case — is one boolean check.
998
+ * No I/O, no parsing of markdown beyond a per-pending-file substring scan.
999
+ */
1000
+ withStalenessNotice(result, projectPath) {
1001
+ if (result.isError)
1002
+ return result;
1003
+ let cg;
1004
+ try {
1005
+ cg = this.getCodeGraph(projectPath);
1006
+ }
1007
+ catch {
1008
+ return result; // no default project — leave as is
1009
+ }
1010
+ // Cross-project `projectPath` calls open a cached CodeGraph WITHOUT a
1011
+ // watcher (watchers are only attached to the default session project).
1012
+ // When the cross-project path happens to be the same project as the
1013
+ // default cg, the cached instance is the wrong one — its pendingFiles is
1014
+ // permanently empty. Detect the equal-path case and prefer the default
1015
+ // cg so the staleness signal still fires when an agent passes the
1016
+ // explicit projectPath form of its own project.
1017
+ if (this.cg && cg !== this.cg) {
1018
+ try {
1019
+ const sameProject = (0, path_1.resolve)(this.cg.getProjectRoot()) === (0, path_1.resolve)(cg.getProjectRoot());
1020
+ if (sameProject)
1021
+ cg = this.cg;
1022
+ }
1023
+ catch {
1024
+ /* getProjectRoot may throw on a closed instance — leave cg as is */
1025
+ }
1026
+ }
1027
+ // Whole-index degradation (#876): once live watching has permanently
1028
+ // stopped, getPendingFiles() is empty so the per-file banner below can't
1029
+ // fire — but the index is now FROZEN and silently drifting stale. Surface
1030
+ // one global notice instead, so the agent Reads for current content rather
1031
+ // than trusting a response off a no-longer-updating index. (Cross-project
1032
+ // calls open a watcher-less CodeGraph, so this is false there — correct: we
1033
+ // only know degraded state for the default session project.)
1034
+ let degraded = false;
1035
+ try {
1036
+ degraded = cg.isWatcherDegraded?.() ?? false;
1037
+ }
1038
+ catch {
1039
+ degraded = false;
1040
+ }
1041
+ if (degraded) {
1042
+ const [head, ...tail] = result.content;
1043
+ if (!head || head.type !== 'text')
1044
+ return result;
1045
+ let reason = null;
1046
+ try {
1047
+ reason = cg.getWatcherDegradedReason?.() ?? null;
1048
+ }
1049
+ catch {
1050
+ reason = null;
1051
+ }
1052
+ const composed = `${formatDegradedBanner(reason)}\n\n${head.text}`;
1053
+ return { ...result, content: [{ type: 'text', text: composed }, ...tail] };
1054
+ }
1055
+ // Defensive: some test fakes inject a partial CodeGraph stub without the
1056
+ // newer pending-files API. Treat missing/throwing as "no pending files."
1057
+ let pending = [];
1058
+ try {
1059
+ pending = cg.getPendingFiles?.() ?? [];
1060
+ }
1061
+ catch {
1062
+ return result;
1063
+ }
1064
+ if (pending.length === 0)
1065
+ return result;
1066
+ const [first, ...rest] = result.content;
1067
+ if (!first || first.type !== 'text')
1068
+ return result;
1069
+ const text = first.text;
1070
+ const inResponse = [];
1071
+ const elsewhere = [];
1072
+ for (const p of pending) {
1073
+ // Substring match against the project-relative POSIX path — that's
1074
+ // exactly the format both the watcher and every cgraphx response
1075
+ // emit, so a plain includes() is sufficient and avoids regex pitfalls.
1076
+ if (text.includes(p.path))
1077
+ inResponse.push(p);
1078
+ else
1079
+ elsewhere.push(p);
1080
+ }
1081
+ let banner = '';
1082
+ if (inResponse.length > 0) {
1083
+ banner = formatStaleBanner(inResponse);
1084
+ }
1085
+ let footer = '';
1086
+ if (elsewhere.length > 0) {
1087
+ footer = formatStaleFooter(elsewhere);
1088
+ }
1089
+ if (!banner && !footer)
1090
+ return result;
1091
+ const composed = [banner, text, footer].filter(Boolean).join('\n\n');
1092
+ return { ...result, content: [{ type: 'text', text: composed }, ...rest] };
1093
+ }
1094
+ /**
1095
+ * Execute a tool by name
1096
+ */
1097
+ async execute(toolName, args) {
1098
+ try {
1099
+ // Block the first tool call on the engine's post-open reconcile so we
1100
+ // never serve rows for files deleted/edited while no MCP server was
1101
+ // running. The wait is time-boxed (#905): a huge-repo reconcile takes
1102
+ // minutes, and blocking the first call on all of it reads as a hang, so
1103
+ // we wait briefly then serve and let it finish in the background. The
1104
+ // gate is cleared after first await — subsequent calls pay nothing.
1105
+ // Catch-up failures are logged by the engine; we proceed regardless so a
1106
+ // transient sync error never breaks tools.
1107
+ if (this.catchUpGate) {
1108
+ const gate = this.catchUpGate;
1109
+ this.catchUpGate = null;
1110
+ await this.awaitCatchUpGate(gate);
1111
+ }
1112
+ // Honor the optional tool allowlist (CGRAPHX_MCP_TOOLS): a trimmed
1113
+ // surface rejects ablated tools defensively even if a client cached them.
1114
+ if (!this.isToolAllowed(toolName)) {
1115
+ return this.errorResult(`Tool ${toolName} is disabled via CGRAPHX_MCP_TOOLS`);
1116
+ }
1117
+ // Cross-cutting input validation. All tools accept an optional
1118
+ // `projectPath` and most accept either `query`, `task`, or
1119
+ // `symbol` — bound their lengths centrally so individual handlers
1120
+ // can stay focused on tool-specific logic.
1121
+ const pathCheck = this.validateOptionalPath(args.projectPath, 'projectPath');
1122
+ if (typeof pathCheck === 'object' && pathCheck !== undefined) {
1123
+ return pathCheck;
1124
+ }
1125
+ // The `path` and `pattern` properties used by codegraph_files are
1126
+ // also path-shaped — apply the same cap.
1127
+ if (args.path !== undefined) {
1128
+ const check = this.validateOptionalPath(args.path, 'path');
1129
+ if (typeof check === 'object' && check !== undefined)
1130
+ return check;
1131
+ }
1132
+ if (args.pattern !== undefined) {
1133
+ const check = this.validateOptionalPath(args.pattern, 'pattern');
1134
+ if (typeof check === 'object' && check !== undefined)
1135
+ return check;
1136
+ }
1137
+ // Read tools resolve through a single result variable so cross-cutting
1138
+ // notices — worktree-index mismatch (issue #155) and per-file
1139
+ // staleness (issue #403) — can be applied in one place. status embeds
1140
+ // its own verbose worktree warning but still flows through the
1141
+ // staleness wrapper so its pending-files section stays consistent
1142
+ // with what the read tools surface.
1143
+ let result;
1144
+ switch (toolName) {
1145
+ case 'codegraph_search':
1146
+ result = await this.handleSearch(args);
1147
+ break;
1148
+ case 'codegraph_callers':
1149
+ result = await this.handleCallers(args);
1150
+ break;
1151
+ case 'codegraph_callees':
1152
+ result = await this.handleCallees(args);
1153
+ break;
1154
+ case 'codegraph_impact':
1155
+ result = await this.handleImpact(args);
1156
+ break;
1157
+ case 'codegraph_explore':
1158
+ result = await this.handleExplore(args);
1159
+ break;
1160
+ case 'codegraph_node':
1161
+ result = await this.handleNode(args);
1162
+ break;
1163
+ case 'codegraph_status':
1164
+ // status embeds the pending-files list as a first-class section
1165
+ // (see handleStatus), so we skip the auto-banner wrapper here to
1166
+ // avoid duplicating the same info at the top of the response.
1167
+ return await this.handleStatus(args);
1168
+ case 'codegraph_files':
1169
+ result = await this.handleFiles(args);
1170
+ break;
1171
+ default:
1172
+ return this.errorResult(`Unknown tool: ${toolName}`);
1173
+ }
1174
+ const withWorktree = this.withWorktreeNotice(result, args.projectPath);
1175
+ return this.withStalenessNotice(withWorktree, args.projectPath);
1176
+ }
1177
+ catch (err) {
1178
+ // Expected condition, not a malfunction: answer as a SUCCESS so the
1179
+ // agent keeps trusting the toolset for projects that ARE indexed.
1180
+ // (An isError here teaches session-long abandonment — see NotIndexedError.)
1181
+ if (err instanceof NotIndexedError) {
1182
+ return this.textResult(err.message);
1183
+ }
1184
+ // Security refusal: a clean error, no retry encouragement.
1185
+ if (err instanceof PathRefusalError) {
1186
+ return this.errorResult(err.message);
1187
+ }
1188
+ return this.errorResult(`Tool execution failed: ${err instanceof Error ? err.message : String(err)}. ` +
1189
+ 'This is an internal cgraphx error — retry the call once; if it persists, ' +
1190
+ 'continue without cgraphx for this task.');
1191
+ }
1192
+ }
1193
+ /**
1194
+ * Handle codegraph_search
1195
+ */
1196
+ async handleSearch(args) {
1197
+ const query = this.validateString(args.query, 'query');
1198
+ if (typeof query !== 'string')
1199
+ return query;
1200
+ const cg = this.getCodeGraph(args.projectPath);
1201
+ const rawKind = args.kind;
1202
+ // The schema enum says 'type' (what agents naturally reach for); the
1203
+ // NodeKind is 'type_alias'. Without the mapping, kind: "type" silently
1204
+ // matched nothing — a filter value we advertise must work.
1205
+ const kind = rawKind === 'type' ? 'type_alias' : rawKind;
1206
+ const rawLimit = Number(args.limit) || 10;
1207
+ const limit = (0, utils_1.clamp)(rawLimit, 1, 100);
1208
+ const results = cg.searchNodes(query, {
1209
+ limit,
1210
+ kinds: kind ? [kind] : undefined,
1211
+ });
1212
+ if (results.length === 0) {
1213
+ return this.textResult(`No results found for "${query}"`);
1214
+ }
1215
+ // Down-rank generated files within the FTS-returned set so a search
1216
+ // for "Send" surfaces the hand-written keeper before .pb.go stubs
1217
+ // that share the name. Stable: only reorders generated vs. not.
1218
+ const ranked = [...results].sort((a, b) => {
1219
+ const aGen = (0, generated_detection_1.isGeneratedFile)(a.node.filePath) ? 1 : 0;
1220
+ const bGen = (0, generated_detection_1.isGeneratedFile)(b.node.filePath) ? 1 : 0;
1221
+ return aGen - bGen;
1222
+ });
1223
+ const formatted = this.formatSearchResults(ranked);
1224
+ return this.textResult(this.truncateOutput(formatted));
1225
+ }
1226
+ /**
1227
+ * Group symbol matches into DISTINCT DEFINITIONS — one group per
1228
+ * (filePath, qualifiedName), so same-file overloads stay together while
1229
+ * unrelated same-named classes across a monorepo's apps (#764: one
1230
+ * `UserService` per NestJS app) are kept apart. Optionally narrowed by a
1231
+ * `file` path/suffix first.
1232
+ */
1233
+ groupDefinitions(nodes, fileFilter) {
1234
+ let pool = nodes;
1235
+ let filteredOut = false;
1236
+ if (fileFilter) {
1237
+ const wanted = fileFilter.replace(/^\.\//, '');
1238
+ const narrowed = pool.filter((n) => n.filePath === wanted || n.filePath.endsWith(wanted) || n.filePath.endsWith(`/${wanted}`));
1239
+ if (narrowed.length > 0) {
1240
+ pool = narrowed;
1241
+ }
1242
+ else {
1243
+ filteredOut = true;
1244
+ }
1245
+ }
1246
+ const byDef = new Map();
1247
+ for (const n of pool) {
1248
+ const key = `${n.filePath}|${n.qualifiedName}`;
1249
+ const group = byDef.get(key);
1250
+ if (group)
1251
+ group.push(n);
1252
+ else
1253
+ byDef.set(key, [n]);
1254
+ }
1255
+ return { groups: [...byDef.values()], filteredOut };
1256
+ }
1257
+ /** Section heading for one distinct definition in grouped output. */
1258
+ definitionHeading(group) {
1259
+ const head = group[0];
1260
+ const line = head.startLine ? `:${head.startLine}` : '';
1261
+ return `**${head.qualifiedName}** (${head.kind}) — ${head.filePath}${line}`;
1262
+ }
1263
+ /**
1264
+ * Handle codegraph_callers
1265
+ */
1266
+ async handleCallers(args) {
1267
+ const symbol = this.validateString(args.symbol, 'symbol');
1268
+ if (typeof symbol !== 'string')
1269
+ return symbol;
1270
+ const cg = this.getCodeGraph(args.projectPath);
1271
+ const limit = (0, utils_1.clamp)(args.limit || 20, 1, 100);
1272
+ const fileFilter = typeof args.file === 'string' ? args.file : undefined;
1273
+ const allMatches = this.findAllSymbols(cg, symbol);
1274
+ if (allMatches.nodes.length === 0) {
1275
+ return this.textResult(`Symbol "${symbol}" not found in the codebase`);
1276
+ }
1277
+ const { groups, filteredOut } = this.groupDefinitions(allMatches.nodes, fileFilter);
1278
+ const filterNote = filteredOut
1279
+ ? `\n\n> **Note:** no definition of "${symbol}" matches file "${fileFilter}" — showing all definitions instead.`
1280
+ : '';
1281
+ const collect = (defNodes) => {
1282
+ const seen = new Set();
1283
+ const callers = [];
1284
+ const labels = new Map();
1285
+ for (const node of defNodes) {
1286
+ for (const c of cg.getCallers(node.id)) {
1287
+ if (!seen.has(c.node.id)) {
1288
+ seen.add(c.node.id);
1289
+ callers.push(c.node);
1290
+ const label = this.edgeLabel(c.edge);
1291
+ if (label)
1292
+ labels.set(c.node.id, label);
1293
+ }
1294
+ }
1295
+ }
1296
+ return { callers, labels };
1297
+ };
1298
+ // Single definition (or same-file overloads): the familiar flat list.
1299
+ if (groups.length === 1) {
1300
+ const { callers, labels } = collect(groups[0]);
1301
+ if (callers.length === 0) {
1302
+ return this.textResult(`No callers found for "${symbol}"${allMatches.note}${filterNote}`);
1303
+ }
1304
+ // A successful `file` narrowing makes the multi-symbol aggregation note
1305
+ // stale — suppress it.
1306
+ const note = fileFilter && !filteredOut ? '' : allMatches.note;
1307
+ const formatted = this.formatNodeList(callers.slice(0, limit), `Callers of ${symbol}`, labels) + note + filterNote;
1308
+ return this.textResult(this.truncateOutput(formatted));
1309
+ }
1310
+ // Multiple DISTINCT definitions (#764): one section per definition so an
1311
+ // agent never mistakes one app's callers for another's. Narrow with
1312
+ // `file` to focus a single definition.
1313
+ const lines = [
1314
+ `**Callers of ${symbol} — ${groups.length} distinct definitions (narrow with \`file\`)**`,
1315
+ ];
1316
+ for (const group of groups) {
1317
+ const { callers, labels } = collect(group);
1318
+ lines.push('', this.definitionHeading(group));
1319
+ if (callers.length === 0) {
1320
+ lines.push('- (no callers)');
1321
+ continue;
1322
+ }
1323
+ for (const node of callers.slice(0, limit)) {
1324
+ const location = node.startLine ? `:${node.startLine}` : '';
1325
+ const label = labels.get(node.id);
1326
+ lines.push(`- ${node.name} (${node.kind}) - ${node.filePath}${location}${label ? ` — via ${label}` : ''}`);
1327
+ }
1328
+ }
1329
+ return this.textResult(this.truncateOutput(lines.join('\n') + filterNote));
1330
+ }
1331
+ /**
1332
+ * Handle codegraph_callees
1333
+ */
1334
+ async handleCallees(args) {
1335
+ const symbol = this.validateString(args.symbol, 'symbol');
1336
+ if (typeof symbol !== 'string')
1337
+ return symbol;
1338
+ const cg = this.getCodeGraph(args.projectPath);
1339
+ const limit = (0, utils_1.clamp)(args.limit || 20, 1, 100);
1340
+ const fileFilter = typeof args.file === 'string' ? args.file : undefined;
1341
+ const allMatches = this.findAllSymbols(cg, symbol);
1342
+ if (allMatches.nodes.length === 0) {
1343
+ return this.textResult(`Symbol "${symbol}" not found in the codebase`);
1344
+ }
1345
+ const { groups, filteredOut } = this.groupDefinitions(allMatches.nodes, fileFilter);
1346
+ const filterNote = filteredOut
1347
+ ? `\n\n> **Note:** no definition of "${symbol}" matches file "${fileFilter}" — showing all definitions instead.`
1348
+ : '';
1349
+ const collect = (defNodes) => {
1350
+ const seen = new Set();
1351
+ const callees = [];
1352
+ const labels = new Map();
1353
+ for (const node of defNodes) {
1354
+ for (const c of cg.getCallees(node.id)) {
1355
+ if (!seen.has(c.node.id)) {
1356
+ seen.add(c.node.id);
1357
+ callees.push(c.node);
1358
+ const label = this.edgeLabel(c.edge);
1359
+ if (label)
1360
+ labels.set(c.node.id, label);
1361
+ }
1362
+ }
1363
+ }
1364
+ return { callees, labels };
1365
+ };
1366
+ if (groups.length === 1) {
1367
+ const { callees, labels } = collect(groups[0]);
1368
+ if (callees.length === 0) {
1369
+ return this.textResult(`No callees found for "${symbol}"${allMatches.note}${filterNote}`);
1370
+ }
1371
+ // A successful `file` narrowing makes the multi-symbol aggregation note
1372
+ // stale — suppress it.
1373
+ const note = fileFilter && !filteredOut ? '' : allMatches.note;
1374
+ const formatted = this.formatNodeList(callees.slice(0, limit), `Callees of ${symbol}`, labels) + note + filterNote;
1375
+ return this.textResult(this.truncateOutput(formatted));
1376
+ }
1377
+ // Multiple DISTINCT definitions (#764): per-definition sections.
1378
+ const lines = [
1379
+ `**Callees of ${symbol} — ${groups.length} distinct definitions (narrow with \`file\`)**`,
1380
+ ];
1381
+ for (const group of groups) {
1382
+ const { callees, labels } = collect(group);
1383
+ lines.push('', this.definitionHeading(group));
1384
+ if (callees.length === 0) {
1385
+ lines.push('- (no callees)');
1386
+ continue;
1387
+ }
1388
+ for (const node of callees.slice(0, limit)) {
1389
+ const location = node.startLine ? `:${node.startLine}` : '';
1390
+ const label = labels.get(node.id);
1391
+ lines.push(`- ${node.name} (${node.kind}) - ${node.filePath}${location}${label ? ` — via ${label}` : ''}`);
1392
+ }
1393
+ }
1394
+ return this.textResult(this.truncateOutput(lines.join('\n') + filterNote));
1395
+ }
1396
+ /**
1397
+ * Handle codegraph_impact
1398
+ */
1399
+ async handleImpact(args) {
1400
+ const symbol = this.validateString(args.symbol, 'symbol');
1401
+ if (typeof symbol !== 'string')
1402
+ return symbol;
1403
+ const cg = this.getCodeGraph(args.projectPath);
1404
+ const depth = (0, utils_1.clamp)(args.depth || 2, 1, 10);
1405
+ const fileFilter = typeof args.file === 'string' ? args.file : undefined;
1406
+ const allMatches = this.findAllSymbols(cg, symbol);
1407
+ if (allMatches.nodes.length === 0) {
1408
+ return this.textResult(`Symbol "${symbol}" not found in the codebase`);
1409
+ }
1410
+ const { groups, filteredOut } = this.groupDefinitions(allMatches.nodes, fileFilter);
1411
+ const filterNote = filteredOut
1412
+ ? `\n\n> **Note:** no definition of "${symbol}" matches file "${fileFilter}" — showing all definitions instead.`
1413
+ : '';
1414
+ const impactOf = (defNodes) => {
1415
+ const mergedNodes = new Map();
1416
+ const mergedEdges = [];
1417
+ const seenEdges = new Set();
1418
+ for (const node of defNodes) {
1419
+ const impact = cg.getImpactRadius(node.id, depth);
1420
+ for (const [id, n] of impact.nodes) {
1421
+ mergedNodes.set(id, n);
1422
+ }
1423
+ for (const e of impact.edges) {
1424
+ const key = `${e.source}->${e.target}:${e.kind}`;
1425
+ if (!seenEdges.has(key)) {
1426
+ seenEdges.add(key);
1427
+ mergedEdges.push(e);
1428
+ }
1429
+ }
1430
+ }
1431
+ return { nodes: mergedNodes, edges: mergedEdges, roots: defNodes.map((n) => n.id) };
1432
+ };
1433
+ // Single definition (or same-file overloads): the familiar merged report.
1434
+ if (groups.length === 1) {
1435
+ const formatted = this.formatImpact(symbol, impactOf(groups[0])) + (fileFilter && !filteredOut ? "" : allMatches.note) + filterNote;
1436
+ return this.textResult(this.truncateOutput(formatted));
1437
+ }
1438
+ // Multiple DISTINCT definitions (#764): a blast radius PER definition —
1439
+ // merging unrelated same-named classes (one UserService per monorepo app)
1440
+ // overstated impact and confused agents. Narrow with `file`.
1441
+ const sections = [
1442
+ `**Impact of ${symbol} — ${groups.length} distinct definitions (each with its own blast radius; narrow with \`file\`)**`,
1443
+ ];
1444
+ for (const group of groups) {
1445
+ const head = group[0];
1446
+ const line = head.startLine ? `:${head.startLine}` : '';
1447
+ sections.push('', this.formatImpact(`${head.qualifiedName} (${head.filePath}${line})`, impactOf(group)));
1448
+ }
1449
+ return this.textResult(this.truncateOutput(sections.join('\n') + filterNote));
1450
+ }
1451
+ /**
1452
+ * Describe a synthesized (dynamic-dispatch) edge for human output: how the
1453
+ * callback was wired up — the bridge static parsing can't see. Returns null
1454
+ * for ordinary static edges. Used by trace + the node trail so a synthesized
1455
+ * hop reads as "registered via onUpdate at App.tsx:3148", not a bare arrow.
1456
+ */
1457
+ synthEdgeNote(edge) {
1458
+ if (!edge || edge.provenance !== 'heuristic')
1459
+ return null;
1460
+ const m = edge.metadata;
1461
+ const registeredAt = typeof m?.registeredAt === 'string' ? m.registeredAt : undefined;
1462
+ const at = registeredAt ? ` @${registeredAt}` : '';
1463
+ if (m?.synthesizedBy === 'callback') {
1464
+ const via = m.via ? `\`${String(m.via)}\`` : 'a registrar';
1465
+ const field = m.field ? ` on .${String(m.field)}` : '';
1466
+ return {
1467
+ label: `callback — registered via ${via}${field} (dynamic dispatch)`,
1468
+ compact: `dynamic: callback via ${via}${at}`,
1469
+ registeredAt,
1470
+ };
1471
+ }
1472
+ if (m?.synthesizedBy === 'event-emitter') {
1473
+ const ev = m.event ? `\`${String(m.event)}\`` : 'an event';
1474
+ return {
1475
+ label: `event ${ev} — emit → handler (dynamic dispatch)`,
1476
+ compact: `dynamic: event ${ev}${at}`,
1477
+ registeredAt,
1478
+ };
1479
+ }
1480
+ if (m?.synthesizedBy === 'react-render') {
1481
+ return {
1482
+ label: `React re-render — \`setState\` re-runs render() (dynamic dispatch)`,
1483
+ compact: `dynamic: React re-render via setState${at}`,
1484
+ registeredAt,
1485
+ };
1486
+ }
1487
+ if (m?.synthesizedBy === 'jsx-render') {
1488
+ const child = m.via ? `<${String(m.via)}>` : 'a child component';
1489
+ return {
1490
+ label: `renders ${child} (JSX child — dynamic dispatch)`,
1491
+ compact: `dynamic: renders ${child}`,
1492
+ registeredAt,
1493
+ };
1494
+ }
1495
+ if (m?.synthesizedBy === 'vue-handler') {
1496
+ const ev = m.event ? `@${String(m.event)}` : 'a template event';
1497
+ return {
1498
+ label: `Vue template handler — bound to ${ev} (dynamic dispatch)`,
1499
+ compact: `dynamic: Vue ${ev} handler`,
1500
+ registeredAt,
1501
+ };
1502
+ }
1503
+ if (m?.synthesizedBy === 'interface-impl') {
1504
+ return {
1505
+ label: `interface/abstract dispatch — runs the implementation override (dynamic dispatch)`,
1506
+ compact: `dynamic: interface → impl${at}`,
1507
+ registeredAt,
1508
+ };
1509
+ }
1510
+ if (m?.synthesizedBy === 'closure-collection') {
1511
+ const field = m.field ? `\`${String(m.field)}\`` : 'a collection';
1512
+ return {
1513
+ label: `closure collection — runs handlers appended to ${field} (dynamic dispatch)`,
1514
+ compact: `dynamic: runs ${field} handlers${at}`,
1515
+ registeredAt,
1516
+ };
1517
+ }
1518
+ if (m?.synthesizedBy === 'fn-pointer-dispatch') {
1519
+ const via = m.via ? `\`${String(m.via)}\`` : 'a function pointer';
1520
+ return {
1521
+ label: `function-pointer dispatch via ${via} (dynamic dispatch)`,
1522
+ compact: `dynamic: fn-pointer ${m.via ? String(m.via) : ''}${at}`,
1523
+ registeredAt,
1524
+ };
1525
+ }
1526
+ if (m?.synthesizedBy === 'goframe-route') {
1527
+ const route = m.route ? `\`${String(m.route)}\`` : 'a route';
1528
+ return {
1529
+ label: `GoFrame route ${route} — reflective Bind → controller method (dynamic dispatch)`,
1530
+ compact: `dynamic: GoFrame route ${m.route ? String(m.route) : ''}${at}`,
1531
+ registeredAt,
1532
+ };
1533
+ }
1534
+ // Generic fallback for any other synthesizer (redux-thunk, gin-middleware-chain,
1535
+ // flutter-build, …): a synthesized hop must never read as a bare static `calls`.
1536
+ // It's a dynamic-dispatch bridge — label it as one and keep its wiring site.
1537
+ if (typeof m?.synthesizedBy === 'string') {
1538
+ const kind = m.synthesizedBy.replace(/-/g, ' ');
1539
+ return { label: `${kind} (dynamic dispatch)`, compact: `dynamic: ${kind}${at}`, registeredAt };
1540
+ }
1541
+ return null;
1542
+ }
1543
+ /**
1544
+ * Flow-from-named-symbols: an agent's codegraph_explore query is a bag of
1545
+ * symbol names that usually spans the flow it's investigating (e.g.
1546
+ * "PmsProductController getList PmsProductService list PmsProductServiceImpl").
1547
+ * Surface the longest call chain AMONG those named symbols — scoped to what the
1548
+ * agent explicitly named, so (unlike a fuzzy relevance set) there's no
1549
+ * wrong-feature wandering. Rides synthesized edges, so controller→service-
1550
+ * interface→impl shows up. Returns '' if no chain of >=3 nodes exists.
1551
+ *
1552
+ * Ambiguous tokens (Java `list` → dozens of nodes) are disambiguated by
1553
+ * CO-NAMING: the agent names the class too, so we keep only `list` candidates
1554
+ * whose qualifiedName contains another named token (`PmsProductServiceImpl::list`),
1555
+ * dropping unrelated `OmsOrderService::list`.
1556
+ */
1557
+ buildFlowFromNamedSymbols(cg, query) {
1558
+ // spineCallSites: for each spine node, the line where it CALLS the next hop —
1559
+ // lets the source assembler window an oversize spine method (e.g. n8n's 962-line
1560
+ // processRunExecutionData) to the call site instead of dumping the whole body.
1561
+ const EMPTY = { text: '', pathNodeIds: new Set(), namedNodeIds: new Set(), uniqueNamedNodeIds: new Set(), spineCallSites: new Map() };
1562
+ try {
1563
+ const CALLABLE = new Set(['method', 'function', 'component', 'constructor']);
1564
+ // Strip only a REAL file extension (Create.cs → Create); KEEP qualified
1565
+ // names (Class.method / Class::method) — the agent's most precise input,
1566
+ // resolved exactly by findAllSymbols. (The old strip mangled Class.method
1567
+ // into Class, throwing the method away.)
1568
+ const FILE_EXT = /\.(?:java|kt|kts|ts|tsx|js|jsx|mjs|cjs|cs|py|go|rb|php|swift|rs|cpp|cc|cxx|c|h|hpp|scala|lua|dart|vue|svelte|astro)$/i;
1569
+ const tokens = [...new Set(query.split(/[\s,()[\]]+/)
1570
+ .map((t) => t.replace(FILE_EXT, '').trim())
1571
+ .filter((t) => t.length >= 3 && /^[A-Za-z_$][\w$]*(?:(?:::|\.)[\w$]+)*$/.test(t)))].slice(0, 16);
1572
+ if (tokens.length < 2)
1573
+ return EMPTY;
1574
+ // Pool of name SEGMENTS (Class + method from every token) used to
1575
+ // disambiguate an ambiguous SIMPLE name: keep a candidate only if its
1576
+ // CONTAINER class is itself named in the query.
1577
+ const segPool = new Set();
1578
+ for (const t of tokens)
1579
+ for (const s of t.toLowerCase().split(/::|\./))
1580
+ if (s)
1581
+ segPool.add(s);
1582
+ const named = new Map();
1583
+ // Nodes whose token is SPECIFIC — a (near-)unique callable name (<=3 defs in
1584
+ // the whole graph). These are safe to SPARE a file on: the agent named THIS
1585
+ // method (`getResponseWithInterceptorChain`, 1 def). A hyper-polymorphic name
1586
+ // (`as_sql`, 110 defs across every Expression/Compiler subclass) is NOT here,
1587
+ // so naming it doesn't keep every backend variant full and flood the budget.
1588
+ const uniqueNamedNodeIds = new Set();
1589
+ // token → resolved node ids: drives the token-coverage check that gates
1590
+ // the dynamic-boundary scan (a token is covered when ANY of its nodes
1591
+ // lands on the main chain — overloads off the chain don't count against).
1592
+ const tokenNodes = new Map();
1593
+ // token → its full same-name callable family (before the container filter).
1594
+ // A LARGE family that fails to connect on the chain is a polymorphic
1595
+ // interface/registry dispatch — surfaced by buildPolymorphicBoundaries below.
1596
+ const tokenFamily = new Map();
1597
+ // Non-callable endpoints (CONSTANT/VARIABLE/FIELD) connected by a SYNTHESIZED
1598
+ // edge. RTK thunks are `const X = createAsyncThunk(...)`, so a thunk→thunk hop
1599
+ // is constant→constant — the CALLABLE-only `named` set can't hold it, and
1600
+ // without this the hop is invisible to the Flow path at every tier (the
1601
+ // Relationships section catches it only on repos ≥500 files). Kept SEPARATE
1602
+ // from `named` (which drives the call-chain + source sizing, callable-only);
1603
+ // fed only to the dynamic-dispatch-links scan below.
1604
+ const dynNamed = new Map();
1605
+ const DYN_KINDS = new Set(['constant', 'variable', 'field', 'property']);
1606
+ const hasHeuristicEdge = (id) => [...cg.getCallers(id), ...cg.getCallees(id)].some(({ edge }) => edge.provenance === 'heuristic');
1607
+ for (const t of tokens) {
1608
+ const hits = this.findAllSymbols(cg, t).nodes;
1609
+ const cands = hits.filter((n) => CALLABLE.has(n.kind));
1610
+ tokenFamily.set(t, cands);
1611
+ // A qualified or otherwise-specific name (<=3 hits) keeps all; an
1612
+ // ambiguous simple name keeps only candidates whose container is named.
1613
+ const specific = cands.length <= 3;
1614
+ const pick = specific
1615
+ ? cands
1616
+ : cands.filter((n) => {
1617
+ const segs = (n.qualifiedName || '').toLowerCase().split(/::|\./).filter(Boolean);
1618
+ const container = segs.length >= 2 ? segs[segs.length - 2] : '';
1619
+ return !!container && segPool.has(container);
1620
+ });
1621
+ const kept = pick.slice(0, 6);
1622
+ tokenNodes.set(t, kept.map((n) => n.id));
1623
+ for (const n of kept) {
1624
+ named.set(n.id, n);
1625
+ if (specific)
1626
+ uniqueNamedNodeIds.add(n.id);
1627
+ }
1628
+ // Same token, non-callable synth endpoints (capped, precision-gated on an
1629
+ // actual heuristic edge so plain config constants never qualify).
1630
+ if (dynNamed.size < 12) {
1631
+ for (const n of hits) {
1632
+ if (CALLABLE.has(n.kind) || !DYN_KINDS.has(n.kind) || dynNamed.has(n.id))
1633
+ continue;
1634
+ if (hasHeuristicEdge(n.id))
1635
+ dynNamed.set(n.id, n);
1636
+ if (dynNamed.size >= 12)
1637
+ break;
1638
+ }
1639
+ }
1640
+ if (named.size > 40)
1641
+ break;
1642
+ }
1643
+ // Surface synthesized (heuristic) edges incident to a named symbol — INCLUDING
1644
+ // the non-callable CONSTANT endpoints in `dynNamed`. `skipInChain` drops a hop
1645
+ // already shown in the rendered main chain (a 2-node chain renders nothing, so a
1646
+ // direct named→named synth hop still surfaces — #687).
1647
+ const collectSynthLinks = (skipInChain) => {
1648
+ const synthLines = [];
1649
+ const synthSeen = new Set();
1650
+ for (const n of [...named.values(), ...dynNamed.values()]) {
1651
+ if (synthLines.length >= 6)
1652
+ break;
1653
+ for (const { node: other, edge } of [...cg.getCallers(n.id), ...cg.getCallees(n.id)]) {
1654
+ if (synthLines.length >= 6)
1655
+ break;
1656
+ if (edge.provenance !== 'heuristic' || other.id === n.id)
1657
+ continue;
1658
+ if (skipInChain && skipInChain(edge))
1659
+ continue;
1660
+ const src = edge.source === n.id ? n : other;
1661
+ const tgt = edge.source === n.id ? other : n;
1662
+ const key = `${src.name}>${tgt.name}`;
1663
+ if (synthSeen.has(key))
1664
+ continue;
1665
+ synthSeen.add(key);
1666
+ const note = this.synthEdgeNote(edge);
1667
+ synthLines.push(`- ${src.name} → ${tgt.name} [${note ? note.compact : edge.kind}]`);
1668
+ }
1669
+ }
1670
+ return synthLines;
1671
+ };
1672
+ if (named.size < 2) {
1673
+ // <2 CALLABLES resolved. Two recoveries before giving up: (1) synthesized
1674
+ // edges among named CONSTANT/VARIABLE endpoints — RTK thunk→thunk is
1675
+ // constant→constant, so `named` can be empty while `dynNamed` holds the
1676
+ // whole chain; (2) the one resolved callable's body may hold the
1677
+ // dynamic-dispatch site that EXPLAINS a half-connected flow.
1678
+ const synthLines = collectSynthLinks(null);
1679
+ const boundaries = named.size === 0 ? '' : (this.buildDynamicBoundaries(cg, [...named.values()], named) || '');
1680
+ if (synthLines.length === 0 && !boundaries)
1681
+ return EMPTY;
1682
+ const out = [];
1683
+ if (synthLines.length)
1684
+ out.push('**Dynamic-dispatch links among your symbols**', '(synthesized — the indirect hops grep/Read would reconstruct; the `@file:line` is the wiring site)', '', ...synthLines, '');
1685
+ if (boundaries)
1686
+ out.push(boundaries);
1687
+ out.push('> Full source for these symbols is below.\n');
1688
+ return { text: out.join('\n'), pathNodeIds: new Set(), namedNodeIds: new Set([...named.keys(), ...dynNamed.keys()]), uniqueNamedNodeIds, spineCallSites: new Map() };
1689
+ }
1690
+ const MAX_HOPS = 7;
1691
+ let best = null;
1692
+ // BFS the full call graph (incl. synth edges) from each named seed, but
1693
+ // only ACCEPT a sink that is also named — both ends anchored to symbols the
1694
+ // agent named, so the chain stays on-topic while bridging intermediates
1695
+ // (e.g. the exact interface overload) that the token resolution missed.
1696
+ for (const seed of [...named.values()].slice(0, 8)) {
1697
+ const parent = new Map();
1698
+ parent.set(seed.id, { prev: null, edge: null, node: seed });
1699
+ const q = [{ id: seed.id, depth: 0, streak: 0 }];
1700
+ let deep = null, deepDepth = 0;
1701
+ const MAX_BRIDGE = 1; // ≤1 consecutive UNNAMED hop: bridge one missing intermediate, never wander a god-function's fan-out
1702
+ for (let h = 0; h < q.length && parent.size < 1500; h++) {
1703
+ const { id, depth, streak } = q[h];
1704
+ if (id !== seed.id && named.has(id) && depth > deepDepth) {
1705
+ deep = id;
1706
+ deepDepth = depth;
1707
+ }
1708
+ if (depth >= MAX_HOPS - 1)
1709
+ continue;
1710
+ for (const c of cg.getCallees(id)) {
1711
+ if (c.edge.kind !== 'calls' || parent.has(c.node.id))
1712
+ continue;
1713
+ const newStreak = named.has(c.node.id) ? 0 : streak + 1;
1714
+ if (newStreak > MAX_BRIDGE)
1715
+ continue;
1716
+ parent.set(c.node.id, { prev: id, edge: c.edge, node: c.node });
1717
+ q.push({ id: c.node.id, depth: depth + 1, streak: newStreak });
1718
+ }
1719
+ }
1720
+ if (!deep)
1721
+ continue;
1722
+ const chain = [];
1723
+ let cur = deep;
1724
+ while (cur) {
1725
+ const p = parent.get(cur);
1726
+ if (!p)
1727
+ break;
1728
+ chain.push({ node: p.node, edge: p.edge });
1729
+ cur = p.prev;
1730
+ }
1731
+ chain.reverse();
1732
+ if (!best || chain.length > best.length)
1733
+ best = chain;
1734
+ }
1735
+ const hasMain = !!best && best.length >= 3;
1736
+ const pathIds = new Set((best ?? []).map((s) => s.node.id));
1737
+ // Where each spine node calls the NEXT hop (best[i+1].edge is the edge from
1738
+ // best[i] → best[i+1]; its line is the call site inside best[i]'s body). Lets
1739
+ // the assembler window an oversize spine method to the call instead of dumping it.
1740
+ const spineCallSites = new Map();
1741
+ if (best)
1742
+ for (let i = 0; i < best.length - 1; i++) {
1743
+ const ln = best[i + 1]?.edge?.line;
1744
+ if (ln && ln > 0 && !spineCallSites.has(best[i].node.id))
1745
+ spineCallSites.set(best[i].node.id, ln);
1746
+ }
1747
+ // Dynamic-boundary scan (#687) — fires ONLY when the flow the agent
1748
+ // asked about did not fully connect: some token resolved to nodes but
1749
+ // none of them sit on the main chain (or there is no chain at all). A
1750
+ // healthy flow skips this entirely. Scan order: the chain's dead end
1751
+ // first (where the partial flow stops), then the disconnected symbols,
1752
+ // agent-specific (unique-named) ones first.
1753
+ let boundaryText = '';
1754
+ {
1755
+ const uncovered = [];
1756
+ if (!hasMain) {
1757
+ // No rendered chain — but a 2-node chain still CONNECTS its two
1758
+ // endpoints (e.g. via one synthesized hop, surfaced below as a
1759
+ // dynamic-dispatch link). Only nodes off that short chain are
1760
+ // unexplained breaks worth scanning.
1761
+ for (const n of named.values())
1762
+ if (!pathIds.has(n.id))
1763
+ uncovered.push(n);
1764
+ }
1765
+ else {
1766
+ for (const ids of tokenNodes.values()) {
1767
+ if (ids.length === 0 || ids.some((id) => pathIds.has(id)))
1768
+ continue;
1769
+ for (const id of ids) {
1770
+ const n = named.get(id);
1771
+ if (n)
1772
+ uncovered.push(n);
1773
+ }
1774
+ }
1775
+ }
1776
+ if (uncovered.length > 0) {
1777
+ const scanList = [];
1778
+ if (hasMain)
1779
+ scanList.push(best[best.length - 1].node);
1780
+ scanList.push(...uncovered.sort((a, b) => (uniqueNamedNodeIds.has(b.id) ? 1 : 0) - (uniqueNamedNodeIds.has(a.id) ? 1 : 0)));
1781
+ boundaryText = this.buildDynamicBoundaries(cg, scanList, named);
1782
+ }
1783
+ }
1784
+ // Interface/registry-dispatch announcement (extends #687 to GRAPH-visible
1785
+ // polymorphism). A method the agent NAMED that resolves to a large same-name
1786
+ // family AND did not land on the main chain is almost always a runtime
1787
+ // dispatch (plugin/strategy/handler interface): the concrete target is chosen
1788
+ // at runtime from N implementations, so no single static edge is the answer.
1789
+ // The body-scan above can't see this — `nodeType.execute()` is textually an
1790
+ // ordinary call; the polymorphism lives in the graph (implements edges), so
1791
+ // detect it there. Fires ONLY for an uncovered named token; a connected flow
1792
+ // stays silent.
1793
+ let polyText = '';
1794
+ {
1795
+ const POLY_MIN_FAMILY = 8; // smaller families are overload sets, not dispatch
1796
+ const polyCands = [];
1797
+ for (const [t, fam] of tokenFamily) {
1798
+ if (fam.length < POLY_MIN_FAMILY)
1799
+ continue;
1800
+ const ids = tokenNodes.get(t) || [];
1801
+ if (ids.some((id) => pathIds.has(id)))
1802
+ continue; // covered by the flow — silent
1803
+ polyCands.push({ token: t, family: fam });
1804
+ }
1805
+ if (polyCands.length)
1806
+ polyText = this.buildPolymorphicBoundaries(cg, polyCands, named);
1807
+ }
1808
+ // Supplementary: dynamic-dispatch (synthesized) edges incident to a named
1809
+ // symbol (incl. the non-callable CONSTANT endpoints in `dynNamed`) — the
1810
+ // indirect hops an agent would otherwise grep/Read to reconstruct ("where do
1811
+ // the appended `validators` actually run?"). Surfaced even when the OTHER end
1812
+ // wasn't named. The skip drops a hop already in the rendered main chain; a
1813
+ // 2-node chain renders nothing (hasMain false) so a direct named→named synth
1814
+ // hop still surfaces — too short for Flow, but #687-visible here.
1815
+ const synthLines = collectSynthLinks(hasMain ? (e) => pathIds.has(e.source) && pathIds.has(e.target) : null);
1816
+ if (!hasMain && synthLines.length === 0 && !boundaryText && !polyText)
1817
+ return EMPTY;
1818
+ const out = [];
1819
+ if (hasMain) {
1820
+ out.push('**Flow (call path among the symbols you queried)**', '');
1821
+ for (let i = 0; i < best.length; i++) {
1822
+ const step = best[i];
1823
+ if (step.edge) {
1824
+ const sy = this.synthEdgeNote(step.edge);
1825
+ out.push(` ↓ ${sy ? sy.compact : step.edge.kind}`);
1826
+ }
1827
+ out.push(`${i + 1}. ${step.node.name} (${step.node.filePath}:${step.node.startLine})`);
1828
+ }
1829
+ out.push('');
1830
+ }
1831
+ if (synthLines.length) {
1832
+ out.push('**Dynamic-dispatch links among your symbols**', '(synthesized — the indirect hops grep/Read would reconstruct; the `@file:line` is the wiring site)', '', ...synthLines, '');
1833
+ }
1834
+ if (boundaryText)
1835
+ out.push(boundaryText);
1836
+ if (polyText)
1837
+ out.push(polyText);
1838
+ out.push('> Full source for these symbols is below — the call flow among them, followed by their bodies.', '');
1839
+ // namedNodeIds = every callable the agent explicitly named (a superset of
1840
+ // the spine). A file holding one is something the agent asked to SEE, so it
1841
+ // must keep full source even if it's an off-spine polymorphic sibling — the
1842
+ // agent named `getResponseWithInterceptorChain` / `SQLCompiler.execute_sql`
1843
+ // as the mechanism, not as an interchangeable leaf. See the skeleton gate.
1844
+ return { text: out.join('\n'), pathNodeIds: pathIds, namedNodeIds: new Set([...named.keys(), ...dynNamed.keys()]), uniqueNamedNodeIds, spineCallSites };
1845
+ }
1846
+ catch {
1847
+ return EMPTY;
1848
+ }
1849
+ }
1850
+ /**
1851
+ * Dynamic-boundary surfacing (#687): when the flow among the agent's named
1852
+ * symbols does not fully connect, scan the disconnected symbols' bodies for
1853
+ * dynamic-dispatch sites (computed member calls, getattr, reflection, typed
1854
+ * message buses, runtime-keyed emits) and ANNOUNCE the boundary — the exact
1855
+ * site, the form, and (when a key is statically visible) candidate targets —
1856
+ * instead of guessing edges. The answer to "how does A reach B" when no
1857
+ * static path exists IS the dispatch site: that's where the flow continues
1858
+ * at runtime. Query-time, deterministic, zero graph mutation; a fully
1859
+ * connected flow never reaches this method.
1860
+ */
1861
+ buildDynamicBoundaries(cg, scanList, named) {
1862
+ const MAX_NOTES = 4; // boundary bullets per explore
1863
+ const MAX_SCAN = 8; // bodies scanned
1864
+ const MAX_TOTAL_CHARS = 200_000;
1865
+ let projectRoot;
1866
+ try {
1867
+ projectRoot = cg.getProjectRoot();
1868
+ }
1869
+ catch {
1870
+ return '';
1871
+ }
1872
+ const notes = [];
1873
+ const seenNode = new Set();
1874
+ const seenSite = new Set();
1875
+ let scanned = 0, charsScanned = 0;
1876
+ for (const node of scanList) {
1877
+ if (notes.length >= MAX_NOTES || scanned >= MAX_SCAN || charsScanned > MAX_TOTAL_CHARS)
1878
+ break;
1879
+ if (seenNode.has(node.id) || !node.startLine || !node.endLine)
1880
+ continue;
1881
+ seenNode.add(node.id);
1882
+ const absPath = (0, utils_1.validatePathWithinRoot)(projectRoot, node.filePath);
1883
+ if (!absPath || !(0, fs_1.existsSync)(absPath))
1884
+ continue;
1885
+ let content;
1886
+ try {
1887
+ content = (0, fs_1.readFileSync)(absPath, 'utf-8');
1888
+ }
1889
+ catch {
1890
+ continue;
1891
+ }
1892
+ const body = content.split('\n').slice(node.startLine - 1, node.endLine).join('\n');
1893
+ scanned++;
1894
+ charsScanned += body.length;
1895
+ for (const m of (0, dynamic_boundaries_1.scanDynamicDispatch)(body, node.language || '', node.startLine)) {
1896
+ if (notes.length >= MAX_NOTES)
1897
+ break;
1898
+ const siteKey = `${node.filePath}:${m.line}:${m.form}`;
1899
+ if (seenSite.has(siteKey))
1900
+ continue;
1901
+ seenSite.add(siteKey);
1902
+ const more = m.moreSites ? ` (+${m.moreSites} more such site${m.moreSites > 1 ? 's' : ''} in this body)` : '';
1903
+ notes.push(`- \`${node.name}\` (${node.filePath}:${m.line}) — ${m.label}: \`${m.snippet}\`${more}`);
1904
+ if (m.key) {
1905
+ const cand = this.boundaryCandidates(cg, m.key, !!m.keyIsType, named, node.id);
1906
+ if (cand)
1907
+ notes.push(` ${cand}`);
1908
+ }
1909
+ }
1910
+ }
1911
+ if (notes.length === 0)
1912
+ return '';
1913
+ return [
1914
+ '**Dynamic boundaries (the static path ends at runtime dispatch)**',
1915
+ '',
1916
+ ...notes,
1917
+ '',
1918
+ '> These sites choose their call target at runtime (registry / bus / reflection) — the site shown IS where the flow continues. To follow it, run codegraph_explore or codegraph_node on a candidate; source for the sites above is included below.',
1919
+ '',
1920
+ ].join('\n');
1921
+ }
1922
+ /**
1923
+ * Interface/registry-dispatch announcement — #687 extended to GRAPH-visible
1924
+ * polymorphism (the body-scan can't see it: `nodeType.execute()` is textually
1925
+ * an ordinary call; the polymorphism lives in the `implements`/`extends` edges).
1926
+ *
1927
+ * A method the agent named that resolves to a large same-name family whose
1928
+ * definers overwhelmingly implement/extend ONE supertype is a runtime dispatch:
1929
+ * the concrete target is chosen at runtime from N implementations, so no single
1930
+ * static edge is "the answer" — the implementations ARE the continuations. We
1931
+ * announce the supertype, its TRUE implementer count, and a few concrete targets,
1932
+ * then steer to codegraph_explore. Graph-only, query-time, zero mutation; the
1933
+ * caller fires it ONLY for an UNCOVERED named token, so a connected flow is silent.
1934
+ *
1935
+ * Robust to FTS sampling bias: the same-name family is a capped FTS sample that
1936
+ * over-represents whatever FTS ranks first (n8n: DB `TableOperation.execute`
1937
+ * outnumbered `INodeType.execute` in the sample 7:6 even though INodeType has
1938
+ * 611 implementers vs a handful). So candidate supertypes are ranked by their
1939
+ * TRUE graph-wide implementer count, NOT their frequency in the sample.
1940
+ */
1941
+ buildPolymorphicBoundaries(cg, candidates, named) {
1942
+ const CLASSY = new Set(['class', 'struct', 'interface', 'trait', 'protocol', 'abstract']);
1943
+ const MIN_IMPL = 8; // a supertype needs >= this many implementers to count as "polymorphic"
1944
+ const MIN_SUPPORT = 2; // >= this many sampled definers must share the supertype (ties it to the token)
1945
+ const SAMPLE = 40; // family members inspected per token
1946
+ const MAX_NOTES = 3;
1947
+ const rel = (p) => p.replace(/\\/g, '/');
1948
+ const containerOf = (m) => {
1949
+ try {
1950
+ const ce = cg.getIncomingEdges(m.id).find((e) => e.kind === 'contains');
1951
+ return ce ? cg.getNode(ce.source) : null;
1952
+ }
1953
+ catch {
1954
+ return null;
1955
+ }
1956
+ };
1957
+ const notes = [];
1958
+ const seenSuper = new Set();
1959
+ for (const { token, family } of candidates) {
1960
+ if (notes.length >= MAX_NOTES)
1961
+ break;
1962
+ // supertype id → how many sampled definers share it + a few example definers
1963
+ const supers = new Map();
1964
+ for (const m of family.slice(0, SAMPLE)) {
1965
+ const container = containerOf(m);
1966
+ if (!container || !CLASSY.has(container.kind))
1967
+ continue;
1968
+ let sups = [];
1969
+ try {
1970
+ sups = cg.getOutgoingEdges(container.id)
1971
+ .filter((e) => e.kind === 'implements' || e.kind === 'extends')
1972
+ .map((e) => { try {
1973
+ return cg.getNode(e.target);
1974
+ }
1975
+ catch {
1976
+ return null;
1977
+ } })
1978
+ .filter((n) => !!n && CLASSY.has(n.kind) && (n.name?.length || 0) >= 3);
1979
+ }
1980
+ catch { /* no supertypes — free function or unresolved */ }
1981
+ for (const s of sups) {
1982
+ const e = supers.get(s.id) || { node: s, count: 0, targets: [] };
1983
+ e.count++;
1984
+ if (e.targets.length < 6)
1985
+ e.targets.push(m);
1986
+ supers.set(s.id, e);
1987
+ }
1988
+ }
1989
+ // Pick the supertype with the most TRUE implementers (graph-wide), among
1990
+ // those genuinely shared by the token's definers.
1991
+ let best = null;
1992
+ for (const { node, count, targets } of supers.values()) {
1993
+ if (count < MIN_SUPPORT)
1994
+ continue;
1995
+ let impl = 0;
1996
+ try {
1997
+ impl = cg.getIncomingEdges(node.id).filter((e) => e.kind === 'implements' || e.kind === 'extends').length;
1998
+ }
1999
+ catch { /* leave 0 — gated out below */ }
2000
+ if (impl < MIN_IMPL)
2001
+ continue;
2002
+ if (!best || impl > best.impl)
2003
+ best = { node, impl, targets };
2004
+ }
2005
+ if (!best || seenSuper.has(best.node.id))
2006
+ continue;
2007
+ seenSuper.add(best.node.id);
2008
+ const namedNames = new Set([...named.values()].map((n) => n.name));
2009
+ const eg = best.targets.slice(0, 4).map((m) => {
2010
+ const cont = containerOf(m);
2011
+ const disp = cont ? `${cont.name}.${m.name}` : (m.qualifiedName || m.name);
2012
+ const mark = cont && namedNames.has(cont.name) ? ' ← you named this' : '';
2013
+ return `\`${disp}\` (${rel(m.filePath)}:${m.startLine})${mark}`;
2014
+ });
2015
+ const more = best.impl > eg.length ? ` +${best.impl - eg.length} more` : '';
2016
+ notes.push(`- \`${token}\` → runtime dispatch to **${best.impl}** types implementing \`${best.node.name}\` — the static path ends here, the target is chosen at runtime. e.g. ${eg.join(', ')}${more}`);
2017
+ }
2018
+ if (notes.length === 0)
2019
+ return '';
2020
+ return [
2021
+ '**Interface dispatch (a named method has many implementations)**',
2022
+ '',
2023
+ ...notes,
2024
+ '',
2025
+ '> The method above is dispatched at runtime to one of the listed implementations (a registry / plugin / strategy interface) — there is no single static caller→callee edge; the implementations ARE the continuations. To follow one, run codegraph_explore on a listed target.',
2026
+ '',
2027
+ ].join('\n');
2028
+ }
2029
+ /**
2030
+ * Shortlist candidate runtime targets for a dispatch key surfaced by
2031
+ * {@link buildDynamicBoundaries}. Exact conventional names first (`save` →
2032
+ * `onSave`/`handleSave`; `CreateCmd` → `CreateCmdHandler`), then FTS, with a
2033
+ * normalized-containment post-filter (FTS camel-splitting is fuzzier than a
2034
+ * candidate list should be). Symbols the agent already named sort first and
2035
+ * are marked — that's the "you were right, here's the wiring" case.
2036
+ */
2037
+ boundaryCandidates(cg, key, keyIsType, named, selfId) {
2038
+ const CALLABLE = new Set(['method', 'function', 'component', 'constructor', 'class']);
2039
+ const norm = (s) => s.toLowerCase().replace(/[^a-z0-9]/g, '');
2040
+ const keyNorm = norm(key);
2041
+ if (keyNorm.length < 3)
2042
+ return '';
2043
+ const cands = new Map();
2044
+ const consider = (n) => {
2045
+ if (!n || n.id === selfId || !CALLABLE.has(n.kind) || cands.has(n.id))
2046
+ return;
2047
+ const nameNorm = norm(n.name || '');
2048
+ if (nameNorm.length < 3)
2049
+ return;
2050
+ if (!nameNorm.includes(keyNorm) && !keyNorm.includes(nameNorm))
2051
+ return;
2052
+ cands.set(n.id, n);
2053
+ };
2054
+ const cap = key.charAt(0).toUpperCase() + key.slice(1);
2055
+ const probes = keyIsType
2056
+ ? [`${key}Handler`, key]
2057
+ : [key, `on${cap}`, `handle${cap}`, `${key}Handler`, `handle_${key}`];
2058
+ for (const p of probes) {
2059
+ try {
2060
+ for (const n of cg.getNodesByName(p))
2061
+ consider(n);
2062
+ }
2063
+ catch { /* exact probe miss is fine */ }
2064
+ }
2065
+ let raw = 0;
2066
+ try {
2067
+ const results = cg.searchNodes(key, { limit: 12 });
2068
+ raw = results.length;
2069
+ for (const r of results)
2070
+ consider(r.node);
2071
+ }
2072
+ catch { /* FTS syntax edge — exact probes already ran */ }
2073
+ if (cands.size === 0) {
2074
+ return raw >= 12 && key.length < 5 ? `key \`${key}\` is too generic to shortlist (${raw}+ matches)` : '';
2075
+ }
2076
+ // A constructor candidate duplicates its class: extractors emit ctors as
2077
+ // METHOD nodes named like the class (C#/Java `Foo::Foo`) — keep the class.
2078
+ const all = [...cands.values()];
2079
+ const classKey = new Set(all.filter((n) => n.kind === 'class').map((n) => `${n.name}|${n.filePath}`));
2080
+ const namedNames = new Set([...named.values()].map((n) => n.name));
2081
+ const isNamed = (n) => named.has(n.id) || namedNames.has(n.name); // the flow's named set holds callables only — transfer the mark to the class
2082
+ const list = all
2083
+ .filter((n) => !(n.kind !== 'class' && classKey.has(`${n.name}|${n.filePath}`)))
2084
+ .sort((a, b) => (isNamed(b) ? 1 : 0) - (isNamed(a) ? 1 : 0))
2085
+ .slice(0, 4)
2086
+ .map((n) => {
2087
+ // Typed-bus convention: the runtime target is the candidate class's
2088
+ // Handle/Execute/Consume method — name the exact node, not just the class.
2089
+ let display = n.qualifiedName || n.name;
2090
+ let at = `${n.filePath}:${n.startLine}`;
2091
+ if (keyIsType && n.kind === 'class') {
2092
+ try {
2093
+ const HANDLER_METHODS = /^(handle|handleAsync|execute|executeAsync|consume|consumeAsync|run|__invoke)$/i;
2094
+ const method = cg.getOutgoingEdges(n.id)
2095
+ .filter((e) => e.kind === 'contains')
2096
+ .map((e) => { try {
2097
+ return cg.getNode(e.target);
2098
+ }
2099
+ catch {
2100
+ return null;
2101
+ } })
2102
+ .find((c) => !!c && c.kind === 'method' && HANDLER_METHODS.test(c.name));
2103
+ if (method) {
2104
+ display = `${n.name}.${method.name}`;
2105
+ at = `${method.filePath}:${method.startLine}`;
2106
+ }
2107
+ }
2108
+ catch { /* class without resolvable members — show the class itself */ }
2109
+ }
2110
+ return `\`${display}\` (${at})${isNamed(n) ? ' ← you named this' : ''}`;
2111
+ });
2112
+ return `candidates for key \`${key}\`: ${list.join(', ')}`;
2113
+ }
2114
+ /**
2115
+ * Compact "blast radius" for the entry symbols of an explore result: who
2116
+ * depends on each (callers) and which test files cover it — LOCATIONS ONLY,
2117
+ * no source, so the agent knows what to update / re-verify before editing
2118
+ * without reaching for a separate impact call. Always-on, but skips symbols
2119
+ * that have no dependents (nothing to warn about), and returns '' when none
2120
+ * qualify so a leaf-only exploration stays clean.
2121
+ */
2122
+ buildBlastRadiusSection(cg, subgraph) {
2123
+ const ROOT_CAP = 5; // only the symbols the query actually targeted
2124
+ const FILE_CAP = 4; // caller files listed per symbol before "+N more"
2125
+ const MEANINGFUL = new Set([
2126
+ 'function', 'method', 'class', 'interface', 'struct', 'trait', 'protocol',
2127
+ 'enum', 'type_alias', 'component', 'constant', 'variable', 'property', 'field',
2128
+ ]);
2129
+ const rel = (p) => p.replace(/\\/g, '/');
2130
+ const roots = subgraph.roots
2131
+ .map((id) => subgraph.nodes.get(id))
2132
+ .filter((n) => !!n && MEANINGFUL.has(n.kind))
2133
+ .slice(0, ROOT_CAP);
2134
+ if (roots.length === 0)
2135
+ return '';
2136
+ const entries = [];
2137
+ for (const root of roots) {
2138
+ let callers = [];
2139
+ try {
2140
+ callers = cg.getCallers(root.id);
2141
+ }
2142
+ catch { /* skip this root */ }
2143
+ const seen = new Set();
2144
+ const uniq = [];
2145
+ for (const c of callers) {
2146
+ if (c?.node && !seen.has(c.node.id)) {
2147
+ seen.add(c.node.id);
2148
+ uniq.push(c.node);
2149
+ }
2150
+ }
2151
+ if (uniq.length === 0)
2152
+ continue; // no blast radius → nothing to flag
2153
+ const callerFiles = [...new Set(uniq.map((n) => rel(n.filePath)))];
2154
+ const testFiles = callerFiles.filter((f) => (0, query_utils_1.isTestFile)(f));
2155
+ const nonTest = callerFiles.filter((f) => !(0, query_utils_1.isTestFile)(f));
2156
+ const shown = nonTest.slice(0, FILE_CAP).map((f) => `\`${f}\``).join(', ');
2157
+ const more = nonTest.length > FILE_CAP ? ` +${nonTest.length - FILE_CAP} more` : '';
2158
+ const where = nonTest.length > 0 ? ` in ${shown}${more}` : '';
2159
+ const tests = testFiles.length > 0
2160
+ ? `; tests: ${testFiles.slice(0, FILE_CAP).map((f) => `\`${f}\``).join(', ')}${testFiles.length > FILE_CAP ? ` +${testFiles.length - FILE_CAP}` : ''}`
2161
+ : '; ⚠️ no covering tests found';
2162
+ entries.push(`- \`${root.name}\` (${rel(root.filePath)}:${root.startLine}) — ${uniq.length} caller${uniq.length === 1 ? '' : 's'}${where}${tests}`);
2163
+ }
2164
+ if (entries.length === 0)
2165
+ return '';
2166
+ return [
2167
+ '**Blast radius — what depends on these (update/verify before editing)**',
2168
+ '',
2169
+ ...entries,
2170
+ '',
2171
+ ].join('\n');
2172
+ }
2173
+ /**
2174
+ * Graph-connectivity relevance via Random-Walk-with-Restart (personalized
2175
+ * PageRank) from the query's matched SEED nodes over the call/reference graph.
2176
+ *
2177
+ * This is the ranking signal text search (FTS/bm25) CANNOT provide, and it's
2178
+ * cgraphx's home turf: relevance by STRUCTURE, not words. A file whose
2179
+ * symbols are call-connected to the matched cluster accrues walk mass and
2180
+ * ranks high; a lone TEXT match — e.g. `LensSwitcher.swift` matched the word
2181
+ * "switch" from `switchOrganization`, but calls none of `setUser`/`fetchUser`
2182
+ * — gets only its own restart probability and ranks ~0. Immune to the
2183
+ * tokenization trap that fools term matching, deterministic, no embeddings.
2184
+ *
2185
+ * Undirected adjacency (reachability both ways), restart α=0.25 to the seeds,
2186
+ * power iteration to convergence. Bounded to the already-relevant subgraph, so
2187
+ * it's a few hundred nodes × ~25 iterations — negligible cost.
2188
+ */
2189
+ computeGraphRelevance(nodeIds, edges, seedIds) {
2190
+ const out = new Map();
2191
+ const n = nodeIds.length;
2192
+ if (n === 0)
2193
+ return out;
2194
+ const idx = new Map();
2195
+ for (let i = 0; i < n; i++)
2196
+ idx.set(nodeIds[i], i);
2197
+ const RANK_EDGES = new Set([
2198
+ 'calls', 'references', 'extends', 'implements', 'overrides',
2199
+ 'instantiates', 'returns', 'type_of', 'imports',
2200
+ ]);
2201
+ const adj = Array.from({ length: n }, () => []);
2202
+ for (const e of edges) {
2203
+ if (!RANK_EDGES.has(e.kind))
2204
+ continue;
2205
+ const i = idx.get(e.source);
2206
+ const j = idx.get(e.target);
2207
+ if (i === undefined || j === undefined || i === j)
2208
+ continue;
2209
+ adj[i].push(j);
2210
+ adj[j].push(i); // undirected — reachable either direction
2211
+ }
2212
+ // Restart vector: uniform over seeds present in the candidate set. (Falls
2213
+ // back to uniform-over-all if no seed landed in the set, so we never return
2214
+ // all-zero.)
2215
+ const r = new Array(n).fill(0);
2216
+ let rsum = 0;
2217
+ for (const id of seedIds) {
2218
+ const i = idx.get(id);
2219
+ if (i !== undefined) {
2220
+ r[i] = 1;
2221
+ rsum += 1;
2222
+ }
2223
+ }
2224
+ if (rsum === 0) {
2225
+ for (let i = 0; i < n; i++)
2226
+ r[i] = 1;
2227
+ rsum = n;
2228
+ }
2229
+ for (let i = 0; i < n; i++)
2230
+ r[i] /= rsum;
2231
+ const alpha = 0.25;
2232
+ let s = r.slice();
2233
+ for (let iter = 0; iter < 25; iter++) {
2234
+ const next = new Array(n).fill(0);
2235
+ for (let i = 0; i < n; i++) {
2236
+ const si = s[i];
2237
+ if (si === 0)
2238
+ continue;
2239
+ const d = adj[i].length;
2240
+ if (d === 0) {
2241
+ next[i] += si;
2242
+ continue;
2243
+ } // dangling: keep its mass
2244
+ const share = si / d;
2245
+ for (const j of adj[i])
2246
+ next[j] += share;
2247
+ }
2248
+ for (let i = 0; i < n; i++)
2249
+ s[i] = (1 - alpha) * next[i] + alpha * r[i];
2250
+ }
2251
+ for (let i = 0; i < n; i++)
2252
+ out.set(nodeIds[i], s[i]);
2253
+ return out;
2254
+ }
2255
+ /**
2256
+ * Handle codegraph_explore — deep exploration in a single call
2257
+ *
2258
+ * Strategy: find relevant symbols via graph traversal, group by file,
2259
+ * then read contiguous file sections covering all symbols per file.
2260
+ * This replaces multiple codegraph_node + Read calls.
2261
+ *
2262
+ * Output size is adaptive to project file count via
2263
+ * `getExploreOutputBudget` — see #185 for why a fixed 35k cap was a
2264
+ * tax on small projects while earning its keep on large ones.
2265
+ */
2266
+ async handleExplore(args) {
2267
+ const query = this.validateString(args.query, 'query');
2268
+ if (typeof query !== 'string')
2269
+ return query;
2270
+ const cg = this.getCodeGraph(args.projectPath);
2271
+ const projectRoot = cg.getProjectRoot();
2272
+ // Resolve adaptive output budget from project size. Falls back to the
2273
+ // largest-tier defaults if stats aren't available, which preserves
2274
+ // pre-#185 behavior for callers that hit the rare stats failure.
2275
+ let budget;
2276
+ try {
2277
+ budget = getExploreOutputBudget(cg.getStats().fileCount);
2278
+ }
2279
+ catch {
2280
+ budget = getExploreOutputBudget(Infinity);
2281
+ }
2282
+ const maxFiles = (0, utils_1.clamp)(args.maxFiles || budget.defaultMaxFiles, 1, 20);
2283
+ // Step 1: Find relevant context with generous parameters.
2284
+ // Use a large maxNodes budget — explore has its own 35k char output limit
2285
+ // that prevents context bloat, so more nodes just means better coverage
2286
+ // across entry points (especially for large files like Svelte components).
2287
+ const subgraph = await cg.findRelevantContext(query, {
2288
+ searchLimit: 8,
2289
+ traversalDepth: 3,
2290
+ maxNodes: 200,
2291
+ minScore: 0.2,
2292
+ });
2293
+ if (subgraph.nodes.size === 0) {
2294
+ return this.textResult(`No relevant code found for "${query}"`);
2295
+ }
2296
+ // Graph-aware glue: findRelevantContext builds the subgraph from name/text
2297
+ // search, so a method that BRIDGES named symbols — e.g. App.tsx's
2298
+ // triggerRender, which calls the named triggerUpdate — is never a search hit
2299
+ // and gets missed, forcing the agent to Read the file to trace it. Pull in
2300
+ // the callers/callees of the entry (root) nodes, but ONLY those that live in
2301
+ // files the subgraph already surfaces (where the agent reads to fill gaps),
2302
+ // so we add wiring without dragging in unrelated files. These get an
2303
+ // importance boost below so they survive the per-file cluster budget.
2304
+ const glueNodeIds = new Set();
2305
+ const subgraphFiles = new Set();
2306
+ for (const n of subgraph.nodes.values())
2307
+ subgraphFiles.add(n.filePath);
2308
+ const GLUE_NODE_CAP = 60;
2309
+ for (const rootId of subgraph.roots) {
2310
+ if (glueNodeIds.size >= GLUE_NODE_CAP)
2311
+ break;
2312
+ let neighbors = [];
2313
+ try {
2314
+ neighbors = [
2315
+ ...cg.getCallers(rootId).map(c => c.node),
2316
+ ...cg.getCallees(rootId).map(c => c.node),
2317
+ ];
2318
+ }
2319
+ catch {
2320
+ continue;
2321
+ }
2322
+ for (const nb of neighbors) {
2323
+ if (glueNodeIds.size >= GLUE_NODE_CAP)
2324
+ break;
2325
+ if (subgraph.nodes.has(nb.id))
2326
+ continue;
2327
+ if (!subgraphFiles.has(nb.filePath))
2328
+ continue;
2329
+ subgraph.nodes.set(nb.id, nb);
2330
+ glueNodeIds.add(nb.id);
2331
+ }
2332
+ }
2333
+ // Named-symbol seeding: findRelevantContext is an FTS/text rank, so a query
2334
+ // that's a BAG of symbol names skewed toward one phase (Alamofire: 5 build
2335
+ // terms, each a high-frequency name, vs 3 validate terms) lets the
2336
+ // lower-frequency names fall below the search cut — their definitions, and
2337
+ // whole files (Validation.swift), never get gathered, so they can never
2338
+ // render and the agent Reads them. Resolve EACH named token to its
2339
+ // substantive definition (skip empty stubs + test files, same relevance the
2340
+ // trace endpoint picker uses) and inject it as an entry, so every symbol the
2341
+ // agent explicitly named is in the subgraph and its file is scored.
2342
+ const namedSeedIds = new Set();
2343
+ {
2344
+ const FILE_EXT = /\.(?:java|kt|kts|ts|tsx|js|jsx|mjs|cjs|cs|py|go|rb|php|swift|rs|cpp|cc|cxx|c|h|hpp|scala|lua|dart|vue|svelte|astro)$/i;
2345
+ const CALLABLE = new Set(['method', 'function', 'component', 'constructor']);
2346
+ const isTestPath = (p) => /(^|\/)(tests?|specs?|__tests__|testdata|mocks?|fixtures?)\//i.test(p) || /\.(test|spec)\.[a-z]+$/i.test(p);
2347
+ const bodyLines = (n) => Math.max(0, (n.endLine ?? n.startLine) - n.startLine);
2348
+ const tokens = [...new Set(query.split(/[\s,()[\]]+/)
2349
+ .map((t) => t.replace(FILE_EXT, '').trim())
2350
+ .filter((t) => t.length >= 3 && /^[A-Za-z_$][\w$]*(?:(?:::|\.)[\w$]+)*$/.test(t)))].slice(0, 16);
2351
+ // PascalCase tokens in the query are type/file disambiguators — when the
2352
+ // agent writes "DataRequest task validate", the `task`/`validate` it wants
2353
+ // are DataRequest's, NOT the same-named overloads in Validation.swift /
2354
+ // Concurrency.swift / the abstract base. Used below to bias overloaded
2355
+ // names toward the file/class the query also names. EXCLUDE the project
2356
+ // name (a PascalCase token a user naturally includes) — it names the whole
2357
+ // repo, so biasing toward it just pulls overloads to whichever stack
2358
+ // embeds it, re-burying the rest (#720).
2359
+ const projectNameTokens = cg.getProjectNameTokens();
2360
+ const typeTokens = tokens.filter((o) => /^[A-Z][A-Za-z0-9]{3,}/.test(o) && !projectNameTokens.has((0, query_utils_1.normalizeNameToken)(o)));
2361
+ const inNamedContext = (n) => typeTokens.some((ct) => {
2362
+ const lc = ct.toLowerCase();
2363
+ return n.filePath.toLowerCase().includes(lc) || n.qualifiedName.toLowerCase().includes(lc);
2364
+ });
2365
+ for (const t of tokens) {
2366
+ // Enumerate ALL defs of a bare token via the direct index, not FTS — a
2367
+ // 50+-overload name (tokio `poll`) ranks the wanted def (`Harness::poll`)
2368
+ // below the FTS cut, so findAllSymbols would never see it and the
2369
+ // type-token bias below couldn't pick the harness.rs one. (Same fix as
2370
+ // codegraph_node's findSymbolMatches.) Qualified tokens keep findAllSymbols.
2371
+ const isQual = /[.\/]|::/.test(t);
2372
+ const raw = isQual ? this.findAllSymbols(cg, t).nodes : cg.getNodesByName(t);
2373
+ const cands = raw
2374
+ .filter((n) => CALLABLE.has(n.kind) && !isTestPath(n.filePath))
2375
+ .sort((a, b) => (bodyLines(b) > 1 ? 1 : 0) - (bodyLines(a) > 1 ? 1 : 0) || bodyLines(b) - bodyLines(a));
2376
+ // A specific name (<=3 defs) injects all its defs. An overloaded name
2377
+ // (`validate` = 10, `request` = 44) would flood the subgraph, so inject
2378
+ // only: the overloads whose file/class the query ALSO names (the agent
2379
+ // told us which one it wants — DataRequest's, not Validation.swift's),
2380
+ // capped; else fall back to the single most-substantive def. This is the
2381
+ // explore-side mirror of codegraph_node's overload disambiguation.
2382
+ let picks;
2383
+ if (cands.length <= 3) {
2384
+ picks = cands;
2385
+ }
2386
+ else {
2387
+ const ctx = cands.filter(inNamedContext);
2388
+ picks = ctx.length > 0 ? ctx.slice(0, 4) : cands.slice(0, 1);
2389
+ }
2390
+ for (const n of picks) {
2391
+ if (!subgraph.nodes.has(n.id))
2392
+ subgraph.nodes.set(n.id, n);
2393
+ // Mark as a named seed EVEN IF the FTS gather already had it — being
2394
+ // "named by the agent" is independent of whether search happened to
2395
+ // surface it, and it drives the +50 score, the gate, and the
2396
+ // named-file sort below. (Previously only NEW injections were marked,
2397
+ // so a named symbol FTS already gathered never sorted to the top.)
2398
+ namedSeedIds.add(n.id);
2399
+ }
2400
+ }
2401
+ }
2402
+ // Step 2: Group nodes by file, score by relevance
2403
+ const fileGroups = new Map();
2404
+ const entryNodeIds = new Set([...subgraph.roots, ...namedSeedIds]);
2405
+ // Build a set of nodes directly connected to entry points (depth 1)
2406
+ const connectedToEntry = new Set();
2407
+ for (const edge of subgraph.edges) {
2408
+ if (entryNodeIds.has(edge.source))
2409
+ connectedToEntry.add(edge.target);
2410
+ if (entryNodeIds.has(edge.target))
2411
+ connectedToEntry.add(edge.source);
2412
+ }
2413
+ for (const node of subgraph.nodes.values()) {
2414
+ // Skip import/export nodes — they add noise without information
2415
+ if (node.kind === 'import' || node.kind === 'export')
2416
+ continue;
2417
+ // SECURITY (#383): never render the on-disk source of a config-leaf
2418
+ // (Spring application.{yml,properties} key) — its line is `key = <secret>`,
2419
+ // so whole-file/cluster rendering here would push secrets into context
2420
+ // unbidden. The key still appears in the flow/symbol listing above.
2421
+ if ((0, utils_1.isConfigLeafNode)(node))
2422
+ continue;
2423
+ const group = fileGroups.get(node.filePath) || { nodes: [], score: 0 };
2424
+ group.nodes.push(node);
2425
+ // Score: a NAMED-SEED node (a symbol the agent named that FTS missed, now
2426
+ // injected) is worth far more than a mere reference — its file is where the
2427
+ // answer lives. Without this, an incidental file that name-drops the flow
2428
+ // (Combine.swift references request/task → score 23 from connected nodes)
2429
+ // outranks the file that DEFINES a named symbol (Validation.swift's
2430
+ // `validate` → 10) and steals its render slot. Definition ≫ reference.
2431
+ if (namedSeedIds.has(node.id)) {
2432
+ group.score += 50;
2433
+ }
2434
+ else if (entryNodeIds.has(node.id)) {
2435
+ group.score += 10;
2436
+ }
2437
+ else if (connectedToEntry.has(node.id)) {
2438
+ group.score += 3;
2439
+ }
2440
+ else {
2441
+ group.score += 1;
2442
+ }
2443
+ fileGroups.set(node.filePath, group);
2444
+ }
2445
+ // Only include files that have entry points or nodes directly connected to entry points
2446
+ let relevantFiles = [...fileGroups.entries()].filter(([, group]) => group.score >= 3);
2447
+ // Extract query terms for relevance checking
2448
+ const queryTerms = query.toLowerCase().split(/\s+/).filter(t => t.length >= 3);
2449
+ // Test/spec/icon/i18n file detector — used both for the pre-sort hard
2450
+ // filter (tiny tier) and the comparator deprioritization (all tiers).
2451
+ const isLowValue = (p) => {
2452
+ const lp = p.toLowerCase();
2453
+ return (/\/(tests?|__tests?__|spec)\//.test(lp) ||
2454
+ /_test\.go$/.test(lp) ||
2455
+ /(?:^|\/)test_[^/]+\.py$/.test(lp) ||
2456
+ /_test\.py$/.test(lp) ||
2457
+ /_spec\.rb$/.test(lp) ||
2458
+ /_test\.rb$/.test(lp) ||
2459
+ /\.(test|spec)\.[jt]sx?$/.test(lp) ||
2460
+ /(test|spec|tests)\.(java|kt|scala)$/.test(lp) ||
2461
+ /(tests?|spec)\.cs$/.test(lp) ||
2462
+ /tests?\.swift$/.test(lp) ||
2463
+ /_test\.dart$/.test(lp) ||
2464
+ /\bicons?\b/.test(lp) ||
2465
+ /\bi18n\b/.test(lp));
2466
+ };
2467
+ // Hard-exclude test/spec files (ALL tiers, not just tiny). One slipped test
2468
+ // file dominates the per-file budget on small repos (cobra's `command_test.go`
2469
+ // displaced `args.go`) AND wastes budget on large ones (Django's
2470
+ // `custom_lookups/tests.py` ate ~2.3 KB of the 28 KB cap, crowding out the
2471
+ // SQLCompiler mechanism the agent then Read). A test file almost never answers
2472
+ // an architecture question. Skip when the query itself is about tests — the
2473
+ // legitimate "explore the tests" case — and only cut if ≥2 non-test candidates
2474
+ // remain (else tests are the only signal for this area).
2475
+ {
2476
+ const queryMentionsTests = /\b(test|tests|testing|spec|verify|verifies)\b/i.test(query);
2477
+ if (!queryMentionsTests) {
2478
+ const nonLow = relevantFiles.filter(([p]) => !isLowValue(p));
2479
+ if (nonLow.length >= 2) {
2480
+ relevantFiles = nonLow;
2481
+ }
2482
+ }
2483
+ }
2484
+ // Secondary signal: how many DISTINCT query terms each file matches (path +
2485
+ // symbol names). Kept only as a tiebreak — the PRIMARY relevance is graph
2486
+ // connectivity below. (Term counting alone tied the real central file with
2487
+ // incidental same-word matches; it's a weak text signal, not the ranker.)
2488
+ const uniqueQueryTerms = [...new Set(queryTerms)].filter(t => t.length >= 3);
2489
+ const fileTermHits = new Map();
2490
+ for (const [fp, group] of relevantFiles) {
2491
+ const hay = fp.toLowerCase() + ' ' + group.nodes.map(n => n.name.toLowerCase()).join(' ');
2492
+ let hits = 0;
2493
+ for (const t of uniqueQueryTerms)
2494
+ if (hay.includes(t))
2495
+ hits++;
2496
+ fileTermHits.set(fp, hits);
2497
+ }
2498
+ // PRIMARY relevance: graph connectivity (Random-Walk-with-Restart from the
2499
+ // matched seeds — see computeGraphRelevance). Aggregate each file's nodes'
2500
+ // walk mass. This is the signal text search lacks: the real cluster
2501
+ // (org-user.storage.ts, call-connected to the matches) accrues mass; a lone
2502
+ // text match (LensSwitcher.swift, matched "switch" but calls nothing in the
2503
+ // flow) gets only its restart probability → ~0, and is dropped by the gate.
2504
+ const nodeRwr = this.computeGraphRelevance([...subgraph.nodes.keys()], subgraph.edges, entryNodeIds);
2505
+ const fileGraphScore = new Map();
2506
+ for (const node of subgraph.nodes.values()) {
2507
+ fileGraphScore.set(node.filePath, (fileGraphScore.get(node.filePath) ?? 0) + (nodeRwr.get(node.id) ?? 0));
2508
+ }
2509
+ const maxGraph = Math.max(0, ...fileGraphScore.values());
2510
+ // Central file(s): the 1-2 most graph-central files that also match the
2511
+ // query textually (so a connected hub-utility with no term match isn't
2512
+ // mistaken for the subject). The heart of the answer — they earn the larger
2513
+ // WHOLE-FILE ceiling below (a god-file central file still exceeds it and
2514
+ // falls to generous full-method sectioning — never a whole dump).
2515
+ const centralFiles = new Set([...fileGraphScore.entries()]
2516
+ .filter(([fp, g]) => g > 0 && (fileTermHits.get(fp) ?? 0) >= 1)
2517
+ .sort((a, b) => b[1] - a[1] || (fileTermHits.get(b[0]) ?? 0) - (fileTermHits.get(a[0]) ?? 0))
2518
+ .slice(0, 2)
2519
+ .map(([f]) => f));
2520
+ // Files that DEFINE a symbol the agent named (or a subgraph root). These are
2521
+ // the highest-relevance files there are — the agent asked for them by name —
2522
+ // so the connectivity gate below must never drop them, even when their RWR
2523
+ // mass is low (a leaf family file like codec.ts is call-connected to little
2524
+ // but is exactly what the agent queried). Without this protection the gate
2525
+ // prunes a named file and the agent Reads it back.
2526
+ const entryFiles = new Set();
2527
+ for (const id of entryNodeIds) {
2528
+ const n = subgraph.nodes.get(id);
2529
+ if (n)
2530
+ entryFiles.add(n.filePath);
2531
+ }
2532
+ // Relevance gate (so the generous budget is a CEILING, not a target): keep a
2533
+ // file only if it is STRUCTURALLY relevant by ANY of:
2534
+ // - graph score within a fraction of the top (it's on/near the flow), OR
2535
+ // - central (a query entry-point lives here), OR
2536
+ // - it DEFINES a symbol the agent named (entryFiles), OR
2537
+ // - it matches >= 2 DISTINCT named query terms — a strong text signal that
2538
+ // the agent is asking about this file even when nothing calls it (codec.ts:
2539
+ // the agent named `encode`/`Codec`/`JsonCodec`, all leaf classes with zero
2540
+ // RWR mass — graph alone wrongly drops it).
2541
+ // A lone text match on one shared word (LensSwitcher: term=1, g~0) is still
2542
+ // dropped, so the budget never fills with incidental files. Guarded so it
2543
+ // never prunes below 2.
2544
+ if (maxGraph > 0) {
2545
+ const gated = relevantFiles.filter(([fp]) => (fileGraphScore.get(fp) ?? 0) >= maxGraph * 0.06
2546
+ || centralFiles.has(fp)
2547
+ || entryFiles.has(fp)
2548
+ || (fileTermHits.get(fp) ?? 0) >= 2);
2549
+ if (gated.length >= 2)
2550
+ relevantFiles = gated;
2551
+ }
2552
+ // Sort files: graph-central first, then distinct-term match, then the
2553
+ // existing low-value/generated/score tiebreaks.
2554
+ // Files that DEFINE a symbol the agent NAMED. These sort first — ahead of
2555
+ // graph connectivity — because the agent asked for them by name. Without
2556
+ // this, a named leaf override reached only by dynamic dispatch (Alamofire's
2557
+ // `DataRequest.task`/`validate`, low RWR mass) sorts below the high-
2558
+ // connectivity abstract base (`Request.swift`) and the same-named overloads
2559
+ // in other files (`Validation.swift`), falls outside the budget, and the
2560
+ // agent Reads it. The named file is the answer — rank it at the top.
2561
+ const namedSeedFiles = new Set();
2562
+ for (const id of namedSeedIds) {
2563
+ const n = subgraph.nodes.get(id);
2564
+ if (n)
2565
+ namedSeedFiles.add(n.filePath);
2566
+ }
2567
+ // Multi-term corroboration tier: a file that is BOTH (a) an entry/central file
2568
+ // (a search root, named seed, or graph-central hub — i.e. structurally part of
2569
+ // the answer) AND (b) matched by ≥2 DISTINCT query terms must not be buried by
2570
+ // graph-centrality mass that accrued to a denser-but-off-topic cluster. In a
2571
+ // cross-layer monorepo (an API server alongside a much larger, internally dense
2572
+ // frontend that mirrors the same domain words) the Random-Walk-with-Restart mass
2573
+ // — seeded from text matches that skew to the bigger layer — floats hits=0
2574
+ // frontend files above the hits=2/3 backend service that IS the answer (its many
2575
+ // callers don't help: it's call-isolated from the frontend seed cluster). The
2576
+ // entry/central GUARD keeps this safe: an INCIDENTAL multi-term file that is
2577
+ // neither entry nor central (a type/util file that matches "element"+x but isn't
2578
+ // the flow) is NOT promoted, so it can't displace the graph-central answer file
2579
+ // (hits=1) the way a blunt hits-only tier would. Single-layer repos with one
2580
+ // cluster are unaffected (no competing mass). Set CGRAPHX_RANK_NO_MULTITERM=1
2581
+ // to disable.
2582
+ const MULTITERM_OFF = process.env.CGRAPHX_RANK_NO_MULTITERM === '1';
2583
+ const isCorroborated = (fp) => !MULTITERM_OFF &&
2584
+ (fileTermHits.get(fp) ?? 0) >= 2 &&
2585
+ (entryFiles.has(fp) || centralFiles.has(fp));
2586
+ const sortedFiles = relevantFiles.sort((a, b) => {
2587
+ const aPath = a[0].toLowerCase();
2588
+ const bPath = b[0].toLowerCase();
2589
+ // Agent-named files first (it asked for a symbol defined here by name).
2590
+ const aNamed = namedSeedFiles.has(a[0]) ? 1 : 0;
2591
+ const bNamed = namedSeedFiles.has(b[0]) ? 1 : 0;
2592
+ if (aNamed !== bNamed)
2593
+ return bNamed - aNamed;
2594
+ // Corroborated (entry/central + ≥2 terms) tier, above the graph signal.
2595
+ const aCorr = isCorroborated(a[0]) ? 1 : 0;
2596
+ const bCorr = isCorroborated(b[0]) ? 1 : 0;
2597
+ if (aCorr !== bCorr)
2598
+ return bCorr - aCorr;
2599
+ // Graph connectivity is the next key (small epsilon so near-ties fall
2600
+ // through to the text signal rather than coin-flipping on float noise).
2601
+ const aG = fileGraphScore.get(a[0]) ?? 0;
2602
+ const bG = fileGraphScore.get(b[0]) ?? 0;
2603
+ if (Math.abs(aG - bG) > maxGraph * 0.01)
2604
+ return bG - aG;
2605
+ const aHits = fileTermHits.get(a[0]) ?? 0;
2606
+ const bHits = fileTermHits.get(b[0]) ?? 0;
2607
+ if (aHits !== bHits)
2608
+ return bHits - aHits;
2609
+ const aLow = isLowValue(aPath);
2610
+ const bLow = isLowValue(bPath);
2611
+ if (aLow !== bLow)
2612
+ return aLow ? 1 : -1;
2613
+ // Deprioritize generated source (.pb.go / .pulsar.go / _mocks.go / …) —
2614
+ // the agent rarely needs to see the protobuf scaffold or gomock output
2615
+ // when asking about the actual flow, and dumping their bodies inflates
2616
+ // the response (the cosmos Q3 explore otherwise leads with
2617
+ // `expected_keepers_mocks.go`, displacing the real `tally.go` content
2618
+ // and forcing the agent to Read tally.go anyway).
2619
+ const aGen = (0, generated_detection_1.isGeneratedFile)(a[0]);
2620
+ const bGen = (0, generated_detection_1.isGeneratedFile)(b[0]);
2621
+ if (aGen !== bGen)
2622
+ return aGen ? 1 : -1;
2623
+ if (a[1].score !== b[1].score)
2624
+ return b[1].score - a[1].score;
2625
+ return b[1].nodes.length - a[1].nodes.length;
2626
+ });
2627
+ // Step 3: Build relationship map
2628
+ const lines = [
2629
+ `**Exploration: ${query}**`,
2630
+ '',
2631
+ `Found ${subgraph.nodes.size} symbols across ${fileGroups.size} files.`,
2632
+ '',
2633
+ ];
2634
+ // Blast radius (always-on, compact): for the entry symbols, who depends on
2635
+ // them + which tests cover them — locations only, no source — so the agent
2636
+ // knows what to update/verify before editing without a separate call.
2637
+ const blastRadius = this.buildBlastRadiusSection(cg, subgraph);
2638
+ if (blastRadius)
2639
+ lines.push(blastRadius);
2640
+ // Relationship map — show how symbols connect
2641
+ const significantEdges = subgraph.edges.filter(e => e.kind !== 'contains' // skip contains — it's implied by file grouping
2642
+ );
2643
+ if (budget.includeRelationships && significantEdges.length > 0) {
2644
+ lines.push('**Relationships**');
2645
+ lines.push('');
2646
+ // Group edges by kind for readability
2647
+ const byKind = new Map();
2648
+ for (const edge of significantEdges) {
2649
+ const sourceNode = subgraph.nodes.get(edge.source);
2650
+ const targetNode = subgraph.nodes.get(edge.target);
2651
+ if (!sourceNode || !targetNode)
2652
+ continue;
2653
+ const group = byKind.get(edge.kind) || [];
2654
+ group.push({ source: sourceNode.name, target: targetNode.name });
2655
+ byKind.set(edge.kind, group);
2656
+ }
2657
+ for (const [kind, edges] of byKind) {
2658
+ const cap = budget.maxEdgesPerRelationshipKind;
2659
+ const shown = edges.slice(0, cap);
2660
+ lines.push(`**${kind}:**`);
2661
+ for (const e of shown) {
2662
+ lines.push(`- ${e.source} → ${e.target}`);
2663
+ }
2664
+ if (edges.length > cap) {
2665
+ lines.push(`- ... and ${edges.length - cap} more`);
2666
+ }
2667
+ lines.push('');
2668
+ }
2669
+ }
2670
+ // Step 4: Read contiguous file sections
2671
+ // Compute the flow spine once — used both to prepend the Flow section (below)
2672
+ // and to gate adaptive source sizing: files on the spine get full source,
2673
+ // off-spine peers skeletonize.
2674
+ const flow = this.buildFlowFromNamedSymbols(cg, query);
2675
+ // Polymorphic-sibling detector for adaptive sizing. A class that implements/
2676
+ // extends a supertype shared by >= MIN_SIBLINGS classes is one of many
2677
+ // INTERCHANGEABLE implementations (OkHttp's 14 `: Interceptor` classes —
2678
+ // showing one + the rest as signatures is enough), as opposed to a DISTINCT
2679
+ // pipeline step (Excalidraw's `renderStaticScene`, which shares no supertype and
2680
+ // must stay full or the agent loses real content). Only off-spine sibling files
2681
+ // skeletonize; distinct steps and on-spine files keep full source. Cache
2682
+ // supertype→(has ≥N implementers) so this stays a handful of edge queries.
2683
+ const MIN_SIBLINGS = 3;
2684
+ const siblingSuper = new Map();
2685
+ const isPolymorphicSibling = (nodes) => {
2686
+ for (const n of nodes) {
2687
+ for (const e of cg.getOutgoingEdges(n.id)) {
2688
+ if (e.kind !== 'implements' && e.kind !== 'extends')
2689
+ continue;
2690
+ let many = siblingSuper.get(e.target);
2691
+ if (many === undefined) {
2692
+ many = cg.getIncomingEdges(e.target)
2693
+ .filter((x) => x.kind === 'implements' || x.kind === 'extends').length >= MIN_SIBLINGS;
2694
+ siblingSuper.set(e.target, many);
2695
+ }
2696
+ if (many)
2697
+ return true;
2698
+ }
2699
+ }
2700
+ return false;
2701
+ };
2702
+ // A file that DEFINES a polymorphic supertype (a class/interface with ≥
2703
+ // MIN_SIBLINGS implementers) AND co-locates its subclasses is a redundant
2704
+ // "family" file — Django's compiler.py holds `SQLCompiler` + its 4 subclasses
2705
+ // (SQLInsert/Update/Delete/AggregateCompiler) in 2,266 lines. Such files are
2706
+ // huge and read-anyway, so they should STILL skeletonize even when the agent
2707
+ // named a method in them: a full one eats ~6.5K of the explore budget (Django
2708
+ // is pinned at the 28K cap, truncating), starving the sibling files the agent
2709
+ // then Reads. This flag OVERRIDES the named-callable spare below — it does NOT
2710
+ // by itself spare a file. (OkHttp's RealCall implements the `Lockable` mixin
2711
+ // but defines no ≥3-impl supertype, so the named spare keeps it full.)
2712
+ const superMany = new Map();
2713
+ const definesPolymorphicSupertype = (nodes) => {
2714
+ for (const n of nodes) {
2715
+ if (n.kind !== 'class' && n.kind !== 'interface' && n.kind !== 'struct'
2716
+ && n.kind !== 'trait' && n.kind !== 'protocol' && n.kind !== 'type_alias')
2717
+ continue;
2718
+ let many = superMany.get(n.id);
2719
+ if (many === undefined) {
2720
+ many = cg.getIncomingEdges(n.id)
2721
+ .filter((x) => x.kind === 'implements' || x.kind === 'extends').length >= MIN_SIBLINGS;
2722
+ superMany.set(n.id, many);
2723
+ }
2724
+ if (many)
2725
+ return true;
2726
+ }
2727
+ return false;
2728
+ };
2729
+ lines.push('**Source Code**');
2730
+ lines.push('');
2731
+ lines.push('> The code below is the **verbatim, current on-disk source** of these files — re-read from disk on this call and line-numbered, byte-for-byte identical to what the Read tool returns. It is NOT a summary, outline, or stale cache. Treat each block as a Read you have already performed: do not Read a file shown here.');
2732
+ lines.push('');
2733
+ let totalChars = lines.join('\n').length;
2734
+ let filesIncluded = 0;
2735
+ let anyFileTrimmed = false;
2736
+ for (const [filePath, group] of sortedFiles) {
2737
+ if (filesIncluded >= maxFiles)
2738
+ break;
2739
+ // A file DEFINES a named/spine symbol (the answer) vs merely references the
2740
+ // flow. Past 90% budget, stop pulling INCIDENTAL files — but keep scanning
2741
+ // for necessary ones, which render even past the cap (bounded by maxFiles).
2742
+ // Without this `continue` (was an unconditional `break`), the loop stopped
2743
+ // after the build + validators-exec files and never reached the ranked-in
2744
+ // validate-logic file (Alamofire's Validation.swift).
2745
+ const fileNecessary = group.nodes.some(n => entryNodeIds.has(n.id) || flow.pathNodeIds.has(n.id) || flow.uniqueNamedNodeIds.has(n.id));
2746
+ if (!fileNecessary && totalChars > budget.maxOutputChars * 0.9)
2747
+ continue;
2748
+ const absPath = (0, utils_1.validatePathWithinRoot)(projectRoot, filePath);
2749
+ if (!absPath || !(0, fs_1.existsSync)(absPath))
2750
+ continue;
2751
+ let fileContent;
2752
+ try {
2753
+ fileContent = (0, fs_1.readFileSync)(absPath, 'utf-8');
2754
+ }
2755
+ catch {
2756
+ continue;
2757
+ }
2758
+ const fileLines = fileContent.split('\n');
2759
+ const lang = group.nodes[0]?.language || '';
2760
+ // Adaptive sizing (CGRAPHX_ADAPTIVE_EXPLORE, default on): collapse a file
2761
+ // to a per-symbol view when it's a redundant member of a polymorphic family.
2762
+ // Engages iff ALL hold:
2763
+ // 1. a flow spine exists,
2764
+ // 2. no symbol in the file is on that spine (it's not the mechanism path),
2765
+ // 3. it IS a polymorphic sibling (≥ MIN_SIBLINGS impls of a shared supertype),
2766
+ // 4. it is NOT SPARED, where a file is spared iff the agent named a
2767
+ // (near-)UNIQUE callable in it (`getResponseWithInterceptorChain`, 1 def →
2768
+ // keep RealCall.kt full) UNLESS the file DEFINES the family supertype (a
2769
+ // base+subclasses "family" file like Django's compiler.py — collapse it).
2770
+ // Uniqueness matters: `as_sql` has 110 defs across every Compiler/Expression
2771
+ // subclass; naming it must NOT keep every backend variant + test file full
2772
+ // and flood the budget. That's why the spare reads uniqueNamedNodeIds.
2773
+ // Within a collapsed file the render is PER-SYMBOL (condition B): a method the
2774
+ // agent NAMED or that's on the spine is shown with its FULL body (so the agent
2775
+ // doesn't Read the file back for it — Django's SQLCompiler.execute_sql/as_sql);
2776
+ // every other symbol is just its signature. So the base mechanism survives while
2777
+ // the file's other ~80 symbols + the redundant subclasses collapse to one line each.
2778
+ const spareNamed = group.nodes.some(n => flow.uniqueNamedNodeIds.has(n.id));
2779
+ const fileDefinesSuper = definesPolymorphicSupertype(group.nodes);
2780
+ const spared = spareNamed && !fileDefinesSuper;
2781
+ const CALLABLE_BODY = new Set(['method', 'function', 'constructor', 'component']);
2782
+ const hasSpineNode = group.nodes.some(n => flow.pathNodeIds.has(n.id));
2783
+ // On-spine god-file: the flow path runs THROUGH this file, but it also holds
2784
+ // many OTHER named methods, and rendering all of them in full blows the
2785
+ // per-file budget and starves the other flow files (Alamofire: the agent
2786
+ // names ~7 Session.swift methods — the build spine PLUS off-path
2787
+ // task/didCompleteTask — far past the whole response budget). Engage the
2788
+ // per-symbol view to keep the SPINE full and collapse the off-path named
2789
+ // methods to signatures. Only when there IS off-path content to shed —
2790
+ // otherwise the spine is irreducible (a sequential flow has no redundancy),
2791
+ // so leave it to the normal full render.
2792
+ const namedBodyChars = group.nodes
2793
+ .filter(n => CALLABLE_BODY.has(n.kind) && (flow.pathNodeIds.has(n.id) || flow.uniqueNamedNodeIds.has(n.id)))
2794
+ .reduce((s, n) => s + fileLines.slice(n.startLine - 1, n.endLine).join('\n').length, 0);
2795
+ const onSpineGodFile = hasSpineNode
2796
+ && namedBodyChars > budget.maxCharsPerFile
2797
+ && group.nodes.some(n => CALLABLE_BODY.has(n.kind) && flow.uniqueNamedNodeIds.has(n.id) && !flow.pathNodeIds.has(n.id));
2798
+ if (adaptiveExploreEnabled() && flow.pathNodeIds.size > 0
2799
+ && (onSpineGodFile || (!hasSpineNode && isPolymorphicSibling(group.nodes) && !spared))) {
2800
+ const syms = group.nodes
2801
+ .filter(n => n.kind !== 'import' && n.kind !== 'export' && n.startLine > 0)
2802
+ .sort((a, b) => a.startLine - b.startLine);
2803
+ // Pass 1: choose which symbols get a FULL body, by priority, greedily within
2804
+ // a per-file body cap — so one huge family file can't body every named method
2805
+ // and crowd out the other flow files (Django's query.py). A symbol earns a
2806
+ // body if it's on-spine, or UNIQUELY named (`SQLCompiler.execute_sql`), or a
2807
+ // co-named method WHEN this file DEFINES the family supertype (so the base
2808
+ // `SQLCompiler.as_sql` body shows, but the 110 leaf `as_sql` overrides — and
2809
+ // OkHttp's 5 `intercept`s if the agent names `intercept` — stay signatures).
2810
+ const prio = (n) => !CALLABLE_BODY.has(n.kind) ? 99
2811
+ : flow.pathNodeIds.has(n.id) ? 0
2812
+ : flow.uniqueNamedNodeIds.has(n.id) ? 1
2813
+ : (fileDefinesSuper && flow.namedNodeIds.has(n.id)) ? 2 : 99;
2814
+ // One ~250-line WINDOW per file. syms are taken by priority (spine first,
2815
+ // then uniquely-named, then family-base), and the cap applies to ALL of
2816
+ // them — including the spine — so a big-spine god-file (tokio's worker.rs:
2817
+ // run→run_task→next_task→steal_work) can't eat the whole response and
2818
+ // starve the co-flow file (harness.rs's poll). The native agent windows
2819
+ // such a file too (~190 lines at a time), so this mimics, not truncates.
2820
+ // Always emit ≥1 (never an empty section).
2821
+ const bodyCap = budget.maxCharsPerFile * 1.5;
2822
+ const bodyIds = new Set();
2823
+ let bodyChars = 0;
2824
+ for (const n of syms.filter(n => prio(n) < 99 && n.endLine >= n.startLine).sort((a, b) => prio(a) - prio(b))) {
2825
+ const sz = fileLines.slice(n.startLine - 1, n.endLine).join('\n').length;
2826
+ if (bodyChars + sz > bodyCap && bodyIds.size > 0)
2827
+ continue;
2828
+ bodyIds.add(n.id);
2829
+ bodyChars += sz;
2830
+ }
2831
+ // Pass 2: render in line order — full body for chosen symbols, else the
2832
+ // signature line (capped, with a "+N more" tail so the structure map of a
2833
+ // god-file doesn't itself bloat the budget).
2834
+ const skel = [];
2835
+ let coveredUntil = 0; // skip symbols already inside an emitted body
2836
+ let sigCount = 0, sigDropped = 0;
2837
+ const SIG_MAX = Math.max(12, budget.maxSymbolsInFileHeader * 2);
2838
+ for (const n of syms) {
2839
+ if (n.startLine <= coveredUntil)
2840
+ continue;
2841
+ if (bodyIds.has(n.id)) {
2842
+ const end = n.endLine;
2843
+ const body = fileLines.slice(n.startLine - 1, end).join('\n');
2844
+ skel.push(exploreLineNumbersEnabled() ? numberSourceLines(body, n.startLine) : body);
2845
+ coveredUntil = end;
2846
+ }
2847
+ else {
2848
+ // Elide the body, emit the signature. node.startLine can point at a
2849
+ // decorator/annotation, so scan forward for the line that names the symbol.
2850
+ let lineNo = n.startLine;
2851
+ for (let k = 0; k < 4; k++) {
2852
+ if ((fileLines[n.startLine - 1 + k] || '').includes(n.name)) {
2853
+ lineNo = n.startLine + k;
2854
+ break;
2855
+ }
2856
+ }
2857
+ if (lineNo <= coveredUntil)
2858
+ continue;
2859
+ if (sigCount >= SIG_MAX) {
2860
+ sigDropped++;
2861
+ continue;
2862
+ }
2863
+ const sig = (fileLines[lineNo - 1] || '').trim();
2864
+ if (sig) {
2865
+ skel.push(exploreLineNumbersEnabled() ? `${lineNo}\t${sig}` : sig);
2866
+ sigCount++;
2867
+ }
2868
+ }
2869
+ }
2870
+ if (sigDropped > 0)
2871
+ skel.push(`… +${sigDropped} more (signatures elided)`);
2872
+ if (skel.length > 0) {
2873
+ const names = [...new Set(group.nodes.filter(n => n.kind !== 'import' && n.kind !== 'export').map(n => n.name))]
2874
+ .slice(0, budget.maxSymbolsInFileHeader).join(', ');
2875
+ // Steer the agent to codegraph_explore for an elided body — NEVER to
2876
+ // Read. The old "Read for more" / "Read for a full body" tags invited
2877
+ // a Read of the very file just skeletonized; on a central, wanted file
2878
+ // (Session.swift, DataRequest.swift) that fired an over-investigation
2879
+ // spiral (the agent Read the skeletonized file, then kept digging).
2880
+ // CLAUDE.md: explore output must never tell the agent to Read.
2881
+ const tag = bodyIds.size > 0
2882
+ ? 'focused (the methods you named in full, the rest as signatures — codegraph_explore a signature by name for its body; do NOT Read)'
2883
+ : 'skeleton (signatures only — codegraph_explore a name for its full body; do NOT Read)';
2884
+ lines.push(fileSectionHeader(filePath, `${names} · ${tag}`), '', '```' + lang, skel.join('\n'), '```', '');
2885
+ totalChars += skel.join('\n').length + 120;
2886
+ filesIncluded++;
2887
+ continue;
2888
+ }
2889
+ }
2890
+ // Whole-file rule: if a relevant file is small enough to afford, return it
2891
+ // ENTIRELY instead of clustering. Clustering exists to tame god-files
2892
+ // (App.tsx ~13k lines); on a ~134-line component a cluster is a lossy
2893
+ // subset of a file the agent will just Read in full anyway — costing a
2894
+ // round-trip and a re-read every later turn. Reserve clustering for files
2895
+ // too big to ship whole. Still bounded by the total maxOutputChars check.
2896
+ //
2897
+ // CENTRAL files (where the query's entry points live) get a larger — but
2898
+ // bounded — ceiling: they're the heart of the answer, the file(s) the agent
2899
+ // would Read whole, so a genuinely small one comes back whole rather than as
2900
+ // thin clusters. A LARGE central file (the 791-line org-user store) exceeds
2901
+ // the ceiling and falls through to sectioning/clustering below — full method
2902
+ // bodies + signatures — so we never dump (or overflow on) a whole god-file.
2903
+ const isCentralFile = centralFiles.has(filePath);
2904
+ // Central files get a slightly larger whole-file window than peripheral ones,
2905
+ // but a TIGHT one (~1.5× the per-file cap): the native read of a central file
2906
+ // is a ~150–250 line orientation window, NOT the whole file. A flat "whole
2907
+ // central file" both overflowed the inline cap AND starved the co-flow files
2908
+ // (worker.rs ate the budget, dropping harness.rs's poll). A larger central
2909
+ // file falls through to per-method windowing/clustering below.
2910
+ const WHOLE_FILE_MAX_LINES = isCentralFile ? 280 : 220;
2911
+ const WHOLE_FILE_MAX_CHARS = isCentralFile
2912
+ ? Math.min(Math.max(0, budget.maxOutputChars - totalChars - 200), Math.round(budget.maxCharsPerFile * 1.5))
2913
+ : budget.maxCharsPerFile * 3;
2914
+ if (fileLines.length <= WHOLE_FILE_MAX_LINES && fileContent.length <= WHOLE_FILE_MAX_CHARS) {
2915
+ const body = fileContent.replace(/\n+$/, '');
2916
+ let wholeSection = exploreLineNumbersEnabled() ? numberSourceLines(body, 1) : body;
2917
+ const uniqSymbols = [...new Set(group.nodes
2918
+ .filter(n => n.kind !== 'import' && n.kind !== 'export')
2919
+ .map(n => `${n.name}(${n.kind})`))];
2920
+ const headerNames = uniqSymbols.slice(0, budget.maxSymbolsInFileHeader);
2921
+ const omitted = uniqSymbols.length - headerNames.length;
2922
+ const wholeHeader = fileSectionHeader(filePath, omitted > 0 ? `${headerNames.join(', ')}, +${omitted} more` : headerNames.join(', '));
2923
+ if (!fileNecessary && totalChars + wholeSection.length + 200 > budget.maxOutputChars) {
2924
+ // Don't slice a whole file mid-method: an incidental file that doesn't
2925
+ // fit is skipped; a necessary one (below) renders in full. Half a file
2926
+ // forces the Read this is meant to prevent.
2927
+ anyFileTrimmed = true;
2928
+ continue;
2929
+ }
2930
+ lines.push(wholeHeader, '', '```' + lang, wholeSection, '```', '');
2931
+ totalChars += wholeSection.length + 200;
2932
+ filesIncluded++;
2933
+ continue;
2934
+ }
2935
+ // Cluster nearby symbols to avoid reading huge gaps between distant symbols.
2936
+ // Sort by start line, then merge overlapping/adjacent ranges (within the
2937
+ // adaptive gap threshold). Include both node ranges AND edge source
2938
+ // locations so template sections with component usages/calls are
2939
+ // covered (not just script block symbols).
2940
+ //
2941
+ // Each range carries an `importance` score so we can rank clusters
2942
+ // when the per-file budget forces us to drop some: entry-point nodes
2943
+ // are worth 10, directly-connected nodes 3, peripheral nodes 1, and
2944
+ // bare edge-source lines 2 (less than a connected node but more than
2945
+ // a peripheral one — they hint at a reference but aren't a definition).
2946
+ // Container kinds whose body can span most/all of a file. When such a
2947
+ // node covers most of the file we drop it from the ranges: keeping it
2948
+ // would merge every method inside it into one giant cluster spanning
2949
+ // the whole file, which then tail-trims down to just the container's
2950
+ // opening lines (its header/declarations) and buries the methods the
2951
+ // query actually asked about (#185 follow-up — Session.swift in
2952
+ // Alamofire is the canonical case: the `Session` class spans ~1,400
2953
+ // lines). We want the granular symbols inside, not the envelope.
2954
+ const ENVELOPE_KINDS = new Set(['file', 'module', 'class', 'struct', 'interface', 'enum', 'namespace', 'protocol', 'trait', 'component']);
2955
+ // Cluster from this file's gathered nodes PLUS any callable the agent NAMED that
2956
+ // lives here. Explore's relevance gather can miss a named method def in a huge
2957
+ // non-sibling file — Django's query.py is 3,040 lines and `_fetch_all` (L2237)
2958
+ // was gathered only as call-reference edges, never as a def, so it formed no
2959
+ // cluster and the agent Read it back. Inject named defs directly and rank them
2960
+ // ABOVE connected/glue nodes (importance 9) so their cluster wins the per-file
2961
+ // budget — the agent explicitly asked for these symbols.
2962
+ const rangeNodes = new Map();
2963
+ for (const n of group.nodes)
2964
+ if (n.startLine > 0 && n.endLine > 0)
2965
+ rangeNodes.set(n.id, n);
2966
+ for (const id of flow.namedNodeIds) {
2967
+ if (rangeNodes.has(id))
2968
+ continue;
2969
+ const n = cg.getNode(id);
2970
+ if (n && n.filePath === filePath && n.startLine > 0 && n.endLine > 0)
2971
+ rangeNodes.set(id, n);
2972
+ }
2973
+ const ranges = [...rangeNodes.values()]
2974
+ // Drop whole-file envelope nodes (containers covering >50% of the file).
2975
+ .filter(n => !(ENVELOPE_KINDS.has(n.kind) && (n.endLine - n.startLine + 1) > fileLines.length * 0.5))
2976
+ .map(n => {
2977
+ let importance = 1;
2978
+ if (entryNodeIds.has(n.id))
2979
+ importance = 10;
2980
+ else if (flow.namedNodeIds.has(n.id))
2981
+ importance = 9; // agent named it → keep its cluster
2982
+ else if (glueNodeIds.has(n.id))
2983
+ importance = 6; // bridging caller/callee of an entry
2984
+ else if (connectedToEntry.has(n.id))
2985
+ importance = 3;
2986
+ // On the rendered call-path spine? That IS the flow answer — its cluster
2987
+ // must never be dropped by the per-file budget (n8n's huge workflow-execute.ts:
2988
+ // processRunExecutionData, the named flow ENTRY at L1562, is a large
2989
+ // low-density method that lost the budget to denser blocks and got cut, so
2990
+ // the agent Read it back — the very thing explore exists to prevent).
2991
+ return { start: n.startLine, end: n.endLine, name: n.name, kind: n.kind, importance, spine: flow.pathNodeIds.has(n.id), spineCallLine: flow.spineCallSites.get(n.id) };
2992
+ });
2993
+ // Add edge source locations in this file — captures template references
2994
+ // (component usages, event handlers) that aren't nodes themselves.
2995
+ // Query edges directly from the DB (not just the subgraph) because BFS
2996
+ // traversal may have pruned template reference targets due to node budget.
2997
+ const edgeLines = new Set(); // dedup by "line:name"
2998
+ for (const node of group.nodes) {
2999
+ const outgoing = cg.getOutgoingEdges(node.id);
3000
+ for (const edge of outgoing) {
3001
+ if (!edge.line || edge.line <= 0 || edge.kind === 'contains')
3002
+ continue;
3003
+ const key = `${edge.line}:${edge.target}`;
3004
+ if (edgeLines.has(key))
3005
+ continue;
3006
+ edgeLines.add(key);
3007
+ // Look up target name from subgraph first, fall back to edge kind
3008
+ const targetNode = subgraph.nodes.get(edge.target);
3009
+ const targetName = targetNode?.name ?? edge.kind;
3010
+ ranges.push({ start: edge.line, end: edge.line, name: targetName, kind: edge.kind, importance: 2, spine: false });
3011
+ }
3012
+ }
3013
+ ranges.sort((a, b) => a.start - b.start);
3014
+ if (ranges.length === 0)
3015
+ continue;
3016
+ const gapThreshold = budget.gapThreshold;
3017
+ const clusters = [];
3018
+ let current = {
3019
+ start: ranges[0].start,
3020
+ end: ranges[0].end,
3021
+ symbols: [`${ranges[0].name}(${ranges[0].kind})`],
3022
+ score: ranges[0].importance,
3023
+ maxImportance: ranges[0].importance,
3024
+ hasSpine: ranges[0].spine,
3025
+ spineCallLine: ranges[0].spineCallLine,
3026
+ };
3027
+ for (let i = 1; i < ranges.length; i++) {
3028
+ const r = ranges[i];
3029
+ if (r.start <= current.end + gapThreshold) {
3030
+ current.end = Math.max(current.end, r.end);
3031
+ current.symbols.push(`${r.name}(${r.kind})`);
3032
+ current.score += r.importance;
3033
+ current.maxImportance = Math.max(current.maxImportance, r.importance);
3034
+ current.hasSpine = current.hasSpine || r.spine;
3035
+ current.spineCallLine = current.spineCallLine ?? r.spineCallLine;
3036
+ }
3037
+ else {
3038
+ clusters.push(current);
3039
+ current = {
3040
+ start: r.start,
3041
+ end: r.end,
3042
+ symbols: [`${r.name}(${r.kind})`],
3043
+ score: r.importance,
3044
+ maxImportance: r.importance,
3045
+ hasSpine: r.spine,
3046
+ spineCallLine: r.spineCallLine,
3047
+ };
3048
+ }
3049
+ }
3050
+ clusters.push(current);
3051
+ // Build file section output from clusters, capped by per-file budget.
3052
+ // The pathological case (#185): a file like Session.swift where every
3053
+ // method is adjacent collapses into one cluster spanning the whole
3054
+ // file, and dumping that into the agent's context is most of the
3055
+ // token cost on small projects. We pick clusters in priority order
3056
+ // until the per-file char cap is hit. Truly enormous single clusters
3057
+ // get tail-trimmed with a marker.
3058
+ const contextPadding = 3;
3059
+ const withLineNumbers = exploreLineNumbersEnabled();
3060
+ // Language-neutral separator (no `//` — not a comment in Python, Ruby,
3061
+ // etc.). With line numbers on, the line-number jump also signals the gap.
3062
+ const GAP_MARKER = '\n\n... (gap) ...\n\n';
3063
+ // An oversize spine method (the call path runs THROUGH a god-method — n8n's
3064
+ // processRunExecutionData is 962 lines) is windowed to its next-hop CALL site
3065
+ // plus the signature head, NOT dumped whole. Without this the cluster is too big
3066
+ // for any per-file cap and gets dropped, so the agent Reads the method back —
3067
+ // the exact gap this closes. Bounded, so a god-method can't blow the budget yet
3068
+ // the spine's call still appears in context.
3069
+ const OVERSIZE_SPINE_LINES = 200;
3070
+ const SPINE_WINDOW = 28; // lines each side of the next-hop call site
3071
+ const buildSection = (c) => {
3072
+ if (c.hasSpine && c.spineCallLine && (c.end - c.start + 1) > OVERSIZE_SPINE_LINES) {
3073
+ const call = c.spineCallLine;
3074
+ const winStart = Math.max(c.start, call - SPINE_WINDOW);
3075
+ const winEnd = Math.min(c.end, call + SPINE_WINDOW);
3076
+ const parts = [];
3077
+ // Signature head, only when it sits clearly above the window (else the
3078
+ // window already covers the method opening).
3079
+ const headEnd = Math.min(c.start + 4, winStart - 2);
3080
+ if (headEnd >= c.start) {
3081
+ const head = fileLines.slice(c.start - 1, headEnd).join('\n');
3082
+ parts.push(withLineNumbers ? numberSourceLines(head, c.start) : head);
3083
+ }
3084
+ const win = fileLines.slice(winStart - 1, winEnd).join('\n');
3085
+ parts.push(withLineNumbers ? numberSourceLines(win, winStart) : win);
3086
+ return parts.join(GAP_MARKER);
3087
+ }
3088
+ const startIdx = Math.max(0, c.start - 1 - contextPadding);
3089
+ const endIdx = Math.min(fileLines.length, c.end + contextPadding);
3090
+ const slice = fileLines.slice(startIdx, endIdx).join('\n');
3091
+ // startIdx is 0-based, so the slice's first line is line startIdx + 1.
3092
+ return withLineNumbers ? numberSourceLines(slice, startIdx + 1) : slice;
3093
+ };
3094
+ // Rank clusters for inclusion under the per-file cap. Entry-point
3095
+ // clusters come first: a cluster containing a query entry point
3096
+ // (importance 10) must outrank a dense block of mere declarations,
3097
+ // otherwise on a large file like Session.swift the top-of-file class
3098
+ // header + property list (many adjacent low-importance nodes, high
3099
+ // density) wins the budget and buries the actual methods the query
3100
+ // asked about (perform/didCreateURLRequest/task live deep in the
3101
+ // file). Within the same importance tier, prefer density (score per
3102
+ // line) so we still favor focused clusters over sprawling ones, then
3103
+ // smaller span as a cheap-to-include tiebreak.
3104
+ const rankedClusters = clusters
3105
+ .map((c, i) => ({ idx: i, span: c.end - c.start + 1, c }))
3106
+ .sort((a, b) => {
3107
+ // Spine clusters first — the rendered call path IS the flow answer, so it
3108
+ // outranks any denser block of peripheral declarations (a low-density entry
3109
+ // method must not lose the budget to them). Within spine / within non-spine,
3110
+ // the existing importance → density → score → span order holds.
3111
+ if (a.c.hasSpine !== b.c.hasSpine)
3112
+ return (b.c.hasSpine ? 1 : 0) - (a.c.hasSpine ? 1 : 0);
3113
+ if (b.c.maxImportance !== a.c.maxImportance)
3114
+ return b.c.maxImportance - a.c.maxImportance;
3115
+ const densityA = a.c.score / a.span;
3116
+ const densityB = b.c.score / b.span;
3117
+ if (densityB !== densityA)
3118
+ return densityB - densityA;
3119
+ if (b.c.score !== a.c.score)
3120
+ return b.c.score - a.c.score;
3121
+ return a.span - b.span;
3122
+ });
3123
+ // Per-file budget is the SMALLER of the per-file cap and what's left of the
3124
+ // total output cap — so selection (which ranks by importance) keeps the
3125
+ // high-importance clusters and drops peripheral ones, instead of the
3126
+ // downstream source-order trim slicing off whatever comes last in the file.
3127
+ // That source-order slice is what cut Django's `_fetch_all` (L2237, importance
3128
+ // 9 — agent-named) when query.py was the last of four big files to be emitted.
3129
+ const fileBudget = Math.min(budget.maxCharsPerFile, Math.max(0, budget.maxOutputChars - totalChars - 200));
3130
+ // Spine ceiling: a flow-path cluster may exceed the per-file cap (the call
3131
+ // path is the answer), but bounded — at most ~2.5× the per-file cap and never
3132
+ // past what's left of the total output cap — so a pathological long in-file
3133
+ // spine can't run away or starve co-flow files entirely.
3134
+ const SPINE_CEILING = Math.min(budget.maxCharsPerFile * 2.5, Math.max(0, budget.maxOutputChars - totalChars - 200));
3135
+ const chosenIndices = new Set();
3136
+ let projectedChars = 0;
3137
+ for (const rc of rankedClusters) {
3138
+ const sectionLen = buildSection(rc.c).length + (chosenIndices.size > 0 ? GAP_MARKER.length : 0);
3139
+ // Always take the top-ranked cluster, even if oversize, so we don't
3140
+ // return an empty file section (agent would then re-Read the file,
3141
+ // negating the savings).
3142
+ if (chosenIndices.size === 0) {
3143
+ chosenIndices.add(rc.idx);
3144
+ projectedChars += sectionLen;
3145
+ continue;
3146
+ }
3147
+ // A spine cluster (the rendered call path) is the flow answer — include it
3148
+ // past the per-file budget up to the spine ceiling; non-spine clusters obey
3149
+ // the normal per-file budget.
3150
+ const fits = projectedChars + sectionLen <= fileBudget;
3151
+ const spineFits = rc.c.hasSpine && projectedChars + sectionLen <= SPINE_CEILING;
3152
+ if (!fits && !spineFits)
3153
+ continue;
3154
+ chosenIndices.add(rc.idx);
3155
+ projectedChars += sectionLen;
3156
+ }
3157
+ // Emit chosen clusters in source order so the file reads top-to-bottom.
3158
+ let fileSection = '';
3159
+ const allSymbols = [];
3160
+ for (let i = 0; i < clusters.length; i++) {
3161
+ if (!chosenIndices.has(i))
3162
+ continue;
3163
+ const cluster = clusters[i];
3164
+ const section = buildSection(cluster);
3165
+ if (fileSection.length > 0)
3166
+ fileSection += GAP_MARKER;
3167
+ fileSection += section;
3168
+ allSymbols.push(...cluster.symbols);
3169
+ }
3170
+ // A chosen cluster is a COMPLETE method-range — we never cut through a body.
3171
+ // An oversize single cluster (a long monolithic function) renders in FULL:
3172
+ // half a method is useless (the agent just Reads the rest for the other half),
3173
+ // which is the very fallback explore exists to prevent. A pathological file is
3174
+ // bounded by the per-file cluster SELECTION above + the total hard ceiling.
3175
+ if (chosenIndices.size < clusters.length) {
3176
+ anyFileTrimmed = true;
3177
+ }
3178
+ // Dedupe + cap the symbols list shown in the per-file header. Some
3179
+ // files (Session.swift in Alamofire) produced 3.4KB symbol lists
3180
+ // from cluster scoring + edge-source lines, dwarfing the per-file
3181
+ // body cap. Show top names by frequency, with a "+N more" tail.
3182
+ const symbolCounts = new Map();
3183
+ for (const s of allSymbols) {
3184
+ symbolCounts.set(s, (symbolCounts.get(s) ?? 0) + 1);
3185
+ }
3186
+ const sortedSymbols = [...symbolCounts.entries()]
3187
+ .sort((a, b) => b[1] - a[1])
3188
+ .map(([name]) => name);
3189
+ const headerCap = budget.maxSymbolsInFileHeader;
3190
+ const headerSymbols = sortedSymbols.slice(0, headerCap);
3191
+ const omittedCount = sortedSymbols.length - headerSymbols.length;
3192
+ const headerSuffix = omittedCount > 0
3193
+ ? `${headerSymbols.join(', ')}, +${omittedCount} more`
3194
+ : headerSymbols.join(', ');
3195
+ const fileHeader = fileSectionHeader(filePath, headerSuffix);
3196
+ // The total cap bounds INCIDENTAL files only. A file that DEFINES a symbol
3197
+ // the agent named (or that's on the flow spine) renders even when the
3198
+ // nominal total is used up — it's the answer, and the set is bounded by
3199
+ // maxFiles AND by true-spine/named-seeding having already trimmed each file
3200
+ // to its necessary content. A file that merely REFERENCES the flow
3201
+ // (Combine.swift name-drops request/task) is incidental → still capped, so
3202
+ // freed budget never leaks into noise. This is the last god-file layer:
3203
+ // build (Session, true-spined) + validators-exec (Request) + validate
3204
+ // (DataRequest/Validation) all render, instead of the cap dropping whichever
3205
+ // phase the file order happened to put last.
3206
+ if (!fileNecessary && totalChars + fileSection.length + 200 > budget.maxOutputChars) {
3207
+ // Incidental file that doesn't fit: SKIP it whole — never slice mid-method.
3208
+ // Keep scanning for necessary files (which bypass this cap and render in
3209
+ // full, bounded by the hard ceiling).
3210
+ anyFileTrimmed = true;
3211
+ continue;
3212
+ }
3213
+ lines.push(fileHeader);
3214
+ lines.push('');
3215
+ lines.push('```' + lang);
3216
+ lines.push(fileSection);
3217
+ lines.push('```');
3218
+ lines.push('');
3219
+ totalChars += fileSection.length + 200;
3220
+ filesIncluded++;
3221
+ }
3222
+ // Add remaining files as references (from both relevant and peripheral files).
3223
+ // Small projects (per budget) skip this — the relevant story already fits
3224
+ // in the source section, and a trailing pointer list is pure overhead.
3225
+ if (budget.includeAdditionalFiles) {
3226
+ const remainingRelevant = sortedFiles.slice(filesIncluded);
3227
+ const peripheralFiles = [...fileGroups.entries()]
3228
+ .filter(([, group]) => group.score < 3)
3229
+ .sort((a, b) => b[1].score - a[1].score);
3230
+ const remainingFiles = [...remainingRelevant, ...peripheralFiles];
3231
+ if (remainingFiles.length > 0) {
3232
+ lines.push('**Not shown above — explore these names for their source**');
3233
+ lines.push('');
3234
+ for (const [filePath, group] of remainingFiles.slice(0, 10)) {
3235
+ const symbols = group.nodes.map(n => `${n.name}:${n.startLine}`).join(', ');
3236
+ lines.push(`- ${filePath}: ${symbols}`);
3237
+ }
3238
+ if (remainingFiles.length > 10) {
3239
+ lines.push(`- ... and ${remainingFiles.length - 10} more files`);
3240
+ }
3241
+ }
3242
+ }
3243
+ // Add completeness signal so agents know they don't need to re-read these files.
3244
+ // On small projects the budget gates this off — but if we actually had to
3245
+ // trim or drop clusters, surface a brief note so the agent knows it can
3246
+ // still Read for more detail.
3247
+ if (budget.includeCompletenessSignal) {
3248
+ lines.push('');
3249
+ lines.push('---');
3250
+ lines.push(`> **Complete source for ${filesIncluded} files is included above — do NOT re-read them.** If your question also needs files/symbols listed under "Not shown above" (or any area this call didn't cover), make ANOTHER codegraph_explore targeting those names — it returns the same source with line numbers and is cheaper and more complete than reading. Reserve Read for a single specific line range explore can't surface.`);
3251
+ }
3252
+ else if (anyFileTrimmed) {
3253
+ lines.push('');
3254
+ lines.push(`> Some file sections were trimmed for size. For a specific symbol you still need, run another \`codegraph_explore\` (or \`codegraph_node\`) with its exact name — line-numbered source, cheaper and more complete than Read.`);
3255
+ }
3256
+ // Add explore budget note based on project size
3257
+ if (budget.includeBudgetNote) {
3258
+ try {
3259
+ const stats = cg.getStats();
3260
+ const callBudget = getExploreBudget(stats.fileCount);
3261
+ lines.push('');
3262
+ lines.push(`> **Explore budget: ${callBudget} calls for this project (${stats.fileCount.toLocaleString()} files indexed).** Each call covers ~6 files; if your question spans more, spend your remaining calls on the uncovered area BEFORE falling back to Read — another explore is cheaper and more complete than reading those files. Synthesize once you've used ${callBudget}.`);
3263
+ }
3264
+ catch {
3265
+ // Stats unavailable — skip budget note
3266
+ }
3267
+ }
3268
+ // Final ceiling — an ABSOLUTE inline cap, not a multiple of the budget. The
3269
+ // render loop renders necessary (named/spine) files even a bit past
3270
+ // maxOutputChars and caps only incidental ones, so this is the last safety.
3271
+ // It MUST stay under the host's inline tool-result limit (~25K chars): above
3272
+ // that the result is externalized to a file the agent Reads back (a 35K
3273
+ // vscode explore did exactly this in the n=4 A/B). So allow a little
3274
+ // necessary overflow above the 24K budget, but hard-stop at 25K — never into
3275
+ // externalize territory.
3276
+ const output = flow.text + lines.join('\n');
3277
+ const hardCeiling = Math.min(Math.round(budget.maxOutputChars * 1.5), 25000);
3278
+ if (output.length > hardCeiling) {
3279
+ // Cut at a FILE-SECTION boundary (the last ``**` `` file header before the
3280
+ // ceiling) so we drop whole trailing file-sections rather than slicing
3281
+ // through a method body — a half-rendered method just forces the Read this
3282
+ // tool exists to prevent. Fall back to a line boundary only if no section
3283
+ // header sits in the back half (degenerate single-giant-section case).
3284
+ const cut = output.slice(0, hardCeiling);
3285
+ const lastSection = cut.lastIndexOf('\n' + FILE_SECTION_PREFIX);
3286
+ const boundary = lastSection > hardCeiling * 0.5 ? lastSection : cut.lastIndexOf('\n');
3287
+ const safe = boundary > 0 ? cut.slice(0, boundary) : cut;
3288
+ return this.textResult(safe + '\n\n... (output truncated to budget; the source above is complete and verbatim — treat it as already Read. For any area not covered, run another codegraph_explore with the specific names — do NOT Read these files.)');
3289
+ }
3290
+ return this.textResult(output);
3291
+ }
3292
+ /**
3293
+ * Handle codegraph_node
3294
+ */
3295
+ async handleNode(args) {
3296
+ const cg = this.getCodeGraph(args.projectPath);
3297
+ // Default to false to minimize context usage
3298
+ const includeCode = args.includeCode === true;
3299
+ const fileHint = typeof args.file === 'string' && args.file.trim() ? args.file.trim() : undefined;
3300
+ const lineHint = typeof args.line === 'number' && args.line > 0 ? args.line : undefined;
3301
+ const offset = typeof args.offset === 'number' && args.offset > 0 ? Math.floor(args.offset) : undefined;
3302
+ const limit = typeof args.limit === 'number' && args.limit > 0 ? Math.floor(args.limit) : undefined;
3303
+ const symbolsOnly = args.symbolsOnly === true;
3304
+ const symbolRaw = typeof args.symbol === 'string' ? args.symbol.trim() : '';
3305
+ // FILE READ MODE: a `file` with no `symbol` reads that file like the Read
3306
+ // tool — its current on-disk source with line numbers, narrowable with
3307
+ // `offset`/`limit` exactly as Read does — PLUS a one-line blast-radius
3308
+ // header (which files depend on it). `symbolsOnly` returns just the
3309
+ // structural map instead. Backed by the index: same bytes Read gives you.
3310
+ if (!symbolRaw && fileHint) {
3311
+ return this.handleFileView(cg, fileHint, { offset, limit, symbolsOnly });
3312
+ }
3313
+ const symbol = this.validateString(args.symbol, 'symbol');
3314
+ if (typeof symbol !== 'string')
3315
+ return symbol;
3316
+ let matches = this.findSymbolMatches(cg, symbol);
3317
+ if (matches.length === 0) {
3318
+ return this.textResult(`Symbol "${symbol}" not found in the codebase`);
3319
+ }
3320
+ // Disambiguate a heavily-overloaded name to a specific definition the caller
3321
+ // pinned by file/line (the `file:line` a trail or another tool showed it) —
3322
+ // so it can fetch e.g. `Harness::poll` at harness.rs:153 out of 50+ `poll`s
3323
+ // instead of Reading. file matches by path suffix/substring; line prefers the
3324
+ // def whose body contains it, else the nearest start. Only narrows (never
3325
+ // empties — if a hint matches nothing it's ignored).
3326
+ if (matches.length > 1 && (fileHint || lineHint !== undefined)) {
3327
+ const norm = (p) => p.replace(/\\/g, '/').toLowerCase();
3328
+ let narrowed = matches;
3329
+ if (fileHint) {
3330
+ const fh = norm(fileHint);
3331
+ const byFile = narrowed.filter((n) => norm(n.filePath).endsWith(fh) || norm(n.filePath).includes(fh));
3332
+ if (byFile.length > 0)
3333
+ narrowed = byFile;
3334
+ }
3335
+ if (lineHint !== undefined && narrowed.length > 1) {
3336
+ const containing = narrowed.filter((n) => n.startLine <= lineHint && (n.endLine ?? n.startLine) >= lineHint);
3337
+ narrowed = containing.length > 0
3338
+ ? containing
3339
+ : [...narrowed].sort((a, b) => Math.abs(a.startLine - lineHint) - Math.abs(b.startLine - lineHint)).slice(0, 1);
3340
+ }
3341
+ if (narrowed.length > 0)
3342
+ matches = narrowed;
3343
+ }
3344
+ // Single definition — the common case.
3345
+ if (matches.length === 1) {
3346
+ return this.textResult(this.truncateOutput(await this.renderNodeSection(cg, matches[0], includeCode)));
3347
+ }
3348
+ // Multiple definitions share this name — overloads, or same-named methods on
3349
+ // different types (Alamofire `didCompleteTask`/`task`/`validate`, gin
3350
+ // `reset`). Returning ONE forces the agent to guess, and when it guesses
3351
+ // wrong it READS the file to find the right overload — the dominant
3352
+ // codegraph_node read cause on Swift/Go. So return them ALL: pack as many
3353
+ // FULL bodies as fit a char budget (the agent gets the one it needs in this
3354
+ // one call, no follow-up parameter to learn), and list any remainder by
3355
+ // file:line so a large overload set can't overflow the per-tool cap.
3356
+ const header = `**${matches.length} definitions named "${symbol}"**`;
3357
+ if (!includeCode) {
3358
+ const list = matches.map((n) => `- \`${n.name}\` (${n.kind}) — ${n.filePath}:${n.startLine}`);
3359
+ return this.textResult(this.truncateOutput([header, '', 'Re-query with `includeCode: true` to get every body in one call — no need to pick one first.', '', ...list].join('\n')));
3360
+ }
3361
+ const BODY_BUDGET = 12000; // leaves room under MAX_OUTPUT_LENGTH for the header + list
3362
+ // The CHAR budget is the real limiter — keep the count cap high so a set of
3363
+ // SHORT overloads (Alamofire's 10 `validate` variants, each a few lines) all
3364
+ // render in full rather than relegating the one the agent wanted to a
3365
+ // bodiless list. Only a set of many LARGE bodies hits the char budget first.
3366
+ const HARD_CAP = 16;
3367
+ const rendered = [];
3368
+ const listed = [];
3369
+ let used = 0;
3370
+ for (const n of matches) {
3371
+ if (rendered.length >= HARD_CAP) {
3372
+ listed.push(n);
3373
+ continue;
3374
+ }
3375
+ const section = await this.renderNodeSection(cg, n, true);
3376
+ // Always emit the first; emit the rest only while within the char budget.
3377
+ if (rendered.length === 0 || used + section.length <= BODY_BUDGET) {
3378
+ rendered.push(section);
3379
+ used += section.length;
3380
+ }
3381
+ else {
3382
+ listed.push(n);
3383
+ }
3384
+ }
3385
+ const out = [
3386
+ header,
3387
+ `Returning ${rendered.length} in full${listed.length ? `; ${listed.length} more listed below` : ''} — pick the one you need (no Read required).`,
3388
+ '',
3389
+ rendered.join('\n\n---\n\n'),
3390
+ ];
3391
+ if (listed.length) {
3392
+ const LIST_CAP = 20;
3393
+ const shownList = listed.slice(0, LIST_CAP);
3394
+ out.push('', '**Other definitions**', ...shownList.map((n) => `- \`${n.name}\` (${n.kind}) — ${n.filePath}:${n.startLine}`));
3395
+ if (listed.length > LIST_CAP)
3396
+ out.push(`- … +${listed.length - LIST_CAP} more`);
3397
+ out.push('', `> Need one of these in full? Call codegraph_node again with \`file\` (e.g. \`"${listed[0].filePath.split('/').pop()}"\`) or \`line\` — do NOT Read it.`);
3398
+ }
3399
+ return this.textResult(this.truncateOutput(out.join('\n')));
3400
+ }
3401
+ /**
3402
+ * FILE READ MODE: resolve `fileArg` (path or basename) to an indexed file and
3403
+ * read it like the Read tool — its current on-disk source with line numbers,
3404
+ * narrowable with `offset`/`limit` exactly as Read's are — preceded by a
3405
+ * one-line blast-radius header (which files depend on it). `symbolsOnly`
3406
+ * returns just the structural map (symbols + dependents) instead of source.
3407
+ *
3408
+ * Parity goal: the numbered source block is byte-for-byte the shape Read
3409
+ * returns (`<n>\t<line>`, no padding), so the agent treats it as a Read — only
3410
+ * faster (served from the index) and with the blast radius attached. Security:
3411
+ * yaml/properties files are summarized by key, never dumped (#383); reads go
3412
+ * through validatePathWithinRoot (#527).
3413
+ */
3414
+ async handleFileView(cg, fileArg, opts = {}) {
3415
+ const normalize = (p) => p.replace(/\\/g, '/').replace(/^(?:\.?\/+)+/, '').replace(/\/+$/, '');
3416
+ const wantLower = normalize(fileArg).toLowerCase();
3417
+ const allFiles = cg.getFiles();
3418
+ if (allFiles.length === 0)
3419
+ return this.textResult('No files indexed. Run `cgraphx index` first.');
3420
+ let resolved = allFiles.find((f) => f.path.toLowerCase() === wantLower);
3421
+ let candidates = [];
3422
+ if (!resolved) {
3423
+ candidates = allFiles.filter((f) => f.path.toLowerCase().endsWith('/' + wantLower));
3424
+ if (candidates.length === 1)
3425
+ resolved = candidates[0];
3426
+ }
3427
+ if (!resolved && candidates.length === 0) {
3428
+ candidates = allFiles.filter((f) => f.path.toLowerCase().includes(wantLower));
3429
+ if (candidates.length === 1)
3430
+ resolved = candidates[0];
3431
+ }
3432
+ if (!resolved && candidates.length > 1) {
3433
+ return this.textResult([`"${fileArg}" matches ${candidates.length} indexed files — pass a longer path:`, '',
3434
+ ...candidates.slice(0, 25).map((f) => `- ${f.path}`)].join('\n'));
3435
+ }
3436
+ if (!resolved) {
3437
+ return this.textResult(`No indexed file matches "${fileArg}". Codegraph indexes source files; configs/docs it doesn't parse won't appear — Read those directly.`);
3438
+ }
3439
+ const filePath = resolved.path;
3440
+ const nodes = cg.getNodesInFile(filePath)
3441
+ .filter((n) => n.kind !== 'file' && n.kind !== 'import' && n.kind !== 'export')
3442
+ .sort((a, b) => a.startLine - b.startLine);
3443
+ const dependents = cg.getFileDependents(filePath);
3444
+ // Compact, one-line blast radius (cgraphx's value-add over a plain Read).
3445
+ const depSummary = dependents.length
3446
+ ? `used by ${dependents.length} file${dependents.length === 1 ? '' : 's'}: ${dependents.slice(0, 8).join(', ')}${dependents.length > 8 ? `, +${dependents.length - 8} more` : ''}`
3447
+ : 'no other indexed file depends on it';
3448
+ // Symbol-map renderer — for symbolsOnly, the config fallback, and read errors.
3449
+ const symbolMap = (heading, limit = 200) => {
3450
+ const lines = [heading];
3451
+ for (const n of nodes.slice(0, limit)) {
3452
+ const sig = n.signature ? ` ${n.signature.replace(/\s+/g, ' ').trim()}` : '';
3453
+ lines.push(`- \`${n.name}\` (${n.kind})${sig} — :${n.startLine}`);
3454
+ }
3455
+ if (nodes.length > limit)
3456
+ lines.push(`- … +${nodes.length - limit} more`);
3457
+ return lines;
3458
+ };
3459
+ // symbolsOnly → the cheap structural overview, no source.
3460
+ if (opts.symbolsOnly) {
3461
+ const out = [`**${filePath}** — ${nodes.length} symbol${nodes.length === 1 ? '' : 's'}, ${depSummary}`, ''];
3462
+ if (nodes.length)
3463
+ out.push(...symbolMap('**Symbols**'));
3464
+ else
3465
+ out.push('_No indexed symbols in this file._');
3466
+ out.push('', '> Drop `symbolsOnly` (or pass `offset`/`limit`) to read the source, like Read.');
3467
+ return this.textResult(this.truncateOutput(out.join('\n')));
3468
+ }
3469
+ // SECURITY (#383): never dump a raw config/data file — a yaml/properties
3470
+ // line is `key: <secret>`. Summarize by key and point to a real Read.
3471
+ if (utils_1.CONFIG_LEAF_LANGUAGES.has(resolved.language)) {
3472
+ const out = [`**${filePath}** — configuration/data file, ${depSummary}`, ''];
3473
+ if (nodes.length)
3474
+ out.push(...symbolMap('**Keys (values withheld for safety)**'));
3475
+ out.push('', '> Values may be secrets, so cgraphx indexes keys only. Read the file directly if you need a value.');
3476
+ return this.textResult(this.truncateOutput(out.join('\n')));
3477
+ }
3478
+ // Read the current bytes from disk through the security chokepoint
3479
+ // (validatePathWithinRoot: blocks `../` traversal and symlink escapes, #527).
3480
+ const abs = (0, utils_1.validatePathWithinRoot)(cg.getProjectRoot(), filePath);
3481
+ let content = null;
3482
+ if (abs) {
3483
+ try {
3484
+ content = (0, fs_1.readFileSync)(abs, 'utf-8');
3485
+ }
3486
+ catch {
3487
+ content = null;
3488
+ }
3489
+ }
3490
+ if (content === null) {
3491
+ const out = [`**${filePath}** — could not read from disk (it may have moved since indexing). ${depSummary}`, ''];
3492
+ if (nodes.length)
3493
+ out.push(...symbolMap('**Symbols**'));
3494
+ out.push('', `> Read \`${filePath}\` directly for its current content.`);
3495
+ return this.textResult(this.truncateOutput(out.join('\n')));
3496
+ }
3497
+ // Split exactly as Read does — keep the trailing empty line a final newline
3498
+ // produces (Read numbers it too), so line numbers line up byte-for-byte.
3499
+ const fileLines = content.split('\n');
3500
+ const total = fileLines.length;
3501
+ // Read-parity windowing: `offset`/`limit` mean exactly what they do on Read
3502
+ // (1-based start line; max line count). Default: the whole file, capped like
3503
+ // Read at 2000 lines and bounded by a char budget that tracks explore's
3504
+ // proven-safe ~38k response ceiling. Overflow is stated explicitly (Read
3505
+ // paginates too) — never the silent 15k truncateOutput chop.
3506
+ const CHAR_BUDGET = 38000;
3507
+ const DEFAULT_LIMIT = 2000;
3508
+ const offset = Math.max(1, opts.offset ?? 1);
3509
+ if (offset > total) {
3510
+ return this.textResult(`**${filePath}** has ${total} line${total === 1 ? '' : 's'} — offset ${offset} is past the end. ${depSummary}`);
3511
+ }
3512
+ const maxLines = Math.max(1, opts.limit ?? DEFAULT_LIMIT);
3513
+ const start = offset - 1; // 0-based
3514
+ const header = `**${filePath}** — ${total} lines, ${nodes.length} symbol${nodes.length === 1 ? '' : 's'} · ${depSummary}`;
3515
+ // Numbered lines, byte-for-byte Read's shape: `<n>\t<line>`, no left-pad.
3516
+ const numbered = [];
3517
+ let used = header.length + 8;
3518
+ let i = start;
3519
+ for (; i < total && numbered.length < maxLines; i++) {
3520
+ const ln = `${i + 1}\t${fileLines[i]}`;
3521
+ if (used + ln.length + 1 > CHAR_BUDGET && numbered.length > 0)
3522
+ break;
3523
+ numbered.push(ln);
3524
+ used += ln.length + 1;
3525
+ }
3526
+ const shownEnd = start + numbered.length;
3527
+ const complete = offset === 1 && shownEnd >= total;
3528
+ const out = [header, '', ...numbered];
3529
+ if (!complete) {
3530
+ out.push('', `(lines ${offset}–${shownEnd} of ${total} — pass \`offset\`/\`limit\` for another range, or \`codegraph_node <symbol>\` for one symbol in full)`);
3531
+ }
3532
+ // Self-bounded to CHAR_BUDGET — do NOT route through truncateOutput (15k).
3533
+ return this.textResult(out.join('\n'));
3534
+ }
3535
+ /** Render one symbol: details + (optional) body/outline + its caller/callee trail. */
3536
+ async renderNodeSection(cg, node, includeCode) {
3537
+ let code = null;
3538
+ let outline = null;
3539
+ if (includeCode) {
3540
+ // For container symbols (class/interface/struct/…), the full body is the
3541
+ // sum of every method body — a wall of source. Return a structural outline
3542
+ // (members + signatures + line numbers) instead; leaf symbols return their
3543
+ // full body.
3544
+ if (CONTAINER_NODE_KINDS.has(node.kind)) {
3545
+ outline = this.buildContainerOutline(cg, node);
3546
+ }
3547
+ if (!outline) {
3548
+ code = await cg.getCode(node.id);
3549
+ }
3550
+ }
3551
+ return this.formatNodeDetails(node, code, outline) + this.formatTrail(cg, node);
3552
+ }
3553
+ /**
3554
+ * Build the "trail" for a symbol: its direct callees (what it calls) and
3555
+ * callers (what calls it), each with file:line — so codegraph_node doubles as
3556
+ * the structural Grep→Read→expand primitive: a spot PLUS where to go next.
3557
+ * Capped to stay cheap. Walk the graph by calling codegraph_node on a trail
3558
+ * entry; no Read needed for covered hops. Empty edges on a non-leaf often mean
3559
+ * dynamic dispatch the static graph couldn't resolve — that absence is itself
3560
+ * a signal (read that one hop) rather than a dead end.
3561
+ */
3562
+ formatTrail(cg, node) {
3563
+ const TRAIL_CAP = 12;
3564
+ const fmt = (e) => {
3565
+ const base = `${e.node.name} (${e.node.filePath}:${e.node.startLine})`;
3566
+ const synth = this.synthEdgeNote(e.edge);
3567
+ return synth ? `${base} [${synth.compact}]` : base;
3568
+ };
3569
+ const collect = (edges) => {
3570
+ const seen = new Set([node.id]);
3571
+ const out = [];
3572
+ for (const e of edges) {
3573
+ if (seen.has(e.node.id))
3574
+ continue;
3575
+ seen.add(e.node.id);
3576
+ out.push(e);
3577
+ }
3578
+ return out;
3579
+ };
3580
+ const callees = collect(cg.getCallees(node.id));
3581
+ const callers = collect(cg.getCallers(node.id));
3582
+ if (callees.length === 0 && callers.length === 0)
3583
+ return '';
3584
+ const lines = ['', '**Trail — codegraph_node any of these to follow it (no Read needed)**'];
3585
+ if (callees.length > 0) {
3586
+ lines.push(`**Calls →** ${callees.slice(0, TRAIL_CAP).map(fmt).join(', ')}${callees.length > TRAIL_CAP ? `, +${callees.length - TRAIL_CAP} more` : ''}`);
3587
+ }
3588
+ if (callers.length > 0) {
3589
+ lines.push(`**Called by ←** ${callers.slice(0, TRAIL_CAP).map(fmt).join(', ')}${callers.length > TRAIL_CAP ? `, +${callers.length - TRAIL_CAP} more` : ''}`);
3590
+ }
3591
+ return lines.join('\n');
3592
+ }
3593
+ /**
3594
+ * Handle codegraph_status
3595
+ */
3596
+ async handleStatus(args) {
3597
+ let cg = this.getCodeGraph(args.projectPath);
3598
+ // Same trick as withStalenessNotice — when an explicit projectPath
3599
+ // resolves to the same project as the default session cg, prefer the
3600
+ // default so getPendingFiles() (only populated by the default's watcher)
3601
+ // is non-empty when there are pending edits.
3602
+ if (this.cg && cg !== this.cg) {
3603
+ try {
3604
+ if ((0, path_1.resolve)(this.cg.getProjectRoot()) === (0, path_1.resolve)(cg.getProjectRoot())) {
3605
+ cg = this.cg;
3606
+ }
3607
+ }
3608
+ catch { /* closed instance — leave as is */ }
3609
+ }
3610
+ const stats = cg.getStats();
3611
+ // Warn when this index actually belongs to a different git working tree
3612
+ // (e.g. the server resolved up from a nested worktree to the main checkout).
3613
+ // Queries then reflect that tree's branch, not the worktree being edited.
3614
+ // status shows the verbose, multi-line form; the read tools get the compact
3615
+ // one-liner via withWorktreeNotice. Both share the cached detection.
3616
+ const mismatch = this.worktreeMismatchFor(args.projectPath);
3617
+ const lines = [
3618
+ '**cgraphx Status**',
3619
+ '',
3620
+ ];
3621
+ if (mismatch) {
3622
+ lines.push(`> ⚠ ${(0, worktree_1.worktreeMismatchWarning)(mismatch).replace(/\n/g, '\n> ')}`, '');
3623
+ }
3624
+ lines.push(`**Files indexed:** ${stats.fileCount}`, `**Total nodes:** ${stats.nodeCount}`, `**Total edges:** ${stats.edgeCount}`, `**Database size:** ${(stats.dbSizeBytes / 1024 / 1024).toFixed(2)} MB`);
3625
+ // Surface the active SQLite backend (node:sqlite, Node's built-in real
3626
+ // SQLite — full WAL + FTS5, no native build).
3627
+ lines.push(`**Backend:** node:sqlite (Node built-in) — full WAL + FTS5`);
3628
+ // Effective journal mode. 'wal' ⇒ concurrent reads never block on a writer;
3629
+ // anything else ⇒ they can ("database is locked"). node:sqlite supports WAL
3630
+ // everywhere, so a non-wal mode means the filesystem can't (network/
3631
+ // virtualized mounts, WSL2 /mnt). See issue #238.
3632
+ const journalMode = cg.getJournalMode();
3633
+ if (journalMode === 'wal') {
3634
+ lines.push(`**Journal mode:** wal (concurrent reads safe)`);
3635
+ }
3636
+ else {
3637
+ lines.push(`**Journal mode:** ⚠ ${journalMode || 'unknown'} — WAL not active, so reads ` +
3638
+ `can block on a concurrent write (WAL appears unsupported on this filesystem)`);
3639
+ }
3640
+ lines.push('', '**Nodes by Kind:**');
3641
+ for (const [kind, count] of Object.entries(stats.nodesByKind)) {
3642
+ if (count > 0) {
3643
+ lines.push(`- ${kind}: ${count}`);
3644
+ }
3645
+ }
3646
+ lines.push('', '**Languages:**');
3647
+ for (const [lang, count] of Object.entries(stats.filesByLanguage)) {
3648
+ if (count > 0) {
3649
+ lines.push(`- ${lang}: ${count}`);
3650
+ }
3651
+ }
3652
+ // Whole-index degradation (#876): when live watching has permanently
3653
+ // stopped, getPendingFiles() is empty (so no "Pending sync" section below)
3654
+ // but the index is frozen — call that out explicitly here, the one place an
3655
+ // agent asks "is the index caught up?".
3656
+ if (cg.isWatcherDegraded()) {
3657
+ lines.push('', '**Auto-sync disabled:**', `- ${cg.getWatcherDegradedReason() ?? 'live file watching stopped'}`, '- The index is frozen; Read files directly for current content.');
3658
+ }
3659
+ // Per-file freshness — the inverse of the auto-prepended staleness banner
3660
+ // (issue #403). Surfacing it inside `status` gives the agent a single
3661
+ // place to ask "is the index caught up?" rather than inferring from
3662
+ // banners on other tool calls.
3663
+ const pending = cg.getPendingFiles();
3664
+ if (pending.length > 0) {
3665
+ lines.push('', '**Pending sync:**');
3666
+ const now = Date.now();
3667
+ for (const p of pending) {
3668
+ const ageMs = Math.max(0, now - p.lastSeenMs);
3669
+ const label = p.indexing ? 'indexing in progress' : 'pending sync';
3670
+ lines.push(`- ${p.path} (edited ${ageMs}ms ago, ${label})`);
3671
+ }
3672
+ }
3673
+ return this.textResult(lines.join('\n'));
3674
+ }
3675
+ /**
3676
+ * Handle codegraph_files - get project file structure from the index
3677
+ */
3678
+ async handleFiles(args) {
3679
+ const cg = this.getCodeGraph(args.projectPath);
3680
+ const pathFilter = args.path;
3681
+ const pattern = args.pattern;
3682
+ const format = args.format || 'tree';
3683
+ const includeMetadata = args.includeMetadata !== false;
3684
+ const maxDepth = args.maxDepth != null ? (0, utils_1.clamp)(args.maxDepth, 1, 20) : undefined;
3685
+ // Get all files from the index
3686
+ const allFiles = cg.getFiles();
3687
+ if (allFiles.length === 0) {
3688
+ return this.textResult('No files indexed. Run `cgraphx index` first.');
3689
+ }
3690
+ // Filter by path prefix. Stored paths are project-relative POSIX (e.g.
3691
+ // "src/foo.ts"), but agents commonly pass project-root variants like "/",
3692
+ // ".", "./", "" or Windows-style "src\foo" — and prefixes with leading
3693
+ // "/", "./" or "\". Normalize all of those before matching so the agent
3694
+ // gets results instead of falling back to Read/Glob (see #426).
3695
+ const normalizedFilter = pathFilter
3696
+ ? pathFilter
3697
+ .replace(/\\/g, '/')
3698
+ .replace(/^(?:\.?\/+)+/, '')
3699
+ .replace(/^\.$/, '')
3700
+ .replace(/\/+$/, '')
3701
+ : '';
3702
+ let files = normalizedFilter
3703
+ ? allFiles.filter(f => f.path === normalizedFilter || f.path.startsWith(normalizedFilter + '/'))
3704
+ : allFiles;
3705
+ // Filter by glob pattern
3706
+ if (pattern) {
3707
+ const regex = this.globToRegex(pattern);
3708
+ files = files.filter(f => regex.test(f.path));
3709
+ }
3710
+ if (files.length === 0) {
3711
+ return this.textResult(`No files found matching the criteria.`);
3712
+ }
3713
+ // Format output
3714
+ let output;
3715
+ switch (format) {
3716
+ case 'flat':
3717
+ output = this.formatFilesFlat(files, includeMetadata);
3718
+ break;
3719
+ case 'grouped':
3720
+ output = this.formatFilesGrouped(files, includeMetadata);
3721
+ break;
3722
+ case 'tree':
3723
+ default:
3724
+ output = this.formatFilesTree(files, includeMetadata, maxDepth);
3725
+ break;
3726
+ }
3727
+ return this.textResult(this.truncateOutput(output));
3728
+ }
3729
+ /**
3730
+ * Convert glob pattern to regex
3731
+ */
3732
+ globToRegex(pattern) {
3733
+ const escaped = pattern
3734
+ .replace(/[.+^${}()|[\]\\]/g, '\\$&') // Escape special regex chars except * and ?
3735
+ .replace(/\*\*/g, '{{GLOBSTAR}}') // Temp placeholder for **
3736
+ .replace(/\*/g, '[^/]*') // * matches anything except /
3737
+ .replace(/\?/g, '[^/]') // ? matches single char except /
3738
+ .replace(/\{\{GLOBSTAR\}\}/g, '.*'); // ** matches anything including /
3739
+ return new RegExp(escaped);
3740
+ }
3741
+ /**
3742
+ * Format files as a flat list
3743
+ */
3744
+ formatFilesFlat(files, includeMetadata) {
3745
+ const lines = [`**Files (${files.length})**`, ''];
3746
+ for (const file of files.sort((a, b) => a.path.localeCompare(b.path))) {
3747
+ if (includeMetadata) {
3748
+ lines.push(`- ${file.path} (${file.language}, ${file.nodeCount} symbols)`);
3749
+ }
3750
+ else {
3751
+ lines.push(`- ${file.path}`);
3752
+ }
3753
+ }
3754
+ return lines.join('\n');
3755
+ }
3756
+ /**
3757
+ * Format files grouped by language
3758
+ */
3759
+ formatFilesGrouped(files, includeMetadata) {
3760
+ const byLang = new Map();
3761
+ for (const file of files) {
3762
+ const existing = byLang.get(file.language) || [];
3763
+ existing.push(file);
3764
+ byLang.set(file.language, existing);
3765
+ }
3766
+ const lines = [`**Files by Language (${files.length} total)**`, ''];
3767
+ // Sort languages by file count (descending)
3768
+ const sortedLangs = [...byLang.entries()].sort((a, b) => b[1].length - a[1].length);
3769
+ for (const [lang, langFiles] of sortedLangs) {
3770
+ lines.push(`**${lang} (${langFiles.length})**`);
3771
+ for (const file of langFiles.sort((a, b) => a.path.localeCompare(b.path))) {
3772
+ if (includeMetadata) {
3773
+ lines.push(`- ${file.path} (${file.nodeCount} symbols)`);
3774
+ }
3775
+ else {
3776
+ lines.push(`- ${file.path}`);
3777
+ }
3778
+ }
3779
+ lines.push('');
3780
+ }
3781
+ return lines.join('\n');
3782
+ }
3783
+ /**
3784
+ * Format files as a tree structure
3785
+ */
3786
+ formatFilesTree(files, includeMetadata, maxDepth) {
3787
+ const root = { name: '', children: new Map() };
3788
+ for (const file of files) {
3789
+ const parts = file.path.split('/');
3790
+ let current = root;
3791
+ for (let i = 0; i < parts.length; i++) {
3792
+ const part = parts[i];
3793
+ if (!part)
3794
+ continue;
3795
+ if (!current.children.has(part)) {
3796
+ current.children.set(part, { name: part, children: new Map() });
3797
+ }
3798
+ current = current.children.get(part);
3799
+ // If this is the last part, it's a file
3800
+ if (i === parts.length - 1) {
3801
+ current.file = { language: file.language, nodeCount: file.nodeCount };
3802
+ }
3803
+ }
3804
+ }
3805
+ // Render tree
3806
+ const lines = [`**Project Structure (${files.length} files)**`, ''];
3807
+ const renderNode = (node, prefix, isLast, depth) => {
3808
+ if (maxDepth !== undefined && depth > maxDepth)
3809
+ return;
3810
+ const connector = isLast ? '└── ' : '├── ';
3811
+ const childPrefix = isLast ? ' ' : '│ ';
3812
+ if (node.name) {
3813
+ let line = prefix + connector + node.name;
3814
+ if (node.file && includeMetadata) {
3815
+ line += ` (${node.file.language}, ${node.file.nodeCount} symbols)`;
3816
+ }
3817
+ lines.push(line);
3818
+ }
3819
+ const children = [...node.children.values()];
3820
+ // Sort: directories first, then files, both alphabetically
3821
+ children.sort((a, b) => {
3822
+ const aIsDir = a.children.size > 0 && !a.file;
3823
+ const bIsDir = b.children.size > 0 && !b.file;
3824
+ if (aIsDir !== bIsDir)
3825
+ return aIsDir ? -1 : 1;
3826
+ return a.name.localeCompare(b.name);
3827
+ });
3828
+ for (let i = 0; i < children.length; i++) {
3829
+ const child = children[i];
3830
+ const nextPrefix = node.name ? prefix + childPrefix : prefix;
3831
+ renderNode(child, nextPrefix, i === children.length - 1, depth + 1);
3832
+ }
3833
+ };
3834
+ renderNode(root, '', true, 0);
3835
+ return lines.join('\n');
3836
+ }
3837
+ // =========================================================================
3838
+ // Symbol resolution helpers
3839
+ // =========================================================================
3840
+ /**
3841
+ * Find a symbol by name, handling disambiguation when multiple matches exist.
3842
+ * Returns the best match and a note about alternatives if any.
3843
+ */
3844
+ /**
3845
+ * Check if a node matches a symbol query.
3846
+ *
3847
+ * Accepts simple names (`run`) and three flavors of qualifier:
3848
+ * - dotted `Session.request` (TS/JS/Python)
3849
+ * - colon-pair `stage_apply::run` (Rust, C++, Ruby)
3850
+ * - slash `configurator/stage_apply` (path-ish)
3851
+ *
3852
+ * Multi-level qualifiers compose: `crate::configurator::stage_apply::run`
3853
+ * works. Rust path prefixes (`crate`, `super`, `self`) are stripped so
3854
+ * the canonical `crate::module::symbol` form resolves.
3855
+ *
3856
+ * Resolution order, last part must always equal `node.name`:
3857
+ * 1. Suffix-match against `qualifiedName` (handles class-scoped methods
3858
+ * where the extractor builds the qualified name from the AST stack)
3859
+ * 2. File-path containment (handles file-derived modules in Rust/
3860
+ * Python — `stage_apply::run` matches a `run` in `stage_apply.rs`)
3861
+ */
3862
+ matchesSymbol(node, symbol) {
3863
+ // Simple name match
3864
+ if (node.name === symbol)
3865
+ return true;
3866
+ // File basename match (e.g., "product-card" matches "product-card.liquid")
3867
+ if (node.kind === 'file' && node.name.replace(/\.[^.]+$/, '') === symbol)
3868
+ return true;
3869
+ // Qualified-name lookups: split on any supported separator. `\w` keeps
3870
+ // identifier chars (incl. `_`) intact; everything else is treated as
3871
+ // a separator we tolerate.
3872
+ if (!/[.\/]|::/.test(symbol))
3873
+ return false;
3874
+ const parts = symbol.split(/::|[./]/).filter((p) => p.length > 0);
3875
+ if (parts.length < 2)
3876
+ return false;
3877
+ const lastPart = parts[parts.length - 1];
3878
+ if (node.name !== lastPart)
3879
+ return false;
3880
+ // Stage 1: qualified-name suffix match. The extractor joins the
3881
+ // semantic hierarchy with `::`, so `Session.request` and
3882
+ // `Session::request` both become `Session::request` here.
3883
+ const colonSuffix = parts.join('::');
3884
+ if (node.qualifiedName.includes(colonSuffix))
3885
+ return true;
3886
+ // Stage 2: file-path containment. Rust modules and Python packages
3887
+ // are not in `qualifiedName` — they're encoded in the file path. So
3888
+ // `stage_apply::run` matches a `run` in any file whose path
3889
+ // contains a `stage_apply` segment (with or without an extension).
3890
+ //
3891
+ // Filter out Rust path prefixes that have no file-system equivalent.
3892
+ const containerHints = parts.slice(0, -1).filter((p) => !RUST_PATH_PREFIXES.has(p));
3893
+ if (containerHints.length === 0)
3894
+ return false;
3895
+ const segments = node.filePath.split('/').filter((s) => s.length > 0);
3896
+ return containerHints.every((hint) => segments.some((seg) => seg === hint || seg.replace(/\.[^.]+$/, '') === hint));
3897
+ }
3898
+ /**
3899
+ * Find ALL definitions matching a name, ranked, so codegraph_node can return
3900
+ * every overload instead of guessing one (the wrong guess → a Read). Keepers
3901
+ * rank before generated stubs (.pb.go etc.); stable within a group preserves
3902
+ * FTS order. Returns [] when nothing matches; a qualified lookup that finds no
3903
+ * exact match returns [] rather than a misleading fuzzy file hit (#173); a
3904
+ * bare name with no exact match falls back to the single top fuzzy result.
3905
+ */
3906
+ findSymbolMatches(cg, symbol) {
3907
+ const isQualified = /[.\/]|::/.test(symbol);
3908
+ // For a bare name, enumerate EVERY exact-name definition via the direct index
3909
+ // (not FTS, which caps + ranks): tokio's `poll` has 50+ defs and the one the
3910
+ // caller wants (`Harness::poll` at harness.rs:153) ranks below any search cut,
3911
+ // so it could be neither rendered nor pinned by the file/line disambiguator —
3912
+ // and the agent Read it. With the full set, the multi-overload render + the
3913
+ // file/line filter can both reach it.
3914
+ if (!isQualified) {
3915
+ const exact = cg.getNodesByName(symbol);
3916
+ if (exact.length > 0) {
3917
+ return [...exact].sort((a, b) => ((0, generated_detection_1.isGeneratedFile)(a.filePath) ? 1 : 0) - ((0, generated_detection_1.isGeneratedFile)(b.filePath) ? 1 : 0));
3918
+ }
3919
+ // No exact match — use the single top fuzzy result (e.g. a file basename).
3920
+ const fuzzy = cg.searchNodes(symbol, { limit: 10 });
3921
+ return fuzzy[0] ? [fuzzy[0].node] : [];
3922
+ }
3923
+ // Qualified lookup (`Session.request`, `stage_apply::run`): FTS + matchesSymbol.
3924
+ const limit = 50;
3925
+ let results = cg.searchNodes(symbol, { limit });
3926
+ // FTS strips colons, so `stage_apply::run` searches the literal
3927
+ // `stage_applyrun` and finds nothing. Re-search by the bare last part and
3928
+ // let `matchesSymbol` filter by qualifier.
3929
+ if (isQualified && results.length === 0) {
3930
+ const tail = lastQualifierPart(symbol);
3931
+ if (tail && tail !== symbol)
3932
+ results = cg.searchNodes(tail, { limit });
3933
+ }
3934
+ if (results.length === 0)
3935
+ return [];
3936
+ const exactMatches = results.filter((r) => this.matchesSymbol(r.node, symbol));
3937
+ if (exactMatches.length === 0) {
3938
+ // No exact match — a qualified lookup must not fall back to a fuzzy file
3939
+ // hit (#173); a bare name may use the single top fuzzy result.
3940
+ return isQualified ? [] : results[0] ? [results[0].node] : [];
3941
+ }
3942
+ // Down-rank generated files (.pb.go, .pulsar.go, _grpc.pb.go, …) so a flow
3943
+ // query prefers the keeper implementation over the protobuf-generated stub.
3944
+ return [...exactMatches]
3945
+ .sort((a, b) => ((0, generated_detection_1.isGeneratedFile)(a.node.filePath) ? 1 : 0) - ((0, generated_detection_1.isGeneratedFile)(b.node.filePath) ? 1 : 0))
3946
+ .map((r) => r.node);
3947
+ }
3948
+ /**
3949
+ * Find ALL symbols matching a name. Used by callers/callees/impact to aggregate
3950
+ * results across all matching symbols (e.g., multiple classes with an `execute` method).
3951
+ */
3952
+ findAllSymbols(cg, symbol) {
3953
+ let results = cg.searchNodes(symbol, { limit: 50 });
3954
+ // Mirror the fallback in `findSymbol` for qualified queries — FTS
3955
+ // strips colons, so a module-qualified lookup needs a second pass
3956
+ // by the bare last part.
3957
+ if (results.length === 0 && /[.\/]|::/.test(symbol)) {
3958
+ const tail = lastQualifierPart(symbol);
3959
+ if (tail && tail !== symbol)
3960
+ results = cg.searchNodes(tail, { limit: 50 });
3961
+ }
3962
+ if (results.length === 0) {
3963
+ return { nodes: [], note: '' };
3964
+ }
3965
+ const exactMatches = results.filter(r => this.matchesSymbol(r.node, symbol));
3966
+ if (exactMatches.length <= 1) {
3967
+ const node = exactMatches[0]?.node ?? results[0].node;
3968
+ return { nodes: [node], note: '' };
3969
+ }
3970
+ // Same generated-file down-rank as findSymbol — keeps callers/callees
3971
+ // /impact aggregation aligned (a query against "Send" returns the
3972
+ // hand-written implementations before the protobuf scaffold).
3973
+ const ranked = [...exactMatches].sort((a, b) => {
3974
+ const aGen = (0, generated_detection_1.isGeneratedFile)(a.node.filePath) ? 1 : 0;
3975
+ const bGen = (0, generated_detection_1.isGeneratedFile)(b.node.filePath) ? 1 : 0;
3976
+ return aGen - bGen;
3977
+ });
3978
+ const locations = ranked.map(r => `${r.node.kind} at ${r.node.filePath}:${r.node.startLine}`);
3979
+ const note = `\n\n> **Note:** Aggregated results across ${ranked.length} symbols named "${symbol}": ${locations.join(', ')}`;
3980
+ return { nodes: ranked.map(r => r.node), note };
3981
+ }
3982
+ /**
3983
+ * Truncate output if it exceeds the maximum length
3984
+ */
3985
+ truncateOutput(text) {
3986
+ if (text.length <= MAX_OUTPUT_LENGTH)
3987
+ return text;
3988
+ const truncated = text.slice(0, MAX_OUTPUT_LENGTH);
3989
+ const lastNewline = truncated.lastIndexOf('\n');
3990
+ const cutPoint = lastNewline > MAX_OUTPUT_LENGTH * 0.8 ? lastNewline : MAX_OUTPUT_LENGTH;
3991
+ return truncated.slice(0, cutPoint) + '\n\n... (output truncated)';
3992
+ }
3993
+ // =========================================================================
3994
+ // Formatting helpers (compact by default to reduce context usage)
3995
+ // =========================================================================
3996
+ formatSearchResults(results) {
3997
+ const lines = [`**Search Results (${results.length} found)**`, ''];
3998
+ for (const result of results) {
3999
+ const { node } = result;
4000
+ const location = node.startLine ? `:${node.startLine}` : '';
4001
+ // Compact format: one line per result with key info
4002
+ lines.push(`**${node.name}** (${node.kind})`);
4003
+ lines.push(`${node.filePath}${location}`);
4004
+ if (node.signature)
4005
+ lines.push(`\`${node.signature}\``);
4006
+ lines.push('');
4007
+ }
4008
+ return lines.join('\n');
4009
+ }
4010
+ formatNodeList(nodes, title, labels) {
4011
+ const lines = [`**${title} (${nodes.length} found)**`, ''];
4012
+ for (const node of nodes) {
4013
+ const location = node.startLine ? `:${node.startLine}` : '';
4014
+ // Compact: just name, kind, location — plus the relationship when it
4015
+ // isn't a plain call (callback registration, instantiation, …).
4016
+ const label = labels?.get(node.id);
4017
+ lines.push(`- ${node.name} (${node.kind}) - ${node.filePath}${location}${label ? ` — via ${label}` : ''}`);
4018
+ }
4019
+ return lines.join('\n');
4020
+ }
4021
+ /**
4022
+ * Relationship label for a non-`calls` edge in callers/callees lists. A
4023
+ * function-as-value edge (#756) is the high-signal one: `callers(cb)`
4024
+ * showing "via callback registration" tells the agent this is where the
4025
+ * callback is WIRED, not where it's invoked.
4026
+ */
4027
+ edgeLabel(edge) {
4028
+ if (edge.kind === 'calls')
4029
+ return null;
4030
+ if (edge.metadata?.fnRef === true)
4031
+ return 'callback registration';
4032
+ if (edge.kind === 'instantiates')
4033
+ return 'instantiation';
4034
+ if (edge.kind === 'imports')
4035
+ return 'import';
4036
+ if (edge.kind === 'references')
4037
+ return 'reference';
4038
+ return edge.kind;
4039
+ }
4040
+ formatImpact(symbol, impact) {
4041
+ const nodeCount = impact.nodes.size;
4042
+ // Compact format: just list affected symbols grouped by file
4043
+ const lines = [
4044
+ `**Impact: "${symbol}" affects ${nodeCount} symbols**`,
4045
+ '',
4046
+ ];
4047
+ // Group by file
4048
+ const byFile = new Map();
4049
+ for (const node of impact.nodes.values()) {
4050
+ const existing = byFile.get(node.filePath) || [];
4051
+ existing.push(node);
4052
+ byFile.set(node.filePath, existing);
4053
+ }
4054
+ for (const [file, nodes] of byFile) {
4055
+ lines.push(`**${file}:**`);
4056
+ // Compact: inline list
4057
+ const nodeList = nodes.map(n => `${n.name}:${n.startLine}`).join(', ');
4058
+ lines.push(nodeList);
4059
+ lines.push('');
4060
+ }
4061
+ return lines.join('\n');
4062
+ }
4063
+ /**
4064
+ * Build a compact structural outline of a container symbol from its
4065
+ * indexed children (methods, fields, properties, …) — name, kind,
4066
+ * line number, and signature — so the agent gets the shape of a class
4067
+ * without the full source of every method. Returns '' when the container
4068
+ * has no indexed children, so the caller can fall back to full source.
4069
+ */
4070
+ buildContainerOutline(cg, node) {
4071
+ const children = cg.getChildren(node.id)
4072
+ .filter(c => c.kind !== 'import' && c.kind !== 'export')
4073
+ .sort((a, b) => (a.startLine ?? 0) - (b.startLine ?? 0));
4074
+ if (children.length === 0)
4075
+ return '';
4076
+ const lines = [`**Members (${children.length}):**`, ''];
4077
+ for (const c of children) {
4078
+ const loc = c.startLine ? `:${c.startLine}` : '';
4079
+ const sig = c.signature ? ` — \`${c.signature}\`` : '';
4080
+ lines.push(`- ${c.name} (${c.kind})${loc}${sig}`);
4081
+ }
4082
+ return lines.join('\n');
4083
+ }
4084
+ formatNodeDetails(node, code, outline) {
4085
+ const location = node.startLine ? `:${node.startLine}` : '';
4086
+ const lines = [
4087
+ `**${node.name}** (${node.kind})`,
4088
+ '',
4089
+ `**Location:** ${node.filePath}${location}`,
4090
+ ];
4091
+ if (node.signature) {
4092
+ lines.push(`**Signature:** \`${node.signature}\``);
4093
+ }
4094
+ // Only include docstring if it's short and useful
4095
+ if (node.docstring && node.docstring.length < 200) {
4096
+ lines.push('', node.docstring);
4097
+ }
4098
+ if (outline) {
4099
+ lines.push('', outline, '', `> Structural outline only. Read \`${node.filePath}\` or call codegraph_node on a specific member for its body.`);
4100
+ }
4101
+ else if (code) {
4102
+ // Line-numbered (cat -n style, like codegraph_explore and Read) so the
4103
+ // agent can cite/edit exact lines without re-Reading the file for them.
4104
+ const numbered = node.startLine ? numberSourceLines(code, node.startLine) : code;
4105
+ lines.push('', '```' + node.language, numbered, '```');
4106
+ }
4107
+ return lines.join('\n');
4108
+ }
4109
+ textResult(text) {
4110
+ return {
4111
+ content: [{ type: 'text', text }],
4112
+ };
4113
+ }
4114
+ errorResult(message) {
4115
+ return {
4116
+ content: [{ type: 'text', text: `Error: ${message}` }],
4117
+ isError: true,
4118
+ };
4119
+ }
4120
+ }
4121
+ exports.ToolHandler = ToolHandler;
4122
+ //# sourceMappingURL=tools.js.map