mirror of
https://github.com/ruvnet/ruflo.git
synced 2026-12-04 06:21:59 +09:00
fix(memory): keep same-key entries from other namespaces in memory_search_unified (#4470)
Signed-off-by: kevin9327 <5299031+kevin9327@users.noreply.github.com>
This commit is contained in:
1 parent
973d0ab497
commit
6ed591a45a
2 files changed
+64
-3
No files matched your search
@@ -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']]);
|
||||
});
|
||||
});
|
||||
@@ -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<string>();
|
||||
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);
|
||||
|
||||
|
||||
Reference in new issue
Block a user