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.
- package/dist/src/cli/commands/guidance.js +487 -668
- package/dist/src/cli/commands/index-validate.js +18 -29
- package/dist/src/cli/commands/mcp-troubleshoot.js +230 -282
- package/dist/src/cli/commands/neural-goal-init.js +92 -125
- package/dist/src/cli/commands/swarm-exec.js +317 -393
- package/dist/src/cli/commands/swarm.js +1 -1
- package/dist/src/cli/commands/validate-framework.js +983 -1100
- package/dist/src/cli/commands/validate.js +144 -223
- package/dist/src/cli/simple-commands/__tests__/agent.test.js +265 -277
- package/dist/src/cli/simple-commands/__tests__/memory.test.js +6 -7
- package/dist/src/cli/simple-commands/__tests__/swarm.test.js +373 -356
- package/dist/src/cli/simple-commands/__tests__/task.test.js +6 -7
- package/dist/src/cli/simple-commands/agent.js +157 -193
- package/dist/src/cli/simple-commands/analysis.js +336 -446
- package/dist/src/cli/simple-commands/automation-executor.js +1095 -1339
- package/dist/src/cli/simple-commands/automation.js +481 -469
- package/dist/src/cli/simple-commands/batch-manager.js +261 -313
- package/dist/src/cli/simple-commands/claude-telemetry.js +241 -267
- package/dist/src/cli/simple-commands/claude-track.js +68 -90
- package/dist/src/cli/simple-commands/concurrent-display.js +266 -320
- package/dist/src/cli/simple-commands/config.js +245 -290
- package/dist/src/cli/simple-commands/coordination.js +182 -234
- package/dist/src/cli/simple-commands/enhanced-ui-views.js +812 -615
- package/dist/src/cli/simple-commands/enhanced-webui-complete.js +922 -981
- package/dist/src/cli/simple-commands/fix-hook-variables.js +274 -294
- package/dist/src/cli/simple-commands/github/gh-coordinator.js +378 -457
- package/dist/src/cli/simple-commands/github/github-api.js +535 -574
- package/dist/src/cli/simple-commands/github/init.js +276 -303
- package/dist/src/cli/simple-commands/github.js +222 -247
- package/dist/src/cli/simple-commands/goal.js +51 -63
- package/dist/src/cli/simple-commands/hive-mind/auto-save-middleware.js +208 -278
- package/dist/src/cli/simple-commands/hive-mind/communication.js +601 -696
- package/dist/src/cli/simple-commands/hive-mind/core.js +907 -979
- package/dist/src/cli/simple-commands/hive-mind/db-optimizer.js +406 -655
- package/dist/src/cli/simple-commands/hive-mind/mcp-wrapper.js +1125 -1245
- package/dist/src/cli/simple-commands/hive-mind/memory.js +854 -1090
- package/dist/src/cli/simple-commands/hive-mind/performance-optimizer.js +459 -574
- package/dist/src/cli/simple-commands/hive-mind/performance-test.js +263 -347
- package/dist/src/cli/simple-commands/hive-mind/queen.js +727 -768
- package/dist/src/cli/simple-commands/hive-mind/session-manager.js +745 -1049
- package/dist/src/cli/simple-commands/hive-mind-optimize.js +227 -283
- package/dist/src/cli/simple-commands/hive-mind-wizard.js +174 -217
- package/dist/src/cli/simple-commands/hive-mind.js +1842 -2283
- package/dist/src/cli/simple-commands/hive.js +90 -79
- package/dist/src/cli/simple-commands/hook-safety.js +431 -521
- package/dist/src/cli/simple-commands/hooks/session-start-soul.js +203 -254
- package/dist/src/cli/simple-commands/hooks.js +1064 -1204
- package/dist/src/cli/simple-commands/init/agent-copier.js +294 -319
- package/dist/src/cli/simple-commands/init/batch-init.js +496 -562
- package/dist/src/cli/simple-commands/init/claude-commands/claude-flow-commands.js +13 -19
- package/dist/src/cli/simple-commands/init/claude-commands/optimized-claude-flow-commands.js +13 -19
- package/dist/src/cli/simple-commands/init/claude-commands/optimized-slash-commands.js +61 -88
- package/dist/src/cli/simple-commands/init/claude-commands/optimized-sparc-commands.js +125 -150
- package/dist/src/cli/simple-commands/init/claude-commands/slash-commands.js +42 -49
- package/dist/src/cli/simple-commands/init/claude-commands/sparc-commands.js +43 -61
- package/dist/src/cli/simple-commands/init/copy-revised-templates.js +141 -147
- package/dist/src/cli/simple-commands/init/executable-wrapper.js +31 -44
- package/dist/src/cli/simple-commands/init/gitignore-updater.js +64 -90
- package/dist/src/cli/simple-commands/init/help.js +104 -107
- package/dist/src/cli/simple-commands/init/hive-mind-init.js +509 -528
- package/dist/src/cli/simple-commands/init/index.js +1510 -1759
- package/dist/src/cli/simple-commands/init/performance-monitor.js +234 -317
- package/dist/src/cli/simple-commands/init/rollback/backup-manager.js +441 -504
- package/dist/src/cli/simple-commands/init/rollback/index.js +289 -364
- package/dist/src/cli/simple-commands/init/rollback/recovery-manager.js +652 -728
- package/dist/src/cli/simple-commands/init/rollback/rollback-executor.js +416 -481
- package/dist/src/cli/simple-commands/init/rollback/state-tracker.js +369 -448
- package/dist/src/cli/simple-commands/init/sparc/roo-readme.js +1 -2
- package/dist/src/cli/simple-commands/init/sparc/roomodes-config.js +122 -99
- package/dist/src/cli/simple-commands/init/sparc/workflows.js +32 -37
- package/dist/src/cli/simple-commands/init/sparc-structure.js +55 -62
- package/dist/src/cli/simple-commands/init/template-copier.js +421 -533
- package/dist/src/cli/simple-commands/init/templates/coordination-md.js +3 -6
- package/dist/src/cli/simple-commands/init/templates/enhanced-templates.js +344 -318
- package/dist/src/cli/simple-commands/init/templates/github-safe-enhanced.js +173 -218
- package/dist/src/cli/simple-commands/init/templates/github-safe.js +65 -75
- package/dist/src/cli/simple-commands/init/templates/memory-bank-md.js +3 -6
- package/dist/src/cli/simple-commands/init/templates/readme-files.js +2 -4
- package/dist/src/cli/simple-commands/init/templates/safe-hook-patterns.js +187 -230
- package/dist/src/cli/simple-commands/init/templates/sparc-modes.js +53 -80
- package/dist/src/cli/simple-commands/init/templates/verification-claude-md.js +101 -85
- package/dist/src/cli/simple-commands/init/validation/config-validator.js +283 -330
- package/dist/src/cli/simple-commands/init/validation/health-checker.js +495 -561
- package/dist/src/cli/simple-commands/init/validation/index.js +302 -358
- package/dist/src/cli/simple-commands/init/validation/mode-validator.js +308 -359
- package/dist/src/cli/simple-commands/init/validation/post-init-validator.js +389 -366
- package/dist/src/cli/simple-commands/init/validation/pre-init-validator.js +270 -268
- package/dist/src/cli/simple-commands/init/validation/test-runner.js +427 -447
- package/dist/src/cli/simple-commands/init.js +1 -2
- package/dist/src/cli/simple-commands/mcp-health.js +131 -158
- package/dist/src/cli/simple-commands/mcp-integration-layer.js +533 -634
- package/dist/src/cli/simple-commands/mcp.js +345 -400
- package/dist/src/cli/simple-commands/memory-consolidation.js +426 -537
- package/dist/src/cli/simple-commands/memory.js +247 -311
- package/dist/src/cli/simple-commands/migrate-hooks.js +39 -46
- package/dist/src/cli/simple-commands/monitor.js +294 -363
- package/dist/src/cli/simple-commands/neural.js +51 -65
- package/dist/src/cli/simple-commands/pair-autofix-only.js +538 -662
- package/dist/src/cli/simple-commands/pair-basic.js +528 -656
- package/dist/src/cli/simple-commands/pair-old.js +430 -543
- package/dist/src/cli/simple-commands/pair-working.js +615 -751
- package/dist/src/cli/simple-commands/pair.js +615 -751
- package/dist/src/cli/simple-commands/performance-hooks.js +83 -111
- package/dist/src/cli/simple-commands/performance-metrics.js +348 -433
- package/dist/src/cli/simple-commands/process-ui-enhanced.js +708 -787
- package/dist/src/cli/simple-commands/process-ui.js +230 -254
- package/dist/src/cli/simple-commands/realtime-update-system.js +525 -611
- package/dist/src/cli/simple-commands/sparc/architecture.js +1704 -1530
- package/dist/src/cli/simple-commands/sparc/commands.js +438 -516
- package/dist/src/cli/simple-commands/sparc/completion.js +1224 -1481
- package/dist/src/cli/simple-commands/sparc/coordinator.js +913 -978
- package/dist/src/cli/simple-commands/sparc/index.js +241 -298
- package/dist/src/cli/simple-commands/sparc/phase-base.js +314 -390
- package/dist/src/cli/simple-commands/sparc/pseudocode.js +965 -869
- package/dist/src/cli/simple-commands/sparc/refinement.js +980 -1273
- package/dist/src/cli/simple-commands/sparc/specification.js +559 -645
- package/dist/src/cli/simple-commands/sparc-modes/architect.js +1 -1
- package/dist/src/cli/simple-commands/sparc-modes/ask.js +1 -1
- package/dist/src/cli/simple-commands/sparc-modes/code.js +1 -1
- package/dist/src/cli/simple-commands/sparc-modes/debug.js +1 -1
- package/dist/src/cli/simple-commands/sparc-modes/devops.js +1 -1
- package/dist/src/cli/simple-commands/sparc-modes/docs-writer.js +1 -1
- package/dist/src/cli/simple-commands/sparc-modes/generic.js +1 -1
- package/dist/src/cli/simple-commands/sparc-modes/index.js +47 -55
- package/dist/src/cli/simple-commands/sparc-modes/integration.js +1 -1
- package/dist/src/cli/simple-commands/sparc-modes/mcp.js +1 -1
- package/dist/src/cli/simple-commands/sparc-modes/monitoring.js +1 -1
- package/dist/src/cli/simple-commands/sparc-modes/optimization.js +1 -1
- package/dist/src/cli/simple-commands/sparc-modes/security-review.js +1 -1
- package/dist/src/cli/simple-commands/sparc-modes/sparc-orchestrator.js +1 -1
- package/dist/src/cli/simple-commands/sparc-modes/spec-pseudocode.js +1 -1
- package/dist/src/cli/simple-commands/sparc-modes/supabase-admin.js +1 -1
- package/dist/src/cli/simple-commands/sparc-modes/swarm.js +101 -87
- package/dist/src/cli/simple-commands/sparc-modes/tdd.js +1 -1
- package/dist/src/cli/simple-commands/sparc-modes/tutorial.js +1 -1
- package/dist/src/cli/simple-commands/sparc.js +465 -493
- package/dist/src/cli/simple-commands/start-ui.js +108 -132
- package/dist/src/cli/simple-commands/start-wrapper.js +240 -268
- package/dist/src/cli/simple-commands/start.js +1 -1
- package/dist/src/cli/simple-commands/status.js +254 -275
- package/dist/src/cli/simple-commands/stream-chain-clean.js +128 -171
- package/dist/src/cli/simple-commands/stream-chain-fixed.js +61 -82
- package/dist/src/cli/simple-commands/stream-chain-real.js +267 -331
- package/dist/src/cli/simple-commands/stream-chain-working.js +211 -263
- package/dist/src/cli/simple-commands/stream-chain.js +260 -318
- package/dist/src/cli/simple-commands/stream-processor.js +290 -315
- package/dist/src/cli/simple-commands/swarm-executor.js +189 -222
- package/dist/src/cli/simple-commands/swarm-metrics-integration.js +208 -300
- package/dist/src/cli/simple-commands/swarm-ui.js +623 -703
- package/dist/src/cli/simple-commands/swarm-webui-integration.js +258 -286
- package/dist/src/cli/simple-commands/swarm.js +887 -1082
- package/dist/src/cli/simple-commands/task.js +161 -206
- package/dist/src/cli/simple-commands/timestamp-fix.js +59 -89
- package/dist/src/cli/simple-commands/token-tracker.js +258 -316
- package/dist/src/cli/simple-commands/tool-execution-framework.js +433 -519
- package/dist/src/cli/simple-commands/train-and-stream.js +275 -331
- package/dist/src/cli/simple-commands/training-pipeline.js +619 -725
- package/dist/src/cli/simple-commands/training.js +170 -227
- package/dist/src/cli/simple-commands/verification-hooks.js +261 -284
- package/dist/src/cli/simple-commands/verification-integration.js +389 -417
- package/dist/src/cli/simple-commands/verification-training-integration.js +486 -606
- package/dist/src/cli/simple-commands/verification.js +493 -513
- package/dist/src/cli/simple-commands/web-server.js +766 -836
- package/dist/src/cli/simple-commands/webui-validator.js +106 -124
- package/dist/src/coordination/event-bus/demo-wasm-integration.js +212 -251
- package/dist/src/coordination/event-bus/qe-event-bus.js +608 -748
- package/dist/src/coordination/event-bus/qe-event-bus.test.js +379 -454
- package/dist/src/coordination/iteration-tracker.js +363 -454
- package/dist/src/enterprise/analytics-manager.js +1135 -0
- package/dist/src/enterprise/audit-manager.js +1115 -0
- package/dist/src/enterprise/cloud-manager.js +891 -0
- package/dist/src/enterprise/deployment-manager.js +966 -0
- package/dist/src/enterprise/index.js +6 -0
- package/dist/src/enterprise/project-manager.js +584 -0
- package/dist/src/enterprise/security-manager.js +991 -0
- package/dist/src/index.js +1 -1
- package/dist/src/mcp/DEPRECATED.js +46 -60
- package/dist/src/mcp/fixes/mcp-error-fixes.js +115 -134
- package/dist/src/mcp/implementations/agent-tracker.js +114 -128
- package/dist/src/mcp/implementations/daa-tools.js +292 -350
- package/dist/src/mcp/implementations/workflow-tools.js +329 -361
- package/dist/src/mcp/mcp-config-manager.js +1183 -1331
- package/dist/src/mcp/mcp-server-novice-simplified.js +11 -17
- package/dist/src/mcp/mcp-server-novice.js +11 -17
- package/dist/src/mcp/mcp-server-sdk.js +11 -17
- package/dist/src/mcp/mcp-server.js +1620 -1484
- package/dist/src/mcp/ruv-swarm-wrapper.js +209 -239
- package/dist/src/memory/advanced-serializer.js +609 -589
- package/dist/src/memory/enhanced-examples.js +220 -305
- package/dist/src/memory/enhanced-memory.js +295 -336
- package/dist/src/memory/enhanced-session-serializer.js +408 -492
- package/dist/src/memory/fallback-memory-system.js +900 -1021
- package/dist/src/memory/fallback-store.js +93 -131
- package/dist/src/memory/high-performance-serialization.js +592 -730
- package/dist/src/memory/in-memory-store.js +161 -213
- package/dist/src/memory/index.js +123 -157
- package/dist/src/memory/lock-free-structures.js +578 -764
- package/dist/src/memory/memory-mapped-persistence.js +585 -766
- package/dist/src/memory/memory-pressure-manager.js +569 -707
- package/dist/src/memory/migration.js +358 -445
- package/dist/src/memory/shared-memory.js +641 -768
- package/dist/src/memory/sqlite-store.js +245 -325
- package/dist/src/memory/sqlite-wrapper.js +122 -151
- package/dist/src/memory/swarm-memory.js +470 -603
- package/dist/src/memory/test-example.js +126 -134
- package/dist/src/memory/ultra-fast-memory-store.js +622 -821
- package/dist/src/memory/unified-memory-manager.js +356 -437
- package/dist/src/migration/index.js +92 -0
- package/dist/src/migration/logger.js +121 -0
- package/dist/src/migration/migration-analyzer.js +268 -0
- package/dist/src/migration/migration-runner.js +522 -0
- package/dist/src/migration/migration-validator.js +285 -0
- package/dist/src/migration/progress-reporter.js +150 -0
- package/dist/src/migration/rollback-manager.js +321 -0
- package/dist/src/migration/tests/migration-system.test.js +7 -0
- package/dist/src/migration/types.js +3 -0
- package/dist/src/swarm/CodeRefactoringSwarm.js +777 -952
- package/dist/src/swarm/__tests__/integration.test.js +227 -0
- package/dist/src/swarm/__tests__/prompt-copier.test.js +344 -0
- package/dist/src/swarm/advanced-orchestrator.js +1095 -0
- package/dist/src/swarm/claude-code-interface.js +961 -0
- package/dist/src/swarm/claude-flow-executor.js +229 -0
- package/dist/src/swarm/consensus-coordinator.js +475 -0
- package/dist/src/swarm/coordinator.js +2993 -0
- package/dist/src/swarm/direct-executor.js +1180 -0
- package/dist/src/swarm/error-recovery/advanced-error-detection.js +691 -0
- package/dist/src/swarm/error-recovery/automated-recovery-workflows.js +998 -0
- package/dist/src/swarm/error-recovery/error-recovery-coordinator.js +1197 -0
- package/dist/src/swarm/error-recovery/recovery-monitoring.js +772 -0
- package/dist/src/swarm/error-recovery/resilience-architecture.js +714 -0
- package/dist/src/swarm/error-recovery/self-healing-mechanisms.js +1319 -0
- package/dist/src/swarm/error-recovery/test-error-recovery-effectiveness.js +808 -0
- package/dist/src/swarm/executor-v2.js +322 -0
- package/dist/src/swarm/executor.js +815 -0
- package/dist/src/swarm/hive-mind-integration.js +703 -0
- package/dist/src/swarm/index.js +41 -0
- package/dist/src/swarm/json-output-aggregator.js +267 -0
- package/dist/src/swarm/large-scale-coordinator.js +542 -0
- package/dist/src/swarm/mcp-integration-wrapper.js +628 -0
- package/dist/src/swarm/memory.js +1117 -0
- package/dist/src/swarm/optimizations/__tests__/optimization.test.js +348 -0
- package/dist/src/swarm/optimizations/async-file-manager.js +285 -0
- package/dist/src/swarm/optimizations/circular-buffer.js +162 -0
- package/dist/src/swarm/optimizations/connection-pool.js +244 -0
- package/dist/src/swarm/optimizations/index.js +28 -0
- package/dist/src/swarm/optimizations/optimized-executor.js +320 -0
- package/dist/src/swarm/optimizations/ttl-map.js +234 -0
- package/dist/src/swarm/prompt-cli.js +200 -0
- package/dist/src/swarm/prompt-copier-enhanced.js +202 -0
- package/dist/src/swarm/prompt-copier.js +381 -0
- package/dist/src/swarm/prompt-manager.js +295 -0
- package/dist/src/swarm/prompt-utils.js +310 -0
- package/dist/src/swarm/result-aggregator.js +718 -0
- package/dist/src/swarm/sparc-executor.js +1568 -0
- package/dist/src/swarm/strategies/auto.js +758 -0
- package/dist/src/swarm/strategies/base.js +128 -0
- package/dist/src/swarm/strategies/research.js +914 -0
- package/dist/src/swarm/strategies/strategy-metrics-patch.js +2 -0
- package/dist/src/swarm/types.js +52 -0
- package/dist/src/swarm/workers/copy-worker.js +56 -0
- package/dist/src/utils/__tests__/github-cli-safety-wrapper.test.js +332 -400
- package/dist/src/utils/github-cli-safe.js +56 -64
- package/dist/src/utils/github-cli-safety-wrapper.js +451 -546
- package/dist/src/utils/npx-isolated-cache.js +104 -119
- package/dist/src/utils/preference-manager.js +622 -652
- package/dist/src/utils/timezone-utils.js +86 -105
- package/dist/src/validators/epic-config-schema.js +214 -0
- package/dist/src/validators/index.js +10 -0
- package/dist/src/validators/swarm-init-validator.js +259 -0
- package/dist/src/validators/todowrite-batching-validator.js +215 -0
- package/dist/src/validators/todowrite-integration.js +187 -0
- 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
|
|
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
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
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
|
-
|
|
113
|
-
|
|
114
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
141
|
-
|
|
142
|
-
|
|
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
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
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
|
-
|
|
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
|
-
|
|
178
|
-
|
|
179
|
-
|
|
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
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
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
|
-
|
|
249
|
-
|
|
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
|
-
|
|
258
|
-
.
|
|
259
|
-
|
|
260
|
-
|
|
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
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
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
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
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
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
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
|
-
|
|
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
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
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
|
-
|
|
368
|
-
|
|
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
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
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
|
-
|
|
423
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
471
|
-
|
|
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
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
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
|
-
|
|
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
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
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
|
-
|
|
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
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
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
|
-
|
|
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
|
-
|
|
551
|
-
|
|
552
|
-
|
|
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
|
-
|
|
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
|
-
|
|
573
|
-
|
|
574
|
-
|
|
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
|
-
|
|
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
|
-
|
|
609
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
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
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
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
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
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
|
-
|
|
688
|
-
|
|
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
|
-
|
|
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
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
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
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
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
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
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
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
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
|
-
|
|
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
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
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
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
853
|
-
|
|
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
|
-
|
|
870
|
-
|
|
871
|
-
|
|
620
|
+
optimizeHiveMindDatabase,
|
|
621
|
+
performMaintenance,
|
|
622
|
+
generateOptimizationReport
|
|
872
623
|
};
|