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
package/tests/graph.test.mjs
CHANGED
|
@@ -1,371 +1,371 @@
|
|
|
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
|
-
const home = fs.mkdtempSync(path.join(os.tmpdir(), 'dsh-memu-memory-graph-'))
|
|
7
|
-
process.env.DSH_MEMU_HOME = home
|
|
8
|
-
const state = await import('../lib/state.js')
|
|
9
|
-
const graph = await import('../lib/graph.js')
|
|
10
|
-
const memory = await import('../lib/memory.js')
|
|
11
|
-
test.after(() => { state.flushState(); fs.rmSync(home, { recursive: true, force: true }) })
|
|
12
|
-
const binding = state.ensureSession('graph', { cwd: path.join(home, 'rp'), agentPreset: 'rp' })
|
|
13
|
-
const libraryId = binding.libraryId
|
|
14
|
-
const exec = { agent: { session: { header: { id: 'graph' } } } }
|
|
15
|
-
const entries = Object.fromEntries(Array.from({ length: 10 }, (_, i) => [`mem-${i}`, { title: `person ${i}`, content: `scene ${i}`, layer: 'long_term', status: 'active', visibility: 'shared' }]))
|
|
16
|
-
state.savePrivateIndex(libraryId, { entries })
|
|
17
|
-
|
|
18
|
-
test('graph stores one direction but queries both within bounded hops', () => {
|
|
19
|
-
for (let i = 0; i < 9; i++) graph.linkEntries({ libraryId, fromId: `mem-${i}`, toId: `mem-${i + 1}`, kind: 'event', note: 'The same event seen from another side.' })
|
|
20
|
-
const result = graph.getGraph({ libraryId, focusId: 'mem-4', depth: 1 })
|
|
21
|
-
assert.deepEqual(new Set(result.nodes.map(node => node.id)), new Set(['mem-3', 'mem-4', 'mem-5']))
|
|
22
|
-
assert.equal(result.edges.length, 2)
|
|
23
|
-
assert.equal(state.loadPrivateIndex(libraryId).entries['mem-5'].links.length, 1)
|
|
24
|
-
})
|
|
25
|
-
|
|
26
|
-
test('deleted memories remain gray dangling nodes rather than hiding the edge', () => {
|
|
27
|
-
state.updatePrivateIndex(libraryId, index => { delete index.entries['mem-1'] })
|
|
28
|
-
const result = graph.getGraph({ libraryId, focusId: 'mem-0', depth: 1 })
|
|
29
|
-
assert.equal(result.nodes.find(node => node.id === 'mem-1').missing, true)
|
|
30
|
-
assert.equal(result.edges.length, 1)
|
|
31
|
-
})
|
|
32
|
-
|
|
33
|
-
test('entity rename changes all references without rewriting memory text', async () => {
|
|
34
|
-
const entity = graph.createEntity({ libraryId, name: 'Old Zhou' })
|
|
35
|
-
graph.linkEntries({ libraryId, fromId: 'mem-2', toId: entity.id, kind: 'person', note: 'Old Zhou is the person mentioned here.' })
|
|
36
|
-
graph.renameEntity({ libraryId, entityId: entity.id, name: 'Zhou', aliases: ['Old Zhou'] })
|
|
37
|
-
assert.equal(graph.getGraph({ libraryId, focusId: entity.id }).nodes.find(node => node.id === entity.id).title, 'Zhou')
|
|
38
|
-
assert.equal(state.loadPrivateIndex(libraryId).entries['mem-2'].content, 'scene 2')
|
|
39
|
-
assert.equal((await memory.privateRecall({ exec, query: 'mem-2' })).files[0].related.find(item => item.id === entity.id).title, 'Zhou')
|
|
40
|
-
})
|
|
41
|
-
|
|
42
|
-
test('invalid kinds, empty reasons and out-of-scope private links are rejected', () => {
|
|
43
|
-
assert.throws(() => graph.linkEntries({ libraryId, fromId: 'mem-2', toId: 'mem-3', kind: 'typo', note: 'explanation' }), /link kind/)
|
|
44
|
-
assert.throws(() => graph.linkEntries({ libraryId, fromId: 'mem-2', toId: 'mem-3', kind: 'event', note: '' }), /8-500/)
|
|
45
|
-
// The remaining boundary is the context scope: this entry belongs to another character,
|
|
46
|
-
// and this conversation has no context at all, so it must stay unreachable.
|
|
47
|
-
state.updatePrivateIndex(libraryId, index => { index.entries.secret = { title: 'private title', status: 'active', visibility: 'secret', context: { characterId: 'other' } } })
|
|
48
|
-
assert.throws(() => graph.linkEntries({ exec, fromId: 'mem-2', toId: 'secret', kind: 'event', note: 'a very long explanation' }), /inaccessible/)
|
|
49
|
-
assert.ok(!JSON.stringify(graph.getGraph({ exec })).includes('private title'))
|
|
50
|
-
})
|
|
51
|
-
|
|
52
|
-
test('an explicit link source persists on the record and surfaces on graph edges', () => {
|
|
53
|
-
state.updatePrivateIndex(libraryId, index => {
|
|
54
|
-
index.entries['mem-src-a'] = { title: 'alpha note', content: 'alpha body', status: 'active', visibility: 'shared' }
|
|
55
|
-
index.entries['mem-src-b'] = { title: 'beta note', content: 'beta body', status: 'active', visibility: 'shared' }
|
|
56
|
-
index.entries['mem-src-c'] = { title: 'gamma note', content: 'gamma body', status: 'active', visibility: 'shared' }
|
|
57
|
-
})
|
|
58
|
-
const linked = graph.linkEntries({
|
|
59
|
-
libraryId, fromId: 'mem-src-a', toId: 'mem-src-b', kind: 'relation',
|
|
60
|
-
note: 'Auto-linked because both memories share the stable name Alpha.', source: 'auto',
|
|
61
|
-
})
|
|
62
|
-
assert.equal(linked.source, 'auto')
|
|
63
|
-
const stored = () => state.loadPrivateIndex(libraryId).entries['mem-src-a'].links
|
|
64
|
-
assert.equal(stored().find(link => link.to === 'mem-src-b').source, 'auto', 'auto is persisted, not only returned')
|
|
65
|
-
|
|
66
|
-
// Replacement stays idempotent and the source survives the rewrite.
|
|
67
|
-
graph.linkEntries({
|
|
68
|
-
libraryId, fromId: 'mem-src-a', toId: 'mem-src-b', kind: 'relation',
|
|
69
|
-
note: 'Re-linked after a rerun with the same evidence.', source: 'auto',
|
|
70
|
-
})
|
|
71
|
-
const replaced = stored().filter(link => link.to === 'mem-src-b')
|
|
72
|
-
assert.equal(replaced.length, 1, 'the same (to, kind) pair is replaced in place')
|
|
73
|
-
assert.equal(replaced[0].source, 'auto')
|
|
74
|
-
|
|
75
|
-
// Only auto/user are meaningful; anything else is omitted rather than stored.
|
|
76
|
-
graph.linkEntries({
|
|
77
|
-
libraryId, fromId: 'mem-src-b', toId: 'mem-src-c', kind: 'event',
|
|
78
|
-
note: 'The same event seen from another side.', source: 'robot',
|
|
79
|
-
})
|
|
80
|
-
assert.equal(state.loadPrivateIndex(libraryId).entries['mem-src-b'].links[0].source, undefined)
|
|
81
|
-
|
|
82
|
-
const view = graph.getGraph({ libraryId, focusId: 'mem-src-b', depth: 1 })
|
|
83
|
-
const autoEdge = view.edges.find(edge => edge.from === 'mem-src-a' && edge.to === 'mem-src-b')
|
|
84
|
-
assert.equal(autoEdge.source, 'auto', 'the UI can tag an auto link')
|
|
85
|
-
assert.equal(autoEdge.kind, 'relation', 'link semantics are unchanged')
|
|
86
|
-
// Legacy auto edges written before the rule schema carry no evidence.rule;
|
|
87
|
-
// they must still surface under the 'auto' bucket so the WebUI rule filter
|
|
88
|
-
// has an option on old data (new writes record the real rule).
|
|
89
|
-
assert.equal(autoEdge.rule, 'auto', 'a legacy auto link reports the compat rule bucket')
|
|
90
|
-
const plainEdge = view.edges.find(edge => edge.from === 'mem-src-b')
|
|
91
|
-
assert.equal(plainEdge.source, null, 'a link without a source reports null, not undefined')
|
|
92
|
-
assert.equal(plainEdge.rule, null, 'a manual link reports no rule')
|
|
93
|
-
})
|
|
94
|
-
|
|
95
|
-
test('graphs cap nodes at 200 and the recall carries at most two pointers', async () => {
|
|
96
|
-
state.updatePrivateIndex(libraryId, index => {
|
|
97
|
-
for (let i = 10; i < 400; i++) index.entries[`mem-${i}`] = { title: `node ${i}`, status: 'active', content: 'node' }
|
|
98
|
-
})
|
|
99
|
-
const result = graph.getGraph({ libraryId, limit: 5000 })
|
|
100
|
-
assert.equal(result.nodes.length, 200)
|
|
101
|
-
assert.equal(result.truncated, true)
|
|
102
|
-
assert.ok((await memory.privateRecall({ exec, query: 'mem-2' })).files[0].related.length <= 2)
|
|
103
|
-
})
|
|
104
|
-
|
|
105
|
-
test('graph pagination reaches entities after 200 memories and returns each cross-page edge once', () => {
|
|
106
|
-
const { libraryId } = state.ensureSession('graph-pages', { cwd: path.join(home, 'rp-pages'), agentPreset: 'rp' })
|
|
107
|
-
const confirmed = graph.createEntity({ libraryId, name: 'Late confirmed', status: 'confirmed' })
|
|
108
|
-
const provisional = graph.createEntity({ libraryId, name: 'Late provisional' })
|
|
109
|
-
const entries = Object.fromEntries(Array.from({ length: 205 }, (_, i) => [
|
|
110
|
-
`mem-${i}`, { title: `node ${i}`, status: 'active', visibility: 'shared', domain: 'pages' },
|
|
111
|
-
]))
|
|
112
|
-
entries['mem-0'].links = [{ to: 'mem-204', kind: 'event' }, { to: confirmed.id, kind: 'person' }]
|
|
113
|
-
entries['mem-204'].links = [{ to: 'mem-0', kind: 'relation' }, { to: provisional.id, kind: 'person' }]
|
|
114
|
-
state.savePrivateIndex(libraryId, { entries })
|
|
115
|
-
const edgePairs = page => page.edges.map(edge => [edge.from, edge.to])
|
|
116
|
-
const legacy = graph.getGraph({ libraryId, limit: 5000 })
|
|
117
|
-
assert.equal(legacy.nodes.length, 200)
|
|
118
|
-
assert.equal(legacy.limit, 200)
|
|
119
|
-
assert.equal(legacy.truncated, true)
|
|
120
|
-
assert.equal(legacy.total, undefined)
|
|
121
|
-
assert.equal(legacy.nextOffset, undefined)
|
|
122
|
-
assert.deepEqual(legacy.edges, [], 'legacy edges still require both endpoints in the result')
|
|
123
|
-
|
|
124
|
-
const first = graph.getGraph({ libraryId, offset: 0, limit: 5000 })
|
|
125
|
-
assert.deepEqual(first.nodes.map(node => node.id), legacy.nodes.map(node => node.id))
|
|
126
|
-
assert.equal(first.total, 207)
|
|
127
|
-
assert.equal(first.nextOffset, 200)
|
|
128
|
-
assert.equal(first.truncated, true)
|
|
129
|
-
assert.deepEqual(edgePairs(first), [['mem-0', 'mem-204'], ['mem-0', confirmed.id]])
|
|
130
|
-
|
|
131
|
-
const last = graph.getGraph({ libraryId, offset: first.nextOffset })
|
|
132
|
-
assert.equal(last.nodes.length, 7)
|
|
133
|
-
assert.equal(last.total, first.total)
|
|
134
|
-
assert.equal(last.nextOffset, null)
|
|
135
|
-
assert.equal(last.truncated, false)
|
|
136
|
-
assert.deepEqual(last.nodes.filter(node => node.kind === 'person').map(node => [node.id, node.status]), [
|
|
137
|
-
[confirmed.id, 'confirmed'], [provisional.id, 'provisional'],
|
|
138
|
-
])
|
|
139
|
-
assert.deepEqual(edgePairs(last), [['mem-204', 'mem-0'], ['mem-204', provisional.id]])
|
|
140
|
-
assert.equal(new Set([...first.nodes, ...last.nodes].map(node => node.id)).size, first.total)
|
|
141
|
-
|
|
142
|
-
const fullLast = graph.getGraph({ libraryId, offset: 205, limit: 2 })
|
|
143
|
-
assert.equal(fullLast.nodes.length, 2)
|
|
144
|
-
assert.equal(fullLast.nextOffset, null)
|
|
145
|
-
assert.equal(fullLast.truncated, false, 'a full final page has no remainder')
|
|
146
|
-
const beyond = graph.getGraph({ libraryId, offset: 500 })
|
|
147
|
-
assert.deepEqual([beyond.nodes, beyond.edges, beyond.total, beyond.nextOffset, beyond.truncated], [[], [], 207, null, false])
|
|
148
|
-
|
|
149
|
-
const focused = graph.getGraph({ libraryId, focusId: 'mem-0', offset: 500, depth: 1 })
|
|
150
|
-
assert.deepEqual(focused.nodes.map(node => node.id), ['mem-0', 'mem-204', confirmed.id])
|
|
151
|
-
assert.equal(focused.nextOffset, undefined, 'offset never changes bounded focus traversal')
|
|
152
|
-
})
|
|
153
|
-
|
|
154
|
-
test('graph domain matches normalized exact memory domains and derives only their linked entities', () => {
|
|
155
|
-
const { libraryId } = state.ensureSession('graph-domains', { cwd: path.join(home, 'rp-domains'), agentPreset: 'rp' })
|
|
156
|
-
const coding = graph.createEntity({ libraryId, name: 'Coding contact' })
|
|
157
|
-
const other = graph.createEntity({ libraryId, name: 'Other contact' })
|
|
158
|
-
const orphan = graph.createEntity({ libraryId, name: 'Unrelated orphan' })
|
|
159
|
-
state.savePrivateIndex(libraryId, { entries: {
|
|
160
|
-
'mem-code': { title: 'coding', domain: ' Coding ', links: [
|
|
161
|
-
{ to: coding.id, kind: 'person' }, { to: 'mem-code-2', kind: 'event' },
|
|
162
|
-
{ to: 'mem-legacy', kind: 'relation', note: 'cross-domain evidence' }, { to: 'mem-other', kind: 'event' },
|
|
163
|
-
{ to: 'mem-removed', kind: 'relation' },
|
|
164
|
-
] },
|
|
165
|
-
'mem-code-2': { title: 'also coding', domain: 'CODING' },
|
|
166
|
-
'mem-legacy': { title: 'legacy' },
|
|
167
|
-
'mem-other': { title: 'other', domain: 'coding-extra', links: [
|
|
168
|
-
{ to: other.id, kind: 'person' }, { to: 'mem-code', kind: 'event' },
|
|
169
|
-
] },
|
|
170
|
-
} })
|
|
171
|
-
const page = graph.getGraph({ libraryId, offset: 0, domain: ' CoDinG ', includeOrphans: true })
|
|
172
|
-
assert.deepEqual(page.domains, ['coding', 'coding-extra', 'general'])
|
|
173
|
-
assert.deepEqual(page.nodes.map(node => node.id), ['mem-code', 'mem-code-2', coding.id, 'mem-removed'])
|
|
174
|
-
assert.deepEqual(page.nodes.map(node => node.domain), ['coding', 'coding', undefined, undefined])
|
|
175
|
-
assert.equal(page.nodes.find(node => node.id === 'mem-removed').missing, true, 'genuine dangling links still have placeholders')
|
|
176
|
-
assert.equal(page.nodes.find(node => node.id === 'mem-code').degree, 3, 'out-of-domain neighbors do not even affect degree')
|
|
177
|
-
assert.deepEqual(page.edges.map(edge => edge.to), [coding.id, 'mem-code-2', 'mem-removed'])
|
|
178
|
-
assert.ok(!JSON.stringify(page).includes('cross-domain evidence'))
|
|
179
|
-
assert.deepEqual(graph.getGraph({ libraryId, domain: ' GENERAL ' }).nodes.map(node => [node.id, node.domain]), [['mem-legacy', 'general']])
|
|
180
|
-
assert.ok(graph.getGraph({ libraryId, domain: ' ', includeOrphans: true }).nodes.some(node => node.id === orphan.id))
|
|
181
|
-
assert.throws(() => graph.getGraph({ libraryId, focusId: orphan.id, domain: 'coding' }), /unknown or inaccessible graph focus/)
|
|
182
|
-
assert.throws(() => graph.getGraph({ libraryId, focusId: 'mem-other', domain: 'coding' }), /unknown or inaccessible graph focus/)
|
|
183
|
-
const none = graph.getGraph({ libraryId, offset: 0, domain: 'unknown', includeOrphans: true })
|
|
184
|
-
assert.deepEqual([none.nodes, none.edges, none.total, none.nextOffset, none.truncated], [[], [], 0, null, false])
|
|
185
|
-
assert.deepEqual(none.domains, page.domains, 'suggestions are gathered before applying the selected domain')
|
|
186
|
-
})
|
|
187
|
-
|
|
188
|
-
test('paged graph keeps bound-library, session eligibility and context checks before collecting domains or edges', () => {
|
|
189
|
-
const { libraryId } = state.ensureSession('graph-page-session', { cwd: path.join(home, 'rp-page-session'), agentPreset: 'rp' })
|
|
190
|
-
const exec = { agent: { session: { header: { id: 'graph-page-session' } } } }
|
|
191
|
-
const visible = graph.createEntity({ libraryId, name: 'Visible contact' })
|
|
192
|
-
const hidden = graph.createEntity({ libraryId, name: 'Hidden contact' })
|
|
193
|
-
state.savePrivateIndex(libraryId, { entries: {
|
|
194
|
-
'mem-visible': { title: 'visible', status: 'active', visibility: 'shared', domain: 'coding', links: [
|
|
195
|
-
{ to: visible.id, kind: 'person' }, { to: 'mem-secret', kind: 'relation' },
|
|
196
|
-
{ to: 'mem-other-context', kind: 'relation' }, { to: 'mem-archived', kind: 'relation' },
|
|
197
|
-
] },
|
|
198
|
-
'mem-secret': { title: 'secret', status: 'active', visibility: 'secret', domain: 'secret-domain', links: [{ to: hidden.id, kind: 'person' }] },
|
|
199
|
-
'mem-other-context': { title: 'private context', status: 'active', visibility: 'shared', domain: 'coding', context: { characterId: 'other' } },
|
|
200
|
-
'mem-archived': { title: 'archived', status: 'archived', visibility: 'shared', domain: 'coding' },
|
|
201
|
-
} })
|
|
202
|
-
const page = graph.getGraph({ exec, libraryId, offset: 0, limit: 1, domain: 'coding', includeOrphans: true })
|
|
203
|
-
assert.deepEqual(page.nodes.map(node => node.id), ['mem-visible'])
|
|
204
|
-
assert.deepEqual(page.domains, ['coding'])
|
|
205
|
-
assert.deepEqual(page.edges.map(edge => edge.to), [visible.id])
|
|
206
|
-
assert.equal(page.total, 2)
|
|
207
|
-
const last = graph.getGraph({ exec, libraryId, offset: page.nextOffset, limit: 1, domain: 'coding', includeOrphans: true })
|
|
208
|
-
assert.deepEqual(last.nodes.map(node => node.id), [visible.id])
|
|
209
|
-
assert.equal(last.nextOffset, null)
|
|
210
|
-
assert.throws(() => graph.getGraph({ exec, libraryId, focusId: hidden.id, offset: 0 }), /unknown or inaccessible graph focus/)
|
|
211
|
-
const otherLibrary = state.ensureSession('graph-page-unbound', { cwd: path.join(home, 'rp-page-unbound'), agentPreset: 'rp' }).libraryId
|
|
212
|
-
assert.throws(() => graph.getGraph({ exec, libraryId: otherLibrary, offset: 0 }), /library is not bound to this conversation/)
|
|
213
|
-
})
|
|
214
|
-
|
|
215
|
-
test('relationship snapshots preserve unknowns, evidence, boundaries and previous versions', () => {
|
|
216
|
-
const a = graph.createEntity({ libraryId, name: 'A' })
|
|
217
|
-
const b = graph.createEntity({ libraryId, name: 'B' })
|
|
218
|
-
const initial = graph.updateRelationship({ libraryId, fromId: a.id, toId: b.id,
|
|
219
|
-
state: { trust: .7, boundaries: ['Do not volunteer private stories.'], openPromises: ['Answer on Friday.'] },
|
|
220
|
-
evidenceIds: ['mem-2'], reason: 'Friday promise is explicit evidence.' }).relationship
|
|
221
|
-
assert.equal(initial.state.warmth, null, 'unknown is not a neutral or affectionate score')
|
|
222
|
-
graph.updateRelationship({ libraryId, fromId: a.id, toId: b.id,
|
|
223
|
-
state: { trust: .75, boundaries: initial.state.boundaries, callbacks: ['the station roof'], openPromises: [] },
|
|
224
|
-
evidenceIds: ['mem-2'], reason: 'The answer was delivered explicitly.' })
|
|
225
|
-
const next = graph.getRelationship({ libraryId, fromId: a.id, toId: b.id }).relationship
|
|
226
|
-
assert.equal(next.history.length, 1)
|
|
227
|
-
assert.deepEqual(next.history[0].state.openPromises, ['Answer on Friday.'])
|
|
228
|
-
assert.ok(next.history[0].validTo)
|
|
229
|
-
assert.throws(() => graph.updateRelationship({ libraryId, fromId: a.id, toId: b.id, state: {}, evidenceIds: [], reason: 'no evidence' }), /evidence/)
|
|
230
|
-
})
|
|
231
|
-
|
|
232
|
-
test('relationship history is capped while the current snapshot and evidence stay', () => {
|
|
233
|
-
const a = graph.createEntity({ libraryId, name: 'History A' })
|
|
234
|
-
const b = graph.createEntity({ libraryId, name: 'History B' })
|
|
235
|
-
for (let i = 0; i < 55; i += 1) {
|
|
236
|
-
graph.updateRelationship({
|
|
237
|
-
libraryId, fromId: a.id, toId: b.id,
|
|
238
|
-
state: { trust: 0.5, callbacks: [`callback ${i}`] },
|
|
239
|
-
evidenceIds: ['mem-2'], reason: `Evidence-backed revision ${i} is explicit.`,
|
|
240
|
-
})
|
|
241
|
-
}
|
|
242
|
-
const relation = graph.getRelationship({ libraryId, fromId: a.id, toId: b.id }).relationship
|
|
243
|
-
assert.equal(relation.history.length, 50, 'old revisions are capped')
|
|
244
|
-
assert.equal(relation.revision, 55)
|
|
245
|
-
assert.deepEqual(relation.state.callbacks, ['callback 54'])
|
|
246
|
-
assert.deepEqual(relation.evidenceIds, ['mem-2'])
|
|
247
|
-
})
|
|
248
|
-
|
|
249
|
-
test('relationship retention is a pure wall-clock decay with access reinforcement', () => {
|
|
250
|
-
assert.equal(graph.relationshipRetentionAt(1, 24), 0.5)
|
|
251
|
-
assert.equal(graph.relationshipRetentionAt(1, 48), 0.25)
|
|
252
|
-
assert.equal(graph.relationshipRetentionAt(1, -5), 1, 'clock rollback never boosts retention')
|
|
253
|
-
const accessed = graph.accessRelationship(0.5, { now: 10_000_000, lastAccessAt: null })
|
|
254
|
-
assert.ok(accessed > 0.5 && accessed < 1)
|
|
255
|
-
assert.equal(graph.accessRelationship(0.5, { now: 10_000_000, lastAccessAt: new Date(10_000_000 - 1000).toISOString() }), 0.5, 'cooldown blocks a second bump')
|
|
256
|
-
})
|
|
257
|
-
|
|
258
|
-
test('relationship snapshots persist retention, last access and next review', () => {
|
|
259
|
-
const a = graph.createEntity({ libraryId, name: 'Lifecycle A' })
|
|
260
|
-
const b = graph.createEntity({ libraryId, name: 'Lifecycle B' })
|
|
261
|
-
const created = graph.updateRelationship({
|
|
262
|
-
libraryId, fromId: a.id, toId: b.id,
|
|
263
|
-
state: { trust: 0.6 }, evidenceIds: ['mem-2'], reason: 'First explicit evidence for this relation.',
|
|
264
|
-
}).relationship
|
|
265
|
-
assert.equal(created.retention_strength, 1)
|
|
266
|
-
assert.ok(created.last_access_at)
|
|
267
|
-
assert.ok(created.next_review_at)
|
|
268
|
-
const next = graph.updateRelationship({
|
|
269
|
-
libraryId, fromId: a.id, toId: b.id,
|
|
270
|
-
state: { trust: 0.7 }, evidenceIds: ['mem-2'], reason: 'Second explicit evidence for this relation.',
|
|
271
|
-
}).relationship
|
|
272
|
-
assert.ok(next.retention_strength > 0 && next.retention_strength <= 1)
|
|
273
|
-
assert.ok(next.last_access_at >= created.last_access_at)
|
|
274
|
-
assert.equal(next.history[0].retention_strength, 1)
|
|
275
|
-
})
|
|
276
|
-
|
|
277
|
-
test('a dangling edge is reported as missing, matching the graph view placeholder', () => {
|
|
278
|
-
// mem-1 was deleted from the index in an earlier test; its inbound edge from
|
|
279
|
-
// mem-0 remains. getGraph renders a [deleted] placeholder for it, so recall
|
|
280
|
-
// must report the same edge with the same missing mark instead of dropping it.
|
|
281
|
-
const view = graph.getGraph({ libraryId, focusId: 'mem-0', depth: 1 })
|
|
282
|
-
assert.equal(view.nodes.find(node => node.id === 'mem-1').missing, true)
|
|
283
|
-
const related = graph.recallRelations(libraryId, state.loadPrivateIndex(libraryId), 'mem-0', null)
|
|
284
|
-
const dangling = related.find(item => item.id === 'mem-1')
|
|
285
|
-
assert.ok(dangling, 'the edge must be reported, not silently dropped')
|
|
286
|
-
assert.equal(dangling.missing, true)
|
|
287
|
-
assert.equal(dangling.title, '[deleted]')
|
|
288
|
-
// mem-2 still links at the Zhou entity: a resolvable relation is marked false.
|
|
289
|
-
const zhou = Object.values(graph.graphExport(libraryId).entities).find(entity => entity.name === 'Zhou')
|
|
290
|
-
const healthy = graph.recallRelations(libraryId, state.loadPrivateIndex(libraryId), 'mem-2', null)
|
|
291
|
-
assert.equal(healthy.find(item => item.id === zhou.id).missing, false)
|
|
292
|
-
})
|
|
293
|
-
|
|
294
|
-
// ---- §6.4 包 D:listEntities 的 linked 派生字段与孤立判定 ----
|
|
295
|
-
// 前提(合并稿 0.2 纠正):无 session 本来就返回全部实体——不"放开过滤",
|
|
296
|
-
// 只加 linked 标记;有 session 才按可见引用收窄。
|
|
297
|
-
|
|
298
|
-
test('listEntities: without a session every entity returns, with a correct derived linked flag', () => {
|
|
299
|
-
const binding = state.ensureSession('vis-all', { cwd: path.join(home, 'ws-vis-all'), agentPreset: 'rp' })
|
|
300
|
-
const lib = binding.libraryId
|
|
301
|
-
const anchored = graph.createEntity({ libraryId: lib, name: 'Anchored' })
|
|
302
|
-
const drifting = graph.createEntity({ libraryId: lib, name: 'Drifting' })
|
|
303
|
-
state.savePrivateIndex(lib, { entries: {
|
|
304
|
-
'mem-anchored': { title: 'anchored', content: 'scene', layer: 'long_term', status: 'active', visibility: 'shared',
|
|
305
|
-
links: [{ to: anchored.id, kind: 'person', note: 'Anchored is the person this scene is about.', confidence: 0.9 }] },
|
|
306
|
-
} })
|
|
307
|
-
const all = graph.listEntities({ libraryId: lib })
|
|
308
|
-
assert.equal(all.items.length, 2, 'without a session the full entity list comes back')
|
|
309
|
-
assert.equal(all.total, 2)
|
|
310
|
-
const anchoredRow = all.items.find(entity => entity.id === anchored.id)
|
|
311
|
-
const driftingRow = all.items.find(entity => entity.id === drifting.id)
|
|
312
|
-
assert.equal(anchoredRow.linked, true, 'a link target is linked')
|
|
313
|
-
assert.equal(driftingRow.linked, false, 'an entity nothing points at is unlinked')
|
|
314
|
-
// linked 是运行时派生字段:实体文件里绝不出现它,启动 prune 的判定不受污染。
|
|
315
|
-
assert.equal(graph.graphExport(lib).entities[drifting.id].linked, undefined, 'the derived flag is never written back')
|
|
316
|
-
})
|
|
317
|
-
|
|
318
|
-
test('recallRelations reuses its inverted index until the index file changes', () => {
|
|
319
|
-
const binding = state.ensureSession('graph-cache', { cwd: path.join(home, 'rp-cache'), agentPreset: 'rp' })
|
|
320
|
-
const lib = binding.libraryId
|
|
321
|
-
const entity = graph.createEntity({ libraryId: lib, name: 'CacheEntity' })
|
|
322
|
-
state.savePrivateIndex(lib, { entries: {
|
|
323
|
-
'mem-cache-a': { title: 'a', content: 'a', status: 'active', visibility: 'shared',
|
|
324
|
-
links: [{ to: entity.id, kind: 'person', note: 'CacheEntity is the person in this scene.', confidence: 0.8 }] },
|
|
325
|
-
} })
|
|
326
|
-
const first = graph.recallRelations(lib, state.loadPrivateIndex(lib), 'mem-cache-a', null)
|
|
327
|
-
const second = graph.recallRelations(lib, state.loadPrivateIndex(lib), 'mem-cache-a', null)
|
|
328
|
-
assert.deepEqual(second, first, 'an unchanged index returns the same edges')
|
|
329
|
-
state.updatePrivateIndex(lib, index => {
|
|
330
|
-
index.entries['mem-cache-b'] = { title: 'b', content: 'b', status: 'active', visibility: 'shared',
|
|
331
|
-
links: [{ to: 'mem-cache-a', kind: 'event', note: 'The same event seen from another side.', confidence: 0.9 }] }
|
|
332
|
-
})
|
|
333
|
-
const refreshed = graph.recallRelations(lib, state.loadPrivateIndex(lib), 'mem-cache-a', null)
|
|
334
|
-
assert.equal(refreshed.length, 2, 'a new index write invalidates the cached inverted index')
|
|
335
|
-
assert.ok(refreshed.some(item => item.id === 'mem-cache-b'))
|
|
336
|
-
assert.ok(refreshed.some(item => item.id === entity.id))
|
|
337
|
-
})
|
|
338
|
-
|
|
339
|
-
test('listEntities: with a session only visible references count, and relationship evidence is session-filtered too', () => {
|
|
340
|
-
const binding = state.ensureSession('vis-scoped', { cwd: path.join(home, 'ws-vis-scoped'), agentPreset: 'rp' })
|
|
341
|
-
const lib = binding.libraryId
|
|
342
|
-
const exec = { agent: { session: { header: { id: 'vis-scoped' } } } }
|
|
343
|
-
const linked = graph.createEntity({ libraryId: lib, name: 'Linked' })
|
|
344
|
-
const secretTarget = graph.createEntity({ libraryId: lib, name: 'SecretTarget' })
|
|
345
|
-
const relVisible = graph.createEntity({ libraryId: lib, name: 'RelVisible' })
|
|
346
|
-
const relHidden = graph.createEntity({ libraryId: lib, name: 'RelHidden' })
|
|
347
|
-
const lonely = graph.createEntity({ libraryId: lib, name: 'Lonely' })
|
|
348
|
-
state.savePrivateIndex(lib, { entries: {
|
|
349
|
-
'mem-open': { title: 'open', content: 'open scene', layer: 'long_term', status: 'active', visibility: 'shared',
|
|
350
|
-
links: [{ to: linked.id, kind: 'person', note: 'Linked is the person this scene is about.', confidence: 0.9 }] },
|
|
351
|
-
'mem-hidden': { title: 'hidden', content: 'secret scene', layer: 'long_term', status: 'active', visibility: 'secret', context: { characterId: 'other' },
|
|
352
|
-
links: [{ to: secretTarget.id, kind: 'person', note: 'Secret is the person this scene is about.', confidence: 0.9 }] },
|
|
353
|
-
} })
|
|
354
|
-
// 关系快照也算引用:evidence 指向本会话可见记忆的算,指不可见记忆的不算。
|
|
355
|
-
graph.updateRelationship({ libraryId: lib, fromId: linked.id, toId: relVisible.id, state: { trust: 0.5 }, evidenceIds: ['mem-open'], reason: 'The open scene is explicit evidence.' })
|
|
356
|
-
graph.updateRelationship({ libraryId: lib, fromId: secretTarget.id, toId: relHidden.id, state: { trust: 0.5 }, evidenceIds: ['mem-hidden'], reason: 'The hidden scene is the only evidence here.' })
|
|
357
|
-
const scoped = graph.listEntities({ exec, libraryId: lib })
|
|
358
|
-
const ids = scoped.items.map(entity => entity.id).sort()
|
|
359
|
-
assert.deepEqual(ids, [linked.id, relVisible.id].sort(),
|
|
360
|
-
'a visible entry link and a relationship with visible evidence both count; secret-referenced and orphan entities do not')
|
|
361
|
-
assert.ok(scoped.items.every(entity => entity.linked === true), 'every session-visible row is linked by construction')
|
|
362
|
-
// 无 session:全量计入(两条快照都算引用),Linked/RelVisible/SecretTarget/RelHidden
|
|
363
|
-
// 都是 linked=true,只有 Lonely 是孤立。
|
|
364
|
-
const all = graph.listEntities({ libraryId: lib })
|
|
365
|
-
assert.equal(all.items.length, 5, 'no session means every entity, referenced or not')
|
|
366
|
-
const linkedMap = Object.fromEntries(all.items.map(entity => [entity.id, entity.linked]))
|
|
367
|
-
for (const entity of [linked, relVisible, secretTarget, relHidden]) {
|
|
368
|
-
assert.equal(linkedMap[entity.id], true, `${entity.name} is referenced and must not be misjudged as an orphan`)
|
|
369
|
-
}
|
|
370
|
-
assert.equal(linkedMap[lonely.id], false, 'the untouched entity stays unlinked')
|
|
371
|
-
})
|
|
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
|
+
const home = fs.mkdtempSync(path.join(os.tmpdir(), 'dsh-memu-memory-graph-'))
|
|
7
|
+
process.env.DSH_MEMU_HOME = home
|
|
8
|
+
const state = await import('../lib/state.js')
|
|
9
|
+
const graph = await import('../lib/graph.js')
|
|
10
|
+
const memory = await import('../lib/memory.js')
|
|
11
|
+
test.after(() => { state.flushState(); fs.rmSync(home, { recursive: true, force: true }) })
|
|
12
|
+
const binding = state.ensureSession('graph', { cwd: path.join(home, 'rp'), agentPreset: 'rp' })
|
|
13
|
+
const libraryId = binding.libraryId
|
|
14
|
+
const exec = { agent: { session: { header: { id: 'graph' } } } }
|
|
15
|
+
const entries = Object.fromEntries(Array.from({ length: 10 }, (_, i) => [`mem-${i}`, { title: `person ${i}`, content: `scene ${i}`, layer: 'long_term', status: 'active', visibility: 'shared' }]))
|
|
16
|
+
state.savePrivateIndex(libraryId, { entries })
|
|
17
|
+
|
|
18
|
+
test('graph stores one direction but queries both within bounded hops', () => {
|
|
19
|
+
for (let i = 0; i < 9; i++) graph.linkEntries({ libraryId, fromId: `mem-${i}`, toId: `mem-${i + 1}`, kind: 'event', note: 'The same event seen from another side.' })
|
|
20
|
+
const result = graph.getGraph({ libraryId, focusId: 'mem-4', depth: 1 })
|
|
21
|
+
assert.deepEqual(new Set(result.nodes.map(node => node.id)), new Set(['mem-3', 'mem-4', 'mem-5']))
|
|
22
|
+
assert.equal(result.edges.length, 2)
|
|
23
|
+
assert.equal(state.loadPrivateIndex(libraryId).entries['mem-5'].links.length, 1)
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
test('deleted memories remain gray dangling nodes rather than hiding the edge', () => {
|
|
27
|
+
state.updatePrivateIndex(libraryId, index => { delete index.entries['mem-1'] })
|
|
28
|
+
const result = graph.getGraph({ libraryId, focusId: 'mem-0', depth: 1 })
|
|
29
|
+
assert.equal(result.nodes.find(node => node.id === 'mem-1').missing, true)
|
|
30
|
+
assert.equal(result.edges.length, 1)
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
test('entity rename changes all references without rewriting memory text', async () => {
|
|
34
|
+
const entity = graph.createEntity({ libraryId, name: 'Old Zhou' })
|
|
35
|
+
graph.linkEntries({ libraryId, fromId: 'mem-2', toId: entity.id, kind: 'person', note: 'Old Zhou is the person mentioned here.' })
|
|
36
|
+
graph.renameEntity({ libraryId, entityId: entity.id, name: 'Zhou', aliases: ['Old Zhou'] })
|
|
37
|
+
assert.equal(graph.getGraph({ libraryId, focusId: entity.id }).nodes.find(node => node.id === entity.id).title, 'Zhou')
|
|
38
|
+
assert.equal(state.loadPrivateIndex(libraryId).entries['mem-2'].content, 'scene 2')
|
|
39
|
+
assert.equal((await memory.privateRecall({ exec, query: 'mem-2' })).files[0].related.find(item => item.id === entity.id).title, 'Zhou')
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
test('invalid kinds, empty reasons and out-of-scope private links are rejected', () => {
|
|
43
|
+
assert.throws(() => graph.linkEntries({ libraryId, fromId: 'mem-2', toId: 'mem-3', kind: 'typo', note: 'explanation' }), /link kind/)
|
|
44
|
+
assert.throws(() => graph.linkEntries({ libraryId, fromId: 'mem-2', toId: 'mem-3', kind: 'event', note: '' }), /8-500/)
|
|
45
|
+
// The remaining boundary is the context scope: this entry belongs to another character,
|
|
46
|
+
// and this conversation has no context at all, so it must stay unreachable.
|
|
47
|
+
state.updatePrivateIndex(libraryId, index => { index.entries.secret = { title: 'private title', status: 'active', visibility: 'secret', context: { characterId: 'other' } } })
|
|
48
|
+
assert.throws(() => graph.linkEntries({ exec, fromId: 'mem-2', toId: 'secret', kind: 'event', note: 'a very long explanation' }), /inaccessible/)
|
|
49
|
+
assert.ok(!JSON.stringify(graph.getGraph({ exec })).includes('private title'))
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
test('an explicit link source persists on the record and surfaces on graph edges', () => {
|
|
53
|
+
state.updatePrivateIndex(libraryId, index => {
|
|
54
|
+
index.entries['mem-src-a'] = { title: 'alpha note', content: 'alpha body', status: 'active', visibility: 'shared' }
|
|
55
|
+
index.entries['mem-src-b'] = { title: 'beta note', content: 'beta body', status: 'active', visibility: 'shared' }
|
|
56
|
+
index.entries['mem-src-c'] = { title: 'gamma note', content: 'gamma body', status: 'active', visibility: 'shared' }
|
|
57
|
+
})
|
|
58
|
+
const linked = graph.linkEntries({
|
|
59
|
+
libraryId, fromId: 'mem-src-a', toId: 'mem-src-b', kind: 'relation',
|
|
60
|
+
note: 'Auto-linked because both memories share the stable name Alpha.', source: 'auto',
|
|
61
|
+
})
|
|
62
|
+
assert.equal(linked.source, 'auto')
|
|
63
|
+
const stored = () => state.loadPrivateIndex(libraryId).entries['mem-src-a'].links
|
|
64
|
+
assert.equal(stored().find(link => link.to === 'mem-src-b').source, 'auto', 'auto is persisted, not only returned')
|
|
65
|
+
|
|
66
|
+
// Replacement stays idempotent and the source survives the rewrite.
|
|
67
|
+
graph.linkEntries({
|
|
68
|
+
libraryId, fromId: 'mem-src-a', toId: 'mem-src-b', kind: 'relation',
|
|
69
|
+
note: 'Re-linked after a rerun with the same evidence.', source: 'auto',
|
|
70
|
+
})
|
|
71
|
+
const replaced = stored().filter(link => link.to === 'mem-src-b')
|
|
72
|
+
assert.equal(replaced.length, 1, 'the same (to, kind) pair is replaced in place')
|
|
73
|
+
assert.equal(replaced[0].source, 'auto')
|
|
74
|
+
|
|
75
|
+
// Only auto/user are meaningful; anything else is omitted rather than stored.
|
|
76
|
+
graph.linkEntries({
|
|
77
|
+
libraryId, fromId: 'mem-src-b', toId: 'mem-src-c', kind: 'event',
|
|
78
|
+
note: 'The same event seen from another side.', source: 'robot',
|
|
79
|
+
})
|
|
80
|
+
assert.equal(state.loadPrivateIndex(libraryId).entries['mem-src-b'].links[0].source, undefined)
|
|
81
|
+
|
|
82
|
+
const view = graph.getGraph({ libraryId, focusId: 'mem-src-b', depth: 1 })
|
|
83
|
+
const autoEdge = view.edges.find(edge => edge.from === 'mem-src-a' && edge.to === 'mem-src-b')
|
|
84
|
+
assert.equal(autoEdge.source, 'auto', 'the UI can tag an auto link')
|
|
85
|
+
assert.equal(autoEdge.kind, 'relation', 'link semantics are unchanged')
|
|
86
|
+
// Legacy auto edges written before the rule schema carry no evidence.rule;
|
|
87
|
+
// they must still surface under the 'auto' bucket so the WebUI rule filter
|
|
88
|
+
// has an option on old data (new writes record the real rule).
|
|
89
|
+
assert.equal(autoEdge.rule, 'auto', 'a legacy auto link reports the compat rule bucket')
|
|
90
|
+
const plainEdge = view.edges.find(edge => edge.from === 'mem-src-b')
|
|
91
|
+
assert.equal(plainEdge.source, null, 'a link without a source reports null, not undefined')
|
|
92
|
+
assert.equal(plainEdge.rule, null, 'a manual link reports no rule')
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
test('graphs cap nodes at 200 and the recall carries at most two pointers', async () => {
|
|
96
|
+
state.updatePrivateIndex(libraryId, index => {
|
|
97
|
+
for (let i = 10; i < 400; i++) index.entries[`mem-${i}`] = { title: `node ${i}`, status: 'active', content: 'node' }
|
|
98
|
+
})
|
|
99
|
+
const result = graph.getGraph({ libraryId, limit: 5000 })
|
|
100
|
+
assert.equal(result.nodes.length, 200)
|
|
101
|
+
assert.equal(result.truncated, true)
|
|
102
|
+
assert.ok((await memory.privateRecall({ exec, query: 'mem-2' })).files[0].related.length <= 2)
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
test('graph pagination reaches entities after 200 memories and returns each cross-page edge once', () => {
|
|
106
|
+
const { libraryId } = state.ensureSession('graph-pages', { cwd: path.join(home, 'rp-pages'), agentPreset: 'rp' })
|
|
107
|
+
const confirmed = graph.createEntity({ libraryId, name: 'Late confirmed', status: 'confirmed' })
|
|
108
|
+
const provisional = graph.createEntity({ libraryId, name: 'Late provisional' })
|
|
109
|
+
const entries = Object.fromEntries(Array.from({ length: 205 }, (_, i) => [
|
|
110
|
+
`mem-${i}`, { title: `node ${i}`, status: 'active', visibility: 'shared', domain: 'pages' },
|
|
111
|
+
]))
|
|
112
|
+
entries['mem-0'].links = [{ to: 'mem-204', kind: 'event' }, { to: confirmed.id, kind: 'person' }]
|
|
113
|
+
entries['mem-204'].links = [{ to: 'mem-0', kind: 'relation' }, { to: provisional.id, kind: 'person' }]
|
|
114
|
+
state.savePrivateIndex(libraryId, { entries })
|
|
115
|
+
const edgePairs = page => page.edges.map(edge => [edge.from, edge.to])
|
|
116
|
+
const legacy = graph.getGraph({ libraryId, limit: 5000 })
|
|
117
|
+
assert.equal(legacy.nodes.length, 200)
|
|
118
|
+
assert.equal(legacy.limit, 200)
|
|
119
|
+
assert.equal(legacy.truncated, true)
|
|
120
|
+
assert.equal(legacy.total, undefined)
|
|
121
|
+
assert.equal(legacy.nextOffset, undefined)
|
|
122
|
+
assert.deepEqual(legacy.edges, [], 'legacy edges still require both endpoints in the result')
|
|
123
|
+
|
|
124
|
+
const first = graph.getGraph({ libraryId, offset: 0, limit: 5000 })
|
|
125
|
+
assert.deepEqual(first.nodes.map(node => node.id), legacy.nodes.map(node => node.id))
|
|
126
|
+
assert.equal(first.total, 207)
|
|
127
|
+
assert.equal(first.nextOffset, 200)
|
|
128
|
+
assert.equal(first.truncated, true)
|
|
129
|
+
assert.deepEqual(edgePairs(first), [['mem-0', 'mem-204'], ['mem-0', confirmed.id]])
|
|
130
|
+
|
|
131
|
+
const last = graph.getGraph({ libraryId, offset: first.nextOffset })
|
|
132
|
+
assert.equal(last.nodes.length, 7)
|
|
133
|
+
assert.equal(last.total, first.total)
|
|
134
|
+
assert.equal(last.nextOffset, null)
|
|
135
|
+
assert.equal(last.truncated, false)
|
|
136
|
+
assert.deepEqual(last.nodes.filter(node => node.kind === 'person').map(node => [node.id, node.status]), [
|
|
137
|
+
[confirmed.id, 'confirmed'], [provisional.id, 'provisional'],
|
|
138
|
+
])
|
|
139
|
+
assert.deepEqual(edgePairs(last), [['mem-204', 'mem-0'], ['mem-204', provisional.id]])
|
|
140
|
+
assert.equal(new Set([...first.nodes, ...last.nodes].map(node => node.id)).size, first.total)
|
|
141
|
+
|
|
142
|
+
const fullLast = graph.getGraph({ libraryId, offset: 205, limit: 2 })
|
|
143
|
+
assert.equal(fullLast.nodes.length, 2)
|
|
144
|
+
assert.equal(fullLast.nextOffset, null)
|
|
145
|
+
assert.equal(fullLast.truncated, false, 'a full final page has no remainder')
|
|
146
|
+
const beyond = graph.getGraph({ libraryId, offset: 500 })
|
|
147
|
+
assert.deepEqual([beyond.nodes, beyond.edges, beyond.total, beyond.nextOffset, beyond.truncated], [[], [], 207, null, false])
|
|
148
|
+
|
|
149
|
+
const focused = graph.getGraph({ libraryId, focusId: 'mem-0', offset: 500, depth: 1 })
|
|
150
|
+
assert.deepEqual(focused.nodes.map(node => node.id), ['mem-0', 'mem-204', confirmed.id])
|
|
151
|
+
assert.equal(focused.nextOffset, undefined, 'offset never changes bounded focus traversal')
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
test('graph domain matches normalized exact memory domains and derives only their linked entities', () => {
|
|
155
|
+
const { libraryId } = state.ensureSession('graph-domains', { cwd: path.join(home, 'rp-domains'), agentPreset: 'rp' })
|
|
156
|
+
const coding = graph.createEntity({ libraryId, name: 'Coding contact' })
|
|
157
|
+
const other = graph.createEntity({ libraryId, name: 'Other contact' })
|
|
158
|
+
const orphan = graph.createEntity({ libraryId, name: 'Unrelated orphan' })
|
|
159
|
+
state.savePrivateIndex(libraryId, { entries: {
|
|
160
|
+
'mem-code': { title: 'coding', domain: ' Coding ', links: [
|
|
161
|
+
{ to: coding.id, kind: 'person' }, { to: 'mem-code-2', kind: 'event' },
|
|
162
|
+
{ to: 'mem-legacy', kind: 'relation', note: 'cross-domain evidence' }, { to: 'mem-other', kind: 'event' },
|
|
163
|
+
{ to: 'mem-removed', kind: 'relation' },
|
|
164
|
+
] },
|
|
165
|
+
'mem-code-2': { title: 'also coding', domain: 'CODING' },
|
|
166
|
+
'mem-legacy': { title: 'legacy' },
|
|
167
|
+
'mem-other': { title: 'other', domain: 'coding-extra', links: [
|
|
168
|
+
{ to: other.id, kind: 'person' }, { to: 'mem-code', kind: 'event' },
|
|
169
|
+
] },
|
|
170
|
+
} })
|
|
171
|
+
const page = graph.getGraph({ libraryId, offset: 0, domain: ' CoDinG ', includeOrphans: true })
|
|
172
|
+
assert.deepEqual(page.domains, ['coding', 'coding-extra', 'general'])
|
|
173
|
+
assert.deepEqual(page.nodes.map(node => node.id), ['mem-code', 'mem-code-2', coding.id, 'mem-removed'])
|
|
174
|
+
assert.deepEqual(page.nodes.map(node => node.domain), ['coding', 'coding', undefined, undefined])
|
|
175
|
+
assert.equal(page.nodes.find(node => node.id === 'mem-removed').missing, true, 'genuine dangling links still have placeholders')
|
|
176
|
+
assert.equal(page.nodes.find(node => node.id === 'mem-code').degree, 3, 'out-of-domain neighbors do not even affect degree')
|
|
177
|
+
assert.deepEqual(page.edges.map(edge => edge.to), [coding.id, 'mem-code-2', 'mem-removed'])
|
|
178
|
+
assert.ok(!JSON.stringify(page).includes('cross-domain evidence'))
|
|
179
|
+
assert.deepEqual(graph.getGraph({ libraryId, domain: ' GENERAL ' }).nodes.map(node => [node.id, node.domain]), [['mem-legacy', 'general']])
|
|
180
|
+
assert.ok(graph.getGraph({ libraryId, domain: ' ', includeOrphans: true }).nodes.some(node => node.id === orphan.id))
|
|
181
|
+
assert.throws(() => graph.getGraph({ libraryId, focusId: orphan.id, domain: 'coding' }), /unknown or inaccessible graph focus/)
|
|
182
|
+
assert.throws(() => graph.getGraph({ libraryId, focusId: 'mem-other', domain: 'coding' }), /unknown or inaccessible graph focus/)
|
|
183
|
+
const none = graph.getGraph({ libraryId, offset: 0, domain: 'unknown', includeOrphans: true })
|
|
184
|
+
assert.deepEqual([none.nodes, none.edges, none.total, none.nextOffset, none.truncated], [[], [], 0, null, false])
|
|
185
|
+
assert.deepEqual(none.domains, page.domains, 'suggestions are gathered before applying the selected domain')
|
|
186
|
+
})
|
|
187
|
+
|
|
188
|
+
test('paged graph keeps bound-library, session eligibility and context checks before collecting domains or edges', () => {
|
|
189
|
+
const { libraryId } = state.ensureSession('graph-page-session', { cwd: path.join(home, 'rp-page-session'), agentPreset: 'rp' })
|
|
190
|
+
const exec = { agent: { session: { header: { id: 'graph-page-session' } } } }
|
|
191
|
+
const visible = graph.createEntity({ libraryId, name: 'Visible contact' })
|
|
192
|
+
const hidden = graph.createEntity({ libraryId, name: 'Hidden contact' })
|
|
193
|
+
state.savePrivateIndex(libraryId, { entries: {
|
|
194
|
+
'mem-visible': { title: 'visible', status: 'active', visibility: 'shared', domain: 'coding', links: [
|
|
195
|
+
{ to: visible.id, kind: 'person' }, { to: 'mem-secret', kind: 'relation' },
|
|
196
|
+
{ to: 'mem-other-context', kind: 'relation' }, { to: 'mem-archived', kind: 'relation' },
|
|
197
|
+
] },
|
|
198
|
+
'mem-secret': { title: 'secret', status: 'active', visibility: 'secret', domain: 'secret-domain', links: [{ to: hidden.id, kind: 'person' }] },
|
|
199
|
+
'mem-other-context': { title: 'private context', status: 'active', visibility: 'shared', domain: 'coding', context: { characterId: 'other' } },
|
|
200
|
+
'mem-archived': { title: 'archived', status: 'archived', visibility: 'shared', domain: 'coding' },
|
|
201
|
+
} })
|
|
202
|
+
const page = graph.getGraph({ exec, libraryId, offset: 0, limit: 1, domain: 'coding', includeOrphans: true })
|
|
203
|
+
assert.deepEqual(page.nodes.map(node => node.id), ['mem-visible'])
|
|
204
|
+
assert.deepEqual(page.domains, ['coding'])
|
|
205
|
+
assert.deepEqual(page.edges.map(edge => edge.to), [visible.id])
|
|
206
|
+
assert.equal(page.total, 2)
|
|
207
|
+
const last = graph.getGraph({ exec, libraryId, offset: page.nextOffset, limit: 1, domain: 'coding', includeOrphans: true })
|
|
208
|
+
assert.deepEqual(last.nodes.map(node => node.id), [visible.id])
|
|
209
|
+
assert.equal(last.nextOffset, null)
|
|
210
|
+
assert.throws(() => graph.getGraph({ exec, libraryId, focusId: hidden.id, offset: 0 }), /unknown or inaccessible graph focus/)
|
|
211
|
+
const otherLibrary = state.ensureSession('graph-page-unbound', { cwd: path.join(home, 'rp-page-unbound'), agentPreset: 'rp' }).libraryId
|
|
212
|
+
assert.throws(() => graph.getGraph({ exec, libraryId: otherLibrary, offset: 0 }), /library is not bound to this conversation/)
|
|
213
|
+
})
|
|
214
|
+
|
|
215
|
+
test('relationship snapshots preserve unknowns, evidence, boundaries and previous versions', () => {
|
|
216
|
+
const a = graph.createEntity({ libraryId, name: 'A' })
|
|
217
|
+
const b = graph.createEntity({ libraryId, name: 'B' })
|
|
218
|
+
const initial = graph.updateRelationship({ libraryId, fromId: a.id, toId: b.id,
|
|
219
|
+
state: { trust: .7, boundaries: ['Do not volunteer private stories.'], openPromises: ['Answer on Friday.'] },
|
|
220
|
+
evidenceIds: ['mem-2'], reason: 'Friday promise is explicit evidence.' }).relationship
|
|
221
|
+
assert.equal(initial.state.warmth, null, 'unknown is not a neutral or affectionate score')
|
|
222
|
+
graph.updateRelationship({ libraryId, fromId: a.id, toId: b.id,
|
|
223
|
+
state: { trust: .75, boundaries: initial.state.boundaries, callbacks: ['the station roof'], openPromises: [] },
|
|
224
|
+
evidenceIds: ['mem-2'], reason: 'The answer was delivered explicitly.' })
|
|
225
|
+
const next = graph.getRelationship({ libraryId, fromId: a.id, toId: b.id }).relationship
|
|
226
|
+
assert.equal(next.history.length, 1)
|
|
227
|
+
assert.deepEqual(next.history[0].state.openPromises, ['Answer on Friday.'])
|
|
228
|
+
assert.ok(next.history[0].validTo)
|
|
229
|
+
assert.throws(() => graph.updateRelationship({ libraryId, fromId: a.id, toId: b.id, state: {}, evidenceIds: [], reason: 'no evidence' }), /evidence/)
|
|
230
|
+
})
|
|
231
|
+
|
|
232
|
+
test('relationship history is capped while the current snapshot and evidence stay', () => {
|
|
233
|
+
const a = graph.createEntity({ libraryId, name: 'History A' })
|
|
234
|
+
const b = graph.createEntity({ libraryId, name: 'History B' })
|
|
235
|
+
for (let i = 0; i < 55; i += 1) {
|
|
236
|
+
graph.updateRelationship({
|
|
237
|
+
libraryId, fromId: a.id, toId: b.id,
|
|
238
|
+
state: { trust: 0.5, callbacks: [`callback ${i}`] },
|
|
239
|
+
evidenceIds: ['mem-2'], reason: `Evidence-backed revision ${i} is explicit.`,
|
|
240
|
+
})
|
|
241
|
+
}
|
|
242
|
+
const relation = graph.getRelationship({ libraryId, fromId: a.id, toId: b.id }).relationship
|
|
243
|
+
assert.equal(relation.history.length, 50, 'old revisions are capped')
|
|
244
|
+
assert.equal(relation.revision, 55)
|
|
245
|
+
assert.deepEqual(relation.state.callbacks, ['callback 54'])
|
|
246
|
+
assert.deepEqual(relation.evidenceIds, ['mem-2'])
|
|
247
|
+
})
|
|
248
|
+
|
|
249
|
+
test('relationship retention is a pure wall-clock decay with access reinforcement', () => {
|
|
250
|
+
assert.equal(graph.relationshipRetentionAt(1, 24), 0.5)
|
|
251
|
+
assert.equal(graph.relationshipRetentionAt(1, 48), 0.25)
|
|
252
|
+
assert.equal(graph.relationshipRetentionAt(1, -5), 1, 'clock rollback never boosts retention')
|
|
253
|
+
const accessed = graph.accessRelationship(0.5, { now: 10_000_000, lastAccessAt: null })
|
|
254
|
+
assert.ok(accessed > 0.5 && accessed < 1)
|
|
255
|
+
assert.equal(graph.accessRelationship(0.5, { now: 10_000_000, lastAccessAt: new Date(10_000_000 - 1000).toISOString() }), 0.5, 'cooldown blocks a second bump')
|
|
256
|
+
})
|
|
257
|
+
|
|
258
|
+
test('relationship snapshots persist retention, last access and next review', () => {
|
|
259
|
+
const a = graph.createEntity({ libraryId, name: 'Lifecycle A' })
|
|
260
|
+
const b = graph.createEntity({ libraryId, name: 'Lifecycle B' })
|
|
261
|
+
const created = graph.updateRelationship({
|
|
262
|
+
libraryId, fromId: a.id, toId: b.id,
|
|
263
|
+
state: { trust: 0.6 }, evidenceIds: ['mem-2'], reason: 'First explicit evidence for this relation.',
|
|
264
|
+
}).relationship
|
|
265
|
+
assert.equal(created.retention_strength, 1)
|
|
266
|
+
assert.ok(created.last_access_at)
|
|
267
|
+
assert.ok(created.next_review_at)
|
|
268
|
+
const next = graph.updateRelationship({
|
|
269
|
+
libraryId, fromId: a.id, toId: b.id,
|
|
270
|
+
state: { trust: 0.7 }, evidenceIds: ['mem-2'], reason: 'Second explicit evidence for this relation.',
|
|
271
|
+
}).relationship
|
|
272
|
+
assert.ok(next.retention_strength > 0 && next.retention_strength <= 1)
|
|
273
|
+
assert.ok(next.last_access_at >= created.last_access_at)
|
|
274
|
+
assert.equal(next.history[0].retention_strength, 1)
|
|
275
|
+
})
|
|
276
|
+
|
|
277
|
+
test('a dangling edge is reported as missing, matching the graph view placeholder', () => {
|
|
278
|
+
// mem-1 was deleted from the index in an earlier test; its inbound edge from
|
|
279
|
+
// mem-0 remains. getGraph renders a [deleted] placeholder for it, so recall
|
|
280
|
+
// must report the same edge with the same missing mark instead of dropping it.
|
|
281
|
+
const view = graph.getGraph({ libraryId, focusId: 'mem-0', depth: 1 })
|
|
282
|
+
assert.equal(view.nodes.find(node => node.id === 'mem-1').missing, true)
|
|
283
|
+
const related = graph.recallRelations(libraryId, state.loadPrivateIndex(libraryId), 'mem-0', null)
|
|
284
|
+
const dangling = related.find(item => item.id === 'mem-1')
|
|
285
|
+
assert.ok(dangling, 'the edge must be reported, not silently dropped')
|
|
286
|
+
assert.equal(dangling.missing, true)
|
|
287
|
+
assert.equal(dangling.title, '[deleted]')
|
|
288
|
+
// mem-2 still links at the Zhou entity: a resolvable relation is marked false.
|
|
289
|
+
const zhou = Object.values(graph.graphExport(libraryId).entities).find(entity => entity.name === 'Zhou')
|
|
290
|
+
const healthy = graph.recallRelations(libraryId, state.loadPrivateIndex(libraryId), 'mem-2', null)
|
|
291
|
+
assert.equal(healthy.find(item => item.id === zhou.id).missing, false)
|
|
292
|
+
})
|
|
293
|
+
|
|
294
|
+
// ---- §6.4 包 D:listEntities 的 linked 派生字段与孤立判定 ----
|
|
295
|
+
// 前提(合并稿 0.2 纠正):无 session 本来就返回全部实体——不"放开过滤",
|
|
296
|
+
// 只加 linked 标记;有 session 才按可见引用收窄。
|
|
297
|
+
|
|
298
|
+
test('listEntities: without a session every entity returns, with a correct derived linked flag', () => {
|
|
299
|
+
const binding = state.ensureSession('vis-all', { cwd: path.join(home, 'ws-vis-all'), agentPreset: 'rp' })
|
|
300
|
+
const lib = binding.libraryId
|
|
301
|
+
const anchored = graph.createEntity({ libraryId: lib, name: 'Anchored' })
|
|
302
|
+
const drifting = graph.createEntity({ libraryId: lib, name: 'Drifting' })
|
|
303
|
+
state.savePrivateIndex(lib, { entries: {
|
|
304
|
+
'mem-anchored': { title: 'anchored', content: 'scene', layer: 'long_term', status: 'active', visibility: 'shared',
|
|
305
|
+
links: [{ to: anchored.id, kind: 'person', note: 'Anchored is the person this scene is about.', confidence: 0.9 }] },
|
|
306
|
+
} })
|
|
307
|
+
const all = graph.listEntities({ libraryId: lib })
|
|
308
|
+
assert.equal(all.items.length, 2, 'without a session the full entity list comes back')
|
|
309
|
+
assert.equal(all.total, 2)
|
|
310
|
+
const anchoredRow = all.items.find(entity => entity.id === anchored.id)
|
|
311
|
+
const driftingRow = all.items.find(entity => entity.id === drifting.id)
|
|
312
|
+
assert.equal(anchoredRow.linked, true, 'a link target is linked')
|
|
313
|
+
assert.equal(driftingRow.linked, false, 'an entity nothing points at is unlinked')
|
|
314
|
+
// linked 是运行时派生字段:实体文件里绝不出现它,启动 prune 的判定不受污染。
|
|
315
|
+
assert.equal(graph.graphExport(lib).entities[drifting.id].linked, undefined, 'the derived flag is never written back')
|
|
316
|
+
})
|
|
317
|
+
|
|
318
|
+
test('recallRelations reuses its inverted index until the index file changes', () => {
|
|
319
|
+
const binding = state.ensureSession('graph-cache', { cwd: path.join(home, 'rp-cache'), agentPreset: 'rp' })
|
|
320
|
+
const lib = binding.libraryId
|
|
321
|
+
const entity = graph.createEntity({ libraryId: lib, name: 'CacheEntity' })
|
|
322
|
+
state.savePrivateIndex(lib, { entries: {
|
|
323
|
+
'mem-cache-a': { title: 'a', content: 'a', status: 'active', visibility: 'shared',
|
|
324
|
+
links: [{ to: entity.id, kind: 'person', note: 'CacheEntity is the person in this scene.', confidence: 0.8 }] },
|
|
325
|
+
} })
|
|
326
|
+
const first = graph.recallRelations(lib, state.loadPrivateIndex(lib), 'mem-cache-a', null)
|
|
327
|
+
const second = graph.recallRelations(lib, state.loadPrivateIndex(lib), 'mem-cache-a', null)
|
|
328
|
+
assert.deepEqual(second, first, 'an unchanged index returns the same edges')
|
|
329
|
+
state.updatePrivateIndex(lib, index => {
|
|
330
|
+
index.entries['mem-cache-b'] = { title: 'b', content: 'b', status: 'active', visibility: 'shared',
|
|
331
|
+
links: [{ to: 'mem-cache-a', kind: 'event', note: 'The same event seen from another side.', confidence: 0.9 }] }
|
|
332
|
+
})
|
|
333
|
+
const refreshed = graph.recallRelations(lib, state.loadPrivateIndex(lib), 'mem-cache-a', null)
|
|
334
|
+
assert.equal(refreshed.length, 2, 'a new index write invalidates the cached inverted index')
|
|
335
|
+
assert.ok(refreshed.some(item => item.id === 'mem-cache-b'))
|
|
336
|
+
assert.ok(refreshed.some(item => item.id === entity.id))
|
|
337
|
+
})
|
|
338
|
+
|
|
339
|
+
test('listEntities: with a session only visible references count, and relationship evidence is session-filtered too', () => {
|
|
340
|
+
const binding = state.ensureSession('vis-scoped', { cwd: path.join(home, 'ws-vis-scoped'), agentPreset: 'rp' })
|
|
341
|
+
const lib = binding.libraryId
|
|
342
|
+
const exec = { agent: { session: { header: { id: 'vis-scoped' } } } }
|
|
343
|
+
const linked = graph.createEntity({ libraryId: lib, name: 'Linked' })
|
|
344
|
+
const secretTarget = graph.createEntity({ libraryId: lib, name: 'SecretTarget' })
|
|
345
|
+
const relVisible = graph.createEntity({ libraryId: lib, name: 'RelVisible' })
|
|
346
|
+
const relHidden = graph.createEntity({ libraryId: lib, name: 'RelHidden' })
|
|
347
|
+
const lonely = graph.createEntity({ libraryId: lib, name: 'Lonely' })
|
|
348
|
+
state.savePrivateIndex(lib, { entries: {
|
|
349
|
+
'mem-open': { title: 'open', content: 'open scene', layer: 'long_term', status: 'active', visibility: 'shared',
|
|
350
|
+
links: [{ to: linked.id, kind: 'person', note: 'Linked is the person this scene is about.', confidence: 0.9 }] },
|
|
351
|
+
'mem-hidden': { title: 'hidden', content: 'secret scene', layer: 'long_term', status: 'active', visibility: 'secret', context: { characterId: 'other' },
|
|
352
|
+
links: [{ to: secretTarget.id, kind: 'person', note: 'Secret is the person this scene is about.', confidence: 0.9 }] },
|
|
353
|
+
} })
|
|
354
|
+
// 关系快照也算引用:evidence 指向本会话可见记忆的算,指不可见记忆的不算。
|
|
355
|
+
graph.updateRelationship({ libraryId: lib, fromId: linked.id, toId: relVisible.id, state: { trust: 0.5 }, evidenceIds: ['mem-open'], reason: 'The open scene is explicit evidence.' })
|
|
356
|
+
graph.updateRelationship({ libraryId: lib, fromId: secretTarget.id, toId: relHidden.id, state: { trust: 0.5 }, evidenceIds: ['mem-hidden'], reason: 'The hidden scene is the only evidence here.' })
|
|
357
|
+
const scoped = graph.listEntities({ exec, libraryId: lib })
|
|
358
|
+
const ids = scoped.items.map(entity => entity.id).sort()
|
|
359
|
+
assert.deepEqual(ids, [linked.id, relVisible.id].sort(),
|
|
360
|
+
'a visible entry link and a relationship with visible evidence both count; secret-referenced and orphan entities do not')
|
|
361
|
+
assert.ok(scoped.items.every(entity => entity.linked === true), 'every session-visible row is linked by construction')
|
|
362
|
+
// 无 session:全量计入(两条快照都算引用),Linked/RelVisible/SecretTarget/RelHidden
|
|
363
|
+
// 都是 linked=true,只有 Lonely 是孤立。
|
|
364
|
+
const all = graph.listEntities({ libraryId: lib })
|
|
365
|
+
assert.equal(all.items.length, 5, 'no session means every entity, referenced or not')
|
|
366
|
+
const linkedMap = Object.fromEntries(all.items.map(entity => [entity.id, entity.linked]))
|
|
367
|
+
for (const entity of [linked, relVisible, secretTarget, relHidden]) {
|
|
368
|
+
assert.equal(linkedMap[entity.id], true, `${entity.name} is referenced and must not be misjudged as an orphan`)
|
|
369
|
+
}
|
|
370
|
+
assert.equal(linkedMap[lonely.id], false, 'the untouched entity stays unlinked')
|
|
371
|
+
})
|