dsh-memu-memory 1.2.5 → 1.2.7
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 +3 -1
- package/README.md +3 -3
- package/README.zh.md +3 -3
- package/docs/contributing.md +1 -1
- package/docs/env-and-commands.md +1 -0
- package/docs/host-adaptation.md +318 -0
- package/docs/host-api.md +29 -5
- package/docs/memory-semantics.md +12 -7
- package/docs/troubleshooting.md +15 -9
- package/lib/binding-diagnostics.js +2 -2
- package/lib/client.js +291 -37
- package/lib/consolidation-ledger.js +46 -1
- package/lib/consolidation-worker.js +1 -0
- package/lib/curator-policy.js +98 -0
- package/lib/cycle.js +51 -17
- package/lib/fork-model.js +312 -0
- package/lib/index.js +8 -0
- package/lib/memory.js +36 -12
- package/lib/memu.js +318 -3
- package/lib/private.js +352 -70
- package/lib/prompts/private-consolidation.mixed.md +1 -0
- package/lib/prompts/private-consolidation.rp.md +1 -0
- package/lib/prompts/private-policy.mixed.md +1 -0
- package/lib/prompts/private-policy.rp.md +1 -1
- package/lib/recall-cache.js +204 -0
- package/lib/rpc.js +48 -7
- package/lib/salient-wake.js +38 -2
- package/lib/state.js +46 -6
- package/lib/text-retrieval.js +221 -28
- package/lib/tokenizer-v2.js +295 -0
- package/lib/webui/standalone.css +17 -1
- package/package.json +1 -1
- package/scripts/bench-recall.mjs +142 -0
- package/scripts/check-seed-compat.mjs +118 -0
- package/tests/absorb-review.test.mjs +2 -1
- package/tests/backend-compensation.test.mjs +1 -1
- package/tests/consolidation-fork.test.mjs +199 -7
- package/tests/curator-absorb.test.mjs +2 -1
- package/tests/curator-clusters.test.mjs +9 -2
- package/tests/curator-commit.test.mjs +2 -1
- package/tests/curator-link.test.mjs +2 -1
- package/tests/curator-pairing.test.mjs +52 -8
- package/tests/curator-review-mode.test.mjs +2 -1
- package/tests/cycle.test.mjs +2 -2
- package/tests/dynamic-rrf.test.mjs +179 -0
- package/tests/fork-model.test.mjs +345 -0
- package/tests/loader.mjs +3 -0
- package/tests/managed-memory.test.mjs +1 -1
- package/tests/negation-polarity.test.mjs +135 -0
- package/tests/recall-cache.test.mjs +290 -0
- package/tests/recall-dedup.test.mjs +1 -1
- package/tests/salient-wake.test.mjs +28 -1
- package/tests/stubs/dsh-llm-pi-ai.js +13 -0
- package/tests/stubs/dsh-schedule.js +35 -0
- package/tests/tokenizer-v2.test.mjs +142 -0
- package/tests/ui-defaults.test.mjs +1 -1
|
@@ -22,7 +22,8 @@ globalThis.__curatorLinkTest = {
|
|
|
22
22
|
}
|
|
23
23
|
const memuSource = 'const bridge = globalThis.__curatorLinkTest;'
|
|
24
24
|
+ 'export const commit = (args) => bridge.commit(args), forget = (args) => bridge.forget(args),'
|
|
25
|
-
+ 'retrieve = (args) => bridge.retrieve(args), listFiles = (args) => bridge.listFiles(args)
|
|
25
|
+
+ 'retrieve = (args) => bridge.retrieve(args), listFiles = (args) => bridge.listFiles(args),'
|
|
26
|
+
+ 'closeMemuResident = () => 0, prewarmMemuResident = async () => false;'
|
|
26
27
|
const hooks = registerHooks({
|
|
27
28
|
resolve(specifier, context, nextResolve) {
|
|
28
29
|
if (specifier === './memu.js') return { url: 'data:text/javascript,' + encodeURIComponent(memuSource), shortCircuit: true }
|
|
@@ -22,8 +22,16 @@ test('tokenize: latin runs of >=2 chars keep ._ - joins, single chars drop', ()
|
|
|
22
22
|
assert.deepEqual(tokenize('per-turn retry'), ['per-turn', 'retry'])
|
|
23
23
|
})
|
|
24
24
|
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
// P0-1 双轨切换(TOKENIZER_V2=true 默认):CJK 从「只出 bigram」升级为
|
|
26
|
+
// unigram+bigram+trigram 三层,4 字 run 另收完整串——断言按新口径更新,
|
|
27
|
+
// 「孤立单字保留、跨标点 run 独立」的意图不变。
|
|
28
|
+
test('tokenize: CJK runs become layered n-grams (V2), isolated chars stay single', () => {
|
|
29
|
+
assert.deepEqual(tokenize('部署窗口'), [
|
|
30
|
+
'部', '署', '窗', '口',
|
|
31
|
+
'部署', '署窗', '窗口',
|
|
32
|
+
'部署窗', '署窗口',
|
|
33
|
+
'部署窗口',
|
|
34
|
+
])
|
|
27
35
|
assert.deepEqual(tokenize('猫'), ['猫'])
|
|
28
36
|
// two CJK runs separated by latin/punct stay separate runs
|
|
29
37
|
assert.deepEqual(tokenize('猫。狗'), ['猫', '狗'])
|
|
@@ -31,7 +39,8 @@ test('tokenize: CJK runs become overlapping bigrams, isolated chars stay single'
|
|
|
31
39
|
|
|
32
40
|
test('tokenize: NFKC folds full-width forms, lowercases, dedupes', () => {
|
|
33
41
|
assert.deepEqual(tokenize('RISCV CACHE CACHE'), ['riscv', 'cache'])
|
|
34
|
-
|
|
42
|
+
// V2 去重后仍只保留一次出现;unigram 是新增层('缓存' bigram 依旧在)
|
|
43
|
+
assert.deepEqual(tokenize('缓存 缓存'), ['缓', '存', '缓存'])
|
|
35
44
|
})
|
|
36
45
|
|
|
37
46
|
// —— §4.1 buildCorpus + bm25 ——
|
|
@@ -173,7 +182,7 @@ function bigCorpus({ withTarget = true } = {}) {
|
|
|
173
182
|
if (withTarget) {
|
|
174
183
|
entries.push({ id: 'target', track: 'memory', domain: 'general', title: '缓存过期', content: 'Redis 缓存统一设置 30 秒过期,命中直接返回,不做全量。', appliesTo: ['deploy'], updatedAt: '2026-01-02T00:00:00.000Z' })
|
|
175
184
|
entries.push({ id: 'title-only', track: 'memory', domain: 'general', title: '缓存过期', content: '完全不同的词汇组织起来的另一段内容。', updatedAt: '2026-01-03T00:00:00.000Z' })
|
|
176
|
-
entries.push({ id: 'single-token', track: 'memory', domain: 'elsewhere', title: '
|
|
185
|
+
entries.push({ id: 'single-token', track: 'memory', domain: 'elsewhere', title: '咖啡豆', content: 'sync 每周补货一次。', updatedAt: '2026-01-04T00:00:00.000Z' })
|
|
177
186
|
}
|
|
178
187
|
return buildCorpus(entries)
|
|
179
188
|
}
|
|
@@ -192,18 +201,20 @@ test('rankPairCandidates: blocking finds the shared-token target in a >300 corpu
|
|
|
192
201
|
|
|
193
202
|
test('rankPairCandidates: exact-title entries enter even without shared tokens', () => {
|
|
194
203
|
const corpus = bigCorpus()
|
|
195
|
-
const proposal = { id: 'p', track: 'memory', domain: 'general', title: '缓存过期', content: '
|
|
204
|
+
const proposal = { id: 'p', track: 'memory', domain: 'general', title: '缓存过期', content: '完全无重叠词元的新内容组合安排 sync。', appliesTo: [] }
|
|
196
205
|
const found = rankPairCandidates({ corpus, proposal, limit: 20 })
|
|
197
206
|
assert.ok(found.some((c) => c.id === 'title-only'))
|
|
198
207
|
})
|
|
199
208
|
|
|
200
209
|
test('rankPairCandidates: a one-shared-token candidate outside the proposal domain stays out', () => {
|
|
201
210
|
const corpus = bigCorpus()
|
|
202
|
-
const proposal = { id: 'p', track: 'memory', domain: 'general', title: '缓存过期', content: '
|
|
211
|
+
const proposal = { id: 'p', track: 'memory', domain: 'general', title: '缓存过期', content: '完全无重叠词元的新内容组合安排 sync。', appliesTo: [] }
|
|
203
212
|
const found = rankPairCandidates({ corpus, proposal, limit: 20 })
|
|
204
|
-
// 'single-token' shares exactly one rare token (
|
|
213
|
+
// 'single-token' shares exactly one rare token (sync) with the proposal and
|
|
205
214
|
// sits in a different domain: blocking needs >=2 shared tokens and the
|
|
206
|
-
// same-domain backfill never reaches a foreign domain.
|
|
215
|
+
// same-domain backfill never reaches a foreign domain. 共享词刻意选 latin——
|
|
216
|
+
// V2 分词会把 CJK 共享词拆出 unigram(安排 → 安排+安+排),latin token 不拆,
|
|
217
|
+
// 「只共享一个词不进候选」的意图在两种分词口径下都成立。
|
|
207
218
|
assert.ok(!found.some((c) => c.id === 'single-token'))
|
|
208
219
|
})
|
|
209
220
|
|
|
@@ -227,6 +238,39 @@ test('rankPairCandidates: deterministic — same input, same output', () => {
|
|
|
227
238
|
assert.deepEqual(run1, run2)
|
|
228
239
|
})
|
|
229
240
|
|
|
241
|
+
// —— §4.3 边界带(P0-1 回归修复的守卫):代理切分 → pairScore 真尺终选 ——
|
|
242
|
+
//
|
|
243
|
+
// 手算秩结构(可复现 fixture,文本两两相同造成 bm25/dice 精确并列,en 路由的
|
|
244
|
+
// containment tiebreak 由 cand-c/d 的逐字包含 0.92 垫高):
|
|
245
|
+
// cand-a: bm1, di1, en4 → rrf = 0.5/61 + 0.3/61 + 0.2/64 = 1.6240e-2(rank 1,切点)
|
|
246
|
+
// cand-b: bm2, di2, en1 → rrf = 0.5/62 + 0.3/62 + 0.2/61 = 1.6183e-2(rank 2,带内:
|
|
247
|
+
// 与切点差 (0.5+0.3)×(1/61−1/62) − 0.2×(1/61−1/64) = 5.78e-5 ≤ RRF_SLICE_BAND)
|
|
248
|
+
// cand-c/d: rrf 更低,带外(仅作 en 路由 containment 垫脚)。
|
|
249
|
+
// pairScore:cand-b 0.9894(entity 匹配)≫ cand-a 0.8394(appliesTo 不相交)。
|
|
250
|
+
// 修复前 pre-cut=limit=1 只评分 cand-a,返回 ['cand-a'];边界带把 cand-b 并入
|
|
251
|
+
// 评分集,真尺终选翻到 cand-b——准入从 RRF 代理换成 pairScore 真尺的直接证据。
|
|
252
|
+
test('rankPairCandidates: the slice boundary band admits a better-pairScore candidate the RRF proxy dropped', () => {
|
|
253
|
+
const twin = 'Redis 缓存统一设置 30 秒过期,命中直接返回。Redis 缓存统一设置 30 秒过期,命中直接返回。'
|
|
254
|
+
const tail = '。另有运维值班轮换表与告警升级路径的完整说明,这里补足长度让长度剪枝生效并保持逐字包含。'
|
|
255
|
+
const proposal = { title: '缓存过期', content: 'Redis 缓存统一设置 30 秒过期,命中直接返回。', appliesTo: ['deploy'], track: 'memory', domain: 'general' }
|
|
256
|
+
const corpus = buildCorpus([
|
|
257
|
+
{ id: 'cand-a', title: '缓存过期', content: twin, appliesTo: [], track: 'memory', domain: 'general' },
|
|
258
|
+
{ id: 'cand-b', title: '缓存过期', content: twin, appliesTo: ['deploy'], track: 'memory', domain: 'general' },
|
|
259
|
+
{ id: 'cand-c', title: '缓存过期', content: proposal.content + tail, appliesTo: [], track: 'memory', domain: 'general' },
|
|
260
|
+
{ id: 'cand-d', title: '缓存过期', content: proposal.content + tail, appliesTo: [], track: 'memory', domain: 'general' },
|
|
261
|
+
])
|
|
262
|
+
const found = rankPairCandidates({ corpus, proposal, limit: 1 })
|
|
263
|
+
// 真尺终选:数量语义不变(≤max),赢家是带内 rescue 的高 pairScore 候选
|
|
264
|
+
assert.equal(found.length, 1)
|
|
265
|
+
assert.equal(found[0].id, 'cand-b')
|
|
266
|
+
// 赢家的 rrf 严格低于切点(cand-a 的手算 rrf)——证明它来自边界带而非 pre-cut
|
|
267
|
+
const cutRrf = 0.5 / (60 + 1) + 0.3 / (60 + 1) + 0.2 / (60 + 4)
|
|
268
|
+
assert.ok(found[0].rrf < cutRrf, `winner rrf ${found[0].rrf} must sit below the pre-cut line ${cutRrf}`)
|
|
269
|
+
// 被切掉的 cand-a 在真尺上确实更差(低 pairScore 者按 pairScore 出局)
|
|
270
|
+
const loserScore = pairScore(pairFeatures(proposal, corpus.docs.get('cand-a').entry))
|
|
271
|
+
assert.ok(found[0].score > loserScore, `band winner ${found[0].score} must beat the displaced candidate ${loserScore}`)
|
|
272
|
+
})
|
|
273
|
+
|
|
230
274
|
// —— §4.8 全局冲突预筛 ——
|
|
231
275
|
|
|
232
276
|
test('globalConflictCheck: same title + different body is same_title; negation flips conflict', () => {
|
|
@@ -32,7 +32,8 @@ globalThis.__curatorReviewTest = {
|
|
|
32
32
|
|
|
33
33
|
const memuSource = 'const bridge = globalThis.__curatorReviewTest;'
|
|
34
34
|
+ 'export const commit = (args) => bridge.commit(args), forget = (args) => bridge.forget(args),'
|
|
35
|
-
+ 'retrieve = (args) => bridge.retrieve(args), listFiles = (args) => bridge.listFiles(args)
|
|
35
|
+
+ 'retrieve = (args) => bridge.retrieve(args), listFiles = (args) => bridge.listFiles(args),'
|
|
36
|
+
+ 'closeMemuResident = () => 0, prewarmMemuResident = async () => false;'
|
|
36
37
|
const hooks = registerHooks({
|
|
37
38
|
resolve(specifier, context, nextResolve) {
|
|
38
39
|
if (specifier === './memu.js') return { url: 'data:text/javascript,' + encodeURIComponent(memuSource), shortCircuit: true }
|
package/tests/cycle.test.mjs
CHANGED
|
@@ -224,7 +224,7 @@ test('a conversation the host says is gone is marked once and never retried', as
|
|
|
224
224
|
assert.equal(binding.sleepRetryAt, null, 'nothing to retry later')
|
|
225
225
|
assert.ok(logs.some((m) => /no longer exists/.test(m)), 'explained once: ' + logs.join(' | '))
|
|
226
226
|
const quiet = await cycle.runSleepPass(fakeCtx({ live: ['cycle-ghost'] }))
|
|
227
|
-
assert.equal(quiet.
|
|
227
|
+
assert.equal(quiet.ran, 0, 'the next sweep does not consolidate a deleted session again')
|
|
228
228
|
})
|
|
229
229
|
|
|
230
230
|
test('a policy greeting skip does not claim a completed worker check', async () => {
|
|
@@ -238,7 +238,7 @@ test('a policy greeting skip does not claim a completed worker check', async ()
|
|
|
238
238
|
const out = await cycle.runSleepPass(ctx)
|
|
239
239
|
assert.equal(out.ran, 0, 'a policy skip is not a verified worker receipt')
|
|
240
240
|
assert.equal(ctx.created.length, 0, 'no fork for small talk')
|
|
241
|
-
assert.
|
|
241
|
+
assert.ok(state.getLastSleepAt('cycle-smalltalk'), 'a terminal skip is recorded so the same greetings are not re-read every sweep')
|
|
242
242
|
})
|
|
243
243
|
|
|
244
244
|
test('auto curation stays off unless an interval is set', async () => {
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import test from 'node:test'
|
|
2
|
+
import assert from 'node:assert/strict'
|
|
3
|
+
import {
|
|
4
|
+
RRF_DYNAMIC,
|
|
5
|
+
RRF_K,
|
|
6
|
+
RRF_W_BM25,
|
|
7
|
+
RRF_W_DICE,
|
|
8
|
+
RRF_W_ENTITY,
|
|
9
|
+
RECENCY_TERMS,
|
|
10
|
+
computeRRFWeights,
|
|
11
|
+
} from '../lib/curator-policy.js'
|
|
12
|
+
import { buildCorpus, rankPairCandidates } from '../lib/text-retrieval.js'
|
|
13
|
+
|
|
14
|
+
// —— 召回升级 P0-2:动态 RRF 权重 ——
|
|
15
|
+
// 权重数字单一来源 lib/curator-policy.js;开关 RRF_DYNAMIC 恒为 const,
|
|
16
|
+
// ESM 不可运行时翻转,因此「开关关闭 = 静态路径」的等效性用两条证据覆盖:
|
|
17
|
+
// (1) computeRRFWeights 对普通查询精确返回静态表常量(动态路径退化为静态);
|
|
18
|
+
// (2) rankPairCandidates 在普通查询下产出的 rrf 与静态三路公式逐位相等。
|
|
19
|
+
|
|
20
|
+
const STATIC_WEIGHTS = { bm25: RRF_W_BM25, dice: RRF_W_DICE, entity: RRF_W_ENTITY }
|
|
21
|
+
|
|
22
|
+
function sumOf(weights) {
|
|
23
|
+
return Object.values(weights).reduce((a, b) => a + b, 0)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// —— computeRRFWeights:权重形状 ——
|
|
27
|
+
|
|
28
|
+
test('近期查询返回含 recency 的权重组且和为 1(设计稿 §4.2 数值)', () => {
|
|
29
|
+
const weights = computeRRFWeights({ title: '最近的部署', content: '昨天更新了缓存策略' }, { size: 3 })
|
|
30
|
+
assert.deepEqual(weights, { bm25: 0.3, dice: 0.3, entity: 0.2, recency: 0.2 })
|
|
31
|
+
assert.ok(Math.abs(sumOf(weights) - 1) < 1e-9)
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
test('近期词典(中英)逐词都能触发 recency 通道,且不依赖分词开关口径', () => {
|
|
35
|
+
for (const term of RECENCY_TERMS) {
|
|
36
|
+
const weights = computeRRFWeights({ title: term, content: '其他内容' }, { size: 3 })
|
|
37
|
+
assert.ok(weights.recency != null, `词条 ${term} 必须触发 recency`)
|
|
38
|
+
assert.ok(Math.abs(sumOf(weights) - 1) < 1e-9, `词条 ${term} 权重和必须为 1`)
|
|
39
|
+
}
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
test('coding 域 entity 提权:×1.5 后归一化,entity 追平 dice、bm25 降权', () => {
|
|
43
|
+
const weights = computeRRFWeights({ title: '部署脚本', content: 'redis 配置调整', domain: 'coding' }, { size: 3 })
|
|
44
|
+
assert.ok(weights.entity > 0.25, `entity 应从 0.2 提到 ~0.2727,got ${weights.entity}`)
|
|
45
|
+
assert.ok(Math.abs(weights.entity - weights.dice) < 1e-9, 'entity 与 dice 同为 0.3/1.1')
|
|
46
|
+
assert.ok(weights.bm25 < RRF_W_BM25, '归一化后 bm25 低于静态值')
|
|
47
|
+
assert.ok(weights.recency == null, '非近期查询不加第四路')
|
|
48
|
+
assert.ok(Math.abs(sumOf(weights) - 1) < 1e-9)
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
test('近期 + coding 叠加:四路权重 clamp/归一化后和仍为 1', () => {
|
|
52
|
+
const weights = computeRRFWeights({ title: '最近的部署脚本', content: '昨天改了配置', domain: 'coding' }, { size: 3 })
|
|
53
|
+
assert.ok(weights.recency != null)
|
|
54
|
+
for (const value of Object.values(weights)) {
|
|
55
|
+
assert.ok(value >= 0.05 - 1e-12 && value <= 0.6 + 1e-12, `每个权重落在 [0.05, 0.6],got ${value}`)
|
|
56
|
+
}
|
|
57
|
+
assert.ok(Math.abs(sumOf(weights) - 1) < 1e-9)
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
test('普通查询回退静态权重:精确等于常量表三路,无 recency 键', () => {
|
|
61
|
+
const weights = computeRRFWeights({ title: '缓存策略', content: 'Redis 缓存统一 30 秒过期' }, { size: 3 })
|
|
62
|
+
assert.deepEqual(weights, STATIC_WEIGHTS)
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
// —— computeRRFWeights:异常输入 fallback(风险表:权重边界 + fallback)——
|
|
66
|
+
|
|
67
|
+
test('异常输入 fallback:proposal/corpus 非对象或字段不可分词都回静态', () => {
|
|
68
|
+
const corpus = { size: 3 }
|
|
69
|
+
assert.deepEqual(computeRRFWeights(null, corpus), STATIC_WEIGHTS)
|
|
70
|
+
assert.deepEqual(computeRRFWeights(undefined, corpus), STATIC_WEIGHTS)
|
|
71
|
+
assert.deepEqual(computeRRFWeights(42, corpus), STATIC_WEIGHTS)
|
|
72
|
+
assert.deepEqual(computeRRFWeights('x', corpus), STATIC_WEIGHTS)
|
|
73
|
+
assert.deepEqual(computeRRFWeights({ title: '缓存' }, null), STATIC_WEIGHTS)
|
|
74
|
+
assert.deepEqual(computeRRFWeights({ title: '缓存' }, undefined), STATIC_WEIGHTS)
|
|
75
|
+
// 读取字段时抛错(getter 异常)也不往上传,按 fallback 处理
|
|
76
|
+
assert.deepEqual(computeRRFWeights({ get title() { throw new Error('bad') } }, corpus), STATIC_WEIGHTS)
|
|
77
|
+
// 近期词在 content 里同样能触发,但异常 proposal 压过一切
|
|
78
|
+
assert.deepEqual(computeRRFWeights(null, { title: '最近的部署' }), STATIC_WEIGHTS)
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
test('纯函数:同输入同输出,返回对象是独立副本(改写不影响下次调用)', () => {
|
|
82
|
+
const proposal = { title: '最近的部署', content: '昨天更新了缓存策略' }
|
|
83
|
+
const corpus = { size: 3 }
|
|
84
|
+
const a = computeRRFWeights(proposal, corpus)
|
|
85
|
+
const b = computeRRFWeights(proposal, corpus)
|
|
86
|
+
assert.deepEqual(a, b)
|
|
87
|
+
a.bm25 = 999
|
|
88
|
+
a.recency = 999
|
|
89
|
+
assert.deepEqual(computeRRFWeights(proposal, corpus), b)
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
// —— rankPairCandidates:动态路径与静态路径的等效与提升 ——
|
|
93
|
+
|
|
94
|
+
function equivalenceCorpus() {
|
|
95
|
+
return buildCorpus([
|
|
96
|
+
{ id: 'doc-1', track: 'memory', domain: 'general', title: '缓存过期', content: 'Redis 缓存统一设置 30 秒过期,命中直接返回。', appliesTo: ['deploy'], updatedAt: '2026-01-01T00:00:00.000Z' },
|
|
97
|
+
{ id: 'doc-2', track: 'memory', domain: 'general', title: '咖啡拿铁', content: '用户下午只喝美式咖啡。', appliesTo: ['coffee'], updatedAt: '2026-01-02T00:00:00.000Z' },
|
|
98
|
+
])
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
test('开关关闭等效:普通查询下 rrf 与静态三路公式逐位相等(权重无 recency 时动态路径退化为静态)', () => {
|
|
102
|
+
const corpus = equivalenceCorpus()
|
|
103
|
+
const proposal = { id: 'p', track: 'memory', domain: 'general', title: '缓存过期', content: 'Redis 缓存统一设置 30 秒过期,命中直接返回。', appliesTo: ['deploy'] }
|
|
104
|
+
const found = rankPairCandidates({ corpus, proposal, limit: 20 })
|
|
105
|
+
assert.equal(found.length, 2)
|
|
106
|
+
// doc-1 文本与 proposal 全同 → 三路都是 rank 1;doc-2 全不相干 → 三路都是 rank 2
|
|
107
|
+
assert.equal(found[0].id, 'doc-1')
|
|
108
|
+
assert.equal(found[1].id, 'doc-2')
|
|
109
|
+
// 与「开关关」分支同一表达式、同一权重常量 → 严格相等
|
|
110
|
+
assert.equal(found[0].rrf, RRF_W_BM25 / (RRF_K + 1) + RRF_W_DICE / (RRF_K + 1) + RRF_W_ENTITY / (RRF_K + 1))
|
|
111
|
+
assert.equal(found[1].rrf, RRF_W_BM25 / (RRF_K + 2) + RRF_W_DICE / (RRF_K + 2) + RRF_W_ENTITY / (RRF_K + 2))
|
|
112
|
+
assert.equal(RRF_DYNAMIC, true, '本测试锚定开关当前默认值:开 + 静态权重 ≡ 关')
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
function recentCorpus() {
|
|
116
|
+
return buildCorpus([
|
|
117
|
+
{ id: 'a-old', track: 'memory', domain: 'general', title: '缓存过期', content: '缓存过期提醒每日检查', appliesTo: [], updatedAt: '2026-01-01T00:00:00.000Z' },
|
|
118
|
+
{ id: 'b-new', track: 'memory', domain: 'general', title: '缓存过期', content: '缓存过期提醒每日检查', appliesTo: [], updatedAt: '2026-06-01T00:00:00.000Z' },
|
|
119
|
+
])
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
test('recency 查询下 updatedAt 更新的候选相对提升:rrf 差距收窄、新者增益为正、旧者为负', () => {
|
|
123
|
+
const corpus = recentCorpus()
|
|
124
|
+
const proposal = { track: 'memory', domain: 'general', title: '缓存过期', content: '昨天 缓存过期提醒每日检查' }
|
|
125
|
+
const found = rankPairCandidates({ corpus, proposal, limit: 20 })
|
|
126
|
+
assert.equal(found.length, 2)
|
|
127
|
+
// 文本全同 → 三路 tie 由 idAsc 定序:a-old 三路 rank1、b-new rank2;
|
|
128
|
+
// recency 通道按 updatedAt 降序:b-new rank1、a-old rank2。
|
|
129
|
+
const rrfOld = found.find((c) => c.id === 'a-old').rrf
|
|
130
|
+
const rrfNew = found.find((c) => c.id === 'b-new').rrf
|
|
131
|
+
// 静态口径的期望值(同一表达式、rank 1/2)
|
|
132
|
+
const statOld = RRF_W_BM25 / (RRF_K + 1) + RRF_W_DICE / (RRF_K + 1) + RRF_W_ENTITY / (RRF_K + 1)
|
|
133
|
+
const statNew = RRF_W_BM25 / (RRF_K + 2) + RRF_W_DICE / (RRF_K + 2) + RRF_W_ENTITY / (RRF_K + 2)
|
|
134
|
+
// 新者的 rrf 相对静态严格提升,旧者严格下降 → 相对排名提升
|
|
135
|
+
assert.ok(rrfNew > statNew, `新者应增益:${rrfNew} > ${statNew}`)
|
|
136
|
+
assert.ok(rrfOld < statOld, `旧者应减益:${rrfOld} < ${statOld}`)
|
|
137
|
+
// 两者差距收窄(0.8δ → 0.6δ),即 recency 把新者往前拉
|
|
138
|
+
assert.ok(rrfOld - rrfNew < statOld - statNew)
|
|
139
|
+
// recency 权重不足以翻转文本三路的合计优势(0.6δ > 0),排序仍稳定
|
|
140
|
+
assert.equal(found[0].id, 'a-old')
|
|
141
|
+
assert.equal(found[1].id, 'b-new')
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
test('非近期查询下同一 corpus 不受 recency 影响:rrf 与静态公式逐位相等', () => {
|
|
145
|
+
const corpus = recentCorpus()
|
|
146
|
+
const proposal = { track: 'memory', domain: 'general', title: '缓存过期', content: '缓存过期提醒每日检查' }
|
|
147
|
+
const found = rankPairCandidates({ corpus, proposal, limit: 20 })
|
|
148
|
+
assert.equal(found[0].rrf, RRF_W_BM25 / (RRF_K + 1) + RRF_W_DICE / (RRF_K + 1) + RRF_W_ENTITY / (RRF_K + 1))
|
|
149
|
+
assert.equal(found[1].rrf, RRF_W_BM25 / (RRF_K + 2) + RRF_W_DICE / (RRF_K + 2) + RRF_W_ENTITY / (RRF_K + 2))
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
test('结果确定性:同输入两次 rankPairCandidates / computeRRFWeights 完全一致', () => {
|
|
153
|
+
const corpus = recentCorpus()
|
|
154
|
+
const proposal = { track: 'memory', domain: 'general', title: '缓存过期', content: '昨天 缓存过期提醒每日检查' }
|
|
155
|
+
assert.deepEqual(rankPairCandidates({ corpus, proposal, limit: 20 }), rankPairCandidates({ corpus, proposal, limit: 20 }))
|
|
156
|
+
const big = equivalenceCorpus()
|
|
157
|
+
const bigProposal = { id: 'p', track: 'memory', domain: 'general', title: '缓存过期', content: 'Redis 缓存统一设置 30 秒过期,命中直接返回。', appliesTo: ['deploy'] }
|
|
158
|
+
assert.deepEqual(rankPairCandidates({ corpus: big, proposal: bigProposal, limit: 20 }), rankPairCandidates({ corpus: big, proposal: bigProposal, limit: 20 }))
|
|
159
|
+
})
|
|
160
|
+
|
|
161
|
+
test('结果确定性(边界带开启):切点附近有带内候选时同输入两次 run 完全一致', () => {
|
|
162
|
+
// 与 tests/curator-pairing.test.mjs 的边界带守卫同一 fixture:cand-a/cand-b
|
|
163
|
+
// rrf 差 5.78e-5,limit=1 时 band 把 cand-b 并入评分集——这个新增分支同样
|
|
164
|
+
// 必须完全确定。
|
|
165
|
+
const twin = 'Redis 缓存统一设置 30 秒过期,命中直接返回。Redis 缓存统一设置 30 秒过期,命中直接返回。'
|
|
166
|
+
const tail = '。另有运维值班轮换表与告警升级路径的完整说明,这里补足长度让长度剪枝生效并保持逐字包含。'
|
|
167
|
+
const proposal = { title: '缓存过期', content: 'Redis 缓存统一设置 30 秒过期,命中直接返回。', appliesTo: ['deploy'], track: 'memory', domain: 'general' }
|
|
168
|
+
const corpus = buildCorpus([
|
|
169
|
+
{ id: 'cand-a', title: '缓存过期', content: twin, appliesTo: [], track: 'memory', domain: 'general' },
|
|
170
|
+
{ id: 'cand-b', title: '缓存过期', content: twin, appliesTo: ['deploy'], track: 'memory', domain: 'general' },
|
|
171
|
+
{ id: 'cand-c', title: '缓存过期', content: proposal.content + tail, appliesTo: [], track: 'memory', domain: 'general' },
|
|
172
|
+
{ id: 'cand-d', title: '缓存过期', content: proposal.content + tail, appliesTo: [], track: 'memory', domain: 'general' },
|
|
173
|
+
])
|
|
174
|
+
const run1 = rankPairCandidates({ corpus, proposal, limit: 1 })
|
|
175
|
+
const run2 = rankPairCandidates({ corpus, proposal, limit: 1 })
|
|
176
|
+
assert.deepEqual(run1, run2)
|
|
177
|
+
assert.equal(run1.length, 1)
|
|
178
|
+
assert.equal(run1[0].id, 'cand-b')
|
|
179
|
+
})
|