@modusensus/dsh-mneme 0.8.14 → 0.8.16
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 +660 -654
- package/assets/icon.svg +3 -3
- package/bin/cli.mjs +691 -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 +275 -0
- package/docs/CONTINUITY.md +124 -0
- package/docs/DAEMON.md +46 -0
- package/docs/DISTILL.md +162 -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 +98 -0
- package/lib/api-standalone.js +656 -673
- package/lib/api.js +1605 -1433
- package/lib/bootstrap.js +211 -211
- package/lib/client.js +6354 -5296
- package/lib/commands.js +95 -95
- package/lib/config.js +947 -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 +694 -674
- package/lib/dream/narratives.js +44 -44
- package/lib/dream/sleep.js +819 -819
- package/lib/dream.js +1503 -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 +108 -46
- package/lib/index.js +552 -503
- package/lib/inject.js +428 -450
- package/lib/lang.js +248 -229
- package/lib/local-embedder.js +358 -358
- package/lib/maintenance.js +162 -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 +190 -175
- package/lib/reclaim-http.js +150 -0
- 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/safe-slice.js +18 -0
- 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 +2688 -2605
- package/lib/settings.js +528 -511
- package/lib/store.js +3196 -3007
- package/lib/summarize.js +1039 -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 +656 -673
- package/src/api.js +1605 -1433
- package/src/bootstrap.js +211 -211
- package/src/commands.js +95 -95
- package/src/config.js +947 -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 +694 -674
- package/src/dream/narratives.js +44 -44
- package/src/dream/sleep.js +819 -819
- package/src/dream.js +1503 -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 +108 -46
- package/src/index.js +552 -503
- package/src/inject.js +428 -450
- package/src/lang.js +248 -229
- package/src/local-embedder.js +358 -358
- package/src/maintenance.js +162 -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 +190 -175
- package/src/reclaim-http.js +150 -0
- 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/safe-slice.js +18 -0
- 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 +2688 -2605
- package/src/settings.js +528 -511
- package/src/store.js +3196 -3007
- package/src/summarize.js +1039 -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 +899 -218
- package/test/api.test.js +1725 -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 +1582 -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/contrast.test.js +314 -0
- 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-merge-guardrail.test.js +219 -0
- 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 +211 -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 +144 -105
- package/test/inject-rotation.test.js +161 -161
- package/test/inject.test.js +443 -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 +696 -598
- package/test/llm-message-source.test.js +207 -207
- package/test/local-embedder.test.js +292 -292
- package/test/maintenance.test.js +501 -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 +444 -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 +274 -189
- package/test/receipt-chain.test.js +451 -451
- package/test/reclaim-http.test.js +209 -0
- 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 +410 -405
- package/test/settings.test.js +269 -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 +504 -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 +1639 -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/continuity.test.js
CHANGED
|
@@ -1,333 +1,333 @@
|
|
|
1
|
-
import test from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
import { mkdtempSync, readFileSync, rmSync } from "node:fs";
|
|
4
|
-
import { tmpdir } from "node:os";
|
|
5
|
-
import { join } from "node:path";
|
|
6
|
-
import { createStore } from "../src/store.js";
|
|
7
|
-
import { createService } from "../src/service.js";
|
|
8
|
-
import { createTools } from "../src/tools.js";
|
|
9
|
-
import { Config, applyLightModePreset, injectChildEnabled } from "../src/config.js";
|
|
10
|
-
import {
|
|
11
|
-
CONTINUITY_KIND,
|
|
12
|
-
CONTINUITY_NOTICE_PREFIX,
|
|
13
|
-
CONTINUITY_FIELD_MAX,
|
|
14
|
-
deriveContinuity,
|
|
15
|
-
renderContinuityNotice,
|
|
16
|
-
createContinuityRescue
|
|
17
|
-
} from "../src/continuity.js";
|
|
18
|
-
|
|
19
|
-
// 回归(issue #249 N3):压缩边缘双落点。
|
|
20
|
-
//
|
|
21
|
-
// 这个文件锁的是「形态」而不是「文案」:宿主的压缩摘要器只看对话里的内容,所以
|
|
22
|
-
// 双落点(落提案 + 末尾追加)缺一半功能就等于没做——每条用例都对应一处会静默退化
|
|
23
|
-
// 的形态,而不是某段文本正好长什么样。
|
|
24
|
-
//
|
|
25
|
-
// 事件形状照 summarize.js 的 collectMessages 口径:user/message 的文本在
|
|
26
|
-
// `data.content`、来源在 `data.source`,assistant/message 的在 `data.message.content`。
|
|
27
|
-
|
|
28
|
-
const KIND = CONTINUITY_KIND;
|
|
29
|
-
|
|
30
|
-
function userEvent(seq, text, source = { kind: "user" }) {
|
|
31
|
-
return { type: "user/message", seq, data: { content: [{ type: "text", text }], source } };
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function assistantEvent(seq, text) {
|
|
35
|
-
return { type: "assistant/message", seq, data: { message: { content: [{ type: "text", text }] } } };
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/** surfaceSeqs 给了才挂表面投影(`session.surface.nodes` 只存活节点的 seq):判重按表面、
|
|
39
|
-
* 抽取按全量日志,这两条口径必须能分开测。 */
|
|
40
|
-
function fakeSession(id, events, surfaceSeqs) {
|
|
41
|
-
const session = { id, snapshotEvents: () => events, append() {} };
|
|
42
|
-
if (surfaceSeqs) session.surface = { nodes: surfaceSeqs };
|
|
43
|
-
return session;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/** 捕获 handler 的 ctx:fire 直接调被注册的函数,返回它的返回值(含 Promise)。 */
|
|
47
|
-
function mockCtx() {
|
|
48
|
-
const handlers = new Map();
|
|
49
|
-
const warnings = [];
|
|
50
|
-
return {
|
|
51
|
-
logger: { warn: (message) => warnings.push(String(message)) },
|
|
52
|
-
warnings,
|
|
53
|
-
on(name, fn) {
|
|
54
|
-
handlers.set(name, fn);
|
|
55
|
-
return () => handlers.delete(name);
|
|
56
|
-
},
|
|
57
|
-
fire(name, ...args) {
|
|
58
|
-
return handlers.get(name)?.(...args);
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function toolDescriptions(config) {
|
|
64
|
-
const store = createStore(":memory:");
|
|
65
|
-
const service = createService({ store, mirror: null, config: {} });
|
|
66
|
-
const registered = [];
|
|
67
|
-
const ctx = { tools: { register(def) { registered.push(def); return () => {}; } } };
|
|
68
|
-
createTools(ctx, service, config, null);
|
|
69
|
-
return new Map(registered.map((t) => [t.name, t.description]));
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
test("#249 N3:抽取口径——最后一句话的指令取头、最后一段回复取尾", () => {
|
|
73
|
-
const long = "x".repeat(CONTINUITY_FIELD_MAX * 2);
|
|
74
|
-
const fields = deriveContinuity(
|
|
75
|
-
fakeSession("s1", [
|
|
76
|
-
userEvent(1, "早期的指令"),
|
|
77
|
-
assistantEvent(2, "早期的回复"),
|
|
78
|
-
userEvent(4, `最新指令 ${long}`),
|
|
79
|
-
assistantEvent(5, `最新回复 ${long}`),
|
|
80
|
-
// 插件消息(我们自己的注入或别的插件)不是"用户当前指令",必须跳过:否则注入物会被
|
|
81
|
-
// 当成用户诉求、下一轮又抄进快照(自我强化)。**刻意放在最后一条 user/message**:
|
|
82
|
-
// 夹在中间时"后写覆盖"会让这条断言恒绿,钉不住这层过滤(审查发现)。
|
|
83
|
-
userEvent(6, "插件注入的一行", { kind: "plugin", plugin: "dsh-mneme" })
|
|
84
|
-
])
|
|
85
|
-
);
|
|
86
|
-
assert.equal(fields.currentWork, `最新指令 ${long}`.slice(0, CONTINUITY_FIELD_MAX), "取最近一条真实用户消息的头部");
|
|
87
|
-
assert.equal(fields.nextStep, `最新回复 ${long}`.slice(-CONTINUITY_FIELD_MAX), "下一步活在回复末尾,故取尾部");
|
|
88
|
-
assert.equal(fields.openQuestions, null, "确定性抽取判不出未决问题,字段留空而不编造");
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
test("#249 N3:渲染——统一前缀 + 三字段单行 + 空字段如实标 none", () => {
|
|
92
|
-
const text = renderContinuityNotice({ currentWork: "改 注入\n第二行", nextStep: null, openQuestions: "要不要开 PR?" });
|
|
93
|
-
assert.ok(text.startsWith(CONTINUITY_NOTICE_PREFIX), "统一前缀是剥离与判重的锚");
|
|
94
|
-
assert.ok(!text.slice(CONTINUITY_NOTICE_PREFIX.length).includes("\n\n"), "字段内部换行被压平,一条消息仍是结构化的三行");
|
|
95
|
-
assert.ok(text.includes("current_work: 改 注入 第二行"), "字段内换行压成空格");
|
|
96
|
-
assert.ok(text.includes("next_step: (none)"), "空字段如实标 none");
|
|
97
|
-
assert.ok(text.includes("open_questions: 要不要开 PR?"));
|
|
98
|
-
assert.equal(renderContinuityNotice({ currentWork: "y".repeat(500) }, 40).length, 40, "整条有硬上限");
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
test("#249 N3:提案落库——同一会话同一类只一条(刷新而不是新增)", () => {
|
|
102
|
-
const store = createStore(":memory:");
|
|
103
|
-
const first = store.saveContinuityProposal({ sessionId: "s1", kind: KIND, currentWork: "第一版", edgeSeq: 7 });
|
|
104
|
-
const created = store.getContinuityProposal("s1", KIND);
|
|
105
|
-
assert.equal(first.created, true);
|
|
106
|
-
const again = store.saveContinuityProposal({ sessionId: "s1", kind: KIND, currentWork: "第二版", edgeSeq: 9 });
|
|
107
|
-
assert.equal(again.created, false, "同一 (session, kind) 是刷新");
|
|
108
|
-
assert.equal(again.id, first.id);
|
|
109
|
-
const row = store.getContinuityProposal("s1", KIND);
|
|
110
|
-
assert.equal(row.current_work, "第二版", "刷新覆盖内容(最新的边缘赢)");
|
|
111
|
-
assert.equal(row.created_at, created.created_at, "created_at 记首次落库,刷新不动它");
|
|
112
|
-
assert.equal(row.edge_seq, 9, "edge_seq 记最近一次触发,是「实际触发率」的证据");
|
|
113
|
-
assert.equal(store.countContinuityProposals({ status: "pending" }), 1);
|
|
114
|
-
// 唯一键是「会话 + 类型」这一对:换任一维都是另一条提案。
|
|
115
|
-
store.saveContinuityProposal({ sessionId: "s2", kind: KIND, currentWork: "别的会话" });
|
|
116
|
-
store.saveContinuityProposal({ sessionId: "s1", kind: "other-kind", currentWork: "别的类" });
|
|
117
|
-
assert.equal(store.countContinuityProposals(), 3);
|
|
118
|
-
assert.equal(store.listContinuityProposals().length, 3, "读侧按 updated_at 倒序返回");
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
test("#249 N3:队列满则弃新——但不是淘汰旧行,且不影响刷新", () => {
|
|
122
|
-
const store = createStore(":memory:");
|
|
123
|
-
store.saveContinuityProposal({ sessionId: "s1", kind: KIND, currentWork: "旧会话的活状态" });
|
|
124
|
-
const dropped = store.saveContinuityProposal({ sessionId: "s2", kind: KIND, currentWork: "新的", maxPending: 1 });
|
|
125
|
-
assert.deepEqual(dropped, { id: null, created: false, dropped: true }, "达上限丢掉本次触发");
|
|
126
|
-
assert.equal(store.countContinuityProposals(), 1, "旧行仍在——弃新不是淘汰旧,旧行是别的会话还没转正的工作状态");
|
|
127
|
-
const refreshed = store.saveContinuityProposal({ sessionId: "s1", kind: KIND, currentWork: "刷新", maxPending: 1 });
|
|
128
|
-
assert.equal(refreshed.created, false, "满队列不影响已有行的刷新");
|
|
129
|
-
assert.equal(store.getContinuityProposal("s1", KIND).current_work, "刷新");
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
test("#249 N3:pre-step 双落点——压缩边缘落提案并往末尾追加一条", async () => {
|
|
133
|
-
const store = createStore(":memory:");
|
|
134
|
-
const ctx = mockCtx();
|
|
135
|
-
const rescue = createContinuityRescue(ctx, store);
|
|
136
|
-
const events = [userEvent(1, "把 N3 实现完"), assistantEvent(2, "接着写回归测试")];
|
|
137
|
-
const nodes = [1, 2];
|
|
138
|
-
const session = fakeSession("s1", events, nodes);
|
|
139
|
-
|
|
140
|
-
const step = () => ctx.fire("agent/pre-step", { agent: { session } }, async () => ({ kind: "enter", messages: [] }));
|
|
141
|
-
|
|
142
|
-
// 没有边缘时不追加:本功能只在宿主真的压缩之后动作,不自定阈值。
|
|
143
|
-
assert.deepEqual((await step()).messages, [], "无压缩事件 → 不做任何事");
|
|
144
|
-
|
|
145
|
-
ctx.fire("session/event", session, { type: "compaction/end", seq: 9 });
|
|
146
|
-
const decision = await step();
|
|
147
|
-
assert.equal(decision.messages.length, 1, "压缩边缘 → 追加一条");
|
|
148
|
-
const message = decision.messages[0];
|
|
149
|
-
assert.equal(message.source.plugin, "dsh-mneme");
|
|
150
|
-
assert.equal(message.source.kind, "plugin:dsh-mneme", "插件来源会被蒸馏管线跳过,注入物不会变成记忆(也不会自我蒸馏);裸 kind: \"plugin\" 被 V4 写入准入拒收(issue #326)");
|
|
151
|
-
assert.ok(message.content[0].text.startsWith(CONTINUITY_NOTICE_PREFIX));
|
|
152
|
-
assert.ok(message.content[0].text.includes("current_work: 把 N3 实现完"));
|
|
153
|
-
const proposal = store.getContinuityProposal("s1", KIND);
|
|
154
|
-
assert.equal(proposal.current_work, "把 N3 实现完", "落库先于注入:提案是脱离对话存活的那一半");
|
|
155
|
-
assert.equal(proposal.edge_seq, 9);
|
|
156
|
-
|
|
157
|
-
// 同一个边缘只消费一次(无新边缘 → 不再追加)。
|
|
158
|
-
assert.deepEqual((await step()).messages, [], "边缘一次性消费,不会每步都追加");
|
|
159
|
-
|
|
160
|
-
// 「不变不重复」:宿主已经把这条注入落进会话表面后,同文本不再追加第二次。
|
|
161
|
-
events.push({ type: "user/message", seq: 11, data: { content: message.content, source: message.source } });
|
|
162
|
-
nodes.push(11); // 宿主把这条注入落进了当前表面
|
|
163
|
-
ctx.fire("session/event", session, { type: "compaction/end", seq: 12 });
|
|
164
|
-
const sameText = await step();
|
|
165
|
-
assert.deepEqual(sameText.messages, [], "同文本已在表面里 → 不重复追加(没有改写钩子,只能做到这一层)");
|
|
166
|
-
assert.equal(store.getContinuityProposal("s1", KIND).edge_seq, 12, "但提案仍被刷新——两半各自独立");
|
|
167
|
-
|
|
168
|
-
// 内容变了就允许再追加一次:判重按全文比较,不是"注入过就再不注入"。
|
|
169
|
-
events.push(assistantEvent(13, "内容变了"));
|
|
170
|
-
ctx.fire("session/event", session, { type: "compaction/end", seq: 14 });
|
|
171
|
-
assert.equal((await step()).messages.length, 1, "快照内容变化 → 追加新的一份");
|
|
172
|
-
|
|
173
|
-
// dispose 只能按行为断言:mockCtx 的 handler 是块体箭头函数、恒返回 undefined,拿 fire 的
|
|
174
|
-
// 返回值断言"已摘除"是恒真的(审查发现的假断言)。pre-step 监听器活着时返回 decision、
|
|
175
|
-
// 摘除后 fire 拿不到 handler,两者才是可分辨的。
|
|
176
|
-
rescue.dispose();
|
|
177
|
-
ctx.fire("session/event", session, { type: "compaction/end", seq: 15 });
|
|
178
|
-
const afterDispose = await step();
|
|
179
|
-
assert.equal(afterDispose, undefined, "dispose 后不再有 pre-step 监听器");
|
|
180
|
-
assert.equal(store.getContinuityProposal("s1", KIND).edge_seq, 14, "dispose 后不再落库(edge_seq 停在最后一条边缘)");
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
test("#249 N3:宿主表面读取抛错时退回全量判重,也不打断宿主的一步", async () => {
|
|
184
|
-
const ctx = mockCtx();
|
|
185
|
-
const stub = { saveContinuityProposal: () => ({ id: "x", created: true, dropped: false }) };
|
|
186
|
-
const rescue = createContinuityRescue(ctx, stub);
|
|
187
|
-
const session = fakeSession("s1", [userEvent(1, "指令")]);
|
|
188
|
-
// 宿主的 `surface.nodes` 是会抛的 getter(它自己内部的 projection 断言)。判重读不到就退回
|
|
189
|
-
// 全量:可选的抢救功能绝不能因为读宿主状态失败而把 agent 的这一步变失败。
|
|
190
|
-
Object.defineProperty(session, "surface", {
|
|
191
|
-
get() {
|
|
192
|
-
throw new Error('session message projection "X" was removed or replaced');
|
|
193
|
-
}
|
|
194
|
-
});
|
|
195
|
-
ctx.fire("session/event", session, { type: "compaction/end", seq: 3 });
|
|
196
|
-
const decision = await ctx.fire("agent/pre-step", { agent: { session } }, async () => ({ kind: "enter", messages: [] }));
|
|
197
|
-
assert.equal(decision.messages.length, 1, "读不到表面不该 reject 这一步,注入照做");
|
|
198
|
-
rescue.dispose();
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
test("#249 N3:压缩事件不带 seq 时仍要注入(「有没有边缘」不等于「序号的真假」)", async () => {
|
|
202
|
-
const store = createStore(":memory:");
|
|
203
|
-
const ctx = mockCtx();
|
|
204
|
-
const rescue = createContinuityRescue(ctx, store);
|
|
205
|
-
const session = fakeSession("s1", [userEvent(1, "指令")]);
|
|
206
|
-
// 边缘只记「有没有」这一件事,序号允许为 null。把「取到的值」当真假判断(`if (!edge.get(id))`
|
|
207
|
-
// 那种写法)会把这条边缘当成不存在:既不注入,也不清理。
|
|
208
|
-
ctx.fire("session/event", session, { type: "compaction/end" });
|
|
209
|
-
const decision = await ctx.fire("agent/pre-step", { agent: { session } }, async () => ({ kind: "enter", messages: [] }));
|
|
210
|
-
assert.equal(decision.messages.length, 1, "序号缺失不影响注入");
|
|
211
|
-
assert.equal(store.getContinuityProposal("s1", KIND).edge_seq, null, "序号缺失就如实记 null,不编一个数");
|
|
212
|
-
rescue.dispose();
|
|
213
|
-
});
|
|
214
|
-
|
|
215
|
-
test("#249 N3:判重按当前表面——被折叠出表面的旧注入不算「已注入」", async () => {
|
|
216
|
-
const store = createStore(":memory:");
|
|
217
|
-
const ctx = mockCtx();
|
|
218
|
-
const rescue = createContinuityRescue(ctx, store);
|
|
219
|
-
const old = renderContinuityNotice({ currentWork: "指令", nextStep: "回复" });
|
|
220
|
-
// seq 0 是上一次注入的快照:还在 append-only 日志里,但已被一次压缩 replace 折叠出表面。
|
|
221
|
-
const events = [
|
|
222
|
-
{ type: "user/message", seq: 0, data: { content: [{ type: "text", text: old }], source: { kind: "plugin", plugin: "dsh-mneme" } } },
|
|
223
|
-
userEvent(1, "指令"),
|
|
224
|
-
assistantEvent(2, "回复")
|
|
225
|
-
];
|
|
226
|
-
const session = fakeSession("s1", events, [1, 2]);
|
|
227
|
-
ctx.fire("session/event", session, { type: "compaction/end", seq: 9 });
|
|
228
|
-
const decision = await ctx.fire("agent/pre-step", { agent: { session } }, async () => ({ kind: "enter", messages: [] }));
|
|
229
|
-
// 按日志判重会在这里静默跳过(同文本命中已折叠的旧事件),注入那一半就空了一次;
|
|
230
|
-
// 按表面判重才补得上——这正是双落点的 ② 存在的理由。
|
|
231
|
-
assert.equal(decision.messages.length, 1, "日志里有同文本、表面里没有 → 这一次仍要补");
|
|
232
|
-
rescue.dispose();
|
|
233
|
-
});
|
|
234
|
-
|
|
235
|
-
test("#249 N3:落库失败不打断宿主的一步(本函数的不变量)", async () => {
|
|
236
|
-
const ctx = mockCtx();
|
|
237
|
-
const broken = { saveContinuityProposal() { throw new Error("boom"); } };
|
|
238
|
-
const rescue = createContinuityRescue(ctx, broken);
|
|
239
|
-
const session = fakeSession("s1", [userEvent(1, "指令")]);
|
|
240
|
-
ctx.fire("session/event", session, { type: "compaction/end", seq: 3 });
|
|
241
|
-
const decision = await ctx.fire("agent/pre-step", { agent: { session } }, async () => ({ kind: "enter", messages: [] }));
|
|
242
|
-
// 写不进去也要把注入那一半做出来:追加不依赖库;更关键的是可选插件的故障绝不该把
|
|
243
|
-
// agent 的这一步变成失败(磁盘满 / busy 超时 / 库损坏都会走到这里)。
|
|
244
|
-
assert.equal(decision.messages.length, 1, "库写不进去,注入那一半照做");
|
|
245
|
-
assert.equal(ctx.warnings.length, 1, "失败要留痕,不静默");
|
|
246
|
-
assert.match(ctx.warnings[0], /not recorded/);
|
|
247
|
-
rescue.dispose();
|
|
248
|
-
});
|
|
249
|
-
|
|
250
|
-
test("#249 N3:队列满的丢弃必须留痕(§8 触发率口径)", async () => {
|
|
251
|
-
const store = createStore(":memory:");
|
|
252
|
-
const ctx = mockCtx();
|
|
253
|
-
const rescue = createContinuityRescue(ctx, store);
|
|
254
|
-
for (let i = 0; i < 200; i += 1) {
|
|
255
|
-
store.saveContinuityProposal({ sessionId: `filler-${i}`, kind: KIND, currentWork: "占位" });
|
|
256
|
-
}
|
|
257
|
-
const session = fakeSession("s1", [userEvent(1, "指令")]);
|
|
258
|
-
ctx.fire("session/event", session, { type: "compaction/end", seq: 3 });
|
|
259
|
-
const decision = await ctx.fire("agent/pre-step", { agent: { session } }, async () => ({ kind: "enter", messages: [] }));
|
|
260
|
-
assert.equal(store.getContinuityProposal("s1", KIND), null, "满队列弃新:本次触发不落库");
|
|
261
|
-
assert.equal(decision.messages.length, 1, "但注入那一半照做(两半各自独立)");
|
|
262
|
-
assert.ok(ctx.warnings.some((w) => w.includes("queue full")), "丢弃是静默失效的那一类,必须报出来");
|
|
263
|
-
rescue.dispose();
|
|
264
|
-
});
|
|
265
|
-
|
|
266
|
-
test("#249 N3:门控不调模型(静态锁)与挂载点的父开关在前", () => {
|
|
267
|
-
// §11「门控不调 LLM」:本模块只 import 消息工厂。静态锁而不是运行时断言——真去调模型的
|
|
268
|
-
// 回归不会从返回值露出来,只会让这一步变慢、变贵。
|
|
269
|
-
const src = readFileSync(new URL("../src/continuity.js", import.meta.url), "utf8");
|
|
270
|
-
assert.deepEqual(src.match(/^import .*$/gm), ['import { createUserMessage } from "@deepseek-ai/dsh-llm";']);
|
|
271
|
-
// 挂载点(`src/index.js`)是本功能唯一的生产入口:父开关必须在前、子项在后,否则「默认关」
|
|
272
|
-
// 只剩 Config 里的默认值撑着,接线漏一次没人发现。
|
|
273
|
-
const index = readFileSync(new URL("../src/index.js", import.meta.url), "utf8");
|
|
274
|
-
assert.match(index, /if \(cfg\.autoInject && injectChildEnabled\(cfg, "continuityRescueEnabled"\)\) \{/);
|
|
275
|
-
assert.match(index, /disposers\.push\(createContinuityRescue\(ctx, store\)\)/);
|
|
276
|
-
});
|
|
277
|
-
|
|
278
|
-
test("#249 N3:reject 放行且不消费边缘;缺 session 不抛", async () => {
|
|
279
|
-
const store = createStore(":memory:");
|
|
280
|
-
const ctx = mockCtx();
|
|
281
|
-
const rescue = createContinuityRescue(ctx, store);
|
|
282
|
-
const session = fakeSession("s1", [userEvent(1, "指令")]);
|
|
283
|
-
ctx.fire("session/event", session, { type: "compaction/end", seq: 3 });
|
|
284
|
-
|
|
285
|
-
const rejected = { kind: "reject", feedback: "no" };
|
|
286
|
-
const viaReject = await ctx.fire("agent/pre-step", { agent: { session } }, async () => rejected);
|
|
287
|
-
assert.equal(viaReject, rejected, "reject 原样返回——这一步不该发生,塞消息没有意义");
|
|
288
|
-
assert.equal(store.countContinuityProposals(), 0, "边缘没被消费");
|
|
289
|
-
const after = await ctx.fire("agent/pre-step", { agent: { session } }, async () => ({ kind: "enter", messages: [] }));
|
|
290
|
-
assert.equal(after.messages.length, 1, "边缘留给下一次真正的步骤");
|
|
291
|
-
|
|
292
|
-
ctx.fire("session/event", fakeSession("s2", []), { type: "compaction/end", seq: 4 });
|
|
293
|
-
const noAgent = await ctx.fire("agent/pre-step", {}, async () => ({ kind: "enter", messages: [] }));
|
|
294
|
-
assert.deepEqual(noAgent.messages, [], "拿不到 session 就什么都不做");
|
|
295
|
-
rescue.dispose();
|
|
296
|
-
});
|
|
297
|
-
|
|
298
|
-
test("#249 N3:契约——默认关、父关不生效、轻量档压掉、降级规则只在开启时进描述", () => {
|
|
299
|
-
assert.equal(Config({}).continuityRescueEnabled, false, "新增注入表面 → opt-in 默认关");
|
|
300
|
-
assert.equal(applyLightModePreset({ lightMode: true }).continuityRescueEnabled, false, "轻量档压掉(同 injectGuidanceEnabled)");
|
|
301
|
-
assert.equal(injectChildEnabled({ autoInject: false, continuityRescueEnabled: true }, "continuityRescueEnabled"), false, "父关则不生效");
|
|
302
|
-
|
|
303
|
-
const plain = toolDescriptions({ continuityRescueEnabled: false });
|
|
304
|
-
const on = toolDescriptions({ autoInject: true, continuityRescueEnabled: true });
|
|
305
|
-
const gated = toolDescriptions({ autoInject: false, continuityRescueEnabled: true });
|
|
306
|
-
assert.notEqual(on.get("memory_save"), plain.get("memory_save"), "开启时 memory_save 描述补降级规则");
|
|
307
|
-
assert.equal(gated.get("memory_save"), plain.get("memory_save"), "父关 → 描述逐字节回到未开状态");
|
|
308
|
-
assert.equal(on.get("memory_search"), plain.get("memory_search"), "其它工具的描述不受牵连");
|
|
309
|
-
// 降级规则本身是可读的持久规则(宿主无压缩前时机时由 agent 自判),不是一句口号。
|
|
310
|
-
for (const field of ["current_work", "next_step"]) {
|
|
311
|
-
assert.ok(on.get("memory_save").includes(field), `降级规则要点名 ${field}`);
|
|
312
|
-
}
|
|
313
|
-
});
|
|
314
|
-
|
|
315
|
-
test("#249 N3:存量库启动即建表(幂等迁移),重开后提案还在", () => {
|
|
316
|
-
const dir = mkdtempSync(join(tmpdir(), "mneme-continuity-"));
|
|
317
|
-
const path = join(dir, "memory.db");
|
|
318
|
-
let store;
|
|
319
|
-
let reopened;
|
|
320
|
-
try {
|
|
321
|
-
store = createStore(path);
|
|
322
|
-
store.saveContinuityProposal({ sessionId: "s1", kind: KIND, currentWork: "重启前" });
|
|
323
|
-
assert.equal(store.getContinuityProposal("s1", KIND).current_work, "重启前");
|
|
324
|
-
reopened = createStore(path);
|
|
325
|
-
assert.equal(reopened.getContinuityProposal("s1", KIND).current_work, "重启前", "SCHEMA 的 IF NOT EXISTS 让存量库重开也不用迁移");
|
|
326
|
-
assert.equal(reopened.countContinuityProposals({ status: "pending" }), 1);
|
|
327
|
-
} finally {
|
|
328
|
-
// 先关句柄再删目录:断言失败时若还开着库,Windows 的 rmSync 会抛 EPERM 盖掉真因。
|
|
329
|
-
try { reopened?.close(); } catch { /* 关闭失败不该掩盖断言差异 */ }
|
|
330
|
-
try { store?.close(); } catch { /* 同上 */ }
|
|
331
|
-
rmSync(dir, { recursive: true, force: true });
|
|
332
|
-
}
|
|
333
|
-
});
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { mkdtempSync, readFileSync, rmSync } from "node:fs";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import { createStore } from "../src/store.js";
|
|
7
|
+
import { createService } from "../src/service.js";
|
|
8
|
+
import { createTools } from "../src/tools.js";
|
|
9
|
+
import { Config, applyLightModePreset, injectChildEnabled } from "../src/config.js";
|
|
10
|
+
import {
|
|
11
|
+
CONTINUITY_KIND,
|
|
12
|
+
CONTINUITY_NOTICE_PREFIX,
|
|
13
|
+
CONTINUITY_FIELD_MAX,
|
|
14
|
+
deriveContinuity,
|
|
15
|
+
renderContinuityNotice,
|
|
16
|
+
createContinuityRescue
|
|
17
|
+
} from "../src/continuity.js";
|
|
18
|
+
|
|
19
|
+
// 回归(issue #249 N3):压缩边缘双落点。
|
|
20
|
+
//
|
|
21
|
+
// 这个文件锁的是「形态」而不是「文案」:宿主的压缩摘要器只看对话里的内容,所以
|
|
22
|
+
// 双落点(落提案 + 末尾追加)缺一半功能就等于没做——每条用例都对应一处会静默退化
|
|
23
|
+
// 的形态,而不是某段文本正好长什么样。
|
|
24
|
+
//
|
|
25
|
+
// 事件形状照 summarize.js 的 collectMessages 口径:user/message 的文本在
|
|
26
|
+
// `data.content`、来源在 `data.source`,assistant/message 的在 `data.message.content`。
|
|
27
|
+
|
|
28
|
+
const KIND = CONTINUITY_KIND;
|
|
29
|
+
|
|
30
|
+
function userEvent(seq, text, source = { kind: "user" }) {
|
|
31
|
+
return { type: "user/message", seq, data: { content: [{ type: "text", text }], source } };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function assistantEvent(seq, text) {
|
|
35
|
+
return { type: "assistant/message", seq, data: { message: { content: [{ type: "text", text }] } } };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** surfaceSeqs 给了才挂表面投影(`session.surface.nodes` 只存活节点的 seq):判重按表面、
|
|
39
|
+
* 抽取按全量日志,这两条口径必须能分开测。 */
|
|
40
|
+
function fakeSession(id, events, surfaceSeqs) {
|
|
41
|
+
const session = { id, snapshotEvents: () => events, append() {} };
|
|
42
|
+
if (surfaceSeqs) session.surface = { nodes: surfaceSeqs };
|
|
43
|
+
return session;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** 捕获 handler 的 ctx:fire 直接调被注册的函数,返回它的返回值(含 Promise)。 */
|
|
47
|
+
function mockCtx() {
|
|
48
|
+
const handlers = new Map();
|
|
49
|
+
const warnings = [];
|
|
50
|
+
return {
|
|
51
|
+
logger: { warn: (message) => warnings.push(String(message)) },
|
|
52
|
+
warnings,
|
|
53
|
+
on(name, fn) {
|
|
54
|
+
handlers.set(name, fn);
|
|
55
|
+
return () => handlers.delete(name);
|
|
56
|
+
},
|
|
57
|
+
fire(name, ...args) {
|
|
58
|
+
return handlers.get(name)?.(...args);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function toolDescriptions(config) {
|
|
64
|
+
const store = createStore(":memory:");
|
|
65
|
+
const service = createService({ store, mirror: null, config: {} });
|
|
66
|
+
const registered = [];
|
|
67
|
+
const ctx = { tools: { register(def) { registered.push(def); return () => {}; } } };
|
|
68
|
+
createTools(ctx, service, config, null);
|
|
69
|
+
return new Map(registered.map((t) => [t.name, t.description]));
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
test("#249 N3:抽取口径——最后一句话的指令取头、最后一段回复取尾", () => {
|
|
73
|
+
const long = "x".repeat(CONTINUITY_FIELD_MAX * 2);
|
|
74
|
+
const fields = deriveContinuity(
|
|
75
|
+
fakeSession("s1", [
|
|
76
|
+
userEvent(1, "早期的指令"),
|
|
77
|
+
assistantEvent(2, "早期的回复"),
|
|
78
|
+
userEvent(4, `最新指令 ${long}`),
|
|
79
|
+
assistantEvent(5, `最新回复 ${long}`),
|
|
80
|
+
// 插件消息(我们自己的注入或别的插件)不是"用户当前指令",必须跳过:否则注入物会被
|
|
81
|
+
// 当成用户诉求、下一轮又抄进快照(自我强化)。**刻意放在最后一条 user/message**:
|
|
82
|
+
// 夹在中间时"后写覆盖"会让这条断言恒绿,钉不住这层过滤(审查发现)。
|
|
83
|
+
userEvent(6, "插件注入的一行", { kind: "plugin", plugin: "dsh-mneme" })
|
|
84
|
+
])
|
|
85
|
+
);
|
|
86
|
+
assert.equal(fields.currentWork, `最新指令 ${long}`.slice(0, CONTINUITY_FIELD_MAX), "取最近一条真实用户消息的头部");
|
|
87
|
+
assert.equal(fields.nextStep, `最新回复 ${long}`.slice(-CONTINUITY_FIELD_MAX), "下一步活在回复末尾,故取尾部");
|
|
88
|
+
assert.equal(fields.openQuestions, null, "确定性抽取判不出未决问题,字段留空而不编造");
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test("#249 N3:渲染——统一前缀 + 三字段单行 + 空字段如实标 none", () => {
|
|
92
|
+
const text = renderContinuityNotice({ currentWork: "改 注入\n第二行", nextStep: null, openQuestions: "要不要开 PR?" });
|
|
93
|
+
assert.ok(text.startsWith(CONTINUITY_NOTICE_PREFIX), "统一前缀是剥离与判重的锚");
|
|
94
|
+
assert.ok(!text.slice(CONTINUITY_NOTICE_PREFIX.length).includes("\n\n"), "字段内部换行被压平,一条消息仍是结构化的三行");
|
|
95
|
+
assert.ok(text.includes("current_work: 改 注入 第二行"), "字段内换行压成空格");
|
|
96
|
+
assert.ok(text.includes("next_step: (none)"), "空字段如实标 none");
|
|
97
|
+
assert.ok(text.includes("open_questions: 要不要开 PR?"));
|
|
98
|
+
assert.equal(renderContinuityNotice({ currentWork: "y".repeat(500) }, 40).length, 40, "整条有硬上限");
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test("#249 N3:提案落库——同一会话同一类只一条(刷新而不是新增)", () => {
|
|
102
|
+
const store = createStore(":memory:");
|
|
103
|
+
const first = store.saveContinuityProposal({ sessionId: "s1", kind: KIND, currentWork: "第一版", edgeSeq: 7 });
|
|
104
|
+
const created = store.getContinuityProposal("s1", KIND);
|
|
105
|
+
assert.equal(first.created, true);
|
|
106
|
+
const again = store.saveContinuityProposal({ sessionId: "s1", kind: KIND, currentWork: "第二版", edgeSeq: 9 });
|
|
107
|
+
assert.equal(again.created, false, "同一 (session, kind) 是刷新");
|
|
108
|
+
assert.equal(again.id, first.id);
|
|
109
|
+
const row = store.getContinuityProposal("s1", KIND);
|
|
110
|
+
assert.equal(row.current_work, "第二版", "刷新覆盖内容(最新的边缘赢)");
|
|
111
|
+
assert.equal(row.created_at, created.created_at, "created_at 记首次落库,刷新不动它");
|
|
112
|
+
assert.equal(row.edge_seq, 9, "edge_seq 记最近一次触发,是「实际触发率」的证据");
|
|
113
|
+
assert.equal(store.countContinuityProposals({ status: "pending" }), 1);
|
|
114
|
+
// 唯一键是「会话 + 类型」这一对:换任一维都是另一条提案。
|
|
115
|
+
store.saveContinuityProposal({ sessionId: "s2", kind: KIND, currentWork: "别的会话" });
|
|
116
|
+
store.saveContinuityProposal({ sessionId: "s1", kind: "other-kind", currentWork: "别的类" });
|
|
117
|
+
assert.equal(store.countContinuityProposals(), 3);
|
|
118
|
+
assert.equal(store.listContinuityProposals().length, 3, "读侧按 updated_at 倒序返回");
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
test("#249 N3:队列满则弃新——但不是淘汰旧行,且不影响刷新", () => {
|
|
122
|
+
const store = createStore(":memory:");
|
|
123
|
+
store.saveContinuityProposal({ sessionId: "s1", kind: KIND, currentWork: "旧会话的活状态" });
|
|
124
|
+
const dropped = store.saveContinuityProposal({ sessionId: "s2", kind: KIND, currentWork: "新的", maxPending: 1 });
|
|
125
|
+
assert.deepEqual(dropped, { id: null, created: false, dropped: true }, "达上限丢掉本次触发");
|
|
126
|
+
assert.equal(store.countContinuityProposals(), 1, "旧行仍在——弃新不是淘汰旧,旧行是别的会话还没转正的工作状态");
|
|
127
|
+
const refreshed = store.saveContinuityProposal({ sessionId: "s1", kind: KIND, currentWork: "刷新", maxPending: 1 });
|
|
128
|
+
assert.equal(refreshed.created, false, "满队列不影响已有行的刷新");
|
|
129
|
+
assert.equal(store.getContinuityProposal("s1", KIND).current_work, "刷新");
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
test("#249 N3:pre-step 双落点——压缩边缘落提案并往末尾追加一条", async () => {
|
|
133
|
+
const store = createStore(":memory:");
|
|
134
|
+
const ctx = mockCtx();
|
|
135
|
+
const rescue = createContinuityRescue(ctx, store);
|
|
136
|
+
const events = [userEvent(1, "把 N3 实现完"), assistantEvent(2, "接着写回归测试")];
|
|
137
|
+
const nodes = [1, 2];
|
|
138
|
+
const session = fakeSession("s1", events, nodes);
|
|
139
|
+
|
|
140
|
+
const step = () => ctx.fire("agent/pre-step", { agent: { session } }, async () => ({ kind: "enter", messages: [] }));
|
|
141
|
+
|
|
142
|
+
// 没有边缘时不追加:本功能只在宿主真的压缩之后动作,不自定阈值。
|
|
143
|
+
assert.deepEqual((await step()).messages, [], "无压缩事件 → 不做任何事");
|
|
144
|
+
|
|
145
|
+
ctx.fire("session/event", session, { type: "compaction/end", seq: 9 });
|
|
146
|
+
const decision = await step();
|
|
147
|
+
assert.equal(decision.messages.length, 1, "压缩边缘 → 追加一条");
|
|
148
|
+
const message = decision.messages[0];
|
|
149
|
+
assert.equal(message.source.plugin, "dsh-mneme");
|
|
150
|
+
assert.equal(message.source.kind, "plugin:dsh-mneme", "插件来源会被蒸馏管线跳过,注入物不会变成记忆(也不会自我蒸馏);裸 kind: \"plugin\" 被 V4 写入准入拒收(issue #326)");
|
|
151
|
+
assert.ok(message.content[0].text.startsWith(CONTINUITY_NOTICE_PREFIX));
|
|
152
|
+
assert.ok(message.content[0].text.includes("current_work: 把 N3 实现完"));
|
|
153
|
+
const proposal = store.getContinuityProposal("s1", KIND);
|
|
154
|
+
assert.equal(proposal.current_work, "把 N3 实现完", "落库先于注入:提案是脱离对话存活的那一半");
|
|
155
|
+
assert.equal(proposal.edge_seq, 9);
|
|
156
|
+
|
|
157
|
+
// 同一个边缘只消费一次(无新边缘 → 不再追加)。
|
|
158
|
+
assert.deepEqual((await step()).messages, [], "边缘一次性消费,不会每步都追加");
|
|
159
|
+
|
|
160
|
+
// 「不变不重复」:宿主已经把这条注入落进会话表面后,同文本不再追加第二次。
|
|
161
|
+
events.push({ type: "user/message", seq: 11, data: { content: message.content, source: message.source } });
|
|
162
|
+
nodes.push(11); // 宿主把这条注入落进了当前表面
|
|
163
|
+
ctx.fire("session/event", session, { type: "compaction/end", seq: 12 });
|
|
164
|
+
const sameText = await step();
|
|
165
|
+
assert.deepEqual(sameText.messages, [], "同文本已在表面里 → 不重复追加(没有改写钩子,只能做到这一层)");
|
|
166
|
+
assert.equal(store.getContinuityProposal("s1", KIND).edge_seq, 12, "但提案仍被刷新——两半各自独立");
|
|
167
|
+
|
|
168
|
+
// 内容变了就允许再追加一次:判重按全文比较,不是"注入过就再不注入"。
|
|
169
|
+
events.push(assistantEvent(13, "内容变了"));
|
|
170
|
+
ctx.fire("session/event", session, { type: "compaction/end", seq: 14 });
|
|
171
|
+
assert.equal((await step()).messages.length, 1, "快照内容变化 → 追加新的一份");
|
|
172
|
+
|
|
173
|
+
// dispose 只能按行为断言:mockCtx 的 handler 是块体箭头函数、恒返回 undefined,拿 fire 的
|
|
174
|
+
// 返回值断言"已摘除"是恒真的(审查发现的假断言)。pre-step 监听器活着时返回 decision、
|
|
175
|
+
// 摘除后 fire 拿不到 handler,两者才是可分辨的。
|
|
176
|
+
rescue.dispose();
|
|
177
|
+
ctx.fire("session/event", session, { type: "compaction/end", seq: 15 });
|
|
178
|
+
const afterDispose = await step();
|
|
179
|
+
assert.equal(afterDispose, undefined, "dispose 后不再有 pre-step 监听器");
|
|
180
|
+
assert.equal(store.getContinuityProposal("s1", KIND).edge_seq, 14, "dispose 后不再落库(edge_seq 停在最后一条边缘)");
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
test("#249 N3:宿主表面读取抛错时退回全量判重,也不打断宿主的一步", async () => {
|
|
184
|
+
const ctx = mockCtx();
|
|
185
|
+
const stub = { saveContinuityProposal: () => ({ id: "x", created: true, dropped: false }) };
|
|
186
|
+
const rescue = createContinuityRescue(ctx, stub);
|
|
187
|
+
const session = fakeSession("s1", [userEvent(1, "指令")]);
|
|
188
|
+
// 宿主的 `surface.nodes` 是会抛的 getter(它自己内部的 projection 断言)。判重读不到就退回
|
|
189
|
+
// 全量:可选的抢救功能绝不能因为读宿主状态失败而把 agent 的这一步变失败。
|
|
190
|
+
Object.defineProperty(session, "surface", {
|
|
191
|
+
get() {
|
|
192
|
+
throw new Error('session message projection "X" was removed or replaced');
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
ctx.fire("session/event", session, { type: "compaction/end", seq: 3 });
|
|
196
|
+
const decision = await ctx.fire("agent/pre-step", { agent: { session } }, async () => ({ kind: "enter", messages: [] }));
|
|
197
|
+
assert.equal(decision.messages.length, 1, "读不到表面不该 reject 这一步,注入照做");
|
|
198
|
+
rescue.dispose();
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
test("#249 N3:压缩事件不带 seq 时仍要注入(「有没有边缘」不等于「序号的真假」)", async () => {
|
|
202
|
+
const store = createStore(":memory:");
|
|
203
|
+
const ctx = mockCtx();
|
|
204
|
+
const rescue = createContinuityRescue(ctx, store);
|
|
205
|
+
const session = fakeSession("s1", [userEvent(1, "指令")]);
|
|
206
|
+
// 边缘只记「有没有」这一件事,序号允许为 null。把「取到的值」当真假判断(`if (!edge.get(id))`
|
|
207
|
+
// 那种写法)会把这条边缘当成不存在:既不注入,也不清理。
|
|
208
|
+
ctx.fire("session/event", session, { type: "compaction/end" });
|
|
209
|
+
const decision = await ctx.fire("agent/pre-step", { agent: { session } }, async () => ({ kind: "enter", messages: [] }));
|
|
210
|
+
assert.equal(decision.messages.length, 1, "序号缺失不影响注入");
|
|
211
|
+
assert.equal(store.getContinuityProposal("s1", KIND).edge_seq, null, "序号缺失就如实记 null,不编一个数");
|
|
212
|
+
rescue.dispose();
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
test("#249 N3:判重按当前表面——被折叠出表面的旧注入不算「已注入」", async () => {
|
|
216
|
+
const store = createStore(":memory:");
|
|
217
|
+
const ctx = mockCtx();
|
|
218
|
+
const rescue = createContinuityRescue(ctx, store);
|
|
219
|
+
const old = renderContinuityNotice({ currentWork: "指令", nextStep: "回复" });
|
|
220
|
+
// seq 0 是上一次注入的快照:还在 append-only 日志里,但已被一次压缩 replace 折叠出表面。
|
|
221
|
+
const events = [
|
|
222
|
+
{ type: "user/message", seq: 0, data: { content: [{ type: "text", text: old }], source: { kind: "plugin", plugin: "dsh-mneme" } } },
|
|
223
|
+
userEvent(1, "指令"),
|
|
224
|
+
assistantEvent(2, "回复")
|
|
225
|
+
];
|
|
226
|
+
const session = fakeSession("s1", events, [1, 2]);
|
|
227
|
+
ctx.fire("session/event", session, { type: "compaction/end", seq: 9 });
|
|
228
|
+
const decision = await ctx.fire("agent/pre-step", { agent: { session } }, async () => ({ kind: "enter", messages: [] }));
|
|
229
|
+
// 按日志判重会在这里静默跳过(同文本命中已折叠的旧事件),注入那一半就空了一次;
|
|
230
|
+
// 按表面判重才补得上——这正是双落点的 ② 存在的理由。
|
|
231
|
+
assert.equal(decision.messages.length, 1, "日志里有同文本、表面里没有 → 这一次仍要补");
|
|
232
|
+
rescue.dispose();
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
test("#249 N3:落库失败不打断宿主的一步(本函数的不变量)", async () => {
|
|
236
|
+
const ctx = mockCtx();
|
|
237
|
+
const broken = { saveContinuityProposal() { throw new Error("boom"); } };
|
|
238
|
+
const rescue = createContinuityRescue(ctx, broken);
|
|
239
|
+
const session = fakeSession("s1", [userEvent(1, "指令")]);
|
|
240
|
+
ctx.fire("session/event", session, { type: "compaction/end", seq: 3 });
|
|
241
|
+
const decision = await ctx.fire("agent/pre-step", { agent: { session } }, async () => ({ kind: "enter", messages: [] }));
|
|
242
|
+
// 写不进去也要把注入那一半做出来:追加不依赖库;更关键的是可选插件的故障绝不该把
|
|
243
|
+
// agent 的这一步变成失败(磁盘满 / busy 超时 / 库损坏都会走到这里)。
|
|
244
|
+
assert.equal(decision.messages.length, 1, "库写不进去,注入那一半照做");
|
|
245
|
+
assert.equal(ctx.warnings.length, 1, "失败要留痕,不静默");
|
|
246
|
+
assert.match(ctx.warnings[0], /not recorded/);
|
|
247
|
+
rescue.dispose();
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
test("#249 N3:队列满的丢弃必须留痕(§8 触发率口径)", async () => {
|
|
251
|
+
const store = createStore(":memory:");
|
|
252
|
+
const ctx = mockCtx();
|
|
253
|
+
const rescue = createContinuityRescue(ctx, store);
|
|
254
|
+
for (let i = 0; i < 200; i += 1) {
|
|
255
|
+
store.saveContinuityProposal({ sessionId: `filler-${i}`, kind: KIND, currentWork: "占位" });
|
|
256
|
+
}
|
|
257
|
+
const session = fakeSession("s1", [userEvent(1, "指令")]);
|
|
258
|
+
ctx.fire("session/event", session, { type: "compaction/end", seq: 3 });
|
|
259
|
+
const decision = await ctx.fire("agent/pre-step", { agent: { session } }, async () => ({ kind: "enter", messages: [] }));
|
|
260
|
+
assert.equal(store.getContinuityProposal("s1", KIND), null, "满队列弃新:本次触发不落库");
|
|
261
|
+
assert.equal(decision.messages.length, 1, "但注入那一半照做(两半各自独立)");
|
|
262
|
+
assert.ok(ctx.warnings.some((w) => w.includes("queue full")), "丢弃是静默失效的那一类,必须报出来");
|
|
263
|
+
rescue.dispose();
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
test("#249 N3:门控不调模型(静态锁)与挂载点的父开关在前", () => {
|
|
267
|
+
// §11「门控不调 LLM」:本模块只 import 消息工厂。静态锁而不是运行时断言——真去调模型的
|
|
268
|
+
// 回归不会从返回值露出来,只会让这一步变慢、变贵。
|
|
269
|
+
const src = readFileSync(new URL("../src/continuity.js", import.meta.url), "utf8");
|
|
270
|
+
assert.deepEqual(src.match(/^import .*$/gm), ['import { createUserMessage } from "@deepseek-ai/dsh-llm";']);
|
|
271
|
+
// 挂载点(`src/index.js`)是本功能唯一的生产入口:父开关必须在前、子项在后,否则「默认关」
|
|
272
|
+
// 只剩 Config 里的默认值撑着,接线漏一次没人发现。
|
|
273
|
+
const index = readFileSync(new URL("../src/index.js", import.meta.url), "utf8");
|
|
274
|
+
assert.match(index, /if \(cfg\.autoInject && injectChildEnabled\(cfg, "continuityRescueEnabled"\)\) \{/);
|
|
275
|
+
assert.match(index, /disposers\.push\(createContinuityRescue\(ctx, store\)\)/);
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
test("#249 N3:reject 放行且不消费边缘;缺 session 不抛", async () => {
|
|
279
|
+
const store = createStore(":memory:");
|
|
280
|
+
const ctx = mockCtx();
|
|
281
|
+
const rescue = createContinuityRescue(ctx, store);
|
|
282
|
+
const session = fakeSession("s1", [userEvent(1, "指令")]);
|
|
283
|
+
ctx.fire("session/event", session, { type: "compaction/end", seq: 3 });
|
|
284
|
+
|
|
285
|
+
const rejected = { kind: "reject", feedback: "no" };
|
|
286
|
+
const viaReject = await ctx.fire("agent/pre-step", { agent: { session } }, async () => rejected);
|
|
287
|
+
assert.equal(viaReject, rejected, "reject 原样返回——这一步不该发生,塞消息没有意义");
|
|
288
|
+
assert.equal(store.countContinuityProposals(), 0, "边缘没被消费");
|
|
289
|
+
const after = await ctx.fire("agent/pre-step", { agent: { session } }, async () => ({ kind: "enter", messages: [] }));
|
|
290
|
+
assert.equal(after.messages.length, 1, "边缘留给下一次真正的步骤");
|
|
291
|
+
|
|
292
|
+
ctx.fire("session/event", fakeSession("s2", []), { type: "compaction/end", seq: 4 });
|
|
293
|
+
const noAgent = await ctx.fire("agent/pre-step", {}, async () => ({ kind: "enter", messages: [] }));
|
|
294
|
+
assert.deepEqual(noAgent.messages, [], "拿不到 session 就什么都不做");
|
|
295
|
+
rescue.dispose();
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
test("#249 N3:契约——默认关、父关不生效、轻量档压掉、降级规则只在开启时进描述", () => {
|
|
299
|
+
assert.equal(Config({}).continuityRescueEnabled, false, "新增注入表面 → opt-in 默认关");
|
|
300
|
+
assert.equal(applyLightModePreset({ lightMode: true }).continuityRescueEnabled, false, "轻量档压掉(同 injectGuidanceEnabled)");
|
|
301
|
+
assert.equal(injectChildEnabled({ autoInject: false, continuityRescueEnabled: true }, "continuityRescueEnabled"), false, "父关则不生效");
|
|
302
|
+
|
|
303
|
+
const plain = toolDescriptions({ continuityRescueEnabled: false });
|
|
304
|
+
const on = toolDescriptions({ autoInject: true, continuityRescueEnabled: true });
|
|
305
|
+
const gated = toolDescriptions({ autoInject: false, continuityRescueEnabled: true });
|
|
306
|
+
assert.notEqual(on.get("memory_save"), plain.get("memory_save"), "开启时 memory_save 描述补降级规则");
|
|
307
|
+
assert.equal(gated.get("memory_save"), plain.get("memory_save"), "父关 → 描述逐字节回到未开状态");
|
|
308
|
+
assert.equal(on.get("memory_search"), plain.get("memory_search"), "其它工具的描述不受牵连");
|
|
309
|
+
// 降级规则本身是可读的持久规则(宿主无压缩前时机时由 agent 自判),不是一句口号。
|
|
310
|
+
for (const field of ["current_work", "next_step"]) {
|
|
311
|
+
assert.ok(on.get("memory_save").includes(field), `降级规则要点名 ${field}`);
|
|
312
|
+
}
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
test("#249 N3:存量库启动即建表(幂等迁移),重开后提案还在", () => {
|
|
316
|
+
const dir = mkdtempSync(join(tmpdir(), "mneme-continuity-"));
|
|
317
|
+
const path = join(dir, "memory.db");
|
|
318
|
+
let store;
|
|
319
|
+
let reopened;
|
|
320
|
+
try {
|
|
321
|
+
store = createStore(path);
|
|
322
|
+
store.saveContinuityProposal({ sessionId: "s1", kind: KIND, currentWork: "重启前" });
|
|
323
|
+
assert.equal(store.getContinuityProposal("s1", KIND).current_work, "重启前");
|
|
324
|
+
reopened = createStore(path);
|
|
325
|
+
assert.equal(reopened.getContinuityProposal("s1", KIND).current_work, "重启前", "SCHEMA 的 IF NOT EXISTS 让存量库重开也不用迁移");
|
|
326
|
+
assert.equal(reopened.countContinuityProposals({ status: "pending" }), 1);
|
|
327
|
+
} finally {
|
|
328
|
+
// 先关句柄再删目录:断言失败时若还开着库,Windows 的 rmSync 会抛 EPERM 盖掉真因。
|
|
329
|
+
try { reopened?.close(); } catch { /* 关闭失败不该掩盖断言差异 */ }
|
|
330
|
+
try { store?.close(); } catch { /* 同上 */ }
|
|
331
|
+
rmSync(dir, { recursive: true, force: true });
|
|
332
|
+
}
|
|
333
|
+
});
|