dsh-memu-memory 1.4.2 → 1.4.3
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 +108 -110
- package/CHANGELOG.md +169 -868
- package/LICENSE +21 -21
- package/README.md +368 -374
- package/README.zh.md +354 -360
- package/assets/icon.svg +7 -7
- package/cordis.patch.yml +8 -8
- package/docs/active-memory-ab.md +279 -385
- package/docs/binding-lifecycle.md +99 -99
- package/docs/client-conventions.md +75 -75
- package/docs/contributing.md +217 -217
- package/docs/emotion-model-spec.md +247 -281
- package/docs/env-and-commands.md +149 -152
- package/docs/host-adaptation.md +339 -360
- package/docs/host-api.md +131 -137
- package/docs/host-versions.md +117 -120
- package/docs/mcp-porting.md +146 -146
- package/docs/memory-semantics.md +271 -259
- package/docs/pending-work.md +44 -51
- package/docs/troubleshooting.md +909 -854
- package/lib/active-memory.js +160 -165
- package/lib/activity-log.js +207 -207
- package/lib/auto-link/apply.js +248 -249
- package/lib/auto-link/candidates.js +163 -163
- package/lib/auto-link/decide.js +300 -300
- package/lib/auto-link/normalize.js +99 -99
- package/lib/auto-link/policy.js +42 -42
- package/lib/auto-link/suppression.js +101 -101
- package/lib/auto-link/terminology.js +311 -311
- package/lib/backup.js +1254 -1254
- package/lib/binding-diagnostics.js +634 -634
- package/lib/budget.js +118 -118
- package/lib/client.js +9820 -9751
- package/lib/commitment.js +178 -138
- package/lib/consolidation-ledger.js +434 -438
- package/lib/consolidation-receipt.js +386 -387
- package/lib/consolidation-snapshot.js +420 -405
- package/lib/consolidation-worker.js +240 -241
- package/lib/context.js +33 -33
- package/lib/curator-evidence.js +131 -131
- package/lib/curator-policy.js +198 -202
- package/lib/curator.js +1813 -1811
- package/lib/cycle.js +524 -513
- package/lib/diagnostics.js +524 -524
- package/lib/embedding-catalog.js +1051 -1055
- package/lib/embedding-download.js +637 -607
- package/lib/embedding-runtime.js +946 -946
- package/lib/engine.js +1103 -1103
- package/lib/entity-repair.js +285 -285
- package/lib/environment-check.js +394 -394
- package/lib/fork-model.js +312 -312
- package/lib/freshness.js +128 -121
- package/lib/global-graph.js +389 -389
- package/lib/graph.js +1193 -1193
- package/lib/host-surface.js +30 -30
- package/lib/inbound.js +284 -281
- package/lib/index.js +506 -511
- package/lib/inject.js +2639 -2619
- package/lib/lexicon-scope.js +143 -143
- package/lib/lexicon.js +1183 -1192
- package/lib/lifecycle-audit.js +37 -0
- package/lib/local-embedding-entry.js +145 -145
- package/lib/local-embedding-manager.js +306 -306
- package/lib/local-embedding-protocol.js +210 -210
- package/lib/local-embedding-worker.js +682 -682
- package/lib/local-vector-service.js +506 -453
- package/lib/maintenance-ledger.js +138 -0
- package/lib/maintenance-review.js +103 -0
- package/lib/maintenance-runner.js +267 -0
- package/lib/managed-runtime.js +118 -118
- package/lib/media.js +607 -513
- package/lib/memory.js +5035 -4941
- package/lib/memu-config.js +213 -213
- package/lib/memu.js +565 -565
- package/lib/migrate.js +200 -200
- package/lib/mood.js +300 -300
- package/lib/paths.js +453 -457
- package/lib/port-surface.js +82 -82
- package/lib/preset-mount.js +79 -0
- package/lib/preset-scope.js +126 -126
- package/lib/private-links.js +567 -567
- package/lib/private.js +2198 -2209
- package/lib/prompts/global-curator.md +135 -135
- package/lib/prompts/image-caption.md +1 -1
- package/lib/prompts/private-consolidation.coding.md +39 -39
- package/lib/prompts/private-consolidation.md +32 -32
- package/lib/prompts/private-consolidation.mixed.md +37 -37
- package/lib/prompts/private-consolidation.rp.md +47 -47
- package/lib/prompts/private-policy.coding.md +35 -35
- package/lib/prompts/private-policy.md +36 -36
- package/lib/prompts/private-policy.mixed.md +29 -29
- package/lib/prompts/private-policy.rp.md +43 -43
- package/lib/prompts/system-guide.coding.md +20 -20
- package/lib/prompts/system-guide.md +25 -25
- package/lib/prompts/system-guide.mixed.md +22 -22
- package/lib/prompts/system-guide.rp.md +23 -23
- package/lib/prompts.js +331 -331
- package/lib/recall-cache.js +212 -212
- package/lib/remote-summary.js +123 -123
- package/lib/rpc.js +2531 -2532
- package/lib/salient-wake.js +438 -438
- package/lib/snippet.js +125 -125
- package/lib/state.js +1948 -1928
- package/lib/stats.js +236 -236
- package/lib/temporal.js +345 -348
- package/lib/text-retrieval.js +992 -1001
- package/lib/text-similarity.js +305 -305
- package/lib/tokenizer-v2.js +271 -271
- package/lib/tools.js +1098 -1091
- package/lib/turn-log.js +545 -545
- package/lib/vector-migration.js +928 -930
- package/lib/vector-profile.js +1353 -1359
- package/lib/visibility.js +59 -59
- package/lib/webui/graph-LICENSE.txt +1 -1
- package/lib/webui/graph-view.bundle.js +2 -2
- package/lib/webui/graph-view.js +1171 -1175
- package/lib/webui/mark.svg +7 -7
- package/lib/webui/standalone.css +1178 -1178
- package/lib/webui/standalone.js +238 -238
- package/lib/webui.js +136 -136
- package/lib/worker-pool.js +1015 -979
- package/package.json +153 -153
- package/requirements.lock +34 -34
- package/scripts/acceptance-local.mjs +946 -946
- package/scripts/active-memory-ab-live.mjs +653 -655
- package/scripts/active-memory-ab.mjs +1302 -1302
- package/scripts/audit-memory-lifecycle.mjs +55 -0
- package/scripts/bench-recall.mjs +142 -142
- package/scripts/build-graph.mjs +44 -45
- package/scripts/chain-matrix.mjs +541 -541
- package/scripts/check-all.mjs +68 -74
- package/scripts/check-doc-drift.mjs +547 -547
- package/scripts/check-graph-browser.mjs +261 -261
- package/scripts/check-i18n.mjs +81 -83
- package/scripts/check-idle-compositing.mjs +61 -61
- package/scripts/check-seed-compat.mjs +115 -118
- package/scripts/check-text-format.mjs +55 -0
- package/scripts/curator-pair-calibrate.mjs +259 -259
- package/scripts/diagnose-bindings.mjs +176 -176
- package/scripts/drive-session.mjs +863 -864
- package/scripts/engine-smoke.mjs +69 -69
- package/scripts/entity-dedupe.mjs +110 -110
- package/scripts/lexicon-calibrate.mjs +230 -230
- package/scripts/lexicon-dedupe.mjs +318 -318
- package/scripts/local-runtime-install.mjs +142 -142
- package/scripts/local-runtime-verify.mjs +56 -56
- package/scripts/local-vector-verify.mjs +93 -93
- package/scripts/make-readme-shots.mjs +59 -59
- package/scripts/migrate-visibility.mjs +94 -94
- package/scripts/mock-llm.mjs +938 -903
- package/scripts/preview.mjs +58 -58
- package/scripts/prune-sessions.mjs +31 -34
- package/scripts/prune-turnlogs.mjs +29 -30
- package/scripts/prune-vectors.mjs +24 -24
- package/scripts/rebuild-auto-links.mjs +252 -252
- package/scripts/recall-bench.mjs +820 -820
- package/scripts/repetition-ab-scenarios.mjs +93 -93
- package/scripts/repetition-detector-snapshot.mjs +314 -314
- package/scripts/repetition-eval-live.mjs +360 -360
- package/scripts/repetition-eval.mjs +702 -702
- package/scripts/replay.mjs +705 -575
- package/scripts/setup.mjs +46 -46
- package/scripts/simulate-injection.mjs +299 -301
- package/tests/absorb-review.test.mjs +354 -354
- package/tests/activation.test.mjs +89 -89
- package/tests/active-memory-ab.test.mjs +745 -745
- package/tests/active-memory.test.mjs +129 -129
- package/tests/activity-log.test.mjs +126 -126
- package/tests/activity-wiring.test.mjs +155 -155
- package/tests/appendix-b.test.mjs +217 -217
- package/tests/appendix-b2.test.mjs +166 -166
- package/tests/audit-batch3.test.mjs +247 -247
- package/tests/audit-regression.test.mjs +275 -275
- package/tests/audit-safety.test.mjs +227 -227
- package/tests/auto-link-candidates.test.mjs +245 -245
- package/tests/auto-link-decide.test.mjs +512 -512
- package/tests/auto-link-migration.test.mjs +133 -133
- package/tests/backend-compensation.test.mjs +272 -272
- package/tests/backup.test.mjs +793 -793
- package/tests/binding-diagnostics.test.mjs +720 -720
- package/tests/browser.test.mjs +476 -476
- package/tests/client-export.test.mjs +521 -521
- package/tests/client-render.test.mjs +2008 -1728
- package/tests/coalesce.test.mjs +113 -113
- package/tests/commitment.test.mjs +243 -144
- package/tests/config.test.mjs +159 -159
- package/tests/consolidation-credential.test.mjs +330 -330
- package/tests/consolidation-feed.test.mjs +122 -122
- package/tests/consolidation-fork.test.mjs +1586 -1539
- package/tests/consolidation-ledger.test.mjs +389 -389
- package/tests/consolidation-snapshot.test.mjs +567 -505
- package/tests/consolidation-value.test.mjs +89 -89
- package/tests/context-scope.test.mjs +40 -40
- package/tests/cross-library-recall.test.mjs +269 -269
- package/tests/cue.test.mjs +58 -58
- package/tests/curator-absorb.test.mjs +444 -444
- package/tests/curator-calibrate.test.mjs +173 -173
- package/tests/curator-clusters.test.mjs +306 -306
- package/tests/curator-commit.test.mjs +325 -325
- package/tests/curator-contract.test.mjs +191 -191
- package/tests/curator-evidence.test.mjs +102 -102
- package/tests/curator-link.test.mjs +317 -317
- package/tests/curator-pairing.test.mjs +339 -339
- package/tests/curator-probe.test.mjs +490 -490
- package/tests/curator-review-mode.test.mjs +315 -315
- package/tests/curator.test.mjs +152 -152
- package/tests/cycle.test.mjs +567 -568
- package/tests/diagnostics.test.mjs +278 -278
- package/tests/disclosure.test.mjs +249 -249
- package/tests/doc-drift.test.mjs +342 -342
- package/tests/drive-session.test.mjs +152 -152
- package/tests/dynamic-rrf.test.mjs +182 -182
- package/tests/dynamic-threshold.test.mjs +52 -52
- package/tests/embedding-catalog.test.mjs +374 -374
- package/tests/embedding-download.test.mjs +529 -486
- package/tests/embedding-runtime.test.mjs +449 -449
- package/tests/emotion-chain.test.mjs +84 -84
- package/tests/emotion-contract.test.mjs +445 -445
- package/tests/engine.test.mjs +108 -108
- package/tests/entity-merge.test.mjs +278 -278
- package/tests/environment-check.test.mjs +187 -187
- package/tests/fact-version.test.mjs +146 -146
- package/tests/fixtures/active-memory-ab/README.md +56 -56
- package/tests/fixtures/active-memory-ab/cases/01-clean-dense-turn.json +351 -351
- package/tests/fixtures/active-memory-ab/cases/02-clean-thin-turn.json +122 -122
- package/tests/fixtures/active-memory-ab/cases/03-clean-starved-small.json +125 -125
- package/tests/fixtures/active-memory-ab/cases/04-clean-large-thin.json +137 -137
- package/tests/fixtures/active-memory-ab/cases/05-clean-half-step.json +166 -166
- package/tests/fixtures/active-memory-ab/cases/06-clean-duplicate-reread.json +197 -197
- package/tests/fixtures/active-memory-ab/cases/07-clean-recent-recall.json +125 -125
- package/tests/fixtures/active-memory-ab/cases/08-global-relevant.json +111 -111
- package/tests/fixtures/active-memory-ab/cases/09-global-irrelevant-conflict.json +158 -158
- package/tests/fixtures/active-memory-ab/cases/10-no-human-turn.json +97 -97
- package/tests/fixtures/active-memory-ab/cases/11-clean-scale-100.json +137 -137
- package/tests/fixtures/active-memory-ab/cases/12-clean-scale-1000.json +137 -137
- package/tests/fixtures/active-memory-ab/cases/13-clean-scale-5000.json +137 -137
- package/tests/fixtures/active-memory-ab/cases/14-clean-scale-20000.json +137 -137
- package/tests/fixtures/active-memory-ab/lead-ai-reviewed-reference.json +191 -191
- package/tests/fixtures/active-memory-ab/manifest.json +199 -199
- package/tests/fixtures/curator-pairs/labeled.v1.jsonl +90 -90
- package/tests/fixtures/lexicon-stub-hooks.mjs +15 -15
- package/tests/fixtures/lexicon-stub.mjs +284 -284
- package/tests/fixtures/mock-llm/reply.txt +1 -1
- package/tests/fixtures/recall-bench/01-paraphrase.json +183 -183
- package/tests/fixtures/recall-bench/02-exact.json +152 -152
- package/tests/fixtures/recall-bench/03-negation.json +172 -172
- package/tests/fixtures/recall-bench/04-no-answer.json +161 -161
- package/tests/fixtures/recall-bench/05-conflict.json +193 -193
- package/tests/fixtures/recall-bench/06-boundary.json +272 -272
- package/tests/fixtures/recall-bench/07-secret.json +190 -190
- package/tests/fixtures/recall-bench/08-coding.json +163 -163
- package/tests/fixtures/recall-bench-gold-candidates/01-paraphrase.json +193 -193
- package/tests/fixtures/recall-bench-gold-candidates/02-exact.json +197 -197
- package/tests/fixtures/recall-bench-gold-candidates/03-negation.json +244 -244
- package/tests/fixtures/recall-bench-gold-candidates/04-no-answer.json +189 -189
- package/tests/fixtures/recall-bench-gold-candidates/05-conflict.json +245 -245
- package/tests/fixtures/recall-bench-gold-candidates/06-boundary.json +279 -279
- package/tests/fixtures/recall-bench-gold-candidates/07-secret.json +224 -224
- package/tests/fixtures/recall-bench-gold-candidates/08-coding.json +218 -218
- package/tests/fixtures/repetition-eval/01-zh-rp.json +92 -92
- package/tests/fixtures/repetition-eval/02-en.json +96 -96
- package/tests/fixtures/repetition-eval/03-code.json +90 -90
- package/tests/fixtures/repetition-eval/04-names.json +115 -115
- package/tests/fixtures/repetition-eval/05-user-quote.json +95 -95
- package/tests/fixtures/repetition-eval/06-agreement.json +85 -85
- package/tests/fixtures/tokenizer-cases.mjs +27 -27
- package/tests/fork-model.test.mjs +345 -345
- package/tests/freshness.test.mjs +140 -140
- package/tests/global-graph.test.mjs +106 -106
- package/tests/global-lifecycle.test.mjs +149 -149
- package/tests/graph-cascade.test.mjs +363 -363
- package/tests/graph-view.test.mjs +414 -414
- package/tests/graph.test.mjs +371 -371
- package/tests/help-page.test.mjs +416 -416
- package/tests/hook-order.test.mjs +72 -72
- package/tests/humanlike.test.mjs +232 -232
- package/tests/hybrid-recall.test.mjs +610 -610
- package/tests/inbound.test.mjs +441 -441
- package/tests/inject-conflict.test.mjs +173 -173
- package/tests/inject-guide.test.mjs +513 -513
- package/tests/inject.test.mjs +384 -384
- package/tests/injection-budget-settings.test.mjs +35 -35
- package/tests/injection-budget.test.mjs +165 -165
- package/tests/injection-chain.test.mjs +442 -442
- package/tests/injection-intent-budget.test.mjs +92 -92
- package/tests/injection-rollback.test.mjs +428 -428
- package/tests/issue2-chain.test.mjs +169 -169
- package/tests/lexical-bm25.test.mjs +171 -171
- package/tests/lexicon-calibrate.test.mjs +48 -48
- package/tests/lexicon-dedupe.test.mjs +171 -171
- package/tests/lexicon-frequency.test.mjs +195 -195
- package/tests/lexicon-inject.test.mjs +459 -459
- package/tests/lexicon-perf.test.mjs +464 -464
- package/tests/lexicon-scope.test.mjs +256 -256
- package/tests/lexicon-ui.test.mjs +667 -667
- package/tests/lexicon.test.mjs +922 -922
- package/tests/lifecycle-audit.test.mjs +56 -0
- package/tests/loader-register.mjs +3 -3
- package/tests/loader.mjs +17 -17
- package/tests/local-embedding-entry.test.mjs +95 -95
- package/tests/local-embedding-env.test.mjs +51 -51
- package/tests/local-embedding-manager.test.mjs +494 -494
- package/tests/local-embedding-protocol.test.mjs +121 -121
- package/tests/local-embedding-worker.test.mjs +440 -440
- package/tests/local-vector-service.test.mjs +41 -0
- package/tests/maintenance-status.test.mjs +77 -0
- package/tests/maintenance.test.mjs +438 -0
- package/tests/managed-memory.test.mjs +1234 -1234
- package/tests/managed-runtime.test.mjs +72 -72
- package/tests/media-injection.test.mjs +225 -0
- package/tests/media.test.mjs +135 -135
- package/tests/memory-chain.test.mjs +169 -169
- package/tests/memory-fields.test.mjs +134 -134
- package/tests/memu-resident-route.test.mjs +130 -130
- package/tests/memu-resident.test.mjs +33 -33
- package/tests/migration-payload.test.mjs +40 -42
- package/tests/mock-llm.test.mjs +263 -225
- package/tests/mood.test.mjs +109 -109
- package/tests/multiprocess.test.mjs +29 -29
- package/tests/negation-polarity.test.mjs +135 -135
- package/tests/new-session-gate.test.mjs +617 -617
- package/tests/open-loop-date.test.mjs +416 -416
- package/tests/ops-hardening.test.mjs +209 -209
- package/tests/package-shipping.test.mjs +88 -88
- package/tests/pending-injection.test.mjs +107 -107
- package/tests/port-surface.test.mjs +138 -138
- package/tests/preset-mount.test.mjs +124 -0
- package/tests/preset-scope.test.mjs +212 -212
- package/tests/private-links.test.mjs +493 -493
- package/tests/prompt-semantics.test.mjs +51 -51
- package/tests/prompts.test.mjs +315 -315
- package/tests/query-decompose.test.mjs +62 -62
- package/tests/react-snapshot.test.mjs +24 -24
- package/tests/recall-bench.test.mjs +317 -317
- package/tests/recall-cache.test.mjs +292 -292
- package/tests/recall-corpus-eligibility.test.mjs +51 -51
- package/tests/recall-dedup.test.mjs +446 -446
- package/tests/recall-intent.test.mjs +172 -172
- package/tests/regression-sweep.test.mjs +311 -311
- package/tests/reminder-fairness.test.mjs +55 -0
- package/tests/remote.test.mjs +140 -140
- package/tests/rename.test.mjs +30 -30
- package/tests/repetition-detector-snapshot.test.mjs +231 -231
- package/tests/repetition-eval-live.test.mjs +177 -177
- package/tests/repetition-eval.test.mjs +373 -373
- package/tests/salience-domain.test.mjs +173 -173
- package/tests/salient-date.test.mjs +142 -142
- package/tests/salient-wake.test.mjs +389 -389
- package/tests/settings-beat.test.mjs +222 -222
- package/tests/settings-coverage.test.mjs +97 -97
- package/tests/settings-plumbing.test.mjs +79 -79
- package/tests/short-term.test.mjs +182 -166
- package/tests/sidebar-beat.test.mjs +213 -213
- package/tests/sidebar-refresh.test.mjs +123 -123
- package/tests/sidebar.test.mjs +102 -74
- package/tests/simulate-injection.test.mjs +76 -76
- package/tests/slot-similarity.test.mjs +153 -153
- package/tests/snippet.test.mjs +79 -79
- package/tests/source-kind.test.mjs +52 -52
- package/tests/speaker-provenance.test.mjs +120 -120
- package/tests/stale-maintenance.test.mjs +206 -131
- package/tests/state-gc.test.mjs +34 -34
- package/tests/state.test.mjs +155 -155
- package/tests/stats.test.mjs +112 -112
- package/tests/storage.test.mjs +125 -125
- package/tests/stubs/child-process-spy.mjs +14 -14
- package/tests/stubs/dsh-llm-pi-ai.js +13 -13
- package/tests/stubs/dsh-llm.js +5 -5
- package/tests/stubs/dsh-schedule.js +140 -140
- package/tests/stubs/dsh-session-query.js +26 -26
- package/tests/stubs/dsh-session.js +7 -7
- package/tests/stubs/dsh-tools.js +101 -101
- package/tests/temporal-repair.test.mjs +78 -0
- package/tests/temporal.test.mjs +124 -124
- package/tests/text-format.test.mjs +24 -0
- package/tests/tokenizer-v2.test.mjs +142 -142
- package/tests/tool-schema.test.mjs +126 -126
- package/tests/tools.test.mjs +810 -810
- package/tests/turn-log.test.mjs +487 -487
- package/tests/ui-defaults.test.mjs +57 -57
- package/tests/update-fields.test.mjs +138 -138
- package/tests/vector-gc.test.mjs +37 -37
- package/tests/vector-migration.test.mjs +810 -810
- package/tests/vector-profile.test.mjs +449 -449
- package/tests/visibility-migration.test.mjs +152 -152
- package/tests/webui-auth.test.mjs +963 -963
- package/tests/webui.test.mjs +421 -421
- package/tests/windows-env.test.mjs +89 -89
- package/tests/worker-lifecycle.test.mjs +1521 -1429
|
@@ -1,610 +1,610 @@
|
|
|
1
|
-
import { test } from 'node:test'
|
|
2
|
-
import assert from 'node:assert/strict'
|
|
3
|
-
|
|
4
|
-
const memory = await import('../lib/memory.js')
|
|
5
|
-
|
|
6
|
-
function entry(overrides = {}) {
|
|
7
|
-
return { status: 'active', title: 'memory', content: 'memory body', createdAt: '2026-10-01T00:00:00.000Z', updatedAt: '2026-10-01T00:00:00.000Z', ...overrides }
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
function indexOf(entries) {
|
|
11
|
-
return { entries }
|
|
12
|
-
}
|
|
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
|
-
|
|
51
|
-
test('RRF keeps one canonical id when both routes find it and preserves the best order', () => {
|
|
52
|
-
const index = indexOf({
|
|
53
|
-
'mem-a': entry({ title: 'alpha' }),
|
|
54
|
-
'mem-b': entry({ title: 'beta' }),
|
|
55
|
-
'mem-c': entry({ title: 'gamma' }),
|
|
56
|
-
})
|
|
57
|
-
const fused = memory.fuseRecallCandidates({
|
|
58
|
-
local: [{ name: 'mem-a' }, { name: 'mem-b' }],
|
|
59
|
-
semantic: [{ name: 'mem-b.md' }, { name: 'mem-c' }],
|
|
60
|
-
index,
|
|
61
|
-
limit: 12,
|
|
62
|
-
})
|
|
63
|
-
assert.deepEqual(fused.files.map((file) => file.name), ['mem-b', 'mem-a', 'mem-c'])
|
|
64
|
-
assert.equal(fused.files[0].retrievalScore, 1)
|
|
65
|
-
assert.deepEqual(fused.files[0].fusion.routes, ['local', 'semantic'])
|
|
66
|
-
assert.equal(fused.debug.shared, 1)
|
|
67
|
-
assert.equal(fused.debug.localOnly, 1)
|
|
68
|
-
assert.equal(fused.debug.semanticOnly, 1)
|
|
69
|
-
assert.equal(fused.debug.fused, 3)
|
|
70
|
-
})
|
|
71
|
-
|
|
72
|
-
test('duplicate names within one route cannot double-score or consume a candidate slot', () => {
|
|
73
|
-
const index = indexOf({ 'mem-a': entry({ title: 'alpha' }), 'mem-b': entry({ title: 'beta' }) })
|
|
74
|
-
const fused = memory.fuseRecallCandidates({
|
|
75
|
-
local: [{ name: 'mem-a' }, { name: 'mem-a.md' }, { name: 'mem-b' }],
|
|
76
|
-
semantic: [],
|
|
77
|
-
index,
|
|
78
|
-
limit: 2,
|
|
79
|
-
})
|
|
80
|
-
assert.deepEqual(fused.files.map((file) => file.name), ['mem-a', 'mem-b'])
|
|
81
|
-
assert.equal(fused.debug.localCandidates, 2)
|
|
82
|
-
const first = fused.files.find((file) => file.name === 'mem-a')
|
|
83
|
-
assert.equal(first.fusion.rrf, 1 / 61)
|
|
84
|
-
assert.equal(first.fusion.bestRank, 1)
|
|
85
|
-
assert.equal(first.fusion.localRank, 1)
|
|
86
|
-
})
|
|
87
|
-
|
|
88
|
-
test('eligibility and context are applied before the candidate cap', () => {
|
|
89
|
-
const index = indexOf({
|
|
90
|
-
'mem-pending': entry({ status: 'pending_review' }),
|
|
91
|
-
'mem-secret': entry({ visibility: 'secret' }),
|
|
92
|
-
'mem-ok': entry({ title: 'ok' }),
|
|
93
|
-
})
|
|
94
|
-
const fused = memory.fuseRecallCandidates({
|
|
95
|
-
local: [{ name: 'mem-pending' }, { name: 'mem-secret' }, { name: 'mem-ok' }],
|
|
96
|
-
semantic: [{ name: 'mem-secret' }],
|
|
97
|
-
index,
|
|
98
|
-
limit: 12,
|
|
99
|
-
automatic: true,
|
|
100
|
-
})
|
|
101
|
-
assert.deepEqual(fused.files.map((file) => file.name), ['mem-ok'])
|
|
102
|
-
assert.equal(fused.debug.localCandidates, 1)
|
|
103
|
-
assert.equal(fused.debug.semanticCandidates, 0)
|
|
104
|
-
})
|
|
105
|
-
|
|
106
|
-
test('the per-route candidate cap is bounded and deterministic', () => {
|
|
107
|
-
const entries = {}
|
|
108
|
-
const local = []
|
|
109
|
-
// Distinct bodies: near-identical candidates would fold into one cluster slot
|
|
110
|
-
// (fact-level dedup), and this test is about the cap, not the folding.
|
|
111
|
-
const alphabet = 'abcdefghijklmnopqrstuvwxyz'
|
|
112
|
-
const token = (n) => alphabet[n % 26] + alphabet[Math.floor(n / 26) % 26]
|
|
113
|
-
for (let i = 0; i < 60; i += 1) {
|
|
114
|
-
const id = 'mem-' + String(i).padStart(2, '0')
|
|
115
|
-
entries[id] = entry({
|
|
116
|
-
title: 'topic ' + token(180 + i),
|
|
117
|
-
content: [token(3 * i), token(3 * i + 1), token(3 * i + 2)].join('of'),
|
|
118
|
-
})
|
|
119
|
-
local.push({ name: id })
|
|
120
|
-
}
|
|
121
|
-
assert.equal(memory.hybridCandidateLimit(1), 12)
|
|
122
|
-
assert.equal(memory.hybridCandidateLimit(3), 12)
|
|
123
|
-
assert.equal(memory.hybridCandidateLimit(8), 32)
|
|
124
|
-
assert.equal(memory.hybridCandidateLimit(50), 40)
|
|
125
|
-
const fused = memory.fuseRecallCandidates({ local, semantic: [], index: indexOf(entries), limit: memory.hybridCandidateLimit(3) })
|
|
126
|
-
assert.equal(fused.files.length, 12)
|
|
127
|
-
assert.deepEqual(fused.files.map((file) => file.name), local.slice(0, 12).map((file) => file.name).sort((a, b) => a.localeCompare(b)))
|
|
128
|
-
})
|
|
129
|
-
|
|
130
|
-
test('near-duplicate candidates fold into one candidate slot before the cap', () => {
|
|
131
|
-
const entries = {}
|
|
132
|
-
const local = []
|
|
133
|
-
for (let i = 0; i < 12; i += 1) {
|
|
134
|
-
const id = 'mem-dup-' + String(i).padStart(2, '0')
|
|
135
|
-
entries[id] = entry({ title: 'deploy window ' + (i ? 'variant ' + i : ''), content: 'the deploy window is thursday two to four am, grey releases only' })
|
|
136
|
-
local.push({ name: id })
|
|
137
|
-
}
|
|
138
|
-
entries['mem-real'] = entry({ title: 'reimbursement deadline', content: 'expense reports are due before the last working day of each month' })
|
|
139
|
-
const fused = memory.fuseRecallCandidates({ local: [...local, { name: 'mem-real' }], semantic: [], index: indexOf(entries), limit: 12 })
|
|
140
|
-
// The cluster spends one slot instead of twelve, so the real memory enters
|
|
141
|
-
// even though 13 candidates competed for 12 places.
|
|
142
|
-
assert.equal(fused.debug.localCandidates, 2)
|
|
143
|
-
assert.deepEqual(fused.files.map((file) => file.name), ['mem-dup-00', 'mem-real'])
|
|
144
|
-
assert.deepEqual(fused.debug.collapsed, [{ kept: 'mem-dup-00', dropped: local.slice(1).map((file) => file.name) }])
|
|
145
|
-
assert.equal(fused.debug.clusterCount, 1)
|
|
146
|
-
})
|
|
147
|
-
|
|
148
|
-
test('equal fused scores fall back to the best rank and then the canonical id', () => {
|
|
149
|
-
// Distinct texts: two byte-identical memories would fold as one fact before
|
|
150
|
-
// the tie-break this test is about ever gets a chance to run.
|
|
151
|
-
const index = indexOf({
|
|
152
|
-
'mem-a': entry({ title: 'alpha topic', content: 'alpha body' }),
|
|
153
|
-
'mem-b': entry({ title: 'beta topic', content: 'beta body' }),
|
|
154
|
-
})
|
|
155
|
-
const first = memory.fuseRecallCandidates({ local: [{ name: 'mem-a' }], semantic: [{ name: 'mem-b' }], index })
|
|
156
|
-
const second = memory.fuseRecallCandidates({ local: [{ name: 'mem-a' }], semantic: [{ name: 'mem-b' }], index })
|
|
157
|
-
assert.deepEqual(first.files.map((file) => file.name), ['mem-a', 'mem-b'])
|
|
158
|
-
assert.deepEqual(first.files.map((file) => file.name), second.files.map((file) => file.name))
|
|
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
|
-
}
|
|
1
|
+
import { test } from 'node:test'
|
|
2
|
+
import assert from 'node:assert/strict'
|
|
3
|
+
|
|
4
|
+
const memory = await import('../lib/memory.js')
|
|
5
|
+
|
|
6
|
+
function entry(overrides = {}) {
|
|
7
|
+
return { status: 'active', title: 'memory', content: 'memory body', createdAt: '2026-10-01T00:00:00.000Z', updatedAt: '2026-10-01T00:00:00.000Z', ...overrides }
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function indexOf(entries) {
|
|
11
|
+
return { entries }
|
|
12
|
+
}
|
|
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
|
+
|
|
51
|
+
test('RRF keeps one canonical id when both routes find it and preserves the best order', () => {
|
|
52
|
+
const index = indexOf({
|
|
53
|
+
'mem-a': entry({ title: 'alpha' }),
|
|
54
|
+
'mem-b': entry({ title: 'beta' }),
|
|
55
|
+
'mem-c': entry({ title: 'gamma' }),
|
|
56
|
+
})
|
|
57
|
+
const fused = memory.fuseRecallCandidates({
|
|
58
|
+
local: [{ name: 'mem-a' }, { name: 'mem-b' }],
|
|
59
|
+
semantic: [{ name: 'mem-b.md' }, { name: 'mem-c' }],
|
|
60
|
+
index,
|
|
61
|
+
limit: 12,
|
|
62
|
+
})
|
|
63
|
+
assert.deepEqual(fused.files.map((file) => file.name), ['mem-b', 'mem-a', 'mem-c'])
|
|
64
|
+
assert.equal(fused.files[0].retrievalScore, 1)
|
|
65
|
+
assert.deepEqual(fused.files[0].fusion.routes, ['local', 'semantic'])
|
|
66
|
+
assert.equal(fused.debug.shared, 1)
|
|
67
|
+
assert.equal(fused.debug.localOnly, 1)
|
|
68
|
+
assert.equal(fused.debug.semanticOnly, 1)
|
|
69
|
+
assert.equal(fused.debug.fused, 3)
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
test('duplicate names within one route cannot double-score or consume a candidate slot', () => {
|
|
73
|
+
const index = indexOf({ 'mem-a': entry({ title: 'alpha' }), 'mem-b': entry({ title: 'beta' }) })
|
|
74
|
+
const fused = memory.fuseRecallCandidates({
|
|
75
|
+
local: [{ name: 'mem-a' }, { name: 'mem-a.md' }, { name: 'mem-b' }],
|
|
76
|
+
semantic: [],
|
|
77
|
+
index,
|
|
78
|
+
limit: 2,
|
|
79
|
+
})
|
|
80
|
+
assert.deepEqual(fused.files.map((file) => file.name), ['mem-a', 'mem-b'])
|
|
81
|
+
assert.equal(fused.debug.localCandidates, 2)
|
|
82
|
+
const first = fused.files.find((file) => file.name === 'mem-a')
|
|
83
|
+
assert.equal(first.fusion.rrf, 1 / 61)
|
|
84
|
+
assert.equal(first.fusion.bestRank, 1)
|
|
85
|
+
assert.equal(first.fusion.localRank, 1)
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
test('eligibility and context are applied before the candidate cap', () => {
|
|
89
|
+
const index = indexOf({
|
|
90
|
+
'mem-pending': entry({ status: 'pending_review' }),
|
|
91
|
+
'mem-secret': entry({ visibility: 'secret' }),
|
|
92
|
+
'mem-ok': entry({ title: 'ok' }),
|
|
93
|
+
})
|
|
94
|
+
const fused = memory.fuseRecallCandidates({
|
|
95
|
+
local: [{ name: 'mem-pending' }, { name: 'mem-secret' }, { name: 'mem-ok' }],
|
|
96
|
+
semantic: [{ name: 'mem-secret' }],
|
|
97
|
+
index,
|
|
98
|
+
limit: 12,
|
|
99
|
+
automatic: true,
|
|
100
|
+
})
|
|
101
|
+
assert.deepEqual(fused.files.map((file) => file.name), ['mem-ok'])
|
|
102
|
+
assert.equal(fused.debug.localCandidates, 1)
|
|
103
|
+
assert.equal(fused.debug.semanticCandidates, 0)
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
test('the per-route candidate cap is bounded and deterministic', () => {
|
|
107
|
+
const entries = {}
|
|
108
|
+
const local = []
|
|
109
|
+
// Distinct bodies: near-identical candidates would fold into one cluster slot
|
|
110
|
+
// (fact-level dedup), and this test is about the cap, not the folding.
|
|
111
|
+
const alphabet = 'abcdefghijklmnopqrstuvwxyz'
|
|
112
|
+
const token = (n) => alphabet[n % 26] + alphabet[Math.floor(n / 26) % 26]
|
|
113
|
+
for (let i = 0; i < 60; i += 1) {
|
|
114
|
+
const id = 'mem-' + String(i).padStart(2, '0')
|
|
115
|
+
entries[id] = entry({
|
|
116
|
+
title: 'topic ' + token(180 + i),
|
|
117
|
+
content: [token(3 * i), token(3 * i + 1), token(3 * i + 2)].join('of'),
|
|
118
|
+
})
|
|
119
|
+
local.push({ name: id })
|
|
120
|
+
}
|
|
121
|
+
assert.equal(memory.hybridCandidateLimit(1), 12)
|
|
122
|
+
assert.equal(memory.hybridCandidateLimit(3), 12)
|
|
123
|
+
assert.equal(memory.hybridCandidateLimit(8), 32)
|
|
124
|
+
assert.equal(memory.hybridCandidateLimit(50), 40)
|
|
125
|
+
const fused = memory.fuseRecallCandidates({ local, semantic: [], index: indexOf(entries), limit: memory.hybridCandidateLimit(3) })
|
|
126
|
+
assert.equal(fused.files.length, 12)
|
|
127
|
+
assert.deepEqual(fused.files.map((file) => file.name), local.slice(0, 12).map((file) => file.name).sort((a, b) => a.localeCompare(b)))
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
test('near-duplicate candidates fold into one candidate slot before the cap', () => {
|
|
131
|
+
const entries = {}
|
|
132
|
+
const local = []
|
|
133
|
+
for (let i = 0; i < 12; i += 1) {
|
|
134
|
+
const id = 'mem-dup-' + String(i).padStart(2, '0')
|
|
135
|
+
entries[id] = entry({ title: 'deploy window ' + (i ? 'variant ' + i : ''), content: 'the deploy window is thursday two to four am, grey releases only' })
|
|
136
|
+
local.push({ name: id })
|
|
137
|
+
}
|
|
138
|
+
entries['mem-real'] = entry({ title: 'reimbursement deadline', content: 'expense reports are due before the last working day of each month' })
|
|
139
|
+
const fused = memory.fuseRecallCandidates({ local: [...local, { name: 'mem-real' }], semantic: [], index: indexOf(entries), limit: 12 })
|
|
140
|
+
// The cluster spends one slot instead of twelve, so the real memory enters
|
|
141
|
+
// even though 13 candidates competed for 12 places.
|
|
142
|
+
assert.equal(fused.debug.localCandidates, 2)
|
|
143
|
+
assert.deepEqual(fused.files.map((file) => file.name), ['mem-dup-00', 'mem-real'])
|
|
144
|
+
assert.deepEqual(fused.debug.collapsed, [{ kept: 'mem-dup-00', dropped: local.slice(1).map((file) => file.name) }])
|
|
145
|
+
assert.equal(fused.debug.clusterCount, 1)
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
test('equal fused scores fall back to the best rank and then the canonical id', () => {
|
|
149
|
+
// Distinct texts: two byte-identical memories would fold as one fact before
|
|
150
|
+
// the tie-break this test is about ever gets a chance to run.
|
|
151
|
+
const index = indexOf({
|
|
152
|
+
'mem-a': entry({ title: 'alpha topic', content: 'alpha body' }),
|
|
153
|
+
'mem-b': entry({ title: 'beta topic', content: 'beta body' }),
|
|
154
|
+
})
|
|
155
|
+
const first = memory.fuseRecallCandidates({ local: [{ name: 'mem-a' }], semantic: [{ name: 'mem-b' }], index })
|
|
156
|
+
const second = memory.fuseRecallCandidates({ local: [{ name: 'mem-a' }], semantic: [{ name: 'mem-b' }], index })
|
|
157
|
+
assert.deepEqual(first.files.map((file) => file.name), ['mem-a', 'mem-b'])
|
|
158
|
+
assert.deepEqual(first.files.map((file) => file.name), second.files.map((file) => file.name))
|
|
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
|
+
}
|