Skip to content

armgit / ruflo

Public Imported from GitHub
0

🌊 The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated

fix(memory): confirm cleanup before dispatching deletion

Rudy Celekli87b26cea6848
diff --git a/v3/@claude-flow/cli/__tests__/memory-cleanup-confirmation.test.ts b/v3/@claude-flow/cli/__tests__/memory-cleanup-confirmation.test.ts
new file mode 100644
index 0000000000..d40873b95f
--- /dev/null
+++ b/v3/@claude-flow/cli/__tests__/memory-cleanup-confirmation.test.ts
@@ -0,0 +1,59 @@
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+const mocks = vi.hoisted(() => ({ call: vi.fn(), confirm: vi.fn(), events: [] as string[] }));
+vi.mock('../src/mcp-client.js', () => ({ callMCPTool: mocks.call, MCPClientError: class extends Error {} }));
+vi.mock('../src/prompt.js', () => ({ confirm: mocks.confirm }));
+vi.mock('../src/output.js', () => ({ output: new Proxy({}, { get: () => (value: unknown) => value }) }));
+vi.mock('../src/commands/memory-distill.js', () => ({ distillCommand: {} }));
+vi.mock('../src/commands/memory-backup.js', () => ({ backupCommand: {} }));
+vi.mock('../src/memory/memory-initializer.js', () => ({ resolveDbPath: (p: string) => p }));
+import { memoryCommand } from '../src/commands/memory.js';
+const cleanup = memoryCommand.subcommands!.find(c => c.name === 'cleanup')!;
+const run = (flags: Record<string, unknown> = {}) => cleanup.action!({ args: [], flags: { dryRun: false, force: false, ...flags } } as any);
+
+beforeEach(() => {
+ mocks.events.length = 0;
+ mocks.call.mockReset().mockImplementation(async (_name, args) => {
+ mocks.events.push(args.dryRun ? 'preview' : 'delete');
+ return { dryRun: args.dryRun, candidates: { expired: 2, stale: 0, lowQuality: 0, total: 2 },
+ deleted: { entries: args.dryRun ? 0 : 2, vectors: 0, patterns: 0 }, freed: { bytes: 10, formatted: '10 B' }, duration: 1 };
+ });
+ mocks.confirm.mockReset().mockImplementation(async () => { mocks.events.push('confirm'); return false; });
+});
+
+describe('memory cleanup destructive confirmation', () => {
+ it('declining cleanup never dispatches a deleting call', async () => {
+ await run();
+ expect(mocks.events).toEqual(['preview', 'confirm']);
+ });
+ it('deletes only after confirmation and keeps the preview selectors', async () => {
+ mocks.confirm.mockImplementation(async () => { mocks.events.push('confirm'); return true; });
+ await run({ namespace: 'commands', olderThan: '7d' });
+ expect(mocks.events).toEqual(['preview', 'confirm', 'delete']);
+ expect(mocks.call.mock.calls[1][1]).toMatchObject({ dryRun: false, namespace: 'commands', olderThan: '7d' });
+ });
+ it('dry run previews without confirmation or deletion', async () => {
+ await run({ dryRun: true });
+ expect(mocks.events).toEqual(['preview']);
+ });
+ it('force explicitly authorizes a single deleting call', async () => {
+ await run({ force: true });
+ expect(mocks.events).toEqual(['delete']);
+ });
+ it('JSON automation must choose a preview or explicitly force deletion', async () => {
+ const result = await run({ format: 'json' });
+ expect(result).toMatchObject({ success: false, exitCode: 1 });
+ expect(mocks.call).not.toHaveBeenCalled();
+ });
+ it('a failed preview cannot reach confirmation or mutation', async () => {
+ mocks.call.mockRejectedValueOnce(new Error('database unavailable'));
+ const result = await run();
+ expect(result.success).toBe(false);
+ expect(mocks.call).toHaveBeenCalledTimes(1);
+ expect(mocks.call.mock.calls[0][1].dryRun).toBe(true);
+ expect(mocks.confirm).not.toHaveBeenCalled();
+ });
+ it('JSON with --force deletes without an interactive prompt', async () => {
+ await run({ format: 'json', force: true });
+ expect(mocks.events).toEqual(['delete']);
+ });
+});
diff --git a/v3/@claude-flow/cli/src/commands/memory.ts b/v3/@claude-flow/cli/src/commands/memory.ts
index ca06bcaeae..14d68ddf5f 100644
--- a/v3/@claude-flow/cli/src/commands/memory.ts
+++ b/v3/@claude-flow/cli/src/commands/memory.ts
@@ -1341,8 +1341,13 @@ const cleanupCommand: Command = {
{ command: 'claude-flow memory cleanup --expired-only', description: 'Clean expired entries' }
],
action: async (ctx: CommandContext): Promise<CommandResult> => {
- const dryRun = ctx.flags.dryRun as boolean;
- const force = ctx.flags.force as boolean;
+ const dryRun = ctx.flags.dryRun === true;
+ const force = ctx.flags.force === true;
+
+ if (ctx.flags.format === 'json' && !dryRun && !force) {
+ output.printJson({ success: false, error: 'Use --dry-run to preview or --force to authorize cleanup with --format json.' });
+ return { success: false, exitCode: 1 };
+ }
if (dryRun) {
output.writeln(output.warning('DRY RUN - No changes will be made'));
@@ -1351,7 +1356,14 @@ const cleanupCommand: Command = {
output.printInfo('Analyzing memory for cleanup...');
try {
- const result = await callMCPTool<{
+ const selectors = {
+ olderThan: ctx.flags.olderThan,
+ expiredOnly: ctx.flags.expiredOnly,
+ lowQualityThreshold: ctx.flags.lowQuality,
+ namespace: ctx.flags.namespace,
+ };
+ // Preview before asking: dryRun:false executes deletion inside the tool.
+ let result = await callMCPTool<{
dryRun: boolean;
candidates: {
expired: number;
@@ -1370,11 +1382,8 @@ const cleanupCommand: Command = {
};
duration: number;
}>('memory_cleanup', {
- dryRun,
- olderThan: ctx.flags.olderThan,
- expiredOnly: ctx.flags.expiredOnly,
- lowQualityThreshold: ctx.flags.lowQuality,
- namespace: ctx.flags.namespace,
+ ...selectors,
+ dryRun: dryRun || !force,
});
if (ctx.flags.format === 'json') {
@@ -1407,6 +1416,7 @@ const cleanupCommand: Command = {
output.printInfo('Cleanup cancelled');
return { success: true, data: result };
}
+ result = await callMCPTool<typeof result>('memory_cleanup', { ...selectors, dryRun: false });
}
if (!dryRun) {