From 093298ef53cef8c7894ec47e70cfa3f59c99a0a2 Mon Sep 17 00:00:00 2001 From: Tanishq Date: Wed, 4 Nov 2026 00:06:43 +0530 Subject: [PATCH] fix(memory): match Claude Code's project slug for all non-alphanumerics in memory_import_claude Claude Code names ~/.claude/projects/ by replacing every character other than [A-Za-z0-9] with `-`. resolveProjectMemoryDir() only tried slugs that replace slashes, backslashes, `:` and whitespace, so for a project path with `_`, `.` or the like (/home/alice/my_app, a Windows user folder such as john.doe, a WSL /mnt/c/.../my_proj path) memory_import_claude found nothing and returned "No Claude memory files found", with or without projectPath. Also try Claude Code's rule after the existing candidates of each path, and for a WSL path on its Windows form too. The cost tracker's _encode-project-path.mjs uses the same rule (#4366). --- .../memory-import-claude-slug.test.ts | 82 +++++++++++++++++++ .../cli/src/mcp-tools/memory-tools.ts | 8 ++ 2 files changed, 90 insertions(+) create mode 100644 v3/@claude-flow/cli/__tests__/memory-import-claude-slug.test.ts diff --git a/v3/@claude-flow/cli/__tests__/memory-import-claude-slug.test.ts b/v3/@claude-flow/cli/__tests__/memory-import-claude-slug.test.ts new file mode 100644 index 0000000000..87712293ee --- /dev/null +++ b/v3/@claude-flow/cli/__tests__/memory-import-claude-slug.test.ts @@ -0,0 +1,82 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join, win32 } from 'node:path'; + +// Store and validator mocks modeled on memory-import-claude-reimport-4197.test.ts, without its +// duplicate-key check, which single imports do not need. +const layer = vi.hoisted(() => ({ rows: new Map() })); +vi.mock('../src/memory/memory-initializer.js', () => ({ + storeEntry: vi.fn(async (o: { key: string; value: string; namespace: string }) => { + const id = `${o.namespace}/${o.key}`; + layer.rows.set(id, o.value); + return { success: true, id }; + }), + searchEntries: vi.fn(), listEntries: vi.fn(), getEntry: vi.fn(), deleteEntry: vi.fn(), + checkMemoryInitialization: vi.fn(async () => ({ initialized: true })), + initializeMemoryDatabase: vi.fn(async () => ({ success: true })), + generateEmbedding: vi.fn(async () => ({ backend: 'mock' })), +})); +vi.mock('../src/mcp-tools/validate-input.js', () => ({ validateIdentifier: () => ({ valid: true }) })); + +const { memoryTools } = await import('../src/mcp-tools/memory-tools.js'); +const importer = memoryTools.find(t => t.name === 'memory_import_claude')!; + +// Claude Code names ~/.claude/projects/ by replacing every character other +// than [A-Za-z0-9] with '-' (plugins/ruflo-cost-tracker/scripts/_encode-project-path.mjs). +const claudeCodeSlug = (projectPath: string) => projectPath.replace(/[^a-zA-Z0-9]/g, '-'); + +let home: string; +const savedEnv = new Map(); +const writeMemory = (slug: string) => { + const memoryDir = join(home, '.claude', 'projects', slug, 'memory'); + mkdirSync(memoryDir, { recursive: true }); + writeFileSync(join(memoryDir, 'MEMORY.md'), `## Notes\n${'memory Claude Code keeps for this project '.repeat(5)}`); +}; +const storedKeys = () => [...layer.rows.keys()]; + +beforeEach(() => { + for (const key of ['HOME', 'USERPROFILE', 'CLAUDE_FLOW_CWD']) savedEnv.set(key, process.env[key]); + delete process.env.CLAUDE_FLOW_CWD; + home = mkdtempSync(join(tmpdir(), 'ruflo-claude-slug-')); + // os.homedir() reads HOME on Linux and macOS, and USERPROFILE on Windows. + process.env.HOME = home; + process.env.USERPROFILE = home; + layer.rows.clear(); +}); +afterEach(() => { + for (const [key, value] of savedEnv) { + if (value === undefined) delete process.env[key]; + else process.env[key] = value; + } + rmSync(home, { recursive: true, force: true }); +}); + +describe('memory_import_claude finds the folder Claude Code names for the project', () => { + it.each([ + ['a POSIX path with "_" and "."', '/work/my_app.v2', '-work-my-app-v2'], + ['a Windows path with "." in the user name', win32.normalize('C:/Users/john.doe/repo'), 'C--Users-john-doe-repo'], + ['a WSL path to a Windows project with "_"', '/mnt/c/Users/x/my_proj', 'C--Users-x-my-proj'], + ])('imports %s', async (_name, projectPath, slug) => { + writeMemory(slug); + expect(await importer.handler({ projectPath })).toMatchObject({ success: true, imported: 1 }); + expect(storedKeys()[0]).toContain(`claude:${slug}:`); + }); + + it('imports the current project (CLAUDE_FLOW_CWD) when its folder name has "_" and "."', async () => { + const project = join(home, 'my_app.v2'); + mkdirSync(project); + writeMemory(claudeCodeSlug(project)); + process.env.CLAUDE_FLOW_CWD = project; + expect(await importer.handler({})).toMatchObject({ success: true, imported: 1 }); + expect(storedKeys()[0]).toContain(`claude:${claudeCodeSlug(project)}:`); + }); + + it('still imports a path the existing slugs matched, and nothing for another project', async () => { + writeMemory('-work-plain-app'); + expect(await importer.handler({ projectPath: '/work/plain-app' })).toMatchObject({ success: true, imported: 1 }); + layer.rows.clear(); + expect(await importer.handler({ projectPath: '/work/other_app' })).toMatchObject({ success: true, imported: 0 }); + expect(layer.rows.size).toBe(0); + }); +}); 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 718ec23121..bf7247c61f 100644 --- a/v3/@claude-flow/cli/src/mcp-tools/memory-tools.ts +++ b/v3/@claude-flow/cli/src/mcp-tools/memory-tools.ts @@ -235,6 +235,14 @@ function resolveProjectMemoryDir(claudeProjectsDir: string, projectPathOverride? if (/^[A-Za-z]:[\\/]/.test(source)) { candidates.add(source.replace(/[:\\/]/g, '-').replace(/\s+/g, '-')); } + + // Candidate 6: Claude Code's own rule, which replaces every character + // other than [A-Za-z0-9] with `-` (the cost tracker's + // _encode-project-path.mjs does the same, #4366). The candidates above + // keep `_`, `.` and the like, so `/work/my_app` never found its folder + // `-work-my-app`. A WSL path is also tried in its Windows form. + candidates.add(source.replace(/[^a-zA-Z0-9]/g, '-')); + if (wsl) candidates.add(`${wsl[1].toUpperCase()}:${wsl[2] ?? ''}`.replace(/[^a-zA-Z0-9]/g, '-')); } for (const projectHash of candidates) {