dsh-memu-memory 1.4.2 → 1.4.4

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 (389) hide show
  1. package/AGENTS.md +108 -110
  2. package/CHANGELOG.md +173 -868
  3. package/LICENSE +21 -21
  4. package/README.md +368 -374
  5. package/README.zh.md +354 -360
  6. package/assets/icon.svg +7 -7
  7. package/cordis.patch.yml +8 -8
  8. package/docs/active-memory-ab.md +279 -385
  9. package/docs/binding-lifecycle.md +99 -99
  10. package/docs/client-conventions.md +75 -75
  11. package/docs/contributing.md +217 -217
  12. package/docs/emotion-model-spec.md +247 -281
  13. package/docs/env-and-commands.md +149 -152
  14. package/docs/host-adaptation.md +339 -360
  15. package/docs/host-api.md +131 -137
  16. package/docs/host-versions.md +117 -120
  17. package/docs/mcp-porting.md +146 -146
  18. package/docs/memory-semantics.md +271 -259
  19. package/docs/pending-work.md +44 -51
  20. package/docs/troubleshooting.md +909 -854
  21. package/lib/active-memory.js +160 -165
  22. package/lib/activity-log.js +207 -207
  23. package/lib/auto-link/apply.js +248 -249
  24. package/lib/auto-link/candidates.js +163 -163
  25. package/lib/auto-link/decide.js +300 -300
  26. package/lib/auto-link/normalize.js +99 -99
  27. package/lib/auto-link/policy.js +42 -42
  28. package/lib/auto-link/suppression.js +101 -101
  29. package/lib/auto-link/terminology.js +311 -311
  30. package/lib/backup.js +1254 -1254
  31. package/lib/binding-diagnostics.js +634 -634
  32. package/lib/budget.js +118 -118
  33. package/lib/client.js +9820 -9751
  34. package/lib/commitment.js +178 -138
  35. package/lib/consolidation-ledger.js +434 -438
  36. package/lib/consolidation-receipt.js +386 -387
  37. package/lib/consolidation-snapshot.js +420 -405
  38. package/lib/consolidation-worker.js +240 -241
  39. package/lib/context.js +33 -33
  40. package/lib/curator-evidence.js +131 -131
  41. package/lib/curator-policy.js +198 -202
  42. package/lib/curator.js +1813 -1811
  43. package/lib/cycle.js +524 -513
  44. package/lib/diagnostics.js +524 -524
  45. package/lib/embedding-catalog.js +1051 -1055
  46. package/lib/embedding-download.js +637 -607
  47. package/lib/embedding-runtime.js +946 -946
  48. package/lib/engine.js +1103 -1103
  49. package/lib/entity-repair.js +285 -285
  50. package/lib/environment-check.js +394 -394
  51. package/lib/fork-model.js +312 -312
  52. package/lib/freshness.js +128 -121
  53. package/lib/global-graph.js +389 -389
  54. package/lib/graph.js +1193 -1193
  55. package/lib/host-surface.js +30 -30
  56. package/lib/inbound.js +284 -281
  57. package/lib/index.js +506 -511
  58. package/lib/inject.js +2639 -2619
  59. package/lib/lexicon-scope.js +143 -143
  60. package/lib/lexicon.js +1183 -1192
  61. package/lib/lifecycle-audit.js +37 -0
  62. package/lib/local-embedding-entry.js +145 -145
  63. package/lib/local-embedding-manager.js +306 -306
  64. package/lib/local-embedding-protocol.js +210 -210
  65. package/lib/local-embedding-worker.js +682 -682
  66. package/lib/local-vector-service.js +506 -453
  67. package/lib/maintenance-ledger.js +138 -0
  68. package/lib/maintenance-review.js +103 -0
  69. package/lib/maintenance-runner.js +267 -0
  70. package/lib/managed-runtime.js +118 -118
  71. package/lib/media.js +607 -513
  72. package/lib/memory.js +5035 -4941
  73. package/lib/memu-config.js +213 -213
  74. package/lib/memu.js +565 -565
  75. package/lib/migrate.js +200 -200
  76. package/lib/mood.js +300 -300
  77. package/lib/paths.js +453 -457
  78. package/lib/port-surface.js +82 -82
  79. package/lib/preset-mount.js +85 -0
  80. package/lib/preset-scope.js +126 -126
  81. package/lib/private-links.js +567 -567
  82. package/lib/private.js +2198 -2209
  83. package/lib/prompts/global-curator.md +135 -135
  84. package/lib/prompts/image-caption.md +1 -1
  85. package/lib/prompts/private-consolidation.coding.md +39 -39
  86. package/lib/prompts/private-consolidation.md +32 -32
  87. package/lib/prompts/private-consolidation.mixed.md +37 -37
  88. package/lib/prompts/private-consolidation.rp.md +47 -47
  89. package/lib/prompts/private-policy.coding.md +35 -35
  90. package/lib/prompts/private-policy.md +36 -36
  91. package/lib/prompts/private-policy.mixed.md +29 -29
  92. package/lib/prompts/private-policy.rp.md +43 -43
  93. package/lib/prompts/system-guide.coding.md +20 -20
  94. package/lib/prompts/system-guide.md +25 -25
  95. package/lib/prompts/system-guide.mixed.md +22 -22
  96. package/lib/prompts/system-guide.rp.md +23 -23
  97. package/lib/prompts.js +331 -331
  98. package/lib/recall-cache.js +212 -212
  99. package/lib/remote-summary.js +123 -123
  100. package/lib/rpc.js +2531 -2532
  101. package/lib/salient-wake.js +438 -438
  102. package/lib/snippet.js +125 -125
  103. package/lib/state.js +1948 -1928
  104. package/lib/stats.js +236 -236
  105. package/lib/temporal.js +345 -348
  106. package/lib/text-retrieval.js +992 -1001
  107. package/lib/text-similarity.js +305 -305
  108. package/lib/tokenizer-v2.js +271 -271
  109. package/lib/tools.js +1098 -1091
  110. package/lib/turn-log.js +545 -545
  111. package/lib/vector-migration.js +928 -930
  112. package/lib/vector-profile.js +1353 -1359
  113. package/lib/visibility.js +59 -59
  114. package/lib/webui/graph-LICENSE.txt +1 -1
  115. package/lib/webui/graph-view.bundle.js +2 -2
  116. package/lib/webui/graph-view.js +1171 -1175
  117. package/lib/webui/mark.svg +7 -7
  118. package/lib/webui/standalone.css +1178 -1178
  119. package/lib/webui/standalone.js +238 -238
  120. package/lib/webui.js +136 -136
  121. package/lib/worker-pool.js +1020 -979
  122. package/package.json +153 -153
  123. package/requirements.lock +34 -34
  124. package/scripts/acceptance-local.mjs +946 -946
  125. package/scripts/active-memory-ab-live.mjs +653 -655
  126. package/scripts/active-memory-ab.mjs +1302 -1302
  127. package/scripts/audit-memory-lifecycle.mjs +55 -0
  128. package/scripts/bench-recall.mjs +142 -142
  129. package/scripts/build-graph.mjs +44 -45
  130. package/scripts/chain-matrix.mjs +541 -541
  131. package/scripts/check-all.mjs +68 -74
  132. package/scripts/check-doc-drift.mjs +547 -547
  133. package/scripts/check-graph-browser.mjs +261 -261
  134. package/scripts/check-i18n.mjs +81 -83
  135. package/scripts/check-idle-compositing.mjs +61 -61
  136. package/scripts/check-seed-compat.mjs +115 -118
  137. package/scripts/check-text-format.mjs +55 -0
  138. package/scripts/curator-pair-calibrate.mjs +259 -259
  139. package/scripts/diagnose-bindings.mjs +176 -176
  140. package/scripts/drive-session.mjs +863 -864
  141. package/scripts/engine-smoke.mjs +69 -69
  142. package/scripts/entity-dedupe.mjs +110 -110
  143. package/scripts/lexicon-calibrate.mjs +230 -230
  144. package/scripts/lexicon-dedupe.mjs +318 -318
  145. package/scripts/local-runtime-install.mjs +142 -142
  146. package/scripts/local-runtime-verify.mjs +56 -56
  147. package/scripts/local-vector-verify.mjs +93 -93
  148. package/scripts/make-readme-shots.mjs +59 -59
  149. package/scripts/migrate-visibility.mjs +94 -94
  150. package/scripts/mock-llm.mjs +938 -903
  151. package/scripts/preview.mjs +58 -58
  152. package/scripts/prune-sessions.mjs +31 -34
  153. package/scripts/prune-turnlogs.mjs +29 -30
  154. package/scripts/prune-vectors.mjs +24 -24
  155. package/scripts/rebuild-auto-links.mjs +252 -252
  156. package/scripts/recall-bench.mjs +820 -820
  157. package/scripts/repetition-ab-scenarios.mjs +93 -93
  158. package/scripts/repetition-detector-snapshot.mjs +314 -314
  159. package/scripts/repetition-eval-live.mjs +360 -360
  160. package/scripts/repetition-eval.mjs +702 -702
  161. package/scripts/replay.mjs +705 -575
  162. package/scripts/setup.mjs +46 -46
  163. package/scripts/simulate-injection.mjs +299 -301
  164. package/tests/absorb-review.test.mjs +354 -354
  165. package/tests/activation.test.mjs +89 -89
  166. package/tests/active-memory-ab.test.mjs +745 -745
  167. package/tests/active-memory.test.mjs +129 -129
  168. package/tests/activity-log.test.mjs +126 -126
  169. package/tests/activity-wiring.test.mjs +155 -155
  170. package/tests/appendix-b.test.mjs +217 -217
  171. package/tests/appendix-b2.test.mjs +166 -166
  172. package/tests/audit-batch3.test.mjs +247 -247
  173. package/tests/audit-regression.test.mjs +275 -275
  174. package/tests/audit-safety.test.mjs +227 -227
  175. package/tests/auto-link-candidates.test.mjs +245 -245
  176. package/tests/auto-link-decide.test.mjs +512 -512
  177. package/tests/auto-link-migration.test.mjs +133 -133
  178. package/tests/backend-compensation.test.mjs +272 -272
  179. package/tests/backup.test.mjs +793 -793
  180. package/tests/binding-diagnostics.test.mjs +720 -720
  181. package/tests/browser.test.mjs +476 -476
  182. package/tests/client-export.test.mjs +521 -521
  183. package/tests/client-render.test.mjs +2008 -1728
  184. package/tests/coalesce.test.mjs +113 -113
  185. package/tests/commitment.test.mjs +243 -144
  186. package/tests/config.test.mjs +159 -159
  187. package/tests/consolidation-credential.test.mjs +330 -330
  188. package/tests/consolidation-feed.test.mjs +122 -122
  189. package/tests/consolidation-fork.test.mjs +1587 -1539
  190. package/tests/consolidation-ledger.test.mjs +389 -389
  191. package/tests/consolidation-snapshot.test.mjs +567 -505
  192. package/tests/consolidation-value.test.mjs +89 -89
  193. package/tests/context-scope.test.mjs +40 -40
  194. package/tests/cross-library-recall.test.mjs +269 -269
  195. package/tests/cue.test.mjs +58 -58
  196. package/tests/curator-absorb.test.mjs +444 -444
  197. package/tests/curator-calibrate.test.mjs +173 -173
  198. package/tests/curator-clusters.test.mjs +306 -306
  199. package/tests/curator-commit.test.mjs +325 -325
  200. package/tests/curator-contract.test.mjs +191 -191
  201. package/tests/curator-evidence.test.mjs +102 -102
  202. package/tests/curator-link.test.mjs +317 -317
  203. package/tests/curator-pairing.test.mjs +339 -339
  204. package/tests/curator-probe.test.mjs +490 -490
  205. package/tests/curator-review-mode.test.mjs +315 -315
  206. package/tests/curator.test.mjs +152 -152
  207. package/tests/cycle.test.mjs +567 -568
  208. package/tests/diagnostics.test.mjs +278 -278
  209. package/tests/disclosure.test.mjs +249 -249
  210. package/tests/doc-drift.test.mjs +342 -342
  211. package/tests/drive-session.test.mjs +152 -152
  212. package/tests/dynamic-rrf.test.mjs +182 -182
  213. package/tests/dynamic-threshold.test.mjs +52 -52
  214. package/tests/embedding-catalog.test.mjs +374 -374
  215. package/tests/embedding-download.test.mjs +529 -486
  216. package/tests/embedding-runtime.test.mjs +449 -449
  217. package/tests/emotion-chain.test.mjs +84 -84
  218. package/tests/emotion-contract.test.mjs +445 -445
  219. package/tests/engine.test.mjs +108 -108
  220. package/tests/entity-merge.test.mjs +278 -278
  221. package/tests/environment-check.test.mjs +187 -187
  222. package/tests/fact-version.test.mjs +146 -146
  223. package/tests/fixtures/active-memory-ab/README.md +56 -56
  224. package/tests/fixtures/active-memory-ab/cases/01-clean-dense-turn.json +351 -351
  225. package/tests/fixtures/active-memory-ab/cases/02-clean-thin-turn.json +122 -122
  226. package/tests/fixtures/active-memory-ab/cases/03-clean-starved-small.json +125 -125
  227. package/tests/fixtures/active-memory-ab/cases/04-clean-large-thin.json +137 -137
  228. package/tests/fixtures/active-memory-ab/cases/05-clean-half-step.json +166 -166
  229. package/tests/fixtures/active-memory-ab/cases/06-clean-duplicate-reread.json +197 -197
  230. package/tests/fixtures/active-memory-ab/cases/07-clean-recent-recall.json +125 -125
  231. package/tests/fixtures/active-memory-ab/cases/08-global-relevant.json +111 -111
  232. package/tests/fixtures/active-memory-ab/cases/09-global-irrelevant-conflict.json +158 -158
  233. package/tests/fixtures/active-memory-ab/cases/10-no-human-turn.json +97 -97
  234. package/tests/fixtures/active-memory-ab/cases/11-clean-scale-100.json +137 -137
  235. package/tests/fixtures/active-memory-ab/cases/12-clean-scale-1000.json +137 -137
  236. package/tests/fixtures/active-memory-ab/cases/13-clean-scale-5000.json +137 -137
  237. package/tests/fixtures/active-memory-ab/cases/14-clean-scale-20000.json +137 -137
  238. package/tests/fixtures/active-memory-ab/lead-ai-reviewed-reference.json +191 -191
  239. package/tests/fixtures/active-memory-ab/manifest.json +199 -199
  240. package/tests/fixtures/curator-pairs/labeled.v1.jsonl +90 -90
  241. package/tests/fixtures/lexicon-stub-hooks.mjs +15 -15
  242. package/tests/fixtures/lexicon-stub.mjs +284 -284
  243. package/tests/fixtures/mock-llm/reply.txt +1 -1
  244. package/tests/fixtures/recall-bench/01-paraphrase.json +183 -183
  245. package/tests/fixtures/recall-bench/02-exact.json +152 -152
  246. package/tests/fixtures/recall-bench/03-negation.json +172 -172
  247. package/tests/fixtures/recall-bench/04-no-answer.json +161 -161
  248. package/tests/fixtures/recall-bench/05-conflict.json +193 -193
  249. package/tests/fixtures/recall-bench/06-boundary.json +272 -272
  250. package/tests/fixtures/recall-bench/07-secret.json +190 -190
  251. package/tests/fixtures/recall-bench/08-coding.json +163 -163
  252. package/tests/fixtures/recall-bench-gold-candidates/01-paraphrase.json +193 -193
  253. package/tests/fixtures/recall-bench-gold-candidates/02-exact.json +197 -197
  254. package/tests/fixtures/recall-bench-gold-candidates/03-negation.json +244 -244
  255. package/tests/fixtures/recall-bench-gold-candidates/04-no-answer.json +189 -189
  256. package/tests/fixtures/recall-bench-gold-candidates/05-conflict.json +245 -245
  257. package/tests/fixtures/recall-bench-gold-candidates/06-boundary.json +279 -279
  258. package/tests/fixtures/recall-bench-gold-candidates/07-secret.json +224 -224
  259. package/tests/fixtures/recall-bench-gold-candidates/08-coding.json +218 -218
  260. package/tests/fixtures/repetition-eval/01-zh-rp.json +92 -92
  261. package/tests/fixtures/repetition-eval/02-en.json +96 -96
  262. package/tests/fixtures/repetition-eval/03-code.json +90 -90
  263. package/tests/fixtures/repetition-eval/04-names.json +115 -115
  264. package/tests/fixtures/repetition-eval/05-user-quote.json +95 -95
  265. package/tests/fixtures/repetition-eval/06-agreement.json +85 -85
  266. package/tests/fixtures/tokenizer-cases.mjs +27 -27
  267. package/tests/fork-model.test.mjs +345 -345
  268. package/tests/freshness.test.mjs +140 -140
  269. package/tests/global-graph.test.mjs +106 -106
  270. package/tests/global-lifecycle.test.mjs +149 -149
  271. package/tests/graph-cascade.test.mjs +363 -363
  272. package/tests/graph-view.test.mjs +414 -414
  273. package/tests/graph.test.mjs +371 -371
  274. package/tests/help-page.test.mjs +416 -416
  275. package/tests/hook-order.test.mjs +72 -72
  276. package/tests/humanlike.test.mjs +232 -232
  277. package/tests/hybrid-recall.test.mjs +610 -610
  278. package/tests/inbound.test.mjs +441 -441
  279. package/tests/inject-conflict.test.mjs +173 -173
  280. package/tests/inject-guide.test.mjs +513 -513
  281. package/tests/inject.test.mjs +384 -384
  282. package/tests/injection-budget-settings.test.mjs +35 -35
  283. package/tests/injection-budget.test.mjs +165 -165
  284. package/tests/injection-chain.test.mjs +442 -442
  285. package/tests/injection-intent-budget.test.mjs +92 -92
  286. package/tests/injection-rollback.test.mjs +428 -428
  287. package/tests/issue2-chain.test.mjs +169 -169
  288. package/tests/lexical-bm25.test.mjs +171 -171
  289. package/tests/lexicon-calibrate.test.mjs +48 -48
  290. package/tests/lexicon-dedupe.test.mjs +171 -171
  291. package/tests/lexicon-frequency.test.mjs +195 -195
  292. package/tests/lexicon-inject.test.mjs +459 -459
  293. package/tests/lexicon-perf.test.mjs +464 -464
  294. package/tests/lexicon-scope.test.mjs +256 -256
  295. package/tests/lexicon-ui.test.mjs +667 -667
  296. package/tests/lexicon.test.mjs +922 -922
  297. package/tests/lifecycle-audit.test.mjs +56 -0
  298. package/tests/loader-register.mjs +3 -3
  299. package/tests/loader.mjs +17 -17
  300. package/tests/local-embedding-entry.test.mjs +95 -95
  301. package/tests/local-embedding-env.test.mjs +51 -51
  302. package/tests/local-embedding-manager.test.mjs +494 -494
  303. package/tests/local-embedding-protocol.test.mjs +121 -121
  304. package/tests/local-embedding-worker.test.mjs +440 -440
  305. package/tests/local-vector-service.test.mjs +41 -0
  306. package/tests/maintenance-status.test.mjs +77 -0
  307. package/tests/maintenance.test.mjs +438 -0
  308. package/tests/managed-memory.test.mjs +1234 -1234
  309. package/tests/managed-runtime.test.mjs +72 -72
  310. package/tests/media-injection.test.mjs +225 -0
  311. package/tests/media.test.mjs +135 -135
  312. package/tests/memory-chain.test.mjs +169 -169
  313. package/tests/memory-fields.test.mjs +134 -134
  314. package/tests/memu-resident-route.test.mjs +130 -130
  315. package/tests/memu-resident.test.mjs +33 -33
  316. package/tests/migration-payload.test.mjs +40 -42
  317. package/tests/mock-llm.test.mjs +263 -225
  318. package/tests/mood.test.mjs +109 -109
  319. package/tests/multiprocess.test.mjs +29 -29
  320. package/tests/negation-polarity.test.mjs +135 -135
  321. package/tests/new-session-gate.test.mjs +617 -617
  322. package/tests/open-loop-date.test.mjs +416 -416
  323. package/tests/ops-hardening.test.mjs +209 -209
  324. package/tests/package-shipping.test.mjs +88 -88
  325. package/tests/pending-injection.test.mjs +107 -107
  326. package/tests/port-surface.test.mjs +138 -138
  327. package/tests/preset-mount.test.mjs +132 -0
  328. package/tests/preset-scope.test.mjs +212 -212
  329. package/tests/private-links.test.mjs +493 -493
  330. package/tests/prompt-semantics.test.mjs +51 -51
  331. package/tests/prompts.test.mjs +315 -315
  332. package/tests/query-decompose.test.mjs +62 -62
  333. package/tests/react-snapshot.test.mjs +24 -24
  334. package/tests/recall-bench.test.mjs +317 -317
  335. package/tests/recall-cache.test.mjs +292 -292
  336. package/tests/recall-corpus-eligibility.test.mjs +51 -51
  337. package/tests/recall-dedup.test.mjs +446 -446
  338. package/tests/recall-intent.test.mjs +172 -172
  339. package/tests/regression-sweep.test.mjs +311 -311
  340. package/tests/reminder-fairness.test.mjs +55 -0
  341. package/tests/remote.test.mjs +140 -140
  342. package/tests/rename.test.mjs +30 -30
  343. package/tests/repetition-detector-snapshot.test.mjs +231 -231
  344. package/tests/repetition-eval-live.test.mjs +177 -177
  345. package/tests/repetition-eval.test.mjs +373 -373
  346. package/tests/salience-domain.test.mjs +173 -173
  347. package/tests/salient-date.test.mjs +142 -142
  348. package/tests/salient-wake.test.mjs +389 -389
  349. package/tests/settings-beat.test.mjs +222 -222
  350. package/tests/settings-coverage.test.mjs +97 -97
  351. package/tests/settings-plumbing.test.mjs +79 -79
  352. package/tests/short-term.test.mjs +182 -166
  353. package/tests/sidebar-beat.test.mjs +213 -213
  354. package/tests/sidebar-refresh.test.mjs +123 -123
  355. package/tests/sidebar.test.mjs +102 -74
  356. package/tests/simulate-injection.test.mjs +76 -76
  357. package/tests/slot-similarity.test.mjs +153 -153
  358. package/tests/snippet.test.mjs +79 -79
  359. package/tests/source-kind.test.mjs +52 -52
  360. package/tests/speaker-provenance.test.mjs +120 -120
  361. package/tests/stale-maintenance.test.mjs +206 -131
  362. package/tests/state-gc.test.mjs +34 -34
  363. package/tests/state.test.mjs +155 -155
  364. package/tests/stats.test.mjs +112 -112
  365. package/tests/storage.test.mjs +125 -125
  366. package/tests/stubs/child-process-spy.mjs +14 -14
  367. package/tests/stubs/dsh-llm-pi-ai.js +13 -13
  368. package/tests/stubs/dsh-llm.js +5 -5
  369. package/tests/stubs/dsh-schedule.js +140 -140
  370. package/tests/stubs/dsh-session-query.js +26 -26
  371. package/tests/stubs/dsh-session.js +7 -7
  372. package/tests/stubs/dsh-tools.js +101 -101
  373. package/tests/temporal-repair.test.mjs +78 -0
  374. package/tests/temporal.test.mjs +124 -124
  375. package/tests/text-format.test.mjs +24 -0
  376. package/tests/tokenizer-v2.test.mjs +142 -142
  377. package/tests/tool-schema.test.mjs +126 -126
  378. package/tests/tools.test.mjs +810 -810
  379. package/tests/turn-log.test.mjs +487 -487
  380. package/tests/ui-defaults.test.mjs +57 -57
  381. package/tests/update-fields.test.mjs +138 -138
  382. package/tests/vector-gc.test.mjs +37 -37
  383. package/tests/vector-migration.test.mjs +810 -810
  384. package/tests/vector-profile.test.mjs +449 -449
  385. package/tests/visibility-migration.test.mjs +152 -152
  386. package/tests/webui-auth.test.mjs +963 -963
  387. package/tests/webui.test.mjs +421 -421
  388. package/tests/windows-env.test.mjs +89 -89
  389. package/tests/worker-lifecycle.test.mjs +1522 -1429
@@ -1,445 +1,445 @@
1
- // 独立验收:按 docs/emotion-model-spec.md v3 §8 写的一组黑盒契约测试。
2
- //
3
- // 这些断言只走公开入口(lib/mood.js 的纯函数、lib/state.js 的会话 mood、
4
- // lib/memory.js 的写入/更新/召回),不读实现私有字段,也不为了迁就现有行为
5
- // 而放宽期望:实现与 spec 不一致时,这里就应该失败。
6
- import test from 'node:test'
7
- import assert from 'node:assert/strict'
8
- import fs from 'node:fs'
9
- import os from 'node:os'
10
- import path from 'node:path'
11
-
12
- const home = fs.mkdtempSync(path.join(os.tmpdir(), 'dsh-memu-memory-emotion-contract-'))
13
- process.env.DSH_MEMU_HOME = home
14
-
15
- const state = await import('../lib/state.js')
16
- const memory = await import('../lib/memory.js')
17
- const mood = await import('../lib/mood.js')
18
-
19
- test.after(() => {
20
- try { fs.rmSync(home, { recursive: true, force: true }) } catch {}
21
- })
22
-
23
- const AXES = ['joy', 'anger', 'sorrow', 'fear']
24
- const shareSum = (emotion) => AXES.reduce((total, axis) => total + Number(emotion[axis]), 0)
25
- const close = (a, b, epsilon = 1e-9) => Math.abs(a - b) < epsilon
26
- const emptyEmotion = () => ({ joy: 0, anger: 0, sorrow: 0, fear: 0 })
27
-
28
- function execFor(sessionId, cwd) {
29
- return { agent: { session: { header: { id: sessionId, cwd, agentPreset: 'coding' } } } }
30
- }
31
-
32
- function setup(name) {
33
- const cwd = path.join(home, 'proj-' + name)
34
- const sessionId = 'emotion-' + name
35
- const binding = state.ensureSession(sessionId, { cwd, agentPreset: 'coding' })
36
- return { cwd, sessionId, binding, exec: execFor(sessionId, cwd) }
37
- }
38
-
39
- function entryOf(binding, id) {
40
- return state.loadPrivateIndex(binding.libraryId).entries[id]
41
- }
42
-
43
- /**
44
- * 反解一个让 fade 正好等于 target 的时刻。
45
- *
46
- * fade = 0.5 ** (minutes / 90),所以先求出实数解,再在相邻 double 上搜一个
47
- * 让 Math.pow 的结果精确落在 target 上的取值——「fade === 0.35 仍保留」这条
48
- * 边界只有拿到精确相等才验证得了。
49
- */
50
- function timeAtFade(target) {
51
- const base = (Math.log(target) / Math.log(0.5)) * 90 * 60_000
52
- for (let i = -20_000; i <= 20_000; i += 1) {
53
- const now = base + i * Number.EPSILON * base
54
- if (Math.pow(0.5, (now / 60_000) / 90) === target) return now
55
- }
56
- return null
57
- }
58
-
59
- // ---- §8 第一条:归一化 ----
60
-
61
- test('normalizeEmotion 归一后和为 1,所有分量非负', () => {
62
- const samples = [
63
- { joy: 3, anger: 1, sorrow: 0, fear: 0 },
64
- { joy: 0.1, anger: 0.2, sorrow: 0.3, fear: 0.4 },
65
- { joy: 0.0001, anger: 0.0002, sorrow: 0.0003, fear: 0.0004 },
66
- { joy: 1_000_000, anger: 1, sorrow: 1, fear: 1 },
67
- ]
68
- for (const raw of samples) {
69
- const normalized = mood.normalizeEmotion(raw)
70
- assert.ok(normalized && typeof normalized === 'object', `合法输入 ${JSON.stringify(raw)} 必须归一化成一个对象`)
71
- for (const axis of AXES) {
72
- assert.ok(Number.isFinite(normalized[axis]), `归一化后的 ${axis} 必须是有限数,否则下游 valence/overlap 会传播 NaN`)
73
- assert.ok(normalized[axis] >= 0, `归一化后的 ${axis} 不得为负:负数只能来自调用方 bug,不能在这里被 clamp 成合法值`)
74
- }
75
- assert.ok(
76
- Math.abs(shareSum(normalized) - 1) < 1e-9,
77
- `构成必须落在单纯形上(误差 < 1e-9),实际 sum=${shareSum(normalized)};份额不归一,overlap 就不再有 [0,1] 的语义`,
78
- )
79
- }
80
- assert.ok(close(mood.normalizeEmotion({ joy: 3, anger: 1, sorrow: 0, fear: 0 }).joy, 0.75, 1e-12))
81
- })
82
-
83
- test('normalizeEmotion 拒绝缺键、负数、非有限数、数组和字符串,错误信息含 emotion', () => {
84
- const cases = [
85
- ['缺少 joy 键', { anger: 1, sorrow: 0, fear: 0 }],
86
- ['joy 为负数', { joy: -1, anger: 2, sorrow: 0, fear: 0 }],
87
- ['joy 为 NaN', { joy: NaN, anger: 1, sorrow: 0, fear: 0 }],
88
- ['fear 为 Infinity', { joy: 1, anger: 0, sorrow: 0, fear: Infinity }],
89
- ['传入数组', [1, 0, 0, 0]],
90
- ['传入字符串', 'joy:1'],
91
- ['轴是字符串数字', { joy: '1', anger: 0, sorrow: 0, fear: 0 }],
92
- ['传入数字', 1],
93
- ]
94
- for (const [label, input] of cases) {
95
- assert.throws(
96
- () => mood.normalizeEmotion(input),
97
- /emotion/,
98
- `${label} 必须被拒绝,且错误里要出现 emotion:静默修复会把调用方 bug 永久藏起来`,
99
- )
100
- }
101
- // null / undefined 是规范的中性表示,不是非法输入。
102
- assert.strictEqual(mood.normalizeEmotion(null), null)
103
- assert.strictEqual(mood.normalizeEmotion(undefined), null)
104
- })
105
-
106
- test('全零输入规范化为 null,不会被当成均匀分布', async () => {
107
- const zero = mood.normalizeEmotion(emptyEmotion())
108
- assert.strictEqual(zero, null, '全零代表「明确中性」;返回 {0.25,0.25,0.25,0.25} 会让召回把中性当成一种情绪')
109
- assert.notDeepEqual(zero, { joy: 0.25, anger: 0.25, sorrow: 0.25, fear: 0.25 })
110
-
111
- // 会话读数和条目写入都必须走同一个规范化,而不是各写一份。
112
- const { sessionId, exec, binding } = setup('zeros')
113
- state.setSessionMood(sessionId, { emotion: emptyEmotion(), arousal: 0.35, dominance: 0 })
114
- assert.strictEqual(state.getSessionMood(sessionId).emotion, null, 'setSessionMood 也要把全零折成 null')
115
-
116
- const { id } = await memory.privateRemember({ exec, content: 'The invoice is due on the fourteenth.', title: 'invoice', emotion: emptyEmotion() })
117
- assert.strictEqual(entryOf(binding, id).emotion, null, '写入路径同样不得把全零存成四轴均匀分布')
118
- })
119
-
120
- // ---- §8 第二条:派生 valence ----
121
-
122
- test('四个纯轴的派生 valence 方向正确且量级一致(统一系数 0.8)', () => {
123
- const pureAxis = (axis) => ({ ...emptyEmotion(), [axis]: 1 })
124
-
125
- const joyValence = mood.derivedValence(pureAxis('joy'))
126
- assert.ok(joyValence > 0, '纯喜悦必须投影为正,否则旧路径的快乐记忆会被推下去')
127
- assert.ok(close(joyValence, 0.8, 1e-12), `纯轴的系数是 0.8,实际 ${joyValence}`)
128
-
129
- for (const axis of ['anger', 'sorrow', 'fear']) {
130
- const valence = mood.derivedValence(pureAxis(axis))
131
- assert.ok(valence < 0, `纯 ${axis} 必须投影为负;愤怒不是「轻微正面」,它和悲伤一样往下拉`)
132
- assert.ok(close(Math.abs(valence), Math.abs(joyValence), 1e-12), `纯 ${axis} 的量级必须与纯 joy 一致`)
133
- }
134
-
135
- // 复合结果按 spec 的公式可复算:0.8*(joy - anger - sorrow - fear)
136
- assert.ok(close(mood.derivedValence({ joy: 0.5, anger: 0, sorrow: 0.5, fear: 0 }), 0, 1e-12), 'joy 0.5 + sorrow 0.5 必须互相抵消为 0')
137
- assert.ok(close(mood.derivedValence({ joy: 0.75, anger: 0.25, sorrow: 0, fear: 0 }), 0.4, 1e-12), '0.8*(0.75-0.25)=0.4,复合轴要能逐项复算')
138
- assert.ok(close(mood.derivedValence(mood.normalizeEmotion({ joy: 3, anger: 1, sorrow: 0, fear: 0 })), 0.4, 1e-12), '归一化后的构成同样要能复算')
139
- })
140
-
141
- // ---- §8 第三条:overlap ----
142
-
143
- test('overlap 自反、对称、落在 [0,1],不相交与 null 输入为 0', () => {
144
- const p = { joy: 0.5, anger: 0.5, sorrow: 0, fear: 0 }
145
- const q = { joy: 0.2, anger: 0.3, sorrow: 0.5, fear: 0 }
146
- const disjoint = { joy: 0, anger: 0, sorrow: 0, fear: 1 }
147
-
148
- assert.ok(close(mood.overlap(p, p), 1, 1e-12), '自反:同一份心情必须 100% 重叠,否则「心情一致」永远拿不到满权重')
149
- assert.ok(close(mood.overlap(p, q), mood.overlap(q, p), 1e-12), '对称:p 对 q 与 q 对 p 必须相等')
150
- assert.ok(close(mood.overlap(p, q), 0.5, 1e-12), 'sum(min(p,q)) = min(.5,.2)+min(.5,.3) = 0.5')
151
- assert.strictEqual(mood.overlap(p, disjoint), 0, '没有任何共同轴时不能凭空产生重叠')
152
- assert.strictEqual(mood.overlap(null, p), 0, '没有情绪的一侧应被视为「无信号」,而不是 0.5')
153
- assert.strictEqual(mood.overlap(p, null), 0)
154
- assert.strictEqual(mood.overlap(null, null), 0)
155
-
156
- for (const [left, right] of [[p, q], [p, disjoint], [q, disjoint], [p, p]]) {
157
- const value = mood.overlap(left, right)
158
- assert.ok(value >= 0 && value <= 1, `overlap 必须落在 [0,1],实际 ${value};它直接乘进 moodBias,越界就会放大成排序事故`)
159
- }
160
- })
161
-
162
- // ---- §8 第四条:bias 界 ----
163
-
164
- test('dominanceBias 始终落在 [0.9,1.1],缺失或非法值是 1 而不是 NaN', () => {
165
- for (const entryDominance of [-1, -0.7, -0.2, 0, 0.4, 1]) {
166
- for (const contextDominance of [-1, -0.3, 0, 0.6, 1]) {
167
- const bias = mood.dominanceBias(entryDominance, contextDominance)
168
- assert.ok(Number.isFinite(bias), `dominanceBias(${entryDominance}, ${contextDominance}) 不能是 NaN`)
169
- assert.ok(bias >= 0.9 && bias <= 1.1, `dominanceBias 必须被限制在 [0.9,1.1],实际 ${bias}`)
170
- }
171
- }
172
- assert.ok(close(mood.dominanceBias(0.3, 0.3), 1.1, 1e-12), '完全同向时取上界 1.1')
173
- assert.ok(close(mood.dominanceBias(-1, 1), 0.9, 1e-12), '完全反向时取下界 0.9')
174
-
175
- assert.strictEqual(mood.dominanceBias(undefined, 0.5), 1, '旧条目没有 dominance:这是「无信号」,不能参与打分')
176
- assert.strictEqual(mood.dominanceBias(null, 0.5), 1, 'spec §5.1:缺失的 dominance 必须是无信号;Number(null) === 0 会把它伪装成一个真实的「零掌控」读数')
177
- assert.strictEqual(mood.dominanceBias(0.5, NaN), 1, '非法 dominance 必须退回 1,绝不能把 NaN 乘进分数')
178
- assert.strictEqual(mood.dominanceBias(Infinity, 0), 1)
179
- })
180
-
181
- test('召回里的 moodBias 实测被限制在 [0.7,1.3]', async () => {
182
- const { sessionId, exec } = setup('mood-bias')
183
- const { id } = await memory.privateRemember({
184
- exec,
185
- content: 'The kestrel ledger was found under the ninth stair.',
186
- title: 'kestrel ledger',
187
- emotion: { joy: 0, anger: 0, sorrow: 1, fear: 0 },
188
- arousal: 0.35,
189
- // dominance 取 1、会话取 0:dominanceBias 恰好是 1(|1-0|/2 = 0.5),
190
- // 这样三次召回的分差里只剩 moodBias,不会混进 ±10% 的掌控感项。
191
- dominance: 1,
192
- })
193
-
194
- // 三次召回只改会话情绪,条目本身完全不变;因此分差只能来自 moodBias。
195
- // purpose=automatic 会跳过 touchAccess,accessCount 不会在三次之间变动。
196
- async function scoreWith(moodShape) {
197
- state.setSessionMood(sessionId, moodShape)
198
- const out = await memory.privateRecall({ exec, query: 'kestrel ledger ninth stair', topK: 1, relevanceThreshold: 0, purpose: 'automatic' })
199
- const hit = (out.files || []).find((file) => file.name === id)
200
- assert.ok(hit, '同一查询必须能召回这条记忆,否则比值无从谈起')
201
- return hit.score
202
- }
203
-
204
- const neutral = await scoreWith({ emotion: null, arousal: 0.35, dominance: 0 })
205
- const congruent = await scoreWith({ emotion: { joy: 0, anger: 0, sorrow: 1, fear: 0 }, arousal: 0.35, dominance: 0 })
206
- const incongruent = await scoreWith({ emotion: { joy: 1, anger: 0, sorrow: 0, fear: 0 }, arousal: 0.35, dominance: 0 })
207
-
208
- // 条目刚写入,fade 因子 ≈ 1,所以正向重叠应拿到 1+0.3,反向重叠应拿到 1-0.3。
209
- const up = congruent / neutral
210
- const down = incongruent / neutral
211
- assert.ok(up >= 0.7 && up <= 1.3, `moodBias 上界必须是 1.3,实测 ${up}`)
212
- assert.ok(down >= 0.7 && down <= 1.3, `moodBias 下界必须是 0.7,实测 ${down}`)
213
- assert.ok(close(up, 1.3, 0.005), `完全重叠时应接近 1.3,实测 ${up};偏差过大说明公式或衰减因子被改动`)
214
- assert.ok(close(down, 0.7, 0.005), `完全相反时应接近 0.7,实测 ${down}`)
215
- })
216
-
217
- // ---- §8 第五条:门控 ----
218
-
219
- test('emotionActive 真值表:null 只关闭第一腿,高 arousal / 高 dominance 仍开启', () => {
220
- const baseline = { emotion: null, arousal: 0.35, dominance: 0 }
221
- assert.strictEqual(mood.emotionActive(baseline), false, '全基线必须是关闭状态,否则每条记忆都带着无意义的情绪乘数')
222
- assert.strictEqual(mood.emotionActive(), false, '默认参数同样是全基线')
223
-
224
- assert.strictEqual(mood.emotionActive({ ...baseline, arousal: 0.9 }), true, 'null + 高 arousal 必须开启:紧张但没有明确好坏也是一种心情')
225
- assert.strictEqual(mood.emotionActive({ ...baseline, arousal: 0.05 }), true, '低于基线的 arousal 同样偏离休息位')
226
- assert.strictEqual(mood.emotionActive({ ...baseline, dominance: 0.3 }), true, 'null + 高 dominance 必须开启')
227
- assert.strictEqual(mood.emotionActive({ ...baseline, dominance: -0.4 }), true, '失控方向同样算高掌控感')
228
-
229
- assert.strictEqual(mood.emotionActive({ ...baseline, arousal: 0.44 }), false, 'arousal 门槛是 0.1:0.44 距基线只有 0.09')
230
- assert.strictEqual(mood.emotionActive({ ...baseline, dominance: 0.24 }), false, 'dominance 门槛是 0.25')
231
-
232
- const lowShare = { joy: 0.2, anger: 0.2, sorrow: 0.2, fear: 0.2 }
233
- assert.strictEqual(mood.emotionActive({ ...baseline, emotion: lowShare }), false, '非空但主导 share 只有 0.2,不能算明确情绪')
234
- const uniform = { joy: 0.25, anger: 0.25, sorrow: 0.25, fear: 0.25 }
235
- assert.strictEqual(mood.emotionActive({ ...baseline, emotion: uniform }), false, '归一化后的均匀构成最大 share 是 0.25,同样低于 0.35 门槛')
236
- const dominant = { joy: 0.5, anger: 0.2, sorrow: 0.2, fear: 0.1 }
237
- assert.strictEqual(mood.emotionActive({ ...baseline, emotion: dominant }), true, '主导 share 0.5 必须开启')
238
- })
239
-
240
- // ---- §8 第六条:decay 边界 ----
241
-
242
- test('moodDecay 在 fade 0.35 两侧行为稳定,清空后 arousal/dominance 仍继续衰减', () => {
243
- const boundary = timeAtFade(0.35)
244
- assert.ok(boundary !== null, '需要先构造出 Math.pow 精确等于 0.35 的时刻,才能验证「严格小于」的边界')
245
- const reading = { emotion: { joy: 0, anger: 0, sorrow: 1, fear: 0 }, arousal: 0.9, dominance: 0.8, at: 0 }
246
-
247
- const exact = mood.moodDecay(reading, boundary)
248
- assert.deepEqual(exact.emotion, reading.emotion, 'spec 写成严格小于:fade === 0.35 仍保留构成,<= 会在这里提前清空')
249
- assert.ok(close(exact.arousal, 0.35 + 0.55 * 0.35, 1e-12))
250
- assert.ok(close(exact.dominance, 0.8 * 0.35, 1e-12))
251
-
252
- const above = mood.moodDecay(reading, boundary - 60_000)
253
- assert.deepEqual(above.emotion, reading.emotion, 'fade 略大于 0.35 时必须保留构成')
254
-
255
- const below = mood.moodDecay(reading, boundary + 60_000)
256
- assert.strictEqual(below.emotion, null, 'fade 略小于 0.35 时必须清空构成')
257
- assert.ok(below.arousal > 0.35, '构成清空不等于情绪轴清空:arousal 仍要返回并高于基线')
258
- assert.ok(below.arousal < above.arousal, '清空后 arousal 仍在向基线衰减')
259
- assert.ok(below.dominance > 0 && below.dominance < above.dominance, '清空后 dominance 仍在向 0 衰减')
260
-
261
- const later = mood.moodDecay(reading, boundary + 60 * 60_000)
262
- assert.strictEqual(later.emotion, null)
263
- assert.ok(later.arousal < below.arousal, '更晚的时刻 arousal 必须更接近 0.35')
264
- assert.ok(later.dominance < below.dominance, '更晚的时刻 dominance 必须更接近 0')
265
- })
266
-
267
- // ---- §8 第七条:兼容 ----
268
-
269
- test('readMood(...).valence 继续存在,旧形状的会话 mood 可读回', () => {
270
- const read = mood.readMood('今天太开心了,测试全部通过')
271
- assert.strictEqual(typeof read.valence, 'number', 'valence 是旧召回路径直接读取的字段,不能因为新增 emotion 就消失')
272
- assert.ok(read.valence > 0, '旧路径的语义不能变:这句话仍然读成正面')
273
- assert.ok('emotion' in read && 'arousal' in read && 'dominance' in read, 'spec §4.1 的四个字段必须同时返回')
274
-
275
- const { sessionId } = setup('compat')
276
- state.setSessionMood(sessionId, { valence: -0.6, arousal: 0.8 })
277
- const stored = state.getSessionMood(sessionId)
278
- assert.strictEqual(stored.valence, -0.6, '旧形状 { valence, arousal } 必须原样可读')
279
- assert.strictEqual(stored.arousal, 0.8)
280
- assert.strictEqual(stored.emotion, null, '只有 valence 的旧读数不得反推四轴')
281
- assert.strictEqual(typeof stored.at, 'number')
282
- })
283
-
284
- // ---- §8 第八条:旧条目不反推 ----
285
-
286
- test('旧条目没有 emotion 时不反推四轴,也不产生 NaN', async () => {
287
- const { binding, exec, sessionId } = setup('legacy')
288
- const stamp = new Date().toISOString()
289
- const index = state.loadPrivateIndex(binding.libraryId)
290
- index.entries['mem-legacy-harbour'] = {
291
- track: 'memory',
292
- layer: 'long_term',
293
- title: 'Harbour bell at dusk',
294
- content: 'The harbour bell rang twice at dusk.',
295
- status: 'active',
296
- owner: 'me',
297
- visibility: 'shared',
298
- source: 'user_said',
299
- moodValence: -0.8,
300
- arousal: 0.6,
301
- peak: false,
302
- confidence: 0.8,
303
- createdAt: stamp,
304
- updatedAt: stamp,
305
- }
306
- state.savePrivateIndex(binding.libraryId, index)
307
-
308
- // 新路径在场(会话有四轴情绪),旧条目仍必须走 legacy valence 分流。
309
- state.setSessionMood(sessionId, { emotion: { joy: 0, anger: 0, sorrow: 1, fear: 0 }, arousal: 0.35, dominance: 0 })
310
- const out = await memory.privateRecall({ exec, query: 'harbour bell dusk', topK: 1, relevanceThreshold: 0, purpose: 'automatic' })
311
- const hit = (out.files || []).find((file) => file.name === 'mem-legacy-harbour')
312
- assert.ok(hit, '没有 emotion 的旧条目仍然必须能被召回')
313
- assert.ok(Number.isFinite(hit.score) && hit.score > 0, `旧条目缺 emotion/dominance 时打分不能变成 NaN,实测 ${hit.score}`)
314
- assert.strictEqual(hit.entry.emotion, undefined, 'spec §3.3:旧条目没有 emotion 就保持原样,不能从 moodValence 反推四轴')
315
- assert.strictEqual(
316
- state.loadPrivateIndex(binding.libraryId).entries['mem-legacy-harbour'].emotion,
317
- undefined,
318
- '召回本身也不能把推断出的构成写回索引',
319
- )
320
- })
321
-
322
- // ---- §8 第九条:显式 0 ----
323
-
324
- test('显式 arousal: 0 与 dominance: 0 写入后读回仍是 0', async () => {
325
- const { exec, binding, sessionId } = setup('explicit-zero')
326
- const { id } = await memory.privateRemember({
327
- exec,
328
- content: 'The gauge read zero at noon.',
329
- title: 'gauge zero',
330
- arousal: 0,
331
- dominance: 0,
332
- })
333
- const entry = entryOf(binding, id)
334
- assert.strictEqual(entry.arousal, 0, 'spec §3.2:显式 0 是读数的一部分,不能被 || 0.35 覆盖')
335
- assert.strictEqual(entry.dominance, 0, 'dominance 的显式 0 同样不能被默认值吞掉')
336
-
337
- state.setSessionMood(sessionId, { valence: 0, arousal: 0, dominance: 0 })
338
- const stored = state.getSessionMood(sessionId)
339
- assert.strictEqual(stored.arousal, 0, '会话 mood 的显式 0 也必须保留')
340
- assert.strictEqual(stored.dominance, 0)
341
- })
342
-
343
- // ---- §8 最后一条:remember -> list -> update -> recall 往返 ----
344
-
345
- test('remember 带 emotion:归一化、来源 model、moodValence 等于派生值', async () => {
346
- const { exec, binding } = setup('remember')
347
- const { id } = await memory.privateRemember({
348
- exec,
349
- content: 'We argued about the broken deploy until midnight.',
350
- title: 'midnight deploy',
351
- emotion: { joy: 0.5, anger: 0.5, sorrow: 0, fear: 0 },
352
- })
353
- const entry = entryOf(binding, id)
354
- assert.ok(close(shareSum(entry.emotion), 1, 1e-9), '存下来的构成必须已经归一化,读回时不能再做一次隐式缩放')
355
- assert.ok(close(entry.emotion.joy, 0.5, 1e-12) && close(entry.emotion.anger, 0.5, 1e-12), '0.5/0.5 归一化后仍是 0.5/0.5')
356
- assert.strictEqual(entry.emotionSource, 'model', '工具参数显式给了 emotion,来源必须是 model,否则审计分不清事实与推断')
357
- assert.ok(close(entry.moodValence, mood.derivedValence(entry.emotion), 1e-12), 'moodValence 是构成的兼容投影,必须与 derivedValence 一致')
358
-
359
- // 0 == 0 的断言是空洞的,再写一条非零投影的。
360
- const second = await memory.privateRemember({
361
- exec,
362
- content: 'The release finally shipped clean.',
363
- title: 'clean release',
364
- emotion: { joy: 0.75, anger: 0.25, sorrow: 0, fear: 0 },
365
- })
366
- assert.ok(close(entryOf(binding, second.id).moodValence, 0.4, 1e-12), '0.8*(0.75-0.25)=0.4')
367
- })
368
-
369
- test('update 三态:省略保留、null 清空并标 unknown、新对象整份替换', async () => {
370
- const { exec, binding } = setup('update')
371
- const { id } = await memory.privateRemember({
372
- exec,
373
- content: 'The roof leaked through the night.',
374
- title: 'roof leak',
375
- emotion: { joy: 0, anger: 0, sorrow: 0.75, fear: 0.25 },
376
- })
377
- const before = entryOf(binding, id).emotion
378
-
379
- await memory.privateUpdate({ exec, id, title: 'roof leak (edited)' })
380
- assert.deepEqual(entryOf(binding, id).emotion, before, '省略 emotion 表示保留:只改标题不能把情绪弄丢')
381
-
382
- await memory.privateUpdate({ exec, id, emotion: null })
383
- const cleared = entryOf(binding, id)
384
- assert.strictEqual(cleared.emotion, null, '显式 null 表示清除构成')
385
- assert.strictEqual(cleared.emotionSource, 'unknown', '清除后来源必须是 unknown,不能再声称是 model 读到的')
386
-
387
- await memory.privateUpdate({ exec, id, emotion: { joy: 1, anger: 0, sorrow: 0, fear: 0 } })
388
- const replaced = entryOf(binding, id)
389
- assert.deepEqual(replaced.emotion, { joy: 1, anger: 0, sorrow: 0, fear: 0 }, '新对象必须整份替换旧构成,不能按轴合并')
390
- assert.strictEqual(replaced.emotionSource, 'model')
391
- assert.ok(close(replaced.moodValence, 0.8, 1e-12), '替换后兼容投影要跟着重算')
392
- })
393
-
394
- test('update 的非法 emotion 必须 reject,错误含 emotion,且不留下半截修改', async () => {
395
- const { exec, binding } = setup('update-bad')
396
- const { id } = await memory.privateRemember({
397
- exec,
398
- content: 'The invoice arrived twice.',
399
- title: 'invoice twice',
400
- emotion: { joy: 0, anger: 0, sorrow: 1, fear: 0 },
401
- })
402
- const before = entryOf(binding, id).emotion
403
-
404
- await assert.rejects(
405
- () => memory.privateUpdate({ exec, id, emotion: { joy: 1, anger: -1, sorrow: 0, fear: 0 } }),
406
- /emotion/,
407
- 'spec §3.1:负数份额只能拒绝,不能 clamp 成合法值;错误信息必须点明 emotion',
408
- )
409
- assert.deepEqual(entryOf(binding, id).emotion, before, '被拒绝的更新不能改动已存构成')
410
- })
411
-
412
- test('about_project 的条目在没有显式 emotion 时保持中性', async () => {
413
- const { exec, binding, sessionId } = setup('project')
414
- // 会话此刻很兴奋:工程事实仍然不能被染色。
415
- state.setSessionMood(sessionId, { emotion: { joy: 1, anger: 0, sorrow: 0, fear: 0 }, arousal: 0.9, dominance: 0.5 })
416
-
417
- const { id } = await memory.privateRemember({
418
- exec,
419
- content: 'Node 22 的构建终于通过了,我特别开心。',
420
- title: 'build green on Node 22',
421
- owner: 'about_project',
422
- })
423
- const entry = entryOf(binding, id)
424
- assert.strictEqual(entry.emotion, null, 'spec §6.1/§6.3:工程事实固定中性;bug 难修不是项目在难过')
425
- assert.strictEqual(entry.emotionSource, 'unknown', '没有证据就必须标 unknown,不能借会话心情冒充推断')
426
- assert.strictEqual(entry.dominance, 0, '工程事实不继承会话的掌控感')
427
- assert.ok(Number.isFinite(entry.moodValence), '中性也要给出可用的兼容投影,而不是 NaN')
428
- })
429
-
430
- test('remember 的显式 emotion: null 表示明确中性(spec §6.1),不继承会话心情', async () => {
431
- const { exec, binding, sessionId } = setup('remember-null')
432
- state.setSessionMood(sessionId, { emotion: { joy: 1, anger: 0, sorrow: 0, fear: 0 }, arousal: 0.9, dominance: 0.4 })
433
-
434
- const { id } = await memory.privateRemember({
435
- exec,
436
- content: 'The build passed.',
437
- title: 'build passed',
438
- emotion: null,
439
- })
440
- assert.strictEqual(
441
- entryOf(binding, id).emotion,
442
- null,
443
- 'spec §6.1:省略=继承、null=明确中性。把 null 当成省略会把整理时的心情写进事件本身',
444
- )
445
- })
1
+ // 独立验收:按 docs/emotion-model-spec.md v3 §8 写的一组黑盒契约测试。
2
+ //
3
+ // 这些断言只走公开入口(lib/mood.js 的纯函数、lib/state.js 的会话 mood、
4
+ // lib/memory.js 的写入/更新/召回),不读实现私有字段,也不为了迁就现有行为
5
+ // 而放宽期望:实现与 spec 不一致时,这里就应该失败。
6
+ import test from 'node:test'
7
+ import assert from 'node:assert/strict'
8
+ import fs from 'node:fs'
9
+ import os from 'node:os'
10
+ import path from 'node:path'
11
+
12
+ const home = fs.mkdtempSync(path.join(os.tmpdir(), 'dsh-memu-memory-emotion-contract-'))
13
+ process.env.DSH_MEMU_HOME = home
14
+
15
+ const state = await import('../lib/state.js')
16
+ const memory = await import('../lib/memory.js')
17
+ const mood = await import('../lib/mood.js')
18
+
19
+ test.after(() => {
20
+ try { fs.rmSync(home, { recursive: true, force: true }) } catch {}
21
+ })
22
+
23
+ const AXES = ['joy', 'anger', 'sorrow', 'fear']
24
+ const shareSum = (emotion) => AXES.reduce((total, axis) => total + Number(emotion[axis]), 0)
25
+ const close = (a, b, epsilon = 1e-9) => Math.abs(a - b) < epsilon
26
+ const emptyEmotion = () => ({ joy: 0, anger: 0, sorrow: 0, fear: 0 })
27
+
28
+ function execFor(sessionId, cwd) {
29
+ return { agent: { session: { header: { id: sessionId, cwd, agentPreset: 'coding' } } } }
30
+ }
31
+
32
+ function setup(name) {
33
+ const cwd = path.join(home, 'proj-' + name)
34
+ const sessionId = 'emotion-' + name
35
+ const binding = state.ensureSession(sessionId, { cwd, agentPreset: 'coding' })
36
+ return { cwd, sessionId, binding, exec: execFor(sessionId, cwd) }
37
+ }
38
+
39
+ function entryOf(binding, id) {
40
+ return state.loadPrivateIndex(binding.libraryId).entries[id]
41
+ }
42
+
43
+ /**
44
+ * 反解一个让 fade 正好等于 target 的时刻。
45
+ *
46
+ * fade = 0.5 ** (minutes / 90),所以先求出实数解,再在相邻 double 上搜一个
47
+ * 让 Math.pow 的结果精确落在 target 上的取值——「fade === 0.35 仍保留」这条
48
+ * 边界只有拿到精确相等才验证得了。
49
+ */
50
+ function timeAtFade(target) {
51
+ const base = (Math.log(target) / Math.log(0.5)) * 90 * 60_000
52
+ for (let i = -20_000; i <= 20_000; i += 1) {
53
+ const now = base + i * Number.EPSILON * base
54
+ if (Math.pow(0.5, (now / 60_000) / 90) === target) return now
55
+ }
56
+ return null
57
+ }
58
+
59
+ // ---- §8 第一条:归一化 ----
60
+
61
+ test('normalizeEmotion 归一后和为 1,所有分量非负', () => {
62
+ const samples = [
63
+ { joy: 3, anger: 1, sorrow: 0, fear: 0 },
64
+ { joy: 0.1, anger: 0.2, sorrow: 0.3, fear: 0.4 },
65
+ { joy: 0.0001, anger: 0.0002, sorrow: 0.0003, fear: 0.0004 },
66
+ { joy: 1_000_000, anger: 1, sorrow: 1, fear: 1 },
67
+ ]
68
+ for (const raw of samples) {
69
+ const normalized = mood.normalizeEmotion(raw)
70
+ assert.ok(normalized && typeof normalized === 'object', `合法输入 ${JSON.stringify(raw)} 必须归一化成一个对象`)
71
+ for (const axis of AXES) {
72
+ assert.ok(Number.isFinite(normalized[axis]), `归一化后的 ${axis} 必须是有限数,否则下游 valence/overlap 会传播 NaN`)
73
+ assert.ok(normalized[axis] >= 0, `归一化后的 ${axis} 不得为负:负数只能来自调用方 bug,不能在这里被 clamp 成合法值`)
74
+ }
75
+ assert.ok(
76
+ Math.abs(shareSum(normalized) - 1) < 1e-9,
77
+ `构成必须落在单纯形上(误差 < 1e-9),实际 sum=${shareSum(normalized)};份额不归一,overlap 就不再有 [0,1] 的语义`,
78
+ )
79
+ }
80
+ assert.ok(close(mood.normalizeEmotion({ joy: 3, anger: 1, sorrow: 0, fear: 0 }).joy, 0.75, 1e-12))
81
+ })
82
+
83
+ test('normalizeEmotion 拒绝缺键、负数、非有限数、数组和字符串,错误信息含 emotion', () => {
84
+ const cases = [
85
+ ['缺少 joy 键', { anger: 1, sorrow: 0, fear: 0 }],
86
+ ['joy 为负数', { joy: -1, anger: 2, sorrow: 0, fear: 0 }],
87
+ ['joy 为 NaN', { joy: NaN, anger: 1, sorrow: 0, fear: 0 }],
88
+ ['fear 为 Infinity', { joy: 1, anger: 0, sorrow: 0, fear: Infinity }],
89
+ ['传入数组', [1, 0, 0, 0]],
90
+ ['传入字符串', 'joy:1'],
91
+ ['轴是字符串数字', { joy: '1', anger: 0, sorrow: 0, fear: 0 }],
92
+ ['传入数字', 1],
93
+ ]
94
+ for (const [label, input] of cases) {
95
+ assert.throws(
96
+ () => mood.normalizeEmotion(input),
97
+ /emotion/,
98
+ `${label} 必须被拒绝,且错误里要出现 emotion:静默修复会把调用方 bug 永久藏起来`,
99
+ )
100
+ }
101
+ // null / undefined 是规范的中性表示,不是非法输入。
102
+ assert.strictEqual(mood.normalizeEmotion(null), null)
103
+ assert.strictEqual(mood.normalizeEmotion(undefined), null)
104
+ })
105
+
106
+ test('全零输入规范化为 null,不会被当成均匀分布', async () => {
107
+ const zero = mood.normalizeEmotion(emptyEmotion())
108
+ assert.strictEqual(zero, null, '全零代表「明确中性」;返回 {0.25,0.25,0.25,0.25} 会让召回把中性当成一种情绪')
109
+ assert.notDeepEqual(zero, { joy: 0.25, anger: 0.25, sorrow: 0.25, fear: 0.25 })
110
+
111
+ // 会话读数和条目写入都必须走同一个规范化,而不是各写一份。
112
+ const { sessionId, exec, binding } = setup('zeros')
113
+ state.setSessionMood(sessionId, { emotion: emptyEmotion(), arousal: 0.35, dominance: 0 })
114
+ assert.strictEqual(state.getSessionMood(sessionId).emotion, null, 'setSessionMood 也要把全零折成 null')
115
+
116
+ const { id } = await memory.privateRemember({ exec, content: 'The invoice is due on the fourteenth.', title: 'invoice', emotion: emptyEmotion() })
117
+ assert.strictEqual(entryOf(binding, id).emotion, null, '写入路径同样不得把全零存成四轴均匀分布')
118
+ })
119
+
120
+ // ---- §8 第二条:派生 valence ----
121
+
122
+ test('四个纯轴的派生 valence 方向正确且量级一致(统一系数 0.8)', () => {
123
+ const pureAxis = (axis) => ({ ...emptyEmotion(), [axis]: 1 })
124
+
125
+ const joyValence = mood.derivedValence(pureAxis('joy'))
126
+ assert.ok(joyValence > 0, '纯喜悦必须投影为正,否则旧路径的快乐记忆会被推下去')
127
+ assert.ok(close(joyValence, 0.8, 1e-12), `纯轴的系数是 0.8,实际 ${joyValence}`)
128
+
129
+ for (const axis of ['anger', 'sorrow', 'fear']) {
130
+ const valence = mood.derivedValence(pureAxis(axis))
131
+ assert.ok(valence < 0, `纯 ${axis} 必须投影为负;愤怒不是「轻微正面」,它和悲伤一样往下拉`)
132
+ assert.ok(close(Math.abs(valence), Math.abs(joyValence), 1e-12), `纯 ${axis} 的量级必须与纯 joy 一致`)
133
+ }
134
+
135
+ // 复合结果按 spec 的公式可复算:0.8*(joy - anger - sorrow - fear)
136
+ assert.ok(close(mood.derivedValence({ joy: 0.5, anger: 0, sorrow: 0.5, fear: 0 }), 0, 1e-12), 'joy 0.5 + sorrow 0.5 必须互相抵消为 0')
137
+ assert.ok(close(mood.derivedValence({ joy: 0.75, anger: 0.25, sorrow: 0, fear: 0 }), 0.4, 1e-12), '0.8*(0.75-0.25)=0.4,复合轴要能逐项复算')
138
+ assert.ok(close(mood.derivedValence(mood.normalizeEmotion({ joy: 3, anger: 1, sorrow: 0, fear: 0 })), 0.4, 1e-12), '归一化后的构成同样要能复算')
139
+ })
140
+
141
+ // ---- §8 第三条:overlap ----
142
+
143
+ test('overlap 自反、对称、落在 [0,1],不相交与 null 输入为 0', () => {
144
+ const p = { joy: 0.5, anger: 0.5, sorrow: 0, fear: 0 }
145
+ const q = { joy: 0.2, anger: 0.3, sorrow: 0.5, fear: 0 }
146
+ const disjoint = { joy: 0, anger: 0, sorrow: 0, fear: 1 }
147
+
148
+ assert.ok(close(mood.overlap(p, p), 1, 1e-12), '自反:同一份心情必须 100% 重叠,否则「心情一致」永远拿不到满权重')
149
+ assert.ok(close(mood.overlap(p, q), mood.overlap(q, p), 1e-12), '对称:p 对 q 与 q 对 p 必须相等')
150
+ assert.ok(close(mood.overlap(p, q), 0.5, 1e-12), 'sum(min(p,q)) = min(.5,.2)+min(.5,.3) = 0.5')
151
+ assert.strictEqual(mood.overlap(p, disjoint), 0, '没有任何共同轴时不能凭空产生重叠')
152
+ assert.strictEqual(mood.overlap(null, p), 0, '没有情绪的一侧应被视为「无信号」,而不是 0.5')
153
+ assert.strictEqual(mood.overlap(p, null), 0)
154
+ assert.strictEqual(mood.overlap(null, null), 0)
155
+
156
+ for (const [left, right] of [[p, q], [p, disjoint], [q, disjoint], [p, p]]) {
157
+ const value = mood.overlap(left, right)
158
+ assert.ok(value >= 0 && value <= 1, `overlap 必须落在 [0,1],实际 ${value};它直接乘进 moodBias,越界就会放大成排序事故`)
159
+ }
160
+ })
161
+
162
+ // ---- §8 第四条:bias 界 ----
163
+
164
+ test('dominanceBias 始终落在 [0.9,1.1],缺失或非法值是 1 而不是 NaN', () => {
165
+ for (const entryDominance of [-1, -0.7, -0.2, 0, 0.4, 1]) {
166
+ for (const contextDominance of [-1, -0.3, 0, 0.6, 1]) {
167
+ const bias = mood.dominanceBias(entryDominance, contextDominance)
168
+ assert.ok(Number.isFinite(bias), `dominanceBias(${entryDominance}, ${contextDominance}) 不能是 NaN`)
169
+ assert.ok(bias >= 0.9 && bias <= 1.1, `dominanceBias 必须被限制在 [0.9,1.1],实际 ${bias}`)
170
+ }
171
+ }
172
+ assert.ok(close(mood.dominanceBias(0.3, 0.3), 1.1, 1e-12), '完全同向时取上界 1.1')
173
+ assert.ok(close(mood.dominanceBias(-1, 1), 0.9, 1e-12), '完全反向时取下界 0.9')
174
+
175
+ assert.strictEqual(mood.dominanceBias(undefined, 0.5), 1, '旧条目没有 dominance:这是「无信号」,不能参与打分')
176
+ assert.strictEqual(mood.dominanceBias(null, 0.5), 1, 'spec §5.1:缺失的 dominance 必须是无信号;Number(null) === 0 会把它伪装成一个真实的「零掌控」读数')
177
+ assert.strictEqual(mood.dominanceBias(0.5, NaN), 1, '非法 dominance 必须退回 1,绝不能把 NaN 乘进分数')
178
+ assert.strictEqual(mood.dominanceBias(Infinity, 0), 1)
179
+ })
180
+
181
+ test('召回里的 moodBias 被限制在 [0.7,1.3]', async () => {
182
+ const { sessionId, exec } = setup('mood-bias')
183
+ const { id } = await memory.privateRemember({
184
+ exec,
185
+ content: 'The kestrel ledger was found under the ninth stair.',
186
+ title: 'kestrel ledger',
187
+ emotion: { joy: 0, anger: 0, sorrow: 1, fear: 0 },
188
+ arousal: 0.35,
189
+ // dominance 取 1、会话取 0:dominanceBias 恰好是 1(|1-0|/2 = 0.5),
190
+ // 这样三次召回的分差里只剩 moodBias,不会混进 ±10% 的掌控感项。
191
+ dominance: 1,
192
+ })
193
+
194
+ // 三次召回只改会话情绪,条目本身完全不变;因此分差只能来自 moodBias。
195
+ // purpose=automatic 会跳过 touchAccess,accessCount 不会在三次之间变动。
196
+ async function scoreWith(moodShape) {
197
+ state.setSessionMood(sessionId, moodShape)
198
+ const out = await memory.privateRecall({ exec, query: 'kestrel ledger ninth stair', topK: 1, relevanceThreshold: 0, purpose: 'automatic' })
199
+ const hit = (out.files || []).find((file) => file.name === id)
200
+ assert.ok(hit, '同一查询必须能召回这条记忆,否则比值无从谈起')
201
+ return hit.score
202
+ }
203
+
204
+ const neutral = await scoreWith({ emotion: null, arousal: 0.35, dominance: 0 })
205
+ const congruent = await scoreWith({ emotion: { joy: 0, anger: 0, sorrow: 1, fear: 0 }, arousal: 0.35, dominance: 0 })
206
+ const incongruent = await scoreWith({ emotion: { joy: 1, anger: 0, sorrow: 0, fear: 0 }, arousal: 0.35, dominance: 0 })
207
+
208
+ // 条目刚写入,fade 因子 ≈ 1,所以正向重叠应拿到 1+0.3,反向重叠应拿到 1-0.3。
209
+ const up = congruent / neutral
210
+ const down = incongruent / neutral
211
+ assert.ok(up >= 0.7 && up <= 1.3, `moodBias 上界必须是 1.3,实测 ${up}`)
212
+ assert.ok(down >= 0.7 && down <= 1.3, `moodBias 下界必须是 0.7,实测 ${down}`)
213
+ assert.ok(close(up, 1.3, 0.005), `完全重叠时应接近 1.3,实测 ${up};偏差过大说明公式或衰减因子被改动`)
214
+ assert.ok(close(down, 0.7, 0.005), `完全相反时应接近 0.7,实测 ${down}`)
215
+ })
216
+
217
+ // ---- §8 第五条:门控 ----
218
+
219
+ test('emotionActive 真值表:null 只关闭第一腿,高 arousal / 高 dominance 仍开启', () => {
220
+ const baseline = { emotion: null, arousal: 0.35, dominance: 0 }
221
+ assert.strictEqual(mood.emotionActive(baseline), false, '全基线必须是关闭状态,否则每条记忆都带着无意义的情绪乘数')
222
+ assert.strictEqual(mood.emotionActive(), false, '默认参数同样是全基线')
223
+
224
+ assert.strictEqual(mood.emotionActive({ ...baseline, arousal: 0.9 }), true, 'null + 高 arousal 必须开启:紧张但没有明确好坏也是一种心情')
225
+ assert.strictEqual(mood.emotionActive({ ...baseline, arousal: 0.05 }), true, '低于基线的 arousal 同样偏离休息位')
226
+ assert.strictEqual(mood.emotionActive({ ...baseline, dominance: 0.3 }), true, 'null + 高 dominance 必须开启')
227
+ assert.strictEqual(mood.emotionActive({ ...baseline, dominance: -0.4 }), true, '失控方向同样算高掌控感')
228
+
229
+ assert.strictEqual(mood.emotionActive({ ...baseline, arousal: 0.44 }), false, 'arousal 门槛是 0.1:0.44 距基线只有 0.09')
230
+ assert.strictEqual(mood.emotionActive({ ...baseline, dominance: 0.24 }), false, 'dominance 门槛是 0.25')
231
+
232
+ const lowShare = { joy: 0.2, anger: 0.2, sorrow: 0.2, fear: 0.2 }
233
+ assert.strictEqual(mood.emotionActive({ ...baseline, emotion: lowShare }), false, '非空但主导 share 只有 0.2,不能算明确情绪')
234
+ const uniform = { joy: 0.25, anger: 0.25, sorrow: 0.25, fear: 0.25 }
235
+ assert.strictEqual(mood.emotionActive({ ...baseline, emotion: uniform }), false, '归一化后的均匀构成最大 share 是 0.25,同样低于 0.35 门槛')
236
+ const dominant = { joy: 0.5, anger: 0.2, sorrow: 0.2, fear: 0.1 }
237
+ assert.strictEqual(mood.emotionActive({ ...baseline, emotion: dominant }), true, '主导 share 0.5 必须开启')
238
+ })
239
+
240
+ // ---- §8 第六条:decay 边界 ----
241
+
242
+ test('moodDecay 在 fade 0.35 两侧行为稳定,清空后 arousal/dominance 仍继续衰减', () => {
243
+ const boundary = timeAtFade(0.35)
244
+ assert.ok(boundary !== null, '需要先构造出 Math.pow 精确等于 0.35 的时刻,才能验证「严格小于」的边界')
245
+ const reading = { emotion: { joy: 0, anger: 0, sorrow: 1, fear: 0 }, arousal: 0.9, dominance: 0.8, at: 0 }
246
+
247
+ const exact = mood.moodDecay(reading, boundary)
248
+ assert.deepEqual(exact.emotion, reading.emotion, 'spec 写成严格小于:fade === 0.35 仍保留构成,<= 会在这里提前清空')
249
+ assert.ok(close(exact.arousal, 0.35 + 0.55 * 0.35, 1e-12))
250
+ assert.ok(close(exact.dominance, 0.8 * 0.35, 1e-12))
251
+
252
+ const above = mood.moodDecay(reading, boundary - 60_000)
253
+ assert.deepEqual(above.emotion, reading.emotion, 'fade 略大于 0.35 时必须保留构成')
254
+
255
+ const below = mood.moodDecay(reading, boundary + 60_000)
256
+ assert.strictEqual(below.emotion, null, 'fade 略小于 0.35 时必须清空构成')
257
+ assert.ok(below.arousal > 0.35, '构成清空不等于情绪轴清空:arousal 仍要返回并高于基线')
258
+ assert.ok(below.arousal < above.arousal, '清空后 arousal 仍在向基线衰减')
259
+ assert.ok(below.dominance > 0 && below.dominance < above.dominance, '清空后 dominance 仍在向 0 衰减')
260
+
261
+ const later = mood.moodDecay(reading, boundary + 60 * 60_000)
262
+ assert.strictEqual(later.emotion, null)
263
+ assert.ok(later.arousal < below.arousal, '更晚的时刻 arousal 必须更接近 0.35')
264
+ assert.ok(later.dominance < below.dominance, '更晚的时刻 dominance 必须更接近 0')
265
+ })
266
+
267
+ // ---- §8 第七条:兼容 ----
268
+
269
+ test('readMood(...).valence 继续存在,旧形状的会话 mood 可读回', () => {
270
+ const read = mood.readMood('今天太开心了,测试全部通过')
271
+ assert.strictEqual(typeof read.valence, 'number', 'valence 是旧召回路径直接读取的字段,不能因为新增 emotion 就消失')
272
+ assert.ok(read.valence > 0, '旧路径的语义不能变:这句话仍然读成正面')
273
+ assert.ok('emotion' in read && 'arousal' in read && 'dominance' in read, 'spec §4.1 的四个字段必须同时返回')
274
+
275
+ const { sessionId } = setup('compat')
276
+ state.setSessionMood(sessionId, { valence: -0.6, arousal: 0.8 })
277
+ const stored = state.getSessionMood(sessionId)
278
+ assert.strictEqual(stored.valence, -0.6, '旧形状 { valence, arousal } 必须原样可读')
279
+ assert.strictEqual(stored.arousal, 0.8)
280
+ assert.strictEqual(stored.emotion, null, '只有 valence 的旧读数不得反推四轴')
281
+ assert.strictEqual(typeof stored.at, 'number')
282
+ })
283
+
284
+ // ---- §8 第八条:旧条目不反推 ----
285
+
286
+ test('旧条目没有 emotion 时不反推四轴,也不产生 NaN', async () => {
287
+ const { binding, exec, sessionId } = setup('legacy')
288
+ const stamp = new Date().toISOString()
289
+ const index = state.loadPrivateIndex(binding.libraryId)
290
+ index.entries['mem-legacy-harbour'] = {
291
+ track: 'memory',
292
+ layer: 'long_term',
293
+ title: 'Harbour bell at dusk',
294
+ content: 'The harbour bell rang twice at dusk.',
295
+ status: 'active',
296
+ owner: 'me',
297
+ visibility: 'shared',
298
+ source: 'user_said',
299
+ moodValence: -0.8,
300
+ arousal: 0.6,
301
+ peak: false,
302
+ confidence: 0.8,
303
+ createdAt: stamp,
304
+ updatedAt: stamp,
305
+ }
306
+ state.savePrivateIndex(binding.libraryId, index)
307
+
308
+ // 新路径在场(会话有四轴情绪),旧条目仍必须走 legacy valence 分流。
309
+ state.setSessionMood(sessionId, { emotion: { joy: 0, anger: 0, sorrow: 1, fear: 0 }, arousal: 0.35, dominance: 0 })
310
+ const out = await memory.privateRecall({ exec, query: 'harbour bell dusk', topK: 1, relevanceThreshold: 0, purpose: 'automatic' })
311
+ const hit = (out.files || []).find((file) => file.name === 'mem-legacy-harbour')
312
+ assert.ok(hit, '没有 emotion 的旧条目仍然必须能被召回')
313
+ assert.ok(Number.isFinite(hit.score) && hit.score > 0, `旧条目缺 emotion/dominance 时打分不能变成 NaN,实测 ${hit.score}`)
314
+ assert.strictEqual(hit.entry.emotion, undefined, 'spec §3.3:旧条目没有 emotion 就保持原样,不能从 moodValence 反推四轴')
315
+ assert.strictEqual(
316
+ state.loadPrivateIndex(binding.libraryId).entries['mem-legacy-harbour'].emotion,
317
+ undefined,
318
+ '召回本身也不能把推断出的构成写回索引',
319
+ )
320
+ })
321
+
322
+ // ---- §8 第九条:显式 0 ----
323
+
324
+ test('显式 arousal: 0 与 dominance: 0 写入后读回仍是 0', async () => {
325
+ const { exec, binding, sessionId } = setup('explicit-zero')
326
+ const { id } = await memory.privateRemember({
327
+ exec,
328
+ content: 'The gauge read zero at noon.',
329
+ title: 'gauge zero',
330
+ arousal: 0,
331
+ dominance: 0,
332
+ })
333
+ const entry = entryOf(binding, id)
334
+ assert.strictEqual(entry.arousal, 0, 'spec §3.2:显式 0 是读数的一部分,不能被 || 0.35 覆盖')
335
+ assert.strictEqual(entry.dominance, 0, 'dominance 的显式 0 同样不能被默认值吞掉')
336
+
337
+ state.setSessionMood(sessionId, { valence: 0, arousal: 0, dominance: 0 })
338
+ const stored = state.getSessionMood(sessionId)
339
+ assert.strictEqual(stored.arousal, 0, '会话 mood 的显式 0 也必须保留')
340
+ assert.strictEqual(stored.dominance, 0)
341
+ })
342
+
343
+ // ---- §8 最后一条:remember -> list -> update -> recall 往返 ----
344
+
345
+ test('remember 带 emotion:归一化、来源 model、moodValence 等于派生值', async () => {
346
+ const { exec, binding } = setup('remember')
347
+ const { id } = await memory.privateRemember({
348
+ exec,
349
+ content: 'We argued about the broken deploy until midnight.',
350
+ title: 'midnight deploy',
351
+ emotion: { joy: 0.5, anger: 0.5, sorrow: 0, fear: 0 },
352
+ })
353
+ const entry = entryOf(binding, id)
354
+ assert.ok(close(shareSum(entry.emotion), 1, 1e-9), '存下来的构成必须已经归一化,读回时不能再做一次隐式缩放')
355
+ assert.ok(close(entry.emotion.joy, 0.5, 1e-12) && close(entry.emotion.anger, 0.5, 1e-12), '0.5/0.5 归一化后仍是 0.5/0.5')
356
+ assert.strictEqual(entry.emotionSource, 'model', '工具参数显式给了 emotion,来源必须是 model,否则审计分不清事实与推断')
357
+ assert.ok(close(entry.moodValence, mood.derivedValence(entry.emotion), 1e-12), 'moodValence 是构成的兼容投影,必须与 derivedValence 一致')
358
+
359
+ // 0 == 0 的断言是空洞的,再写一条非零投影的。
360
+ const second = await memory.privateRemember({
361
+ exec,
362
+ content: 'The release finally shipped clean.',
363
+ title: 'clean release',
364
+ emotion: { joy: 0.75, anger: 0.25, sorrow: 0, fear: 0 },
365
+ })
366
+ assert.ok(close(entryOf(binding, second.id).moodValence, 0.4, 1e-12), '0.8*(0.75-0.25)=0.4')
367
+ })
368
+
369
+ test('update 三态:省略保留、null 清空并标 unknown、新对象整份替换', async () => {
370
+ const { exec, binding } = setup('update')
371
+ const { id } = await memory.privateRemember({
372
+ exec,
373
+ content: 'The roof leaked through the night.',
374
+ title: 'roof leak',
375
+ emotion: { joy: 0, anger: 0, sorrow: 0.75, fear: 0.25 },
376
+ })
377
+ const before = entryOf(binding, id).emotion
378
+
379
+ await memory.privateUpdate({ exec, id, title: 'roof leak (edited)' })
380
+ assert.deepEqual(entryOf(binding, id).emotion, before, '省略 emotion 表示保留:只改标题不能把情绪弄丢')
381
+
382
+ await memory.privateUpdate({ exec, id, emotion: null })
383
+ const cleared = entryOf(binding, id)
384
+ assert.strictEqual(cleared.emotion, null, '显式 null 表示清除构成')
385
+ assert.strictEqual(cleared.emotionSource, 'unknown', '清除后来源必须是 unknown,不能再声称是 model 读到的')
386
+
387
+ await memory.privateUpdate({ exec, id, emotion: { joy: 1, anger: 0, sorrow: 0, fear: 0 } })
388
+ const replaced = entryOf(binding, id)
389
+ assert.deepEqual(replaced.emotion, { joy: 1, anger: 0, sorrow: 0, fear: 0 }, '新对象必须整份替换旧构成,不能按轴合并')
390
+ assert.strictEqual(replaced.emotionSource, 'model')
391
+ assert.ok(close(replaced.moodValence, 0.8, 1e-12), '替换后兼容投影要跟着重算')
392
+ })
393
+
394
+ test('update 的非法 emotion 必须 reject,错误含 emotion,且不留下半截修改', async () => {
395
+ const { exec, binding } = setup('update-bad')
396
+ const { id } = await memory.privateRemember({
397
+ exec,
398
+ content: 'The invoice arrived twice.',
399
+ title: 'invoice twice',
400
+ emotion: { joy: 0, anger: 0, sorrow: 1, fear: 0 },
401
+ })
402
+ const before = entryOf(binding, id).emotion
403
+
404
+ await assert.rejects(
405
+ () => memory.privateUpdate({ exec, id, emotion: { joy: 1, anger: -1, sorrow: 0, fear: 0 } }),
406
+ /emotion/,
407
+ 'spec §3.1:负数份额只能拒绝,不能 clamp 成合法值;错误信息必须点明 emotion',
408
+ )
409
+ assert.deepEqual(entryOf(binding, id).emotion, before, '被拒绝的更新不能改动已存构成')
410
+ })
411
+
412
+ test('about_project 的条目在没有显式 emotion 时保持中性', async () => {
413
+ const { exec, binding, sessionId } = setup('project')
414
+ // 会话此刻很兴奋:工程事实仍然不能被染色。
415
+ state.setSessionMood(sessionId, { emotion: { joy: 1, anger: 0, sorrow: 0, fear: 0 }, arousal: 0.9, dominance: 0.5 })
416
+
417
+ const { id } = await memory.privateRemember({
418
+ exec,
419
+ content: 'Node 22 的构建终于通过了,我特别开心。',
420
+ title: 'build green on Node 22',
421
+ owner: 'about_project',
422
+ })
423
+ const entry = entryOf(binding, id)
424
+ assert.strictEqual(entry.emotion, null, 'spec §6.1/§6.3:工程事实固定中性;bug 难修不是项目在难过')
425
+ assert.strictEqual(entry.emotionSource, 'unknown', '没有证据就必须标 unknown,不能借会话心情冒充推断')
426
+ assert.strictEqual(entry.dominance, 0, '工程事实不继承会话的掌控感')
427
+ assert.ok(Number.isFinite(entry.moodValence), '中性也要给出可用的兼容投影,而不是 NaN')
428
+ })
429
+
430
+ test('remember 的显式 emotion: null 表示明确中性(spec §6.1),不继承会话心情', async () => {
431
+ const { exec, binding, sessionId } = setup('remember-null')
432
+ state.setSessionMood(sessionId, { emotion: { joy: 1, anger: 0, sorrow: 0, fear: 0 }, arousal: 0.9, dominance: 0.4 })
433
+
434
+ const { id } = await memory.privateRemember({
435
+ exec,
436
+ content: 'The build passed.',
437
+ title: 'build passed',
438
+ emotion: null,
439
+ })
440
+ assert.strictEqual(
441
+ entryOf(binding, id).emotion,
442
+ null,
443
+ 'spec §6.1:省略=继承、null=明确中性。把 null 当成省略会把整理时的心情写进事件本身',
444
+ )
445
+ })