From e0fb4ebd74269e2f910de03360b2f91a9af18031 Mon Sep 17 00:00:00 2001 From: Rudy Celekli <47457359+rudycelekli@users.noreply.github.com> Date: Sun, 4 Oct 2026 02:12:54 -0400 Subject: [PATCH] fix(memory): match each cache key independently during invalidation Signed-off-by: Rudy Celekli <47457359+rudycelekli@users.noreply.github.com> --- ...he-manager-invalidation-regression.test.ts | 30 +++++++++++++++++++ v3/@claude-flow/memory/src/cache-manager.ts | 7 ++++- 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 v3/@claude-flow/memory/src/cache-manager-invalidation-regression.test.ts diff --git a/v3/@claude-flow/memory/src/cache-manager-invalidation-regression.test.ts b/v3/@claude-flow/memory/src/cache-manager-invalidation-regression.test.ts new file mode 100644 index 0000000000..93c2afce45 --- /dev/null +++ b/v3/@claude-flow/memory/src/cache-manager-invalidation-regression.test.ts @@ -0,0 +1,30 @@ +import { afterEach, describe, it, expect } from 'vitest'; +import { CacheManager } from './cache-manager.js'; +const caches: CacheManager[] = []; +afterEach(() => { + for (const cache of caches.splice(0)) cache.shutdown(); +}); +describe('stateful cache invalidation patterns', () => { + it.each([/article/g, /article/y])( + 'invalidates every matching entry with %s', + (pattern) => { + const cache = new CacheManager(); + caches.push(cache); + cache.set('article-one', 'old result one'); + cache.set('article-two', 'old result two'); + cache.set('article-three', 'old result three'); + cache.set('unrelated', 'valid'); + expect(cache.invalidatePattern(pattern)).toBe(3); + expect(cache.keys()).toEqual(['unrelated']); + }, + ); + it('preserves the caller pattern cursor instead of skipping the first key', () => { + const cache = new CacheManager(); + caches.push(cache); + cache.set('article-one', 'old'); + const pattern = /article/g; + pattern.lastIndex = 4; + expect(cache.invalidatePattern(pattern)).toBe(1); + expect(pattern.lastIndex).toBe(4); + }); +}); diff --git a/v3/@claude-flow/memory/src/cache-manager.ts b/v3/@claude-flow/memory/src/cache-manager.ts index 2bb67f2e6b..191cf43417 100644 --- a/v3/@claude-flow/memory/src/cache-manager.ts +++ b/v3/@claude-flow/memory/src/cache-manager.ts @@ -296,7 +296,12 @@ export class CacheManager extends EventEmitter { let invalidated = 0; for (const key of this.cache.keys()) { - if (regex.test(key)) { + // RegExp cursors belong to a single input, not the whole key sequence. + const lastIndex = regex.lastIndex; + regex.lastIndex = 0; + const matches = regex.test(key); + regex.lastIndex = lastIndex; + if (matches) { this.delete(key); invalidated++; }