@suifei/dsh-ai-team 0.4.145 → 0.4.147
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/CHANGELOG.md +11 -0
- package/LICENSE +21 -21
- package/README.md +31 -2
- package/client.js +551 -49
- package/cordis.patch.yml +31 -31
- package/icon.svg +12 -12
- package/index.js +105 -25
- package/lib/domain/ids.js +26 -26
- package/lib/domain/inbox.js +93 -93
- package/lib/domain/member-config.js +168 -168
- package/lib/domain/policy.js +8 -1
- package/lib/domain/task.js +2 -0
- package/lib/domain/team-blueprint.js +240 -0
- package/lib/host/decision-report.js +72 -72
- package/lib/host/default-roster.js +18 -0
- package/lib/host/default-workspace.js +69 -69
- package/lib/host/fault-adapter.js +50 -50
- package/lib/host/member-tools.js +6 -1
- package/lib/host/people-view.js +9 -2
- package/lib/host/permission-guard.js +61 -61
- package/lib/host/project-registry.js +104 -104
- package/lib/host/project-runtime.js +2 -2
- package/lib/host/risk-view.js +99 -99
- package/lib/host/service.js +195 -16
- package/lib/host/views.js +42 -1
- package/lib/host/waker.js +38 -8
- package/lib/host/weekly-view.js +86 -86
- package/lib/host/workspace-path.js +94 -94
- package/lib/ledger/checkpoint.js +415 -415
- package/lib/ledger/events.js +398 -398
- package/lib/ledger/fold.js +23 -0
- package/lib/ledger/namespace.js +124 -124
- package/lib/ledger/tools.js +118 -118
- package/lib/ledger/writer.js +1266 -1266
- package/lib/runtime/error-chain.js +31 -31
- package/lib/runtime/shell-guard.js +102 -102
- package/lib/runtime/skill-catalog.js +41 -41
- package/lib/runtime/skill-policy.js +12 -12
- package/lib/runtime/stop-guard.js +17 -2
- package/lib/skillopt/adopt-gate.js +108 -108
- package/lib/skillopt/gate-wiring.js +146 -146
- package/lib/skillopt/jobs.js +135 -135
- package/lib/skillopt/proposal-store.js +33 -33
- package/lib/skillopt/review-runner.js +107 -107
- package/lib/skillopt/rubric.js +189 -189
- package/lib/skillopt/subagent-text.js +101 -101
- package/lib/skillopt/tool-access.js +56 -56
- package/locale/en.json +6 -6
- package/locale/zh.json +6 -6
- package/package.json +1 -1
package/lib/ledger/checkpoint.js
CHANGED
|
@@ -1,415 +1,415 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 检查点:{ v, stateVersion, lastSeq, lastHash, state } 的原子保存/加载/备份/校验。
|
|
3
|
-
*
|
|
4
|
-
* 对应 03-架构设计报告 §6.4 / §6.5 / §6.7、附录 B §B.3.6-4:
|
|
5
|
-
* - 原子写:同目录 tmp → fsync → rename 覆盖 checkpoint.json。
|
|
6
|
-
* - 备份:覆盖前把上一份“有效”检查点复制为 checkpoint.bak.json。
|
|
7
|
-
* - 加载:JSON/结构/stateVersion/lastSeq+lastHash 全部校验;失败回退备份,
|
|
8
|
-
* 再失败由 recoverCheckpoint 从 seq 1 全量重放。
|
|
9
|
-
* - 检查点绝不领先账本:lastHash 必须等于账本中 lastSeq 行的事件 hash。
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
import fsDefault from 'node:fs';
|
|
13
|
-
import path from 'node:path';
|
|
14
|
-
import { randomBytes } from 'node:crypto';
|
|
15
|
-
import {
|
|
16
|
-
GENESIS_HASH,
|
|
17
|
-
HASH_PATTERN,
|
|
18
|
-
LedgerError,
|
|
19
|
-
canonicalJson,
|
|
20
|
-
isPlainObject,
|
|
21
|
-
} from './events.js';
|
|
22
|
-
|
|
23
|
-
export const CHECKPOINT_VERSION = 1;
|
|
24
|
-
export const CHECKPOINT_FILE = 'checkpoint.json';
|
|
25
|
-
export const CHECKPOINT_BACKUP_FILE = 'checkpoint.bak.json';
|
|
26
|
-
|
|
27
|
-
/** <dataDir>/checkpoint.json */
|
|
28
|
-
export function checkpointPath(dir) {
|
|
29
|
-
return path.join(dir, CHECKPOINT_FILE);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/** <dataDir>/checkpoint.bak.json */
|
|
33
|
-
export function checkpointBackupPath(dir) {
|
|
34
|
-
return path.join(dir, CHECKPOINT_BACKUP_FILE);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function invalid(code, message, details = {}) {
|
|
38
|
-
return { ok: false, code, message, details };
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function writeAll(fs, fd, buffer) {
|
|
42
|
-
let offset = 0;
|
|
43
|
-
while (offset < buffer.length) {
|
|
44
|
-
const written = fs.writeSync(fd, buffer, offset, buffer.length - offset, null);
|
|
45
|
-
if (!Number.isFinite(written) || written <= 0) throw new Error('short write');
|
|
46
|
-
offset += written;
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function writeAtomic(fs, filePath, text) {
|
|
51
|
-
const fd = fs.openSync(filePath, 'wx');
|
|
52
|
-
try {
|
|
53
|
-
writeAll(fs, fd, Buffer.from(text, 'utf8'));
|
|
54
|
-
fs.fsyncSync(fd);
|
|
55
|
-
} catch (error) {
|
|
56
|
-
try {
|
|
57
|
-
fs.closeSync(fd);
|
|
58
|
-
} catch {
|
|
59
|
-
// 忽略清理失败。
|
|
60
|
-
}
|
|
61
|
-
try {
|
|
62
|
-
fs.unlinkSync(filePath);
|
|
63
|
-
} catch {
|
|
64
|
-
// 忽略清理失败。
|
|
65
|
-
}
|
|
66
|
-
throw error;
|
|
67
|
-
}
|
|
68
|
-
fs.closeSync(fd);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function fsyncDirectory(fs, dir) {
|
|
72
|
-
// Windows 不支持对目录 fsync;这是 best-effort,不影响 rename 的原子性。
|
|
73
|
-
try {
|
|
74
|
-
const fd = fs.openSync(dir, 'r');
|
|
75
|
-
try {
|
|
76
|
-
fs.fsyncSync(fd);
|
|
77
|
-
} finally {
|
|
78
|
-
fs.closeSync(fd);
|
|
79
|
-
}
|
|
80
|
-
} catch {
|
|
81
|
-
// EPERM / EISDIR / EINVAL:忽略。
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* 校验检查点结构与版本。返回 {ok:true} 或 {ok:false, code, message, details}。
|
|
87
|
-
* options.stateVersion 给定时要与检查点一致(不一致即丢弃重放)。
|
|
88
|
-
*/
|
|
89
|
-
export function validateCheckpoint(checkpoint, options = {}) {
|
|
90
|
-
if (!isPlainObject(checkpoint)) {
|
|
91
|
-
return invalid('E_CHECKPOINT_INVALID', 'checkpoint must be a plain object');
|
|
92
|
-
}
|
|
93
|
-
if (checkpoint.v !== CHECKPOINT_VERSION) {
|
|
94
|
-
return invalid('E_CHECKPOINT_VERSION', 'unsupported checkpoint version', {
|
|
95
|
-
supported: CHECKPOINT_VERSION,
|
|
96
|
-
actual: checkpoint.v,
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
if (!Number.isSafeInteger(checkpoint.stateVersion) || checkpoint.stateVersion < 1) {
|
|
100
|
-
return invalid('E_CHECKPOINT_INVALID', 'checkpoint.stateVersion must be a positive integer', {
|
|
101
|
-
stateVersion: checkpoint.stateVersion,
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
if (
|
|
105
|
-
options.stateVersion !== undefined &&
|
|
106
|
-
checkpoint.stateVersion !== options.stateVersion
|
|
107
|
-
) {
|
|
108
|
-
return invalid('E_CHECKPOINT_STATE_VERSION', 'checkpoint stateVersion does not match the fold', {
|
|
109
|
-
expected: options.stateVersion,
|
|
110
|
-
actual: checkpoint.stateVersion,
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
if (!Number.isSafeInteger(checkpoint.lastSeq) || checkpoint.lastSeq < 0) {
|
|
114
|
-
return invalid('E_CHECKPOINT_INVALID', 'checkpoint.lastSeq must be a non-negative integer', {
|
|
115
|
-
lastSeq: checkpoint.lastSeq,
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
if (typeof checkpoint.lastHash !== 'string' || !HASH_PATTERN.test(checkpoint.lastHash)) {
|
|
119
|
-
return invalid('E_CHECKPOINT_INVALID', 'checkpoint.lastHash must be 64 lowercase hex chars', {
|
|
120
|
-
lastHash: checkpoint.lastHash,
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
if (checkpoint.lastSeq === 0 && checkpoint.lastHash !== GENESIS_HASH) {
|
|
124
|
-
return invalid('E_CHECKPOINT_INVALID', 'seq 0 checkpoint must carry the genesis hash');
|
|
125
|
-
}
|
|
126
|
-
if (checkpoint.lastSeq > 0 && checkpoint.lastHash === GENESIS_HASH) {
|
|
127
|
-
return invalid('E_CHECKPOINT_INVALID', 'non-empty checkpoint cannot carry the genesis hash');
|
|
128
|
-
}
|
|
129
|
-
if (!isPlainObject(checkpoint.state)) {
|
|
130
|
-
return invalid('E_CHECKPOINT_INVALID', 'checkpoint.state must be a plain object');
|
|
131
|
-
}
|
|
132
|
-
// state 自述水位必须与外层水位一致:hashMatches 只证明 lastHash 等于账本第
|
|
133
|
-
// lastSeq 条事件的 hash,并不覆盖 state 内容。state 被截断/错位而外层水位仍
|
|
134
|
-
// 有效时,openLedger 的齐平分支持零尾放采信错误投影。(只校验 seq:内容哈希
|
|
135
|
-
// 由外层 lastHash 链保证,避免遮蔽 E_CHECKPOINT_HASH 的诊断。)
|
|
136
|
-
const stateSeq = checkpoint.state.lastSeq;
|
|
137
|
-
if (Number.isSafeInteger(stateSeq) && stateSeq !== checkpoint.lastSeq) {
|
|
138
|
-
return invalid('E_CHECKPOINT_STATE_MISMATCH', 'checkpoint.state.lastSeq does not match the checkpoint watermark', {
|
|
139
|
-
expected: checkpoint.lastSeq,
|
|
140
|
-
actual: stateSeq,
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
return { ok: true };
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
function hashMatches(checkpoint, hashAt) {
|
|
147
|
-
if (typeof hashAt !== 'function') return true;
|
|
148
|
-
try {
|
|
149
|
-
const expected = checkpoint.lastSeq === 0 ? GENESIS_HASH : hashAt(checkpoint.lastSeq);
|
|
150
|
-
return typeof expected === 'string' && expected === checkpoint.lastHash;
|
|
151
|
-
} catch {
|
|
152
|
-
return false;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* 原子保存检查点,并把上一份有效检查点备份为 checkpoint.bak.json。
|
|
158
|
-
*
|
|
159
|
-
* @param {string} dir
|
|
160
|
-
* @param {{stateVersion:number,lastSeq:number,lastHash:string,state:object}} input
|
|
161
|
-
* @param {{fs?:object,hashAt?:(seq:number)=>string|undefined}} [options]
|
|
162
|
-
* hashAt 用于在备份前确认旧检查点仍与账本一致;不一致则保留更旧的备份。
|
|
163
|
-
* @returns {{path:string,bytes:number,backedUp:boolean,backupPath:string,backupError:string|null}}
|
|
164
|
-
*/
|
|
165
|
-
export function saveCheckpoint(dir, input, options = {}) {
|
|
166
|
-
const fs = options.fs ?? fsDefault;
|
|
167
|
-
if (typeof dir !== 'string' || dir.length === 0) {
|
|
168
|
-
throw new LedgerError('E_INVALID_OPTION', 'saveCheckpoint requires a directory');
|
|
169
|
-
}
|
|
170
|
-
if (!isPlainObject(input)) {
|
|
171
|
-
throw new LedgerError('E_CHECKPOINT_INVALID', 'saveCheckpoint expects a checkpoint object');
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
const checkpoint = {
|
|
175
|
-
v: CHECKPOINT_VERSION,
|
|
176
|
-
stateVersion: input.stateVersion,
|
|
177
|
-
lastSeq: input.lastSeq,
|
|
178
|
-
lastHash: input.lastHash,
|
|
179
|
-
state: input.state,
|
|
180
|
-
};
|
|
181
|
-
const validation = validateCheckpoint(checkpoint, { stateVersion: input.stateVersion });
|
|
182
|
-
if (!validation.ok) {
|
|
183
|
-
throw new LedgerError(validation.code, validation.message, validation.details);
|
|
184
|
-
}
|
|
185
|
-
// 调用方给了账本 hashAt 时,拒绝保存“检查点领先账本”的错误快照。
|
|
186
|
-
if (!hashMatches(checkpoint, options.hashAt)) {
|
|
187
|
-
throw new LedgerError('E_CHECKPOINT_HASH', 'checkpoint.lastHash does not match the ledger', {
|
|
188
|
-
lastSeq: checkpoint.lastSeq,
|
|
189
|
-
lastHash: checkpoint.lastHash,
|
|
190
|
-
});
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
let text;
|
|
194
|
-
try {
|
|
195
|
-
text = `${canonicalJson(checkpoint)}\n`;
|
|
196
|
-
} catch (error) {
|
|
197
|
-
throw new LedgerError('E_CHECKPOINT_INVALID', `checkpoint is not JSON serializable: ${error.message}`, {
|
|
198
|
-
cause: error.message,
|
|
199
|
-
});
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
try {
|
|
203
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
204
|
-
} catch (error) {
|
|
205
|
-
throw new LedgerError('E_STORAGE', `cannot create checkpoint directory: ${error.message}`, {
|
|
206
|
-
sysCode: error.code ?? null,
|
|
207
|
-
});
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
const primary = checkpointPath(dir);
|
|
211
|
-
const backup = checkpointBackupPath(dir);
|
|
212
|
-
const suffix = `${process.pid}.${Date.now()}.${randomBytes(4).toString('hex')}`;
|
|
213
|
-
const temp = path.join(dir, `.checkpoint.${suffix}.tmp`);
|
|
214
|
-
|
|
215
|
-
try {
|
|
216
|
-
writeAtomic(fs, temp, text);
|
|
217
|
-
} catch (error) {
|
|
218
|
-
throw new LedgerError('E_STORAGE', `cannot write checkpoint temp file: ${error.message}`, {
|
|
219
|
-
sysCode: error.code ?? null,
|
|
220
|
-
});
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
let backedUp = false;
|
|
224
|
-
let backupError = null;
|
|
225
|
-
if (fs.existsSync(primary)) {
|
|
226
|
-
try {
|
|
227
|
-
const previousText = fs.readFileSync(primary, 'utf8');
|
|
228
|
-
const previous = JSON.parse(previousText);
|
|
229
|
-
// 只有结构有效、且(给了 hashAt 时)与账本对得上的旧检查点才值得备份。
|
|
230
|
-
if (validateCheckpoint(previous).ok && hashMatches(previous, options.hashAt)) {
|
|
231
|
-
const backupTemp = path.join(dir, `.checkpoint-bak.${suffix}.tmp`);
|
|
232
|
-
writeAtomic(fs, backupTemp, previousText.endsWith('\n') ? previousText : `${previousText}\n`);
|
|
233
|
-
fs.renameSync(backupTemp, backup);
|
|
234
|
-
backedUp = true;
|
|
235
|
-
}
|
|
236
|
-
} catch (error) {
|
|
237
|
-
// 旧检查点损坏时不要覆盖已有的好备份。
|
|
238
|
-
backupError = error.message;
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
try {
|
|
243
|
-
fs.renameSync(temp, primary);
|
|
244
|
-
} catch (error) {
|
|
245
|
-
try {
|
|
246
|
-
fs.unlinkSync(temp);
|
|
247
|
-
} catch {
|
|
248
|
-
// 忽略清理失败。
|
|
249
|
-
}
|
|
250
|
-
throw new LedgerError('E_STORAGE', `cannot install checkpoint: ${error.message}`, {
|
|
251
|
-
sysCode: error.code ?? null,
|
|
252
|
-
});
|
|
253
|
-
}
|
|
254
|
-
fsyncDirectory(fs, dir);
|
|
255
|
-
|
|
256
|
-
return {
|
|
257
|
-
path: primary,
|
|
258
|
-
bytes: Buffer.byteLength(text, 'utf8'),
|
|
259
|
-
backedUp,
|
|
260
|
-
backupPath: backup,
|
|
261
|
-
backupError,
|
|
262
|
-
};
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
/**
|
|
266
|
-
* 加载检查点:先 primary,再 backup;每一步都校验结构与 lastHash。
|
|
267
|
-
* 不抛“损坏”异常(I/O 之外的损坏都记入 errors);调用方据 source=null 决定重放。
|
|
268
|
-
*
|
|
269
|
-
* @returns {{checkpoint:object|null,source:'primary'|'backup'|null,errors:object[]}}
|
|
270
|
-
*/
|
|
271
|
-
export function loadCheckpoint(dir, options = {}) {
|
|
272
|
-
const fs = options.fs ?? fsDefault;
|
|
273
|
-
const errors = [];
|
|
274
|
-
const sources = [
|
|
275
|
-
['primary', checkpointPath(dir)],
|
|
276
|
-
['backup', checkpointBackupPath(dir)],
|
|
277
|
-
];
|
|
278
|
-
|
|
279
|
-
for (const [source, file] of sources) {
|
|
280
|
-
let text;
|
|
281
|
-
try {
|
|
282
|
-
text = fs.readFileSync(file, 'utf8');
|
|
283
|
-
} catch (error) {
|
|
284
|
-
errors.push({
|
|
285
|
-
source,
|
|
286
|
-
file,
|
|
287
|
-
code: error.code ?? 'E_STORAGE',
|
|
288
|
-
reason: error.code === 'ENOENT' ? 'missing' : error.message,
|
|
289
|
-
});
|
|
290
|
-
continue;
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
let checkpoint;
|
|
294
|
-
try {
|
|
295
|
-
checkpoint = JSON.parse(text);
|
|
296
|
-
} catch (error) {
|
|
297
|
-
errors.push({
|
|
298
|
-
source,
|
|
299
|
-
file,
|
|
300
|
-
code: 'E_CHECKPOINT_PARSE',
|
|
301
|
-
reason: error.message,
|
|
302
|
-
});
|
|
303
|
-
continue;
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
const validation = validateCheckpoint(checkpoint, { stateVersion: options.stateVersion });
|
|
307
|
-
if (!validation.ok) {
|
|
308
|
-
errors.push({
|
|
309
|
-
source,
|
|
310
|
-
file,
|
|
311
|
-
code: validation.code,
|
|
312
|
-
reason: validation.message,
|
|
313
|
-
details: validation.details,
|
|
314
|
-
});
|
|
315
|
-
continue;
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
if (!hashMatches(checkpoint, options.hashAt)) {
|
|
319
|
-
errors.push({
|
|
320
|
-
source,
|
|
321
|
-
file,
|
|
322
|
-
code: 'E_CHECKPOINT_HASH',
|
|
323
|
-
reason: 'checkpoint.lastHash does not match the ledger at lastSeq',
|
|
324
|
-
details: {
|
|
325
|
-
lastSeq: checkpoint.lastSeq,
|
|
326
|
-
lastHash: checkpoint.lastHash,
|
|
327
|
-
},
|
|
328
|
-
});
|
|
329
|
-
continue;
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
return { checkpoint, source, errors };
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
return { checkpoint: null, source: null, errors };
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
/**
|
|
339
|
-
* 恢复:加载检查点并重放尾部;检查点不可用时从 seq 1 全量重放。
|
|
340
|
-
* 返回的 lastSeq/lastHash 以账本 head 为准,不从检查点“继承”。
|
|
341
|
-
*
|
|
342
|
-
* @param {{
|
|
343
|
-
* dir:string,
|
|
344
|
-
* writer:import('./writer.js').LedgerWriter,
|
|
345
|
-
* stateVersion:number,
|
|
346
|
-
* apply:(state:object,event:object)=>object,
|
|
347
|
-
* initialState:object,
|
|
348
|
-
* save?:boolean,
|
|
349
|
-
* fs?:object
|
|
350
|
-
* }} options
|
|
351
|
-
*/
|
|
352
|
-
export function recoverCheckpoint(options) {
|
|
353
|
-
const { dir, writer, stateVersion, apply, initialState: emptyState, save = false } = options;
|
|
354
|
-
const fs = options.fs ?? fsDefault;
|
|
355
|
-
if (!writer || typeof writer.replay !== 'function' || typeof writer.hashAt !== 'function') {
|
|
356
|
-
throw new LedgerError('E_INVALID_OPTION', 'recoverCheckpoint requires an open ledger writer');
|
|
357
|
-
}
|
|
358
|
-
if (typeof apply !== 'function') {
|
|
359
|
-
throw new LedgerError('E_INVALID_OPTION', 'recoverCheckpoint requires apply(state,event)');
|
|
360
|
-
}
|
|
361
|
-
if (!isPlainObject(emptyState)) {
|
|
362
|
-
throw new LedgerError('E_INVALID_OPTION', 'recoverCheckpoint requires an initialState object');
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
const loaded = loadCheckpoint(dir, {
|
|
366
|
-
fs,
|
|
367
|
-
stateVersion,
|
|
368
|
-
hashAt: (seq) => writer.hashAt(seq),
|
|
369
|
-
});
|
|
370
|
-
|
|
371
|
-
const checkpoint = loaded.checkpoint;
|
|
372
|
-
const fromSeq = checkpoint ? checkpoint.lastSeq + 1 : 1;
|
|
373
|
-
const startState = checkpoint ? checkpoint.state : emptyState;
|
|
374
|
-
const state = writer.replay(startState, apply, { fromSeq });
|
|
375
|
-
const replayed = Math.max(0, writer.head.seq - (fromSeq - 1));
|
|
376
|
-
// 重放到 head 之后,投影自述的水位必须等于 head:否则调用方拿到的 state 与
|
|
377
|
-
// 它自己的 lastSeq 不一致(saveCheckpoint 也会因此拒绝写盘)。
|
|
378
|
-
if (state.lastSeq !== writer.head.seq) {
|
|
379
|
-
state.lastSeq = writer.head.seq;
|
|
380
|
-
state.lastHash = writer.head.hash;
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
const result = {
|
|
384
|
-
state,
|
|
385
|
-
source: loaded.source ?? 'replay',
|
|
386
|
-
checkpoint,
|
|
387
|
-
fromSeq,
|
|
388
|
-
replayed,
|
|
389
|
-
lastSeq: writer.head.seq,
|
|
390
|
-
lastHash: writer.head.hash,
|
|
391
|
-
errors: loaded.errors,
|
|
392
|
-
saved: null,
|
|
393
|
-
};
|
|
394
|
-
|
|
395
|
-
if (save) {
|
|
396
|
-
// 恢复会把 state 重放到 head,因此写入 checkpoint 时水位必须与 state 自述
|
|
397
|
-
// 一致:这里显式把 lastSeq/lastHash 对齐到 writer.head,否则校验会用
|
|
398
|
-
// state 里的旧水位报 E_CHECKPOINT_STATE_MISMATCH(save:true 分支因此从未可用)。
|
|
399
|
-
result.saved = saveCheckpoint(
|
|
400
|
-
dir,
|
|
401
|
-
{
|
|
402
|
-
stateVersion,
|
|
403
|
-
lastSeq: writer.head.seq,
|
|
404
|
-
lastHash: writer.head.hash,
|
|
405
|
-
state: { ...state, lastSeq: writer.head.seq, lastHash: writer.head.hash },
|
|
406
|
-
},
|
|
407
|
-
{
|
|
408
|
-
fs,
|
|
409
|
-
hashAt: (seq) => writer.hashAt(seq),
|
|
410
|
-
},
|
|
411
|
-
);
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
return result;
|
|
415
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* 检查点:{ v, stateVersion, lastSeq, lastHash, state } 的原子保存/加载/备份/校验。
|
|
3
|
+
*
|
|
4
|
+
* 对应 03-架构设计报告 §6.4 / §6.5 / §6.7、附录 B §B.3.6-4:
|
|
5
|
+
* - 原子写:同目录 tmp → fsync → rename 覆盖 checkpoint.json。
|
|
6
|
+
* - 备份:覆盖前把上一份“有效”检查点复制为 checkpoint.bak.json。
|
|
7
|
+
* - 加载:JSON/结构/stateVersion/lastSeq+lastHash 全部校验;失败回退备份,
|
|
8
|
+
* 再失败由 recoverCheckpoint 从 seq 1 全量重放。
|
|
9
|
+
* - 检查点绝不领先账本:lastHash 必须等于账本中 lastSeq 行的事件 hash。
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import fsDefault from 'node:fs';
|
|
13
|
+
import path from 'node:path';
|
|
14
|
+
import { randomBytes } from 'node:crypto';
|
|
15
|
+
import {
|
|
16
|
+
GENESIS_HASH,
|
|
17
|
+
HASH_PATTERN,
|
|
18
|
+
LedgerError,
|
|
19
|
+
canonicalJson,
|
|
20
|
+
isPlainObject,
|
|
21
|
+
} from './events.js';
|
|
22
|
+
|
|
23
|
+
export const CHECKPOINT_VERSION = 1;
|
|
24
|
+
export const CHECKPOINT_FILE = 'checkpoint.json';
|
|
25
|
+
export const CHECKPOINT_BACKUP_FILE = 'checkpoint.bak.json';
|
|
26
|
+
|
|
27
|
+
/** <dataDir>/checkpoint.json */
|
|
28
|
+
export function checkpointPath(dir) {
|
|
29
|
+
return path.join(dir, CHECKPOINT_FILE);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** <dataDir>/checkpoint.bak.json */
|
|
33
|
+
export function checkpointBackupPath(dir) {
|
|
34
|
+
return path.join(dir, CHECKPOINT_BACKUP_FILE);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function invalid(code, message, details = {}) {
|
|
38
|
+
return { ok: false, code, message, details };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function writeAll(fs, fd, buffer) {
|
|
42
|
+
let offset = 0;
|
|
43
|
+
while (offset < buffer.length) {
|
|
44
|
+
const written = fs.writeSync(fd, buffer, offset, buffer.length - offset, null);
|
|
45
|
+
if (!Number.isFinite(written) || written <= 0) throw new Error('short write');
|
|
46
|
+
offset += written;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function writeAtomic(fs, filePath, text) {
|
|
51
|
+
const fd = fs.openSync(filePath, 'wx');
|
|
52
|
+
try {
|
|
53
|
+
writeAll(fs, fd, Buffer.from(text, 'utf8'));
|
|
54
|
+
fs.fsyncSync(fd);
|
|
55
|
+
} catch (error) {
|
|
56
|
+
try {
|
|
57
|
+
fs.closeSync(fd);
|
|
58
|
+
} catch {
|
|
59
|
+
// 忽略清理失败。
|
|
60
|
+
}
|
|
61
|
+
try {
|
|
62
|
+
fs.unlinkSync(filePath);
|
|
63
|
+
} catch {
|
|
64
|
+
// 忽略清理失败。
|
|
65
|
+
}
|
|
66
|
+
throw error;
|
|
67
|
+
}
|
|
68
|
+
fs.closeSync(fd);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function fsyncDirectory(fs, dir) {
|
|
72
|
+
// Windows 不支持对目录 fsync;这是 best-effort,不影响 rename 的原子性。
|
|
73
|
+
try {
|
|
74
|
+
const fd = fs.openSync(dir, 'r');
|
|
75
|
+
try {
|
|
76
|
+
fs.fsyncSync(fd);
|
|
77
|
+
} finally {
|
|
78
|
+
fs.closeSync(fd);
|
|
79
|
+
}
|
|
80
|
+
} catch {
|
|
81
|
+
// EPERM / EISDIR / EINVAL:忽略。
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* 校验检查点结构与版本。返回 {ok:true} 或 {ok:false, code, message, details}。
|
|
87
|
+
* options.stateVersion 给定时要与检查点一致(不一致即丢弃重放)。
|
|
88
|
+
*/
|
|
89
|
+
export function validateCheckpoint(checkpoint, options = {}) {
|
|
90
|
+
if (!isPlainObject(checkpoint)) {
|
|
91
|
+
return invalid('E_CHECKPOINT_INVALID', 'checkpoint must be a plain object');
|
|
92
|
+
}
|
|
93
|
+
if (checkpoint.v !== CHECKPOINT_VERSION) {
|
|
94
|
+
return invalid('E_CHECKPOINT_VERSION', 'unsupported checkpoint version', {
|
|
95
|
+
supported: CHECKPOINT_VERSION,
|
|
96
|
+
actual: checkpoint.v,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
if (!Number.isSafeInteger(checkpoint.stateVersion) || checkpoint.stateVersion < 1) {
|
|
100
|
+
return invalid('E_CHECKPOINT_INVALID', 'checkpoint.stateVersion must be a positive integer', {
|
|
101
|
+
stateVersion: checkpoint.stateVersion,
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
if (
|
|
105
|
+
options.stateVersion !== undefined &&
|
|
106
|
+
checkpoint.stateVersion !== options.stateVersion
|
|
107
|
+
) {
|
|
108
|
+
return invalid('E_CHECKPOINT_STATE_VERSION', 'checkpoint stateVersion does not match the fold', {
|
|
109
|
+
expected: options.stateVersion,
|
|
110
|
+
actual: checkpoint.stateVersion,
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
if (!Number.isSafeInteger(checkpoint.lastSeq) || checkpoint.lastSeq < 0) {
|
|
114
|
+
return invalid('E_CHECKPOINT_INVALID', 'checkpoint.lastSeq must be a non-negative integer', {
|
|
115
|
+
lastSeq: checkpoint.lastSeq,
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
if (typeof checkpoint.lastHash !== 'string' || !HASH_PATTERN.test(checkpoint.lastHash)) {
|
|
119
|
+
return invalid('E_CHECKPOINT_INVALID', 'checkpoint.lastHash must be 64 lowercase hex chars', {
|
|
120
|
+
lastHash: checkpoint.lastHash,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
if (checkpoint.lastSeq === 0 && checkpoint.lastHash !== GENESIS_HASH) {
|
|
124
|
+
return invalid('E_CHECKPOINT_INVALID', 'seq 0 checkpoint must carry the genesis hash');
|
|
125
|
+
}
|
|
126
|
+
if (checkpoint.lastSeq > 0 && checkpoint.lastHash === GENESIS_HASH) {
|
|
127
|
+
return invalid('E_CHECKPOINT_INVALID', 'non-empty checkpoint cannot carry the genesis hash');
|
|
128
|
+
}
|
|
129
|
+
if (!isPlainObject(checkpoint.state)) {
|
|
130
|
+
return invalid('E_CHECKPOINT_INVALID', 'checkpoint.state must be a plain object');
|
|
131
|
+
}
|
|
132
|
+
// state 自述水位必须与外层水位一致:hashMatches 只证明 lastHash 等于账本第
|
|
133
|
+
// lastSeq 条事件的 hash,并不覆盖 state 内容。state 被截断/错位而外层水位仍
|
|
134
|
+
// 有效时,openLedger 的齐平分支持零尾放采信错误投影。(只校验 seq:内容哈希
|
|
135
|
+
// 由外层 lastHash 链保证,避免遮蔽 E_CHECKPOINT_HASH 的诊断。)
|
|
136
|
+
const stateSeq = checkpoint.state.lastSeq;
|
|
137
|
+
if (Number.isSafeInteger(stateSeq) && stateSeq !== checkpoint.lastSeq) {
|
|
138
|
+
return invalid('E_CHECKPOINT_STATE_MISMATCH', 'checkpoint.state.lastSeq does not match the checkpoint watermark', {
|
|
139
|
+
expected: checkpoint.lastSeq,
|
|
140
|
+
actual: stateSeq,
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
return { ok: true };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function hashMatches(checkpoint, hashAt) {
|
|
147
|
+
if (typeof hashAt !== 'function') return true;
|
|
148
|
+
try {
|
|
149
|
+
const expected = checkpoint.lastSeq === 0 ? GENESIS_HASH : hashAt(checkpoint.lastSeq);
|
|
150
|
+
return typeof expected === 'string' && expected === checkpoint.lastHash;
|
|
151
|
+
} catch {
|
|
152
|
+
return false;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* 原子保存检查点,并把上一份有效检查点备份为 checkpoint.bak.json。
|
|
158
|
+
*
|
|
159
|
+
* @param {string} dir
|
|
160
|
+
* @param {{stateVersion:number,lastSeq:number,lastHash:string,state:object}} input
|
|
161
|
+
* @param {{fs?:object,hashAt?:(seq:number)=>string|undefined}} [options]
|
|
162
|
+
* hashAt 用于在备份前确认旧检查点仍与账本一致;不一致则保留更旧的备份。
|
|
163
|
+
* @returns {{path:string,bytes:number,backedUp:boolean,backupPath:string,backupError:string|null}}
|
|
164
|
+
*/
|
|
165
|
+
export function saveCheckpoint(dir, input, options = {}) {
|
|
166
|
+
const fs = options.fs ?? fsDefault;
|
|
167
|
+
if (typeof dir !== 'string' || dir.length === 0) {
|
|
168
|
+
throw new LedgerError('E_INVALID_OPTION', 'saveCheckpoint requires a directory');
|
|
169
|
+
}
|
|
170
|
+
if (!isPlainObject(input)) {
|
|
171
|
+
throw new LedgerError('E_CHECKPOINT_INVALID', 'saveCheckpoint expects a checkpoint object');
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const checkpoint = {
|
|
175
|
+
v: CHECKPOINT_VERSION,
|
|
176
|
+
stateVersion: input.stateVersion,
|
|
177
|
+
lastSeq: input.lastSeq,
|
|
178
|
+
lastHash: input.lastHash,
|
|
179
|
+
state: input.state,
|
|
180
|
+
};
|
|
181
|
+
const validation = validateCheckpoint(checkpoint, { stateVersion: input.stateVersion });
|
|
182
|
+
if (!validation.ok) {
|
|
183
|
+
throw new LedgerError(validation.code, validation.message, validation.details);
|
|
184
|
+
}
|
|
185
|
+
// 调用方给了账本 hashAt 时,拒绝保存“检查点领先账本”的错误快照。
|
|
186
|
+
if (!hashMatches(checkpoint, options.hashAt)) {
|
|
187
|
+
throw new LedgerError('E_CHECKPOINT_HASH', 'checkpoint.lastHash does not match the ledger', {
|
|
188
|
+
lastSeq: checkpoint.lastSeq,
|
|
189
|
+
lastHash: checkpoint.lastHash,
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
let text;
|
|
194
|
+
try {
|
|
195
|
+
text = `${canonicalJson(checkpoint)}\n`;
|
|
196
|
+
} catch (error) {
|
|
197
|
+
throw new LedgerError('E_CHECKPOINT_INVALID', `checkpoint is not JSON serializable: ${error.message}`, {
|
|
198
|
+
cause: error.message,
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
try {
|
|
203
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
204
|
+
} catch (error) {
|
|
205
|
+
throw new LedgerError('E_STORAGE', `cannot create checkpoint directory: ${error.message}`, {
|
|
206
|
+
sysCode: error.code ?? null,
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const primary = checkpointPath(dir);
|
|
211
|
+
const backup = checkpointBackupPath(dir);
|
|
212
|
+
const suffix = `${process.pid}.${Date.now()}.${randomBytes(4).toString('hex')}`;
|
|
213
|
+
const temp = path.join(dir, `.checkpoint.${suffix}.tmp`);
|
|
214
|
+
|
|
215
|
+
try {
|
|
216
|
+
writeAtomic(fs, temp, text);
|
|
217
|
+
} catch (error) {
|
|
218
|
+
throw new LedgerError('E_STORAGE', `cannot write checkpoint temp file: ${error.message}`, {
|
|
219
|
+
sysCode: error.code ?? null,
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
let backedUp = false;
|
|
224
|
+
let backupError = null;
|
|
225
|
+
if (fs.existsSync(primary)) {
|
|
226
|
+
try {
|
|
227
|
+
const previousText = fs.readFileSync(primary, 'utf8');
|
|
228
|
+
const previous = JSON.parse(previousText);
|
|
229
|
+
// 只有结构有效、且(给了 hashAt 时)与账本对得上的旧检查点才值得备份。
|
|
230
|
+
if (validateCheckpoint(previous).ok && hashMatches(previous, options.hashAt)) {
|
|
231
|
+
const backupTemp = path.join(dir, `.checkpoint-bak.${suffix}.tmp`);
|
|
232
|
+
writeAtomic(fs, backupTemp, previousText.endsWith('\n') ? previousText : `${previousText}\n`);
|
|
233
|
+
fs.renameSync(backupTemp, backup);
|
|
234
|
+
backedUp = true;
|
|
235
|
+
}
|
|
236
|
+
} catch (error) {
|
|
237
|
+
// 旧检查点损坏时不要覆盖已有的好备份。
|
|
238
|
+
backupError = error.message;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
try {
|
|
243
|
+
fs.renameSync(temp, primary);
|
|
244
|
+
} catch (error) {
|
|
245
|
+
try {
|
|
246
|
+
fs.unlinkSync(temp);
|
|
247
|
+
} catch {
|
|
248
|
+
// 忽略清理失败。
|
|
249
|
+
}
|
|
250
|
+
throw new LedgerError('E_STORAGE', `cannot install checkpoint: ${error.message}`, {
|
|
251
|
+
sysCode: error.code ?? null,
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
fsyncDirectory(fs, dir);
|
|
255
|
+
|
|
256
|
+
return {
|
|
257
|
+
path: primary,
|
|
258
|
+
bytes: Buffer.byteLength(text, 'utf8'),
|
|
259
|
+
backedUp,
|
|
260
|
+
backupPath: backup,
|
|
261
|
+
backupError,
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* 加载检查点:先 primary,再 backup;每一步都校验结构与 lastHash。
|
|
267
|
+
* 不抛“损坏”异常(I/O 之外的损坏都记入 errors);调用方据 source=null 决定重放。
|
|
268
|
+
*
|
|
269
|
+
* @returns {{checkpoint:object|null,source:'primary'|'backup'|null,errors:object[]}}
|
|
270
|
+
*/
|
|
271
|
+
export function loadCheckpoint(dir, options = {}) {
|
|
272
|
+
const fs = options.fs ?? fsDefault;
|
|
273
|
+
const errors = [];
|
|
274
|
+
const sources = [
|
|
275
|
+
['primary', checkpointPath(dir)],
|
|
276
|
+
['backup', checkpointBackupPath(dir)],
|
|
277
|
+
];
|
|
278
|
+
|
|
279
|
+
for (const [source, file] of sources) {
|
|
280
|
+
let text;
|
|
281
|
+
try {
|
|
282
|
+
text = fs.readFileSync(file, 'utf8');
|
|
283
|
+
} catch (error) {
|
|
284
|
+
errors.push({
|
|
285
|
+
source,
|
|
286
|
+
file,
|
|
287
|
+
code: error.code ?? 'E_STORAGE',
|
|
288
|
+
reason: error.code === 'ENOENT' ? 'missing' : error.message,
|
|
289
|
+
});
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
let checkpoint;
|
|
294
|
+
try {
|
|
295
|
+
checkpoint = JSON.parse(text);
|
|
296
|
+
} catch (error) {
|
|
297
|
+
errors.push({
|
|
298
|
+
source,
|
|
299
|
+
file,
|
|
300
|
+
code: 'E_CHECKPOINT_PARSE',
|
|
301
|
+
reason: error.message,
|
|
302
|
+
});
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
const validation = validateCheckpoint(checkpoint, { stateVersion: options.stateVersion });
|
|
307
|
+
if (!validation.ok) {
|
|
308
|
+
errors.push({
|
|
309
|
+
source,
|
|
310
|
+
file,
|
|
311
|
+
code: validation.code,
|
|
312
|
+
reason: validation.message,
|
|
313
|
+
details: validation.details,
|
|
314
|
+
});
|
|
315
|
+
continue;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
if (!hashMatches(checkpoint, options.hashAt)) {
|
|
319
|
+
errors.push({
|
|
320
|
+
source,
|
|
321
|
+
file,
|
|
322
|
+
code: 'E_CHECKPOINT_HASH',
|
|
323
|
+
reason: 'checkpoint.lastHash does not match the ledger at lastSeq',
|
|
324
|
+
details: {
|
|
325
|
+
lastSeq: checkpoint.lastSeq,
|
|
326
|
+
lastHash: checkpoint.lastHash,
|
|
327
|
+
},
|
|
328
|
+
});
|
|
329
|
+
continue;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
return { checkpoint, source, errors };
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
return { checkpoint: null, source: null, errors };
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* 恢复:加载检查点并重放尾部;检查点不可用时从 seq 1 全量重放。
|
|
340
|
+
* 返回的 lastSeq/lastHash 以账本 head 为准,不从检查点“继承”。
|
|
341
|
+
*
|
|
342
|
+
* @param {{
|
|
343
|
+
* dir:string,
|
|
344
|
+
* writer:import('./writer.js').LedgerWriter,
|
|
345
|
+
* stateVersion:number,
|
|
346
|
+
* apply:(state:object,event:object)=>object,
|
|
347
|
+
* initialState:object,
|
|
348
|
+
* save?:boolean,
|
|
349
|
+
* fs?:object
|
|
350
|
+
* }} options
|
|
351
|
+
*/
|
|
352
|
+
export function recoverCheckpoint(options) {
|
|
353
|
+
const { dir, writer, stateVersion, apply, initialState: emptyState, save = false } = options;
|
|
354
|
+
const fs = options.fs ?? fsDefault;
|
|
355
|
+
if (!writer || typeof writer.replay !== 'function' || typeof writer.hashAt !== 'function') {
|
|
356
|
+
throw new LedgerError('E_INVALID_OPTION', 'recoverCheckpoint requires an open ledger writer');
|
|
357
|
+
}
|
|
358
|
+
if (typeof apply !== 'function') {
|
|
359
|
+
throw new LedgerError('E_INVALID_OPTION', 'recoverCheckpoint requires apply(state,event)');
|
|
360
|
+
}
|
|
361
|
+
if (!isPlainObject(emptyState)) {
|
|
362
|
+
throw new LedgerError('E_INVALID_OPTION', 'recoverCheckpoint requires an initialState object');
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
const loaded = loadCheckpoint(dir, {
|
|
366
|
+
fs,
|
|
367
|
+
stateVersion,
|
|
368
|
+
hashAt: (seq) => writer.hashAt(seq),
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
const checkpoint = loaded.checkpoint;
|
|
372
|
+
const fromSeq = checkpoint ? checkpoint.lastSeq + 1 : 1;
|
|
373
|
+
const startState = checkpoint ? checkpoint.state : emptyState;
|
|
374
|
+
const state = writer.replay(startState, apply, { fromSeq });
|
|
375
|
+
const replayed = Math.max(0, writer.head.seq - (fromSeq - 1));
|
|
376
|
+
// 重放到 head 之后,投影自述的水位必须等于 head:否则调用方拿到的 state 与
|
|
377
|
+
// 它自己的 lastSeq 不一致(saveCheckpoint 也会因此拒绝写盘)。
|
|
378
|
+
if (state.lastSeq !== writer.head.seq) {
|
|
379
|
+
state.lastSeq = writer.head.seq;
|
|
380
|
+
state.lastHash = writer.head.hash;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
const result = {
|
|
384
|
+
state,
|
|
385
|
+
source: loaded.source ?? 'replay',
|
|
386
|
+
checkpoint,
|
|
387
|
+
fromSeq,
|
|
388
|
+
replayed,
|
|
389
|
+
lastSeq: writer.head.seq,
|
|
390
|
+
lastHash: writer.head.hash,
|
|
391
|
+
errors: loaded.errors,
|
|
392
|
+
saved: null,
|
|
393
|
+
};
|
|
394
|
+
|
|
395
|
+
if (save) {
|
|
396
|
+
// 恢复会把 state 重放到 head,因此写入 checkpoint 时水位必须与 state 自述
|
|
397
|
+
// 一致:这里显式把 lastSeq/lastHash 对齐到 writer.head,否则校验会用
|
|
398
|
+
// state 里的旧水位报 E_CHECKPOINT_STATE_MISMATCH(save:true 分支因此从未可用)。
|
|
399
|
+
result.saved = saveCheckpoint(
|
|
400
|
+
dir,
|
|
401
|
+
{
|
|
402
|
+
stateVersion,
|
|
403
|
+
lastSeq: writer.head.seq,
|
|
404
|
+
lastHash: writer.head.hash,
|
|
405
|
+
state: { ...state, lastSeq: writer.head.seq, lastHash: writer.head.hash },
|
|
406
|
+
},
|
|
407
|
+
{
|
|
408
|
+
fs,
|
|
409
|
+
hashAt: (seq) => writer.hashAt(seq),
|
|
410
|
+
},
|
|
411
|
+
);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
return result;
|
|
415
|
+
}
|