@modusensus/dsh-mneme 0.8.13 → 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 -626
- 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 -0
- 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 -572
- 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 -836
- 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 -671
- 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 -0
- package/lib/sensitive-scan.js +209 -209
- package/lib/serve.js +215 -0
- package/lib/service.js +2663 -2596
- package/lib/settings.js +517 -509
- 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 +8 -4
- 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 -0
- 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 -572
- package/src/api.js +1437 -1433
- package/src/bootstrap.js +211 -211
- package/src/commands.js +95 -95
- package/src/config.js +880 -836
- 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 -671
- 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 -0
- package/src/sensitive-scan.js +209 -209
- package/src/serve.js +215 -0
- package/src/service.js +2663 -2596
- package/src/settings.js +517 -509
- 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 -1688
- 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 -0
- 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 -61
- 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 -343
- 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 -0
- package/test/serve.test.js +196 -0
- 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 -544
- 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,194 +1,194 @@
|
|
|
1
|
-
import test from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
import { createDreamScheduler } from "../src/dream.js";
|
|
4
|
-
import { createStore } from "../src/store.js";
|
|
5
|
-
import { createService } from "../src/service.js";
|
|
6
|
-
|
|
7
|
-
// Issue #239(第 4 项,错峰队列)镜像到巩固:dreamPeakHours / dreamPeakMaxDeferMinutes。
|
|
8
|
-
//
|
|
9
|
-
// 这些用例锁的是「排程语义」,不是实现细节——它们防的回归是:
|
|
10
|
-
// ① 错峰闸门被写反/失效 → 高峰期又开始做梦(省钱能力静默消失);
|
|
11
|
-
// ② 顺延路径把 skip 做成静默 → 用户看到「不再巩固了」却查不出原因;
|
|
12
|
-
// ③ 顺延没有上限 → 长高峰(如全天)把巩固永久饿死;
|
|
13
|
-
// ④ 非法时段串被当成「全天高峰」→ 一个笔误把功能整个停掉;
|
|
14
|
-
// ⑤ dispose 漏清顺延定时器 → 进程关掉后仍触发一次巩固。
|
|
15
|
-
//
|
|
16
|
-
// 时钟与定时器全部注入(同 src/dream/sleep.js 的房型):排程不绑死真实时钟,
|
|
17
|
-
// 「高峰顺延 → 非高峰补跑」才能被确定性覆盖,也不会在 CI 上留下真实等待。
|
|
18
|
-
function dreamSetup() {
|
|
19
|
-
const store = createStore(":memory:");
|
|
20
|
-
const service = createService({ store, mirror: null, config: {} });
|
|
21
|
-
return { store, service };
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/** 本地墙钟时间:用本地时区构造,getHours() 在 ubuntu/windows CI 上都等于入参。 */
|
|
25
|
-
function at(hour, minute = 0) {
|
|
26
|
-
return new Date(2026, 5, 15, hour, minute, 0, 0).getTime();
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function fakeClock(startMs) {
|
|
30
|
-
let nowMs = startMs;
|
|
31
|
-
const timers = [];
|
|
32
|
-
let seq = 1;
|
|
33
|
-
return {
|
|
34
|
-
now: () => nowMs,
|
|
35
|
-
setNow: (v) => { nowMs = v; },
|
|
36
|
-
timers,
|
|
37
|
-
setTimeoutFn: (fn, delay) => { const t = { id: seq++, at: nowMs + delay, fn }; timers.push(t); return t.id; },
|
|
38
|
-
clearTimeoutFn: (id) => { const i = timers.findIndex((t) => t.id === id); if (i >= 0) timers.splice(i, 1); }
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/** 造出「超过阈值」的写入量(thresholdCount=2)。 */
|
|
43
|
-
function fill(service, n = 2) {
|
|
44
|
-
for (let i = 0; i < n; i++) service.saveWithDedupe({ type: "project", title: `m${i}`, content: "x".repeat(50) });
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
test("dreamPeakHours: 命中高峰不调模型,登记 skip 审计并顺延", () => {
|
|
48
|
-
const { store, service } = dreamSetup();
|
|
49
|
-
const clock = fakeClock(at(10, 0)); // 本地 10:00,落在 09:00-18:00 内
|
|
50
|
-
let runs = 0;
|
|
51
|
-
const audits = [];
|
|
52
|
-
const dream = createDreamScheduler({
|
|
53
|
-
onRun: async () => { runs++; return { ok: true }; },
|
|
54
|
-
thresholdCount: 2, thresholdChars: 5000, delayMs: 0,
|
|
55
|
-
peakHours: "09:00-18:00",
|
|
56
|
-
peakMaxDeferMinutes: 0, // 不设上限:顺延应落在真正的高峰结束时刻
|
|
57
|
-
now: clock.now, setTimeoutFn: clock.setTimeoutFn, clearTimeoutFn: clock.clearTimeoutFn,
|
|
58
|
-
auditPeakSkip: (info) => audits.push(info)
|
|
59
|
-
});
|
|
60
|
-
fill(service);
|
|
61
|
-
assert.equal(dream.maybeSchedule(service), false, "高峰内不排「马上跑」");
|
|
62
|
-
assert.equal(runs, 0, "高峰内绝不调 LLM");
|
|
63
|
-
assert.equal(audits.length, 1, "skip 必须留痕,否则「为什么不再巩固」不可观测");
|
|
64
|
-
assert.equal(audits[0].count, 2, "审计带上窗口里积了多少(阈值继续累积,不是丢弃)");
|
|
65
|
-
assert.equal(clock.timers.length, 1, "挂了一个顺延定时器");
|
|
66
|
-
assert.equal(clock.timers[0].at, at(18, 0), "顺延到最近的高峰结束时刻");
|
|
67
|
-
store.close();
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
test("dreamPeakHours: 顺延到点后补跑(非高峰真的会跑)", async () => {
|
|
71
|
-
const { store, service } = dreamSetup();
|
|
72
|
-
const clock = fakeClock(at(10, 0));
|
|
73
|
-
let runs = 0;
|
|
74
|
-
const dream = createDreamScheduler({
|
|
75
|
-
onRun: async () => { runs++; return { ok: true }; },
|
|
76
|
-
thresholdCount: 2, thresholdChars: 5000, delayMs: 0,
|
|
77
|
-
peakHours: "09:00-18:00",
|
|
78
|
-
now: clock.now, setTimeoutFn: clock.setTimeoutFn, clearTimeoutFn: clock.clearTimeoutFn,
|
|
79
|
-
auditPeakSkip: () => {}
|
|
80
|
-
});
|
|
81
|
-
fill(service);
|
|
82
|
-
dream.maybeSchedule(service);
|
|
83
|
-
const deferred = clock.timers[0];
|
|
84
|
-
clock.setNow(deferred.at); // 走到 18:00
|
|
85
|
-
await deferred.fn();
|
|
86
|
-
assert.equal(runs, 1, "出高峰后补跑一次");
|
|
87
|
-
store.close();
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
test("dreamPeakHours: 重复触发不叠加顺延定时器", () => {
|
|
91
|
-
const { store, service } = dreamSetup();
|
|
92
|
-
const clock = fakeClock(at(10, 0));
|
|
93
|
-
let audits = 0;
|
|
94
|
-
const dream = createDreamScheduler({
|
|
95
|
-
onRun: async () => ({ ok: true }),
|
|
96
|
-
thresholdCount: 2, thresholdChars: 5000, delayMs: 0,
|
|
97
|
-
peakHours: "09:00-18:00",
|
|
98
|
-
now: clock.now, setTimeoutFn: clock.setTimeoutFn, clearTimeoutFn: clock.clearTimeoutFn,
|
|
99
|
-
auditPeakSkip: () => { audits++; }
|
|
100
|
-
});
|
|
101
|
-
fill(service);
|
|
102
|
-
dream.maybeSchedule(service);
|
|
103
|
-
dream.maybeSchedule(service); // 顺延期间又来一次写入触发
|
|
104
|
-
assert.equal(clock.timers.length, 1, "同一时刻只有一个顺延定时器");
|
|
105
|
-
assert.equal(audits, 1, "不重复刷审计行");
|
|
106
|
-
store.close();
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
test("dreamPeakMaxDeferMinutes: 上限截断后到点照跑,长高峰不饿死巩固", async () => {
|
|
110
|
-
const { store, service } = dreamSetup();
|
|
111
|
-
const clock = fakeClock(at(10, 0));
|
|
112
|
-
let runs = 0;
|
|
113
|
-
const dream = createDreamScheduler({
|
|
114
|
-
onRun: async () => { runs++; return { ok: true }; },
|
|
115
|
-
thresholdCount: 2, thresholdChars: 5000, delayMs: 0,
|
|
116
|
-
peakHours: "00:00-23:59", // 全天高峰:不设上限就会顺延到 23:59
|
|
117
|
-
peakMaxDeferMinutes: 120,
|
|
118
|
-
now: clock.now, setTimeoutFn: clock.setTimeoutFn, clearTimeoutFn: clock.clearTimeoutFn,
|
|
119
|
-
auditPeakSkip: () => {}
|
|
120
|
-
});
|
|
121
|
-
fill(service);
|
|
122
|
-
dream.maybeSchedule(service);
|
|
123
|
-
const t = clock.timers[0];
|
|
124
|
-
assert.equal(t.at, at(12, 0), "被上限截断到 120 分钟后(而不是等到 23:59)");
|
|
125
|
-
clock.setNow(t.at);
|
|
126
|
-
await t.fn();
|
|
127
|
-
assert.equal(runs, 1, "截断放行:仍在高峰也照跑,避免无限顺延");
|
|
128
|
-
store.close();
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
test("dreamPeakHours: 非法时段串按「未配置」处理(宁可不省,也不能误停)", async () => {
|
|
132
|
-
const { store, service } = dreamSetup();
|
|
133
|
-
const clock = fakeClock(at(10, 0));
|
|
134
|
-
let runs = 0;
|
|
135
|
-
let audits = 0;
|
|
136
|
-
const dream = createDreamScheduler({
|
|
137
|
-
onRun: async () => { runs++; return { ok: true }; },
|
|
138
|
-
thresholdCount: 2, thresholdChars: 5000, delayMs: 0,
|
|
139
|
-
peakHours: "10:00-99:00", // 非法:解析失败必须回落成「关闭」
|
|
140
|
-
now: clock.now, setTimeoutFn: clock.setTimeoutFn, clearTimeoutFn: clock.clearTimeoutFn,
|
|
141
|
-
auditPeakSkip: () => { audits++; }
|
|
142
|
-
});
|
|
143
|
-
fill(service);
|
|
144
|
-
assert.equal(dream.maybeSchedule(service), true, "非法串 = 未配置 → 走原有触发路径");
|
|
145
|
-
assert.equal(audits, 0, "不写 skip 审计");
|
|
146
|
-
const t = clock.timers[0];
|
|
147
|
-
clock.setNow(t.at);
|
|
148
|
-
await t.fn();
|
|
149
|
-
assert.equal(runs, 1, "巩固照常进行");
|
|
150
|
-
store.close();
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
test("dreamPeakHours: 非高峰时段不受影响(行为与现状一致)", async () => {
|
|
154
|
-
const { store, service } = dreamSetup();
|
|
155
|
-
const clock = fakeClock(at(20, 0)); // 本地 20:00,在 09:00-18:00 之外
|
|
156
|
-
let runs = 0;
|
|
157
|
-
const dream = createDreamScheduler({
|
|
158
|
-
onRun: async () => { runs++; return { ok: true }; },
|
|
159
|
-
thresholdCount: 2, thresholdChars: 5000, delayMs: 5,
|
|
160
|
-
peakHours: "09:00-18:00",
|
|
161
|
-
now: clock.now, setTimeoutFn: clock.setTimeoutFn, clearTimeoutFn: clock.clearTimeoutFn,
|
|
162
|
-
auditPeakSkip: () => { throw new Error("非高峰不该写 skip 审计"); }
|
|
163
|
-
});
|
|
164
|
-
fill(service);
|
|
165
|
-
assert.equal(dream.maybeSchedule(service), true, "非高峰照常排程");
|
|
166
|
-
const t = clock.timers[0];
|
|
167
|
-
assert.equal(t.at, at(20, 0) + 5, "走的是 delayMs 的「马上跑」路径,不是顺延路径");
|
|
168
|
-
clock.setNow(t.at);
|
|
169
|
-
await t.fn();
|
|
170
|
-
assert.equal(runs, 1);
|
|
171
|
-
store.close();
|
|
172
|
-
});
|
|
173
|
-
|
|
174
|
-
test("dreamPeakHours: dispose 清掉顺延定时器,进程关闭后不再触发巩固", async () => {
|
|
175
|
-
const { store, service } = dreamSetup();
|
|
176
|
-
const clock = fakeClock(at(10, 0));
|
|
177
|
-
let runs = 0;
|
|
178
|
-
const dream = createDreamScheduler({
|
|
179
|
-
onRun: async () => { runs++; return { ok: true }; },
|
|
180
|
-
thresholdCount: 2, thresholdChars: 5000, delayMs: 0,
|
|
181
|
-
peakHours: "09:00-18:00",
|
|
182
|
-
now: clock.now, setTimeoutFn: clock.setTimeoutFn, clearTimeoutFn: clock.clearTimeoutFn,
|
|
183
|
-
auditPeakSkip: () => {}
|
|
184
|
-
});
|
|
185
|
-
fill(service);
|
|
186
|
-
dream.maybeSchedule(service);
|
|
187
|
-
const t = clock.timers[0];
|
|
188
|
-
await dream.dispose();
|
|
189
|
-
assert.equal(clock.timers.length, 0, "顺延定时器已清");
|
|
190
|
-
clock.setNow(t.at);
|
|
191
|
-
await t.fn(); // 模拟「定时器已经进了事件队列,清不掉」的那一帧
|
|
192
|
-
assert.equal(runs, 0, "disposed 守卫拦住补跑");
|
|
193
|
-
store.close();
|
|
194
|
-
});
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { createDreamScheduler } from "../src/dream.js";
|
|
4
|
+
import { createStore } from "../src/store.js";
|
|
5
|
+
import { createService } from "../src/service.js";
|
|
6
|
+
|
|
7
|
+
// Issue #239(第 4 项,错峰队列)镜像到巩固:dreamPeakHours / dreamPeakMaxDeferMinutes。
|
|
8
|
+
//
|
|
9
|
+
// 这些用例锁的是「排程语义」,不是实现细节——它们防的回归是:
|
|
10
|
+
// ① 错峰闸门被写反/失效 → 高峰期又开始做梦(省钱能力静默消失);
|
|
11
|
+
// ② 顺延路径把 skip 做成静默 → 用户看到「不再巩固了」却查不出原因;
|
|
12
|
+
// ③ 顺延没有上限 → 长高峰(如全天)把巩固永久饿死;
|
|
13
|
+
// ④ 非法时段串被当成「全天高峰」→ 一个笔误把功能整个停掉;
|
|
14
|
+
// ⑤ dispose 漏清顺延定时器 → 进程关掉后仍触发一次巩固。
|
|
15
|
+
//
|
|
16
|
+
// 时钟与定时器全部注入(同 src/dream/sleep.js 的房型):排程不绑死真实时钟,
|
|
17
|
+
// 「高峰顺延 → 非高峰补跑」才能被确定性覆盖,也不会在 CI 上留下真实等待。
|
|
18
|
+
function dreamSetup() {
|
|
19
|
+
const store = createStore(":memory:");
|
|
20
|
+
const service = createService({ store, mirror: null, config: {} });
|
|
21
|
+
return { store, service };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** 本地墙钟时间:用本地时区构造,getHours() 在 ubuntu/windows CI 上都等于入参。 */
|
|
25
|
+
function at(hour, minute = 0) {
|
|
26
|
+
return new Date(2026, 5, 15, hour, minute, 0, 0).getTime();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function fakeClock(startMs) {
|
|
30
|
+
let nowMs = startMs;
|
|
31
|
+
const timers = [];
|
|
32
|
+
let seq = 1;
|
|
33
|
+
return {
|
|
34
|
+
now: () => nowMs,
|
|
35
|
+
setNow: (v) => { nowMs = v; },
|
|
36
|
+
timers,
|
|
37
|
+
setTimeoutFn: (fn, delay) => { const t = { id: seq++, at: nowMs + delay, fn }; timers.push(t); return t.id; },
|
|
38
|
+
clearTimeoutFn: (id) => { const i = timers.findIndex((t) => t.id === id); if (i >= 0) timers.splice(i, 1); }
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** 造出「超过阈值」的写入量(thresholdCount=2)。 */
|
|
43
|
+
function fill(service, n = 2) {
|
|
44
|
+
for (let i = 0; i < n; i++) service.saveWithDedupe({ type: "project", title: `m${i}`, content: "x".repeat(50) });
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
test("dreamPeakHours: 命中高峰不调模型,登记 skip 审计并顺延", () => {
|
|
48
|
+
const { store, service } = dreamSetup();
|
|
49
|
+
const clock = fakeClock(at(10, 0)); // 本地 10:00,落在 09:00-18:00 内
|
|
50
|
+
let runs = 0;
|
|
51
|
+
const audits = [];
|
|
52
|
+
const dream = createDreamScheduler({
|
|
53
|
+
onRun: async () => { runs++; return { ok: true }; },
|
|
54
|
+
thresholdCount: 2, thresholdChars: 5000, delayMs: 0,
|
|
55
|
+
peakHours: "09:00-18:00",
|
|
56
|
+
peakMaxDeferMinutes: 0, // 不设上限:顺延应落在真正的高峰结束时刻
|
|
57
|
+
now: clock.now, setTimeoutFn: clock.setTimeoutFn, clearTimeoutFn: clock.clearTimeoutFn,
|
|
58
|
+
auditPeakSkip: (info) => audits.push(info)
|
|
59
|
+
});
|
|
60
|
+
fill(service);
|
|
61
|
+
assert.equal(dream.maybeSchedule(service), false, "高峰内不排「马上跑」");
|
|
62
|
+
assert.equal(runs, 0, "高峰内绝不调 LLM");
|
|
63
|
+
assert.equal(audits.length, 1, "skip 必须留痕,否则「为什么不再巩固」不可观测");
|
|
64
|
+
assert.equal(audits[0].count, 2, "审计带上窗口里积了多少(阈值继续累积,不是丢弃)");
|
|
65
|
+
assert.equal(clock.timers.length, 1, "挂了一个顺延定时器");
|
|
66
|
+
assert.equal(clock.timers[0].at, at(18, 0), "顺延到最近的高峰结束时刻");
|
|
67
|
+
store.close();
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test("dreamPeakHours: 顺延到点后补跑(非高峰真的会跑)", async () => {
|
|
71
|
+
const { store, service } = dreamSetup();
|
|
72
|
+
const clock = fakeClock(at(10, 0));
|
|
73
|
+
let runs = 0;
|
|
74
|
+
const dream = createDreamScheduler({
|
|
75
|
+
onRun: async () => { runs++; return { ok: true }; },
|
|
76
|
+
thresholdCount: 2, thresholdChars: 5000, delayMs: 0,
|
|
77
|
+
peakHours: "09:00-18:00",
|
|
78
|
+
now: clock.now, setTimeoutFn: clock.setTimeoutFn, clearTimeoutFn: clock.clearTimeoutFn,
|
|
79
|
+
auditPeakSkip: () => {}
|
|
80
|
+
});
|
|
81
|
+
fill(service);
|
|
82
|
+
dream.maybeSchedule(service);
|
|
83
|
+
const deferred = clock.timers[0];
|
|
84
|
+
clock.setNow(deferred.at); // 走到 18:00
|
|
85
|
+
await deferred.fn();
|
|
86
|
+
assert.equal(runs, 1, "出高峰后补跑一次");
|
|
87
|
+
store.close();
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test("dreamPeakHours: 重复触发不叠加顺延定时器", () => {
|
|
91
|
+
const { store, service } = dreamSetup();
|
|
92
|
+
const clock = fakeClock(at(10, 0));
|
|
93
|
+
let audits = 0;
|
|
94
|
+
const dream = createDreamScheduler({
|
|
95
|
+
onRun: async () => ({ ok: true }),
|
|
96
|
+
thresholdCount: 2, thresholdChars: 5000, delayMs: 0,
|
|
97
|
+
peakHours: "09:00-18:00",
|
|
98
|
+
now: clock.now, setTimeoutFn: clock.setTimeoutFn, clearTimeoutFn: clock.clearTimeoutFn,
|
|
99
|
+
auditPeakSkip: () => { audits++; }
|
|
100
|
+
});
|
|
101
|
+
fill(service);
|
|
102
|
+
dream.maybeSchedule(service);
|
|
103
|
+
dream.maybeSchedule(service); // 顺延期间又来一次写入触发
|
|
104
|
+
assert.equal(clock.timers.length, 1, "同一时刻只有一个顺延定时器");
|
|
105
|
+
assert.equal(audits, 1, "不重复刷审计行");
|
|
106
|
+
store.close();
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test("dreamPeakMaxDeferMinutes: 上限截断后到点照跑,长高峰不饿死巩固", async () => {
|
|
110
|
+
const { store, service } = dreamSetup();
|
|
111
|
+
const clock = fakeClock(at(10, 0));
|
|
112
|
+
let runs = 0;
|
|
113
|
+
const dream = createDreamScheduler({
|
|
114
|
+
onRun: async () => { runs++; return { ok: true }; },
|
|
115
|
+
thresholdCount: 2, thresholdChars: 5000, delayMs: 0,
|
|
116
|
+
peakHours: "00:00-23:59", // 全天高峰:不设上限就会顺延到 23:59
|
|
117
|
+
peakMaxDeferMinutes: 120,
|
|
118
|
+
now: clock.now, setTimeoutFn: clock.setTimeoutFn, clearTimeoutFn: clock.clearTimeoutFn,
|
|
119
|
+
auditPeakSkip: () => {}
|
|
120
|
+
});
|
|
121
|
+
fill(service);
|
|
122
|
+
dream.maybeSchedule(service);
|
|
123
|
+
const t = clock.timers[0];
|
|
124
|
+
assert.equal(t.at, at(12, 0), "被上限截断到 120 分钟后(而不是等到 23:59)");
|
|
125
|
+
clock.setNow(t.at);
|
|
126
|
+
await t.fn();
|
|
127
|
+
assert.equal(runs, 1, "截断放行:仍在高峰也照跑,避免无限顺延");
|
|
128
|
+
store.close();
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
test("dreamPeakHours: 非法时段串按「未配置」处理(宁可不省,也不能误停)", async () => {
|
|
132
|
+
const { store, service } = dreamSetup();
|
|
133
|
+
const clock = fakeClock(at(10, 0));
|
|
134
|
+
let runs = 0;
|
|
135
|
+
let audits = 0;
|
|
136
|
+
const dream = createDreamScheduler({
|
|
137
|
+
onRun: async () => { runs++; return { ok: true }; },
|
|
138
|
+
thresholdCount: 2, thresholdChars: 5000, delayMs: 0,
|
|
139
|
+
peakHours: "10:00-99:00", // 非法:解析失败必须回落成「关闭」
|
|
140
|
+
now: clock.now, setTimeoutFn: clock.setTimeoutFn, clearTimeoutFn: clock.clearTimeoutFn,
|
|
141
|
+
auditPeakSkip: () => { audits++; }
|
|
142
|
+
});
|
|
143
|
+
fill(service);
|
|
144
|
+
assert.equal(dream.maybeSchedule(service), true, "非法串 = 未配置 → 走原有触发路径");
|
|
145
|
+
assert.equal(audits, 0, "不写 skip 审计");
|
|
146
|
+
const t = clock.timers[0];
|
|
147
|
+
clock.setNow(t.at);
|
|
148
|
+
await t.fn();
|
|
149
|
+
assert.equal(runs, 1, "巩固照常进行");
|
|
150
|
+
store.close();
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test("dreamPeakHours: 非高峰时段不受影响(行为与现状一致)", async () => {
|
|
154
|
+
const { store, service } = dreamSetup();
|
|
155
|
+
const clock = fakeClock(at(20, 0)); // 本地 20:00,在 09:00-18:00 之外
|
|
156
|
+
let runs = 0;
|
|
157
|
+
const dream = createDreamScheduler({
|
|
158
|
+
onRun: async () => { runs++; return { ok: true }; },
|
|
159
|
+
thresholdCount: 2, thresholdChars: 5000, delayMs: 5,
|
|
160
|
+
peakHours: "09:00-18:00",
|
|
161
|
+
now: clock.now, setTimeoutFn: clock.setTimeoutFn, clearTimeoutFn: clock.clearTimeoutFn,
|
|
162
|
+
auditPeakSkip: () => { throw new Error("非高峰不该写 skip 审计"); }
|
|
163
|
+
});
|
|
164
|
+
fill(service);
|
|
165
|
+
assert.equal(dream.maybeSchedule(service), true, "非高峰照常排程");
|
|
166
|
+
const t = clock.timers[0];
|
|
167
|
+
assert.equal(t.at, at(20, 0) + 5, "走的是 delayMs 的「马上跑」路径,不是顺延路径");
|
|
168
|
+
clock.setNow(t.at);
|
|
169
|
+
await t.fn();
|
|
170
|
+
assert.equal(runs, 1);
|
|
171
|
+
store.close();
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
test("dreamPeakHours: dispose 清掉顺延定时器,进程关闭后不再触发巩固", async () => {
|
|
175
|
+
const { store, service } = dreamSetup();
|
|
176
|
+
const clock = fakeClock(at(10, 0));
|
|
177
|
+
let runs = 0;
|
|
178
|
+
const dream = createDreamScheduler({
|
|
179
|
+
onRun: async () => { runs++; return { ok: true }; },
|
|
180
|
+
thresholdCount: 2, thresholdChars: 5000, delayMs: 0,
|
|
181
|
+
peakHours: "09:00-18:00",
|
|
182
|
+
now: clock.now, setTimeoutFn: clock.setTimeoutFn, clearTimeoutFn: clock.clearTimeoutFn,
|
|
183
|
+
auditPeakSkip: () => {}
|
|
184
|
+
});
|
|
185
|
+
fill(service);
|
|
186
|
+
dream.maybeSchedule(service);
|
|
187
|
+
const t = clock.timers[0];
|
|
188
|
+
await dream.dispose();
|
|
189
|
+
assert.equal(clock.timers.length, 0, "顺延定时器已清");
|
|
190
|
+
clock.setNow(t.at);
|
|
191
|
+
await t.fn(); // 模拟「定时器已经进了事件队列,清不掉」的那一帧
|
|
192
|
+
assert.equal(runs, 0, "disposed 守卫拦住补跑");
|
|
193
|
+
store.close();
|
|
194
|
+
});
|