@modusensus/dsh-mneme 0.8.14 → 0.8.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +655 -654
- package/assets/icon.svg +3 -3
- package/bin/cli.mjs +689 -689
- package/bin/dsh-mneme-mcp.mjs +510 -510
- package/bin/dsh-mneme-serve.mjs +151 -151
- package/cordis.patch.yml +21 -21
- package/docs/CONFIGURATION.md +263 -0
- package/docs/CONTINUITY.md +124 -0
- package/docs/DAEMON.md +46 -0
- package/docs/ENTITIES.md +245 -0
- package/docs/LOCAL_MODEL.md +222 -0
- package/docs/SEMANTIC.md +256 -0
- package/docs/SLEEP.md +164 -0
- package/docs/STORAGE.md +89 -0
- package/lib/api-standalone.js +673 -673
- package/lib/api.js +1437 -1433
- package/lib/bootstrap.js +211 -211
- package/lib/client.js +5444 -5295
- package/lib/commands.js +95 -95
- package/lib/config.js +880 -843
- package/lib/content-hash.js +54 -54
- package/lib/continuity.js +220 -220
- package/lib/document-index.js +94 -94
- package/lib/document.js +303 -303
- package/lib/dream/clustering.js +118 -118
- package/lib/dream/decisions.js +674 -674
- package/lib/dream/narratives.js +44 -44
- package/lib/dream/sleep.js +819 -819
- package/lib/dream.js +1501 -1501
- package/lib/embedding.js +177 -177
- package/lib/entities/extractor.js +252 -252
- package/lib/graph/anchoring.js +179 -179
- package/lib/guide.js +74 -74
- package/lib/heat.js +144 -144
- package/lib/hot-memory.js +46 -46
- package/lib/index.js +535 -503
- package/lib/inject.js +464 -450
- package/lib/lang.js +248 -229
- package/lib/local-embedder.js +358 -358
- package/lib/maintenance.js +142 -142
- package/lib/mirror.js +330 -330
- package/lib/organize.js +450 -450
- package/lib/peak-hours.js +117 -117
- package/lib/placement.js +13 -0
- package/lib/pre-inject-gate.js +340 -0
- package/lib/quality-filter.js +126 -126
- package/lib/recall-stats.js +175 -175
- package/lib/reranker.js +269 -269
- package/lib/runtime/adopt.js +176 -176
- package/lib/runtime/closure.js +172 -172
- package/lib/runtime/download.js +247 -247
- package/lib/runtime/layout.js +265 -265
- package/lib/runtime/loader.js +252 -252
- package/lib/runtime/model-fetch.js +361 -361
- package/lib/runtime/provision.js +237 -237
- package/lib/runtime/tarball.js +130 -130
- package/lib/runtime/verify.js +223 -204
- package/lib/scope.js +146 -146
- package/lib/search/adaptive.js +60 -60
- package/lib/search/bm25.js +96 -96
- package/lib/semantic.js +229 -229
- package/lib/sensitive-scan.js +209 -209
- package/lib/serve.js +215 -215
- package/lib/service.js +2663 -2605
- package/lib/settings.js +517 -511
- package/lib/store.js +3007 -3007
- package/lib/summarize.js +891 -856
- package/lib/tools.js +743 -737
- package/lib/vector-index.js +110 -110
- package/lib/version-check.js +122 -122
- package/lib/write-admission.js +402 -402
- package/locale/en.json +6 -6
- package/locale/zh.json +6 -6
- package/package.json +118 -118
- package/runtime-manifest.json +526 -526
- package/scripts/benchmark-embed.js +201 -201
- package/scripts/benchmark-recall.js +249 -249
- package/scripts/benchmark-rerank.js +166 -166
- package/scripts/build-runtime-manifest.mjs +224 -224
- package/scripts/changelog-prep.mjs +36 -36
- package/scripts/check-sync.js +42 -42
- package/scripts/e2e-dsh.js +220 -220
- package/scripts/mneme-runtime.mjs +277 -277
- package/scripts/stress-dsh.js +255 -255
- package/scripts/sync-lib.js +47 -47
- package/scripts/sync-test-badge.mjs +69 -69
- package/scripts/test-count-sync.mjs +44 -44
- package/src/api-standalone.js +673 -673
- package/src/api.js +1437 -1433
- package/src/bootstrap.js +211 -211
- package/src/commands.js +95 -95
- package/src/config.js +880 -843
- package/src/content-hash.js +54 -54
- package/src/continuity.js +220 -220
- package/src/document-index.js +94 -94
- package/src/document.js +303 -303
- package/src/dream/clustering.js +118 -118
- package/src/dream/decisions.js +674 -674
- package/src/dream/narratives.js +44 -44
- package/src/dream/sleep.js +819 -819
- package/src/dream.js +1501 -1501
- package/src/embedding.js +177 -177
- package/src/entities/extractor.js +252 -252
- package/src/graph/anchoring.js +179 -179
- package/src/guide.js +74 -74
- package/src/heat.js +144 -144
- package/src/hot-memory.js +46 -46
- package/src/index.js +535 -503
- package/src/inject.js +464 -450
- package/src/lang.js +248 -229
- package/src/local-embedder.js +358 -358
- package/src/maintenance.js +142 -142
- package/src/mirror.js +330 -330
- package/src/organize.js +450 -450
- package/src/peak-hours.js +117 -117
- package/src/placement.js +13 -0
- package/src/pre-inject-gate.js +340 -0
- package/src/quality-filter.js +126 -126
- package/src/recall-stats.js +175 -175
- package/src/reranker.js +269 -269
- package/src/runtime/adopt.js +176 -176
- package/src/runtime/closure.js +172 -172
- package/src/runtime/download.js +247 -247
- package/src/runtime/layout.js +265 -265
- package/src/runtime/loader.js +252 -252
- package/src/runtime/model-fetch.js +361 -361
- package/src/runtime/provision.js +237 -237
- package/src/runtime/tarball.js +130 -130
- package/src/runtime/verify.js +223 -204
- package/src/scope.js +146 -146
- package/src/search/adaptive.js +60 -60
- package/src/search/bm25.js +96 -96
- package/src/semantic.js +229 -229
- package/src/sensitive-scan.js +209 -209
- package/src/serve.js +215 -215
- package/src/service.js +2663 -2605
- package/src/settings.js +517 -511
- package/src/store.js +3007 -3007
- package/src/summarize.js +891 -856
- package/src/tools.js +743 -737
- package/src/vector-index.js +110 -110
- package/src/version-check.js +122 -122
- package/src/write-admission.js +402 -402
- package/test/adaptive-inject-budget.test.js +85 -85
- package/test/api-routes-gaps.test.js +218 -218
- package/test/api.test.js +1702 -1690
- package/test/audit.test.js +448 -448
- package/test/baseline-persist.test.js +248 -248
- package/test/benchmark.test.js +94 -94
- package/test/bootstrap.test.js +220 -220
- package/test/changelog-prep.test.js +60 -60
- package/test/client.test.js +1230 -1169
- package/test/clustering.test.js +100 -100
- package/test/commands.test.js +106 -106
- package/test/config.test.js +66 -66
- package/test/conflict-freeze.test.js +300 -300
- package/test/content-hash.test.js +204 -204
- package/test/continuity.test.js +333 -333
- package/test/document-dir.test.js +233 -233
- package/test/document.test.js +671 -671
- package/test/dream-archive-guardrail.test.js +116 -116
- package/test/dream-candidate-hybrid.test.js +281 -281
- package/test/dream-coverage-degrade.test.js +86 -86
- package/test/dream-failure-backoff.test.js +173 -173
- package/test/dream-peak-hours.test.js +194 -194
- package/test/dream.test.js +1385 -1383
- package/test/embedding-ready.test.js +196 -196
- package/test/entities.test.js +627 -547
- package/test/entity-recall.test.js +179 -179
- package/test/epistemic.test.js +325 -325
- package/test/fnew-0112.test.js +311 -311
- package/test/fnew-03.test.js +422 -422
- package/test/graph-anchoring.test.js +184 -184
- package/test/graph-api.test.js +175 -175
- package/test/graph-inject-budget.test.js +103 -103
- package/test/graph-passive-confirm.test.js +91 -91
- package/test/graph-weight-evolution.test.js +104 -104
- package/test/heat.test.js +175 -175
- package/test/helpers/dream-mock.js +82 -82
- package/test/helpers/peer-worker.mjs +25 -25
- package/test/helpers/runtime-source.js +60 -60
- package/test/helpers/tar-builder.js +80 -80
- package/test/helpers/write-admission-samples.js +308 -308
- package/test/hot-memory.test.js +145 -145
- package/test/inject-content-cap.test.js +91 -91
- package/test/inject-first-round-bm25.test.js +89 -89
- package/test/inject-heat.test.js +77 -77
- package/test/inject-parent-gate.test.js +106 -106
- package/test/inject-pools.test.js +194 -194
- package/test/inject-recall.test.js +105 -105
- package/test/inject-rotation.test.js +161 -161
- package/test/inject.test.js +420 -420
- package/test/lang.test.js +87 -87
- package/test/lastrunat-persist.test.js +101 -101
- package/test/lib-smoke.test.js +96 -96
- package/test/llm-audit.test.js +598 -598
- package/test/llm-message-source.test.js +207 -207
- package/test/local-embedder.test.js +292 -292
- package/test/maintenance.test.js +391 -391
- package/test/mcp-stdio.test.js +385 -385
- package/test/mirror-dirty.test.js +424 -424
- package/test/mirror-edit-digest.test.js +187 -187
- package/test/mirror-generation.test.js +499 -499
- package/test/mirror-types.test.js +208 -208
- package/test/mirror.test.js +249 -249
- package/test/narrative-bars.test.js +162 -162
- package/test/organize.test.js +418 -418
- package/test/peer-blockers.test.js +193 -193
- package/test/placement.test.js +36 -0
- package/test/policy-epoch.test.js +259 -259
- package/test/pre-inject-gate.test.js +434 -0
- package/test/quality-filter.test.js +186 -186
- package/test/reasoning-effort.test.js +752 -752
- package/test/recall-anti-update.test.js +82 -82
- package/test/recall-evals.test.js +253 -253
- package/test/recall-layer.test.js +326 -326
- package/test/recall-stats.test.js +189 -189
- package/test/receipt-chain.test.js +451 -451
- package/test/reflection.test.js +226 -226
- package/test/reindex-backfill.test.js +62 -62
- package/test/reranker-single-logit.test.js +73 -73
- package/test/reranker.test.js +197 -197
- package/test/resident-status.test.js +83 -83
- package/test/runtime-adopt.test.js +206 -206
- package/test/runtime-cli.test.js +89 -89
- package/test/runtime-closure.test.js +193 -193
- package/test/runtime-download.test.js +302 -302
- package/test/runtime-layout.test.js +163 -163
- package/test/runtime-loader.test.js +255 -255
- package/test/runtime-manifest.test.js +159 -159
- package/test/runtime-model-fetch.test.js +348 -348
- package/test/runtime-provision.test.js +101 -101
- package/test/runtime-tarball.test.js +81 -81
- package/test/runtime-verify.test.js +319 -279
- package/test/scope-candidates.test.js +239 -239
- package/test/scope-explicit.test.js +452 -452
- package/test/scope-inject-soft.test.js +106 -106
- package/test/scope-retrieval.test.js +197 -197
- package/test/scope-storage.test.js +255 -255
- package/test/scope-strict.test.js +367 -367
- package/test/search-fusion.test.js +90 -90
- package/test/semantic.test.js +124 -124
- package/test/sensitive-scan.test.js +207 -207
- package/test/serve-bin.test.js +121 -121
- package/test/serve.test.js +196 -196
- package/test/service-search.test.js +174 -174
- package/test/service.test.js +405 -405
- package/test/settings.test.js +261 -261
- package/test/skip-invalid-restore.test.js +262 -262
- package/test/sleep-action-set.test.js +422 -422
- package/test/sleep-heat.test.js +126 -126
- package/test/sleep-scheduler-rearm.test.js +89 -89
- package/test/sleep.test.js +527 -527
- package/test/standalone-api.test.js +751 -751
- package/test/store-vector-cache.test.js +98 -98
- package/test/store.test.js +396 -396
- package/test/stress.test.js +209 -209
- package/test/summarize-occurred-at.test.js +127 -0
- package/test/summarize-reasoning-effort.test.js +189 -189
- package/test/summarize.test.js +1465 -1465
- package/test/supersede-state-tracking.test.js +394 -0
- package/test/test-count-sync.test.js +60 -60
- package/test/tools-dto-schema-matrix.test.js +325 -325
- package/test/tools-memory-get-schema.test.js +65 -65
- package/test/tools.test.js +428 -374
- package/test/updated-at-semantics.test.js +113 -113
- package/test/vector-index.test.js +221 -221
- package/test/version-check.test.js +152 -152
- package/test/webServer-optional.test.js +82 -82
- package/test/write-admission.test.js +668 -668
package/test/maintenance.test.js
CHANGED
|
@@ -1,391 +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
|
-
});
|
|
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
|
+
});
|