@modusensus/dsh-mneme 0.8.13 → 0.8.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (271) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +655 -626
  3. package/assets/icon.svg +3 -3
  4. package/bin/cli.mjs +689 -689
  5. package/bin/dsh-mneme-mcp.mjs +510 -510
  6. package/bin/dsh-mneme-serve.mjs +151 -0
  7. package/cordis.patch.yml +21 -21
  8. package/docs/CONFIGURATION.md +263 -0
  9. package/docs/CONTINUITY.md +124 -0
  10. package/docs/DAEMON.md +46 -0
  11. package/docs/ENTITIES.md +245 -0
  12. package/docs/LOCAL_MODEL.md +222 -0
  13. package/docs/SEMANTIC.md +256 -0
  14. package/docs/SLEEP.md +164 -0
  15. package/docs/STORAGE.md +89 -0
  16. package/lib/api-standalone.js +673 -572
  17. package/lib/api.js +1437 -1433
  18. package/lib/bootstrap.js +211 -211
  19. package/lib/client.js +5444 -5295
  20. package/lib/commands.js +95 -95
  21. package/lib/config.js +880 -836
  22. package/lib/content-hash.js +54 -54
  23. package/lib/continuity.js +220 -220
  24. package/lib/document-index.js +94 -94
  25. package/lib/document.js +303 -303
  26. package/lib/dream/clustering.js +118 -118
  27. package/lib/dream/decisions.js +674 -674
  28. package/lib/dream/narratives.js +44 -44
  29. package/lib/dream/sleep.js +819 -819
  30. package/lib/dream.js +1501 -1501
  31. package/lib/embedding.js +177 -177
  32. package/lib/entities/extractor.js +252 -252
  33. package/lib/graph/anchoring.js +179 -179
  34. package/lib/guide.js +74 -74
  35. package/lib/heat.js +144 -144
  36. package/lib/hot-memory.js +46 -46
  37. package/lib/index.js +535 -671
  38. package/lib/inject.js +464 -450
  39. package/lib/lang.js +248 -229
  40. package/lib/local-embedder.js +358 -358
  41. package/lib/maintenance.js +142 -142
  42. package/lib/mirror.js +330 -330
  43. package/lib/organize.js +450 -450
  44. package/lib/peak-hours.js +117 -117
  45. package/lib/placement.js +13 -0
  46. package/lib/pre-inject-gate.js +340 -0
  47. package/lib/quality-filter.js +126 -126
  48. package/lib/recall-stats.js +175 -175
  49. package/lib/reranker.js +269 -269
  50. package/lib/runtime/adopt.js +176 -176
  51. package/lib/runtime/closure.js +172 -172
  52. package/lib/runtime/download.js +247 -247
  53. package/lib/runtime/layout.js +265 -265
  54. package/lib/runtime/loader.js +252 -252
  55. package/lib/runtime/model-fetch.js +361 -361
  56. package/lib/runtime/provision.js +237 -237
  57. package/lib/runtime/tarball.js +130 -130
  58. package/lib/runtime/verify.js +223 -204
  59. package/lib/scope.js +146 -146
  60. package/lib/search/adaptive.js +60 -60
  61. package/lib/search/bm25.js +96 -96
  62. package/lib/semantic.js +229 -0
  63. package/lib/sensitive-scan.js +209 -209
  64. package/lib/serve.js +215 -0
  65. package/lib/service.js +2663 -2596
  66. package/lib/settings.js +517 -509
  67. package/lib/store.js +3007 -3007
  68. package/lib/summarize.js +891 -856
  69. package/lib/tools.js +743 -737
  70. package/lib/vector-index.js +110 -110
  71. package/lib/version-check.js +122 -122
  72. package/lib/write-admission.js +402 -402
  73. package/locale/en.json +6 -6
  74. package/locale/zh.json +6 -6
  75. package/package.json +8 -4
  76. package/runtime-manifest.json +526 -526
  77. package/scripts/benchmark-embed.js +201 -201
  78. package/scripts/benchmark-recall.js +249 -249
  79. package/scripts/benchmark-rerank.js +166 -166
  80. package/scripts/build-runtime-manifest.mjs +224 -224
  81. package/scripts/changelog-prep.mjs +36 -0
  82. package/scripts/check-sync.js +42 -42
  83. package/scripts/e2e-dsh.js +220 -220
  84. package/scripts/mneme-runtime.mjs +277 -277
  85. package/scripts/stress-dsh.js +255 -255
  86. package/scripts/sync-lib.js +47 -47
  87. package/scripts/sync-test-badge.mjs +69 -69
  88. package/scripts/test-count-sync.mjs +44 -44
  89. package/src/api-standalone.js +673 -572
  90. package/src/api.js +1437 -1433
  91. package/src/bootstrap.js +211 -211
  92. package/src/commands.js +95 -95
  93. package/src/config.js +880 -836
  94. package/src/content-hash.js +54 -54
  95. package/src/continuity.js +220 -220
  96. package/src/document-index.js +94 -94
  97. package/src/document.js +303 -303
  98. package/src/dream/clustering.js +118 -118
  99. package/src/dream/decisions.js +674 -674
  100. package/src/dream/narratives.js +44 -44
  101. package/src/dream/sleep.js +819 -819
  102. package/src/dream.js +1501 -1501
  103. package/src/embedding.js +177 -177
  104. package/src/entities/extractor.js +252 -252
  105. package/src/graph/anchoring.js +179 -179
  106. package/src/guide.js +74 -74
  107. package/src/heat.js +144 -144
  108. package/src/hot-memory.js +46 -46
  109. package/src/index.js +535 -671
  110. package/src/inject.js +464 -450
  111. package/src/lang.js +248 -229
  112. package/src/local-embedder.js +358 -358
  113. package/src/maintenance.js +142 -142
  114. package/src/mirror.js +330 -330
  115. package/src/organize.js +450 -450
  116. package/src/peak-hours.js +117 -117
  117. package/src/placement.js +13 -0
  118. package/src/pre-inject-gate.js +340 -0
  119. package/src/quality-filter.js +126 -126
  120. package/src/recall-stats.js +175 -175
  121. package/src/reranker.js +269 -269
  122. package/src/runtime/adopt.js +176 -176
  123. package/src/runtime/closure.js +172 -172
  124. package/src/runtime/download.js +247 -247
  125. package/src/runtime/layout.js +265 -265
  126. package/src/runtime/loader.js +252 -252
  127. package/src/runtime/model-fetch.js +361 -361
  128. package/src/runtime/provision.js +237 -237
  129. package/src/runtime/tarball.js +130 -130
  130. package/src/runtime/verify.js +223 -204
  131. package/src/scope.js +146 -146
  132. package/src/search/adaptive.js +60 -60
  133. package/src/search/bm25.js +96 -96
  134. package/src/semantic.js +229 -0
  135. package/src/sensitive-scan.js +209 -209
  136. package/src/serve.js +215 -0
  137. package/src/service.js +2663 -2596
  138. package/src/settings.js +517 -509
  139. package/src/store.js +3007 -3007
  140. package/src/summarize.js +891 -856
  141. package/src/tools.js +743 -737
  142. package/src/vector-index.js +110 -110
  143. package/src/version-check.js +122 -122
  144. package/src/write-admission.js +402 -402
  145. package/test/adaptive-inject-budget.test.js +85 -85
  146. package/test/api-routes-gaps.test.js +218 -218
  147. package/test/api.test.js +1702 -1688
  148. package/test/audit.test.js +448 -448
  149. package/test/baseline-persist.test.js +248 -248
  150. package/test/benchmark.test.js +94 -94
  151. package/test/bootstrap.test.js +220 -220
  152. package/test/changelog-prep.test.js +60 -0
  153. package/test/client.test.js +1230 -1169
  154. package/test/clustering.test.js +100 -100
  155. package/test/commands.test.js +106 -106
  156. package/test/config.test.js +66 -66
  157. package/test/conflict-freeze.test.js +300 -300
  158. package/test/content-hash.test.js +204 -204
  159. package/test/continuity.test.js +333 -333
  160. package/test/document-dir.test.js +233 -233
  161. package/test/document.test.js +671 -671
  162. package/test/dream-archive-guardrail.test.js +116 -116
  163. package/test/dream-candidate-hybrid.test.js +281 -281
  164. package/test/dream-coverage-degrade.test.js +86 -86
  165. package/test/dream-failure-backoff.test.js +173 -173
  166. package/test/dream-peak-hours.test.js +194 -194
  167. package/test/dream.test.js +1385 -1383
  168. package/test/embedding-ready.test.js +196 -196
  169. package/test/entities.test.js +627 -547
  170. package/test/entity-recall.test.js +179 -179
  171. package/test/epistemic.test.js +325 -325
  172. package/test/fnew-0112.test.js +311 -311
  173. package/test/fnew-03.test.js +422 -422
  174. package/test/graph-anchoring.test.js +184 -184
  175. package/test/graph-api.test.js +175 -175
  176. package/test/graph-inject-budget.test.js +103 -103
  177. package/test/graph-passive-confirm.test.js +91 -91
  178. package/test/graph-weight-evolution.test.js +104 -104
  179. package/test/heat.test.js +175 -175
  180. package/test/helpers/dream-mock.js +82 -82
  181. package/test/helpers/peer-worker.mjs +25 -25
  182. package/test/helpers/runtime-source.js +60 -60
  183. package/test/helpers/tar-builder.js +80 -80
  184. package/test/helpers/write-admission-samples.js +308 -308
  185. package/test/hot-memory.test.js +145 -145
  186. package/test/inject-content-cap.test.js +91 -91
  187. package/test/inject-first-round-bm25.test.js +89 -89
  188. package/test/inject-heat.test.js +77 -61
  189. package/test/inject-parent-gate.test.js +106 -106
  190. package/test/inject-pools.test.js +194 -194
  191. package/test/inject-recall.test.js +105 -105
  192. package/test/inject-rotation.test.js +161 -161
  193. package/test/inject.test.js +420 -420
  194. package/test/lang.test.js +87 -87
  195. package/test/lastrunat-persist.test.js +101 -101
  196. package/test/lib-smoke.test.js +96 -96
  197. package/test/llm-audit.test.js +598 -598
  198. package/test/llm-message-source.test.js +207 -207
  199. package/test/local-embedder.test.js +292 -292
  200. package/test/maintenance.test.js +391 -391
  201. package/test/mcp-stdio.test.js +385 -385
  202. package/test/mirror-dirty.test.js +424 -424
  203. package/test/mirror-edit-digest.test.js +187 -187
  204. package/test/mirror-generation.test.js +499 -499
  205. package/test/mirror-types.test.js +208 -208
  206. package/test/mirror.test.js +249 -249
  207. package/test/narrative-bars.test.js +162 -162
  208. package/test/organize.test.js +418 -418
  209. package/test/peer-blockers.test.js +193 -193
  210. package/test/placement.test.js +36 -0
  211. package/test/policy-epoch.test.js +259 -259
  212. package/test/pre-inject-gate.test.js +434 -0
  213. package/test/quality-filter.test.js +186 -186
  214. package/test/reasoning-effort.test.js +752 -752
  215. package/test/recall-anti-update.test.js +82 -82
  216. package/test/recall-evals.test.js +253 -253
  217. package/test/recall-layer.test.js +326 -326
  218. package/test/recall-stats.test.js +189 -189
  219. package/test/receipt-chain.test.js +451 -451
  220. package/test/reflection.test.js +226 -226
  221. package/test/reindex-backfill.test.js +62 -62
  222. package/test/reranker-single-logit.test.js +73 -73
  223. package/test/reranker.test.js +197 -197
  224. package/test/resident-status.test.js +83 -83
  225. package/test/runtime-adopt.test.js +206 -206
  226. package/test/runtime-cli.test.js +89 -89
  227. package/test/runtime-closure.test.js +193 -193
  228. package/test/runtime-download.test.js +302 -302
  229. package/test/runtime-layout.test.js +163 -163
  230. package/test/runtime-loader.test.js +255 -255
  231. package/test/runtime-manifest.test.js +159 -159
  232. package/test/runtime-model-fetch.test.js +348 -348
  233. package/test/runtime-provision.test.js +101 -101
  234. package/test/runtime-tarball.test.js +81 -81
  235. package/test/runtime-verify.test.js +319 -279
  236. package/test/scope-candidates.test.js +239 -239
  237. package/test/scope-explicit.test.js +452 -452
  238. package/test/scope-inject-soft.test.js +106 -106
  239. package/test/scope-retrieval.test.js +197 -197
  240. package/test/scope-storage.test.js +255 -255
  241. package/test/scope-strict.test.js +367 -343
  242. package/test/search-fusion.test.js +90 -90
  243. package/test/semantic.test.js +124 -124
  244. package/test/sensitive-scan.test.js +207 -207
  245. package/test/serve-bin.test.js +121 -0
  246. package/test/serve.test.js +196 -0
  247. package/test/service-search.test.js +174 -174
  248. package/test/service.test.js +405 -405
  249. package/test/settings.test.js +261 -261
  250. package/test/skip-invalid-restore.test.js +262 -262
  251. package/test/sleep-action-set.test.js +422 -422
  252. package/test/sleep-heat.test.js +126 -126
  253. package/test/sleep-scheduler-rearm.test.js +89 -89
  254. package/test/sleep.test.js +527 -527
  255. package/test/standalone-api.test.js +751 -544
  256. package/test/store-vector-cache.test.js +98 -98
  257. package/test/store.test.js +396 -396
  258. package/test/stress.test.js +209 -209
  259. package/test/summarize-occurred-at.test.js +127 -0
  260. package/test/summarize-reasoning-effort.test.js +189 -189
  261. package/test/summarize.test.js +1465 -1465
  262. package/test/supersede-state-tracking.test.js +394 -0
  263. package/test/test-count-sync.test.js +60 -60
  264. package/test/tools-dto-schema-matrix.test.js +325 -325
  265. package/test/tools-memory-get-schema.test.js +65 -65
  266. package/test/tools.test.js +428 -374
  267. package/test/updated-at-semantics.test.js +113 -113
  268. package/test/vector-index.test.js +221 -221
  269. package/test/version-check.test.js +152 -152
  270. package/test/webServer-optional.test.js +82 -82
  271. package/test/write-admission.test.js +668 -668
@@ -1,343 +1,367 @@
1
- import test from "node:test";
2
- import assert from "node:assert/strict";
3
- import { createStore } from "../src/store.js";
4
- import { createService } from "../src/service.js";
5
- import { createTools } from "../src/tools.js";
6
-
7
- // v0.8.0 A3(issue #17)→ v0.8.1 第 3 步(issue #170 4.3,作者已确认):
8
- // strictScope 硬过滤只认显式声明。可见性公式收窄为:记忆可见 ⇔ 对每一维
9
- // (该维标注存在且来源=explicit 时:命中当前会话)AND。
10
- // - explicit 行为硬墙:他者维度不可见(含 memory_get 无存在性泄漏);
11
- // - auto / 存量 NULL 来源(v0.8.0 自动标注)不进硬过滤,只吃 A2 软加权
12
- // (foreign ×0.5 保留可见);
13
- // - 当前会话维度解析不到 → 该维 explicit 行一律不可见(fail-closed 收窄)。
14
-
15
- function setup(config) {
16
- const store = createStore(":memory:");
17
- const service = createService({ store, mirror: null, config });
18
- return { store, service };
19
- }
20
-
21
- test("visibility predicate: explicit rows keep four-quadrant semantics", () => {
22
- const { service } = setup({ scopeEnabled: true, strictScope: true });
23
- const vis = service.isVisibleInScope;
24
- const me = { agent_scope: "coder", workspace_scope: "D:\\p" };
25
- const E = "explicit";
26
-
27
- // 全局(未标注)对谁都可见。
28
- assert.equal(vis({}, me), true);
29
- // agent 专属:同 agent 任意 workspace 可见,他人不可见。
30
- assert.equal(vis({ agent_scope: "coder", agent_scope_source: E }, me), true);
31
- assert.equal(vis({ agent_scope: "novelist", agent_scope_source: E }, me), false);
32
- // workspace 专属:同 workspace 任意 agent 可见,别的 workspace 不可见。
33
- assert.equal(vis({ workspace_scope: "D:\\p", workspace_scope_source: E }, me), true);
34
- assert.equal(vis({ workspace_scope: "D:\\q", workspace_scope_source: E }, me), false);
35
- // 双标注:精确命中才可见。
36
- assert.equal(vis({ agent_scope: "coder", agent_scope_source: E, workspace_scope: "D:\\p", workspace_scope_source: E }, me), true);
37
- assert.equal(vis({ agent_scope: "coder", agent_scope_source: E, workspace_scope: "D:\\q", workspace_scope_source: E }, me), false);
38
- assert.equal(vis({ agent_scope: "novelist", agent_scope_source: E, workspace_scope: "D:\\p", workspace_scope_source: E }, me), false);
39
- });
40
-
41
- test("visibility predicate: auto and legacy rows are NOT hard walls (soft only)", () => {
42
- const { service } = setup({ scopeEnabled: true, strictScope: true });
43
- const vis = service.isVisibleInScope;
44
- const me = { agent_scope: "coder", workspace_scope: "D:\\p" };
45
-
46
- // auto(v0.8.1 载体自动标注)与存量 NULL 来源(v0.8.0 自动标注)他者维度
47
- // 也可见——硬过滤只认 explicit。
48
- assert.equal(vis({ agent_scope: "novelist" }, me), true, "legacy NULL source");
49
- assert.equal(vis({ agent_scope: "novelist", agent_scope_source: "auto" }, me), true);
50
- assert.equal(vis({ workspace_scope: "D:\\q", workspace_scope_source: "auto" }, me), true);
51
- // 混合:agent 维 auto + workspace 维 explicit → 只 workspace 维构成硬墙。
52
- assert.equal(vis({ agent_scope: "novelist", agent_scope_source: "auto", workspace_scope: "D:\\q", workspace_scope_source: "explicit" }, me), false);
53
- assert.equal(vis({ agent_scope: "novelist", agent_scope_source: "explicit", workspace_scope: "D:\\q", workspace_scope_source: "auto" }, me), false);
54
- });
55
-
56
- test("visibility predicate: anonymous session fail-closes only explicit rows", () => {
57
- const { service } = setup({ scopeEnabled: true, strictScope: true });
58
- const vis = service.isVisibleInScope;
59
- const anon = { agent_scope: null, workspace_scope: null };
60
- assert.equal(vis({}, anon), true);
61
- assert.equal(vis({ agent_scope: "coder", agent_scope_source: "explicit" }, anon), false);
62
- assert.equal(vis({ workspace_scope: "D:\\p", workspace_scope_source: "explicit" }, anon), false);
63
- // auto/存量行照常可见(不冒认,但也不因身份缺失误杀软标注)。
64
- assert.equal(vis({ agent_scope: "coder" }, anon), true);
65
- assert.equal(vis({ agent_scope: "coder", agent_scope_source: "auto" }, anon), true);
66
- });
67
-
68
- test("searchMemories: explicit foreign filtered, auto/legacy foreign stay visible under strictScope", async () => {
69
- const { store, service } = setup({ scopeEnabled: true, strictScope: true });
70
- store.save({ type: "project", title: "global needle", content: "x" });
71
- store.save({ type: "project", title: "mine needle", content: "x", agent_scope: "me", agent_scope_source: "explicit" });
72
- store.save({ type: "project", title: "explicit foreign needle", content: "x", agent_scope: "other", agent_scope_source: "explicit" });
73
- store.save({ type: "project", title: "auto foreign needle", content: "x", agent_scope: "other", agent_scope_source: "auto" });
74
- store.save({ type: "project", title: "legacy foreign needle", content: "x", agent_scope: "other" });
75
-
76
- const rows = await service.searchMemories("needle", {
77
- mode: "keyword",
78
- scope: { agent_scope: "me", workspace_scope: null }
79
- });
80
- // explicit 他者出局;auto/存量他者保留(A2 降权仍可见);未标注与命中行保留。
81
- assert.deepEqual(rows.map((m) => m.title).sort(), [
82
- "auto foreign needle", "global needle", "legacy foreign needle", "mine needle"
83
- ]);
84
- });
85
-
86
- test("strictScope off keeps A2 behavior: foreign rows demoted but visible", async () => {
87
- const { store, service } = setup({ scopeEnabled: true });
88
- store.save({ type: "project", title: "foreign needle", content: "x", agent_scope: "other" });
89
- const rows = await service.searchMemories("needle", {
90
- mode: "keyword",
91
- scope: { agent_scope: "me", workspace_scope: null }
92
- });
93
- assert.deepEqual(rows.map((m) => m.title), ["foreign needle"]);
94
- });
95
-
96
- test("store.list/count visibility: explicit hard wall + auto/legacy pass, consistent totals", () => {
97
- const { store } = setup({ scopeEnabled: true, strictScope: true });
98
- store.save({ type: "decision", title: "g", content: "x" });
99
- store.save({ type: "decision", title: "mine", content: "x", agent_scope: "me", agent_scope_source: "explicit", workspace_scope: "D:\\p", workspace_scope_source: "explicit" });
100
- store.save({ type: "decision", title: "theirs-explicit", content: "x", agent_scope: "other", agent_scope_source: "explicit" });
101
- store.save({ type: "decision", title: "theirs-auto", content: "x", agent_scope: "other", agent_scope_source: "auto" });
102
- store.save({ type: "decision", title: "theirs-legacy", content: "x", agent_scope: "other" });
103
- store.save({ type: "decision", title: "other-ws-explicit", content: "x", workspace_scope: "D:\\q", workspace_scope_source: "explicit" });
104
-
105
- const vis = { agentScope: "me", workspaceScope: "D:\\p" };
106
- const titles = store.list({ visibility: vis }).map((m) => m.title).sort();
107
- assert.deepEqual(titles, ["g", "mine", "theirs-auto", "theirs-legacy"]);
108
- assert.equal(store.count("decision", { visibility: vis }), 4);
109
-
110
- // 匿名会话:explicit 行全出局,auto/存量/未标注放行。
111
- const anonTitles = store.list({ visibility: { agentScope: null, workspaceScope: null } }).map((m) => m.title).sort();
112
- assert.deepEqual(anonTitles, ["g", "theirs-auto", "theirs-legacy"]);
113
- assert.equal(store.count("decision", { visibility: { agentScope: null, workspaceScope: null } }), 3);
114
- });
115
-
116
- test("injectCandidates hard-filters explicit foreign rows when strictScope is on", () => {
117
- const { store, service } = setup({ scopeEnabled: true, strictScope: true });
118
- store.save({ type: "decision", title: "global", content: "x", importance: 4 });
119
- store.save({ type: "decision", title: "foreign", content: "x", importance: 4, agent_scope: "other", agent_scope_source: "explicit" });
120
- store.save({ type: "decision", title: "auto-foreign", content: "x", importance: 4, agent_scope: "other", agent_scope_source: "auto" });
121
-
122
- const injected = service.injectCandidates({
123
- maxItems: 5,
124
- threshold: 3,
125
- scope: { agent_scope: "me", workspace_scope: null }
126
- });
127
- assert.deepEqual(injected.map((m) => m.title).sort(), ["auto-foreign", "global"]);
128
-
129
- // flag 下(scope 不传)→ 他 scope 照常注入(A2 前行为)。
130
- const unscoped = service.injectCandidates({ maxItems: 5, threshold: 3 });
131
- assert.deepEqual(unscoped.map((m) => m.title).sort(), ["auto-foreign", "foreign", "global"]);
132
- });
133
-
134
- test("memory_update/memory_delete hide explicit foreign-scope rows under strictScope (review item 4, aligned with memory_get)", async () => {
135
- const { store, pick } = setupTools({ scopeEnabled: true, strictScope: true });
136
- const theirs = store.save({ type: "pitfall", title: "theirs", content: "c", agent_scope: "other", agent_scope_source: "explicit" });
137
- const autoTheirs = store.save({ type: "pitfall", title: "auto-theirs", content: "c", agent_scope: "other", agent_scope_source: "auto" });
138
- const update = pick("memory_update");
139
- const del = pick("memory_delete");
140
-
141
- // explicit 他 scope:update 按不存在拒绝、delete 视作不存在——不能凭 id 直改直删。
142
- await assert.rejects(() => runHandler(update, { id: theirs.id, content: "tampered" }, STRICT_EXEC), /memory not found/);
143
- assert.equal(store.getById(theirs.id).content, "c", "row untouched by the rejected update");
144
- const delOut = await runHandler(del, { id: theirs.id }, STRICT_EXEC);
145
- assert.equal(delOut.deleted, false, "delete treats invisible rows as absent (no existence leak)");
146
- assert.ok(store.getById(theirs.id), "row NOT deleted");
147
-
148
- // auto 他 scope:不构成硬墙,照常可改(与 get 的可见性口径一致)。
149
- const updAuto = await runHandler(update, { id: autoTheirs.id, content: "updated" }, STRICT_EXEC);
150
- assert.equal(updAuto.memory.id, autoTheirs.id);
151
-
152
- // 行主人自己照常可改。
153
- const ownerOut = await runHandler(update, { id: theirs.id, content: "owner edit" }, OTHER_EXEC);
154
- assert.equal(ownerOut.memory.id, theirs.id);
155
-
156
- // strictScope 关:update/delete 不做可见性校验(管理语义回归)。
157
- const { store: store2, pick: pick2 } = setupTools({ scopeEnabled: true });
158
- const t2 = store2.save({ type: "pitfall", title: "theirs", content: "c", agent_scope: "other", agent_scope_source: "explicit" });
159
- await runHandler(pick2("memory_update"), { id: t2.id, content: "edited" }, STRICT_EXEC);
160
- assert.equal(store2.getById(t2.id).content, "edited");
161
- const del2 = await runHandler(pick2("memory_delete"), { id: t2.id }, STRICT_EXEC);
162
- assert.equal(del2.deleted, true);
163
- });
164
-
165
- function setupTools(config) {
166
- const store = createStore(":memory:");
167
- const service = createService({ store, mirror: null, config });
168
- const registered = [];
169
- createTools(
170
- { tools: { register(def) { registered.push(def); return () => {}; } } },
171
- service,
172
- config
173
- );
174
- const pick = (name) => registered.find((def) => def.name === name);
175
- return { store, pick };
176
- }
177
-
178
- async function runHandler(def, args, exec) {
179
- const handler = def.execute.bind(def);
180
- return handler(args, exec);
181
- }
182
-
183
- const STRICT_EXEC = { agent: { session: { id: "s1", header: { agentPreset: "me", cwd: "D:\\p" } } } };
184
- const OTHER_EXEC = { agent: { session: { id: "s2", header: { agentPreset: "other", cwd: "D:\\q" } } } };
185
-
186
- test("memory_get hides explicit foreign-scope rows under strictScope, resolvable for the owner", async () => {
187
- const { store, pick } = setupTools({ scopeEnabled: true, strictScope: true });
188
- const mine = store.save({ type: "pitfall", title: "mine", content: "c", agent_scope: "me", agent_scope_source: "explicit" });
189
- const theirs = store.save({ type: "pitfall", title: "theirs", content: "c", agent_scope: "other", agent_scope_source: "explicit" });
190
- const autoTheirs = store.save({ type: "pitfall", title: "auto-theirs", content: "c", agent_scope: "other", agent_scope_source: "auto" });
191
- const get = pick("memory_get");
192
-
193
- const out = await runHandler(get, { id: mine.id }, STRICT_EXEC);
194
- assert.equal(out.memory.title, "mine");
195
-
196
- // explicit 他 scope → 按不存在处理(无存在性泄漏)。
197
- await assert.rejects(() => runHandler(get, { id: theirs.id }, STRICT_EXEC), /memory not found/);
198
- // auto 他 scope → 可见(软加权不进硬过滤)。
199
- const autoOut = await runHandler(get, { id: autoTheirs.id }, STRICT_EXEC);
200
- assert.equal(autoOut.memory.title, "auto-theirs");
201
- // 行主人自己照常可取。
202
- const ownerOut = await runHandler(get, { id: theirs.id }, OTHER_EXEC);
203
- assert.equal(ownerOut.memory.title, "theirs");
204
- });
205
-
206
- test("memory_get without strictScope returns any row regardless of scope annotation", async () => {
207
- const { store, pick } = setupTools({ scopeEnabled: true });
208
- const theirs = store.save({ type: "pitfall", title: "theirs", content: "c", agent_scope: "other", agent_scope_source: "explicit" });
209
- const get = pick("memory_get");
210
- const out = await runHandler(get, { id: theirs.id }, STRICT_EXEC);
211
- assert.equal(out.memory.title, "theirs");
212
- });
213
-
214
- test("memory_list filters explicit rows and keeps total consistent under strictScope", async () => {
215
- const { store, pick } = setupTools({ scopeEnabled: true, strictScope: true });
216
- store.save({ type: "decision", title: "g", content: "x" });
217
- store.save({ type: "decision", title: "mine", content: "x", agent_scope: "me", agent_scope_source: "explicit" });
218
- store.save({ type: "decision", title: "theirs", content: "x", agent_scope: "other", agent_scope_source: "explicit" });
219
- const list = pick("memory_list");
220
-
221
- const out = await runHandler(list, { type: "decision" }, STRICT_EXEC);
222
- assert.deepEqual(out.items.map((m) => m.title).sort(), ["g", "mine"]);
223
- assert.equal(out.total, 2);
224
-
225
- // flag 关(A2 默认):他 scope 的行照常出现在浏览视图。
226
- const { store: store2, pick: pick2 } = setupTools({ scopeEnabled: true });
227
- store2.save({ type: "decision", title: "theirs", content: "x", agent_scope: "other", agent_scope_source: "explicit" });
228
- const out2 = await runHandler(pick2("memory_list"), { type: "decision" }, STRICT_EXEC);
229
- assert.deepEqual(out2.items.map((m) => m.title), ["theirs"]);
230
- assert.equal(out2.total, 1);
231
- });
232
-
233
- // --- entity: / attr: 前缀路的 scope 闸 ----------------------------------------
234
- // 回归锁:这两条前缀路在 searchMemories 的入口就 return,早于融合池那道 strictScope
235
- // 过滤,曾经整条绕过硬墙——显式他者 scope 的记忆用 `entity:` / `attr:` 就能原样读出,
236
- // 而且是满分返回(连 A2 的 ×0.5 都没有)。所以除了「出局」,还要钉住「auto / 存量
237
- // 他者保留」这条 A2 语义没有跟着塌掉。
238
-
239
- /** 把若干记忆挂到同一个实体上。
240
- * 属性键必须各不相同:同一 (实体, 键) 有时间轴语义,后写的一条会把前一条作废,
241
- * 那样只有最后一条会被链接上(写这个测试时踩过,别改成同一个键)。 */
242
- function linkToEntity(store, name, memories) {
243
- const entity = store.createEntity({ name, type: "person" });
244
- memories.forEach((mem, i) => {
245
- store.saveAttr({ entity_id: entity.id, attr_key: `attr${i}`, attr_value: `value${i}`, memory_id: mem.id });
246
- });
247
- return entity;
248
- }
249
-
250
- /** 五条覆盖各种 scope 来源的记忆,供下面两条用。 */
251
- function saveScopeBattery(store) {
252
- return {
253
- global: store.save({ type: "project", title: "global", content: "x" }),
254
- mine: store.save({ type: "project", title: "mine", content: "x", agent_scope: "me", agent_scope_source: "explicit" }),
255
- theirs: store.save({ type: "project", title: "theirs", content: "x", agent_scope: "other", agent_scope_source: "explicit" }),
256
- autoForeign: store.save({ type: "project", title: "auto", content: "x", agent_scope: "other", agent_scope_source: "auto" }),
257
- legacyForeign: store.save({ type: "project", title: "legacy", content: "x", agent_scope: "other" })
258
- };
259
- }
260
-
261
- test("searchMemories entity: prefix honours the strictScope hard filter", async () => {
262
- const { store, service } = setup({ scopeEnabled: true, strictScope: true, entitySearchEnabled: true });
263
- const rows = saveScopeBattery(store);
264
- linkToEntity(store, "阿尔托", Object.values(rows));
265
-
266
- const asMe = await service.searchMemories("entity:阿尔托", { scope: { agent_scope: "me", workspace_scope: null } });
267
- assert.deepEqual(asMe.map((m) => m.title).sort(), ["auto", "global", "legacy", "mine"], "显式他者出局,auto / 存量他者按 A2 保留");
268
-
269
- // 原主人自己查:显式那条必须还在(否则上面是「谁都搜不到」的假绿)。
270
- const asOther = await service.searchMemories("entity:阿尔托", { scope: { agent_scope: "other", workspace_scope: null } });
271
- assert.ok(asOther.some((m) => m.title === "theirs"), "命中当前 agent 的显式行必须可见");
272
- });
273
-
274
- test("searchMemories attr: prefix honours the strictScope hard filter", async () => {
275
- const { store, service } = setup({ scopeEnabled: true, strictScope: true, entitySearchEnabled: true });
276
- const rows = saveScopeBattery(store);
277
- // 每条挂到各自实体上、共用一个属性键:这样 attr:key=value 能一次覆盖全部五条
278
- // (同实体同键会被时间轴作废,见 linkToEntity 的注释)。
279
- Object.values(rows).forEach((mem, i) => {
280
- const entity = store.createEntity({ name: `entity-${i}`, type: "person" });
281
- store.saveAttr({ entity_id: entity.id, attr_key: "国籍", attr_value: "芬兰", memory_id: mem.id });
282
- });
283
-
284
- const asMe = await service.searchMemories("attr:国籍=芬兰", { scope: { agent_scope: "me", workspace_scope: null } });
285
- assert.deepEqual(asMe.map((m) => m.title).sort(), ["auto", "global", "legacy", "mine"], "显式他者出局,auto / 存量他者按 A2 保留");
286
-
287
- const asOther = await service.searchMemories("attr:国籍=芬兰", { scope: { agent_scope: "other", workspace_scope: null } });
288
- assert.ok(asOther.some((m) => m.title === "theirs"), "命中当前 agent 的显式行必须可见");
289
- });
290
-
291
- test("entity: / attr: 的 scope 闸在触达之前:出局的行不被回温,也不 bump 关联边", async () => {
292
- // 闸门若只加在「返回前」,出局的行仍会先被 touchRecalled 摸一遍——一次越权检索
293
- // 照样给它刷回温时钟、并在被动确认开启时 bump 它的关联边。所以这条钉的是次序。
294
- const { store, service } = setup({
295
- scopeEnabled: true, strictScope: true, entitySearchEnabled: true,
296
- heatEnabled: true, graphWeightEnabled: true, graphPassiveConfirm: true, graphWeightDelta: 0.1
297
- });
298
- const theirs = store.save({ type: "project", title: "theirs", content: "x", agent_scope: "other", agent_scope_source: "explicit" });
299
- const global = store.save({ type: "project", title: "global", content: "x" });
300
- linkToEntity(store, "阿尔托", [theirs, global]);
301
-
302
- const a = store.createEntity({ name: "A", type: "technology" });
303
- const b = store.createEntity({ name: "B", type: "technology" });
304
- for (const mem of [theirs, global]) {
305
- store.saveRelation({ from_entity: a.id, to_entity: b.id, relation_type: "uses", memory_id: mem.id, source: "llm" });
306
- }
307
-
308
- const rows = await service.searchMemories("entity:阿尔托", { scope: { agent_scope: "me", workspace_scope: null } });
309
- assert.deepEqual(rows.map((m) => m.title), ["global"], "只剩可见那条");
310
-
311
- // 正向对照:可见那条确实被触达了——否则下面两个「没被触达」可能是假绿。
312
- assert.ok(store.getRelationsByMemory(global.id)[0].weight > 0.4, "可见行照常 bump 关联边");
313
- assert.ok(store.getById(global.id).last_accessed_at, "可见行照常回温");
314
-
315
- assert.equal(store.getRelationsByMemory(theirs.id)[0].weight, 0.4, "出局行不该 bump 关联边");
316
- assert.equal(store.getById(theirs.id).last_accessed_at ?? null, null, "出局行不该被回温");
317
- });
318
-
319
- test("scope 闸先于 topK 截断:出局的候选不占名额(topK=1 仍有可见结果)", async () => {
320
- // 回归锁(评审发现):先 slice 再 filter 的话,排在前面那条被闸掉的候选会占住唯一的
321
- // 槽位,明明还有可见匹配却返回空数组。用 topK=1 把次序钉死——排序不动,只是闸门要插
322
- // 在截断之前。
323
- const me = { agent_scope: "me", workspace_scope: null };
324
-
325
- const { store, service } = setup({ scopeEnabled: true, strictScope: true, entitySearchEnabled: true });
326
- const theirsE = store.save({ type: "project", title: "theirs", content: "x", agent_scope: "other", agent_scope_source: "explicit" });
327
- const mineE = store.save({ type: "project", title: "mine", content: "x", agent_scope: "me", agent_scope_source: "explicit" });
328
- // 先挂 theirs、后挂 mine:前者排在候选前面,正好充当那个「占位」的候选。
329
- linkToEntity(store, "阿尔托", [theirsE, mineE]);
330
- const viaEntity = await service.searchMemories("entity:阿尔托", { topK: 1, scope: me });
331
- assert.deepEqual(viaEntity.map((m) => m.title), ["mine"], "entity: 在 topK=1 下不该被出局候选挤空");
332
-
333
- // attr: 同理——两条挂同一个属性键、各挂自己的实体,theirs 先写入。
334
- const { store: s2, service: svc2 } = setup({ scopeEnabled: true, strictScope: true, entitySearchEnabled: true });
335
- const theirsA = s2.save({ type: "project", title: "theirs", content: "x", agent_scope: "other", agent_scope_source: "explicit" });
336
- const mineA = s2.save({ type: "project", title: "mine", content: "x", agent_scope: "me", agent_scope_source: "explicit" });
337
- for (const [i, mem] of [theirsA, mineA].entries()) {
338
- const entity = s2.createEntity({ name: `e${i}`, type: "person" });
339
- s2.saveAttr({ entity_id: entity.id, attr_key: "国籍", attr_value: "芬兰", memory_id: mem.id });
340
- }
341
- const viaAttr = await svc2.searchMemories("attr:国籍=芬兰", { topK: 1, scope: me });
342
- assert.deepEqual(viaAttr.map((m) => m.title), ["mine"], "attr: 在 topK=1 下不该被出局候选挤空");
343
- });
1
+ import test from "node:test";
2
+ import assert from "node:assert/strict";
3
+ import { createStore } from "../src/store.js";
4
+ import { createService } from "../src/service.js";
5
+ import { createTools } from "../src/tools.js";
6
+
7
+ // v0.8.0 A3(issue #17)→ v0.8.1 第 3 步(issue #170 4.3,作者已确认):
8
+ // strictScope 硬过滤只认显式声明。可见性公式收窄为:记忆可见 ⇔ 对每一维
9
+ // (该维标注存在且来源=explicit 时:命中当前会话)AND。
10
+ // - explicit 行为硬墙:他者维度不可见(含 memory_get 无存在性泄漏);
11
+ // - auto / 存量 NULL 来源(v0.8.0 自动标注)不进硬过滤,只吃 A2 软加权
12
+ // (foreign ×0.5 保留可见);
13
+ // - 当前会话维度解析不到 → 该维 explicit 行一律不可见(fail-closed 收窄)。
14
+
15
+ function setup(config) {
16
+ const store = createStore(":memory:");
17
+ const service = createService({ store, mirror: null, config });
18
+ return { store, service };
19
+ }
20
+
21
+ test("visibility predicate: explicit rows keep four-quadrant semantics", () => {
22
+ const { service } = setup({ scopeEnabled: true, strictScope: true });
23
+ const vis = service.isVisibleInScope;
24
+ const me = { agent_scope: "coder", workspace_scope: "D:\\p" };
25
+ const E = "explicit";
26
+
27
+ // 全局(未标注)对谁都可见。
28
+ assert.equal(vis({}, me), true);
29
+ // agent 专属:同 agent 任意 workspace 可见,他人不可见。
30
+ assert.equal(vis({ agent_scope: "coder", agent_scope_source: E }, me), true);
31
+ assert.equal(vis({ agent_scope: "novelist", agent_scope_source: E }, me), false);
32
+ // workspace 专属:同 workspace 任意 agent 可见,别的 workspace 不可见。
33
+ assert.equal(vis({ workspace_scope: "D:\\p", workspace_scope_source: E }, me), true);
34
+ assert.equal(vis({ workspace_scope: "D:\\q", workspace_scope_source: E }, me), false);
35
+ // 双标注:精确命中才可见。
36
+ assert.equal(vis({ agent_scope: "coder", agent_scope_source: E, workspace_scope: "D:\\p", workspace_scope_source: E }, me), true);
37
+ assert.equal(vis({ agent_scope: "coder", agent_scope_source: E, workspace_scope: "D:\\q", workspace_scope_source: E }, me), false);
38
+ assert.equal(vis({ agent_scope: "novelist", agent_scope_source: E, workspace_scope: "D:\\p", workspace_scope_source: E }, me), false);
39
+ });
40
+
41
+ test("visibility predicate: auto and legacy rows are NOT hard walls (soft only)", () => {
42
+ const { service } = setup({ scopeEnabled: true, strictScope: true });
43
+ const vis = service.isVisibleInScope;
44
+ const me = { agent_scope: "coder", workspace_scope: "D:\\p" };
45
+
46
+ // auto(v0.8.1 载体自动标注)与存量 NULL 来源(v0.8.0 自动标注)他者维度
47
+ // 也可见——硬过滤只认 explicit。
48
+ assert.equal(vis({ agent_scope: "novelist" }, me), true, "legacy NULL source");
49
+ assert.equal(vis({ agent_scope: "novelist", agent_scope_source: "auto" }, me), true);
50
+ assert.equal(vis({ workspace_scope: "D:\\q", workspace_scope_source: "auto" }, me), true);
51
+ // 混合:agent 维 auto + workspace 维 explicit → 只 workspace 维构成硬墙。
52
+ assert.equal(vis({ agent_scope: "novelist", agent_scope_source: "auto", workspace_scope: "D:\\q", workspace_scope_source: "explicit" }, me), false);
53
+ assert.equal(vis({ agent_scope: "novelist", agent_scope_source: "explicit", workspace_scope: "D:\\q", workspace_scope_source: "auto" }, me), false);
54
+ });
55
+
56
+ test("visibility predicate: anonymous session fail-closes only explicit rows", () => {
57
+ const { service } = setup({ scopeEnabled: true, strictScope: true });
58
+ const vis = service.isVisibleInScope;
59
+ const anon = { agent_scope: null, workspace_scope: null };
60
+ assert.equal(vis({}, anon), true);
61
+ assert.equal(vis({ agent_scope: "coder", agent_scope_source: "explicit" }, anon), false);
62
+ assert.equal(vis({ workspace_scope: "D:\\p", workspace_scope_source: "explicit" }, anon), false);
63
+ // auto/存量行照常可见(不冒认,但也不因身份缺失误杀软标注)。
64
+ assert.equal(vis({ agent_scope: "coder" }, anon), true);
65
+ assert.equal(vis({ agent_scope: "coder", agent_scope_source: "auto" }, anon), true);
66
+ });
67
+
68
+ test("searchMemories: explicit foreign filtered, auto/legacy foreign stay visible under strictScope", async () => {
69
+ const { store, service } = setup({ scopeEnabled: true, strictScope: true });
70
+ store.save({ type: "project", title: "global needle", content: "x" });
71
+ store.save({ type: "project", title: "mine needle", content: "x", agent_scope: "me", agent_scope_source: "explicit" });
72
+ store.save({ type: "project", title: "explicit foreign needle", content: "x", agent_scope: "other", agent_scope_source: "explicit" });
73
+ store.save({ type: "project", title: "auto foreign needle", content: "x", agent_scope: "other", agent_scope_source: "auto" });
74
+ store.save({ type: "project", title: "legacy foreign needle", content: "x", agent_scope: "other" });
75
+
76
+ const rows = await service.searchMemories("needle", {
77
+ mode: "keyword",
78
+ scope: { agent_scope: "me", workspace_scope: null }
79
+ });
80
+ // explicit 他者出局;auto/存量他者保留(A2 降权仍可见);未标注与命中行保留。
81
+ assert.deepEqual(rows.map((m) => m.title).sort(), [
82
+ "auto foreign needle", "global needle", "legacy foreign needle", "mine needle"
83
+ ]);
84
+ });
85
+
86
+ test("strictScope off keeps A2 behavior: foreign rows demoted but visible", async () => {
87
+ const { store, service } = setup({ scopeEnabled: true });
88
+ store.save({ type: "project", title: "foreign needle", content: "x", agent_scope: "other" });
89
+ const rows = await service.searchMemories("needle", {
90
+ mode: "keyword",
91
+ scope: { agent_scope: "me", workspace_scope: null }
92
+ });
93
+ assert.deepEqual(rows.map((m) => m.title), ["foreign needle"]);
94
+ });
95
+
96
+ test("store.list/count visibility: explicit hard wall + auto/legacy pass, consistent totals", () => {
97
+ const { store } = setup({ scopeEnabled: true, strictScope: true });
98
+ store.save({ type: "decision", title: "g", content: "x" });
99
+ store.save({ type: "decision", title: "mine", content: "x", agent_scope: "me", agent_scope_source: "explicit", workspace_scope: "D:\\p", workspace_scope_source: "explicit" });
100
+ store.save({ type: "decision", title: "theirs-explicit", content: "x", agent_scope: "other", agent_scope_source: "explicit" });
101
+ store.save({ type: "decision", title: "theirs-auto", content: "x", agent_scope: "other", agent_scope_source: "auto" });
102
+ store.save({ type: "decision", title: "theirs-legacy", content: "x", agent_scope: "other" });
103
+ store.save({ type: "decision", title: "other-ws-explicit", content: "x", workspace_scope: "D:\\q", workspace_scope_source: "explicit" });
104
+
105
+ const vis = { agentScope: "me", workspaceScope: "D:\\p" };
106
+ const titles = store.list({ visibility: vis }).map((m) => m.title).sort();
107
+ assert.deepEqual(titles, ["g", "mine", "theirs-auto", "theirs-legacy"]);
108
+ assert.equal(store.count("decision", { visibility: vis }), 4);
109
+
110
+ // 匿名会话:explicit 行全出局,auto/存量/未标注放行。
111
+ const anonTitles = store.list({ visibility: { agentScope: null, workspaceScope: null } }).map((m) => m.title).sort();
112
+ assert.deepEqual(anonTitles, ["g", "theirs-auto", "theirs-legacy"]);
113
+ assert.equal(store.count("decision", { visibility: { agentScope: null, workspaceScope: null } }), 3);
114
+ });
115
+
116
+ test("injectCandidates hard-filters explicit foreign rows when strictScope is on", () => {
117
+ const { store, service } = setup({ scopeEnabled: true, strictScope: true });
118
+ store.save({ type: "decision", title: "global", content: "x", importance: 4 });
119
+ store.save({ type: "decision", title: "foreign", content: "x", importance: 4, agent_scope: "other", agent_scope_source: "explicit" });
120
+ store.save({ type: "decision", title: "auto-foreign", content: "x", importance: 4, agent_scope: "other", agent_scope_source: "auto" });
121
+
122
+ const injected = service.injectCandidates({
123
+ maxItems: 5,
124
+ threshold: 3,
125
+ scope: { agent_scope: "me", workspace_scope: null }
126
+ });
127
+ assert.deepEqual(injected.map((m) => m.title).sort(), ["auto-foreign", "global"]);
128
+
129
+ // flag 下(scope 不传)→ 他 scope 照常注入(A2 前行为)。
130
+ const unscoped = service.injectCandidates({ maxItems: 5, threshold: 3 });
131
+ assert.deepEqual(unscoped.map((m) => m.title).sort(), ["auto-foreign", "foreign", "global"]);
132
+ });
133
+
134
+ test("memory_update/memory_delete hide explicit foreign-scope rows under strictScope (review item 4, aligned with memory_get)", async () => {
135
+ const { store, pick } = setupTools({ scopeEnabled: true, strictScope: true });
136
+ const theirs = store.save({ type: "pitfall", title: "theirs", content: "c", agent_scope: "other", agent_scope_source: "explicit" });
137
+ const autoTheirs = store.save({ type: "pitfall", title: "auto-theirs", content: "c", agent_scope: "other", agent_scope_source: "auto" });
138
+ const update = pick("memory_update");
139
+ const del = pick("memory_delete");
140
+
141
+ // explicit 他 scope:update 按不存在拒绝、delete 视作不存在——不能凭 id 直改直删。
142
+ await assert.rejects(() => runHandler(update, { id: theirs.id, content: "tampered" }, STRICT_EXEC), /memory not found/);
143
+ assert.equal(store.getById(theirs.id).content, "c", "row untouched by the rejected update");
144
+ const delOut = await runHandler(del, { id: theirs.id }, STRICT_EXEC);
145
+ assert.equal(delOut.deleted, false, "delete treats invisible rows as absent (no existence leak)");
146
+ assert.ok(store.getById(theirs.id), "row NOT deleted");
147
+
148
+ // auto 他 scope:不构成硬墙,照常可改(与 get 的可见性口径一致)。
149
+ const updAuto = await runHandler(update, { id: autoTheirs.id, content: "updated" }, STRICT_EXEC);
150
+ assert.equal(updAuto.memory.id, autoTheirs.id);
151
+
152
+ // 行主人自己照常可改。
153
+ const ownerOut = await runHandler(update, { id: theirs.id, content: "owner edit" }, OTHER_EXEC);
154
+ assert.equal(ownerOut.memory.id, theirs.id);
155
+
156
+ // strictScope 关:update/delete 不做可见性校验(管理语义回归)。
157
+ const { store: store2, pick: pick2 } = setupTools({ scopeEnabled: true });
158
+ const t2 = store2.save({ type: "pitfall", title: "theirs", content: "c", agent_scope: "other", agent_scope_source: "explicit" });
159
+ await runHandler(pick2("memory_update"), { id: t2.id, content: "edited" }, STRICT_EXEC);
160
+ assert.equal(store2.getById(t2.id).content, "edited");
161
+ const del2 = await runHandler(pick2("memory_delete"), { id: t2.id }, STRICT_EXEC);
162
+ assert.equal(del2.deleted, true);
163
+ });
164
+
165
+ function setupTools(config) {
166
+ const store = createStore(":memory:");
167
+ const service = createService({ store, mirror: null, config });
168
+ const registered = [];
169
+ createTools(
170
+ { tools: { register(def) { registered.push(def); return () => {}; } } },
171
+ service,
172
+ config
173
+ );
174
+ const pick = (name) => registered.find((def) => def.name === name);
175
+ return { store, pick };
176
+ }
177
+
178
+ async function runHandler(def, args, exec) {
179
+ const handler = def.execute.bind(def);
180
+ return handler(args, exec);
181
+ }
182
+
183
+ const STRICT_EXEC = { agent: { session: { id: "s1", header: { agentPreset: "me", cwd: "D:\\p" } } } };
184
+ const OTHER_EXEC = { agent: { session: { id: "s2", header: { agentPreset: "other", cwd: "D:\\q" } } } };
185
+
186
+ test("memory_get hides explicit foreign-scope rows under strictScope, resolvable for the owner", async () => {
187
+ const { store, pick } = setupTools({ scopeEnabled: true, strictScope: true });
188
+ const mine = store.save({ type: "pitfall", title: "mine", content: "c", agent_scope: "me", agent_scope_source: "explicit" });
189
+ const theirs = store.save({ type: "pitfall", title: "theirs", content: "c", agent_scope: "other", agent_scope_source: "explicit" });
190
+ const autoTheirs = store.save({ type: "pitfall", title: "auto-theirs", content: "c", agent_scope: "other", agent_scope_source: "auto" });
191
+ const get = pick("memory_get");
192
+
193
+ const out = await runHandler(get, { id: mine.id }, STRICT_EXEC);
194
+ assert.equal(out.memory.title, "mine");
195
+
196
+ // explicit 他 scope → 按不存在处理(无存在性泄漏)。
197
+ await assert.rejects(() => runHandler(get, { id: theirs.id }, STRICT_EXEC), /memory not found/);
198
+ // auto 他 scope → 可见(软加权不进硬过滤)。
199
+ const autoOut = await runHandler(get, { id: autoTheirs.id }, STRICT_EXEC);
200
+ assert.equal(autoOut.memory.title, "auto-theirs");
201
+ // 行主人自己照常可取。
202
+ const ownerOut = await runHandler(get, { id: theirs.id }, OTHER_EXEC);
203
+ assert.equal(ownerOut.memory.title, "theirs");
204
+ });
205
+
206
+ test("memory_get without strictScope returns any row regardless of scope annotation", async () => {
207
+ const { store, pick } = setupTools({ scopeEnabled: true });
208
+ const theirs = store.save({ type: "pitfall", title: "theirs", content: "c", agent_scope: "other", agent_scope_source: "explicit" });
209
+ const get = pick("memory_get");
210
+ const out = await runHandler(get, { id: theirs.id }, STRICT_EXEC);
211
+ assert.equal(out.memory.title, "theirs");
212
+ });
213
+
214
+ test("memory_list filters explicit rows and keeps total consistent under strictScope", async () => {
215
+ const { store, pick } = setupTools({ scopeEnabled: true, strictScope: true });
216
+ store.save({ type: "decision", title: "g", content: "x" });
217
+ store.save({ type: "decision", title: "mine", content: "x", agent_scope: "me", agent_scope_source: "explicit" });
218
+ store.save({ type: "decision", title: "theirs", content: "x", agent_scope: "other", agent_scope_source: "explicit" });
219
+ const list = pick("memory_list");
220
+
221
+ const out = await runHandler(list, { type: "decision" }, STRICT_EXEC);
222
+ assert.deepEqual(out.items.map((m) => m.title).sort(), ["g", "mine"]);
223
+ assert.equal(out.total, 2);
224
+
225
+ // flag 关(A2 默认):他 scope 的行照常出现在浏览视图。
226
+ const { store: store2, pick: pick2 } = setupTools({ scopeEnabled: true });
227
+ store2.save({ type: "decision", title: "theirs", content: "x", agent_scope: "other", agent_scope_source: "explicit" });
228
+ const out2 = await runHandler(pick2("memory_list"), { type: "decision" }, STRICT_EXEC);
229
+ assert.deepEqual(out2.items.map((m) => m.title), ["theirs"]);
230
+ assert.equal(out2.total, 1);
231
+ });
232
+
233
+ // --- entity: / attr: 前缀路的 scope 闸 ----------------------------------------
234
+ // 回归锁:这两条前缀路在 searchMemories 的入口就 return,早于融合池那道 strictScope
235
+ // 过滤,曾经整条绕过硬墙——显式他者 scope 的记忆用 `entity:` / `attr:` 就能原样读出,
236
+ // 而且是满分返回(连 A2 的 ×0.5 都没有)。所以除了「出局」,还要钉住「auto / 存量
237
+ // 他者保留」这条 A2 语义没有跟着塌掉。
238
+
239
+ /** 把若干记忆挂到同一个实体上。
240
+ * 属性键必须各不相同:同一 (实体, 键) 有时间轴语义,后写的一条会把前一条作废,
241
+ * 那样只有最后一条会被链接上(写这个测试时踩过,别改成同一个键)。 */
242
+ function linkToEntity(store, name, memories) {
243
+ const entity = store.createEntity({ name, type: "person" });
244
+ memories.forEach((mem, i) => {
245
+ store.saveAttr({ entity_id: entity.id, attr_key: `attr${i}`, attr_value: `value${i}`, memory_id: mem.id });
246
+ });
247
+ return entity;
248
+ }
249
+
250
+ /** 五条覆盖各种 scope 来源的记忆,供下面两条用。 */
251
+ function saveScopeBattery(store) {
252
+ return {
253
+ global: store.save({ type: "project", title: "global", content: "x" }),
254
+ mine: store.save({ type: "project", title: "mine", content: "x", agent_scope: "me", agent_scope_source: "explicit" }),
255
+ theirs: store.save({ type: "project", title: "theirs", content: "x", agent_scope: "other", agent_scope_source: "explicit" }),
256
+ autoForeign: store.save({ type: "project", title: "auto", content: "x", agent_scope: "other", agent_scope_source: "auto" }),
257
+ legacyForeign: store.save({ type: "project", title: "legacy", content: "x", agent_scope: "other" })
258
+ };
259
+ }
260
+
261
+ test("searchMemories entity: prefix honours the strictScope hard filter", async () => {
262
+ const { store, service } = setup({ scopeEnabled: true, strictScope: true, entitySearchEnabled: true });
263
+ const rows = saveScopeBattery(store);
264
+ linkToEntity(store, "阿尔托", Object.values(rows));
265
+
266
+ const asMe = await service.searchMemories("entity:阿尔托", { scope: { agent_scope: "me", workspace_scope: null } });
267
+ assert.deepEqual(asMe.map((m) => m.title).sort(), ["auto", "global", "legacy", "mine"], "显式他者出局,auto / 存量他者按 A2 保留");
268
+
269
+ // 原主人自己查:显式那条必须还在(否则上面是「谁都搜不到」的假绿)。
270
+ const asOther = await service.searchMemories("entity:阿尔托", { scope: { agent_scope: "other", workspace_scope: null } });
271
+ assert.ok(asOther.some((m) => m.title === "theirs"), "命中当前 agent 的显式行必须可见");
272
+ });
273
+
274
+ test("searchMemories attr: prefix honours the strictScope hard filter", async () => {
275
+ const { store, service } = setup({ scopeEnabled: true, strictScope: true, entitySearchEnabled: true });
276
+ const rows = saveScopeBattery(store);
277
+ // 每条挂到各自实体上、共用一个属性键:这样 attr:key=value 能一次覆盖全部五条
278
+ // (同实体同键会被时间轴作废,见 linkToEntity 的注释)。
279
+ Object.values(rows).forEach((mem, i) => {
280
+ const entity = store.createEntity({ name: `entity-${i}`, type: "person" });
281
+ store.saveAttr({ entity_id: entity.id, attr_key: "国籍", attr_value: "芬兰", memory_id: mem.id });
282
+ });
283
+
284
+ const asMe = await service.searchMemories("attr:国籍=芬兰", { scope: { agent_scope: "me", workspace_scope: null } });
285
+ assert.deepEqual(asMe.map((m) => m.title).sort(), ["auto", "global", "legacy", "mine"], "显式他者出局,auto / 存量他者按 A2 保留");
286
+
287
+ const asOther = await service.searchMemories("attr:国籍=芬兰", { scope: { agent_scope: "other", workspace_scope: null } });
288
+ assert.ok(asOther.some((m) => m.title === "theirs"), "命中当前 agent 的显式行必须可见");
289
+ });
290
+
291
+ test("entity: / attr: 的 scope 闸在触达之前:出局的行不被回温,也不 bump 关联边", async () => {
292
+ // 闸门若只加在「返回前」,出局的行仍会先被 touchRecalled 摸一遍——一次越权检索
293
+ // 照样给它刷回温时钟、并在被动确认开启时 bump 它的关联边。所以这条钉的是次序。
294
+ const { store, service } = setup({
295
+ scopeEnabled: true, strictScope: true, entitySearchEnabled: true,
296
+ heatEnabled: true, graphWeightEnabled: true, graphPassiveConfirm: true, graphWeightDelta: 0.1
297
+ });
298
+ const theirs = store.save({ type: "project", title: "theirs", content: "x", agent_scope: "other", agent_scope_source: "explicit" });
299
+ const global = store.save({ type: "project", title: "global", content: "x" });
300
+ linkToEntity(store, "阿尔托", [theirs, global]);
301
+
302
+ const a = store.createEntity({ name: "A", type: "technology" });
303
+ const b = store.createEntity({ name: "B", type: "technology" });
304
+ for (const mem of [theirs, global]) {
305
+ store.saveRelation({ from_entity: a.id, to_entity: b.id, relation_type: "uses", memory_id: mem.id, source: "llm" });
306
+ }
307
+
308
+ const rows = await service.searchMemories("entity:阿尔托", { scope: { agent_scope: "me", workspace_scope: null } });
309
+ assert.deepEqual(rows.map((m) => m.title), ["global"], "只剩可见那条");
310
+
311
+ // 正向对照:可见那条确实被触达了——否则下面两个「没被触达」可能是假绿。
312
+ assert.ok(store.getRelationsByMemory(global.id)[0].weight > 0.4, "可见行照常 bump 关联边");
313
+ assert.ok(store.getById(global.id).last_accessed_at, "可见行照常回温");
314
+
315
+ assert.equal(store.getRelationsByMemory(theirs.id)[0].weight, 0.4, "出局行不该 bump 关联边");
316
+ assert.equal(store.getById(theirs.id).last_accessed_at ?? null, null, "出局行不该被回温");
317
+ });
318
+
319
+ test("injectCandidates:strictScope 硬过滤必须晚于图召回合并(graphHint 不能把出局行带回来)", async () => {
320
+ // 回归锁(CodeRabbit on #371 的可达性分析,#17 A3 的图召回分支):entityRecall
321
+ // 本身不做 scope 门控,硬过滤原本长在图合并**之前**——过滤出局的显式他域行会
322
+ // 被图召回重新打上 graphHint 带回候选池,graphInjectHint 开时即作为线索行进入
323
+ // 注入块(宿主注入与 /context 同受影响)。硬过滤移到图合并之后、pin 池之前封死。
324
+ // 夹具注意:只挂 theirs 一条——挂满全 battery 会让每行都成 graphHint 行、被
325
+ // graphInjectBudget=1 的线索预算挤到失真(写这条测试时踩过)。
326
+ const { store, service } = setup({
327
+ scopeEnabled: true, strictScope: true, entityRecallEnabled: true, graphInjectHint: true
328
+ });
329
+ const rows = saveScopeBattery(store);
330
+ linkToEntity(store, "阿尔托", [rows.theirs]);
331
+
332
+ const asMe = service.injectCandidates({ query: "阿尔托", maxItems: 5, scope: { agent_scope: "me", workspace_scope: null } });
333
+ const titles = asMe.map((m) => m.title);
334
+ assert.ok(!titles.includes("theirs"), "图召回不得把显式他域行带进注入块");
335
+ assert.ok(titles.includes("global"), "未挂联的可见行照常经规则路进块(排除真空假绿)");
336
+
337
+ // 正向对照:原主人自己取,显式行仍在(作为线索行或普通行均可——否则是
338
+ // 「谁都看不到」的假绿)。
339
+ const asOther = service.injectCandidates({ query: "阿尔托", maxItems: 5, scope: { agent_scope: "other", workspace_scope: null } });
340
+ assert.ok(asOther.some((m) => m.title === "theirs"), "命中当前 agent 的显式行必须可见");
341
+ });
342
+
343
+ test("scope 闸先于 topK 截断:出局的候选不占名额(topK=1 仍有可见结果)", async () => {
344
+ // 回归锁(评审发现):先 slice 再 filter 的话,排在前面那条被闸掉的候选会占住唯一的
345
+ // 槽位,明明还有可见匹配却返回空数组。用 topK=1 把次序钉死——排序不动,只是闸门要插
346
+ // 在截断之前。
347
+ const me = { agent_scope: "me", workspace_scope: null };
348
+
349
+ const { store, service } = setup({ scopeEnabled: true, strictScope: true, entitySearchEnabled: true });
350
+ const theirsE = store.save({ type: "project", title: "theirs", content: "x", agent_scope: "other", agent_scope_source: "explicit" });
351
+ const mineE = store.save({ type: "project", title: "mine", content: "x", agent_scope: "me", agent_scope_source: "explicit" });
352
+ // 先挂 theirs、后挂 mine:前者排在候选前面,正好充当那个「占位」的候选。
353
+ linkToEntity(store, "阿尔托", [theirsE, mineE]);
354
+ const viaEntity = await service.searchMemories("entity:阿尔托", { topK: 1, scope: me });
355
+ assert.deepEqual(viaEntity.map((m) => m.title), ["mine"], "entity: 在 topK=1 下不该被出局候选挤空");
356
+
357
+ // attr: 同理——两条挂同一个属性键、各挂自己的实体,theirs 先写入。
358
+ const { store: s2, service: svc2 } = setup({ scopeEnabled: true, strictScope: true, entitySearchEnabled: true });
359
+ const theirsA = s2.save({ type: "project", title: "theirs", content: "x", agent_scope: "other", agent_scope_source: "explicit" });
360
+ const mineA = s2.save({ type: "project", title: "mine", content: "x", agent_scope: "me", agent_scope_source: "explicit" });
361
+ for (const [i, mem] of [theirsA, mineA].entries()) {
362
+ const entity = s2.createEntity({ name: `e${i}`, type: "person" });
363
+ s2.saveAttr({ entity_id: entity.id, attr_key: "国籍", attr_value: "芬兰", memory_id: mem.id });
364
+ }
365
+ const viaAttr = await svc2.searchMemories("attr:国籍=芬兰", { topK: 1, scope: me });
366
+ assert.deepEqual(viaAttr.map((m) => m.title), ["mine"], "attr: 在 topK=1 下不该被出局候选挤空");
367
+ });