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

test(memory): cover the WASM-missing disable path and concurrent auto-build (#4203) (#4319)

Gss19fb5c6e7cbf
diff --git a/v3/@claude-flow/cli/__tests__/issue-4203-rabitq-auto-build.test.ts b/v3/@claude-flow/cli/__tests__/issue-4203-rabitq-auto-build.test.ts
index 5db35afe07..c592068e4a 100644
--- a/v3/@claude-flow/cli/__tests__/issue-4203-rabitq-auto-build.test.ts
+++ b/v3/@claude-flow/cli/__tests__/issue-4203-rabitq-auto-build.test.ts
@@ -287,4 +287,37 @@ describe('#4203 RaBitQ index is built on demand instead of only via the manual t
rmSync(root, { recursive: true, force: true });
}
});
+
+ // Review follow-up: the throttle writes `lastAutoBuildAttempt` synchronously
+ // before its first await, so callers that arrive together must not each start
+ // a build. Exactly one owns it; the rest report "no index yet" and let their
+ // caller keep the exhaustive scan.
+ it('concurrent ensureRabitqIndex() calls start a single build', async () => {
+ const root = mkdtempSync(join(tmpdir(), 'ruflo-4203-concurrent-'));
+ try {
+ const dbPath = await seedStore(root);
+ if (!dbPath) return; // bridge unavailable in this environment
+
+ const rabitq = await import('../src/memory/rabitq-index.js');
+ expect(rabitq.getRabitqStatus().initialized).toBe(false);
+
+ const results = await Promise.all([
+ rabitq.ensureRabitqIndex({ dbPath, dimensions: DIMENSIONS }),
+ rabitq.ensureRabitqIndex({ dbPath, dimensions: DIMENSIONS }),
+ rabitq.ensureRabitqIndex({ dbPath, dimensions: DIMENSIONS }),
+ rabitq.ensureRabitqIndex({ dbPath, dimensions: DIMENSIONS }),
+ ]);
+
+ expect(results.filter(r => r === true).length).toBe(1);
+ expect(rabitq.getRabitqStatus().initialized).toBe(true);
+ expect(rabitq.getRabitqStatus().vectorCount).toBe(ROW_COUNT);
+
+ // The winner's build is the one that answers searches afterwards.
+ const hits = await rabitq.searchRabitq(vectorFor(0), { k: 2 });
+ expect(hits).not.toBeNull();
+ expect(hits![0].key).toBe('emb-0');
+ } finally {
+ rmSync(root, { recursive: true, force: true });
+ }
+ });
});
diff --git a/v3/@claude-flow/cli/__tests__/issue-4203-rabitq-wasm-missing.test.ts b/v3/@claude-flow/cli/__tests__/issue-4203-rabitq-wasm-missing.test.ts
new file mode 100644
index 0000000000..bf0025c3f3
--- /dev/null
+++ b/v3/@claude-flow/cli/__tests__/issue-4203-rabitq-wasm-missing.test.ts
@@ -0,0 +1,93 @@
+import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
+import { mkdtempSync, rmSync } from 'node:fs';
+import { join } from 'node:path';
+import { tmpdir } from 'node:os';
+
+// #4203 follow-up: `ensureRabitqIndex()` flips a process-level `autoBuildDisabled`
+// flag when the optional WASM module is missing, so a store without RaBitQ stops
+// paying a failed dynamic import on every search. That branch had no coverage,
+// because issue-4203-rabitq-auto-build.test.ts runs with the module installed.
+//
+// The mock throws on import, which is what `loadRabitqModule()` sees when the
+// optional dependency is not present.
+const wasm = vi.hoisted(() => ({ imports: 0 }));
+
+vi.mock('@ruvector/rabitq-wasm', () => {
+ wasm.imports++;
+ throw new Error('@ruvector/rabitq-wasm not available');
+});
+
+const DIMENSIONS = 384;
+const ROW_COUNT = 12;
+
+function vectorFor(seed: number): number[] {
+ const out: number[] = [];
+ for (let i = 0; i < DIMENSIONS; i++) {
+ out.push(Math.sin(seed * 0.7 + i * 0.11) * Math.cos(seed * 1.3 + i * 0.03));
+ }
+ return out;
+}
+
+/** Seed a real store so the 10-vector floor cannot short-circuit the build. */
+async function seedStore(root: string): Promise<string | null> {
+ const dbPath = join(root, 'memory.db');
+ const bridge = await import('../src/memory/memory-bridge.js');
+ const { initializeMemoryDatabase } = await import('../src/memory/memory-initializer.js');
+
+ await initializeMemoryDatabase({ dbPath, force: true, verbose: false });
+
+ const status = await bridge.bridgeGetVectorSearchStatus(dbPath);
+ if (!status) return null; // bridge unavailable in this environment
+
+ for (let i = 0; i < ROW_COUNT; i++) {
+ const id = `00000000-0000-4000-8000-${String(i).padStart(12, '0')}`;
+ const written = await bridge.bridgeAddEmbedding(
+ id,
+ vectorFor(i),
+ { id, key: `emb-${i}`, namespace: 'test', content: `row ${i}` },
+ dbPath,
+ );
+ if (written !== true) return null;
+ }
+
+ return dbPath;
+}
+
+describe('#4203 follow-up: a missing WASM module disables auto-build for the process', () => {
+ beforeEach(() => {
+ vi.resetModules();
+ wasm.imports = 0;
+ });
+
+ afterEach(() => {
+ vi.restoreAllMocks();
+ });
+
+ it('stops retrying once the WASM module is known to be missing', async () => {
+ const root = mkdtempSync(join(tmpdir(), 'ruflo-4203-nowasm-'));
+ try {
+ const dbPath = await seedStore(root);
+ if (!dbPath) return; // bridge unavailable in this environment
+
+ const rabitq = await import('../src/memory/rabitq-index.js');
+
+ expect(await rabitq.ensureRabitqIndex({ dbPath, dimensions: DIMENSIONS })).toBe(false);
+ expect(wasm.imports).toBe(1);
+ expect(rabitq.getRabitqStatus().initialized).toBe(false);
+
+ // Move past the 60s throttle. Without `autoBuildDisabled` this call would
+ // try the import again, so an unchanged counter isolates that flag from
+ // the throttle it would otherwise be indistinguishable from.
+ const realNow = Date.now;
+ vi.spyOn(Date, 'now').mockImplementation(() => realNow() + 10 * 60_000);
+
+ expect(await rabitq.ensureRabitqIndex({ dbPath, dimensions: DIMENSIONS })).toBe(false);
+ expect(wasm.imports).toBe(1);
+
+ // Callers keep their exhaustive path: no index, and no throw.
+ expect(await rabitq.searchRabitq(vectorFor(0), { k: 3 })).toBeNull();
+ } finally {
+ rmSync(root, { recursive: true, force: true });
+ }
+ });
+});