brain: Keep failed comment changes retryable
444c45c9ccb6dc6b39b73995246ed568deba6306
Stage comment mutations until their encoded store is persisted, and keep an active draft open when persistence fails. This prevents the displayed comment state from diverging from the hidden ref while preserving the user's retry path. Assisted-by: Codex:gpt-5.6-sol
1 parent
080440f3
crates/brain/src/comments.rs
+49 -0
| 261 | 261 | .collect(); |
|
| 262 | 262 | ||
| 263 | 263 | Self { by_hunk, targets } |
|
| 264 | 264 | } |
|
| 265 | 265 | ||
| 266 | + | /// Applies one comment mutation only after `persist` accepts its encoded |
|
| 267 | + | /// store, leaving both the store and this view unchanged on failure. |
|
| 268 | + | pub fn transact<E>( |
|
| 269 | + | &mut self, |
|
| 270 | + | store: &mut ReviewComments, |
|
| 271 | + | update: impl FnOnce(&mut Self, &mut ReviewComments) -> bool, |
|
| 272 | + | persist: impl FnOnce(&str) -> std::result::Result<(), E>, |
|
| 273 | + | ) -> std::result::Result<bool, E> { |
|
| 274 | + | let mut updated_store = store.clone(); |
|
| 275 | + | let mut updated_view = self.clone(); |
|
| 276 | + | if !update(&mut updated_view, &mut updated_store) { |
|
| 277 | + | return Ok(false); |
|
| 278 | + | } |
|
| 279 | + | let encoded = updated_store.encode(); |
|
| 280 | + | persist(&encoded)?; |
|
| 281 | + | *store = updated_store; |
|
| 282 | + | *self = updated_view; |
|
| 283 | + | Ok(true) |
|
| 284 | + | } |
|
| 285 | + | ||
| 266 | 286 | /// Authors a comment on `hunk` into `store`, returning whether one was |
|
| 267 | 287 | /// added. The caller is responsible for persisting the updated store. |
|
| 268 | 288 | pub fn author(&mut self, store: &mut ReviewComments, hunk: HunkId, body: String) -> bool { |
|
| 269 | 289 | let Some(target) = self.targets.get(&hunk) else { |
|
| 270 | 290 | return false; |
| 454 | 474 | assert!(comments.delete(&mut store, HUNK, &id)); |
|
| 455 | 475 | ||
| 456 | 476 | assert!(comments.for_hunk(HUNK).is_empty()); |
|
| 457 | 477 | assert!(store.encode().is_empty()); |
|
| 458 | 478 | } |
|
| 479 | + | ||
| 480 | + | #[test] |
|
| 481 | + | fn failed_comment_persistence_keeps_store_and_view_unchanged() { |
|
| 482 | + | let (mut store, mut comments) = empty_comments(); |
|
| 483 | + | assert!(comments.author(&mut store, HUNK, "old".to_owned())); |
|
| 484 | + | let before = store.encode(); |
|
| 485 | + | let original = comments.for_hunk(HUNK).to_vec(); |
|
| 486 | + | let id = original[0].id.clone(); |
|
| 487 | + | ||
| 488 | + | let edited = comments.transact( |
|
| 489 | + | &mut store, |
|
| 490 | + | |comments, store| comments.edit(store, HUNK, &id, "new".to_owned()), |
|
| 491 | + | |_| Err::<(), _>("write failed"), |
|
| 492 | + | ); |
|
| 493 | + | ||
| 494 | + | assert!(edited.is_err()); |
|
| 495 | + | assert_eq!(store.encode(), before); |
|
| 496 | + | assert_eq!(comments.for_hunk(HUNK), original); |
|
| 497 | + | ||
| 498 | + | let deleted = comments.transact( |
|
| 499 | + | &mut store, |
|
| 500 | + | |comments, store| comments.delete(store, HUNK, &id), |
|
| 501 | + | |_| Err::<(), _>("write failed"), |
|
| 502 | + | ); |
|
| 503 | + | ||
| 504 | + | assert!(deleted.is_err()); |
|
| 505 | + | assert_eq!(store.encode(), before); |
|
| 506 | + | assert_eq!(comments.for_hunk(HUNK), original); |
|
| 507 | + | } |
|
| 459 | 508 | } |
crates/brain/src/session.rs
+20 -9
| 815 | 815 | fn save_comment(&mut self) -> Result<(), Error> { |
|
| 816 | 816 | let Mode::Hunk(view) = &mut self.mode else { |
|
| 817 | 817 | return Ok(()); |
|
| 818 | 818 | }; |
|
| 819 | 819 | let hunk = view.hunk; |
|
| 820 | - | let Some((target, body)) = view.finish_edit() else { |
|
| 820 | + | let Some((target, body)) = view.pending_edit() else { |
|
| 821 | + | view.cancel_edit(); |
|
| 821 | 822 | return Ok(()); |
|
| 822 | 823 | }; |
|
| 823 | 824 | let Some(loaded) = &mut self.commits[self.current].loaded else { |
|
| 824 | 825 | return Ok(()); |
|
| 825 | 826 | }; |
|
| 826 | 827 | let added = matches!(target, CommentEdit::Add); |
|
| 827 | - | let changed = match target { |
|
| 828 | - | CommentEdit::Add => loaded.comments.author(&mut self.comments, hunk, body), |
|
| 829 | - | CommentEdit::Edit(id) => loaded.comments.edit(&mut self.comments, hunk, &id, body), |
|
| 830 | - | }; |
|
| 828 | + | let brain = &self.brain; |
|
| 829 | + | let changed = loaded.comments.transact( |
|
| 830 | + | &mut self.comments, |
|
| 831 | + | |comments, store| match target { |
|
| 832 | + | CommentEdit::Add => comments.author(store, hunk, body), |
|
| 833 | + | CommentEdit::Edit(id) => comments.edit(store, hunk, &id, body), |
|
| 834 | + | }, |
|
| 835 | + | |encoded| brain.write_comments(encoded), |
|
| 836 | + | )?; |
|
| 831 | 837 | if changed { |
|
| 832 | - | self.brain.write_comments(&self.comments.encode())?; |
|
| 833 | 838 | loaded.refresh_decorations(); |
|
| 834 | 839 | if added { |
|
| 835 | 840 | let comments = loaded.comments.for_hunk(hunk).len(); |
|
| 836 | 841 | view.select_comment(comments.saturating_sub(1)); |
|
| 837 | 842 | } |
|
| 838 | 843 | } |
|
| 844 | + | view.cancel_edit(); |
|
| 839 | 845 | Ok(()) |
|
| 840 | 846 | } |
|
| 841 | 847 | ||
| 842 | 848 | /// Returns the number of comments on the active hunk. |
|
| 843 | 849 | fn hunk_comment_count(&self) -> usize { |
| 913 | 919 | view.clamp_selection(comments.len()); |
|
| 914 | 920 | let Some(comment) = comments.get(view.selected()) else { |
|
| 915 | 921 | return Ok(()); |
|
| 916 | 922 | }; |
|
| 917 | 923 | let id = comment.id.clone(); |
|
| 918 | - | if loaded.comments.delete(&mut self.comments, view.hunk, &id) { |
|
| 919 | - | self.brain.write_comments(&self.comments.encode())?; |
|
| 924 | + | let hunk = view.hunk; |
|
| 925 | + | let brain = &self.brain; |
|
| 926 | + | if loaded.comments.transact( |
|
| 927 | + | &mut self.comments, |
|
| 928 | + | |comments, store| comments.delete(store, hunk, &id), |
|
| 929 | + | |encoded| brain.write_comments(encoded), |
|
| 930 | + | )? { |
|
| 920 | 931 | loaded.refresh_decorations(); |
|
| 921 | - | let comments = loaded.comments.for_hunk(view.hunk).len(); |
|
| 932 | + | let comments = loaded.comments.for_hunk(hunk).len(); |
|
| 922 | 933 | view.clamp_selection(comments); |
|
| 923 | 934 | } |
|
| 924 | 935 | Ok(()) |
|
| 925 | 936 | } |
|
| 926 | 937 |
crates/brain/src/session/hunk.rs
+3 -7
| 97 | 97 | self.mode = HunkMode::Browsing { |
|
| 98 | 98 | selected: self.selected(), |
|
| 99 | 99 | }; |
|
| 100 | 100 | } |
|
| 101 | 101 | ||
| 102 | - | /// Finishes the active draft, returning the target and body. |
|
| 103 | - | pub(super) fn finish_edit(&mut self) -> Option<(CommentEdit, String)> { |
|
| 102 | + | /// Returns the active non-empty draft without closing its editor. |
|
| 103 | + | pub(super) fn pending_edit(&self) -> Option<(CommentEdit, String)> { |
|
| 104 | 104 | let HunkMode::Editing { target, input, .. } = &self.mode else { |
|
| 105 | 105 | return None; |
|
| 106 | 106 | }; |
|
| 107 | 107 | if input.is_empty() { |
|
| 108 | - | self.cancel_edit(); |
|
| 109 | 108 | return None; |
|
| 110 | 109 | } |
|
| 111 | - | let target = target.clone(); |
|
| 112 | - | let body = input.value().to_owned(); |
|
| 113 | - | self.cancel_edit(); |
|
| 114 | 110 | ||
| 115 | - | Some((target, body)) |
|
| 111 | + | Some((target.clone(), input.value().to_owned())) |
|
| 116 | 112 | } |
|
| 117 | 113 | ||
| 118 | 114 | /// Applies an edit key to the draft comment. |
|
| 119 | 115 | pub(super) fn input(&mut self, key: rustik_tui::Key) -> bool { |
|
| 120 | 116 | let HunkMode::Editing { input, .. } = &mut self.mode else { |
a.txt
+0 -0