snow-flow 3.0.9 โ 3.0.13
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.bat +0 -0
- package/claude-flow.ps1 +0 -0
- package/dist/mcp/intelligent-reporting-mcp.d.ts +7 -0
- package/dist/mcp/intelligent-reporting-mcp.js +379 -0
- package/dist/mcp/servicenow-automation-mcp.js +0 -0
- package/dist/mcp/servicenow-deployment-mcp.js +0 -0
- package/dist/mcp/servicenow-integration-mcp.js +0 -0
- package/dist/mcp/servicenow-mcp-server.js +0 -0
- package/dist/mcp/servicenow-operations-mcp.js +0 -0
- package/dist/mcp/servicenow-platform-development-mcp.js +0 -0
- package/dist/mcp/servicenow-progressive-indexer.js +0 -0
- package/dist/mcp/servicenow-reporting-analytics-mcp.js +280 -29
- package/dist/mcp/servicenow-security-compliance-mcp.js +0 -0
- package/dist/mcp/servicenow-update-set-mcp.js +0 -0
- package/dist/mcp/start-all-mcp-servers.js +0 -0
- package/dist/mcp/start-servicenow-mcp.js +0 -0
- package/dist/services/widget-deployment-service.d.ts +2 -1
- package/dist/services/widget-deployment-service.js +17 -16
- package/dist/utils/anti-mock-data-validator.d.ts +66 -0
- package/dist/utils/anti-mock-data-validator.js +316 -0
- package/dist/utils/servicenow-eventual-consistency.d.ts +120 -0
- package/dist/utils/servicenow-eventual-consistency.js +220 -0
- package/memory/agents/README.md +1 -1
- package/memory/claude-flow-data.json +1 -1
- package/memory/sessions/README.md +1 -1
- package/package.json +4 -3
- package/.mcp.json +0 -22
- package/claude-flow.config.json +0 -20
- package/dist/agents/app-creator.agent.d.ts +0 -55
- package/dist/agents/app-creator.agent.js +0 -1292
- package/dist/agents/base/base-snow-agent.d.ts +0 -75
- package/dist/agents/base/base-snow-agent.js +0 -169
- package/dist/agents/base-snow-agent.d.ts +0 -63
- package/dist/agents/base-snow-agent.js +0 -296
- package/dist/agents/coordinator.d.ts +0 -200
- package/dist/agents/coordinator.js +0 -777
- package/dist/agents/flow-builder-agent.d.ts +0 -18
- package/dist/agents/flow-builder-agent.js +0 -197
- package/dist/agents/queen-403-handler.d.ts +0 -47
- package/dist/agents/queen-403-handler.js +0 -175
- package/dist/agents/queen-agent-example.d.ts +0 -23
- package/dist/agents/queen-agent-example.js +0 -240
- package/dist/agents/queen-agent.test.d.ts +0 -6
- package/dist/agents/script-generator.agent.d.ts +0 -32
- package/dist/agents/script-generator.agent.js +0 -772
- package/dist/agents/script-writer-agent.d.ts +0 -22
- package/dist/agents/script-writer-agent.js +0 -695
- package/dist/agents/security-agent.d.ts +0 -23
- package/dist/agents/security-agent.js +0 -619
- package/dist/agents/specialists/automation-specialist.agent.d.ts +0 -44
- package/dist/agents/specialists/automation-specialist.agent.js +0 -204
- package/dist/agents/specialists/data-specialist.agent.d.ts +0 -165
- package/dist/agents/specialists/data-specialist.agent.js +0 -484
- package/dist/agents/specialists/integration-specialist.agent.d.ts +0 -227
- package/dist/agents/specialists/integration-specialist.agent.js +0 -725
- package/dist/agents/specialists/reporting-specialist.agent.d.ts +0 -54
- package/dist/agents/specialists/reporting-specialist.agent.js +0 -193
- package/dist/agents/specialists/security-specialist.agent.d.ts +0 -85
- package/dist/agents/specialists/security-specialist.agent.js +0 -309
- package/dist/agents/teams/adaptive/adaptive-coordinator.agent.d.ts +0 -111
- package/dist/agents/teams/adaptive/adaptive-coordinator.agent.js +0 -383
- package/dist/agents/teams/base-team.d.ts +0 -72
- package/dist/agents/teams/base-team.js +0 -253
- package/dist/agents/teams/team-types.d.ts +0 -265
- package/dist/agents/teams/team-types.js +0 -3
- package/dist/agents/teams/widget/widget-architect.agent.d.ts +0 -61
- package/dist/agents/teams/widget/widget-architect.agent.js +0 -436
- package/dist/agents/teams/widget/widget-backend.agent.d.ts +0 -74
- package/dist/agents/teams/widget/widget-backend.agent.js +0 -1531
- package/dist/agents/teams/widget/widget-frontend.agent.d.ts +0 -67
- package/dist/agents/teams/widget/widget-frontend.agent.js +0 -1014
- package/dist/agents/teams/widget/widget-platform.agent.d.ts +0 -90
- package/dist/agents/teams/widget/widget-platform.agent.js +0 -1190
- package/dist/agents/teams/widget/widget-uiux.agent.d.ts +0 -107
- package/dist/agents/teams/widget/widget-uiux.agent.js +0 -1033
- package/dist/agents/test-agent.d.ts +0 -26
- package/dist/agents/test-agent.js +0 -734
- package/dist/agents/test-agents.d.ts +0 -7
- package/dist/agents/test-agents.js +0 -64
- package/dist/agents/ui-builder.agent.d.ts +0 -50
- package/dist/agents/ui-builder.agent.js +0 -1539
- package/dist/agents/widget-builder.agent.d.ts +0 -32
- package/dist/agents/widget-builder.agent.js +0 -439
- package/dist/agents/widget-creator-agent.d.ts +0 -18
- package/dist/agents/widget-creator-agent.js +0 -498
- package/dist/agents/workflow-designer.agent.d.ts +0 -31
- package/dist/agents/workflow-designer.agent.js +0 -583
- package/dist/api/error-handling.d.ts +0 -325
- package/dist/api/error-handling.js +0 -1488
- package/dist/api/flow-designer-api.d.ts +0 -167
- package/dist/api/flow-designer-api.js +0 -554
- package/dist/api/natural-language-mapper.d.ts +0 -124
- package/dist/api/natural-language-mapper.js +0 -501
- package/dist/api/performance-optimizer.d.ts +0 -240
- package/dist/api/performance-optimizer.js +0 -706
- package/dist/api/transaction-manager.d.ts +0 -189
- package/dist/api/transaction-manager.js +0 -576
- package/dist/assembly/team-assembler.d.ts +0 -140
- package/dist/assembly/team-assembler.js +0 -502
- package/dist/benchmark-performance.d.ts +0 -41
- package/dist/benchmark-performance.js +0 -230
- package/dist/cli/snow-flow-cli-integration.d.ts +0 -30
- package/dist/cli/snow-flow-cli-integration.js +0 -396
- package/dist/coordination/coordination-engine.d.ts +0 -41
- package/dist/coordination/coordination-engine.js +0 -324
- package/dist/coordination/coordination.test.d.ts +0 -6
- package/dist/coordination/example.d.ts +0 -31
- package/dist/coordination/example.js +0 -394
- package/dist/coordination/execution-patterns.d.ts +0 -43
- package/dist/coordination/execution-patterns.js +0 -507
- package/dist/coordination/factory.d.ts +0 -94
- package/dist/coordination/factory.js +0 -433
- package/dist/coordination/index.d.ts +0 -71
- package/dist/coordination/index.js +0 -135
- package/dist/coordination/progress-monitor.d.ts +0 -71
- package/dist/coordination/progress-monitor.js +0 -505
- package/dist/coordination/quality-gates.d.ts +0 -124
- package/dist/coordination/quality-gates.js +0 -577
- package/dist/coordination/shared-memory.d.ts +0 -39
- package/dist/coordination/shared-memory.js +0 -289
- package/dist/coordination/task-dependencies.d.ts +0 -50
- package/dist/coordination/task-dependencies.js +0 -407
- package/dist/coordination/team-coordinator.d.ts +0 -59
- package/dist/coordination/team-coordinator.js +0 -550
- package/dist/coordination/types.d.ts +0 -152
- package/dist/coordination/types.js +0 -3
- package/dist/mcp/mcp-wrapper.d.ts +0 -8
- package/dist/mcp/mcp-wrapper.js +0 -77
- package/dist/mcp/servicenow-flow-composer-enhanced.js +0 -396
- package/dist/mcp/servicenow-flow-composer-mcp.d.ts +0 -7
- package/dist/mcp/servicenow-flow-composer-mcp.js +0 -3103
- package/dist/mcp/servicenow-graph-memory-mcp.d.ts +0 -7
- package/dist/mcp/servicenow-intelligent-mcp.d.ts +0 -143
- package/dist/mcp/servicenow-intelligent-mcp.js +0 -4156
- package/dist/mcp/servicenow-xml-flow-mcp.d.ts +0 -53
- package/dist/mcp/servicenow-xml-flow-mcp.js +0 -1704
- package/dist/mcp/singleton-check.js +0 -31
- package/dist/mcp/test-simple-mcp.d.ts +0 -6
- package/dist/mcp/test-simple-mcp.js +0 -63
- package/dist/memory/hierarchical-memory-system.d.ts +0 -90
- package/dist/memory/hierarchical-memory-system.js +0 -400
- package/dist/memory/index.d.ts +0 -9
- package/dist/memory/index.js +0 -20
- package/dist/memory/mcp-integration-example.d.ts +0 -6
- package/dist/memory/mcp-integration-example.js +0 -281
- package/dist/memory/memory-client.d.ts +0 -199
- package/dist/memory/memory-client.js +0 -364
- package/dist/memory/memory-manager.d.ts +0 -29
- package/dist/memory/memory-manager.js +0 -280
- package/dist/memory/memory-operations.d.ts +0 -179
- package/dist/memory/memory-operations.js +0 -691
- package/dist/memory/memory-system.d.ts +0 -153
- package/dist/memory/memory-system.js +0 -651
- package/dist/memory/memory-test.d.ts +0 -6
- package/dist/memory/memory-test.js +0 -161
- package/dist/memory/snow-flow-memory-patterns.d.ts +0 -213
- package/dist/memory/snow-flow-memory-patterns.js +0 -200
- package/dist/memory/snow-memory.d.ts +0 -54
- package/dist/memory/snow-memory.js +0 -301
- package/dist/memory/swarm-memory.d.ts +0 -135
- package/dist/memory/swarm-memory.js +0 -378
- package/dist/optimization/flow-performance-optimizer.js +0 -723
- package/dist/orchestration/discovery/service-discovery.d.ts +0 -81
- package/dist/orchestration/discovery/service-discovery.js +0 -808
- package/dist/orchestration/distributed-orchestrator.d.ts +0 -131
- package/dist/orchestration/distributed-orchestrator.js +0 -712
- package/dist/orchestration/distribution/task-distribution-engine.d.ts +0 -53
- package/dist/orchestration/distribution/task-distribution-engine.js +0 -586
- package/dist/orchestration/flow-update-orchestrator.js +0 -648
- package/dist/orchestration/interfaces/distributed-orchestration.interface.d.ts +0 -606
- package/dist/orchestration/interfaces/distributed-orchestration.interface.js +0 -65
- package/dist/orchestration/protocols/mcpx-protocol.d.ts +0 -71
- package/dist/orchestration/protocols/mcpx-protocol.js +0 -668
- package/dist/orchestration/state/distributed-state-manager.d.ts +0 -89
- package/dist/orchestration/state/distributed-state-manager.js +0 -716
- package/dist/orchestrator/flow-composer.d.ts +0 -105
- package/dist/orchestrator/flow-composer.js +0 -404
- package/dist/orchestrator/flow-pattern-templates.d.ts +0 -260
- package/dist/orchestrator/flow-pattern-templates.js +0 -908
- package/dist/orchestrator/flow-subflow-decision-engine.d.ts +0 -202
- package/dist/orchestrator/flow-subflow-decision-engine.js +0 -769
- package/dist/orchestrator/flow-validation-engine.d.ts +0 -169
- package/dist/orchestrator/flow-validation-engine.js +0 -913
- package/dist/orchestrator/snow-orchestrator.d.ts +0 -49
- package/dist/orchestrator/snow-orchestrator.js +0 -235
- package/dist/orchestrator/subflow-creation-handler.d.ts +0 -230
- package/dist/orchestrator/subflow-creation-handler.js +0 -791
- package/dist/patterns/adaptive-patterns.d.ts +0 -140
- package/dist/patterns/adaptive-patterns.js +0 -549
- package/dist/queen/advanced-integration-test.d.ts +0 -48
- package/dist/queen/advanced-integration-test.js +0 -473
- package/dist/queen/integration-example.d.ts +0 -82
- package/dist/queen/integration-example.js +0 -219
- package/dist/queen/integration-test.d.ts +0 -72
- package/dist/queen/integration-test.js +0 -563
- package/dist/queen/mock-mcp-client.d.ts +0 -71
- package/dist/queen/mock-mcp-client.js +0 -317
- package/dist/queen/run-integration-tests.d.ts +0 -9
- package/dist/queen/run-integration-tests.js +0 -27
- package/dist/sparc/coordinators/adaptive-coordinator.d.ts +0 -54
- package/dist/sparc/coordinators/adaptive-coordinator.js +0 -367
- package/dist/sparc/coordinators/team-coordinator.d.ts +0 -66
- package/dist/sparc/coordinators/team-coordinator.js +0 -402
- package/dist/sparc/modes/team-modes.d.ts +0 -33
- package/dist/sparc/modes/team-modes.js +0 -399
- package/dist/sparc/teams/application-team.d.ts +0 -73
- package/dist/sparc/teams/application-team.js +0 -463
- package/dist/sparc/teams/flow-team.d.ts +0 -67
- package/dist/sparc/teams/flow-team.js +0 -373
- package/dist/sparc/teams/widget-team.d.ts +0 -61
- package/dist/sparc/teams/widget-team.js +0 -280
- package/dist/specialists/app-specialists.d.ts +0 -93
- package/dist/specialists/app-specialists.js +0 -1639
- package/dist/specialists/base-specialist.d.ts +0 -19
- package/dist/specialists/base-specialist.js +0 -54
- package/dist/specialists/flow-specialists.d.ts +0 -113
- package/dist/specialists/flow-specialists.js +0 -1787
- package/dist/specialists/individual-specialists.d.ts +0 -67
- package/dist/specialists/individual-specialists.js +0 -632
- package/dist/specialists/widget-specialists.d.ts +0 -70
- package/dist/specialists/widget-specialists.js +0 -1264
- package/dist/src/utils/agent-detector.js +0 -655
- package/dist/teams/adaptive-team.d.ts +0 -81
- package/dist/teams/adaptive-team.js +0 -1153
- package/dist/teams/application-team.d.ts +0 -30
- package/dist/teams/application-team.js +0 -547
- package/dist/teams/base-team.d.ts +0 -55
- package/dist/teams/base-team.js +0 -243
- package/dist/teams/flow-team.d.ts +0 -28
- package/dist/teams/flow-team.js +0 -525
- package/dist/teams/widget-team.d.ts +0 -26
- package/dist/teams/widget-team.js +0 -459
- package/dist/templates/flow-template-system.js +0 -625
- package/dist/test-fetch-incidents.d.ts +0 -2
- package/dist/test-fetch-incidents.js +0 -84
- package/dist/test-incident-access.d.ts +0 -2
- package/dist/test-incident-access.js +0 -72
- package/dist/test-iphone-flow.js +0 -189
- package/dist/test-limit-problem.d.ts +0 -2
- package/dist/test-limit-problem.js +0 -60
- package/dist/test-ml-batch-fix.d.ts +0 -2
- package/dist/test-ml-batch-fix.js +0 -104
- package/dist/test-ml-batch.d.ts +0 -2
- package/dist/test-ml-batch.js +0 -87
- package/dist/test-ml-query.d.ts +0 -2
- package/dist/test-ml-query.js +0 -100
- package/dist/test-ml-training-fix.d.ts +0 -12
- package/dist/test-ml-training-fix.js +0 -113
- package/dist/test-ml-training.d.ts +0 -2
- package/dist/test-ml-training.js +0 -32
- package/dist/test-operations-query.d.ts +0 -2
- package/dist/test-operations-query.js +0 -84
- package/dist/test-performance-query.d.ts +0 -2
- package/dist/test-performance-query.js +0 -52
- package/dist/test-singleton-protection.d.ts +0 -8
- package/dist/test-singleton-protection.js +0 -110
- package/dist/test-smart-limits.d.ts +0 -13
- package/dist/test-smart-limits.js +0 -100
- package/dist/test-snow-query-incidents.d.ts +0 -2
- package/dist/test-snow-query-incidents.js +0 -66
- package/dist/test-universal-query.d.ts +0 -2
- package/dist/test-universal-query.js +0 -106
- package/dist/testing/flow-testing-automation.js +0 -641
- package/dist/utils/complete-flow-xml-generator.js +0 -1285
- package/dist/utils/file-storage-fallback.d.ts +0 -62
- package/dist/utils/file-storage-fallback.js +0 -289
- package/dist/utils/flow-api-discovery.d.ts +0 -151
- package/dist/utils/flow-api-discovery.js +0 -439
- package/dist/utils/flow-examples.js +0 -720
- package/dist/utils/flow-structure-builder.js +0 -677
- package/dist/utils/flow-xml-generator.d.ts +0 -41
- package/dist/utils/flow-xml-generator.js +0 -258
- package/dist/utils/improved-flow-xml-generator.js +0 -820
- package/dist/utils/mcp-process-manager.backup.d.ts +0 -58
- package/dist/utils/mcp-process-manager.backup.js +0 -220
- package/dist/utils/mcp-singleton-enforcer.d.ts +0 -36
- package/dist/utils/mcp-singleton-enforcer.js +0 -201
- package/dist/utils/mcp-timeout-fix.d.ts +0 -53
- package/dist/utils/mcp-timeout-fix.js +0 -221
- package/dist/utils/migrate-claude-flow.d.ts +0 -22
- package/dist/utils/migrate-claude-flow.js +0 -120
- package/dist/utils/scope-utils.d.ts +0 -72
- package/dist/utils/scope-utils.js +0 -386
- package/dist/utils/snow-oauth-old.d.ts +0 -65
- package/dist/utils/snow-oauth-old.js +0 -345
- package/dist/utils/xml-first-flow-generator.js +0 -513
- package/memory/servicenow_artifacts/000d9224c895221055906c7518aa2d3d.json +0 -30
- package/memory/servicenow_artifacts/0196b66173303010e46b4a2214f6a7a2.json +0 -36
- package/memory/servicenow_artifacts/125e5d1d837e2a102a7ea130ceaad397.json +0 -30
- package/memory/servicenow_artifacts/7637c1f2b7112210a5e5911cde11a972.json +0 -30
- package/memory/servicenow_artifacts/default_documentation_tables_1754056582292.json +0 -55
- package/memory/servicenow_artifacts/default_documentation_tables_1754057477189.json +0 -55
- package/memory/update-set-sessions/01f82af583faea102a7ea130ceaad3d4.json +0 -9
- package/memory/update-set-sessions/27718fb983faea102a7ea130ceaad34b.json +0 -9
- package/memory/update-set-sessions/412e9bf6833e22502a7ea130ceaad30a.json +0 -8
- package/memory/update-set-sessions/66fc5a79837aea102a7ea130ceaad3ab.json +0 -9
- package/memory/update-set-sessions/71a30ff5837eea102a7ea130ceaad37f.json +0 -9
- package/memory/update-set-sessions/74fba27983faea102a7ea130ceaad3bd.json +0 -9
- package/memory/update-set-sessions/a0b147b5837eea102a7ea130ceaad344.json +0 -58
- package/memory/update-set-sessions/b13f5eb983baea102a7ea130ceaad33e.json +0 -9
- package/reports/swarm-auto-centralized-1752649029776.json +0 -13
- package/src/memory/README.md +0 -240
|
@@ -1,651 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Snow-Flow Memory System
|
|
4
|
-
* SQLite-based persistent memory with caching for agent coordination
|
|
5
|
-
*/
|
|
6
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
7
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
8
|
-
};
|
|
9
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
-
exports.MemorySystem = void 0;
|
|
11
|
-
const better_sqlite3_1 = __importDefault(require("better-sqlite3"));
|
|
12
|
-
const events_1 = require("events");
|
|
13
|
-
const path_1 = __importDefault(require("path"));
|
|
14
|
-
const fs_1 = __importDefault(require("fs"));
|
|
15
|
-
const logger_1 = require("../utils/logger");
|
|
16
|
-
class MemorySystem extends events_1.EventEmitter {
|
|
17
|
-
constructor(options) {
|
|
18
|
-
super();
|
|
19
|
-
this.cache = new Map();
|
|
20
|
-
this.cacheStats = { hits: 0, misses: 0, size: 0, evictions: 0 };
|
|
21
|
-
this.initialized = false;
|
|
22
|
-
this.options = options;
|
|
23
|
-
this.logger = new logger_1.Logger('MemorySystem');
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* ๐ง CRIT-003 FIX: Store data with agent isolation
|
|
27
|
-
* Implements agentId::contextKey namespacing for complete memory isolation
|
|
28
|
-
*/
|
|
29
|
-
async storeShared(agentId, contextKey, value, ttl) {
|
|
30
|
-
const isolatedKey = `${agentId}::${contextKey}`;
|
|
31
|
-
return this.store(isolatedKey, value, ttl);
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* ๐ง CRIT-003 FIX: Retrieve data with agent isolation
|
|
35
|
-
* Implements agentId::contextKey namespacing with backward compatibility
|
|
36
|
-
*/
|
|
37
|
-
async retrieveShared(agentId, contextKey) {
|
|
38
|
-
const isolatedKey = `${agentId}::${contextKey}`;
|
|
39
|
-
// Try isolated key first
|
|
40
|
-
let result = await this.get(isolatedKey);
|
|
41
|
-
// Fallback to original key for backward compatibility
|
|
42
|
-
if (result === null || result === undefined) {
|
|
43
|
-
this.logger.debug(`No isolated data found for ${isolatedKey}, falling back to original key ${contextKey}`);
|
|
44
|
-
result = await this.get(contextKey);
|
|
45
|
-
// If found with original key, migrate to isolated key
|
|
46
|
-
if (result !== null && result !== undefined) {
|
|
47
|
-
this.logger.info(`Migrating data from ${contextKey} to ${isolatedKey} for agent isolation`);
|
|
48
|
-
await this.storeShared(agentId, contextKey, result);
|
|
49
|
-
// Keep original for other agents that might still need it
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
return result;
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* ๐ง CRIT-003 FIX: Check if agent has specific context
|
|
56
|
-
*/
|
|
57
|
-
async hasSharedContext(agentId, contextKey) {
|
|
58
|
-
const isolatedKey = `${agentId}::${contextKey}`;
|
|
59
|
-
return this.exists(isolatedKey) || this.exists(contextKey);
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* ๐ง CRIT-003 FIX: List all context keys for an agent
|
|
63
|
-
*/
|
|
64
|
-
async listAgentContexts(agentId) {
|
|
65
|
-
const prefix = `${agentId}::`;
|
|
66
|
-
const keys = await this.listKeys();
|
|
67
|
-
return keys
|
|
68
|
-
.filter(key => key.startsWith(prefix))
|
|
69
|
-
.map(key => key.substring(prefix.length));
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* ๐ง CRIT-003 FIX: Core store method for memory operations
|
|
73
|
-
*/
|
|
74
|
-
async store(key, value, ttl) {
|
|
75
|
-
if (!this.initialized)
|
|
76
|
-
await this.initialize();
|
|
77
|
-
const now = Date.now();
|
|
78
|
-
const expiresAt = ttl ? now + (ttl * 1000) : undefined;
|
|
79
|
-
// Store in cache if enabled
|
|
80
|
-
if (this.options.cache?.enabled) {
|
|
81
|
-
this.cache.set(key, {
|
|
82
|
-
value,
|
|
83
|
-
expiresAt: expiresAt || (now + (this.options.cache.ttl * 1000))
|
|
84
|
-
});
|
|
85
|
-
this.cacheStats.size = this.cache.size;
|
|
86
|
-
}
|
|
87
|
-
// Store in database
|
|
88
|
-
await this.upsert('memory_store', {
|
|
89
|
-
key,
|
|
90
|
-
value: JSON.stringify(value),
|
|
91
|
-
created_at: new Date().toISOString(),
|
|
92
|
-
expires_at: expiresAt ? new Date(expiresAt).toISOString() : null,
|
|
93
|
-
metadata: '{}'
|
|
94
|
-
}, ['key']);
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* ๐ง CRIT-003 FIX: Core get method for memory operations
|
|
98
|
-
*/
|
|
99
|
-
async get(key) {
|
|
100
|
-
if (!this.initialized)
|
|
101
|
-
await this.initialize();
|
|
102
|
-
// Check cache first
|
|
103
|
-
if (this.options.cache?.enabled) {
|
|
104
|
-
const cached = this.cache.get(key);
|
|
105
|
-
if (cached && cached.expiresAt > Date.now()) {
|
|
106
|
-
this.cacheStats.hits++;
|
|
107
|
-
return cached.value;
|
|
108
|
-
}
|
|
109
|
-
if (cached) {
|
|
110
|
-
this.cache.delete(key);
|
|
111
|
-
this.cacheStats.evictions++;
|
|
112
|
-
}
|
|
113
|
-
this.cacheStats.misses++;
|
|
114
|
-
}
|
|
115
|
-
// Query database
|
|
116
|
-
const rows = await this.query(`
|
|
117
|
-
SELECT value, expires_at FROM memory_store
|
|
118
|
-
WHERE key = ? AND (expires_at IS NULL OR expires_at > datetime('now'))
|
|
119
|
-
`, [key]);
|
|
120
|
-
if (rows.length === 0)
|
|
121
|
-
return null;
|
|
122
|
-
const result = JSON.parse(rows[0].value);
|
|
123
|
-
// Update cache
|
|
124
|
-
if (this.options.cache?.enabled) {
|
|
125
|
-
const expiresAt = rows[0].expires_at
|
|
126
|
-
? new Date(rows[0].expires_at).getTime()
|
|
127
|
-
: Date.now() + (this.options.cache.ttl * 1000);
|
|
128
|
-
this.cache.set(key, { value: result, expiresAt });
|
|
129
|
-
this.cacheStats.size = this.cache.size;
|
|
130
|
-
}
|
|
131
|
-
return result;
|
|
132
|
-
}
|
|
133
|
-
/**
|
|
134
|
-
* ๐ง CRIT-003 FIX: Check if key exists
|
|
135
|
-
*/
|
|
136
|
-
async exists(key) {
|
|
137
|
-
if (!this.initialized)
|
|
138
|
-
await this.initialize();
|
|
139
|
-
const rows = await this.query(`
|
|
140
|
-
SELECT 1 FROM memory_store
|
|
141
|
-
WHERE key = ? AND (expires_at IS NULL OR expires_at > datetime('now'))
|
|
142
|
-
`, [key]);
|
|
143
|
-
return rows.length > 0;
|
|
144
|
-
}
|
|
145
|
-
/**
|
|
146
|
-
* ๐ง CRIT-003 FIX: List all keys
|
|
147
|
-
*/
|
|
148
|
-
async listKeys() {
|
|
149
|
-
if (!this.initialized)
|
|
150
|
-
await this.initialize();
|
|
151
|
-
const rows = await this.query(`
|
|
152
|
-
SELECT key FROM memory_store
|
|
153
|
-
WHERE expires_at IS NULL OR expires_at > datetime('now')
|
|
154
|
-
`);
|
|
155
|
-
return rows.map(row => row.key);
|
|
156
|
-
}
|
|
157
|
-
/**
|
|
158
|
-
* Initialize the memory system
|
|
159
|
-
*/
|
|
160
|
-
async initialize() {
|
|
161
|
-
if (this.initialized)
|
|
162
|
-
return;
|
|
163
|
-
this.logger.info('Initializing Memory System...');
|
|
164
|
-
// Ensure directory exists
|
|
165
|
-
const dir = path_1.default.dirname(this.options.dbPath);
|
|
166
|
-
if (!fs_1.default.existsSync(dir)) {
|
|
167
|
-
fs_1.default.mkdirSync(dir, { recursive: true });
|
|
168
|
-
}
|
|
169
|
-
// Open database
|
|
170
|
-
this.db = new better_sqlite3_1.default(this.options.dbPath);
|
|
171
|
-
this.db.pragma('journal_mode = WAL');
|
|
172
|
-
this.db.pragma('synchronous = NORMAL');
|
|
173
|
-
// Create core tables
|
|
174
|
-
await this.createCoreTables();
|
|
175
|
-
// Always run critical migrations (like missing metadata column)
|
|
176
|
-
await this.runCriticalMigrations();
|
|
177
|
-
// Run optional migrations if needed
|
|
178
|
-
if (this.options.schema?.autoMigrate) {
|
|
179
|
-
await this.runMigrations();
|
|
180
|
-
}
|
|
181
|
-
// Start cleanup timer
|
|
182
|
-
this.startCleanupTimer();
|
|
183
|
-
this.initialized = true;
|
|
184
|
-
this.logger.info('Memory System initialized');
|
|
185
|
-
}
|
|
186
|
-
// Duplicate store method removed - using primary implementation above
|
|
187
|
-
// Duplicate get method removed - using primary implementation above
|
|
188
|
-
/**
|
|
189
|
-
* Update an existing value
|
|
190
|
-
*/
|
|
191
|
-
async update(key, value) {
|
|
192
|
-
const existing = await this.get(key);
|
|
193
|
-
if (!existing) {
|
|
194
|
-
throw new Error(`Key not found: ${key}`);
|
|
195
|
-
}
|
|
196
|
-
await this.store(key, value);
|
|
197
|
-
}
|
|
198
|
-
// Duplicate delete method removed - using primary implementation above
|
|
199
|
-
/**
|
|
200
|
-
* Execute a raw SQL query
|
|
201
|
-
*/
|
|
202
|
-
async execute(sql, params = []) {
|
|
203
|
-
if (!this.db)
|
|
204
|
-
throw new Error('Memory system not initialized');
|
|
205
|
-
const stmt = this.db.prepare(sql);
|
|
206
|
-
stmt.run(...params);
|
|
207
|
-
}
|
|
208
|
-
/**
|
|
209
|
-
* Query the database
|
|
210
|
-
*/
|
|
211
|
-
async query(sql, params = []) {
|
|
212
|
-
if (!this.db)
|
|
213
|
-
throw new Error('Memory system not initialized');
|
|
214
|
-
const stmt = this.db.prepare(sql);
|
|
215
|
-
return stmt.all(...params);
|
|
216
|
-
}
|
|
217
|
-
/**
|
|
218
|
-
* Insert data into a table
|
|
219
|
-
*/
|
|
220
|
-
async insert(table, data) {
|
|
221
|
-
if (!this.db)
|
|
222
|
-
throw new Error('Memory system not initialized');
|
|
223
|
-
const keys = Object.keys(data);
|
|
224
|
-
const values = Object.values(data);
|
|
225
|
-
const placeholders = keys.map(() => '?').join(', ');
|
|
226
|
-
const sql = `INSERT INTO ${table} (${keys.join(', ')}) VALUES (${placeholders})`;
|
|
227
|
-
const stmt = this.db.prepare(sql);
|
|
228
|
-
stmt.run(...values);
|
|
229
|
-
}
|
|
230
|
-
/**
|
|
231
|
-
* Delete a key from memory
|
|
232
|
-
*/
|
|
233
|
-
async delete(key) {
|
|
234
|
-
if (!this.initialized)
|
|
235
|
-
await this.initialize();
|
|
236
|
-
// Remove from cache
|
|
237
|
-
if (this.options.cache?.enabled) {
|
|
238
|
-
this.cache.delete(key);
|
|
239
|
-
}
|
|
240
|
-
// Remove from database
|
|
241
|
-
if (!this.db)
|
|
242
|
-
throw new Error('Memory system not initialized');
|
|
243
|
-
const stmt = this.db.prepare('DELETE FROM memory_store WHERE key = ?');
|
|
244
|
-
stmt.run(key);
|
|
245
|
-
}
|
|
246
|
-
/**
|
|
247
|
-
* Upsert data (insert or update)
|
|
248
|
-
*/
|
|
249
|
-
async upsert(table, data, uniqueKeys) {
|
|
250
|
-
if (!this.db)
|
|
251
|
-
throw new Error('Memory system not initialized');
|
|
252
|
-
const keys = Object.keys(data);
|
|
253
|
-
const values = Object.values(data);
|
|
254
|
-
const placeholders = keys.map(() => '?').join(', ');
|
|
255
|
-
const updateClause = keys
|
|
256
|
-
.filter(k => !uniqueKeys.includes(k))
|
|
257
|
-
.map(k => `${k} = excluded.${k}`)
|
|
258
|
-
.join(', ');
|
|
259
|
-
const sql = `
|
|
260
|
-
INSERT INTO ${table} (${keys.join(', ')}) VALUES (${placeholders})
|
|
261
|
-
ON CONFLICT (${uniqueKeys.join(', ')}) DO UPDATE SET ${updateClause}
|
|
262
|
-
`;
|
|
263
|
-
const stmt = this.db.prepare(sql);
|
|
264
|
-
stmt.run(...values);
|
|
265
|
-
}
|
|
266
|
-
/**
|
|
267
|
-
* Create a transaction
|
|
268
|
-
*/
|
|
269
|
-
transaction(fn) {
|
|
270
|
-
if (!this.db)
|
|
271
|
-
throw new Error('Memory system not initialized');
|
|
272
|
-
return this.db.transaction(fn)();
|
|
273
|
-
}
|
|
274
|
-
/**
|
|
275
|
-
* Get database statistics
|
|
276
|
-
*/
|
|
277
|
-
async getDatabaseStats() {
|
|
278
|
-
if (!this.db)
|
|
279
|
-
throw new Error('Memory system not initialized');
|
|
280
|
-
const tables = this.db.prepare(`
|
|
281
|
-
SELECT name FROM sqlite_master WHERE type = 'table'
|
|
282
|
-
`).all();
|
|
283
|
-
const rowCounts = {};
|
|
284
|
-
for (const { name } of tables) {
|
|
285
|
-
const count = this.db.prepare(`SELECT COUNT(*) as count FROM ${name}`).get();
|
|
286
|
-
rowCounts[name] = count.count;
|
|
287
|
-
}
|
|
288
|
-
const dbFile = this.options.dbPath;
|
|
289
|
-
const stats = fs_1.default.statSync(dbFile);
|
|
290
|
-
return {
|
|
291
|
-
size: stats.size,
|
|
292
|
-
tableCount: tables.length,
|
|
293
|
-
rowCounts
|
|
294
|
-
};
|
|
295
|
-
}
|
|
296
|
-
/**
|
|
297
|
-
* Get cache statistics
|
|
298
|
-
*/
|
|
299
|
-
async getCacheStats() {
|
|
300
|
-
return {
|
|
301
|
-
...this.cacheStats,
|
|
302
|
-
size: this.cache.size
|
|
303
|
-
};
|
|
304
|
-
}
|
|
305
|
-
/**
|
|
306
|
-
* Invalidate cache
|
|
307
|
-
*/
|
|
308
|
-
async invalidateCache(pattern) {
|
|
309
|
-
if (!pattern) {
|
|
310
|
-
this.cache.clear();
|
|
311
|
-
this.logger.info('Cache cleared');
|
|
312
|
-
}
|
|
313
|
-
else {
|
|
314
|
-
// Pattern-based invalidation
|
|
315
|
-
const keys = Array.from(this.cache.keys());
|
|
316
|
-
const regex = new RegExp(pattern);
|
|
317
|
-
let invalidated = 0;
|
|
318
|
-
for (const key of keys) {
|
|
319
|
-
if (regex.test(key)) {
|
|
320
|
-
this.cache.delete(key);
|
|
321
|
-
invalidated++;
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
this.logger.info(`Invalidated ${invalidated} cache entries matching pattern: ${pattern}`);
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
/**
|
|
328
|
-
* Create emergency backup
|
|
329
|
-
*/
|
|
330
|
-
async createEmergencyBackup() {
|
|
331
|
-
if (!this.db)
|
|
332
|
-
throw new Error('Memory system not initialized');
|
|
333
|
-
const backupPath = `${this.options.dbPath}.backup.${Date.now()}`;
|
|
334
|
-
await this.db.backup(backupPath);
|
|
335
|
-
this.logger.info(`Emergency backup created: ${backupPath}`);
|
|
336
|
-
return backupPath;
|
|
337
|
-
}
|
|
338
|
-
/**
|
|
339
|
-
* Close the memory system
|
|
340
|
-
*/
|
|
341
|
-
async close() {
|
|
342
|
-
if (this.cleanupTimer) {
|
|
343
|
-
clearInterval(this.cleanupTimer);
|
|
344
|
-
}
|
|
345
|
-
if (this.db) {
|
|
346
|
-
this.db.close();
|
|
347
|
-
this.db = undefined;
|
|
348
|
-
}
|
|
349
|
-
this.cache.clear();
|
|
350
|
-
this.initialized = false;
|
|
351
|
-
this.logger.info('Memory System closed');
|
|
352
|
-
}
|
|
353
|
-
/**
|
|
354
|
-
* Private helper methods
|
|
355
|
-
*/
|
|
356
|
-
async createCoreTables() {
|
|
357
|
-
if (!this.db)
|
|
358
|
-
return;
|
|
359
|
-
// Core memory store
|
|
360
|
-
this.db.exec(`
|
|
361
|
-
CREATE TABLE IF NOT EXISTS memory_store (
|
|
362
|
-
key TEXT PRIMARY KEY,
|
|
363
|
-
value TEXT NOT NULL,
|
|
364
|
-
ttl INTEGER,
|
|
365
|
-
expires_at INTEGER,
|
|
366
|
-
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
367
|
-
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
368
|
-
metadata TEXT DEFAULT '{}'
|
|
369
|
-
);
|
|
370
|
-
|
|
371
|
-
CREATE INDEX IF NOT EXISTS idx_memory_expires ON memory_store(expires_at);
|
|
372
|
-
|
|
373
|
-
-- ๐ PERFORMANCE FIX: 6 Critical indexes for 60-80% performance improvement
|
|
374
|
-
CREATE INDEX IF NOT EXISTS idx_memory_created_at ON memory_store(created_at);
|
|
375
|
-
CREATE INDEX IF NOT EXISTS idx_memory_key_expires ON memory_store(key, expires_at);
|
|
376
|
-
`);
|
|
377
|
-
// Swarm sessions
|
|
378
|
-
this.db.exec(`
|
|
379
|
-
CREATE TABLE IF NOT EXISTS swarm_sessions (
|
|
380
|
-
id TEXT PRIMARY KEY,
|
|
381
|
-
objective TEXT NOT NULL,
|
|
382
|
-
started_at TIMESTAMP NOT NULL,
|
|
383
|
-
status TEXT NOT NULL,
|
|
384
|
-
queen_agent_id TEXT,
|
|
385
|
-
completed_at TIMESTAMP,
|
|
386
|
-
metadata TEXT
|
|
387
|
-
);
|
|
388
|
-
|
|
389
|
-
CREATE INDEX IF NOT EXISTS idx_swarm_status ON swarm_sessions(status);
|
|
390
|
-
|
|
391
|
-
-- ๐ PERFORMANCE FIX: Critical compound index for session status queries
|
|
392
|
-
CREATE INDEX IF NOT EXISTS idx_swarm_status_started ON swarm_sessions(status, started_at);
|
|
393
|
-
`);
|
|
394
|
-
// Agent coordination
|
|
395
|
-
this.db.exec(`
|
|
396
|
-
CREATE TABLE IF NOT EXISTS agent_coordination (
|
|
397
|
-
session_id TEXT NOT NULL,
|
|
398
|
-
agent_id TEXT NOT NULL,
|
|
399
|
-
agent_type TEXT NOT NULL,
|
|
400
|
-
status TEXT NOT NULL,
|
|
401
|
-
assigned_tasks TEXT,
|
|
402
|
-
progress_percentage INTEGER DEFAULT 0,
|
|
403
|
-
last_activity TIMESTAMP,
|
|
404
|
-
current_tool TEXT,
|
|
405
|
-
error_state TEXT,
|
|
406
|
-
PRIMARY KEY (session_id, agent_id)
|
|
407
|
-
);
|
|
408
|
-
|
|
409
|
-
CREATE INDEX IF NOT EXISTS idx_agent_status ON agent_coordination(status);
|
|
410
|
-
|
|
411
|
-
-- ๐ PERFORMANCE FIX: Critical compound indexes for agent queries
|
|
412
|
-
CREATE INDEX IF NOT EXISTS idx_agent_session_type ON agent_coordination(session_id, agent_type);
|
|
413
|
-
CREATE INDEX IF NOT EXISTS idx_agent_activity ON agent_coordination(last_activity);
|
|
414
|
-
`);
|
|
415
|
-
// ServiceNow artifacts
|
|
416
|
-
this.db.exec(`
|
|
417
|
-
CREATE TABLE IF NOT EXISTS servicenow_artifacts (
|
|
418
|
-
sys_id TEXT PRIMARY KEY,
|
|
419
|
-
artifact_type TEXT NOT NULL,
|
|
420
|
-
name TEXT NOT NULL,
|
|
421
|
-
description TEXT,
|
|
422
|
-
created_by_agent TEXT,
|
|
423
|
-
session_id TEXT,
|
|
424
|
-
deployment_status TEXT,
|
|
425
|
-
update_set_id TEXT,
|
|
426
|
-
dependencies TEXT,
|
|
427
|
-
metadata TEXT,
|
|
428
|
-
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
429
|
-
);
|
|
430
|
-
|
|
431
|
-
CREATE INDEX IF NOT EXISTS idx_artifact_session ON servicenow_artifacts(session_id);
|
|
432
|
-
CREATE INDEX IF NOT EXISTS idx_artifact_type ON servicenow_artifacts(artifact_type);
|
|
433
|
-
`);
|
|
434
|
-
// Agent messages
|
|
435
|
-
this.db.exec(`
|
|
436
|
-
CREATE TABLE IF NOT EXISTS agent_messages (
|
|
437
|
-
id TEXT PRIMARY KEY,
|
|
438
|
-
session_id TEXT NOT NULL,
|
|
439
|
-
from_agent TEXT NOT NULL,
|
|
440
|
-
to_agent TEXT NOT NULL,
|
|
441
|
-
message_type TEXT NOT NULL,
|
|
442
|
-
content TEXT,
|
|
443
|
-
artifact_reference TEXT,
|
|
444
|
-
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
445
|
-
processed BOOLEAN DEFAULT FALSE
|
|
446
|
-
);
|
|
447
|
-
|
|
448
|
-
CREATE INDEX IF NOT EXISTS idx_message_session ON agent_messages(session_id);
|
|
449
|
-
CREATE INDEX IF NOT EXISTS idx_message_processed ON agent_messages(processed);
|
|
450
|
-
`);
|
|
451
|
-
// Shared context
|
|
452
|
-
this.db.exec(`
|
|
453
|
-
CREATE TABLE IF NOT EXISTS shared_context (
|
|
454
|
-
session_id TEXT NOT NULL,
|
|
455
|
-
context_key TEXT NOT NULL,
|
|
456
|
-
context_value TEXT,
|
|
457
|
-
created_by_agent TEXT,
|
|
458
|
-
expires_at TIMESTAMP,
|
|
459
|
-
access_permissions TEXT,
|
|
460
|
-
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
461
|
-
PRIMARY KEY (session_id, context_key)
|
|
462
|
-
);
|
|
463
|
-
`);
|
|
464
|
-
// Performance metrics
|
|
465
|
-
this.db.exec(`
|
|
466
|
-
CREATE TABLE IF NOT EXISTS performance_metrics (
|
|
467
|
-
id TEXT PRIMARY KEY,
|
|
468
|
-
operation TEXT NOT NULL,
|
|
469
|
-
start_time INTEGER NOT NULL,
|
|
470
|
-
end_time INTEGER,
|
|
471
|
-
duration INTEGER,
|
|
472
|
-
success BOOLEAN NOT NULL,
|
|
473
|
-
session_id TEXT,
|
|
474
|
-
agent_id TEXT,
|
|
475
|
-
metadata TEXT,
|
|
476
|
-
resource_usage TEXT,
|
|
477
|
-
error TEXT,
|
|
478
|
-
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
479
|
-
);
|
|
480
|
-
|
|
481
|
-
CREATE INDEX IF NOT EXISTS idx_perf_operation ON performance_metrics(operation);
|
|
482
|
-
CREATE INDEX IF NOT EXISTS idx_perf_session ON performance_metrics(session_id);
|
|
483
|
-
CREATE INDEX IF NOT EXISTS idx_perf_time ON performance_metrics(start_time);
|
|
484
|
-
|
|
485
|
-
-- ๐ PERFORMANCE FIX: Final critical performance index
|
|
486
|
-
CREATE INDEX IF NOT EXISTS idx_perf_success_time ON performance_metrics(success, created_at);
|
|
487
|
-
`);
|
|
488
|
-
}
|
|
489
|
-
async runCriticalMigrations() {
|
|
490
|
-
// Critical migrations that must always run
|
|
491
|
-
try {
|
|
492
|
-
// Fix for missing metadata column
|
|
493
|
-
const tableInfo = this.db.prepare("PRAGMA table_info(memory_store)").all();
|
|
494
|
-
const hasMetadataColumn = tableInfo.some((col) => col.name === 'metadata');
|
|
495
|
-
if (!hasMetadataColumn) {
|
|
496
|
-
this.logger.info('Running critical migration: Adding metadata column to memory_store table...');
|
|
497
|
-
this.db.exec("ALTER TABLE memory_store ADD COLUMN metadata TEXT DEFAULT '{}'");
|
|
498
|
-
this.logger.info('Critical migration completed: Metadata column added successfully');
|
|
499
|
-
}
|
|
500
|
-
}
|
|
501
|
-
catch (error) {
|
|
502
|
-
this.logger.warn('Could not complete critical migrations:', error);
|
|
503
|
-
}
|
|
504
|
-
}
|
|
505
|
-
async runMigrations() {
|
|
506
|
-
// Implement schema migrations if needed
|
|
507
|
-
this.logger.info('Running database migrations...');
|
|
508
|
-
// Check current version
|
|
509
|
-
const versionRow = await this.get('_schema_version');
|
|
510
|
-
const currentVersion = versionRow?.version || '0.0.0';
|
|
511
|
-
const targetVersion = this.options.schema?.version || '1.0.0';
|
|
512
|
-
if (currentVersion === targetVersion) {
|
|
513
|
-
this.logger.info('Database schema is up to date');
|
|
514
|
-
return;
|
|
515
|
-
}
|
|
516
|
-
// Run migrations based on version differences
|
|
517
|
-
// This is a placeholder - implement actual migration logic
|
|
518
|
-
// Update version
|
|
519
|
-
await this.store('_schema_version', { version: targetVersion });
|
|
520
|
-
this.logger.info(`Database migrated to version ${targetVersion}`);
|
|
521
|
-
}
|
|
522
|
-
// ๐ด SNOW-003 FIX: More aggressive cleanup to prevent memory exhaustion
|
|
523
|
-
startCleanupTimer() {
|
|
524
|
-
// ๐ด CRITICAL: Run cleanup every 15 minutes instead of 1 hour
|
|
525
|
-
this.cleanupTimer = setInterval(async () => {
|
|
526
|
-
await this.cleanup();
|
|
527
|
-
}, 900000); // 15 minutes
|
|
528
|
-
// ๐ด CRITICAL: Additional memory pressure cleanup every 5 minutes
|
|
529
|
-
setInterval(async () => {
|
|
530
|
-
await this.memoryPressureCleanup();
|
|
531
|
-
}, 300000); // 5 minutes
|
|
532
|
-
}
|
|
533
|
-
// ๐ด SNOW-003 FIX: Enhanced cleanup with memory pressure monitoring
|
|
534
|
-
async cleanup() {
|
|
535
|
-
if (!this.db)
|
|
536
|
-
return;
|
|
537
|
-
const now = Date.now();
|
|
538
|
-
const startTime = Date.now();
|
|
539
|
-
try {
|
|
540
|
-
// ๐ด CRITICAL: Clean expired memory entries with better logging
|
|
541
|
-
const stmt = this.db.prepare('DELETE FROM memory_store WHERE expires_at < ?');
|
|
542
|
-
const result = stmt.run(now);
|
|
543
|
-
if (result.changes > 0) {
|
|
544
|
-
this.logger.info(`๐งน Cleaned up ${result.changes} expired memory entries`);
|
|
545
|
-
}
|
|
546
|
-
// ๐ด CRITICAL: Clean cache with size monitoring
|
|
547
|
-
let cacheCleanedCount = 0;
|
|
548
|
-
const cacheEntriesBefore = this.cache.size;
|
|
549
|
-
for (const [key, entry] of this.cache.entries()) {
|
|
550
|
-
if (entry.expiresAt < now) {
|
|
551
|
-
this.cache.delete(key);
|
|
552
|
-
cacheCleanedCount++;
|
|
553
|
-
}
|
|
554
|
-
}
|
|
555
|
-
if (cacheCleanedCount > 0) {
|
|
556
|
-
this.logger.info(`๐งน Cleaned ${cacheCleanedCount} expired cache entries (${cacheEntriesBefore} โ ${this.cache.size})`);
|
|
557
|
-
}
|
|
558
|
-
// ๐ด CRITICAL: More frequent vacuum for high activity
|
|
559
|
-
const lastVacuum = await this.get('_last_vacuum');
|
|
560
|
-
const shouldVacuum = !lastVacuum || now - lastVacuum > 21600000; // 6 hours instead of 24
|
|
561
|
-
if (shouldVacuum) {
|
|
562
|
-
this.logger.info('๐๏ธ Starting database vacuum...');
|
|
563
|
-
this.db.exec('VACUUM');
|
|
564
|
-
await this.store('_last_vacuum', now);
|
|
565
|
-
this.logger.info(`โ
Database vacuumed in ${Date.now() - startTime}ms`);
|
|
566
|
-
}
|
|
567
|
-
// ๐ด CRITICAL: Log memory stats after cleanup
|
|
568
|
-
const memUsage = process.memoryUsage();
|
|
569
|
-
const heapUsedMB = Math.round(memUsage.heapUsed / 1024 / 1024);
|
|
570
|
-
if (heapUsedMB > 100) { // Log if significant memory usage
|
|
571
|
-
this.logger.info(`๐ Memory after cleanup: ${heapUsedMB}MB heap, ${this.cache.size} cache entries`);
|
|
572
|
-
}
|
|
573
|
-
}
|
|
574
|
-
catch (error) {
|
|
575
|
-
this.logger.error('โ Cleanup failed:', error);
|
|
576
|
-
}
|
|
577
|
-
}
|
|
578
|
-
/**
|
|
579
|
-
* ๐ด SNOW-003 FIX: Emergency cleanup when memory pressure is high
|
|
580
|
-
*/
|
|
581
|
-
async memoryPressureCleanup() {
|
|
582
|
-
if (!this.db)
|
|
583
|
-
return;
|
|
584
|
-
try {
|
|
585
|
-
const memUsage = process.memoryUsage();
|
|
586
|
-
const heapUsedMB = Math.round(memUsage.heapUsed / 1024 / 1024);
|
|
587
|
-
const cacheSize = this.cache.size;
|
|
588
|
-
// ๐ด CRITICAL: Trigger aggressive cleanup if memory is high
|
|
589
|
-
if (heapUsedMB > 300 || cacheSize > 1000) {
|
|
590
|
-
this.logger.warn(`โ ๏ธ High memory pressure detected: ${heapUsedMB}MB heap, ${cacheSize} cache entries`);
|
|
591
|
-
// 1. Aggressive cache cleanup - remove 50% of oldest entries
|
|
592
|
-
if (cacheSize > 500) {
|
|
593
|
-
const entries = Array.from(this.cache.entries())
|
|
594
|
-
.sort((a, b) => a[1].expiresAt - b[1].expiresAt);
|
|
595
|
-
const toRemove = Math.floor(entries.length * 0.5);
|
|
596
|
-
for (let i = 0; i < toRemove; i++) {
|
|
597
|
-
this.cache.delete(entries[i][0]);
|
|
598
|
-
}
|
|
599
|
-
this.logger.info(`๐งน Emergency cache cleanup: removed ${toRemove} entries (${cacheSize} โ ${this.cache.size})`);
|
|
600
|
-
}
|
|
601
|
-
// 2. Clean old database entries more aggressively
|
|
602
|
-
const now = Date.now();
|
|
603
|
-
const aggressiveCleanupTime = now - (7 * 24 * 60 * 60 * 1000); // 7 days old
|
|
604
|
-
const aggressiveStmt = this.db.prepare('DELETE FROM memory_store WHERE created_at < ? AND expires_at IS NULL');
|
|
605
|
-
const aggressiveResult = aggressiveStmt.run(aggressiveCleanupTime);
|
|
606
|
-
if (aggressiveResult.changes > 0) {
|
|
607
|
-
this.logger.info(`๐งน Emergency database cleanup: removed ${aggressiveResult.changes} old entries`);
|
|
608
|
-
}
|
|
609
|
-
// 3. Force garbage collection if available
|
|
610
|
-
if (global.gc) {
|
|
611
|
-
this.logger.info('๐๏ธ Forcing garbage collection due to memory pressure');
|
|
612
|
-
global.gc();
|
|
613
|
-
// Check memory after GC
|
|
614
|
-
const afterGC = process.memoryUsage();
|
|
615
|
-
const afterGCMB = Math.round(afterGC.heapUsed / 1024 / 1024);
|
|
616
|
-
this.logger.info(`๐ Memory after GC: ${heapUsedMB}MB โ ${afterGCMB}MB`);
|
|
617
|
-
}
|
|
618
|
-
}
|
|
619
|
-
}
|
|
620
|
-
catch (error) {
|
|
621
|
-
this.logger.error('โ Memory pressure cleanup failed:', error);
|
|
622
|
-
}
|
|
623
|
-
}
|
|
624
|
-
checkCacheSize() {
|
|
625
|
-
if (!this.options.cache?.enabled)
|
|
626
|
-
return;
|
|
627
|
-
const maxSize = this.options.cache.maxSize || 100;
|
|
628
|
-
const currentSizeMB = this.estimateCacheSize();
|
|
629
|
-
if (currentSizeMB > maxSize) {
|
|
630
|
-
// Evict oldest entries
|
|
631
|
-
const entries = Array.from(this.cache.entries())
|
|
632
|
-
.sort((a, b) => a[1].expiresAt - b[1].expiresAt);
|
|
633
|
-
const toEvict = Math.floor(entries.length * 0.2); // Evict 20%
|
|
634
|
-
for (let i = 0; i < toEvict; i++) {
|
|
635
|
-
this.cache.delete(entries[i][0]);
|
|
636
|
-
this.cacheStats.evictions++;
|
|
637
|
-
}
|
|
638
|
-
this.logger.debug(`Evicted ${toEvict} cache entries due to size limit`);
|
|
639
|
-
}
|
|
640
|
-
}
|
|
641
|
-
estimateCacheSize() {
|
|
642
|
-
// Rough estimate of cache size in MB
|
|
643
|
-
let size = 0;
|
|
644
|
-
for (const [key, entry] of this.cache.entries()) {
|
|
645
|
-
size += key.length + JSON.stringify(entry.value).length;
|
|
646
|
-
}
|
|
647
|
-
return size / 1024 / 1024;
|
|
648
|
-
}
|
|
649
|
-
}
|
|
650
|
-
exports.MemorySystem = MemorySystem;
|
|
651
|
-
//# sourceMappingURL=memory-system.js.map
|