mirror of
https://github.com/ruvnet/ruflo.git
synced 2026-12-04 06:21:59 +09:00
fix(memory): RvfBackend search through hnsw-index.ts (recall@10 0.09 → 0.89) (#4836)
* fix(memory): RvfBackend search through hnsw-index.ts (recall@10 0.09 -> 0.89) RvfBackend's inlined HnswLite seeded every search from the first efConstruction vectors in insertion order and BFS'd a flat neighbour graph, and each insert was an O(n) brute-force scan. Measured against exact search on 5,000 random unit vectors (cosine, 200 queries, seed 42): index 384-dim recall@10 1536-dim clustered 384 HnswLite (before) 0.091 0.079 0.999 @ruvector/diskann 0.1.3 0.896 0.739 1.000 hnsw-index.ts, ef=200 0.888 0.744 0.990 DiskANN matched HNSWIndex on recall but a vector inserted after build() is not searchable until a full rebuild (3.6-4.3 s at 1k vectors), which breaks store()-then-search(). HNSWIndex is incremental and is the single HNSW implementation ADR-125 asks for, so RvfBackend now uses it through a private RvfVectorIndex (not exported; the prepublishOnly surface check still passes). - indexes of <= 1000 vectors and dimension-mismatched queries use an exact scan - distances map back to the old similarity scale; threshold filter unchanged - re-storing an entry without an embedding removes its old vector - update() re-indexes when an untyped caller passes an embedding New rvf-backend-recall.test.ts drives the real RvfBackend.search: recall@10 >= 0.9 on 2000 random 64-dim vectors (HnswLite scored 0.294 there), exact results at 300 vectors, delete/re-store correctness, recall after reopen. @claude-flow/memory 3.2.2 -> 3.2.3. Co-Authored-By: claude-flow <ruv@ruv.net> * chore(memory): leave the version at 3.2.2 in this PR audit-leaf-package-publish requires the CLI pin to cover the workspace version, and audit-leaf-versions-published requires that pin to exist on the registry. The 3.2.3 bump, its publish and the CLI pin move together in the release that follows. Co-Authored-By: claude-flow <ruv@ruv.net> * release(memory): @claude-flow/memory 3.2.3, CLI pins it The RvfBackend change has to reach users through a standalone publish: memory is not bundled into the release tarballs (Publishing Rules). @claude-flow/memory@3.2.3 is published from this branch's tree; the CLI pin (dependencies + optionalDependencies) moves to it so audit-leaf-package-publish and audit-leaf-versions-published both hold. The lockfile also gains the @ruvector/sona 0.1.10 package entries pnpm reported missing. Co-Authored-By: claude-flow <ruv@ruv.net> --------- Co-authored-by: r <r@x>
This commit is contained in:
1 parent
c6061039da
commit
722232566a
5 files changed
+296
-116
No files matched your search
@@ -128,12 +128,12 @@
|
||||
"ws": "^8.21.0",
|
||||
"yaml": "^2.8.0",
|
||||
"zod": "^3.22.0",
|
||||
"@claude-flow/memory": "3.2.2"
|
||||
"@claude-flow/memory": "3.2.3"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
"@agntcy/slim-bindings": "2.1.1",
|
||||
"@claude-flow/aidefence": "3.1.0",
|
||||
"@claude-flow/memory": "3.2.2",
|
||||
"@claude-flow/memory": "3.2.3",
|
||||
"@metaharness/darwin": "~0.12.0",
|
||||
"@metaharness/flywheel": "~0.1.10",
|
||||
"@metaharness/radio": "~0.1.0",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@claude-flow/memory",
|
||||
"version": "3.2.2",
|
||||
"version": "3.2.3",
|
||||
"type": "module",
|
||||
"description": "Memory module - AgentDB unification, HNSW indexing, vector search, hybrid SQLite+AgentDB backend (ADR-009)",
|
||||
"main": "dist/index.js",
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
/**
|
||||
* RvfBackend search quality — recall@10 against exact search, through the
|
||||
* real RvfBackend.search() path.
|
||||
*
|
||||
* The backend's previous inlined `HnswLite` seeded its search from the first
|
||||
* `efConstruction` vectors in insertion order and had no recall guarantee
|
||||
* (recall@10 = 0.09 on 5k random 384-dim vectors). It now delegates to the
|
||||
* canonical HNSWIndex (ADR-125), with an exact scan for small indexes.
|
||||
*/
|
||||
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import * as fs from 'node:fs';
|
||||
import * as path from 'node:path';
|
||||
import * as os from 'node:os';
|
||||
import { RvfBackend } from './rvf-backend.js';
|
||||
import { createDefaultEntry, type MemoryEntry } from './types.js';
|
||||
|
||||
const DIM = 64;
|
||||
const K = 10;
|
||||
|
||||
function rng(seed: number): () => number {
|
||||
let s = seed >>> 0;
|
||||
return () => { s = (s * 1664525 + 1013904223) >>> 0; return s / 4294967296; };
|
||||
}
|
||||
|
||||
function unitVectors(count: number, seed: number): Float32Array[] {
|
||||
const r = rng(seed);
|
||||
const gauss = () => Math.sqrt(-2 * Math.log(r() || 1e-12)) * Math.cos(2 * Math.PI * r());
|
||||
return Array.from({ length: count }, () => {
|
||||
const v = Float32Array.from({ length: DIM }, gauss);
|
||||
let n = 0; for (const x of v) n += x * x; n = Math.sqrt(n);
|
||||
for (let i = 0; i < DIM; i++) v[i] /= n;
|
||||
return v;
|
||||
});
|
||||
}
|
||||
|
||||
function dot(a: Float32Array, b: Float32Array): number {
|
||||
let s = 0; for (let i = 0; i < a.length; i++) s += a[i] * b[i]; return s;
|
||||
}
|
||||
|
||||
function exactTopK(data: Float32Array[], q: Float32Array, k: number): string[] {
|
||||
return data.map((v, i) => [i, dot(q, v)] as const)
|
||||
.sort((a, b) => b[1] - a[1]).slice(0, k).map(([i]) => `v${i}`);
|
||||
}
|
||||
|
||||
function entry(i: number, embedding: Float32Array): MemoryEntry {
|
||||
const e = createDefaultEntry({ key: `k${i}`, content: `c${i}`, namespace: 'recall' });
|
||||
return { ...e, id: `v${i}`, embedding };
|
||||
}
|
||||
|
||||
async function recallAt10(backend: RvfBackend, data: Float32Array[], queries: Float32Array[]): Promise<number> {
|
||||
let hits = 0;
|
||||
for (const q of queries) {
|
||||
const truth = new Set(exactTopK(data, q, K));
|
||||
const got = await backend.search(q, { k: K });
|
||||
hits += got.filter(r => truth.has(r.entry.id)).length;
|
||||
}
|
||||
return hits / (queries.length * K);
|
||||
}
|
||||
|
||||
describe('RvfBackend recall', () => {
|
||||
it('reaches recall@10 >= 0.9 on 2,000 random unit vectors (graph path)', async () => {
|
||||
const data = unitVectors(2000, 7);
|
||||
const queries = unitVectors(50, 99);
|
||||
const backend = new RvfBackend({ databasePath: ':memory:', dimensions: DIM, autoPersistInterval: 0 });
|
||||
await backend.initialize();
|
||||
for (let i = 0; i < data.length; i++) await backend.store(entry(i, data[i]));
|
||||
|
||||
const recall = await recallAt10(backend, data, queries);
|
||||
expect(recall).toBeGreaterThanOrEqual(0.9);
|
||||
await backend.shutdown();
|
||||
}, 20_000);
|
||||
|
||||
it('is exact for small indexes', async () => {
|
||||
const data = unitVectors(300, 3);
|
||||
const queries = unitVectors(20, 4);
|
||||
const backend = new RvfBackend({ databasePath: ':memory:', dimensions: DIM, autoPersistInterval: 0 });
|
||||
await backend.initialize();
|
||||
for (let i = 0; i < data.length; i++) await backend.store(entry(i, data[i]));
|
||||
expect(await recallAt10(backend, data, queries)).toBe(1);
|
||||
await backend.shutdown();
|
||||
});
|
||||
|
||||
it('never returns deleted ids and re-indexes a re-stored embedding', async () => {
|
||||
const data = unitVectors(1500, 11);
|
||||
const backend = new RvfBackend({ databasePath: ':memory:', dimensions: DIM, autoPersistInterval: 0 });
|
||||
await backend.initialize();
|
||||
for (let i = 0; i < data.length; i++) await backend.store(entry(i, data[i]));
|
||||
|
||||
// Delete every 3rd entry, then query each deleted vector directly.
|
||||
const deleted = new Set<string>();
|
||||
for (let i = 0; i < data.length; i += 3) { await backend.delete(`v${i}`); deleted.add(`v${i}`); }
|
||||
for (let i = 0; i < data.length; i += 30) {
|
||||
const got = await backend.search(data[i], { k: K });
|
||||
expect(got.length).toBe(K);
|
||||
for (const r of got) expect(deleted.has(r.entry.id)).toBe(false);
|
||||
}
|
||||
|
||||
// Move v1 onto v2's vector: v1 must now be found at the new location
|
||||
// (as a top-2 hit for v2's vector) and no longer be the hit for its old one.
|
||||
const moved = Float32Array.from(data[2]);
|
||||
await backend.store(entry(1, moved));
|
||||
const atNew = (await backend.search(data[2], { k: 2 })).map(r => r.entry.id);
|
||||
expect(atNew).toContain('v1');
|
||||
const atOld = await backend.search(data[1], { k: 1 });
|
||||
expect(atOld[0]?.entry.id).not.toBe('v1');
|
||||
await backend.shutdown();
|
||||
}, 20_000);
|
||||
|
||||
it('rebuilds a searchable index when the snapshot is reopened', async () => {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'cf-mem-rvf-recall-'));
|
||||
const dbPath = path.join(dir, 'memory.rvf');
|
||||
try {
|
||||
const data = unitVectors(1200, 21);
|
||||
const queries = unitVectors(30, 22);
|
||||
const first = new RvfBackend({ databasePath: dbPath, dimensions: DIM, autoPersistInterval: 0 });
|
||||
await first.initialize();
|
||||
for (let i = 0; i < data.length; i++) await first.store(entry(i, data[i]));
|
||||
await first.shutdown();
|
||||
|
||||
const second = new RvfBackend({ databasePath: dbPath, dimensions: DIM, autoPersistInterval: 0 });
|
||||
await second.initialize();
|
||||
expect(await recallAt10(second, data, queries)).toBeGreaterThanOrEqual(0.9);
|
||||
await second.shutdown();
|
||||
} finally {
|
||||
fs.rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
}, 20_000);
|
||||
});
|
||||
@@ -12,11 +12,26 @@ import type {
|
||||
HealthCheckResult,
|
||||
MemoryType,
|
||||
} from './types.js';
|
||||
import { HNSWIndex } from './hnsw-index.js';
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// HnswLite — small brute-force-degrading HNSW used ONLY by RvfBackend.
|
||||
// Inlined here by ADR-125 Phase 3 so the canonical `src/hnsw-index.ts` can be
|
||||
// the single HNSW implementation in the public surface. Not exported.
|
||||
// RvfVectorIndex — RvfBackend's vector index, backed by the canonical
|
||||
// `HNSWIndex` from `src/hnsw-index.ts` (ADR-125: one HNSW implementation).
|
||||
//
|
||||
// It replaces the inlined `HnswLite`, whose search seeded from the first
|
||||
// `efConstruction` vectors in insertion order and BFS'd over a flat
|
||||
// neighbour graph: on 5,000 random 384-dim unit vectors it reached
|
||||
// recall@10 = 0.091 against exact search (0.079 at 1536-dim), and every
|
||||
// insert was an O(n) brute-force scan. HNSWIndex measured 0.888 (384-dim)
|
||||
// and 0.744 (1536-dim) on the same data, 0.990 on clustered data.
|
||||
// @ruvector/diskann was also measured (0.896 / 0.739) but a vector inserted
|
||||
// after build() is not searchable until a full rebuild (3.6 s at 1k vectors),
|
||||
// which breaks store()-then-search(); see the RvfBackend recall test.
|
||||
//
|
||||
// Small indexes (<= EXACT_SEARCH_MAX vectors) are searched exactly, so the
|
||||
// common small memory store keeps recall 1.0. Embeddings whose length does not
|
||||
// match the configured dimensions are kept out of the graph (HNSWIndex
|
||||
// rejects them) and are only reachable through the exact scan. Not exported.
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
interface HnswSearchResult {
|
||||
@@ -24,94 +39,65 @@ interface HnswSearchResult {
|
||||
score: number;
|
||||
}
|
||||
|
||||
class HnswLite {
|
||||
private vectors = new Map<string, Float32Array>();
|
||||
private neighbors = new Map<string, Set<string>>();
|
||||
private readonly dimensions: number;
|
||||
private readonly maxNeighbors: number;
|
||||
private readonly efConstruction: number;
|
||||
private readonly metric: string;
|
||||
/** Below this many indexed vectors, search() is an exact scan. */
|
||||
const EXACT_SEARCH_MAX = 1000;
|
||||
|
||||
constructor(dimensions: number, m: number, efConstruction: number, metric: string) {
|
||||
class RvfVectorIndex {
|
||||
private readonly vectors = new Map<string, Float32Array>();
|
||||
private readonly graph: HNSWIndex;
|
||||
private readonly dimensions: number;
|
||||
private readonly efConstruction: number;
|
||||
private readonly metric: 'cosine' | 'euclidean' | 'dot';
|
||||
|
||||
constructor(dimensions: number, m: number, efConstruction: number, metric: 'cosine' | 'euclidean' | 'dot') {
|
||||
this.dimensions = dimensions;
|
||||
this.maxNeighbors = m;
|
||||
this.efConstruction = efConstruction;
|
||||
this.metric = metric;
|
||||
this.graph = new HNSWIndex({
|
||||
dimensions,
|
||||
M: m,
|
||||
efConstruction,
|
||||
// RvfBackend never capped its index; keep that (maxElements is advisory
|
||||
// in RvfBackendConfig).
|
||||
maxElements: Number.MAX_SAFE_INTEGER,
|
||||
metric,
|
||||
});
|
||||
}
|
||||
|
||||
get size(): number { return this.vectors.size; }
|
||||
|
||||
add(id: string, vector: Float32Array): void {
|
||||
async add(id: string, vector: Float32Array): Promise<void> {
|
||||
if (this.vectors.has(id)) await this.remove(id);
|
||||
this.vectors.set(id, vector);
|
||||
if (this.vectors.size === 1) {
|
||||
this.neighbors.set(id, new Set());
|
||||
return;
|
||||
}
|
||||
const nearest = this.findNearest(vector, this.maxNeighbors);
|
||||
const neighborSet = new Set<string>();
|
||||
for (const n of nearest) {
|
||||
neighborSet.add(n.id);
|
||||
const nNeighbors = this.neighbors.get(n.id);
|
||||
if (nNeighbors) {
|
||||
nNeighbors.add(id);
|
||||
if (nNeighbors.size > this.maxNeighbors * 2) this.pruneNeighbors(n.id);
|
||||
}
|
||||
}
|
||||
this.neighbors.set(id, neighborSet);
|
||||
if (vector.length === this.dimensions) await this.graph.addPoint(id, vector);
|
||||
}
|
||||
|
||||
remove(id: string): void {
|
||||
this.vectors.delete(id);
|
||||
const my = this.neighbors.get(id);
|
||||
if (my) for (const nId of my) this.neighbors.get(nId)?.delete(id);
|
||||
this.neighbors.delete(id);
|
||||
async remove(id: string): Promise<void> {
|
||||
if (!this.vectors.delete(id)) return;
|
||||
if (this.graph.has(id)) await this.graph.removePoint(id);
|
||||
}
|
||||
|
||||
search(query: Float32Array, k: number, threshold?: number): HnswSearchResult[] {
|
||||
if (this.vectors.size === 0) return [];
|
||||
if (this.vectors.size <= k * 2) return this.bruteForce(query, k, threshold);
|
||||
|
||||
const visited = new Set<string>();
|
||||
const candidates: HnswSearchResult[] = [];
|
||||
|
||||
let entryId: string | undefined;
|
||||
let bestScore = -1;
|
||||
for (const [id] of this.vectors) {
|
||||
const score = this.similarity(query, this.vectors.get(id)!);
|
||||
if (score > bestScore) { bestScore = score; entryId = id; }
|
||||
if (visited.size >= Math.min(this.efConstruction, this.vectors.size)) break;
|
||||
visited.add(id);
|
||||
candidates.push({ id, score });
|
||||
async search(query: Float32Array, k: number, threshold?: number): Promise<HnswSearchResult[]> {
|
||||
if (this.vectors.size === 0 || k <= 0) return [];
|
||||
if (this.vectors.size <= EXACT_SEARCH_MAX || query.length !== this.dimensions) {
|
||||
return this.exact(query, k, threshold);
|
||||
}
|
||||
|
||||
if (entryId) {
|
||||
const queue = [entryId];
|
||||
let idx = 0;
|
||||
while (idx < queue.length && visited.size < this.efConstruction * 2) {
|
||||
const currentId = queue[idx++];
|
||||
const currentNeighbors = this.neighbors.get(currentId);
|
||||
if (!currentNeighbors) continue;
|
||||
for (const nId of currentNeighbors) {
|
||||
if (visited.has(nId)) continue;
|
||||
visited.add(nId);
|
||||
const vec = this.vectors.get(nId);
|
||||
if (!vec) continue;
|
||||
const score = this.similarity(query, vec);
|
||||
candidates.push({ id: nId, score });
|
||||
queue.push(nId);
|
||||
}
|
||||
}
|
||||
const ef = Math.max(k, this.efConstruction);
|
||||
const raw = await this.graph.search(query, k, ef);
|
||||
const out: HnswSearchResult[] = [];
|
||||
for (const r of raw) {
|
||||
if (!this.vectors.has(r.id)) continue;
|
||||
const score = this.toScore(r.distance);
|
||||
if (threshold !== undefined && score < threshold) continue;
|
||||
out.push({ id: r.id, score });
|
||||
}
|
||||
|
||||
candidates.sort((a, b) => b.score - a.score);
|
||||
let filtered = candidates;
|
||||
if (threshold !== undefined) filtered = filtered.filter(c => c.score >= threshold);
|
||||
return filtered.slice(0, k);
|
||||
return out;
|
||||
}
|
||||
|
||||
private bruteForce(query: Float32Array, k: number, threshold?: number): HnswSearchResult[] {
|
||||
private exact(query: Float32Array, k: number, threshold?: number): HnswSearchResult[] {
|
||||
const results: HnswSearchResult[] = [];
|
||||
for (const [id, vec] of this.vectors) {
|
||||
if (vec.length !== query.length) continue;
|
||||
const score = this.similarity(query, vec);
|
||||
if (threshold !== undefined && score < threshold) continue;
|
||||
results.push({ id, score });
|
||||
@@ -120,24 +106,11 @@ class HnswLite {
|
||||
return results.slice(0, k);
|
||||
}
|
||||
|
||||
private findNearest(query: Float32Array, k: number): HnswSearchResult[] {
|
||||
return this.bruteForce(query, k);
|
||||
}
|
||||
|
||||
private pruneNeighbors(id: string): void {
|
||||
const my = this.neighbors.get(id);
|
||||
if (!my) return;
|
||||
const vec = this.vectors.get(id);
|
||||
if (!vec) return;
|
||||
const scored: HnswSearchResult[] = [];
|
||||
for (const nId of my) {
|
||||
const nVec = this.vectors.get(nId);
|
||||
if (!nVec) continue;
|
||||
scored.push({ id: nId, score: this.similarity(vec, nVec) });
|
||||
}
|
||||
scored.sort((a, b) => b.score - a.score);
|
||||
const keep = new Set(scored.slice(0, this.maxNeighbors).map(s => s.id));
|
||||
for (const nId of my) if (!keep.has(nId)) my.delete(nId);
|
||||
/** Map HNSWIndex distances back to the similarity scale RvfBackend reports. */
|
||||
private toScore(distance: number): number {
|
||||
if (this.metric === 'dot') return -distance;
|
||||
if (this.metric === 'euclidean') return 1 / (1 + distance);
|
||||
return 1 - distance;
|
||||
}
|
||||
|
||||
private similarity(a: Float32Array, b: Float32Array): number {
|
||||
@@ -212,7 +185,7 @@ const DEFAULT_PERSIST_INTERVAL = 30000;
|
||||
|
||||
/**
|
||||
* Pure-TS RVF backend: entries live in memory and persist to a single
|
||||
* `RVF\0` snapshot file at `databasePath`, with `HnswLite` for search.
|
||||
* `RVF\0` snapshot file at `databasePath`, with `RvfVectorIndex` (the canonical HNSWIndex) for search.
|
||||
*
|
||||
* There is deliberately no `@ruvector/rvf` native path (#4279). The SDK is a
|
||||
* vector index (u64 ids, `{id, distance}` results, no payloads), so it cannot
|
||||
@@ -222,7 +195,7 @@ const DEFAULT_PERSIST_INTERVAL = 30000;
|
||||
export class RvfBackend implements IMemoryBackend {
|
||||
private entries = new Map<string, MemoryEntry>();
|
||||
private keyIndex = new Map<string, string>();
|
||||
private hnswIndex: HnswLite | null = null;
|
||||
private hnswIndex: RvfVectorIndex | null = null;
|
||||
private config: Required<RvfBackendConfig>;
|
||||
private initialized = false;
|
||||
private dirty = false;
|
||||
@@ -254,7 +227,7 @@ export class RvfBackend implements IMemoryBackend {
|
||||
async initialize(): Promise<void> {
|
||||
if (this.initialized) return;
|
||||
|
||||
this.hnswIndex = new HnswLite(
|
||||
this.hnswIndex = new RvfVectorIndex(
|
||||
this.config.dimensions,
|
||||
this.config.hnswM,
|
||||
this.config.hnswEfConstruction,
|
||||
@@ -298,8 +271,11 @@ export class RvfBackend implements IMemoryBackend {
|
||||
const e = ns !== entry.namespace ? { ...entry, namespace: ns } : entry;
|
||||
this.entries.set(e.id, e);
|
||||
this.keyIndex.set(this.compositeKey(e.namespace, e.key), e.id);
|
||||
if (e.embedding && this.hnswIndex) {
|
||||
this.hnswIndex.add(e.id, e.embedding);
|
||||
if (this.hnswIndex) {
|
||||
// Upsert: add() replaces any previous vector for this id; an entry
|
||||
// re-stored without an embedding must not keep its old one searchable.
|
||||
if (e.embedding) await this.hnswIndex.add(e.id, e.embedding);
|
||||
else await this.hnswIndex.remove(e.id);
|
||||
}
|
||||
this.dirty = true;
|
||||
}
|
||||
@@ -329,6 +305,12 @@ export class RvfBackend implements IMemoryBackend {
|
||||
version: entry.version + 1,
|
||||
};
|
||||
this.entries.set(id, updated);
|
||||
// MemoryEntryUpdate has no `embedding`, but untyped callers can pass one;
|
||||
// keep the index in step with the stored entry either way.
|
||||
if (this.hnswIndex && updated.embedding !== entry.embedding) {
|
||||
if (updated.embedding) await this.hnswIndex.add(id, updated.embedding);
|
||||
else await this.hnswIndex.remove(id);
|
||||
}
|
||||
this.dirty = true;
|
||||
return updated;
|
||||
}
|
||||
@@ -338,7 +320,7 @@ export class RvfBackend implements IMemoryBackend {
|
||||
if (!entry) return false;
|
||||
this.entries.delete(id);
|
||||
this.keyIndex.delete(this.compositeKey(entry.namespace, entry.key));
|
||||
if (this.hnswIndex) this.hnswIndex.remove(id);
|
||||
if (this.hnswIndex) await this.hnswIndex.remove(id);
|
||||
this.dirty = true;
|
||||
return true;
|
||||
}
|
||||
@@ -364,7 +346,7 @@ export class RvfBackend implements IMemoryBackend {
|
||||
}
|
||||
|
||||
if (q.type === 'semantic' && q.embedding && this.hnswIndex) {
|
||||
const searchResults = this.hnswIndex.search(q.embedding, q.limit, q.threshold);
|
||||
const searchResults = await this.hnswIndex.search(q.embedding, q.limit, q.threshold);
|
||||
const idSet = new Set(searchResults.map(r => r.id));
|
||||
results = results.filter(e => idSet.has(e.id));
|
||||
}
|
||||
@@ -381,7 +363,7 @@ export class RvfBackend implements IMemoryBackend {
|
||||
let results: SearchResult[];
|
||||
|
||||
if (this.hnswIndex) {
|
||||
const raw = this.hnswIndex.search(embedding, options.k * 2, options.threshold);
|
||||
const raw = await this.hnswIndex.search(embedding, options.k * 2, options.threshold);
|
||||
results = [];
|
||||
for (const r of raw) {
|
||||
const entry = this.entries.get(r.id);
|
||||
@@ -404,7 +386,7 @@ export class RvfBackend implements IMemoryBackend {
|
||||
for (const entry of entries) {
|
||||
this.entries.set(entry.id, entry);
|
||||
this.keyIndex.set(this.compositeKey(entry.namespace, entry.key), entry.id);
|
||||
if (entry.embedding && this.hnswIndex) this.hnswIndex.add(entry.id, entry.embedding);
|
||||
if (entry.embedding && this.hnswIndex) await this.hnswIndex.add(entry.id, entry.embedding);
|
||||
}
|
||||
this.dirty = true;
|
||||
}
|
||||
@@ -416,7 +398,7 @@ export class RvfBackend implements IMemoryBackend {
|
||||
if (entry) {
|
||||
this.entries.delete(id);
|
||||
this.keyIndex.delete(this.compositeKey(entry.namespace, entry.key));
|
||||
if (this.hnswIndex) this.hnswIndex.remove(id);
|
||||
if (this.hnswIndex) await this.hnswIndex.remove(id);
|
||||
count++;
|
||||
}
|
||||
}
|
||||
@@ -448,7 +430,7 @@ export class RvfBackend implements IMemoryBackend {
|
||||
const entry = this.entries.get(id)!;
|
||||
this.entries.delete(id);
|
||||
this.keyIndex.delete(this.compositeKey(entry.namespace, entry.key));
|
||||
if (this.hnswIndex) this.hnswIndex.remove(id);
|
||||
if (this.hnswIndex) await this.hnswIndex.remove(id);
|
||||
}
|
||||
if (toDelete.length > 0) this.dirty = true;
|
||||
return toDelete.length;
|
||||
@@ -575,7 +557,7 @@ export class RvfBackend implements IMemoryBackend {
|
||||
const entry: MemoryEntry = parsed;
|
||||
this.entries.set(entry.id, entry);
|
||||
this.keyIndex.set(this.compositeKey(entry.namespace, entry.key), entry.id);
|
||||
if (entry.embedding && this.hnswIndex) this.hnswIndex.add(entry.id, entry.embedding);
|
||||
if (entry.embedding && this.hnswIndex) await this.hnswIndex.add(entry.id, entry.embedding);
|
||||
}
|
||||
} catch (err) {
|
||||
if (this.config.verbose) {
|
||||
|
||||
Generated
+80
-11
@@ -218,7 +218,7 @@ importers:
|
||||
specifier: 3.1.0
|
||||
version: link:../aidefence
|
||||
'@claude-flow/memory':
|
||||
specifier: 3.2.2
|
||||
specifier: 3.2.3
|
||||
version: link:../memory
|
||||
'@metaharness/darwin':
|
||||
specifier: ~0.12.0
|
||||
@@ -6115,6 +6115,13 @@ packages:
|
||||
'@ruvector/rvf-wasm': 0.1.9
|
||||
optional: true
|
||||
|
||||
/@ruvector/sona-darwin-arm64@0.1.10:
|
||||
resolution: {integrity: sha512-KDb++39pQ8hg1Wy9z6qmPQKWusskitgLMxTZlbTqiHgC7LxnMH4AkA0fsZnJUOLxohhWvSEFVnPZ4DNP9qnR5Q==}
|
||||
cpu: [arm64]
|
||||
os: [darwin]
|
||||
requiresBuild: true
|
||||
optional: true
|
||||
|
||||
/@ruvector/sona-darwin-arm64@0.1.5:
|
||||
resolution: {integrity: sha512-+bXB7+WdbkiNbcw0Xh5VENNILgDVpuma7fyx8NwGylWaYRe1niM03k/rezdM3zbpf4qGSqp6fVKIIDU8/H9lHw==}
|
||||
cpu: [arm64]
|
||||
@@ -6136,6 +6143,14 @@ packages:
|
||||
cpu: [arm64]
|
||||
os: [darwin]
|
||||
requiresBuild: true
|
||||
dev: false
|
||||
optional: true
|
||||
|
||||
/@ruvector/sona-darwin-x64@0.1.10:
|
||||
resolution: {integrity: sha512-fxK6eIIfsTJIJbO9gbuhLVgjH6HmqsfY6t1hBzABTwBcaLG7iX60G//fdOW580wLy2vmSTdS1TmTIxnHo2RVKw==}
|
||||
cpu: [x64]
|
||||
os: [darwin]
|
||||
requiresBuild: true
|
||||
optional: true
|
||||
|
||||
/@ruvector/sona-darwin-x64@0.1.5:
|
||||
@@ -6159,6 +6174,14 @@ packages:
|
||||
cpu: [x64]
|
||||
os: [darwin]
|
||||
requiresBuild: true
|
||||
dev: false
|
||||
optional: true
|
||||
|
||||
/@ruvector/sona-linux-arm64-gnu@0.1.10:
|
||||
resolution: {integrity: sha512-kqpSo2fhF4s6SGpy4Ap/4IaAZOGP5qkyaw4Fi6HXCTUe4WYZcpOGVhL5VsiaWBPCRqqHj5zpQ95pPyG/awH0qA==}
|
||||
cpu: [arm64]
|
||||
os: [linux]
|
||||
requiresBuild: true
|
||||
optional: true
|
||||
|
||||
/@ruvector/sona-linux-arm64-gnu@0.1.5:
|
||||
@@ -6182,6 +6205,14 @@ packages:
|
||||
cpu: [arm64]
|
||||
os: [linux]
|
||||
requiresBuild: true
|
||||
dev: false
|
||||
optional: true
|
||||
|
||||
/@ruvector/sona-linux-x64-gnu@0.1.10:
|
||||
resolution: {integrity: sha512-tarsh0L5UW7INzVWobp7WLQY4IJZVU4OmbzSrJgrejG66a5Qr3gCVIqpno5FqM9jufOnKq/tsuKRtNTPo+xdxQ==}
|
||||
cpu: [x64]
|
||||
os: [linux]
|
||||
requiresBuild: true
|
||||
optional: true
|
||||
|
||||
/@ruvector/sona-linux-x64-gnu@0.1.5:
|
||||
@@ -6205,6 +6236,14 @@ packages:
|
||||
cpu: [x64]
|
||||
os: [linux]
|
||||
requiresBuild: true
|
||||
dev: false
|
||||
optional: true
|
||||
|
||||
/@ruvector/sona-linux-x64-musl@0.1.10:
|
||||
resolution: {integrity: sha512-8YdaDc4GAuVwqv79eDokMCtzV+jMevIiCraFSQ+pItFn8Jmi+2Q2mcUyVH2g/glfeRjSFQYUSC/W7yFJiiWIfA==}
|
||||
cpu: [x64]
|
||||
os: [linux]
|
||||
requiresBuild: true
|
||||
optional: true
|
||||
|
||||
/@ruvector/sona-linux-x64-musl@0.1.5:
|
||||
@@ -6228,6 +6267,14 @@ packages:
|
||||
cpu: [x64]
|
||||
os: [linux]
|
||||
requiresBuild: true
|
||||
dev: false
|
||||
optional: true
|
||||
|
||||
/@ruvector/sona-win32-arm64-msvc@0.1.10:
|
||||
resolution: {integrity: sha512-NUzYU94CUWzKAf7xZHUYvqOUvIkk2TUbCgAH33yphlH6mLQpCcC03Xt+9UdYvFk0NVA0ZLZ62UrzNwwTasmQxw==}
|
||||
cpu: [arm64]
|
||||
os: [win32]
|
||||
requiresBuild: true
|
||||
optional: true
|
||||
|
||||
/@ruvector/sona-win32-arm64-msvc@0.1.5:
|
||||
@@ -6251,6 +6298,14 @@ packages:
|
||||
cpu: [arm64]
|
||||
os: [win32]
|
||||
requiresBuild: true
|
||||
dev: false
|
||||
optional: true
|
||||
|
||||
/@ruvector/sona-win32-x64-msvc@0.1.10:
|
||||
resolution: {integrity: sha512-kw99twEsiu+QEcHJ8gQRt/XVjOpUb0R6r5dvaVirtjbeNFPFGivuji4JTGwREZgxB6rmAdXTyJZ++Ul1OIZ3Rg==}
|
||||
cpu: [x64]
|
||||
os: [win32]
|
||||
requiresBuild: true
|
||||
optional: true
|
||||
|
||||
/@ruvector/sona-win32-x64-msvc@0.1.5:
|
||||
@@ -6274,8 +6329,21 @@ packages:
|
||||
cpu: [x64]
|
||||
os: [win32]
|
||||
requiresBuild: true
|
||||
dev: false
|
||||
optional: true
|
||||
|
||||
/@ruvector/sona@0.1.10:
|
||||
resolution: {integrity: sha512-z+WyumpoJbtpQiZD9D/CtQfrTInZkupqvkDttxiNun+0lzXjVVw30k1kWCKJeGxQAI+GPirjrT7eBcxl+hTvpw==}
|
||||
engines: {node: '>= 16'}
|
||||
optionalDependencies:
|
||||
'@ruvector/sona-darwin-arm64': 0.1.10
|
||||
'@ruvector/sona-darwin-x64': 0.1.10
|
||||
'@ruvector/sona-linux-arm64-gnu': 0.1.10
|
||||
'@ruvector/sona-linux-x64-gnu': 0.1.10
|
||||
'@ruvector/sona-linux-x64-musl': 0.1.10
|
||||
'@ruvector/sona-win32-arm64-msvc': 0.1.10
|
||||
'@ruvector/sona-win32-x64-msvc': 0.1.10
|
||||
|
||||
/@ruvector/sona@0.1.5:
|
||||
resolution: {integrity: sha512-XHQbphnWsRzWRWGK/HxK+RreY6OdlggMrOvrfkWlEq8EctHVy2ASwps7hk25Somx8Umj7+CkNuyQEbHcNcl/oQ==}
|
||||
engines: {node: '>= 16'}
|
||||
@@ -6315,6 +6383,7 @@ packages:
|
||||
'@ruvector/sona-linux-x64-musl': 0.1.8
|
||||
'@ruvector/sona-win32-arm64-msvc': 0.1.8
|
||||
'@ruvector/sona-win32-x64-msvc': 0.1.8
|
||||
dev: false
|
||||
|
||||
/@ruvector/tiny-dancer-darwin-arm64@0.1.15:
|
||||
resolution: {integrity: sha512-99vy9OLjppPj3kjusQnirgLvOnBt6Jrt2pij3Fvs5pzyp+zh04gb1np0tFR4JCxftKnuoKYqN7bCtQvgQbBBSg==}
|
||||
@@ -7548,7 +7617,7 @@ packages:
|
||||
'@ruvector/rvf-node': 0.1.8
|
||||
'@ruvector/rvf-solver': 0.1.8
|
||||
'@ruvector/rvf-wasm': 0.1.9
|
||||
'@ruvector/sona': 0.1.8
|
||||
'@ruvector/sona': 0.1.10
|
||||
argon2: 0.44.0
|
||||
better-sqlite3: 12.9.0
|
||||
chalk: 5.6.2
|
||||
@@ -7593,7 +7662,7 @@ packages:
|
||||
'@ruvector/rvf-node': 0.1.8
|
||||
'@ruvector/rvf-solver': 0.1.8
|
||||
'@ruvector/rvf-wasm': 0.1.9
|
||||
'@ruvector/sona': 0.1.8
|
||||
'@ruvector/sona': 0.1.10
|
||||
argon2: 0.44.0
|
||||
better-sqlite3: 12.9.0
|
||||
chalk: 5.6.2
|
||||
@@ -7766,7 +7835,7 @@ packages:
|
||||
optionalDependencies:
|
||||
'@rollup/rollup-darwin-arm64': 4.60.3
|
||||
'@ruvector/attention': 0.1.32
|
||||
'@ruvector/sona': 0.1.8
|
||||
'@ruvector/sona': 0.1.10
|
||||
agentdb: 3.3.0(@types/node@20.19.27)(zod@3.25.76)
|
||||
better-sqlite3: 12.9.0
|
||||
onnxruntime-node: 1.24.3
|
||||
@@ -7825,7 +7894,7 @@ packages:
|
||||
optionalDependencies:
|
||||
'@rollup/rollup-darwin-arm64': 4.60.3
|
||||
'@ruvector/attention': 0.1.32
|
||||
'@ruvector/sona': 0.1.8
|
||||
'@ruvector/sona': 0.1.10
|
||||
'@xenova/transformers': 2.17.2
|
||||
agentdb: 3.3.0(@types/node@20.19.27)(zod@3.25.76)
|
||||
better-sqlite3: 12.9.0
|
||||
@@ -7886,7 +7955,7 @@ packages:
|
||||
zod: 3.25.76
|
||||
optionalDependencies:
|
||||
'@ruvector/attention': 0.1.32
|
||||
'@ruvector/sona': 0.1.8
|
||||
'@ruvector/sona': 0.1.10
|
||||
agentdb: 2.0.0-alpha.3.7(@types/node@20.19.27)(express@5.2.1)(marked@11.2.0)
|
||||
better-sqlite3: 12.9.0
|
||||
onnxruntime-node: 1.24.3
|
||||
@@ -7947,7 +8016,7 @@ packages:
|
||||
optionalDependencies:
|
||||
'@rollup/rollup-darwin-arm64': 4.60.3
|
||||
'@ruvector/attention': 0.1.32
|
||||
'@ruvector/sona': 0.1.8
|
||||
'@ruvector/sona': 0.1.10
|
||||
better-sqlite3: 12.9.0
|
||||
onnxruntime-node: 1.24.3
|
||||
sharp: 0.35.5(@types/node@20.19.27)
|
||||
@@ -12601,7 +12670,7 @@ packages:
|
||||
'@ruvector/attention': 0.1.32
|
||||
'@ruvector/core': 0.1.30
|
||||
'@ruvector/gnn': 0.1.25
|
||||
'@ruvector/sona': 0.1.8
|
||||
'@ruvector/sona': 0.1.10
|
||||
chalk: 4.1.2
|
||||
commander: 11.1.0
|
||||
ora: 5.4.1
|
||||
@@ -12621,7 +12690,7 @@ packages:
|
||||
'@ruvector/attention': 0.1.32
|
||||
'@ruvector/core': 0.1.30
|
||||
'@ruvector/gnn': 0.1.25
|
||||
'@ruvector/sona': 0.1.8
|
||||
'@ruvector/sona': 0.1.10
|
||||
'@xenova/transformers': 2.17.2
|
||||
chalk: 4.1.2
|
||||
commander: 11.1.0
|
||||
@@ -12669,7 +12738,7 @@ packages:
|
||||
'@ruvector/gnn': 0.1.25
|
||||
'@ruvector/router': 0.1.30
|
||||
'@ruvector/ruvllm': 2.6.0
|
||||
'@ruvector/sona': 0.1.8
|
||||
'@ruvector/sona': 0.1.10
|
||||
chalk: 4.1.2
|
||||
commander: 11.1.0
|
||||
metaharness: 0.4.2
|
||||
@@ -12777,7 +12846,7 @@ packages:
|
||||
'@ruvector/gnn': 0.1.25
|
||||
'@ruvector/router': 0.1.30
|
||||
'@ruvector/ruvllm': 2.6.0
|
||||
'@ruvector/sona': 0.1.8
|
||||
'@ruvector/sona': 0.1.10
|
||||
chalk: 4.1.2
|
||||
commander: 11.1.0
|
||||
ora: 5.4.1
|
||||
|
||||
Reference in new issue
Block a user