@modusensus/dsh-mneme 0.6.8 → 0.6.10

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 (117) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +468 -463
  3. package/cordis.patch.yml +15 -15
  4. package/lib/api.js +783 -783
  5. package/lib/client.js +1754 -1757
  6. package/lib/commands.js +64 -64
  7. package/lib/config.js +298 -288
  8. package/lib/dream/clustering.js +118 -118
  9. package/lib/dream/decisions.js +488 -439
  10. package/lib/dream/sleep.js +561 -561
  11. package/lib/dream/tag-extractor.js +156 -156
  12. package/lib/dream.js +958 -935
  13. package/lib/embedding.js +154 -154
  14. package/lib/entities/extractor.js +242 -242
  15. package/lib/hot-memory.js +53 -53
  16. package/lib/index.js +361 -361
  17. package/lib/inject.js +208 -208
  18. package/lib/local-embedder.js +282 -282
  19. package/lib/mirror.js +170 -170
  20. package/lib/parser/tag.js +59 -59
  21. package/lib/parser/wiki-link.js +38 -38
  22. package/lib/quality-filter.js +123 -123
  23. package/lib/reranker.js +218 -218
  24. package/lib/search/adaptive.js +22 -22
  25. package/lib/search/bm25.js +96 -96
  26. package/lib/search/tag-boost.js +61 -61
  27. package/lib/service.js +1726 -1726
  28. package/lib/settings.js +172 -172
  29. package/lib/store.js +2238 -2238
  30. package/lib/summarize.js +236 -236
  31. package/lib/tools.js +290 -290
  32. package/lib/vector-index.js +116 -116
  33. package/package.json +80 -80
  34. package/scripts/benchmark-embed.js +201 -201
  35. package/scripts/benchmark-recall.js +133 -133
  36. package/scripts/benchmark-rerank.js +166 -166
  37. package/scripts/e2e-dsh.js +218 -218
  38. package/scripts/stress-dsh.js +255 -255
  39. package/scripts/sync-lib.js +52 -52
  40. package/src/api.js +783 -783
  41. package/src/commands.js +64 -64
  42. package/src/config.js +298 -288
  43. package/src/dream/clustering.js +118 -118
  44. package/src/dream/decisions.js +488 -439
  45. package/src/dream/sleep.js +561 -561
  46. package/src/dream/tag-extractor.js +156 -156
  47. package/src/dream.js +958 -935
  48. package/src/embedding.js +154 -154
  49. package/src/entities/extractor.js +242 -242
  50. package/src/hot-memory.js +53 -53
  51. package/src/index.js +361 -361
  52. package/src/inject.js +208 -208
  53. package/src/local-embedder.js +282 -282
  54. package/src/mirror.js +170 -170
  55. package/src/parser/tag.js +59 -59
  56. package/src/parser/wiki-link.js +38 -38
  57. package/src/quality-filter.js +123 -123
  58. package/src/reranker.js +218 -218
  59. package/src/search/adaptive.js +22 -22
  60. package/src/search/bm25.js +96 -96
  61. package/src/search/tag-boost.js +61 -61
  62. package/src/service.js +1726 -1726
  63. package/src/settings.js +172 -172
  64. package/src/store.js +2238 -2238
  65. package/src/summarize.js +236 -236
  66. package/src/tools.js +290 -290
  67. package/src/vector-index.js +116 -116
  68. package/test/api.test.js +594 -594
  69. package/test/audit.test.js +448 -448
  70. package/test/benchmark.test.js +35 -35
  71. package/test/boundary-v0625.test.js +82 -82
  72. package/test/client.test.js +368 -368
  73. package/test/clustering.test.js +100 -100
  74. package/test/commands.test.js +69 -69
  75. package/test/config.test.js +50 -50
  76. package/test/conflict-freeze.test.js +290 -290
  77. package/test/directory.test.js +134 -134
  78. package/test/dream.test.js +1060 -903
  79. package/test/entities.test.js +522 -522
  80. package/test/epistemic.test.js +298 -298
  81. package/test/fnew-0112.test.js +311 -311
  82. package/test/fnew-03.test.js +422 -422
  83. package/test/graph-api.test.js +175 -175
  84. package/test/helpers/dream-mock.js +82 -82
  85. package/test/hot-memory.test.js +174 -174
  86. package/test/inject.test.js +103 -103
  87. package/test/llm-audit.test.js +279 -279
  88. package/test/local-embedder.test.js +227 -227
  89. package/test/mirror-dirty.test.js +424 -424
  90. package/test/mirror-edit-digest.test.js +187 -187
  91. package/test/mirror-generation.test.js +499 -499
  92. package/test/mirror.test.js +249 -249
  93. package/test/normalize-decisions.test.js +120 -120
  94. package/test/peer-blockers.test.js +190 -190
  95. package/test/policy-epoch.test.js +259 -259
  96. package/test/provenance.test.js +103 -103
  97. package/test/quality-filter.test.js +118 -118
  98. package/test/reasoning-effort.test.js +199 -199
  99. package/test/recall-evals.test.js +235 -235
  100. package/test/recall-layer.test.js +315 -315
  101. package/test/receipt-chain.test.js +451 -451
  102. package/test/reflection.test.js +226 -226
  103. package/test/reranker.test.js +240 -240
  104. package/test/search-fusion.test.js +90 -90
  105. package/test/semantic.test.js +124 -124
  106. package/test/service-search.test.js +199 -199
  107. package/test/service.test.js +435 -435
  108. package/test/settings.test.js +118 -118
  109. package/test/sleep.test.js +365 -365
  110. package/test/store.test.js +436 -436
  111. package/test/stress.test.js +209 -209
  112. package/test/summarize.test.js +191 -191
  113. package/test/tag-boost.test.js +125 -125
  114. package/test/tag.test.js +312 -312
  115. package/test/tools.test.js +285 -285
  116. package/test/vector-index.test.js +221 -221
  117. package/test/wiki-link.test.js +332 -332
package/test/tag.test.js CHANGED
@@ -1,312 +1,312 @@
1
- // v0.6.2 Tag 系统测试。
2
- // 覆盖:parser parseTags/sanitizeTags(正常/多标签去重/非法/超长/中文/连字符)/
3
- // store.setMemoryTags 幂等(同 memory 只存一条、覆盖写、清空即删)/
4
- // tag: 搜索(基础/多标签 AND/与关键词组合/不存在 tag)/
5
- // autoDream tag(写 entity_attrs/fail-safe/限频/runDream 集成)/
6
- // mirror 渲染(有 tag 出 #行、无 tag 不渲染 + service 打标后文件同步)。
7
- import test from "node:test";
8
- import assert from "node:assert/strict";
9
- import { mkdtempSync, readFileSync, rmSync } from "node:fs";
10
- import { tmpdir } from "node:os";
11
- import { join } from "node:path";
12
- import { createStore } from "../src/store.js";
13
- import { createService } from "../src/service.js";
14
- import { createMirror } from "../src/mirror.js";
15
- import { parseTags, sanitizeTags, MAX_TAG_LENGTH } from "../src/parser/tag.js";
16
- import { runAutoTag } from "../src/dream/tag-extractor.js";
17
- import { createDreamScheduler } from "../src/dream.js";
18
-
19
- function openStore() {
20
- return createStore(":memory:");
21
- }
22
-
23
- function makeService(config = {}) {
24
- const store = createStore(":memory:");
25
- const service = createService({ store, mirror: null, config });
26
- return { store, service };
27
- }
28
-
29
- function makeMirrorService(config = {}) {
30
- const dir = mkdtempSync(join(tmpdir(), "dsh-mneme-tag-"));
31
- const store = createStore(":memory:");
32
- const mirror = createMirror(join(dir, "mirror"));
33
- const service = createService({ store, mirror, config, logger: { warn: () => {} } });
34
- return { store, service, dir, file: (t) => join(dir, "mirror", `${t}.md`) };
35
- }
36
-
37
- function seed(service, title, content, type = "preference") {
38
- return service.saveWithDedupe({ type, title, content, importance: 3 }).memory;
39
- }
40
-
41
- // ============================================================ parser
42
-
43
- test("parseTags: plain #tags are extracted in order", () => {
44
- assert.deepEqual(parseTags("今天用 #linux 学了 #bash 脚本"), ["linux", "bash"]);
45
- });
46
-
47
- test("parseTags: multiple tags on one line dedupe (first occurrence wins)", () => {
48
- assert.deepEqual(parseTags("#a #b #a #c"), ["a", "b", "c"]);
49
- });
50
-
51
- test("parseTags: illegal and non-tag markers are ignored", () => {
52
- assert.deepEqual(parseTags("## 标题 和 #"), [], "markdown heading and a bare # yield nothing");
53
- assert.deepEqual(parseTags("#foo#bar"), ["foo", "bar"], "back-to-back tags both parse");
54
- assert.deepEqual(parseTags(""), []);
55
- assert.deepEqual(parseTags(undefined), []);
56
- assert.deepEqual(parseTags(null), []);
57
- });
58
-
59
- test("parseTags: over-long tags (> 20 chars) are dropped", () => {
60
- const long = "a".repeat(MAX_TAG_LENGTH + 1);
61
- const ok = "b".repeat(MAX_TAG_LENGTH);
62
- assert.deepEqual(parseTags(`#${long} 和 #${ok}`), [ok]);
63
- });
64
-
65
- test("parseTags: CJK + edge-hyphen tags work", () => {
66
- assert.deepEqual(parseTags("#考研 资料与 #深度学习-入门-"), ["考研", "深度学习-入门"]);
67
- });
68
-
69
- test("sanitizeTags: LLM-style arrays are validated/deduped and tolerate leading #", () => {
70
- assert.deepEqual(sanitizeTags(["linux", " 考研 ", "#bash", "linux", "a/b", "x".repeat(30)]), [
71
- "linux", "考研", "bash"
72
- ]);
73
- assert.deepEqual(sanitizeTags("not-an-array"), []);
74
- });
75
-
76
- // ============================================================ storage
77
-
78
- test("setMemoryTags writes exactly one live tags row (idempotent overwrite)", () => {
79
- const store = openStore();
80
- const mem = store.save({ type: "preference", title: "Alpha", content: "c", tags: [] });
81
- store.setMemoryTags(mem.id, ["linux", "考研"]);
82
- assert.deepEqual(store.getMemoryTags(mem.id), ["linux", "考研"]);
83
- const live = store.getAttrsByMemory(mem.id).filter((a) => a.attr_key === "tags" && !a.valid_until);
84
- assert.equal(live.length, 1, "one live tags row per memory");
85
- // overwrite keeps one live row, value replaced
86
- store.setMemoryTags(mem.id, ["bash", "linux"]);
87
- assert.deepEqual(store.getMemoryTags(mem.id), ["bash", "linux"]);
88
- const live2 = store.getAttrsByMemory(mem.id).filter((a) => a.attr_key === "tags" && !a.valid_until);
89
- assert.equal(live2.length, 1, "still exactly one live row after overwrite");
90
- store.close();
91
- });
92
-
93
- test("setMemoryTags clears the row when the tag list is empty", () => {
94
- const store = openStore();
95
- const mem = store.save({ type: "preference", title: "Alpha", content: "c", tags: [] });
96
- store.setMemoryTags(mem.id, ["linux"]);
97
- assert.deepEqual(store.getMemoryTags(mem.id), ["linux"]);
98
- store.setMemoryTags(mem.id, []);
99
- assert.deepEqual(store.getMemoryTags(mem.id), []);
100
- const live = store.getAttrsByMemory(mem.id).filter((a) => a.attr_key === "tags" && !a.valid_until);
101
- assert.equal(live.length, 0, "no live tags row after clear");
102
- store.close();
103
- });
104
-
105
- // ============================================================ tag: search
106
-
107
- test("searchMemories: tag:xxx returns only memories carrying that tag", async () => {
108
- const { store, service } = makeService();
109
- const a = seed(service, "Linux 笔记", "内核与发行版");
110
- const b = seed(service, "考研计划", "公共管理学 631");
111
- store.setMemoryTags(a.id, ["linux"]);
112
- store.setMemoryTags(b.id, ["考研"]);
113
- const hits = await service.searchMemories("tag:linux");
114
- assert.equal(hits.length, 1);
115
- assert.equal(hits[0].id, a.id);
116
- assert.equal(hits[0].source, "tag");
117
- assert.equal((await service.searchMemories("tag:考研"))[0].id, b.id);
118
- store.close();
119
- });
120
-
121
- test("searchMemories: multiple tag: tokens use AND (must carry every tag)", async () => {
122
- const { store, service } = makeService();
123
- const both = seed(service, "两者皆有", "正文");
124
- const one = seed(service, "只有一个", "正文");
125
- store.setMemoryTags(both.id, ["a", "b"]);
126
- store.setMemoryTags(one.id, ["a"]);
127
- const hits = await service.searchMemories("tag:a tag:b");
128
- assert.equal(hits.length, 1);
129
- assert.equal(hits[0].id, both.id);
130
- store.close();
131
- });
132
-
133
- test("searchMemories: tag: combines with keyword text (ranked intersection)", async () => {
134
- const { store, service } = makeService();
135
- const hit = seed(service, "内核调度", "进程调度器与负载均衡");
136
- const other = seed(service, "内核编译", "无关关键词");
137
- store.setMemoryTags(hit.id, ["linux"]);
138
- store.setMemoryTags(other.id, ["linux"]);
139
- const hits = await service.searchMemories("tag:linux 调度");
140
- assert.equal(hits.length, 1, "only the tagged memory whose content mentions 调度");
141
- assert.equal(hits[0].id, hit.id);
142
- assert.equal(hits[0].source, "tag");
143
- store.close();
144
- });
145
-
146
- test("searchMemories: non-existent tag returns []", async () => {
147
- const { store, service } = makeService();
148
- const a = seed(service, "Alpha", "正文");
149
- store.setMemoryTags(a.id, ["linux"]);
150
- assert.deepEqual(await service.searchMemories("tag:不存在的标签"), []);
151
- store.close();
152
- });
153
-
154
- // ============================================================ autoDream tag
155
-
156
- /** Minimal LLM ctx whose stream yields a queue of texts, one per call. */
157
- function makeCtx(calls) {
158
- let n = 0;
159
- return {
160
- llm: {
161
- stream: async function* () {
162
- n++;
163
- const text = calls[Math.min(n - 1, calls.length - 1)];
164
- if (text !== undefined) yield { type: "text-delta", text };
165
- yield { type: "finish", reason: { kind: "ok" } };
166
- }
167
- },
168
- logger: { warn: () => {}, info: () => {} }
169
- };
170
- }
171
-
172
- test("runAutoTag writes validated tags into entity_attrs", async () => {
173
- const { store, service } = makeService();
174
- const a = seed(service, "Linux", "内核");
175
- const b = seed(service, "考研", "公共管理");
176
- const text = JSON.stringify([
177
- { id: a.id, tags: ["linux", "内核"] },
178
- { id: b.id, tags: ["考研", "公共管理"] }
179
- ]);
180
- const ctx = makeCtx([text]);
181
- const result = await runAutoTag({ ctx, service, config: { dreamProvider: "d", dreamModel: "m" } });
182
- assert.equal(result.ok, true);
183
- assert.equal(result.tagged, 2);
184
- assert.deepEqual(store.getMemoryTags(a.id), ["linux", "内核"]);
185
- assert.deepEqual(store.getMemoryTags(b.id), ["考研", "公共管理"]);
186
- store.close();
187
- });
188
-
189
- test("runAutoTag: skips already-tagged memories, merges into fresh ones", async () => {
190
- const { store, service } = makeService();
191
- const a = seed(service, "Linux", "内核");
192
- const b = seed(service, "考研", "公共管理");
193
- store.setMemoryTags(a.id, ["已有"]); // manual tag
194
- const text = JSON.stringify([
195
- { id: a.id, tags: ["linux"] },
196
- { id: b.id, tags: ["考研"] }
197
- ]);
198
- const ctx = makeCtx([text]);
199
- const result = await runAutoTag({ ctx, service, config: { dreamProvider: "d", dreamModel: "m" } });
200
- assert.equal(result.ok, true);
201
- assert.equal(result.tagged, 1, "only the untagged memory is tagged");
202
- assert.deepEqual(store.getMemoryTags(a.id), ["已有"], "manual tags left untouched");
203
- assert.deepEqual(store.getMemoryTags(b.id), ["考研"]);
204
- store.close();
205
- });
206
-
207
- test("runAutoTag fail-safe: garbage / aborted LLM output never throws and writes nothing", async () => {
208
- const { store, service } = makeService();
209
- const a = seed(service, "Alpha", "正文");
210
- // garbage JSON → skipped, no writes
211
- const r1 = await runAutoTag({
212
- ctx: makeCtx(["not json at all"]),
213
- service,
214
- config: { dreamProvider: "d", dreamModel: "m" }
215
- });
216
- assert.equal(r1.ok, false);
217
- assert.deepEqual(store.getMemoryTags(a.id), []);
218
- // unknown id / illegal tags are dropped, valid ones still land
219
- const text = JSON.stringify([
220
- { id: "不存在", tags: ["x"] },
221
- { id: a.id, tags: ["ok-tag", "a/b", "y".repeat(30)] }
222
- ]);
223
- const r2 = await runAutoTag({ ctx: makeCtx([text]), service, config: { dreamProvider: "d", dreamModel: "m" } });
224
- assert.equal(r2.ok, true);
225
- assert.deepEqual(store.getMemoryTags(a.id), ["ok-tag"], "illegal/over-long dropped, valid kept");
226
- store.close();
227
- });
228
-
229
- test("runAutoTag respects autoTagMaxPerRun cap", async () => {
230
- const { store, service } = makeService();
231
- const mems = [];
232
- for (let i = 0; i < 5; i++) mems.push(seed(service, `M${i}`, "正文"));
233
- const text = JSON.stringify(mems.map((m, i) => ({ id: m.id, tags: [`t${i}`] })));
234
- const ctx = makeCtx([text]);
235
- const result = await runAutoTag({ ctx, service, config: { dreamProvider: "d", dreamModel: "m", autoTagMaxPerRun: 2 } });
236
- assert.equal(result.tagged, 2, "only 2 of 5 memories tagged (cap)");
237
- const tagged = mems.filter((m) => store.getMemoryTags(m.id).length);
238
- assert.equal(tagged.length, 2, "exactly 2 memories carry tags");
239
- const untagged = mems.filter((m) => !store.getMemoryTags(m.id).length);
240
- assert.equal(untagged.length, 3, "the rest are untouched");
241
- store.close();
242
- });
243
-
244
- test("runDream auto-tags retained memories after consolidation when autoTagEnabled=true", async () => {
245
- const { store, service } = makeService({ autoTagEnabled: true });
246
- const a = seed(service, "旧1", "第一段");
247
- const b = seed(service, "旧2", "第二段");
248
- const keep = (id) => JSON.stringify([{ action: "keep", ids: [id] }]);
249
- let calls = 0;
250
- const ctx = {
251
- llm: {
252
- stream: async function* () {
253
- calls++;
254
- if (calls === 1) yield { type: "text-delta", text: keep(a.id) }; // consolidation
255
- else if (calls === 2) yield { type: "text-delta", text: JSON.stringify([{ id: a.id, tags: ["内核"] }]) }; // auto-tag
256
- else yield { type: "text-delta", text: "总览" }; // summary
257
- yield { type: "finish", reason: { kind: "ok" } };
258
- }
259
- },
260
- logger: { warn: () => {}, info: () => {} }
261
- };
262
- const dream = createDreamScheduler({ thresholdCount: 1, thresholdChars: 0, delayMs: 0 });
263
- const result = await dream.runDream(ctx, service, {
264
- dreamProvider: "deepseek", dreamModel: "deepseek-chat", autoTagEnabled: true
265
- });
266
- assert.equal(result.ok, true);
267
- assert.deepEqual(store.getMemoryTags(a.id), ["内核"], "auto-tag landed after consolidation");
268
- store.close();
269
- });
270
-
271
- // ============================================================ mirror rendering
272
-
273
- test("mirror renderMemory draws a #tag line under the title only when tags exist", () => {
274
- const dir = mkdtempSync(join(tmpdir(), "dsh-mneme-tag-mirror-"));
275
- const mirror = createMirror(join(dir, "m"));
276
- const now = new Date().toISOString();
277
- mirror.sync([
278
- { id: "m1", type: "preference", title: "Alpha", content: "正文", importance: 3, updated_at: now, tags: [], entityTags: ["linux", "考研"] },
279
- { id: "m2", type: "project", title: "Beta", content: "正文2", importance: 3, updated_at: now, tags: [], entityTags: [] }
280
- ]);
281
- const pref = readFileSync(join(dir, "m", "preferences.md"), "utf8");
282
- assert.match(pref, /## Alpha/);
283
- assert.match(pref, /^#linux #考研$/m, "tag line rendered under the title");
284
- const proj = readFileSync(join(dir, "m", "projects.md"), "utf8");
285
- assert.match(proj, /## Beta/);
286
- assert.doesNotMatch(proj, /^#[^#\s]/m, "no # tag line when the memory has no tags");
287
- rmSync(dir, { recursive: true, force: true });
288
- });
289
-
290
- test("service.setMemoryTags re-renders the mirror with the #tag line", () => {
291
- const { store, service, file, dir } = makeMirrorService();
292
- const { memory } = service.saveWithDedupe({ type: "preference", title: "Alpha", content: "正文" });
293
- const r = service.setMemoryTags(memory.id, ["bash"]);
294
- assert.equal(r.ok, true);
295
- assert.deepEqual(r.tags, ["bash"]);
296
- const pref = readFileSync(file("preferences"), "utf8");
297
- assert.match(pref, /^#bash$/m, "mirror file updated with the # tag line");
298
- // no tag line after clearing
299
- service.setMemoryTags(memory.id, []);
300
- const cleared = readFileSync(file("preferences"), "utf8");
301
- assert.doesNotMatch(cleared, /^#[^#\s]/m, "tag line removed after clearing");
302
- assert.deepEqual(store.getMemoryTags(memory.id), []);
303
- rmSync(dir, { recursive: true, force: true });
304
- });
305
-
306
- test("service.setMemoryTags respects manualTagEnabled=false gate", () => {
307
- const { service } = makeService({ manualTagEnabled: false });
308
- const a = seed(service, "Alpha", "正文");
309
- const r = service.setMemoryTags(a.id, ["linux"]);
310
- assert.equal(r.ok, false);
311
- assert.match(r.error, /manualTagEnabled/);
312
- });
1
+ // v0.6.2 Tag 系统测试。
2
+ // 覆盖:parser parseTags/sanitizeTags(正常/多标签去重/非法/超长/中文/连字符)/
3
+ // store.setMemoryTags 幂等(同 memory 只存一条、覆盖写、清空即删)/
4
+ // tag: 搜索(基础/多标签 AND/与关键词组合/不存在 tag)/
5
+ // autoDream tag(写 entity_attrs/fail-safe/限频/runDream 集成)/
6
+ // mirror 渲染(有 tag 出 #行、无 tag 不渲染 + service 打标后文件同步)。
7
+ import test from "node:test";
8
+ import assert from "node:assert/strict";
9
+ import { mkdtempSync, readFileSync, rmSync } from "node:fs";
10
+ import { tmpdir } from "node:os";
11
+ import { join } from "node:path";
12
+ import { createStore } from "../src/store.js";
13
+ import { createService } from "../src/service.js";
14
+ import { createMirror } from "../src/mirror.js";
15
+ import { parseTags, sanitizeTags, MAX_TAG_LENGTH } from "../src/parser/tag.js";
16
+ import { runAutoTag } from "../src/dream/tag-extractor.js";
17
+ import { createDreamScheduler } from "../src/dream.js";
18
+
19
+ function openStore() {
20
+ return createStore(":memory:");
21
+ }
22
+
23
+ function makeService(config = {}) {
24
+ const store = createStore(":memory:");
25
+ const service = createService({ store, mirror: null, config });
26
+ return { store, service };
27
+ }
28
+
29
+ function makeMirrorService(config = {}) {
30
+ const dir = mkdtempSync(join(tmpdir(), "dsh-mneme-tag-"));
31
+ const store = createStore(":memory:");
32
+ const mirror = createMirror(join(dir, "mirror"));
33
+ const service = createService({ store, mirror, config, logger: { warn: () => {} } });
34
+ return { store, service, dir, file: (t) => join(dir, "mirror", `${t}.md`) };
35
+ }
36
+
37
+ function seed(service, title, content, type = "preference") {
38
+ return service.saveWithDedupe({ type, title, content, importance: 3 }).memory;
39
+ }
40
+
41
+ // ============================================================ parser
42
+
43
+ test("parseTags: plain #tags are extracted in order", () => {
44
+ assert.deepEqual(parseTags("今天用 #linux 学了 #bash 脚本"), ["linux", "bash"]);
45
+ });
46
+
47
+ test("parseTags: multiple tags on one line dedupe (first occurrence wins)", () => {
48
+ assert.deepEqual(parseTags("#a #b #a #c"), ["a", "b", "c"]);
49
+ });
50
+
51
+ test("parseTags: illegal and non-tag markers are ignored", () => {
52
+ assert.deepEqual(parseTags("## 标题 和 #"), [], "markdown heading and a bare # yield nothing");
53
+ assert.deepEqual(parseTags("#foo#bar"), ["foo", "bar"], "back-to-back tags both parse");
54
+ assert.deepEqual(parseTags(""), []);
55
+ assert.deepEqual(parseTags(undefined), []);
56
+ assert.deepEqual(parseTags(null), []);
57
+ });
58
+
59
+ test("parseTags: over-long tags (> 20 chars) are dropped", () => {
60
+ const long = "a".repeat(MAX_TAG_LENGTH + 1);
61
+ const ok = "b".repeat(MAX_TAG_LENGTH);
62
+ assert.deepEqual(parseTags(`#${long} 和 #${ok}`), [ok]);
63
+ });
64
+
65
+ test("parseTags: CJK + edge-hyphen tags work", () => {
66
+ assert.deepEqual(parseTags("#考研 资料与 #深度学习-入门-"), ["考研", "深度学习-入门"]);
67
+ });
68
+
69
+ test("sanitizeTags: LLM-style arrays are validated/deduped and tolerate leading #", () => {
70
+ assert.deepEqual(sanitizeTags(["linux", " 考研 ", "#bash", "linux", "a/b", "x".repeat(30)]), [
71
+ "linux", "考研", "bash"
72
+ ]);
73
+ assert.deepEqual(sanitizeTags("not-an-array"), []);
74
+ });
75
+
76
+ // ============================================================ storage
77
+
78
+ test("setMemoryTags writes exactly one live tags row (idempotent overwrite)", () => {
79
+ const store = openStore();
80
+ const mem = store.save({ type: "preference", title: "Alpha", content: "c", tags: [] });
81
+ store.setMemoryTags(mem.id, ["linux", "考研"]);
82
+ assert.deepEqual(store.getMemoryTags(mem.id), ["linux", "考研"]);
83
+ const live = store.getAttrsByMemory(mem.id).filter((a) => a.attr_key === "tags" && !a.valid_until);
84
+ assert.equal(live.length, 1, "one live tags row per memory");
85
+ // overwrite keeps one live row, value replaced
86
+ store.setMemoryTags(mem.id, ["bash", "linux"]);
87
+ assert.deepEqual(store.getMemoryTags(mem.id), ["bash", "linux"]);
88
+ const live2 = store.getAttrsByMemory(mem.id).filter((a) => a.attr_key === "tags" && !a.valid_until);
89
+ assert.equal(live2.length, 1, "still exactly one live row after overwrite");
90
+ store.close();
91
+ });
92
+
93
+ test("setMemoryTags clears the row when the tag list is empty", () => {
94
+ const store = openStore();
95
+ const mem = store.save({ type: "preference", title: "Alpha", content: "c", tags: [] });
96
+ store.setMemoryTags(mem.id, ["linux"]);
97
+ assert.deepEqual(store.getMemoryTags(mem.id), ["linux"]);
98
+ store.setMemoryTags(mem.id, []);
99
+ assert.deepEqual(store.getMemoryTags(mem.id), []);
100
+ const live = store.getAttrsByMemory(mem.id).filter((a) => a.attr_key === "tags" && !a.valid_until);
101
+ assert.equal(live.length, 0, "no live tags row after clear");
102
+ store.close();
103
+ });
104
+
105
+ // ============================================================ tag: search
106
+
107
+ test("searchMemories: tag:xxx returns only memories carrying that tag", async () => {
108
+ const { store, service } = makeService();
109
+ const a = seed(service, "Linux 笔记", "内核与发行版");
110
+ const b = seed(service, "考研计划", "公共管理学 631");
111
+ store.setMemoryTags(a.id, ["linux"]);
112
+ store.setMemoryTags(b.id, ["考研"]);
113
+ const hits = await service.searchMemories("tag:linux");
114
+ assert.equal(hits.length, 1);
115
+ assert.equal(hits[0].id, a.id);
116
+ assert.equal(hits[0].source, "tag");
117
+ assert.equal((await service.searchMemories("tag:考研"))[0].id, b.id);
118
+ store.close();
119
+ });
120
+
121
+ test("searchMemories: multiple tag: tokens use AND (must carry every tag)", async () => {
122
+ const { store, service } = makeService();
123
+ const both = seed(service, "两者皆有", "正文");
124
+ const one = seed(service, "只有一个", "正文");
125
+ store.setMemoryTags(both.id, ["a", "b"]);
126
+ store.setMemoryTags(one.id, ["a"]);
127
+ const hits = await service.searchMemories("tag:a tag:b");
128
+ assert.equal(hits.length, 1);
129
+ assert.equal(hits[0].id, both.id);
130
+ store.close();
131
+ });
132
+
133
+ test("searchMemories: tag: combines with keyword text (ranked intersection)", async () => {
134
+ const { store, service } = makeService();
135
+ const hit = seed(service, "内核调度", "进程调度器与负载均衡");
136
+ const other = seed(service, "内核编译", "无关关键词");
137
+ store.setMemoryTags(hit.id, ["linux"]);
138
+ store.setMemoryTags(other.id, ["linux"]);
139
+ const hits = await service.searchMemories("tag:linux 调度");
140
+ assert.equal(hits.length, 1, "only the tagged memory whose content mentions 调度");
141
+ assert.equal(hits[0].id, hit.id);
142
+ assert.equal(hits[0].source, "tag");
143
+ store.close();
144
+ });
145
+
146
+ test("searchMemories: non-existent tag returns []", async () => {
147
+ const { store, service } = makeService();
148
+ const a = seed(service, "Alpha", "正文");
149
+ store.setMemoryTags(a.id, ["linux"]);
150
+ assert.deepEqual(await service.searchMemories("tag:不存在的标签"), []);
151
+ store.close();
152
+ });
153
+
154
+ // ============================================================ autoDream tag
155
+
156
+ /** Minimal LLM ctx whose stream yields a queue of texts, one per call. */
157
+ function makeCtx(calls) {
158
+ let n = 0;
159
+ return {
160
+ llm: {
161
+ stream: async function* () {
162
+ n++;
163
+ const text = calls[Math.min(n - 1, calls.length - 1)];
164
+ if (text !== undefined) yield { type: "text-delta", text };
165
+ yield { type: "finish", reason: { kind: "ok" } };
166
+ }
167
+ },
168
+ logger: { warn: () => {}, info: () => {} }
169
+ };
170
+ }
171
+
172
+ test("runAutoTag writes validated tags into entity_attrs", async () => {
173
+ const { store, service } = makeService();
174
+ const a = seed(service, "Linux", "内核");
175
+ const b = seed(service, "考研", "公共管理");
176
+ const text = JSON.stringify([
177
+ { id: a.id, tags: ["linux", "内核"] },
178
+ { id: b.id, tags: ["考研", "公共管理"] }
179
+ ]);
180
+ const ctx = makeCtx([text]);
181
+ const result = await runAutoTag({ ctx, service, config: { dreamProvider: "d", dreamModel: "m" } });
182
+ assert.equal(result.ok, true);
183
+ assert.equal(result.tagged, 2);
184
+ assert.deepEqual(store.getMemoryTags(a.id), ["linux", "内核"]);
185
+ assert.deepEqual(store.getMemoryTags(b.id), ["考研", "公共管理"]);
186
+ store.close();
187
+ });
188
+
189
+ test("runAutoTag: skips already-tagged memories, merges into fresh ones", async () => {
190
+ const { store, service } = makeService();
191
+ const a = seed(service, "Linux", "内核");
192
+ const b = seed(service, "考研", "公共管理");
193
+ store.setMemoryTags(a.id, ["已有"]); // manual tag
194
+ const text = JSON.stringify([
195
+ { id: a.id, tags: ["linux"] },
196
+ { id: b.id, tags: ["考研"] }
197
+ ]);
198
+ const ctx = makeCtx([text]);
199
+ const result = await runAutoTag({ ctx, service, config: { dreamProvider: "d", dreamModel: "m" } });
200
+ assert.equal(result.ok, true);
201
+ assert.equal(result.tagged, 1, "only the untagged memory is tagged");
202
+ assert.deepEqual(store.getMemoryTags(a.id), ["已有"], "manual tags left untouched");
203
+ assert.deepEqual(store.getMemoryTags(b.id), ["考研"]);
204
+ store.close();
205
+ });
206
+
207
+ test("runAutoTag fail-safe: garbage / aborted LLM output never throws and writes nothing", async () => {
208
+ const { store, service } = makeService();
209
+ const a = seed(service, "Alpha", "正文");
210
+ // garbage JSON → skipped, no writes
211
+ const r1 = await runAutoTag({
212
+ ctx: makeCtx(["not json at all"]),
213
+ service,
214
+ config: { dreamProvider: "d", dreamModel: "m" }
215
+ });
216
+ assert.equal(r1.ok, false);
217
+ assert.deepEqual(store.getMemoryTags(a.id), []);
218
+ // unknown id / illegal tags are dropped, valid ones still land
219
+ const text = JSON.stringify([
220
+ { id: "不存在", tags: ["x"] },
221
+ { id: a.id, tags: ["ok-tag", "a/b", "y".repeat(30)] }
222
+ ]);
223
+ const r2 = await runAutoTag({ ctx: makeCtx([text]), service, config: { dreamProvider: "d", dreamModel: "m" } });
224
+ assert.equal(r2.ok, true);
225
+ assert.deepEqual(store.getMemoryTags(a.id), ["ok-tag"], "illegal/over-long dropped, valid kept");
226
+ store.close();
227
+ });
228
+
229
+ test("runAutoTag respects autoTagMaxPerRun cap", async () => {
230
+ const { store, service } = makeService();
231
+ const mems = [];
232
+ for (let i = 0; i < 5; i++) mems.push(seed(service, `M${i}`, "正文"));
233
+ const text = JSON.stringify(mems.map((m, i) => ({ id: m.id, tags: [`t${i}`] })));
234
+ const ctx = makeCtx([text]);
235
+ const result = await runAutoTag({ ctx, service, config: { dreamProvider: "d", dreamModel: "m", autoTagMaxPerRun: 2 } });
236
+ assert.equal(result.tagged, 2, "only 2 of 5 memories tagged (cap)");
237
+ const tagged = mems.filter((m) => store.getMemoryTags(m.id).length);
238
+ assert.equal(tagged.length, 2, "exactly 2 memories carry tags");
239
+ const untagged = mems.filter((m) => !store.getMemoryTags(m.id).length);
240
+ assert.equal(untagged.length, 3, "the rest are untouched");
241
+ store.close();
242
+ });
243
+
244
+ test("runDream auto-tags retained memories after consolidation when autoTagEnabled=true", async () => {
245
+ const { store, service } = makeService({ autoTagEnabled: true });
246
+ const a = seed(service, "旧1", "第一段");
247
+ const b = seed(service, "旧2", "第二段");
248
+ const keep = (id) => JSON.stringify([{ action: "keep", ids: [id] }]);
249
+ let calls = 0;
250
+ const ctx = {
251
+ llm: {
252
+ stream: async function* () {
253
+ calls++;
254
+ if (calls === 1) yield { type: "text-delta", text: keep(a.id) }; // consolidation
255
+ else if (calls === 2) yield { type: "text-delta", text: JSON.stringify([{ id: a.id, tags: ["内核"] }]) }; // auto-tag
256
+ else yield { type: "text-delta", text: "总览" }; // summary
257
+ yield { type: "finish", reason: { kind: "ok" } };
258
+ }
259
+ },
260
+ logger: { warn: () => {}, info: () => {} }
261
+ };
262
+ const dream = createDreamScheduler({ thresholdCount: 1, thresholdChars: 0, delayMs: 0 });
263
+ const result = await dream.runDream(ctx, service, {
264
+ dreamProvider: "deepseek", dreamModel: "deepseek-chat", autoTagEnabled: true
265
+ });
266
+ assert.equal(result.ok, true);
267
+ assert.deepEqual(store.getMemoryTags(a.id), ["内核"], "auto-tag landed after consolidation");
268
+ store.close();
269
+ });
270
+
271
+ // ============================================================ mirror rendering
272
+
273
+ test("mirror renderMemory draws a #tag line under the title only when tags exist", () => {
274
+ const dir = mkdtempSync(join(tmpdir(), "dsh-mneme-tag-mirror-"));
275
+ const mirror = createMirror(join(dir, "m"));
276
+ const now = new Date().toISOString();
277
+ mirror.sync([
278
+ { id: "m1", type: "preference", title: "Alpha", content: "正文", importance: 3, updated_at: now, tags: [], entityTags: ["linux", "考研"] },
279
+ { id: "m2", type: "project", title: "Beta", content: "正文2", importance: 3, updated_at: now, tags: [], entityTags: [] }
280
+ ]);
281
+ const pref = readFileSync(join(dir, "m", "preferences.md"), "utf8");
282
+ assert.match(pref, /## Alpha/);
283
+ assert.match(pref, /^#linux #考研$/m, "tag line rendered under the title");
284
+ const proj = readFileSync(join(dir, "m", "projects.md"), "utf8");
285
+ assert.match(proj, /## Beta/);
286
+ assert.doesNotMatch(proj, /^#[^#\s]/m, "no # tag line when the memory has no tags");
287
+ rmSync(dir, { recursive: true, force: true });
288
+ });
289
+
290
+ test("service.setMemoryTags re-renders the mirror with the #tag line", () => {
291
+ const { store, service, file, dir } = makeMirrorService();
292
+ const { memory } = service.saveWithDedupe({ type: "preference", title: "Alpha", content: "正文" });
293
+ const r = service.setMemoryTags(memory.id, ["bash"]);
294
+ assert.equal(r.ok, true);
295
+ assert.deepEqual(r.tags, ["bash"]);
296
+ const pref = readFileSync(file("preferences"), "utf8");
297
+ assert.match(pref, /^#bash$/m, "mirror file updated with the # tag line");
298
+ // no tag line after clearing
299
+ service.setMemoryTags(memory.id, []);
300
+ const cleared = readFileSync(file("preferences"), "utf8");
301
+ assert.doesNotMatch(cleared, /^#[^#\s]/m, "tag line removed after clearing");
302
+ assert.deepEqual(store.getMemoryTags(memory.id), []);
303
+ rmSync(dir, { recursive: true, force: true });
304
+ });
305
+
306
+ test("service.setMemoryTags respects manualTagEnabled=false gate", () => {
307
+ const { service } = makeService({ manualTagEnabled: false });
308
+ const a = seed(service, "Alpha", "正文");
309
+ const r = service.setMemoryTags(a.id, ["linux"]);
310
+ assert.equal(r.ok, false);
311
+ assert.match(r.error, /manualTagEnabled/);
312
+ });