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,512 +1,512 @@
|
|
|
1
|
-
import test from 'node:test'
|
|
2
|
-
import assert from 'node:assert/strict'
|
|
3
|
-
import fs from 'node:fs'
|
|
4
|
-
import os from 'node:os'
|
|
5
|
-
import path from 'node:path'
|
|
6
|
-
import { performance } from 'node:perf_hooks'
|
|
7
|
-
|
|
8
|
-
const home = fs.mkdtempSync(path.join(os.tmpdir(), 'dsh-memu-memory-autolink-'))
|
|
9
|
-
process.env.DSH_MEMU_HOME = home
|
|
10
|
-
process.env.DSH_MEMU_MEMORY_HOME = home
|
|
11
|
-
|
|
12
|
-
const state = await import('../lib/state.js')
|
|
13
|
-
const memory = await import('../lib/memory.js')
|
|
14
|
-
const graph = await import('../lib/graph.js')
|
|
15
|
-
const decide = await import('../lib/auto-link/decide.js')
|
|
16
|
-
const apply = await import('../lib/auto-link/apply.js')
|
|
17
|
-
const suppression = await import('../lib/auto-link/suppression.js')
|
|
18
|
-
const normalize = await import('../lib/auto-link/normalize.js')
|
|
19
|
-
const policy = await import('../lib/auto-link/policy.js')
|
|
20
|
-
const privateLinks = await import('../lib/private-links.js')
|
|
21
|
-
const rpc = await import('../lib/rpc.js')
|
|
22
|
-
|
|
23
|
-
test.after(() => {
|
|
24
|
-
try { state.flushState() } catch {}
|
|
25
|
-
try { fs.rmSync(home, { recursive: true, force: true }) } catch {}
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
let counter = 0
|
|
29
|
-
function library(name) {
|
|
30
|
-
counter += 1
|
|
31
|
-
const binding = state.ensureSession(`autolink-${name}-${counter}`, { cwd: path.join(home, `ws-${name}-${counter}`), agentPreset: 'rp' })
|
|
32
|
-
return binding.libraryId
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const at = (day) => `2026-11-${String(day).padStart(2, '0')}T00:00:00.000Z`
|
|
36
|
-
|
|
37
|
-
function entry(title, content, extra = {}) {
|
|
38
|
-
return { title, content, status: 'active', visibility: 'shared', layer: 'long_term', createdAt: at(1), updatedAt: at(1), ...extra }
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function index(entries) {
|
|
42
|
-
return { version: 1, entries }
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// ---------------------------------------------------------------- normalization
|
|
46
|
-
|
|
47
|
-
test('mentions are boundary-aware and punctuation-insensitive: CJK substrings, identifiers, NFKC folding', () => {
|
|
48
|
-
assert.equal(normalize.mentionCount('yuike 和 yuikeee', 'yuike'), 1, 'Latin must not match a longer identifier')
|
|
49
|
-
assert.equal(normalize.mentionCount('YUIKE!来了', 'yuike'), 1, 'full-width and punctuation fold away')
|
|
50
|
-
assert.equal(normalize.mentionCount('洛云澜来了,洛云澜走了', '洛云澜'), 2)
|
|
51
|
-
// §8.1: NFKC + 去标点/空白 applies to the name as well as the text.
|
|
52
|
-
assert.equal(normalize.mentionCount('小·檀来了', '小檀'), 1)
|
|
53
|
-
assert.equal(normalize.mentionCount('小 檀来了', '小檀'), 1)
|
|
54
|
-
assert.equal(normalize.mentionCount('小檀来了', '小 檀'), 1)
|
|
55
|
-
// §2: code identifiers keep their punctuation and their word boundaries.
|
|
56
|
-
assert.equal(normalize.mentionCount('Yuike_01 上线', 'Yuike_01'), 1)
|
|
57
|
-
assert.equal(normalize.mentionCount('yuike.dev is down', 'yuike.dev'), 1)
|
|
58
|
-
assert.equal(normalize.mentionCount('洛云澜-alex 来了', '洛云澜-alex'), 1)
|
|
59
|
-
assert.equal(normalize.firstMention('猫娘yuike', 'yuike'), 2)
|
|
60
|
-
assert.equal(normalize.firstMention('「小·檀」来了', '小檀'), 1, 'offset is the raw text position, not the compact index')
|
|
61
|
-
assert.equal(normalize.isMentionSurface('小'), false, 'one CJK character is not a surface')
|
|
62
|
-
assert.equal(normalize.isMentionSurface('小檀'), true)
|
|
63
|
-
assert.equal(normalize.isMentionSurface('a'), false, 'one Latin character is not a surface')
|
|
64
|
-
})
|
|
65
|
-
|
|
66
|
-
test('punctuated and identifier surfaces produce mention edges with raw offsets', () => {
|
|
67
|
-
const lib = library('punct')
|
|
68
|
-
const you = graph.createEntity({ libraryId: lib, name: '小檀', kind: 'person', status: 'confirmed' })
|
|
69
|
-
const yuike = graph.createEntity({ libraryId: lib, name: 'yuike_01', kind: 'person', status: 'confirmed' })
|
|
70
|
-
const mixed = graph.createEntity({ libraryId: lib, name: '洛云澜-alex', kind: 'person', status: 'confirmed' })
|
|
71
|
-
const snapshot = index({
|
|
72
|
-
'mem-a': entry('「小·檀」来了', '她带着伞。'),
|
|
73
|
-
'mem-b': entry('yuike_01 上线', 'yuike_01 迟到了。'),
|
|
74
|
-
'mem-c': entry('洛云澜-alex 到访', '洛云澜-alex 和 洛云澜 yuike 都来了。'),
|
|
75
|
-
})
|
|
76
|
-
const edges = decide.decideAutoLinks({ index: snapshot, entities: [you, yuike, mixed] })
|
|
77
|
-
const mention = edges.filter(edge => edge.rule === 'entity-mention')
|
|
78
|
-
assert.equal(mention.length, 3, JSON.stringify(mention.map(edge => `${edge.from}->${edge.to}`)))
|
|
79
|
-
for (const edge of edges) assert.ok(policy.EVIDENCE_RULES.includes(edge.rule), 'only the declared rule vocabulary: ' + edge.rule)
|
|
80
|
-
const byEntity = new Map(mention.map(edge => [edge.to, edge]))
|
|
81
|
-
const youEdge = byEntity.get(you.id)
|
|
82
|
-
assert.equal(youEdge.evidence.surface, '小檀')
|
|
83
|
-
assert.equal(youEdge.evidence.offset, 1, 'raw offset inside 「小·檀」')
|
|
84
|
-
assert.ok(byEntity.has(yuike.id), 'identifier with underscore is found')
|
|
85
|
-
assert.ok(byEntity.has(mixed.id), 'mixed CJK-Latin name is found')
|
|
86
|
-
})
|
|
87
|
-
|
|
88
|
-
// ---------------------------------------------------------------- decision layer
|
|
89
|
-
|
|
90
|
-
test('only confirmed entities produce mention edges; provisional, archived and secret rows do not', () => {
|
|
91
|
-
const lib = library('mention')
|
|
92
|
-
const you = graph.createEntity({ libraryId: lib, name: '小檀', kind: 'person', aliases: ['檀子'], status: 'confirmed', confirmedBy: 'user' })
|
|
93
|
-
graph.createEntity({ libraryId: lib, name: '沈砚舟', kind: 'person' })
|
|
94
|
-
graph.createEntity({ libraryId: lib, name: '阿兰', kind: 'person', status: 'archived' })
|
|
95
|
-
const snapshot = index({
|
|
96
|
-
'mem-a': entry('小檀今天来了', '她带了一篮水果。'),
|
|
97
|
-
'mem-b': entry('沈砚舟的日记', '沈砚舟写下了今天的安排。'),
|
|
98
|
-
'mem-c': entry('阿兰回信', '阿兰说下周再联系。'),
|
|
99
|
-
'mem-d': entry('小檀与秘密', '关于小檀的一件不能外传的事。', { visibility: 'secret' }),
|
|
100
|
-
'mem-e': entry('小檀旧事', '小檀很久以前来过。', { status: 'archived' }),
|
|
101
|
-
})
|
|
102
|
-
const edges = decide.decideAutoLinks({ index: snapshot, entities: [you, { id: 'ent-provisional', name: '沈砚舟', kind: 'person', status: 'provisional' }, { id: 'ent-archived', name: '阿兰', kind: 'person', status: 'archived' }] })
|
|
103
|
-
const mention = edges.filter(edge => edge.rule === 'entity-mention')
|
|
104
|
-
assert.equal(mention.length, 1, 'one eligible memory mentions the confirmed entity')
|
|
105
|
-
assert.equal(mention[0].from, 'mem-a')
|
|
106
|
-
assert.equal(mention[0].to, you.id)
|
|
107
|
-
assert.equal(mention[0].kind, 'person')
|
|
108
|
-
assert.equal(mention[0].confidence, 1)
|
|
109
|
-
assert.equal(mention[0].schemaVersion, 2)
|
|
110
|
-
assert.ok(mention[0].evidence.surface === '小檀' || mention[0].evidence.surface === '檀子')
|
|
111
|
-
assert.equal(mention[0].evidence.entityId, you.id)
|
|
112
|
-
assert.equal(mention[0].evidence.field, 'title')
|
|
113
|
-
assert.equal(mention[0].evidence.offset, 0)
|
|
114
|
-
})
|
|
115
|
-
|
|
116
|
-
test('suppression removes a decided edge and decide stays pure', () => {
|
|
117
|
-
const lib = library('suppress')
|
|
118
|
-
const you = graph.createEntity({ libraryId: lib, name: '小檀', kind: 'person', status: 'confirmed' })
|
|
119
|
-
const snapshot = index({ 'mem-a': entry('小檀来了', '小檀进门坐下。') })
|
|
120
|
-
const first = decide.decideAutoLinks({ index: snapshot, entities: [you] })
|
|
121
|
-
const second = decide.decideAutoLinks({ index: snapshot, entities: [you] })
|
|
122
|
-
assert.deepEqual(second, first, 'same input -> same output')
|
|
123
|
-
const key = suppression.suppressionKey(first[0])
|
|
124
|
-
const suppressed = decide.decideAutoLinks({ index: snapshot, entities: [you], suppression: new Set([key]) })
|
|
125
|
-
assert.deepEqual(suppressed, [])
|
|
126
|
-
})
|
|
127
|
-
|
|
128
|
-
test('field evidence builds conflict, supersede and fact-key edges without cliques', () => {
|
|
129
|
-
const lib = library('fields')
|
|
130
|
-
const snapshot = index({
|
|
131
|
-
'mem-a': entry('说法一', '内容一。', { conflicts_with: 'mem-b' }),
|
|
132
|
-
'mem-b': entry('说法二', '内容二。', { conflicts_with: 'mem-a' }),
|
|
133
|
-
'mem-c': entry('新版本', '更新后的说法。', { supersedes: 'mem-d' }),
|
|
134
|
-
'mem-d': entry('旧版本', '旧的说明。', { status: 'superseded' }),
|
|
135
|
-
'mem-e': entry('同一事实 1', '第一版。', { fact_key: 'k1', context: { worldId: 'w1' }, createdAt: at(1), updatedAt: at(1), version: 1 }),
|
|
136
|
-
'mem-f': entry('同一事实 2', '第二版。', { fact_key: 'k1', context: { worldId: 'w1' }, createdAt: at(2), updatedAt: at(2), version: 2 }),
|
|
137
|
-
'mem-g': entry('同一事实 3', '第三版。', { fact_key: 'k1', context: { worldId: 'w1' }, createdAt: at(3), updatedAt: at(3), version: 3, conflicts_with: 'mem-f' }),
|
|
138
|
-
})
|
|
139
|
-
const edges = decide.decideAutoLinks({ index: snapshot, entities: [] })
|
|
140
|
-
const conflicts = edges.filter(edge => edge.rule === 'conflict')
|
|
141
|
-
assert.equal(conflicts.length, 3, 'a<->b plus g->f (the reverse f->g is not a field)')
|
|
142
|
-
assert.ok(conflicts.some(edge => edge.from === 'mem-a' && edge.to === 'mem-b' && edge.kind === 'conflict'))
|
|
143
|
-
assert.ok(conflicts.some(edge => edge.from === 'mem-b' && edge.to === 'mem-a'))
|
|
144
|
-
assert.ok(conflicts.some(edge => edge.from === 'mem-g' && edge.to === 'mem-f'))
|
|
145
|
-
const supersedes = edges.filter(edge => edge.rule === 'supersede')
|
|
146
|
-
assert.deepEqual(supersedes.map(edge => `${edge.from}->${edge.to}:${edge.kind}`), ['mem-c->mem-d:supersede'])
|
|
147
|
-
const factKey = edges.filter(edge => edge.rule === 'fact-key')
|
|
148
|
-
assert.deepEqual(factKey.map(edge => `${edge.from}->${edge.to}`), ['mem-f->mem-e'], 'chain, not a clique; g-f is already a conflict')
|
|
149
|
-
})
|
|
150
|
-
|
|
151
|
-
// ---------------------------------------------------------------- write layer
|
|
152
|
-
|
|
153
|
-
test('apply writes evidence and never overwrites a manual edge', () => {
|
|
154
|
-
const lib = library('apply')
|
|
155
|
-
const you = graph.createEntity({ libraryId: lib, name: '小檀', kind: 'person', status: 'confirmed' })
|
|
156
|
-
state.savePrivateIndex(lib, index({
|
|
157
|
-
'mem-a': entry('小檀来了', '小檀进门坐下。'),
|
|
158
|
-
'mem-b': entry('小檀与阿檀', '小檀提起阿檀。', {
|
|
159
|
-
links: [{ to: you.id, kind: 'person', note: '用户手动确认过这条关系。', confidence: 0.9 }],
|
|
160
|
-
}),
|
|
161
|
-
}))
|
|
162
|
-
const edges = decide.decideAutoLinks({ index: state.loadPrivateIndex(lib), entities: [you] })
|
|
163
|
-
const applied = apply.applyAutoEdges({ libraryId: lib, edges, entities: [you] })
|
|
164
|
-
assert.equal(applied.added, 1)
|
|
165
|
-
assert.equal(applied.skippedManual, 1, 'the manual memory→entity edge wins')
|
|
166
|
-
const after = state.loadPrivateIndex(lib)
|
|
167
|
-
const link = after.entries['mem-a'].links[0]
|
|
168
|
-
assert.equal(link.source, 'auto')
|
|
169
|
-
assert.equal(link.confidence, 1)
|
|
170
|
-
assert.equal(link.evidence.rule, 'entity-mention')
|
|
171
|
-
assert.equal(link.evidence.entityId, you.id)
|
|
172
|
-
assert.match(link.note, /已确认实体/)
|
|
173
|
-
assert.equal(after.entries['mem-b'].links.length, 1)
|
|
174
|
-
assert.equal(after.entries['mem-b'].links[0].source, undefined, 'the manual edge is untouched')
|
|
175
|
-
const again = apply.applyAutoEdges({ libraryId: lib, edges, entities: [you] })
|
|
176
|
-
assert.equal(again.added, 0, 'the same decision is a no-op')
|
|
177
|
-
assert.equal(again.updated, 0)
|
|
178
|
-
})
|
|
179
|
-
|
|
180
|
-
// ---------------------------------------------------------------- entity lifecycle
|
|
181
|
-
|
|
182
|
-
test('confirming an entity flips its status and backfills mention edges immediately', () => {
|
|
183
|
-
const lib = library('confirm')
|
|
184
|
-
const you = graph.createEntity({ libraryId: lib, name: '小檀', kind: 'person' })
|
|
185
|
-
assert.equal(you.status, 'provisional', 'creation alone does not confirm')
|
|
186
|
-
state.savePrivateIndex(lib, index({
|
|
187
|
-
'mem-a': entry('小檀来了', '小檀进门坐下。'),
|
|
188
|
-
'mem-b': entry('小檀的伞', '小檀把伞落在门口。'),
|
|
189
|
-
}))
|
|
190
|
-
const confirmed = graph.confirmEntities({ libraryId: lib, entityIds: [you.id], by: 'user' })
|
|
191
|
-
assert.deepEqual(confirmed.confirmed, [you.id])
|
|
192
|
-
assert.equal(confirmed.entities, 2, 'both memories gained an edge in the same action')
|
|
193
|
-
const store = graph.graphExport(lib)
|
|
194
|
-
assert.equal(store.entities[you.id].status, 'confirmed')
|
|
195
|
-
assert.equal(store.entities[you.id].confirmedBy, 'user')
|
|
196
|
-
assert.ok(store.entities[you.id].confirmedAt)
|
|
197
|
-
const rows = state.loadPrivateIndex(lib)
|
|
198
|
-
assert.ok((rows.entries['mem-a'].links || []).some(link => link.to === you.id && link.evidence?.rule === 'entity-mention'))
|
|
199
|
-
const again = graph.confirmEntities({ libraryId: lib, entityIds: [you.id], by: 'user' })
|
|
200
|
-
assert.deepEqual(again.confirmed, [])
|
|
201
|
-
assert.equal(again.entities, 0, 'confirming twice is a no-op')
|
|
202
|
-
})
|
|
203
|
-
|
|
204
|
-
test('renaming a confirmed entity retires stale mention edges and attaches the new surface', () => {
|
|
205
|
-
const lib = library('rename')
|
|
206
|
-
const you = graph.createEntity({ libraryId: lib, name: '小檀', kind: 'person' })
|
|
207
|
-
state.savePrivateIndex(lib, index({
|
|
208
|
-
'mem-a': entry('小檀来了', '小檀进门坐下。'),
|
|
209
|
-
'mem-b': entry('檀子来了', '檀子带着伞。'),
|
|
210
|
-
}))
|
|
211
|
-
graph.confirmEntities({ libraryId: lib, entityIds: [you.id], by: 'user' })
|
|
212
|
-
assert.ok((state.loadPrivateIndex(lib).entries['mem-a'].links || []).some(link => link.to === you.id))
|
|
213
|
-
graph.renameEntity({ libraryId: lib, entityId: you.id, name: '檀子', aliases: [] })
|
|
214
|
-
const rows = state.loadPrivateIndex(lib)
|
|
215
|
-
assert.equal((rows.entries['mem-a'].links || []).some(link => link.to === you.id), false, 'old name no longer matches')
|
|
216
|
-
assert.ok((rows.entries['mem-b'].links || []).some(link => link.to === you.id && link.evidence?.surface === '檀子'))
|
|
217
|
-
})
|
|
218
|
-
|
|
219
|
-
test('merging a confirmed source into a provisional target confirms it and remaps evidence', () => {
|
|
220
|
-
const lib = library('merge')
|
|
221
|
-
const source = graph.createEntity({ libraryId: lib, name: '小檀', kind: 'person' })
|
|
222
|
-
state.savePrivateIndex(lib, index({ 'mem-a': entry('小檀来了', '小檀进门坐下。') }))
|
|
223
|
-
graph.confirmEntities({ libraryId: lib, entityIds: [source.id], by: 'user' })
|
|
224
|
-
const target = graph.createEntity({ libraryId: lib, name: '檀子', kind: 'person' })
|
|
225
|
-
graph.mergeEntities({ libraryId: lib, targetId: target.id, sourceIds: [source.id] })
|
|
226
|
-
const store = graph.graphExport(lib)
|
|
227
|
-
assert.equal(store.entities[target.id].status, 'confirmed', 'a confirmed source confirms the surviving row')
|
|
228
|
-
assert.deepEqual(store.entities[target.id].aliases, ['小檀'])
|
|
229
|
-
const link = (state.loadPrivateIndex(lib).entries['mem-a'].links || []).find(item => item.to === target.id)
|
|
230
|
-
assert.ok(link, 'the mention edge points at the merged row')
|
|
231
|
-
assert.equal(link.evidence.entityId, target.id)
|
|
232
|
-
})
|
|
233
|
-
|
|
234
|
-
test('deleting an entity records suppression for the auto edges it collects', () => {
|
|
235
|
-
const lib = library('delete')
|
|
236
|
-
const you = graph.createEntity({ libraryId: lib, name: '小檀', kind: 'person' })
|
|
237
|
-
state.savePrivateIndex(lib, index({ 'mem-a': entry('小檀来了', '小檀进门坐下。') }))
|
|
238
|
-
graph.confirmEntities({ libraryId: lib, entityIds: [you.id], by: 'user' })
|
|
239
|
-
graph.deleteEntity({ libraryId: lib, entityId: you.id, force: true })
|
|
240
|
-
const store = suppression.readSuppression(lib)
|
|
241
|
-
const keys = Object.keys(store.entries)
|
|
242
|
-
assert.ok(keys.includes(`mem-a|${you.id}|person|entity-mention`), 'the collected edge key is suppressed: ' + keys.join(','))
|
|
243
|
-
})
|
|
244
|
-
|
|
245
|
-
test('removing an auto edge by hand suppresses it against the next recompute', () => {
|
|
246
|
-
const lib = library('unlink')
|
|
247
|
-
const you = graph.createEntity({ libraryId: lib, name: '小檀', kind: 'person' })
|
|
248
|
-
state.savePrivateIndex(lib, index({ 'mem-a': entry('小檀来了', '小檀进门坐下。') }))
|
|
249
|
-
graph.confirmEntities({ libraryId: lib, entityIds: [you.id], by: 'user' })
|
|
250
|
-
const removed = graph.unlinkEntry({ libraryId: lib, fromId: 'mem-a', toId: you.id, kind: 'person' })
|
|
251
|
-
assert.equal(removed.removed, 1)
|
|
252
|
-
const set = suppression.suppressionSet(lib)
|
|
253
|
-
assert.ok(set.has(`mem-a|${you.id}|person|entity-mention`))
|
|
254
|
-
assert.ok(set.has(`mem-a|${you.id}|person|*`), 'the triple wildcard is written too')
|
|
255
|
-
apply.refreshEntityMentions({ libraryId: lib, entity: graph.graphExport(lib).entities[you.id], suppression: set })
|
|
256
|
-
assert.equal((state.loadPrivateIndex(lib).entries['mem-a'].links || []).some(link => link.to === you.id), false, 'suppressed edge stays gone')
|
|
257
|
-
})
|
|
258
|
-
|
|
259
|
-
test('deleting a legacy auto edge also blocks the later entity-mention edge (P6)', () => {
|
|
260
|
-
const lib = library('legacy-suppress')
|
|
261
|
-
const you = graph.createEntity({ libraryId: lib, name: '小檀', kind: 'person' })
|
|
262
|
-
state.savePrivateIndex(lib, index({
|
|
263
|
-
'mem-a': entry('小檀来了', '小檀进门坐下。', {
|
|
264
|
-
links: [{ to: you.id, kind: 'person', note: '旧算法自动建立的实体枢纽。', confidence: 0.65, source: 'auto' }],
|
|
265
|
-
}),
|
|
266
|
-
}))
|
|
267
|
-
const removed = graph.unlinkEntry({ libraryId: lib, fromId: 'mem-a', toId: you.id, kind: 'person' })
|
|
268
|
-
assert.equal(removed.removed, 1)
|
|
269
|
-
const set = suppression.suppressionSet(lib)
|
|
270
|
-
assert.ok(set.has(`mem-a|${you.id}|person|auto`), 'the legacy edge had no rule')
|
|
271
|
-
// Confirming the entity would normally rebuild the mention edge; the deleted
|
|
272
|
-
// fact must stay deleted even though the rule string changed.
|
|
273
|
-
const result = graph.confirmEntities({ libraryId: lib, entityIds: [you.id], by: 'user' })
|
|
274
|
-
assert.equal(result.entities, 0)
|
|
275
|
-
assert.equal((state.loadPrivateIndex(lib).entries['mem-a'].links || []).length, 0, 'the edge does not come back under entity-mention')
|
|
276
|
-
})
|
|
277
|
-
|
|
278
|
-
test('purgeAutoLinks removes only auto edges, keeps a manual edge byte-for-byte and suppresses', () => {
|
|
279
|
-
const lib = library('purge')
|
|
280
|
-
state.savePrivateIndex(lib, index({
|
|
281
|
-
'mem-a': entry('a', 'a', {
|
|
282
|
-
links: [
|
|
283
|
-
{ to: 'mem-b', kind: 'relation', note: '手动边,必须原样保留。', confidence: 0.7 },
|
|
284
|
-
{ to: 'mem-b', kind: 'event', note: '自动边,应被清理。', confidence: 1, source: 'auto', evidence: { rule: 'fact-key', field: 'fact_key' } },
|
|
285
|
-
],
|
|
286
|
-
}),
|
|
287
|
-
'mem-b': entry('b', 'b'),
|
|
288
|
-
}))
|
|
289
|
-
const before = JSON.parse(JSON.stringify(state.loadPrivateIndex(lib).entries['mem-a'].links[0]))
|
|
290
|
-
const result = graph.purgeAutoLinks({ libraryId: lib })
|
|
291
|
-
assert.equal(result.removed, 1)
|
|
292
|
-
const after = state.loadPrivateIndex(lib).entries['mem-a'].links
|
|
293
|
-
assert.equal(after.length, 1)
|
|
294
|
-
assert.deepEqual(after[0], before, 'the manual edge object is untouched')
|
|
295
|
-
assert.ok(suppression.suppressionSet(lib).has('mem-a|mem-b|event|*'), 'the removed pair is suppressed')
|
|
296
|
-
})
|
|
297
|
-
|
|
298
|
-
test('off mode closes explicit mention maintenance too, and apply rejects unknown rules', () => {
|
|
299
|
-
const lib = library('off-mode')
|
|
300
|
-
const you = graph.createEntity({ libraryId: lib, name: '小檀', kind: 'person' })
|
|
301
|
-
state.savePrivateIndex(lib, index({ 'mem-a': entry('小檀来了', '小檀进门坐下。') }))
|
|
302
|
-
state.updateSettings({ autoLinkMode: 'off' })
|
|
303
|
-
try {
|
|
304
|
-
const result = graph.confirmEntities({ libraryId: lib, entityIds: [you.id], by: 'user' })
|
|
305
|
-
assert.deepEqual(result.confirmed, [you.id], 'status still changes')
|
|
306
|
-
assert.equal(result.entities, 0, 'off writes no edge')
|
|
307
|
-
assert.equal((state.loadPrivateIndex(lib).entries['mem-a'].links || []).length, 0)
|
|
308
|
-
// A hand-supplied edge outside the evidence vocabulary is refused by the writer.
|
|
309
|
-
state.savePrivateIndex(lib, index({ 'mem-a': entry('小檀来了', '小檀进门坐下。') }))
|
|
310
|
-
const applied = apply.applyAutoEdges({
|
|
311
|
-
libraryId: lib,
|
|
312
|
-
entities: [you],
|
|
313
|
-
edges: [{ from: 'mem-a', to: you.id, kind: 'person', rule: 'made-up', evidence: { rule: 'made-up' } }],
|
|
314
|
-
})
|
|
315
|
-
assert.equal(applied.skipped, 1)
|
|
316
|
-
assert.equal((state.loadPrivateIndex(lib).entries['mem-a'].links || []).length, 0)
|
|
317
|
-
} finally {
|
|
318
|
-
state.updateSettings({ autoLinkMode: 'suggest' })
|
|
319
|
-
}
|
|
320
|
-
})
|
|
321
|
-
|
|
322
|
-
test('clearLinkSuppression removes markers and the deterministic rebuild restores the edge', async () => {
|
|
323
|
-
const lib = library('clear-suppress')
|
|
324
|
-
const you = graph.createEntity({ libraryId: lib, name: '小檀', kind: 'person' })
|
|
325
|
-
state.savePrivateIndex(lib, index({ 'mem-a': entry('小檀来了', '小檀进门坐下。') }))
|
|
326
|
-
state.updateSettings({ autoLinkMode: 'deterministic' })
|
|
327
|
-
try {
|
|
328
|
-
graph.confirmEntities({ libraryId: lib, entityIds: [you.id], by: 'user' })
|
|
329
|
-
graph.unlinkEntry({ libraryId: lib, fromId: 'mem-a', toId: you.id, kind: 'person' })
|
|
330
|
-
assert.ok(suppression.suppressionSet(lib).size > 0)
|
|
331
|
-
assert.equal((state.loadPrivateIndex(lib).entries['mem-a'].links || []).length, 0)
|
|
332
|
-
const cleared = await rpc.dispatch({}, 'clearLinkSuppression', { libraryId: lib })
|
|
333
|
-
assert.ok(cleared.cleared >= 1)
|
|
334
|
-
assert.equal(suppression.suppressionSet(lib).size, 0)
|
|
335
|
-
assert.ok((state.loadPrivateIndex(lib).entries['mem-a'].links || []).some(link => link.to === you.id), 'the full rebuild restores the deleted edge')
|
|
336
|
-
} finally {
|
|
337
|
-
state.updateSettings({ autoLinkMode: 'suggest' })
|
|
338
|
-
}
|
|
339
|
-
})
|
|
340
|
-
|
|
341
|
-
test('switching to deterministic backfills entities confirmed while off', async () => {
|
|
342
|
-
const lib = library('mode-switch')
|
|
343
|
-
const you = graph.createEntity({ libraryId: lib, name: '小檀', kind: 'person' })
|
|
344
|
-
state.savePrivateIndex(lib, index({ 'mem-a': entry('小檀来了', '小檀进门坐下。') }))
|
|
345
|
-
state.updateSettings({ autoLinkMode: 'off' })
|
|
346
|
-
try {
|
|
347
|
-
const off = graph.confirmEntities({ libraryId: lib, entityIds: [you.id], by: 'user' })
|
|
348
|
-
assert.equal(off.entities, 0)
|
|
349
|
-
assert.equal((state.loadPrivateIndex(lib).entries['mem-a'].links || []).length, 0)
|
|
350
|
-
await rpc.dispatch({}, 'setSettings', { patch: { autoLinkMode: 'deterministic' } })
|
|
351
|
-
const links = state.loadPrivateIndex(lib).entries['mem-a'].links || []
|
|
352
|
-
assert.ok(links.some(link => link.to === you.id && link.evidence?.rule === 'entity-mention'), 'the switch rebuilds missing mentions')
|
|
353
|
-
} finally {
|
|
354
|
-
state.updateSettings({ autoLinkMode: 'suggest' })
|
|
355
|
-
}
|
|
356
|
-
})
|
|
357
|
-
|
|
358
|
-
test('re-confirming an already-confirmed entity backfills missing mentions', () => {
|
|
359
|
-
const lib = library('reconfirm')
|
|
360
|
-
const you = graph.createEntity({ libraryId: lib, name: '小檀', kind: 'person', status: 'confirmed' })
|
|
361
|
-
state.savePrivateIndex(lib, index({ 'mem-a': entry('小檀来了', '小檀进门坐下。') }))
|
|
362
|
-
const result = graph.confirmEntities({ libraryId: lib, entityIds: [you.id], by: 'user' })
|
|
363
|
-
assert.deepEqual(result.confirmed, [], 'it was already confirmed')
|
|
364
|
-
assert.equal(result.entities, 1, 'the refresh still runs and attaches the edge')
|
|
365
|
-
assert.ok((state.loadPrivateIndex(lib).entries['mem-a'].links || []).some(link => link.to === you.id))
|
|
366
|
-
})
|
|
367
|
-
|
|
368
|
-
test('three automatic passes in suggest mode add no edges', async () => {
|
|
369
|
-
state.updateSettings({ autoLinkMode: 'suggest' })
|
|
370
|
-
try {
|
|
371
|
-
const lib = library('suggest-three')
|
|
372
|
-
graph.createEntity({ libraryId: lib, name: '小檀', kind: 'person', status: 'confirmed' })
|
|
373
|
-
state.savePrivateIndex(lib, index({ 'mem-a': entry('小檀来了', '小檀进门坐下。') }))
|
|
374
|
-
for (let pass = 0; pass < 3; pass += 1) {
|
|
375
|
-
const result = await privateLinks.refreshPrivateAutoLinks({ libraryId: lib, ids: ['mem-a'] })
|
|
376
|
-
assert.equal(result.skipped, 'suggest')
|
|
377
|
-
}
|
|
378
|
-
assert.equal((state.loadPrivateIndex(lib).entries['mem-a'].links || []).length, 0)
|
|
379
|
-
} finally {
|
|
380
|
-
state.updateSettings({ autoLinkMode: 'suggest' })
|
|
381
|
-
}
|
|
382
|
-
})
|
|
383
|
-
|
|
384
|
-
test('confirming eight entities backfills all mentions in one batch (8 x 9 = 72)', () => {
|
|
385
|
-
const lib = library('batch-confirm')
|
|
386
|
-
const names = Array.from({ length: 8 }, (_, i) => `角色${i + 1}`)
|
|
387
|
-
const entities = names.map(name => graph.createEntity({ libraryId: lib, name, kind: 'person' }))
|
|
388
|
-
const entries = {}
|
|
389
|
-
for (let i = 0; i < 9; i += 1) entries[`mem-${i}`] = entry(`第 ${i} 场`, names.join('、') + ' 都在场。')
|
|
390
|
-
state.savePrivateIndex(lib, index(entries))
|
|
391
|
-
const result = graph.confirmEntities({ libraryId: lib, entityIds: entities.map(entity => entity.id), by: 'user' })
|
|
392
|
-
assert.equal(result.confirmed.length, 8)
|
|
393
|
-
assert.equal(result.entities, 72, '9 memories x 8 entities')
|
|
394
|
-
const second = graph.confirmEntities({ libraryId: lib, entityIds: entities.map(entity => entity.id), by: 'user' })
|
|
395
|
-
assert.deepEqual(second.confirmed, [])
|
|
396
|
-
assert.equal(second.entities, 0)
|
|
397
|
-
})
|
|
398
|
-
|
|
399
|
-
test('a broken entity store makes the automatic refresh fail open (warn, no throw)', async () => {
|
|
400
|
-
const lib = library('broken-entities')
|
|
401
|
-
state.savePrivateIndex(lib, index({ 'mem-a': entry('小檀来了', '小檀进门坐下。') }))
|
|
402
|
-
const file = path.join(home, 'index', `${lib}.entities.json`)
|
|
403
|
-
fs.mkdirSync(path.dirname(file), { recursive: true })
|
|
404
|
-
fs.writeFileSync(file, '{not json', 'utf8')
|
|
405
|
-
state.updateSettings({ autoLinkMode: 'deterministic' })
|
|
406
|
-
try {
|
|
407
|
-
const result = await privateLinks.refreshPrivateAutoLinks({ libraryId: lib, ids: ['mem-a'] })
|
|
408
|
-
assert.equal(result.skipped, 'entities')
|
|
409
|
-
assert.equal(result.applied, null)
|
|
410
|
-
} finally {
|
|
411
|
-
state.updateSettings({ autoLinkMode: 'suggest' })
|
|
412
|
-
fs.rmSync(file, { force: true })
|
|
413
|
-
}
|
|
414
|
-
})
|
|
415
|
-
|
|
416
|
-
test('field evidence stays linear: 10k full rebuild and a 10-id incremental stay in budget', () => {
|
|
417
|
-
const entries = {}
|
|
418
|
-
for (let i = 0; i < 10000; i += 1) {
|
|
419
|
-
const id = `mem-${String(i).padStart(5, '0')}`
|
|
420
|
-
const row = entry(`记忆 ${i}`, `第 ${i} 条正文。`, { createdAt: at(1), updatedAt: at(1) })
|
|
421
|
-
if (i % 10 === 1) row.conflicts_with = `mem-${String(i - 1).padStart(5, '0')}`
|
|
422
|
-
if (i % 20 === 1) {
|
|
423
|
-
// 200-row fact families stress the grouping/sort path as well as the count.
|
|
424
|
-
row.fact_key = `k${Math.floor(i / 200)}`
|
|
425
|
-
row.context = { worldId: `w${Math.floor(i / 200)}` }
|
|
426
|
-
row.version = i % 200
|
|
427
|
-
}
|
|
428
|
-
entries[id] = row
|
|
429
|
-
}
|
|
430
|
-
const snapshot = index(entries)
|
|
431
|
-
const startedFull = performance.now()
|
|
432
|
-
const full = decide.fieldEvidenceEdges(snapshot)
|
|
433
|
-
const fullMs = performance.now() - startedFull
|
|
434
|
-
const sample = Object.keys(entries).slice(0, 10)
|
|
435
|
-
const startedInc = performance.now()
|
|
436
|
-
const incremental = decide.fieldEvidenceEdgesFor(snapshot, sample)
|
|
437
|
-
const incMs = performance.now() - startedInc
|
|
438
|
-
assert.ok(full.length > 900, 'the fixture really produces conflict/fact-key edges: ' + full.length)
|
|
439
|
-
assert.ok(incremental.length > 0)
|
|
440
|
-
assert.ok(fullMs < 5000, `10k full rebuild took ${Math.round(fullMs)} ms (design budget <5000)`)
|
|
441
|
-
assert.ok(incMs < 500, `10-id incremental took ${Math.round(incMs)} ms (design budget <50, allow CI slack)`)
|
|
442
|
-
})
|
|
443
|
-
|
|
444
|
-
// ---------------------------------------------------------------- write-path hooks
|
|
445
|
-
|
|
446
|
-
test('a private write carrying conflicts_with gets its evidence edges immediately', async () => {
|
|
447
|
-
state.updateSettings({ autoLinkMode: 'deterministic' })
|
|
448
|
-
try {
|
|
449
|
-
const cwd = path.join(home, 'ws-write-hook')
|
|
450
|
-
const binding = state.ensureSession('write-hook', { cwd, agentPreset: 'rp' })
|
|
451
|
-
const exec = { agent: { session: { header: { id: 'write-hook', cwd, agentPreset: 'rp' } } } }
|
|
452
|
-
const first = await memory.privateRemember({ exec, title: '周五安排', content: '旧说法:周五去书店。' })
|
|
453
|
-
const second = await memory.privateRemember({ exec, title: '周五安排', content: '新说法:周五改去河边。', conflictsWith: first.id })
|
|
454
|
-
assert.equal(second.conflictsWith, first.id)
|
|
455
|
-
const index = state.loadPrivateIndex(binding.libraryId)
|
|
456
|
-
const forward = (index.entries[second.id].links || []).find(link => link.to === first.id)
|
|
457
|
-
assert.ok(forward, 'the new entry links to the conflicting version')
|
|
458
|
-
assert.equal(forward.kind, 'conflict')
|
|
459
|
-
assert.equal(forward.evidence?.rule, 'conflict')
|
|
460
|
-
assert.equal(forward.confidence, 1)
|
|
461
|
-
const reverse = (index.entries[first.id].links || []).find(link => link.to === second.id)
|
|
462
|
-
assert.ok(reverse, 'memory.js writes the reverse field; the reverse edge follows')
|
|
463
|
-
assert.equal(reverse.evidence?.rule, 'conflict')
|
|
464
|
-
} finally {
|
|
465
|
-
state.updateSettings({ autoLinkMode: 'suggest' })
|
|
466
|
-
}
|
|
467
|
-
})
|
|
468
|
-
|
|
469
|
-
test('review accept refreshes the supersede edge immediately and stays silent in suggest mode', async () => {
|
|
470
|
-
const lib = library('review-hook')
|
|
471
|
-
const seed = () => index({
|
|
472
|
-
'mem-old': entry('周五安排', '旧说法:周五去书店。'),
|
|
473
|
-
'mem-new': entry('周五安排', '新说法:周五改去河边。', { status: 'pending_review', conflictingWith: 'mem-old' }),
|
|
474
|
-
})
|
|
475
|
-
state.savePrivateIndex(lib, seed())
|
|
476
|
-
state.updateSettings({ autoLinkMode: 'deterministic' })
|
|
477
|
-
try {
|
|
478
|
-
await memory.resolveMemoryReview({ libraryId: lib, id: 'mem-new', action: 'accept', resolution: 'replace', sessionId: null })
|
|
479
|
-
const links = state.loadPrivateIndex(lib).entries['mem-new'].links || []
|
|
480
|
-
const edge = links.find(link => link.to === 'mem-old')
|
|
481
|
-
assert.ok(edge, 'the supersede edge is written with the review commit')
|
|
482
|
-
assert.equal(edge.kind, 'supersede')
|
|
483
|
-
assert.equal(edge.evidence?.rule, 'supersede')
|
|
484
|
-
|
|
485
|
-
state.savePrivateIndex(lib, seed())
|
|
486
|
-
state.updateSettings({ autoLinkMode: 'suggest' })
|
|
487
|
-
await memory.resolveMemoryReview({ libraryId: lib, id: 'mem-new', action: 'accept', resolution: 'replace', sessionId: null })
|
|
488
|
-
const after = state.loadPrivateIndex(lib).entries['mem-new'].links || []
|
|
489
|
-
assert.equal(after.some(link => link.evidence?.rule === 'supersede'), false, 'suggest mode never writes an edge')
|
|
490
|
-
} finally {
|
|
491
|
-
state.updateSettings({ autoLinkMode: 'suggest' })
|
|
492
|
-
}
|
|
493
|
-
})
|
|
494
|
-
|
|
495
|
-
// ---------------------------------------------------------------- recall governance
|
|
496
|
-
|
|
497
|
-
test('recallRelations ignores entity-mention edges even at confidence 1.0 but keeps field evidence', () => {
|
|
498
|
-
const lib = library('recall')
|
|
499
|
-
const you = graph.createEntity({ libraryId: lib, name: '小檀', kind: 'person', status: 'confirmed' })
|
|
500
|
-
state.savePrivateIndex(lib, index({
|
|
501
|
-
'mem-a': entry('小檀来了', '小檀进门坐下。', {
|
|
502
|
-
links: [
|
|
503
|
-
{ to: you.id, kind: 'person', note: '提及已确认实体「小檀」(标题)。', confidence: 1, source: 'auto', evidence: { rule: 'entity-mention', entityId: you.id, surface: '小檀', field: 'title', offset: 0 } },
|
|
504
|
-
{ to: 'mem-b', kind: 'conflict', note: '字段证据:conflicts_with 指向这条记忆。', confidence: 1, source: 'auto', evidence: { rule: 'conflict', field: 'conflicts_with' } },
|
|
505
|
-
],
|
|
506
|
-
}),
|
|
507
|
-
'mem-b': entry('另一种说法', '内容不同。'),
|
|
508
|
-
}))
|
|
509
|
-
const related = graph.recallRelations(lib, state.loadPrivateIndex(lib), 'mem-a', null)
|
|
510
|
-
assert.deepEqual(related.map(item => item.id), ['mem-b'], 'the mention edge never takes a top-2 seat')
|
|
511
|
-
assert.equal(related[0].kind, 'conflict')
|
|
512
|
-
})
|
|
1
|
+
import test from 'node:test'
|
|
2
|
+
import assert from 'node:assert/strict'
|
|
3
|
+
import fs from 'node:fs'
|
|
4
|
+
import os from 'node:os'
|
|
5
|
+
import path from 'node:path'
|
|
6
|
+
import { performance } from 'node:perf_hooks'
|
|
7
|
+
|
|
8
|
+
const home = fs.mkdtempSync(path.join(os.tmpdir(), 'dsh-memu-memory-autolink-'))
|
|
9
|
+
process.env.DSH_MEMU_HOME = home
|
|
10
|
+
process.env.DSH_MEMU_MEMORY_HOME = home
|
|
11
|
+
|
|
12
|
+
const state = await import('../lib/state.js')
|
|
13
|
+
const memory = await import('../lib/memory.js')
|
|
14
|
+
const graph = await import('../lib/graph.js')
|
|
15
|
+
const decide = await import('../lib/auto-link/decide.js')
|
|
16
|
+
const apply = await import('../lib/auto-link/apply.js')
|
|
17
|
+
const suppression = await import('../lib/auto-link/suppression.js')
|
|
18
|
+
const normalize = await import('../lib/auto-link/normalize.js')
|
|
19
|
+
const policy = await import('../lib/auto-link/policy.js')
|
|
20
|
+
const privateLinks = await import('../lib/private-links.js')
|
|
21
|
+
const rpc = await import('../lib/rpc.js')
|
|
22
|
+
|
|
23
|
+
test.after(() => {
|
|
24
|
+
try { state.flushState() } catch {}
|
|
25
|
+
try { fs.rmSync(home, { recursive: true, force: true }) } catch {}
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
let counter = 0
|
|
29
|
+
function library(name) {
|
|
30
|
+
counter += 1
|
|
31
|
+
const binding = state.ensureSession(`autolink-${name}-${counter}`, { cwd: path.join(home, `ws-${name}-${counter}`), agentPreset: 'rp' })
|
|
32
|
+
return binding.libraryId
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const at = (day) => `2026-11-${String(day).padStart(2, '0')}T00:00:00.000Z`
|
|
36
|
+
|
|
37
|
+
function entry(title, content, extra = {}) {
|
|
38
|
+
return { title, content, status: 'active', visibility: 'shared', layer: 'long_term', createdAt: at(1), updatedAt: at(1), ...extra }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function index(entries) {
|
|
42
|
+
return { version: 1, entries }
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// ---------------------------------------------------------------- normalization
|
|
46
|
+
|
|
47
|
+
test('mentions are boundary-aware and punctuation-insensitive: CJK substrings, identifiers, NFKC folding', () => {
|
|
48
|
+
assert.equal(normalize.mentionCount('yuike 和 yuikeee', 'yuike'), 1, 'Latin must not match a longer identifier')
|
|
49
|
+
assert.equal(normalize.mentionCount('YUIKE!来了', 'yuike'), 1, 'full-width and punctuation fold away')
|
|
50
|
+
assert.equal(normalize.mentionCount('洛云澜来了,洛云澜走了', '洛云澜'), 2)
|
|
51
|
+
// §8.1: NFKC + 去标点/空白 applies to the name as well as the text.
|
|
52
|
+
assert.equal(normalize.mentionCount('小·檀来了', '小檀'), 1)
|
|
53
|
+
assert.equal(normalize.mentionCount('小 檀来了', '小檀'), 1)
|
|
54
|
+
assert.equal(normalize.mentionCount('小檀来了', '小 檀'), 1)
|
|
55
|
+
// §2: code identifiers keep their punctuation and their word boundaries.
|
|
56
|
+
assert.equal(normalize.mentionCount('Yuike_01 上线', 'Yuike_01'), 1)
|
|
57
|
+
assert.equal(normalize.mentionCount('yuike.dev is down', 'yuike.dev'), 1)
|
|
58
|
+
assert.equal(normalize.mentionCount('洛云澜-alex 来了', '洛云澜-alex'), 1)
|
|
59
|
+
assert.equal(normalize.firstMention('猫娘yuike', 'yuike'), 2)
|
|
60
|
+
assert.equal(normalize.firstMention('「小·檀」来了', '小檀'), 1, 'offset is the raw text position, not the compact index')
|
|
61
|
+
assert.equal(normalize.isMentionSurface('小'), false, 'one CJK character is not a surface')
|
|
62
|
+
assert.equal(normalize.isMentionSurface('小檀'), true)
|
|
63
|
+
assert.equal(normalize.isMentionSurface('a'), false, 'one Latin character is not a surface')
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
test('punctuated and identifier surfaces produce mention edges with raw offsets', () => {
|
|
67
|
+
const lib = library('punct')
|
|
68
|
+
const you = graph.createEntity({ libraryId: lib, name: '小檀', kind: 'person', status: 'confirmed' })
|
|
69
|
+
const yuike = graph.createEntity({ libraryId: lib, name: 'yuike_01', kind: 'person', status: 'confirmed' })
|
|
70
|
+
const mixed = graph.createEntity({ libraryId: lib, name: '洛云澜-alex', kind: 'person', status: 'confirmed' })
|
|
71
|
+
const snapshot = index({
|
|
72
|
+
'mem-a': entry('「小·檀」来了', '她带着伞。'),
|
|
73
|
+
'mem-b': entry('yuike_01 上线', 'yuike_01 迟到了。'),
|
|
74
|
+
'mem-c': entry('洛云澜-alex 到访', '洛云澜-alex 和 洛云澜 yuike 都来了。'),
|
|
75
|
+
})
|
|
76
|
+
const edges = decide.decideAutoLinks({ index: snapshot, entities: [you, yuike, mixed] })
|
|
77
|
+
const mention = edges.filter(edge => edge.rule === 'entity-mention')
|
|
78
|
+
assert.equal(mention.length, 3, JSON.stringify(mention.map(edge => `${edge.from}->${edge.to}`)))
|
|
79
|
+
for (const edge of edges) assert.ok(policy.EVIDENCE_RULES.includes(edge.rule), 'only the declared rule vocabulary: ' + edge.rule)
|
|
80
|
+
const byEntity = new Map(mention.map(edge => [edge.to, edge]))
|
|
81
|
+
const youEdge = byEntity.get(you.id)
|
|
82
|
+
assert.equal(youEdge.evidence.surface, '小檀')
|
|
83
|
+
assert.equal(youEdge.evidence.offset, 1, 'raw offset inside 「小·檀」')
|
|
84
|
+
assert.ok(byEntity.has(yuike.id), 'identifier with underscore is found')
|
|
85
|
+
assert.ok(byEntity.has(mixed.id), 'mixed CJK-Latin name is found')
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
// ---------------------------------------------------------------- decision layer
|
|
89
|
+
|
|
90
|
+
test('only confirmed entities produce mention edges; provisional, archived and secret rows do not', () => {
|
|
91
|
+
const lib = library('mention')
|
|
92
|
+
const you = graph.createEntity({ libraryId: lib, name: '小檀', kind: 'person', aliases: ['檀子'], status: 'confirmed', confirmedBy: 'user' })
|
|
93
|
+
graph.createEntity({ libraryId: lib, name: '沈砚舟', kind: 'person' })
|
|
94
|
+
graph.createEntity({ libraryId: lib, name: '阿兰', kind: 'person', status: 'archived' })
|
|
95
|
+
const snapshot = index({
|
|
96
|
+
'mem-a': entry('小檀今天来了', '她带了一篮水果。'),
|
|
97
|
+
'mem-b': entry('沈砚舟的日记', '沈砚舟写下了今天的安排。'),
|
|
98
|
+
'mem-c': entry('阿兰回信', '阿兰说下周再联系。'),
|
|
99
|
+
'mem-d': entry('小檀与秘密', '关于小檀的一件不能外传的事。', { visibility: 'secret' }),
|
|
100
|
+
'mem-e': entry('小檀旧事', '小檀很久以前来过。', { status: 'archived' }),
|
|
101
|
+
})
|
|
102
|
+
const edges = decide.decideAutoLinks({ index: snapshot, entities: [you, { id: 'ent-provisional', name: '沈砚舟', kind: 'person', status: 'provisional' }, { id: 'ent-archived', name: '阿兰', kind: 'person', status: 'archived' }] })
|
|
103
|
+
const mention = edges.filter(edge => edge.rule === 'entity-mention')
|
|
104
|
+
assert.equal(mention.length, 1, 'one eligible memory mentions the confirmed entity')
|
|
105
|
+
assert.equal(mention[0].from, 'mem-a')
|
|
106
|
+
assert.equal(mention[0].to, you.id)
|
|
107
|
+
assert.equal(mention[0].kind, 'person')
|
|
108
|
+
assert.equal(mention[0].confidence, 1)
|
|
109
|
+
assert.equal(mention[0].schemaVersion, 2)
|
|
110
|
+
assert.ok(mention[0].evidence.surface === '小檀' || mention[0].evidence.surface === '檀子')
|
|
111
|
+
assert.equal(mention[0].evidence.entityId, you.id)
|
|
112
|
+
assert.equal(mention[0].evidence.field, 'title')
|
|
113
|
+
assert.equal(mention[0].evidence.offset, 0)
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
test('suppression removes a decided edge and decide stays pure', () => {
|
|
117
|
+
const lib = library('suppress')
|
|
118
|
+
const you = graph.createEntity({ libraryId: lib, name: '小檀', kind: 'person', status: 'confirmed' })
|
|
119
|
+
const snapshot = index({ 'mem-a': entry('小檀来了', '小檀进门坐下。') })
|
|
120
|
+
const first = decide.decideAutoLinks({ index: snapshot, entities: [you] })
|
|
121
|
+
const second = decide.decideAutoLinks({ index: snapshot, entities: [you] })
|
|
122
|
+
assert.deepEqual(second, first, 'same input -> same output')
|
|
123
|
+
const key = suppression.suppressionKey(first[0])
|
|
124
|
+
const suppressed = decide.decideAutoLinks({ index: snapshot, entities: [you], suppression: new Set([key]) })
|
|
125
|
+
assert.deepEqual(suppressed, [])
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
test('field evidence builds conflict, supersede and fact-key edges without cliques', () => {
|
|
129
|
+
const lib = library('fields')
|
|
130
|
+
const snapshot = index({
|
|
131
|
+
'mem-a': entry('说法一', '内容一。', { conflicts_with: 'mem-b' }),
|
|
132
|
+
'mem-b': entry('说法二', '内容二。', { conflicts_with: 'mem-a' }),
|
|
133
|
+
'mem-c': entry('新版本', '更新后的说法。', { supersedes: 'mem-d' }),
|
|
134
|
+
'mem-d': entry('旧版本', '旧的说明。', { status: 'superseded' }),
|
|
135
|
+
'mem-e': entry('同一事实 1', '第一版。', { fact_key: 'k1', context: { worldId: 'w1' }, createdAt: at(1), updatedAt: at(1), version: 1 }),
|
|
136
|
+
'mem-f': entry('同一事实 2', '第二版。', { fact_key: 'k1', context: { worldId: 'w1' }, createdAt: at(2), updatedAt: at(2), version: 2 }),
|
|
137
|
+
'mem-g': entry('同一事实 3', '第三版。', { fact_key: 'k1', context: { worldId: 'w1' }, createdAt: at(3), updatedAt: at(3), version: 3, conflicts_with: 'mem-f' }),
|
|
138
|
+
})
|
|
139
|
+
const edges = decide.decideAutoLinks({ index: snapshot, entities: [] })
|
|
140
|
+
const conflicts = edges.filter(edge => edge.rule === 'conflict')
|
|
141
|
+
assert.equal(conflicts.length, 3, 'a<->b plus g->f (the reverse f->g is not a field)')
|
|
142
|
+
assert.ok(conflicts.some(edge => edge.from === 'mem-a' && edge.to === 'mem-b' && edge.kind === 'conflict'))
|
|
143
|
+
assert.ok(conflicts.some(edge => edge.from === 'mem-b' && edge.to === 'mem-a'))
|
|
144
|
+
assert.ok(conflicts.some(edge => edge.from === 'mem-g' && edge.to === 'mem-f'))
|
|
145
|
+
const supersedes = edges.filter(edge => edge.rule === 'supersede')
|
|
146
|
+
assert.deepEqual(supersedes.map(edge => `${edge.from}->${edge.to}:${edge.kind}`), ['mem-c->mem-d:supersede'])
|
|
147
|
+
const factKey = edges.filter(edge => edge.rule === 'fact-key')
|
|
148
|
+
assert.deepEqual(factKey.map(edge => `${edge.from}->${edge.to}`), ['mem-f->mem-e'], 'chain, not a clique; g-f is already a conflict')
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
// ---------------------------------------------------------------- write layer
|
|
152
|
+
|
|
153
|
+
test('apply writes evidence and never overwrites a manual edge', () => {
|
|
154
|
+
const lib = library('apply')
|
|
155
|
+
const you = graph.createEntity({ libraryId: lib, name: '小檀', kind: 'person', status: 'confirmed' })
|
|
156
|
+
state.savePrivateIndex(lib, index({
|
|
157
|
+
'mem-a': entry('小檀来了', '小檀进门坐下。'),
|
|
158
|
+
'mem-b': entry('小檀与阿檀', '小檀提起阿檀。', {
|
|
159
|
+
links: [{ to: you.id, kind: 'person', note: '用户手动确认过这条关系。', confidence: 0.9 }],
|
|
160
|
+
}),
|
|
161
|
+
}))
|
|
162
|
+
const edges = decide.decideAutoLinks({ index: state.loadPrivateIndex(lib), entities: [you] })
|
|
163
|
+
const applied = apply.applyAutoEdges({ libraryId: lib, edges, entities: [you] })
|
|
164
|
+
assert.equal(applied.added, 1)
|
|
165
|
+
assert.equal(applied.skippedManual, 1, 'the manual memory→entity edge wins')
|
|
166
|
+
const after = state.loadPrivateIndex(lib)
|
|
167
|
+
const link = after.entries['mem-a'].links[0]
|
|
168
|
+
assert.equal(link.source, 'auto')
|
|
169
|
+
assert.equal(link.confidence, 1)
|
|
170
|
+
assert.equal(link.evidence.rule, 'entity-mention')
|
|
171
|
+
assert.equal(link.evidence.entityId, you.id)
|
|
172
|
+
assert.match(link.note, /已确认实体/)
|
|
173
|
+
assert.equal(after.entries['mem-b'].links.length, 1)
|
|
174
|
+
assert.equal(after.entries['mem-b'].links[0].source, undefined, 'the manual edge is untouched')
|
|
175
|
+
const again = apply.applyAutoEdges({ libraryId: lib, edges, entities: [you] })
|
|
176
|
+
assert.equal(again.added, 0, 'the same decision is a no-op')
|
|
177
|
+
assert.equal(again.updated, 0)
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
// ---------------------------------------------------------------- entity lifecycle
|
|
181
|
+
|
|
182
|
+
test('confirming an entity flips its status and backfills mention edges immediately', () => {
|
|
183
|
+
const lib = library('confirm')
|
|
184
|
+
const you = graph.createEntity({ libraryId: lib, name: '小檀', kind: 'person' })
|
|
185
|
+
assert.equal(you.status, 'provisional', 'creation alone does not confirm')
|
|
186
|
+
state.savePrivateIndex(lib, index({
|
|
187
|
+
'mem-a': entry('小檀来了', '小檀进门坐下。'),
|
|
188
|
+
'mem-b': entry('小檀的伞', '小檀把伞落在门口。'),
|
|
189
|
+
}))
|
|
190
|
+
const confirmed = graph.confirmEntities({ libraryId: lib, entityIds: [you.id], by: 'user' })
|
|
191
|
+
assert.deepEqual(confirmed.confirmed, [you.id])
|
|
192
|
+
assert.equal(confirmed.entities, 2, 'both memories gained an edge in the same action')
|
|
193
|
+
const store = graph.graphExport(lib)
|
|
194
|
+
assert.equal(store.entities[you.id].status, 'confirmed')
|
|
195
|
+
assert.equal(store.entities[you.id].confirmedBy, 'user')
|
|
196
|
+
assert.ok(store.entities[you.id].confirmedAt)
|
|
197
|
+
const rows = state.loadPrivateIndex(lib)
|
|
198
|
+
assert.ok((rows.entries['mem-a'].links || []).some(link => link.to === you.id && link.evidence?.rule === 'entity-mention'))
|
|
199
|
+
const again = graph.confirmEntities({ libraryId: lib, entityIds: [you.id], by: 'user' })
|
|
200
|
+
assert.deepEqual(again.confirmed, [])
|
|
201
|
+
assert.equal(again.entities, 0, 'confirming twice is a no-op')
|
|
202
|
+
})
|
|
203
|
+
|
|
204
|
+
test('renaming a confirmed entity retires stale mention edges and attaches the new surface', () => {
|
|
205
|
+
const lib = library('rename')
|
|
206
|
+
const you = graph.createEntity({ libraryId: lib, name: '小檀', kind: 'person' })
|
|
207
|
+
state.savePrivateIndex(lib, index({
|
|
208
|
+
'mem-a': entry('小檀来了', '小檀进门坐下。'),
|
|
209
|
+
'mem-b': entry('檀子来了', '檀子带着伞。'),
|
|
210
|
+
}))
|
|
211
|
+
graph.confirmEntities({ libraryId: lib, entityIds: [you.id], by: 'user' })
|
|
212
|
+
assert.ok((state.loadPrivateIndex(lib).entries['mem-a'].links || []).some(link => link.to === you.id))
|
|
213
|
+
graph.renameEntity({ libraryId: lib, entityId: you.id, name: '檀子', aliases: [] })
|
|
214
|
+
const rows = state.loadPrivateIndex(lib)
|
|
215
|
+
assert.equal((rows.entries['mem-a'].links || []).some(link => link.to === you.id), false, 'old name no longer matches')
|
|
216
|
+
assert.ok((rows.entries['mem-b'].links || []).some(link => link.to === you.id && link.evidence?.surface === '檀子'))
|
|
217
|
+
})
|
|
218
|
+
|
|
219
|
+
test('merging a confirmed source into a provisional target confirms it and remaps evidence', () => {
|
|
220
|
+
const lib = library('merge')
|
|
221
|
+
const source = graph.createEntity({ libraryId: lib, name: '小檀', kind: 'person' })
|
|
222
|
+
state.savePrivateIndex(lib, index({ 'mem-a': entry('小檀来了', '小檀进门坐下。') }))
|
|
223
|
+
graph.confirmEntities({ libraryId: lib, entityIds: [source.id], by: 'user' })
|
|
224
|
+
const target = graph.createEntity({ libraryId: lib, name: '檀子', kind: 'person' })
|
|
225
|
+
graph.mergeEntities({ libraryId: lib, targetId: target.id, sourceIds: [source.id] })
|
|
226
|
+
const store = graph.graphExport(lib)
|
|
227
|
+
assert.equal(store.entities[target.id].status, 'confirmed', 'a confirmed source confirms the surviving row')
|
|
228
|
+
assert.deepEqual(store.entities[target.id].aliases, ['小檀'])
|
|
229
|
+
const link = (state.loadPrivateIndex(lib).entries['mem-a'].links || []).find(item => item.to === target.id)
|
|
230
|
+
assert.ok(link, 'the mention edge points at the merged row')
|
|
231
|
+
assert.equal(link.evidence.entityId, target.id)
|
|
232
|
+
})
|
|
233
|
+
|
|
234
|
+
test('deleting an entity records suppression for the auto edges it collects', () => {
|
|
235
|
+
const lib = library('delete')
|
|
236
|
+
const you = graph.createEntity({ libraryId: lib, name: '小檀', kind: 'person' })
|
|
237
|
+
state.savePrivateIndex(lib, index({ 'mem-a': entry('小檀来了', '小檀进门坐下。') }))
|
|
238
|
+
graph.confirmEntities({ libraryId: lib, entityIds: [you.id], by: 'user' })
|
|
239
|
+
graph.deleteEntity({ libraryId: lib, entityId: you.id, force: true })
|
|
240
|
+
const store = suppression.readSuppression(lib)
|
|
241
|
+
const keys = Object.keys(store.entries)
|
|
242
|
+
assert.ok(keys.includes(`mem-a|${you.id}|person|entity-mention`), 'the collected edge key is suppressed: ' + keys.join(','))
|
|
243
|
+
})
|
|
244
|
+
|
|
245
|
+
test('removing an auto edge by hand suppresses it against the next recompute', () => {
|
|
246
|
+
const lib = library('unlink')
|
|
247
|
+
const you = graph.createEntity({ libraryId: lib, name: '小檀', kind: 'person' })
|
|
248
|
+
state.savePrivateIndex(lib, index({ 'mem-a': entry('小檀来了', '小檀进门坐下。') }))
|
|
249
|
+
graph.confirmEntities({ libraryId: lib, entityIds: [you.id], by: 'user' })
|
|
250
|
+
const removed = graph.unlinkEntry({ libraryId: lib, fromId: 'mem-a', toId: you.id, kind: 'person' })
|
|
251
|
+
assert.equal(removed.removed, 1)
|
|
252
|
+
const set = suppression.suppressionSet(lib)
|
|
253
|
+
assert.ok(set.has(`mem-a|${you.id}|person|entity-mention`))
|
|
254
|
+
assert.ok(set.has(`mem-a|${you.id}|person|*`), 'the triple wildcard is written too')
|
|
255
|
+
apply.refreshEntityMentions({ libraryId: lib, entity: graph.graphExport(lib).entities[you.id], suppression: set })
|
|
256
|
+
assert.equal((state.loadPrivateIndex(lib).entries['mem-a'].links || []).some(link => link.to === you.id), false, 'suppressed edge stays gone')
|
|
257
|
+
})
|
|
258
|
+
|
|
259
|
+
test('deleting a legacy auto edge also blocks the later entity-mention edge (P6)', () => {
|
|
260
|
+
const lib = library('legacy-suppress')
|
|
261
|
+
const you = graph.createEntity({ libraryId: lib, name: '小檀', kind: 'person' })
|
|
262
|
+
state.savePrivateIndex(lib, index({
|
|
263
|
+
'mem-a': entry('小檀来了', '小檀进门坐下。', {
|
|
264
|
+
links: [{ to: you.id, kind: 'person', note: '旧算法自动建立的实体枢纽。', confidence: 0.65, source: 'auto' }],
|
|
265
|
+
}),
|
|
266
|
+
}))
|
|
267
|
+
const removed = graph.unlinkEntry({ libraryId: lib, fromId: 'mem-a', toId: you.id, kind: 'person' })
|
|
268
|
+
assert.equal(removed.removed, 1)
|
|
269
|
+
const set = suppression.suppressionSet(lib)
|
|
270
|
+
assert.ok(set.has(`mem-a|${you.id}|person|auto`), 'the legacy edge had no rule')
|
|
271
|
+
// Confirming the entity would normally rebuild the mention edge; the deleted
|
|
272
|
+
// fact must stay deleted even though the rule string changed.
|
|
273
|
+
const result = graph.confirmEntities({ libraryId: lib, entityIds: [you.id], by: 'user' })
|
|
274
|
+
assert.equal(result.entities, 0)
|
|
275
|
+
assert.equal((state.loadPrivateIndex(lib).entries['mem-a'].links || []).length, 0, 'the edge does not come back under entity-mention')
|
|
276
|
+
})
|
|
277
|
+
|
|
278
|
+
test('purgeAutoLinks removes only auto edges, keeps a manual edge byte-for-byte and suppresses', () => {
|
|
279
|
+
const lib = library('purge')
|
|
280
|
+
state.savePrivateIndex(lib, index({
|
|
281
|
+
'mem-a': entry('a', 'a', {
|
|
282
|
+
links: [
|
|
283
|
+
{ to: 'mem-b', kind: 'relation', note: '手动边,必须原样保留。', confidence: 0.7 },
|
|
284
|
+
{ to: 'mem-b', kind: 'event', note: '自动边,应被清理。', confidence: 1, source: 'auto', evidence: { rule: 'fact-key', field: 'fact_key' } },
|
|
285
|
+
],
|
|
286
|
+
}),
|
|
287
|
+
'mem-b': entry('b', 'b'),
|
|
288
|
+
}))
|
|
289
|
+
const before = JSON.parse(JSON.stringify(state.loadPrivateIndex(lib).entries['mem-a'].links[0]))
|
|
290
|
+
const result = graph.purgeAutoLinks({ libraryId: lib })
|
|
291
|
+
assert.equal(result.removed, 1)
|
|
292
|
+
const after = state.loadPrivateIndex(lib).entries['mem-a'].links
|
|
293
|
+
assert.equal(after.length, 1)
|
|
294
|
+
assert.deepEqual(after[0], before, 'the manual edge object is untouched')
|
|
295
|
+
assert.ok(suppression.suppressionSet(lib).has('mem-a|mem-b|event|*'), 'the removed pair is suppressed')
|
|
296
|
+
})
|
|
297
|
+
|
|
298
|
+
test('off mode closes explicit mention maintenance too, and apply rejects unknown rules', () => {
|
|
299
|
+
const lib = library('off-mode')
|
|
300
|
+
const you = graph.createEntity({ libraryId: lib, name: '小檀', kind: 'person' })
|
|
301
|
+
state.savePrivateIndex(lib, index({ 'mem-a': entry('小檀来了', '小檀进门坐下。') }))
|
|
302
|
+
state.updateSettings({ autoLinkMode: 'off' })
|
|
303
|
+
try {
|
|
304
|
+
const result = graph.confirmEntities({ libraryId: lib, entityIds: [you.id], by: 'user' })
|
|
305
|
+
assert.deepEqual(result.confirmed, [you.id], 'status still changes')
|
|
306
|
+
assert.equal(result.entities, 0, 'off writes no edge')
|
|
307
|
+
assert.equal((state.loadPrivateIndex(lib).entries['mem-a'].links || []).length, 0)
|
|
308
|
+
// A hand-supplied edge outside the evidence vocabulary is refused by the writer.
|
|
309
|
+
state.savePrivateIndex(lib, index({ 'mem-a': entry('小檀来了', '小檀进门坐下。') }))
|
|
310
|
+
const applied = apply.applyAutoEdges({
|
|
311
|
+
libraryId: lib,
|
|
312
|
+
entities: [you],
|
|
313
|
+
edges: [{ from: 'mem-a', to: you.id, kind: 'person', rule: 'made-up', evidence: { rule: 'made-up' } }],
|
|
314
|
+
})
|
|
315
|
+
assert.equal(applied.skipped, 1)
|
|
316
|
+
assert.equal((state.loadPrivateIndex(lib).entries['mem-a'].links || []).length, 0)
|
|
317
|
+
} finally {
|
|
318
|
+
state.updateSettings({ autoLinkMode: 'suggest' })
|
|
319
|
+
}
|
|
320
|
+
})
|
|
321
|
+
|
|
322
|
+
test('clearLinkSuppression removes markers and the deterministic rebuild restores the edge', async () => {
|
|
323
|
+
const lib = library('clear-suppress')
|
|
324
|
+
const you = graph.createEntity({ libraryId: lib, name: '小檀', kind: 'person' })
|
|
325
|
+
state.savePrivateIndex(lib, index({ 'mem-a': entry('小檀来了', '小檀进门坐下。') }))
|
|
326
|
+
state.updateSettings({ autoLinkMode: 'deterministic' })
|
|
327
|
+
try {
|
|
328
|
+
graph.confirmEntities({ libraryId: lib, entityIds: [you.id], by: 'user' })
|
|
329
|
+
graph.unlinkEntry({ libraryId: lib, fromId: 'mem-a', toId: you.id, kind: 'person' })
|
|
330
|
+
assert.ok(suppression.suppressionSet(lib).size > 0)
|
|
331
|
+
assert.equal((state.loadPrivateIndex(lib).entries['mem-a'].links || []).length, 0)
|
|
332
|
+
const cleared = await rpc.dispatch({}, 'clearLinkSuppression', { libraryId: lib })
|
|
333
|
+
assert.ok(cleared.cleared >= 1)
|
|
334
|
+
assert.equal(suppression.suppressionSet(lib).size, 0)
|
|
335
|
+
assert.ok((state.loadPrivateIndex(lib).entries['mem-a'].links || []).some(link => link.to === you.id), 'the full rebuild restores the deleted edge')
|
|
336
|
+
} finally {
|
|
337
|
+
state.updateSettings({ autoLinkMode: 'suggest' })
|
|
338
|
+
}
|
|
339
|
+
})
|
|
340
|
+
|
|
341
|
+
test('switching to deterministic backfills entities confirmed while off', async () => {
|
|
342
|
+
const lib = library('mode-switch')
|
|
343
|
+
const you = graph.createEntity({ libraryId: lib, name: '小檀', kind: 'person' })
|
|
344
|
+
state.savePrivateIndex(lib, index({ 'mem-a': entry('小檀来了', '小檀进门坐下。') }))
|
|
345
|
+
state.updateSettings({ autoLinkMode: 'off' })
|
|
346
|
+
try {
|
|
347
|
+
const off = graph.confirmEntities({ libraryId: lib, entityIds: [you.id], by: 'user' })
|
|
348
|
+
assert.equal(off.entities, 0)
|
|
349
|
+
assert.equal((state.loadPrivateIndex(lib).entries['mem-a'].links || []).length, 0)
|
|
350
|
+
await rpc.dispatch({}, 'setSettings', { patch: { autoLinkMode: 'deterministic' } })
|
|
351
|
+
const links = state.loadPrivateIndex(lib).entries['mem-a'].links || []
|
|
352
|
+
assert.ok(links.some(link => link.to === you.id && link.evidence?.rule === 'entity-mention'), 'the switch rebuilds missing mentions')
|
|
353
|
+
} finally {
|
|
354
|
+
state.updateSettings({ autoLinkMode: 'suggest' })
|
|
355
|
+
}
|
|
356
|
+
})
|
|
357
|
+
|
|
358
|
+
test('re-confirming an already-confirmed entity backfills missing mentions', () => {
|
|
359
|
+
const lib = library('reconfirm')
|
|
360
|
+
const you = graph.createEntity({ libraryId: lib, name: '小檀', kind: 'person', status: 'confirmed' })
|
|
361
|
+
state.savePrivateIndex(lib, index({ 'mem-a': entry('小檀来了', '小檀进门坐下。') }))
|
|
362
|
+
const result = graph.confirmEntities({ libraryId: lib, entityIds: [you.id], by: 'user' })
|
|
363
|
+
assert.deepEqual(result.confirmed, [], 'it was already confirmed')
|
|
364
|
+
assert.equal(result.entities, 1, 'the refresh still runs and attaches the edge')
|
|
365
|
+
assert.ok((state.loadPrivateIndex(lib).entries['mem-a'].links || []).some(link => link.to === you.id))
|
|
366
|
+
})
|
|
367
|
+
|
|
368
|
+
test('three automatic passes in suggest mode add no edges', async () => {
|
|
369
|
+
state.updateSettings({ autoLinkMode: 'suggest' })
|
|
370
|
+
try {
|
|
371
|
+
const lib = library('suggest-three')
|
|
372
|
+
graph.createEntity({ libraryId: lib, name: '小檀', kind: 'person', status: 'confirmed' })
|
|
373
|
+
state.savePrivateIndex(lib, index({ 'mem-a': entry('小檀来了', '小檀进门坐下。') }))
|
|
374
|
+
for (let pass = 0; pass < 3; pass += 1) {
|
|
375
|
+
const result = await privateLinks.refreshPrivateAutoLinks({ libraryId: lib, ids: ['mem-a'] })
|
|
376
|
+
assert.equal(result.skipped, 'suggest')
|
|
377
|
+
}
|
|
378
|
+
assert.equal((state.loadPrivateIndex(lib).entries['mem-a'].links || []).length, 0)
|
|
379
|
+
} finally {
|
|
380
|
+
state.updateSettings({ autoLinkMode: 'suggest' })
|
|
381
|
+
}
|
|
382
|
+
})
|
|
383
|
+
|
|
384
|
+
test('confirming eight entities backfills all mentions in one batch (8 x 9 = 72)', () => {
|
|
385
|
+
const lib = library('batch-confirm')
|
|
386
|
+
const names = Array.from({ length: 8 }, (_, i) => `角色${i + 1}`)
|
|
387
|
+
const entities = names.map(name => graph.createEntity({ libraryId: lib, name, kind: 'person' }))
|
|
388
|
+
const entries = {}
|
|
389
|
+
for (let i = 0; i < 9; i += 1) entries[`mem-${i}`] = entry(`第 ${i} 场`, names.join('、') + ' 都在场。')
|
|
390
|
+
state.savePrivateIndex(lib, index(entries))
|
|
391
|
+
const result = graph.confirmEntities({ libraryId: lib, entityIds: entities.map(entity => entity.id), by: 'user' })
|
|
392
|
+
assert.equal(result.confirmed.length, 8)
|
|
393
|
+
assert.equal(result.entities, 72, '9 memories x 8 entities')
|
|
394
|
+
const second = graph.confirmEntities({ libraryId: lib, entityIds: entities.map(entity => entity.id), by: 'user' })
|
|
395
|
+
assert.deepEqual(second.confirmed, [])
|
|
396
|
+
assert.equal(second.entities, 0)
|
|
397
|
+
})
|
|
398
|
+
|
|
399
|
+
test('a broken entity store makes the automatic refresh fail open (warn, no throw)', async () => {
|
|
400
|
+
const lib = library('broken-entities')
|
|
401
|
+
state.savePrivateIndex(lib, index({ 'mem-a': entry('小檀来了', '小檀进门坐下。') }))
|
|
402
|
+
const file = path.join(home, 'index', `${lib}.entities.json`)
|
|
403
|
+
fs.mkdirSync(path.dirname(file), { recursive: true })
|
|
404
|
+
fs.writeFileSync(file, '{not json', 'utf8')
|
|
405
|
+
state.updateSettings({ autoLinkMode: 'deterministic' })
|
|
406
|
+
try {
|
|
407
|
+
const result = await privateLinks.refreshPrivateAutoLinks({ libraryId: lib, ids: ['mem-a'] })
|
|
408
|
+
assert.equal(result.skipped, 'entities')
|
|
409
|
+
assert.equal(result.applied, null)
|
|
410
|
+
} finally {
|
|
411
|
+
state.updateSettings({ autoLinkMode: 'suggest' })
|
|
412
|
+
fs.rmSync(file, { force: true })
|
|
413
|
+
}
|
|
414
|
+
})
|
|
415
|
+
|
|
416
|
+
test('field evidence stays linear: 10k full rebuild and a 10-id incremental stay in budget', () => {
|
|
417
|
+
const entries = {}
|
|
418
|
+
for (let i = 0; i < 10000; i += 1) {
|
|
419
|
+
const id = `mem-${String(i).padStart(5, '0')}`
|
|
420
|
+
const row = entry(`记忆 ${i}`, `第 ${i} 条正文。`, { createdAt: at(1), updatedAt: at(1) })
|
|
421
|
+
if (i % 10 === 1) row.conflicts_with = `mem-${String(i - 1).padStart(5, '0')}`
|
|
422
|
+
if (i % 20 === 1) {
|
|
423
|
+
// 200-row fact families stress the grouping/sort path as well as the count.
|
|
424
|
+
row.fact_key = `k${Math.floor(i / 200)}`
|
|
425
|
+
row.context = { worldId: `w${Math.floor(i / 200)}` }
|
|
426
|
+
row.version = i % 200
|
|
427
|
+
}
|
|
428
|
+
entries[id] = row
|
|
429
|
+
}
|
|
430
|
+
const snapshot = index(entries)
|
|
431
|
+
const startedFull = performance.now()
|
|
432
|
+
const full = decide.fieldEvidenceEdges(snapshot)
|
|
433
|
+
const fullMs = performance.now() - startedFull
|
|
434
|
+
const sample = Object.keys(entries).slice(0, 10)
|
|
435
|
+
const startedInc = performance.now()
|
|
436
|
+
const incremental = decide.fieldEvidenceEdgesFor(snapshot, sample)
|
|
437
|
+
const incMs = performance.now() - startedInc
|
|
438
|
+
assert.ok(full.length > 900, 'the fixture really produces conflict/fact-key edges: ' + full.length)
|
|
439
|
+
assert.ok(incremental.length > 0)
|
|
440
|
+
assert.ok(fullMs < 5000, `10k full rebuild took ${Math.round(fullMs)} ms (design budget <5000)`)
|
|
441
|
+
assert.ok(incMs < 500, `10-id incremental took ${Math.round(incMs)} ms (design budget <50, allow CI slack)`)
|
|
442
|
+
})
|
|
443
|
+
|
|
444
|
+
// ---------------------------------------------------------------- write-path hooks
|
|
445
|
+
|
|
446
|
+
test('a private write carrying conflicts_with gets its evidence edges immediately', async () => {
|
|
447
|
+
state.updateSettings({ autoLinkMode: 'deterministic' })
|
|
448
|
+
try {
|
|
449
|
+
const cwd = path.join(home, 'ws-write-hook')
|
|
450
|
+
const binding = state.ensureSession('write-hook', { cwd, agentPreset: 'rp' })
|
|
451
|
+
const exec = { agent: { session: { header: { id: 'write-hook', cwd, agentPreset: 'rp' } } } }
|
|
452
|
+
const first = await memory.privateRemember({ exec, title: '周五安排', content: '旧说法:周五去书店。' })
|
|
453
|
+
const second = await memory.privateRemember({ exec, title: '周五安排', content: '新说法:周五改去河边。', conflictsWith: first.id })
|
|
454
|
+
assert.equal(second.conflictsWith, first.id)
|
|
455
|
+
const index = state.loadPrivateIndex(binding.libraryId)
|
|
456
|
+
const forward = (index.entries[second.id].links || []).find(link => link.to === first.id)
|
|
457
|
+
assert.ok(forward, 'the new entry links to the conflicting version')
|
|
458
|
+
assert.equal(forward.kind, 'conflict')
|
|
459
|
+
assert.equal(forward.evidence?.rule, 'conflict')
|
|
460
|
+
assert.equal(forward.confidence, 1)
|
|
461
|
+
const reverse = (index.entries[first.id].links || []).find(link => link.to === second.id)
|
|
462
|
+
assert.ok(reverse, 'memory.js writes the reverse field; the reverse edge follows')
|
|
463
|
+
assert.equal(reverse.evidence?.rule, 'conflict')
|
|
464
|
+
} finally {
|
|
465
|
+
state.updateSettings({ autoLinkMode: 'suggest' })
|
|
466
|
+
}
|
|
467
|
+
})
|
|
468
|
+
|
|
469
|
+
test('review accept refreshes the supersede edge immediately and stays silent in suggest mode', async () => {
|
|
470
|
+
const lib = library('review-hook')
|
|
471
|
+
const seed = () => index({
|
|
472
|
+
'mem-old': entry('周五安排', '旧说法:周五去书店。'),
|
|
473
|
+
'mem-new': entry('周五安排', '新说法:周五改去河边。', { status: 'pending_review', conflictingWith: 'mem-old' }),
|
|
474
|
+
})
|
|
475
|
+
state.savePrivateIndex(lib, seed())
|
|
476
|
+
state.updateSettings({ autoLinkMode: 'deterministic' })
|
|
477
|
+
try {
|
|
478
|
+
await memory.resolveMemoryReview({ libraryId: lib, id: 'mem-new', action: 'accept', resolution: 'replace', sessionId: null })
|
|
479
|
+
const links = state.loadPrivateIndex(lib).entries['mem-new'].links || []
|
|
480
|
+
const edge = links.find(link => link.to === 'mem-old')
|
|
481
|
+
assert.ok(edge, 'the supersede edge is written with the review commit')
|
|
482
|
+
assert.equal(edge.kind, 'supersede')
|
|
483
|
+
assert.equal(edge.evidence?.rule, 'supersede')
|
|
484
|
+
|
|
485
|
+
state.savePrivateIndex(lib, seed())
|
|
486
|
+
state.updateSettings({ autoLinkMode: 'suggest' })
|
|
487
|
+
await memory.resolveMemoryReview({ libraryId: lib, id: 'mem-new', action: 'accept', resolution: 'replace', sessionId: null })
|
|
488
|
+
const after = state.loadPrivateIndex(lib).entries['mem-new'].links || []
|
|
489
|
+
assert.equal(after.some(link => link.evidence?.rule === 'supersede'), false, 'suggest mode never writes an edge')
|
|
490
|
+
} finally {
|
|
491
|
+
state.updateSettings({ autoLinkMode: 'suggest' })
|
|
492
|
+
}
|
|
493
|
+
})
|
|
494
|
+
|
|
495
|
+
// ---------------------------------------------------------------- recall governance
|
|
496
|
+
|
|
497
|
+
test('recallRelations ignores entity-mention edges even at confidence 1.0 but keeps field evidence', () => {
|
|
498
|
+
const lib = library('recall')
|
|
499
|
+
const you = graph.createEntity({ libraryId: lib, name: '小檀', kind: 'person', status: 'confirmed' })
|
|
500
|
+
state.savePrivateIndex(lib, index({
|
|
501
|
+
'mem-a': entry('小檀来了', '小檀进门坐下。', {
|
|
502
|
+
links: [
|
|
503
|
+
{ to: you.id, kind: 'person', note: '提及已确认实体「小檀」(标题)。', confidence: 1, source: 'auto', evidence: { rule: 'entity-mention', entityId: you.id, surface: '小檀', field: 'title', offset: 0 } },
|
|
504
|
+
{ to: 'mem-b', kind: 'conflict', note: '字段证据:conflicts_with 指向这条记忆。', confidence: 1, source: 'auto', evidence: { rule: 'conflict', field: 'conflicts_with' } },
|
|
505
|
+
],
|
|
506
|
+
}),
|
|
507
|
+
'mem-b': entry('另一种说法', '内容不同。'),
|
|
508
|
+
}))
|
|
509
|
+
const related = graph.recallRelations(lib, state.loadPrivateIndex(lib), 'mem-a', null)
|
|
510
|
+
assert.deepEqual(related.map(item => item.id), ['mem-b'], 'the mention edge never takes a top-2 seat')
|
|
511
|
+
assert.equal(related[0].kind, 'conflict')
|
|
512
|
+
})
|