@modusensus/dsh-mneme 0.8.3 → 0.8.5

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.
Files changed (205) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +727 -636
  3. package/bin/cli.mjs +0 -0
  4. package/bin/dsh-mneme-mcp.mjs +510 -0
  5. package/cordis.patch.yml +21 -21
  6. package/lib/api-standalone.js +506 -316
  7. package/lib/api.js +1403 -1316
  8. package/lib/bootstrap.js +211 -0
  9. package/lib/client.js +4657 -4366
  10. package/lib/commands.js +95 -95
  11. package/lib/config.js +615 -510
  12. package/lib/document.js +221 -0
  13. package/lib/dream/clustering.js +118 -118
  14. package/lib/dream/decisions.js +658 -658
  15. package/lib/dream/narratives.js +44 -0
  16. package/lib/dream/sleep.js +702 -700
  17. package/lib/dream.js +147 -16
  18. package/lib/embedding.js +177 -177
  19. package/lib/entities/extractor.js +249 -249
  20. package/lib/guide.js +44 -0
  21. package/lib/heat.js +144 -136
  22. package/lib/hot-memory.js +46 -46
  23. package/lib/index.js +515 -515
  24. package/lib/inject.js +375 -295
  25. package/lib/lang.js +208 -179
  26. package/lib/local-embedder.js +320 -320
  27. package/lib/mirror.js +198 -198
  28. package/lib/organize.js +450 -0
  29. package/lib/quality-filter.js +126 -126
  30. package/lib/recall-stats.js +125 -0
  31. package/lib/reranker.js +269 -269
  32. package/lib/runtime/adopt.js +176 -176
  33. package/lib/runtime/closure.js +172 -172
  34. package/lib/runtime/download.js +247 -247
  35. package/lib/runtime/layout.js +265 -235
  36. package/lib/runtime/loader.js +252 -249
  37. package/lib/runtime/model-fetch.js +361 -361
  38. package/lib/runtime/provision.js +237 -237
  39. package/lib/runtime/tarball.js +130 -130
  40. package/lib/runtime/verify.js +204 -191
  41. package/lib/scope.js +118 -118
  42. package/lib/search/adaptive.js +60 -22
  43. package/lib/search/bm25.js +96 -96
  44. package/lib/service.js +2324 -2087
  45. package/lib/settings.js +459 -420
  46. package/lib/store.js +2549 -2367
  47. package/lib/summarize.js +820 -449
  48. package/lib/tools.js +702 -574
  49. package/lib/vector-index.js +110 -110
  50. package/lib/version-check.js +122 -122
  51. package/package.json +109 -108
  52. package/runtime-manifest.json +526 -596
  53. package/scripts/benchmark-embed.js +201 -201
  54. package/scripts/benchmark-recall.js +199 -199
  55. package/scripts/benchmark-rerank.js +166 -166
  56. package/scripts/build-runtime-manifest.mjs +224 -217
  57. package/scripts/check-sync.js +42 -42
  58. package/scripts/e2e-dsh.js +220 -220
  59. package/scripts/mneme-runtime.mjs +277 -277
  60. package/scripts/stress-dsh.js +255 -255
  61. package/scripts/sync-lib.js +47 -47
  62. package/scripts/sync-test-badge.mjs +67 -61
  63. package/src/api-standalone.js +506 -316
  64. package/src/api.js +1403 -1316
  65. package/src/bootstrap.js +211 -0
  66. package/src/commands.js +95 -95
  67. package/src/config.js +615 -510
  68. package/src/document.js +221 -0
  69. package/src/dream/clustering.js +118 -118
  70. package/src/dream/decisions.js +658 -658
  71. package/src/dream/narratives.js +44 -0
  72. package/src/dream/sleep.js +702 -700
  73. package/src/dream.js +147 -16
  74. package/src/embedding.js +177 -177
  75. package/src/entities/extractor.js +249 -249
  76. package/src/guide.js +44 -0
  77. package/src/heat.js +144 -136
  78. package/src/hot-memory.js +46 -46
  79. package/src/index.js +515 -515
  80. package/src/inject.js +375 -295
  81. package/src/lang.js +208 -179
  82. package/src/local-embedder.js +320 -320
  83. package/src/mirror.js +198 -198
  84. package/src/organize.js +450 -0
  85. package/src/quality-filter.js +126 -126
  86. package/src/recall-stats.js +125 -0
  87. package/src/reranker.js +269 -269
  88. package/src/runtime/adopt.js +176 -176
  89. package/src/runtime/closure.js +172 -172
  90. package/src/runtime/download.js +247 -247
  91. package/src/runtime/layout.js +265 -235
  92. package/src/runtime/loader.js +252 -249
  93. package/src/runtime/model-fetch.js +361 -361
  94. package/src/runtime/provision.js +237 -237
  95. package/src/runtime/tarball.js +130 -130
  96. package/src/runtime/verify.js +204 -191
  97. package/src/scope.js +118 -118
  98. package/src/search/adaptive.js +60 -22
  99. package/src/search/bm25.js +96 -96
  100. package/src/service.js +2324 -2087
  101. package/src/settings.js +459 -420
  102. package/src/store.js +2549 -2367
  103. package/src/summarize.js +820 -449
  104. package/src/tools.js +702 -574
  105. package/src/vector-index.js +110 -110
  106. package/src/version-check.js +122 -122
  107. package/test/adaptive-inject-budget.test.js +85 -0
  108. package/test/api-routes-gaps.test.js +218 -218
  109. package/test/api.test.js +1640 -1482
  110. package/test/audit.test.js +448 -448
  111. package/test/benchmark.test.js +94 -94
  112. package/test/bootstrap.test.js +220 -0
  113. package/test/client.test.js +860 -736
  114. package/test/clustering.test.js +100 -100
  115. package/test/commands.test.js +106 -106
  116. package/test/config.test.js +66 -66
  117. package/test/conflict-freeze.test.js +300 -300
  118. package/test/document.test.js +523 -0
  119. package/test/dream-archive-guardrail.test.js +116 -116
  120. package/test/dream-candidate-hybrid.test.js +281 -281
  121. package/test/dream-coverage-degrade.test.js +86 -86
  122. package/test/dream.test.js +1308 -1254
  123. package/test/embedding-ready.test.js +196 -196
  124. package/test/entities.test.js +547 -547
  125. package/test/entity-recall.test.js +179 -0
  126. package/test/epistemic.test.js +298 -298
  127. package/test/fnew-0112.test.js +311 -311
  128. package/test/fnew-03.test.js +422 -422
  129. package/test/graph-api.test.js +175 -175
  130. package/test/heat.test.js +175 -148
  131. package/test/helpers/dream-mock.js +82 -82
  132. package/test/helpers/peer-worker.mjs +25 -25
  133. package/test/helpers/runtime-source.js +60 -60
  134. package/test/helpers/tar-builder.js +80 -80
  135. package/test/hot-memory.test.js +145 -145
  136. package/test/inject-content-cap.test.js +91 -0
  137. package/test/inject-first-round-bm25.test.js +89 -89
  138. package/test/inject-heat.test.js +61 -0
  139. package/test/inject-pools.test.js +170 -0
  140. package/test/inject-recall.test.js +105 -0
  141. package/test/inject-rotation.test.js +161 -161
  142. package/test/inject.test.js +283 -249
  143. package/test/lang.test.js +87 -87
  144. package/test/lib-smoke.test.js +96 -96
  145. package/test/llm-audit.test.js +462 -309
  146. package/test/llm-message-source.test.js +177 -177
  147. package/test/local-embedder.test.js +249 -249
  148. package/test/mcp-stdio.test.js +385 -0
  149. package/test/mirror-dirty.test.js +424 -424
  150. package/test/mirror-edit-digest.test.js +187 -187
  151. package/test/mirror-generation.test.js +499 -499
  152. package/test/mirror.test.js +249 -249
  153. package/test/narrative-bars.test.js +162 -0
  154. package/test/organize.test.js +418 -0
  155. package/test/peer-blockers.test.js +193 -193
  156. package/test/policy-epoch.test.js +259 -259
  157. package/test/quality-filter.test.js +186 -186
  158. package/test/reasoning-effort.test.js +746 -746
  159. package/test/recall-evals.test.js +235 -235
  160. package/test/recall-layer.test.js +326 -326
  161. package/test/recall-stats.test.js +123 -0
  162. package/test/receipt-chain.test.js +451 -451
  163. package/test/reflection.test.js +226 -226
  164. package/test/reindex-backfill.test.js +62 -62
  165. package/test/reranker-single-logit.test.js +73 -73
  166. package/test/reranker.test.js +197 -197
  167. package/test/resident-status.test.js +83 -0
  168. package/test/runtime-adopt.test.js +206 -206
  169. package/test/runtime-cli.test.js +89 -89
  170. package/test/runtime-closure.test.js +193 -193
  171. package/test/runtime-download.test.js +302 -302
  172. package/test/runtime-layout.test.js +163 -163
  173. package/test/runtime-loader.test.js +255 -237
  174. package/test/runtime-manifest.test.js +159 -158
  175. package/test/runtime-model-fetch.test.js +348 -348
  176. package/test/runtime-provision.test.js +101 -101
  177. package/test/runtime-tarball.test.js +81 -81
  178. package/test/runtime-verify.test.js +279 -214
  179. package/test/scope-candidates.test.js +239 -239
  180. package/test/scope-explicit.test.js +452 -452
  181. package/test/scope-retrieval.test.js +197 -197
  182. package/test/scope-storage.test.js +255 -255
  183. package/test/scope-strict.test.js +231 -231
  184. package/test/search-fusion.test.js +90 -90
  185. package/test/semantic.test.js +124 -124
  186. package/test/service-search.test.js +174 -174
  187. package/test/service.test.js +405 -405
  188. package/test/settings.test.js +261 -261
  189. package/test/skip-invalid-restore.test.js +262 -262
  190. package/test/sleep-action-set.test.js +422 -422
  191. package/test/sleep-heat.test.js +126 -125
  192. package/test/sleep-scheduler-rearm.test.js +89 -89
  193. package/test/sleep.test.js +410 -373
  194. package/test/standalone-api.test.js +544 -359
  195. package/test/store-vector-cache.test.js +98 -98
  196. package/test/store.test.js +396 -383
  197. package/test/stress.test.js +209 -209
  198. package/test/summarize.test.js +1288 -575
  199. package/test/tools-dto-schema-matrix.test.js +325 -0
  200. package/test/tools-memory-get-schema.test.js +65 -65
  201. package/test/tools.test.js +374 -341
  202. package/test/updated-at-semantics.test.js +113 -113
  203. package/test/vector-index.test.js +221 -221
  204. package/test/version-check.test.js +152 -152
  205. package/test/webServer-optional.test.js +82 -82
@@ -1,214 +1,279 @@
1
- import test from "node:test";
2
- import assert from "node:assert/strict";
3
- import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
4
- import { tmpdir } from "node:os";
5
- import { join } from "node:path";
6
- import { DEFAULT_PROBE_TEXTS, defaultEngine, verifyFunctional, verifyPayload } from "../src/runtime/verify.js";
7
- import { TRANSFORMERS_ENTRY } from "../src/runtime/layout.js";
8
- import { pathToFileURL } from "node:url";
9
-
10
- // 验证三件套(issue #131 / PR-A)。重点不是「能跑通」,而是「坏的那几种能被抓住」:
11
- // 结构对但原生二进制与本机不匹配、返回非归一化向量、向量退化、加载直接抛——
12
- // 这些都必须变成一条可读的失败结论,而不是让调用方拿到一个假的 ok。
13
-
14
- /** 造一个结构上合法的 payload(只有形状,没有真东西)。 */
15
- function makePayload({ platform = "win32", arch = "x64", withEntry = true } = {}) {
16
- const dir = mkdtempSync(join(tmpdir(), "mneme-verify-"));
17
- const write = (rel, body) => {
18
- const full = join(dir, rel);
19
- mkdirSync(join(full, ".."), { recursive: true });
20
- writeFileSync(full, body);
21
- };
22
- for (const name of ["@huggingface/transformers", "onnxruntime-node", "sharp"]) {
23
- write(join("node_modules", name, "package.json"), JSON.stringify({ name, version: name === "@huggingface/transformers" ? "4.2.0" : "1.0.0" }));
24
- }
25
- if (withEntry) write(TRANSFORMERS_ENTRY, "export const x = 1;\n");
26
- mkdirSync(join(dir, "node_modules", "onnxruntime-node", "bin", "napi-v6", platform, arch), { recursive: true });
27
- return dir;
28
- }
29
-
30
- /** 归一化的假向量:`rows` 行,每行 dim 维,方向由 seed 决定。 */
31
- function fakeRows(rows, dim, seed = 0) {
32
- return Array.from({ length: rows }, (_, r) => {
33
- const v = Array.from({ length: dim }, (_, i) => Math.sin((i + 1) * (r + 1 + seed)));
34
- const length = Math.hypot(...v);
35
- return v.map((x) => x / length);
36
- });
37
- }
38
-
39
- /** 注入一个「什么都能返回」的 engine。 */
40
- function engineReturning(value) {
41
- return async () => async () => value;
42
- }
43
-
44
- test("功能验证:正常的向量通过,并报出维度与耗时", async () => {
45
- const result = await verifyFunctional("/whatever", { engine: engineReturning(fakeRows(2, 512)) });
46
- assert.equal(result.ok, true, result.reason);
47
- assert.equal(result.dim, 512);
48
- assert.equal(result.rows, 2);
49
- assert.ok(result.elapsedMs >= 0);
50
- });
51
-
52
- test("功能验证:行数不符要报出来(而不是只取第一行当作通过)", async () => {
53
- const result = await verifyFunctional("/whatever", { engine: engineReturning(fakeRows(1, 512)) });
54
- assert.equal(result.ok, false);
55
- assert.match(result.reason, /返回行数不符/);
56
- });
57
-
58
- test("功能验证:非归一化向量要报出来(说明 normalize 没生效)", async () => {
59
- const rows = fakeRows(2, 8).map((row) => row.map((x) => x * 3));
60
- const result = await verifyFunctional("/whatever", { engine: engineReturning(rows) });
61
- assert.equal(result.ok, false);
62
- assert.match(result.reason, /不是 L2 归一化/);
63
- });
64
-
65
- test("功能验证:向量退化成同一方向要报出来", async () => {
66
- const one = fakeRows(1, 16)[0];
67
- const result = await verifyFunctional("/whatever", { engine: engineReturning([one, one]) });
68
- assert.equal(result.ok, false);
69
- assert.match(result.reason, /嵌入退化/);
70
- });
71
-
72
- test("功能验证:加载失败与推理失败分别给出不同措辞", async () => {
73
- const loadFail = await verifyFunctional("/whatever", {
74
- engine: async () => {
75
- throw new Error("Cannot find package 'onnxruntime-node'");
76
- }
77
- });
78
- assert.equal(loadFail.ok, false);
79
- assert.match(loadFail.reason, /加载运行时失败/);
80
- assert.match(loadFail.reason, /onnxruntime-node/);
81
-
82
- const embedFail = await verifyFunctional("/whatever", {
83
- engine: async () => async () => {
84
- throw new Error("boom");
85
- }
86
- });
87
- assert.equal(embedFail.ok, false);
88
- assert.match(embedFail.reason, /推理失败/);
89
- });
90
-
91
- test("功能验证:返回空向量要报出来,而不是当成 dim=0 通过", async () => {
92
- const result = await verifyFunctional("/whatever", { engine: engineReturning([[], []]) });
93
- assert.equal(result.ok, false);
94
- assert.match(result.reason, /空向量/);
95
- });
96
-
97
- test("三件套:结构不过就不再做功能验证(省掉一次无谓的加载)", async () => {
98
- const dir = makePayload({ withEntry: false });
99
- let engineCalled = false;
100
- const result = await verifyPayload(dir, {
101
- platform: "win32",
102
- arch: "x64",
103
- engine: async () => {
104
- engineCalled = true;
105
- return async () => fakeRows(2, 8);
106
- }
107
- });
108
- assert.equal(result.ok, false);
109
- assert.equal(result.functional.ok, false);
110
- assert.match(result.functional.reason, /跳过功能验证/);
111
- assert.equal(engineCalled, false);
112
- });
113
-
114
- test("三件套:结构 + 功能都过、完整性缺失时如实标 unverified 且不判定失败", async () => {
115
- const dir = makePayload();
116
- const result = await verifyPayload(dir, {
117
- platform: "win32",
118
- arch: "x64",
119
- engine: engineReturning(fakeRows(2, 8))
120
- });
121
- assert.equal(result.ok, true);
122
- assert.equal(result.integrity.status, "unverified");
123
- assert.equal(result.integrity.ok, true);
124
- assert.equal(result.version, "4.2.0");
125
- });
126
-
127
- test("三件套:--strict 场景下哈希不匹配即整体不通过", async () => {
128
- const dir = makePayload();
129
- const matched = await verifyPayload(dir, {
130
- platform: "win32",
131
- arch: "x64",
132
- engine: engineReturning(fakeRows(2, 8)),
133
- integrity: { status: "sha512-matched", detail: "tarball 比对通过" }
134
- });
135
- assert.equal(matched.ok, true);
136
- assert.equal(matched.integrity.status, "sha512-matched");
137
-
138
- const mismatch = await verifyPayload(dir, {
139
- platform: "win32",
140
- arch: "x64",
141
- engine: engineReturning(fakeRows(2, 8)),
142
- integrity: { status: "sha512-mismatch", detail: "与钉死的哈希不一致" }
143
- });
144
- assert.equal(mismatch.ok, false);
145
- assert.equal(mismatch.structural.ok, true, "结构仍然是对的,失败来自完整性");
146
- assert.equal(mismatch.functional.ok, true);
147
- });
148
-
149
- test("假通过防线 ①:行不是数组时返回失败,而不是抛异常", async () => {
150
- // 契约是「任何失败都返回结果对象,不抛」;非数组行会让 .length/.some 直接抛。
151
- const dir = makePayload();
152
- const result = await verifyPayload(dir, {
153
- platform: "win32",
154
- arch: "x64",
155
- engine: engineReturning([null, null])
156
- });
157
- assert.equal(result.ok, false);
158
- assert.match(result.functional.reason, /不是数组/);
159
- });
160
-
161
- test("假通过防线 ②:非有限数值必须判失败(NaN 的比较恒为 false,会静默通过)", async () => {
162
- const dir = makePayload();
163
- for (const bad of [[NaN, 0], [Infinity, 0], ["0.5", "0.5"]]) {
164
- const result = await verifyPayload(dir, {
165
- platform: "win32",
166
- arch: "x64",
167
- engine: engineReturning([bad, [0, 0]])
168
- });
169
- assert.equal(result.ok, false, `含 ${JSON.stringify(bad)} 的向量不该被判通过`);
170
- assert.match(result.functional.reason, /非有限数值/);
171
- }
172
- });
173
-
174
- test("默认 engine 是导出的函数,且探针文本固定(改它等于改验收标准)", () => {
175
- assert.equal(typeof defaultEngine, "function");
176
- assert.deepEqual(DEFAULT_PROBE_TEXTS, ["猫咪喜欢晒太阳", "the quick brown fox"]);
177
- });
178
-
179
- // ── defaultEngine:注入假 transformers 模块,钉住「真 import + 行切分」的契约 ──
180
-
181
- /** 造一个假 transformers 入口:export env + pipeline,把收到的参数记到 globalThis。 */
182
- function fakeEntry(dims) {
183
- const dir = mkdtempSync(join(tmpdir(), "mneme-engine-"));
184
- const entry = join(dir, "entry.mjs");
185
- writeFileSync(entry, `
186
- export const env = {};
187
- export async function pipeline(kind, model, opts) {
188
- globalThis.__engineSeen = { kind, model, opts, cacheDir: env.cacheDir, remote: env.allowRemoteModels };
189
- const d = ${JSON.stringify(dims)};
190
- const data = Float32Array.from({ length: d[0] * d[1] }, (_, i) => i + 1);
191
- return async () => ({ dims: d, data });
192
- }
193
- `);
194
- return pathToFileURL(entry).href;
195
- }
196
-
197
- test("defaultEngine: 假 transformers 入口可驱动嵌入,并按输出维度做行切分", async () => {
198
- const entry = fakeEntry([2, 4]);
199
- const embed = await defaultEngine(entry, { cacheDir: "/tmp/mneme-cache" });
200
- const rows = await embed(["猫咪", "fox"]);
201
- assert.equal(rows.length, 2);
202
- assert.deepEqual(rows[0], [1, 2, 3, 4]);
203
- assert.deepEqual(rows[1], [5, 6, 7, 8]);
204
- // 缓存目录与离线开关必须落到 env:验证过程绝不能偷偷下载模型。
205
- assert.equal(globalThis.__engineSeen.cacheDir, "/tmp/mneme-cache");
206
- assert.equal(globalThis.__engineSeen.remote, false);
207
- assert.equal(globalThis.__engineSeen.kind, "feature-extraction");
208
- });
209
-
210
- test("defaultEngine: 输出宽度为 0 时明确抛错(防止调用方死循环)", async () => {
211
- const entry = fakeEntry([2, 0]);
212
- const embed = await defaultEngine(entry, {});
213
- await assert.rejects(() => embed(["x"]), /维度不可用/);
214
- });
1
+ import test from "node:test";
2
+ import assert from "node:assert/strict";
3
+ import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
4
+ import { tmpdir } from "node:os";
5
+ import { join } from "node:path";
6
+ import { DEFAULT_PROBE_TEXTS, defaultEngine, verifyFunctional, verifyPayload } from "../src/runtime/verify.js";
7
+ import { TRANSFORMERS_ENTRY } from "../src/runtime/layout.js";
8
+ import { pathToFileURL } from "node:url";
9
+
10
+ // 验证三件套(issue #131 / PR-A)。重点不是「能跑通」,而是「坏的那几种能被抓住」:
11
+ // 结构对但原生二进制与本机不匹配、返回非归一化向量、向量退化、加载直接抛——
12
+ // 这些都必须变成一条可读的失败结论,而不是让调用方拿到一个假的 ok。
13
+
14
+ /** 造一个结构上合法的 payload(只有形状,没有真东西)。 */
15
+ function makePayload({ platform = "win32", arch = "x64", withEntry = true } = {}) {
16
+ const dir = mkdtempSync(join(tmpdir(), "mneme-verify-"));
17
+ const write = (rel, body) => {
18
+ const full = join(dir, rel);
19
+ mkdirSync(join(full, ".."), { recursive: true });
20
+ writeFileSync(full, body);
21
+ };
22
+ for (const name of ["@huggingface/transformers", "onnxruntime-node", "sharp"]) {
23
+ write(join("node_modules", name, "package.json"), JSON.stringify({ name, version: name === "@huggingface/transformers" ? "4.2.0" : "1.0.0" }));
24
+ }
25
+ if (withEntry) write(TRANSFORMERS_ENTRY, "export const x = 1;\n");
26
+ mkdirSync(join(dir, "node_modules", "onnxruntime-node", "bin", "napi-v6", platform, arch), { recursive: true });
27
+ return dir;
28
+ }
29
+
30
+ /** 归一化的假向量:`rows` 行,每行 dim 维,方向由 seed 决定。 */
31
+ function fakeRows(rows, dim, seed = 0) {
32
+ return Array.from({ length: rows }, (_, r) => {
33
+ const v = Array.from({ length: dim }, (_, i) => Math.sin((i + 1) * (r + 1 + seed)));
34
+ const length = Math.hypot(...v);
35
+ return v.map((x) => x / length);
36
+ });
37
+ }
38
+
39
+ /** 注入一个「什么都能返回」的 engine。 */
40
+ function engineReturning(value) {
41
+ return async () => async () => value;
42
+ }
43
+
44
+ test("功能验证:正常的向量通过,并报出维度与耗时", async () => {
45
+ const result = await verifyFunctional("/whatever", { engine: engineReturning(fakeRows(2, 512)) });
46
+ assert.equal(result.ok, true, result.reason);
47
+ assert.equal(result.dim, 512);
48
+ assert.equal(result.rows, 2);
49
+ assert.ok(result.elapsedMs >= 0);
50
+ });
51
+
52
+ test("功能验证:行数不符要报出来(而不是只取第一行当作通过)", async () => {
53
+ const result = await verifyFunctional("/whatever", { engine: engineReturning(fakeRows(1, 512)) });
54
+ assert.equal(result.ok, false);
55
+ assert.match(result.reason, /返回行数不符/);
56
+ });
57
+
58
+ test("功能验证:非归一化向量要报出来(说明 normalize 没生效)", async () => {
59
+ const rows = fakeRows(2, 8).map((row) => row.map((x) => x * 3));
60
+ const result = await verifyFunctional("/whatever", { engine: engineReturning(rows) });
61
+ assert.equal(result.ok, false);
62
+ assert.match(result.reason, /不是 L2 归一化/);
63
+ });
64
+
65
+ test("功能验证:向量退化成同一方向要报出来", async () => {
66
+ const one = fakeRows(1, 16)[0];
67
+ const result = await verifyFunctional("/whatever", { engine: engineReturning([one, one]) });
68
+ assert.equal(result.ok, false);
69
+ assert.match(result.reason, /嵌入退化/);
70
+ });
71
+
72
+ test("功能验证:加载失败与推理失败分别给出不同措辞", async () => {
73
+ const loadFail = await verifyFunctional("/whatever", {
74
+ engine: async () => {
75
+ throw new Error("Cannot find package 'onnxruntime-node'");
76
+ }
77
+ });
78
+ assert.equal(loadFail.ok, false);
79
+ assert.match(loadFail.reason, /加载运行时失败/);
80
+ assert.match(loadFail.reason, /onnxruntime-node/);
81
+
82
+ const embedFail = await verifyFunctional("/whatever", {
83
+ engine: async () => async () => {
84
+ throw new Error("boom");
85
+ }
86
+ });
87
+ assert.equal(embedFail.ok, false);
88
+ assert.match(embedFail.reason, /推理失败/);
89
+ });
90
+
91
+ test("功能验证:返回空向量要报出来,而不是当成 dim=0 通过", async () => {
92
+ const result = await verifyFunctional("/whatever", { engine: engineReturning([[], []]) });
93
+ assert.equal(result.ok, false);
94
+ assert.match(result.reason, /空向量/);
95
+ });
96
+
97
+ test("三件套:结构不过就不再做功能验证(省掉一次无谓的加载)", async () => {
98
+ const dir = makePayload({ withEntry: false });
99
+ let engineCalled = false;
100
+ const result = await verifyPayload(dir, {
101
+ platform: "win32",
102
+ arch: "x64",
103
+ engine: async () => {
104
+ engineCalled = true;
105
+ return async () => fakeRows(2, 8);
106
+ }
107
+ });
108
+ assert.equal(result.ok, false);
109
+ assert.equal(result.functional.ok, false);
110
+ assert.match(result.functional.reason, /跳过功能验证/);
111
+ assert.equal(engineCalled, false);
112
+ });
113
+
114
+ test("三件套:结构 + 功能都过、完整性缺失时如实标 unverified 且不判定失败", async () => {
115
+ const dir = makePayload();
116
+ const result = await verifyPayload(dir, {
117
+ platform: "win32",
118
+ arch: "x64",
119
+ engine: engineReturning(fakeRows(2, 8))
120
+ });
121
+ assert.equal(result.ok, true);
122
+ assert.equal(result.integrity.status, "unverified");
123
+ assert.equal(result.integrity.ok, true);
124
+ assert.equal(result.version, "4.2.0");
125
+ });
126
+
127
+ test("三件套:--strict 场景下哈希不匹配即整体不通过", async () => {
128
+ const dir = makePayload();
129
+ const matched = await verifyPayload(dir, {
130
+ platform: "win32",
131
+ arch: "x64",
132
+ engine: engineReturning(fakeRows(2, 8)),
133
+ integrity: { status: "sha512-matched", detail: "tarball 比对通过" }
134
+ });
135
+ assert.equal(matched.ok, true);
136
+ assert.equal(matched.integrity.status, "sha512-matched");
137
+
138
+ const mismatch = await verifyPayload(dir, {
139
+ platform: "win32",
140
+ arch: "x64",
141
+ engine: engineReturning(fakeRows(2, 8)),
142
+ integrity: { status: "sha512-mismatch", detail: "与钉死的哈希不一致" }
143
+ });
144
+ assert.equal(mismatch.ok, false);
145
+ assert.equal(mismatch.structural.ok, true, "结构仍然是对的,失败来自完整性");
146
+ assert.equal(mismatch.functional.ok, true);
147
+ });
148
+
149
+ test("三件套:清单里记下的来源完整性被认账,不因状态词不同就判失败(issue #268)", async () => {
150
+ const dir = makePayload();
151
+ // download.js 在落盘前逐个 tarball 比对过 sha512,把这个结论写进清单的形状与词就是下面这样。
152
+ writeFileSync(
153
+ join(dir, "mneme-runtime.json"),
154
+ JSON.stringify({
155
+ procedure: "downloaded",
156
+ version: "4.2.0",
157
+ integrity: { status: "verified", checked: 3, detail: "每个 tarball 的 sha512 与 runtime-manifest.json 一致" }
158
+ })
159
+ );
160
+ const result = await verifyPayload(dir, {
161
+ platform: "win32",
162
+ arch: "x64",
163
+ engine: engineReturning(fakeRows(2, 8))
164
+ });
165
+ assert.equal(result.integrity.status, "verified");
166
+ assert.equal(result.integrity.ok, true, "下载档记下的 verified 是可用结论,不是失败");
167
+ assert.match(result.integrity.detail, /每个 tarball/);
168
+ assert.equal(result.ok, true);
169
+ });
170
+
171
+ test("三件套:清单里记下的不一致照样判失败(别把「读到一份结论」变成「一律放过」)", async () => {
172
+ const dir = makePayload();
173
+ writeFileSync(
174
+ join(dir, "mneme-runtime.json"),
175
+ JSON.stringify({ procedure: "downloaded", integrity: { status: "sha512-mismatch", detail: "与钉死的哈希不一致" } })
176
+ );
177
+ const result = await verifyPayload(dir, {
178
+ platform: "win32",
179
+ arch: "x64",
180
+ engine: engineReturning(fakeRows(2, 8))
181
+ });
182
+ assert.equal(result.integrity.ok, false);
183
+ assert.equal(result.integrity.status, "sha512-mismatch");
184
+ assert.equal(result.ok, false);
185
+ });
186
+
187
+ test("三件套:清单里的 integrity 形状非法时 fail-closed,不读成「没验过」(#269 评审)", async () => {
188
+ const dir = makePayload();
189
+ writeFileSync(join(dir, "mneme-runtime.json"), JSON.stringify({ procedure: "downloaded", integrity: false }));
190
+ const result = await verifyPayload(dir, {
191
+ platform: "win32",
192
+ arch: "x64",
193
+ engine: engineReturning(fakeRows(2, 8))
194
+ });
195
+ assert.equal(result.integrity.status, "malformed");
196
+ assert.equal(result.integrity.ok, false, "形状错误不是「验过了」,也不能当成「没记录」放过");
197
+ assert.equal(result.ok, false);
198
+ assert.match(result.integrity.detail, /形状非法/);
199
+ });
200
+
201
+ test("三件套:显式报 unverified 与「没传」同判,不因为说出口就变失败", async () => {
202
+ const dir = makePayload();
203
+ const result = await verifyPayload(dir, {
204
+ platform: "win32",
205
+ arch: "x64",
206
+ engine: engineReturning(fakeRows(2, 8)),
207
+ integrity: { status: "unverified" }
208
+ });
209
+ assert.equal(result.ok, true);
210
+ assert.equal(result.integrity.status, "unverified");
211
+ assert.match(result.integrity.detail, /没有可比对的原始产物/);
212
+ });
213
+
214
+ test("假通过防线 ①:行不是数组时返回失败,而不是抛异常", async () => {
215
+ // 契约是「任何失败都返回结果对象,不抛」;非数组行会让 .length/.some 直接抛。
216
+ const dir = makePayload();
217
+ const result = await verifyPayload(dir, {
218
+ platform: "win32",
219
+ arch: "x64",
220
+ engine: engineReturning([null, null])
221
+ });
222
+ assert.equal(result.ok, false);
223
+ assert.match(result.functional.reason, /不是数组/);
224
+ });
225
+
226
+ test("假通过防线 ②:非有限数值必须判失败(NaN 的比较恒为 false,会静默通过)", async () => {
227
+ const dir = makePayload();
228
+ for (const bad of [[NaN, 0], [Infinity, 0], ["0.5", "0.5"]]) {
229
+ const result = await verifyPayload(dir, {
230
+ platform: "win32",
231
+ arch: "x64",
232
+ engine: engineReturning([bad, [0, 0]])
233
+ });
234
+ assert.equal(result.ok, false, `含 ${JSON.stringify(bad)} 的向量不该被判通过`);
235
+ assert.match(result.functional.reason, /非有限数值/);
236
+ }
237
+ });
238
+
239
+ test("默认 engine 是导出的函数,且探针文本固定(改它等于改验收标准)", () => {
240
+ assert.equal(typeof defaultEngine, "function");
241
+ assert.deepEqual(DEFAULT_PROBE_TEXTS, ["猫咪喜欢晒太阳", "the quick brown fox"]);
242
+ });
243
+
244
+ // ── defaultEngine:注入假 transformers 模块,钉住「真 import + 行切分」的契约 ──
245
+
246
+ /** 造一个假 transformers 入口:export env + pipeline,把收到的参数记到 globalThis。 */
247
+ function fakeEntry(dims) {
248
+ const dir = mkdtempSync(join(tmpdir(), "mneme-engine-"));
249
+ const entry = join(dir, "entry.mjs");
250
+ writeFileSync(entry, `
251
+ export const env = {};
252
+ export async function pipeline(kind, model, opts) {
253
+ globalThis.__engineSeen = { kind, model, opts, cacheDir: env.cacheDir, remote: env.allowRemoteModels };
254
+ const d = ${JSON.stringify(dims)};
255
+ const data = Float32Array.from({ length: d[0] * d[1] }, (_, i) => i + 1);
256
+ return async () => ({ dims: d, data });
257
+ }
258
+ `);
259
+ return pathToFileURL(entry).href;
260
+ }
261
+
262
+ test("defaultEngine: 假 transformers 入口可驱动嵌入,并按输出维度做行切分", async () => {
263
+ const entry = fakeEntry([2, 4]);
264
+ const embed = await defaultEngine(entry, { cacheDir: "/tmp/mneme-cache" });
265
+ const rows = await embed(["猫咪", "fox"]);
266
+ assert.equal(rows.length, 2);
267
+ assert.deepEqual(rows[0], [1, 2, 3, 4]);
268
+ assert.deepEqual(rows[1], [5, 6, 7, 8]);
269
+ // 缓存目录与离线开关必须落到 env:验证过程绝不能偷偷下载模型。
270
+ assert.equal(globalThis.__engineSeen.cacheDir, "/tmp/mneme-cache");
271
+ assert.equal(globalThis.__engineSeen.remote, false);
272
+ assert.equal(globalThis.__engineSeen.kind, "feature-extraction");
273
+ });
274
+
275
+ test("defaultEngine: 输出宽度为 0 时明确抛错(防止调用方死循环)", async () => {
276
+ const entry = fakeEntry([2, 0]);
277
+ const embed = await defaultEngine(entry, {});
278
+ await assert.rejects(() => embed(["x"]), /维度不可用/);
279
+ });