From 6ed591a45ae58fa2dc23125de268bb7c7ab28890 Mon Sep 17 00:00:00 2001 From: kevin <5299031+kevin9327@users.noreply.github.com> Date: Sun, 25 Oct 2026 09:45:59 +0900 Subject: [PATCH] fix(memory): keep same-key entries from other namespaces in memory_search_unified (#4470) Signed-off-by: kevin9327 <5299031+kevin9327@users.noreply.github.com> --- .../memory-search-unified-same-key.test.ts | 58 +++++++++++++++++++ .../cli/src/mcp-tools/memory-tools.ts | 9 ++- 2 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 v3/@claude-flow/cli/__tests__/memory-search-unified-same-key.test.ts diff --git a/v3/@claude-flow/cli/__tests__/memory-search-unified-same-key.test.ts b/v3/@claude-flow/cli/__tests__/memory-search-unified-same-key.test.ts new file mode 100644 index 0000000000..25b1aeff45 --- /dev/null +++ b/v3/@claude-flow/cli/__tests__/memory-search-unified-same-key.test.ts @@ -0,0 +1,58 @@ +// memory_search_unified fans out one search per namespace and merges the +// results. Entries are identified by (namespace, key) (see #2271), so two +// entries that share a key in different namespaces must both survive the +// merge instead of the lower-scored one being dropped as a duplicate. + +import { beforeEach, describe, expect, it, vi } from 'vitest'; + +vi.mock('fs', () => ({ + existsSync: vi.fn(() => false), mkdirSync: vi.fn(), readdirSync: vi.fn(() => []), + readFileSync: vi.fn(() => '{}'), statSync: vi.fn(), unlinkSync: vi.fn(), writeFileSync: vi.fn(), +})); + +const { searchEntries } = vi.hoisted(() => ({ searchEntries: vi.fn() })); +vi.mock('../src/memory/memory-initializer.js', () => ({ + searchEntries, + storeEntry: vi.fn(), listEntries: vi.fn(), getEntry: vi.fn(), deleteEntry: vi.fn(), + initializeMemoryDatabase: vi.fn(), + checkMemoryInitialization: vi.fn(async () => ({ initialized: true })), +})); + +import { memoryTools } from '../src/mcp-tools/memory-tools.js'; + +const tool = memoryTools.find((t) => t.name === 'memory_search_unified')!; + +interface UnifiedResult { + results: Array<{ key: string; namespace: string; score: number }>; + total: number; +} + +describe('memory_search_unified merge', () => { + beforeEach(() => { + searchEntries.mockReset(); + searchEntries.mockImplementation(async ({ namespace }: { namespace: string }) => ({ + success: true, + results: namespace === 'project-a' + ? [{ key: 'config', content: 'a config', score: 0.9, namespace }] + : [{ key: 'config', content: 'b config', score: 0.7, namespace }], + })); + }); + + it('keeps entries that share a key in different namespaces', async () => { + const r = await tool.handler({ + query: 'config', + namespaces: ['project-a', 'project-b'], + }) as UnifiedResult; + + expect(r.results.map((e) => [e.namespace, e.key])).toEqual([ + ['project-a', 'config'], + ['project-b', 'config'], + ]); + expect(r.total).toBe(2); + }); + + it('still returns a key once when it appears once per namespace', async () => { + const r = await tool.handler({ query: 'config', namespace: 'project-a' }) as UnifiedResult; + expect(r.results.map((e) => [e.namespace, e.key])).toEqual([['project-a', 'config']]); + }); +}); diff --git a/v3/@claude-flow/cli/src/mcp-tools/memory-tools.ts b/v3/@claude-flow/cli/src/mcp-tools/memory-tools.ts index 8605652ccb..8b2d09ef59 100644 --- a/v3/@claude-flow/cli/src/mcp-tools/memory-tools.ts +++ b/v3/@claude-flow/cli/src/mcp-tools/memory-tools.ts @@ -1542,12 +1542,15 @@ export const memoryTools: MCPTool[] = [ } catch { /* namespace may not exist */ } } - // Sort by score, deduplicate by key, take top N + // Sort by score, deduplicate by (namespace, key), take top N. An entry is + // identified by both (see #2271), so the same key in another namespace is + // a different entry and must not be dropped as a duplicate. allResults.sort((a, b) => b.score - a.score); const seen = new Set(); const deduplicated = allResults.filter(r => { - if (seen.has(r.key)) return false; - seen.add(r.key); + const id = JSON.stringify([r.namespace, r.key]); + if (seen.has(id)) return false; + seen.add(id); return true; }).slice(0, limit);