dsh-memu-memory 1.3.15 → 1.4.0
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 +5 -2
- package/CHANGELOG.md +33 -1
- package/docs/client-conventions.md +1 -0
- package/docs/host-adaptation.md +52 -36
- package/docs/host-versions.md +120 -0
- package/docs/pending-work.md +51 -0
- package/docs/troubleshooting.md +15 -2
- package/lib/auto-link/candidates.js +4 -1
- package/lib/auto-link/decide.js +1 -1
- package/lib/auto-link/normalize.js +2 -2
- package/lib/auto-link/terminology.js +31 -1
- package/lib/binding-diagnostics.js +1 -1
- package/lib/client.js +53 -6
- package/lib/consolidation-snapshot.js +7 -1
- package/lib/curator.js +1 -1
- package/lib/diagnostics.js +10 -0
- package/lib/entity-repair.js +285 -0
- package/lib/environment-check.js +72 -3
- package/lib/global-graph.js +11 -2
- package/lib/graph.js +32 -8
- package/lib/host-surface.js +30 -0
- package/lib/index.js +116 -1
- package/lib/private-links.js +12 -0
- package/lib/rpc.js +48 -10
- package/lib/state.js +5 -1
- package/package.json +16 -1
- package/scripts/chain-matrix.mjs +4 -0
- package/scripts/check-doc-drift.mjs +194 -0
- package/scripts/entity-dedupe.mjs +110 -0
- package/tests/auto-link-candidates.test.mjs +22 -9
- package/tests/auto-link-decide.test.mjs +59 -59
- package/tests/auto-link-migration.test.mjs +4 -4
- package/tests/client-render.test.mjs +112 -1
- package/tests/cycle.test.mjs +2 -2
- package/tests/diagnostics.test.mjs +21 -0
- package/tests/doc-drift.test.mjs +122 -2
- package/tests/entity-merge.test.mjs +17 -0
- package/tests/environment-check.test.mjs +28 -0
- package/tests/global-graph.test.mjs +16 -0
- package/tests/global-lifecycle.test.mjs +24 -4
- package/tests/graph-cascade.test.mjs +111 -0
- package/tests/hybrid-recall.test.mjs +1 -1
- package/tests/managed-memory.test.mjs +7 -7
- package/tests/private-links.test.mjs +12 -0
- package/tests/recall-corpus-eligibility.test.mjs +3 -3
- package/tests/settings-plumbing.test.mjs +2 -0
- package/tests/tools.test.mjs +1 -1
- package/tests/webui-auth.test.mjs +58 -0
package/scripts/chain-matrix.mjs
CHANGED
|
@@ -88,6 +88,10 @@ const engineSource = [
|
|
|
88
88
|
'export const retrieve = (...a) => b.retrieve(...a);',
|
|
89
89
|
'export const forget = (...a) => b.forget(...a);',
|
|
90
90
|
'export const listFiles = (...a) => b.listFiles(...a);',
|
|
91
|
+
// lib/rpc.js imports the resident-engine hooks from the same module: without
|
|
92
|
+
// these two exports the whole matrix dies at import time (1.3.15 regression).
|
|
93
|
+
'export const closeMemuResident = () => 0;',
|
|
94
|
+
'export const prewarmMemuResident = async () => false;',
|
|
91
95
|
].join(' ')
|
|
92
96
|
const engineUrl = 'data:text/javascript,' + encodeURIComponent(engineSource)
|
|
93
97
|
const hooks = registerHooks({
|
|
@@ -288,11 +288,205 @@ export function checkLoaderStubs(root) {
|
|
|
288
288
|
return outcome(label, 'ok', REQUIRED_FILES.length + ' required loader/stub file(s) present; ' + testCount + ' *.test.mjs file(s)', [])
|
|
289
289
|
}
|
|
290
290
|
|
|
291
|
+
// Gate 4 — the cross-version host ledger (docs/host-versions.md §13).
|
|
292
|
+
//
|
|
293
|
+
// Why this exists: per-build host facts used to live in three places at once
|
|
294
|
+
// (host-adaptation §2 column, §5 record, §7 table). §7 stopped at 0.2.2-rc.1 and §2
|
|
295
|
+
// still claimed `connection.requestRejection` was "未变" after the host removed it in
|
|
296
|
+
// 0.2.3-alpha.1. The ledger is now the single owner, and this gate is what keeps it
|
|
297
|
+
// from rotting the same way: a build named anywhere in the docs must have a ledger row,
|
|
298
|
+
// every row must state how it was verified, and every probe site it points at must still
|
|
299
|
+
// exist in lib/.
|
|
300
|
+
const HOST_DOC = 'docs/host-versions.md'
|
|
301
|
+
const HOST_BUILD_RE = /\b0\.2\.\d+-(?:rc|alpha|beta)\.\d+\b/g
|
|
302
|
+
const LEDGER_ROW_RE = /^\|\s*`(0\.2\.\d+(?:-(?:rc|alpha|beta)\.\d+)?)`\s*\|([^|]*)\|([^|]*)\|([^|]*)\|([^|]*)\|([^|]*)\|/
|
|
303
|
+
const SURFACE_ROW_RE = /^\|\s*`([a-z][A-Za-z0-9]*)`\s*\|(.*)$/
|
|
304
|
+
const PROBE_SITE_RE = /(lib\/[\w./-]+\.js):([A-Za-z_$][\w$]*)/g
|
|
305
|
+
const LEDGER_VERDICTS = new Set(['真机', '源码', '未测'])
|
|
306
|
+
const LEDGER_GRADES = new Set(['exact', 'line', 'nearest'])
|
|
307
|
+
|
|
308
|
+
// Fenced code blocks are examples, not claims about the docs.
|
|
309
|
+
const stripFences = (text) => String(text || '').replace(/```[\s\S]*?```/g, '')
|
|
310
|
+
|
|
311
|
+
// Comment-free view of a JS file. Line-based on purpose: a character scanner has to
|
|
312
|
+
// know regex literals well enough not to mistake `/x/ // note` for a string, and a
|
|
313
|
+
// wrong guess makes this gate permanently red — which is worse than its blind spot
|
|
314
|
+
// (a version literal tucked into a trailing comment). Documentation lines are the
|
|
315
|
+
// place host versions legitimately get named, so those are what this exempts.
|
|
316
|
+
function stripJsComments(source) {
|
|
317
|
+
return String(source || '').split(/\r?\n/)
|
|
318
|
+
.map((line) => (/^[ \t]*(\/\/|\/?\*|\/\*)/.test(line) ? '' : line))
|
|
319
|
+
.join('\n')
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
function libSources(root) {
|
|
323
|
+
const out = []
|
|
324
|
+
const walk = (dir, rel) => {
|
|
325
|
+
let entries = []
|
|
326
|
+
try { entries = fs.readdirSync(dir, { withFileTypes: true }) } catch { return }
|
|
327
|
+
for (const entry of entries) {
|
|
328
|
+
const abs = path.join(dir, entry.name)
|
|
329
|
+
const nextRel = rel ? rel + '/' + entry.name : entry.name
|
|
330
|
+
if (entry.isDirectory()) walk(abs, nextRel)
|
|
331
|
+
else if (entry.isFile() && entry.name.endsWith('.js')) out.push({ rel: 'lib/' + nextRel, abs })
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
walk(path.join(root, 'lib'), '')
|
|
335
|
+
return out
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// Markdown table cells are usually written inline-code styled; unwrap them before
|
|
339
|
+
// comparing against the grammar the gate enforces.
|
|
340
|
+
const ledgerCell = (value) => String(value || '').replace(/`/g, '').trim()
|
|
341
|
+
|
|
342
|
+
function checkHostVersionRows(lines) {
|
|
343
|
+
const rows = []
|
|
344
|
+
for (const line of lines) {
|
|
345
|
+
const match = LEDGER_ROW_RE.exec(line)
|
|
346
|
+
if (!match) continue
|
|
347
|
+
rows.push({
|
|
348
|
+
build: match[1],
|
|
349
|
+
commit: ledgerCell(match[2]),
|
|
350
|
+
verdict: ledgerCell(match[3]),
|
|
351
|
+
grade: ledgerCell(match[4]),
|
|
352
|
+
date: ledgerCell(match[5]),
|
|
353
|
+
fingerprint: ledgerCell(match[6]),
|
|
354
|
+
})
|
|
355
|
+
}
|
|
356
|
+
return rows
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
export function checkHostVersions(root = DEFAULT_ROOT) {
|
|
360
|
+
const label = 'host-versions'
|
|
361
|
+
let text = ''
|
|
362
|
+
try {
|
|
363
|
+
text = fs.readFileSync(path.join(root, HOST_DOC), 'utf8')
|
|
364
|
+
} catch (error) {
|
|
365
|
+
if (!fs.existsSync(path.join(root, 'docs'))) {
|
|
366
|
+
return outcome(label, 'skipped', 'no docs/ directory - skipped, not applicable (npm package install)', [])
|
|
367
|
+
}
|
|
368
|
+
return outcome(label, 'fail', HOST_DOC + ' is missing or unreadable', [
|
|
369
|
+
firstLine(error.message),
|
|
370
|
+
'the per-build ledger owns every host-version fact (docs/host-adaptation.md §7 points here)',
|
|
371
|
+
])
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
const problems = []
|
|
375
|
+
const lines = text.split(/\r?\n/)
|
|
376
|
+
|
|
377
|
+
// 4b — ledger rows carry commit / verdict / grade / date.
|
|
378
|
+
const rows = checkHostVersionRows(lines)
|
|
379
|
+
if (rows.length === 0) {
|
|
380
|
+
problems.push('§13.1 has no ledger rows (expected one `| `0.2.x-rc.y` | commit | 核对方式 | 判定 | 日期 | 指纹 | … |` row per build)')
|
|
381
|
+
}
|
|
382
|
+
for (const row of rows) {
|
|
383
|
+
if (!/^[0-9a-f]{7,12}$/.test(row.commit)) {
|
|
384
|
+
problems.push(`§13.1 ${row.build}: commit "${row.commit}" is not a short sha (use git rev-parse --short <tag>^{commit})`)
|
|
385
|
+
}
|
|
386
|
+
if (!LEDGER_VERDICTS.has(row.verdict)) problems.push(`§13.1 ${row.build}: 核对方式 "${row.verdict}" must be one of 真机 / 源码 / 未测`)
|
|
387
|
+
if (!LEDGER_GRADES.has(row.grade)) problems.push(`§13.1 ${row.build}: 判定 "${row.grade}" must be one of exact / line / nearest`)
|
|
388
|
+
if (!/^\d{4}-\d{2}-\d{2}$/.test(row.date)) problems.push(`§13.1 ${row.build}: date "${row.date}" must be ISO YYYY-MM-DD`)
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
// 4c + 4e — registered surfaces and their probe sites.
|
|
392
|
+
const surfaces = new Map()
|
|
393
|
+
for (const line of lines) {
|
|
394
|
+
const match = SURFACE_ROW_RE.exec(line)
|
|
395
|
+
if (!match) continue
|
|
396
|
+
const sites = [...String(match[2]).matchAll(PROBE_SITE_RE)].map((site) => ({ file: site[1], symbol: site[2] }))
|
|
397
|
+
if (sites.length === 0) {
|
|
398
|
+
problems.push(`§13.2 ${match[1]}: no probe site written as lib/<file>.js:<symbol>`)
|
|
399
|
+
continue
|
|
400
|
+
}
|
|
401
|
+
surfaces.set(match[1], sites)
|
|
402
|
+
}
|
|
403
|
+
if (surfaces.size === 0) problems.push('§13.2 has no surface rows (expected `| \\`面ID\\` | … |`)')
|
|
404
|
+
for (const [slug, sites] of surfaces) {
|
|
405
|
+
for (const site of sites) {
|
|
406
|
+
let source = ''
|
|
407
|
+
try {
|
|
408
|
+
source = fs.readFileSync(path.join(root, site.file), 'utf8')
|
|
409
|
+
} catch {
|
|
410
|
+
problems.push(`§13.2 ${slug}: ${site.file} does not exist (renamed? update the ledger in the same commit)`)
|
|
411
|
+
continue
|
|
412
|
+
}
|
|
413
|
+
if (!new RegExp(`\\b${site.symbol.replace(/\$/g, '\\$')}\\b`).test(source)) {
|
|
414
|
+
problems.push(`§13.2 ${slug}: symbol ${site.symbol} not found in ${site.file} (renamed? update the ledger in the same commit)`)
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
for (const row of rows) {
|
|
419
|
+
const pairs = String(row.fingerprint || '').split(';').map((part) => part.trim()).filter(Boolean)
|
|
420
|
+
if (pairs.length < 3) problems.push(`§13.1 ${row.build}: fingerprint "${row.fingerprint}" must list at least 3 key=value pairs`)
|
|
421
|
+
for (const pair of pairs) {
|
|
422
|
+
const key = pair.split('=')[0].trim()
|
|
423
|
+
if (!pair.includes('=') || !surfaces.has(key)) problems.push(`§13.1 ${row.build}: fingerprint key "${key}" is not a surface registered in §13.2`)
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
// 4a — every host build named anywhere in the docs needs a ledger row.
|
|
428
|
+
const documented = new Map()
|
|
429
|
+
for (const rel of docFiles(root)) {
|
|
430
|
+
if (rel === HOST_DOC) continue
|
|
431
|
+
let body = ''
|
|
432
|
+
try {
|
|
433
|
+
body = stripFences(fs.readFileSync(path.join(root, rel), 'utf8'))
|
|
434
|
+
} catch {
|
|
435
|
+
continue
|
|
436
|
+
}
|
|
437
|
+
for (const match of body.matchAll(HOST_BUILD_RE)) {
|
|
438
|
+
if (!documented.has(match[0])) documented.set(match[0], rel)
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
const ledger = new Set(rows.map((row) => row.build))
|
|
442
|
+
for (const [build, rel] of documented) {
|
|
443
|
+
if (!ledger.has(build)) problems.push(`${rel} mentions ${build} but §13.1 has no row for it (newest row: ${[...ledger].sort().pop() || 'none'})`)
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
// 4d — no host version literals in running code (AGENTS.md 硬规则 1).
|
|
447
|
+
for (const { rel, abs } of libSources(root)) {
|
|
448
|
+
let source = ''
|
|
449
|
+
try {
|
|
450
|
+
source = fs.readFileSync(abs, 'utf8')
|
|
451
|
+
} catch {
|
|
452
|
+
continue
|
|
453
|
+
}
|
|
454
|
+
// DSH builds always carry a prerelease tag (`0.2.3-alpha.1`); a bare `0.2.0` in
|
|
455
|
+
// this repo is some other artifact (the tokenizers crate), and flagging it would
|
|
456
|
+
// make the gate permanently red.
|
|
457
|
+
const code = stripJsComments(source)
|
|
458
|
+
const hit = /\b0\.2\.\d+-(?:rc|alpha|beta)\.\d+\b/.exec(code)
|
|
459
|
+
if (hit) problems.push(`${rel}: host version "${hit[0]}" sits in running code — probe the shape instead (AGENTS.md 硬规则 1)`)
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
// 4f — package.json dshReleases must agree with the verified rows.
|
|
463
|
+
let manifest = null
|
|
464
|
+
try {
|
|
465
|
+
manifest = JSON.parse(fs.readFileSync(path.join(root, 'package.json'), 'utf8'))
|
|
466
|
+
} catch {
|
|
467
|
+
manifest = null
|
|
468
|
+
}
|
|
469
|
+
const declared = manifest?.dsh?.compatibility?.dshReleases
|
|
470
|
+
if (Array.isArray(declared)) {
|
|
471
|
+
const expected = rows.filter((row) => row.grade === 'exact' || row.grade === 'line').map((row) => row.build).sort()
|
|
472
|
+
const got = [...declared].map((value) => String(value)).sort()
|
|
473
|
+
if (expected.join('|') !== got.join('|')) {
|
|
474
|
+
problems.push('package.json dsh.compatibility.dshReleases disagrees with §13.1: declared ' + (got.join(', ') || '(none)')
|
|
475
|
+
+ ' vs ledger ' + (expected.join(', ') || '(none)'))
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
if (problems.length > 0) return outcome(label, 'fail', 'host ledger drift: ' + problems.length + ' problem(s)', problems)
|
|
480
|
+
return outcome(label, 'ok', rows.length + ' verified build(s), ' + surfaces.size
|
|
481
|
+
+ ' registered host surface(s), no undocumented build reference', [])
|
|
482
|
+
}
|
|
483
|
+
|
|
291
484
|
export function runChecks(root = DEFAULT_ROOT) {
|
|
292
485
|
const checks = [
|
|
293
486
|
['rpc-method-count', checkRpcMethodCount],
|
|
294
487
|
['test-tracking', checkTestTracking],
|
|
295
488
|
['loader-stubs', checkLoaderStubs],
|
|
489
|
+
['host-versions', checkHostVersions],
|
|
296
490
|
]
|
|
297
491
|
return checks.map(([label, run]) => {
|
|
298
492
|
try {
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// scripts/entity-dedupe.mjs — 实体名字碎片的体检与修复(docs/pending-work.md §14.2 P7)
|
|
3
|
+
//
|
|
4
|
+
// 规划与执行都在 lib/entity-repair.js 里——启动自愈、诊断页的修复按钮和本脚本共用同一套
|
|
5
|
+
// 判据,不存在"脚本会合、程序不会合"这种分叉。这里只是命令行外壳。
|
|
6
|
+
//
|
|
7
|
+
// 两类结论,处理方式不同:
|
|
8
|
+
// · attested(可证明)——片段首尾重叠重建出的完整名在正文里出现过,或短名从未独立于长名出现。
|
|
9
|
+
// · similar(只是相似)——编辑距离 / 名字核撞上来的两个称呼。默认**不动**,要 `--include-similar`
|
|
10
|
+
// 且逐条打印后仍由你决定;批量自动跑永远只碰 attested。
|
|
11
|
+
//
|
|
12
|
+
// 从不删除记忆:合并只改指向,条目正文一字不动(硬规则 5)。孤立节点只列出来。
|
|
13
|
+
//
|
|
14
|
+
// 用法:
|
|
15
|
+
// node scripts/entity-dedupe.mjs # 全部库体检(默认 dry-run,零写入)
|
|
16
|
+
// node scripts/entity-dedupe.mjs --library <id> # 只看一个库
|
|
17
|
+
// node scripts/entity-dedupe.mjs --apply # 先图备份,再合并 attested 组
|
|
18
|
+
// node scripts/entity-dedupe.mjs --apply --include-similar
|
|
19
|
+
// DSH_MEMU_HOME=<dsh home>/dsh-memu-memory node scripts/entity-dedupe.mjs
|
|
20
|
+
import process from 'node:process'
|
|
21
|
+
import { pathToFileURL } from 'node:url'
|
|
22
|
+
|
|
23
|
+
function parseArgs(argv) {
|
|
24
|
+
const options = { apply: false, library: null, help: false, includeSimilar: false }
|
|
25
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
26
|
+
const arg = argv[i]
|
|
27
|
+
if (arg === '--apply') options.apply = true
|
|
28
|
+
else if (arg === '--include-similar') options.includeSimilar = true
|
|
29
|
+
else if (arg === '--library') options.library = String(argv[++i] || '')
|
|
30
|
+
else if (arg.startsWith('--library=')) options.library = arg.slice('--library='.length)
|
|
31
|
+
else if (arg === '--help' || arg === '-h') options.help = true
|
|
32
|
+
else throw new Error('unknown argument: ' + arg + ' (see --help)')
|
|
33
|
+
}
|
|
34
|
+
return options
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async function main() {
|
|
38
|
+
const options = parseArgs(process.argv.slice(2))
|
|
39
|
+
if (options.help) {
|
|
40
|
+
console.log('usage: node scripts/entity-dedupe.mjs [--library <id>] [--apply] [--include-similar]')
|
|
41
|
+
console.log('dry-run by default; --apply takes one graph backup first, then merges the provable groups')
|
|
42
|
+
return
|
|
43
|
+
}
|
|
44
|
+
const state = await import('../lib/state.js')
|
|
45
|
+
const repair = await import('../lib/entity-repair.js')
|
|
46
|
+
const backup = await import('../lib/backup.js')
|
|
47
|
+
|
|
48
|
+
if (options.library && !state.getLibrary(String(options.library))) throw new Error('library not found: ' + options.library)
|
|
49
|
+
const plan = repair.planAllRepairs({
|
|
50
|
+
listLibraries: state.listLibraries,
|
|
51
|
+
loadPrivateIndex: state.loadPrivateIndex,
|
|
52
|
+
libraryId: options.library || null,
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
for (const entry of plan.plans) {
|
|
56
|
+
if (!entry.groups.length && !entry.ambiguous.length) continue
|
|
57
|
+
console.log(`\n[${entry.libraryId}] — ${entry.scanned} 个实体,${entry.groups.length} 组可证明,${entry.ambiguous.length} 组只是相似`
|
|
58
|
+
+ (entry.partial ? '(正文只扫了最近 3000 条)' : ''))
|
|
59
|
+
for (const line of repair.summarizePlan({ groups: entry.groups })) console.log(' · ' + line)
|
|
60
|
+
for (const line of repair.summarizePlan({ groups: entry.ambiguous })) console.log(' ? ' + line + ' ← 需人工确认')
|
|
61
|
+
}
|
|
62
|
+
for (const failure of plan.failures) console.log(` ! [${failure.libraryId}] 规划失败:${failure.error}`)
|
|
63
|
+
|
|
64
|
+
if (!plan.attested && !plan.similar) {
|
|
65
|
+
console.log('\n没有需要处理的实体组。')
|
|
66
|
+
return
|
|
67
|
+
}
|
|
68
|
+
if (!options.apply) {
|
|
69
|
+
console.log(`\ndry-run:可自动合并 ${plan.attested} 组,相似待确认 ${plan.similar} 组。确认无误后加 --apply(会先做一次图备份)`)
|
|
70
|
+
return
|
|
71
|
+
}
|
|
72
|
+
const groups = []
|
|
73
|
+
for (const entry of plan.plans) {
|
|
74
|
+
const picked = options.includeSimilar ? [...entry.groups, ...entry.ambiguous] : entry.groups
|
|
75
|
+
if (picked.length) groups.push({ libraryId: entry.libraryId, picked })
|
|
76
|
+
}
|
|
77
|
+
if (!groups.length) {
|
|
78
|
+
console.log('\n没有可自动合并的组;相似候选请加 --include-similar 或到关系图谱里逐个确认。')
|
|
79
|
+
return
|
|
80
|
+
}
|
|
81
|
+
// 备份在任何写之前:合并只改指向,但撤销要靠这份快照。
|
|
82
|
+
const created = await backup.createBackup({ label: 'entity-dedupe' })
|
|
83
|
+
console.log('\n图备份:' + (created?.name || created?.dir || JSON.stringify(created)))
|
|
84
|
+
|
|
85
|
+
let merged = 0
|
|
86
|
+
let made = 0
|
|
87
|
+
let protectedRows = 0
|
|
88
|
+
const failures = []
|
|
89
|
+
for (const entry of groups) {
|
|
90
|
+
const result = repair.applyRepairGroups(entry.libraryId, entry.picked)
|
|
91
|
+
merged += result.merged
|
|
92
|
+
made += result.created
|
|
93
|
+
protectedRows += result.restored
|
|
94
|
+
for (const failure of result.failures) failures.push(`[${entry.libraryId}] ${failure.group}: ${failure.error}`)
|
|
95
|
+
}
|
|
96
|
+
console.log(`\n已合并 ${merged} 组(新建完整名 ${made} 个,写回被清扫误伤的实体 ${protectedRows} 个)。`)
|
|
97
|
+
console.log('下一步重算确定性边:node scripts/rebuild-auto-links.mjs --apply')
|
|
98
|
+
if (failures.length) {
|
|
99
|
+
console.log('\n失败明细:')
|
|
100
|
+
for (const line of failures) console.log(' ! ' + line)
|
|
101
|
+
process.exitCode = 1
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
|
106
|
+
main().catch((error) => {
|
|
107
|
+
console.error('entity-dedupe failed: ' + (error && error.stack ? error.stack : error))
|
|
108
|
+
process.exitCode = 1
|
|
109
|
+
})
|
|
110
|
+
}
|
|
@@ -31,8 +31,8 @@ function entry(title, content, extra = {}) {
|
|
|
31
31
|
|
|
32
32
|
function fixture() {
|
|
33
33
|
return {
|
|
34
|
-
'mem-1': entry('
|
|
35
|
-
'mem-2': entry('
|
|
34
|
+
'mem-1': entry('「小檀」今天来了', '她带着伞。'),
|
|
35
|
+
'mem-2': entry('小檀的伞', '小檀把伞放在门口。'),
|
|
36
36
|
'mem-3': entry('她叫阿兰', '阿兰是隔壁的邻居。'),
|
|
37
37
|
'mem-4': entry('yuike_01 上线', 'yuike_01 又迟到了。'),
|
|
38
38
|
'mem-5': entry('星港夜班', '今日星港开始检修,傍晚星港的风很大。'),
|
|
@@ -46,8 +46,8 @@ function fixture() {
|
|
|
46
46
|
test('term statistics carry df/titleDf/boundary/cohesion for real names and drop glue bigrams', () => {
|
|
47
47
|
const rows = Object.entries(fixture()).map(([id, item]) => ({ id, title: item.title, text: `${item.title}\n${item.content}` }))
|
|
48
48
|
const stats = terminology.buildTermStats(rows)
|
|
49
|
-
const you = stats.get('
|
|
50
|
-
assert.ok(you, '
|
|
49
|
+
const you = stats.get('小檀')
|
|
50
|
+
assert.ok(you, '小檀 is a repeated bigram')
|
|
51
51
|
assert.equal(you.df, 2)
|
|
52
52
|
assert.equal(you.titleDf, 2)
|
|
53
53
|
assert.ok(you.avLeft >= 2 && you.avRight >= 2, 'both boundaries vary: ' + you.avLeft + '/' + you.avRight)
|
|
@@ -58,7 +58,7 @@ test('term statistics carry df/titleDf/boundary/cohesion for real names and drop
|
|
|
58
58
|
assert.ok(star && star.df >= 2 && star.tf >= 4)
|
|
59
59
|
assert.equal(terminology.looksGeneric('这个'), true, 'function characters disqualify a surface')
|
|
60
60
|
assert.equal(terminology.looksGeneric('一条'), true, 'measure-word templates disqualify a surface')
|
|
61
|
-
assert.equal(terminology.looksGeneric('
|
|
61
|
+
assert.equal(terminology.looksGeneric('小檀'), false)
|
|
62
62
|
})
|
|
63
63
|
|
|
64
64
|
test('ranking puts evidenced terms in tier 1 and pure statistics in tier 2', () => {
|
|
@@ -67,13 +67,26 @@ test('ranking puts evidenced terms in tier 1 and pure statistics in tier 2', ()
|
|
|
67
67
|
const ranked = terminology.rankCandidates(entries, stats)
|
|
68
68
|
const tier1 = ranked.tier1.map(row => row.term)
|
|
69
69
|
const tier2 = ranked.tier2.map(row => row.term)
|
|
70
|
-
assert.ok(tier1.includes('
|
|
70
|
+
assert.ok(tier1.includes('小檀'), 'quoted name is tier 1: ' + tier1.join(','))
|
|
71
71
|
assert.ok(tier1.includes('阿兰'), 'name-intro is tier 1: ' + tier1.join(','))
|
|
72
72
|
assert.ok(tier1.includes('yuike_01'), 'mixed identifier is tier 1: ' + tier1.join(','))
|
|
73
73
|
assert.ok(tier2.includes('星港'), 'statistical-only name stays tier 2: ' + tier2.join(','))
|
|
74
74
|
assert.equal([...tier1, ...tier2].includes('这个'), false, 'glue bigram never becomes a candidate')
|
|
75
75
|
assert.equal([...tier1, ...tier2].includes('2026'), false)
|
|
76
|
-
assert.ok(ranked.tier1.find(row => row.term === '
|
|
76
|
+
assert.ok(ranked.tier1.find(row => row.term === '小檀').quoted)
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
test('overlapping window fragments of a longer name never become candidates', () => {
|
|
80
|
+
const entries = [
|
|
81
|
+
{ id: 'm1', title: '南宫晚晴的午后', text: '南宫晚晴的午后\n南宫晚晴坐在窗边讲这件事。' },
|
|
82
|
+
{ id: 'm2', title: '和南宫晚晴一起', text: '和南宫晚晴一起\n南宫晚晴先到了门口。' },
|
|
83
|
+
]
|
|
84
|
+
const stats = terminology.buildTermStats(entries)
|
|
85
|
+
const ranked = terminology.rankCandidates(entries, stats)
|
|
86
|
+
const terms = [...ranked.tier1, ...ranked.tier2].map(row => row.term)
|
|
87
|
+
assert.ok(terms.includes('南宫晚晴'), 'the whole surface stays nominated: ' + terms.join(','))
|
|
88
|
+
assert.equal(terms.includes('南宫晚'), false, 'a window of a longer name is not a person')
|
|
89
|
+
assert.equal(terms.includes('宫晚晴'), false, 'the other window is not a second person either')
|
|
77
90
|
})
|
|
78
91
|
|
|
79
92
|
test('the queue snapshot persists, maps existing entities and stays stable across reads', () => {
|
|
@@ -212,10 +225,10 @@ test('custom candidate policy invalidates both cache layers for that library', a
|
|
|
212
225
|
const lib = library('policy-cache')
|
|
213
226
|
state.savePrivateIndex(lib, { version: 1, entries: fixture() })
|
|
214
227
|
const normal = candidates.generateAnchorCandidates({ libraryId: lib, now: 1 })
|
|
215
|
-
assert.equal(normal.tiers.tier1.find(row => row.term === '
|
|
228
|
+
assert.equal(normal.tiers.tier1.find(row => row.term === '小檀')?.df, 2)
|
|
216
229
|
const strictPolicy = { ...AUTOLINK_POLICY_V3, minDf: 3 }
|
|
217
230
|
const strict = candidates.generateAnchorCandidates({ libraryId: lib, now: 2, policy: strictPolicy })
|
|
218
|
-
assert.equal(strict.tiers.tier1.some(row => row.term === '
|
|
231
|
+
assert.equal(strict.tiers.tier1.some(row => row.term === '小檀'), false)
|
|
219
232
|
candidates.resetAnchorCandidatesCache()
|
|
220
233
|
assert.deepEqual(candidates.generateAnchorCandidates({ libraryId: lib, now: 3, policy: { ...strictPolicy } }), strict)
|
|
221
234
|
candidates.resetAnchorCandidatesCache()
|