@modusensus/dsh-mneme 0.8.6 → 0.8.7
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 +18 -148
- package/bin/cli.mjs +689 -603
- package/lib/api-standalone.js +57 -1
- package/lib/api.js +34 -11
- package/lib/client.js +289 -59
- package/lib/config.js +54 -6
- package/lib/content-hash.js +54 -0
- package/lib/document-index.js +94 -0
- package/lib/document.js +93 -11
- package/lib/index.js +50 -11
- package/lib/inject.js +6 -2
- package/lib/lang.js +15 -4
- package/lib/maintenance.js +142 -0
- package/lib/mirror.js +147 -15
- package/lib/recall-stats.js +52 -2
- package/lib/scope.js +34 -6
- package/lib/service.js +86 -7
- package/lib/settings.js +2 -1
- package/lib/store.js +231 -21
- package/lib/tools.js +27 -9
- package/lib/write-admission.js +259 -0
- package/package.json +4 -4
- package/src/api-standalone.js +57 -1
- package/src/api.js +34 -11
- package/src/config.js +54 -6
- package/src/content-hash.js +54 -0
- package/src/document-index.js +94 -0
- package/src/document.js +93 -11
- package/src/index.js +50 -11
- package/src/inject.js +6 -2
- package/src/lang.js +15 -4
- package/src/maintenance.js +142 -0
- package/src/mirror.js +147 -15
- package/src/recall-stats.js +52 -2
- package/src/scope.js +34 -6
- package/src/service.js +86 -7
- package/src/settings.js +2 -1
- package/src/store.js +231 -21
- package/src/tools.js +27 -9
- package/src/write-admission.js +259 -0
- package/test/api.test.js +12 -0
- package/test/client.test.js +90 -5
- package/test/content-hash.test.js +204 -0
- package/test/document-dir.test.js +233 -0
- package/test/document.test.js +151 -3
- package/test/inject-parent-gate.test.js +106 -0
- package/test/inject-pools.test.js +8 -3
- package/test/llm-audit.test.js +42 -0
- package/test/maintenance.test.js +391 -0
- package/test/mirror-types.test.js +208 -0
- package/test/recall-evals.test.js +18 -0
- package/test/recall-stats.test.js +66 -0
- package/test/write-admission.test.js +345 -0
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { execFile, spawnSync } from "node:child_process";
|
|
4
|
+
import { promisify } from "node:util";
|
|
5
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
6
|
+
import { tmpdir } from "node:os";
|
|
7
|
+
import { dirname, join, resolve } from "node:path";
|
|
8
|
+
import { fileURLToPath } from "node:url";
|
|
9
|
+
import { createStore } from "../src/store.js";
|
|
10
|
+
import { createService } from "../src/service.js";
|
|
11
|
+
import { createSettings } from "../src/settings.js";
|
|
12
|
+
import { createMaintenance } from "../src/maintenance.js";
|
|
13
|
+
import { createStandaloneApi } from "../src/api-standalone.js";
|
|
14
|
+
|
|
15
|
+
// #275 存储生命周期第一批:无损回收的手动入口。
|
|
16
|
+
// 这一批的验收就三条:dry-run 一个字节都不改(数字与执行出自同一处统计);执行时只丢
|
|
17
|
+
// 「可由记忆库重建的输入快照」与「检索按定义不可达的归档行向量」,行数不变、决策原文
|
|
18
|
+
// 留住、活跃行向量留住;收益按 VACUUM 前后体积量,不按列字节估。
|
|
19
|
+
|
|
20
|
+
const NOW = Date.parse("2026-09-24T00:00:00.000Z");
|
|
21
|
+
const DAY = 86400000;
|
|
22
|
+
const CLI = resolve(dirname(fileURLToPath(import.meta.url)), "..", "bin", "cli.mjs");
|
|
23
|
+
// 服务跑在本进程里,所以子进程必须**异步**起:spawnSync 会堵住事件循环,服务就永远
|
|
24
|
+
// 回不了应答,CLI 只会等到自己的 10s 超时(自造死锁)。
|
|
25
|
+
const runCli = promisify(execFile);
|
|
26
|
+
|
|
27
|
+
function fileStore() {
|
|
28
|
+
const dir = mkdtempSync(join(tmpdir(), "dsh-mneme-reclaim-"));
|
|
29
|
+
const store = createStore(join(dir, "memory.db"));
|
|
30
|
+
return {
|
|
31
|
+
store,
|
|
32
|
+
// 闭包拿 store,不用 this:裸函数调用下 this 是 undefined,异常一旦被吞掉就会
|
|
33
|
+
// 变成「库没关就删目录」的 EPERM(清理失败还说不清原因)。
|
|
34
|
+
close() {
|
|
35
|
+
try {
|
|
36
|
+
store.close();
|
|
37
|
+
} catch { /* already closed */ }
|
|
38
|
+
rmSync(dir, { recursive: true, force: true });
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** 两条 run(窗口外 / 窗口内)+ 一行活跃 + 一行带向量的归档行。 */
|
|
44
|
+
function seed(store) {
|
|
45
|
+
store.saveDreamRun({
|
|
46
|
+
id: "old-run",
|
|
47
|
+
created_at: new Date(NOW - 30 * DAY).toISOString(),
|
|
48
|
+
status: "ok",
|
|
49
|
+
snapshot_hash: "h-old",
|
|
50
|
+
input_count: 2,
|
|
51
|
+
input: JSON.stringify([{ id: "m1", content: "x".repeat(600) }]),
|
|
52
|
+
decisions: JSON.stringify([{ action: "keep", ids: ["m1"] }]),
|
|
53
|
+
receipt: "dsh-mneme:run:old-run:ok:h-old"
|
|
54
|
+
});
|
|
55
|
+
store.saveDreamRun({
|
|
56
|
+
id: "fresh-run",
|
|
57
|
+
created_at: new Date(NOW - 1 * DAY).toISOString(),
|
|
58
|
+
status: "ok",
|
|
59
|
+
snapshot_hash: "h-new",
|
|
60
|
+
input_count: 1,
|
|
61
|
+
input: JSON.stringify([{ id: "m2", content: "y".repeat(600) }]),
|
|
62
|
+
decisions: JSON.stringify([{ action: "keep", ids: ["m2"] }]),
|
|
63
|
+
receipt: "dsh-mneme:run:fresh-run:ok:h-new"
|
|
64
|
+
});
|
|
65
|
+
const active = store.save({ type: "project", title: "活跃", content: "活跃内容" });
|
|
66
|
+
store.setEmbedding(active.id, [0.125, 0.25, 0.5]);
|
|
67
|
+
const archived = store.save({ type: "project", title: "归档", content: "归档内容" });
|
|
68
|
+
store.setEmbedding(archived.id, [0.5, 0.25, 0.125]);
|
|
69
|
+
store.setArchived(archived.id, true);
|
|
70
|
+
// organize 的 run:同一列 `input` 存的是 apply 的重放载荷(organize.js 的 apply
|
|
71
|
+
// 直接读它重建 byIndex),不是可重建的快照——它必须活过任何窗口的回收。
|
|
72
|
+
store.saveDreamRun({
|
|
73
|
+
id: "organize-run",
|
|
74
|
+
created_at: new Date(NOW - 30 * DAY).toISOString(),
|
|
75
|
+
status: "ok",
|
|
76
|
+
snapshot_hash: "h-org",
|
|
77
|
+
input_count: 1,
|
|
78
|
+
input: JSON.stringify([{ index: 0, candidate: { type: "project", title: "整理候选", content: "候选正文" } }]),
|
|
79
|
+
decisions: JSON.stringify([{ action: "save", index: 0 }]),
|
|
80
|
+
receipt: "dsh-mneme:run:organize-run:ok:h-org",
|
|
81
|
+
run_type: "organize"
|
|
82
|
+
});
|
|
83
|
+
return { active, archived };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const rawRun = (store, id) =>
|
|
87
|
+
store.db.prepare("SELECT input, decisions, receipt, status FROM dream_runs WHERE id = ?").get(id);
|
|
88
|
+
const rawVec = (store, id) =>
|
|
89
|
+
store.db.prepare("SELECT embedding FROM memories WHERE id = ?").get(id).embedding;
|
|
90
|
+
// count() 默认不含归档行,而「一条记忆都没删」要连归档一起数
|
|
91
|
+
const rawMemoryCount = (store) => store.db.prepare("SELECT count(*) AS c FROM memories").get().c;
|
|
92
|
+
|
|
93
|
+
function maintenanceFor(store, config = {}) {
|
|
94
|
+
return createMaintenance({ store, config, logger: null, now: () => NOW });
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
test("dry-run:报出将清理的行数与列文本大小,一个字节都不改", () => {
|
|
98
|
+
const { store, close } = fileStore();
|
|
99
|
+
try {
|
|
100
|
+
const { archived } = seed(store);
|
|
101
|
+
const report = maintenanceFor(store).reclaim({ dryRun: true });
|
|
102
|
+
|
|
103
|
+
assert.equal(report.dryRun, true);
|
|
104
|
+
assert.equal(report.olderThanDays, 7, "默认保留窗口 7 天");
|
|
105
|
+
assert.deepEqual(report.planned, { dream_run_inputs: 1, archived_embeddings: 1 });
|
|
106
|
+
assert.deepEqual(report.cleared, { dream_run_inputs: 0, archived_embeddings: 0 });
|
|
107
|
+
assert.ok(report.column_bytes.dream_run_inputs > 600, "列文本大小要给真实数字(它是上界)");
|
|
108
|
+
assert.equal(typeof report.size.before_bytes, "number");
|
|
109
|
+
assert.equal(report.vacuum.ran, false);
|
|
110
|
+
|
|
111
|
+
assert.ok(rawRun(store, "old-run").input, "dry-run 不得置空 input");
|
|
112
|
+
assert.ok(rawVec(store, archived.id), "dry-run 不得清向量");
|
|
113
|
+
assert.equal(store.listLlmAudits().length, 0, "dry-run 不落回执");
|
|
114
|
+
} finally {
|
|
115
|
+
close();
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
test("执行:只清窗口外的输入快照,run 的骨架与决策原文留住", () => {
|
|
120
|
+
const { store, close } = fileStore();
|
|
121
|
+
try {
|
|
122
|
+
seed(store);
|
|
123
|
+
const report = maintenanceFor(store).reclaim({ dryRun: false });
|
|
124
|
+
assert.equal(report.cleared.dream_run_inputs, 1);
|
|
125
|
+
|
|
126
|
+
const old = rawRun(store, "old-run");
|
|
127
|
+
assert.equal(old.input, null, "窗口外的快照置空");
|
|
128
|
+
assert.equal(old.status, "ok", "状态是骨架,留住");
|
|
129
|
+
assert.ok(old.decisions, "LLM 决策原文不可重建,留住");
|
|
130
|
+
assert.ok(old.receipt, "receipt 留住");
|
|
131
|
+
assert.ok(rawRun(store, "fresh-run").input, "窗口内的快照不动");
|
|
132
|
+
assert.equal(rawMemoryCount(store), 2, "一条记忆都没删(本入口止体积不止条数)");
|
|
133
|
+
} finally {
|
|
134
|
+
close();
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
test("执行:清归档行向量、留活跃行向量", () => {
|
|
139
|
+
const { store, close } = fileStore();
|
|
140
|
+
try {
|
|
141
|
+
const { active, archived } = seed(store);
|
|
142
|
+
const report = maintenanceFor(store).reclaim({ dryRun: false });
|
|
143
|
+
assert.equal(report.cleared.archived_embeddings, 1);
|
|
144
|
+
assert.equal(rawVec(store, archived.id), null, "归档行向量按定义不可达,清掉");
|
|
145
|
+
assert.ok(rawVec(store, active.id), "活跃行向量不许碰");
|
|
146
|
+
} finally {
|
|
147
|
+
close();
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
test("olderThanDays=0 清掉全部快照;窗口外的行数与 dry-run 报的一致", () => {
|
|
152
|
+
const { store, close } = fileStore();
|
|
153
|
+
try {
|
|
154
|
+
seed(store);
|
|
155
|
+
const maintenance = maintenanceFor(store);
|
|
156
|
+
assert.equal(maintenance.reclaim({ dryRun: true }).planned.dream_run_inputs, 1);
|
|
157
|
+
const report = maintenance.reclaim({ dryRun: false, olderThanDays: 0 });
|
|
158
|
+
assert.equal(report.cleared.dream_run_inputs, 2, "0 天 = 全部快照");
|
|
159
|
+
assert.equal(rawRun(store, "fresh-run").input, null);
|
|
160
|
+
assert.ok(rawRun(store, "fresh-run").decisions, "决策原文仍在");
|
|
161
|
+
} finally {
|
|
162
|
+
close();
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
test("organize 的 input 是重放载荷,任何窗口都不清(清了 apply 会静默空转)", () => {
|
|
167
|
+
const { store, close } = fileStore();
|
|
168
|
+
try {
|
|
169
|
+
seed(store);
|
|
170
|
+
const maintenance = maintenanceFor(store);
|
|
171
|
+
// 窗口 0 = 清全部快照,也必须绕开 organize
|
|
172
|
+
assert.equal(maintenance.reclaim({ dryRun: true, olderThanDays: 0 }).planned.dream_run_inputs, 2);
|
|
173
|
+
const report = maintenance.reclaim({ dryRun: false, olderThanDays: 0 });
|
|
174
|
+
assert.equal(report.cleared.dream_run_inputs, 2, "只清 auto/sleep 的两个 run");
|
|
175
|
+
assert.ok(rawRun(store, "organize-run").input, "organize 的重放载荷必须留住");
|
|
176
|
+
assert.ok(rawRun(store, "organize-run").decisions, "决策也留住");
|
|
177
|
+
} finally {
|
|
178
|
+
close();
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
test("VACUUM 失败不回吞:清理照做、报告与回执都带上失败原因", () => {
|
|
183
|
+
const { store, close } = fileStore();
|
|
184
|
+
try {
|
|
185
|
+
seed(store);
|
|
186
|
+
// 模拟锁被别的进程占着(SQLITE_BUSY)——这是最常见的失败,且两个 UPDATE 早已各自提交
|
|
187
|
+
const maintenance = createMaintenance({
|
|
188
|
+
store: {
|
|
189
|
+
...store,
|
|
190
|
+
dreamRunInputStats: (b) => store.dreamRunInputStats(b),
|
|
191
|
+
clearDreamRunInputs: (b) => store.clearDreamRunInputs(b),
|
|
192
|
+
archivedEmbeddingStats: () => store.archivedEmbeddingStats(),
|
|
193
|
+
clearArchivedEmbeddings: () => store.clearArchivedEmbeddings(),
|
|
194
|
+
storageStats: () => store.storageStats(),
|
|
195
|
+
saveLlmAudit: (e) => store.saveLlmAudit(e),
|
|
196
|
+
vacuum: () => { throw new Error("SQLITE_BUSY: database is locked"); }
|
|
197
|
+
},
|
|
198
|
+
config: {},
|
|
199
|
+
logger: null,
|
|
200
|
+
now: () => NOW
|
|
201
|
+
});
|
|
202
|
+
const report = maintenance.reclaim({ dryRun: false, vacuum: true });
|
|
203
|
+
assert.equal(report.cleared.dream_run_inputs, 1, "清理已完成");
|
|
204
|
+
assert.equal(report.vacuum.ran, false);
|
|
205
|
+
assert.match(report.vacuum.error, /SQLITE_BUSY/);
|
|
206
|
+
assert.ok(report.receipt, "清掉了东西就必须有回执,哪怕 VACUUM 没跑成");
|
|
207
|
+
assert.equal(rawRun(store, "old-run").input, null);
|
|
208
|
+
assert.equal(store.listLlmAudits({ source: "maintenance" }).length, 1);
|
|
209
|
+
} finally {
|
|
210
|
+
close();
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
test("--vacuum:体积量到真实下降、freelist 归零,库仍可读可写", () => {
|
|
215
|
+
const { store, close } = fileStore();
|
|
216
|
+
try {
|
|
217
|
+
seed(store);
|
|
218
|
+
// 撑出可回收的体积:VACUUM 的收益按前后体积量,没有真体积就量不到下降
|
|
219
|
+
store.saveDreamRun({
|
|
220
|
+
id: "bulk-run",
|
|
221
|
+
created_at: new Date(NOW - 60 * DAY).toISOString(),
|
|
222
|
+
status: "ok",
|
|
223
|
+
snapshot_hash: "h-bulk",
|
|
224
|
+
input_count: 1,
|
|
225
|
+
input: JSON.stringify([{ id: "m9", content: "z".repeat(400_000) }]),
|
|
226
|
+
decisions: "[]",
|
|
227
|
+
receipt: "dsh-mneme:run:bulk-run:ok:h-bulk"
|
|
228
|
+
});
|
|
229
|
+
const before = store.storageStats().fileBytes;
|
|
230
|
+
const report = maintenanceFor(store).reclaim({ dryRun: false, vacuum: true });
|
|
231
|
+
assert.equal(report.vacuum.requested, true);
|
|
232
|
+
assert.equal(report.vacuum.ran, true, report.vacuum.error ?? "");
|
|
233
|
+
assert.equal(report.size.freelist_pages, 0, "VACUUM 之后没有空闲页");
|
|
234
|
+
assert.ok(report.size.after_bytes < before, `体积必须真的下降:${before} → ${report.size.after_bytes}`);
|
|
235
|
+
assert.equal(rawMemoryCount(store), 2);
|
|
236
|
+
const after = store.save({ type: "project", title: "vacuum 之后", content: "写入照常" });
|
|
237
|
+
assert.ok(store.getById(after.id), "VACUUM 后库仍可用");
|
|
238
|
+
} finally {
|
|
239
|
+
close();
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
test("执行落一行回执;llmAudit 关掉时不落回执但清理照做", () => {
|
|
244
|
+
const on = fileStore();
|
|
245
|
+
try {
|
|
246
|
+
seed(on.store);
|
|
247
|
+
const report = maintenanceFor(on.store).reclaim({ dryRun: false });
|
|
248
|
+
const rows = on.store.listLlmAudits({ source: "maintenance" });
|
|
249
|
+
assert.equal(rows.length, 1, "执行留一行 receipt");
|
|
250
|
+
assert.equal(rows[0].operation_type, "storage_reclaim");
|
|
251
|
+
assert.equal(rows[0].model_id, "-", "不是模型调用,占位串避免混进花费统计");
|
|
252
|
+
assert.equal(rows[0].metadata.cleared.dream_run_inputs, 1);
|
|
253
|
+
assert.equal(rows[0].metadata.vacuum, false);
|
|
254
|
+
assert.equal(report.receipt, rows[0].id);
|
|
255
|
+
} finally {
|
|
256
|
+
on.close();
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
const off = fileStore();
|
|
260
|
+
try {
|
|
261
|
+
seed(off.store);
|
|
262
|
+
const report = maintenanceFor(off.store, { llmAudit: { enabled: false } }).reclaim({ dryRun: false });
|
|
263
|
+
assert.equal(off.store.listLlmAudits().length, 0, "审计关掉时不落回执");
|
|
264
|
+
assert.equal(report.cleared.dream_run_inputs, 1, "但清理照做:记不上账不等于没发生");
|
|
265
|
+
} finally {
|
|
266
|
+
off.close();
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
test("取消归档重新排队嵌入:被回收的向量能拿回来", () => {
|
|
271
|
+
const { store, close } = fileStore();
|
|
272
|
+
try {
|
|
273
|
+
const service = createService({ store, mirror: null, config: {} });
|
|
274
|
+
const scheduled = [];
|
|
275
|
+
service.setEmbedder({ ready: true, schedule: (memory) => scheduled.push(memory.id) });
|
|
276
|
+
const { archived } = seed(store);
|
|
277
|
+
store.clearArchivedEmbeddings();
|
|
278
|
+
service.setArchived(archived.id, false);
|
|
279
|
+
assert.deepEqual(scheduled, [archived.id], "还原时必须重新排队嵌入,否则回收成了单程票");
|
|
280
|
+
|
|
281
|
+
// 判据要与 store.setArchived 的归一化同口径(真值):API/工具传 0 也会把行放回
|
|
282
|
+
// 活跃面,那条路同样得补嵌入,否则就是「还原了但没有向量」的静默降级。
|
|
283
|
+
store.setArchived(archived.id, true);
|
|
284
|
+
service.setArchived(archived.id, 0);
|
|
285
|
+
assert.deepEqual(scheduled, [archived.id, archived.id]);
|
|
286
|
+
} finally {
|
|
287
|
+
close();
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
test("HTTP 面:默认 dry-run;带 dryRun:false 但缺 confirm 只回报告不执行", async () => {
|
|
292
|
+
const { store, close } = fileStore();
|
|
293
|
+
const maintenance = maintenanceFor(store);
|
|
294
|
+
const service = createService({ store, mirror: null, config: {} });
|
|
295
|
+
const settings = createSettings(store.db);
|
|
296
|
+
const api = createStandaloneApi({ service, store, config: {}, settings, logger: null, port: 0, maintenance });
|
|
297
|
+
await api.ready;
|
|
298
|
+
const url = `http://127.0.0.1:${api.port}/maintenance/reclaim`;
|
|
299
|
+
const post = (body) =>
|
|
300
|
+
fetch(url, {
|
|
301
|
+
method: "POST",
|
|
302
|
+
headers: { authorization: `Bearer ${api.token}`, "content-type": "application/json" },
|
|
303
|
+
body: JSON.stringify(body)
|
|
304
|
+
});
|
|
305
|
+
try {
|
|
306
|
+
const { archived } = seed(store);
|
|
307
|
+
|
|
308
|
+
const dry = await post({});
|
|
309
|
+
assert.equal(dry.status, 200);
|
|
310
|
+
const dryBody = await dry.json();
|
|
311
|
+
assert.equal(dryBody.dryRun, true);
|
|
312
|
+
assert.equal(dryBody.planned.dream_run_inputs, 1);
|
|
313
|
+
assert.ok(rawVec(store, archived.id), "不带 body 的默认路径不改数据");
|
|
314
|
+
|
|
315
|
+
const refused = await post({ dryRun: false, vacuum: true });
|
|
316
|
+
assert.equal(refused.status, 400);
|
|
317
|
+
const refusedBody = await refused.json();
|
|
318
|
+
assert.equal(refusedBody.error, "confirm-required");
|
|
319
|
+
assert.equal(refusedBody.report.planned.archived_embeddings, 1, "拒绝执行时把 dry-run 报告一并回给调用方");
|
|
320
|
+
assert.equal(refusedBody.report.vacuum.requested, true, "报告必须描述将要执行的那个动作(含 vacuum)");
|
|
321
|
+
assert.ok(rawVec(store, archived.id), "缺 confirm 时一个字节都不改");
|
|
322
|
+
|
|
323
|
+
const applied = await post({ dryRun: false, confirm: true });
|
|
324
|
+
assert.equal(applied.status, 200);
|
|
325
|
+
const appliedBody = await applied.json();
|
|
326
|
+
assert.equal(appliedBody.cleared.dream_run_inputs, 1);
|
|
327
|
+
assert.equal(appliedBody.cleared.archived_embeddings, 1);
|
|
328
|
+
assert.equal(rawVec(store, archived.id), null);
|
|
329
|
+
|
|
330
|
+
const badDays = await post({ olderThanDays: -1 });
|
|
331
|
+
assert.equal(badDays.status, 400, "非法窗口给干净 400,不落进 SQL");
|
|
332
|
+
assert.equal((await badDays.json()).error, "invalid-older-than-days");
|
|
333
|
+
|
|
334
|
+
// `Number()` 会把 ""/[]/false 化成 0,而 0 在这里是「清全部快照」:最坏解释绝不
|
|
335
|
+
// 能让畸形输入静默拿到,HTTP 面只收 JSON 数字
|
|
336
|
+
for (const bogus of ["", [], false]) {
|
|
337
|
+
const bad = await post({ olderThanDays: bogus });
|
|
338
|
+
assert.equal(bad.status, 400, `畸形窗口必须拒:${JSON.stringify(bogus)}`);
|
|
339
|
+
assert.equal((await bad.json()).error, "invalid-older-than-days");
|
|
340
|
+
}
|
|
341
|
+
} finally {
|
|
342
|
+
api.server.closeIdleConnections?.();
|
|
343
|
+
api.server.close();
|
|
344
|
+
close();
|
|
345
|
+
}
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
// CLI 那一层是用户真正敲的东西(`dsh-mneme reclaim`),参数语义错一步就是「以为在
|
|
349
|
+
// dry-run 却动了数据」这类事故,所以起真服务、真 spawn 它。
|
|
350
|
+
|
|
351
|
+
test("CLI:reclaim 默认 dry-run 报数字,--apply --vacuum 才真收", async () => {
|
|
352
|
+
const { store, close } = fileStore();
|
|
353
|
+
const maintenance = maintenanceFor(store);
|
|
354
|
+
const service = createService({ store, mirror: null, config: {} });
|
|
355
|
+
const settings = createSettings(store.db);
|
|
356
|
+
const api = createStandaloneApi({ service, store, config: {}, settings, logger: null, port: 0, maintenance });
|
|
357
|
+
await api.ready;
|
|
358
|
+
const auth = [`--url`, `http://127.0.0.1:${api.port}`, `--token`, api.token];
|
|
359
|
+
const run = async (args) => {
|
|
360
|
+
const { stdout, stderr } = await runCli(process.execPath, [CLI, ...args], { encoding: "utf8" });
|
|
361
|
+
return { stdout, stderr };
|
|
362
|
+
};
|
|
363
|
+
try {
|
|
364
|
+
const { archived } = seed(store);
|
|
365
|
+
|
|
366
|
+
const dryRun = await run(["reclaim", ...auth]);
|
|
367
|
+
assert.match(dryRun.stdout, /dry-run,未改动任何数据/);
|
|
368
|
+
assert.match(dryRun.stdout, /将清理: dream_runs\.input 1 行 \/ 归档行向量 1 行/);
|
|
369
|
+
assert.ok(rawVec(store, archived.id), "默认路径不得动数据");
|
|
370
|
+
|
|
371
|
+
const apply = await run(["reclaim", "--apply", "--vacuum", ...auth]);
|
|
372
|
+
assert.match(apply.stdout, /已执行/);
|
|
373
|
+
assert.match(apply.stdout, /已清理: dream_runs\.input 1 行 \/ 归档行向量 1 行/);
|
|
374
|
+
assert.match(apply.stdout, /回执: llm_audit_logs #\d+/);
|
|
375
|
+
assert.equal(rawVec(store, archived.id), null);
|
|
376
|
+
assert.equal(rawRun(store, "old-run").input, null);
|
|
377
|
+
|
|
378
|
+
const asJson = await run(["reclaim", "--json", ...auth]);
|
|
379
|
+
assert.equal(JSON.parse(asJson.stdout).dryRun, true);
|
|
380
|
+
} finally {
|
|
381
|
+
api.server.closeIdleConnections?.();
|
|
382
|
+
api.server.close();
|
|
383
|
+
close();
|
|
384
|
+
}
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
test("CLI:--vacuum 单独用会被拒(dry-run 没有可回收的页),也不动数据", () => {
|
|
388
|
+
const result = spawnSync(process.execPath, [CLI, "reclaim", "--vacuum", "--token", "x"], { encoding: "utf8" });
|
|
389
|
+
assert.equal(result.status, 1);
|
|
390
|
+
assert.match(result.stderr, /--vacuum 需要配 --apply/);
|
|
391
|
+
});
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { mkdtempSync, readFileSync, rmSync, existsSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import { MIRROR_EXCLUDED_TYPES, MIRROR_READONLY_TYPES, TYPE_FILE, createMirror, parseHumanEdits, renderMirrorText } from "../src/mirror.js";
|
|
7
|
+
import { createStore, TYPES } from "../src/store.js";
|
|
8
|
+
import { createService } from "../src/service.js";
|
|
9
|
+
import { PACKAGE_VERSION } from "../src/version-check.js";
|
|
10
|
+
|
|
11
|
+
// #278 第一批:落盘盲区。TYPE_FILE 原先只有 5 个键,pitfall / constraint /
|
|
12
|
+
// rejected_solution / pattern 这些后加的 type 从不进镜像与导出——落点存在,但对
|
|
13
|
+
// 一半活跃记忆是盲的,且没有任何提示。#296 第二批把 document 也收了进来(只读
|
|
14
|
+
// 指针视图),排除集因此为空;两个集合的边界仍由下面的双向断言钉住。
|
|
15
|
+
const FORMERLY_UNCOVERED = ["pitfall", "constraint", "rejected_solution", "pattern"];
|
|
16
|
+
|
|
17
|
+
function tempDir() {
|
|
18
|
+
return mkdtempSync(join(tmpdir(), "dsh-mneme-types-"));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function memory(type, over = {}) {
|
|
22
|
+
return {
|
|
23
|
+
id: "m1", type, title: "标题", content: "内容", tags: ["a"], importance: 3,
|
|
24
|
+
forgotten: false, created_at: "2026-01-01T00:00:00.000Z",
|
|
25
|
+
updated_at: "2026-01-01T00:00:00.000Z", ...over
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
test("#278: every store type has a landing point except the pinned exclusions", () => {
|
|
30
|
+
const missing = [...TYPES].filter((t) => !TYPE_FILE[t] && !MIRROR_EXCLUDED_TYPES.has(t));
|
|
31
|
+
assert.deepEqual(missing, [], "新增 type 必须显式决定落不落镜像,不能留下静默盲区");
|
|
32
|
+
assert.deepEqual(Object.keys(TYPE_FILE).filter((t) => !TYPES.has(t)), [], "TYPE_FILE 不得含 store 不认识的键");
|
|
33
|
+
assert.deepEqual([...MIRROR_EXCLUDED_TYPES].filter((t) => !TYPES.has(t)), [], "排除集里的 key 必须是真 type");
|
|
34
|
+
// 双向:注释说的「TYPES \ TYPE_FILE 恰好等于排除集」要真被钉住。只查上面两条
|
|
35
|
+
// 的话,将来有人把 document 加进 TYPE_FILE 却忘了从排除集删,两边同时变假还全绿。
|
|
36
|
+
assert.deepEqual([...MIRROR_EXCLUDED_TYPES].filter((t) => TYPE_FILE[t]), [], "排除集与落盘集不得重叠");
|
|
37
|
+
for (const type of FORMERLY_UNCOVERED) {
|
|
38
|
+
assert.ok(TYPE_FILE[type], `${type} 必须有镜像文件(#278 第一批验收项)`);
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test("#278: an entry of a formerly uncovered type survives render → parse round-trip", () => {
|
|
43
|
+
const text = renderMirrorText("pitfall", [memory("pitfall")]);
|
|
44
|
+
const [edit] = parseHumanEdits(text);
|
|
45
|
+
assert.equal(edit.id, "m1");
|
|
46
|
+
assert.equal(edit.title, "标题");
|
|
47
|
+
assert.equal(edit.content, "内容", "frontmatter 与元数据行都不得进 content");
|
|
48
|
+
assert.equal(edit.updated_at, "2026-01-01T00:00:00.000Z");
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("#278: file header carries type / covered / coverage / tags union", () => {
|
|
52
|
+
const text = renderMirrorText("constraint", [
|
|
53
|
+
memory("constraint", { id: "m2", tags: ["z", "a"], updated_at: "2026-02-01T00:00:00.000Z" }),
|
|
54
|
+
memory("constraint", { id: "m1", tags: ["a", "m"] })
|
|
55
|
+
]);
|
|
56
|
+
assert.ok(text.startsWith("---\n"), "frontmatter 必须在文件最前");
|
|
57
|
+
const fm = text.slice(4, text.indexOf("\n---", 3));
|
|
58
|
+
assert.match(fm, /^type: constraint$/m);
|
|
59
|
+
// OKF §7 actor 约定:<producer>/<version>,不是裸的 producer 名
|
|
60
|
+
assert.equal(fm.match(/^generated\.by: (.+)$/m)?.[1], `dsh-mneme/${PACKAGE_VERSION}`);
|
|
61
|
+
assert.match(fm, /^generated\.at: \d{4}-\d{2}-\d{2}T/m);
|
|
62
|
+
assert.match(fm, /^covered: 2$/m);
|
|
63
|
+
assert.match(fm, /^coverage: active-only$/m);
|
|
64
|
+
assert.match(fm, /^tags: \["a", "m", "z"\]$/m);
|
|
65
|
+
// frontmatter 不是条目、也不改条目数
|
|
66
|
+
assert.equal(parseHumanEdits(text).length, 2);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test("#278/#296: mirror writes the formerly uncovered files, and documents land as a pointer view", () => {
|
|
70
|
+
const dir = tempDir();
|
|
71
|
+
try {
|
|
72
|
+
const mirror = createMirror(dir);
|
|
73
|
+
const rows = FORMERLY_UNCOVERED.map((type, i) => memory(type, { id: `m${i}`, content: `内容${i}` }));
|
|
74
|
+
rows.push(memory("document", {
|
|
75
|
+
id: "doc1", title: "报告", content: "第一句。第二句不该出现。", doc_path: "C:/docs/report.md"
|
|
76
|
+
}));
|
|
77
|
+
mirror.sync(rows);
|
|
78
|
+
for (const [i, type] of FORMERLY_UNCOVERED.entries()) {
|
|
79
|
+
const file = join(dir, TYPE_FILE[type]);
|
|
80
|
+
assert.ok(existsSync(file), `${TYPE_FILE[type]} 必须落盘`);
|
|
81
|
+
const [edit] = parseHumanEdits(readFileSync(file, "utf8"));
|
|
82
|
+
assert.equal(edit.id, `m${i}`);
|
|
83
|
+
assert.equal(edit.content, `内容${i}`);
|
|
84
|
+
}
|
|
85
|
+
// #296 第 4 条:document 落盘,但只给指针行——id + 标题 + 摘要首句 + 路径,
|
|
86
|
+
// 不含正文;文件头必须说清这是只读视图(不能复用「可编辑」那句)。
|
|
87
|
+
assert.ok(MIRROR_READONLY_TYPES.has("document"), "document 是只读 type");
|
|
88
|
+
assert.ok(existsSync(join(dir, "documents.md")), "document 指针行现在落盘");
|
|
89
|
+
const docs = readFileSync(join(dir, "documents.md"), "utf8");
|
|
90
|
+
assert.match(docs, /只读视图/);
|
|
91
|
+
assert.ok(docs.includes("doc1") && docs.includes("报告") && docs.includes("第一句"));
|
|
92
|
+
assert.ok(docs.includes("report.md"), "指针行必须给出 doc_path,否则「看着完整、其实找不到文件」的老问题原样回来");
|
|
93
|
+
assert.ok(!docs.includes("第二句"), "只读视图不含全文,只留摘要首句");
|
|
94
|
+
assert.equal(parseHumanEdits(docs).length, 0, "指针行不是可回填的条目");
|
|
95
|
+
} finally {
|
|
96
|
+
rmSync(dir, { recursive: true, force: true });
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test("#296: readHumanEdits never returns the read-only type — a hand-edited view cannot reach the store", () => {
|
|
101
|
+
const dir = tempDir();
|
|
102
|
+
try {
|
|
103
|
+
const mirror = createMirror(dir);
|
|
104
|
+
mirror.sync([memory("document", { id: "doc1", doc_path: "/tmp/report.md" })]);
|
|
105
|
+
// 模拟有人在 documents.md 里手改(视图是机器所有,改动必须被忽略而不是回填)
|
|
106
|
+
const file = join(dir, TYPE_FILE.document);
|
|
107
|
+
writeFileSync(file, readFileSync(file, "utf8") + "- **ID**: `doc1`\n- **类型**: document\n被人改过的正文\n", "utf8");
|
|
108
|
+
assert.deepEqual(mirror.readHumanEdits("document"), [], "指定的只读 type 也要跳过");
|
|
109
|
+
assert.deepEqual(mirror.readHumanEdits().filter((e) => e.id === "doc1"), [], "全量扫描同样跳过它");
|
|
110
|
+
} finally {
|
|
111
|
+
rmSync(dir, { recursive: true, force: true });
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test("#278: mirror covers every active row — no silent 500-row truncation", () => {
|
|
116
|
+
const dir = tempDir();
|
|
117
|
+
let store;
|
|
118
|
+
try {
|
|
119
|
+
store = createStore(join(dir, "memory.db"));
|
|
120
|
+
const service = createService({ store, mirror: createMirror(dir), config: {} });
|
|
121
|
+
for (let i = 0; i < 501; i++) store.save({ type: "pitfall", title: `t${i}`, content: `c${i}` });
|
|
122
|
+
// 任意一次业务写触发一次镜像同步(不是逐条 save,避免 501 次渲染)
|
|
123
|
+
service.saveWithDedupe({ type: "pattern", title: "trigger", content: "x" });
|
|
124
|
+
const text = readFileSync(join(dir, TYPE_FILE.pitfall), "utf8");
|
|
125
|
+
assert.match(text, /^covered: 501$/m, "文件头必须如实自述覆盖条数");
|
|
126
|
+
assert.equal((text.match(/^- \*\*ID\*\*: /gm) ?? []).length, 501, "超过 500 条的活跃集必须全部落盘");
|
|
127
|
+
assert.equal(parseHumanEdits(text).length, 501);
|
|
128
|
+
} finally {
|
|
129
|
+
// close 必须在 rm 之前:Windows 上库文件仍被打开时 rmSync 会 EPERM,把断言
|
|
130
|
+
// 失败覆盖成文件锁错误。
|
|
131
|
+
store?.close();
|
|
132
|
+
rmSync(dir, { recursive: true, force: true });
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
test("#278: scope / sensitivity 只作展示字段,且永不进 content", () => {
|
|
137
|
+
const text = renderMirrorText("constraint", [memory("constraint", {
|
|
138
|
+
agent_scope: "standard",
|
|
139
|
+
workspace_scope: "D:/WorkSpace/demo",
|
|
140
|
+
sensitivity: "personal"
|
|
141
|
+
})]);
|
|
142
|
+
assert.match(text, /^- \*\*作用域\*\*: standard \/ D:\/WorkSpace\/demo$/m);
|
|
143
|
+
assert.match(text, /^- \*\*敏感度\*\*: personal$/m);
|
|
144
|
+
assert.equal(parseHumanEdits(text)[0].content, "内容", "新字段不得进 content");
|
|
145
|
+
// 没值的条目不该多出空字段行(本机 2843 条里只有 37 条带 scope 标签)
|
|
146
|
+
const bare = renderMirrorText("constraint", [memory("constraint")]);
|
|
147
|
+
assert.ok(!bare.includes("作用域") && !bare.includes("敏感度"));
|
|
148
|
+
// 值是调用方传的任意字符串:里面的换行不得伪造出第二个条目头
|
|
149
|
+
const injected = renderMirrorText("constraint", [memory("constraint", {
|
|
150
|
+
sensitivity: "x\n- **ID**: `ghost`\n- **类型**: decision"
|
|
151
|
+
})]);
|
|
152
|
+
assert.equal(parseHumanEdits(injected).length, 1, "值里的换行不得造出幽灵条目");
|
|
153
|
+
// 值本身仍在(被压成一行),关键是它不再位于行首——行首才是条目头的锚
|
|
154
|
+
assert.ok(!/^- \*\*ID\*\*: `ghost`/m.test(injected), "不得出现伪造的条目头");
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
test("#278: mirror keeps its active-only claim — archived and forgotten rows stay out", () => {
|
|
158
|
+
const dir = tempDir();
|
|
159
|
+
let store;
|
|
160
|
+
try {
|
|
161
|
+
store = createStore(join(dir, "memory.db"));
|
|
162
|
+
const service = createService({ store, mirror: createMirror(dir), config: {} });
|
|
163
|
+
// forgotten 在 INSERT 里硬编码为 0(store.js insertMemoryRow),只能经 setForget
|
|
164
|
+
// 设置;archived 可以在载荷里给,这里统一走 setter 保持同一个路径。
|
|
165
|
+
for (const [id, title] of [["keep-c", "留"], ["arch-c", "归档"], ["gone-c", "遗忘"]]) {
|
|
166
|
+
store.save({ id, type: "constraint", title, content: "c" });
|
|
167
|
+
}
|
|
168
|
+
store.setArchived("arch-c", true);
|
|
169
|
+
store.setForget("gone-c", true);
|
|
170
|
+
service.saveWithDedupe({ type: "pattern", title: "trigger", content: "x" });
|
|
171
|
+
const text = readFileSync(join(dir, TYPE_FILE.constraint), "utf8");
|
|
172
|
+
// 文件头写 coverage: active-only,正文就必须只有活跃行——说一套写一套比没有
|
|
173
|
+
// 这个字段更糟(外部工具照着聚合)。
|
|
174
|
+
assert.match(text, /^covered: 1$/m, "覆盖条数只数活跃行");
|
|
175
|
+
assert.ok(text.includes("keep-c"));
|
|
176
|
+
assert.ok(!text.includes("arch-c"), "归档行不进镜像");
|
|
177
|
+
assert.ok(!text.includes("gone-c"), "遗忘行不进镜像");
|
|
178
|
+
} finally {
|
|
179
|
+
store?.close();
|
|
180
|
+
rmSync(dir, { recursive: true, force: true });
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
test("#278: re-syncing unchanged content does not rewrite the file", () => {
|
|
185
|
+
const dir = tempDir();
|
|
186
|
+
try {
|
|
187
|
+
const mirror = createMirror(dir);
|
|
188
|
+
const rows = [memory("pitfall"), memory("constraint", { id: "m2" })];
|
|
189
|
+
mirror.sync(rows);
|
|
190
|
+
const first = readFileSync(join(dir, TYPE_FILE.pitfall), "utf8");
|
|
191
|
+
const firstAt = first.match(/^generated\.at: (.+)$/m)[1];
|
|
192
|
+
// generated.at 每次渲染都是新时间戳:无脑落盘会让编辑器、同步盘与 git 在每次
|
|
193
|
+
// 业务写后都看到全部镜像「被外部修改」,而内容一字未变。
|
|
194
|
+
mirror.sync(rows);
|
|
195
|
+
const second = readFileSync(join(dir, TYPE_FILE.pitfall), "utf8");
|
|
196
|
+
assert.equal(second, first, "内容未变的文件不得被重写");
|
|
197
|
+
assert.equal(second.match(/^generated\.at: (.+)$/m)[1], firstAt, "未变时保留原时间戳");
|
|
198
|
+
// 内容真变了必须落盘
|
|
199
|
+
mirror.sync([memory("pitfall", { content: "改了" })]);
|
|
200
|
+
const third = readFileSync(join(dir, TYPE_FILE.pitfall), "utf8");
|
|
201
|
+
assert.notEqual(third, second);
|
|
202
|
+
assert.ok(third.includes("改了"));
|
|
203
|
+
// 顺手确认「空 type 删文件」这条防复活行为没被上面的跳过逻辑带坏
|
|
204
|
+
assert.ok(!existsSync(join(dir, TYPE_FILE.constraint)), "该 type 没行时文件要删掉");
|
|
205
|
+
} finally {
|
|
206
|
+
rmSync(dir, { recursive: true, force: true });
|
|
207
|
+
}
|
|
208
|
+
});
|
|
@@ -233,3 +233,21 @@ test("production search never writes recall_evals, even with the switch on", asy
|
|
|
233
233
|
assert.deepEqual(store.listRecallEvals(), [], "recall_evals stays untouched by production search");
|
|
234
234
|
store.close();
|
|
235
235
|
});
|
|
236
|
+
|
|
237
|
+
// ---------------------------------------------------------------- per-source signals in receipt
|
|
238
|
+
|
|
239
|
+
test("recall receipt: candidates carry per-source signals for offline fusion-weight profiling", async () => {
|
|
240
|
+
const { store, service } = setup({});
|
|
241
|
+
const captured = [];
|
|
242
|
+
service.setRecallRecorder((recall) => captured.push(recall));
|
|
243
|
+
saveMemory(service, "量子计算入门", "叠加态");
|
|
244
|
+
const rows = await service.searchMemories("量子", { mode: "hybrid", recordRecall: true });
|
|
245
|
+
assert.ok(rows.length >= 1, "search returned results");
|
|
246
|
+
assert.equal(captured.length, 1, "exactly one receipt");
|
|
247
|
+
for (const c of captured[0].candidates) {
|
|
248
|
+
assert.ok(c.signals && typeof c.signals === "object", "every candidate carries a signals object");
|
|
249
|
+
assert.ok("keyword" in c.signals, "raw keyword score present (pre-fusion, unweighted)");
|
|
250
|
+
assert.ok(!("final" in c.signals), "raw signals only — the fused score already lives in c.score");
|
|
251
|
+
}
|
|
252
|
+
store.close();
|
|
253
|
+
});
|