claude-flow-novice 1.3.0 → 1.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-flow-novice/preferences/generation.json +147 -0
- package/.claude-flow-novice/preferences/language-configs/javascript.json +51 -0
- package/.claude-flow-novice/preferences/language-configs/python.json +50 -0
- package/.claude-flow-novice/preferences/language-configs/rust.json +237 -0
- package/.claude-flow-novice/preferences/language-configs/typescript.json +54 -0
- package/.claude-flow-novice/preferences/project-local.json +91 -0
- package/.claude-flow-novice/preferences/resource-delegation.json +120 -0
- package/.claude-flow-novice/preferences/team-shared.json +195 -0
- package/.claude-flow-novice/preferences/user-global.json +247 -0
- package/.claude-flow-novice/templates/claude-md-templates/CLAUDE-JAVASCRIPT.md +769 -0
- package/.claude-flow-novice/templates/claude-md-templates/CLAUDE-PYTHON.md +1214 -0
- package/.claude-flow-novice/templates/claude-md-templates/CLAUDE-RUST.md +475 -0
- package/.claude-flow-novice/templates/claude-md-templates/CLAUDE-TYPESCRIPT.md +851 -0
- package/.claude-flow-novice/templates/claude-md-templates/README.md +263 -0
- package/CLAUDE.md +81 -0
- package/README-NPM.md +0 -0
- package/package.json +11 -7
- package/scripts/build/README.md +167 -0
- package/scripts/build/build-config.js +27 -0
- package/scripts/build/build-prompt-copier.sh +30 -0
- package/scripts/build/performance-monitor.js +869 -0
- package/scripts/build/prepare-publish.js +150 -0
- package/scripts/build/typescript-fixer.js +621 -0
- package/scripts/build/unified-builder.sh +428 -0
- package/scripts/build/update-bin-version.js +32 -0
- package/scripts/dev/README.md +264 -0
- package/scripts/dev/claude-flow-wrapper.sh +35 -0
- package/scripts/dev/claude-monitor.py +419 -0
- package/scripts/dev/claude-sparc.sh +562 -0
- package/scripts/dev/claude-wrapper.sh +17 -0
- package/scripts/dev/demo-phase3-compliance.js +172 -0
- package/scripts/dev/demo-task-system.ts +224 -0
- package/scripts/dev/deployment-validator.js +315 -0
- package/scripts/dev/spawn-claude-terminal.sh +32 -0
- package/scripts/dev/start-portal.sh +506 -0
- package/scripts/dev/start-web-ui.js +15 -0
- package/scripts/dev/stop-portal.sh +311 -0
- package/scripts/dev/validate-examples.ts +288 -0
- package/scripts/dev/validate-phase2.cjs +451 -0
- package/scripts/dev/validate-phase2.js +785 -0
- package/scripts/dev/validate-phase3.cjs +208 -0
- package/scripts/dev/validate-security-remediation.js +1 -0
- package/scripts/legacy/README.md +272 -0
- package/scripts/legacy/batch-fix-ts.sh +54 -0
- package/scripts/legacy/build-migration.sh +105 -0
- package/scripts/legacy/build-monitor.js +209 -0
- package/scripts/legacy/build-with-filter.sh +84 -0
- package/scripts/legacy/build-workaround.sh +71 -0
- package/scripts/legacy/fix-ts-advanced.js +358 -0
- package/scripts/legacy/fix-ts-final.sh +50 -0
- package/scripts/legacy/fix-ts-targeted.sh +49 -0
- package/scripts/legacy/fix-typescript-errors.js +305 -0
- package/scripts/legacy/force-build.sh +63 -0
- package/scripts/legacy/optimize-performance.js +400 -0
- package/scripts/legacy/performance-monitor.js +263 -0
- package/scripts/legacy/performance-monitoring.js +532 -0
- package/scripts/legacy/performance-test-runner.js +645 -0
- package/scripts/legacy/quick-fix-ts.js +281 -0
- package/scripts/legacy/safe-build.sh +63 -0
- package/scripts/migration/README.md +434 -0
- package/scripts/migration/install-arm64.js +78 -0
- package/scripts/migration/install.js +83 -0
- package/scripts/migration/migrate-hooks.js +173 -0
- package/scripts/migration/migration-examples.ts +318 -0
- package/scripts/optimization/build-optimizer.js +438 -0
- package/scripts/optimization/config-validator.js +761 -0
- package/scripts/optimization/test-optimization.js +432 -0
- package/scripts/optimization/unified-activation.js +839 -0
- package/scripts/performance/ACTIVATION_COMMANDS.md +292 -0
- package/scripts/performance/sqlite-enhanced-activation.sh +583 -0
- package/scripts/performance/test-enhanced-backend.sh +504 -0
- package/scripts/performance-test-runner.js +698 -0
- package/scripts/security/README.md +339 -0
- package/scripts/security/install-git-hooks.sh +132 -0
- package/scripts/security/ruv-swarm-safe.js +74 -0
- package/scripts/test/README.md +236 -0
- package/scripts/test/check-links.ts +274 -0
- package/scripts/test/check-performance-regression.ts +168 -0
- package/scripts/test/coverage-report.ts +692 -0
- package/scripts/test/generate-swarm-tests.js +633 -0
- package/scripts/test/integration-test-validation.cjs +253 -0
- package/scripts/test/load-test-swarm.js +576 -0
- package/scripts/test/run-phase3-compliance-tests.js +427 -0
- package/scripts/test/test-batch-tasks.ts +29 -0
- package/scripts/test/test-byzantine-resolution.js +246 -0
- package/scripts/test/test-claude-spawn-options.sh +63 -0
- package/scripts/test/test-cli-wizard.js +331 -0
- package/scripts/test/test-comprehensive.js +401 -0
- package/scripts/test/test-coordination-features.ts +238 -0
- package/scripts/test/test-fallback-systems.js +276 -0
- package/scripts/test/test-init-command.ts +302 -0
- package/scripts/test/test-mcp.ts +251 -0
- package/scripts/test/test-runner.ts +568 -0
- package/scripts/test/test-swarm-integration.sh +92 -0
- package/scripts/test/test-swarm.ts +142 -0
- package/scripts/test/validation-summary.ts +408 -0
- package/scripts/utils/README.md +261 -0
- package/scripts/utils/clean-build-artifacts.sh +94 -0
- package/scripts/utils/cleanup-root.sh +69 -0
- package/scripts/utils/fix-cliffy-imports.js +307 -0
- package/scripts/utils/fix-duplicate-imports.js +114 -0
- package/scripts/utils/fix-error-handling.cjs +70 -0
- package/scripts/utils/fix-import-paths.js +104 -0
- package/scripts/utils/fix-imports.js +116 -0
- package/scripts/utils/fix-shebang.js +78 -0
- package/scripts/utils/fix-test-modules.js +27 -0
- package/scripts/utils/fix-timezone-issue-246.js +200 -0
- package/scripts/utils/fix-ts-comprehensive.py +182 -0
- package/scripts/utils/fix-ts-targeted-batch.js +250 -0
- package/scripts/utils/remove-benchmark-conflicts.sh +140 -0
- package/scripts/utils/simple-test-fixer.js +190 -0
- package/scripts/utils/validate-metrics-structure.cjs +144 -0
- package/scripts/verify-mcp-server.js +86 -0
- package/src/cli/simple-commands/__tests__/agent.test.js +291 -0
- package/src/cli/simple-commands/__tests__/memory.test.js +8 -0
- package/src/cli/simple-commands/__tests__/swarm.test.js +371 -0
- package/src/cli/simple-commands/__tests__/task.test.js +8 -0
- package/src/cli/simple-commands/agent.js +216 -0
- package/src/cli/simple-commands/analysis.js +570 -0
- package/src/cli/simple-commands/automation-executor.js +1603 -0
- package/src/cli/simple-commands/automation.js +627 -0
- package/src/cli/simple-commands/batch-manager.js +338 -0
- package/src/cli/simple-commands/claude-telemetry.js +311 -0
- package/src/cli/simple-commands/claude-track.js +102 -0
- package/src/cli/simple-commands/concurrent-display.js +348 -0
- package/src/cli/simple-commands/config.js +319 -0
- package/src/cli/simple-commands/coordination.js +307 -0
- package/src/cli/simple-commands/enhanced-ui-views.js +654 -0
- package/src/cli/simple-commands/enhanced-webui-complete.js +1038 -0
- package/src/cli/simple-commands/fix-hook-variables.js +363 -0
- package/src/cli/simple-commands/github/gh-coordinator.js +605 -0
- package/src/cli/simple-commands/github/github-api.js +624 -0
- package/src/cli/simple-commands/github/init.js +543 -0
- package/src/cli/simple-commands/github.js +377 -0
- package/src/cli/simple-commands/goal.js +145 -0
- package/src/cli/simple-commands/hive-mind/auto-save-middleware.js +311 -0
- package/src/cli/simple-commands/hive-mind/communication.js +740 -0
- package/src/cli/simple-commands/hive-mind/core.js +1031 -0
- package/src/cli/simple-commands/hive-mind/db-optimizer.js +872 -0
- package/src/cli/simple-commands/hive-mind/mcp-wrapper.js +1364 -0
- package/src/cli/simple-commands/hive-mind/memory.js +1292 -0
- package/src/cli/simple-commands/hive-mind/performance-optimizer.js +618 -0
- package/src/cli/simple-commands/hive-mind/performance-test.js +373 -0
- package/src/cli/simple-commands/hive-mind/queen.js +809 -0
- package/src/cli/simple-commands/hive-mind/session-manager.js +1223 -0
- package/src/cli/simple-commands/hive-mind-optimize.js +361 -0
- package/src/cli/simple-commands/hive-mind-wizard.js +281 -0
- package/src/cli/simple-commands/hive-mind.js +3112 -0
- package/src/cli/simple-commands/hive.js +140 -0
- package/src/cli/simple-commands/hook-safety.js +671 -0
- package/src/cli/simple-commands/hooks.js +1268 -0
- package/src/cli/simple-commands/init/.claude/checkpoints/1756224542.json +7 -0
- package/src/cli/simple-commands/init/.claude/checkpoints/1756224544.json +8 -0
- package/src/cli/simple-commands/init/README.md +106 -0
- package/src/cli/simple-commands/init/VALIDATION_ROLLBACK.md +488 -0
- package/src/cli/simple-commands/init/agent-copier.js +347 -0
- package/src/cli/simple-commands/init/batch-init.js +663 -0
- package/src/cli/simple-commands/init/claude-commands/claude-flow-commands.js +438 -0
- package/src/cli/simple-commands/init/claude-commands/optimized-claude-flow-commands.js +876 -0
- package/src/cli/simple-commands/init/claude-commands/optimized-slash-commands.js +356 -0
- package/src/cli/simple-commands/init/claude-commands/optimized-sparc-commands.js +501 -0
- package/src/cli/simple-commands/init/claude-commands/slash-commands.js +57 -0
- package/src/cli/simple-commands/init/claude-commands/sparc-commands.js +296 -0
- package/src/cli/simple-commands/init/copy-revised-templates.js +175 -0
- package/src/cli/simple-commands/init/executable-wrapper.js +122 -0
- package/src/cli/simple-commands/init/gitignore-updater.js +137 -0
- package/src/cli/simple-commands/init/help.js +110 -0
- package/src/cli/simple-commands/init/hive-mind-init.js +749 -0
- package/src/cli/simple-commands/init/index.js +1953 -0
- package/src/cli/simple-commands/init/performance-monitor.js +344 -0
- package/src/cli/simple-commands/init/rollback/backup-manager.js +542 -0
- package/src/cli/simple-commands/init/rollback/index.js +399 -0
- package/src/cli/simple-commands/init/rollback/recovery-manager.js +778 -0
- package/src/cli/simple-commands/init/rollback/rollback-executor.js +521 -0
- package/src/cli/simple-commands/init/rollback/state-tracker.js +486 -0
- package/src/cli/simple-commands/init/sparc/roo-readme.js +61 -0
- package/src/cli/simple-commands/init/sparc/roomodes-config.js +102 -0
- package/src/cli/simple-commands/init/sparc/workflows.js +40 -0
- package/src/cli/simple-commands/init/sparc-structure.js +68 -0
- package/src/cli/simple-commands/init/template-copier.js +640 -0
- package/src/cli/simple-commands/init/templates/CLAUDE.md +1185 -0
- package/src/cli/simple-commands/init/templates/CLAUDE.md.optimized +265 -0
- package/src/cli/simple-commands/init/templates/claude-flow-universal +81 -0
- package/src/cli/simple-commands/init/templates/claude-flow.bat +18 -0
- package/src/cli/simple-commands/init/templates/claude-flow.ps1 +24 -0
- package/src/cli/simple-commands/init/templates/claude-md.js +1101 -0
- package/src/cli/simple-commands/init/templates/commands/analysis/bottleneck-detect.md +162 -0
- package/src/cli/simple-commands/init/templates/commands/automation/auto-agent.md +122 -0
- package/src/cli/simple-commands/init/templates/commands/coordination/swarm-init.md +85 -0
- package/src/cli/simple-commands/init/templates/commands/github/github-swarm.md +121 -0
- package/src/cli/simple-commands/init/templates/commands/helpers/standard-checkpoint-hooks.sh +179 -0
- package/src/cli/simple-commands/init/templates/commands/hooks/notification.md +113 -0
- package/src/cli/simple-commands/init/templates/commands/hooks/post-command.md +116 -0
- package/src/cli/simple-commands/init/templates/commands/hooks/post-edit.md +117 -0
- package/src/cli/simple-commands/init/templates/commands/hooks/post-task.md +112 -0
- package/src/cli/simple-commands/init/templates/commands/hooks/pre-command.md +113 -0
- package/src/cli/simple-commands/init/templates/commands/hooks/pre-edit.md +113 -0
- package/src/cli/simple-commands/init/templates/commands/hooks/pre-search.md +112 -0
- package/src/cli/simple-commands/init/templates/commands/hooks/pre-task.md +111 -0
- package/src/cli/simple-commands/init/templates/commands/hooks/session-end.md +118 -0
- package/src/cli/simple-commands/init/templates/commands/hooks/session-restore.md +118 -0
- package/src/cli/simple-commands/init/templates/commands/hooks/session-start.md +117 -0
- package/src/cli/simple-commands/init/templates/coordination-md.js +340 -0
- package/src/cli/simple-commands/init/templates/coordination.md +16 -0
- package/src/cli/simple-commands/init/templates/enhanced-templates.js +2347 -0
- package/src/cli/simple-commands/init/templates/github-safe-enhanced.js +331 -0
- package/src/cli/simple-commands/init/templates/github-safe.js +106 -0
- package/src/cli/simple-commands/init/templates/memory-bank-md.js +259 -0
- package/src/cli/simple-commands/init/templates/memory-bank.md +16 -0
- package/src/cli/simple-commands/init/templates/readme-files.js +72 -0
- package/src/cli/simple-commands/init/templates/safe-hook-patterns.js +430 -0
- package/src/cli/simple-commands/init/templates/settings.json +109 -0
- package/src/cli/simple-commands/init/templates/settings.json.enhanced +35 -0
- package/src/cli/simple-commands/init/templates/sparc-modes.js +1401 -0
- package/src/cli/simple-commands/init/templates/verification-claude-md.js +432 -0
- package/src/cli/simple-commands/init/validation/config-validator.js +354 -0
- package/src/cli/simple-commands/init/validation/health-checker.js +599 -0
- package/src/cli/simple-commands/init/validation/index.js +388 -0
- package/src/cli/simple-commands/init/validation/mode-validator.js +387 -0
- package/src/cli/simple-commands/init/validation/post-init-validator.js +390 -0
- package/src/cli/simple-commands/init/validation/pre-init-validator.js +290 -0
- package/src/cli/simple-commands/init/validation/test-runner.js +488 -0
- package/src/cli/simple-commands/init.js +4 -0
- package/src/cli/simple-commands/mcp-health.js +163 -0
- package/src/cli/simple-commands/mcp-integration-layer.js +689 -0
- package/src/cli/simple-commands/mcp.js +420 -0
- package/src/cli/simple-commands/memory-consolidation.js +631 -0
- package/src/cli/simple-commands/memory.js +345 -0
- package/src/cli/simple-commands/migrate-hooks.js +63 -0
- package/src/cli/simple-commands/monitor.js +417 -0
- package/src/cli/simple-commands/neural.js +148 -0
- package/src/cli/simple-commands/pair-autofix-only.js +755 -0
- package/src/cli/simple-commands/pair-basic.js +751 -0
- package/src/cli/simple-commands/pair-old.js +623 -0
- package/src/cli/simple-commands/pair-working.js +849 -0
- package/src/cli/simple-commands/pair.js +849 -0
- package/src/cli/simple-commands/performance-hooks.js +149 -0
- package/src/cli/simple-commands/performance-metrics.js +601 -0
- package/src/cli/simple-commands/process-ui-enhanced.js +821 -0
- package/src/cli/simple-commands/process-ui.js +274 -0
- package/src/cli/simple-commands/realtime-update-system.js +659 -0
- package/src/cli/simple-commands/sparc/architecture.js +1750 -0
- package/src/cli/simple-commands/sparc/commands.js +575 -0
- package/src/cli/simple-commands/sparc/completion.js +1831 -0
- package/src/cli/simple-commands/sparc/coordinator.js +1045 -0
- package/src/cli/simple-commands/sparc/index.js +321 -0
- package/src/cli/simple-commands/sparc/phase-base.js +430 -0
- package/src/cli/simple-commands/sparc/pseudocode.js +984 -0
- package/src/cli/simple-commands/sparc/refinement.js +1856 -0
- package/src/cli/simple-commands/sparc/specification.js +736 -0
- package/src/cli/simple-commands/sparc-modes/architect.js +125 -0
- package/src/cli/simple-commands/sparc-modes/ask.js +126 -0
- package/src/cli/simple-commands/sparc-modes/code.js +148 -0
- package/src/cli/simple-commands/sparc-modes/debug.js +112 -0
- package/src/cli/simple-commands/sparc-modes/devops.js +137 -0
- package/src/cli/simple-commands/sparc-modes/docs-writer.js +38 -0
- package/src/cli/simple-commands/sparc-modes/generic.js +34 -0
- package/src/cli/simple-commands/sparc-modes/index.js +201 -0
- package/src/cli/simple-commands/sparc-modes/integration.js +55 -0
- package/src/cli/simple-commands/sparc-modes/mcp.js +38 -0
- package/src/cli/simple-commands/sparc-modes/monitoring.js +38 -0
- package/src/cli/simple-commands/sparc-modes/optimization.js +38 -0
- package/src/cli/simple-commands/sparc-modes/security-review.js +130 -0
- package/src/cli/simple-commands/sparc-modes/sparc-orchestrator.js +167 -0
- package/src/cli/simple-commands/sparc-modes/spec-pseudocode.js +38 -0
- package/src/cli/simple-commands/sparc-modes/supabase-admin.js +149 -0
- package/src/cli/simple-commands/sparc-modes/swarm.js +436 -0
- package/src/cli/simple-commands/sparc-modes/tdd.js +112 -0
- package/src/cli/simple-commands/sparc-modes/tutorial.js +277 -0
- package/src/cli/simple-commands/sparc.js +530 -0
- package/src/cli/simple-commands/start-ui.js +147 -0
- package/src/cli/simple-commands/start-wrapper.js +285 -0
- package/src/cli/simple-commands/start.js +2 -0
- package/src/cli/simple-commands/status.js +303 -0
- package/src/cli/simple-commands/stream-chain-clean.js +221 -0
- package/src/cli/simple-commands/stream-chain-fixed.js +89 -0
- package/src/cli/simple-commands/stream-chain-real.js +408 -0
- package/src/cli/simple-commands/stream-chain-working.js +323 -0
- package/src/cli/simple-commands/stream-chain.js +491 -0
- package/src/cli/simple-commands/stream-processor.js +340 -0
- package/src/cli/simple-commands/swarm-executor.js +253 -0
- package/src/cli/simple-commands/swarm-metrics-integration.js +371 -0
- package/src/cli/simple-commands/swarm-ui.js +741 -0
- package/src/cli/simple-commands/swarm-webui-integration.js +311 -0
- package/src/cli/simple-commands/swarm.js +2277 -0
- package/src/cli/simple-commands/task.js +228 -0
- package/src/cli/simple-commands/templates/mle-star-workflow.json +294 -0
- package/src/cli/simple-commands/timestamp-fix.js +104 -0
- package/src/cli/simple-commands/token-tracker.js +372 -0
- package/src/cli/simple-commands/tool-execution-framework.js +555 -0
- package/src/cli/simple-commands/train-and-stream.js +354 -0
- package/src/cli/simple-commands/training-pipeline.js +874 -0
- package/src/cli/simple-commands/training.js +288 -0
- package/src/cli/simple-commands/verification-hooks.js +336 -0
- package/src/cli/simple-commands/verification-integration.js +464 -0
- package/src/cli/simple-commands/verification-training-integration.js +646 -0
- package/src/cli/simple-commands/verification.js +551 -0
- package/src/cli/simple-commands/web-server.js +929 -0
- package/src/cli/simple-commands/webui-validator.js +136 -0
- package/src/language/README.md +503 -0
- package/src/language/claude-md-generator.js +618 -0
- package/src/language/cli.js +422 -0
- package/src/language/example.js +347 -0
- package/src/language/integration-system.js +619 -0
- package/src/language/language-detector.js +581 -0
|
@@ -0,0 +1,872 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hive Mind Database Optimizer
|
|
3
|
+
*
|
|
4
|
+
* Safe, backward-compatible database optimization for existing deployments
|
|
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
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 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');
|
|
99
|
+
}
|
|
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
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* 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
|
+
`
|
|
118
|
+
SELECT name FROM sqlite_master
|
|
119
|
+
WHERE type='table' AND name='schema_version'
|
|
120
|
+
`,
|
|
121
|
+
)
|
|
122
|
+
.get();
|
|
123
|
+
|
|
124
|
+
if (!tableExists) {
|
|
125
|
+
// Create schema version table
|
|
126
|
+
db.exec(`
|
|
127
|
+
CREATE TABLE schema_version (
|
|
128
|
+
version REAL PRIMARY KEY,
|
|
129
|
+
applied_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
130
|
+
description TEXT
|
|
131
|
+
)
|
|
132
|
+
`);
|
|
133
|
+
|
|
134
|
+
// Insert initial version
|
|
135
|
+
db.prepare(
|
|
136
|
+
`
|
|
137
|
+
INSERT INTO schema_version (version, description)
|
|
138
|
+
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
|
+
`
|
|
149
|
+
SELECT version FROM schema_version
|
|
150
|
+
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
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Update schema version
|
|
164
|
+
*/
|
|
165
|
+
function updateSchemaVersion(db, version, description = '') {
|
|
166
|
+
db.prepare(
|
|
167
|
+
`
|
|
168
|
+
INSERT OR REPLACE INTO schema_version (version, description)
|
|
169
|
+
VALUES (?, ?)
|
|
170
|
+
`,
|
|
171
|
+
).run(version, description || `Updated to version ${version}`);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* 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
|
+
`
|
|
185
|
+
SELECT name FROM sqlite_master
|
|
186
|
+
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}`);
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Ensure all required columns exist
|
|
247
|
+
*/
|
|
248
|
+
function ensureRequiredColumns(db) {
|
|
249
|
+
// First check which tables exist
|
|
250
|
+
const tables = db
|
|
251
|
+
.prepare(
|
|
252
|
+
`
|
|
253
|
+
SELECT name FROM sqlite_master
|
|
254
|
+
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
|
+
`
|
|
268
|
+
SELECT COUNT(*) as count FROM pragma_table_info('tasks')
|
|
269
|
+
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;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// Check and add completed_at column to tasks table
|
|
289
|
+
const hasCompletedAt = db
|
|
290
|
+
.prepare(
|
|
291
|
+
`
|
|
292
|
+
SELECT COUNT(*) as count FROM pragma_table_info('tasks')
|
|
293
|
+
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;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
// Check and add result column to tasks table
|
|
313
|
+
const hasResult = db
|
|
314
|
+
.prepare(
|
|
315
|
+
`
|
|
316
|
+
SELECT COUNT(*) as count FROM pragma_table_info('tasks')
|
|
317
|
+
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;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
if (tableSet.has('swarms')) {
|
|
338
|
+
// Check and add updated_at column to swarms table
|
|
339
|
+
const hasUpdatedAt = db
|
|
340
|
+
.prepare(
|
|
341
|
+
`
|
|
342
|
+
SELECT COUNT(*) as count FROM pragma_table_info('swarms')
|
|
343
|
+
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;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Apply advanced performance indexes
|
|
366
|
+
*/
|
|
367
|
+
function applyAdvancedIndexes(db) {
|
|
368
|
+
// Check which tables exist
|
|
369
|
+
const tables = db
|
|
370
|
+
.prepare(
|
|
371
|
+
`
|
|
372
|
+
SELECT name FROM sqlite_master
|
|
373
|
+
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}`);
|
|
415
|
+
}
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* Add performance tracking tables
|
|
421
|
+
*/
|
|
422
|
+
function addPerformanceTracking(db) {
|
|
423
|
+
// Agent performance metrics
|
|
424
|
+
db.exec(`
|
|
425
|
+
CREATE TABLE IF NOT EXISTS agent_performance (
|
|
426
|
+
agent_id TEXT PRIMARY KEY,
|
|
427
|
+
tasks_completed INTEGER DEFAULT 0,
|
|
428
|
+
tasks_failed INTEGER DEFAULT 0,
|
|
429
|
+
avg_completion_time REAL,
|
|
430
|
+
success_rate REAL,
|
|
431
|
+
last_updated DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
432
|
+
FOREIGN KEY (agent_id) REFERENCES agents(id)
|
|
433
|
+
)
|
|
434
|
+
`);
|
|
435
|
+
|
|
436
|
+
// Swarm performance metrics
|
|
437
|
+
db.exec(`
|
|
438
|
+
CREATE TABLE IF NOT EXISTS swarm_performance (
|
|
439
|
+
swarm_id TEXT PRIMARY KEY,
|
|
440
|
+
total_tasks INTEGER DEFAULT 0,
|
|
441
|
+
completed_tasks INTEGER DEFAULT 0,
|
|
442
|
+
avg_consensus_time REAL,
|
|
443
|
+
memory_efficiency REAL,
|
|
444
|
+
last_updated DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
445
|
+
FOREIGN KEY (swarm_id) REFERENCES swarms(id)
|
|
446
|
+
)
|
|
447
|
+
`);
|
|
448
|
+
|
|
449
|
+
// Create triggers to update performance metrics
|
|
450
|
+
db.exec(`
|
|
451
|
+
CREATE TRIGGER IF NOT EXISTS update_agent_performance
|
|
452
|
+
AFTER UPDATE OF status ON tasks
|
|
453
|
+
WHEN NEW.status = 'completed' OR NEW.status = 'failed'
|
|
454
|
+
BEGIN
|
|
455
|
+
INSERT OR REPLACE INTO agent_performance (agent_id, tasks_completed, tasks_failed)
|
|
456
|
+
VALUES (
|
|
457
|
+
NEW.agent_id,
|
|
458
|
+
COALESCE((SELECT tasks_completed FROM agent_performance WHERE agent_id = NEW.agent_id), 0) +
|
|
459
|
+
CASE WHEN NEW.status = 'completed' THEN 1 ELSE 0 END,
|
|
460
|
+
COALESCE((SELECT tasks_failed FROM agent_performance WHERE agent_id = NEW.agent_id), 0) +
|
|
461
|
+
CASE WHEN NEW.status = 'failed' THEN 1 ELSE 0 END
|
|
462
|
+
);
|
|
463
|
+
END
|
|
464
|
+
`);
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
/**
|
|
468
|
+
* Add memory optimization features
|
|
469
|
+
*/
|
|
470
|
+
function addMemoryOptimization(db) {
|
|
471
|
+
// Check if collective_memory table exists
|
|
472
|
+
const tables = db
|
|
473
|
+
.prepare(
|
|
474
|
+
`
|
|
475
|
+
SELECT name FROM sqlite_master
|
|
476
|
+
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
|
+
`
|
|
490
|
+
SELECT COUNT(*) as count FROM pragma_table_info('collective_memory')
|
|
491
|
+
WHERE name = 'access_count'
|
|
492
|
+
`,
|
|
493
|
+
)
|
|
494
|
+
.get();
|
|
495
|
+
|
|
496
|
+
if (!hasAccessCount || hasAccessCount.count === 0) {
|
|
497
|
+
try {
|
|
498
|
+
db.exec(`
|
|
499
|
+
ALTER TABLE collective_memory
|
|
500
|
+
ADD COLUMN access_count INTEGER DEFAULT 0
|
|
501
|
+
`);
|
|
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
|
+
}
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
// Check and add accessed_at column (not last_accessed)
|
|
511
|
+
const hasAccessedAt = db
|
|
512
|
+
.prepare(
|
|
513
|
+
`
|
|
514
|
+
SELECT COUNT(*) as count FROM pragma_table_info('collective_memory')
|
|
515
|
+
WHERE name = 'accessed_at'
|
|
516
|
+
`,
|
|
517
|
+
)
|
|
518
|
+
.get();
|
|
519
|
+
|
|
520
|
+
if (!hasAccessedAt || hasAccessedAt.count === 0) {
|
|
521
|
+
try {
|
|
522
|
+
db.exec(`
|
|
523
|
+
ALTER TABLE collective_memory
|
|
524
|
+
ADD COLUMN accessed_at DATETIME
|
|
525
|
+
`);
|
|
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
|
+
}
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
// Add compressed and size columns if missing
|
|
535
|
+
const hasCompressed = db
|
|
536
|
+
.prepare(
|
|
537
|
+
`
|
|
538
|
+
SELECT COUNT(*) as count FROM pragma_table_info('collective_memory')
|
|
539
|
+
WHERE name = 'compressed'
|
|
540
|
+
`,
|
|
541
|
+
)
|
|
542
|
+
.get();
|
|
543
|
+
|
|
544
|
+
if (!hasCompressed || hasCompressed.count === 0) {
|
|
545
|
+
try {
|
|
546
|
+
db.exec(`
|
|
547
|
+
ALTER TABLE collective_memory
|
|
548
|
+
ADD COLUMN compressed INTEGER DEFAULT 0
|
|
549
|
+
`);
|
|
550
|
+
} catch (error) {
|
|
551
|
+
if (!error.message.includes('duplicate column') && !error.message.includes('no such table')) {
|
|
552
|
+
throw error;
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
const hasSize = db
|
|
558
|
+
.prepare(
|
|
559
|
+
`
|
|
560
|
+
SELECT COUNT(*) as count FROM pragma_table_info('collective_memory')
|
|
561
|
+
WHERE name = 'size'
|
|
562
|
+
`,
|
|
563
|
+
)
|
|
564
|
+
.get();
|
|
565
|
+
|
|
566
|
+
if (!hasSize || hasSize.count === 0) {
|
|
567
|
+
try {
|
|
568
|
+
db.exec(`
|
|
569
|
+
ALTER TABLE collective_memory
|
|
570
|
+
ADD COLUMN size INTEGER DEFAULT 0
|
|
571
|
+
`);
|
|
572
|
+
} catch (error) {
|
|
573
|
+
if (!error.message.includes('duplicate column') && !error.message.includes('no such table')) {
|
|
574
|
+
throw error;
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
// Create memory usage summary view
|
|
580
|
+
db.exec(`
|
|
581
|
+
CREATE VIEW IF NOT EXISTS memory_usage_summary AS
|
|
582
|
+
SELECT
|
|
583
|
+
swarm_id,
|
|
584
|
+
COUNT(*) as total_entries,
|
|
585
|
+
SUM(LENGTH(value)) as total_size,
|
|
586
|
+
AVG(access_count) as avg_access_count,
|
|
587
|
+
COUNT(CASE WHEN access_count = 0 THEN 1 END) as unused_entries
|
|
588
|
+
FROM collective_memory
|
|
589
|
+
GROUP BY swarm_id
|
|
590
|
+
`);
|
|
591
|
+
|
|
592
|
+
// Add memory cleanup tracking
|
|
593
|
+
db.exec(`
|
|
594
|
+
CREATE TABLE IF NOT EXISTS memory_cleanup_log (
|
|
595
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
596
|
+
swarm_id TEXT,
|
|
597
|
+
entries_removed INTEGER,
|
|
598
|
+
space_reclaimed INTEGER,
|
|
599
|
+
cleanup_type TEXT,
|
|
600
|
+
performed_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
|
601
|
+
)
|
|
602
|
+
`);
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
/**
|
|
606
|
+
* Add behavioral tracking features
|
|
607
|
+
*/
|
|
608
|
+
function addBehavioralTracking(db) {
|
|
609
|
+
// Agent interaction patterns
|
|
610
|
+
db.exec(`
|
|
611
|
+
CREATE TABLE IF NOT EXISTS agent_interactions (
|
|
612
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
613
|
+
from_agent_id TEXT,
|
|
614
|
+
to_agent_id TEXT,
|
|
615
|
+
interaction_type TEXT,
|
|
616
|
+
swarm_id TEXT,
|
|
617
|
+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
618
|
+
FOREIGN KEY (from_agent_id) REFERENCES agents(id),
|
|
619
|
+
FOREIGN KEY (to_agent_id) REFERENCES agents(id),
|
|
620
|
+
FOREIGN KEY (swarm_id) REFERENCES swarms(id)
|
|
621
|
+
)
|
|
622
|
+
`);
|
|
623
|
+
|
|
624
|
+
// Behavioral patterns
|
|
625
|
+
db.exec(`
|
|
626
|
+
CREATE TABLE IF NOT EXISTS behavioral_patterns (
|
|
627
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
628
|
+
swarm_id TEXT,
|
|
629
|
+
pattern_type TEXT,
|
|
630
|
+
pattern_data TEXT, -- JSON
|
|
631
|
+
confidence REAL,
|
|
632
|
+
first_observed DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
633
|
+
last_observed DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
634
|
+
occurrence_count INTEGER DEFAULT 1,
|
|
635
|
+
FOREIGN KEY (swarm_id) REFERENCES swarms(id)
|
|
636
|
+
)
|
|
637
|
+
`);
|
|
638
|
+
|
|
639
|
+
// Create indexes for behavioral analysis
|
|
640
|
+
db.exec(`
|
|
641
|
+
CREATE INDEX IF NOT EXISTS idx_interactions_swarm ON agent_interactions(swarm_id);
|
|
642
|
+
CREATE INDEX IF NOT EXISTS idx_interactions_agents ON agent_interactions(from_agent_id, to_agent_id);
|
|
643
|
+
CREATE INDEX IF NOT EXISTS idx_patterns_swarm_type ON behavioral_patterns(swarm_id, pattern_type);
|
|
644
|
+
`);
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
/**
|
|
648
|
+
* 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
|
+
`
|
|
662
|
+
SELECT name FROM sqlite_master
|
|
663
|
+
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
|
+
`
|
|
677
|
+
DELETE FROM collective_memory
|
|
678
|
+
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}`));
|
|
686
|
+
}
|
|
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(`
|
|
698
|
+
CREATE TABLE IF NOT EXISTS tasks_archive AS
|
|
699
|
+
SELECT * FROM tasks WHERE 1=0
|
|
700
|
+
`);
|
|
701
|
+
|
|
702
|
+
// Check if completed_at column exists
|
|
703
|
+
const hasCompletedAt = db
|
|
704
|
+
.prepare(
|
|
705
|
+
`
|
|
706
|
+
SELECT COUNT(*) as count FROM pragma_table_info('tasks')
|
|
707
|
+
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(`
|
|
720
|
+
INSERT INTO tasks_archive
|
|
721
|
+
SELECT * FROM tasks
|
|
722
|
+
WHERE status = 'completed' AND completed_at < '${archiveCutoff.toISOString()}'
|
|
723
|
+
`);
|
|
724
|
+
|
|
725
|
+
archived = db
|
|
726
|
+
.prepare(
|
|
727
|
+
`
|
|
728
|
+
DELETE FROM tasks
|
|
729
|
+
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(`
|
|
739
|
+
INSERT INTO tasks_archive
|
|
740
|
+
SELECT * FROM tasks
|
|
741
|
+
WHERE status = 'completed' AND created_at < '${archiveCutoff.toISOString()}'
|
|
742
|
+
`);
|
|
743
|
+
|
|
744
|
+
archived = db
|
|
745
|
+
.prepare(
|
|
746
|
+
`
|
|
747
|
+
DELETE FROM tasks
|
|
748
|
+
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
|
+
}
|
|
770
|
+
}
|
|
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
|
+
}
|
|
779
|
+
|
|
780
|
+
/**
|
|
781
|
+
* 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
|
+
`
|
|
798
|
+
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
|
+
`
|
|
808
|
+
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
|
+
`
|
|
823
|
+
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
|
+
`
|
|
835
|
+
SELECT COUNT(*) as count FROM pragma_table_info('tasks')
|
|
836
|
+
WHERE name = 'completed_at'
|
|
837
|
+
`,
|
|
838
|
+
)
|
|
839
|
+
.get();
|
|
840
|
+
|
|
841
|
+
if (hasCompletedAt && hasCompletedAt.count > 0) {
|
|
842
|
+
avgTaskTime = db
|
|
843
|
+
.prepare(
|
|
844
|
+
`
|
|
845
|
+
SELECT AVG(julianday(completed_at) - julianday(created_at)) * 24 * 60 as avg_minutes
|
|
846
|
+
FROM tasks WHERE completed_at IS NOT NULL
|
|
847
|
+
`,
|
|
848
|
+
)
|
|
849
|
+
.get();
|
|
850
|
+
}
|
|
851
|
+
} catch (error) {
|
|
852
|
+
// If error, just use default value
|
|
853
|
+
console.warn('Could not calculate average task time:', error.message);
|
|
854
|
+
}
|
|
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
|
+
}
|
|
866
|
+
|
|
867
|
+
// Export for use in CLI
|
|
868
|
+
export default {
|
|
869
|
+
optimizeHiveMindDatabase,
|
|
870
|
+
performMaintenance,
|
|
871
|
+
generateOptimizationReport,
|
|
872
|
+
};
|