@modusensus/dsh-mneme 0.6.7 → 0.6.8
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.
- package/LICENSE +21 -21
- package/README.md +463 -463
- package/cordis.patch.yml +15 -15
- package/lib/api.js +783 -783
- package/lib/client.js +1757 -1757
- package/lib/commands.js +64 -64
- package/lib/config.js +288 -288
- package/lib/dream/clustering.js +118 -118
- package/lib/dream/decisions.js +439 -439
- package/lib/dream/sleep.js +561 -554
- package/lib/dream/tag-extractor.js +156 -156
- package/lib/dream.js +935 -929
- package/lib/embedding.js +154 -154
- package/lib/entities/extractor.js +242 -242
- package/lib/hot-memory.js +53 -53
- package/lib/index.js +361 -361
- package/lib/inject.js +208 -208
- package/lib/local-embedder.js +282 -282
- package/lib/mirror.js +170 -170
- package/lib/parser/tag.js +59 -59
- package/lib/parser/wiki-link.js +38 -38
- package/lib/quality-filter.js +123 -123
- package/lib/reranker.js +218 -218
- package/lib/search/adaptive.js +22 -22
- package/lib/search/bm25.js +96 -96
- package/lib/search/tag-boost.js +61 -61
- package/lib/service.js +1726 -1726
- package/lib/settings.js +172 -172
- package/lib/store.js +2238 -2238
- package/lib/summarize.js +236 -236
- package/lib/tools.js +290 -290
- package/lib/vector-index.js +116 -116
- package/package.json +80 -80
- package/scripts/benchmark-embed.js +201 -201
- package/scripts/benchmark-recall.js +133 -133
- package/scripts/benchmark-rerank.js +166 -166
- package/scripts/e2e-dsh.js +218 -218
- package/scripts/stress-dsh.js +255 -255
- package/scripts/sync-lib.js +52 -52
- package/src/api.js +783 -783
- package/src/commands.js +64 -64
- package/src/config.js +288 -288
- package/src/dream/clustering.js +118 -118
- package/src/dream/decisions.js +439 -439
- package/src/dream/sleep.js +561 -554
- package/src/dream/tag-extractor.js +156 -156
- package/src/dream.js +935 -929
- package/src/embedding.js +154 -154
- package/src/entities/extractor.js +242 -242
- package/src/hot-memory.js +53 -53
- package/src/index.js +361 -361
- package/src/inject.js +208 -208
- package/src/local-embedder.js +282 -282
- package/src/mirror.js +170 -170
- package/src/parser/tag.js +59 -59
- package/src/parser/wiki-link.js +38 -38
- package/src/quality-filter.js +123 -123
- package/src/reranker.js +218 -218
- package/src/search/adaptive.js +22 -22
- package/src/search/bm25.js +96 -96
- package/src/search/tag-boost.js +61 -61
- package/src/service.js +1726 -1726
- package/src/settings.js +172 -172
- package/src/store.js +2238 -2238
- package/src/summarize.js +236 -236
- package/src/tools.js +290 -290
- package/src/vector-index.js +116 -116
- package/test/api.test.js +594 -594
- package/test/audit.test.js +448 -448
- package/test/benchmark.test.js +35 -35
- package/test/boundary-v0625.test.js +82 -82
- package/test/client.test.js +368 -368
- package/test/clustering.test.js +100 -100
- package/test/commands.test.js +69 -69
- package/test/config.test.js +50 -50
- package/test/conflict-freeze.test.js +290 -290
- package/test/directory.test.js +134 -134
- package/test/dream.test.js +903 -901
- package/test/entities.test.js +522 -522
- package/test/epistemic.test.js +298 -298
- package/test/fnew-0112.test.js +311 -311
- package/test/fnew-03.test.js +422 -422
- package/test/graph-api.test.js +175 -175
- package/test/helpers/dream-mock.js +82 -82
- package/test/hot-memory.test.js +174 -174
- package/test/inject.test.js +103 -103
- package/test/llm-audit.test.js +279 -279
- package/test/local-embedder.test.js +227 -227
- package/test/mirror-dirty.test.js +424 -424
- package/test/mirror-edit-digest.test.js +187 -187
- package/test/mirror-generation.test.js +499 -499
- package/test/mirror.test.js +249 -249
- package/test/normalize-decisions.test.js +120 -120
- package/test/peer-blockers.test.js +190 -190
- package/test/policy-epoch.test.js +259 -259
- package/test/provenance.test.js +103 -103
- package/test/quality-filter.test.js +118 -118
- package/test/reasoning-effort.test.js +199 -199
- package/test/recall-evals.test.js +235 -235
- package/test/recall-layer.test.js +315 -315
- package/test/receipt-chain.test.js +451 -451
- package/test/reflection.test.js +226 -226
- package/test/reranker.test.js +240 -240
- package/test/search-fusion.test.js +90 -90
- package/test/semantic.test.js +124 -124
- package/test/service-search.test.js +199 -199
- package/test/service.test.js +435 -435
- package/test/settings.test.js +118 -118
- package/test/sleep.test.js +365 -365
- package/test/store.test.js +436 -436
- package/test/stress.test.js +209 -209
- package/test/summarize.test.js +191 -191
- package/test/tag-boost.test.js +125 -125
- package/test/tag.test.js +312 -312
- package/test/tools.test.js +285 -285
- package/test/vector-index.test.js +221 -221
- package/test/wiki-link.test.js +332 -332
package/test/store.test.js
CHANGED
|
@@ -1,436 +1,436 @@
|
|
|
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
|
-
});
|
|
294
|
-
|
|
295
|
-
// --- conflict freeze: pending manual review -------------------------------
|
|
296
|
-
|
|
297
|
-
test("saveConflictPending inserts and listConflictPending excludes resolved by default", () => {
|
|
298
|
-
const store = openMemory();
|
|
299
|
-
const a = store.save({ type: "decision", title: "截止", content: "8月20日", importance: 4 });
|
|
300
|
-
const b = store.save({ type: "decision", title: "截止旧", content: "8月15日", importance: 4 });
|
|
301
|
-
const pending = store.saveConflictPending({ run_id: "run-1", memory_a: a.id, memory_b: b.id, reason: "日期更新" });
|
|
302
|
-
assert.ok(pending.id, "has id");
|
|
303
|
-
assert.equal(pending.run_id, "run-1");
|
|
304
|
-
assert.equal(pending.reason, "日期更新");
|
|
305
|
-
assert.ok(pending.created_at);
|
|
306
|
-
assert.equal(pending.resolved_at, undefined);
|
|
307
|
-
|
|
308
|
-
const list = store.listConflictPending();
|
|
309
|
-
assert.equal(list.length, 1);
|
|
310
|
-
assert.ok([list[0].memory_a, list[0].memory_b].includes(a.id), "pair holds both sides");
|
|
311
|
-
assert.ok([list[0].memory_a, list[0].memory_b].includes(b.id));
|
|
312
|
-
|
|
313
|
-
const resolved = store.resolveConflictPending(pending.id, { winner: a.id });
|
|
314
|
-
assert.ok(resolved.resolved_at, "resolution stamped");
|
|
315
|
-
assert.equal(resolved.resolved_winner, a.id);
|
|
316
|
-
assert.equal(store.listConflictPending().length, 0, "resolved excluded by default");
|
|
317
|
-
const all = store.listConflictPending({ includeResolved: true });
|
|
318
|
-
assert.equal(all.length, 1, "resolved visible with includeResolved");
|
|
319
|
-
assert.equal(all[0].resolved_winner, a.id);
|
|
320
|
-
store.close();
|
|
321
|
-
});
|
|
322
|
-
|
|
323
|
-
test("saveConflictPending dedupes the same pair regardless of order", () => {
|
|
324
|
-
const store = openMemory();
|
|
325
|
-
const a = store.save({ type: "decision", title: "截止", content: "x" });
|
|
326
|
-
const b = store.save({ type: "decision", title: "截止2", content: "y" });
|
|
327
|
-
const p1 = store.saveConflictPending({ memory_a: a.id, memory_b: b.id, reason: "r1" });
|
|
328
|
-
const p2 = store.saveConflictPending({ memory_a: b.id, memory_b: a.id, reason: "r2" });
|
|
329
|
-
assert.equal(p2.id, p1.id, "same pair re-detected returns the existing pending row");
|
|
330
|
-
assert.equal(p2.reason, "r1", "original reason preserved");
|
|
331
|
-
assert.equal(store.listConflictPending().length, 1, "never a duplicate queue entry");
|
|
332
|
-
store.close();
|
|
333
|
-
});
|
|
334
|
-
|
|
335
|
-
test("countConflictPending counts unresolved rows only; resolve unknown id is undefined", () => {
|
|
336
|
-
const store = openMemory();
|
|
337
|
-
const a = store.save({ type: "decision", title: "a", content: "x" });
|
|
338
|
-
const b = store.save({ type: "decision", title: "b", content: "y" });
|
|
339
|
-
const c = store.save({ type: "decision", title: "c", content: "z" });
|
|
340
|
-
store.saveConflictPending({ memory_a: a.id, memory_b: b.id, reason: "ab" });
|
|
341
|
-
const p2 = store.saveConflictPending({ memory_a: b.id, memory_b: c.id, reason: "bc" });
|
|
342
|
-
assert.equal(store.countConflictPending(), 2);
|
|
343
|
-
store.resolveConflictPending(p2.id, { winner: b.id });
|
|
344
|
-
assert.equal(store.countConflictPending(), 1, "resolved no longer pending");
|
|
345
|
-
assert.equal(store.resolveConflictPending("ghost"), undefined, "unknown id resolves to undefined");
|
|
346
|
-
store.close();
|
|
347
|
-
});
|
|
348
|
-
|
|
349
|
-
test("conflict_pending table persists across store reopen", () => {
|
|
350
|
-
const dir = mkdtempSync(join(tmpdir(), "dsh-mneme-conflict-"));
|
|
351
|
-
const path = join(dir, "memory.db");
|
|
352
|
-
const s1 = createStore(path);
|
|
353
|
-
s1.saveConflictPending({ run_id: "run-1", memory_a: "ma", memory_b: "mb", reason: "x" });
|
|
354
|
-
s1.close();
|
|
355
|
-
const s2 = createStore(path);
|
|
356
|
-
const pending = s2.listConflictPending();
|
|
357
|
-
assert.equal(pending.length, 1);
|
|
358
|
-
assert.equal(pending[0].reason, "x");
|
|
359
|
-
s2.close();
|
|
360
|
-
});
|
|
361
|
-
|
|
362
|
-
// --- session lifecycle (v0.6.0): disposed rows are filtered out ------------
|
|
363
|
-
|
|
364
|
-
test("session_disposed_at column exists and survives migration", () => {
|
|
365
|
-
const dir = mkdtempSync(join(tmpdir(), "dsh-mneme-disposed-"));
|
|
366
|
-
const path = join(dir, "memory.db");
|
|
367
|
-
const s1 = createStore(path);
|
|
368
|
-
const { id } = s1.save({ type: "decision", title: "迁移", content: "c", session_id: "sess-m" });
|
|
369
|
-
s1.setDisposedBySession("sess-m", true);
|
|
370
|
-
const row = s1.db.prepare("SELECT session_disposed_at FROM memories WHERE id = ?").get(id);
|
|
371
|
-
assert.ok(row.session_disposed_at, "dispose timestamp persisted");
|
|
372
|
-
s1.close();
|
|
373
|
-
// reopen: column survives
|
|
374
|
-
const s2 = createStore(path);
|
|
375
|
-
const live = s2.list({ type: "decision" });
|
|
376
|
-
assert.ok(!live.some((m) => m.id === id), "disposed row excluded after reopen");
|
|
377
|
-
s2.close();
|
|
378
|
-
});
|
|
379
|
-
|
|
380
|
-
test("search and list exclude disposed rows unless includeDisposed", () => {
|
|
381
|
-
const store = createStore(":memory:");
|
|
382
|
-
const a = store.save({ type: "decision", title: "活着", content: "keyword alpha", session_id: "s1" });
|
|
383
|
-
const b = store.save({ type: "decision", title: "被销毁", content: "keyword beta", session_id: "s2" });
|
|
384
|
-
store.setDisposedBySession("s2", true);
|
|
385
|
-
|
|
386
|
-
const searchRes = store.search("keyword");
|
|
387
|
-
assert.ok(searchRes.some((m) => m.id === a.id), "live row found");
|
|
388
|
-
assert.ok(!searchRes.some((m) => m.id === b.id), "disposed row excluded from search");
|
|
389
|
-
const searchIncl = store.search("keyword", { includeDisposed: true });
|
|
390
|
-
assert.ok(searchIncl.some((m) => m.id === b.id), "includeDisposed surfaces it");
|
|
391
|
-
|
|
392
|
-
const listRes = store.list({ type: "decision" });
|
|
393
|
-
assert.ok(listRes.some((m) => m.id === a.id));
|
|
394
|
-
assert.ok(!listRes.some((m) => m.id === b.id), "disposed excluded from list");
|
|
395
|
-
const listIncl = store.list({ type: "decision", includeDisposed: true });
|
|
396
|
-
assert.ok(listIncl.some((m) => m.id === b.id), "includeDisposed lists it");
|
|
397
|
-
assert.equal(store.count("decision", { includeDisposed: true }), 2);
|
|
398
|
-
});
|
|
399
|
-
|
|
400
|
-
test("searchVector excludes disposed rows", () => {
|
|
401
|
-
const store = createStore(":memory:");
|
|
402
|
-
const a = store.save({ type: "decision", title: "向量A", content: "v", session_id: "s1" });
|
|
403
|
-
const b = store.save({ type: "decision", title: "向量B", content: "v", session_id: "s2" });
|
|
404
|
-
store.setEmbedding(a.id, [1, 0, 0]);
|
|
405
|
-
store.setEmbedding(b.id, [1, 0, 0]);
|
|
406
|
-
store.setDisposedBySession("s2", true);
|
|
407
|
-
|
|
408
|
-
const res = store.searchVector([1, 0, 0], { limit: 10 });
|
|
409
|
-
assert.ok(res.some((m) => m.id === a.id));
|
|
410
|
-
assert.ok(!res.some((m) => m.id === b.id), "disposed row excluded from vector search");
|
|
411
|
-
});
|
|
412
|
-
|
|
413
|
-
test("setDisposedBySession round-trips and listBySession sees all states", () => {
|
|
414
|
-
const store = createStore(":memory:");
|
|
415
|
-
const a = store.save({ type: "decision", title: "会话记忆", content: "c", session_id: "sess-z" });
|
|
416
|
-
assert.equal(store.setDisposedBySession("sess-z", true), 1, "first dispose flips 1");
|
|
417
|
-
assert.equal(store.setDisposedBySession("sess-z", true), 0, "re-dispose is a no-op");
|
|
418
|
-
assert.equal(store.setDisposedBySession("sess-z", false), 1, "restore flips 1");
|
|
419
|
-
assert.equal(store.setDisposedBySession("sess-z", false), 0, "re-restore is a no-op");
|
|
420
|
-
assert.equal(store.getById(a.id).session_disposed_at, undefined, "mark cleared after restore");
|
|
421
|
-
|
|
422
|
-
const bySession = store.listBySession("sess-z");
|
|
423
|
-
assert.equal(bySession.length, 1);
|
|
424
|
-
assert.equal(bySession[0].id, a.id);
|
|
425
|
-
});
|
|
426
|
-
|
|
427
|
-
test("listBySession hides disposed by default, includeDisposed opts in", () => {
|
|
428
|
-
const store = createStore(":memory:");
|
|
429
|
-
const a = store.save({ type: "decision", title: "会话记忆", content: "c", session_id: "sess-y" });
|
|
430
|
-
store.setDisposedBySession("sess-y", true);
|
|
431
|
-
|
|
432
|
-
assert.equal(store.listBySession("sess-y").length, 0, "disposed row hidden by default");
|
|
433
|
-
const all = store.listBySession("sess-y", { includeDisposed: true });
|
|
434
|
-
assert.equal(all.length, 1, "includeDisposed reveals it");
|
|
435
|
-
assert.equal(all[0].id, a.id);
|
|
436
|
-
});
|
|
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
|
+
});
|
|
294
|
+
|
|
295
|
+
// --- conflict freeze: pending manual review -------------------------------
|
|
296
|
+
|
|
297
|
+
test("saveConflictPending inserts and listConflictPending excludes resolved by default", () => {
|
|
298
|
+
const store = openMemory();
|
|
299
|
+
const a = store.save({ type: "decision", title: "截止", content: "8月20日", importance: 4 });
|
|
300
|
+
const b = store.save({ type: "decision", title: "截止旧", content: "8月15日", importance: 4 });
|
|
301
|
+
const pending = store.saveConflictPending({ run_id: "run-1", memory_a: a.id, memory_b: b.id, reason: "日期更新" });
|
|
302
|
+
assert.ok(pending.id, "has id");
|
|
303
|
+
assert.equal(pending.run_id, "run-1");
|
|
304
|
+
assert.equal(pending.reason, "日期更新");
|
|
305
|
+
assert.ok(pending.created_at);
|
|
306
|
+
assert.equal(pending.resolved_at, undefined);
|
|
307
|
+
|
|
308
|
+
const list = store.listConflictPending();
|
|
309
|
+
assert.equal(list.length, 1);
|
|
310
|
+
assert.ok([list[0].memory_a, list[0].memory_b].includes(a.id), "pair holds both sides");
|
|
311
|
+
assert.ok([list[0].memory_a, list[0].memory_b].includes(b.id));
|
|
312
|
+
|
|
313
|
+
const resolved = store.resolveConflictPending(pending.id, { winner: a.id });
|
|
314
|
+
assert.ok(resolved.resolved_at, "resolution stamped");
|
|
315
|
+
assert.equal(resolved.resolved_winner, a.id);
|
|
316
|
+
assert.equal(store.listConflictPending().length, 0, "resolved excluded by default");
|
|
317
|
+
const all = store.listConflictPending({ includeResolved: true });
|
|
318
|
+
assert.equal(all.length, 1, "resolved visible with includeResolved");
|
|
319
|
+
assert.equal(all[0].resolved_winner, a.id);
|
|
320
|
+
store.close();
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
test("saveConflictPending dedupes the same pair regardless of order", () => {
|
|
324
|
+
const store = openMemory();
|
|
325
|
+
const a = store.save({ type: "decision", title: "截止", content: "x" });
|
|
326
|
+
const b = store.save({ type: "decision", title: "截止2", content: "y" });
|
|
327
|
+
const p1 = store.saveConflictPending({ memory_a: a.id, memory_b: b.id, reason: "r1" });
|
|
328
|
+
const p2 = store.saveConflictPending({ memory_a: b.id, memory_b: a.id, reason: "r2" });
|
|
329
|
+
assert.equal(p2.id, p1.id, "same pair re-detected returns the existing pending row");
|
|
330
|
+
assert.equal(p2.reason, "r1", "original reason preserved");
|
|
331
|
+
assert.equal(store.listConflictPending().length, 1, "never a duplicate queue entry");
|
|
332
|
+
store.close();
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
test("countConflictPending counts unresolved rows only; resolve unknown id is undefined", () => {
|
|
336
|
+
const store = openMemory();
|
|
337
|
+
const a = store.save({ type: "decision", title: "a", content: "x" });
|
|
338
|
+
const b = store.save({ type: "decision", title: "b", content: "y" });
|
|
339
|
+
const c = store.save({ type: "decision", title: "c", content: "z" });
|
|
340
|
+
store.saveConflictPending({ memory_a: a.id, memory_b: b.id, reason: "ab" });
|
|
341
|
+
const p2 = store.saveConflictPending({ memory_a: b.id, memory_b: c.id, reason: "bc" });
|
|
342
|
+
assert.equal(store.countConflictPending(), 2);
|
|
343
|
+
store.resolveConflictPending(p2.id, { winner: b.id });
|
|
344
|
+
assert.equal(store.countConflictPending(), 1, "resolved no longer pending");
|
|
345
|
+
assert.equal(store.resolveConflictPending("ghost"), undefined, "unknown id resolves to undefined");
|
|
346
|
+
store.close();
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
test("conflict_pending table persists across store reopen", () => {
|
|
350
|
+
const dir = mkdtempSync(join(tmpdir(), "dsh-mneme-conflict-"));
|
|
351
|
+
const path = join(dir, "memory.db");
|
|
352
|
+
const s1 = createStore(path);
|
|
353
|
+
s1.saveConflictPending({ run_id: "run-1", memory_a: "ma", memory_b: "mb", reason: "x" });
|
|
354
|
+
s1.close();
|
|
355
|
+
const s2 = createStore(path);
|
|
356
|
+
const pending = s2.listConflictPending();
|
|
357
|
+
assert.equal(pending.length, 1);
|
|
358
|
+
assert.equal(pending[0].reason, "x");
|
|
359
|
+
s2.close();
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
// --- session lifecycle (v0.6.0): disposed rows are filtered out ------------
|
|
363
|
+
|
|
364
|
+
test("session_disposed_at column exists and survives migration", () => {
|
|
365
|
+
const dir = mkdtempSync(join(tmpdir(), "dsh-mneme-disposed-"));
|
|
366
|
+
const path = join(dir, "memory.db");
|
|
367
|
+
const s1 = createStore(path);
|
|
368
|
+
const { id } = s1.save({ type: "decision", title: "迁移", content: "c", session_id: "sess-m" });
|
|
369
|
+
s1.setDisposedBySession("sess-m", true);
|
|
370
|
+
const row = s1.db.prepare("SELECT session_disposed_at FROM memories WHERE id = ?").get(id);
|
|
371
|
+
assert.ok(row.session_disposed_at, "dispose timestamp persisted");
|
|
372
|
+
s1.close();
|
|
373
|
+
// reopen: column survives
|
|
374
|
+
const s2 = createStore(path);
|
|
375
|
+
const live = s2.list({ type: "decision" });
|
|
376
|
+
assert.ok(!live.some((m) => m.id === id), "disposed row excluded after reopen");
|
|
377
|
+
s2.close();
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
test("search and list exclude disposed rows unless includeDisposed", () => {
|
|
381
|
+
const store = createStore(":memory:");
|
|
382
|
+
const a = store.save({ type: "decision", title: "活着", content: "keyword alpha", session_id: "s1" });
|
|
383
|
+
const b = store.save({ type: "decision", title: "被销毁", content: "keyword beta", session_id: "s2" });
|
|
384
|
+
store.setDisposedBySession("s2", true);
|
|
385
|
+
|
|
386
|
+
const searchRes = store.search("keyword");
|
|
387
|
+
assert.ok(searchRes.some((m) => m.id === a.id), "live row found");
|
|
388
|
+
assert.ok(!searchRes.some((m) => m.id === b.id), "disposed row excluded from search");
|
|
389
|
+
const searchIncl = store.search("keyword", { includeDisposed: true });
|
|
390
|
+
assert.ok(searchIncl.some((m) => m.id === b.id), "includeDisposed surfaces it");
|
|
391
|
+
|
|
392
|
+
const listRes = store.list({ type: "decision" });
|
|
393
|
+
assert.ok(listRes.some((m) => m.id === a.id));
|
|
394
|
+
assert.ok(!listRes.some((m) => m.id === b.id), "disposed excluded from list");
|
|
395
|
+
const listIncl = store.list({ type: "decision", includeDisposed: true });
|
|
396
|
+
assert.ok(listIncl.some((m) => m.id === b.id), "includeDisposed lists it");
|
|
397
|
+
assert.equal(store.count("decision", { includeDisposed: true }), 2);
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
test("searchVector excludes disposed rows", () => {
|
|
401
|
+
const store = createStore(":memory:");
|
|
402
|
+
const a = store.save({ type: "decision", title: "向量A", content: "v", session_id: "s1" });
|
|
403
|
+
const b = store.save({ type: "decision", title: "向量B", content: "v", session_id: "s2" });
|
|
404
|
+
store.setEmbedding(a.id, [1, 0, 0]);
|
|
405
|
+
store.setEmbedding(b.id, [1, 0, 0]);
|
|
406
|
+
store.setDisposedBySession("s2", true);
|
|
407
|
+
|
|
408
|
+
const res = store.searchVector([1, 0, 0], { limit: 10 });
|
|
409
|
+
assert.ok(res.some((m) => m.id === a.id));
|
|
410
|
+
assert.ok(!res.some((m) => m.id === b.id), "disposed row excluded from vector search");
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
test("setDisposedBySession round-trips and listBySession sees all states", () => {
|
|
414
|
+
const store = createStore(":memory:");
|
|
415
|
+
const a = store.save({ type: "decision", title: "会话记忆", content: "c", session_id: "sess-z" });
|
|
416
|
+
assert.equal(store.setDisposedBySession("sess-z", true), 1, "first dispose flips 1");
|
|
417
|
+
assert.equal(store.setDisposedBySession("sess-z", true), 0, "re-dispose is a no-op");
|
|
418
|
+
assert.equal(store.setDisposedBySession("sess-z", false), 1, "restore flips 1");
|
|
419
|
+
assert.equal(store.setDisposedBySession("sess-z", false), 0, "re-restore is a no-op");
|
|
420
|
+
assert.equal(store.getById(a.id).session_disposed_at, undefined, "mark cleared after restore");
|
|
421
|
+
|
|
422
|
+
const bySession = store.listBySession("sess-z");
|
|
423
|
+
assert.equal(bySession.length, 1);
|
|
424
|
+
assert.equal(bySession[0].id, a.id);
|
|
425
|
+
});
|
|
426
|
+
|
|
427
|
+
test("listBySession hides disposed by default, includeDisposed opts in", () => {
|
|
428
|
+
const store = createStore(":memory:");
|
|
429
|
+
const a = store.save({ type: "decision", title: "会话记忆", content: "c", session_id: "sess-y" });
|
|
430
|
+
store.setDisposedBySession("sess-y", true);
|
|
431
|
+
|
|
432
|
+
assert.equal(store.listBySession("sess-y").length, 0, "disposed row hidden by default");
|
|
433
|
+
const all = store.listBySession("sess-y", { includeDisposed: true });
|
|
434
|
+
assert.equal(all.length, 1, "includeDisposed reveals it");
|
|
435
|
+
assert.equal(all[0].id, a.id);
|
|
436
|
+
});
|