@modusensus/dsh-mneme 0.2.4 → 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.
- package/README.md +10 -9
- package/lib/config.js +5 -2
- package/lib/dream/decisions.js +174 -62
- package/lib/dream.js +30 -5
- package/lib/index.js +3 -1
- package/lib/mirror.js +7 -1
- package/lib/service.js +117 -3
- package/lib/store.js +40 -0
- package/lib/tools.js +47 -4
- package/package.json +3 -1
- package/scripts/benchmark-embed.js +201 -0
- package/scripts/benchmark-rerank.js +166 -0
- package/scripts/e2e-dsh.js +216 -0
- package/scripts/stress-dsh.js +255 -0
- package/scripts/sync-lib.js +47 -0
- package/src/config.js +5 -2
- package/src/dream/decisions.js +174 -62
- package/src/dream.js +30 -5
- package/src/index.js +3 -1
- package/src/mirror.js +7 -1
- package/src/service.js +117 -3
- package/src/store.js +40 -0
- package/src/tools.js +47 -4
- package/test/api.test.js +385 -0
- package/test/audit.test.js +290 -0
- package/test/client.test.js +26 -0
- package/test/clustering.test.js +100 -0
- package/test/commands.test.js +69 -0
- package/test/config.test.js +31 -0
- package/test/dream.test.js +526 -0
- package/test/helpers/dream-mock.js +82 -0
- package/test/inject.test.js +82 -0
- package/test/local-embedder.test.js +227 -0
- package/test/mirror.test.js +249 -0
- package/test/reflection.test.js +226 -0
- package/test/reranker.test.js +197 -0
- package/test/semantic.test.js +123 -0
- package/test/service-search.test.js +169 -0
- package/test/service.test.js +198 -0
- package/test/settings.test.js +101 -0
- package/test/store.test.js +293 -0
- package/test/stress.test.js +209 -0
- package/test/summarize.test.js +156 -0
- package/test/tools.test.js +265 -0
- package/test/vector-index.test.js +205 -0
package/test/api.test.js
ADDED
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { EventEmitter } from "node:events";
|
|
4
|
+
import { createStore } from "../src/store.js";
|
|
5
|
+
import { createService } from "../src/service.js";
|
|
6
|
+
import { createApi } from "../src/api.js";
|
|
7
|
+
import { createSettings } from "../src/settings.js";
|
|
8
|
+
|
|
9
|
+
class FakeRes extends EventEmitter {
|
|
10
|
+
constructor() { super(); this.statusCode = 200; this.body = ""; }
|
|
11
|
+
writeHead(code, headers) { this.statusCode = code; this.headers = headers; return this; }
|
|
12
|
+
end(text) { this.body = text ?? ""; this.emit("end"); return this; }
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function req(path, method = "GET", body = null) {
|
|
16
|
+
const r = new EventEmitter();
|
|
17
|
+
r.url = path;
|
|
18
|
+
r.method = method;
|
|
19
|
+
r.headers = {};
|
|
20
|
+
if (body !== null) {
|
|
21
|
+
process.nextTick(() => {
|
|
22
|
+
r.emit("data", Buffer.from(JSON.stringify(body)));
|
|
23
|
+
r.emit("end");
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
return r;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function setup(embedder, apiToken = "") {
|
|
30
|
+
const store = createStore(":memory:");
|
|
31
|
+
const service = createService({ store, mirror: null, config: {} });
|
|
32
|
+
const settings = createSettings(store.db);
|
|
33
|
+
const commands = {
|
|
34
|
+
add: (def) => settings.addCommand(def),
|
|
35
|
+
remove: (id) => settings.removeCommand(id),
|
|
36
|
+
list: () => settings.listCommands()
|
|
37
|
+
};
|
|
38
|
+
const routes = [];
|
|
39
|
+
const ctx = {
|
|
40
|
+
webServer: {
|
|
41
|
+
register(route) {
|
|
42
|
+
routes.push(route);
|
|
43
|
+
return () => {};
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
const api = createApi(ctx, service, settings, commands, embedder, undefined, apiToken);
|
|
48
|
+
return { store, service, routes, api, settings, apiToken };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function findHandler(routes, path) {
|
|
52
|
+
const route = routes.find((r) => r.path === path || (r.kind === "prefix" && path.startsWith(r.path)));
|
|
53
|
+
return route;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
test("registers list, search, and get prefix routes", () => {
|
|
57
|
+
const { routes } = setup();
|
|
58
|
+
const paths = routes.map((r) => r.path);
|
|
59
|
+
assert.ok(paths.includes("/api/dsh-mneme/list"));
|
|
60
|
+
assert.ok(paths.includes("/api/dsh-mneme/search"));
|
|
61
|
+
assert.ok(paths.includes("/api/dsh-mneme"));
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test("GET /api/dsh-mneme/list returns memories as JSON", async () => {
|
|
65
|
+
const { routes, service } = setup();
|
|
66
|
+
service.saveWithDedupe({ type: "preference", title: "语言", content: "中文" });
|
|
67
|
+
const route = routes.find((r) => r.path === "/api/dsh-mneme/list");
|
|
68
|
+
const res = new FakeRes();
|
|
69
|
+
await route.handler(req("/api/dsh-mneme/list?type=preference"), res);
|
|
70
|
+
assert.equal(res.statusCode, 200);
|
|
71
|
+
const data = JSON.parse(res.body);
|
|
72
|
+
assert.equal(data.items.length, 1);
|
|
73
|
+
assert.equal(data.items[0].title, "语言");
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test("GET /api/dsh-mneme/search?q= returns matches", async () => {
|
|
77
|
+
const { routes, service } = setup();
|
|
78
|
+
service.saveWithDedupe({ type: "project", title: "记忆插件", content: "SQLite 中文搜索" });
|
|
79
|
+
const route = routes.find((r) => r.path === "/api/dsh-mneme/search");
|
|
80
|
+
const res = new FakeRes();
|
|
81
|
+
await route.handler(req("/api/dsh-mneme/search?q=%E4%B8%AD%E6%96%87"), res);
|
|
82
|
+
assert.equal(res.statusCode, 200);
|
|
83
|
+
const data = JSON.parse(res.body);
|
|
84
|
+
assert.equal(data.items.length, 1);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test("unknown route under prefix returns 404 json", async () => {
|
|
88
|
+
const { routes } = setup();
|
|
89
|
+
const route = findHandler(routes, "/api/dsh-mneme");
|
|
90
|
+
const res = new FakeRes();
|
|
91
|
+
await route.handler(req("/api/dsh-mneme/nope"), res);
|
|
92
|
+
assert.equal(res.statusCode, 404);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test("list total excludes forgotten entries", async () => {
|
|
96
|
+
const { routes, service } = setup();
|
|
97
|
+
service.saveWithDedupe({ type: "preference", title: "正常", content: "可见" });
|
|
98
|
+
const forgotten = service.saveWithDedupe({ type: "preference", title: "遗忘", content: "隐藏" });
|
|
99
|
+
service.saveWithDedupe({ type: "project", title: "项目", content: "其他类型" });
|
|
100
|
+
service.setForget(forgotten.memory.id, true);
|
|
101
|
+
const route = routes.find((r) => r.path === "/api/dsh-mneme/list");
|
|
102
|
+
const res = new FakeRes();
|
|
103
|
+
await route.handler(req("/api/dsh-mneme/list?type=preference"), res);
|
|
104
|
+
assert.equal(res.statusCode, 200);
|
|
105
|
+
const data = JSON.parse(res.body);
|
|
106
|
+
assert.equal(data.items.length, 1);
|
|
107
|
+
assert.equal(data.total, 1, "total matches visible items, forgotten excluded");
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
test("list honors limit/offset", async () => {
|
|
111
|
+
const { routes, service } = setup();
|
|
112
|
+
service.saveWithDedupe({ type: "preference", title: "a", content: "1" });
|
|
113
|
+
service.saveWithDedupe({ type: "preference", title: "b", content: "2" });
|
|
114
|
+
const route = routes.find((r) => r.path === "/api/dsh-mneme/list");
|
|
115
|
+
const res = new FakeRes();
|
|
116
|
+
await route.handler(req("/api/dsh-mneme/list?limit=1&offset=0"), res);
|
|
117
|
+
const data = JSON.parse(res.body);
|
|
118
|
+
assert.equal(data.items.length, 1);
|
|
119
|
+
assert.equal(data.total, 2);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
test("responses carry application/json content-type", async () => {
|
|
123
|
+
const { routes } = setup();
|
|
124
|
+
const route = routes.find((r) => r.path === "/api/dsh-mneme/list");
|
|
125
|
+
const res = new FakeRes();
|
|
126
|
+
await route.handler(req("/api/dsh-mneme/list"), res);
|
|
127
|
+
assert.match(res.headers["Content-Type"], /application\/json/);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
test("handler errors return 500 json instead of leaking to host", async () => {
|
|
131
|
+
const routes = [];
|
|
132
|
+
const ctx = {
|
|
133
|
+
webServer: {
|
|
134
|
+
register(route) {
|
|
135
|
+
routes.push(route);
|
|
136
|
+
return () => {};
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
const service = {
|
|
141
|
+
list() { throw new Error("boom"); },
|
|
142
|
+
count() { throw new Error("boom"); },
|
|
143
|
+
search() { throw new Error("boom"); },
|
|
144
|
+
toApiList() { return []; }
|
|
145
|
+
};
|
|
146
|
+
createApi(ctx, service, {}, { add: () => { throw new Error("x"); }, remove: () => false, list: () => [] });
|
|
147
|
+
const route = routes.find((r) => r.path === "/api/dsh-mneme/list");
|
|
148
|
+
const res = new FakeRes();
|
|
149
|
+
await route.handler(req("/api/dsh-mneme/list"), res);
|
|
150
|
+
assert.equal(res.statusCode, 500);
|
|
151
|
+
assert.deepEqual(JSON.parse(res.body), { error: "internal" });
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
test("GET /api/dsh-mneme/profile returns stored profile", async () => {
|
|
155
|
+
const { routes, settings } = setup();
|
|
156
|
+
settings.setProfile("我是前端");
|
|
157
|
+
const route = routes.find((r) => r.path === "/api/dsh-mneme/profile");
|
|
158
|
+
const res = new FakeRes();
|
|
159
|
+
await route.handler(req("/api/dsh-mneme/profile"), res);
|
|
160
|
+
assert.equal(res.statusCode, 200);
|
|
161
|
+
assert.equal(JSON.parse(res.body).profile, "我是前端");
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
test("PUT /api/dsh-mneme/profile saves profile", async () => {
|
|
165
|
+
const { routes, settings } = setup();
|
|
166
|
+
const route = routes.find((r) => r.path === "/api/dsh-mneme/profile");
|
|
167
|
+
const res = new FakeRes();
|
|
168
|
+
await route.handler(req("/api/dsh-mneme/profile", "PUT", { profile: "新画像" }), res);
|
|
169
|
+
assert.equal(res.statusCode, 200);
|
|
170
|
+
assert.equal(JSON.parse(res.body).profile, "新画像");
|
|
171
|
+
assert.equal(settings.getProfile(), "新画像");
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
test("GET /api/dsh-mneme/rules returns stored rules", async () => {
|
|
175
|
+
const { routes, settings } = setup();
|
|
176
|
+
settings.setRules(["规则1", "规则2"]);
|
|
177
|
+
const route = routes.find((r) => r.path === "/api/dsh-mneme/rules");
|
|
178
|
+
const res = new FakeRes();
|
|
179
|
+
await route.handler(req("/api/dsh-mneme/rules"), res);
|
|
180
|
+
assert.equal(res.statusCode, 200);
|
|
181
|
+
assert.deepEqual(JSON.parse(res.body).rules, ["规则1", "规则2"]);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
test("PUT /api/dsh-mneme/rules saves rules", async () => {
|
|
185
|
+
const { routes, settings } = setup();
|
|
186
|
+
const route = routes.find((r) => r.path === "/api/dsh-mneme/rules");
|
|
187
|
+
const res = new FakeRes();
|
|
188
|
+
await route.handler(req("/api/dsh-mneme/rules", "PUT", { rules: ["a", "b"] }), res);
|
|
189
|
+
assert.equal(res.statusCode, 200);
|
|
190
|
+
assert.deepEqual(settings.getRules(), ["a", "b"]);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
test("GET /api/dsh-mneme/commands lists commands", async () => {
|
|
194
|
+
const { routes, settings } = setup();
|
|
195
|
+
settings.addCommand({ name: "agenda", instruction: "x" });
|
|
196
|
+
const route = routes.find((r) => r.path === "/api/dsh-mneme/commands");
|
|
197
|
+
const res = new FakeRes();
|
|
198
|
+
await route.handler(req("/api/dsh-mneme/commands"), res);
|
|
199
|
+
assert.equal(res.statusCode, 200);
|
|
200
|
+
assert.equal(JSON.parse(res.body).commands.length, 1);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
test("POST /api/dsh-mneme/commands adds a command; DELETE removes", async () => {
|
|
204
|
+
const { routes } = setup();
|
|
205
|
+
const route = routes.find((r) => r.path === "/api/dsh-mneme/commands");
|
|
206
|
+
const res = new FakeRes();
|
|
207
|
+
await route.handler(req("/api/dsh-mneme/commands", "POST", { name: "fmt", description: "d", instruction: "格式化" }), res);
|
|
208
|
+
assert.equal(res.statusCode, 200);
|
|
209
|
+
const { command } = JSON.parse(res.body);
|
|
210
|
+
assert.equal(command.name, "fmt");
|
|
211
|
+
const del = new FakeRes();
|
|
212
|
+
await route.handler(req(`/api/dsh-mneme/commands?id=${command.id}`, "DELETE"), del);
|
|
213
|
+
assert.equal(JSON.parse(del.body).removed, true);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
test("POST /api/dsh-mneme/commands rejects invalid name with 400", async () => {
|
|
217
|
+
const { routes } = setup();
|
|
218
|
+
const route = routes.find((r) => r.path === "/api/dsh-mneme/commands");
|
|
219
|
+
const res = new FakeRes();
|
|
220
|
+
await route.handler(req("/api/dsh-mneme/commands", "POST", { name: "Bad Name", instruction: "x" }), res);
|
|
221
|
+
assert.equal(res.statusCode, 400);
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
// --- vector search routes ---
|
|
225
|
+
|
|
226
|
+
function setupWithEmbedder(embedder) {
|
|
227
|
+
const base = setup(embedder);
|
|
228
|
+
base.settings.setVectorConfig({
|
|
229
|
+
enabled: true,
|
|
230
|
+
baseUrl: "https://api.example.com/v1",
|
|
231
|
+
apiKey: "sk-test",
|
|
232
|
+
model: "text-embedding-v3"
|
|
233
|
+
});
|
|
234
|
+
return base;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
test("vector-config defaults and round-trips through PUT/GET", async () => {
|
|
238
|
+
const { routes, settings } = setup();
|
|
239
|
+
const route = routes.find((r) => r.path === "/api/dsh-mneme/vector-config");
|
|
240
|
+
|
|
241
|
+
const get1 = new FakeRes();
|
|
242
|
+
await route.handler(req("/api/dsh-mneme/vector-config"), get1);
|
|
243
|
+
assert.equal(JSON.parse(get1.body).config.enabled, false);
|
|
244
|
+
|
|
245
|
+
const put = new FakeRes();
|
|
246
|
+
await route.handler(req("/api/dsh-mneme/vector-config", "PUT", { enabled: true, baseUrl: "https://api.openai.com/v1", apiKey: "sk-x", model: "text-embedding-3-small" }), put);
|
|
247
|
+
assert.equal(JSON.parse(put.body).config.model, "text-embedding-3-small");
|
|
248
|
+
assert.equal(settings.getVectorConfig().enabled, true);
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
test("search mode=vector merges vector results when embedder returns a vector", async () => {
|
|
252
|
+
const embedder = {
|
|
253
|
+
embed: async () => [1, 0, 0],
|
|
254
|
+
reindexMissing: async () => ({ indexed: 0, skipped: 0 })
|
|
255
|
+
};
|
|
256
|
+
const { routes, store, service } = setupWithEmbedder(embedder);
|
|
257
|
+
const v = service.saveWithDedupe({ type: "preference", title: "猫", content: "喜欢猫" });
|
|
258
|
+
store.setEmbedding(v.memory.id, [1, 0, 0]);
|
|
259
|
+
service.saveWithDedupe({ type: "preference", title: "狗", content: "喜欢狗" });
|
|
260
|
+
|
|
261
|
+
const route = routes.find((r) => r.path === "/api/dsh-mneme/search");
|
|
262
|
+
const res = new FakeRes();
|
|
263
|
+
await route.handler(req("/api/dsh-mneme/search?q=%E7%8C%AB&mode=vector"), res);
|
|
264
|
+
const data = JSON.parse(res.body);
|
|
265
|
+
assert.equal(data.mode, "vector");
|
|
266
|
+
assert.equal(data.items.length, 1, "keyword hit + vector fill merged");
|
|
267
|
+
assert.equal(data.items[0].title, "猫");
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
test("search falls back to keyword when embedder disabled or unavailable", async () => {
|
|
271
|
+
// embedder that resolves null (disabled provider)
|
|
272
|
+
const embedder = { embed: async () => null, reindexMissing: async () => ({ indexed: 0, skipped: 0 }) };
|
|
273
|
+
const { routes, service } = setupWithEmbedder(embedder);
|
|
274
|
+
service.saveWithDedupe({ type: "preference", title: "语言", content: "中文交流" });
|
|
275
|
+
const route = routes.find((r) => r.path === "/api/dsh-mneme/search");
|
|
276
|
+
const res = new FakeRes();
|
|
277
|
+
await route.handler(req("/api/dsh-mneme/search?q=%E4%B8%AD%E6%96%87"), res);
|
|
278
|
+
const data = JSON.parse(res.body);
|
|
279
|
+
assert.equal(data.mode, "keyword");
|
|
280
|
+
assert.equal(data.items.length, 1);
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
test("vector-reindex calls embedder and returns counts", async () => {
|
|
284
|
+
const embedder = { reindexMissing: async () => ({ indexed: 2, skipped: 1 }) };
|
|
285
|
+
const { routes } = setupWithEmbedder(embedder);
|
|
286
|
+
const route = routes.find((r) => r.path === "/api/dsh-mneme/vector-reindex");
|
|
287
|
+
const res = new FakeRes();
|
|
288
|
+
await route.handler(req("/api/dsh-mneme/vector-reindex"), res);
|
|
289
|
+
const data = JSON.parse(res.body);
|
|
290
|
+
assert.equal(data.indexed, 2);
|
|
291
|
+
assert.equal(data.skipped, 1);
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
test("vector-config masks apiKey; empty/masked key on PUT keeps existing", async () => {
|
|
295
|
+
const { routes, settings } = setup();
|
|
296
|
+
const route = routes.find((r) => r.path === "/api/dsh-mneme/vector-config");
|
|
297
|
+
|
|
298
|
+
// PUT stores the real key but responds masked
|
|
299
|
+
const put = new FakeRes();
|
|
300
|
+
await route.handler(req("/api/dsh-mneme/vector-config", "PUT", {
|
|
301
|
+
enabled: true, baseUrl: "https://api.openai.com/v1", apiKey: "sk-abcdefghijklmnop", model: "m1"
|
|
302
|
+
}), put);
|
|
303
|
+
const putBody = JSON.parse(put.body);
|
|
304
|
+
assert.equal(putBody.config.apiKey, "sk-***mnop", "PUT response masked");
|
|
305
|
+
assert.equal(settings.getVectorConfig().apiKey, "sk-abcdefghijklmnop", "storage keeps the real key");
|
|
306
|
+
|
|
307
|
+
// GET returns masked key, other fields intact
|
|
308
|
+
const get = new FakeRes();
|
|
309
|
+
await route.handler(req("/api/dsh-mneme/vector-config"), get);
|
|
310
|
+
const getBody = JSON.parse(get.body);
|
|
311
|
+
assert.equal(getBody.config.apiKey, "sk-***mnop");
|
|
312
|
+
assert.equal(getBody.config.baseUrl, "https://api.openai.com/v1");
|
|
313
|
+
assert.equal(getBody.config.enabled, true);
|
|
314
|
+
|
|
315
|
+
// PUT with empty apiKey keeps the previous key
|
|
316
|
+
const put2 = new FakeRes();
|
|
317
|
+
await route.handler(req("/api/dsh-mneme/vector-config", "PUT", {
|
|
318
|
+
enabled: true, baseUrl: "https://api.openai.com/v1", apiKey: "", model: "m2"
|
|
319
|
+
}), put2);
|
|
320
|
+
assert.equal(settings.getVectorConfig().apiKey, "sk-abcdefghijklmnop", "empty key keeps existing");
|
|
321
|
+
assert.equal(JSON.parse(put2.body).config.model, "m2");
|
|
322
|
+
|
|
323
|
+
// PUT with a masked apiKey (client round-trip) also keeps the previous key
|
|
324
|
+
const put3 = new FakeRes();
|
|
325
|
+
await route.handler(req("/api/dsh-mneme/vector-config", "PUT", {
|
|
326
|
+
enabled: true, baseUrl: "https://api.openai.com/v1", apiKey: "sk-***mnop", model: "m3"
|
|
327
|
+
}), put3);
|
|
328
|
+
assert.equal(settings.getVectorConfig().apiKey, "sk-abcdefghijklmnop", "masked key keeps existing");
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
test("apiToken protects write/secret endpoints while read endpoints stay open", async () => {
|
|
332
|
+
const { routes } = setup(undefined, "secret-token");
|
|
333
|
+
const list = routes.find((r) => r.path === "/api/dsh-mneme/list");
|
|
334
|
+
const profile = routes.find((r) => r.path === "/api/dsh-mneme/profile");
|
|
335
|
+
const vec = routes.find((r) => r.path === "/api/dsh-mneme/vector-config");
|
|
336
|
+
const reindex = routes.find((r) => r.path === "/api/dsh-mneme/vector-reindex");
|
|
337
|
+
|
|
338
|
+
// read-only endpoint stays open without a token
|
|
339
|
+
const resList = new FakeRes();
|
|
340
|
+
await list.handler(req("/api/dsh-mneme/list"), resList);
|
|
341
|
+
assert.equal(resList.statusCode, 200, "list stays open");
|
|
342
|
+
|
|
343
|
+
// secret endpoint without token → 401
|
|
344
|
+
const resVec = new FakeRes();
|
|
345
|
+
await vec.handler(req("/api/dsh-mneme/vector-config"), resVec);
|
|
346
|
+
assert.equal(resVec.statusCode, 401, "vector-config GET requires token");
|
|
347
|
+
|
|
348
|
+
// write endpoint without token → 401
|
|
349
|
+
const resProfile = new FakeRes();
|
|
350
|
+
await profile.handler(req("/api/dsh-mneme/profile", "PUT", { profile: "x" }), resProfile);
|
|
351
|
+
assert.equal(resProfile.statusCode, 401, "profile PUT requires token");
|
|
352
|
+
|
|
353
|
+
// reindex without token → 401
|
|
354
|
+
const resReindex = new FakeRes();
|
|
355
|
+
await reindex.handler(req("/api/dsh-mneme/vector-reindex"), resReindex);
|
|
356
|
+
assert.equal(resReindex.statusCode, 401, "vector-reindex requires token");
|
|
357
|
+
|
|
358
|
+
// with a Bearer token everything is allowed
|
|
359
|
+
const authReq = (path, method = "GET", body = null) => {
|
|
360
|
+
const r = req(path, method, body);
|
|
361
|
+
r.headers = { authorization: "Bearer secret-token" };
|
|
362
|
+
return r;
|
|
363
|
+
};
|
|
364
|
+
const resVecOk = new FakeRes();
|
|
365
|
+
await vec.handler(authReq("/api/dsh-mneme/vector-config"), resVecOk);
|
|
366
|
+
assert.equal(resVecOk.statusCode, 200, "vector-config GET with token");
|
|
367
|
+
const resProfileOk = new FakeRes();
|
|
368
|
+
await profile.handler(authReq("/api/dsh-mneme/profile", "PUT", { profile: "hi" }), resProfileOk);
|
|
369
|
+
assert.equal(resProfileOk.statusCode, 200, "profile PUT with token");
|
|
370
|
+
|
|
371
|
+
// wrong token → 401
|
|
372
|
+
const bad = req("/api/dsh-mneme/vector-config");
|
|
373
|
+
bad.headers = { authorization: "Bearer wrong" };
|
|
374
|
+
const resBad = new FakeRes();
|
|
375
|
+
await vec.handler(bad, resBad);
|
|
376
|
+
assert.equal(resBad.statusCode, 401, "wrong token rejected");
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
test("no apiToken configured keeps all endpoints open", async () => {
|
|
380
|
+
const { routes } = setup();
|
|
381
|
+
const vec = routes.find((r) => r.path === "/api/dsh-mneme/vector-config");
|
|
382
|
+
const res = new FakeRes();
|
|
383
|
+
await vec.handler(req("/api/dsh-mneme/vector-config"), res);
|
|
384
|
+
assert.equal(res.statusCode, 200, "open when apiToken is unset");
|
|
385
|
+
});
|
|
@@ -0,0 +1,290 @@
|
|
|
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 {
|
|
7
|
+
createDreamScheduler,
|
|
8
|
+
hashSnapshot,
|
|
9
|
+
buildReceipt,
|
|
10
|
+
parseReceipt,
|
|
11
|
+
buildOutcome
|
|
12
|
+
} from "../src/dream.js";
|
|
13
|
+
import { applyDecisions } from "../src/dream/decisions.js";
|
|
14
|
+
import { createStore } from "../src/store.js";
|
|
15
|
+
import { createService } from "../src/service.js";
|
|
16
|
+
|
|
17
|
+
// ---------------------------------------------------------------- helpers
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Minimal DSH-like ctx whose LLM distinguishes the two dream calls by the user
|
|
21
|
+
* prompt shape: consolidation prompts start with "id=…", summary prompts with
|
|
22
|
+
* "- title: content". `onConsolidation` receives the raw list text and returns
|
|
23
|
+
* a decisions JSON string; `summaryText` (or a throw) drives the summary call.
|
|
24
|
+
*/
|
|
25
|
+
function mockCtx({ onConsolidation, summaryText = "记忆库总览:用户偏好中文。" } = {}) {
|
|
26
|
+
return {
|
|
27
|
+
logger: { warn: () => {} },
|
|
28
|
+
agentDefaultModel: { currentSelection: () => ({ provider: "mock", model: "mock-model" }) },
|
|
29
|
+
llm: {
|
|
30
|
+
async *stream(options) {
|
|
31
|
+
const userText = options.messages.find((m) => m.role === "user")?.content?.[0]?.text ?? "";
|
|
32
|
+
if (userText.startsWith("id=")) {
|
|
33
|
+
yield { type: "block-start", index: 0, blockType: "text" };
|
|
34
|
+
yield { type: "text-delta", index: 0, text: onConsolidation ? onConsolidation(userText) : "[]" };
|
|
35
|
+
yield { type: "block-end", index: 0, block: { type: "text" } };
|
|
36
|
+
yield { type: "finish", reason: { kind: "stop" } };
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (typeof summaryText === "string") {
|
|
40
|
+
yield { type: "text-delta", index: 0, text: summaryText };
|
|
41
|
+
yield { type: "finish", reason: { kind: "stop" } };
|
|
42
|
+
} else {
|
|
43
|
+
throw summaryText instanceof Error ? summaryText : new Error(String(summaryText));
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function setup() {
|
|
51
|
+
const store = createStore(":memory:");
|
|
52
|
+
const service = createService({ store, mirror: null, config: {} });
|
|
53
|
+
const dream = createDreamScheduler({ onRun: () => Promise.resolve({ ok: true, skipped: true }) });
|
|
54
|
+
return { store, service, dream };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// ---------------------------------------------------------------- audit rows
|
|
58
|
+
|
|
59
|
+
test("successful runDream writes an audit row with receipt, decisions and outcome", async () => {
|
|
60
|
+
const { store, service, dream } = setup();
|
|
61
|
+
const { memory: a } = service.saveWithDedupe({ type: "project", title: "插件", content: "旧", importance: 3 });
|
|
62
|
+
const { memory: b } = service.saveWithDedupe({ type: "project", title: "插件2", content: "新细节", importance: 4 });
|
|
63
|
+
const ctx = mockCtx({
|
|
64
|
+
onConsolidation: () => JSON.stringify([
|
|
65
|
+
{ action: "merge", ids: [a.id, b.id], keepSource: b.id, title: "插件总览", content: "合并内容", importance: 4 }
|
|
66
|
+
])
|
|
67
|
+
});
|
|
68
|
+
const result = await dream.runDream(ctx, service, {});
|
|
69
|
+
assert.equal(result.ok, true);
|
|
70
|
+
assert.equal(result.applied, 1);
|
|
71
|
+
|
|
72
|
+
const runs = store.listDreamRuns();
|
|
73
|
+
assert.equal(runs.length, 1);
|
|
74
|
+
const run = runs[0];
|
|
75
|
+
assert.equal(run.status, "ok");
|
|
76
|
+
assert.equal(run.input_count, 2);
|
|
77
|
+
assert.equal(run.applied, 1);
|
|
78
|
+
assert.equal(run.summary_stored, true);
|
|
79
|
+
assert.equal(run.provider, "mock");
|
|
80
|
+
assert.match(run.receipt, /^dsh-mneme:run:/);
|
|
81
|
+
|
|
82
|
+
// receipt round-trips and correlates with the audit row
|
|
83
|
+
const parsed = parseReceipt(run.receipt);
|
|
84
|
+
assert.deepEqual(parsed, {
|
|
85
|
+
runId: run.id,
|
|
86
|
+
status: "ok",
|
|
87
|
+
snapshotHash: run.snapshot_hash.slice(0, 12),
|
|
88
|
+
inputCount: 2,
|
|
89
|
+
applied: 1,
|
|
90
|
+
summaryStored: true
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
// per-id outcome is derived correctly
|
|
94
|
+
assert.equal(run.outcome.byId[a.id], "merge-archived");
|
|
95
|
+
assert.equal(run.outcome.byId[b.id], "merge-keep");
|
|
96
|
+
// raw decisions are stored for replay
|
|
97
|
+
assert.equal(run.decisions.length, 1);
|
|
98
|
+
assert.equal(run.decisions[0].action, "merge");
|
|
99
|
+
// full input snapshot is persisted and its digest matches — the audit row
|
|
100
|
+
// alone can rebuild the exact arbitration input offline
|
|
101
|
+
assert.equal(run.input.length, 2);
|
|
102
|
+
assert.deepEqual(run.input.map((m) => m.title).sort(), ["插件", "插件2"]);
|
|
103
|
+
assert.equal(hashSnapshot(run.input), run.snapshot_hash, "input snapshot digest matches stored snapshot_hash");
|
|
104
|
+
store.close();
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
test("failed runDream records status failed with the error", async () => {
|
|
108
|
+
const { store, service, dream } = setup();
|
|
109
|
+
service.saveWithDedupe({ type: "preference", title: "语言", content: "中文" });
|
|
110
|
+
const ctx = mockCtx({ onConsolidation: () => "抱歉,我无法处理这个任务" });
|
|
111
|
+
const result = await dream.runDream(ctx, service, {});
|
|
112
|
+
assert.equal(result.ok, false);
|
|
113
|
+
assert.match(result.error, /no json array/);
|
|
114
|
+
|
|
115
|
+
const run = store.listDreamRuns()[0];
|
|
116
|
+
assert.equal(run.status, "failed");
|
|
117
|
+
assert.equal(run.decisions, undefined);
|
|
118
|
+
assert.match(run.error, /no json array/);
|
|
119
|
+
const parsed = parseReceipt(run.receipt);
|
|
120
|
+
assert.equal(parsed.status, "failed");
|
|
121
|
+
store.close();
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
test("summary failure still records the applied decisions for replay", async () => {
|
|
125
|
+
const { store, service, dream } = setup();
|
|
126
|
+
const { memory: m } = service.saveWithDedupe({ type: "preference", title: "语言", content: "中文" });
|
|
127
|
+
const ctx = mockCtx({
|
|
128
|
+
onConsolidation: () => JSON.stringify([{ action: "keep", ids: [m.id] }]),
|
|
129
|
+
summaryText: new Error("summary boom")
|
|
130
|
+
});
|
|
131
|
+
const result = await dream.runDream(ctx, service, {});
|
|
132
|
+
assert.equal(result.ok, false);
|
|
133
|
+
assert.equal(result.error, "llm failed");
|
|
134
|
+
|
|
135
|
+
const run = store.listDreamRuns()[0];
|
|
136
|
+
assert.equal(run.status, "failed");
|
|
137
|
+
assert.equal(run.decisions.length, 1, "decisions captured despite summary failure");
|
|
138
|
+
assert.equal(run.outcome.byId[m.id], "keep");
|
|
139
|
+
store.close();
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
test("audit rows persist across store reopen (file-backed)", () => {
|
|
143
|
+
const dir = mkdtempSync(join(tmpdir(), "dsh-mneme-audit-"));
|
|
144
|
+
const path = join(dir, "memory.db");
|
|
145
|
+
const s1 = createStore(path);
|
|
146
|
+
s1.saveDreamRun({
|
|
147
|
+
id: "run-1", status: "ok", snapshot_hash: "abc", input_count: 1,
|
|
148
|
+
decisions: [{ action: "keep", ids: ["x"] }],
|
|
149
|
+
outcome: { byId: { x: "keep" } }, applied: 0, summary_stored: false,
|
|
150
|
+
receipt: "dsh-mneme:run:run-1:ok:abc:1:0:0"
|
|
151
|
+
});
|
|
152
|
+
s1.close();
|
|
153
|
+
const s2 = createStore(path);
|
|
154
|
+
const run = s2.getDreamRun("run-1");
|
|
155
|
+
assert.equal(run.status, "ok");
|
|
156
|
+
assert.equal(run.decisions[0].action, "keep");
|
|
157
|
+
s2.close();
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
// ---------------------------------------------------------------- hashing & receipts
|
|
161
|
+
|
|
162
|
+
test("hashSnapshot is deterministic and order-independent", () => {
|
|
163
|
+
const mk = (id, title, content = "c") => ({ id, type: "project", title, content, importance: 3, updated_at: "2026-01-01T00:00:00.000Z" });
|
|
164
|
+
const a = mk("a", "t1");
|
|
165
|
+
const b = mk("b", "t2");
|
|
166
|
+
assert.equal(hashSnapshot([a, b]), hashSnapshot([b, a]), "order independent");
|
|
167
|
+
assert.equal(hashSnapshot([a, b]), hashSnapshot([{ ...a }, { ...b }]), "field order / clone independent");
|
|
168
|
+
assert.notEqual(hashSnapshot([a, b]), hashSnapshot([a, { ...b, content: "changed" }]), "content change flips hash");
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
test("receipt round-trips and malformed receipts parse to undefined", () => {
|
|
172
|
+
const receipt = buildReceipt({ runId: "r1", status: "ok", snapshotHash: "0123456789abcdef", inputCount: 3, applied: 2, summaryStored: true });
|
|
173
|
+
assert.deepEqual(parseReceipt(receipt), { runId: "r1", status: "ok", snapshotHash: "0123456789ab", inputCount: 3, applied: 2, summaryStored: true });
|
|
174
|
+
for (const bad of ["nope", "", "dsh-mneme:run:r1", "dsh-mneme:run:r1:weird:abc:1:0:0", "dsh-mneme:run:r1:ok:abc:nan:0:0"]) {
|
|
175
|
+
assert.equal(parseReceipt(bad), undefined, `rejects ${JSON.stringify(bad)}`);
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
test("buildOutcome maps every decision action to per-id disposition", () => {
|
|
180
|
+
const outcome = buildOutcome([
|
|
181
|
+
{ action: "keep", ids: ["k"] },
|
|
182
|
+
{ action: "archive", ids: ["a1", "a2"] },
|
|
183
|
+
{ action: "merge", ids: ["m1", "m2"], keepSource: "m1" },
|
|
184
|
+
{ action: "conflict", winner: "w", loser: "l" }
|
|
185
|
+
]);
|
|
186
|
+
assert.equal(outcome.byId.k, "keep");
|
|
187
|
+
assert.equal(outcome.byId.a1, "archived");
|
|
188
|
+
assert.equal(outcome.byId.a2, "archived");
|
|
189
|
+
assert.equal(outcome.byId.m1, "merge-keep");
|
|
190
|
+
assert.equal(outcome.byId.m2, "merge-archived");
|
|
191
|
+
assert.equal(outcome.byId.w, "conflict-winner");
|
|
192
|
+
assert.equal(outcome.byId.l, "conflict-archived");
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
// ---------------------------------------------------------------- replayability
|
|
196
|
+
|
|
197
|
+
test("replaying a recorded decision list is idempotent", async () => {
|
|
198
|
+
const { store, service, dream } = setup();
|
|
199
|
+
const { memory: a } = service.saveWithDedupe({ type: "decision", title: "截止", content: "8月15日", importance: 4 });
|
|
200
|
+
const { memory: b } = service.saveWithDedupe({ type: "decision", title: "截止新", content: "8月20日", importance: 5 });
|
|
201
|
+
const ctx = mockCtx({
|
|
202
|
+
onConsolidation: () => JSON.stringify([{ action: "conflict", winner: b.id, loser: a.id, reason: "更新" }])
|
|
203
|
+
});
|
|
204
|
+
const result = await dream.runDream(ctx, service, {});
|
|
205
|
+
assert.equal(result.ok, true);
|
|
206
|
+
|
|
207
|
+
const run = store.getDreamRun(result.runId);
|
|
208
|
+
assert.equal(run.decisions[0].winner, b.id);
|
|
209
|
+
assert.equal(run.outcome.byId[a.id], "conflict-archived");
|
|
210
|
+
assert.equal(run.outcome.byId[b.id], "conflict-winner");
|
|
211
|
+
|
|
212
|
+
// store already reflects the run
|
|
213
|
+
assert.equal(store.getById(a.id).archived, true);
|
|
214
|
+
assert.ok(store.getById(b.id).content.includes("已否决旧信息"), "provenance note appended");
|
|
215
|
+
|
|
216
|
+
// replay the exact recorded decision list → no-op (idempotent)
|
|
217
|
+
const replayed = applyDecisions(run.decisions, service);
|
|
218
|
+
assert.equal(replayed.applied, 0, "already-applied decisions re-apply nothing");
|
|
219
|
+
assert.equal(store.getById(a.id).archived, true);
|
|
220
|
+
assert.equal(store.getById(b.id).content, store.getById(b.id).content);
|
|
221
|
+
store.close();
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
test("re-applying a decision many times has no cumulative side effects", async () => {
|
|
225
|
+
const { store, service, dream } = setup();
|
|
226
|
+
const { memory: a } = service.saveWithDedupe({ type: "decision", title: "截止", content: "8月15日", importance: 4 });
|
|
227
|
+
const { memory: b } = service.saveWithDedupe({ type: "decision", title: "截止新", content: "8月20日", importance: 5 });
|
|
228
|
+
const { memory: m1 } = service.saveWithDedupe({ type: "project", title: "甲", content: "旧甲", importance: 3 });
|
|
229
|
+
const { memory: m2 } = service.saveWithDedupe({ type: "project", title: "乙", content: "旧乙", importance: 4 });
|
|
230
|
+
const decisions = [
|
|
231
|
+
{ action: "conflict", winner: b.id, loser: a.id, reason: "更新" },
|
|
232
|
+
{ action: "merge", ids: [m1.id, m2.id], keepSource: m2.id, title: "甲乙", content: "合并", importance: 4 }
|
|
233
|
+
];
|
|
234
|
+
// apply 3 times (e.g. concurrent/replayed dream runs)
|
|
235
|
+
for (let i = 0; i < 3; i++) applyDecisions(decisions, service);
|
|
236
|
+
|
|
237
|
+
const winner = store.getById(b.id);
|
|
238
|
+
assert.equal(winner.content.split("已否决旧信息").length - 1, 1, "provenance note appended exactly once");
|
|
239
|
+
assert.equal(store.getById(a.id).archived, true);
|
|
240
|
+
assert.equal(store.getById(m2.id).title, "甲乙", "keeper merged");
|
|
241
|
+
assert.equal(store.getById(m1.id).archived, true, "merge source archived once");
|
|
242
|
+
store.close();
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
// ---------------------------------------------------------------- reconcile (item ②)
|
|
246
|
+
|
|
247
|
+
test("runDream records status reconcile, not a fake ok, when a target changed during the run", async () => {
|
|
248
|
+
const { store, service, dream } = setup();
|
|
249
|
+
const { memory: a } = service.saveWithDedupe({ type: "project", title: "旧", content: "A", importance: 3 });
|
|
250
|
+
const { memory: b } = service.saveWithDedupe({ type: "project", title: "新", content: "B", importance: 4 });
|
|
251
|
+
// The "LLM call" mutates a target while producing the decision list —
|
|
252
|
+
// simulating a concurrent human/agent edit inside the run window.
|
|
253
|
+
const ctx = mockCtx({
|
|
254
|
+
onConsolidation: () => {
|
|
255
|
+
service.update(b.id, { content: "并发编辑" });
|
|
256
|
+
return JSON.stringify([
|
|
257
|
+
{ action: "merge", ids: [a.id, b.id], title: "合并", content: "合并内容", importance: 4, keepSource: b.id }
|
|
258
|
+
]);
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
const result = await dream.runDream(ctx, service, {});
|
|
262
|
+
assert.equal(result.ok, false, "partial commit is not ok");
|
|
263
|
+
assert.equal(result.status, "reconcile", "explicit reconcile status");
|
|
264
|
+
assert.equal(result.applied, 0, "conflicting merge not applied");
|
|
265
|
+
|
|
266
|
+
const run = store.listDreamRuns()[0];
|
|
267
|
+
assert.equal(run.status, "reconcile", "audit row records reconcile");
|
|
268
|
+
assert.equal(run.applied, 0);
|
|
269
|
+
const parsed = parseReceipt(run.receipt);
|
|
270
|
+
assert.equal(parsed.status, "reconcile", "receipt records reconcile, never ok");
|
|
271
|
+
assert.equal(parsed.applied, 0);
|
|
272
|
+
// outcome is derived from what actually committed: the merge is NOT claimed
|
|
273
|
+
assert.equal(run.outcome.byId[b.id], undefined, "keeper not claimed as merge-keep");
|
|
274
|
+
assert.equal(run.outcome.byId[a.id], undefined, "source not claimed as merge-archived");
|
|
275
|
+
assert.equal(run.outcome.conflicts.length, 1, "conflict recorded in the audit row");
|
|
276
|
+
// the concurrent edit survived
|
|
277
|
+
assert.equal(store.getById(b.id).content, "并发编辑");
|
|
278
|
+
store.close();
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
test("buildOutcome over actually-committed sub-steps never claims a rolled-back merge", () => {
|
|
282
|
+
const committed = [
|
|
283
|
+
{ action: "keep", ids: ["k"] },
|
|
284
|
+
{ action: "merge", ids: ["m1", "m2"], keepSource: "m1", title: "t", content: "c" }
|
|
285
|
+
];
|
|
286
|
+
const outcome = buildOutcome(committed);
|
|
287
|
+
assert.equal(outcome.byId.m1, "merge-keep");
|
|
288
|
+
assert.equal(outcome.byId.m2, "merge-archived");
|
|
289
|
+
assert.equal(outcome.byId.k, "keep");
|
|
290
|
+
});
|