claude-flow 3.62.0 → 3.63.1
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/helpers/.helpers-version +1 -0
- package/.claude/helpers/helpers.manifest.json +13 -0
- package/.claude/proven-config.json +42 -0
- package/.claude/settings.local.json +8 -0
- package/.claude-plugin/marketplace.json +3 -3
- package/README.md +2 -2
- package/node_modules/@claude-flow/codex/dist/cli.js +0 -0
- package/node_modules/@claude-flow/plugin-agent-federation/dist/bin.js +0 -0
- package/node_modules/@claude-flow/security/dist/input-validator.d.ts +6 -6
- package/package.json +1 -1
- package/v3/@claude-flow/cli/README.md +2 -2
- package/v3/@claude-flow/cli/catalog-manifest.json +3 -3
- package/v3/@claude-flow/cli/dist/src/mcp-tools/agentbbs-tools.js +7 -2
- package/v3/@claude-flow/cli/dist/src/mcp-tools/managed-agent-tools.d.ts +1 -20
- package/v3/@claude-flow/cli/dist/src/mcp-tools/managed-agent-tools.js +347 -108
- package/v3/@claude-flow/cli/dist/src/mcp-tools/managed-learning-tool.d.ts +4 -0
- package/v3/@claude-flow/cli/dist/src/mcp-tools/managed-learning-tool.js +46 -0
- package/v3/@claude-flow/cli/dist/src/mcp-tools/memory-tools.js +6 -3
- package/v3/@claude-flow/cli/dist/src/missions/schemas.d.ts +12 -12
- package/v3/@claude-flow/cli/dist/src/ruvector/diskann-backend.d.ts +78 -0
- package/v3/@claude-flow/cli/dist/src/ruvector/diskann-backend.js +310 -0
- package/v3/@claude-flow/cli/dist/src/services/flywheel-transaction.js +5 -0
- package/v3/@claude-flow/cli/dist/src/services/managed-learning-loop.d.ts +79 -0
- package/v3/@claude-flow/cli/dist/src/services/managed-learning-loop.js +76 -0
- package/v3/@claude-flow/cli/dist/src/services/managed-learning-store.d.ts +187 -0
- package/v3/@claude-flow/cli/dist/src/services/managed-learning-store.js +238 -0
- package/v3/@claude-flow/cli/dist/src/services/managed-learning.d.ts +108 -0
- package/v3/@claude-flow/cli/dist/src/services/managed-learning.js +197 -0
- package/v3/@claude-flow/cli/dist/src/services/managed-memory-client.d.ts +72 -0
- package/v3/@claude-flow/cli/dist/src/services/managed-memory-client.js +236 -0
- package/v3/@claude-flow/cli/dist/src/services/managed-memory-worker.d.ts +56 -0
- package/v3/@claude-flow/cli/dist/src/services/managed-memory-worker.js +161 -0
- package/v3/@claude-flow/cli/dist/src/services/managed-run-collector.d.ts +93 -0
- package/v3/@claude-flow/cli/dist/src/services/managed-run-collector.js +181 -0
- package/v3/@claude-flow/cli/dist/src/services/managed-run-store.d.ts +52 -0
- package/v3/@claude-flow/cli/dist/src/services/managed-run-store.js +119 -0
- package/v3/@claude-flow/cli/dist/src/services/managed-workflows.d.ts +101 -0
- package/v3/@claude-flow/cli/dist/src/services/managed-workflows.js +183 -0
- package/v3/@claude-flow/cli/package.json +1 -1
- package/v3/@claude-flow/shared/dist/events/event-store.test.d.ts +8 -0
- package/v3/@claude-flow/shared/dist/events/event-store.test.js +293 -0
- package/v3/@claude-flow/shared/dist/hooks/hooks.test.d.ts +9 -0
- package/v3/@claude-flow/shared/dist/hooks/hooks.test.js +322 -0
- package/v3/@claude-flow/shared/dist/hooks/verify-exports.test.d.ts +9 -0
- package/v3/@claude-flow/shared/dist/hooks/verify-exports.test.js +93 -0
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/** Learning MCP boundary, kept separate from provider transport and native workers. */
|
|
2
|
+
import { getProjectCwd } from './types.js';
|
|
3
|
+
export const managedLearningTool = {
|
|
4
|
+
inputSchema: { type: 'object', properties: {
|
|
5
|
+
action: { type: 'string', enum: ['status', 'record', 'recommend', 'evaluate', 'index', 'recall', 'cycle'] },
|
|
6
|
+
evidence: { type: 'object', description: 'Closed version1 observation and optional host signature; caller trust labels rejected' },
|
|
7
|
+
scope: { type: 'object', description: 'Exact taskType, policyHash and evaluatorHash cohort' },
|
|
8
|
+
manifest: { type: 'object', description: 'Frozen unique heldout taskHashes and at least three trials' },
|
|
9
|
+
cycle: { type: 'object', description: 'Closed scope/evidence batch, optional index, maxP95Ms and evaluation; no commands, keys or promotion flags' },
|
|
10
|
+
baselineId: { type: 'string' }, candidateId: { type: 'string' }, id: { type: 'string' }, maxP95Ms: { type: 'number' },
|
|
11
|
+
}, required: ['action'] },
|
|
12
|
+
handler: async (input) => {
|
|
13
|
+
if (input.action !== 'status' && process.env.RUFLO_MANAGED_LEARNING !== '1')
|
|
14
|
+
return { success: false, error: 'Managed learning writes require host RUFLO_MANAGED_LEARNING=1' };
|
|
15
|
+
const root = getProjectCwd(), key = process.env.RUFLO_MANAGED_VERIFIER_PUBLIC_KEY;
|
|
16
|
+
const authorize = () => process.env.RUFLO_MANAGED_LEARNING === '1' && process.env.RUFLO_MANAGED_VERIFIER_PUBLIC_KEY === key && getProjectCwd() === root;
|
|
17
|
+
try {
|
|
18
|
+
const learning = await import('../services/managed-learning-store.js');
|
|
19
|
+
if (input.action !== 'status' && !authorize())
|
|
20
|
+
throw new Error('Host learning policy changed');
|
|
21
|
+
if (input.action === 'cycle') {
|
|
22
|
+
if (Object.keys(input).some(k => !['action', 'cycle'].includes(k)))
|
|
23
|
+
throw new Error('Invalid cycle envelope');
|
|
24
|
+
const { cycleLearning } = await import('../services/managed-learning-loop.js');
|
|
25
|
+
return { success: true, ...await cycleLearning(root, input.cycle, key, authorize) };
|
|
26
|
+
}
|
|
27
|
+
if (input.action === 'status')
|
|
28
|
+
return { success: true, ...learning.learningStatus(root, key) };
|
|
29
|
+
if (input.action === 'record')
|
|
30
|
+
return { success: true, ...learning.recordLearning(root, input.evidence, key) };
|
|
31
|
+
if (input.action === 'recommend')
|
|
32
|
+
return { success: true, ...learning.recommendLearning(root, input.scope, key, input.maxP95Ms) };
|
|
33
|
+
if (input.action === 'evaluate')
|
|
34
|
+
return { success: true, ...learning.evaluateLearning(root, { scope: input.scope, manifest: input.manifest, baselineId: String(input.baselineId ?? ''), candidateId: String(input.candidateId ?? '') }, key) };
|
|
35
|
+
if (input.action === 'recall')
|
|
36
|
+
return { success: true, ...await learning.recallLearning(root, input.scope, key, authorize) };
|
|
37
|
+
if (input.action === 'index')
|
|
38
|
+
return { success: true, ...await learning.indexLearning(root, String(input.id ?? ''), key, authorize) };
|
|
39
|
+
return { success: false, error: 'Unknown managed learning action' };
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
return { success: false, error: 'Managed learning failed: validate metadata, verifier and store; state may persist, inspect status before retrying; no default changed' };
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
//# sourceMappingURL=managed-learning-tool.js.map
|
|
@@ -1466,13 +1466,16 @@ export const memoryTools = [
|
|
|
1466
1466
|
}
|
|
1467
1467
|
catch { /* namespace may not exist */ }
|
|
1468
1468
|
}
|
|
1469
|
-
// Sort by score, deduplicate by key, take top N
|
|
1469
|
+
// Sort by score, deduplicate by (namespace, key), take top N. An entry is
|
|
1470
|
+
// identified by both (see #2271), so the same key in another namespace is
|
|
1471
|
+
// a different entry and must not be dropped as a duplicate.
|
|
1470
1472
|
allResults.sort((a, b) => b.score - a.score);
|
|
1471
1473
|
const seen = new Set();
|
|
1472
1474
|
const deduplicated = allResults.filter(r => {
|
|
1473
|
-
|
|
1475
|
+
const id = JSON.stringify([r.namespace, r.key]);
|
|
1476
|
+
if (seen.has(id))
|
|
1474
1477
|
return false;
|
|
1475
|
-
seen.add(
|
|
1478
|
+
seen.add(id);
|
|
1476
1479
|
return true;
|
|
1477
1480
|
}).slice(0, limit);
|
|
1478
1481
|
return {
|
|
@@ -77,6 +77,10 @@ export declare const missionRecordSchema: z.ZodObject<{
|
|
|
77
77
|
updatedAt: string;
|
|
78
78
|
schemaVersion: 1;
|
|
79
79
|
state: "completed" | "cancelled" | "failed" | "blocked" | "running" | "paused" | "queued" | "draft" | "planned" | "awaitingAuthorization" | "pauseRequested" | "verifying" | "cancelRequested";
|
|
80
|
+
acceptance: {
|
|
81
|
+
revision: number;
|
|
82
|
+
criteriaDigest: string;
|
|
83
|
+
};
|
|
80
84
|
plan: {
|
|
81
85
|
revision: number;
|
|
82
86
|
digest: string;
|
|
@@ -92,10 +96,6 @@ export declare const missionRecordSchema: z.ZodObject<{
|
|
|
92
96
|
budgetRef: string;
|
|
93
97
|
executionMode: "durable-executor" | "session-bound";
|
|
94
98
|
evidenceRefs: string[];
|
|
95
|
-
acceptance: {
|
|
96
|
-
revision: number;
|
|
97
|
-
criteriaDigest: string;
|
|
98
|
-
};
|
|
99
99
|
lastEventSequence: number;
|
|
100
100
|
authorizationRef?: string | undefined;
|
|
101
101
|
}, {
|
|
@@ -103,6 +103,10 @@ export declare const missionRecordSchema: z.ZodObject<{
|
|
|
103
103
|
updatedAt: string;
|
|
104
104
|
schemaVersion: 1;
|
|
105
105
|
state: "completed" | "cancelled" | "failed" | "blocked" | "running" | "paused" | "queued" | "draft" | "planned" | "awaitingAuthorization" | "pauseRequested" | "verifying" | "cancelRequested";
|
|
106
|
+
acceptance: {
|
|
107
|
+
revision: number;
|
|
108
|
+
criteriaDigest: string;
|
|
109
|
+
};
|
|
106
110
|
plan: {
|
|
107
111
|
revision: number;
|
|
108
112
|
digest: string;
|
|
@@ -118,10 +122,6 @@ export declare const missionRecordSchema: z.ZodObject<{
|
|
|
118
122
|
budgetRef: string;
|
|
119
123
|
executionMode: "durable-executor" | "session-bound";
|
|
120
124
|
evidenceRefs: string[];
|
|
121
|
-
acceptance: {
|
|
122
|
-
revision: number;
|
|
123
|
-
criteriaDigest: string;
|
|
124
|
-
};
|
|
125
125
|
lastEventSequence: number;
|
|
126
126
|
authorizationRef?: string | undefined;
|
|
127
127
|
}>;
|
|
@@ -397,11 +397,11 @@ export declare const createInputSchema: z.ZodObject<{
|
|
|
397
397
|
requestId: z.ZodString;
|
|
398
398
|
objective: z.ZodString;
|
|
399
399
|
}, "strict", z.ZodTypeAny, {
|
|
400
|
-
objective: string;
|
|
401
400
|
requestId: string;
|
|
402
|
-
}, {
|
|
403
401
|
objective: string;
|
|
402
|
+
}, {
|
|
404
403
|
requestId: string;
|
|
404
|
+
objective: string;
|
|
405
405
|
}>;
|
|
406
406
|
export declare const planInputSchema: z.ZodObject<{
|
|
407
407
|
requestId: z.ZodString;
|
|
@@ -580,6 +580,7 @@ export declare const planInputSchema: z.ZodObject<{
|
|
|
580
580
|
} | undefined;
|
|
581
581
|
}>;
|
|
582
582
|
}, "strict", z.ZodTypeAny, {
|
|
583
|
+
requestId: string;
|
|
583
584
|
plan: {
|
|
584
585
|
tasks: {
|
|
585
586
|
id: string;
|
|
@@ -616,10 +617,10 @@ export declare const planInputSchema: z.ZodObject<{
|
|
|
616
617
|
baseline?: string | undefined;
|
|
617
618
|
}[];
|
|
618
619
|
};
|
|
619
|
-
requestId: string;
|
|
620
620
|
missionId: string;
|
|
621
621
|
expectedRevision: number;
|
|
622
622
|
}, {
|
|
623
|
+
requestId: string;
|
|
623
624
|
plan: {
|
|
624
625
|
tasks: {
|
|
625
626
|
id: string;
|
|
@@ -656,7 +657,6 @@ export declare const planInputSchema: z.ZodObject<{
|
|
|
656
657
|
capabilities?: string[] | undefined;
|
|
657
658
|
} | undefined;
|
|
658
659
|
};
|
|
659
|
-
requestId: string;
|
|
660
660
|
missionId: string;
|
|
661
661
|
expectedRevision: number;
|
|
662
662
|
}>;
|
|
@@ -0,0 +1,78 @@
|
|
|
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
|
|
@@ -0,0 +1,310 @@
|
|
|
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
|
|
@@ -91,6 +91,11 @@ function atomicWriteJson(file, value) {
|
|
|
91
91
|
fs.closeSync(fd);
|
|
92
92
|
}
|
|
93
93
|
fs.renameSync(tmp, file);
|
|
94
|
+
// Windows cannot flush a directory handle (FlushFileBuffers fails with EPERM on NTFS), and there the journaled rename needs no such step.
|
|
95
|
+
// Skipping it there is the same rule policy-runtime's syncDirectory follows (#3398). On POSIX a failure here means the rename may not be
|
|
96
|
+
// durable, so it still reaches the caller (#4492).
|
|
97
|
+
if (process.platform === 'win32')
|
|
98
|
+
return;
|
|
94
99
|
const dirFd = fs.openSync(path.dirname(file), fs.constants.O_RDONLY);
|
|
95
100
|
try {
|
|
96
101
|
fs.fsyncSync(dirFd);
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
export type ManagedCycleInput = {
|
|
2
|
+
scope: unknown;
|
|
3
|
+
evidence: unknown[];
|
|
4
|
+
index?: boolean;
|
|
5
|
+
maxP95Ms?: number;
|
|
6
|
+
evaluation?: {
|
|
7
|
+
manifest: unknown;
|
|
8
|
+
baselineId: string;
|
|
9
|
+
candidateId: string;
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
/** authorize is host-owned and rechecked across asynchronous boundaries. */
|
|
13
|
+
export declare function cycleLearning(root: string, value: unknown, publicKey: string | undefined, authorize: () => boolean): Promise<{
|
|
14
|
+
cycleId: string;
|
|
15
|
+
phases: string[];
|
|
16
|
+
recorded: string[];
|
|
17
|
+
duplicates: string[];
|
|
18
|
+
indexed: string[];
|
|
19
|
+
deferred: string[];
|
|
20
|
+
recommendation: {
|
|
21
|
+
advisory: boolean;
|
|
22
|
+
selected: {
|
|
23
|
+
strategy: {
|
|
24
|
+
id: string;
|
|
25
|
+
model: string;
|
|
26
|
+
workers: number;
|
|
27
|
+
template: string;
|
|
28
|
+
};
|
|
29
|
+
samples: number;
|
|
30
|
+
successes: number;
|
|
31
|
+
successRate: number;
|
|
32
|
+
successesPerDollar: number;
|
|
33
|
+
p95Ms: number;
|
|
34
|
+
evidenceIds: string[];
|
|
35
|
+
};
|
|
36
|
+
candidates: {
|
|
37
|
+
strategy: {
|
|
38
|
+
id: string;
|
|
39
|
+
model: string;
|
|
40
|
+
workers: number;
|
|
41
|
+
template: string;
|
|
42
|
+
};
|
|
43
|
+
samples: number;
|
|
44
|
+
successes: number;
|
|
45
|
+
successRate: number;
|
|
46
|
+
successesPerDollar: number;
|
|
47
|
+
p95Ms: number;
|
|
48
|
+
evidenceIds: string[];
|
|
49
|
+
}[];
|
|
50
|
+
abstained: boolean;
|
|
51
|
+
reason: string;
|
|
52
|
+
defaultsChanged: boolean;
|
|
53
|
+
};
|
|
54
|
+
evaluation: {
|
|
55
|
+
advisory: boolean;
|
|
56
|
+
eligible: boolean;
|
|
57
|
+
baselineP95Ms: number;
|
|
58
|
+
candidateP95Ms: number;
|
|
59
|
+
delta: number;
|
|
60
|
+
baselineEfficiency: number;
|
|
61
|
+
candidateEfficiency: number;
|
|
62
|
+
pairedResults: number;
|
|
63
|
+
corpusHash: string;
|
|
64
|
+
noRegression: boolean;
|
|
65
|
+
metaharness: import("./harness-benchmark.js").AcceptResult;
|
|
66
|
+
defaultsChanged: boolean;
|
|
67
|
+
evidenceIds: string[];
|
|
68
|
+
} | null;
|
|
69
|
+
memory: {
|
|
70
|
+
available: boolean;
|
|
71
|
+
recalled: number;
|
|
72
|
+
backend: string;
|
|
73
|
+
} | null;
|
|
74
|
+
elapsedMs: number;
|
|
75
|
+
advisory: boolean;
|
|
76
|
+
defaultsChanged: boolean;
|
|
77
|
+
promotionRequested: boolean;
|
|
78
|
+
}>;
|
|
79
|
+
//# sourceMappingURL=managed-learning-loop.d.ts.map
|