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(ai-team): prevent invalidated builds restoring stale indexes

Rudy Celekli6e245cb0ce16
diff --git a/plugins/ruflo-ai-team/src/vector-memory.mjs b/plugins/ruflo-ai-team/src/vector-memory.mjs
index 57ee936d1a..9282c4f2a5 100644
--- a/plugins/ruflo-ai-team/src/vector-memory.mjs
+++ b/plugins/ruflo-ai-team/src/vector-memory.mjs
@@ -16,8 +16,10 @@ export function featureVector(text, dimensions = 256) {
const cosine = (a, b) => { let total = 0; for (let i = 0; i < a.length; i++) total += a[i] * b[i]; return total; };
export class TenantVectorMemory {
- constructor(store, { dimensions = 256, VectorDb, maxIndexes = 64 } = {}) { this.store=store; this.dimensions=dimensions; this.VectorDb=VectorDb; this.maxIndexes=maxIndexes; this.indexes=new Map(); }
- #rememberIndex(key, value) {
+ constructor(store, { dimensions = 256, VectorDb, maxIndexes = 64 } = {}) { this.store=store; this.dimensions=dimensions; this.VectorDb=VectorDb; this.maxIndexes=maxIndexes; this.indexes=new Map(); this.generation=0; }
+ #rememberIndex(key, value, generation) {
+ // A mutation during asynchronous construction must keep this snapshot uncached.
+ if (generation !== this.generation) return value;
if (this.indexes.has(key)) this.indexes.delete(key);
this.indexes.set(key, value);
while (this.indexes.size > this.maxIndexes) this.indexes.delete(this.indexes.keys().next().value);
@@ -25,15 +27,16 @@ export class TenantVectorMemory {
}
async #index(tenantId, teamId) {
const key=`${tenantId}:${teamId||'*'}`; if(this.indexes.has(key))return this.indexes.get(key);
+ const generation=this.generation;
const memories=await this.store.listMemories(tenantId,{teamId});
if (this.VectorDb) {
const db=new this.VectorDb({dimensions:this.dimensions,maxElements:Math.max(1000,memories.length+100)});
for(const m of memories) await db.insert({id:m.id,vector:featureVector(m.text,this.dimensions)});
- const value={kind:'ruvector-native',degraded:false,db,items:new Map(memories.map(m=>[m.id,m]))}; return this.#rememberIndex(key,value);
+ const value={kind:'ruvector-native',degraded:false,db,items:new Map(memories.map(m=>[m.id,m]))}; return this.#rememberIndex(key,value,generation);
}
- const value={kind:'lexical-degraded',degraded:true,items:memories.map(m=>({m,v:featureVector(m.text,this.dimensions)}))}; return this.#rememberIndex(key,value);
+ const value={kind:'lexical-degraded',degraded:true,items:memories.map(m=>({m,v:featureVector(m.text,this.dimensions)}))}; return this.#rememberIndex(key,value,generation);
}
- invalidate(tenantId) { for(const key of this.indexes.keys())if(key.startsWith(`${tenantId}:`))this.indexes.delete(key); }
+ invalidate(tenantId) { this.generation++; for(const key of this.indexes.keys())if(key.startsWith(`${tenantId}:`))this.indexes.delete(key); }
async search(tenantId,{teamId,query,limit=5}) {
const idx=await this.#index(tenantId,teamId); const vector=featureVector(query,this.dimensions);
if(idx.kind==='ruvector-native') return {backend:idx.kind,degraded:idx.degraded,results:(await idx.db.search({vector,k:limit})).map(r=>({score:r.score,memory:idx.items.get(r.id)})).filter(x=>x.memory)};
diff --git a/plugins/ruflo-ai-team/test/vector-invalidation-race.node.mjs b/plugins/ruflo-ai-team/test/vector-invalidation-race.node.mjs
new file mode 100644
index 0000000000..8467811885
--- /dev/null
+++ b/plugins/ruflo-ai-team/test/vector-invalidation-race.node.mjs
@@ -0,0 +1,15 @@
+import test from 'node:test';
+import assert from 'node:assert/strict';
+import { TenantVectorMemory } from '../src/vector-memory.mjs';
+test('a pending index build cannot restore invalidated memory', async () => {
+ let release; let calls = 0;
+ const old = { id: 'old', text: 'alpha' }, fresh = { id: 'fresh', text: 'alpha updated' };
+ const store = { listMemories: async () => { if (++calls === 1) return new Promise(resolve => { release = () => resolve([old]); }); return [fresh]; } };
+ const memory = new TenantVectorMemory(store);
+ const pending = memory.search('tenant', { query: 'alpha' });
+ memory.invalidate('tenant'); release(); await pending;
+ const result = await memory.search('tenant', { query: 'alpha' });
+ assert.equal(result.results[0].memory.id, 'fresh');
+ assert.equal(calls, 2);
+ await memory.search('tenant', { query: 'alpha' }); assert.equal(calls, 2);
+});