Fuzzer: undo checks follow the bounded undo history (#2332)

* Fuzzer: undo checks follow the bounded undo history

The undo history drops its oldest steps past 100 entries or 128 MB of snapshots (#1263). The
command-sequence fuzzer still compared "undo everything" with the episode's starting workbook,
so any episode whose snapshots passed the budget failed with "undoing all N steps doesn't give
back the starting workbook" although undo worked (seeds 104, 110 and 163 at 80 steps: a column
inserted next to whole-column borders, or rows deleted after filling whole columns, each made a
100 MB step).

The harness notices when an undoable command changed the workbook without the history growing,
and then compares with the state before the oldest step still in the history.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Fuzzer: undo and redo checks follow the reachable history boundary

Review of #2332. The harness now tracks the oldest state the undo history
can reach instead of a trimmed flag: whenever the history's oldest entry
changes without it emptying in between, the history dropped a step, and
undoing everything stops at the new oldest entry. That boundary is kept
when an edit.undo in the sequence uses up the retained history before the
episode ends, which used to fall back to the episode's start and report a
false undo mismatch.

The redo round trip no longer assumes n undos leave n redo entries: the
redo history is bounded too, so when it dropped the first step undone,
redo is expected to stop at the oldest entry it kept.

---------

Co-authored-by: nihilist-narwhal <240798405+nihilist-narwhal@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
authored and GitHub committed 2026-11-17 22:36:17 -06:00
1 parent 4c388df949
commit bea2191b35
1 file changed
+82 -9
@@ -35,7 +35,7 @@ use std::sync::Arc;
use gridcraft_core::{CellError, CellRef, MAX_COLS, MAX_ROWS, RangeRef, Value, col_to_letters};
use gridcraft_engine::calc::Calc;
use gridcraft_engine::model::{CalcMode, Formula, Sheet, SheetViewMode, Workbook};
use gridcraft_engine::{EngineError, Session, command_specs, find_command, io};
use gridcraft_engine::{EngineError, HistoryEntry, Session, command_specs, find_command, io};
use serde_json::{Value as Json, json};
const SAMPLES: [&str; 3] = ["sales", "budget", "grades"];
@@ -219,8 +219,24 @@ fn shrink(sample: &str, steps: &[Step], kind: &str) -> Vec<Step> {
struct Harness {
s: Session,
/// The workbook at the start of the current episode (what undoing everything gives back).
base: Arc<Workbook>,
/// What undoing everything gives back: the workbook at the start of the current episode or,
/// once the undo history has dropped its oldest steps (it is bounded by count and by the
/// memory its snapshots hold), the state before the oldest step it kept.
floor: Arc<Workbook>,
/// The workbook of the oldest undo step when the history was last looked at, to notice the
/// history dropping it.
oldest: Option<Arc<Workbook>>,
}
/// Where redoing the `n` steps just undone ends. The first of them went onto the redo history
/// with the final workbook `end`. The redo history is bounded like undo and drops its oldest
/// entries first, so when that entry is gone every older one is too, and redo stops after fewer
/// than `n` steps at the oldest entry it kept.
fn redo_target(end: &Arc<Workbook>, redo: &[HistoryEntry], n: usize) -> Arc<Workbook> {
if n == 0 || redo.iter().rev().take(n).any(|e| Arc::ptr_eq(&e.wb, end)) {
return end.clone();
}
redo.first().map_or_else(|| end.clone(), |e| e.wb.clone())
}
fn fail(step: usize, kind: &'static str, what: impl Into<String>) -> Failure {
@@ -243,8 +259,8 @@ impl Harness {
fn new(sample: &str) -> Harness {
let mut s = Session::new();
s.execute("file.new", json!({"sample": sample})).expect("sample opens");
let base = s.doc().expect("document").wb.clone();
Harness { s, base }
let floor = s.doc().expect("document").wb.clone();
Harness { s, floor, oldest: None }
}
fn wb(&self) -> Arc<Workbook> {
@@ -262,6 +278,7 @@ impl Harness {
return Err(fail(i, "documents", format!("`{id}` changed the number of open documents; exclude it in EXCLUDED")));
}
let after = self.wb();
self.track_history();
let spec = find_command(id).expect("generated ids are registered");
if !spec.undoable
&& !matches!(id, "edit.undo" | "edit.redo")
@@ -293,6 +310,7 @@ impl Harness {
/// and reload; the next episode starts from here with an empty history.
fn end_episode(&mut self, i: usize) -> Result<(), Failure> {
let end = self.wb();
self.track_history();
let mut n = 0;
while !self.s.doc().expect("document").undo.is_empty() {
if let Err(EngineError::Internal(_, msg)) = self.s.execute("edit.undo", json!({})) {
@@ -304,7 +322,7 @@ impl Harness {
}
}
let undone = self.wb();
if let Some(diff) = workbook_diff(&self.base, &undone) {
if let Some(diff) = workbook_diff(&self.floor, &undone) {
return Err(fail(i, "undo", format!("undoing all {n} steps doesn't give back the starting workbook: {diff}")));
}
if watch_list(&end) != watch_list(&undone) {
@@ -312,23 +330,39 @@ impl Harness {
}
self.check_state(i)?;
// Only the steps undone here: an `edit.undo {steps}` in the sequence may have left more.
let target = redo_target(&end, &self.s.doc().expect("document").redo, n);
for _ in 0..n {
if let Err(EngineError::Internal(_, msg)) = self.s.execute("edit.redo", json!({})) {
return Err(fail(i, "panic", format!("`edit.redo` panicked while redoing everything: {msg}")));
}
}
if let Some(diff) = workbook_diff(&end, &self.wb()) {
return Err(fail(i, "redo", format!("redoing all {n} steps doesn't give back the final workbook: {diff}")));
if let Some(diff) = workbook_diff(&target, &self.wb()) {
let what = if Arc::ptr_eq(&target, &end) { "the final workbook" } else { "the newest step the redo history kept" };
return Err(fail(i, "redo", format!("redoing all {n} steps doesn't give back {what}: {diff}")));
}
self.check_state(i)?;
guarded(i, "xlsx roundtrip", || roundtrip_diff(&end))?;
let d = self.s.active_mut().expect("document");
d.undo.clear();
d.redo.clear();
self.base = d.wb.clone();
self.floor = d.wb.clone();
self.oldest = None;
Ok(())
}
/// Notices the undo history dropping its oldest step: its oldest entry changed without the
/// history emptying in between. From then on undoing everything stops at the new oldest entry,
/// including after an `edit.undo` in the sequence has already used up what is left.
fn track_history(&mut self) {
let oldest = self.s.doc().expect("document").undo.first().map(|e| e.wb.clone());
if let (Some(was), Some(now)) = (&self.oldest, &oldest)
&& !Arc::ptr_eq(was, now)
{
self.floor = now.clone();
}
self.oldest = oldest;
}
fn finish(&mut self, steps: usize) -> Result<(), Failure> {
self.end_episode(steps)
}
@@ -1135,3 +1169,42 @@ fn view_mode_switch_is_not_an_undo_difference() {
assert!(failure.is_none(), "`{id}`: {failure:?}");
}
}
/// Review of #2332: once the undo history has dropped its oldest steps, an `edit.undo` in the
/// sequence can use up everything it kept before the episode ends. Undoing everything then has
/// nothing left to do, and the workbook is the state before the oldest step kept, not the
/// episode's start.
#[test]
fn a_trimmed_history_undone_in_the_sequence_stops_at_its_oldest_step() {
let mut h = Harness::new("sales");
// Past the engine's 100-step undo limit, so it drops the oldest 5 steps.
for i in 0..105 {
h.s.execute("cell.set", json!({"cell": format!("Z{}", i + 1), "input": i.to_string()})).expect("cell.set");
h.track_history();
}
assert_eq!(h.s.doc().expect("document").undo.len(), 100, "the history dropped its oldest steps");
if let Err(f) = h.step(0, "edit.undo", &json!({"steps": 100})) {
panic!("{f:?}");
}
assert!(h.s.doc().expect("document").undo.is_empty());
if let Err(f) = h.end_episode(0) {
panic!("{f:?}");
}
}
/// Review of #2332: the redo history is bounded too, so undoing `n` steps may leave fewer than
/// `n` to redo, and redo then stops at the oldest entry it kept rather than at the final workbook.
#[test]
fn redo_stops_at_the_oldest_step_its_history_kept() {
let h = Harness::new("sales");
let selection = h.s.doc().expect("document").selection.clone();
let entry = |wb: &Arc<Workbook>| HistoryEntry { label: String::new(), wb: wb.clone(), selection: selection.clone(), retained_bytes: 0 };
let end = Arc::new((*h.wb()).clone());
let kept = Arc::new((*h.wb()).clone());
// Both steps kept: the first one undone went onto the redo history with the final workbook.
assert!(Arc::ptr_eq(&redo_target(&end, &[entry(&end), entry(&kept)], 2), &end));
// Its entry dropped: redo stops at the oldest entry left.
assert!(Arc::ptr_eq(&redo_target(&end, &[entry(&kept)], 2), &kept));
// Nothing undone, so nothing to redo, whatever an earlier `edit.undo` left there.
assert!(Arc::ptr_eq(&redo_target(&end, &[entry(&kept)], 0), &end));
}