@specship/specship 0.11.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 (879) hide show
  1. package/.claude-plugin/plugin.json +6 -0
  2. package/LICENSE +21 -0
  3. package/README.md +583 -0
  4. package/agents/specship-explorer.md +29 -0
  5. package/commands/ss-behaviour.md +116 -0
  6. package/commands/ss-check.md +43 -0
  7. package/commands/ss-design-implement.md +84 -0
  8. package/commands/ss-design-loop.md +125 -0
  9. package/commands/ss-explore.md +43 -0
  10. package/commands/ss-spec.md +118 -0
  11. package/dist/activation/starter-prompt.d.ts +57 -0
  12. package/dist/activation/starter-prompt.d.ts.map +1 -0
  13. package/dist/activation/starter-prompt.js +164 -0
  14. package/dist/activation/starter-prompt.js.map +1 -0
  15. package/dist/analytics/specship-impact.d.ts +72 -0
  16. package/dist/analytics/specship-impact.d.ts.map +1 -0
  17. package/dist/analytics/specship-impact.js +216 -0
  18. package/dist/analytics/specship-impact.js.map +1 -0
  19. package/dist/behaviour/behaviour-surface.d.ts +59 -0
  20. package/dist/behaviour/behaviour-surface.d.ts.map +1 -0
  21. package/dist/behaviour/behaviour-surface.js +112 -0
  22. package/dist/behaviour/behaviour-surface.js.map +1 -0
  23. package/dist/bin/node-version-check.d.ts +37 -0
  24. package/dist/bin/node-version-check.d.ts.map +1 -0
  25. package/dist/bin/node-version-check.js +79 -0
  26. package/dist/bin/node-version-check.js.map +1 -0
  27. package/dist/bin/specship.d.ts +25 -0
  28. package/dist/bin/specship.d.ts.map +1 -0
  29. package/dist/bin/specship.js +2823 -0
  30. package/dist/bin/specship.js.map +1 -0
  31. package/dist/bin/uninstall.d.ts +13 -0
  32. package/dist/bin/uninstall.d.ts.map +1 -0
  33. package/dist/bin/uninstall.js +35 -0
  34. package/dist/bin/uninstall.js.map +1 -0
  35. package/dist/context/formatter.d.ts +30 -0
  36. package/dist/context/formatter.d.ts.map +1 -0
  37. package/dist/context/formatter.js +263 -0
  38. package/dist/context/formatter.js.map +1 -0
  39. package/dist/context/index.d.ts +119 -0
  40. package/dist/context/index.d.ts.map +1 -0
  41. package/dist/context/index.js +1289 -0
  42. package/dist/context/index.js.map +1 -0
  43. package/dist/context/markers.d.ts +19 -0
  44. package/dist/context/markers.d.ts.map +1 -0
  45. package/dist/context/markers.js +22 -0
  46. package/dist/context/markers.js.map +1 -0
  47. package/dist/db/index.d.ts +103 -0
  48. package/dist/db/index.d.ts.map +1 -0
  49. package/dist/db/index.js +279 -0
  50. package/dist/db/index.js.map +1 -0
  51. package/dist/db/migrations.d.ts +44 -0
  52. package/dist/db/migrations.d.ts.map +1 -0
  53. package/dist/db/migrations.js +503 -0
  54. package/dist/db/migrations.js.map +1 -0
  55. package/dist/db/queries.d.ts +357 -0
  56. package/dist/db/queries.d.ts.map +1 -0
  57. package/dist/db/queries.js +1504 -0
  58. package/dist/db/queries.js.map +1 -0
  59. package/dist/db/schema.sql +451 -0
  60. package/dist/db/spec-queries.d.ts +130 -0
  61. package/dist/db/spec-queries.d.ts.map +1 -0
  62. package/dist/db/spec-queries.js +738 -0
  63. package/dist/db/spec-queries.js.map +1 -0
  64. package/dist/db/sqlite-adapter.d.ts +65 -0
  65. package/dist/db/sqlite-adapter.d.ts.map +1 -0
  66. package/dist/db/sqlite-adapter.js +214 -0
  67. package/dist/db/sqlite-adapter.js.map +1 -0
  68. package/dist/designer/artifact-store.js +54 -0
  69. package/dist/designer/browser.js +141 -0
  70. package/dist/designer/cdp-ensure.js +60 -0
  71. package/dist/designer/cdp-env.js +18 -0
  72. package/dist/designer/cdp-trace.js +599 -0
  73. package/dist/designer/cross-platform.js +74 -0
  74. package/dist/designer/designer-controller.js +1413 -0
  75. package/dist/designer/file-panel.js +39 -0
  76. package/dist/designer/interstitials.js +97 -0
  77. package/dist/designer/oopif-reader.js +176 -0
  78. package/dist/designer/package-meta.js +18 -0
  79. package/dist/designer/preview-host.js +50 -0
  80. package/dist/designer/repo-root.js +31 -0
  81. package/dist/designer/run-state.js +353 -0
  82. package/dist/designer/session-store.js +59 -0
  83. package/dist/designer/ui-anchors.js +651 -0
  84. package/dist/directory.d.ts +67 -0
  85. package/dist/directory.d.ts.map +1 -0
  86. package/dist/directory.js +267 -0
  87. package/dist/directory.js.map +1 -0
  88. package/dist/enforce/enforce.d.ts +70 -0
  89. package/dist/enforce/enforce.d.ts.map +1 -0
  90. package/dist/enforce/enforce.js +125 -0
  91. package/dist/enforce/enforce.js.map +1 -0
  92. package/dist/errors.d.ts +136 -0
  93. package/dist/errors.d.ts.map +1 -0
  94. package/dist/errors.js +219 -0
  95. package/dist/errors.js.map +1 -0
  96. package/dist/extraction/dfm-extractor.d.ts +31 -0
  97. package/dist/extraction/dfm-extractor.d.ts.map +1 -0
  98. package/dist/extraction/dfm-extractor.js +151 -0
  99. package/dist/extraction/dfm-extractor.js.map +1 -0
  100. package/dist/extraction/generated-detection.d.ts +30 -0
  101. package/dist/extraction/generated-detection.d.ts.map +1 -0
  102. package/dist/extraction/generated-detection.js +80 -0
  103. package/dist/extraction/generated-detection.js.map +1 -0
  104. package/dist/extraction/grammars.d.ts +100 -0
  105. package/dist/extraction/grammars.d.ts.map +1 -0
  106. package/dist/extraction/grammars.js +426 -0
  107. package/dist/extraction/grammars.js.map +1 -0
  108. package/dist/extraction/index.d.ts +138 -0
  109. package/dist/extraction/index.d.ts.map +1 -0
  110. package/dist/extraction/index.js +1394 -0
  111. package/dist/extraction/index.js.map +1 -0
  112. package/dist/extraction/languages/c-cpp.d.ts +4 -0
  113. package/dist/extraction/languages/c-cpp.d.ts.map +1 -0
  114. package/dist/extraction/languages/c-cpp.js +171 -0
  115. package/dist/extraction/languages/c-cpp.js.map +1 -0
  116. package/dist/extraction/languages/csharp.d.ts +3 -0
  117. package/dist/extraction/languages/csharp.d.ts.map +1 -0
  118. package/dist/extraction/languages/csharp.js +73 -0
  119. package/dist/extraction/languages/csharp.js.map +1 -0
  120. package/dist/extraction/languages/dart.d.ts +3 -0
  121. package/dist/extraction/languages/dart.d.ts.map +1 -0
  122. package/dist/extraction/languages/dart.js +192 -0
  123. package/dist/extraction/languages/dart.js.map +1 -0
  124. package/dist/extraction/languages/go.d.ts +3 -0
  125. package/dist/extraction/languages/go.d.ts.map +1 -0
  126. package/dist/extraction/languages/go.js +74 -0
  127. package/dist/extraction/languages/go.js.map +1 -0
  128. package/dist/extraction/languages/index.d.ts +10 -0
  129. package/dist/extraction/languages/index.d.ts.map +1 -0
  130. package/dist/extraction/languages/index.js +51 -0
  131. package/dist/extraction/languages/index.js.map +1 -0
  132. package/dist/extraction/languages/java.d.ts +3 -0
  133. package/dist/extraction/languages/java.d.ts.map +1 -0
  134. package/dist/extraction/languages/java.js +70 -0
  135. package/dist/extraction/languages/java.js.map +1 -0
  136. package/dist/extraction/languages/javascript.d.ts +3 -0
  137. package/dist/extraction/languages/javascript.d.ts.map +1 -0
  138. package/dist/extraction/languages/javascript.js +90 -0
  139. package/dist/extraction/languages/javascript.js.map +1 -0
  140. package/dist/extraction/languages/kotlin.d.ts +3 -0
  141. package/dist/extraction/languages/kotlin.d.ts.map +1 -0
  142. package/dist/extraction/languages/kotlin.js +259 -0
  143. package/dist/extraction/languages/kotlin.js.map +1 -0
  144. package/dist/extraction/languages/lua.d.ts +3 -0
  145. package/dist/extraction/languages/lua.d.ts.map +1 -0
  146. package/dist/extraction/languages/lua.js +150 -0
  147. package/dist/extraction/languages/lua.js.map +1 -0
  148. package/dist/extraction/languages/luau.d.ts +3 -0
  149. package/dist/extraction/languages/luau.d.ts.map +1 -0
  150. package/dist/extraction/languages/luau.js +37 -0
  151. package/dist/extraction/languages/luau.js.map +1 -0
  152. package/dist/extraction/languages/objc.d.ts +3 -0
  153. package/dist/extraction/languages/objc.d.ts.map +1 -0
  154. package/dist/extraction/languages/objc.js +133 -0
  155. package/dist/extraction/languages/objc.js.map +1 -0
  156. package/dist/extraction/languages/pascal.d.ts +3 -0
  157. package/dist/extraction/languages/pascal.d.ts.map +1 -0
  158. package/dist/extraction/languages/pascal.js +66 -0
  159. package/dist/extraction/languages/pascal.js.map +1 -0
  160. package/dist/extraction/languages/php.d.ts +3 -0
  161. package/dist/extraction/languages/php.d.ts.map +1 -0
  162. package/dist/extraction/languages/php.js +107 -0
  163. package/dist/extraction/languages/php.js.map +1 -0
  164. package/dist/extraction/languages/python.d.ts +3 -0
  165. package/dist/extraction/languages/python.d.ts.map +1 -0
  166. package/dist/extraction/languages/python.js +56 -0
  167. package/dist/extraction/languages/python.js.map +1 -0
  168. package/dist/extraction/languages/ruby.d.ts +3 -0
  169. package/dist/extraction/languages/ruby.d.ts.map +1 -0
  170. package/dist/extraction/languages/ruby.js +114 -0
  171. package/dist/extraction/languages/ruby.js.map +1 -0
  172. package/dist/extraction/languages/rust.d.ts +3 -0
  173. package/dist/extraction/languages/rust.d.ts.map +1 -0
  174. package/dist/extraction/languages/rust.js +109 -0
  175. package/dist/extraction/languages/rust.js.map +1 -0
  176. package/dist/extraction/languages/scala.d.ts +3 -0
  177. package/dist/extraction/languages/scala.d.ts.map +1 -0
  178. package/dist/extraction/languages/scala.js +139 -0
  179. package/dist/extraction/languages/scala.js.map +1 -0
  180. package/dist/extraction/languages/swift.d.ts +3 -0
  181. package/dist/extraction/languages/swift.d.ts.map +1 -0
  182. package/dist/extraction/languages/swift.js +91 -0
  183. package/dist/extraction/languages/swift.js.map +1 -0
  184. package/dist/extraction/languages/typescript.d.ts +3 -0
  185. package/dist/extraction/languages/typescript.d.ts.map +1 -0
  186. package/dist/extraction/languages/typescript.js +129 -0
  187. package/dist/extraction/languages/typescript.js.map +1 -0
  188. package/dist/extraction/liquid-extractor.d.ts +52 -0
  189. package/dist/extraction/liquid-extractor.d.ts.map +1 -0
  190. package/dist/extraction/liquid-extractor.js +313 -0
  191. package/dist/extraction/liquid-extractor.js.map +1 -0
  192. package/dist/extraction/mybatis-extractor.d.ts +48 -0
  193. package/dist/extraction/mybatis-extractor.d.ts.map +1 -0
  194. package/dist/extraction/mybatis-extractor.js +198 -0
  195. package/dist/extraction/mybatis-extractor.js.map +1 -0
  196. package/dist/extraction/parse-worker.d.ts +8 -0
  197. package/dist/extraction/parse-worker.d.ts.map +1 -0
  198. package/dist/extraction/parse-worker.js +94 -0
  199. package/dist/extraction/parse-worker.js.map +1 -0
  200. package/dist/extraction/specs/markdown-spec-extractor.d.ts +114 -0
  201. package/dist/extraction/specs/markdown-spec-extractor.d.ts.map +1 -0
  202. package/dist/extraction/specs/markdown-spec-extractor.js +699 -0
  203. package/dist/extraction/specs/markdown-spec-extractor.js.map +1 -0
  204. package/dist/extraction/specs/types.d.ts +39 -0
  205. package/dist/extraction/specs/types.d.ts.map +1 -0
  206. package/dist/extraction/specs/types.js +8 -0
  207. package/dist/extraction/specs/types.js.map +1 -0
  208. package/dist/extraction/svelte-extractor.d.ts +56 -0
  209. package/dist/extraction/svelte-extractor.d.ts.map +1 -0
  210. package/dist/extraction/svelte-extractor.js +272 -0
  211. package/dist/extraction/svelte-extractor.js.map +1 -0
  212. package/dist/extraction/tree-sitter-helpers.d.ts +28 -0
  213. package/dist/extraction/tree-sitter-helpers.d.ts.map +1 -0
  214. package/dist/extraction/tree-sitter-helpers.js +103 -0
  215. package/dist/extraction/tree-sitter-helpers.js.map +1 -0
  216. package/dist/extraction/tree-sitter-types.d.ts +193 -0
  217. package/dist/extraction/tree-sitter-types.d.ts.map +1 -0
  218. package/dist/extraction/tree-sitter-types.js +10 -0
  219. package/dist/extraction/tree-sitter-types.js.map +1 -0
  220. package/dist/extraction/tree-sitter.d.ts +317 -0
  221. package/dist/extraction/tree-sitter.d.ts.map +1 -0
  222. package/dist/extraction/tree-sitter.js +3092 -0
  223. package/dist/extraction/tree-sitter.js.map +1 -0
  224. package/dist/extraction/vue-extractor.d.ts +51 -0
  225. package/dist/extraction/vue-extractor.d.ts.map +1 -0
  226. package/dist/extraction/vue-extractor.js +251 -0
  227. package/dist/extraction/vue-extractor.js.map +1 -0
  228. package/dist/extraction/wasm/tree-sitter-lua.wasm +0 -0
  229. package/dist/extraction/wasm/tree-sitter-luau.wasm +0 -0
  230. package/dist/extraction/wasm/tree-sitter-pascal.wasm +0 -0
  231. package/dist/extraction/wasm/tree-sitter-scala.wasm +0 -0
  232. package/dist/extraction/wasm-runtime-flags.d.ts +38 -0
  233. package/dist/extraction/wasm-runtime-flags.d.ts.map +1 -0
  234. package/dist/extraction/wasm-runtime-flags.js +106 -0
  235. package/dist/extraction/wasm-runtime-flags.js.map +1 -0
  236. package/dist/fitness/fitness.d.ts +75 -0
  237. package/dist/fitness/fitness.d.ts.map +1 -0
  238. package/dist/fitness/fitness.js +204 -0
  239. package/dist/fitness/fitness.js.map +1 -0
  240. package/dist/graph/index.d.ts +8 -0
  241. package/dist/graph/index.d.ts.map +1 -0
  242. package/dist/graph/index.js +13 -0
  243. package/dist/graph/index.js.map +1 -0
  244. package/dist/graph/maintainability.d.ts +115 -0
  245. package/dist/graph/maintainability.d.ts.map +1 -0
  246. package/dist/graph/maintainability.js +299 -0
  247. package/dist/graph/maintainability.js.map +1 -0
  248. package/dist/graph/queries.d.ts +106 -0
  249. package/dist/graph/queries.d.ts.map +1 -0
  250. package/dist/graph/queries.js +366 -0
  251. package/dist/graph/queries.js.map +1 -0
  252. package/dist/graph/traversal.d.ts +127 -0
  253. package/dist/graph/traversal.d.ts.map +1 -0
  254. package/dist/graph/traversal.js +531 -0
  255. package/dist/graph/traversal.js.map +1 -0
  256. package/dist/health/smoke-check.d.ts +85 -0
  257. package/dist/health/smoke-check.d.ts.map +1 -0
  258. package/dist/health/smoke-check.js +246 -0
  259. package/dist/health/smoke-check.js.map +1 -0
  260. package/dist/index.d.ts +674 -0
  261. package/dist/index.d.ts.map +1 -0
  262. package/dist/index.js +1473 -0
  263. package/dist/index.js.map +1 -0
  264. package/dist/installer/config-writer.d.ts +28 -0
  265. package/dist/installer/config-writer.d.ts.map +1 -0
  266. package/dist/installer/config-writer.js +91 -0
  267. package/dist/installer/config-writer.js.map +1 -0
  268. package/dist/installer/index.d.ts +100 -0
  269. package/dist/installer/index.d.ts.map +1 -0
  270. package/dist/installer/index.js +442 -0
  271. package/dist/installer/index.js.map +1 -0
  272. package/dist/installer/init-offer.d.ts +31 -0
  273. package/dist/installer/init-offer.d.ts.map +1 -0
  274. package/dist/installer/init-offer.js +30 -0
  275. package/dist/installer/init-offer.js.map +1 -0
  276. package/dist/installer/instructions-template.d.ts +36 -0
  277. package/dist/installer/instructions-template.d.ts.map +1 -0
  278. package/dist/installer/instructions-template.js +57 -0
  279. package/dist/installer/instructions-template.js.map +1 -0
  280. package/dist/installer/targets/claude.d.ts +146 -0
  281. package/dist/installer/targets/claude.d.ts.map +1 -0
  282. package/dist/installer/targets/claude.js +864 -0
  283. package/dist/installer/targets/claude.js.map +1 -0
  284. package/dist/installer/targets/registry.d.ts +19 -0
  285. package/dist/installer/targets/registry.d.ts.map +1 -0
  286. package/dist/installer/targets/registry.js +31 -0
  287. package/dist/installer/targets/registry.js.map +1 -0
  288. package/dist/installer/targets/shared.d.ts +76 -0
  289. package/dist/installer/targets/shared.d.ts.map +1 -0
  290. package/dist/installer/targets/shared.js +260 -0
  291. package/dist/installer/targets/shared.js.map +1 -0
  292. package/dist/installer/targets/types.d.ts +95 -0
  293. package/dist/installer/targets/types.d.ts.map +1 -0
  294. package/dist/installer/targets/types.js +12 -0
  295. package/dist/installer/targets/types.js.map +1 -0
  296. package/dist/isolation/worktree.d.ts +65 -0
  297. package/dist/isolation/worktree.d.ts.map +1 -0
  298. package/dist/isolation/worktree.js +231 -0
  299. package/dist/isolation/worktree.js.map +1 -0
  300. package/dist/mcp/daemon-paths.d.ts +46 -0
  301. package/dist/mcp/daemon-paths.d.ts.map +1 -0
  302. package/dist/mcp/daemon-paths.js +125 -0
  303. package/dist/mcp/daemon-paths.js.map +1 -0
  304. package/dist/mcp/daemon.d.ts +161 -0
  305. package/dist/mcp/daemon.d.ts.map +1 -0
  306. package/dist/mcp/daemon.js +403 -0
  307. package/dist/mcp/daemon.js.map +1 -0
  308. package/dist/mcp/designer-tools.d.ts +33 -0
  309. package/dist/mcp/designer-tools.d.ts.map +1 -0
  310. package/dist/mcp/designer-tools.js +313 -0
  311. package/dist/mcp/designer-tools.js.map +1 -0
  312. package/dist/mcp/engine.d.ts +105 -0
  313. package/dist/mcp/engine.d.ts.map +1 -0
  314. package/dist/mcp/engine.js +270 -0
  315. package/dist/mcp/engine.js.map +1 -0
  316. package/dist/mcp/fitness-tool.d.ts +12 -0
  317. package/dist/mcp/fitness-tool.d.ts.map +1 -0
  318. package/dist/mcp/fitness-tool.js +46 -0
  319. package/dist/mcp/fitness-tool.js.map +1 -0
  320. package/dist/mcp/index.d.ts +112 -0
  321. package/dist/mcp/index.d.ts.map +1 -0
  322. package/dist/mcp/index.js +477 -0
  323. package/dist/mcp/index.js.map +1 -0
  324. package/dist/mcp/maintainability-tool.d.ts +13 -0
  325. package/dist/mcp/maintainability-tool.d.ts.map +1 -0
  326. package/dist/mcp/maintainability-tool.js +64 -0
  327. package/dist/mcp/maintainability-tool.js.map +1 -0
  328. package/dist/mcp/proxy.d.ts +81 -0
  329. package/dist/mcp/proxy.d.ts.map +1 -0
  330. package/dist/mcp/proxy.js +510 -0
  331. package/dist/mcp/proxy.js.map +1 -0
  332. package/dist/mcp/server-instructions.d.ts +18 -0
  333. package/dist/mcp/server-instructions.d.ts.map +1 -0
  334. package/dist/mcp/server-instructions.js +79 -0
  335. package/dist/mcp/server-instructions.js.map +1 -0
  336. package/dist/mcp/session.d.ts +77 -0
  337. package/dist/mcp/session.d.ts.map +1 -0
  338. package/dist/mcp/session.js +294 -0
  339. package/dist/mcp/session.js.map +1 -0
  340. package/dist/mcp/spec-tools.d.ts +39 -0
  341. package/dist/mcp/spec-tools.d.ts.map +1 -0
  342. package/dist/mcp/spec-tools.js +534 -0
  343. package/dist/mcp/spec-tools.js.map +1 -0
  344. package/dist/mcp/tools.d.ts +417 -0
  345. package/dist/mcp/tools.d.ts.map +1 -0
  346. package/dist/mcp/tools.js +3179 -0
  347. package/dist/mcp/tools.js.map +1 -0
  348. package/dist/mcp/transport.d.ts +188 -0
  349. package/dist/mcp/transport.d.ts.map +1 -0
  350. package/dist/mcp/transport.js +343 -0
  351. package/dist/mcp/transport.js.map +1 -0
  352. package/dist/mcp/version.d.ts +19 -0
  353. package/dist/mcp/version.d.ts.map +1 -0
  354. package/dist/mcp/version.js +71 -0
  355. package/dist/mcp/version.js.map +1 -0
  356. package/dist/reflect/apply.d.ts +31 -0
  357. package/dist/reflect/apply.d.ts.map +1 -0
  358. package/dist/reflect/apply.js +286 -0
  359. package/dist/reflect/apply.js.map +1 -0
  360. package/dist/reflect/hash.d.ts +20 -0
  361. package/dist/reflect/hash.d.ts.map +1 -0
  362. package/dist/reflect/hash.js +36 -0
  363. package/dist/reflect/hash.js.map +1 -0
  364. package/dist/reflect/index.d.ts +16 -0
  365. package/dist/reflect/index.d.ts.map +1 -0
  366. package/dist/reflect/index.js +43 -0
  367. package/dist/reflect/index.js.map +1 -0
  368. package/dist/reflect/miner.d.ts +21 -0
  369. package/dist/reflect/miner.d.ts.map +1 -0
  370. package/dist/reflect/miner.js +463 -0
  371. package/dist/reflect/miner.js.map +1 -0
  372. package/dist/reflect/store.d.ts +31 -0
  373. package/dist/reflect/store.d.ts.map +1 -0
  374. package/dist/reflect/store.js +101 -0
  375. package/dist/reflect/store.js.map +1 -0
  376. package/dist/reflect/sweep.d.ts +26 -0
  377. package/dist/reflect/sweep.d.ts.map +1 -0
  378. package/dist/reflect/sweep.js +42 -0
  379. package/dist/reflect/sweep.js.map +1 -0
  380. package/dist/reflect/targets.d.ts +52 -0
  381. package/dist/reflect/targets.d.ts.map +1 -0
  382. package/dist/reflect/targets.js +192 -0
  383. package/dist/reflect/targets.js.map +1 -0
  384. package/dist/reflect/types.d.ts +96 -0
  385. package/dist/reflect/types.d.ts.map +1 -0
  386. package/dist/reflect/types.js +13 -0
  387. package/dist/reflect/types.js.map +1 -0
  388. package/dist/resolution/brief-link-resolver.d.ts +114 -0
  389. package/dist/resolution/brief-link-resolver.d.ts.map +1 -0
  390. package/dist/resolution/brief-link-resolver.js +261 -0
  391. package/dist/resolution/brief-link-resolver.js.map +1 -0
  392. package/dist/resolution/callback-synthesizer.d.ts +10 -0
  393. package/dist/resolution/callback-synthesizer.d.ts.map +1 -0
  394. package/dist/resolution/callback-synthesizer.js +1300 -0
  395. package/dist/resolution/callback-synthesizer.js.map +1 -0
  396. package/dist/resolution/domain-gap-seed.d.ts +60 -0
  397. package/dist/resolution/domain-gap-seed.d.ts.map +1 -0
  398. package/dist/resolution/domain-gap-seed.js +87 -0
  399. package/dist/resolution/domain-gap-seed.js.map +1 -0
  400. package/dist/resolution/frameworks/cargo-workspace.d.ts +18 -0
  401. package/dist/resolution/frameworks/cargo-workspace.d.ts.map +1 -0
  402. package/dist/resolution/frameworks/cargo-workspace.js +225 -0
  403. package/dist/resolution/frameworks/cargo-workspace.js.map +1 -0
  404. package/dist/resolution/frameworks/csharp.d.ts +8 -0
  405. package/dist/resolution/frameworks/csharp.d.ts.map +1 -0
  406. package/dist/resolution/frameworks/csharp.js +241 -0
  407. package/dist/resolution/frameworks/csharp.js.map +1 -0
  408. package/dist/resolution/frameworks/drupal.d.ts +51 -0
  409. package/dist/resolution/frameworks/drupal.d.ts.map +1 -0
  410. package/dist/resolution/frameworks/drupal.js +367 -0
  411. package/dist/resolution/frameworks/drupal.js.map +1 -0
  412. package/dist/resolution/frameworks/expo-modules.d.ts +3 -0
  413. package/dist/resolution/frameworks/expo-modules.d.ts.map +1 -0
  414. package/dist/resolution/frameworks/expo-modules.js +143 -0
  415. package/dist/resolution/frameworks/expo-modules.js.map +1 -0
  416. package/dist/resolution/frameworks/express.d.ts +8 -0
  417. package/dist/resolution/frameworks/express.d.ts.map +1 -0
  418. package/dist/resolution/frameworks/express.js +308 -0
  419. package/dist/resolution/frameworks/express.js.map +1 -0
  420. package/dist/resolution/frameworks/fabric.d.ts +3 -0
  421. package/dist/resolution/frameworks/fabric.d.ts.map +1 -0
  422. package/dist/resolution/frameworks/fabric.js +354 -0
  423. package/dist/resolution/frameworks/fabric.js.map +1 -0
  424. package/dist/resolution/frameworks/go.d.ts +8 -0
  425. package/dist/resolution/frameworks/go.d.ts.map +1 -0
  426. package/dist/resolution/frameworks/go.js +161 -0
  427. package/dist/resolution/frameworks/go.js.map +1 -0
  428. package/dist/resolution/frameworks/index.d.ts +48 -0
  429. package/dist/resolution/frameworks/index.d.ts.map +1 -0
  430. package/dist/resolution/frameworks/index.js +161 -0
  431. package/dist/resolution/frameworks/index.js.map +1 -0
  432. package/dist/resolution/frameworks/java.d.ts +8 -0
  433. package/dist/resolution/frameworks/java.d.ts.map +1 -0
  434. package/dist/resolution/frameworks/java.js +504 -0
  435. package/dist/resolution/frameworks/java.js.map +1 -0
  436. package/dist/resolution/frameworks/laravel.d.ts +13 -0
  437. package/dist/resolution/frameworks/laravel.d.ts.map +1 -0
  438. package/dist/resolution/frameworks/laravel.js +257 -0
  439. package/dist/resolution/frameworks/laravel.js.map +1 -0
  440. package/dist/resolution/frameworks/nestjs.d.ts +26 -0
  441. package/dist/resolution/frameworks/nestjs.d.ts.map +1 -0
  442. package/dist/resolution/frameworks/nestjs.js +698 -0
  443. package/dist/resolution/frameworks/nestjs.js.map +1 -0
  444. package/dist/resolution/frameworks/play.d.ts +19 -0
  445. package/dist/resolution/frameworks/play.d.ts.map +1 -0
  446. package/dist/resolution/frameworks/play.js +111 -0
  447. package/dist/resolution/frameworks/play.js.map +1 -0
  448. package/dist/resolution/frameworks/python.d.ts +10 -0
  449. package/dist/resolution/frameworks/python.d.ts.map +1 -0
  450. package/dist/resolution/frameworks/python.js +396 -0
  451. package/dist/resolution/frameworks/python.js.map +1 -0
  452. package/dist/resolution/frameworks/react-native.d.ts +3 -0
  453. package/dist/resolution/frameworks/react-native.d.ts.map +1 -0
  454. package/dist/resolution/frameworks/react-native.js +360 -0
  455. package/dist/resolution/frameworks/react-native.js.map +1 -0
  456. package/dist/resolution/frameworks/react.d.ts +8 -0
  457. package/dist/resolution/frameworks/react.d.ts.map +1 -0
  458. package/dist/resolution/frameworks/react.js +365 -0
  459. package/dist/resolution/frameworks/react.js.map +1 -0
  460. package/dist/resolution/frameworks/ruby.d.ts +8 -0
  461. package/dist/resolution/frameworks/ruby.d.ts.map +1 -0
  462. package/dist/resolution/frameworks/ruby.js +302 -0
  463. package/dist/resolution/frameworks/ruby.js.map +1 -0
  464. package/dist/resolution/frameworks/rust.d.ts +8 -0
  465. package/dist/resolution/frameworks/rust.d.ts.map +1 -0
  466. package/dist/resolution/frameworks/rust.js +304 -0
  467. package/dist/resolution/frameworks/rust.js.map +1 -0
  468. package/dist/resolution/frameworks/svelte.d.ts +9 -0
  469. package/dist/resolution/frameworks/svelte.d.ts.map +1 -0
  470. package/dist/resolution/frameworks/svelte.js +249 -0
  471. package/dist/resolution/frameworks/svelte.js.map +1 -0
  472. package/dist/resolution/frameworks/swift-objc.d.ts +37 -0
  473. package/dist/resolution/frameworks/swift-objc.d.ts.map +1 -0
  474. package/dist/resolution/frameworks/swift-objc.js +252 -0
  475. package/dist/resolution/frameworks/swift-objc.js.map +1 -0
  476. package/dist/resolution/frameworks/swift.d.ts +10 -0
  477. package/dist/resolution/frameworks/swift.d.ts.map +1 -0
  478. package/dist/resolution/frameworks/swift.js +400 -0
  479. package/dist/resolution/frameworks/swift.js.map +1 -0
  480. package/dist/resolution/frameworks/vue.d.ts +9 -0
  481. package/dist/resolution/frameworks/vue.d.ts.map +1 -0
  482. package/dist/resolution/frameworks/vue.js +306 -0
  483. package/dist/resolution/frameworks/vue.js.map +1 -0
  484. package/dist/resolution/go-module.d.ts +26 -0
  485. package/dist/resolution/go-module.d.ts.map +1 -0
  486. package/dist/resolution/go-module.js +78 -0
  487. package/dist/resolution/go-module.js.map +1 -0
  488. package/dist/resolution/import-resolver.d.ts +68 -0
  489. package/dist/resolution/import-resolver.d.ts.map +1 -0
  490. package/dist/resolution/import-resolver.js +1275 -0
  491. package/dist/resolution/import-resolver.js.map +1 -0
  492. package/dist/resolution/index.d.ts +117 -0
  493. package/dist/resolution/index.d.ts.map +1 -0
  494. package/dist/resolution/index.js +895 -0
  495. package/dist/resolution/index.js.map +1 -0
  496. package/dist/resolution/lru-cache.d.ts +24 -0
  497. package/dist/resolution/lru-cache.d.ts.map +1 -0
  498. package/dist/resolution/lru-cache.js +62 -0
  499. package/dist/resolution/lru-cache.js.map +1 -0
  500. package/dist/resolution/name-matcher.d.ts +32 -0
  501. package/dist/resolution/name-matcher.d.ts.map +1 -0
  502. package/dist/resolution/name-matcher.js +596 -0
  503. package/dist/resolution/name-matcher.js.map +1 -0
  504. package/dist/resolution/path-aliases.d.ts +68 -0
  505. package/dist/resolution/path-aliases.d.ts.map +1 -0
  506. package/dist/resolution/path-aliases.js +238 -0
  507. package/dist/resolution/path-aliases.js.map +1 -0
  508. package/dist/resolution/spec-link-resolver.d.ts +148 -0
  509. package/dist/resolution/spec-link-resolver.d.ts.map +1 -0
  510. package/dist/resolution/spec-link-resolver.js +337 -0
  511. package/dist/resolution/spec-link-resolver.js.map +1 -0
  512. package/dist/resolution/strip-comments.d.ts +27 -0
  513. package/dist/resolution/strip-comments.d.ts.map +1 -0
  514. package/dist/resolution/strip-comments.js +441 -0
  515. package/dist/resolution/strip-comments.js.map +1 -0
  516. package/dist/resolution/swift-objc-bridge.d.ts +134 -0
  517. package/dist/resolution/swift-objc-bridge.d.ts.map +1 -0
  518. package/dist/resolution/swift-objc-bridge.js +256 -0
  519. package/dist/resolution/swift-objc-bridge.js.map +1 -0
  520. package/dist/resolution/types.d.ts +216 -0
  521. package/dist/resolution/types.d.ts.map +1 -0
  522. package/dist/resolution/types.js +8 -0
  523. package/dist/resolution/types.js.map +1 -0
  524. package/dist/resolution/workspace-packages.d.ts +48 -0
  525. package/dist/resolution/workspace-packages.d.ts.map +1 -0
  526. package/dist/resolution/workspace-packages.js +208 -0
  527. package/dist/resolution/workspace-packages.js.map +1 -0
  528. package/dist/search/query-parser.d.ts +57 -0
  529. package/dist/search/query-parser.d.ts.map +1 -0
  530. package/dist/search/query-parser.js +177 -0
  531. package/dist/search/query-parser.js.map +1 -0
  532. package/dist/search/query-utils.d.ts +71 -0
  533. package/dist/search/query-utils.d.ts.map +1 -0
  534. package/dist/search/query-utils.js +380 -0
  535. package/dist/search/query-utils.js.map +1 -0
  536. package/dist/server/cli.js +152 -0
  537. package/dist/server/index.js +12 -0
  538. package/dist/server/ingest/impact-backfill.js +69 -0
  539. package/dist/server/ingest/impact-query.js +343 -0
  540. package/dist/server/ingest/index.js +19 -0
  541. package/dist/server/ingest/ingestor.js +541 -0
  542. package/dist/server/ingest/parser.js +104 -0
  543. package/dist/server/ingest/pricing.js +78 -0
  544. package/dist/server/ingest/specship-classify.js +153 -0
  545. package/dist/server/ingest/types.js +9 -0
  546. package/dist/server/ingest/watcher.js +77 -0
  547. package/dist/server/package.json +3 -0
  548. package/dist/server/project-registry.js +101 -0
  549. package/dist/server/routes/claude.js +907 -0
  550. package/dist/server/routes/domain.js +0 -0
  551. package/dist/server/routes/events.js +134 -0
  552. package/dist/server/routes/graph.js +248 -0
  553. package/dist/server/routes/maintainability.js +18 -0
  554. package/dist/server/routes/memory.js +272 -0
  555. package/dist/server/routes/projects.js +197 -0
  556. package/dist/server/routes/reflect.js +93 -0
  557. package/dist/server/routes/spec.js +373 -0
  558. package/dist/server/routes/status.js +112 -0
  559. package/dist/server/routes/workflow.js +253 -0
  560. package/dist/server/server.js +238 -0
  561. package/dist/server/static-handler.js +87 -0
  562. package/dist/statusline/active-run.d.ts +16 -0
  563. package/dist/statusline/active-run.d.ts.map +1 -0
  564. package/dist/statusline/active-run.js +73 -0
  565. package/dist/statusline/active-run.js.map +1 -0
  566. package/dist/statusline/cache.d.ts +21 -0
  567. package/dist/statusline/cache.d.ts.map +1 -0
  568. package/dist/statusline/cache.js +34 -0
  569. package/dist/statusline/cache.js.map +1 -0
  570. package/dist/statusline/index.d.ts +24 -0
  571. package/dist/statusline/index.d.ts.map +1 -0
  572. package/dist/statusline/index.js +128 -0
  573. package/dist/statusline/index.js.map +1 -0
  574. package/dist/statusline/paths.d.ts +27 -0
  575. package/dist/statusline/paths.d.ts.map +1 -0
  576. package/dist/statusline/paths.js +101 -0
  577. package/dist/statusline/paths.js.map +1 -0
  578. package/dist/statusline/render.d.ts +25 -0
  579. package/dist/statusline/render.d.ts.map +1 -0
  580. package/dist/statusline/render.js +72 -0
  581. package/dist/statusline/render.js.map +1 -0
  582. package/dist/statusline/session-marker.d.ts +30 -0
  583. package/dist/statusline/session-marker.d.ts.map +1 -0
  584. package/dist/statusline/session-marker.js +71 -0
  585. package/dist/statusline/session-marker.js.map +1 -0
  586. package/dist/statusline/types.d.ts +67 -0
  587. package/dist/statusline/types.d.ts.map +1 -0
  588. package/dist/statusline/types.js +16 -0
  589. package/dist/statusline/types.js.map +1 -0
  590. package/dist/sync/git-hooks.d.ts +45 -0
  591. package/dist/sync/git-hooks.d.ts.map +1 -0
  592. package/dist/sync/git-hooks.js +225 -0
  593. package/dist/sync/git-hooks.js.map +1 -0
  594. package/dist/sync/index.d.ts +19 -0
  595. package/dist/sync/index.d.ts.map +1 -0
  596. package/dist/sync/index.js +35 -0
  597. package/dist/sync/index.js.map +1 -0
  598. package/dist/sync/watch-policy.d.ts +48 -0
  599. package/dist/sync/watch-policy.d.ts.map +1 -0
  600. package/dist/sync/watch-policy.js +124 -0
  601. package/dist/sync/watch-policy.js.map +1 -0
  602. package/dist/sync/watcher.d.ts +283 -0
  603. package/dist/sync/watcher.d.ts.map +1 -0
  604. package/dist/sync/watcher.js +606 -0
  605. package/dist/sync/watcher.js.map +1 -0
  606. package/dist/sync/worktree.d.ts +54 -0
  607. package/dist/sync/worktree.d.ts.map +1 -0
  608. package/dist/sync/worktree.js +137 -0
  609. package/dist/sync/worktree.js.map +1 -0
  610. package/dist/types.d.ts +625 -0
  611. package/dist/types.d.ts.map +1 -0
  612. package/dist/types.js +118 -0
  613. package/dist/types.js.map +1 -0
  614. package/dist/ui/glyphs.d.ts +42 -0
  615. package/dist/ui/glyphs.d.ts.map +1 -0
  616. package/dist/ui/glyphs.js +78 -0
  617. package/dist/ui/glyphs.js.map +1 -0
  618. package/dist/ui/shimmer-progress.d.ts +11 -0
  619. package/dist/ui/shimmer-progress.d.ts.map +1 -0
  620. package/dist/ui/shimmer-progress.js +90 -0
  621. package/dist/ui/shimmer-progress.js.map +1 -0
  622. package/dist/ui/shimmer-worker.d.ts +2 -0
  623. package/dist/ui/shimmer-worker.d.ts.map +1 -0
  624. package/dist/ui/shimmer-worker.js +118 -0
  625. package/dist/ui/shimmer-worker.js.map +1 -0
  626. package/dist/ui/types.d.ts +17 -0
  627. package/dist/ui/types.d.ts.map +1 -0
  628. package/dist/ui/types.js +3 -0
  629. package/dist/ui/types.js.map +1 -0
  630. package/dist/utils.d.ts +205 -0
  631. package/dist/utils.d.ts.map +1 -0
  632. package/dist/utils.js +549 -0
  633. package/dist/utils.js.map +1 -0
  634. package/dist/web/chunk-2AJCHB7P.js +1 -0
  635. package/dist/web/chunk-2CPLUFCH.js +2 -0
  636. package/dist/web/chunk-2I7L37NS.js +1 -0
  637. package/dist/web/chunk-2NAWAJB5.js +1 -0
  638. package/dist/web/chunk-2OJBIPE4.js +1 -0
  639. package/dist/web/chunk-372AYXK6.js +17 -0
  640. package/dist/web/chunk-3E2WB6D5.js +1 -0
  641. package/dist/web/chunk-3EBFYSCH.js +2 -0
  642. package/dist/web/chunk-3QCQ4BXS.js +1 -0
  643. package/dist/web/chunk-42XVAQ6I.js +1 -0
  644. package/dist/web/chunk-4IMMPEYM.js +1 -0
  645. package/dist/web/chunk-4JYHAP7B.js +1 -0
  646. package/dist/web/chunk-4TJQJPCZ.js +1 -0
  647. package/dist/web/chunk-4WZIHTPC.js +1 -0
  648. package/dist/web/chunk-4YVSYOSD.js +1 -0
  649. package/dist/web/chunk-52PO6IMB.js +2 -0
  650. package/dist/web/chunk-54D6RFSW.js +1 -0
  651. package/dist/web/chunk-5BQIOYKW.js +1 -0
  652. package/dist/web/chunk-5HGWHUJA.js +1 -0
  653. package/dist/web/chunk-5XRUOPZE.js +1 -0
  654. package/dist/web/chunk-5Y244R4G.js +1 -0
  655. package/dist/web/chunk-6O7Z3P2M.js +1 -0
  656. package/dist/web/chunk-6QXULGLG.js +1 -0
  657. package/dist/web/chunk-6RRDPT5Z.js +1 -0
  658. package/dist/web/chunk-6VKB2ZWM.js +1 -0
  659. package/dist/web/chunk-7DMFVTU4.js +1 -0
  660. package/dist/web/chunk-7P5CVBJZ.js +1 -0
  661. package/dist/web/chunk-7SMPKVEP.js +1 -0
  662. package/dist/web/chunk-AHLX543M.js +1 -0
  663. package/dist/web/chunk-AMGJBO7D.js +3 -0
  664. package/dist/web/chunk-AZJVTPLU.js +1 -0
  665. package/dist/web/chunk-B3CWIVBW.js +1 -0
  666. package/dist/web/chunk-BLBRMCN2.js +1 -0
  667. package/dist/web/chunk-BMIAXD2V.js +2 -0
  668. package/dist/web/chunk-BPCJLNBS.js +47 -0
  669. package/dist/web/chunk-BRHEUDLY.js +6 -0
  670. package/dist/web/chunk-BUXWEHIY.js +1 -0
  671. package/dist/web/chunk-CD5IZM7Y.js +1 -0
  672. package/dist/web/chunk-DLQPZWSI.css +1 -0
  673. package/dist/web/chunk-DSGNOCKQ.js +1 -0
  674. package/dist/web/chunk-DT5LJYFX.js +1 -0
  675. package/dist/web/chunk-DYRFLPJA.js +1 -0
  676. package/dist/web/chunk-E3J3CXR5.js +1 -0
  677. package/dist/web/chunk-E73OX2P7.js +1 -0
  678. package/dist/web/chunk-EAXRKDLV.js +1 -0
  679. package/dist/web/chunk-EBKKDHYI.js +1 -0
  680. package/dist/web/chunk-EE7V7Q5P.js +1 -0
  681. package/dist/web/chunk-EKY2FUHU.js +1 -0
  682. package/dist/web/chunk-EP6XOPXH.js +1 -0
  683. package/dist/web/chunk-ESGDLJOJ.js +1 -0
  684. package/dist/web/chunk-ETJG7NCY.js +1 -0
  685. package/dist/web/chunk-EUUEFEDI.js +1 -0
  686. package/dist/web/chunk-EX4ZHR4F.js +1 -0
  687. package/dist/web/chunk-F5UNCSXP.js +1 -0
  688. package/dist/web/chunk-FFGJXUHI.js +1 -0
  689. package/dist/web/chunk-FGNZDHTL.js +11 -0
  690. package/dist/web/chunk-FIJW2UNJ.js +1 -0
  691. package/dist/web/chunk-FMV5PXRC.js +5 -0
  692. package/dist/web/chunk-G7VZT5KB.js +3 -0
  693. package/dist/web/chunk-GCOM4JPR.js +2 -0
  694. package/dist/web/chunk-GEIIDO6C.js +1 -0
  695. package/dist/web/chunk-GRZYXPSO.js +7 -0
  696. package/dist/web/chunk-GWBABPZ5.js +1 -0
  697. package/dist/web/chunk-GYGPS3AN.js +1 -0
  698. package/dist/web/chunk-H4GLRD3Q.js +1 -0
  699. package/dist/web/chunk-H5TWEFYX.js +1 -0
  700. package/dist/web/chunk-H7AF7YS4.js +1 -0
  701. package/dist/web/chunk-HCB2N2KH.js +1 -0
  702. package/dist/web/chunk-HDZDQILN.js +1 -0
  703. package/dist/web/chunk-HMK6UO6N.js +1 -0
  704. package/dist/web/chunk-HVVXPI4D.js +1 -0
  705. package/dist/web/chunk-IHEE5NYJ.js +1 -0
  706. package/dist/web/chunk-IPB746BT.js +1 -0
  707. package/dist/web/chunk-ISNEBICW.js +1 -0
  708. package/dist/web/chunk-J2GZVLHH.js +1 -0
  709. package/dist/web/chunk-JTFXTIPE.js +903 -0
  710. package/dist/web/chunk-KHU5M2AL.js +1 -0
  711. package/dist/web/chunk-KW3DHCFV.js +1 -0
  712. package/dist/web/chunk-LB6JPLX2.js +1 -0
  713. package/dist/web/chunk-LBXLFPVN.js +1 -0
  714. package/dist/web/chunk-LGNSHRCE.js +1 -0
  715. package/dist/web/chunk-LNSVDHCI.js +1 -0
  716. package/dist/web/chunk-LVGIY3SO.js +1 -0
  717. package/dist/web/chunk-LXLHIHEN.js +1 -0
  718. package/dist/web/chunk-MFHO2F2U.js +4 -0
  719. package/dist/web/chunk-N5OSSQFZ.js +1 -0
  720. package/dist/web/chunk-N6SS4G6S.js +1 -0
  721. package/dist/web/chunk-NAJYJNHS.js +1 -0
  722. package/dist/web/chunk-NHD66NOI.js +1 -0
  723. package/dist/web/chunk-NNLJ55MY.js +1 -0
  724. package/dist/web/chunk-NTBJG6SJ.js +1 -0
  725. package/dist/web/chunk-NUDB3Q2Y.js +3 -0
  726. package/dist/web/chunk-OM7JVWQQ.js +1 -0
  727. package/dist/web/chunk-OXEF5E3E.js +1 -0
  728. package/dist/web/chunk-PGGJPDJG.js +1 -0
  729. package/dist/web/chunk-PUYSJNJR.js +1 -0
  730. package/dist/web/chunk-Q2RVFS45.js +1 -0
  731. package/dist/web/chunk-Q7L6LLAK.js +1 -0
  732. package/dist/web/chunk-QCMKJIWY.js +1 -0
  733. package/dist/web/chunk-QEQRY4QQ.js +1 -0
  734. package/dist/web/chunk-QH6CF3M3.js +1 -0
  735. package/dist/web/chunk-QQ5LD7PI.js +1 -0
  736. package/dist/web/chunk-QR6L3KAC.js +1 -0
  737. package/dist/web/chunk-QXJS6F3U.js +1 -0
  738. package/dist/web/chunk-R2DLK4HO.js +1 -0
  739. package/dist/web/chunk-RD6TVPOT.js +1 -0
  740. package/dist/web/chunk-RKY4EJYJ.js +1 -0
  741. package/dist/web/chunk-RONYWVY7.js +1 -0
  742. package/dist/web/chunk-RSZZWGGC.js +1 -0
  743. package/dist/web/chunk-RXKXYF2C.js +1 -0
  744. package/dist/web/chunk-SCNDZRN2.js +1 -0
  745. package/dist/web/chunk-SH6UVHQC.js +1 -0
  746. package/dist/web/chunk-SWKJRNYY.js +1 -0
  747. package/dist/web/chunk-T7AZ65JP.js +1 -0
  748. package/dist/web/chunk-TCZDVOHD.js +1 -0
  749. package/dist/web/chunk-TF5TF6IP.js +1 -0
  750. package/dist/web/chunk-TPTITA3V.js +1 -0
  751. package/dist/web/chunk-TR335633.js +1 -0
  752. package/dist/web/chunk-UR5KDXPX.js +1 -0
  753. package/dist/web/chunk-UR6O2GEH.js +1 -0
  754. package/dist/web/chunk-UTNMGWTP.js +1 -0
  755. package/dist/web/chunk-VECWMHJP.js +1 -0
  756. package/dist/web/chunk-VUACT35R.js +3 -0
  757. package/dist/web/chunk-VZI7H4SZ.js +1 -0
  758. package/dist/web/chunk-W22AVG3N.js +1 -0
  759. package/dist/web/chunk-W6NGHRHX.js +1 -0
  760. package/dist/web/chunk-WB6YHOD4.js +1 -0
  761. package/dist/web/chunk-WBT64AWV.js +1 -0
  762. package/dist/web/chunk-WFXJIXZE.js +4 -0
  763. package/dist/web/chunk-WTGYRH3Z.js +298 -0
  764. package/dist/web/chunk-WXTCVDTP.js +1 -0
  765. package/dist/web/chunk-XCDHWLVH.js +1 -0
  766. package/dist/web/chunk-Y3H6FFUZ.js +1 -0
  767. package/dist/web/chunk-Y4F5ULGJ.js +1 -0
  768. package/dist/web/chunk-YEGKAAEE.js +1 -0
  769. package/dist/web/chunk-YM2KU57F.js +1 -0
  770. package/dist/web/chunk-YRERBP6T.js +1 -0
  771. package/dist/web/chunk-ZLV4VCDG.js +3 -0
  772. package/dist/web/chunk-ZQUJMA5K.js +4 -0
  773. package/dist/web/chunk-ZTVI5KFF.js +1 -0
  774. package/dist/web/favicon-16.png +0 -0
  775. package/dist/web/favicon-180.png +0 -0
  776. package/dist/web/favicon-32.png +0 -0
  777. package/dist/web/favicon-512.png +0 -0
  778. package/dist/web/favicon-small.svg +15 -0
  779. package/dist/web/favicon.ico +0 -0
  780. package/dist/web/favicon.svg +20 -0
  781. package/dist/web/icon-192.png +0 -0
  782. package/dist/web/icon-512.png +0 -0
  783. package/dist/web/index.html +146 -0
  784. package/dist/web/main-ESADRXN2.css +1 -0
  785. package/dist/web/main-SQFUMVQA.js +1 -0
  786. package/dist/web/manifest.webmanifest +15 -0
  787. package/dist/web/media/codicon-LN6W7LCM.ttf +0 -0
  788. package/dist/web/styles-KSOPUVDA.css +1 -0
  789. package/dist/web/sw.js +69 -0
  790. package/dist/workflows/condition-evaluator.d.ts +75 -0
  791. package/dist/workflows/condition-evaluator.d.ts.map +1 -0
  792. package/dist/workflows/condition-evaluator.js +282 -0
  793. package/dist/workflows/condition-evaluator.js.map +1 -0
  794. package/dist/workflows/defaults/claude-design-implement.yaml +336 -0
  795. package/dist/workflows/defaults/index.d.ts +26 -0
  796. package/dist/workflows/defaults/index.d.ts.map +1 -0
  797. package/dist/workflows/defaults/index.js +94 -0
  798. package/dist/workflows/defaults/index.js.map +1 -0
  799. package/dist/workflows/defaults/spec-author.yaml +214 -0
  800. package/dist/workflows/defaults/spec-fix.yaml +110 -0
  801. package/dist/workflows/defaults/spec-implement.yaml +150 -0
  802. package/dist/workflows/defaults/spec-relink.yaml +81 -0
  803. package/dist/workflows/defaults/spec-verify.yaml +51 -0
  804. package/dist/workflows/discovery.d.ts +46 -0
  805. package/dist/workflows/discovery.d.ts.map +1 -0
  806. package/dist/workflows/discovery.js +193 -0
  807. package/dist/workflows/discovery.js.map +1 -0
  808. package/dist/workflows/executor.d.ts +98 -0
  809. package/dist/workflows/executor.d.ts.map +1 -0
  810. package/dist/workflows/executor.js +664 -0
  811. package/dist/workflows/executor.js.map +1 -0
  812. package/dist/workflows/runners/approval.d.ts +18 -0
  813. package/dist/workflows/runners/approval.d.ts.map +1 -0
  814. package/dist/workflows/runners/approval.js +34 -0
  815. package/dist/workflows/runners/approval.js.map +1 -0
  816. package/dist/workflows/runners/bash.d.ts +13 -0
  817. package/dist/workflows/runners/bash.d.ts.map +1 -0
  818. package/dist/workflows/runners/bash.js +143 -0
  819. package/dist/workflows/runners/bash.js.map +1 -0
  820. package/dist/workflows/runners/cancel.d.ts +10 -0
  821. package/dist/workflows/runners/cancel.d.ts.map +1 -0
  822. package/dist/workflows/runners/cancel.js +19 -0
  823. package/dist/workflows/runners/cancel.js.map +1 -0
  824. package/dist/workflows/runners/prompt.d.ts +51 -0
  825. package/dist/workflows/runners/prompt.d.ts.map +1 -0
  826. package/dist/workflows/runners/prompt.js +306 -0
  827. package/dist/workflows/runners/prompt.js.map +1 -0
  828. package/dist/workflows/runners/script.d.ts +17 -0
  829. package/dist/workflows/runners/script.d.ts.map +1 -0
  830. package/dist/workflows/runners/script.js +155 -0
  831. package/dist/workflows/runners/script.js.map +1 -0
  832. package/dist/workflows/runners/types.d.ts +57 -0
  833. package/dist/workflows/runners/types.d.ts.map +1 -0
  834. package/dist/workflows/runners/types.js +13 -0
  835. package/dist/workflows/runners/types.js.map +1 -0
  836. package/dist/workflows/schemas/workflow.d.ts +166 -0
  837. package/dist/workflows/schemas/workflow.d.ts.map +1 -0
  838. package/dist/workflows/schemas/workflow.js +437 -0
  839. package/dist/workflows/schemas/workflow.js.map +1 -0
  840. package/hooks/hooks.json +38 -0
  841. package/package.json +78 -0
  842. package/scripts/add-lang/bench.sh +60 -0
  843. package/scripts/add-lang/check-grammar.mjs +75 -0
  844. package/scripts/add-lang/dump-ast.mjs +103 -0
  845. package/scripts/add-lang/verify-extraction.mjs +70 -0
  846. package/scripts/agent-eval/arms-F.sh +21 -0
  847. package/scripts/agent-eval/arms-matrix.sh +37 -0
  848. package/scripts/agent-eval/audit.sh +68 -0
  849. package/scripts/agent-eval/bench-readme.sh +28 -0
  850. package/scripts/agent-eval/bench-why-repo.sh +22 -0
  851. package/scripts/agent-eval/block-read-hook.sh +19 -0
  852. package/scripts/agent-eval/hook-settings.json +15 -0
  853. package/scripts/agent-eval/itrun.sh +120 -0
  854. package/scripts/agent-eval/parse-arms.mjs +116 -0
  855. package/scripts/agent-eval/parse-bench-readme.mjs +84 -0
  856. package/scripts/agent-eval/parse-run.mjs +45 -0
  857. package/scripts/agent-eval/parse-session.mjs +93 -0
  858. package/scripts/agent-eval/probe-context.mjs +21 -0
  859. package/scripts/agent-eval/probe-explore.mjs +40 -0
  860. package/scripts/agent-eval/probe-node.mjs +20 -0
  861. package/scripts/agent-eval/probe-sweep.mjs +119 -0
  862. package/scripts/agent-eval/probe-trace.mjs +20 -0
  863. package/scripts/agent-eval/run-agent.sh +34 -0
  864. package/scripts/agent-eval/run-all.sh +67 -0
  865. package/scripts/agent-eval/run-arms.sh +56 -0
  866. package/scripts/agent-eval/seq-matrix.mjs +137 -0
  867. package/scripts/build-bundle.sh +118 -0
  868. package/scripts/build-server-bundle.mjs +80 -0
  869. package/scripts/build-web-bundle.mjs +66 -0
  870. package/scripts/extract-release-notes.mjs +130 -0
  871. package/scripts/local-install.sh +41 -0
  872. package/scripts/npm-sdk.js +75 -0
  873. package/scripts/npm-shim.js +246 -0
  874. package/scripts/offline-install.ps1 +148 -0
  875. package/scripts/offline-install.sh +149 -0
  876. package/scripts/pack-npm.sh +119 -0
  877. package/scripts/prepare-release.mjs +270 -0
  878. package/scripts/sync-shim-version.mjs +64 -0
  879. package/selectors.json +41 -0
@@ -0,0 +1,3179 @@
1
+ "use strict";
2
+ /**
3
+ * MCP Tool Definitions
4
+ *
5
+ * Defines the tools exposed by the SpecShip MCP server.
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.ToolHandler = exports.tools = void 0;
9
+ exports.getExploreBudget = getExploreBudget;
10
+ exports.getExploreOutputBudget = getExploreOutputBudget;
11
+ exports.formatStaleBanner = formatStaleBanner;
12
+ exports.formatStaleFooter = formatStaleFooter;
13
+ exports.getStaticTools = getStaticTools;
14
+ const directory_1 = require("../directory");
15
+ const statusline_1 = require("../statusline");
16
+ // Lazy-load the heavy SpecShip 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
+ // SpecShip is pulled in only when a tool actually opens a project. require() is
20
+ // sync + cached (CommonJS build).
21
+ const loadSpecShip = () => 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 path_1 = require("path");
28
+ /** Maximum output length to prevent context bloat (characters) */
29
+ const MAX_OUTPUT_LENGTH = 15000;
30
+ /**
31
+ * Maximum length for free-form string inputs (query, task, symbol).
32
+ * Bounds memory and CPU when a buggy or hostile MCP client sends a
33
+ * huge payload — without this an attacker could ship a 100MB string
34
+ * and force a full FTS5 scan / OOM the server. 10 000 characters is
35
+ * far beyond any realistic legitimate query.
36
+ */
37
+ const MAX_INPUT_LENGTH = 10_000;
38
+ /**
39
+ * Maximum length for path-like string inputs (projectPath, path
40
+ * filter, glob pattern). Paths beyond a few thousand chars are
41
+ * never legitimate and signal abuse or a bug upstream.
42
+ */
43
+ const MAX_PATH_LENGTH = 4_096;
44
+ /**
45
+ * Rust path roots that have no file-system equivalent — `crate` is the
46
+ * current crate, `super` is the parent module, `self` is the current
47
+ * module. Used by `matchesSymbol` to strip these before file-path
48
+ * matching so `crate::configurator::stage_apply::run` resolves the
49
+ * same as `configurator::stage_apply::run`.
50
+ */
51
+ const RUST_PATH_PREFIXES = new Set(['crate', 'super', 'self']);
52
+ /**
53
+ * Node kinds that contain other symbols. For these, `specship_node` with
54
+ * `includeCode=true` returns a structural outline (member names + signatures
55
+ * + line numbers) instead of the full body, which for a large class is a
56
+ * multi-thousand-character wall of source that bloats the agent's context.
57
+ */
58
+ const CONTAINER_NODE_KINDS = new Set([
59
+ 'class', 'struct', 'interface', 'trait', 'protocol', 'enum', 'namespace', 'module',
60
+ ]);
61
+ /** Last `::` / `.` / `/`-separated segment of a qualified symbol. */
62
+ function lastQualifierPart(symbol) {
63
+ const parts = symbol.split(/::|[./]/).filter((p) => p.length > 0);
64
+ return parts[parts.length - 1] ?? symbol;
65
+ }
66
+ /**
67
+ * Calculate the recommended number of specship_explore calls based on project size.
68
+ * Larger codebases need more exploration calls to cover their surface area,
69
+ * but smaller ones should use fewer to avoid unnecessary overhead.
70
+ */
71
+ function getExploreBudget(fileCount) {
72
+ if (fileCount < 500)
73
+ return 1;
74
+ if (fileCount < 5000)
75
+ return 2;
76
+ if (fileCount < 15000)
77
+ return 3;
78
+ if (fileCount < 25000)
79
+ return 4;
80
+ return 5;
81
+ }
82
+ function getExploreOutputBudget(fileCount) {
83
+ // Tiered budget, scaled to project size. The budget is a CEILING (relevance
84
+ // still gates WHAT is included), and it MUST stay under the agent's INLINE
85
+ // tool-result cap (~25K chars). Above that, the host externalizes the result
86
+ // to a file the agent then Reads back — re-introducing a read AND the
87
+ // cache-write cost — which is exactly what a 35K vscode explore did in the
88
+ // n=4 README A/B. So even large repos cap at ~24K: the answer is the handful
89
+ // of ~100-line flow windows the agent would have grep-located and read (it
90
+ // natively reads ~6–9 files, median 100-line ranges), NOT a sprawl of 12
91
+ // files. Concentration onto the flow emerges from this cap + the named-file-
92
+ // first sort dropping peripheral files. Invariant: a larger tier must never
93
+ // get a smaller `maxCharsPerFile` than a smaller tier.
94
+ if (fileCount < 150) {
95
+ return {
96
+ // ITER3: revert iter2's aggressive body shrink (forced Read fallback —
97
+ // the per-file 2.5K cap pushed the agent to Read instead of node).
98
+ // Back to the iter1 shape (13K/4/3.8K) but keep the test-file
99
+ // hard-exclude. The cost lever for this tier lives in steering the
100
+ // agent to stop after 1-2 calls, not in this budget.
101
+ maxOutputChars: 13000,
102
+ defaultMaxFiles: 4,
103
+ maxCharsPerFile: 3800,
104
+ gapThreshold: 7,
105
+ maxSymbolsInFileHeader: 5,
106
+ maxEdgesPerRelationshipKind: 4,
107
+ includeRelationships: false,
108
+ includeAdditionalFiles: false,
109
+ includeCompletenessSignal: false,
110
+ includeBudgetNote: false,
111
+ excludeLowValueFiles: true,
112
+ };
113
+ }
114
+ if (fileCount < 500) {
115
+ return {
116
+ // ITER3: same revert/keep-filter pattern as <150.
117
+ maxOutputChars: 18000,
118
+ defaultMaxFiles: 5,
119
+ maxCharsPerFile: 3800,
120
+ gapThreshold: 8,
121
+ maxSymbolsInFileHeader: 6,
122
+ maxEdgesPerRelationshipKind: 6,
123
+ includeRelationships: false,
124
+ includeAdditionalFiles: false,
125
+ includeCompletenessSignal: false,
126
+ includeBudgetNote: false,
127
+ excludeLowValueFiles: true,
128
+ };
129
+ }
130
+ if (fileCount < 5000) {
131
+ return {
132
+ // ~150-line per-file window (the native read unit) × ~6 files, capped at
133
+ // the ~24K inline ceiling so the response is never externalized. Per-file
134
+ // stays ≥ the <500 tier (3800) — monotonic.
135
+ maxOutputChars: 24000,
136
+ defaultMaxFiles: 8,
137
+ maxCharsPerFile: 6500,
138
+ gapThreshold: 12,
139
+ maxSymbolsInFileHeader: 10,
140
+ maxEdgesPerRelationshipKind: 10,
141
+ includeRelationships: true,
142
+ includeAdditionalFiles: true,
143
+ includeCompletenessSignal: true,
144
+ includeBudgetNote: true,
145
+ excludeLowValueFiles: false,
146
+ };
147
+ }
148
+ // Large + very-large repos: SAME ~24K inline ceiling (a bigger response just
149
+ // externalizes — see vscode). More files indexed → more CALLS via
150
+ // getExploreBudget, not a bigger single response. Per-file 7000 (≥ smaller
151
+ // tiers) gives the central file a ~180-line orientation window.
152
+ if (fileCount < 15000) {
153
+ return {
154
+ maxOutputChars: 24000,
155
+ defaultMaxFiles: 8,
156
+ maxCharsPerFile: 7000,
157
+ gapThreshold: 15,
158
+ maxSymbolsInFileHeader: 15,
159
+ maxEdgesPerRelationshipKind: 15,
160
+ includeRelationships: true,
161
+ includeAdditionalFiles: true,
162
+ includeCompletenessSignal: true,
163
+ includeBudgetNote: true,
164
+ excludeLowValueFiles: false,
165
+ };
166
+ }
167
+ return {
168
+ maxOutputChars: 24000,
169
+ defaultMaxFiles: 8,
170
+ maxCharsPerFile: 7000,
171
+ gapThreshold: 15,
172
+ maxSymbolsInFileHeader: 15,
173
+ maxEdgesPerRelationshipKind: 15,
174
+ includeRelationships: true,
175
+ includeAdditionalFiles: true,
176
+ includeCompletenessSignal: true,
177
+ includeBudgetNote: true,
178
+ excludeLowValueFiles: false,
179
+ };
180
+ }
181
+ /**
182
+ * Whether `specship_explore` should prefix source lines with their line
183
+ * numbers (cat -n style: `<num>\t<code>`).
184
+ *
185
+ * Line numbers let the agent cite `file:line` straight from the explore
186
+ * payload instead of re-Reading the file just to find a line number — the
187
+ * dominant residual cost on precise-tracing questions (#185 follow-up).
188
+ *
189
+ * Defaults ON. Set `SPECSHIP_EXPLORE_LINENUMS=0` to disable (used by the
190
+ * A/B harness to measure the payload-cost vs. read-savings tradeoff).
191
+ */
192
+ function exploreLineNumbersEnabled() {
193
+ return process.env.SPECSHIP_EXPLORE_LINENUMS !== '0';
194
+ }
195
+ /**
196
+ * Adaptive explore sizing (default ON). `specship_explore` skeletonizes OFF-SPINE
197
+ * polymorphic-sibling files — a file whose class is one of ≥3 interchangeable
198
+ * implementations of a shared interface (e.g. OkHttp's `: Interceptor` classes) —
199
+ * to class + member signatures (bodies elided), keeping the on-spine exemplar full.
200
+ * This sizes the response to the answer instead of the budget cap on sibling-heavy
201
+ * flows (OkHttp interceptor-chain explore 28.5k→16.6k, ~28% cheaper than native
202
+ * search, reads flat). It is PROVABLY INERT elsewhere: distinct pipeline steps (no
203
+ * ≥3-implementer supertype, e.g. Excalidraw's `renderStaticScene`) and on-spine
204
+ * files keep full source — output is byte-identical to shipped on excalidraw /
205
+ * tokio / django / vscode / gin. Set `SPECSHIP_ADAPTIVE_EXPLORE=0` to disable.
206
+ */
207
+ function adaptiveExploreEnabled() {
208
+ return process.env.SPECSHIP_ADAPTIVE_EXPLORE !== '0' && process.env.SPECSHIP_ADAPTIVE_EXPLORE !== 'false';
209
+ }
210
+ /**
211
+ * Prefix each line of a source slice with its 1-based line number, matching
212
+ * the Read tool's `cat -n` convention (number + tab) so the agent treats it
213
+ * the same way it treats Read output.
214
+ *
215
+ * @param slice contiguous source text (already extracted from the file)
216
+ * @param firstLineNumber the 1-based line number of the slice's first line
217
+ */
218
+ function numberSourceLines(slice, firstLineNumber) {
219
+ const out = [];
220
+ const split = slice.split('\n');
221
+ for (let i = 0; i < split.length; i++) {
222
+ out.push(`${firstLineNumber + i}\t${split[i]}`);
223
+ }
224
+ return out.join('\n');
225
+ }
226
+ /**
227
+ * Per-file staleness banner emitted at the top of a tool response when the
228
+ * file watcher has pending events for files referenced by the response.
229
+ * The agent uses this to fall back to Read for those specific files
230
+ * without waiting for the debounced sync (issue #403).
231
+ */
232
+ function formatStaleBanner(stale) {
233
+ const now = Date.now();
234
+ const lines = stale.map((p) => {
235
+ const ageMs = Math.max(0, now - p.lastSeenMs);
236
+ const label = p.indexing ? 'indexing in progress' : 'pending sync';
237
+ return ` - ${p.path} (edited ${ageMs}ms ago, ${label})`;
238
+ });
239
+ return ('⚠️ Some files referenced below were edited since the last index sync — ' +
240
+ 'their specship entries may be stale:\n' +
241
+ lines.join('\n') +
242
+ '\nFor accurate content of those specific files, Read them directly. ' +
243
+ 'The rest of this response is fresh.');
244
+ }
245
+ /**
246
+ * Compact footer listing pending files that are NOT referenced in this
247
+ * response. Gives the agent a complete project-wide freshness picture
248
+ * without bloating the main banner.
249
+ */
250
+ function formatStaleFooter(stale) {
251
+ const MAX = 5;
252
+ const now = Date.now();
253
+ const shown = stale.slice(0, MAX);
254
+ const lines = shown.map((p) => {
255
+ const ageMs = Math.max(0, now - p.lastSeenMs);
256
+ return ` - ${p.path} (edited ${ageMs}ms ago)`;
257
+ });
258
+ const more = stale.length > MAX ? `\n - …and ${stale.length - MAX} more` : '';
259
+ return (`(Note: ${stale.length} file(s) elsewhere in this project are pending index ` +
260
+ `sync but were not referenced above:\n${lines.join('\n')}${more})`);
261
+ }
262
+ /**
263
+ * Common projectPath property for cross-project queries
264
+ */
265
+ const projectPathProperty = {
266
+ type: 'string',
267
+ description: 'Path to a different project with .specship/ initialized. If omitted, uses current project. Use this to query other codebases.',
268
+ };
269
+ /**
270
+ * All SpecShip MCP tools
271
+ *
272
+ * Designed for minimal context usage - use specship_explore as the primary tool
273
+ * (one call usually answers the whole question), and only use other tools for
274
+ * targeted follow-up queries.
275
+ *
276
+ * All tools support cross-project queries via the optional `projectPath` parameter.
277
+ */
278
+ const spec_tools_1 = require("./spec-tools");
279
+ const maintainability_tool_1 = require("./maintainability-tool");
280
+ const fitness_tool_1 = require("./fitness-tool");
281
+ const designer_tools_1 = require("./designer-tools");
282
+ exports.tools = [
283
+ {
284
+ name: 'specship_search',
285
+ description: 'Quick symbol search by name. Returns locations only (no code). Use specship_explore instead to get the actual source / understand an area in one call.',
286
+ inputSchema: {
287
+ type: 'object',
288
+ properties: {
289
+ query: {
290
+ type: 'string',
291
+ description: 'Symbol name or partial name (e.g., "auth", "signIn", "UserService")',
292
+ },
293
+ kind: {
294
+ type: 'string',
295
+ description: 'Filter by node kind',
296
+ enum: ['function', 'method', 'class', 'interface', 'type', 'variable', 'route', 'component'],
297
+ },
298
+ limit: {
299
+ type: 'number',
300
+ description: 'Maximum results (default: 10)',
301
+ default: 10,
302
+ },
303
+ projectPath: projectPathProperty,
304
+ },
305
+ required: ['query'],
306
+ },
307
+ },
308
+ {
309
+ name: 'specship_callers',
310
+ description: 'List functions that call <symbol>. For the full flow, use specship_explore.',
311
+ inputSchema: {
312
+ type: 'object',
313
+ properties: {
314
+ symbol: {
315
+ type: 'string',
316
+ description: 'Name of the function, method, or class to find callers for',
317
+ },
318
+ limit: {
319
+ type: 'number',
320
+ description: 'Maximum number of callers to return (default: 20)',
321
+ default: 20,
322
+ },
323
+ projectPath: projectPathProperty,
324
+ },
325
+ required: ['symbol'],
326
+ },
327
+ },
328
+ {
329
+ name: 'specship_callees',
330
+ description: 'List functions that <symbol> calls. For the full flow, use specship_explore.',
331
+ inputSchema: {
332
+ type: 'object',
333
+ properties: {
334
+ symbol: {
335
+ type: 'string',
336
+ description: 'Name of the function, method, or class to find callees for',
337
+ },
338
+ limit: {
339
+ type: 'number',
340
+ description: 'Maximum number of callees to return (default: 20)',
341
+ default: 20,
342
+ },
343
+ projectPath: projectPathProperty,
344
+ },
345
+ required: ['symbol'],
346
+ },
347
+ },
348
+ {
349
+ name: 'specship_impact',
350
+ description: 'List symbols affected by changing <symbol>. Use before a refactor.',
351
+ inputSchema: {
352
+ type: 'object',
353
+ properties: {
354
+ symbol: {
355
+ type: 'string',
356
+ description: 'Name of the symbol to analyze impact for',
357
+ },
358
+ depth: {
359
+ type: 'number',
360
+ description: 'How many levels of dependencies to traverse (default: 2)',
361
+ default: 2,
362
+ },
363
+ projectPath: projectPathProperty,
364
+ },
365
+ required: ['symbol'],
366
+ },
367
+ },
368
+ {
369
+ name: 'specship_node',
370
+ description: 'SECONDARY (after specship_explore): get ONE symbol in full — its location, signature, callers/callees trail, and verbatim body (includeCode=true). When the name is AMBIGUOUS (an overloaded method, or the same method name on different types), it returns EVERY matching definition\'s full body in a single call — so you never need to Read a file to find the specific overload you want. For a heavily-overloaded name, pass `file` (and/or `line`) to pin the exact definition — e.g. the `file:line` a trail or another tool already showed you. Reach for this when explore trimmed a body you need. Use specship_explore for several related symbols or the full flow.',
371
+ inputSchema: {
372
+ type: 'object',
373
+ properties: {
374
+ symbol: {
375
+ type: 'string',
376
+ description: 'Name of the symbol to get details for',
377
+ },
378
+ includeCode: {
379
+ type: 'boolean',
380
+ description: 'Include full source code (default: false to minimize context)',
381
+ default: false,
382
+ },
383
+ file: {
384
+ type: 'string',
385
+ description: 'Optional: disambiguate an overloaded name to the definition in this file (path or basename, e.g. "harness.rs").',
386
+ },
387
+ line: {
388
+ type: 'number',
389
+ description: 'Optional: disambiguate to the definition at/around this line (use with the file:line a trail showed you).',
390
+ },
391
+ projectPath: projectPathProperty,
392
+ },
393
+ required: ['symbol'],
394
+ },
395
+ },
396
+ {
397
+ name: 'specship_explore',
398
+ description: 'PRIMARY TOOL — call FIRST for almost any question: how does X work, architecture, a bug, where/what is X, or surveying an area. Returns the verbatim source of the relevant symbols grouped by file in ONE capped call (Read-equivalent — do NOT re-open shown files). Query can be a natural-language question OR a bag of symbol/file names. Usually the ONLY call you need — answers without further search/node/Read/Grep.',
399
+ inputSchema: {
400
+ type: 'object',
401
+ properties: {
402
+ query: {
403
+ type: 'string',
404
+ description: 'Symbol names, file names, or short code terms to explore (e.g., "AuthService loginUser session-manager", "GraphTraverser BFS impact traversal.ts"). Use specship_search first to find relevant names.',
405
+ },
406
+ maxFiles: {
407
+ type: 'number',
408
+ description: 'Maximum number of files to include source code from (default: 12)',
409
+ default: 12,
410
+ },
411
+ projectPath: projectPathProperty,
412
+ },
413
+ required: ['query'],
414
+ },
415
+ },
416
+ {
417
+ name: 'specship_status',
418
+ description: 'Index health check (files / nodes / edges). Skip unless debugging.',
419
+ inputSchema: {
420
+ type: 'object',
421
+ properties: {
422
+ projectPath: projectPathProperty,
423
+ },
424
+ },
425
+ },
426
+ {
427
+ name: 'specship_files',
428
+ description: 'Indexed file tree with language + symbol counts. Faster than Glob for project layout.',
429
+ inputSchema: {
430
+ type: 'object',
431
+ properties: {
432
+ path: {
433
+ type: 'string',
434
+ description: 'Filter to files under this directory path (e.g., "src/components"). Returns all files if not specified.',
435
+ },
436
+ pattern: {
437
+ type: 'string',
438
+ description: 'Filter files matching this glob pattern (e.g., "*.tsx", "**/*.test.ts")',
439
+ },
440
+ format: {
441
+ type: 'string',
442
+ description: 'Output format: "tree" (hierarchical, default), "flat" (simple list), "grouped" (by language)',
443
+ enum: ['tree', 'flat', 'grouped'],
444
+ default: 'tree',
445
+ },
446
+ includeMetadata: {
447
+ type: 'boolean',
448
+ description: 'Include file metadata like language and symbol count (default: true)',
449
+ default: true,
450
+ },
451
+ maxDepth: {
452
+ type: 'number',
453
+ description: 'Maximum directory depth to show (default: unlimited)',
454
+ },
455
+ projectPath: projectPathProperty,
456
+ },
457
+ },
458
+ },
459
+ // Spec-layer tools (v5): see ./spec-tools.ts for handlers.
460
+ ...spec_tools_1.specToolDefinitions,
461
+ ...maintainability_tool_1.maintainabilityToolDefinitions,
462
+ ...fitness_tool_1.fitnessToolDefinitions,
463
+ // Designer tools: claude.ai/design driving, vendored from @pro-vi/designer.
464
+ // See ./designer-tools.ts for handlers. Drives a debug Chrome over CDP.
465
+ ...designer_tools_1.designerToolDefinitions,
466
+ ];
467
+ /**
468
+ * Allowlist-filtered tool definitions WITHOUT an engine — the static surface the
469
+ * proxy answers `tools/list` with before any project is open. Mirrors
470
+ * `ToolHandler.getTools()` in the no-SpecShip case (the dynamic per-repo budget
471
+ * note in a description only adds once `cg` is loaded; the schemas are static).
472
+ */
473
+ function getStaticTools() {
474
+ const raw = process.env.SPECSHIP_MCP_TOOLS;
475
+ if (!raw || !raw.trim())
476
+ return exports.tools;
477
+ const allow = new Set(raw.split(',').map(s => s.trim().replace(/^specship_/, '')).filter(Boolean));
478
+ return allow.size ? exports.tools.filter(t => allow.has(t.name.replace(/^specship_/, ''))) : exports.tools;
479
+ }
480
+ /**
481
+ * Tool handler that executes tools against a SpecShip instance
482
+ *
483
+ * Supports cross-project queries via the projectPath parameter.
484
+ * Other projects are opened on-demand and cached for performance.
485
+ */
486
+ class ToolHandler {
487
+ cg;
488
+ // Cache of opened SpecShip instances for cross-project queries
489
+ projectCache = new Map();
490
+ // The directory the server last searched for a default project. Surfaced in
491
+ // the "not initialized" error so users can see why detection missed.
492
+ defaultProjectHint = null;
493
+ // Per-start-path cache of the git worktree/index mismatch (issue #155). The
494
+ // mismatch is a fixed property of (where the request came from → which
495
+ // .specship/ it resolves to), so the up-to-two `git rev-parse` spawns run
496
+ // once and every later tool call reuses the result — never shelling out to
497
+ // git on the hot path. `undefined` = not computed yet; `null` = no mismatch.
498
+ worktreeMismatchCache = new Map();
499
+ // Gate that the MCP engine pokes after `cg.open()` so the first tool call
500
+ // blocks on the post-open filesystem reconcile (catch-up sync). Without
501
+ // this, a tool call that races past `catchUpSync()` serves rows for files
502
+ // that were deleted (or edited) while no MCP server was running — and the
503
+ // per-file staleness banner can't help, because `getPendingFiles()` is
504
+ // populated by the watcher, not by catch-up. Cleared on first await so
505
+ // subsequent calls don't pay any cost.
506
+ catchUpGate = null;
507
+ constructor(cg) {
508
+ this.cg = cg;
509
+ }
510
+ /**
511
+ * Update the default SpecShip instance (e.g. after lazy initialization)
512
+ */
513
+ setDefaultSpecShip(cg) {
514
+ this.cg = cg;
515
+ // Reset the per-session call marker once, when this server process first
516
+ // attaches its code graph (REQ-STATUSLINE-004). Idempotent per process.
517
+ (0, statusline_1.initSession)(cg.getProjectRoot());
518
+ }
519
+ /**
520
+ * Engine-only: register the catch-up sync promise so the next `execute()`
521
+ * call awaits it before serving. The handler swallows rejections (the
522
+ * engine logs them) so a sync failure never propagates as a tool error;
523
+ * we still want to serve a best-effort result over the same potentially-
524
+ * stale data, which is what would have happened without the gate.
525
+ */
526
+ setCatchUpGate(p) {
527
+ this.catchUpGate = p;
528
+ }
529
+ /**
530
+ * Record the directory the server tried to resolve the default project from.
531
+ * Used only to make the "no default project" error actionable.
532
+ */
533
+ setDefaultProjectHint(searchedPath) {
534
+ this.defaultProjectHint = searchedPath;
535
+ }
536
+ /**
537
+ * Whether a default SpecShip instance is available
538
+ */
539
+ hasDefaultSpecShip() {
540
+ return this.cg !== null;
541
+ }
542
+ /**
543
+ * Optional allowlist of exposed tools, parsed from the SPECSHIP_MCP_TOOLS
544
+ * env var (comma-separated short names, e.g. "trace,search,node,context").
545
+ * Unset/empty → every tool is exposed. Lets an operator (or an A/B harness)
546
+ * trim the tool surface without rebuilding the client config; the ablated
547
+ * tool is then truly absent from ListTools rather than merely denied on call.
548
+ * Matching is on the short form, so "node" and "specship_node" both work.
549
+ */
550
+ toolAllowlist() {
551
+ const raw = process.env.SPECSHIP_MCP_TOOLS;
552
+ if (!raw || !raw.trim())
553
+ return null;
554
+ const short = (s) => s.trim().replace(/^specship_/, '');
555
+ const set = new Set(raw.split(',').map(short).filter(Boolean));
556
+ return set.size ? set : null;
557
+ }
558
+ /** Whether a tool name passes the SPECSHIP_MCP_TOOLS allowlist (if any). */
559
+ isToolAllowed(name) {
560
+ const allow = this.toolAllowlist();
561
+ return !allow || allow.has(name.replace(/^specship_/, ''));
562
+ }
563
+ /**
564
+ * Get tool definitions with dynamic descriptions based on project size.
565
+ * The specship_explore tool description includes a budget recommendation
566
+ * scaled to the number of indexed files. Honors the SPECSHIP_MCP_TOOLS
567
+ * allowlist so a trimmed surface is reflected in ListTools.
568
+ */
569
+ getTools() {
570
+ const allow = this.toolAllowlist();
571
+ let visible = allow
572
+ ? exports.tools.filter(t => allow.has(t.name.replace(/^specship_/, '')))
573
+ : exports.tools;
574
+ if (!this.cg)
575
+ return visible;
576
+ try {
577
+ const stats = this.cg.getStats();
578
+ const budget = getExploreBudget(stats.fileCount);
579
+ // Tiny-repo tool gating: on projects under TINY_REPO_FILE_THRESHOLD
580
+ // files, only expose the 5 core tools (search, context, node,
581
+ // explore, trace). The 5 omitted tools (callers, callees, impact,
582
+ // status, files) reduce to one grep at this scale.
583
+ //
584
+ // n=2 audits ruled out cutting below 5 tools:
585
+ // - 3-tool gate (search + context + trace): cost regressed on
586
+ // cobra/ky/sinatra. The agent fell back to raw Reads to cover
587
+ // what specship_node + specship_explore would have answered.
588
+ // - 1-tool gate (search only): catastrophic regression — express
589
+ // went from -43% WIN to +107% LOSS. With only search, the agent
590
+ // can't navigate the call graph structurally and reads everything.
591
+ //
592
+ // 5 is the empirical lower bound. Tools beyond search/context/
593
+ // node/explore/trace pay overhead that the agent doesn't recoup
594
+ // on tiny-repo flow questions.
595
+ // ITER4: raise threshold 150 → 500 so single-file frameworks
596
+ // (sinatra at 159, slim_framework around 200) also get the
597
+ // 5-tool surface. The empirical 5-tool floor was set on <150
598
+ // probes; iter3 measurement showed sinatra is structurally the
599
+ // SAME problem as cobra (single-file WITHOUT-arm Read wins),
600
+ // so it deserves the same gating.
601
+ const TINY_REPO_FILE_THRESHOLD = 500;
602
+ const TINY_REPO_CORE_TOOLS = new Set([
603
+ 'specship_explore',
604
+ 'specship_search',
605
+ 'specship_node',
606
+ ]);
607
+ if (stats.fileCount < TINY_REPO_FILE_THRESHOLD) {
608
+ // Designer tools are not code-graph tools — the tiny-repo flow-question
609
+ // economics that justify trimming don't apply, so they always survive.
610
+ visible = visible.filter(t => TINY_REPO_CORE_TOOLS.has(t.name) || t.name.startsWith('designer_'));
611
+ }
612
+ return visible.map(tool => {
613
+ if (tool.name === 'specship_explore') {
614
+ return {
615
+ ...tool,
616
+ description: `${tool.description} Budget: make at most ${budget} calls for this project (${stats.fileCount.toLocaleString()} files indexed).`,
617
+ };
618
+ }
619
+ return tool;
620
+ });
621
+ }
622
+ catch {
623
+ return visible;
624
+ }
625
+ }
626
+ /**
627
+ * Get SpecShip instance for a project
628
+ *
629
+ * If projectPath is provided, opens that project's SpecShip (cached).
630
+ * Otherwise returns the default SpecShip instance.
631
+ *
632
+ * Walks up parent directories to find the nearest .specship/ folder,
633
+ * similar to how git finds .git/ directories.
634
+ */
635
+ getSpecShip(projectPath) {
636
+ if (!projectPath) {
637
+ if (!this.cg) {
638
+ const searched = this.defaultProjectHint ?? process.cwd();
639
+ throw new Error('No SpecShip project is loaded for this session.\n' +
640
+ `Searched for a .specship/ directory starting from: ${searched}\n` +
641
+ 'The index is likely fine — this is a working-directory detection issue: ' +
642
+ "the MCP client launched the server outside your project and didn't report the " +
643
+ 'workspace root. Fix it either way:\n' +
644
+ ' • Pass projectPath to the tool call, e.g. projectPath: "/absolute/path/to/your/project"\n' +
645
+ ' • Or add --path to the server\'s MCP config args: ["serve", "--mcp", "--path", "/absolute/path/to/your/project"]');
646
+ }
647
+ return this.cg;
648
+ }
649
+ // Check cache first (using original path as key)
650
+ if (this.projectCache.has(projectPath)) {
651
+ return this.projectCache.get(projectPath);
652
+ }
653
+ // Reject sensitive system directories before opening. Only validate a
654
+ // path that actually exists — a nested or not-yet-created sub-path of a
655
+ // real project must still be allowed to resolve UP to its .specship/
656
+ // root below (issue #238), so we don't run the existence-checking
657
+ // validator on paths that are meant to walk up.
658
+ if ((0, fs_1.existsSync)(projectPath)) {
659
+ const pathError = (0, utils_1.validateProjectPath)(projectPath);
660
+ if (pathError) {
661
+ throw new Error(pathError);
662
+ }
663
+ }
664
+ // Walk up parent directories to find nearest .specship/
665
+ const resolvedRoot = (0, directory_1.findNearestSpecShipRoot)(projectPath);
666
+ if (!resolvedRoot) {
667
+ throw new Error(`SpecShip not initialized in ${projectPath}. Run 'specship init' in that project first.`);
668
+ }
669
+ // If the path resolves to the default project, reuse the already-open
670
+ // default instance rather than opening a SECOND connection to the same DB.
671
+ // A duplicate connection serializes reads against the watcher's auto-sync
672
+ // writes; on the wasm backend (no WAL) that surfaces as intermittent
673
+ // "database is locked" on concurrent tool calls. See issue #238. Deliberately
674
+ // not cached under projectPath — the server owns and closes the default
675
+ // instance, so routing it through projectCache.closeAll() would double-close it.
676
+ if (this.cg && this.cg.getProjectRoot() === resolvedRoot) {
677
+ return this.cg;
678
+ }
679
+ // Check if we already have this resolved root cached (different path, same project)
680
+ if (this.projectCache.has(resolvedRoot)) {
681
+ const cg = this.projectCache.get(resolvedRoot);
682
+ // Cache under original path too for faster future lookups
683
+ this.projectCache.set(projectPath, cg);
684
+ return cg;
685
+ }
686
+ // Open and cache under both paths
687
+ const cg = loadSpecShip().openSync(resolvedRoot);
688
+ this.projectCache.set(resolvedRoot, cg);
689
+ if (projectPath !== resolvedRoot) {
690
+ this.projectCache.set(projectPath, cg);
691
+ }
692
+ return cg;
693
+ }
694
+ /**
695
+ * Close all cached project connections
696
+ */
697
+ closeAll() {
698
+ for (const cg of this.projectCache.values()) {
699
+ cg.close();
700
+ }
701
+ this.projectCache.clear();
702
+ this.worktreeMismatchCache.clear();
703
+ }
704
+ /**
705
+ * Validate that a value is a non-empty string within length bounds.
706
+ *
707
+ * The `maxLength` cap protects against MCP clients that ship huge
708
+ * payloads (10MB+ query strings either by accident or maliciously).
709
+ * Without this, a single oversized input can pin the FTS5 index or
710
+ * exhaust memory before any real work runs.
711
+ */
712
+ validateString(value, name, maxLength = MAX_INPUT_LENGTH) {
713
+ if (typeof value !== 'string' || value.length === 0) {
714
+ return this.errorResult(`${name} must be a non-empty string`);
715
+ }
716
+ if (value.length > maxLength) {
717
+ return this.errorResult(`${name} exceeds maximum length of ${maxLength} characters (got ${value.length})`);
718
+ }
719
+ return value;
720
+ }
721
+ /**
722
+ * Validate an optional path-like string input. Returns the value if
723
+ * valid (or undefined), or a ToolResult with the error.
724
+ */
725
+ validateOptionalPath(value, name) {
726
+ if (value === undefined || value === null)
727
+ return undefined;
728
+ if (typeof value !== 'string') {
729
+ return this.errorResult(`${name} must be a string`);
730
+ }
731
+ if (value.length > MAX_PATH_LENGTH) {
732
+ return this.errorResult(`${name} exceeds maximum length of ${MAX_PATH_LENGTH} characters (got ${value.length})`);
733
+ }
734
+ return value;
735
+ }
736
+ /**
737
+ * Cached git worktree/index mismatch for a tool call's effective project.
738
+ *
739
+ * The "effective project" is what the request targets: an explicit
740
+ * `projectPath` arg, else the directory the server resolved its default
741
+ * project from (`defaultProjectHint`), else cwd. Memoized per start path —
742
+ * see `worktreeMismatchCache`. Best-effort: if the project can't be resolved
743
+ * (e.g. nothing initialized yet), it reports "no mismatch" so a tool is never
744
+ * broken by this check.
745
+ */
746
+ worktreeMismatchFor(projectPath) {
747
+ const startPath = projectPath ?? this.defaultProjectHint ?? process.cwd();
748
+ const cached = this.worktreeMismatchCache.get(startPath);
749
+ if (cached !== undefined)
750
+ return cached;
751
+ let mismatch = null;
752
+ try {
753
+ mismatch = (0, worktree_1.detectWorktreeIndexMismatch)(startPath, this.getSpecShip(projectPath).getProjectRoot());
754
+ }
755
+ catch {
756
+ // No resolvable project (or any other resolution error) → nothing to warn.
757
+ mismatch = null;
758
+ }
759
+ this.worktreeMismatchCache.set(startPath, mismatch);
760
+ return mismatch;
761
+ }
762
+ /**
763
+ * Prefix a successful read-tool result with a compact worktree-mismatch
764
+ * notice when the resolved index belongs to a different git working tree than
765
+ * the caller's (issue #155). Without this, an agent in a nested worktree
766
+ * silently trusts main-branch results. No-op on error results and when there
767
+ * is no mismatch. `specship_status` is excluded — it embeds its own verbose
768
+ * warning — so it stays out of this path.
769
+ */
770
+ withWorktreeNotice(result, projectPath) {
771
+ if (result.isError)
772
+ return result;
773
+ const mismatch = this.worktreeMismatchFor(projectPath);
774
+ if (!mismatch)
775
+ return result;
776
+ const notice = (0, worktree_1.worktreeMismatchNotice)(mismatch);
777
+ const [first, ...rest] = result.content;
778
+ if (first && first.type === 'text') {
779
+ return { ...result, content: [{ type: 'text', text: `${notice}\n\n${first.text}` }, ...rest] };
780
+ }
781
+ return result;
782
+ }
783
+ /**
784
+ * Annotate a successful read-tool result with per-file staleness — the
785
+ * non-blocking answer to issue #403. The file watcher tracks every event
786
+ * it sees per path; here we intersect "files referenced in this response"
787
+ * against that pending set and prepend a compact banner so the agent can
788
+ * fall back to Read for those *specific* files without waiting for the
789
+ * debounced sync to fire. Other pending files in the project (not
790
+ * referenced by this response) get a small footer so the agent has a
791
+ * complete picture without bloating the banner.
792
+ *
793
+ * Cost when nothing is pending — the common case — is one boolean check.
794
+ * No I/O, no parsing of markdown beyond a per-pending-file substring scan.
795
+ */
796
+ withStalenessNotice(result, projectPath) {
797
+ if (result.isError)
798
+ return result;
799
+ let cg;
800
+ try {
801
+ cg = this.getSpecShip(projectPath);
802
+ }
803
+ catch {
804
+ return result; // no default project — leave as is
805
+ }
806
+ // Cross-project `projectPath` calls open a cached SpecShip WITHOUT a
807
+ // watcher (watchers are only attached to the default session project).
808
+ // When the cross-project path happens to be the same project as the
809
+ // default cg, the cached instance is the wrong one — its pendingFiles is
810
+ // permanently empty. Detect the equal-path case and prefer the default
811
+ // cg so the staleness signal still fires when an agent passes the
812
+ // explicit projectPath form of its own project.
813
+ if (this.cg && cg !== this.cg) {
814
+ try {
815
+ const sameProject = (0, path_1.resolve)(this.cg.getProjectRoot()) === (0, path_1.resolve)(cg.getProjectRoot());
816
+ if (sameProject)
817
+ cg = this.cg;
818
+ }
819
+ catch {
820
+ /* getProjectRoot may throw on a closed instance — leave cg as is */
821
+ }
822
+ }
823
+ // Defensive: some test fakes inject a partial SpecShip stub without the
824
+ // newer pending-files API. Treat missing/throwing as "no pending files."
825
+ let pending = [];
826
+ try {
827
+ pending = cg.getPendingFiles?.() ?? [];
828
+ }
829
+ catch {
830
+ return result;
831
+ }
832
+ if (pending.length === 0)
833
+ return result;
834
+ const [first, ...rest] = result.content;
835
+ if (!first || first.type !== 'text')
836
+ return result;
837
+ const text = first.text;
838
+ const inResponse = [];
839
+ const elsewhere = [];
840
+ for (const p of pending) {
841
+ // Substring match against the project-relative POSIX path — that's
842
+ // exactly the format both the watcher and every specship response
843
+ // emit, so a plain includes() is sufficient and avoids regex pitfalls.
844
+ if (text.includes(p.path))
845
+ inResponse.push(p);
846
+ else
847
+ elsewhere.push(p);
848
+ }
849
+ let banner = '';
850
+ if (inResponse.length > 0) {
851
+ banner = formatStaleBanner(inResponse);
852
+ }
853
+ let footer = '';
854
+ if (elsewhere.length > 0) {
855
+ footer = formatStaleFooter(elsewhere);
856
+ }
857
+ if (!banner && !footer)
858
+ return result;
859
+ const composed = [banner, text, footer].filter(Boolean).join('\n\n');
860
+ return { ...result, content: [{ type: 'text', text: composed }, ...rest] };
861
+ }
862
+ /**
863
+ * Execute a tool by name
864
+ */
865
+ async execute(toolName, args) {
866
+ try {
867
+ // Block the first tool call on the engine's post-open reconcile so we
868
+ // never serve rows for files deleted/edited while no MCP server was
869
+ // running. The gate is cleared after first await — subsequent calls
870
+ // pay nothing. Catch-up failures are logged by the engine; we
871
+ // proceed regardless so a transient sync error never breaks tools.
872
+ // Record this lookup into the per-session status-line marker
873
+ // (REQ-STATUSLINE-004). Best-effort and only for specship_* tools. The
874
+ // ENTIRE block is guarded — including resolving the project root — so a
875
+ // marker step can never turn a real tool call into an error (A3); a mock
876
+ // or partially-initialized code graph must pass straight through.
877
+ if (toolName.startsWith('specship_') && this.cg) {
878
+ try {
879
+ const root = this.cg.getProjectRoot();
880
+ (0, statusline_1.initSession)(root); // idempotent per process — fixes the session start
881
+ (0, statusline_1.recordCall)(root, toolName);
882
+ }
883
+ catch { /* never let status-line bookkeeping affect the tool call */ }
884
+ }
885
+ if (this.catchUpGate) {
886
+ const gate = this.catchUpGate;
887
+ this.catchUpGate = null;
888
+ try {
889
+ await gate;
890
+ }
891
+ catch { /* engine already logged */ }
892
+ }
893
+ // Honor the optional tool allowlist (SPECSHIP_MCP_TOOLS): a trimmed
894
+ // surface rejects ablated tools defensively even if a client cached them.
895
+ if (!this.isToolAllowed(toolName)) {
896
+ return this.errorResult(`Tool ${toolName} is disabled via SPECSHIP_MCP_TOOLS`);
897
+ }
898
+ // Cross-cutting input validation. All tools accept an optional
899
+ // `projectPath` and most accept either `query`, `task`, or
900
+ // `symbol` — bound their lengths centrally so individual handlers
901
+ // can stay focused on tool-specific logic.
902
+ const pathCheck = this.validateOptionalPath(args.projectPath, 'projectPath');
903
+ if (typeof pathCheck === 'object' && pathCheck !== undefined) {
904
+ return pathCheck;
905
+ }
906
+ // The `path` and `pattern` properties used by specship_files are
907
+ // also path-shaped — apply the same cap.
908
+ if (args.path !== undefined) {
909
+ const check = this.validateOptionalPath(args.path, 'path');
910
+ if (typeof check === 'object' && check !== undefined)
911
+ return check;
912
+ }
913
+ if (args.pattern !== undefined) {
914
+ const check = this.validateOptionalPath(args.pattern, 'pattern');
915
+ if (typeof check === 'object' && check !== undefined)
916
+ return check;
917
+ }
918
+ // Read tools resolve through a single result variable so cross-cutting
919
+ // notices — worktree-index mismatch (issue #155) and per-file
920
+ // staleness (issue #403) — can be applied in one place. status embeds
921
+ // its own verbose worktree warning but still flows through the
922
+ // staleness wrapper so its pending-files section stays consistent
923
+ // with what the read tools surface.
924
+ let result;
925
+ switch (toolName) {
926
+ case 'specship_search':
927
+ result = await this.handleSearch(args);
928
+ break;
929
+ case 'specship_callers':
930
+ result = await this.handleCallers(args);
931
+ break;
932
+ case 'specship_callees':
933
+ result = await this.handleCallees(args);
934
+ break;
935
+ case 'specship_impact':
936
+ result = await this.handleImpact(args);
937
+ break;
938
+ case 'specship_explore':
939
+ result = await this.handleExplore(args);
940
+ break;
941
+ case 'specship_node':
942
+ result = await this.handleNode(args);
943
+ break;
944
+ case 'specship_status':
945
+ // status embeds the pending-files list as a first-class section
946
+ // (see handleStatus), so we skip the auto-banner wrapper here to
947
+ // avoid duplicating the same info at the top of the response.
948
+ return await this.handleStatus(args);
949
+ case 'specship_files':
950
+ result = await this.handleFiles(args);
951
+ break;
952
+ case 'specship_spec':
953
+ result = await (0, spec_tools_1.handleSpecshipSpec)(this.getSpecShip(args.projectPath), args);
954
+ break;
955
+ case 'specship_link_assert':
956
+ result = await (0, spec_tools_1.handleSpecshipLinkAssert)(this.getSpecShip(args.projectPath), args);
957
+ break;
958
+ case 'specship_link_verify':
959
+ result = await (0, spec_tools_1.handleSpecshipLinkVerify)(this.getSpecShip(args.projectPath), args);
960
+ break;
961
+ case 'specship_drifted':
962
+ result = await (0, spec_tools_1.handleSpecshipDrifted)(this.getSpecShip(args.projectPath), args);
963
+ break;
964
+ case 'specship_maintainability':
965
+ result = await (0, maintainability_tool_1.handleSpecshipMaintainability)(this.getSpecShip(args.projectPath), args);
966
+ break;
967
+ case 'specship_fitness':
968
+ result = await (0, fitness_tool_1.handleSpecshipFitness)(this.getSpecShip(args.projectPath), args);
969
+ break;
970
+ // Designer tools are independent of the code graph — return directly,
971
+ // bypassing the worktree/staleness notice wrappers (which assume a
972
+ // code-graph query).
973
+ case 'designer_session':
974
+ return await (0, designer_tools_1.handleDesignerSession)(args);
975
+ case 'designer_prompt':
976
+ return await (0, designer_tools_1.handleDesignerPrompt)(args);
977
+ case 'designer_ask':
978
+ return await (0, designer_tools_1.handleDesignerAsk)(args);
979
+ case 'designer_list':
980
+ return await (0, designer_tools_1.handleDesignerList)(args);
981
+ case 'designer_snapshot':
982
+ return await (0, designer_tools_1.handleDesignerSnapshot)(args);
983
+ case 'designer_handoff':
984
+ return await (0, designer_tools_1.handleDesignerHandoff)(args);
985
+ default:
986
+ return this.errorResult(`Unknown tool: ${toolName}`);
987
+ }
988
+ const withWorktree = this.withWorktreeNotice(result, args.projectPath);
989
+ return this.withStalenessNotice(withWorktree, args.projectPath);
990
+ }
991
+ catch (err) {
992
+ return this.errorResult(`Tool execution failed: ${err instanceof Error ? err.message : String(err)}`);
993
+ }
994
+ }
995
+ /**
996
+ * Handle specship_search
997
+ */
998
+ async handleSearch(args) {
999
+ const query = this.validateString(args.query, 'query');
1000
+ if (typeof query !== 'string')
1001
+ return query;
1002
+ const cg = this.getSpecShip(args.projectPath);
1003
+ const kind = args.kind;
1004
+ const rawLimit = Number(args.limit) || 10;
1005
+ const limit = (0, utils_1.clamp)(rawLimit, 1, 100);
1006
+ const results = cg.searchNodes(query, {
1007
+ limit,
1008
+ kinds: kind ? [kind] : undefined,
1009
+ });
1010
+ if (results.length === 0) {
1011
+ return this.textResult(`No results found for "${query}"`);
1012
+ }
1013
+ // Down-rank generated files within the FTS-returned set so a search
1014
+ // for "Send" surfaces the hand-written keeper before .pb.go stubs
1015
+ // that share the name. Stable: only reorders generated vs. not.
1016
+ const ranked = [...results].sort((a, b) => {
1017
+ const aGen = (0, generated_detection_1.isGeneratedFile)(a.node.filePath) ? 1 : 0;
1018
+ const bGen = (0, generated_detection_1.isGeneratedFile)(b.node.filePath) ? 1 : 0;
1019
+ return aGen - bGen;
1020
+ });
1021
+ const formatted = this.formatSearchResults(ranked);
1022
+ return this.textResult(this.truncateOutput(formatted));
1023
+ }
1024
+ /**
1025
+ * Handle specship_callers
1026
+ */
1027
+ async handleCallers(args) {
1028
+ const symbol = this.validateString(args.symbol, 'symbol');
1029
+ if (typeof symbol !== 'string')
1030
+ return symbol;
1031
+ const cg = this.getSpecShip(args.projectPath);
1032
+ const limit = (0, utils_1.clamp)(args.limit || 20, 1, 100);
1033
+ const allMatches = this.findAllSymbols(cg, symbol);
1034
+ if (allMatches.nodes.length === 0) {
1035
+ return this.textResult(`Symbol "${symbol}" not found in the codebase`);
1036
+ }
1037
+ // Aggregate callers across all matching symbols
1038
+ const seen = new Set();
1039
+ const allCallers = [];
1040
+ for (const node of allMatches.nodes) {
1041
+ for (const c of cg.getCallers(node.id)) {
1042
+ if (!seen.has(c.node.id)) {
1043
+ seen.add(c.node.id);
1044
+ allCallers.push(c.node);
1045
+ }
1046
+ }
1047
+ }
1048
+ if (allCallers.length === 0) {
1049
+ return this.textResult(`No callers found for "${symbol}"${allMatches.note}`);
1050
+ }
1051
+ const formatted = this.formatNodeList(allCallers.slice(0, limit), `Callers of ${symbol}`) + allMatches.note;
1052
+ return this.textResult(this.truncateOutput(formatted));
1053
+ }
1054
+ /**
1055
+ * Handle specship_callees
1056
+ */
1057
+ async handleCallees(args) {
1058
+ const symbol = this.validateString(args.symbol, 'symbol');
1059
+ if (typeof symbol !== 'string')
1060
+ return symbol;
1061
+ const cg = this.getSpecShip(args.projectPath);
1062
+ const limit = (0, utils_1.clamp)(args.limit || 20, 1, 100);
1063
+ const allMatches = this.findAllSymbols(cg, symbol);
1064
+ if (allMatches.nodes.length === 0) {
1065
+ return this.textResult(`Symbol "${symbol}" not found in the codebase`);
1066
+ }
1067
+ // Aggregate callees across all matching symbols
1068
+ const seen = new Set();
1069
+ const allCallees = [];
1070
+ for (const node of allMatches.nodes) {
1071
+ for (const c of cg.getCallees(node.id)) {
1072
+ if (!seen.has(c.node.id)) {
1073
+ seen.add(c.node.id);
1074
+ allCallees.push(c.node);
1075
+ }
1076
+ }
1077
+ }
1078
+ if (allCallees.length === 0) {
1079
+ return this.textResult(`No callees found for "${symbol}"${allMatches.note}`);
1080
+ }
1081
+ const formatted = this.formatNodeList(allCallees.slice(0, limit), `Callees of ${symbol}`) + allMatches.note;
1082
+ return this.textResult(this.truncateOutput(formatted));
1083
+ }
1084
+ /**
1085
+ * Handle specship_impact
1086
+ */
1087
+ async handleImpact(args) {
1088
+ const symbol = this.validateString(args.symbol, 'symbol');
1089
+ if (typeof symbol !== 'string')
1090
+ return symbol;
1091
+ const cg = this.getSpecShip(args.projectPath);
1092
+ const depth = (0, utils_1.clamp)(args.depth || 2, 1, 10);
1093
+ const allMatches = this.findAllSymbols(cg, symbol);
1094
+ if (allMatches.nodes.length === 0) {
1095
+ return this.textResult(`Symbol "${symbol}" not found in the codebase`);
1096
+ }
1097
+ // Aggregate impact across all matching symbols
1098
+ const mergedNodes = new Map();
1099
+ const mergedEdges = [];
1100
+ const seenEdges = new Set();
1101
+ for (const node of allMatches.nodes) {
1102
+ const impact = cg.getImpactRadius(node.id, depth);
1103
+ for (const [id, n] of impact.nodes) {
1104
+ mergedNodes.set(id, n);
1105
+ }
1106
+ for (const e of impact.edges) {
1107
+ const key = `${e.source}->${e.target}:${e.kind}`;
1108
+ if (!seenEdges.has(key)) {
1109
+ seenEdges.add(key);
1110
+ mergedEdges.push(e);
1111
+ }
1112
+ }
1113
+ }
1114
+ const mergedImpact = {
1115
+ nodes: mergedNodes,
1116
+ edges: mergedEdges,
1117
+ roots: allMatches.nodes.map(n => n.id),
1118
+ };
1119
+ const formatted = this.formatImpact(symbol, mergedImpact) + allMatches.note;
1120
+ return this.textResult(this.truncateOutput(formatted));
1121
+ }
1122
+ /**
1123
+ * Describe a synthesized (dynamic-dispatch) edge for human output: how the
1124
+ * callback was wired up — the bridge static parsing can't see. Returns null
1125
+ * for ordinary static edges. Used by trace + the node trail so a synthesized
1126
+ * hop reads as "registered via onUpdate at App.tsx:3148", not a bare arrow.
1127
+ */
1128
+ synthEdgeNote(edge) {
1129
+ if (!edge || edge.provenance !== 'heuristic')
1130
+ return null;
1131
+ const m = edge.metadata;
1132
+ const registeredAt = typeof m?.registeredAt === 'string' ? m.registeredAt : undefined;
1133
+ const at = registeredAt ? ` @${registeredAt}` : '';
1134
+ if (m?.synthesizedBy === 'callback') {
1135
+ const via = m.via ? `\`${String(m.via)}\`` : 'a registrar';
1136
+ const field = m.field ? ` on .${String(m.field)}` : '';
1137
+ return {
1138
+ label: `callback — registered via ${via}${field} (dynamic dispatch)`,
1139
+ compact: `dynamic: callback via ${via}${at}`,
1140
+ registeredAt,
1141
+ };
1142
+ }
1143
+ if (m?.synthesizedBy === 'event-emitter') {
1144
+ const ev = m.event ? `\`${String(m.event)}\`` : 'an event';
1145
+ return {
1146
+ label: `event ${ev} — emit → handler (dynamic dispatch)`,
1147
+ compact: `dynamic: event ${ev}${at}`,
1148
+ registeredAt,
1149
+ };
1150
+ }
1151
+ if (m?.synthesizedBy === 'react-render') {
1152
+ return {
1153
+ label: `React re-render — \`setState\` re-runs render() (dynamic dispatch)`,
1154
+ compact: `dynamic: React re-render via setState${at}`,
1155
+ registeredAt,
1156
+ };
1157
+ }
1158
+ if (m?.synthesizedBy === 'jsx-render') {
1159
+ const child = m.via ? `<${String(m.via)}>` : 'a child component';
1160
+ return {
1161
+ label: `renders ${child} (JSX child — dynamic dispatch)`,
1162
+ compact: `dynamic: renders ${child}`,
1163
+ registeredAt,
1164
+ };
1165
+ }
1166
+ if (m?.synthesizedBy === 'vue-handler') {
1167
+ const ev = m.event ? `@${String(m.event)}` : 'a template event';
1168
+ return {
1169
+ label: `Vue template handler — bound to ${ev} (dynamic dispatch)`,
1170
+ compact: `dynamic: Vue ${ev} handler`,
1171
+ registeredAt,
1172
+ };
1173
+ }
1174
+ if (m?.synthesizedBy === 'interface-impl') {
1175
+ return {
1176
+ label: `interface/abstract dispatch — runs the implementation override (dynamic dispatch)`,
1177
+ compact: `dynamic: interface → impl${at}`,
1178
+ registeredAt,
1179
+ };
1180
+ }
1181
+ if (m?.synthesizedBy === 'closure-collection') {
1182
+ const field = m.field ? `\`${String(m.field)}\`` : 'a collection';
1183
+ return {
1184
+ label: `closure collection — runs handlers appended to ${field} (dynamic dispatch)`,
1185
+ compact: `dynamic: runs ${field} handlers${at}`,
1186
+ registeredAt,
1187
+ };
1188
+ }
1189
+ return null;
1190
+ }
1191
+ /**
1192
+ * Flow-from-named-symbols: an agent's specship_explore query is a bag of
1193
+ * symbol names that usually spans the flow it's investigating (e.g.
1194
+ * "PmsProductController getList PmsProductService list PmsProductServiceImpl").
1195
+ * Surface the longest call chain AMONG those named symbols — scoped to what the
1196
+ * agent explicitly named, so (unlike a fuzzy relevance set) there's no
1197
+ * wrong-feature wandering. Rides synthesized edges, so controller→service-
1198
+ * interface→impl shows up. Returns '' if no chain of >=3 nodes exists.
1199
+ *
1200
+ * Ambiguous tokens (Java `list` → dozens of nodes) are disambiguated by
1201
+ * CO-NAMING: the agent names the class too, so we keep only `list` candidates
1202
+ * whose qualifiedName contains another named token (`PmsProductServiceImpl::list`),
1203
+ * dropping unrelated `OmsOrderService::list`.
1204
+ */
1205
+ buildFlowFromNamedSymbols(cg, query) {
1206
+ const EMPTY = { text: '', pathNodeIds: new Set(), namedNodeIds: new Set(), uniqueNamedNodeIds: new Set() };
1207
+ try {
1208
+ const CALLABLE = new Set(['method', 'function', 'component', 'constructor']);
1209
+ // Strip only a REAL file extension (Create.cs → Create); KEEP qualified
1210
+ // names (Class.method / Class::method) — the agent's most precise input,
1211
+ // resolved exactly by findAllSymbols. (The old strip mangled Class.method
1212
+ // into Class, throwing the method away.)
1213
+ 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)$/i;
1214
+ const tokens = [...new Set(query.split(/[\s,()[\]]+/)
1215
+ .map((t) => t.replace(FILE_EXT, '').trim())
1216
+ .filter((t) => t.length >= 3 && /^[A-Za-z_$][\w$]*(?:(?:::|\.)[\w$]+)*$/.test(t)))].slice(0, 16);
1217
+ if (tokens.length < 2)
1218
+ return EMPTY;
1219
+ // Pool of name SEGMENTS (Class + method from every token) used to
1220
+ // disambiguate an ambiguous SIMPLE name: keep a candidate only if its
1221
+ // CONTAINER class is itself named in the query.
1222
+ const segPool = new Set();
1223
+ for (const t of tokens)
1224
+ for (const s of t.toLowerCase().split(/::|\./))
1225
+ if (s)
1226
+ segPool.add(s);
1227
+ const named = new Map();
1228
+ // Nodes whose token is SPECIFIC — a (near-)unique callable name (<=3 defs in
1229
+ // the whole graph). These are safe to SPARE a file on: the agent named THIS
1230
+ // method (`getResponseWithInterceptorChain`, 1 def). A hyper-polymorphic name
1231
+ // (`as_sql`, 110 defs across every Expression/Compiler subclass) is NOT here,
1232
+ // so naming it doesn't keep every backend variant full and flood the budget.
1233
+ const uniqueNamedNodeIds = new Set();
1234
+ for (const t of tokens) {
1235
+ const cands = this.findAllSymbols(cg, t).nodes.filter((n) => CALLABLE.has(n.kind));
1236
+ // A qualified or otherwise-specific name (<=3 hits) keeps all; an
1237
+ // ambiguous simple name keeps only candidates whose container is named.
1238
+ const specific = cands.length <= 3;
1239
+ const pick = specific
1240
+ ? cands
1241
+ : cands.filter((n) => {
1242
+ const segs = (n.qualifiedName || '').toLowerCase().split(/::|\./).filter(Boolean);
1243
+ const container = segs.length >= 2 ? segs[segs.length - 2] : '';
1244
+ return !!container && segPool.has(container);
1245
+ });
1246
+ for (const n of pick.slice(0, 6)) {
1247
+ named.set(n.id, n);
1248
+ if (specific)
1249
+ uniqueNamedNodeIds.add(n.id);
1250
+ }
1251
+ if (named.size > 40)
1252
+ break;
1253
+ }
1254
+ if (named.size < 2)
1255
+ return EMPTY;
1256
+ const MAX_HOPS = 7;
1257
+ let best = null;
1258
+ // BFS the full call graph (incl. synth edges) from each named seed, but
1259
+ // only ACCEPT a sink that is also named — both ends anchored to symbols the
1260
+ // agent named, so the chain stays on-topic while bridging intermediates
1261
+ // (e.g. the exact interface overload) that the token resolution missed.
1262
+ for (const seed of [...named.values()].slice(0, 8)) {
1263
+ const parent = new Map();
1264
+ parent.set(seed.id, { prev: null, edge: null, node: seed });
1265
+ const q = [{ id: seed.id, depth: 0, streak: 0 }];
1266
+ let deep = null, deepDepth = 0;
1267
+ const MAX_BRIDGE = 1; // ≤1 consecutive UNNAMED hop: bridge one missing intermediate, never wander a god-function's fan-out
1268
+ for (let h = 0; h < q.length && parent.size < 1500; h++) {
1269
+ const { id, depth, streak } = q[h];
1270
+ if (id !== seed.id && named.has(id) && depth > deepDepth) {
1271
+ deep = id;
1272
+ deepDepth = depth;
1273
+ }
1274
+ if (depth >= MAX_HOPS - 1)
1275
+ continue;
1276
+ for (const c of cg.getCallees(id)) {
1277
+ if (c.edge.kind !== 'calls' || parent.has(c.node.id))
1278
+ continue;
1279
+ const newStreak = named.has(c.node.id) ? 0 : streak + 1;
1280
+ if (newStreak > MAX_BRIDGE)
1281
+ continue;
1282
+ parent.set(c.node.id, { prev: id, edge: c.edge, node: c.node });
1283
+ q.push({ id: c.node.id, depth: depth + 1, streak: newStreak });
1284
+ }
1285
+ }
1286
+ if (!deep)
1287
+ continue;
1288
+ const chain = [];
1289
+ let cur = deep;
1290
+ while (cur) {
1291
+ const p = parent.get(cur);
1292
+ if (!p)
1293
+ break;
1294
+ chain.push({ node: p.node, edge: p.edge });
1295
+ cur = p.prev;
1296
+ }
1297
+ chain.reverse();
1298
+ if (!best || chain.length > best.length)
1299
+ best = chain;
1300
+ }
1301
+ const hasMain = !!best && best.length >= 3;
1302
+ const pathIds = new Set((best ?? []).map((s) => s.node.id));
1303
+ // Supplementary: dynamic-dispatch (synthesized) edges incident to a NAMED
1304
+ // symbol — the indirect hops an agent would otherwise grep/Read to
1305
+ // reconstruct ("where do the appended `validators` actually run?"). The
1306
+ // synth edge IS that answer, so surface it even when the OTHER end wasn't
1307
+ // named (e.g. the agent names `validate` but not the `didCompleteTask`
1308
+ // that drains the collection). On-topic by construction: only heuristic
1309
+ // edges touching a symbol the agent named; skipped when the hop already
1310
+ // shows in the main chain.
1311
+ const synthLines = [];
1312
+ const synthSeen = new Set();
1313
+ for (const n of named.values()) {
1314
+ if (synthLines.length >= 6)
1315
+ break;
1316
+ for (const { node: other, edge } of [...cg.getCallers(n.id), ...cg.getCallees(n.id)]) {
1317
+ if (synthLines.length >= 6)
1318
+ break;
1319
+ if (edge.provenance !== 'heuristic' || other.id === n.id)
1320
+ continue;
1321
+ if (pathIds.has(edge.source) && pathIds.has(edge.target))
1322
+ continue; // already in the main chain
1323
+ const src = edge.source === n.id ? n : other;
1324
+ const tgt = edge.source === n.id ? other : n;
1325
+ const key = `${src.name}>${tgt.name}`;
1326
+ if (synthSeen.has(key))
1327
+ continue;
1328
+ synthSeen.add(key);
1329
+ const note = this.synthEdgeNote(edge);
1330
+ synthLines.push(`- ${src.name} → ${tgt.name} [${note ? note.compact : edge.kind}]`);
1331
+ }
1332
+ }
1333
+ if (!hasMain && synthLines.length === 0)
1334
+ return EMPTY;
1335
+ const out = [];
1336
+ if (hasMain) {
1337
+ out.push('## Flow (call path among the symbols you queried)', '');
1338
+ for (let i = 0; i < best.length; i++) {
1339
+ const step = best[i];
1340
+ if (step.edge) {
1341
+ const sy = this.synthEdgeNote(step.edge);
1342
+ out.push(` ↓ ${sy ? sy.compact : step.edge.kind}`);
1343
+ }
1344
+ out.push(`${i + 1}. ${step.node.name} (${step.node.filePath}:${step.node.startLine})`);
1345
+ }
1346
+ out.push('');
1347
+ }
1348
+ if (synthLines.length) {
1349
+ out.push('## Dynamic-dispatch links among your symbols', '(synthesized — the indirect hops grep/Read would reconstruct; the `@file:line` is the wiring site)', '', ...synthLines, '');
1350
+ }
1351
+ out.push('> Full source for these symbols is below — the call flow among them, followed by their bodies.', '');
1352
+ // namedNodeIds = every callable the agent explicitly named (a superset of
1353
+ // the spine). A file holding one is something the agent asked to SEE, so it
1354
+ // must keep full source even if it's an off-spine polymorphic sibling — the
1355
+ // agent named `getResponseWithInterceptorChain` / `SQLCompiler.execute_sql`
1356
+ // as the mechanism, not as an interchangeable leaf. See the skeleton gate.
1357
+ return { text: out.join('\n'), pathNodeIds: pathIds, namedNodeIds: new Set(named.keys()), uniqueNamedNodeIds };
1358
+ }
1359
+ catch {
1360
+ return EMPTY;
1361
+ }
1362
+ }
1363
+ /**
1364
+ * Compact "blast radius" for the entry symbols of an explore result: who
1365
+ * depends on each (callers) and which test files cover it — LOCATIONS ONLY,
1366
+ * no source, so the agent knows what to update / re-verify before editing
1367
+ * without reaching for a separate impact call. Always-on, but skips symbols
1368
+ * that have no dependents (nothing to warn about), and returns '' when none
1369
+ * qualify so a leaf-only exploration stays clean.
1370
+ */
1371
+ /**
1372
+ * Domain-facts section for `specship_explore` (REQ-DOMAIN-005.A2). Domain
1373
+ * facts live in the spec layer, not the node graph, so explore's symbol
1374
+ * search never reaches them. When the query names a documented domain term
1375
+ * or entity, surface the matching human-confirmed fact bodies inline so the
1376
+ * agent gets the authoritative project semantics without a second call.
1377
+ *
1378
+ * Additive + silent: returns '' unless a domain fact matches a query token,
1379
+ * so it never bloats a normal code exploration (the hot-path guard in
1380
+ * CLAUDE.md). Facts are few and human-authored, so a linear scan + token
1381
+ * match is cheap and precise enough.
1382
+ */
1383
+ buildDomainFactsSection(cg, query) {
1384
+ let facts;
1385
+ try {
1386
+ facts = cg.getSpecQueries().getSpecsByKind('domain');
1387
+ }
1388
+ catch {
1389
+ return '';
1390
+ }
1391
+ if (!facts || facts.length === 0)
1392
+ return '';
1393
+ // Tokens of length >= 4 keep the match precise — short words ("the", "id")
1394
+ // would match almost any fact body.
1395
+ const tokens = [...new Set(query.toLowerCase().split(/[^a-z0-9_]+/i).filter((t) => t.length >= 4))];
1396
+ if (tokens.length === 0)
1397
+ return '';
1398
+ const matched = facts
1399
+ .filter((f) => {
1400
+ const hay = `${f.title} ${f.body ?? ''}`.toLowerCase();
1401
+ return tokens.some((t) => hay.includes(t));
1402
+ })
1403
+ .slice(0, 5);
1404
+ if (matched.length === 0)
1405
+ return '';
1406
+ const out = [
1407
+ '### Domain facts',
1408
+ '',
1409
+ '_Human-confirmed domain knowledge matching this query (from the spec layer, not code). Treat as authoritative project context — no need to look further for these terms._',
1410
+ '',
1411
+ ];
1412
+ for (const f of matched) {
1413
+ const factType = f.metadata?.type;
1414
+ const typeSuffix = typeof factType === 'string' ? ` · ${factType}` : '';
1415
+ out.push(`#### ${f.id} — ${f.title}${typeSuffix}`);
1416
+ const body = (f.body ?? '').trim();
1417
+ out.push(body.length > 800 ? `${body.slice(0, 800)}…` : body || '_(empty)_');
1418
+ out.push('');
1419
+ }
1420
+ return out.join('\n');
1421
+ }
1422
+ buildBlastRadiusSection(cg, subgraph) {
1423
+ const ROOT_CAP = 5; // only the symbols the query actually targeted
1424
+ const FILE_CAP = 4; // caller files listed per symbol before "+N more"
1425
+ const MEANINGFUL = new Set([
1426
+ 'function', 'method', 'class', 'interface', 'struct', 'trait', 'protocol',
1427
+ 'enum', 'type_alias', 'component', 'constant', 'variable', 'property', 'field',
1428
+ ]);
1429
+ const rel = (p) => p.replace(/\\/g, '/');
1430
+ const roots = subgraph.roots
1431
+ .map((id) => subgraph.nodes.get(id))
1432
+ .filter((n) => !!n && MEANINGFUL.has(n.kind))
1433
+ .slice(0, ROOT_CAP);
1434
+ if (roots.length === 0)
1435
+ return '';
1436
+ const entries = [];
1437
+ for (const root of roots) {
1438
+ let callers = [];
1439
+ try {
1440
+ callers = cg.getCallers(root.id);
1441
+ }
1442
+ catch { /* skip this root */ }
1443
+ const seen = new Set();
1444
+ const uniq = [];
1445
+ for (const c of callers) {
1446
+ if (c?.node && !seen.has(c.node.id)) {
1447
+ seen.add(c.node.id);
1448
+ uniq.push(c.node);
1449
+ }
1450
+ }
1451
+ if (uniq.length === 0)
1452
+ continue; // no blast radius → nothing to flag
1453
+ const callerFiles = [...new Set(uniq.map((n) => rel(n.filePath)))];
1454
+ const testFiles = callerFiles.filter((f) => (0, query_utils_1.isTestFile)(f));
1455
+ const nonTest = callerFiles.filter((f) => !(0, query_utils_1.isTestFile)(f));
1456
+ const shown = nonTest.slice(0, FILE_CAP).map((f) => `\`${f}\``).join(', ');
1457
+ const more = nonTest.length > FILE_CAP ? ` +${nonTest.length - FILE_CAP} more` : '';
1458
+ const where = nonTest.length > 0 ? ` in ${shown}${more}` : '';
1459
+ const tests = testFiles.length > 0
1460
+ ? `; tests: ${testFiles.slice(0, FILE_CAP).map((f) => `\`${f}\``).join(', ')}${testFiles.length > FILE_CAP ? ` +${testFiles.length - FILE_CAP}` : ''}`
1461
+ : '; ⚠️ no covering tests found';
1462
+ entries.push(`- \`${root.name}\` (${rel(root.filePath)}:${root.startLine}) — ${uniq.length} caller${uniq.length === 1 ? '' : 's'}${where}${tests}`);
1463
+ }
1464
+ if (entries.length === 0)
1465
+ return '';
1466
+ return [
1467
+ '### Blast radius — what depends on these (update/verify before editing)',
1468
+ '',
1469
+ ...entries,
1470
+ '',
1471
+ ].join('\n');
1472
+ }
1473
+ /**
1474
+ * Graph-connectivity relevance via Random-Walk-with-Restart (personalized
1475
+ * PageRank) from the query's matched SEED nodes over the call/reference graph.
1476
+ *
1477
+ * This is the ranking signal text search (FTS/bm25) CANNOT provide, and it's
1478
+ * specship's home turf: relevance by STRUCTURE, not words. A file whose
1479
+ * symbols are call-connected to the matched cluster accrues walk mass and
1480
+ * ranks high; a lone TEXT match — e.g. `LensSwitcher.swift` matched the word
1481
+ * "switch" from `switchOrganization`, but calls none of `setUser`/`fetchUser`
1482
+ * — gets only its own restart probability and ranks ~0. Immune to the
1483
+ * tokenization trap that fools term matching, deterministic, no embeddings.
1484
+ *
1485
+ * Undirected adjacency (reachability both ways), restart α=0.25 to the seeds,
1486
+ * power iteration to convergence. Bounded to the already-relevant subgraph, so
1487
+ * it's a few hundred nodes × ~25 iterations — negligible cost.
1488
+ */
1489
+ computeGraphRelevance(nodeIds, edges, seedIds) {
1490
+ const out = new Map();
1491
+ const n = nodeIds.length;
1492
+ if (n === 0)
1493
+ return out;
1494
+ const idx = new Map();
1495
+ for (let i = 0; i < n; i++)
1496
+ idx.set(nodeIds[i], i);
1497
+ const RANK_EDGES = new Set([
1498
+ 'calls', 'references', 'extends', 'implements', 'overrides',
1499
+ 'instantiates', 'returns', 'type_of', 'imports',
1500
+ ]);
1501
+ const adj = Array.from({ length: n }, () => []);
1502
+ for (const e of edges) {
1503
+ if (!RANK_EDGES.has(e.kind))
1504
+ continue;
1505
+ const i = idx.get(e.source);
1506
+ const j = idx.get(e.target);
1507
+ if (i === undefined || j === undefined || i === j)
1508
+ continue;
1509
+ adj[i].push(j);
1510
+ adj[j].push(i); // undirected — reachable either direction
1511
+ }
1512
+ // Restart vector: uniform over seeds present in the candidate set. (Falls
1513
+ // back to uniform-over-all if no seed landed in the set, so we never return
1514
+ // all-zero.)
1515
+ const r = new Array(n).fill(0);
1516
+ let rsum = 0;
1517
+ for (const id of seedIds) {
1518
+ const i = idx.get(id);
1519
+ if (i !== undefined) {
1520
+ r[i] = 1;
1521
+ rsum += 1;
1522
+ }
1523
+ }
1524
+ if (rsum === 0) {
1525
+ for (let i = 0; i < n; i++)
1526
+ r[i] = 1;
1527
+ rsum = n;
1528
+ }
1529
+ for (let i = 0; i < n; i++)
1530
+ r[i] /= rsum;
1531
+ const alpha = 0.25;
1532
+ let s = r.slice();
1533
+ for (let iter = 0; iter < 25; iter++) {
1534
+ const next = new Array(n).fill(0);
1535
+ for (let i = 0; i < n; i++) {
1536
+ const si = s[i];
1537
+ if (si === 0)
1538
+ continue;
1539
+ const d = adj[i].length;
1540
+ if (d === 0) {
1541
+ next[i] += si;
1542
+ continue;
1543
+ } // dangling: keep its mass
1544
+ const share = si / d;
1545
+ for (const j of adj[i])
1546
+ next[j] += share;
1547
+ }
1548
+ for (let i = 0; i < n; i++)
1549
+ s[i] = (1 - alpha) * next[i] + alpha * r[i];
1550
+ }
1551
+ for (let i = 0; i < n; i++)
1552
+ out.set(nodeIds[i], s[i]);
1553
+ return out;
1554
+ }
1555
+ /**
1556
+ * Handle specship_explore — deep exploration in a single call
1557
+ *
1558
+ * Strategy: find relevant symbols via graph traversal, group by file,
1559
+ * then read contiguous file sections covering all symbols per file.
1560
+ * This replaces multiple specship_node + Read calls.
1561
+ *
1562
+ * Output size is adaptive to project file count via
1563
+ * `getExploreOutputBudget` — see #185 for why a fixed 35k cap was a
1564
+ * tax on small projects while earning its keep on large ones.
1565
+ */
1566
+ async handleExplore(args) {
1567
+ const query = this.validateString(args.query, 'query');
1568
+ if (typeof query !== 'string')
1569
+ return query;
1570
+ const cg = this.getSpecShip(args.projectPath);
1571
+ const projectRoot = cg.getProjectRoot();
1572
+ // Resolve adaptive output budget from project size. Falls back to the
1573
+ // largest-tier defaults if stats aren't available, which preserves
1574
+ // pre-#185 behavior for callers that hit the rare stats failure.
1575
+ let budget;
1576
+ try {
1577
+ budget = getExploreOutputBudget(cg.getStats().fileCount);
1578
+ }
1579
+ catch {
1580
+ budget = getExploreOutputBudget(Infinity);
1581
+ }
1582
+ const maxFiles = (0, utils_1.clamp)(args.maxFiles || budget.defaultMaxFiles, 1, 20);
1583
+ // Step 1: Find relevant context with generous parameters.
1584
+ // Use a large maxNodes budget — explore has its own 35k char output limit
1585
+ // that prevents context bloat, so more nodes just means better coverage
1586
+ // across entry points (especially for large files like Svelte components).
1587
+ const subgraph = await cg.findRelevantContext(query, {
1588
+ searchLimit: 8,
1589
+ traversalDepth: 3,
1590
+ maxNodes: 200,
1591
+ minScore: 0.2,
1592
+ });
1593
+ if (subgraph.nodes.size === 0) {
1594
+ // No code matched — but a documented domain term may still have a fact
1595
+ // (it lives in the spec layer, not the node graph). Surface it so naming
1596
+ // a pure domain concept isn't a dead end (REQ-DOMAIN-005.A2).
1597
+ const domainOnly = this.buildDomainFactsSection(cg, query);
1598
+ if (domainOnly) {
1599
+ return this.textResult(`## Exploration: ${query}\n\n${domainOnly}`);
1600
+ }
1601
+ return this.textResult(`No relevant code found for "${query}"`);
1602
+ }
1603
+ // Graph-aware glue: findRelevantContext builds the subgraph from name/text
1604
+ // search, so a method that BRIDGES named symbols — e.g. App.tsx's
1605
+ // triggerRender, which calls the named triggerUpdate — is never a search hit
1606
+ // and gets missed, forcing the agent to Read the file to trace it. Pull in
1607
+ // the callers/callees of the entry (root) nodes, but ONLY those that live in
1608
+ // files the subgraph already surfaces (where the agent reads to fill gaps),
1609
+ // so we add wiring without dragging in unrelated files. These get an
1610
+ // importance boost below so they survive the per-file cluster budget.
1611
+ const glueNodeIds = new Set();
1612
+ const subgraphFiles = new Set();
1613
+ for (const n of subgraph.nodes.values())
1614
+ subgraphFiles.add(n.filePath);
1615
+ const GLUE_NODE_CAP = 60;
1616
+ for (const rootId of subgraph.roots) {
1617
+ if (glueNodeIds.size >= GLUE_NODE_CAP)
1618
+ break;
1619
+ let neighbors = [];
1620
+ try {
1621
+ neighbors = [
1622
+ ...cg.getCallers(rootId).map(c => c.node),
1623
+ ...cg.getCallees(rootId).map(c => c.node),
1624
+ ];
1625
+ }
1626
+ catch {
1627
+ continue;
1628
+ }
1629
+ for (const nb of neighbors) {
1630
+ if (glueNodeIds.size >= GLUE_NODE_CAP)
1631
+ break;
1632
+ if (subgraph.nodes.has(nb.id))
1633
+ continue;
1634
+ if (!subgraphFiles.has(nb.filePath))
1635
+ continue;
1636
+ subgraph.nodes.set(nb.id, nb);
1637
+ glueNodeIds.add(nb.id);
1638
+ }
1639
+ }
1640
+ // Named-symbol seeding: findRelevantContext is an FTS/text rank, so a query
1641
+ // that's a BAG of symbol names skewed toward one phase (Alamofire: 5 build
1642
+ // terms, each a high-frequency name, vs 3 validate terms) lets the
1643
+ // lower-frequency names fall below the search cut — their definitions, and
1644
+ // whole files (Validation.swift), never get gathered, so they can never
1645
+ // render and the agent Reads them. Resolve EACH named token to its
1646
+ // substantive definition (skip empty stubs + test files, same relevance the
1647
+ // trace endpoint picker uses) and inject it as an entry, so every symbol the
1648
+ // agent explicitly named is in the subgraph and its file is scored.
1649
+ const namedSeedIds = new Set();
1650
+ {
1651
+ 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)$/i;
1652
+ const CALLABLE = new Set(['method', 'function', 'component', 'constructor']);
1653
+ const isTestPath = (p) => /(^|\/)(tests?|specs?|__tests__|testdata|mocks?|fixtures?)\//i.test(p) || /\.(test|spec)\.[a-z]+$/i.test(p);
1654
+ const bodyLines = (n) => Math.max(0, (n.endLine ?? n.startLine) - n.startLine);
1655
+ const tokens = [...new Set(query.split(/[\s,()[\]]+/)
1656
+ .map((t) => t.replace(FILE_EXT, '').trim())
1657
+ .filter((t) => t.length >= 3 && /^[A-Za-z_$][\w$]*(?:(?:::|\.)[\w$]+)*$/.test(t)))].slice(0, 16);
1658
+ // PascalCase tokens in the query are type/file disambiguators — when the
1659
+ // agent writes "DataRequest task validate", the `task`/`validate` it wants
1660
+ // are DataRequest's, NOT the same-named overloads in Validation.swift /
1661
+ // Concurrency.swift / the abstract base. Used below to bias overloaded
1662
+ // names toward the file/class the query also names.
1663
+ const typeTokens = tokens.filter((o) => /^[A-Z][A-Za-z0-9]{3,}/.test(o));
1664
+ const inNamedContext = (n) => typeTokens.some((ct) => {
1665
+ const lc = ct.toLowerCase();
1666
+ return n.filePath.toLowerCase().includes(lc) || n.qualifiedName.toLowerCase().includes(lc);
1667
+ });
1668
+ for (const t of tokens) {
1669
+ // Enumerate ALL defs of a bare token via the direct index, not FTS — a
1670
+ // 50+-overload name (tokio `poll`) ranks the wanted def (`Harness::poll`)
1671
+ // below the FTS cut, so findAllSymbols would never see it and the
1672
+ // type-token bias below couldn't pick the harness.rs one. (Same fix as
1673
+ // specship_node's findSymbolMatches.) Qualified tokens keep findAllSymbols.
1674
+ const isQual = /[.\/]|::/.test(t);
1675
+ const raw = isQual ? this.findAllSymbols(cg, t).nodes : cg.getNodesByName(t);
1676
+ const cands = raw
1677
+ .filter((n) => CALLABLE.has(n.kind) && !isTestPath(n.filePath))
1678
+ .sort((a, b) => (bodyLines(b) > 1 ? 1 : 0) - (bodyLines(a) > 1 ? 1 : 0) || bodyLines(b) - bodyLines(a));
1679
+ // A specific name (<=3 defs) injects all its defs. An overloaded name
1680
+ // (`validate` = 10, `request` = 44) would flood the subgraph, so inject
1681
+ // only: the overloads whose file/class the query ALSO names (the agent
1682
+ // told us which one it wants — DataRequest's, not Validation.swift's),
1683
+ // capped; else fall back to the single most-substantive def. This is the
1684
+ // explore-side mirror of specship_node's overload disambiguation.
1685
+ let picks;
1686
+ if (cands.length <= 3) {
1687
+ picks = cands;
1688
+ }
1689
+ else {
1690
+ const ctx = cands.filter(inNamedContext);
1691
+ picks = ctx.length > 0 ? ctx.slice(0, 4) : cands.slice(0, 1);
1692
+ }
1693
+ for (const n of picks) {
1694
+ if (!subgraph.nodes.has(n.id))
1695
+ subgraph.nodes.set(n.id, n);
1696
+ // Mark as a named seed EVEN IF the FTS gather already had it — being
1697
+ // "named by the agent" is independent of whether search happened to
1698
+ // surface it, and it drives the +50 score, the gate, and the
1699
+ // named-file sort below. (Previously only NEW injections were marked,
1700
+ // so a named symbol FTS already gathered never sorted to the top.)
1701
+ namedSeedIds.add(n.id);
1702
+ }
1703
+ }
1704
+ }
1705
+ // Step 2: Group nodes by file, score by relevance
1706
+ const fileGroups = new Map();
1707
+ const entryNodeIds = new Set([...subgraph.roots, ...namedSeedIds]);
1708
+ // Build a set of nodes directly connected to entry points (depth 1)
1709
+ const connectedToEntry = new Set();
1710
+ for (const edge of subgraph.edges) {
1711
+ if (entryNodeIds.has(edge.source))
1712
+ connectedToEntry.add(edge.target);
1713
+ if (entryNodeIds.has(edge.target))
1714
+ connectedToEntry.add(edge.source);
1715
+ }
1716
+ for (const node of subgraph.nodes.values()) {
1717
+ // Skip import/export nodes — they add noise without information
1718
+ if (node.kind === 'import' || node.kind === 'export')
1719
+ continue;
1720
+ const group = fileGroups.get(node.filePath) || { nodes: [], score: 0 };
1721
+ group.nodes.push(node);
1722
+ // Score: a NAMED-SEED node (a symbol the agent named that FTS missed, now
1723
+ // injected) is worth far more than a mere reference — its file is where the
1724
+ // answer lives. Without this, an incidental file that name-drops the flow
1725
+ // (Combine.swift references request/task → score 23 from connected nodes)
1726
+ // outranks the file that DEFINES a named symbol (Validation.swift's
1727
+ // `validate` → 10) and steals its render slot. Definition ≫ reference.
1728
+ if (namedSeedIds.has(node.id)) {
1729
+ group.score += 50;
1730
+ }
1731
+ else if (entryNodeIds.has(node.id)) {
1732
+ group.score += 10;
1733
+ }
1734
+ else if (connectedToEntry.has(node.id)) {
1735
+ group.score += 3;
1736
+ }
1737
+ else {
1738
+ group.score += 1;
1739
+ }
1740
+ fileGroups.set(node.filePath, group);
1741
+ }
1742
+ // Only include files that have entry points or nodes directly connected to entry points
1743
+ let relevantFiles = [...fileGroups.entries()].filter(([, group]) => group.score >= 3);
1744
+ // Extract query terms for relevance checking
1745
+ const queryTerms = query.toLowerCase().split(/\s+/).filter(t => t.length >= 3);
1746
+ // Test/spec/icon/i18n file detector — used both for the pre-sort hard
1747
+ // filter (tiny tier) and the comparator deprioritization (all tiers).
1748
+ const isLowValue = (p) => {
1749
+ const lp = p.toLowerCase();
1750
+ return (/\/(tests?|__tests?__|spec)\//.test(lp) ||
1751
+ /_test\.go$/.test(lp) ||
1752
+ /(?:^|\/)test_[^/]+\.py$/.test(lp) ||
1753
+ /_test\.py$/.test(lp) ||
1754
+ /_spec\.rb$/.test(lp) ||
1755
+ /_test\.rb$/.test(lp) ||
1756
+ /\.(test|spec)\.[jt]sx?$/.test(lp) ||
1757
+ /(test|spec|tests)\.(java|kt|scala)$/.test(lp) ||
1758
+ /(tests?|spec)\.cs$/.test(lp) ||
1759
+ /tests?\.swift$/.test(lp) ||
1760
+ /_test\.dart$/.test(lp) ||
1761
+ /\bicons?\b/.test(lp) ||
1762
+ /\bi18n\b/.test(lp));
1763
+ };
1764
+ // Hard-exclude test/spec files (ALL tiers, not just tiny). One slipped test
1765
+ // file dominates the per-file budget on small repos (cobra's `command_test.go`
1766
+ // displaced `args.go`) AND wastes budget on large ones (Django's
1767
+ // `custom_lookups/tests.py` ate ~2.3 KB of the 28 KB cap, crowding out the
1768
+ // SQLCompiler mechanism the agent then Read). A test file almost never answers
1769
+ // an architecture question. Skip when the query itself is about tests — the
1770
+ // legitimate "explore the tests" case — and only cut if ≥2 non-test candidates
1771
+ // remain (else tests are the only signal for this area).
1772
+ {
1773
+ const queryMentionsTests = /\b(test|tests|testing|spec|verify|verifies)\b/i.test(query);
1774
+ if (!queryMentionsTests) {
1775
+ const nonLow = relevantFiles.filter(([p]) => !isLowValue(p));
1776
+ if (nonLow.length >= 2) {
1777
+ relevantFiles = nonLow;
1778
+ }
1779
+ }
1780
+ }
1781
+ // Secondary signal: how many DISTINCT query terms each file matches (path +
1782
+ // symbol names). Kept only as a tiebreak — the PRIMARY relevance is graph
1783
+ // connectivity below. (Term counting alone tied the real central file with
1784
+ // incidental same-word matches; it's a weak text signal, not the ranker.)
1785
+ const uniqueQueryTerms = [...new Set(queryTerms)].filter(t => t.length >= 3);
1786
+ const fileTermHits = new Map();
1787
+ for (const [fp, group] of relevantFiles) {
1788
+ const hay = fp.toLowerCase() + ' ' + group.nodes.map(n => n.name.toLowerCase()).join(' ');
1789
+ let hits = 0;
1790
+ for (const t of uniqueQueryTerms)
1791
+ if (hay.includes(t))
1792
+ hits++;
1793
+ fileTermHits.set(fp, hits);
1794
+ }
1795
+ // PRIMARY relevance: graph connectivity (Random-Walk-with-Restart from the
1796
+ // matched seeds — see computeGraphRelevance). Aggregate each file's nodes'
1797
+ // walk mass. This is the signal text search lacks: the real cluster
1798
+ // (org-user.storage.ts, call-connected to the matches) accrues mass; a lone
1799
+ // text match (LensSwitcher.swift, matched "switch" but calls nothing in the
1800
+ // flow) gets only its restart probability → ~0, and is dropped by the gate.
1801
+ const nodeRwr = this.computeGraphRelevance([...subgraph.nodes.keys()], subgraph.edges, entryNodeIds);
1802
+ const fileGraphScore = new Map();
1803
+ for (const node of subgraph.nodes.values()) {
1804
+ fileGraphScore.set(node.filePath, (fileGraphScore.get(node.filePath) ?? 0) + (nodeRwr.get(node.id) ?? 0));
1805
+ }
1806
+ const maxGraph = Math.max(0, ...fileGraphScore.values());
1807
+ // Central file(s): the 1-2 most graph-central files that also match the
1808
+ // query textually (so a connected hub-utility with no term match isn't
1809
+ // mistaken for the subject). The heart of the answer — they earn the larger
1810
+ // WHOLE-FILE ceiling below (a god-file central file still exceeds it and
1811
+ // falls to generous full-method sectioning — never a whole dump).
1812
+ const centralFiles = new Set([...fileGraphScore.entries()]
1813
+ .filter(([fp, g]) => g > 0 && (fileTermHits.get(fp) ?? 0) >= 1)
1814
+ .sort((a, b) => b[1] - a[1] || (fileTermHits.get(b[0]) ?? 0) - (fileTermHits.get(a[0]) ?? 0))
1815
+ .slice(0, 2)
1816
+ .map(([f]) => f));
1817
+ // Files that DEFINE a symbol the agent named (or a subgraph root). These are
1818
+ // the highest-relevance files there are — the agent asked for them by name —
1819
+ // so the connectivity gate below must never drop them, even when their RWR
1820
+ // mass is low (a leaf family file like codec.ts is call-connected to little
1821
+ // but is exactly what the agent queried). Without this protection the gate
1822
+ // prunes a named file and the agent Reads it back.
1823
+ const entryFiles = new Set();
1824
+ for (const id of entryNodeIds) {
1825
+ const n = subgraph.nodes.get(id);
1826
+ if (n)
1827
+ entryFiles.add(n.filePath);
1828
+ }
1829
+ // Relevance gate (so the generous budget is a CEILING, not a target): keep a
1830
+ // file only if it is STRUCTURALLY relevant by ANY of:
1831
+ // - graph score within a fraction of the top (it's on/near the flow), OR
1832
+ // - central (a query entry-point lives here), OR
1833
+ // - it DEFINES a symbol the agent named (entryFiles), OR
1834
+ // - it matches >= 2 DISTINCT named query terms — a strong text signal that
1835
+ // the agent is asking about this file even when nothing calls it (codec.ts:
1836
+ // the agent named `encode`/`Codec`/`JsonCodec`, all leaf classes with zero
1837
+ // RWR mass — graph alone wrongly drops it).
1838
+ // A lone text match on one shared word (LensSwitcher: term=1, g~0) is still
1839
+ // dropped, so the budget never fills with incidental files. Guarded so it
1840
+ // never prunes below 2.
1841
+ if (maxGraph > 0) {
1842
+ const gated = relevantFiles.filter(([fp]) => (fileGraphScore.get(fp) ?? 0) >= maxGraph * 0.06
1843
+ || centralFiles.has(fp)
1844
+ || entryFiles.has(fp)
1845
+ || (fileTermHits.get(fp) ?? 0) >= 2);
1846
+ if (gated.length >= 2)
1847
+ relevantFiles = gated;
1848
+ }
1849
+ // Sort files: graph-central first, then distinct-term match, then the
1850
+ // existing low-value/generated/score tiebreaks.
1851
+ // Files that DEFINE a symbol the agent NAMED. These sort first — ahead of
1852
+ // graph connectivity — because the agent asked for them by name. Without
1853
+ // this, a named leaf override reached only by dynamic dispatch (Alamofire's
1854
+ // `DataRequest.task`/`validate`, low RWR mass) sorts below the high-
1855
+ // connectivity abstract base (`Request.swift`) and the same-named overloads
1856
+ // in other files (`Validation.swift`), falls outside the budget, and the
1857
+ // agent Reads it. The named file is the answer — rank it at the top.
1858
+ const namedSeedFiles = new Set();
1859
+ for (const id of namedSeedIds) {
1860
+ const n = subgraph.nodes.get(id);
1861
+ if (n)
1862
+ namedSeedFiles.add(n.filePath);
1863
+ }
1864
+ const sortedFiles = relevantFiles.sort((a, b) => {
1865
+ const aPath = a[0].toLowerCase();
1866
+ const bPath = b[0].toLowerCase();
1867
+ // Agent-named files first (it asked for a symbol defined here by name).
1868
+ const aNamed = namedSeedFiles.has(a[0]) ? 1 : 0;
1869
+ const bNamed = namedSeedFiles.has(b[0]) ? 1 : 0;
1870
+ if (aNamed !== bNamed)
1871
+ return bNamed - aNamed;
1872
+ // Graph connectivity is the next key (small epsilon so near-ties fall
1873
+ // through to the text signal rather than coin-flipping on float noise).
1874
+ const aG = fileGraphScore.get(a[0]) ?? 0;
1875
+ const bG = fileGraphScore.get(b[0]) ?? 0;
1876
+ if (Math.abs(aG - bG) > maxGraph * 0.01)
1877
+ return bG - aG;
1878
+ const aHits = fileTermHits.get(a[0]) ?? 0;
1879
+ const bHits = fileTermHits.get(b[0]) ?? 0;
1880
+ if (aHits !== bHits)
1881
+ return bHits - aHits;
1882
+ const aLow = isLowValue(aPath);
1883
+ const bLow = isLowValue(bPath);
1884
+ if (aLow !== bLow)
1885
+ return aLow ? 1 : -1;
1886
+ // Deprioritize generated source (.pb.go / .pulsar.go / _mocks.go / …) —
1887
+ // the agent rarely needs to see the protobuf scaffold or gomock output
1888
+ // when asking about the actual flow, and dumping their bodies inflates
1889
+ // the response (the cosmos Q3 explore otherwise leads with
1890
+ // `expected_keepers_mocks.go`, displacing the real `tally.go` content
1891
+ // and forcing the agent to Read tally.go anyway).
1892
+ const aGen = (0, generated_detection_1.isGeneratedFile)(a[0]);
1893
+ const bGen = (0, generated_detection_1.isGeneratedFile)(b[0]);
1894
+ if (aGen !== bGen)
1895
+ return aGen ? 1 : -1;
1896
+ if (a[1].score !== b[1].score)
1897
+ return b[1].score - a[1].score;
1898
+ return b[1].nodes.length - a[1].nodes.length;
1899
+ });
1900
+ // Step 3: Build relationship map
1901
+ const lines = [
1902
+ `## Exploration: ${query}`,
1903
+ '',
1904
+ `Found ${subgraph.nodes.size} symbols across ${fileGroups.size} files.`,
1905
+ '',
1906
+ ];
1907
+ // Domain facts (REQ-DOMAIN-005.A2): if the query names a documented domain
1908
+ // term/entity, lead with the human-confirmed fact body. Placed near the top
1909
+ // (before the source sections) so it is never lost to the output-budget
1910
+ // truncation that drops trailing file sections.
1911
+ const domainFacts = this.buildDomainFactsSection(cg, query);
1912
+ if (domainFacts)
1913
+ lines.push(domainFacts);
1914
+ // Blast radius (always-on, compact): for the entry symbols, who depends on
1915
+ // them + which tests cover them — locations only, no source — so the agent
1916
+ // knows what to update/verify before editing without a separate call.
1917
+ const blastRadius = this.buildBlastRadiusSection(cg, subgraph);
1918
+ if (blastRadius)
1919
+ lines.push(blastRadius);
1920
+ // Relationship map — show how symbols connect
1921
+ const significantEdges = subgraph.edges.filter(e => e.kind !== 'contains' // skip contains — it's implied by file grouping
1922
+ );
1923
+ if (budget.includeRelationships && significantEdges.length > 0) {
1924
+ lines.push('### Relationships');
1925
+ lines.push('');
1926
+ // Group edges by kind for readability
1927
+ const byKind = new Map();
1928
+ for (const edge of significantEdges) {
1929
+ const sourceNode = subgraph.nodes.get(edge.source);
1930
+ const targetNode = subgraph.nodes.get(edge.target);
1931
+ if (!sourceNode || !targetNode)
1932
+ continue;
1933
+ const group = byKind.get(edge.kind) || [];
1934
+ group.push({ source: sourceNode.name, target: targetNode.name });
1935
+ byKind.set(edge.kind, group);
1936
+ }
1937
+ for (const [kind, edges] of byKind) {
1938
+ const cap = budget.maxEdgesPerRelationshipKind;
1939
+ const shown = edges.slice(0, cap);
1940
+ lines.push(`**${kind}:**`);
1941
+ for (const e of shown) {
1942
+ lines.push(`- ${e.source} → ${e.target}`);
1943
+ }
1944
+ if (edges.length > cap) {
1945
+ lines.push(`- ... and ${edges.length - cap} more`);
1946
+ }
1947
+ lines.push('');
1948
+ }
1949
+ }
1950
+ // Step 4: Read contiguous file sections
1951
+ // Compute the flow spine once — used both to prepend the Flow section (below)
1952
+ // and to gate adaptive source sizing: files on the spine get full source,
1953
+ // off-spine peers skeletonize.
1954
+ const flow = this.buildFlowFromNamedSymbols(cg, query);
1955
+ // Polymorphic-sibling detector for adaptive sizing. A class that implements/
1956
+ // extends a supertype shared by >= MIN_SIBLINGS classes is one of many
1957
+ // INTERCHANGEABLE implementations (OkHttp's 14 `: Interceptor` classes —
1958
+ // showing one + the rest as signatures is enough), as opposed to a DISTINCT
1959
+ // pipeline step (Excalidraw's `renderStaticScene`, which shares no supertype and
1960
+ // must stay full or the agent loses real content). Only off-spine sibling files
1961
+ // skeletonize; distinct steps and on-spine files keep full source. Cache
1962
+ // supertype→(has ≥N implementers) so this stays a handful of edge queries.
1963
+ const MIN_SIBLINGS = 3;
1964
+ const siblingSuper = new Map();
1965
+ const isPolymorphicSibling = (nodes) => {
1966
+ for (const n of nodes) {
1967
+ for (const e of cg.getOutgoingEdges(n.id)) {
1968
+ if (e.kind !== 'implements' && e.kind !== 'extends')
1969
+ continue;
1970
+ let many = siblingSuper.get(e.target);
1971
+ if (many === undefined) {
1972
+ many = cg.getIncomingEdges(e.target)
1973
+ .filter((x) => x.kind === 'implements' || x.kind === 'extends').length >= MIN_SIBLINGS;
1974
+ siblingSuper.set(e.target, many);
1975
+ }
1976
+ if (many)
1977
+ return true;
1978
+ }
1979
+ }
1980
+ return false;
1981
+ };
1982
+ // A file that DEFINES a polymorphic supertype (a class/interface with ≥
1983
+ // MIN_SIBLINGS implementers) AND co-locates its subclasses is a redundant
1984
+ // "family" file — Django's compiler.py holds `SQLCompiler` + its 4 subclasses
1985
+ // (SQLInsert/Update/Delete/AggregateCompiler) in 2,266 lines. Such files are
1986
+ // huge and read-anyway, so they should STILL skeletonize even when the agent
1987
+ // named a method in them: a full one eats ~6.5K of the explore budget (Django
1988
+ // is pinned at the 28K cap, truncating), starving the sibling files the agent
1989
+ // then Reads. This flag OVERRIDES the named-callable spare below — it does NOT
1990
+ // by itself spare a file. (OkHttp's RealCall implements the `Lockable` mixin
1991
+ // but defines no ≥3-impl supertype, so the named spare keeps it full.)
1992
+ const superMany = new Map();
1993
+ const definesPolymorphicSupertype = (nodes) => {
1994
+ for (const n of nodes) {
1995
+ if (n.kind !== 'class' && n.kind !== 'interface' && n.kind !== 'struct'
1996
+ && n.kind !== 'trait' && n.kind !== 'protocol' && n.kind !== 'type_alias')
1997
+ continue;
1998
+ let many = superMany.get(n.id);
1999
+ if (many === undefined) {
2000
+ many = cg.getIncomingEdges(n.id)
2001
+ .filter((x) => x.kind === 'implements' || x.kind === 'extends').length >= MIN_SIBLINGS;
2002
+ superMany.set(n.id, many);
2003
+ }
2004
+ if (many)
2005
+ return true;
2006
+ }
2007
+ return false;
2008
+ };
2009
+ lines.push('### Source Code');
2010
+ lines.push('');
2011
+ 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.');
2012
+ lines.push('');
2013
+ let totalChars = lines.join('\n').length;
2014
+ let filesIncluded = 0;
2015
+ let anyFileTrimmed = false;
2016
+ for (const [filePath, group] of sortedFiles) {
2017
+ if (filesIncluded >= maxFiles)
2018
+ break;
2019
+ // A file DEFINES a named/spine symbol (the answer) vs merely references the
2020
+ // flow. Past 90% budget, stop pulling INCIDENTAL files — but keep scanning
2021
+ // for necessary ones, which render even past the cap (bounded by maxFiles).
2022
+ // Without this `continue` (was an unconditional `break`), the loop stopped
2023
+ // after the build + validators-exec files and never reached the ranked-in
2024
+ // validate-logic file (Alamofire's Validation.swift).
2025
+ const fileNecessary = group.nodes.some(n => entryNodeIds.has(n.id) || flow.pathNodeIds.has(n.id) || flow.uniqueNamedNodeIds.has(n.id));
2026
+ if (!fileNecessary && totalChars > budget.maxOutputChars * 0.9)
2027
+ continue;
2028
+ const absPath = (0, utils_1.validatePathWithinRoot)(projectRoot, filePath);
2029
+ if (!absPath || !(0, fs_1.existsSync)(absPath))
2030
+ continue;
2031
+ let fileContent;
2032
+ try {
2033
+ fileContent = (0, fs_1.readFileSync)(absPath, 'utf-8');
2034
+ }
2035
+ catch {
2036
+ continue;
2037
+ }
2038
+ const fileLines = fileContent.split('\n');
2039
+ const lang = group.nodes[0]?.language || '';
2040
+ // Adaptive sizing (SPECSHIP_ADAPTIVE_EXPLORE, default on): collapse a file
2041
+ // to a per-symbol view when it's a redundant member of a polymorphic family.
2042
+ // Engages iff ALL hold:
2043
+ // 1. a flow spine exists,
2044
+ // 2. no symbol in the file is on that spine (it's not the mechanism path),
2045
+ // 3. it IS a polymorphic sibling (≥ MIN_SIBLINGS impls of a shared supertype),
2046
+ // 4. it is NOT SPARED, where a file is spared iff the agent named a
2047
+ // (near-)UNIQUE callable in it (`getResponseWithInterceptorChain`, 1 def →
2048
+ // keep RealCall.kt full) UNLESS the file DEFINES the family supertype (a
2049
+ // base+subclasses "family" file like Django's compiler.py — collapse it).
2050
+ // Uniqueness matters: `as_sql` has 110 defs across every Compiler/Expression
2051
+ // subclass; naming it must NOT keep every backend variant + test file full
2052
+ // and flood the budget. That's why the spare reads uniqueNamedNodeIds.
2053
+ // Within a collapsed file the render is PER-SYMBOL (condition B): a method the
2054
+ // agent NAMED or that's on the spine is shown with its FULL body (so the agent
2055
+ // doesn't Read the file back for it — Django's SQLCompiler.execute_sql/as_sql);
2056
+ // every other symbol is just its signature. So the base mechanism survives while
2057
+ // the file's other ~80 symbols + the redundant subclasses collapse to one line each.
2058
+ const spareNamed = group.nodes.some(n => flow.uniqueNamedNodeIds.has(n.id));
2059
+ const fileDefinesSuper = definesPolymorphicSupertype(group.nodes);
2060
+ const spared = spareNamed && !fileDefinesSuper;
2061
+ const CALLABLE_BODY = new Set(['method', 'function', 'constructor', 'component']);
2062
+ const hasSpineNode = group.nodes.some(n => flow.pathNodeIds.has(n.id));
2063
+ // On-spine god-file: the flow path runs THROUGH this file, but it also holds
2064
+ // many OTHER named methods, and rendering all of them in full blows the
2065
+ // per-file budget and starves the other flow files (Alamofire: the agent
2066
+ // names ~7 Session.swift methods — the build spine PLUS off-path
2067
+ // task/didCompleteTask — far past the whole response budget). Engage the
2068
+ // per-symbol view to keep the SPINE full and collapse the off-path named
2069
+ // methods to signatures. Only when there IS off-path content to shed —
2070
+ // otherwise the spine is irreducible (a sequential flow has no redundancy),
2071
+ // so leave it to the normal full render.
2072
+ const namedBodyChars = group.nodes
2073
+ .filter(n => CALLABLE_BODY.has(n.kind) && (flow.pathNodeIds.has(n.id) || flow.uniqueNamedNodeIds.has(n.id)))
2074
+ .reduce((s, n) => s + fileLines.slice(n.startLine - 1, n.endLine).join('\n').length, 0);
2075
+ const onSpineGodFile = hasSpineNode
2076
+ && namedBodyChars > budget.maxCharsPerFile
2077
+ && group.nodes.some(n => CALLABLE_BODY.has(n.kind) && flow.uniqueNamedNodeIds.has(n.id) && !flow.pathNodeIds.has(n.id));
2078
+ if (adaptiveExploreEnabled() && flow.pathNodeIds.size > 0
2079
+ && (onSpineGodFile || (!hasSpineNode && isPolymorphicSibling(group.nodes) && !spared))) {
2080
+ const syms = group.nodes
2081
+ .filter(n => n.kind !== 'import' && n.kind !== 'export' && n.startLine > 0)
2082
+ .sort((a, b) => a.startLine - b.startLine);
2083
+ // Pass 1: choose which symbols get a FULL body, by priority, greedily within
2084
+ // a per-file body cap — so one huge family file can't body every named method
2085
+ // and crowd out the other flow files (Django's query.py). A symbol earns a
2086
+ // body if it's on-spine, or UNIQUELY named (`SQLCompiler.execute_sql`), or a
2087
+ // co-named method WHEN this file DEFINES the family supertype (so the base
2088
+ // `SQLCompiler.as_sql` body shows, but the 110 leaf `as_sql` overrides — and
2089
+ // OkHttp's 5 `intercept`s if the agent names `intercept` — stay signatures).
2090
+ const prio = (n) => !CALLABLE_BODY.has(n.kind) ? 99
2091
+ : flow.pathNodeIds.has(n.id) ? 0
2092
+ : flow.uniqueNamedNodeIds.has(n.id) ? 1
2093
+ : (fileDefinesSuper && flow.namedNodeIds.has(n.id)) ? 2 : 99;
2094
+ // One ~250-line WINDOW per file. syms are taken by priority (spine first,
2095
+ // then uniquely-named, then family-base), and the cap applies to ALL of
2096
+ // them — including the spine — so a big-spine god-file (tokio's worker.rs:
2097
+ // run→run_task→next_task→steal_work) can't eat the whole response and
2098
+ // starve the co-flow file (harness.rs's poll). The native agent windows
2099
+ // such a file too (~190 lines at a time), so this mimics, not truncates.
2100
+ // Always emit ≥1 (never an empty section).
2101
+ const bodyCap = budget.maxCharsPerFile * 1.5;
2102
+ const bodyIds = new Set();
2103
+ let bodyChars = 0;
2104
+ for (const n of syms.filter(n => prio(n) < 99 && n.endLine >= n.startLine).sort((a, b) => prio(a) - prio(b))) {
2105
+ const sz = fileLines.slice(n.startLine - 1, n.endLine).join('\n').length;
2106
+ if (bodyChars + sz > bodyCap && bodyIds.size > 0)
2107
+ continue;
2108
+ bodyIds.add(n.id);
2109
+ bodyChars += sz;
2110
+ }
2111
+ // Pass 2: render in line order — full body for chosen symbols, else the
2112
+ // signature line (capped, with a "+N more" tail so the structure map of a
2113
+ // god-file doesn't itself bloat the budget).
2114
+ const skel = [];
2115
+ let coveredUntil = 0; // skip symbols already inside an emitted body
2116
+ let sigCount = 0, sigDropped = 0;
2117
+ const SIG_MAX = Math.max(12, budget.maxSymbolsInFileHeader * 2);
2118
+ for (const n of syms) {
2119
+ if (n.startLine <= coveredUntil)
2120
+ continue;
2121
+ if (bodyIds.has(n.id)) {
2122
+ const end = n.endLine;
2123
+ const body = fileLines.slice(n.startLine - 1, end).join('\n');
2124
+ skel.push(exploreLineNumbersEnabled() ? numberSourceLines(body, n.startLine) : body);
2125
+ coveredUntil = end;
2126
+ }
2127
+ else {
2128
+ // Elide the body, emit the signature. node.startLine can point at a
2129
+ // decorator/annotation, so scan forward for the line that names the symbol.
2130
+ let lineNo = n.startLine;
2131
+ for (let k = 0; k < 4; k++) {
2132
+ if ((fileLines[n.startLine - 1 + k] || '').includes(n.name)) {
2133
+ lineNo = n.startLine + k;
2134
+ break;
2135
+ }
2136
+ }
2137
+ if (lineNo <= coveredUntil)
2138
+ continue;
2139
+ if (sigCount >= SIG_MAX) {
2140
+ sigDropped++;
2141
+ continue;
2142
+ }
2143
+ const sig = (fileLines[lineNo - 1] || '').trim();
2144
+ if (sig) {
2145
+ skel.push(exploreLineNumbersEnabled() ? `${lineNo}\t${sig}` : sig);
2146
+ sigCount++;
2147
+ }
2148
+ }
2149
+ }
2150
+ if (sigDropped > 0)
2151
+ skel.push(`… +${sigDropped} more (signatures elided)`);
2152
+ if (skel.length > 0) {
2153
+ const names = [...new Set(group.nodes.filter(n => n.kind !== 'import' && n.kind !== 'export').map(n => n.name))]
2154
+ .slice(0, budget.maxSymbolsInFileHeader).join(', ');
2155
+ // Steer the agent to specship_explore for an elided body — NEVER to
2156
+ // Read. The old "Read for more" / "Read for a full body" tags invited
2157
+ // a Read of the very file just skeletonized; on a central, wanted file
2158
+ // (Session.swift, DataRequest.swift) that fired an over-investigation
2159
+ // spiral (the agent Read the skeletonized file, then kept digging).
2160
+ // CLAUDE.md: explore output must never tell the agent to Read.
2161
+ const tag = bodyIds.size > 0
2162
+ ? 'focused (the methods you named in full, the rest as signatures — specship_explore a signature by name for its body; do NOT Read)'
2163
+ : 'skeleton (signatures only — specship_explore a name for its full body; do NOT Read)';
2164
+ lines.push(`#### ${filePath} — ${names} · ${tag}`, '', '```' + lang, skel.join('\n'), '```', '');
2165
+ totalChars += skel.join('\n').length + 120;
2166
+ filesIncluded++;
2167
+ continue;
2168
+ }
2169
+ }
2170
+ // Whole-file rule: if a relevant file is small enough to afford, return it
2171
+ // ENTIRELY instead of clustering. Clustering exists to tame god-files
2172
+ // (App.tsx ~13k lines); on a ~134-line component a cluster is a lossy
2173
+ // subset of a file the agent will just Read in full anyway — costing a
2174
+ // round-trip and a re-read every later turn. Reserve clustering for files
2175
+ // too big to ship whole. Still bounded by the total maxOutputChars check.
2176
+ //
2177
+ // CENTRAL files (where the query's entry points live) get a larger — but
2178
+ // bounded — ceiling: they're the heart of the answer, the file(s) the agent
2179
+ // would Read whole, so a genuinely small one comes back whole rather than as
2180
+ // thin clusters. A LARGE central file (the 791-line org-user store) exceeds
2181
+ // the ceiling and falls through to sectioning/clustering below — full method
2182
+ // bodies + signatures — so we never dump (or overflow on) a whole god-file.
2183
+ const isCentralFile = centralFiles.has(filePath);
2184
+ // Central files get a slightly larger whole-file window than peripheral ones,
2185
+ // but a TIGHT one (~1.5× the per-file cap): the native read of a central file
2186
+ // is a ~150–250 line orientation window, NOT the whole file. A flat "whole
2187
+ // central file" both overflowed the inline cap AND starved the co-flow files
2188
+ // (worker.rs ate the budget, dropping harness.rs's poll). A larger central
2189
+ // file falls through to per-method windowing/clustering below.
2190
+ const WHOLE_FILE_MAX_LINES = isCentralFile ? 280 : 220;
2191
+ const WHOLE_FILE_MAX_CHARS = isCentralFile
2192
+ ? Math.min(Math.max(0, budget.maxOutputChars - totalChars - 200), Math.round(budget.maxCharsPerFile * 1.5))
2193
+ : budget.maxCharsPerFile * 3;
2194
+ if (fileLines.length <= WHOLE_FILE_MAX_LINES && fileContent.length <= WHOLE_FILE_MAX_CHARS) {
2195
+ const body = fileContent.replace(/\n+$/, '');
2196
+ let wholeSection = exploreLineNumbersEnabled() ? numberSourceLines(body, 1) : body;
2197
+ const uniqSymbols = [...new Set(group.nodes
2198
+ .filter(n => n.kind !== 'import' && n.kind !== 'export')
2199
+ .map(n => `${n.name}(${n.kind})`))];
2200
+ const headerNames = uniqSymbols.slice(0, budget.maxSymbolsInFileHeader);
2201
+ const omitted = uniqSymbols.length - headerNames.length;
2202
+ const wholeHeader = `#### ${filePath} — ${omitted > 0 ? `${headerNames.join(', ')}, +${omitted} more` : headerNames.join(', ')}`;
2203
+ if (!fileNecessary && totalChars + wholeSection.length + 200 > budget.maxOutputChars) {
2204
+ // Don't slice a whole file mid-method: an incidental file that doesn't
2205
+ // fit is skipped; a necessary one (below) renders in full. Half a file
2206
+ // forces the Read this is meant to prevent.
2207
+ anyFileTrimmed = true;
2208
+ continue;
2209
+ }
2210
+ lines.push(wholeHeader, '', '```' + lang, wholeSection, '```', '');
2211
+ totalChars += wholeSection.length + 200;
2212
+ filesIncluded++;
2213
+ continue;
2214
+ }
2215
+ // Cluster nearby symbols to avoid reading huge gaps between distant symbols.
2216
+ // Sort by start line, then merge overlapping/adjacent ranges (within the
2217
+ // adaptive gap threshold). Include both node ranges AND edge source
2218
+ // locations so template sections with component usages/calls are
2219
+ // covered (not just script block symbols).
2220
+ //
2221
+ // Each range carries an `importance` score so we can rank clusters
2222
+ // when the per-file budget forces us to drop some: entry-point nodes
2223
+ // are worth 10, directly-connected nodes 3, peripheral nodes 1, and
2224
+ // bare edge-source lines 2 (less than a connected node but more than
2225
+ // a peripheral one — they hint at a reference but aren't a definition).
2226
+ // Container kinds whose body can span most/all of a file. When such a
2227
+ // node covers most of the file we drop it from the ranges: keeping it
2228
+ // would merge every method inside it into one giant cluster spanning
2229
+ // the whole file, which then tail-trims down to just the container's
2230
+ // opening lines (its header/declarations) and buries the methods the
2231
+ // query actually asked about (#185 follow-up — Session.swift in
2232
+ // Alamofire is the canonical case: the `Session` class spans ~1,400
2233
+ // lines). We want the granular symbols inside, not the envelope.
2234
+ const ENVELOPE_KINDS = new Set(['file', 'module', 'class', 'struct', 'interface', 'enum', 'namespace', 'protocol', 'trait', 'component']);
2235
+ // Cluster from this file's gathered nodes PLUS any callable the agent NAMED that
2236
+ // lives here. Explore's relevance gather can miss a named method def in a huge
2237
+ // non-sibling file — Django's query.py is 3,040 lines and `_fetch_all` (L2237)
2238
+ // was gathered only as call-reference edges, never as a def, so it formed no
2239
+ // cluster and the agent Read it back. Inject named defs directly and rank them
2240
+ // ABOVE connected/glue nodes (importance 9) so their cluster wins the per-file
2241
+ // budget — the agent explicitly asked for these symbols.
2242
+ const rangeNodes = new Map();
2243
+ for (const n of group.nodes)
2244
+ if (n.startLine > 0 && n.endLine > 0)
2245
+ rangeNodes.set(n.id, n);
2246
+ for (const id of flow.namedNodeIds) {
2247
+ if (rangeNodes.has(id))
2248
+ continue;
2249
+ const n = cg.getNode(id);
2250
+ if (n && n.filePath === filePath && n.startLine > 0 && n.endLine > 0)
2251
+ rangeNodes.set(id, n);
2252
+ }
2253
+ const ranges = [...rangeNodes.values()]
2254
+ // Drop whole-file envelope nodes (containers covering >50% of the file).
2255
+ .filter(n => !(ENVELOPE_KINDS.has(n.kind) && (n.endLine - n.startLine + 1) > fileLines.length * 0.5))
2256
+ .map(n => {
2257
+ let importance = 1;
2258
+ if (entryNodeIds.has(n.id))
2259
+ importance = 10;
2260
+ else if (flow.namedNodeIds.has(n.id))
2261
+ importance = 9; // agent named it → keep its cluster
2262
+ else if (glueNodeIds.has(n.id))
2263
+ importance = 6; // bridging caller/callee of an entry
2264
+ else if (connectedToEntry.has(n.id))
2265
+ importance = 3;
2266
+ return { start: n.startLine, end: n.endLine, name: n.name, kind: n.kind, importance };
2267
+ });
2268
+ // Add edge source locations in this file — captures template references
2269
+ // (component usages, event handlers) that aren't nodes themselves.
2270
+ // Query edges directly from the DB (not just the subgraph) because BFS
2271
+ // traversal may have pruned template reference targets due to node budget.
2272
+ const edgeLines = new Set(); // dedup by "line:name"
2273
+ for (const node of group.nodes) {
2274
+ const outgoing = cg.getOutgoingEdges(node.id);
2275
+ for (const edge of outgoing) {
2276
+ if (!edge.line || edge.line <= 0 || edge.kind === 'contains')
2277
+ continue;
2278
+ const key = `${edge.line}:${edge.target}`;
2279
+ if (edgeLines.has(key))
2280
+ continue;
2281
+ edgeLines.add(key);
2282
+ // Look up target name from subgraph first, fall back to edge kind
2283
+ const targetNode = subgraph.nodes.get(edge.target);
2284
+ const targetName = targetNode?.name ?? edge.kind;
2285
+ ranges.push({ start: edge.line, end: edge.line, name: targetName, kind: edge.kind, importance: 2 });
2286
+ }
2287
+ }
2288
+ ranges.sort((a, b) => a.start - b.start);
2289
+ if (ranges.length === 0)
2290
+ continue;
2291
+ const gapThreshold = budget.gapThreshold;
2292
+ const clusters = [];
2293
+ let current = {
2294
+ start: ranges[0].start,
2295
+ end: ranges[0].end,
2296
+ symbols: [`${ranges[0].name}(${ranges[0].kind})`],
2297
+ score: ranges[0].importance,
2298
+ maxImportance: ranges[0].importance,
2299
+ };
2300
+ for (let i = 1; i < ranges.length; i++) {
2301
+ const r = ranges[i];
2302
+ if (r.start <= current.end + gapThreshold) {
2303
+ current.end = Math.max(current.end, r.end);
2304
+ current.symbols.push(`${r.name}(${r.kind})`);
2305
+ current.score += r.importance;
2306
+ current.maxImportance = Math.max(current.maxImportance, r.importance);
2307
+ }
2308
+ else {
2309
+ clusters.push(current);
2310
+ current = {
2311
+ start: r.start,
2312
+ end: r.end,
2313
+ symbols: [`${r.name}(${r.kind})`],
2314
+ score: r.importance,
2315
+ maxImportance: r.importance,
2316
+ };
2317
+ }
2318
+ }
2319
+ clusters.push(current);
2320
+ // Build file section output from clusters, capped by per-file budget.
2321
+ // The pathological case (#185): a file like Session.swift where every
2322
+ // method is adjacent collapses into one cluster spanning the whole
2323
+ // file, and dumping that into the agent's context is most of the
2324
+ // token cost on small projects. We pick clusters in priority order
2325
+ // until the per-file char cap is hit. Truly enormous single clusters
2326
+ // get tail-trimmed with a marker.
2327
+ const contextPadding = 3;
2328
+ const withLineNumbers = exploreLineNumbersEnabled();
2329
+ const buildSection = (c) => {
2330
+ const startIdx = Math.max(0, c.start - 1 - contextPadding);
2331
+ const endIdx = Math.min(fileLines.length, c.end + contextPadding);
2332
+ const slice = fileLines.slice(startIdx, endIdx).join('\n');
2333
+ // startIdx is 0-based, so the slice's first line is line startIdx + 1.
2334
+ return withLineNumbers ? numberSourceLines(slice, startIdx + 1) : slice;
2335
+ };
2336
+ // Language-neutral separator (no `//` — not a comment in Python, Ruby,
2337
+ // etc.). With line numbers on, the line-number jump also signals the gap.
2338
+ const GAP_MARKER = '\n\n... (gap) ...\n\n';
2339
+ // Rank clusters for inclusion under the per-file cap. Entry-point
2340
+ // clusters come first: a cluster containing a query entry point
2341
+ // (importance 10) must outrank a dense block of mere declarations,
2342
+ // otherwise on a large file like Session.swift the top-of-file class
2343
+ // header + property list (many adjacent low-importance nodes, high
2344
+ // density) wins the budget and buries the actual methods the query
2345
+ // asked about (perform/didCreateURLRequest/task live deep in the
2346
+ // file). Within the same importance tier, prefer density (score per
2347
+ // line) so we still favor focused clusters over sprawling ones, then
2348
+ // smaller span as a cheap-to-include tiebreak.
2349
+ const rankedClusters = clusters
2350
+ .map((c, i) => ({ idx: i, span: c.end - c.start + 1, c }))
2351
+ .sort((a, b) => {
2352
+ if (b.c.maxImportance !== a.c.maxImportance)
2353
+ return b.c.maxImportance - a.c.maxImportance;
2354
+ const densityA = a.c.score / a.span;
2355
+ const densityB = b.c.score / b.span;
2356
+ if (densityB !== densityA)
2357
+ return densityB - densityA;
2358
+ if (b.c.score !== a.c.score)
2359
+ return b.c.score - a.c.score;
2360
+ return a.span - b.span;
2361
+ });
2362
+ // Per-file budget is the SMALLER of the per-file cap and what's left of the
2363
+ // total output cap — so selection (which ranks by importance) keeps the
2364
+ // high-importance clusters and drops peripheral ones, instead of the
2365
+ // downstream source-order trim slicing off whatever comes last in the file.
2366
+ // That source-order slice is what cut Django's `_fetch_all` (L2237, importance
2367
+ // 9 — agent-named) when query.py was the last of four big files to be emitted.
2368
+ const fileBudget = Math.min(budget.maxCharsPerFile, Math.max(0, budget.maxOutputChars - totalChars - 200));
2369
+ const chosenIndices = new Set();
2370
+ let projectedChars = 0;
2371
+ for (const rc of rankedClusters) {
2372
+ const sectionLen = buildSection(rc.c).length + (chosenIndices.size > 0 ? GAP_MARKER.length : 0);
2373
+ // Always take the top-ranked cluster, even if oversize, so we don't
2374
+ // return an empty file section (agent would then re-Read the file,
2375
+ // negating the savings).
2376
+ if (chosenIndices.size === 0) {
2377
+ chosenIndices.add(rc.idx);
2378
+ projectedChars += sectionLen;
2379
+ continue;
2380
+ }
2381
+ if (projectedChars + sectionLen > fileBudget)
2382
+ continue;
2383
+ chosenIndices.add(rc.idx);
2384
+ projectedChars += sectionLen;
2385
+ }
2386
+ // Emit chosen clusters in source order so the file reads top-to-bottom.
2387
+ let fileSection = '';
2388
+ const allSymbols = [];
2389
+ for (let i = 0; i < clusters.length; i++) {
2390
+ if (!chosenIndices.has(i))
2391
+ continue;
2392
+ const cluster = clusters[i];
2393
+ const section = buildSection(cluster);
2394
+ if (fileSection.length > 0)
2395
+ fileSection += GAP_MARKER;
2396
+ fileSection += section;
2397
+ allSymbols.push(...cluster.symbols);
2398
+ }
2399
+ // A chosen cluster is a COMPLETE method-range — we never cut through a body.
2400
+ // An oversize single cluster (a long monolithic function) renders in FULL:
2401
+ // half a method is useless (the agent just Reads the rest for the other half),
2402
+ // which is the very fallback explore exists to prevent. A pathological file is
2403
+ // bounded by the per-file cluster SELECTION above + the total hard ceiling.
2404
+ if (chosenIndices.size < clusters.length) {
2405
+ anyFileTrimmed = true;
2406
+ }
2407
+ // Dedupe + cap the symbols list shown in the per-file header. Some
2408
+ // files (Session.swift in Alamofire) produced 3.4KB symbol lists
2409
+ // from cluster scoring + edge-source lines, dwarfing the per-file
2410
+ // body cap. Show top names by frequency, with a "+N more" tail.
2411
+ const symbolCounts = new Map();
2412
+ for (const s of allSymbols) {
2413
+ symbolCounts.set(s, (symbolCounts.get(s) ?? 0) + 1);
2414
+ }
2415
+ const sortedSymbols = [...symbolCounts.entries()]
2416
+ .sort((a, b) => b[1] - a[1])
2417
+ .map(([name]) => name);
2418
+ const headerCap = budget.maxSymbolsInFileHeader;
2419
+ const headerSymbols = sortedSymbols.slice(0, headerCap);
2420
+ const omittedCount = sortedSymbols.length - headerSymbols.length;
2421
+ const headerSuffix = omittedCount > 0
2422
+ ? `${headerSymbols.join(', ')}, +${omittedCount} more`
2423
+ : headerSymbols.join(', ');
2424
+ const fileHeader = `#### ${filePath} — ${headerSuffix}`;
2425
+ // The total cap bounds INCIDENTAL files only. A file that DEFINES a symbol
2426
+ // the agent named (or that's on the flow spine) renders even when the
2427
+ // nominal total is used up — it's the answer, and the set is bounded by
2428
+ // maxFiles AND by true-spine/named-seeding having already trimmed each file
2429
+ // to its necessary content. A file that merely REFERENCES the flow
2430
+ // (Combine.swift name-drops request/task) is incidental → still capped, so
2431
+ // freed budget never leaks into noise. This is the last god-file layer:
2432
+ // build (Session, true-spined) + validators-exec (Request) + validate
2433
+ // (DataRequest/Validation) all render, instead of the cap dropping whichever
2434
+ // phase the file order happened to put last.
2435
+ if (!fileNecessary && totalChars + fileSection.length + 200 > budget.maxOutputChars) {
2436
+ // Incidental file that doesn't fit: SKIP it whole — never slice mid-method.
2437
+ // Keep scanning for necessary files (which bypass this cap and render in
2438
+ // full, bounded by the hard ceiling).
2439
+ anyFileTrimmed = true;
2440
+ continue;
2441
+ }
2442
+ lines.push(fileHeader);
2443
+ lines.push('');
2444
+ lines.push('```' + lang);
2445
+ lines.push(fileSection);
2446
+ lines.push('```');
2447
+ lines.push('');
2448
+ totalChars += fileSection.length + 200;
2449
+ filesIncluded++;
2450
+ }
2451
+ // Add remaining files as references (from both relevant and peripheral files).
2452
+ // Small projects (per budget) skip this — the relevant story already fits
2453
+ // in the source section, and a trailing pointer list is pure overhead.
2454
+ if (budget.includeAdditionalFiles) {
2455
+ const remainingRelevant = sortedFiles.slice(filesIncluded);
2456
+ const peripheralFiles = [...fileGroups.entries()]
2457
+ .filter(([, group]) => group.score < 3)
2458
+ .sort((a, b) => b[1].score - a[1].score);
2459
+ const remainingFiles = [...remainingRelevant, ...peripheralFiles];
2460
+ if (remainingFiles.length > 0) {
2461
+ lines.push('### Not shown above — explore these names for their source');
2462
+ lines.push('');
2463
+ for (const [filePath, group] of remainingFiles.slice(0, 10)) {
2464
+ const symbols = group.nodes.map(n => `${n.name}:${n.startLine}`).join(', ');
2465
+ lines.push(`- ${filePath}: ${symbols}`);
2466
+ }
2467
+ if (remainingFiles.length > 10) {
2468
+ lines.push(`- ... and ${remainingFiles.length - 10} more files`);
2469
+ }
2470
+ }
2471
+ }
2472
+ // Add completeness signal so agents know they don't need to re-read these files.
2473
+ // On small projects the budget gates this off — but if we actually had to
2474
+ // trim or drop clusters, surface a brief note so the agent knows it can
2475
+ // still Read for more detail.
2476
+ if (budget.includeCompletenessSignal) {
2477
+ lines.push('');
2478
+ lines.push('---');
2479
+ 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 specship_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.`);
2480
+ }
2481
+ else if (anyFileTrimmed) {
2482
+ lines.push('');
2483
+ lines.push(`> Some file sections were trimmed for size. For a specific symbol you still need, run another \`specship_explore\` (or \`specship_node\`) with its exact name — line-numbered source, cheaper and more complete than Read.`);
2484
+ }
2485
+ // Add explore budget note based on project size
2486
+ if (budget.includeBudgetNote) {
2487
+ try {
2488
+ const stats = cg.getStats();
2489
+ const callBudget = getExploreBudget(stats.fileCount);
2490
+ lines.push('');
2491
+ 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}.`);
2492
+ }
2493
+ catch {
2494
+ // Stats unavailable — skip budget note
2495
+ }
2496
+ }
2497
+ // Final ceiling — an ABSOLUTE inline cap, not a multiple of the budget. The
2498
+ // render loop renders necessary (named/spine) files even a bit past
2499
+ // maxOutputChars and caps only incidental ones, so this is the last safety.
2500
+ // It MUST stay under the host's inline tool-result limit (~25K chars): above
2501
+ // that the result is externalized to a file the agent Reads back (a 35K
2502
+ // vscode explore did exactly this in the n=4 A/B). So allow a little
2503
+ // necessary overflow above the 24K budget, but hard-stop at 25K — never into
2504
+ // externalize territory.
2505
+ const output = flow.text + lines.join('\n');
2506
+ const hardCeiling = Math.min(Math.round(budget.maxOutputChars * 1.5), 25000);
2507
+ if (output.length > hardCeiling) {
2508
+ // Cut at a FILE-SECTION boundary (the last `#### ` header before the
2509
+ // ceiling) so we drop whole trailing file-sections rather than slicing
2510
+ // through a method body — a half-rendered method just forces the Read this
2511
+ // tool exists to prevent. Fall back to a line boundary only if no section
2512
+ // header sits in the back half (degenerate single-giant-section case).
2513
+ const cut = output.slice(0, hardCeiling);
2514
+ const lastSection = cut.lastIndexOf('\n#### ');
2515
+ const boundary = lastSection > hardCeiling * 0.5 ? lastSection : cut.lastIndexOf('\n');
2516
+ const safe = boundary > 0 ? cut.slice(0, boundary) : cut;
2517
+ 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 specship_explore with the specific names — do NOT Read these files.)');
2518
+ }
2519
+ return this.textResult(output);
2520
+ }
2521
+ /**
2522
+ * Handle specship_node
2523
+ */
2524
+ async handleNode(args) {
2525
+ const symbol = this.validateString(args.symbol, 'symbol');
2526
+ if (typeof symbol !== 'string')
2527
+ return symbol;
2528
+ const cg = this.getSpecShip(args.projectPath);
2529
+ // Default to false to minimize context usage
2530
+ const includeCode = args.includeCode === true;
2531
+ const fileHint = typeof args.file === 'string' && args.file.trim() ? args.file.trim() : undefined;
2532
+ const lineHint = typeof args.line === 'number' && args.line > 0 ? args.line : undefined;
2533
+ let matches = this.findSymbolMatches(cg, symbol);
2534
+ if (matches.length === 0) {
2535
+ return this.textResult(`Symbol "${symbol}" not found in the codebase`);
2536
+ }
2537
+ // Disambiguate a heavily-overloaded name to a specific definition the caller
2538
+ // pinned by file/line (the `file:line` a trail or another tool showed it) —
2539
+ // so it can fetch e.g. `Harness::poll` at harness.rs:153 out of 50+ `poll`s
2540
+ // instead of Reading. file matches by path suffix/substring; line prefers the
2541
+ // def whose body contains it, else the nearest start. Only narrows (never
2542
+ // empties — if a hint matches nothing it's ignored).
2543
+ if (matches.length > 1 && (fileHint || lineHint !== undefined)) {
2544
+ const norm = (p) => p.replace(/\\/g, '/').toLowerCase();
2545
+ let narrowed = matches;
2546
+ if (fileHint) {
2547
+ const fh = norm(fileHint);
2548
+ const byFile = narrowed.filter((n) => norm(n.filePath).endsWith(fh) || norm(n.filePath).includes(fh));
2549
+ if (byFile.length > 0)
2550
+ narrowed = byFile;
2551
+ }
2552
+ if (lineHint !== undefined && narrowed.length > 1) {
2553
+ const containing = narrowed.filter((n) => n.startLine <= lineHint && (n.endLine ?? n.startLine) >= lineHint);
2554
+ narrowed = containing.length > 0
2555
+ ? containing
2556
+ : [...narrowed].sort((a, b) => Math.abs(a.startLine - lineHint) - Math.abs(b.startLine - lineHint)).slice(0, 1);
2557
+ }
2558
+ if (narrowed.length > 0)
2559
+ matches = narrowed;
2560
+ }
2561
+ // Single definition — the common case.
2562
+ if (matches.length === 1) {
2563
+ return this.textResult(this.truncateOutput(await this.renderNodeSection(cg, matches[0], includeCode)));
2564
+ }
2565
+ // Multiple definitions share this name — overloads, or same-named methods on
2566
+ // different types (Alamofire `didCompleteTask`/`task`/`validate`, gin
2567
+ // `reset`). Returning ONE forces the agent to guess, and when it guesses
2568
+ // wrong it READS the file to find the right overload — the dominant
2569
+ // specship_node read cause on Swift/Go. So return them ALL: pack as many
2570
+ // FULL bodies as fit a char budget (the agent gets the one it needs in this
2571
+ // one call, no follow-up parameter to learn), and list any remainder by
2572
+ // file:line so a large overload set can't overflow the per-tool cap.
2573
+ const header = `**${matches.length} definitions named "${symbol}"**`;
2574
+ if (!includeCode) {
2575
+ const list = matches.map((n) => `- \`${n.name}\` (${n.kind}) — ${n.filePath}:${n.startLine}`);
2576
+ 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')));
2577
+ }
2578
+ const BODY_BUDGET = 12000; // leaves room under MAX_OUTPUT_LENGTH for the header + list
2579
+ // The CHAR budget is the real limiter — keep the count cap high so a set of
2580
+ // SHORT overloads (Alamofire's 10 `validate` variants, each a few lines) all
2581
+ // render in full rather than relegating the one the agent wanted to a
2582
+ // bodiless list. Only a set of many LARGE bodies hits the char budget first.
2583
+ const HARD_CAP = 16;
2584
+ const rendered = [];
2585
+ const listed = [];
2586
+ let used = 0;
2587
+ for (const n of matches) {
2588
+ if (rendered.length >= HARD_CAP) {
2589
+ listed.push(n);
2590
+ continue;
2591
+ }
2592
+ const section = await this.renderNodeSection(cg, n, true);
2593
+ // Always emit the first; emit the rest only while within the char budget.
2594
+ if (rendered.length === 0 || used + section.length <= BODY_BUDGET) {
2595
+ rendered.push(section);
2596
+ used += section.length;
2597
+ }
2598
+ else {
2599
+ listed.push(n);
2600
+ }
2601
+ }
2602
+ const out = [
2603
+ header,
2604
+ `Returning ${rendered.length} in full${listed.length ? `; ${listed.length} more listed below` : ''} — pick the one you need (no Read required).`,
2605
+ '',
2606
+ rendered.join('\n\n---\n\n'),
2607
+ ];
2608
+ if (listed.length) {
2609
+ const LIST_CAP = 20;
2610
+ const shownList = listed.slice(0, LIST_CAP);
2611
+ out.push('', '### Other definitions', ...shownList.map((n) => `- \`${n.name}\` (${n.kind}) — ${n.filePath}:${n.startLine}`));
2612
+ if (listed.length > LIST_CAP)
2613
+ out.push(`- … +${listed.length - LIST_CAP} more`);
2614
+ out.push('', `> Need one of these in full? Call specship_node again with \`file\` (e.g. \`"${listed[0].filePath.split('/').pop()}"\`) or \`line\` — do NOT Read it.`);
2615
+ }
2616
+ return this.textResult(this.truncateOutput(out.join('\n')));
2617
+ }
2618
+ /** Render one symbol: details + (optional) body/outline + its caller/callee trail. */
2619
+ async renderNodeSection(cg, node, includeCode) {
2620
+ let code = null;
2621
+ let outline = null;
2622
+ if (includeCode) {
2623
+ // For container symbols (class/interface/struct/…), the full body is the
2624
+ // sum of every method body — a wall of source. Return a structural outline
2625
+ // (members + signatures + line numbers) instead; leaf symbols return their
2626
+ // full body.
2627
+ if (CONTAINER_NODE_KINDS.has(node.kind)) {
2628
+ outline = this.buildContainerOutline(cg, node);
2629
+ }
2630
+ if (!outline) {
2631
+ code = await cg.getCode(node.id);
2632
+ }
2633
+ }
2634
+ return (this.formatNodeDetails(node, code, outline) +
2635
+ this.formatTrail(cg, node) +
2636
+ (0, spec_tools_1.renderLinkedSpecsForNode)(cg, node.id));
2637
+ }
2638
+ /**
2639
+ * Build the "trail" for a symbol: its direct callees (what it calls) and
2640
+ * callers (what calls it), each with file:line — so specship_node doubles as
2641
+ * the structural Grep→Read→expand primitive: a spot PLUS where to go next.
2642
+ * Capped to stay cheap. Walk the graph by calling specship_node on a trail
2643
+ * entry; no Read needed for covered hops. Empty edges on a non-leaf often mean
2644
+ * dynamic dispatch the static graph couldn't resolve — that absence is itself
2645
+ * a signal (read that one hop) rather than a dead end.
2646
+ */
2647
+ formatTrail(cg, node) {
2648
+ const TRAIL_CAP = 12;
2649
+ const fmt = (e) => {
2650
+ const base = `${e.node.name} (${e.node.filePath}:${e.node.startLine})`;
2651
+ const synth = this.synthEdgeNote(e.edge);
2652
+ return synth ? `${base} [${synth.compact}]` : base;
2653
+ };
2654
+ const collect = (edges) => {
2655
+ const seen = new Set([node.id]);
2656
+ const out = [];
2657
+ for (const e of edges) {
2658
+ if (seen.has(e.node.id))
2659
+ continue;
2660
+ seen.add(e.node.id);
2661
+ out.push(e);
2662
+ }
2663
+ return out;
2664
+ };
2665
+ const callees = collect(cg.getCallees(node.id));
2666
+ const callers = collect(cg.getCallers(node.id));
2667
+ if (callees.length === 0 && callers.length === 0)
2668
+ return '';
2669
+ const lines = ['', '### Trail — specship_node any of these to follow it (no Read needed)'];
2670
+ if (callees.length > 0) {
2671
+ lines.push(`**Calls →** ${callees.slice(0, TRAIL_CAP).map(fmt).join(', ')}${callees.length > TRAIL_CAP ? `, +${callees.length - TRAIL_CAP} more` : ''}`);
2672
+ }
2673
+ if (callers.length > 0) {
2674
+ lines.push(`**Called by ←** ${callers.slice(0, TRAIL_CAP).map(fmt).join(', ')}${callers.length > TRAIL_CAP ? `, +${callers.length - TRAIL_CAP} more` : ''}`);
2675
+ }
2676
+ return lines.join('\n');
2677
+ }
2678
+ /**
2679
+ * Handle specship_status
2680
+ */
2681
+ async handleStatus(args) {
2682
+ let cg = this.getSpecShip(args.projectPath);
2683
+ // Same trick as withStalenessNotice — when an explicit projectPath
2684
+ // resolves to the same project as the default session cg, prefer the
2685
+ // default so getPendingFiles() (only populated by the default's watcher)
2686
+ // is non-empty when there are pending edits.
2687
+ if (this.cg && cg !== this.cg) {
2688
+ try {
2689
+ if ((0, path_1.resolve)(this.cg.getProjectRoot()) === (0, path_1.resolve)(cg.getProjectRoot())) {
2690
+ cg = this.cg;
2691
+ }
2692
+ }
2693
+ catch { /* closed instance — leave as is */ }
2694
+ }
2695
+ const stats = cg.getStats();
2696
+ // Warn when this index actually belongs to a different git working tree
2697
+ // (e.g. the server resolved up from a nested worktree to the main checkout).
2698
+ // Queries then reflect that tree's branch, not the worktree being edited.
2699
+ // status shows the verbose, multi-line form; the read tools get the compact
2700
+ // one-liner via withWorktreeNotice. Both share the cached detection.
2701
+ const mismatch = this.worktreeMismatchFor(args.projectPath);
2702
+ const lines = [
2703
+ '## SpecShip Status',
2704
+ '',
2705
+ ];
2706
+ if (mismatch) {
2707
+ lines.push(`> ⚠ ${(0, worktree_1.worktreeMismatchWarning)(mismatch).replace(/\n/g, '\n> ')}`, '');
2708
+ }
2709
+ lines.push(`**Files indexed:** ${stats.fileCount}`, `**Total nodes:** ${stats.nodeCount}`, `**Total edges:** ${stats.edgeCount}`, `**Database size:** ${(stats.dbSizeBytes / 1024 / 1024).toFixed(2)} MB`);
2710
+ // Surface the active SQLite backend (node:sqlite, Node's built-in real
2711
+ // SQLite — full WAL + FTS5, no native build).
2712
+ lines.push(`**Backend:** node:sqlite (Node built-in) — full WAL + FTS5`);
2713
+ // Effective journal mode. 'wal' ⇒ concurrent reads never block on a writer;
2714
+ // anything else ⇒ they can ("database is locked"). node:sqlite supports WAL
2715
+ // everywhere, so a non-wal mode means the filesystem can't (network/
2716
+ // virtualized mounts, WSL2 /mnt). See issue #238.
2717
+ const journalMode = cg.getJournalMode();
2718
+ if (journalMode === 'wal') {
2719
+ lines.push(`**Journal mode:** wal (concurrent reads safe)`);
2720
+ }
2721
+ else {
2722
+ lines.push(`**Journal mode:** ⚠ ${journalMode || 'unknown'} — WAL not active, so reads ` +
2723
+ `can block on a concurrent write (WAL appears unsupported on this filesystem)`);
2724
+ }
2725
+ lines.push('', '### Nodes by Kind:');
2726
+ for (const [kind, count] of Object.entries(stats.nodesByKind)) {
2727
+ if (count > 0) {
2728
+ lines.push(`- ${kind}: ${count}`);
2729
+ }
2730
+ }
2731
+ lines.push('', '### Languages:');
2732
+ for (const [lang, count] of Object.entries(stats.filesByLanguage)) {
2733
+ if (count > 0) {
2734
+ lines.push(`- ${lang}: ${count}`);
2735
+ }
2736
+ }
2737
+ // Per-file freshness — the inverse of the auto-prepended staleness banner
2738
+ // (issue #403). Surfacing it inside `status` gives the agent a single
2739
+ // place to ask "is the index caught up?" rather than inferring from
2740
+ // banners on other tool calls.
2741
+ const pending = cg.getPendingFiles();
2742
+ if (pending.length > 0) {
2743
+ lines.push('', '### Pending sync:');
2744
+ const now = Date.now();
2745
+ for (const p of pending) {
2746
+ const ageMs = Math.max(0, now - p.lastSeenMs);
2747
+ const label = p.indexing ? 'indexing in progress' : 'pending sync';
2748
+ lines.push(`- ${p.path} (edited ${ageMs}ms ago, ${label})`);
2749
+ }
2750
+ }
2751
+ return this.textResult(lines.join('\n'));
2752
+ }
2753
+ /**
2754
+ * Handle specship_files - get project file structure from the index
2755
+ */
2756
+ async handleFiles(args) {
2757
+ const cg = this.getSpecShip(args.projectPath);
2758
+ const pathFilter = args.path;
2759
+ const pattern = args.pattern;
2760
+ const format = args.format || 'tree';
2761
+ const includeMetadata = args.includeMetadata !== false;
2762
+ const maxDepth = args.maxDepth != null ? (0, utils_1.clamp)(args.maxDepth, 1, 20) : undefined;
2763
+ // Get all files from the index
2764
+ const allFiles = cg.getFiles();
2765
+ if (allFiles.length === 0) {
2766
+ return this.textResult('No files indexed. Run `specship index` first.');
2767
+ }
2768
+ // Filter by path prefix. Stored paths are project-relative POSIX (e.g.
2769
+ // "src/foo.ts"), but agents commonly pass project-root variants like "/",
2770
+ // ".", "./", "" or Windows-style "src\foo" — and prefixes with leading
2771
+ // "/", "./" or "\". Normalize all of those before matching so the agent
2772
+ // gets results instead of falling back to Read/Glob (see #426).
2773
+ const normalizedFilter = pathFilter
2774
+ ? pathFilter
2775
+ .replace(/\\/g, '/')
2776
+ .replace(/^(?:\.?\/+)+/, '')
2777
+ .replace(/^\.$/, '')
2778
+ .replace(/\/+$/, '')
2779
+ : '';
2780
+ let files = normalizedFilter
2781
+ ? allFiles.filter(f => f.path === normalizedFilter || f.path.startsWith(normalizedFilter + '/'))
2782
+ : allFiles;
2783
+ // Filter by glob pattern
2784
+ if (pattern) {
2785
+ const regex = this.globToRegex(pattern);
2786
+ files = files.filter(f => regex.test(f.path));
2787
+ }
2788
+ if (files.length === 0) {
2789
+ return this.textResult(`No files found matching the criteria.`);
2790
+ }
2791
+ // Format output
2792
+ let output;
2793
+ switch (format) {
2794
+ case 'flat':
2795
+ output = this.formatFilesFlat(files, includeMetadata);
2796
+ break;
2797
+ case 'grouped':
2798
+ output = this.formatFilesGrouped(files, includeMetadata);
2799
+ break;
2800
+ case 'tree':
2801
+ default:
2802
+ output = this.formatFilesTree(files, includeMetadata, maxDepth);
2803
+ break;
2804
+ }
2805
+ return this.textResult(this.truncateOutput(output));
2806
+ }
2807
+ /**
2808
+ * Convert glob pattern to regex
2809
+ */
2810
+ globToRegex(pattern) {
2811
+ const escaped = pattern
2812
+ .replace(/[.+^${}()|[\]\\]/g, '\\$&') // Escape special regex chars except * and ?
2813
+ .replace(/\*\*/g, '{{GLOBSTAR}}') // Temp placeholder for **
2814
+ .replace(/\*/g, '[^/]*') // * matches anything except /
2815
+ .replace(/\?/g, '[^/]') // ? matches single char except /
2816
+ .replace(/\{\{GLOBSTAR\}\}/g, '.*'); // ** matches anything including /
2817
+ return new RegExp(escaped);
2818
+ }
2819
+ /**
2820
+ * Format files as a flat list
2821
+ */
2822
+ formatFilesFlat(files, includeMetadata) {
2823
+ const lines = [`## Files (${files.length})`, ''];
2824
+ for (const file of files.sort((a, b) => a.path.localeCompare(b.path))) {
2825
+ if (includeMetadata) {
2826
+ lines.push(`- ${file.path} (${file.language}, ${file.nodeCount} symbols)`);
2827
+ }
2828
+ else {
2829
+ lines.push(`- ${file.path}`);
2830
+ }
2831
+ }
2832
+ return lines.join('\n');
2833
+ }
2834
+ /**
2835
+ * Format files grouped by language
2836
+ */
2837
+ formatFilesGrouped(files, includeMetadata) {
2838
+ const byLang = new Map();
2839
+ for (const file of files) {
2840
+ const existing = byLang.get(file.language) || [];
2841
+ existing.push(file);
2842
+ byLang.set(file.language, existing);
2843
+ }
2844
+ const lines = [`## Files by Language (${files.length} total)`, ''];
2845
+ // Sort languages by file count (descending)
2846
+ const sortedLangs = [...byLang.entries()].sort((a, b) => b[1].length - a[1].length);
2847
+ for (const [lang, langFiles] of sortedLangs) {
2848
+ lines.push(`### ${lang} (${langFiles.length})`);
2849
+ for (const file of langFiles.sort((a, b) => a.path.localeCompare(b.path))) {
2850
+ if (includeMetadata) {
2851
+ lines.push(`- ${file.path} (${file.nodeCount} symbols)`);
2852
+ }
2853
+ else {
2854
+ lines.push(`- ${file.path}`);
2855
+ }
2856
+ }
2857
+ lines.push('');
2858
+ }
2859
+ return lines.join('\n');
2860
+ }
2861
+ /**
2862
+ * Format files as a tree structure
2863
+ */
2864
+ formatFilesTree(files, includeMetadata, maxDepth) {
2865
+ const root = { name: '', children: new Map() };
2866
+ for (const file of files) {
2867
+ const parts = file.path.split('/');
2868
+ let current = root;
2869
+ for (let i = 0; i < parts.length; i++) {
2870
+ const part = parts[i];
2871
+ if (!part)
2872
+ continue;
2873
+ if (!current.children.has(part)) {
2874
+ current.children.set(part, { name: part, children: new Map() });
2875
+ }
2876
+ current = current.children.get(part);
2877
+ // If this is the last part, it's a file
2878
+ if (i === parts.length - 1) {
2879
+ current.file = { language: file.language, nodeCount: file.nodeCount };
2880
+ }
2881
+ }
2882
+ }
2883
+ // Render tree
2884
+ const lines = [`## Project Structure (${files.length} files)`, ''];
2885
+ const renderNode = (node, prefix, isLast, depth) => {
2886
+ if (maxDepth !== undefined && depth > maxDepth)
2887
+ return;
2888
+ const connector = isLast ? '└── ' : '├── ';
2889
+ const childPrefix = isLast ? ' ' : '│ ';
2890
+ if (node.name) {
2891
+ let line = prefix + connector + node.name;
2892
+ if (node.file && includeMetadata) {
2893
+ line += ` (${node.file.language}, ${node.file.nodeCount} symbols)`;
2894
+ }
2895
+ lines.push(line);
2896
+ }
2897
+ const children = [...node.children.values()];
2898
+ // Sort: directories first, then files, both alphabetically
2899
+ children.sort((a, b) => {
2900
+ const aIsDir = a.children.size > 0 && !a.file;
2901
+ const bIsDir = b.children.size > 0 && !b.file;
2902
+ if (aIsDir !== bIsDir)
2903
+ return aIsDir ? -1 : 1;
2904
+ return a.name.localeCompare(b.name);
2905
+ });
2906
+ for (let i = 0; i < children.length; i++) {
2907
+ const child = children[i];
2908
+ const nextPrefix = node.name ? prefix + childPrefix : prefix;
2909
+ renderNode(child, nextPrefix, i === children.length - 1, depth + 1);
2910
+ }
2911
+ };
2912
+ renderNode(root, '', true, 0);
2913
+ return lines.join('\n');
2914
+ }
2915
+ // =========================================================================
2916
+ // Symbol resolution helpers
2917
+ // =========================================================================
2918
+ /**
2919
+ * Find a symbol by name, handling disambiguation when multiple matches exist.
2920
+ * Returns the best match and a note about alternatives if any.
2921
+ */
2922
+ /**
2923
+ * Check if a node matches a symbol query.
2924
+ *
2925
+ * Accepts simple names (`run`) and three flavors of qualifier:
2926
+ * - dotted `Session.request` (TS/JS/Python)
2927
+ * - colon-pair `stage_apply::run` (Rust, C++, Ruby)
2928
+ * - slash `configurator/stage_apply` (path-ish)
2929
+ *
2930
+ * Multi-level qualifiers compose: `crate::configurator::stage_apply::run`
2931
+ * works. Rust path prefixes (`crate`, `super`, `self`) are stripped so
2932
+ * the canonical `crate::module::symbol` form resolves.
2933
+ *
2934
+ * Resolution order, last part must always equal `node.name`:
2935
+ * 1. Suffix-match against `qualifiedName` (handles class-scoped methods
2936
+ * where the extractor builds the qualified name from the AST stack)
2937
+ * 2. File-path containment (handles file-derived modules in Rust/
2938
+ * Python — `stage_apply::run` matches a `run` in `stage_apply.rs`)
2939
+ */
2940
+ matchesSymbol(node, symbol) {
2941
+ // Simple name match
2942
+ if (node.name === symbol)
2943
+ return true;
2944
+ // File basename match (e.g., "product-card" matches "product-card.liquid")
2945
+ if (node.kind === 'file' && node.name.replace(/\.[^.]+$/, '') === symbol)
2946
+ return true;
2947
+ // Qualified-name lookups: split on any supported separator. `\w` keeps
2948
+ // identifier chars (incl. `_`) intact; everything else is treated as
2949
+ // a separator we tolerate.
2950
+ if (!/[.\/]|::/.test(symbol))
2951
+ return false;
2952
+ const parts = symbol.split(/::|[./]/).filter((p) => p.length > 0);
2953
+ if (parts.length < 2)
2954
+ return false;
2955
+ const lastPart = parts[parts.length - 1];
2956
+ if (node.name !== lastPart)
2957
+ return false;
2958
+ // Stage 1: qualified-name suffix match. The extractor joins the
2959
+ // semantic hierarchy with `::`, so `Session.request` and
2960
+ // `Session::request` both become `Session::request` here.
2961
+ const colonSuffix = parts.join('::');
2962
+ if (node.qualifiedName.includes(colonSuffix))
2963
+ return true;
2964
+ // Stage 2: file-path containment. Rust modules and Python packages
2965
+ // are not in `qualifiedName` — they're encoded in the file path. So
2966
+ // `stage_apply::run` matches a `run` in any file whose path
2967
+ // contains a `stage_apply` segment (with or without an extension).
2968
+ //
2969
+ // Filter out Rust path prefixes that have no file-system equivalent.
2970
+ const containerHints = parts.slice(0, -1).filter((p) => !RUST_PATH_PREFIXES.has(p));
2971
+ if (containerHints.length === 0)
2972
+ return false;
2973
+ const segments = node.filePath.split('/').filter((s) => s.length > 0);
2974
+ return containerHints.every((hint) => segments.some((seg) => seg === hint || seg.replace(/\.[^.]+$/, '') === hint));
2975
+ }
2976
+ /**
2977
+ * Find ALL definitions matching a name, ranked, so specship_node can return
2978
+ * every overload instead of guessing one (the wrong guess → a Read). Keepers
2979
+ * rank before generated stubs (.pb.go etc.); stable within a group preserves
2980
+ * FTS order. Returns [] when nothing matches; a qualified lookup that finds no
2981
+ * exact match returns [] rather than a misleading fuzzy file hit (#173); a
2982
+ * bare name with no exact match falls back to the single top fuzzy result.
2983
+ */
2984
+ findSymbolMatches(cg, symbol) {
2985
+ const isQualified = /[.\/]|::/.test(symbol);
2986
+ // For a bare name, enumerate EVERY exact-name definition via the direct index
2987
+ // (not FTS, which caps + ranks): tokio's `poll` has 50+ defs and the one the
2988
+ // caller wants (`Harness::poll` at harness.rs:153) ranks below any search cut,
2989
+ // so it could be neither rendered nor pinned by the file/line disambiguator —
2990
+ // and the agent Read it. With the full set, the multi-overload render + the
2991
+ // file/line filter can both reach it.
2992
+ if (!isQualified) {
2993
+ const exact = cg.getNodesByName(symbol);
2994
+ if (exact.length > 0) {
2995
+ return [...exact].sort((a, b) => ((0, generated_detection_1.isGeneratedFile)(a.filePath) ? 1 : 0) - ((0, generated_detection_1.isGeneratedFile)(b.filePath) ? 1 : 0));
2996
+ }
2997
+ // No exact match — use the single top fuzzy result (e.g. a file basename).
2998
+ const fuzzy = cg.searchNodes(symbol, { limit: 10 });
2999
+ return fuzzy[0] ? [fuzzy[0].node] : [];
3000
+ }
3001
+ // Qualified lookup (`Session.request`, `stage_apply::run`): FTS + matchesSymbol.
3002
+ const limit = 50;
3003
+ let results = cg.searchNodes(symbol, { limit });
3004
+ // FTS strips colons, so `stage_apply::run` searches the literal
3005
+ // `stage_applyrun` and finds nothing. Re-search by the bare last part and
3006
+ // let `matchesSymbol` filter by qualifier.
3007
+ if (isQualified && results.length === 0) {
3008
+ const tail = lastQualifierPart(symbol);
3009
+ if (tail && tail !== symbol)
3010
+ results = cg.searchNodes(tail, { limit });
3011
+ }
3012
+ if (results.length === 0)
3013
+ return [];
3014
+ const exactMatches = results.filter((r) => this.matchesSymbol(r.node, symbol));
3015
+ if (exactMatches.length === 0) {
3016
+ // No exact match — a qualified lookup must not fall back to a fuzzy file
3017
+ // hit (#173); a bare name may use the single top fuzzy result.
3018
+ return isQualified ? [] : results[0] ? [results[0].node] : [];
3019
+ }
3020
+ // Down-rank generated files (.pb.go, .pulsar.go, _grpc.pb.go, …) so a flow
3021
+ // query prefers the keeper implementation over the protobuf-generated stub.
3022
+ return [...exactMatches]
3023
+ .sort((a, b) => ((0, generated_detection_1.isGeneratedFile)(a.node.filePath) ? 1 : 0) - ((0, generated_detection_1.isGeneratedFile)(b.node.filePath) ? 1 : 0))
3024
+ .map((r) => r.node);
3025
+ }
3026
+ /**
3027
+ * Find ALL symbols matching a name. Used by callers/callees/impact to aggregate
3028
+ * results across all matching symbols (e.g., multiple classes with an `execute` method).
3029
+ */
3030
+ findAllSymbols(cg, symbol) {
3031
+ let results = cg.searchNodes(symbol, { limit: 50 });
3032
+ // Mirror the fallback in `findSymbol` for qualified queries — FTS
3033
+ // strips colons, so a module-qualified lookup needs a second pass
3034
+ // by the bare last part.
3035
+ if (results.length === 0 && /[.\/]|::/.test(symbol)) {
3036
+ const tail = lastQualifierPart(symbol);
3037
+ if (tail && tail !== symbol)
3038
+ results = cg.searchNodes(tail, { limit: 50 });
3039
+ }
3040
+ if (results.length === 0) {
3041
+ return { nodes: [], note: '' };
3042
+ }
3043
+ const exactMatches = results.filter(r => this.matchesSymbol(r.node, symbol));
3044
+ if (exactMatches.length <= 1) {
3045
+ const node = exactMatches[0]?.node ?? results[0].node;
3046
+ return { nodes: [node], note: '' };
3047
+ }
3048
+ // Same generated-file down-rank as findSymbol — keeps callers/callees
3049
+ // /impact aggregation aligned (a query against "Send" returns the
3050
+ // hand-written implementations before the protobuf scaffold).
3051
+ const ranked = [...exactMatches].sort((a, b) => {
3052
+ const aGen = (0, generated_detection_1.isGeneratedFile)(a.node.filePath) ? 1 : 0;
3053
+ const bGen = (0, generated_detection_1.isGeneratedFile)(b.node.filePath) ? 1 : 0;
3054
+ return aGen - bGen;
3055
+ });
3056
+ const locations = ranked.map(r => `${r.node.kind} at ${r.node.filePath}:${r.node.startLine}`);
3057
+ const note = `\n\n> **Note:** Aggregated results across ${ranked.length} symbols named "${symbol}": ${locations.join(', ')}`;
3058
+ return { nodes: ranked.map(r => r.node), note };
3059
+ }
3060
+ /**
3061
+ * Truncate output if it exceeds the maximum length
3062
+ */
3063
+ truncateOutput(text) {
3064
+ if (text.length <= MAX_OUTPUT_LENGTH)
3065
+ return text;
3066
+ const truncated = text.slice(0, MAX_OUTPUT_LENGTH);
3067
+ const lastNewline = truncated.lastIndexOf('\n');
3068
+ const cutPoint = lastNewline > MAX_OUTPUT_LENGTH * 0.8 ? lastNewline : MAX_OUTPUT_LENGTH;
3069
+ return truncated.slice(0, cutPoint) + '\n\n... (output truncated)';
3070
+ }
3071
+ // =========================================================================
3072
+ // Formatting helpers (compact by default to reduce context usage)
3073
+ // =========================================================================
3074
+ formatSearchResults(results) {
3075
+ const lines = [`## Search Results (${results.length} found)`, ''];
3076
+ for (const result of results) {
3077
+ const { node } = result;
3078
+ const location = node.startLine ? `:${node.startLine}` : '';
3079
+ // Compact format: one line per result with key info
3080
+ lines.push(`### ${node.name} (${node.kind})`);
3081
+ lines.push(`${node.filePath}${location}`);
3082
+ if (node.signature)
3083
+ lines.push(`\`${node.signature}\``);
3084
+ lines.push('');
3085
+ }
3086
+ return lines.join('\n');
3087
+ }
3088
+ formatNodeList(nodes, title) {
3089
+ const lines = [`## ${title} (${nodes.length} found)`, ''];
3090
+ for (const node of nodes) {
3091
+ const location = node.startLine ? `:${node.startLine}` : '';
3092
+ // Compact: just name, kind, location
3093
+ lines.push(`- ${node.name} (${node.kind}) - ${node.filePath}${location}`);
3094
+ }
3095
+ return lines.join('\n');
3096
+ }
3097
+ formatImpact(symbol, impact) {
3098
+ const nodeCount = impact.nodes.size;
3099
+ // Compact format: just list affected symbols grouped by file
3100
+ const lines = [
3101
+ `## Impact: "${symbol}" affects ${nodeCount} symbols`,
3102
+ '',
3103
+ ];
3104
+ // Group by file
3105
+ const byFile = new Map();
3106
+ for (const node of impact.nodes.values()) {
3107
+ const existing = byFile.get(node.filePath) || [];
3108
+ existing.push(node);
3109
+ byFile.set(node.filePath, existing);
3110
+ }
3111
+ for (const [file, nodes] of byFile) {
3112
+ lines.push(`**${file}:**`);
3113
+ // Compact: inline list
3114
+ const nodeList = nodes.map(n => `${n.name}:${n.startLine}`).join(', ');
3115
+ lines.push(nodeList);
3116
+ lines.push('');
3117
+ }
3118
+ return lines.join('\n');
3119
+ }
3120
+ /**
3121
+ * Build a compact structural outline of a container symbol from its
3122
+ * indexed children (methods, fields, properties, …) — name, kind,
3123
+ * line number, and signature — so the agent gets the shape of a class
3124
+ * without the full source of every method. Returns '' when the container
3125
+ * has no indexed children, so the caller can fall back to full source.
3126
+ */
3127
+ buildContainerOutline(cg, node) {
3128
+ const children = cg.getChildren(node.id)
3129
+ .filter(c => c.kind !== 'import' && c.kind !== 'export')
3130
+ .sort((a, b) => (a.startLine ?? 0) - (b.startLine ?? 0));
3131
+ if (children.length === 0)
3132
+ return '';
3133
+ const lines = [`**Members (${children.length}):**`, ''];
3134
+ for (const c of children) {
3135
+ const loc = c.startLine ? `:${c.startLine}` : '';
3136
+ const sig = c.signature ? ` — \`${c.signature}\`` : '';
3137
+ lines.push(`- ${c.name} (${c.kind})${loc}${sig}`);
3138
+ }
3139
+ return lines.join('\n');
3140
+ }
3141
+ formatNodeDetails(node, code, outline) {
3142
+ const location = node.startLine ? `:${node.startLine}` : '';
3143
+ const lines = [
3144
+ `## ${node.name} (${node.kind})`,
3145
+ '',
3146
+ `**Location:** ${node.filePath}${location}`,
3147
+ ];
3148
+ if (node.signature) {
3149
+ lines.push(`**Signature:** \`${node.signature}\``);
3150
+ }
3151
+ // Only include docstring if it's short and useful
3152
+ if (node.docstring && node.docstring.length < 200) {
3153
+ lines.push('', node.docstring);
3154
+ }
3155
+ if (outline) {
3156
+ lines.push('', outline, '', `> Structural outline only. Read \`${node.filePath}\` or call specship_node on a specific member for its body.`);
3157
+ }
3158
+ else if (code) {
3159
+ // Line-numbered (cat -n style, like specship_explore and Read) so the
3160
+ // agent can cite/edit exact lines without re-Reading the file for them.
3161
+ const numbered = node.startLine ? numberSourceLines(code, node.startLine) : code;
3162
+ lines.push('', '```' + node.language, numbered, '```');
3163
+ }
3164
+ return lines.join('\n');
3165
+ }
3166
+ textResult(text) {
3167
+ return {
3168
+ content: [{ type: 'text', text }],
3169
+ };
3170
+ }
3171
+ errorResult(message) {
3172
+ return {
3173
+ content: [{ type: 'text', text: `Error: ${message}` }],
3174
+ isError: true,
3175
+ };
3176
+ }
3177
+ }
3178
+ exports.ToolHandler = ToolHandler;
3179
+ //# sourceMappingURL=tools.js.map