claude-code-workflow 6.1.4 → 6.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (437) hide show
  1. package/.claude/CLAUDE.md +10 -0
  2. package/.claude/agents/action-planning-agent.md +857 -778
  3. package/.claude/agents/cli-execution-agent.md +266 -269
  4. package/.claude/agents/cli-explore-agent.md +2 -2
  5. package/.claude/agents/cli-lite-planning-agent.md +142 -92
  6. package/.claude/agents/cli-planning-agent.md +4 -4
  7. package/.claude/agents/code-developer.md +7 -6
  8. package/.claude/agents/conceptual-planning-agent.md +2 -2
  9. package/.claude/agents/context-search-agent.md +31 -32
  10. package/.claude/agents/doc-generator.md +4 -4
  11. package/.claude/agents/memory-bridge.md +93 -93
  12. package/.claude/agents/test-context-search-agent.md +8 -7
  13. package/.claude/agents/test-fix-agent.md +7 -6
  14. package/.claude/commands/clean.md +516 -0
  15. package/.claude/commands/memory/compact.md +383 -0
  16. package/.claude/commands/memory/docs-full-cli.md +471 -471
  17. package/.claude/commands/memory/docs-related-cli.md +386 -386
  18. package/.claude/commands/memory/docs.md +615 -615
  19. package/.claude/commands/memory/load.md +5 -5
  20. package/.claude/commands/memory/tech-research-rules.md +310 -0
  21. package/.claude/commands/memory/update-full.md +332 -332
  22. package/.claude/commands/memory/workflow-skill-memory.md +4 -4
  23. package/.claude/commands/task/create.md +151 -151
  24. package/.claude/commands/version.md +254 -254
  25. package/.claude/commands/workflow/brainstorm/api-designer.md +587 -585
  26. package/.claude/commands/workflow/brainstorm/artifacts.md +1 -0
  27. package/.claude/commands/workflow/brainstorm/auto-parallel.md +443 -443
  28. package/.claude/commands/workflow/brainstorm/data-architect.md +220 -220
  29. package/.claude/commands/workflow/brainstorm/product-manager.md +200 -200
  30. package/.claude/commands/workflow/brainstorm/product-owner.md +200 -200
  31. package/.claude/commands/workflow/brainstorm/scrum-master.md +200 -200
  32. package/.claude/commands/workflow/brainstorm/subject-matter-expert.md +200 -200
  33. package/.claude/commands/workflow/brainstorm/system-architect.md +389 -387
  34. package/.claude/commands/workflow/brainstorm/ui-designer.md +221 -221
  35. package/.claude/commands/workflow/brainstorm/ux-expert.md +221 -221
  36. package/.claude/commands/workflow/debug.md +321 -0
  37. package/.claude/commands/workflow/execute.md +13 -0
  38. package/.claude/commands/workflow/init.md +165 -164
  39. package/.claude/commands/workflow/lite-execute.md +119 -13
  40. package/.claude/commands/workflow/lite-fix.md +623 -621
  41. package/.claude/commands/workflow/lite-plan.md +610 -592
  42. package/.claude/commands/workflow/plan.md +5 -5
  43. package/.claude/commands/workflow/review-module-cycle.md +2 -0
  44. package/.claude/commands/workflow/review-session-cycle.md +2 -0
  45. package/.claude/commands/workflow/review.md +297 -291
  46. package/.claude/commands/workflow/session/complete.md +153 -500
  47. package/.claude/commands/workflow/session/list.md +95 -95
  48. package/.claude/commands/workflow/session/resume.md +60 -60
  49. package/.claude/commands/workflow/session/start.md +199 -199
  50. package/.claude/commands/workflow/tdd-plan.md +3 -3
  51. package/.claude/commands/workflow/tdd-verify.md +23 -9
  52. package/.claude/commands/workflow/test-cycle-execute.md +2 -0
  53. package/.claude/commands/workflow/test-fix-gen.md +699 -699
  54. package/.claude/commands/workflow/tools/conflict-resolution.md +104 -18
  55. package/.claude/commands/workflow/tools/context-gather.md +436 -434
  56. package/.claude/commands/workflow/tools/task-generate-agent.md +490 -291
  57. package/.claude/commands/workflow/tools/task-generate-tdd.md +18 -10
  58. package/.claude/commands/workflow/tools/test-concept-enhanced.md +2 -1
  59. package/.claude/commands/workflow/tools/test-context-gather.md +1 -0
  60. package/.claude/commands/workflow/tools/test-task-generate.md +1 -0
  61. package/.claude/commands/workflow/ui-design/import-from-code.md +9 -6
  62. package/.claude/skills/command-guide/SKILL.md +5 -5
  63. package/.claude/skills/command-guide/index/all-commands.json +1 -1
  64. package/.claude/skills/command-guide/index/by-category.json +1 -1
  65. package/.claude/skills/command-guide/index/by-use-case.json +1 -1
  66. package/.claude/skills/command-guide/reference/agents/action-planning-agent.md +857 -778
  67. package/.claude/skills/command-guide/reference/agents/cli-execution-agent.md +266 -269
  68. package/.claude/skills/command-guide/reference/agents/cli-explore-agent.md +2 -2
  69. package/.claude/skills/command-guide/reference/agents/cli-lite-planning-agent.md +142 -92
  70. package/.claude/skills/command-guide/reference/agents/cli-planning-agent.md +4 -4
  71. package/.claude/skills/command-guide/reference/agents/code-developer.md +7 -6
  72. package/.claude/skills/command-guide/reference/agents/conceptual-planning-agent.md +2 -2
  73. package/.claude/skills/command-guide/reference/agents/context-search-agent.md +31 -32
  74. package/.claude/skills/command-guide/reference/agents/doc-generator.md +4 -4
  75. package/.claude/skills/command-guide/reference/agents/memory-bridge.md +93 -93
  76. package/.claude/skills/command-guide/reference/agents/test-context-search-agent.md +8 -7
  77. package/.claude/skills/command-guide/reference/agents/test-fix-agent.md +7 -6
  78. package/.claude/skills/command-guide/reference/commands/memory/docs-full-cli.md +471 -471
  79. package/.claude/skills/command-guide/reference/commands/memory/docs-related-cli.md +386 -386
  80. package/.claude/skills/command-guide/reference/commands/memory/docs.md +17 -16
  81. package/.claude/skills/command-guide/reference/commands/memory/load.md +5 -5
  82. package/.claude/skills/command-guide/reference/commands/memory/tech-research.md +194 -357
  83. package/.claude/skills/command-guide/reference/commands/memory/update-full.md +332 -332
  84. package/.claude/skills/command-guide/reference/commands/memory/workflow-skill-memory.md +4 -4
  85. package/.claude/skills/command-guide/reference/commands/task/create.md +151 -151
  86. package/.claude/skills/command-guide/reference/commands/version.md +254 -254
  87. package/.claude/skills/command-guide/reference/commands/workflow/brainstorm/api-designer.md +585 -585
  88. package/.claude/skills/command-guide/reference/commands/workflow/brainstorm/auto-parallel.md +443 -443
  89. package/.claude/skills/command-guide/reference/commands/workflow/brainstorm/data-architect.md +220 -220
  90. package/.claude/skills/command-guide/reference/commands/workflow/brainstorm/product-manager.md +200 -200
  91. package/.claude/skills/command-guide/reference/commands/workflow/brainstorm/product-owner.md +200 -200
  92. package/.claude/skills/command-guide/reference/commands/workflow/brainstorm/scrum-master.md +200 -200
  93. package/.claude/skills/command-guide/reference/commands/workflow/brainstorm/subject-matter-expert.md +200 -200
  94. package/.claude/skills/command-guide/reference/commands/workflow/brainstorm/system-architect.md +387 -387
  95. package/.claude/skills/command-guide/reference/commands/workflow/brainstorm/ui-designer.md +221 -221
  96. package/.claude/skills/command-guide/reference/commands/workflow/brainstorm/ux-expert.md +221 -221
  97. package/.claude/skills/command-guide/reference/commands/workflow/execute.md +25 -20
  98. package/.claude/skills/command-guide/reference/commands/workflow/init.md +164 -164
  99. package/.claude/skills/command-guide/reference/commands/workflow/lite-execute.md +748 -686
  100. package/.claude/skills/command-guide/reference/commands/workflow/lite-fix.md +664 -621
  101. package/.claude/skills/command-guide/reference/commands/workflow/lite-plan.md +645 -592
  102. package/.claude/skills/command-guide/reference/commands/workflow/plan.md +5 -5
  103. package/.claude/skills/command-guide/reference/commands/workflow/review.md +25 -18
  104. package/.claude/skills/command-guide/reference/commands/workflow/session/complete.md +547 -500
  105. package/.claude/skills/command-guide/reference/commands/workflow/session/list.md +45 -27
  106. package/.claude/skills/command-guide/reference/commands/workflow/session/resume.md +35 -19
  107. package/.claude/skills/command-guide/reference/commands/workflow/session/start.md +90 -33
  108. package/.claude/skills/command-guide/reference/commands/workflow/tdd-plan.md +3 -3
  109. package/.claude/skills/command-guide/reference/commands/workflow/tdd-verify.md +23 -9
  110. package/.claude/skills/command-guide/reference/commands/workflow/test-fix-gen.md +699 -699
  111. package/.claude/skills/command-guide/reference/commands/workflow/tools/conflict-resolution.md +103 -17
  112. package/.claude/skills/command-guide/reference/commands/workflow/tools/context-gather.md +434 -434
  113. package/.claude/skills/command-guide/reference/commands/workflow/tools/task-generate-agent.md +487 -291
  114. package/.claude/skills/command-guide/reference/commands/workflow/tools/task-generate-tdd.md +17 -10
  115. package/.claude/skills/command-guide/reference/commands/workflow/tools/test-concept-enhanced.md +1 -1
  116. package/.claude/skills/command-guide/reference/commands/workflow/ui-design/import-from-code.md +6 -6
  117. package/.claude/workflows/chinese-response.md +38 -0
  118. package/.claude/workflows/cli-templates/prompts/rules/rule-api.txt +122 -0
  119. package/.claude/workflows/cli-templates/prompts/rules/rule-components.txt +122 -0
  120. package/.claude/workflows/cli-templates/prompts/rules/rule-config.txt +89 -0
  121. package/.claude/workflows/cli-templates/prompts/rules/rule-core.txt +60 -0
  122. package/.claude/workflows/cli-templates/prompts/rules/rule-patterns.txt +70 -0
  123. package/.claude/workflows/cli-templates/prompts/rules/rule-testing.txt +81 -0
  124. package/.claude/workflows/cli-templates/prompts/rules/tech-rules-agent-prompt.txt +89 -0
  125. package/.claude/workflows/cli-templates/prompts/workflow/gemini-solution-design.txt +131 -131
  126. package/.claude/workflows/cli-templates/prompts/workflow/skill-conflict-patterns.txt +5 -9
  127. package/.claude/workflows/cli-templates/prompts/workflow/skill-lessons-learned.txt +5 -9
  128. package/.claude/workflows/cli-templates/protocols/analysis-protocol.md +112 -0
  129. package/.claude/workflows/cli-templates/protocols/write-protocol.md +201 -0
  130. package/.claude/workflows/cli-templates/schemas/conflict-resolution-schema.json +137 -0
  131. package/.claude/workflows/cli-templates/schemas/debug-log-json-schema.json +127 -0
  132. package/.claude/workflows/cli-templates/schemas/fix-plan-json-schema.json +25 -0
  133. package/.claude/workflows/cli-templates/schemas/plan-json-schema.json +25 -0
  134. package/.claude/workflows/cli-tools-usage.md +526 -0
  135. package/{CLAUDE.md → .claude/workflows/coding-philosophy.md} +24 -45
  136. package/.claude/workflows/context-tools.md +84 -0
  137. package/.claude/workflows/file-modification.md +64 -0
  138. package/.claude/workflows/tool-strategy.md +216 -79
  139. package/.claude/workflows/windows-platform.md +16 -0
  140. package/.claude/workflows/workflow-architecture.md +942 -942
  141. package/.codex/AGENTS.md +63 -330
  142. package/.codex/prompts/debug.md +318 -0
  143. package/.codex/prompts/execute.md +273 -0
  144. package/.codex/prompts/lite-execute.md +164 -0
  145. package/.codex/prompts/lite-plan.md +469 -0
  146. package/.codex/prompts.zip +0 -0
  147. package/.gemini/GEMINI.md +25 -164
  148. package/.qwen/QWEN.md +0 -139
  149. package/README.md +29 -9
  150. package/ccw/README.md +30 -6
  151. package/ccw/bin/ccw-mcp.js +7 -0
  152. package/ccw/bin/ccw.js +9 -9
  153. package/ccw/package.json +65 -47
  154. package/ccw/src/.workflow/.cli-history/history.db +0 -0
  155. package/ccw/src/.workflow/.cli-history/history.db-shm +0 -0
  156. package/ccw/src/.workflow/.cli-history/history.db-wal +0 -0
  157. package/ccw/src/cli.ts +244 -0
  158. package/ccw/src/commands/cli.ts +740 -0
  159. package/ccw/src/commands/core-memory.ts +770 -0
  160. package/ccw/src/commands/hook.ts +315 -0
  161. package/ccw/src/commands/install.ts +519 -0
  162. package/ccw/src/commands/{list.js → list.ts} +1 -1
  163. package/ccw/src/commands/memory.ts +1090 -0
  164. package/ccw/src/commands/{serve.js → serve.ts} +14 -5
  165. package/ccw/src/commands/session-path-resolver.ts +372 -0
  166. package/ccw/src/commands/session.ts +1141 -0
  167. package/ccw/src/commands/{stop.js → stop.ts} +16 -6
  168. package/ccw/src/commands/tool.ts +201 -0
  169. package/ccw/src/commands/{uninstall.js → uninstall.ts} +89 -40
  170. package/ccw/src/commands/{upgrade.js → upgrade.ts} +68 -23
  171. package/ccw/src/commands/{view.js → view.ts} +22 -8
  172. package/ccw/src/config/storage-paths.ts +670 -0
  173. package/ccw/src/core/cache-manager.ts +294 -0
  174. package/ccw/src/core/claude-freshness.ts +319 -0
  175. package/ccw/src/core/core-memory-store.ts +1528 -0
  176. package/ccw/src/core/{dashboard-generator-patch.js → dashboard-generator-patch.ts} +18 -0
  177. package/ccw/src/core/{dashboard-generator.js → dashboard-generator.ts} +69 -12
  178. package/ccw/src/core/data-aggregator.ts +584 -0
  179. package/ccw/src/core/history-importer.ts +625 -0
  180. package/ccw/src/core/{lite-scanner.js → lite-scanner-complete.ts} +162 -66
  181. package/ccw/src/core/lite-scanner.ts +469 -0
  182. package/ccw/src/core/{manifest.js → manifest.ts} +104 -34
  183. package/ccw/src/core/memory-embedder-bridge.ts +262 -0
  184. package/ccw/src/core/memory-store.ts +978 -0
  185. package/ccw/src/core/routes/ccw-routes.ts +96 -0
  186. package/ccw/src/core/routes/claude-routes.ts +1183 -0
  187. package/ccw/src/core/routes/cli-routes.ts +561 -0
  188. package/ccw/src/core/routes/codexlens-routes.ts +806 -0
  189. package/ccw/src/core/routes/core-memory-routes.ts +605 -0
  190. package/ccw/src/core/routes/files-routes.ts +428 -0
  191. package/ccw/src/core/routes/graph-routes.md +164 -0
  192. package/ccw/src/core/routes/graph-routes.ts +626 -0
  193. package/ccw/src/core/routes/help-routes.ts +308 -0
  194. package/ccw/src/core/routes/hooks-routes.ts +405 -0
  195. package/ccw/src/core/routes/mcp-routes.ts +1271 -0
  196. package/ccw/src/core/routes/mcp-routes.ts.backup +550 -0
  197. package/ccw/src/core/routes/mcp-templates-db.ts +268 -0
  198. package/ccw/src/core/routes/memory-routes.ts +1206 -0
  199. package/ccw/src/core/routes/rules-routes.ts +526 -0
  200. package/ccw/src/core/routes/session-routes.ts +467 -0
  201. package/ccw/src/core/routes/skills-routes.ts +599 -0
  202. package/ccw/src/core/routes/status-routes.ts +57 -0
  203. package/ccw/src/core/routes/system-routes.ts +427 -0
  204. package/ccw/src/core/server.ts +431 -0
  205. package/ccw/src/core/session-clustering-service.ts +1258 -0
  206. package/ccw/src/core/session-scanner.ts +283 -0
  207. package/ccw/src/core/websocket.ts +190 -0
  208. package/ccw/src/{index.js → index.ts} +1 -0
  209. package/ccw/src/mcp-server/index.ts +186 -0
  210. package/ccw/src/templates/assets/css/github-dark.min.css +10 -0
  211. package/ccw/src/templates/assets/css/github.min.css +10 -0
  212. package/ccw/src/templates/assets/js/cytoscape.min.js +32 -0
  213. package/ccw/src/templates/assets/js/d3.min.js +2 -0
  214. package/ccw/src/templates/assets/js/highlight.min.js +1244 -0
  215. package/ccw/src/templates/assets/js/lucide.min.js +12 -0
  216. package/ccw/src/templates/assets/js/marked.min.js +69 -0
  217. package/ccw/src/templates/assets/js/tailwind.js +83 -0
  218. package/ccw/src/templates/dashboard-css/01-base.css +11 -0
  219. package/ccw/src/templates/dashboard-css/02-session.css +22 -0
  220. package/ccw/src/templates/dashboard-css/04-lite-tasks.css +10 -0
  221. package/ccw/src/templates/dashboard-css/06-cards.css +10 -4
  222. package/ccw/src/templates/dashboard-css/07-managers.css +1178 -7
  223. package/ccw/src/templates/dashboard-css/09-explorer.css +23 -12
  224. package/ccw/src/templates/dashboard-css/10-cli-status.css +337 -0
  225. package/ccw/src/templates/dashboard-css/11-cli-history.css +271 -0
  226. package/ccw/src/templates/dashboard-css/12-cli-legacy.css +796 -0
  227. package/ccw/src/templates/dashboard-css/13-cli-ccw.css +199 -0
  228. package/ccw/src/templates/dashboard-css/14-cli-modals.css +258 -0
  229. package/ccw/src/templates/dashboard-css/15-cli-endpoints.css +305 -0
  230. package/ccw/src/templates/dashboard-css/16-cli-session.css +241 -0
  231. package/ccw/src/templates/dashboard-css/17-cli-conversation.css +283 -0
  232. package/ccw/src/templates/dashboard-css/18-cli-settings.css +160 -0
  233. package/ccw/src/templates/dashboard-css/19-cli-native-session.css +496 -0
  234. package/ccw/src/templates/dashboard-css/20-cli-taskqueue.css +188 -0
  235. package/ccw/src/templates/dashboard-css/21-cli-toolmgmt.css +310 -0
  236. package/ccw/src/templates/dashboard-css/22-cli-semantic.css +240 -0
  237. package/ccw/src/templates/dashboard-css/23-memory.css +2390 -0
  238. package/ccw/src/templates/dashboard-css/24-prompt-history.css +1089 -0
  239. package/ccw/src/templates/dashboard-css/25-skills-rules.css +326 -0
  240. package/ccw/src/templates/dashboard-css/26-claude-manager.css +908 -0
  241. package/ccw/src/templates/dashboard-css/27-graph-explorer.css +1678 -0
  242. package/ccw/src/templates/dashboard-css/28-mcp-manager.css +748 -0
  243. package/ccw/src/templates/dashboard-css/29-help.css +264 -0
  244. package/ccw/src/templates/dashboard-css/30-core-memory.css +1700 -0
  245. package/ccw/src/templates/dashboard-js/api.js +162 -142
  246. package/ccw/src/templates/dashboard-js/components/carousel.js +4 -4
  247. package/ccw/src/templates/dashboard-js/components/cli-history.js +876 -0
  248. package/ccw/src/templates/dashboard-js/components/cli-status.js +978 -0
  249. package/ccw/src/templates/dashboard-js/components/global-notifications.js +508 -219
  250. package/ccw/src/templates/dashboard-js/components/hook-manager.js +1277 -282
  251. package/ccw/src/templates/dashboard-js/components/index-manager.js +302 -0
  252. package/ccw/src/templates/dashboard-js/components/mcp-manager.js +718 -27
  253. package/ccw/src/templates/dashboard-js/components/modals.js +66 -0
  254. package/ccw/src/templates/dashboard-js/components/navigation.js +80 -12
  255. package/ccw/src/templates/dashboard-js/components/notifications.js +758 -194
  256. package/ccw/src/templates/dashboard-js/components/storage-manager.js +478 -0
  257. package/ccw/src/templates/dashboard-js/components/tabs-other.js +157 -6
  258. package/ccw/src/templates/dashboard-js/components/task-queue-sidebar.js +716 -0
  259. package/ccw/src/templates/dashboard-js/help-i18n.js +272 -0
  260. package/ccw/src/templates/dashboard-js/i18n.js +2807 -0
  261. package/ccw/src/templates/dashboard-js/main.js +15 -0
  262. package/ccw/src/templates/dashboard-js/state.js +243 -42
  263. package/ccw/src/templates/dashboard-js/utils.js +47 -1
  264. package/ccw/src/templates/dashboard-js/views/claude-manager.js +912 -0
  265. package/ccw/src/templates/dashboard-js/views/cli-manager.js +2272 -0
  266. package/ccw/src/templates/dashboard-js/views/codexlens-manager.js +964 -0
  267. package/ccw/src/templates/dashboard-js/views/core-memory-clusters.js +503 -0
  268. package/ccw/src/templates/dashboard-js/views/core-memory.js +782 -0
  269. package/ccw/src/templates/dashboard-js/views/explorer.js +888 -852
  270. package/ccw/src/templates/dashboard-js/views/graph-explorer.js +1157 -0
  271. package/ccw/src/templates/dashboard-js/views/help.js +856 -0
  272. package/ccw/src/templates/dashboard-js/views/history.js +337 -0
  273. package/ccw/src/templates/dashboard-js/views/home.js +61 -15
  274. package/ccw/src/templates/dashboard-js/views/hook-manager.js +311 -43
  275. package/ccw/src/templates/dashboard-js/views/lite-tasks.js +204 -28
  276. package/ccw/src/templates/dashboard-js/views/mcp-manager.js +2187 -411
  277. package/ccw/src/templates/dashboard-js/views/mcp-manager.js.backup +1729 -0
  278. package/ccw/src/templates/dashboard-js/views/mcp-manager.js.new +928 -0
  279. package/ccw/src/templates/dashboard-js/views/memory.js +1221 -0
  280. package/ccw/src/templates/dashboard-js/views/prompt-history.js +713 -0
  281. package/ccw/src/templates/dashboard-js/views/rules-manager.js +828 -0
  282. package/ccw/src/templates/dashboard-js/views/session-detail.js +54 -53
  283. package/ccw/src/templates/dashboard-js/views/skills-manager.js +819 -0
  284. package/ccw/src/templates/dashboard.html +185 -85
  285. package/ccw/src/templates/hooks-config-example.json +60 -0
  286. package/ccw/src/tools/classify-folders.ts +245 -0
  287. package/ccw/src/tools/cli-config-manager.ts +268 -0
  288. package/ccw/src/tools/cli-executor.ts +2014 -0
  289. package/ccw/src/tools/cli-history-store.ts +1195 -0
  290. package/ccw/src/tools/codex-lens.ts +1141 -0
  291. package/ccw/src/tools/{convert-tokens-to-css.js → convert-tokens-to-css.ts} +73 -23
  292. package/ccw/src/tools/core-memory.ts +444 -0
  293. package/ccw/src/tools/detect-changed-modules.ts +325 -0
  294. package/ccw/src/tools/{discover-design-files.js → discover-design-files.ts} +74 -24
  295. package/ccw/src/tools/edit-file.ts +568 -0
  296. package/ccw/src/tools/{generate-module-docs.js → generate-module-docs.ts} +207 -185
  297. package/ccw/src/tools/{get-modules-by-depth.js → get-modules-by-depth.ts} +120 -79
  298. package/ccw/src/tools/index.ts +370 -0
  299. package/ccw/src/tools/native-session-discovery.ts +795 -0
  300. package/ccw/src/tools/notifier.ts +129 -0
  301. package/ccw/src/tools/read-file.ts +410 -0
  302. package/ccw/src/tools/resume-strategy.ts +345 -0
  303. package/ccw/src/tools/session-content-parser.ts +619 -0
  304. package/ccw/src/tools/session-manager.ts +1026 -0
  305. package/ccw/src/tools/smart-context.ts +228 -0
  306. package/ccw/src/tools/smart-search.ts +2065 -0
  307. package/ccw/src/tools/smart-search.ts.backup +1233 -0
  308. package/ccw/src/tools/storage-manager.ts +455 -0
  309. package/ccw/src/tools/write-file.ts +222 -0
  310. package/ccw/src/types/config.ts +11 -0
  311. package/ccw/src/types/index.ts +3 -0
  312. package/ccw/src/types/session.ts +25 -0
  313. package/ccw/src/types/tool.ts +41 -0
  314. package/ccw/src/utils/{browser-launcher.js → browser-launcher.ts} +10 -8
  315. package/ccw/src/utils/file-utils.ts +48 -0
  316. package/ccw/src/utils/{path-resolver.js → path-resolver.ts} +114 -78
  317. package/ccw/src/utils/path-validator.ts +153 -0
  318. package/ccw/src/utils/{ui.js → ui.ts} +32 -25
  319. package/codex-lens/pyproject.toml +48 -0
  320. package/codex-lens/src/codexlens/.workflow/.cli-history/history.db +0 -0
  321. package/codex-lens/src/codexlens/__init__.py +28 -0
  322. package/codex-lens/src/codexlens/__main__.py +14 -0
  323. package/codex-lens/src/codexlens/__pycache__/__init__.cpython-313.pyc +0 -0
  324. package/codex-lens/src/codexlens/__pycache__/__main__.cpython-313.pyc +0 -0
  325. package/codex-lens/src/codexlens/__pycache__/config.cpython-313.pyc +0 -0
  326. package/codex-lens/src/codexlens/__pycache__/entities.cpython-313.pyc +0 -0
  327. package/codex-lens/src/codexlens/__pycache__/errors.cpython-313.pyc +0 -0
  328. package/codex-lens/src/codexlens/cli/__init__.py +27 -0
  329. package/codex-lens/src/codexlens/cli/__pycache__/__init__.cpython-313.pyc +0 -0
  330. package/codex-lens/src/codexlens/cli/__pycache__/commands.cpython-313.pyc +0 -0
  331. package/codex-lens/src/codexlens/cli/__pycache__/embedding_manager.cpython-313.pyc +0 -0
  332. package/codex-lens/src/codexlens/cli/__pycache__/model_manager.cpython-313.pyc +0 -0
  333. package/codex-lens/src/codexlens/cli/__pycache__/output.cpython-313.pyc +0 -0
  334. package/codex-lens/src/codexlens/cli/commands.py +1931 -0
  335. package/codex-lens/src/codexlens/cli/embedding_manager.py +620 -0
  336. package/codex-lens/src/codexlens/cli/model_manager.py +289 -0
  337. package/codex-lens/src/codexlens/cli/output.py +124 -0
  338. package/codex-lens/src/codexlens/config.py +201 -0
  339. package/codex-lens/src/codexlens/entities.py +121 -0
  340. package/codex-lens/src/codexlens/errors.py +55 -0
  341. package/codex-lens/src/codexlens/indexing/README.md +77 -0
  342. package/codex-lens/src/codexlens/indexing/__init__.py +4 -0
  343. package/codex-lens/src/codexlens/indexing/__pycache__/__init__.cpython-313.pyc +0 -0
  344. package/codex-lens/src/codexlens/indexing/__pycache__/symbol_extractor.cpython-313.pyc +0 -0
  345. package/codex-lens/src/codexlens/indexing/symbol_extractor.py +243 -0
  346. package/codex-lens/src/codexlens/parsers/__init__.py +8 -0
  347. package/codex-lens/src/codexlens/parsers/__pycache__/__init__.cpython-313.pyc +0 -0
  348. package/codex-lens/src/codexlens/parsers/__pycache__/encoding.cpython-313.pyc +0 -0
  349. package/codex-lens/src/codexlens/parsers/__pycache__/factory.cpython-313.pyc +0 -0
  350. package/codex-lens/src/codexlens/parsers/__pycache__/tokenizer.cpython-313.pyc +0 -0
  351. package/codex-lens/src/codexlens/parsers/__pycache__/treesitter_parser.cpython-313.pyc +0 -0
  352. package/codex-lens/src/codexlens/parsers/encoding.py +202 -0
  353. package/codex-lens/src/codexlens/parsers/factory.py +256 -0
  354. package/codex-lens/src/codexlens/parsers/tokenizer.py +98 -0
  355. package/codex-lens/src/codexlens/parsers/treesitter_parser.py +335 -0
  356. package/codex-lens/src/codexlens/search/__init__.py +15 -0
  357. package/codex-lens/src/codexlens/search/__pycache__/__init__.cpython-313.pyc +0 -0
  358. package/codex-lens/src/codexlens/search/__pycache__/chain_search.cpython-313.pyc +0 -0
  359. package/codex-lens/src/codexlens/search/__pycache__/enrichment.cpython-313.pyc +0 -0
  360. package/codex-lens/src/codexlens/search/__pycache__/hybrid_search.cpython-313.pyc +0 -0
  361. package/codex-lens/src/codexlens/search/__pycache__/query_parser.cpython-313.pyc +0 -0
  362. package/codex-lens/src/codexlens/search/__pycache__/ranking.cpython-313.pyc +0 -0
  363. package/codex-lens/src/codexlens/search/chain_search.py +647 -0
  364. package/codex-lens/src/codexlens/search/enrichment.py +150 -0
  365. package/codex-lens/src/codexlens/search/hybrid_search.py +313 -0
  366. package/codex-lens/src/codexlens/search/query_parser.py +242 -0
  367. package/codex-lens/src/codexlens/search/ranking.py +274 -0
  368. package/codex-lens/src/codexlens/semantic/__init__.py +39 -0
  369. package/codex-lens/src/codexlens/semantic/__pycache__/__init__.cpython-313.pyc +0 -0
  370. package/codex-lens/src/codexlens/semantic/__pycache__/ann_index.cpython-313.pyc +0 -0
  371. package/codex-lens/src/codexlens/semantic/__pycache__/chunker.cpython-313.pyc +0 -0
  372. package/codex-lens/src/codexlens/semantic/__pycache__/code_extractor.cpython-313.pyc +0 -0
  373. package/codex-lens/src/codexlens/semantic/__pycache__/embedder.cpython-313.pyc +0 -0
  374. package/codex-lens/src/codexlens/semantic/__pycache__/graph_analyzer.cpython-313.pyc +0 -0
  375. package/codex-lens/src/codexlens/semantic/__pycache__/llm_enhancer.cpython-313.pyc +0 -0
  376. package/codex-lens/src/codexlens/semantic/__pycache__/vector_store.cpython-313.pyc +0 -0
  377. package/codex-lens/src/codexlens/semantic/ann_index.py +414 -0
  378. package/codex-lens/src/codexlens/semantic/chunker.py +448 -0
  379. package/codex-lens/src/codexlens/semantic/code_extractor.py +274 -0
  380. package/codex-lens/src/codexlens/semantic/embedder.py +185 -0
  381. package/codex-lens/src/codexlens/semantic/vector_store.py +955 -0
  382. package/codex-lens/src/codexlens/storage/__init__.py +29 -0
  383. package/codex-lens/src/codexlens/storage/__pycache__/__init__.cpython-313.pyc +0 -0
  384. package/codex-lens/src/codexlens/storage/__pycache__/dir_index.cpython-313.pyc +0 -0
  385. package/codex-lens/src/codexlens/storage/__pycache__/file_cache.cpython-313.pyc +0 -0
  386. package/codex-lens/src/codexlens/storage/__pycache__/index_tree.cpython-313.pyc +0 -0
  387. package/codex-lens/src/codexlens/storage/__pycache__/migration_manager.cpython-313.pyc +0 -0
  388. package/codex-lens/src/codexlens/storage/__pycache__/path_mapper.cpython-313.pyc +0 -0
  389. package/codex-lens/src/codexlens/storage/__pycache__/registry.cpython-313.pyc +0 -0
  390. package/codex-lens/src/codexlens/storage/__pycache__/sqlite_store.cpython-313.pyc +0 -0
  391. package/codex-lens/src/codexlens/storage/__pycache__/sqlite_utils.cpython-313.pyc +0 -0
  392. package/codex-lens/src/codexlens/storage/dir_index.py +1850 -0
  393. package/codex-lens/src/codexlens/storage/file_cache.py +32 -0
  394. package/codex-lens/src/codexlens/storage/index_tree.py +776 -0
  395. package/codex-lens/src/codexlens/storage/migration_manager.py +154 -0
  396. package/codex-lens/src/codexlens/storage/migrations/__init__.py +1 -0
  397. package/codex-lens/src/codexlens/storage/migrations/__pycache__/__init__.cpython-313.pyc +0 -0
  398. package/codex-lens/src/codexlens/storage/migrations/__pycache__/migration_001_normalize_keywords.cpython-313.pyc +0 -0
  399. package/codex-lens/src/codexlens/storage/migrations/__pycache__/migration_002_add_token_metadata.cpython-313.pyc +0 -0
  400. package/codex-lens/src/codexlens/storage/migrations/__pycache__/migration_003_code_relationships.cpython-313.pyc +0 -0
  401. package/codex-lens/src/codexlens/storage/migrations/__pycache__/migration_004_dual_fts.cpython-313.pyc +0 -0
  402. package/codex-lens/src/codexlens/storage/migrations/__pycache__/migration_005_cleanup_unused_fields.cpython-313.pyc +0 -0
  403. package/codex-lens/src/codexlens/storage/migrations/migration_001_normalize_keywords.py +123 -0
  404. package/codex-lens/src/codexlens/storage/migrations/migration_002_add_token_metadata.py +48 -0
  405. package/codex-lens/src/codexlens/storage/migrations/migration_004_dual_fts.py +232 -0
  406. package/codex-lens/src/codexlens/storage/migrations/migration_005_cleanup_unused_fields.py +196 -0
  407. package/codex-lens/src/codexlens/storage/path_mapper.py +274 -0
  408. package/codex-lens/src/codexlens/storage/registry.py +670 -0
  409. package/codex-lens/src/codexlens/storage/sqlite_store.py +576 -0
  410. package/codex-lens/src/codexlens/storage/sqlite_utils.py +64 -0
  411. package/package.json +4 -1
  412. package/.claude/commands/memory/tech-research.md +0 -477
  413. package/.claude/scripts/classify-folders.sh +0 -39
  414. package/.claude/scripts/convert_tokens_to_css.sh +0 -229
  415. package/.claude/scripts/detect_changed_modules.sh +0 -161
  416. package/.claude/scripts/discover-design-files.sh +0 -87
  417. package/.claude/scripts/extract-animations.js +0 -243
  418. package/.claude/scripts/extract-computed-styles.js +0 -118
  419. package/.claude/scripts/extract-layout-structure.js +0 -411
  420. package/.claude/scripts/generate_module_docs.sh +0 -717
  421. package/.claude/scripts/get_modules_by_depth.sh +0 -170
  422. package/.claude/scripts/ui-generate-preview.sh +0 -395
  423. package/.claude/scripts/ui-instantiate-prototypes.sh +0 -815
  424. package/.claude/scripts/update_module_claude.sh +0 -337
  425. package/.claude/workflows/context-search-strategy.md +0 -77
  426. package/.claude/workflows/intelligent-tools-strategy.md +0 -662
  427. package/ccw/src/cli.js +0 -119
  428. package/ccw/src/commands/install.js +0 -324
  429. package/ccw/src/commands/tool.js +0 -138
  430. package/ccw/src/core/data-aggregator.js +0 -409
  431. package/ccw/src/core/server.js +0 -2063
  432. package/ccw/src/core/session-scanner.js +0 -235
  433. package/ccw/src/tools/classify-folders.js +0 -204
  434. package/ccw/src/tools/detect-changed-modules.js +0 -288
  435. package/ccw/src/tools/edit-file.js +0 -266
  436. package/ccw/src/tools/index.js +0 -176
  437. package/ccw/src/utils/file-utils.js +0 -48
@@ -0,0 +1,2065 @@
1
+ /**
2
+ * Smart Search Tool - Unified intelligent search with CodexLens integration
3
+ *
4
+ * Features:
5
+ * - Intent classification with automatic mode selection
6
+ * - CodexLens integration (init, hybrid, vector, semantic)
7
+ * - Ripgrep fallback for exact mode
8
+ * - Index status checking and warnings
9
+ * - Multi-backend search routing with RRF ranking
10
+ *
11
+ * Actions:
12
+ * - init: Initialize CodexLens index
13
+ * - search: Intelligent search with auto mode selection
14
+ * - status: Check index status
15
+ */
16
+
17
+ import { z } from 'zod';
18
+ import type { ToolSchema, ToolResult } from '../types/tool.js';
19
+ import { spawn, execSync } from 'child_process';
20
+ import {
21
+ ensureReady as ensureCodexLensReady,
22
+ executeCodexLens,
23
+ } from './codex-lens.js';
24
+ import type { ProgressInfo } from './codex-lens.js';
25
+ import { getProjectRoot } from '../utils/path-validator.js';
26
+
27
+ // Define Zod schema for validation
28
+ const ParamsSchema = z.object({
29
+ // Action: search (content), find_files (path/name pattern), init, status
30
+ // Note: search_files is deprecated, use search with output_mode='files_only'
31
+ action: z.enum(['init', 'search', 'search_files', 'find_files', 'status']).default('search'),
32
+ query: z.string().optional().describe('Content search query (for action="search")'),
33
+ pattern: z.string().optional().describe('Glob pattern for path matching (for action="find_files")'),
34
+ mode: z.enum(['auto', 'hybrid', 'exact', 'ripgrep', 'priority']).default('auto'),
35
+ output_mode: z.enum(['full', 'files_only', 'count']).default('full'),
36
+ path: z.string().optional(),
37
+ paths: z.array(z.string()).default([]),
38
+ contextLines: z.number().default(0),
39
+ maxResults: z.number().default(20), // Increased default
40
+ includeHidden: z.boolean().default(false),
41
+ languages: z.array(z.string()).optional(),
42
+ limit: z.number().default(20), // Increased default
43
+ offset: z.number().default(0), // NEW: Pagination offset (start_index)
44
+ enrich: z.boolean().default(false),
45
+ // Search modifiers for ripgrep mode
46
+ regex: z.boolean().default(true), // Use regex pattern matching (default: enabled)
47
+ caseSensitive: z.boolean().default(true), // Case sensitivity (default: case-sensitive)
48
+ tokenize: z.boolean().default(true), // Tokenize multi-word queries for OR matching (default: enabled)
49
+ // Fuzzy matching is implicit in hybrid mode (RRF fusion)
50
+ });
51
+
52
+ type Params = z.infer<typeof ParamsSchema>;
53
+
54
+ // Search mode constants
55
+ const SEARCH_MODES = ['auto', 'hybrid', 'exact', 'ripgrep', 'priority'] as const;
56
+
57
+ // Classification confidence threshold
58
+ const CONFIDENCE_THRESHOLD = 0.7;
59
+
60
+ // File filtering configuration (ported from code-index)
61
+ const FILTER_CONFIG = {
62
+ exclude_directories: new Set([
63
+ '.git', '.svn', '.hg', '.bzr',
64
+ 'node_modules', '__pycache__', '.venv', 'venv', 'vendor', 'bower_components',
65
+ 'dist', 'build', 'target', 'out', 'bin', 'obj',
66
+ '.idea', '.vscode', '.vs', '.sublime-workspace',
67
+ '.pytest_cache', '.coverage', '.tox', '.nyc_output', 'coverage', 'htmlcov',
68
+ '.next', '.nuxt', '.cache', '.parcel-cache',
69
+ '.DS_Store', 'Thumbs.db',
70
+ ]),
71
+ exclude_files: new Set([
72
+ '*.tmp', '*.temp', '*.swp', '*.swo', '*.bak', '*~', '*.orig', '*.log',
73
+ 'package-lock.json', 'yarn.lock', 'pnpm-lock.yaml', 'Pipfile.lock',
74
+ ]),
75
+ // Windows device files - must use **/ pattern to match in any directory
76
+ // These cause "os error 1" on Windows when accessed
77
+ windows_device_files: new Set([
78
+ 'nul', 'con', 'aux', 'prn',
79
+ 'com1', 'com2', 'com3', 'com4', 'com5', 'com6', 'com7', 'com8', 'com9',
80
+ 'lpt1', 'lpt2', 'lpt3', 'lpt4', 'lpt5', 'lpt6', 'lpt7', 'lpt8', 'lpt9',
81
+ ]),
82
+ };
83
+
84
+ function buildExcludeArgs(): string[] {
85
+ const args: string[] = [];
86
+ for (const dir of FILTER_CONFIG.exclude_directories) {
87
+ args.push('--glob', `!**/${dir}/**`);
88
+ }
89
+ for (const pattern of FILTER_CONFIG.exclude_files) {
90
+ args.push('--glob', `!${pattern}`);
91
+ }
92
+ // Windows device files need case-insensitive matching in any directory
93
+ for (const device of FILTER_CONFIG.windows_device_files) {
94
+ args.push('--glob', `!**/${device}`);
95
+ args.push('--glob', `!**/${device.toUpperCase()}`);
96
+ }
97
+ return args;
98
+ }
99
+
100
+ /**
101
+ * Tokenize query for multi-word OR matching
102
+ * Splits on whitespace and common delimiters, filters stop words and short tokens
103
+ * @param query - The search query
104
+ * @returns Array of tokens
105
+ */
106
+ function tokenizeQuery(query: string): string[] {
107
+ // Stop words for filtering (common English + programming keywords)
108
+ const stopWords = new Set([
109
+ 'the', 'a', 'an', 'is', 'are', 'was', 'were', 'be', 'been', 'being',
110
+ 'have', 'has', 'had', 'do', 'does', 'did', 'will', 'would', 'could',
111
+ 'should', 'may', 'might', 'must', 'can', 'to', 'of', 'in', 'for', 'on',
112
+ 'with', 'at', 'by', 'from', 'as', 'into', 'through', 'and', 'but', 'if',
113
+ 'or', 'not', 'this', 'that', 'these', 'those', 'it', 'its', 'how', 'what',
114
+ 'where', 'when', 'why', 'which', 'who', 'whom',
115
+ ]);
116
+
117
+ // Split on whitespace and common delimiters, keep meaningful tokens
118
+ const tokens = query
119
+ .split(/[\s,;:]+/)
120
+ .map(token => token.trim())
121
+ .filter(token => {
122
+ // Keep tokens that are:
123
+ // - At least 2 characters long
124
+ // - Not a stop word (case-insensitive)
125
+ // - Or look like identifiers (contain underscore/camelCase)
126
+ if (token.length < 2) return false;
127
+ if (stopWords.has(token.toLowerCase()) && !token.includes('_') && !/[A-Z]/.test(token)) {
128
+ return false;
129
+ }
130
+ return true;
131
+ });
132
+
133
+ return tokens;
134
+ }
135
+
136
+ /**
137
+ * Score results based on token match count for ranking
138
+ * @param results - Search results
139
+ * @param tokens - Query tokens
140
+ * @returns Results with match scores
141
+ */
142
+ function scoreByTokenMatch(results: ExactMatch[], tokens: string[]): ExactMatch[] {
143
+ if (tokens.length <= 1) return results;
144
+
145
+ // Create case-insensitive patterns for each token
146
+ const tokenPatterns = tokens.map(t => {
147
+ const escaped = t.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
148
+ return new RegExp(escaped, 'i');
149
+ });
150
+
151
+ return results.map(r => {
152
+ const content = r.content || '';
153
+ const file = r.file || '';
154
+ const searchText = `${file} ${content}`;
155
+
156
+ // Count how many tokens match
157
+ let matchCount = 0;
158
+ for (const pattern of tokenPatterns) {
159
+ if (pattern.test(searchText)) {
160
+ matchCount++;
161
+ }
162
+ }
163
+
164
+ // Calculate match ratio (0 to 1)
165
+ const matchRatio = matchCount / tokens.length;
166
+
167
+ return {
168
+ ...r,
169
+ matchScore: matchRatio,
170
+ matchCount,
171
+ };
172
+ }).sort((a, b) => {
173
+ // Sort by match ratio (descending), then by line number
174
+ if (b.matchScore !== a.matchScore) {
175
+ return b.matchScore - a.matchScore;
176
+ }
177
+ return (a.line || 0) - (b.line || 0);
178
+ });
179
+ }
180
+
181
+ interface Classification {
182
+ mode: string;
183
+ confidence: number;
184
+ reasoning: string;
185
+ }
186
+
187
+ interface ExactMatch {
188
+ file: string;
189
+ line: number;
190
+ column: number;
191
+ content: string;
192
+ matchScore?: number; // Token match ratio (0-1) for multi-word queries
193
+ matchCount?: number; // Number of tokens matched
194
+ }
195
+
196
+ interface RelationshipInfo {
197
+ type: string; // 'calls', 'imports', 'called_by', 'imported_by'
198
+ direction: 'outgoing' | 'incoming';
199
+ target?: string; // Target symbol name (for outgoing)
200
+ source?: string; // Source symbol name (for incoming)
201
+ file: string; // File path
202
+ line?: number; // Line number
203
+ }
204
+
205
+ interface SemanticMatch {
206
+ file: string;
207
+ score: number;
208
+ content: string;
209
+ symbol: string | null;
210
+ relationships?: RelationshipInfo[];
211
+ }
212
+
213
+ interface GraphMatch {
214
+ file: string;
215
+ symbols: unknown;
216
+ relationships: unknown[];
217
+ }
218
+
219
+ // File match for find_files action (path-based search)
220
+ interface FileMatch {
221
+ path: string;
222
+ type: 'file' | 'directory';
223
+ name: string; // Filename only
224
+ extension?: string; // File extension (without dot)
225
+ }
226
+
227
+ interface PaginationInfo {
228
+ offset: number; // Starting index of returned results
229
+ limit: number; // Number of results requested
230
+ total: number; // Total number of results found
231
+ has_more: boolean; // True if more results are available
232
+ }
233
+
234
+ interface SearchMetadata {
235
+ mode?: string;
236
+ backend?: string;
237
+ count?: number;
238
+ query?: string;
239
+ pattern?: string; // For find_files action
240
+ classified_as?: string;
241
+ confidence?: number;
242
+ reasoning?: string;
243
+ embeddings_coverage_percent?: number;
244
+ warning?: string;
245
+ note?: string;
246
+ index_status?: 'indexed' | 'not_indexed' | 'partial';
247
+ fallback?: string; // Fallback mode used (e.g., 'fuzzy')
248
+ fallback_history?: string[];
249
+ suggested_weights?: Record<string, number>;
250
+ // Tokenization metadata (ripgrep mode)
251
+ tokens?: string[]; // Query tokens used for multi-word search
252
+ tokenized?: boolean; // Whether tokenization was applied
253
+ // Pagination metadata
254
+ pagination?: PaginationInfo;
255
+ // Init action specific
256
+ action?: string;
257
+ path?: string;
258
+ progress?: {
259
+ stage: string;
260
+ message: string;
261
+ percent: number;
262
+ filesProcessed?: number;
263
+ totalFiles?: number;
264
+ };
265
+ progressHistory?: ProgressInfo[];
266
+ }
267
+
268
+ interface SearchResult {
269
+ success: boolean;
270
+ results?: ExactMatch[] | SemanticMatch[] | GraphMatch[] | FileMatch[] | unknown;
271
+ output?: string;
272
+ metadata?: SearchMetadata;
273
+ error?: string;
274
+ status?: unknown;
275
+ message?: string;
276
+ }
277
+
278
+ interface IndexStatus {
279
+ indexed: boolean;
280
+ has_embeddings: boolean;
281
+ file_count?: number;
282
+ embeddings_coverage_percent?: number;
283
+ warning?: string;
284
+ }
285
+
286
+ /**
287
+ * Strip ANSI color codes from string (for JSON parsing)
288
+ */
289
+ function stripAnsi(str: string): string {
290
+ return str.replace(/\x1b\[[0-9;]*m/g, '');
291
+ }
292
+
293
+ /**
294
+ * Check if CodexLens index exists for current directory
295
+ * @param path - Directory path to check
296
+ * @returns Index status
297
+ */
298
+ async function checkIndexStatus(path: string = '.'): Promise<IndexStatus> {
299
+ try {
300
+ const result = await executeCodexLens(['status', '--json'], { cwd: path });
301
+
302
+ if (!result.success) {
303
+ return {
304
+ indexed: false,
305
+ has_embeddings: false,
306
+ warning: 'No CodexLens index found. Run smart_search(action="init") to create index for better search results.',
307
+ };
308
+ }
309
+
310
+ // Parse status output
311
+ try {
312
+ // Strip ANSI color codes from JSON output
313
+ const cleanOutput = stripAnsi(result.output || '{}');
314
+ const parsed = JSON.parse(cleanOutput);
315
+ // Handle both direct and nested response formats (status returns {success, result: {...}})
316
+ const status = parsed.result || parsed;
317
+ const indexed = status.projects_count > 0 || status.total_files > 0;
318
+
319
+ // Get embeddings coverage from comprehensive status
320
+ const embeddingsData = status.embeddings || {};
321
+ const embeddingsCoverage = embeddingsData.coverage_percent || 0;
322
+ const has_embeddings = embeddingsCoverage >= 50; // Threshold: 50%
323
+
324
+ let warning: string | undefined;
325
+ if (!indexed) {
326
+ warning = 'No CodexLens index found. Run smart_search(action="init") to create index for better search results.';
327
+ } else if (embeddingsCoverage === 0) {
328
+ warning = 'Index exists but no embeddings generated. Run: codexlens embeddings-generate --recursive';
329
+ } else if (embeddingsCoverage < 50) {
330
+ warning = `Embeddings coverage is ${embeddingsCoverage.toFixed(1)}% (below 50%). Hybrid search will use exact mode. Run: codexlens embeddings-generate --recursive`;
331
+ }
332
+
333
+ return {
334
+ indexed,
335
+ has_embeddings,
336
+ file_count: status.total_files,
337
+ embeddings_coverage_percent: embeddingsCoverage,
338
+ warning,
339
+ };
340
+ } catch {
341
+ return {
342
+ indexed: false,
343
+ has_embeddings: false,
344
+ warning: 'Failed to parse index status',
345
+ };
346
+ }
347
+ } catch {
348
+ return {
349
+ indexed: false,
350
+ has_embeddings: false,
351
+ warning: 'CodexLens not available',
352
+ };
353
+ }
354
+ }
355
+
356
+ /**
357
+ * Detection heuristics for intent classification
358
+ */
359
+
360
+ /**
361
+ * Detect literal string query (simple alphanumeric or quoted strings)
362
+ */
363
+ function detectLiteral(query: string): boolean {
364
+ return /^[a-zA-Z0-9_-]+$/.test(query) || /^["'].*["']$/.test(query);
365
+ }
366
+
367
+ /**
368
+ * Detect regex pattern (contains regex metacharacters)
369
+ */
370
+ function detectRegex(query: string): boolean {
371
+ return /[.*+?^${}()|[\]\\]/.test(query);
372
+ }
373
+
374
+ /**
375
+ * Detect natural language query (sentence structure, questions, multi-word phrases)
376
+ */
377
+ function detectNaturalLanguage(query: string): boolean {
378
+ return query.split(/\s+/).length >= 3 || /\?$/.test(query);
379
+ }
380
+
381
+ /**
382
+ * Detect file path query (path separators, file extensions)
383
+ */
384
+ function detectFilePath(query: string): boolean {
385
+ return /[/\\]/.test(query) || /\.[a-z]{2,4}$/i.test(query);
386
+ }
387
+
388
+ /**
389
+ * Detect relationship query (import, export, dependency keywords)
390
+ */
391
+ function detectRelationship(query: string): boolean {
392
+ return /(import|export|uses?|depends?|calls?|extends?)\s/i.test(query);
393
+ }
394
+
395
+ function looksLikeCodeQuery(query: string): boolean {
396
+ if (/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(query)) return true;
397
+ if (/[:.<>\-=(){}[\]]/.test(query) && query.split(/\s+/).length <= 2) return true;
398
+ if (/\.\*|\\\(|\\\[|\\s/.test(query)) return true;
399
+ if (/^[a-zA-Z_][a-zA-Z0-9_]*\.[a-zA-Z_][a-zA-Z0-9_]*$/.test(query)) return true;
400
+ return false;
401
+ }
402
+
403
+ /**
404
+ * Classify query intent and recommend search mode
405
+ * Simple mapping: hybrid (NL + index + embeddings) | exact (index or insufficient embeddings) | ripgrep (no index)
406
+ * @param query - Search query string
407
+ * @param hasIndex - Whether CodexLens index exists
408
+ * @param hasSufficientEmbeddings - Whether embeddings coverage >= 50%
409
+ * @returns Classification result
410
+ */
411
+ function classifyIntent(query: string, hasIndex: boolean = false, hasSufficientEmbeddings: boolean = false): Classification {
412
+ const isNaturalLanguage = detectNaturalLanguage(query);
413
+ const isCodeQuery = looksLikeCodeQuery(query);
414
+ const isRegexPattern = detectRegex(query);
415
+
416
+ let mode: string;
417
+ let confidence: number;
418
+
419
+ if (!hasIndex) {
420
+ mode = 'ripgrep';
421
+ confidence = 1.0;
422
+ } else if (isCodeQuery || isRegexPattern) {
423
+ mode = 'exact';
424
+ confidence = 0.95;
425
+ } else if (isNaturalLanguage && hasSufficientEmbeddings) {
426
+ mode = 'hybrid';
427
+ confidence = 0.9;
428
+ } else {
429
+ mode = 'exact';
430
+ confidence = 0.8;
431
+ }
432
+
433
+ const detectedPatterns: string[] = [];
434
+ if (detectLiteral(query)) detectedPatterns.push('literal');
435
+ if (detectRegex(query)) detectedPatterns.push('regex');
436
+ if (detectNaturalLanguage(query)) detectedPatterns.push('natural language');
437
+ if (detectFilePath(query)) detectedPatterns.push('file path');
438
+ if (detectRelationship(query)) detectedPatterns.push('relationship');
439
+ if (isCodeQuery) detectedPatterns.push('code identifier');
440
+
441
+ const reasoning = `Query classified as ${mode} (confidence: ${confidence.toFixed(2)}, detected: ${detectedPatterns.join(', ')}, index: ${hasIndex ? 'available' : 'not available'}, embeddings: ${hasSufficientEmbeddings ? 'sufficient' : 'insufficient'})`;
442
+
443
+ return { mode, confidence, reasoning };
444
+ }
445
+
446
+ /**
447
+ * Check if a tool is available in PATH
448
+ * @param toolName - Tool executable name
449
+ * @returns True if available
450
+ */
451
+ function checkToolAvailability(toolName: string): boolean {
452
+ try {
453
+ const isWindows = process.platform === 'win32';
454
+ const command = isWindows ? 'where' : 'which';
455
+ execSync(`${command} ${toolName}`, { stdio: 'ignore' });
456
+ return true;
457
+ } catch {
458
+ return false;
459
+ }
460
+ }
461
+
462
+ /**
463
+ * Build ripgrep command arguments
464
+ * Supports tokenized multi-word queries with OR matching
465
+ * @param params - Search parameters
466
+ * @returns Command, arguments, and tokens used
467
+ */
468
+ function buildRipgrepCommand(params: {
469
+ query: string;
470
+ paths: string[];
471
+ contextLines: number;
472
+ maxResults: number;
473
+ includeHidden: boolean;
474
+ regex?: boolean;
475
+ caseSensitive?: boolean;
476
+ tokenize?: boolean;
477
+ }): { command: string; args: string[]; tokens: string[] } {
478
+ const { query, paths = ['.'], contextLines = 0, maxResults = 10, includeHidden = false, regex = false, caseSensitive = true, tokenize = true } = params;
479
+
480
+ const args = [
481
+ '-n',
482
+ '--color=never',
483
+ '--json',
484
+ ];
485
+
486
+ // Add file filtering (unless includeHidden is true)
487
+ if (!includeHidden) {
488
+ args.push(...buildExcludeArgs());
489
+ }
490
+
491
+ // Case sensitivity
492
+ if (!caseSensitive) {
493
+ args.push('--ignore-case');
494
+ }
495
+
496
+ if (contextLines > 0) {
497
+ args.push('-C', contextLines.toString());
498
+ }
499
+
500
+ if (maxResults > 0) {
501
+ args.push('--max-count', maxResults.toString());
502
+ }
503
+
504
+ if (includeHidden) {
505
+ args.push('--hidden');
506
+ }
507
+
508
+ // Tokenize query for multi-word OR matching
509
+ const tokens = tokenize ? tokenizeQuery(query) : [query];
510
+
511
+ if (tokens.length > 1) {
512
+ // Multi-token: use multiple -e patterns (OR matching)
513
+ // Each token is escaped for regex safety unless regex mode is enabled
514
+ for (const token of tokens) {
515
+ if (regex) {
516
+ args.push('-e', token);
517
+ } else {
518
+ // Escape regex special chars for literal matching
519
+ const escaped = token.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
520
+ args.push('-e', escaped);
521
+ }
522
+ }
523
+ } else {
524
+ // Single token or no tokenization: use original behavior
525
+ if (regex) {
526
+ args.push('-e', query);
527
+ } else {
528
+ args.push('-F', query);
529
+ }
530
+ }
531
+
532
+ args.push(...paths);
533
+
534
+ return { command: 'rg', args, tokens };
535
+ }
536
+
537
+ /**
538
+ * Action: init - Initialize CodexLens index (FTS only, no embeddings)
539
+ * For semantic/vector search, use ccw view dashboard or codexlens CLI directly
540
+ */
541
+ async function executeInitAction(params: Params): Promise<SearchResult> {
542
+ const { path = '.', languages } = params;
543
+
544
+ // Check CodexLens availability
545
+ const readyStatus = await ensureCodexLensReady();
546
+ if (!readyStatus.ready) {
547
+ return {
548
+ success: false,
549
+ error: `CodexLens not available: ${readyStatus.error}. CodexLens will be auto-installed on first use.`,
550
+ };
551
+ }
552
+
553
+ // Build args with --no-embeddings for FTS-only index (faster)
554
+ const args = ['init', path, '--no-embeddings'];
555
+ if (languages && languages.length > 0) {
556
+ args.push('--languages', languages.join(','));
557
+ }
558
+
559
+ // Track progress updates
560
+ const progressUpdates: ProgressInfo[] = [];
561
+ let lastProgress: ProgressInfo | null = null;
562
+
563
+ const result = await executeCodexLens(args, {
564
+ cwd: path,
565
+ timeout: 1800000, // 30 minutes for large codebases
566
+ onProgress: (progress: ProgressInfo) => {
567
+ progressUpdates.push(progress);
568
+ lastProgress = progress;
569
+ },
570
+ });
571
+
572
+ // Build metadata with progress info
573
+ const metadata: SearchMetadata = {
574
+ action: 'init',
575
+ path,
576
+ };
577
+
578
+ if (lastProgress !== null) {
579
+ const p = lastProgress as ProgressInfo;
580
+ metadata.progress = {
581
+ stage: p.stage,
582
+ message: p.message,
583
+ percent: p.percent,
584
+ filesProcessed: p.filesProcessed,
585
+ totalFiles: p.totalFiles,
586
+ };
587
+ }
588
+
589
+ if (progressUpdates.length > 0) {
590
+ metadata.progressHistory = progressUpdates.slice(-5); // Keep last 5 progress updates
591
+ }
592
+
593
+ const successMessage = result.success
594
+ ? `FTS index created for ${path}. Note: For semantic/vector search, create vector index via "ccw view" dashboard or run "codexlens init ${path}" (without --no-embeddings).`
595
+ : undefined;
596
+
597
+ return {
598
+ success: result.success,
599
+ error: result.error,
600
+ message: successMessage,
601
+ metadata,
602
+ };
603
+ }
604
+
605
+ /**
606
+ * Action: status - Check CodexLens index status
607
+ */
608
+ async function executeStatusAction(params: Params): Promise<SearchResult> {
609
+ const { path = '.' } = params;
610
+
611
+ const indexStatus = await checkIndexStatus(path);
612
+
613
+ return {
614
+ success: true,
615
+ status: indexStatus,
616
+ message: indexStatus.warning || `Index status: ${indexStatus.indexed ? 'indexed' : 'not indexed'}, embeddings: ${indexStatus.has_embeddings ? 'available' : 'not available'}`,
617
+ };
618
+ }
619
+
620
+ /**
621
+ * Mode: auto - Intent classification and mode selection
622
+ * Routes to: hybrid (NL + index) | exact (index) | ripgrep (no index)
623
+ */
624
+ async function executeAutoMode(params: Params): Promise<SearchResult> {
625
+ const { query, path = '.' } = params;
626
+
627
+ if (!query) {
628
+ return {
629
+ success: false,
630
+ error: 'Query is required for search action',
631
+ };
632
+ }
633
+
634
+ // Check index status
635
+ const indexStatus = await checkIndexStatus(path);
636
+
637
+ // Classify intent with index and embeddings awareness
638
+ const classification = classifyIntent(
639
+ query,
640
+ indexStatus.indexed,
641
+ indexStatus.has_embeddings // This now considers 50% threshold
642
+ );
643
+
644
+ // Route to appropriate mode based on classification
645
+ let result: SearchResult;
646
+
647
+ switch (classification.mode) {
648
+ case 'hybrid':
649
+ result = await executeHybridMode(params);
650
+ break;
651
+
652
+ case 'exact':
653
+ result = await executeCodexLensExactMode(params);
654
+ break;
655
+
656
+ case 'ripgrep':
657
+ result = await executeRipgrepMode(params);
658
+ break;
659
+
660
+ default:
661
+ // Fallback to ripgrep
662
+ result = await executeRipgrepMode(params);
663
+ break;
664
+ }
665
+
666
+ // Add classification metadata
667
+ if (result.metadata) {
668
+ result.metadata.classified_as = classification.mode;
669
+ result.metadata.confidence = classification.confidence;
670
+ result.metadata.reasoning = classification.reasoning;
671
+ result.metadata.embeddings_coverage_percent = indexStatus.embeddings_coverage_percent;
672
+ result.metadata.index_status = indexStatus.indexed
673
+ ? (indexStatus.has_embeddings ? 'indexed' : 'partial')
674
+ : 'not_indexed';
675
+
676
+ // Add warning if needed
677
+ if (indexStatus.warning) {
678
+ result.metadata.warning = indexStatus.warning;
679
+ }
680
+ }
681
+
682
+ return result;
683
+ }
684
+
685
+ /**
686
+ * Mode: ripgrep - Fast literal string matching using ripgrep
687
+ * No index required, fallback to CodexLens if ripgrep unavailable
688
+ * Supports tokenized multi-word queries with OR matching and result ranking
689
+ */
690
+ async function executeRipgrepMode(params: Params): Promise<SearchResult> {
691
+ const { query, paths = [], contextLines = 0, maxResults = 10, includeHidden = false, path = '.', regex = true, caseSensitive = true, tokenize = true } = params;
692
+
693
+ if (!query) {
694
+ return {
695
+ success: false,
696
+ error: 'Query is required for search',
697
+ };
698
+ }
699
+
700
+ // Check if ripgrep is available
701
+ const hasRipgrep = checkToolAvailability('rg');
702
+
703
+ // If ripgrep not available, fall back to CodexLens exact mode
704
+ if (!hasRipgrep) {
705
+ const readyStatus = await ensureCodexLensReady();
706
+ if (!readyStatus.ready) {
707
+ return {
708
+ success: false,
709
+ error: 'Neither ripgrep nor CodexLens available. Install ripgrep (rg) or CodexLens for search functionality.',
710
+ };
711
+ }
712
+
713
+ // Use CodexLens exact mode as fallback
714
+ const args = ['search', query, '--limit', maxResults.toString(), '--mode', 'exact', '--json'];
715
+ const result = await executeCodexLens(args, { cwd: path });
716
+
717
+ if (!result.success) {
718
+ return {
719
+ success: false,
720
+ error: result.error,
721
+ metadata: {
722
+ mode: 'ripgrep',
723
+ backend: 'codexlens-fallback',
724
+ count: 0,
725
+ query,
726
+ },
727
+ };
728
+ }
729
+
730
+ // Parse results
731
+ let results: SemanticMatch[] = [];
732
+ try {
733
+ const parsed = JSON.parse(stripAnsi(result.output || '{}'));
734
+ const data = parsed.result?.results || parsed.results || parsed;
735
+ results = (Array.isArray(data) ? data : []).map((item: any) => ({
736
+ file: item.path || item.file,
737
+ score: item.score || 0,
738
+ content: item.excerpt || item.content || '',
739
+ symbol: item.symbol || null,
740
+ }));
741
+ } catch {
742
+ // Keep empty results
743
+ }
744
+
745
+ return {
746
+ success: true,
747
+ results,
748
+ metadata: {
749
+ mode: 'ripgrep',
750
+ backend: 'codexlens-fallback',
751
+ count: results.length,
752
+ query,
753
+ note: 'Using CodexLens exact mode (ripgrep not available)',
754
+ },
755
+ };
756
+ }
757
+
758
+ // Use ripgrep
759
+ const { command, args, tokens } = buildRipgrepCommand({
760
+ query,
761
+ paths: paths.length > 0 ? paths : [path],
762
+ contextLines,
763
+ maxResults,
764
+ includeHidden,
765
+ regex,
766
+ caseSensitive,
767
+ tokenize,
768
+ });
769
+
770
+ return new Promise((resolve) => {
771
+ const child = spawn(command, args, {
772
+ cwd: path || getProjectRoot(),
773
+ stdio: ['ignore', 'pipe', 'pipe'],
774
+ });
775
+
776
+ let stdout = '';
777
+ let stderr = '';
778
+ let resultLimitReached = false;
779
+
780
+ child.stdout.on('data', (data) => {
781
+ stdout += data.toString();
782
+ });
783
+
784
+ child.stderr.on('data', (data) => {
785
+ stderr += data.toString();
786
+ });
787
+
788
+ child.on('close', (code) => {
789
+ const results: ExactMatch[] = [];
790
+ const lines = stdout.split('\n').filter((line) => line.trim());
791
+ // Limit total results to prevent memory overflow (--max-count only limits per-file)
792
+ const effectiveLimit = maxResults > 0 ? maxResults : 500;
793
+
794
+ for (const line of lines) {
795
+ // Stop collecting if we've reached the limit
796
+ if (results.length >= effectiveLimit) {
797
+ resultLimitReached = true;
798
+ break;
799
+ }
800
+
801
+ try {
802
+ const item = JSON.parse(line);
803
+
804
+ if (item.type === 'match') {
805
+ const match: ExactMatch = {
806
+ file: item.data.path.text,
807
+ line: item.data.line_number,
808
+ column:
809
+ item.data.submatches && item.data.submatches[0]
810
+ ? item.data.submatches[0].start + 1
811
+ : 1,
812
+ content: item.data.lines.text.trim(),
813
+ };
814
+ results.push(match);
815
+ }
816
+ } catch {
817
+ continue;
818
+ }
819
+ }
820
+
821
+ // Handle Windows device file errors gracefully (os error 1)
822
+ // If we have results despite the error, return them as partial success
823
+ const isWindowsDeviceError = stderr.includes('os error 1') || stderr.includes('函数不正确');
824
+
825
+ // Apply token-based scoring and sorting for multi-word queries
826
+ // Results matching more tokens are ranked higher (exact matches first)
827
+ const scoredResults = tokens.length > 1 ? scoreByTokenMatch(results, tokens) : results;
828
+
829
+ if (code === 0 || code === 1 || (isWindowsDeviceError && scoredResults.length > 0)) {
830
+ // Build warning message for various conditions
831
+ const warnings: string[] = [];
832
+ if (resultLimitReached) {
833
+ warnings.push(`Result limit reached (${effectiveLimit}). Use a more specific query or increase limit.`);
834
+ }
835
+ if (isWindowsDeviceError) {
836
+ warnings.push('Some Windows device files were skipped');
837
+ }
838
+
839
+ resolve({
840
+ success: true,
841
+ results: scoredResults,
842
+ metadata: {
843
+ mode: 'ripgrep',
844
+ backend: 'ripgrep',
845
+ count: scoredResults.length,
846
+ query,
847
+ tokens: tokens.length > 1 ? tokens : undefined, // Include tokens in metadata for debugging
848
+ tokenized: tokens.length > 1,
849
+ ...(warnings.length > 0 && { warning: warnings.join('; ') }),
850
+ },
851
+ });
852
+ } else if (isWindowsDeviceError && results.length === 0) {
853
+ // Windows device error but no results - might be the only issue
854
+ resolve({
855
+ success: true,
856
+ results: [],
857
+ metadata: {
858
+ mode: 'ripgrep',
859
+ backend: 'ripgrep',
860
+ count: 0,
861
+ query,
862
+ warning: 'No matches found (some Windows device files were skipped)',
863
+ },
864
+ });
865
+ } else {
866
+ resolve({
867
+ success: false,
868
+ error: `ripgrep execution failed with code ${code}: ${stderr}`,
869
+ results: [],
870
+ });
871
+ }
872
+ });
873
+
874
+ child.on('error', (error) => {
875
+ resolve({
876
+ success: false,
877
+ error: `Failed to spawn ripgrep: ${error.message}`,
878
+ results: [],
879
+ });
880
+ });
881
+ });
882
+ }
883
+
884
+ /**
885
+ * Mode: exact - CodexLens exact/FTS search
886
+ * Requires index
887
+ */
888
+ async function executeCodexLensExactMode(params: Params): Promise<SearchResult> {
889
+ const { query, path = '.', maxResults = 10, enrich = false } = params;
890
+
891
+ if (!query) {
892
+ return {
893
+ success: false,
894
+ error: 'Query is required for search',
895
+ };
896
+ }
897
+
898
+ // Check CodexLens availability
899
+ const readyStatus = await ensureCodexLensReady();
900
+ if (!readyStatus.ready) {
901
+ return {
902
+ success: false,
903
+ error: `CodexLens not available: ${readyStatus.error}`,
904
+ };
905
+ }
906
+
907
+ // Check index status
908
+ const indexStatus = await checkIndexStatus(path);
909
+
910
+ const args = ['search', query, '--limit', maxResults.toString(), '--mode', 'exact', '--json'];
911
+ if (enrich) {
912
+ args.push('--enrich');
913
+ }
914
+ const result = await executeCodexLens(args, { cwd: path });
915
+
916
+ if (!result.success) {
917
+ return {
918
+ success: false,
919
+ error: result.error,
920
+ metadata: {
921
+ mode: 'exact',
922
+ backend: 'codexlens',
923
+ count: 0,
924
+ query,
925
+ warning: indexStatus.warning,
926
+ },
927
+ };
928
+ }
929
+
930
+ // Parse results
931
+ let results: SemanticMatch[] = [];
932
+ try {
933
+ const parsed = JSON.parse(stripAnsi(result.output || '{}'));
934
+ const data = parsed.result?.results || parsed.results || parsed;
935
+ results = (Array.isArray(data) ? data : []).map((item: any) => ({
936
+ file: item.path || item.file,
937
+ score: item.score || 0,
938
+ content: item.excerpt || item.content || '',
939
+ symbol: item.symbol || null,
940
+ }));
941
+ } catch {
942
+ // Keep empty results
943
+ }
944
+
945
+ // Fallback to fuzzy mode if exact returns no results
946
+ if (results.length === 0) {
947
+ const fuzzyArgs = ['search', query, '--limit', maxResults.toString(), '--mode', 'fuzzy', '--json'];
948
+ if (enrich) {
949
+ fuzzyArgs.push('--enrich');
950
+ }
951
+ const fuzzyResult = await executeCodexLens(fuzzyArgs, { cwd: path });
952
+
953
+ if (fuzzyResult.success) {
954
+ try {
955
+ const parsed = JSON.parse(stripAnsi(fuzzyResult.output || '{}'));
956
+ const data = parsed.result?.results || parsed.results || parsed;
957
+ results = (Array.isArray(data) ? data : []).map((item: any) => ({
958
+ file: item.path || item.file,
959
+ score: item.score || 0,
960
+ content: item.excerpt || item.content || '',
961
+ symbol: item.symbol || null,
962
+ }));
963
+ } catch {
964
+ // Keep empty results
965
+ }
966
+
967
+ if (results.length > 0) {
968
+ return {
969
+ success: true,
970
+ results,
971
+ metadata: {
972
+ mode: 'exact',
973
+ backend: 'codexlens',
974
+ count: results.length,
975
+ query,
976
+ warning: indexStatus.warning,
977
+ note: 'No exact matches found, showing fuzzy results',
978
+ fallback: 'fuzzy',
979
+ },
980
+ };
981
+ }
982
+ }
983
+ }
984
+
985
+ return {
986
+ success: true,
987
+ results,
988
+ metadata: {
989
+ mode: 'exact',
990
+ backend: 'codexlens',
991
+ count: results.length,
992
+ query,
993
+ warning: indexStatus.warning,
994
+ },
995
+ };
996
+ }
997
+
998
+ /**
999
+ * Mode: hybrid - Best quality search with RRF fusion
1000
+ * Uses CodexLens hybrid mode (exact + fuzzy + vector)
1001
+ * Requires index with embeddings
1002
+ */
1003
+ async function executeHybridMode(params: Params): Promise<SearchResult> {
1004
+ const { query, path = '.', maxResults = 10, enrich = false } = params;
1005
+
1006
+ if (!query) {
1007
+ return {
1008
+ success: false,
1009
+ error: 'Query is required for search',
1010
+ };
1011
+ }
1012
+
1013
+ // Check CodexLens availability
1014
+ const readyStatus = await ensureCodexLensReady();
1015
+ if (!readyStatus.ready) {
1016
+ return {
1017
+ success: false,
1018
+ error: `CodexLens not available: ${readyStatus.error}`,
1019
+ };
1020
+ }
1021
+
1022
+ // Check index status
1023
+ const indexStatus = await checkIndexStatus(path);
1024
+
1025
+ const args = ['search', query, '--limit', maxResults.toString(), '--mode', 'hybrid', '--json'];
1026
+ if (enrich) {
1027
+ args.push('--enrich');
1028
+ }
1029
+ const result = await executeCodexLens(args, { cwd: path });
1030
+
1031
+ if (!result.success) {
1032
+ return {
1033
+ success: false,
1034
+ error: result.error,
1035
+ metadata: {
1036
+ mode: 'hybrid',
1037
+ backend: 'codexlens',
1038
+ count: 0,
1039
+ query,
1040
+ warning: indexStatus.warning,
1041
+ },
1042
+ };
1043
+ }
1044
+
1045
+ // Parse results
1046
+ let results: SemanticMatch[] = [];
1047
+ let baselineInfo: { score: number; count: number } | null = null;
1048
+ let initialCount = 0;
1049
+
1050
+ try {
1051
+ const parsed = JSON.parse(stripAnsi(result.output || '{}'));
1052
+ const data = parsed.result?.results || parsed.results || parsed;
1053
+ results = (Array.isArray(data) ? data : []).map((item: any) => {
1054
+ const rawScore = item.score || 0;
1055
+ // Hybrid mode returns distance scores (lower is better).
1056
+ // Convert to similarity scores (higher is better) for consistency.
1057
+ // Formula: similarity = 1 / (1 + distance)
1058
+ const similarityScore = rawScore > 0 ? 1 / (1 + rawScore) : 1;
1059
+ return {
1060
+ file: item.path || item.file,
1061
+ score: similarityScore,
1062
+ content: item.excerpt || item.content || '',
1063
+ symbol: item.symbol || null,
1064
+ };
1065
+ });
1066
+
1067
+ initialCount = results.length;
1068
+
1069
+ // Post-processing pipeline to improve semantic search quality
1070
+ // 0. Filter dominant baseline scores (hot spot detection)
1071
+ const baselineResult = filterDominantBaselineScores(results);
1072
+ results = baselineResult.filteredResults;
1073
+ baselineInfo = baselineResult.baselineInfo;
1074
+
1075
+ // 1. Filter noisy files (coverage, node_modules, etc.)
1076
+ results = filterNoisyFiles(results);
1077
+ // 2. Boost results containing query keywords
1078
+ results = applyKeywordBoosting(results, query);
1079
+ // 3. Enforce score diversity (penalize identical scores)
1080
+ results = enforceScoreDiversity(results);
1081
+ // 4. Re-sort by adjusted scores
1082
+ results.sort((a, b) => b.score - a.score);
1083
+ } catch {
1084
+ return {
1085
+ success: true,
1086
+ results: [],
1087
+ output: result.output,
1088
+ metadata: {
1089
+ mode: 'hybrid',
1090
+ backend: 'codexlens',
1091
+ count: 0,
1092
+ query,
1093
+ warning: indexStatus.warning || 'Failed to parse JSON output',
1094
+ },
1095
+ };
1096
+ }
1097
+
1098
+ // Build metadata with baseline info if detected
1099
+ let note = 'Hybrid mode uses RRF fusion (exact + fuzzy + vector) for best results';
1100
+ if (baselineInfo) {
1101
+ note += ` | Filtered ${initialCount - results.length} hot-spot results with baseline score ~${baselineInfo.score.toFixed(4)}`;
1102
+ }
1103
+
1104
+ return {
1105
+ success: true,
1106
+ results,
1107
+ metadata: {
1108
+ mode: 'hybrid',
1109
+ backend: 'codexlens',
1110
+ count: results.length,
1111
+ query,
1112
+ note,
1113
+ warning: indexStatus.warning,
1114
+ suggested_weights: getRRFWeights(query),
1115
+ },
1116
+ };
1117
+ }
1118
+
1119
+ const RRF_WEIGHTS = {
1120
+ code: { exact: 0.7, fuzzy: 0.2, vector: 0.1 },
1121
+ natural: { exact: 0.4, fuzzy: 0.2, vector: 0.4 },
1122
+ default: { exact: 0.5, fuzzy: 0.2, vector: 0.3 },
1123
+ };
1124
+
1125
+ function getRRFWeights(query: string): Record<string, number> {
1126
+ const isCode = looksLikeCodeQuery(query);
1127
+ const isNatural = detectNaturalLanguage(query);
1128
+ if (isCode) return RRF_WEIGHTS.code;
1129
+ if (isNatural) return RRF_WEIGHTS.natural;
1130
+ return RRF_WEIGHTS.default;
1131
+ }
1132
+
1133
+ /**
1134
+ * Post-processing: Filter noisy files from semantic search results
1135
+ * Uses FILTER_CONFIG patterns to remove irrelevant files.
1136
+ * Optimized: pre-compiled regexes, accurate path segment matching.
1137
+ */
1138
+ // Pre-compile file exclusion regexes once (avoid recompilation in loop)
1139
+ const FILE_EXCLUDE_REGEXES = [...FILTER_CONFIG.exclude_files].map(pattern =>
1140
+ new RegExp('^' + pattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&').replace(/\\\*/g, '.*') + '$')
1141
+ );
1142
+
1143
+ function filterNoisyFiles(results: SemanticMatch[]): SemanticMatch[] {
1144
+ return results.filter(r => {
1145
+ const filePath = r.file || '';
1146
+ if (!filePath) return true;
1147
+
1148
+ const segments = filePath.split(/[/\\]/);
1149
+
1150
+ // Accurate directory check: segment must exactly match excluded directory
1151
+ if (segments.some(segment => FILTER_CONFIG.exclude_directories.has(segment))) {
1152
+ return false;
1153
+ }
1154
+
1155
+ // Accurate file check: pattern matches filename only (not full path)
1156
+ const filename = segments.pop() || '';
1157
+ if (FILE_EXCLUDE_REGEXES.some(regex => regex.test(filename))) {
1158
+ return false;
1159
+ }
1160
+
1161
+ return true;
1162
+ });
1163
+ }
1164
+
1165
+ /**
1166
+ * Post-processing: Boost results containing query keywords
1167
+ * Extracts keywords from query and boosts matching results.
1168
+ * Optimized: uses whole-word matching with regex for accuracy.
1169
+ */
1170
+ // Helper to escape regex special characters
1171
+ function escapeRegExp(str: string): string {
1172
+ return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
1173
+ }
1174
+
1175
+ function applyKeywordBoosting(results: SemanticMatch[], query: string): SemanticMatch[] {
1176
+ // Extract meaningful keywords (ignore common words)
1177
+ const stopWords = new Set(['the', 'a', 'an', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'do', 'does', 'did', 'will', 'would', 'could', 'should', 'may', 'might', 'must', 'shall', 'can', 'need', 'dare', 'ought', 'used', 'to', 'of', 'in', 'for', 'on', 'with', 'at', 'by', 'from', 'as', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'between', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 'just', 'and', 'but', 'if', 'or', 'because', 'until', 'while', 'although', 'though', 'after', 'before', 'when', 'whenever', 'where', 'wherever', 'whether', 'which', 'who', 'whom', 'whose', 'what', 'whatever', 'whichever', 'whoever', 'whomever', 'this', 'that', 'these', 'those', 'it', 'its']);
1178
+
1179
+ const keywords = query
1180
+ .toLowerCase()
1181
+ .split(/[\s,.;:()"{}[\]-]+/) // More robust splitting on punctuation
1182
+ .filter(word => word.length > 2 && !stopWords.has(word));
1183
+
1184
+ if (keywords.length === 0) return results;
1185
+
1186
+ // Create case-insensitive regexes for whole-word matching
1187
+ const keywordRegexes = keywords.map(kw => new RegExp(`\\b${escapeRegExp(kw)}\\b`, 'i'));
1188
+
1189
+ return results.map(r => {
1190
+ const content = r.content || '';
1191
+ const file = r.file || '';
1192
+
1193
+ // Count keyword matches using whole-word regex
1194
+ let matchCount = 0;
1195
+ for (const regex of keywordRegexes) {
1196
+ if (regex.test(content) || regex.test(file)) {
1197
+ matchCount++;
1198
+ }
1199
+ }
1200
+
1201
+ // Apply boost only if there are matches
1202
+ if (matchCount > 0) {
1203
+ const matchRatio = matchCount / keywords.length;
1204
+ const boost = 1 + (matchRatio * 0.3); // Up to 30% boost for full match
1205
+ return {
1206
+ ...r,
1207
+ score: r.score * boost,
1208
+ };
1209
+ }
1210
+
1211
+ return r;
1212
+ });
1213
+ }
1214
+
1215
+ /**
1216
+ * Post-processing: Enforce score diversity
1217
+ * Penalizes results with identical scores (indicates undifferentiated matching)
1218
+ */
1219
+ function enforceScoreDiversity(results: SemanticMatch[]): SemanticMatch[] {
1220
+ if (results.length < 2) return results;
1221
+
1222
+ // Count occurrences of each score (rounded to 3 decimal places for comparison)
1223
+ const scoreCounts = new Map<number, number>();
1224
+ for (const r of results) {
1225
+ const roundedScore = Math.round(r.score * 1000) / 1000;
1226
+ scoreCounts.set(roundedScore, (scoreCounts.get(roundedScore) || 0) + 1);
1227
+ }
1228
+
1229
+ // Apply penalty to scores that appear more than twice
1230
+ return results.map(r => {
1231
+ const roundedScore = Math.round(r.score * 1000) / 1000;
1232
+ const count = scoreCounts.get(roundedScore) || 1;
1233
+
1234
+ if (count > 2) {
1235
+ // Progressive penalty: more duplicates = bigger penalty
1236
+ const penalty = Math.max(0.7, 1 - (count * 0.05));
1237
+ return { ...r, score: r.score * penalty };
1238
+ }
1239
+ return r;
1240
+ });
1241
+ }
1242
+
1243
+ /**
1244
+ * Post-processing: Filter results with dominant baseline score (hot spot detection)
1245
+ * When backend returns default "hot spot" files with identical high scores,
1246
+ * this function detects and removes them.
1247
+ *
1248
+ * Detection criteria:
1249
+ * - A single score appears in >50% of results
1250
+ * - That score is suspiciously high (>0.9)
1251
+ * - This indicates fallback mechanism returned placeholder results
1252
+ */
1253
+ function filterDominantBaselineScores(
1254
+ results: SemanticMatch[]
1255
+ ): { filteredResults: SemanticMatch[]; baselineInfo: { score: number; count: number } | null } {
1256
+ if (results.length < 4) {
1257
+ return { filteredResults: results, baselineInfo: null };
1258
+ }
1259
+
1260
+ // Count occurrences of each score (rounded to 4 decimal places)
1261
+ const scoreCounts = new Map<number, number>();
1262
+ results.forEach(r => {
1263
+ const rounded = Math.round(r.score * 10000) / 10000;
1264
+ scoreCounts.set(rounded, (scoreCounts.get(rounded) || 0) + 1);
1265
+ });
1266
+
1267
+ // Find the most dominant score
1268
+ let dominantScore: number | null = null;
1269
+ let dominantCount = 0;
1270
+ scoreCounts.forEach((count, score) => {
1271
+ if (count > dominantCount) {
1272
+ dominantCount = count;
1273
+ dominantScore = score;
1274
+ }
1275
+ });
1276
+
1277
+ // If a single score is present in >50% of results and is high (>0.9),
1278
+ // treat it as a suspicious baseline score and filter it out
1279
+ const BASELINE_THRESHOLD = 0.5; // >50% of results have same score
1280
+ const HIGH_SCORE_THRESHOLD = 0.9; // Score above 0.9 is suspiciously high
1281
+
1282
+ if (
1283
+ dominantScore !== null &&
1284
+ dominantCount > results.length * BASELINE_THRESHOLD &&
1285
+ dominantScore > HIGH_SCORE_THRESHOLD
1286
+ ) {
1287
+ const filteredResults = results.filter(r => {
1288
+ const rounded = Math.round(r.score * 10000) / 10000;
1289
+ return rounded !== dominantScore;
1290
+ });
1291
+
1292
+ return {
1293
+ filteredResults,
1294
+ baselineInfo: { score: dominantScore, count: dominantCount },
1295
+ };
1296
+ }
1297
+
1298
+ return { filteredResults: results, baselineInfo: null };
1299
+ }
1300
+
1301
+ /**
1302
+ * TypeScript implementation of Reciprocal Rank Fusion
1303
+ * Reference: codex-lens/src/codexlens/search/ranking.py
1304
+ * Formula: score(d) = Σ weight_source / (k + rank_source(d))
1305
+ */
1306
+ function applyRRFFusion(
1307
+ resultsMap: Map<string, any[]>,
1308
+ weights: Record<string, number>,
1309
+ limit: number,
1310
+ k: number = 60,
1311
+ ): any[] {
1312
+ const pathScores = new Map<string, { score: number; result: any; sources: string[] }>();
1313
+
1314
+ resultsMap.forEach((results, source) => {
1315
+ const weight = weights[source] || 0;
1316
+ if (weight === 0 || !results) return;
1317
+
1318
+ results.forEach((result, rank) => {
1319
+ const path = result.file || result.path;
1320
+ if (!path) return;
1321
+
1322
+ const rrfContribution = weight / (k + rank + 1);
1323
+
1324
+ if (!pathScores.has(path)) {
1325
+ pathScores.set(path, { score: 0, result, sources: [] });
1326
+ }
1327
+ const entry = pathScores.get(path)!;
1328
+ entry.score += rrfContribution;
1329
+ if (!entry.sources.includes(source)) {
1330
+ entry.sources.push(source);
1331
+ }
1332
+ });
1333
+ });
1334
+
1335
+ // Sort by fusion score descending
1336
+ return Array.from(pathScores.values())
1337
+ .sort((a, b) => b.score - a.score)
1338
+ .slice(0, limit)
1339
+ .map(item => ({
1340
+ ...item.result,
1341
+ fusion_score: item.score,
1342
+ matched_backends: item.sources,
1343
+ }));
1344
+ }
1345
+
1346
+ /**
1347
+ * Promise wrapper with timeout support
1348
+ * @param promise - The promise to wrap
1349
+ * @param ms - Timeout in milliseconds
1350
+ * @param modeName - Name of the mode for error message
1351
+ * @returns A new promise that rejects on timeout
1352
+ */
1353
+ function withTimeout<T>(promise: Promise<T>, ms: number, modeName: string): Promise<T> {
1354
+ return new Promise((resolve, reject) => {
1355
+ const timer = setTimeout(() => {
1356
+ reject(new Error(`'${modeName}' search timed out after ${ms}ms`));
1357
+ }, ms);
1358
+
1359
+ promise
1360
+ .then(resolve)
1361
+ .catch(reject)
1362
+ .finally(() => clearTimeout(timer));
1363
+ });
1364
+ }
1365
+
1366
+ /**
1367
+ * Mode: priority - Fallback search strategy: hybrid -> exact -> ripgrep
1368
+ * Returns results from the first backend that succeeds and provides results.
1369
+ * More efficient than parallel mode - stops as soon as valid results are found.
1370
+ */
1371
+ async function executePriorityFallbackMode(params: Params): Promise<SearchResult> {
1372
+ const { query, path = '.' } = params;
1373
+ const fallbackHistory: string[] = [];
1374
+
1375
+ if (!query) {
1376
+ return { success: false, error: 'Query is required for search' };
1377
+ }
1378
+
1379
+ // Check index status first
1380
+ const indexStatus = await checkIndexStatus(path);
1381
+
1382
+ // 1. Try Hybrid search (highest priority) - 90s timeout for large indexes
1383
+ if (indexStatus.indexed && indexStatus.has_embeddings) {
1384
+ try {
1385
+ const hybridResult = await withTimeout(executeHybridMode(params), 90000, 'hybrid');
1386
+ if (hybridResult.success && hybridResult.results && (hybridResult.results as any[]).length > 0) {
1387
+ fallbackHistory.push('hybrid: success');
1388
+ return {
1389
+ ...hybridResult,
1390
+ metadata: {
1391
+ ...hybridResult.metadata,
1392
+ mode: 'priority',
1393
+ note: 'Result from hybrid search (semantic + vector).',
1394
+ fallback_history: fallbackHistory,
1395
+ },
1396
+ };
1397
+ }
1398
+ fallbackHistory.push('hybrid: no results');
1399
+ } catch (error) {
1400
+ fallbackHistory.push(`hybrid: ${(error as Error).message}`);
1401
+ }
1402
+ } else {
1403
+ fallbackHistory.push(`hybrid: skipped (${!indexStatus.indexed ? 'no index' : 'no embeddings'})`);
1404
+ }
1405
+
1406
+ // 2. Fallback to Exact search - 10s timeout
1407
+ if (indexStatus.indexed) {
1408
+ try {
1409
+ const exactResult = await withTimeout(executeCodexLensExactMode(params), 10000, 'exact');
1410
+ if (exactResult.success && exactResult.results && (exactResult.results as any[]).length > 0) {
1411
+ fallbackHistory.push('exact: success');
1412
+ return {
1413
+ ...exactResult,
1414
+ metadata: {
1415
+ ...exactResult.metadata,
1416
+ mode: 'priority',
1417
+ note: 'Result from exact/FTS search (fallback from hybrid).',
1418
+ fallback_history: fallbackHistory,
1419
+ },
1420
+ };
1421
+ }
1422
+ fallbackHistory.push('exact: no results');
1423
+ } catch (error) {
1424
+ fallbackHistory.push(`exact: ${(error as Error).message}`);
1425
+ }
1426
+ } else {
1427
+ fallbackHistory.push('exact: skipped (no index)');
1428
+ }
1429
+
1430
+ // 3. Final fallback to Ripgrep - 5s timeout
1431
+ try {
1432
+ const ripgrepResult = await withTimeout(executeRipgrepMode(params), 5000, 'ripgrep');
1433
+ fallbackHistory.push(ripgrepResult.success ? 'ripgrep: success' : 'ripgrep: failed');
1434
+ return {
1435
+ ...ripgrepResult,
1436
+ metadata: {
1437
+ ...ripgrepResult.metadata,
1438
+ mode: 'priority',
1439
+ note: 'Result from ripgrep search (final fallback).',
1440
+ fallback_history: fallbackHistory,
1441
+ },
1442
+ };
1443
+ } catch (error) {
1444
+ fallbackHistory.push(`ripgrep: ${(error as Error).message}`);
1445
+ }
1446
+
1447
+ // All modes failed
1448
+ return {
1449
+ success: false,
1450
+ error: 'All search backends in priority mode failed or returned no results.',
1451
+ metadata: {
1452
+ mode: 'priority',
1453
+ query,
1454
+ fallback_history: fallbackHistory,
1455
+ } as any,
1456
+ };
1457
+ }
1458
+
1459
+ // Tool schema for MCP
1460
+ export const schema: ToolSchema = {
1461
+ name: 'smart_search',
1462
+ description: `Unified code search tool with content search, file discovery, and semantic search capabilities.
1463
+
1464
+ **Actions:**
1465
+ - search: Search file content (default)
1466
+ - find_files: Find files by path/name pattern (glob matching)
1467
+ - init: Create FTS index
1468
+ - status: Check index status
1469
+
1470
+ **Content Search (action="search"):**
1471
+ smart_search(query="authentication logic") # auto mode - routes to best backend
1472
+ smart_search(query="MyClass", mode="exact") # exact mode - precise FTS matching
1473
+ smart_search(query="auth", mode="ripgrep") # ripgrep mode - fast literal search
1474
+ smart_search(query="how to auth", mode="hybrid") # hybrid mode - semantic + fuzzy search
1475
+
1476
+ **File Discovery (action="find_files"):**
1477
+ smart_search(action="find_files", pattern="*.ts") # find all TypeScript files
1478
+ smart_search(action="find_files", pattern="src/**/*.js") # recursive glob pattern
1479
+ smart_search(action="find_files", pattern="test_*.py") # find test files
1480
+ smart_search(action="find_files", pattern="*.tsx", offset=20, limit=10) # pagination
1481
+
1482
+ **Pagination:** All actions support offset/limit for paginated results:
1483
+ smart_search(query="auth", limit=10, offset=0) # first page
1484
+ smart_search(query="auth", limit=10, offset=10) # second page
1485
+
1486
+ **Multi-Word Search (ripgrep mode with tokenization):**
1487
+ smart_search(query="CCW_PROJECT_ROOT CCW_ALLOWED_DIRS", mode="ripgrep") # tokenized OR matching
1488
+ smart_search(query="auth login user", mode="ripgrep") # matches any token, ranks by match count
1489
+ smart_search(query="exact phrase", mode="ripgrep", tokenize=false) # disable tokenization
1490
+
1491
+ **Regex Search (ripgrep mode):**
1492
+ smart_search(query="class.*Builder") # auto-detects regex pattern
1493
+ smart_search(query="def.*\\(.*\\):") # find function definitions
1494
+ smart_search(query="import.*from", caseSensitive=false) # case-insensitive
1495
+
1496
+ **Modes:** auto (intelligent routing), hybrid (semantic+fuzzy), exact (FTS), ripgrep (fast with tokenization), priority (fallback chain)`,
1497
+ inputSchema: {
1498
+ type: 'object',
1499
+ properties: {
1500
+ action: {
1501
+ type: 'string',
1502
+ enum: ['init', 'search', 'find_files', 'status', 'search_files'],
1503
+ description: 'Action: search (content search), find_files (path pattern matching), init (create index), status (check index). Note: search_files is deprecated.',
1504
+ default: 'search',
1505
+ },
1506
+ query: {
1507
+ type: 'string',
1508
+ description: 'Content search query (for action="search")',
1509
+ },
1510
+ pattern: {
1511
+ type: 'string',
1512
+ description: 'Glob pattern for file discovery (for action="find_files"). Examples: "*.ts", "src/**/*.js", "test_*.py"',
1513
+ },
1514
+ mode: {
1515
+ type: 'string',
1516
+ enum: SEARCH_MODES,
1517
+ description: 'Search mode: auto (default), hybrid (best quality), exact (CodexLens FTS), ripgrep (fast, no index), priority (fallback: hybrid->exact->ripgrep)',
1518
+ default: 'auto',
1519
+ },
1520
+ output_mode: {
1521
+ type: 'string',
1522
+ enum: ['full', 'files_only', 'count'],
1523
+ description: 'Output format: full (default), files_only (paths only), count (per-file counts)',
1524
+ default: 'full',
1525
+ },
1526
+ path: {
1527
+ type: 'string',
1528
+ description: 'Directory path for init/search actions (default: current directory)',
1529
+ },
1530
+ paths: {
1531
+ type: 'array',
1532
+ description: 'Multiple paths to search within (for search action)',
1533
+ items: {
1534
+ type: 'string',
1535
+ },
1536
+ default: [],
1537
+ },
1538
+ contextLines: {
1539
+ type: 'number',
1540
+ description: 'Number of context lines around matches (exact mode only)',
1541
+ default: 0,
1542
+ },
1543
+ maxResults: {
1544
+ type: 'number',
1545
+ description: 'Maximum number of results (default: 20)',
1546
+ default: 20,
1547
+ },
1548
+ limit: {
1549
+ type: 'number',
1550
+ description: 'Alias for maxResults (default: 20)',
1551
+ default: 20,
1552
+ },
1553
+ offset: {
1554
+ type: 'number',
1555
+ description: 'Pagination offset - skip first N results (default: 0)',
1556
+ default: 0,
1557
+ },
1558
+ includeHidden: {
1559
+ type: 'boolean',
1560
+ description: 'Include hidden files/directories',
1561
+ default: false,
1562
+ },
1563
+ languages: {
1564
+ type: 'array',
1565
+ items: { type: 'string' },
1566
+ description: 'Languages to index (for init action). Example: ["javascript", "typescript"]',
1567
+ },
1568
+ enrich: {
1569
+ type: 'boolean',
1570
+ description: 'Enrich search results with code graph relationships (calls, imports, called_by, imported_by).',
1571
+ default: false,
1572
+ },
1573
+ regex: {
1574
+ type: 'boolean',
1575
+ description: 'Use regex pattern matching instead of literal string (ripgrep mode only). Default: enabled. Example: smart_search(query="class.*Builder")',
1576
+ default: true,
1577
+ },
1578
+ caseSensitive: {
1579
+ type: 'boolean',
1580
+ description: 'Case-sensitive search (default: true). Set to false for case-insensitive matching.',
1581
+ default: true,
1582
+ },
1583
+ tokenize: {
1584
+ type: 'boolean',
1585
+ description: 'Tokenize multi-word queries for OR matching (ripgrep mode). Default: true. Results are ranked by token match count (exact matches first).',
1586
+ default: true,
1587
+ },
1588
+ },
1589
+ required: [],
1590
+ },
1591
+ };
1592
+
1593
+ /**
1594
+ * Action: find_files - Find files by path/name pattern (glob matching)
1595
+ * Unlike search which looks inside file content, find_files matches file paths
1596
+ */
1597
+ async function executeFindFilesAction(params: Params): Promise<SearchResult> {
1598
+ const { pattern, path = '.', limit = 20, offset = 0, includeHidden = false, caseSensitive = true } = params;
1599
+
1600
+ if (!pattern) {
1601
+ return {
1602
+ success: false,
1603
+ error: 'Pattern is required for find_files action. Use glob patterns like "*.ts", "src/**/*.js", or "test_*.py"',
1604
+ };
1605
+ }
1606
+
1607
+ // Use ripgrep with --files flag for fast file listing with glob pattern
1608
+ const hasRipgrep = checkToolAvailability('rg');
1609
+
1610
+ if (!hasRipgrep) {
1611
+ // Fallback to CodexLens file listing if available
1612
+ const readyStatus = await ensureCodexLensReady();
1613
+ if (!readyStatus.ready) {
1614
+ return {
1615
+ success: false,
1616
+ error: 'Neither ripgrep nor CodexLens available for file discovery.',
1617
+ };
1618
+ }
1619
+
1620
+ // Try CodexLens file list command
1621
+ const args = ['list-files', '--json'];
1622
+ const result = await executeCodexLens(args, { cwd: path });
1623
+
1624
+ if (!result.success) {
1625
+ return {
1626
+ success: false,
1627
+ error: `Failed to list files: ${result.error}`,
1628
+ };
1629
+ }
1630
+
1631
+ // Parse and filter results by pattern
1632
+ let files: string[] = [];
1633
+ try {
1634
+ const parsed = JSON.parse(stripAnsi(result.output || '[]'));
1635
+ files = Array.isArray(parsed) ? parsed : (parsed.files || []);
1636
+ } catch {
1637
+ return {
1638
+ success: false,
1639
+ error: 'Failed to parse file list from CodexLens',
1640
+ };
1641
+ }
1642
+
1643
+ // Apply glob pattern matching using minimatch-style regex
1644
+ const globRegex = globToRegex(pattern, caseSensitive);
1645
+ const matchedFiles = files.filter(f => globRegex.test(f));
1646
+
1647
+ // Apply pagination
1648
+ const total = matchedFiles.length;
1649
+ const paginatedFiles = matchedFiles.slice(offset, offset + limit);
1650
+
1651
+ const results: FileMatch[] = paginatedFiles.map(filePath => {
1652
+ const parts = filePath.split(/[/\\]/);
1653
+ const name = parts[parts.length - 1] || '';
1654
+ const ext = name.includes('.') ? name.split('.').pop() : undefined;
1655
+ return {
1656
+ path: filePath,
1657
+ type: 'file' as const,
1658
+ name,
1659
+ extension: ext,
1660
+ };
1661
+ });
1662
+
1663
+ return {
1664
+ success: true,
1665
+ results,
1666
+ metadata: {
1667
+ pattern,
1668
+ backend: 'codexlens',
1669
+ count: results.length,
1670
+ pagination: {
1671
+ offset,
1672
+ limit,
1673
+ total,
1674
+ has_more: offset + limit < total,
1675
+ },
1676
+ },
1677
+ };
1678
+ }
1679
+
1680
+ // Use ripgrep --files with glob pattern for fast file discovery
1681
+ return new Promise((resolve) => {
1682
+ const args = ['--files'];
1683
+
1684
+ // Add exclude patterns
1685
+ if (!includeHidden) {
1686
+ args.push(...buildExcludeArgs());
1687
+ } else {
1688
+ args.push('--hidden');
1689
+ }
1690
+
1691
+ // Add glob pattern
1692
+ args.push('--glob', pattern);
1693
+
1694
+ // Case sensitivity for glob matching
1695
+ if (!caseSensitive) {
1696
+ args.push('--iglob', pattern);
1697
+ // Remove the case-sensitive glob and use iglob instead
1698
+ const globIndex = args.indexOf('--glob');
1699
+ if (globIndex !== -1) {
1700
+ args.splice(globIndex, 2);
1701
+ }
1702
+ }
1703
+
1704
+ const child = spawn('rg', args, {
1705
+ cwd: path || getProjectRoot(),
1706
+ stdio: ['ignore', 'pipe', 'pipe'],
1707
+ });
1708
+
1709
+ let stdout = '';
1710
+ let stderr = '';
1711
+
1712
+ child.stdout.on('data', (data) => {
1713
+ stdout += data.toString();
1714
+ });
1715
+
1716
+ child.stderr.on('data', (data) => {
1717
+ stderr += data.toString();
1718
+ });
1719
+
1720
+ child.on('close', (code) => {
1721
+ // ripgrep returns 1 when no matches found, which is not an error
1722
+ if (code !== 0 && code !== 1 && !stderr.includes('os error 1')) {
1723
+ resolve({
1724
+ success: false,
1725
+ error: `ripgrep file search failed: ${stderr}`,
1726
+ });
1727
+ return;
1728
+ }
1729
+
1730
+ const allFiles = stdout.split('\n').filter(line => line.trim());
1731
+ const total = allFiles.length;
1732
+
1733
+ // Apply pagination
1734
+ const paginatedFiles = allFiles.slice(offset, offset + limit);
1735
+
1736
+ const results: FileMatch[] = paginatedFiles.map(filePath => {
1737
+ const normalizedPath = filePath.replace(/\\/g, '/');
1738
+ const parts = normalizedPath.split('/');
1739
+ const name = parts[parts.length - 1] || '';
1740
+ const ext = name.includes('.') ? name.split('.').pop() : undefined;
1741
+ return {
1742
+ path: normalizedPath,
1743
+ type: 'file' as const,
1744
+ name,
1745
+ extension: ext,
1746
+ };
1747
+ });
1748
+
1749
+ resolve({
1750
+ success: true,
1751
+ results,
1752
+ metadata: {
1753
+ pattern,
1754
+ backend: 'ripgrep',
1755
+ count: results.length,
1756
+ pagination: {
1757
+ offset,
1758
+ limit,
1759
+ total,
1760
+ has_more: offset + limit < total,
1761
+ },
1762
+ },
1763
+ });
1764
+ });
1765
+
1766
+ child.on('error', (error) => {
1767
+ resolve({
1768
+ success: false,
1769
+ error: `Failed to spawn ripgrep: ${error.message}`,
1770
+ });
1771
+ });
1772
+ });
1773
+ }
1774
+
1775
+ /**
1776
+ * Convert glob pattern to regex for file matching
1777
+ * Supports: *, **, ?, [abc], [!abc]
1778
+ */
1779
+ function globToRegex(pattern: string, caseSensitive: boolean = true): RegExp {
1780
+ let i = 0;
1781
+ const out: string[] = [];
1782
+ const special = '.^$+{}|()';
1783
+
1784
+ while (i < pattern.length) {
1785
+ const c = pattern[i];
1786
+
1787
+ if (c === '*') {
1788
+ if (i + 1 < pattern.length && pattern[i + 1] === '*') {
1789
+ // ** matches any path including /
1790
+ out.push('.*');
1791
+ i += 2;
1792
+ // Skip following / if present
1793
+ if (pattern[i] === '/') {
1794
+ i++;
1795
+ }
1796
+ continue;
1797
+ } else {
1798
+ // * matches any character except /
1799
+ out.push('[^/]*');
1800
+ }
1801
+ } else if (c === '?') {
1802
+ out.push('[^/]');
1803
+ } else if (c === '[') {
1804
+ // Character class
1805
+ let j = i + 1;
1806
+ let negated = false;
1807
+ if (pattern[j] === '!' || pattern[j] === '^') {
1808
+ negated = true;
1809
+ j++;
1810
+ }
1811
+ let classContent = '';
1812
+ while (j < pattern.length && pattern[j] !== ']') {
1813
+ classContent += pattern[j];
1814
+ j++;
1815
+ }
1816
+ if (negated) {
1817
+ out.push(`[^${classContent}]`);
1818
+ } else {
1819
+ out.push(`[${classContent}]`);
1820
+ }
1821
+ i = j;
1822
+ } else if (special.includes(c)) {
1823
+ out.push('\\' + c);
1824
+ } else {
1825
+ out.push(c);
1826
+ }
1827
+ i++;
1828
+ }
1829
+
1830
+ const flags = caseSensitive ? '' : 'i';
1831
+ return new RegExp('^' + out.join('') + '$', flags);
1832
+ }
1833
+
1834
+ /**
1835
+ * Apply pagination to search results and add pagination metadata
1836
+ */
1837
+ function applyPagination<T>(
1838
+ results: T[],
1839
+ offset: number,
1840
+ limit: number
1841
+ ): { paginatedResults: T[]; pagination: PaginationInfo } {
1842
+ const total = results.length;
1843
+ const paginatedResults = results.slice(offset, offset + limit);
1844
+
1845
+ return {
1846
+ paginatedResults,
1847
+ pagination: {
1848
+ offset,
1849
+ limit,
1850
+ total,
1851
+ has_more: offset + limit < total,
1852
+ },
1853
+ };
1854
+ }
1855
+
1856
+ /**
1857
+ * Transform results based on output_mode
1858
+ */
1859
+ function transformOutput(
1860
+ results: ExactMatch[] | SemanticMatch[] | GraphMatch[] | unknown[],
1861
+ outputMode: 'full' | 'files_only' | 'count'
1862
+ ): unknown {
1863
+ if (!Array.isArray(results)) {
1864
+ return results;
1865
+ }
1866
+
1867
+ switch (outputMode) {
1868
+ case 'files_only': {
1869
+ // Extract unique file paths
1870
+ const files = [...new Set(results.map((r: any) => r.file))].filter(Boolean);
1871
+ return { files, count: files.length };
1872
+ }
1873
+ case 'count': {
1874
+ // Count matches per file
1875
+ const counts: Record<string, number> = {};
1876
+ for (const r of results) {
1877
+ const file = (r as any).file;
1878
+ if (file) {
1879
+ counts[file] = (counts[file] || 0) + 1;
1880
+ }
1881
+ }
1882
+ return {
1883
+ files: Object.entries(counts).map(([file, count]) => ({ file, count })),
1884
+ total: results.length,
1885
+ };
1886
+ }
1887
+ case 'full':
1888
+ default:
1889
+ return results;
1890
+ }
1891
+ }
1892
+
1893
+ // Handler function
1894
+ export async function handler(params: Record<string, unknown>): Promise<ToolResult<SearchResult>> {
1895
+ const parsed = ParamsSchema.safeParse(params);
1896
+ if (!parsed.success) {
1897
+ return { success: false, error: `Invalid params: ${parsed.error.message}` };
1898
+ }
1899
+
1900
+ const { action, mode, output_mode, offset = 0 } = parsed.data;
1901
+
1902
+ // Sync limit and maxResults - use the larger of the two if both provided
1903
+ // This ensures user-provided values take precedence over defaults
1904
+ const effectiveLimit = Math.max(parsed.data.limit || 20, parsed.data.maxResults || 20);
1905
+ parsed.data.maxResults = effectiveLimit;
1906
+ parsed.data.limit = effectiveLimit;
1907
+
1908
+ // Track if search_files was used (deprecated)
1909
+ let deprecationWarning: string | undefined;
1910
+
1911
+ try {
1912
+ let result: SearchResult;
1913
+
1914
+ // Handle actions
1915
+ switch (action) {
1916
+ case 'init':
1917
+ result = await executeInitAction(parsed.data);
1918
+ break;
1919
+
1920
+ case 'status':
1921
+ result = await executeStatusAction(parsed.data);
1922
+ break;
1923
+
1924
+ case 'find_files':
1925
+ // NEW: File path/name pattern matching (glob-based)
1926
+ result = await executeFindFilesAction(parsed.data);
1927
+ break;
1928
+
1929
+ case 'search_files':
1930
+ // DEPRECATED: Redirect to search with files_only output
1931
+ deprecationWarning = 'action="search_files" is deprecated. Use action="search" with output_mode="files_only" for content-to-files search, or action="find_files" for path pattern matching.';
1932
+ parsed.data.output_mode = 'files_only';
1933
+ // Fall through to search
1934
+
1935
+ case 'search':
1936
+ default:
1937
+ // Handle search modes: auto | hybrid | exact | ripgrep | priority
1938
+ switch (mode) {
1939
+ case 'auto':
1940
+ result = await executeAutoMode(parsed.data);
1941
+ break;
1942
+ case 'hybrid':
1943
+ result = await executeHybridMode(parsed.data);
1944
+ break;
1945
+ case 'exact':
1946
+ result = await executeCodexLensExactMode(parsed.data);
1947
+ break;
1948
+ case 'ripgrep':
1949
+ result = await executeRipgrepMode(parsed.data);
1950
+ break;
1951
+ case 'priority':
1952
+ result = await executePriorityFallbackMode(parsed.data);
1953
+ break;
1954
+ default:
1955
+ throw new Error(`Unsupported mode: ${mode}. Use: auto, hybrid, exact, ripgrep, or priority`);
1956
+ }
1957
+ break;
1958
+ }
1959
+
1960
+ // Transform output based on output_mode (for search actions only)
1961
+ if (action === 'search' || action === 'search_files') {
1962
+ if (result.success && result.results && output_mode !== 'full') {
1963
+ result.results = transformOutput(result.results as any[], output_mode);
1964
+ }
1965
+
1966
+ // Add pagination metadata for search results if not already present
1967
+ if (result.success && result.results && Array.isArray(result.results)) {
1968
+ const totalResults = (result.results as any[]).length;
1969
+ if (!result.metadata) {
1970
+ result.metadata = {};
1971
+ }
1972
+ if (!result.metadata.pagination) {
1973
+ result.metadata.pagination = {
1974
+ offset: 0,
1975
+ limit: effectiveLimit,
1976
+ total: totalResults,
1977
+ has_more: false, // Already limited by backend
1978
+ };
1979
+ }
1980
+ }
1981
+ }
1982
+
1983
+ // Add deprecation warning if applicable
1984
+ if (deprecationWarning && result.metadata) {
1985
+ result.metadata.warning = deprecationWarning;
1986
+ }
1987
+
1988
+ return result.success ? { success: true, result } : { success: false, error: result.error };
1989
+ } catch (error) {
1990
+ return { success: false, error: (error as Error).message };
1991
+ }
1992
+ }
1993
+
1994
+ /**
1995
+ * Execute init action with external progress callback
1996
+ * Used by MCP server for streaming progress
1997
+ */
1998
+ export async function executeInitWithProgress(
1999
+ params: Record<string, unknown>,
2000
+ onProgress?: (progress: ProgressInfo) => void
2001
+ ): Promise<SearchResult> {
2002
+ const path = (params.path as string) || '.';
2003
+ const languages = params.languages as string[] | undefined;
2004
+
2005
+ // Check CodexLens availability
2006
+ const readyStatus = await ensureCodexLensReady();
2007
+ if (!readyStatus.ready) {
2008
+ return {
2009
+ success: false,
2010
+ error: `CodexLens not available: ${readyStatus.error}. CodexLens will be auto-installed on first use.`,
2011
+ };
2012
+ }
2013
+
2014
+ const args = ['init', path];
2015
+ if (languages && languages.length > 0) {
2016
+ args.push('--languages', languages.join(','));
2017
+ }
2018
+
2019
+ // Track progress updates
2020
+ const progressUpdates: ProgressInfo[] = [];
2021
+ let lastProgress: ProgressInfo | null = null;
2022
+
2023
+ const result = await executeCodexLens(args, {
2024
+ cwd: path,
2025
+ timeout: 1800000, // 30 minutes for large codebases
2026
+ onProgress: (progress: ProgressInfo) => {
2027
+ progressUpdates.push(progress);
2028
+ lastProgress = progress;
2029
+ // Call external progress callback if provided
2030
+ if (onProgress) {
2031
+ onProgress(progress);
2032
+ }
2033
+ },
2034
+ });
2035
+
2036
+ // Build metadata with progress info
2037
+ const metadata: SearchMetadata = {
2038
+ action: 'init',
2039
+ path,
2040
+ };
2041
+
2042
+ if (lastProgress !== null) {
2043
+ const p = lastProgress as ProgressInfo;
2044
+ metadata.progress = {
2045
+ stage: p.stage,
2046
+ message: p.message,
2047
+ percent: p.percent,
2048
+ filesProcessed: p.filesProcessed,
2049
+ totalFiles: p.totalFiles,
2050
+ };
2051
+ }
2052
+
2053
+ if (progressUpdates.length > 0) {
2054
+ metadata.progressHistory = progressUpdates.slice(-5);
2055
+ }
2056
+
2057
+ return {
2058
+ success: result.success,
2059
+ error: result.error,
2060
+ message: result.success
2061
+ ? `CodexLens index created successfully for ${path}`
2062
+ : undefined,
2063
+ metadata,
2064
+ };
2065
+ }