dsh-memu-memory 1.2.2 → 1.2.4
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 +4 -1
- package/README.md +12 -16
- package/README.zh.md +13 -17
- package/docs/client-conventions.md +2 -0
- package/docs/contributing.md +7 -6
- package/docs/emotion-model-spec.md +2 -2
- package/docs/env-and-commands.md +63 -1
- package/docs/host-api.md +19 -0
- package/docs/memory-semantics.md +10 -1
- package/docs/troubleshooting.md +77 -3
- package/lib/activity-log.js +1 -1
- package/lib/binding-diagnostics.js +1 -1
- package/lib/client.js +223 -19
- package/lib/cycle.js +9 -1
- package/lib/embedding-catalog.js +416 -138
- package/lib/embedding-download.js +36 -11
- package/lib/freshness.js +97 -0
- package/lib/graph.js +244 -2
- package/lib/inject.js +218 -11
- package/lib/local-embedding-manager.js +3 -2
- package/lib/local-embedding-protocol.js +40 -3
- package/lib/local-embedding-worker.js +36 -6
- package/lib/local-vector-service.js +18 -4
- package/lib/memory.js +83 -17
- package/lib/memu.js +15 -7
- package/lib/private.js +25 -5
- package/lib/rpc.js +17 -3
- package/lib/salient-wake.js +402 -0
- package/lib/state.js +173 -1
- package/package.json +3 -2
- package/scripts/drive-session.mjs +864 -0
- package/scripts/mock-llm.mjs +903 -0
- package/scripts/simulate-injection.mjs +298 -0
- package/tests/client-render.test.mjs +69 -2
- package/tests/consolidation-fork.test.mjs +14 -1
- package/tests/drive-session.test.mjs +152 -0
- package/tests/embedding-catalog.test.mjs +116 -8
- package/tests/embedding-download.test.mjs +97 -2
- package/tests/embedding-runtime.test.mjs +21 -13
- package/tests/entity-merge.test.mjs +261 -0
- package/tests/fixtures/mock-llm/reply.txt +1 -0
- package/tests/freshness.test.mjs +140 -0
- package/tests/help-page.test.mjs +4 -4
- package/tests/injection-chain.test.mjs +176 -0
- package/tests/injection-rollback.test.mjs +230 -0
- package/tests/local-embedding-env.test.mjs +51 -0
- package/tests/local-embedding-manager.test.mjs +18 -0
- package/tests/local-embedding-protocol.test.mjs +28 -2
- package/tests/local-embedding-worker.test.mjs +94 -0
- package/tests/mock-llm.test.mjs +225 -0
- package/tests/open-loop-date.test.mjs +406 -0
- package/tests/salient-wake.test.mjs +362 -0
- package/tests/settings-plumbing.test.mjs +76 -0
- package/tests/sidebar.test.mjs +5 -1
- package/tests/simulate-injection.test.mjs +76 -0
- package/tests/stubs/dsh-schedule.js +105 -0
- package/tests/vector-profile.test.mjs +28 -4
- package/tests/webui-auth.test.mjs +4 -0
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
PROVIDER_ORDER,
|
|
7
7
|
getEmbeddingCatalog,
|
|
8
8
|
getEmbeddingModel,
|
|
9
|
+
prefixesRequireRole,
|
|
9
10
|
profileFingerprint,
|
|
10
11
|
readOnnxGraphHeader,
|
|
11
12
|
resolveEmbeddingSources,
|
|
@@ -61,12 +62,79 @@ test('目录是冻结的只读快照,未知 id 返回 null 而不是默认模
|
|
|
61
62
|
assert.ok(Object.isFrozen(catalog))
|
|
62
63
|
assert.ok(Object.isFrozen(catalog[0]))
|
|
63
64
|
assert.throws(() => { catalog.push({ id: 'x' }) }, TypeError)
|
|
64
|
-
assert.deepEqual(catalog.map(entry => entry.id), [
|
|
65
|
+
assert.deepEqual(catalog.map(entry => entry.id), [
|
|
66
|
+
BGE, 'all-minilm-l6-v2-int8', 'multilingual-e5-small', 'qwen3-embedding-0.6b-int8', 'qwen3-embedding-0.6b-q4f16',
|
|
67
|
+
])
|
|
65
68
|
assert.equal(getEmbeddingModel('nope'), null)
|
|
66
69
|
assert.equal(getEmbeddingModel(undefined), null)
|
|
67
70
|
assert.equal(getEmbeddingModel('../../etc/passwd'), null)
|
|
68
71
|
})
|
|
69
72
|
|
|
73
|
+
test('三个模型的向量空间指纹钉死:改目录必须是有意识地来这里更新', () => {
|
|
74
|
+
assert.deepEqual(
|
|
75
|
+
Object.fromEntries(getEmbeddingCatalog().map(entry => [entry.id, entry.profileFingerprint])),
|
|
76
|
+
{
|
|
77
|
+
'bge-small-zh-v1.5-int8': '47511436d7468fa53b17dbd46d5dfd1454dbca9a70f34dc016c3af0a9decd748',
|
|
78
|
+
'all-minilm-l6-v2-int8': '2ede97e232225e6e6a14d9b0ef870fe8cc5ba5ac6273ae9a6da8263c314b4082',
|
|
79
|
+
'multilingual-e5-small': '4826773bf4a498168decd66edcef48e66befb982c3c60405a1416fb3fd0c5fcb',
|
|
80
|
+
'qwen3-embedding-0.6b-int8': '0934c5b89781cea545ba40f87b1bc0a9e9eb5a95cdde20390953f3642bd386bb',
|
|
81
|
+
'qwen3-embedding-0.6b-q4f16': 'a987005b6c033a3e2fdb7502836d73d6376bf8c15ea3c10be60ea0ca63a0a030',
|
|
82
|
+
},
|
|
83
|
+
)
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
// int8 与 int4 是两种量化、两个向量空间(quantization 进指纹),所以必须是两张卡片、
|
|
87
|
+
// 两份 DB,而不是一个条目挂两个图文件让用户以为可以混用。
|
|
88
|
+
test('Qwen3 的两张卡片是两个不同的向量空间', () => {
|
|
89
|
+
const int8 = getEmbeddingModel('qwen3-embedding-0.6b-int8')
|
|
90
|
+
const q4 = getEmbeddingModel('qwen3-embedding-0.6b-q4f16')
|
|
91
|
+
assert.notEqual(int8.profileFingerprint, q4.profileFingerprint)
|
|
92
|
+
assert.equal(int8.quantization.weights, 'qint8')
|
|
93
|
+
assert.equal(int8.quantization.scheme, 'dynamic-per-tensor')
|
|
94
|
+
assert.equal(q4.quantization.weights, 'uint4-nbits')
|
|
95
|
+
assert.notEqual(q4.quantization.scheme, int8.quantization.scheme)
|
|
96
|
+
for (const entry of [int8, q4]) {
|
|
97
|
+
assert.equal(entry.availability, 'available', entry.id)
|
|
98
|
+
assert.equal(entry.pooling, 'last', entry.id)
|
|
99
|
+
assert.equal(entry.dimension, 1024, entry.id)
|
|
100
|
+
assert.equal(entry.graph.opset, 21, entry.id)
|
|
101
|
+
// 因果 LM:只有 input_ids + attention_mask,没有 token_type_ids
|
|
102
|
+
assert.deepEqual(entry.graph.inputs.map(io => io.name), ['input_ids', 'attention_mask'], entry.id)
|
|
103
|
+
assert.deepEqual(entry.graph.outputs[0].shape, [1, 'sequence_length', 1024], entry.id)
|
|
104
|
+
// 32k 上下文的制品按 4096 分窗:刻意的延迟策略(不得超过制品上限,那条由下载器的
|
|
105
|
+
// verifyStagedArtifacts 把),窗口本身进指纹所以可复现。
|
|
106
|
+
assert.equal(entry.maxTokens, 4096, entry.id)
|
|
107
|
+
assert.ok(validateEmbeddingManifest(entry).errors.length === 0, entry.id)
|
|
108
|
+
}
|
|
109
|
+
assert.equal(int8.graph.outputs[0].dtype, 'float32')
|
|
110
|
+
assert.equal(q4.graph.outputs[0].dtype, 'float16')
|
|
111
|
+
assert.equal(int8.tier, 'preferred')
|
|
112
|
+
assert.equal(q4.tier, 'optional')
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
// Qwen3 的导出仓库在 ModelScope 上没有对应条目,所以它只能走 huggingface 系:
|
|
116
|
+
// 默认镜像站,源站作第二顺位;两者 pin 同一个 commit,落盘都按同一份 sha256 校。
|
|
117
|
+
test('Qwen3 卡片的默认来源是 hf-mirror,源站同 commit 可选', () => {
|
|
118
|
+
for (const id of ['qwen3-embedding-0.6b-int8', 'qwen3-embedding-0.6b-q4f16']) {
|
|
119
|
+
const resolved = resolveEmbeddingSources(id)
|
|
120
|
+
assert.deepEqual(resolved.sources.map(source => source.provider), ['hf-mirror', 'huggingface'], id)
|
|
121
|
+
assert.equal(resolved.primary.provider, 'hf-mirror', id + ' 默认必须是镜像站')
|
|
122
|
+
assert.deepEqual(resolved.skipped.map(item => item.provider), ['modelscope'], id)
|
|
123
|
+
assert.equal(resolved.skipped[0].reason, 'not-mapped', id)
|
|
124
|
+
const revision = getEmbeddingModel(id).sources['hf-mirror'].revision
|
|
125
|
+
assert.equal(getEmbeddingModel(id).sources.huggingface.revision, revision, id + ' 两个来源必须同 commit')
|
|
126
|
+
const mirrorGraph = resolved.primary.files.find(file => file.role === 'graph')
|
|
127
|
+
const originGraph = resolveEmbeddingSources(id, { providers: ['huggingface'] }).primary.files.find(file => file.role === 'graph')
|
|
128
|
+
assert.ok(mirrorGraph.url.startsWith('https://hf-mirror.com/n24q02m/'), mirrorGraph.url)
|
|
129
|
+
assert.ok(originGraph.url.startsWith('https://huggingface.co/n24q02m/'), originGraph.url)
|
|
130
|
+
assert.equal(mirrorGraph.sha256, originGraph.sha256, id)
|
|
131
|
+
// 见证制品来自基座仓库,且两个 provider 都合法(否则换源会被 origin 规则整条封掉)
|
|
132
|
+
const pooling = resolveEmbeddingSources(id, { providers: ['huggingface'] }).primary.files.find(file => file.path === '1_Pooling/config.json')
|
|
133
|
+
assert.ok(pooling.url.includes('/Qwen/Qwen3-Embedding-0.6B/'), pooling.url)
|
|
134
|
+
assert.equal(validateEmbeddingManifest(getEmbeddingModel(id)).warnings.filter(item => item.code === 'SOURCE_REVISION_UNPINNED').length, 0, id)
|
|
135
|
+
}
|
|
136
|
+
})
|
|
137
|
+
|
|
70
138
|
test('首个 BGE 条目带着已核实的固定 commit、逐文件字节数与 SHA-256', () => {
|
|
71
139
|
const entry = getEmbeddingModel(BGE)
|
|
72
140
|
assert.equal(entry.availability, 'available')
|
|
@@ -105,19 +173,59 @@ test('已核实的 BGE manifest 通过校验,但把未核实项如实标成警
|
|
|
105
173
|
assert.equal(codes.filter(code => code === 'SOURCE_REVISION_UNPINNED').length, 2)
|
|
106
174
|
})
|
|
107
175
|
|
|
108
|
-
test('
|
|
176
|
+
test('MiniLM 与 e5 已核实:制品齐全、清单合法、来源可下载', () => {
|
|
109
177
|
for (const id of ['all-minilm-l6-v2-int8', 'multilingual-e5-small']) {
|
|
110
178
|
const entry = getEmbeddingModel(id)
|
|
111
|
-
assert.equal(entry.availability, '
|
|
112
|
-
assert.deepEqual(entry.
|
|
113
|
-
assert.equal(entry.
|
|
114
|
-
assert.
|
|
179
|
+
assert.equal(entry.availability, 'available', id)
|
|
180
|
+
assert.deepEqual(entry.pendingReasons, [], id)
|
|
181
|
+
assert.equal(entry.verification.status, 'verified', id)
|
|
182
|
+
assert.match(entry.sources.modelscope.revision, /^[0-9a-f]{40}$/, id + ' revision')
|
|
183
|
+
assert.equal(entry.dimension, 384, id)
|
|
184
|
+
assert.equal(entry.maxTokens, 512, id)
|
|
185
|
+
assert.equal(entry.pooling, 'mean', id)
|
|
186
|
+
assert.ok(entry.files.length >= 8, id + ' files')
|
|
187
|
+
assert.ok(entry.totalBytes > 0, id)
|
|
188
|
+
assert.equal(entry.graph.outputs[0].shape[2], entry.dimension, id)
|
|
115
189
|
const report = validateEmbeddingManifest(entry)
|
|
116
|
-
assert.
|
|
117
|
-
|
|
190
|
+
assert.deepEqual(report.errors.map(item => item.code), [], id + ' manifest must be installable')
|
|
191
|
+
const resolved = resolveEmbeddingSources(entry)
|
|
192
|
+
assert.equal(resolved.primary.provider, 'modelscope', id)
|
|
193
|
+
assert.equal(resolved.primary.files.length, entry.files.length, id)
|
|
194
|
+
// 见证制品(官方仓库的 1_Pooling)必须来自另一个 repo+revision,而不是冒充导出仓库
|
|
195
|
+
const pooling = resolved.primary.files.find(file => file.path === '1_Pooling/config.json')
|
|
196
|
+
assert.ok(pooling, id + ' needs a pooling witness')
|
|
197
|
+
assert.notEqual(pooling.repo, entry.sources.modelscope.repo, id)
|
|
118
198
|
}
|
|
119
199
|
})
|
|
120
200
|
|
|
201
|
+
test('只有非对称前缀的模型需要角色通道,对称前缀的模型不需要', () => {
|
|
202
|
+
assert.equal(prefixesRequireRole(getEmbeddingModel('bge-small-zh-v1.5-int8')), false)
|
|
203
|
+
assert.equal(prefixesRequireRole(getEmbeddingModel('all-minilm-l6-v2-int8')), false)
|
|
204
|
+
assert.equal(prefixesRequireRole(getEmbeddingModel('multilingual-e5-small')), true)
|
|
205
|
+
// 这条要求必须对用户可见:校验器把它列成警告,而不是静默地把模型标成可用
|
|
206
|
+
const codes = validateEmbeddingManifest(getEmbeddingModel('multilingual-e5-small')).warnings.map(item => item.code)
|
|
207
|
+
assert.ok(codes.includes('PREFIX_ASYMMETRIC'))
|
|
208
|
+
assert.equal(validateEmbeddingManifest(getEmbeddingModel('all-minilm-l6-v2-int8')).warnings
|
|
209
|
+
.some(item => item.code === 'PREFIX_ASYMMETRIC'), false)
|
|
210
|
+
})
|
|
211
|
+
|
|
212
|
+
test('未核实的目录条目依然被挡住:没有文件、没有哈希、理由齐全', () => {
|
|
213
|
+
const pending = clone(getEmbeddingModel('all-minilm-l6-v2-int8'))
|
|
214
|
+
pending.availability = 'pending'
|
|
215
|
+
pending.pendingReasons = ['制品未核实']
|
|
216
|
+
pending.files = []
|
|
217
|
+
pending.totalBytes = 0
|
|
218
|
+
pending.verification = { ...pending.verification, status: 'unverified' }
|
|
219
|
+
delete pending.profileFingerprint
|
|
220
|
+
const withFingerprint = { ...pending, profileFingerprint: profileFingerprint(pending) }
|
|
221
|
+
const report = validateEmbeddingManifest(withFingerprint)
|
|
222
|
+
assert.equal(report.ok, true, 'pending 条目本身结构合法')
|
|
223
|
+
assert.equal(report.errors.length, 0)
|
|
224
|
+
// pending 的原因必须写出来,不能只说「不可用」
|
|
225
|
+
const noReason = { ...withFingerprint, pendingReasons: [] }
|
|
226
|
+
assert.ok(validateEmbeddingManifest(noReason).errors.some(item => item.code === 'PENDING_REASON_MISSING'))
|
|
227
|
+
})
|
|
228
|
+
|
|
121
229
|
test('manifest 校验会拒绝伪造/缺失的制品元数据', () => {
|
|
122
230
|
const broken = clone(getEmbeddingModel(BGE))
|
|
123
231
|
broken.files[0].sha256 = 'not-a-hash'
|
|
@@ -147,9 +147,19 @@ test('没有明确确认就绝不下模型,也不发任何请求', async () =>
|
|
|
147
147
|
|
|
148
148
|
test('pending 模型与未知 id 都被拒绝,且不会联网', async () => {
|
|
149
149
|
const { entry, payloads } = fixtureManifest()
|
|
150
|
+
const pending = {
|
|
151
|
+
...entry,
|
|
152
|
+
id: 'fixture-pending-model',
|
|
153
|
+
availability: 'pending',
|
|
154
|
+
files: [],
|
|
155
|
+
totalBytes: 0,
|
|
156
|
+
pendingReasons: ['制品未核实:没有可比对的 bytes/sha256'],
|
|
157
|
+
verification: { ...entry.verification, status: 'unverified' },
|
|
158
|
+
}
|
|
159
|
+
pending.profileFingerprint = profileFingerprint(pending)
|
|
150
160
|
const { fetchImpl, calls } = makeFetch(entry, payloads)
|
|
151
|
-
const { manager } = managerFor(
|
|
152
|
-
await assert.rejects(manager.download('
|
|
161
|
+
const { manager } = managerFor(pending, fetchImpl)
|
|
162
|
+
await assert.rejects(manager.download('fixture-pending-model', { confirmed: true }), error => {
|
|
153
163
|
assert.equal(error.code, 'EMBEDDING_MODEL_UNAVAILABLE')
|
|
154
164
|
assert.ok(error.pendingReasons.length > 0)
|
|
155
165
|
return true
|
|
@@ -159,6 +169,91 @@ test('pending 模型与未知 id 都被拒绝,且不会联网', async () => {
|
|
|
159
169
|
assert.equal(manager.getStatus('does-not-exist').state, 'unknown')
|
|
160
170
|
})
|
|
161
171
|
|
|
172
|
+
// hf-mirror 对小文件用 chunked 传输(不发 content-length)。把「头缺失」当成 0 字节会
|
|
173
|
+
// 让每一个小文件都被误判成大小不符——真实装 Qwen3 时踩到的就是这个。
|
|
174
|
+
test('来源不发 content-length 时按下落的实际字节判断,不当成 0', async () => {
|
|
175
|
+
const { entry, payloads } = fixtureManifest()
|
|
176
|
+
const { fetchImpl } = makeFetch(entry, payloads, { responseFor: filePath => (filePath === 'config.json' ? { contentLength: null } : {}) })
|
|
177
|
+
const { manager } = managerFor(entry, fetchImpl)
|
|
178
|
+
const status = await manager.download(entry.id, { confirmed: true })
|
|
179
|
+
assert.equal(status.state, 'installed')
|
|
180
|
+
assert.equal(status.installed, true)
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
// 多来源条目(Qwen3 卡片就是 hf-mirror + huggingface 同一个 commit):默认取目录顺序里
|
|
184
|
+
// 第一个,显式选源就只能用那一个,失败也不许回退到另一个源去下载。
|
|
185
|
+
test('显式选源只走那一个 provider,不回退到默认源', async () => {
|
|
186
|
+
const { entry, payloads } = fixtureManifest()
|
|
187
|
+
const mirrored = { ...entry, sources: {
|
|
188
|
+
'hf-mirror': { repo: 'fixture/tiny', revision: REVISION },
|
|
189
|
+
huggingface: { repo: 'fixture/tiny', revision: REVISION },
|
|
190
|
+
} }
|
|
191
|
+
const routes = new Map()
|
|
192
|
+
for (const provider of ['hf-mirror', 'huggingface']) {
|
|
193
|
+
for (const file of resolveEmbeddingSources(mirrored, { providers: [provider] }).primary.files) routes.set(file.url, { provider, path: file.path })
|
|
194
|
+
}
|
|
195
|
+
const calls = []
|
|
196
|
+
const fetchImpl = async (url, init) => {
|
|
197
|
+
calls.push(url)
|
|
198
|
+
const route = routes.get(url)
|
|
199
|
+
if (!route) return { ok: false, status: 404, headers: { get: () => null }, body: null }
|
|
200
|
+
return responseFor(payloads[route.path], {})
|
|
201
|
+
}
|
|
202
|
+
const { manager } = managerFor(mirrored, fetchImpl)
|
|
203
|
+
const byDefault = await manager.download(mirrored.id, { confirmed: true })
|
|
204
|
+
assert.equal(byDefault.provider, 'hf-mirror', '默认必须是镜像站')
|
|
205
|
+
assert.ok(calls[0].startsWith('https://hf-mirror.com/'), calls[0])
|
|
206
|
+
|
|
207
|
+
const { root } = managerFor(mirrored, fetchImpl)
|
|
208
|
+
const second = createEmbeddingDownloadManager({ root, catalog: [mirrored], fetchImpl })
|
|
209
|
+
const chosen = await second.download(mirrored.id, { confirmed: true, provider: 'huggingface' })
|
|
210
|
+
assert.equal(chosen.provider, 'huggingface')
|
|
211
|
+
assert.ok(calls.some(url => url.startsWith('https://huggingface.co/')), '选源后要真的打到源站')
|
|
212
|
+
// 不存在的 provider 与「存在但这个条目没固定」是两种错,都要如实分开
|
|
213
|
+
await assert.rejects(second.download(mirrored.id, { confirmed: true, provider: 'ollama' }), error => error.code === 'EMBEDDING_PROVIDER_UNKNOWN')
|
|
214
|
+
await assert.rejects(second.download(mirrored.id, { confirmed: true, provider: 'modelscope' }), error => {
|
|
215
|
+
assert.equal(error.code, 'EMBEDDING_SOURCE_UNAVAILABLE')
|
|
216
|
+
assert.ok(String(error.message).includes('on modelscope'), error.message)
|
|
217
|
+
return true
|
|
218
|
+
})
|
|
219
|
+
// 换源之后默认源仍然可用(不许把条目卡在唯一来源上)
|
|
220
|
+
const third = createEmbeddingDownloadManager({ root, catalog: [mirrored], fetchImpl })
|
|
221
|
+
assert.equal((await third.download(mirrored.id, { confirmed: true })).provider, 'hf-mirror')
|
|
222
|
+
})
|
|
223
|
+
|
|
224
|
+
test('verifyStagedArtifacts 核对 last-token pooling、float16 输出与窗口上限', async () => {
|
|
225
|
+
const dir = fs.mkdtempSync(path.join(scratch, 'last-'))
|
|
226
|
+
const graph = tinyOnnxGraph(8)
|
|
227
|
+
fs.mkdirSync(path.join(dir, '1_Pooling'), { recursive: true })
|
|
228
|
+
const base = {
|
|
229
|
+
id: 'fixture-last', dimension: 8, maxTokens: 32, pooling: 'last', normalization: 'l2',
|
|
230
|
+
graph: { path: 'onnx/tiny.onnx', opset: 11, inputs: [
|
|
231
|
+
{ name: 'input_ids', dtype: 'int64' }, { name: 'attention_mask', dtype: 'int64' }, { name: 'token_type_ids', dtype: 'int64' },
|
|
232
|
+
], outputs: [{ name: 'last_hidden_state', dtype: 'float32', shape: ['batch_size', 'sequence_length', 8] }] },
|
|
233
|
+
files: [
|
|
234
|
+
{ path: 'config.json', role: 'config' },
|
|
235
|
+
{ path: 'onnx/tiny.onnx', role: 'graph' },
|
|
236
|
+
{ path: '1_Pooling/config.json', role: 'pooling-config' },
|
|
237
|
+
],
|
|
238
|
+
}
|
|
239
|
+
fs.writeFileSync(path.join(dir, 'config.json'), JSON.stringify({ hidden_size: 8, max_position_embeddings: 32 }), 'utf8')
|
|
240
|
+
fs.mkdirSync(path.join(dir, 'onnx'), { recursive: true })
|
|
241
|
+
fs.writeFileSync(path.join(dir, 'onnx/tiny.onnx'), graph)
|
|
242
|
+
fs.writeFileSync(path.join(dir, '1_Pooling', 'config.json'), JSON.stringify({ word_embedding_dimension: 8, pooling_mode_lasttoken: true }), 'utf8')
|
|
243
|
+
const ok = await verifyStagedArtifacts({ entry: base, dir })
|
|
244
|
+
assert.ok(ok.checks.includes('pooling=last'), JSON.stringify(ok.checks))
|
|
245
|
+
|
|
246
|
+
fs.writeFileSync(path.join(dir, '1_Pooling', 'config.json'), JSON.stringify({ word_embedding_dimension: 8, pooling_mode_mean_tokens: true }), 'utf8')
|
|
247
|
+
await assert.rejects(verifyStagedArtifacts({ entry: base, dir }), /not last-token pooling/)
|
|
248
|
+
fs.writeFileSync(path.join(dir, '1_Pooling', 'config.json'), JSON.stringify({ word_embedding_dimension: 8, pooling_mode_lasttoken: true }), 'utf8')
|
|
249
|
+
|
|
250
|
+
// 窗口可以比制品小(刻意策略),但不能比制品大(必炸)
|
|
251
|
+
await assert.rejects(verifyStagedArtifacts({ entry: { ...base, maxTokens: 64 }, dir }), /exceeds the artifact's max_position_embeddings/)
|
|
252
|
+
// float16 图输出是可以接受的,但声明必须与图一致
|
|
253
|
+
const fp16 = { ...base, files: base.files.map(file => (file.role === 'graph' ? file : file)), graph: { ...base.graph, outputs: [{ name: 'last_hidden_state', dtype: 'float16', shape: ['batch_size', 'sequence_length', 8] }] } }
|
|
254
|
+
await assert.rejects(verifyStagedArtifacts({ entry: fp16, dir }), /output dtype/)
|
|
255
|
+
})
|
|
256
|
+
|
|
162
257
|
test('校验通过的制品原子安装:staging 清空、installed.json 记录 active:false', async () => {
|
|
163
258
|
const { entry, payloads } = fixtureManifest()
|
|
164
259
|
const { fetchImpl, calls } = makeFetch(entry, payloads)
|
|
@@ -10,7 +10,7 @@ process.env.DSH_MEMU_HOME = home
|
|
|
10
10
|
delete process.env.DSH_MEMU_EMBED_RUNTIME_DIR
|
|
11
11
|
|
|
12
12
|
const runtime = await import('../lib/embedding-runtime.js')
|
|
13
|
-
const { getEmbeddingModel } = await import('../lib/embedding-catalog.js')
|
|
13
|
+
const { getEmbeddingCatalog, getEmbeddingModel } = await import('../lib/embedding-catalog.js')
|
|
14
14
|
const service = await import('../lib/local-vector-service.js')
|
|
15
15
|
const { profileFromCatalog, setRequestedProfile } = await import('../lib/vector-profile.js')
|
|
16
16
|
|
|
@@ -105,7 +105,12 @@ function makeInstaller(root, options = {}) {
|
|
|
105
105
|
}
|
|
106
106
|
|
|
107
107
|
test('a pending catalog model pins no runtime and is refused before any download', () => {
|
|
108
|
-
|
|
108
|
+
// 目录里现在没有 pending 条目,所以这里造一个:未核实的候选就是没有 runtime 事实。
|
|
109
|
+
const plan = runtime.runtimePlan({
|
|
110
|
+
id: 'pending-candidate',
|
|
111
|
+
availability: 'pending',
|
|
112
|
+
runtime: { engine: 'onnxruntime-node', version: null, abi: 'napi-v6' },
|
|
113
|
+
}, 'win32-x64')
|
|
109
114
|
assert.equal(plan.ok, false)
|
|
110
115
|
assert.ok(plan.errors.some(item => item.code === 'RUNTIME_ENTRY_NOT_AVAILABLE'))
|
|
111
116
|
assert.ok(plan.errors.some(item => item.code === 'RUNTIME_ARTIFACTS_MISSING'))
|
|
@@ -118,18 +123,21 @@ test('a platform with no pinned native binaries fails closed instead of guessing
|
|
|
118
123
|
assert.deepEqual(plan.errors.map(item => item.code), ['RUNTIME_NATIVE_MISSING'])
|
|
119
124
|
})
|
|
120
125
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
assert.
|
|
129
|
-
|
|
130
|
-
|
|
126
|
+
// 三个模型共用同一棵 pin 住的受管运行时树(PINNED_ONNX_RUNTIME),所以每条都得能整成
|
|
127
|
+
// 完整计划;哪天共享常量被改坏,这里会先红。
|
|
128
|
+
test('每一条已核实的目录条目都钉住了每个平台的完整运行时', () => {
|
|
129
|
+
for (const entry of getEmbeddingCatalog()) {
|
|
130
|
+
for (const platform of ['win32-x64', 'win32-arm64', 'linux-x64', 'linux-arm64', 'darwin-arm64']) {
|
|
131
|
+
const plan = runtime.runtimePlan(entry, platform)
|
|
132
|
+
assert.equal(plan.ok, true, entry.id + ' ' + platform + ': ' + JSON.stringify(plan.errors))
|
|
133
|
+
assert.equal(plan.artifacts.length, 3, entry.id)
|
|
134
|
+
for (const artifact of plan.artifacts) {
|
|
135
|
+
assert.match(artifact.sha256, /^[0-9a-f]{64}$/)
|
|
136
|
+
assert.equal(artifact.url.startsWith('https://'), true)
|
|
137
|
+
assert.ok(artifact.bytes > 0)
|
|
138
|
+
}
|
|
139
|
+
assert.ok(plan.native.length >= 2, entry.id + ' ' + platform)
|
|
131
140
|
}
|
|
132
|
-
assert.ok(plan.native.length >= 2)
|
|
133
141
|
}
|
|
134
142
|
})
|
|
135
143
|
|
|
@@ -0,0 +1,261 @@
|
|
|
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
|
+
|
|
7
|
+
// 工单 F:实体主节点合并(§9)。断言针对库内实体文件与私有索引的真实落盘状态,
|
|
8
|
+
// 不 mock 存储层——合并的价值就在于两个文件一起收敛,且中途失败不能留下悬空边。
|
|
9
|
+
const home = fs.mkdtempSync(path.join(os.tmpdir(), 'dsh-memu-memory-entity-merge-'))
|
|
10
|
+
process.env.DSH_MEMU_HOME = home
|
|
11
|
+
const state = await import('../lib/state.js')
|
|
12
|
+
const graph = await import('../lib/graph.js')
|
|
13
|
+
const { indexDir } = await import('../lib/paths.js')
|
|
14
|
+
const { listEvents } = await import('../lib/activity-log.js')
|
|
15
|
+
|
|
16
|
+
test.after(() => { state.flushState(); fs.rmSync(home, { recursive: true, force: true }) })
|
|
17
|
+
|
|
18
|
+
function newLibrary(name) {
|
|
19
|
+
return state.ensureSession(name, { cwd: path.join(home, name), agentPreset: 'rp' }).libraryId
|
|
20
|
+
}
|
|
21
|
+
const entitiesFile = (libraryId) => path.join(indexDir(), `${libraryId}.entities.json`)
|
|
22
|
+
const readStore = (libraryId) => JSON.parse(fs.readFileSync(entitiesFile(libraryId), 'utf8'))
|
|
23
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
|
|
24
|
+
|
|
25
|
+
test('mergeEntities remaps every link to the target, dedupes (to, kind) and deletes the sources', () => {
|
|
26
|
+
const libraryId = newLibrary('merge-core')
|
|
27
|
+
const target = graph.createEntity({ libraryId, name: 'Lin Mian', kind: 'person', aliases: ['Mian'] })
|
|
28
|
+
const source1 = graph.createEntity({ libraryId, name: 'Lin', kind: 'person', aliases: ["Mian'er"] })
|
|
29
|
+
const source2 = graph.createEntity({ libraryId, name: 'Mian', kind: 'person', aliases: ['Lin Mian'] })
|
|
30
|
+
state.savePrivateIndex(libraryId, { entries: {
|
|
31
|
+
'mem-merge-a': { title: 'a', content: 'a scene', status: 'active', visibility: 'shared', links: [
|
|
32
|
+
{ to: source1.id, kind: 'person', note: 'Lin is the person this scene is about.', confidence: 0.9 },
|
|
33
|
+
{ to: target.id, kind: 'person', note: 'The pre-existing target edge wins the dedupe.', confidence: 0.4 },
|
|
34
|
+
{ to: source1.id, kind: 'relation', note: 'Lin is related to the events in this scene.', confidence: 0.5 },
|
|
35
|
+
{ to: 'mem-merge-b', kind: 'event', note: 'The same event seen from another side.', confidence: 0.6 },
|
|
36
|
+
] },
|
|
37
|
+
'mem-merge-b': { title: 'b', content: 'b scene', status: 'active', visibility: 'shared', links: [
|
|
38
|
+
{ to: source2.id, kind: 'person', note: 'Mian is the person this scene is about.', confidence: 0.8, source: 'auto' },
|
|
39
|
+
{ to: source1.id, kind: 'person', note: 'The same person named Lin again.', confidence: 0.7 },
|
|
40
|
+
] },
|
|
41
|
+
} })
|
|
42
|
+
|
|
43
|
+
const result = graph.mergeEntities({ libraryId, targetId: target.id, sourceIds: [source1.id, source2.id] })
|
|
44
|
+
assert.equal(result.targetId, target.id)
|
|
45
|
+
assert.deepEqual(result.merged, [source1.id, source2.id])
|
|
46
|
+
assert.equal(result.remappedLinks, 4, 'every link that pointed at a source is redirected')
|
|
47
|
+
assert.equal(result.duplicateLinks, 2, 'the second (to, kind) pair per entry is dropped')
|
|
48
|
+
assert.deepEqual(result.relationships, { moved: 0, conflicts: [] })
|
|
49
|
+
assert.deepEqual(result.droppedAliases, [])
|
|
50
|
+
assert.deepEqual(result.aliases, ['Mian', 'Lin', "Mian'er", 'Lin Mian'])
|
|
51
|
+
|
|
52
|
+
const index = state.loadPrivateIndex(libraryId)
|
|
53
|
+
const allLinks = Object.values(index.entries).flatMap(entry => entry.links || [])
|
|
54
|
+
assert.ok(allLinks.every(link => link.to !== source1.id && link.to !== source2.id), 'no link survives pointing at a merged source')
|
|
55
|
+
const aLinks = index.entries['mem-merge-a'].links
|
|
56
|
+
assert.equal(aLinks.filter(link => link.to === target.id && link.kind === 'person').length, 1)
|
|
57
|
+
assert.equal(aLinks.find(link => link.to === target.id && link.kind === 'person').note, 'The pre-existing target edge wins the dedupe.')
|
|
58
|
+
assert.equal(aLinks.find(link => link.to === target.id && link.kind === 'relation').confidence, 0.5)
|
|
59
|
+
const bLinks = index.entries['mem-merge-b'].links
|
|
60
|
+
assert.equal(bLinks.length, 1, 'the duplicate source edge is dropped')
|
|
61
|
+
assert.equal(bLinks[0].to, target.id)
|
|
62
|
+
assert.equal(bLinks[0].source, 'auto', 'auto metadata survives the merge')
|
|
63
|
+
|
|
64
|
+
const store = graph.graphExport(libraryId)
|
|
65
|
+
assert.equal(store.entities[source1.id], undefined)
|
|
66
|
+
assert.equal(store.entities[source2.id], undefined)
|
|
67
|
+
assert.equal(store.entities[target.id].name, 'Lin Mian', 'the target name stays the display name')
|
|
68
|
+
assert.deepEqual(store.entities[target.id].aliases, ['Mian', 'Lin', "Mian'er", 'Lin Mian'])
|
|
69
|
+
|
|
70
|
+
const view = graph.getGraph({ libraryId, focusId: 'mem-merge-b', depth: 1 })
|
|
71
|
+
assert.ok(view.nodes.some(node => node.id === target.id && node.title === 'Lin Mian'))
|
|
72
|
+
assert.ok(!view.nodes.some(node => node.id === source1.id || node.id === source2.id), 'getGraph resolves the merged ids, not placeholders')
|
|
73
|
+
|
|
74
|
+
const events = listEvents({ limit: 100, kind: 'write' })
|
|
75
|
+
const event = events.find(item => item.detail?.op === 'entity-merge' && item.detail?.targetId === target.id)
|
|
76
|
+
assert.ok(event, 'the merge is recorded in the activity log')
|
|
77
|
+
assert.deepEqual(event.refs, [target.id, source1.id, source2.id])
|
|
78
|
+
assert.equal(event.ok, true)
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
test('mergeEntities unions aliases, caps at 30 and reports the dropped tail', () => {
|
|
82
|
+
const libraryId = newLibrary('merge-aliases')
|
|
83
|
+
const targetAliases = Array.from({ length: 10 }, (_, index) => `target-alias-${index}`)
|
|
84
|
+
const source1Aliases = Array.from({ length: 25 }, (_, index) => `source-one-alias-${index}`)
|
|
85
|
+
const source2Aliases = Array.from({ length: 10 }, (_, index) => `source-two-alias-${index}`)
|
|
86
|
+
const target = graph.createEntity({ libraryId, name: 'Alias Target', kind: 'person', aliases: targetAliases })
|
|
87
|
+
const source1 = graph.createEntity({ libraryId, name: 'Alias Source One', kind: 'person', aliases: source1Aliases })
|
|
88
|
+
const source2 = graph.createEntity({ libraryId, name: 'Alias Source Two', kind: 'person', aliases: source2Aliases })
|
|
89
|
+
const union = [...targetAliases, 'Alias Source One', ...source1Aliases, 'Alias Source Two', ...source2Aliases]
|
|
90
|
+
|
|
91
|
+
const result = graph.mergeEntities({ libraryId, targetId: target.id, sourceIds: [source1.id, source2.id] })
|
|
92
|
+
assert.equal(result.aliases.length, 30)
|
|
93
|
+
assert.deepEqual(result.aliases, union.slice(0, 30))
|
|
94
|
+
assert.deepEqual(result.droppedAliases, union.slice(30))
|
|
95
|
+
assert.equal(result.droppedAliases.length, union.length - 30)
|
|
96
|
+
const stored = readStore(libraryId).entities[target.id]
|
|
97
|
+
assert.ok(stored, 'the merge target is never collected by the safety-net prune, even while unreferenced')
|
|
98
|
+
assert.equal(stored.name, 'Alias Target')
|
|
99
|
+
assert.deepEqual(stored.aliases, union.slice(0, 30))
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
test('mergeEntities rejects kind mismatches, self-sources, empty lists and unknown ids without writing', () => {
|
|
103
|
+
const libraryId = newLibrary('merge-validation')
|
|
104
|
+
const otherLibraryId = newLibrary('merge-validation-other')
|
|
105
|
+
const keep = graph.createEntity({ libraryId, name: 'Keep', kind: 'person' })
|
|
106
|
+
const otherKind = graph.createEntity({ libraryId, name: 'Keep Place', kind: 'place' })
|
|
107
|
+
const peer = graph.createEntity({ libraryId, name: 'Peer', kind: 'person' })
|
|
108
|
+
const foreign = graph.createEntity({ libraryId: otherLibraryId, name: 'Foreign', kind: 'person' })
|
|
109
|
+
state.savePrivateIndex(libraryId, { entries: {
|
|
110
|
+
'mem-validation': { title: 'validation', content: 'validation scene', status: 'active', visibility: 'shared', links: [
|
|
111
|
+
{ to: peer.id, kind: 'person', note: 'Peer is the person this scene is about.', confidence: 0.9 },
|
|
112
|
+
] },
|
|
113
|
+
} })
|
|
114
|
+
const before = fs.readFileSync(entitiesFile(libraryId), 'utf8')
|
|
115
|
+
assert.throws(() => graph.mergeEntities({ libraryId, targetId: keep.id, sourceIds: [otherKind.id] }), /different kinds/)
|
|
116
|
+
assert.throws(() => graph.mergeEntities({ libraryId, targetId: keep.id, sourceIds: [keep.id] }), /own sources/)
|
|
117
|
+
assert.throws(() => graph.mergeEntities({ libraryId, targetId: keep.id, sourceIds: [] }), /non-empty array/)
|
|
118
|
+
assert.throws(() => graph.mergeEntities({ libraryId, targetId: keep.id }), /non-empty array/)
|
|
119
|
+
assert.throws(() => graph.mergeEntities({ libraryId, targetId: keep.id, sourceIds: [peer.id, 'ent-ghost'] }), /unknown entity/)
|
|
120
|
+
assert.throws(() => graph.mergeEntities({ libraryId, targetId: keep.id, sourceIds: [foreign.id] }), /unknown entity/, 'an id that lives in another library is not mergeable here')
|
|
121
|
+
assert.throws(() => graph.mergeEntities({ libraryId, targetId: 'ent-ghost', sourceIds: [peer.id] }), /unknown entity/)
|
|
122
|
+
assert.throws(() => graph.mergeEntities({ targetId: keep.id, sourceIds: [peer.id] }), /no bound session/)
|
|
123
|
+
assert.equal(fs.readFileSync(entitiesFile(libraryId), 'utf8'), before, 'a rejected merge never rewrites the entity store')
|
|
124
|
+
const links = state.loadPrivateIndex(libraryId).entries['mem-validation'].links
|
|
125
|
+
assert.deepEqual(links.map(link => link.to), [peer.id])
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
test('mergeEntities re-keys relationship snapshots, preserves the target side and reports conflicts', () => {
|
|
129
|
+
const libraryId = newLibrary('merge-relationships')
|
|
130
|
+
const target = graph.createEntity({ libraryId, name: 'Target', kind: 'person' })
|
|
131
|
+
const source1 = graph.createEntity({ libraryId, name: 'Source One', kind: 'person' })
|
|
132
|
+
const source2 = graph.createEntity({ libraryId, name: 'Source Two', kind: 'person' })
|
|
133
|
+
const buddy = graph.createEntity({ libraryId, name: 'Buddy', kind: 'person' })
|
|
134
|
+
const companion = graph.createEntity({ libraryId, name: 'Companion', kind: 'person' })
|
|
135
|
+
const orphan = graph.createEntity({ libraryId, name: 'Orphan', kind: 'person' })
|
|
136
|
+
state.savePrivateIndex(libraryId, { entries: {
|
|
137
|
+
'mem-rel-evidence': { title: 'evidence', content: 'evidence scene', status: 'active', visibility: 'shared' },
|
|
138
|
+
} })
|
|
139
|
+
graph.updateRelationship({ libraryId, fromId: target.id, toId: buddy.id, state: { trust: 0.2 }, evidenceIds: ['mem-rel-evidence'], reason: 'The target-side snapshot is the older evidence.' })
|
|
140
|
+
graph.updateRelationship({ libraryId, fromId: source1.id, toId: buddy.id, state: { trust: 0.9 }, evidenceIds: ['mem-rel-evidence'], reason: 'The source-side snapshot is the newer evidence.' })
|
|
141
|
+
graph.updateRelationship({ libraryId, fromId: buddy.id, toId: source1.id, state: { warmth: 0.4 }, evidenceIds: ['mem-rel-evidence'], reason: 'The reverse direction is its own snapshot.' })
|
|
142
|
+
graph.updateRelationship({ libraryId, fromId: buddy.id, toId: source1.id, state: { warmth: 0.5 }, evidenceIds: ['mem-rel-evidence'], reason: 'A second revision gives the snapshot history.' })
|
|
143
|
+
graph.updateRelationship({ libraryId, fromId: source1.id, toId: source2.id, state: { trust: 0.3 }, evidenceIds: ['mem-rel-evidence'], reason: 'A source-only snapshot becomes a self loop.' })
|
|
144
|
+
graph.updateRelationship({ libraryId, fromId: source2.id, toId: companion.id, state: { familiarity: 0.6 }, evidenceIds: ['mem-rel-evidence'], reason: 'A source behind the relationship moves with it.' })
|
|
145
|
+
|
|
146
|
+
const result = graph.mergeEntities({ libraryId, targetId: target.id, sourceIds: [source1.id, source2.id] })
|
|
147
|
+
assert.deepEqual(result.relationships, {
|
|
148
|
+
moved: 2,
|
|
149
|
+
conflicts: [
|
|
150
|
+
{ key: JSON.stringify([source1.id, buddy.id]), reason: 'existing-target', keptKey: JSON.stringify([target.id, buddy.id]) },
|
|
151
|
+
{ key: JSON.stringify([source1.id, source2.id]), reason: 'self' },
|
|
152
|
+
],
|
|
153
|
+
})
|
|
154
|
+
const kept = graph.getRelationship({ libraryId, fromId: target.id, toId: buddy.id }).relationship
|
|
155
|
+
assert.equal(kept.state.trust, 0.2, 'the existing target-side snapshot is not overwritten')
|
|
156
|
+
assert.equal(kept.revision, 1)
|
|
157
|
+
const moved = graph.getRelationship({ libraryId, fromId: buddy.id, toId: target.id }).relationship
|
|
158
|
+
assert.equal(moved.fromId, buddy.id)
|
|
159
|
+
assert.equal(moved.toId, target.id)
|
|
160
|
+
assert.equal(moved.state.warmth, 0.5)
|
|
161
|
+
assert.equal(moved.history.length, 1, 'history moves with the snapshot')
|
|
162
|
+
assert.equal(moved.revision, 2)
|
|
163
|
+
const movedOut = graph.getRelationship({ libraryId, fromId: target.id, toId: companion.id }).relationship
|
|
164
|
+
assert.equal(movedOut.state.familiarity, 0.6)
|
|
165
|
+
assert.equal(graph.getRelationship({ libraryId, fromId: source1.id, toId: buddy.id }).relationship, null, 'no old source key survives')
|
|
166
|
+
const store = graph.graphExport(libraryId)
|
|
167
|
+
assert.ok(Object.keys(store.relationships).every(key => !key.includes(source1.id) && !key.includes(source2.id)))
|
|
168
|
+
assert.equal(store.entities[orphan.id], undefined, 'the prune safety net collected the unrelated orphan')
|
|
169
|
+
assert.ok(store.entities[target.id], 'the target itself stays')
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
test('suggestEntityMerges: containment and normalization produce shorter→longer pairs', () => {
|
|
173
|
+
const libraryId = newLibrary('suggest-names')
|
|
174
|
+
const lin = graph.createEntity({ libraryId, name: 'Lin', kind: 'person' })
|
|
175
|
+
const linMian = graph.createEntity({ libraryId, name: 'Lin Mian', kind: 'person' })
|
|
176
|
+
const lower = graph.createEntity({ libraryId, name: 'lin', kind: 'person' })
|
|
177
|
+
const yuike = graph.createEntity({ libraryId, name: 'YUIKE', kind: 'person' })
|
|
178
|
+
const tanaka = graph.createEntity({ libraryId, name: 'yuike tanaka', kind: 'person' })
|
|
179
|
+
const place = graph.createEntity({ libraryId, name: 'Lin Road', kind: 'place' })
|
|
180
|
+
|
|
181
|
+
const { items } = graph.suggestEntityMerges({ libraryId })
|
|
182
|
+
const pair = (source, target) => items.find(item => item.sourceId === source.id && item.targetId === target.id)
|
|
183
|
+
assert.ok(pair(lin, linMian), 'the shorter normalized name folds into the longer one')
|
|
184
|
+
assert.ok(pair(lower, linMian))
|
|
185
|
+
assert.equal(pair(lin, linMian).reason, 'containment')
|
|
186
|
+
assert.ok(pair(yuike, tanaka), 'NFKC/case folding finds the full-width variant')
|
|
187
|
+
assert.ok(!items.some(item => (item.sourceId === lin.id && item.targetId === lower.id) || (item.sourceId === lower.id && item.targetId === lin.id)),
|
|
188
|
+
'identical normalized names are not a candidate')
|
|
189
|
+
assert.ok(!items.some(item => (item.sourceId === place.id && item.targetId === lin.id) || (item.sourceId === lin.id && item.targetId === place.id)),
|
|
190
|
+
'different kinds never pair up')
|
|
191
|
+
assert.ok(!items.some(item => item.sourceId === place.id || item.targetId === place.id), 'the place has no same-kind candidate')
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
test('suggestEntityMerges: near-miss spellings within two edits are candidates, with a stable direction', async () => {
|
|
195
|
+
const libraryId = newLibrary('suggest-edit')
|
|
196
|
+
const kyle = graph.createEntity({ libraryId, name: 'Kyle', kind: 'person' })
|
|
197
|
+
await sleep(5)
|
|
198
|
+
const kile = graph.createEntity({ libraryId, name: 'Kile', kind: 'person' })
|
|
199
|
+
await sleep(5)
|
|
200
|
+
const marcus = graph.createEntity({ libraryId, name: 'Marcus', kind: 'person' })
|
|
201
|
+
await sleep(5)
|
|
202
|
+
const marcos = graph.createEntity({ libraryId, name: 'Marcos', kind: 'person' })
|
|
203
|
+
|
|
204
|
+
const first = graph.suggestEntityMerges({ libraryId })
|
|
205
|
+
const second = graph.suggestEntityMerges({ libraryId })
|
|
206
|
+
assert.deepEqual(first, second, 'suggestion order is deterministic')
|
|
207
|
+
assert.equal(first.items.length, 2)
|
|
208
|
+
assert.ok(first.items.every(item => item.reason === 'edit-distance'))
|
|
209
|
+
assert.deepEqual(first.items.find(item => [item.sourceId, item.targetId].includes(kyle.id)),
|
|
210
|
+
{ sourceId: kile.id, targetId: kyle.id, reason: 'edit-distance' }, 'equal length folds into the older id')
|
|
211
|
+
assert.deepEqual(first.items.find(item => [item.sourceId, item.targetId].includes(marcus.id)),
|
|
212
|
+
{ sourceId: marcos.id, targetId: marcus.id, reason: 'edit-distance' })
|
|
213
|
+
})
|
|
214
|
+
|
|
215
|
+
test('suggestEntityMerges: kind mismatch and identical folded names yield no candidates', () => {
|
|
216
|
+
const libraryId = newLibrary('suggest-filter')
|
|
217
|
+
graph.createEntity({ libraryId, name: 'Aria', kind: 'person' })
|
|
218
|
+
graph.createEntity({ libraryId, name: 'Ariadne', kind: 'place' })
|
|
219
|
+
graph.createEntity({ libraryId, name: 'Mika', kind: 'person' })
|
|
220
|
+
graph.createEntity({ libraryId, name: 'MIKA', kind: 'person' })
|
|
221
|
+
assert.deepEqual(graph.suggestEntityMerges({ libraryId }), { items: [] })
|
|
222
|
+
})
|
|
223
|
+
|
|
224
|
+
test('suggestEntityMerges: unrelated names yield an empty list', () => {
|
|
225
|
+
const libraryId = newLibrary('suggest-none')
|
|
226
|
+
graph.createEntity({ libraryId, name: 'Zephyr', kind: 'person' })
|
|
227
|
+
graph.createEntity({ libraryId, name: 'Quartz', kind: 'person' })
|
|
228
|
+
assert.deepEqual(graph.suggestEntityMerges({ libraryId }).items, [])
|
|
229
|
+
})
|
|
230
|
+
|
|
231
|
+
test('suggestEntityMerges caps the list at 20 and never writes', () => {
|
|
232
|
+
const libraryId = newLibrary('suggest-cap')
|
|
233
|
+
for (let index = 0; index < 25; index += 1) {
|
|
234
|
+
const suffix = String(index).padStart(2, '0')
|
|
235
|
+
graph.createEntity({ libraryId, name: `NestBase${suffix}`, kind: 'person' })
|
|
236
|
+
graph.createEntity({ libraryId, name: `NestBase${suffix}Extra`, kind: 'person' })
|
|
237
|
+
}
|
|
238
|
+
const file = entitiesFile(libraryId)
|
|
239
|
+
const before = fs.readFileSync(file, 'utf8')
|
|
240
|
+
const first = graph.suggestEntityMerges({ libraryId })
|
|
241
|
+
const second = graph.suggestEntityMerges({ libraryId })
|
|
242
|
+
assert.equal(fs.readFileSync(file, 'utf8'), before, 'suggesting is a pure read')
|
|
243
|
+
assert.deepEqual(second, first)
|
|
244
|
+
assert.equal(first.items.length, 20)
|
|
245
|
+
assert.ok(first.items.every(item => item.sourceId && item.targetId && item.sourceId !== item.targetId))
|
|
246
|
+
assert.ok(first.items.every(item => item.reason === 'containment' || item.reason === 'edit-distance'))
|
|
247
|
+
})
|
|
248
|
+
|
|
249
|
+
test('the RPC cases take exactly the payload the graph panel sends', async () => {
|
|
250
|
+
const libraryId = newLibrary('rpc-shape')
|
|
251
|
+
const target = graph.createEntity({ libraryId, name: '林眠儿', kind: 'person' })
|
|
252
|
+
const source = graph.createEntity({ libraryId, name: '林眠', kind: 'person' })
|
|
253
|
+
const rpc = await import('../lib/rpc.js')
|
|
254
|
+
const suggested = await rpc.dispatch({}, 'suggestEntityMerges', { libraryId })
|
|
255
|
+
assert.deepEqual(suggested.items, [{ sourceId: source.id, targetId: target.id, reason: 'containment' }])
|
|
256
|
+
// 面板一次只确认一对:sourceIds 是数组,但 UI 传的就是单个来源。
|
|
257
|
+
const merged = await rpc.dispatch({}, 'mergeEntities', { libraryId, targetId: target.id, sourceIds: [source.id] })
|
|
258
|
+
assert.equal(merged.targetId, target.id)
|
|
259
|
+
assert.deepEqual(merged.merged, [source.id])
|
|
260
|
+
assert.ok(merged.aliases.includes('林眠'), 'the source name becomes an alias of the main node')
|
|
261
|
+
})
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(测试回复)好的,我记得这件事。
|