@modusensus/dsh-mneme 0.8.6 → 0.8.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/README.md +19 -148
- package/bin/cli.mjs +689 -603
- package/lib/api-standalone.js +57 -1
- package/lib/api.js +34 -11
- package/lib/client.js +349 -61
- package/lib/config.js +118 -7
- package/lib/content-hash.js +54 -0
- package/lib/continuity.js +220 -0
- package/lib/document-index.js +94 -0
- package/lib/document.js +93 -11
- package/lib/dream.js +138 -39
- package/lib/guide.js +18 -0
- package/lib/index.js +87 -12
- 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/peak-hours.js +117 -0
- package/lib/recall-stats.js +52 -2
- package/lib/scope.js +34 -6
- package/lib/service.js +86 -7
- package/lib/settings.js +16 -2
- package/lib/store.js +318 -21
- package/lib/summarize.js +52 -113
- package/lib/tools.js +37 -11
- 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 +118 -7
- package/src/content-hash.js +54 -0
- package/src/continuity.js +220 -0
- package/src/document-index.js +94 -0
- package/src/document.js +93 -11
- package/src/dream.js +138 -39
- package/src/guide.js +18 -0
- package/src/index.js +87 -12
- 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/peak-hours.js +117 -0
- package/src/recall-stats.js +52 -2
- package/src/scope.js +34 -6
- package/src/service.js +86 -7
- package/src/settings.js +16 -2
- package/src/store.js +318 -21
- package/src/summarize.js +52 -113
- package/src/tools.js +37 -11
- package/src/write-admission.js +259 -0
- package/test/api.test.js +21 -3
- package/test/client.test.js +103 -6
- package/test/content-hash.test.js +204 -0
- package/test/continuity.test.js +333 -0
- package/test/document-dir.test.js +233 -0
- package/test/document.test.js +151 -3
- package/test/dream-failure-backoff.test.js +173 -0
- package/test/dream-peak-hours.test.js +194 -0
- 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/reasoning-effort.test.js +6 -0
- package/test/recall-evals.test.js +18 -0
- package/test/recall-stats.test.js +66 -0
- package/test/summarize-reasoning-effort.test.js +189 -0
- package/test/write-admission.test.js +345 -0
|
@@ -12,7 +12,9 @@ import { MEMORY_GUIDE_SECTION } from "../src/guide.js";
|
|
|
12
12
|
// 总则段(常驻段必须同会话稳定,不逐轮复读);
|
|
13
13
|
// ②B1 pin 池(pinnedInjectBudget):约束/偏好不进相关性竞争与跨轮轮换、逐字
|
|
14
14
|
// 保真、排在块内排序之前,且有独立小预算(不占 maxInjectedItems 名额)。
|
|
15
|
-
//
|
|
15
|
+
// 本文件给的是裸 config,两个键缺省时都按关/零算:能力说明的解析默认值由 config.js
|
|
16
|
+
// 的 schema 给出(#249 第二批起为开,另见 test/inject-parent-gate.test.js),pin 池
|
|
17
|
+
// 的默认值是 0。
|
|
16
18
|
|
|
17
19
|
const userMsg = (text) => ({
|
|
18
20
|
type: "user/message",
|
|
@@ -122,9 +124,12 @@ test("#249: pinned content does not eat the general block budget", () => {
|
|
|
122
124
|
assert.ok(body.includes("总览正文必须带出来"), "普通候选照旧拿完整正文:pin 不占共享预算");
|
|
123
125
|
});
|
|
124
126
|
|
|
125
|
-
test("#249: capability guide
|
|
127
|
+
test("#249: capability guide registers exactly once, and stays off when the key is absent", () => {
|
|
128
|
+
// 这里给的是裸 config(不经 schema 解析),所以「缺键 = 不生效」;生产默认值
|
|
129
|
+
// 由 config.js 的 schema 给出(#249 第二批起为 true),那条锁在
|
|
130
|
+
// test/inject-parent-gate.test.js。
|
|
126
131
|
const off = setup({});
|
|
127
|
-
assert.equal(off.sections.length, 0, "
|
|
132
|
+
assert.equal(off.sections.length, 0, "缺键 → 不注册提示段");
|
|
128
133
|
|
|
129
134
|
const on = setup({ injectGuidanceEnabled: true });
|
|
130
135
|
assert.equal(on.sections.length, 1, "exactly one guide section");
|
package/test/llm-audit.test.js
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import test from "node:test";
|
|
2
2
|
import assert from "node:assert/strict";
|
|
3
3
|
import { EventEmitter } from "node:events";
|
|
4
|
+
import { DatabaseSync } from "node:sqlite";
|
|
5
|
+
import { mkdtempSync } from "node:fs";
|
|
6
|
+
import { tmpdir } from "node:os";
|
|
7
|
+
import { join } from "node:path";
|
|
8
|
+
import { Context } from "@deepseek-ai/cordis";
|
|
4
9
|
import { createStore } from "../src/store.js";
|
|
10
|
+
import * as mneme from "../src/index.js";
|
|
5
11
|
import { createService } from "../src/service.js";
|
|
6
12
|
import { createDreamScheduler } from "../src/dream.js";
|
|
7
13
|
import { createSummarizer } from "../src/summarize.js";
|
|
@@ -554,3 +560,39 @@ test("entity extraction writes no audit row when llmAudit.enabled === false", as
|
|
|
554
560
|
assert.equal(service.listLlmAudits().length, 0, "no audit rows when disabled");
|
|
555
561
|
store.close();
|
|
556
562
|
});
|
|
563
|
+
|
|
564
|
+
// 启动期保留清理(Bug8)必须与写入侧读**同一份**配置。面板把 llmAudit.enabled 写进
|
|
565
|
+
// kv、经 nestedFlags 合进 cfg,bundle 里可能是关的:清理若读 rawCfg,就会出现「面板
|
|
566
|
+
// 说开 → 各写入点照写,raw 说关 → 不清理」这种无保留期的增长(写入准入 #254 也是读
|
|
567
|
+
// cfg 的写入点之一)。所以这条锁的是「清理跟着装配后的 cfg 走」。
|
|
568
|
+
test("boot retention purge reads the assembled config, not rawCfg", async () => {
|
|
569
|
+
const dir = mkdtempSync(join(tmpdir(), "mneme-audit-retention-"));
|
|
570
|
+
const dbPath = join(dir, "memory.db");
|
|
571
|
+
|
|
572
|
+
const seed = createStore(dbPath);
|
|
573
|
+
createSettings(seed.db).setFeatureFlags({ "llmAudit.enabled": true });
|
|
574
|
+
seed.saveLlmAudit({
|
|
575
|
+
timestamp: new Date(Date.now() - 2 * 86400000).toISOString(),
|
|
576
|
+
trigger_source: "autoDream",
|
|
577
|
+
operation_type: "dream_consolidate",
|
|
578
|
+
model_id: "m",
|
|
579
|
+
status: "success"
|
|
580
|
+
});
|
|
581
|
+
assert.equal(seed.listLlmAudits().length, 1, "先放一行过期审计");
|
|
582
|
+
seed.close();
|
|
583
|
+
|
|
584
|
+
const ctx = new Context();
|
|
585
|
+
ctx.provide("tools", { register() { return () => {}; } });
|
|
586
|
+
ctx.provide("commands", { register() { return () => {}; } });
|
|
587
|
+
ctx.provide("systemPrompt", { context() { return () => {}; } });
|
|
588
|
+
ctx.provide("agentDefaultModel", { currentSelection() { return { provider: "mock", model: "mock" }; } });
|
|
589
|
+
ctx.provide("llm", { async *stream() { yield { type: "finish", reason: { kind: "stop" } }; } });
|
|
590
|
+
ctx.provide("webServer", { register() { return () => {}; } });
|
|
591
|
+
// bundle 说关、面板说开,保留期 1 天而那行已过期 2 天:只有读装配后的 cfg 才会清掉它
|
|
592
|
+
await ctx.plugin(mneme, { memoryDir: dir, llmAudit: { enabled: false, retentionDays: 1 } });
|
|
593
|
+
|
|
594
|
+
const after = new DatabaseSync(dbPath);
|
|
595
|
+
const left = after.prepare("SELECT count(*) AS c FROM llm_audit_logs").get().c;
|
|
596
|
+
after.close();
|
|
597
|
+
assert.equal(left, 0, "面板开着的审计必须按 cfg 清理,否则审计行只增不减");
|
|
598
|
+
});
|
|
@@ -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
|
+
});
|