-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory-manager.js
More file actions
561 lines (488 loc) · 17.4 KB
/
Copy pathmemory-manager.js
File metadata and controls
561 lines (488 loc) · 17.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
/**
* Memory Manager
* Core logic for tracking, analyzing, and managing user memory data.
*/
const { MemoryStorage, DEFAULT_MEMORY, cloneData, parseDotPath } = require('./storage');
const { DataLifecycleManager } = require('./data-lifecycle');
const fs = require('fs').promises;
const path = require('path');
const { redactSensitiveData, redactProjectPath } = require('./privacy');
const {
INPUT_LIMITS,
assertTextLength,
assertDataWithinLimits,
trimArrayToLimits
} = require('./limits');
const IMPORTED_HISTORY_FIELDS = [
['inputHabits', 'commonCommands'],
['inputHabits', 'frequentPatterns'],
['inputHabits', 'preferredTools'],
['sessionHistory', 'recentTopics'],
['sessionHistory', 'frequentTasks']
];
function boundImportedHistory(data, maxHistoryItems) {
if (!data || typeof data !== 'object' || Array.isArray(data)) return data;
for (const [sectionName, fieldName] of IMPORTED_HISTORY_FIELDS) {
const section = data[sectionName];
if (!section || typeof section !== 'object' || Array.isArray(section)) continue;
if (Array.isArray(section[fieldName])) {
section[fieldName] = trimArrayToLimits(
section[fieldName],
maxHistoryItems,
INPUT_LIMITS.maxStoredValueBytes
);
}
}
return data;
}
function isRecoverableMemoryFileError(error) {
return error instanceof SyntaxError || error?.message === 'Invalid memory data format';
}
function isNonEmptyString(value) {
return typeof value === 'string' && value.trim() !== '';
}
function isRecommendationCommand(value) {
return value && typeof value === 'object' && !Array.isArray(value) &&
isNonEmptyString(value.command) && Number.isFinite(value.count);
}
function isRecommendationProject(value) {
return value && typeof value === 'object' && !Array.isArray(value) &&
isNonEmptyString(value.path);
}
class MemoryManager {
constructor(config, storage) {
this.config = config;
this.storage = storage;
this.lifecycle = new DataLifecycleManager(storage, config);
this.autoSaveTimer = null;
this._autoSaveGeneration = 0;
this.sessionStartTime = null;
this._initializePromise = null;
this.recommendationMetrics = {
requests: 0,
availableRequests: 0,
contextualRequests: 0,
contextMatches: 0,
fallbackRequests: 0,
suggestions: 0
};
}
/**
* Initialize the memory manager
*/
async initialize() {
if (this.sessionStartTime) return;
if (this._initializePromise) return this._initializePromise;
this._initializePromise = (async () => {
const automaticCollectionEnabled = this.isAutomaticCollectionEnabled();
if (this.config.backupOnInitialize) {
try {
await fs.access(this.storage.storagePath);
await this.lifecycle.backupFile('startup');
} catch (error) {
if (error.code !== 'ENOENT') throw error;
}
}
try {
await this.storage.initialize({
persistIfMissing: automaticCollectionEnabled,
persistSanitized: automaticCollectionEnabled
});
} catch (error) {
if (!isRecoverableMemoryFileError(error)) throw error;
try {
const recovery = await this.lifecycle.recoverFromLatestBackup();
console.warn(`Memory plugin: Recovered corrupt memory from backup ${recovery.restored}`);
} catch (recoveryError) {
error.recoveryError = recoveryError;
throw error;
}
}
if (automaticCollectionEnabled) this.startAutoSave();
this.sessionStartTime = Date.now();
// Update session metadata
if (automaticCollectionEnabled) {
await this.storage.recordSessionStart();
}
await this.lifecycle.applyRetention();
})();
try {
await this._initializePromise;
} catch (error) {
this.stopAutoSave();
this.sessionStartTime = null;
throw error;
} finally {
this._initializePromise = null;
}
}
async ensureInitialized() {
await this.initialize();
}
isAutomaticCollectionEnabled() {
return this.config.trackToolCalls ||
this.config.trackPreferences ||
this.config.trackProjectContext ||
this.config.trackSessionHistory;
}
/**
* Start automatic saving at configured intervals
*/
startAutoSave() {
this.stopAutoSave();
const generation = this._autoSaveGeneration;
const schedule = () => {
if (generation !== this._autoSaveGeneration) return;
this.autoSaveTimer = setTimeout(async () => {
if (generation !== this._autoSaveGeneration) return;
try {
await this.storage.save();
} catch (error) {
console.error('Memory plugin: Auto-save failed:', error.message);
} finally {
schedule();
}
}, this.config.autoSaveInterval);
};
schedule();
}
/**
* Stop automatic saving.
*/
stopAutoSave() {
this._autoSaveGeneration += 1;
if (this.autoSaveTimer) {
clearTimeout(this.autoSaveTimer);
this.autoSaveTimer = null;
}
}
/**
* Record a tool call
* @param {Object} toolCall - Tool call information
*/
async recordToolCall(toolCall) {
if (!this.config.trackToolCalls) return;
if (!toolCall || typeof toolCall !== 'object' || typeof toolCall.name !== 'string' || toolCall.name.trim() === '') {
throw new Error('toolCall.name must be a non-empty string');
}
assertTextLength(toolCall.name, 'toolCall.name', INPUT_LIMITS.maxProjectNameLength);
await this.ensureInitialized();
const { name, args } = toolCall;
const mutations = [
{
dotPath: 'sessionHistory.toolUsageStats',
mutate: (stats) => {
const nextStats = stats && typeof stats === 'object' && !Array.isArray(stats) ? stats : {};
const currentCount = Number.isSafeInteger(nextStats[name]) && nextStats[name] >= 0
? nextStats[name]
: 0;
nextStats[name] = currentCount + 1;
return nextStats;
}
}
];
if (this.config.trackPreferences) {
mutations.push({
dotPath: 'inputHabits.preferredTools',
maxArrayBytes: INPUT_LIMITS.maxStoredValueBytes,
mutate: (tools) => {
const nextTools = Array.isArray(tools) ? tools : [];
if (!nextTools.includes(name)) nextTools.push(name);
return nextTools;
}
});
}
if (args && args.command) {
if (typeof args.command !== 'string') throw new Error('command must be a string');
assertTextLength(args.command, 'command');
const safeCommand = redactSensitiveData(args.command);
mutations.push({
dotPath: 'inputHabits.commonCommands',
maxArrayLength: this.config.maxHistoryItems,
mutate: (commands) => {
const nextCommands = Array.isArray(commands) ? commands : [];
const existingIndex = nextCommands.findIndex((entry) => entry && entry.command === safeCommand);
const now = new Date().toISOString();
if (existingIndex >= 0) {
const existing = nextCommands[existingIndex];
existing.count = Number.isSafeInteger(existing.count) && existing.count >= 0 ? existing.count + 1 : 1;
existing.lastUsed = now;
} else {
nextCommands.unshift({ command: safeCommand, count: 1, firstUsed: now, lastUsed: now });
}
nextCommands.sort((left, right) => right.count - left.count);
return nextCommands.slice(0, this.config.maxHistoryItems);
}
});
}
await this.storage.mutatePersistedBatch(mutations);
}
/**
* Analyze and record command patterns
* @param {string} command - The command that was executed
*/
async analyzeCommand(command) {
if (typeof command !== 'string') {
throw new Error('command must be a string');
}
assertTextLength(command, 'command');
await this.ensureInitialized();
const safeCommand = redactSensitiveData(command);
await this.storage.recordCommandUsage(safeCommand, this.config.maxHistoryItems);
}
/**
* Record user preference update
* @param {string} preferenceKey - The preference key
* @param {*} value - The preference value
*/
async recordPreference(preferenceKey, value) {
if (typeof preferenceKey !== 'string' || preferenceKey.trim() === '') {
throw new Error('preferenceKey must be a non-empty string');
}
assertTextLength(preferenceKey, 'preferenceKey', INPUT_LIMITS.maxProjectNameLength);
assertDataWithinLimits(value, 'stored value', INPUT_LIMITS.maxStoredValueBytes);
parseDotPath(`userPreferences.${preferenceKey}`);
await this.ensureInitialized();
this.storage.set(`userPreferences.${preferenceKey}`, redactSensitiveData(value));
await this.storage.save();
}
/**
* Record active project context
* @param {Object} projectInfo - Project information
*/
async recordProjectContext(projectInfo) {
if (!projectInfo || typeof projectInfo !== 'object' || Array.isArray(projectInfo)) {
throw new Error('projectInfo must be an object');
}
if (typeof projectInfo.path !== 'string' || projectInfo.path.trim() === '') {
throw new Error('projectInfo.path must be a non-empty string');
}
assertTextLength(projectInfo.path, 'projectInfo.path', INPUT_LIMITS.maxProjectPathLength);
if (projectInfo.name !== undefined && typeof projectInfo.name !== 'string') {
throw new Error('projectInfo.name must be a string');
}
if (projectInfo.name !== undefined) {
assertTextLength(projectInfo.name, 'projectInfo.name', INPUT_LIMITS.maxProjectNameLength);
}
if (projectInfo.tags !== undefined && (!Array.isArray(projectInfo.tags) || projectInfo.tags.some((tag) => typeof tag !== 'string'))) {
throw new Error('projectInfo.tags must be an array of strings');
}
if (projectInfo.tags !== undefined) {
if (projectInfo.tags.length > INPUT_LIMITS.maxTags) {
throw new Error(`projectInfo.tags must not contain more than ${INPUT_LIMITS.maxTags} items`);
}
projectInfo.tags.forEach((tag) => assertTextLength(tag, 'projectInfo.tag', INPUT_LIMITS.maxTagLength));
}
await this.ensureInitialized();
const safeProjectInfo = redactSensitiveData(projectInfo);
safeProjectInfo.path = redactProjectPath(safeProjectInfo.path);
await this.storage.addProject({
path: safeProjectInfo.path,
name: safeProjectInfo.name || path.basename(safeProjectInfo.path),
tags: safeProjectInfo.tags || []
});
}
/**
* Record session topic or task
* @param {string} type - 'topic' or 'task'
* @param {string} content - The topic or task content
*/
async recordSessionItem(type, content) {
if (type !== 'topic' && type !== 'task') {
throw new Error('session item type must be topic or task');
}
if (typeof content !== 'string') {
throw new Error('session item content must be a string');
}
assertTextLength(content, 'session item content');
await this.ensureInitialized();
const path = type === 'topic'
? 'sessionHistory.recentTopics'
: 'sessionHistory.frequentTasks';
await this.storage.appendToArray(path, {
content: redactSensitiveData(content),
timestamp: new Date().toISOString()
}, this.config.maxHistoryItems, 'content');
}
/**
* Get recommendations based on memory data
* @param {string} context - Current context for recommendations
* @returns {Object} Recommendations object
*/
getRecommendations(context) {
this.recommendationMetrics.requests += 1;
if (!this.config.enableRecommendations) {
return { available: false };
}
this.recommendationMetrics.availableRequests += 1;
const recommendations = {
available: true,
suggestions: []
};
if (!this.storage.memory) return recommendations;
// Recommend preferred agents
const preferredAgents = (this.storage.get('userPreferences.preferredAgents') || [])
.filter(isNonEmptyString);
if (preferredAgents.length > 0) {
recommendations.suggestions.push({
type: 'agent',
items: preferredAgents.slice(0, 3),
reason: 'Based on your usage history'
});
}
// Recommend default model
const defaultModel = this.storage.get('userPreferences.defaultModel');
if (isNonEmptyString(defaultModel)) {
recommendations.suggestions.push({
type: 'model',
items: [defaultModel],
reason: 'Your preferred model'
});
}
const contextTokens = typeof context === 'string'
? context.trim().toLowerCase().split(/\s+/).filter(Boolean)
: [];
const hasContext = contextTokens.length > 0;
if (hasContext) this.recommendationMetrics.contextualRequests += 1;
const matchesContext = (value) => {
if (contextTokens.length === 0) return true;
const text = String(value || '').toLowerCase();
return contextTokens.some((token) => text.includes(token));
};
// Recommend common commands that have reached the recognition threshold
const commonCommands = this.storage.get('inputHabits.commonCommands') || [];
const frequentCommands = commonCommands.filter((command) => (
isRecommendationCommand(command) && command.count >= this.config.patternRecognitionThreshold
));
const contextualCommands = frequentCommands.filter((command) => matchesContext(command.command));
const commandsToRecommend = contextualCommands.length > 0 ? contextualCommands : frequentCommands;
if (commandsToRecommend.length > 0) {
recommendations.suggestions.push({
type: 'commands',
items: commandsToRecommend.slice(0, 5).map(cmd => cmd.command),
reason: 'Frequently used commands'
});
}
// Recommend projects
const activeProjects = (this.storage.get('projectContext.activeProjects') || [])
.filter(isRecommendationProject);
const contextualProjects = activeProjects.filter((project) => matchesContext([
project.name,
project.path,
...(Array.isArray(project.tags) ? project.tags.filter(isNonEmptyString) : [])
].join(' ')));
const projectsToRecommend = contextualProjects.length > 0 ? contextualProjects : activeProjects;
if (projectsToRecommend.length > 0) {
recommendations.suggestions.push({
type: 'projects',
items: projectsToRecommend.slice(0, 3).map(p => p.name || p.path),
reason: 'Recently accessed projects'
});
}
if (hasContext) {
const hasContextMatch = contextualCommands.length > 0 || contextualProjects.length > 0;
const hasFallbackCandidate = !hasContextMatch && (
frequentCommands.length > 0 || activeProjects.length > 0
);
if (hasContextMatch) this.recommendationMetrics.contextMatches += 1;
else if (hasFallbackCandidate) this.recommendationMetrics.fallbackRequests += 1;
}
this.recommendationMetrics.suggestions += recommendations.suggestions.length;
return recommendations;
}
/**
* Get privacy-safe recommendation quality metrics for the current process.
* @returns {Object} Recommendation request, coverage, and fallback metrics
*/
getRecommendationMetrics() {
const { contextualRequests } = this.recommendationMetrics;
return {
...this.recommendationMetrics,
contextMatchRate: contextualRequests > 0
? this.recommendationMetrics.contextMatches / contextualRequests
: null,
fallbackRate: contextualRequests > 0
? this.recommendationMetrics.fallbackRequests / contextualRequests
: null,
patternRecognitionThreshold: this.config.patternRecognitionThreshold
};
}
/**
* Get memory statistics
* @returns {Object} Statistics
*/
getStats() {
if (!this.storage.memory) {
return {
totalSessions: 0,
lastUpdated: null,
trackedTools: 0,
activeProjects: 0,
preferredAgents: 0
};
}
return this.storage.getStats();
}
/**
* Export memory data
* @returns {Object} Complete memory data
*/
exportData() {
if (!this.storage.memory) return cloneData(DEFAULT_MEMORY);
return this.storage.exportData();
}
/**
* Import memory data
* @param {Object} data - Memory data to import
*/
async importData(data) {
await this.ensureInitialized();
const safeData = boundImportedHistory(
redactSensitiveData(data),
this.config.maxHistoryItems
);
await this.lifecycle.backup('import-safety');
await this.storage.importData(safeData);
}
async backup(reason = 'manual') {
await this.ensureInitialized();
return this.lifecycle.backup(reason);
}
async listBackups() {
await this.ensureInitialized();
return this.lifecycle.listBackups();
}
async restoreBackup(name) {
await this.ensureInitialized();
return this.lifecycle.restoreBackup(name);
}
async applyRetention() {
await this.ensureInitialized();
return this.lifecycle.applyRetention();
}
/**
* Clear all memory data
*/
async clearMemory() {
if (!this.config.allowClearMemory) {
throw new Error('Memory clearing is disabled in configuration');
}
await this.ensureInitialized();
await this.lifecycle.backup('clear-safety');
await this.storage.clear();
}
/**
* Cleanup resources
*/
async dispose() {
if (this._initializePromise) {
await this._initializePromise.catch(() => {});
}
this.stopAutoSave();
if (this.storage.memory) {
await this.storage.save();
await this.storage.flush();
}
}
}
module.exports = { MemoryManager };