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(cli): store patterns in LocalReasoningBank when it is the registered bank (#4567)

rUvcbd1986979a6
diff --git a/v3/@claude-flow/cli/__tests__/pattern-store-local-reasoningbank.test.ts b/v3/@claude-flow/cli/__tests__/pattern-store-local-reasoningbank.test.ts
new file mode 100644
index 0000000000..ea4fed8499
--- /dev/null
+++ b/v3/@claude-flow/cli/__tests__/pattern-store-local-reasoningbank.test.ts
@@ -0,0 +1,111 @@
+/**
+ * Regression: pattern store and search must use the same backend when the
+ * registered `reasoningBank` is the intelligence module's LocalReasoningBank.
+ *
+ * `@claude-flow/memory` 3.1.0 (#4550) added `ControllerRegistry.set()`, so the
+ * memory bridge's post-init wiring now really registers LocalReasoningBank
+ * whenever agentdb's own ReasoningBank is not available. LocalReasoningBank
+ * has `store()`/`findSimilar()`/`getAll()` but no `storePattern()`, so
+ * `bridgeStorePattern` skipped it and wrote to the SQL `pattern` namespace,
+ * while `bridgeSearchPatterns` read LocalReasoningBank. A bank that already
+ * held other patterns returned those as non-empty `reasoningBank` results, so
+ * the pattern just stored was never found (seen on CI as the #2226 test
+ * failing in bug-cluster-2219-2226.test.ts).
+ */
+import { afterAll, describe, expect, it } from 'vitest';
+import Database from 'better-sqlite3';
+import { mkdtempSync, rmSync } from 'node:fs';
+import { join } from 'node:path';
+import { tmpdir } from 'node:os';
+
+const root = mkdtempSync(join(tmpdir(), 'ruflo-local-rb-pattern-'));
+const db = new Database(join(root, 'memory.db'));
+
+/** Minimal stand-in for LocalReasoningBank: same method surface, in memory. */
+function makeLocalBank() {
+ const list: Array<{ id: string; type: string; content: string; confidence: number; embedding: number[] }> = [];
+ const cos = (a: number[], b: number[]) => {
+ if (!a?.length || !b?.length) return 0;
+ let dot = 0, na = 0, nb = 0;
+ for (let i = 0; i < Math.min(a.length, b.length); i++) { dot += a[i] * b[i]; na += a[i] * a[i]; nb += b[i] * b[i]; }
+ return na && nb ? dot / Math.sqrt(na * nb) : 0;
+ };
+ return {
+ flushed: 0,
+ store(p: { id: string; type: string; content: string; confidence: number; embedding: number[] }) {
+ list.push(p);
+ return true;
+ },
+ findSimilar(q: number[], opts: { k?: number; threshold?: number }) {
+ return list
+ .map(p => ({ ...p, similarity: cos(q, p.embedding) }))
+ .filter(p => p.similarity >= (opts.threshold ?? 0.5))
+ .sort((a, b) => b.similarity - a.similarity)
+ .slice(0, opts.k ?? 5);
+ },
+ getAll() { return list; },
+ flushToDisk() { this.flushed++; },
+ };
+}
+
+afterAll(async () => {
+ const { __setMemoryBridgeRegistryForTests } = await import('../src/memory/memory-bridge.js');
+ __setMemoryBridgeRegistryForTests(null);
+ db.close();
+ rmSync(root, { recursive: true, force: true });
+});
+
+describe('pattern store/search with LocalReasoningBank registered', () => {
+ it('finds a stored pattern even when the bank already holds other patterns', async () => {
+ const { __setMemoryBridgeRegistryForTests, bridgeStorePattern, bridgeSearchPatterns } = await import(
+ '../src/memory/memory-bridge.js'
+ );
+ const { generateEmbedding } = await import('../src/memory/memory-initializer.js');
+
+ const bank = makeLocalBank();
+ // A pattern learned earlier: every query scores against it, which is what
+ // hid the stored pattern behind a non-empty result set.
+ const prior = await generateEmbedding('prior unrelated routing pattern');
+ bank.store({ id: 'prior', type: 'general', content: 'prior unrelated routing pattern', confidence: 0.9, embedding: prior.embedding });
+
+ __setMemoryBridgeRegistryForTests({
+ getAgentDB: () => ({ database: db, embedder: null }),
+ get: (name: string) => (name === 'reasoningBank' ? bank : null),
+ });
+
+ const marker = `local-rb-roundtrip-${process.pid}-${Date.now()}`;
+ const stored = await bridgeStorePattern({ pattern: `Use ${marker} for session renewal`, type: 'auth-pattern', confidence: 0.9 });
+ expect(stored?.success).toBe(true);
+ expect(stored?.controller).toBe('reasoningBank');
+ expect(bank.flushed).toBeGreaterThan(0);
+
+ const found = await bridgeSearchPatterns({ query: `Use ${marker} for session renewal`, topK: 5, minConfidence: 0.1 });
+ expect(found?.controller).toBe('reasoningBank');
+ expect(found?.results.some(r => r.content.includes(marker))).toBe(true);
+ });
+
+ it('falls through to the SQL namespace when the bank has no match', async () => {
+ const { __setMemoryBridgeRegistryForTests, bridgeStoreEntry, bridgeSearchPatterns } = await import(
+ '../src/memory/memory-bridge.js'
+ );
+
+ const bank = makeLocalBank();
+ // A pattern written to SQL by an earlier fallback-path store.
+ __setMemoryBridgeRegistryForTests({ getAgentDB: () => ({ database: db, embedder: null }), get: () => null });
+ const marker = `sql-only-pattern-${process.pid}-${Date.now()}`;
+ const written = await bridgeStoreEntry({
+ key: `pattern-${marker}`,
+ value: JSON.stringify({ pattern: `Use ${marker}`, type: 'general', confidence: 0.9 }),
+ namespace: 'pattern',
+ generateEmbeddingFlag: true,
+ });
+ expect(written?.success).toBe(true);
+
+ __setMemoryBridgeRegistryForTests({
+ getAgentDB: () => ({ database: db, embedder: null }),
+ get: (name: string) => (name === 'reasoningBank' ? bank : null),
+ });
+ const found = await bridgeSearchPatterns({ query: `Use ${marker}`, topK: 5, minConfidence: 0.1 });
+ expect(found?.results.some(r => r.content.includes(marker))).toBe(true);
+ });
+});
diff --git a/v3/@claude-flow/cli/src/memory/memory-bridge.ts b/v3/@claude-flow/cli/src/memory/memory-bridge.ts
index 79bae6fdd5..c856bf1ea0 100644
--- a/v3/@claude-flow/cli/src/memory/memory-bridge.ts
+++ b/v3/@claude-flow/cli/src/memory/memory-bridge.ts
@@ -2569,6 +2569,39 @@ export async function bridgeStorePattern(options: {
};
}
+ // The intelligence module's LocalReasoningBank (registered when agentdb's
+ // own ReasoningBank is unavailable) has store()/findSimilar() but no
+ // storePattern(). bridgeSearchPatterns reads it through findSimilar(), so
+ // the write must land there too, with an embedding from the same
+ // generateEmbedding() the search uses. Otherwise store and search split
+ // across two backends and a stored pattern is never found.
+ if (reasoningBank && typeof reasoningBank.store === 'function' && typeof reasoningBank.findSimilar === 'function') {
+ let embedding: number[] = [];
+ try {
+ const { generateEmbedding } = await import('./memory-initializer.js');
+ const emb = await generateEmbedding(options.pattern);
+ if (emb && Array.isArray(emb.embedding)) embedding = emb.embedding;
+ } catch { /* no embedder — findSimilar misses it, the getAll() substring scan still finds it */ }
+ // store() returns false when it merges into an existing pattern with the
+ // same content; that is still a successful write.
+ reasoningBank.store({
+ id: patternId,
+ type: options.type,
+ content: options.pattern,
+ confidence: options.confidence,
+ embedding,
+ metadata: options.metadata,
+ });
+ // Saves are debounced; a short-lived CLI process can exit first.
+ if (typeof reasoningBank.flushToDisk === 'function') reasoningBank.flushToDisk();
+ return {
+ success: true,
+ patternId,
+ controller: 'reasoningBank',
+ hasEmbedding: embedding.length > 0,
+ };
+ }
+
// Fallback: store via bridge SQL
const patternValue = JSON.stringify({ pattern: options.pattern, type: options.type, confidence: options.confidence, metadata: options.metadata });
const result = await bridgeStoreEntry({
@@ -2708,7 +2741,10 @@ export async function bridgeSearchPatterns(options: {
.map((p: any) => ({ id: p.id ?? '', content: p.content ?? '', score: p.confidence ?? 0 }));
}
- return { results: mapped, controller: 'reasoningBank' };
+ // Nothing in the bank: patterns written by the SQL fallback path (before
+ // the bank was registered, or by a process without it) live in the
+ // `pattern` namespace, so search that instead of returning empty.
+ if (mapped.length > 0) return { results: mapped, controller: 'reasoningBank' };
}
// Fallback: search via bridge