@modusensus/dsh-mneme 0.2.3 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/README.md +27 -9
  2. package/lib/api.js +54 -4
  3. package/lib/client.js +58 -14
  4. package/lib/config.js +14 -2
  5. package/lib/dream/decisions.js +174 -62
  6. package/lib/dream.js +30 -5
  7. package/lib/index.js +4 -2
  8. package/lib/mirror.js +7 -1
  9. package/lib/service.js +117 -3
  10. package/lib/store.js +40 -0
  11. package/lib/tools.js +47 -4
  12. package/package.json +3 -1
  13. package/scripts/benchmark-embed.js +201 -0
  14. package/scripts/benchmark-rerank.js +166 -0
  15. package/scripts/e2e-dsh.js +216 -0
  16. package/scripts/stress-dsh.js +255 -0
  17. package/scripts/sync-lib.js +47 -0
  18. package/src/api.js +54 -4
  19. package/src/config.js +14 -2
  20. package/src/dream/decisions.js +174 -62
  21. package/src/dream.js +30 -5
  22. package/src/index.js +4 -2
  23. package/src/mirror.js +7 -1
  24. package/src/service.js +117 -3
  25. package/src/store.js +40 -0
  26. package/src/tools.js +47 -4
  27. package/test/api.test.js +385 -0
  28. package/test/audit.test.js +290 -0
  29. package/test/client.test.js +26 -0
  30. package/test/clustering.test.js +100 -0
  31. package/test/commands.test.js +69 -0
  32. package/test/config.test.js +31 -0
  33. package/test/dream.test.js +526 -0
  34. package/test/helpers/dream-mock.js +82 -0
  35. package/test/inject.test.js +82 -0
  36. package/test/local-embedder.test.js +227 -0
  37. package/test/mirror.test.js +249 -0
  38. package/test/reflection.test.js +226 -0
  39. package/test/reranker.test.js +197 -0
  40. package/test/semantic.test.js +123 -0
  41. package/test/service-search.test.js +169 -0
  42. package/test/service.test.js +198 -0
  43. package/test/settings.test.js +101 -0
  44. package/test/store.test.js +293 -0
  45. package/test/stress.test.js +209 -0
  46. package/test/summarize.test.js +156 -0
  47. package/test/tools.test.js +265 -0
  48. package/test/vector-index.test.js +205 -0
@@ -0,0 +1,293 @@
1
+ import test from "node:test";
2
+ import assert from "node:assert/strict";
3
+ import { DatabaseSync } from "node:sqlite";
4
+ import { mkdtempSync, rmSync } from "node:fs";
5
+ import { tmpdir } from "node:os";
6
+ import { join } from "node:path";
7
+ import { createStore } from "../src/store.js";
8
+
9
+ function openMemory() {
10
+ return createStore(":memory:");
11
+ }
12
+
13
+ test("createStore initializes schema and opens db", () => {
14
+ const store = openMemory();
15
+ const row = store.db.prepare(
16
+ "SELECT name FROM sqlite_master WHERE type='table' AND name='memories'"
17
+ ).get();
18
+ assert.ok(row, "memories table exists");
19
+ store.close();
20
+ });
21
+
22
+ test("save inserts a memory and returns it with id/created_at", () => {
23
+ const store = openMemory();
24
+ const saved = store.save({
25
+ type: "preference",
26
+ title: "语言",
27
+ content: "用户用中文交流",
28
+ tags: ["偏好"],
29
+ importance: 5,
30
+ source: "manual"
31
+ });
32
+ assert.ok(saved.id, "has id");
33
+ assert.ok(saved.created_at, "has created_at");
34
+ assert.equal(saved.type, "preference");
35
+ assert.equal(store.count(), 1);
36
+ store.close();
37
+ });
38
+
39
+ test("getById returns the memory", () => {
40
+ const store = openMemory();
41
+ const saved = store.save({ type: "project", title: "t", content: "c", importance: 3 });
42
+ const got = store.getById(saved.id);
43
+ assert.equal(got.title, "t");
44
+ assert.equal(got.content, "c");
45
+ store.close();
46
+ });
47
+
48
+ test("update modifies fields and bumps updated_at", () => {
49
+ const store = openMemory();
50
+ const saved = store.save({ type: "project", title: "t", content: "c", importance: 3 });
51
+ const updated = store.update(saved.id, { content: "new content", importance: 4 });
52
+ assert.equal(updated.content, "new content");
53
+ assert.equal(updated.importance, 4);
54
+ assert.notEqual(updated.updated_at, saved.updated_at);
55
+ store.close();
56
+ });
57
+
58
+ test("remove deletes the memory", () => {
59
+ const store = openMemory();
60
+ const saved = store.save({ type: "project", title: "t", content: "c" });
61
+ store.remove(saved.id);
62
+ assert.equal(store.getById(saved.id), undefined);
63
+ assert.equal(store.count(), 0);
64
+ store.close();
65
+ });
66
+
67
+ test("list filters by type and paginates", () => {
68
+ const store = openMemory();
69
+ for (let i = 0; i < 5; i++) store.save({ type: "preference", title: `p${i}`, content: "c" });
70
+ for (let i = 0; i < 3; i++) store.save({ type: "project", title: `j${i}`, content: "c" });
71
+ assert.equal(store.list({ type: "preference" }).length, 5);
72
+ assert.equal(store.list({ type: "project" }).length, 3);
73
+ assert.equal(store.list({ limit: 2 }).length, 2);
74
+ assert.equal(store.list({ limit: 2, offset: 2 }).length, 2);
75
+ store.close();
76
+ });
77
+
78
+ test("setForget toggles injection suppression", () => {
79
+ const store = openMemory();
80
+ const saved = store.save({ type: "project", title: "t", content: "c", importance: 5 });
81
+ store.setForget(saved.id, true);
82
+ const got = store.getById(saved.id);
83
+ // toRow maps SQLite 0/1 to boolean
84
+ assert.equal(got.forgotten, true);
85
+ store.close();
86
+ });
87
+
88
+ test("count excludes forgotten by default, includeForgotten opts in", () => {
89
+ const store = openMemory();
90
+ const a = store.save({ type: "project", title: "t1", content: "c" });
91
+ store.save({ type: "project", title: "t2", content: "c" });
92
+ store.save({ type: "preference", title: "p", content: "c" });
93
+ store.setForget(a.id, true);
94
+ assert.equal(store.count(), 2, "default excludes forgotten (matches list)");
95
+ assert.equal(store.count("project"), 1);
96
+ assert.equal(store.count("preference"), 1);
97
+ assert.equal(store.count(undefined, { includeForgotten: true }), 3);
98
+ assert.equal(store.count("project", { includeForgotten: true }), 2);
99
+ store.close();
100
+ });
101
+
102
+ test("search matches title, content and tags", () => {
103
+ const store = openMemory();
104
+ store.save({ type: "project", title: "记忆插件", content: "c1", tags: [] });
105
+ store.save({ type: "project", title: "t2", content: "用户用中文交流", tags: [] });
106
+ store.save({ type: "preference", title: "t3", content: "c3", tags: ["偏好"] });
107
+ assert.ok(store.search("记忆插件").some((m) => m.title === "记忆插件"));
108
+ assert.ok(store.search("中文").some((m) => m.content === "用户用中文交流"));
109
+ assert.ok(store.search("偏好").some((m) => m.tags.includes("偏好")));
110
+ store.close();
111
+ });
112
+
113
+ test("search with empty query returns []", () => {
114
+ const store = openMemory();
115
+ store.save({ type: "project", title: "t", content: "c" });
116
+ assert.deepEqual(store.search(""), []);
117
+ assert.deepEqual(store.search(" "), []);
118
+ store.close();
119
+ });
120
+
121
+ test("forgotten memories are excluded from list and search until un-forgotten", () => {
122
+ const store = openMemory();
123
+ const saved = store.save({ type: "project", title: "秘密", content: "c", importance: 5 });
124
+ assert.ok(store.list().some((m) => m.id === saved.id));
125
+ assert.ok(store.search("秘密").some((m) => m.id === saved.id));
126
+ store.setForget(saved.id, true);
127
+ assert.ok(!store.list().some((m) => m.id === saved.id));
128
+ assert.ok(!store.search("秘密").some((m) => m.id === saved.id));
129
+ store.setForget(saved.id, false);
130
+ assert.ok(store.list().some((m) => m.id === saved.id));
131
+ assert.ok(store.search("秘密").some((m) => m.id === saved.id));
132
+ store.close();
133
+ });
134
+
135
+ test("search matches CJK substring", () => {
136
+ const store = openMemory();
137
+ store.save({ type: "project", title: "t", content: "用户用中文交流" });
138
+ assert.equal(store.search("中文").length, 1);
139
+ store.close();
140
+ });
141
+
142
+ test("search respects limit", () => {
143
+ const store = openMemory();
144
+ for (let i = 0; i < 3; i++) store.save({ type: "project", title: `m${i}`, content: "匹配词" });
145
+ assert.equal(store.search("匹配词").length, 3);
146
+ assert.equal(store.search("匹配词", { limit: 2 }).length, 2);
147
+ store.close();
148
+ });
149
+
150
+ test("list sanitizes invalid limit/offset", () => {
151
+ const store = openMemory();
152
+ for (let i = 0; i < 3; i++) store.save({ type: "project", title: `t${i}`, content: "c" });
153
+ assert.equal(store.list({ limit: -1 }).length, 3);
154
+ assert.equal(store.list({ limit: 0 }).length, 3);
155
+ assert.equal(store.list({ limit: 1.5 }).length, 3);
156
+ assert.equal(store.list({ limit: 2, offset: -5 }).length, 2);
157
+ store.close();
158
+ });
159
+
160
+ test("schema migration adds archived column to legacy database", () => {
161
+ const dir = mkdtempSync(join(tmpdir(), "dsh-mneme-migrate-"));
162
+ const dbPath = join(dir, "legacy.db");
163
+ try {
164
+ // Create a legacy db WITHOUT archived column
165
+ const legacy = new DatabaseSync(dbPath);
166
+ legacy.exec(`CREATE TABLE memories (
167
+ id TEXT PRIMARY KEY, type TEXT NOT NULL, title TEXT NOT NULL, content TEXT NOT NULL,
168
+ tags TEXT NOT NULL DEFAULT '[]', importance INTEGER NOT NULL DEFAULT 3,
169
+ forgotten INTEGER NOT NULL DEFAULT 0, source TEXT, created_at TEXT NOT NULL, updated_at TEXT NOT NULL
170
+ );`);
171
+ legacy.close();
172
+ // Open with createStore → should ALTER TABLE
173
+ const store = createStore(dbPath);
174
+ const cols = store.db.prepare("PRAGMA table_info(memories)").all().map((c) => c.name);
175
+ assert.ok(cols.includes("archived"), "archived column added");
176
+ store.close();
177
+ } finally {
178
+ rmSync(dir, { recursive: true, force: true });
179
+ }
180
+ });
181
+
182
+ test("setArchived marks entry archived; list excludes it by default", () => {
183
+ const store = openMemory();
184
+ const saved = store.save({ type: "project", title: "t", content: "c", importance: 3 });
185
+ const archived = store.setArchived(saved.id, true);
186
+ assert.equal(archived.archived, true);
187
+ assert.equal(store.list().length, 0, "excluded from default list");
188
+ assert.equal(store.list({ includeArchived: true }).length, 1, "included with flag");
189
+ assert.equal(store.count(), 0, "excluded from default count");
190
+ assert.equal(store.count(undefined, { includeArchived: true }), 1);
191
+ store.close();
192
+ });
193
+
194
+ test("search excludes archived by default", () => {
195
+ const store = openMemory();
196
+ const saved = store.save({ type: "project", title: "secret", content: "hidden content", importance: 3 });
197
+ store.setArchived(saved.id, true);
198
+ assert.equal(store.search("secret").length, 0);
199
+ assert.equal(store.search("secret", { includeArchived: true }).length, 1);
200
+ store.close();
201
+ });
202
+
203
+ // --- vector search ---
204
+
205
+ test("save/update persist embedding vector and searchVector ranks by cosine", () => {
206
+ const store = openMemory();
207
+ const a = store.save({ type: "preference", title: "猫", content: "喜欢猫", embedding: [1, 0, 0] });
208
+ const b = store.save({ type: "preference", title: "狗", content: "喜欢狗", embedding: [0, 1, 0] });
209
+ const c = store.save({ type: "preference", title: "猫狗", content: "都养", embedding: [0.9, 0.1, 0] });
210
+
211
+ const hits = store.searchVector([1, 0, 0], { limit: 3 });
212
+ assert.deepEqual(hits.map((m) => m.id), [a.id, c.id, b.id]);
213
+ assert.equal(hits[0].score, 1);
214
+ assert.ok(hits[1].score > hits[2].score);
215
+
216
+ // update re-stores embedding
217
+ store.update(b.id, { embedding: [1, 1, 0] });
218
+ const hits2 = store.searchVector([1, 1, 0], { limit: 3 });
219
+ assert.equal(hits2[0].id, b.id);
220
+ store.close();
221
+ });
222
+
223
+ test("searchVector only considers rows with an embedding and filters forgotten/archived", () => {
224
+ const store = openMemory();
225
+ const plain = store.save({ type: "project", title: "无向量", content: "x" });
226
+ const withVec = store.save({ type: "project", title: "有向量", content: "y", embedding: [1, 0, 0] });
227
+ store.setForget(withVec.id, true);
228
+ assert.deepEqual(store.searchVector([1, 0, 0]).map((m) => m.id), []);
229
+ store.setForget(withVec.id, false);
230
+ store.setArchived(withVec.id, true);
231
+ assert.deepEqual(store.searchVector([1, 0, 0]).map((m) => m.id), []);
232
+ store.setArchived(withVec.id, false);
233
+ assert.deepEqual(store.searchVector([1, 0, 0]).map((m) => m.id), [withVec.id]);
234
+ assert.equal(plain.id, plain.id, "plain row keeps id");
235
+ store.close();
236
+ });
237
+
238
+ test("setEmbedding, embeddedCount, needsEmbedding and threshold filtering", () => {
239
+ const store = openMemory();
240
+ const t1 = store.save({ type: "project", title: "t1", content: "c1" });
241
+ const m2 = store.save({ type: "project", title: "t2", content: "c2" });
242
+ assert.equal(store.embeddedCount(), 0);
243
+ store.setEmbedding(m2.id, [0, 1]);
244
+ assert.equal(store.embeddedCount(), 1);
245
+ const missing = store.needsEmbedding(10);
246
+ assert.equal(missing.length, 1);
247
+ assert.equal(missing[0].id, t1.id, "only the non-embedded row is listed");
248
+
249
+ const t3 = store.save({ type: "project", title: "t3", content: "c3", embedding: [1, 1] });
250
+ // threshold 0.99: only t3 (cos=1) survives; m2 ([0,1]) scores ~0.707.
251
+ const near = store.searchVector([1, 1], { limit: 5, threshold: 0.99 });
252
+ assert.deepEqual(near.map((m) => m.id), [t3.id]);
253
+ store.close();
254
+ });
255
+
256
+ test("save/update preserve archived flag", () => {
257
+ const store = openMemory();
258
+ const saved = store.save({ type: "project", title: "t", content: "c" });
259
+ store.setArchived(saved.id, true);
260
+ const updated = store.update(saved.id, { content: "new" });
261
+ assert.equal(updated.archived, true, "update keeps archived");
262
+ store.close();
263
+ });
264
+
265
+ // --- compare-and-set update (item ①/③) ------------------------------------
266
+
267
+ test("compareAndUpdate applies when the version token still matches", () => {
268
+ const store = openMemory();
269
+ const saved = store.save({ type: "project", title: "t", content: "count=0" });
270
+ const before = store.getById(saved.id);
271
+ const updated = store.compareAndUpdate(saved.id, before.updated_at, { content: "count=1" });
272
+ assert.ok(updated, "CAS with the current version succeeds");
273
+ assert.equal(updated.content, "count=1");
274
+ assert.notEqual(updated.updated_at, before.updated_at, "version token advances");
275
+ store.close();
276
+ });
277
+
278
+ test("compareAndUpdate rejects a stale version token without writing", () => {
279
+ const store = openMemory();
280
+ const saved = store.save({ type: "project", title: "t", content: "count=0" });
281
+ const stale = store.getById(saved.id).updated_at;
282
+ store.update(saved.id, { content: "count=1" }); // concurrent write wins
283
+ const result = store.compareAndUpdate(saved.id, stale, { content: "count=2" });
284
+ assert.equal(result, undefined, "stale CAS is a miss");
285
+ assert.equal(store.getById(saved.id).content, "count=1", "no lost update");
286
+ store.close();
287
+ });
288
+
289
+ test("compareAndUpdate on unknown id throws like update", () => {
290
+ const store = openMemory();
291
+ assert.throws(() => store.compareAndUpdate("ghost", "any", { content: "x" }), /not found/);
292
+ store.close();
293
+ });
@@ -0,0 +1,209 @@
1
+ import test from "node:test";
2
+ import assert from "node:assert/strict";
3
+ import { mkdtempSync } from "node:fs";
4
+ import { tmpdir } from "node:os";
5
+ import { join } from "node:path";
6
+ import { createStore } from "../src/store.js";
7
+ import { createService } from "../src/service.js";
8
+ import { createDreamScheduler, parseReceipt } from "../src/dream.js";
9
+ import { applyDecisions } from "../src/dream/decisions.js";
10
+ import {
11
+ sessionDecisions,
12
+ arbitrationDecisions,
13
+ mockCtx
14
+ } from "./helpers/dream-mock.js";
15
+
16
+ function setup() {
17
+ const store = createStore(":memory:");
18
+ const service = createService({ store, mirror: null, config: {} });
19
+ const dream = createDreamScheduler({ onRun: () => Promise.resolve({ ok: true, skipped: true }) });
20
+ return { store, service, dream };
21
+ }
22
+
23
+ function tmpPath(prefix) {
24
+ return join(mkdtempSync(join(tmpdir(), prefix)), "memory.db");
25
+ }
26
+
27
+ // ---------------------------------------------------------------- 轴线 1
28
+
29
+ test("long-session stress: Recall@k stays high, stale residual drops to zero", async () => {
30
+ const { store, service, dream } = setup();
31
+ const ctx = mockCtx({ onConsolidation: sessionDecisions });
32
+ const topics = 5;
33
+ const rounds = 3;
34
+
35
+ const gt = [];
36
+ for (let t = 1; t <= topics; t++) {
37
+ const title = `主题${String(t).padStart(2, "0")}`;
38
+ const { memory } = service.saveWithDedupe({ type: "project", title, content: `${title} 规范内容`, importance: 5 });
39
+ gt.push({ title, id: memory.id });
40
+ }
41
+ for (let r = 1; r <= rounds; r++) {
42
+ for (let t = 1; t <= topics; t++) {
43
+ service.saveWithDedupe({
44
+ type: "project",
45
+ title: `主题${String(t).padStart(2, "0")}·变体${r}`,
46
+ content: "旧变体",
47
+ importance: 3
48
+ });
49
+ }
50
+ const result = await dream.runDream(ctx, service, {});
51
+ assert.equal(result.ok, true);
52
+ }
53
+
54
+ let hits = 0;
55
+ for (const { title, id } of gt) {
56
+ if (service.search(title, { limit: 5 }).some((m) => m.id === id)) hits++;
57
+ }
58
+ assert.equal(hits, topics, "every canonical memory recalled in top-5 after 3 consolidation rounds");
59
+
60
+ const all = store.all();
61
+ const variants = all.filter((m) => m.type === "project" && m.title.includes("变体"));
62
+ assert.equal(variants.filter((m) => !m.archived).length, 0, "no stale variants remain active");
63
+ assert.equal(store.listDreamRuns().length, rounds, "every run written to the audit trail");
64
+ store.close();
65
+ });
66
+
67
+ // ---------------------------------------------------------------- 轴线 2
68
+
69
+ test("arbitration set is replayable: deterministic, correct, idempotent replay", async () => {
70
+ const { store, service, dream } = setup();
71
+ const ctx = mockCtx({ onConsolidation: arbitrationDecisions });
72
+ const sets = [
73
+ { w: { type: "decision", title: "截止", content: "8月20日", importance: 5 }, l: { type: "decision", title: "截止(旧)", content: "8月15日", importance: 3 } },
74
+ { w: { type: "preference", title: "语言", content: "简体中文", importance: 5 }, l: { type: "preference", title: "语言(旧)", content: "繁体中文", importance: 2 } }
75
+ ];
76
+ for (const s of sets) {
77
+ service.saveWithDedupe({ ...s.l });
78
+ service.saveWithDedupe({ ...s.w });
79
+ }
80
+
81
+ const run = await dream.runDream(ctx, service, {});
82
+ assert.equal(run.ok, true);
83
+ assert.equal(run.applied, sets.length, "one conflict per arbitration set");
84
+
85
+ // correctness: winner kept (with provenance), loser archived
86
+ for (const s of sets) {
87
+ const winner = service.list({ type: s.w.type }).find((m) => m.title === s.w.title);
88
+ const loser = store.all().find((m) => m.title === s.l.title);
89
+ assert.ok(winner, "winner present");
90
+ assert.ok(loser?.archived, "loser archived");
91
+ assert.ok(winner.content.includes("已否决旧信息"), "provenance note appended");
92
+ }
93
+
94
+ // audit + receipt are replayable
95
+ const audit = store.getDreamRun(run.runId);
96
+ assert.equal(audit.status, "ok");
97
+ const receipt = parseReceipt(audit.receipt);
98
+ assert.equal(receipt.status, "ok");
99
+ assert.equal(Object.keys(audit.outcome.byId).length, sets.length * 2, "outcome covers every arbitrated id");
100
+ assert.equal(audit.decisions.length, sets.length, "raw decisions persisted");
101
+
102
+ // re-applying the recorded decision list is a no-op (idempotent replay)
103
+ assert.equal(applyDecisions(audit.decisions, service).applied, 0, "replay applies nothing");
104
+ store.close();
105
+ });
106
+
107
+ // ---------------------------------------------------------------- 轴线 3
108
+
109
+ function bump(content) {
110
+ return `count=${Number(content.match(/count=(\d+)/)[1]) + 1}`;
111
+ }
112
+
113
+ test("concurrent agents: no duplicate merge, lost-update reproduced then fixed, crash recovery", () => {
114
+ const path = tmpPath("dsh-mneme-stress-test-");
115
+ const sA = createStore(path);
116
+ const svA = createService({ store: sA, mirror: null, config: {} });
117
+
118
+ // duplicate merge: 20 agents save the same title → exactly one active row
119
+ for (let i = 0; i < 20; i++) {
120
+ svA.saveWithDedupe({ type: "preference", title: "并发任务", content: `agent-${i}` });
121
+ }
122
+ assert.equal(
123
+ svA.list({ type: "preference", limit: 100 }).filter((m) => m.title === "并发任务").length,
124
+ 1,
125
+ "no duplicate rows after concurrent same-title saves"
126
+ );
127
+
128
+ // lost update: two connections read the same baseline, then both write
129
+ const sB = createStore(path);
130
+ const svB = createService({ store: sB, mirror: null, config: {} });
131
+ svA.saveWithDedupe({ type: "history", title: "计数器", content: "count=0", importance: 3 });
132
+ const id = svA.list({ type: "history" })[0].id;
133
+
134
+ const readA = svA.getById(id).content; // count=0
135
+ const readB = svB.getById(id).content; // count=0 (stale)
136
+ svA.update(id, { content: bump(readA) }); // count=1
137
+ svB.update(id, { content: bump(readB) }); // count=1 → overwrites A's increment
138
+ assert.equal(svB.getById(id).content, "count=1", "unlocked read-modify-write loses an increment");
139
+
140
+ // serialized fix: re-read the latest value before writing
141
+ svA.update(id, { content: "count=0" });
142
+ svA.update(id, { content: bump(svA.getById(id).content) }); // reads 0 → 1
143
+ svB.update(id, { content: bump(svB.getById(id).content) }); // reads 1 → 2
144
+ assert.equal(svB.getById(id).content, "count=2", "re-read before write keeps both increments");
145
+
146
+ // crash recovery: committed writes survive an exception and a reopen
147
+ svA.saveWithDedupe({ type: "project", title: "已提交", content: "x" });
148
+ assert.throws(() => svA.update("ghost", { content: "boom" }), /not found/, "unknown-id update throws");
149
+ sA.close();
150
+ sB.close();
151
+
152
+ const sR = createStore(path);
153
+ assert.ok(sR.getById(id), "counter survives reopen");
154
+ assert.ok(sR.all().some((m) => m.title === "已提交"), "committed write survives reopen");
155
+ assert.equal(sR.all().filter((m) => m.title === "已提交").length, 1, "no partial residue");
156
+ sR.close();
157
+ });
158
+
159
+ // ---------------------------------------------------------------- CAS + atomicity (item ③)
160
+
161
+ test("CAS concurrent increments: stale version rejected, retry lands both increments", () => {
162
+ const path = tmpPath("dsh-mneme-stress-cas-");
163
+ const sA = createStore(path);
164
+ const svA = createService({ store: sA, mirror: null, config: {} });
165
+ const sB = createStore(path);
166
+ const svB = createService({ store: sB, mirror: null, config: {} });
167
+
168
+ svA.saveWithDedupe({ type: "history", title: "计数器", content: "count=0", importance: 3 });
169
+ const id = svA.list({ type: "history" })[0].id;
170
+ svA.update(id, { content: "count=0" });
171
+ const baseline = svA.getById(id); // updated_at=T, count=0
172
+
173
+ const aOk = svA.compareAndUpdate(id, baseline.updated_at, { content: bump("count=0") });
174
+ assert.ok(aOk, "current-version CAS lands");
175
+ const bStale = svB.compareAndUpdate(id, baseline.updated_at, { content: bump("count=0") });
176
+ assert.equal(bStale, undefined, "stale-version CAS rejected — no lost update");
177
+
178
+ const cur = svB.getById(id);
179
+ svB.compareAndUpdate(id, cur.updated_at, { content: bump(cur.content) });
180
+ assert.equal(svB.getById(id).content, "count=2", "re-read + retry keeps both increments");
181
+ sA.close();
182
+ sB.close();
183
+ });
184
+
185
+ test("multi-step transaction is atomic: a mid-way throw rolls back every step", () => {
186
+ const path = tmpPath("dsh-mneme-stress-atomic-");
187
+ const s = createStore(path);
188
+ const sv = createService({ store: s, mirror: null, config: {} });
189
+
190
+ assert.throws(
191
+ () => sv.transaction(() => {
192
+ sv.saveWithDedupe({ type: "project", title: "原子A", content: "x" });
193
+ sv.saveWithDedupe({ type: "project", title: "原子B", content: "y" });
194
+ throw new Error("boom");
195
+ }),
196
+ /boom/
197
+ );
198
+ assert.ok(!s.all().some((m) => m.title === "原子A"), "atomicA fully rolled back");
199
+ assert.ok(!s.all().some((m) => m.title === "原子B"), "atomicB fully rolled back");
200
+
201
+ // a clean transaction commits both steps
202
+ sv.transaction(() => {
203
+ sv.saveWithDedupe({ type: "project", title: "完整A", content: "x" });
204
+ sv.saveWithDedupe({ type: "project", title: "完整B", content: "y" });
205
+ });
206
+ assert.equal(s.all().filter((m) => m.title === "完整A").length, 1, "committed step A");
207
+ assert.equal(s.all().filter((m) => m.title === "完整B").length, 1, "committed step B");
208
+ s.close();
209
+ });
@@ -0,0 +1,156 @@
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 { createSummarizer, parseSummaryJson } from "../src/summarize.js";
6
+
7
+ function setup(over = {}, opts = {}) {
8
+ const store = createStore(":memory:");
9
+ const service = createService({ store, mirror: null, config: {} });
10
+ const events = [];
11
+ const calls = [];
12
+ const ctx = {
13
+ on(name, fn) {
14
+ events.push({ name, fn });
15
+ return () => {
16
+ const i = events.findIndex((e) => e.name === name && e.fn === fn);
17
+ if (i !== -1) events.splice(i, 1);
18
+ };
19
+ },
20
+ llm: {
21
+ stream(options) {
22
+ calls.push(options);
23
+ if (opts.stream) return opts.stream(options);
24
+ const json = JSON.stringify([
25
+ { type: "decision", title: "选型", content: "确定用 node:sqlite", importance: 4 },
26
+ { type: "preference", title: "语言", content: "用户喜欢中文交流", importance: 5 }
27
+ ]);
28
+ return (async function* () {
29
+ yield { type: "block-start", block: { type: "text" } };
30
+ yield { type: "text-delta", delta: json };
31
+ yield { type: "block-end", block: { type: "text" } };
32
+ yield { type: "finish", kind: "ok" };
33
+ })();
34
+ }
35
+ }
36
+ };
37
+ const config = { autoSummarize: true, ...over };
38
+ const summarizer = createSummarizer(ctx, service, config);
39
+ return { store, service, events, calls, summarizer };
40
+ }
41
+
42
+ // A realistic direct human prompt event (source.kind === "user").
43
+ function userMessage(text) {
44
+ return {
45
+ type: "user/message",
46
+ data: { source: { kind: "user" }, content: [{ type: "text", text }] }
47
+ };
48
+ }
49
+
50
+ test("parseSummaryJson extracts valid entries and skips malformed ones", () => {
51
+ const parsed = parseSummaryJson(`前导文字 {"a":1}
52
+ [
53
+ {"type":"decision","title":"t1","content":"c1","importance":4},
54
+ {"type":"nonsense","title":"bad","content":"x"},
55
+ "garbage",
56
+ {"type":"preference","title":"t2","content":"c2","importance":2}
57
+ ]`);
58
+ assert.equal(parsed.length, 2);
59
+ assert.equal(parsed[0].type, "decision");
60
+ assert.equal(parsed[1].type, "preference");
61
+ });
62
+
63
+ test("subscribes to session/event when autoSummarize enabled", () => {
64
+ const { events } = setup();
65
+ assert.ok(events.some((e) => e.name === "session/event"));
66
+ });
67
+
68
+ test("does not subscribe when autoSummarize disabled", () => {
69
+ const { events } = setup({ autoSummarize: false });
70
+ assert.ok(!events.some((e) => e.name === "session/event"));
71
+ });
72
+
73
+ test("turn/end event triggers summarization and stores entries", async () => {
74
+ const { events, store } = setup();
75
+ const handler = events.find((e) => e.name === "session/event").fn;
76
+ const session = {
77
+ id: "s1",
78
+ requestHeader: () => ({ config: { provider: "deepseek", model: "deepseek-chat" } }),
79
+ events: [userMessage("帮我选型"), { seq: 2, type: "turn/end" }]
80
+ };
81
+ await handler(session, { seq: 2, type: "turn/end" });
82
+ assert.equal(store.count(), 2);
83
+ const all = store.all();
84
+ assert.ok(all.some((m) => m.type === "decision"));
85
+ assert.ok(all.some((m) => m.type === "preference"));
86
+ });
87
+
88
+ test("skips summarization for events other than turn/end", async () => {
89
+ const { events, store, calls } = setup();
90
+ const handler = events.find((e) => e.name === "session/event").fn;
91
+ const session = { id: "s1", requestHeader: () => ({ config: {} }), events: [] };
92
+ await handler(session, { seq: 1, type: "user/message" });
93
+ assert.equal(store.count(), 0);
94
+ assert.equal(calls.length, 0);
95
+ });
96
+
97
+ test("dispose unsubscribes and stops later turn/end events from summarizing", async () => {
98
+ const { events, summarizer, calls } = setup();
99
+ const handler = events.find((e) => e.name === "session/event").fn;
100
+ summarizer.dispose();
101
+ // The ctx.on() disposer must have removed the listener.
102
+ assert.ok(!events.some((e) => e.name === "session/event"));
103
+ const session = {
104
+ id: "s1",
105
+ requestHeader: () => ({ config: { provider: "deepseek", model: "deepseek-chat" } }),
106
+ events: [userMessage("你好"), { seq: 2, type: "turn/end" }]
107
+ };
108
+ // Even a stale handler reference must not start a new LLM call.
109
+ await handler(session, { seq: 2, type: "turn/end" });
110
+ assert.equal(calls.length, 0);
111
+ });
112
+
113
+ test("excludes plugin-injected user/message events from summarization input", async () => {
114
+ const { events, store, calls } = setup();
115
+ const handler = events.find((e) => e.name === "session/event").fn;
116
+ const session = {
117
+ id: "s2",
118
+ requestHeader: () => ({ config: { provider: "deepseek", model: "deepseek-chat" } }),
119
+ events: [
120
+ {
121
+ seq: 1,
122
+ type: "user/message",
123
+ data: { source: { kind: "plugin" }, content: [{ type: "text", text: "AGENTS.md 内容" }] }
124
+ },
125
+ userMessage("帮我看看这个报错"),
126
+ { seq: 3, type: "turn/end" }
127
+ ]
128
+ };
129
+ await handler(session, { seq: 3, type: "turn/end" });
130
+ assert.equal(calls.length, 1);
131
+ const userMessages = calls[0].messages.filter((m) => m.role === "user");
132
+ assert.equal(userMessages.length, 1);
133
+ assert.ok(!JSON.stringify(calls[0].messages).includes("AGENTS.md"));
134
+ assert.equal(store.count(), 2);
135
+ });
136
+
137
+ test("aborted finish does not store entries", async () => {
138
+ const { events, store, calls } = setup({}, {
139
+ stream() {
140
+ return (async function* () {
141
+ yield { type: "block-start", block: { type: "text" } };
142
+ yield { type: "text-delta", delta: "[]" };
143
+ yield { type: "finish", kind: "aborted" };
144
+ })();
145
+ }
146
+ });
147
+ const handler = events.find((e) => e.name === "session/event").fn;
148
+ const session = {
149
+ id: "s3",
150
+ requestHeader: () => ({ config: { provider: "deepseek", model: "deepseek-chat" } }),
151
+ events: [userMessage("继续"), { seq: 2, type: "turn/end" }]
152
+ };
153
+ await handler(session, { seq: 2, type: "turn/end" });
154
+ assert.equal(calls.length, 1); // the stream was actually reached
155
+ assert.equal(store.count(), 0);
156
+ });