The anchoring problem
A comment points at a span of text, but that text shifts as people insert and delete around it. The hard part is keeping the comment anchored to the right words even after heavy editing.
Stable anchors
Rather than storing raw character offsets, systems anchor comments to the document model using relative positions tied to stable character ids. When text is inserted before the span, the anchor still resolves to the original words.
Orphaned comments
If the anchored text is deleted entirely, the comment becomes orphaned. Good systems detect this and either resolve the comment or move it to a margin so the discussion is not lost.
Comments themselves often form their own synchronized list, so threads and replies converge with the same machinery as the document body.
Key idea
Comments anchor to stable relative positions so they follow the right text through edits and degrade gracefully when that text is deleted.