@modusensus/dsh-mneme 0.11.0 → 0.12.0

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 (51) hide show
  1. package/README.md +10 -6
  2. package/docs/CONFIGURATION.md +2 -0
  3. package/lib/api.js +172 -6
  4. package/lib/client.js +619 -18
  5. package/lib/config.js +32 -1
  6. package/lib/document.js +9 -3
  7. package/lib/dream/clustering.js +6 -1
  8. package/lib/index.js +40 -4
  9. package/lib/inject.js +4 -2
  10. package/lib/lang.js +28 -0
  11. package/lib/profile-facts.js +356 -0
  12. package/lib/service.js +109 -4
  13. package/lib/settings.js +5 -0
  14. package/lib/store.js +483 -34
  15. package/lib/tools.js +140 -23
  16. package/lib/user-settings.js +63 -7
  17. package/lib/wiki/queries.js +149 -9
  18. package/package.json +1 -1
  19. package/scripts/sync-test-badge.mjs +1 -1
  20. package/scripts/test-count-sync.mjs +9 -5
  21. package/src/api.js +172 -6
  22. package/src/config.js +32 -1
  23. package/src/document.js +9 -3
  24. package/src/dream/clustering.js +6 -1
  25. package/src/index.js +40 -4
  26. package/src/inject.js +4 -2
  27. package/src/lang.js +28 -0
  28. package/src/profile-facts.js +356 -0
  29. package/src/service.js +109 -4
  30. package/src/settings.js +5 -0
  31. package/src/store.js +483 -34
  32. package/src/tools.js +140 -23
  33. package/src/user-settings.js +63 -7
  34. package/src/wiki/queries.js +149 -9
  35. package/test/api.test.js +3 -1
  36. package/test/client.test.js +147 -2
  37. package/test/config.test.js +79 -0
  38. package/test/document.test.js +32 -0
  39. package/test/embedding-cache-f32.test.js +276 -0
  40. package/test/inject-pin-explicit.test.js +300 -0
  41. package/test/inject-pin-vector-crowd.test.js +225 -0
  42. package/test/persona-settings.test.js +31 -0
  43. package/test/profile-facts.test.js +492 -0
  44. package/test/session-gate.test.js +1 -1
  45. package/test/test-count-sync.test.js +14 -3
  46. package/test/tool-state-ignore.test.js +65 -0
  47. package/test/tools-dto-schema-matrix.test.js +23 -2
  48. package/test/tools.test.js +144 -7
  49. package/test/wiki-api.test.js +121 -1
  50. package/test/wiki-queries.test.js +84 -1
  51. package/test/wiki-tools.test.js +131 -76
@@ -0,0 +1,300 @@
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
+
6
+ // #249 第二批(显式 pin)验收。判据从「类型是 constraint/preference」换成
7
+ // 「用户亲手钉在 pinned_memories 里」。
8
+ //
9
+ // 为什么需要这一层:旧的类型口径把 1163 条 constraint/preference 全当成 pin,
10
+ // 而 pin 预算最多 5 条,于是池子永远比预算大两个数量级——真正该每轮必进的
11
+ // 「提交署名硬约束」「拒绝谄媚」在 200 行规则窗口里排 133 / 157 名,就算封顶
12
+ // 和补位都修好也轮不到。根因是「type 就算 pin」这个隐含假设本身。
13
+ //
14
+ // 本文件按 Amarre 的验收清单逐条锁:
15
+ // ① 表空 → 回落旧类型口径(升级即等价,她钉第一条才切换)
16
+ // ② pin 一条**不在 200 行窗口内**的记忆仍能注入(补丁存在的理由本身)
17
+ // ③ pin 一条 **pattern** 类型能注入(写得最全的那版往往是 pattern)
18
+ // ④ pin 的行越权(strictScope 不通过)→ 不进块
19
+ // ⑤ pin 的行属暂停作用域 → 不进块
20
+ // ⑥ pinnedInjectBudget === 0 → shown=0 且注入序列与改前完全一致
21
+ // ⑦ 被 pin 的行 forgotten/archived → 不进块
22
+ //
23
+ // ②③ 的种子必须造 >200 条同 importance 的行,否则种子库窗口装得下、
24
+ // 用例假绿——「窗口外」这件事根本没被造出来。
25
+
26
+ function setup(config = {}) {
27
+ const store = createStore(":memory:");
28
+ const service = createService({ store, mirror: null, config });
29
+ return { store, service };
30
+ }
31
+
32
+ const titlesOf = (list) => list.map((m) => m.title);
33
+
34
+ // 造 n 条 importance=5 的普通条目。updated_at 递增,于是「最先造的」在
35
+ // store.list 的 (importance DESC, updated_at DESC, id) 序里排在最后,
36
+ // 200 行窗口装不下它——这就是「窗口外」的造法。
37
+ function seedCrowd(service, n, prefix = "普通条目") {
38
+ const ids = [];
39
+ for (let i = 0; i < n; i++) {
40
+ const m = service.saveWithDedupe({
41
+ type: "decision",
42
+ title: `${prefix}${i}`,
43
+ content: `第 ${i} 条与 pin 无关的普通决定,用来把窗口撑满`,
44
+ importance: 5
45
+ }).memory;
46
+ ids.push(m.id);
47
+ }
48
+ return ids;
49
+ }
50
+
51
+ function saveOne(service, { type = "preference", title, content, importance = 5, extra = {} }) {
52
+ return service.saveWithDedupe({ type, title, content, importance, ...extra }).memory;
53
+ }
54
+
55
+ test("表空时回落旧类型口径:pattern 不会被当成 pin", () => {
56
+ // 升级后 pinned_memories 是空表。若此时直接改读表,她现有的「按类型挑 5 条」
57
+ // 会静默降到 0 条——所以空表必须回落旧口径,升级即等价。
58
+ const { store, service } = setup({ maxInjectedItems: 5, pinnedInjectBudget: 5, importanceThreshold: 3 });
59
+ seedCrowd(service, 10);
60
+ saveOne(service, { type: "preference", title: "旧口径偏好", content: "类型即 pin 的旧口径下它该进块" });
61
+ // pattern 是旧口径的反证:它写得最全,但不在 PINNED_MEMORY_TYPES 里,
62
+ // 表空时不得被当成 pin。
63
+ saveOne(service, { type: "pattern", title: "旧口径模式", content: "pattern 不在旧口径的 pin 类型里" });
64
+
65
+ assert.equal(store.countPinned(), 0, "前提:pin 表是空的");
66
+
67
+ const pinnedStats = {};
68
+ const selected = service.injectCandidates({ query: "与 pin 无关的问题", maxItems: 5, pinnedStats });
69
+
70
+ assert.equal(pinnedStats.source, "type-fallback", "空表必须标成 type-fallback");
71
+ assert.equal(pinnedStats.outsideWindow, 0, "回落口径只能从窗口里取,不该有窗口外补位");
72
+ assert.equal(pinnedStats.shown, 1, "旧口径下只有那一条 preference 算 pin");
73
+ assert.ok(titlesOf(selected).slice(0, 1).includes("旧口径偏好"), "旧口径的 pin 仍要前置");
74
+ assert.ok(!titlesOf(selected).includes("旧口径模式"), "表空时 pattern 不得被当成 pin");
75
+ });
76
+
77
+ test("pin 一条不在 200 行窗口内的记忆仍能注入", () => {
78
+ // 这是整个第二层存在的理由:i=5 的 1143 条里 943 条(83%)进不了 200 行窗口,
79
+ // 靠相关性排序永远轮不到「署名」「拒绝谄媚」这类稳定偏好。
80
+ const { store, service } = setup({ maxInjectedItems: 5, pinnedInjectBudget: 5, importanceThreshold: 3 });
81
+ // 必须先造 200+ 条同 importance 的行,否则窗口装得下、这条用例假绿。
82
+ const crowd = seedCrowd(service, 220);
83
+ // crowd[0] 最早造 → updated_at 最小 → 排在 220 条的最后 → 在 200 行窗口外。
84
+ const target = crowd[0];
85
+
86
+ // 先证明它真的在窗口外:不改 pin 时它进不了候选池。
87
+ const before = service.injectCandidates({ query: "与 pin 无关的问题", maxItems: 5, pinnedStats: {} });
88
+ assert.ok(
89
+ !before.some((m) => m.id === target),
90
+ "前提:未 pin 时这条在 200 行窗口外,进不了注入块"
91
+ );
92
+
93
+ store.pinMemory(target);
94
+ assert.equal(store.countPinned(), 1);
95
+
96
+ const pinnedStats = {};
97
+ const after = service.injectCandidates({ query: "与 pin 无关的问题", maxItems: 5, pinnedStats });
98
+
99
+ assert.equal(pinnedStats.source, "explicit", "表非空 → explicit 口径");
100
+ assert.equal(pinnedStats.outsideWindow, 1, "这条是点查补回来的,必须计入 outsideWindow");
101
+ assert.equal(pinnedStats.shown, 1, "窗口外的 pin 也要占一个 pin 槽");
102
+ assert.ok(after.some((m) => m.id === target), "pin 一条窗口外的记忆,它必须真的进块");
103
+ assert.ok(titlesOf(after).slice(0, 1).includes("普通条目0"), `pin 前置到块首:${titlesOf(after).join(" / ")}`);
104
+ });
105
+
106
+ test("pin 一条 pattern 类型能注入", () => {
107
+ // 语义去重扫出 10 组,内容最全的那版 8 组是 pattern(「带学铁律」449 字含
108
+ // 「一次一个概念块」,限类型就只能拿 141 字的 constraint 残缺版)。
109
+ // 所以 explicit 口径的 filter 里不能出现 PINNED_MEMORY_TYPES。
110
+ const { store, service } = setup({ maxInjectedItems: 5, pinnedInjectBudget: 5, importanceThreshold: 3 });
111
+ seedCrowd(service, 10);
112
+ const m = saveOne(service, { type: "pattern", title: "带学铁律全版", content: "一次只讲一个概念块,讲完停下等她回应" });
113
+ store.pinMemory(m.id);
114
+
115
+ const pinnedStats = {};
116
+ const selected = service.injectCandidates({ query: "与 pin 无关的问题", maxItems: 5, pinnedStats });
117
+
118
+ assert.equal(pinnedStats.source, "explicit");
119
+ assert.equal(pinnedStats.shown, 1, "pattern 被显式钉住就该进 pin 池");
120
+ assert.ok(titlesOf(selected).includes("带学铁律全版"), `pattern 类型的 pin 必须进块:${titlesOf(selected).join(" / ")}`);
121
+ });
122
+
123
+ test("pin 的行越权时不得进块(strictScope)", () => {
124
+ // pin 不是绕过闸门的通道:补位与点查都发生在 strictScope 之前,硬墙照旧生效。
125
+ const { store, service } = setup({
126
+ maxInjectedItems: 5,
127
+ pinnedInjectBudget: 5,
128
+ importanceThreshold: 3,
129
+ strictScope: true
130
+ });
131
+ seedCrowd(service, 10);
132
+ const m = saveOne(service, {
133
+ title: "越权偏好",
134
+ content: "只属于 proj-a 的偏好,不该泄进 proj-b 的注入块",
135
+ extra: { workspace_scope: "proj-a", workspace_scope_source: "explicit" }
136
+ });
137
+ store.pinMemory(m.id);
138
+
139
+ const pinnedStats = {};
140
+ const selected = service.injectCandidates({
141
+ query: "与 pin 无关的问题",
142
+ maxItems: 5,
143
+ scope: { agent_scope: null, workspace_scope: "proj-b" },
144
+ pinnedStats
145
+ });
146
+
147
+ assert.equal(pinnedStats.source, "explicit", "前提:确实走了 explicit 口径");
148
+ assert.equal(pinnedStats.shown, 0, "被 strictScope 挡下的行不能算成已 pin");
149
+ assert.ok(!titlesOf(selected).includes("越权偏好"), `越权 pin 不得进块:${titlesOf(selected).join(" / ")}`);
150
+ });
151
+
152
+ test("pin 的行属暂停作用域时不得进块(scopeState)", () => {
153
+ const { store, service } = setup({
154
+ maxInjectedItems: 5,
155
+ pinnedInjectBudget: 5,
156
+ importanceThreshold: 3,
157
+ scopeState: { enabled: true, enforce: true }
158
+ });
159
+ seedCrowd(service, 10);
160
+ const m = saveOne(service, {
161
+ title: "暂停域偏好",
162
+ content: "属于 proj-a 的偏好,而 proj-a 此刻被按下",
163
+ extra: { workspace_scope: "proj-a", workspace_scope_source: "explicit" }
164
+ });
165
+ store.pinMemory(m.id);
166
+ service.setScopeState({ dimension: "workspace", scope_value: "proj-a", state: "paused" });
167
+
168
+ const pinnedStats = {};
169
+ const selected = service.injectCandidates({ query: "与 pin 无关的问题", maxItems: 5, pinnedStats });
170
+
171
+ assert.equal(pinnedStats.source, "explicit");
172
+ assert.equal(pinnedStats.shown, 0, "被暂停闸拦下的行不能算成已 pin");
173
+ assert.ok(!titlesOf(selected).includes("暂停域偏好"), `暂停作用域的 pin 不得进块:${titlesOf(selected).join(" / ")}`);
174
+ });
175
+
176
+ test("pinnedInjectBudget=0 时表里有 pin 也不得改变注入序列", () => {
177
+ // 关闭态必须与改动前逐字节一致:预算 0 时整段不执行——不读表、不改 candidates。
178
+ const { store, service } = setup({ maxInjectedItems: 5, pinnedInjectBudget: 0, importanceThreshold: 3 });
179
+ seedCrowd(service, 10);
180
+ const m = saveOne(service, { title: "被钉但预算关闭", content: "预算 0 时它不该被前置" });
181
+ store.pinMemory(m.id);
182
+ assert.equal(store.countPinned(), 1, "前提:表里确实有 pin 行");
183
+
184
+ const pinnedStats = {};
185
+ const selected = service.injectCandidates({ query: "与 pin 无关的问题", maxItems: 5, pinnedStats });
186
+
187
+ assert.equal(pinnedStats.shown, 0, "预算 0 → pin 池关闭");
188
+ assert.equal(pinnedStats.source, undefined, "关闭态不该标口径");
189
+ assert.equal(pinnedStats.outsideWindow, 0);
190
+ assert.equal(selected.length, 5, "槽位数不变");
191
+ assert.ok(!titlesOf(selected).includes("被钉但预算关闭"), "关闭态下 pin 不得被前置");
192
+ });
193
+
194
+ test("被 pin 的行 forgotten/archived 后不得进块", () => {
195
+ // pin 不是复活通道:点查取回的行照样要过遗忘/归档判定。
196
+ const { store, service } = setup({ maxInjectedItems: 5, pinnedInjectBudget: 5, importanceThreshold: 3 });
197
+ seedCrowd(service, 10);
198
+ const forgotten = saveOne(service, { title: "钉过又遗忘", content: "钉住之后被遗忘,不该再进块" });
199
+ const archived = saveOne(service, { title: "钉过又归档", content: "钉住之后被归档,不该再进块" });
200
+ store.pinMemory(forgotten.id);
201
+ store.pinMemory(archived.id);
202
+ service.setForget(forgotten.id, true);
203
+ service.setArchived(archived.id, true);
204
+
205
+ const pinnedStats = {};
206
+ const selected = service.injectCandidates({ query: "与 pin 无关的问题", maxItems: 5, pinnedStats });
207
+
208
+ assert.equal(pinnedStats.shown, 0, "遗忘/归档的 pin 不得占槽");
209
+ assert.ok(!titlesOf(selected).includes("钉过又遗忘"), "遗忘的 pin 不得进块");
210
+ assert.ok(!titlesOf(selected).includes("钉过又归档"), "归档的 pin 不得进块");
211
+ });
212
+
213
+ // --- 判据:看「能用的」而不是「表里有几行」 ---------------------------------
214
+ //
215
+ // 下面三条是同一个 bug 的三个面:pin 行会在 memories 那边被归档 / 遗忘 / 硬删,
216
+ // 而 pinned_memories 对这三种一无所知。若判据写成 countPinned() > 0,只要表里
217
+ // 还剩一行(哪怕那一行早已归档),pinMode 就永远钉在 explicit、eligible 恒空
218
+ // → pin 全灭且**永不回落**,无日志无报错。这正是这批改动要治的静默失败。
219
+
220
+ function pinTargets(service, titles) {
221
+ return titles.map((title) => saveOne(service, { title, content: `${title} 的正文` }));
222
+ }
223
+
224
+ test("钉过的行被归档后:回落旧口径,而不是 pin 全灭", () => {
225
+ const { store, service } = setup({ maxInjectedItems: 5, pinnedInjectBudget: 5, importanceThreshold: 3 });
226
+ seedCrowd(service, 10);
227
+ const legacy = saveOne(service, { type: "preference", title: "旧口径仍该进", content: "类型口径下它每轮前置" });
228
+ const [pinned] = pinTargets(service, ["钉过又归档"]);
229
+ store.pinMemory(pinned.id);
230
+ service.setArchived(pinned.id, true);
231
+
232
+ const pinnedStats = {};
233
+ const selected = service.injectCandidates({ query: "与 pin 无关的问题", maxItems: 5, pinnedStats });
234
+
235
+ assert.equal(store.countPinned(), 1, "前提:表里还留着那一行(归档不会删 pin 行)");
236
+ assert.equal(pinnedStats.source, "fallback-none-usable", "钉过但一条都用不了 → 必须留痕,不能静默");
237
+ assert.ok(titlesOf(selected).includes("旧口径仍该进"), `archived 掉唯一 pin 后应回落类型口径:${titlesOf(selected).join(" / ")}`);
238
+ assert.ok(!titlesOf(selected).includes("钉过又归档"), "归档的 pin 不得复活");
239
+ assert.ok(legacy.id);
240
+ });
241
+
242
+ test("钉过的行被遗忘后:同样回落,而不是卡死", () => {
243
+ const { store, service } = setup({ maxInjectedItems: 5, pinnedInjectBudget: 5, importanceThreshold: 3 });
244
+ seedCrowd(service, 10);
245
+ saveOne(service, { type: "preference", title: "旧口径仍该进", content: "类型口径下它每轮前置" });
246
+ const [pinned] = pinTargets(service, ["钉过又遗忘"]);
247
+ store.pinMemory(pinned.id);
248
+ service.setForget(pinned.id, true);
249
+
250
+ const pinnedStats = {};
251
+ const selected = service.injectCandidates({ query: "与 pin 无关的问题", maxItems: 5, pinnedStats });
252
+
253
+ assert.equal(pinnedStats.source, "fallback-none-usable");
254
+ assert.ok(titlesOf(selected).includes("旧口径仍该进"), `forgotten 掉唯一 pin 后应回落类型口径:${titlesOf(selected).join(" / ")}`);
255
+ assert.ok(!titlesOf(selected).includes("钉过又遗忘"), "遗忘的 pin 不得复活");
256
+ });
257
+
258
+ test("钉过的行被硬删后:没有可用行就回落,孤儿行还会被清掉", () => {
259
+ const { store, service } = setup({ maxInjectedItems: 5, pinnedInjectBudget: 5, importanceThreshold: 3 });
260
+ seedCrowd(service, 10);
261
+ saveOne(service, { type: "preference", title: "旧口径仍该进", content: "类型口径下它每轮前置" });
262
+ const [pinned] = pinTargets(service, ["钉过又被删"]);
263
+ store.pinMemory(pinned.id);
264
+ service.remove(pinned.id);
265
+
266
+ // remove() 走的清理路径要顺带清掉 pin 行(防攒垃圾);但即使没清,回落也必须
267
+ // 成立——判据看的是「能不能用」,不是「表里有没有」。
268
+ assert.equal(store.countPinned(), 0, "硬删记忆时要顺带清掉它的 pin 行,否则孤儿会越攒越多");
269
+
270
+ const pinnedStats = {};
271
+ const selected = service.injectCandidates({ query: "与 pin 无关的问题", maxItems: 5, pinnedStats });
272
+
273
+ assert.equal(pinnedStats.source, "type-fallback", "表被清空后就是「从没钉过」,走旧口径");
274
+ assert.ok(titlesOf(selected).includes("旧口径仍该进"), `全部 pin 失效后应回落类型口径:${titlesOf(selected).join(" / ")}`);
275
+ });
276
+
277
+ test("钉了多条、只有一部分还能用:留 explicit,只注入能用的那几条", () => {
278
+ // 关键取舍:不能因为「有 2 条归档」就整段回落到类型口径——那会突然冒出一批
279
+ // 她根本没钉过的条目,比少注入更糟。只要还有一条能用,口径就是 explicit。
280
+ const { store, service } = setup({ maxInjectedItems: 5, pinnedInjectBudget: 5, importanceThreshold: 3 });
281
+ seedCrowd(service, 10);
282
+ saveOne(service, { type: "preference", title: "不该冒出来的旧口径条目", content: "explicit 下它不该被当 pin" });
283
+ const [live, gone] = pinTargets(service, ["还活着的那条 pin", "已经归档的那条 pin"]);
284
+ store.pinMemory(live.id);
285
+ store.pinMemory(gone.id);
286
+ service.setArchived(gone.id, true);
287
+
288
+ const pinnedStats = {};
289
+ const selected = service.injectCandidates({ query: "与 pin 无关的问题", maxItems: 5, pinnedStats });
290
+
291
+ assert.equal(pinnedStats.source, "explicit", "还有一条能用 → 保持 explicit");
292
+ assert.equal(pinnedStats.shown, 1, "只注入能用的那一条");
293
+ assert.ok(titlesOf(selected).includes("还活着的那条 pin"), "能用的 pin 必须进块");
294
+ assert.ok(!titlesOf(selected).includes("已经归档的那条 pin"), "归档的 pin 不得进块");
295
+ assert.ok(
296
+ !titlesOf(selected).includes("不该冒出来的旧口径条目"),
297
+ "explicit 下不得回落类型口径——否则会冒出她没钉过的条目"
298
+ );
299
+ assert.ok(!titlesOf(selected).slice(0, 1).includes("不该冒出来的旧口径条目"));
300
+ });
@@ -0,0 +1,225 @@
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 { createVectorIndex } from "../src/vector-index.js";
6
+
7
+ // 回归(pin 池被向量路封顶饿死):`injectCandidates` 里 hybrid 分支先把
8
+ // `vectorIndex.search(qv, {limit: poolSize})` 的结果全量 push 进 merged,随后
9
+ // `if (merged.length >= poolSize) break;` 在**pin 筛选之前**就封顶——生产
10
+ // maxItems=5 → poolSize=10,向量路一次正好返回 10 条即 break,规则窗口里那些
11
+ // constraint/preference 一条都进不了 candidates,pin 池只能从这 10 条里筛,
12
+ // 命中几条纯看它们碰巧是什么类型。
13
+ //
14
+ // 代码注释向来自称「pin 不占 maxItems 名额」,实现里却把名额占掉了。实测真库
15
+ // maxItems=5 时 pin shown 只有 2/5,补回后恒 5/5;maxItems=10(poolSize=20)
16
+ // 时前后都是 5/5——所以修法不是多给名额,是把被向量路吃掉的 pin 候选补回来。
17
+ //
18
+ // 修法位置:不在 hybrid 分支之前提前摘(那样会绕过下面 strictScope 与
19
+ // scopeState 两道闸门,而 :1907 的注释明确写着「pin 池 eligible 取自过滤之后的
20
+ // 候选,顺带保证越权行进不了 pin」),而是在合并之后、闸门之前把被甩掉的 pin
21
+ // 类行补回候选池尾部——闸门照旧作用其上。
22
+ //
23
+ // 本文件锁三件事:
24
+ // ① 向量路把 poolSize 填满时,pin 池照旧拿满预算(缺口本体)
25
+ // ② 补回的是规则窗口(items)里的 pin,与向量命中集无关——即使查询向量与
26
+ // pin 条目毫不相干,它们也能进块(这是 pin 的定义:每轮必进)
27
+ // ③ 预算关时行为逐字节不变
28
+
29
+ function setup(config = {}) {
30
+ const store = createStore(":memory:");
31
+ const service = createService({ store, mirror: null, config });
32
+ const vectorIndex = createVectorIndex({ store });
33
+ service.setVectorIndex(vectorIndex);
34
+ return { store, service, vectorIndex };
35
+ }
36
+
37
+ // 向量路上与查询完全同向的普通条目:正好填满 poolSize 个名额,且一条都不是 pin 类型。
38
+ function seedVectorCrowd(service, vectorIndex, n) {
39
+ const ids = [];
40
+ for (let i = 0; i < n; i++) {
41
+ const m = service.saveWithDedupe({
42
+ type: "decision",
43
+ title: `决定条目${i}`,
44
+ content: `第 ${i} 条与查询同向的普通决定,不该占掉 pin 的名额`,
45
+ importance: 5
46
+ }).memory;
47
+ vectorIndex.saveEmbedding(m.id, [1, 0, 0]);
48
+ ids.push(m.id);
49
+ }
50
+ return ids;
51
+ }
52
+
53
+ // 与查询向量正交的 pin 条目:向量路永远检索不到它们,只能靠规则窗口进块。
54
+ function seedPinnedPreferences(service, vectorIndex, n) {
55
+ const ids = [];
56
+ for (let i = 0; i < n; i++) {
57
+ const m = service.saveWithDedupe({
58
+ type: "preference",
59
+ title: `硬偏好${i}`,
60
+ content: `第 ${i} 条约束/偏好,每轮必进,与查询向量正交`,
61
+ importance: 5
62
+ }).memory;
63
+ vectorIndex.saveEmbedding(m.id, [0, 1, 0]);
64
+ ids.push(m.id);
65
+ }
66
+ return ids;
67
+ }
68
+
69
+ const titlesOf = (list) => list.map((m) => m.title);
70
+
71
+ test("pin 池不被向量路封顶:poolSize 被填满时仍拿满 pinnedInjectBudget", () => {
72
+ // maxItems=5 → poolSize=10,与生产默认值一致;预算是 5,满额。
73
+ const { service, vectorIndex } = setup({
74
+ maxInjectedItems: 5,
75
+ pinnedInjectBudget: 5,
76
+ importanceThreshold: 3
77
+ });
78
+ seedVectorCrowd(service, vectorIndex, 10); // 正好 poolSize 条,全是 decision
79
+ seedPinnedPreferences(service, vectorIndex, 5);
80
+
81
+ const pinnedStats = {};
82
+ const selected = service.injectCandidates({
83
+ query: "随便问一个与偏好无关的问题",
84
+ maxItems: 5,
85
+ queryVector: [1, 0, 0],
86
+ pinnedStats
87
+ });
88
+
89
+ // 缺口本体的断言:修前这里是 0(向量路 10 条里一条 preference 都没有)。
90
+ assert.equal(pinnedStats.shown, 5, "pin 池必须拿满预算,不能被向量路的 poolSize 截断");
91
+ const pinnedTitles = titlesOf(selected).filter((t) => t.startsWith("硬偏好"));
92
+ assert.equal(pinnedTitles.length, 5, "5 条 pin 必须真的进块(不只是计数对)");
93
+ // pin 前置在块内:前 5 位必须全是 pin 条目(同 importance/updated_at 的
94
+ // 同批条目内部次序由 id 决定,不是本用例要锁的东西,故只断言集合)。
95
+ const head = titlesOf(selected).slice(0, 5);
96
+ assert.ok(head.every((t) => t.startsWith("硬偏好")), `pin 前置到块内排序之前:${head.join(" / ")}`);
97
+ assert.deepEqual([...head].sort(), [...pinnedTitles].sort(), "前 5 位就是那 5 条 pin");
98
+ });
99
+
100
+ test("pin 池取自规则窗口,不取自被封顶的 candidates", () => {
101
+ // 这条锁的是「怎么修」而不是「修没修」:向量路返回的 10 条与 pin 类型完全
102
+ // 不相交,若 eligible 仍从 candidates 过滤,pin 恒为 0——无论 poolSize 多大。
103
+ const { service, vectorIndex } = setup({
104
+ maxInjectedItems: 2,
105
+ pinnedInjectBudget: 2,
106
+ importanceThreshold: 3
107
+ });
108
+ seedVectorCrowd(service, vectorIndex, 4); // poolSize = maxItems*2 = 4,正好填满
109
+ seedPinnedPreferences(service, vectorIndex, 2);
110
+
111
+ const pinnedStats = {};
112
+ const selected = service.injectCandidates({
113
+ query: "另一个与偏好无关的问题",
114
+ maxItems: 2,
115
+ queryVector: [1, 0, 0],
116
+ pinnedStats
117
+ });
118
+
119
+ assert.equal(pinnedStats.shown, 2, "maxItems=2 时 pin 也要拿满 2 条");
120
+ const head = titlesOf(selected).slice(0, 2);
121
+ assert.ok(
122
+ head.every((t) => t.startsWith("硬偏好")),
123
+ `pin 从规则窗口取,与向量命中集无关:${head.join(" / ")}`
124
+ );
125
+ });
126
+
127
+ test("pin 池关闭时行为不变:pinnedInjectBudget=0 不得凭空引入偏好", () => {
128
+ // 关闭态必须与改动前逐字节一致——补回的 pin 只在预算 > 0 时发生,
129
+ // 预算关时 candidates 与改前完全一样。
130
+ const { service, vectorIndex } = setup({
131
+ maxInjectedItems: 5,
132
+ pinnedInjectBudget: 0,
133
+ importanceThreshold: 3
134
+ });
135
+ seedVectorCrowd(service, vectorIndex, 10);
136
+ seedPinnedPreferences(service, vectorIndex, 5);
137
+
138
+ const pinnedStats = {};
139
+ const selected = service.injectCandidates({
140
+ query: "又一个与偏好无关的问题",
141
+ maxItems: 5,
142
+ queryVector: [1, 0, 0],
143
+ pinnedStats
144
+ });
145
+
146
+ assert.equal(pinnedStats.shown, 0, "预算 0 → pin 池关闭");
147
+ assert.equal(selected.length, 5, "槽位数不变");
148
+ assert.ok(
149
+ titlesOf(selected).every((t) => t.startsWith("决定条目")),
150
+ "关闭态下 preference 不得被前置(顺序与改动前一致)"
151
+ );
152
+ });
153
+
154
+ // 安全底线:补回 pin 是在 strictScope 与 scopeState 两道闸门**之前**做的,
155
+ // 所以这两道闸必须照旧能拦住被补回来的 pin 行。service.js 里那条位置纪律
156
+ // (「pin 池 eligible 取自过滤之后的候选,顺带保证越权行进不了 pin」)就是靠
157
+ // 这两条用例守住的——不然「补回」会顺带开一个越权口子。
158
+ test("补回的 pin 仍受 strictScope 硬过滤:越权偏好不得因补回而进块", () => {
159
+ const { store, service, vectorIndex } = setup({
160
+ maxInjectedItems: 5,
161
+ pinnedInjectBudget: 5,
162
+ importanceThreshold: 3,
163
+ strictScope: true
164
+ });
165
+ seedVectorCrowd(service, vectorIndex, 10);
166
+ // 一条属于 proj-a 的偏好:调用方 scope 是 proj-b 时它必须进不来。
167
+ const m = service.saveWithDedupe({
168
+ type: "preference",
169
+ title: "越权偏好",
170
+ content: "只属于 proj-a 的偏好,不该泄进 proj-b 的注入块",
171
+ importance: 5,
172
+ workspace_scope: "proj-a",
173
+ workspace_scope_source: "explicit"
174
+ }).memory;
175
+ vectorIndex.saveEmbedding(m.id, [0, 1, 0]);
176
+
177
+ const pinnedStats = {};
178
+ const selected = service.injectCandidates({
179
+ query: "与偏好无关的问题",
180
+ maxItems: 5,
181
+ queryVector: [1, 0, 0],
182
+ scope: { agent_scope: null, workspace_scope: "proj-b" },
183
+ pinnedStats
184
+ });
185
+
186
+ assert.ok(
187
+ !titlesOf(selected).includes("越权偏好"),
188
+ `strictScope 必须挡住被补回的越权 pin:${titlesOf(selected).join(" / ")}`
189
+ );
190
+ assert.equal(pinnedStats.shown, 0, "被 scope 挡下的行不能算成已 pin");
191
+ });
192
+
193
+ test("补回的 pin 仍受 scopeState 拦截档约束:暂停作用域的偏好不得进块", () => {
194
+ const { service, vectorIndex } = setup({
195
+ maxInjectedItems: 5,
196
+ pinnedInjectBudget: 5,
197
+ importanceThreshold: 3,
198
+ scopeState: { enabled: true, enforce: true }
199
+ });
200
+ seedVectorCrowd(service, vectorIndex, 10);
201
+ const m = service.saveWithDedupe({
202
+ type: "preference",
203
+ title: "暂停域偏好",
204
+ content: "属于 proj-a 的偏好,而 proj-a 此刻被按下",
205
+ importance: 5,
206
+ workspace_scope: "proj-a",
207
+ workspace_scope_source: "explicit"
208
+ }).memory;
209
+ vectorIndex.saveEmbedding(m.id, [0, 1, 0]);
210
+ service.setScopeState({ dimension: "workspace", scope_value: "proj-a", state: "paused" });
211
+
212
+ const pinnedStats = {};
213
+ const selected = service.injectCandidates({
214
+ query: "与偏好无关的问题",
215
+ maxItems: 5,
216
+ queryVector: [1, 0, 0],
217
+ pinnedStats
218
+ });
219
+
220
+ assert.ok(
221
+ !titlesOf(selected).includes("暂停域偏好"),
222
+ `scopeState 拦截档必须挡住被补回的 pin:${titlesOf(selected).join(" / ")}`
223
+ );
224
+ assert.equal(pinnedStats.shown, 0, "被闸门拦下的行不能算成已 pin");
225
+ });
@@ -165,3 +165,34 @@ test("personaEnabled is in the feature-flag boolean whitelist", () => {
165
165
  assert.throws(() => settings.setFeatureFlags({ notARealFlag: true }), TypeError);
166
166
  store.close();
167
167
  });
168
+
169
+ test("strict 寻址:一张身份卡都没建时仍回落 default(#587 的例外)", () => {
170
+ // 两种处境必须分开:
171
+ // A) 建了身份卡、只是没这一张 → strict 下不注入(见上一条用例,行为不变)
172
+ // B) 一张身份卡都没建 → default 是用户手里唯一那张卡,strict 不许把它关死
173
+ // DS Bot 这类宿主里所有 bot 会话共用 agentPreset "standard",用户按 bot 名字建卡
174
+ // 永远不命中,于是必然落进 B——此时按 none 处理等于把唯一那张卡静默废掉。
175
+ const { store, settings } = setup();
176
+ settings.setPersona({ name: "Amarre", enabled: true });
177
+ assert.deepEqual(settings.getPersona().byAgent, {}, "前提:byAgent 整体为空");
178
+ const { card, source } = resolvePersonaCard(settings, "standard", { strict: true });
179
+ assert.equal(source, "default", "一张卡都没建时严格寻址仍回落 default");
180
+ assert.equal(card.name, "Amarre");
181
+ // 建了任意一张身份卡之后,例外立即失效——兜底退回严格语义
182
+ settings.setPersonaFor("zephyr", { name: "泽菲尔", enabled: true });
183
+ assert.equal(resolvePersonaCard(settings, "standard", { strict: true }).source, "none");
184
+ // 而这张卡自己仍然命中
185
+ assert.equal(resolvePersonaCard(settings, "zephyr", { strict: true }).source, "byAgent");
186
+ store.close();
187
+ });
188
+
189
+ test("strict 寻址:身份卡存在但全被关掉,仍算「建过卡」——例外只看有没有键", () => {
190
+ // 例外判的是 byAgent 的键数,不是「有没有可用的卡」:一张 enabled=false 的卡
191
+ // 也算「用户已经做过分区」,此时不该再拿 default 去兜。这条锁住判据不会被
192
+ // 改成「有可用卡才算建过」,否则关掉唯一那张身份卡会重新落回 default。
193
+ const { store, settings } = setup();
194
+ settings.setPersona({ name: "Amarre", enabled: true });
195
+ settings.setPersonaFor("zephyr", { name: "泽菲尔", enabled: false });
196
+ assert.equal(resolvePersonaCard(settings, "standard", { strict: true }).source, "none");
197
+ store.close();
198
+ });