From b436516881d7e0fe2ea0a4275edbdbb9ed4dfe55 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 28 Oct 2026 05:41:16 -0700 Subject: [PATCH] History: a newest History Brush source counts as the state the byte budget keeps When the source was the newest past state, trimming kept it and one older state as well, beyond the budget. --- crates/ops/src/lib.rs | 2 +- crates/ops/src/tests.rs | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/crates/ops/src/lib.rs b/crates/ops/src/lib.rs index 6dacd3ec97..5cee5b2159 100644 --- a/crates/ops/src/lib.rs +++ b/crates/ops/src/lib.rs @@ -632,7 +632,7 @@ impl History { continue; } total = total.saturating_add(tile_bytes(&s.doc, &mut seen)); - if total > self.max_bytes && keep >= 1 { + if total > self.max_bytes && (keep >= 1 || source == Some(undo.len() - 1)) { break; } keep += 1; diff --git a/crates/ops/src/tests.rs b/crates/ops/src/tests.rs index 7cb1fc328a..bd7de002f0 100644 --- a/crates/ops/src/tests.rs +++ b/crates/ops/src/tests.rs @@ -222,6 +222,28 @@ fn byte_budget_keeps_the_brush_source_of_the_cleared_states() { assert!(Arc::ptr_eq(&h.row_doc(0, &cur).unwrap(), &source), "the History Brush paints from the same state"); } +/// When the History Brush's source is the newest past state, it is the state the byte budget +/// always keeps, and no older state is kept beside it. +#[test] +fn byte_budget_counts_a_newest_brush_source_as_the_kept_state() { + let mut h = History::new(50); + let mut cur = Arc::new(base()); + for i in 0..5 { + edit(&mut h, &mut cur, &format!("paint {i}"), |d| { + let id = d.layers[0].id; + d.layer_mut(id).unwrap().surface_mut().unwrap().write_pixel(1, 1, &[i as f32 / 8.0, 0.0, 0.0, 1.0]); + }); + } + assert!(h.set_brush_source(4)); + let source = h.state(4).unwrap(); + h.max_bytes = 1; + assert_eq!(h.trim(&cur), 4); + assert_counter(&h, &cur); + assert_eq!(h.past_len(), 1); + assert_eq!(h.brush_source(), Some(0)); + assert!(Arc::ptr_eq(&h.row_doc(0, &cur).unwrap(), &source)); +} + #[test] fn new_edit_clears_redo() { let mut h = History::default();