Collaborative Editing

Real-time multi-user editing with O-tree CRDT, Ed25519-signed author attribution, awareness state (cursors/selections), automatic three-way merge fallback, and LLM-assisted change narration.

1. Overview 2. O-Tree CRDT Model 3. Awareness State 4. Signed Updates & Author Identity 5. Three-Way Merge Fallback 6. LLM Integration 7. Protocol Flow 8. Udanax-Gold Comparison

1. Overview

Xudanu supports real-time collaborative editing: multiple users can edit the same document simultaneously, seeing each other's changes live. The system is built on an O-tree CRDT (Conflict-free Replicated Data Type) with Ed25519 cryptographic attribution for every character.

The implementation lives in src/server/otree_crdt.rs (~2,030 lines) and provides:

2. O-Tree CRDT Model

The OtreeCrdt struct manages collaborative editing state for all works on the server. Each work has its own CRDT "document" that tracks:

Text delta operations

When a client sends a text edit, the CRDT applies it as a delta:

  1. Client sends TextDeltaOp with the full replacement text
  2. Server computes the diff against the current text
  3. A new revision is created with Ed25519 attribution
  4. The update is relayed to all other sessions editing the same work
Design choice: The current implementation uses full-text replacement (the client sends the entire document). This is simpler than operational transform and works well for the document sizes typical of hypertext content. Future work may add fine-grained character-level deltas for lower bandwidth.

Revision storage

Every edit creates a new revision. The CRDT maintains:

3. Awareness State

Awareness is the real-time presence system. Each editing session broadcasts its state to all other sessions on the same work:

FieldTypePurpose
session_idu64Unique session identifier
user_nameStringDisplay name
cursorOption<OtreeCursorPosition>Cursor position (character index)
selectionOption<OtreeSelectionRange>Selected text range
is_typingboolWhether the user is actively typing
club_idOption<BeId>The user's personal club
author_public_keyOption<Vec<u8>>Ed25519 public key for attribution

When a session updates its awareness, the server relays it to all other sessions editing the same work via OtreeAwarenessRelayResult, which contains the list of (SessionId, OtreeSyncSessionId) pairs to notify.

4. Signed Updates & Author Identity

Every edit is cryptographically signed. The OtreeSignedUpdate contains:

The OtreeAuthorIdentity struct ties the public key to a human-readable name and club:

Author type classification

Authors are classified into types that affect attribution coloring in the UI:

Human

Direct user input, signed with the user's club key. Shown in green in the attribution panel.

LLM

AI-generated text from Ollama/OpenRouter. Signed with the server's key. Shown in purple.

Historical

Imported texts (Project Gutenberg, etc.). Signed with the importing user's key. Shown in amber.

Unattributed

Content without provenance. Occurs in legacy data or anonymous edits.

5. Three-Way Merge Fallback

When CRDT state diverges beyond what can be reconciled (e.g., two users make conflicting edits to the same paragraph), the system falls back to three-way merge:

  1. The last common revision is identified as the base
  2. Both divergent revisions are compared against the base
  3. Non-conflicting changes are merged automatically
  4. Conflicts are resolved using LastWriterWins strategy (based on timestamp)
  5. The merged result becomes the new canonical revision

The three-way merge engine (src/edition/three_way.rs, ~1,889 lines) produces:

6. LLM Integration

The CRDT integrates with the LLM subsystem (src/server/ollama.rs) for:

LLM-appended text is attributed with a separate OtreeAuthorIdentity marked as LLM type, ensuring the attribution panel correctly shows it in purple.

7. Protocol Flow

Client A OtreeCrdt Server State Client B ① edit + signature ② relay update ③ awareness update ④ relay awareness Attribution Storage ElementProvenance → Ed25519 signature per span Revision Chain rev 1 → rev 2 → rev 3 → ... → rev N (current)

Figure 1: Collaborative editing protocol. Edits flow through the server with attribution; awareness is relayed between sessions.

8. Udanax-Gold Comparison

AspectUdanax-GoldXudanu
Concurrency modelSingle-user; grab/release locks with queuingMulti-user CRDT with awareness state
Conflict resolutionManual: last writer wins by defaultAutomatic: three-way merge with LWW fallback
AttributionClub-based ownership of entire worksPer-span Ed25519-signed attribution with author types
Real-time updatesPoll-based; no push notificationsWebSocket relay for edits and awareness
Version historyFlat revision list per workSame, plus revision diffing and comparison
AI assistanceNoneOllama/OpenRouter integration for narration and auto-titling
Key enhancement: The original Udanax-Gold had a sophisticated grab/release lock system where only one user could edit a work at a time. Xudanu retains grab/release for backward compatibility but adds CRDT-based collaborative editing that allows multiple simultaneous editors without locking.