From 615eb9ef123bf2cf18eb24d82aa04f55990f42e9 Mon Sep 17 00:00:00 2001 From: SYMBiEX Date: Tue, 29 Jul 2025 03:15:33 -0500 Subject: [PATCH] refactor(utils): improve cache and memory manager typing --- mind-agents/src/utils/MemoryManager.ts | 20 ++++++++++---------- mind-agents/src/utils/context-cache.ts | 18 +++++++++++------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/mind-agents/src/utils/MemoryManager.ts b/mind-agents/src/utils/MemoryManager.ts index 8da5b6e..a28f510 100644 --- a/mind-agents/src/utils/MemoryManager.ts +++ b/mind-agents/src/utils/MemoryManager.ts @@ -24,10 +24,10 @@ interface LeakDetectionResult { recommendations: string[]; } -interface ManagedResource { +interface ManagedResource { id: string; type: string; - resource: any; + resource: T; cleanup: () => Promise | void; createdAt: number; lastAccessed: number; @@ -35,12 +35,12 @@ interface ManagedResource { ttl?: number; } -export class MemoryManager extends EventEmitter { +export class MemoryManager extends EventEmitter { private snapshots: MemorySnapshot[] = []; private maxSnapshots = 100; private snapshotInterval = 30000; // 30 seconds - private resources = new Map(); - private weakRefs = new Set>(); + private resources = new Map>(); + private weakRefs = new Set>(); private isMonitoring = false; private monitoringTimer?: NodeJS.Timer; private cleanupTimer?: NodeJS.Timer; @@ -131,7 +131,7 @@ export class MemoryManager extends EventEmitter { */ registerResource( type: string, - resource: any, + resource: T, cleanup: () => Promise | void, options?: { ttl?: number; @@ -140,7 +140,7 @@ export class MemoryManager extends EventEmitter { ): string { const id = options?.id || this.generateResourceId(); - const managedResource: ManagedResource = { + const managedResource: ManagedResource = { id, type, resource, @@ -185,7 +185,7 @@ export class MemoryManager extends EventEmitter { /** * Access a registered resource (updates access tracking) */ - accessResource(id: string): any { + accessResource(id: string): T | null { const resource = this.resources.get(id); if (!resource) return null; @@ -476,7 +476,7 @@ export class MemoryManager extends EventEmitter { private cleanupWeakRefs(): void { const initialSize = this.weakRefs.size; - const toRemove: WeakRef[] = []; + const toRemove: WeakRef[] = []; for (const weakRef of this.weakRefs) { if (weakRef.deref() === undefined) { @@ -503,4 +503,4 @@ export const memoryManager = new MemoryManager({ // Auto-start if not in test environment if (process.env.NODE_ENV !== 'test') { memoryManager.start(); -} \ No newline at end of file +} diff --git a/mind-agents/src/utils/context-cache.ts b/mind-agents/src/utils/context-cache.ts index f83cb53..0f02aa6 100644 --- a/mind-agents/src/utils/context-cache.ts +++ b/mind-agents/src/utils/context-cache.ts @@ -17,7 +17,7 @@ export interface CacheEntry { hits: number; size: number; ttl?: number; - metadata?: Record; + metadata?: Record; } /** @@ -157,7 +157,7 @@ export class LRUCache { /** * Estimate size of value */ - private estimateSize(value: any): number { + private estimateSize(value: T): number { try { return JSON.stringify(value).length; } catch { @@ -202,8 +202,8 @@ export class LRUCache { /** * Multi-level context cache */ -export class ContextCache { - private l1Cache: LRUCache; // Hot cache - in memory +export class ContextCache { + private l1Cache: LRUCache; // Hot cache - in memory private l2Cache: LRUCache; // Warm cache - compressed private cleanupInterval?: NodeJS.Timeout; @@ -229,7 +229,7 @@ export class ContextCache { /** * Get context from cache */ - async get(key: string): Promise { + async get(key: string): Promise { // Check L1 first const l1Result = this.l1Cache.get(key); if (l1Result) { @@ -256,7 +256,11 @@ export class ContextCache { /** * Set context in cache */ - async set(key: string, value: any, options: { ttl?: number; priority?: 'high' | 'normal' } = {}): Promise { + async set( + key: string, + value: T, + options: { ttl?: number; priority?: 'high' | 'normal' } = {} + ): Promise { // Always set in L1 for immediate access this.l1Cache.set(key, value, options.ttl); @@ -379,4 +383,4 @@ export const contextCache = new ContextCache(); // Cleanup on process exit process.on('beforeExit', () => { contextCache.stop(); -}); \ No newline at end of file +});