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): settle hybrid shutdown, real-path sql.js lock test for AgentDBBackend

rdd8fd3cda8a3
diff --git a/v3/@claude-flow/memory/agentdb.rvf.idmap.json b/v3/@claude-flow/memory/agentdb.rvf.idmap.json
deleted file mode 100644
index 48a7cbe14d..0000000000
--- a/v3/@claude-flow/memory/agentdb.rvf.idmap.json
+++ /dev/null
@@ -1 +0,0 @@
-{"idToLabel":{},"labelToId":{},"nextLabel":1}
\ No newline at end of file
diff --git a/v3/@claude-flow/memory/src/agentdb-backend-lock.test.ts b/v3/@claude-flow/memory/src/agentdb-backend-lock.test.ts
new file mode 100644
index 0000000000..b33b095d59
--- /dev/null
+++ b/v3/@claude-flow/memory/src/agentdb-backend-lock.test.ts
@@ -0,0 +1,62 @@
+/**
+ * Unmocked end-to-end: AgentDBBackend (forceWasm) -> real AgentDB -> real
+ * agentdb sql.js save with a stale dead-PID `<db>.agentdb.lock`.
+ * Persistence is verified with better-sqlite3 (not sql.js).
+ */
+import { describe, it, expect, beforeAll, beforeEach, afterEach } from 'vitest';
+import * as fs from 'node:fs';
+import * as os from 'node:os';
+import * as path from 'node:path';
+import Database from 'better-sqlite3';
+import { AgentDBBackend } from './agentdb-backend.js';
+import { AgentdbLockError } from './agentdb-lock-guard.js';
+
+const DEAD_PID = 2_147_483_000;
+let dir: string;
+let dbFile: string;
+let lockFile: string;
+
+function plant(pid: number): void {
+ fs.writeFileSync(lockFile, `${pid}\n`);
+ const old = new Date(Date.now() - 3600_000);
+ fs.utimesSync(lockFile, old, old);
+}
+const mk = () => new AgentDBBackend({ dbPath: dbFile, namespace: 'lock', vectorDimension: 384, forceWasm: true });
+
+// AgentDB's forceWasm still picks better-sqlite3 inside db-fallback unless this is set.
+beforeAll(() => { process.env.AGENTDB_FORCE_SQLJS = '1'; });
+
+beforeEach(() => {
+ dir = fs.mkdtempSync(path.join(os.tmpdir(), 'backend-lock-'));
+ dbFile = path.join(dir, 'agentdb-memory.db');
+ lockFile = `${dbFile}.agentdb.lock`;
+});
+afterEach(() => fs.rmSync(dir, { recursive: true, force: true }));
+
+describe('AgentDBBackend with real sql.js agentdb and a lock file', () => {
+ it('dead-PID lock: initialize + store + shutdown persists the row and clears the lock', async () => {
+ plant(DEAD_PID);
+ const b = mk();
+ await b.initialize();
+ const raw = (b as any).getAgentDB().database;
+ expect(raw.constructor.name).toBe('SqlJsDatabase'); // proves the locked sql.js path is exercised
+ raw.exec('CREATE TABLE memory_entries(content TEXT)');
+ raw.exec("INSERT INTO memory_entries VALUES('survives stale lock')");
+ await b.shutdown();
+ expect(fs.existsSync(lockFile)).toBe(false);
+ const db = new Database(dbFile, { readonly: true });
+ try {
+ const rows = db.prepare('SELECT content FROM memory_entries').all() as Array<{ content: string }>;
+ expect(rows.map((r) => r.content)).toContain('survives stale lock');
+ } finally { db.close(); }
+ });
+
+ it('live-holder lock: shutdown rejects with AgentdbLockError and leaves the lock', async () => {
+ const b = mk();
+ await b.initialize();
+ (b as any).getAgentDB().database.exec('CREATE TABLE t(x)');
+ plant(process.ppid);
+ await expect(b.shutdown()).rejects.toBeInstanceOf(AgentdbLockError);
+ expect(fs.existsSync(lockFile)).toBe(true);
+ });
+});
diff --git a/v3/@claude-flow/memory/src/hybrid-backend.ts b/v3/@claude-flow/memory/src/hybrid-backend.ts
index 9270377765..9ac2cbeac8 100644
--- a/v3/@claude-flow/memory/src/hybrid-backend.ts
+++ b/v3/@claude-flow/memory/src/hybrid-backend.ts
@@ -213,10 +213,16 @@ export class HybridBackend extends EventEmitter implements IMemoryBackend {
async shutdown(): Promise<void> {
if (!this.initialized) return;
- await Promise.all([this.sqlite.shutdown(), this.agentdb.shutdown()]);
+ // Settle both so one backend's failure (e.g. an AgentDB persist failure,
+ // which must not be swallowed) never leaves the other open or this
+ // backend stuck "initialized"; the first failure is rethrown afterwards.
+ const results = await Promise.allSettled([this.sqlite.shutdown(), this.agentdb.shutdown()]);
this.initialized = false;
this.emit('shutdown');
+
+ const failed = results.find((r): r is PromiseRejectedResult => r.status === 'rejected');
+ if (failed) throw failed.reason;
}
/**