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,1195 @@
1
+ /**
2
+ * CLI History Store - SQLite Storage Backend
3
+ * Provides persistent storage for CLI execution history with efficient queries
4
+ */
5
+
6
+ import Database from 'better-sqlite3';
7
+ import { existsSync, mkdirSync, readdirSync, readFileSync, statSync, unlinkSync, rmdirSync } from 'fs';
8
+ import { join } from 'path';
9
+ import { parseSessionFile, formatConversation, extractConversationPairs, type ParsedSession, type ParsedTurn } from './session-content-parser.js';
10
+ import { StoragePaths, ensureStorageDir, getProjectId } from '../config/storage-paths.js';
11
+
12
+ // Types
13
+ export interface ConversationTurn {
14
+ turn: number;
15
+ timestamp: string;
16
+ prompt: string;
17
+ duration_ms: number;
18
+ status: 'success' | 'error' | 'timeout';
19
+ exit_code: number | null;
20
+ // NOTE: Naming inconsistency - using prompt/stdout vs tool_args/tool_output in MemoryStore
21
+ // This reflects CLI-specific semantics (prompt -> execution -> output)
22
+ output: {
23
+ stdout: string;
24
+ stderr: string;
25
+ truncated: boolean;
26
+ };
27
+ }
28
+
29
+ // Execution category types
30
+ export type ExecutionCategory = 'user' | 'internal' | 'insight';
31
+
32
+ export interface ConversationRecord {
33
+ id: string;
34
+ created_at: string;
35
+ updated_at: string;
36
+ tool: string;
37
+ model: string;
38
+ mode: string;
39
+ category: ExecutionCategory; // user | internal | insight
40
+ total_duration_ms: number;
41
+ turn_count: number;
42
+ latest_status: 'success' | 'error' | 'timeout';
43
+ turns: ConversationTurn[];
44
+ parent_execution_id?: string; // For fork/retry scenarios
45
+ }
46
+
47
+ export interface HistoryQueryOptions {
48
+ limit?: number;
49
+ offset?: number;
50
+ tool?: string | null;
51
+ status?: string | null;
52
+ category?: ExecutionCategory | null;
53
+ search?: string | null;
54
+ startDate?: string | null;
55
+ endDate?: string | null;
56
+ }
57
+
58
+ export interface HistoryIndexEntry {
59
+ id: string;
60
+ timestamp: string;
61
+ updated_at?: string;
62
+ tool: string;
63
+ status: string;
64
+ category?: ExecutionCategory;
65
+ duration_ms: number;
66
+ turn_count?: number;
67
+ prompt_preview: string;
68
+ sourceDir?: string;
69
+ }
70
+
71
+ // Native session mapping interface
72
+ export interface NativeSessionMapping {
73
+ ccw_id: string; // CCW execution ID (e.g., 1702123456789-gemini)
74
+ tool: string; // gemini | qwen | codex
75
+ native_session_id: string; // Native UUID
76
+ native_session_path?: string; // Native file path
77
+ project_hash?: string; // Project hash (Gemini/Qwen)
78
+ created_at: string;
79
+ }
80
+
81
+ // Review record interface
82
+ export type ReviewStatus = 'pending' | 'approved' | 'rejected' | 'changes_requested';
83
+
84
+ export interface ReviewRecord {
85
+ id?: number;
86
+ execution_id: string;
87
+ status: ReviewStatus;
88
+ rating?: number;
89
+ comments?: string;
90
+ reviewer?: string;
91
+ created_at: string;
92
+ updated_at: string;
93
+ }
94
+
95
+ /**
96
+ * CLI History Store using SQLite
97
+ */
98
+ export class CliHistoryStore {
99
+ private db: Database.Database;
100
+ private dbPath: string;
101
+ private projectPath: string;
102
+
103
+ constructor(baseDir: string) {
104
+ this.projectPath = baseDir;
105
+
106
+ // Use centralized storage path
107
+ const paths = StoragePaths.project(baseDir);
108
+ const historyDir = paths.cliHistory;
109
+ ensureStorageDir(historyDir);
110
+
111
+ this.dbPath = paths.historyDb;
112
+ this.db = new Database(this.dbPath);
113
+ this.db.pragma('journal_mode = WAL');
114
+ this.db.pragma('synchronous = NORMAL');
115
+
116
+ this.initSchema();
117
+ this.migrateFromJson(historyDir);
118
+ }
119
+
120
+ /**
121
+ * Initialize database schema
122
+ */
123
+ private initSchema(): void {
124
+ this.db.exec(`
125
+ -- Conversations table (conversation metadata)
126
+ CREATE TABLE IF NOT EXISTS conversations (
127
+ id TEXT PRIMARY KEY,
128
+ created_at TEXT NOT NULL,
129
+ updated_at TEXT NOT NULL,
130
+ tool TEXT NOT NULL,
131
+ model TEXT DEFAULT 'default',
132
+ mode TEXT DEFAULT 'analysis',
133
+ category TEXT DEFAULT 'user',
134
+ total_duration_ms INTEGER DEFAULT 0,
135
+ turn_count INTEGER DEFAULT 0,
136
+ latest_status TEXT DEFAULT 'success',
137
+ prompt_preview TEXT,
138
+ parent_execution_id TEXT,
139
+ FOREIGN KEY (parent_execution_id) REFERENCES conversations(id) ON DELETE SET NULL
140
+ );
141
+
142
+ -- Turns table (individual conversation turns)
143
+ CREATE TABLE IF NOT EXISTS turns (
144
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
145
+ conversation_id TEXT NOT NULL,
146
+ turn_number INTEGER NOT NULL,
147
+ timestamp TEXT NOT NULL,
148
+ prompt TEXT NOT NULL,
149
+ duration_ms INTEGER DEFAULT 0,
150
+ status TEXT DEFAULT 'success',
151
+ exit_code INTEGER,
152
+ stdout TEXT,
153
+ stderr TEXT,
154
+ truncated INTEGER DEFAULT 0,
155
+ FOREIGN KEY (conversation_id) REFERENCES conversations(id) ON DELETE CASCADE,
156
+ UNIQUE(conversation_id, turn_number)
157
+ );
158
+
159
+ -- Indexes for efficient queries
160
+ CREATE INDEX IF NOT EXISTS idx_conversations_tool ON conversations(tool);
161
+ CREATE INDEX IF NOT EXISTS idx_conversations_status ON conversations(latest_status);
162
+ CREATE INDEX IF NOT EXISTS idx_conversations_category ON conversations(category);
163
+ CREATE INDEX IF NOT EXISTS idx_conversations_updated ON conversations(updated_at DESC);
164
+ CREATE INDEX IF NOT EXISTS idx_conversations_created ON conversations(created_at DESC);
165
+ CREATE INDEX IF NOT EXISTS idx_turns_conversation ON turns(conversation_id);
166
+
167
+ -- Full-text search for prompts
168
+ CREATE VIRTUAL TABLE IF NOT EXISTS turns_fts USING fts5(
169
+ prompt,
170
+ stdout,
171
+ content='turns',
172
+ content_rowid='id'
173
+ );
174
+
175
+ -- Triggers to keep FTS index updated
176
+ CREATE TRIGGER IF NOT EXISTS turns_ai AFTER INSERT ON turns BEGIN
177
+ INSERT INTO turns_fts(rowid, prompt, stdout) VALUES (new.id, new.prompt, new.stdout);
178
+ END;
179
+
180
+ CREATE TRIGGER IF NOT EXISTS turns_ad AFTER DELETE ON turns BEGIN
181
+ INSERT INTO turns_fts(turns_fts, rowid, prompt, stdout) VALUES('delete', old.id, old.prompt, old.stdout);
182
+ END;
183
+
184
+ CREATE TRIGGER IF NOT EXISTS turns_au AFTER UPDATE ON turns BEGIN
185
+ INSERT INTO turns_fts(turns_fts, rowid, prompt, stdout) VALUES('delete', old.id, old.prompt, old.stdout);
186
+ INSERT INTO turns_fts(rowid, prompt, stdout) VALUES (new.id, new.prompt, new.stdout);
187
+ END;
188
+
189
+ -- Native session mapping table (CCW ID <-> Native Session ID)
190
+ CREATE TABLE IF NOT EXISTS native_session_mapping (
191
+ ccw_id TEXT PRIMARY KEY,
192
+ tool TEXT NOT NULL,
193
+ native_session_id TEXT NOT NULL,
194
+ native_session_path TEXT,
195
+ project_hash TEXT,
196
+ created_at TEXT DEFAULT CURRENT_TIMESTAMP,
197
+ UNIQUE(tool, native_session_id)
198
+ );
199
+
200
+ -- Indexes for native session lookups
201
+ CREATE INDEX IF NOT EXISTS idx_native_tool_session ON native_session_mapping(tool, native_session_id);
202
+ CREATE INDEX IF NOT EXISTS idx_native_session_id ON native_session_mapping(native_session_id);
203
+
204
+ -- Insights analysis history table
205
+ CREATE TABLE IF NOT EXISTS insights (
206
+ id TEXT PRIMARY KEY,
207
+ created_at TEXT NOT NULL,
208
+ tool TEXT NOT NULL,
209
+ prompt_count INTEGER DEFAULT 0,
210
+ patterns TEXT,
211
+ suggestions TEXT,
212
+ raw_output TEXT,
213
+ execution_id TEXT,
214
+ lang TEXT DEFAULT 'en'
215
+ );
216
+
217
+ CREATE INDEX IF NOT EXISTS idx_insights_created ON insights(created_at DESC);
218
+ CREATE INDEX IF NOT EXISTS idx_insights_tool ON insights(tool);
219
+
220
+ -- Reviews table for CLI execution reviews
221
+ CREATE TABLE IF NOT EXISTS reviews (
222
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
223
+ execution_id TEXT NOT NULL UNIQUE,
224
+ status TEXT NOT NULL DEFAULT 'pending',
225
+ rating INTEGER,
226
+ comments TEXT,
227
+ reviewer TEXT,
228
+ created_at TEXT NOT NULL,
229
+ updated_at TEXT NOT NULL,
230
+ FOREIGN KEY (execution_id) REFERENCES conversations(id) ON DELETE CASCADE
231
+ );
232
+
233
+ CREATE INDEX IF NOT EXISTS idx_reviews_execution ON reviews(execution_id);
234
+ CREATE INDEX IF NOT EXISTS idx_reviews_status ON reviews(status);
235
+ CREATE INDEX IF NOT EXISTS idx_reviews_created ON reviews(created_at DESC);
236
+ `);
237
+
238
+ // Migration: Add category column if not exists (for existing databases)
239
+ this.migrateSchema();
240
+ }
241
+
242
+ /**
243
+ * Migrate schema for existing databases
244
+ */
245
+ private migrateSchema(): void {
246
+ try {
247
+ // Check if columns exist
248
+ const tableInfo = this.db.prepare('PRAGMA table_info(conversations)').all() as Array<{ name: string }>;
249
+ const hasCategory = tableInfo.some(col => col.name === 'category');
250
+ const hasParentExecutionId = tableInfo.some(col => col.name === 'parent_execution_id');
251
+ const hasProjectRoot = tableInfo.some(col => col.name === 'project_root');
252
+ const hasRelativePath = tableInfo.some(col => col.name === 'relative_path');
253
+
254
+ if (!hasCategory) {
255
+ console.log('[CLI History] Migrating database: adding category column...');
256
+ this.db.exec(`
257
+ ALTER TABLE conversations ADD COLUMN category TEXT DEFAULT 'user';
258
+ `);
259
+ // Create index separately to handle potential errors
260
+ try {
261
+ this.db.exec(`CREATE INDEX IF NOT EXISTS idx_conversations_category ON conversations(category);`);
262
+ } catch (indexErr) {
263
+ console.warn('[CLI History] Category index creation warning:', (indexErr as Error).message);
264
+ }
265
+ console.log('[CLI History] Migration complete: category column added');
266
+ }
267
+
268
+ if (!hasParentExecutionId) {
269
+ console.log('[CLI History] Migrating database: adding parent_execution_id column...');
270
+ this.db.exec(`
271
+ ALTER TABLE conversations ADD COLUMN parent_execution_id TEXT;
272
+ `);
273
+ try {
274
+ this.db.exec(`CREATE INDEX IF NOT EXISTS idx_conversations_parent ON conversations(parent_execution_id);`);
275
+ } catch (indexErr) {
276
+ console.warn('[CLI History] Parent execution index creation warning:', (indexErr as Error).message);
277
+ }
278
+ console.log('[CLI History] Migration complete: parent_execution_id column added');
279
+ }
280
+
281
+ // Add hierarchical storage support columns
282
+ if (!hasProjectRoot) {
283
+ console.log('[CLI History] Migrating database: adding project_root column for hierarchical storage...');
284
+ this.db.exec(`
285
+ ALTER TABLE conversations ADD COLUMN project_root TEXT;
286
+ `);
287
+ try {
288
+ this.db.exec(`CREATE INDEX IF NOT EXISTS idx_conversations_project_root ON conversations(project_root);`);
289
+ } catch (indexErr) {
290
+ console.warn('[CLI History] Project root index creation warning:', (indexErr as Error).message);
291
+ }
292
+ console.log('[CLI History] Migration complete: project_root column added');
293
+ }
294
+
295
+ if (!hasRelativePath) {
296
+ console.log('[CLI History] Migrating database: adding relative_path column for hierarchical storage...');
297
+ this.db.exec(`
298
+ ALTER TABLE conversations ADD COLUMN relative_path TEXT;
299
+ `);
300
+ console.log('[CLI History] Migration complete: relative_path column added');
301
+ }
302
+
303
+ // Add missing timestamp index for turns table (for time-based queries)
304
+ try {
305
+ const indexExists = this.db.prepare(`
306
+ SELECT name FROM sqlite_master
307
+ WHERE type='index' AND name='idx_turns_timestamp'
308
+ `).get();
309
+
310
+ if (!indexExists) {
311
+ console.log('[CLI History] Adding missing timestamp index to turns table...');
312
+ this.db.exec(`CREATE INDEX IF NOT EXISTS idx_turns_timestamp ON turns(timestamp DESC);`);
313
+ console.log('[CLI History] Migration complete: turns timestamp index added');
314
+ }
315
+ } catch (indexErr) {
316
+ console.warn('[CLI History] Turns timestamp index creation warning:', (indexErr as Error).message);
317
+ }
318
+ } catch (err) {
319
+ console.error('[CLI History] Migration error:', (err as Error).message);
320
+ // Don't throw - allow the store to continue working with existing schema
321
+ }
322
+ }
323
+
324
+ /**
325
+ * Migrate existing JSON files to SQLite
326
+ */
327
+ private migrateFromJson(historyDir: string): void {
328
+ const migrationMarker = join(historyDir, '.migrated');
329
+ if (existsSync(migrationMarker)) {
330
+ return; // Already migrated
331
+ }
332
+
333
+ // Find all date directories
334
+ const dateDirs = readdirSync(historyDir).filter(d => {
335
+ const dirPath = join(historyDir, d);
336
+ return statSync(dirPath).isDirectory() && /^\d{4}-\d{2}-\d{2}$/.test(d);
337
+ });
338
+
339
+ let migratedCount = 0;
340
+
341
+ for (const dateDir of dateDirs) {
342
+ const dirPath = join(historyDir, dateDir);
343
+ const files = readdirSync(dirPath).filter(f => f.endsWith('.json'));
344
+
345
+ for (const file of files) {
346
+ try {
347
+ const filePath = join(dirPath, file);
348
+ const data = JSON.parse(readFileSync(filePath, 'utf8'));
349
+
350
+ // Convert to conversation record if legacy format
351
+ const conversation = this.normalizeRecord(data);
352
+ this.saveConversation(conversation);
353
+ migratedCount++;
354
+
355
+ // Optionally delete the JSON file after migration
356
+ // unlinkSync(filePath);
357
+ } catch (err) {
358
+ console.error(`Failed to migrate ${file}:`, (err as Error).message);
359
+ }
360
+ }
361
+ }
362
+
363
+ // Create migration marker
364
+ if (migratedCount > 0) {
365
+ require('fs').writeFileSync(migrationMarker, new Date().toISOString());
366
+ console.log(`[CLI History] Migrated ${migratedCount} records to SQLite`);
367
+ }
368
+ }
369
+
370
+ /**
371
+ * Normalize legacy record to ConversationRecord format
372
+ */
373
+ private normalizeRecord(data: any): ConversationRecord {
374
+ if (data.turns && Array.isArray(data.turns)) {
375
+ return data as ConversationRecord;
376
+ }
377
+
378
+ // Legacy single execution format
379
+ return {
380
+ id: data.id,
381
+ created_at: data.timestamp,
382
+ updated_at: data.timestamp,
383
+ tool: data.tool,
384
+ model: data.model || 'default',
385
+ mode: data.mode || 'analysis',
386
+ category: data.category || 'user',
387
+ total_duration_ms: data.duration_ms || 0,
388
+ turn_count: 1,
389
+ latest_status: data.status || 'success',
390
+ turns: [{
391
+ turn: 1,
392
+ timestamp: data.timestamp,
393
+ prompt: data.prompt,
394
+ duration_ms: data.duration_ms || 0,
395
+ status: data.status || 'success',
396
+ exit_code: data.exit_code,
397
+ output: data.output || { stdout: '', stderr: '', truncated: false }
398
+ }]
399
+ };
400
+ }
401
+
402
+ /**
403
+ * Save or update a conversation
404
+ */
405
+ saveConversation(conversation: ConversationRecord): void {
406
+ const promptPreview = conversation.turns.length > 0
407
+ ? conversation.turns[conversation.turns.length - 1].prompt.substring(0, 100)
408
+ : '';
409
+
410
+ const upsertConversation = this.db.prepare(`
411
+ INSERT INTO conversations (id, created_at, updated_at, tool, model, mode, category, total_duration_ms, turn_count, latest_status, prompt_preview, parent_execution_id, project_root, relative_path)
412
+ VALUES (@id, @created_at, @updated_at, @tool, @model, @mode, @category, @total_duration_ms, @turn_count, @latest_status, @prompt_preview, @parent_execution_id, @project_root, @relative_path)
413
+ ON CONFLICT(id) DO UPDATE SET
414
+ updated_at = @updated_at,
415
+ total_duration_ms = @total_duration_ms,
416
+ turn_count = @turn_count,
417
+ latest_status = @latest_status,
418
+ prompt_preview = @prompt_preview,
419
+ project_root = @project_root,
420
+ relative_path = @relative_path
421
+ `);
422
+
423
+ const upsertTurn = this.db.prepare(`
424
+ INSERT INTO turns (conversation_id, turn_number, timestamp, prompt, duration_ms, status, exit_code, stdout, stderr, truncated)
425
+ VALUES (@conversation_id, @turn_number, @timestamp, @prompt, @duration_ms, @status, @exit_code, @stdout, @stderr, @truncated)
426
+ ON CONFLICT(conversation_id, turn_number) DO UPDATE SET
427
+ timestamp = @timestamp,
428
+ prompt = @prompt,
429
+ duration_ms = @duration_ms,
430
+ status = @status,
431
+ exit_code = @exit_code,
432
+ stdout = @stdout,
433
+ stderr = @stderr,
434
+ truncated = @truncated
435
+ `);
436
+
437
+ const transaction = this.db.transaction(() => {
438
+ upsertConversation.run({
439
+ id: conversation.id,
440
+ created_at: conversation.created_at,
441
+ updated_at: conversation.updated_at,
442
+ tool: conversation.tool,
443
+ model: conversation.model,
444
+ mode: conversation.mode,
445
+ category: conversation.category || 'user',
446
+ total_duration_ms: conversation.total_duration_ms,
447
+ turn_count: conversation.turn_count,
448
+ latest_status: conversation.latest_status,
449
+ prompt_preview: promptPreview,
450
+ parent_execution_id: conversation.parent_execution_id || null,
451
+ project_root: this.projectPath,
452
+ relative_path: null // For future hierarchical tracking
453
+ });
454
+
455
+ for (const turn of conversation.turns) {
456
+ upsertTurn.run({
457
+ conversation_id: conversation.id,
458
+ turn_number: turn.turn,
459
+ timestamp: turn.timestamp,
460
+ prompt: turn.prompt,
461
+ duration_ms: turn.duration_ms,
462
+ status: turn.status,
463
+ exit_code: turn.exit_code,
464
+ stdout: turn.output.stdout,
465
+ stderr: turn.output.stderr,
466
+ truncated: turn.output.truncated ? 1 : 0
467
+ });
468
+ }
469
+ });
470
+
471
+ transaction();
472
+ }
473
+
474
+ /**
475
+ * Get conversation by ID
476
+ */
477
+ getConversation(id: string): ConversationRecord | null {
478
+ const conv = this.db.prepare(`
479
+ SELECT * FROM conversations WHERE id = ?
480
+ `).get(id) as any;
481
+
482
+ if (!conv) return null;
483
+
484
+ const turns = this.db.prepare(`
485
+ SELECT * FROM turns WHERE conversation_id = ? ORDER BY turn_number ASC
486
+ `).all(id) as any[];
487
+
488
+ return {
489
+ id: conv.id,
490
+ created_at: conv.created_at,
491
+ updated_at: conv.updated_at,
492
+ tool: conv.tool,
493
+ model: conv.model,
494
+ mode: conv.mode,
495
+ category: conv.category || 'user',
496
+ total_duration_ms: conv.total_duration_ms,
497
+ turn_count: conv.turn_count,
498
+ latest_status: conv.latest_status,
499
+ parent_execution_id: conv.parent_execution_id || undefined,
500
+ turns: turns.map(t => ({
501
+ turn: t.turn_number,
502
+ timestamp: t.timestamp,
503
+ prompt: t.prompt,
504
+ duration_ms: t.duration_ms,
505
+ status: t.status,
506
+ exit_code: t.exit_code,
507
+ output: {
508
+ stdout: t.stdout || '',
509
+ stderr: t.stderr || '',
510
+ truncated: !!t.truncated
511
+ }
512
+ }))
513
+ };
514
+ }
515
+
516
+ /**
517
+ * Get conversation with native session info
518
+ */
519
+ getConversationWithNativeInfo(id: string): (ConversationRecord & {
520
+ hasNativeSession: boolean;
521
+ nativeSessionId?: string;
522
+ nativeSessionPath?: string;
523
+ }) | null {
524
+ const conv = this.getConversation(id);
525
+ if (!conv) return null;
526
+
527
+ const mapping = this.getNativeSessionMapping(id);
528
+ return {
529
+ ...conv,
530
+ hasNativeSession: !!mapping,
531
+ nativeSessionId: mapping?.native_session_id,
532
+ nativeSessionPath: mapping?.native_session_path
533
+ };
534
+ }
535
+
536
+ /**
537
+ * Query execution history
538
+ */
539
+ getHistory(options: HistoryQueryOptions = {}): {
540
+ total: number;
541
+ count: number;
542
+ executions: HistoryIndexEntry[];
543
+ } {
544
+ const { limit = 50, offset = 0, tool, status, category, search, startDate, endDate } = options;
545
+
546
+ let whereClause = '1=1';
547
+ const params: any = {};
548
+
549
+ if (tool) {
550
+ whereClause += ' AND tool = @tool';
551
+ params.tool = tool;
552
+ }
553
+
554
+ if (status) {
555
+ whereClause += ' AND latest_status = @status';
556
+ params.status = status;
557
+ }
558
+
559
+ if (category) {
560
+ whereClause += ' AND category = @category';
561
+ params.category = category;
562
+ }
563
+
564
+ if (startDate) {
565
+ whereClause += ' AND created_at >= @startDate';
566
+ params.startDate = startDate;
567
+ }
568
+
569
+ if (endDate) {
570
+ whereClause += ' AND created_at <= @endDate';
571
+ params.endDate = endDate;
572
+ }
573
+
574
+ // Full-text search
575
+ let joinClause = '';
576
+ if (search) {
577
+ joinClause = `
578
+ INNER JOIN (
579
+ SELECT DISTINCT conversation_id FROM turns t
580
+ INNER JOIN turns_fts ON turns_fts.rowid = t.id
581
+ WHERE turns_fts MATCH @search
582
+ ) AS matched ON c.id = matched.conversation_id
583
+ `;
584
+ params.search = search;
585
+ }
586
+
587
+ const countQuery = this.db.prepare(`
588
+ SELECT COUNT(*) as count FROM conversations c ${joinClause} WHERE ${whereClause}
589
+ `);
590
+ const total = (countQuery.get(params) as any).count;
591
+
592
+ const dataQuery = this.db.prepare(`
593
+ SELECT c.* FROM conversations c ${joinClause}
594
+ WHERE ${whereClause}
595
+ ORDER BY c.updated_at DESC
596
+ LIMIT @limit OFFSET @offset
597
+ `);
598
+
599
+ const rows = dataQuery.all({ ...params, limit, offset }) as any[];
600
+
601
+ return {
602
+ total,
603
+ count: rows.length,
604
+ executions: rows.map(r => ({
605
+ id: r.id,
606
+ timestamp: r.created_at,
607
+ updated_at: r.updated_at,
608
+ tool: r.tool,
609
+ status: r.latest_status,
610
+ category: r.category || 'user',
611
+ duration_ms: r.total_duration_ms,
612
+ turn_count: r.turn_count,
613
+ prompt_preview: r.prompt_preview || ''
614
+ }))
615
+ };
616
+ }
617
+
618
+ /**
619
+ * Delete a conversation
620
+ */
621
+ deleteConversation(id: string): { success: boolean; error?: string } {
622
+ try {
623
+ const result = this.db.prepare('DELETE FROM conversations WHERE id = ?').run(id);
624
+ return { success: result.changes > 0 };
625
+ } catch (err) {
626
+ return { success: false, error: (err as Error).message };
627
+ }
628
+ }
629
+
630
+ /**
631
+ * Batch delete conversations
632
+ */
633
+ batchDelete(ids: string[]): { success: boolean; deleted: number; errors?: string[] } {
634
+ const deleteStmt = this.db.prepare('DELETE FROM conversations WHERE id = ?');
635
+ const errors: string[] = [];
636
+ let deleted = 0;
637
+
638
+ const transaction = this.db.transaction(() => {
639
+ for (const id of ids) {
640
+ try {
641
+ const result = deleteStmt.run(id);
642
+ if (result.changes > 0) deleted++;
643
+ } catch (err) {
644
+ errors.push(`${id}: ${(err as Error).message}`);
645
+ }
646
+ }
647
+ });
648
+
649
+ transaction();
650
+
651
+ return {
652
+ success: true,
653
+ deleted,
654
+ errors: errors.length > 0 ? errors : undefined
655
+ };
656
+ }
657
+
658
+ /**
659
+ * Delete conversations by tool
660
+ */
661
+ deleteByTool(tool: string): { success: boolean; deleted: number } {
662
+ const result = this.db.prepare('DELETE FROM conversations WHERE tool = ?').run(tool);
663
+ return { success: true, deleted: result.changes };
664
+ }
665
+
666
+ /**
667
+ * Delete all conversations
668
+ */
669
+ deleteAll(): { success: boolean; deleted: number } {
670
+ const count = (this.db.prepare('SELECT COUNT(*) as c FROM conversations').get() as any).c;
671
+ this.db.prepare('DELETE FROM conversations').run();
672
+ return { success: true, deleted: count };
673
+ }
674
+
675
+ /**
676
+ * Get statistics
677
+ */
678
+ getStats(): {
679
+ total: number;
680
+ byTool: Record<string, number>;
681
+ byStatus: Record<string, number>;
682
+ totalDuration: number;
683
+ } {
684
+ const total = (this.db.prepare('SELECT COUNT(*) as c FROM conversations').get() as any).c;
685
+
686
+ const byToolRows = this.db.prepare(`
687
+ SELECT tool, COUNT(*) as count FROM conversations GROUP BY tool
688
+ `).all() as any[];
689
+ const byTool: Record<string, number> = {};
690
+ for (const row of byToolRows) {
691
+ byTool[row.tool] = row.count;
692
+ }
693
+
694
+ const byStatusRows = this.db.prepare(`
695
+ SELECT latest_status, COUNT(*) as count FROM conversations GROUP BY latest_status
696
+ `).all() as any[];
697
+ const byStatus: Record<string, number> = {};
698
+ for (const row of byStatusRows) {
699
+ byStatus[row.latest_status] = row.count;
700
+ }
701
+
702
+ const totalDuration = (this.db.prepare(`
703
+ SELECT COALESCE(SUM(total_duration_ms), 0) as total FROM conversations
704
+ `).get() as any).total;
705
+
706
+ return { total, byTool, byStatus, totalDuration };
707
+ }
708
+
709
+ // ========== Native Session Mapping Methods ==========
710
+
711
+ /**
712
+ * Save or update native session mapping
713
+ */
714
+ saveNativeSessionMapping(mapping: NativeSessionMapping): void {
715
+ const stmt = this.db.prepare(`
716
+ INSERT INTO native_session_mapping (ccw_id, tool, native_session_id, native_session_path, project_hash, created_at)
717
+ VALUES (@ccw_id, @tool, @native_session_id, @native_session_path, @project_hash, @created_at)
718
+ ON CONFLICT(ccw_id) DO UPDATE SET
719
+ native_session_id = @native_session_id,
720
+ native_session_path = @native_session_path,
721
+ project_hash = @project_hash
722
+ `);
723
+
724
+ stmt.run({
725
+ ccw_id: mapping.ccw_id,
726
+ tool: mapping.tool,
727
+ native_session_id: mapping.native_session_id,
728
+ native_session_path: mapping.native_session_path || null,
729
+ project_hash: mapping.project_hash || null,
730
+ created_at: mapping.created_at || new Date().toISOString()
731
+ });
732
+ }
733
+
734
+ /**
735
+ * Get native session ID by CCW ID
736
+ */
737
+ getNativeSessionId(ccwId: string): string | null {
738
+ const row = this.db.prepare(`
739
+ SELECT native_session_id FROM native_session_mapping WHERE ccw_id = ?
740
+ `).get(ccwId) as any;
741
+ return row?.native_session_id || null;
742
+ }
743
+
744
+ /**
745
+ * Get CCW ID by native session ID
746
+ */
747
+ getCcwIdByNativeSession(tool: string, nativeSessionId: string): string | null {
748
+ const row = this.db.prepare(`
749
+ SELECT ccw_id FROM native_session_mapping WHERE tool = ? AND native_session_id = ?
750
+ `).get(tool, nativeSessionId) as any;
751
+ return row?.ccw_id || null;
752
+ }
753
+
754
+ /**
755
+ * Get full mapping by CCW ID
756
+ */
757
+ getNativeSessionMapping(ccwId: string): NativeSessionMapping | null {
758
+ const row = this.db.prepare(`
759
+ SELECT * FROM native_session_mapping WHERE ccw_id = ?
760
+ `).get(ccwId) as any;
761
+
762
+ if (!row) return null;
763
+
764
+ return {
765
+ ccw_id: row.ccw_id,
766
+ tool: row.tool,
767
+ native_session_id: row.native_session_id,
768
+ native_session_path: row.native_session_path,
769
+ project_hash: row.project_hash,
770
+ created_at: row.created_at
771
+ };
772
+ }
773
+
774
+ /**
775
+ * Get latest native session mapping for a tool
776
+ */
777
+ getLatestNativeMapping(tool: string): NativeSessionMapping | null {
778
+ const row = this.db.prepare(`
779
+ SELECT * FROM native_session_mapping
780
+ WHERE tool = ?
781
+ ORDER BY created_at DESC
782
+ LIMIT 1
783
+ `).get(tool) as any;
784
+
785
+ if (!row) return null;
786
+
787
+ return {
788
+ ccw_id: row.ccw_id,
789
+ tool: row.tool,
790
+ native_session_id: row.native_session_id,
791
+ native_session_path: row.native_session_path,
792
+ project_hash: row.project_hash,
793
+ created_at: row.created_at
794
+ };
795
+ }
796
+
797
+ /**
798
+ * Delete native session mapping
799
+ */
800
+ deleteNativeSessionMapping(ccwId: string): boolean {
801
+ const result = this.db.prepare('DELETE FROM native_session_mapping WHERE ccw_id = ?').run(ccwId);
802
+ return result.changes > 0;
803
+ }
804
+
805
+ /**
806
+ * Check if CCW ID has native session mapping
807
+ */
808
+ hasNativeSession(ccwId: string): boolean {
809
+ const row = this.db.prepare(`
810
+ SELECT 1 FROM native_session_mapping WHERE ccw_id = ? LIMIT 1
811
+ `).get(ccwId);
812
+ return !!row;
813
+ }
814
+
815
+ // ========== Native Session Content Methods ==========
816
+
817
+ /**
818
+ * Get parsed native session content by CCW ID
819
+ * Returns full conversation with all turns from native session file
820
+ */
821
+ getNativeSessionContent(ccwId: string): ParsedSession | null {
822
+ const mapping = this.getNativeSessionMapping(ccwId);
823
+ if (!mapping || !mapping.native_session_path) {
824
+ return null;
825
+ }
826
+
827
+ return parseSessionFile(mapping.native_session_path, mapping.tool);
828
+ }
829
+
830
+ /**
831
+ * Get formatted conversation text from native session
832
+ */
833
+ getFormattedNativeConversation(ccwId: string, options?: {
834
+ includeThoughts?: boolean;
835
+ includeToolCalls?: boolean;
836
+ includeTokens?: boolean;
837
+ maxContentLength?: number;
838
+ }): string | null {
839
+ const session = this.getNativeSessionContent(ccwId);
840
+ if (!session) {
841
+ return null;
842
+ }
843
+ return formatConversation(session, options);
844
+ }
845
+
846
+ /**
847
+ * Get conversation pairs (user prompt + assistant response) from native session
848
+ */
849
+ getNativeConversationPairs(ccwId: string): Array<{
850
+ turn: number;
851
+ userPrompt: string;
852
+ assistantResponse: string;
853
+ timestamp: string;
854
+ }> | null {
855
+ const session = this.getNativeSessionContent(ccwId);
856
+ if (!session) {
857
+ return null;
858
+ }
859
+ return extractConversationPairs(session);
860
+ }
861
+
862
+ /**
863
+ * Get conversation with enriched native session data
864
+ * Merges CCW history with native session content
865
+ */
866
+ getEnrichedConversation(ccwId: string): {
867
+ ccw: ConversationRecord | null;
868
+ native: ParsedSession | null;
869
+ merged: Array<{
870
+ turn: number;
871
+ timestamp: string;
872
+ ccwPrompt?: string;
873
+ ccwOutput?: string;
874
+ nativeUserContent?: string;
875
+ nativeAssistantContent?: string;
876
+ nativeThoughts?: string[];
877
+ nativeToolCalls?: Array<{ name: string; arguments?: string; output?: string }>;
878
+ }>;
879
+ } | null {
880
+ const ccwConv = this.getConversation(ccwId);
881
+ const nativeSession = this.getNativeSessionContent(ccwId);
882
+
883
+ if (!ccwConv && !nativeSession) {
884
+ return null;
885
+ }
886
+
887
+ const merged: Array<{
888
+ turn: number;
889
+ timestamp: string;
890
+ ccwPrompt?: string;
891
+ ccwOutput?: string;
892
+ nativeUserContent?: string;
893
+ nativeAssistantContent?: string;
894
+ nativeThoughts?: string[];
895
+ nativeToolCalls?: Array<{ name: string; arguments?: string; output?: string }>;
896
+ }> = [];
897
+
898
+ // Determine max turn count
899
+ const maxTurns = Math.max(
900
+ ccwConv?.turn_count || 0,
901
+ nativeSession?.turns.filter(t => t.role === 'user').length || 0
902
+ );
903
+
904
+ for (let i = 1; i <= maxTurns; i++) {
905
+ const ccwTurn = ccwConv?.turns.find(t => t.turn === i);
906
+ const nativeUserTurn = nativeSession?.turns.find(t => t.turnNumber === i && t.role === 'user');
907
+ const nativeAssistantTurn = nativeSession?.turns.find(t => t.turnNumber === i && t.role === 'assistant');
908
+
909
+ merged.push({
910
+ turn: i,
911
+ timestamp: ccwTurn?.timestamp || nativeUserTurn?.timestamp || '',
912
+ ccwPrompt: ccwTurn?.prompt,
913
+ ccwOutput: ccwTurn?.output.stdout,
914
+ nativeUserContent: nativeUserTurn?.content,
915
+ nativeAssistantContent: nativeAssistantTurn?.content,
916
+ nativeThoughts: nativeAssistantTurn?.thoughts,
917
+ nativeToolCalls: nativeAssistantTurn?.toolCalls
918
+ });
919
+ }
920
+
921
+ return { ccw: ccwConv, native: nativeSession, merged };
922
+ }
923
+
924
+ /**
925
+ * List all conversations with native session info
926
+ */
927
+ getHistoryWithNativeInfo(options: HistoryQueryOptions = {}): {
928
+ total: number;
929
+ count: number;
930
+ executions: Array<HistoryIndexEntry & {
931
+ hasNativeSession: boolean;
932
+ nativeSessionId?: string;
933
+ nativeSessionPath?: string;
934
+ }>;
935
+ } {
936
+ const history = this.getHistory(options);
937
+
938
+ const enrichedExecutions = history.executions.map(exec => {
939
+ const mapping = this.getNativeSessionMapping(exec.id);
940
+ return {
941
+ ...exec,
942
+ hasNativeSession: !!mapping,
943
+ nativeSessionId: mapping?.native_session_id,
944
+ nativeSessionPath: mapping?.native_session_path
945
+ };
946
+ });
947
+
948
+ return {
949
+ total: history.total,
950
+ count: history.count,
951
+ executions: enrichedExecutions
952
+ };
953
+ }
954
+
955
+ // ========== Insights Methods ==========
956
+
957
+ /**
958
+ * Save an insights analysis result
959
+ */
960
+ saveInsight(insight: {
961
+ id: string;
962
+ tool: string;
963
+ promptCount: number;
964
+ patterns: any[];
965
+ suggestions: any[];
966
+ rawOutput?: string;
967
+ executionId?: string;
968
+ lang?: string;
969
+ }): void {
970
+ const stmt = this.db.prepare(`
971
+ INSERT OR REPLACE INTO insights (id, created_at, tool, prompt_count, patterns, suggestions, raw_output, execution_id, lang)
972
+ VALUES (@id, @created_at, @tool, @prompt_count, @patterns, @suggestions, @raw_output, @execution_id, @lang)
973
+ `);
974
+
975
+ stmt.run({
976
+ id: insight.id,
977
+ created_at: new Date().toISOString(),
978
+ tool: insight.tool,
979
+ prompt_count: insight.promptCount,
980
+ patterns: JSON.stringify(insight.patterns || []),
981
+ suggestions: JSON.stringify(insight.suggestions || []),
982
+ raw_output: insight.rawOutput || null,
983
+ execution_id: insight.executionId || null,
984
+ lang: insight.lang || 'en'
985
+ });
986
+ }
987
+
988
+ /**
989
+ * Get insights history
990
+ */
991
+ getInsights(options: { limit?: number; tool?: string } = {}): {
992
+ id: string;
993
+ created_at: string;
994
+ tool: string;
995
+ prompt_count: number;
996
+ patterns: any[];
997
+ suggestions: any[];
998
+ execution_id: string | null;
999
+ lang: string;
1000
+ }[] {
1001
+ const { limit = 20, tool } = options;
1002
+
1003
+ let sql = 'SELECT id, created_at, tool, prompt_count, patterns, suggestions, execution_id, lang FROM insights';
1004
+ const params: any = {};
1005
+
1006
+ if (tool) {
1007
+ sql += ' WHERE tool = @tool';
1008
+ params.tool = tool;
1009
+ }
1010
+
1011
+ sql += ' ORDER BY created_at DESC LIMIT @limit';
1012
+ params.limit = limit;
1013
+
1014
+ const rows = this.db.prepare(sql).all(params) as any[];
1015
+
1016
+ return rows.map(row => ({
1017
+ ...row,
1018
+ patterns: JSON.parse(row.patterns || '[]'),
1019
+ suggestions: JSON.parse(row.suggestions || '[]')
1020
+ }));
1021
+ }
1022
+
1023
+ /**
1024
+ * Get a single insight by ID
1025
+ */
1026
+ getInsight(id: string): {
1027
+ id: string;
1028
+ created_at: string;
1029
+ tool: string;
1030
+ prompt_count: number;
1031
+ patterns: any[];
1032
+ suggestions: any[];
1033
+ raw_output: string | null;
1034
+ execution_id: string | null;
1035
+ lang: string;
1036
+ } | null {
1037
+ const row = this.db.prepare(
1038
+ 'SELECT * FROM insights WHERE id = ?'
1039
+ ).get(id) as any;
1040
+
1041
+ if (!row) return null;
1042
+
1043
+ return {
1044
+ ...row,
1045
+ patterns: JSON.parse(row.patterns || '[]'),
1046
+ suggestions: JSON.parse(row.suggestions || '[]')
1047
+ };
1048
+ }
1049
+
1050
+ /**
1051
+ * Delete an insight
1052
+ */
1053
+ deleteInsight(id: string): boolean {
1054
+ const result = this.db.prepare('DELETE FROM insights WHERE id = ?').run(id);
1055
+ return result.changes > 0;
1056
+ }
1057
+
1058
+ /**
1059
+ * Save or update a review for an execution
1060
+ */
1061
+ saveReview(review: Omit<ReviewRecord, 'id' | 'created_at' | 'updated_at'> & { created_at?: string; updated_at?: string }): ReviewRecord {
1062
+ const now = new Date().toISOString();
1063
+ const created_at = review.created_at || now;
1064
+ const updated_at = review.updated_at || now;
1065
+
1066
+ const stmt = this.db.prepare(`
1067
+ INSERT INTO reviews (execution_id, status, rating, comments, reviewer, created_at, updated_at)
1068
+ VALUES (@execution_id, @status, @rating, @comments, @reviewer, @created_at, @updated_at)
1069
+ ON CONFLICT(execution_id) DO UPDATE SET
1070
+ status = @status,
1071
+ rating = @rating,
1072
+ comments = @comments,
1073
+ reviewer = @reviewer,
1074
+ updated_at = @updated_at
1075
+ `);
1076
+
1077
+ const result = stmt.run({
1078
+ execution_id: review.execution_id,
1079
+ status: review.status,
1080
+ rating: review.rating ?? null,
1081
+ comments: review.comments ?? null,
1082
+ reviewer: review.reviewer ?? null,
1083
+ created_at,
1084
+ updated_at
1085
+ });
1086
+
1087
+ return {
1088
+ id: result.lastInsertRowid as number,
1089
+ execution_id: review.execution_id,
1090
+ status: review.status,
1091
+ rating: review.rating,
1092
+ comments: review.comments,
1093
+ reviewer: review.reviewer,
1094
+ created_at,
1095
+ updated_at
1096
+ };
1097
+ }
1098
+
1099
+ /**
1100
+ * Get review for an execution
1101
+ */
1102
+ getReview(executionId: string): ReviewRecord | null {
1103
+ const row = this.db.prepare(
1104
+ 'SELECT * FROM reviews WHERE execution_id = ?'
1105
+ ).get(executionId) as any;
1106
+
1107
+ if (!row) return null;
1108
+
1109
+ return {
1110
+ id: row.id,
1111
+ execution_id: row.execution_id,
1112
+ status: row.status as ReviewStatus,
1113
+ rating: row.rating,
1114
+ comments: row.comments,
1115
+ reviewer: row.reviewer,
1116
+ created_at: row.created_at,
1117
+ updated_at: row.updated_at
1118
+ };
1119
+ }
1120
+
1121
+ /**
1122
+ * Get reviews with optional filtering
1123
+ */
1124
+ getReviews(options: { status?: ReviewStatus; limit?: number } = {}): ReviewRecord[] {
1125
+ const { status, limit = 50 } = options;
1126
+
1127
+ let sql = 'SELECT * FROM reviews';
1128
+ const params: any = { limit };
1129
+
1130
+ if (status) {
1131
+ sql += ' WHERE status = @status';
1132
+ params.status = status;
1133
+ }
1134
+
1135
+ sql += ' ORDER BY updated_at DESC LIMIT @limit';
1136
+
1137
+ const rows = this.db.prepare(sql).all(params) as any[];
1138
+
1139
+ return rows.map(row => ({
1140
+ id: row.id,
1141
+ execution_id: row.execution_id,
1142
+ status: row.status as ReviewStatus,
1143
+ rating: row.rating,
1144
+ comments: row.comments,
1145
+ reviewer: row.reviewer,
1146
+ created_at: row.created_at,
1147
+ updated_at: row.updated_at
1148
+ }));
1149
+ }
1150
+
1151
+ /**
1152
+ * Delete a review
1153
+ */
1154
+ deleteReview(executionId: string): boolean {
1155
+ const result = this.db.prepare('DELETE FROM reviews WHERE execution_id = ?').run(executionId);
1156
+ return result.changes > 0;
1157
+ }
1158
+
1159
+ /**
1160
+ * Close database connection
1161
+ */
1162
+ close(): void {
1163
+ this.db.close();
1164
+ }
1165
+ }
1166
+
1167
+ // Singleton instance cache - keyed by normalized project ID for consistency
1168
+ const storeCache = new Map<string, CliHistoryStore>();
1169
+
1170
+ /**
1171
+ * Get or create a store instance for a directory
1172
+ * Uses normalized project ID as cache key to handle path casing differences
1173
+ */
1174
+ export function getHistoryStore(baseDir: string): CliHistoryStore {
1175
+ // Use getProjectId to normalize path for consistent cache key
1176
+ const cacheKey = getProjectId(baseDir);
1177
+
1178
+ if (!storeCache.has(cacheKey)) {
1179
+ storeCache.set(cacheKey, new CliHistoryStore(baseDir));
1180
+ }
1181
+ return storeCache.get(cacheKey)!;
1182
+ }
1183
+
1184
+ /**
1185
+ * Close all store instances
1186
+ */
1187
+ export function closeAllStores(): void {
1188
+ for (const store of storeCache.values()) {
1189
+ store.close();
1190
+ }
1191
+ storeCache.clear();
1192
+ }
1193
+
1194
+ // Re-export types from session-content-parser
1195
+ export type { ParsedSession, ParsedTurn } from './session-content-parser.js';