claude-flow 3.41.4 → 3.42.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/.proven-config-version +1 -0
- package/.claude/proven-config.json +42 -0
- package/node_modules/@claude-flow/codex/dist/cli.js +0 -0
- package/node_modules/@claude-flow/codex/dist/dual-mode/orchestrator.d.ts +4 -0
- package/node_modules/@claude-flow/codex/dist/dual-mode/orchestrator.d.ts.map +1 -1
- package/node_modules/@claude-flow/codex/dist/dual-mode/orchestrator.js +38 -1
- package/node_modules/@claude-flow/codex/dist/dual-mode/orchestrator.js.map +1 -1
- package/node_modules/@claude-flow/codex/package.json +2 -1
- package/node_modules/@claude-flow/plugin-agent-federation/dist/bin.js +0 -0
- package/node_modules/@claude-flow/security/dist/index.d.ts +1 -1
- package/node_modules/@claude-flow/security/dist/index.d.ts.map +1 -1
- package/node_modules/@claude-flow/security/dist/index.js +1 -1
- package/node_modules/@claude-flow/security/dist/index.js.map +1 -1
- package/node_modules/@claude-flow/security/dist/mcp-caller-identity.d.ts +11 -0
- package/node_modules/@claude-flow/security/dist/mcp-caller-identity.d.ts.map +1 -1
- package/node_modules/@claude-flow/security/dist/mcp-caller-identity.js +0 -0
- package/node_modules/@claude-flow/security/dist/mcp-caller-identity.js.map +1 -1
- package/node_modules/@claude-flow/security/package.json +1 -0
- package/package.json +1 -1
- package/v3/@claude-flow/cli/catalog-manifest.json +2 -2
- package/v3/@claude-flow/cli/dist/src/commands/hive-mind.js +4 -3
- package/v3/@claude-flow/cli/dist/src/init/claudemd-generator.js +2 -2
- package/v3/@claude-flow/cli/dist/src/mcp-server.js +12 -0
- package/v3/@claude-flow/cli/dist/src/mcp-tools/agentdb-tools.js +17 -1
- package/v3/@claude-flow/cli/dist/src/mcp-tools/hive-mind-tools.d.ts +8 -0
- package/v3/@claude-flow/cli/dist/src/mcp-tools/hive-mind-tools.js +80 -2
- package/v3/@claude-flow/cli/dist/src/mcp-tools/policy-enforcer.d.ts +126 -0
- package/v3/@claude-flow/cli/dist/src/mcp-tools/policy-enforcer.js +177 -0
- package/v3/@claude-flow/cli/dist/src/memory/intelligence.d.ts +14 -2
- package/v3/@claude-flow/cli/dist/src/memory/intelligence.js +33 -12
- package/v3/@claude-flow/cli/dist/src/memory/memory-bridge.js +10 -2
- package/v3/@claude-flow/cli/dist/src/services/policy-runtime.js +63 -5
- package/v3/@claude-flow/cli/package.json +1 -1
- package/v3/@claude-flow/cli/dist/src/ruvector/diskann-backend.d.ts +0 -78
- package/v3/@claude-flow/cli/dist/src/ruvector/diskann-backend.js +0 -310
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AgenticPolicyEngine, createLegacyCompatibleState, } from '@claude-flow/security';
|
|
1
|
+
import { AgenticPolicyEngine, createLegacyCompatibleState, isMcpCallerAuthEnabled, decodeTokenEnvelope, publicKeyFromHex, verifyInvocationToken, } from '@claude-flow/security';
|
|
2
2
|
import { createHash, createHmac, randomBytes, randomUUID, timingSafeEqual } from 'node:crypto';
|
|
3
3
|
import { execFileSync } from 'node:child_process';
|
|
4
4
|
import { closeSync, existsSync, mkdirSync, openSync, readFileSync, realpathSync, renameSync, statSync, unlinkSync, writeFileSync, } from 'node:fs';
|
|
@@ -257,6 +257,67 @@ export async function revokePolicyApproval(id, projectRoot = process.cwd()) {
|
|
|
257
257
|
export async function verifyPolicyLedger(projectRoot = process.cwd()) {
|
|
258
258
|
return withPolicyTransaction(projectRoot, (engine) => engine.verifyLedger());
|
|
259
259
|
}
|
|
260
|
+
/**
|
|
261
|
+
* Resolve the caller identity for `authorizeMcpTool`.
|
|
262
|
+
*
|
|
263
|
+
* ADR-377 Phase 3 (dream-cycle candidate, 2026-08-26) — off by default. By
|
|
264
|
+
* default (`isMcpCallerAuthEnabled()` false), this reproduces the exact
|
|
265
|
+
* pre-existing two-line behavior: the plain, unsigned `CLAUDE_FLOW_PRINCIPAL_ID`
|
|
266
|
+
* env var is trusted outright. That is the byte-identical-when-disabled
|
|
267
|
+
* requirement for this candidate — nothing about this branch's shape or
|
|
268
|
+
* output changes when the flag is off.
|
|
269
|
+
*
|
|
270
|
+
* When the flag is on, `CLAUDE_FLOW_PRINCIPAL_ID` alone is no longer
|
|
271
|
+
* sufficient — any process can set an arbitrary env var with zero proof.
|
|
272
|
+
* Instead this requires (and verifies) an Ed25519-signed `InvocationToken`
|
|
273
|
+
* that only a private-key holder (the DualModeOrchestrator that spawned this
|
|
274
|
+
* worker) could have minted, per `mcp-caller-identity.ts`. Missing, corrupt,
|
|
275
|
+
* forged, or expired tokens fail closed — this function throws rather than
|
|
276
|
+
* falling back to the unverified env var, matching the existing fail-closed
|
|
277
|
+
* pattern already used above in this function (`invalid-worker-capability-envelope`,
|
|
278
|
+
* `authoritative-worker-policy-root-unavailable`).
|
|
279
|
+
*
|
|
280
|
+
* Scope trade-off (deliberate — see mcp-caller-identity.ts's file header and
|
|
281
|
+
* orchestrator.ts's `workerEnvironment` for the fuller rationale): the token
|
|
282
|
+
* is minted once per worker spawn with a worker-lifetime TTL and a wildcard
|
|
283
|
+
* `toolName: '*'`, not the single-tool, 30s-TTL shape `InvocationToken` was
|
|
284
|
+
* originally designed for — so `opts.toolName` is deliberately not passed to
|
|
285
|
+
* `verifyInvocationToken` here, and the tool-mismatch check is not exercised
|
|
286
|
+
* for this use case. What this retains: a process that never received the
|
|
287
|
+
* signed token cannot forge one, so it cannot impersonate `agent:<workerId>`.
|
|
288
|
+
* What it does NOT retain: a compromised worker process can still use its
|
|
289
|
+
* own valid token for every MCP call for the rest of its lifetime — the
|
|
290
|
+
* token does not limit blast radius to a single call.
|
|
291
|
+
*/
|
|
292
|
+
function resolveMcpCallerIdentity() {
|
|
293
|
+
if (!isMcpCallerAuthEnabled()) {
|
|
294
|
+
return {
|
|
295
|
+
id: process.env.CLAUDE_FLOW_PRINCIPAL_ID ?? 'legacy-cli',
|
|
296
|
+
type: process.env.CLAUDE_FLOW_PRINCIPAL_ID ? 'agent' : 'legacy',
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
const encodedToken = process.env.CLAUDE_FLOW_MCP_INVOCATION_TOKEN;
|
|
300
|
+
const publicKeyHex = process.env.CLAUDE_FLOW_MCP_CALLER_PUBKEY;
|
|
301
|
+
if (!encodedToken || !publicKeyHex) {
|
|
302
|
+
throw new Error('mcp-caller-auth-enabled-but-no-token');
|
|
303
|
+
}
|
|
304
|
+
const token = decodeTokenEnvelope(encodedToken);
|
|
305
|
+
if (!token) {
|
|
306
|
+
throw new Error('mcp-caller-auth-enabled-but-no-token');
|
|
307
|
+
}
|
|
308
|
+
let publicKey;
|
|
309
|
+
try {
|
|
310
|
+
publicKey = publicKeyFromHex(publicKeyHex);
|
|
311
|
+
}
|
|
312
|
+
catch {
|
|
313
|
+
throw new Error('mcp-caller-auth-enabled-but-no-token');
|
|
314
|
+
}
|
|
315
|
+
const result = verifyInvocationToken(token, publicKey, {});
|
|
316
|
+
if (!result.valid) {
|
|
317
|
+
throw new Error(`mcp-caller-auth-verification-failed:${result.reason}`);
|
|
318
|
+
}
|
|
319
|
+
return { id: token.callerId, type: 'agent' };
|
|
320
|
+
}
|
|
260
321
|
export async function authorizeMcpTool(toolName, input, context = {}, attributes = {}) {
|
|
261
322
|
let projectRoot = typeof context.projectRoot === 'string' ? context.projectRoot : process.cwd();
|
|
262
323
|
let processEnvelope;
|
|
@@ -284,10 +345,7 @@ export async function authorizeMcpTool(toolName, input, context = {}, attributes
|
|
|
284
345
|
}
|
|
285
346
|
}
|
|
286
347
|
return evaluatePolicyRequest({
|
|
287
|
-
identity:
|
|
288
|
-
id: process.env.CLAUDE_FLOW_PRINCIPAL_ID ?? 'legacy-cli',
|
|
289
|
-
type: process.env.CLAUDE_FLOW_PRINCIPAL_ID ? 'agent' : 'legacy',
|
|
290
|
-
},
|
|
348
|
+
identity: resolveMcpCallerIdentity(),
|
|
291
349
|
action: {
|
|
292
350
|
type: attributes.actionType ?? 'mcp.tool.call',
|
|
293
351
|
resource: toolName,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@claude-flow/cli",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.42.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Ruflo CLI - Enterprise AI agent orchestration with 60+ specialized agents, swarm coordination, MCP server, self-learning hooks, and vector memory for Claude Code",
|
|
6
6
|
"main": "dist/src/index.js",
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* DiskANN Vector Search Backend
|
|
3
|
-
*
|
|
4
|
-
* SSD-friendly approximate nearest neighbor search using Vamana graph.
|
|
5
|
-
* Falls back gracefully to HNSW (@ruvector/router VectorDb) or
|
|
6
|
-
* pure-JS cosine similarity when DiskANN is unavailable.
|
|
7
|
-
*
|
|
8
|
-
* @module v3/cli/ruvector/diskann-backend
|
|
9
|
-
*/
|
|
10
|
-
export interface DiskAnnConfig {
|
|
11
|
-
dim: number;
|
|
12
|
-
maxDegree?: number;
|
|
13
|
-
buildBeam?: number;
|
|
14
|
-
searchBeam?: number;
|
|
15
|
-
alpha?: number;
|
|
16
|
-
pqSubspaces?: number;
|
|
17
|
-
storagePath?: string;
|
|
18
|
-
}
|
|
19
|
-
export interface SearchResult {
|
|
20
|
-
id: string;
|
|
21
|
-
distance: number;
|
|
22
|
-
score: number;
|
|
23
|
-
}
|
|
24
|
-
export type VectorBackend = 'diskann' | 'hnsw' | 'cosine-js';
|
|
25
|
-
/**
|
|
26
|
-
* Check if @ruvector/diskann is available
|
|
27
|
-
*/
|
|
28
|
-
export declare function isDiskAnnAvailable(): Promise<boolean>;
|
|
29
|
-
/**
|
|
30
|
-
* Create or get a DiskANN index instance
|
|
31
|
-
*/
|
|
32
|
-
export declare function getDiskAnnIndex(config: DiskAnnConfig): Promise<{
|
|
33
|
-
index: any;
|
|
34
|
-
backend: VectorBackend;
|
|
35
|
-
}>;
|
|
36
|
-
/**
|
|
37
|
-
* Get the active backend name
|
|
38
|
-
*/
|
|
39
|
-
export declare function getActiveBackend(): VectorBackend;
|
|
40
|
-
/**
|
|
41
|
-
* Reset the index (for testing)
|
|
42
|
-
*/
|
|
43
|
-
export declare function resetIndex(): void;
|
|
44
|
-
/**
|
|
45
|
-
* Insert a vector into the active backend
|
|
46
|
-
*/
|
|
47
|
-
export declare function insertVector(id: string, vector: Float32Array, config?: DiskAnnConfig): Promise<{
|
|
48
|
-
backend: VectorBackend;
|
|
49
|
-
}>;
|
|
50
|
-
/**
|
|
51
|
-
* Build the index (required for DiskANN before search)
|
|
52
|
-
*/
|
|
53
|
-
export declare function buildIndex(config?: DiskAnnConfig): Promise<void>;
|
|
54
|
-
/**
|
|
55
|
-
* Search for k nearest neighbors
|
|
56
|
-
*/
|
|
57
|
-
export declare function searchVectors(query: Float32Array, k: number, config?: DiskAnnConfig): Promise<SearchResult[]>;
|
|
58
|
-
export interface BenchmarkResult {
|
|
59
|
-
backend: VectorBackend;
|
|
60
|
-
dim: number;
|
|
61
|
-
vectorCount: number;
|
|
62
|
-
insertTimeMs: number;
|
|
63
|
-
buildTimeMs: number;
|
|
64
|
-
searchTimeMs: number;
|
|
65
|
-
searchesPerSecond: number;
|
|
66
|
-
recall: number;
|
|
67
|
-
memoryMB: number;
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* Run a benchmark comparing available backends
|
|
71
|
-
*/
|
|
72
|
-
export declare function benchmark(opts?: {
|
|
73
|
-
dim?: number;
|
|
74
|
-
vectorCount?: number;
|
|
75
|
-
k?: number;
|
|
76
|
-
queries?: number;
|
|
77
|
-
}): Promise<BenchmarkResult[]>;
|
|
78
|
-
//# sourceMappingURL=diskann-backend.d.ts.map
|
|
@@ -1,310 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* DiskANN Vector Search Backend
|
|
3
|
-
*
|
|
4
|
-
* SSD-friendly approximate nearest neighbor search using Vamana graph.
|
|
5
|
-
* Falls back gracefully to HNSW (@ruvector/router VectorDb) or
|
|
6
|
-
* pure-JS cosine similarity when DiskANN is unavailable.
|
|
7
|
-
*
|
|
8
|
-
* @module v3/cli/ruvector/diskann-backend
|
|
9
|
-
*/
|
|
10
|
-
// ===== Lazy loading =====
|
|
11
|
-
let diskannInstance = null;
|
|
12
|
-
let diskannAvailable = null;
|
|
13
|
-
let activeBackend = 'cosine-js';
|
|
14
|
-
/**
|
|
15
|
-
* Check if @ruvector/diskann is available
|
|
16
|
-
*/
|
|
17
|
-
export async function isDiskAnnAvailable() {
|
|
18
|
-
if (diskannAvailable !== null)
|
|
19
|
-
return diskannAvailable;
|
|
20
|
-
try {
|
|
21
|
-
const { createRequire } = await import('module');
|
|
22
|
-
const require2 = createRequire(import.meta.url);
|
|
23
|
-
const mod = require2('@ruvector/diskann');
|
|
24
|
-
diskannAvailable = typeof mod.DiskAnn === 'function';
|
|
25
|
-
return diskannAvailable;
|
|
26
|
-
}
|
|
27
|
-
catch {
|
|
28
|
-
diskannAvailable = false;
|
|
29
|
-
return false;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Create or get a DiskANN index instance
|
|
34
|
-
*/
|
|
35
|
-
export async function getDiskAnnIndex(config) {
|
|
36
|
-
if (diskannInstance)
|
|
37
|
-
return { index: diskannInstance, backend: activeBackend };
|
|
38
|
-
// Try DiskANN first
|
|
39
|
-
if (await isDiskAnnAvailable()) {
|
|
40
|
-
try {
|
|
41
|
-
const { createRequire } = await import('module');
|
|
42
|
-
const require2 = createRequire(import.meta.url);
|
|
43
|
-
const { DiskAnn } = require2('@ruvector/diskann');
|
|
44
|
-
const index = new DiskAnn({
|
|
45
|
-
dim: config.dim,
|
|
46
|
-
maxDegree: config.maxDegree ?? 64,
|
|
47
|
-
buildBeam: config.buildBeam ?? 128,
|
|
48
|
-
searchBeam: config.searchBeam ?? 64,
|
|
49
|
-
alpha: config.alpha ?? 1.2,
|
|
50
|
-
pqSubspaces: config.pqSubspaces ?? 0,
|
|
51
|
-
storagePath: config.storagePath,
|
|
52
|
-
});
|
|
53
|
-
diskannInstance = index;
|
|
54
|
-
activeBackend = 'diskann';
|
|
55
|
-
return { index, backend: 'diskann' };
|
|
56
|
-
}
|
|
57
|
-
catch {
|
|
58
|
-
// Fall through
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
// Try HNSW (@ruvector/router VectorDb) as fallback
|
|
62
|
-
try {
|
|
63
|
-
const { createRequire } = await import('module');
|
|
64
|
-
const require2 = createRequire(import.meta.url);
|
|
65
|
-
const router = require2('@ruvector/router');
|
|
66
|
-
if (router.VectorDb && router.DistanceMetric) {
|
|
67
|
-
const index = new router.VectorDb({
|
|
68
|
-
dimensions: config.dim,
|
|
69
|
-
distanceMetric: router.DistanceMetric.Cosine,
|
|
70
|
-
hnswM: 16,
|
|
71
|
-
hnswEfConstruction: 200,
|
|
72
|
-
hnswEfSearch: 100,
|
|
73
|
-
});
|
|
74
|
-
diskannInstance = index;
|
|
75
|
-
activeBackend = 'hnsw';
|
|
76
|
-
return { index, backend: 'hnsw' };
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
catch {
|
|
80
|
-
// Fall through
|
|
81
|
-
}
|
|
82
|
-
// Pure JS fallback
|
|
83
|
-
const jsIndex = createJsFallbackIndex(config.dim);
|
|
84
|
-
diskannInstance = jsIndex;
|
|
85
|
-
activeBackend = 'cosine-js';
|
|
86
|
-
return { index: jsIndex, backend: 'cosine-js' };
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* Get the active backend name
|
|
90
|
-
*/
|
|
91
|
-
export function getActiveBackend() {
|
|
92
|
-
return activeBackend;
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* Reset the index (for testing)
|
|
96
|
-
*/
|
|
97
|
-
export function resetIndex() {
|
|
98
|
-
diskannInstance = null;
|
|
99
|
-
diskannAvailable = null;
|
|
100
|
-
activeBackend = 'cosine-js';
|
|
101
|
-
}
|
|
102
|
-
// ===== Unified search interface =====
|
|
103
|
-
/**
|
|
104
|
-
* Insert a vector into the active backend
|
|
105
|
-
*/
|
|
106
|
-
export async function insertVector(id, vector, config = { dim: 384 }) {
|
|
107
|
-
const { index, backend } = await getDiskAnnIndex(config);
|
|
108
|
-
if (backend === 'diskann') {
|
|
109
|
-
index.insert(id, vector);
|
|
110
|
-
}
|
|
111
|
-
else if (backend === 'hnsw') {
|
|
112
|
-
index.insert(id, vector);
|
|
113
|
-
}
|
|
114
|
-
else {
|
|
115
|
-
index.insert(id, vector);
|
|
116
|
-
}
|
|
117
|
-
return { backend };
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* Build the index (required for DiskANN before search)
|
|
121
|
-
*/
|
|
122
|
-
export async function buildIndex(config = { dim: 384 }) {
|
|
123
|
-
const { index, backend } = await getDiskAnnIndex(config);
|
|
124
|
-
if (backend === 'diskann' && typeof index.build === 'function') {
|
|
125
|
-
index.build();
|
|
126
|
-
}
|
|
127
|
-
// HNSW and JS fallback don't need explicit build
|
|
128
|
-
}
|
|
129
|
-
/**
|
|
130
|
-
* Search for k nearest neighbors
|
|
131
|
-
*/
|
|
132
|
-
export async function searchVectors(query, k, config = { dim: 384 }) {
|
|
133
|
-
const { index, backend } = await getDiskAnnIndex(config);
|
|
134
|
-
if (backend === 'diskann') {
|
|
135
|
-
const results = index.search(query, k);
|
|
136
|
-
return results.map((r) => ({
|
|
137
|
-
id: r.id,
|
|
138
|
-
distance: r.distance,
|
|
139
|
-
score: 1 / (1 + r.distance), // Convert L2 distance to similarity
|
|
140
|
-
}));
|
|
141
|
-
}
|
|
142
|
-
if (backend === 'hnsw') {
|
|
143
|
-
const results = index.search(query, k);
|
|
144
|
-
return results.map((r) => ({
|
|
145
|
-
id: r.id,
|
|
146
|
-
distance: r.score, // VectorDb returns distance as 'score'
|
|
147
|
-
score: 1 / (1 + r.score),
|
|
148
|
-
}));
|
|
149
|
-
}
|
|
150
|
-
// JS fallback
|
|
151
|
-
return index.search(query, k);
|
|
152
|
-
}
|
|
153
|
-
// ===== Pure JS fallback =====
|
|
154
|
-
function createJsFallbackIndex(dim) {
|
|
155
|
-
const vectors = new Map();
|
|
156
|
-
return {
|
|
157
|
-
insert(id, vector) {
|
|
158
|
-
vectors.set(id, new Float32Array(vector));
|
|
159
|
-
},
|
|
160
|
-
search(query, k) {
|
|
161
|
-
const results = [];
|
|
162
|
-
for (const [id, vec] of vectors) {
|
|
163
|
-
let dot = 0, normQ = 0, normV = 0;
|
|
164
|
-
for (let i = 0; i < dim; i++) {
|
|
165
|
-
dot += query[i] * vec[i];
|
|
166
|
-
normQ += query[i] * query[i];
|
|
167
|
-
normV += vec[i] * vec[i];
|
|
168
|
-
}
|
|
169
|
-
const cosine = dot / (Math.sqrt(normQ) * Math.sqrt(normV) || 1);
|
|
170
|
-
const distance = 1 - cosine;
|
|
171
|
-
results.push({ id, distance, score: cosine });
|
|
172
|
-
}
|
|
173
|
-
return results.sort((a, b) => a.distance - b.distance).slice(0, k);
|
|
174
|
-
},
|
|
175
|
-
count() { return vectors.size; },
|
|
176
|
-
delete(id) { return vectors.delete(id); },
|
|
177
|
-
build() { },
|
|
178
|
-
};
|
|
179
|
-
}
|
|
180
|
-
/**
|
|
181
|
-
* Run a benchmark comparing available backends
|
|
182
|
-
*/
|
|
183
|
-
export async function benchmark(opts = {}) {
|
|
184
|
-
const dim = opts.dim ?? 384;
|
|
185
|
-
const n = opts.vectorCount ?? 1000;
|
|
186
|
-
const k = opts.k ?? 10;
|
|
187
|
-
const queryCount = opts.queries ?? 100;
|
|
188
|
-
const results = [];
|
|
189
|
-
// Generate random vectors
|
|
190
|
-
const vectors = [];
|
|
191
|
-
for (let i = 0; i < n; i++) {
|
|
192
|
-
const v = new Float32Array(dim);
|
|
193
|
-
for (let j = 0; j < dim; j++)
|
|
194
|
-
v[j] = Math.random() * 2 - 1;
|
|
195
|
-
// Normalize
|
|
196
|
-
let norm = 0;
|
|
197
|
-
for (let j = 0; j < dim; j++)
|
|
198
|
-
norm += v[j] * v[j];
|
|
199
|
-
norm = Math.sqrt(norm);
|
|
200
|
-
for (let j = 0; j < dim; j++)
|
|
201
|
-
v[j] /= norm;
|
|
202
|
-
vectors.push([`vec-${i}`, v]);
|
|
203
|
-
}
|
|
204
|
-
// Generate query vectors
|
|
205
|
-
const queries = [];
|
|
206
|
-
for (let i = 0; i < queryCount; i++) {
|
|
207
|
-
const q = new Float32Array(dim);
|
|
208
|
-
for (let j = 0; j < dim; j++)
|
|
209
|
-
q[j] = Math.random() * 2 - 1;
|
|
210
|
-
let norm = 0;
|
|
211
|
-
for (let j = 0; j < dim; j++)
|
|
212
|
-
norm += q[j] * q[j];
|
|
213
|
-
norm = Math.sqrt(norm);
|
|
214
|
-
for (let j = 0; j < dim; j++)
|
|
215
|
-
q[j] /= norm;
|
|
216
|
-
queries.push(q);
|
|
217
|
-
}
|
|
218
|
-
// Brute force ground truth
|
|
219
|
-
function bruteForceSearch(query) {
|
|
220
|
-
const scores = [];
|
|
221
|
-
for (const [id, vec] of vectors) {
|
|
222
|
-
let dist = 0;
|
|
223
|
-
for (let j = 0; j < dim; j++) {
|
|
224
|
-
const d = query[j] - vec[j];
|
|
225
|
-
dist += d * d;
|
|
226
|
-
}
|
|
227
|
-
scores.push({ id, dist });
|
|
228
|
-
}
|
|
229
|
-
scores.sort((a, b) => a.dist - b.dist);
|
|
230
|
-
return scores.slice(0, k).map(s => s.id);
|
|
231
|
-
}
|
|
232
|
-
const groundTruth = queries.map(q => bruteForceSearch(q));
|
|
233
|
-
// Test each available backend
|
|
234
|
-
for (const backendName of ['diskann', 'hnsw', 'cosine-js']) {
|
|
235
|
-
resetIndex();
|
|
236
|
-
try {
|
|
237
|
-
let index;
|
|
238
|
-
const memBefore = process.memoryUsage().heapUsed;
|
|
239
|
-
if (backendName === 'diskann') {
|
|
240
|
-
if (!(await isDiskAnnAvailable()))
|
|
241
|
-
continue;
|
|
242
|
-
const { createRequire } = await import('module');
|
|
243
|
-
const require2 = createRequire(import.meta.url);
|
|
244
|
-
const { DiskAnn } = require2('@ruvector/diskann');
|
|
245
|
-
index = new DiskAnn({ dim, maxDegree: 64, buildBeam: 128, searchBeam: 64 });
|
|
246
|
-
}
|
|
247
|
-
else if (backendName === 'hnsw') {
|
|
248
|
-
try {
|
|
249
|
-
const { createRequire } = await import('module');
|
|
250
|
-
const require2 = createRequire(import.meta.url);
|
|
251
|
-
const router = require2('@ruvector/router');
|
|
252
|
-
if (!router.VectorDb)
|
|
253
|
-
continue;
|
|
254
|
-
index = new router.VectorDb({ dimensions: dim, distanceMetric: router.DistanceMetric.Cosine });
|
|
255
|
-
}
|
|
256
|
-
catch {
|
|
257
|
-
continue;
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
else {
|
|
261
|
-
index = createJsFallbackIndex(dim);
|
|
262
|
-
}
|
|
263
|
-
// Insert
|
|
264
|
-
const insertStart = performance.now();
|
|
265
|
-
for (const [id, vec] of vectors) {
|
|
266
|
-
index.insert(id, vec);
|
|
267
|
-
}
|
|
268
|
-
const insertTime = performance.now() - insertStart;
|
|
269
|
-
// Build
|
|
270
|
-
const buildStart = performance.now();
|
|
271
|
-
if (typeof index.build === 'function')
|
|
272
|
-
index.build();
|
|
273
|
-
const buildTime = performance.now() - buildStart;
|
|
274
|
-
// Search
|
|
275
|
-
const searchStart = performance.now();
|
|
276
|
-
const searchResults = [];
|
|
277
|
-
for (const q of queries) {
|
|
278
|
-
const r = index.search(q, k);
|
|
279
|
-
searchResults.push(r.map((x) => x.id));
|
|
280
|
-
}
|
|
281
|
-
const searchTime = performance.now() - searchStart;
|
|
282
|
-
// Recall vs ground truth
|
|
283
|
-
let totalRecall = 0;
|
|
284
|
-
for (let i = 0; i < queryCount; i++) {
|
|
285
|
-
const truth = new Set(groundTruth[i]);
|
|
286
|
-
const found = searchResults[i].filter(id => truth.has(id)).length;
|
|
287
|
-
totalRecall += found / k;
|
|
288
|
-
}
|
|
289
|
-
const recall = totalRecall / queryCount;
|
|
290
|
-
const memAfter = process.memoryUsage().heapUsed;
|
|
291
|
-
results.push({
|
|
292
|
-
backend: backendName,
|
|
293
|
-
dim,
|
|
294
|
-
vectorCount: n,
|
|
295
|
-
insertTimeMs: Math.round(insertTime * 100) / 100,
|
|
296
|
-
buildTimeMs: Math.round(buildTime * 100) / 100,
|
|
297
|
-
searchTimeMs: Math.round(searchTime * 100) / 100,
|
|
298
|
-
searchesPerSecond: Math.round(queryCount / (searchTime / 1000)),
|
|
299
|
-
recall: Math.round(recall * 1000) / 1000,
|
|
300
|
-
memoryMB: Math.round((memAfter - memBefore) / 1024 / 1024 * 100) / 100,
|
|
301
|
-
});
|
|
302
|
-
}
|
|
303
|
-
catch {
|
|
304
|
-
// Backend failed, skip
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
resetIndex();
|
|
308
|
-
return results;
|
|
309
|
-
}
|
|
310
|
-
//# sourceMappingURL=diskann-backend.js.map
|