@onigeya/dsh-kanabo-memory 0.0.0-stage → 0.1.1
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/backfill-child.js +310 -0
- package/backfill-vectors.js +125 -0
- package/contract.js +114 -0
- package/cordis.patch.yml +7 -0
- package/cursor.js +116 -0
- package/dedupe.js +101 -0
- package/egress.js +790 -0
- package/embed-child.js +152 -0
- package/embed.js +475 -0
- package/envelope.js +368 -0
- package/extract.js +420 -0
- package/idle-interrupt.js +922 -0
- package/index.js +154 -0
- package/migrate.js +1723 -0
- package/package.json +68 -3
- package/persona.js +949 -0
- package/providers.js +786 -0
- package/query-window.js +562 -0
- package/recall-stages.js +150 -0
- package/recall-trivium.js +916 -0
- package/recall.js +129 -0
- package/remove-legacy.mjs +720 -0
- package/retention-sweep.js +124 -0
- package/serve.js +138 -0
- package/store-trivium-child.js +447 -0
- package/store-trivium.js +510 -0
- package/store.js +347 -0
- package/sweep.js +296 -0
- package/tools.js +507 -0
- package/trivium-runtime.js +1900 -0
- package/README.md +0 -3
|
@@ -0,0 +1,720 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* 删除 legacy JSON 记忆库(checkbox 43)。
|
|
4
|
+
*
|
|
5
|
+
* 顺序不可颠倒:**先证明 40/41/42/16 绿,再删**。任一前置为红,本脚本只写一份
|
|
6
|
+
* `status:"refused"` 的审计并退出非零——legacy JSON 原样留在磁盘上。
|
|
7
|
+
*
|
|
8
|
+
* 删除对象只有 JSON 记忆文档:`<store-dir>/*.json` 中形状为 `{version:1, entries:[…]}`
|
|
9
|
+
* 的顶层文件。`trivium/` 子目录、`.tdb`/`.wal` 等引擎文件、迁移状态与审计本身**永不触碰**。
|
|
10
|
+
*
|
|
11
|
+
* 用法:
|
|
12
|
+
* node remove-legacy.mjs --store-dir <dir> [--dry-run] [--audit <path>] [--repo <root>]
|
|
13
|
+
* [--trivium-dir <dir>] [--fast] [--timeout <ms>]
|
|
14
|
+
*
|
|
15
|
+
* 审计(默认 `<store-dir>/legacy-removal-audit.json`)记录每个被删文件的
|
|
16
|
+
* `bytes`/`sha256`、删除前条目数、前置信号、TriviumDB 库存与 `verifyDigest`。
|
|
17
|
+
* 审计文件永不删除,任何一次运行都会覆盖写入最近一次结论(含 refused/dry-run)。
|
|
18
|
+
*/
|
|
19
|
+
import { createHash } from 'node:crypto'
|
|
20
|
+
import { spawnSync } from 'node:child_process'
|
|
21
|
+
import {
|
|
22
|
+
cpSync,
|
|
23
|
+
existsSync,
|
|
24
|
+
mkdirSync,
|
|
25
|
+
mkdtempSync,
|
|
26
|
+
readFileSync,
|
|
27
|
+
readdirSync,
|
|
28
|
+
realpathSync,
|
|
29
|
+
renameSync,
|
|
30
|
+
rmSync,
|
|
31
|
+
unlinkSync,
|
|
32
|
+
writeFileSync,
|
|
33
|
+
} from 'node:fs'
|
|
34
|
+
import { dirname, isAbsolute, join, relative, resolve } from 'node:path'
|
|
35
|
+
import { tmpdir } from 'node:os'
|
|
36
|
+
import { fileURLToPath } from 'node:url'
|
|
37
|
+
|
|
38
|
+
import { EMBEDDING_DIM } from './contract.js'
|
|
39
|
+
|
|
40
|
+
const HERE = dirname(fileURLToPath(import.meta.url))
|
|
41
|
+
const DEFAULT_REPO = resolve(HERE, '..', '..')
|
|
42
|
+
|
|
43
|
+
/** 迁移/游标/扫描状态与审计都不是记忆文档,绝不当 legacy store 删掉。 */
|
|
44
|
+
export const NON_STORE_JSON = Object.freeze(['migration-state.json', 'cursors.json', 'sweep-state.json'])
|
|
45
|
+
export const AUDIT_FILE = 'legacy-removal-audit.json'
|
|
46
|
+
const REQUIRED_DOCS = ['architecture.md', 'envelope.md', 'migration.md', 'troubleshooting.md']
|
|
47
|
+
|
|
48
|
+
const sha256 = (bytes) => createHash('sha256').update(bytes).digest('hex')
|
|
49
|
+
|
|
50
|
+
function readJsonSafe(file) {
|
|
51
|
+
try {
|
|
52
|
+
return { ok: true, value: JSON.parse(readFileSync(file, 'utf8')) }
|
|
53
|
+
} catch (error) {
|
|
54
|
+
return { ok: false, error: error?.code ?? error?.message ?? String(error) }
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** legacy 记忆文档的形状(`store.js` 的 `readStore` 契约):`version:1` + `entries` 数组。 */
|
|
59
|
+
export function isLegacyStoreDoc(value) {
|
|
60
|
+
return value !== null && typeof value === 'object' && value.version === 1 && Array.isArray(value.entries)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** 条目 id:`addEntry` 总会写一个非空字符串 id;缺 id/类型不对的条目无法与库节点对齐,单独计数。 */
|
|
64
|
+
function entryIdOf(entry) {
|
|
65
|
+
return typeof entry?.id === 'string' && entry.id.length > 0 ? entry.id : null
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function walk(dir, out = []) {
|
|
69
|
+
if (!existsSync(dir)) return out
|
|
70
|
+
for (const name of readdirSync(dir, { withFileTypes: true })) {
|
|
71
|
+
const full = join(dir, name.name)
|
|
72
|
+
if (name.isDirectory()) {
|
|
73
|
+
if (name.name !== 'node_modules' && name.name !== '.git') walk(full, out)
|
|
74
|
+
} else if (name.isFile()) {
|
|
75
|
+
out.push(full)
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return out
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** 文件库存:相对路径 → 字节数与 sha256。用于证明 TriviumDB 在删除前后逐字节不变。 */
|
|
82
|
+
export function inventory(dir, base = dir) {
|
|
83
|
+
const map = {}
|
|
84
|
+
for (const file of walk(dir)) {
|
|
85
|
+
const bytes = readFileSync(file)
|
|
86
|
+
map[relative(base, file)] = { bytes: bytes.length, sha256: sha256(bytes) }
|
|
87
|
+
}
|
|
88
|
+
return map
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function inventoryDigest(map) {
|
|
92
|
+
const keys = Object.keys(map).sort()
|
|
93
|
+
return sha256(keys.map((key) => `${key}\u0000${map[key].bytes}\u0000${map[key].sha256}`).join('\n'))
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/* ------------------------------------------------------------------ probes */
|
|
97
|
+
|
|
98
|
+
function lastResultLine(text) {
|
|
99
|
+
const lines = String(text).split('\n').map((line) => line.trim()).filter((line) => line.length > 0)
|
|
100
|
+
for (let index = lines.length - 1; index >= 0; index -= 1) {
|
|
101
|
+
if (/^RESULT:\s*(PASS|FAIL)$/.test(lines[index])) return lines[index]
|
|
102
|
+
}
|
|
103
|
+
return ''
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** 有界跑一个探针:超时即 FAIL(绝不挂住)。stdout/stderr 截断保存,便于审计。 */
|
|
107
|
+
export function runProbe({ name, script, repoRoot, timeoutMs, env = {} }) {
|
|
108
|
+
const startedAt = Date.now()
|
|
109
|
+
const result = spawnSync(process.execPath, [script], {
|
|
110
|
+
cwd: repoRoot,
|
|
111
|
+
timeout: timeoutMs,
|
|
112
|
+
encoding: 'utf8',
|
|
113
|
+
maxBuffer: 8 * 1024 * 1024,
|
|
114
|
+
env: { ...process.env, ...env },
|
|
115
|
+
})
|
|
116
|
+
const stdout = result.stdout ?? ''
|
|
117
|
+
const stderr = result.stderr ?? ''
|
|
118
|
+
const timedOut = result.error?.code === 'ETIMEDOUT' || result.signal === 'SIGTERM'
|
|
119
|
+
const line = lastResultLine(stdout)
|
|
120
|
+
const ok = !timedOut && result.status === 0 && line === 'RESULT: PASS'
|
|
121
|
+
const observable = timedOut
|
|
122
|
+
? `${name} TIMEOUT after ${timeoutMs}ms`
|
|
123
|
+
: `${name} exit=${result.status} ${line || 'no RESULT line'} elapsed=${Date.now() - startedAt}ms${ok ? '' : ` tail=${JSON.stringify((stdout + stderr).trim().slice(-400))}`}`
|
|
124
|
+
return { name, ok, observable, stdout, stderr, timedOut }
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** 40:真实跑 e2e 长视频验收,只认 `RESULT: PASS`。 */
|
|
128
|
+
function e2eSignal({ repoRoot, timeoutMs, fast, workDir }) {
|
|
129
|
+
const script = join(repoRoot, 'scripts', 'e2e-longvideo.mjs')
|
|
130
|
+
if (!existsSync(script)) return { name: 'e2e', ok: false, observable: `missing ${script}` }
|
|
131
|
+
const env = {
|
|
132
|
+
KANABO_MEMORY_DIR: join(workDir, 'e2e-memory'),
|
|
133
|
+
KANABO_TRIVIUM_DIR: join(workDir, 'e2e-memory', 'trivium'),
|
|
134
|
+
}
|
|
135
|
+
if (fast) env.KANABO_E2E_WINDOW_SECONDS = '12'
|
|
136
|
+
const probe = runProbe({ name: 'e2e', script, repoRoot, timeoutMs, env })
|
|
137
|
+
return { name: 'e2e', ok: probe.ok, observable: probe.observable }
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** 41:真实跑回归矩阵,要求全行变绿且末尾 `RESULT: PASS`。 */
|
|
141
|
+
function regressionSignal({ repoRoot, timeoutMs, workDir }) {
|
|
142
|
+
const script = join(repoRoot, 'scripts', 'regression-matrix.mjs')
|
|
143
|
+
if (!existsSync(script)) return { name: 'regression', ok: false, observable: `missing ${script}` }
|
|
144
|
+
const probe = runProbe({
|
|
145
|
+
name: 'regression',
|
|
146
|
+
script,
|
|
147
|
+
repoRoot,
|
|
148
|
+
timeoutMs,
|
|
149
|
+
env: { KANABO_MEMORY_DIR: join(workDir, 'rows-memory') },
|
|
150
|
+
})
|
|
151
|
+
const rows = /rows=(\d+)\s+pass=(\d+)/.exec(probe.stdout)
|
|
152
|
+
const allRows = rows !== null && Number(rows[1]) === 14 && Number(rows[2]) === 14
|
|
153
|
+
return {
|
|
154
|
+
name: 'regression',
|
|
155
|
+
ok: probe.ok && allRows,
|
|
156
|
+
observable: `${probe.observable}; rows=${rows === null ? 'n/a' : `${rows[1]}/${rows[2]}`}`,
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** 42:README 的已知限制一节 + 四份 docs 存在。 */
|
|
161
|
+
export function docsSignal({ repoRoot }) {
|
|
162
|
+
const missing = []
|
|
163
|
+
const readme = join(repoRoot, 'README.md')
|
|
164
|
+
if (!existsSync(readme)) missing.push('README.md')
|
|
165
|
+
else if (!/已知限制|Known limits|KNOWN LIMITS/.test(readFileSync(readme, 'utf8'))) missing.push('README.md#known-limits')
|
|
166
|
+
for (const doc of REQUIRED_DOCS) if (!existsSync(join(repoRoot, 'docs', doc))) missing.push(`docs/${doc}`)
|
|
167
|
+
return {
|
|
168
|
+
name: 'docs',
|
|
169
|
+
ok: missing.length === 0,
|
|
170
|
+
observable: missing.length === 0 ? `README known-limits + ${REQUIRED_DOCS.length} docs present` : `missing ${missing.join(', ')}`,
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** 16:迁移已验证——状态文件说 verified,或磁盘上已有 `.tdb`。 */
|
|
175
|
+
export function migrationSignal({ storeDir, triviumDir }) {
|
|
176
|
+
const stateFile = join(storeDir, 'migration-state.json')
|
|
177
|
+
if (existsSync(stateFile)) {
|
|
178
|
+
const state = readJsonSafe(stateFile)
|
|
179
|
+
if (!state.ok) return { name: 'migration', ok: false, observable: `migration-state.json unreadable: ${state.error}`, state: null }
|
|
180
|
+
const status = state.value?.migration?.status
|
|
181
|
+
return {
|
|
182
|
+
name: 'migration',
|
|
183
|
+
ok: status === 'verified',
|
|
184
|
+
observable: `migration-state.json migration.status=${String(status)} authoritative=${String(state.value?.authoritative)}`,
|
|
185
|
+
state: state.value,
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
if (!existsSync(triviumDir)) {
|
|
189
|
+
return { name: 'migration', ok: false, observable: `no migration-state.json and no ${triviumDir}`, state: null }
|
|
190
|
+
}
|
|
191
|
+
const tdb = walk(triviumDir).filter((file) => file.endsWith('.tdb'))
|
|
192
|
+
return {
|
|
193
|
+
name: 'migration',
|
|
194
|
+
ok: tdb.length > 0,
|
|
195
|
+
observable: `no migration-state.json; TriviumDB files present: ${tdb.length} (${tdb.map((file) => relative(triviumDir, file)).join(', ')})`,
|
|
196
|
+
state: null,
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/* ------------------------------------------------------------------ code scan */
|
|
201
|
+
|
|
202
|
+
const STORE_MODULE = join('packages', 'dsh-kanabo-memory', 'store.js')
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* 找出仍在读写 legacy JSON 的调用点。写入必须在 TriviumDB 里发生:
|
|
206
|
+
* `packages/` 下**除 `store.js` 自身之外**的 `writeStore(` 调用即视为「JSON 仍是写入目标」,前置判红。
|
|
207
|
+
* `store.js` 里的同名字段是模块内部实现(内存镜像 / 降级回退),`scripts/` 下是开发探针——只记入 remaining 列表。
|
|
208
|
+
*/
|
|
209
|
+
export function scanLegacyCallers({ repoRoot }) {
|
|
210
|
+
// realpath:仓库常以 symlink 形式出现在 /tmp,否则本模块会把自己源码里的 `writeStore(` 计成调用点。
|
|
211
|
+
const self = realpathSync(fileURLToPath(import.meta.url))
|
|
212
|
+
const writers = []
|
|
213
|
+
const readers = []
|
|
214
|
+
for (const file of [...walk(join(repoRoot, 'packages')), ...walk(join(repoRoot, 'scripts'))]) {
|
|
215
|
+
if (!/\.(mjs|js|cjs)$/.test(file)) continue
|
|
216
|
+
let real
|
|
217
|
+
try {
|
|
218
|
+
real = realpathSync(file)
|
|
219
|
+
} catch {
|
|
220
|
+
continue
|
|
221
|
+
}
|
|
222
|
+
if (real === self) continue
|
|
223
|
+
const text = readFileSync(file, 'utf8')
|
|
224
|
+
const rel = relative(repoRoot, file)
|
|
225
|
+
text.split('\n').forEach((line, index) => {
|
|
226
|
+
if (/\bwriteStore\s*\(/.test(line) && !/export function writeStore/.test(line)) {
|
|
227
|
+
writers.push({ file: rel, line: index + 1 })
|
|
228
|
+
}
|
|
229
|
+
if (/\breadStore\s*\(/.test(line) && !/export function readStore/.test(line)) {
|
|
230
|
+
readers.push({ file: rel, line: index + 1 })
|
|
231
|
+
}
|
|
232
|
+
})
|
|
233
|
+
}
|
|
234
|
+
const isModuleInternal = (hit) => hit.file === STORE_MODULE
|
|
235
|
+
const liveWriters = writers.filter((hit) => !isModuleInternal(hit) && hit.file.startsWith('packages'))
|
|
236
|
+
const remaining = [...writers, ...readers].filter((hit) => !isModuleInternal(hit))
|
|
237
|
+
return {
|
|
238
|
+
name: 'writes',
|
|
239
|
+
ok: liveWriters.length === 0,
|
|
240
|
+
observable: liveWriters.length === 0
|
|
241
|
+
? `no packages/ JSON writer outside ${STORE_MODULE}; remaining refs: ${remaining.map((hit) => `${hit.file}:${hit.line}`).join(', ') || 'none'}`
|
|
242
|
+
: `legacy JSON still written in packages/: ${liveWriters.map((hit) => `${hit.file}:${hit.line}`).join(', ')}`,
|
|
243
|
+
writers,
|
|
244
|
+
readers,
|
|
245
|
+
remaining,
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/* ------------------------------------------------------------------ gating */
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* 断言 40/41/42/16 + 写路径全部为绿。
|
|
253
|
+
* @returns {{ok:boolean, results:Array<{name:string,ok:boolean,observable:string}>}}
|
|
254
|
+
*/
|
|
255
|
+
export function assertPreconditions({
|
|
256
|
+
repoRoot = DEFAULT_REPO,
|
|
257
|
+
storeDir,
|
|
258
|
+
triviumDir = storeDir ? resolve(storeDir, 'trivium') : undefined,
|
|
259
|
+
timeoutMs = 600_000,
|
|
260
|
+
fast = false,
|
|
261
|
+
workDir,
|
|
262
|
+
} = {}) {
|
|
263
|
+
const scratch = workDir ?? mkdtempSync(join(tmpdir(), 'kanabo-remove-legacy-pre-'))
|
|
264
|
+
const probes = [
|
|
265
|
+
e2eSignal({ repoRoot, timeoutMs, fast, workDir: scratch }),
|
|
266
|
+
regressionSignal({ repoRoot, timeoutMs, workDir: scratch }),
|
|
267
|
+
docsSignal({ repoRoot }),
|
|
268
|
+
migrationSignal({ storeDir, triviumDir }),
|
|
269
|
+
scanLegacyCallers({ repoRoot }),
|
|
270
|
+
]
|
|
271
|
+
if (workDir === undefined) rmSync(scratch, { recursive: true, force: true })
|
|
272
|
+
return { ok: probes.every((probe) => probe.ok), results: probes }
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/* ------------------------------------------------------------------ removal */
|
|
276
|
+
|
|
277
|
+
function listLegacyDocs({ storeDir, auditPath }) {
|
|
278
|
+
const docs = []
|
|
279
|
+
const skipped = []
|
|
280
|
+
if (!existsSync(storeDir)) return { docs, skipped }
|
|
281
|
+
for (const name of readdirSync(storeDir, { withFileTypes: true })) {
|
|
282
|
+
if (!name.isFile() || !name.name.endsWith('.json')) continue
|
|
283
|
+
const file = join(storeDir, name.name)
|
|
284
|
+
if (auditPath !== undefined && resolve(file) === resolve(auditPath)) continue
|
|
285
|
+
if (NON_STORE_JSON.includes(name.name)) {
|
|
286
|
+
skipped.push({ path: file, reason: 'non-store state file' })
|
|
287
|
+
continue
|
|
288
|
+
}
|
|
289
|
+
const parsed = readJsonSafe(file)
|
|
290
|
+
if (!parsed.ok) {
|
|
291
|
+
skipped.push({ path: file, reason: `unparseable JSON: ${parsed.error}` })
|
|
292
|
+
continue
|
|
293
|
+
}
|
|
294
|
+
if (!isLegacyStoreDoc(parsed.value)) {
|
|
295
|
+
skipped.push({ path: file, reason: 'not a legacy store doc (shape mismatch)' })
|
|
296
|
+
continue
|
|
297
|
+
}
|
|
298
|
+
const bytes = readFileSync(file)
|
|
299
|
+
docs.push({
|
|
300
|
+
path: file,
|
|
301
|
+
bytes: bytes.length,
|
|
302
|
+
sha256: sha256(bytes),
|
|
303
|
+
scope: typeof parsed.value.scope === 'string' ? parsed.value.scope : null,
|
|
304
|
+
entries: parsed.value.entries.length,
|
|
305
|
+
entryIds: parsed.value.entries.map(entryIdOf).filter((id) => id !== null),
|
|
306
|
+
})
|
|
307
|
+
}
|
|
308
|
+
return { docs, skipped }
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function writeAudit(auditPath, record) {
|
|
312
|
+
mkdirSync(dirname(auditPath), { recursive: true })
|
|
313
|
+
const tmp = `${auditPath}.${process.pid}.tmp`
|
|
314
|
+
writeFileSync(tmp, `${JSON.stringify(record, null, 2)}\n`, { mode: 0o600 })
|
|
315
|
+
renameSync(tmp, auditPath)
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function guardPath(label, dir) {
|
|
319
|
+
const abs = resolve(dir)
|
|
320
|
+
if (abs.includes(`${resolve(process.env.HOME ?? '~')}/.dsh`)) throw new Error(`${label} 不得位于 ~/.dsh:${abs}`)
|
|
321
|
+
if (abs === '/' || abs === '') throw new Error(`${label} 不得是文件系统根:${abs}`)
|
|
322
|
+
if (!isAbsolute(abs)) throw new Error(`${label} 需要绝对路径:${abs}`)
|
|
323
|
+
return abs
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* 无迁移状态时的**真比对**:把即将删除的 JSON 条目与库内节点逐条对齐(条数 + id 集合)。
|
|
328
|
+
*
|
|
329
|
+
* 旧的 `migrationSignal` 在 `migration-state.json` 缺失时只要看到任意 `.tdb` 就把迁移当「已验证」,
|
|
330
|
+
* 于是 JSON 有、库里没有的条目会被直接删掉而无人察觉。这里补上任务 43 要求的「确认迁移」:
|
|
331
|
+
* 每个 workspace 一个门面,在**库的副本**上 `open()` 后按 `kind:kanabo-envelope` + `workspace` 读出
|
|
332
|
+
* 节点 payload 的 `legacy_id`(与 `migrate.js` 的 `readWorkspaceRows`/`payloadFor` 同一口径),
|
|
333
|
+
* 与 JSON 文档的 `scope` 配成对,比对条数与 id 集合。任一 workspace 不一致即整体拒删。
|
|
334
|
+
*
|
|
335
|
+
* 返回 `{name:'migration-compare', ok, observable, workspaces, missing, extra}`;调用方只在
|
|
336
|
+
* 无迁移状态时用它取代 `migrationSignal` 的 `.tdb` 存在性判据。只在副本上开库(同 `openTrivium`),
|
|
337
|
+
* 真实库逐字节不变。
|
|
338
|
+
*/
|
|
339
|
+
export async function compareLegacyAgainstTrivium({ docs, triviumDir }) {
|
|
340
|
+
const workspaces = []
|
|
341
|
+
const missing = []
|
|
342
|
+
const extra = []
|
|
343
|
+
const errors = []
|
|
344
|
+
for (const doc of docs) {
|
|
345
|
+
const scope = doc.scope ?? 'unknown-workspace'
|
|
346
|
+
const jsonIds = doc.entryIds
|
|
347
|
+
let copy = null
|
|
348
|
+
let store = null
|
|
349
|
+
let dbIds = null
|
|
350
|
+
let nodeCount = null
|
|
351
|
+
try {
|
|
352
|
+
if (existsSync(triviumDir)) {
|
|
353
|
+
copy = mkdtempSync(join(tmpdir(), 'kanabo-trivium-compare-'))
|
|
354
|
+
cpSync(triviumDir, copy, { recursive: true })
|
|
355
|
+
}
|
|
356
|
+
const { createTriviumStore } = await import('./store-trivium.js')
|
|
357
|
+
store = createTriviumStore({ workspace: scope, dir: copy ?? triviumDir, dim: EMBEDDING_DIM, suffix: '', idleMs: 1000 })
|
|
358
|
+
const opened = await store.open({}, { timeoutMs: 20_000 })
|
|
359
|
+
nodeCount = opened?.nodeCount ?? null
|
|
360
|
+
const query = `FIND {kind: "kanabo-envelope", workspace: ${JSON.stringify(scope)}} RETURN *`
|
|
361
|
+
const result = await store.tql(query)
|
|
362
|
+
const rows = Array.isArray(result) ? result : (Array.isArray(result?.rows) ? result.rows : [])
|
|
363
|
+
const ids = new Set()
|
|
364
|
+
for (const row of rows) {
|
|
365
|
+
const node = row?._ ?? row
|
|
366
|
+
const legacyId = node?.payload?.legacy_id
|
|
367
|
+
if (typeof legacyId === 'string' && legacyId.length > 0) ids.add(legacyId)
|
|
368
|
+
}
|
|
369
|
+
dbIds = ids
|
|
370
|
+
} catch (error) {
|
|
371
|
+
errors.push({ scope, error: error?.message ?? String(error) })
|
|
372
|
+
} finally {
|
|
373
|
+
try {
|
|
374
|
+
await store?.close()
|
|
375
|
+
} catch {
|
|
376
|
+
// 关闭失败不影响已读到的结论。
|
|
377
|
+
}
|
|
378
|
+
if (copy !== null) rmSync(copy, { recursive: true, force: true })
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
const missingHere = dbIds === null ? [] : jsonIds.filter((id) => !dbIds.has(id))
|
|
382
|
+
const extraHere = dbIds === null ? [] : [...dbIds].filter((id) => !jsonIds.includes(id))
|
|
383
|
+
missing.push(...missingHere.map((id) => ({ scope, id })))
|
|
384
|
+
extra.push(...extraHere.map((id) => ({ scope, id })))
|
|
385
|
+
const countsDiffer = dbIds === null ? true : jsonIds.length !== dbIds.size
|
|
386
|
+
const idsDiverge = missingHere.length > 0 || extraHere.length > 0
|
|
387
|
+
workspaces.push({
|
|
388
|
+
scope,
|
|
389
|
+
file: doc.path,
|
|
390
|
+
jsonEntries: doc.entries,
|
|
391
|
+
jsonIds: jsonIds.length,
|
|
392
|
+
dbNodes: dbIds === null ? null : dbIds.size,
|
|
393
|
+
nodeCount,
|
|
394
|
+
ok: dbIds !== null && !countsDiffer && !idsDiverge,
|
|
395
|
+
missing: missingHere,
|
|
396
|
+
extra: extraHere,
|
|
397
|
+
})
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
const ok = errors.length === 0 && workspaces.length > 0 && workspaces.every((ws) => ws.ok)
|
|
401
|
+
const parts = workspaces.map((ws) =>
|
|
402
|
+
`${ws.scope}: JSON ${ws.jsonIds}/${ws.jsonEntries} 条 vs 库 ${ws.dbNodes ?? 'unreadable'} 节点`
|
|
403
|
+
+ (ws.ok ? '(一致)' : `(缺 ${ws.missing.length}、多 ${ws.extra.length})`))
|
|
404
|
+
const detail = errors.length > 0
|
|
405
|
+
? `打开库失败:${errors.map((row) => `${row.scope} → ${row.error}`).join('; ')}`
|
|
406
|
+
: (parts.join('; ') || '没有 legacy 文档可比对')
|
|
407
|
+
return {
|
|
408
|
+
name: 'migration-compare',
|
|
409
|
+
ok,
|
|
410
|
+
observable: detail,
|
|
411
|
+
workspaces,
|
|
412
|
+
missing,
|
|
413
|
+
extra,
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
function formatIdList(rows, limit = 10) {
|
|
418
|
+
if (rows.length === 0) return 'none'
|
|
419
|
+
const shown = rows.slice(0, limit).map((row) => `${row.scope}:${row.id}`)
|
|
420
|
+
return `${shown.join(', ')}${rows.length > limit ? ` …(+${rows.length - limit})` : ''}`
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* 前置绿后删除 legacy JSON,写审计;任何一项红则只写 `status:"refused"` 并保持文件原样。
|
|
425
|
+
*
|
|
426
|
+
* @returns {{ok:boolean, status:'removed'|'refused'|'dry-run', auditPath:string, audit:object}}
|
|
427
|
+
*/
|
|
428
|
+
export async function removeLegacy({
|
|
429
|
+
storeDir,
|
|
430
|
+
dryRun = false,
|
|
431
|
+
auditPath,
|
|
432
|
+
repoRoot = DEFAULT_REPO,
|
|
433
|
+
triviumDir,
|
|
434
|
+
fast = false,
|
|
435
|
+
timeoutMs = 600_000,
|
|
436
|
+
// 测试接缝:注入前缀结果(形状与 assertPreconditions 的返回值一致)。CLI 从不传它。
|
|
437
|
+
preconditions: injected,
|
|
438
|
+
} = {}) {
|
|
439
|
+
if (typeof storeDir !== 'string' || storeDir.length === 0) throw new Error('removeLegacy 需要 storeDir')
|
|
440
|
+
const dir = guardPath('storeDir', storeDir)
|
|
441
|
+
const trivium = guardPath('triviumDir', triviumDir ?? join(dir, 'trivium'))
|
|
442
|
+
const audit = auditPath === undefined ? join(dir, AUDIT_FILE) : guardPath('audit', auditPath)
|
|
443
|
+
|
|
444
|
+
const preconditions = injected ?? assertPreconditions({ repoRoot, storeDir: dir, triviumDir: trivium, timeoutMs, fast })
|
|
445
|
+
// 闸门只认逐项结果:顶层 ok 若和 results 矛盾,以 results 为准(注入/篡改都绕不过)。
|
|
446
|
+
const preconditionsOk = Array.isArray(preconditions.results)
|
|
447
|
+
&& preconditions.results.length >= 4
|
|
448
|
+
&& preconditions.results.every((probe) => probe.ok === true)
|
|
449
|
+
const scanned = listLegacyDocs({ storeDir: dir, auditPath: audit })
|
|
450
|
+
const base = {
|
|
451
|
+
removedAt: null,
|
|
452
|
+
attemptedAt: new Date().toISOString(),
|
|
453
|
+
storeDir: dir,
|
|
454
|
+
triviumDir: trivium,
|
|
455
|
+
repoRoot,
|
|
456
|
+
dryRun,
|
|
457
|
+
mode: fast ? 'fast' : 'full',
|
|
458
|
+
files: [],
|
|
459
|
+
entryCountsBefore: { files: 0, entries: 0 },
|
|
460
|
+
verifyDigest: null,
|
|
461
|
+
preconditions: Object.fromEntries(preconditions.results.map((probe) => [probe.name, { ok: probe.ok, observable: probe.observable }])),
|
|
462
|
+
skipped: scanned.skipped,
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
if (!preconditionsOk) {
|
|
466
|
+
const record = { ...base, status: 'refused', reason: 'preconditions red', preconditionsOk: false }
|
|
467
|
+
writeAudit(audit, record)
|
|
468
|
+
return { ok: false, status: 'refused', auditPath: audit, audit: record }
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
const stateFile = join(dir, 'migration-state.json')
|
|
472
|
+
const state = existsSync(stateFile) ? readJsonSafe(stateFile) : { ok: true, value: null }
|
|
473
|
+
if (!state.ok) {
|
|
474
|
+
const record = { ...base, status: 'refused', reason: `migration-state.json unreadable: ${state.error}`, preconditionsOk: true }
|
|
475
|
+
writeAudit(audit, record)
|
|
476
|
+
return { ok: false, status: 'refused', auditPath: audit, audit: record }
|
|
477
|
+
}
|
|
478
|
+
const dualWriteBefore = state.value === null ? null : state.value.dualWrite === true
|
|
479
|
+
const authorityBefore = state.value === null ? null : state.value.authoritative
|
|
480
|
+
if (authorityBefore === 'json') {
|
|
481
|
+
const record = {
|
|
482
|
+
...base,
|
|
483
|
+
status: 'refused',
|
|
484
|
+
reason: 'migration-state.json authoritative="json":JSON 仍是权威写入目标,删除会丢记忆',
|
|
485
|
+
preconditionsOk: true,
|
|
486
|
+
dualWrite: { before: dualWriteBefore, after: dualWriteBefore, action: 'none' },
|
|
487
|
+
}
|
|
488
|
+
writeAudit(audit, record)
|
|
489
|
+
return { ok: false, status: 'refused', auditPath: audit, audit: record }
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
// 无迁移状态(migration-state.json 缺失):`migrationSignal` 只证明了「有 .tdb」,不等于任务 43 的「确认迁移」。
|
|
493
|
+
// 删除前在库副本上逐条比对 JSON 条目与节点(条数 + id 集合),不一致即拒删(证据:task-62)。
|
|
494
|
+
let migrationCompare = null
|
|
495
|
+
if (state.value === null && scanned.docs.length > 0) {
|
|
496
|
+
migrationCompare = await compareLegacyAgainstTrivium({ docs: scanned.docs, triviumDir: trivium })
|
|
497
|
+
if (!migrationCompare.ok) {
|
|
498
|
+
const record = {
|
|
499
|
+
...base,
|
|
500
|
+
status: 'refused',
|
|
501
|
+
reason: `migration-state.json 缺失且逐条比对不通过:${migrationCompare.observable};`
|
|
502
|
+
+ `缺失 ${migrationCompare.missing.length} 个 id(${formatIdList(migrationCompare.missing)});`
|
|
503
|
+
+ `多出 ${migrationCompare.extra.length} 个 id(${formatIdList(migrationCompare.extra)})`,
|
|
504
|
+
preconditionsOk: true,
|
|
505
|
+
migrationCompare,
|
|
506
|
+
}
|
|
507
|
+
writeAudit(audit, record)
|
|
508
|
+
return { ok: false, status: 'refused', auditPath: audit, audit: record }
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
// dualWrite 只是 migration-state.json 里的一个记录标志,不是代码路径;确认权威已切到 trivium 后把它落成 false,
|
|
513
|
+
// 防止下一次双写把 legacy JSON 重新写出来。TriviumDB 文件不受影响。
|
|
514
|
+
let dualWriteAfter = dualWriteBefore
|
|
515
|
+
if (state.value !== null && dualWriteBefore === true) {
|
|
516
|
+
const next = { ...state.value, dualWrite: false, dualWriteAt: Date.now() }
|
|
517
|
+
if (typeof next.authoritative !== 'string') next.authoritative = 'trivium'
|
|
518
|
+
const tmp = `${stateFile}.${process.pid}.tmp`
|
|
519
|
+
writeFileSync(tmp, `${JSON.stringify(next, null, 2)}\n`, { mode: 0o600 })
|
|
520
|
+
if (!dryRun) renameSync(tmp, stateFile)
|
|
521
|
+
else rmSync(tmp, { force: true })
|
|
522
|
+
dualWriteAfter = false
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
const triviumBefore = existsSync(trivium) ? inventory(trivium) : {}
|
|
526
|
+
const files = scanned.docs.map(({ path, bytes, sha256: digest, scope, entries }) => ({ path, bytes, sha256: digest, scope, entries }))
|
|
527
|
+
const entryCountsBefore = { files: files.length, entries: files.reduce((sum, file) => sum + file.entries, 0) }
|
|
528
|
+
|
|
529
|
+
if (dryRun) {
|
|
530
|
+
const record = {
|
|
531
|
+
...base,
|
|
532
|
+
status: 'dry-run',
|
|
533
|
+
files,
|
|
534
|
+
entryCountsBefore,
|
|
535
|
+
migrationCompare,
|
|
536
|
+
trivium: { before: { files: Object.keys(triviumBefore).length, digest: inventoryDigest(triviumBefore) } },
|
|
537
|
+
dualWrite: { before: dualWriteBefore, after: dualWriteAfter, action: dualWriteBefore === true ? 'would-set-false' : 'none' },
|
|
538
|
+
}
|
|
539
|
+
writeAudit(audit, record)
|
|
540
|
+
return { ok: true, status: 'dry-run', auditPath: audit, audit: record }
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
for (const file of files) unlinkSync(file.path)
|
|
544
|
+
|
|
545
|
+
const remaining = listLegacyDocs({ storeDir: dir, auditPath: audit })
|
|
546
|
+
const triviumAfter = existsSync(trivium) ? inventory(trivium) : {}
|
|
547
|
+
const beforeDigest = inventoryDigest(triviumBefore)
|
|
548
|
+
const afterDigest = inventoryDigest(triviumAfter)
|
|
549
|
+
|
|
550
|
+
// 删除后的 TriviumDB 复查:老库仍能打开(证明删的只是 JSON,不是引擎)。
|
|
551
|
+
const opened = await openTrivium({ triviumDir: trivium, scope: files[0]?.scope ?? 'unknown-workspace' })
|
|
552
|
+
|
|
553
|
+
const checks = {
|
|
554
|
+
legacyGone: remaining.docs.length === 0,
|
|
555
|
+
triviumIntact: beforeDigest === afterDigest,
|
|
556
|
+
auditWritten: true,
|
|
557
|
+
storeOpen: opened.ok === true,
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
const verifyDigest = sha256(JSON.stringify({
|
|
561
|
+
files: files.map((file) => ({ path: relative(dir, file.path), sha256: file.sha256, bytes: file.bytes })),
|
|
562
|
+
entryCountsBefore,
|
|
563
|
+
trivium: beforeDigest,
|
|
564
|
+
preconditions: preconditions.results.map((probe) => ({ name: probe.name, ok: probe.ok })),
|
|
565
|
+
}))
|
|
566
|
+
const record = {
|
|
567
|
+
...base,
|
|
568
|
+
status: 'removed',
|
|
569
|
+
preconditionsOk: true,
|
|
570
|
+
removedAt: new Date().toISOString(),
|
|
571
|
+
files,
|
|
572
|
+
entryCountsBefore,
|
|
573
|
+
verifyDigest,
|
|
574
|
+
trivium: {
|
|
575
|
+
before: { files: Object.keys(triviumBefore).length, digest: beforeDigest },
|
|
576
|
+
after: { files: Object.keys(triviumAfter).length, digest: afterDigest },
|
|
577
|
+
unchanged: beforeDigest === afterDigest,
|
|
578
|
+
},
|
|
579
|
+
storeOpen: opened,
|
|
580
|
+
dualWrite: { before: dualWriteBefore, after: dualWriteAfter, action: dualWriteBefore === true ? 'set-false' : 'none' },
|
|
581
|
+
migrationCompare,
|
|
582
|
+
postChecks: checks,
|
|
583
|
+
remaining: remaining.docs.map((doc) => doc.path),
|
|
584
|
+
}
|
|
585
|
+
writeAudit(audit, record)
|
|
586
|
+
return { ok: Object.values(checks).every(Boolean), status: 'removed', auditPath: audit, audit: record }
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
/**
|
|
590
|
+
* 删除后复查:TriviumDB 还开得起来。用引擎自己的门面(`store-trivium.js`),不另写打开逻辑。
|
|
591
|
+
* 关键:在**副本**上打开——引擎的 `open()` 会重写 `.tdb`/`.idx` 并重编 `pld.N`,
|
|
592
|
+
* 那属于正常行为,但本工具不该因此改动真实库;副本打开后原库逐字节不变。
|
|
593
|
+
*/
|
|
594
|
+
export async function openTrivium({ triviumDir, scope }) {
|
|
595
|
+
let store = null
|
|
596
|
+
let copy = null
|
|
597
|
+
try {
|
|
598
|
+
copy = mkdtempSync(join(tmpdir(), 'kanabo-trivium-open-'))
|
|
599
|
+
cpSync(triviumDir, copy, { recursive: true })
|
|
600
|
+
const { createTriviumStore } = await import('./store-trivium.js')
|
|
601
|
+
store = createTriviumStore({ workspace: scope, dir: copy, dim: 384, suffix: '', idleMs: 1000 })
|
|
602
|
+
const opened = await store.open({}, { timeoutMs: 20_000 })
|
|
603
|
+
return {
|
|
604
|
+
ok: true,
|
|
605
|
+
observable: `open() on copy ok nodeCount=${opened?.nodeCount ?? 'n/a'} dim=${opened?.dim ?? 'n/a'}`,
|
|
606
|
+
nodeCount: opened?.nodeCount ?? null,
|
|
607
|
+
}
|
|
608
|
+
} catch (error) {
|
|
609
|
+
return { ok: false, observable: `open threw: ${error?.message ?? String(error)}` }
|
|
610
|
+
} finally {
|
|
611
|
+
try {
|
|
612
|
+
await store?.close()
|
|
613
|
+
} catch {
|
|
614
|
+
// 关闭失败不影响已得到的打开结论。
|
|
615
|
+
}
|
|
616
|
+
if (copy !== null) rmSync(copy, { recursive: true, force: true })
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
/* ------------------------------------------------------------------ CLI */
|
|
621
|
+
|
|
622
|
+
const USAGE = `用法:node remove-legacy.mjs --store-dir <dir> [--dry-run] [--audit <path>] [--repo <root>]
|
|
623
|
+
[--trivium-dir <dir>] [--fast] [--timeout <ms>]
|
|
624
|
+
|
|
625
|
+
--store-dir <dir> legacy JSON 记忆目录(删除目标;默认 KANABO_MEMORY_DIR 或 <tmp>/dsh-kanabo-memory)
|
|
626
|
+
--trivium-dir <dir> TriviumDB 目录(默认 <store-dir>/trivium)
|
|
627
|
+
--audit <path> 审计文件路径(默认 <store-dir>/${AUDIT_FILE};永不删除)
|
|
628
|
+
--repo <root> 仓库根(默认脚本上两级)
|
|
629
|
+
--dry-run 只写审计,不删文件
|
|
630
|
+
--fast 缩短 e2e 分析窗口(实测 e2e ~7s、矩阵 ~20s)
|
|
631
|
+
--timeout <ms> 单个探针的期限(默认 600000)
|
|
632
|
+
`
|
|
633
|
+
|
|
634
|
+
function parseArgs(argv) {
|
|
635
|
+
const parsed = { dryRun: false, fast: false, timeoutMs: 600_000 }
|
|
636
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
637
|
+
const arg = argv[index]
|
|
638
|
+
const take = () => {
|
|
639
|
+
const value = argv[++index]
|
|
640
|
+
if (value === undefined) throw new Error(`${arg} 缺少值`)
|
|
641
|
+
return value
|
|
642
|
+
}
|
|
643
|
+
if (arg === '--store-dir') parsed.storeDir = take()
|
|
644
|
+
else if (arg === '--trivium-dir') parsed.triviumDir = take()
|
|
645
|
+
else if (arg === '--audit') parsed.auditPath = take()
|
|
646
|
+
else if (arg === '--repo') parsed.repoRoot = take()
|
|
647
|
+
else if (arg === '--timeout') parsed.timeoutMs = Number(take())
|
|
648
|
+
else if (arg === '--dry-run') parsed.dryRun = true
|
|
649
|
+
else if (arg === '--fast') parsed.fast = true
|
|
650
|
+
else if (arg === '--help') parsed.help = true
|
|
651
|
+
else throw new Error(`未知参数:${arg}`)
|
|
652
|
+
}
|
|
653
|
+
return parsed
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
async function main(argv) {
|
|
657
|
+
let parsed
|
|
658
|
+
try {
|
|
659
|
+
parsed = parseArgs(argv)
|
|
660
|
+
} catch (error) {
|
|
661
|
+
process.stderr.write(`${error.message}\n\n${USAGE}`)
|
|
662
|
+
return 3
|
|
663
|
+
}
|
|
664
|
+
if (parsed.help) {
|
|
665
|
+
process.stdout.write(USAGE)
|
|
666
|
+
return 0
|
|
667
|
+
}
|
|
668
|
+
const storeDir = parsed.storeDir ?? process.env.KANABO_MEMORY_DIR ?? join(tmpdir(), 'dsh-kanabo-memory')
|
|
669
|
+
const outcome = await removeLegacy({
|
|
670
|
+
storeDir,
|
|
671
|
+
dryRun: parsed.dryRun,
|
|
672
|
+
auditPath: parsed.auditPath,
|
|
673
|
+
repoRoot: parsed.repoRoot,
|
|
674
|
+
triviumDir: parsed.triviumDir,
|
|
675
|
+
fast: parsed.fast,
|
|
676
|
+
timeoutMs: parsed.timeoutMs,
|
|
677
|
+
})
|
|
678
|
+
|
|
679
|
+
for (const [name, probe] of Object.entries(outcome.audit.preconditions)) {
|
|
680
|
+
process.stdout.write(`${probe.ok ? 'PASS' : 'FAIL'} pre:${name} :: ${probe.observable}\n`)
|
|
681
|
+
}
|
|
682
|
+
for (const skipped of outcome.audit.skipped ?? []) {
|
|
683
|
+
process.stdout.write(`SKIP ${skipped.path} :: ${skipped.reason}\n`)
|
|
684
|
+
}
|
|
685
|
+
for (const file of outcome.audit.files ?? []) {
|
|
686
|
+
process.stdout.write(`FILE ${file.path} :: ${file.bytes} bytes sha256=${file.sha256} entries=${file.entries}\n`)
|
|
687
|
+
}
|
|
688
|
+
if (outcome.status === 'removed') {
|
|
689
|
+
const detail = {
|
|
690
|
+
legacyGone: `remaining legacy docs=${outcome.audit.remaining.length}`,
|
|
691
|
+
triviumIntact: `trivium ${outcome.audit.trivium.before.files} files digest ${outcome.audit.trivium.before.digest.slice(0, 12)} → ${outcome.audit.trivium.after.digest.slice(0, 12)}`,
|
|
692
|
+
auditWritten: outcome.auditPath,
|
|
693
|
+
storeOpen: outcome.audit.storeOpen?.observable ?? 'n/a',
|
|
694
|
+
}
|
|
695
|
+
for (const [name, ok] of Object.entries(outcome.audit.postChecks)) {
|
|
696
|
+
process.stdout.write(`${ok ? 'PASS' : 'FAIL'} post:${name} :: ${detail[name] ?? ''}\n`)
|
|
697
|
+
}
|
|
698
|
+
process.stdout.write(`AUDIT ${outcome.auditPath} :: verifyDigest=${outcome.audit.verifyDigest}\n`)
|
|
699
|
+
}
|
|
700
|
+
process.stdout.write(`STATUS: ${outcome.status}${outcome.audit.reason ? ` (${outcome.audit.reason})` : ''}\n`)
|
|
701
|
+
process.stdout.write(`RESULT: ${outcome.ok ? 'PASS' : 'FAIL'}\n`)
|
|
702
|
+
return outcome.ok ? 0 : 1
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
function isEntryPoint() {
|
|
706
|
+
const script = process.argv[1]
|
|
707
|
+
if (typeof script !== 'string' || script.length === 0) return false
|
|
708
|
+
try {
|
|
709
|
+
return resolve(script) === resolve(fileURLToPath(import.meta.url))
|
|
710
|
+
} catch {
|
|
711
|
+
return false
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
if (isEntryPoint()) {
|
|
716
|
+
main(process.argv.slice(2)).then((code) => { process.exitCode = code }, (error) => {
|
|
717
|
+
process.stderr.write(`remove-legacy: ${error?.stack ?? String(error)}\n`)
|
|
718
|
+
process.exitCode = 1
|
|
719
|
+
})
|
|
720
|
+
}
|