@modusensus/dsh-mneme 0.8.13 → 0.8.15
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/LICENSE +21 -21
- package/README.md +655 -626
- package/assets/icon.svg +3 -3
- package/bin/cli.mjs +689 -689
- package/bin/dsh-mneme-mcp.mjs +510 -510
- package/bin/dsh-mneme-serve.mjs +151 -0
- package/cordis.patch.yml +21 -21
- package/docs/CONFIGURATION.md +263 -0
- package/docs/CONTINUITY.md +124 -0
- package/docs/DAEMON.md +46 -0
- package/docs/ENTITIES.md +245 -0
- package/docs/LOCAL_MODEL.md +222 -0
- package/docs/SEMANTIC.md +256 -0
- package/docs/SLEEP.md +164 -0
- package/docs/STORAGE.md +89 -0
- package/lib/api-standalone.js +673 -572
- package/lib/api.js +1437 -1433
- package/lib/bootstrap.js +211 -211
- package/lib/client.js +5444 -5295
- package/lib/commands.js +95 -95
- package/lib/config.js +880 -836
- package/lib/content-hash.js +54 -54
- package/lib/continuity.js +220 -220
- package/lib/document-index.js +94 -94
- package/lib/document.js +303 -303
- package/lib/dream/clustering.js +118 -118
- package/lib/dream/decisions.js +674 -674
- package/lib/dream/narratives.js +44 -44
- package/lib/dream/sleep.js +819 -819
- package/lib/dream.js +1501 -1501
- package/lib/embedding.js +177 -177
- package/lib/entities/extractor.js +252 -252
- package/lib/graph/anchoring.js +179 -179
- package/lib/guide.js +74 -74
- package/lib/heat.js +144 -144
- package/lib/hot-memory.js +46 -46
- package/lib/index.js +535 -671
- package/lib/inject.js +464 -450
- package/lib/lang.js +248 -229
- package/lib/local-embedder.js +358 -358
- package/lib/maintenance.js +142 -142
- package/lib/mirror.js +330 -330
- package/lib/organize.js +450 -450
- package/lib/peak-hours.js +117 -117
- package/lib/placement.js +13 -0
- package/lib/pre-inject-gate.js +340 -0
- package/lib/quality-filter.js +126 -126
- package/lib/recall-stats.js +175 -175
- package/lib/reranker.js +269 -269
- package/lib/runtime/adopt.js +176 -176
- package/lib/runtime/closure.js +172 -172
- package/lib/runtime/download.js +247 -247
- package/lib/runtime/layout.js +265 -265
- package/lib/runtime/loader.js +252 -252
- package/lib/runtime/model-fetch.js +361 -361
- package/lib/runtime/provision.js +237 -237
- package/lib/runtime/tarball.js +130 -130
- package/lib/runtime/verify.js +223 -204
- package/lib/scope.js +146 -146
- package/lib/search/adaptive.js +60 -60
- package/lib/search/bm25.js +96 -96
- package/lib/semantic.js +229 -0
- package/lib/sensitive-scan.js +209 -209
- package/lib/serve.js +215 -0
- package/lib/service.js +2663 -2596
- package/lib/settings.js +517 -509
- package/lib/store.js +3007 -3007
- package/lib/summarize.js +891 -856
- package/lib/tools.js +743 -737
- package/lib/vector-index.js +110 -110
- package/lib/version-check.js +122 -122
- package/lib/write-admission.js +402 -402
- package/locale/en.json +6 -6
- package/locale/zh.json +6 -6
- package/package.json +8 -4
- package/runtime-manifest.json +526 -526
- package/scripts/benchmark-embed.js +201 -201
- package/scripts/benchmark-recall.js +249 -249
- package/scripts/benchmark-rerank.js +166 -166
- package/scripts/build-runtime-manifest.mjs +224 -224
- package/scripts/changelog-prep.mjs +36 -0
- package/scripts/check-sync.js +42 -42
- package/scripts/e2e-dsh.js +220 -220
- package/scripts/mneme-runtime.mjs +277 -277
- package/scripts/stress-dsh.js +255 -255
- package/scripts/sync-lib.js +47 -47
- package/scripts/sync-test-badge.mjs +69 -69
- package/scripts/test-count-sync.mjs +44 -44
- package/src/api-standalone.js +673 -572
- package/src/api.js +1437 -1433
- package/src/bootstrap.js +211 -211
- package/src/commands.js +95 -95
- package/src/config.js +880 -836
- package/src/content-hash.js +54 -54
- package/src/continuity.js +220 -220
- package/src/document-index.js +94 -94
- package/src/document.js +303 -303
- package/src/dream/clustering.js +118 -118
- package/src/dream/decisions.js +674 -674
- package/src/dream/narratives.js +44 -44
- package/src/dream/sleep.js +819 -819
- package/src/dream.js +1501 -1501
- package/src/embedding.js +177 -177
- package/src/entities/extractor.js +252 -252
- package/src/graph/anchoring.js +179 -179
- package/src/guide.js +74 -74
- package/src/heat.js +144 -144
- package/src/hot-memory.js +46 -46
- package/src/index.js +535 -671
- package/src/inject.js +464 -450
- package/src/lang.js +248 -229
- package/src/local-embedder.js +358 -358
- package/src/maintenance.js +142 -142
- package/src/mirror.js +330 -330
- package/src/organize.js +450 -450
- package/src/peak-hours.js +117 -117
- package/src/placement.js +13 -0
- package/src/pre-inject-gate.js +340 -0
- package/src/quality-filter.js +126 -126
- package/src/recall-stats.js +175 -175
- package/src/reranker.js +269 -269
- package/src/runtime/adopt.js +176 -176
- package/src/runtime/closure.js +172 -172
- package/src/runtime/download.js +247 -247
- package/src/runtime/layout.js +265 -265
- package/src/runtime/loader.js +252 -252
- package/src/runtime/model-fetch.js +361 -361
- package/src/runtime/provision.js +237 -237
- package/src/runtime/tarball.js +130 -130
- package/src/runtime/verify.js +223 -204
- package/src/scope.js +146 -146
- package/src/search/adaptive.js +60 -60
- package/src/search/bm25.js +96 -96
- package/src/semantic.js +229 -0
- package/src/sensitive-scan.js +209 -209
- package/src/serve.js +215 -0
- package/src/service.js +2663 -2596
- package/src/settings.js +517 -509
- package/src/store.js +3007 -3007
- package/src/summarize.js +891 -856
- package/src/tools.js +743 -737
- package/src/vector-index.js +110 -110
- package/src/version-check.js +122 -122
- package/src/write-admission.js +402 -402
- package/test/adaptive-inject-budget.test.js +85 -85
- package/test/api-routes-gaps.test.js +218 -218
- package/test/api.test.js +1702 -1688
- package/test/audit.test.js +448 -448
- package/test/baseline-persist.test.js +248 -248
- package/test/benchmark.test.js +94 -94
- package/test/bootstrap.test.js +220 -220
- package/test/changelog-prep.test.js +60 -0
- package/test/client.test.js +1230 -1169
- package/test/clustering.test.js +100 -100
- package/test/commands.test.js +106 -106
- package/test/config.test.js +66 -66
- package/test/conflict-freeze.test.js +300 -300
- package/test/content-hash.test.js +204 -204
- package/test/continuity.test.js +333 -333
- package/test/document-dir.test.js +233 -233
- package/test/document.test.js +671 -671
- package/test/dream-archive-guardrail.test.js +116 -116
- package/test/dream-candidate-hybrid.test.js +281 -281
- package/test/dream-coverage-degrade.test.js +86 -86
- package/test/dream-failure-backoff.test.js +173 -173
- package/test/dream-peak-hours.test.js +194 -194
- package/test/dream.test.js +1385 -1383
- package/test/embedding-ready.test.js +196 -196
- package/test/entities.test.js +627 -547
- package/test/entity-recall.test.js +179 -179
- package/test/epistemic.test.js +325 -325
- package/test/fnew-0112.test.js +311 -311
- package/test/fnew-03.test.js +422 -422
- package/test/graph-anchoring.test.js +184 -184
- package/test/graph-api.test.js +175 -175
- package/test/graph-inject-budget.test.js +103 -103
- package/test/graph-passive-confirm.test.js +91 -91
- package/test/graph-weight-evolution.test.js +104 -104
- package/test/heat.test.js +175 -175
- package/test/helpers/dream-mock.js +82 -82
- package/test/helpers/peer-worker.mjs +25 -25
- package/test/helpers/runtime-source.js +60 -60
- package/test/helpers/tar-builder.js +80 -80
- package/test/helpers/write-admission-samples.js +308 -308
- package/test/hot-memory.test.js +145 -145
- package/test/inject-content-cap.test.js +91 -91
- package/test/inject-first-round-bm25.test.js +89 -89
- package/test/inject-heat.test.js +77 -61
- package/test/inject-parent-gate.test.js +106 -106
- package/test/inject-pools.test.js +194 -194
- package/test/inject-recall.test.js +105 -105
- package/test/inject-rotation.test.js +161 -161
- package/test/inject.test.js +420 -420
- package/test/lang.test.js +87 -87
- package/test/lastrunat-persist.test.js +101 -101
- package/test/lib-smoke.test.js +96 -96
- package/test/llm-audit.test.js +598 -598
- package/test/llm-message-source.test.js +207 -207
- package/test/local-embedder.test.js +292 -292
- package/test/maintenance.test.js +391 -391
- package/test/mcp-stdio.test.js +385 -385
- package/test/mirror-dirty.test.js +424 -424
- package/test/mirror-edit-digest.test.js +187 -187
- package/test/mirror-generation.test.js +499 -499
- package/test/mirror-types.test.js +208 -208
- package/test/mirror.test.js +249 -249
- package/test/narrative-bars.test.js +162 -162
- package/test/organize.test.js +418 -418
- package/test/peer-blockers.test.js +193 -193
- package/test/placement.test.js +36 -0
- package/test/policy-epoch.test.js +259 -259
- package/test/pre-inject-gate.test.js +434 -0
- package/test/quality-filter.test.js +186 -186
- package/test/reasoning-effort.test.js +752 -752
- package/test/recall-anti-update.test.js +82 -82
- package/test/recall-evals.test.js +253 -253
- package/test/recall-layer.test.js +326 -326
- package/test/recall-stats.test.js +189 -189
- package/test/receipt-chain.test.js +451 -451
- package/test/reflection.test.js +226 -226
- package/test/reindex-backfill.test.js +62 -62
- package/test/reranker-single-logit.test.js +73 -73
- package/test/reranker.test.js +197 -197
- package/test/resident-status.test.js +83 -83
- package/test/runtime-adopt.test.js +206 -206
- package/test/runtime-cli.test.js +89 -89
- package/test/runtime-closure.test.js +193 -193
- package/test/runtime-download.test.js +302 -302
- package/test/runtime-layout.test.js +163 -163
- package/test/runtime-loader.test.js +255 -255
- package/test/runtime-manifest.test.js +159 -159
- package/test/runtime-model-fetch.test.js +348 -348
- package/test/runtime-provision.test.js +101 -101
- package/test/runtime-tarball.test.js +81 -81
- package/test/runtime-verify.test.js +319 -279
- package/test/scope-candidates.test.js +239 -239
- package/test/scope-explicit.test.js +452 -452
- package/test/scope-inject-soft.test.js +106 -106
- package/test/scope-retrieval.test.js +197 -197
- package/test/scope-storage.test.js +255 -255
- package/test/scope-strict.test.js +367 -343
- package/test/search-fusion.test.js +90 -90
- package/test/semantic.test.js +124 -124
- package/test/sensitive-scan.test.js +207 -207
- package/test/serve-bin.test.js +121 -0
- package/test/serve.test.js +196 -0
- package/test/service-search.test.js +174 -174
- package/test/service.test.js +405 -405
- package/test/settings.test.js +261 -261
- package/test/skip-invalid-restore.test.js +262 -262
- package/test/sleep-action-set.test.js +422 -422
- package/test/sleep-heat.test.js +126 -126
- package/test/sleep-scheduler-rearm.test.js +89 -89
- package/test/sleep.test.js +527 -527
- package/test/standalone-api.test.js +751 -544
- package/test/store-vector-cache.test.js +98 -98
- package/test/store.test.js +396 -396
- package/test/stress.test.js +209 -209
- package/test/summarize-occurred-at.test.js +127 -0
- package/test/summarize-reasoning-effort.test.js +189 -189
- package/test/summarize.test.js +1465 -1465
- package/test/supersede-state-tracking.test.js +394 -0
- package/test/test-count-sync.test.js +60 -60
- package/test/tools-dto-schema-matrix.test.js +325 -325
- package/test/tools-memory-get-schema.test.js +65 -65
- package/test/tools.test.js +428 -374
- package/test/updated-at-semantics.test.js +113 -113
- package/test/vector-index.test.js +221 -221
- package/test/version-check.test.js +152 -152
- package/test/webServer-optional.test.js +82 -82
- package/test/write-admission.test.js +668 -668
package/src/runtime/verify.js
CHANGED
|
@@ -1,204 +1,223 @@
|
|
|
1
|
-
// 运行时验证三件套(issue #131 / PR-A)
|
|
2
|
-
//
|
|
3
|
-
// 为什么要分三层,而不是「能 import 就算通过」:
|
|
4
|
-
//
|
|
5
|
-
// 结构 —— 入口在不在、版本对不对、平台子树对不对、闭包有没有缺口。
|
|
6
|
-
// 便宜、确定,能立刻发现「解压到一半」。
|
|
7
|
-
// 功能 —— 真的 import 入口并跑一次推理。这是唯一能证明「这份运行时在这个
|
|
8
|
-
// 环境里真的算得出来」的一层;结构全对但原生二进制与本机 ABI 不
|
|
9
|
-
// 匹配时,只有它能发现。
|
|
10
|
-
// 完整性 —— 只对「有原始产物可比对」的来源做(下载的 tarball、本地 tarball)。
|
|
11
|
-
// 收编来的目录没有原始 tarball,如实标 unverified,不假装验过。
|
|
12
|
-
//
|
|
13
|
-
// 功能验证复用 src/local-embedder.js 的 engineFactory 思路:真正干活的是注入进来
|
|
14
|
-
// 的 engine,默认实现才去 import 真实运行时。这样测试不需要网络、不需要模型,
|
|
15
|
-
// 也能把「返回了非归一化向量」「只返回一行」这类问题钉住。
|
|
16
|
-
//
|
|
17
|
-
// 默认 engine 会强制 env.allowRemoteModels = false:验证过程绝不能偷偷下载模型,
|
|
18
|
-
// 否则「验证」本身就成了一个新的网络依赖点。
|
|
19
|
-
//
|
|
20
|
-
// @module dsh-mneme/runtime/verify
|
|
21
|
-
import { pathToFileURL } from "node:url";
|
|
22
|
-
import { join } from "node:path";
|
|
23
|
-
import { TRANSFORMERS_ENTRY, describePayload, recordedIntegrity } from "./layout.js";
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* 能证明「与清单里的 sha512 一致」的状态词。下载通道(`download.js`)记的是 `"verified"`,
|
|
27
|
-
* 本模块的强校验路径记的是 `"sha512-matched"` —— 同一件事的两种记法,判据必须都认,
|
|
28
|
-
* 否则清单里那份结论喂进来就是系统性判失败(issue #268)。
|
|
29
|
-
*/
|
|
30
|
-
const MATCHED_INTEGRITY = new Set(["sha512-matched", "verified"]);
|
|
31
|
-
/** 没有可比对原始产物时的说明文案(收编路径的默认值)。 */
|
|
32
|
-
const UNVERIFIED_DETAIL = "该来源没有可比对的原始产物(收编);如需强校验请用 --strict 走 tarball 通道";
|
|
33
|
-
|
|
34
|
-
/** 默认探针文本:中英各一条,避免只覆盖单字节语种。 */
|
|
35
|
-
export const DEFAULT_PROBE_TEXTS = ["猫咪喜欢晒太阳", "the quick brown fox"];
|
|
36
|
-
/** 默认模型,与 src/config.js 的 localEmbedModel 默认值一致。 */
|
|
37
|
-
export const DEFAULT_PROBE_MODEL = "Xenova/bge-small-zh-v1.5";
|
|
38
|
-
/** L2 归一化的容忍度(我们显式请求了 normalize: true)。 */
|
|
39
|
-
const NORM_TOLERANCE = 0.01;
|
|
40
|
-
/** 判定「退化向量」的余弦上限:两条毫不相干的文本不该几乎同向。 */
|
|
41
|
-
const DEGENERATE_COS = 0.999;
|
|
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
|
-
|
|
1
|
+
// 运行时验证三件套(issue #131 / PR-A)
|
|
2
|
+
//
|
|
3
|
+
// 为什么要分三层,而不是「能 import 就算通过」:
|
|
4
|
+
//
|
|
5
|
+
// 结构 —— 入口在不在、版本对不对、平台子树对不对、闭包有没有缺口。
|
|
6
|
+
// 便宜、确定,能立刻发现「解压到一半」。
|
|
7
|
+
// 功能 —— 真的 import 入口并跑一次推理。这是唯一能证明「这份运行时在这个
|
|
8
|
+
// 环境里真的算得出来」的一层;结构全对但原生二进制与本机 ABI 不
|
|
9
|
+
// 匹配时,只有它能发现。
|
|
10
|
+
// 完整性 —— 只对「有原始产物可比对」的来源做(下载的 tarball、本地 tarball)。
|
|
11
|
+
// 收编来的目录没有原始 tarball,如实标 unverified,不假装验过。
|
|
12
|
+
//
|
|
13
|
+
// 功能验证复用 src/local-embedder.js 的 engineFactory 思路:真正干活的是注入进来
|
|
14
|
+
// 的 engine,默认实现才去 import 真实运行时。这样测试不需要网络、不需要模型,
|
|
15
|
+
// 也能把「返回了非归一化向量」「只返回一行」这类问题钉住。
|
|
16
|
+
//
|
|
17
|
+
// 默认 engine 会强制 env.allowRemoteModels = false:验证过程绝不能偷偷下载模型,
|
|
18
|
+
// 否则「验证」本身就成了一个新的网络依赖点。
|
|
19
|
+
//
|
|
20
|
+
// @module dsh-mneme/runtime/verify
|
|
21
|
+
import { pathToFileURL } from "node:url";
|
|
22
|
+
import { join } from "node:path";
|
|
23
|
+
import { TRANSFORMERS_ENTRY, defaultModelCacheDir, describePayload, recordedIntegrity } from "./layout.js";
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 能证明「与清单里的 sha512 一致」的状态词。下载通道(`download.js`)记的是 `"verified"`,
|
|
27
|
+
* 本模块的强校验路径记的是 `"sha512-matched"` —— 同一件事的两种记法,判据必须都认,
|
|
28
|
+
* 否则清单里那份结论喂进来就是系统性判失败(issue #268)。
|
|
29
|
+
*/
|
|
30
|
+
const MATCHED_INTEGRITY = new Set(["sha512-matched", "verified"]);
|
|
31
|
+
/** 没有可比对原始产物时的说明文案(收编路径的默认值)。 */
|
|
32
|
+
const UNVERIFIED_DETAIL = "该来源没有可比对的原始产物(收编);如需强校验请用 --strict 走 tarball 通道";
|
|
33
|
+
|
|
34
|
+
/** 默认探针文本:中英各一条,避免只覆盖单字节语种。 */
|
|
35
|
+
export const DEFAULT_PROBE_TEXTS = ["猫咪喜欢晒太阳", "the quick brown fox"];
|
|
36
|
+
/** 默认模型,与 src/config.js 的 localEmbedModel 默认值一致。 */
|
|
37
|
+
export const DEFAULT_PROBE_MODEL = "Xenova/bge-small-zh-v1.5";
|
|
38
|
+
/** L2 归一化的容忍度(我们显式请求了 normalize: true)。 */
|
|
39
|
+
const NORM_TOLERANCE = 0.01;
|
|
40
|
+
/** 判定「退化向量」的余弦上限:两条毫不相干的文本不该几乎同向。 */
|
|
41
|
+
const DEGENERATE_COS = 0.999;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 把模型缓存目录解析到「真的有一份模型」的地方:空值落回用户级默认目录。
|
|
45
|
+
*
|
|
46
|
+
* `embedModelCacheDir` 这条配置的约定是「空 = ~/.dsh/mneme/models」(README、CONFIGURATION.md、
|
|
47
|
+
* LOCAL_MODEL.md 都这么写),local-embedder 与 reranker 也各自按 `String(x ?? "").trim() || 默认`
|
|
48
|
+
* 解析。验证模块一度把这个值原样交给 transformers.js —— 而 transformers.js 收到 `undefined`
|
|
49
|
+
* 等于「不设置 env.cacheDir」,于是它转去找包内相对路径:真实嵌入用的是用户目录那份模型,面板
|
|
50
|
+
* verify 却去包内找,报出 `functional: false` 的假失败(issue #387)。解析收在默认 engine 这层
|
|
51
|
+
* 而不是调用方,是为了让「验证与真实推理用同一份模型」成为模块自身的不变量:任何调用点都
|
|
52
|
+
* 不可能再因为传了空串而验错地方。
|
|
53
|
+
* @param {string} [cacheDir] - 配置或调用方给的缓存目录,允许空值。
|
|
54
|
+
* @returns {string} 可用的缓存目录(绝对路径)。
|
|
55
|
+
*/
|
|
56
|
+
export function resolveModelCacheDir(cacheDir) {
|
|
57
|
+
return String(cacheDir ?? "").trim() || defaultModelCacheDir();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* 默认 engine:真的去 import 运行时并建一个 feature-extraction 管道。
|
|
62
|
+
*
|
|
63
|
+
* 返回一个 `embed(texts) => number[][]`。之所以不返回 transformers 的 tensor,
|
|
64
|
+
* 是为了把「Tensor 形状怎么读」这种细节留在实现里,验证逻辑只看数字。
|
|
65
|
+
* @param {string} entryUrl - 入口文件的 file URL。
|
|
66
|
+
* @param {{cacheDir?: string, model?: string, dtype?: string, device?: string}} opts - 选项。
|
|
67
|
+
* `cacheDir` 允许空值(落回用户级默认目录,见 {@link resolveModelCacheDir})。
|
|
68
|
+
* @returns {Promise<(texts: string[]) => Promise<number[][]>>} 嵌入函数。
|
|
69
|
+
*/
|
|
70
|
+
export async function defaultEngine(entryUrl, { cacheDir, model = DEFAULT_PROBE_MODEL, dtype = "q8", device = "cpu" } = {}) {
|
|
71
|
+
const mod = await import(entryUrl);
|
|
72
|
+
const { env, pipeline } = mod;
|
|
73
|
+
// 空值必须在这里就补上默认目录:交给 transformers.js 自己决定,它会去找包内相对路径,
|
|
74
|
+
// 验证就变成「拿另一份模型下结论」(issue #387)。
|
|
75
|
+
const resolvedCacheDir = resolveModelCacheDir(cacheDir);
|
|
76
|
+
try {
|
|
77
|
+
env.cacheDir = resolvedCacheDir;
|
|
78
|
+
} catch {
|
|
79
|
+
/* 老版本可能没有这个字段:忽略,交给下面 pipeline 自己找缓存 */
|
|
80
|
+
}
|
|
81
|
+
// 验证不允许触网:缓存命中就本地加载,命中不了就明确失败。
|
|
82
|
+
try {
|
|
83
|
+
env.allowRemoteModels = false;
|
|
84
|
+
} catch {
|
|
85
|
+
/* 同上 */
|
|
86
|
+
}
|
|
87
|
+
const extractor = await pipeline("feature-extraction", model, { dtype, device, cache_dir: resolvedCacheDir });
|
|
88
|
+
return async (texts) => {
|
|
89
|
+
const tensor = await extractor(texts, { pooling: "mean", normalize: true });
|
|
90
|
+
const dims = Array.from(tensor.dims ?? []);
|
|
91
|
+
const width = dims[dims.length - 1] || 0;
|
|
92
|
+
// 宽度为 0 时下面的 `i += width` 会原地打转 —— 那是**死循环**,会把调用方(乃至 DSH 主进程)
|
|
93
|
+
// 挂住。宁可明确失败:调用方按契约把失败变成 {ok:false, reason},而不是永远等不到结果。
|
|
94
|
+
if (!Number.isInteger(width) || width <= 0) {
|
|
95
|
+
throw new Error(`推理输出的维度不可用:dims=${JSON.stringify(dims)}`);
|
|
96
|
+
}
|
|
97
|
+
const rows = [];
|
|
98
|
+
for (let i = 0; i < tensor.data.length; i += width) {
|
|
99
|
+
rows.push(Array.from(tensor.data.subarray(i, i + width)));
|
|
100
|
+
}
|
|
101
|
+
return rows;
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** 向量模长。 */
|
|
106
|
+
function norm(row) {
|
|
107
|
+
let sum = 0;
|
|
108
|
+
for (const value of row) sum += value * value;
|
|
109
|
+
return Math.sqrt(sum);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** 两行向量的余弦。 */
|
|
113
|
+
function cosine(a, b) {
|
|
114
|
+
let dot = 0;
|
|
115
|
+
for (let i = 0; i < a.length; i++) dot += a[i] * b[i];
|
|
116
|
+
return dot;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* 跑功能验证:真的 import 并嵌入探针文本,然后检查结果的形状与数值性质。
|
|
121
|
+
*
|
|
122
|
+
* 任何失败都返回结果对象,不抛异常——验证本身必须是 fail-safe 的,
|
|
123
|
+
* 否则一条坏运行时会把调用方(面板、状态接口)也带崩。
|
|
124
|
+
* @param {string} dir - payload 目录。
|
|
125
|
+
* @param {object} [opts] - 选项。
|
|
126
|
+
* @returns {Promise<{ok: boolean, reason: string|null, dim: number|null, rows: number, elapsedMs: number}>}
|
|
127
|
+
*/
|
|
128
|
+
export async function verifyFunctional(dir, {
|
|
129
|
+
engine = defaultEngine,
|
|
130
|
+
probeTexts = DEFAULT_PROBE_TEXTS,
|
|
131
|
+
cacheDir,
|
|
132
|
+
model,
|
|
133
|
+
dtype,
|
|
134
|
+
device
|
|
135
|
+
} = {}) {
|
|
136
|
+
const startedAt = Date.now();
|
|
137
|
+
const fail = (reason) => ({ ok: false, reason, dim: null, rows: 0, elapsedMs: Date.now() - startedAt });
|
|
138
|
+
|
|
139
|
+
const entry = join(dir, TRANSFORMERS_ENTRY);
|
|
140
|
+
let embed;
|
|
141
|
+
try {
|
|
142
|
+
embed = await engine(pathToFileURL(entry).href, { cacheDir, model, dtype, device });
|
|
143
|
+
} catch (error) {
|
|
144
|
+
return fail(`加载运行时失败:${error?.message ?? error}`);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
let rows;
|
|
148
|
+
try {
|
|
149
|
+
rows = await embed(probeTexts);
|
|
150
|
+
} catch (error) {
|
|
151
|
+
return fail(`推理失败:${error?.message ?? error}`);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (!Array.isArray(rows) || rows.length !== probeTexts.length) {
|
|
155
|
+
return fail(`返回行数不符:期望 ${probeTexts.length},实际 ${Array.isArray(rows) ? rows.length : typeof rows}`);
|
|
156
|
+
}
|
|
157
|
+
// 先逐行确认「是数组」且「全是有限数」。这一步不能省:non-finite(NaN/Infinity)会让
|
|
158
|
+
// 下面所有比较恒为 false —— `Math.abs(NaN - 1) > tol` 是 false,`Math.abs(NaN) > 阈值`
|
|
159
|
+
// 也是 false —— 于是一份坏掉的运行时会被判成「验证通过」。这是本模块最危险的失败模式
|
|
160
|
+
// (假通过),比抛异常糟得多。行不是数组时同样要先拦住,否则 .length 直接抛,
|
|
161
|
+
// 违背「任何失败都返回结果对象,不抛异常」的契约。
|
|
162
|
+
for (let i = 0; i < rows.length; i++) {
|
|
163
|
+
if (!Array.isArray(rows[i])) return fail(`第 ${i} 行不是数组(${typeof rows[i]})`);
|
|
164
|
+
if (rows[i].some((v) => !Number.isFinite(v))) return fail(`第 ${i} 行含有非有限数值`);
|
|
165
|
+
}
|
|
166
|
+
const dim = rows[0].length;
|
|
167
|
+
if (dim === 0) return fail("返回了空向量");
|
|
168
|
+
if (rows.some((row) => row.length !== dim)) return fail("各行维度不一致");
|
|
169
|
+
|
|
170
|
+
for (let i = 0; i < rows.length; i++) {
|
|
171
|
+
const length = norm(rows[i]);
|
|
172
|
+
if (Math.abs(length - 1) > NORM_TOLERANCE) {
|
|
173
|
+
return fail(`第 ${i} 行不是 L2 归一化向量(模长 ${length.toFixed(4)})`);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
if (rows.length >= 2 && Math.abs(cosine(rows[0], rows[1])) > DEGENERATE_COS) {
|
|
177
|
+
return fail(`嵌入退化:两条不相干文本的余弦为 ${cosine(rows[0], rows[1]).toFixed(4)}`);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
return { ok: true, reason: null, dim, rows: rows.length, elapsedMs: Date.now() - startedAt };
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* 三层验证,任何一层失败都如实记录;结构不过就不再做功能验证(省掉一次无谓的加载)。
|
|
185
|
+
* @param {string} dir - payload 目录。
|
|
186
|
+
* @param {object} [opts] - 选项。
|
|
187
|
+
* @param {{status: string, detail?: string}} [opts.integrity] - 调用方算好的完整性结论
|
|
188
|
+
* (下载/本地 tarball 路径在解包前比对过 sha512,把结果传进来)。省略时读 payload
|
|
189
|
+
* 清单里记下的那份(下载通道会把结论写进去);两边都没有才如实标 unverified。
|
|
190
|
+
* @returns {Promise<object>} 三件套结果 + 总判定。
|
|
191
|
+
*/
|
|
192
|
+
export async function verifyPayload(dir, {
|
|
193
|
+
platform = process.platform,
|
|
194
|
+
engine,
|
|
195
|
+
probeTexts,
|
|
196
|
+
cacheDir,
|
|
197
|
+
model,
|
|
198
|
+
dtype,
|
|
199
|
+
device,
|
|
200
|
+
integrity
|
|
201
|
+
} = {}) {
|
|
202
|
+
const structural = describePayload(dir, { platform });
|
|
203
|
+
// 调用方没显式给结论时,读清单里记下的那份:下载档在落盘前逐个 tarball 比对过 sha512,
|
|
204
|
+
// 结论就落在 <dir>/mneme-runtime.json 里。不读它,这一层在生产路径上永远不生效(issue #268)。
|
|
205
|
+
const claimed = recordedIntegrity(integrity ?? structural.manifest?.integrity);
|
|
206
|
+
const integrityResult =
|
|
207
|
+
claimed.status === "unverified"
|
|
208
|
+
? { ok: true, status: "unverified", detail: claimed.detail ?? UNVERIFIED_DETAIL }
|
|
209
|
+
: { ok: MATCHED_INTEGRITY.has(claimed.status), status: claimed.status, detail: claimed.detail };
|
|
210
|
+
|
|
211
|
+
const functional = structural.ok
|
|
212
|
+
? await verifyFunctional(dir, { engine, probeTexts, cacheDir, model, dtype, device })
|
|
213
|
+
: { ok: false, reason: "结构检查未通过,跳过功能验证", dim: null, rows: 0, elapsedMs: 0 };
|
|
214
|
+
|
|
215
|
+
return {
|
|
216
|
+
dir,
|
|
217
|
+
version: structural.version,
|
|
218
|
+
ok: structural.ok && functional.ok && integrityResult.ok,
|
|
219
|
+
structural,
|
|
220
|
+
functional,
|
|
221
|
+
integrity: integrityResult
|
|
222
|
+
};
|
|
223
|
+
}
|