diff --git a/v3/@claude-flow/cli/.claude/commands/memory/memory-persist.md b/v3/@claude-flow/cli/.claude/commands/memory/memory-persist.md index c3651d0c46..33beed11ea 100644 --- a/v3/@claude-flow/cli/.claude/commands/memory/memory-persist.md +++ b/v3/@claude-flow/cli/.claude/commands/memory/memory-persist.md @@ -4,22 +4,25 @@ Persist memory across sessions. ## Usage ```bash -npx @claude-flow/cli@latest memory persist [options] +npx @claude-flow/cli@latest memory export --output [options] +npx @claude-flow/cli@latest memory import --input [options] ``` ## Options -- `--export ` - Export to file -- `--import ` - Import from file -- `--compress` - Compress memory data +- `--output ` - JSON export output file +- `--input ` - JSON export file to import +- `--namespace ` (export) - Limit the export to one namespace +- `--namespace ` (import) - Store every imported entry under this namespace, overriding namespaces in the export +- `--merge` - Merge is the default; update (overwrite) entries with the same key in the target namespace +- `--no-merge` - Leave entries whose key already exists in the target namespace unchanged and report them as failed + +Exports are JSON; embeddings are not included (the import re-embeds). ## Examples ```bash # Export memory -npx @claude-flow/cli@latest memory persist --export memory-backup.json +npx @claude-flow/cli@latest memory export --output memory-backup.json # Import memory -npx @claude-flow/cli@latest memory persist --import memory-backup.json - -# Compressed export -npx @claude-flow/cli@latest memory persist --export memory.gz --compress +npx @claude-flow/cli@latest memory import --input memory-backup.json ``` diff --git a/v3/@claude-flow/cli/.claude/commands/memory/memory-search.md b/v3/@claude-flow/cli/.claude/commands/memory/memory-search.md index 7079addc4e..f662861ca2 100644 --- a/v3/@claude-flow/cli/.claude/commands/memory/memory-search.md +++ b/v3/@claude-flow/cli/.claude/commands/memory/memory-search.md @@ -9,7 +9,7 @@ npx @claude-flow/cli@latest memory search [options] ## Options - `--query ` - Search query -- `--pattern ` - Pattern matching +- `--type ` - Search type (semantic, keyword, hybrid) - `--limit ` - Result limit ## Examples @@ -17,8 +17,8 @@ npx @claude-flow/cli@latest memory search [options] # Search memory npx @claude-flow/cli@latest memory search --query "authentication" -# Pattern search -npx @claude-flow/cli@latest memory search --pattern "api-.*" +# Keyword search +npx @claude-flow/cli@latest memory search --query "api" --type keyword # Limited results npx @claude-flow/cli@latest memory search --query "config" --limit 10 diff --git a/v3/@claude-flow/cli/.claude/commands/memory/memory-usage.md b/v3/@claude-flow/cli/.claude/commands/memory/memory-usage.md index 3c163a37db..a4f8188c45 100644 --- a/v3/@claude-flow/cli/.claude/commands/memory/memory-usage.md +++ b/v3/@claude-flow/cli/.claude/commands/memory/memory-usage.md @@ -4,22 +4,37 @@ Manage persistent memory storage. ## Usage ```bash -npx @claude-flow/cli@latest memory usage [options] +npx @claude-flow/cli@latest memory store --key --value [--namespace ] +npx @claude-flow/cli@latest memory retrieve --key [--namespace ] +npx @claude-flow/cli@latest memory list [--namespace ] [--limit ] +npx @claude-flow/cli@latest memory delete --key [--namespace ] [--force] +npx @claude-flow/cli@latest memory purge --namespace [--dry-run] [--force] ``` +`memory purge` permanently deletes every entry in a namespace. + ## Options -- `--action ` - Action (store, retrieve, list, clear) -- `--key ` - Memory key -- `--value ` - Data to store (JSON) +- `--key ` - Memory key for store, retrieve, or delete +- `--value ` - Data to store +- `--namespace ` - Memory namespace +- `--limit ` - Maximum entries to list +- `--force` - Delete without confirmation +- `--dry-run` - Report how many entries a purge would permanently delete without deleting them ## Examples ```bash # Store memory -npx @claude-flow/cli@latest memory usage --action store --key "project-config" --value '{"api": "v2"}' +npx @claude-flow/cli@latest memory store --key "project-config" --value '{"api": "v2"}' # Retrieve memory -npx @claude-flow/cli@latest memory usage --action retrieve --key "project-config" +npx @claude-flow/cli@latest memory retrieve --key "project-config" -# List all keys -npx @claude-flow/cli@latest memory usage --action list +# List entries +npx @claude-flow/cli@latest memory list --limit 20 + +# Delete one key +npx @claude-flow/cli@latest memory delete --key "project-config" --force + +# Preview permanently deleting every entry in a namespace +npx @claude-flow/cli@latest memory purge --namespace "stale-cache" --dry-run ``` diff --git a/v3/@claude-flow/cli/__tests__/init-memory-commands-kit-5167.test.ts b/v3/@claude-flow/cli/__tests__/init-memory-commands-kit-5167.test.ts new file mode 100644 index 0000000000..9aeb11eaff --- /dev/null +++ b/v3/@claude-flow/cli/__tests__/init-memory-commands-kit-5167.test.ts @@ -0,0 +1,123 @@ +import { describe, expect, it, vi } from 'vitest'; +import { existsSync, readFileSync } from 'node:fs'; +import { join } from 'node:path'; +import { fileURLToPath } from 'node:url'; + +vi.mock('../src/output.js', () => ({ + output: new Proxy({}, { get: () => (value: unknown) => value }), +})); +vi.mock('../src/prompt.js', () => ({ select: vi.fn(), confirm: vi.fn(), input: vi.fn() })); +vi.mock('../src/mcp-client.js', () => ({ callMCPTool: vi.fn(), MCPClientError: class extends Error {} })); +vi.mock('../src/mcp-tools/path-containment.js', () => ({ localCliContext: () => ({}) })); +vi.mock('../src/mcp-tools/validate-input.js', () => ({ + validateIdentifier: () => ({ valid: true }), +})); +vi.mock('../src/mcp-tools/memory-tools.js', () => ({ + memoryKeyError: () => null, + MEMORY_SCOPES: ['project', 'local', 'user'], + resolveScopedNamespace: ({ namespace }: { namespace: string }) => ({ ok: true, namespace }), +})); +vi.mock('../src/commands/memory-distill.js', () => ({ distillCommand: {} })); +vi.mock('../src/commands/memory-backup.js', () => ({ backupCommand: {} })); +vi.mock('../src/memory/memory-initializer.js', () => ({ resolveDbPath: (path?: string) => path ?? 'memory.db' })); + +import { memoryCommand } from '../src/commands/memory.js'; +import { extractInvocations, loadCommandTree, resolveCommand } from '../../../../scripts/audit-skill-cli-commands.mjs'; + +const MEMORY_COMMANDS_ROOT = join( + fileURLToPath(new URL('..', import.meta.url)), + '.claude', + 'commands', + 'memory', +); +const PAGES = ['memory-usage.md', 'memory-persist.md', 'memory-search.md']; + +function commandFlags(subcommand: string): Set { + const command = memoryCommand.subcommands?.find( + (candidate) => candidate.name === subcommand || candidate.aliases?.includes(subcommand), + ); + expect(command, `memory ${subcommand} must be registered`).toBeDefined(); + + const flags = new Set(); + for (const option of command?.options ?? []) { + flags.add(`--${option.name}`); + if (option.short) flags.add(`-${option.short}`); + } + return flags; +} + +function flagsOnLine(line: string): string[] { + return [...line.matchAll(/(^|[\s\[])(--?[a-z][a-z0-9-]*)\b/g)].map((match) => match[2]); +} + +function declaredFlag(subcommand: string, flag: string): string { + if (!flag.startsWith('--no-')) return flag; + + const baseName = flag.slice('--no-'.length); + const command = memoryCommand.subcommands?.find( + (candidate) => candidate.name === subcommand || candidate.aliases?.includes(subcommand), + ); + const option = command?.options?.find((candidate) => candidate.name === baseName); + return option?.type === 'boolean' ? `--${baseName}` : flag; +} + +function optionsSection(markdown: string): string[] { + const section = markdown.match(/## Options\s*\n([\s\S]*?)(?=\n## |$)/)?.[1] ?? ''; + return [...section.matchAll(/`(--[a-z][a-z0-9-]*)\b/g)].map((match) => match[1]); +} + +describe('default init memory command pages use the CLI that v3 ships', () => { + it('ships the default memory command directory', () => { + expect(existsSync(MEMORY_COMMANDS_ROOT)).toBe(true); + }); + + it.each(PAGES)('%s references only registered memory subcommands and their declared options', async (page) => { + const tree = await loadCommandTree(); + + const file = join(MEMORY_COMMANDS_ROOT, page); + expect(existsSync(file), `${page} must remain in the init command kit`).toBe(true); + + const markdown = readFileSync(file, 'utf8'); + const lines = markdown.split('\n'); + const invocations = extractInvocations(markdown); + expect(invocations.length, `${page} must contain at least one CLI invocation`).toBeGreaterThan(0); + + const pageSubcommands = new Set(); + for (const invocation of invocations) { + const command = resolveCommand(tree, invocation.command); + expect(command, `${page}:${invocation.line} must use a registered command`).toBe('memory'); + + const subcommands = tree.get(command!)?.subcommands; + const subcommand = invocation.subcommand; + const resolvedSubcommand = subcommand && subcommands + ? (subcommands.has(subcommand) + ? subcommand + : [...subcommands].find(([, aliases]) => aliases.includes(subcommand))?.[0]) + : null; + expect( + resolvedSubcommand, + `${page}:${invocation.line} must use a registered memory subcommand`, + ).toBeTruthy(); + + pageSubcommands.add(resolvedSubcommand!); + const declared = commandFlags(resolvedSubcommand!); + for (const flag of flagsOnLine(lines[invocation.line - 1])) { + expect( + declared.has(declaredFlag(resolvedSubcommand!, flag)), + `${page}:${invocation.line} memory ${resolvedSubcommand} does not declare ${flag}`, + ).toBe(true); + } + } + + const listedOptions = optionsSection(markdown); + expect(listedOptions.length, `${page} must document at least one option`).toBeGreaterThan(0); + for (const option of listedOptions) { + expect( + [...pageSubcommands].some((subcommand) => ( + commandFlags(subcommand).has(declaredFlag(subcommand, option)) + )), + `${page} lists ${option}, but none of its example subcommands declare it`, + ).toBe(true); + } + }, 120_000); +});