@kition/convergence 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js ADDED
@@ -0,0 +1,1033 @@
1
+ import { randomActor as $e, Replica as ye, canonicalText as S } from "@kition/kdm";
2
+ import { KitSession as ke } from "@kition/kit-engine";
3
+ function H(t) {
4
+ const { kind: e, ...n } = t, s = Object.entries(n).map(([r, o]) => `${r}: ${typeof o == "string" ? JSON.stringify(o) : String(o)}`);
5
+ return s.length === 0 ? e : `${e} { ${s.join(", ")} }`;
6
+ }
7
+ const L = Object.freeze({ seeds: [1, 2, 3], replicas: [2, 3, 5], steps: 120 });
8
+ function nt(t) {
9
+ const e = { ...L, seeds: [...L.seeds], replicas: [...L.replicas] }, n = Number.parseInt(t.KITION_CONVERGENCE_SEED_BASE ?? "", 10), s = Number.isSafeInteger(n) && n >= 0 ? n : 0, r = Number.parseInt(t.KITION_CONVERGENCE_SEEDS ?? "", 10), o = Number.isSafeInteger(r) && r >= 0 ? Math.max(r, 1) : s > 0 ? e.seeds.length : 0;
10
+ o > 0 && (e.seeds = Array.from({ length: o }, (c, h) => s + h + 1));
11
+ const i = Number.parseInt(t.KITION_CONVERGENCE_STEPS ?? "", 10);
12
+ return Number.isSafeInteger(i) && i >= 0 && (e.steps = Math.max(i, 1)), e;
13
+ }
14
+ function xe(t) {
15
+ const e = [];
16
+ for (const r of [...t.keys()].sort()) {
17
+ const o = t.get(r), i = new Uint8Array(8);
18
+ new DataView(i.buffer).setBigUint64(0, BigInt(o.length), !0), e.push(new TextEncoder().encode(r), i, o);
19
+ }
20
+ const n = new Uint8Array(e.reduce((r, o) => r + o.length, 0));
21
+ let s = 0;
22
+ for (const r of e)
23
+ n.set(r, s), s += r.length;
24
+ return n;
25
+ }
26
+ function ve(t) {
27
+ const e = /* @__PURE__ */ new Map(), n = new DataView(t.buffer, t.byteOffset, t.byteLength);
28
+ let s = 0;
29
+ for (; s < t.length; ) {
30
+ if (s + 72 > t.length) throw new Error("a truncated blob bundle");
31
+ const r = new TextDecoder().decode(t.subarray(s, s + 64)), o = Number(n.getBigUint64(s + 64, !0)), i = s + 72;
32
+ if (i + o > t.length) throw new Error("a truncated blob bundle");
33
+ e.set(r, t.slice(i, i + o)), s = i + o;
34
+ }
35
+ return e;
36
+ }
37
+ class st {
38
+ name;
39
+ schema;
40
+ model;
41
+ documentClass;
42
+ newActor;
43
+ fileName;
44
+ constructor(e) {
45
+ this.name = e.name, this.schema = e.schema, this.model = e.model, this.documentClass = e.documentClass, this.newActor = e.actor ?? $e, this.fileName = e.fileName ?? null;
46
+ }
47
+ /** A document over `replica`, as the kit's class makes one. */
48
+ open(e) {
49
+ return new this.documentClass(new ke(this.model, e));
50
+ }
51
+ join(e, n) {
52
+ const s = this.open(ye.load(this.model.schema, e, this.newActor()));
53
+ return this.addBlobs(s, n), s;
54
+ }
55
+ version(e) {
56
+ return e.version();
57
+ }
58
+ exportChanges(e, n) {
59
+ return e.exportChanges(n);
60
+ }
61
+ importChanges(e, n) {
62
+ e.importChanges(n);
63
+ }
64
+ snapshot(e) {
65
+ return e.snapshot();
66
+ }
67
+ state(e) {
68
+ return e.session.replica.stateJson();
69
+ }
70
+ undo(e) {
71
+ return JSON.parse(e.undo()).done;
72
+ }
73
+ redo(e) {
74
+ return JSON.parse(e.redo()).done;
75
+ }
76
+ blobs(e) {
77
+ return ve(e.blobs());
78
+ }
79
+ addBlobs(e, n) {
80
+ n.size > 0 && e.addBlobs(xe(n));
81
+ }
82
+ exportFile(e) {
83
+ return this.fileName === null ? null : e.save(this.fileName);
84
+ }
85
+ script(e, n) {
86
+ return !1;
87
+ }
88
+ /** Runs one of the kit's commands as the host's person, through the document's `execute`; answers its JSON result. */
89
+ run(e, n, s) {
90
+ return JSON.parse(e.execute(n, JSON.stringify(s)));
91
+ }
92
+ }
93
+ function rt(t, e) {
94
+ let n = 0;
95
+ for (let s = 0; s < e && n < t.length; s += 1) {
96
+ const r = t.codePointAt(n);
97
+ n += r > 65535 ? 2 : 1;
98
+ }
99
+ return n;
100
+ }
101
+ const V = (1n << 64n) - 1n;
102
+ class Ee {
103
+ state;
104
+ constructor(e) {
105
+ this.state = BigInt(e) * 0x9e3779b97f4a7c15n & V | 1n;
106
+ }
107
+ nextU64() {
108
+ return this.state ^= this.state >> 12n, this.state = (this.state ^ this.state << 25n) & V, this.state ^= this.state >> 27n, this.state * 0x2545f4914f6cdd1dn & V;
109
+ }
110
+ /** A value in `0..bound`, or 0 when `bound` is 0. */
111
+ below(e) {
112
+ return e <= 0 ? 0 : Number(this.nextU64() % BigInt(e));
113
+ }
114
+ /** `true` with `percent` percent probability. */
115
+ chance(e) {
116
+ return this.nextU64() % 100n < BigInt(e);
117
+ }
118
+ shuffle(e) {
119
+ for (let n = e.length - 1; n > 0; n -= 1) {
120
+ const s = this.below(n + 1);
121
+ [e[n], e[s]] = [e[s], e[n]];
122
+ }
123
+ }
124
+ /** One item, or `undefined` for an empty list. */
125
+ pick(e) {
126
+ return e[this.below(e.length)];
127
+ }
128
+ }
129
+ function Se(t) {
130
+ return !["text", "list", "tree", "map"].includes(t.kind);
131
+ }
132
+ function W(t, e) {
133
+ if (t === e) return 0;
134
+ const n = Math.min(t.length, e.length);
135
+ for (let s = 0; s < n; s += 1) {
136
+ const r = t.codePointAt(s), o = e.codePointAt(s);
137
+ if (r !== o) return r < o ? -1 : 1;
138
+ r > 65535 && (s += 1);
139
+ }
140
+ return t.length - e.length;
141
+ }
142
+ function v(t) {
143
+ return [...t].sort(W);
144
+ }
145
+ function x(t, e) {
146
+ return S(t ?? null) === S(e ?? null);
147
+ }
148
+ function C(t) {
149
+ return t !== null && typeof t == "object" && !Array.isArray(t);
150
+ }
151
+ function p(t, e) {
152
+ return C(t) && Object.hasOwn(t, e) ? t[e] : null;
153
+ }
154
+ class K {
155
+ constructor(e, n, s) {
156
+ this.kit = e, this.root = n, this.types = s;
157
+ }
158
+ /** Reads `model/schema.json`. */
159
+ static parse(e) {
160
+ let n;
161
+ try {
162
+ n = JSON.parse(e);
163
+ } catch (i) {
164
+ throw new Error(`the schema is not JSON: ${i.message}`);
165
+ }
166
+ const s = p(n, "types");
167
+ if (!C(s)) throw new Error("the schema has no types");
168
+ const r = /* @__PURE__ */ new Map();
169
+ for (const i of v(Object.keys(s))) {
170
+ const c = /* @__PURE__ */ new Map(), h = p(s[i], "fields"), l = C(h) ? h : {};
171
+ for (const a of v(Object.keys(l))) {
172
+ const d = l[a], f = p(d, "kind");
173
+ if (typeof f != "string") throw new Error(`${i}.${a} has no kind`);
174
+ const u = p(d, "of");
175
+ c.set(a, { kind: f, of: typeof u == "string" ? u : null });
176
+ }
177
+ r.set(i, c);
178
+ }
179
+ const o = (i) => {
180
+ const c = p(n, i);
181
+ return typeof c == "string" ? c : "";
182
+ };
183
+ return new K(o("kit"), o("root"), r);
184
+ }
185
+ /** Whether any type has a field of `kind`. */
186
+ hasKind(e) {
187
+ return [...this.types.values()].some((n) => [...n.values()].some((s) => s.kind === e));
188
+ }
189
+ field(e, n) {
190
+ return this.types.get(e)?.get(n);
191
+ }
192
+ /** A type's fields in name order (none for an unknown type). */
193
+ fieldsOf(e) {
194
+ return this.types.get(e) ?? /* @__PURE__ */ new Map();
195
+ }
196
+ }
197
+ function E(t) {
198
+ const e = p(t, "objects");
199
+ return C(e) ? e : {};
200
+ }
201
+ function z(t) {
202
+ const e = p(t, "$type");
203
+ return typeof e == "string" ? e : null;
204
+ }
205
+ function le(t) {
206
+ return Array.isArray(t) ? t.filter((e) => typeof e == "string") : [];
207
+ }
208
+ function P(t) {
209
+ return Array.isArray(t) ? t : [];
210
+ }
211
+ function Oe(t, e, n) {
212
+ const s = E(e), r = (h) => Object.hasOwn(s, h) ? z(s[h]) : null, o = [], i = /* @__PURE__ */ new Map();
213
+ for (const h of v(Object.keys(s))) {
214
+ const l = s[h], a = z(l);
215
+ if (a === null) {
216
+ o.push(`${h} has no $type`);
217
+ continue;
218
+ }
219
+ const d = t.types.get(a);
220
+ if (d)
221
+ for (const [f, u] of d) {
222
+ const g = p(l, f), O = (k, b) => {
223
+ const $ = r(k);
224
+ $ === null ? n(a, f) || o.push(`${a} ${h}.${f} ${b} ${k}, which does not exist`) : u.of !== null && u.of !== $ && o.push(`${a} ${h}.${f} ${b} ${k}, a ${$} rather than a ${u.of}`);
225
+ };
226
+ if (u.kind === "ref")
227
+ typeof g == "string" && O(g, "names");
228
+ else if (u.kind === "list") {
229
+ const k = /* @__PURE__ */ new Set();
230
+ for (const b of le(g))
231
+ k.has(b) && o.push(`${a} ${h}.${f} holds ${b} twice`), k.add(b), O(b, "holds");
232
+ } else if (u.kind === "tree") {
233
+ const k = [...P(g)];
234
+ for (; k.length > 0; ) {
235
+ const b = k.pop(), $ = p(b, "object");
236
+ if (typeof $ == "string") {
237
+ const M = `${h}.${f}`, Y = i.get($);
238
+ Y !== void 0 && o.push(`${$} is in a tree twice (${Y} and ${M})`), i.set($, M), r($) !== null && O($, "has a node for");
239
+ }
240
+ k.push(...P(p(b, "children")));
241
+ }
242
+ }
243
+ }
244
+ }
245
+ if (o.length === 0) return;
246
+ const c = o.length > 5 ? ` (and ${o.length - 5} more)` : "";
247
+ throw new Error(`invalid references: ${o.slice(0, 5).join("; ")}${c}`);
248
+ }
249
+ class N {
250
+ entries = /* @__PURE__ */ new Map();
251
+ static key(e) {
252
+ return `${e[0]}\0${e[1]}`;
253
+ }
254
+ set(e, n) {
255
+ this.entries.set(N.key(e), { slot: e, value: n });
256
+ }
257
+ has(e) {
258
+ return this.entries.has(N.key(e));
259
+ }
260
+ get(e) {
261
+ return this.entries.get(N.key(e))?.value;
262
+ }
263
+ get size() {
264
+ return this.entries.size;
265
+ }
266
+ /** The registers in slot order. */
267
+ list() {
268
+ return v(this.entries.keys()).map((e) => this.entries.get(e));
269
+ }
270
+ filter(e) {
271
+ const n = new N();
272
+ for (const { slot: s, value: r } of this.list()) e(s, r) && n.set(s, r);
273
+ return n;
274
+ }
275
+ }
276
+ function je(t, e, n) {
277
+ const s = E(e), r = new N(), o = E(n);
278
+ for (const i of v(Object.keys(o))) {
279
+ const c = o[i], h = t.types.get(z(c) ?? "");
280
+ if (!h) continue;
281
+ const l = Object.hasOwn(s, i) ? s[i] : void 0;
282
+ for (const [a, d] of h) {
283
+ const f = p(c, a), u = l === void 0 ? void 0 : p(l, a);
284
+ if (Se(d))
285
+ (u === void 0 || !x(u, f)) && r.set([i, a], f);
286
+ else if (d.kind === "map" && C(f))
287
+ for (const g of v(Object.keys(f)))
288
+ (u === void 0 || !x(p(u, g), f[g])) && r.set([i, `${a}/${g}`], f[g]);
289
+ }
290
+ }
291
+ return r;
292
+ }
293
+ function he(t, e, n) {
294
+ const s = E(e), r = Object.hasOwn(s, n[0]) ? s[n[0]] : null;
295
+ if (t.field(z(r) ?? "", n[1])) return p(r, n[1]);
296
+ const o = n[1].indexOf("/");
297
+ return o < 0 ? null : p(p(r, n[1].slice(0, o)), n[1].slice(o + 1));
298
+ }
299
+ function Z(t, e) {
300
+ const n = E(t);
301
+ return v(Object.keys(E(e)).filter((s) => !Object.hasOwn(n, s)));
302
+ }
303
+ function Ie(t, e) {
304
+ return Z(e, t);
305
+ }
306
+ function F(t, e) {
307
+ const n = /* @__PURE__ */ new Map(), s = E(e);
308
+ for (const r of v(Object.keys(s))) {
309
+ const o = s[r];
310
+ for (const [i, c] of t.fieldsOf(z(o) ?? ""))
311
+ c.kind === "list" && n.set(`${r}.${i}`, new Set(le(p(o, i))));
312
+ }
313
+ return new Map(v(n.keys()).map((r) => [r, n.get(r)]));
314
+ }
315
+ function ee(t, e) {
316
+ const n = /* @__PURE__ */ new Map(), s = E(e);
317
+ for (const r of v(Object.keys(s))) {
318
+ const o = s[r];
319
+ for (const [i, c] of t.fieldsOf(z(o) ?? "")) {
320
+ if (c.kind !== "tree") continue;
321
+ let h = 0;
322
+ const l = [...P(p(o, i))];
323
+ for (; l.length > 0; ) {
324
+ const a = l.pop();
325
+ h += 1, l.push(...P(p(a, "children")));
326
+ }
327
+ n.set(`${r}.${i}`, h);
328
+ }
329
+ }
330
+ return new Map(v(n.keys()).map((r) => [r, n.get(r)]));
331
+ }
332
+ function B(t, e) {
333
+ const n = (s, r, o) => {
334
+ if (C(s) && C(r)) {
335
+ for (const i of v(/* @__PURE__ */ new Set([...Object.keys(s), ...Object.keys(r)]))) {
336
+ const c = Object.hasOwn(s, i) ? s[i] : null, h = Object.hasOwn(r, i) ? r[i] : null;
337
+ if (!x(c, h)) return n(c, h, `${o}.${i}`);
338
+ }
339
+ return null;
340
+ }
341
+ if (Array.isArray(s) && Array.isArray(r) && s.length === r.length) {
342
+ for (let i = 0; i < s.length; i += 1)
343
+ if (!x(s[i], r[i])) return n(s[i], r[i], `${o}[${i}]`);
344
+ return null;
345
+ }
346
+ return x(s, r) ? null : `${o || "(root)"}: ${te(s)} vs ${te(r)}`;
347
+ };
348
+ return n(t, e, "");
349
+ }
350
+ function te(t) {
351
+ const e = S(t ?? null), n = Array.from(e);
352
+ return n.length > 160 ? `${n.slice(0, 160).join("")}...` : e;
353
+ }
354
+ function ot(t) {
355
+ const e = (1n << 64n) - 1n;
356
+ let n = 0xcbf29ce484222325n;
357
+ for (const s of new TextEncoder().encode(S(t ?? null)))
358
+ n ^= BigInt(s), n = n * 0x100000001b3n & e;
359
+ return n.toString(16).padStart(16, "0");
360
+ }
361
+ class X extends Error {
362
+ }
363
+ function Ae(t, e) {
364
+ return t.length === e.length && t.every((n, s) => n === e[s]);
365
+ }
366
+ const Ne = new Uint8Array(0);
367
+ function I(t) {
368
+ return t instanceof Error ? t.message : String(t);
369
+ }
370
+ function y(t, e) {
371
+ try {
372
+ return e();
373
+ } catch (n) {
374
+ throw n instanceof X ? n : new Error(`${t}${I(n)}`);
375
+ }
376
+ }
377
+ class w {
378
+ constructor(e, n, s) {
379
+ this.kit = e, this.schema = K.parse(e.schema);
380
+ const r = y("create: ", () => e.create()), o = e.snapshot(r), i = A(e, r);
381
+ this.docs = [r];
382
+ for (let c = 1; c < Math.max(n, 1); c += 1) this.docs.push(y("join: ", () => e.join(o, i)));
383
+ this.rng = new Ee(s), this.initial = { snapshot: o, blobs: i };
384
+ }
385
+ docs;
386
+ schema;
387
+ rng;
388
+ /** The snapshot and blobs every replica started from. */
389
+ initial;
390
+ doc(e) {
391
+ if (!Number.isSafeInteger(e) || e < 0 || e >= this.docs.length) throw new Error(`no replica ${e} of ${this.docs.length}`);
392
+ return this.docs[e];
393
+ }
394
+ state(e) {
395
+ return this.kit.state(this.doc(e));
396
+ }
397
+ /** Runs a scripted step on one replica; `false` when the kit has no such step. */
398
+ script(e, n) {
399
+ const s = this.doc(e);
400
+ return y(`replica ${e}: ${H(n)}: `, () => this.kit.script?.(s, n) ?? !1);
401
+ }
402
+ /** Like `script`, and skips the scenario (throws `Skip`) when the kit has no such step. */
403
+ step(e, n) {
404
+ if (!this.script(e, n)) throw new X(`the adapter has no ${n.kind}`);
405
+ }
406
+ /** Like `script`, and fails when the kit has no such step. */
407
+ must(e, n) {
408
+ if (!this.script(e, n)) throw new Error(`the adapter does not support ${H(n)}`);
409
+ }
410
+ text(e) {
411
+ const n = this.kit.text?.(this.doc(e)) ?? null;
412
+ if (n === null) throw new Error("the adapter has no text");
413
+ return n;
414
+ }
415
+ /** Sends `to` what it lacks from `from`, blobs first. */
416
+ deliver(e, n) {
417
+ const s = this.kit.version(this.doc(n)), r = this.kit.exportChanges(this.doc(e), s), o = A(this.kit, this.doc(e)), i = this.doc(n);
418
+ this.kit.addBlobs?.(i, o), y(`replica ${n} importing from ${e}: `, () => this.kit.importChanges(i, r));
419
+ }
420
+ /**
421
+ * Delivers every replica's changes to every other in seeded orders until no state changes
422
+ * (repairs made on import travel too), then sends everything again from scratch to prove
423
+ * duplicates harmless.
424
+ */
425
+ sync() {
426
+ const e = this.docs.length, n = [];
427
+ for (let r = 0; r < e; r += 1)
428
+ for (let o = 0; o < e; o += 1) o !== r && n.push([r, o]);
429
+ let s = null;
430
+ for (let r = 0; r < 12; r += 1) {
431
+ this.rng.shuffle(n);
432
+ for (const [i, c] of n) this.deliver(i, c);
433
+ const o = this.docs.map((i, c) => S(this.state(c) ?? null));
434
+ if (s !== null && s.length === o.length && s.every((i, c) => i === o[c])) break;
435
+ s = o;
436
+ }
437
+ for (const [r, o] of n) {
438
+ const i = this.kit.exportChanges(this.doc(r), Ne);
439
+ y(`replica ${o} importing a duplicate of everything from ${r}: `, () => this.kit.importChanges(this.doc(o), i));
440
+ }
441
+ }
442
+ /** The common state; throws with the first difference. */
443
+ converged() {
444
+ const e = this.state(0), n = S(e ?? null);
445
+ for (let s = 1; s < this.docs.length; s += 1) {
446
+ const r = this.state(s);
447
+ if (S(r ?? null) !== n) throw new Error(`replicas 0 and ${s} differ at ${B(e, r) ?? ""}`);
448
+ }
449
+ return e;
450
+ }
451
+ /** `converged`, the schema's references, and every replica's view. */
452
+ check() {
453
+ const e = this.converged();
454
+ return Oe(this.schema, e, (n, s) => this.kit.allowsDangling?.(n, s) ?? !1), this.docs.forEach((n, s) => y(`replica ${s}'s view: `, () => this.kit.checkView?.(n))), e;
455
+ }
456
+ /** `sync` then `check`. */
457
+ settle() {
458
+ return this.sync(), this.check();
459
+ }
460
+ }
461
+ function A(t, e) {
462
+ return new Map(t.blobs?.(e) ?? []);
463
+ }
464
+ const Ce = "kition-convergence 1.0";
465
+ class de {
466
+ constructor(e) {
467
+ this.kit = e;
468
+ }
469
+ checks = [];
470
+ /** No scenario failed. Skips are allowed here; `level6` is stricter. */
471
+ passed() {
472
+ return this.checks.length > 0 && this.checks.every((e) => e.outcome.kind !== "failed");
473
+ }
474
+ /** The kit passes the convergence suite for L6: nothing failed and no required scenario was skipped. */
475
+ level6() {
476
+ return this.passed() && this.checks.every((e) => !e.required || e.outcome.kind === "passed");
477
+ }
478
+ outcome(e) {
479
+ return this.checks.find((n) => n.id === e)?.outcome;
480
+ }
481
+ toJson() {
482
+ return {
483
+ kit: this.kit,
484
+ suite: Ce,
485
+ level6: this.level6(),
486
+ checks: this.checks.map((e) => ({
487
+ id: e.id,
488
+ title: e.title,
489
+ required: e.required,
490
+ outcome: e.outcome.kind,
491
+ ...e.outcome.kind === "passed" ? {} : { reason: e.outcome.reason }
492
+ }))
493
+ };
494
+ }
495
+ /**
496
+ * The JSON as the Rust suite writes it to `KITION_CONVERGENCE_REPORT` (`{:#}` of a
497
+ * `serde_json` value): keys sorted, two-space indents, a final newline.
498
+ */
499
+ toJsonText() {
500
+ return `${JSON.stringify(G(this.toJson()), null, 2)}
501
+ `;
502
+ }
503
+ /** One line per scenario, then the summary. */
504
+ render() {
505
+ let e = "";
506
+ for (const s of this.checks) {
507
+ const { outcome: r } = s;
508
+ r.kind === "passed" ? e += `PASS ${s.id} - ${s.title}
509
+ ` : r.kind === "failed" ? e += `FAIL ${s.id} - ${s.title}: ${r.reason}
510
+ ` : e += `SKIP ${s.id} - ${s.title}${s.required ? " (required for L6)" : ""}: ${r.reason}
511
+ `;
512
+ }
513
+ const n = this.checks.filter((s) => s.outcome.kind === "passed").length;
514
+ return e += `${this.kit}: ${n}/${this.checks.length} scenarios passed; L6 convergence ${this.level6() ? "passed" : "not passed"}
515
+ `, e;
516
+ }
517
+ push(e, n, s, r) {
518
+ this.checks.push({ id: e, title: n, required: s, outcome: r });
519
+ }
520
+ }
521
+ function G(t) {
522
+ if (Array.isArray(t)) return t.map(G);
523
+ if (t !== null && typeof t == "object") {
524
+ const e = t;
525
+ return Object.fromEntries(Object.keys(e).sort().map((n) => [n, G(e[n])]));
526
+ }
527
+ return t;
528
+ }
529
+ const fe = [
530
+ ["fields", "concurrent edits to different fields of one object keep both"],
531
+ ["same-field", "concurrent edits to one field resolve to one of the written values everywhere"],
532
+ ["create", "concurrent creation from one snapshot makes distinct ids, all kept"],
533
+ ["text-position", "concurrent text at one position (IME composition, CJK, emoji) keeps both without loss"],
534
+ ["text-delete-insert", "text typed inside a range deleted concurrently survives"],
535
+ ["text-split", "a paragraph split concurrent with typing keeps the typing"],
536
+ ["text-merge", "a paragraph merge concurrent with typing in the merged paragraph keeps the typing"],
537
+ ["reorder", "reorder versus reorder keeps every item once"],
538
+ ["reparent", "reparent versus reparent keeps every node once, with no cycle"],
539
+ ["delete-edit", "delete versus edit: the delete wins and references stay valid"],
540
+ ["duplicate-delete", "duplicate versus delete leaves valid references"],
541
+ ["undo", "undo by one actor keeps another actor's later changes"],
542
+ ["undo-reconnect", "undo and redo while offline keep another actor's changes after reconnecting"],
543
+ ["delivery", "delivery in any order, with duplicates and retries, gives identical state"],
544
+ ["crash", "a crash after a local commit and before the push recovers the change"],
545
+ ["blobs-late", "blobs that arrive after their changes do not corrupt the document"],
546
+ ["fuzz", "seeded random commands, undo, redo, partial delivery, and crashes on 2 to 5 replicas converge"]
547
+ ], ne = String.fromCodePoint(20013), se = String.fromCodePoint(25991), re = String.fromCodePoint(128512), oe = String.fromCodePoint(128077), ie = String.fromCodePoint(127997);
548
+ function ue(t) {
549
+ const e = K.parse(t.schema), n = t.create();
550
+ return { text: (t.text?.(n) ?? null) !== null, lists: e.hasKind("list"), trees: e.hasKind("tree"), blobs: e.hasKind("blob") };
551
+ }
552
+ function pe(t, e) {
553
+ switch (t) {
554
+ case "text-position":
555
+ case "text-delete-insert":
556
+ return e.text;
557
+ case "reorder":
558
+ return e.lists;
559
+ case "reparent":
560
+ return e.trees;
561
+ case "blobs-late":
562
+ return e.blobs;
563
+ case "text-split":
564
+ case "text-merge":
565
+ case "duplicate-delete":
566
+ return !1;
567
+ default:
568
+ return !0;
569
+ }
570
+ }
571
+ function it(t, e) {
572
+ const n = new de(t.name);
573
+ let s;
574
+ try {
575
+ s = ue(t);
576
+ } catch (r) {
577
+ return n.push("setup", "the kit makes a document and its schema parses", !0, { kind: "failed", reason: I(r) }), n;
578
+ }
579
+ for (const [r, o] of fe) n.push(r, o, pe(r, s), ge(t, r, e));
580
+ return n;
581
+ }
582
+ function ct(t, e, n) {
583
+ const s = fe.find(([i]) => i === e);
584
+ if (!s) return null;
585
+ const r = new de(t.name);
586
+ let o = !0;
587
+ try {
588
+ o = pe(e, ue(t));
589
+ } catch {
590
+ }
591
+ return r.push(s[0], s[1], o, ge(t, e, n)), r;
592
+ }
593
+ const Te = {
594
+ fields: (t) => Me(t),
595
+ "same-field": (t) => ze(t),
596
+ create: (t) => Be(t),
597
+ "text-position": (t) => De(t),
598
+ "text-delete-insert": (t) => Fe(t),
599
+ "text-split": (t) => Je(t),
600
+ "text-merge": (t) => Pe(t),
601
+ reorder: (t) => Ke(t),
602
+ reparent: (t) => _e(t),
603
+ "delete-edit": (t) => Ue(t),
604
+ "duplicate-delete": (t) => qe(t),
605
+ undo: (t) => Le(t),
606
+ "undo-reconnect": (t) => Ve(t),
607
+ delivery: (t) => Ge(t),
608
+ crash: (t) => Qe(t),
609
+ "blobs-late": (t) => Ze(t),
610
+ fuzz: (t, e) => Xe(t, e)
611
+ };
612
+ function ge(t, e, n) {
613
+ const s = Te[e];
614
+ if (!s) return { kind: "failed", reason: `no scenario ${e}` };
615
+ try {
616
+ const r = s(t, n);
617
+ return r === null ? { kind: "passed" } : { kind: "skipped", reason: r };
618
+ } catch (r) {
619
+ return r instanceof X ? { kind: "skipped", reason: r.message } : { kind: "failed", reason: I(r) };
620
+ }
621
+ }
622
+ function T(t, e, n) {
623
+ const s = t.state(e);
624
+ return { after: s, changed: je(t.schema, n, s) };
625
+ }
626
+ const Q = (t) => S(t ?? null);
627
+ function D(t, e, n, s) {
628
+ for (const { slot: r, value: o } of n.list()) {
629
+ const i = he(t, e, r);
630
+ if (!x(i, o)) throw new Error(`${s} change to ${r[0]}.${r[1]} was lost: expected ${Q(o)}, found ${Q(i)}`);
631
+ }
632
+ }
633
+ const ce = (t) => `[${[...t].map((e) => JSON.stringify(e)).join(", ")}]`, m = (t) => JSON.stringify(t), we = (t) => Array.from(t).length;
634
+ function Me(t) {
635
+ const e = new w(t, 2, 11), n = e.settle();
636
+ e.step(0, { kind: "SetField", slot: 0, value: 101 }), e.step(1, { kind: "SetField", slot: 1, value: 202 });
637
+ const s = T(e, 0, n).changed, r = T(e, 1, n).changed;
638
+ if (s.size === 0 || r.size === 0) throw new Error(`SetField changed no register (slot 0: ${s.size} changes, slot 1: ${r.size})`);
639
+ const o = s.filter((l) => !r.has(l)), i = r.filter((l) => !s.has(l));
640
+ if (o.size === 0 || i.size === 0)
641
+ throw new Error(`SetField slots 0 and 1 changed the same registers: [${s.list().map(({ slot: l }) => `(${m(l[0])}, ${m(l[1])})`).join(", ")}]`);
642
+ const c = new Set(o.list().map(({ slot: l }) => l[0]));
643
+ if (!i.list().some(({ slot: l }) => c.has(l[0]))) throw new Error("SetField slots 0 and 1 changed different objects; they must be two fields of one object");
644
+ const h = e.settle();
645
+ return D(e.schema, h, o, "replica 0's"), D(e.schema, h, i, "replica 1's"), null;
646
+ }
647
+ function ze(t) {
648
+ const e = new w(t, 3, 12), n = e.settle(), s = [];
649
+ for (let i = 0; i < 3; i += 1)
650
+ e.step(i, { kind: "SetField", slot: 0, value: 300 + i }), s.push(T(e, i, n).changed);
651
+ const r = e.settle(), o = new N();
652
+ for (const i of s) for (const { slot: c } of i.list()) o.set(c, null);
653
+ for (const { slot: i } of o.list()) {
654
+ const c = he(e.schema, r, i);
655
+ if (!s.some((h) => h.has(i) && x(h.get(i), c)))
656
+ throw new Error(`${i[0]}.${i[1]} resolved to ${Q(c)}, which no replica wrote`);
657
+ }
658
+ return null;
659
+ }
660
+ function Be(t) {
661
+ const e = new w(t, 3, 13), n = e.settle(), s = [];
662
+ for (let o = 0; o < 3; o += 1) {
663
+ e.step(o, { kind: "Create" });
664
+ const i = Z(n, e.state(o));
665
+ if (i.length === 0) throw new Error(`Create on replica ${o} created no object`);
666
+ s.push(i);
667
+ }
668
+ s.forEach((o, i) => {
669
+ for (let c = i + 1; c < s.length; c += 1) {
670
+ const h = new Set(s[c]), l = o.find((a) => h.has(a));
671
+ if (l !== void 0) throw new Error(`replicas ${i} and ${c} both created ${l}`);
672
+ }
673
+ });
674
+ const r = E(e.settle());
675
+ return s.forEach((o, i) => {
676
+ const c = o.find((h) => !Object.hasOwn(r, h));
677
+ if (c !== void 0) throw new Error(`replica ${i}'s new object ${c} is gone after the merge`);
678
+ }), null;
679
+ }
680
+ function _(t, e) {
681
+ const n = t.kit.text?.(t.doc(0)) ?? null;
682
+ if (n === null) return null;
683
+ const s = we(n.split(`
684
+ `)[0] ?? "");
685
+ if (s < e) {
686
+ const r = Array.from("abcdefgh").slice(0, e - s).join("");
687
+ if (!t.script(0, { kind: "InsertText", at: s, text: r })) return null;
688
+ t.settle();
689
+ }
690
+ return t.text(0);
691
+ }
692
+ function U(t, e) {
693
+ let n = t;
694
+ for (const s of e) {
695
+ const r = n.indexOf(s);
696
+ if (r < 0) return null;
697
+ n = n.slice(0, r) + n.slice(r + s.length);
698
+ }
699
+ return n;
700
+ }
701
+ function De(t) {
702
+ const e = new w(t, 2, 14), n = _(e, 2);
703
+ if (n === null) return "the kit has no text";
704
+ const s = ne + se + re, r = `XY${oe}${ie}`;
705
+ e.step(0, { kind: "InsertText", at: 1, text: "zh" }), e.step(0, { kind: "DeleteText", at: 1, len: 2 }), e.step(0, { kind: "InsertText", at: 1, text: ne }), e.step(0, { kind: "InsertText", at: 2, text: se }), e.step(0, { kind: "InsertText", at: 3, text: re }), e.step(1, { kind: "InsertText", at: 1, text: "XY" }), e.step(1, { kind: "InsertText", at: 3, text: oe + ie }), e.settle();
706
+ const o = e.text(0), i = U(o, [s, r]);
707
+ if (i === null) throw new Error(`an insertion was lost or interleaved: ${m(o)}`);
708
+ if (i !== n) throw new Error(`the text around the insertions changed: ${m(i)}, was ${m(n)}`);
709
+ return null;
710
+ }
711
+ function Fe(t) {
712
+ const e = new w(t, 2, 15), n = _(e, 4);
713
+ if (n === null) return "the kit has no text";
714
+ e.step(0, { kind: "DeleteText", at: 1, len: 2 }), e.step(1, { kind: "InsertText", at: 2, text: "ZZ" }), e.settle();
715
+ const s = e.text(0), r = Array.from(n).filter((i, c) => c < 1 || c >= 3).join(""), o = U(s, ["ZZ"]);
716
+ if (o === null) throw new Error(`the typing inside the deleted range was lost: ${m(s)}`);
717
+ if (o !== r) throw new Error(`after deleting 2 characters at 1 the text is ${m(o)}, expected ${m(r)}`);
718
+ return null;
719
+ }
720
+ const j = (t) => t.split(`
721
+ `).length - 1;
722
+ function Je(t) {
723
+ const e = new w(t, 2, 16), n = _(e, 4);
724
+ if (n === null) return "the kit has no text";
725
+ e.step(0, { kind: "SplitParagraph", at: 2 }), e.step(1, { kind: "InsertText", at: 2, text: "QQ" }), e.step(1, { kind: "InsertText", at: 5, text: "WW" }), e.settle();
726
+ const s = e.text(0);
727
+ if (j(s) !== j(n) + 1) throw new Error(`the split made ${j(s)} paragraph breaks out of ${j(n)}: ${m(s)}`);
728
+ const r = U(s, ["QQ", "WW"]);
729
+ if (r === null) throw new Error(`typing concurrent with the split was lost: ${m(s)}`);
730
+ if (r.replaceAll(`
731
+ `, "") !== n.replaceAll(`
732
+ `, "")) throw new Error(`the split changed the text: ${m(r)}, was ${m(n)}`);
733
+ return null;
734
+ }
735
+ function Pe(t) {
736
+ const e = new w(t, 2, 17);
737
+ let n = _(e, 2);
738
+ if (n === null) return "the kit has no text";
739
+ if (!n.includes(`
740
+ `)) {
741
+ const i = we(n);
742
+ e.step(0, { kind: "SplitParagraph", at: Math.min(i, 1) }), e.settle(), n = e.text(0);
743
+ }
744
+ const s = Array.from(n).indexOf(`
745
+ `);
746
+ if (s < 0) throw new Error("SplitParagraph made no paragraph break");
747
+ e.step(0, { kind: "MergeParagraph", at: s }), e.step(1, { kind: "InsertText", at: s + 1, text: "RR" }), e.settle();
748
+ const r = e.text(0);
749
+ if (j(r) + 1 !== j(n)) throw new Error(`the merge left ${j(r)} paragraph breaks out of ${j(n)}: ${m(r)}`);
750
+ const o = U(r, ["RR"]);
751
+ if (o === null) throw new Error(`typing in the merged paragraph was lost: ${m(r)}`);
752
+ if (o.replaceAll(`
753
+ `, "") !== n.replaceAll(`
754
+ `, "")) throw new Error(`the merge changed the text: ${m(o)}, was ${m(n)}`);
755
+ return null;
756
+ }
757
+ function Re(t, e) {
758
+ if (t.size !== e.size) return !1;
759
+ for (const [n, s] of t) {
760
+ const r = e.get(n);
761
+ if (!r || r.size !== s.size || [...s].some((o) => !r.has(o))) return !1;
762
+ }
763
+ return !0;
764
+ }
765
+ const ae = (t) => [...t].sort(W);
766
+ function Ke(t) {
767
+ const e = new w(t, 2, 18), n = e.settle();
768
+ e.step(0, { kind: "Reorder", which: 0 }), e.step(1, { kind: "Reorder", which: 1 });
769
+ const s = e.state(0);
770
+ if (Re(F(e.schema, s), F(e.schema, n)) && x(s, n)) throw new Error("Reorder changed nothing");
771
+ const r = e.settle(), o = F(e.schema, r);
772
+ for (const [i, c] of F(e.schema, n)) {
773
+ const h = o.get(i);
774
+ if (h === void 0) continue;
775
+ const l = ae([...c].filter((d) => !h.has(d))), a = ae([...h].filter((d) => !c.has(d)));
776
+ if (l.length > 0 || a.length > 0)
777
+ throw new Error(`${i} changed its items under concurrent reorders: lost ${ce(l)}, gained ${ce(a)}`);
778
+ }
779
+ return null;
780
+ }
781
+ const J = (t) => `{${[...t].map(([e, n]) => `${JSON.stringify(e)}: ${n}`).join(", ")}}`;
782
+ function _e(t) {
783
+ const e = new w(t, 2, 19), n = e.settle();
784
+ e.step(0, { kind: "Reparent", which: 0 }), e.step(1, { kind: "Reparent", which: 1 });
785
+ const s = e.settle(), r = ee(e.schema, n), o = ee(e.schema, s);
786
+ if (J(r) !== J(o)) throw new Error(`concurrent reparents changed the trees' node counts: ${J(r)} to ${J(o)}`);
787
+ return null;
788
+ }
789
+ function Ue(t) {
790
+ const e = new w(t, 2, 20), n = e.settle();
791
+ e.step(0, { kind: "Delete" }), e.step(1, { kind: "SetField", slot: 0, value: 707 });
792
+ const s = new Set(Ie(n, e.state(0)));
793
+ if (s.size === 0) throw new Error("Delete deleted no object");
794
+ const r = T(e, 1, n).changed, o = e.settle(), i = E(o), c = [...s].find((h) => Object.hasOwn(i, h));
795
+ if (c !== void 0) throw new Error(`${c} was deleted on replica 0 but exists after the merge (delete must win over edit)`);
796
+ return D(e.schema, o, r.filter((h) => !s.has(h[0])), "replica 1's"), null;
797
+ }
798
+ function qe(t) {
799
+ const e = new w(t, 2, 21), n = e.settle();
800
+ if (e.step(0, { kind: "Duplicate" }), e.step(1, { kind: "Delete" }), Z(n, e.state(0)).length === 0) throw new Error("Duplicate created no object");
801
+ return e.settle(), null;
802
+ }
803
+ function Le(t) {
804
+ const e = new w(t, 2, 22);
805
+ e.settle(), e.step(0, { kind: "SetField", slot: 0, value: 11 });
806
+ const n = e.settle();
807
+ e.step(1, { kind: "SetField", slot: 0, value: 22 }), e.step(1, { kind: "SetField", slot: 1, value: 23 });
808
+ const s = T(e, 1, n).changed;
809
+ if (e.settle(), !t.undo(e.doc(0))) throw new Error("replica 0 had no step to undo after its edit");
810
+ const r = e.settle();
811
+ return D(e.schema, r, s, "replica 1's later"), null;
812
+ }
813
+ function Ve(t) {
814
+ const e = new w(t, 2, 23);
815
+ e.settle();
816
+ const n = e.state(0);
817
+ e.step(0, { kind: "SetField", slot: 1, value: 31 });
818
+ const s = T(e, 0, n).changed, r = e.settle();
819
+ e.step(1, { kind: "SetField", slot: 0, value: 41 });
820
+ const o = T(e, 1, r).changed;
821
+ if (!t.undo(e.doc(0))) throw new Error("replica 0 had no step to undo");
822
+ if (!t.redo(e.doc(0))) throw new Error("replica 0 had no step to redo after its undo");
823
+ const i = e.settle();
824
+ return D(e.schema, i, o, "replica 1's offline"), D(e.schema, i, s.filter((c) => !o.has(c)), "replica 0's redone"), null;
825
+ }
826
+ function q(t, e, n) {
827
+ for (let s = 0; s < 8; s += 1) {
828
+ const r = t.kit.randomEdit(t.doc(e), t.rng);
829
+ if (r !== null) return r;
830
+ }
831
+ if (t.script(e, { kind: "SetField", slot: n % 2, value: 9e3 + n })) return "SetField";
832
+ throw new Error("random_edit applied nothing in 8 tries and the adapter has no SetField");
833
+ }
834
+ function R(t, e, n) {
835
+ const s = t.doc(e);
836
+ return { bytes: t.kit.exportChanges(s, n), version: t.kit.version(s) };
837
+ }
838
+ function Ge(t) {
839
+ const e = new w(t, 3, 24), n = e.docs.length, s = e.docs.map((l) => t.version(l)), r = [], o = /* @__PURE__ */ new Map();
840
+ for (let l = 0; l < 4; l += 1) {
841
+ for (let a = 0; a < n; a += 1) {
842
+ q(e, a, l * 10 + a);
843
+ const { bytes: d, version: f } = R(e, a, s[a]);
844
+ r.push(d), s[a] = f;
845
+ }
846
+ l === 1 && e.sync();
847
+ }
848
+ e.sync(), s.forEach((l, a) => {
849
+ r.push(R(e, a, l).bytes);
850
+ for (const [d, f] of A(t, e.doc(a))) o.set(d, f);
851
+ });
852
+ const i = e.check(), c = S(i ?? null), h = [];
853
+ for (let l = 0; l < 2; l += 1) {
854
+ const a = t.join(e.initial.snapshot, e.initial.blobs);
855
+ t.addBlobs?.(a, o);
856
+ let d = [...r];
857
+ if (l === 0) {
858
+ e.rng.shuffle(d);
859
+ for (let u = 0; u < Math.floor(r.length / 2); u += 1) {
860
+ const g = e.rng.below(d.length), O = d[g] ?? new Uint8Array(0);
861
+ d.splice(e.rng.below(d.length + 1), 0, O);
862
+ }
863
+ } else
864
+ d.reverse(), d = d.flatMap((u) => [u, u]);
865
+ d.forEach((u, g) => y(`late replica ${l} at message ${g}: `, () => t.importChanges(a, u)));
866
+ const f = t.state(a);
867
+ S(f ?? null) !== c && h.push(`late replica ${l} differs at ${B(i, f) ?? ""} before exchanging its own changes`), e.docs.push(a);
868
+ }
869
+ try {
870
+ e.settle();
871
+ } catch (l) {
872
+ throw new Error(h.length === 0 ? I(l) : `${I(l)} (${h.join("; ")})`);
873
+ }
874
+ return null;
875
+ }
876
+ function me(t, e) {
877
+ return { snapshot: t.snapshot(e), blobs: A(t, e), log: [] };
878
+ }
879
+ function be(t, e) {
880
+ const n = y("reopening the saved snapshot: ", () => t.join(e.snapshot, e.blobs));
881
+ return e.log.forEach((s, r) => y(`replaying logged commit ${r}: `, () => t.importChanges(n, s))), n;
882
+ }
883
+ function Qe(t) {
884
+ const e = new w(t, 2, 25);
885
+ let n = { snapshot: e.initial.snapshot, blobs: new Map(e.initial.blobs), log: [] }, s = t.version(e.doc(0));
886
+ for (let c = 0; c < 6; c += 1) {
887
+ c === 2 && (n = me(t, e.doc(0)), s = t.version(e.doc(0))), q(e, 0, c);
888
+ const { bytes: h, version: l } = R(e, 0, s);
889
+ n.log.push(h);
890
+ for (const [a, d] of A(t, e.doc(0))) n.blobs.set(a, d);
891
+ s = l;
892
+ }
893
+ const r = e.state(0);
894
+ e.docs[0] = be(t, n);
895
+ const o = e.state(0);
896
+ if (!x(o, r)) throw new Error(`the recovered replica differs at ${B(r, o) ?? ""}`);
897
+ const i = e.settle();
898
+ if (!x(i, r)) throw new Error(`the recovered changes did not reach the peer: ${B(r, i) ?? ""}`);
899
+ return null;
900
+ }
901
+ function We(t, e) {
902
+ const n = /* @__PURE__ */ new Set();
903
+ for (const s of Object.values(E(e))) {
904
+ const r = s;
905
+ for (const [o, i] of t.fieldsOf(typeof r.$type == "string" ? r.$type : "")) {
906
+ const c = r[o];
907
+ i.kind === "blob" && c && typeof c == "object" && typeof c.hash == "string" && n.add(c.hash);
908
+ }
909
+ }
910
+ return [...n].sort(W);
911
+ }
912
+ function Ze(t) {
913
+ const e = new w(t, 2, 26);
914
+ e.settle();
915
+ const n = e.state(1);
916
+ e.step(0, { kind: "InsertBlob", seed: 7 });
917
+ const s = e.state(0), r = t.version(e.doc(1)), o = t.exportChanges(e.doc(0), r);
918
+ let i = null;
919
+ try {
920
+ t.importChanges(e.doc(1), o);
921
+ } catch (a) {
922
+ i = a;
923
+ }
924
+ const c = e.state(1);
925
+ if (i === null && !x(c, s))
926
+ throw new Error(`changes imported without their blobs differ at ${B(s, c) ?? ""}`);
927
+ if (i !== null && !x(c, n)) throw new Error("a refused import without blobs changed the replica");
928
+ q(e, 1, 1), t.addBlobs?.(e.doc(1), A(t, e.doc(0))), y("importing again once the blobs arrived: ", () => t.importChanges(e.doc(1), o));
929
+ const h = e.settle(), l = A(t, e.doc(1));
930
+ if (l.size > 0) {
931
+ const a = We(e.schema, h).find((d) => !l.has(d));
932
+ if (a !== void 0) throw new Error(`replica 1 references blob ${a} it does not hold once the blobs arrived`);
933
+ }
934
+ for (let a = 0; a < 2; a += 1) y(`replica ${a} cannot save after the late blobs: `, () => t.exportFile?.(e.doc(a)));
935
+ return null;
936
+ }
937
+ function Xe(t, e) {
938
+ for (const n of e.replicas)
939
+ for (const s of e.seeds) y(`${n} replicas, seed ${s}: `, () => Ye(t, n, s, e.steps));
940
+ return null;
941
+ }
942
+ function Ye(t, e, n, s) {
943
+ const r = new w(t, e, n), o = r.docs.map((a) => t.version(a)), i = r.docs.map(() => ({ snapshot: r.initial.snapshot, blobs: new Map(r.initial.blobs), log: [] })), c = r.docs.map(() => []), h = [], l = () => h.slice(-12).join(", ");
944
+ for (let a = 0; a < s; a += 1) {
945
+ const d = r.rng.below(e), f = r.rng.below(100), u = y(`step ${a} on replica ${d}: `, () => {
946
+ try {
947
+ return He(r, d, f, a, o, i, c);
948
+ } catch (b) {
949
+ throw new Error(`${I(b)}; after ${l()}`);
950
+ }
951
+ });
952
+ h.push(`${d}:${u}`);
953
+ const g = o[d], { bytes: O, version: k } = R(r, d, g);
954
+ if (!Ae(k, g)) {
955
+ const b = A(t, r.doc(d));
956
+ i[d].log.push(O);
957
+ for (const [$, M] of b) i[d].blobs.set($, M);
958
+ c.forEach(($, M) => {
959
+ M !== d && $.push({ bytes: O, blobs: b });
960
+ }), o[d] = k;
961
+ }
962
+ }
963
+ c.forEach((a, d) => {
964
+ r.rng.shuffle(a);
965
+ for (const f of a.splice(0)) {
966
+ const u = r.doc(d);
967
+ t.addBlobs?.(u, f.blobs), y(`final delivery to replica ${d}: `, () => {
968
+ try {
969
+ t.importChanges(u, f.bytes);
970
+ } catch (g) {
971
+ throw new Error(`${I(g)}; after ${l()}`);
972
+ }
973
+ });
974
+ }
975
+ });
976
+ try {
977
+ r.settle();
978
+ } catch (a) {
979
+ throw new Error(`${I(a)}; after ${l()}`);
980
+ }
981
+ }
982
+ function He(t, e, n, s, r, o, i) {
983
+ const { kit: c } = t;
984
+ if (n < 55) return q(t, e, s);
985
+ if (n < 62) return `undo=${c.undo(t.doc(e))}`;
986
+ if (n < 67) return `redo=${c.redo(t.doc(e))}`;
987
+ if (n < 90) {
988
+ const a = i[e];
989
+ if (a.length === 0) return "idle";
990
+ const d = t.rng.below(a.length), f = t.rng.chance(20) ? a[d] : a.splice(d, 1)[0], u = t.doc(e);
991
+ return c.addBlobs?.(u, f.blobs), c.importChanges(u, f.bytes), "deliver";
992
+ }
993
+ if (n < 95)
994
+ return o[e] = me(c, t.doc(e)), "save";
995
+ const h = t.state(e);
996
+ t.docs[e] = be(c, o[e]);
997
+ const l = t.state(e);
998
+ if (!x(l, h)) throw new Error(`the recovered replica differs at ${B(h, l) ?? ""}`);
999
+ return r[e] = c.version(t.doc(e)), "crash";
1000
+ }
1001
+ export {
1002
+ L as DEFAULT_OPTIONS,
1003
+ st as KitEngineAdapter,
1004
+ w as Net,
1005
+ N as Registers,
1006
+ de as Report,
1007
+ Ee as Rng,
1008
+ fe as SCENARIOS,
1009
+ Ce as SUITE,
1010
+ K as SchemaInfo,
1011
+ X as Skip,
1012
+ Ae as bytesEqual,
1013
+ Oe as checkReferences,
1014
+ W as compareStrings,
1015
+ Z as created,
1016
+ ve as decodeBlobBundle,
1017
+ Ie as deleted,
1018
+ H as describeStep,
1019
+ ot as digest,
1020
+ xe as encodeBlobBundle,
1021
+ B as firstDifference,
1022
+ Se as isRegister,
1023
+ F as listSets,
1024
+ E as objects,
1025
+ nt as optionsFromEnv,
1026
+ he as register,
1027
+ je as registerChanges,
1028
+ it as run,
1029
+ ct as runOne,
1030
+ x as sameValue,
1031
+ ee as treeNodes,
1032
+ rt as utf16Index
1033
+ };