@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
|
@@ -1,233 +1,233 @@
|
|
|
1
|
-
import test from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
import { existsSync, mkdtempSync, mkdirSync, readFileSync, readdirSync, rmSync, writeFileSync, unlinkSync } from "node:fs";
|
|
4
|
-
import { createHash } from "node:crypto";
|
|
5
|
-
import { homedir, tmpdir } from "node:os";
|
|
6
|
-
import { join, resolve } from "node:path";
|
|
7
|
-
import { createDocumentIndex } from "../src/document-index.js";
|
|
8
|
-
import { isManagedDocumentPath, resolveDocumentDir } from "../src/document.js";
|
|
9
|
-
import { createMirror } from "../src/mirror.js";
|
|
10
|
-
import { createService } from "../src/service.js";
|
|
11
|
-
import { createStore } from "../src/store.js";
|
|
12
|
-
|
|
13
|
-
// 回归(issue #296 第二批):document 的落盘目录与归属。
|
|
14
|
-
// ①`documentDir` 默认跟随 memoryDir 的 `<memoryDir>/documents/`,`~` 与相对路径
|
|
15
|
-
// 按 memoryDir 同一套解析;②managed / external 的边界(目录内的文件是机器产物
|
|
16
|
-
// 的一部分,目录外只登记指针行);③`index.md` 整文件机器所有、可从库重建;
|
|
17
|
-
// ④镜像侧 documents.md 是只读指针视图(另见 test/mirror-types.test.js)。
|
|
18
|
-
|
|
19
|
-
function tempDir() {
|
|
20
|
-
return mkdtempSync(join(tmpdir(), "dsh-mneme-docdir-"));
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
test("#296: documentDir 默认为 memoryDir 下的 documents,三种路径写法都可解析", () => {
|
|
24
|
-
const memoryDir = join(tmpdir(), "dsh-mneme-memory");
|
|
25
|
-
assert.equal(resolveDocumentDir(memoryDir, ""), resolve(memoryDir, "documents"), "空 = 跟随 memoryDir");
|
|
26
|
-
assert.equal(resolveDocumentDir(memoryDir), resolve(memoryDir, "documents"), "缺省同空串");
|
|
27
|
-
assert.equal(resolveDocumentDir(memoryDir, "./notes"), resolve(memoryDir, "notes"), "相对路径落在 memoryDir 下");
|
|
28
|
-
assert.equal(resolveDocumentDir(memoryDir, "docs"), resolve(memoryDir, "docs"));
|
|
29
|
-
assert.equal(resolveDocumentDir(memoryDir, join(tmpdir(), "elsewhere")), resolve(join(tmpdir(), "elsewhere")), "绝对路径原样用");
|
|
30
|
-
assert.equal(resolveDocumentDir(memoryDir, "~/docs"), resolve(homedir(), "docs"), "~ 展开到 home");
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
test("#296: managed 判定只认目录内的路径(前缀相像的不算)", () => {
|
|
34
|
-
const base = join(tmpdir(), "dsh-mneme-docroot");
|
|
35
|
-
const dir = join(base, "documents");
|
|
36
|
-
assert.equal(isManagedDocumentPath(dir, join(dir, "report.md")), true);
|
|
37
|
-
assert.equal(isManagedDocumentPath(dir, join(dir, "sub", "deep.md")), true, "子目录也算 managed");
|
|
38
|
-
assert.equal(isManagedDocumentPath(dir, dir), true);
|
|
39
|
-
assert.equal(isManagedDocumentPath(dir, join(base, "documents-old", "x.md")), false, "前缀相像的兄弟目录不是 managed");
|
|
40
|
-
assert.equal(isManagedDocumentPath(dir, join(base, "other.md")), false);
|
|
41
|
-
assert.equal(isManagedDocumentPath(dir, ""), false);
|
|
42
|
-
assert.equal(isManagedDocumentPath(dir, undefined), false);
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
test("#296: index.md 只列指针行、可整文件重建、内容不变不落盘", () => {
|
|
46
|
-
const dir = tempDir();
|
|
47
|
-
try {
|
|
48
|
-
const index = createDocumentIndex(join(dir, "documents"));
|
|
49
|
-
const managed = join(dir, "documents", "report.md");
|
|
50
|
-
const rows = [
|
|
51
|
-
{ id: "d1", title: "报告", content: "第一句。第二句。", doc_path: managed, updated_at: "2026-02-01T00:00:00.000Z" },
|
|
52
|
-
{ id: "d2", title: "外部文档", content: "正文不必进索引。", doc_path: join(dir, "notes.md"), updated_at: "2026-01-01T00:00:00.000Z" }
|
|
53
|
-
];
|
|
54
|
-
assert.equal(index.sync(rows).changed, true);
|
|
55
|
-
const first = readFileSync(join(dir, "documents", "index.md"), "utf8");
|
|
56
|
-
assert.ok(first.includes("整文件机器所有"), "文件头说清归属");
|
|
57
|
-
assert.equal((first.match(/^- `/gm) ?? []).length, 2, "一行一个 document");
|
|
58
|
-
assert.ok(first.includes("d1") && first.includes("报告") && first.includes(managed), "带定位");
|
|
59
|
-
assert.ok(/\bd1\b.*managed/.test(first), "目录内的标 managed");
|
|
60
|
-
assert.ok(/\bd2\b.*external/.test(first), "目录外的标 external");
|
|
61
|
-
assert.ok(!first.includes("第一句"), "索引不带摘要正文(那是镜像视图的活)");
|
|
62
|
-
// 重建:删掉再同步得到逐字节相同的文件(所以文件里不写生成时间)
|
|
63
|
-
unlinkSync(index.filePath);
|
|
64
|
-
index.sync(rows);
|
|
65
|
-
assert.equal(readFileSync(index.filePath, "utf8"), first, "可从库重建,且逐字节一致");
|
|
66
|
-
// 内容没变就不重写(与镜像同一条「不无谓写盘」纪律)
|
|
67
|
-
assert.equal(index.sync(rows).changed, false);
|
|
68
|
-
// 空集也要有一份合法的索引(不是删文件)
|
|
69
|
-
assert.equal(index.sync([]).changed, true);
|
|
70
|
-
assert.ok(readFileSync(index.filePath, "utf8").includes("机器所有"));
|
|
71
|
-
} finally {
|
|
72
|
-
rmSync(dir, { recursive: true, force: true });
|
|
73
|
-
}
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
test("#296: 索引写不进去只报失败,不抛(写失败不能连累业务写)", () => {
|
|
77
|
-
const dir = tempDir();
|
|
78
|
-
try {
|
|
79
|
-
// 让目标目录的「父级」是一个普通文件:mkdir 必失败,且不依赖平台错误码
|
|
80
|
-
const blocker = join(dir, "blocker");
|
|
81
|
-
writeFileSync(blocker, "not a directory", "utf8");
|
|
82
|
-
const index = createDocumentIndex(join(blocker, "documents"));
|
|
83
|
-
const ensured = index.ensure();
|
|
84
|
-
assert.equal(ensured.ok, false, "建目录失败要如实返回(调用方据此 warn)");
|
|
85
|
-
assert.ok(ensured.error, "失败要带原因");
|
|
86
|
-
const result = index.sync([{ id: "d1", title: "t", content: "c", doc_path: "x", updated_at: "2026-01-01T00:00:00.000Z" }]);
|
|
87
|
-
assert.equal(result.ok, false, "失败要如实返回,不能假装成功");
|
|
88
|
-
assert.ok(result.error, "失败要带原因");
|
|
89
|
-
assert.ok(!existsSync(index.filePath));
|
|
90
|
-
} finally {
|
|
91
|
-
rmSync(dir, { recursive: true, force: true });
|
|
92
|
-
}
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
test("#296: index.md 删掉后从库里重建,逐字节一致", () => {
|
|
96
|
-
const dir = tempDir();
|
|
97
|
-
let store;
|
|
98
|
-
try {
|
|
99
|
-
const documentDir = join(dir, "documents");
|
|
100
|
-
store = createStore(join(dir, "memory.db"));
|
|
101
|
-
const index = createDocumentIndex(documentDir);
|
|
102
|
-
const service = createService({
|
|
103
|
-
store, mirror: createMirror(dir), config: { documentMemoryEnabled: true }, documentIndex: index
|
|
104
|
-
});
|
|
105
|
-
writeFileSync(join(dir, "report.md"), "x", "utf8");
|
|
106
|
-
const doc = store.saveDocument({
|
|
107
|
-
type: "document", title: "报告", content: "第一句。第二句。", tags: [], importance: 3,
|
|
108
|
-
source: "tool", evidence: [], doc_path: join(documentDir, "report.md")
|
|
109
|
-
});
|
|
110
|
-
service.saveWithDedupe({ type: "pattern", title: "触发", content: "x" });
|
|
111
|
-
const first = readFileSync(index.filePath, "utf8");
|
|
112
|
-
// 真删文件,再从库同步一次:读数必须一样(这才是「可从库重建」,不是拿同一份
|
|
113
|
-
// 内存数组重算)
|
|
114
|
-
unlinkSync(index.filePath);
|
|
115
|
-
assert.ok(!existsSync(index.filePath));
|
|
116
|
-
service.saveWithDedupe({ type: "pattern", title: "触发2", content: "y" });
|
|
117
|
-
assert.equal(readFileSync(index.filePath, "utf8"), first, "删掉重建后逐字节一致");
|
|
118
|
-
assert.ok(first.includes(doc.id) && first.includes("managed"));
|
|
119
|
-
} finally {
|
|
120
|
-
store?.close();
|
|
121
|
-
rmSync(dir, { recursive: true, force: true });
|
|
122
|
-
}
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
test("#296: managed 文件走一次完整注册往返,字节哈希不变", async () => {
|
|
126
|
-
const dir = tempDir();
|
|
127
|
-
let store;
|
|
128
|
-
try {
|
|
129
|
-
const documentDir = join(dir, "documents");
|
|
130
|
-
mkdirSync(documentDir, { recursive: true });
|
|
131
|
-
const docFile = join(documentDir, "report.md");
|
|
132
|
-
writeFileSync(docFile, "# 报告\n\n正文由 agent 写。\n", "utf8");
|
|
133
|
-
const before = createHash("sha256").update(readFileSync(docFile)).digest("hex");
|
|
134
|
-
store = createStore(join(dir, "memory.db"));
|
|
135
|
-
const service = createService({
|
|
136
|
-
store, mirror: createMirror(dir), config: { documentMemoryEnabled: true },
|
|
137
|
-
documentIndex: createDocumentIndex(documentDir)
|
|
138
|
-
});
|
|
139
|
-
const result = await service.registerDocument({ path: docFile, title: "报告", summary: "第一句。第二句。" });
|
|
140
|
-
assert.equal(result.action, "created");
|
|
141
|
-
assert.equal(result.memory.doc_path, resolve(docFile), "指针行落在 managed 路径上");
|
|
142
|
-
assert.equal(
|
|
143
|
-
createHash("sha256").update(readFileSync(docFile)).digest("hex"), before,
|
|
144
|
-
"管线对正文零读零写:注册往返后 managed 文件必须逐字节不变"
|
|
145
|
-
);
|
|
146
|
-
// 同时也确认没有偷偷在 documentDir 里生成第二份「正文」(机器产物只有 index.md)
|
|
147
|
-
assert.deepEqual(readdirSync(documentDir).sort(), ["index.md", "report.md"]);
|
|
148
|
-
} finally {
|
|
149
|
-
store?.close();
|
|
150
|
-
rmSync(dir, { recursive: true, force: true });
|
|
151
|
-
}
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
test("#296: 两个落点共用 documentMemoryEnabled 闸,闸关时视图与索引一起清掉", () => {
|
|
155
|
-
const dir = tempDir();
|
|
156
|
-
let store;
|
|
157
|
-
try {
|
|
158
|
-
const documentDir = join(dir, "documents");
|
|
159
|
-
store = createStore(join(dir, "memory.db"));
|
|
160
|
-
const index = createDocumentIndex(documentDir);
|
|
161
|
-
const on = createService({
|
|
162
|
-
store, mirror: createMirror(dir), config: { documentMemoryEnabled: true }, documentIndex: index
|
|
163
|
-
});
|
|
164
|
-
writeFileSync(join(dir, "outside.md"), "x", "utf8");
|
|
165
|
-
const kept = store.saveDocument({
|
|
166
|
-
type: "document", title: "报告", content: "第一句。第二句。", tags: [], importance: 3,
|
|
167
|
-
source: "tool", evidence: [], doc_path: join(documentDir, "report.md")
|
|
168
|
-
});
|
|
169
|
-
store.saveDocument({
|
|
170
|
-
type: "document", title: "外部", content: "另一份。", tags: [], importance: 3,
|
|
171
|
-
source: "tool", evidence: [], doc_path: join(dir, "outside.md")
|
|
172
|
-
});
|
|
173
|
-
// 任意一次业务写触发写后语(镜像 + 索引一起)
|
|
174
|
-
on.saveWithDedupe({ type: "pattern", title: "触发", content: "x" });
|
|
175
|
-
assert.ok(existsSync(join(dir, "documents.md")), "闸开 → 镜像视图落盘");
|
|
176
|
-
const md = readFileSync(join(dir, "documents.md"), "utf8");
|
|
177
|
-
assert.ok(md.includes("报告") && md.includes(join(documentDir, "report.md")));
|
|
178
|
-
assert.ok(existsSync(index.filePath), "闸开 → 索引落盘");
|
|
179
|
-
assert.ok(readFileSync(index.filePath, "utf8").includes("managed"));
|
|
180
|
-
|
|
181
|
-
// 归档一行:索引只列活跃行(与镜像的 coverage: active-only 同口径)
|
|
182
|
-
store.setArchived(kept.id, true);
|
|
183
|
-
on.saveWithDedupe({ type: "pattern", title: "触发1b", content: "w" });
|
|
184
|
-
assert.ok(!readFileSync(index.filePath, "utf8").includes(kept.id), "已归档的行不进索引");
|
|
185
|
-
|
|
186
|
-
// 闸关:document 行不再进渲染集 → documents.md 被既有「空 type 删文件」清掉;
|
|
187
|
-
// index.md 归 syncDocumentIndex 清掉——留一份陈旧索引就是「看着还在、其实已关」
|
|
188
|
-
// 的视图,正是 documents.md 在闸关时要避免的那一类。
|
|
189
|
-
const off = createService({ store, mirror: createMirror(dir), config: {}, documentIndex: index });
|
|
190
|
-
off.saveWithDedupe({ type: "pattern", title: "触发2", content: "y" });
|
|
191
|
-
assert.ok(!existsSync(join(dir, "documents.md")), "闸关 → 镜像视图清掉");
|
|
192
|
-
assert.ok(!existsSync(index.filePath), "闸关 → 索引也清掉");
|
|
193
|
-
|
|
194
|
-
// 闸重开:下一次业务写从库重建索引(自愈,不需要手工删文件)
|
|
195
|
-
const back = createService({
|
|
196
|
-
store, mirror: createMirror(dir), config: { documentMemoryEnabled: true }, documentIndex: index
|
|
197
|
-
});
|
|
198
|
-
back.saveWithDedupe({ type: "pattern", title: "触发3", content: "z" });
|
|
199
|
-
assert.ok(readFileSync(index.filePath, "utf8").includes("外部"), "闸重开 → 索引从库重建");
|
|
200
|
-
} finally {
|
|
201
|
-
store?.close();
|
|
202
|
-
rmSync(dir, { recursive: true, force: true });
|
|
203
|
-
}
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
test("#296: 两条新文案的英文分支也要能渲染(en 实例)", () => {
|
|
207
|
-
const dir = tempDir();
|
|
208
|
-
try {
|
|
209
|
-
// 新文案是 per-language 的 lambda:en 分支此前零调用点,缺键或笔误只在
|
|
210
|
-
// memory.language=en 的实例上炸,而全量测试照样绿(审查指出)。
|
|
211
|
-
const index = createDocumentIndex(join(dir, "documents"), "en");
|
|
212
|
-
index.sync([{
|
|
213
|
-
id: "d1", title: "Report", content: "First sentence. Second.", updated_at: "2026-01-01T00:00:00.000Z",
|
|
214
|
-
doc_path: join(dir, "documents", "report.md")
|
|
215
|
-
}]);
|
|
216
|
-
const indexText = readFileSync(index.filePath, "utf8");
|
|
217
|
-
assert.match(indexText, /document index — dsh-mneme/, "en 的索引头");
|
|
218
|
-
assert.match(indexText, /Machine-owned as a whole/, "en 的归属说明");
|
|
219
|
-
|
|
220
|
-
const mirror = createMirror(dir, "en");
|
|
221
|
-
mirror.sync([{
|
|
222
|
-
id: "doc1", type: "document", title: "Report", content: "First sentence. Second.", tags: [],
|
|
223
|
-
importance: 3, forgotten: false, created_at: "2026-01-01T00:00:00.000Z",
|
|
224
|
-
updated_at: "2026-01-01T00:00:00.000Z", doc_path: join(dir, "documents", "report.md")
|
|
225
|
-
}]);
|
|
226
|
-
const docs = readFileSync(join(dir, "documents.md"), "utf8");
|
|
227
|
-
assert.match(docs, /read-only view/, "en 的只读视图文件头");
|
|
228
|
-
assert.match(docs, /never the full text/, "en 的只读说明");
|
|
229
|
-
assert.ok(docs.includes("First sentence") && !docs.includes("Second."), "en 下同样只留摘要首句");
|
|
230
|
-
} finally {
|
|
231
|
-
rmSync(dir, { recursive: true, force: true });
|
|
232
|
-
}
|
|
233
|
-
});
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { existsSync, mkdtempSync, mkdirSync, readFileSync, readdirSync, rmSync, writeFileSync, unlinkSync } from "node:fs";
|
|
4
|
+
import { createHash } from "node:crypto";
|
|
5
|
+
import { homedir, tmpdir } from "node:os";
|
|
6
|
+
import { join, resolve } from "node:path";
|
|
7
|
+
import { createDocumentIndex } from "../src/document-index.js";
|
|
8
|
+
import { isManagedDocumentPath, resolveDocumentDir } from "../src/document.js";
|
|
9
|
+
import { createMirror } from "../src/mirror.js";
|
|
10
|
+
import { createService } from "../src/service.js";
|
|
11
|
+
import { createStore } from "../src/store.js";
|
|
12
|
+
|
|
13
|
+
// 回归(issue #296 第二批):document 的落盘目录与归属。
|
|
14
|
+
// ①`documentDir` 默认跟随 memoryDir 的 `<memoryDir>/documents/`,`~` 与相对路径
|
|
15
|
+
// 按 memoryDir 同一套解析;②managed / external 的边界(目录内的文件是机器产物
|
|
16
|
+
// 的一部分,目录外只登记指针行);③`index.md` 整文件机器所有、可从库重建;
|
|
17
|
+
// ④镜像侧 documents.md 是只读指针视图(另见 test/mirror-types.test.js)。
|
|
18
|
+
|
|
19
|
+
function tempDir() {
|
|
20
|
+
return mkdtempSync(join(tmpdir(), "dsh-mneme-docdir-"));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
test("#296: documentDir 默认为 memoryDir 下的 documents,三种路径写法都可解析", () => {
|
|
24
|
+
const memoryDir = join(tmpdir(), "dsh-mneme-memory");
|
|
25
|
+
assert.equal(resolveDocumentDir(memoryDir, ""), resolve(memoryDir, "documents"), "空 = 跟随 memoryDir");
|
|
26
|
+
assert.equal(resolveDocumentDir(memoryDir), resolve(memoryDir, "documents"), "缺省同空串");
|
|
27
|
+
assert.equal(resolveDocumentDir(memoryDir, "./notes"), resolve(memoryDir, "notes"), "相对路径落在 memoryDir 下");
|
|
28
|
+
assert.equal(resolveDocumentDir(memoryDir, "docs"), resolve(memoryDir, "docs"));
|
|
29
|
+
assert.equal(resolveDocumentDir(memoryDir, join(tmpdir(), "elsewhere")), resolve(join(tmpdir(), "elsewhere")), "绝对路径原样用");
|
|
30
|
+
assert.equal(resolveDocumentDir(memoryDir, "~/docs"), resolve(homedir(), "docs"), "~ 展开到 home");
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test("#296: managed 判定只认目录内的路径(前缀相像的不算)", () => {
|
|
34
|
+
const base = join(tmpdir(), "dsh-mneme-docroot");
|
|
35
|
+
const dir = join(base, "documents");
|
|
36
|
+
assert.equal(isManagedDocumentPath(dir, join(dir, "report.md")), true);
|
|
37
|
+
assert.equal(isManagedDocumentPath(dir, join(dir, "sub", "deep.md")), true, "子目录也算 managed");
|
|
38
|
+
assert.equal(isManagedDocumentPath(dir, dir), true);
|
|
39
|
+
assert.equal(isManagedDocumentPath(dir, join(base, "documents-old", "x.md")), false, "前缀相像的兄弟目录不是 managed");
|
|
40
|
+
assert.equal(isManagedDocumentPath(dir, join(base, "other.md")), false);
|
|
41
|
+
assert.equal(isManagedDocumentPath(dir, ""), false);
|
|
42
|
+
assert.equal(isManagedDocumentPath(dir, undefined), false);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test("#296: index.md 只列指针行、可整文件重建、内容不变不落盘", () => {
|
|
46
|
+
const dir = tempDir();
|
|
47
|
+
try {
|
|
48
|
+
const index = createDocumentIndex(join(dir, "documents"));
|
|
49
|
+
const managed = join(dir, "documents", "report.md");
|
|
50
|
+
const rows = [
|
|
51
|
+
{ id: "d1", title: "报告", content: "第一句。第二句。", doc_path: managed, updated_at: "2026-02-01T00:00:00.000Z" },
|
|
52
|
+
{ id: "d2", title: "外部文档", content: "正文不必进索引。", doc_path: join(dir, "notes.md"), updated_at: "2026-01-01T00:00:00.000Z" }
|
|
53
|
+
];
|
|
54
|
+
assert.equal(index.sync(rows).changed, true);
|
|
55
|
+
const first = readFileSync(join(dir, "documents", "index.md"), "utf8");
|
|
56
|
+
assert.ok(first.includes("整文件机器所有"), "文件头说清归属");
|
|
57
|
+
assert.equal((first.match(/^- `/gm) ?? []).length, 2, "一行一个 document");
|
|
58
|
+
assert.ok(first.includes("d1") && first.includes("报告") && first.includes(managed), "带定位");
|
|
59
|
+
assert.ok(/\bd1\b.*managed/.test(first), "目录内的标 managed");
|
|
60
|
+
assert.ok(/\bd2\b.*external/.test(first), "目录外的标 external");
|
|
61
|
+
assert.ok(!first.includes("第一句"), "索引不带摘要正文(那是镜像视图的活)");
|
|
62
|
+
// 重建:删掉再同步得到逐字节相同的文件(所以文件里不写生成时间)
|
|
63
|
+
unlinkSync(index.filePath);
|
|
64
|
+
index.sync(rows);
|
|
65
|
+
assert.equal(readFileSync(index.filePath, "utf8"), first, "可从库重建,且逐字节一致");
|
|
66
|
+
// 内容没变就不重写(与镜像同一条「不无谓写盘」纪律)
|
|
67
|
+
assert.equal(index.sync(rows).changed, false);
|
|
68
|
+
// 空集也要有一份合法的索引(不是删文件)
|
|
69
|
+
assert.equal(index.sync([]).changed, true);
|
|
70
|
+
assert.ok(readFileSync(index.filePath, "utf8").includes("机器所有"));
|
|
71
|
+
} finally {
|
|
72
|
+
rmSync(dir, { recursive: true, force: true });
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test("#296: 索引写不进去只报失败,不抛(写失败不能连累业务写)", () => {
|
|
77
|
+
const dir = tempDir();
|
|
78
|
+
try {
|
|
79
|
+
// 让目标目录的「父级」是一个普通文件:mkdir 必失败,且不依赖平台错误码
|
|
80
|
+
const blocker = join(dir, "blocker");
|
|
81
|
+
writeFileSync(blocker, "not a directory", "utf8");
|
|
82
|
+
const index = createDocumentIndex(join(blocker, "documents"));
|
|
83
|
+
const ensured = index.ensure();
|
|
84
|
+
assert.equal(ensured.ok, false, "建目录失败要如实返回(调用方据此 warn)");
|
|
85
|
+
assert.ok(ensured.error, "失败要带原因");
|
|
86
|
+
const result = index.sync([{ id: "d1", title: "t", content: "c", doc_path: "x", updated_at: "2026-01-01T00:00:00.000Z" }]);
|
|
87
|
+
assert.equal(result.ok, false, "失败要如实返回,不能假装成功");
|
|
88
|
+
assert.ok(result.error, "失败要带原因");
|
|
89
|
+
assert.ok(!existsSync(index.filePath));
|
|
90
|
+
} finally {
|
|
91
|
+
rmSync(dir, { recursive: true, force: true });
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test("#296: index.md 删掉后从库里重建,逐字节一致", () => {
|
|
96
|
+
const dir = tempDir();
|
|
97
|
+
let store;
|
|
98
|
+
try {
|
|
99
|
+
const documentDir = join(dir, "documents");
|
|
100
|
+
store = createStore(join(dir, "memory.db"));
|
|
101
|
+
const index = createDocumentIndex(documentDir);
|
|
102
|
+
const service = createService({
|
|
103
|
+
store, mirror: createMirror(dir), config: { documentMemoryEnabled: true }, documentIndex: index
|
|
104
|
+
});
|
|
105
|
+
writeFileSync(join(dir, "report.md"), "x", "utf8");
|
|
106
|
+
const doc = store.saveDocument({
|
|
107
|
+
type: "document", title: "报告", content: "第一句。第二句。", tags: [], importance: 3,
|
|
108
|
+
source: "tool", evidence: [], doc_path: join(documentDir, "report.md")
|
|
109
|
+
});
|
|
110
|
+
service.saveWithDedupe({ type: "pattern", title: "触发", content: "x" });
|
|
111
|
+
const first = readFileSync(index.filePath, "utf8");
|
|
112
|
+
// 真删文件,再从库同步一次:读数必须一样(这才是「可从库重建」,不是拿同一份
|
|
113
|
+
// 内存数组重算)
|
|
114
|
+
unlinkSync(index.filePath);
|
|
115
|
+
assert.ok(!existsSync(index.filePath));
|
|
116
|
+
service.saveWithDedupe({ type: "pattern", title: "触发2", content: "y" });
|
|
117
|
+
assert.equal(readFileSync(index.filePath, "utf8"), first, "删掉重建后逐字节一致");
|
|
118
|
+
assert.ok(first.includes(doc.id) && first.includes("managed"));
|
|
119
|
+
} finally {
|
|
120
|
+
store?.close();
|
|
121
|
+
rmSync(dir, { recursive: true, force: true });
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
test("#296: managed 文件走一次完整注册往返,字节哈希不变", async () => {
|
|
126
|
+
const dir = tempDir();
|
|
127
|
+
let store;
|
|
128
|
+
try {
|
|
129
|
+
const documentDir = join(dir, "documents");
|
|
130
|
+
mkdirSync(documentDir, { recursive: true });
|
|
131
|
+
const docFile = join(documentDir, "report.md");
|
|
132
|
+
writeFileSync(docFile, "# 报告\n\n正文由 agent 写。\n", "utf8");
|
|
133
|
+
const before = createHash("sha256").update(readFileSync(docFile)).digest("hex");
|
|
134
|
+
store = createStore(join(dir, "memory.db"));
|
|
135
|
+
const service = createService({
|
|
136
|
+
store, mirror: createMirror(dir), config: { documentMemoryEnabled: true },
|
|
137
|
+
documentIndex: createDocumentIndex(documentDir)
|
|
138
|
+
});
|
|
139
|
+
const result = await service.registerDocument({ path: docFile, title: "报告", summary: "第一句。第二句。" });
|
|
140
|
+
assert.equal(result.action, "created");
|
|
141
|
+
assert.equal(result.memory.doc_path, resolve(docFile), "指针行落在 managed 路径上");
|
|
142
|
+
assert.equal(
|
|
143
|
+
createHash("sha256").update(readFileSync(docFile)).digest("hex"), before,
|
|
144
|
+
"管线对正文零读零写:注册往返后 managed 文件必须逐字节不变"
|
|
145
|
+
);
|
|
146
|
+
// 同时也确认没有偷偷在 documentDir 里生成第二份「正文」(机器产物只有 index.md)
|
|
147
|
+
assert.deepEqual(readdirSync(documentDir).sort(), ["index.md", "report.md"]);
|
|
148
|
+
} finally {
|
|
149
|
+
store?.close();
|
|
150
|
+
rmSync(dir, { recursive: true, force: true });
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
test("#296: 两个落点共用 documentMemoryEnabled 闸,闸关时视图与索引一起清掉", () => {
|
|
155
|
+
const dir = tempDir();
|
|
156
|
+
let store;
|
|
157
|
+
try {
|
|
158
|
+
const documentDir = join(dir, "documents");
|
|
159
|
+
store = createStore(join(dir, "memory.db"));
|
|
160
|
+
const index = createDocumentIndex(documentDir);
|
|
161
|
+
const on = createService({
|
|
162
|
+
store, mirror: createMirror(dir), config: { documentMemoryEnabled: true }, documentIndex: index
|
|
163
|
+
});
|
|
164
|
+
writeFileSync(join(dir, "outside.md"), "x", "utf8");
|
|
165
|
+
const kept = store.saveDocument({
|
|
166
|
+
type: "document", title: "报告", content: "第一句。第二句。", tags: [], importance: 3,
|
|
167
|
+
source: "tool", evidence: [], doc_path: join(documentDir, "report.md")
|
|
168
|
+
});
|
|
169
|
+
store.saveDocument({
|
|
170
|
+
type: "document", title: "外部", content: "另一份。", tags: [], importance: 3,
|
|
171
|
+
source: "tool", evidence: [], doc_path: join(dir, "outside.md")
|
|
172
|
+
});
|
|
173
|
+
// 任意一次业务写触发写后语(镜像 + 索引一起)
|
|
174
|
+
on.saveWithDedupe({ type: "pattern", title: "触发", content: "x" });
|
|
175
|
+
assert.ok(existsSync(join(dir, "documents.md")), "闸开 → 镜像视图落盘");
|
|
176
|
+
const md = readFileSync(join(dir, "documents.md"), "utf8");
|
|
177
|
+
assert.ok(md.includes("报告") && md.includes(join(documentDir, "report.md")));
|
|
178
|
+
assert.ok(existsSync(index.filePath), "闸开 → 索引落盘");
|
|
179
|
+
assert.ok(readFileSync(index.filePath, "utf8").includes("managed"));
|
|
180
|
+
|
|
181
|
+
// 归档一行:索引只列活跃行(与镜像的 coverage: active-only 同口径)
|
|
182
|
+
store.setArchived(kept.id, true);
|
|
183
|
+
on.saveWithDedupe({ type: "pattern", title: "触发1b", content: "w" });
|
|
184
|
+
assert.ok(!readFileSync(index.filePath, "utf8").includes(kept.id), "已归档的行不进索引");
|
|
185
|
+
|
|
186
|
+
// 闸关:document 行不再进渲染集 → documents.md 被既有「空 type 删文件」清掉;
|
|
187
|
+
// index.md 归 syncDocumentIndex 清掉——留一份陈旧索引就是「看着还在、其实已关」
|
|
188
|
+
// 的视图,正是 documents.md 在闸关时要避免的那一类。
|
|
189
|
+
const off = createService({ store, mirror: createMirror(dir), config: {}, documentIndex: index });
|
|
190
|
+
off.saveWithDedupe({ type: "pattern", title: "触发2", content: "y" });
|
|
191
|
+
assert.ok(!existsSync(join(dir, "documents.md")), "闸关 → 镜像视图清掉");
|
|
192
|
+
assert.ok(!existsSync(index.filePath), "闸关 → 索引也清掉");
|
|
193
|
+
|
|
194
|
+
// 闸重开:下一次业务写从库重建索引(自愈,不需要手工删文件)
|
|
195
|
+
const back = createService({
|
|
196
|
+
store, mirror: createMirror(dir), config: { documentMemoryEnabled: true }, documentIndex: index
|
|
197
|
+
});
|
|
198
|
+
back.saveWithDedupe({ type: "pattern", title: "触发3", content: "z" });
|
|
199
|
+
assert.ok(readFileSync(index.filePath, "utf8").includes("外部"), "闸重开 → 索引从库重建");
|
|
200
|
+
} finally {
|
|
201
|
+
store?.close();
|
|
202
|
+
rmSync(dir, { recursive: true, force: true });
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
test("#296: 两条新文案的英文分支也要能渲染(en 实例)", () => {
|
|
207
|
+
const dir = tempDir();
|
|
208
|
+
try {
|
|
209
|
+
// 新文案是 per-language 的 lambda:en 分支此前零调用点,缺键或笔误只在
|
|
210
|
+
// memory.language=en 的实例上炸,而全量测试照样绿(审查指出)。
|
|
211
|
+
const index = createDocumentIndex(join(dir, "documents"), "en");
|
|
212
|
+
index.sync([{
|
|
213
|
+
id: "d1", title: "Report", content: "First sentence. Second.", updated_at: "2026-01-01T00:00:00.000Z",
|
|
214
|
+
doc_path: join(dir, "documents", "report.md")
|
|
215
|
+
}]);
|
|
216
|
+
const indexText = readFileSync(index.filePath, "utf8");
|
|
217
|
+
assert.match(indexText, /document index — dsh-mneme/, "en 的索引头");
|
|
218
|
+
assert.match(indexText, /Machine-owned as a whole/, "en 的归属说明");
|
|
219
|
+
|
|
220
|
+
const mirror = createMirror(dir, "en");
|
|
221
|
+
mirror.sync([{
|
|
222
|
+
id: "doc1", type: "document", title: "Report", content: "First sentence. Second.", tags: [],
|
|
223
|
+
importance: 3, forgotten: false, created_at: "2026-01-01T00:00:00.000Z",
|
|
224
|
+
updated_at: "2026-01-01T00:00:00.000Z", doc_path: join(dir, "documents", "report.md")
|
|
225
|
+
}]);
|
|
226
|
+
const docs = readFileSync(join(dir, "documents.md"), "utf8");
|
|
227
|
+
assert.match(docs, /read-only view/, "en 的只读视图文件头");
|
|
228
|
+
assert.match(docs, /never the full text/, "en 的只读说明");
|
|
229
|
+
assert.ok(docs.includes("First sentence") && !docs.includes("Second."), "en 下同样只留摘要首句");
|
|
230
|
+
} finally {
|
|
231
|
+
rmSync(dir, { recursive: true, force: true });
|
|
232
|
+
}
|
|
233
|
+
});
|