@modusensus/dsh-mneme 0.8.3 → 0.8.5
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 +727 -636
- package/bin/cli.mjs +0 -0
- package/bin/dsh-mneme-mcp.mjs +510 -0
- package/cordis.patch.yml +21 -21
- package/lib/api-standalone.js +506 -316
- package/lib/api.js +1403 -1316
- package/lib/bootstrap.js +211 -0
- package/lib/client.js +4657 -4366
- package/lib/commands.js +95 -95
- package/lib/config.js +615 -510
- package/lib/document.js +221 -0
- package/lib/dream/clustering.js +118 -118
- package/lib/dream/decisions.js +658 -658
- package/lib/dream/narratives.js +44 -0
- package/lib/dream/sleep.js +702 -700
- package/lib/dream.js +147 -16
- package/lib/embedding.js +177 -177
- package/lib/entities/extractor.js +249 -249
- package/lib/guide.js +44 -0
- package/lib/heat.js +144 -136
- package/lib/hot-memory.js +46 -46
- package/lib/index.js +515 -515
- package/lib/inject.js +375 -295
- package/lib/lang.js +208 -179
- package/lib/local-embedder.js +320 -320
- package/lib/mirror.js +198 -198
- package/lib/organize.js +450 -0
- package/lib/quality-filter.js +126 -126
- package/lib/recall-stats.js +125 -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 -235
- package/lib/runtime/loader.js +252 -249
- 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 +204 -191
- package/lib/scope.js +118 -118
- package/lib/search/adaptive.js +60 -22
- package/lib/search/bm25.js +96 -96
- package/lib/service.js +2324 -2087
- package/lib/settings.js +459 -420
- package/lib/store.js +2549 -2367
- package/lib/summarize.js +820 -449
- package/lib/tools.js +702 -574
- package/lib/vector-index.js +110 -110
- package/lib/version-check.js +122 -122
- package/package.json +109 -108
- package/runtime-manifest.json +526 -596
- package/scripts/benchmark-embed.js +201 -201
- package/scripts/benchmark-recall.js +199 -199
- package/scripts/benchmark-rerank.js +166 -166
- package/scripts/build-runtime-manifest.mjs +224 -217
- 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 +67 -61
- package/src/api-standalone.js +506 -316
- package/src/api.js +1403 -1316
- package/src/bootstrap.js +211 -0
- package/src/commands.js +95 -95
- package/src/config.js +615 -510
- package/src/document.js +221 -0
- package/src/dream/clustering.js +118 -118
- package/src/dream/decisions.js +658 -658
- package/src/dream/narratives.js +44 -0
- package/src/dream/sleep.js +702 -700
- package/src/dream.js +147 -16
- package/src/embedding.js +177 -177
- package/src/entities/extractor.js +249 -249
- package/src/guide.js +44 -0
- package/src/heat.js +144 -136
- package/src/hot-memory.js +46 -46
- package/src/index.js +515 -515
- package/src/inject.js +375 -295
- package/src/lang.js +208 -179
- package/src/local-embedder.js +320 -320
- package/src/mirror.js +198 -198
- package/src/organize.js +450 -0
- package/src/quality-filter.js +126 -126
- package/src/recall-stats.js +125 -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 -235
- package/src/runtime/loader.js +252 -249
- 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 +204 -191
- package/src/scope.js +118 -118
- package/src/search/adaptive.js +60 -22
- package/src/search/bm25.js +96 -96
- package/src/service.js +2324 -2087
- package/src/settings.js +459 -420
- package/src/store.js +2549 -2367
- package/src/summarize.js +820 -449
- package/src/tools.js +702 -574
- package/src/vector-index.js +110 -110
- package/src/version-check.js +122 -122
- package/test/adaptive-inject-budget.test.js +85 -0
- package/test/api-routes-gaps.test.js +218 -218
- package/test/api.test.js +1640 -1482
- package/test/audit.test.js +448 -448
- package/test/benchmark.test.js +94 -94
- package/test/bootstrap.test.js +220 -0
- package/test/client.test.js +860 -736
- 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/document.test.js +523 -0
- 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.test.js +1308 -1254
- package/test/embedding-ready.test.js +196 -196
- package/test/entities.test.js +547 -547
- package/test/entity-recall.test.js +179 -0
- 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/heat.test.js +175 -148
- 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/hot-memory.test.js +145 -145
- package/test/inject-content-cap.test.js +91 -0
- package/test/inject-first-round-bm25.test.js +89 -89
- package/test/inject-heat.test.js +61 -0
- package/test/inject-pools.test.js +170 -0
- package/test/inject-recall.test.js +105 -0
- package/test/inject-rotation.test.js +161 -161
- package/test/inject.test.js +283 -249
- package/test/lang.test.js +87 -87
- package/test/lib-smoke.test.js +96 -96
- package/test/llm-audit.test.js +462 -309
- package/test/llm-message-source.test.js +177 -177
- package/test/local-embedder.test.js +249 -249
- package/test/mcp-stdio.test.js +385 -0
- 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/narrative-bars.test.js +162 -0
- package/test/organize.test.js +418 -0
- package/test/peer-blockers.test.js +193 -193
- package/test/policy-epoch.test.js +259 -259
- package/test/quality-filter.test.js +186 -186
- package/test/reasoning-effort.test.js +746 -746
- package/test/recall-evals.test.js +235 -235
- package/test/recall-layer.test.js +326 -326
- package/test/recall-stats.test.js +123 -0
- package/test/receipt-chain.test.js +451 -451
- package/test/reflection.test.js +226 -226
- package/test/reindex-backfill.test.js +62 -62
- package/test/reranker-single-logit.test.js +73 -73
- package/test/reranker.test.js +197 -197
- package/test/resident-status.test.js +83 -0
- 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 -237
- package/test/runtime-manifest.test.js +159 -158
- 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 +279 -214
- package/test/scope-candidates.test.js +239 -239
- package/test/scope-explicit.test.js +452 -452
- package/test/scope-retrieval.test.js +197 -197
- package/test/scope-storage.test.js +255 -255
- package/test/scope-strict.test.js +231 -231
- package/test/search-fusion.test.js +90 -90
- package/test/semantic.test.js +124 -124
- package/test/service-search.test.js +174 -174
- package/test/service.test.js +405 -405
- package/test/settings.test.js +261 -261
- package/test/skip-invalid-restore.test.js +262 -262
- package/test/sleep-action-set.test.js +422 -422
- package/test/sleep-heat.test.js +126 -125
- package/test/sleep-scheduler-rearm.test.js +89 -89
- package/test/sleep.test.js +410 -373
- package/test/standalone-api.test.js +544 -359
- package/test/store-vector-cache.test.js +98 -98
- package/test/store.test.js +396 -383
- package/test/stress.test.js +209 -209
- package/test/summarize.test.js +1288 -575
- package/test/tools-dto-schema-matrix.test.js +325 -0
- package/test/tools-memory-get-schema.test.js +65 -65
- package/test/tools.test.js +374 -341
- 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/summarize.test.js
CHANGED
|
@@ -1,575 +1,1288 @@
|
|
|
1
|
-
import test from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
const
|
|
53
|
-
[
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
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
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
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
|
-
});
|
|
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
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
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
|
-
test("
|
|
213
|
-
const
|
|
214
|
-
const
|
|
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
|
-
const { events,
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
);
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
};
|
|
265
|
-
const
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
assert.
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
const
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
const
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
test("
|
|
326
|
-
const
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
const
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
assert.
|
|
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
|
-
await handler(session, { seq:
|
|
378
|
-
assert.equal(calls.length,
|
|
379
|
-
assert.equal(
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
const
|
|
385
|
-
assert.ok(
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
const { events, calls } = setup(
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
await
|
|
405
|
-
assert.equal(calls.length,
|
|
406
|
-
});
|
|
407
|
-
|
|
408
|
-
test("
|
|
409
|
-
const { events,
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
const
|
|
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
|
-
const
|
|
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
|
-
assert.
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
}
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
);
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { tmpdir } from "node:os";
|
|
6
|
+
import { createStore } from "../src/store.js";
|
|
7
|
+
import { createService } from "../src/service.js";
|
|
8
|
+
import { createSummarizer, parseSummaryJson, parsePeakSpec, isInPeakWindow, nextOffPeakAt } from "../src/summarize.js";
|
|
9
|
+
import { createSettings } from "../src/settings.js";
|
|
10
|
+
|
|
11
|
+
// Issue #239 第 4 项:可注入的假时钟 + 定时器——错峰队列的测试必须能「推进时间」,
|
|
12
|
+
// 真实 setTimeout 会让补跑路径要么测不到、要么拖慢测试。
|
|
13
|
+
function fakeClock(start) {
|
|
14
|
+
let current = start.getTime();
|
|
15
|
+
const timers = [];
|
|
16
|
+
const deps = {
|
|
17
|
+
now: () => current,
|
|
18
|
+
setTimeoutFn: (fn, ms) => {
|
|
19
|
+
const entry = { fn, at: current + ms };
|
|
20
|
+
timers.push(entry);
|
|
21
|
+
return entry;
|
|
22
|
+
},
|
|
23
|
+
clearTimeoutFn: (entry) => {
|
|
24
|
+
const i = timers.indexOf(entry);
|
|
25
|
+
if (i !== -1) timers.splice(i, 1);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
const flush = async () => {
|
|
29
|
+
// 补跑是异步链(enqueueDistill → stream):推完时间要给微任务与一轮宏任务机会。
|
|
30
|
+
for (let i = 0; i < 4; i++) await new Promise((resolve) => setTimeout(resolve, 0));
|
|
31
|
+
};
|
|
32
|
+
return {
|
|
33
|
+
deps,
|
|
34
|
+
pending: () => timers.length,
|
|
35
|
+
async advanceTo(date) {
|
|
36
|
+
current = date.getTime();
|
|
37
|
+
for (const entry of [...timers].sort((a, b) => a.at - b.at)) {
|
|
38
|
+
if (entry.at > current) continue;
|
|
39
|
+
timers.splice(timers.indexOf(entry), 1);
|
|
40
|
+
entry.fn();
|
|
41
|
+
}
|
|
42
|
+
await flush();
|
|
43
|
+
},
|
|
44
|
+
async advance(ms) {
|
|
45
|
+
await this.advanceTo(new Date(current + ms));
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function setup(over = {}, opts = {}) {
|
|
51
|
+
const store = opts.store ?? createStore(":memory:");
|
|
52
|
+
const service = createService({ store, mirror: null, config: {} });
|
|
53
|
+
const events = [];
|
|
54
|
+
const calls = [];
|
|
55
|
+
const ctx = {
|
|
56
|
+
on(name, fn) {
|
|
57
|
+
events.push({ name, fn });
|
|
58
|
+
return () => {
|
|
59
|
+
const i = events.findIndex((e) => e.name === name && e.fn === fn);
|
|
60
|
+
if (i !== -1) events.splice(i, 1);
|
|
61
|
+
};
|
|
62
|
+
},
|
|
63
|
+
llm: {
|
|
64
|
+
stream(options) {
|
|
65
|
+
calls.push(options);
|
|
66
|
+
if (opts.stream) return opts.stream(options);
|
|
67
|
+
const json = JSON.stringify([
|
|
68
|
+
{ type: "decision", title: "选型", content: "确定用 node:sqlite", importance: 4 },
|
|
69
|
+
{ type: "preference", title: "语言", content: "用户喜欢中文交流", importance: 5 }
|
|
70
|
+
]);
|
|
71
|
+
return (async function* () {
|
|
72
|
+
yield { type: "block-start", block: { type: "text" } };
|
|
73
|
+
yield { type: "text-delta", delta: json };
|
|
74
|
+
yield { type: "block-end", block: { type: "text" } };
|
|
75
|
+
yield { type: "finish", kind: "ok" };
|
|
76
|
+
})();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
const config = { autoSummarize: true, ...over };
|
|
81
|
+
const summarizer = createSummarizer(ctx, service, config, opts.deps ?? {});
|
|
82
|
+
return { store, service, events, calls, summarizer };
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// A realistic direct human prompt event (source.kind === "user").
|
|
86
|
+
function userMessage(text, seq) {
|
|
87
|
+
const event = {
|
|
88
|
+
type: "user/message",
|
|
89
|
+
data: { source: { kind: "user" }, content: [{ type: "text", text }] }
|
|
90
|
+
};
|
|
91
|
+
if (seq !== undefined) event.seq = seq;
|
|
92
|
+
return event;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function toolResultEvent(text, isError, seq) {
|
|
96
|
+
return {
|
|
97
|
+
seq,
|
|
98
|
+
type: "tool/result",
|
|
99
|
+
data: {
|
|
100
|
+
message: {
|
|
101
|
+
source: { kind: "tool", callId: `call-${seq}` },
|
|
102
|
+
content: [{
|
|
103
|
+
type: "tool-result",
|
|
104
|
+
toolCallId: `call-${seq}`,
|
|
105
|
+
isError,
|
|
106
|
+
content: [{ type: "text", text }]
|
|
107
|
+
}]
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function codeDispatchEvent(text, isError, seq) {
|
|
114
|
+
return {
|
|
115
|
+
seq,
|
|
116
|
+
type: "tool/code-dispatch",
|
|
117
|
+
data: {
|
|
118
|
+
isError,
|
|
119
|
+
content: [{ type: "text", text }]
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function inboxMessage(id, kind, text) {
|
|
125
|
+
return {
|
|
126
|
+
id,
|
|
127
|
+
role: "user",
|
|
128
|
+
source: { kind },
|
|
129
|
+
content: [{ type: "text", text }]
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
test("parseSummaryJson extracts valid entries and skips malformed ones", () => {
|
|
134
|
+
const parsed = parseSummaryJson(`前导文字 {"a":1}
|
|
135
|
+
[
|
|
136
|
+
{"type":"decision","title":"t1","content":"c1","importance":4},
|
|
137
|
+
{"type":"nonsense","title":"bad","content":"x"},
|
|
138
|
+
"garbage",
|
|
139
|
+
{"type":"preference","title":"t2","content":"c2","importance":2}
|
|
140
|
+
]`);
|
|
141
|
+
assert.equal(parsed.length, 2);
|
|
142
|
+
assert.equal(parsed[0].type, "decision");
|
|
143
|
+
assert.equal(parsed[1].type, "preference");
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
test("subscribes to session/event when autoSummarize enabled", () => {
|
|
147
|
+
const { events } = setup();
|
|
148
|
+
assert.ok(events.some((e) => e.name === "session/event"));
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
test("does not subscribe when autoSummarize disabled", () => {
|
|
152
|
+
const { events } = setup({ autoSummarize: false });
|
|
153
|
+
assert.ok(!events.some((e) => e.name === "session/event"));
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
test("turn/end event triggers summarization and stores entries", async () => {
|
|
157
|
+
const { events, store } = setup();
|
|
158
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
159
|
+
const session = {
|
|
160
|
+
id: "s1",
|
|
161
|
+
requestHeader: () => ({ config: { provider: "deepseek", model: "deepseek-chat" } }),
|
|
162
|
+
events: [userMessage("帮我选型"), { seq: 2, type: "turn/end" }]
|
|
163
|
+
};
|
|
164
|
+
await handler(session, { seq: 2, type: "turn/end" });
|
|
165
|
+
assert.equal(store.count(), 2);
|
|
166
|
+
const all = store.all();
|
|
167
|
+
assert.ok(all.some((m) => m.type === "decision"));
|
|
168
|
+
assert.ok(all.some((m) => m.type === "preference"));
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
test("skips summarization for events other than turn/end", async () => {
|
|
172
|
+
const { events, store, calls } = setup();
|
|
173
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
174
|
+
const session = { id: "s1", requestHeader: () => ({ config: {} }), events: [] };
|
|
175
|
+
await handler(session, { seq: 1, type: "user/message" });
|
|
176
|
+
assert.equal(store.count(), 0);
|
|
177
|
+
assert.equal(calls.length, 0);
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
// DSH ≥0.1.2-rc removed Session.events; the session object only exposes
|
|
181
|
+
// snapshotEvents(). Regression for #59: with only the old .events path the
|
|
182
|
+
// collector saw an empty log and no LLM call ever happened.
|
|
183
|
+
test("reads events from snapshotEvents() when Session.events is absent", async () => {
|
|
184
|
+
const { events, store, calls } = setup();
|
|
185
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
186
|
+
const session = {
|
|
187
|
+
id: "s1",
|
|
188
|
+
requestHeader: () => ({ config: { provider: "deepseek", model: "deepseek-chat" } }),
|
|
189
|
+
snapshotEvents: () => [userMessage("帮我选型"), { seq: 2, type: "turn/end" }]
|
|
190
|
+
};
|
|
191
|
+
await handler(session, { seq: 2, type: "turn/end" });
|
|
192
|
+
assert.equal(calls.length, 1, "an LLM call must be made");
|
|
193
|
+
assert.equal(store.count(), 2);
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
// --- Issue #239:成本感知级联(零 LLM 预判)与有界检查点 ---------------------
|
|
197
|
+
// 验收对齐 #239:日志可见 skip 原因、默认配置下行为零变化、开启后调用量下降。
|
|
198
|
+
|
|
199
|
+
test("#239 预判默认关:阈值为 0 时不改变现状,照常发起调用", async () => {
|
|
200
|
+
const { events, calls, store } = setup();
|
|
201
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
202
|
+
const session = {
|
|
203
|
+
id: "s239-default",
|
|
204
|
+
requestHeader: () => ({ config: { provider: "deepseek", model: "deepseek-chat" } }),
|
|
205
|
+
events: [userMessage("短", 1), { seq: 2, type: "turn/end" }]
|
|
206
|
+
};
|
|
207
|
+
await handler(session, { seq: 2, type: "turn/end" });
|
|
208
|
+
assert.equal(calls.length, 1, "默认不预判:再短的窗口也照常蒸馏");
|
|
209
|
+
assert.equal(store.count(), 2);
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
test("#239 窗口过短:零 LLM 预判拦下调用,留 skip 审计并消费游标", async () => {
|
|
213
|
+
const { events, calls, service, store } = setup({ summarizeMinWindowChars: 1000 });
|
|
214
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
215
|
+
const session = {
|
|
216
|
+
id: "s239-small",
|
|
217
|
+
requestHeader: () => ({ config: { provider: "deepseek", model: "deepseek-chat" } }),
|
|
218
|
+
events: [userMessage("好", 1), { seq: 2, type: "turn/end" }]
|
|
219
|
+
};
|
|
220
|
+
await handler(session, { seq: 2, type: "turn/end" });
|
|
221
|
+
assert.equal(calls.length, 0, "短窗口不得发起 LLM 调用");
|
|
222
|
+
assert.equal(store.count(), 0, "被拦下就不该写入记忆");
|
|
223
|
+
const rows = service.listLlmAudits();
|
|
224
|
+
assert.equal(rows[0].status, "skipped");
|
|
225
|
+
assert.equal(rows[0].error_message, "window-too-small", "skip 原因必须可观测");
|
|
226
|
+
|
|
227
|
+
await handler(session, { seq: 2, type: "turn/end" });
|
|
228
|
+
assert.equal(calls.length, 0);
|
|
229
|
+
assert.equal(service.listLlmAudits().length, 1, "游标已消费:同一窗口不重复评估、不重复审计");
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
test("#239 窗口达到阈值时预判放行(不误杀)", async () => {
|
|
233
|
+
const { events, calls } = setup({ summarizeMinWindowChars: 5 });
|
|
234
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
235
|
+
const session = {
|
|
236
|
+
id: "s239-pass",
|
|
237
|
+
requestHeader: () => ({ config: { provider: "deepseek", model: "deepseek-chat" } }),
|
|
238
|
+
events: [userMessage("这是一段足够长的用户输入", 1), { seq: 2, type: "turn/end" }]
|
|
239
|
+
};
|
|
240
|
+
await handler(session, { seq: 2, type: "turn/end" });
|
|
241
|
+
assert.equal(calls.length, 1);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
test("#239 每会话 run 预算:额度用尽后不再调用,审计写明原因", async () => {
|
|
245
|
+
const { events, calls, service } = setup({ summarizeMaxRunsPerSession: 1, distillRateLimitIntervalMs: 0 });
|
|
246
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
247
|
+
const session = {
|
|
248
|
+
id: "s239-cap",
|
|
249
|
+
requestHeader: () => ({ config: { provider: "deepseek", model: "deepseek-chat" } }),
|
|
250
|
+
events: [userMessage("第一轮", 1), { seq: 2, type: "turn/end" }]
|
|
251
|
+
};
|
|
252
|
+
await handler(session, { seq: 2, type: "turn/end" });
|
|
253
|
+
assert.equal(calls.length, 1, "第一次在预算内");
|
|
254
|
+
|
|
255
|
+
session.events.push(userMessage("第二轮", 3), { seq: 4, type: "turn/end" });
|
|
256
|
+
await handler(session, { seq: 4, type: "turn/end" });
|
|
257
|
+
assert.equal(calls.length, 1, "预算用尽后不得再调用");
|
|
258
|
+
const rows = service.listLlmAudits();
|
|
259
|
+
assert.equal(rows[0].status, "skipped");
|
|
260
|
+
assert.equal(rows[0].error_message, "max-runs-per-session");
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
test("#239 预算只计真实调用:被预判拦下的窗口不消耗额度", async () => {
|
|
264
|
+
const { events, calls } = setup({ summarizeMaxRunsPerSession: 1, summarizeMinWindowChars: 10, distillRateLimitIntervalMs: 0 });
|
|
265
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
266
|
+
const session = {
|
|
267
|
+
id: "s239-budget",
|
|
268
|
+
requestHeader: () => ({ config: { provider: "deepseek", model: "deepseek-chat" } }),
|
|
269
|
+
events: [userMessage("短", 1), { seq: 2, type: "turn/end" }]
|
|
270
|
+
};
|
|
271
|
+
await handler(session, { seq: 2, type: "turn/end" });
|
|
272
|
+
assert.equal(calls.length, 0, "第一轮被零 LLM 预判拦下");
|
|
273
|
+
|
|
274
|
+
session.events.push(userMessage("第二轮写得足够长,应该放行并消耗唯一的一次额度", 3), { seq: 4, type: "turn/end" });
|
|
275
|
+
await handler(session, { seq: 4, type: "turn/end" });
|
|
276
|
+
assert.equal(calls.length, 1, "零成本窗口不该吃掉预算");
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
// --- Issue #239 第 4 项:错峰队列 -------------------------------------------------
|
|
280
|
+
|
|
281
|
+
test("#239-4 高峰时段解析:合法 / 多段 / 跨零点 / 星期前缀 / 非法一律按未配置", () => {
|
|
282
|
+
assert.equal(parsePeakSpec(""), null);
|
|
283
|
+
assert.equal(parsePeakSpec(" "), null);
|
|
284
|
+
assert.deepEqual(parsePeakSpec("09:00-18:00"), { days: null, windows: [{ start: 540, end: 1080 }] });
|
|
285
|
+
assert.deepEqual(parsePeakSpec("09:00-12:00, 14:00-18:00"), {
|
|
286
|
+
days: null,
|
|
287
|
+
windows: [{ start: 540, end: 720 }, { start: 840, end: 1080 }]
|
|
288
|
+
});
|
|
289
|
+
assert.deepEqual(parsePeakSpec("23:00-06:00"), { days: null, windows: [{ start: 1380, end: 360 }] });
|
|
290
|
+
// 星期前缀:ISO 1=周一…7=周日,也认 mon..sun;支持列表与跨周环绕
|
|
291
|
+
assert.deepEqual(parsePeakSpec("mon-fri 08:00-12:00,14:00-18:00"), {
|
|
292
|
+
days: [1, 2, 3, 4, 5],
|
|
293
|
+
windows: [{ start: 480, end: 720 }, { start: 840, end: 1080 }]
|
|
294
|
+
});
|
|
295
|
+
assert.deepEqual(parsePeakSpec("1-5 08:00-12:00").days, [1, 2, 3, 4, 5]);
|
|
296
|
+
assert.deepEqual(parsePeakSpec("sat,sun 10:00-12:00").days, [6, 7]);
|
|
297
|
+
assert.deepEqual(parsePeakSpec("fri-mon 10:00-12:00").days, [1, 5, 6, 7], "跨周环绕");
|
|
298
|
+
// 排程是省钱手段,绝不该因为写错格式把蒸馏停掉:任何非法写法都当作「关闭」。
|
|
299
|
+
for (const bad of ["09:00~18:00", "24:00-06:00", "09:60-18:00", "09:00-09:00", "abc", "09:00-18:00,oops", "weekday 09:00-18:00", "8 09:00-18:00"]) {
|
|
300
|
+
assert.equal(parsePeakSpec(bad), null, `${bad} 应视为未配置`);
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
test("#239-4 高峰判定与补跑时刻(跨零点、右开区间)", () => {
|
|
305
|
+
const at = (h, m) => new Date(2026, 8, 19, h, m, 30);
|
|
306
|
+
assert.equal(isInPeakWindow(at(10, 0), "09:00-18:00"), true);
|
|
307
|
+
assert.equal(isInPeakWindow(at(18, 0), "09:00-18:00"), false, "右开区间:18:00 已出高峰");
|
|
308
|
+
assert.equal(isInPeakWindow(at(3, 0), "23:00-06:00"), true, "跨零点时段");
|
|
309
|
+
assert.equal(isInPeakWindow(at(12, 0), "23:00-06:00"), false);
|
|
310
|
+
assert.equal(isInPeakWindow(at(10, 0), ""), false, "未配置 = 永不高峰");
|
|
311
|
+
const next = nextOffPeakAt(at(10, 30), "09:00-18:00");
|
|
312
|
+
assert.equal(next.getHours(), 18);
|
|
313
|
+
assert.equal(next.getMinutes(), 0);
|
|
314
|
+
assert.equal(nextOffPeakAt(at(3, 0), "23:00-06:00").getHours(), 6, "跨零点取次日 06:00 结束点");
|
|
315
|
+
assert.equal(nextOffPeakAt(at(20, 0), "09:00-18:00"), null, "非高峰时刻无需补跑");
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
// 夹具:按 ISO 星期几(1=周一…7=周日)从当月 1 号推算出日期,避免把星期写死在测试里。
|
|
319
|
+
function dateOnIsoDay(isoDay, hour, minute) {
|
|
320
|
+
const first = new Date(2026, 8, 1, hour, minute, 0);
|
|
321
|
+
const firstIso = first.getDay() === 0 ? 7 : first.getDay();
|
|
322
|
+
return new Date(2026, 8, 1 + ((isoDay - firstIso + 7) % 7), hour, minute, 0);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
test("#239-4 星期过滤:工作日高峰不误伤周末(跨零点段按「开窗那天」认星期)", () => {
|
|
326
|
+
const spec = "mon-fri 08:00-12:00,14:00-18:00";
|
|
327
|
+
assert.equal(dateOnIsoDay(1, 9, 0).getDay(), 1, "夹具自检:周一");
|
|
328
|
+
assert.equal(dateOnIsoDay(6, 9, 0).getDay(), 6, "夹具自检:周六");
|
|
329
|
+
assert.equal(isInPeakWindow(dateOnIsoDay(1, 9, 0), spec), true, "工作日 09:00 在高峰");
|
|
330
|
+
assert.equal(isInPeakWindow(dateOnIsoDay(1, 12, 30), spec), false, "工作日午休不在高峰");
|
|
331
|
+
assert.equal(isInPeakWindow(dateOnIsoDay(6, 9, 0), spec), false, "周六同刻不在高峰(按周计费)");
|
|
332
|
+
assert.equal(isInPeakWindow(dateOnIsoDay(7, 15, 0), spec), false, "周日下午不在高峰");
|
|
333
|
+
assert.equal(nextOffPeakAt(dateOnIsoDay(1, 9, 0), spec).getHours(), 12, "工作日顺延到 12:00");
|
|
334
|
+
assert.equal(nextOffPeakAt(dateOnIsoDay(6, 9, 0), spec), null, "周末无需顺延");
|
|
335
|
+
// 跨零点段:周五 23:00 开的窗口延续到周六凌晨,仍算高峰;周六开的则不覆盖周日
|
|
336
|
+
const overnight = "mon-fri 23:00-06:00";
|
|
337
|
+
assert.equal(isInPeakWindow(dateOnIsoDay(6, 2, 0), overnight), true, "周六 02:00 属周五开的窗口");
|
|
338
|
+
assert.equal(isInPeakWindow(dateOnIsoDay(7, 2, 0), overnight), false, "周日 02:00 属周六开的窗口(周六不在集合内)");
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
test("#239-4 默认关:未配置高峰时行为与现状一致", async () => {
|
|
342
|
+
const clock = fakeClock(new Date(2026, 8, 19, 10, 0, 0));
|
|
343
|
+
const { events, calls } = setup({}, { deps: clock.deps });
|
|
344
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
345
|
+
const session = {
|
|
346
|
+
id: "s239-peak-off",
|
|
347
|
+
requestHeader: () => ({ config: { provider: "deepseek", model: "deepseek-chat" } }),
|
|
348
|
+
events: [userMessage("普通一轮", 1), { seq: 2, type: "turn/end" }]
|
|
349
|
+
};
|
|
350
|
+
await handler(session, { seq: 2, type: "turn/end" });
|
|
351
|
+
assert.equal(calls.length, 1, "未配置高峰 → 照常蒸馏");
|
|
352
|
+
assert.equal(clock.pending(), 0, "不该挂补跑定时器");
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
test("#239-4 高峰内不调模型:skip 审计 + 不消费游标,非高峰补跑整窗", async () => {
|
|
356
|
+
const clock = fakeClock(new Date(2026, 8, 19, 10, 0, 0));
|
|
357
|
+
const { events, calls, service, store } = setup(
|
|
358
|
+
{ summarizePeakHours: "09:00-18:00", distillRateLimitIntervalMs: 0 },
|
|
359
|
+
{ deps: clock.deps }
|
|
360
|
+
);
|
|
361
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
362
|
+
const session = {
|
|
363
|
+
id: "s239-peak",
|
|
364
|
+
requestHeader: () => ({ config: { provider: "deepseek", model: "deepseek-chat" } }),
|
|
365
|
+
events: [userMessage("高峰里的第一段", 1), { seq: 2, type: "turn/end" }]
|
|
366
|
+
};
|
|
367
|
+
await handler(session, { seq: 2, type: "turn/end" });
|
|
368
|
+
assert.equal(calls.length, 0, "高峰期不得调模型");
|
|
369
|
+
assert.equal(store.count(), 0, "不该写入记忆");
|
|
370
|
+
const rows = service.listLlmAudits();
|
|
371
|
+
assert.equal(rows[0].status, "skipped");
|
|
372
|
+
assert.equal(rows[0].error_message, "peak-hours", "skip 原因必须可观测");
|
|
373
|
+
assert.equal(clock.pending(), 1, "应挂一个补跑定时器");
|
|
374
|
+
|
|
375
|
+
// 高峰期间又来一轮:游标未消费 → 窗口累积;仍不调模型、定时器不叠加
|
|
376
|
+
session.events.push(userMessage("高峰里的第二段", 3), { seq: 4, type: "turn/end" });
|
|
377
|
+
await handler(session, { seq: 4, type: "turn/end" });
|
|
378
|
+
assert.equal(calls.length, 0);
|
|
379
|
+
assert.equal(clock.pending(), 1, "每会话只挂一个定时器");
|
|
380
|
+
|
|
381
|
+
// 推到非高峰:一次补跑,且蒸到的是累积后的整窗
|
|
382
|
+
await clock.advanceTo(new Date(2026, 8, 19, 18, 0, 5));
|
|
383
|
+
assert.equal(calls.length, 1, "非高峰补跑一次");
|
|
384
|
+
const prompt = JSON.stringify(calls[0].messages);
|
|
385
|
+
assert.ok(prompt.includes("高峰里的第一段") && prompt.includes("高峰里的第二段"),
|
|
386
|
+
"补跑应蒸馏累积后的完整窗口");
|
|
387
|
+
assert.ok(store.count() > 0, "补跑真的写入了记忆");
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
test("#239-4 顺延上限:长高峰到点照跑,不饿死蒸馏", async () => {
|
|
391
|
+
const clock = fakeClock(new Date(2026, 8, 19, 9, 0, 0));
|
|
392
|
+
const { events, calls } = setup(
|
|
393
|
+
{ summarizePeakHours: "09:00-18:00", summarizePeakMaxDeferMinutes: 30, distillRateLimitIntervalMs: 0 },
|
|
394
|
+
{ deps: clock.deps }
|
|
395
|
+
);
|
|
396
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
397
|
+
const session = {
|
|
398
|
+
id: "s239-peak-cap",
|
|
399
|
+
requestHeader: () => ({ config: { provider: "deepseek", model: "deepseek-chat" } }),
|
|
400
|
+
events: [userMessage("长高峰里的一轮", 1), { seq: 2, type: "turn/end" }]
|
|
401
|
+
};
|
|
402
|
+
await handler(session, { seq: 2, type: "turn/end" });
|
|
403
|
+
assert.equal(calls.length, 0, "高峰内先顺延");
|
|
404
|
+
await clock.advance(31 * 60000);
|
|
405
|
+
assert.equal(calls.length, 1, "上限到点仍处高峰也照跑(bypassPeak)");
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
test("does not call the LLM when no event was added after the last successful seq", async () => {
|
|
409
|
+
const { events, calls } = setup({ distillRateLimitIntervalMs: 0 });
|
|
410
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
411
|
+
const transcript = [userMessage("首轮", 1), { seq: 2, type: "turn/end" }];
|
|
412
|
+
const ranges = [];
|
|
413
|
+
const session = {
|
|
414
|
+
id: "s-noop",
|
|
415
|
+
requestHeader: () => ({ config: { provider: "deepseek", model: "deepseek-chat" } }),
|
|
416
|
+
snapshotEvents(fromSeq, toSeq) {
|
|
417
|
+
ranges.push([fromSeq, toSeq]);
|
|
418
|
+
return transcript;
|
|
419
|
+
}
|
|
420
|
+
};
|
|
421
|
+
|
|
422
|
+
await handler(session, { seq: 2, type: "turn/end" });
|
|
423
|
+
await handler(session, { seq: 2, type: "turn/end" });
|
|
424
|
+
|
|
425
|
+
assert.equal(calls.length, 1, "a successful window must not be distilled twice");
|
|
426
|
+
assert.equal(ranges[1][0], 2, "the successful event seq is passed as the next snapshot lower bound");
|
|
427
|
+
});
|
|
428
|
+
|
|
429
|
+
test("resumes a persisted seq cursor after the summarizer is restarted", async () => {
|
|
430
|
+
const dir = mkdtempSync(join(tmpdir(), "dsh-mneme-distill-cursor-"));
|
|
431
|
+
const dbPath = join(dir, "memory.db");
|
|
432
|
+
let firstStore;
|
|
433
|
+
let firstSummarizer;
|
|
434
|
+
let secondStore;
|
|
435
|
+
let secondSummarizer;
|
|
436
|
+
try {
|
|
437
|
+
firstStore = createStore(dbPath);
|
|
438
|
+
const first = setup({ distillRateLimitIntervalMs: 0 }, { store: firstStore });
|
|
439
|
+
firstSummarizer = first.summarizer;
|
|
440
|
+
const session = {
|
|
441
|
+
id: "s-restart",
|
|
442
|
+
requestHeader: () => ({ config: { provider: "deepseek", model: "deepseek-chat" } }),
|
|
443
|
+
events: [userMessage("重启前的窗口", 1), { seq: 2, type: "turn/end" }]
|
|
444
|
+
};
|
|
445
|
+
const firstHandler = first.events.find((e) => e.name === "session/event").fn;
|
|
446
|
+
await firstHandler(session, { seq: 2, type: "turn/end" });
|
|
447
|
+
assert.equal(first.calls.length, 1);
|
|
448
|
+
assert.equal(firstStore.getDistillCursor("s-restart").last_seq, 2);
|
|
449
|
+
|
|
450
|
+
firstSummarizer.dispose();
|
|
451
|
+
firstStore.close();
|
|
452
|
+
firstSummarizer = undefined;
|
|
453
|
+
firstStore = undefined;
|
|
454
|
+
|
|
455
|
+
secondStore = createStore(dbPath);
|
|
456
|
+
const second = setup({ distillRateLimitIntervalMs: 0 }, { store: secondStore });
|
|
457
|
+
secondSummarizer = second.summarizer;
|
|
458
|
+
const secondHandler = second.events.find((e) => e.name === "session/event").fn;
|
|
459
|
+
await secondHandler(session, { seq: 2, type: "turn/end" });
|
|
460
|
+
|
|
461
|
+
assert.equal(second.calls.length, 0, "a restarted summarizer must reuse the persisted cursor");
|
|
462
|
+
assert.equal(secondStore.count(), 2, "the restart must not duplicate memories");
|
|
463
|
+
assert.equal(secondStore.getDistillCursor("s-restart").last_seq, 2);
|
|
464
|
+
} finally {
|
|
465
|
+
secondSummarizer?.dispose();
|
|
466
|
+
secondStore?.close();
|
|
467
|
+
firstSummarizer?.dispose();
|
|
468
|
+
firstStore?.close();
|
|
469
|
+
rmSync(dir, { recursive: true, force: true });
|
|
470
|
+
}
|
|
471
|
+
});
|
|
472
|
+
|
|
473
|
+
test("treats a valid empty summary as a successful cursor commit", async () => {
|
|
474
|
+
const { events, store, calls } = setup(
|
|
475
|
+
{ distillRateLimitIntervalMs: 0 },
|
|
476
|
+
{ stream: streamOf([]) }
|
|
477
|
+
);
|
|
478
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
479
|
+
const session = {
|
|
480
|
+
id: "s-empty-summary",
|
|
481
|
+
requestHeader: () => ({ config: { provider: "deepseek", model: "deepseek-chat" } }),
|
|
482
|
+
events: [userMessage("这一轮没有可保存的长期记忆", 1), { seq: 2, type: "turn/end" }]
|
|
483
|
+
};
|
|
484
|
+
|
|
485
|
+
await handler(session, { seq: 2, type: "turn/end" });
|
|
486
|
+
assert.equal(calls.length, 1);
|
|
487
|
+
assert.equal(store.count(), 0);
|
|
488
|
+
assert.equal(store.getDistillCursor(session.id).last_seq, 2);
|
|
489
|
+
|
|
490
|
+
await handler(session, { seq: 2, type: "turn/end" });
|
|
491
|
+
assert.equal(calls.length, 1, "a valid empty summary must consume the window once");
|
|
492
|
+
});
|
|
493
|
+
|
|
494
|
+
test("advances the cursor to the window end for mixed valid and invalid entries", async () => {
|
|
495
|
+
const valid = { type: "history", title: "有效摘要", content: "只保存这一条", importance: 3 };
|
|
496
|
+
const { events, store, calls } = setup(
|
|
497
|
+
{ distillRateLimitIntervalMs: 0 },
|
|
498
|
+
{ stream: streamOf([valid, { type: "unknown", title: "无效摘要", content: "忽略" }, null]) }
|
|
499
|
+
);
|
|
500
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
501
|
+
const session = {
|
|
502
|
+
id: "s-mixed-summary",
|
|
503
|
+
requestHeader: () => ({ config: { provider: "deepseek", model: "deepseek-chat" } }),
|
|
504
|
+
events: [userMessage("混合摘要窗口", 1), { seq: 2, type: "assistant/message" }, { seq: 3, type: "turn/end" }]
|
|
505
|
+
};
|
|
506
|
+
|
|
507
|
+
await handler(session, { seq: 3, type: "turn/end" });
|
|
508
|
+
assert.equal(calls.length, 1);
|
|
509
|
+
assert.equal(store.count(), 1);
|
|
510
|
+
assert.equal(store.all()[0].title, valid.title);
|
|
511
|
+
assert.equal(store.getDistillCursor(session.id).last_seq, 3);
|
|
512
|
+
|
|
513
|
+
await handler(session, { seq: 3, type: "turn/end" });
|
|
514
|
+
assert.equal(calls.length, 1, "a mixed summary must consume the window once");
|
|
515
|
+
});
|
|
516
|
+
|
|
517
|
+
test("distills only new seq events and keeps the recent tail when a window exceeds distillMaxChars", async () => {
|
|
518
|
+
const { events, calls } = setup(
|
|
519
|
+
{ distillMaxChars: 80, distillRateLimitIntervalMs: 0 },
|
|
520
|
+
{ stream: streamOf([]) }
|
|
521
|
+
);
|
|
522
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
523
|
+
const transcript = [
|
|
524
|
+
userMessage(`OLD_BEGINNING ${"x".repeat(160)} OLD_RECENT`, 1),
|
|
525
|
+
{ seq: 2, type: "assistant/message", data: { message: { content: [{ type: "text", text: "旧回复" }] } } },
|
|
526
|
+
{ seq: 3, type: "turn/end" }
|
|
527
|
+
];
|
|
528
|
+
const session = {
|
|
529
|
+
id: "s-incremental",
|
|
530
|
+
requestHeader: () => ({ config: { provider: "deepseek", model: "deepseek-chat" } }),
|
|
531
|
+
snapshotEvents: () => transcript
|
|
532
|
+
};
|
|
533
|
+
|
|
534
|
+
await handler(session, { seq: 3, type: "turn/end" });
|
|
535
|
+
const firstInput = calls[0].messages.find((message) => message.role === "user").content[0].text;
|
|
536
|
+
|
|
537
|
+
transcript.push(
|
|
538
|
+
userMessage(`NEW_BEGINNING ${"y".repeat(160)} NEW_RECENT`, 4),
|
|
539
|
+
{ seq: 5, type: "assistant/message", data: { message: { content: [{ type: "text", text: "新回复" }] } } },
|
|
540
|
+
{ seq: 6, type: "turn/end" }
|
|
541
|
+
);
|
|
542
|
+
await handler(session, { seq: 6, type: "turn/end" });
|
|
543
|
+
const secondInput = calls[1].messages.find((message) => message.role === "user").content[0].text;
|
|
544
|
+
|
|
545
|
+
assert.notEqual(firstInput, secondInput, "successive distills must receive different windows");
|
|
546
|
+
assert.ok(secondInput.includes("NEW_RECENT"), "the newest event content remains in the bounded window");
|
|
547
|
+
assert.ok(!secondInput.includes("OLD_BEGINNING"), "the second window must not restart at the session beginning");
|
|
548
|
+
assert.ok(!secondInput.includes("OLD_RECENT"), "the previous successful window must not be repeated");
|
|
549
|
+
});
|
|
550
|
+
|
|
551
|
+
test("dispose unsubscribes and stops later turn/end events from summarizing", async () => {
|
|
552
|
+
const { events, summarizer, calls } = setup();
|
|
553
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
554
|
+
summarizer.dispose();
|
|
555
|
+
// The ctx.on() disposer must have removed the listener.
|
|
556
|
+
assert.ok(!events.some((e) => e.name === "session/event"));
|
|
557
|
+
const session = {
|
|
558
|
+
id: "s1",
|
|
559
|
+
requestHeader: () => ({ config: { provider: "deepseek", model: "deepseek-chat" } }),
|
|
560
|
+
events: [userMessage("你好"), { seq: 2, type: "turn/end" }]
|
|
561
|
+
};
|
|
562
|
+
// Even a stale handler reference must not start a new LLM call.
|
|
563
|
+
await handler(session, { seq: 2, type: "turn/end" });
|
|
564
|
+
assert.equal(calls.length, 0);
|
|
565
|
+
});
|
|
566
|
+
|
|
567
|
+
test("excludes plugin-injected user/message events from summarization input", async () => {
|
|
568
|
+
const { events, store, calls } = setup();
|
|
569
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
570
|
+
const session = {
|
|
571
|
+
id: "s2",
|
|
572
|
+
requestHeader: () => ({ config: { provider: "deepseek", model: "deepseek-chat" } }),
|
|
573
|
+
events: [
|
|
574
|
+
{
|
|
575
|
+
seq: 1,
|
|
576
|
+
type: "user/message",
|
|
577
|
+
data: { source: { kind: "plugin" }, content: [{ type: "text", text: "AGENTS.md 内容" }] }
|
|
578
|
+
},
|
|
579
|
+
userMessage("帮我看看这个报错"),
|
|
580
|
+
{ seq: 3, type: "turn/end" }
|
|
581
|
+
]
|
|
582
|
+
};
|
|
583
|
+
await handler(session, { seq: 3, type: "turn/end" });
|
|
584
|
+
assert.equal(calls.length, 1);
|
|
585
|
+
const userMessages = calls[0].messages.filter((m) => m.role === "user");
|
|
586
|
+
assert.equal(userMessages.length, 1);
|
|
587
|
+
assert.ok(!JSON.stringify(calls[0].messages).includes("AGENTS.md"));
|
|
588
|
+
assert.equal(store.count(), 2);
|
|
589
|
+
});
|
|
590
|
+
|
|
591
|
+
test("aborted finish does not store entries and leaves the seq window retryable", async () => {
|
|
592
|
+
let attempt = 0;
|
|
593
|
+
const { events, store, calls } = setup({ distillRateLimitIntervalMs: 0 }, {
|
|
594
|
+
stream() {
|
|
595
|
+
attempt++;
|
|
596
|
+
return (async function* () {
|
|
597
|
+
yield { type: "block-start", block: { type: "text" } };
|
|
598
|
+
if (attempt === 1) {
|
|
599
|
+
yield { type: "text-delta", delta: "[]" };
|
|
600
|
+
yield { type: "finish", kind: "aborted" };
|
|
601
|
+
return;
|
|
602
|
+
}
|
|
603
|
+
yield { type: "text-delta", delta: JSON.stringify([{ type: "history", title: "重试", content: "保留原窗口", importance: 3 }]) };
|
|
604
|
+
yield { type: "finish", kind: "ok" };
|
|
605
|
+
})();
|
|
606
|
+
}
|
|
607
|
+
});
|
|
608
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
609
|
+
const session = {
|
|
610
|
+
id: "s3",
|
|
611
|
+
requestHeader: () => ({ config: { provider: "deepseek", model: "deepseek-chat" } }),
|
|
612
|
+
events: [userMessage("继续", 1), { seq: 2, type: "turn/end" }]
|
|
613
|
+
};
|
|
614
|
+
await handler(session, { seq: 2, type: "turn/end" });
|
|
615
|
+
assert.equal(calls.length, 1); // the stream was actually reached
|
|
616
|
+
assert.equal(store.count(), 0);
|
|
617
|
+
await handler(session, { seq: 2, type: "turn/end" });
|
|
618
|
+
assert.equal(calls.length, 2, "an aborted window must be retried instead of being marked consumed");
|
|
619
|
+
assert.equal(store.count(), 1);
|
|
620
|
+
});
|
|
621
|
+
|
|
622
|
+
test("a stream failure leaves the seq window retryable", async () => {
|
|
623
|
+
let attempt = 0;
|
|
624
|
+
const { events, store, calls } = setup({ distillRateLimitIntervalMs: 0 }, {
|
|
625
|
+
stream() {
|
|
626
|
+
attempt++;
|
|
627
|
+
if (attempt === 1) {
|
|
628
|
+
return (async function* () {
|
|
629
|
+
throw new Error("temporary stream failure");
|
|
630
|
+
})();
|
|
631
|
+
}
|
|
632
|
+
return streamOf([{ type: "history", title: "重试成功", content: "失败窗口未丢失", importance: 3 }])();
|
|
633
|
+
}
|
|
634
|
+
});
|
|
635
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
636
|
+
const session = {
|
|
637
|
+
id: "s-stream-failure",
|
|
638
|
+
requestHeader: () => ({ config: { provider: "deepseek", model: "deepseek-chat" } }),
|
|
639
|
+
events: [userMessage("保留失败窗口", 1), { seq: 2, type: "turn/end" }]
|
|
640
|
+
};
|
|
641
|
+
|
|
642
|
+
await handler(session, { seq: 2, type: "turn/end" });
|
|
643
|
+
assert.equal(calls.length, 1);
|
|
644
|
+
assert.equal(store.count(), 0);
|
|
645
|
+
await handler(session, { seq: 2, type: "turn/end" });
|
|
646
|
+
assert.equal(calls.length, 2, "a failed stream must be retried even without new events");
|
|
647
|
+
assert.equal(store.count(), 1);
|
|
648
|
+
});
|
|
649
|
+
|
|
650
|
+
test("invalid summary JSON leaves the seq window retryable", async () => {
|
|
651
|
+
let attempt = 0;
|
|
652
|
+
const { events, store, calls } = setup({ distillRateLimitIntervalMs: 0 }, {
|
|
653
|
+
stream() {
|
|
654
|
+
attempt++;
|
|
655
|
+
const output = attempt === 1 ? "not a JSON array" : "[]";
|
|
656
|
+
return (async function* () {
|
|
657
|
+
yield { type: "block-start", block: { type: "text" } };
|
|
658
|
+
yield { type: "text-delta", delta: output };
|
|
659
|
+
yield { type: "finish", kind: "ok" };
|
|
660
|
+
})();
|
|
661
|
+
}
|
|
662
|
+
});
|
|
663
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
664
|
+
const session = {
|
|
665
|
+
id: "s-parse-failure",
|
|
666
|
+
requestHeader: () => ({ config: { provider: "deepseek", model: "deepseek-chat" } }),
|
|
667
|
+
events: [userMessage("保留解析窗口", 1), { seq: 2, type: "turn/end" }]
|
|
668
|
+
};
|
|
669
|
+
|
|
670
|
+
await handler(session, { seq: 2, type: "turn/end" });
|
|
671
|
+
await handler(session, { seq: 2, type: "turn/end" });
|
|
672
|
+
assert.equal(calls.length, 2, "a parse failure must not advance the seq cursor");
|
|
673
|
+
assert.equal(store.count(), 0, "the valid empty retry remains a no-op");
|
|
674
|
+
});
|
|
675
|
+
|
|
676
|
+
test("an all-invalid summary leaves the same window retryable until valid memories are saved", async (t) => {
|
|
677
|
+
for (const invalid of [
|
|
678
|
+
[{}],
|
|
679
|
+
[{ type: "unknown", title: "无效类型", content: "不能消费窗口" }],
|
|
680
|
+
[{ type: "history", title: " ", content: "不能消费窗口" }, null, "garbage"]
|
|
681
|
+
]) {
|
|
682
|
+
await t.test(JSON.stringify(invalid), async (t) => {
|
|
683
|
+
let attempt = 0;
|
|
684
|
+
const valid = { type: "history", title: "重试成功", content: "全无效摘要后仍保留原窗口", importance: 3 };
|
|
685
|
+
const { events, service, store, calls, summarizer } = setup({ distillRateLimitIntervalMs: 0 }, {
|
|
686
|
+
stream() {
|
|
687
|
+
return streamOf(attempt++ === 0 ? invalid : [valid])();
|
|
688
|
+
}
|
|
689
|
+
});
|
|
690
|
+
t.after(() => { summarizer.dispose(); store.close(); });
|
|
691
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
692
|
+
const session = {
|
|
693
|
+
id: "s-all-invalid",
|
|
694
|
+
requestHeader: () => ({ config: { provider: "deepseek", model: "deepseek-chat" } }),
|
|
695
|
+
events: [userMessage("保留原窗口", 1), { seq: 2, type: "turn/end" }]
|
|
696
|
+
};
|
|
697
|
+
|
|
698
|
+
await handler(session, { seq: 2, type: "turn/end" });
|
|
699
|
+
assert.equal(service.count(), 0);
|
|
700
|
+
await handler(session, { seq: 2, type: "turn/end" });
|
|
701
|
+
assert.equal(calls.length, 2, "all-invalid entries must not consume the window");
|
|
702
|
+
const firstTranscript = calls[0].messages.find((message) => message.role === "user").content[0].text;
|
|
703
|
+
const retryTranscript = calls[1].messages.find((message) => message.role === "user").content[0].text;
|
|
704
|
+
assert.equal(retryTranscript, firstTranscript, "retry receives the original transcript");
|
|
705
|
+
assert.equal(service.count(), 1);
|
|
706
|
+
assert.equal(service.list()[0].content, valid.content);
|
|
707
|
+
assert.ok(service.listLlmAudits({ source: "autoSummarize" }).some(
|
|
708
|
+
(audit) => audit.status === "error" && audit.error_message === "invalid summary JSON"
|
|
709
|
+
), "an all-invalid summary must be audited as an error");
|
|
710
|
+
|
|
711
|
+
await handler(session, { seq: 2, type: "turn/end" });
|
|
712
|
+
assert.equal(calls.length, 2, "the successfully retried window is consumed once");
|
|
713
|
+
assert.equal(service.count(), 1);
|
|
714
|
+
});
|
|
715
|
+
}
|
|
716
|
+
});
|
|
717
|
+
|
|
718
|
+
test("rolls back partial memory writes so retrying a failed window does not duplicate entries", async () => {
|
|
719
|
+
const { events, store, service, calls } = setup(
|
|
720
|
+
{ distillRateLimitIntervalMs: 0 },
|
|
721
|
+
{
|
|
722
|
+
stream: streamOf([
|
|
723
|
+
{ type: "history", title: "第一条", content: "第一条内容", importance: 3 },
|
|
724
|
+
{ type: "decision", title: "第二条", content: "第二条内容", importance: 4 }
|
|
725
|
+
])
|
|
726
|
+
}
|
|
727
|
+
);
|
|
728
|
+
const originalSave = service.saveWithDedupe.bind(service);
|
|
729
|
+
let failOnSecondEntry = true;
|
|
730
|
+
service.saveWithDedupe = (memory) => {
|
|
731
|
+
const result = originalSave(memory);
|
|
732
|
+
if (failOnSecondEntry && memory.title === "第二条") {
|
|
733
|
+
failOnSecondEntry = false;
|
|
734
|
+
throw new Error("simulated memory write failure");
|
|
735
|
+
}
|
|
736
|
+
return result;
|
|
737
|
+
};
|
|
738
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
739
|
+
const session = {
|
|
740
|
+
id: "s-partial-write",
|
|
741
|
+
requestHeader: () => ({ config: { provider: "deepseek", model: "deepseek-chat" } }),
|
|
742
|
+
events: [userMessage("不要丢失这个窗口", 1), { seq: 2, type: "turn/end" }]
|
|
743
|
+
};
|
|
744
|
+
|
|
745
|
+
await handler(session, { seq: 2, type: "turn/end" });
|
|
746
|
+
assert.equal(calls.length, 1);
|
|
747
|
+
assert.equal(store.count(), 0, "a failed write transaction must leave no partial memory");
|
|
748
|
+
assert.equal(store.getDistillCursor("s-partial-write"), undefined, "a failed write must not advance the cursor");
|
|
749
|
+
|
|
750
|
+
await handler(session, { seq: 2, type: "turn/end" });
|
|
751
|
+
assert.equal(calls.length, 2, "the failed seq window must be retried");
|
|
752
|
+
assert.equal(store.count(), 2);
|
|
753
|
+
assert.equal(store.getDistillCursor("s-partial-write").last_seq, 2);
|
|
754
|
+
assert.equal(store.all().find((memory) => memory.title === "第一条")?.content, "第一条内容");
|
|
755
|
+
|
|
756
|
+
await handler(session, { seq: 2, type: "turn/end" });
|
|
757
|
+
assert.equal(calls.length, 2, "a successfully retried window must not be distilled again");
|
|
758
|
+
assert.equal(store.count(), 2);
|
|
759
|
+
});
|
|
760
|
+
|
|
761
|
+
test("rolls back memory writes when persisting the cursor fails", async () => {
|
|
762
|
+
const { events, store, service, calls } = setup(
|
|
763
|
+
{ distillRateLimitIntervalMs: 0 },
|
|
764
|
+
{ stream: streamOf([{ type: "history", title: "游标失败", content: "事务应回滚", importance: 3 }]) }
|
|
765
|
+
);
|
|
766
|
+
const originalSetCursor = service.setDistillCursor.bind(service);
|
|
767
|
+
let failOnce = true;
|
|
768
|
+
service.setDistillCursor = (sessionId, lastSeq) => {
|
|
769
|
+
if (failOnce) {
|
|
770
|
+
failOnce = false;
|
|
771
|
+
throw new Error("simulated cursor write failure");
|
|
772
|
+
}
|
|
773
|
+
return originalSetCursor(sessionId, lastSeq);
|
|
774
|
+
};
|
|
775
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
776
|
+
const session = {
|
|
777
|
+
id: "s-cursor-write-failure",
|
|
778
|
+
requestHeader: () => ({ config: { provider: "deepseek", model: "deepseek-chat" } }),
|
|
779
|
+
events: [userMessage("游标写入失败也不能丢窗口", 1), { seq: 2, type: "turn/end" }]
|
|
780
|
+
};
|
|
781
|
+
|
|
782
|
+
await handler(session, { seq: 2, type: "turn/end" });
|
|
783
|
+
assert.equal(calls.length, 1);
|
|
784
|
+
assert.equal(store.count(), 0, "cursor failure must roll back the memory write");
|
|
785
|
+
assert.equal(store.getDistillCursor(session.id), undefined);
|
|
786
|
+
const failedAudit = service.listLlmAudits({ source: "autoSummarize" }).find(
|
|
787
|
+
(audit) => audit.error_message === "simulated cursor write failure"
|
|
788
|
+
);
|
|
789
|
+
assert.equal(failedAudit?.status, "error", "cursor failure must not be audited as success");
|
|
790
|
+
|
|
791
|
+
await handler(session, { seq: 2, type: "turn/end" });
|
|
792
|
+
assert.equal(calls.length, 2, "the failed cursor window must be retryable");
|
|
793
|
+
assert.equal(store.count(), 1);
|
|
794
|
+
assert.equal(store.getDistillCursor(session.id).last_seq, 2);
|
|
795
|
+
});
|
|
796
|
+
|
|
797
|
+
test("falls back to the in-memory cursor when service lacks setDistillCursor", async () => {
|
|
798
|
+
// 旧版 service(#274 之前)没有持久化游标:缺方法必须降级为内存游标照常蒸馏,
|
|
799
|
+
// 而不是抛错打断每轮蒸馏。方法存在但抛错走的是上一条回滚用例,两者不可混淆。
|
|
800
|
+
const { events, store, service, calls } = setup(
|
|
801
|
+
{ distillRateLimitIntervalMs: 0 },
|
|
802
|
+
{ stream: streamOf([{ type: "history", title: "降级游标", content: "缺方法也能蒸馏", importance: 3 }]) }
|
|
803
|
+
);
|
|
804
|
+
delete service.setDistillCursor; // 模拟旧版 service:实例属性,直接删掉
|
|
805
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
806
|
+
const session = {
|
|
807
|
+
id: "s-no-cursor-api",
|
|
808
|
+
requestHeader: () => ({ config: { provider: "deepseek", model: "deepseek-chat" } }),
|
|
809
|
+
events: [userMessage("旧宿主没有游标接口", 1), { seq: 2, type: "turn/end" }]
|
|
810
|
+
};
|
|
811
|
+
|
|
812
|
+
await handler(session, { seq: 2, type: "turn/end" });
|
|
813
|
+
assert.equal(calls.length, 1, "distillation must still run without a persisted cursor");
|
|
814
|
+
assert.equal(store.count(), 1, "the memory itself must be saved");
|
|
815
|
+
assert.equal(store.getDistillCursor(session.id), undefined, "nothing may be persisted without the API");
|
|
816
|
+
|
|
817
|
+
await handler(session, { seq: 2, type: "turn/end" });
|
|
818
|
+
assert.equal(calls.length, 1, "the in-memory cursor must prevent re-distillation in this process");
|
|
819
|
+
});
|
|
820
|
+
|
|
821
|
+
test("uses summarizeProvider/summarizeModel config override when set", async () => {
|
|
822
|
+
const { events, calls } = setup({
|
|
823
|
+
summarizeProvider: "aliyun",
|
|
824
|
+
summarizeModel: "qwen3.6-plus"
|
|
825
|
+
});
|
|
826
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
827
|
+
const session = {
|
|
828
|
+
id: "s4",
|
|
829
|
+
// Session header has a different model — config override should win.
|
|
830
|
+
requestHeader: () => ({ config: { provider: "deepseek", model: "deepseek-v4-pro" } }),
|
|
831
|
+
events: [userMessage("测试覆盖"), { seq: 2, type: "turn/end" }]
|
|
832
|
+
};
|
|
833
|
+
await handler(session, { seq: 2, type: "turn/end" });
|
|
834
|
+
assert.equal(calls.length, 1);
|
|
835
|
+
assert.equal(calls[0].provider, "aliyun");
|
|
836
|
+
assert.equal(calls[0].model, "qwen3.6-plus");
|
|
837
|
+
});
|
|
838
|
+
|
|
839
|
+
test("falls back to session header when summarize config is empty", async () => {
|
|
840
|
+
const { events, calls } = setup({
|
|
841
|
+
summarizeProvider: "",
|
|
842
|
+
summarizeModel: ""
|
|
843
|
+
});
|
|
844
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
845
|
+
const session = {
|
|
846
|
+
id: "s5",
|
|
847
|
+
requestHeader: () => ({ config: { provider: "deepseek", model: "deepseek-chat" } }),
|
|
848
|
+
events: [userMessage("回退测试"), { seq: 2, type: "turn/end" }]
|
|
849
|
+
};
|
|
850
|
+
await handler(session, { seq: 2, type: "turn/end" });
|
|
851
|
+
assert.equal(calls.length, 1);
|
|
852
|
+
assert.equal(calls[0].provider, "deepseek");
|
|
853
|
+
assert.equal(calls[0].model, "deepseek-chat");
|
|
854
|
+
});
|
|
855
|
+
|
|
856
|
+
// ── v0.7.11:智能调速器(429 保护)+ 完整转录/原子记忆 ─────────────────────
|
|
857
|
+
|
|
858
|
+
test("serializes distill LLM calls across sessions (global queue, no concurrency)", async () => {
|
|
859
|
+
const timeline = [];
|
|
860
|
+
const { events, store } = setup({ distillRateLimitIntervalMs: 0 }, {
|
|
861
|
+
stream() {
|
|
862
|
+
return (async function* () {
|
|
863
|
+
timeline.push(`start:${Date.now()}`);
|
|
864
|
+
await new Promise((r) => setTimeout(r, 8));
|
|
865
|
+
yield { type: "block-start", block: { type: "text" } };
|
|
866
|
+
yield { type: "text-delta", delta: "[]" };
|
|
867
|
+
yield { type: "finish", kind: "ok" };
|
|
868
|
+
timeline.push(`end:${Date.now()}`);
|
|
869
|
+
})();
|
|
870
|
+
}
|
|
871
|
+
});
|
|
872
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
873
|
+
const mkSession = (id) => ({
|
|
874
|
+
id,
|
|
875
|
+
requestHeader: () => ({ config: { provider: "deepseek", model: "deepseek-chat" } }),
|
|
876
|
+
events: [userMessage(`问题${id}`), { seq: 2, type: "turn/end" }]
|
|
877
|
+
});
|
|
878
|
+
// 两个会话几乎同时 turn/end → 必须排队,第二个请求不能与第一个并发。
|
|
879
|
+
await Promise.all([
|
|
880
|
+
handler(mkSession("a"), { seq: 2, type: "turn/end" }),
|
|
881
|
+
handler(mkSession("b"), { seq: 2, type: "turn/end" })
|
|
882
|
+
]);
|
|
883
|
+
assert.equal(timeline.length, 4); // 每个流 start + end
|
|
884
|
+
const starts = timeline.filter((t) => t.startsWith("start")).map((t) => Number(t.slice(6)));
|
|
885
|
+
const ends = timeline.filter((t) => t.startsWith("end")).map((t) => Number(t.slice(4)));
|
|
886
|
+
assert.ok(starts[1] >= ends[0], "second distill must start only after the first finished (serial queue)");
|
|
887
|
+
});
|
|
888
|
+
|
|
889
|
+
test("retries with exponential backoff on 429 and still stores entries", async () => {
|
|
890
|
+
let attempts = 0;
|
|
891
|
+
const { events, store } = setup(
|
|
892
|
+
{ distillRateLimitRetries: 3, distillRateLimitBaseDelayMs: 5, distillRateLimitIntervalMs: 0 },
|
|
893
|
+
{
|
|
894
|
+
stream() {
|
|
895
|
+
return (async function* () {
|
|
896
|
+
attempts++;
|
|
897
|
+
if (attempts < 3) throw Object.assign(new Error("rate limit exceeded"), { status: 429 });
|
|
898
|
+
yield { type: "block-start", block: { type: "text" } };
|
|
899
|
+
yield { type: "text-delta", delta: JSON.stringify([{ type: "history", title: "重试成功", content: "第三次请求成功", importance: 3 }]) };
|
|
900
|
+
yield { type: "finish", kind: "ok" };
|
|
901
|
+
})();
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
);
|
|
905
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
906
|
+
const session = {
|
|
907
|
+
id: "s9",
|
|
908
|
+
requestHeader: () => ({ config: { provider: "deepseek", model: "deepseek-chat" } }),
|
|
909
|
+
events: [userMessage("限流测试"), { seq: 2, type: "turn/end" }]
|
|
910
|
+
};
|
|
911
|
+
const startedAt = Date.now();
|
|
912
|
+
await handler(session, { seq: 2, type: "turn/end" });
|
|
913
|
+
// 429 两次 → 退避重试(5ms + 10ms),第三次成功入库。
|
|
914
|
+
assert.equal(attempts, 3);
|
|
915
|
+
assert.ok(Date.now() - startedAt >= 15, "backoff waits should be visible");
|
|
916
|
+
assert.equal(store.count(), 1);
|
|
917
|
+
assert.equal(store.all()[0].title, "重试成功");
|
|
918
|
+
});
|
|
919
|
+
|
|
920
|
+
test("distills full transcript (tool calls, results, code output) with atomic-memory prompt", async () => {
|
|
921
|
+
const { events, calls } = setup();
|
|
922
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
923
|
+
const session = {
|
|
924
|
+
id: "s10",
|
|
925
|
+
requestHeader: () => ({ config: { provider: "deepseek", model: "deepseek-chat" } }),
|
|
926
|
+
events: [
|
|
927
|
+
userMessage("帮我修这个 bug"),
|
|
928
|
+
{ seq: 2, type: "tool/call", data: { name: "Bash", arguments: "node test.js" } },
|
|
929
|
+
toolResultEvent("TypeError: x is not a function", true, 3),
|
|
930
|
+
codeDispatchEvent("fixed", false, 4),
|
|
931
|
+
{ seq: 5, type: "turn/end" }
|
|
932
|
+
]
|
|
933
|
+
};
|
|
934
|
+
await handler(session, { seq: 5, type: "turn/end" });
|
|
935
|
+
const transcript = JSON.stringify(calls[0].messages);
|
|
936
|
+
assert.ok(transcript.includes("修这个 bug"));
|
|
937
|
+
assert.ok(transcript.includes("TypeError: x is not a function"));
|
|
938
|
+
assert.ok(transcript.includes("代码执行"));
|
|
939
|
+
// 原子记忆 prompt:不再"硬压 2-3 条",而是按需多提、贴近原始细节。
|
|
940
|
+
assert.ok(calls[0].messages[0].content[0].text.includes("原子记忆"));
|
|
941
|
+
});
|
|
942
|
+
|
|
943
|
+
test("collects real tool result and code dispatch payloads with failure status", async () => {
|
|
944
|
+
const { events, calls } = setup({ distillRateLimitIntervalMs: 0 });
|
|
945
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
946
|
+
const session = {
|
|
947
|
+
id: "s-real-payloads",
|
|
948
|
+
requestHeader: () => ({ config: { provider: "deepseek", model: "deepseek-chat" } }),
|
|
949
|
+
events: [
|
|
950
|
+
userMessage("检查真实事件形状", 1),
|
|
951
|
+
toolResultEvent("TypeError: tool failed", true, 2),
|
|
952
|
+
codeDispatchEvent("代码执行结果", false, 3),
|
|
953
|
+
{ seq: 4, type: "turn/end" }
|
|
954
|
+
]
|
|
955
|
+
};
|
|
956
|
+
|
|
957
|
+
await handler(session, { seq: 4, type: "turn/end" });
|
|
958
|
+
const transcript = calls[0].messages.find((message) => message.role === "user").content[0].text;
|
|
959
|
+
assert.match(transcript, /工具结果(失败):TypeError: tool failed/);
|
|
960
|
+
assert.match(transcript, /代码执行(成功):代码执行结果/);
|
|
961
|
+
});
|
|
962
|
+
|
|
963
|
+
test("collects subagent delivery once across inbox and user-message views", async () => {
|
|
964
|
+
const { events, calls } = setup({ distillRateLimitIntervalMs: 0 });
|
|
965
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
966
|
+
const delivery = inboxMessage("delivery-1", "subagent-settled", "子会话 closing message");
|
|
967
|
+
const agentMessage = inboxMessage("delivery-2", "agent-message", "子代理补充报告");
|
|
968
|
+
const injectedInstruction = inboxMessage("instruction-1", "agent-instructions", "不要把这条注入指令沉淀为记忆");
|
|
969
|
+
const session = {
|
|
970
|
+
id: "s-agent-delivery",
|
|
971
|
+
requestHeader: () => ({ config: { provider: "deepseek", model: "deepseek-chat" } }),
|
|
972
|
+
events: [
|
|
973
|
+
userMessage("检查子会话交付", 1),
|
|
974
|
+
{ seq: 2, type: "agent/inbox/spliced", data: { inserted: [delivery, agentMessage, injectedInstruction] } },
|
|
975
|
+
{ seq: 3, type: "user/message", data: delivery },
|
|
976
|
+
{ seq: 4, type: "turn/end" }
|
|
977
|
+
]
|
|
978
|
+
};
|
|
979
|
+
|
|
980
|
+
await handler(session, { seq: 4, type: "turn/end" });
|
|
981
|
+
const transcript = calls[0].messages.find((message) => message.role === "user").content[0].text;
|
|
982
|
+
assert.equal(transcript.match(/子会话 closing message/g)?.length, 1);
|
|
983
|
+
assert.equal(transcript.match(/子代理补充报告/g)?.length, 1);
|
|
984
|
+
assert.ok(!transcript.includes("不要把这条注入指令沉淀为记忆"));
|
|
985
|
+
});
|
|
986
|
+
|
|
987
|
+
test("keeps prefix trimming for tool arguments and tool results", async () => {
|
|
988
|
+
const { events, calls } = setup({ distillRateLimitIntervalMs: 0 });
|
|
989
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
990
|
+
const session = {
|
|
991
|
+
id: "s-trim",
|
|
992
|
+
requestHeader: () => ({ config: { provider: "deepseek", model: "deepseek-chat" } }),
|
|
993
|
+
events: [
|
|
994
|
+
userMessage("检查截断", 1),
|
|
995
|
+
{ seq: 2, type: "tool/call", data: { name: "Bash", arguments: `ARG_START ${"a".repeat(350)} ARG_END` } },
|
|
996
|
+
toolResultEvent(`OUT_START ${"b".repeat(550)} OUT_END`, false, 3),
|
|
997
|
+
{ seq: 4, type: "turn/end" }
|
|
998
|
+
]
|
|
999
|
+
};
|
|
1000
|
+
await handler(session, { seq: 4, type: "turn/end" });
|
|
1001
|
+
const transcript = JSON.stringify(calls[0].messages);
|
|
1002
|
+
assert.ok(transcript.includes("ARG_START"));
|
|
1003
|
+
assert.ok(!transcript.includes("ARG_END"), "tool arguments keep their existing prefix trim");
|
|
1004
|
+
assert.ok(transcript.includes("OUT_START"));
|
|
1005
|
+
assert.ok(!transcript.includes("OUT_END"), "tool results keep their existing prefix trim");
|
|
1006
|
+
});
|
|
1007
|
+
|
|
1008
|
+
test("distill excludes private assistant reasoning blocks from the transcript", async () => {
|
|
1009
|
+
const { events, calls } = setup();
|
|
1010
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
1011
|
+
const session = {
|
|
1012
|
+
id: "s12",
|
|
1013
|
+
requestHeader: () => ({ config: { provider: "deepseek", model: "deepseek-chat" } }),
|
|
1014
|
+
events: [
|
|
1015
|
+
userMessage("解释一下这段代码"),
|
|
1016
|
+
{
|
|
1017
|
+
type: "assistant/message",
|
|
1018
|
+
data: {
|
|
1019
|
+
message: {
|
|
1020
|
+
content: [
|
|
1021
|
+
{ type: "reasoning", text: "私有推理:内部权衡过程不该进记忆" },
|
|
1022
|
+
{ type: "text", text: "这段代码有死循环,第 3 行 while 条件恒真。" }
|
|
1023
|
+
]
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
},
|
|
1027
|
+
{ seq: 3, type: "turn/end" }
|
|
1028
|
+
]
|
|
1029
|
+
};
|
|
1030
|
+
await handler(session, { seq: 3, type: "turn/end" });
|
|
1031
|
+
const transcript = JSON.stringify(calls[0].messages);
|
|
1032
|
+
assert.ok(transcript.includes("有死循环,第 3 行"), "public assistant text still distills");
|
|
1033
|
+
assert.ok(!transcript.includes("私有推理"), "private reasoning must never enter the distill context");
|
|
1034
|
+
});
|
|
1035
|
+
|
|
1036
|
+
test("codingRetrospect stores coding memory types in the coding memory type set", async () => {
|
|
1037
|
+
const { events, store, calls } = setup({ codingRetrospect: true }, {
|
|
1038
|
+
stream() {
|
|
1039
|
+
return (async function* () {
|
|
1040
|
+
yield { type: "block-start", block: { type: "text" } };
|
|
1041
|
+
yield { type: "text-delta", delta: JSON.stringify([
|
|
1042
|
+
{ type: "rejected_solution", title: "弃用方案", content: "A 方案被否决,改用 B", importance: 4 }
|
|
1043
|
+
]) };
|
|
1044
|
+
yield { type: "finish", kind: "ok" };
|
|
1045
|
+
})();
|
|
1046
|
+
}
|
|
1047
|
+
});
|
|
1048
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
1049
|
+
const session = {
|
|
1050
|
+
id: "s11",
|
|
1051
|
+
requestHeader: () => ({ config: { provider: "deepseek", model: "deepseek-chat" } }),
|
|
1052
|
+
events: [userMessage("编码任务"), { seq: 2, type: "turn/end" }]
|
|
1053
|
+
};
|
|
1054
|
+
await handler(session, { seq: 2, type: "turn/end" });
|
|
1055
|
+
assert.equal(store.count(), 1);
|
|
1056
|
+
const m = store.all()[0];
|
|
1057
|
+
assert.equal(m.type, "rejected_solution");
|
|
1058
|
+
// 读取侧门控靠 m.type(rejected_solution/pitfall/constraint ∈ INJECT_TYPES),
|
|
1059
|
+
// sanitizeTags 不认 `type:` 前缀,所以不给编码记忆打 tag(会清空 tags 列)。
|
|
1060
|
+
assert.deepEqual(m.tags, []);
|
|
1061
|
+
// 编码模式用编码 prompt(含 rejected_solution 类型说明)。
|
|
1062
|
+
assert.ok(calls[0].messages[0].content[0].text.includes("rejected_solution"));
|
|
1063
|
+
});
|
|
1064
|
+
|
|
1065
|
+
// ── Issue #127:节流 / 条数上限 / 同会话去重 ────────────────────────────────
|
|
1066
|
+
|
|
1067
|
+
/** A distill stream that always returns exactly these entries. */
|
|
1068
|
+
function streamOf(entries) {
|
|
1069
|
+
return () => (async function* () {
|
|
1070
|
+
yield { type: "block-start", block: { type: "text" } };
|
|
1071
|
+
yield { type: "text-delta", delta: JSON.stringify(entries) };
|
|
1072
|
+
yield { type: "finish", kind: "ok" };
|
|
1073
|
+
})();
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1076
|
+
function sessionFor(id) {
|
|
1077
|
+
return {
|
|
1078
|
+
id,
|
|
1079
|
+
requestHeader: () => ({ config: { provider: "deepseek", model: "deepseek-chat" } }),
|
|
1080
|
+
events: [userMessage("继续"), { seq: 2, type: "turn/end" }]
|
|
1081
|
+
};
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1084
|
+
test("issue#127: min-interval gate suppresses the second turn/end and audits it as skipped", async () => {
|
|
1085
|
+
const { events, store, calls } = setup({ summarizeMinIntervalMinutes: 30 });
|
|
1086
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
1087
|
+
const session = sessionFor("t1");
|
|
1088
|
+
await handler(session, { seq: 2, type: "turn/end" });
|
|
1089
|
+
assert.equal(calls.length, 1, "the first turn/end distills");
|
|
1090
|
+
assert.equal(store.count(), 2);
|
|
1091
|
+
await handler(session, { seq: 3, type: "turn/end" });
|
|
1092
|
+
assert.equal(calls.length, 1, "the second turn/end inside the window makes no LLM call");
|
|
1093
|
+
assert.equal(store.count(), 2, "nothing new is stored");
|
|
1094
|
+
// 可观测性:被节流不再静默——留一行 status='skipped',一条 SQL 可自查。
|
|
1095
|
+
const audits = store.listLlmAudits({ source: "autoSummarize" });
|
|
1096
|
+
assert.ok(
|
|
1097
|
+
audits.some((a) => a.status === "skipped" && a.error_message === "min-interval"),
|
|
1098
|
+
"the suppression is visible as a status='skipped' audit row"
|
|
1099
|
+
);
|
|
1100
|
+
});
|
|
1101
|
+
|
|
1102
|
+
test("issue#127: min-interval 0 still distills every new event window", async () => {
|
|
1103
|
+
const { events, calls } = setup({ summarizeMinIntervalMinutes: 0 });
|
|
1104
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
1105
|
+
const session = sessionFor("t2");
|
|
1106
|
+
await handler(session, { seq: 2, type: "turn/end" });
|
|
1107
|
+
session.events.push(userMessage("第二轮", 3), { seq: 4, type: "turn/end" });
|
|
1108
|
+
await handler(session, { seq: 4, type: "turn/end" });
|
|
1109
|
+
assert.equal(calls.length, 2, "0 = no gate (zero behavior change)");
|
|
1110
|
+
});
|
|
1111
|
+
|
|
1112
|
+
test("issue#127: the interval gate is per-session (one session never throttles another)", async () => {
|
|
1113
|
+
const { events, calls } = setup({ summarizeMinIntervalMinutes: 30 });
|
|
1114
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
1115
|
+
await handler(sessionFor("ta"), { seq: 2, type: "turn/end" });
|
|
1116
|
+
await handler(sessionFor("tb"), { seq: 2, type: "turn/end" });
|
|
1117
|
+
assert.equal(calls.length, 2, "a different session is not throttled by another's lastRunAt");
|
|
1118
|
+
});
|
|
1119
|
+
|
|
1120
|
+
test("issue#127: summarizeMaxEntriesPerRun caps the stored entries and audits parsed/capped", async () => {
|
|
1121
|
+
const { events, store } = setup({ summarizeMaxEntriesPerRun: 1 }, {
|
|
1122
|
+
stream: streamOf([
|
|
1123
|
+
{ type: "decision", title: "选型", content: "确定用 node:sqlite", importance: 4 },
|
|
1124
|
+
{ type: "preference", title: "语言", content: "用户喜欢中文交流", importance: 5 }
|
|
1125
|
+
])
|
|
1126
|
+
});
|
|
1127
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
1128
|
+
await handler(sessionFor("t3"), { seq: 2, type: "turn/end" });
|
|
1129
|
+
assert.equal(store.count(), 1, "only the first capped entry is stored");
|
|
1130
|
+
const audit = store.listLlmAudits({ source: "autoSummarize" })[0];
|
|
1131
|
+
assert.equal(audit.metadata?.parsed, 2, "the raw parsed count is kept for tuning the cap");
|
|
1132
|
+
assert.equal(audit.metadata?.capped, 1);
|
|
1133
|
+
});
|
|
1134
|
+
|
|
1135
|
+
test("issue#127: title dedupe absorbs a normalized-same title", async () => {
|
|
1136
|
+
const { events, store, service } = setup({ summarizeDedupeMode: "title" }, {
|
|
1137
|
+
stream: streamOf([{ type: "pitfall", title: "Win7 OpenSSH 失效", content: "第二次记录", importance: 4 }])
|
|
1138
|
+
});
|
|
1139
|
+
// 既有条目:同会话、标题归一化后全等(大小写与空白差异)
|
|
1140
|
+
const seeded = service.saveWithDedupe({
|
|
1141
|
+
type: "pitfall", title: "win7 openssh 失效", content: "第一次记录", source: "session:t5"
|
|
1142
|
+
}).memory;
|
|
1143
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
1144
|
+
await handler(sessionFor("t5"), { seq: 2, type: "turn/end" });
|
|
1145
|
+
assert.equal(store.count(), 1, "the normalized-same title merged instead of creating a row");
|
|
1146
|
+
assert.ok(store.getById(seeded.id).content.includes("第二次记录"), "content appended to the existing row");
|
|
1147
|
+
});
|
|
1148
|
+
|
|
1149
|
+
test("issue#127: title dedupe leaves a rephrase alone (it only catches an exact same title)", async () => {
|
|
1150
|
+
// 同一事实的另一种措辞 → title 档不命中,正常新建。这条界定了 title 档的边界,
|
|
1151
|
+
// 避免被误当成 vector 档的替代(cos≥0.85 的簇里「归一化标题全同」为 0 个)。
|
|
1152
|
+
const { events, store, service } = setup({ summarizeDedupeMode: "title" }, {
|
|
1153
|
+
stream: streamOf([{ type: "pitfall", title: "Win7 下 OpenSSH 官方脚本装不上", content: "新记录", importance: 4 }])
|
|
1154
|
+
});
|
|
1155
|
+
service.saveWithDedupe({
|
|
1156
|
+
type: "pitfall", title: "OpenSSH 官方脚本在 Win7 失效", content: "旧记录", source: "session:other"
|
|
1157
|
+
});
|
|
1158
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
1159
|
+
await handler(sessionFor("t7"), { seq: 2, type: "turn/end" });
|
|
1160
|
+
assert.equal(store.count(), 2, "a rephrase (different title) is not merged by the title tier");
|
|
1161
|
+
});
|
|
1162
|
+
|
|
1163
|
+
test("issue#127: vector dedupe merges a same-fact rephrase and keeps content_history", async () => {
|
|
1164
|
+
const embedder = {
|
|
1165
|
+
ready: true,
|
|
1166
|
+
embedSingle: async (text) => (String(text).includes("OpenSSH") ? [1, 0, 0] : [0, 1, 0])
|
|
1167
|
+
};
|
|
1168
|
+
const { events, store, service } = setup(
|
|
1169
|
+
{ summarizeDedupeMode: "vector", summarizeDedupeMinSim: 0.92 },
|
|
1170
|
+
{ stream: streamOf([{ type: "pitfall", title: "Win7 下 OpenSSH 官方脚本装不上", content: "重复踩坑", importance: 4 }]) }
|
|
1171
|
+
);
|
|
1172
|
+
service.setEmbedder(embedder);
|
|
1173
|
+
// 同一事实的另一种措辞,已有向量 → 余弦 1.0 ≥ 0.92
|
|
1174
|
+
const seeded = service.saveWithDedupe({
|
|
1175
|
+
type: "pitfall", title: "OpenSSH 官方脚本在 Win7 失效", content: "第一次记录", source: "session:t8"
|
|
1176
|
+
}).memory;
|
|
1177
|
+
store.setEmbedding(seeded.id, [1, 0, 0]);
|
|
1178
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
1179
|
+
await handler(sessionFor("t8"), { seq: 2, type: "turn/end" });
|
|
1180
|
+
assert.equal(store.count(), 1, "no new row: the rephrase was absorbed by cosine");
|
|
1181
|
+
const merged = store.getById(seeded.id);
|
|
1182
|
+
assert.ok(merged.content.includes("重复踩坑"), "content appended into the existing row");
|
|
1183
|
+
assert.ok((merged.content_history ?? []).length >= 1, "content_history keeps the prior version");
|
|
1184
|
+
const audit = store.listLlmAudits({ source: "autoSummarize" })[0];
|
|
1185
|
+
assert.equal(audit.metadata?.deduped, 1);
|
|
1186
|
+
assert.equal(audit.metadata?.mode, "vector");
|
|
1187
|
+
});
|
|
1188
|
+
|
|
1189
|
+
test("issue#127: no embedder means vector dedupe silently falls back to a normal write", async () => {
|
|
1190
|
+
const { events, store } = setup(
|
|
1191
|
+
{ summarizeDedupeMode: "vector" },
|
|
1192
|
+
{ stream: streamOf([{ type: "pitfall", title: "无关的一条", content: "内容", importance: 4 }]) }
|
|
1193
|
+
);
|
|
1194
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
1195
|
+
await handler(sessionFor("t9"), { seq: 2, type: "turn/end" });
|
|
1196
|
+
assert.equal(store.count(), 1, "dedupe failure never blocks the write");
|
|
1197
|
+
});
|
|
1198
|
+
|
|
1199
|
+
test("issue#127: the five summarize knobs are whitelisted and range-checked", () => {
|
|
1200
|
+
const store = createStore(":memory:");
|
|
1201
|
+
const settings = createSettings(store.db);
|
|
1202
|
+
const merged = settings.setFeatureFlags({
|
|
1203
|
+
summarizeMinIntervalMinutes: 30,
|
|
1204
|
+
summarizeMaxEntriesPerRun: 5,
|
|
1205
|
+
summarizeDedupeMode: "vector",
|
|
1206
|
+
summarizeDedupeMinSim: 0.9,
|
|
1207
|
+
summarizeDedupeWindowHours: 12
|
|
1208
|
+
});
|
|
1209
|
+
assert.equal(merged.summarizeMinIntervalMinutes, 30);
|
|
1210
|
+
assert.equal(merged.summarizeDedupeMode, "vector");
|
|
1211
|
+
assert.equal(merged.summarizeDedupeMinSim, 0.9, "float thresholds round-trip (new numbers whitelist)");
|
|
1212
|
+
// 面板/API 写入走同一套逐键校验:越界与非法枚举必须被拒。
|
|
1213
|
+
assert.throws(() => settings.setFeatureFlags({ summarizeDedupeMinSim: 0.3 }), /number in \[0.5, 0.99\]/);
|
|
1214
|
+
assert.throws(() => settings.setFeatureFlags({ summarizeDedupeMode: "semantic" }), /one of: off, title, vector/);
|
|
1215
|
+
store.close();
|
|
1216
|
+
});
|
|
1217
|
+
|
|
1218
|
+
// ── Issue #127 补测:fail-safe 与打点回滚的边界路径 ────────────────────────
|
|
1219
|
+
|
|
1220
|
+
test("issue#127: a throwing audit writer never breaks the min-interval skip path", async () => {
|
|
1221
|
+
const { events, store, service } = setup({ summarizeMinIntervalMinutes: 30 });
|
|
1222
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
1223
|
+
const session = sessionFor("t10");
|
|
1224
|
+
await handler(session, { seq: 2, type: "turn/end" });
|
|
1225
|
+
assert.equal(store.count(), 2);
|
|
1226
|
+
// 审计写入器抛错:跳过路径必须吞掉异常——审计是观测手段,绝不能反过来打断蒸馏。
|
|
1227
|
+
service.saveLlmAudit = () => { throw new Error("audit sink down"); };
|
|
1228
|
+
await handler(session, { seq: 3, type: "turn/end" });
|
|
1229
|
+
assert.equal(store.count(), 2, "the throttled turn stays a no-op instead of rejecting");
|
|
1230
|
+
});
|
|
1231
|
+
|
|
1232
|
+
test("issue#127: an aborted run rolls the interval claim back so the next turn is admitted", async () => {
|
|
1233
|
+
// 间隔取毫秒级小数值(schema 的粒度是 1 分钟,测试直接构造 config 绕开它),
|
|
1234
|
+
// 这样"回滚到上一次打点"与"保留本次打点"才有可观察差异:
|
|
1235
|
+
// 回滚 → 下一次落在 T1+gap 之外,应被接纳;
|
|
1236
|
+
// 不滚 → 距本次打点才几毫秒,必被挡。
|
|
1237
|
+
// 若沿用 gap=0,两种实现的对外行为完全相同,测试无法发现 else 分支写错。
|
|
1238
|
+
let call = 0;
|
|
1239
|
+
const { events } = setup({ summarizeMinIntervalMinutes: 0.01, distillRateLimitIntervalMs: 0 }, {
|
|
1240
|
+
stream() {
|
|
1241
|
+
call++;
|
|
1242
|
+
if (call === 2) {
|
|
1243
|
+
return (async function* () {
|
|
1244
|
+
yield { type: "block-start", block: { type: "text" } };
|
|
1245
|
+
yield { type: "text-delta", delta: "[]" };
|
|
1246
|
+
yield { type: "finish", kind: "aborted" };
|
|
1247
|
+
})();
|
|
1248
|
+
}
|
|
1249
|
+
return (async function* () {
|
|
1250
|
+
yield { type: "block-start", block: { type: "text" } };
|
|
1251
|
+
yield { type: "text-delta", delta: JSON.stringify([{ type: "decision", title: `第${call}条`, content: "内容", importance: 3 }]) };
|
|
1252
|
+
yield { type: "finish", kind: "ok" };
|
|
1253
|
+
})();
|
|
1254
|
+
}
|
|
1255
|
+
});
|
|
1256
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
1257
|
+
const session = sessionFor("t11");
|
|
1258
|
+
await handler(session, { seq: 2, type: "turn/end" }); // T1:成功,占用间隔
|
|
1259
|
+
assert.equal(call, 1, "the first turn distills");
|
|
1260
|
+
await new Promise((r) => setTimeout(r, 700)); // 越过 600ms 间隔窗
|
|
1261
|
+
session.events.push(userMessage("第二轮", 3), { seq: 4, type: "turn/end" });
|
|
1262
|
+
await handler(session, { seq: 4, type: "turn/end" }); // T2:aborted → 回滚到 T1
|
|
1263
|
+
assert.equal(call, 2, "the aborted turn still reached the LLM call");
|
|
1264
|
+
await handler(session, { seq: 4, type: "turn/end" }); // 立刻再发:只有回滚到 T1 才会被接纳
|
|
1265
|
+
assert.equal(call, 3, "the aborted run did not consume the interval window");
|
|
1266
|
+
});
|
|
1267
|
+
|
|
1268
|
+
test("issue#127: a throwing candidate lookup degrades to undefined (service fail-safe)", async () => {
|
|
1269
|
+
const { service } = setup({ summarizeDedupeMode: "vector" });
|
|
1270
|
+
// 候选查询在 try 内抛错 → findSessionDuplicate 吞掉并返回 undefined(不向外抛)。
|
|
1271
|
+
const hostile = { get type() { throw new Error("store down"); }, title: "标题", content: "内容" };
|
|
1272
|
+
const hit = await service.findSessionDuplicate(hostile, { mode: "vector", source: "session:z" });
|
|
1273
|
+
assert.equal(hit, undefined, "a failed lookup yields no duplicate instead of throwing");
|
|
1274
|
+
});
|
|
1275
|
+
|
|
1276
|
+
test("issue#127: a dedupe lookup that yields nothing still lands the entry (never dropped)", async () => {
|
|
1277
|
+
// 走真实的 session/event 落库路径。只让去重判定不可用(service 层为何返回 undefined
|
|
1278
|
+
// 由上一条覆盖)——蒸馏必须照常写入,不能因为拿不到去重结论就把这一条丢掉。
|
|
1279
|
+
const { events, store, service } = setup(
|
|
1280
|
+
{ summarizeDedupeMode: "vector" },
|
|
1281
|
+
{ stream: streamOf([{ type: "decision", title: "必须落库", content: "内容", importance: 3 }]) }
|
|
1282
|
+
);
|
|
1283
|
+
service.findSessionDuplicate = async () => undefined;
|
|
1284
|
+
const handler = events.find((e) => e.name === "session/event").fn;
|
|
1285
|
+
await handler(sessionFor("t9"), { seq: 2, type: "turn/end" });
|
|
1286
|
+
assert.equal(store.count(), 1, "the entry is stored through the normal write path");
|
|
1287
|
+
assert.equal(store.all()[0].title, "必须落库");
|
|
1288
|
+
});
|