Skip to content

armgit / ruflo

Public Imported from GitHub
0

🌊 The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated

fix(memory): account for replacement sizes before cache admission

Rudy Celeklie36ec17c78d6
diff --git a/v3/@claude-flow/memory/src/cache-manager-memory-regression.test.ts b/v3/@claude-flow/memory/src/cache-manager-memory-regression.test.ts
new file mode 100644
index 0000000000..6d3b276792
--- /dev/null
+++ b/v3/@claude-flow/memory/src/cache-manager-memory-regression.test.ts
@@ -0,0 +1,46 @@
+import { afterEach, describe, it, expect } from 'vitest';
+import { CacheManager } from './cache-manager.js';
+const caches: CacheManager<string>[] = [];
+afterEach(() => {
+ for (const cache of caches.splice(0)) cache.shutdown();
+});
+function create(maxMemory = 1000): CacheManager<string> {
+ const cache = new CacheManager<string>({ maxMemory });
+ caches.push(cache);
+ return cache;
+}
+describe('replacement memory accounting', () => {
+ it('accounts for growing and shrinking entries and returns to zero after deletion', () => {
+ const cache = create();
+ cache.set('a', 'small');
+ cache.set('a', 'a larger replacement');
+ expect(cache.getStats().memoryUsage).toBe(
+ JSON.stringify('a larger replacement').length * 2,
+ );
+ cache.set('a', 'x');
+ expect(cache.getStats().memoryUsage).toBe(6);
+ cache.delete('a');
+ expect(cache.getStats().memoryUsage).toBe(0);
+ });
+ it('evicts an older neighbor when a fitting replacement needs room', () => {
+ const cache = create(32);
+ cache.set('a', '1234');
+ cache.set('b', '1234');
+ cache.set('a', '1234567890');
+ expect(cache.get('a')).toBe('1234567890');
+ expect(cache.get('b')).toBeNull();
+ expect(cache.getStats().memoryUsage).toBe(24);
+ });
+ it('skips oversized entries without evicting unrelated valid values or retaining a stale replacement', () => {
+ const cache = create(32);
+ cache.set('a', 'old');
+ cache.set('b', 'valid');
+ cache.set('new', 'x'.repeat(100));
+ expect(cache.get('a')).toBe('old');
+ expect(cache.get('b')).toBe('valid');
+ cache.set('a', 'x'.repeat(100));
+ expect(cache.get('a')).toBeNull();
+ expect(cache.get('b')).toBe('valid');
+ expect(cache.getStats().memoryUsage).toBe(14);
+ });
+});
diff --git a/v3/@claude-flow/memory/src/cache-manager.ts b/v3/@claude-flow/memory/src/cache-manager.ts
index 2bb67f2e6b..192eb1c72c 100644
--- a/v3/@claude-flow/memory/src/cache-manager.ts
+++ b/v3/@claude-flow/memory/src/cache-manager.ts
@@ -109,9 +109,19 @@ export class CacheManager<T = MemoryEntry> extends EventEmitter {
const now = Date.now();
const entryTtl = ttl || this.config.ttl;
+ const entryMemory = this.estimateSize(data);
+ // A value larger than the entire budget cannot be cached. Invalidate an
+ // older value for this key, but preserve unrelated useful entries.
+ if (this.config.maxMemory && entryMemory > this.config.maxMemory) {
+ this.delete(key);
+ return;
+ }
+
// Check if key already exists
const existingNode = this.cache.get(key);
if (existingNode) {
+ // Account for the old value before overwriting it.
+ this.currentMemory += entryMemory - this.estimateSize(existingNode.value.data);
// Update existing entry
existingNode.value.data = data;
existingNode.value.cachedAt = now;
@@ -119,13 +129,15 @@ export class CacheManager<T = MemoryEntry> extends EventEmitter {
existingNode.value.lastAccessedAt = now;
this.moveToFront(existingNode);
+ if (this.config.maxMemory) {
+ while (this.currentMemory > this.config.maxMemory && this.cache.size > 0) {
+ this.evictLRU();
+ }
+ }
this.stats.writes++;
return;
}
- // Calculate memory for new entry
- const entryMemory = this.estimateSize(data);
-
// Evict entries if needed for memory pressure
if (this.config.maxMemory) {
while (