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): export all pages only after successful reads

Rudy Celeklidcea7abc1c82
diff --git a/v3/@claude-flow/cli/__tests__/memory-export-completeness.test.ts b/v3/@claude-flow/cli/__tests__/memory-export-completeness.test.ts
new file mode 100644
index 0000000000..9d68dce276
--- /dev/null
+++ b/v3/@claude-flow/cli/__tests__/memory-export-completeness.test.ts
@@ -0,0 +1,52 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+const layer = vi.hoisted(() => ({ listEntries: vi.fn() }));
+vi.mock('../src/memory/memory-initializer.js', () => ({
+ listEntries: layer.listEntries, storeEntry: vi.fn(), searchEntries: vi.fn(), getEntry: vi.fn(), deleteEntry: vi.fn(),
+ initializeMemoryDatabase: vi.fn(), checkMemoryInitialization: vi.fn(async () => ({ initialized: true })),
+}));
+const { memoryTools } = await import('../src/mcp-tools/memory-tools.js');
+const exporter = memoryTools.find(t => t.name === 'memory_export')!;
+let dir: string;
+let outputPath: string;
+beforeEach(() => {
+ dir = mkdtempSync(join(tmpdir(), 'ruflo-export-pages-'));
+ outputPath = join(dir, 'backup.json');
+ writeFileSync(outputPath, 'existing complete backup');
+ layer.listEntries.mockReset();
+});
+afterEach(() => rmSync(dir, { recursive: true, force: true }));
+const entry = (key: string) => ({ id: key, key, namespace: 'notes', content: `body ${key}`, hasEmbedding: false });
+describe('memory export completeness', () => {
+ it.each([false, true])('preserves the prior export when a read fails (after a page: %s)', async afterPage => {
+ if (afterPage) layer.listEntries.mockResolvedValueOnce({ success: true, entries: [entry('first')], total: 2 });
+ layer.listEntries.mockResolvedValue({ success: false, entries: [], total: 0, error: 'database read refused' });
+ const result = await exporter.handler({ outputPath, namespace: 'notes' });
+ expect(result).toMatchObject({ error: expect.stringContaining('database read refused') });
+ expect(readFileSync(outputPath, 'utf8')).toBe('existing complete backup');
+ });
+ it('reads every backend page and keeps namespace/content options', async () => {
+ const rows = Array.from({ length: 5 }, (_, i) => entry(String(i)));
+ layer.listEntries.mockImplementation(async ({ offset = 0 }) => ({ success: true, entries: rows.slice(offset, offset + 2), total: 5 }));
+ const result = await exporter.handler({ outputPath, namespace: 'notes' });
+ expect(result).toMatchObject({ exported: { entries: 5 } });
+ const data = JSON.parse(readFileSync(outputPath, 'utf8'));
+ expect(data.count).toBe(5);
+ expect(data.entries.map((e: {value: string}) => e.value)).toEqual(rows.map(e => e.content));
+ expect(layer.listEntries.mock.calls.map(([options]) => options.offset)).toEqual([0, 2, 4]);
+ expect(layer.listEntries.mock.calls.every(([options]) => options.namespace === 'notes' && options.includeContent)).toBe(true);
+ });
+ it('refuses an incomplete read instead of overwriting with a partial snapshot', async () => {
+ layer.listEntries.mockResolvedValueOnce({ success: true, entries: [entry('first')], total: 2 });
+ layer.listEntries.mockResolvedValue({ success: true, entries: [], total: 2 });
+ expect(await exporter.handler({ outputPath })).toMatchObject({ error: expect.stringMatching(/incomplete/i) });
+ expect(readFileSync(outputPath, 'utf8')).toBe('existing complete backup');
+ });
+ it('allows a successfully read empty store', async () => {
+ layer.listEntries.mockResolvedValue({ success: true, entries: [], total: 0 });
+ expect(await exporter.handler({ outputPath })).toMatchObject({ exported: { entries: 0 } });
+ expect(JSON.parse(readFileSync(outputPath, 'utf8')).entries).toEqual([]);
+ });
+});
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 6cfee268e3..0f37821c3c 100644
--- a/v3/@claude-flow/cli/src/mcp-tools/memory-tools.ts
+++ b/v3/@claude-flow/cli/src/mcp-tools/memory-tools.ts
@@ -1510,7 +1510,20 @@ export const memoryTools: MCPTool[] = [
// entry body. Without this, `value` is always null because listEntries
// strips content by default (callers pay for the JSON parse only when
// they need it).
- const all = await listEntries({ limit: 100000, namespace, includeContent: true });
+ // A successful export must cover the whole reported store. Read all
+ // pages before touching an existing snapshot, including on read failure.
+ const entries: Awaited<ReturnType<typeof listEntries>>['entries'] = [];
+ let total: number | undefined;
+ do {
+ const page = await listEntries({ limit: 10000, offset: entries.length, namespace, includeContent: true });
+ if (!page.success) return { error: page.error || 'Could not read memory for export' };
+ total ??= page.total;
+ if (page.entries.length === 0 && entries.length < total) {
+ return { error: `Incomplete memory export: read ${entries.length} of ${total} entries` };
+ }
+ entries.push(...page.entries);
+ } while (entries.length < total);
+ const all = { entries };
const payload = {
schema: 'ruflo-memory-export/v1',
exportedAt: new Date().toISOString(),