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): use the ReasoningBank embedder instead of the hash fallback

555aaditya1ec4cddcfa5d
diff --git a/v3/@claude-flow/cli/__tests__/fixtures/agentic-flow-root-probe/hooks.mjs b/v3/@claude-flow/cli/__tests__/fixtures/agentic-flow-root-probe/hooks.mjs
index d9fb9d162f..670c27d7c6 100644
--- a/v3/@claude-flow/cli/__tests__/fixtures/agentic-flow-root-probe/hooks.mjs
+++ b/v3/@claude-flow/cli/__tests__/fixtures/agentic-flow-root-probe/hooks.mjs
@@ -1,11 +1,24 @@
// Loader hooks for agentic-flow-root-import.test.ts. Replaces agentic-flow and the
// embedders ahead of it with in-memory modules, so the test never loads (or runs)
// the real agentic-flow CLI. The fake root entry only reports that it was evaluated.
+// PROBE_REASONINGBANK=float32|dataview gives reasoningbank a computeEmbedding that
+// resolves to a 384-d Float32Array (what agentic-flow returns) or a DataView over one.
+const REASONINGBANK_EMBED = `
+ export const retrieveMemories = undefined;
+ export async function computeEmbedding(text) {
+ let h = 0;
+ for (let i = 0; i < text.length; i++) h = (h * 31 + text.charCodeAt(i)) | 0;
+ const v = Float32Array.from({ length: 384 }, (_, i) => Math.sin(h + i * 7) + 1.5);
+ process.stderr.write('REASONINGBANK_EMBED ' + text + '\\n');
+ return ${process.env.PROBE_REASONINGBANK === 'dataview' ? 'new DataView(v.buffer)' : 'v'};
+ }`;
const FAKES = {
'agentic-flow': "process.stderr.write('AGENTIC_FLOW_ROOT_EVALUATED\\n'); export const embeddings = undefined;",
'agentic-flow/reasoningbank': process.env.PROBE_REASONINGBANK === 'throw'
? "throw new Error('reasoningbank unavailable');"
- : 'export const computeEmbedding = undefined; export const retrieveMemories = undefined;',
+ : ['float32', 'dataview'].includes(process.env.PROBE_REASONINGBANK)
+ ? REASONINGBANK_EMBED
+ : 'export const computeEmbedding = undefined; export const retrieveMemories = undefined;',
'@huggingface/transformers': 'export const pipeline = undefined;',
'@xenova/transformers': 'export const pipeline = undefined;',
ruvector: 'export const initOnnxEmbedder = undefined; export const getOptimizedOnnxEmbedder = undefined;',
diff --git a/v3/@claude-flow/cli/__tests__/reasoningbank-embedder-3113-3114.test.ts b/v3/@claude-flow/cli/__tests__/reasoningbank-embedder-3113-3114.test.ts
new file mode 100644
index 0000000000..7caf11de2e
--- /dev/null
+++ b/v3/@claude-flow/cli/__tests__/reasoningbank-embedder-3113-3114.test.ts
@@ -0,0 +1,71 @@
+/**
+ * Regressions for #3113 and #3114: the agentic-flow ReasoningBank embedder
+ * loaded but was never used.
+ *
+ * #3113: the loader registered it as `{ embed: computeEmbedding }`, but
+ * generateLocalEmbedding() only calls the model when it is a function, so
+ * every store took the hash fallback (`model: 'hash-fallback'`). It also
+ * advertised 768 dimensions for a 384-dim model.
+ *
+ * #3114: computeEmbedding() resolves to a Float32Array, which has no `.data`
+ * and fails Array.isArray(), so even a callable model's output was discarded.
+ * A DataView is also an ArrayBuffer view but has no iterator (Array.from is
+ * []), so it must keep falling back instead of returning an empty vector.
+ *
+ * The vitest config externalizes `agentic-flow*`, so vi.mock cannot replace
+ * it; like agentic-flow-root-import.test.ts, the source runs in a child
+ * process under tsx with the agentic-flow-root-probe loader hooks, which make
+ * every embedder ahead of ReasoningBank unavailable.
+ */
+import { describe, expect, it } from 'vitest';
+import { spawnSync } from 'node:child_process';
+import { join, resolve } from 'node:path';
+import { pathToFileURL } from 'node:url';
+
+const CLI = resolve(__dirname, '..');
+const PROBE = join(__dirname, 'fixtures', 'agentic-flow-root-probe', 'register.mjs');
+const INIT = JSON.stringify(pathToFileURL(join(CLI, 'src', 'memory', 'memory-initializer.ts')).href);
+
+function runProbe(mode: 'float32' | 'dataview') {
+ const script = `
+ const init = await import(${INIT});
+ const loaded = await init.loadEmbeddingModel();
+ const out = await init.generateLocalEmbedding('hello world');
+ console.log('RESULT=' + JSON.stringify({ loaded, model: out.model, backend: out.backend, dimensions: out.dimensions, head: out.embedding.slice(0, 3), length: out.embedding.length }));
+ `;
+ const child = spawnSync(process.execPath, ['--import', 'tsx', '--import', pathToFileURL(PROBE).href, '--input-type=module', '-e', script], {
+ cwd: CLI,
+ env: { ...process.env, CLAUDE_FLOW_DISABLE_BRIDGE: '1', RUFLO_REQUIRE_REAL_EMBEDDINGS: '', PROBE_REASONINGBANK: mode },
+ encoding: 'utf8',
+ timeout: 120_000,
+ });
+ const line = child.stdout.split('\n').find((l) => l.startsWith('RESULT='));
+ return { result: line ? JSON.parse(line.slice('RESULT='.length)) : null, stderr: child.stderr };
+}
+
+// Same deterministic vector the fixture returns.
+function expectedHead(text: string): number[] {
+ let h = 0;
+ for (let i = 0; i < text.length; i++) h = (h * 31 + text.charCodeAt(i)) | 0;
+ return [0, 1, 2].map((i) => Math.fround(Math.sin(h + i * 7) + 1.5));
+}
+
+describe('ReasoningBank embedder is used by generateLocalEmbedding (#3113, #3114)', () => {
+ it('uses the Float32Array embedding as a real 384-d vector instead of the hash fallback', () => {
+ const { result, stderr } = runProbe('float32');
+ expect(result).not.toBeNull();
+ // #3113: the model is registered with its real dimensions.
+ expect(result.loaded).toMatchObject({ success: true, modelName: 'agentic-flow/reasoningbank', dimensions: 384 });
+ // Pre-fix: computeEmbedding was never called and the result was model 'hash-fallback'.
+ expect(stderr).toContain('REASONINGBANK_EMBED hello world');
+ expect(result).toMatchObject({ model: 'onnx', backend: 'onnx', dimensions: 384, length: 384 });
+ expect(result.head).toEqual(expectedHead('hello world'));
+ }, 150_000);
+
+ it('still falls back to the hash embedding for a DataView result (no empty "real" vector)', () => {
+ const { result } = runProbe('dataview');
+ expect(result).not.toBeNull();
+ expect(result).toMatchObject({ model: 'hash-fallback', backend: 'mock' });
+ expect(result.length).toBeGreaterThan(0);
+ }, 150_000);
+});
diff --git a/v3/@claude-flow/cli/src/memory/memory-initializer.ts b/v3/@claude-flow/cli/src/memory/memory-initializer.ts
index 3d788d8e38..38bb512473 100644
--- a/v3/@claude-flow/cli/src/memory/memory-initializer.ts
+++ b/v3/@claude-flow/cli/src/memory/memory-initializer.ts
@@ -2408,16 +2408,20 @@ async function loadLocalEmbeddingChain(verbose = false, startTime = Date.now()):
console.log('Loading agentic-flow ReasoningBank embedding model...');
}
+ // #3113: register a callable, like the ruvector branch below —
+ // generateLocalEmbedding() only invokes function models. The bundled
+ // model is all-MiniLM-L6-v2 (384-dim, per agentic-flow's
+ // getEmbeddingDimensions()).
embeddingModelState = {
loaded: true,
- model: { embed: reasoningBank.computeEmbedding },
+ model: (text: string) => reasoningBank.computeEmbedding(text),
tokenizer: null,
- dimensions: 768
+ dimensions: 384
};
return {
success: true,
- dimensions: 768,
+ dimensions: 384,
modelName: 'agentic-flow/reasoningbank',
loadTime: Date.now() - startTime
};
@@ -2570,10 +2574,13 @@ export async function generateLocalEmbedding(text: string): Promise<{
if (state.model && typeof (state.model as any) === 'function') {
try {
const output = await (state.model as any)(text, { pooling: 'mean', normalize: true });
- // Handle both @xenova/transformers (output.data) and ruvector (plain array) formats
+ // Handle @xenova/transformers (output.data), ruvector (plain array) and
+ // typed-array (ReasoningBank Float32Array, #3114) formats. DataView is an
+ // ArrayBuffer view without an iterator (Array.from gives []), so exclude it.
const embedding = output?.data
? Array.from(output.data as Float32Array)
- : Array.isArray(output) ? output : null;
+ : Array.isArray(output) ? output
+ : ArrayBuffer.isView(output) && !(output instanceof DataView) ? Array.from(output as Float32Array) : null;
if (embedding) {
return {
embedding,