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): memory_migrate reports what it migrated and force clears the checked marker (#4194)

Rudy Celeklicfb0ed8c9ea1
diff --git a/v3/@claude-flow/cli/__tests__/memory-migrate-honest-4194.test.ts b/v3/@claude-flow/cli/__tests__/memory-migrate-honest-4194.test.ts
new file mode 100644
index 0000000000..81d370d558
--- /dev/null
+++ b/v3/@claude-flow/cli/__tests__/memory-migrate-honest-4194.test.ts
@@ -0,0 +1,69 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+
+const mocks = vi.hoisted(() => ({ store: vi.fn() }));
+vi.mock('../src/memory/memory-initializer.js', () => ({
+ storeEntry: mocks.store,
+ searchEntries: vi.fn(),
+ listEntries: vi.fn(),
+ getEntry: vi.fn(),
+ deleteEntry: vi.fn(),
+ initializeMemoryDatabase: vi.fn(),
+ checkMemoryInitialization: vi.fn(async () => ({ initialized: true })),
+ resolveDbPath: () => '.swarm/memory.db',
+ getHNSWStatus: () => ({ algorithm: 'brute-force' }),
+}));
+import { memoryTools } from '../src/mcp-tools/memory-tools.js';
+
+const migrate = memoryTools.find(t => t.name === 'memory_migrate')!;
+const MARKER = '.claude-flow/memory/.migrated-to-sqlite';
+let cwd: string;
+let dir: string;
+
+beforeEach(() => {
+ cwd = process.cwd();
+ dir = mkdtempSync(join(tmpdir(), 'memory-migrate-4194-'));
+ mkdirSync(join(dir, '.claude-flow/memory'), { recursive: true });
+ writeFileSync(join(dir, '.claude-flow/memory/store.json'), JSON.stringify({
+ version: '1',
+ entries: { a: { key: 'a', value: 'one' }, b: { key: 'b', value: 'two' } },
+ }));
+ process.chdir(dir);
+ mocks.store.mockReset().mockResolvedValue({ success: true });
+ vi.spyOn(console, 'error').mockImplementation(() => {});
+});
+afterEach(() => {
+ process.chdir(cwd);
+ rmSync(dir, { recursive: true, force: true });
+ vi.restoreAllMocks();
+});
+
+describe('memory_migrate reports what it moved (#4194)', () => {
+ it('does not claim a migration when the marker already exists', async () => {
+ writeFileSync(join(dir, MARKER), '{}');
+ const result = await migrate.handler({}) as any;
+ expect(result.migrated).toBe(0);
+ expect(result.message).not.toBe('Migration completed');
+ expect(mocks.store).not.toHaveBeenCalled();
+ });
+
+ it('force clears the marker that is actually checked and re-runs', async () => {
+ writeFileSync(join(dir, MARKER), '{}');
+ const result = await migrate.handler({ force: true }) as any;
+ expect(mocks.store).toHaveBeenCalledTimes(2);
+ expect(result).toMatchObject({ success: true, message: 'Migration completed', migrated: 2, total: 2 });
+ expect(existsSync(join(dir, MARKER))).toBe(true);
+ });
+
+ it('reports the real count when some entries fail', async () => {
+ mocks.store.mockImplementation(async (e: { key: string }) => {
+ if (e.key === 'b') throw new Error('boom');
+ return { success: true };
+ });
+ const result = await migrate.handler({}) as any;
+ expect(result).toMatchObject({ success: true, migrated: 1, total: 2 });
+ expect(result.message).toContain('1 of 2');
+ });
+});
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 b0bbfbb54d..f667373fce 100644
--- a/v3/@claude-flow/cli/src/mcp-tools/memory-tools.ts
+++ b/v3/@claude-flow/cli/src/mcp-tools/memory-tools.ts
@@ -244,15 +244,31 @@ function loadLegacyStore(): LegacyMemoryStore | null {
/**
* Mark migration as complete
*/
-function markMigrationComplete(): void {
+function markMigrationComplete(counts?: { migrated: number; total: number }): void {
const legacyDir = resolve(LEGACY_MEMORY_DIR);
if (!existsSync(legacyDir)) mkdirSync(legacyDir, { recursive: true });
writeFileSync(resolve(join(LEGACY_MEMORY_DIR, MIGRATION_MARKER)), JSON.stringify({
migratedAt: new Date().toISOString(),
version: '3.0.0',
+ ...(counts ?? {}),
}), 'utf-8');
}
+/**
+ * Counts recorded by the migration that wrote the marker, or null when no
+ * marker (or an older marker without counts) is present.
+ */
+function readMigrationCounts(): { migrated: number; total: number } | null {
+ try {
+ const marker = JSON.parse(readFileSync(resolve(join(LEGACY_MEMORY_DIR, MIGRATION_MARKER)), 'utf-8'));
+ return typeof marker?.migrated === 'number' && typeof marker?.total === 'number'
+ ? { migrated: marker.migrated, total: marker.total }
+ : null;
+ } catch {
+ return null;
+ }
+}
+
/**
* Lazy-load memory initializer functions to avoid circular deps
*/
@@ -446,7 +462,7 @@ async function ensureInitialized(dbPath?: string): Promise<void> {
}
console.error(`[MCP Memory] Migrated ${migrated}/${Object.keys(legacyStore.entries).length} entries`);
- markMigrationComplete();
+ markMigrationComplete({ migrated, total: Object.keys(legacyStore.entries).length });
}
}
}
@@ -1046,11 +1062,17 @@ export const memoryTools: MCPTool[] = [
handler: async (input) => {
const force = input.force as boolean;
- // Remove migration marker if forcing
+ // Remove the migration marker if forcing. hasLegacyStore() checks the marker
+ // beside the legacy store, so that is the one force must clear (#4194);
+ // the old .swarm marker path is cleared too for compatibility.
if (force) {
- const markerPath = getMigrationMarkerPath();
- if (existsSync(markerPath)) {
- unlinkSync(markerPath);
+ for (const markerPath of [
+ resolve(join(LEGACY_MEMORY_DIR, MIGRATION_MARKER)),
+ getMigrationMarkerPath(),
+ ]) {
+ if (existsSync(markerPath)) {
+ unlinkSync(markerPath);
+ }
}
}
@@ -1064,13 +1086,38 @@ export const memoryTools: MCPTool[] = [
};
}
+ // A marker beside the store means a migration already ran; ensureInitialized
+ // would move nothing, so say so instead of reporting the store's size.
+ if (!hasLegacyStore()) {
+ return {
+ success: true,
+ message: 'Legacy store already migrated (marker present); nothing was moved. Pass force: true to re-run.',
+ migrated: 0,
+ total: Object.keys(legacyStore.entries).length,
+ };
+ }
+
// Run migration via ensureInitialized
await ensureInitialized();
+ // ensureInitialized swallows errors and records what it moved in the marker.
+ const counts = readMigrationCounts();
+ if (!counts) {
+ return {
+ success: false,
+ message: 'Migration did not complete; no entries were recorded as migrated. See the MCP server log.',
+ migrated: 0,
+ total: Object.keys(legacyStore.entries).length,
+ };
+ }
+
return {
success: true,
- message: 'Migration completed',
- migrated: Object.keys(legacyStore.entries).length,
+ message: counts.migrated === counts.total
+ ? 'Migration completed'
+ : `Migration completed with failures: ${counts.migrated} of ${counts.total} entries migrated`,
+ migrated: counts.migrated,
+ total: counts.total,
backend: await describeBackend(),
};
},