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): use collision-resistant persistent cache keys (#3681)

Rudy Mizrahi Celekli14c57de92d7d
diff --git a/v3/@claude-flow/embeddings/src/persistent-cache-identity.test.ts b/v3/@claude-flow/embeddings/src/persistent-cache-identity.test.ts
new file mode 100644
index 0000000000..d4b78e8941
--- /dev/null
+++ b/v3/@claude-flow/embeddings/src/persistent-cache-identity.test.ts
@@ -0,0 +1,44 @@
+import { afterEach, describe, it, expect } 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 dirs: string[] = [];
+const caches: PersistentEmbeddingCache[] = [];
+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 });
+});
+function create(dbPath: string) {
+ const cache = new PersistentEmbeddingCache({ dbPath });
+ caches.push(cache);
+ return cache;
+}
+describe('persistent embedding text identity', () => {
+ it('keeps equal-length FNV collisions distinct across writes and reopening', async () => {
+ const dir = mkdtempSync(join(tmpdir(), 'ruflo-cache-identity-'));
+ dirs.push(dir);
+ const path = join(dir, 'embeddings.sqlite');
+ const cache = create(path);
+ await cache.set('0004hiu', new Float32Array([1, 2]));
+ expect(await cache.get('000b0yd')).toBeNull();
+ await cache.set('000b0yd', new Float32Array([3, 4]));
+ expect(Array.from((await cache.get('0004hiu'))!)).toEqual([1, 2]);
+ expect(Array.from((await cache.get('000b0yd'))!)).toEqual([3, 4]);
+ await cache.close();
+ const reopened = create(path);
+ expect(Array.from((await reopened.get('0004hiu'))!)).toEqual([1, 2]);
+ expect(Array.from((await reopened.get('000b0yd'))!)).toEqual([3, 4]);
+ });
+ it('preserves distinct JavaScript code units for lone surrogate inputs', async () => {
+ const dir = mkdtempSync(join(tmpdir(), 'ruflo-cache-surrogate-'));
+ dirs.push(dir);
+ const cache = create(join(dir, 'embeddings.sqlite'));
+ await cache.set('\ud800', new Float32Array([1]));
+ expect(await cache.get('\ud801')).toBeNull();
+ await cache.set('\ud801', new Float32Array([2]));
+ expect(Array.from((await cache.get('\ud800'))!)).toEqual([1]);
+ expect(Array.from((await cache.get('\ud801'))!)).toEqual([2]);
+ });
+});
diff --git a/v3/@claude-flow/embeddings/src/persistent-cache.ts b/v3/@claude-flow/embeddings/src/persistent-cache.ts
index 7ff8e39340..f323cd9e8d 100644
--- a/v3/@claude-flow/embeddings/src/persistent-cache.ts
+++ b/v3/@claude-flow/embeddings/src/persistent-cache.ts
@@ -12,6 +12,7 @@
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
import { dirname } from 'path';
+import { createHash } from 'node:crypto';
// Use 'any' for sql.js types to avoid complex typing issues
// sql.js has its own types but they don't always match perfectly
@@ -181,13 +182,9 @@ export class PersistentEmbeddingCache {
* Generate cache key from text
*/
private hashKey(text: string): string {
- // FNV-1a hash for fast, deterministic key generation
- let hash = 0x811c9dc5;
- for (let i = 0; i < text.length; i++) {
- hash ^= text.charCodeAt(i);
- hash = (hash * 0x01000193) >>> 0;
- }
- return `emb_${hash.toString(16)}_${text.length}`;
+ // The text identity must distinguish equal-length 32-bit hash collisions.
+ // A versioned prefix makes legacy cache entries harmless cold misses.
+ return `emb_v2_${createHash('sha256').update(Buffer.from(text, 'utf16le')).digest('hex')}`;
}
/**