brain: Distinguish duplicate comment targets
84f14f01c58c82b6127e7df94fd623ba9ccff926
Persist each patch occurrence ordinal with its comment target so identical hunks in one commit cannot share comment mutations. Assisted-by: Codex:gpt-5.6-sol
1 parent
c26212b9
crates/brain/src/comments.rs
+49 -3
| 89 | 89 | pub heading: String, |
|
| 90 | 90 | /// Optional file path associated with the hunk. |
|
| 91 | 91 | pub path: Option<PathBuf>, |
|
| 92 | 92 | /// Stable patch id of the hunk the comment is attached to. |
|
| 93 | 93 | pub patch_id: PatchId, |
|
| 94 | + | /// Occurrence of this patch id within the commit. |
|
| 95 | + | pub ordinal: u32, |
|
| 94 | 96 | } |
|
| 95 | 97 | ||
| 96 | 98 | /// Persistent comment store for one review identity. |
|
| 97 | 99 | #[derive(Clone, Debug)] |
|
| 98 | 100 | pub struct ReviewComments { |
| 115 | 117 | heading: String, |
|
| 116 | 118 | /// Optional file path associated with the hunk. |
|
| 117 | 119 | path: Option<PathBuf>, |
|
| 118 | 120 | /// Stable patch id of the hunk the comment is attached to. |
|
| 119 | 121 | patch_id: PatchId, |
|
| 122 | + | /// Occurrence of this patch id within the commit. |
|
| 123 | + | ordinal: u32, |
|
| 120 | 124 | /// Comment body. |
|
| 121 | 125 | body: String, |
|
| 122 | 126 | /// Agent update waiting for reviewer re-review. |
|
| 123 | 127 | pending_updates: Option<PendingUpdates>, |
|
| 124 | 128 | } |
| 174 | 178 | /// Returns the number of stored comment rows. |
|
| 175 | 179 | pub fn len(&self) -> usize { |
|
| 176 | 180 | self.records.len() |
|
| 177 | 181 | } |
|
| 178 | 182 | ||
| 179 | - | /// Returns comments attached to `target`'s patch id, in storage order. |
|
| 183 | + | /// Returns comments attached to `target`'s patch occurrence, in storage order. |
|
| 180 | 184 | fn matching(&self, target: &CommentTarget) -> Vec<Comment> { |
|
| 181 | 185 | self.records |
|
| 182 | 186 | .iter() |
|
| 183 | - | .filter(|record| record.patch_id == target.patch_id) |
|
| 187 | + | .filter(|record| record.patch_id == target.patch_id && record.ordinal == target.ordinal) |
|
| 184 | 188 | .map(Comment::from) |
|
| 185 | 189 | .collect() |
|
| 186 | 190 | } |
|
| 187 | 191 | ||
| 188 | 192 | /// Adds one comment row for `target` and returns its view. |
| 196 | 200 | author: self.author.clone(), |
|
| 197 | 201 | timestamp, |
|
| 198 | 202 | heading: target.heading.clone(), |
|
| 199 | 203 | path: target.path.clone(), |
|
| 200 | 204 | patch_id: target.patch_id.clone(), |
|
| 205 | + | ordinal: target.ordinal, |
|
| 201 | 206 | body, |
|
| 202 | 207 | pending_updates: None, |
|
| 203 | 208 | }; |
|
| 204 | 209 | let comment = Comment::from(&record); |
|
| 205 | 210 | self.records.push(record); |
| 418 | 423 | mod tests { |
|
| 419 | 424 | use super::*; |
|
| 420 | 425 | ||
| 421 | 426 | /// Hunk used by comment view tests. |
|
| 422 | 427 | const HUNK: HunkId = HunkId::new(0); |
|
| 428 | + | /// Second hunk used by comment view tests. |
|
| 429 | + | const OTHER_HUNK: HunkId = HunkId::new(1); |
|
| 423 | 430 | ||
| 424 | 431 | /// Builds an empty comment store and matching hunk view. |
|
| 425 | 432 | fn empty_comments() -> (ReviewComments, Comments) { |
|
| 426 | 433 | let target = CommentTarget { |
|
| 427 | 434 | heading: "h".to_owned(), |
|
| 428 | 435 | path: None, |
|
| 429 | 436 | patch_id: PatchId::from("p".to_owned()), |
|
| 437 | + | ordinal: 0, |
|
| 430 | 438 | }; |
|
| 431 | 439 | let mut targets = BTreeMap::new(); |
|
| 432 | 440 | targets.insert(HUNK, target); |
|
| 433 | 441 | let store = ReviewComments::parse("").unwrap(); |
|
| 434 | 442 | let comments = Comments::new(&store, targets); |
| 444 | 452 | } |
|
| 445 | 453 | ||
| 446 | 454 | #[test] |
|
| 447 | 455 | fn next_id_continues_above_stored_ids() { |
|
| 448 | 456 | let store = ReviewComments::parse( |
|
| 449 | - | r#"{"id":7,"author":"A","timestamp":1,"heading":"h","path":null,"patch_id":"p","body":"old","pending_updates":null}"#, |
|
| 457 | + | r#"{"id":7,"author":"A","timestamp":1,"heading":"h","path":null,"patch_id":"p","ordinal":0,"body":"old","pending_updates":null}"#, |
|
| 450 | 458 | ) |
|
| 451 | 459 | .unwrap(); |
|
| 452 | 460 | ||
| 453 | 461 | assert_eq!(store.next_id(), CommentId::from(8)); |
|
| 454 | 462 | } |
| 475 | 483 | ||
| 476 | 484 | assert!(comments.for_hunk(HUNK).is_empty()); |
|
| 477 | 485 | assert!(store.encode().is_empty()); |
|
| 478 | 486 | } |
|
| 479 | 487 | ||
| 488 | + | #[test] |
|
| 489 | + | fn identical_patch_occurrences_keep_comments_separate() { |
|
| 490 | + | let first = CommentTarget { |
|
| 491 | + | heading: "h".to_owned(), |
|
| 492 | + | path: Some(PathBuf::from("file")), |
|
| 493 | + | patch_id: PatchId::from("same".to_owned()), |
|
| 494 | + | ordinal: 0, |
|
| 495 | + | }; |
|
| 496 | + | let second = CommentTarget { |
|
| 497 | + | ordinal: 1, |
|
| 498 | + | ..first.clone() |
|
| 499 | + | }; |
|
| 500 | + | let targets = BTreeMap::from([(HUNK, first), (OTHER_HUNK, second)]); |
|
| 501 | + | let mut store = ReviewComments::parse("").unwrap(); |
|
| 502 | + | let mut comments = Comments::new(&store, targets.clone()); |
|
| 503 | + | ||
| 504 | + | assert!(comments.author(&mut store, HUNK, "first occurrence".to_owned())); |
|
| 505 | + | ||
| 506 | + | let mut reopened = Comments::new(&store, targets.clone()); |
|
| 507 | + | assert_eq!(reopened.for_hunk(HUNK)[0].body, "first occurrence"); |
|
| 508 | + | assert!(reopened.for_hunk(OTHER_HUNK).is_empty()); |
|
| 509 | + | ||
| 510 | + | assert!(reopened.author(&mut store, OTHER_HUNK, "second occurrence".to_owned())); |
|
| 511 | + | let first_id = reopened.for_hunk(HUNK)[0].id.clone(); |
|
| 512 | + | let second_id = reopened.for_hunk(OTHER_HUNK)[0].id.clone(); |
|
| 513 | + | assert!(reopened.edit( |
|
| 514 | + | &mut store, |
|
| 515 | + | OTHER_HUNK, |
|
| 516 | + | &second_id, |
|
| 517 | + | "edited second".to_owned(), |
|
| 518 | + | )); |
|
| 519 | + | assert!(reopened.delete(&mut store, HUNK, &first_id)); |
|
| 520 | + | ||
| 521 | + | let reopened = Comments::new(&store, targets); |
|
| 522 | + | assert!(reopened.for_hunk(HUNK).is_empty()); |
|
| 523 | + | assert_eq!(reopened.for_hunk(OTHER_HUNK)[0].body, "edited second"); |
|
| 524 | + | } |
|
| 525 | + | ||
| 480 | 526 | #[test] |
|
| 481 | 527 | fn failed_comment_persistence_keeps_store_and_view_unchanged() { |
|
| 482 | 528 | let (mut store, mut comments) = empty_comments(); |
|
| 483 | 529 | assert!(comments.author(&mut store, HUNK, "old".to_owned())); |
|
| 484 | 530 | let before = store.encode(); |
crates/brain/src/session/commit.rs
+1 -0
| 125 | 125 | hunk.id, |
|
| 126 | 126 | CommentTarget { |
|
| 127 | 127 | heading: heading.clone(), |
|
| 128 | 128 | path: path.clone(), |
|
| 129 | 129 | patch_id: identity.patch_id.clone(), |
|
| 130 | + | ordinal: identity.ordinal, |
|
| 130 | 131 | }, |
|
| 131 | 132 | ); |
|
| 132 | 133 | } |
|
| 133 | 134 | } |
|
| 134 | 135 | let comments = Comments::new(store, targets); |
a.rs
+0 -0