@suifei/dsh-ai-team 0.4.145 → 0.4.146
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 +5 -0
- package/LICENSE +21 -21
- package/README.md +19 -1
- package/client.js +118 -17
- package/cordis.patch.yml +31 -31
- package/icon.svg +12 -12
- package/index.js +7 -1
- package/lib/domain/ids.js +26 -26
- package/lib/domain/inbox.js +93 -93
- package/lib/domain/member-config.js +168 -168
- package/lib/host/decision-report.js +72 -72
- package/lib/host/default-workspace.js +69 -69
- package/lib/host/fault-adapter.js +50 -50
- 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/risk-view.js +99 -99
- 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/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/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/events.js
CHANGED
|
@@ -1,398 +1,398 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 账本事件信封、canonical JSON、哈希链与 ID 生成。
|
|
3
|
-
*
|
|
4
|
-
* 对应 03-架构设计报告 §6.2 / §6.5:
|
|
5
|
-
* - 每行一个 JSON 对象,字段固定为 ENVELOPE_FIELDS。
|
|
6
|
-
* - hash = sha256(canonicalJson(除 hash 之外的全部字段)),小写 hex。
|
|
7
|
-
* - prevHash 指向前一事件的 hash;创世为 64 个 "0"。
|
|
8
|
-
* - id 为 ULID(时间有序),外部引用用 id,排序用 seq。
|
|
9
|
-
*
|
|
10
|
-
* 本文件不做 I/O,也不理解事件语义;writer / fold / checkpoint 共用这里的
|
|
11
|
-
* 序列化与校验规则,避免同一规则出现第二份实现。
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
import { createHash, randomBytes } from 'node:crypto';
|
|
15
|
-
|
|
16
|
-
/** 信封结构版本。读到更高版本时由 writer 拒绝并提示升级。 */
|
|
17
|
-
export const EVENT_VERSION = 1;
|
|
18
|
-
|
|
19
|
-
/** 创世前一事件的 hash(64 个 0)。 */
|
|
20
|
-
export const GENESIS_HASH = '0'.repeat(64);
|
|
21
|
-
|
|
22
|
-
/** 单事件上限 256 KB(含行尾 \n,见 03 §6.2)。 */
|
|
23
|
-
export const MAX_EVENT_BYTES = 256 * 1024;
|
|
24
|
-
|
|
25
|
-
/** hash / prevHash 的格式:小写 sha256 hex。 */
|
|
26
|
-
export const HASH_PATTERN = /^[0-9a-f]{64}$/;
|
|
27
|
-
|
|
28
|
-
/** 信封字段顺序(也是 JSON 示例中的展示顺序)。 */
|
|
29
|
-
export const ENVELOPE_FIELDS = Object.freeze([
|
|
30
|
-
'v',
|
|
31
|
-
'id',
|
|
32
|
-
'projectId',
|
|
33
|
-
'seq',
|
|
34
|
-
'type',
|
|
35
|
-
'at',
|
|
36
|
-
'actor',
|
|
37
|
-
'correlationId',
|
|
38
|
-
'causationId',
|
|
39
|
-
'payload',
|
|
40
|
-
'prevHash',
|
|
41
|
-
'hash',
|
|
42
|
-
]);
|
|
43
|
-
|
|
44
|
-
const ENVELOPE_FIELD_SET = new Set(ENVELOPE_FIELDS);
|
|
45
|
-
const ACTOR_KINDS = new Set(['human', 'agent', 'system']);
|
|
46
|
-
const ULID_ALPHABET = '0123456789ABCDEFGHJKMNPQRSTVWXYZ';
|
|
47
|
-
const ID_PREFIX_PATTERN = /^[a-z][a-z0-9]*_$/;
|
|
48
|
-
const EVENT_TYPE_PATTERN = /^[a-z][a-z0-9_-]*\/[a-z][a-z0-9_-]*$/;
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* 全账本统一的错误类型。
|
|
52
|
-
* code 是稳定的错误码(E_*),details 只放可序列化的诊断信息。
|
|
53
|
-
*/
|
|
54
|
-
export class LedgerError extends Error {
|
|
55
|
-
constructor(code, message, details = {}) {
|
|
56
|
-
super(message);
|
|
57
|
-
this.name = 'LedgerError';
|
|
58
|
-
this.code = code;
|
|
59
|
-
this.details = details;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/** 是否为普通对象(拒绝 Date / Map / class 实例,保证 canonical JSON 无歧义)。 */
|
|
64
|
-
export function isPlainObject(value) {
|
|
65
|
-
if (value === null || typeof value !== 'object') return false;
|
|
66
|
-
const proto = Object.getPrototypeOf(value);
|
|
67
|
-
return proto === Object.prototype || proto === null;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function fail(code, message, details = {}) {
|
|
71
|
-
return { ok: false, code, message, details };
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* 确定性 JSON(canonical JSON):
|
|
76
|
-
* - 对象键按 UTF-16 码元序升序;数组保持原序。
|
|
77
|
-
* - 对象中值为 undefined 的键按 JSON 语义省略;数组中 undefined 变 null。
|
|
78
|
-
* - 拒绝 NaN / Infinity / BigInt / 函数 / Symbol / 非普通对象 / 循环引用。
|
|
79
|
-
* - 字符串使用 JSON.stringify 的转义,输出 UTF-8 文本。
|
|
80
|
-
*/
|
|
81
|
-
export function canonicalJson(value) {
|
|
82
|
-
const stack = new Set();
|
|
83
|
-
|
|
84
|
-
const encode = (input) => {
|
|
85
|
-
if (input === null) return 'null';
|
|
86
|
-
|
|
87
|
-
const type = typeof input;
|
|
88
|
-
if (type === 'string') return JSON.stringify(input);
|
|
89
|
-
if (type === 'boolean') return input ? 'true' : 'false';
|
|
90
|
-
if (type === 'number') {
|
|
91
|
-
if (!Number.isFinite(input)) {
|
|
92
|
-
throw new TypeError('canonicalJson: non-finite numbers are not allowed');
|
|
93
|
-
}
|
|
94
|
-
return JSON.stringify(input);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
if (type !== 'object') {
|
|
98
|
-
throw new TypeError(`canonicalJson: unsupported value type "${type}"`);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
if (stack.has(input)) {
|
|
102
|
-
throw new TypeError('canonicalJson: circular reference');
|
|
103
|
-
}
|
|
104
|
-
stack.add(input);
|
|
105
|
-
try {
|
|
106
|
-
if (Array.isArray(input)) {
|
|
107
|
-
const parts = input.map((item) => (item === undefined ? 'null' : encode(item)));
|
|
108
|
-
return `[${parts.join(',')}]`;
|
|
109
|
-
}
|
|
110
|
-
if (!isPlainObject(input)) {
|
|
111
|
-
throw new TypeError('canonicalJson: only plain objects and arrays are allowed');
|
|
112
|
-
}
|
|
113
|
-
const keys = Object.keys(input)
|
|
114
|
-
.filter((key) => input[key] !== undefined)
|
|
115
|
-
.sort();
|
|
116
|
-
const parts = keys.map((key) => `${JSON.stringify(key)}:${encode(input[key])}`);
|
|
117
|
-
return `{${parts.join(',')}}`;
|
|
118
|
-
} finally {
|
|
119
|
-
stack.delete(input);
|
|
120
|
-
}
|
|
121
|
-
};
|
|
122
|
-
|
|
123
|
-
return encode(value);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/** sha256(text) 的小写 hex。 */
|
|
127
|
-
export function sha256Hex(text) {
|
|
128
|
-
return createHash('sha256').update(text, 'utf8').digest('hex');
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* 计算事件 hash:对除 hash 外的全部字段取 canonical JSON 后 sha256。
|
|
133
|
-
* 注意:不校验信封形状,只负责摘要;形状校验在 verifyEvent / sealEvent。
|
|
134
|
-
*/
|
|
135
|
-
export function hashEvent(event) {
|
|
136
|
-
if (!isPlainObject(event)) {
|
|
137
|
-
throw new TypeError('hashEvent: event must be a plain object');
|
|
138
|
-
}
|
|
139
|
-
const { hash: _ignored, ...rest } = event;
|
|
140
|
-
return sha256Hex(canonicalJson(rest));
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
function normalizeActor(actor) {
|
|
144
|
-
if (!isPlainObject(actor)) {
|
|
145
|
-
throw new LedgerError('E_EVENT_ACTOR', 'event.actor must be a plain object');
|
|
146
|
-
}
|
|
147
|
-
for (const key of Object.keys(actor)) {
|
|
148
|
-
if (key !== 'kind' && key !== 'memberId') {
|
|
149
|
-
throw new LedgerError('E_EVENT_ACTOR', `event.actor has unknown field "${key}"`, { field: key });
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
if (!ACTOR_KINDS.has(actor.kind)) {
|
|
153
|
-
throw new LedgerError('E_EVENT_ACTOR', 'event.actor.kind must be human | agent | system', {
|
|
154
|
-
kind: actor.kind,
|
|
155
|
-
});
|
|
156
|
-
}
|
|
157
|
-
if (actor.kind === 'agent' && (typeof actor.memberId !== 'string' || actor.memberId.length === 0)) {
|
|
158
|
-
throw new LedgerError('E_EVENT_ACTOR', 'agent actor requires a non-empty memberId');
|
|
159
|
-
}
|
|
160
|
-
if (actor.memberId !== undefined && typeof actor.memberId !== 'string') {
|
|
161
|
-
throw new LedgerError('E_EVENT_ACTOR', 'event.actor.memberId must be a string when present');
|
|
162
|
-
}
|
|
163
|
-
return actor.memberId === undefined
|
|
164
|
-
? { kind: actor.kind }
|
|
165
|
-
: { kind: actor.kind, memberId: actor.memberId };
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
function validateEnvelope(event) {
|
|
169
|
-
if (!isPlainObject(event)) return fail('E_EVENT_SHAPE', 'event must be a plain object');
|
|
170
|
-
|
|
171
|
-
for (const key of Object.keys(event)) {
|
|
172
|
-
if (!ENVELOPE_FIELD_SET.has(key)) {
|
|
173
|
-
return fail('E_EVENT_FIELD', `event has unknown field "${key}"`, { field: key });
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
for (const key of ENVELOPE_FIELDS) {
|
|
177
|
-
if (!(key in event)) {
|
|
178
|
-
return fail('E_EVENT_FIELD', `event is missing field "${key}"`, { field: key });
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
if (!Number.isInteger(event.v) || event.v < 1) {
|
|
183
|
-
return fail('E_EVENT_VERSION', 'event.v must be a positive integer', { v: event.v });
|
|
184
|
-
}
|
|
185
|
-
if (event.v !== EVENT_VERSION) {
|
|
186
|
-
return fail('E_EVENT_VERSION', `unsupported event version ${event.v}`, {
|
|
187
|
-
supported: EVENT_VERSION,
|
|
188
|
-
actual: event.v,
|
|
189
|
-
});
|
|
190
|
-
}
|
|
191
|
-
if (typeof event.id !== 'string' || event.id.length === 0) {
|
|
192
|
-
return fail('E_EVENT_ID', 'event.id must be a non-empty string');
|
|
193
|
-
}
|
|
194
|
-
if (typeof event.projectId !== 'string' || event.projectId.length === 0) {
|
|
195
|
-
return fail('E_EVENT_PROJECT', 'event.projectId must be a non-empty string');
|
|
196
|
-
}
|
|
197
|
-
if (!Number.isSafeInteger(event.seq) || event.seq < 1) {
|
|
198
|
-
return fail('E_EVENT_SEQ', 'event.seq must be a positive safe integer', { seq: event.seq });
|
|
199
|
-
}
|
|
200
|
-
if (typeof event.type !== 'string' || !EVENT_TYPE_PATTERN.test(event.type)) {
|
|
201
|
-
return fail('E_EVENT_TYPE', 'event.type must look like "<domain>/<name>"', { type: event.type });
|
|
202
|
-
}
|
|
203
|
-
if (!Number.isSafeInteger(event.at) || event.at < 0) {
|
|
204
|
-
return fail('E_EVENT_AT', 'event.at must be a non-negative safe integer (epoch ms)', { at: event.at });
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
try {
|
|
208
|
-
normalizeActor(event.actor);
|
|
209
|
-
} catch (error) {
|
|
210
|
-
return fail(error.code ?? 'E_EVENT_ACTOR', error.message, error.details ?? {});
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
if (event.correlationId !== null && typeof event.correlationId !== 'string') {
|
|
214
|
-
return fail('E_EVENT_CORRELATION', 'event.correlationId must be a string or null');
|
|
215
|
-
}
|
|
216
|
-
if (event.causationId !== null && typeof event.causationId !== 'string') {
|
|
217
|
-
return fail('E_EVENT_CAUSATION', 'event.causationId must be a string or null');
|
|
218
|
-
}
|
|
219
|
-
if (!isPlainObject(event.payload)) {
|
|
220
|
-
return fail('E_EVENT_PAYLOAD', 'event.payload must be a plain object');
|
|
221
|
-
}
|
|
222
|
-
if (typeof event.prevHash !== 'string' || !HASH_PATTERN.test(event.prevHash)) {
|
|
223
|
-
return fail('E_EVENT_PREV_HASH', 'event.prevHash must be 64 lowercase hex chars', {
|
|
224
|
-
prevHash: event.prevHash,
|
|
225
|
-
});
|
|
226
|
-
}
|
|
227
|
-
if (typeof event.hash !== 'string' || !HASH_PATTERN.test(event.hash)) {
|
|
228
|
-
return fail('E_EVENT_HASH', 'event.hash must be 64 lowercase hex chars', { hash: event.hash });
|
|
229
|
-
}
|
|
230
|
-
return { ok: true };
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
/**
|
|
234
|
-
* 生成 ULID 风格的 ID:`<prefix><10 字符时间><16 字符随机>`。
|
|
235
|
-
* 前缀自带下划线(`evt_` / `cmd_` / `mem_`),时间部分为 48-bit 毫秒,
|
|
236
|
-
* Crockford Base32;随机部分 80-bit。
|
|
237
|
-
*/
|
|
238
|
-
export function newId(prefix, now = Date.now()) {
|
|
239
|
-
if (typeof prefix !== 'string' || !ID_PREFIX_PATTERN.test(prefix)) {
|
|
240
|
-
throw new LedgerError('E_ID_PREFIX', 'id prefix must match /^[a-z][a-z0-9]*_$/', { prefix });
|
|
241
|
-
}
|
|
242
|
-
if (!Number.isSafeInteger(now) || now < 0 || now >= 2 ** 48) {
|
|
243
|
-
throw new LedgerError('E_ID_TIME', 'id timestamp must be a 48-bit non-negative integer', { now });
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
let time = now;
|
|
247
|
-
const timeChars = new Array(10);
|
|
248
|
-
for (let index = 9; index >= 0; index -= 1) {
|
|
249
|
-
timeChars[index] = ULID_ALPHABET[time % 32];
|
|
250
|
-
time = Math.floor(time / 32);
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
// 256 % 32 === 0,按字节取低 5 bit 无模偏差。
|
|
254
|
-
const bytes = randomBytes(16);
|
|
255
|
-
let randomChars = '';
|
|
256
|
-
for (let index = 0; index < 16; index += 1) {
|
|
257
|
-
randomChars += ULID_ALPHABET[bytes[index] % 32];
|
|
258
|
-
}
|
|
259
|
-
return `${prefix}${timeChars.join('')}${randomChars}`;
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
/** 生成事件 ID(`evt_` 前缀)。 */
|
|
263
|
-
export function newEventId(now = Date.now()) {
|
|
264
|
-
return newId('evt_', now);
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
/**
|
|
268
|
-
* 封事件:补默认字段、计算 hash,并返回“与落盘字节一致”的普通对象。
|
|
269
|
-
*
|
|
270
|
-
* 返回值经过 canonical JSON 往返,因此 undefined 字段已被省略 —— 内存中的
|
|
271
|
-
* 事件与重新读盘得到的事件逐字段相等,避免检查点状态出现 JSON 往返差异。
|
|
272
|
-
*
|
|
273
|
-
* @param {object} fields 除 hash 外的信封字段;id/at/v/correlationId/causationId/payload/prevHash 可缺省。
|
|
274
|
-
* @returns {object} 带 hash 的事件信封。
|
|
275
|
-
*/
|
|
276
|
-
export function sealEvent(fields = {}) {
|
|
277
|
-
if (!isPlainObject(fields)) {
|
|
278
|
-
throw new LedgerError('E_EVENT_SHAPE', 'sealEvent expects a plain object');
|
|
279
|
-
}
|
|
280
|
-
if ('hash' in fields) {
|
|
281
|
-
throw new LedgerError('E_EVENT_HASH', 'sealEvent must not receive a hash; it is computed here');
|
|
282
|
-
}
|
|
283
|
-
for (const key of Object.keys(fields)) {
|
|
284
|
-
if (!ENVELOPE_FIELD_SET.has(key) || key === 'hash') {
|
|
285
|
-
throw new LedgerError('E_EVENT_FIELD', `sealEvent received unknown field "${key}"`, { field: key });
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
const at = fields.at ?? Date.now();
|
|
290
|
-
const envelope = {
|
|
291
|
-
v: fields.v ?? EVENT_VERSION,
|
|
292
|
-
id: fields.id ?? newEventId(at),
|
|
293
|
-
projectId: fields.projectId,
|
|
294
|
-
seq: fields.seq,
|
|
295
|
-
type: fields.type,
|
|
296
|
-
at,
|
|
297
|
-
actor: normalizeActor(fields.actor),
|
|
298
|
-
correlationId: fields.correlationId ?? null,
|
|
299
|
-
causationId: fields.causationId ?? null,
|
|
300
|
-
payload: fields.payload === undefined ? {} : fields.payload,
|
|
301
|
-
prevHash: fields.prevHash ?? GENESIS_HASH,
|
|
302
|
-
};
|
|
303
|
-
|
|
304
|
-
let event;
|
|
305
|
-
try {
|
|
306
|
-
const hash = hashEvent(envelope);
|
|
307
|
-
event = JSON.parse(canonicalJson({ ...envelope, hash }));
|
|
308
|
-
} catch (error) {
|
|
309
|
-
throw new LedgerError('E_EVENT_INVALID', `event is not canonical-JSON serializable: ${error.message}`, {
|
|
310
|
-
cause: error.message,
|
|
311
|
-
});
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
const check = validateEnvelope(event);
|
|
315
|
-
if (!check.ok) {
|
|
316
|
-
throw new LedgerError(check.code, check.message, check.details);
|
|
317
|
-
}
|
|
318
|
-
return event;
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
/**
|
|
322
|
-
* 校验单个事件的信封与 hash(可同时校验链接位置)。
|
|
323
|
-
* 返回 {ok:true} 或 {ok:false, code, message, details};不抛异常。
|
|
324
|
-
*/
|
|
325
|
-
export function verifyEvent(event, options = {}) {
|
|
326
|
-
const check = validateEnvelope(event);
|
|
327
|
-
if (!check.ok) return check;
|
|
328
|
-
|
|
329
|
-
let expectedHash;
|
|
330
|
-
try {
|
|
331
|
-
expectedHash = hashEvent(event);
|
|
332
|
-
} catch (error) {
|
|
333
|
-
return fail('E_EVENT_CANONICAL', `event cannot be canonicalized: ${error.message}`);
|
|
334
|
-
}
|
|
335
|
-
if (event.hash !== expectedHash) {
|
|
336
|
-
return fail('E_HASH_MISMATCH', 'event hash does not match its content', {
|
|
337
|
-
eventId: event.id,
|
|
338
|
-
seq: event.seq,
|
|
339
|
-
expected: expectedHash,
|
|
340
|
-
actual: event.hash,
|
|
341
|
-
});
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
if (options.expectedSeq !== undefined && event.seq !== options.expectedSeq) {
|
|
345
|
-
return fail('E_SEQ_GAP', 'event seq is not the expected next seq', {
|
|
346
|
-
eventId: event.id,
|
|
347
|
-
expected: options.expectedSeq,
|
|
348
|
-
actual: event.seq,
|
|
349
|
-
});
|
|
350
|
-
}
|
|
351
|
-
if (options.expectedPrevHash !== undefined && event.prevHash !== options.expectedPrevHash) {
|
|
352
|
-
return fail('E_HASH_CHAIN', 'event prevHash does not point at the previous event', {
|
|
353
|
-
eventId: event.id,
|
|
354
|
-
seq: event.seq,
|
|
355
|
-
expected: options.expectedPrevHash,
|
|
356
|
-
actual: event.prevHash,
|
|
357
|
-
});
|
|
358
|
-
}
|
|
359
|
-
if (options.projectId !== undefined && event.projectId !== options.projectId) {
|
|
360
|
-
return fail('E_EVENT_PROJECT', 'event belongs to a different project', {
|
|
361
|
-
eventId: event.id,
|
|
362
|
-
expected: options.projectId,
|
|
363
|
-
actual: event.projectId,
|
|
364
|
-
});
|
|
365
|
-
}
|
|
366
|
-
return { ok: true };
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
/**
|
|
370
|
-
* 校验一段连续事件的 hash 链与 seq 连续性。
|
|
371
|
-
* startPrevHash / startSeq 用于从检查点之后开始校验的场景。
|
|
372
|
-
*/
|
|
373
|
-
export function verifyChain(events, options = {}) {
|
|
374
|
-
if (!Array.isArray(events)) {
|
|
375
|
-
throw new TypeError('verifyChain: events must be an array');
|
|
376
|
-
}
|
|
377
|
-
let prevHash = options.startPrevHash ?? GENESIS_HASH;
|
|
378
|
-
let seq = options.startSeq ?? 1;
|
|
379
|
-
|
|
380
|
-
for (let index = 0; index < events.length; index += 1) {
|
|
381
|
-
const result = verifyEvent(events[index], {
|
|
382
|
-
expectedSeq: seq,
|
|
383
|
-
expectedPrevHash: prevHash,
|
|
384
|
-
projectId: options.projectId,
|
|
385
|
-
});
|
|
386
|
-
if (!result.ok) {
|
|
387
|
-
return { ...result, index, eventId: events[index]?.id ?? null };
|
|
388
|
-
}
|
|
389
|
-
prevHash = events[index].hash;
|
|
390
|
-
seq += 1;
|
|
391
|
-
}
|
|
392
|
-
return { ok: true, nextSeq: seq, lastHash: prevHash };
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
/** 事件落盘的唯一字节形式:canonical JSON + \n。 */
|
|
396
|
-
export function serializeEvent(event) {
|
|
397
|
-
return `${canonicalJson(event)}\n`;
|
|
398
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* 账本事件信封、canonical JSON、哈希链与 ID 生成。
|
|
3
|
+
*
|
|
4
|
+
* 对应 03-架构设计报告 §6.2 / §6.5:
|
|
5
|
+
* - 每行一个 JSON 对象,字段固定为 ENVELOPE_FIELDS。
|
|
6
|
+
* - hash = sha256(canonicalJson(除 hash 之外的全部字段)),小写 hex。
|
|
7
|
+
* - prevHash 指向前一事件的 hash;创世为 64 个 "0"。
|
|
8
|
+
* - id 为 ULID(时间有序),外部引用用 id,排序用 seq。
|
|
9
|
+
*
|
|
10
|
+
* 本文件不做 I/O,也不理解事件语义;writer / fold / checkpoint 共用这里的
|
|
11
|
+
* 序列化与校验规则,避免同一规则出现第二份实现。
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { createHash, randomBytes } from 'node:crypto';
|
|
15
|
+
|
|
16
|
+
/** 信封结构版本。读到更高版本时由 writer 拒绝并提示升级。 */
|
|
17
|
+
export const EVENT_VERSION = 1;
|
|
18
|
+
|
|
19
|
+
/** 创世前一事件的 hash(64 个 0)。 */
|
|
20
|
+
export const GENESIS_HASH = '0'.repeat(64);
|
|
21
|
+
|
|
22
|
+
/** 单事件上限 256 KB(含行尾 \n,见 03 §6.2)。 */
|
|
23
|
+
export const MAX_EVENT_BYTES = 256 * 1024;
|
|
24
|
+
|
|
25
|
+
/** hash / prevHash 的格式:小写 sha256 hex。 */
|
|
26
|
+
export const HASH_PATTERN = /^[0-9a-f]{64}$/;
|
|
27
|
+
|
|
28
|
+
/** 信封字段顺序(也是 JSON 示例中的展示顺序)。 */
|
|
29
|
+
export const ENVELOPE_FIELDS = Object.freeze([
|
|
30
|
+
'v',
|
|
31
|
+
'id',
|
|
32
|
+
'projectId',
|
|
33
|
+
'seq',
|
|
34
|
+
'type',
|
|
35
|
+
'at',
|
|
36
|
+
'actor',
|
|
37
|
+
'correlationId',
|
|
38
|
+
'causationId',
|
|
39
|
+
'payload',
|
|
40
|
+
'prevHash',
|
|
41
|
+
'hash',
|
|
42
|
+
]);
|
|
43
|
+
|
|
44
|
+
const ENVELOPE_FIELD_SET = new Set(ENVELOPE_FIELDS);
|
|
45
|
+
const ACTOR_KINDS = new Set(['human', 'agent', 'system']);
|
|
46
|
+
const ULID_ALPHABET = '0123456789ABCDEFGHJKMNPQRSTVWXYZ';
|
|
47
|
+
const ID_PREFIX_PATTERN = /^[a-z][a-z0-9]*_$/;
|
|
48
|
+
const EVENT_TYPE_PATTERN = /^[a-z][a-z0-9_-]*\/[a-z][a-z0-9_-]*$/;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 全账本统一的错误类型。
|
|
52
|
+
* code 是稳定的错误码(E_*),details 只放可序列化的诊断信息。
|
|
53
|
+
*/
|
|
54
|
+
export class LedgerError extends Error {
|
|
55
|
+
constructor(code, message, details = {}) {
|
|
56
|
+
super(message);
|
|
57
|
+
this.name = 'LedgerError';
|
|
58
|
+
this.code = code;
|
|
59
|
+
this.details = details;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** 是否为普通对象(拒绝 Date / Map / class 实例,保证 canonical JSON 无歧义)。 */
|
|
64
|
+
export function isPlainObject(value) {
|
|
65
|
+
if (value === null || typeof value !== 'object') return false;
|
|
66
|
+
const proto = Object.getPrototypeOf(value);
|
|
67
|
+
return proto === Object.prototype || proto === null;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function fail(code, message, details = {}) {
|
|
71
|
+
return { ok: false, code, message, details };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 确定性 JSON(canonical JSON):
|
|
76
|
+
* - 对象键按 UTF-16 码元序升序;数组保持原序。
|
|
77
|
+
* - 对象中值为 undefined 的键按 JSON 语义省略;数组中 undefined 变 null。
|
|
78
|
+
* - 拒绝 NaN / Infinity / BigInt / 函数 / Symbol / 非普通对象 / 循环引用。
|
|
79
|
+
* - 字符串使用 JSON.stringify 的转义,输出 UTF-8 文本。
|
|
80
|
+
*/
|
|
81
|
+
export function canonicalJson(value) {
|
|
82
|
+
const stack = new Set();
|
|
83
|
+
|
|
84
|
+
const encode = (input) => {
|
|
85
|
+
if (input === null) return 'null';
|
|
86
|
+
|
|
87
|
+
const type = typeof input;
|
|
88
|
+
if (type === 'string') return JSON.stringify(input);
|
|
89
|
+
if (type === 'boolean') return input ? 'true' : 'false';
|
|
90
|
+
if (type === 'number') {
|
|
91
|
+
if (!Number.isFinite(input)) {
|
|
92
|
+
throw new TypeError('canonicalJson: non-finite numbers are not allowed');
|
|
93
|
+
}
|
|
94
|
+
return JSON.stringify(input);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (type !== 'object') {
|
|
98
|
+
throw new TypeError(`canonicalJson: unsupported value type "${type}"`);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (stack.has(input)) {
|
|
102
|
+
throw new TypeError('canonicalJson: circular reference');
|
|
103
|
+
}
|
|
104
|
+
stack.add(input);
|
|
105
|
+
try {
|
|
106
|
+
if (Array.isArray(input)) {
|
|
107
|
+
const parts = input.map((item) => (item === undefined ? 'null' : encode(item)));
|
|
108
|
+
return `[${parts.join(',')}]`;
|
|
109
|
+
}
|
|
110
|
+
if (!isPlainObject(input)) {
|
|
111
|
+
throw new TypeError('canonicalJson: only plain objects and arrays are allowed');
|
|
112
|
+
}
|
|
113
|
+
const keys = Object.keys(input)
|
|
114
|
+
.filter((key) => input[key] !== undefined)
|
|
115
|
+
.sort();
|
|
116
|
+
const parts = keys.map((key) => `${JSON.stringify(key)}:${encode(input[key])}`);
|
|
117
|
+
return `{${parts.join(',')}}`;
|
|
118
|
+
} finally {
|
|
119
|
+
stack.delete(input);
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
return encode(value);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** sha256(text) 的小写 hex。 */
|
|
127
|
+
export function sha256Hex(text) {
|
|
128
|
+
return createHash('sha256').update(text, 'utf8').digest('hex');
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* 计算事件 hash:对除 hash 外的全部字段取 canonical JSON 后 sha256。
|
|
133
|
+
* 注意:不校验信封形状,只负责摘要;形状校验在 verifyEvent / sealEvent。
|
|
134
|
+
*/
|
|
135
|
+
export function hashEvent(event) {
|
|
136
|
+
if (!isPlainObject(event)) {
|
|
137
|
+
throw new TypeError('hashEvent: event must be a plain object');
|
|
138
|
+
}
|
|
139
|
+
const { hash: _ignored, ...rest } = event;
|
|
140
|
+
return sha256Hex(canonicalJson(rest));
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function normalizeActor(actor) {
|
|
144
|
+
if (!isPlainObject(actor)) {
|
|
145
|
+
throw new LedgerError('E_EVENT_ACTOR', 'event.actor must be a plain object');
|
|
146
|
+
}
|
|
147
|
+
for (const key of Object.keys(actor)) {
|
|
148
|
+
if (key !== 'kind' && key !== 'memberId') {
|
|
149
|
+
throw new LedgerError('E_EVENT_ACTOR', `event.actor has unknown field "${key}"`, { field: key });
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
if (!ACTOR_KINDS.has(actor.kind)) {
|
|
153
|
+
throw new LedgerError('E_EVENT_ACTOR', 'event.actor.kind must be human | agent | system', {
|
|
154
|
+
kind: actor.kind,
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
if (actor.kind === 'agent' && (typeof actor.memberId !== 'string' || actor.memberId.length === 0)) {
|
|
158
|
+
throw new LedgerError('E_EVENT_ACTOR', 'agent actor requires a non-empty memberId');
|
|
159
|
+
}
|
|
160
|
+
if (actor.memberId !== undefined && typeof actor.memberId !== 'string') {
|
|
161
|
+
throw new LedgerError('E_EVENT_ACTOR', 'event.actor.memberId must be a string when present');
|
|
162
|
+
}
|
|
163
|
+
return actor.memberId === undefined
|
|
164
|
+
? { kind: actor.kind }
|
|
165
|
+
: { kind: actor.kind, memberId: actor.memberId };
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function validateEnvelope(event) {
|
|
169
|
+
if (!isPlainObject(event)) return fail('E_EVENT_SHAPE', 'event must be a plain object');
|
|
170
|
+
|
|
171
|
+
for (const key of Object.keys(event)) {
|
|
172
|
+
if (!ENVELOPE_FIELD_SET.has(key)) {
|
|
173
|
+
return fail('E_EVENT_FIELD', `event has unknown field "${key}"`, { field: key });
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
for (const key of ENVELOPE_FIELDS) {
|
|
177
|
+
if (!(key in event)) {
|
|
178
|
+
return fail('E_EVENT_FIELD', `event is missing field "${key}"`, { field: key });
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (!Number.isInteger(event.v) || event.v < 1) {
|
|
183
|
+
return fail('E_EVENT_VERSION', 'event.v must be a positive integer', { v: event.v });
|
|
184
|
+
}
|
|
185
|
+
if (event.v !== EVENT_VERSION) {
|
|
186
|
+
return fail('E_EVENT_VERSION', `unsupported event version ${event.v}`, {
|
|
187
|
+
supported: EVENT_VERSION,
|
|
188
|
+
actual: event.v,
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
if (typeof event.id !== 'string' || event.id.length === 0) {
|
|
192
|
+
return fail('E_EVENT_ID', 'event.id must be a non-empty string');
|
|
193
|
+
}
|
|
194
|
+
if (typeof event.projectId !== 'string' || event.projectId.length === 0) {
|
|
195
|
+
return fail('E_EVENT_PROJECT', 'event.projectId must be a non-empty string');
|
|
196
|
+
}
|
|
197
|
+
if (!Number.isSafeInteger(event.seq) || event.seq < 1) {
|
|
198
|
+
return fail('E_EVENT_SEQ', 'event.seq must be a positive safe integer', { seq: event.seq });
|
|
199
|
+
}
|
|
200
|
+
if (typeof event.type !== 'string' || !EVENT_TYPE_PATTERN.test(event.type)) {
|
|
201
|
+
return fail('E_EVENT_TYPE', 'event.type must look like "<domain>/<name>"', { type: event.type });
|
|
202
|
+
}
|
|
203
|
+
if (!Number.isSafeInteger(event.at) || event.at < 0) {
|
|
204
|
+
return fail('E_EVENT_AT', 'event.at must be a non-negative safe integer (epoch ms)', { at: event.at });
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
try {
|
|
208
|
+
normalizeActor(event.actor);
|
|
209
|
+
} catch (error) {
|
|
210
|
+
return fail(error.code ?? 'E_EVENT_ACTOR', error.message, error.details ?? {});
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (event.correlationId !== null && typeof event.correlationId !== 'string') {
|
|
214
|
+
return fail('E_EVENT_CORRELATION', 'event.correlationId must be a string or null');
|
|
215
|
+
}
|
|
216
|
+
if (event.causationId !== null && typeof event.causationId !== 'string') {
|
|
217
|
+
return fail('E_EVENT_CAUSATION', 'event.causationId must be a string or null');
|
|
218
|
+
}
|
|
219
|
+
if (!isPlainObject(event.payload)) {
|
|
220
|
+
return fail('E_EVENT_PAYLOAD', 'event.payload must be a plain object');
|
|
221
|
+
}
|
|
222
|
+
if (typeof event.prevHash !== 'string' || !HASH_PATTERN.test(event.prevHash)) {
|
|
223
|
+
return fail('E_EVENT_PREV_HASH', 'event.prevHash must be 64 lowercase hex chars', {
|
|
224
|
+
prevHash: event.prevHash,
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
if (typeof event.hash !== 'string' || !HASH_PATTERN.test(event.hash)) {
|
|
228
|
+
return fail('E_EVENT_HASH', 'event.hash must be 64 lowercase hex chars', { hash: event.hash });
|
|
229
|
+
}
|
|
230
|
+
return { ok: true };
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* 生成 ULID 风格的 ID:`<prefix><10 字符时间><16 字符随机>`。
|
|
235
|
+
* 前缀自带下划线(`evt_` / `cmd_` / `mem_`),时间部分为 48-bit 毫秒,
|
|
236
|
+
* Crockford Base32;随机部分 80-bit。
|
|
237
|
+
*/
|
|
238
|
+
export function newId(prefix, now = Date.now()) {
|
|
239
|
+
if (typeof prefix !== 'string' || !ID_PREFIX_PATTERN.test(prefix)) {
|
|
240
|
+
throw new LedgerError('E_ID_PREFIX', 'id prefix must match /^[a-z][a-z0-9]*_$/', { prefix });
|
|
241
|
+
}
|
|
242
|
+
if (!Number.isSafeInteger(now) || now < 0 || now >= 2 ** 48) {
|
|
243
|
+
throw new LedgerError('E_ID_TIME', 'id timestamp must be a 48-bit non-negative integer', { now });
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
let time = now;
|
|
247
|
+
const timeChars = new Array(10);
|
|
248
|
+
for (let index = 9; index >= 0; index -= 1) {
|
|
249
|
+
timeChars[index] = ULID_ALPHABET[time % 32];
|
|
250
|
+
time = Math.floor(time / 32);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// 256 % 32 === 0,按字节取低 5 bit 无模偏差。
|
|
254
|
+
const bytes = randomBytes(16);
|
|
255
|
+
let randomChars = '';
|
|
256
|
+
for (let index = 0; index < 16; index += 1) {
|
|
257
|
+
randomChars += ULID_ALPHABET[bytes[index] % 32];
|
|
258
|
+
}
|
|
259
|
+
return `${prefix}${timeChars.join('')}${randomChars}`;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/** 生成事件 ID(`evt_` 前缀)。 */
|
|
263
|
+
export function newEventId(now = Date.now()) {
|
|
264
|
+
return newId('evt_', now);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* 封事件:补默认字段、计算 hash,并返回“与落盘字节一致”的普通对象。
|
|
269
|
+
*
|
|
270
|
+
* 返回值经过 canonical JSON 往返,因此 undefined 字段已被省略 —— 内存中的
|
|
271
|
+
* 事件与重新读盘得到的事件逐字段相等,避免检查点状态出现 JSON 往返差异。
|
|
272
|
+
*
|
|
273
|
+
* @param {object} fields 除 hash 外的信封字段;id/at/v/correlationId/causationId/payload/prevHash 可缺省。
|
|
274
|
+
* @returns {object} 带 hash 的事件信封。
|
|
275
|
+
*/
|
|
276
|
+
export function sealEvent(fields = {}) {
|
|
277
|
+
if (!isPlainObject(fields)) {
|
|
278
|
+
throw new LedgerError('E_EVENT_SHAPE', 'sealEvent expects a plain object');
|
|
279
|
+
}
|
|
280
|
+
if ('hash' in fields) {
|
|
281
|
+
throw new LedgerError('E_EVENT_HASH', 'sealEvent must not receive a hash; it is computed here');
|
|
282
|
+
}
|
|
283
|
+
for (const key of Object.keys(fields)) {
|
|
284
|
+
if (!ENVELOPE_FIELD_SET.has(key) || key === 'hash') {
|
|
285
|
+
throw new LedgerError('E_EVENT_FIELD', `sealEvent received unknown field "${key}"`, { field: key });
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
const at = fields.at ?? Date.now();
|
|
290
|
+
const envelope = {
|
|
291
|
+
v: fields.v ?? EVENT_VERSION,
|
|
292
|
+
id: fields.id ?? newEventId(at),
|
|
293
|
+
projectId: fields.projectId,
|
|
294
|
+
seq: fields.seq,
|
|
295
|
+
type: fields.type,
|
|
296
|
+
at,
|
|
297
|
+
actor: normalizeActor(fields.actor),
|
|
298
|
+
correlationId: fields.correlationId ?? null,
|
|
299
|
+
causationId: fields.causationId ?? null,
|
|
300
|
+
payload: fields.payload === undefined ? {} : fields.payload,
|
|
301
|
+
prevHash: fields.prevHash ?? GENESIS_HASH,
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
let event;
|
|
305
|
+
try {
|
|
306
|
+
const hash = hashEvent(envelope);
|
|
307
|
+
event = JSON.parse(canonicalJson({ ...envelope, hash }));
|
|
308
|
+
} catch (error) {
|
|
309
|
+
throw new LedgerError('E_EVENT_INVALID', `event is not canonical-JSON serializable: ${error.message}`, {
|
|
310
|
+
cause: error.message,
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
const check = validateEnvelope(event);
|
|
315
|
+
if (!check.ok) {
|
|
316
|
+
throw new LedgerError(check.code, check.message, check.details);
|
|
317
|
+
}
|
|
318
|
+
return event;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* 校验单个事件的信封与 hash(可同时校验链接位置)。
|
|
323
|
+
* 返回 {ok:true} 或 {ok:false, code, message, details};不抛异常。
|
|
324
|
+
*/
|
|
325
|
+
export function verifyEvent(event, options = {}) {
|
|
326
|
+
const check = validateEnvelope(event);
|
|
327
|
+
if (!check.ok) return check;
|
|
328
|
+
|
|
329
|
+
let expectedHash;
|
|
330
|
+
try {
|
|
331
|
+
expectedHash = hashEvent(event);
|
|
332
|
+
} catch (error) {
|
|
333
|
+
return fail('E_EVENT_CANONICAL', `event cannot be canonicalized: ${error.message}`);
|
|
334
|
+
}
|
|
335
|
+
if (event.hash !== expectedHash) {
|
|
336
|
+
return fail('E_HASH_MISMATCH', 'event hash does not match its content', {
|
|
337
|
+
eventId: event.id,
|
|
338
|
+
seq: event.seq,
|
|
339
|
+
expected: expectedHash,
|
|
340
|
+
actual: event.hash,
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
if (options.expectedSeq !== undefined && event.seq !== options.expectedSeq) {
|
|
345
|
+
return fail('E_SEQ_GAP', 'event seq is not the expected next seq', {
|
|
346
|
+
eventId: event.id,
|
|
347
|
+
expected: options.expectedSeq,
|
|
348
|
+
actual: event.seq,
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
if (options.expectedPrevHash !== undefined && event.prevHash !== options.expectedPrevHash) {
|
|
352
|
+
return fail('E_HASH_CHAIN', 'event prevHash does not point at the previous event', {
|
|
353
|
+
eventId: event.id,
|
|
354
|
+
seq: event.seq,
|
|
355
|
+
expected: options.expectedPrevHash,
|
|
356
|
+
actual: event.prevHash,
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
if (options.projectId !== undefined && event.projectId !== options.projectId) {
|
|
360
|
+
return fail('E_EVENT_PROJECT', 'event belongs to a different project', {
|
|
361
|
+
eventId: event.id,
|
|
362
|
+
expected: options.projectId,
|
|
363
|
+
actual: event.projectId,
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
return { ok: true };
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* 校验一段连续事件的 hash 链与 seq 连续性。
|
|
371
|
+
* startPrevHash / startSeq 用于从检查点之后开始校验的场景。
|
|
372
|
+
*/
|
|
373
|
+
export function verifyChain(events, options = {}) {
|
|
374
|
+
if (!Array.isArray(events)) {
|
|
375
|
+
throw new TypeError('verifyChain: events must be an array');
|
|
376
|
+
}
|
|
377
|
+
let prevHash = options.startPrevHash ?? GENESIS_HASH;
|
|
378
|
+
let seq = options.startSeq ?? 1;
|
|
379
|
+
|
|
380
|
+
for (let index = 0; index < events.length; index += 1) {
|
|
381
|
+
const result = verifyEvent(events[index], {
|
|
382
|
+
expectedSeq: seq,
|
|
383
|
+
expectedPrevHash: prevHash,
|
|
384
|
+
projectId: options.projectId,
|
|
385
|
+
});
|
|
386
|
+
if (!result.ok) {
|
|
387
|
+
return { ...result, index, eventId: events[index]?.id ?? null };
|
|
388
|
+
}
|
|
389
|
+
prevHash = events[index].hash;
|
|
390
|
+
seq += 1;
|
|
391
|
+
}
|
|
392
|
+
return { ok: true, nextSeq: seq, lastHash: prevHash };
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/** 事件落盘的唯一字节形式:canonical JSON + \n。 */
|
|
396
|
+
export function serializeEvent(event) {
|
|
397
|
+
return `${canonicalJson(event)}\n`;
|
|
398
|
+
}
|