From 31f3d08d638c0785300a11aa44373b8ff9f66d08 Mon Sep 17 00:00:00 2001 From: Lorenz Leutgeb Date: Mon, 17 Aug 2026 18:31:25 +0200 Subject: [PATCH] radicle/cob/thread: Use `timeline: IndexSet` Plenty conservative `debug_assert!`s, cause panics as soon as two actions that should be added to the timeline appear within the same operation. But there is no code in place to prevent this. It also does not seem to be problematic to visit the same operation multiple times (once per contained action). A set that retains iteration order does the job. Analysis of this bug was done over Zulip, see . `Update::Reviewed::review` is boxed due to: error: large size difference between variants --> crates/radicle-cli/src/terminal/patch/timeline.rs:158:1 | 158 | / enum Update<'a> { 159 | | /// A revision of the patch was reviewed. 160 | | Reviewed { review: Review }, | | --------------------------- the largest variant contains at least 352 bytes 161 | | /// A revision of the patch was merged. 162 | |/ Merged { 163 | || author: Author<'a>, 164 | || /// If the merge is none, this means that it was a fast-forward merge. 165 | || merge: Option, 166 | || }, | ||_____- the second-largest variant contains at least 120 bytes 167 | | } | |__^ the entire enum is at least 352 bytes --- .../src/terminal/patch/timeline.rs | 4 +- crates/radicle/src/cob/thread.rs | 77 +++++++++++++++---- 2 files changed, 64 insertions(+), 17 deletions(-) diff --git a/crates/radicle-cli/src/terminal/patch/timeline.rs b/crates/radicle-cli/src/terminal/patch/timeline.rs index c4250bcd3..9a8d8d6a4 100644 --- a/crates/radicle-cli/src/terminal/patch/timeline.rs +++ b/crates/radicle-cli/src/terminal/patch/timeline.rs @@ -74,7 +74,7 @@ impl<'a> RevisionEntry<'a> { ( review.timestamp(), Update::Reviewed { - review: review.clone(), + review: Box::new(review.clone()), }, ) })); @@ -157,7 +157,7 @@ impl<'a> RevisionEntry<'a> { /// An update in the [`Patch`]'s timeline. enum Update<'a> { /// A revision of the patch was reviewed. - Reviewed { review: Review }, + Reviewed { review: Box }, /// A revision of the patch was merged. Merged { author: Author<'a>, diff --git a/crates/radicle/src/cob/thread.rs b/crates/radicle/src/cob/thread.rs index 0368389ac..451376e70 100644 --- a/crates/radicle/src/cob/thread.rs +++ b/crates/radicle/src/cob/thread.rs @@ -3,6 +3,7 @@ use std::collections::{BTreeMap, BTreeSet}; use std::str::FromStr; use std::sync::LazyLock; +use indexmap::IndexSet; use serde::{Deserialize, Serialize, ser::SerializeStruct}; use thiserror::Error; @@ -291,14 +292,14 @@ pub struct Thread { /// The comments under the thread. comments: BTreeMap>, /// Comment timeline. - timeline: Vec, + timeline: IndexSet, } impl Default for Thread { fn default() -> Self { Self { comments: BTreeMap::default(), - timeline: Vec::default(), + timeline: IndexSet::default(), } } } @@ -313,7 +314,7 @@ impl Thread { pub fn new(id: CommentId, comment: T) -> Self { Self { comments: BTreeMap::from_iter([(id, Some(comment))]), - timeline: vec![id], + timeline: [id].into(), } } @@ -512,8 +513,7 @@ pub fn comment( { return Err(Error::Missing(id)); } - debug_assert!(!thread.timeline.contains(&id)); - thread.timeline.push(id); + thread.timeline.insert(id); // Nb. If a comment is already present, it must be redacted, because the // underlying store guarantees exactly-once delivery of ops. @@ -536,8 +536,7 @@ pub fn edit( body: String, embeds: Vec>, ) -> Result<(), Error> { - debug_assert!(!thread.timeline.contains(&id)); - thread.timeline.push(id); + thread.timeline.insert(id); // It's possible for a comment to be redacted before we're able to edit it, in // case of a concurrent update. @@ -556,8 +555,7 @@ pub fn edit( pub fn redact(thread: &mut Thread, id: EntryId, comment: EntryId) -> Result<(), Error> { if let Some(comment) = thread.comments.get_mut(&comment) { - debug_assert!(!thread.timeline.contains(&id)); - thread.timeline.push(id); + thread.timeline.insert(id); *comment = None; } else { @@ -579,8 +577,7 @@ pub fn react( return Err(Error::Missing(comment)); }; if let Some(comment) = comment { - debug_assert!(!thread.timeline.contains(&id)); - thread.timeline.push(id); + thread.timeline.insert(id); if active { comment.reactions.insert(key); @@ -601,8 +598,7 @@ pub fn resolve( }; if let Some(comment) = comment { - debug_assert!(!thread.timeline.contains(&id)); - thread.timeline.push(id); + thread.timeline.insert(id); comment.resolve(); } Ok(()) @@ -618,8 +614,7 @@ pub fn unresolve( }; if let Some(comment) = comment { - debug_assert!(!thread.timeline.contains(&id)); - thread.timeline.push(id); + thread.timeline.insert(id); comment.unresolve(); } Ok(()) @@ -713,6 +708,58 @@ mod tests { assert_eq!(t1.comment(&c0.id()).unwrap().body(), "Goodbye world!"); } + /// Regression test for commit `1bb04020a4afb4b7a071dad9d0a06791c087fb3a` + /// in `rad:z2reN9XFdJgQmSbh23KUp9va689YJ`, which occurs in patch + /// `0978f920a494b52744a2b349b3453a42a8106513`. + /// + /// Generally, operations may carry multiple actions, thus, multiple actions + /// might be referred to by the same ID. + #[test] + fn resolve_and_unresolve_multiple_comments_in_one_operation() { + let mut alice = SigningKey::mock(49); + let repo = r#gen::(1); + + let comment_1 = alice.comment("First", None); + let comment_2 = alice.comment("Second", None); + let mut thread = Thread::from_ops([comment_1.clone(), comment_2.clone()], &repo).unwrap(); + + // Note that the same `operation` is used to resolve *both* + // `comment_1` and `comment_2`. + let operation = arbitrary::entry_id(); + + resolve(&mut thread, operation, comment_1.id()).unwrap(); + resolve(&mut thread, operation, comment_2.id()).unwrap(); + + assert!(thread.comment(&comment_1.id()).unwrap().is_resolved()); + assert!(thread.comment(&comment_2.id()).unwrap().is_resolved()); + assert_eq!( + thread + .timeline + .iter() + .filter(|id| **id == operation) + .count(), + 1 + ); + + // Note that the same `operation` is used to unresolve *both* + // `comment_1` and `comment_2`. + let operation = arbitrary::entry_id(); + + unresolve(&mut thread, operation, comment_1.id()).unwrap(); + unresolve(&mut thread, operation, comment_2.id()).unwrap(); + + assert!(!thread.comment(&comment_1.id()).unwrap().is_resolved()); + assert!(!thread.comment(&comment_2.id()).unwrap().is_resolved()); + assert_eq!( + thread + .timeline + .iter() + .filter(|id| **id == operation) + .count(), + 1 + ); + } + #[test] fn test_timeline() { let alice = SigningKey::mock(94);