dsh-memu-memory 1.3.6 → 1.3.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/AGENTS.md +2 -1
- package/CHANGELOG.md +87 -0
- package/README.md +7 -7
- package/README.zh.md +7 -7
- package/docs/client-conventions.md +4 -0
- package/docs/contributing.md +2 -2
- package/docs/memory-semantics.md +10 -7
- package/docs/troubleshooting.md +132 -7
- package/lib/binding-diagnostics.js +2 -2
- package/lib/client.js +296 -122
- package/lib/consolidation-ledger.js +6 -2
- package/lib/consolidation-worker.js +7 -0
- package/lib/inbound.js +24 -9
- package/lib/inject.js +379 -215
- package/lib/lexicon.js +5 -0
- package/lib/local-embedding-manager.js +4 -0
- package/lib/local-vector-service.js +65 -1
- package/lib/memory.js +711 -175
- package/lib/paths.js +24 -0
- package/lib/private.js +284 -127
- package/lib/rpc.js +144 -45
- package/lib/snippet.js +49 -28
- package/lib/state.js +185 -26
- package/lib/tools.js +150 -31
- package/lib/webui/standalone.css +28 -1
- package/lib/webui.js +25 -1
- package/lib/worker-pool.js +260 -26
- package/package.json +1 -1
- package/scripts/check-i18n.mjs +19 -0
- package/scripts/recall-bench.mjs +4 -4
- package/scripts/simulate-injection.mjs +3 -0
- package/tests/activity-wiring.test.mjs +52 -0
- package/tests/client-render.test.mjs +207 -0
- package/tests/consolidation-feed.test.mjs +26 -0
- package/tests/consolidation-fork.test.mjs +618 -33
- package/tests/consolidation-snapshot.test.mjs +2 -1
- package/tests/cross-library-recall.test.mjs +156 -0
- package/tests/cue.test.mjs +15 -2
- package/tests/hybrid-recall.test.mjs +488 -0
- package/tests/inbound.test.mjs +49 -4
- package/tests/inject-conflict.test.mjs +70 -9
- package/tests/inject-guide.test.mjs +13 -1
- package/tests/inject.test.mjs +10 -0
- package/tests/injection-budget.test.mjs +101 -0
- package/tests/injection-chain.test.mjs +37 -0
- package/tests/injection-rollback.test.mjs +172 -9
- package/tests/issue2-chain.test.mjs +169 -0
- package/tests/lexicon-inject.test.mjs +33 -5
- package/tests/lexicon-ui.test.mjs +99 -2
- package/tests/managed-memory.test.mjs +625 -1
- package/tests/media.test.mjs +1 -1
- package/tests/new-session-gate.test.mjs +280 -25
- package/tests/open-loop-date.test.mjs +11 -1
- package/tests/recall-intent.test.mjs +52 -3
- package/tests/simulate-injection.test.mjs +4 -4
- package/tests/snippet.test.mjs +33 -1
- package/tests/state.test.mjs +8 -3
- package/tests/tool-schema.test.mjs +52 -0
- package/tests/tools.test.mjs +394 -0
- package/tests/webui.test.mjs +16 -2
- package/tests/worker-lifecycle.test.mjs +419 -36
|
@@ -11,6 +11,43 @@ function indexOf(entries) {
|
|
|
11
11
|
return { entries }
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
+
function closeTo(actual, expected, message) {
|
|
15
|
+
assert.ok(Number.isFinite(actual), `${message}: expected a finite number, got ${actual}`)
|
|
16
|
+
assert.ok(Math.abs(actual - expected) <= 1e-12, `${message}: expected ${expected}, got ${actual}`)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function freezeInput(value) {
|
|
20
|
+
if (!value || typeof value !== 'object') return value
|
|
21
|
+
for (const child of Object.values(value)) freezeInput(child)
|
|
22
|
+
return Object.freeze(value)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function fuseUnchanged(options) {
|
|
26
|
+
const before = structuredClone(options)
|
|
27
|
+
freezeInput(options)
|
|
28
|
+
const fused = memory.fuseRecallCandidates(options)
|
|
29
|
+
assert.deepEqual(options, before, 'fusion must not mutate either route, candidate metadata, or the index')
|
|
30
|
+
return fused
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function assertNoDominance(fused) {
|
|
34
|
+
for (const file of fused.files) {
|
|
35
|
+
for (const key of ['lexicalDominant', 'lexicalDominantId', 'lexicalDiscount', 'dominanceStage']) {
|
|
36
|
+
assert.equal(Object.hasOwn(file.fusion, key), false, `${file.name}: no blowout must omit ${key}`)
|
|
37
|
+
}
|
|
38
|
+
closeTo(file.retrievalScore, file.fusion.rawRetrievalScore, `${file.name}: no blowout must preserve its raw score`)
|
|
39
|
+
}
|
|
40
|
+
assert.equal(fused.debug.lexicalDominance, null)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function duplicateIndex() {
|
|
44
|
+
return indexOf({
|
|
45
|
+
'mem-a': entry({ title: 'deploy window', content: 'the deploy window is thursday two to four am, grey releases only' }),
|
|
46
|
+
'mem-z': entry({ title: 'deploy window', content: 'the deploy window is thursday two to four am, grey releases only' }),
|
|
47
|
+
'mem-other': entry({ title: 'reimbursement deadline', content: 'expense reports are due before the last working day of each month' }),
|
|
48
|
+
})
|
|
49
|
+
}
|
|
50
|
+
|
|
14
51
|
test('RRF keeps one canonical id when both routes find it and preserves the best order', () => {
|
|
15
52
|
const index = indexOf({
|
|
16
53
|
'mem-a': entry({ title: 'alpha' }),
|
|
@@ -120,3 +157,454 @@ test('equal fused scores fall back to the best rank and then the canonical id',
|
|
|
120
157
|
assert.deepEqual(first.files.map((file) => file.name), ['mem-a', 'mem-b'])
|
|
121
158
|
assert.deepEqual(first.files.map((file) => file.name), second.files.map((file) => file.name))
|
|
122
159
|
})
|
|
160
|
+
|
|
161
|
+
// 2026-11-18 排序修正:RRF 名次归一把"词法满分 vs 普通命中"的原始分差压成近平手
|
|
162
|
+
// (1.0 vs 0.95),±50% 的有界偏置(首因/峰终)随即把它顶到前面——运行时实测把
|
|
163
|
+
// lexical 219.5 的完美命中挤到 #8。词法巨无霸(topLex ≥ 24 且对第二名 ≥4 倍断层)
|
|
164
|
+
// 在场时,其余行的 share 按 max(1/(1+topLex/50), sqrt(lexical/topLex)) 折减,强命中保住头部。
|
|
165
|
+
test('a dominant lexical hit keeps its seat; the rank-flattened near-tie cannot be exploited by biases', () => {
|
|
166
|
+
const index = indexOf({
|
|
167
|
+
'mem-food': entry({ title: '主人是云南人,爱吃松茸菌子和过桥米线', createdAt: '2026-11-17T00:00:00.000Z', updatedAt: '2026-11-17T00:00:00.000Z', strength: 0.8 }),
|
|
168
|
+
'mem-meta': entry({ title: '运营值守安排', createdAt: '2026-10-01T00:00:00.000Z', updatedAt: '2026-10-01T00:00:00.000Z', strength: 1 }),
|
|
169
|
+
})
|
|
170
|
+
const fused = memory.fuseRecallCandidates({
|
|
171
|
+
// 词法腿:food 满分、meta 尾随;语义腿次序相反——正是运行时被压过的形态。
|
|
172
|
+
local: [{ name: 'mem-food', lexical: 219.5 }, { name: 'mem-meta', lexical: 4 }],
|
|
173
|
+
semantic: [{ name: 'mem-meta' }, { name: 'mem-food' }],
|
|
174
|
+
index,
|
|
175
|
+
limit: 12,
|
|
176
|
+
})
|
|
177
|
+
const food = fused.files.find((file) => file.name === 'mem-food')
|
|
178
|
+
const meta = fused.files.find((file) => file.name === 'mem-meta')
|
|
179
|
+
assert.equal(fused.files[0].name, 'mem-food', 'the dominant lexical hit must rank first')
|
|
180
|
+
assert.equal(food.retrievalScore, 1)
|
|
181
|
+
assert.ok(meta.retrievalScore < 0.2, 'the flattened near-tie must be broken, got ' + meta.retrievalScore)
|
|
182
|
+
assert.equal(food.fusion.lexicalDominant, 219.5, 'the dominance is observable in fusion metadata')
|
|
183
|
+
const floor = 1 / (1 + 219.5 / 50)
|
|
184
|
+
closeTo(floor, 0.18552875695732837, '219.5-vs-4 uses the new floor, not the old sqrt-only discount')
|
|
185
|
+
closeTo(meta.fusion.rawRetrievalScore, 1, 'meta keeps its original RRF share')
|
|
186
|
+
closeTo(meta.fusion.lexicalDiscount, floor, '4/219.5 is below the lexical floor')
|
|
187
|
+
closeTo(meta.retrievalScore, floor, 'the discounted meta score is exactly the floor')
|
|
188
|
+
for (const file of [food, meta]) {
|
|
189
|
+
assert.equal(file.fusion.lexicalDominantId, 'mem-food')
|
|
190
|
+
assert.equal(file.fusion.dominanceStage, 'library')
|
|
191
|
+
}
|
|
192
|
+
assert.equal(food.fusion.lexicalDiscount, 1)
|
|
193
|
+
})
|
|
194
|
+
|
|
195
|
+
test('no lexical blowout means no discount: the human-like bias ordering is untouched', () => {
|
|
196
|
+
const index = indexOf({
|
|
197
|
+
'mem-x': entry({ title: 'x 记录', content: '甲项目上线时间表和回滚窗口安排', createdAt: '2026-10-01T00:00:00.000Z' }),
|
|
198
|
+
'mem-y': entry({ title: 'y 记录', content: '乙仓库的镜像同步频率与带宽预算', createdAt: '2026-10-02T00:00:00.000Z' }),
|
|
199
|
+
})
|
|
200
|
+
const fused = memory.fuseRecallCandidates({
|
|
201
|
+
local: [{ name: 'mem-x', lexical: 5 }, { name: 'mem-y', lexical: 4 }],
|
|
202
|
+
semantic: [{ name: 'mem-y' }, { name: 'mem-x' }],
|
|
203
|
+
index,
|
|
204
|
+
limit: 12,
|
|
205
|
+
})
|
|
206
|
+
const x = fused.files.find((file) => file.name === 'mem-x')
|
|
207
|
+
const y = fused.files.find((file) => file.name === 'mem-y')
|
|
208
|
+
assert.ok(x && y, 'distinct texts must not fold into one cluster slot')
|
|
209
|
+
assert.equal(x.fusion.lexicalDominant, undefined, 'a 5-vs-4 gap is not a blowout')
|
|
210
|
+
assert.equal(x.retrievalScore, 1)
|
|
211
|
+
assert.equal(y.retrievalScore, 1)
|
|
212
|
+
assertNoDominance(fused)
|
|
213
|
+
})
|
|
214
|
+
|
|
215
|
+
// P2: tiny positive evidence must never score worse than zero evidence.
|
|
216
|
+
test('lexical dominance uses the exact floor and is monotone at zero, epsilon, 0.51, 5, and top', () => {
|
|
217
|
+
const topLex = 1000
|
|
218
|
+
const floor = 1 / (1 + topLex / 50)
|
|
219
|
+
const scores = []
|
|
220
|
+
const discounts = []
|
|
221
|
+
for (const lexical of [0, Number.EPSILON, 0.51, 5]) {
|
|
222
|
+
const fused = fuseUnchanged({
|
|
223
|
+
local: [{ name: 'mem-a', lexical: topLex }, { name: 'mem-other', lexical }],
|
|
224
|
+
semantic: [{ name: 'mem-other' }, { name: 'mem-a' }],
|
|
225
|
+
index: duplicateIndex(),
|
|
226
|
+
})
|
|
227
|
+
const top = fused.files.find((file) => file.name === 'mem-a')
|
|
228
|
+
const weak = fused.files.find((file) => file.name === 'mem-other')
|
|
229
|
+
const expected = Math.max(floor, Math.sqrt(Math.min(1, lexical / topLex)))
|
|
230
|
+
assert.equal(fused.files[0].name, 'mem-a', `lexical=${lexical}: the strong fact must stay first`)
|
|
231
|
+
closeTo(top.fusion.rawRetrievalScore, 1, 'top keeps the undiscounted near-tie')
|
|
232
|
+
closeTo(top.fusion.lexicalDiscount, 1, 'top evidence is never discounted')
|
|
233
|
+
closeTo(top.retrievalScore, 1, 'top score remains one')
|
|
234
|
+
closeTo(weak.fusion.rawRetrievalScore, 1, 'weak keeps the same raw score for the monotonic comparison')
|
|
235
|
+
closeTo(weak.fusion.lexicalDiscount, expected, `lexical=${lexical}: exact max(floor, sqrt) discount`)
|
|
236
|
+
closeTo(weak.retrievalScore, expected, `lexical=${lexical}: exact discounted retrieval`)
|
|
237
|
+
for (const file of [top, weak]) {
|
|
238
|
+
assert.equal(file.fusion.lexicalDominant, topLex)
|
|
239
|
+
assert.equal(file.fusion.lexicalDominantId, 'mem-a')
|
|
240
|
+
assert.equal(file.fusion.dominanceStage, 'library')
|
|
241
|
+
}
|
|
242
|
+
scores.push(weak.retrievalScore)
|
|
243
|
+
discounts.push(weak.fusion.lexicalDiscount)
|
|
244
|
+
}
|
|
245
|
+
scores.push(1)
|
|
246
|
+
discounts.push(1)
|
|
247
|
+
closeTo(scores[0], 1 / 21, 'zero evidence uses the exact floor')
|
|
248
|
+
closeTo(scores[1], scores[0], 'epsilon must not be punished relative to zero')
|
|
249
|
+
closeTo(scores[2], scores[0], '0.51 remains protected by the floor')
|
|
250
|
+
closeTo(scores[3], Math.sqrt(5 / topLex), '5 crosses the floor and takes the sqrt branch')
|
|
251
|
+
for (let i = 1; i < scores.length; i += 1) {
|
|
252
|
+
assert.ok(scores[i] >= scores[i - 1], 'more lexical evidence must never reduce the retrieval score')
|
|
253
|
+
assert.ok(discounts[i] >= discounts[i - 1], 'more lexical evidence must never reduce the discount factor')
|
|
254
|
+
}
|
|
255
|
+
})
|
|
256
|
+
|
|
257
|
+
for (const { label, topLex, second, dominant } of [
|
|
258
|
+
{ label: 'below 24 even with no runner-up evidence', topLex: 24 - 1e-6, second: 0, dominant: false },
|
|
259
|
+
{ label: 'at 24 with no runner-up evidence', topLex: 24, second: 0, dominant: true },
|
|
260
|
+
{ label: 'at both 24 and exactly four times the runner-up', topLex: 24, second: 6, dominant: true },
|
|
261
|
+
{ label: 'at 24 but just below the four-times ratio', topLex: 24, second: 6 + 1e-6, dominant: false },
|
|
262
|
+
]) {
|
|
263
|
+
test(`lexical dominance threshold: ${label}`, () => {
|
|
264
|
+
const fused = fuseUnchanged({
|
|
265
|
+
local: [{ name: 'mem-a', lexical: topLex }, { name: 'mem-other', lexical: second }],
|
|
266
|
+
semantic: [{ name: 'mem-other' }, { name: 'mem-a' }],
|
|
267
|
+
index: duplicateIndex(),
|
|
268
|
+
})
|
|
269
|
+
assert.equal(fused.files.length, 2)
|
|
270
|
+
if (!dominant) {
|
|
271
|
+
assertNoDominance(fused)
|
|
272
|
+
for (const file of fused.files) assert.equal(file.retrievalScore, 1)
|
|
273
|
+
return
|
|
274
|
+
}
|
|
275
|
+
assert.equal(fused.files[0].name, 'mem-a')
|
|
276
|
+
const other = fused.files.find((file) => file.name === 'mem-other')
|
|
277
|
+
closeTo(other.fusion.lexicalDiscount, Math.max(1 / (1 + topLex / 50), Math.sqrt(second / topLex)), 'threshold discount')
|
|
278
|
+
for (const file of fused.files) {
|
|
279
|
+
assert.equal(file.fusion.lexicalDominant, topLex)
|
|
280
|
+
assert.equal(file.fusion.lexicalDominantId, 'mem-a')
|
|
281
|
+
assert.equal(file.fusion.dominanceStage, 'library')
|
|
282
|
+
assert.equal(file.fusion.rawRetrievalScore, 1)
|
|
283
|
+
}
|
|
284
|
+
assert.equal(fused.debug.lexicalDominance.second, second)
|
|
285
|
+
})
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
test('two equally strong but different facts prevent lexical dominance and keep raw RRF ordering', () => {
|
|
289
|
+
const fused = fuseUnchanged({
|
|
290
|
+
local: [{ name: 'mem-a', lexical: 100 }, { name: 'mem-other', lexical: 100 }],
|
|
291
|
+
semantic: [],
|
|
292
|
+
index: duplicateIndex(),
|
|
293
|
+
})
|
|
294
|
+
assert.deepEqual(fused.files.map((file) => file.name), ['mem-a', 'mem-other'])
|
|
295
|
+
assert.equal(fused.files.length, 2, 'different facts must not fold despite equal lexical scores')
|
|
296
|
+
assertNoDominance(fused)
|
|
297
|
+
closeTo(fused.files[0].retrievalScore, 1, 'the leading local rank is unchanged')
|
|
298
|
+
closeTo(fused.files[1].retrievalScore, 61 / 62, 'the second local rank is unchanged')
|
|
299
|
+
})
|
|
300
|
+
|
|
301
|
+
for (const route of ['local', 'semantic']) {
|
|
302
|
+
test(`same-leg ${route} duplicate inherits the strongest lexical evidence without a second RRF contribution`, () => {
|
|
303
|
+
const fused = fuseUnchanged({
|
|
304
|
+
local: route === 'local' ? [{ name: 'mem-a', lexical: 0 }, { name: 'mem-z', lexical: 100 }, { name: 'mem-other', lexical: 20 }] : [],
|
|
305
|
+
semantic: route === 'semantic' ? [{ name: 'mem-a', lexical: 0 }, { name: 'mem-z', lexical: 100 }, { name: 'mem-other', lexical: 20 }] : [],
|
|
306
|
+
index: duplicateIndex(),
|
|
307
|
+
})
|
|
308
|
+
assert.deepEqual(fused.files.map((file) => file.name), ['mem-a', 'mem-other'])
|
|
309
|
+
const representative = fused.files[0]
|
|
310
|
+
const other = fused.files[1]
|
|
311
|
+
assert.equal(representative.lexical, 100)
|
|
312
|
+
assert.equal(representative.lexicalEvidence, 100, 'omitted lexicalEvidence must fall back to the absorbed lexical score')
|
|
313
|
+
assert.equal(representative.fusion.lexicalDominant, 100)
|
|
314
|
+
assert.equal(representative.fusion.lexicalDominantId, 'mem-a')
|
|
315
|
+
assert.equal(representative.fusion.dominanceStage, 'library')
|
|
316
|
+
assert.equal(representative.fusion.lexicalDiscount, 1, 'the representative must not be treated as zero-lexical evidence')
|
|
317
|
+
assert.equal(representative.retrievalScore, 1)
|
|
318
|
+
assert.deepEqual(representative.fusion.routes, [route])
|
|
319
|
+
assert.equal(representative.fusion.localRank, route === 'local' ? 1 : null)
|
|
320
|
+
assert.equal(representative.fusion.semanticRank, route === 'semantic' ? 1 : null)
|
|
321
|
+
assert.equal(representative.fusion.bestRank, 1)
|
|
322
|
+
closeTo(representative.fusion.rrf, 1 / 61, 'one route contributes once despite duplicate facts')
|
|
323
|
+
closeTo(other.fusion.rrf, 1 / 62, 'the folded duplicate must not consume a route rank')
|
|
324
|
+
closeTo(other.fusion.rawRetrievalScore, 61 / 62, 'normalization uses only surviving RRF contributions')
|
|
325
|
+
closeTo(other.retrievalScore, (61 / 62) * Math.sqrt(20 / 100), 'the surviving weak fact is discounted once')
|
|
326
|
+
assert.deepEqual(fused.debug.collapsed, [{ kept: 'mem-a', dropped: ['mem-z'] }])
|
|
327
|
+
assert.equal(fused.debug[`${route}Candidates`], 2)
|
|
328
|
+
})
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
for (const strongRoute of ['local', 'semantic']) {
|
|
332
|
+
test(`cross-leg duplicate inherits ${strongRoute} evidence and each route's best rank exactly once`, () => {
|
|
333
|
+
const fused = fuseUnchanged({
|
|
334
|
+
local: [{ name: strongRoute === 'local' ? 'mem-z' : 'mem-a', lexical: strongRoute === 'local' ? 100 : 0 }, { name: 'mem-other', lexical: 20 }],
|
|
335
|
+
semantic: [{ name: strongRoute === 'semantic' ? 'mem-z' : 'mem-a', lexical: strongRoute === 'semantic' ? 100 : 0 }, { name: 'mem-other' }],
|
|
336
|
+
index: duplicateIndex(),
|
|
337
|
+
})
|
|
338
|
+
assert.deepEqual(fused.files.map((file) => file.name), ['mem-a', 'mem-other'])
|
|
339
|
+
const representative = fused.files[0]
|
|
340
|
+
const other = fused.files[1]
|
|
341
|
+
assert.equal(representative.lexical, 100, 'the alphabetical representative must inherit the absorbed strong fact')
|
|
342
|
+
assert.equal(representative.lexicalEvidence, 100)
|
|
343
|
+
assert.deepEqual(representative.mergedTogether, ['mem-z'])
|
|
344
|
+
assert.deepEqual([...representative.fusion.routes].sort(), ['local', 'semantic'])
|
|
345
|
+
assert.equal(representative.fusion.localRank, 1)
|
|
346
|
+
assert.equal(representative.fusion.semanticRank, 1)
|
|
347
|
+
assert.equal(representative.fusion.bestRank, 1)
|
|
348
|
+
closeTo(representative.fusion.rrf, 2 / 61, 'each distinct route contributes its best rank once')
|
|
349
|
+
closeTo(representative.fusion.rawRetrievalScore, 1, 'the new representative normalizes against the surviving maximum')
|
|
350
|
+
assert.equal(representative.fusion.lexicalDominant, 100)
|
|
351
|
+
assert.equal(representative.fusion.lexicalDominantId, 'mem-a')
|
|
352
|
+
assert.equal(representative.fusion.dominanceStage, 'library')
|
|
353
|
+
assert.equal(representative.fusion.lexicalDiscount, 1, 'inherited local evidence must not be discounted as semantic-only')
|
|
354
|
+
assert.equal(representative.retrievalScore, 1)
|
|
355
|
+
closeTo(other.fusion.rrf, 2 / 62, 'the unrelated shared fact has only two contributions')
|
|
356
|
+
closeTo(other.fusion.rawRetrievalScore, 61 / 62, 'cross-leg folding preserves raw rank shares')
|
|
357
|
+
assert.equal(fused.debug.lexicalDominance.second, 20)
|
|
358
|
+
assert.deepEqual(fused.debug.collapsed, [{ kept: 'mem-a', dropped: ['mem-z'] }])
|
|
359
|
+
})
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
test('absorb regression: local z100,a100,other20 and semantic a,z,other leave a strong and first', () => {
|
|
363
|
+
const fused = fuseUnchanged({
|
|
364
|
+
local: [{ name: 'mem-z', lexical: 100 }, { name: 'mem-a', lexical: 100 }, { name: 'mem-other', lexical: 20 }],
|
|
365
|
+
semantic: [{ name: 'mem-a' }, { name: 'mem-z' }, { name: 'mem-other' }],
|
|
366
|
+
index: duplicateIndex(),
|
|
367
|
+
})
|
|
368
|
+
assert.deepEqual(fused.files.map((file) => file.name), ['mem-a', 'mem-other'])
|
|
369
|
+
const representative = fused.files[0]
|
|
370
|
+
const other = fused.files[1]
|
|
371
|
+
assert.equal(representative.lexical, 100, 'a must inherit z rather than remain semantic-only')
|
|
372
|
+
assert.equal(representative.lexicalEvidence, 100)
|
|
373
|
+
assert.deepEqual(representative.mergedTogether, ['mem-z'])
|
|
374
|
+
assert.deepEqual([...representative.fusion.routes].sort(), ['local', 'semantic'])
|
|
375
|
+
assert.equal(representative.fusion.localRank, 1)
|
|
376
|
+
assert.equal(representative.fusion.semanticRank, 1)
|
|
377
|
+
assert.equal(representative.fusion.bestRank, 1)
|
|
378
|
+
closeTo(representative.fusion.rrf, 2 / 61, 'both duplicate pairs still contribute just once per route')
|
|
379
|
+
assert.equal(representative.fusion.rawRetrievalScore, 1)
|
|
380
|
+
assert.equal(representative.fusion.lexicalDominant, 100, 'dominance is recomputed after a absorbs z')
|
|
381
|
+
assert.equal(representative.fusion.lexicalDominantId, 'mem-a')
|
|
382
|
+
assert.equal(representative.fusion.dominanceStage, 'library')
|
|
383
|
+
assert.equal(representative.fusion.lexicalDiscount, 1)
|
|
384
|
+
assert.equal(representative.retrievalScore, 1)
|
|
385
|
+
assert.equal(fused.debug.lexicalDominance.second, 20, 'the absorbed 100 must not count as the runner-up')
|
|
386
|
+
assert.equal(fused.debug.localCandidates, 2)
|
|
387
|
+
assert.equal(fused.debug.semanticCandidates, 2)
|
|
388
|
+
closeTo(other.fusion.rrf, 2 / 62, 'other has no duplicate RRF bonus')
|
|
389
|
+
closeTo(other.fusion.rawRetrievalScore, 61 / 62, 'other keeps its undiscounted fused rank')
|
|
390
|
+
closeTo(other.retrievalScore, (61 / 62) * Math.sqrt(20 / 100), 'other receives one survivor-stage discount')
|
|
391
|
+
})
|
|
392
|
+
|
|
393
|
+
test('cross-leg folding of overlapping route contributors keeps only the best rank per route', () => {
|
|
394
|
+
// Dice is not transitive: the two shortened variants remain distinct on the
|
|
395
|
+
// local leg, while the semantic bridge matches both. Disjoint bigram ranges
|
|
396
|
+
// make that shape deterministic without depending on natural-language tokens.
|
|
397
|
+
const common = Array.from({ length: 50 }, (_, i) => String.fromCharCode(0x3400 + i)).join('')
|
|
398
|
+
const left = Array.from({ length: 20 }, (_, i) => String.fromCharCode(0x3500 + i)).join('')
|
|
399
|
+
const right = Array.from({ length: 20 }, (_, i) => String.fromCharCode(0x3600 + i)).join('')
|
|
400
|
+
const fused = fuseUnchanged({
|
|
401
|
+
local: [{ name: 'mem-b', lexical: 0 }, { name: 'mem-c', lexical: 100 }, { name: 'mem-other', lexical: 20 }],
|
|
402
|
+
semantic: [{ name: 'mem-a' }, { name: 'mem-other' }],
|
|
403
|
+
index: indexOf({
|
|
404
|
+
'mem-a': entry({ title: 'deployment', content: common + left + right }),
|
|
405
|
+
'mem-b': entry({ title: 'deployment', content: common + left }),
|
|
406
|
+
'mem-c': entry({ title: 'deployment', content: common + right }),
|
|
407
|
+
'mem-other': duplicateIndex().entries['mem-other'],
|
|
408
|
+
}),
|
|
409
|
+
})
|
|
410
|
+
assert.equal(fused.debug.localCandidates, 3, 'the two shortened variants must survive the same-leg fold')
|
|
411
|
+
assert.equal(fused.debug.semanticCandidates, 2)
|
|
412
|
+
assert.deepEqual(fused.files.map((file) => file.name), ['mem-a', 'mem-other'])
|
|
413
|
+
const representative = fused.files[0]
|
|
414
|
+
const other = fused.files[1]
|
|
415
|
+
assert.deepEqual(representative.mergedTogether, ['mem-b', 'mem-c'])
|
|
416
|
+
assert.deepEqual([...representative.fusion.routes].sort(), ['local', 'semantic'])
|
|
417
|
+
assert.equal(representative.fusion.localRank, 1, 'absorbing local rank two must keep the better local rank one')
|
|
418
|
+
assert.equal(representative.fusion.semanticRank, 1)
|
|
419
|
+
assert.equal(representative.fusion.bestRank, 1)
|
|
420
|
+
closeTo(representative.fusion.rrf, 2 / 61, 'the second local contributor must not add another RRF term')
|
|
421
|
+
assert.equal(representative.lexical, 100)
|
|
422
|
+
assert.equal(representative.lexicalEvidence, 100)
|
|
423
|
+
assert.equal(representative.fusion.lexicalDominant, 100)
|
|
424
|
+
assert.equal(representative.fusion.lexicalDominantId, 'mem-a')
|
|
425
|
+
assert.equal(representative.fusion.lexicalDiscount, 1)
|
|
426
|
+
assert.equal(representative.fusion.rawRetrievalScore, 1)
|
|
427
|
+
assert.equal(representative.retrievalScore, 1)
|
|
428
|
+
closeTo(other.fusion.rrf, 1 / 63 + 1 / 62, 'unrelated route ranks remain unchanged by cross-leg folding')
|
|
429
|
+
closeTo(other.fusion.rawRetrievalScore, (1 / 63 + 1 / 62) / (2 / 61), 'survivor normalization excludes duplicate route terms')
|
|
430
|
+
assert.deepEqual(fused.debug.collapsed, [{ kept: 'mem-a', dropped: ['mem-b', 'mem-c'] }])
|
|
431
|
+
})
|
|
432
|
+
|
|
433
|
+
test('the same canonical id across legs inherits evidence while keeping one contribution per route', () => {
|
|
434
|
+
const fused = fuseUnchanged({
|
|
435
|
+
local: [{ name: 'mem-a', lexical: 100, lexicalEvidence: 0 }, { name: 'mem-other', lexical: 4 }],
|
|
436
|
+
semantic: [{ name: 'mem-a', lexical: 30, lexicalEvidence: 30 }, { name: 'mem-other' }],
|
|
437
|
+
index: duplicateIndex(),
|
|
438
|
+
})
|
|
439
|
+
assert.deepEqual(fused.files.map((file) => file.name), ['mem-a', 'mem-other'])
|
|
440
|
+
const representative = fused.files[0]
|
|
441
|
+
assert.equal(representative.lexical, 100, 'the full-query local score remains the maximum')
|
|
442
|
+
assert.equal(representative.lexicalEvidence, 30, 'the semantic object may supply stronger topic evidence')
|
|
443
|
+
assert.deepEqual(representative.fusion.routes, ['local', 'semantic'])
|
|
444
|
+
assert.equal(representative.fusion.localRank, 1)
|
|
445
|
+
assert.equal(representative.fusion.semanticRank, 1)
|
|
446
|
+
assert.equal(representative.fusion.bestRank, 1)
|
|
447
|
+
closeTo(representative.fusion.rrf, 2 / 61, 'a canonical id receives only one RRF contribution from each leg')
|
|
448
|
+
assert.equal(representative.fusion.rawRetrievalScore, 1)
|
|
449
|
+
assert.equal(representative.fusion.lexicalDominant, 30)
|
|
450
|
+
assert.equal(representative.fusion.lexicalDominantId, 'mem-a')
|
|
451
|
+
assert.equal(representative.fusion.lexicalDiscount, 1)
|
|
452
|
+
assert.equal(fused.debug.shared, 2)
|
|
453
|
+
assert.equal(fused.debug.clusterCount, 0)
|
|
454
|
+
})
|
|
455
|
+
|
|
456
|
+
test('canonical names and .md aliases inherit their maximum evidence but never double-add RRF or consume ranks', () => {
|
|
457
|
+
const fused = fuseUnchanged({
|
|
458
|
+
local: [{ name: 'mem-a', lexical: 0 }, { name: 'mem-a.md', lexical: 100 }, { name: 'mem-other', lexical: 20 }],
|
|
459
|
+
semantic: [{ name: 'mem-a.md' }, { name: 'mem-a' }, { name: 'mem-other.md' }],
|
|
460
|
+
index: duplicateIndex(),
|
|
461
|
+
limit: 2,
|
|
462
|
+
})
|
|
463
|
+
assert.deepEqual(fused.files.map((file) => file.name), ['mem-a', 'mem-other'])
|
|
464
|
+
const representative = fused.files[0]
|
|
465
|
+
const other = fused.files[1]
|
|
466
|
+
assert.equal(representative.lexical, 100, 'the later alias still contributes its stronger lexical evidence')
|
|
467
|
+
assert.equal(representative.lexicalEvidence, 100)
|
|
468
|
+
assert.deepEqual(representative.fusion.routes, ['local', 'semantic'])
|
|
469
|
+
assert.equal(representative.fusion.localRank, 1)
|
|
470
|
+
assert.equal(representative.fusion.semanticRank, 1)
|
|
471
|
+
assert.equal(representative.fusion.bestRank, 1)
|
|
472
|
+
closeTo(representative.fusion.rrf, 2 / 61, 'alias repetitions do not create extra route contributions')
|
|
473
|
+
assert.equal(representative.fusion.rawRetrievalScore, 1)
|
|
474
|
+
assert.equal(representative.fusion.lexicalDominant, 100)
|
|
475
|
+
assert.equal(representative.fusion.lexicalDominantId, 'mem-a')
|
|
476
|
+
assert.equal(representative.fusion.lexicalDiscount, 1)
|
|
477
|
+
assert.equal(other.fusion.localRank, 2)
|
|
478
|
+
assert.equal(other.fusion.semanticRank, 2)
|
|
479
|
+
closeTo(other.fusion.rrf, 2 / 62, 'aliases never consume the rank of an unrelated fact')
|
|
480
|
+
closeTo(other.fusion.rawRetrievalScore, 61 / 62, 'aliases never inflate the normalization denominator')
|
|
481
|
+
assert.equal(fused.debug.localCandidates, 2)
|
|
482
|
+
assert.equal(fused.debug.semanticCandidates, 2)
|
|
483
|
+
assert.equal(fused.debug.shared, 2)
|
|
484
|
+
})
|
|
485
|
+
|
|
486
|
+
test('duplicate folding merges full-query lexical and topic lexicalEvidence maxima independently', () => {
|
|
487
|
+
const fused = fuseUnchanged({
|
|
488
|
+
local: [{ name: 'mem-a', lexical: 200, lexicalEvidence: 0 }, { name: 'mem-z', lexical: 100, lexicalEvidence: 100 }, { name: 'mem-other', lexical: 20 }],
|
|
489
|
+
semantic: [],
|
|
490
|
+
index: duplicateIndex(),
|
|
491
|
+
})
|
|
492
|
+
assert.deepEqual(fused.files.map((file) => file.name), ['mem-a', 'mem-other'])
|
|
493
|
+
const representative = fused.files[0]
|
|
494
|
+
const other = fused.files[1]
|
|
495
|
+
assert.equal(representative.lexical, 200, 'the full-query maximum belongs to the original representative')
|
|
496
|
+
assert.equal(representative.lexicalEvidence, 100, 'the topic maximum belongs to the absorbed duplicate')
|
|
497
|
+
assert.equal(representative.fusion.lexicalDominant, 100, 'dominance uses topic evidence, not the larger full-query score')
|
|
498
|
+
assert.equal(representative.fusion.lexicalDominantId, 'mem-a')
|
|
499
|
+
assert.equal(representative.fusion.lexicalDiscount, 1)
|
|
500
|
+
closeTo(representative.fusion.rrf, 1 / 61, 'merging two lexical maxima does not add a second route score')
|
|
501
|
+
closeTo(other.fusion.lexicalDiscount, Math.sqrt(20 / 100), 'the weak fact is compared to the topic maximum')
|
|
502
|
+
})
|
|
503
|
+
|
|
504
|
+
test('a strong candidate outside the actual per-leg survivors cannot trigger dominance', () => {
|
|
505
|
+
const fused = fuseUnchanged({
|
|
506
|
+
local: [{ name: 'mem-a', lexical: 5 }, { name: 'mem-other', lexical: 4 }, { name: 'mem-excluded', lexical: 1000 }],
|
|
507
|
+
semantic: [],
|
|
508
|
+
index: indexOf({
|
|
509
|
+
...duplicateIndex().entries,
|
|
510
|
+
'mem-excluded': entry({ title: 'garden watering', content: 'water the bamboo garden at sunrise with rainwater' }),
|
|
511
|
+
}),
|
|
512
|
+
limit: 2,
|
|
513
|
+
})
|
|
514
|
+
assert.deepEqual(fused.files.map((file) => file.name), ['mem-a', 'mem-other'])
|
|
515
|
+
assertNoDominance(fused)
|
|
516
|
+
closeTo(fused.files[0].fusion.rawRetrievalScore, 1, 'the surviving first route rank stays intact')
|
|
517
|
+
closeTo(fused.files[1].fusion.rawRetrievalScore, 61 / 62, 'the surviving second route rank stays intact')
|
|
518
|
+
})
|
|
519
|
+
|
|
520
|
+
test('explicit lexicalEvidence zero prevents a full-query template score of 100 from triggering topic dominance', () => {
|
|
521
|
+
const fused = fuseUnchanged({
|
|
522
|
+
local: [{ name: 'mem-a', lexical: 100, lexicalEvidence: 0 }, { name: 'mem-other', lexical: 4, lexicalEvidence: 4 }],
|
|
523
|
+
semantic: [{ name: 'mem-a' }],
|
|
524
|
+
index: duplicateIndex(),
|
|
525
|
+
})
|
|
526
|
+
assert.deepEqual(fused.files.map((file) => file.name), ['mem-a', 'mem-other'])
|
|
527
|
+
const template = fused.files[0]
|
|
528
|
+
const other = fused.files[1]
|
|
529
|
+
assert.equal(template.lexical, 100, 'the complete-query score must not be rewritten to the topic score')
|
|
530
|
+
assert.equal(template.lexicalEvidence, 0, 'explicit zero must not fall back to full-query lexical')
|
|
531
|
+
assert.equal(other.lexical, 4)
|
|
532
|
+
assert.equal(other.lexicalEvidence, 4)
|
|
533
|
+
closeTo(template.fusion.rrf, 2 / 61, 'template still has its ordinary two-route RRF rank')
|
|
534
|
+
closeTo(other.fusion.rrf, 1 / 62, 'other still has its ordinary local RRF rank')
|
|
535
|
+
closeTo(template.fusion.rawRetrievalScore, 1, 'template raw ranking is retained')
|
|
536
|
+
closeTo(other.fusion.rawRetrievalScore, 61 / 124, 'other raw ranking is retained')
|
|
537
|
+
assertNoDominance(fused)
|
|
538
|
+
})
|
|
539
|
+
|
|
540
|
+
test('real topic evidence 30 beats template full-query lexical 100 and exposes the correct dominant id', () => {
|
|
541
|
+
const fused = fuseUnchanged({
|
|
542
|
+
local: [{ name: 'mem-a', lexical: 100, lexicalEvidence: 0 }, { name: 'mem-other', lexical: 40, lexicalEvidence: 30 }],
|
|
543
|
+
semantic: [{ name: 'mem-a' }, { name: 'mem-other' }],
|
|
544
|
+
index: duplicateIndex(),
|
|
545
|
+
})
|
|
546
|
+
assert.deepEqual(fused.files.map((file) => file.name), ['mem-other', 'mem-a'])
|
|
547
|
+
const topic = fused.files[0]
|
|
548
|
+
const template = fused.files[1]
|
|
549
|
+
assert.equal(template.lexical, 100)
|
|
550
|
+
assert.equal(template.lexicalEvidence, 0)
|
|
551
|
+
assert.equal(topic.lexical, 40)
|
|
552
|
+
assert.equal(topic.lexicalEvidence, 30)
|
|
553
|
+
closeTo(template.fusion.rawRetrievalScore, 1, 'template keeps its original leading raw rank')
|
|
554
|
+
closeTo(topic.fusion.rawRetrievalScore, 61 / 62, 'topic keeps its original trailing raw rank')
|
|
555
|
+
closeTo(template.fusion.lexicalDiscount, 1 / (1 + 30 / 50), 'zero topic evidence takes the floor against 30')
|
|
556
|
+
closeTo(template.retrievalScore, 0.625, 'template is discounted from raw one exactly once')
|
|
557
|
+
assert.equal(topic.fusion.lexicalDiscount, 1)
|
|
558
|
+
closeTo(topic.retrievalScore, 61 / 62, 'the real topic score is not discounted')
|
|
559
|
+
for (const file of [topic, template]) {
|
|
560
|
+
assert.equal(file.fusion.lexicalDominant, 30)
|
|
561
|
+
assert.equal(file.fusion.lexicalDominantId, 'mem-other', 'top id must follow topic evidence, not full-query lexical')
|
|
562
|
+
assert.equal(file.fusion.dominanceStage, 'library')
|
|
563
|
+
}
|
|
564
|
+
assert.equal(fused.debug.lexicalDominance.id, 'mem-other')
|
|
565
|
+
assert.equal(fused.debug.lexicalDominance.topLex, 30)
|
|
566
|
+
assert.equal(fused.debug.lexicalDominance.second, 0)
|
|
567
|
+
})
|
|
568
|
+
|
|
569
|
+
for (const { label, topLex, second, dominant } of [
|
|
570
|
+
{ label: 'a real survivor dominates', topLex: 100, second: 20, dominant: true },
|
|
571
|
+
{ label: 'no survivor dominates', topLex: 5, second: 4, dominant: false },
|
|
572
|
+
]) {
|
|
573
|
+
test(`pending and automatic secret qualifiers do not participate in the lexical top: ${label}`, () => {
|
|
574
|
+
const index = indexOf({
|
|
575
|
+
...duplicateIndex().entries,
|
|
576
|
+
'mem-pending': entry({ status: 'pending_review', title: 'unreviewed voyage', content: 'the polar expedition awaits review of its glacier itinerary' }),
|
|
577
|
+
'mem-secret': entry({ visibility: 'secret', title: 'private credential', content: 'the encrypted archive passphrase is reserved for explicit requests' }),
|
|
578
|
+
})
|
|
579
|
+
const fused = fuseUnchanged({
|
|
580
|
+
local: [{ name: 'mem-pending', lexical: 10000, lexicalEvidence: 10000 }, { name: 'mem-secret', lexical: 9000, lexicalEvidence: 9000 }, { name: 'mem-a', lexical: topLex }, { name: 'mem-other', lexical: second }],
|
|
581
|
+
semantic: [{ name: 'mem-secret', lexical: 9000 }, { name: 'mem-pending', lexical: 10000 }, { name: 'mem-other' }, { name: 'mem-a' }],
|
|
582
|
+
index,
|
|
583
|
+
automatic: true,
|
|
584
|
+
})
|
|
585
|
+
assert.deepEqual(fused.files.map((file) => file.name), ['mem-a', 'mem-other'])
|
|
586
|
+
assert.equal(fused.debug.localCandidates, 2)
|
|
587
|
+
assert.equal(fused.debug.semanticCandidates, 2)
|
|
588
|
+
for (const file of fused.files) {
|
|
589
|
+
closeTo(file.fusion.rrf, 1 / 61 + 1 / 62, 'excluded qualifiers cannot consume route ranks')
|
|
590
|
+
assert.equal(file.fusion.rawRetrievalScore, 1)
|
|
591
|
+
}
|
|
592
|
+
if (!dominant) {
|
|
593
|
+
assertNoDominance(fused)
|
|
594
|
+
return
|
|
595
|
+
}
|
|
596
|
+
const top = fused.files[0]
|
|
597
|
+
const other = fused.files[1]
|
|
598
|
+
assert.equal(top.fusion.lexicalDiscount, 1)
|
|
599
|
+
assert.equal(top.retrievalScore, 1)
|
|
600
|
+
closeTo(other.fusion.lexicalDiscount, Math.sqrt(second / topLex), 'only eligible facts determine the gap')
|
|
601
|
+
closeTo(other.retrievalScore, Math.sqrt(second / topLex), 'qualifier scores do not over-discount eligible facts')
|
|
602
|
+
for (const file of fused.files) {
|
|
603
|
+
assert.equal(file.fusion.lexicalDominant, topLex)
|
|
604
|
+
assert.equal(file.fusion.lexicalDominantId, 'mem-a')
|
|
605
|
+
assert.equal(file.fusion.dominanceStage, 'library')
|
|
606
|
+
}
|
|
607
|
+
assert.equal(fused.debug.lexicalDominance.topLex, topLex)
|
|
608
|
+
assert.equal(fused.debug.lexicalDominance.second, second)
|
|
609
|
+
})
|
|
610
|
+
}
|
package/tests/inbound.test.mjs
CHANGED
|
@@ -7,7 +7,9 @@ import crypto from 'node:crypto'
|
|
|
7
7
|
import sharp from 'sharp'
|
|
8
8
|
|
|
9
9
|
const home = fs.mkdtempSync(path.join(os.tmpdir(), 'dsh-memu-memory-inbound-'))
|
|
10
|
+
process.env.DSH_HOME = home
|
|
10
11
|
process.env.DSH_MEMU_HOME = home
|
|
12
|
+
process.env.DSH_MEMU_MEMORY_HOME = home
|
|
11
13
|
const state = await import('../lib/state.js')
|
|
12
14
|
const inbound = await import('../lib/inbound.js')
|
|
13
15
|
const inject = await import('../lib/inject.js')
|
|
@@ -32,8 +34,20 @@ function result(callId, value, meta) {
|
|
|
32
34
|
return { type: 'tool/result', data: { turn: 1, step: 2, message: { role: 'tool', source: { kind: 'tool', callId }, toolCallId: callId,
|
|
33
35
|
content: [{ type: 'text', text: typeof value === 'string' ? value : JSON.stringify(value) }] }, ...(meta ? { meta } : {}) } }
|
|
34
36
|
}
|
|
37
|
+
const admissionHandlers = new Map()
|
|
38
|
+
inject.registerInjection({ on: (name, handler) => { admissionHandlers.set(name, handler); return () => {} }, logger: { warn() {} } })
|
|
39
|
+
async function accept(f, out) {
|
|
40
|
+
const history = f.session.messages ||= []
|
|
41
|
+
for (const message of out.messages || []) {
|
|
42
|
+
if (history.some(item => item.id === message.id)) continue
|
|
43
|
+
history.push(message)
|
|
44
|
+
if (message.role === 'user') await admissionHandlers.get('session/event')(f.session, { type: 'user/message', data: message })
|
|
45
|
+
}
|
|
46
|
+
return out
|
|
47
|
+
}
|
|
35
48
|
async function continueStep(f, step = 2, options = {}) {
|
|
36
|
-
|
|
49
|
+
const out = await inject.injectForStep({ agent: f.agent, decision: { kind: 'enter', messages: [] }, turn: 1, step, ...options })
|
|
50
|
+
return accept(f, out)
|
|
37
51
|
}
|
|
38
52
|
function textOf(decision) { return decision.messages.flatMap(message => message.content || []).map(block => block.text || '').join('\n') }
|
|
39
53
|
async function saved(f, title = '部署脚本') {
|
|
@@ -155,7 +169,7 @@ test('real user inbox messages admitted on a later step work without a tool adap
|
|
|
155
169
|
const entry = await saved(f)
|
|
156
170
|
const user = { id: 'late-user', role: 'user', source: { kind: 'user' }, content: [{ type: 'text', text: '部署脚本在哪里' }] }
|
|
157
171
|
const decision = { kind: 'enter', messages: [user] }
|
|
158
|
-
const out = await inject.injectForStep({ agent: f.agent, turn: 1, step: 7, decision })
|
|
172
|
+
const out = await accept(f, await inject.injectForStep({ agent: f.agent, turn: 1, step: 7, decision }))
|
|
159
173
|
assert.equal(out.messages[0], user, 'do not rewrite or disguise the producer message')
|
|
160
174
|
assert.ok(textOf(out).includes(entry.id))
|
|
161
175
|
const again = await inject.injectForStep({ agent: f.agent, turn: 1, step: 8, decision })
|
|
@@ -225,7 +239,8 @@ test('disabled memory does not capture image bytes or inject on continuation ste
|
|
|
225
239
|
} }] }) })
|
|
226
240
|
assert.deepEqual((await continueStep(f, 2, { ctx })).messages, [])
|
|
227
241
|
assert.equal(reads, 0)
|
|
228
|
-
assert.equal(state.getSessionBinding(f.id).inbound.pending.length,
|
|
242
|
+
assert.equal(state.getSessionBinding(f.id).inbound.pending.length, 1, 'disabled memory is not Native input acceptance')
|
|
243
|
+
assert.deepEqual(f.session.messages, [], 'nothing entered history')
|
|
229
244
|
})
|
|
230
245
|
|
|
231
246
|
test('retrieval errors stay fail-open, preserve the batch and use a retry delay', async () => {
|
|
@@ -300,7 +315,8 @@ test('messages received during media draining stay queued for the next admission
|
|
|
300
315
|
await first
|
|
301
316
|
assert.deepEqual(state.getSessionBinding(f.id).inbound.pending.map(item => item.id), ['later-text'])
|
|
302
317
|
await continueStep(f, 3, { ctx })
|
|
303
|
-
assert.equal(state.getSessionBinding(f.id).inbound.pending.length,
|
|
318
|
+
assert.equal(state.getSessionBinding(f.id).inbound.pending.length, 1, 'no-match is not evidence of input acceptance')
|
|
319
|
+
assert.ok(!f.session.messages.some(message => message.source?.kind === 'user' && message.id === 'later-text'))
|
|
304
320
|
})
|
|
305
321
|
|
|
306
322
|
test('restored pending batches drain on a first step even with no native user message', async () => {
|
|
@@ -394,3 +410,32 @@ test('switching libraries while image collection waits cannot publish old-batch
|
|
|
394
410
|
assert.deepEqual((await continueStep(f, 3, { ctx })).messages, [])
|
|
395
411
|
assert.equal(state.getSessionBinding(f.id).lastInboundOutcome.reason, 'scope')
|
|
396
412
|
})
|
|
413
|
+
|
|
414
|
+
test('no-match handling waits for exact Native human admission rather than clearing a proposed inbox', async () => {
|
|
415
|
+
const f = fixture('no-match-native')
|
|
416
|
+
state.setSessionBinding(f.id, { useGlobal: false })
|
|
417
|
+
const human = { id: 'empty-match', role: 'user', source: { kind: 'user' }, content: [{ type: 'text', text: 'nothing matches this empty library' }] }
|
|
418
|
+
const decision = { kind: 'enter', messages: [human] }
|
|
419
|
+
const out = await inject.injectForStep({ agent: f.agent, turn: 1, step: 2, decision })
|
|
420
|
+
assert.equal(out, decision)
|
|
421
|
+
assert.equal(state.getSessionBinding(f.id).inbound.pending.length, 1)
|
|
422
|
+
assert.deepEqual(f.session.messages || [], [], 'a returned enter decision is not history')
|
|
423
|
+
inbound.noteNativeUserMessages(f.id, [human])
|
|
424
|
+
assert.equal(state.getSessionBinding(f.id).inbound.pending.length, 1, 'no admission flag is not proof')
|
|
425
|
+
await accept(f, out)
|
|
426
|
+
assert.equal(state.getSessionBinding(f.id).inbound.pending.length, 0, 'the actual append notification acknowledges no-match input')
|
|
427
|
+
})
|
|
428
|
+
|
|
429
|
+
test('unaccepted external no-match batches stay queued and retries do not move their logical clock', async () => {
|
|
430
|
+
const f = fixture('no-match-external')
|
|
431
|
+
state.setSessionBinding(f.id, { useGlobal: false })
|
|
432
|
+
inbound.ingestInboundBatch({ sessionId: f.id, batch: batch('unmatched-external', 'nothing matches this empty library') })
|
|
433
|
+
const out = await continueStep(f, 2)
|
|
434
|
+
assert.deepEqual(out.messages, [])
|
|
435
|
+
assert.deepEqual(f.session.messages, [])
|
|
436
|
+
const before = state.getSessionBinding(f.id)
|
|
437
|
+
assert.equal(before.inbound.pending.length, 1)
|
|
438
|
+
await continueStep(f, 3)
|
|
439
|
+
assert.equal(state.getSessionBinding(f.id).inbound.pending.length, 1)
|
|
440
|
+
assert.equal(state.getSessionBinding(f.id).inbound.clock, before.inbound.clock)
|
|
441
|
+
})
|