@modusensus/dsh-mneme 0.6.7 → 0.6.8
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 +463 -463
- package/cordis.patch.yml +15 -15
- package/lib/api.js +783 -783
- package/lib/client.js +1757 -1757
- package/lib/commands.js +64 -64
- package/lib/config.js +288 -288
- package/lib/dream/clustering.js +118 -118
- package/lib/dream/decisions.js +439 -439
- package/lib/dream/sleep.js +561 -554
- package/lib/dream/tag-extractor.js +156 -156
- package/lib/dream.js +935 -929
- package/lib/embedding.js +154 -154
- package/lib/entities/extractor.js +242 -242
- package/lib/hot-memory.js +53 -53
- package/lib/index.js +361 -361
- package/lib/inject.js +208 -208
- package/lib/local-embedder.js +282 -282
- package/lib/mirror.js +170 -170
- package/lib/parser/tag.js +59 -59
- package/lib/parser/wiki-link.js +38 -38
- package/lib/quality-filter.js +123 -123
- package/lib/reranker.js +218 -218
- package/lib/search/adaptive.js +22 -22
- package/lib/search/bm25.js +96 -96
- package/lib/search/tag-boost.js +61 -61
- package/lib/service.js +1726 -1726
- package/lib/settings.js +172 -172
- package/lib/store.js +2238 -2238
- package/lib/summarize.js +236 -236
- package/lib/tools.js +290 -290
- package/lib/vector-index.js +116 -116
- package/package.json +80 -80
- package/scripts/benchmark-embed.js +201 -201
- package/scripts/benchmark-recall.js +133 -133
- package/scripts/benchmark-rerank.js +166 -166
- package/scripts/e2e-dsh.js +218 -218
- package/scripts/stress-dsh.js +255 -255
- package/scripts/sync-lib.js +52 -52
- package/src/api.js +783 -783
- package/src/commands.js +64 -64
- package/src/config.js +288 -288
- package/src/dream/clustering.js +118 -118
- package/src/dream/decisions.js +439 -439
- package/src/dream/sleep.js +561 -554
- package/src/dream/tag-extractor.js +156 -156
- package/src/dream.js +935 -929
- package/src/embedding.js +154 -154
- package/src/entities/extractor.js +242 -242
- package/src/hot-memory.js +53 -53
- package/src/index.js +361 -361
- package/src/inject.js +208 -208
- package/src/local-embedder.js +282 -282
- package/src/mirror.js +170 -170
- package/src/parser/tag.js +59 -59
- package/src/parser/wiki-link.js +38 -38
- package/src/quality-filter.js +123 -123
- package/src/reranker.js +218 -218
- package/src/search/adaptive.js +22 -22
- package/src/search/bm25.js +96 -96
- package/src/search/tag-boost.js +61 -61
- package/src/service.js +1726 -1726
- package/src/settings.js +172 -172
- package/src/store.js +2238 -2238
- package/src/summarize.js +236 -236
- package/src/tools.js +290 -290
- package/src/vector-index.js +116 -116
- package/test/api.test.js +594 -594
- package/test/audit.test.js +448 -448
- package/test/benchmark.test.js +35 -35
- package/test/boundary-v0625.test.js +82 -82
- package/test/client.test.js +368 -368
- package/test/clustering.test.js +100 -100
- package/test/commands.test.js +69 -69
- package/test/config.test.js +50 -50
- package/test/conflict-freeze.test.js +290 -290
- package/test/directory.test.js +134 -134
- package/test/dream.test.js +903 -901
- package/test/entities.test.js +522 -522
- package/test/epistemic.test.js +298 -298
- package/test/fnew-0112.test.js +311 -311
- package/test/fnew-03.test.js +422 -422
- package/test/graph-api.test.js +175 -175
- package/test/helpers/dream-mock.js +82 -82
- package/test/hot-memory.test.js +174 -174
- package/test/inject.test.js +103 -103
- package/test/llm-audit.test.js +279 -279
- package/test/local-embedder.test.js +227 -227
- 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.test.js +249 -249
- package/test/normalize-decisions.test.js +120 -120
- package/test/peer-blockers.test.js +190 -190
- package/test/policy-epoch.test.js +259 -259
- package/test/provenance.test.js +103 -103
- package/test/quality-filter.test.js +118 -118
- package/test/reasoning-effort.test.js +199 -199
- package/test/recall-evals.test.js +235 -235
- package/test/recall-layer.test.js +315 -315
- package/test/receipt-chain.test.js +451 -451
- package/test/reflection.test.js +226 -226
- package/test/reranker.test.js +240 -240
- package/test/search-fusion.test.js +90 -90
- package/test/semantic.test.js +124 -124
- package/test/service-search.test.js +199 -199
- package/test/service.test.js +435 -435
- package/test/settings.test.js +118 -118
- package/test/sleep.test.js +365 -365
- package/test/store.test.js +436 -436
- package/test/stress.test.js +209 -209
- package/test/summarize.test.js +191 -191
- package/test/tag-boost.test.js +125 -125
- package/test/tag.test.js +312 -312
- package/test/tools.test.js +285 -285
- package/test/vector-index.test.js +221 -221
- package/test/wiki-link.test.js +332 -332
package/src/dream/sleep.js
CHANGED
|
@@ -1,554 +1,561 @@
|
|
|
1
|
-
// System-level sleep (v0.4.0): an idle-triggered, LLM-assisted deep pass over
|
|
2
|
-
// the memory store. Four independent, fail-safe phases:
|
|
3
|
-
// 1. conflict resolution — high-similarity same-type pairs are either parked
|
|
4
|
-
// for review (freeze mode) or adjudicated by the LLM (winner kept / loser
|
|
5
|
-
// archived), reusing the dream conflict machinery. Strictness-graded.
|
|
6
|
-
// 2. archival demotion — memories unreferenced past sleepArchiveDays shrink
|
|
7
|
-
// to a one-line summary with the full body moved to _full_content; past
|
|
8
|
-
// sleepCompressDays they are archived outright.
|
|
9
|
-
// 3. pattern discovery — the LLM scans the most recent memories and mints
|
|
10
|
-
// type=pattern entries carrying evidence id references.
|
|
11
|
-
// 4. relation completion — orphan entities (zero relations) get implied
|
|
12
|
-
// relations completed from memory co-occurrence.
|
|
13
|
-
// Each phase is wrapped so one failure never aborts the others, and a missing
|
|
14
|
-
// LLM route / semantic embedder only skips the phases that need it. A run is
|
|
15
|
-
// abortable via an AbortController signal (user activity) — phases check the
|
|
16
|
-
// signal between batches so a running cycle yields promptly.
|
|
17
|
-
import { randomUUID, createHash } from "node:crypto";
|
|
18
|
-
import { validateDecisions, applyDecisions } from "./decisions.js";
|
|
19
|
-
import { findPotentialConflicts } from "./clustering.js";
|
|
20
|
-
import { buildReceipt } from "../dream.js";
|
|
21
|
-
|
|
22
|
-
const SUMMARY_MAX = 120;
|
|
23
|
-
// Conflict similarity threshold per strictness level (v0.4.0):
|
|
24
|
-
// gentle only high-confidence pairs (0.92) — first-time users
|
|
25
|
-
// normal standard dream-level (0.85) — default
|
|
26
|
-
// aggressive low-confidence pairs too (0.75) — bloated stores
|
|
27
|
-
const CONFLICT_THRESHOLDS = { gentle: 0.92, normal: 0.85, aggressive: 0.75 };
|
|
28
|
-
|
|
29
|
-
const CONFLICT_PROMPT = `你是记忆库冲突仲裁助手。下面是检测到的高相似度记忆对,可能内容矛盾或重复。
|
|
30
|
-
对每一对输出一个 decision 对象:
|
|
31
|
-
- 两条确实矛盾/重复 → { "action": "conflict", "winner": <保留的id>, "loser": <归档的id>, "reason": "理由" }
|
|
32
|
-
- 两条只是主题相近、并无矛盾 → { "action": "keep", "ids": [<两个id>] }
|
|
33
|
-
规则:
|
|
34
|
-
- winner 应为信息更完整、更新或更可信的一条
|
|
35
|
-
- 只使用提供的 id,不要编造
|
|
36
|
-
- 每对必须输出一个 decision
|
|
37
|
-
- 只输出 JSON 数组,不要其他文字`;
|
|
38
|
-
|
|
39
|
-
const PATTERN_PROMPT = `你是记忆库模式发现助手。下面是最近的记忆条目(id、类型、标题、内容)。
|
|
40
|
-
请发现跨条目的稳定模式:用户偏好的规律、反复出现的主题、可复用的工作流或项目规律。
|
|
41
|
-
对每个模式输出一个 create decision:
|
|
42
|
-
{ "action": "create", "type": "pattern", "title": "模式一句话标题", "content": "模式详细描述(2-4句)", "importance": 1-5, "evidence": ["支持该模式的记忆id"] }
|
|
43
|
-
规则:
|
|
44
|
-
- 只输出有据可依的模式,宁缺毋滥
|
|
45
|
-
- evidence 必须是列表中真实存在的 id
|
|
46
|
-
- 最多输出 N 个模式
|
|
47
|
-
- 只输出 JSON 数组,不要其他文字`;
|
|
48
|
-
|
|
49
|
-
function parseJsonArray(text) {
|
|
50
|
-
if (typeof text !== "string") return undefined;
|
|
51
|
-
const start = text.indexOf("[");
|
|
52
|
-
const end = text.lastIndexOf("]");
|
|
53
|
-
if (start === -1 || end <= start) return undefined;
|
|
54
|
-
try {
|
|
55
|
-
const parsed = JSON.parse(text.slice(start, end + 1));
|
|
56
|
-
return Array.isArray(parsed) ? parsed : undefined;
|
|
57
|
-
} catch {
|
|
58
|
-
return undefined;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/** Same stream consumption contract as dream.js. */
|
|
63
|
-
async function streamText(ctx, options) {
|
|
64
|
-
if (!ctx?.llm?.stream) return undefined;
|
|
65
|
-
let text = "";
|
|
66
|
-
for await (const chunk of ctx.llm.stream(options)) {
|
|
67
|
-
if (chunk.type === "text-delta" && typeof chunk.text === "string") text += chunk.text;
|
|
68
|
-
if (chunk.type === "finish" && (chunk.reason?.kind === "error" || chunk.reason?.kind === "aborted")) {
|
|
69
|
-
return undefined;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
return text;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
if (config.sleepProvider && config.sleepModel) return { provider: config.sleepProvider, model: config.sleepModel };
|
|
84
|
-
if (config.dreamProvider && config.dreamModel) return { provider: config.dreamProvider, model: config.dreamModel };
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
const
|
|
112
|
-
|
|
113
|
-
if (
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
const
|
|
117
|
-
const
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
const
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
const
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
const
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
};
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
if (
|
|
280
|
-
|
|
281
|
-
const
|
|
282
|
-
.
|
|
283
|
-
.
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
const
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
};
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
function
|
|
341
|
-
|
|
342
|
-
if (
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
const
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
*
|
|
508
|
-
*
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
1
|
+
// System-level sleep (v0.4.0): an idle-triggered, LLM-assisted deep pass over
|
|
2
|
+
// the memory store. Four independent, fail-safe phases:
|
|
3
|
+
// 1. conflict resolution — high-similarity same-type pairs are either parked
|
|
4
|
+
// for review (freeze mode) or adjudicated by the LLM (winner kept / loser
|
|
5
|
+
// archived), reusing the dream conflict machinery. Strictness-graded.
|
|
6
|
+
// 2. archival demotion — memories unreferenced past sleepArchiveDays shrink
|
|
7
|
+
// to a one-line summary with the full body moved to _full_content; past
|
|
8
|
+
// sleepCompressDays they are archived outright.
|
|
9
|
+
// 3. pattern discovery — the LLM scans the most recent memories and mints
|
|
10
|
+
// type=pattern entries carrying evidence id references.
|
|
11
|
+
// 4. relation completion — orphan entities (zero relations) get implied
|
|
12
|
+
// relations completed from memory co-occurrence.
|
|
13
|
+
// Each phase is wrapped so one failure never aborts the others, and a missing
|
|
14
|
+
// LLM route / semantic embedder only skips the phases that need it. A run is
|
|
15
|
+
// abortable via an AbortController signal (user activity) — phases check the
|
|
16
|
+
// signal between batches so a running cycle yields promptly.
|
|
17
|
+
import { randomUUID, createHash } from "node:crypto";
|
|
18
|
+
import { validateDecisions, applyDecisions } from "./decisions.js";
|
|
19
|
+
import { findPotentialConflicts } from "./clustering.js";
|
|
20
|
+
import { buildReceipt } from "../dream.js";
|
|
21
|
+
|
|
22
|
+
const SUMMARY_MAX = 120;
|
|
23
|
+
// Conflict similarity threshold per strictness level (v0.4.0):
|
|
24
|
+
// gentle only high-confidence pairs (0.92) — first-time users
|
|
25
|
+
// normal standard dream-level (0.85) — default
|
|
26
|
+
// aggressive low-confidence pairs too (0.75) — bloated stores
|
|
27
|
+
const CONFLICT_THRESHOLDS = { gentle: 0.92, normal: 0.85, aggressive: 0.75 };
|
|
28
|
+
|
|
29
|
+
const CONFLICT_PROMPT = `你是记忆库冲突仲裁助手。下面是检测到的高相似度记忆对,可能内容矛盾或重复。
|
|
30
|
+
对每一对输出一个 decision 对象:
|
|
31
|
+
- 两条确实矛盾/重复 → { "action": "conflict", "winner": <保留的id>, "loser": <归档的id>, "reason": "理由" }
|
|
32
|
+
- 两条只是主题相近、并无矛盾 → { "action": "keep", "ids": [<两个id>] }
|
|
33
|
+
规则:
|
|
34
|
+
- winner 应为信息更完整、更新或更可信的一条
|
|
35
|
+
- 只使用提供的 id,不要编造
|
|
36
|
+
- 每对必须输出一个 decision
|
|
37
|
+
- 只输出 JSON 数组,不要其他文字`;
|
|
38
|
+
|
|
39
|
+
const PATTERN_PROMPT = `你是记忆库模式发现助手。下面是最近的记忆条目(id、类型、标题、内容)。
|
|
40
|
+
请发现跨条目的稳定模式:用户偏好的规律、反复出现的主题、可复用的工作流或项目规律。
|
|
41
|
+
对每个模式输出一个 create decision:
|
|
42
|
+
{ "action": "create", "type": "pattern", "title": "模式一句话标题", "content": "模式详细描述(2-4句)", "importance": 1-5, "evidence": ["支持该模式的记忆id"] }
|
|
43
|
+
规则:
|
|
44
|
+
- 只输出有据可依的模式,宁缺毋滥
|
|
45
|
+
- evidence 必须是列表中真实存在的 id
|
|
46
|
+
- 最多输出 N 个模式
|
|
47
|
+
- 只输出 JSON 数组,不要其他文字`;
|
|
48
|
+
|
|
49
|
+
function parseJsonArray(text) {
|
|
50
|
+
if (typeof text !== "string") return undefined;
|
|
51
|
+
const start = text.indexOf("[");
|
|
52
|
+
const end = text.lastIndexOf("]");
|
|
53
|
+
if (start === -1 || end <= start) return undefined;
|
|
54
|
+
try {
|
|
55
|
+
const parsed = JSON.parse(text.slice(start, end + 1));
|
|
56
|
+
return Array.isArray(parsed) ? parsed : undefined;
|
|
57
|
+
} catch {
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Same stream consumption contract as dream.js. */
|
|
63
|
+
async function streamText(ctx, options) {
|
|
64
|
+
if (!ctx?.llm?.stream) return undefined;
|
|
65
|
+
let text = "";
|
|
66
|
+
for await (const chunk of ctx.llm.stream(options)) {
|
|
67
|
+
if (chunk.type === "text-delta" && typeof chunk.text === "string") text += chunk.text;
|
|
68
|
+
if (chunk.type === "finish" && (chunk.reason?.kind === "error" || chunk.reason?.kind === "aborted")) {
|
|
69
|
+
return undefined;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return text;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// LLM route (Issue #25): explicit config wins — sleepProvider/sleepModel first,
|
|
76
|
+
// then the dream route as a shared fallback (config is the user's declared
|
|
77
|
+
// override), and agent default model only last as the generic fallback. Sleep
|
|
78
|
+
// can pin a cheaper model for its bulk passes without disturbing the dream
|
|
79
|
+
// route. The agent-default-last ordering matters: in a standard DSH install
|
|
80
|
+
// agentDefaultModel always resolves, so putting it first would make every
|
|
81
|
+
// config route dead code.
|
|
82
|
+
function resolveSleepRoute(ctx, config, logger) {
|
|
83
|
+
if (config.sleepProvider && config.sleepModel) return { provider: config.sleepProvider, model: config.sleepModel };
|
|
84
|
+
if (config.dreamProvider && config.dreamModel) return { provider: config.dreamProvider, model: config.dreamModel };
|
|
85
|
+
try {
|
|
86
|
+
const sel = ctx?.agentDefaultModel?.currentSelection?.();
|
|
87
|
+
if (sel?.provider && sel?.model) {
|
|
88
|
+
logger?.info?.("dsh-mneme sleep: no sleepProvider/sleepModel config, falling back to agent default");
|
|
89
|
+
return { provider: sel.provider, model: sel.model };
|
|
90
|
+
}
|
|
91
|
+
} catch { /* fall through */ }
|
|
92
|
+
logger?.warn?.("dsh-mneme sleep: no llm route available");
|
|
93
|
+
return undefined;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function makeSummary(m) {
|
|
97
|
+
const text = (m.content ?? "").trim();
|
|
98
|
+
if (!text) return (m.title ?? "").trim();
|
|
99
|
+
return text.length <= SUMMARY_MAX ? text : `${text.slice(0, SUMMARY_MAX)}…`;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// ---------------------------------------------------------------- phases
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Phase 1 — conflict resolution. Needs a semantic embedder + vector index.
|
|
106
|
+
* In freeze mode (conflictFreezeEnabled) conflicting pairs are parked in
|
|
107
|
+
* conflict_pending for human review (no LLM). Otherwise the LLM adjudicates:
|
|
108
|
+
* each pair → winner kept / loser archived. Returns a per-run summary.
|
|
109
|
+
*/
|
|
110
|
+
async function phaseConflicts(ctx, service, config, logger, runId, semantic = null, signal = null) {
|
|
111
|
+
const embedder = semantic?.embedder;
|
|
112
|
+
const vectorIndex = semantic?.vectorIndex;
|
|
113
|
+
if (!embedder || !vectorIndex || typeof embedder.embed !== "function") {
|
|
114
|
+
return { status: "skipped", reason: "no semantic embedder" };
|
|
115
|
+
}
|
|
116
|
+
const strictness = config.sleepConflictStrictness ?? "normal";
|
|
117
|
+
const threshold = CONFLICT_THRESHOLDS[strictness] ?? CONFLICT_THRESHOLDS.normal;
|
|
118
|
+
const memories = service.all().filter((m) => !m.archived && !m.session_disposed_at && !m.forgotten && m.type !== "summary");
|
|
119
|
+
if (memories.length < 2) return { status: "skipped", reason: "too few memories" };
|
|
120
|
+
if (signal?.aborted) return { status: "aborted", reason: "user activity" };
|
|
121
|
+
|
|
122
|
+
// Backfill + collect vectors for every eligible memory (best effort).
|
|
123
|
+
const vectors = new Array(memories.length);
|
|
124
|
+
const missing = [];
|
|
125
|
+
for (let i = 0; i < memories.length; i++) {
|
|
126
|
+
const cached = vectorIndex.getEmbedding?.(memories[i].id);
|
|
127
|
+
if (cached) vectors[i] = cached;
|
|
128
|
+
else missing.push(i);
|
|
129
|
+
}
|
|
130
|
+
if (missing.length) {
|
|
131
|
+
try {
|
|
132
|
+
const texts = missing.map((i) => [memories[i].title, memories[i].content].filter(Boolean).join("\n"));
|
|
133
|
+
const rows = await embedder.embed(texts);
|
|
134
|
+
missing.forEach((mi, j) => {
|
|
135
|
+
if (rows[j]?.length) {
|
|
136
|
+
vectors[mi] = rows[j];
|
|
137
|
+
vectorIndex.saveEmbedding?.(memories[mi].id, rows[j]);
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
} catch (error) {
|
|
141
|
+
logger?.warn?.(`dsh-mneme sleep: conflict vector backfill failed: ${String(error)}`);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
const usable = [];
|
|
145
|
+
for (let i = 0; i < memories.length; i++) {
|
|
146
|
+
if (vectors[i]?.length) usable.push(i);
|
|
147
|
+
}
|
|
148
|
+
if (usable.length < 2) return { status: "skipped", reason: "no usable vectors" };
|
|
149
|
+
const usableMemories = usable.map((i) => memories[i]);
|
|
150
|
+
const usableVectors = usable.map((i) => vectors[i]);
|
|
151
|
+
|
|
152
|
+
const pairs = findPotentialConflicts(usableMemories, usableVectors, threshold);
|
|
153
|
+
if (pairs.length === 0) return { status: "skipped", reason: "no conflicts found" };
|
|
154
|
+
|
|
155
|
+
// Dedupe: each memory participates in at most one pair, highest similarity
|
|
156
|
+
// first — overlapping pairs would violate validateDecisions' "one claim".
|
|
157
|
+
pairs.sort((a, b) => b.similarity - a.similarity);
|
|
158
|
+
const used = new Set();
|
|
159
|
+
const selected = [];
|
|
160
|
+
for (const p of pairs) {
|
|
161
|
+
if (used.has(p.a.id) || used.has(p.b.id)) continue;
|
|
162
|
+
used.add(p.a.id);
|
|
163
|
+
used.add(p.b.id);
|
|
164
|
+
selected.push(p);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Freeze mode: park pairs for manual review, no LLM required.
|
|
168
|
+
if (config.conflictFreezeEnabled === true) {
|
|
169
|
+
let frozen = 0;
|
|
170
|
+
for (const p of selected) {
|
|
171
|
+
try {
|
|
172
|
+
service.saveConflictPending({ run_id: runId, memory_a: p.a.id, memory_b: p.b.id, reason: `相似度 ${p.similarity.toFixed(2)}` });
|
|
173
|
+
frozen++;
|
|
174
|
+
} catch (error) {
|
|
175
|
+
logger?.warn?.(`dsh-mneme sleep: failed to freeze conflict ${p.a.id}/${p.b.id}: ${String(error)}`);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return { status: frozen > 0 ? "ok" : "noop", frozen, pairs: selected.length };
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// LLM adjudication.
|
|
182
|
+
const route = resolveSleepRoute(ctx, config, logger);
|
|
183
|
+
if (!route) return { status: "skipped", reason: "no llm route" };
|
|
184
|
+
const snapshot = new Map();
|
|
185
|
+
for (const p of selected) {
|
|
186
|
+
snapshot.set(p.a.id, p.a);
|
|
187
|
+
snapshot.set(p.b.id, p.b);
|
|
188
|
+
}
|
|
189
|
+
const listText = selected.map((p) =>
|
|
190
|
+
`候选冲突:\nid=${p.a.id} | type=${p.a.type} | title=${p.a.title}\n${p.a.content}\n---\nid=${p.b.id} | type=${p.b.type} | title=${p.b.title}\n${p.b.content}\n(相似度 ${p.similarity.toFixed(2)})`
|
|
191
|
+
).join("\n\n");
|
|
192
|
+
const text = await streamText(ctx, {
|
|
193
|
+
provider: route.provider,
|
|
194
|
+
model: route.model,
|
|
195
|
+
purpose: "sleep-conflict",
|
|
196
|
+
maxTokens: 2048,
|
|
197
|
+
...(config.sleepReasoningEffort && config.sleepReasoningEffort !== "none"
|
|
198
|
+
? { reasoningEffort: config.sleepReasoningEffort }
|
|
199
|
+
: {}),
|
|
200
|
+
messages: [
|
|
201
|
+
{ role: "system", content: [{ type: "text", text: CONFLICT_PROMPT }] },
|
|
202
|
+
{ role: "user", content: [{ type: "text", text: listText }] }
|
|
203
|
+
]
|
|
204
|
+
});
|
|
205
|
+
if (text === undefined) return { status: "failed", error: "llm failed" };
|
|
206
|
+
const decisions = parseJsonArray(text);
|
|
207
|
+
if (!decisions) return { status: "failed", error: "invalid decisions json" };
|
|
208
|
+
// validateDecisions 要求每个 snapshot id 恰好被 claim 一次。v0.4.4 起它本身
|
|
209
|
+
// 就会为未覆盖的 id 自动补 keep(dreamImplicitKeep 默认开启),这里保留显式
|
|
210
|
+
// 预填作为防御性双保险——漏判读作"未裁决冲突"而非"冲突阶段整体失败"。
|
|
211
|
+
const covered = new Set();
|
|
212
|
+
for (const d of decisions) {
|
|
213
|
+
if (d?.action === "conflict") {
|
|
214
|
+
if (typeof d?.winner === "string") covered.add(d.winner);
|
|
215
|
+
if (typeof d?.loser === "string") covered.add(d.loser);
|
|
216
|
+
} else if (Array.isArray(d?.ids)) {
|
|
217
|
+
for (const id of d.ids) covered.add(id);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
for (const id of snapshot.keys()) {
|
|
221
|
+
if (!covered.has(id)) decisions.push({ action: "keep", ids: [id] });
|
|
222
|
+
}
|
|
223
|
+
const { ok, errors } = validateDecisions(decisions, snapshot, {});
|
|
224
|
+
if (!ok) return { status: "failed", error: `invalid decisions: ${errors.join("; ")}` };
|
|
225
|
+
const { applied, failures, conflicts } = applyDecisions(decisions, service, logger, snapshot, config);
|
|
226
|
+
return {
|
|
227
|
+
status: applied > 0 ? "ok" : failures.length ? "failed" : "noop",
|
|
228
|
+
pairs: selected.length,
|
|
229
|
+
applied,
|
|
230
|
+
failures,
|
|
231
|
+
conflicts
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Phase 2 — archival demotion. No LLM: tiering is time-based, summaries are
|
|
237
|
+
* truncations, and the full body is preserved in _full_content so nothing is
|
|
238
|
+
* lost. Deterministic and cheap, so it runs even with no LLM route.
|
|
239
|
+
*/
|
|
240
|
+
function phaseDemotion(service, config, logger, runId, signal = null) {
|
|
241
|
+
const archiveDays = config.sleepArchiveDays ?? 30;
|
|
242
|
+
const compressDays = config.sleepCompressDays ?? 90;
|
|
243
|
+
const archiveCut = Date.now() - archiveDays * 86400000;
|
|
244
|
+
const compressCut = Date.now() - compressDays * 86400000;
|
|
245
|
+
const demoted = [];
|
|
246
|
+
const archived = [];
|
|
247
|
+
for (const m of service.all()) {
|
|
248
|
+
if (signal?.aborted) break;
|
|
249
|
+
if (m.archived || m.forgotten || m.session_disposed_at) continue;
|
|
250
|
+
const ref = m.last_accessed_at ?? m.updated_at ?? m.created_at;
|
|
251
|
+
if (!ref) continue;
|
|
252
|
+
const t = new Date(ref).getTime();
|
|
253
|
+
if (Number.isNaN(t)) continue;
|
|
254
|
+
if (t < compressCut) {
|
|
255
|
+
service.setArchived(m.id, true);
|
|
256
|
+
archived.push(m.id);
|
|
257
|
+
} else if (t < archiveCut) {
|
|
258
|
+
// minRefTimeMs re-checks freshness inside demoteToSummary's transaction:
|
|
259
|
+
// a recall touch landing after this snapshot must not demote the memory.
|
|
260
|
+
service.demoteToSummary(m.id, makeSummary(m), { minRefTimeMs: archiveCut });
|
|
261
|
+
demoted.push(m.id);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return {
|
|
265
|
+
status: demoted.length || archived.length ? "ok" : "noop",
|
|
266
|
+
demoted,
|
|
267
|
+
archived
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Phase 3 — pattern discovery. The LLM scans the most recent memories and
|
|
273
|
+
* mints type=pattern entries (create actions) with evidence references.
|
|
274
|
+
* The empty snapshot is intentional: create claims no existing id, so the
|
|
275
|
+
* "every id claimed" invariant is trivially satisfied for pure-create lists.
|
|
276
|
+
*/
|
|
277
|
+
async function phasePatterns(ctx, service, config, logger, runId, signal = null) {
|
|
278
|
+
const route = resolveSleepRoute(ctx, config, logger);
|
|
279
|
+
if (!route) return { status: "skipped", reason: "no llm route" };
|
|
280
|
+
const limit = config.sleepPatternMinMemories ?? 100;
|
|
281
|
+
const memories = service
|
|
282
|
+
.list({ limit: 200, includeForgotten: false })
|
|
283
|
+
.filter((m) => !m.archived && !m.session_disposed_at && m.type !== "summary" && m.type !== "pattern")
|
|
284
|
+
.sort((a, b) => (a.updated_at < b.updated_at ? 1 : -1))
|
|
285
|
+
.slice(0, limit);
|
|
286
|
+
if (memories.length === 0) return { status: "skipped", reason: "no memories to scan" };
|
|
287
|
+
if (signal?.aborted) return { status: "aborted", reason: "user activity" };
|
|
288
|
+
const listText = memories
|
|
289
|
+
.map((m) => `id=${m.id} | type=${m.type} | importance=${m.importance} | updated=${m.updated_at} | title=${m.title} | content=${m.content}`)
|
|
290
|
+
.join("\n");
|
|
291
|
+
const maxPatterns = config.sleepMaxPatternPerRun ?? 3;
|
|
292
|
+
const text = await streamText(ctx, {
|
|
293
|
+
provider: route.provider,
|
|
294
|
+
model: route.model,
|
|
295
|
+
purpose: "sleep-pattern",
|
|
296
|
+
maxTokens: 2048,
|
|
297
|
+
...(config.sleepReasoningEffort && config.sleepReasoningEffort !== "none"
|
|
298
|
+
? { reasoningEffort: config.sleepReasoningEffort }
|
|
299
|
+
: {}),
|
|
300
|
+
messages: [
|
|
301
|
+
{ role: "system", content: [{ type: "text", text: PATTERN_PROMPT.replace("N", String(maxPatterns)) }] },
|
|
302
|
+
{ role: "user", content: [{ type: "text", text: listText }] }
|
|
303
|
+
]
|
|
304
|
+
});
|
|
305
|
+
if (text === undefined) return { status: "failed", error: "llm failed" };
|
|
306
|
+
const decisions = parseJsonArray(text);
|
|
307
|
+
if (!decisions || decisions.length === 0) return { status: "skipped", reason: "no patterns found" };
|
|
308
|
+
// Evidence ids are provenance refs; an LLM-fabricated id would mint a dead
|
|
309
|
+
// ev:tag pointing nowhere. Intersect evidence with the scanned set so only
|
|
310
|
+
// real memory references survive.
|
|
311
|
+
const scannedIds = new Set(memories.map((m) => m.id));
|
|
312
|
+
for (const d of decisions) {
|
|
313
|
+
if (d?.action === "create" && Array.isArray(d.evidence)) {
|
|
314
|
+
d.evidence = d.evidence.filter((id) => typeof id === "string" && scannedIds.has(id));
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
const snapshot = new Map();
|
|
318
|
+
const { ok, errors } = validateDecisions(decisions, snapshot, {
|
|
319
|
+
maxCreatePerRun: maxPatterns
|
|
320
|
+
});
|
|
321
|
+
if (!ok) return { status: "failed", error: `invalid decisions: ${errors.join("; ")}` };
|
|
322
|
+
const { applied, failures, conflicts } = applyDecisions(decisions, service, logger, snapshot, config);
|
|
323
|
+
return {
|
|
324
|
+
status: applied > 0 ? "ok" : "noop",
|
|
325
|
+
scanned: memories.length,
|
|
326
|
+
applied,
|
|
327
|
+
failures,
|
|
328
|
+
conflicts
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Phase 4 — entity relation completion. Detects orphan entities (zero
|
|
334
|
+
* relations) and completes implied relations from memory co-occurrence:
|
|
335
|
+
* entities named in the same memory → related_to; container kinds
|
|
336
|
+
* (project/module) → part_of; tech-ish pairs → depends_on. Deterministic,
|
|
337
|
+
* no LLM — cheap, so it runs even without a route. saveRelation is
|
|
338
|
+
* bookkeeping (no write hook), so it never re-triggers the scheduler.
|
|
339
|
+
*/
|
|
340
|
+
function inferRelationType(a, b) {
|
|
341
|
+
if ((a.type === "project" || a.type === "module") && a.type !== b.type) return "part_of";
|
|
342
|
+
if ((b.type === "project" || b.type === "module") && b.type !== a.type) return "part_of";
|
|
343
|
+
if (/npm|plugin|api|sdk|lib|framework|package|deps?|build/i.test(`${a.name} ${b.name}`)) return "depends_on";
|
|
344
|
+
return "related_to";
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
function phaseRelations(service, config, logger, runId, signal = null) {
|
|
348
|
+
const entities = service.listEntities({ limit: 1000 }) ?? [];
|
|
349
|
+
if (entities.length < 2) return { status: "skipped", reason: "too few entities" };
|
|
350
|
+
const orphans = entities.filter((e) => (service.getRelations(e.id) ?? []).length === 0);
|
|
351
|
+
if (orphans.length === 0) return { status: "skipped", reason: "no orphan entities" };
|
|
352
|
+
const memories = service.all().filter((m) => !m.archived && !m.session_disposed_at && !m.forgotten);
|
|
353
|
+
const seen = new Set();
|
|
354
|
+
const related = [];
|
|
355
|
+
const MAX_RELATIONS_PER_ORPHAN = 3;
|
|
356
|
+
for (const o of orphans) {
|
|
357
|
+
if (signal?.aborted) break;
|
|
358
|
+
let made = 0;
|
|
359
|
+
for (const m of memories) {
|
|
360
|
+
if (signal?.aborted || made >= MAX_RELATIONS_PER_ORPHAN) break;
|
|
361
|
+
const text = `${m.title ?? ""} ${m.content ?? ""}`;
|
|
362
|
+
if (!text.includes(o.name)) continue;
|
|
363
|
+
for (const other of entities) {
|
|
364
|
+
if (other.id === o.id || other.name === o.name) continue;
|
|
365
|
+
const key = [o.id, other.id].sort().join("|");
|
|
366
|
+
if (seen.has(key)) continue;
|
|
367
|
+
if (!text.includes(other.name)) continue;
|
|
368
|
+
const relationType = inferRelationType(o, other);
|
|
369
|
+
try {
|
|
370
|
+
service.saveRelation({ from_entity: o.id, to_entity: other.id, relation_type: relationType, memory_id: m.id, metadata: { source: "sleep_relation_completion" } });
|
|
371
|
+
seen.add(key);
|
|
372
|
+
related.push({ from: o.id, to: other.id, type: relationType });
|
|
373
|
+
made++;
|
|
374
|
+
} catch (error) {
|
|
375
|
+
logger?.warn?.(`dsh-mneme sleep: relation ${o.id}/${other.id} failed: ${String(error)}`);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
return {
|
|
381
|
+
status: related.length > 0 ? "ok" : "noop",
|
|
382
|
+
orphanCount: orphans.length,
|
|
383
|
+
related
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
// ---------------------------------------------------------------- run
|
|
388
|
+
|
|
389
|
+
function deriveStatus(phases) {
|
|
390
|
+
const list = Object.values(phases);
|
|
391
|
+
if (list.length === 0) return "noop";
|
|
392
|
+
const anyError = list.some((p) => p.status === "failed" || p.status === "error");
|
|
393
|
+
const anyWork = list.some((p) => p.status === "ok");
|
|
394
|
+
if (anyWork && anyError) return "degraded";
|
|
395
|
+
if (anyError) return "failed";
|
|
396
|
+
if (anyWork) return "ok";
|
|
397
|
+
return "noop";
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* Run one full sleep cycle. Best-effort across all phases; writes a
|
|
402
|
+
* run_type='sleep' audit row (same dream_runs table) so sleep activity is
|
|
403
|
+
* observable alongside consolidation runs.
|
|
404
|
+
*/
|
|
405
|
+
export async function runSleep(ctx, service, config, logger, semantic = null, signal = null) {
|
|
406
|
+
const runId = randomUUID();
|
|
407
|
+
const phases = {};
|
|
408
|
+
const attempt = async (name, fn) => {
|
|
409
|
+
if (signal?.aborted) return; // user resumed activity — stop before next phase
|
|
410
|
+
try {
|
|
411
|
+
phases[name] = await fn();
|
|
412
|
+
} catch (error) {
|
|
413
|
+
phases[name] = { status: "failed", error: error?.message ?? String(error) };
|
|
414
|
+
logger?.warn?.(`dsh-mneme sleep: ${name} phase failed: ${error?.message ?? error}`);
|
|
415
|
+
}
|
|
416
|
+
};
|
|
417
|
+
await attempt("conflicts", () => phaseConflicts(ctx, service, config, logger, runId, semantic, signal));
|
|
418
|
+
await attempt("demotion", () => phaseDemotion(service, config, logger, runId, signal));
|
|
419
|
+
await attempt("patterns", () => phasePatterns(ctx, service, config, logger, runId, signal));
|
|
420
|
+
await attempt("relations", () => phaseRelations(service, config, logger, runId, signal));
|
|
421
|
+
|
|
422
|
+
const status = deriveStatus(phases);
|
|
423
|
+
const route = resolveSleepRoute(ctx, config, logger);
|
|
424
|
+
const totalApplied = Object.values(phases).reduce((n, p) => n + (Number.isInteger(p?.applied) ? p.applied : 0), 0);
|
|
425
|
+
const snapshotHash = createHash("sha256").update(JSON.stringify(phases)).digest("hex");
|
|
426
|
+
const receipt = buildReceipt({
|
|
427
|
+
runId,
|
|
428
|
+
status,
|
|
429
|
+
snapshotHash,
|
|
430
|
+
inputCount: 0,
|
|
431
|
+
applied: totalApplied,
|
|
432
|
+
summaryStored: false
|
|
433
|
+
});
|
|
434
|
+
try {
|
|
435
|
+
service.saveDreamRun({
|
|
436
|
+
id: runId,
|
|
437
|
+
status,
|
|
438
|
+
provider: route?.provider,
|
|
439
|
+
model: route?.model,
|
|
440
|
+
snapshot_hash: snapshotHash,
|
|
441
|
+
input_count: 0,
|
|
442
|
+
input: null,
|
|
443
|
+
decisions: phases,
|
|
444
|
+
outcome: phases,
|
|
445
|
+
applied: totalApplied,
|
|
446
|
+
summary_stored: false,
|
|
447
|
+
receipt,
|
|
448
|
+
policy_epoch: config.policyEpoch ?? 0,
|
|
449
|
+
run_type: "sleep"
|
|
450
|
+
});
|
|
451
|
+
} catch (error) {
|
|
452
|
+
logger?.warn?.(`dsh-mneme sleep: failed to record audit run: ${String(error)}`);
|
|
453
|
+
}
|
|
454
|
+
return { ok: status === "ok" || status === "degraded", status, runId, phases, receipt };
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
// ---------------------------------------------------------------- scheduler
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* Idle-triggered scheduler. DSH plugins have no resident cron, so a sleep run
|
|
461
|
+
* fires when: sleep is enabled, the store has been quiet for sleepIdleMinutes,
|
|
462
|
+
* and the previous run is older than sleepMinIntervalHours. noteWrite() is
|
|
463
|
+
* called on every store write and (re)arms an idle timer that re-checks at the
|
|
464
|
+
* exact moment the idle window elapses — no polling, no cron.
|
|
465
|
+
*
|
|
466
|
+
* A `now` clock can be injected for tests; it defaults to Date.now.
|
|
467
|
+
*/
|
|
468
|
+
export function createSleepScheduler({
|
|
469
|
+
service,
|
|
470
|
+
config,
|
|
471
|
+
logger,
|
|
472
|
+
onRun = null,
|
|
473
|
+
now = () => Date.now(),
|
|
474
|
+
setTimeoutFn = setTimeout,
|
|
475
|
+
clearTimeoutFn = clearTimeout
|
|
476
|
+
}) {
|
|
477
|
+
let lastWriteAt = now();
|
|
478
|
+
let lastRunAt = 0;
|
|
479
|
+
let running = false;
|
|
480
|
+
let disposed = false;
|
|
481
|
+
let idleTimer = null;
|
|
482
|
+
let sleepAbort = null;
|
|
483
|
+
|
|
484
|
+
function armIdleTimer() {
|
|
485
|
+
if (disposed || idleTimer) return;
|
|
486
|
+
if (config.sleepModeEnabled !== true) return;
|
|
487
|
+
const idleMs = (config.sleepIdleMinutes ?? 5) * 60000;
|
|
488
|
+
const delay = Math.max(0, idleMs - (now() - lastWriteAt)) + 1000;
|
|
489
|
+
idleTimer = setTimeoutFn(async () => {
|
|
490
|
+
idleTimer = null;
|
|
491
|
+
await maybeSchedule();
|
|
492
|
+
}, delay);
|
|
493
|
+
idleTimer.unref?.();
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
function shouldRun(at = now()) {
|
|
497
|
+
if (disposed || running) return false;
|
|
498
|
+
if (config.sleepModeEnabled !== true) return false;
|
|
499
|
+
if (at - lastWriteAt < (config.sleepIdleMinutes ?? 5) * 60000) return false;
|
|
500
|
+
// lastRunAt === 0 means never ran — the min-interval check must not block
|
|
501
|
+
// the very first cycle (a real run stamps a nonzero timestamp).
|
|
502
|
+
if (lastRunAt > 0 && at - lastRunAt < (config.sleepMinIntervalHours ?? 8) * 3600000) return false;
|
|
503
|
+
return true;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/** Called on writes: resets the idle clock and re-arms the fire timer. The
|
|
507
|
+
* pending timer is cleared first — a stale timer armed against the old idle
|
|
508
|
+
* window would otherwise fire early, fail shouldRun, and leave nothing armed
|
|
509
|
+
* for the next window (a missed trigger until the next write).
|
|
510
|
+
*
|
|
511
|
+
* While a sleep run is executing (running=true) the in-flight AbortController
|
|
512
|
+
* is NOT aborted: the run's own writes (demoteToSummary / setArchived ride
|
|
513
|
+
* the normal write-hook path) would otherwise self-abort the cycle. External
|
|
514
|
+
* activity during the run still resets the idle clock here, so no new cycle
|
|
515
|
+
* fires until the store is quiet again. */
|
|
516
|
+
function noteWrite() {
|
|
517
|
+
lastWriteAt = now();
|
|
518
|
+
if (!running && sleepAbort) {
|
|
519
|
+
sleepAbort.abort(); // user resumed activity — interrupt an idle run
|
|
520
|
+
}
|
|
521
|
+
if (idleTimer) {
|
|
522
|
+
clearTimeoutFn(idleTimer);
|
|
523
|
+
idleTimer = null;
|
|
524
|
+
}
|
|
525
|
+
armIdleTimer();
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
async function maybeSchedule() {
|
|
529
|
+
if (!shouldRun()) return false;
|
|
530
|
+
running = true;
|
|
531
|
+
const abort = new AbortController();
|
|
532
|
+
sleepAbort = abort;
|
|
533
|
+
try {
|
|
534
|
+
lastRunAt = now();
|
|
535
|
+
const result = await service.enqueue(() =>
|
|
536
|
+
onRun ? onRun(abort.signal) : Promise.resolve({ ok: true, skipped: true })
|
|
537
|
+
);
|
|
538
|
+
return !!(result && result.ok);
|
|
539
|
+
} catch (error) {
|
|
540
|
+
logger?.warn?.(`dsh-mneme sleep: run failed: ${error?.message ?? error}`);
|
|
541
|
+
return false;
|
|
542
|
+
} finally {
|
|
543
|
+
sleepAbort = null;
|
|
544
|
+
running = false;
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
async function dispose() {
|
|
549
|
+
disposed = true;
|
|
550
|
+
if (idleTimer) {
|
|
551
|
+
clearTimeoutFn(idleTimer);
|
|
552
|
+
idleTimer = null;
|
|
553
|
+
}
|
|
554
|
+
if (sleepAbort) {
|
|
555
|
+
sleepAbort.abort();
|
|
556
|
+
sleepAbort = null;
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
return { noteWrite, maybeSchedule, shouldRun, dispose };
|
|
561
|
+
}
|