From 45e8c06132cc35014e13ad9928bd829b474694f9 Mon Sep 17 00:00:00 2001 From: Luke Parker <10430890+Hona@users.noreply.github.com> Date: Sun, 6 Dec 2026 16:14:10 +1000 Subject: [PATCH] fix(diff,review): track DiffDocState revisions in plugin deps and harden row bounds (#2562) --- .../src/plugin_host/native/custom_diff.rs | 4 ++ apps/desktop/src/plugin_host/native/diff.rs | 40 ++++++++++++++----- crates/opencode-code-view/src/diff/chrome.rs | 8 ++-- crates/opencode-code-view/src/diff/nav.rs | 7 +++- crates/opencode-code-view/src/diff/state.rs | 21 ++++++++-- crates/opencode-code-view/src/diff/view.rs | 11 +++-- features/review/src/mobile.rs | 4 ++ 7 files changed, 75 insertions(+), 20 deletions(-) diff --git a/apps/desktop/src/plugin_host/native/custom_diff.rs b/apps/desktop/src/plugin_host/native/custom_diff.rs index ab401dd1d..6d933b245 100644 --- a/apps/desktop/src/plugin_host/native/custom_diff.rs +++ b/apps/desktop/src/plugin_host/native/custom_diff.rs @@ -225,6 +225,8 @@ pub fn version(app: &App, id: u64) -> u64 { if let Some(e) = store.entries.get(&id) { ( e.stamp, + e.diffs.keys_rev(), + e.diffs.rev(), e.diffs.mode() == DiffMode::Split, e.diffs.expand_mode() == ExpandMode::Expand, e.diffs.only(), @@ -249,6 +251,8 @@ pub fn watched_stamp(app: &App) -> u64 { ( *id, e.stamp, + e.diffs.keys_rev(), + e.diffs.rev(), e.diffs.mode() == DiffMode::Split, e.diffs.expand_mode() == ExpandMode::Expand, e.diffs.only(), diff --git a/apps/desktop/src/plugin_host/native/diff.rs b/apps/desktop/src/plugin_host/native/diff.rs index b4f239761..f9a240df0 100644 --- a/apps/desktop/src/plugin_host/native/diff.rs +++ b/apps/desktop/src/plugin_host/native/diff.rs @@ -453,22 +453,42 @@ pub fn tree_version(app: &App, session: &str) -> u64 { } pub type FindVersion = (usize, usize, String, bool); -pub type DiffVersion = (u64, bool, bool, Option, Option); +pub type DiffVersion = (u64, u64, u64, bool, bool, Option, Option); -/// The version of what `Dep::Diff(session)` reads, the file heading stuck over the scrolled -/// diff included: scrolling past a file's heading sticks it without changing the diff. +/// The version of what `Dep::Diff(session)` reads: the session stamp (kept at `.0` for +/// `Diff.Changed`), `diffs.keys_rev()` / `diffs.rev()` (bumped when header collapse, fold +/// reveal, wrap toggle, or offline text failure rebuilds rows without touching `sr.stamp`), +/// the active modes, sticky header, and find bar. pub fn version(app: &App, session: &str) -> DiffVersion { - let sr = app.kernel.slots.get(&crate::review::REVIEW).sessions.get(session); + let review = app.kernel.slots.get(&crate::review::REVIEW); + let sr = review.sessions.get(session); // The find bar the diff draws over itself: open, its matches, its field. let find = sr.map(|sr| &sr.diffs).filter(|d| d.find_open()).map(|d| (d.find_count(), d.find_index(), d.find.input.text.clone(), d.find.input.focused)); - (sr.map_or(0, |sr| sr.stamp), app.kernel.slots.get(&crate::review::REVIEW).diff_mode == DiffMode::Split, app.kernel.slots.get(&crate::review::REVIEW).expand_mode == ExpandMode::Expand, sr.and_then(|sr| sr.diffs.sticky()), find) + ( + sr.map_or(0, |sr| sr.stamp), + sr.map_or(0, |sr| sr.diffs.keys_rev()), + sr.map_or(0, |sr| sr.diffs.rev()), + review.diff_mode == DiffMode::Split, + review.expand_mode == ExpandMode::Expand, + sr.and_then(|sr| sr.diffs.sticky()), + find, + ) } -/// The version of what `Dep::NarrowReview(session)` reads: the files open in the narrow list -/// and the session's line comments. -pub fn narrow_version(app: &App, session: &str) -> (Vec, u64) { - let open = app.kernel.slots.get(&crate::review::REVIEW).sessions.get(session).map(|sr| sr.narrow.open_paths()).unwrap_or_default(); - (open, app.kernel.slots.get(&crate::line_comments::LINE_COMMENTS).version(session)) +/// The version of what `Dep::NarrowReview(session)` reads: the session stamp, each open +/// narrow diff's path and row/wrap revisions, and the session's line comments. +pub fn narrow_version(app: &App, session: &str) -> u64 { + use std::hash::{Hash, Hasher}; + let mut h = std::hash::DefaultHasher::new(); + if let Some(sr) = app.kernel.slots.get(&crate::review::REVIEW).sessions.get(session) { + sr.stamp.hash(&mut h); + sr.narrow.open_diffs().len().hash(&mut h); + for (path, doc) in sr.narrow.open_diffs() { + (path, doc.keys_rev(), doc.rev()).hash(&mut h); + } + } + app.kernel.slots.get(&crate::line_comments::LINE_COMMENTS).version(session).hash(&mut h); + h.finish() } /// `ui.diffView`: the preview of `file`, else every file one under another; with `list`, the diff --git a/crates/opencode-code-view/src/diff/chrome.rs b/crates/opencode-code-view/src/diff/chrome.rs index 140290b37..de558efe7 100644 --- a/crates/opencode-code-view/src/diff/chrome.rs +++ b/crates/opencode-code-view/src/diff/chrome.rs @@ -361,8 +361,8 @@ impl Inner { } }; let after = st.selection(); - let path = st.files()[f].path().to_owned(); - if let Some(notes) = ¬es { + let path = notes.as_ref().and_then(|_| st.files().get(f).map(|file| file.path().to_owned())); + if let (Some(notes), Some(path)) = (¬es, path) { hand_off(notes.as_ref(), s, &path, before, after, ended.then_some(unselect), tx); } }) @@ -411,7 +411,9 @@ impl Inner { let before = st.selection(); st.click_code(f, at); let after = st.selection(); - let path = st.files()[f].path().to_owned(); + let Some(path) = st.files().get(f).map(|file| file.path().to_owned()) else { + return; + }; hand_off(notes.as_ref(), s, &path, before, after, Some(None), tx); }); } diff --git a/crates/opencode-code-view/src/diff/nav.rs b/crates/opencode-code-view/src/diff/nav.rs index 539696596..0877e682e 100644 --- a/crates/opencode-code-view/src/diff/nav.rs +++ b/crates/opencode-code-view/src/diff/nav.rs @@ -95,7 +95,12 @@ impl DiffDocState { } pub fn scroll_to_file(&mut self, file: usize, tx: &mut Tx) { - let Some(&r) = self.file_rows.get(file) else { return }; + if self.only.is_some_and(|o| o != file) { + return; + } + let Some(&r) = self.file_rows.get(file).filter(|&&r| (r as usize) < self.rows.len()) else { + return; + }; tx.instant(); self.reveal_at_top(r, tx); self.visible = file; diff --git a/crates/opencode-code-view/src/diff/state.rs b/crates/opencode-code-view/src/diff/state.rs index 403b8192d..480edb760 100644 --- a/crates/opencode-code-view/src/diff/state.rs +++ b/crates/opencode-code-view/src/diff/state.rs @@ -446,6 +446,14 @@ impl DiffDocState { &self.files } + pub fn rev(&self) -> u64 { + self.rev + } + + pub fn keys_rev(&self) -> u64 { + self.keys_rev + } + /// The rows' height one under another, unwrapped: a diff shown whole inside another scroll. /// A row laid out with comments under it counts as tall as it came out. pub fn rows_height(&self) -> f32 { @@ -565,11 +573,13 @@ impl DiffDocState { pub(crate) fn set_visible_rows(&mut self, rows: Range) -> (Option, bool) { let before_sticky = self.sticky(); - self.visible_rows = rows; + let len = self.rows.len(); + self.visible_rows = rows.start.min(len)..rows.end.min(len); if let Some(n) = self.nav && self.visible_rows.contains(&(n as usize)) + && let Some(&r) = self.rows.get(n as usize) { - self.visible = self.rows[n as usize].file(); + self.visible = r.file(); } else { self.nav = None; if let Some(&r) = self.rows.get(self.visible_rows.start) { @@ -605,8 +615,11 @@ impl DiffDocState { return None; } let f = self.visible; + if self.collapsed(f) || self.only.is_some_and(|o| o != f) { + return None; + } let header = *self.file_rows.get(f)? as usize; - (self.visible_rows.start > header).then_some(f) + (self.visible_rows.start > header && self.visible_rows.start < self.rows.len()).then_some(f) } /// Row `r`'s height: as laid out, else its kind's (a wrapped line, or one with comments @@ -746,7 +759,9 @@ impl DiffDocState { } } } + let len = rows.len(); self.rows = rows; + self.visible_rows = self.visible_rows.start.min(len)..self.visible_rows.end.min(len); self.refind(); } diff --git a/crates/opencode-code-view/src/diff/view.rs b/crates/opencode-code-view/src/diff/view.rs index 4681e73aa..af3523323 100644 --- a/crates/opencode-code-view/src/diff/view.rs +++ b/crates/opencode-code-view/src/diff/view.rs @@ -165,7 +165,10 @@ impl DiffViewer { } fn row_keys(get: Get) -> impl for<'a> Fn(&'a S, usize) -> Cow<'a, str> + 'static { - move |s, i| Cow::Borrowed(get(s).keys[i].as_str()) + move |s, i| match get(s).keys.get(i) { + Some(k) => Cow::Borrowed(k.as_str()), + None => Cow::Owned(format!("df-stale-{i}")), + } } #[derive(Clone, Copy)] @@ -367,8 +370,10 @@ fn annotation_wrap(pal: &Pal, st: &DiffDocState, f: usize, gutter: f32, line: El impl Inner { fn row(&self, s: &S, i: usize) -> El { let st = (self.d.get)(s); - let r = st.rows[i]; - let key = st.keys[i].as_str(); + let (Some(&r), Some(key)) = (st.rows.get(i), st.keys.get(i)) else { + return el(); + }; + let key = key.as_str(); match r { DiffRow::File(f) if self.d.header_actions.is_none() => { let fu = f as usize; diff --git a/features/review/src/mobile.rs b/features/review/src/mobile.rs index 3d9456560..8a2fd09da 100644 --- a/features/review/src/mobile.rs +++ b/features/review/src/mobile.rs @@ -31,6 +31,10 @@ impl NarrowReview { self.open.iter().map(|(p, _)| p.clone()).collect() } + pub fn open_diffs(&self) -> &[(String, DiffDocState)] { + &self.open + } + pub fn is_open(&self, path: &str) -> bool { self.open.iter().any(|(p, _)| p == path) }