TextCrdt
Alice, Bob, and Carol can type in one field note without losing concurrent edits.
Three hikers edit one field note
Alice and Bob revise the same sentence while Carol reads an older copy of the field note.
Alice inserts “still” while Bob inserts “calm” at the same place. Carol has not received either edit. Each written symbol has its own identity, so all three notebooks can later keep both changes in the same order.
A character offset is only local
SequenceCrdt showed that a position names a different trail stop
in each notebook. Text has the same problem, one character at a
time. A basic text editor can describe one character insertion with a
number: insert("s", 4). The number means “put this
symbol before the symbol that is currently at position 4.” It
works while one copy owns the string.
The number becomes stale when separate copies change. Alice and Bob
both start with The weir is clear.. Alice inserts
still as six character operations at positions 4
through 9. Bob inserts calm as five operations at
positions 4 through 8. If a replica applies those raw offsets in
arrival order, the result depends on which message arrives first.
| Arrival order | Naive result |
|---|---|
| Alice, then Bob | The calm still weir is clear. |
| Bob, then Alice | The still calm weir is clear. |
A stale range is more dangerous. An instruction such as
delete(4, 8) names “weir” only in the version where
the instruction was created. A concurrent insertion can move those
offsets, so another replica can remove different symbols. Sending
raw offsets is not enough to preserve intent or convergence.
Replay the raw offsets
This counterexample intentionally uses neither Watershed nor a sequencer. It applies each character operation's original number to the string that happens to be local when the message arrives.
Naive rule Replay each character at its original numeric position.
Alice sends six operations
s@4 · t@5 · i@6 · l@7 · l@8 · space@9
Bob sends five operations
c@4 · a@5 · l@6 · m@7 · space@8
Alice
The weir is clear.
Bob
The weir is clear.
Carol
The weir is clear.
Ready: all three copies contain The weir is clear.
Enable JavaScript to run the offset counterexample.
Same edits, two mechanisms: IDs or transforms
Every client in both paths starts with The weir is clear.
Alice inserts "still " at position 4; Bob concurrently
inserts "calm " at position 4. Carol waits for delivery.
No formatting is changed in this comparison.
TextCrdt: each character has an ID
Each insert carries the IDs of its characters. Every copy merges by ID; the original position is kept only for diagnostics.
Alice
The weir is clear.
Bob
The weir is clear.
Carol
The weir is clear.
Three copies share the initial text.
RichTextOt: rewrite against the ordered history
Each insert records the last numbered edit its author saw. The sequencer orders the two inserts, and each copy rewrites the later one so it still fits.
Alice
The weir is clear.
Bob
The weir is clear.
Carol
The weir is clear.
Three copies share the initial text.
Ready: the same initial text and authored inserts are used in both paths.
Enable JavaScript to run both kernels. Both paths keep both inserts and converge within their own model.
Convergence is a promise within each model, not between models. TextCrdt's identity ordering and RichTextOt's sequenced transformation can choose different word orders without losing either insert.
Inspect the operations, IDs, and references
The CRDT evidence includes each native operation's diagnostic index and authoritative grapheme delta. The OT evidence includes the actual wire reference and delta, then Carol's transformed operation from the native ordered log. Delivery runs native remote-apply and local-ack paths.
Stage the inserts to inspect the native operations.
Text is a sequence of graphemes
TextCrdt first divides a string into graphemes. A grapheme is one visible written symbol. It can contain more than one Unicode code point, such as a letter plus a combining accent or a family emoji joined from several code points.
This boundary matters because an editor must not put a cursor inside one visible symbol. Watershed counts graphemes, not UTF-16 code units. Insert, delete, and replace ranges therefore use the same units that a reader sees.
TextCrdt uses the SequenceCrdt rule
A TextCrdt
is a SequenceCrdt whose items are graphemes. The order
resolution is not merely similar. It is the same sequence CRDT. Watershed's text value wraps a
Sequence(String). Each grapheme is a sequence item
with a stable identity, and concurrent insertions use the same
deterministic order as SequenceCrdt.
TextCrdt adds a text layer around that sequence. The layer segments strings into graphemes, joins the visible items back into a string, and builds range operations for deletion and replacement. It does not add a second ordering rule.
- Describe the local edit. The editor compares the old and new strings and finds one insertion, deletion, or replacement in local grapheme indexes. This demo sends each inserted grapheme as its own operation, so inserting a word creates several character operations.
- Create a sequence delta. The text layer converts each operation into a delta that names stable sequence items and the new item created by this replica.
- Update the local copy. The author sees the edit at once. The operation keeps the numeric indexes for diagnostics, but the delta is the authoritative payload.
- Number and deliver the operation. Watershed's text kernel sends each edit through a sequencer, the ranger station in the story, which numbers it. The author uses the number to confirm its pending edit, and the other replicas merge the delta. The merge does not depend on that order: the IDs decide where each character goes.
Delete and replace target observed symbols
An insertion creates new sequence items. A deletion records the identities of the graphemes that the author observed in the range. Applying the same deletion twice has no extra effect, and a recipient can identify those graphemes even if other edits changed their visible indexes.
A replacement combines both operations: remove the observed
graphemes, then insert new graphemes at that place. In the demo,
Alice replaces “weir” with “levee” while Bob deletes “ei” from the
original word. Both operations refer to the original grapheme
identities. Every notebook therefore reaches
The levee is clear., regardless of delivery order.
The naive panel above had neither IDs nor a sequencer. A sequencer alone would not save it: it can put raw offsets in one order, but the offsets are still stale against each author's local text. OT fixes that by rewriting positions; the next lesson shows it.
Run the conflict
Type in any notebook, or run a worked conflict. Turn off Auto-deliver to compare local copies before the edits travel.
Write the field note together
Type in any notebook. Turn off Auto-deliver to hold edits locally, then turn it on to watch every copy converge.
Merge rule Give each character its own ID, as SequenceCrdt does for trail stops, so an edit names the characters around it instead of a position that may have moved.
Alice
Latest local edit No local edit yet. Edits name written-symbol positions, not raw code units.
Sequencer
Assigns one shared order to character operations
- No text edits yet.
Bob
Latest local edit No local edit yet. Edits name written-symbol positions, not raw code units.
Carol
Latest local edit No local edit yet. Edits name written-symbol positions, not raw code units.
- The weir is clear.
Run the worked example
Crowd one position with two words, overlap edits to one word, or type your own changes above.
Enable JavaScript to run the race. The expected result remains in the lesson.
Ready: The weir is clear.
Read the result
Alice and Bob target the same place before “weir.” Both words remain after delivery because TextCrdt identifies the nearby symbols instead of relying on one changing character offset.
Every character carries an ID
Each grapheme keeps its own ID and ordering reference, so a long document carries far more metadata than its visible text. TextCrdt also edits plain text only.
The next lesson, RichTextOt, adds formatting and embedded content, and returns to OT.
Sources and implementation
Sources:
Watershed's text_kernel.
Implementation: The demo calls text_kernel.insert, text_kernel.delete_range, text_kernel.replace_range, text_kernel.ack_local, text_kernel.apply_remote, and text_kernel.value
through the Gleam toolkit. The displayed client records and shared
view come from those kernel results.