-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
1151 lines (1106 loc) · 43.6 KB
/
index.html
File metadata and controls
1151 lines (1106 loc) · 43.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Convergent Replicated Struct (CR-Struct)</title>
<meta
name="description"
content="Specification and reference JavaScript binding for a convergent replicated struct delta CRDT with fixed keys, UUIDv7 field identities, per-field tombstone acknowledgements, and garbage collection."
/>
<meta
name="keywords"
content="cr-struct, convergent replicated struct, crdt, uuidv7, delta, snapshot, acknowledgement, garbage collection, javascript, typescript"
/>
<meta name="author" content="Jori Lehtinen" />
<meta
name="robots"
content="index, follow, max-image-preview:large, max-snippet:-1, max-video-preview:-1"
/>
<meta name="application-name" content="convergent-replicated-struct" />
<link
rel="canonical"
href="https://sovereignbase.dev/convergent-replicated-struct/"
/>
<link
rel="icon"
type="image/png"
href="https://sovereignbase.dev/sovereignbase_logo.png"
/>
<link
rel="apple-touch-icon"
href="https://sovereignbase.dev/sovereignbase_logo.png"
/>
<meta property="og:type" content="article" />
<meta property="og:locale" content="en_US" />
<meta property="og:site_name" content="Sovereignbase" />
<meta
property="og:title"
content="Convergent Replicated Struct (CR-Struct)"
/>
<meta
property="og:description"
content="Specification and reference JavaScript binding for a convergent replicated struct delta CRDT with fixed keys, UUIDv7 field identities, per-field tombstone acknowledgements, and garbage collection."
/>
<meta
property="og:url"
content="https://sovereignbase.dev/convergent-replicated-struct/"
/>
<meta
property="og:image"
content="https://sovereignbase.dev/sovereignbase_social_sharing_image.png"
/>
<meta property="og:image:type" content="image/png" />
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="630" />
<meta
property="og:image:alt"
content="Sovereignbase social sharing image for the CR-Struct specification."
/>
<meta name="twitter:card" content="summary_large_image" />
<meta
name="twitter:title"
content="Convergent Replicated Struct (CR-Struct)"
/>
<meta
name="twitter:description"
content="Specification and reference JavaScript binding for a convergent replicated struct delta CRDT with fixed keys, UUIDv7 field identities, per-field tombstone acknowledgements, and garbage collection."
/>
<meta
name="twitter:image"
content="https://sovereignbase.dev/sovereignbase_social_sharing_image.png"
/>
<meta
name="twitter:image:alt"
content="Sovereignbase social sharing image for the CR-Struct specification."
/>
<meta name="twitter:creator" content="@jortsupetterson" />
<script
src="https://www.w3.org/Tools/respec/respec-w3c"
class="remove"
defer
></script>
<script class="remove">
var respecConfig = {
specStatus: 'unofficial',
shortName: 'convergent-replicated-struct',
github: 'sovereignbase/convergent-replicated-struct',
latestVersion: null,
edDraftURI: null,
editors: [
{ name: 'Jori Lehtinen', url: 'https://github.com/jortsupetterson' },
{
name: 'GPT-5.4',
url: 'https://openai.com/en-US/index/introducing-gpt-5-4/',
},
],
xref: 'web-platform',
lint: {
'informative-dfn': true,
'local-refs-exist': true,
'no-unused-dfns': true,
},
localBiblio: {
MARTIN_KLEPPMANN: {
title: 'CRDTs: The Hard Parts',
href: 'https://martin.kleppmann.com/2020/07/06/crdt-hard-parts-hydra.html',
publisher: 'Martin Kleppmann',
date: '2020-06',
},
RFC9562: {
title: 'Universally Unique IDentifiers (UUIDs)',
href: 'https://www.rfc-editor.org/rfc/rfc9562',
publisher: 'IETF',
date: '2024-05',
},
INFRA: {
title: 'Infra',
href: 'https://infra.spec.whatwg.org/',
publisher: 'WHATWG',
date: '2026-03',
},
HTML: {
title: '2.7 Safe passing of structured data',
href: 'https://html.spec.whatwg.org/multipage/structured-data.html',
publisher: 'WHATWG',
date: '2026-04',
},
},
}
</script>
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@graph": [
{
"@type": "Organization",
"@id": "https://sovereignbase.dev/convergent-replicated-struct/#organization",
"name": "Sovereignbase",
"url": "https://github.com/sovereignbase",
"alternateName": ["@sovereignbase"],
"logo": {
"@type": "ImageObject",
"url": "https://sovereignbase.dev/sovereignbase_logo.png",
"width": 320,
"height": 320
},
"sameAs": [
"https://github.com/sovereignbase",
"https://jsr.io/@sovereignbase",
"https://www.npmjs.com/org/sovereignbase"
]
},
{
"@type": "Person",
"@id": "https://sovereignbase.dev/convergent-replicated-struct/#author",
"name": "Jori Lehtinen",
"url": "https://github.com/jortsupetterson",
"alternateName": ["jortsupetterson", "@jortsupetterson"],
"sameAs": [
"https://github.com/jortsupetterson",
"https://www.npmjs.com/~jortsupetterson",
"https://jsr.io/user/ab186a13-a64f-429e-a757-927aa4fa6e6c",
"https://linkedin.com/in/jortsupetterson",
"https://x.com/jortsupetterson",
"https://www.youtube.com/@jortsupetterson"
]
},
{
"@type": "WebSite",
"@id": "https://sovereignbase.dev/convergent-replicated-struct/#website",
"url": "https://sovereignbase.dev/convergent-replicated-struct/",
"name": "convergent-replicated-struct",
"inLanguage": "en",
"publisher": {
"@id": "https://sovereignbase.dev/convergent-replicated-struct/#organization"
}
},
{
"@type": "SoftwareSourceCode",
"@id": "https://sovereignbase.dev/convergent-replicated-struct/#software",
"name": "@sovereignbase/convergent-replicated-struct",
"alternateName": ["convergent-replicated-struct", "cr-struct"],
"url": "https://sovereignbase.dev/convergent-replicated-struct/",
"description": "Convergent replicated struct delta CRDT with fixed keys, UUIDv7 field identities, per-field tombstone acknowledgements, and garbage collection.",
"codeRepository": "https://github.com/sovereignbase/convergent-replicated-struct",
"license": "https://www.apache.org/licenses/LICENSE-2.0",
"programmingLanguage": ["TypeScript", "JavaScript"],
"runtimePlatform": [
"Node.js",
"Web Browser",
"Bun",
"Deno",
"Cloudflare Workers",
"Edge Runtime"
],
"keywords": [
"uuidv7",
"convergent",
"replicated",
"delta",
"crdt",
"convergent-replicated-struct",
"cr-struct",
"core",
"crud",
"mags"
],
"image": "https://sovereignbase.dev/sovereignbase_social_sharing_image.png",
"isAccessibleForFree": true,
"author": {
"@id": "https://sovereignbase.dev/convergent-replicated-struct/#author"
},
"maintainer": {
"@id": "https://sovereignbase.dev/convergent-replicated-struct/#organization"
},
"publisher": {
"@id": "https://sovereignbase.dev/convergent-replicated-struct/#organization"
},
"sameAs": [
"https://jsr.io/@sovereignbase/convergent-replicated-struct/",
"https://www.npmjs.com/package/@sovereignbase/convergent-replicated-struct"
]
},
{
"@type": "TechArticle",
"@id": "https://sovereignbase.dev/convergent-replicated-struct/#specification",
"headline": "Convergent Replicated Struct (CR-Struct)",
"name": "Convergent Replicated Struct (CR-Struct)",
"url": "https://sovereignbase.dev/convergent-replicated-struct/",
"description": "Specification and reference JavaScript binding for a convergent replicated struct delta CRDT with fixed keys, UUIDv7 field identities, per-field tombstone acknowledgements, and garbage collection.",
"inLanguage": "en",
"isPartOf": {
"@id": "https://sovereignbase.dev/convergent-replicated-struct/#website"
},
"mainEntityOfPage": {
"@id": "https://sovereignbase.dev/convergent-replicated-struct/#webpage"
},
"about": {
"@id": "https://sovereignbase.dev/convergent-replicated-struct/#software"
},
"author": {
"@id": "https://sovereignbase.dev/convergent-replicated-struct/#author"
},
"publisher": {
"@id": "https://sovereignbase.dev/convergent-replicated-struct/#organization"
},
"image": {
"@type": "ImageObject",
"url": "https://sovereignbase.dev/sovereignbase_social_sharing_image.png",
"width": 1200,
"height": 630
}
},
{
"@type": "WebPage",
"@id": "https://sovereignbase.dev/convergent-replicated-struct/#webpage",
"url": "https://sovereignbase.dev/convergent-replicated-struct/",
"name": "Convergent Replicated Struct (CR-Struct)",
"description": "Specification and reference JavaScript binding for a convergent replicated struct delta CRDT with fixed keys, UUIDv7 field identities, per-field tombstone acknowledgements, and garbage collection.",
"inLanguage": "en",
"isPartOf": {
"@id": "https://sovereignbase.dev/convergent-replicated-struct/#website"
},
"about": {
"@id": "https://sovereignbase.dev/convergent-replicated-struct/#software"
},
"author": {
"@id": "https://sovereignbase.dev/convergent-replicated-struct/#author"
},
"publisher": {
"@id": "https://sovereignbase.dev/convergent-replicated-struct/#organization"
},
"primaryImageOfPage": {
"@type": "ImageObject",
"url": "https://sovereignbase.dev/sovereignbase_social_sharing_image.png",
"width": 1200,
"height": 630
},
"relatedLink": [
"https://github.com/sovereignbase",
"https://jsr.io/@sovereignbase",
"https://www.npmjs.com/org/sovereignbase",
"https://github.com/sovereignbase/convergent-replicated-struct",
"https://jsr.io/@sovereignbase/convergent-replicated-struct/",
"https://www.npmjs.com/package/@sovereignbase/convergent-replicated-struct",
"https://github.com/jortsupetterson",
"https://www.npmjs.com/~jortsupetterson",
"https://jsr.io/user/ab186a13-a64f-429e-a757-927aa4fa6e6c",
"https://linkedin.com/in/jortsupetterson",
"https://x.com/jortsupetterson",
"https://www.youtube.com/@jortsupetterson"
]
}
]
}
</script>
</head>
<body>
<section id="abstract">
<p>
This document defines Convergent Replicated Struct (CR-Struct), a delta
CRDT for a fixed-key object-like data structure. Each field resolves to
one visible value. Every overwrite is assigned a UUIDv7, and overwritten
identifiers are retained as per-field tombstones.
</p>
<p>
The specification defines the data structure, merge rules, snapshot and
delta formats, garbage-collection rules, acknowledgement formats, and
one conforming JavaScript binding.
</p>
</section>
<section id="sotd">
<p>
CR-Struct is designed to serve as a small replicated core for fixed-key
object state, configuration state, and nested CRDT compositions.
</p>
<p>
This specification is maintained alongside a reference implementation.
</p>
<p>To use the reference implementation see:</p>
<ul>
<li>
<a
href="https://www.npmjs.com/package/@sovereignbase/convergent-replicated-struct"
>NPM Registry</a
>
</li>
<li>
<a href="https://jsr.io/@sovereignbase/convergent-replicated-struct"
>JSR Registry</a
>
</li>
</ul>
<p>Inspired by [[MARTIN_KLEPPMANN]].</p>
</section>
<section id="conformance">
<p>
Unless otherwise stated, data-structure and algorithm terms such as
<em>list</em>, <em>set</em>, <em>map</em>, <em>record</em>,
<em>parameters</em>, <em>variables</em>, and <em>iterate</em> are used
with the meanings defined by Infra [[INFRA]].
</p>
<p>
A conforming implementation MUST preserve the fixed-key replica shape,
field overwrite semantics, convergence properties, and per-field
garbage-collection safety defined by the algorithms in this
specification.
</p>
</section>
<section>
<h2>Terminology</h2>
<p>
A <dfn>CR-Struct replica</dfn> is one independently mutable instance of
the data structure.
</p>
<p>
A <dfn>UUIDv7 identifier</dfn> is an identifier that is a valid UUID
version 7 as defined by [[RFC9562]].
</p>
<p>
A <dfn>serializable object</dfn> is an object that supports structured
serialization and later structured deserialization as defined by
[[HTML]].
</p>
<p>
A <dfn>field key</dfn> is one own property name present in the replica's
default object.
</p>
<p>
A <dfn>field value</dfn> is the consumer-defined payload presently
selected for one <a>field key</a>.
</p>
<p>
A <dfn>field entry</dfn> is the internal record for one
<a>field key</a>, consisting of a current winning <code>uuidv7</code>,
current winning <code>value</code>, current winning
<code>predecessor</code>, and a set of retained <code>tombstones</code>.
</p>
<p>
A <dfn>predecessor identifier</dfn> is an <a>UUIDv7 identifier</a>
naming the immediately previous winning identifier for the same
<a>field key</a>.
</p>
<p>
A <dfn>field tombstone</dfn> is one overwritten
<a>UUIDv7 identifier</a> retained by a <a>field entry</a>.
</p>
<p>
An <dfn>acknowledgement frontier</dfn> is, for one <a>field key</a>, the
largest retained <a>field tombstone</a> under lexicographic comparison.
</p>
<p>
A <dfn>snapshot</dfn> is the full <a>serializable object</a> form of the
current <a>CR-Struct replica</a>.
</p>
<p>
A <dfn>delta</dfn> is a partial <a>snapshot</a> containing only the
fields a replica wishes to share.
</p>
</section>
<section id="core-model">
<h2>Core Model</h2>
<p>
A <a>CR-Struct replica</a> consists of a fixed set of
<a>field key</a>(s) defined at construction time. Each key stores
exactly one current visible <a>field value</a>, one current winning
<a>UUIDv7 identifier</a>, one current winning
<a>predecessor identifier</a>, and a set of retained
<a>field tombstone</a>(s).
</p>
<p>
The replica shape MUST remain fixed after construction. A conforming
implementation MUST ignore unknown keys arriving from snapshots, deltas,
and acknowledgement frontiers, and MUST preserve only keys present in
the default object supplied at construction time.
</p>
<p>
Each local overwrite produces a fresh winning
<a>UUIDv7 identifier</a>, moves the previous winning identifier into the
field's tombstone set, and records the previous winning identifier as
the new <a>predecessor identifier</a>.
</p>
<p>
On merge, each field is resolved independently. Incoming tombstones
extend local knowledge. Incoming winners may be adopted directly, may be
rejected with a reply delta, or may trigger a fresh local overwrite that
collapses a conflicting same-UUID state into one newer winning event.
</p>
<p>
A snapshot or delta input MAY contain malformed or unknown members. A
conforming implementation processes only the known top-level field keys
and ignores entries that do not successfully parse against the local
default field runtime type.
</p>
</section>
<section>
<h2>Data Structure</h2>
<p>
The same logical replica can be viewed through three useful
representations: the serializable snapshot form used for transport and
storage, one possible runtime projection used internally by a binding,
and the final consumer-facing visible object.
</p>
<section>
<h3>Snapshot</h3>
<pre
class="json"
data-include="./examples/snapshot.json"
data-include-format="text"
></pre>
</section>
<section>
<h3>Runtime</h3>
<pre
class="js"
data-include="./examples/runtime.js"
data-include-format="text"
></pre>
</section>
<section>
<h3>Consumer</h3>
<pre
class="json"
data-include="./examples/consumer.json"
data-include-format="text"
></pre>
</section>
</section>
<section>
<h2>Core Algorithms</h2>
<section>
<h3>CRUD</h3>
<section>
<h4>Create</h4>
<ol class="algorithm">
<li>
Attempt to copy the supplied defaults object with
<code>structuredClone</code>. If copying fails, then fail.
</li>
<li>
Let the result be a new empty <a>CR-Struct replica</a> whose
<code>entries</code> member is empty and whose
<code>defaults</code> member is the copied defaults object.
</li>
<li>
Let <var>snapshotIsRecord</var> be true if the supplied snapshot
is a record, otherwise false.
</li>
<li>
For each own <a>field key</a> of the original defaults object:
<ol>
<li>
Let <var>defaultValue</var> be the copied default value for
that key.
</li>
<li>
If <var>snapshotIsRecord</var> is true, the snapshot has an
own property for that key, and the snapshot entry successfully
runs the
<a>transform a snapshot entry to a state entry</a> algorithm
against <var>defaultValue</var>, then store the minted entry
as the live field entry and continue to the next key.
</li>
<li>
Otherwise mint one fresh root UUIDv7 and one fresh winning
UUIDv7.
</li>
<li>
Store a field entry whose <code>uuidv7</code> is the fresh
winning UUIDv7, whose <code>predecessor</code> is the fresh
root UUIDv7, whose <code>value</code> is
<var>defaultValue</var>, and whose <code>tombstones</code> set
initially contains only the fresh root UUIDv7.
</li>
</ol>
</li>
<li>Return the result.</li>
</ol>
</section>
<section>
<h4>Read</h4>
<ol class="algorithm">
<li>
Let <var>entry</var> be the current live <a>field entry</a> for
the supplied <a>field key</a>.
</li>
<li>
Return a detached structured clone of <var>entry</var>'s
<code>value</code>.
</li>
</ol>
</section>
<section>
<h4>Update</h4>
<ol class="algorithm">
<li>
Attempt to copy the supplied value with
<code>structuredClone</code>. If copying fails, then fail.
</li>
<li>
If the runtime prototype of the copied value does not equal the
runtime prototype of the default value for the supplied
<a>field key</a>, then fail.
</li>
<li>
Run the local overwrite procedure for that field using the copied
value.
</li>
<li>
Return a <a>delta</a> containing the new snapshot entry for that
field together with a visible change patch mapping the key to the
copied value.
</li>
</ol>
</section>
<section>
<h4>Delete</h4>
<ol class="algorithm">
<li>
If a specific key was supplied and the replica does not contain
that key, then fail.
</li>
<li>
If a specific key was supplied, then run the local overwrite
procedure for that field using the field's default value.
</li>
<li>
Otherwise, for each <a>field key</a> in the replica, run the local
overwrite procedure for that field using that field's default
value.
</li>
<li>
Return a <a>delta</a> containing the resulting snapshot entries
together with a visible change patch mapping each touched key to
its reset value.
</li>
</ol>
</section>
</section>
<section>
<h3>MAGS</h3>
<section>
<h4>Merge</h4>
<ol class="algorithm">
<li>
If the supplied candidate is not a record, then return no change.
</li>
<li>Let the reply delta be an empty record.</li>
<li>Let the visible change patch be an empty record.</li>
<li>
For each own property of the incoming candidate:
<ol>
<li>
If the property name is not a known <a>field key</a>, then
continue.
</li>
<li>
Run the
<a>transform a snapshot entry to a state entry</a> algorithm
against the local default value for that key. If minting
fails, then continue.
</li>
<li>
Let <var>target</var> be the current local field entry for
that key.
</li>
<li>
Let <var>current</var> be a shallow copy of <var>target</var>.
</li>
<li>
Let <var>frontier</var> be the lexicographically greatest
tombstone currently retained by <var>target</var>.
</li>
<li>
For each incoming tombstone in the candidate field entry:
<ol>
<li>
If the tombstone is less than or equal to
<var>frontier</var>, continue.
</li>
<li>
If <var>target</var> already retains that tombstone,
continue.
</li>
<li>Add the tombstone to <var>target</var>.</li>
</ol>
</li>
<li>
If <var>target</var> already tombstones the candidate
<code>uuidv7</code>, then continue.
</li>
<li>
If <var>current</var>'s <code>uuidv7</code> equals the
candidate <code>uuidv7</code>:
<ol>
<li>
If <var>current</var>'s <code>predecessor</code> is
lexicographically less than the candidate
<code>predecessor</code>, then adopt the candidate
<code>value</code> and <code>predecessor</code>, add the
candidate predecessor to the tombstone set, record the
visible field change, and continue.
</li>
<li>
Otherwise run the local overwrite procedure using
<var>current</var>'s value, append the resulting snapshot
entry to the reply delta, and continue.
</li>
</ol>
</li>
<li>
If <var>current</var>'s <code>uuidv7</code> equals the
candidate <code>predecessor</code>, or the target tombstones
already contain <var>current</var>'s <code>uuidv7</code>, or
the candidate <code>uuidv7</code> is lexicographically greater
than <var>current</var>'s <code>uuidv7</code>, then:
<ol>
<li>
Adopt the candidate <code>uuidv7</code>,
<code>value</code>, and <code>predecessor</code>.
</li>
<li>
Add the candidate predecessor and the previous local
winner's UUIDv7 to the target tombstone set.
</li>
<li>Record the visible field change.</li>
<li>Continue.</li>
</ol>
</li>
<li>
Otherwise add the candidate <code>uuidv7</code> to the target
tombstone set and append the current local field entry to the
reply delta.
</li>
</ol>
</li>
<li>
If neither the reply delta nor the visible change patch contains
any own properties, then return no change.
</li>
<li>
Return the reply delta together with the visible change patch.
</li>
</ol>
</section>
<section>
<h4>Acknowledge</h4>
<ol class="algorithm">
<li>Let the result be an empty acknowledgement record.</li>
<li>
For each local <a>field entry</a>:
<ol>
<li>
Iterate the retained tombstones and keep the lexicographically
greatest tombstone seen so far.
</li>
<li>
Store that greatest tombstone in the result under the field's
key.
</li>
</ol>
</li>
<li>Return the result.</li>
</ol>
</section>
<section>
<h4>Garbage Collect</h4>
<ol class="algorithm">
<li>
If the supplied list of acknowledgement frontier objects is empty,
then return.
</li>
<li>Let <var>smallestPerKey</var> be an empty record.</li>
<li>
For each supplied acknowledgement frontier object:
<ol>
<li>
For each own property of that frontier:
<ol>
<li>
If the property name is not a known <a>field key</a>, then
continue.
</li>
<li>
If the frontier value is not a valid
<a>UUIDv7 identifier</a>, then continue.
</li>
<li>
If <var>smallestPerKey</var> already stores a value for
that key and that stored value is lexicographically less
than or equal to the new frontier, then continue.
</li>
<li>
Otherwise store the new frontier as the smallest known
acknowledgement for that key.
</li>
</ol>
</li>
</ol>
</li>
<li>
For each key stored in <var>smallestPerKey</var>:
<ol>
<li>
Let <var>target</var> be the current local field entry for
that key.
</li>
<li>
Remove every retained tombstone less than or equal to the
smallest acknowledgement for that key, except the field's
current <code>predecessor</code>.
</li>
</ol>
</li>
</ol>
<p class="note">
This algorithm is only safe when the caller supplies a
membership-complete set of acknowledgement frontiers covering every
replica whose future convergence must still be preserved. Supplying
only a partial frontier set is caller misuse and does not guarantee
that an offline replica can later catch up from deltas alone.
</p>
</section>
<section>
<h4>Snapshot</h4>
<ol class="algorithm">
<li>Let the result be an empty record.</li>
<li>
For each current local <a>field entry</a>, run the
<a>transform a state entry to a snapshot entry</a> algorithm.
</li>
<li>
Store each shed field entry in the result under its field key.
</li>
<li>Return the resulting <a>snapshot</a>.</li>
</ol>
</section>
</section>
</section>
<section>
<h2>Reference JavaScript Binding</h2>
<p>
This section defines one conforming JavaScript binding for the model
above.
</p>
<section>
<h3>API Summary</h3>
<pre class="idl">
[Exposed=*]
interface CRStruct {
constructor(object defaults, optional object snapshot = {});
undefined merge(optional object delta = {});
undefined acknowledge();
undefined garbageCollect(sequence<object> frontiers);
undefined snapshot();
sequence<DOMString> keys();
undefined clear();
object clone();
sequence<any> values();
sequence<sequence<any>> entries();
undefined addEventListener(
DOMString type,
EventListener? listener,
optional (AddEventListenerOptions or boolean) options = {}
);
undefined removeEventListener(
DOMString type,
EventListener? listener,
optional (EventListenerOptions or boolean) options = {}
);
object toJSON();
stringifier DOMString ();
iterable<DOMString, any>;
};
dictionary CRStructSnapshotEntry {
required DOMString uuidv7;
required any value;
required DOMString predecessor;
required sequence<DOMString> tombstones;
};
typedef record<DOMString, CRStructSnapshotEntry> CRStructSnapshot;
typedef record<DOMString, CRStructSnapshotEntry> CRStructDelta;
typedef record<DOMString, any> CRStructChange;
typedef record<DOMString, DOMString> CRStructAck;
</pre>
<p>
The binding additionally exposes direct property access through a
JavaScript <code>Proxy</code>. Reading <code>struct.key</code> returns
a detached copy of the current value for that field. Writing
<code>struct.key = value</code> performs a local overwrite, and
<code>delete struct.key</code> resets that field to its default value.
</p>
<p>
Only keys present in the constructor's defaults object participate in
proxy field access. The binding resolves those known field keys before
ordinary reflective property access.
</p>
<p>
The constructor and <a data-link-for="CRStruct">merge()</a> methods
are typed as taking generic objects in the coarse Web IDL summary
above so that the IDL matches the permissive JavaScript ingress
behavior. The expected object shapes are
<code>CRStructSnapshot</code> and <code>CRStructDelta</code> as
defined by the declarations above.
</p>
<p>
When the current <a>snapshot</a> is JSON-compatible,
<code>JSON.stringify(struct)</code> serializes the binding through
that snapshot. <code>for...of</code> iterates detached
<code>[key, value]</code> pairs for the current live fields, and
<code>toString()</code> attempts to return the JSON string form of the
current snapshot.
</p>
</section>
<section>
<h3>Errors</h3>
<p>
The JavaScript binding throws <code>CRStructError</code> with a string
<code>code</code> attribute for local API misuse.
</p>
<dl>
<dt><code>DEFAULTS_NOT_CLONEABLE</code></dt>
<dd>
Thrown when construction receives a defaults object that is not
supported by <code>structuredClone</code>.
</dd>
<dt><code>VALUE_NOT_CLONEABLE</code></dt>
<dd>
Thrown when a local field overwrite receives a value that is not
supported by <code>structuredClone</code>.
</dd>
<dt><code>VALUE_TYPE_MISMATCH</code></dt>
<dd>
Thrown when a local field overwrite receives a value whose runtime
prototype does not match that field's default value runtime
prototype.
</dd>
</dl>
</section>
<section>
<h3>Events</h3>
<p>
The JavaScript binding maintains an internal
<code>EventTarget</code> object as defined by DOM [[DOM]].
</p>
<p>
The binding forwards addEventListener() and removeEventListener() to
that internal event target. Local mutations and merges dispatch
synthetic events through the same target.
</p>
<p>
The JavaScript binding defines four synthetic event types. Each is a
<code>CustomEvent</code> object whose <code>detail</code> attribute is
initialized as described below [[DOM]].
</p>
<dl>
<dt><dfn>delta event</dfn></dt>
<dd>
A <code>CustomEvent</code> whose <code>detail</code> is a
<a>delta</a>. The binding dispatches this event for local field
mutations and for merges that need to emit a reply delta.
</dd>
<dt><dfn>change event</dfn></dt>
<dd>
A <code>CustomEvent</code> whose <code>detail</code> is a partial
object mapping changed field keys to their new visible values.
</dd>
<dt><dfn>ack event</dfn></dt>
<dd>
A <code>CustomEvent</code> whose <code>detail</code> is the current
per-field acknowledgement frontier produced when
<a data-link-for="CRStruct">acknowledge()</a> is invoked.
</dd>
<dt><dfn>snapshot event</dfn></dt>
<dd>
A <code>CustomEvent</code> whose <code>detail</code> is the full
current snapshot produced when
<a data-link-for="CRStruct">snapshot()</a> is invoked.
</dd>
</dl>
<p>
Direct property writes, direct property deletes, and
<a data-link-for="CRStruct">clear()</a> MUST dispatch
<a>delta event</a> before <a>change event</a>.
<a data-link-for="CRStruct">merge()</a> MAY dispatch
<a>delta event</a>, <a>change event</a>, or both, and when both are
dispatched the <a>delta event</a> MUST be dispatched first.
<a data-link-for="CRStruct">acknowledge()</a> MUST dispatch
<a>ack event</a>, and <a data-link-for="CRStruct">snapshot()</a> MUST
dispatch a <a>snapshot event</a>.
</p>
</section>
<section>
<h3>Auxiliary Algorithms</h3>
<section>
<h4><dfn>transform a snapshot entry to a state entry</dfn></h4>
<ol class="algorithm">
<li>
If the candidate value is not a record, then return failure.
</li>
<li>
If the candidate value does not have its own <code>uuidv7</code>,
<code>value</code>, <code>predecessor</code>, and
<code>tombstones</code> members, then return failure.
</li>
<li>
If the candidate value's <code>uuidv7</code> member is not a valid
<a>UUIDv7 identifier</a>, then return failure.
</li>
<li>
If the candidate value's <code>predecessor</code> member is not a
valid <a>UUIDv7 identifier</a>, then return failure.
</li>
<li>
If the candidate value's <code>tombstones</code> member is not a
list, then return failure.
</li>
<li>
Attempt to copy the candidate value's <code>value</code> member
with <code>structuredClone</code>. If copying fails, then return
failure.
</li>
<li>
If the copied value's runtime prototype does not equal the
supplied default value's runtime prototype, then return failure.
</li>
<li>
Initialize a new tombstone set from the listed tombstones,
retaining only valid <a>UUIDv7 identifier</a>(s).
</li>
<li>
If the resulting tombstone set contains the candidate