@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/sleep.test.js
CHANGED
|
@@ -1,527 +1,527 @@
|
|
|
1
|
-
import test from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
import { createSleepScheduler, runSleep } from "../src/dream/sleep.js";
|
|
4
|
-
import { Config } from "../src/config.js";
|
|
5
|
-
import { createStore } from "../src/store.js";
|
|
6
|
-
import { createService } from "../src/service.js";
|
|
7
|
-
import { createVectorIndex } from "../src/vector-index.js";
|
|
8
|
-
|
|
9
|
-
// Mock embedder: every query maps to [1,0,0] so vectors are identical unless a
|
|
10
|
-
// test pre-seeds a custom vector via vectorIndex.saveEmbedding.
|
|
11
|
-
const embedder = {
|
|
12
|
-
embedSingle: async () => [1, 0, 0],
|
|
13
|
-
embed: async () => [1, 0, 0],
|
|
14
|
-
schedule: () => {},
|
|
15
|
-
modelHash: "mock#1",
|
|
16
|
-
dimension: 3
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
// Deterministic LLM: onConsolidation(userText) => decisions JSON string.
|
|
20
|
-
function mockCtx(onConsolidation, selection = { provider: "mock", model: "sleep-model" }) {
|
|
21
|
-
return {
|
|
22
|
-
logger: { warn: () => {}, info: () => {} },
|
|
23
|
-
agentDefaultModel: { currentSelection: () => selection },
|
|
24
|
-
llm: {
|
|
25
|
-
async *stream(options) {
|
|
26
|
-
const userText = options.messages.find((m) => m.role === "user")?.content?.[0]?.text ?? "";
|
|
27
|
-
const reply = onConsolidation ? onConsolidation(userText) : "[]";
|
|
28
|
-
yield { type: "text-delta", index: 0, text: reply };
|
|
29
|
-
yield { type: "finish", reason: { kind: "stop" } };
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function baseConfig(overrides = {}) {
|
|
36
|
-
return {
|
|
37
|
-
sleepModeEnabled: true,
|
|
38
|
-
sleepIdleMinutes: 5,
|
|
39
|
-
sleepMinIntervalHours: 8,
|
|
40
|
-
sleepConflictStrictness: "normal",
|
|
41
|
-
sleepArchiveDays: 30,
|
|
42
|
-
sleepCompressDays: 90,
|
|
43
|
-
sleepPatternMinMemories: 100,
|
|
44
|
-
sleepMaxPatternPerRun: 3,
|
|
45
|
-
...overrides
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// v0.7.0 heat 双保护默认保守:默认 heatTypeDecay 下 project 记忆 40 天热值
|
|
50
|
-
// ≈0.55、100 天 ≈0.28,始终高于 sleepHeatThreshold(0.05),demotion 不触发。
|
|
51
|
-
// 降级语义测试把 project 的 λ 调快到 0.02(40 天热值≈0.03),复现"时间窗冷态
|
|
52
|
-
// 即降级"的旧路径;默认保守语义由 sleep-heat.test.js 单独覆盖。
|
|
53
|
-
function demotionConfig(overrides = {}) {
|
|
54
|
-
return baseConfig({ heatTypeDecay: { project: 0.02 }, ...overrides });
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function setup() {
|
|
58
|
-
const store = createStore(":memory:");
|
|
59
|
-
const service = createService({ store, mirror: null, config: {} });
|
|
60
|
-
const vectorIndex = createVectorIndex({ store });
|
|
61
|
-
service.setEmbedder(embedder);
|
|
62
|
-
service.setVectorIndex(vectorIndex);
|
|
63
|
-
return { store, service, vectorIndex };
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function makeMemory(service, title, content, type = "project") {
|
|
67
|
-
return service.saveWithDedupe({ type, title, content, importance: 3 }).memory;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// ------------------------------------------------------------ scheduler
|
|
71
|
-
|
|
72
|
-
test("sleep: scheduler does not run when disabled", () => {
|
|
73
|
-
const { service, store } = setup();
|
|
74
|
-
const sched = createSleepScheduler({
|
|
75
|
-
service, config: baseConfig({ sleepModeEnabled: false }), logger: { warn: () => {} },
|
|
76
|
-
onRun: async () => ({ ok: true }),
|
|
77
|
-
now: () => 1_000_000, setTimeoutFn: () => 1, clearTimeoutFn: () => {}
|
|
78
|
-
});
|
|
79
|
-
assert.equal(sched.shouldRun(1_000_000 + 60 * 60000), false, "disabled never schedules");
|
|
80
|
-
store.close();
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
test("sleep: scheduler fires once idle window elapses", () => {
|
|
84
|
-
const { service, store } = setup();
|
|
85
|
-
const sched = createSleepScheduler({
|
|
86
|
-
service, config: baseConfig(), logger: { warn: () => {} },
|
|
87
|
-
onRun: async () => ({ ok: true }),
|
|
88
|
-
now: () => 1_000_000, setTimeoutFn: () => 1, clearTimeoutFn: () => {}
|
|
89
|
-
});
|
|
90
|
-
assert.equal(sched.shouldRun(1_000_000 + 4 * 60000), false, "still within idle window");
|
|
91
|
-
assert.equal(sched.shouldRun(1_000_000 + 6 * 60000), true, "idle window elapsed");
|
|
92
|
-
store.close();
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
test("sleep: noteWrite resets the idle clock (stale-timer guard)", () => {
|
|
96
|
-
const { service, store } = setup();
|
|
97
|
-
let now = 1_000_000;
|
|
98
|
-
const sched = createSleepScheduler({
|
|
99
|
-
service, config: baseConfig(), logger: { warn: () => {} },
|
|
100
|
-
onRun: async () => ({ ok: true }),
|
|
101
|
-
now: () => now, setTimeoutFn: () => 1, clearTimeoutFn: () => {}
|
|
102
|
-
});
|
|
103
|
-
assert.equal(sched.shouldRun(now + 6 * 60000), true, "idle after 6min");
|
|
104
|
-
now = 1_000_000 + 6 * 60000;
|
|
105
|
-
sched.noteWrite();
|
|
106
|
-
assert.equal(sched.shouldRun(now), false, "write resets idle clock");
|
|
107
|
-
assert.equal(sched.shouldRun(now + 4 * 60000), false, "still idle-pending after reset");
|
|
108
|
-
assert.equal(sched.shouldRun(now + 6 * 60000), true, "idle again after fresh window");
|
|
109
|
-
store.close();
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
test("sleep: first-ever run is not blocked by min interval (lastRunAt=0)", () => {
|
|
113
|
-
const { service, store } = setup();
|
|
114
|
-
const sched = createSleepScheduler({
|
|
115
|
-
service, config: baseConfig(), logger: { warn: () => {} },
|
|
116
|
-
onRun: async () => ({ ok: true }),
|
|
117
|
-
now: () => 1_000_000, setTimeoutFn: () => 1, clearTimeoutFn: () => {}
|
|
118
|
-
});
|
|
119
|
-
assert.equal(sched.shouldRun(1_000_000 + 6 * 60000), true, "first run allowed");
|
|
120
|
-
store.close();
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
test("sleep: min interval blocks a re-run within the window, allows after", async () => {
|
|
124
|
-
const { service, store } = setup();
|
|
125
|
-
let now = 1_000_000;
|
|
126
|
-
const sched = createSleepScheduler({
|
|
127
|
-
service, config: baseConfig(), logger: { warn: () => {} },
|
|
128
|
-
onRun: async () => ({ ok: true }),
|
|
129
|
-
now: () => now, setTimeoutFn: () => 1, clearTimeoutFn: () => {}
|
|
130
|
-
});
|
|
131
|
-
now = 1_000_000 + 6 * 60000; // idle satisfied
|
|
132
|
-
const ok = await sched.maybeSchedule();
|
|
133
|
-
assert.equal(ok, true, "first run executed");
|
|
134
|
-
assert.equal(sched.shouldRun(now + 1 * 3600000), false, "1h later still inside 8h min interval");
|
|
135
|
-
assert.equal(sched.shouldRun(now + 9 * 3600000), true, "9h later past min interval → allowed");
|
|
136
|
-
store.close();
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
test("sleep: maybeSchedule enqueues via service and serializes with other work", async () => {
|
|
140
|
-
const { service, store } = setup();
|
|
141
|
-
const config = baseConfig();
|
|
142
|
-
let now = 1_000_000;
|
|
143
|
-
let order = [];
|
|
144
|
-
const sched = createSleepScheduler({
|
|
145
|
-
service, config, logger: { warn: () => {} },
|
|
146
|
-
onRun: async () => { order.push("sleep"); return { ok: true, applied: 7 }; },
|
|
147
|
-
now: () => now, setTimeoutFn: () => 1, clearTimeoutFn: () => {}
|
|
148
|
-
});
|
|
149
|
-
now = 1_000_000 + 6 * 60000;
|
|
150
|
-
const p1 = service.enqueue(async () => { order.push("a"); });
|
|
151
|
-
const p2 = sched.maybeSchedule();
|
|
152
|
-
await p1;
|
|
153
|
-
const result = await p2;
|
|
154
|
-
assert.equal(result, true, "run result propagated through enqueue");
|
|
155
|
-
assert.deepEqual(order, ["a", "sleep"], "sleep run serialized behind queued work");
|
|
156
|
-
store.close();
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
test("sleep: dispose clears the timer and prevents further runs", async () => {
|
|
160
|
-
const { service, store } = setup();
|
|
161
|
-
let cleared = false;
|
|
162
|
-
const sched = createSleepScheduler({
|
|
163
|
-
service, config: baseConfig(), logger: { warn: () => {} },
|
|
164
|
-
onRun: async () => ({ ok: true }),
|
|
165
|
-
now: () => 1_000_000,
|
|
166
|
-
setTimeoutFn: () => 1,
|
|
167
|
-
clearTimeoutFn: () => { cleared = true; }
|
|
168
|
-
});
|
|
169
|
-
// #187 起构造即挂表——dispose 必须能清掉这张(以及 noteWrite 重挂过的任何)表。
|
|
170
|
-
assert.equal(cleared, false, "timer armed at construction, not yet cleared");
|
|
171
|
-
await sched.dispose();
|
|
172
|
-
assert.equal(cleared, true, "idle timer cleared on dispose");
|
|
173
|
-
assert.equal(sched.shouldRun(1_000_000 + 6 * 60000), false, "disposed never runs");
|
|
174
|
-
store.close();
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
// ------------------------------------------------------------ runSleep phases
|
|
178
|
-
|
|
179
|
-
test("sleep: demotion shrinks cold memory to summary, keeps _full_content", async () => {
|
|
180
|
-
const { service, store } = setup();
|
|
181
|
-
const now = Date.now();
|
|
182
|
-
const m = makeMemory(service, "cold", "原内容".repeat(60), "project");
|
|
183
|
-
service.touchLastAccess(m.id, new Date(now - 40 * 86400000).toISOString());
|
|
184
|
-
const ctx = mockCtx(() => "[]");
|
|
185
|
-
const result = await runSleep(ctx, service, demotionConfig(), ctx.logger, null, null);
|
|
186
|
-
const after = service.getById(m.id);
|
|
187
|
-
assert.equal(result.status, "ok");
|
|
188
|
-
assert.ok(after._full_content && after._full_content.length > 0, "full body preserved");
|
|
189
|
-
assert.ok(after.content.length < "原内容".repeat(60).length, "content shrank to summary");
|
|
190
|
-
assert.equal(after.archived, false, "not fully archived at 40 days");
|
|
191
|
-
store.close();
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
test("sleep: demotion fully archives memory past sleepCompressDays", async () => {
|
|
195
|
-
const { service, store } = setup();
|
|
196
|
-
const now = Date.now();
|
|
197
|
-
const m = makeMemory(service, "ancient", "很老的记忆", "project");
|
|
198
|
-
service.touchLastAccess(m.id, new Date(now - 100 * 86400000).toISOString());
|
|
199
|
-
const ctx = mockCtx(() => "[]");
|
|
200
|
-
const result = await runSleep(ctx, service, demotionConfig(), ctx.logger, null, null);
|
|
201
|
-
const after = service.getById(m.id);
|
|
202
|
-
assert.equal(result.status, "ok");
|
|
203
|
-
assert.equal(after.archived, true, "past compress days → archived");
|
|
204
|
-
store.close();
|
|
205
|
-
});
|
|
206
|
-
|
|
207
|
-
test("sleep: demoteToSummary minRefTimeMs skips a memory touched after snapshot", () => {
|
|
208
|
-
const { service, store } = setup();
|
|
209
|
-
const now = Date.now();
|
|
210
|
-
const m = makeMemory(service, "touched", "内容".repeat(60), "project");
|
|
211
|
-
service.touchLastAccess(m.id, new Date(now - 40 * 86400000).toISOString());
|
|
212
|
-
const snapshotCut = now - 1 * 86400000;
|
|
213
|
-
service.touchLastAccess(m.id, new Date(snapshotCut + 60000).toISOString());
|
|
214
|
-
const updated = service.demoteToSummary(m.id, "摘要", { minRefTimeMs: snapshotCut });
|
|
215
|
-
assert.equal(updated, undefined, "touched-after-snapshot memory is not demoted");
|
|
216
|
-
assert.equal(service.getById(m.id)._full_content ?? null, null, "no demotion happened");
|
|
217
|
-
store.close();
|
|
218
|
-
});
|
|
219
|
-
|
|
220
|
-
test("sleep: demotion demotes a memory still cold after snapshot", () => {
|
|
221
|
-
const { service, store } = setup();
|
|
222
|
-
const now = Date.now();
|
|
223
|
-
const m = makeMemory(service, "stillcold", "内容".repeat(60), "project");
|
|
224
|
-
service.touchLastAccess(m.id, new Date(now - 40 * 86400000).toISOString());
|
|
225
|
-
const snapshotCut = now - 1 * 86400000;
|
|
226
|
-
const updated = service.demoteToSummary(m.id, "摘要", { minRefTimeMs: snapshotCut });
|
|
227
|
-
assert.ok(updated && updated._full_content, "cold memory demoted to summary");
|
|
228
|
-
store.close();
|
|
229
|
-
});
|
|
230
|
-
|
|
231
|
-
test("sleep: pattern discovery filters fabricated evidence ids", async () => {
|
|
232
|
-
const { service, store } = setup();
|
|
233
|
-
const a = makeMemory(service, "模式A", "反复出现的模式A", "project");
|
|
234
|
-
const b = makeMemory(service, "模式B", "反复出现的模式B", "project");
|
|
235
|
-
const ctx = mockCtx(() =>
|
|
236
|
-
JSON.stringify([
|
|
237
|
-
{ action: "create", type: "pattern", title: "真模式", content: "从 a 和 b 提取", importance: 3, evidence: [a.id, "fake-zzz", b.id] }
|
|
238
|
-
])
|
|
239
|
-
);
|
|
240
|
-
const result = await runSleep(ctx, service, baseConfig({ sleepPatternMinMemories: 10 }), ctx.logger, null, null);
|
|
241
|
-
assert.ok(!JSON.stringify(result.phases.patterns).includes("fake-zzz"), "fabricated evidence filtered before apply");
|
|
242
|
-
const pattern = service.all().find((x) => x.type === "pattern");
|
|
243
|
-
assert.ok(pattern, "pattern minted");
|
|
244
|
-
store.close();
|
|
245
|
-
});
|
|
246
|
-
|
|
247
|
-
test("sleep: relation completion links orphan entities co-occurring in a memory", async () => {
|
|
248
|
-
const { service, store } = setup();
|
|
249
|
-
const alpha = service.createEntity({ name: "Alpha", type: "project" });
|
|
250
|
-
const beta = service.createEntity({ name: "Beta", type: "project" });
|
|
251
|
-
makeMemory(service, "协作", "Alpha 与 Beta 一起干活", "project");
|
|
252
|
-
const ctx = mockCtx(() => "[]");
|
|
253
|
-
const result = await runSleep(ctx, service, baseConfig(), ctx.logger, null, null);
|
|
254
|
-
assert.equal(result.phases.relations.status, "ok", "relations phase ran");
|
|
255
|
-
const rels = service.getRelations(alpha.id);
|
|
256
|
-
assert.ok(rels.length >= 1, "orphan Alpha gained a relation");
|
|
257
|
-
assert.ok(rels.some((r) => r.to_entity === beta.id || r.from_entity === beta.id), "relation targets Beta");
|
|
258
|
-
store.close();
|
|
259
|
-
});
|
|
260
|
-
|
|
261
|
-
test("sleep: runSleep writes an audit receipt with run_type='sleep'", async () => {
|
|
262
|
-
const { service, store } = setup();
|
|
263
|
-
const now = Date.now();
|
|
264
|
-
const m = makeMemory(service, "cold", "内容".repeat(60), "project");
|
|
265
|
-
service.touchLastAccess(m.id, new Date(now - 40 * 86400000).toISOString());
|
|
266
|
-
const ctx = mockCtx(() => "[]");
|
|
267
|
-
const result = await runSleep(ctx, service, baseConfig(), ctx.logger, null, null);
|
|
268
|
-
const runs = service.listDreamRuns();
|
|
269
|
-
const last = runs[runs.length - 1];
|
|
270
|
-
assert.equal(last.run_type, "sleep", "audit row tagged sleep");
|
|
271
|
-
assert.equal(result.runId, last.id, "run id matches audit row");
|
|
272
|
-
assert.ok(typeof result.receipt === "string" && result.receipt.startsWith("dsh-mneme:run:"), "receipt is the audit string");
|
|
273
|
-
store.close();
|
|
274
|
-
});
|
|
275
|
-
|
|
276
|
-
test("sleep: a failing phase does not block the others (fail-safe)", async () => {
|
|
277
|
-
const { service, store } = setup();
|
|
278
|
-
service.setEmbedder({
|
|
279
|
-
embed: async () => { throw new Error("embed down"); },
|
|
280
|
-
embedSingle: async () => { throw new Error("embed down"); },
|
|
281
|
-
modelHash: "x", dimension: 3
|
|
282
|
-
});
|
|
283
|
-
const now = Date.now();
|
|
284
|
-
const m = makeMemory(service, "cold", "内容".repeat(60), "project");
|
|
285
|
-
service.touchLastAccess(m.id, new Date(now - 40 * 86400000).toISOString());
|
|
286
|
-
const ctx = mockCtx(() => "[]");
|
|
287
|
-
const result = await runSleep(ctx, service, demotionConfig(), ctx.logger, null, null);
|
|
288
|
-
assert.equal(result.phases.conflicts.status, "skipped", "conflicts phase degraded gracefully (no usable vectors)");
|
|
289
|
-
assert.equal(result.phases.demotion.status, "ok", "demotion still ran");
|
|
290
|
-
assert.equal(result.status, "ok", "overall run still ok despite conflicts degrading");
|
|
291
|
-
assert.ok(service.getById(m.id)._full_content, "cold memory still demoted despite conflicts failure");
|
|
292
|
-
store.close();
|
|
293
|
-
});
|
|
294
|
-
|
|
295
|
-
test("sleep: no LLM route skips LLM phases but demotion still runs", async () => {
|
|
296
|
-
const { service, store } = setup();
|
|
297
|
-
const now = Date.now();
|
|
298
|
-
const m = makeMemory(service, "cold", "内容".repeat(60), "project");
|
|
299
|
-
service.touchLastAccess(m.id, new Date(now - 40 * 86400000).toISOString());
|
|
300
|
-
const ctx = mockCtx(() => "[]", null); // currentSelection() → null, no route
|
|
301
|
-
const result = await runSleep(ctx, service, demotionConfig(), ctx.logger, null, null);
|
|
302
|
-
assert.equal(result.phases.conflicts.status, "skipped", "no llm route → conflicts skipped");
|
|
303
|
-
assert.equal(result.phases.patterns.status, "skipped", "no llm route → patterns skipped");
|
|
304
|
-
assert.equal(result.phases.demotion.status, "ok", "demotion is LLM-free and runs");
|
|
305
|
-
store.close();
|
|
306
|
-
});
|
|
307
|
-
|
|
308
|
-
// ------------------------------------------------------------ conflict strictness
|
|
309
|
-
|
|
310
|
-
function seedConflictPair(service, vectorIndex, sim) {
|
|
311
|
-
const a = makeMemory(service, "主题X", "内容A 关于主题X", "project");
|
|
312
|
-
const b = makeMemory(service, "主题X副本", "内容B 关于主题X", "project");
|
|
313
|
-
const sin = Math.sqrt(Math.max(0, 1 - sim * sim));
|
|
314
|
-
vectorIndex.saveEmbedding(a.id, [1, 0, 0]);
|
|
315
|
-
vectorIndex.saveEmbedding(b.id, [sim, sin, 0]);
|
|
316
|
-
return { a, b };
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
test("sleep: conflict strictness gentle ignores sim 0.88 pairs", async () => {
|
|
320
|
-
const { service, store, vectorIndex } = setup();
|
|
321
|
-
seedConflictPair(service, vectorIndex, 0.88);
|
|
322
|
-
const config = baseConfig({ sleepConflictStrictness: "gentle", conflictFreezeEnabled: true });
|
|
323
|
-
const ctx = mockCtx(() => "[]");
|
|
324
|
-
const result = await runSleep(ctx, service, config, ctx.logger, { embedder, vectorIndex }, null);
|
|
325
|
-
assert.equal(result.phases.conflicts.status, "skipped", "0.88 below gentle 0.92 → no conflicts");
|
|
326
|
-
store.close();
|
|
327
|
-
});
|
|
328
|
-
|
|
329
|
-
test("sleep: conflict strictness normal resolves sim 0.88 pairs", async () => {
|
|
330
|
-
const { service, store, vectorIndex } = setup();
|
|
331
|
-
seedConflictPair(service, vectorIndex, 0.88);
|
|
332
|
-
const config = baseConfig({ sleepConflictStrictness: "normal", conflictFreezeEnabled: true });
|
|
333
|
-
const ctx = mockCtx(() => "[]");
|
|
334
|
-
const result = await runSleep(ctx, service, config, ctx.logger, { embedder, vectorIndex }, null);
|
|
335
|
-
assert.equal(result.phases.conflicts.status, "ok", "0.88 above normal 0.85 → conflicts found");
|
|
336
|
-
assert.ok(result.phases.conflicts.frozen >= 1, "pairs frozen for review");
|
|
337
|
-
store.close();
|
|
338
|
-
});
|
|
339
|
-
|
|
340
|
-
test("sleep: conflict strictness aggressive adjudicates low-confidence pairs", async () => {
|
|
341
|
-
const { service, store, vectorIndex } = setup();
|
|
342
|
-
seedConflictPair(service, vectorIndex, 0.80);
|
|
343
|
-
const config = baseConfig({ sleepConflictStrictness: "aggressive", conflictFreezeEnabled: true });
|
|
344
|
-
const ctx = mockCtx(() => "[]");
|
|
345
|
-
const result = await runSleep(ctx, service, config, ctx.logger, { embedder, vectorIndex }, null);
|
|
346
|
-
assert.equal(result.phases.conflicts.status, "ok", "0.80 above aggressive 0.75 → conflicts found");
|
|
347
|
-
assert.ok(result.phases.conflicts.frozen >= 1, "pairs frozen");
|
|
348
|
-
store.close();
|
|
349
|
-
});
|
|
350
|
-
|
|
351
|
-
test("sleep: conflict LLM arbitration applies winner/loser", async () => {
|
|
352
|
-
const { service, store, vectorIndex } = setup();
|
|
353
|
-
const { a, b } = seedConflictPair(service, vectorIndex, 1.0);
|
|
354
|
-
const ctx = mockCtx(() =>
|
|
355
|
-
JSON.stringify([{ action: "conflict", winner: a.id, loser: b.id, reason: "重复覆盖" }])
|
|
356
|
-
);
|
|
357
|
-
const result = await runSleep(ctx, service, baseConfig(), ctx.logger, { embedder, vectorIndex }, null);
|
|
358
|
-
assert.equal(result.phases.conflicts.status, "ok", "conflict resolved");
|
|
359
|
-
assert.equal(service.getById(b.id).archived, true, "loser archived by arbitration");
|
|
360
|
-
store.close();
|
|
361
|
-
});
|
|
362
|
-
|
|
363
|
-
test("sleep: runSleep is abortable via signal between phases", async () => {
|
|
364
|
-
const { service, store } = setup();
|
|
365
|
-
const now = Date.now();
|
|
366
|
-
const m = makeMemory(service, "cold", "内容".repeat(60), "project");
|
|
367
|
-
service.touchLastAccess(m.id, new Date(now - 40 * 86400000).toISOString());
|
|
368
|
-
const ctrl = new AbortController();
|
|
369
|
-
ctrl.abort(); // pre-aborted
|
|
370
|
-
const ctx = mockCtx(() => "[]");
|
|
371
|
-
const result = await runSleep(ctx, service, baseConfig(), ctx.logger, null, ctrl.signal);
|
|
372
|
-
assert.equal(result.phases.conflicts, undefined, "aborted before any phase ran");
|
|
373
|
-
store.close();
|
|
374
|
-
});
|
|
375
|
-
|
|
376
|
-
// Issue #257:冲突/模式两阶段的输出预算原硬编码 2048——sleepActionSet=full
|
|
377
|
-
// 实测 24 对需约 6967 token,截断即 invalid decisions json 整轮失败。两阶段
|
|
378
|
-
// 必须读取 sleepMaxTokens 配置并透传到 ctx.llm.stream 的 options。
|
|
379
|
-
test("issue#257 conflict and pattern phases forward configured sleepMaxTokens", async () => {
|
|
380
|
-
const { service, store, vectorIndex } = setup();
|
|
381
|
-
const { a, b } = seedConflictPair(service, vectorIndex, 1.0);
|
|
382
|
-
const seen = [];
|
|
383
|
-
const ctx = mockCtx((userText) =>
|
|
384
|
-
JSON.stringify([{ action: "conflict", winner: a.id, loser: b.id, reason: "重复覆盖" }])
|
|
385
|
-
);
|
|
386
|
-
const origStream = ctx.llm.stream.bind(ctx.llm);
|
|
387
|
-
ctx.llm.stream = async function* (options) {
|
|
388
|
-
if (options?.purpose === "sleep-conflict" || options?.purpose === "sleep-pattern") {
|
|
389
|
-
seen.push({ purpose: options.purpose, maxTokens: options.maxTokens });
|
|
390
|
-
}
|
|
391
|
-
if (options?.purpose === "sleep-pattern") {
|
|
392
|
-
// pattern 阶段喂确定性空结果(no patterns found → skipped),
|
|
393
|
-
// 不复用 conflict 决策(宽松模式下会被当 Fabricated 静默跳过)。
|
|
394
|
-
yield { type: "text-delta", index: 0, text: "[]" };
|
|
395
|
-
yield { type: "finish", reason: { kind: "stop" } };
|
|
396
|
-
return;
|
|
397
|
-
}
|
|
398
|
-
yield* origStream(options);
|
|
399
|
-
};
|
|
400
|
-
const result = await runSleep(ctx, service, baseConfig({ sleepMaxTokens: 6967 }), ctx.logger, { embedder, vectorIndex }, null);
|
|
401
|
-
assert.equal(result.status, "ok", "run completes with the configured budget");
|
|
402
|
-
assert.ok(seen.some((s) => s.purpose === "sleep-conflict" && s.maxTokens === 6967), "conflict phase reads sleepMaxTokens");
|
|
403
|
-
assert.ok(seen.some((s) => s.purpose === "sleep-pattern" && s.maxTokens === 6967), "pattern phase reads sleepMaxTokens");
|
|
404
|
-
store.close();
|
|
405
|
-
});
|
|
406
|
-
|
|
407
|
-
test("issue#257 schema default for sleepMaxTokens is 8192 (was hardcoded 2048)", () => {
|
|
408
|
-
const cfg = Config({});
|
|
409
|
-
assert.equal(cfg.sleepMaxTokens, 8192, "schema default covers the measured full-set peak (6967)");
|
|
410
|
-
});
|
|
411
|
-
|
|
412
|
-
// ------------------------------------------- sleep LLM accounting (issue #250)
|
|
413
|
-
// Bug8 给 dream 与 summarize 接记账时漏了 sleep:src/dream/sleep.js 有一份自己的
|
|
414
|
-
// streamText,签名里没有 onUsage,两条 LLM 链路(conflict 裁决 / pattern 挖掘)的
|
|
415
|
-
// token 与状态从未进 llm_audit_logs——面板因此只看到两条链路,回答不了「sleep 花了
|
|
416
|
-
// 多少」。下面两条用例锁定「审计行落库 + token 数值来自 usage chunk」。
|
|
417
|
-
// 两条链路共用同一处 streamText 改动,故放在同一轮 red-green 里。
|
|
418
|
-
|
|
419
|
-
/** 在 base ctx 的流里、finish 之前插入一个 usage chunk(形态同 llm-audit.test.js)。 */
|
|
420
|
-
function withUsageChunk(base, usageChunk) {
|
|
421
|
-
return {
|
|
422
|
-
...base,
|
|
423
|
-
llm: {
|
|
424
|
-
async *stream(options) {
|
|
425
|
-
for await (const chunk of base.llm.stream(options)) {
|
|
426
|
-
if (chunk.type === "finish") yield usageChunk;
|
|
427
|
-
yield chunk;
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
};
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
test("issue#250 sleep conflict phase writes an llm_audit row with the streamed tokens", async () => {
|
|
435
|
-
const { service, store, vectorIndex } = setup();
|
|
436
|
-
const { a, b } = seedConflictPair(service, vectorIndex, 1.0);
|
|
437
|
-
const ctx = withUsageChunk(
|
|
438
|
-
mockCtx(() => JSON.stringify([{ action: "conflict", winner: a.id, loser: b.id, reason: "重复覆盖" }])),
|
|
439
|
-
{ type: "usage", usage: { inputTokens: 1200, outputTokens: 300, totalTokens: 1500 } }
|
|
440
|
-
);
|
|
441
|
-
const result = await runSleep(ctx, service, baseConfig(), ctx.logger, { embedder, vectorIndex }, null);
|
|
442
|
-
assert.equal(result.phases.conflicts.status, "ok", "conflict resolved");
|
|
443
|
-
|
|
444
|
-
const conflict = service.listLlmAudits({ source: "sleep" }).find((r) => r.operation_type === "sleep_conflict");
|
|
445
|
-
assert.ok(conflict, "sleep_conflict audit row present");
|
|
446
|
-
assert.equal(conflict.trigger_source, "sleep");
|
|
447
|
-
assert.equal(conflict.status, "success");
|
|
448
|
-
assert.equal(conflict.model_id, "mock:sleep-model", "route actually used is recorded");
|
|
449
|
-
assert.equal(conflict.input_tokens, 1200, "input tokens come from chunk.usage");
|
|
450
|
-
assert.equal(conflict.output_tokens, 300, "output tokens come from chunk.usage");
|
|
451
|
-
assert.equal(conflict.total_tokens, 1500);
|
|
452
|
-
assert.ok(typeof conflict.duration_ms === "number" && conflict.duration_ms >= 0);
|
|
453
|
-
store.close();
|
|
454
|
-
});
|
|
455
|
-
|
|
456
|
-
test("issue#250 sleep pattern phase writes an llm_audit row with the streamed tokens", async () => {
|
|
457
|
-
const { service, store } = setup();
|
|
458
|
-
const a = makeMemory(service, "模式A", "反复出现的模式A", "project");
|
|
459
|
-
const b = makeMemory(service, "模式B", "反复出现的模式B", "project");
|
|
460
|
-
const ctx = withUsageChunk(
|
|
461
|
-
mockCtx(() => JSON.stringify([
|
|
462
|
-
{ action: "create", type: "pattern", title: "真模式", content: "从 a 和 b 提取", importance: 3, evidence: [a.id, b.id] }
|
|
463
|
-
])),
|
|
464
|
-
{ type: "usage", usage: { inputTokens: 900, outputTokens: 250, totalTokens: 1150 } }
|
|
465
|
-
);
|
|
466
|
-
const result = await runSleep(ctx, service, baseConfig({ sleepPatternMinMemories: 10 }), ctx.logger, null, null);
|
|
467
|
-
assert.ok(result.phases.patterns, "pattern phase ran");
|
|
468
|
-
|
|
469
|
-
const pattern = service.listLlmAudits({ source: "sleep" }).find((r) => r.operation_type === "sleep_pattern");
|
|
470
|
-
assert.ok(pattern, "sleep_pattern audit row present");
|
|
471
|
-
assert.equal(pattern.trigger_source, "sleep");
|
|
472
|
-
assert.equal(pattern.status, "success");
|
|
473
|
-
assert.equal(pattern.input_tokens, 900, "input tokens come from chunk.usage");
|
|
474
|
-
assert.equal(pattern.output_tokens, 250, "output tokens come from chunk.usage");
|
|
475
|
-
assert.equal(pattern.total_tokens, 1150);
|
|
476
|
-
store.close();
|
|
477
|
-
});
|
|
478
|
-
|
|
479
|
-
// 审计诚实性(issue #250):流式成功但输出里没有 JSON 数组时,phase 报 failed,
|
|
480
|
-
// 审计行也必须记 error——不能一个调用在两表里自相矛盾。
|
|
481
|
-
test("issue#250 sleep conflict parse failure is audited as status=error, not fake success", async () => {
|
|
482
|
-
const { service, store, vectorIndex } = setup();
|
|
483
|
-
const { a, b } = seedConflictPair(service, vectorIndex, 1.0);
|
|
484
|
-
const ctx = mockCtx(() => "抱歉,我无法把裁决整理成 JSON。");
|
|
485
|
-
const result = await runSleep(ctx, service, baseConfig(), ctx.logger, { embedder, vectorIndex }, null);
|
|
486
|
-
assert.equal(result.phases.conflicts.status, "failed", "the phase reports the failure");
|
|
487
|
-
|
|
488
|
-
const row = service.listLlmAudits({ source: "sleep" }).find((r) => r.operation_type === "sleep_conflict");
|
|
489
|
-
assert.ok(row, "the failed call is still audited");
|
|
490
|
-
assert.equal(row.status, "error", "a stream that returned unusable output is not a success");
|
|
491
|
-
assert.match(row.error_message, /no json array in llm output/);
|
|
492
|
-
store.close();
|
|
493
|
-
});
|
|
494
|
-
|
|
495
|
-
test("issue#250 sleep pattern parse failure is audited as status=error, not fake success", async () => {
|
|
496
|
-
const { service, store } = setup();
|
|
497
|
-
makeMemory(service, "模式A", "反复出现的模式A", "project");
|
|
498
|
-
makeMemory(service, "模式B", "反复出现的模式B", "project");
|
|
499
|
-
const ctx = mockCtx(() => "这不是 JSON 数组");
|
|
500
|
-
const result = await runSleep(ctx, service, baseConfig({ sleepPatternMinMemories: 10 }), ctx.logger, null, null);
|
|
501
|
-
// pattern 阶段对不可解析输出的既定语义是 skipped("no patterns found")——那是
|
|
502
|
-
// 「有没有铸造出模式」的结论,不是「这次调用成没成功」的结论,此处不改上游语义。
|
|
503
|
-
assert.equal(result.phases.patterns.status, "skipped", "the phase keeps its existing semantics");
|
|
504
|
-
|
|
505
|
-
const row = service.listLlmAudits({ source: "sleep" }).find((r) => r.operation_type === "sleep_pattern");
|
|
506
|
-
assert.ok(row, "the call still consumed tokens and is still audited");
|
|
507
|
-
assert.equal(row.status, "error", "the audit records the call's truth, not the phase's conclusion");
|
|
508
|
-
assert.match(row.error_message, /no json array in llm output/);
|
|
509
|
-
store.close();
|
|
510
|
-
});
|
|
511
|
-
|
|
512
|
-
// 回归护栏:llmAudit.enabled === false 时 auditedSleepLlm 整段早退,auditError 钩子
|
|
513
|
-
// 不会被调用。所以两个 phase 的控制流必须各自解析输出、不能依赖那个钩子的副作用
|
|
514
|
-
// ——dream 的 runAuditedLlm 正是那样踩了(关掉审计 flag 会让 autoDream 直接失效)。
|
|
515
|
-
test("issue#250 sleep still adjudicates normally when llmAudit.enabled === false", async () => {
|
|
516
|
-
const { service, store, vectorIndex } = setup();
|
|
517
|
-
const { a, b } = seedConflictPair(service, vectorIndex, 1.0);
|
|
518
|
-
const ctx = mockCtx(() =>
|
|
519
|
-
JSON.stringify([{ action: "conflict", winner: a.id, loser: b.id, reason: "重复覆盖" }])
|
|
520
|
-
);
|
|
521
|
-
const config = baseConfig({ llmAudit: { enabled: false } });
|
|
522
|
-
const result = await runSleep(ctx, service, config, ctx.logger, { embedder, vectorIndex }, null);
|
|
523
|
-
assert.equal(result.phases.conflicts.status, "ok", "turning the audit off must not break sleep");
|
|
524
|
-
assert.equal(service.getById(b.id).archived, true, "the arbitration still applied");
|
|
525
|
-
assert.equal(service.listLlmAudits().length, 0, "no audit rows when disabled");
|
|
526
|
-
store.close();
|
|
527
|
-
});
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { createSleepScheduler, runSleep } from "../src/dream/sleep.js";
|
|
4
|
+
import { Config } from "../src/config.js";
|
|
5
|
+
import { createStore } from "../src/store.js";
|
|
6
|
+
import { createService } from "../src/service.js";
|
|
7
|
+
import { createVectorIndex } from "../src/vector-index.js";
|
|
8
|
+
|
|
9
|
+
// Mock embedder: every query maps to [1,0,0] so vectors are identical unless a
|
|
10
|
+
// test pre-seeds a custom vector via vectorIndex.saveEmbedding.
|
|
11
|
+
const embedder = {
|
|
12
|
+
embedSingle: async () => [1, 0, 0],
|
|
13
|
+
embed: async () => [1, 0, 0],
|
|
14
|
+
schedule: () => {},
|
|
15
|
+
modelHash: "mock#1",
|
|
16
|
+
dimension: 3
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
// Deterministic LLM: onConsolidation(userText) => decisions JSON string.
|
|
20
|
+
function mockCtx(onConsolidation, selection = { provider: "mock", model: "sleep-model" }) {
|
|
21
|
+
return {
|
|
22
|
+
logger: { warn: () => {}, info: () => {} },
|
|
23
|
+
agentDefaultModel: { currentSelection: () => selection },
|
|
24
|
+
llm: {
|
|
25
|
+
async *stream(options) {
|
|
26
|
+
const userText = options.messages.find((m) => m.role === "user")?.content?.[0]?.text ?? "";
|
|
27
|
+
const reply = onConsolidation ? onConsolidation(userText) : "[]";
|
|
28
|
+
yield { type: "text-delta", index: 0, text: reply };
|
|
29
|
+
yield { type: "finish", reason: { kind: "stop" } };
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function baseConfig(overrides = {}) {
|
|
36
|
+
return {
|
|
37
|
+
sleepModeEnabled: true,
|
|
38
|
+
sleepIdleMinutes: 5,
|
|
39
|
+
sleepMinIntervalHours: 8,
|
|
40
|
+
sleepConflictStrictness: "normal",
|
|
41
|
+
sleepArchiveDays: 30,
|
|
42
|
+
sleepCompressDays: 90,
|
|
43
|
+
sleepPatternMinMemories: 100,
|
|
44
|
+
sleepMaxPatternPerRun: 3,
|
|
45
|
+
...overrides
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// v0.7.0 heat 双保护默认保守:默认 heatTypeDecay 下 project 记忆 40 天热值
|
|
50
|
+
// ≈0.55、100 天 ≈0.28,始终高于 sleepHeatThreshold(0.05),demotion 不触发。
|
|
51
|
+
// 降级语义测试把 project 的 λ 调快到 0.02(40 天热值≈0.03),复现"时间窗冷态
|
|
52
|
+
// 即降级"的旧路径;默认保守语义由 sleep-heat.test.js 单独覆盖。
|
|
53
|
+
function demotionConfig(overrides = {}) {
|
|
54
|
+
return baseConfig({ heatTypeDecay: { project: 0.02 }, ...overrides });
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function setup() {
|
|
58
|
+
const store = createStore(":memory:");
|
|
59
|
+
const service = createService({ store, mirror: null, config: {} });
|
|
60
|
+
const vectorIndex = createVectorIndex({ store });
|
|
61
|
+
service.setEmbedder(embedder);
|
|
62
|
+
service.setVectorIndex(vectorIndex);
|
|
63
|
+
return { store, service, vectorIndex };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function makeMemory(service, title, content, type = "project") {
|
|
67
|
+
return service.saveWithDedupe({ type, title, content, importance: 3 }).memory;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// ------------------------------------------------------------ scheduler
|
|
71
|
+
|
|
72
|
+
test("sleep: scheduler does not run when disabled", () => {
|
|
73
|
+
const { service, store } = setup();
|
|
74
|
+
const sched = createSleepScheduler({
|
|
75
|
+
service, config: baseConfig({ sleepModeEnabled: false }), logger: { warn: () => {} },
|
|
76
|
+
onRun: async () => ({ ok: true }),
|
|
77
|
+
now: () => 1_000_000, setTimeoutFn: () => 1, clearTimeoutFn: () => {}
|
|
78
|
+
});
|
|
79
|
+
assert.equal(sched.shouldRun(1_000_000 + 60 * 60000), false, "disabled never schedules");
|
|
80
|
+
store.close();
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test("sleep: scheduler fires once idle window elapses", () => {
|
|
84
|
+
const { service, store } = setup();
|
|
85
|
+
const sched = createSleepScheduler({
|
|
86
|
+
service, config: baseConfig(), logger: { warn: () => {} },
|
|
87
|
+
onRun: async () => ({ ok: true }),
|
|
88
|
+
now: () => 1_000_000, setTimeoutFn: () => 1, clearTimeoutFn: () => {}
|
|
89
|
+
});
|
|
90
|
+
assert.equal(sched.shouldRun(1_000_000 + 4 * 60000), false, "still within idle window");
|
|
91
|
+
assert.equal(sched.shouldRun(1_000_000 + 6 * 60000), true, "idle window elapsed");
|
|
92
|
+
store.close();
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test("sleep: noteWrite resets the idle clock (stale-timer guard)", () => {
|
|
96
|
+
const { service, store } = setup();
|
|
97
|
+
let now = 1_000_000;
|
|
98
|
+
const sched = createSleepScheduler({
|
|
99
|
+
service, config: baseConfig(), logger: { warn: () => {} },
|
|
100
|
+
onRun: async () => ({ ok: true }),
|
|
101
|
+
now: () => now, setTimeoutFn: () => 1, clearTimeoutFn: () => {}
|
|
102
|
+
});
|
|
103
|
+
assert.equal(sched.shouldRun(now + 6 * 60000), true, "idle after 6min");
|
|
104
|
+
now = 1_000_000 + 6 * 60000;
|
|
105
|
+
sched.noteWrite();
|
|
106
|
+
assert.equal(sched.shouldRun(now), false, "write resets idle clock");
|
|
107
|
+
assert.equal(sched.shouldRun(now + 4 * 60000), false, "still idle-pending after reset");
|
|
108
|
+
assert.equal(sched.shouldRun(now + 6 * 60000), true, "idle again after fresh window");
|
|
109
|
+
store.close();
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test("sleep: first-ever run is not blocked by min interval (lastRunAt=0)", () => {
|
|
113
|
+
const { service, store } = setup();
|
|
114
|
+
const sched = createSleepScheduler({
|
|
115
|
+
service, config: baseConfig(), logger: { warn: () => {} },
|
|
116
|
+
onRun: async () => ({ ok: true }),
|
|
117
|
+
now: () => 1_000_000, setTimeoutFn: () => 1, clearTimeoutFn: () => {}
|
|
118
|
+
});
|
|
119
|
+
assert.equal(sched.shouldRun(1_000_000 + 6 * 60000), true, "first run allowed");
|
|
120
|
+
store.close();
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
test("sleep: min interval blocks a re-run within the window, allows after", async () => {
|
|
124
|
+
const { service, store } = setup();
|
|
125
|
+
let now = 1_000_000;
|
|
126
|
+
const sched = createSleepScheduler({
|
|
127
|
+
service, config: baseConfig(), logger: { warn: () => {} },
|
|
128
|
+
onRun: async () => ({ ok: true }),
|
|
129
|
+
now: () => now, setTimeoutFn: () => 1, clearTimeoutFn: () => {}
|
|
130
|
+
});
|
|
131
|
+
now = 1_000_000 + 6 * 60000; // idle satisfied
|
|
132
|
+
const ok = await sched.maybeSchedule();
|
|
133
|
+
assert.equal(ok, true, "first run executed");
|
|
134
|
+
assert.equal(sched.shouldRun(now + 1 * 3600000), false, "1h later still inside 8h min interval");
|
|
135
|
+
assert.equal(sched.shouldRun(now + 9 * 3600000), true, "9h later past min interval → allowed");
|
|
136
|
+
store.close();
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
test("sleep: maybeSchedule enqueues via service and serializes with other work", async () => {
|
|
140
|
+
const { service, store } = setup();
|
|
141
|
+
const config = baseConfig();
|
|
142
|
+
let now = 1_000_000;
|
|
143
|
+
let order = [];
|
|
144
|
+
const sched = createSleepScheduler({
|
|
145
|
+
service, config, logger: { warn: () => {} },
|
|
146
|
+
onRun: async () => { order.push("sleep"); return { ok: true, applied: 7 }; },
|
|
147
|
+
now: () => now, setTimeoutFn: () => 1, clearTimeoutFn: () => {}
|
|
148
|
+
});
|
|
149
|
+
now = 1_000_000 + 6 * 60000;
|
|
150
|
+
const p1 = service.enqueue(async () => { order.push("a"); });
|
|
151
|
+
const p2 = sched.maybeSchedule();
|
|
152
|
+
await p1;
|
|
153
|
+
const result = await p2;
|
|
154
|
+
assert.equal(result, true, "run result propagated through enqueue");
|
|
155
|
+
assert.deepEqual(order, ["a", "sleep"], "sleep run serialized behind queued work");
|
|
156
|
+
store.close();
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
test("sleep: dispose clears the timer and prevents further runs", async () => {
|
|
160
|
+
const { service, store } = setup();
|
|
161
|
+
let cleared = false;
|
|
162
|
+
const sched = createSleepScheduler({
|
|
163
|
+
service, config: baseConfig(), logger: { warn: () => {} },
|
|
164
|
+
onRun: async () => ({ ok: true }),
|
|
165
|
+
now: () => 1_000_000,
|
|
166
|
+
setTimeoutFn: () => 1,
|
|
167
|
+
clearTimeoutFn: () => { cleared = true; }
|
|
168
|
+
});
|
|
169
|
+
// #187 起构造即挂表——dispose 必须能清掉这张(以及 noteWrite 重挂过的任何)表。
|
|
170
|
+
assert.equal(cleared, false, "timer armed at construction, not yet cleared");
|
|
171
|
+
await sched.dispose();
|
|
172
|
+
assert.equal(cleared, true, "idle timer cleared on dispose");
|
|
173
|
+
assert.equal(sched.shouldRun(1_000_000 + 6 * 60000), false, "disposed never runs");
|
|
174
|
+
store.close();
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
// ------------------------------------------------------------ runSleep phases
|
|
178
|
+
|
|
179
|
+
test("sleep: demotion shrinks cold memory to summary, keeps _full_content", async () => {
|
|
180
|
+
const { service, store } = setup();
|
|
181
|
+
const now = Date.now();
|
|
182
|
+
const m = makeMemory(service, "cold", "原内容".repeat(60), "project");
|
|
183
|
+
service.touchLastAccess(m.id, new Date(now - 40 * 86400000).toISOString());
|
|
184
|
+
const ctx = mockCtx(() => "[]");
|
|
185
|
+
const result = await runSleep(ctx, service, demotionConfig(), ctx.logger, null, null);
|
|
186
|
+
const after = service.getById(m.id);
|
|
187
|
+
assert.equal(result.status, "ok");
|
|
188
|
+
assert.ok(after._full_content && after._full_content.length > 0, "full body preserved");
|
|
189
|
+
assert.ok(after.content.length < "原内容".repeat(60).length, "content shrank to summary");
|
|
190
|
+
assert.equal(after.archived, false, "not fully archived at 40 days");
|
|
191
|
+
store.close();
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
test("sleep: demotion fully archives memory past sleepCompressDays", async () => {
|
|
195
|
+
const { service, store } = setup();
|
|
196
|
+
const now = Date.now();
|
|
197
|
+
const m = makeMemory(service, "ancient", "很老的记忆", "project");
|
|
198
|
+
service.touchLastAccess(m.id, new Date(now - 100 * 86400000).toISOString());
|
|
199
|
+
const ctx = mockCtx(() => "[]");
|
|
200
|
+
const result = await runSleep(ctx, service, demotionConfig(), ctx.logger, null, null);
|
|
201
|
+
const after = service.getById(m.id);
|
|
202
|
+
assert.equal(result.status, "ok");
|
|
203
|
+
assert.equal(after.archived, true, "past compress days → archived");
|
|
204
|
+
store.close();
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
test("sleep: demoteToSummary minRefTimeMs skips a memory touched after snapshot", () => {
|
|
208
|
+
const { service, store } = setup();
|
|
209
|
+
const now = Date.now();
|
|
210
|
+
const m = makeMemory(service, "touched", "内容".repeat(60), "project");
|
|
211
|
+
service.touchLastAccess(m.id, new Date(now - 40 * 86400000).toISOString());
|
|
212
|
+
const snapshotCut = now - 1 * 86400000;
|
|
213
|
+
service.touchLastAccess(m.id, new Date(snapshotCut + 60000).toISOString());
|
|
214
|
+
const updated = service.demoteToSummary(m.id, "摘要", { minRefTimeMs: snapshotCut });
|
|
215
|
+
assert.equal(updated, undefined, "touched-after-snapshot memory is not demoted");
|
|
216
|
+
assert.equal(service.getById(m.id)._full_content ?? null, null, "no demotion happened");
|
|
217
|
+
store.close();
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
test("sleep: demotion demotes a memory still cold after snapshot", () => {
|
|
221
|
+
const { service, store } = setup();
|
|
222
|
+
const now = Date.now();
|
|
223
|
+
const m = makeMemory(service, "stillcold", "内容".repeat(60), "project");
|
|
224
|
+
service.touchLastAccess(m.id, new Date(now - 40 * 86400000).toISOString());
|
|
225
|
+
const snapshotCut = now - 1 * 86400000;
|
|
226
|
+
const updated = service.demoteToSummary(m.id, "摘要", { minRefTimeMs: snapshotCut });
|
|
227
|
+
assert.ok(updated && updated._full_content, "cold memory demoted to summary");
|
|
228
|
+
store.close();
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
test("sleep: pattern discovery filters fabricated evidence ids", async () => {
|
|
232
|
+
const { service, store } = setup();
|
|
233
|
+
const a = makeMemory(service, "模式A", "反复出现的模式A", "project");
|
|
234
|
+
const b = makeMemory(service, "模式B", "反复出现的模式B", "project");
|
|
235
|
+
const ctx = mockCtx(() =>
|
|
236
|
+
JSON.stringify([
|
|
237
|
+
{ action: "create", type: "pattern", title: "真模式", content: "从 a 和 b 提取", importance: 3, evidence: [a.id, "fake-zzz", b.id] }
|
|
238
|
+
])
|
|
239
|
+
);
|
|
240
|
+
const result = await runSleep(ctx, service, baseConfig({ sleepPatternMinMemories: 10 }), ctx.logger, null, null);
|
|
241
|
+
assert.ok(!JSON.stringify(result.phases.patterns).includes("fake-zzz"), "fabricated evidence filtered before apply");
|
|
242
|
+
const pattern = service.all().find((x) => x.type === "pattern");
|
|
243
|
+
assert.ok(pattern, "pattern minted");
|
|
244
|
+
store.close();
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
test("sleep: relation completion links orphan entities co-occurring in a memory", async () => {
|
|
248
|
+
const { service, store } = setup();
|
|
249
|
+
const alpha = service.createEntity({ name: "Alpha", type: "project" });
|
|
250
|
+
const beta = service.createEntity({ name: "Beta", type: "project" });
|
|
251
|
+
makeMemory(service, "协作", "Alpha 与 Beta 一起干活", "project");
|
|
252
|
+
const ctx = mockCtx(() => "[]");
|
|
253
|
+
const result = await runSleep(ctx, service, baseConfig(), ctx.logger, null, null);
|
|
254
|
+
assert.equal(result.phases.relations.status, "ok", "relations phase ran");
|
|
255
|
+
const rels = service.getRelations(alpha.id);
|
|
256
|
+
assert.ok(rels.length >= 1, "orphan Alpha gained a relation");
|
|
257
|
+
assert.ok(rels.some((r) => r.to_entity === beta.id || r.from_entity === beta.id), "relation targets Beta");
|
|
258
|
+
store.close();
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
test("sleep: runSleep writes an audit receipt with run_type='sleep'", async () => {
|
|
262
|
+
const { service, store } = setup();
|
|
263
|
+
const now = Date.now();
|
|
264
|
+
const m = makeMemory(service, "cold", "内容".repeat(60), "project");
|
|
265
|
+
service.touchLastAccess(m.id, new Date(now - 40 * 86400000).toISOString());
|
|
266
|
+
const ctx = mockCtx(() => "[]");
|
|
267
|
+
const result = await runSleep(ctx, service, baseConfig(), ctx.logger, null, null);
|
|
268
|
+
const runs = service.listDreamRuns();
|
|
269
|
+
const last = runs[runs.length - 1];
|
|
270
|
+
assert.equal(last.run_type, "sleep", "audit row tagged sleep");
|
|
271
|
+
assert.equal(result.runId, last.id, "run id matches audit row");
|
|
272
|
+
assert.ok(typeof result.receipt === "string" && result.receipt.startsWith("dsh-mneme:run:"), "receipt is the audit string");
|
|
273
|
+
store.close();
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
test("sleep: a failing phase does not block the others (fail-safe)", async () => {
|
|
277
|
+
const { service, store } = setup();
|
|
278
|
+
service.setEmbedder({
|
|
279
|
+
embed: async () => { throw new Error("embed down"); },
|
|
280
|
+
embedSingle: async () => { throw new Error("embed down"); },
|
|
281
|
+
modelHash: "x", dimension: 3
|
|
282
|
+
});
|
|
283
|
+
const now = Date.now();
|
|
284
|
+
const m = makeMemory(service, "cold", "内容".repeat(60), "project");
|
|
285
|
+
service.touchLastAccess(m.id, new Date(now - 40 * 86400000).toISOString());
|
|
286
|
+
const ctx = mockCtx(() => "[]");
|
|
287
|
+
const result = await runSleep(ctx, service, demotionConfig(), ctx.logger, null, null);
|
|
288
|
+
assert.equal(result.phases.conflicts.status, "skipped", "conflicts phase degraded gracefully (no usable vectors)");
|
|
289
|
+
assert.equal(result.phases.demotion.status, "ok", "demotion still ran");
|
|
290
|
+
assert.equal(result.status, "ok", "overall run still ok despite conflicts degrading");
|
|
291
|
+
assert.ok(service.getById(m.id)._full_content, "cold memory still demoted despite conflicts failure");
|
|
292
|
+
store.close();
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
test("sleep: no LLM route skips LLM phases but demotion still runs", async () => {
|
|
296
|
+
const { service, store } = setup();
|
|
297
|
+
const now = Date.now();
|
|
298
|
+
const m = makeMemory(service, "cold", "内容".repeat(60), "project");
|
|
299
|
+
service.touchLastAccess(m.id, new Date(now - 40 * 86400000).toISOString());
|
|
300
|
+
const ctx = mockCtx(() => "[]", null); // currentSelection() → null, no route
|
|
301
|
+
const result = await runSleep(ctx, service, demotionConfig(), ctx.logger, null, null);
|
|
302
|
+
assert.equal(result.phases.conflicts.status, "skipped", "no llm route → conflicts skipped");
|
|
303
|
+
assert.equal(result.phases.patterns.status, "skipped", "no llm route → patterns skipped");
|
|
304
|
+
assert.equal(result.phases.demotion.status, "ok", "demotion is LLM-free and runs");
|
|
305
|
+
store.close();
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
// ------------------------------------------------------------ conflict strictness
|
|
309
|
+
|
|
310
|
+
function seedConflictPair(service, vectorIndex, sim) {
|
|
311
|
+
const a = makeMemory(service, "主题X", "内容A 关于主题X", "project");
|
|
312
|
+
const b = makeMemory(service, "主题X副本", "内容B 关于主题X", "project");
|
|
313
|
+
const sin = Math.sqrt(Math.max(0, 1 - sim * sim));
|
|
314
|
+
vectorIndex.saveEmbedding(a.id, [1, 0, 0]);
|
|
315
|
+
vectorIndex.saveEmbedding(b.id, [sim, sin, 0]);
|
|
316
|
+
return { a, b };
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
test("sleep: conflict strictness gentle ignores sim 0.88 pairs", async () => {
|
|
320
|
+
const { service, store, vectorIndex } = setup();
|
|
321
|
+
seedConflictPair(service, vectorIndex, 0.88);
|
|
322
|
+
const config = baseConfig({ sleepConflictStrictness: "gentle", conflictFreezeEnabled: true });
|
|
323
|
+
const ctx = mockCtx(() => "[]");
|
|
324
|
+
const result = await runSleep(ctx, service, config, ctx.logger, { embedder, vectorIndex }, null);
|
|
325
|
+
assert.equal(result.phases.conflicts.status, "skipped", "0.88 below gentle 0.92 → no conflicts");
|
|
326
|
+
store.close();
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
test("sleep: conflict strictness normal resolves sim 0.88 pairs", async () => {
|
|
330
|
+
const { service, store, vectorIndex } = setup();
|
|
331
|
+
seedConflictPair(service, vectorIndex, 0.88);
|
|
332
|
+
const config = baseConfig({ sleepConflictStrictness: "normal", conflictFreezeEnabled: true });
|
|
333
|
+
const ctx = mockCtx(() => "[]");
|
|
334
|
+
const result = await runSleep(ctx, service, config, ctx.logger, { embedder, vectorIndex }, null);
|
|
335
|
+
assert.equal(result.phases.conflicts.status, "ok", "0.88 above normal 0.85 → conflicts found");
|
|
336
|
+
assert.ok(result.phases.conflicts.frozen >= 1, "pairs frozen for review");
|
|
337
|
+
store.close();
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
test("sleep: conflict strictness aggressive adjudicates low-confidence pairs", async () => {
|
|
341
|
+
const { service, store, vectorIndex } = setup();
|
|
342
|
+
seedConflictPair(service, vectorIndex, 0.80);
|
|
343
|
+
const config = baseConfig({ sleepConflictStrictness: "aggressive", conflictFreezeEnabled: true });
|
|
344
|
+
const ctx = mockCtx(() => "[]");
|
|
345
|
+
const result = await runSleep(ctx, service, config, ctx.logger, { embedder, vectorIndex }, null);
|
|
346
|
+
assert.equal(result.phases.conflicts.status, "ok", "0.80 above aggressive 0.75 → conflicts found");
|
|
347
|
+
assert.ok(result.phases.conflicts.frozen >= 1, "pairs frozen");
|
|
348
|
+
store.close();
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
test("sleep: conflict LLM arbitration applies winner/loser", async () => {
|
|
352
|
+
const { service, store, vectorIndex } = setup();
|
|
353
|
+
const { a, b } = seedConflictPair(service, vectorIndex, 1.0);
|
|
354
|
+
const ctx = mockCtx(() =>
|
|
355
|
+
JSON.stringify([{ action: "conflict", winner: a.id, loser: b.id, reason: "重复覆盖" }])
|
|
356
|
+
);
|
|
357
|
+
const result = await runSleep(ctx, service, baseConfig(), ctx.logger, { embedder, vectorIndex }, null);
|
|
358
|
+
assert.equal(result.phases.conflicts.status, "ok", "conflict resolved");
|
|
359
|
+
assert.equal(service.getById(b.id).archived, true, "loser archived by arbitration");
|
|
360
|
+
store.close();
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
test("sleep: runSleep is abortable via signal between phases", async () => {
|
|
364
|
+
const { service, store } = setup();
|
|
365
|
+
const now = Date.now();
|
|
366
|
+
const m = makeMemory(service, "cold", "内容".repeat(60), "project");
|
|
367
|
+
service.touchLastAccess(m.id, new Date(now - 40 * 86400000).toISOString());
|
|
368
|
+
const ctrl = new AbortController();
|
|
369
|
+
ctrl.abort(); // pre-aborted
|
|
370
|
+
const ctx = mockCtx(() => "[]");
|
|
371
|
+
const result = await runSleep(ctx, service, baseConfig(), ctx.logger, null, ctrl.signal);
|
|
372
|
+
assert.equal(result.phases.conflicts, undefined, "aborted before any phase ran");
|
|
373
|
+
store.close();
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
// Issue #257:冲突/模式两阶段的输出预算原硬编码 2048——sleepActionSet=full
|
|
377
|
+
// 实测 24 对需约 6967 token,截断即 invalid decisions json 整轮失败。两阶段
|
|
378
|
+
// 必须读取 sleepMaxTokens 配置并透传到 ctx.llm.stream 的 options。
|
|
379
|
+
test("issue#257 conflict and pattern phases forward configured sleepMaxTokens", async () => {
|
|
380
|
+
const { service, store, vectorIndex } = setup();
|
|
381
|
+
const { a, b } = seedConflictPair(service, vectorIndex, 1.0);
|
|
382
|
+
const seen = [];
|
|
383
|
+
const ctx = mockCtx((userText) =>
|
|
384
|
+
JSON.stringify([{ action: "conflict", winner: a.id, loser: b.id, reason: "重复覆盖" }])
|
|
385
|
+
);
|
|
386
|
+
const origStream = ctx.llm.stream.bind(ctx.llm);
|
|
387
|
+
ctx.llm.stream = async function* (options) {
|
|
388
|
+
if (options?.purpose === "sleep-conflict" || options?.purpose === "sleep-pattern") {
|
|
389
|
+
seen.push({ purpose: options.purpose, maxTokens: options.maxTokens });
|
|
390
|
+
}
|
|
391
|
+
if (options?.purpose === "sleep-pattern") {
|
|
392
|
+
// pattern 阶段喂确定性空结果(no patterns found → skipped),
|
|
393
|
+
// 不复用 conflict 决策(宽松模式下会被当 Fabricated 静默跳过)。
|
|
394
|
+
yield { type: "text-delta", index: 0, text: "[]" };
|
|
395
|
+
yield { type: "finish", reason: { kind: "stop" } };
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
yield* origStream(options);
|
|
399
|
+
};
|
|
400
|
+
const result = await runSleep(ctx, service, baseConfig({ sleepMaxTokens: 6967 }), ctx.logger, { embedder, vectorIndex }, null);
|
|
401
|
+
assert.equal(result.status, "ok", "run completes with the configured budget");
|
|
402
|
+
assert.ok(seen.some((s) => s.purpose === "sleep-conflict" && s.maxTokens === 6967), "conflict phase reads sleepMaxTokens");
|
|
403
|
+
assert.ok(seen.some((s) => s.purpose === "sleep-pattern" && s.maxTokens === 6967), "pattern phase reads sleepMaxTokens");
|
|
404
|
+
store.close();
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
test("issue#257 schema default for sleepMaxTokens is 8192 (was hardcoded 2048)", () => {
|
|
408
|
+
const cfg = Config({});
|
|
409
|
+
assert.equal(cfg.sleepMaxTokens, 8192, "schema default covers the measured full-set peak (6967)");
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
// ------------------------------------------- sleep LLM accounting (issue #250)
|
|
413
|
+
// Bug8 给 dream 与 summarize 接记账时漏了 sleep:src/dream/sleep.js 有一份自己的
|
|
414
|
+
// streamText,签名里没有 onUsage,两条 LLM 链路(conflict 裁决 / pattern 挖掘)的
|
|
415
|
+
// token 与状态从未进 llm_audit_logs——面板因此只看到两条链路,回答不了「sleep 花了
|
|
416
|
+
// 多少」。下面两条用例锁定「审计行落库 + token 数值来自 usage chunk」。
|
|
417
|
+
// 两条链路共用同一处 streamText 改动,故放在同一轮 red-green 里。
|
|
418
|
+
|
|
419
|
+
/** 在 base ctx 的流里、finish 之前插入一个 usage chunk(形态同 llm-audit.test.js)。 */
|
|
420
|
+
function withUsageChunk(base, usageChunk) {
|
|
421
|
+
return {
|
|
422
|
+
...base,
|
|
423
|
+
llm: {
|
|
424
|
+
async *stream(options) {
|
|
425
|
+
for await (const chunk of base.llm.stream(options)) {
|
|
426
|
+
if (chunk.type === "finish") yield usageChunk;
|
|
427
|
+
yield chunk;
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
test("issue#250 sleep conflict phase writes an llm_audit row with the streamed tokens", async () => {
|
|
435
|
+
const { service, store, vectorIndex } = setup();
|
|
436
|
+
const { a, b } = seedConflictPair(service, vectorIndex, 1.0);
|
|
437
|
+
const ctx = withUsageChunk(
|
|
438
|
+
mockCtx(() => JSON.stringify([{ action: "conflict", winner: a.id, loser: b.id, reason: "重复覆盖" }])),
|
|
439
|
+
{ type: "usage", usage: { inputTokens: 1200, outputTokens: 300, totalTokens: 1500 } }
|
|
440
|
+
);
|
|
441
|
+
const result = await runSleep(ctx, service, baseConfig(), ctx.logger, { embedder, vectorIndex }, null);
|
|
442
|
+
assert.equal(result.phases.conflicts.status, "ok", "conflict resolved");
|
|
443
|
+
|
|
444
|
+
const conflict = service.listLlmAudits({ source: "sleep" }).find((r) => r.operation_type === "sleep_conflict");
|
|
445
|
+
assert.ok(conflict, "sleep_conflict audit row present");
|
|
446
|
+
assert.equal(conflict.trigger_source, "sleep");
|
|
447
|
+
assert.equal(conflict.status, "success");
|
|
448
|
+
assert.equal(conflict.model_id, "mock:sleep-model", "route actually used is recorded");
|
|
449
|
+
assert.equal(conflict.input_tokens, 1200, "input tokens come from chunk.usage");
|
|
450
|
+
assert.equal(conflict.output_tokens, 300, "output tokens come from chunk.usage");
|
|
451
|
+
assert.equal(conflict.total_tokens, 1500);
|
|
452
|
+
assert.ok(typeof conflict.duration_ms === "number" && conflict.duration_ms >= 0);
|
|
453
|
+
store.close();
|
|
454
|
+
});
|
|
455
|
+
|
|
456
|
+
test("issue#250 sleep pattern phase writes an llm_audit row with the streamed tokens", async () => {
|
|
457
|
+
const { service, store } = setup();
|
|
458
|
+
const a = makeMemory(service, "模式A", "反复出现的模式A", "project");
|
|
459
|
+
const b = makeMemory(service, "模式B", "反复出现的模式B", "project");
|
|
460
|
+
const ctx = withUsageChunk(
|
|
461
|
+
mockCtx(() => JSON.stringify([
|
|
462
|
+
{ action: "create", type: "pattern", title: "真模式", content: "从 a 和 b 提取", importance: 3, evidence: [a.id, b.id] }
|
|
463
|
+
])),
|
|
464
|
+
{ type: "usage", usage: { inputTokens: 900, outputTokens: 250, totalTokens: 1150 } }
|
|
465
|
+
);
|
|
466
|
+
const result = await runSleep(ctx, service, baseConfig({ sleepPatternMinMemories: 10 }), ctx.logger, null, null);
|
|
467
|
+
assert.ok(result.phases.patterns, "pattern phase ran");
|
|
468
|
+
|
|
469
|
+
const pattern = service.listLlmAudits({ source: "sleep" }).find((r) => r.operation_type === "sleep_pattern");
|
|
470
|
+
assert.ok(pattern, "sleep_pattern audit row present");
|
|
471
|
+
assert.equal(pattern.trigger_source, "sleep");
|
|
472
|
+
assert.equal(pattern.status, "success");
|
|
473
|
+
assert.equal(pattern.input_tokens, 900, "input tokens come from chunk.usage");
|
|
474
|
+
assert.equal(pattern.output_tokens, 250, "output tokens come from chunk.usage");
|
|
475
|
+
assert.equal(pattern.total_tokens, 1150);
|
|
476
|
+
store.close();
|
|
477
|
+
});
|
|
478
|
+
|
|
479
|
+
// 审计诚实性(issue #250):流式成功但输出里没有 JSON 数组时,phase 报 failed,
|
|
480
|
+
// 审计行也必须记 error——不能一个调用在两表里自相矛盾。
|
|
481
|
+
test("issue#250 sleep conflict parse failure is audited as status=error, not fake success", async () => {
|
|
482
|
+
const { service, store, vectorIndex } = setup();
|
|
483
|
+
const { a, b } = seedConflictPair(service, vectorIndex, 1.0);
|
|
484
|
+
const ctx = mockCtx(() => "抱歉,我无法把裁决整理成 JSON。");
|
|
485
|
+
const result = await runSleep(ctx, service, baseConfig(), ctx.logger, { embedder, vectorIndex }, null);
|
|
486
|
+
assert.equal(result.phases.conflicts.status, "failed", "the phase reports the failure");
|
|
487
|
+
|
|
488
|
+
const row = service.listLlmAudits({ source: "sleep" }).find((r) => r.operation_type === "sleep_conflict");
|
|
489
|
+
assert.ok(row, "the failed call is still audited");
|
|
490
|
+
assert.equal(row.status, "error", "a stream that returned unusable output is not a success");
|
|
491
|
+
assert.match(row.error_message, /no json array in llm output/);
|
|
492
|
+
store.close();
|
|
493
|
+
});
|
|
494
|
+
|
|
495
|
+
test("issue#250 sleep pattern parse failure is audited as status=error, not fake success", async () => {
|
|
496
|
+
const { service, store } = setup();
|
|
497
|
+
makeMemory(service, "模式A", "反复出现的模式A", "project");
|
|
498
|
+
makeMemory(service, "模式B", "反复出现的模式B", "project");
|
|
499
|
+
const ctx = mockCtx(() => "这不是 JSON 数组");
|
|
500
|
+
const result = await runSleep(ctx, service, baseConfig({ sleepPatternMinMemories: 10 }), ctx.logger, null, null);
|
|
501
|
+
// pattern 阶段对不可解析输出的既定语义是 skipped("no patterns found")——那是
|
|
502
|
+
// 「有没有铸造出模式」的结论,不是「这次调用成没成功」的结论,此处不改上游语义。
|
|
503
|
+
assert.equal(result.phases.patterns.status, "skipped", "the phase keeps its existing semantics");
|
|
504
|
+
|
|
505
|
+
const row = service.listLlmAudits({ source: "sleep" }).find((r) => r.operation_type === "sleep_pattern");
|
|
506
|
+
assert.ok(row, "the call still consumed tokens and is still audited");
|
|
507
|
+
assert.equal(row.status, "error", "the audit records the call's truth, not the phase's conclusion");
|
|
508
|
+
assert.match(row.error_message, /no json array in llm output/);
|
|
509
|
+
store.close();
|
|
510
|
+
});
|
|
511
|
+
|
|
512
|
+
// 回归护栏:llmAudit.enabled === false 时 auditedSleepLlm 整段早退,auditError 钩子
|
|
513
|
+
// 不会被调用。所以两个 phase 的控制流必须各自解析输出、不能依赖那个钩子的副作用
|
|
514
|
+
// ——dream 的 runAuditedLlm 正是那样踩了(关掉审计 flag 会让 autoDream 直接失效)。
|
|
515
|
+
test("issue#250 sleep still adjudicates normally when llmAudit.enabled === false", async () => {
|
|
516
|
+
const { service, store, vectorIndex } = setup();
|
|
517
|
+
const { a, b } = seedConflictPair(service, vectorIndex, 1.0);
|
|
518
|
+
const ctx = mockCtx(() =>
|
|
519
|
+
JSON.stringify([{ action: "conflict", winner: a.id, loser: b.id, reason: "重复覆盖" }])
|
|
520
|
+
);
|
|
521
|
+
const config = baseConfig({ llmAudit: { enabled: false } });
|
|
522
|
+
const result = await runSleep(ctx, service, config, ctx.logger, { embedder, vectorIndex }, null);
|
|
523
|
+
assert.equal(result.phases.conflicts.status, "ok", "turning the audit off must not break sleep");
|
|
524
|
+
assert.equal(service.getById(b.id).archived, true, "the arbitration still applied");
|
|
525
|
+
assert.equal(service.listLlmAudits().length, 0, "no audit rows when disabled");
|
|
526
|
+
store.close();
|
|
527
|
+
});
|