Repository navigation
Expand file tree
/
Copy pathRiverMemoEngine.js
More file actions
606 lines (582 loc) · 25.8 KB
/
Copy pathRiverMemoEngine.js
File metadata and controls
606 lines (582 loc) · 25.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
'use strict';
const { cleanRiverConfig } = require('./modules/knowledgeBase/memoConfig');
const {
computeRiverObservability
} = require('./modules/tagmemoV10/riverObservability');
const VERSION = 'rivermemo_v1';
const ALGORITHM_VERSION = 'rivermemo.topology-v3.1';
const RESULT_SCHEMA = 'rivermemo-topology-v3-result-v1';
function clamp01(value) {
return Math.max(0, Math.min(1, Number(value) || 0));
}
function candidateId(candidate) {
const id = Number(candidate?.id ?? candidate?.chunkId ?? candidate?.label);
return Number.isFinite(id) && id > 0 ? id : null;
}
class RiverMemoEngine {
constructor(v10Engine, options = {}) {
if (!v10Engine) {
throw new TypeError('RiverMemoEngine requires a TagMemoV10Engine runtime');
}
this.runtime = v10Engine;
this.config = options.config || {};
}
updateConfig(config = {}) {
this.config = config || {};
}
getArtifactSnapshot(options = {}) {
return this.runtime.getArtifactSnapshot(options);
}
measureOmega(queryState, options = {}) {
const artifact = options.artifact
|| this.runtime.getArtifactSnapshot({ buildIfMissing: false });
const config = {
...(artifact?.effectiveConfig?.riverObservability || {}),
...(this.config?.riverObservability || {}),
...(options.config || {})
};
return computeRiverObservability(queryState, {
...config,
structRoleMinOmega:
options.structRoleMinOmega
?? cleanRiverConfig(artifact?.effectiveConfig).topology?.structRoleMinOmega
});
}
_nativeConfig(artifact, options = {}) {
const candidate = {
...(artifact.effectiveConfig?.candidateSuperset || {}),
...(this.config?.candidateSuperset || {}),
...(options.candidateSuperset || {})
};
const path = {
...(artifact.effectiveConfig?.pathGeometry || {}),
...(options.pathEvaluation || {})
};
const relative = {
...(artifact.effectiveConfig?.relativeTopology || {}),
...(options.pathEvaluation?.relativeTopology || {})
};
const omega = {
...(artifact.effectiveConfig?.riverObservability || {}),
...(this.config?.riverObservability || {}),
...(options.riverObservability || {})
};
const anchor = {
...(artifact.effectiveConfig?.directAnchor || {}),
...(this.config?.directAnchor || {})
};
const topology = {
...(cleanRiverConfig(artifact.effectiveConfig).topology || {}),
...(cleanRiverConfig(options).topology || {})
};
return {
queryK: candidate.queryK,
denoisedK: candidate.denoisedK ?? candidate.queryK,
localFieldK: candidate.localFieldK,
transferFieldK: candidate.transferFieldK,
bm25K: candidate.bm25K,
anchorK: candidate.anchorK,
maxUnionCandidates: candidate.maxUnionCandidates,
localWeight: path.localWeight,
transferWeight: path.transferWeight,
directionFloor: path.directionFloor,
closureFloor: path.closureFloor,
semanticNodeThreshold: relative.semanticNodeThreshold,
relativeDistanceTemperature: relative.relativeDistanceTemperature,
reverseDirectionCredit: relative.reverseDirectionCredit,
minimumRiverEdgeFlow: relative.minimumRiverEdgeFlow,
maximumRiverEdges: relative.maximumRiverEdges,
nodeOnlyReliabilityCap: relative.nodeOnlyReliabilityCap,
kappaEdge: omega.kappaEdge,
kappaRatio: omega.kappaRatio,
omegaEpsilon: omega.epsilon,
collapsedThreshold: omega.collapsedThreshold,
sparseThreshold: omega.sparseThreshold,
semanticAnchorThreshold: anchor.semanticThreshold,
semanticAnchorDiscount: anchor.semanticDiscount,
specificityFloor: anchor.specificityFloor,
rarityFloor: anchor.rarityFloor,
reliabilitySeedSaturation: anchor.reliabilitySeedSaturation,
fallbackReliabilityCap: anchor.fallbackReliabilityCap,
topologyBonusCap: topology.topologyBonusCap,
topologyPathSaturation: topology.topologyPathSaturation,
conditionalBandwidth: topology.conditionalBandwidth,
conditionalClosureBandwidth: topology.conditionalClosureBandwidth,
conditionalDirectBandwidth: topology.conditionalDirectBandwidth,
minimumPeers: topology.minimumPeers,
minimumEffectivePeers: topology.minimumEffectivePeers,
innovationConfidenceZ: topology.innovationConfidenceZ,
innovationScale: topology.innovationScale,
omegaGamma: topology.omegaGamma,
structRoleMinOmega: topology.structRoleMinOmega,
anchorBonusCap: topology.anchorBonusCap,
anchorActivationZ: topology.anchorActivationZ,
anchorActivationFloor: topology.anchorActivationFloor,
anchorSaturation: topology.anchorSaturation,
anchorFrontierContrast: topology.anchorFrontierContrast,
anchorFrontierAbsFloor: topology.anchorFrontierAbsFloor
};
}
async _rerankNative(query, inputCandidates, agentContext, options, artifact, prepared) {
const queryState = prepared.queryState;
const river = queryState.queryRiverGraph || {};
const fieldProvenance = (
Array.isArray(queryState.sourceObservation?.fieldProvenance)
? queryState.sourceObservation.fieldProvenance
: []
).map(entry => ({
id: Number(entry?.[0]),
hop: Number(entry?.[1]?.hop) || 0,
sourceType: String(entry?.[1]?.sourceType || '')
}));
const dbPath = String(options.dbPath || '');
if (!dbPath || dbPath === ':memory:') {
const error = new Error(
'RiverMemo native Topology V3 requires a file-backed SQLite database path.'
);
error.code = 'RIVERMEMO_NATIVE_DB_PATH_UNAVAILABLE';
throw error;
}
const observationHandle = typeof options.observationHandle === 'string'
&& options.observationHandle
? options.observationHandle
: null;
const payload = {
observationHandle,
dimension: Number(this.runtime.config.dimension),
topK: Math.max(
1,
Math.floor(Number(options.topK) || inputCandidates.length)
),
includeTrace: options.includeTrace === true,
query: {
text: String(query?.text || ''),
vector: observationHandle
? []
: Array.from(query?.vector || options.vector || [])
},
...(observationHandle ? {} : {
denoisedVector: Array.from(prepared.denoisedVector),
localVector: Array.from(prepared.localVector),
transferVector: Array.from(prepared.transferVector)
}),
candidates: inputCandidates.map(candidate => ({
id: candidateId(candidate),
score: Number(candidate.score) || 0,
hybridScore: Number(candidate.hybridScore) || 0,
vectorScore: Number(candidate.vectorScore) || 0,
bm25Score: Number(candidate.bm25Score) || 0,
timeScore: Number(candidate.timeScore) || 0,
anchorScore: Number(candidate.anchorScore) || 0
})).filter(candidate => candidate.id !== null),
queryState: {
queryId: queryState.queryId,
completeObservation: observationHandle
? true
: queryState.sourceObservation?.diagnostics
?.completeObservation === true,
...(observationHandle ? {} : {
sourceField: queryState.sourceField || [],
localField: queryState.localField || [],
transferField: queryState.transferField || [],
localDomainIds: queryState.localDomain?.ids || [],
transferDomainIds: queryState.transferDomain?.ids || [],
riverNodes: Array.isArray(river.nodes) ? river.nodes : [],
riverEdges: Array.isArray(river.edges) ? river.edges : [],
fieldProvenance
})
},
allowedFileIds: Array.isArray(agentContext.allowedFileIds)
? agentContext.allowedFileIds.map(Number).filter(Number.isFinite)
: [],
config: this._nativeConfig(artifact, options)
};
const nativeStartedAt = Date.now();
const inputJson = JSON.stringify(payload);
const tagIndex = this.runtime.tagIndex;
const nativeKnowledgeRuntime = options.nativeKnowledgeRuntime || null;
const jointRequested = options.nativeJointQuery === true;
let jointUsed = false;
let jointFallbackReason = null;
let nativePayload;
if (jointRequested) {
const hybridPlan = options.nativeHybridPlan
&& typeof options.nativeHybridPlan === 'object'
? options.nativeHybridPlan
: null;
const supplementalVectors = options.nativeSupplementalVectors instanceof Float32Array
? options.nativeSupplementalVectors
: new Float32Array(options.nativeSupplementalVectors || []);
const useHybridAbi = !!hybridPlan;
const nativeMethodAvailable = nativeKnowledgeRuntime && (
useHybridAbi
? typeof nativeKnowledgeRuntime.executeRiverQueryHybrid === 'function'
: typeof nativeKnowledgeRuntime.executeRiverQuery === 'function'
);
if (!nativeMethodAvailable) {
const error = new Error(
useHybridAbi
? 'NativeKnowledgeRuntime hybrid River ABI is unavailable'
: 'NativeKnowledgeRuntime joint River ABI is unavailable'
);
error.code = 'NATIVE_RIVER_QUERY_ABI_UNAVAILABLE';
if (options.nativeJointFallbackToLegacy === false) throw error;
jointFallbackReason = error.code;
} else {
try {
const diaryNames = [...new Set(
(Array.isArray(agentContext.diaryNames)
? agentContext.diaryNames
: []
).map(name => String(name || '').trim()).filter(Boolean)
)];
if (diaryNames.length === 0) {
const error = new Error(
'Native joint River query requires an explicit diary scope'
);
error.code = 'NATIVE_RIVER_QUERY_EMPTY_DIARY_SCOPE';
throw error;
}
const annVector = options.nativeAnnVector || query?.vector;
const queryVector = annVector instanceof Float32Array
? annVector
: new Float32Array(annVector || []);
const nativePerIndexK = Math.max(
1,
Math.floor(Number(options.nativePerIndexK) || 300)
);
const nativeCandidateK = Math.max(
1,
Math.floor(Number(
options.nativeCandidateK
?? inputCandidates.length
?? 300
) || 300)
);
const nativeSemanticThreshold = Math.max(
-1,
Math.min(
1,
Number(options.nativeSemanticThreshold) || 0.92
)
);
if (useHybridAbi) {
nativePayload = await nativeKnowledgeRuntime.executeRiverQueryHybrid(
dbPath,
artifact.artifactSig,
inputJson,
diaryNames,
queryVector,
supplementalVectors,
JSON.stringify(hybridPlan),
nativePerIndexK,
nativeCandidateK,
nativeSemanticThreshold
);
} else {
nativePayload = await nativeKnowledgeRuntime.executeRiverQuery(
dbPath,
artifact.artifactSig,
inputJson,
diaryNames,
queryVector,
nativePerIndexK,
nativeCandidateK,
nativeSemanticThreshold
);
}
jointUsed = true;
} catch (error) {
if (options.nativeJointFallbackToLegacy === false) throw error;
jointFallbackReason =
error.code || error.message || 'native-joint-failed';
console.warn(
`[RiverMemo] Native joint query failed; falling back to ` +
`legacy native Topology input: ${error.message}`
);
}
}
}
if (!jointUsed) {
if (typeof tagIndex?.rerankRivermemoTopologyV3 !== 'function') {
const error = new Error(
'RiverMemo requires the VexusIndex-owned native runtime; rebuild rust-vexus-lite.'
);
error.code = 'RIVERMEMO_UNIFIED_NATIVE_RUNTIME_UNAVAILABLE';
throw error;
}
nativePayload = await tagIndex.rerankRivermemoTopologyV3(
dbPath,
artifact.artifactSig,
inputJson
);
}
const nativeResult = JSON.parse(nativePayload);
const memoRuntimeStats = typeof tagIndex?.memoRuntimeStats === 'function'
? tagIndex.memoRuntimeStats()
: null;
const inputById = new Map(
inputCandidates
.map(candidate => [candidateId(candidate), candidate])
.filter(([id]) => id !== null)
);
// 联合查询优先消费 Rust 在同一读事务内回填的最终正文。
// 仅旧 ABI 缺少该字段时才经异步门面补水;不得复用调用方旧正文。
if (jointUsed) {
inputById.clear();
for (const item of Array.isArray(nativeResult.results) ? nativeResult.results : []) {
const id = Number(item?.chunkId ?? item?.id);
const row = item?.hydratedChunk;
if (!Number.isSafeInteger(id) || id <= 0 || !row || typeof row !== 'object') continue;
inputById.set(id, {
...row,
id,
chunkId: id,
fullPath: row.fullPath || row.sourceFile,
source: row.source || 'rag'
});
}
}
if (jointUsed && typeof options.hydrateChunksByIds === 'function') {
const finalIds = [...new Set(
(Array.isArray(nativeResult.results)
? nativeResult.results
: []
).map(item => Number(item?.chunkId ?? item?.id))
.filter(id => Number.isSafeInteger(id) && id > 0 && !inputById.has(id))
)];
if (finalIds.length > 0) {
const rows = await options.hydrateChunksByIds(finalIds);
for (const row of Array.isArray(rows) ? rows : []) {
const id = Number(row?.id ?? row?.chunkId);
if (!Number.isSafeInteger(id) || id <= 0) continue;
inputById.set(id, {
...row,
id,
chunkId: id,
fullPath: row.fullPath || row.sourceFile,
source: row.source || 'rag'
});
}
}
}
const sourceObservation = queryState.sourceObservation || {};
const queryMatchedTags = Object.freeze(
[...new Set(
(Array.isArray(sourceObservation.matchedTags)
? sourceObservation.matchedTags
: [])
.map(tag => String(tag || '').trim())
.filter(Boolean)
)]
);
const queryCoreTagsMatched = Object.freeze(
[...new Set(
(Array.isArray(sourceObservation.coreTagsMatched)
? sourceObservation.coreTagsMatched
: [])
.map(tag => String(tag || '').trim())
.filter(Boolean)
)]
);
const queryCoreTagSet = new Set(
queryCoreTagsMatched.map(tag => tag.toLowerCase())
);
const includeTrace = options.includeTrace === true;
const results = (Array.isArray(nativeResult.results)
? nativeResult.results
: []
).map(item => {
const original = inputById.get(Number(item.chunkId)) || {};
const rawCandidateSources = (Array.isArray(item.candidateSources)
? item.candidateSources
: []
).map(source => typeof source === 'string'
? source
: String(source?.source || '')
).filter(Boolean);
const resolvedSource = rawCandidateSources.includes('time')
? 'time'
: (rawCandidateSources.includes('bm25')
? (options.nativeHybridPlan?.bm25Mode === 'body'
? 'bm25_body'
: 'bm25_tag')
: (original.source || 'rag'));
const matchedTags = Object.freeze(
[...new Set(
(Array.isArray(item.matchedTags) ? item.matchedTags : [])
.map(tag => String(tag || '').trim())
.filter(Boolean)
)]
);
const stable = {
...original,
id: Number(item.chunkId),
chunkId: Number(item.chunkId),
source: resolvedSource,
rank: Number(item.rank),
score: clamp01(item.score),
originalScore: Number(item.originalScore) || 0,
baseScore: clamp01(item.baseScore),
topologyBonus: clamp01(item.topologyBonus),
anchorBonus: clamp01(item.anchorBonus),
matchedTags,
coreTagsMatched: Object.freeze(
matchedTags.filter(tag =>
queryCoreTagSet.has(tag.toLowerCase())
)
),
tagMatchCount: matchedTags.length,
role: item.role || 'thematic_neighbor',
omega: clamp01(item.omega),
riverRegime:
item.riverRegime
|| nativeResult.omega?.regime
|| 'collapsed',
candidateSources: Object.freeze(
rawCandidateSources
.map(source => typeof source === 'string'
? Object.freeze({ source })
: Object.freeze({ ...source }))
)
};
if (!includeTrace) return Object.freeze(stable);
return Object.freeze({
...stable,
topologyV3: item.topologyV3 || null,
topologyV2: null,
relativeTopology: item.relativeTopology || null,
geometry: item.geometry || null,
observables: item.observables || null
});
});
return Object.freeze({
schema: RESULT_SCHEMA,
version: VERSION,
algorithmVersion: ALGORITHM_VERSION,
artifactSig: artifact.artifactSig,
artifactGeneration: artifact.generation,
queryId: queryState.queryId,
omega: Object.freeze(nativeResult.omega || {}),
queryTags: Object.freeze({
matchedTags: queryMatchedTags,
coreTagsMatched: queryCoreTagsMatched,
sourceMode: sourceObservation.sourceMode || null
}),
results: Object.freeze(results),
diagnostics: Object.freeze({
offeredCandidates: inputCandidates.length,
projectedCandidates:
nativeResult.diagnostics?.projectedCandidates || 0,
selectedCandidates:
nativeResult.diagnostics?.selectedCandidates || 0,
rankedCandidates:
nativeResult.diagnostics?.rankedCandidates || 0,
returnedCandidates: results.length,
nativeTopologyV3: Object.freeze({
...(nativeResult.diagnostics || {}),
ffiTotalMs: Date.now() - nativeStartedAt,
hybridPlanUsed: jointUsed && !!options.nativeHybridPlan,
runtimeOwnership: jointUsed
? 'native-knowledge-runtime-joint'
: 'vexus-index-instance',
jointRequested,
jointUsed,
jointFallbackReason,
memoRuntime: memoRuntimeStats
? Object.freeze({ ...memoRuntimeStats })
: null
}),
exactDerivedAssets:
this.runtime.getDerivedAssetDiagnostics?.() || null,
fieldProjection: prepared.fieldProjectionDiagnostics || null,
preparationTimings: prepared.preparationTimings || null,
field: queryState.fieldDiagnostics,
queryProfile: Object.freeze({
mode: nativeResult.queryMode || null,
backend: 'rust-rayon'
}),
anchorBatchAvailable: true
})
});
}
async rerank(query, candidates, agentContext = {}, options = {}) {
const rerankStartedAt = Date.now();
const stageTimings = {};
let stageStartedAt = rerankStartedAt;
const markStage = name => {
const now = Date.now();
stageTimings[name] = now - stageStartedAt;
stageStartedAt = now;
};
const inputCandidates = Array.isArray(candidates) ? candidates : [];
if (inputCandidates.length === 0) {
return Object.freeze({
schema: RESULT_SCHEMA,
version: VERSION,
algorithmVersion: ALGORITHM_VERSION,
artifactSig: null,
queryId: null,
omega: null,
results: Object.freeze([]),
diagnostics: Object.freeze({
offeredCandidates: 0,
rankedCandidates: 0,
reason: 'empty-candidate-set'
})
});
}
const artifact = options.artifact || this.runtime.getArtifactSnapshot();
if (!artifact) {
const error = new Error('RiverMemo artifact is unavailable');
error.code = 'RIVERMEMO_ARTIFACT_UNAVAILABLE';
throw error;
}
const queryVectorRaw = query?.vector || options.vector;
const queryVector = queryVectorRaw instanceof Float32Array
? queryVectorRaw
: new Float32Array(queryVectorRaw || []);
if (queryVector.length !== Number(this.runtime.config.dimension)) {
throw new RangeError(
`RiverMemo query vector dimension must be ${this.runtime.config.dimension}, got ${queryVector.length}`
);
}
// 生产统一管线已经在 Rust MemoRuntime 内完成 Local/Transfer
// scaled-resolvent、有效支持域和向量投影。禁止再次调用 JS
// prepareQuery()/solveQueryState() 持有并遍历第二份 CSR。
const prepared = options.nativePreparedQuery || null;
if (!prepared) {
const error = new Error(
'RiverMemo requires Rust-native prepared dual fields'
);
error.code = 'RIVERMEMO_NATIVE_DUAL_FIELDS_UNAVAILABLE';
throw error;
}
markStage('prepareQueryMs');
if (
!prepared?.queryState
|| !prepared.denoisedVector
|| !prepared.localVector
|| !prepared.transferVector
) {
const error = new Error('RiverMemo could not construct the continuous query fields');
error.code = 'RIVERMEMO_QUERY_FIELD_UNAVAILABLE';
throw error;
}
// Topology V3 的候选投影、路径几何、相对拓扑、DSTC、Direct Anchor、
// 批级条件创新和最终排序全部由 Rust/Rayon 在一个 N-API 调用内完成。
// JS 仅准备查询连续场并消费稳定结果,禁止再进入旧的逐候选计算链。
return await this._rerankNative(
query,
inputCandidates,
agentContext,
options,
artifact,
prepared
);
}
}
RiverMemoEngine.VERSION = VERSION;
RiverMemoEngine.ALGORITHM_VERSION = ALGORITHM_VERSION;
RiverMemoEngine.RESULT_SCHEMA = RESULT_SCHEMA;
module.exports = RiverMemoEngine;