@modusensus/dsh-mneme 0.8.3 → 0.8.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (205) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +727 -636
  3. package/bin/cli.mjs +0 -0
  4. package/bin/dsh-mneme-mcp.mjs +510 -0
  5. package/cordis.patch.yml +21 -21
  6. package/lib/api-standalone.js +506 -316
  7. package/lib/api.js +1403 -1316
  8. package/lib/bootstrap.js +211 -0
  9. package/lib/client.js +4657 -4366
  10. package/lib/commands.js +95 -95
  11. package/lib/config.js +615 -510
  12. package/lib/document.js +221 -0
  13. package/lib/dream/clustering.js +118 -118
  14. package/lib/dream/decisions.js +658 -658
  15. package/lib/dream/narratives.js +44 -0
  16. package/lib/dream/sleep.js +702 -700
  17. package/lib/dream.js +147 -16
  18. package/lib/embedding.js +177 -177
  19. package/lib/entities/extractor.js +249 -249
  20. package/lib/guide.js +44 -0
  21. package/lib/heat.js +144 -136
  22. package/lib/hot-memory.js +46 -46
  23. package/lib/index.js +515 -515
  24. package/lib/inject.js +375 -295
  25. package/lib/lang.js +208 -179
  26. package/lib/local-embedder.js +320 -320
  27. package/lib/mirror.js +198 -198
  28. package/lib/organize.js +450 -0
  29. package/lib/quality-filter.js +126 -126
  30. package/lib/recall-stats.js +125 -0
  31. package/lib/reranker.js +269 -269
  32. package/lib/runtime/adopt.js +176 -176
  33. package/lib/runtime/closure.js +172 -172
  34. package/lib/runtime/download.js +247 -247
  35. package/lib/runtime/layout.js +265 -235
  36. package/lib/runtime/loader.js +252 -249
  37. package/lib/runtime/model-fetch.js +361 -361
  38. package/lib/runtime/provision.js +237 -237
  39. package/lib/runtime/tarball.js +130 -130
  40. package/lib/runtime/verify.js +204 -191
  41. package/lib/scope.js +118 -118
  42. package/lib/search/adaptive.js +60 -22
  43. package/lib/search/bm25.js +96 -96
  44. package/lib/service.js +2324 -2087
  45. package/lib/settings.js +459 -420
  46. package/lib/store.js +2549 -2367
  47. package/lib/summarize.js +820 -449
  48. package/lib/tools.js +702 -574
  49. package/lib/vector-index.js +110 -110
  50. package/lib/version-check.js +122 -122
  51. package/package.json +109 -108
  52. package/runtime-manifest.json +526 -596
  53. package/scripts/benchmark-embed.js +201 -201
  54. package/scripts/benchmark-recall.js +199 -199
  55. package/scripts/benchmark-rerank.js +166 -166
  56. package/scripts/build-runtime-manifest.mjs +224 -217
  57. package/scripts/check-sync.js +42 -42
  58. package/scripts/e2e-dsh.js +220 -220
  59. package/scripts/mneme-runtime.mjs +277 -277
  60. package/scripts/stress-dsh.js +255 -255
  61. package/scripts/sync-lib.js +47 -47
  62. package/scripts/sync-test-badge.mjs +67 -61
  63. package/src/api-standalone.js +506 -316
  64. package/src/api.js +1403 -1316
  65. package/src/bootstrap.js +211 -0
  66. package/src/commands.js +95 -95
  67. package/src/config.js +615 -510
  68. package/src/document.js +221 -0
  69. package/src/dream/clustering.js +118 -118
  70. package/src/dream/decisions.js +658 -658
  71. package/src/dream/narratives.js +44 -0
  72. package/src/dream/sleep.js +702 -700
  73. package/src/dream.js +147 -16
  74. package/src/embedding.js +177 -177
  75. package/src/entities/extractor.js +249 -249
  76. package/src/guide.js +44 -0
  77. package/src/heat.js +144 -136
  78. package/src/hot-memory.js +46 -46
  79. package/src/index.js +515 -515
  80. package/src/inject.js +375 -295
  81. package/src/lang.js +208 -179
  82. package/src/local-embedder.js +320 -320
  83. package/src/mirror.js +198 -198
  84. package/src/organize.js +450 -0
  85. package/src/quality-filter.js +126 -126
  86. package/src/recall-stats.js +125 -0
  87. package/src/reranker.js +269 -269
  88. package/src/runtime/adopt.js +176 -176
  89. package/src/runtime/closure.js +172 -172
  90. package/src/runtime/download.js +247 -247
  91. package/src/runtime/layout.js +265 -235
  92. package/src/runtime/loader.js +252 -249
  93. package/src/runtime/model-fetch.js +361 -361
  94. package/src/runtime/provision.js +237 -237
  95. package/src/runtime/tarball.js +130 -130
  96. package/src/runtime/verify.js +204 -191
  97. package/src/scope.js +118 -118
  98. package/src/search/adaptive.js +60 -22
  99. package/src/search/bm25.js +96 -96
  100. package/src/service.js +2324 -2087
  101. package/src/settings.js +459 -420
  102. package/src/store.js +2549 -2367
  103. package/src/summarize.js +820 -449
  104. package/src/tools.js +702 -574
  105. package/src/vector-index.js +110 -110
  106. package/src/version-check.js +122 -122
  107. package/test/adaptive-inject-budget.test.js +85 -0
  108. package/test/api-routes-gaps.test.js +218 -218
  109. package/test/api.test.js +1640 -1482
  110. package/test/audit.test.js +448 -448
  111. package/test/benchmark.test.js +94 -94
  112. package/test/bootstrap.test.js +220 -0
  113. package/test/client.test.js +860 -736
  114. package/test/clustering.test.js +100 -100
  115. package/test/commands.test.js +106 -106
  116. package/test/config.test.js +66 -66
  117. package/test/conflict-freeze.test.js +300 -300
  118. package/test/document.test.js +523 -0
  119. package/test/dream-archive-guardrail.test.js +116 -116
  120. package/test/dream-candidate-hybrid.test.js +281 -281
  121. package/test/dream-coverage-degrade.test.js +86 -86
  122. package/test/dream.test.js +1308 -1254
  123. package/test/embedding-ready.test.js +196 -196
  124. package/test/entities.test.js +547 -547
  125. package/test/entity-recall.test.js +179 -0
  126. package/test/epistemic.test.js +298 -298
  127. package/test/fnew-0112.test.js +311 -311
  128. package/test/fnew-03.test.js +422 -422
  129. package/test/graph-api.test.js +175 -175
  130. package/test/heat.test.js +175 -148
  131. package/test/helpers/dream-mock.js +82 -82
  132. package/test/helpers/peer-worker.mjs +25 -25
  133. package/test/helpers/runtime-source.js +60 -60
  134. package/test/helpers/tar-builder.js +80 -80
  135. package/test/hot-memory.test.js +145 -145
  136. package/test/inject-content-cap.test.js +91 -0
  137. package/test/inject-first-round-bm25.test.js +89 -89
  138. package/test/inject-heat.test.js +61 -0
  139. package/test/inject-pools.test.js +170 -0
  140. package/test/inject-recall.test.js +105 -0
  141. package/test/inject-rotation.test.js +161 -161
  142. package/test/inject.test.js +283 -249
  143. package/test/lang.test.js +87 -87
  144. package/test/lib-smoke.test.js +96 -96
  145. package/test/llm-audit.test.js +462 -309
  146. package/test/llm-message-source.test.js +177 -177
  147. package/test/local-embedder.test.js +249 -249
  148. package/test/mcp-stdio.test.js +385 -0
  149. package/test/mirror-dirty.test.js +424 -424
  150. package/test/mirror-edit-digest.test.js +187 -187
  151. package/test/mirror-generation.test.js +499 -499
  152. package/test/mirror.test.js +249 -249
  153. package/test/narrative-bars.test.js +162 -0
  154. package/test/organize.test.js +418 -0
  155. package/test/peer-blockers.test.js +193 -193
  156. package/test/policy-epoch.test.js +259 -259
  157. package/test/quality-filter.test.js +186 -186
  158. package/test/reasoning-effort.test.js +746 -746
  159. package/test/recall-evals.test.js +235 -235
  160. package/test/recall-layer.test.js +326 -326
  161. package/test/recall-stats.test.js +123 -0
  162. package/test/receipt-chain.test.js +451 -451
  163. package/test/reflection.test.js +226 -226
  164. package/test/reindex-backfill.test.js +62 -62
  165. package/test/reranker-single-logit.test.js +73 -73
  166. package/test/reranker.test.js +197 -197
  167. package/test/resident-status.test.js +83 -0
  168. package/test/runtime-adopt.test.js +206 -206
  169. package/test/runtime-cli.test.js +89 -89
  170. package/test/runtime-closure.test.js +193 -193
  171. package/test/runtime-download.test.js +302 -302
  172. package/test/runtime-layout.test.js +163 -163
  173. package/test/runtime-loader.test.js +255 -237
  174. package/test/runtime-manifest.test.js +159 -158
  175. package/test/runtime-model-fetch.test.js +348 -348
  176. package/test/runtime-provision.test.js +101 -101
  177. package/test/runtime-tarball.test.js +81 -81
  178. package/test/runtime-verify.test.js +279 -214
  179. package/test/scope-candidates.test.js +239 -239
  180. package/test/scope-explicit.test.js +452 -452
  181. package/test/scope-retrieval.test.js +197 -197
  182. package/test/scope-storage.test.js +255 -255
  183. package/test/scope-strict.test.js +231 -231
  184. package/test/search-fusion.test.js +90 -90
  185. package/test/semantic.test.js +124 -124
  186. package/test/service-search.test.js +174 -174
  187. package/test/service.test.js +405 -405
  188. package/test/settings.test.js +261 -261
  189. package/test/skip-invalid-restore.test.js +262 -262
  190. package/test/sleep-action-set.test.js +422 -422
  191. package/test/sleep-heat.test.js +126 -125
  192. package/test/sleep-scheduler-rearm.test.js +89 -89
  193. package/test/sleep.test.js +410 -373
  194. package/test/standalone-api.test.js +544 -359
  195. package/test/store-vector-cache.test.js +98 -98
  196. package/test/store.test.js +396 -383
  197. package/test/stress.test.js +209 -209
  198. package/test/summarize.test.js +1288 -575
  199. package/test/tools-dto-schema-matrix.test.js +325 -0
  200. package/test/tools-memory-get-schema.test.js +65 -65
  201. package/test/tools.test.js +374 -341
  202. package/test/updated-at-semantics.test.js +113 -113
  203. package/test/vector-index.test.js +221 -221
  204. package/test/version-check.test.js +152 -152
  205. package/test/webServer-optional.test.js +82 -82
@@ -1,231 +1,231 @@
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
- });
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
+ });