dsh-memu-memory 1.4.4 → 1.4.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +3 -1
- package/CHANGELOG.md +11 -0
- package/README.md +33 -14
- package/README.zh.md +34 -12
- package/assets/screenshots/drawer.jpg +0 -0
- package/assets/screenshots/libraries.jpg +0 -0
- package/assets/screenshots/mobile.jpg +0 -0
- package/assets/screenshots/relations.jpg +0 -0
- package/assets/screenshots/settings.jpg +0 -0
- package/assets/screenshots/webui-dark.jpg +0 -0
- package/assets/screenshots/webui-memory.jpg +0 -0
- package/docs/client-conventions.md +19 -3
- package/docs/contributing.md +4 -2
- package/docs/env-and-commands.md +9 -1
- package/docs/graph-scale.md +214 -0
- package/docs/lexicon-recall.md +100 -0
- package/docs/mcp-porting.md +8 -6
- package/docs/memory-semantics.md +35 -3
- package/docs/pending-work.md +3 -1
- package/docs/retention.md +83 -0
- package/docs/rt-index.md +452 -0
- package/docs/scale-performance.md +101 -0
- package/docs/token-budget.md +92 -0
- package/docs/troubleshooting.md +50 -11
- package/lib/auto-link/apply.js +7 -2
- package/lib/auto-link/candidates.js +32 -10
- package/lib/backup.js +67 -32
- package/lib/binding-diagnostics.js +1 -1
- package/lib/bounded-cache.js +80 -0
- package/lib/client.js +2096 -409
- package/lib/consolidation-ledger.js +21 -4
- package/lib/consolidation-worker.js +14 -1
- package/lib/cycle.js +16 -11
- package/lib/diagnostic-context.js +372 -0
- package/lib/diagnostics.js +147 -61
- package/lib/embedding-download.js +7 -8
- package/lib/engine-sources.js +286 -0
- package/lib/engine.js +23 -10
- package/lib/entity-repair.js +43 -13
- package/lib/entity-review.js +64 -0
- package/lib/environment-check.js +348 -36
- package/lib/feedback.js +25 -0
- package/lib/global-graph.js +46 -2
- package/lib/graph-adjacency-index.js +525 -0
- package/lib/graph-overview.js +293 -0
- package/lib/graph.js +658 -105
- package/lib/inbound.js +44 -15
- package/lib/index-pages.js +57 -0
- package/lib/index.js +44 -6
- package/lib/inject.js +119 -50
- package/lib/lexicon-rt-source.js +25 -0
- package/lib/lexicon-rt-worker.js +26 -0
- package/lib/lexicon-rt.js +224 -0
- package/lib/lexicon-scope.js +56 -5
- package/lib/lexicon.js +143 -54
- package/lib/local-vector-service.js +3 -0
- package/lib/maintenance-ledger.js +542 -12
- package/lib/managed-runtime.js +260 -80
- package/lib/media.js +1 -1
- package/lib/memory.js +564 -131
- package/lib/port-surface.js +96 -11
- package/lib/private.js +47 -21
- package/lib/recall-cache.js +20 -34
- package/lib/retention.js +173 -0
- package/lib/rpc.js +135 -52
- package/lib/rt-index/builder.js +209 -0
- package/lib/rt-index/format.js +231 -0
- package/lib/rt-index/manifest.js +105 -0
- package/lib/rt-index/reader.js +673 -0
- package/lib/rt-index-retention.js +151 -0
- package/lib/rt-index-service.js +283 -0
- package/lib/rt-index-worker.js +21 -0
- package/lib/salient-wake.js +55 -4
- package/lib/state.js +195 -24
- package/lib/temporal.js +39 -13
- package/lib/token-measurement.js +126 -0
- package/lib/tools.js +101 -6
- package/lib/turn-log.js +108 -3
- package/lib/webui/graph-layout-worker.js +68 -0
- package/lib/webui/graph-local-physics.js +390 -0
- package/lib/webui/graph-spatial-grid.js +64 -0
- package/lib/webui/graph-view.bundle.js +6 -1
- package/lib/webui/graph-view.js +478 -135
- package/lib/webui/graph-webgl-renderer.js +74 -0
- package/lib/webui/standalone.js +1 -1
- package/lib/worker-pool.js +30 -4
- package/package.json +3 -2
- package/scripts/acceptance-local.mjs +4 -10
- package/scripts/bench-all-libraries.mjs +47 -0
- package/scripts/check-classic-graph-browser.mjs +189 -0
- package/scripts/check-client-browser.mjs +929 -0
- package/scripts/check-graph-browser.mjs +6 -0
- package/scripts/check-graph-elastic-browser.mjs +139 -0
- package/scripts/check-memory-review-browser.mjs +124 -0
- package/scripts/entity-dedupe.mjs +19 -3
- package/scripts/graph-artifact-provenance.mjs +30 -0
- package/scripts/graph-freeze-manifest.mjs +129 -0
- package/scripts/graph-geometry-fixture.mjs +40 -0
- package/scripts/graph-physics-bench.mjs +218 -0
- package/scripts/graph-scale-bench.mjs +213 -0
- package/scripts/graph-scale-memory.mjs +48 -0
- package/scripts/graph-service-bench.mjs +72 -0
- package/scripts/make-readme-shots.mjs +17 -5
- package/scripts/preview.mjs +1 -0
- package/scripts/rebuild-rt.mjs +18 -0
- package/scripts/scale-bench.mjs +183 -0
- package/scripts/simulate-injection.mjs +47 -8
- package/scripts/verify-host-fixtures.mjs +108 -0
- package/scripts/verify-host-recovery.mjs +565 -0
- package/scripts/verify-vector-recovery.mjs +740 -0
- package/tests/auto-link-candidates.test.mjs +20 -0
- package/tests/browser.test.mjs +16 -2
- package/tests/client-export.test.mjs +16 -1
- package/tests/client-pagination.test.mjs +1495 -0
- package/tests/client-render.test.mjs +223 -28
- package/tests/consolidation-fork.test.mjs +93 -10
- package/tests/consolidation-ledger.test.mjs +17 -1
- package/tests/diagnostic-entity-candidates.test.mjs +54 -0
- package/tests/diagnostic-report.test.mjs +429 -0
- package/tests/diagnostic-wiring.test.mjs +88 -0
- package/tests/diagnostics.test.mjs +27 -0
- package/tests/embedding-download.test.mjs +44 -0
- package/tests/engine-source-settings.test.mjs +69 -0
- package/tests/engine-sources.test.mjs +145 -0
- package/tests/entity-dedupe.test.mjs +143 -0
- package/tests/entity-merge-review.test.mjs +363 -0
- package/tests/entity-merge.test.mjs +12 -4
- package/tests/entity-review.test.mjs +87 -0
- package/tests/environment-check.test.mjs +51 -1
- package/tests/feedback.test.mjs +95 -0
- package/tests/graph-adjacency-index.test.mjs +536 -0
- package/tests/graph-cascade.test.mjs +229 -0
- package/tests/graph-import-diagnostic.test.mjs +25 -0
- package/tests/graph-overview.test.mjs +709 -0
- package/tests/graph-session-authorization.test.mjs +419 -0
- package/tests/graph-view-geometry.test.mjs +426 -0
- package/tests/graph-view-scale.test.mjs +88 -0
- package/tests/graph-view.test.mjs +444 -5
- package/tests/graph.test.mjs +43 -0
- package/tests/help-page.test.mjs +128 -54
- package/tests/inbound.test.mjs +19 -0
- package/tests/index-pagination.test.mjs +113 -0
- package/tests/index-read-view.test.mjs +212 -0
- package/tests/inject-guide.test.mjs +9 -0
- package/tests/injection-dedupe.test.mjs +213 -0
- package/tests/lexicon-recall.test.mjs +641 -0
- package/tests/lexicon-rt-lifecycle.test.mjs +196 -0
- package/tests/lexicon-rt.test.mjs +468 -0
- package/tests/local-embedding-worker.test.mjs +38 -0
- package/tests/local-vector-service.test.mjs +32 -3
- package/tests/maintenance-compat.test.mjs +128 -0
- package/tests/maintenance-settlement.test.mjs +1003 -0
- package/tests/managed-memory.test.mjs +39 -11
- package/tests/managed-runtime.test.mjs +109 -0
- package/tests/memory-fields.test.mjs +24 -12
- package/tests/observed-evidence.test.mjs +52 -0
- package/tests/port-surface.test.mjs +93 -1
- package/tests/recall-cache.test.mjs +56 -3
- package/tests/recall-dedup.test.mjs +63 -2
- package/tests/retention.test.mjs +260 -0
- package/tests/rt-index-cross-library.test.mjs +230 -0
- package/tests/rt-index-integration.test.mjs +194 -0
- package/tests/rt-index-retention.test.mjs +552 -0
- package/tests/rt-index-scale.test.mjs +204 -0
- package/tests/rt-index.test.mjs +705 -0
- package/tests/salient-date-boundaries.test.mjs +121 -0
- package/tests/salient-wake.test.mjs +29 -0
- package/tests/scale-bench.test.mjs +53 -0
- package/tests/settings-plumbing.test.mjs +25 -0
- package/tests/simulate-injection.test.mjs +15 -1
- package/tests/standalone-navigation.test.mjs +44 -0
- package/tests/state.test.mjs +51 -0
- package/tests/temporal-precision.test.mjs +102 -0
- package/tests/temporal.test.mjs +10 -6
- package/tests/token-measurement.test.mjs +98 -0
- package/tests/tool-schema.test.mjs +41 -5
- package/tests/tools.test.mjs +6 -3
- package/tests/turn-token-budget.test.mjs +149 -0
- package/tests/webui-auth.test.mjs +57 -1
- package/tests/webui.test.mjs +40 -15
|
@@ -0,0 +1,709 @@
|
|
|
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 { buildGraphOverview, selectGraphOverview, cachedGraphOverview } from '../lib/graph-overview.js'
|
|
7
|
+
|
|
8
|
+
const home = fs.mkdtempSync(path.join(os.tmpdir(), 'dsh-p4-overview-'))
|
|
9
|
+
process.env.DSH_MEMU_HOME = home
|
|
10
|
+
process.env.DSH_MEMU_MEMORY_HOME = home
|
|
11
|
+
const state = await import('../lib/state.js')
|
|
12
|
+
const { getGraphOverview, getGraph, createEntity, renameEntity, recallRelations } = await import('../lib/graph.js')
|
|
13
|
+
const { getGlobalGraphOverview, getGlobalGraph } = await import('../lib/global-graph.js')
|
|
14
|
+
const { dispatch } = await import('../lib/rpc.js')
|
|
15
|
+
const { indexDir, dataDir, setWarnSink } = await import('../lib/paths.js')
|
|
16
|
+
const { graphAdjacencyLocation, graphAdjacencyDiagnostics } = await import('../lib/graph-adjacency-index.js')
|
|
17
|
+
test.after(() => { state.flushState(); fs.rmSync(home, { recursive: true, force: true }) })
|
|
18
|
+
|
|
19
|
+
function fixture(count, connected = true) {
|
|
20
|
+
return { nodes: Array.from({ length: count }, (_, i) => ({ id: `n-${String(i).padStart(6, '0')}`, title: `Node ${i}`, kind: 'memory', strength: .5, domain: 'rp' })),
|
|
21
|
+
edges: connected ? Array.from({ length: count - 1 }, (_, i) => ({ from: `n-${String(i).padStart(6, '0')}`, to: `n-${String(i + 1).padStart(6, '0')}`, kind: 'relation' })) : [], domains: ['rp'] }
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
test('components, cluster IDs, geometry and edge ordering are independent of input order', () => {
|
|
25
|
+
const data = fixture(2200), original = structuredClone(data)
|
|
26
|
+
const a = buildGraphOverview(data, { revision: 'r1' })
|
|
27
|
+
const b = buildGraphOverview({ ...data, nodes: data.nodes.toReversed(), edges: data.edges.toReversed() }, { revision: 'r1' })
|
|
28
|
+
assert.deepEqual(a.clusters, b.clusters)
|
|
29
|
+
assert.equal(a.totals.components, 1); assert.equal(a.totals.clusters, 3)
|
|
30
|
+
assert.equal(a.clusters.reduce((sum, node) => sum + node.count, 0), 2200)
|
|
31
|
+
assert.deepEqual(data, original)
|
|
32
|
+
assert.deepEqual(a.nodes.map(node => [node.id, node.x, node.y]), b.nodes.map(node => [node.id, node.x, node.y]))
|
|
33
|
+
assert.equal(new Set(a.clusters.map(cluster => cluster.id)).size, 3)
|
|
34
|
+
assert.ok(a.clusterEdges.length > 0)
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
test('overview caps each layer, reports exact totals and paginates without loss', () => {
|
|
38
|
+
const disconnected = buildGraphOverview(fixture(321, false))
|
|
39
|
+
const first = selectGraphOverview(disconnected, { limit: 10000 })
|
|
40
|
+
assert.equal(first.mode, 'clusters'); assert.equal(first.nodes.length, 200)
|
|
41
|
+
assert.equal(first.totals.nodes, 321); assert.equal(first.total, 321)
|
|
42
|
+
const second = selectGraphOverview(disconnected, { offset: first.nextOffset })
|
|
43
|
+
assert.equal(second.nodes.length, 121); assert.equal(second.nextOffset, null)
|
|
44
|
+
assert.equal(new Set([...first.nodes, ...second.nodes].map(node => node.id)).size, 321)
|
|
45
|
+
const model = buildGraphOverview(fixture(3400))
|
|
46
|
+
const detail = selectGraphOverview(model, { bbox: model.bbox, zoom: 1, limit: 10000 })
|
|
47
|
+
assert.equal(detail.mode, 'detail'); assert.equal(detail.nodes.length, 3000); assert.equal(detail.total, 3400)
|
|
48
|
+
assert.equal(detail.nextOffset, 3000); assert.equal(detail.truncated, true)
|
|
49
|
+
const rest = selectGraphOverview(model, { bbox: model.bbox, zoom: 1, offset: detail.nextOffset })
|
|
50
|
+
assert.equal(rest.nodes.length, 400); assert.equal(rest.nextOffset, null)
|
|
51
|
+
const ids = new Set(detail.nodes.map(node => node.id))
|
|
52
|
+
assert.ok(detail.edges.every(edge => ids.has(edge.from) && ids.has(edge.to)))
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
test('classic view pages every authorized node and retains edges across page boundaries without grid coordinates', () => {
|
|
56
|
+
const model = buildGraphOverview(fixture(3405), { revision: 'classic-r1', positionScope: 'authorized-classic' })
|
|
57
|
+
const nodes = [], edges = []
|
|
58
|
+
let offset = 0
|
|
59
|
+
do {
|
|
60
|
+
const page = selectGraphOverview(model, { layout: 'classic', limit: 3000, offset,
|
|
61
|
+
bbox: [1e8, 1e8, 1e8 + 1, 1e8 + 1], zoom: 5 })
|
|
62
|
+
assert.equal(page.mode, 'classic'); assert.equal(page.positionScope, 'authorized-classic')
|
|
63
|
+
assert.equal(page.nodes.some(node => node.kind === 'cluster' || 'x' in node || 'y' in node), false)
|
|
64
|
+
assert.ok(page.nodes.length <= 3000)
|
|
65
|
+
nodes.push(...page.nodes); edges.push(...page.edges)
|
|
66
|
+
offset = page.nextOffset
|
|
67
|
+
} while (offset !== null)
|
|
68
|
+
assert.equal(nodes.length, 3405); assert.equal(new Set(nodes.map(node => node.id)).size, 3405)
|
|
69
|
+
assert.equal(edges.length, 3404)
|
|
70
|
+
assert.ok(edges.some(edge => edge.from === 'n-002999' && edge.to === 'n-003000'), 'the page boundary cannot cut a recorded link')
|
|
71
|
+
assert.equal(selectGraphOverview(model).mode, 'clusters', 'other overview callers keep their existing bounded API')
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
test('search matches normalized node IDs/titles and recorded edge metadata across the entire authorized model', () => {
|
|
75
|
+
const graph = fixture(5, false)
|
|
76
|
+
graph.nodes[0].title = 'Alpha 東京'; graph.nodes[0].content = 'body-only-do-not-search'
|
|
77
|
+
graph.edges = [
|
|
78
|
+
{ from: 'n-000001', to: 'n-000002', kind: 'relation', note: 'Note-Marker', source: 'user' },
|
|
79
|
+
{ from: 'n-000003', to: 'n-000004', kind: 'person', source: 'auto', rule: 'entity-mention',
|
|
80
|
+
evidence: { rule: 'entity-mention', field: 'title', surface: 'Surface-Marker', entityId: 'entity-technical-id', offset: 77,
|
|
81
|
+
citations: [{ quote: 'recorded-quote-marker', sessionId: 'session-technical-id' }] } },
|
|
82
|
+
]
|
|
83
|
+
const original = structuredClone(graph), model = buildGraphOverview(graph)
|
|
84
|
+
const ids = args => selectGraphOverview(model, args).nodes.map(node => node.id)
|
|
85
|
+
assert.deepEqual(ids({ query: ' \u3000ALPHA 東京 ' }), ['n-000000'])
|
|
86
|
+
assert.deepEqual(ids({ query: ' N-000004 ' }), ['n-000004'])
|
|
87
|
+
assert.deepEqual(ids({ query: 'note-marker' }), ['n-000001', 'n-000002'])
|
|
88
|
+
for (const query of ['surface-marker', 'ENTITY-MENTION', 'entity-technical-id', 'recorded-quote-marker', 'session-technical-id', '77', 'person', 'auto']) {
|
|
89
|
+
const result = selectGraphOverview(model, { query })
|
|
90
|
+
assert.deepEqual(result.nodes.map(node => node.id), ['n-000003', 'n-000004'], query)
|
|
91
|
+
assert.equal(result.edges.length, 1); assert.deepEqual(result.edges[0].evidence, graph.edges[1].evidence)
|
|
92
|
+
}
|
|
93
|
+
const found = selectGraphOverview(model, { query: ' ALPHA 東京 ', bbox: [1e9, 1e9, 1e9 + 1, 1e9 + 1],
|
|
94
|
+
clusterId: 'stale-cluster-not-in-this-view', focusId: 'stale-focus-not-in-this-view', zoom: 0 })
|
|
95
|
+
assert.equal(found.mode, 'detail'); assert.equal(found.query, 'alpha 東京'); assert.equal(found.total, 1)
|
|
96
|
+
assert.equal(found.clusterId, null); assert.equal(found.focusId, null); assert.deepEqual(found.clusters, [])
|
|
97
|
+
assert.doesNotMatch(JSON.stringify(found), /body-only-do-not-search|"content"|"body"/)
|
|
98
|
+
assert.equal(selectGraphOverview(model, { query: 'body-only-do-not-search' }).total, 0)
|
|
99
|
+
found.nodes[0].title = 'mutated response'
|
|
100
|
+
assert.equal(selectGraphOverview(model, { query: 'alpha' }).nodes[0].title, 'Alpha 東京')
|
|
101
|
+
assert.deepEqual(graph, original, 'search must neither mutate source rows nor retain response mutations')
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
test('classic search preserves links between pages while excluding nodes outside the search', () => {
|
|
105
|
+
const data = fixture(3406)
|
|
106
|
+
data.nodes.at(-1).title = 'Excluded result'
|
|
107
|
+
const model = buildGraphOverview(data)
|
|
108
|
+
const first = selectGraphOverview(model, { layout: 'classic', query: 'Node', limit: 3000 })
|
|
109
|
+
const second = selectGraphOverview(model, { layout: 'classic', query: 'Node', offset: first.nextOffset })
|
|
110
|
+
assert.equal(first.total, 3405); assert.equal(first.nextOffset, 3000); assert.equal(second.nextOffset, null)
|
|
111
|
+
const nodes = [...first.nodes, ...second.nodes], edges = [...first.edges, ...second.edges]
|
|
112
|
+
assert.equal(nodes.length, 3405); assert.equal(edges.length, 3404)
|
|
113
|
+
assert.ok(first.edges.some(edge => edge.from === 'n-002999' && edge.to === 'n-003000'))
|
|
114
|
+
assert.ok(edges.every(edge => edge.from !== 'n-003405' && edge.to !== 'n-003405'))
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
test('empty search preserves overview behavior and malformed search is rejected before model construction', () => {
|
|
118
|
+
const model = buildGraphOverview(fixture(205, false)), expected = selectGraphOverview(model)
|
|
119
|
+
for (const query of [undefined, null, '', ' \u3000 ']) assert.deepEqual(selectGraphOverview(model, { query }), expected)
|
|
120
|
+
const clusterId = expected.nodes[0].id
|
|
121
|
+
assert.deepEqual(selectGraphOverview(model, { clusterId, query: ' ' }), selectGraphOverview(model, { clusterId }))
|
|
122
|
+
assert.equal(selectGraphOverview(model, { query: 'q'.repeat(256) }).total, 0)
|
|
123
|
+
let builds = 0, opens = 0
|
|
124
|
+
const options = { key: 'invalid-search-params', revision: 'r1', build() { builds++; return fixture(1) }, adjacency() { opens++ } }
|
|
125
|
+
for (const query of [0, 1, false, true, {}, [], ['node'], NaN, 'x'.repeat(257), ' '.repeat(257), '\ufb03'.repeat(86)]) {
|
|
126
|
+
assert.throws(() => selectGraphOverview(model, { query }), /graph query/)
|
|
127
|
+
assert.throws(() => cachedGraphOverview(options, { query }), /graph query/)
|
|
128
|
+
}
|
|
129
|
+
assert.equal(builds, 0); assert.equal(opens, 0, 'invalid search must not build or open derived storage')
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
test('getGraph RPC searches unloaded nodes, ignores viewport/cluster and paginates all matches at the detail cap', async () => {
|
|
133
|
+
const libraryId = state.ensureSession('graph-search-pages', { cwd: path.join(home, 'search-pages'), agentPreset: 'rp' }).libraryId
|
|
134
|
+
const count = 3041, graph = fixture(count, false)
|
|
135
|
+
const entries = Object.fromEntries(graph.nodes.map(node => [node.id, {
|
|
136
|
+
title: `Service Node ${node.id}`, content: 'body-only-service-marker', status: 'active', visibility: 'shared', domain: 'rp',
|
|
137
|
+
}]))
|
|
138
|
+
state.savePrivateIndex(libraryId, { entries })
|
|
139
|
+
const sourceFile = path.join(indexDir(), `${libraryId}.json`), sourceBefore = fs.readFileSync(sourceFile)
|
|
140
|
+
const args = { libraryId, overview: true }
|
|
141
|
+
const overview = await dispatch({}, 'getGraph', args)
|
|
142
|
+
assert.equal(overview.mode, 'clusters'); assert.equal(overview.nodes.length, 200)
|
|
143
|
+
const loaded = new Set(overview.nodes.map(node => node.representativeId))
|
|
144
|
+
const target = graph.nodes.find(node => !loaded.has(node.id))
|
|
145
|
+
assert.ok(target, 'the search fixture must include a node outside the loaded overview page')
|
|
146
|
+
const found = await dispatch({}, 'getGraph', { ...args, query: ` ${target.id.toUpperCase()} `,
|
|
147
|
+
bbox: [1e9, 1e9, 1e9 + 1, 1e9 + 1], clusterId: overview.nodes[0].id })
|
|
148
|
+
assert.deepEqual(found.nodes.map(node => node.id), [target.id]); assert.equal(found.total, 1); assert.equal(found.mode, 'detail')
|
|
149
|
+
const first = await dispatch({}, 'getGraph', { ...args, query: ' SERVICE NODE ', limit: 9999 })
|
|
150
|
+
assert.equal(first.nodes.length, 3000); assert.equal(first.limit, 3000); assert.equal(first.total, count)
|
|
151
|
+
assert.equal(first.offset, 0); assert.equal(first.nextOffset, 3000); assert.equal(first.truncated, true)
|
|
152
|
+
const second = await dispatch({}, 'getGraph', { ...args, query: 'service node', offset: first.nextOffset })
|
|
153
|
+
assert.equal(second.nodes.length, 41); assert.equal(second.total, count); assert.equal(second.offset, 3000)
|
|
154
|
+
assert.equal(second.nextOffset, null); assert.equal(second.truncated, false)
|
|
155
|
+
assert.deepEqual([...first.nodes, ...second.nodes].map(node => node.id), graph.nodes.map(node => node.id))
|
|
156
|
+
const outside = await dispatch({}, 'getGraph', { ...args, query: 'service node', offset: count + 10 })
|
|
157
|
+
assert.deepEqual(outside.nodes, []); assert.equal(outside.total, count); assert.equal(outside.nextOffset, null)
|
|
158
|
+
const small = await dispatch({}, 'getGraph', { ...args, query: 'service node', offset: '2', limit: '3' })
|
|
159
|
+
assert.deepEqual(small.nodes.map(node => node.id), graph.nodes.slice(2, 5).map(node => node.id)); assert.equal(small.nextOffset, 5)
|
|
160
|
+
for (const result of [found, first, second, outside, small]) assert.doesNotMatch(JSON.stringify(result), /body-only-service-marker|"content"|"body"/)
|
|
161
|
+
assert.equal((await dispatch({}, 'getGraph', { ...args, query: 'body-only-service-marker' })).total, 0)
|
|
162
|
+
assert.deepEqual(await dispatch({}, 'getGraph', { ...args, query: ' \u3000 ' }), overview)
|
|
163
|
+
for (const query of [{}, true, 'x'.repeat(257)]) await assert.rejects(() => dispatch({}, 'getGraph', { ...args, query }), /graph query/)
|
|
164
|
+
assert.deepEqual(fs.readFileSync(sourceFile), sourceBefore, 'all search pages must leave the canonical index byte-for-byte intact')
|
|
165
|
+
})
|
|
166
|
+
|
|
167
|
+
test('private RPC search uses the current session/context/domain model after management and other permission views are warm', async () => {
|
|
168
|
+
const sessionId = 'graph-search-permissions'
|
|
169
|
+
const libraryId = state.ensureSession(sessionId, { cwd: path.join(home, 'search-permissions'), agentPreset: 'rp' }).libraryId
|
|
170
|
+
state.setSessionBinding(sessionId, { memoryContext: { characterId: 'alice' } })
|
|
171
|
+
const visible = createEntity({ libraryId, name: 'Needle visible entity' }), hidden = createEntity({ libraryId, name: 'Needle hidden entity' })
|
|
172
|
+
state.savePrivateIndex(libraryId, { entries: {
|
|
173
|
+
shared: { title: 'Needle shared', status: 'active', visibility: 'shared', domain: 'rp', links: [
|
|
174
|
+
{ to: visible.id, kind: 'person', note: 'visible-proof-marker' },
|
|
175
|
+
{ to: 'secret', kind: 'relation', note: 'hidden-edge-note-marker' },
|
|
176
|
+
{ to: 'bob', kind: 'relation', evidence: { surface: 'hidden-edge-evidence-marker' } },
|
|
177
|
+
{ to: 'work', kind: 'relation', note: 'cross-domain-edge-marker' },
|
|
178
|
+
] },
|
|
179
|
+
alice: { title: 'Needle alice', status: 'active', visibility: 'shared', domain: 'rp', context: { knownBy: ['alice'] } },
|
|
180
|
+
bob: { title: 'Needle bob', status: 'active', visibility: 'shared', domain: 'rp', context: { characterId: 'bob' } },
|
|
181
|
+
secret: { title: 'Needle secret', status: 'active', visibility: 'secret', domain: 'rp', links: [{ to: hidden.id, kind: 'person' }] },
|
|
182
|
+
work: { title: 'Needle work', status: 'active', visibility: 'shared', domain: 'coding' },
|
|
183
|
+
} })
|
|
184
|
+
const management = getGraphOverview({ libraryId, query: 'needle', includeOrphans: true })
|
|
185
|
+
assert.ok(management.nodes.some(node => node.id === 'secret')); assert.ok(management.nodes.some(node => node.id === hidden.id))
|
|
186
|
+
const args = { libraryId, sessionId, overview: true, domain: ' RP ', includeOrphans: true }
|
|
187
|
+
const search = query => dispatch({}, 'getGraph', { ...args, query })
|
|
188
|
+
const current = await search('needle')
|
|
189
|
+
assert.deepEqual(new Set(current.nodes.map(node => node.id)), new Set(['shared', 'alice', visible.id]))
|
|
190
|
+
assert.equal(current.total, 3)
|
|
191
|
+
assert.doesNotMatch(JSON.stringify(current), /Needle secret|Needle bob|Needle hidden entity|hidden-edge-|cross-domain-edge-marker/)
|
|
192
|
+
for (const query of ['secret', 'bob', hidden.id, 'hidden-edge-note-marker', 'hidden-edge-evidence-marker', 'cross-domain-edge-marker']) {
|
|
193
|
+
assert.equal((await search(query)).total, 0, query)
|
|
194
|
+
}
|
|
195
|
+
const coding = await dispatch({}, 'getGraph', { ...args, domain: 'coding', query: 'needle' })
|
|
196
|
+
assert.deepEqual(coding.nodes.map(node => node.id), ['work'])
|
|
197
|
+
assert.deepEqual((await search('needle')).nodes.map(node => node.id), current.nodes.map(node => node.id), 'a warm other-domain view must not contaminate this search')
|
|
198
|
+
state.setSessionBinding(sessionId, { memoryContext: { characterId: 'bob' } })
|
|
199
|
+
assert.equal((await search('alice')).total, 0)
|
|
200
|
+
assert.deepEqual((await search('needle bob')).nodes.map(node => node.id), ['bob'])
|
|
201
|
+
assert.equal((await search('secret')).total, 0)
|
|
202
|
+
const otherLibrary = state.ensureSession('graph-search-other-library', { cwd: path.join(home, 'search-other-library'), agentPreset: 'rp' }).libraryId
|
|
203
|
+
state.savePrivateIndex(otherLibrary, { entries: { shared: { title: 'other-library-only-marker', status: 'active', visibility: 'shared' } } })
|
|
204
|
+
getGraphOverview({ libraryId: otherLibrary, query: 'other-library-only-marker' })
|
|
205
|
+
assert.equal((await search('other-library-only-marker')).total, 0)
|
|
206
|
+
await assert.rejects(() => dispatch({}, 'getGraph', { ...args, libraryId: otherLibrary, query: 'needle' }), /not bound/)
|
|
207
|
+
state.setSessionBinding(sessionId, { useLibrary: false })
|
|
208
|
+
await assert.rejects(() => search('needle'), /turned off/)
|
|
209
|
+
})
|
|
210
|
+
|
|
211
|
+
test('actual private service searches entity names and recorded note/evidence and invalidates changed metadata without writing sources', async () => {
|
|
212
|
+
const libraryId = state.ensureSession('graph-search-metadata', { cwd: path.join(home, 'search-metadata'), agentPreset: 'rp' }).libraryId
|
|
213
|
+
const entity = createEntity({ libraryId, name: 'Search Entity' })
|
|
214
|
+
const entries = {
|
|
215
|
+
a: { title: 'first', content: 'not-in-graph-body-marker', status: 'active', visibility: 'shared', links: [
|
|
216
|
+
{ to: 'b', kind: 'relation', note: 'recorded-note-marker', evidence: { rule: 'lexical-overlap', field: 'title' } },
|
|
217
|
+
{ to: entity.id, kind: 'person', source: 'auto', evidence: { rule: 'entity-mention', entityId: entity.id, field: 'content', surface: 'Evidence-Marker' } },
|
|
218
|
+
] },
|
|
219
|
+
b: { title: 'second', status: 'active', visibility: 'shared' },
|
|
220
|
+
}
|
|
221
|
+
state.savePrivateIndex(libraryId, { entries })
|
|
222
|
+
const files = [path.join(indexDir(), `${libraryId}.json`), path.join(indexDir(), `${libraryId}.entities.json`)]
|
|
223
|
+
const before = files.map(file => fs.readFileSync(file))
|
|
224
|
+
const args = { libraryId, overview: true }
|
|
225
|
+
const name = await dispatch({}, 'getGraph', { ...args, query: ' search entity ' })
|
|
226
|
+
assert.deepEqual(name.nodes.map(node => node.id), [entity.id])
|
|
227
|
+
const note = await dispatch({}, 'getGraph', { ...args, query: 'RECORDED-NOTE-MARKER' })
|
|
228
|
+
assert.deepEqual(note.nodes.map(node => node.id), ['a', 'b']); assert.equal(note.edges[0].note, 'recorded-note-marker')
|
|
229
|
+
const evidence = await dispatch({}, 'getGraph', { ...args, query: 'evidence-marker' })
|
|
230
|
+
assert.deepEqual(new Set(evidence.nodes.map(node => node.id)), new Set(['a', entity.id]))
|
|
231
|
+
assert.deepEqual(evidence.edges[0].evidence, entries.a.links[1].evidence)
|
|
232
|
+
assert.equal((await dispatch({}, 'getGraph', { ...args, query: 'not-in-graph-body-marker' })).total, 0)
|
|
233
|
+
for (let i = 0; i < files.length; i++) assert.deepEqual(fs.readFileSync(files[i]), before[i])
|
|
234
|
+
entries.a.links[0].note = 'updated-note-marker'
|
|
235
|
+
state.savePrivateIndex(libraryId, { entries })
|
|
236
|
+
assert.equal((await dispatch({}, 'getGraph', { ...args, query: 'recorded-note-marker' })).total, 0)
|
|
237
|
+
const updated = await dispatch({}, 'getGraph', { ...args, query: 'updated-note-marker' })
|
|
238
|
+
assert.deepEqual(updated.nodes.map(node => node.id), ['a', 'b']); assert.notEqual(updated.revision, note.revision)
|
|
239
|
+
})
|
|
240
|
+
|
|
241
|
+
test('global getGraph RPC searches its full authorized domain model while hiding suppressed edges and all memory bodies', async () => {
|
|
242
|
+
state.saveGlobalIndex({ entries: {
|
|
243
|
+
g1: { title: 'Global Search', content: 'global-body-only-marker', status: 'active', domain: 'coding', links: [
|
|
244
|
+
{ to: 'g2', kind: 'supports', note: 'global-search-note-marker' },
|
|
245
|
+
{ to: 'hidden', kind: 'supports', note: 'suppressed-edge-marker' },
|
|
246
|
+
{ to: 'rp', kind: 'related', note: 'other-domain-edge-marker' },
|
|
247
|
+
] },
|
|
248
|
+
g2: { title: 'second global', status: 'active', domain: 'coding' },
|
|
249
|
+
hidden: { title: 'suppressed-only-marker', status: 'suppressed', domain: 'coding' },
|
|
250
|
+
rp: { title: 'rp-only-marker', status: 'active', domain: 'rp' },
|
|
251
|
+
} })
|
|
252
|
+
const args = { scope: 'global', overview: true, domain: 'CODING' }
|
|
253
|
+
const found = await dispatch({}, 'getGraph', { ...args, query: ' GLOBAL SEARCH ', bbox: [1e9, 1e9, 1e9 + 1, 1e9 + 1] })
|
|
254
|
+
assert.deepEqual(found.nodes.map(node => node.id), ['g1']); assert.equal(found.scope, 'global'); assert.equal(found.mode, 'detail')
|
|
255
|
+
const note = await dispatch({}, 'getGraph', { ...args, query: 'global-search-note-marker' })
|
|
256
|
+
assert.deepEqual(note.nodes.map(node => node.id), ['g1', 'g2']); assert.equal(note.edges.length, 1)
|
|
257
|
+
for (const query of ['suppressed-only-marker', 'suppressed-edge-marker', 'other-domain-edge-marker', 'rp-only-marker', 'global-body-only-marker']) {
|
|
258
|
+
assert.equal((await dispatch({}, 'getGraph', { ...args, query })).total, 0, query)
|
|
259
|
+
}
|
|
260
|
+
assert.doesNotMatch(JSON.stringify(note), /global-body-only-marker|"content"|"body"/)
|
|
261
|
+
})
|
|
262
|
+
|
|
263
|
+
test('bbox, zoom and cluster expansion select the intended deterministic world rows', () => {
|
|
264
|
+
const model = buildGraphOverview(fixture(2500)), cluster = model.clusters[0]
|
|
265
|
+
const detail = selectGraphOverview(model, { clusterId: cluster.id })
|
|
266
|
+
assert.equal(detail.mode, 'detail'); assert.equal(detail.nodes.length, cluster.count)
|
|
267
|
+
assert.ok(detail.nodes.every(node => node.clusterId === cluster.id))
|
|
268
|
+
const near = selectGraphOverview(model, { bbox: cluster.bbox, zoom: 1 })
|
|
269
|
+
assert.equal(near.nodes.length, cluster.count)
|
|
270
|
+
const far = selectGraphOverview(model, { bbox: cluster.bbox, zoom: .1 })
|
|
271
|
+
assert.deepEqual(far.nodes.map(node => node.id), [cluster.id])
|
|
272
|
+
assert.equal(selectGraphOverview(model, { bbox: [1e9, 1e9, 1e9 + 1, 1e9 + 1], zoom: 1 }).nodes.length, 0)
|
|
273
|
+
for (const bbox of [{}, [1, 0, -1, 2], [NaN, 0, 1, 2], [0, 0, Infinity, 2]]) assert.throws(() => selectGraphOverview(model, { bbox }), /bbox/)
|
|
274
|
+
assert.throws(() => selectGraphOverview(model, { clusterId: 'unavailable' }), /unknown or inaccessible/)
|
|
275
|
+
})
|
|
276
|
+
|
|
277
|
+
test('revision cache reuses derived adjacency but returns detached responses', () => {
|
|
278
|
+
let builds = 0
|
|
279
|
+
const options = { key: 'pure-test', revision: 'r1', build: () => { builds++; return fixture(4) } }
|
|
280
|
+
const first = cachedGraphOverview(options)
|
|
281
|
+
first.nodes[0].title = 'modified'; first.totals.nodes = -1
|
|
282
|
+
const next = cachedGraphOverview(options)
|
|
283
|
+
assert.equal(builds, 1); assert.equal(next.totals.nodes, 4); assert.notEqual(next.nodes[0].title, 'modified')
|
|
284
|
+
cachedGraphOverview({ ...options, revision: 'r2' })
|
|
285
|
+
assert.equal(builds, 2)
|
|
286
|
+
assert.equal(cachedGraphOverview({ ...options, revision: 'r2' }).positionScope, first.positionScope,
|
|
287
|
+
'coordinate identity must survive canonical revisions and viewport pages')
|
|
288
|
+
assert.notEqual(cachedGraphOverview({ ...options, key: 'other-permission-view' }).positionScope, first.positionScope,
|
|
289
|
+
'fixed coordinates must not cross a different library or visibility context')
|
|
290
|
+
assert.match(first.positionScope, /^graph-[a-f0-9]{24}$/)
|
|
291
|
+
})
|
|
292
|
+
|
|
293
|
+
test('classic cross-library pages reuse all ten models without disk adjacency work', () => {
|
|
294
|
+
const builds = new Map(), options = Array.from({ length: 10 }, (_, i) => ({
|
|
295
|
+
key: ['classic-ten-libraries', i], revision: 'r1',
|
|
296
|
+
build: () => { builds.set(i, (builds.get(i) || 0) + 1); return fixture(205) },
|
|
297
|
+
adjacency: () => { throw new Error('classic pages must use canonical CSR without opening disk adjacency') },
|
|
298
|
+
onAdjacencyError: error => { throw error },
|
|
299
|
+
}))
|
|
300
|
+
for (const offset of [0, 200, 0]) for (const option of options) {
|
|
301
|
+
const page = cachedGraphOverview(option, { layout: 'classic', limit: 200, offset })
|
|
302
|
+
assert.equal(page.nodes.length, offset ? 5 : 200)
|
|
303
|
+
if (!offset) assert.ok(page.edges.some(edge => edge.to === 'n-000200'), 'cross-page edges survive')
|
|
304
|
+
}
|
|
305
|
+
assert.equal(builds.size, 10)
|
|
306
|
+
assert.ok([...builds.values()].every(count => count === 1), 'small libraries cannot evict one another after the fourth library')
|
|
307
|
+
})
|
|
308
|
+
|
|
309
|
+
test('persistent adjacency is opened on cache hits, uses both directions for focus and canonical ordinals for evidence', () => {
|
|
310
|
+
const graph = fixture(5)
|
|
311
|
+
graph.edges.reverse(); graph.edges[0].note = 'canonical note'; graph.edges[0].evidence = { rule: 'fact-key' }
|
|
312
|
+
let builds = 0, opens = 0, incoming = 0, outgoing = 0, failed = 0, corrupt = false
|
|
313
|
+
const options = { key: 'persistent-seam', revision: 'seam-r1', build: () => { builds++; return graph },
|
|
314
|
+
onAdjacencyError: () => { failed++ }, adjacency: source => {
|
|
315
|
+
opens++
|
|
316
|
+
const current = source(), rows = current.edges.map((edge, ordinal) => ({ from: edge.from, to: edge.to, kind: edge.kind, ordinal }))
|
|
317
|
+
return { outgoing(id) { outgoing++; return rows.filter(row => row.from === id).map(row => corrupt ? { ...row, ordinal: 9999 } : row) },
|
|
318
|
+
incoming(id) { incoming++; return rows.filter(row => row.to === id) } }
|
|
319
|
+
} }
|
|
320
|
+
const result = cachedGraphOverview(options, { focusId: 'n-000004' })
|
|
321
|
+
assert.deepEqual(new Set(result.nodes.map(node => node.id)), new Set(['n-000003', 'n-000004']))
|
|
322
|
+
assert.equal(result.edges[0].note, 'canonical note'); assert.deepEqual(result.edges[0].evidence, { rule: 'fact-key' })
|
|
323
|
+
assert.ok(incoming > 0 && outgoing > 0)
|
|
324
|
+
const previousIncoming = incoming
|
|
325
|
+
cachedGraphOverview(options, { clusterId: result.nodes[0].clusterId })
|
|
326
|
+
assert.equal(builds, 1); assert.equal(opens, 2)
|
|
327
|
+
assert.equal(incoming, previousIncoming, 'induced detail edges only need outgoing sections')
|
|
328
|
+
corrupt = true
|
|
329
|
+
const fallback = cachedGraphOverview(options, { focusId: 'n-000003' })
|
|
330
|
+
assert.equal(failed, 1)
|
|
331
|
+
assert.deepEqual(new Set(fallback.nodes.map(node => node.id)), new Set(['n-000002', 'n-000003', 'n-000004']))
|
|
332
|
+
assert.equal(fallback.edges.length, 2, 'a corrupt ordinal falls back to complete canonical edges in this request')
|
|
333
|
+
assert.equal(builds, 1)
|
|
334
|
+
})
|
|
335
|
+
|
|
336
|
+
test('a just-rebuilt source facade uses canonical CSR without repeatedly scanning fallback edges', () => {
|
|
337
|
+
let opens = 0
|
|
338
|
+
const result = cachedGraphOverview({ key: 'source-fallback-csr', revision: 'r1', build: () => fixture(3200),
|
|
339
|
+
adjacency: source => {
|
|
340
|
+
opens++; assert.equal(source().nodes.length, 3200)
|
|
341
|
+
return { backend: 'source', outgoing() { assert.fail('source facade must not rescan the entire edge table') } }
|
|
342
|
+
} }, { focusId: 'n-000002' })
|
|
343
|
+
assert.equal(opens, 1)
|
|
344
|
+
assert.deepEqual(result.nodes.map(node => node.id).sort(), ['n-000001', 'n-000002', 'n-000003'])
|
|
345
|
+
assert.equal(result.edges.length, 2)
|
|
346
|
+
})
|
|
347
|
+
|
|
348
|
+
test('persistent detail batches once and focus batches once per BFS frontier, with canonical-order fallback', () => {
|
|
349
|
+
const graph = fixture(3200), calls = []
|
|
350
|
+
let corrupt = false, errors = 0
|
|
351
|
+
const options = { key: 'batch-persistent', revision: 'r1', build: () => graph,
|
|
352
|
+
onAdjacencyError: () => { errors++ }, adjacency: () => ({
|
|
353
|
+
backend: 'persistent', outgoing() { assert.fail('detail and focus must use batch selection') },
|
|
354
|
+
incoming() { assert.fail('focus must batch its incoming sections') },
|
|
355
|
+
selectMany(query) {
|
|
356
|
+
calls.push(query)
|
|
357
|
+
const out = new Set(query.outgoing || []), incoming = new Set(query.incoming || [])
|
|
358
|
+
return graph.edges.map((edge, ordinal) => ({ ...edge, ordinal: corrupt ? -1 : ordinal }))
|
|
359
|
+
.filter(edge => out.has(edge.from) || incoming.has(edge.to))
|
|
360
|
+
},
|
|
361
|
+
}) }
|
|
362
|
+
const detail = cachedGraphOverview(options, { bbox: [-1e9, -1e9, 1e9, 1e9], zoom: 1 })
|
|
363
|
+
assert.equal(detail.nodes.length, 3000)
|
|
364
|
+
assert.equal(calls.length, 1); assert.equal(calls[0].outgoing.length, 3000)
|
|
365
|
+
assert.equal(calls[0].incoming, undefined)
|
|
366
|
+
calls.length = 0
|
|
367
|
+
const args = { focusId: 'n-000002', depth: 2 }
|
|
368
|
+
const focus = cachedGraphOverview(options, args)
|
|
369
|
+
assert.equal(calls.length, 3, 'two BFS frontier reads and one induced edge batch')
|
|
370
|
+
assert.deepEqual(calls[0], { outgoing: ['n-000002'], incoming: ['n-000002'] })
|
|
371
|
+
assert.equal(calls[1].outgoing.length, 2)
|
|
372
|
+
corrupt = true
|
|
373
|
+
const fallback = cachedGraphOverview(options, args)
|
|
374
|
+
assert.equal(errors, 1)
|
|
375
|
+
assert.deepEqual(fallback, focus, 'a broken batch must return the identical canonical page in this request')
|
|
376
|
+
})
|
|
377
|
+
|
|
378
|
+
function privateAdjacency(libraryId, { sessionId = null, memoryContext = null, domain = '', includeOrphans = false } = {}) {
|
|
379
|
+
return graphAdjacencyLocation({ scope: 'private', libraryId,
|
|
380
|
+
key: [indexDir(), libraryId, sessionId, memoryContext, domain, includeOrphans] })
|
|
381
|
+
}
|
|
382
|
+
function publication(location) {
|
|
383
|
+
const pointer = JSON.parse(fs.readFileSync(location.pointer, 'utf8')).payload
|
|
384
|
+
return { generation: pointer.generation, file: path.join(location.root, pointer.slot, 'edges.idx') }
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
test('private graph service publishes and really reads persistent in/out sections on a model-cache hit', t => {
|
|
388
|
+
const libraryId = state.ensureSession('disk-graph-reader', { cwd: path.join(home, 'disk-graph'), agentPreset: 'rp' }).libraryId
|
|
389
|
+
state.savePrivateIndex(libraryId, { entries: {
|
|
390
|
+
a: { title: 'canonical title never persisted', status: 'active', visibility: 'shared',
|
|
391
|
+
links: [{ to: 'b', kind: 'relation', note: 'canonical note never persisted', evidence: { rule: 'private-evidence' } }] },
|
|
392
|
+
b: { title: 'second node', status: 'active', visibility: 'shared', links: [{ to: 'c', kind: 'relation' }] },
|
|
393
|
+
c: { title: 'third node', status: 'active', visibility: 'shared' },
|
|
394
|
+
} })
|
|
395
|
+
const first = getGraphOverview({ libraryId }), location = privateAdjacency(libraryId), disk = publication(location)
|
|
396
|
+
assert.ok(fs.statSync(disk.file).size > 40)
|
|
397
|
+
const before = graphAdjacencyDiagnostics(), readSync = fs.readSync, openSync = fs.openSync, fds = new Set(), rows = []
|
|
398
|
+
t.mock.method(fs, 'openSync', function (file, ...args) {
|
|
399
|
+
const fd = openSync.call(this, file, ...args)
|
|
400
|
+
if (file === disk.file) fds.add(fd)
|
|
401
|
+
else fds.delete(fd)
|
|
402
|
+
return fd
|
|
403
|
+
})
|
|
404
|
+
t.mock.method(fs, 'readSync', function (fd, buffer, offset, length, position) {
|
|
405
|
+
if (fds.has(fd)) rows.push({ length, position })
|
|
406
|
+
return readSync.call(this, fd, buffer, offset, length, position)
|
|
407
|
+
})
|
|
408
|
+
const detail = getGraphOverview({ libraryId, focusId: 'b' })
|
|
409
|
+
assert.equal(detail.revision, first.revision)
|
|
410
|
+
assert.equal(publication(location).generation, disk.generation, 'a valid publication must be reused')
|
|
411
|
+
assert.deepEqual(new Set(detail.nodes.map(node => node.id)), new Set(['a', 'b', 'c']))
|
|
412
|
+
assert.deepEqual(new Set(detail.edges.map(edge => `${edge.from}>${edge.to}`)), new Set(['a>b', 'b>c']))
|
|
413
|
+
assert.equal(detail.edges.find(edge => edge.from === 'a').note, 'canonical note never persisted')
|
|
414
|
+
assert.deepEqual(detail.edges.find(edge => edge.from === 'a').evidence, { rule: 'private-evidence' })
|
|
415
|
+
assert.ok(rows.filter(row => row.length === 4).length >= 4, 'read row-order indices for incoming and outgoing sections')
|
|
416
|
+
const recordOffset = 32 + (3 + 1) * 8 + 2 * 8
|
|
417
|
+
assert.ok(rows.filter(row => row.position >= recordOffset && row.length % 16 === 0)
|
|
418
|
+
.reduce((sum, row) => sum + row.length, 0) >= 64, 'read actual edge records, including coalesced disk spans')
|
|
419
|
+
assert.equal(graphAdjacencyDiagnostics().fallbacks, before.fallbacks)
|
|
420
|
+
assert.ok(graphAdjacencyDiagnostics().hits > before.hits)
|
|
421
|
+
const serialized = fs.readFileSync(path.join(path.dirname(disk.file), 'nodes.json'), 'utf8')
|
|
422
|
+
+ fs.readFileSync(path.join(path.dirname(disk.file), 'manifest.json'), 'utf8')
|
|
423
|
+
assert.doesNotMatch(serialized, /canonical title|canonical note|private-evidence|memoryContext/)
|
|
424
|
+
})
|
|
425
|
+
|
|
426
|
+
test('warm graph service detects same-size restored-mtime edges corruption and deletion in the current request', () => {
|
|
427
|
+
const libraryId = state.ensureSession('disk-corrupt-reader', { cwd: path.join(home, 'disk-corrupt'), agentPreset: 'rp' }).libraryId
|
|
428
|
+
state.savePrivateIndex(libraryId, { entries: {
|
|
429
|
+
a: { title: 'a', status: 'active', visibility: 'shared', links: [{ to: 'b', kind: 'relation' }] },
|
|
430
|
+
b: { title: 'b', status: 'active', visibility: 'shared' },
|
|
431
|
+
} })
|
|
432
|
+
getGraphOverview({ libraryId }); getGraphOverview({ libraryId, focusId: 'b' })
|
|
433
|
+
const location = privateAdjacency(libraryId), first = publication(location), stat = fs.statSync(first.file)
|
|
434
|
+
const bytes = fs.readFileSync(first.file), warnings = []
|
|
435
|
+
bytes[bytes.length - 1] ^= 1
|
|
436
|
+
fs.writeFileSync(first.file, bytes); fs.utimesSync(first.file, stat.atime, stat.mtime)
|
|
437
|
+
setWarnSink(message => warnings.push(message))
|
|
438
|
+
try {
|
|
439
|
+
const recovered = getGraphOverview({ libraryId, focusId: 'b' })
|
|
440
|
+
assert.deepEqual(recovered.edges.map(edge => [edge.from, edge.to]), [['a', 'b']])
|
|
441
|
+
assert.ok(warnings.some(message => message.includes('GRAPH_ADJACENCY_CORRUPT')))
|
|
442
|
+
const second = publication(location)
|
|
443
|
+
assert.notEqual(second.generation, first.generation)
|
|
444
|
+
getGraphOverview({ libraryId, focusId: 'b' })
|
|
445
|
+
fs.unlinkSync(second.file); warnings.length = 0
|
|
446
|
+
const deleted = getGraphOverview({ libraryId, focusId: 'b' })
|
|
447
|
+
assert.deepEqual(deleted.edges.map(edge => [edge.from, edge.to]), [['a', 'b']])
|
|
448
|
+
assert.ok(warnings.some(message => message.includes('GRAPH_ADJACENCY_MISSING')))
|
|
449
|
+
assert.notEqual(publication(location).generation, second.generation)
|
|
450
|
+
} finally { setWarnSink(null) }
|
|
451
|
+
})
|
|
452
|
+
|
|
453
|
+
test('damage during an actual persistent batch discards that batch and returns current canonical edges', t => {
|
|
454
|
+
const libraryId = state.ensureSession('disk-midread-reader', { cwd: path.join(home, 'disk-midread'), agentPreset: 'rp' }).libraryId
|
|
455
|
+
state.savePrivateIndex(libraryId, { entries: {
|
|
456
|
+
a: { title: 'a', status: 'active', visibility: 'shared', links: [{ to: 'b', kind: 'relation', note: 'current canonical evidence' }] },
|
|
457
|
+
b: { title: 'b', status: 'active', visibility: 'shared' },
|
|
458
|
+
} })
|
|
459
|
+
const args = { libraryId, focusId: 'b' }
|
|
460
|
+
getGraphOverview(args)
|
|
461
|
+
const expected = getGraphOverview(args), location = privateAdjacency(libraryId), disk = publication(location)
|
|
462
|
+
const bytes = fs.readFileSync(disk.file), readSync = fs.readSync, openSync = fs.openSync, fds = new Set(), warnings = []
|
|
463
|
+
let damaged = false
|
|
464
|
+
t.mock.method(fs, 'openSync', function (file, ...rest) {
|
|
465
|
+
const fd = openSync.call(this, file, ...rest)
|
|
466
|
+
if (file === disk.file) fds.add(fd)
|
|
467
|
+
else fds.delete(fd)
|
|
468
|
+
return fd
|
|
469
|
+
})
|
|
470
|
+
t.mock.method(fs, 'readSync', function (fd, buffer, offset, length, position) {
|
|
471
|
+
const count = readSync.call(this, fd, buffer, offset, length, position)
|
|
472
|
+
if (!damaged && fds.has(fd) && length === 16) {
|
|
473
|
+
damaged = true; bytes[0] ^= 1; fs.writeFileSync(disk.file, bytes)
|
|
474
|
+
}
|
|
475
|
+
return count
|
|
476
|
+
})
|
|
477
|
+
setWarnSink(message => warnings.push(message))
|
|
478
|
+
try {
|
|
479
|
+
assert.deepEqual(getGraphOverview(args), expected)
|
|
480
|
+
assert.equal(damaged, true, 'the corruption must happen after a real edge record read')
|
|
481
|
+
assert.ok(warnings.some(message => message.includes('GRAPH_ADJACENCY_CORRUPT')))
|
|
482
|
+
assert.notEqual(publication(location).generation, disk.generation)
|
|
483
|
+
} finally { setWarnSink(null) }
|
|
484
|
+
})
|
|
485
|
+
|
|
486
|
+
test('global graph service also reads persistent sections and safely recovers its deleted index', t => {
|
|
487
|
+
state.saveGlobalIndex({ entries: {
|
|
488
|
+
ga: { title: 'global persistent a', status: 'active', links: [{ to: 'gb', kind: 'supports' }] },
|
|
489
|
+
gb: { title: 'global persistent b', status: 'active' },
|
|
490
|
+
} })
|
|
491
|
+
getGlobalGraphOverview()
|
|
492
|
+
const location = graphAdjacencyLocation({ scope: 'global', key: [dataDir(), 'global', ''] }), disk = publication(location)
|
|
493
|
+
const readSync = fs.readSync, openSync = fs.openSync, fds = new Set(), rows = []
|
|
494
|
+
t.mock.method(fs, 'openSync', function (file, ...args) {
|
|
495
|
+
const fd = openSync.call(this, file, ...args)
|
|
496
|
+
if (file === disk.file) fds.add(fd)
|
|
497
|
+
else fds.delete(fd)
|
|
498
|
+
return fd
|
|
499
|
+
})
|
|
500
|
+
t.mock.method(fs, 'readSync', function (fd, buffer, offset, length, position) {
|
|
501
|
+
if (fds.has(fd)) rows.push(length)
|
|
502
|
+
return readSync.call(this, fd, buffer, offset, length, position)
|
|
503
|
+
})
|
|
504
|
+
const result = getGlobalGraphOverview({ focusId: 'gb' })
|
|
505
|
+
assert.deepEqual(result.edges.map(edge => [edge.from, edge.to]), [['ga', 'gb']])
|
|
506
|
+
assert.ok(rows.includes(4) && rows.includes(16), 'the global service must use the persisted edge sections')
|
|
507
|
+
fs.unlinkSync(disk.file)
|
|
508
|
+
assert.deepEqual(getGlobalGraphOverview({ focusId: 'gb' }).edges, result.edges)
|
|
509
|
+
assert.notEqual(publication(location).generation, disk.generation)
|
|
510
|
+
})
|
|
511
|
+
|
|
512
|
+
test('session permission filtering precedes clustering, domains, counts and edge generation', () => {
|
|
513
|
+
const binding = state.ensureSession('p4-session', { cwd: path.join(home, 'private'), agentPreset: 'rp' })
|
|
514
|
+
const libraryId = binding.libraryId, exec = { agent: { session: { header: { id: 'p4-session' } } } }
|
|
515
|
+
const visible = createEntity({ libraryId, name: 'Visible entity' }), hidden = createEntity({ libraryId, name: 'Hidden entity' })
|
|
516
|
+
state.savePrivateIndex(libraryId, { entries: {
|
|
517
|
+
a: { title: 'visible', status: 'active', visibility: 'shared', domain: 'rp', links: [
|
|
518
|
+
{ to: visible.id, kind: 'person' }, { to: 'secret', kind: 'relation' }, { to: 'context', kind: 'relation' }, { to: 'deleted', kind: 'relation' }] },
|
|
519
|
+
secret: { title: 'secret-title', status: 'active', visibility: 'secret', domain: 'secret-domain', links: [{ to: hidden.id, kind: 'person' }] },
|
|
520
|
+
context: { title: 'context-title', status: 'active', visibility: 'shared', domain: 'hidden-domain', context: { characterId: 'other' } },
|
|
521
|
+
} })
|
|
522
|
+
const overview = getGraphOverview({ libraryId, exec, includeOrphans: true })
|
|
523
|
+
const sessionDisk = privateAdjacency(libraryId, { sessionId: 'p4-session', memoryContext: binding.memoryContext || null, includeOrphans: true })
|
|
524
|
+
const sessionGeneration = publication(sessionDisk).generation
|
|
525
|
+
assert.equal(overview.totals.nodes, 3); assert.equal(overview.totals.edges, 2)
|
|
526
|
+
assert.deepEqual(overview.domains, ['rp'])
|
|
527
|
+
const detail = getGraphOverview({ libraryId, exec, clusterId: overview.nodes[0].id })
|
|
528
|
+
assert.deepEqual(new Set(detail.nodes.map(node => node.id)), new Set(['a', visible.id, 'deleted']))
|
|
529
|
+
assert.equal(detail.nodes.find(node => node.id === 'deleted').missing, true)
|
|
530
|
+
assert.doesNotMatch(JSON.stringify(overview), /secret-title|context-title|Hidden entity|hidden-domain|secret-domain/)
|
|
531
|
+
// Warm a management snapshot first: it must never be reused for a session.
|
|
532
|
+
const management = getGraphOverview({ libraryId, includeOrphans: true })
|
|
533
|
+
const managementDisk = privateAdjacency(libraryId, { includeOrphans: true })
|
|
534
|
+
assert.notEqual(managementDisk.root, sessionDisk.root)
|
|
535
|
+
assert.ok(fs.existsSync(managementDisk.pointer), 'management has an independent physical publication')
|
|
536
|
+
assert.ok(management.totals.nodes > overview.totals.nodes)
|
|
537
|
+
assert.deepEqual(getGraphOverview({ libraryId, exec }).totals, overview.totals)
|
|
538
|
+
const classicPages = [0, 1, 2].map(offset => getGraphOverview({ libraryId, exec, layout: 'classic', limit: 1, offset }))
|
|
539
|
+
assert.deepEqual(new Set(classicPages.flatMap(page => page.nodes.map(node => node.id))), new Set(['a', visible.id, 'deleted']))
|
|
540
|
+
assert.equal(classicPages.reduce((sum, page) => sum + page.edges.length, 0), 2)
|
|
541
|
+
assert.doesNotMatch(JSON.stringify(classicPages), /secret-title|context-title|Hidden entity|hidden-domain|secret-domain/)
|
|
542
|
+
assert.throws(() => getGraphOverview({ libraryId, exec, focusId: hidden.id }), /unknown or inaccessible/)
|
|
543
|
+
const beforeRevision = overview.revision
|
|
544
|
+
renameEntity({ libraryId, entityId: visible.id, name: 'Renamed entity' })
|
|
545
|
+
const renamed = getGraphOverview({ libraryId, exec, clusterId: overview.nodes[0].id })
|
|
546
|
+
assert.notEqual(renamed.revision, beforeRevision)
|
|
547
|
+
assert.equal(renamed.nodes.find(node => node.id === visible.id).title, 'Renamed entity')
|
|
548
|
+
getGraphOverview({ libraryId, exec, includeOrphans: true })
|
|
549
|
+
assert.notEqual(publication(sessionDisk).generation, sessionGeneration, 'entity edits invalidate persisted topology too')
|
|
550
|
+
state.setSessionBinding('p4-session', { memoryContext: { characterId: 'other' } })
|
|
551
|
+
assert.ok(getGraphOverview({ libraryId, exec }).totals.nodes > overview.totals.nodes, 'context changes invalidate the permission view')
|
|
552
|
+
assert.ok(fs.existsSync(privateAdjacency(libraryId, { sessionId: 'p4-session', memoryContext: { characterId: 'other' } }).pointer))
|
|
553
|
+
getGraphOverview({ libraryId, exec, domain: 'rp' })
|
|
554
|
+
assert.ok(fs.existsSync(privateAdjacency(libraryId, { sessionId: 'p4-session', memoryContext: { characterId: 'other' }, domain: 'rp' }).pointer))
|
|
555
|
+
const otherLibrary = state.ensureSession('other-library', { cwd: path.join(home, 'other'), agentPreset: 'rp' }).libraryId
|
|
556
|
+
assert.throws(() => getGraphOverview({ libraryId: otherLibrary, exec }), /not bound/)
|
|
557
|
+
})
|
|
558
|
+
|
|
559
|
+
test('memory lifecycle status survives overview projection for management feedback', () => {
|
|
560
|
+
const binding = state.ensureSession('p4-status-session', { cwd: path.join(home, 'status-private'), agentPreset: 'rp' })
|
|
561
|
+
const libraryId = binding.libraryId
|
|
562
|
+
state.savePrivateIndex(libraryId, { entries: {
|
|
563
|
+
active: { title: 'active row', status: 'active', visibility: 'shared', domain: 'rp' },
|
|
564
|
+
archived: { title: 'archived row', status: 'archived', visibility: 'shared', domain: 'rp' },
|
|
565
|
+
} })
|
|
566
|
+
const metadata = getGraphOverview({ libraryId })
|
|
567
|
+
const management = getGraphOverview({ libraryId, bbox: metadata.bbox, zoom: 1, limit: 3000 })
|
|
568
|
+
assert.equal(management.nodes.find(node => node.id === 'active').status, 'active')
|
|
569
|
+
assert.equal(management.nodes.find(node => node.id === 'archived').status, 'archived')
|
|
570
|
+
})
|
|
571
|
+
|
|
572
|
+
test('global overview hides existing suppressed targets and preserves deleted placeholders', async () => {
|
|
573
|
+
state.saveGlobalIndex({ entries: {
|
|
574
|
+
ga: { title: 'global-a', status: 'active', domain: 'coding', links: [
|
|
575
|
+
{ id: 'same', to: 'gb', kind: 'related' }, { to: 'hidden', kind: 'supports' }, { to: 'gone', kind: 'supports' }] },
|
|
576
|
+
gb: { title: 'global-b', status: 'active', domain: 'coding', links: [{ id: 'same', to: 'ga', kind: 'related' }] },
|
|
577
|
+
hidden: { title: 'suppressed-title', status: 'suppressed', domain: 'hidden-domain' },
|
|
578
|
+
} })
|
|
579
|
+
const overview = getGlobalGraphOverview()
|
|
580
|
+
assert.equal(overview.scope, 'global'); assert.equal(overview.totals.nodes, 3); assert.equal(overview.totals.edges, 2)
|
|
581
|
+
assert.deepEqual(overview.domains, ['coding']); assert.doesNotMatch(JSON.stringify(overview), /suppressed-title|hidden-domain/)
|
|
582
|
+
const rpc = await dispatch({}, 'getGraph', { scope: 'global', overview: true })
|
|
583
|
+
assert.deepEqual(rpc, overview)
|
|
584
|
+
const legacy = await dispatch({}, 'getGraph', { scope: 'global' })
|
|
585
|
+
assert.equal(legacy.overview, undefined); assert.equal(legacy.nodes.length, 3)
|
|
586
|
+
})
|
|
587
|
+
|
|
588
|
+
test('private RPC opt-in leaves the existing bounded graph unchanged', async () => {
|
|
589
|
+
const libraryId = state.ensureSession('legacy-private', { cwd: path.join(home, 'legacy'), agentPreset: 'rp' }).libraryId
|
|
590
|
+
state.savePrivateIndex(libraryId, { entries: Object.fromEntries(fixture(205, false).nodes.map(node => [node.id, { title: node.title, status: 'active', visibility: 'shared' }])) })
|
|
591
|
+
assert.equal(getGraph({ libraryId, limit: 5000 }).nodes.length, 200)
|
|
592
|
+
const rpc = await dispatch({}, 'getGraph', { libraryId, overview: true })
|
|
593
|
+
assert.equal(rpc.mode, 'clusters'); assert.equal(rpc.totals.nodes, 205)
|
|
594
|
+
assert.equal(rpc.nodes.length, 200); assert.equal(rpc.nextOffset, 200)
|
|
595
|
+
})
|
|
596
|
+
|
|
597
|
+
test('overview page parameters stay finite, bounded and serializable', () => {
|
|
598
|
+
const model = buildGraphOverview(fixture(205, false))
|
|
599
|
+
const page = selectGraphOverview(model, { limit: '2', offset: '3' })
|
|
600
|
+
assert.equal(page.limit, 2); assert.equal(page.offset, 3); assert.equal(page.nextOffset, 5)
|
|
601
|
+
assert.equal(selectGraphOverview(model, { limit: 0 }).limit, 1)
|
|
602
|
+
assert.equal(selectGraphOverview(model, { offset: -3 }).offset, 0)
|
|
603
|
+
assert.equal(selectGraphOverview(model, { offset: 999 }).nextOffset, null)
|
|
604
|
+
for (const key of ['limit', 'offset', 'depth', 'zoom']) for (const value of [Infinity, -Infinity, NaN, {}, [], true, 'bad', '']) {
|
|
605
|
+
assert.throws(() => selectGraphOverview(model, { [key]: value }), new RegExp(key))
|
|
606
|
+
}
|
|
607
|
+
assert.throws(() => selectGraphOverview(model, { offset: Number.MAX_SAFE_INTEGER + 1 }), /offset/)
|
|
608
|
+
let builds = 0
|
|
609
|
+
assert.throws(() => cachedGraphOverview({ key: 'bad-page', revision: 'r1', build: () => { builds++; return fixture(1) } }, { offset: Infinity }), /offset/)
|
|
610
|
+
assert.equal(builds, 0, 'invalid query parameters must be rejected before building a full graph')
|
|
611
|
+
assert.deepEqual(JSON.parse(JSON.stringify(page)).offset, 3)
|
|
612
|
+
})
|
|
613
|
+
|
|
614
|
+
test('RPC session identity cannot reuse management or another context cache', async () => {
|
|
615
|
+
const sessionId = 'rpc-graph-reader', binding = state.ensureSession(sessionId, { cwd: path.join(home, 'rpc-private'), agentPreset: 'rp' })
|
|
616
|
+
const libraryId = binding.libraryId
|
|
617
|
+
state.setSessionBinding(sessionId, { memoryContext: { characterId: 'alice' } })
|
|
618
|
+
state.savePrivateIndex(libraryId, { entries: {
|
|
619
|
+
shared: { title: 'public', status: 'active', visibility: 'shared', links: [{ to: 'secret', kind: 'relation' }] },
|
|
620
|
+
alice: { title: 'alice-only', status: 'active', visibility: 'shared', context: { knownBy: ['alice'] } },
|
|
621
|
+
bob: { title: 'bob-only', status: 'active', visibility: 'shared', context: { characterId: 'bob' } },
|
|
622
|
+
secret: { title: 'secret-only', status: 'active', visibility: 'secret', domain: 'secret-domain' },
|
|
623
|
+
} })
|
|
624
|
+
assert.equal(getGraphOverview({ libraryId }).totals.nodes, 4)
|
|
625
|
+
for (const overview of [false, true]) {
|
|
626
|
+
const result = await dispatch({}, 'getGraph', { libraryId, sessionId, overview })
|
|
627
|
+
assert.equal(result.totals?.nodes ?? result.nodes.length, 2)
|
|
628
|
+
assert.doesNotMatch(JSON.stringify(result), /bob-only|secret-only|secret-domain/)
|
|
629
|
+
}
|
|
630
|
+
state.setSessionBinding(sessionId, { memoryContext: { characterId: 'bob' } })
|
|
631
|
+
const changed = await dispatch({}, 'getGraph', { libraryId, sessionId, overview: true })
|
|
632
|
+
assert.doesNotMatch(JSON.stringify(changed), /alice-only|secret-only/)
|
|
633
|
+
await assert.rejects(() => dispatch({}, 'getGraph', { libraryId, sessionId: 'unknown-graph-reader', overview: true }), /not bound/)
|
|
634
|
+
state.setSessionBinding(sessionId, { useLibrary: false })
|
|
635
|
+
await assert.rejects(() => dispatch({}, 'getGraph', { libraryId, sessionId, overview: true }), /turned off/)
|
|
636
|
+
})
|
|
637
|
+
|
|
638
|
+
test('whole graph revisions detect equal-size external index and entity changes', () => {
|
|
639
|
+
const libraryId = state.ensureSession('graph-stat-reader', { cwd: path.join(home, 'graph-stat'), agentPreset: 'rp' }).libraryId
|
|
640
|
+
const entity = createEntity({ libraryId, name: 'Entity old' })
|
|
641
|
+
state.savePrivateIndex(libraryId, { entries: { a: { title: 'Private old', status: 'active', visibility: 'shared', links: [{ to: entity.id, kind: 'person' }] } } })
|
|
642
|
+
const first = getGraphOverview({ libraryId }), file = path.join(indexDir(), `${libraryId}.json`)
|
|
643
|
+
const stat = fs.statSync(file)
|
|
644
|
+
fs.writeFileSync(file, fs.readFileSync(file, 'utf8').replace('Private old', 'Private new'))
|
|
645
|
+
fs.utimesSync(file, stat.atime, stat.mtime)
|
|
646
|
+
const next = getGraphOverview({ libraryId, clusterId: first.nodes[0].id })
|
|
647
|
+
assert.notEqual(next.revision, first.revision)
|
|
648
|
+
assert.equal(next.nodes.find(node => node.id === 'a').title, 'Private new')
|
|
649
|
+
const entityFile = path.join(indexDir(), `${libraryId}.entities.json`), entityStat = fs.statSync(entityFile)
|
|
650
|
+
fs.writeFileSync(entityFile, fs.readFileSync(entityFile, 'utf8').replace('Entity old', 'Entity new'))
|
|
651
|
+
fs.utimesSync(entityFile, entityStat.atime, entityStat.mtime)
|
|
652
|
+
const renamed = getGraphOverview({ libraryId, clusterId: first.nodes[0].id })
|
|
653
|
+
assert.notEqual(renamed.revision, next.revision)
|
|
654
|
+
assert.equal(renamed.nodes.find(node => node.id === entity.id).title, 'Entity new')
|
|
655
|
+
state.saveGlobalIndex({ entries: { g: { title: 'Global old', status: 'active' } } })
|
|
656
|
+
const global = getGlobalGraphOverview(), globalFile = path.join(indexDir(), 'global.json'), globalStat = fs.statSync(globalFile)
|
|
657
|
+
fs.writeFileSync(globalFile, fs.readFileSync(globalFile, 'utf8').replace('Global old', 'Global new'))
|
|
658
|
+
fs.utimesSync(globalFile, globalStat.atime, globalStat.mtime)
|
|
659
|
+
const revised = getGlobalGraphOverview()
|
|
660
|
+
assert.notEqual(revised.revision, global.revision); assert.equal(revised.nodes[0].title, 'Global new')
|
|
661
|
+
})
|
|
662
|
+
|
|
663
|
+
test('recall relation cache also invalidates same-length restored-mtime edits and never caches unversioned snapshots', () => {
|
|
664
|
+
const libraryId = state.ensureSession('relation-stat-reader', { cwd: path.join(home, 'relation-stat'), agentPreset: 'rp' }).libraryId
|
|
665
|
+
state.savePrivateIndex(libraryId, { entries: {
|
|
666
|
+
a: { title: 'a', status: 'active', visibility: 'shared', links: [{ to: 'b', kind: 'relation', note: 'current scene' }] },
|
|
667
|
+
b: { title: 'b', status: 'active', visibility: 'shared' }, c: { title: 'c', status: 'active', visibility: 'shared' },
|
|
668
|
+
} })
|
|
669
|
+
assert.equal(recallRelations(libraryId, state.readPrivateIndex(libraryId), 'a', null)[0].id, 'b')
|
|
670
|
+
const file = path.join(indexDir(), `${libraryId}.json`), stat = fs.statSync(file)
|
|
671
|
+
const original = fs.readFileSync(file, 'utf8'), edited = original.replace(/"to"\s*:\s*"b"/, match => match.replace('"b"', '"c"'))
|
|
672
|
+
assert.notEqual(edited, original); assert.equal(edited.length, original.length)
|
|
673
|
+
fs.writeFileSync(file, edited); fs.utimesSync(file, stat.atime, stat.mtime)
|
|
674
|
+
assert.equal(recallRelations(libraryId, state.readPrivateIndex(libraryId), 'a', null)[0].id, 'c')
|
|
675
|
+
const detached = structuredClone(state.readPrivateIndex(libraryId))
|
|
676
|
+
detached.entries.a.links[0].to = 'b'
|
|
677
|
+
assert.equal(recallRelations(libraryId, detached, 'a', null)[0].id, 'b')
|
|
678
|
+
detached.entries.a.links[0].to = 'c'
|
|
679
|
+
assert.equal(recallRelations(libraryId, detached, 'a', null)[0].id, 'c')
|
|
680
|
+
})
|
|
681
|
+
|
|
682
|
+
test('a warm session graph invalidates when an expiry crosses without a write', t => {
|
|
683
|
+
let now = Date.now()
|
|
684
|
+
t.mock.method(Date, 'now', () => now)
|
|
685
|
+
const sessionId = 'graph-expiry-reader', binding = state.ensureSession(sessionId, { cwd: path.join(home, 'graph-expiry'), agentPreset: 'rp' })
|
|
686
|
+
const libraryId = binding.libraryId
|
|
687
|
+
const exec = { agent: { session: { header: { id: sessionId } } } }
|
|
688
|
+
state.savePrivateIndex(libraryId, { entries: { a: { title: 'expires-soon', status: 'active', visibility: 'shared', expiresAt: new Date(now + 1000).toISOString() } } })
|
|
689
|
+
const first = getGraphOverview({ libraryId, exec })
|
|
690
|
+
const location = privateAdjacency(libraryId, { sessionId, memoryContext: binding.memoryContext || null })
|
|
691
|
+
const generation = publication(location).generation
|
|
692
|
+
assert.equal(first.totals.nodes, 1)
|
|
693
|
+
now += 1001
|
|
694
|
+
const next = getGraphOverview({ libraryId, exec })
|
|
695
|
+
assert.notEqual(next.revision, first.revision); assert.equal(next.totals.nodes, 0)
|
|
696
|
+
assert.notEqual(publication(location).generation, generation, 'expiry invalidates the persistent publication without a write')
|
|
697
|
+
})
|
|
698
|
+
|
|
699
|
+
test('corrupt graph indexes report failure and never masquerade as an empty graph', () => {
|
|
700
|
+
const libraryId = state.ensureSession('graph-corrupt-reader', { cwd: path.join(home, 'graph-corrupt'), agentPreset: 'rp' }).libraryId
|
|
701
|
+
const files = [[path.join(indexDir(), `${libraryId}.json`), [() => getGraphOverview({ libraryId }), () => getGraph({ libraryId })]],
|
|
702
|
+
[path.join(indexDir(), 'global.json'), [() => getGlobalGraphOverview(), () => getGlobalGraph()]]]
|
|
703
|
+
for (const [file, readers] of files) {
|
|
704
|
+
fs.writeFileSync(file, '{invalid graph fixture')
|
|
705
|
+
for (const read of readers) { assert.throws(read, /cannot read graph index/); assert.throws(read, /cannot read graph index/) }
|
|
706
|
+
fs.writeFileSync(file, JSON.stringify({ entries: { recovered: { title: 'recovered', status: 'active', visibility: 'shared' } } }))
|
|
707
|
+
for (const read of readers) { const result = read(); assert.equal(result.totals?.nodes ?? result.nodes.length, 1) }
|
|
708
|
+
}
|
|
709
|
+
})
|