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(embeddings): serialize persistent cache initialization (#3685)

Rudy Mizrahi Celekli1865341e9405
diff --git a/v3/@claude-flow/embeddings/src/persistent-cache-initialization.test.ts b/v3/@claude-flow/embeddings/src/persistent-cache-initialization.test.ts
new file mode 100644
index 0000000000..90e8a29381
--- /dev/null
+++ b/v3/@claude-flow/embeddings/src/persistent-cache-initialization.test.ts
@@ -0,0 +1,46 @@
+import { afterEach, beforeEach, describe, it, expect, vi } from 'vitest';
+import { mkdtempSync, rmSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { PersistentEmbeddingCache } from './persistent-cache.js';
+const observed = vi.hoisted(() => ({ loads: 0 }));
+vi.mock('sql.js', async (importOriginal) => {
+ const actual = await importOriginal<typeof import('sql.js')>();
+ return {
+ ...actual,
+ default: async (...args: Parameters<typeof actual.default>) => {
+ observed.loads++;
+ return actual.default(...args);
+ },
+ };
+});
+const dirs: string[] = [];
+const caches: PersistentEmbeddingCache[] = [];
+beforeEach(() => {
+ observed.loads = 0;
+});
+afterEach(async () => {
+ for (const cache of caches.splice(0)) await cache.close();
+ for (const dir of dirs.splice(0))
+ rmSync(dir, { recursive: true, force: true });
+});
+describe('persistent cache initialization', () => {
+ it('uses one real SQLite initialization for concurrent cold callers', async () => {
+ const dir = mkdtempSync(join(tmpdir(), 'ruflo-cache-init-'));
+ dirs.push(dir);
+ const cache = new PersistentEmbeddingCache({
+ dbPath: join(dir, 'cache.sqlite'),
+ });
+ caches.push(cache);
+ await Promise.all([
+ cache.set('a', new Float32Array([1])),
+ cache.set('b', new Float32Array([2])),
+ cache.set('c', new Float32Array([3])),
+ ]);
+ expect(observed.loads).toBe(1);
+ expect((await cache.getStats()).size).toBe(3);
+ expect(Array.from((await cache.get('a'))!)).toEqual([1]);
+ expect(Array.from((await cache.get('b'))!)).toEqual([2]);
+ expect(Array.from((await cache.get('c'))!)).toEqual([3]);
+ });
+});
diff --git a/v3/@claude-flow/embeddings/src/persistent-cache.ts b/v3/@claude-flow/embeddings/src/persistent-cache.ts
index aeddc0148b..7ff8e39340 100644
--- a/v3/@claude-flow/embeddings/src/persistent-cache.ts
+++ b/v3/@claude-flow/embeddings/src/persistent-cache.ts
@@ -53,6 +53,7 @@ export class PersistentEmbeddingCache {
private db: SqlJsDatabase | null = null;
private SQL: SqlJsStatic | null = null;
private initialized = false;
+ private initializationPromise: Promise<void> | null = null;
private dirty = false;
private hits = 0;
private misses = 0;
@@ -75,7 +76,15 @@ export class PersistentEmbeddingCache {
*/
private async ensureInitialized(): Promise<void> {
if (this.initialized) return;
+ if (!this.initializationPromise) {
+ this.initializationPromise = this.initialize().finally(() => {
+ this.initializationPromise = null;
+ });
+ }
+ return this.initializationPromise;
+ }
+ private async initialize(): Promise<void> {
try {
// Dynamically import sql.js
const initSqlJs = (await import('sql.js')).default;