Skip to main content

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    /// Ids on the root→`current` path (inclusive), which pruning must never
1089    /// touch. Small (one per undo level), so a `Vec` membership check is fine.
1090    fn current_path(&self) -> Vec<NodeId> {
1091        let mut path = Vec::new();
1092        let mut n = Some(self.current);
1093        while let Some(id) = n {
1094            path.push(id);
1095            n = self.get(id).parent;
1096        }
1097        path
1098    }
1099
1100    /// Lowest-`seq` leaf that is not on the root→`current` path, if any.
1101    fn lowest_offpath_leaf(&self) -> Option<NodeId> {
1102        let path = self.current_path();
1103        let mut best: Option<(u64, NodeId)> = None;
1104        for (id, slot) in self.nodes.iter().enumerate() {
1105            if let Some(n) = slot
1106                && n.children.is_empty()
1107                && !path.contains(&id)
1108                && best.is_none_or(|(bs, _)| n.seq < bs)
1109            {
1110                best = Some((n.seq, id));
1111            }
1112        }
1113        best.map(|(_, id)| id)
1114    }
1115
1116    /// Unlink `leaf` from its parent and free it (leaf ⇒ no subtree to recurse).
1117    fn detach_leaf(&mut self, leaf: NodeId) {
1118        if let Some(par) = self.get(leaf).parent {
1119            let par_node = self.get_mut(par);
1120            par_node.children.retain(|&c| c != leaf);
1121            if par_node.last_child == Some(leaf) {
1122                par_node.last_child = par_node.children.last().copied();
1123            }
1124        }
1125        self.free(leaf);
1126    }
1127
1128    /// Promote the root's on-path child to the new root and free the old root.
1129    /// Returns `false` when the root is `current` (nothing left to trim).
1130    fn prune_root_side(&mut self) -> bool {
1131        let root = self.root;
1132        if root == self.current {
1133            return false;
1134        }
1135        // The child on the path to `current` (the root always has one here).
1136        let path = self.current_path();
1137        let Some(&child) = self.get(root).children.iter().find(|c| path.contains(c)) else {
1138            return false;
1139        };
1140        // Any OTHER root children are off-path branches; drop them with the root.
1141        let others: Vec<NodeId> = self
1142            .get(root)
1143            .children
1144            .iter()
1145            .copied()
1146            .filter(|&c| c != child)
1147            .collect();
1148        for c in others {
1149            self.free_subtree(c);
1150        }
1151        // The promoted child becomes the new root: materialize it (while the old
1152        // root still anchors the chain) into a full base rope, then drop its
1153        // now-meaningless parent edge. This keeps every delta below it valid.
1154        let base = self.materialize(child);
1155        {
1156            let node = self.get_mut(child);
1157            node.parent = None;
1158            node.base = Some(base);
1159            node.delta = None;
1160            node.rope_cache = None;
1161        }
1162        self.warm.retain(|&n| n != child);
1163        self.keyframes.retain(|&n| n != child);
1164        // `child` was already on the path (it is the on-path child) and is now
1165        // its root end; the old root drops off it as it is freed.
1166        self.get_mut(root).on_path = false;
1167        self.root = child;
1168        self.free(root);
1169        true
1170    }
1171
1172    /// `redo_stack.clear()` — drop `current`'s forward branch.
1173    pub(crate) fn clear_redo(&mut self) {
1174        let cur = self.current;
1175        let kids = std::mem::take(&mut self.get_mut(cur).children);
1176        self.get_mut(cur).last_child = None;
1177        for c in kids {
1178            self.free_subtree(c);
1179        }
1180    }
1181
1182    /// `undo_stack.clear(); redo_stack.clear()` — collapse to a single root ==
1183    /// current node, preserving the live state. Frees every other node.
1184    pub(crate) fn clear_all(&mut self) {
1185        let cur = self.current;
1186        // The survivor becomes a self-contained root: give it a full base rope
1187        // (materialized while the chain is still intact) so it needs no parent.
1188        let base = self.materialize(cur);
1189        for id in 0..self.nodes.len() {
1190            if id != cur && self.nodes[id].is_some() {
1191                self.nodes[id] = None;
1192                self.free.push(id);
1193            }
1194        }
1195        self.warm.clear();
1196        self.keyframes.clear();
1197        // Bulk free bypassed `free`, so rebuild the index around the survivor.
1198        self.by_seq.clear();
1199        self.by_seq.insert(self.get(cur).seq, cur);
1200        let node = self.get_mut(cur);
1201        node.parent = None;
1202        node.children.clear();
1203        node.last_child = None;
1204        node.delta = None;
1205        node.base = Some(base);
1206        node.rope_cache = None;
1207        // The survivor is the new root: restart the depth numbering under it so
1208        // its descendants land on the keyframe ladder from 0 again.
1209        node.depth = 0;
1210        // Sole survivor ⇒ root == current ⇒ it is the whole path.
1211        node.on_path = true;
1212        self.root = cur;
1213    }
1214}
1215
1216// ─── Serializable projection (Phase 3b) ───────────────────────────────────────
1217//
1218// The undofile persists the tree as a compact, self-consistent projection: the
1219// root's full base text (String) plus, per node, its edge `delta` and links.
1220// `rope_cache`/`warm` are runtime-only and dropped — every node reconstructs
1221// from the root base + deltas, so the round-trip reproduces identical content
1222// at every node. NodeIds are DENSE in the projection (the live-slab holes are
1223// compacted away and links remapped), so `from_serializable` rebuilds a fresh
1224// arena 1:1 with no free list.
1225
1226/// One node of the serialized undo tree. Mirrors [`UndoNode`] minus the
1227/// runtime-only materialization cache; ids are dense indices into
1228/// [`SerTree::nodes`].
1229#[derive(Debug, Clone, Serialize, Deserialize)]
1230pub struct SerNode {
1231    /// Parent index, `None` only for the root.
1232    pub parent: Option<u32>,
1233    /// Child indices (order preserved; `> 1` ⇒ branch point).
1234    pub children: Vec<u32>,
1235    /// `<C-r>` target child index.
1236    pub last_child: Option<u32>,
1237    /// Reversible edge delta from the parent, `None` only for the root.
1238    pub delta: Option<Delta>,
1239    /// Post-state cursor `(row, col)`.
1240    pub cursor: (u32, u32),
1241    /// Wall-clock creation time, ms since the UNIX epoch.
1242    pub timestamp_unix_ms: u64,
1243    /// Marks / jumplist / changelist snapshot.
1244    pub marks: MarkSnapshot,
1245    /// Global monotonic change number.
1246    pub seq: u64,
1247}
1248
1249/// Serializable projection of an [`UndoTree`] for the undofile. Postcard-encoded
1250/// (non-self-describing, so a schema/version drift surfaces as a parse `Err`
1251/// that the reader discards). See [`UndoTree::to_serializable`] /
1252/// [`UndoTree::from_serializable`].
1253#[derive(Debug, Clone, Serialize, Deserialize)]
1254pub struct SerTree {
1255    /// Root base text (the anchor the delta chain replays from).
1256    pub base: String,
1257    /// Dense node arena (no holes).
1258    pub nodes: Vec<SerNode>,
1259    /// Root index into `nodes`.
1260    pub root: u32,
1261    /// Current (live) index into `nodes`.
1262    pub current: u32,
1263    /// Next `seq` to assign.
1264    pub next_seq: u64,
1265}
1266
1267/// [`SystemTime`] → ms since the UNIX epoch (saturating, pre-epoch ⇒ 0).
1268fn system_time_to_unix_ms(t: SystemTime) -> u64 {
1269    t.duration_since(UNIX_EPOCH)
1270        .map_or(0, |d| d.as_millis() as u64)
1271}
1272
1273/// ms since the UNIX epoch → [`SystemTime`].
1274fn unix_ms_to_system_time(ms: u64) -> SystemTime {
1275    UNIX_EPOCH + Duration::from_millis(ms)
1276}
1277
1278impl UndoTree {
1279    /// `seq` of the current (live) node — the header's `current_seq` for the
1280    /// undofile (the just-saved content per the §6 invariant).
1281    pub(crate) fn current_node_seq(&self) -> u64 {
1282        self.get(self.current).seq
1283    }
1284
1285    /// Materialize the current (live) node's content. Used by the swap
1286    /// recovery consistency guard (docs §6c) to check a deserialized tree
1287    /// agrees with the freshly-recovered buffer text before it's installed.
1288    pub(crate) fn current_content(&mut self) -> ropey::Rope {
1289        let cur = self.current;
1290        self.materialize(cur)
1291    }
1292
1293    /// Stash `rope` into the current node as the live buffer state, preserving
1294    /// that node's own cursor/timestamp/marks. Called just before serializing so
1295    /// the on-disk tree's `current` edge is exact even when `current` is a fresh
1296    /// (still-stale) leaf — the in-session self-heal (first undo/edit stashes
1297    /// live) applied eagerly at save time.
1298    pub(crate) fn sync_current(&mut self, rope: ropey::Rope) {
1299        let cur = self.current;
1300        let (cursor, ts, marks) = {
1301            let n = self.get(cur);
1302            (n.cursor, n.timestamp, n.marks.clone())
1303        };
1304        self.set_node_state(cur, rope, cursor, ts, marks);
1305    }
1306
1307    /// Project the live tree into a serializable, dense form (holes compacted,
1308    /// links remapped). `rope_cache`/`warm` are dropped; the root's `base`
1309    /// carries the anchor text and every non-root node its edge `delta`.
1310    pub(crate) fn to_serializable(&self) -> SerTree {
1311        // Dense remap: old NodeId → new index, in slab order.
1312        let mut map: Vec<Option<u32>> = vec![None; self.nodes.len()];
1313        let mut order: Vec<NodeId> = Vec::new();
1314        for (id, slot) in self.nodes.iter().enumerate() {
1315            if slot.is_some() {
1316                map[id] = Some(order.len() as u32);
1317                order.push(id);
1318            }
1319        }
1320        let remap = |id: NodeId| map[id].expect("live link points at a live node");
1321        let nodes = order
1322            .iter()
1323            .map(|&id| {
1324                let n = self.get(id);
1325                SerNode {
1326                    parent: n.parent.map(remap),
1327                    children: n.children.iter().map(|&c| remap(c)).collect(),
1328                    last_child: n.last_child.map(remap),
1329                    delta: n.delta.clone(),
1330                    cursor: (n.cursor.0 as u32, n.cursor.1 as u32),
1331                    timestamp_unix_ms: system_time_to_unix_ms(n.timestamp),
1332                    marks: (*n.marks).clone(),
1333                    seq: n.seq,
1334                }
1335            })
1336            .collect();
1337        let base = self
1338            .get(self.root)
1339            .base
1340            .as_ref()
1341            .map(|r| r.to_string())
1342            .unwrap_or_default();
1343        SerTree {
1344            base,
1345            nodes,
1346            root: remap(self.root),
1347            current: remap(self.current),
1348            next_seq: self.next_seq,
1349        }
1350    }
1351
1352    /// Rebuild an arena tree from a projection. Returns `None` on any structural
1353    /// inconsistency (out-of-range link, a non-root node missing its delta, a
1354    /// root carrying one, `children` lists that do not partition the non-root
1355    /// nodes, a child disagreeing with the node that listed it, a node
1356    /// unreachable from the root, a repeated `seq`) so a corrupt-but-parseable
1357    /// file degrades to a fresh tree rather than a broken one. The root's
1358    /// content comes from `base`; the current node's is materialized on demand
1359    /// from base + deltas.
1360    ///
1361    /// The partition + reachability pair is what makes the parent links a tree,
1362    /// and that is load-bearing rather than tidiness: [`Self::materialize`] and
1363    /// [`Self::retarget_current`] follow `parent` in unbounded loops, so a
1364    /// parent-link cycle is a hang (or an unbounded `path`) rather than a
1365    /// degraded tree. Rejecting it here is the only place that can see it.
1366    pub(crate) fn from_serializable(s: &SerTree) -> Option<Self> {
1367        let len = s.nodes.len();
1368        if len == 0 || s.root as usize >= len || s.current as usize >= len {
1369            return None;
1370        }
1371        // Validate links and the root/non-root delta discipline up front.
1372        let mut seqs = std::collections::BTreeSet::new();
1373        for (i, n) in s.nodes.iter().enumerate() {
1374            let is_root = i as u32 == s.root;
1375            match (is_root, &n.delta, &n.parent) {
1376                (true, None, None) => {}
1377                (false, Some(_), Some(_)) => {}
1378                _ => return None,
1379            }
1380            if let Some(p) = n.parent
1381                && p as usize >= len
1382            {
1383                return None;
1384            }
1385            if n.children.iter().any(|&c| c as usize >= len) {
1386                return None;
1387            }
1388            if let Some(c) = n.last_child
1389                && c as usize >= len
1390            {
1391                return None;
1392            }
1393            // `by_seq` is keyed by `seq`, so a repeat would drop a node from the
1394            // `g-` / `g+` index without dropping it from the arena.
1395            if !seqs.insert(n.seq) {
1396                return None;
1397            }
1398        }
1399        // The `children` lists must PARTITION the non-root nodes — each listed
1400        // once, by nobody for the root — and every child's own `parent` must
1401        // name the node that listed it. Mutual agreement alone is not enough:
1402        // two nodes can name each other as parent AND as child while both stay
1403        // reachable from the root through the lists they were originally in.
1404        // Requiring a single lister is what turns the two directions into one
1405        // tree. Runs after the range loop so the indices below are in bounds.
1406        let mut listed_by: Vec<Option<u32>> = vec![None; len];
1407        for (i, n) in s.nodes.iter().enumerate() {
1408            for &c in &n.children {
1409                if c == s.root || listed_by[c as usize].is_some() {
1410                    return None;
1411                }
1412                listed_by[c as usize] = Some(i as u32);
1413            }
1414        }
1415        if s.nodes
1416            .iter()
1417            .zip(listed_by.iter())
1418            .any(|(n, &lister)| n.parent != lister)
1419        {
1420            return None;
1421        }
1422        let base = ropey::Rope::from_str(&s.base);
1423        let (depths, reachable) = depths_from_root(s);
1424        // With the lists partitioning the nodes, reachability from the root is
1425        // what rules out a cycle: a node inside one is reachable from no root.
1426        if reachable.iter().any(|&r| !r) {
1427            return None;
1428        }
1429        let mut nodes: Vec<Option<UndoNode>> = s
1430            .nodes
1431            .iter()
1432            .enumerate()
1433            .map(|(i, n)| {
1434                let is_root = i as u32 == s.root;
1435                Some(UndoNode {
1436                    parent: n.parent.map(|p| p as NodeId),
1437                    children: n.children.iter().map(|&c| c as NodeId).collect(),
1438                    last_child: n.last_child.map(|c| c as NodeId),
1439                    delta: n.delta.clone(),
1440                    base: if is_root { Some(base.clone()) } else { None },
1441                    rope_cache: None,
1442                    depth: depths[i],
1443                    cursor: (n.cursor.0 as usize, n.cursor.1 as usize),
1444                    timestamp: unix_ms_to_system_time(n.timestamp_unix_ms),
1445                    marks: Arc::new(n.marks.clone()),
1446                    seq: n.seq,
1447                    // Set below, once the whole arena exists to walk.
1448                    on_path: false,
1449                })
1450            })
1451            .collect();
1452        // Establish the root→`current` path invariant on the loaded tree: flag
1453        // the chain, and make each ancestor name its on-path child. A projection
1454        // written by `to_serializable` already agrees (it came from a tree
1455        // holding the invariant), so this is a no-op on any file we produced —
1456        // doing it unconditionally is what stops a hand-edited or truncated one
1457        // from loading into a tree whose `<C-r>` direction contradicts its own
1458        // `current`, which `retarget_current` would no longer repair on the way
1459        // past. The validation above already rules out a parent-link cycle, so
1460        // the `len` bound is belt-and-braces on the one walk that runs before
1461        // any invariant of the rebuilt tree holds.
1462        let mut node = s.current as NodeId;
1463        for _ in 0..len {
1464            let n = nodes[node].as_mut().expect("the projection is dense");
1465            n.on_path = true;
1466            let Some(p) = n.parent else { break };
1467            nodes[p]
1468                .as_mut()
1469                .expect("the projection is dense")
1470                .last_child = Some(node);
1471            node = p;
1472        }
1473        let by_seq = nodes
1474            .iter()
1475            .enumerate()
1476            .filter_map(|(id, slot)| slot.as_ref().map(|n| (n.seq, id)))
1477            .collect();
1478        Some(Self {
1479            nodes,
1480            free: Vec::new(),
1481            warm: Vec::new(),
1482            keyframes: Vec::new(),
1483            by_seq,
1484            root: s.root as NodeId,
1485            current: s.current as NodeId,
1486            next_seq: s.next_seq,
1487        })
1488    }
1489}
1490
1491/// Depth-from-root of every node in a projection, by BFS over `children`, plus
1492/// the reachable set the walk visited.
1493///
1494/// Depth is NOT part of the on-disk format — it is derivable, and the undofile
1495/// deliberately stores only what is not (issue #302: keyframes are an in-memory
1496/// cache, so nothing about them enters `SerTree`). The `seen` guard makes this
1497/// terminate on a malformed file whose links form a cycle; anything unreachable
1498/// from the root keeps depth 0. `from_serializable` rejects a projection with
1499/// any unreachable node, so the returned depths are only ever used on a tree
1500/// where every one of them was computed by the walk.
1501fn depths_from_root(s: &SerTree) -> (Vec<usize>, Vec<bool>) {
1502    let mut depths = vec![0usize; s.nodes.len()];
1503    let mut seen = vec![false; s.nodes.len()];
1504    let mut queue = std::collections::VecDeque::new();
1505    seen[s.root as usize] = true;
1506    queue.push_back(s.root as usize);
1507    while let Some(i) = queue.pop_front() {
1508        for &c in &s.nodes[i].children {
1509            let c = c as usize;
1510            if !seen[c] {
1511                seen[c] = true;
1512                depths[c] = depths[i] + 1;
1513                queue.push_back(c);
1514            }
1515        }
1516    }
1517    (depths, seen)
1518}
1519
1520#[cfg(test)]
1521impl UndoTree {
1522    /// Ids of every live node, for warm-vs-cold materialization checks.
1523    fn live_ids(&self) -> Vec<NodeId> {
1524        (0..self.nodes.len())
1525            .filter(|&i| self.nodes[i].is_some())
1526            .collect()
1527    }
1528
1529    /// Materialize `id` for a test (public wrapper over the private method).
1530    fn materialize_for_test(&mut self, id: NodeId) -> ropey::Rope {
1531        self.materialize(id)
1532    }
1533
1534    /// Evict every cache INCLUDING the pinned keyframes (root keeps its `base`),
1535    /// forcing the next materialization of any node to reconstruct purely from
1536    /// deltas off the root — the strongest cold path there is.
1537    fn drop_all_caches(&mut self) {
1538        for n in self.nodes.iter_mut().flatten() {
1539            n.rope_cache = None;
1540        }
1541        self.warm.clear();
1542        self.keyframes.clear();
1543    }
1544
1545    /// Evict only the ordinary warm LRU, leaving the pinned keyframes — the
1546    /// steady state a deep history walk actually runs in.
1547    fn drop_warm_caches(&mut self) {
1548        for id in std::mem::take(&mut self.warm) {
1549            if let Some(n) = self.nodes[id].as_mut() {
1550                n.rope_cache = None;
1551            }
1552        }
1553    }
1554
1555    /// How many forward delta applies `materialize(id)` would perform right now
1556    /// (0 when `id` already holds content). This is the cost keyframes exist to
1557    /// bound, made assertable.
1558    fn replay_distance(&self, id: NodeId) -> usize {
1559        let mut n = 0;
1560        let mut cur = id;
1561        loop {
1562            let node = self.get(cur);
1563            if node.rope_cache.is_some() || node.base.is_some() {
1564                return n;
1565            }
1566            n += 1;
1567            match node.parent {
1568                Some(p) => cur = p,
1569                None => return n,
1570            }
1571        }
1572    }
1573
1574    /// The root→`current` path, by brute force: walk `parent` links up from
1575    /// `current` and reverse. Deliberately consults NEITHER `on_path` nor
1576    /// `last_child`, so it is an independent oracle for both.
1577    fn brute_force_path(&self) -> Vec<NodeId> {
1578        let mut walk = Vec::new();
1579        let mut n = Some(self.current);
1580        while let Some(id) = n {
1581            walk.push(id);
1582            n = self.get(id).parent;
1583        }
1584        walk.reverse();
1585        walk
1586    }
1587
1588    /// The two halves of the path invariant `retarget_current` now relies on,
1589    /// checked against [`Self::brute_force_path`]:
1590    ///
1591    /// 1. `on_path` is set on EXACTLY the nodes of the root→`current` walk.
1592    /// 2. every node on that walk except the tip names its successor as
1593    ///    `last_child` — the observable property (`<C-r>` retraces the branch
1594    ///    landed on) that the old full-chain rewrite established directly.
1595    ///
1596    /// (1) is the maintenance; (2) is what the maintenance buys. Checking only
1597    /// (2) would pass on a tree whose flags had drifted but whose links happened
1598    /// to be right; checking only (1) would pass on a tree that had stopped
1599    /// linking. Both must hold after every operation that moves `current`,
1600    /// allocates, or frees.
1601    #[track_caller]
1602    fn assert_path_invariant(&self, when: &str) {
1603        let walk = self.brute_force_path();
1604        assert_eq!(
1605            walk.first().copied(),
1606            Some(self.root),
1607            "the root→current walk does not start at the root after {when}"
1608        );
1609        let mut want = walk.clone();
1610        want.sort_unstable();
1611        let flagged: Vec<NodeId> = (0..self.nodes.len())
1612            .filter(|&i| self.nodes[i].as_ref().is_some_and(|n| n.on_path))
1613            .collect();
1614        assert_eq!(
1615            flagged, want,
1616            "on_path flags name {flagged:?}, the root→current walk is {want:?}, after {when}"
1617        );
1618        for w in walk.windows(2) {
1619            assert_eq!(
1620                self.get(w[0]).last_child,
1621                Some(w[1]),
1622                "node {} last_child is {:?}, not its on-path child {}, after {when}",
1623                w[0],
1624                self.get(w[0]).last_child,
1625                w[1]
1626            );
1627        }
1628    }
1629
1630    /// Reconstruct `id`'s content the naive way: walk to the root and replay
1631    /// every forward delta off the root `base`, consulting NO cache and NO
1632    /// keyframe. The differential oracle for keyframe-accelerated
1633    /// [`Self::materialize`] — the two must agree exactly, always.
1634    fn materialize_naive(&self, id: NodeId) -> ropey::Rope {
1635        let mut path = vec![id];
1636        let mut cur = id;
1637        while let Some(p) = self.get(cur).parent {
1638            path.push(p);
1639            cur = p;
1640        }
1641        let mut rope = self
1642            .get(cur)
1643            .base
1644            .clone()
1645            .expect("the root always carries a base");
1646        // Skip the root itself (it has no edge delta); replay root-ward → target.
1647        for &node in path.iter().rev().skip(1) {
1648            let d = self
1649                .get(node)
1650                .delta
1651                .as_ref()
1652                .expect("a non-root node always carries its edge delta");
1653            rope = apply_forward(&rope, d);
1654        }
1655        rope
1656    }
1657}
1658
1659#[cfg(test)]
1660mod tree_tests {
1661    use super::*;
1662
1663    fn entry(text: &str) -> UndoEntry {
1664        UndoEntry {
1665            rope: ropey::Rope::from_str(text),
1666            cursor: (0, 0),
1667            timestamp: SystemTime::now(),
1668            marks: MarkSnapshot::default(),
1669        }
1670    }
1671
1672    fn live(text: &str) -> (ropey::Rope, (usize, usize), MarkSnapshot) {
1673        (ropey::Rope::from_str(text), (0, 0), MarkSnapshot::default())
1674    }
1675
1676    #[test]
1677    fn fresh_tree_is_root_current_empty() {
1678        let t = UndoTree::new(ropey::Rope::from_str("hello"));
1679        assert!(t.is_at_root());
1680        assert!(!t.has_redo());
1681        assert_eq!(t.depth_from_root(), 0);
1682        assert_eq!(t.root, t.current);
1683    }
1684
1685    #[test]
1686    fn push_links_child_and_advances_current() {
1687        let mut t = UndoTree::new(ropey::Rope::from_str("hello"));
1688        let root = t.current;
1689        t.push(entry("hello"));
1690        // root now parents current; current is a fresh leaf.
1691        assert_eq!(t.get(t.current).parent, Some(root));
1692        assert_eq!(t.get(root).last_child, Some(t.current));
1693        assert_eq!(t.get(root).children, vec![t.current]);
1694        assert_eq!(t.depth_from_root(), 1);
1695        assert!(!t.has_redo());
1696        assert!(!t.is_at_root());
1697    }
1698
1699    #[test]
1700    fn undo_then_redo_round_trips_links() {
1701        let mut t = UndoTree::new(ropey::Rope::from_str("s0"));
1702        t.push(entry("s0")); // commit s0, current = n1 (live s1)
1703        let n0 = t.root;
1704        let n1 = t.current;
1705        // undo: current -> n0, restores s0.
1706        let (r, c, m) = live("s1");
1707        let restored = t.undo_step(r, c, m).unwrap();
1708        assert_eq!(restored.rope.to_string(), "s0");
1709        assert_eq!(t.current, n0);
1710        assert!(t.has_redo());
1711        assert_eq!(t.get(n0).last_child, Some(n1));
1712        // redo: current -> n1, restores what we left (s1).
1713        let (r, c, m) = live("s0");
1714        let restored = t.redo_step(r, c, m).unwrap();
1715        assert_eq!(restored.rope.to_string(), "s1");
1716        assert_eq!(t.current, n1);
1717        assert!(!t.has_redo());
1718    }
1719
1720    #[test]
1721    fn undo_at_root_and_redo_at_leaf_are_noops() {
1722        let mut t = UndoTree::new(ropey::Rope::from_str("x"));
1723        let (r, c, m) = live("x");
1724        assert!(t.undo_step(r, c, m).is_none());
1725        let (r, c, m) = live("x");
1726        assert!(t.redo_step(r, c, m).is_none());
1727        assert_eq!(t.depth_from_root(), 0);
1728    }
1729
1730    #[test]
1731    fn push_retains_forward_branch() {
1732        // Phase 2b: an edit after an undo forks a new branch; the old forward
1733        // branch is NOT dropped and remains reachable by seq.
1734        let mut t = UndoTree::new(ropey::Rope::from_str("s0"));
1735        t.push(entry("A")); // root -> nA (seq1, "A")
1736        let root = t.root;
1737        let na = t.current;
1738        let (r, c, m) = live("A");
1739        t.undo_step(r, c, m); // back to root, nA is the redo child
1740        assert!(t.has_redo());
1741        // A new edit from the root forks a SECOND child (nB, seq2).
1742        t.push(entry("B"));
1743        let nb = t.current;
1744        assert_ne!(nb, na);
1745        // Both branches live: root now has two children.
1746        assert_eq!(t.get(root).children.len(), 2);
1747        assert!(t.get(root).children.contains(&na));
1748        assert!(t.get(root).children.contains(&nb));
1749        // `<C-r>` follows the freshest branch (nB).
1750        assert_eq!(t.get(root).last_child, Some(nb));
1751        // Four live nodes: root + nA + nB + (nB is current/leaf). No leak of nA.
1752        let live = t.nodes.iter().filter(|n| n.is_some()).count();
1753        assert_eq!(live, 3);
1754    }
1755
1756    #[test]
1757    fn seq_walk_crosses_branches() {
1758        // Mirror nvim `iA<Esc>uiB<Esc>` then g-/g+ (buffer starts empty "").
1759        // `push(entry)` writes `entry` into the node being LEFT (its true
1760        // pre-edit content); the fresh leaf holds the live post-edit state only
1761        // once it is stashed on the way past — exactly the engine's discipline.
1762        let mut t = UndoTree::new(ropey::Rope::from_str(""));
1763        t.push(entry("")); // leave root("") -> nA(seq1), live "A"
1764        let (r, c, m) = live("A");
1765        t.undo_step(r, c, m); // stash "A" into nA, back to root("")
1766        t.push(entry("")); // leave root("") -> nB(seq2), branch, live "B"
1767        let nb = t.current;
1768        // At B (seq2). g- -> greatest seq below 2 = seq1 = "A".
1769        let (r, c, m) = live("B");
1770        let a = t.seq_earlier_step(r, c, m).unwrap();
1771        assert_eq!(a.rope.to_string(), "A");
1772        // g- again -> root "".
1773        let (r, c, m) = live("A");
1774        let root_snap = t.seq_earlier_step(r, c, m).unwrap();
1775        assert_eq!(root_snap.rope.to_string(), "");
1776        // g+ -> back up to seq1 "A".
1777        let (r, c, m) = live("");
1778        let a2 = t.seq_later_step(r, c, m).unwrap();
1779        assert_eq!(a2.rope.to_string(), "A");
1780        // g+ -> seq2 "B" (crosses to the other branch).
1781        let (r, c, m) = live("A");
1782        let b = t.seq_later_step(r, c, m).unwrap();
1783        assert_eq!(b.rope.to_string(), "B");
1784        assert_eq!(t.current, nb);
1785        // At the tip: no higher seq.
1786        let (r, c, m) = live("B");
1787        assert!(t.seq_later_step(r, c, m).is_none());
1788    }
1789
1790    #[test]
1791    fn seq_walk_updates_retrace_path() {
1792        // Land on a deep leaf via g-, then u/u and <C-r>/<C-r> must retrace it
1793        // (nvim `iX<Esc>iY<Esc>uiZ<Esc>g-uu<C-r><C-r>`). State labels: root "R".
1794        let mut t = UndoTree::new(ropey::Rope::from_str("R"));
1795        t.push(entry("R")); // leave root("R") -> nX(seq1), live "X"
1796        t.push(entry("X")); // leave nX("X") -> nY(seq2), live "Y"
1797        let (r, c, m) = live("Y");
1798        t.undo_step(r, c, m); // stash "Y" into nY, back to nX("X")
1799        t.push(entry("X")); // leave nX("X") -> nZ(seq3), branch, live "Z"
1800        // g- from Z(seq3) -> nY(seq2) "Y".
1801        let (r, c, m) = live("Z");
1802        let y = t.seq_earlier_step(r, c, m).unwrap();
1803        assert_eq!(y.rope.to_string(), "Y");
1804        // u,u back to root.
1805        let (r, c, m) = live("Y");
1806        t.undo_step(r, c, m);
1807        let (r, c, m) = live("X");
1808        t.undo_step(r, c, m);
1809        assert!(t.is_at_root());
1810        // <C-r>,<C-r> retraces the branch we landed on: root->X->Y.
1811        let (r, c, m) = live("R");
1812        let x = t.redo_step(r, c, m).unwrap();
1813        assert_eq!(x.rope.to_string(), "X");
1814        let (r, c, m) = live("X");
1815        let y2 = t.redo_step(r, c, m).unwrap();
1816        assert_eq!(y2.rope.to_string(), "Y");
1817    }
1818
1819    #[test]
1820    fn leaves_lists_branch_tips_by_seq() {
1821        // root -> nX -> nY -> nW (leaf, seq3, depth3) and nX -> nZ (leaf, seq4,
1822        // depth2). Mirrors nvim `iX iY iW uu iZ`.
1823        let mut t = UndoTree::new(ropey::Rope::from_str(""));
1824        t.push(entry("X"));
1825        t.push(entry("Y"));
1826        t.push(entry("W"));
1827        let (r, c, m) = live("W");
1828        t.undo_step(r, c, m);
1829        let (r, c, m) = live("Y");
1830        t.undo_step(r, c, m); // back to nX
1831        t.push(entry("Z")); // nX -> nZ(seq4)
1832        let leaves = t.leaves();
1833        // Two leaves: W(seq3, depth3) and Z(seq4, depth2). Z is current.
1834        let dims: Vec<(u64, usize, bool)> =
1835            leaves.iter().map(|&(s, d, _, cur)| (s, d, cur)).collect();
1836        assert_eq!(dims, vec![(3, 3, false), (4, 2, true)]);
1837    }
1838
1839    #[test]
1840    fn cap_prunes_oldest_from_root_side() {
1841        let mut t = UndoTree::new(ropey::Rope::from_str("s"));
1842        for _ in 0..5 {
1843            t.push(entry("s"));
1844        }
1845        assert_eq!(t.depth_from_root(), 5);
1846        t.cap(3);
1847        assert_eq!(t.depth_from_root(), 3);
1848        // Redo side untouched (there is none), current unchanged.
1849        assert!(!t.has_redo());
1850        // Two oldest slots were reclaimed.
1851        assert_eq!(t.free.len(), 2);
1852    }
1853
1854    #[test]
1855    fn cap_drops_offpath_leaf_before_main_line() {
1856        // Fork two abandoned branches off the root, then extend the main line,
1857        // and cap: the lowest-seq OFF-PATH leaf must go first, and `current`
1858        // plus its ancestors must survive.
1859        let mut t = UndoTree::new(ropey::Rope::from_str(""));
1860        t.push(entry("A")); // root -> nA(seq1) [abandoned branch tip]
1861        let na = t.current;
1862        let (r, c, m) = live("A");
1863        t.undo_step(r, c, m);
1864        t.push(entry("B")); // root -> nB(seq2) [abandoned branch tip]
1865        let nb = t.current;
1866        let (r, c, m) = live("B");
1867        t.undo_step(r, c, m);
1868        t.push(entry("C")); // root -> nC(seq3), the live main line
1869        let nc = t.current;
1870        // 4 live nodes (root, nA, nB, nC) => 3 states. Cap to 2.
1871        assert_eq!(t.leaves().len(), 3);
1872        t.cap(2);
1873        // The lowest-seq off-path leaf (nA, seq1) was dropped; current (nC) and
1874        // its ancestor (root) survive, and the newer off-path leaf nB survives.
1875        assert!(t.nodes[na].is_none());
1876        assert!(t.nodes[nb].is_some());
1877        assert_eq!(t.current, nc);
1878        assert!(!t.is_at_root());
1879        assert!(t.get(t.root).children.contains(&nb));
1880        assert!(t.get(t.root).children.contains(&nc));
1881    }
1882
1883    #[test]
1884    fn pop_committed_reverses_last_push() {
1885        let mut t = UndoTree::new(ropey::Rope::from_str("s0"));
1886        t.push(entry("s0")); // depth 1, current = fresh leaf
1887        assert_eq!(t.depth_from_root(), 1);
1888        assert!(t.pop_committed());
1889        // The just-pushed leaf is gone; current stepped back to the root.
1890        assert_eq!(t.depth_from_root(), 0);
1891        assert!(t.is_at_root());
1892        assert_eq!(t.free.len(), 1);
1893        // Seq reclaimed so the next push is gapless.
1894        assert_eq!(t.next_seq, 1);
1895    }
1896
1897    #[test]
1898    fn pop_committed_retains_sibling_branches() {
1899        // Fork a branch, then a no-op push at the fork must pop cleanly without
1900        // orphaning the sibling branch.
1901        let mut t = UndoTree::new(ropey::Rope::from_str(""));
1902        t.push(entry("A")); // root -> nA(seq1)
1903        let na = t.current;
1904        let (r, c, m) = live("A");
1905        t.undo_step(r, c, m); // back to root
1906        t.push(entry("B")); // root -> nB(seq2); root children [nA, nB]
1907        let root = t.root;
1908        // A spurious no-op push at nB, then pop it.
1909        assert!(t.pop_committed());
1910        // nB is gone, current back at root; nA branch still intact & reachable.
1911        assert!(t.get(root).children.contains(&na));
1912        assert_eq!(t.get(root).children.len(), 1);
1913        assert_eq!(t.current, root);
1914        let live = t.nodes.iter().filter(|n| n.is_some()).count();
1915        assert_eq!(live, 2); // root + nA
1916    }
1917
1918    #[test]
1919    fn pop_committed_at_root_is_false() {
1920        let mut t = UndoTree::new(ropey::Rope::from_str("s"));
1921        assert!(!t.pop_committed());
1922    }
1923
1924    #[test]
1925    fn clear_redo_drops_forward_only() {
1926        let mut t = UndoTree::new(ropey::Rope::from_str("s0"));
1927        t.push(entry("s0"));
1928        let (r, c, m) = live("s1");
1929        t.undo_step(r, c, m);
1930        assert!(t.has_redo());
1931        assert_eq!(t.depth_from_root(), 0);
1932        t.clear_redo();
1933        assert!(!t.has_redo());
1934        assert_eq!(t.depth_from_root(), 0);
1935    }
1936
1937    #[test]
1938    fn clear_all_collapses_to_single_node() {
1939        let mut t = UndoTree::new(ropey::Rope::from_str("s"));
1940        for _ in 0..3 {
1941            t.push(entry("s"));
1942        }
1943        t.clear_all();
1944        assert!(t.is_at_root());
1945        assert!(!t.has_redo());
1946        assert_eq!(t.depth_from_root(), 0);
1947        assert_eq!(t.root, t.current);
1948    }
1949
1950    /// The depth measures where `current` sits, not how many nodes exist: an
1951    /// undo walks it back down while the branch it came from stays live, and a
1952    /// redo climbs the same steps again.
1953    #[test]
1954    fn depth_from_root_follows_current_not_tree_size() {
1955        let mut t = UndoTree::new(ropey::Rope::from_str("s0"));
1956        t.push(entry("s0"));
1957        t.push(entry("s1"));
1958        t.push(entry("s2"));
1959        assert_eq!(t.depth_from_root(), 3);
1960
1961        let (r, c, m) = live("s3");
1962        assert!(t.undo_step(r, c, m).is_some());
1963        let (r, c, m) = live("s2");
1964        assert!(t.undo_step(r, c, m).is_some());
1965        assert_eq!(t.depth_from_root(), 1);
1966        // Nothing was pruned — the three pushed nodes are still reachable.
1967        assert!(t.has_redo());
1968        assert_eq!(t.live_count(), 4);
1969
1970        let (r, c, m) = live("s1");
1971        assert!(t.redo_step(r, c, m).is_some());
1972        assert_eq!(t.depth_from_root(), 2);
1973    }
1974
1975    /// `on_path` and the ancestors' `last_child` chain must agree with a
1976    /// brute-force root→`current` walk after EVERY operation that moves
1977    /// `current`, allocates, or frees — that invariant is the only thing making
1978    /// `retarget_current`'s fork-point shortcut sound, and a drift in it is
1979    /// silent (a later `<C-r>` restores another branch's text, no error).
1980    ///
1981    /// One tree walked through every such site in turn: `new`, `push`,
1982    /// `undo_step`, `redo_step`, `seq_earlier_step`/`seq_later_step` (i.e.
1983    /// `retarget_current`, including a landing that crosses to a sibling
1984    /// branch), `clear_redo`, `cap` (both the off-path-leaf and the
1985    /// root-promotion prune), `pop_committed`, `from_serializable` and
1986    /// `clear_all`.
1987    #[test]
1988    fn path_flags_track_the_root_to_current_walk() {
1989        let mut t = UndoTree::new(ropey::Rope::from_str("R"));
1990        t.assert_path_invariant("new");
1991
1992        // Main line: R -> A -> B -> C.
1993        for s in ["R", "A", "B"] {
1994            t.push(entry(s));
1995            t.assert_path_invariant("push");
1996        }
1997        // Back down two, forking a sibling branch off the middle.
1998        let (r, c, m) = live("C");
1999        assert!(t.undo_step(r, c, m).is_some());
2000        t.assert_path_invariant("undo_step");
2001        let (r, c, m) = live("B");
2002        assert!(t.undo_step(r, c, m).is_some());
2003        t.assert_path_invariant("undo_step");
2004        let (r, c, m) = live("A");
2005        assert!(t.redo_step(r, c, m).is_some());
2006        t.assert_path_invariant("redo_step");
2007        let (r, c, m) = live("B");
2008        assert!(t.undo_step(r, c, m).is_some());
2009        t.push(entry("A")); // A -> X, a second branch under A
2010        t.assert_path_invariant("push forking a branch");
2011
2012        // `g-` / `g+` walk the whole tree by seq, so they land on nodes in the
2013        // OTHER branch — the case that actually exercises the fork point rather
2014        // than a parent/child step.
2015        let mut cur = String::from("X");
2016        for _ in 0..6 {
2017            if let Some(e) =
2018                t.seq_earlier_step(ropey::Rope::from_str(&cur), (0, 0), MarkSnapshot::default())
2019            {
2020                cur = e.rope.to_string();
2021            }
2022            t.assert_path_invariant("seq_earlier_step");
2023        }
2024        for _ in 0..6 {
2025            if let Some(e) =
2026                t.seq_later_step(ropey::Rope::from_str(&cur), (0, 0), MarkSnapshot::default())
2027            {
2028                cur = e.rope.to_string();
2029            }
2030            t.assert_path_invariant("seq_later_step");
2031        }
2032
2033        // A serialize round trip has to rebuild the flags from scratch.
2034        let round = UndoTree::from_serializable(&t.to_serializable()).expect("round trip");
2035        round.assert_path_invariant("from_serializable");
2036
2037        // Deepen, then prune: `cap` drops off-path leaves first and only then
2038        // promotes the root's on-path child, so both prune shapes run.
2039        for s in ["P", "Q", "S", "T"] {
2040            t.push(entry(s));
2041        }
2042        t.assert_path_invariant("pushes before cap");
2043        assert!(t.live_count() > 3, "cap has something to prune");
2044        t.cap(2);
2045        t.assert_path_invariant("cap");
2046
2047        assert!(t.pop_committed());
2048        t.assert_path_invariant("pop_committed");
2049
2050        t.push(entry("Z"));
2051        t.clear_redo();
2052        t.assert_path_invariant("clear_redo");
2053
2054        t.clear_all();
2055        t.assert_path_invariant("clear_all");
2056    }
2057}
2058
2059// ─── Phase 3a delta-storage tests ─────────────────────────────────────────────
2060//
2061// Correctness of the reversible delta and the warm/cold materialization is
2062// where text gets silently corrupted, so these lean hard on it: exact diff
2063// round-trips over random (incl. multi-byte) content, every node reconstructing
2064// identically warm and cold, and a random op stream cross-checked against a
2065// full-snapshot reference model kept alongside. All randomness is a deterministic
2066// xorshift seeded from a fixed constant — never `SystemTime`/entropy — so a
2067// failure reproduces exactly.
2068#[cfg(test)]
2069mod delta_tests {
2070    use super::*;
2071
2072    /// Iteration count for the randomized differential loops below, capped
2073    /// hard under miri.
2074    ///
2075    /// These loops are worth thousands of steps on a normal run: their value
2076    /// is statistical, shaking out logic bugs in the diff / keyframe code from
2077    /// a random op mix. That is a property of *executing* them, and miri
2078    /// interprets instead — six loops totalling ~22 300 iterations is the bulk
2079    /// of the weekly miri job's runtime, enough to push it past an hour.
2080    ///
2081    /// miri is there to catch UB, and UB shows up on the code *paths*, not on
2082    /// the thousandth repetition of one — so a short pass covers what miri can
2083    /// actually detect. Normal runs are untouched and keep the full count.
2084    fn stress_iters(n: usize) -> usize {
2085        if cfg!(miri) { n.min(50) } else { n }
2086    }
2087
2088    /// Deterministic xorshift64* PRNG, fixed-seeded so runs are reproducible.
2089    struct Rng(u64);
2090    impl Rng {
2091        fn new(seed: u64) -> Self {
2092            // xorshift needs a non-zero state.
2093            Self(if seed == 0 {
2094                0x9E37_79B9_7F4A_7C15
2095            } else {
2096                seed
2097            })
2098        }
2099        fn next_u64(&mut self) -> u64 {
2100            let mut x = self.0;
2101            x ^= x >> 12;
2102            x ^= x << 25;
2103            x ^= x >> 27;
2104            self.0 = x;
2105            x.wrapping_mul(0x2545_F491_4F6C_DD1D)
2106        }
2107        fn below(&mut self, n: usize) -> usize {
2108            (self.next_u64() % n as u64) as usize
2109        }
2110    }
2111
2112    /// A random char-granular mutation of `s`: insert, delete, or replace a
2113    /// span, drawing from an alphabet that mixes ASCII, accented, CJK, and
2114    /// emoji so multi-byte boundaries are exercised.
2115    fn mutate(s: &str, rng: &mut Rng) -> String {
2116        const ALPHABET: [char; 10] = ['a', 'b', '\n', 'é', '日', '本', '🎉', '語', 'x', 'z'];
2117        let chars: Vec<char> = s.chars().collect();
2118        let pick = |rng: &mut Rng| ALPHABET[rng.below(ALPHABET.len())];
2119        match rng.below(3) {
2120            0 => {
2121                let pos = rng.below(chars.len() + 1);
2122                let mut v = chars.clone();
2123                v.insert(pos, pick(rng));
2124                v.into_iter().collect()
2125            }
2126            1 if !chars.is_empty() => {
2127                let pos = rng.below(chars.len());
2128                let mut v = chars.clone();
2129                v.remove(pos);
2130                v.into_iter().collect()
2131            }
2132            _ => {
2133                if chars.is_empty() {
2134                    return pick(rng).to_string();
2135                }
2136                let a = rng.below(chars.len());
2137                let b = (a + rng.below(chars.len() - a + 1)).min(chars.len());
2138                let mut v = chars[..a].to_vec();
2139                v.push(pick(rng));
2140                v.extend_from_slice(&chars[b..]);
2141                v.into_iter().collect()
2142            }
2143        }
2144    }
2145
2146    fn entry_str(s: &str) -> UndoEntry {
2147        UndoEntry {
2148            rope: ropey::Rope::from_str(s),
2149            cursor: (0, 0),
2150            timestamp: SystemTime::now(),
2151            marks: MarkSnapshot::default(),
2152        }
2153    }
2154
2155    // ── (0) differential oracle: the pre-chunk-walk `diff` ────────────────────
2156    //
2157    // The original implementation, verbatim, materializing BOTH ropes with
2158    // `to_string()` before scanning bytes. `diff` was rewritten to walk chunks
2159    // instead (no full materialization); this is the semantic pin — the two must
2160    // agree on the EXACT `Delta` for every input, not merely round-trip.
2161
2162    fn diff_reference(parent: &ropey::Rope, child: &ropey::Rope) -> Delta {
2163        let a = parent.to_string();
2164        let b = child.to_string();
2165        let ab = a.as_bytes();
2166        let bb = b.as_bytes();
2167
2168        let max_pre = ab.len().min(bb.len());
2169        let mut pre = 0;
2170        while pre < max_pre && ab[pre] == bb[pre] {
2171            pre += 1;
2172        }
2173        while pre > 0 && !a.is_char_boundary(pre) {
2174            pre -= 1;
2175        }
2176
2177        let max_suf = max_pre - pre;
2178        let mut suf = 0;
2179        while suf < max_suf && ab[ab.len() - 1 - suf] == bb[bb.len() - 1 - suf] {
2180            suf += 1;
2181        }
2182        let mut a_end = ab.len() - suf;
2183        while a_end < ab.len() && !a.is_char_boundary(a_end) {
2184            a_end += 1;
2185        }
2186        let b_end = bb.len() - (ab.len() - a_end);
2187
2188        Delta {
2189            start: a[..pre].chars().count(),
2190            old: a[pre..a_end].to_string(),
2191            new: b[pre..b_end].to_string(),
2192        }
2193    }
2194
2195    /// Assert the chunk-walking `diff` is byte-identical to `diff_reference`,
2196    /// over BOTH single-chunk ropes and multi-chunk ones (ropey only splits past
2197    /// its ~1 KiB leaf size, so short fixtures alone would never exercise the
2198    /// cross-chunk cursor logic).
2199    #[track_caller]
2200    fn assert_diff_matches_reference(sa: &str, sb: &str) {
2201        let a = ropey::Rope::from_str(sa);
2202        let b = ropey::Rope::from_str(sb);
2203        assert_eq!(
2204            diff(&a, &b),
2205            diff_reference(&a, &b),
2206            "diff != reference for {sa:?} -> {sb:?}"
2207        );
2208        // Same content, but built by insertion so the two ropes have DIFFERENT,
2209        // misaligned chunk layouts — the reference sees only bytes, the walker
2210        // sees chunk seams, and they must still agree.
2211        let mut a2 = ropey::Rope::new();
2212        a2.insert(0, sa);
2213        let mut b2 = ropey::Rope::new();
2214        for (i, c) in sb.chars().enumerate() {
2215            b2.insert_char(i, c);
2216        }
2217        assert_eq!(
2218            diff(&a2, &b2),
2219            diff_reference(&a2, &b2),
2220            "diff != reference (misaligned chunks) for {sa:?} -> {sb:?}"
2221        );
2222    }
2223
2224    #[test]
2225    // Deliberately NOT size-scaled for miri: the documents here are sized to span
2226    // several of ropey's ~1 KB leaf chunks, which is the entire property under
2227    // test, so shrinking them would quietly test something weaker. Running them
2228    // interpreted costs >10 min on its own. hjkl-buffer has no `unsafe`, so miri's
2229    // reach is UB in ropey/std on these code paths — already covered by the ~185
2230    // other tests in this crate that do run under it.
2231    #[cfg_attr(miri, ignore = "multi-chunk documents are too slow interpreted")]
2232    fn diff_matches_reference_on_edge_cases() {
2233        let cases: &[(&str, &str)] = &[
2234            // equal / empty
2235            ("", ""),
2236            ("", "a"),
2237            ("a", ""),
2238            ("abc", "abc"),
2239            ("café🎉", "café🎉"),
2240            // prefix-only / suffix-only change
2241            ("abcdef", "abcdefXY"),
2242            ("abcdefXY", "abcdef"),
2243            ("Xabcdef", "abcdef"),
2244            ("abcdef", "Xabcdef"),
2245            // change at position 0 and at the very end
2246            ("abcdef", "Zbcdef"),
2247            ("abcdef", "abcdeZ"),
2248            // overlapping repeats — prefix and suffix scans would collide
2249            ("abcabc", "abc"),
2250            ("abc", "abcabc"),
2251            ("aaaa", "aa"),
2252            ("aa", "aaaa"),
2253            ("abab", "ababab"),
2254            ("xyxyxy", "xyxy"),
2255            // multi-byte chars sitting exactly on the cut points
2256            ("café", "cafés"),
2257            ("cafés", "café"),
2258            ("café", "cafè"),
2259            ("日本語", "日語"),
2260            ("日本語", "日本本語"),
2261            ("🎉🎉🎉", "🎉🎉"),
2262            ("🎉🎉", "🎉🎉🎉"),
2263            ("🎉x🎉", "🎉y🎉"),
2264            ("a🎉b", "a🎊b"),
2265            ("é", "e"),
2266            ("e", "é"),
2267            ("🎉", ""),
2268            ("", "🎉"),
2269            // byte-level suffix match that is NOT a char boundary: the tails of
2270            // 'é' (0xC3 0xA9) and 'é' share no byte, but 日 (E6 97 A5) vs 旦
2271            // (E6 97 A6) share a two-byte prefix mid-codepoint.
2272            ("日", "旦"),
2273            ("x日y", "x旦y"),
2274            ("語", "誤"),
2275            // long enough to be multi-chunk in both ropes
2276            (
2277                &"the quick brown fox ".repeat(400),
2278                &"the quick brown fox ".repeat(400),
2279            ),
2280        ];
2281        for (sa, sb) in cases {
2282            assert_diff_matches_reference(sa, sb);
2283        }
2284
2285        // Multi-chunk with an edit in the middle / at each end.
2286        let big: String = "the quick brown fox jumps over the lazy dog\n".repeat(200);
2287        let mid = big.len() / 2;
2288        let mut edited = big.clone();
2289        edited.insert(mid, 'Z');
2290        assert_diff_matches_reference(&big, &edited);
2291        assert_diff_matches_reference(&edited, &big);
2292        assert_diff_matches_reference(&big, &format!("Z{big}"));
2293        assert_diff_matches_reference(&big, &format!("{big}Z"));
2294        assert_diff_matches_reference(&big, &big.repeat(2));
2295
2296        // Multi-chunk with multi-byte chars straddling likely leaf seams.
2297        let uni: String = "café 日本語 🎉 αβγ\n".repeat(200);
2298        let umid = uni.len() / 2;
2299        let umid = (0..=umid).rev().find(|i| uni.is_char_boundary(*i)).unwrap();
2300        let mut uedited = uni.clone();
2301        uedited.insert(umid, '🎊');
2302        assert_diff_matches_reference(&uni, &uedited);
2303        assert_diff_matches_reference(&uedited, &uni);
2304    }
2305
2306    #[test]
2307    fn diff_matches_reference_over_random_evolving_content() {
2308        let mut rng = Rng::new(0x0BAD_F00D_1234_5678);
2309        let mut s = String::from("seed café 日本語\n🎉");
2310        for _ in 0..stress_iters(4000) {
2311            let t = mutate(&s, &mut rng);
2312            let a = ropey::Rope::from_str(&s);
2313            let b = ropey::Rope::from_str(&t);
2314            assert_eq!(diff(&a, &b), diff_reference(&a, &b), "{s:?} -> {t:?}");
2315            assert_eq!(diff(&b, &a), diff_reference(&b, &a), "{t:?} -> {s:?}");
2316            s = t;
2317        }
2318    }
2319
2320    #[test]
2321    fn diff_matches_reference_on_shared_leaf_clones() {
2322        // The `Arc`-shared-leaf fast path: `child` is a CLONE of `parent` plus
2323        // one edit, so most chunks are pointer-identical. Exercised at several
2324        // edit positions across a multi-chunk rope, plus deletes and the
2325        // degenerate no-op clone.
2326        let base: String = "the quick brown fox jumps over the lazy dog\n".repeat(300);
2327        let parent = ropey::Rope::from_str(&base);
2328        assert_eq!(
2329            diff(&parent, &parent.clone()),
2330            diff_reference(&parent, &parent.clone())
2331        );
2332        let n = parent.len_chars();
2333        for at in [0, 1, n / 4, n / 2, n - 1, n] {
2334            let mut child = parent.clone();
2335            child.insert_char(at, '𝄞');
2336            assert_eq!(
2337                diff(&parent, &child),
2338                diff_reference(&parent, &child),
2339                "@{at}"
2340            );
2341            assert_eq!(
2342                diff(&child, &parent),
2343                diff_reference(&child, &parent),
2344                "@{at}"
2345            );
2346        }
2347        for at in [0, n / 3, n - 10] {
2348            let mut child = parent.clone();
2349            child.remove(at..at + 5);
2350            assert_eq!(
2351                diff(&parent, &child),
2352                diff_reference(&parent, &child),
2353                "-{at}"
2354            );
2355            assert_eq!(
2356                diff(&child, &parent),
2357                diff_reference(&child, &parent),
2358                "-{at}"
2359            );
2360        }
2361    }
2362
2363    #[test]
2364    // Same reasoning as `diff_matches_reference_on_edge_cases`: the 300-line base
2365    // document exists to force multi-chunk ropes, so it is not size-scaled and the
2366    // test is skipped under miri rather than weakened.
2367    #[cfg_attr(miri, ignore = "multi-chunk documents are too slow interpreted")]
2368    fn diff_matches_reference_over_random_multi_chunk_pairs() {
2369        // Random pairs built from a multi-chunk corpus, so chunk seams land in
2370        // arbitrary places relative to the common prefix/suffix.
2371        let mut rng = Rng::new(0xF00D_BEEF_0BAD_C0DE);
2372        let units = ["ab", "café ", "日本語", "🎉", "\n", "x", "語日", "é"];
2373        let build = |rng: &mut Rng| -> String {
2374            let mut s = String::new();
2375            for _ in 0..rng.below(400) {
2376                s.push_str(units[rng.below(units.len())]);
2377            }
2378            s
2379        };
2380        for _ in 0..stress_iters(300) {
2381            let sa = build(&mut rng);
2382            // Half the pairs share a long common prefix/suffix with `sa`.
2383            let sb = if rng.below(2) == 0 {
2384                build(&mut rng)
2385            } else {
2386                let mut t = sa.clone();
2387                if !t.is_empty() {
2388                    let cut = rng.below(t.chars().count() + 1);
2389                    let byte = t.char_indices().nth(cut).map_or(t.len(), |(i, _)| i);
2390                    t.insert_str(byte, "🎊zz");
2391                }
2392                t
2393            };
2394            let a = ropey::Rope::from_str(&sa);
2395            let b = ropey::Rope::from_str(&sb);
2396            assert_eq!(diff(&a, &b), diff_reference(&a, &b));
2397            assert_eq!(diff(&b, &a), diff_reference(&b, &a));
2398        }
2399    }
2400
2401    // ── (i) delta round-trip: apply(diff(a,b))==b and apply_inverse==a ────────
2402
2403    #[test]
2404    fn diff_round_trips_over_random_evolving_content() {
2405        let mut rng = Rng::new(0x1234_5678_9ABC_DEF0);
2406        let mut s = String::from("seed café 日本語\n🎉");
2407        for _ in 0..stress_iters(4000) {
2408            let t = mutate(&s, &mut rng);
2409            let a = ropey::Rope::from_str(&s);
2410            let b = ropey::Rope::from_str(&t);
2411            let d = diff(&a, &b);
2412            assert_eq!(
2413                apply_forward(&a, &d).to_string(),
2414                t,
2415                "forward a->b failed (start={}, old={:?}, new={:?})",
2416                d.start,
2417                d.old,
2418                d.new
2419            );
2420            assert_eq!(
2421                apply_inverse(&b, &d).to_string(),
2422                s,
2423                "inverse b->a failed (start={}, old={:?}, new={:?})",
2424                d.start,
2425                d.old,
2426                d.new
2427            );
2428            s = t;
2429        }
2430    }
2431
2432    #[test]
2433    fn diff_round_trips_over_unrelated_pairs() {
2434        // Disjoint corpus pairs (not just single-edit neighbours) so the diff's
2435        // prefix/suffix logic is stressed on wholly different multi-byte text.
2436        let corpus = [
2437            "",
2438            "a",
2439            "café\n日本語\n",
2440            "🎉🎉🎉",
2441            "abcdef",
2442            "日本",
2443            "x\ny\nz\n",
2444            "aXb",
2445            "café",
2446            "語日本",
2447            "\n\n\n",
2448            "🎉x🎉y🎉",
2449        ];
2450        let mut rng = Rng::new(0xDEAD_BEEF_CAFE_1234);
2451        for _ in 0..stress_iters(3000) {
2452            let sa = corpus[rng.below(corpus.len())];
2453            let sb = corpus[rng.below(corpus.len())];
2454            let a = ropey::Rope::from_str(sa);
2455            let b = ropey::Rope::from_str(sb);
2456            let d = diff(&a, &b);
2457            assert_eq!(apply_forward(&a, &d).to_string(), sb);
2458            assert_eq!(apply_inverse(&b, &d).to_string(), sa);
2459        }
2460    }
2461
2462    // ── non-ASCII edit → undo → redo round-trip (multi-byte across a leave) ───
2463
2464    #[test]
2465    fn non_ascii_edit_undo_redo_round_trip() {
2466        // Edits land INSIDE multi-byte lines; undo/redo must round-trip the exact
2467        // bytes, proving the char-offset delta never splits a codepoint.
2468        let mut d = Driver::new("café\n日本語\n");
2469        d.edit("cafés\n日本語\n");
2470        d.edit("cafés\n日本語です\n");
2471        d.edit("cafés\n日本語です🎉\n");
2472        assert_eq!(d.undo().as_deref(), Some("cafés\n日本語です\n"));
2473        assert_eq!(d.undo().as_deref(), Some("cafés\n日本語\n"));
2474        assert_eq!(d.undo().as_deref(), Some("café\n日本語\n"));
2475        assert_eq!(d.redo().as_deref(), Some("cafés\n日本語\n"));
2476        assert_eq!(d.redo().as_deref(), Some("cafés\n日本語です\n"));
2477        assert_eq!(d.redo().as_deref(), Some("cafés\n日本語です🎉\n"));
2478        // Cold reconstruction of every node still matches (drop all caches).
2479        assert_warm_equals_cold(&mut d.t);
2480    }
2481
2482    // ── (ii) + (iii) random op stream vs a full-snapshot reference model ──────
2483
2484    #[test]
2485    fn tree_matches_full_snapshot_reference_over_random_ops() {
2486        let mut rng = Rng::new(0x9E37_79B9_7F4A_7C15);
2487        let start = "α\nβγ\n日本🎉\n";
2488        let mut real = UndoTree::new(ropey::Rope::from_str(start));
2489        let mut refr = RefTree::new(start);
2490        let mut live = start.to_string();
2491
2492        for step in 0..stress_iters(6000) {
2493            // Structural predicates stay in lockstep with the reference.
2494            assert_eq!(real.is_at_root(), refr.is_at_root(), "is_at_root @ {step}");
2495            assert_eq!(real.has_redo(), refr.has_redo(), "has_redo @ {step}");
2496            assert_eq!(real.depth_from_root(), refr.depth(), "depth @ {step}");
2497
2498            match rng.below(6) {
2499                0 | 1 => {
2500                    // Edit: push the PRE-edit state (engine discipline), then
2501                    // mutate the live buffer.
2502                    let pre = live.clone();
2503                    real.push(entry_str(&pre));
2504                    refr.push(&pre);
2505                    live = mutate(&live, &mut rng);
2506                }
2507                2 => {
2508                    let got = real
2509                        .undo_step(
2510                            ropey::Rope::from_str(&live),
2511                            (0, 0),
2512                            MarkSnapshot::default(),
2513                        )
2514                        .map(|e| e.rope.to_string());
2515                    let want = refr.undo_step(&live);
2516                    assert_eq!(got, want, "undo @ {step}");
2517                    if let Some(c) = got {
2518                        live = c;
2519                    }
2520                }
2521                3 => {
2522                    let got = real
2523                        .redo_step(
2524                            ropey::Rope::from_str(&live),
2525                            (0, 0),
2526                            MarkSnapshot::default(),
2527                        )
2528                        .map(|e| e.rope.to_string());
2529                    let want = refr.redo_step(&live);
2530                    assert_eq!(got, want, "redo @ {step}");
2531                    if let Some(c) = got {
2532                        live = c;
2533                    }
2534                }
2535                4 => {
2536                    let got = real
2537                        .seq_earlier_step(
2538                            ropey::Rope::from_str(&live),
2539                            (0, 0),
2540                            MarkSnapshot::default(),
2541                        )
2542                        .map(|e| e.rope.to_string());
2543                    let want = refr.seq_earlier_step(&live);
2544                    assert_eq!(got, want, "g- @ {step}");
2545                    if let Some(c) = got {
2546                        live = c;
2547                    }
2548                }
2549                _ => {
2550                    let got = real
2551                        .seq_later_step(
2552                            ropey::Rope::from_str(&live),
2553                            (0, 0),
2554                            MarkSnapshot::default(),
2555                        )
2556                        .map(|e| e.rope.to_string());
2557                    let want = refr.seq_later_step(&live);
2558                    assert_eq!(got, want, "g+ @ {step}");
2559                    if let Some(c) = got {
2560                        live = c;
2561                    }
2562                }
2563            }
2564
2565            // The reference model still rewrites the whole root→target chain on
2566            // every landing, so the two agreeing above already says the shortcut
2567            // reproduces it. Check the structure it relies on directly too: an
2568            // `on_path` drift is what would let the shortcut skip a stale
2569            // ancestor, and it is cheap to catch here at every step.
2570            real.assert_path_invariant(&format!("op @ {step}"));
2571
2572            // (ii) Every so often, assert warm and cold materialization agree
2573            // for every node — a cold-reconstructed node must equal the rope the
2574            // full-snapshot model would have held.
2575            if step % 200 == 0 {
2576                assert_warm_equals_cold(&mut real);
2577            }
2578        }
2579        assert_warm_equals_cold(&mut real);
2580    }
2581
2582    // ── (iv) keyframes: accelerated materialize vs the naive root replay ──────
2583    //
2584    // Keyframes (issue #302) pin a materialized rope every `KEYFRAME_INTERVAL`
2585    // nodes so a cold `g-` replays O(K) deltas instead of O(depth). They are a
2586    // CACHE: whatever they accelerate must be bit-identical to replaying every
2587    // delta from the root base with no cache at all. `materialize_naive` is that
2588    // oracle, in the same spirit as `diff_reference` above.
2589
2590    /// For every live node: the keyframe-accelerated `materialize` must equal the
2591    /// naive root-base replay exactly.
2592    #[track_caller]
2593    fn assert_materialize_matches_naive(t: &mut UndoTree) {
2594        for id in t.live_ids() {
2595            let naive = t.materialize_naive(id).to_string();
2596            let got = t.materialize_for_test(id).to_string();
2597            assert_eq!(got, naive, "accelerated != naive root replay for node {id}");
2598        }
2599    }
2600
2601    /// A linear history `n` states deep (so it crosses many keyframe intervals),
2602    /// plus the expected content of each state indexed by `seq`/depth. Every node
2603    /// is finalized, including the tip.
2604    fn deep_linear_history(n: usize) -> (UndoTree, Vec<String>) {
2605        // Under miri the document is shrunk 10x. `n` is deliberately NOT
2606        // touched: the keyframe ladder, the `n > 4 * KEYFRAME_INTERVAL`
2607        // assertion and every depth-related property stay exactly as they are
2608        // on a normal run — only the per-step rope volume drops. Without this
2609        // a single one of these tests ran for over 22 minutes under miri
2610        // (interpreted, not executed) and stalled the weekly job. The base
2611        // keeps its multi-line and multi-byte content, which is the part that
2612        // matters for the rope/delta paths.
2613        let reps = if cfg!(miri) { 2 } else { 20 };
2614        let base: String =
2615            "the quick brown fox\njumps over the lazy dog\ncafé 日本語 🎉\n".repeat(reps);
2616        let mut t = UndoTree::new(ropey::Rope::from_str(&base));
2617        let mut states = vec![base.clone()];
2618        let mut live = base;
2619        for i in 0..n {
2620            // Engine discipline: commit the PRE-edit state, then mutate.
2621            t.push(entry_str(&live));
2622            live = format!("e{i} {live}");
2623            states.push(live.clone());
2624        }
2625        // Stash the tip's live content so no node is left holding a stale edge.
2626        t.sync_current(ropey::Rope::from_str(&live));
2627        (t, states)
2628    }
2629
2630    #[test]
2631    fn deep_history_walks_back_and_forward_exactly() {
2632        // The `:earlier 9999` / `:later 9999` shape, deep enough that most jumps
2633        // land outside the warm window and go through a keyframe.
2634        // 65 under miri still satisfies the `> 4 * KEYFRAME_INTERVAL` floor
2635        // asserted below, so the walk still crosses four keyframes — the
2636        // property under test. See `deep_linear_history` for why.
2637        let n = if cfg!(miri) { 65 } else { 200 };
2638        assert!(n > 4 * KEYFRAME_INTERVAL);
2639        let (mut t, states) = deep_linear_history(n);
2640
2641        let mut live = states[n].clone();
2642        for want in (0..n).rev() {
2643            let got = t
2644                .seq_earlier_step(
2645                    ropey::Rope::from_str(&live),
2646                    (0, 0),
2647                    MarkSnapshot::default(),
2648                )
2649                .expect("history is deeper than the walk");
2650            live = got.rope.to_string();
2651            assert_eq!(live, states[want], "g- onto seq {want}");
2652        }
2653        assert!(
2654            t.seq_earlier_step(
2655                ropey::Rope::from_str(&live),
2656                (0, 0),
2657                MarkSnapshot::default()
2658            )
2659            .is_none(),
2660            "walk ended at the oldest state"
2661        );
2662        for (seq, want) in states.iter().enumerate().skip(1) {
2663            let got = t
2664                .seq_later_step(
2665                    ropey::Rope::from_str(&live),
2666                    (0, 0),
2667                    MarkSnapshot::default(),
2668                )
2669                .expect("history is deeper than the walk");
2670            live = got.rope.to_string();
2671            assert_eq!(&live, want, "g+ onto seq {seq}");
2672        }
2673        assert_materialize_matches_naive(&mut t);
2674        assert_warm_equals_cold(&mut t);
2675    }
2676
2677    /// `by_seq` must name exactly the live nodes, with the right ids. It is a
2678    /// second source of truth for `g-`/`g+` targets, so drift here silently
2679    /// sends history steps to the wrong state (or reports the end of history
2680    /// early) rather than failing loudly.
2681    ///
2682    /// Checked against a brute-force arena scan — the code `node_below` /
2683    /// `node_above` used before the index existed — after pushes, branch
2684    /// creation, undo/redo, pruning to a node budget, `clear_all`, and a
2685    /// serialize round trip, since those are the paths that allocate and free.
2686    #[test]
2687    fn seq_index_matches_the_arena() {
2688        fn brute(t: &UndoTree) -> std::collections::BTreeMap<u64, NodeId> {
2689            t.nodes
2690                .iter()
2691                .enumerate()
2692                .filter_map(|(id, slot)| slot.as_ref().map(|n| (n.seq, id)))
2693                .collect()
2694        }
2695        fn check(t: &UndoTree, when: &str) {
2696            assert_eq!(t.by_seq, brute(t), "seq index drifted after {when}");
2697            // Every step target agrees with a scan of the arena, which is what
2698            // the index replaced.
2699            for probe in 0..t.next_seq + 1 {
2700                let below = t
2701                    .nodes
2702                    .iter()
2703                    .enumerate()
2704                    .filter_map(|(id, s)| s.as_ref().map(|n| (n.seq, id)))
2705                    .filter(|&(sq, _)| sq < probe)
2706                    .max_by_key(|&(sq, _)| sq)
2707                    .map(|(_, id)| id);
2708                let above = t
2709                    .nodes
2710                    .iter()
2711                    .enumerate()
2712                    .filter_map(|(id, s)| s.as_ref().map(|n| (n.seq, id)))
2713                    .filter(|&(sq, _)| sq > probe)
2714                    .min_by_key(|&(sq, _)| sq)
2715                    .map(|(_, id)| id);
2716                assert_eq!(
2717                    t.node_below(probe),
2718                    below,
2719                    "node_below({probe}) after {when}"
2720                );
2721                assert_eq!(
2722                    t.node_above(probe),
2723                    above,
2724                    "node_above({probe}) after {when}"
2725                );
2726            }
2727        }
2728
2729        let mk = |text: &str| UndoEntry {
2730            rope: ropey::Rope::from_str(text),
2731            cursor: (0, 0),
2732            timestamp: SystemTime::now(),
2733            marks: MarkSnapshot::default(),
2734        };
2735
2736        let mut t = UndoTree::new(ropey::Rope::from_str("s0"));
2737        check(&t, "new");
2738
2739        for i in 1..=8 {
2740            t.push(mk(&format!("s{i}")));
2741        }
2742        check(&t, "pushes");
2743
2744        // Undo twice then push: forks a branch, frees nothing.
2745        t.undo_step(ropey::Rope::from_str("s8"), (0, 0), MarkSnapshot::default());
2746        t.undo_step(ropey::Rope::from_str("s7"), (0, 0), MarkSnapshot::default());
2747        t.push(mk("branch"));
2748        check(&t, "branch");
2749
2750        // Enforcing a node budget frees through `free` / `free_subtree`.
2751        t.cap(3);
2752        check(&t, "cap");
2753
2754        let round = UndoTree::from_serializable(&t.to_serializable()).expect("round trip");
2755        check(&round, "from_serializable");
2756
2757        t.clear_all();
2758        check(&t, "clear_all");
2759    }
2760
2761    #[test]
2762    fn keyframes_bound_the_cold_replay_distance() {
2763        // 65 still spans four keyframe intervals, which is what makes the
2764        // per-node replay-distance bound below meaningful. See
2765        // `deep_linear_history` for why miri gets a smaller history.
2766        let n = if cfg!(miri) { 65 } else { 200 };
2767        let (mut t, _) = deep_linear_history(n);
2768        // Steady state: the ordinary warm entries have aged out, the keyframes
2769        // are still pinned. Every node must be within one interval of an anchor.
2770        t.drop_warm_caches();
2771        for id in t.live_ids() {
2772            let d = t.replay_distance(id);
2773            assert!(
2774                d < KEYFRAME_INTERVAL,
2775                "node {id} (depth {}) replays {d} deltas, over the keyframe bound",
2776                t.get(id).depth
2777            );
2778        }
2779        // Drop the keyframes too and the bound is gone — proof that it is the
2780        // keyframes doing the bounding and not the warm LRU or the tree shape.
2781        let deepest = *t
2782            .live_ids()
2783            .iter()
2784            .max_by_key(|&&id| t.get(id).depth)
2785            .unwrap();
2786        t.drop_all_caches();
2787        assert!(t.replay_distance(deepest) > KEYFRAME_INTERVAL);
2788        // One materialize off the fully-cold tree re-pins the whole ladder.
2789        t.materialize_for_test(deepest);
2790        t.drop_warm_caches();
2791        for id in t.live_ids() {
2792            assert!(t.replay_distance(id) < KEYFRAME_INTERVAL, "node {id}");
2793        }
2794    }
2795
2796    #[test]
2797    fn keyframe_materialize_matches_naive_over_random_ops() {
2798        // Push-heavy op mix so the tree gets deep enough to cross many keyframe
2799        // intervals, with undo/redo/g-/g+ and periodic `cap` pruning mixed in —
2800        // pruning renumbers nothing but does free nodes and re-root the tree, so
2801        // it is where a stale keyframe would surface as corrupted text.
2802        let mut rng = Rng::new(0x0FF1_CE00_D15E_A5E5);
2803        let start = "α\nβγ\n日本🎉\nthe quick brown fox\n";
2804        let mut t = UndoTree::new(ropey::Rope::from_str(start));
2805        let mut live = start.to_string();
2806
2807        for step in 0..stress_iters(5000) {
2808            match rng.below(10) {
2809                0..=5 => {
2810                    let pre = live.clone();
2811                    t.push(entry_str(&pre));
2812                    live = mutate(&live, &mut rng);
2813                }
2814                6 => {
2815                    if let Some(e) = t.undo_step(
2816                        ropey::Rope::from_str(&live),
2817                        (0, 0),
2818                        MarkSnapshot::default(),
2819                    ) {
2820                        live = e.rope.to_string();
2821                    }
2822                }
2823                7 => {
2824                    if let Some(e) = t.redo_step(
2825                        ropey::Rope::from_str(&live),
2826                        (0, 0),
2827                        MarkSnapshot::default(),
2828                    ) {
2829                        live = e.rope.to_string();
2830                    }
2831                }
2832                8 => {
2833                    if let Some(e) = t.seq_earlier_step(
2834                        ropey::Rope::from_str(&live),
2835                        (0, 0),
2836                        MarkSnapshot::default(),
2837                    ) {
2838                        live = e.rope.to_string();
2839                    }
2840                }
2841                _ => {
2842                    if let Some(e) = t.seq_later_step(
2843                        ropey::Rope::from_str(&live),
2844                        (0, 0),
2845                        MarkSnapshot::default(),
2846                    ) {
2847                        live = e.rope.to_string();
2848                    }
2849                }
2850            }
2851            // Whatever the tree hands back must be what the naive replay of the
2852            // node it landed on says — checked every step, cheaply.
2853            let cur = t.current;
2854            assert_eq!(
2855                t.materialize_for_test(cur).to_string(),
2856                t.materialize_naive(cur).to_string(),
2857                "current node diverged @ {step}"
2858            );
2859            t.assert_path_invariant(&format!("op @ {step}"));
2860            if step % 250 == 0 {
2861                assert_materialize_matches_naive(&mut t);
2862            }
2863            if step % 700 == 0 {
2864                t.cap(60);
2865                t.assert_path_invariant(&format!("cap @ {step}"));
2866            }
2867        }
2868        assert_materialize_matches_naive(&mut t);
2869        assert_warm_equals_cold(&mut t);
2870    }
2871
2872    #[test]
2873    fn deserialized_deep_tree_rebuilds_the_keyframe_ladder() {
2874        // Depth is NOT serialized (keyframes are a cache, the on-disk format is
2875        // untouched), so a loaded tree has to recompute it — otherwise every
2876        // cross-session `g-` would be a full replay again.
2877        // 65 still spans four keyframe intervals, so the reloaded tree must
2878        // still rebuild a real ladder rather than a trivial one. See
2879        // `deep_linear_history` for why miri gets a smaller history.
2880        let n = if cfg!(miri) { 65 } else { 100 };
2881        let (t, states) = deep_linear_history(n);
2882        let ser = t.to_serializable();
2883        let mut back = UndoTree::from_serializable(&ser).expect("valid projection");
2884
2885        let deepest = *back
2886            .live_ids()
2887            .iter()
2888            .max_by_key(|&&id| back.get(id).depth)
2889            .unwrap();
2890        assert_eq!(back.get(deepest).depth, n, "depths recomputed on load");
2891        assert_eq!(back.materialize_for_test(deepest).to_string(), states[n]);
2892        assert_materialize_matches_naive(&mut back);
2893
2894        back.drop_warm_caches();
2895        for id in back.live_ids() {
2896            assert!(back.replay_distance(id) < KEYFRAME_INTERVAL, "node {id}");
2897        }
2898    }
2899
2900    /// For every live node: materialize warm, drop all caches, materialize cold,
2901    /// assert identical. Restores nothing else (test-local).
2902    fn assert_warm_equals_cold(t: &mut UndoTree) {
2903        let ids = t.live_ids();
2904        let warm: Vec<String> = ids
2905            .iter()
2906            .map(|&id| t.materialize_for_test(id).to_string())
2907            .collect();
2908        t.drop_all_caches();
2909        for (i, &id) in ids.iter().enumerate() {
2910            let cold = t.materialize_for_test(id).to_string();
2911            assert_eq!(cold, warm[i], "warm != cold for node {id}");
2912        }
2913    }
2914
2915    /// Engine-faithful driver over the real (delta) [`UndoTree`]: mirrors how
2916    /// `editor.rs` pushes the PRE-edit state and restores returned content.
2917    struct Driver {
2918        t: UndoTree,
2919        live: String,
2920    }
2921    impl Driver {
2922        fn new(s: &str) -> Self {
2923            Self {
2924                t: UndoTree::new(ropey::Rope::from_str(s)),
2925                live: s.to_string(),
2926            }
2927        }
2928        fn edit(&mut self, new: &str) {
2929            self.t.push(entry_str(&self.live));
2930            self.live = new.to_string();
2931        }
2932        fn undo(&mut self) -> Option<String> {
2933            let e = self.t.undo_step(
2934                ropey::Rope::from_str(&self.live),
2935                (0, 0),
2936                MarkSnapshot::default(),
2937            )?;
2938            self.live = e.rope.to_string();
2939            Some(self.live.clone())
2940        }
2941        fn redo(&mut self) -> Option<String> {
2942            let e = self.t.redo_step(
2943                ropey::Rope::from_str(&self.live),
2944                (0, 0),
2945                MarkSnapshot::default(),
2946            )?;
2947            self.live = e.rope.to_string();
2948            Some(self.live.clone())
2949        }
2950    }
2951
2952    /// Full-snapshot reference tree — Phase 2b's model (a whole rope per node),
2953    /// the oracle the delta tree is cross-checked against. Content only (cursor /
2954    /// marks / timestamps are covered by the existing tree tests).
2955    struct RefNode {
2956        parent: Option<usize>,
2957        children: Vec<usize>,
2958        last_child: Option<usize>,
2959        content: String,
2960        seq: u64,
2961    }
2962    struct RefTree {
2963        nodes: Vec<Option<RefNode>>,
2964        current: usize,
2965        next_seq: u64,
2966    }
2967    impl RefTree {
2968        fn new(s: &str) -> Self {
2969            let root = RefNode {
2970                parent: None,
2971                children: Vec::new(),
2972                last_child: None,
2973                content: s.to_string(),
2974                seq: 0,
2975            };
2976            Self {
2977                nodes: vec![Some(root)],
2978                current: 0,
2979                next_seq: 1,
2980            }
2981        }
2982        fn get(&self, id: usize) -> &RefNode {
2983            self.nodes[id].as_ref().unwrap()
2984        }
2985        fn get_mut(&mut self, id: usize) -> &mut RefNode {
2986            self.nodes[id].as_mut().unwrap()
2987        }
2988        fn alloc(&mut self, n: RefNode) -> usize {
2989            self.nodes.push(Some(n));
2990            self.nodes.len() - 1
2991        }
2992        fn is_at_root(&self) -> bool {
2993            self.get(self.current).parent.is_none()
2994        }
2995        fn has_redo(&self) -> bool {
2996            self.get(self.current).last_child.is_some()
2997        }
2998        fn depth(&self) -> usize {
2999            let mut d = 0;
3000            let mut n = self.get(self.current).parent;
3001            while let Some(p) = n {
3002                d += 1;
3003                n = self.get(p).parent;
3004            }
3005            d
3006        }
3007        fn push(&mut self, pre: &str) {
3008            let cur = self.current;
3009            self.get_mut(cur).content = pre.to_string();
3010            let seq = self.next_seq;
3011            self.next_seq += 1;
3012            let child = self.alloc(RefNode {
3013                parent: Some(cur),
3014                children: Vec::new(),
3015                last_child: None,
3016                content: pre.to_string(),
3017                seq,
3018            });
3019            let c = self.get_mut(cur);
3020            c.children.push(child);
3021            c.last_child = Some(child);
3022            self.current = child;
3023        }
3024        fn undo_step(&mut self, live: &str) -> Option<String> {
3025            let cur = self.current;
3026            let par = self.get(cur).parent?;
3027            self.get_mut(cur).content = live.to_string();
3028            self.get_mut(par).last_child = Some(cur);
3029            self.current = par;
3030            Some(self.get(par).content.clone())
3031        }
3032        fn redo_step(&mut self, live: &str) -> Option<String> {
3033            let cur = self.current;
3034            let child = self.get(cur).last_child?;
3035            self.get_mut(cur).content = live.to_string();
3036            self.current = child;
3037            Some(self.get(child).content.clone())
3038        }
3039        fn current_seq(&self) -> u64 {
3040            self.get(self.current).seq
3041        }
3042        fn node_below(&self, s: u64) -> Option<usize> {
3043            let mut best: Option<(u64, usize)> = None;
3044            for (id, slot) in self.nodes.iter().enumerate() {
3045                if let Some(n) = slot
3046                    && n.seq < s
3047                    && best.is_none_or(|(bs, _)| n.seq > bs)
3048                {
3049                    best = Some((n.seq, id));
3050                }
3051            }
3052            best.map(|(_, id)| id)
3053        }
3054        fn node_above(&self, s: u64) -> Option<usize> {
3055            let mut best: Option<(u64, usize)> = None;
3056            for (id, slot) in self.nodes.iter().enumerate() {
3057                if let Some(n) = slot
3058                    && n.seq > s
3059                    && best.is_none_or(|(bs, _)| n.seq < bs)
3060                {
3061                    best = Some((n.seq, id));
3062                }
3063            }
3064            best.map(|(_, id)| id)
3065        }
3066        fn retarget(&mut self, target: usize) {
3067            self.current = target;
3068            let mut node = target;
3069            while let Some(p) = self.get(node).parent {
3070                self.get_mut(p).last_child = Some(node);
3071                node = p;
3072            }
3073        }
3074        fn stash_and_move(&mut self, target: usize, live: &str) {
3075            let cur = self.current;
3076            self.get_mut(cur).content = live.to_string();
3077            self.retarget(target);
3078        }
3079        fn seq_earlier_step(&mut self, live: &str) -> Option<String> {
3080            let target = self.node_below(self.current_seq())?;
3081            self.stash_and_move(target, live);
3082            Some(self.get(target).content.clone())
3083        }
3084        fn seq_later_step(&mut self, live: &str) -> Option<String> {
3085            let target = self.node_above(self.current_seq())?;
3086            self.stash_and_move(target, live);
3087            Some(self.get(target).content.clone())
3088        }
3089    }
3090}
3091
3092// ─── Phase 3b serialize/deserialize tests ─────────────────────────────────────
3093//
3094// The undofile is only as trustworthy as this round-trip: a projection that
3095// loses a branch, mislinks a parent, or reconstructs a node's content wrong
3096// would silently corrupt cross-session undo. These build the headline tree
3097// (5 edits, u, u), project it, rebuild, and assert BOTH the per-node content
3098// (keyed by the stable `seq`) and the live walk (`<C-r>` forward, `u` back)
3099// survive the trip.
3100#[cfg(test)]
3101mod serialize_tests {
3102    use super::*;
3103
3104    fn e(text: &str) -> UndoEntry {
3105        UndoEntry {
3106            rope: ropey::Rope::from_str(text),
3107            cursor: (0, 0),
3108            timestamp: SystemTime::now(),
3109            marks: MarkSnapshot::default(),
3110        }
3111    }
3112    fn l(text: &str) -> (ropey::Rope, (usize, usize), MarkSnapshot) {
3113        (ropey::Rope::from_str(text), (0, 0), MarkSnapshot::default())
3114    }
3115
3116    /// The headline tree: root "s0", five edits to live "s5", then `u` twice so
3117    /// `current` sits on "s3" with the forward branch (s4/s5) retained — exactly
3118    /// the state a `:wq` would persist.
3119    fn headline_tree() -> UndoTree {
3120        let mut t = UndoTree::new(ropey::Rope::from_str("s0"));
3121        for pre in ["s0", "s1", "s2", "s3", "s4"] {
3122            t.push(e(pre)); // engine discipline: push the PRE-edit live state
3123        }
3124        let (r, c, m) = l("s5");
3125        t.undo_step(r, c, m); // -> s4
3126        let (r, c, m) = l("s4");
3127        t.undo_step(r, c, m); // -> s3
3128        t.sync_current(ropey::Rope::from_str("s3")); // stash exact live, like save
3129        t
3130    }
3131
3132    /// Every node's content (keyed by `seq`), materialized cold-then-warm.
3133    fn content_by_seq(t: &mut UndoTree) -> std::collections::BTreeMap<u64, String> {
3134        t.live_ids()
3135            .into_iter()
3136            .map(|id| {
3137                let seq = t.get(id).seq;
3138                (seq, t.materialize_for_test(id).to_string())
3139            })
3140            .collect()
3141    }
3142
3143    #[test]
3144    fn round_trip_reproduces_structure_and_content() {
3145        let mut orig = headline_tree();
3146        let cur_seq = orig.current_node_seq();
3147        let ser = orig.to_serializable();
3148        let orig_content = content_by_seq(&mut orig);
3149
3150        let mut back = UndoTree::from_serializable(&ser).expect("valid projection");
3151        assert_eq!(back.current_node_seq(), cur_seq, "current preserved");
3152        assert_eq!(back.next_seq, orig.next_seq, "next_seq preserved");
3153        // Force cold reconstruction (fresh tree has no warm caches) and compare.
3154        assert_eq!(
3155            content_by_seq(&mut back),
3156            orig_content,
3157            "content at every node reproduced"
3158        );
3159        // Six states: s0..s5.
3160        assert_eq!(orig_content.len(), 6);
3161        assert_eq!(orig_content[&3], "s3");
3162        assert_eq!(orig_content[&5], "s5");
3163    }
3164
3165    #[test]
3166    fn deserialized_tree_walks_forward_and_back() {
3167        let ser = headline_tree().to_serializable();
3168        let mut t = UndoTree::from_serializable(&ser).unwrap();
3169        // `<C-r>` twice: s3 -> s4 -> s5 (the retained forward branch).
3170        let (r, c, m) = l("s3");
3171        assert_eq!(t.redo_step(r, c, m).unwrap().rope.to_string(), "s4");
3172        let (r, c, m) = l("s4");
3173        assert_eq!(t.redo_step(r, c, m).unwrap().rope.to_string(), "s5");
3174        // `u` all the way back to the root.
3175        let mut live = "s5".to_string();
3176        for want in ["s4", "s3", "s2", "s1", "s0"] {
3177            let (r, c, m) = l(&live);
3178            assert_eq!(t.undo_step(r, c, m).unwrap().rope.to_string(), want);
3179            live = want.to_string();
3180        }
3181        assert!(t.is_at_root());
3182    }
3183
3184    #[test]
3185    fn from_serializable_rejects_out_of_range_current() {
3186        let mut ser = headline_tree().to_serializable();
3187        ser.current = ser.nodes.len() as u32; // past the end
3188        assert!(UndoTree::from_serializable(&ser).is_none());
3189    }
3190
3191    #[test]
3192    fn from_serializable_rejects_non_root_missing_delta() {
3193        let mut ser = headline_tree().to_serializable();
3194        // Blank a non-root node's delta ⇒ structurally invalid ⇒ rejected.
3195        let victim = if ser.root == 0 { 1 } else { 0 };
3196        ser.nodes[victim].delta = None;
3197        assert!(UndoTree::from_serializable(&ser).is_none());
3198    }
3199
3200    /// A parent-link cycle must be rejected at load, not walked at use.
3201    /// `materialize` follows `parent` in an unbounded loop, so a cycle there
3202    /// hangs while growing `path` — this is the only place that can see it.
3203    #[test]
3204    fn from_serializable_rejects_a_parent_link_cycle() {
3205        let mut ser = headline_tree().to_serializable();
3206        assert!(ser.nodes.len() > 2, "fixture needs three nodes");
3207        // Point two non-root nodes at each other, both ways, so the links stay
3208        // mutually consistent and every node stays reachable from the root
3209        // through the list it was already in. Only the single-lister rule
3210        // rejects this.
3211        let (a, b) = match ser.root {
3212            0 => (1, 2),
3213            1 => (0, 2),
3214            _ => (0, 1),
3215        };
3216        ser.nodes[a].parent = Some(b as u32);
3217        ser.nodes[b].parent = Some(a as u32);
3218        ser.nodes[a].children.push(b as u32);
3219        ser.nodes[b].children.push(a as u32);
3220        assert!(UndoTree::from_serializable(&ser).is_none());
3221    }
3222
3223    /// A parent link that the named parent does not mirror as a child leaves
3224    /// the two walks (`children` forward, `parent` back) disagreeing.
3225    #[test]
3226    fn from_serializable_rejects_an_unmirrored_parent_link() {
3227        let mut ser = headline_tree().to_serializable();
3228        let victim = if ser.root == 0 { 1 } else { 0 };
3229        let parent = ser.nodes[victim].parent.expect("non-root") as usize;
3230        ser.nodes[parent].children.retain(|&c| c != victim as u32);
3231        assert!(UndoTree::from_serializable(&ser).is_none());
3232    }
3233
3234    /// A cycle that hangs off no root at all: nodes 1 and 2 name each other
3235    /// both ways, so the lists still partition — reachability from the root is
3236    /// the guard that catches this one. This is the shape that hung
3237    /// `install_recovered_undo_tree` before the loader checked for it.
3238    #[test]
3239    fn from_serializable_rejects_a_cycle_unreachable_from_the_root() {
3240        let d = || {
3241            Some(Delta {
3242                start: 0,
3243                old: String::new(),
3244                new: String::from("x"),
3245            })
3246        };
3247        let n = |parent, children, delta, seq| SerNode {
3248            parent,
3249            children,
3250            last_child: None,
3251            delta,
3252            cursor: (0, 0),
3253            timestamp_unix_ms: 0,
3254            marks: MarkSnapshot::default(),
3255            seq,
3256        };
3257        let ser = SerTree {
3258            base: "hello".into(),
3259            nodes: vec![
3260                n(None, vec![], None, 0),
3261                n(Some(2), vec![2], d(), 1),
3262                n(Some(1), vec![1], d(), 2),
3263            ],
3264            root: 0,
3265            current: 1,
3266            next_seq: 3,
3267        };
3268        assert!(UndoTree::from_serializable(&ser).is_none());
3269    }
3270
3271    /// `by_seq` is keyed by `seq`, so a repeat would silently drop a node from
3272    /// the `g-` / `g+` index while leaving it in the arena.
3273    #[test]
3274    fn from_serializable_rejects_a_repeated_seq() {
3275        let mut ser = headline_tree().to_serializable();
3276        let victim = if ser.root == 0 { 1 } else { 0 };
3277        let other = if victim == 0 { 1 } else { 0 };
3278        ser.nodes[victim].seq = ser.nodes[other].seq;
3279        assert!(UndoTree::from_serializable(&ser).is_none());
3280    }
3281
3282    #[test]
3283    fn multibyte_content_survives_round_trip() {
3284        let mut t = UndoTree::new(ropey::Rope::from_str("café\n日本語"));
3285        t.push(e("café\n日本語"));
3286        t.push(e("cafés\n日本語"));
3287        t.sync_current(ropey::Rope::from_str("cafés\n日本語です🎉"));
3288        let want = content_by_seq(&mut t);
3289        let ser = t.to_serializable();
3290        let mut back = UndoTree::from_serializable(&ser).unwrap();
3291        assert_eq!(content_by_seq(&mut back), want);
3292    }
3293}