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): open AgentDB with our better-sqlite3 so Node 24 does not abort

Dragan Spiridonovb1645c6f47ad
diff --git a/v3/@claude-flow/memory/src/agentdb-backend.ts b/v3/@claude-flow/memory/src/agentdb-backend.ts
index 83a7c6450c..894d066e66 100644
--- a/v3/@claude-flow/memory/src/agentdb-backend.ts
+++ b/v3/@claude-flow/memory/src/agentdb-backend.ts
@@ -13,6 +13,7 @@
import { withAgentdbLockRecovery, closeWithAgentdbLockRecovery } from './agentdb-lock-guard.js';
import { EventEmitter } from 'node:events';
import { safeJsonParse } from './json-security.js';
+import { useHostSqliteDriver } from './agentdb-native-driver.js';
import {
IMemoryBackend,
MemoryEntry,
@@ -222,6 +223,8 @@ export class AgentDBBackend extends EventEmitter implements IMemoryBackend {
vectorBackend: this.config.vectorBackend,
vectorDimension: this.config.vectorDimension,
});
+ // Keep agentdb's nested better-sqlite3 11.x out of the process (Node 24 abort).
+ useHostSqliteDriver(this.agentdb);
await this.agentdb.initialize();
});
} finally {
diff --git a/v3/@claude-flow/memory/src/agentdb-native-driver.test.ts b/v3/@claude-flow/memory/src/agentdb-native-driver.test.ts
new file mode 100644
index 0000000000..a50818e8e1
--- /dev/null
+++ b/v3/@claude-flow/memory/src/agentdb-native-driver.test.ts
@@ -0,0 +1,90 @@
+/**
+ * Node 24 abort in agentdb's nested better-sqlite3 11.x
+ * (`Assertion failed: (env) != nullptr` in Statement::~Statement).
+ *
+ * useHostSqliteDriver() must make AgentDB open its handle with this
+ * package's better-sqlite3, so the source-built 11.x addon is never loaded.
+ */
+
+import { describe, it, expect } from 'vitest';
+import { mkdtempSync, rmSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import Database from 'better-sqlite3';
+import { useHostSqliteDriver } from './agentdb-native-driver.js';
+
+function fakeAgentDB(config: { forceWasm?: boolean } = {}) {
+ const calls: string[] = [];
+ const own = { own: true };
+ const agentdb: {
+ config: { forceWasm?: boolean };
+ usingWasm: boolean;
+ initializeDatabase: (dbPath: string) => Promise<any>;
+ } = {
+ config,
+ usingWasm: true,
+ async initializeDatabase(dbPath: string) {
+ calls.push(dbPath);
+ return own;
+ },
+ };
+ return { agentdb, calls, own };
+}
+
+describe('useHostSqliteDriver', () => {
+ it('opens the AgentDB handle with our better-sqlite3, not agentdb\'s', async () => {
+ const dir = mkdtempSync(join(tmpdir(), 'cf-agentdb-driver-'));
+ const { agentdb, calls } = fakeAgentDB();
+ try {
+ useHostSqliteDriver(agentdb);
+ const db = await agentdb.initializeDatabase(join(dir, 'agentdb-memory.db'));
+
+ expect(db).toBeInstanceOf(Database);
+ expect(calls).toEqual([]);
+ expect(agentdb.usingWasm).toBe(false);
+ expect(db.pragma('journal_mode', { simple: true })).toBe('wal');
+ db.close();
+ } finally {
+ rmSync(dir, { recursive: true, force: true });
+ }
+ });
+
+ it('falls back to AgentDB\'s own loader when ours cannot open the path', async () => {
+ const { agentdb, calls, own } = fakeAgentDB();
+ useHostSqliteDriver(agentdb);
+ const missingDir = join(tmpdir(), 'cf-agentdb-driver-missing', 'nested', 'x.db');
+
+ await expect(agentdb.initializeDatabase(missingDir)).resolves.toBe(own);
+ expect(calls).toEqual([missingDir]);
+ });
+
+ it('leaves forceWasm instances on AgentDB\'s own loader', async () => {
+ const { agentdb, calls, own } = fakeAgentDB({ forceWasm: true });
+ useHostSqliteDriver(agentdb);
+
+ await expect(agentdb.initializeDatabase(':memory:')).resolves.toBe(own);
+ expect(calls).toEqual([':memory:']);
+ });
+
+ it('ignores objects without initializeDatabase', () => {
+ expect(() => useHostSqliteDriver(null)).not.toThrow();
+ expect(() => useHostSqliteDriver({})).not.toThrow();
+ });
+
+ it('hands the real AgentDB.initialize() a working handle', async () => {
+ const { AgentDB } = (await import('agentdb')) as any;
+ const agentdb = new AgentDB({ dbPath: ':memory:' });
+ useHostSqliteDriver(agentdb);
+ let opened: unknown;
+ const wrapped = agentdb.initializeDatabase.bind(agentdb);
+ agentdb.initializeDatabase = async (p: string) => (opened = await wrapped(p));
+ try {
+ await agentdb.initialize();
+ } catch {
+ // Embedder/vector-backend setup may fail offline; the handle is what matters.
+ }
+
+ expect(opened).toBeInstanceOf(Database);
+ (opened as Database.Database | undefined)?.close();
+ }, 60_000);
+});
diff --git a/v3/@claude-flow/memory/src/agentdb-native-driver.ts b/v3/@claude-flow/memory/src/agentdb-native-driver.ts
new file mode 100644
index 0000000000..65097a46ff
--- /dev/null
+++ b/v3/@claude-flow/memory/src/agentdb-native-driver.ts
@@ -0,0 +1,50 @@
+/**
+ * Open AgentDB's native SQLite handle with this package's better-sqlite3.
+ *
+ * agentdb 3.0.0-alpha.x declares `better-sqlite3: ^11.8.1`, so npm gives it a
+ * nested 11.x copy next to our 12.x one. better-sqlite3 11.x ships no prebuilt
+ * binary for Node 24 (ABI 137), so on Node 24 that copy is compiled from
+ * source against the local headers. Node 24.19+ headers carry a partial
+ * backport of `node::ObjectWrap` cleanup hooks (nodejs/node#65446): a
+ * Statement collected by an allocation-driven GC calls
+ * RemoveEnvironmentCleanupHook() with no Environment and the process aborts
+ * with `Assertion failed: (env) != nullptr` — exit 134 for the CLI, "Connection
+ * closed" for the MCP server.
+ *
+ * The 12.x copy we depend on ships a Node 24 prebuild, so routing AgentDB's
+ * handle through it keeps the source-built 11.x addon out of the process.
+ * `overrides` cannot do this: npm ignores them in a dependency's package.json,
+ * which is where ours sit for anyone installing ruflo.
+ */
+
+type AgentDBLike = {
+ config?: { forceWasm?: boolean };
+ usingWasm?: boolean;
+ initializeDatabase?: (dbPath: string) => Promise<unknown>;
+};
+
+/**
+ * Replace `agentdb.initializeDatabase` so `initialize()` opens the database
+ * with our better-sqlite3. Must be called before `agentdb.initialize()`.
+ * Falls back to AgentDB's own loader when ours cannot open the file, and
+ * leaves `forceWasm` instances alone.
+ */
+export function useHostSqliteDriver(agentdb: AgentDBLike | null | undefined): void {
+ if (!agentdb || typeof agentdb.initializeDatabase !== 'function') return;
+ if (agentdb.config?.forceWasm) return;
+
+ const agentdbLoader = agentdb.initializeDatabase.bind(agentdb);
+ agentdb.initializeDatabase = async (dbPath: string) => {
+ let db: { pragma(sql: string): unknown };
+ try {
+ const Database = (await import('better-sqlite3')).default;
+ db = new Database(dbPath);
+ } catch {
+ return agentdbLoader(dbPath);
+ }
+ // Same setup as AgentDB.initializeDatabase()'s native branch.
+ db.pragma('journal_mode = WAL');
+ agentdb.usingWasm = false;
+ return db;
+ };
+}
diff --git a/v3/@claude-flow/memory/src/controller-registry.ts b/v3/@claude-flow/memory/src/controller-registry.ts
index d87890b916..9abd60c7af 100644
--- a/v3/@claude-flow/memory/src/controller-registry.ts
+++ b/v3/@claude-flow/memory/src/controller-registry.ts
@@ -27,6 +27,7 @@ import type { MemoryGraphConfig } from './memory-graph.js';
import { TieredCacheManager } from './cache-manager.js';
import type { CacheConfig } from './types.js';
import { TieredMemoryStore } from './tiered-memory.js';
+import { useHostSqliteDriver } from './agentdb-native-driver.js';
// ===== Types =====
@@ -579,6 +580,8 @@ export class ControllerRegistry extends EventEmitter {
// stale) and init retried once with a fresh instance.
await withAgentdbLockRecovery(dbPath, async () => {
this.agentdb = new AgentDBClass({ dbPath });
+ // Keep agentdb's nested better-sqlite3 11.x out of the process (Node 24 abort).
+ useHostSqliteDriver(this.agentdb);
await this.agentdb.initialize();
});
} finally {