Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(sync-service): keep blame when a comment mark is added or removed (#9019)

Wolf Mermelstein96d72f13f481
diff --git a/services/sync-service/src/state.rs b/services/sync-service/src/state.rs
index 997eacb6e3..3d049de8b5 100644
--- a/services/sync-service/src/state.rs
+++ b/services/sync-service/src/state.rs
@@ -1,7 +1,8 @@
use std::{borrow::Cow, collections::HashSet, sync::Mutex};
use loro::{
- Container, ContainerID, ExportMode, Frontiers, ID, LoroDoc, LoroValue, ToJson, VersionVector,
+ Container, ContainerID, ExportMode, Frontiers, ID, IdSpan, JsonMapOp, JsonMovableListOp,
+ JsonOpContent, LoroDoc, LoroValue, ToJson, VersionVector,
};
use tracing::debug;
use web_time::Instant;
@@ -15,12 +16,15 @@ mod test;
const FROM_CLIENT_TAG: &str = "from_client";
const FROM_SERVICE_TAG: &str = "from_service";
const FRONTIERS_ID_SEPERATOR: &str = "|";
+/// The Lexical `type` of the node that wraps text carrying a comment mark.
+const COMMENT_NODE_TYPE: &str = "comment";
/// Result of applying an update, before any persistence awaits.
pub struct ImportedUpdate {
/// Whether the operation log gained new operations.
pub changed: bool,
- /// Lexical node IDs used by the existing blame tracking.
+ /// Lexical node IDs the update edited, for blame tracking. Empty when the
+ /// update only adds or removes a comment mark.
pub touched_nodes: Vec<String>,
}
@@ -158,8 +162,13 @@ impl DocumentState {
/// so it reads only those operations: diffing the two versions checks the
/// document out and costs time that grows with its history.
fn touched_lexical_ids(&self, before: &VersionVector, after: &VersionVector) -> Vec<String> {
+ let spans: Vec<IdSpan> = after.sub_iter(before).collect();
+ if spans.iter().any(|span| self.changes_comment_marks(*span)) {
+ debug!("update adds or removes a comment mark; recording no blame");
+ return Vec::new();
+ }
let mut containers = HashSet::new();
- for span in after.sub_iter(before) {
+ for span in spans {
let Ok(len) = usize::try_from(span.counter.end - span.counter.start) else {
continue;
};
@@ -177,6 +186,54 @@ impl DocumentState {
touched
}
+ /// Whether the operations in `span` add or remove a comment mark. Lexical
+ /// moves marked text into a `comment` node, which the client's mirror
+ /// writes as recreated or split text nodes; removing the mark merges them
+ /// back. Neither writes new text, so it must not take over the text's blame.
+ fn changes_comment_marks(&self, span: IdSpan) -> bool {
+ self.loro_doc
+ .export_json_in_id_span(span)
+ .iter()
+ .flat_map(|change| &change.ops)
+ .any(|op| match &op.content {
+ JsonOpContent::Map(JsonMapOp::Insert { key, value }) => {
+ key == "type" && is_comment_type(value)
+ }
+ JsonOpContent::MovableList(JsonMovableListOp::Delete { len, start_id, .. }) => {
+ (0..len.abs()).any(|offset| self.is_comment_node(start_id.inc(offset)))
+ }
+ _ => false,
+ })
+ }
+
+ /// Whether the list item created by the operation `item` holds a comment
+ /// node. Loro keeps a deleted container's state, so this also answers for
+ /// items the update removed. Items moved within their list, or older than
+ /// a shallow snapshot, read as not comments.
+ fn is_comment_node(&self, item: ID) -> bool {
+ let inserted = self
+ .loro_doc
+ .export_json_in_id_span(IdSpan::new(item.peer, item.counter, item.counter + 1))
+ .into_iter()
+ .flat_map(|change| change.ops)
+ .find_map(|op| match op.content {
+ JsonOpContent::MovableList(JsonMovableListOp::Insert { value, .. }) => {
+ let index = usize::try_from(item.counter - op.counter).ok()?;
+ value.into_iter().nth(index)
+ }
+ _ => None,
+ });
+ let Some(LoroValue::Container(container_id)) = inserted else {
+ return false;
+ };
+ let Some(Container::Map(node)) = self.loro_doc.get_container(container_id) else {
+ return false;
+ };
+ node.get("type")
+ .and_then(|kind| kind.into_value().ok())
+ .is_some_and(|kind| is_comment_type(&kind))
+ }
+
/// Walk from a changed container up to the nearest ancestor LoroMap whose
/// `$` submap has an `id` — that string is the Lexical node ID.
fn find_lexical_id(&self, container_id: &ContainerID) -> Option<String> {
@@ -259,6 +316,10 @@ impl DocumentState {
}
}
+fn is_comment_type(value: &LoroValue) -> bool {
+ matches!(value, LoroValue::String(kind) if kind.as_str() == COMMENT_NODE_TYPE)
+}
+
fn deep_json(doc: &LoroDoc) -> Result<serde_json::Value> {
serde_json::from_str(&doc.get_deep_value().to_json()).context("failed to read document json")
}
diff --git a/services/sync-service/src/state/test.rs b/services/sync-service/src/state/test.rs
index 355d7bd3d3..c89075aaec 100644
--- a/services/sync-service/src/state/test.rs
+++ b/services/sync-service/src/state/test.rs
@@ -1,4 +1,4 @@
-use loro::{LoroMap, LoroMovableList, LoroText};
+use loro::{LoroList, LoroMap, LoroMovableList, LoroText};
use super::*;
@@ -62,6 +62,203 @@ fn imported_updates_report_the_lexical_nodes_they_touch() {
assert_eq!(unblocking.touched_nodes, ["body", "heading"]);
}
+/// One user writes a paragraph next to another's. The second user comments
+/// on the author's text and later deletes the comment. The client's mirror
+/// rewrites the author's text node each time, but the commenter wrote none of
+/// it, so blame must not move.
+#[test]
+fn comment_marks_keep_the_text_authors_blame() {
+ const AUTHOR: u64 = 1;
+ const COMMENTER: u64 = 2;
+ let state = DocumentState::new();
+ let client = LoroDoc::new();
+ client.set_peer_id(AUTHOR).unwrap();
+ let root_children = client
+ .get_map("root")
+ .insert_container("children", LoroMovableList::new())
+ .unwrap();
+ let author_paragraph = root_children.push_container(LoroMap::new()).unwrap();
+ author_paragraph.insert("type", "paragraph").unwrap();
+ author_paragraph
+ .insert_container("$", LoroMap::new())
+ .unwrap()
+ .insert("id", "p-author")
+ .unwrap();
+ let author_children = author_paragraph
+ .insert_container("children", LoroMovableList::new())
+ .unwrap();
+ let author_text = author_children.push_container(LoroMap::new()).unwrap();
+ author_text.insert("type", "text").unwrap();
+ author_text
+ .insert_container("$", LoroMap::new())
+ .unwrap()
+ .insert("id", "t-author")
+ .unwrap();
+ author_text
+ .insert_container("text", LoroText::new())
+ .unwrap()
+ .insert(0, "author wrote this")
+ .unwrap();
+ client.commit();
+ client.set_peer_id(COMMENTER).unwrap();
+ let commenter_paragraph = root_children.push_container(LoroMap::new()).unwrap();
+ commenter_paragraph.insert("type", "paragraph").unwrap();
+ commenter_paragraph
+ .insert_container("$", LoroMap::new())
+ .unwrap()
+ .insert("id", "p-commenter")
+ .unwrap();
+ let commenter_text = commenter_paragraph
+ .insert_container("children", LoroMovableList::new())
+ .unwrap()
+ .push_container(LoroMap::new())
+ .unwrap();
+ commenter_text.insert("type", "text").unwrap();
+ commenter_text
+ .insert_container("$", LoroMap::new())
+ .unwrap()
+ .insert("id", "t-commenter")
+ .unwrap();
+ commenter_text
+ .insert_container("text", LoroText::new())
+ .unwrap()
+ .insert(0, "commenter wrote this")
+ .unwrap();
+ client.commit();
+ let written = state
+ .import(&client.export(ExportMode::all_updates()).unwrap())
+ .unwrap();
+ assert_eq!(
+ written.touched_nodes,
+ ["p-author", "p-commenter", "t-author", "t-commenter"]
+ );
+
+ // The commenter marks all of the author's text: the mirror deletes the
+ // text node and recreates it, same id and text, inside a comment node.
+ let seen = client.oplog_vv();
+ author_children.delete(0, 1).unwrap();
+ let comment = author_children.insert_container(0, LoroMap::new()).unwrap();
+ comment.insert("type", "comment").unwrap();
+ comment
+ .insert_container("$", LoroMap::new())
+ .unwrap()
+ .insert("id", "c-whole")
+ .unwrap();
+ comment
+ .insert_container("ids", LoroList::new())
+ .unwrap()
+ .push("mark-1")
+ .unwrap();
+ let wrapped = comment
+ .insert_container("children", LoroMovableList::new())
+ .unwrap()
+ .push_container(LoroMap::new())
+ .unwrap();
+ wrapped.insert("type", "text").unwrap();
+ wrapped
+ .insert_container("$", LoroMap::new())
+ .unwrap()
+ .insert("id", "t-author")
+ .unwrap();
+ wrapped
+ .insert_container("text", LoroText::new())
+ .unwrap()
+ .insert(0, "author wrote this")
+ .unwrap();
+ client.commit();
+ let commented = state
+ .import(&client.export(ExportMode::updates(&seen)).unwrap())
+ .unwrap();
+ assert!(commented.changed);
+ assert!(commented.touched_nodes.is_empty());
+
+ // The commenter deletes the comment: the comment node goes and the text
+ // node is recreated directly in the paragraph again.
+ let seen = client.oplog_vv();
+ author_children.delete(0, 1).unwrap();
+ let unwrapped = author_children.insert_container(0, LoroMap::new()).unwrap();
+ unwrapped.insert("type", "text").unwrap();
+ unwrapped
+ .insert_container("$", LoroMap::new())
+ .unwrap()
+ .insert("id", "t-author")
+ .unwrap();
+ let unwrapped_text = unwrapped.insert_container("text", LoroText::new()).unwrap();
+ unwrapped_text.insert(0, "author wrote this").unwrap();
+ client.commit();
+ let uncommented = state
+ .import(&client.export(ExportMode::updates(&seen)).unwrap())
+ .unwrap();
+ assert!(uncommented.changed);
+ assert!(uncommented.touched_nodes.is_empty());
+
+ // The commenter marks "wrote": Lexical splits the author's text node,
+ // keeping "author " in it and moving the rest into new nodes.
+ let seen = client.oplog_vv();
+ let comment = author_children.push_container(LoroMap::new()).unwrap();
+ comment.insert("type", "comment").unwrap();
+ comment
+ .insert_container("$", LoroMap::new())
+ .unwrap()
+ .insert("id", "c-part")
+ .unwrap();
+ let marked = comment
+ .insert_container("children", LoroMovableList::new())
+ .unwrap()
+ .push_container(LoroMap::new())
+ .unwrap();
+ marked.insert("type", "text").unwrap();
+ marked
+ .insert_container("$", LoroMap::new())
+ .unwrap()
+ .insert("id", "t-marked")
+ .unwrap();
+ marked
+ .insert_container("text", LoroText::new())
+ .unwrap()
+ .insert(0, "wrote")
+ .unwrap();
+ let tail = author_children.push_container(LoroMap::new()).unwrap();
+ tail.insert("type", "text").unwrap();
+ tail.insert_container("$", LoroMap::new())
+ .unwrap()
+ .insert("id", "t-tail")
+ .unwrap();
+ tail.insert_container("text", LoroText::new())
+ .unwrap()
+ .insert(0, " this")
+ .unwrap();
+ unwrapped_text.delete(7, 10).unwrap();
+ client.commit();
+ let split = state
+ .import(&client.export(ExportMode::updates(&seen)).unwrap())
+ .unwrap();
+ assert!(split.changed);
+ assert!(split.touched_nodes.is_empty());
+
+ // The commenter deletes that comment: Lexical merges the pieces back
+ // into the author's text node and removes the comment and tail nodes.
+ let seen = client.oplog_vv();
+ author_children.delete(1, 2).unwrap();
+ unwrapped_text.insert(7, "wrote this").unwrap();
+ client.commit();
+ let merged = state
+ .import(&client.export(ExportMode::updates(&seen)).unwrap())
+ .unwrap();
+ assert!(merged.changed);
+ assert!(merged.touched_nodes.is_empty());
+ assert_eq!(unwrapped_text.to_string(), "author wrote this");
+
+ // Typing into the author's text is still the commenter's edit.
+ let seen = client.oplog_vv();
+ unwrapped_text.insert(17, "!").unwrap();
+ client.commit();
+ let typed = state
+ .import(&client.export(ExportMode::updates(&seen)).unwrap())
+ .unwrap();
+ assert_eq!(typed.touched_nodes, ["t-author"]);
+}
+
#[test]
fn concurrent_branches_survive_session_snapshot_roundtrip() {
for heads in 1..=9 {