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