Three replicas.
One converged buffer.
A concrete walkthrough of concurrent edits in Zed's text engine: independent local changes, different operation arrival orders, causal readiness, and the test coverage that checks convergence.
1. The situation: three people, one file
Imagine three developers editing the same shared file while temporarily disconnected from one another. They all start from the same document state. Each makes a local change before receiving the others' operations.
The upstream test uses a deliberately tiny base string, abcdef, so the ranges and expected results are easy to inspect. It creates three text buffers with distinct replica IDs, all initialized from that same text and buffer ID.
abcdef
All three replicas begin with the same visible text and the same initial causal history.
2. Each replica edits locally
Each developer replaces a different one-character range. The ranges below are byte offsets in this ASCII-only test fixture.
| REPLICA | EDIT | LOCAL RESULT |
|---|---|---|
| A · ID 1 | 1..2 replaces b with 12 | a12cdef |
| B · ID 2 | 3..4 replaces d with 34 | abc34ef |
| C · ID 3 | 5..6 replaces f with 56 | abcde56 |
These are the exact operations and local results asserted by Zed's test_concurrent_edits ↗.
3. The network delivers operations in different orders
After the local edits, each buffer applies the other two operations. The receiving sequences are intentionally not identical:
Replica A: A (local) → B → C Replica B: B (local) → A → C Replica C: C (local) → A → B
The test does not merge complete document strings. It exchanges the edit operations returned by each local Buffer::edit call and feeds those operations to the other buffers with apply_op.
4. The convergence check
All three buffers are expected to finish with the same visible text:
Replica A → a12c34e56 Replica B → a12c34e56 Replica C → a12c34e56
VERIFIED IN SOURCE These three equality assertions are present in the upstream test at tests.rs ↗.
5. What makes remote replay possible?
The local edit is represented as an operation, not just as a new document string. The EditOperation carries a Lamport timestamp, the version the edit was based on, ranges, and replacement text. That version is essential context for interpreting the edit against another replica's fragment history.
When operations arrive, Buffer::apply_ops stores them in history and checks whether the receiver has observed the operation's causal base version. Ready operations are applied; operations whose prerequisites are missing are held in the deferred queue and reconsidered by flush_deferred_ops. See apply_ops / apply_op ↗, deferred queue and causal readiness ↗, and remote edit replay ↗.
ANALYSIS - this is why a remote edit cannot be understood as “replace bytes at the same numeric offset in whatever text happens to be here now.” The receiver has to interpret the sender's ranges in the context of the sender's observed version and the receiver's fragment structure.
6. The larger stress test
The same test file contains test_random_concurrent_edits, which is more demanding than the three-replica example:
- It creates five replicas by default (overridable with
PEERS). - It schedules ten mutations by default (overridable with
OPERATIONS). - It randomly chooses between local edits, undo/redo, and receiving pending network operations.
- It checks buffer invariants during the run and, once the simulated network is idle, asserts that every replica has the same final text.
Source: test_random_concurrent_edits ↗. This is randomized test coverage, not a proof over every possible operation schedule.
7. What the deterministic test does not settle
The three-replica test changes disjoint ranges. It proves convergence for that checked-in scenario, but it does not specify same-cursor insertion, same-range replacement, or overlapping deletion behavior.
OPEN QUESTION - those cases need targeted assertions. The implementation contains an explicit ordering rule for concurrent insertions, which we can trace next, but a source-derived prediction is not a substitute for a regression test.
Nor should one infer a formal CRDT guarantee from these tests alone. The implementation and tests provide evidence for the exercised schedules; a universal guarantee would require a broader specification and proof than this walkthrough supplies.
8. Same-cursor insertion: derive the order
Consider two fresh replicas that both start from ab. Before receiving any remote operation, A inserts X at byte offset 1 and B inserts Y at that exact same position.
Replica A (ID 1): aXb Replica B (ID 2): aYb
With non-empty base text, the constructor seeds the base fragment with a local Lamport timestamp and advances each replica's clock past it. The first local edit therefore gets the same sequence value on both replicas; the replica ID breaks the tie. In this example the timestamps are (value 2, replica 1) and (value 2, replica 2).
The ordering code is unusually explicit:
Lamport::cmpcompares the sequence value first, then the replica ID. So(2, 2)sorts higher than(2, 1).- During remote replay,
apply_remote_editskips over a concurrent insertion at the same versioned offset when that fragment has a higher Lamport timestamp. - The incoming text is then inserted before the remaining fragments in the target range.
Sources: Lamport ordering and ticking ↗, buffer clock initialization ↗, and concurrent insertion handling ↗.
SOURCE-DERIVED PREDICTION - for these two first edits, the code implies that the higher timestamp is placed first. A receiving B's operation, A inserts Y before its existing X; B receiving A's operation preserves its higher-timestamp Y before inserting X. Both should therefore end at aYXb, independent of which remote operation is applied at the other replica.
The following small test would turn that deduction into an executable assertion inside Zed's crates/text/src/tests.rs. It is a proposed test, not a test already present in upstream:
#[test]
fn test_concurrent_insertions_at_same_cursor() {
let mut a = Buffer::new(ReplicaId::new(1), BufferId::new(1).unwrap(), "ab");
let mut b = Buffer::new(ReplicaId::new(2), BufferId::new(1).unwrap(), "ab");
let op_a = a.edit([(1..1, "X")]);
let op_b = b.edit([(1..1, "Y")]);
a.apply_op(op_b);
b.apply_op(op_a);
assert_eq!(a.text(), "aYXb");
assert_eq!(b.text(), "aYXb");
}
NOT YET EXECUTED - the predicted result follows from the inspected branches and timestamp comparison. Running this test against the pinned upstream revision is the next verification step.
9. The next conflict experiments
After adding and running the same-cursor test, the remaining high-value cases are:
- Same-range replacement: A and B replace the same original characters with different strings.
- Delete versus insert: A deletes a range while B inserts inside that range from the same starting version.
- Undo after remote change: A undoes its own edit after receiving B's concurrent operation.
- Causal reordering: a later operation arrives before the earlier operation it depends on, exercising deferral and queue flushing.
Each case should record the initial version, operation timestamps, delivery order, final visible text, and invariant checks. Only then can we describe the conflict policy accurately.
REPRODUCING THE EXISTING TEST - from a local checkout of the inspected upstream revision, run cargo test -p text test_concurrent_edits. This repository documents the checked-in assertions; the command has not been executed as part of this static analysis.