claude-flow-novice 2.0.3 → 2.0.4

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 (272) hide show
  1. package/dist/src/cli/commands/guidance.js +487 -668
  2. package/dist/src/cli/commands/index-validate.js +18 -29
  3. package/dist/src/cli/commands/mcp-troubleshoot.js +230 -282
  4. package/dist/src/cli/commands/neural-goal-init.js +92 -125
  5. package/dist/src/cli/commands/swarm-exec.js +317 -393
  6. package/dist/src/cli/commands/swarm.js +1 -1
  7. package/dist/src/cli/commands/validate-framework.js +983 -1100
  8. package/dist/src/cli/commands/validate.js +144 -223
  9. package/dist/src/cli/simple-commands/__tests__/agent.test.js +265 -277
  10. package/dist/src/cli/simple-commands/__tests__/memory.test.js +6 -7
  11. package/dist/src/cli/simple-commands/__tests__/swarm.test.js +373 -356
  12. package/dist/src/cli/simple-commands/__tests__/task.test.js +6 -7
  13. package/dist/src/cli/simple-commands/agent.js +157 -193
  14. package/dist/src/cli/simple-commands/analysis.js +336 -446
  15. package/dist/src/cli/simple-commands/automation-executor.js +1095 -1339
  16. package/dist/src/cli/simple-commands/automation.js +481 -469
  17. package/dist/src/cli/simple-commands/batch-manager.js +261 -313
  18. package/dist/src/cli/simple-commands/claude-telemetry.js +241 -267
  19. package/dist/src/cli/simple-commands/claude-track.js +68 -90
  20. package/dist/src/cli/simple-commands/concurrent-display.js +266 -320
  21. package/dist/src/cli/simple-commands/config.js +245 -290
  22. package/dist/src/cli/simple-commands/coordination.js +182 -234
  23. package/dist/src/cli/simple-commands/enhanced-ui-views.js +812 -615
  24. package/dist/src/cli/simple-commands/enhanced-webui-complete.js +922 -981
  25. package/dist/src/cli/simple-commands/fix-hook-variables.js +274 -294
  26. package/dist/src/cli/simple-commands/github/gh-coordinator.js +378 -457
  27. package/dist/src/cli/simple-commands/github/github-api.js +535 -574
  28. package/dist/src/cli/simple-commands/github/init.js +276 -303
  29. package/dist/src/cli/simple-commands/github.js +222 -247
  30. package/dist/src/cli/simple-commands/goal.js +51 -63
  31. package/dist/src/cli/simple-commands/hive-mind/auto-save-middleware.js +208 -278
  32. package/dist/src/cli/simple-commands/hive-mind/communication.js +601 -696
  33. package/dist/src/cli/simple-commands/hive-mind/core.js +907 -979
  34. package/dist/src/cli/simple-commands/hive-mind/db-optimizer.js +406 -655
  35. package/dist/src/cli/simple-commands/hive-mind/mcp-wrapper.js +1125 -1245
  36. package/dist/src/cli/simple-commands/hive-mind/memory.js +854 -1090
  37. package/dist/src/cli/simple-commands/hive-mind/performance-optimizer.js +459 -574
  38. package/dist/src/cli/simple-commands/hive-mind/performance-test.js +263 -347
  39. package/dist/src/cli/simple-commands/hive-mind/queen.js +727 -768
  40. package/dist/src/cli/simple-commands/hive-mind/session-manager.js +745 -1049
  41. package/dist/src/cli/simple-commands/hive-mind-optimize.js +227 -283
  42. package/dist/src/cli/simple-commands/hive-mind-wizard.js +174 -217
  43. package/dist/src/cli/simple-commands/hive-mind.js +1842 -2283
  44. package/dist/src/cli/simple-commands/hive.js +90 -79
  45. package/dist/src/cli/simple-commands/hook-safety.js +431 -521
  46. package/dist/src/cli/simple-commands/hooks/session-start-soul.js +203 -254
  47. package/dist/src/cli/simple-commands/hooks.js +1064 -1204
  48. package/dist/src/cli/simple-commands/init/agent-copier.js +294 -319
  49. package/dist/src/cli/simple-commands/init/batch-init.js +496 -562
  50. package/dist/src/cli/simple-commands/init/claude-commands/claude-flow-commands.js +13 -19
  51. package/dist/src/cli/simple-commands/init/claude-commands/optimized-claude-flow-commands.js +13 -19
  52. package/dist/src/cli/simple-commands/init/claude-commands/optimized-slash-commands.js +61 -88
  53. package/dist/src/cli/simple-commands/init/claude-commands/optimized-sparc-commands.js +125 -150
  54. package/dist/src/cli/simple-commands/init/claude-commands/slash-commands.js +42 -49
  55. package/dist/src/cli/simple-commands/init/claude-commands/sparc-commands.js +43 -61
  56. package/dist/src/cli/simple-commands/init/copy-revised-templates.js +141 -147
  57. package/dist/src/cli/simple-commands/init/executable-wrapper.js +31 -44
  58. package/dist/src/cli/simple-commands/init/gitignore-updater.js +64 -90
  59. package/dist/src/cli/simple-commands/init/help.js +104 -107
  60. package/dist/src/cli/simple-commands/init/hive-mind-init.js +509 -528
  61. package/dist/src/cli/simple-commands/init/index.js +1510 -1759
  62. package/dist/src/cli/simple-commands/init/performance-monitor.js +234 -317
  63. package/dist/src/cli/simple-commands/init/rollback/backup-manager.js +441 -504
  64. package/dist/src/cli/simple-commands/init/rollback/index.js +289 -364
  65. package/dist/src/cli/simple-commands/init/rollback/recovery-manager.js +652 -728
  66. package/dist/src/cli/simple-commands/init/rollback/rollback-executor.js +416 -481
  67. package/dist/src/cli/simple-commands/init/rollback/state-tracker.js +369 -448
  68. package/dist/src/cli/simple-commands/init/sparc/roo-readme.js +1 -2
  69. package/dist/src/cli/simple-commands/init/sparc/roomodes-config.js +122 -99
  70. package/dist/src/cli/simple-commands/init/sparc/workflows.js +32 -37
  71. package/dist/src/cli/simple-commands/init/sparc-structure.js +55 -62
  72. package/dist/src/cli/simple-commands/init/template-copier.js +421 -533
  73. package/dist/src/cli/simple-commands/init/templates/coordination-md.js +3 -6
  74. package/dist/src/cli/simple-commands/init/templates/enhanced-templates.js +344 -318
  75. package/dist/src/cli/simple-commands/init/templates/github-safe-enhanced.js +173 -218
  76. package/dist/src/cli/simple-commands/init/templates/github-safe.js +65 -75
  77. package/dist/src/cli/simple-commands/init/templates/memory-bank-md.js +3 -6
  78. package/dist/src/cli/simple-commands/init/templates/readme-files.js +2 -4
  79. package/dist/src/cli/simple-commands/init/templates/safe-hook-patterns.js +187 -230
  80. package/dist/src/cli/simple-commands/init/templates/sparc-modes.js +53 -80
  81. package/dist/src/cli/simple-commands/init/templates/verification-claude-md.js +101 -85
  82. package/dist/src/cli/simple-commands/init/validation/config-validator.js +283 -330
  83. package/dist/src/cli/simple-commands/init/validation/health-checker.js +495 -561
  84. package/dist/src/cli/simple-commands/init/validation/index.js +302 -358
  85. package/dist/src/cli/simple-commands/init/validation/mode-validator.js +308 -359
  86. package/dist/src/cli/simple-commands/init/validation/post-init-validator.js +389 -366
  87. package/dist/src/cli/simple-commands/init/validation/pre-init-validator.js +270 -268
  88. package/dist/src/cli/simple-commands/init/validation/test-runner.js +427 -447
  89. package/dist/src/cli/simple-commands/init.js +1 -2
  90. package/dist/src/cli/simple-commands/mcp-health.js +131 -158
  91. package/dist/src/cli/simple-commands/mcp-integration-layer.js +533 -634
  92. package/dist/src/cli/simple-commands/mcp.js +345 -400
  93. package/dist/src/cli/simple-commands/memory-consolidation.js +426 -537
  94. package/dist/src/cli/simple-commands/memory.js +247 -311
  95. package/dist/src/cli/simple-commands/migrate-hooks.js +39 -46
  96. package/dist/src/cli/simple-commands/monitor.js +294 -363
  97. package/dist/src/cli/simple-commands/neural.js +51 -65
  98. package/dist/src/cli/simple-commands/pair-autofix-only.js +538 -662
  99. package/dist/src/cli/simple-commands/pair-basic.js +528 -656
  100. package/dist/src/cli/simple-commands/pair-old.js +430 -543
  101. package/dist/src/cli/simple-commands/pair-working.js +615 -751
  102. package/dist/src/cli/simple-commands/pair.js +615 -751
  103. package/dist/src/cli/simple-commands/performance-hooks.js +83 -111
  104. package/dist/src/cli/simple-commands/performance-metrics.js +348 -433
  105. package/dist/src/cli/simple-commands/process-ui-enhanced.js +708 -787
  106. package/dist/src/cli/simple-commands/process-ui.js +230 -254
  107. package/dist/src/cli/simple-commands/realtime-update-system.js +525 -611
  108. package/dist/src/cli/simple-commands/sparc/architecture.js +1704 -1530
  109. package/dist/src/cli/simple-commands/sparc/commands.js +438 -516
  110. package/dist/src/cli/simple-commands/sparc/completion.js +1224 -1481
  111. package/dist/src/cli/simple-commands/sparc/coordinator.js +913 -978
  112. package/dist/src/cli/simple-commands/sparc/index.js +241 -298
  113. package/dist/src/cli/simple-commands/sparc/phase-base.js +314 -390
  114. package/dist/src/cli/simple-commands/sparc/pseudocode.js +965 -869
  115. package/dist/src/cli/simple-commands/sparc/refinement.js +980 -1273
  116. package/dist/src/cli/simple-commands/sparc/specification.js +559 -645
  117. package/dist/src/cli/simple-commands/sparc-modes/architect.js +1 -1
  118. package/dist/src/cli/simple-commands/sparc-modes/ask.js +1 -1
  119. package/dist/src/cli/simple-commands/sparc-modes/code.js +1 -1
  120. package/dist/src/cli/simple-commands/sparc-modes/debug.js +1 -1
  121. package/dist/src/cli/simple-commands/sparc-modes/devops.js +1 -1
  122. package/dist/src/cli/simple-commands/sparc-modes/docs-writer.js +1 -1
  123. package/dist/src/cli/simple-commands/sparc-modes/generic.js +1 -1
  124. package/dist/src/cli/simple-commands/sparc-modes/index.js +47 -55
  125. package/dist/src/cli/simple-commands/sparc-modes/integration.js +1 -1
  126. package/dist/src/cli/simple-commands/sparc-modes/mcp.js +1 -1
  127. package/dist/src/cli/simple-commands/sparc-modes/monitoring.js +1 -1
  128. package/dist/src/cli/simple-commands/sparc-modes/optimization.js +1 -1
  129. package/dist/src/cli/simple-commands/sparc-modes/security-review.js +1 -1
  130. package/dist/src/cli/simple-commands/sparc-modes/sparc-orchestrator.js +1 -1
  131. package/dist/src/cli/simple-commands/sparc-modes/spec-pseudocode.js +1 -1
  132. package/dist/src/cli/simple-commands/sparc-modes/supabase-admin.js +1 -1
  133. package/dist/src/cli/simple-commands/sparc-modes/swarm.js +101 -87
  134. package/dist/src/cli/simple-commands/sparc-modes/tdd.js +1 -1
  135. package/dist/src/cli/simple-commands/sparc-modes/tutorial.js +1 -1
  136. package/dist/src/cli/simple-commands/sparc.js +465 -493
  137. package/dist/src/cli/simple-commands/start-ui.js +108 -132
  138. package/dist/src/cli/simple-commands/start-wrapper.js +240 -268
  139. package/dist/src/cli/simple-commands/start.js +1 -1
  140. package/dist/src/cli/simple-commands/status.js +254 -275
  141. package/dist/src/cli/simple-commands/stream-chain-clean.js +128 -171
  142. package/dist/src/cli/simple-commands/stream-chain-fixed.js +61 -82
  143. package/dist/src/cli/simple-commands/stream-chain-real.js +267 -331
  144. package/dist/src/cli/simple-commands/stream-chain-working.js +211 -263
  145. package/dist/src/cli/simple-commands/stream-chain.js +260 -318
  146. package/dist/src/cli/simple-commands/stream-processor.js +290 -315
  147. package/dist/src/cli/simple-commands/swarm-executor.js +189 -222
  148. package/dist/src/cli/simple-commands/swarm-metrics-integration.js +208 -300
  149. package/dist/src/cli/simple-commands/swarm-ui.js +623 -703
  150. package/dist/src/cli/simple-commands/swarm-webui-integration.js +258 -286
  151. package/dist/src/cli/simple-commands/swarm.js +887 -1082
  152. package/dist/src/cli/simple-commands/task.js +161 -206
  153. package/dist/src/cli/simple-commands/timestamp-fix.js +59 -89
  154. package/dist/src/cli/simple-commands/token-tracker.js +258 -316
  155. package/dist/src/cli/simple-commands/tool-execution-framework.js +433 -519
  156. package/dist/src/cli/simple-commands/train-and-stream.js +275 -331
  157. package/dist/src/cli/simple-commands/training-pipeline.js +619 -725
  158. package/dist/src/cli/simple-commands/training.js +170 -227
  159. package/dist/src/cli/simple-commands/verification-hooks.js +261 -284
  160. package/dist/src/cli/simple-commands/verification-integration.js +389 -417
  161. package/dist/src/cli/simple-commands/verification-training-integration.js +486 -606
  162. package/dist/src/cli/simple-commands/verification.js +493 -513
  163. package/dist/src/cli/simple-commands/web-server.js +766 -836
  164. package/dist/src/cli/simple-commands/webui-validator.js +106 -124
  165. package/dist/src/coordination/event-bus/demo-wasm-integration.js +212 -251
  166. package/dist/src/coordination/event-bus/qe-event-bus.js +608 -748
  167. package/dist/src/coordination/event-bus/qe-event-bus.test.js +379 -454
  168. package/dist/src/coordination/iteration-tracker.js +363 -454
  169. package/dist/src/enterprise/analytics-manager.js +1135 -0
  170. package/dist/src/enterprise/audit-manager.js +1115 -0
  171. package/dist/src/enterprise/cloud-manager.js +891 -0
  172. package/dist/src/enterprise/deployment-manager.js +966 -0
  173. package/dist/src/enterprise/index.js +6 -0
  174. package/dist/src/enterprise/project-manager.js +584 -0
  175. package/dist/src/enterprise/security-manager.js +991 -0
  176. package/dist/src/index.js +1 -1
  177. package/dist/src/mcp/DEPRECATED.js +46 -60
  178. package/dist/src/mcp/fixes/mcp-error-fixes.js +115 -134
  179. package/dist/src/mcp/implementations/agent-tracker.js +114 -128
  180. package/dist/src/mcp/implementations/daa-tools.js +292 -350
  181. package/dist/src/mcp/implementations/workflow-tools.js +329 -361
  182. package/dist/src/mcp/mcp-config-manager.js +1183 -1331
  183. package/dist/src/mcp/mcp-server-novice-simplified.js +11 -17
  184. package/dist/src/mcp/mcp-server-novice.js +11 -17
  185. package/dist/src/mcp/mcp-server-sdk.js +11 -17
  186. package/dist/src/mcp/mcp-server.js +1620 -1484
  187. package/dist/src/mcp/ruv-swarm-wrapper.js +209 -239
  188. package/dist/src/memory/advanced-serializer.js +609 -589
  189. package/dist/src/memory/enhanced-examples.js +220 -305
  190. package/dist/src/memory/enhanced-memory.js +295 -336
  191. package/dist/src/memory/enhanced-session-serializer.js +408 -492
  192. package/dist/src/memory/fallback-memory-system.js +900 -1021
  193. package/dist/src/memory/fallback-store.js +93 -131
  194. package/dist/src/memory/high-performance-serialization.js +592 -730
  195. package/dist/src/memory/in-memory-store.js +161 -213
  196. package/dist/src/memory/index.js +123 -157
  197. package/dist/src/memory/lock-free-structures.js +578 -764
  198. package/dist/src/memory/memory-mapped-persistence.js +585 -766
  199. package/dist/src/memory/memory-pressure-manager.js +569 -707
  200. package/dist/src/memory/migration.js +358 -445
  201. package/dist/src/memory/shared-memory.js +641 -768
  202. package/dist/src/memory/sqlite-store.js +245 -325
  203. package/dist/src/memory/sqlite-wrapper.js +122 -151
  204. package/dist/src/memory/swarm-memory.js +470 -603
  205. package/dist/src/memory/test-example.js +126 -134
  206. package/dist/src/memory/ultra-fast-memory-store.js +622 -821
  207. package/dist/src/memory/unified-memory-manager.js +356 -437
  208. package/dist/src/migration/index.js +92 -0
  209. package/dist/src/migration/logger.js +121 -0
  210. package/dist/src/migration/migration-analyzer.js +268 -0
  211. package/dist/src/migration/migration-runner.js +522 -0
  212. package/dist/src/migration/migration-validator.js +285 -0
  213. package/dist/src/migration/progress-reporter.js +150 -0
  214. package/dist/src/migration/rollback-manager.js +321 -0
  215. package/dist/src/migration/tests/migration-system.test.js +7 -0
  216. package/dist/src/migration/types.js +3 -0
  217. package/dist/src/swarm/CodeRefactoringSwarm.js +777 -952
  218. package/dist/src/swarm/__tests__/integration.test.js +227 -0
  219. package/dist/src/swarm/__tests__/prompt-copier.test.js +344 -0
  220. package/dist/src/swarm/advanced-orchestrator.js +1095 -0
  221. package/dist/src/swarm/claude-code-interface.js +961 -0
  222. package/dist/src/swarm/claude-flow-executor.js +229 -0
  223. package/dist/src/swarm/consensus-coordinator.js +475 -0
  224. package/dist/src/swarm/coordinator.js +2993 -0
  225. package/dist/src/swarm/direct-executor.js +1180 -0
  226. package/dist/src/swarm/error-recovery/advanced-error-detection.js +691 -0
  227. package/dist/src/swarm/error-recovery/automated-recovery-workflows.js +998 -0
  228. package/dist/src/swarm/error-recovery/error-recovery-coordinator.js +1197 -0
  229. package/dist/src/swarm/error-recovery/recovery-monitoring.js +772 -0
  230. package/dist/src/swarm/error-recovery/resilience-architecture.js +714 -0
  231. package/dist/src/swarm/error-recovery/self-healing-mechanisms.js +1319 -0
  232. package/dist/src/swarm/error-recovery/test-error-recovery-effectiveness.js +808 -0
  233. package/dist/src/swarm/executor-v2.js +322 -0
  234. package/dist/src/swarm/executor.js +815 -0
  235. package/dist/src/swarm/hive-mind-integration.js +703 -0
  236. package/dist/src/swarm/index.js +41 -0
  237. package/dist/src/swarm/json-output-aggregator.js +267 -0
  238. package/dist/src/swarm/large-scale-coordinator.js +542 -0
  239. package/dist/src/swarm/mcp-integration-wrapper.js +628 -0
  240. package/dist/src/swarm/memory.js +1117 -0
  241. package/dist/src/swarm/optimizations/__tests__/optimization.test.js +348 -0
  242. package/dist/src/swarm/optimizations/async-file-manager.js +285 -0
  243. package/dist/src/swarm/optimizations/circular-buffer.js +162 -0
  244. package/dist/src/swarm/optimizations/connection-pool.js +244 -0
  245. package/dist/src/swarm/optimizations/index.js +28 -0
  246. package/dist/src/swarm/optimizations/optimized-executor.js +320 -0
  247. package/dist/src/swarm/optimizations/ttl-map.js +234 -0
  248. package/dist/src/swarm/prompt-cli.js +200 -0
  249. package/dist/src/swarm/prompt-copier-enhanced.js +202 -0
  250. package/dist/src/swarm/prompt-copier.js +381 -0
  251. package/dist/src/swarm/prompt-manager.js +295 -0
  252. package/dist/src/swarm/prompt-utils.js +310 -0
  253. package/dist/src/swarm/result-aggregator.js +718 -0
  254. package/dist/src/swarm/sparc-executor.js +1568 -0
  255. package/dist/src/swarm/strategies/auto.js +758 -0
  256. package/dist/src/swarm/strategies/base.js +128 -0
  257. package/dist/src/swarm/strategies/research.js +914 -0
  258. package/dist/src/swarm/strategies/strategy-metrics-patch.js +2 -0
  259. package/dist/src/swarm/types.js +52 -0
  260. package/dist/src/swarm/workers/copy-worker.js +56 -0
  261. package/dist/src/utils/__tests__/github-cli-safety-wrapper.test.js +332 -400
  262. package/dist/src/utils/github-cli-safe.js +56 -64
  263. package/dist/src/utils/github-cli-safety-wrapper.js +451 -546
  264. package/dist/src/utils/npx-isolated-cache.js +104 -119
  265. package/dist/src/utils/preference-manager.js +622 -652
  266. package/dist/src/utils/timezone-utils.js +86 -105
  267. package/dist/src/validators/epic-config-schema.js +214 -0
  268. package/dist/src/validators/index.js +10 -0
  269. package/dist/src/validators/swarm-init-validator.js +259 -0
  270. package/dist/src/validators/todowrite-batching-validator.js +215 -0
  271. package/dist/src/validators/todowrite-integration.js +187 -0
  272. package/package.json +2 -2
@@ -3,425 +3,282 @@
3
3
  *
4
4
  * Safe, backward-compatible database optimization for existing deployments
5
5
  * Adds indexes, performance improvements, and new features without breaking changes
6
- */
7
-
8
- import Database from 'better-sqlite3';
9
- import { existsSync, mkdirSync } from 'fs';
10
- import { writeFile, readFile } from 'fs/promises';
11
- import path from 'path';
12
- import chalk from 'chalk';
13
- import ora from 'ora';
14
-
6
+ */ import Database from "better-sqlite3";
7
+ import chalk from "chalk";
8
+ import ora from "ora";
15
9
  /**
16
10
  * Optimize existing hive mind database with backward compatibility
17
- */
18
- export async function optimizeHiveMindDatabase(dbPath, options = {}) {
19
- const spinner = ora('Optimizing Hive Mind database...').start();
20
-
21
- try {
22
- // Open database with write-ahead logging for better performance
23
- const db = new Database(dbPath, {
24
- verbose: options.verbose ? console.log : null,
25
- });
26
-
27
- // Enable WAL mode for better concurrency
28
- db.pragma('journal_mode = WAL');
29
- db.pragma('synchronous = NORMAL');
30
-
31
- // Get current schema version
32
- const schemaVersion = getSchemaVersion(db);
33
- spinner.text = `Current schema version: ${schemaVersion}`;
34
-
35
- // Apply optimizations based on version
36
- let optimizationsApplied = [];
37
-
38
- // Version 1.0 -> 1.1: Add basic indexes
39
- if (schemaVersion < 1.1) {
40
- spinner.text = 'Applying performance indexes...';
41
- applyBasicIndexes(db);
42
- optimizationsApplied.push('Basic performance indexes');
43
- }
44
-
45
- // Version 1.1 -> 1.2: Add advanced indexes and analyze
46
- if (schemaVersion < 1.2) {
47
- spinner.text = 'Applying advanced indexes...';
48
- applyAdvancedIndexes(db);
49
- optimizationsApplied.push('Advanced query optimization');
50
- }
51
-
52
- // Version 1.2 -> 1.3: Add performance tracking tables
53
- if (schemaVersion < 1.3) {
54
- spinner.text = 'Adding performance tracking...';
55
- addPerformanceTracking(db);
56
- optimizationsApplied.push('Performance monitoring tables');
57
- }
58
-
59
- // Version 1.3 -> 1.4: Add memory optimization
60
- if (schemaVersion < 1.4) {
61
- spinner.text = 'Optimizing memory management...';
62
- addMemoryOptimization(db);
63
- optimizationsApplied.push('Memory optimization features');
64
- }
65
-
66
- // Version 1.4 -> 1.5: Add behavioral tracking
67
- if (schemaVersion < 1.5) {
68
- spinner.text = 'Adding behavioral analysis...';
69
- addBehavioralTracking(db);
70
- optimizationsApplied.push('Behavioral pattern tracking');
71
- }
72
-
73
- // Run ANALYZE to update query planner statistics
74
- spinner.text = 'Updating query statistics...';
75
- db.exec('ANALYZE');
76
-
77
- // Vacuum if requested (requires exclusive access)
78
- if (options.vacuum) {
79
- spinner.text = 'Vacuuming database...';
80
- db.exec('VACUUM');
81
- optimizationsApplied.push('Database vacuumed');
82
- }
83
-
84
- // Update schema version
85
- updateSchemaVersion(db, 1.5);
86
-
87
- // Close database
88
- db.close();
89
-
90
- spinner.succeed('Database optimization complete!');
91
-
92
- if (optimizationsApplied.length > 0) {
93
- console.log('\n' + chalk.green('✓') + ' Optimizations applied:');
94
- optimizationsApplied.forEach((opt) => {
95
- console.log(' - ' + opt);
96
- });
97
- } else {
98
- console.log('\n' + chalk.yellow('ℹ') + ' Database already optimized');
11
+ */ export async function optimizeHiveMindDatabase(dbPath, options = {}) {
12
+ const spinner = ora('Optimizing Hive Mind database...').start();
13
+ try {
14
+ // Open database with write-ahead logging for better performance
15
+ const db = new Database(dbPath, {
16
+ verbose: options.verbose ? console.log : null
17
+ });
18
+ // Enable WAL mode for better concurrency
19
+ db.pragma('journal_mode = WAL');
20
+ db.pragma('synchronous = NORMAL');
21
+ // Get current schema version
22
+ const schemaVersion = getSchemaVersion(db);
23
+ spinner.text = `Current schema version: ${schemaVersion}`;
24
+ // Apply optimizations based on version
25
+ let optimizationsApplied = [];
26
+ // Version 1.0 -> 1.1: Add basic indexes
27
+ if (schemaVersion < 1.1) {
28
+ spinner.text = 'Applying performance indexes...';
29
+ applyBasicIndexes(db);
30
+ optimizationsApplied.push('Basic performance indexes');
31
+ }
32
+ // Version 1.1 -> 1.2: Add advanced indexes and analyze
33
+ if (schemaVersion < 1.2) {
34
+ spinner.text = 'Applying advanced indexes...';
35
+ applyAdvancedIndexes(db);
36
+ optimizationsApplied.push('Advanced query optimization');
37
+ }
38
+ // Version 1.2 -> 1.3: Add performance tracking tables
39
+ if (schemaVersion < 1.3) {
40
+ spinner.text = 'Adding performance tracking...';
41
+ addPerformanceTracking(db);
42
+ optimizationsApplied.push('Performance monitoring tables');
43
+ }
44
+ // Version 1.3 -> 1.4: Add memory optimization
45
+ if (schemaVersion < 1.4) {
46
+ spinner.text = 'Optimizing memory management...';
47
+ addMemoryOptimization(db);
48
+ optimizationsApplied.push('Memory optimization features');
49
+ }
50
+ // Version 1.4 -> 1.5: Add behavioral tracking
51
+ if (schemaVersion < 1.5) {
52
+ spinner.text = 'Adding behavioral analysis...';
53
+ addBehavioralTracking(db);
54
+ optimizationsApplied.push('Behavioral pattern tracking');
55
+ }
56
+ // Run ANALYZE to update query planner statistics
57
+ spinner.text = 'Updating query statistics...';
58
+ db.exec('ANALYZE');
59
+ // Vacuum if requested (requires exclusive access)
60
+ if (options.vacuum) {
61
+ spinner.text = 'Vacuuming database...';
62
+ db.exec('VACUUM');
63
+ optimizationsApplied.push('Database vacuumed');
64
+ }
65
+ // Update schema version
66
+ updateSchemaVersion(db, 1.5);
67
+ // Close database
68
+ db.close();
69
+ spinner.succeed('Database optimization complete!');
70
+ if (optimizationsApplied.length > 0) {
71
+ console.log('\n' + chalk.green('✓') + ' Optimizations applied:');
72
+ optimizationsApplied.forEach((opt)=>{
73
+ console.log(' - ' + opt);
74
+ });
75
+ } else {
76
+ console.log('\n' + chalk.yellow('ℹ') + ' Database already optimized');
77
+ }
78
+ return {
79
+ success: true,
80
+ optimizations: optimizationsApplied
81
+ };
82
+ } catch (error) {
83
+ spinner.fail('Database optimization failed');
84
+ console.error(chalk.red('Error:'), error.message);
85
+ return {
86
+ success: false,
87
+ error: error.message
88
+ };
99
89
  }
100
-
101
- return { success: true, optimizations: optimizationsApplied };
102
- } catch (error) {
103
- spinner.fail('Database optimization failed');
104
- console.error(chalk.red('Error:'), error.message);
105
- return { success: false, error: error.message };
106
- }
107
90
  }
108
-
109
91
  /**
110
92
  * Get current schema version
111
- */
112
- function getSchemaVersion(db) {
113
- try {
114
- // Check if schema_version table exists
115
- const tableExists = db
116
- .prepare(
117
- `
93
+ */ function getSchemaVersion(db) {
94
+ try {
95
+ // Check if schema_version table exists
96
+ const tableExists = db.prepare(`
118
97
  SELECT name FROM sqlite_master
119
98
  WHERE type='table' AND name='schema_version'
120
- `,
121
- )
122
- .get();
123
-
124
- if (!tableExists) {
125
- // Create schema version table
126
- db.exec(`
99
+ `).get();
100
+ if (!tableExists) {
101
+ // Create schema version table
102
+ db.exec(`
127
103
  CREATE TABLE schema_version (
128
104
  version REAL PRIMARY KEY,
129
105
  applied_at DATETIME DEFAULT CURRENT_TIMESTAMP,
130
106
  description TEXT
131
107
  )
132
108
  `);
133
-
134
- // Insert initial version
135
- db.prepare(
136
- `
109
+ // Insert initial version
110
+ db.prepare(`
137
111
  INSERT INTO schema_version (version, description)
138
112
  VALUES (1.0, 'Initial schema')
139
- `,
140
- ).run();
141
-
142
- return 1.0;
143
- }
144
-
145
- // Get latest version
146
- const result = db
147
- .prepare(
148
- `
113
+ `).run();
114
+ return 1.0;
115
+ }
116
+ // Get latest version
117
+ const result = db.prepare(`
149
118
  SELECT version FROM schema_version
150
119
  ORDER BY version DESC LIMIT 1
151
- `,
152
- )
153
- .get();
154
-
155
- return result ? result.version : 1.0;
156
- } catch (error) {
157
- // If any error, assume version 1.0
158
- return 1.0;
159
- }
120
+ `).get();
121
+ return result ? result.version : 1.0;
122
+ } catch (error) {
123
+ // If any error, assume version 1.0
124
+ return 1.0;
125
+ }
160
126
  }
161
-
162
127
  /**
163
128
  * Update schema version
164
- */
165
- function updateSchemaVersion(db, version, description = '') {
166
- db.prepare(
167
- `
129
+ */ function updateSchemaVersion(db, version, description = '') {
130
+ db.prepare(`
168
131
  INSERT OR REPLACE INTO schema_version (version, description)
169
132
  VALUES (?, ?)
170
- `,
171
- ).run(version, description || `Updated to version ${version}`);
133
+ `).run(version, description || `Updated to version ${version}`);
172
134
  }
173
-
174
135
  /**
175
136
  * Apply basic performance indexes
176
- */
177
- function applyBasicIndexes(db) {
178
- // First ensure all required columns exist
179
- ensureRequiredColumns(db);
180
-
181
- // Check which tables exist before creating indexes
182
- const tables = db
183
- .prepare(
184
- `
137
+ */ function applyBasicIndexes(db) {
138
+ // First ensure all required columns exist
139
+ ensureRequiredColumns(db);
140
+ // Check which tables exist before creating indexes
141
+ const tables = db.prepare(`
185
142
  SELECT name FROM sqlite_master
186
143
  WHERE type='table' AND name NOT LIKE 'sqlite_%'
187
- `,
188
- )
189
- .all()
190
- .map((row) => row.name);
191
-
192
- const tableSet = new Set(tables);
193
-
194
- const indexes = [];
195
-
196
- // Only create indexes for tables that exist
197
- if (tableSet.has('swarms')) {
198
- indexes.push(
199
- 'CREATE INDEX IF NOT EXISTS idx_swarms_status ON swarms(status)',
200
- 'CREATE INDEX IF NOT EXISTS idx_swarms_created ON swarms(created_at)',
201
- );
202
- }
203
-
204
- if (tableSet.has('agents')) {
205
- indexes.push(
206
- 'CREATE INDEX IF NOT EXISTS idx_agents_swarm ON agents(swarm_id)',
207
- 'CREATE INDEX IF NOT EXISTS idx_agents_type ON agents(type)',
208
- 'CREATE INDEX IF NOT EXISTS idx_agents_status ON agents(status)',
209
- );
210
- }
211
-
212
- if (tableSet.has('tasks')) {
213
- indexes.push(
214
- 'CREATE INDEX IF NOT EXISTS idx_tasks_swarm ON tasks(swarm_id)',
215
- 'CREATE INDEX IF NOT EXISTS idx_tasks_agent ON tasks(agent_id)',
216
- 'CREATE INDEX IF NOT EXISTS idx_tasks_status ON tasks(status)',
217
- 'CREATE INDEX IF NOT EXISTS idx_tasks_priority ON tasks(priority DESC)',
218
- );
219
- }
220
-
221
- if (tableSet.has('collective_memory')) {
222
- indexes.push(
223
- 'CREATE INDEX IF NOT EXISTS idx_memory_swarm ON collective_memory(swarm_id)',
224
- 'CREATE INDEX IF NOT EXISTS idx_memory_key ON collective_memory(key)',
225
- 'CREATE INDEX IF NOT EXISTS idx_memory_type ON collective_memory(type)',
226
- );
227
- }
228
-
229
- if (tableSet.has('consensus_decisions')) {
230
- indexes.push(
231
- 'CREATE INDEX IF NOT EXISTS idx_consensus_swarm ON consensus_decisions(swarm_id)',
232
- 'CREATE INDEX IF NOT EXISTS idx_consensus_created ON consensus_decisions(created_at)',
233
- );
234
- }
235
-
236
- indexes.forEach((sql) => {
237
- try {
238
- db.exec(sql);
239
- } catch (error) {
240
- console.warn(`Warning: Could not create index: ${error.message}`);
144
+ `).all().map((row)=>row.name);
145
+ const tableSet = new Set(tables);
146
+ const indexes = [];
147
+ // Only create indexes for tables that exist
148
+ if (tableSet.has('swarms')) {
149
+ indexes.push('CREATE INDEX IF NOT EXISTS idx_swarms_status ON swarms(status)', 'CREATE INDEX IF NOT EXISTS idx_swarms_created ON swarms(created_at)');
150
+ }
151
+ if (tableSet.has('agents')) {
152
+ indexes.push('CREATE INDEX IF NOT EXISTS idx_agents_swarm ON agents(swarm_id)', 'CREATE INDEX IF NOT EXISTS idx_agents_type ON agents(type)', 'CREATE INDEX IF NOT EXISTS idx_agents_status ON agents(status)');
153
+ }
154
+ if (tableSet.has('tasks')) {
155
+ indexes.push('CREATE INDEX IF NOT EXISTS idx_tasks_swarm ON tasks(swarm_id)', 'CREATE INDEX IF NOT EXISTS idx_tasks_agent ON tasks(agent_id)', 'CREATE INDEX IF NOT EXISTS idx_tasks_status ON tasks(status)', 'CREATE INDEX IF NOT EXISTS idx_tasks_priority ON tasks(priority DESC)');
241
156
  }
242
- });
157
+ if (tableSet.has('collective_memory')) {
158
+ indexes.push('CREATE INDEX IF NOT EXISTS idx_memory_swarm ON collective_memory(swarm_id)', 'CREATE INDEX IF NOT EXISTS idx_memory_key ON collective_memory(key)', 'CREATE INDEX IF NOT EXISTS idx_memory_type ON collective_memory(type)');
159
+ }
160
+ if (tableSet.has('consensus_decisions')) {
161
+ indexes.push('CREATE INDEX IF NOT EXISTS idx_consensus_swarm ON consensus_decisions(swarm_id)', 'CREATE INDEX IF NOT EXISTS idx_consensus_created ON consensus_decisions(created_at)');
162
+ }
163
+ indexes.forEach((sql)=>{
164
+ try {
165
+ db.exec(sql);
166
+ } catch (error) {
167
+ console.warn(`Warning: Could not create index: ${error.message}`);
168
+ }
169
+ });
243
170
  }
244
-
245
171
  /**
246
172
  * Ensure all required columns exist
247
- */
248
- function ensureRequiredColumns(db) {
249
- // First check which tables exist
250
- const tables = db
251
- .prepare(
252
- `
173
+ */ function ensureRequiredColumns(db) {
174
+ // First check which tables exist
175
+ const tables = db.prepare(`
253
176
  SELECT name FROM sqlite_master
254
177
  WHERE type='table' AND name NOT LIKE 'sqlite_%'
255
- `,
256
- )
257
- .all()
258
- .map((row) => row.name);
259
-
260
- const tableSet = new Set(tables);
261
-
262
- // Only check columns for tables that exist
263
- if (tableSet.has('tasks')) {
264
- // Check and add priority column to tasks table
265
- const hasPriority = db
266
- .prepare(
267
- `
178
+ `).all().map((row)=>row.name);
179
+ const tableSet = new Set(tables);
180
+ // Only check columns for tables that exist
181
+ if (tableSet.has('tasks')) {
182
+ // Check and add priority column to tasks table
183
+ const hasPriority = db.prepare(`
268
184
  SELECT COUNT(*) as count FROM pragma_table_info('tasks')
269
185
  WHERE name = 'priority'
270
- `,
271
- )
272
- .get();
273
-
274
- if (!hasPriority || hasPriority.count === 0) {
275
- try {
276
- db.exec('ALTER TABLE tasks ADD COLUMN priority INTEGER DEFAULT 5');
277
- console.log('Added missing priority column to tasks table');
278
- } catch (error) {
279
- if (
280
- !error.message.includes('duplicate column') &&
281
- !error.message.includes('no such table')
282
- ) {
283
- throw error;
186
+ `).get();
187
+ if (!hasPriority || hasPriority.count === 0) {
188
+ try {
189
+ db.exec('ALTER TABLE tasks ADD COLUMN priority INTEGER DEFAULT 5');
190
+ console.log('Added missing priority column to tasks table');
191
+ } catch (error) {
192
+ if (!error.message.includes('duplicate column') && !error.message.includes('no such table')) {
193
+ throw error;
194
+ }
195
+ }
284
196
  }
285
- }
286
- }
287
-
288
- // Check and add completed_at column to tasks table
289
- const hasCompletedAt = db
290
- .prepare(
291
- `
197
+ // Check and add completed_at column to tasks table
198
+ const hasCompletedAt = db.prepare(`
292
199
  SELECT COUNT(*) as count FROM pragma_table_info('tasks')
293
200
  WHERE name = 'completed_at'
294
- `,
295
- )
296
- .get();
297
-
298
- if (!hasCompletedAt || hasCompletedAt.count === 0) {
299
- try {
300
- db.exec('ALTER TABLE tasks ADD COLUMN completed_at DATETIME');
301
- console.log('Added missing completed_at column to tasks table');
302
- } catch (error) {
303
- if (
304
- !error.message.includes('duplicate column') &&
305
- !error.message.includes('no such table')
306
- ) {
307
- throw error;
201
+ `).get();
202
+ if (!hasCompletedAt || hasCompletedAt.count === 0) {
203
+ try {
204
+ db.exec('ALTER TABLE tasks ADD COLUMN completed_at DATETIME');
205
+ console.log('Added missing completed_at column to tasks table');
206
+ } catch (error) {
207
+ if (!error.message.includes('duplicate column') && !error.message.includes('no such table')) {
208
+ throw error;
209
+ }
210
+ }
308
211
  }
309
- }
310
- }
311
-
312
- // Check and add result column to tasks table
313
- const hasResult = db
314
- .prepare(
315
- `
212
+ // Check and add result column to tasks table
213
+ const hasResult = db.prepare(`
316
214
  SELECT COUNT(*) as count FROM pragma_table_info('tasks')
317
215
  WHERE name = 'result'
318
- `,
319
- )
320
- .get();
321
-
322
- if (!hasResult || hasResult.count === 0) {
323
- try {
324
- db.exec('ALTER TABLE tasks ADD COLUMN result TEXT');
325
- console.log('Added missing result column to tasks table');
326
- } catch (error) {
327
- if (
328
- !error.message.includes('duplicate column') &&
329
- !error.message.includes('no such table')
330
- ) {
331
- throw error;
216
+ `).get();
217
+ if (!hasResult || hasResult.count === 0) {
218
+ try {
219
+ db.exec('ALTER TABLE tasks ADD COLUMN result TEXT');
220
+ console.log('Added missing result column to tasks table');
221
+ } catch (error) {
222
+ if (!error.message.includes('duplicate column') && !error.message.includes('no such table')) {
223
+ throw error;
224
+ }
225
+ }
332
226
  }
333
- }
334
227
  }
335
- }
336
-
337
- if (tableSet.has('swarms')) {
338
- // Check and add updated_at column to swarms table
339
- const hasUpdatedAt = db
340
- .prepare(
341
- `
228
+ if (tableSet.has('swarms')) {
229
+ // Check and add updated_at column to swarms table
230
+ const hasUpdatedAt = db.prepare(`
342
231
  SELECT COUNT(*) as count FROM pragma_table_info('swarms')
343
232
  WHERE name = 'updated_at'
344
- `,
345
- )
346
- .get();
347
-
348
- if (!hasUpdatedAt || hasUpdatedAt.count === 0) {
349
- try {
350
- db.exec('ALTER TABLE swarms ADD COLUMN updated_at DATETIME');
351
- console.log('Added missing updated_at column to swarms table');
352
- } catch (error) {
353
- if (
354
- !error.message.includes('duplicate column') &&
355
- !error.message.includes('no such table')
356
- ) {
357
- throw error;
233
+ `).get();
234
+ if (!hasUpdatedAt || hasUpdatedAt.count === 0) {
235
+ try {
236
+ db.exec('ALTER TABLE swarms ADD COLUMN updated_at DATETIME');
237
+ console.log('Added missing updated_at column to swarms table');
238
+ } catch (error) {
239
+ if (!error.message.includes('duplicate column') && !error.message.includes('no such table')) {
240
+ throw error;
241
+ }
242
+ }
358
243
  }
359
- }
360
244
  }
361
- }
362
245
  }
363
-
364
246
  /**
365
247
  * Apply advanced performance indexes
366
- */
367
- function applyAdvancedIndexes(db) {
368
- // Check which tables exist
369
- const tables = db
370
- .prepare(
371
- `
248
+ */ function applyAdvancedIndexes(db) {
249
+ // Check which tables exist
250
+ const tables = db.prepare(`
372
251
  SELECT name FROM sqlite_master
373
252
  WHERE type='table' AND name NOT LIKE 'sqlite_%'
374
- `,
375
- )
376
- .all()
377
- .map((row) => row.name);
378
-
379
- const tableSet = new Set(tables);
380
- const indexes = [];
381
-
382
- // Composite indexes for common queries
383
- if (tableSet.has('tasks')) {
384
- indexes.push(
385
- 'CREATE INDEX IF NOT EXISTS idx_tasks_swarm_status ON tasks(swarm_id, status)',
386
- 'CREATE INDEX IF NOT EXISTS idx_tasks_full ON tasks(swarm_id, agent_id, status, priority)',
387
- "CREATE INDEX IF NOT EXISTS idx_tasks_pending ON tasks(swarm_id, priority) WHERE status = 'pending'",
388
- );
389
- }
390
-
391
- if (tableSet.has('agents')) {
392
- indexes.push(
393
- 'CREATE INDEX IF NOT EXISTS idx_agents_swarm_type ON agents(swarm_id, type)',
394
- 'CREATE INDEX IF NOT EXISTS idx_agents_full ON agents(swarm_id, type, status, role)',
395
- );
396
- }
397
-
398
- if (tableSet.has('collective_memory')) {
399
- indexes.push(
400
- 'CREATE INDEX IF NOT EXISTS idx_memory_swarm_key ON collective_memory(swarm_id, key)',
401
- );
402
- }
403
-
404
- if (tableSet.has('swarms')) {
405
- indexes.push(
406
- "CREATE INDEX IF NOT EXISTS idx_swarms_active ON swarms(id, name) WHERE status = 'active'",
407
- );
408
- }
409
-
410
- indexes.forEach((sql) => {
411
- try {
412
- db.exec(sql);
413
- } catch (error) {
414
- console.warn(`Warning: Could not create index: ${error.message}`);
253
+ `).all().map((row)=>row.name);
254
+ const tableSet = new Set(tables);
255
+ const indexes = [];
256
+ // Composite indexes for common queries
257
+ if (tableSet.has('tasks')) {
258
+ indexes.push('CREATE INDEX IF NOT EXISTS idx_tasks_swarm_status ON tasks(swarm_id, status)', 'CREATE INDEX IF NOT EXISTS idx_tasks_full ON tasks(swarm_id, agent_id, status, priority)', "CREATE INDEX IF NOT EXISTS idx_tasks_pending ON tasks(swarm_id, priority) WHERE status = 'pending'");
259
+ }
260
+ if (tableSet.has('agents')) {
261
+ indexes.push('CREATE INDEX IF NOT EXISTS idx_agents_swarm_type ON agents(swarm_id, type)', 'CREATE INDEX IF NOT EXISTS idx_agents_full ON agents(swarm_id, type, status, role)');
262
+ }
263
+ if (tableSet.has('collective_memory')) {
264
+ indexes.push('CREATE INDEX IF NOT EXISTS idx_memory_swarm_key ON collective_memory(swarm_id, key)');
415
265
  }
416
- });
266
+ if (tableSet.has('swarms')) {
267
+ indexes.push("CREATE INDEX IF NOT EXISTS idx_swarms_active ON swarms(id, name) WHERE status = 'active'");
268
+ }
269
+ indexes.forEach((sql)=>{
270
+ try {
271
+ db.exec(sql);
272
+ } catch (error) {
273
+ console.warn(`Warning: Could not create index: ${error.message}`);
274
+ }
275
+ });
417
276
  }
418
-
419
277
  /**
420
278
  * Add performance tracking tables
421
- */
422
- function addPerformanceTracking(db) {
423
- // Agent performance metrics
424
- db.exec(`
279
+ */ function addPerformanceTracking(db) {
280
+ // Agent performance metrics
281
+ db.exec(`
425
282
  CREATE TABLE IF NOT EXISTS agent_performance (
426
283
  agent_id TEXT PRIMARY KEY,
427
284
  tasks_completed INTEGER DEFAULT 0,
@@ -432,9 +289,8 @@ function addPerformanceTracking(db) {
432
289
  FOREIGN KEY (agent_id) REFERENCES agents(id)
433
290
  )
434
291
  `);
435
-
436
- // Swarm performance metrics
437
- db.exec(`
292
+ // Swarm performance metrics
293
+ db.exec(`
438
294
  CREATE TABLE IF NOT EXISTS swarm_performance (
439
295
  swarm_id TEXT PRIMARY KEY,
440
296
  total_tasks INTEGER DEFAULT 0,
@@ -445,9 +301,8 @@ function addPerformanceTracking(db) {
445
301
  FOREIGN KEY (swarm_id) REFERENCES swarms(id)
446
302
  )
447
303
  `);
448
-
449
- // Create triggers to update performance metrics
450
- db.exec(`
304
+ // Create triggers to update performance metrics
305
+ db.exec(`
451
306
  CREATE TRIGGER IF NOT EXISTS update_agent_performance
452
307
  AFTER UPDATE OF status ON tasks
453
308
  WHEN NEW.status = 'completed' OR NEW.status = 'failed'
@@ -463,121 +318,89 @@ function addPerformanceTracking(db) {
463
318
  END
464
319
  `);
465
320
  }
466
-
467
321
  /**
468
322
  * Add memory optimization features
469
- */
470
- function addMemoryOptimization(db) {
471
- // Check if collective_memory table exists
472
- const tables = db
473
- .prepare(
474
- `
323
+ */ function addMemoryOptimization(db) {
324
+ // Check if collective_memory table exists
325
+ const tables = db.prepare(`
475
326
  SELECT name FROM sqlite_master
476
327
  WHERE type='table' AND name = 'collective_memory'
477
- `,
478
- )
479
- .all();
480
-
481
- if (tables.length === 0) {
482
- console.log('collective_memory table does not exist, skipping memory optimization');
483
- return;
484
- }
485
-
486
- // Check and add access_count column
487
- const hasAccessCount = db
488
- .prepare(
489
- `
328
+ `).all();
329
+ if (tables.length === 0) {
330
+ console.log('collective_memory table does not exist, skipping memory optimization');
331
+ return;
332
+ }
333
+ // Check and add access_count column
334
+ const hasAccessCount = db.prepare(`
490
335
  SELECT COUNT(*) as count FROM pragma_table_info('collective_memory')
491
336
  WHERE name = 'access_count'
492
- `,
493
- )
494
- .get();
495
-
496
- if (!hasAccessCount || hasAccessCount.count === 0) {
497
- try {
498
- db.exec(`
337
+ `).get();
338
+ if (!hasAccessCount || hasAccessCount.count === 0) {
339
+ try {
340
+ db.exec(`
499
341
  ALTER TABLE collective_memory
500
342
  ADD COLUMN access_count INTEGER DEFAULT 0
501
343
  `);
502
- console.log('Added access_count column to collective_memory table');
503
- } catch (error) {
504
- if (!error.message.includes('duplicate column') && !error.message.includes('no such table')) {
505
- throw error;
506
- }
344
+ console.log('Added access_count column to collective_memory table');
345
+ } catch (error) {
346
+ if (!error.message.includes('duplicate column') && !error.message.includes('no such table')) {
347
+ throw error;
348
+ }
349
+ }
507
350
  }
508
- }
509
-
510
- // Check and add accessed_at column (not last_accessed)
511
- const hasAccessedAt = db
512
- .prepare(
513
- `
351
+ // Check and add accessed_at column (not last_accessed)
352
+ const hasAccessedAt = db.prepare(`
514
353
  SELECT COUNT(*) as count FROM pragma_table_info('collective_memory')
515
354
  WHERE name = 'accessed_at'
516
- `,
517
- )
518
- .get();
519
-
520
- if (!hasAccessedAt || hasAccessedAt.count === 0) {
521
- try {
522
- db.exec(`
355
+ `).get();
356
+ if (!hasAccessedAt || hasAccessedAt.count === 0) {
357
+ try {
358
+ db.exec(`
523
359
  ALTER TABLE collective_memory
524
360
  ADD COLUMN accessed_at DATETIME
525
361
  `);
526
- console.log('Added accessed_at column to collective_memory table');
527
- } catch (error) {
528
- if (!error.message.includes('duplicate column') && !error.message.includes('no such table')) {
529
- throw error;
530
- }
362
+ console.log('Added accessed_at column to collective_memory table');
363
+ } catch (error) {
364
+ if (!error.message.includes('duplicate column') && !error.message.includes('no such table')) {
365
+ throw error;
366
+ }
367
+ }
531
368
  }
532
- }
533
-
534
- // Add compressed and size columns if missing
535
- const hasCompressed = db
536
- .prepare(
537
- `
369
+ // Add compressed and size columns if missing
370
+ const hasCompressed = db.prepare(`
538
371
  SELECT COUNT(*) as count FROM pragma_table_info('collective_memory')
539
372
  WHERE name = 'compressed'
540
- `,
541
- )
542
- .get();
543
-
544
- if (!hasCompressed || hasCompressed.count === 0) {
545
- try {
546
- db.exec(`
373
+ `).get();
374
+ if (!hasCompressed || hasCompressed.count === 0) {
375
+ try {
376
+ db.exec(`
547
377
  ALTER TABLE collective_memory
548
378
  ADD COLUMN compressed INTEGER DEFAULT 0
549
379
  `);
550
- } catch (error) {
551
- if (!error.message.includes('duplicate column') && !error.message.includes('no such table')) {
552
- throw error;
553
- }
380
+ } catch (error) {
381
+ if (!error.message.includes('duplicate column') && !error.message.includes('no such table')) {
382
+ throw error;
383
+ }
384
+ }
554
385
  }
555
- }
556
-
557
- const hasSize = db
558
- .prepare(
559
- `
386
+ const hasSize = db.prepare(`
560
387
  SELECT COUNT(*) as count FROM pragma_table_info('collective_memory')
561
388
  WHERE name = 'size'
562
- `,
563
- )
564
- .get();
565
-
566
- if (!hasSize || hasSize.count === 0) {
567
- try {
568
- db.exec(`
389
+ `).get();
390
+ if (!hasSize || hasSize.count === 0) {
391
+ try {
392
+ db.exec(`
569
393
  ALTER TABLE collective_memory
570
394
  ADD COLUMN size INTEGER DEFAULT 0
571
395
  `);
572
- } catch (error) {
573
- if (!error.message.includes('duplicate column') && !error.message.includes('no such table')) {
574
- throw error;
575
- }
396
+ } catch (error) {
397
+ if (!error.message.includes('duplicate column') && !error.message.includes('no such table')) {
398
+ throw error;
399
+ }
400
+ }
576
401
  }
577
- }
578
-
579
- // Create memory usage summary view
580
- db.exec(`
402
+ // Create memory usage summary view
403
+ db.exec(`
581
404
  CREATE VIEW IF NOT EXISTS memory_usage_summary AS
582
405
  SELECT
583
406
  swarm_id,
@@ -588,9 +411,8 @@ function addMemoryOptimization(db) {
588
411
  FROM collective_memory
589
412
  GROUP BY swarm_id
590
413
  `);
591
-
592
- // Add memory cleanup tracking
593
- db.exec(`
414
+ // Add memory cleanup tracking
415
+ db.exec(`
594
416
  CREATE TABLE IF NOT EXISTS memory_cleanup_log (
595
417
  id INTEGER PRIMARY KEY AUTOINCREMENT,
596
418
  swarm_id TEXT,
@@ -601,13 +423,11 @@ function addMemoryOptimization(db) {
601
423
  )
602
424
  `);
603
425
  }
604
-
605
426
  /**
606
427
  * Add behavioral tracking features
607
- */
608
- function addBehavioralTracking(db) {
609
- // Agent interaction patterns
610
- db.exec(`
428
+ */ function addBehavioralTracking(db) {
429
+ // Agent interaction patterns
430
+ db.exec(`
611
431
  CREATE TABLE IF NOT EXISTS agent_interactions (
612
432
  id INTEGER PRIMARY KEY AUTOINCREMENT,
613
433
  from_agent_id TEXT,
@@ -620,9 +440,8 @@ function addBehavioralTracking(db) {
620
440
  FOREIGN KEY (swarm_id) REFERENCES swarms(id)
621
441
  )
622
442
  `);
623
-
624
- // Behavioral patterns
625
- db.exec(`
443
+ // Behavioral patterns
444
+ db.exec(`
626
445
  CREATE TABLE IF NOT EXISTS behavioral_patterns (
627
446
  id INTEGER PRIMARY KEY AUTOINCREMENT,
628
447
  swarm_id TEXT,
@@ -635,238 +454,170 @@ function addBehavioralTracking(db) {
635
454
  FOREIGN KEY (swarm_id) REFERENCES swarms(id)
636
455
  )
637
456
  `);
638
-
639
- // Create indexes for behavioral analysis
640
- db.exec(`
457
+ // Create indexes for behavioral analysis
458
+ db.exec(`
641
459
  CREATE INDEX IF NOT EXISTS idx_interactions_swarm ON agent_interactions(swarm_id);
642
460
  CREATE INDEX IF NOT EXISTS idx_interactions_agents ON agent_interactions(from_agent_id, to_agent_id);
643
461
  CREATE INDEX IF NOT EXISTS idx_patterns_swarm_type ON behavioral_patterns(swarm_id, pattern_type);
644
462
  `);
645
463
  }
646
-
647
464
  /**
648
465
  * Database maintenance utilities
649
- */
650
- export async function performMaintenance(dbPath, options = {}) {
651
- const spinner = ora('Performing database maintenance...').start();
652
-
653
- try {
654
- const db = new Database(dbPath);
655
-
656
- // Clean up old memory entries
657
- if (options.cleanMemory) {
658
- // Check if collective_memory table exists
659
- const hasMemoryTable = db
660
- .prepare(
661
- `
466
+ */ export async function performMaintenance(dbPath, options = {}) {
467
+ const spinner = ora('Performing database maintenance...').start();
468
+ try {
469
+ const db = new Database(dbPath);
470
+ // Clean up old memory entries
471
+ if (options.cleanMemory) {
472
+ // Check if collective_memory table exists
473
+ const hasMemoryTable = db.prepare(`
662
474
  SELECT name FROM sqlite_master
663
475
  WHERE type='table' AND name='collective_memory'
664
- `,
665
- )
666
- .get();
667
-
668
- if (hasMemoryTable) {
669
- spinner.text = 'Cleaning old memory entries...';
670
- const cutoffDate = new Date();
671
- cutoffDate.setDate(cutoffDate.getDate() - (options.memoryRetentionDays || 30));
672
-
673
- try {
674
- const result = db
675
- .prepare(
676
- `
476
+ `).get();
477
+ if (hasMemoryTable) {
478
+ spinner.text = 'Cleaning old memory entries...';
479
+ const cutoffDate = new Date();
480
+ cutoffDate.setDate(cutoffDate.getDate() - (options.memoryRetentionDays || 30));
481
+ try {
482
+ const result = db.prepare(`
677
483
  DELETE FROM collective_memory
678
484
  WHERE accessed_at < ? AND access_count < 5
679
- `,
680
- )
681
- .run(cutoffDate.toISOString());
682
-
683
- console.log(chalk.green(`✓ Removed ${result.changes} old memory entries`));
684
- } catch (error) {
685
- console.warn(chalk.yellow(`⚠ Could not clean memory entries: ${error.message}`));
485
+ `).run(cutoffDate.toISOString());
486
+ console.log(chalk.green(`✓ Removed ${result.changes} old memory entries`));
487
+ } catch (error) {
488
+ console.warn(chalk.yellow(`⚠ Could not clean memory entries: ${error.message}`));
489
+ }
490
+ } else {
491
+ console.log(chalk.yellow('⚠ collective_memory table not found, skipping memory cleanup'));
492
+ }
686
493
  }
687
- } else {
688
- console.log(chalk.yellow('⚠ collective_memory table not found, skipping memory cleanup'));
689
- }
690
- }
691
-
692
- // Archive completed tasks
693
- if (options.archiveTasks) {
694
- spinner.text = 'Archiving completed tasks...';
695
-
696
- // Create archive table if not exists
697
- db.exec(`
494
+ // Archive completed tasks
495
+ if (options.archiveTasks) {
496
+ spinner.text = 'Archiving completed tasks...';
497
+ // Create archive table if not exists
498
+ db.exec(`
698
499
  CREATE TABLE IF NOT EXISTS tasks_archive AS
699
500
  SELECT * FROM tasks WHERE 1=0
700
501
  `);
701
-
702
- // Check if completed_at column exists
703
- const hasCompletedAt = db
704
- .prepare(
705
- `
502
+ // Check if completed_at column exists
503
+ const hasCompletedAt = db.prepare(`
706
504
  SELECT COUNT(*) as count FROM pragma_table_info('tasks')
707
505
  WHERE name = 'completed_at'
708
- `,
709
- )
710
- .get();
711
-
712
- let archived = { changes: 0 };
713
-
714
- if (hasCompletedAt && hasCompletedAt.count > 0) {
715
- // Move old completed tasks using completed_at
716
- const archiveCutoff = new Date();
717
- archiveCutoff.setDate(archiveCutoff.getDate() - (options.taskRetentionDays || 7));
718
-
719
- db.exec(`
506
+ `).get();
507
+ let archived = {
508
+ changes: 0
509
+ };
510
+ if (hasCompletedAt && hasCompletedAt.count > 0) {
511
+ // Move old completed tasks using completed_at
512
+ const archiveCutoff = new Date();
513
+ archiveCutoff.setDate(archiveCutoff.getDate() - (options.taskRetentionDays || 7));
514
+ db.exec(`
720
515
  INSERT INTO tasks_archive
721
516
  SELECT * FROM tasks
722
517
  WHERE status = 'completed' AND completed_at < '${archiveCutoff.toISOString()}'
723
518
  `);
724
-
725
- archived = db
726
- .prepare(
727
- `
519
+ archived = db.prepare(`
728
520
  DELETE FROM tasks
729
521
  WHERE status = 'completed' AND completed_at < ?
730
- `,
731
- )
732
- .run(archiveCutoff.toISOString());
733
- } else {
734
- // Use created_at as fallback
735
- const archiveCutoff = new Date();
736
- archiveCutoff.setDate(archiveCutoff.getDate() - (options.taskRetentionDays || 7));
737
-
738
- db.exec(`
522
+ `).run(archiveCutoff.toISOString());
523
+ } else {
524
+ // Use created_at as fallback
525
+ const archiveCutoff = new Date();
526
+ archiveCutoff.setDate(archiveCutoff.getDate() - (options.taskRetentionDays || 7));
527
+ db.exec(`
739
528
  INSERT INTO tasks_archive
740
529
  SELECT * FROM tasks
741
530
  WHERE status = 'completed' AND created_at < '${archiveCutoff.toISOString()}'
742
531
  `);
743
-
744
- archived = db
745
- .prepare(
746
- `
532
+ archived = db.prepare(`
747
533
  DELETE FROM tasks
748
534
  WHERE status = 'completed' AND created_at < ?
749
- `,
750
- )
751
- .run(archiveCutoff.toISOString());
752
- }
753
-
754
- console.log(chalk.green(`✓ Archived ${archived.changes} completed tasks`));
755
- }
756
-
757
- // Update statistics
758
- spinner.text = 'Updating database statistics...';
759
- db.exec('ANALYZE');
760
-
761
- // Check integrity
762
- if (options.checkIntegrity) {
763
- spinner.text = 'Checking database integrity...';
764
- const integrityCheck = db.prepare('PRAGMA integrity_check').get();
765
- if (integrityCheck.integrity_check === 'ok') {
766
- console.log(chalk.green('✓ Database integrity check passed'));
767
- } else {
768
- console.log(chalk.yellow('⚠ Database integrity issues detected'));
769
- }
535
+ `).run(archiveCutoff.toISOString());
536
+ }
537
+ console.log(chalk.green(`✓ Archived ${archived.changes} completed tasks`));
538
+ }
539
+ // Update statistics
540
+ spinner.text = 'Updating database statistics...';
541
+ db.exec('ANALYZE');
542
+ // Check integrity
543
+ if (options.checkIntegrity) {
544
+ spinner.text = 'Checking database integrity...';
545
+ const integrityCheck = db.prepare('PRAGMA integrity_check').get();
546
+ if (integrityCheck.integrity_check === 'ok') {
547
+ console.log(chalk.green('✓ Database integrity check passed'));
548
+ } else {
549
+ console.log(chalk.yellow('⚠ Database integrity issues detected'));
550
+ }
551
+ }
552
+ db.close();
553
+ spinner.succeed('Database maintenance complete!');
554
+ } catch (error) {
555
+ spinner.fail('Database maintenance failed');
556
+ console.error(chalk.red('Error:'), error.message);
770
557
  }
771
-
772
- db.close();
773
- spinner.succeed('Database maintenance complete!');
774
- } catch (error) {
775
- spinner.fail('Database maintenance failed');
776
- console.error(chalk.red('Error:'), error.message);
777
- }
778
558
  }
779
-
780
559
  /**
781
560
  * Generate optimization report
782
- */
783
- export async function generateOptimizationReport(dbPath) {
784
- try {
785
- const db = new Database(dbPath, { readonly: true });
786
-
787
- const report = {
788
- schemaVersion: getSchemaVersion(db),
789
- tables: {},
790
- indexes: [],
791
- performance: {},
792
- };
793
-
794
- // Get table statistics
795
- const tables = db
796
- .prepare(
797
- `
561
+ */ export async function generateOptimizationReport(dbPath) {
562
+ try {
563
+ const db = new Database(dbPath, {
564
+ readonly: true
565
+ });
566
+ const report = {
567
+ schemaVersion: getSchemaVersion(db),
568
+ tables: {},
569
+ indexes: [],
570
+ performance: {}
571
+ };
572
+ // Get table statistics
573
+ const tables = db.prepare(`
798
574
  SELECT name FROM sqlite_master WHERE type='table'
799
- `,
800
- )
801
- .all();
802
-
803
- for (const table of tables) {
804
- const count = db.prepare(`SELECT COUNT(*) as count FROM ${table.name}`).get();
805
- const size = db
806
- .prepare(
807
- `
575
+ `).all();
576
+ for (const table of tables){
577
+ const count = db.prepare(`SELECT COUNT(*) as count FROM ${table.name}`).get();
578
+ const size = db.prepare(`
808
579
  SELECT SUM(pgsize) as size FROM dbstat WHERE name=?
809
- `,
810
- )
811
- .get(table.name);
812
-
813
- report.tables[table.name] = {
814
- rowCount: count.count,
815
- sizeBytes: size?.size || 0,
816
- };
817
- }
818
-
819
- // Get index information
820
- report.indexes = db
821
- .prepare(
822
- `
580
+ `).get(table.name);
581
+ report.tables[table.name] = {
582
+ rowCount: count.count,
583
+ sizeBytes: size?.size || 0
584
+ };
585
+ }
586
+ // Get index information
587
+ report.indexes = db.prepare(`
823
588
  SELECT name, tbl_name FROM sqlite_master WHERE type='index'
824
- `,
825
- )
826
- .all();
827
-
828
- // Get performance metrics (check if completed_at column exists)
829
- let avgTaskTime = { avg_minutes: 0 };
830
- try {
831
- // First check if completed_at column exists
832
- const hasCompletedAt = db
833
- .prepare(
834
- `
589
+ `).all();
590
+ // Get performance metrics (check if completed_at column exists)
591
+ let avgTaskTime = {
592
+ avg_minutes: 0
593
+ };
594
+ try {
595
+ // First check if completed_at column exists
596
+ const hasCompletedAt = db.prepare(`
835
597
  SELECT COUNT(*) as count FROM pragma_table_info('tasks')
836
598
  WHERE name = 'completed_at'
837
- `,
838
- )
839
- .get();
840
-
841
- if (hasCompletedAt && hasCompletedAt.count > 0) {
842
- avgTaskTime = db
843
- .prepare(
844
- `
599
+ `).get();
600
+ if (hasCompletedAt && hasCompletedAt.count > 0) {
601
+ avgTaskTime = db.prepare(`
845
602
  SELECT AVG(julianday(completed_at) - julianday(created_at)) * 24 * 60 as avg_minutes
846
603
  FROM tasks WHERE completed_at IS NOT NULL
847
- `,
848
- )
849
- .get();
850
- }
604
+ `).get();
605
+ }
606
+ } catch (error) {
607
+ // If error, just use default value
608
+ console.warn('Could not calculate average task time:', error.message);
609
+ }
610
+ report.performance.avgTaskCompletionMinutes = avgTaskTime?.avg_minutes || 0;
611
+ db.close();
612
+ return report;
851
613
  } catch (error) {
852
- // If error, just use default value
853
- console.warn('Could not calculate average task time:', error.message);
614
+ console.error('Error generating report:', error);
615
+ return null;
854
616
  }
855
-
856
- report.performance.avgTaskCompletionMinutes = avgTaskTime?.avg_minutes || 0;
857
-
858
- db.close();
859
-
860
- return report;
861
- } catch (error) {
862
- console.error('Error generating report:', error);
863
- return null;
864
- }
865
617
  }
866
-
867
618
  // Export for use in CLI
868
619
  export default {
869
- optimizeHiveMindDatabase,
870
- performMaintenance,
871
- generateOptimizationReport,
620
+ optimizeHiveMindDatabase,
621
+ performMaintenance,
622
+ generateOptimizationReport
872
623
  };