dsh-config-sync-plugin 0.3.66 → 0.3.67
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/README.en.md +23 -1
- package/README.md +23 -1
- package/client.js +1 -1
- package/index.js +4 -4
- package/package.json +1 -1
- package/runtime/apply.ps1 +33 -1
- package/runtime/capture.ps1 +68 -38
- package/runtime/dsh-sync-common.ps1 +131 -1
- package/runtime/tools/session-cache-guard.mjs +407 -0
- package/runtime/tools/session-log-guard.mjs +438 -0
- package/runtime/tools/session-log-lib.mjs +322 -0
- package/tools/check-session-log.mjs +156 -0
- package/tools/pack-plugin.ps1 +1 -1
- package/tools/selftest.mjs +4 -2
- package/tools/session-log-guard.mjs +438 -0
- package/tools/session-log-lib.mjs +322 -0
|
@@ -0,0 +1,438 @@
|
|
|
1
|
+
// 会话日志跨机同步护栏
|
|
2
|
+
//
|
|
3
|
+
// 解决的问题
|
|
4
|
+
// 同步是「整文件覆盖」:capture 把本机日志整份拷进仓库、apply 把仓库日志整份铺回本机。
|
|
5
|
+
// 两台机器同时在同一个会话里跑,各自 append 出不同 lineage;整文件覆盖必然让其中一方
|
|
6
|
+
// 的事件整段消失,DSH 启动时报
|
|
7
|
+
// corrupt session log: released v2 row N has seq gap (expected N, got M)
|
|
8
|
+
// 更糟的是拼接:本机文件被换成较短版本后 DSH 仍按自己的 eventCount 继续 append,
|
|
9
|
+
// 于是「A 的前 10776 条 + B 从 12304 起」首尾相接 —— B 机报的就是这个。
|
|
10
|
+
//
|
|
11
|
+
// 用法(capture.ps1 / apply.ps1 调用)
|
|
12
|
+
// node session-log-guard.mjs decide <srcFile> <dstFile> <outJson>
|
|
13
|
+
// 输出决策 { ok, action, relation, detail, srcInfo, dstInfo, mergedFile? }
|
|
14
|
+
// action = keep_dst 保留目标(目标更全或完全相同)
|
|
15
|
+
// take_src 源是目标的严格超集,正常覆盖
|
|
16
|
+
// merge 真分叉,并集已写到 mergedFile,调用方用它覆盖
|
|
17
|
+
// corrupt ★任何一方自身 seq 不连续,拒绝写,等人工处理
|
|
18
|
+
// node session-log-guard.mjs merge <aFile> <bFile> <outFile>
|
|
19
|
+
// 求并集写盘(救援用)
|
|
20
|
+
// node session-log-guard.mjs check <file> [<file2>]
|
|
21
|
+
// 只体检
|
|
22
|
+
//
|
|
23
|
+
// 退出码:0=可以继续,1=坏档/无法自动处理,2=用法或 IO 错误
|
|
24
|
+
|
|
25
|
+
import fs from 'node:fs';
|
|
26
|
+
import os from 'node:os';
|
|
27
|
+
import path from 'node:path';
|
|
28
|
+
import {
|
|
29
|
+
loadLog, restAfterHeader, compareRaw, compareSeqs, findGaps, seqTailOf, mergeLogs,
|
|
30
|
+
} from './session-log-lib.mjs';
|
|
31
|
+
|
|
32
|
+
/** 中间产物一律落在临时目录,绝不写进会话目录(DSH 会扫那个目录) */
|
|
33
|
+
function tmpPath(tag) {
|
|
34
|
+
return path.join(os.tmpdir(), `dsh-sessionlog-${tag}-${Date.now()}-${process.pid}.zstd`);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function summarize(log) {
|
|
38
|
+
const g = findGaps(log.rows);
|
|
39
|
+
return {
|
|
40
|
+
bytes: log.bytes,
|
|
41
|
+
frames: log.frames.length,
|
|
42
|
+
rows: log.rows.length,
|
|
43
|
+
seqFirst: g.first,
|
|
44
|
+
seqTail: g.tail,
|
|
45
|
+
gaps: g.gaps.length,
|
|
46
|
+
gapSample: g.gaps.slice(0, 3),
|
|
47
|
+
cwd: log.header && log.header.cwd,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function decide(srcFile, dstFile) {
|
|
52
|
+
const out = { src: srcFile, dst: dstFile };
|
|
53
|
+
|
|
54
|
+
if (!fs.existsSync(dstFile)) {
|
|
55
|
+
out.ok = true;
|
|
56
|
+
out.action = 'take_src';
|
|
57
|
+
out.relation = 'dst_missing';
|
|
58
|
+
out.detail = '目标不存在,直接铺';
|
|
59
|
+
return out;
|
|
60
|
+
}
|
|
61
|
+
if (!fs.existsSync(srcFile)) {
|
|
62
|
+
out.ok = true;
|
|
63
|
+
out.action = 'keep_dst';
|
|
64
|
+
out.relation = 'src_missing';
|
|
65
|
+
out.detail = '源不存在,保留目标';
|
|
66
|
+
return out;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const sSize = fs.statSync(srcFile).size;
|
|
70
|
+
const dSize = fs.statSync(dstFile).size;
|
|
71
|
+
if (sSize === dSize) {
|
|
72
|
+
const a = fs.readFileSync(srcFile);
|
|
73
|
+
const b = fs.readFileSync(dstFile);
|
|
74
|
+
if (a.equals(b)) {
|
|
75
|
+
out.ok = true;
|
|
76
|
+
out.action = 'keep_dst';
|
|
77
|
+
out.relation = 'identical';
|
|
78
|
+
out.detail = '两边字节完全相同';
|
|
79
|
+
out.srcInfo = { bytes: sSize };
|
|
80
|
+
out.dstInfo = { bytes: dSize };
|
|
81
|
+
return out;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
let S;
|
|
86
|
+
let D;
|
|
87
|
+
try {
|
|
88
|
+
S = loadLog(srcFile);
|
|
89
|
+
} catch (e) {
|
|
90
|
+
out.ok = false;
|
|
91
|
+
out.action = 'keep_dst';
|
|
92
|
+
out.relation = 'src_unreadable';
|
|
93
|
+
out.detail = `源无法解析(${e.message}),保留目标不动`;
|
|
94
|
+
return out;
|
|
95
|
+
}
|
|
96
|
+
try {
|
|
97
|
+
D = loadLog(dstFile);
|
|
98
|
+
} catch (e) {
|
|
99
|
+
out.ok = true;
|
|
100
|
+
out.action = 'take_src';
|
|
101
|
+
out.relation = 'dst_unreadable';
|
|
102
|
+
out.detail = `目标无法解析(${e.message}),用源覆盖修复`;
|
|
103
|
+
out.srcInfo = summarize(S);
|
|
104
|
+
return out;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
out.srcInfo = summarize(S);
|
|
108
|
+
out.dstInfo = summarize(D);
|
|
109
|
+
|
|
110
|
+
// 自身已坏的档不能用来覆盖,也不能当合并基底
|
|
111
|
+
if (out.srcInfo.gaps > 0) {
|
|
112
|
+
out.ok = false;
|
|
113
|
+
out.action = 'keep_dst';
|
|
114
|
+
out.relation = 'src_corrupt';
|
|
115
|
+
out.detail = `源自身有 ${out.srcInfo.gaps} 处 seq 跳号(已坏),拒绝覆盖目标`;
|
|
116
|
+
return out;
|
|
117
|
+
}
|
|
118
|
+
if (out.dstInfo.gaps > 0) {
|
|
119
|
+
// 目标自身已断档(正是 B 机报 "seq gap" 的形态)
|
|
120
|
+
// 1) 源干净且 seq 覆盖目标 → 直接用源
|
|
121
|
+
// 2) 否则拿源去填目标的洞:并集连续就写并集(救援)
|
|
122
|
+
// 3) 洞填不满 → 事件真丢了,拒绝写,留给人工
|
|
123
|
+
const sq = compareSeqs(S.rows, D.rows);
|
|
124
|
+
out.seqRelation = { relation: sq.relation, onlyA: sq.onlyA.length, onlyB: sq.onlyB.length };
|
|
125
|
+
if (sq.relation === 'a_superset' || sq.relation === 'equal') {
|
|
126
|
+
out.ok = true;
|
|
127
|
+
out.action = 'take_src';
|
|
128
|
+
out.relation = 'dst_corrupt_fix_by_src';
|
|
129
|
+
out.detail = `目标自身有 ${out.dstInfo.gaps} 处 seq 跳号,源是它的超集且干净,用源修复`;
|
|
130
|
+
return out;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const repaired = mergeLogs(S, D);
|
|
134
|
+
out.forkDetail = {
|
|
135
|
+
onlySrc: sq.onlyA.length,
|
|
136
|
+
onlyDst: sq.onlyB.length,
|
|
137
|
+
sampleOnlySrc: sq.onlyA.slice(0, 5),
|
|
138
|
+
sampleOnlyDst: sq.onlyB.slice(0, 5),
|
|
139
|
+
holes: repaired.holes,
|
|
140
|
+
conflictingSeq: repaired.conflicts.slice(0, 10),
|
|
141
|
+
};
|
|
142
|
+
if (repaired.holes.length > 0) {
|
|
143
|
+
out.ok = false;
|
|
144
|
+
out.action = 'keep_dst';
|
|
145
|
+
out.relation = 'dst_corrupt_unrepairable';
|
|
146
|
+
out.detail = `目标自身有 ${out.dstInfo.gaps} 处 seq 跳号,用源补齐后仍有 ${repaired.holes.length} 处空洞(事件已丢失),需人工处理`;
|
|
147
|
+
return out;
|
|
148
|
+
}
|
|
149
|
+
const repairedPath = tmpPath('repaired');
|
|
150
|
+
fs.writeFileSync(repairedPath, repaired.buffer);
|
|
151
|
+
out.ok = true;
|
|
152
|
+
out.action = 'merge';
|
|
153
|
+
out.mergedFile = repairedPath;
|
|
154
|
+
out.mergedInfo = {
|
|
155
|
+
bytes: repaired.buffer.length,
|
|
156
|
+
rows: repaired.rows,
|
|
157
|
+
seqFirst: repaired.seqFirst,
|
|
158
|
+
seqTail: repaired.seqTail,
|
|
159
|
+
fromSrc: repaired.fromA,
|
|
160
|
+
fromDst: repaired.fromB,
|
|
161
|
+
conflictingSeq: repaired.conflicts.length,
|
|
162
|
+
};
|
|
163
|
+
out.relation = 'dst_corrupt_repaired_by_src';
|
|
164
|
+
out.detail = `目标自身有 ${out.dstInfo.gaps} 处 seq 跳号,用源补齐洞后连续(${repaired.rows} 个事件,seq ${repaired.seqFirst}..${repaired.seqTail}),用修复件覆盖`;
|
|
165
|
+
return out;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// 两边都自洽:先做字节前缀判定(最快最严),再做 seq 集合判定
|
|
169
|
+
const restS = restAfterHeader(S.buf);
|
|
170
|
+
const restD = restAfterHeader(D.buf);
|
|
171
|
+
const cmp = restS && restD ? compareRaw(restS, restD) : { relation: 'fork' };
|
|
172
|
+
out.byteRelation = cmp.relation;
|
|
173
|
+
|
|
174
|
+
if (cmp.relation === 'identical') {
|
|
175
|
+
out.ok = true;
|
|
176
|
+
out.action = 'keep_dst';
|
|
177
|
+
out.relation = 'identical';
|
|
178
|
+
out.detail = '去掉首帧后字节一致,同源且同步';
|
|
179
|
+
return out;
|
|
180
|
+
}
|
|
181
|
+
if (cmp.relation === 'b_is_prefix_of_a') {
|
|
182
|
+
out.ok = true;
|
|
183
|
+
out.action = 'take_src';
|
|
184
|
+
out.relation = 'dst_prefix_of_src';
|
|
185
|
+
out.detail = `目标落后 ${out.srcInfo.rows - out.dstInfo.rows} 个事件且是其字节前缀,安全覆盖`;
|
|
186
|
+
return out;
|
|
187
|
+
}
|
|
188
|
+
if (cmp.relation === 'a_is_prefix_of_b') {
|
|
189
|
+
out.ok = true;
|
|
190
|
+
out.action = 'keep_dst';
|
|
191
|
+
out.relation = 'src_prefix_of_dst';
|
|
192
|
+
out.detail = `源落后 ${out.dstInfo.rows - out.srcInfo.rows} 个事件,保留目标(不回退)`;
|
|
193
|
+
return out;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// 字节不是前缀关系:可能是「同一批事件被重新压过帧」,也可能是真分叉。用 seq 集合判定。
|
|
197
|
+
const sq = compareSeqs(S.rows, D.rows);
|
|
198
|
+
out.seqRelation = { relation: sq.relation, onlyA: sq.onlyA.length, onlyB: sq.onlyB.length };
|
|
199
|
+
|
|
200
|
+
if (sq.relation === 'equal') {
|
|
201
|
+
out.ok = true;
|
|
202
|
+
out.action = 'keep_dst';
|
|
203
|
+
out.relation = 'same_seqset';
|
|
204
|
+
out.detail = `seq 集合一致(${sq.countA} 个事件),只是帧边界/压缩不同,保留目标`;
|
|
205
|
+
return out;
|
|
206
|
+
}
|
|
207
|
+
if (sq.relation === 'a_superset') {
|
|
208
|
+
out.ok = true;
|
|
209
|
+
out.action = 'take_src';
|
|
210
|
+
out.relation = 'src_superset';
|
|
211
|
+
out.detail = `源多 ${sq.onlyA.length} 个事件、不丢目标任何事件,安全覆盖`;
|
|
212
|
+
return out;
|
|
213
|
+
}
|
|
214
|
+
if (sq.relation === 'b_superset') {
|
|
215
|
+
out.ok = true;
|
|
216
|
+
out.action = 'keep_dst';
|
|
217
|
+
out.relation = 'dst_superset';
|
|
218
|
+
out.detail = `目标多 ${sq.onlyB.length} 个事件,保留目标(不回退)`;
|
|
219
|
+
return out;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// 真分叉:两边各有对方没有的事件
|
|
223
|
+
const merged = mergeLogs(S, D);
|
|
224
|
+
out.relation = 'fork';
|
|
225
|
+
out.forkDetail = {
|
|
226
|
+
divergeAt: cmp.divergeAt,
|
|
227
|
+
onlySrc: sq.onlyA.length,
|
|
228
|
+
onlyDst: sq.onlyB.length,
|
|
229
|
+
sampleOnlySrc: sq.onlyA.slice(0, 5),
|
|
230
|
+
sampleOnlyDst: sq.onlyB.slice(0, 5),
|
|
231
|
+
holes: merged.holes,
|
|
232
|
+
conflictingSeq: merged.conflicts.slice(0, 10),
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
if (merged.holes.length > 0) {
|
|
236
|
+
out.ok = false;
|
|
237
|
+
out.action = 'keep_dst';
|
|
238
|
+
out.detail = `两边分叉且并集仍有 ${merged.holes.length} 处空洞(事件真丢了,补不回来),保留目标并备份,需人工判断`;
|
|
239
|
+
return out;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const mergedPath = tmpPath('merged');
|
|
243
|
+
fs.writeFileSync(mergedPath, merged.buffer);
|
|
244
|
+
out.ok = true;
|
|
245
|
+
out.action = 'merge';
|
|
246
|
+
out.mergedFile = mergedPath;
|
|
247
|
+
out.mergedInfo = {
|
|
248
|
+
bytes: merged.buffer.length,
|
|
249
|
+
rows: merged.rows,
|
|
250
|
+
seqFirst: merged.seqFirst,
|
|
251
|
+
seqTail: merged.seqTail,
|
|
252
|
+
fromSrc: merged.fromA,
|
|
253
|
+
fromDst: merged.fromB,
|
|
254
|
+
conflictingSeq: merged.conflicts.length,
|
|
255
|
+
};
|
|
256
|
+
out.detail = `真分叉,已按 seq 求并集为 ${merged.rows} 个事件(seq ${merged.seqFirst}..${merged.seqTail},相容冲突 ${merged.conflicts.length} 个),用并集覆盖以免丢事件`;
|
|
257
|
+
return out;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// ---------------- 批量模式(capture.ps1 / apply.ps1 用这个) ----------------
|
|
261
|
+
//
|
|
262
|
+
// 为什么批量:一台机器有几十个会话日志,逐个 spawn node 光是进程启动就要几秒。
|
|
263
|
+
// 一次调用把整批跑完,只启动一次进程。
|
|
264
|
+
//
|
|
265
|
+
// node session-log-guard.mjs batch <jobJson> <outJson>
|
|
266
|
+
//
|
|
267
|
+
// jobJson:
|
|
268
|
+
// { "jobs": [ { "src": "<要被采用的候选>", "dst": "<当前生效的那份>", "writeTo": "<把赢家写到哪里>" } ] }
|
|
269
|
+
//
|
|
270
|
+
// capture 语义:src = 本机文件,dst = 仓库文件,writeTo = 仓库文件
|
|
271
|
+
// apply 语义:src = 仓库文件,dst = 本机文件,writeTo = 本机文件
|
|
272
|
+
//
|
|
273
|
+
// 每项结果 { src, dst, ok, action, relation, detail, wrote, backup, mergedFile? }
|
|
274
|
+
// action=keep_dst → 不写
|
|
275
|
+
// action=take_src → 把 src 字节写到 writeTo(原文件先备份)
|
|
276
|
+
// action=merge → 把并集写到 writeTo(原文件先备份)
|
|
277
|
+
// ok=false → 不写,调用方应 Warn 并让用户知道这条会话需要人工处理
|
|
278
|
+
//
|
|
279
|
+
// 备份位置由调用方给(backupDir):绝不能把备份写进会话目录 —— DSH 会扫那个目录,
|
|
280
|
+
// 多出来的 *.guard-bak 文件会被当成无法识别的会话产物。
|
|
281
|
+
function backupTo(srcPath, backupDir) {
|
|
282
|
+
if (!backupDir) return null;
|
|
283
|
+
fs.mkdirSync(backupDir, { recursive: true });
|
|
284
|
+
const base = path.basename(srcPath).replace(/[^\w.\-]+/g, '_');
|
|
285
|
+
const dst = path.join(backupDir, `${Date.now()}-${process.pid}-${base}`);
|
|
286
|
+
fs.copyFileSync(srcPath, dst);
|
|
287
|
+
return dst;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
export function runBatch(jobFile, outFile) {
|
|
291
|
+
const spec = JSON.parse(fs.readFileSync(jobFile, 'utf8'));
|
|
292
|
+
const jobs = Array.isArray(spec.jobs) ? spec.jobs : [];
|
|
293
|
+
const backupDir = spec.backupDir || '';
|
|
294
|
+
const results = [];
|
|
295
|
+
let nTake = 0;
|
|
296
|
+
let nMerge = 0;
|
|
297
|
+
let nKeep = 0;
|
|
298
|
+
let nFail = 0;
|
|
299
|
+
|
|
300
|
+
for (const j of jobs) {
|
|
301
|
+
const r = { src: j.src, dst: j.dst };
|
|
302
|
+
try {
|
|
303
|
+
const d = decide(j.src, j.dst);
|
|
304
|
+
r.ok = d.ok;
|
|
305
|
+
r.action = d.action;
|
|
306
|
+
r.relation = d.relation;
|
|
307
|
+
r.detail = d.detail;
|
|
308
|
+
if (d.forkDetail) r.forkDetail = d.forkDetail;
|
|
309
|
+
if (d.mergedInfo) r.mergedInfo = d.mergedInfo;
|
|
310
|
+
// 目标不存在时 decide 直接给 take_src,此时没有 dst 可备份
|
|
311
|
+
if (d.action === 'take_src' && j.writeTo) {
|
|
312
|
+
if (fs.existsSync(j.writeTo)) r.backup = backupTo(j.writeTo, backupDir);
|
|
313
|
+
fs.copyFileSync(j.src, j.writeTo);
|
|
314
|
+
r.wrote = j.writeTo;
|
|
315
|
+
nTake++;
|
|
316
|
+
} else if (d.action === 'merge' && j.writeTo && d.mergedFile) {
|
|
317
|
+
if (fs.existsSync(j.writeTo)) r.backup = backupTo(j.writeTo, backupDir);
|
|
318
|
+
fs.copyFileSync(d.mergedFile, j.writeTo);
|
|
319
|
+
r.wrote = j.writeTo;
|
|
320
|
+
try { fs.unlinkSync(d.mergedFile); } catch { /* 临时件删不掉也不影响结果 */ }
|
|
321
|
+
nMerge++;
|
|
322
|
+
} else if (d.action === 'keep_dst') {
|
|
323
|
+
nKeep++;
|
|
324
|
+
}
|
|
325
|
+
if (!d.ok) nFail++;
|
|
326
|
+
} catch (e) {
|
|
327
|
+
r.ok = false;
|
|
328
|
+
r.action = 'keep_dst';
|
|
329
|
+
r.relation = 'guard_error';
|
|
330
|
+
r.detail = `护栏自身出错:${e.message}`;
|
|
331
|
+
nFail++;
|
|
332
|
+
}
|
|
333
|
+
results.push(r);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
const report = { ok: nFail === 0, total: jobs.length, take: nTake, merge: nMerge, keep: nKeep, failed: nFail, results };
|
|
337
|
+
if (outFile) fs.writeFileSync(outFile, JSON.stringify(report, null, 2), 'utf8');
|
|
338
|
+
return report;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// ---------------- CLI ----------------
|
|
342
|
+
|
|
343
|
+
const [cmd, ...rest] = process.argv.slice(2).filter((a) => a !== undefined);
|
|
344
|
+
|
|
345
|
+
function emit(code, obj) {
|
|
346
|
+
console.log(JSON.stringify(obj, null, 2));
|
|
347
|
+
process.exit(code);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
const invokedDirectly = process.argv[1] && /session-log-guard\.mjs$/i.test(process.argv[1]);
|
|
351
|
+
|
|
352
|
+
if (invokedDirectly) {
|
|
353
|
+
if (cmd === 'decide') {
|
|
354
|
+
const [src, dst, outFile] = rest;
|
|
355
|
+
if (!src || !dst) emit(2, { error: '用法:decide <src> <dst> [outJson]' });
|
|
356
|
+
let r;
|
|
357
|
+
try {
|
|
358
|
+
r = decide(src, dst);
|
|
359
|
+
} catch (e) {
|
|
360
|
+
r = { ok: false, action: 'keep_dst', relation: 'guard_error', detail: `护栏自身出错:${e.message}` };
|
|
361
|
+
}
|
|
362
|
+
const text = JSON.stringify(r, null, 2);
|
|
363
|
+
if (outFile) {
|
|
364
|
+
try {
|
|
365
|
+
fs.writeFileSync(outFile, text, 'utf8');
|
|
366
|
+
} catch {
|
|
367
|
+
/* 写不出就只走 stdout */
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
emit(r.ok ? 0 : 1, r);
|
|
371
|
+
} else if (cmd === 'merge') {
|
|
372
|
+
const [a, b, outFile] = rest;
|
|
373
|
+
if (!a || !b || !outFile) emit(2, { error: '用法:merge <a> <b> <out>' });
|
|
374
|
+
try {
|
|
375
|
+
const A = loadLog(a);
|
|
376
|
+
const B = loadLog(b);
|
|
377
|
+
const m = mergeLogs(A, B);
|
|
378
|
+
fs.writeFileSync(outFile, m.buffer);
|
|
379
|
+
emit(m.holes.length === 0 ? 0 : 1, {
|
|
380
|
+
ok: m.holes.length === 0,
|
|
381
|
+
out: outFile,
|
|
382
|
+
bytes: m.buffer.length,
|
|
383
|
+
rows: m.rows,
|
|
384
|
+
seqFirst: m.seqFirst,
|
|
385
|
+
seqTail: m.seqTail,
|
|
386
|
+
holes: m.holes,
|
|
387
|
+
conflictingSeq: m.conflicts.length,
|
|
388
|
+
});
|
|
389
|
+
} catch (e) {
|
|
390
|
+
emit(2, { error: e.message });
|
|
391
|
+
}
|
|
392
|
+
} else if (cmd === 'check') {
|
|
393
|
+
const [a, b] = rest;
|
|
394
|
+
if (!a) emit(2, { error: '用法:check <file> [<file2>]' });
|
|
395
|
+
try {
|
|
396
|
+
const A = loadLog(a);
|
|
397
|
+
const gapsA = findGaps(A.rows);
|
|
398
|
+
const out = { self: { file: a, ok: gapsA.gaps.length === 0, ...summarize(A) } };
|
|
399
|
+
let rc = out.self.ok ? 0 : 1;
|
|
400
|
+
if (b) {
|
|
401
|
+
const B = loadLog(b);
|
|
402
|
+
const gapsB = findGaps(B.rows);
|
|
403
|
+
out.other = { file: b, ok: gapsB.gaps.length === 0, ...summarize(B) };
|
|
404
|
+
out.compare = {
|
|
405
|
+
bytes: (() => {
|
|
406
|
+
const ra = restAfterHeader(A.buf);
|
|
407
|
+
const rb = restAfterHeader(B.buf);
|
|
408
|
+
return ra && rb ? compareRaw(ra, rb) : { relation: 'unavailable' };
|
|
409
|
+
})(),
|
|
410
|
+
seqs: (() => {
|
|
411
|
+
const s = compareSeqs(A.rows, B.rows);
|
|
412
|
+
return { relation: s.relation, onlyA: s.onlyA.length, onlyB: s.onlyB.length, sampleOnlyA: s.onlyA.slice(0, 5), sampleOnlyB: s.onlyB.slice(0, 5) };
|
|
413
|
+
})(),
|
|
414
|
+
};
|
|
415
|
+
if (out.compare.bytes.relation === 'fork' && out.compare.seqs.relation === 'both_exclusive') rc = 1;
|
|
416
|
+
}
|
|
417
|
+
emit(rc, out);
|
|
418
|
+
} catch (e) {
|
|
419
|
+
emit(2, { error: e.message });
|
|
420
|
+
}
|
|
421
|
+
} else if (cmd === 'batch') {
|
|
422
|
+
const [jobFile, outFile] = rest;
|
|
423
|
+
if (!jobFile) emit(2, { error: '用法:batch <jobJson> <outJson>' });
|
|
424
|
+
try {
|
|
425
|
+
const r = runBatch(jobFile, outFile);
|
|
426
|
+
emit(r.ok ? 0 : 1, r);
|
|
427
|
+
} catch (e) {
|
|
428
|
+
emit(2, { error: e.message });
|
|
429
|
+
}
|
|
430
|
+
} else {
|
|
431
|
+
emit(2, {
|
|
432
|
+
error: '用法:session-log-guard.mjs <decide|merge|check|batch> ...',
|
|
433
|
+
examples: ['decide <src> <dst> [outJson]', 'merge <a> <b> <out>', 'check <file> [<file2>]', 'batch <jobJson> <outJson>'],
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
export { seqTailOf };
|