Decodes base state (or starts from empty if None).
Detects stale base: if base shares <50% common prefix with both sides, replaces base
with ours_text to prevent duplicate insertions.
Advances base to the line-snapped common prefix of ours and theirs when that prefix
extends beyond the current base — prevents duplication of shared new content.
Computes similar-based line diffs from base → ours and base → theirs.
Merges by applying theirs’ incremental update into the agent doc.
Runs dedup_adjacent_blocks to remove identical adjacent paragraph-level blocks.
Returns the merged string (always conflict-free).
dedup_adjacent_blocks(text): removes duplicate adjacent blocks (separated by \n\n)
where a block has ≥2 non-empty lines, to clean up CRDT double-insertion artifacts.
compact(state): decode then re-encode a CRDT state to GC tombstones.
merge is always conflict-free — it never produces conflict markers.
Agent content (client_id 1) appears before user content (client_id 2) at identical
insertion points due to Yrs’ deterministic client-ID ordering.
Short-circuit: if ours_text == theirs_text, merge returns immediately without any
CRDT operations.
Stale base detection prevents duplicate insertions across multiple merge cycles.
Shared common prefix (line-boundary snapped) is never inserted twice.
dedup_adjacent_blocks only removes blocks with ≥2 non-empty lines; single-line
repeated content (e.g., ---) is not deduplicated to avoid false positives.
encode_state / decode_state are inverse operations: round-trip preserves all text.
compact is idempotent: compacting already-compact state is a no-op in terms of text.