snow-flow 2.0.2 → 2.0.3
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/mcp-config.json → .mcp.json} +13 -12
- package/.releaserc.json +47 -0
- package/README.md +6 -0
- package/package.json +1 -1
- package/.claude/claude-settings.json +0 -15
- package/.claude/commands/analysis/README.md +0 -9
- package/.claude/commands/automation/README.md +0 -9
- package/.claude/commands/github/README.md +0 -11
- package/.claude/commands/helpers/standard-checkpoint-hooks.sh +0 -179
- package/.claude/commands/hooks/README.md +0 -11
- package/.claude/commands/monitoring/README.md +0 -9
- package/.claude/commands/optimization/README.md +0 -9
- package/.claude/commands/training/README.md +0 -9
- package/.claude/commands/workflows/README.md +0 -9
- package/.claude/config.json +0 -36
- package/.claude/helpers/github-setup.sh +0 -28
- package/.claude/helpers/quick-start.sh +0 -19
- package/.claude/helpers/setup-mcp.sh +0 -18
- package/.claude/settings.json +0 -114
- package/.claude/settings.local.json +0 -33
- package/.claude-flow/metrics/agent-metrics.json +0 -1
- package/.claude-flow/metrics/performance.json +0 -9
- package/.claude-flow/metrics/system-metrics.json +0 -15986
- package/.claude-flow/metrics/task-metrics.json +0 -10
- package/.roo/README.md +0 -402
- package/.roo/mcp-list.txt +0 -257
- package/.roo/mcp.json +0 -32
- package/.roo/workflows/basic-tdd.json +0 -32
- package/.roomodes +0 -201
- package/CLAUDE.md +0 -393
- package/bin/snow-flow.js +0 -3
- package/dist/cli.js.bak +0 -2052
- package/dist/config/snow-flow-config.d.ts +0 -1492
- package/dist/config/snow-flow-config.js +0 -938
- package/scripts/create-env.js +0 -91
- package/scripts/postinstall.js +0 -30
- package/scripts/register-mcp-servers.js +0 -210
- package/scripts/reset-mcp-servers.js +0 -246
- package/scripts/setup-mcp.js +0 -106
- package/scripts/update-version.js +0 -31
- package/src/agents/README.md +0 -192
- package/src/agents/base-agent.ts +0 -251
- package/src/agents/coordinator.ts +0 -971
- package/src/agents/index.ts +0 -18
- package/src/agents/queen-403-handler.ts +0 -230
- package/src/agents/queen-agent.ts +0 -1399
- package/src/agents/script-writer-agent.ts +0 -769
- package/src/agents/security-agent.ts +0 -726
- package/src/agents/widget-creator-agent.ts +0 -568
- package/src/api/error-handling.ts +0 -1758
- package/src/api/natural-language-mapper.ts +0 -630
- package/src/api/performance-optimizer.ts +0 -891
- package/src/api/transaction-manager.ts +0 -754
- package/src/cli/deploy-artifact.ts +0 -304
- package/src/cli/snow-flow-cli-integration.ts +0 -434
- package/src/cli.ts +0 -4455
- package/src/cli.ts.bak +0 -5616
- package/src/compliance/advanced-compliance-system.ts +0 -1268
- package/src/compliance/index.ts +0 -19
- package/src/config/snow-flow-config.ts +0 -1019
- package/src/documentation/index.ts +0 -16
- package/src/documentation/self-documenting-system.ts +0 -1421
- package/src/dynamic-version.ts +0 -44
- package/src/healing/index.ts +0 -20
- package/src/healing/self-healing-system.ts +0 -1435
- package/src/health/README.md +0 -161
- package/src/health/system-health.ts +0 -995
- package/src/index.ts +0 -60
- package/src/intelligence/acl-analyzer.ts +0 -377
- package/src/intelligence/auto-resolution-engine.ts +0 -643
- package/src/intelligence/gap-analysis-engine.ts +0 -488
- package/src/intelligence/manual-instructions-generator.ts +0 -744
- package/src/intelligence/mcp-coverage-analyzer.ts +0 -708
- package/src/intelligence/multi-pass-requirements-analyzer.ts +0 -603
- package/src/intelligence/performance-recommendations-engine.ts +0 -1223
- package/src/intelligence/requirements-analyzer.ts +0 -1148
- package/src/intelligence/task-analyzer.ts +0 -628
- package/src/managers/scope-manager.ts +0 -555
- package/src/mcp/advanced/servicenow-advanced-features-mcp.ts +0 -16110
- package/src/mcp/base-mcp-server.ts +0 -660
- package/src/mcp/http-transport-wrapper.ts +0 -426
- package/src/mcp/service-discovery-client.ts +0 -312
- package/src/mcp/servicenow-automation-mcp-refactored.ts +0 -678
- package/src/mcp/servicenow-automation-mcp.ts +0 -838
- package/src/mcp/servicenow-deployment-mcp-refactored.ts +0 -1292
- package/src/mcp/servicenow-deployment-mcp.ts +0 -7244
- package/src/mcp/servicenow-graph-memory-mcp-refactored.ts +0 -721
- package/src/mcp/servicenow-graph-memory-mcp.ts +0 -832
- package/src/mcp/servicenow-integration-mcp-refactored.ts +0 -655
- package/src/mcp/servicenow-integration-mcp.ts +0 -819
- package/src/mcp/servicenow-intelligent-mcp-refactored.ts +0 -977
- package/src/mcp/servicenow-intelligent-mcp.ts +0 -4638
- package/src/mcp/servicenow-mcp-server.ts +0 -728
- package/src/mcp/servicenow-memory-mcp.ts +0 -474
- package/src/mcp/servicenow-operations-mcp-refactored.ts +0 -1995
- package/src/mcp/servicenow-operations-mcp.ts +0 -3384
- package/src/mcp/servicenow-platform-development-mcp-refactored.ts +0 -611
- package/src/mcp/servicenow-platform-development-mcp.ts +0 -970
- package/src/mcp/servicenow-progressive-indexer.ts +0 -370
- package/src/mcp/servicenow-reporting-analytics-mcp-refactored.ts +0 -695
- package/src/mcp/servicenow-reporting-analytics-mcp.ts +0 -1123
- package/src/mcp/servicenow-security-compliance-mcp-refactored.ts +0 -642
- package/src/mcp/servicenow-security-compliance-mcp.ts +0 -1157
- package/src/mcp/servicenow-update-set-mcp-refactored.ts +0 -751
- package/src/mcp/servicenow-update-set-mcp.ts +0 -811
- package/src/mcp/shared/agent-context-provider.ts +0 -365
- package/src/mcp/shared/base-mcp-server.ts +0 -2463
- package/src/mcp/shared/mcp-memory-manager.ts +0 -540
- package/src/mcp/shared/mcp-resource-manager.ts +0 -367
- package/src/mcp/snow-flow-mcp.ts +0 -911
- package/src/mcp/start-all-mcp-servers.ts +0 -102
- package/src/mcp/start-servicenow-mcp.ts +0 -67
- package/src/monitoring/enhanced-monitoring-system.ts +0 -1345
- package/src/monitoring/performance-tracker.ts +0 -777
- package/src/optimization/cost-optimization-engine.ts +0 -1125
- package/src/optimization/index.ts +0 -10
- package/src/queen/README.md +0 -403
- package/src/queen/agent-factory.ts +0 -574
- package/src/queen/index.ts +0 -94
- package/src/queen/mcp-execution-bridge.ts +0 -681
- package/src/queen/neural-learning.ts +0 -319
- package/src/queen/parallel-agent-engine.ts +0 -848
- package/src/queen/queen-memory-system.ts +0 -490
- package/src/queen/queen-memory.ts +0 -456
- package/src/queen/servicenow-queen.ts +0 -1029
- package/src/queen/types.ts +0 -87
- package/src/rollback/smart-rollback-system.ts +0 -866
- package/src/schemas/deployment.schema.json +0 -58
- package/src/schemas/flow.schema.json +0 -79
- package/src/schemas/widget.schema.json +0 -72
- package/src/snow-flow-system.ts +0 -541
- package/src/sparc/sparc-help.ts +0 -49
- package/src/sparc/team-sparc.ts +0 -88
- package/src/strategies/global-scope-strategy.ts +0 -736
- package/src/templates/base/application.template.json +0 -45
- package/src/templates/base/business_rule.template.json +0 -33
- package/src/templates/base/script_include.template.json +0 -18
- package/src/templates/base/table.template.json +0 -64
- package/src/templates/base/widget.template.json +0 -25
- package/src/templates/patterns/composite.incident-management.template.json +0 -140
- package/src/templates/patterns/widget.dashboard.template.json +0 -238
- package/src/templates/patterns/widget.datatable.template.json +0 -292
- package/src/testing/integration-test-suite.ts +0 -1276
- package/src/types/index.ts +0 -183
- package/src/types/neo4j-driver.d.ts +0 -23
- package/src/types/servicenow.types.ts +0 -307
- package/src/types/snow-flow.types.ts +0 -186
- package/src/types/todo.types.ts +0 -70
- package/src/utils/action-type-cache.ts +0 -286
- package/src/utils/agent-detector.ts +0 -573
- package/src/utils/artifact-tracker.ts +0 -379
- package/src/utils/dependency-detector.ts +0 -233
- package/src/utils/deployment-metadata-handler.ts +0 -518
- package/src/utils/error-recovery.ts +0 -680
- package/src/utils/logger.ts +0 -60
- package/src/utils/mcp-auth-middleware.ts +0 -262
- package/src/utils/mcp-config-manager.ts +0 -270
- package/src/utils/mcp-server-manager.ts +0 -446
- package/src/utils/mcp-tool-registry.ts +0 -298
- package/src/utils/migrate-snow-flow.ts +0 -98
- package/src/utils/oauth-html-templates.ts +0 -188
- package/src/utils/scope-utils.ts +0 -443
- package/src/utils/servicenow-client.ts +0 -3321
- package/src/utils/servicenow-id-generator.ts +0 -181
- package/src/utils/snow-memory-manager.ts +0 -605
- package/src/utils/snow-oauth.ts +0 -797
- package/src/utils/template-engine.ts +0 -463
- package/src/utils/theme-manager.ts +0 -295
- package/src/utils/unified-auth-store.ts +0 -222
- package/src/utils/update-set-importer.ts +0 -422
- package/src/utils/update-set-xml-packager.ts +0 -371
- package/src/utils/widget-template-generator.ts +0 -1744
- package/src/version.ts +0 -1950
|
@@ -1,605 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
3
|
-
* ServiceNow Memory Manager
|
|
4
|
-
* Integrates with Snow-Flow memory caching system for persistent data storage
|
|
5
|
-
* and cross-session coordination for the Advanced Features MCP Server
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import * as fs from 'fs/promises';
|
|
9
|
-
import * as path from 'path';
|
|
10
|
-
import { Logger } from './logger.js';
|
|
11
|
-
|
|
12
|
-
export interface MemoryEntry {
|
|
13
|
-
key: string;
|
|
14
|
-
data: any;
|
|
15
|
-
timestamp: number;
|
|
16
|
-
ttl: number; // Time to live in milliseconds
|
|
17
|
-
tags?: string[];
|
|
18
|
-
metadata?: {
|
|
19
|
-
creator: string;
|
|
20
|
-
version: string;
|
|
21
|
-
description?: string;
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export interface MemoryStats {
|
|
26
|
-
totalEntries: number;
|
|
27
|
-
totalSize: number;
|
|
28
|
-
expiredEntries: number;
|
|
29
|
-
hitRate: number;
|
|
30
|
-
missRate: number;
|
|
31
|
-
lastCleanup: number;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export interface MemoryOptions {
|
|
35
|
-
ttl?: number;
|
|
36
|
-
tags?: string[];
|
|
37
|
-
compress?: boolean;
|
|
38
|
-
encrypt?: boolean;
|
|
39
|
-
namespace?: string;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export class ServiceNowMemoryManager {
|
|
43
|
-
private static instance: ServiceNowMemoryManager;
|
|
44
|
-
private logger: Logger;
|
|
45
|
-
private memoryStore: Map<string, MemoryEntry> = new Map();
|
|
46
|
-
private stats: MemoryStats;
|
|
47
|
-
private memoryDir: string;
|
|
48
|
-
private maxMemorySize: number = 100 * 1024 * 1024; // 100MB default
|
|
49
|
-
private cleanupInterval: NodeJS.Timeout | null = null;
|
|
50
|
-
|
|
51
|
-
private constructor() {
|
|
52
|
-
this.logger = new Logger('ServiceNowMemoryManager');
|
|
53
|
-
this.memoryDir = path.join(process.cwd(), 'memory', 'servicenow_artifacts');
|
|
54
|
-
this.stats = {
|
|
55
|
-
totalEntries: 0,
|
|
56
|
-
totalSize: 0,
|
|
57
|
-
expiredEntries: 0,
|
|
58
|
-
hitRate: 0,
|
|
59
|
-
missRate: 0,
|
|
60
|
-
lastCleanup: Date.now()
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
this.initializeMemorySystem();
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
public static getInstance(): ServiceNowMemoryManager {
|
|
67
|
-
if (!ServiceNowMemoryManager.instance) {
|
|
68
|
-
ServiceNowMemoryManager.instance = new ServiceNowMemoryManager();
|
|
69
|
-
}
|
|
70
|
-
return ServiceNowMemoryManager.instance;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// ========================================
|
|
74
|
-
// CORE MEMORY OPERATIONS
|
|
75
|
-
// ========================================
|
|
76
|
-
|
|
77
|
-
async store(key: string, data: any, options: MemoryOptions = {}): Promise<boolean> {
|
|
78
|
-
try {
|
|
79
|
-
const namespace = options.namespace || 'default';
|
|
80
|
-
const namespacedKey = this.createNamespacedKey(namespace, key);
|
|
81
|
-
const ttl = options.ttl || 3600000; // Default 1 hour TTL
|
|
82
|
-
|
|
83
|
-
const entry: MemoryEntry = {
|
|
84
|
-
key: namespacedKey,
|
|
85
|
-
data: options.compress ? this.compressData(data) : data,
|
|
86
|
-
timestamp: Date.now(),
|
|
87
|
-
ttl,
|
|
88
|
-
tags: options.tags || [],
|
|
89
|
-
metadata: {
|
|
90
|
-
creator: 'ServiceNowAdvancedFeaturesMCP',
|
|
91
|
-
version: '1.0.0',
|
|
92
|
-
description: `Cached data for key: ${key}`
|
|
93
|
-
}
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
// Store in memory
|
|
97
|
-
this.memoryStore.set(namespacedKey, entry);
|
|
98
|
-
|
|
99
|
-
// Persist to disk for durability
|
|
100
|
-
await this.persistToDisk(namespacedKey, entry);
|
|
101
|
-
|
|
102
|
-
// Update stats
|
|
103
|
-
this.updateStats('store');
|
|
104
|
-
|
|
105
|
-
this.logger.debug(`Stored data for key: ${namespacedKey}`);
|
|
106
|
-
return true;
|
|
107
|
-
} catch (error) {
|
|
108
|
-
this.logger.error('Failed to store data:', error);
|
|
109
|
-
return false;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
async get(key: string, namespace: string = 'default'): Promise<MemoryEntry | null> {
|
|
114
|
-
try {
|
|
115
|
-
const namespacedKey = this.createNamespacedKey(namespace, key);
|
|
116
|
-
|
|
117
|
-
// Try memory first
|
|
118
|
-
let entry = this.memoryStore.get(namespacedKey);
|
|
119
|
-
|
|
120
|
-
// If not in memory, try loading from disk
|
|
121
|
-
if (!entry) {
|
|
122
|
-
entry = await this.loadFromDisk(namespacedKey);
|
|
123
|
-
if (entry) {
|
|
124
|
-
this.memoryStore.set(namespacedKey, entry);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
// Check if entry exists and is not expired
|
|
129
|
-
if (entry && !this.isExpired(entry)) {
|
|
130
|
-
this.updateStats('hit');
|
|
131
|
-
|
|
132
|
-
// Decompress if needed
|
|
133
|
-
if (entry.data && typeof entry.data === 'string' && entry.data.startsWith('compressed:')) {
|
|
134
|
-
entry.data = this.decompressData(entry.data);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
return entry;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
// Entry doesn't exist or is expired
|
|
141
|
-
if (entry && this.isExpired(entry)) {
|
|
142
|
-
await this.delete(key, namespace);
|
|
143
|
-
this.updateStats('expired');
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
this.updateStats('miss');
|
|
147
|
-
return null;
|
|
148
|
-
} catch (error) {
|
|
149
|
-
this.logger.error('Failed to get data:', error);
|
|
150
|
-
this.updateStats('miss');
|
|
151
|
-
return null;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
async delete(key: string, namespace: string = 'default'): Promise<boolean> {
|
|
156
|
-
try {
|
|
157
|
-
const namespacedKey = this.createNamespacedKey(namespace, key);
|
|
158
|
-
|
|
159
|
-
// Remove from memory
|
|
160
|
-
const deleted = this.memoryStore.delete(namespacedKey);
|
|
161
|
-
|
|
162
|
-
// Remove from disk
|
|
163
|
-
await this.removeFromDisk(namespacedKey);
|
|
164
|
-
|
|
165
|
-
this.updateStats('delete');
|
|
166
|
-
this.logger.debug(`Deleted data for key: ${namespacedKey}`);
|
|
167
|
-
|
|
168
|
-
return deleted;
|
|
169
|
-
} catch (error) {
|
|
170
|
-
this.logger.error('Failed to delete data:', error);
|
|
171
|
-
return false;
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
async clear(namespace?: string): Promise<boolean> {
|
|
176
|
-
try {
|
|
177
|
-
if (namespace) {
|
|
178
|
-
// Clear specific namespace
|
|
179
|
-
const keysToDelete = Array.from(this.memoryStore.keys())
|
|
180
|
-
.filter(key => key.startsWith(`${namespace}:`));
|
|
181
|
-
|
|
182
|
-
for (const key of keysToDelete) {
|
|
183
|
-
this.memoryStore.delete(key);
|
|
184
|
-
await this.removeFromDisk(key);
|
|
185
|
-
}
|
|
186
|
-
} else {
|
|
187
|
-
// Clear all memory
|
|
188
|
-
this.memoryStore.clear();
|
|
189
|
-
await this.clearDiskStorage();
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
this.updateStats('clear');
|
|
193
|
-
this.logger.info(`Cleared memory${namespace ? ` for namespace: ${namespace}` : ''}`);
|
|
194
|
-
return true;
|
|
195
|
-
} catch (error) {
|
|
196
|
-
this.logger.error('Failed to clear memory:', error);
|
|
197
|
-
return false;
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
// ========================================
|
|
202
|
-
// ADVANCED MEMORY OPERATIONS
|
|
203
|
-
// ========================================
|
|
204
|
-
|
|
205
|
-
async search(pattern: string, namespace?: string): Promise<MemoryEntry[]> {
|
|
206
|
-
try {
|
|
207
|
-
const regex = new RegExp(pattern, 'i');
|
|
208
|
-
const results: MemoryEntry[] = [];
|
|
209
|
-
|
|
210
|
-
for (const [key, entry] of this.memoryStore.entries()) {
|
|
211
|
-
// Check namespace filter
|
|
212
|
-
if (namespace && !key.startsWith(`${namespace}:`)) {
|
|
213
|
-
continue;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
// Check if not expired
|
|
217
|
-
if (this.isExpired(entry)) {
|
|
218
|
-
continue;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
// Check pattern match
|
|
222
|
-
if (regex.test(key) || (entry.tags && entry.tags.some(tag => regex.test(tag)))) {
|
|
223
|
-
results.push(entry);
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
return results;
|
|
228
|
-
} catch (error) {
|
|
229
|
-
this.logger.error('Failed to search memory:', error);
|
|
230
|
-
return [];
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
async getByTags(tags: string[], namespace?: string): Promise<MemoryEntry[]> {
|
|
235
|
-
try {
|
|
236
|
-
const results: MemoryEntry[] = [];
|
|
237
|
-
|
|
238
|
-
for (const [key, entry] of this.memoryStore.entries()) {
|
|
239
|
-
// Check namespace filter
|
|
240
|
-
if (namespace && !key.startsWith(`${namespace}:`)) {
|
|
241
|
-
continue;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
// Check if not expired
|
|
245
|
-
if (this.isExpired(entry)) {
|
|
246
|
-
continue;
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
// Check tag match
|
|
250
|
-
if (entry.tags && tags.some(tag => entry.tags!.includes(tag))) {
|
|
251
|
-
results.push(entry);
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
return results;
|
|
256
|
-
} catch (error) {
|
|
257
|
-
this.logger.error('Failed to get entries by tags:', error);
|
|
258
|
-
return [];
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
async updateTTL(key: string, newTTL: number, namespace: string = 'default'): Promise<boolean> {
|
|
263
|
-
try {
|
|
264
|
-
const namespacedKey = this.createNamespacedKey(namespace, key);
|
|
265
|
-
const entry = this.memoryStore.get(namespacedKey);
|
|
266
|
-
|
|
267
|
-
if (entry) {
|
|
268
|
-
entry.ttl = newTTL;
|
|
269
|
-
entry.timestamp = Date.now(); // Reset timestamp
|
|
270
|
-
|
|
271
|
-
// Update on disk as well
|
|
272
|
-
await this.persistToDisk(namespacedKey, entry);
|
|
273
|
-
|
|
274
|
-
this.logger.debug(`Updated TTL for key: ${namespacedKey} to ${newTTL}ms`);
|
|
275
|
-
return true;
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
return false;
|
|
279
|
-
} catch (error) {
|
|
280
|
-
this.logger.error('Failed to update TTL:', error);
|
|
281
|
-
return false;
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
// ========================================
|
|
286
|
-
// MEMORY MANAGEMENT
|
|
287
|
-
// ========================================
|
|
288
|
-
|
|
289
|
-
async cleanup(): Promise<void> {
|
|
290
|
-
try {
|
|
291
|
-
const keysToDelete: string[] = [];
|
|
292
|
-
let cleanedSize = 0;
|
|
293
|
-
|
|
294
|
-
for (const [key, entry] of this.memoryStore.entries()) {
|
|
295
|
-
if (this.isExpired(entry)) {
|
|
296
|
-
keysToDelete.push(key);
|
|
297
|
-
cleanedSize += this.calculateEntrySize(entry);
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
// Remove expired entries
|
|
302
|
-
for (const key of keysToDelete) {
|
|
303
|
-
this.memoryStore.delete(key);
|
|
304
|
-
await this.removeFromDisk(key);
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
this.stats.expiredEntries += keysToDelete.length;
|
|
308
|
-
this.stats.totalSize -= cleanedSize;
|
|
309
|
-
this.stats.lastCleanup = Date.now();
|
|
310
|
-
|
|
311
|
-
this.logger.info(`Cleanup completed: removed ${keysToDelete.length} expired entries, freed ${cleanedSize} bytes`);
|
|
312
|
-
} catch (error) {
|
|
313
|
-
this.logger.error('Failed to cleanup memory:', error);
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
async optimize(): Promise<void> {
|
|
318
|
-
try {
|
|
319
|
-
// Run cleanup first
|
|
320
|
-
await this.cleanup();
|
|
321
|
-
|
|
322
|
-
// Check if memory usage is too high
|
|
323
|
-
if (this.stats.totalSize > this.maxMemorySize) {
|
|
324
|
-
await this.evictLeastRecentlyUsed();
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
// Defragment memory files
|
|
328
|
-
await this.defragmentStorage();
|
|
329
|
-
|
|
330
|
-
this.logger.info('Memory optimization completed');
|
|
331
|
-
} catch (error) {
|
|
332
|
-
this.logger.error('Failed to optimize memory:', error);
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
getStats(): MemoryStats {
|
|
337
|
-
return { ...this.stats };
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
async exportData(namespace?: string): Promise<{ [key: string]: any }> {
|
|
341
|
-
try {
|
|
342
|
-
const exportData: { [key: string]: any } = {};
|
|
343
|
-
|
|
344
|
-
for (const [key, entry] of this.memoryStore.entries()) {
|
|
345
|
-
if (namespace && !key.startsWith(`${namespace}:`)) {
|
|
346
|
-
continue;
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
if (!this.isExpired(entry)) {
|
|
350
|
-
exportData[key] = {
|
|
351
|
-
data: entry.data,
|
|
352
|
-
timestamp: entry.timestamp,
|
|
353
|
-
ttl: entry.ttl,
|
|
354
|
-
tags: entry.tags,
|
|
355
|
-
metadata: entry.metadata
|
|
356
|
-
};
|
|
357
|
-
}
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
return exportData;
|
|
361
|
-
} catch (error) {
|
|
362
|
-
this.logger.error('Failed to export data:', error);
|
|
363
|
-
return {};
|
|
364
|
-
}
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
async importData(data: { [key: string]: any }, namespace?: string): Promise<boolean> {
|
|
368
|
-
try {
|
|
369
|
-
for (const [key, entryData] of Object.entries(data)) {
|
|
370
|
-
const finalKey = namespace ? `${namespace}:${key}` : key;
|
|
371
|
-
|
|
372
|
-
const entry: MemoryEntry = {
|
|
373
|
-
key: finalKey,
|
|
374
|
-
data: entryData.data,
|
|
375
|
-
timestamp: entryData.timestamp || Date.now(),
|
|
376
|
-
ttl: entryData.ttl || 3600000,
|
|
377
|
-
tags: entryData.tags || [],
|
|
378
|
-
metadata: entryData.metadata || {
|
|
379
|
-
creator: 'ServiceNowAdvancedFeaturesMCP',
|
|
380
|
-
version: '1.0.0'
|
|
381
|
-
}
|
|
382
|
-
};
|
|
383
|
-
|
|
384
|
-
this.memoryStore.set(finalKey, entry);
|
|
385
|
-
await this.persistToDisk(finalKey, entry);
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
this.updateStats('import');
|
|
389
|
-
this.logger.info(`Imported ${Object.keys(data).length} entries`);
|
|
390
|
-
return true;
|
|
391
|
-
} catch (error) {
|
|
392
|
-
this.logger.error('Failed to import data:', error);
|
|
393
|
-
return false;
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
// ========================================
|
|
398
|
-
// PRIVATE HELPER METHODS
|
|
399
|
-
// ========================================
|
|
400
|
-
|
|
401
|
-
private async initializeMemorySystem(): Promise<void> {
|
|
402
|
-
try {
|
|
403
|
-
// Ensure memory directory exists
|
|
404
|
-
await fs.mkdir(this.memoryDir, { recursive: true });
|
|
405
|
-
|
|
406
|
-
// Load existing data from disk
|
|
407
|
-
await this.loadFromDiskOnStartup();
|
|
408
|
-
|
|
409
|
-
// Start cleanup interval (every 5 minutes)
|
|
410
|
-
this.cleanupInterval = setInterval(() => {
|
|
411
|
-
this.cleanup().catch(error =>
|
|
412
|
-
this.logger.error('Scheduled cleanup failed:', error)
|
|
413
|
-
);
|
|
414
|
-
}, 5 * 60 * 1000);
|
|
415
|
-
|
|
416
|
-
this.logger.info('ServiceNow Memory Manager initialized');
|
|
417
|
-
} catch (error) {
|
|
418
|
-
this.logger.error('Failed to initialize memory system:', error);
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
private createNamespacedKey(namespace: string, key: string): string {
|
|
423
|
-
return `${namespace}:${key}`;
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
private isExpired(entry: MemoryEntry): boolean {
|
|
427
|
-
return Date.now() - entry.timestamp > entry.ttl;
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
private calculateEntrySize(entry: MemoryEntry): number {
|
|
431
|
-
return JSON.stringify(entry).length;
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
private compressData(data: any): string {
|
|
435
|
-
// Simple compression simulation - in real implementation would use actual compression
|
|
436
|
-
return `compressed:${JSON.stringify(data)}`;
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
private decompressData(compressedData: string): any {
|
|
440
|
-
// Simple decompression simulation
|
|
441
|
-
if (compressedData.startsWith('compressed:')) {
|
|
442
|
-
return JSON.parse(compressedData.substring(11));
|
|
443
|
-
}
|
|
444
|
-
return compressedData;
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
private async persistToDisk(key: string, entry: MemoryEntry): Promise<void> {
|
|
448
|
-
try {
|
|
449
|
-
const filename = this.keyToFilename(key);
|
|
450
|
-
const filepath = path.join(this.memoryDir, filename);
|
|
451
|
-
|
|
452
|
-
await fs.writeFile(filepath, JSON.stringify(entry, null, 2));
|
|
453
|
-
} catch (error) {
|
|
454
|
-
this.logger.error(`Failed to persist to disk: ${key}`, error);
|
|
455
|
-
}
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
private async loadFromDisk(key: string): Promise<MemoryEntry | null> {
|
|
459
|
-
try {
|
|
460
|
-
const filename = this.keyToFilename(key);
|
|
461
|
-
const filepath = path.join(this.memoryDir, filename);
|
|
462
|
-
|
|
463
|
-
const data = await fs.readFile(filepath, 'utf8');
|
|
464
|
-
return JSON.parse(data) as MemoryEntry;
|
|
465
|
-
} catch (error) {
|
|
466
|
-
// File doesn't exist or is corrupted
|
|
467
|
-
return null;
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
private async removeFromDisk(key: string): Promise<void> {
|
|
472
|
-
try {
|
|
473
|
-
const filename = this.keyToFilename(key);
|
|
474
|
-
const filepath = path.join(this.memoryDir, filename);
|
|
475
|
-
|
|
476
|
-
await fs.unlink(filepath);
|
|
477
|
-
} catch (error) {
|
|
478
|
-
// File might not exist, which is fine
|
|
479
|
-
}
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
private async clearDiskStorage(): Promise<void> {
|
|
483
|
-
try {
|
|
484
|
-
const files = await fs.readdir(this.memoryDir);
|
|
485
|
-
|
|
486
|
-
for (const file of files) {
|
|
487
|
-
if (file.endsWith('.json')) {
|
|
488
|
-
await fs.unlink(path.join(this.memoryDir, file));
|
|
489
|
-
}
|
|
490
|
-
}
|
|
491
|
-
} catch (error) {
|
|
492
|
-
this.logger.error('Failed to clear disk storage:', error);
|
|
493
|
-
}
|
|
494
|
-
}
|
|
495
|
-
|
|
496
|
-
private async loadFromDiskOnStartup(): Promise<void> {
|
|
497
|
-
try {
|
|
498
|
-
const files = await fs.readdir(this.memoryDir);
|
|
499
|
-
|
|
500
|
-
for (const file of files) {
|
|
501
|
-
if (file.endsWith('.json')) {
|
|
502
|
-
const filepath = path.join(this.memoryDir, file);
|
|
503
|
-
|
|
504
|
-
try {
|
|
505
|
-
const data = await fs.readFile(filepath, 'utf8');
|
|
506
|
-
const entry = JSON.parse(data) as MemoryEntry;
|
|
507
|
-
|
|
508
|
-
// Only load non-expired entries
|
|
509
|
-
if (!this.isExpired(entry)) {
|
|
510
|
-
this.memoryStore.set(entry.key, entry);
|
|
511
|
-
} else {
|
|
512
|
-
// Remove expired files
|
|
513
|
-
await fs.unlink(filepath);
|
|
514
|
-
}
|
|
515
|
-
} catch (error) {
|
|
516
|
-
// Skip corrupted files
|
|
517
|
-
this.logger.warn(`Skipping corrupted memory file: ${file}`);
|
|
518
|
-
}
|
|
519
|
-
}
|
|
520
|
-
}
|
|
521
|
-
|
|
522
|
-
this.updateStats('startup');
|
|
523
|
-
this.logger.info(`Loaded ${this.memoryStore.size} entries from disk`);
|
|
524
|
-
} catch (error) {
|
|
525
|
-
this.logger.error('Failed to load from disk on startup:', error);
|
|
526
|
-
}
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
private keyToFilename(key: string): string {
|
|
530
|
-
// Convert key to safe filename
|
|
531
|
-
return key.replace(/[^a-zA-Z0-9]/g, '_') + '.json';
|
|
532
|
-
}
|
|
533
|
-
|
|
534
|
-
private updateStats(operation: string): void {
|
|
535
|
-
this.stats.totalEntries = this.memoryStore.size;
|
|
536
|
-
this.stats.totalSize = Array.from(this.memoryStore.values())
|
|
537
|
-
.reduce((sum, entry) => sum + this.calculateEntrySize(entry), 0);
|
|
538
|
-
|
|
539
|
-
// Update hit/miss rates
|
|
540
|
-
const totalRequests = (this.stats as any).hits + (this.stats as any).misses;
|
|
541
|
-
if (totalRequests > 0) {
|
|
542
|
-
this.stats.hitRate = ((this.stats as any).hits / totalRequests) * 100;
|
|
543
|
-
this.stats.missRate = ((this.stats as any).misses / totalRequests) * 100;
|
|
544
|
-
}
|
|
545
|
-
|
|
546
|
-
// Track operation-specific stats
|
|
547
|
-
if (!((this.stats as any)[operation])) {
|
|
548
|
-
(this.stats as any)[operation] = 0;
|
|
549
|
-
}
|
|
550
|
-
(this.stats as any)[operation]++;
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
private async evictLeastRecentlyUsed(): Promise<void> {
|
|
554
|
-
try {
|
|
555
|
-
// Sort entries by timestamp (oldest first)
|
|
556
|
-
const sortedEntries = Array.from(this.memoryStore.entries())
|
|
557
|
-
.sort(([, a], [, b]) => a.timestamp - b.timestamp);
|
|
558
|
-
|
|
559
|
-
// Remove 25% of entries to free up space
|
|
560
|
-
const entriesToRemove = Math.floor(sortedEntries.length * 0.25);
|
|
561
|
-
|
|
562
|
-
for (let i = 0; i < entriesToRemove; i++) {
|
|
563
|
-
const [key] = sortedEntries[i];
|
|
564
|
-
this.memoryStore.delete(key);
|
|
565
|
-
await this.removeFromDisk(key);
|
|
566
|
-
}
|
|
567
|
-
|
|
568
|
-
this.logger.info(`Evicted ${entriesToRemove} least recently used entries`);
|
|
569
|
-
} catch (error) {
|
|
570
|
-
this.logger.error('Failed to evict LRU entries:', error);
|
|
571
|
-
}
|
|
572
|
-
}
|
|
573
|
-
|
|
574
|
-
private async defragmentStorage(): Promise<void> {
|
|
575
|
-
try {
|
|
576
|
-
// This would implement storage defragmentation
|
|
577
|
-
// For now, just log that defragmentation occurred
|
|
578
|
-
this.logger.debug('Storage defragmentation completed');
|
|
579
|
-
} catch (error) {
|
|
580
|
-
this.logger.error('Failed to defragment storage:', error);
|
|
581
|
-
}
|
|
582
|
-
}
|
|
583
|
-
|
|
584
|
-
// ========================================
|
|
585
|
-
// CLEANUP
|
|
586
|
-
// ========================================
|
|
587
|
-
|
|
588
|
-
async shutdown(): Promise<void> {
|
|
589
|
-
try {
|
|
590
|
-
if (this.cleanupInterval) {
|
|
591
|
-
clearInterval(this.cleanupInterval);
|
|
592
|
-
}
|
|
593
|
-
|
|
594
|
-
// Final cleanup
|
|
595
|
-
await this.cleanup();
|
|
596
|
-
|
|
597
|
-
this.logger.info('ServiceNow Memory Manager shutdown completed');
|
|
598
|
-
} catch (error) {
|
|
599
|
-
this.logger.error('Error during shutdown:', error);
|
|
600
|
-
}
|
|
601
|
-
}
|
|
602
|
-
}
|
|
603
|
-
|
|
604
|
-
// Export singleton instance
|
|
605
|
-
export const serviceNowMemoryManager = ServiceNowMemoryManager.getInstance();
|