hjkl_buffer/undo.rs
1//! Undo/redo entry type for per-buffer undo history.
2//!
3//! Lives in `hjkl-buffer` so that [`crate::Buffer`] can own the undo stack
4//! directly, keeping per-buffer state co-located with the rope.
5
6use std::collections::BTreeMap;
7use std::sync::Arc;
8use std::time::{Duration, SystemTime, UNIX_EPOCH};
9
10use serde::{Deserialize, Serialize};
11
12/// A single entry in the undo or redo stack.
13///
14/// The `timestamp` records the wall-clock time at which the snapshot was
15/// taken (i.e. when `push_undo` was called), enabling the `:earlier` /
16/// `:later` time-travel ex commands to walk the stack by duration rather
17/// than by step count.
18///
19/// Stored as a `ropey::Rope` (O(1) Arc-clone) rather than a `String` so
20/// snapshot cost is negligible even on multi-MB buffers.
21#[derive(Debug, Clone)]
22pub struct UndoEntry {
23 pub rope: ropey::Rope,
24 pub cursor: (usize, usize),
25 pub timestamp: SystemTime,
26 /// Local marks / jumplist / changelist / this-buffer's-global-marks
27 /// snapshot, so undo/redo restore mark-ish positions alongside the
28 /// text instead of leaving them shifted by the edit being undone
29 /// (audit-r2 fix 2). `Default::default()` (all empty) for callers
30 /// that don't populate it — restoring an all-empty snapshot is a
31 /// no-op against a freshly-constructed buffer's own empty state, so
32 /// existing fixtures that only care about text/cursor stay valid.
33 pub marks: MarkSnapshot,
34}
35
36/// Buffer-scoped "edit coherence" state snapshotted alongside a
37/// [`UndoEntry`]'s rope so undo/redo can restore marks, not just text.
38///
39/// Positions are plain `(row, col)` (or `(row, col)` values keyed by
40/// mark char) — no buffer-id tagging needed here even for
41/// `global_marks`, because a `MarkSnapshot` always belongs to exactly
42/// one buffer's undo stack; the engine is responsible for reattaching
43/// its own `buffer_id` when writing entries back into the session-global
44/// marks map (see `Editor::restore_marks`).
45#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
46pub struct MarkSnapshot {
47 /// `ma`-`mz` local marks (`View::marks_cloned`).
48 pub local_marks: BTreeMap<char, (usize, usize)>,
49 /// Back-jumplist (`Ctrl-o` stack), newest at the back.
50 pub jump_back: Vec<(usize, usize)>,
51 /// Forward-jumplist (`Ctrl-i` stack), newest at the back.
52 pub jump_fwd: Vec<(usize, usize)>,
53 /// `` `. `` / `'.` — position of the most recent change.
54 pub change_last_edit: Option<(usize, usize)>,
55 /// Changelist ring (`g;` / `g,`).
56 pub change_list: Vec<(usize, usize)>,
57 /// Walk cursor into `change_list`; `None` outside a walk.
58 pub change_cursor: Option<usize>,
59 /// `mA`-`mZ` global marks that belong to THIS buffer (bare
60 /// `(row, col)` — the buffer-id is implicit, this buffer).
61 pub global_marks: BTreeMap<char, (usize, usize)>,
62}
63
64// ─── Reversible edge delta (Phase 3a) ──────────────────────────────────────────
65//
66// Phase 2b stored a FULL rope snapshot on every node. Phase 3a stores only a
67// reversible **delta** on each parent→child edge (the root keeps a full base
68// rope) plus a materialization cache, so the in-RAM hot path stays snapshot-fast
69// while a future undofile shrinks from hundreds of MB to KB. This slice changes
70// ONLY internal storage — every public signature, and every observable
71// behaviour, is byte-identical to Phase 2b.
72
73/// A reversible edit between two adjacent buffer states, expressed as a single
74/// spanning replacement in **char-offset space** on the rope.
75///
76/// The index space is ropey `char` offsets throughout — never bytes — so
77/// multi-byte UTF-8 round-trips (a byte offset could split a codepoint). In the
78/// PARENT state `chars[start .. start + old.chars().count()] == old`; replacing
79/// that region with `new` yields the CHILD state, and swapping the two inverts
80/// it. A whole undo group collapses to the one region spanning its edits
81/// (common-prefix / common-suffix diff); a `Vec<Delta>` for disjoint regions is
82/// an acceptable future generalization, but one spanning region is all Phase 3a
83/// needs.
84#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
85pub struct Delta {
86 /// Char offset of the first differing char (the common-prefix length).
87 pub start: usize,
88 /// Chars present in the PARENT but not the CHILD (removed going forward).
89 pub old: String,
90 /// Chars present in the CHILD but not the PARENT (inserted going forward).
91 pub new: String,
92}
93
94/// Is `byte_idx` a char boundary of `r`? (`str::is_char_boundary` for ropes.)
95///
96/// Ropey always splits chunks ON char boundaries, so the question is answerable
97/// inside the one chunk containing the byte — O(log N), no materialization.
98fn is_char_boundary(r: &ropey::Rope, byte_idx: usize) -> bool {
99 if byte_idx == 0 || byte_idx == r.len_bytes() {
100 return true;
101 }
102 let (chunk, chunk_start, _, _) = r.chunk_at_byte(byte_idx);
103 chunk.is_char_boundary(byte_idx - chunk_start)
104}
105
106/// Length of the longest common byte PREFIX of `a` and `b`, capped at `max`.
107///
108/// Walks both ropes' chunks in lockstep — never materializes either rope. Two
109/// fast paths keep the common editor case near-free: identical chunk pointers
110/// (ropey leaves are `Arc`-shared, so a clone-then-edit child shares every leaf
111/// outside the edit) are accepted without reading bytes, and otherwise whole
112/// overlapping runs are compared with one slice `==` (memcmp).
113fn common_prefix_bytes(a: &ropey::Rope, b: &ropey::Rope, max: usize) -> usize {
114 let mut a_chunks = a.chunks();
115 let mut b_chunks = b.chunks();
116 let mut at: &[u8] = &[];
117 let mut bt: &[u8] = &[];
118 let mut n = 0;
119 while n < max {
120 if at.is_empty() {
121 match a_chunks.next() {
122 Some(c) => at = c.as_bytes(),
123 None => break,
124 }
125 continue;
126 }
127 if bt.is_empty() {
128 match b_chunks.next() {
129 Some(c) => bt = c.as_bytes(),
130 None => break,
131 }
132 continue;
133 }
134 // Same shared leaf, wholly within the cap: equal without looking.
135 if at.as_ptr() == bt.as_ptr() && at.len() == bt.len() && at.len() <= max - n {
136 n += at.len();
137 at = &[];
138 bt = &[];
139 continue;
140 }
141 let m = at.len().min(bt.len()).min(max - n);
142 if at[..m] == bt[..m] {
143 n += m;
144 at = &at[m..];
145 bt = &bt[m..];
146 } else {
147 let mut i = 0;
148 while at[i] == bt[i] {
149 i += 1;
150 }
151 n += i;
152 break;
153 }
154 }
155 n
156}
157
158/// Length of the longest common byte SUFFIX of `a` and `b`, capped at `max`.
159///
160/// The mirror of [`common_prefix_bytes`], walking both ropes' chunk cursors
161/// backwards from the end via [`ropey::iter::Chunks::prev`].
162fn common_suffix_bytes(a: &ropey::Rope, b: &ropey::Rope, max: usize) -> usize {
163 let mut a_chunks = a.chunks_at_byte(a.len_bytes()).0;
164 let mut b_chunks = b.chunks_at_byte(b.len_bytes()).0;
165 let mut at: &[u8] = &[];
166 let mut bt: &[u8] = &[];
167 let mut n = 0;
168 while n < max {
169 if at.is_empty() {
170 match a_chunks.prev() {
171 Some(c) => at = c.as_bytes(),
172 None => break,
173 }
174 continue;
175 }
176 if bt.is_empty() {
177 match b_chunks.prev() {
178 Some(c) => bt = c.as_bytes(),
179 None => break,
180 }
181 continue;
182 }
183 if at.as_ptr() == bt.as_ptr() && at.len() == bt.len() && at.len() <= max - n {
184 n += at.len();
185 at = &[];
186 bt = &[];
187 continue;
188 }
189 let m = at.len().min(bt.len()).min(max - n);
190 let (a_tail, b_tail) = (&at[at.len() - m..], &bt[bt.len() - m..]);
191 if a_tail == b_tail {
192 n += m;
193 at = &at[..at.len() - m];
194 bt = &bt[..bt.len() - m];
195 } else {
196 let mut i = 0;
197 while a_tail[m - 1 - i] == b_tail[m - 1 - i] {
198 i += 1;
199 }
200 n += i;
201 break;
202 }
203 }
204 n
205}
206
207/// Common-prefix / common-suffix diff of two ropes → the minimal single spanning
208/// [`Delta`]. Guarantees `apply_forward(a, diff(a, b)) == b` and
209/// `apply_inverse(b, diff(a, b)) == a` for ALL `a`, `b` (see the property
210/// tests). Boundaries are found on bytes (fast) then snapped to char boundaries
211/// so `old`/`new` are always valid UTF-8 and `start` is a true char offset.
212///
213/// The scans walk the ropes' chunks directly and only the differing MIDDLE is
214/// materialized — a full `to_string()` of both sides used to dominate every edit
215/// on a multi-MB buffer (measured 1.55 ms + ~6.4 MB of allocation per push at
216/// 3.2 MB). `diff_reference` in the tests below is the old materializing
217/// implementation, kept as the differential-test oracle.
218fn diff(parent: &ropey::Rope, child: &ropey::Rope) -> Delta {
219 let a_len = parent.len_bytes();
220 let b_len = child.len_bytes();
221
222 // Longest common byte prefix, snapped DOWN to a char boundary.
223 let max_pre = a_len.min(b_len);
224 let mut pre = common_prefix_bytes(parent, child, max_pre);
225 while pre > 0 && !is_char_boundary(parent, pre) {
226 pre -= 1;
227 }
228
229 // Longest common byte suffix not overlapping the prefix. The cut points
230 // `a_end`/`b_end` sit at identical trailing bytes, so snapping `a_end` UP to
231 // a char boundary snaps `b_end` by the same byte delta simultaneously.
232 let suf = common_suffix_bytes(parent, child, max_pre - pre);
233 let mut a_end = a_len - suf;
234 while a_end < a_len && !is_char_boundary(parent, a_end) {
235 a_end += 1;
236 }
237 let b_end = b_len - (a_len - a_end);
238
239 Delta {
240 start: parent.byte_to_char(pre),
241 old: parent.byte_slice(pre..a_end).to_string(),
242 new: child.byte_slice(pre..b_end).to_string(),
243 }
244}
245
246/// Apply a forward delta (PARENT → CHILD) to `parent`, returning the child rope.
247fn apply_forward(parent: &ropey::Rope, d: &Delta) -> ropey::Rope {
248 let mut r = parent.clone();
249 let old_chars = d.old.chars().count();
250 r.remove(d.start..d.start + old_chars);
251 r.insert(d.start, &d.new);
252 r
253}
254
255/// Apply an inverse delta (CHILD → PARENT) to `child`, returning the parent rope.
256fn apply_inverse(child: &ropey::Rope, d: &Delta) -> ropey::Rope {
257 let mut r = child.clone();
258 let new_chars = d.new.chars().count();
259 r.remove(d.start..d.start + new_chars);
260 r.insert(d.start, &d.old);
261 r
262}
263
264// ─── Undo arena tree (Phase 2b + Phase 3a delta storage) ──────────────────────
265//
266// The undo history is a real arena TREE of buffer states (Phase 2a introduced
267// the arena; Phase 2b makes it branch; Phase 3a stores edges as deltas). An edit
268// after an undo FORKS a new child instead of truncating the forward branch, so
269// old branches stay reachable — matching nvim's undo tree. `seq` is
270// load-bearing: `g-`/`g+` and the `:earlier`/`:later` count forms walk ALL
271// states by global `seq` (see `seq_earlier_step`/`seq_later_step`), while
272// `u`/`<C-r>` stay branch-local (parent / `last_child`).
273//
274// The linear-history subset is unchanged: with no forks the tree is a single
275// root→current→leaf path and every operation degrades to the old two-stack
276// behaviour.
277//
278// - `current` points at the node representing the LIVE buffer state.
279// - The ancestors of `current` (parent, … up to `root`) are the reachable undo
280// line; `current.parent` is the `u` target.
281// - `current.last_child` is the `<C-r>` target. Landing on any node (undo,
282// redo, or a `g-`/`g+` jump) rewrites `last_child` down the root→node path so
283// a later `<C-r>` retraces the branch just taken.
284//
285// Storage (Phase 3a): each non-root node holds the reversible `delta` on its
286// edge from `parent`; the root holds a full `base` rope. A node's content is
287// reconstructed on demand (`materialize`) from the nearest cached ancestor (or
288// the root base) by replaying forward deltas, or — for the `u`/`<C-r>` hot path
289// — from the adjacent warm node by one delta apply. Recently materialized ropes
290// are kept in a bounded LRU (`warm`); `current` is always kept warm. A node's
291// `delta`/content is FINALIZED lazily on the way past it (whenever the live rope
292// is written into it), never read as a restore target until then — so the fresh
293// leaf `current` holds a placeholder edge that is corrected before it matters.
294//
295// Keyframes (issue #302): the warm LRU alone bounds nothing — a `g-` onto a node
296// far outside it replayed the WHOLE chain from the root, so one jump was O(depth)
297// and `:earlier 9999` was O(depth²) (measured 212 ms for a 1024-deep history).
298// Every node at a depth that is a multiple of `KEYFRAME_INTERVAL` therefore PINS
299// its materialized rope, capping any single replay at `KEYFRAME_INTERVAL - 1`
300// applies; and `materialize` caches every intermediate it replays, so the
301// step-by-step walk pays that replay once per interval rather than once per step.
302// Keyframes are a pure in-memory cache: they are recomputable from the root base
303// plus the deltas, so they are NOT part of the `SerTree` on-disk projection, and
304// dropping every one of them changes only speed, never content.
305
306/// Keyframe spacing, in nodes of depth. Every node whose depth from the root is
307/// a multiple of this pins its materialized rope, so `materialize` never replays
308/// more than `KEYFRAME_INTERVAL - 1` deltas from the nearest anchor.
309///
310/// **Why 16.** The cost of a keyframe is *not* a document copy. `ropey::Rope` is
311/// a persistent tree with `Arc`-shared leaves, so a snapshot taken between small
312/// edits shares every chunk outside the edited path with its neighbours and only
313/// retains the O(log N) interior nodes the edit rewrote. Measured marginal RSS of
314/// retaining one such snapshot (1024 small edits, keeping every 16th):
315///
316/// | document | bytes retained per keyframe |
317/// | --- | --- |
318/// | 119 KiB | ~3.0 KiB |
319/// | 11.9 MiB | ~7.2 KiB |
320/// | 11.9 MiB, 4 KiB edits | ~11.5 KiB |
321///
322/// i.e. essentially independent of document size — a 200 MB buffer does not pay
323/// 200 MB per keyframe. At one keyframe per 16 nodes that is well under a KiB of
324/// amortized overhead per undo state, next to the `Delta` (two `String`s of the
325/// changed span) every node already stores unconditionally. 16 also sits under
326/// [`WARM_CAP`], which is what lets a full walk stay linear (see there).
327///
328/// The one shape that would break the "cheap" argument — an edit that rewrites
329/// the entire document, so consecutive states share nothing — already costs two
330/// full-document `String`s in that node's own `Delta`, so the keyframe adds at
331/// most another 1/16 of a cost the tree was paying anyway.
332const KEYFRAME_INTERVAL: usize = 16;
333
334/// Hard ceiling on how many keyframes are pinned at once; beyond it the
335/// least-recently-touched keyframe is unpinned (it becomes an ordinary cold
336/// node, replayable as before — correctness is unaffected).
337///
338/// Deliberately a COUNT, not a byte budget: by the measurement on
339/// [`KEYFRAME_INTERVAL`] a keyframe's real retention is roughly document-size
340/// *independent*, so a byte budget computed from `len_bytes()` would be a wild
341/// over-estimate and would switch keyframes off precisely on the large documents
342/// that need them most. 512 keyframes covers 8192 undo states — past any sane
343/// `undolevels` — for a measured ceiling of a few MiB.
344const KEYFRAME_CAP: usize = 512;
345
346/// Index into [`UndoTree::nodes`]. Slots are reused via a free list, so an id is
347/// only valid while the node it names is live — the tree never hands ids out.
348pub type NodeId = usize;
349
350/// How many recently-materialized ORDINARY node ropes to keep warm (besides the
351/// root base, `current`, and the pinned keyframes, which are always available).
352///
353/// Kept above [`KEYFRAME_INTERVAL`] on purpose: `materialize` caches every
354/// intermediate it replays, so one keyframe interval's worth of intermediates has
355/// to survive here for a step-by-step history walk (`:earlier 9999`) to cost one
356/// replay per INTERVAL rather than one per step — the difference between an O(N)
357/// and an O(N·K) walk.
358const WARM_CAP: usize = 32;
359
360/// One node of the undo arena tree: a buffer state the user could land on, plus
361/// its links and the reversible edge to its parent. A node with `> 1` child is a
362/// branch point (Phase 2b); `last_child` records which child `<C-r>` follows.
363#[derive(Debug, Clone)]
364pub struct UndoNode {
365 pub parent: Option<NodeId>,
366 pub children: Vec<NodeId>,
367 pub last_child: Option<NodeId>,
368 /// Reversible edit from the parent's content to this node's content. `None`
369 /// only for the root (and any node promoted to root by pruning), which holds
370 /// `base` instead.
371 pub delta: Option<Delta>,
372 /// Full base rope. `Some` ONLY for the root — the anchor the delta chain
373 /// replays from. Non-root nodes leave this `None` and carry a `delta`.
374 pub base: Option<ropey::Rope>,
375 /// Materialized content, LRU-managed. Warm for `current`, recently visited
376 /// nodes, and keyframe-depth nodes (which are pinned rather than aged out);
377 /// `None` (cold) otherwise, reconstructable from deltas.
378 pub rope_cache: Option<ropey::Rope>,
379 /// Distance from the root, root == 0. Assigned once at creation and never
380 /// renumbered — root-side pruning shifts the whole numbering down uniformly,
381 /// which leaves keyframes exactly [`KEYFRAME_INTERVAL`] apart either way.
382 /// Purely a cache-placement input: a wrong depth costs speed, never content.
383 pub depth: usize,
384 /// Post-state cursor for this node (restored alongside the text).
385 pub cursor: (usize, usize),
386 /// Wall-clock time this state was created — drives `:earlier`/`:later`.
387 pub timestamp: SystemTime,
388 /// Marks / jumplist / changelist snapshot restored with the text.
389 ///
390 /// Shared (`Arc`) rather than owned: a `push` writes the SAME snapshot
391 /// into the node being left and into the fresh child, and a
392 /// `MarkSnapshot` is up to five collections. Nodes never mutate it in
393 /// place — it is only ever replaced wholesale — so sharing is invisible.
394 pub marks: Arc<MarkSnapshot>,
395 /// Global monotonic order across the whole tree — the change number that
396 /// `g-`/`g+`, `:earlier`/`:later`, and `:undolist` traverse and display.
397 pub seq: u64,
398 /// Is this node on the root→`current` path (inclusive of both ends)?
399 ///
400 /// A maintained INVARIANT, not a hint, and the whole reason
401 /// [`UndoTree::retarget_current`] no longer walks the chain: the set of
402 /// flagged nodes is exactly the chain, so the first flagged node found
403 /// walking up from a landing target is the fork point, and every ancestor
404 /// above it already names its on-path child. Every site that moves
405 /// `current` — `push`, `undo_step`, `redo_step`, `retarget_current`,
406 /// `pop_committed` — maintains it, and the bulk paths (`new`,
407 /// `clear_all`, `from_serializable`) establish it outright.
408 /// `path_flags_track_the_root_to_current_walk` pins it against a
409 /// brute-force parent walk after each of those.
410 ///
411 /// Runtime-only and derivable, so — like `depth` and `rope_cache` — it is
412 /// not part of the [`SerTree`] projection.
413 pub on_path: bool,
414}
415
416/// Arena tree of [`UndoNode`]s. Replaces the old `undo_stack`/`redo_stack`
417/// `Vec<UndoEntry>` pair on [`crate::Buffer`]; see the module comment for how
418/// `u`/`<C-r>` (branch-local) and `g-`/`g+` (seq-ordered) map onto it, and how
419/// Phase 3a stores edges as deltas behind a materialization cache.
420#[derive(Debug)]
421pub struct UndoTree {
422 /// Slab; `None` slots are free and recorded in `free`.
423 nodes: Vec<Option<UndoNode>>,
424 /// Reusable slot indices (frees push here, allocs pop here first).
425 free: Vec<NodeId>,
426 /// LRU of ORDINARY node ids with a warm `rope_cache` (root and keyframe-depth
427 /// nodes excluded — they live in `base` / `keyframes`), most-recently-touched
428 /// last. Bounded by [`WARM_CAP`]; `current` is never evicted.
429 warm: Vec<NodeId>,
430 /// Node ids at a keyframe depth whose `rope_cache` is PINNED — the replay
431 /// anchors that bound `materialize` at [`KEYFRAME_INTERVAL`] applies.
432 /// Most-recently-touched last, bounded by [`KEYFRAME_CAP`].
433 keyframes: Vec<NodeId>,
434 /// `seq` → node id for every LIVE node, ordered.
435 ///
436 /// `g-` / `g+` / `:earlier` / `:later` need the neighbouring node in seq
437 /// order tree-wide, which was a full arena scan per step — an O(N) floor
438 /// under every history step, so holding `g-` over a deep history was
439 /// O(N * depth) in the lookup alone, dwarfing the bounded delta replay
440 /// keyframes had already achieved. A `BTreeMap` answers both directions in
441 /// O(log N) via `range`.
442 ///
443 /// Every live node appears exactly once. `seq` is assigned at creation and
444 /// never mutated, so the only maintenance points are `alloc` and `free`
445 /// (plus the two bulk paths, `clear_all` and `from_serializable`, which
446 /// rebuild it). `seq_index_matches_the_arena` pins that invariant against a
447 /// brute-force scan.
448 by_seq: std::collections::BTreeMap<u64, NodeId>,
449 root: NodeId,
450 current: NodeId,
451 next_seq: u64,
452}
453
454/// Trim `list` (an LRU, oldest first) down to `cap`, dropping the evicted
455/// nodes' materialized ropes. `current` is never evicted — the live state must
456/// stay available without a replay.
457///
458/// A free function over the pieces rather than a method so it can hold `&mut`
459/// on one arena field and one list at the same time.
460fn evict_to(list: &mut Vec<NodeId>, nodes: &mut [Option<UndoNode>], current: NodeId, cap: usize) {
461 while list.len() > cap {
462 let Some(pos) = list.iter().position(|&n| n != current) else {
463 break;
464 };
465 let victim = list.remove(pos);
466 if let Some(node) = nodes[victim].as_mut() {
467 node.rope_cache = None;
468 }
469 }
470}
471
472impl UndoTree {
473 /// New tree with a single root == current node holding `rope` as its base
474 /// state (the buffer as opened / last saved). The root is always
475 /// materializable from this base.
476 pub(crate) fn new(rope: ropey::Rope) -> Self {
477 let root = UndoNode {
478 parent: None,
479 children: Vec::new(),
480 last_child: None,
481 delta: None,
482 base: Some(rope),
483 rope_cache: None,
484 depth: 0,
485 cursor: (0, 0),
486 timestamp: SystemTime::now(),
487 marks: Arc::default(),
488 seq: 0,
489 // The root is `current`, so it is the whole path.
490 on_path: true,
491 };
492 Self {
493 nodes: vec![Some(root)],
494 free: Vec::new(),
495 warm: Vec::new(),
496 keyframes: Vec::new(),
497 by_seq: std::iter::once((0, 0)).collect(),
498 root: 0,
499 current: 0,
500 next_seq: 1,
501 }
502 }
503
504 // ── slab helpers ─────────────────────────────────────────────────────────
505
506 fn get(&self, id: NodeId) -> &UndoNode {
507 self.nodes[id].as_ref().expect("live NodeId")
508 }
509
510 fn get_mut(&mut self, id: NodeId) -> &mut UndoNode {
511 self.nodes[id].as_mut().expect("live NodeId")
512 }
513
514 fn alloc(&mut self, node: UndoNode) -> NodeId {
515 let seq = node.seq;
516 let id = if let Some(id) = self.free.pop() {
517 self.nodes[id] = Some(node);
518 id
519 } else {
520 self.nodes.push(Some(node));
521 self.nodes.len() - 1
522 };
523 self.by_seq.insert(seq, id);
524 id
525 }
526
527 /// Free a single slot (does NOT recurse into children — callers detach
528 /// links first). Drops the node's delta + materialized cache and purges it
529 /// from both cache LRUs.
530 fn free(&mut self, id: NodeId) {
531 if let Some(n) = self.nodes[id].as_ref() {
532 self.by_seq.remove(&n.seq);
533 }
534 self.nodes[id] = None;
535 self.free.push(id);
536 self.warm.retain(|&n| n != id);
537 self.keyframes.retain(|&n| n != id);
538 }
539
540 // ── materialization (Phase 3a + keyframes) ───────────────────────────────
541
542 /// Is `id` at a keyframe depth, i.e. should its materialized rope be PINNED
543 /// as a replay anchor rather than aged out of the ordinary warm LRU?
544 ///
545 /// The root qualifies arithmetically (depth 0) but is excluded: it carries a
546 /// full `base` and is already an anchor.
547 fn is_keyframe(&self, id: NodeId) -> bool {
548 id != self.root && self.get(id).depth.is_multiple_of(KEYFRAME_INTERVAL)
549 }
550
551 /// Record `id` as freshly materialized. Keyframe-depth nodes go into the
552 /// pinned `keyframes` LRU (bounded by [`KEYFRAME_CAP`]), everything else into
553 /// the ordinary `warm` LRU (bounded by [`WARM_CAP`]). Neither ever evicts
554 /// `current`; the root is skipped entirely (it has no cache, it has `base`).
555 fn touch_warm(&mut self, id: NodeId) {
556 if id == self.root {
557 return;
558 }
559 let (list, cap) = if self.is_keyframe(id) {
560 (&mut self.keyframes, KEYFRAME_CAP)
561 } else {
562 (&mut self.warm, WARM_CAP)
563 };
564 list.retain(|&n| n != id);
565 list.push(id);
566 evict_to(list, &mut self.nodes, self.current, cap);
567 }
568
569 /// Materialize node `id`'s content, warming its cache. Uses the node's own
570 /// cache if present, else the root `base`, else replays forward deltas from
571 /// the nearest materialized ancestor — a warm node, a pinned keyframe, or the
572 /// root. Always terminates: the root carries a base.
573 ///
574 /// Every intermediate along the replay is cached too, not just the target:
575 /// they were computed anyway and a `ropey::Rope` clone is an `Arc` bump, so
576 /// caching them is free — and it is what makes a step-by-step history walk
577 /// (`g-` held down, `:earlier 9999`) pay ONE replay per keyframe interval
578 /// instead of one per step.
579 fn materialize(&mut self, id: NodeId) -> ropey::Rope {
580 if let Some(r) = &self.get(id).rope_cache {
581 return r.clone();
582 }
583 if let Some(base) = &self.get(id).base {
584 return base.clone();
585 }
586 // Walk up to the nearest ancestor that holds content (warm cache, pinned
587 // keyframe, or the root base), recording the path to replay forward.
588 // Bounded by the keyframe spacing whenever the ancestor chain has been
589 // materialized before.
590 let mut path = Vec::new();
591 let base_rope;
592 let mut anchor = id;
593 loop {
594 path.push(anchor);
595 let par = self
596 .get(anchor)
597 .parent
598 .expect("a non-root, non-based node always has a parent");
599 if let Some(r) = &self.get(par).rope_cache {
600 base_rope = r.clone();
601 break;
602 }
603 if let Some(b) = &self.get(par).base {
604 base_rope = b.clone();
605 break;
606 }
607 anchor = par;
608 }
609 let mut rope = base_rope;
610 // `path` is target-first, so replaying in reverse ends on `id` — which
611 // therefore lands last in its LRU and cannot be the eviction picked by
612 // its own `touch_warm`.
613 for &node in path.iter().rev() {
614 let d = self
615 .get(node)
616 .delta
617 .as_ref()
618 .expect("a non-root node always carries its edge delta");
619 rope = apply_forward(&rope, d);
620 self.get_mut(node).rope_cache = Some(rope.clone());
621 self.touch_warm(node);
622 }
623 rope
624 }
625
626 /// Reconstruct node `id`'s restorable [`UndoEntry`] — the byte-for-byte
627 /// equivalent of Phase 2b's `node.snapshot.clone()`.
628 fn entry_of(&mut self, id: NodeId) -> UndoEntry {
629 let rope = self.materialize(id);
630 let n = self.get(id);
631 UndoEntry {
632 rope,
633 cursor: n.cursor,
634 timestamp: n.timestamp,
635 marks: (*n.marks).clone(),
636 }
637 }
638
639 /// Finalize node `id` to hold `rope` as its content, recomputing its edge
640 /// delta (or the root base) and updating cursor/timestamp/marks. A no-op
641 /// diff is skipped when the content is unchanged (the common case on a
642 /// history walk, where only the fields move) — which also avoids
643 /// materializing the parent, keeping the walk cheap.
644 fn set_node_state(
645 &mut self,
646 id: NodeId,
647 rope: ropey::Rope,
648 cursor: (usize, usize),
649 timestamp: SystemTime,
650 marks: Arc<MarkSnapshot>,
651 ) {
652 let is_root = self.get(id).parent.is_none();
653 // `Rope`'s `PartialEq` walks both ropes, so this comparison alone was
654 // O(document) on every history step. `is_instance` is an `Arc::ptr_eq`
655 // on the shared root and answers the case that actually occurs here —
656 // the caller stashes back the very rope it was handed by the previous
657 // step, still a clone of what the node cached. It is only a sufficient
658 // test for equality, never a necessary one, so an unrelated-but-equal
659 // rope still falls through to the full compare and is found unchanged.
660 let same = |a: Option<&ropey::Rope>| a.is_some_and(|a| a.is_instance(&rope) || *a == rope);
661 let unchanged =
662 same(self.get(id).rope_cache.as_ref()) || (is_root && same(self.get(id).base.as_ref()));
663 {
664 let node = self.get_mut(id);
665 node.cursor = cursor;
666 node.timestamp = timestamp;
667 node.marks = marks;
668 }
669 if unchanged {
670 return;
671 }
672 if is_root {
673 self.get_mut(id).base = Some(rope);
674 // The root is materialized from `base`; keep no stale cache.
675 self.get_mut(id).rope_cache = None;
676 self.warm.retain(|&n| n != id);
677 self.keyframes.retain(|&n| n != id);
678 } else {
679 let par = self.get(id).parent.expect("non-root has a parent");
680 let par_rope = self.materialize(par);
681 let d = diff(&par_rope, &rope);
682 let node = self.get_mut(id);
683 node.delta = Some(d);
684 node.rope_cache = Some(rope);
685 self.touch_warm(id);
686 }
687 }
688
689 /// Free `id` and its whole subtree (iteratively, so a long redo chain can't
690 /// overflow the stack).
691 fn free_subtree(&mut self, id: NodeId) {
692 let mut stack = vec![id];
693 while let Some(n) = stack.pop() {
694 let kids = std::mem::take(&mut self.get_mut(n).children);
695 stack.extend(kids);
696 self.free(n);
697 }
698 }
699
700 // ── read-only queries (mirror the old stack accessors) ───────────────────
701
702 /// `undo_stack.is_empty()` ⇔ `current` has no parent (is the root).
703 pub(crate) fn is_at_root(&self) -> bool {
704 self.get(self.current).parent.is_none()
705 }
706
707 /// `!redo_stack.is_empty()` ⇔ `current` has a forward child.
708 pub(crate) fn has_redo(&self) -> bool {
709 self.get(self.current).last_child.is_some()
710 }
711
712 /// Number of ancestors of `current`, i.e. its distance from the root.
713 ///
714 /// Walked rather than read off `Node::depth` so it stays right after a
715 /// prune or a load has renumbered the tree. Backs `View::undo_stack_len`,
716 /// which is the `undo_stack.len()` of the pre-tree implementation.
717 pub(crate) fn depth_from_root(&self) -> usize {
718 let mut d = 0;
719 let mut n = self.get(self.current).parent;
720 while let Some(p) = n {
721 d += 1;
722 n = self.get(p).parent;
723 }
724 d
725 }
726
727 /// `undo_stack.last().timestamp` == `current.parent`'s timestamp.
728 pub(crate) fn parent_timestamp(&self) -> Option<SystemTime> {
729 self.get(self.current).parent.map(|p| self.get(p).timestamp)
730 }
731
732 /// `redo_stack.last().timestamp` == `current.last_child`'s timestamp.
733 pub(crate) fn child_timestamp(&self) -> Option<SystemTime> {
734 self.get(self.current)
735 .last_child
736 .map(|c| self.get(c).timestamp)
737 }
738
739 // ── mutations ────────────────────────────────────────────────────────────
740
741 /// Commit a new boundary from `current`, growing the tree (Phase 2b).
742 ///
743 /// `entry` is the pre-edit LIVE state. It is written into `current`'s
744 /// snapshot (making `current` a real, restorable state), then a fresh child
745 /// is APPENDED and becomes the new `current` for the edit about to happen.
746 ///
747 /// Unlike Phase 2a this does NOT drop `current`'s existing children: an edit
748 /// after an undo now forks a new branch and the old forward branch(es) stay
749 /// reachable via `g-`/`g+` and `:undolist`, matching nvim's undo tree. The
750 /// new child is made `last_child` so a subsequent `<C-r>` follows the branch
751 /// just created.
752 pub(crate) fn push(&mut self, entry: UndoEntry) {
753 let cur = self.current;
754 // ONE snapshot serves both the node being left and the fresh child —
755 // this runs per edit, and a deep `MarkSnapshot` copy is up to five
756 // collection allocations.
757 let marks = Arc::new(entry.marks);
758 // Finalize the node being left with the pre-edit live state, recomputing
759 // its edge delta from its parent (or the root base).
760 self.set_node_state(
761 cur,
762 entry.rope.clone(),
763 entry.cursor,
764 entry.timestamp,
765 Arc::clone(&marks),
766 );
767 let seq = self.next_seq;
768 self.next_seq += 1;
769 // Fresh child: identical to `cur` for now (empty edge delta + warm cache
770 // holding the pre-edit rope). Its true post-edit content is finalized on
771 // the way past it (next move) or by the next `push`, at which point the
772 // edge delta is recomputed against `cur`.
773 let child_depth = self.get(cur).depth + 1;
774 let child = self.alloc(UndoNode {
775 parent: Some(cur),
776 children: Vec::new(),
777 last_child: None,
778 delta: Some(Delta::default()),
779 base: None,
780 rope_cache: Some(entry.rope),
781 depth: child_depth,
782 cursor: entry.cursor,
783 timestamp: entry.timestamp,
784 marks,
785 seq,
786 // The fresh child becomes `current`, extending the path by one.
787 on_path: true,
788 });
789 let cur_node = self.get_mut(cur);
790 // Append (retain old branches); the freshest child is the redo target.
791 cur_node.children.push(child);
792 cur_node.last_child = Some(child);
793 self.current = child;
794 self.touch_warm(child);
795 }
796
797 /// One undo step. `live` is the current buffer state (the node being left);
798 /// it is written into that node but INHERITS the destination (parent)
799 /// timestamp — byte-parity with the old dance, where the pushed redo entry
800 /// took the popped undo entry's timestamp. Returns the parent snapshot to
801 /// restore, or `None` at the root.
802 pub(crate) fn undo_step(
803 &mut self,
804 rope: ropey::Rope,
805 cursor: (usize, usize),
806 marks: MarkSnapshot,
807 ) -> Option<UndoEntry> {
808 let cur = self.current;
809 let par = self.get(cur).parent?;
810 let dest_ts = self.get(par).timestamp;
811 self.set_node_state(cur, rope, cursor, dest_ts, Arc::new(marks));
812 // Redo from the parent must return to the node we just left.
813 self.get_mut(par).last_child = Some(cur);
814 // The path shortens by one: `cur` drops off its tip, `par` is the new
815 // tip and was already on it.
816 self.get_mut(cur).on_path = false;
817 self.current = par;
818 // Hot-path materialization: derive the (possibly cold) parent from the
819 // just-finalized child by one inverse delta apply, so `u` never walks the
820 // ancestor chain even far outside the warm window.
821 if self.get(par).rope_cache.is_none() && self.get(par).base.is_none() {
822 let child_rope = self.get(cur).rope_cache.clone();
823 let child_delta = self.get(cur).delta.clone();
824 if let (Some(cr), Some(d)) = (child_rope, child_delta) {
825 let par_rope = apply_inverse(&cr, &d);
826 self.get_mut(par).rope_cache = Some(par_rope);
827 self.touch_warm(par);
828 }
829 }
830 Some(self.entry_of(par))
831 }
832
833 /// One redo step. Symmetric to [`Self::undo_step`]: `live` is written into
834 /// the node being left (which becomes an undo ancestor) with the
835 /// destination (child) timestamp. Returns the child snapshot to restore, or
836 /// `None` when there is no forward branch.
837 pub(crate) fn redo_step(
838 &mut self,
839 rope: ropey::Rope,
840 cursor: (usize, usize),
841 marks: MarkSnapshot,
842 ) -> Option<UndoEntry> {
843 let cur = self.current;
844 let child = self.get(cur).last_child?;
845 let dest_ts = self.get(child).timestamp;
846 self.set_node_state(cur, rope, cursor, dest_ts, Arc::new(marks));
847 // The path extends by one onto `child`; `cur.last_child` already names
848 // it (it is what we followed), so the ancestor chain stays correct.
849 self.get_mut(child).on_path = true;
850 self.current = child;
851 // `cur` is now warm, so materializing the child is one forward apply.
852 Some(self.entry_of(child))
853 }
854
855 // ── seq-ordered tree walk (`g-` / `g+`, `:earlier`/`:later` — Phase 2b) ───
856 //
857 // `u`/`<C-r>` are branch-local (parent / `last_child`); `g-`/`g+` traverse
858 // ALL states by global `seq`, crossing branch boundaries. `g-` restores the
859 // node with the greatest `seq` strictly below `current`'s; `g+` the least
860 // `seq` strictly above. Confirmed against nvim v0.12.4 (`iA<Esc>uiB<Esc>`
861 // then `g-`/`g-g-`/`g-g+` walks empty↔A↔B by change number).
862
863 /// `seq` of the node the buffer currently shows.
864 fn current_seq(&self) -> u64 {
865 self.get(self.current).seq
866 }
867
868 /// Live node with the greatest `seq` strictly below `s` (the `g-` target).
869 ///
870 /// O(log N) through [`Self::by_seq`]; this used to scan the whole arena on
871 /// every history step.
872 fn node_below(&self, s: u64) -> Option<NodeId> {
873 self.by_seq.range(..s).next_back().map(|(_, &id)| id)
874 }
875
876 /// Live node with the least `seq` strictly above `s` (the `g+` target).
877 ///
878 /// O(log N) through [`Self::by_seq`]; see [`Self::node_below`].
879 fn node_above(&self, s: u64) -> Option<NodeId> {
880 use std::ops::Bound;
881 self.by_seq
882 .range((Bound::Excluded(s), Bound::Unbounded))
883 .next()
884 .map(|(_, &id)| id)
885 }
886
887 /// Point `current` at `target`, re-establishing the invariant that every
888 /// ancestor of `current` names the child on the root→`current` path as its
889 /// `last_child`, so a later `<C-r>` retraces the branch just landed on
890 /// (nvim parity: landing on a node updates its ancestors' redo direction).
891 ///
892 /// Costs O(tree distance from the old `current` to `target`) — 1 for a step
893 /// along a branch — not O(depth). It used to rewrite the whole root→target
894 /// chain on every landing, which was the remaining per-step floor under
895 /// `g-`/`g+` once keyframes had bounded the materialization.
896 ///
897 /// What makes that sound is [`UndoNode::on_path`] being a maintained
898 /// invariant rather than a local test: the flagged nodes are EXACTLY the
899 /// root→`current` chain, and every flagged non-tip node already names its
900 /// on-path child. So the first flagged node found walking up from `target`
901 /// is the fork of the two paths, everything above it is already correct by
902 /// that invariant, and only the segment below it can be wrong.
903 ///
904 /// Do NOT replace this with an early exit out of the old full-chain walk at
905 /// the first ancestor that already names the right child. That is unsound
906 /// and was tried: `last_child` is also written by [`Self::push`] (immediate
907 /// parent only) and by leaf removal, so an ancestor can point the right way
908 /// while ITS ancestors still point down an abandoned branch; the early exit
909 /// leaves those stale and a later `<C-r>` walks into the wrong subtree —
910 /// caught by `tree_matches_full_snapshot_reference_over_random_ops` as a
911 /// redo returning another branch's text. The fix has to come from the path
912 /// being known, which is what `on_path` supplies.
913 fn retarget_current(&mut self, target: NodeId) {
914 // Walk up from `target`, linking each node as its parent's `last_child`,
915 // until a node already on the path: the fork. The root is always on the
916 // path, so this terminates there at the latest.
917 let mut node = target;
918 while !self.get(node).on_path {
919 let p = self
920 .get(node)
921 .parent
922 .expect("the root is always on the path, so the walk stops there");
923 self.get_mut(p).last_child = Some(node);
924 self.get_mut(node).on_path = true;
925 node = p;
926 }
927 let fork = node;
928 // Everything from the old `current` up to (not including) the fork
929 // leaves the path. Their stored `last_child` is deliberately left alone:
930 // an off-path node remembers the branch last taken through it, which is
931 // what lets `<C-r>` retrace that branch if the user lands back on it.
932 let mut leaving = self.current;
933 while leaving != fork {
934 self.get_mut(leaving).on_path = false;
935 leaving = self
936 .get(leaving)
937 .parent
938 .expect("the fork is on the path, hence an ancestor of `current`");
939 }
940 self.current = target;
941 }
942
943 /// Stash the live buffer state into the node being left (it may be a fresh,
944 /// still-stale leaf), preserving that node's own timestamp, then move.
945 fn stash_and_move(
946 &mut self,
947 target: NodeId,
948 rope: ropey::Rope,
949 cursor: (usize, usize),
950 marks: MarkSnapshot,
951 ) {
952 let cur = self.current;
953 let ts = self.get(cur).timestamp;
954 self.set_node_state(cur, rope, cursor, ts, Arc::new(marks));
955 self.retarget_current(target);
956 }
957
958 /// One `g-` / `:earlier` step: move to the next-lower-`seq` node tree-wide.
959 /// Returns its snapshot to restore, or `None` at the lowest state.
960 pub(crate) fn seq_earlier_step(
961 &mut self,
962 rope: ropey::Rope,
963 cursor: (usize, usize),
964 marks: MarkSnapshot,
965 ) -> Option<UndoEntry> {
966 let target = self.node_below(self.current_seq())?;
967 self.stash_and_move(target, rope, cursor, marks);
968 Some(self.entry_of(target))
969 }
970
971 /// One `g+` / `:later` step: move to the next-higher-`seq` node tree-wide.
972 /// Returns its snapshot to restore, or `None` at the highest state.
973 pub(crate) fn seq_later_step(
974 &mut self,
975 rope: ropey::Rope,
976 cursor: (usize, usize),
977 marks: MarkSnapshot,
978 ) -> Option<UndoEntry> {
979 let target = self.node_above(self.current_seq())?;
980 self.stash_and_move(target, rope, cursor, marks);
981 Some(self.entry_of(target))
982 }
983
984 /// Timestamp of the next-lower-`seq` node (the `:earlier Ns` predicate walks
985 /// the seq order tree-wide, stopping once this dips to/below the cutoff).
986 pub(crate) fn seq_earlier_timestamp(&self) -> Option<SystemTime> {
987 self.node_below(self.current_seq())
988 .map(|id| self.get(id).timestamp)
989 }
990
991 /// Timestamp of the next-higher-`seq` node (the `:later Ns` predicate).
992 pub(crate) fn seq_later_timestamp(&self) -> Option<SystemTime> {
993 self.node_above(self.current_seq())
994 .map(|id| self.get(id).timestamp)
995 }
996
997 /// Leaves of the tree (nodes with no children), each as
998 /// `(seq, depth-from-root, timestamp, is_current)`, sorted by `seq`.
999 /// Drives `:undolist`, which — like nvim — lists only branch leaves.
1000 pub(crate) fn leaves(&self) -> Vec<(u64, usize, SystemTime, bool)> {
1001 let mut out: Vec<(u64, usize, SystemTime, bool)> = Vec::new();
1002 for (id, slot) in self.nodes.iter().enumerate() {
1003 let Some(n) = slot else { continue };
1004 // The root is the base state (change number 0), never a listed
1005 // "change" — like nvim, an untouched buffer lists nothing.
1006 if id == self.root || !n.children.is_empty() {
1007 continue;
1008 }
1009 // Depth = number of ancestors (root leaf ⇒ 0).
1010 let mut depth = 0;
1011 let mut p = n.parent;
1012 while let Some(pid) = p {
1013 depth += 1;
1014 p = self.get(pid).parent;
1015 }
1016 out.push((n.seq, depth, n.timestamp, id == self.current));
1017 }
1018 out.sort_by_key(|&(seq, ..)| seq);
1019 out
1020 }
1021
1022 /// Number of live nodes (used by [`Self::cap`] as the state budget).
1023 fn live_count(&self) -> usize {
1024 self.nodes.iter().filter(|n| n.is_some()).count()
1025 }
1026
1027 /// `undo_stack.pop()` — discard the most-recent boundary WITHOUT moving the
1028 /// live state. Used by `:s` with zero replacements and by a no-op undo
1029 /// group; in both, `current` is the childless leaf the last [`Self::push`]
1030 /// created, so reverse that push: drop the leaf, step `current` back to its
1031 /// parent (its snapshot equals the unchanged buffer), and restore the
1032 /// parent's `last_child`. Retains any sibling branches the push appended to.
1033 /// Returns `false` at the root, or if `current` is not a childless leaf
1034 /// (nothing safe to pop).
1035 pub(crate) fn pop_committed(&mut self) -> bool {
1036 let cur = self.current;
1037 if !self.get(cur).children.is_empty() {
1038 return false;
1039 }
1040 let Some(par) = self.get(cur).parent else {
1041 return false;
1042 };
1043 let par_node = self.get_mut(par);
1044 par_node.children.retain(|&c| c != cur);
1045 // The freshest surviving sibling (if any) becomes the redo target again.
1046 par_node.last_child = par_node.children.last().copied();
1047 // The path shortens onto `par` (already on it); `cur` is about to be
1048 // freed, but clear its flag so the invariant holds at every point.
1049 self.get_mut(cur).on_path = false;
1050 self.current = par;
1051 // The popped leaf always holds the highest seq (push assigns it last),
1052 // so reclaim the seq to keep numbering gapless.
1053 if self.get(cur).seq + 1 == self.next_seq {
1054 self.next_seq -= 1;
1055 }
1056 self.free(cur);
1057 true
1058 }
1059
1060 /// Node budget (`undolevels`). While the number of undo states (live nodes
1061 /// minus the root) exceeds `cap`, prune — branch-aware (Phase 2b):
1062 ///
1063 /// 1. First drop the lowest-`seq` LEAF that is NOT on the root→`current`
1064 /// path — an abandoned branch tip. This never touches `current` or its
1065 /// ancestors, so the state you're on and its full undo line survive.
1066 /// 2. When only the main line remains (no off-path leaves left), fall back
1067 /// to promoting the root's on-path child to root and dropping the old
1068 /// root — the Phase 2a root-side prune, which matches nvim's linear
1069 /// `undolevels` trimming (oldest states drop first).
1070 ///
1071 /// `cap == 0` means unlimited (matches the old guard).
1072 pub(crate) fn cap(&mut self, cap: usize) {
1073 if cap == 0 {
1074 return;
1075 }
1076 // Guard against a pathological loop: at most one prune per live node.
1077 let mut budget_iters = self.live_count() + 1;
1078 while self.live_count().saturating_sub(1) > cap && budget_iters > 0 {
1079 budget_iters -= 1;
1080 if let Some(leaf) = self.lowest_offpath_leaf() {
1081 self.detach_leaf(leaf);
1082 } else if !self.prune_root_side() {
1083 break;
1084 }
1085 }
1086 }
1087
1088 /// Lowest-`seq` leaf that is not on the root→`current` path, if any.
1089 fn lowest_offpath_leaf(&self) -> Option<NodeId> {
1090 let mut best: Option<(u64, NodeId)> = None;
1091 for (id, slot) in self.nodes.iter().enumerate() {
1092 if let Some(n) = slot
1093 && n.children.is_empty()
1094 && !n.on_path
1095 && best.is_none_or(|(bs, _)| n.seq < bs)
1096 {
1097 best = Some((n.seq, id));
1098 }
1099 }
1100 best.map(|(_, id)| id)
1101 }
1102
1103 /// Unlink `leaf` from its parent and free it (leaf ⇒ no subtree to recurse).
1104 fn detach_leaf(&mut self, leaf: NodeId) {
1105 if let Some(par) = self.get(leaf).parent {
1106 let par_node = self.get_mut(par);
1107 par_node.children.retain(|&c| c != leaf);
1108 if par_node.last_child == Some(leaf) {
1109 par_node.last_child = par_node.children.last().copied();
1110 }
1111 }
1112 self.free(leaf);
1113 }
1114
1115 /// Promote the root's on-path child to the new root and free the old root.
1116 /// Returns `false` when the root is `current` (nothing left to trim).
1117 fn prune_root_side(&mut self) -> bool {
1118 let root = self.root;
1119 if root == self.current {
1120 return false;
1121 }
1122 // The child on the path to `current` (the root always has one here).
1123 let Some(&child) = self
1124 .get(root)
1125 .children
1126 .iter()
1127 .find(|c| self.get(**c).on_path)
1128 else {
1129 return false;
1130 };
1131 // Any OTHER root children are off-path branches; drop them with the root.
1132 let others: Vec<NodeId> = self
1133 .get(root)
1134 .children
1135 .iter()
1136 .copied()
1137 .filter(|&c| c != child)
1138 .collect();
1139 for c in others {
1140 self.free_subtree(c);
1141 }
1142 // The promoted child becomes the new root: materialize it (while the old
1143 // root still anchors the chain) into a full base rope, then drop its
1144 // now-meaningless parent edge. This keeps every delta below it valid.
1145 let base = self.materialize(child);
1146 {
1147 let node = self.get_mut(child);
1148 node.parent = None;
1149 node.base = Some(base);
1150 node.delta = None;
1151 node.rope_cache = None;
1152 }
1153 self.warm.retain(|&n| n != child);
1154 self.keyframes.retain(|&n| n != child);
1155 // `child` was already on the path (it is the on-path child) and is now
1156 // its root end; the old root drops off it as it is freed.
1157 self.get_mut(root).on_path = false;
1158 self.root = child;
1159 self.free(root);
1160 true
1161 }
1162
1163 /// `redo_stack.clear()` — drop `current`'s forward branch.
1164 pub(crate) fn clear_redo(&mut self) {
1165 let cur = self.current;
1166 let kids = std::mem::take(&mut self.get_mut(cur).children);
1167 self.get_mut(cur).last_child = None;
1168 for c in kids {
1169 self.free_subtree(c);
1170 }
1171 }
1172
1173 /// `undo_stack.clear(); redo_stack.clear()` — collapse to a single root ==
1174 /// current node, preserving the live state. Frees every other node.
1175 pub(crate) fn clear_all(&mut self) {
1176 let cur = self.current;
1177 // The survivor becomes a self-contained root: give it a full base rope
1178 // (materialized while the chain is still intact) so it needs no parent.
1179 let base = self.materialize(cur);
1180 for id in 0..self.nodes.len() {
1181 if id != cur && self.nodes[id].is_some() {
1182 self.nodes[id] = None;
1183 self.free.push(id);
1184 }
1185 }
1186 self.warm.clear();
1187 self.keyframes.clear();
1188 // Bulk free bypassed `free`, so rebuild the index around the survivor.
1189 self.by_seq.clear();
1190 self.by_seq.insert(self.get(cur).seq, cur);
1191 let node = self.get_mut(cur);
1192 node.parent = None;
1193 node.children.clear();
1194 node.last_child = None;
1195 node.delta = None;
1196 node.base = Some(base);
1197 node.rope_cache = None;
1198 // The survivor is the new root: restart the depth numbering under it so
1199 // its descendants land on the keyframe ladder from 0 again.
1200 node.depth = 0;
1201 // Sole survivor ⇒ root == current ⇒ it is the whole path.
1202 node.on_path = true;
1203 self.root = cur;
1204 }
1205}
1206
1207// ─── Serializable projection (Phase 3b) ───────────────────────────────────────
1208//
1209// The undofile persists the tree as a compact, self-consistent projection: the
1210// root's full base text (String) plus, per node, its edge `delta` and links.
1211// `rope_cache`/`warm` are runtime-only and dropped — every node reconstructs
1212// from the root base + deltas, so the round-trip reproduces identical content
1213// at every node. NodeIds are DENSE in the projection (the live-slab holes are
1214// compacted away and links remapped), so `from_serializable` rebuilds a fresh
1215// arena 1:1 with no free list.
1216
1217/// One node of the serialized undo tree. Mirrors [`UndoNode`] minus the
1218/// runtime-only materialization cache; ids are dense indices into
1219/// [`SerTree::nodes`].
1220#[derive(Debug, Clone, Serialize, Deserialize)]
1221pub struct SerNode {
1222 /// Parent index, `None` only for the root.
1223 pub parent: Option<u32>,
1224 /// Child indices (order preserved; `> 1` ⇒ branch point).
1225 pub children: Vec<u32>,
1226 /// `<C-r>` target child index.
1227 pub last_child: Option<u32>,
1228 /// Reversible edge delta from the parent, `None` only for the root.
1229 pub delta: Option<Delta>,
1230 /// Post-state cursor `(row, col)`.
1231 pub cursor: (u32, u32),
1232 /// Wall-clock creation time, ms since the UNIX epoch.
1233 pub timestamp_unix_ms: u64,
1234 /// Marks / jumplist / changelist snapshot.
1235 pub marks: MarkSnapshot,
1236 /// Global monotonic change number.
1237 pub seq: u64,
1238}
1239
1240/// Serializable projection of an [`UndoTree`] for the undofile and the swap
1241/// undo section. Postcard-encoded (non-self-describing, so a schema/version
1242/// drift surfaces as a parse `Err` that the reader discards). See
1243/// [`UndoTree::to_serializable`] / [`UndoTree::from_serializable`].
1244#[derive(Debug, Clone, Serialize, Deserialize)]
1245pub struct SerTree {
1246 /// Root base text (the anchor the delta chain replays from). Always
1247 /// `Some` on write; `from_serializable` rejects `None` — a tree without an
1248 /// anchor is structurally invalid outside the swap context. The swap
1249 /// writer drops it for single-node trees (the body IS the base) and the
1250 /// swap reader re-substitutes the body text before the tree is rebuilt.
1251 pub base: Option<String>,
1252 /// Dense node arena (no holes).
1253 pub nodes: Vec<SerNode>,
1254 /// Root index into `nodes`.
1255 pub root: u32,
1256 /// Current (live) index into `nodes`.
1257 pub current: u32,
1258 /// Next `seq` to assign.
1259 pub next_seq: u64,
1260}
1261
1262/// [`SystemTime`] → ms since the UNIX epoch (saturating, pre-epoch ⇒ 0).
1263fn system_time_to_unix_ms(t: SystemTime) -> u64 {
1264 t.duration_since(UNIX_EPOCH)
1265 .map_or(0, |d| d.as_millis() as u64)
1266}
1267
1268/// ms since the UNIX epoch → [`SystemTime`].
1269fn unix_ms_to_system_time(ms: u64) -> SystemTime {
1270 UNIX_EPOCH + Duration::from_millis(ms)
1271}
1272
1273impl UndoTree {
1274 /// `seq` of the current (live) node — the header's `current_seq` for the
1275 /// undofile (the just-saved content per the §6 invariant).
1276 pub(crate) fn current_node_seq(&self) -> u64 {
1277 self.get(self.current).seq
1278 }
1279
1280 /// Materialize the current (live) node's content. Used by the swap
1281 /// recovery consistency guard (docs §6c) to check a deserialized tree
1282 /// agrees with the freshly-recovered buffer text before it's installed.
1283 pub(crate) fn current_content(&mut self) -> ropey::Rope {
1284 let cur = self.current;
1285 self.materialize(cur)
1286 }
1287
1288 /// Stash `rope` into the current node as the live buffer state, preserving
1289 /// that node's own cursor/timestamp/marks. Called just before serializing so
1290 /// the on-disk tree's `current` edge is exact even when `current` is a fresh
1291 /// (still-stale) leaf — the in-session self-heal (first undo/edit stashes
1292 /// live) applied eagerly at save time.
1293 pub(crate) fn sync_current(&mut self, rope: ropey::Rope) {
1294 let cur = self.current;
1295 let (cursor, ts, marks) = {
1296 let n = self.get(cur);
1297 (n.cursor, n.timestamp, n.marks.clone())
1298 };
1299 self.set_node_state(cur, rope, cursor, ts, marks);
1300 }
1301
1302 /// Project the live tree into a serializable, dense form (holes compacted,
1303 /// links remapped). `rope_cache`/`warm` are dropped; the root's `base`
1304 /// carries the anchor text and every non-root node its edge `delta`.
1305 pub(crate) fn to_serializable(&self) -> SerTree {
1306 // Dense remap: old NodeId → new index, in slab order.
1307 let mut map: Vec<Option<u32>> = vec![None; self.nodes.len()];
1308 let mut order: Vec<NodeId> = Vec::new();
1309 for (id, slot) in self.nodes.iter().enumerate() {
1310 if slot.is_some() {
1311 map[id] = Some(order.len() as u32);
1312 order.push(id);
1313 }
1314 }
1315 let remap = |id: NodeId| map[id].expect("live link points at a live node");
1316 let nodes = order
1317 .iter()
1318 .map(|&id| {
1319 let n = self.get(id);
1320 SerNode {
1321 parent: n.parent.map(remap),
1322 children: n.children.iter().map(|&c| remap(c)).collect(),
1323 last_child: n.last_child.map(remap),
1324 delta: n.delta.clone(),
1325 cursor: (n.cursor.0 as u32, n.cursor.1 as u32),
1326 timestamp_unix_ms: system_time_to_unix_ms(n.timestamp),
1327 marks: (*n.marks).clone(),
1328 seq: n.seq,
1329 }
1330 })
1331 .collect();
1332 // Always `Some`: the undofile and the swap's multi-node trees anchor on
1333 // the root text; only the swap writer may drop it (single-node trees,
1334 // where the body IS the base) and it re-substitutes on read.
1335 let base = Some(
1336 self.get(self.root)
1337 .base
1338 .as_ref()
1339 .map(|r| r.to_string())
1340 .unwrap_or_default(),
1341 );
1342 SerTree {
1343 base,
1344 nodes,
1345 root: remap(self.root),
1346 current: remap(self.current),
1347 next_seq: self.next_seq,
1348 }
1349 }
1350
1351 /// Rebuild an arena tree from a projection. Returns `None` on any structural
1352 /// inconsistency (out-of-range link, a non-root node missing its delta, a
1353 /// root carrying one, `children` lists that do not partition the non-root
1354 /// nodes, a child disagreeing with the node that listed it, a node
1355 /// unreachable from the root, a repeated `seq`) so a corrupt-but-parseable
1356 /// file degrades to a fresh tree rather than a broken one. The root's
1357 /// content comes from `base`; the current node's is materialized on demand
1358 /// from base + deltas.
1359 ///
1360 /// The partition + reachability pair is what makes the parent links a tree,
1361 /// and that is load-bearing rather than tidiness: [`Self::materialize`] and
1362 /// [`Self::retarget_current`] follow `parent` in unbounded loops, so a
1363 /// parent-link cycle is a hang (or an unbounded `path`) rather than a
1364 /// degraded tree. Rejecting it here is the only place that can see it.
1365 pub(crate) fn from_serializable(s: &SerTree) -> Option<Self> {
1366 let len = s.nodes.len();
1367 if len == 0 || s.root as usize >= len || s.current as usize >= len {
1368 return None;
1369 }
1370 // A tree without an anchor is structurally invalid outside the swap
1371 // context, where the body supplies the base (the swap reader fills it
1372 // before this is ever reached). Reject rather than guess.
1373 let base_str = s.base.as_deref()?;
1374 // Validate links and the root/non-root delta discipline up front.
1375 let mut seqs = std::collections::BTreeSet::new();
1376 for (i, n) in s.nodes.iter().enumerate() {
1377 let is_root = i as u32 == s.root;
1378 match (is_root, &n.delta, &n.parent) {
1379 (true, None, None) => {}
1380 (false, Some(_), Some(_)) => {}
1381 _ => return None,
1382 }
1383 if let Some(p) = n.parent
1384 && p as usize >= len
1385 {
1386 return None;
1387 }
1388 if n.children.iter().any(|&c| c as usize >= len) {
1389 return None;
1390 }
1391 if let Some(c) = n.last_child
1392 && c as usize >= len
1393 {
1394 return None;
1395 }
1396 // `by_seq` is keyed by `seq`, so a repeat would drop a node from the
1397 // `g-` / `g+` index without dropping it from the arena.
1398 if !seqs.insert(n.seq) {
1399 return None;
1400 }
1401 }
1402 // The `children` lists must PARTITION the non-root nodes — each listed
1403 // once, by nobody for the root — and every child's own `parent` must
1404 // name the node that listed it. Mutual agreement alone is not enough:
1405 // two nodes can name each other as parent AND as child while both stay
1406 // reachable from the root through the lists they were originally in.
1407 // Requiring a single lister is what turns the two directions into one
1408 // tree. Runs after the range loop so the indices below are in bounds.
1409 let mut listed_by: Vec<Option<u32>> = vec![None; len];
1410 for (i, n) in s.nodes.iter().enumerate() {
1411 for &c in &n.children {
1412 if c == s.root || listed_by[c as usize].is_some() {
1413 return None;
1414 }
1415 listed_by[c as usize] = Some(i as u32);
1416 }
1417 }
1418 if s.nodes
1419 .iter()
1420 .zip(listed_by.iter())
1421 .any(|(n, &lister)| n.parent != lister)
1422 {
1423 return None;
1424 }
1425 let base = ropey::Rope::from_str(base_str);
1426 let (depths, reachable) = depths_from_root(s);
1427 // With the lists partitioning the nodes, reachability from the root is
1428 // what rules out a cycle: a node inside one is reachable from no root.
1429 if reachable.iter().any(|&r| !r) {
1430 return None;
1431 }
1432 let mut nodes: Vec<Option<UndoNode>> = s
1433 .nodes
1434 .iter()
1435 .enumerate()
1436 .map(|(i, n)| {
1437 let is_root = i as u32 == s.root;
1438 Some(UndoNode {
1439 parent: n.parent.map(|p| p as NodeId),
1440 children: n.children.iter().map(|&c| c as NodeId).collect(),
1441 last_child: n.last_child.map(|c| c as NodeId),
1442 delta: n.delta.clone(),
1443 base: if is_root { Some(base.clone()) } else { None },
1444 rope_cache: None,
1445 depth: depths[i],
1446 cursor: (n.cursor.0 as usize, n.cursor.1 as usize),
1447 timestamp: unix_ms_to_system_time(n.timestamp_unix_ms),
1448 marks: Arc::new(n.marks.clone()),
1449 seq: n.seq,
1450 // Set below, once the whole arena exists to walk.
1451 on_path: false,
1452 })
1453 })
1454 .collect();
1455 // Establish the root→`current` path invariant on the loaded tree: flag
1456 // the chain, and make each ancestor name its on-path child. A projection
1457 // written by `to_serializable` already agrees (it came from a tree
1458 // holding the invariant), so this is a no-op on any file we produced —
1459 // doing it unconditionally is what stops a hand-edited or truncated one
1460 // from loading into a tree whose `<C-r>` direction contradicts its own
1461 // `current`, which `retarget_current` would no longer repair on the way
1462 // past. The validation above already rules out a parent-link cycle, so
1463 // the `len` bound is belt-and-braces on the one walk that runs before
1464 // any invariant of the rebuilt tree holds.
1465 let mut node = s.current as NodeId;
1466 for _ in 0..len {
1467 let n = nodes[node].as_mut().expect("the projection is dense");
1468 n.on_path = true;
1469 let Some(p) = n.parent else { break };
1470 nodes[p]
1471 .as_mut()
1472 .expect("the projection is dense")
1473 .last_child = Some(node);
1474 node = p;
1475 }
1476 let by_seq = nodes
1477 .iter()
1478 .enumerate()
1479 .filter_map(|(id, slot)| slot.as_ref().map(|n| (n.seq, id)))
1480 .collect();
1481 Some(Self {
1482 nodes,
1483 free: Vec::new(),
1484 warm: Vec::new(),
1485 keyframes: Vec::new(),
1486 by_seq,
1487 root: s.root as NodeId,
1488 current: s.current as NodeId,
1489 next_seq: s.next_seq,
1490 })
1491 }
1492}
1493
1494/// Depth-from-root of every node in a projection, by BFS over `children`, plus
1495/// the reachable set the walk visited.
1496///
1497/// Depth is NOT part of the on-disk format — it is derivable, and the undofile
1498/// deliberately stores only what is not (issue #302: keyframes are an in-memory
1499/// cache, so nothing about them enters `SerTree`). The `seen` guard makes this
1500/// terminate on a malformed file whose links form a cycle; anything unreachable
1501/// from the root keeps depth 0. `from_serializable` rejects a projection with
1502/// any unreachable node, so the returned depths are only ever used on a tree
1503/// where every one of them was computed by the walk.
1504fn depths_from_root(s: &SerTree) -> (Vec<usize>, Vec<bool>) {
1505 let mut depths = vec![0usize; s.nodes.len()];
1506 let mut seen = vec![false; s.nodes.len()];
1507 let mut queue = std::collections::VecDeque::new();
1508 seen[s.root as usize] = true;
1509 queue.push_back(s.root as usize);
1510 while let Some(i) = queue.pop_front() {
1511 for &c in &s.nodes[i].children {
1512 let c = c as usize;
1513 if !seen[c] {
1514 seen[c] = true;
1515 depths[c] = depths[i] + 1;
1516 queue.push_back(c);
1517 }
1518 }
1519 }
1520 (depths, seen)
1521}
1522
1523#[cfg(test)]
1524impl UndoTree {
1525 /// Ids of every live node, for warm-vs-cold materialization checks.
1526 fn live_ids(&self) -> Vec<NodeId> {
1527 (0..self.nodes.len())
1528 .filter(|&i| self.nodes[i].is_some())
1529 .collect()
1530 }
1531
1532 /// Materialize `id` for a test (public wrapper over the private method).
1533 fn materialize_for_test(&mut self, id: NodeId) -> ropey::Rope {
1534 self.materialize(id)
1535 }
1536
1537 /// Evict every cache INCLUDING the pinned keyframes (root keeps its `base`),
1538 /// forcing the next materialization of any node to reconstruct purely from
1539 /// deltas off the root — the strongest cold path there is.
1540 fn drop_all_caches(&mut self) {
1541 for n in self.nodes.iter_mut().flatten() {
1542 n.rope_cache = None;
1543 }
1544 self.warm.clear();
1545 self.keyframes.clear();
1546 }
1547
1548 /// Evict only the ordinary warm LRU, leaving the pinned keyframes — the
1549 /// steady state a deep history walk actually runs in.
1550 fn drop_warm_caches(&mut self) {
1551 for id in std::mem::take(&mut self.warm) {
1552 if let Some(n) = self.nodes[id].as_mut() {
1553 n.rope_cache = None;
1554 }
1555 }
1556 }
1557
1558 /// How many forward delta applies `materialize(id)` would perform right now
1559 /// (0 when `id` already holds content). This is the cost keyframes exist to
1560 /// bound, made assertable.
1561 fn replay_distance(&self, id: NodeId) -> usize {
1562 let mut n = 0;
1563 let mut cur = id;
1564 loop {
1565 let node = self.get(cur);
1566 if node.rope_cache.is_some() || node.base.is_some() {
1567 return n;
1568 }
1569 n += 1;
1570 match node.parent {
1571 Some(p) => cur = p,
1572 None => return n,
1573 }
1574 }
1575 }
1576
1577 /// The root→`current` path, by brute force: walk `parent` links up from
1578 /// `current` and reverse. Deliberately consults NEITHER `on_path` nor
1579 /// `last_child`, so it is an independent oracle for both.
1580 fn brute_force_path(&self) -> Vec<NodeId> {
1581 let mut walk = Vec::new();
1582 let mut n = Some(self.current);
1583 while let Some(id) = n {
1584 walk.push(id);
1585 n = self.get(id).parent;
1586 }
1587 walk.reverse();
1588 walk
1589 }
1590
1591 /// The two halves of the path invariant `retarget_current` now relies on,
1592 /// checked against [`Self::brute_force_path`]:
1593 ///
1594 /// 1. `on_path` is set on EXACTLY the nodes of the root→`current` walk.
1595 /// 2. every node on that walk except the tip names its successor as
1596 /// `last_child` — the observable property (`<C-r>` retraces the branch
1597 /// landed on) that the old full-chain rewrite established directly.
1598 ///
1599 /// (1) is the maintenance; (2) is what the maintenance buys. Checking only
1600 /// (2) would pass on a tree whose flags had drifted but whose links happened
1601 /// to be right; checking only (1) would pass on a tree that had stopped
1602 /// linking. Both must hold after every operation that moves `current`,
1603 /// allocates, or frees.
1604 #[track_caller]
1605 fn assert_path_invariant(&self, when: &str) {
1606 let walk = self.brute_force_path();
1607 assert_eq!(
1608 walk.first().copied(),
1609 Some(self.root),
1610 "the root→current walk does not start at the root after {when}"
1611 );
1612 let mut want = walk.clone();
1613 want.sort_unstable();
1614 let flagged: Vec<NodeId> = (0..self.nodes.len())
1615 .filter(|&i| self.nodes[i].as_ref().is_some_and(|n| n.on_path))
1616 .collect();
1617 assert_eq!(
1618 flagged, want,
1619 "on_path flags name {flagged:?}, the root→current walk is {want:?}, after {when}"
1620 );
1621 for w in walk.windows(2) {
1622 assert_eq!(
1623 self.get(w[0]).last_child,
1624 Some(w[1]),
1625 "node {} last_child is {:?}, not its on-path child {}, after {when}",
1626 w[0],
1627 self.get(w[0]).last_child,
1628 w[1]
1629 );
1630 }
1631 }
1632
1633 /// Reconstruct `id`'s content the naive way: walk to the root and replay
1634 /// every forward delta off the root `base`, consulting NO cache and NO
1635 /// keyframe. The differential oracle for keyframe-accelerated
1636 /// [`Self::materialize`] — the two must agree exactly, always.
1637 fn materialize_naive(&self, id: NodeId) -> ropey::Rope {
1638 let mut path = vec![id];
1639 let mut cur = id;
1640 while let Some(p) = self.get(cur).parent {
1641 path.push(p);
1642 cur = p;
1643 }
1644 let mut rope = self
1645 .get(cur)
1646 .base
1647 .clone()
1648 .expect("the root always carries a base");
1649 // Skip the root itself (it has no edge delta); replay root-ward → target.
1650 for &node in path.iter().rev().skip(1) {
1651 let d = self
1652 .get(node)
1653 .delta
1654 .as_ref()
1655 .expect("a non-root node always carries its edge delta");
1656 rope = apply_forward(&rope, d);
1657 }
1658 rope
1659 }
1660}
1661
1662#[cfg(test)]
1663mod tree_tests {
1664 use super::*;
1665
1666 fn entry(text: &str) -> UndoEntry {
1667 UndoEntry {
1668 rope: ropey::Rope::from_str(text),
1669 cursor: (0, 0),
1670 timestamp: SystemTime::now(),
1671 marks: MarkSnapshot::default(),
1672 }
1673 }
1674
1675 fn live(text: &str) -> (ropey::Rope, (usize, usize), MarkSnapshot) {
1676 (ropey::Rope::from_str(text), (0, 0), MarkSnapshot::default())
1677 }
1678
1679 #[test]
1680 fn fresh_tree_is_root_current_empty() {
1681 let t = UndoTree::new(ropey::Rope::from_str("hello"));
1682 assert!(t.is_at_root());
1683 assert!(!t.has_redo());
1684 assert_eq!(t.depth_from_root(), 0);
1685 assert_eq!(t.root, t.current);
1686 }
1687
1688 #[test]
1689 fn push_links_child_and_advances_current() {
1690 let mut t = UndoTree::new(ropey::Rope::from_str("hello"));
1691 let root = t.current;
1692 t.push(entry("hello"));
1693 // root now parents current; current is a fresh leaf.
1694 assert_eq!(t.get(t.current).parent, Some(root));
1695 assert_eq!(t.get(root).last_child, Some(t.current));
1696 assert_eq!(t.get(root).children, vec![t.current]);
1697 assert_eq!(t.depth_from_root(), 1);
1698 assert!(!t.has_redo());
1699 assert!(!t.is_at_root());
1700 }
1701
1702 #[test]
1703 fn undo_then_redo_round_trips_links() {
1704 let mut t = UndoTree::new(ropey::Rope::from_str("s0"));
1705 t.push(entry("s0")); // commit s0, current = n1 (live s1)
1706 let n0 = t.root;
1707 let n1 = t.current;
1708 // undo: current -> n0, restores s0.
1709 let (r, c, m) = live("s1");
1710 let restored = t.undo_step(r, c, m).unwrap();
1711 assert_eq!(restored.rope.to_string(), "s0");
1712 assert_eq!(t.current, n0);
1713 assert!(t.has_redo());
1714 assert_eq!(t.get(n0).last_child, Some(n1));
1715 // redo: current -> n1, restores what we left (s1).
1716 let (r, c, m) = live("s0");
1717 let restored = t.redo_step(r, c, m).unwrap();
1718 assert_eq!(restored.rope.to_string(), "s1");
1719 assert_eq!(t.current, n1);
1720 assert!(!t.has_redo());
1721 }
1722
1723 #[test]
1724 fn undo_at_root_and_redo_at_leaf_are_noops() {
1725 let mut t = UndoTree::new(ropey::Rope::from_str("x"));
1726 let (r, c, m) = live("x");
1727 assert!(t.undo_step(r, c, m).is_none());
1728 let (r, c, m) = live("x");
1729 assert!(t.redo_step(r, c, m).is_none());
1730 assert_eq!(t.depth_from_root(), 0);
1731 }
1732
1733 #[test]
1734 fn push_retains_forward_branch() {
1735 // Phase 2b: an edit after an undo forks a new branch; the old forward
1736 // branch is NOT dropped and remains reachable by seq.
1737 let mut t = UndoTree::new(ropey::Rope::from_str("s0"));
1738 t.push(entry("A")); // root -> nA (seq1, "A")
1739 let root = t.root;
1740 let na = t.current;
1741 let (r, c, m) = live("A");
1742 t.undo_step(r, c, m); // back to root, nA is the redo child
1743 assert!(t.has_redo());
1744 // A new edit from the root forks a SECOND child (nB, seq2).
1745 t.push(entry("B"));
1746 let nb = t.current;
1747 assert_ne!(nb, na);
1748 // Both branches live: root now has two children.
1749 assert_eq!(t.get(root).children.len(), 2);
1750 assert!(t.get(root).children.contains(&na));
1751 assert!(t.get(root).children.contains(&nb));
1752 // `<C-r>` follows the freshest branch (nB).
1753 assert_eq!(t.get(root).last_child, Some(nb));
1754 // Four live nodes: root + nA + nB + (nB is current/leaf). No leak of nA.
1755 let live = t.nodes.iter().filter(|n| n.is_some()).count();
1756 assert_eq!(live, 3);
1757 }
1758
1759 #[test]
1760 fn seq_walk_crosses_branches() {
1761 // Mirror nvim `iA<Esc>uiB<Esc>` then g-/g+ (buffer starts empty "").
1762 // `push(entry)` writes `entry` into the node being LEFT (its true
1763 // pre-edit content); the fresh leaf holds the live post-edit state only
1764 // once it is stashed on the way past — exactly the engine's discipline.
1765 let mut t = UndoTree::new(ropey::Rope::from_str(""));
1766 t.push(entry("")); // leave root("") -> nA(seq1), live "A"
1767 let (r, c, m) = live("A");
1768 t.undo_step(r, c, m); // stash "A" into nA, back to root("")
1769 t.push(entry("")); // leave root("") -> nB(seq2), branch, live "B"
1770 let nb = t.current;
1771 // At B (seq2). g- -> greatest seq below 2 = seq1 = "A".
1772 let (r, c, m) = live("B");
1773 let a = t.seq_earlier_step(r, c, m).unwrap();
1774 assert_eq!(a.rope.to_string(), "A");
1775 // g- again -> root "".
1776 let (r, c, m) = live("A");
1777 let root_snap = t.seq_earlier_step(r, c, m).unwrap();
1778 assert_eq!(root_snap.rope.to_string(), "");
1779 // g+ -> back up to seq1 "A".
1780 let (r, c, m) = live("");
1781 let a2 = t.seq_later_step(r, c, m).unwrap();
1782 assert_eq!(a2.rope.to_string(), "A");
1783 // g+ -> seq2 "B" (crosses to the other branch).
1784 let (r, c, m) = live("A");
1785 let b = t.seq_later_step(r, c, m).unwrap();
1786 assert_eq!(b.rope.to_string(), "B");
1787 assert_eq!(t.current, nb);
1788 // At the tip: no higher seq.
1789 let (r, c, m) = live("B");
1790 assert!(t.seq_later_step(r, c, m).is_none());
1791 }
1792
1793 #[test]
1794 fn seq_walk_updates_retrace_path() {
1795 // Land on a deep leaf via g-, then u/u and <C-r>/<C-r> must retrace it
1796 // (nvim `iX<Esc>iY<Esc>uiZ<Esc>g-uu<C-r><C-r>`). State labels: root "R".
1797 let mut t = UndoTree::new(ropey::Rope::from_str("R"));
1798 t.push(entry("R")); // leave root("R") -> nX(seq1), live "X"
1799 t.push(entry("X")); // leave nX("X") -> nY(seq2), live "Y"
1800 let (r, c, m) = live("Y");
1801 t.undo_step(r, c, m); // stash "Y" into nY, back to nX("X")
1802 t.push(entry("X")); // leave nX("X") -> nZ(seq3), branch, live "Z"
1803 // g- from Z(seq3) -> nY(seq2) "Y".
1804 let (r, c, m) = live("Z");
1805 let y = t.seq_earlier_step(r, c, m).unwrap();
1806 assert_eq!(y.rope.to_string(), "Y");
1807 // u,u back to root.
1808 let (r, c, m) = live("Y");
1809 t.undo_step(r, c, m);
1810 let (r, c, m) = live("X");
1811 t.undo_step(r, c, m);
1812 assert!(t.is_at_root());
1813 // <C-r>,<C-r> retraces the branch we landed on: root->X->Y.
1814 let (r, c, m) = live("R");
1815 let x = t.redo_step(r, c, m).unwrap();
1816 assert_eq!(x.rope.to_string(), "X");
1817 let (r, c, m) = live("X");
1818 let y2 = t.redo_step(r, c, m).unwrap();
1819 assert_eq!(y2.rope.to_string(), "Y");
1820 }
1821
1822 #[test]
1823 fn leaves_lists_branch_tips_by_seq() {
1824 // root -> nX -> nY -> nW (leaf, seq3, depth3) and nX -> nZ (leaf, seq4,
1825 // depth2). Mirrors nvim `iX iY iW uu iZ`.
1826 let mut t = UndoTree::new(ropey::Rope::from_str(""));
1827 t.push(entry("X"));
1828 t.push(entry("Y"));
1829 t.push(entry("W"));
1830 let (r, c, m) = live("W");
1831 t.undo_step(r, c, m);
1832 let (r, c, m) = live("Y");
1833 t.undo_step(r, c, m); // back to nX
1834 t.push(entry("Z")); // nX -> nZ(seq4)
1835 let leaves = t.leaves();
1836 // Two leaves: W(seq3, depth3) and Z(seq4, depth2). Z is current.
1837 let dims: Vec<(u64, usize, bool)> =
1838 leaves.iter().map(|&(s, d, _, cur)| (s, d, cur)).collect();
1839 assert_eq!(dims, vec![(3, 3, false), (4, 2, true)]);
1840 }
1841
1842 #[test]
1843 fn cap_prunes_oldest_from_root_side() {
1844 let mut t = UndoTree::new(ropey::Rope::from_str("s"));
1845 for _ in 0..5 {
1846 t.push(entry("s"));
1847 }
1848 assert_eq!(t.depth_from_root(), 5);
1849 t.cap(3);
1850 assert_eq!(t.depth_from_root(), 3);
1851 // Redo side untouched (there is none), current unchanged.
1852 assert!(!t.has_redo());
1853 // Two oldest slots were reclaimed.
1854 assert_eq!(t.free.len(), 2);
1855 }
1856
1857 #[test]
1858 fn cap_drops_offpath_leaf_before_main_line() {
1859 // Fork two abandoned branches off the root, then extend the main line,
1860 // and cap: the lowest-seq OFF-PATH leaf must go first, and `current`
1861 // plus its ancestors must survive.
1862 let mut t = UndoTree::new(ropey::Rope::from_str(""));
1863 t.push(entry("A")); // root -> nA(seq1) [abandoned branch tip]
1864 let na = t.current;
1865 let (r, c, m) = live("A");
1866 t.undo_step(r, c, m);
1867 t.push(entry("B")); // root -> nB(seq2) [abandoned branch tip]
1868 let nb = t.current;
1869 let (r, c, m) = live("B");
1870 t.undo_step(r, c, m);
1871 t.push(entry("C")); // root -> nC(seq3), the live main line
1872 let nc = t.current;
1873 // 4 live nodes (root, nA, nB, nC) => 3 states. Cap to 2.
1874 assert_eq!(t.leaves().len(), 3);
1875 t.cap(2);
1876 // The lowest-seq off-path leaf (nA, seq1) was dropped; current (nC) and
1877 // its ancestor (root) survive, and the newer off-path leaf nB survives.
1878 assert!(t.nodes[na].is_none());
1879 assert!(t.nodes[nb].is_some());
1880 assert_eq!(t.current, nc);
1881 assert!(!t.is_at_root());
1882 assert!(t.get(t.root).children.contains(&nb));
1883 assert!(t.get(t.root).children.contains(&nc));
1884 }
1885
1886 #[test]
1887 fn pop_committed_reverses_last_push() {
1888 let mut t = UndoTree::new(ropey::Rope::from_str("s0"));
1889 t.push(entry("s0")); // depth 1, current = fresh leaf
1890 assert_eq!(t.depth_from_root(), 1);
1891 assert!(t.pop_committed());
1892 // The just-pushed leaf is gone; current stepped back to the root.
1893 assert_eq!(t.depth_from_root(), 0);
1894 assert!(t.is_at_root());
1895 assert_eq!(t.free.len(), 1);
1896 // Seq reclaimed so the next push is gapless.
1897 assert_eq!(t.next_seq, 1);
1898 }
1899
1900 #[test]
1901 fn pop_committed_retains_sibling_branches() {
1902 // Fork a branch, then a no-op push at the fork must pop cleanly without
1903 // orphaning the sibling branch.
1904 let mut t = UndoTree::new(ropey::Rope::from_str(""));
1905 t.push(entry("A")); // root -> nA(seq1)
1906 let na = t.current;
1907 let (r, c, m) = live("A");
1908 t.undo_step(r, c, m); // back to root
1909 t.push(entry("B")); // root -> nB(seq2); root children [nA, nB]
1910 let root = t.root;
1911 // A spurious no-op push at nB, then pop it.
1912 assert!(t.pop_committed());
1913 // nB is gone, current back at root; nA branch still intact & reachable.
1914 assert!(t.get(root).children.contains(&na));
1915 assert_eq!(t.get(root).children.len(), 1);
1916 assert_eq!(t.current, root);
1917 let live = t.nodes.iter().filter(|n| n.is_some()).count();
1918 assert_eq!(live, 2); // root + nA
1919 }
1920
1921 #[test]
1922 fn pop_committed_at_root_is_false() {
1923 let mut t = UndoTree::new(ropey::Rope::from_str("s"));
1924 assert!(!t.pop_committed());
1925 }
1926
1927 #[test]
1928 fn clear_redo_drops_forward_only() {
1929 let mut t = UndoTree::new(ropey::Rope::from_str("s0"));
1930 t.push(entry("s0"));
1931 let (r, c, m) = live("s1");
1932 t.undo_step(r, c, m);
1933 assert!(t.has_redo());
1934 assert_eq!(t.depth_from_root(), 0);
1935 t.clear_redo();
1936 assert!(!t.has_redo());
1937 assert_eq!(t.depth_from_root(), 0);
1938 }
1939
1940 #[test]
1941 fn clear_all_collapses_to_single_node() {
1942 let mut t = UndoTree::new(ropey::Rope::from_str("s"));
1943 for _ in 0..3 {
1944 t.push(entry("s"));
1945 }
1946 t.clear_all();
1947 assert!(t.is_at_root());
1948 assert!(!t.has_redo());
1949 assert_eq!(t.depth_from_root(), 0);
1950 assert_eq!(t.root, t.current);
1951 }
1952
1953 /// The depth measures where `current` sits, not how many nodes exist: an
1954 /// undo walks it back down while the branch it came from stays live, and a
1955 /// redo climbs the same steps again.
1956 #[test]
1957 fn depth_from_root_follows_current_not_tree_size() {
1958 let mut t = UndoTree::new(ropey::Rope::from_str("s0"));
1959 t.push(entry("s0"));
1960 t.push(entry("s1"));
1961 t.push(entry("s2"));
1962 assert_eq!(t.depth_from_root(), 3);
1963
1964 let (r, c, m) = live("s3");
1965 assert!(t.undo_step(r, c, m).is_some());
1966 let (r, c, m) = live("s2");
1967 assert!(t.undo_step(r, c, m).is_some());
1968 assert_eq!(t.depth_from_root(), 1);
1969 // Nothing was pruned — the three pushed nodes are still reachable.
1970 assert!(t.has_redo());
1971 assert_eq!(t.live_count(), 4);
1972
1973 let (r, c, m) = live("s1");
1974 assert!(t.redo_step(r, c, m).is_some());
1975 assert_eq!(t.depth_from_root(), 2);
1976 }
1977
1978 /// `on_path` and the ancestors' `last_child` chain must agree with a
1979 /// brute-force root→`current` walk after EVERY operation that moves
1980 /// `current`, allocates, or frees — that invariant is the only thing making
1981 /// `retarget_current`'s fork-point shortcut sound, and a drift in it is
1982 /// silent (a later `<C-r>` restores another branch's text, no error).
1983 ///
1984 /// One tree walked through every such site in turn: `new`, `push`,
1985 /// `undo_step`, `redo_step`, `seq_earlier_step`/`seq_later_step` (i.e.
1986 /// `retarget_current`, including a landing that crosses to a sibling
1987 /// branch), `clear_redo`, `cap` (both the off-path-leaf and the
1988 /// root-promotion prune), `pop_committed`, `from_serializable` and
1989 /// `clear_all`.
1990 #[test]
1991 fn path_flags_track_the_root_to_current_walk() {
1992 let mut t = UndoTree::new(ropey::Rope::from_str("R"));
1993 t.assert_path_invariant("new");
1994
1995 // Main line: R -> A -> B -> C.
1996 for s in ["R", "A", "B"] {
1997 t.push(entry(s));
1998 t.assert_path_invariant("push");
1999 }
2000 // Back down two, forking a sibling branch off the middle.
2001 let (r, c, m) = live("C");
2002 assert!(t.undo_step(r, c, m).is_some());
2003 t.assert_path_invariant("undo_step");
2004 let (r, c, m) = live("B");
2005 assert!(t.undo_step(r, c, m).is_some());
2006 t.assert_path_invariant("undo_step");
2007 let (r, c, m) = live("A");
2008 assert!(t.redo_step(r, c, m).is_some());
2009 t.assert_path_invariant("redo_step");
2010 let (r, c, m) = live("B");
2011 assert!(t.undo_step(r, c, m).is_some());
2012 t.push(entry("A")); // A -> X, a second branch under A
2013 t.assert_path_invariant("push forking a branch");
2014
2015 // `g-` / `g+` walk the whole tree by seq, so they land on nodes in the
2016 // OTHER branch — the case that actually exercises the fork point rather
2017 // than a parent/child step.
2018 let mut cur = String::from("X");
2019 for _ in 0..6 {
2020 if let Some(e) =
2021 t.seq_earlier_step(ropey::Rope::from_str(&cur), (0, 0), MarkSnapshot::default())
2022 {
2023 cur = e.rope.to_string();
2024 }
2025 t.assert_path_invariant("seq_earlier_step");
2026 }
2027 for _ in 0..6 {
2028 if let Some(e) =
2029 t.seq_later_step(ropey::Rope::from_str(&cur), (0, 0), MarkSnapshot::default())
2030 {
2031 cur = e.rope.to_string();
2032 }
2033 t.assert_path_invariant("seq_later_step");
2034 }
2035
2036 // A serialize round trip has to rebuild the flags from scratch.
2037 let round = UndoTree::from_serializable(&t.to_serializable()).expect("round trip");
2038 round.assert_path_invariant("from_serializable");
2039
2040 // Deepen, then prune: `cap` drops off-path leaves first and only then
2041 // promotes the root's on-path child, so both prune shapes run.
2042 for s in ["P", "Q", "S", "T"] {
2043 t.push(entry(s));
2044 }
2045 t.assert_path_invariant("pushes before cap");
2046 assert!(t.live_count() > 3, "cap has something to prune");
2047 t.cap(2);
2048 t.assert_path_invariant("cap");
2049
2050 assert!(t.pop_committed());
2051 t.assert_path_invariant("pop_committed");
2052
2053 t.push(entry("Z"));
2054 t.clear_redo();
2055 t.assert_path_invariant("clear_redo");
2056
2057 t.clear_all();
2058 t.assert_path_invariant("clear_all");
2059 }
2060}
2061
2062// ─── Phase 3a delta-storage tests ─────────────────────────────────────────────
2063//
2064// Correctness of the reversible delta and the warm/cold materialization is
2065// where text gets silently corrupted, so these lean hard on it: exact diff
2066// round-trips over random (incl. multi-byte) content, every node reconstructing
2067// identically warm and cold, and a random op stream cross-checked against a
2068// full-snapshot reference model kept alongside. All randomness is a deterministic
2069// xorshift seeded from a fixed constant — never `SystemTime`/entropy — so a
2070// failure reproduces exactly.
2071#[cfg(test)]
2072mod delta_tests {
2073 use super::*;
2074
2075 /// Iteration count for the randomized differential loops below, capped
2076 /// hard under miri.
2077 ///
2078 /// These loops are worth thousands of steps on a normal run: their value
2079 /// is statistical, shaking out logic bugs in the diff / keyframe code from
2080 /// a random op mix. That is a property of *executing* them, and miri
2081 /// interprets instead — six loops totalling ~22 300 iterations is the bulk
2082 /// of the weekly miri job's runtime, enough to push it past an hour.
2083 ///
2084 /// miri is there to catch UB, and UB shows up on the code *paths*, not on
2085 /// the thousandth repetition of one — so a short pass covers what miri can
2086 /// actually detect. Normal runs are untouched and keep the full count.
2087 fn stress_iters(n: usize) -> usize {
2088 if cfg!(miri) { n.min(50) } else { n }
2089 }
2090
2091 /// Deterministic xorshift64* PRNG, fixed-seeded so runs are reproducible.
2092 struct Rng(u64);
2093 impl Rng {
2094 fn new(seed: u64) -> Self {
2095 // xorshift needs a non-zero state.
2096 Self(if seed == 0 {
2097 0x9E37_79B9_7F4A_7C15
2098 } else {
2099 seed
2100 })
2101 }
2102 fn next_u64(&mut self) -> u64 {
2103 let mut x = self.0;
2104 x ^= x >> 12;
2105 x ^= x << 25;
2106 x ^= x >> 27;
2107 self.0 = x;
2108 x.wrapping_mul(0x2545_F491_4F6C_DD1D)
2109 }
2110 fn below(&mut self, n: usize) -> usize {
2111 (self.next_u64() % n as u64) as usize
2112 }
2113 }
2114
2115 /// A random char-granular mutation of `s`: insert, delete, or replace a
2116 /// span, drawing from an alphabet that mixes ASCII, accented, CJK, and
2117 /// emoji so multi-byte boundaries are exercised.
2118 fn mutate(s: &str, rng: &mut Rng) -> String {
2119 const ALPHABET: [char; 10] = ['a', 'b', '\n', 'é', '日', '本', '🎉', '語', 'x', 'z'];
2120 let chars: Vec<char> = s.chars().collect();
2121 let pick = |rng: &mut Rng| ALPHABET[rng.below(ALPHABET.len())];
2122 match rng.below(3) {
2123 0 => {
2124 let pos = rng.below(chars.len() + 1);
2125 let mut v = chars.clone();
2126 v.insert(pos, pick(rng));
2127 v.into_iter().collect()
2128 }
2129 1 if !chars.is_empty() => {
2130 let pos = rng.below(chars.len());
2131 let mut v = chars.clone();
2132 v.remove(pos);
2133 v.into_iter().collect()
2134 }
2135 _ => {
2136 if chars.is_empty() {
2137 return pick(rng).to_string();
2138 }
2139 let a = rng.below(chars.len());
2140 let b = (a + rng.below(chars.len() - a + 1)).min(chars.len());
2141 let mut v = chars[..a].to_vec();
2142 v.push(pick(rng));
2143 v.extend_from_slice(&chars[b..]);
2144 v.into_iter().collect()
2145 }
2146 }
2147 }
2148
2149 fn entry_str(s: &str) -> UndoEntry {
2150 UndoEntry {
2151 rope: ropey::Rope::from_str(s),
2152 cursor: (0, 0),
2153 timestamp: SystemTime::now(),
2154 marks: MarkSnapshot::default(),
2155 }
2156 }
2157
2158 // ── (0) differential oracle: the pre-chunk-walk `diff` ────────────────────
2159 //
2160 // The original implementation, verbatim, materializing BOTH ropes with
2161 // `to_string()` before scanning bytes. `diff` was rewritten to walk chunks
2162 // instead (no full materialization); this is the semantic pin — the two must
2163 // agree on the EXACT `Delta` for every input, not merely round-trip.
2164
2165 fn diff_reference(parent: &ropey::Rope, child: &ropey::Rope) -> Delta {
2166 let a = parent.to_string();
2167 let b = child.to_string();
2168 let ab = a.as_bytes();
2169 let bb = b.as_bytes();
2170
2171 let max_pre = ab.len().min(bb.len());
2172 let mut pre = 0;
2173 while pre < max_pre && ab[pre] == bb[pre] {
2174 pre += 1;
2175 }
2176 while pre > 0 && !a.is_char_boundary(pre) {
2177 pre -= 1;
2178 }
2179
2180 let max_suf = max_pre - pre;
2181 let mut suf = 0;
2182 while suf < max_suf && ab[ab.len() - 1 - suf] == bb[bb.len() - 1 - suf] {
2183 suf += 1;
2184 }
2185 let mut a_end = ab.len() - suf;
2186 while a_end < ab.len() && !a.is_char_boundary(a_end) {
2187 a_end += 1;
2188 }
2189 let b_end = bb.len() - (ab.len() - a_end);
2190
2191 Delta {
2192 start: a[..pre].chars().count(),
2193 old: a[pre..a_end].to_string(),
2194 new: b[pre..b_end].to_string(),
2195 }
2196 }
2197
2198 /// Assert the chunk-walking `diff` is byte-identical to `diff_reference`,
2199 /// over BOTH single-chunk ropes and multi-chunk ones (ropey only splits past
2200 /// its ~1 KiB leaf size, so short fixtures alone would never exercise the
2201 /// cross-chunk cursor logic).
2202 #[track_caller]
2203 fn assert_diff_matches_reference(sa: &str, sb: &str) {
2204 let a = ropey::Rope::from_str(sa);
2205 let b = ropey::Rope::from_str(sb);
2206 assert_eq!(
2207 diff(&a, &b),
2208 diff_reference(&a, &b),
2209 "diff != reference for {sa:?} -> {sb:?}"
2210 );
2211 // Same content, but built by insertion so the two ropes have DIFFERENT,
2212 // misaligned chunk layouts — the reference sees only bytes, the walker
2213 // sees chunk seams, and they must still agree.
2214 let mut a2 = ropey::Rope::new();
2215 a2.insert(0, sa);
2216 let mut b2 = ropey::Rope::new();
2217 for (i, c) in sb.chars().enumerate() {
2218 b2.insert_char(i, c);
2219 }
2220 assert_eq!(
2221 diff(&a2, &b2),
2222 diff_reference(&a2, &b2),
2223 "diff != reference (misaligned chunks) for {sa:?} -> {sb:?}"
2224 );
2225 }
2226
2227 #[test]
2228 // Deliberately NOT size-scaled for miri: the documents here are sized to span
2229 // several of ropey's ~1 KB leaf chunks, which is the entire property under
2230 // test, so shrinking them would quietly test something weaker. Running them
2231 // interpreted costs >10 min on its own. hjkl-buffer has no `unsafe`, so miri's
2232 // reach is UB in ropey/std on these code paths — already covered by the ~185
2233 // other tests in this crate that do run under it.
2234 #[cfg_attr(miri, ignore = "multi-chunk documents are too slow interpreted")]
2235 fn diff_matches_reference_on_edge_cases() {
2236 let cases: &[(&str, &str)] = &[
2237 // equal / empty
2238 ("", ""),
2239 ("", "a"),
2240 ("a", ""),
2241 ("abc", "abc"),
2242 ("café🎉", "café🎉"),
2243 // prefix-only / suffix-only change
2244 ("abcdef", "abcdefXY"),
2245 ("abcdefXY", "abcdef"),
2246 ("Xabcdef", "abcdef"),
2247 ("abcdef", "Xabcdef"),
2248 // change at position 0 and at the very end
2249 ("abcdef", "Zbcdef"),
2250 ("abcdef", "abcdeZ"),
2251 // overlapping repeats — prefix and suffix scans would collide
2252 ("abcabc", "abc"),
2253 ("abc", "abcabc"),
2254 ("aaaa", "aa"),
2255 ("aa", "aaaa"),
2256 ("abab", "ababab"),
2257 ("xyxyxy", "xyxy"),
2258 // multi-byte chars sitting exactly on the cut points
2259 ("café", "cafés"),
2260 ("cafés", "café"),
2261 ("café", "cafè"),
2262 ("日本語", "日語"),
2263 ("日本語", "日本本語"),
2264 ("🎉🎉🎉", "🎉🎉"),
2265 ("🎉🎉", "🎉🎉🎉"),
2266 ("🎉x🎉", "🎉y🎉"),
2267 ("a🎉b", "a🎊b"),
2268 ("é", "e"),
2269 ("e", "é"),
2270 ("🎉", ""),
2271 ("", "🎉"),
2272 // byte-level suffix match that is NOT a char boundary: the tails of
2273 // 'é' (0xC3 0xA9) and 'é' share no byte, but 日 (E6 97 A5) vs 旦
2274 // (E6 97 A6) share a two-byte prefix mid-codepoint.
2275 ("日", "旦"),
2276 ("x日y", "x旦y"),
2277 ("語", "誤"),
2278 // long enough to be multi-chunk in both ropes
2279 (
2280 &"the quick brown fox ".repeat(400),
2281 &"the quick brown fox ".repeat(400),
2282 ),
2283 ];
2284 for (sa, sb) in cases {
2285 assert_diff_matches_reference(sa, sb);
2286 }
2287
2288 // Multi-chunk with an edit in the middle / at each end.
2289 let big: String = "the quick brown fox jumps over the lazy dog\n".repeat(200);
2290 let mid = big.len() / 2;
2291 let mut edited = big.clone();
2292 edited.insert(mid, 'Z');
2293 assert_diff_matches_reference(&big, &edited);
2294 assert_diff_matches_reference(&edited, &big);
2295 assert_diff_matches_reference(&big, &format!("Z{big}"));
2296 assert_diff_matches_reference(&big, &format!("{big}Z"));
2297 assert_diff_matches_reference(&big, &big.repeat(2));
2298
2299 // Multi-chunk with multi-byte chars straddling likely leaf seams.
2300 let uni: String = "café 日本語 🎉 αβγ\n".repeat(200);
2301 let umid = uni.len() / 2;
2302 let umid = (0..=umid).rev().find(|i| uni.is_char_boundary(*i)).unwrap();
2303 let mut uedited = uni.clone();
2304 uedited.insert(umid, '🎊');
2305 assert_diff_matches_reference(&uni, &uedited);
2306 assert_diff_matches_reference(&uedited, &uni);
2307 }
2308
2309 #[test]
2310 fn diff_matches_reference_over_random_evolving_content() {
2311 let mut rng = Rng::new(0x0BAD_F00D_1234_5678);
2312 let mut s = String::from("seed café 日本語\n🎉");
2313 for _ in 0..stress_iters(4000) {
2314 let t = mutate(&s, &mut rng);
2315 let a = ropey::Rope::from_str(&s);
2316 let b = ropey::Rope::from_str(&t);
2317 assert_eq!(diff(&a, &b), diff_reference(&a, &b), "{s:?} -> {t:?}");
2318 assert_eq!(diff(&b, &a), diff_reference(&b, &a), "{t:?} -> {s:?}");
2319 s = t;
2320 }
2321 }
2322
2323 #[test]
2324 fn diff_matches_reference_on_shared_leaf_clones() {
2325 // The `Arc`-shared-leaf fast path: `child` is a CLONE of `parent` plus
2326 // one edit, so most chunks are pointer-identical. Exercised at several
2327 // edit positions across a multi-chunk rope, plus deletes and the
2328 // degenerate no-op clone.
2329 let base: String = "the quick brown fox jumps over the lazy dog\n".repeat(300);
2330 let parent = ropey::Rope::from_str(&base);
2331 assert_eq!(
2332 diff(&parent, &parent.clone()),
2333 diff_reference(&parent, &parent.clone())
2334 );
2335 let n = parent.len_chars();
2336 for at in [0, 1, n / 4, n / 2, n - 1, n] {
2337 let mut child = parent.clone();
2338 child.insert_char(at, '𝄞');
2339 assert_eq!(
2340 diff(&parent, &child),
2341 diff_reference(&parent, &child),
2342 "@{at}"
2343 );
2344 assert_eq!(
2345 diff(&child, &parent),
2346 diff_reference(&child, &parent),
2347 "@{at}"
2348 );
2349 }
2350 for at in [0, n / 3, n - 10] {
2351 let mut child = parent.clone();
2352 child.remove(at..at + 5);
2353 assert_eq!(
2354 diff(&parent, &child),
2355 diff_reference(&parent, &child),
2356 "-{at}"
2357 );
2358 assert_eq!(
2359 diff(&child, &parent),
2360 diff_reference(&child, &parent),
2361 "-{at}"
2362 );
2363 }
2364 }
2365
2366 #[test]
2367 // Same reasoning as `diff_matches_reference_on_edge_cases`: the 300-line base
2368 // document exists to force multi-chunk ropes, so it is not size-scaled and the
2369 // test is skipped under miri rather than weakened.
2370 #[cfg_attr(miri, ignore = "multi-chunk documents are too slow interpreted")]
2371 fn diff_matches_reference_over_random_multi_chunk_pairs() {
2372 // Random pairs built from a multi-chunk corpus, so chunk seams land in
2373 // arbitrary places relative to the common prefix/suffix.
2374 let mut rng = Rng::new(0xF00D_BEEF_0BAD_C0DE);
2375 let units = ["ab", "café ", "日本語", "🎉", "\n", "x", "語日", "é"];
2376 let build = |rng: &mut Rng| -> String {
2377 let mut s = String::new();
2378 for _ in 0..rng.below(400) {
2379 s.push_str(units[rng.below(units.len())]);
2380 }
2381 s
2382 };
2383 for _ in 0..stress_iters(300) {
2384 let sa = build(&mut rng);
2385 // Half the pairs share a long common prefix/suffix with `sa`.
2386 let sb = if rng.below(2) == 0 {
2387 build(&mut rng)
2388 } else {
2389 let mut t = sa.clone();
2390 if !t.is_empty() {
2391 let cut = rng.below(t.chars().count() + 1);
2392 let byte = t.char_indices().nth(cut).map_or(t.len(), |(i, _)| i);
2393 t.insert_str(byte, "🎊zz");
2394 }
2395 t
2396 };
2397 let a = ropey::Rope::from_str(&sa);
2398 let b = ropey::Rope::from_str(&sb);
2399 assert_eq!(diff(&a, &b), diff_reference(&a, &b));
2400 assert_eq!(diff(&b, &a), diff_reference(&b, &a));
2401 }
2402 }
2403
2404 // ── (i) delta round-trip: apply(diff(a,b))==b and apply_inverse==a ────────
2405
2406 #[test]
2407 fn diff_round_trips_over_random_evolving_content() {
2408 let mut rng = Rng::new(0x1234_5678_9ABC_DEF0);
2409 let mut s = String::from("seed café 日本語\n🎉");
2410 for _ in 0..stress_iters(4000) {
2411 let t = mutate(&s, &mut rng);
2412 let a = ropey::Rope::from_str(&s);
2413 let b = ropey::Rope::from_str(&t);
2414 let d = diff(&a, &b);
2415 assert_eq!(
2416 apply_forward(&a, &d).to_string(),
2417 t,
2418 "forward a->b failed (start={}, old={:?}, new={:?})",
2419 d.start,
2420 d.old,
2421 d.new
2422 );
2423 assert_eq!(
2424 apply_inverse(&b, &d).to_string(),
2425 s,
2426 "inverse b->a failed (start={}, old={:?}, new={:?})",
2427 d.start,
2428 d.old,
2429 d.new
2430 );
2431 s = t;
2432 }
2433 }
2434
2435 #[test]
2436 fn diff_round_trips_over_unrelated_pairs() {
2437 // Disjoint corpus pairs (not just single-edit neighbours) so the diff's
2438 // prefix/suffix logic is stressed on wholly different multi-byte text.
2439 let corpus = [
2440 "",
2441 "a",
2442 "café\n日本語\n",
2443 "🎉🎉🎉",
2444 "abcdef",
2445 "日本",
2446 "x\ny\nz\n",
2447 "aXb",
2448 "café",
2449 "語日本",
2450 "\n\n\n",
2451 "🎉x🎉y🎉",
2452 ];
2453 let mut rng = Rng::new(0xDEAD_BEEF_CAFE_1234);
2454 for _ in 0..stress_iters(3000) {
2455 let sa = corpus[rng.below(corpus.len())];
2456 let sb = corpus[rng.below(corpus.len())];
2457 let a = ropey::Rope::from_str(sa);
2458 let b = ropey::Rope::from_str(sb);
2459 let d = diff(&a, &b);
2460 assert_eq!(apply_forward(&a, &d).to_string(), sb);
2461 assert_eq!(apply_inverse(&b, &d).to_string(), sa);
2462 }
2463 }
2464
2465 // ── non-ASCII edit → undo → redo round-trip (multi-byte across a leave) ───
2466
2467 #[test]
2468 fn non_ascii_edit_undo_redo_round_trip() {
2469 // Edits land INSIDE multi-byte lines; undo/redo must round-trip the exact
2470 // bytes, proving the char-offset delta never splits a codepoint.
2471 let mut d = Driver::new("café\n日本語\n");
2472 d.edit("cafés\n日本語\n");
2473 d.edit("cafés\n日本語です\n");
2474 d.edit("cafés\n日本語です🎉\n");
2475 assert_eq!(d.undo().as_deref(), Some("cafés\n日本語です\n"));
2476 assert_eq!(d.undo().as_deref(), Some("cafés\n日本語\n"));
2477 assert_eq!(d.undo().as_deref(), Some("café\n日本語\n"));
2478 assert_eq!(d.redo().as_deref(), Some("cafés\n日本語\n"));
2479 assert_eq!(d.redo().as_deref(), Some("cafés\n日本語です\n"));
2480 assert_eq!(d.redo().as_deref(), Some("cafés\n日本語です🎉\n"));
2481 // Cold reconstruction of every node still matches (drop all caches).
2482 assert_warm_equals_cold(&mut d.t);
2483 }
2484
2485 // ── (ii) + (iii) random op stream vs a full-snapshot reference model ──────
2486
2487 #[test]
2488 fn tree_matches_full_snapshot_reference_over_random_ops() {
2489 let mut rng = Rng::new(0x9E37_79B9_7F4A_7C15);
2490 let start = "α\nβγ\n日本🎉\n";
2491 let mut real = UndoTree::new(ropey::Rope::from_str(start));
2492 let mut refr = RefTree::new(start);
2493 let mut live = start.to_string();
2494
2495 for step in 0..stress_iters(6000) {
2496 // Structural predicates stay in lockstep with the reference.
2497 assert_eq!(real.is_at_root(), refr.is_at_root(), "is_at_root @ {step}");
2498 assert_eq!(real.has_redo(), refr.has_redo(), "has_redo @ {step}");
2499 assert_eq!(real.depth_from_root(), refr.depth(), "depth @ {step}");
2500
2501 match rng.below(6) {
2502 0 | 1 => {
2503 // Edit: push the PRE-edit state (engine discipline), then
2504 // mutate the live buffer.
2505 let pre = live.clone();
2506 real.push(entry_str(&pre));
2507 refr.push(&pre);
2508 live = mutate(&live, &mut rng);
2509 }
2510 2 => {
2511 let got = real
2512 .undo_step(
2513 ropey::Rope::from_str(&live),
2514 (0, 0),
2515 MarkSnapshot::default(),
2516 )
2517 .map(|e| e.rope.to_string());
2518 let want = refr.undo_step(&live);
2519 assert_eq!(got, want, "undo @ {step}");
2520 if let Some(c) = got {
2521 live = c;
2522 }
2523 }
2524 3 => {
2525 let got = real
2526 .redo_step(
2527 ropey::Rope::from_str(&live),
2528 (0, 0),
2529 MarkSnapshot::default(),
2530 )
2531 .map(|e| e.rope.to_string());
2532 let want = refr.redo_step(&live);
2533 assert_eq!(got, want, "redo @ {step}");
2534 if let Some(c) = got {
2535 live = c;
2536 }
2537 }
2538 4 => {
2539 let got = real
2540 .seq_earlier_step(
2541 ropey::Rope::from_str(&live),
2542 (0, 0),
2543 MarkSnapshot::default(),
2544 )
2545 .map(|e| e.rope.to_string());
2546 let want = refr.seq_earlier_step(&live);
2547 assert_eq!(got, want, "g- @ {step}");
2548 if let Some(c) = got {
2549 live = c;
2550 }
2551 }
2552 _ => {
2553 let got = real
2554 .seq_later_step(
2555 ropey::Rope::from_str(&live),
2556 (0, 0),
2557 MarkSnapshot::default(),
2558 )
2559 .map(|e| e.rope.to_string());
2560 let want = refr.seq_later_step(&live);
2561 assert_eq!(got, want, "g+ @ {step}");
2562 if let Some(c) = got {
2563 live = c;
2564 }
2565 }
2566 }
2567
2568 // The reference model still rewrites the whole root→target chain on
2569 // every landing, so the two agreeing above already says the shortcut
2570 // reproduces it. Check the structure it relies on directly too: an
2571 // `on_path` drift is what would let the shortcut skip a stale
2572 // ancestor, and it is cheap to catch here at every step.
2573 real.assert_path_invariant(&format!("op @ {step}"));
2574
2575 // (ii) Every so often, assert warm and cold materialization agree
2576 // for every node — a cold-reconstructed node must equal the rope the
2577 // full-snapshot model would have held.
2578 if step % 200 == 0 {
2579 assert_warm_equals_cold(&mut real);
2580 }
2581 }
2582 assert_warm_equals_cold(&mut real);
2583 }
2584
2585 // ── (iv) keyframes: accelerated materialize vs the naive root replay ──────
2586 //
2587 // Keyframes (issue #302) pin a materialized rope every `KEYFRAME_INTERVAL`
2588 // nodes so a cold `g-` replays O(K) deltas instead of O(depth). They are a
2589 // CACHE: whatever they accelerate must be bit-identical to replaying every
2590 // delta from the root base with no cache at all. `materialize_naive` is that
2591 // oracle, in the same spirit as `diff_reference` above.
2592
2593 /// For every live node: the keyframe-accelerated `materialize` must equal the
2594 /// naive root-base replay exactly.
2595 #[track_caller]
2596 fn assert_materialize_matches_naive(t: &mut UndoTree) {
2597 for id in t.live_ids() {
2598 let naive = t.materialize_naive(id).to_string();
2599 let got = t.materialize_for_test(id).to_string();
2600 assert_eq!(got, naive, "accelerated != naive root replay for node {id}");
2601 }
2602 }
2603
2604 /// A linear history `n` states deep (so it crosses many keyframe intervals),
2605 /// plus the expected content of each state indexed by `seq`/depth. Every node
2606 /// is finalized, including the tip.
2607 fn deep_linear_history(n: usize) -> (UndoTree, Vec<String>) {
2608 // Under miri the document is shrunk 10x. `n` is deliberately NOT
2609 // touched: the keyframe ladder, the `n > 4 * KEYFRAME_INTERVAL`
2610 // assertion and every depth-related property stay exactly as they are
2611 // on a normal run — only the per-step rope volume drops. Without this
2612 // a single one of these tests ran for over 22 minutes under miri
2613 // (interpreted, not executed) and stalled the weekly job. The base
2614 // keeps its multi-line and multi-byte content, which is the part that
2615 // matters for the rope/delta paths.
2616 let reps = if cfg!(miri) { 2 } else { 20 };
2617 let base: String =
2618 "the quick brown fox\njumps over the lazy dog\ncafé 日本語 🎉\n".repeat(reps);
2619 let mut t = UndoTree::new(ropey::Rope::from_str(&base));
2620 let mut states = vec![base.clone()];
2621 let mut live = base;
2622 for i in 0..n {
2623 // Engine discipline: commit the PRE-edit state, then mutate.
2624 t.push(entry_str(&live));
2625 live = format!("e{i} {live}");
2626 states.push(live.clone());
2627 }
2628 // Stash the tip's live content so no node is left holding a stale edge.
2629 t.sync_current(ropey::Rope::from_str(&live));
2630 (t, states)
2631 }
2632
2633 #[test]
2634 fn deep_history_walks_back_and_forward_exactly() {
2635 // The `:earlier 9999` / `:later 9999` shape, deep enough that most jumps
2636 // land outside the warm window and go through a keyframe.
2637 // 65 under miri still satisfies the `> 4 * KEYFRAME_INTERVAL` floor
2638 // asserted below, so the walk still crosses four keyframes — the
2639 // property under test. See `deep_linear_history` for why.
2640 let n = if cfg!(miri) { 65 } else { 200 };
2641 assert!(n > 4 * KEYFRAME_INTERVAL);
2642 let (mut t, states) = deep_linear_history(n);
2643
2644 let mut live = states[n].clone();
2645 for want in (0..n).rev() {
2646 let got = t
2647 .seq_earlier_step(
2648 ropey::Rope::from_str(&live),
2649 (0, 0),
2650 MarkSnapshot::default(),
2651 )
2652 .expect("history is deeper than the walk");
2653 live = got.rope.to_string();
2654 assert_eq!(live, states[want], "g- onto seq {want}");
2655 }
2656 assert!(
2657 t.seq_earlier_step(
2658 ropey::Rope::from_str(&live),
2659 (0, 0),
2660 MarkSnapshot::default()
2661 )
2662 .is_none(),
2663 "walk ended at the oldest state"
2664 );
2665 for (seq, want) in states.iter().enumerate().skip(1) {
2666 let got = t
2667 .seq_later_step(
2668 ropey::Rope::from_str(&live),
2669 (0, 0),
2670 MarkSnapshot::default(),
2671 )
2672 .expect("history is deeper than the walk");
2673 live = got.rope.to_string();
2674 assert_eq!(&live, want, "g+ onto seq {seq}");
2675 }
2676 assert_materialize_matches_naive(&mut t);
2677 assert_warm_equals_cold(&mut t);
2678 }
2679
2680 /// `by_seq` must name exactly the live nodes, with the right ids. It is a
2681 /// second source of truth for `g-`/`g+` targets, so drift here silently
2682 /// sends history steps to the wrong state (or reports the end of history
2683 /// early) rather than failing loudly.
2684 ///
2685 /// Checked against a brute-force arena scan — the code `node_below` /
2686 /// `node_above` used before the index existed — after pushes, branch
2687 /// creation, undo/redo, pruning to a node budget, `clear_all`, and a
2688 /// serialize round trip, since those are the paths that allocate and free.
2689 #[test]
2690 fn seq_index_matches_the_arena() {
2691 fn brute(t: &UndoTree) -> std::collections::BTreeMap<u64, NodeId> {
2692 t.nodes
2693 .iter()
2694 .enumerate()
2695 .filter_map(|(id, slot)| slot.as_ref().map(|n| (n.seq, id)))
2696 .collect()
2697 }
2698 fn check(t: &UndoTree, when: &str) {
2699 assert_eq!(t.by_seq, brute(t), "seq index drifted after {when}");
2700 // Every step target agrees with a scan of the arena, which is what
2701 // the index replaced.
2702 for probe in 0..t.next_seq + 1 {
2703 let below = t
2704 .nodes
2705 .iter()
2706 .enumerate()
2707 .filter_map(|(id, s)| s.as_ref().map(|n| (n.seq, id)))
2708 .filter(|&(sq, _)| sq < probe)
2709 .max_by_key(|&(sq, _)| sq)
2710 .map(|(_, id)| id);
2711 let above = t
2712 .nodes
2713 .iter()
2714 .enumerate()
2715 .filter_map(|(id, s)| s.as_ref().map(|n| (n.seq, id)))
2716 .filter(|&(sq, _)| sq > probe)
2717 .min_by_key(|&(sq, _)| sq)
2718 .map(|(_, id)| id);
2719 assert_eq!(
2720 t.node_below(probe),
2721 below,
2722 "node_below({probe}) after {when}"
2723 );
2724 assert_eq!(
2725 t.node_above(probe),
2726 above,
2727 "node_above({probe}) after {when}"
2728 );
2729 }
2730 }
2731
2732 let mk = |text: &str| UndoEntry {
2733 rope: ropey::Rope::from_str(text),
2734 cursor: (0, 0),
2735 timestamp: SystemTime::now(),
2736 marks: MarkSnapshot::default(),
2737 };
2738
2739 let mut t = UndoTree::new(ropey::Rope::from_str("s0"));
2740 check(&t, "new");
2741
2742 for i in 1..=8 {
2743 t.push(mk(&format!("s{i}")));
2744 }
2745 check(&t, "pushes");
2746
2747 // Undo twice then push: forks a branch, frees nothing.
2748 t.undo_step(ropey::Rope::from_str("s8"), (0, 0), MarkSnapshot::default());
2749 t.undo_step(ropey::Rope::from_str("s7"), (0, 0), MarkSnapshot::default());
2750 t.push(mk("branch"));
2751 check(&t, "branch");
2752
2753 // Enforcing a node budget frees through `free` / `free_subtree`.
2754 t.cap(3);
2755 check(&t, "cap");
2756
2757 let round = UndoTree::from_serializable(&t.to_serializable()).expect("round trip");
2758 check(&round, "from_serializable");
2759
2760 t.clear_all();
2761 check(&t, "clear_all");
2762 }
2763
2764 #[test]
2765 fn keyframes_bound_the_cold_replay_distance() {
2766 // 65 still spans four keyframe intervals, which is what makes the
2767 // per-node replay-distance bound below meaningful. See
2768 // `deep_linear_history` for why miri gets a smaller history.
2769 let n = if cfg!(miri) { 65 } else { 200 };
2770 let (mut t, _) = deep_linear_history(n);
2771 // Steady state: the ordinary warm entries have aged out, the keyframes
2772 // are still pinned. Every node must be within one interval of an anchor.
2773 t.drop_warm_caches();
2774 for id in t.live_ids() {
2775 let d = t.replay_distance(id);
2776 assert!(
2777 d < KEYFRAME_INTERVAL,
2778 "node {id} (depth {}) replays {d} deltas, over the keyframe bound",
2779 t.get(id).depth
2780 );
2781 }
2782 // Drop the keyframes too and the bound is gone — proof that it is the
2783 // keyframes doing the bounding and not the warm LRU or the tree shape.
2784 let deepest = *t
2785 .live_ids()
2786 .iter()
2787 .max_by_key(|&&id| t.get(id).depth)
2788 .unwrap();
2789 t.drop_all_caches();
2790 assert!(t.replay_distance(deepest) > KEYFRAME_INTERVAL);
2791 // One materialize off the fully-cold tree re-pins the whole ladder.
2792 t.materialize_for_test(deepest);
2793 t.drop_warm_caches();
2794 for id in t.live_ids() {
2795 assert!(t.replay_distance(id) < KEYFRAME_INTERVAL, "node {id}");
2796 }
2797 }
2798
2799 #[test]
2800 fn keyframe_materialize_matches_naive_over_random_ops() {
2801 // Push-heavy op mix so the tree gets deep enough to cross many keyframe
2802 // intervals, with undo/redo/g-/g+ and periodic `cap` pruning mixed in —
2803 // pruning renumbers nothing but does free nodes and re-root the tree, so
2804 // it is where a stale keyframe would surface as corrupted text.
2805 let mut rng = Rng::new(0x0FF1_CE00_D15E_A5E5);
2806 let start = "α\nβγ\n日本🎉\nthe quick brown fox\n";
2807 let mut t = UndoTree::new(ropey::Rope::from_str(start));
2808 let mut live = start.to_string();
2809
2810 for step in 0..stress_iters(5000) {
2811 match rng.below(10) {
2812 0..=5 => {
2813 let pre = live.clone();
2814 t.push(entry_str(&pre));
2815 live = mutate(&live, &mut rng);
2816 }
2817 6 => {
2818 if let Some(e) = t.undo_step(
2819 ropey::Rope::from_str(&live),
2820 (0, 0),
2821 MarkSnapshot::default(),
2822 ) {
2823 live = e.rope.to_string();
2824 }
2825 }
2826 7 => {
2827 if let Some(e) = t.redo_step(
2828 ropey::Rope::from_str(&live),
2829 (0, 0),
2830 MarkSnapshot::default(),
2831 ) {
2832 live = e.rope.to_string();
2833 }
2834 }
2835 8 => {
2836 if let Some(e) = t.seq_earlier_step(
2837 ropey::Rope::from_str(&live),
2838 (0, 0),
2839 MarkSnapshot::default(),
2840 ) {
2841 live = e.rope.to_string();
2842 }
2843 }
2844 _ => {
2845 if let Some(e) = t.seq_later_step(
2846 ropey::Rope::from_str(&live),
2847 (0, 0),
2848 MarkSnapshot::default(),
2849 ) {
2850 live = e.rope.to_string();
2851 }
2852 }
2853 }
2854 // Whatever the tree hands back must be what the naive replay of the
2855 // node it landed on says — checked every step, cheaply.
2856 let cur = t.current;
2857 assert_eq!(
2858 t.materialize_for_test(cur).to_string(),
2859 t.materialize_naive(cur).to_string(),
2860 "current node diverged @ {step}"
2861 );
2862 t.assert_path_invariant(&format!("op @ {step}"));
2863 if step % 250 == 0 {
2864 assert_materialize_matches_naive(&mut t);
2865 }
2866 if step % 700 == 0 {
2867 t.cap(60);
2868 t.assert_path_invariant(&format!("cap @ {step}"));
2869 }
2870 }
2871 assert_materialize_matches_naive(&mut t);
2872 assert_warm_equals_cold(&mut t);
2873 }
2874
2875 #[test]
2876 fn deserialized_deep_tree_rebuilds_the_keyframe_ladder() {
2877 // Depth is NOT serialized (keyframes are a cache, the on-disk format is
2878 // untouched), so a loaded tree has to recompute it — otherwise every
2879 // cross-session `g-` would be a full replay again.
2880 // 65 still spans four keyframe intervals, so the reloaded tree must
2881 // still rebuild a real ladder rather than a trivial one. See
2882 // `deep_linear_history` for why miri gets a smaller history.
2883 let n = if cfg!(miri) { 65 } else { 100 };
2884 let (t, states) = deep_linear_history(n);
2885 let ser = t.to_serializable();
2886 let mut back = UndoTree::from_serializable(&ser).expect("valid projection");
2887
2888 let deepest = *back
2889 .live_ids()
2890 .iter()
2891 .max_by_key(|&&id| back.get(id).depth)
2892 .unwrap();
2893 assert_eq!(back.get(deepest).depth, n, "depths recomputed on load");
2894 assert_eq!(back.materialize_for_test(deepest).to_string(), states[n]);
2895 assert_materialize_matches_naive(&mut back);
2896
2897 back.drop_warm_caches();
2898 for id in back.live_ids() {
2899 assert!(back.replay_distance(id) < KEYFRAME_INTERVAL, "node {id}");
2900 }
2901 }
2902
2903 /// For every live node: materialize warm, drop all caches, materialize cold,
2904 /// assert identical. Restores nothing else (test-local).
2905 fn assert_warm_equals_cold(t: &mut UndoTree) {
2906 let ids = t.live_ids();
2907 let warm: Vec<String> = ids
2908 .iter()
2909 .map(|&id| t.materialize_for_test(id).to_string())
2910 .collect();
2911 t.drop_all_caches();
2912 for (i, &id) in ids.iter().enumerate() {
2913 let cold = t.materialize_for_test(id).to_string();
2914 assert_eq!(cold, warm[i], "warm != cold for node {id}");
2915 }
2916 }
2917
2918 /// Engine-faithful driver over the real (delta) [`UndoTree`]: mirrors how
2919 /// `editor.rs` pushes the PRE-edit state and restores returned content.
2920 struct Driver {
2921 t: UndoTree,
2922 live: String,
2923 }
2924 impl Driver {
2925 fn new(s: &str) -> Self {
2926 Self {
2927 t: UndoTree::new(ropey::Rope::from_str(s)),
2928 live: s.to_string(),
2929 }
2930 }
2931 fn edit(&mut self, new: &str) {
2932 self.t.push(entry_str(&self.live));
2933 self.live = new.to_string();
2934 }
2935 fn undo(&mut self) -> Option<String> {
2936 let e = self.t.undo_step(
2937 ropey::Rope::from_str(&self.live),
2938 (0, 0),
2939 MarkSnapshot::default(),
2940 )?;
2941 self.live = e.rope.to_string();
2942 Some(self.live.clone())
2943 }
2944 fn redo(&mut self) -> Option<String> {
2945 let e = self.t.redo_step(
2946 ropey::Rope::from_str(&self.live),
2947 (0, 0),
2948 MarkSnapshot::default(),
2949 )?;
2950 self.live = e.rope.to_string();
2951 Some(self.live.clone())
2952 }
2953 }
2954
2955 /// Full-snapshot reference tree — Phase 2b's model (a whole rope per node),
2956 /// the oracle the delta tree is cross-checked against. Content only (cursor /
2957 /// marks / timestamps are covered by the existing tree tests).
2958 struct RefNode {
2959 parent: Option<usize>,
2960 children: Vec<usize>,
2961 last_child: Option<usize>,
2962 content: String,
2963 seq: u64,
2964 }
2965 struct RefTree {
2966 nodes: Vec<Option<RefNode>>,
2967 current: usize,
2968 next_seq: u64,
2969 }
2970 impl RefTree {
2971 fn new(s: &str) -> Self {
2972 let root = RefNode {
2973 parent: None,
2974 children: Vec::new(),
2975 last_child: None,
2976 content: s.to_string(),
2977 seq: 0,
2978 };
2979 Self {
2980 nodes: vec![Some(root)],
2981 current: 0,
2982 next_seq: 1,
2983 }
2984 }
2985 fn get(&self, id: usize) -> &RefNode {
2986 self.nodes[id].as_ref().unwrap()
2987 }
2988 fn get_mut(&mut self, id: usize) -> &mut RefNode {
2989 self.nodes[id].as_mut().unwrap()
2990 }
2991 fn alloc(&mut self, n: RefNode) -> usize {
2992 self.nodes.push(Some(n));
2993 self.nodes.len() - 1
2994 }
2995 fn is_at_root(&self) -> bool {
2996 self.get(self.current).parent.is_none()
2997 }
2998 fn has_redo(&self) -> bool {
2999 self.get(self.current).last_child.is_some()
3000 }
3001 fn depth(&self) -> usize {
3002 let mut d = 0;
3003 let mut n = self.get(self.current).parent;
3004 while let Some(p) = n {
3005 d += 1;
3006 n = self.get(p).parent;
3007 }
3008 d
3009 }
3010 fn push(&mut self, pre: &str) {
3011 let cur = self.current;
3012 self.get_mut(cur).content = pre.to_string();
3013 let seq = self.next_seq;
3014 self.next_seq += 1;
3015 let child = self.alloc(RefNode {
3016 parent: Some(cur),
3017 children: Vec::new(),
3018 last_child: None,
3019 content: pre.to_string(),
3020 seq,
3021 });
3022 let c = self.get_mut(cur);
3023 c.children.push(child);
3024 c.last_child = Some(child);
3025 self.current = child;
3026 }
3027 fn undo_step(&mut self, live: &str) -> Option<String> {
3028 let cur = self.current;
3029 let par = self.get(cur).parent?;
3030 self.get_mut(cur).content = live.to_string();
3031 self.get_mut(par).last_child = Some(cur);
3032 self.current = par;
3033 Some(self.get(par).content.clone())
3034 }
3035 fn redo_step(&mut self, live: &str) -> Option<String> {
3036 let cur = self.current;
3037 let child = self.get(cur).last_child?;
3038 self.get_mut(cur).content = live.to_string();
3039 self.current = child;
3040 Some(self.get(child).content.clone())
3041 }
3042 fn current_seq(&self) -> u64 {
3043 self.get(self.current).seq
3044 }
3045 fn node_below(&self, s: u64) -> Option<usize> {
3046 let mut best: Option<(u64, usize)> = None;
3047 for (id, slot) in self.nodes.iter().enumerate() {
3048 if let Some(n) = slot
3049 && n.seq < s
3050 && best.is_none_or(|(bs, _)| n.seq > bs)
3051 {
3052 best = Some((n.seq, id));
3053 }
3054 }
3055 best.map(|(_, id)| id)
3056 }
3057 fn node_above(&self, s: u64) -> Option<usize> {
3058 let mut best: Option<(u64, usize)> = None;
3059 for (id, slot) in self.nodes.iter().enumerate() {
3060 if let Some(n) = slot
3061 && n.seq > s
3062 && best.is_none_or(|(bs, _)| n.seq < bs)
3063 {
3064 best = Some((n.seq, id));
3065 }
3066 }
3067 best.map(|(_, id)| id)
3068 }
3069 fn retarget(&mut self, target: usize) {
3070 self.current = target;
3071 let mut node = target;
3072 while let Some(p) = self.get(node).parent {
3073 self.get_mut(p).last_child = Some(node);
3074 node = p;
3075 }
3076 }
3077 fn stash_and_move(&mut self, target: usize, live: &str) {
3078 let cur = self.current;
3079 self.get_mut(cur).content = live.to_string();
3080 self.retarget(target);
3081 }
3082 fn seq_earlier_step(&mut self, live: &str) -> Option<String> {
3083 let target = self.node_below(self.current_seq())?;
3084 self.stash_and_move(target, live);
3085 Some(self.get(target).content.clone())
3086 }
3087 fn seq_later_step(&mut self, live: &str) -> Option<String> {
3088 let target = self.node_above(self.current_seq())?;
3089 self.stash_and_move(target, live);
3090 Some(self.get(target).content.clone())
3091 }
3092 }
3093}
3094
3095// ─── Phase 3b serialize/deserialize tests ─────────────────────────────────────
3096//
3097// The undofile is only as trustworthy as this round-trip: a projection that
3098// loses a branch, mislinks a parent, or reconstructs a node's content wrong
3099// would silently corrupt cross-session undo. These build the headline tree
3100// (5 edits, u, u), project it, rebuild, and assert BOTH the per-node content
3101// (keyed by the stable `seq`) and the live walk (`<C-r>` forward, `u` back)
3102// survive the trip.
3103#[cfg(test)]
3104mod serialize_tests {
3105 use super::*;
3106
3107 fn e(text: &str) -> UndoEntry {
3108 UndoEntry {
3109 rope: ropey::Rope::from_str(text),
3110 cursor: (0, 0),
3111 timestamp: SystemTime::now(),
3112 marks: MarkSnapshot::default(),
3113 }
3114 }
3115 fn l(text: &str) -> (ropey::Rope, (usize, usize), MarkSnapshot) {
3116 (ropey::Rope::from_str(text), (0, 0), MarkSnapshot::default())
3117 }
3118
3119 /// The headline tree: root "s0", five edits to live "s5", then `u` twice so
3120 /// `current` sits on "s3" with the forward branch (s4/s5) retained — exactly
3121 /// the state a `:wq` would persist.
3122 fn headline_tree() -> UndoTree {
3123 let mut t = UndoTree::new(ropey::Rope::from_str("s0"));
3124 for pre in ["s0", "s1", "s2", "s3", "s4"] {
3125 t.push(e(pre)); // engine discipline: push the PRE-edit live state
3126 }
3127 let (r, c, m) = l("s5");
3128 t.undo_step(r, c, m); // -> s4
3129 let (r, c, m) = l("s4");
3130 t.undo_step(r, c, m); // -> s3
3131 t.sync_current(ropey::Rope::from_str("s3")); // stash exact live, like save
3132 t
3133 }
3134
3135 /// Every node's content (keyed by `seq`), materialized cold-then-warm.
3136 fn content_by_seq(t: &mut UndoTree) -> std::collections::BTreeMap<u64, String> {
3137 t.live_ids()
3138 .into_iter()
3139 .map(|id| {
3140 let seq = t.get(id).seq;
3141 (seq, t.materialize_for_test(id).to_string())
3142 })
3143 .collect()
3144 }
3145
3146 #[test]
3147 fn round_trip_reproduces_structure_and_content() {
3148 let mut orig = headline_tree();
3149 let cur_seq = orig.current_node_seq();
3150 let ser = orig.to_serializable();
3151 let orig_content = content_by_seq(&mut orig);
3152
3153 let mut back = UndoTree::from_serializable(&ser).expect("valid projection");
3154 assert_eq!(back.current_node_seq(), cur_seq, "current preserved");
3155 assert_eq!(back.next_seq, orig.next_seq, "next_seq preserved");
3156 // Force cold reconstruction (fresh tree has no warm caches) and compare.
3157 assert_eq!(
3158 content_by_seq(&mut back),
3159 orig_content,
3160 "content at every node reproduced"
3161 );
3162 // Six states: s0..s5.
3163 assert_eq!(orig_content.len(), 6);
3164 assert_eq!(orig_content[&3], "s3");
3165 assert_eq!(orig_content[&5], "s5");
3166 }
3167
3168 #[test]
3169 fn deserialized_tree_walks_forward_and_back() {
3170 let ser = headline_tree().to_serializable();
3171 let mut t = UndoTree::from_serializable(&ser).unwrap();
3172 // `<C-r>` twice: s3 -> s4 -> s5 (the retained forward branch).
3173 let (r, c, m) = l("s3");
3174 assert_eq!(t.redo_step(r, c, m).unwrap().rope.to_string(), "s4");
3175 let (r, c, m) = l("s4");
3176 assert_eq!(t.redo_step(r, c, m).unwrap().rope.to_string(), "s5");
3177 // `u` all the way back to the root.
3178 let mut live = "s5".to_string();
3179 for want in ["s4", "s3", "s2", "s1", "s0"] {
3180 let (r, c, m) = l(&live);
3181 assert_eq!(t.undo_step(r, c, m).unwrap().rope.to_string(), want);
3182 live = want.to_string();
3183 }
3184 assert!(t.is_at_root());
3185 }
3186
3187 #[test]
3188 fn from_serializable_rejects_out_of_range_current() {
3189 let mut ser = headline_tree().to_serializable();
3190 ser.current = ser.nodes.len() as u32; // past the end
3191 assert!(UndoTree::from_serializable(&ser).is_none());
3192 }
3193
3194 /// A tree without a root base is structurally invalid outside the swap
3195 /// context (where the body supplies the base). `to_serializable` always
3196 /// emits `Some`; a `None` can only come from the swap writer's dedup, which
3197 /// the swap reader reverses before rebuilding.
3198 #[test]
3199 fn from_serializable_rejects_missing_base() {
3200 let mut ser = headline_tree().to_serializable();
3201 ser.base = None;
3202 assert!(UndoTree::from_serializable(&ser).is_none());
3203 }
3204
3205 #[test]
3206 fn from_serializable_rejects_non_root_missing_delta() {
3207 let mut ser = headline_tree().to_serializable();
3208 // Blank a non-root node's delta ⇒ structurally invalid ⇒ rejected.
3209 let victim = if ser.root == 0 { 1 } else { 0 };
3210 ser.nodes[victim].delta = None;
3211 assert!(UndoTree::from_serializable(&ser).is_none());
3212 }
3213
3214 /// A parent-link cycle must be rejected at load, not walked at use.
3215 /// `materialize` follows `parent` in an unbounded loop, so a cycle there
3216 /// hangs while growing `path` — this is the only place that can see it.
3217 #[test]
3218 fn from_serializable_rejects_a_parent_link_cycle() {
3219 let mut ser = headline_tree().to_serializable();
3220 assert!(ser.nodes.len() > 2, "fixture needs three nodes");
3221 // Point two non-root nodes at each other, both ways, so the links stay
3222 // mutually consistent and every node stays reachable from the root
3223 // through the list it was already in. Only the single-lister rule
3224 // rejects this.
3225 let (a, b) = match ser.root {
3226 0 => (1, 2),
3227 1 => (0, 2),
3228 _ => (0, 1),
3229 };
3230 ser.nodes[a].parent = Some(b as u32);
3231 ser.nodes[b].parent = Some(a as u32);
3232 ser.nodes[a].children.push(b as u32);
3233 ser.nodes[b].children.push(a as u32);
3234 assert!(UndoTree::from_serializable(&ser).is_none());
3235 }
3236
3237 /// A parent link that the named parent does not mirror as a child leaves
3238 /// the two walks (`children` forward, `parent` back) disagreeing.
3239 #[test]
3240 fn from_serializable_rejects_an_unmirrored_parent_link() {
3241 let mut ser = headline_tree().to_serializable();
3242 let victim = if ser.root == 0 { 1 } else { 0 };
3243 let parent = ser.nodes[victim].parent.expect("non-root") as usize;
3244 ser.nodes[parent].children.retain(|&c| c != victim as u32);
3245 assert!(UndoTree::from_serializable(&ser).is_none());
3246 }
3247
3248 /// A cycle that hangs off no root at all: nodes 1 and 2 name each other
3249 /// both ways, so the lists still partition — reachability from the root is
3250 /// the guard that catches this one. This is the shape that hung
3251 /// `install_recovered_undo_tree` before the loader checked for it.
3252 #[test]
3253 fn from_serializable_rejects_a_cycle_unreachable_from_the_root() {
3254 let d = || {
3255 Some(Delta {
3256 start: 0,
3257 old: String::new(),
3258 new: String::from("x"),
3259 })
3260 };
3261 let n = |parent, children, delta, seq| SerNode {
3262 parent,
3263 children,
3264 last_child: None,
3265 delta,
3266 cursor: (0, 0),
3267 timestamp_unix_ms: 0,
3268 marks: MarkSnapshot::default(),
3269 seq,
3270 };
3271 let ser = SerTree {
3272 base: Some("hello".into()),
3273 nodes: vec![
3274 n(None, vec![], None, 0),
3275 n(Some(2), vec![2], d(), 1),
3276 n(Some(1), vec![1], d(), 2),
3277 ],
3278 root: 0,
3279 current: 1,
3280 next_seq: 3,
3281 };
3282 assert!(UndoTree::from_serializable(&ser).is_none());
3283 }
3284
3285 /// `by_seq` is keyed by `seq`, so a repeat would silently drop a node from
3286 /// the `g-` / `g+` index while leaving it in the arena.
3287 #[test]
3288 fn from_serializable_rejects_a_repeated_seq() {
3289 let mut ser = headline_tree().to_serializable();
3290 let victim = if ser.root == 0 { 1 } else { 0 };
3291 let other = if victim == 0 { 1 } else { 0 };
3292 ser.nodes[victim].seq = ser.nodes[other].seq;
3293 assert!(UndoTree::from_serializable(&ser).is_none());
3294 }
3295
3296 #[test]
3297 fn multibyte_content_survives_round_trip() {
3298 let mut t = UndoTree::new(ropey::Rope::from_str("café\n日本語"));
3299 t.push(e("café\n日本語"));
3300 t.push(e("cafés\n日本語"));
3301 t.sync_current(ropey::Rope::from_str("cafés\n日本語です🎉"));
3302 let want = content_by_seq(&mut t);
3303 let ser = t.to_serializable();
3304 let mut back = UndoTree::from_serializable(&ser).unwrap();
3305 assert_eq!(content_by_seq(&mut back), want);
3306 }
3307}