π 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
TypeScript agentic-aiagentic-frameworkagentic-workflowagentsai-agentsai-assistantai-skillsautonomous-agentsclaudecodexdsh-pluginharnessmcp-servermulti-agentmulti-agent-systemsnpmskillsswarmswarm-intelligencetypescript
fix(memory): resolve graph reads through explicit CLAUDE_FLOW_DB_PATH binding (#4291)
r14f659089fa1
diff --git a/v3/@claude-flow/cli/__tests__/graph-query-explicit-db-path-4291.test.ts b/v3/@claude-flow/cli/__tests__/graph-query-explicit-db-path-4291.test.tsnew file mode 100644index 0000000000..3396145fa6--- /dev/null+++ b/v3/@claude-flow/cli/__tests__/graph-query-explicit-db-path-4291.test.ts@@ -0,0 +1,198 @@+/**+ * #4291: `agentdb_graph-query` (k-hop mode) and `agentdb_graph-pathfinder` read+ * the graph_edges table through `getBridgeDb()` / `isBridgeDbEncryptedAtRest()`+ * in src/memory/graph-edge-writer.ts. Both functions previously resolved the+ * database file as `customDbPath ?? path.join(getMemoryRoot(), 'memory.db')`+ * β bypassing `resolveDbPath()` (src/memory/memory-initializer.ts), the+ * function that actually implements the project's documented path-priority+ * rule: an explicit `CLAUDE_FLOW_DB_PATH` binding must win over the plain+ * `memory.db` default.+ *+ * Consequence: a caller that starts a fresh MCP with+ * `CLAUDE_FLOW_MEMORY_PATH=<fixture>/.swarm` AND+ * `CLAUDE_FLOW_DB_PATH=<fixture>/.swarm/agentdb-memory.db` (an explicit,+ * deliberate store selection) silently got answers from `.swarm/memory.db`+ * instead β a source-bound graph read leaking data from a store the caller+ * did NOT select.+ *+ * This suite reproduces the exact synthetic fixture from the issue and also+ * covers: (a) unchanged default behavior with no env binding, (b) refusal+ * (never fallback) when the explicitly-bound path is missing, and (c)+ * refusal when the explicitly-bound source is encrypted at rest.+ *+ * Modeled on the mock-free, real-sqlite style of+ * __tests__/graph-edge-writer-rfe1-guard-2889.test.ts.+ */+import { afterEach, beforeAll, beforeEach, describe, expect, it } from 'vitest';+import { createRequire } from 'node:module';+import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from 'node:fs';+import { join } from 'node:path';+import { tmpdir } from 'node:os';+import { randomBytes } from 'node:crypto';++import { _resetMemoryRootCache } from '../src/memory/memory-initializer.js';+import { _resetBridgeDb, isBridgeDbEncryptedAtRest } from '../src/memory/graph-edge-writer.js';+import { agentdbGraphQuery } from '../src/mcp-tools/agentdb-tools.js';++const req = createRequire(import.meta.url);+let Database: any;+let native = true;++beforeAll(() => {+ try {+ Database = req('better-sqlite3');+ const probe = new Database(':memory:');+ probe.close();+ } catch {+ native = false;+ }+});++/**+ * Seed a graph_edges table with the given (source, target, relation) rows.+ * Must match the full production schema (graph-edge-writer.ts's own+ * `CREATE TABLE IF NOT EXISTS` + its index set) β `getBridgeDb()` always runs+ * `CREATE INDEX IF NOT EXISTS idx_graph_edges_reinforced ON graph_edges+ * (last_reinforced)` against whatever table is already there, and a+ * fixture missing that column makes the whole open throw.+ */+function seedGraphDb(dbPath: string, edges: Array<[string, string, string]>): void {+ const db = new Database(dbPath);+ db.exec(`+ CREATE TABLE IF NOT EXISTS graph_edges (+ id TEXT PRIMARY KEY,+ source_id TEXT NOT NULL,+ target_id TEXT NOT NULL,+ relation TEXT NOT NULL,+ weight REAL DEFAULT 1.0,+ confidence REAL DEFAULT 1.0,+ decay_rate REAL DEFAULT 0.0,+ last_reinforced TEXT,+ witness_id TEXT,+ embedding_ref TEXT,+ metadata TEXT,+ created_at TEXT NOT NULL+ );+ CREATE INDEX IF NOT EXISTS idx_graph_edges_source ON graph_edges (source_id);+ CREATE INDEX IF NOT EXISTS idx_graph_edges_target ON graph_edges (target_id);+ CREATE INDEX IF NOT EXISTS idx_graph_edges_relation ON graph_edges (relation);+ CREATE INDEX IF NOT EXISTS idx_graph_edges_reinforced ON graph_edges (last_reinforced);+ `);+ const insert = db.prepare(+ `INSERT INTO graph_edges (id, source_id, target_id, relation, created_at) VALUES (?, ?, ?, ?, ?)`,+ );+ edges.forEach(([source, target, relation], i) => {+ insert.run(`edge-${i}`, source, target, relation, new Date().toISOString());+ });+ db.close();+}++const ENV_KEYS = ['CLAUDE_FLOW_MEMORY_PATH', 'CLAUDE_FLOW_DB_PATH'] as const;+let savedEnv: Record<string, string | undefined>;+let root: string;+let swarmDir: string;++function resetCaches(): void {+ _resetMemoryRootCache();+ _resetBridgeDb();+}++beforeEach(() => {+ savedEnv = {};+ for (const key of ENV_KEYS) savedEnv[key] = process.env[key];+ root = mkdtempSync(join(tmpdir(), 'ruflo-4291-'));+ swarmDir = join(root, '.swarm');+ mkdirSync(swarmDir, { recursive: true });+ resetCaches();+});++afterEach(() => {+ for (const key of ENV_KEYS) {+ if (savedEnv[key] === undefined) delete process.env[key];+ else process.env[key] = savedEnv[key];+ }+ resetCaches();+ try {+ rmSync(root, { recursive: true, force: true });+ } catch {+ /* best-effort */+ }+});++const khop = (nodeId: string) =>+ agentdbGraphQuery.handler({ nodeId, mode: 'k-hop' }) as Promise<any>;++describe.skipIf(!native)(+ '#4291 agentdb_graph-query resolves graph reads through explicit CLAUDE_FLOW_DB_PATH binding',+ () => {+ it('does not leak the unselected store: CLAUDE_FLOW_DB_PATH must win over the memory.db default (exact issue repro)', async () => {+ const memoryDbPath = join(swarmDir, 'memory.db');+ const agentDbPath = join(swarmDir, 'agentdb-memory.db');+ seedGraphDb(memoryDbPath, [['entity:probe', 'entity:excluded-operation', 'related']]);+ seedGraphDb(agentDbPath, [['entity:probe', 'entity:permitted-research', 'related']]);++ process.env.CLAUDE_FLOW_MEMORY_PATH = swarmDir;+ process.env.CLAUDE_FLOW_DB_PATH = agentDbPath;+ resetCaches();++ const result = await khop('entity:probe');++ expect(result.success).toBe(true);+ const ids = (result.results as Array<{ nodeId: string }>).map((r) => r.nodeId);+ expect(ids).toContain('entity:permitted-research');+ expect(ids).not.toContain('entity:excluded-operation');+ });++ it('(a) unchanged default: resolves to memory.db when no CLAUDE_FLOW_DB_PATH binding is set', async () => {+ const memoryDbPath = join(swarmDir, 'memory.db');+ seedGraphDb(memoryDbPath, [['entity:probe', 'entity:excluded-operation', 'related']]);++ process.env.CLAUDE_FLOW_MEMORY_PATH = swarmDir;+ delete process.env.CLAUDE_FLOW_DB_PATH;+ resetCaches();++ const result = await khop('entity:probe');++ expect(result.success).toBe(true);+ const ids = (result.results as Array<{ nodeId: string }>).map((r) => r.nodeId);+ expect(ids).toContain('entity:excluded-operation');+ });++ it('(b) refuses outright β never falls back to another store β when the explicitly-bound path is missing', async () => {+ const memoryDbPath = join(swarmDir, 'memory.db');+ seedGraphDb(memoryDbPath, [['entity:probe', 'entity:excluded-operation', 'related']]);++ process.env.CLAUDE_FLOW_MEMORY_PATH = swarmDir;+ process.env.CLAUDE_FLOW_DB_PATH = join(swarmDir, 'does-not-exist.db');+ resetCaches();++ const result = await khop('entity:probe');++ expect(result.success).toBe(false);+ expect(JSON.stringify(result)).not.toContain('entity:excluded-operation');+ });++ it('(c) refuses outright β never falls back to another store β when the explicitly-bound source is encrypted at rest', async () => {+ const memoryDbPath = join(swarmDir, 'memory.db');+ seedGraphDb(memoryDbPath, [['entity:probe', 'entity:excluded-operation', 'related']]);++ const encPath = join(swarmDir, 'agentdb-memory.db');+ const { encryptBuffer } = await import('../src/encryption/vault.js');+ const key = randomBytes(32);+ writeFileSync(encPath, encryptBuffer(Buffer.from('irrelevant plaintext payload'), key));++ process.env.CLAUDE_FLOW_MEMORY_PATH = swarmDir;+ process.env.CLAUDE_FLOW_DB_PATH = encPath;+ resetCaches();++ // Sanity-check the encryption sniff itself agrees with the resolved path.+ expect(isBridgeDbEncryptedAtRest()).toBe(true);++ const result = await khop('entity:probe');++ expect(result.success).toBe(false);+ expect(JSON.stringify(result)).not.toContain('entity:excluded-operation');+ expect(result.error).toMatch(/encrypt/i);+ });+ },+);diff --git a/v3/@claude-flow/cli/src/memory/graph-edge-writer.ts b/v3/@claude-flow/cli/src/memory/graph-edge-writer.tsindex 0c460ad462..4041cd26a9 100644--- a/v3/@claude-flow/cli/src/memory/graph-edge-writer.ts+++ b/v3/@claude-flow/cli/src/memory/graph-edge-writer.ts@@ -34,7 +34,7 @@ import * as path from 'path';import * as fs from 'fs';import * as crypto from 'crypto';import { execFileSync } from 'child_process';-import { getMemoryRoot } from './memory-initializer.js';+import { resolveDbPath } from './memory-initializer.js';import { encodeEmbedding } from './embedding-quantization.js';import { loadBetterSqlite3 } from './shared-sqlite.js';import { isEncryptedBlob } from '../encryption/vault.js';@@ -134,13 +134,15 @@ function warnRfe1EncryptedOnce(dbPath: string): void {}/**- * True iff the memory.db bridge at `customDbPath` (or the default location)- * is currently RFE1-encrypted-at-rest. Callers that receive `null` from- * getBridgeDb()/insertGraphEdge() can use this to avoid recommending the- * destructive `memory init` command for this specific, non-corrupt case.+ * True iff the memory.db bridge at `customDbPath` β or, when omitted, the+ * path `resolveDbPath()` resolves from the `CLAUDE_FLOW_DB_PATH` env binding+ * / memory root default (#4291) β is currently RFE1-encrypted-at-rest.+ * Callers that receive `null` from getBridgeDb()/insertGraphEdge() can use+ * this to avoid recommending the destructive `memory init` command for this+ * specific, non-corrupt case.*/export function isBridgeDbEncryptedAtRest(customDbPath?: string): boolean {- const dbPath = customDbPath ?? path.join(getMemoryRoot(), 'memory.db');+ const dbPath = resolveDbPath(customDbPath);return fs.existsSync(dbPath) && isRfe1EncryptedAtRest(dbPath);}@@ -224,9 +226,19 @@ export function releaseBridgeDb(dbPath?: string): boolean {* #2431 fix: better-sqlite3 + WAL mode replaces sql.js + whole-file* writeFileSync. Eliminates the dual-write race that corrupted memory.db* when called alongside the memory bridge's better-sqlite3 writer.+ *+ * #4291 fix: the file is resolved via `resolveDbPath()` β the project's one+ * documented path-priority rule (explicit `customDbPath` argument wins, then+ * the `CLAUDE_FLOW_DB_PATH` env binding, then the plain `memory.db` default+ * under the memory root) β instead of a local `customDbPath ?? <default>`+ * expression that silently ignored `CLAUDE_FLOW_DB_PATH`. A caller that+ * explicitly selected a store via that env var no longer gets answers read+ * from a different, unselected database file. When the resolved file is+ * missing (and `createIfMissing` is false) or encrypted at rest, this+ * refuses (returns null) rather than falling back to any other store.*/export async function getBridgeDb(customDbPath?: string, opts?: { createIfMissing?: boolean }): Promise<any | null> {- const dbPath = customDbPath ?? path.join(getMemoryRoot(), 'memory.db');+ const dbPath = resolveDbPath(customDbPath);const createIfMissing = opts?.createIfMissing === true;if (_db && _dbPath === dbPath) { armIdleRelease(); return _db; }