fix(memory): content-hash dedupe in memory_import_claude (#1791.8) (#1808)

`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<string>` 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).
This commit is contained in:
rUv authored and GitHub committed 2026-05-06 16:52:20 -04:00
1 parent 122193a454
commit 3eb6b4d656
1 file changed
+21
@@ -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<string>();
let duplicatesSkipped = 0;
const projects = new Set<string>();
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,