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): keep mirror removals native-safe

Rudy Celekli55699d774a96
diff --git a/v3/@claude-flow/cli/__tests__/memory-cli-mirror-removal.test.ts b/v3/@claude-flow/cli/__tests__/memory-cli-mirror-removal.test.ts
index 4dbe824715..c52f560a2b 100644
--- a/v3/@claude-flow/cli/__tests__/memory-cli-mirror-removal.test.ts
+++ b/v3/@claude-flow/cli/__tests__/memory-cli-mirror-removal.test.ts
@@ -1,13 +1,14 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
-import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
+import { mkdtempSync, rmSync, writeFileSync, readFileSync } from 'node:fs';
import { join } from 'node:path';
import { tmpdir } from 'node:os';
vi.mock('../src/output.js', () => ({ output: new Proxy({}, { get: () => vi.fn() }) }));
vi.mock('../src/prompt.js', () => ({ confirm: vi.fn(async () => true), select: vi.fn(), input: vi.fn() }));
vi.mock('../src/mcp-client.js', () => ({ callMCPTool: vi.fn(), MCPClientError: class extends Error {} }));
+import { writeFileRestricted } from '../src/fs-secure.js';
import { memoryCommand } from '../src/commands/memory.js';
-import { _resetMemoryRootCache, initializeMemoryDatabase, storeEntry, listEntries } from '../src/memory/memory-initializer.js';
+import { _resetMemoryRootCache, initializeMemoryDatabase, storeEntry, listEntries, purgeNamespace } from '../src/memory/memory-initializer.js';
let dir: string;
let primary: string;
@@ -25,6 +26,7 @@ beforeEach(async () => {
vi.stubEnv('CLAUDE_FLOW_MEMORY_PATH', dir);
vi.stubEnv('CLAUDE_FLOW_DISABLE_BRIDGE', '1');
vi.stubEnv('CLAUDE_FLOW_DB_PATH', '');
+ vi.stubEnv('CLAUDE_FLOW_ENCRYPT_AT_REST', '0');
_resetMemoryRootCache();
primary = join(dir, 'memory.db');
mirror = join(dir, 'agentdb-memory.db');
@@ -50,6 +52,41 @@ describe('CLI removal of default memory and its AgentDB mirror', () => {
expect(await count(mirror, 'keep')).toBe(1);
});
+ it.each(['delete', 'purge', 'preview'])('%s preserves a plaintext mirror when the primary is encrypted', async command => {
+ vi.stubEnv('CLAUDE_FLOW_ENCRYPT_AT_REST', '1');
+ vi.stubEnv('CLAUDE_FLOW_ENCRYPTION_KEY', '11'.repeat(32));
+ writeFileRestricted(primary, readFileSync(primary), { encrypt: true });
+ expect(readFileSync(primary).subarray(0, 4).toString()).toBe('RFE1');
+ expect(readFileSync(mirror).subarray(0, 16).toString()).toBe('SQLite format 3\0');
+ expect((await invoke(command === 'preview' ? 'purge' : command, { dryRun: command === 'preview' })).success).toBe(true);
+ // Inspect raw bytes before any list helper can rewrite schema metadata.
+ expect(readFileSync(primary).subarray(0, 4).toString()).toBe('RFE1');
+ expect(readFileSync(mirror).subarray(0, 16).toString()).toBe('SQLite format 3\0');
+ // Open the native file directly with SQLite, bypassing the decrypting reader.
+ const SQL = await (await import('sql.js')).default();
+ const db = new SQL.Database(readFileSync(mirror));
+ try {
+ expect(db.exec("SELECT COUNT(*) FROM memory_entries WHERE namespace = 'scratch' AND status = 'active'")[0].values[0][0]).toBe(command === 'preview' ? 1 : 0);
+ expect(db.exec("SELECT COUNT(*) FROM memory_entries WHERE namespace = 'keep'")[0].values[0][0]).toBe(1);
+ } finally { db.close(); }
+ });
+
+ it.each(['delete', 'purge', 'preview'])('%s refuses to rewrite a mirror with native WAL sidecars', async command => {
+ const before = readFileSync(mirror);
+ writeFileSync(`${mirror}-wal`, 'owned by another native process');
+ expect(await invoke(command === 'preview' ? 'purge' : command, { dryRun: command === 'preview' }))
+ .toMatchObject({ success: false, exitCode: 1 });
+ expect(readFileSync(mirror)).toEqual(before);
+ });
+
+ it('refuses native WAL sidecars in the purge fallback after preview', async () => {
+ const before = readFileSync(mirror);
+ writeFileSync(`${mirror}-wal`, 'native writer acquired after preview');
+ expect(await purgeNamespace({ namespace: 'scratch', dbPath: mirror, encryptWrites: false }))
+ .toMatchObject({ success: false });
+ expect(readFileSync(mirror)).toEqual(before);
+ });
+
it('refuses an unconfirmed purge without changing either store', async () => {
expect(await invoke('purge', { force: false })).toMatchObject({ success: false, exitCode: 1 });
expect(await count(primary)).toBe(1);
diff --git a/v3/@claude-flow/cli/src/commands/memory.ts b/v3/@claude-flow/cli/src/commands/memory.ts
index a7d38e0ff8..81e32a56eb 100644
--- a/v3/@claude-flow/cli/src/commands/memory.ts
+++ b/v3/@claude-flow/cli/src/commands/memory.ts
@@ -35,15 +35,21 @@ async function warnIfSiblingHasRows(pathFlag: unknown): Promise<void> {
}
/** Default CLI writes may be mirrored into AgentDB; remove both copies. */
-function removalDbPaths(pathFlag?: string): string[] {
+function removalDbTargets(pathFlag?: string): Array<{ dbPath: string; encryptWrites?: boolean }> {
const primary = resolveDbPath(pathFlag);
// An explicit file selection is a single-store operation, as documented by
// --path. Do not widen a caller's requested destructive scope.
- if (pathFlag || process.env.CLAUDE_FLOW_DB_PATH) return [primary];
+ if (pathFlag || process.env.CLAUDE_FLOW_DB_PATH) return [{ dbPath: primary }];
const sibling = siblingAgentDbPath(primary);
- const paths = [primary, ...(sibling ? [sibling] : [])].filter(existsSync);
+ // The AgentDB mirror is consumed by native SQLite and must stay plaintext.
+ // Pass this per operation, including preview migrations; never toggle the
+ // process encryption setting while asynchronous operations are in flight.
+ const targets = [
+ { dbPath: primary },
+ ...(sibling ? [{ dbPath: sibling, encryptWrites: false }] : []),
+ ].filter(target => existsSync(target.dbPath));
// Preserve the existing missing-database error when neither store exists.
- return paths.length ? paths : [primary];
+ return targets.length ? targets : [{ dbPath: primary }];
}
// Memory backends
@@ -950,10 +956,11 @@ const deleteCommand: Command = {
// Use sql.js directly for consistent data access (Issue #980)
try {
const { deleteEntry } = await import('../memory/memory-initializer.js');
- const paths = removalDbPaths(ctx.flags.path as string | undefined);
+ const paths = removalDbTargets(ctx.flags.path as string | undefined);
const stores = [];
- for (const dbPath of paths) {
- const entry = await deleteEntry({ key, namespace, dbPath });
+ for (const target of paths) {
+ const { dbPath } = target;
+ const entry = await deleteEntry({ key, namespace, ...target });
if (!entry.success) throw new Error(`${dbPath}: ${entry.error || 'Failed to delete'}`);
stores.push({ dbPath, ...entry });
}
@@ -1028,11 +1035,11 @@ const purgeCommand: Command = {
try {
const { listEntries, purgeNamespace } = await import('../memory/memory-initializer.js');
- const paths = removalDbPaths(dbPath);
+ const paths = removalDbTargets(dbPath);
let previewCount = 0;
for (const target of paths) {
- const preview = await listEntries({ namespace, limit: 1, dbPath: target });
- if (!preview.success) throw new Error(`${target}: ${preview.error || 'Failed to read purge target'}`);
+ const preview = await listEntries({ namespace, limit: 1, ...target });
+ if (!preview.success) throw new Error(`${target.dbPath}: ${preview.error || 'Failed to read purge target'}`);
previewCount += preview.total ?? preview.entries?.length ?? 0;
}
@@ -1057,9 +1064,9 @@ const purgeCommand: Command = {
const stores = [];
for (const target of paths) {
- const purged = await purgeNamespace({ namespace, dbPath: target });
- if (!purged.success) throw new Error(`${target}: ${purged.error || 'Failed to purge'}`);
- stores.push({ dbPath: target, ...purged });
+ const purged = await purgeNamespace({ namespace, ...target });
+ if (!purged.success) throw new Error(`${target.dbPath}: ${purged.error || 'Failed to purge'}`);
+ stores.push({ dbPath: target.dbPath, ...purged });
}
const result = {
success: true, stores,
diff --git a/v3/@claude-flow/cli/src/memory/memory-initializer.ts b/v3/@claude-flow/cli/src/memory/memory-initializer.ts
index 634c4df7ea..9708008662 100644
--- a/v3/@claude-flow/cli/src/memory/memory-initializer.ts
+++ b/v3/@claude-flow/cli/src/memory/memory-initializer.ts
@@ -1300,7 +1300,7 @@ export interface MemoryInitResult {
* Ensure memory_entries table has all required columns
* Adds missing columns for older databases (e.g., 'content' column)
*/
-export async function ensureSchemaColumns(dbPath: string): Promise<{
+export async function ensureSchemaColumns(dbPath: string, options: { encryptWrites?: boolean } = {}): Promise<{
success: boolean;
columnsAdded: string[];
error?: string;
@@ -1384,7 +1384,7 @@ export async function ensureSchemaColumns(dbPath: string): Promise<{
if (modified) {
// Save updated database
const data = db.export();
- writeFileRestricted(dbPath, Buffer.from(data), { encrypt: true });
+ writeFileRestricted(dbPath, Buffer.from(data), { encrypt: options.encryptWrites ?? true });
}
db.close();
@@ -3418,6 +3418,8 @@ export async function listEntries(options: {
limit?: number;
offset?: number;
dbPath?: string;
+ /** Internal native-mirror writes must remain plaintext, including schema migration. */
+ encryptWrites?: boolean;
/** #2073: When true, include the entry's full `content` string in each result. */
includeContent?: boolean;
/** ADR-323: restrict rows to these provenance types. */
@@ -3476,8 +3478,15 @@ export async function listEntries(options: {
return { success: false, entries: [], total: 0, error: 'Database not found' };
}
+ // Listing can migrate/backfill the schema, so it is also a whole-image
+ // writer. The newly selected native mirror may still have a live WAL.
+ await releaseOwnNativeHandle(dbPath);
+ if (hasNativeWalSidecars(dbPath)) {
+ return { success: false, entries: [], total: 0, error: await walRefusalError('read/write') };
+ }
+
// Ensure schema has all required columns (migration for older DBs)
- await ensureSchemaColumns(dbPath);
+ await ensureSchemaColumns(dbPath, options);
const initSqlJs = (await import('sql.js')).default;
const SQL = await initSqlJs();
@@ -3742,6 +3751,8 @@ export async function deleteEntry(options: {
key: string;
namespace?: string;
dbPath?: string;
+ /** Internal native-mirror writes must remain plaintext, including schema migration. */
+ encryptWrites?: boolean;
}): Promise<{
success: boolean;
deleted: boolean;
@@ -3804,7 +3815,7 @@ export async function deleteEntry(options: {
// writer's flush resurrects the row this call just tombstoned.
return await withMemoryDbLock(dbPath, async () => {
// Ensure schema has all required columns (migration for older DBs)
- await ensureSchemaColumns(dbPath);
+ await ensureSchemaColumns(dbPath, options);
const initSqlJs = (await import('sql.js')).default;
const SQL = await initSqlJs();
@@ -3861,7 +3872,7 @@ export async function deleteEntry(options: {
// Save updated database
const data = db.export();
- writeFileRestricted(dbPath, Buffer.from(data), { encrypt: true });
+ writeFileRestricted(dbPath, Buffer.from(data), { encrypt: options.encryptWrites ?? true });
db.close();
@@ -3987,6 +3998,8 @@ const NAMESPACE_PATTERN = /^[A-Za-z0-9._-]{1,128}$/;
export async function purgeNamespace(options: {
namespace: string;
dbPath?: string;
+ /** Internal native-mirror writes must remain plaintext, including schema migration. */
+ encryptWrites?: boolean;
}): Promise<{
success: boolean;
deletedCount: number;
@@ -4028,7 +4041,13 @@ export async function purgeNamespace(options: {
return { success: false, deletedCount: 0, remainingEntries: 0, error: 'Database not found' };
}
- await ensureSchemaColumns(dbPath);
+ // Recheck at mutation time even when the CLI already read a preview.
+ await releaseOwnNativeHandle(dbPath);
+ if (hasNativeWalSidecars(dbPath)) {
+ return { success: false, deletedCount: 0, remainingEntries: 0, error: await walRefusalError('write') };
+ }
+
+ await ensureSchemaColumns(dbPath, options);
const initSqlJs = (await import('sql.js')).default;
const SQL = await initSqlJs();
@@ -4045,7 +4064,7 @@ export async function purgeNamespace(options: {
const remainingEntries = (countResult[0]?.values?.[0]?.[0] as number) || 0;
const data = db.export();
- writeFileRestricted(dbPath, Buffer.from(data), { encrypt: true });
+ writeFileRestricted(dbPath, Buffer.from(data), { encrypt: options.encryptWrites ?? true });
db.close();
if (deletedCount > 0 && hnswIndex?.entries) {