Repository navigation
Expand file tree
/
Copy pathTagMemoV10Engine.js
More file actions
339 lines (303 loc) · 11.7 KB
/
Copy pathTagMemoV10Engine.js
File metadata and controls
339 lines (303 loc) · 11.7 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
'use strict';
const { immutableSnapshot } = require(
'./modules/tagmemoV10/immutable'
);
const VERSION = 'v10_alpha';
const ALGORITHM_VERSION = 'v10.alpha.1';
const ARTIFACT_SCHEMA = 'tagmemo-v10-alpha-artifact-v1';
const QUERY_TRACE_SCHEMA = 'tagmemo-v10-alpha-query-trace-v1';
class TagMemoV10Engine {
constructor(db, tagIndex, config, ragParams, options = {}) {
this.tagIndex = tagIndex;
this.config = config || {};
this.ragParams = ragParams || {};
this.v9Engine = options.v9Engine || null;
this.getNativeRuntime = typeof options.getNativeRuntime === 'function'
? options.getNativeRuntime
: () => null;
this._activeArtifact = null;
this._artifactGeneration = 0;
// 生产图资产、RiverMemo 持久化和派生资产均由 Rust MemoRuntime
// 管理。JS 只保留配置、artifact 句柄和兼容门面。
this.artifactRepository = options.artifactRepository || null;
this.derivedAssetManager = options.derivedAssetManager || null;
}
updateRagParams(ragParams) {
this.ragParams = ragParams || {};
}
getConditionedOperatorCacheDiagnostics() {
return Object.freeze({
enabled: false,
limit: 0,
entries: 0,
hits: 0,
misses: 0,
bypasses: 0,
evictions: 0,
estimatedBytes: 0,
runtimeOwnership: 'rust-memo-runtime'
});
}
ensureExactDerivedAssets() {
const error = new Error(
'JS V10 exact derived asset maintenance is retired; use NativeKnowledgeRuntime'
);
error.code = 'TAGMEMO_JS_SQL_RETIRED';
throw error;
}
getDerivedAssetDiagnostics() {
return this.derivedAssetManager?.lastDiagnostics || null;
}
invalidateArtifact(reason = 'source-artifact-changed') {
const previous = this._activeArtifact;
this._activeArtifact = null;
if (previous) {
console.warn(
`[TagMemo-V10] 🧹 Active artifact invalidated: reason=${reason}, ` +
`artifact=${previous.artifactSig}, sourceV9=${previous.sourceArtifactSig}`
);
}
return previous;
}
_isArtifactCompatibleWithActiveV9(artifact) {
if (!artifact) return false;
const activeV9 = this.v9Engine?.getArtifactBundleSnapshot?.('v9');
return Boolean(
activeV9?.artifactSig
&& artifact.sourceArtifactSig === activeV9.artifactSig
);
}
getEffectiveConfig(overrides = {}) {
const { cleanRiverConfig } = require('./modules/knowledgeBase/memoConfig');
return immutableSnapshot(cleanRiverConfig({
...cleanRiverConfig(this.ragParams?.KnowledgeBaseManager?.riverMemo),
...overrides
}, true));
}
publishNativeArtifactHandle(nativeResult, options = {}) {
if (
!nativeResult?.artifactSig
|| !nativeResult?.sourceArtifactSig
|| nativeResult.persisted !== true
) {
throw new TypeError(
'Native Memo artifact handle requires persisted artifact/source signatures'
);
}
const generation = ++this._artifactGeneration;
const publishedAt = Number(options.publishedAt) || Date.now();
const effectiveConfig = options.effectiveConfig
|| this.getEffectiveConfig(options.config || {});
const artifact = Object.freeze({
schema: ARTIFACT_SCHEMA,
version: VERSION,
algorithmVersion:
nativeResult.algorithmVersion || 'memo.native-artifact-v1',
artifactSig: nativeResult.artifactSig,
generation,
nativeGeneration:
Number(nativeResult.generation) || null,
graphGeneration: nativeResult.graphGeneration || '',
databaseGeneration: nativeResult.databaseGeneration || '',
provenanceGeneration:
nativeResult.provenanceGeneration || '',
modelSig:
nativeResult.modelSig
|| this.v9Engine?.modelSig
|| this.config.modelSig
|| this.config.model
|| 'unknown-model',
configHash: nativeResult.configHash || null,
sourceArtifactSig: nativeResult.sourceArtifactSig,
sourceGraphGeneration:
nativeResult.sourceGraphGeneration
|| nativeResult.graphGeneration
|| '',
maxInbound: Math.max(
0,
Number(nativeResult.maxInbound) || 0
),
effectiveConfig,
nodeCount: Math.max(
0,
Number(nativeResult.nodeCount) || 0
),
edgeCount: Math.max(
0,
Number(nativeResult.edgeCount) || 0
),
storageMode: 'rust-memo-runtime',
residentAtPublication: nativeResult.resident === true,
publishedAt
});
this._activeArtifact = artifact;
console.log(
`[TagMemo-V10] 🦀 Native control handle published: ` +
`generation=${generation}, nativeGeneration=` +
`${artifact.nativeGeneration ?? 'lazy'}, ` +
`artifact=${artifact.artifactSig}, nodes=${artifact.nodeCount}, ` +
`edges=${artifact.edgeCount}, resident=` +
`${artifact.residentAtPublication}.`
);
return artifact;
}
publishArtifact(staging, options = {}) {
if (
!staging
|| staging.schema !== ARTIFACT_SCHEMA
|| staging.version !== VERSION
) {
throw new TypeError('Invalid V10 artifact staging object');
}
const generation = ++this._artifactGeneration;
const publishedAt = Number(options.publishedAt) || Date.now();
const nodeCount = Math.max(
0,
Number(staging.sharedTransport?.nodeCount) || 0
);
const edgeCount = Math.max(
0,
Number(staging.sharedTransport?.edgeCount) || 0
);
// Rust MemoRuntime 是生产图资产的唯一常驻所有者。JS 发布对象只保留
// 路由所需的签名、配置和诊断摘要,不保留 CSR、provenance 或 V9 借用视图。
const artifact = Object.freeze({
schema: staging.schema,
version: staging.version,
algorithmVersion: staging.algorithmVersion,
artifactSig: staging.artifactSig,
generation,
graphGeneration: staging.graphGeneration,
databaseGeneration: staging.databaseGeneration,
provenanceGeneration: staging.provenanceGeneration,
modelSig: staging.modelSig,
configHash: staging.configHash,
sourceArtifactSig: staging.sourceArtifactSig,
sourceGraphGeneration: staging.sourceGraphGeneration || '',
maxInbound: Math.max(0, Number(staging.maxInbound) || 0),
effectiveConfig: staging.effectiveConfig,
nodeCount,
edgeCount,
storageMode: 'rust-memo-runtime',
publishedAt
});
this._activeArtifact = artifact;
console.log(
`[TagMemo-V10] 📦 Native-owned artifact handle published: ` +
`generation=${generation}, artifact=${artifact.artifactSig}, ` +
`nodes=${nodeCount}, edges=${edgeCount}, ` +
`sourceV9=${artifact.sourceArtifactSig}`
);
return artifact;
}
_nativeArtifactUnavailableError(operation = 'getArtifactSnapshot') {
const error = new Error(
`${operation} requires a Rust-owned Memo artifact handle; ` +
'JavaScript artifact construction and SQLite persistence are retired'
);
error.code = 'TAGMEMO_NATIVE_ARTIFACT_UNAVAILABLE';
error.operation = operation;
error.retryable = true;
return error;
}
buildPersistAndPublishArtifact() {
throw this._nativeArtifactUnavailableError(
'buildPersistAndPublishArtifact'
);
}
loadCompatiblePersistedArtifact() {
throw this._nativeArtifactUnavailableError(
'loadCompatiblePersistedArtifact'
);
}
restoreOrBuildArtifact() {
throw this._nativeArtifactUnavailableError('restoreOrBuildArtifact');
}
buildAndPublishArtifact() {
throw this._nativeArtifactUnavailableError('buildAndPublishArtifact');
}
getArtifactSnapshot(options = {}) {
if (
this._activeArtifact
&& !this._isArtifactCompatibleWithActiveV9(this._activeArtifact)
) {
this.invalidateArtifact('active-v9-signature-mismatch');
}
if (!this._activeArtifact && options.buildIfMissing !== false) {
throw this._nativeArtifactUnavailableError('getArtifactSnapshot');
}
return this._activeArtifact;
}
_retiredJsGraphOperation(operation) {
const error = new Error(
`${operation} is retired; use NativeKnowledgeRuntime`
);
error.code = 'TAGMEMO_JS_GRAPH_RUNTIME_RETIRED';
error.retryable = false;
return error;
}
createSourceFieldFromVector() {
throw this._retiredJsGraphOperation('createSourceFieldFromVector()');
}
projectDualFieldVectors() {
throw this._retiredJsGraphOperation('projectDualFieldVectors()');
}
projectFieldVector() {
throw this._retiredJsGraphOperation('projectFieldVector()');
}
prepareQuery() {
throw this._retiredJsGraphOperation(
'prepareQuery(); use KnowledgeBaseManager.prepareUnifiedMemoObservation()'
);
}
createQueryState() {
throw this._retiredJsGraphOperation('createQueryState()');
}
buildCandidateSuperset() {
throw this._retiredJsGraphOperation('buildCandidateSuperset()');
}
projectCandidateCurves() {
throw this._retiredJsGraphOperation('projectCandidateCurves()');
}
evaluateCandidateCurves() {
throw this._retiredJsGraphOperation('evaluateCandidateCurves()');
}
computeDstcObservables() {
throw this._retiredJsGraphOperation('computeDstcObservables()');
}
scoreExperimentArm() {
throw this._retiredJsGraphOperation('scoreExperimentArm()');
}
runExperimentArms() {
throw this._retiredJsGraphOperation('runExperimentArms()');
}
solveQueryState() {
throw this._retiredJsGraphOperation(
'solveQueryState(); use Rust MemoRuntime'
);
}
assertReplayCompatible(queryState, artifact = null) {
const snapshot = artifact || this.getArtifactSnapshot({ buildIfMissing: false });
if (!queryState || queryState.schema !== QUERY_TRACE_SCHEMA) {
throw new TypeError('Invalid V10 query trace schema');
}
const mismatches = [];
if (!snapshot) mismatches.push('artifact-unavailable');
else {
if (queryState.artifactSig !== snapshot.artifactSig) mismatches.push('artifactSig');
if (queryState.configHash !== snapshot.configHash) mismatches.push('configHash');
if (queryState.databaseGeneration !== snapshot.databaseGeneration) {
mismatches.push('databaseGeneration');
}
}
return Object.freeze({
compatible: mismatches.length === 0,
mismatches: Object.freeze(mismatches)
});
}
}
TagMemoV10Engine.VERSION = VERSION;
TagMemoV10Engine.ALGORITHM_VERSION = ALGORITHM_VERSION;
TagMemoV10Engine.ARTIFACT_SCHEMA = ARTIFACT_SCHEMA;
TagMemoV10Engine.QUERY_TRACE_SCHEMA = QUERY_TRACE_SCHEMA;
module.exports = TagMemoV10Engine;