@modusensus/dsh-mneme 0.8.3 → 0.8.4

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