@modusensus/dsh-mneme 0.8.14 → 0.8.16

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 (279) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +660 -654
  3. package/assets/icon.svg +3 -3
  4. package/bin/cli.mjs +691 -689
  5. package/bin/dsh-mneme-mcp.mjs +510 -510
  6. package/bin/dsh-mneme-serve.mjs +151 -151
  7. package/cordis.patch.yml +21 -21
  8. package/docs/CONFIGURATION.md +275 -0
  9. package/docs/CONTINUITY.md +124 -0
  10. package/docs/DAEMON.md +46 -0
  11. package/docs/DISTILL.md +162 -0
  12. package/docs/ENTITIES.md +245 -0
  13. package/docs/LOCAL_MODEL.md +222 -0
  14. package/docs/SEMANTIC.md +256 -0
  15. package/docs/SLEEP.md +164 -0
  16. package/docs/STORAGE.md +98 -0
  17. package/lib/api-standalone.js +656 -673
  18. package/lib/api.js +1605 -1433
  19. package/lib/bootstrap.js +211 -211
  20. package/lib/client.js +6354 -5296
  21. package/lib/commands.js +95 -95
  22. package/lib/config.js +947 -843
  23. package/lib/content-hash.js +54 -54
  24. package/lib/continuity.js +220 -220
  25. package/lib/document-index.js +94 -94
  26. package/lib/document.js +303 -303
  27. package/lib/dream/clustering.js +118 -118
  28. package/lib/dream/decisions.js +694 -674
  29. package/lib/dream/narratives.js +44 -44
  30. package/lib/dream/sleep.js +819 -819
  31. package/lib/dream.js +1503 -1501
  32. package/lib/embedding.js +177 -177
  33. package/lib/entities/extractor.js +252 -252
  34. package/lib/graph/anchoring.js +179 -179
  35. package/lib/guide.js +74 -74
  36. package/lib/heat.js +144 -144
  37. package/lib/hot-memory.js +108 -46
  38. package/lib/index.js +552 -503
  39. package/lib/inject.js +428 -450
  40. package/lib/lang.js +248 -229
  41. package/lib/local-embedder.js +358 -358
  42. package/lib/maintenance.js +162 -142
  43. package/lib/mirror.js +330 -330
  44. package/lib/organize.js +450 -450
  45. package/lib/peak-hours.js +117 -117
  46. package/lib/placement.js +13 -0
  47. package/lib/pre-inject-gate.js +340 -0
  48. package/lib/quality-filter.js +126 -126
  49. package/lib/recall-stats.js +190 -175
  50. package/lib/reclaim-http.js +150 -0
  51. package/lib/reranker.js +269 -269
  52. package/lib/runtime/adopt.js +176 -176
  53. package/lib/runtime/closure.js +172 -172
  54. package/lib/runtime/download.js +247 -247
  55. package/lib/runtime/layout.js +265 -265
  56. package/lib/runtime/loader.js +252 -252
  57. package/lib/runtime/model-fetch.js +361 -361
  58. package/lib/runtime/provision.js +237 -237
  59. package/lib/runtime/tarball.js +130 -130
  60. package/lib/runtime/verify.js +223 -204
  61. package/lib/safe-slice.js +18 -0
  62. package/lib/scope.js +146 -146
  63. package/lib/search/adaptive.js +60 -60
  64. package/lib/search/bm25.js +96 -96
  65. package/lib/semantic.js +229 -229
  66. package/lib/sensitive-scan.js +209 -209
  67. package/lib/serve.js +215 -215
  68. package/lib/service.js +2688 -2605
  69. package/lib/settings.js +528 -511
  70. package/lib/store.js +3196 -3007
  71. package/lib/summarize.js +1039 -856
  72. package/lib/tools.js +743 -737
  73. package/lib/vector-index.js +110 -110
  74. package/lib/version-check.js +122 -122
  75. package/lib/write-admission.js +402 -402
  76. package/locale/en.json +6 -6
  77. package/locale/zh.json +6 -6
  78. package/package.json +118 -118
  79. package/runtime-manifest.json +526 -526
  80. package/scripts/benchmark-embed.js +201 -201
  81. package/scripts/benchmark-recall.js +249 -249
  82. package/scripts/benchmark-rerank.js +166 -166
  83. package/scripts/build-runtime-manifest.mjs +224 -224
  84. package/scripts/changelog-prep.mjs +36 -36
  85. package/scripts/check-sync.js +42 -42
  86. package/scripts/e2e-dsh.js +220 -220
  87. package/scripts/mneme-runtime.mjs +277 -277
  88. package/scripts/stress-dsh.js +255 -255
  89. package/scripts/sync-lib.js +47 -47
  90. package/scripts/sync-test-badge.mjs +69 -69
  91. package/scripts/test-count-sync.mjs +44 -44
  92. package/src/api-standalone.js +656 -673
  93. package/src/api.js +1605 -1433
  94. package/src/bootstrap.js +211 -211
  95. package/src/commands.js +95 -95
  96. package/src/config.js +947 -843
  97. package/src/content-hash.js +54 -54
  98. package/src/continuity.js +220 -220
  99. package/src/document-index.js +94 -94
  100. package/src/document.js +303 -303
  101. package/src/dream/clustering.js +118 -118
  102. package/src/dream/decisions.js +694 -674
  103. package/src/dream/narratives.js +44 -44
  104. package/src/dream/sleep.js +819 -819
  105. package/src/dream.js +1503 -1501
  106. package/src/embedding.js +177 -177
  107. package/src/entities/extractor.js +252 -252
  108. package/src/graph/anchoring.js +179 -179
  109. package/src/guide.js +74 -74
  110. package/src/heat.js +144 -144
  111. package/src/hot-memory.js +108 -46
  112. package/src/index.js +552 -503
  113. package/src/inject.js +428 -450
  114. package/src/lang.js +248 -229
  115. package/src/local-embedder.js +358 -358
  116. package/src/maintenance.js +162 -142
  117. package/src/mirror.js +330 -330
  118. package/src/organize.js +450 -450
  119. package/src/peak-hours.js +117 -117
  120. package/src/placement.js +13 -0
  121. package/src/pre-inject-gate.js +340 -0
  122. package/src/quality-filter.js +126 -126
  123. package/src/recall-stats.js +190 -175
  124. package/src/reclaim-http.js +150 -0
  125. package/src/reranker.js +269 -269
  126. package/src/runtime/adopt.js +176 -176
  127. package/src/runtime/closure.js +172 -172
  128. package/src/runtime/download.js +247 -247
  129. package/src/runtime/layout.js +265 -265
  130. package/src/runtime/loader.js +252 -252
  131. package/src/runtime/model-fetch.js +361 -361
  132. package/src/runtime/provision.js +237 -237
  133. package/src/runtime/tarball.js +130 -130
  134. package/src/runtime/verify.js +223 -204
  135. package/src/safe-slice.js +18 -0
  136. package/src/scope.js +146 -146
  137. package/src/search/adaptive.js +60 -60
  138. package/src/search/bm25.js +96 -96
  139. package/src/semantic.js +229 -229
  140. package/src/sensitive-scan.js +209 -209
  141. package/src/serve.js +215 -215
  142. package/src/service.js +2688 -2605
  143. package/src/settings.js +528 -511
  144. package/src/store.js +3196 -3007
  145. package/src/summarize.js +1039 -856
  146. package/src/tools.js +743 -737
  147. package/src/vector-index.js +110 -110
  148. package/src/version-check.js +122 -122
  149. package/src/write-admission.js +402 -402
  150. package/test/adaptive-inject-budget.test.js +85 -85
  151. package/test/api-routes-gaps.test.js +899 -218
  152. package/test/api.test.js +1725 -1690
  153. package/test/audit.test.js +448 -448
  154. package/test/baseline-persist.test.js +248 -248
  155. package/test/benchmark.test.js +94 -94
  156. package/test/bootstrap.test.js +220 -220
  157. package/test/changelog-prep.test.js +60 -60
  158. package/test/client.test.js +1582 -1169
  159. package/test/clustering.test.js +100 -100
  160. package/test/commands.test.js +106 -106
  161. package/test/config.test.js +66 -66
  162. package/test/conflict-freeze.test.js +300 -300
  163. package/test/content-hash.test.js +204 -204
  164. package/test/continuity.test.js +333 -333
  165. package/test/contrast.test.js +314 -0
  166. package/test/document-dir.test.js +233 -233
  167. package/test/document.test.js +671 -671
  168. package/test/dream-archive-guardrail.test.js +116 -116
  169. package/test/dream-candidate-hybrid.test.js +281 -281
  170. package/test/dream-coverage-degrade.test.js +86 -86
  171. package/test/dream-failure-backoff.test.js +173 -173
  172. package/test/dream-merge-guardrail.test.js +219 -0
  173. package/test/dream-peak-hours.test.js +194 -194
  174. package/test/dream.test.js +1385 -1383
  175. package/test/embedding-ready.test.js +196 -196
  176. package/test/entities.test.js +627 -547
  177. package/test/entity-recall.test.js +179 -179
  178. package/test/epistemic.test.js +325 -325
  179. package/test/fnew-0112.test.js +311 -311
  180. package/test/fnew-03.test.js +422 -422
  181. package/test/graph-anchoring.test.js +184 -184
  182. package/test/graph-api.test.js +175 -175
  183. package/test/graph-inject-budget.test.js +103 -103
  184. package/test/graph-passive-confirm.test.js +91 -91
  185. package/test/graph-weight-evolution.test.js +104 -104
  186. package/test/heat.test.js +175 -175
  187. package/test/helpers/dream-mock.js +82 -82
  188. package/test/helpers/peer-worker.mjs +25 -25
  189. package/test/helpers/runtime-source.js +60 -60
  190. package/test/helpers/tar-builder.js +80 -80
  191. package/test/helpers/write-admission-samples.js +308 -308
  192. package/test/hot-memory.test.js +211 -145
  193. package/test/inject-content-cap.test.js +91 -91
  194. package/test/inject-first-round-bm25.test.js +89 -89
  195. package/test/inject-heat.test.js +77 -77
  196. package/test/inject-parent-gate.test.js +106 -106
  197. package/test/inject-pools.test.js +194 -194
  198. package/test/inject-recall.test.js +144 -105
  199. package/test/inject-rotation.test.js +161 -161
  200. package/test/inject.test.js +443 -420
  201. package/test/lang.test.js +87 -87
  202. package/test/lastrunat-persist.test.js +101 -101
  203. package/test/lib-smoke.test.js +96 -96
  204. package/test/llm-audit.test.js +696 -598
  205. package/test/llm-message-source.test.js +207 -207
  206. package/test/local-embedder.test.js +292 -292
  207. package/test/maintenance.test.js +501 -391
  208. package/test/mcp-stdio.test.js +385 -385
  209. package/test/mirror-dirty.test.js +424 -424
  210. package/test/mirror-edit-digest.test.js +187 -187
  211. package/test/mirror-generation.test.js +499 -499
  212. package/test/mirror-types.test.js +208 -208
  213. package/test/mirror.test.js +249 -249
  214. package/test/narrative-bars.test.js +162 -162
  215. package/test/organize.test.js +418 -418
  216. package/test/peer-blockers.test.js +193 -193
  217. package/test/placement.test.js +36 -0
  218. package/test/policy-epoch.test.js +259 -259
  219. package/test/pre-inject-gate.test.js +444 -0
  220. package/test/quality-filter.test.js +186 -186
  221. package/test/reasoning-effort.test.js +752 -752
  222. package/test/recall-anti-update.test.js +82 -82
  223. package/test/recall-evals.test.js +253 -253
  224. package/test/recall-layer.test.js +326 -326
  225. package/test/recall-stats.test.js +274 -189
  226. package/test/receipt-chain.test.js +451 -451
  227. package/test/reclaim-http.test.js +209 -0
  228. package/test/reflection.test.js +226 -226
  229. package/test/reindex-backfill.test.js +62 -62
  230. package/test/reranker-single-logit.test.js +73 -73
  231. package/test/reranker.test.js +197 -197
  232. package/test/resident-status.test.js +83 -83
  233. package/test/runtime-adopt.test.js +206 -206
  234. package/test/runtime-cli.test.js +89 -89
  235. package/test/runtime-closure.test.js +193 -193
  236. package/test/runtime-download.test.js +302 -302
  237. package/test/runtime-layout.test.js +163 -163
  238. package/test/runtime-loader.test.js +255 -255
  239. package/test/runtime-manifest.test.js +159 -159
  240. package/test/runtime-model-fetch.test.js +348 -348
  241. package/test/runtime-provision.test.js +101 -101
  242. package/test/runtime-tarball.test.js +81 -81
  243. package/test/runtime-verify.test.js +319 -279
  244. package/test/scope-candidates.test.js +239 -239
  245. package/test/scope-explicit.test.js +452 -452
  246. package/test/scope-inject-soft.test.js +106 -106
  247. package/test/scope-retrieval.test.js +197 -197
  248. package/test/scope-storage.test.js +255 -255
  249. package/test/scope-strict.test.js +367 -367
  250. package/test/search-fusion.test.js +90 -90
  251. package/test/semantic.test.js +124 -124
  252. package/test/sensitive-scan.test.js +207 -207
  253. package/test/serve-bin.test.js +121 -121
  254. package/test/serve.test.js +196 -196
  255. package/test/service-search.test.js +174 -174
  256. package/test/service.test.js +410 -405
  257. package/test/settings.test.js +269 -261
  258. package/test/skip-invalid-restore.test.js +262 -262
  259. package/test/sleep-action-set.test.js +422 -422
  260. package/test/sleep-heat.test.js +126 -126
  261. package/test/sleep-scheduler-rearm.test.js +89 -89
  262. package/test/sleep.test.js +527 -527
  263. package/test/standalone-api.test.js +751 -751
  264. package/test/store-vector-cache.test.js +98 -98
  265. package/test/store.test.js +504 -396
  266. package/test/stress.test.js +209 -209
  267. package/test/summarize-occurred-at.test.js +127 -0
  268. package/test/summarize-reasoning-effort.test.js +189 -189
  269. package/test/summarize.test.js +1639 -1465
  270. package/test/supersede-state-tracking.test.js +394 -0
  271. package/test/test-count-sync.test.js +60 -60
  272. package/test/tools-dto-schema-matrix.test.js +325 -325
  273. package/test/tools-memory-get-schema.test.js +65 -65
  274. package/test/tools.test.js +428 -374
  275. package/test/updated-at-semantics.test.js +113 -113
  276. package/test/vector-index.test.js +221 -221
  277. package/test/version-check.test.js +152 -152
  278. package/test/webServer-optional.test.js +82 -82
  279. package/test/write-admission.test.js +668 -668
@@ -1,367 +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("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
- });
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
+ });