From 3eb6b4d6562080d964235adeaa88e34ada5ae85f Mon Sep 17 00:00:00 2001 From: rUv Date: Wed, 6 May 2026 16:52:20 -0400 Subject: [PATCH] fix(memory): content-hash dedupe in memory_import_claude (#1791.8) (#1808) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `memory_import_claude --allProjects=true` was indexing the same memory file content under multiple `project_id` prefixes (e.g. `claude:-home-matheus:`, `claude:-home-mathe:`, `claude:-home-atheus-ssali:`), producing 5-8x duplication on long-lived homes. The reporter measured 2648 entries from 456 files on an 8-project home (vs 331 entries from 57 files with `--allProjects=false` — the natural 8x reduction). Root cause: Claude Code's `~/.claude/projects/` accumulates historical project_id directories (truncated forms, sandbox cwds, renamed workspaces). Each contains its own copy of the same memory files. The previous import walked all of them and stored every copy. Fix: track a `Set` of SHA-256 prefix hashes of each file's raw content. When iterating, skip any file whose content hash we've already imported under a different project_id directory. Surface the new `duplicatesSkipped` count in the response so users can see how much dedupe happened. This preserves the FIRST project_id seen for each unique content (a deterministic-enough behavior given readdir's sort order). Real behavioral differences between projects' versions of "the same" memory file would, of course, hash differently and import normally. Closes #1791 (sub-bug 8). --- .../cli/src/mcp-tools/memory-tools.ts | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) 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 669672d1ea..65185b81c0 100644 --- a/v3/@claude-flow/cli/src/mcp-tools/memory-tools.ts +++ b/v3/@claude-flow/cli/src/mcp-tools/memory-tools.ts @@ -13,6 +13,7 @@ import { existsSync, mkdirSync, readdirSync, readFileSync, unlinkSync, writeFileSync } from 'fs'; import { homedir } from 'os'; import { join, resolve } from 'path'; +import { createHash } from 'crypto'; import type { MCPTool } from './types.js'; import { validateIdentifier } from './validate-input.js'; @@ -711,12 +712,31 @@ export const memoryTools: MCPTool[] = [ let imported = 0; let skipped = 0; + // #1791.8 — Claude Code's `~/.claude/projects/` accumulates historical + // project_id directories (truncated forms, sandbox cwds, renamed + // workspaces) that all contain copies of the same memory files. The + // previous import indexed each copy under a different `project_id` + // prefix, producing 5–8x duplication on long-lived homes. Dedupe by + // file content hash so the same memory is imported once even if it + // appears under several project directories. + const seenContentHashes = new Set(); + let duplicatesSkipped = 0; const projects = new Set(); for (const memFile of memoryFiles) { projects.add(memFile.project); try { const content = readFileSync(memFile.path, 'utf-8'); + + // #1791.8 — Skip if we've already imported this exact content under + // a different project_id directory. + const contentHash = createHash('sha256').update(content).digest('hex').slice(0, 16); + if (seenContentHashes.has(contentHash)) { + duplicatesSkipped++; + continue; + } + seenContentHashes.add(contentHash); + const frontmatterMatch = content.match(/^---\n([\s\S]*?)\n---\n([\s\S]*)$/); let name = memFile.file.replace('.md', ''); let body = content; @@ -753,6 +773,7 @@ export const memoryTools: MCPTool[] = [ success: true, imported, skipped, + duplicatesSkipped, files: memoryFiles.length, projects: projects.size, namespace: ns,