Skip to content

feat: implement CRDT support and efficient run-based text synchronization#25

Merged
brunoga merged 6 commits intomainfrom
feat/crdt-support
Feb 12, 2026
Merged

feat: implement CRDT support and efficient run-based text synchronization#25
brunoga merged 6 commits intomainfrom
feat/crdt-support

Conversation

@brunoga
Copy link
Owner

@brunoga brunoga commented Feb 12, 2026

This PR introduces full support for Conflict-free Replicated Data Types (CRDTs) and distributed state synchronization. It enables
seamless merging of concurrent changes across different nodes with guaranteed deterministic convergence.

Key Features:

  • CRDT Wrapper: New crdt.CRDT[T] type managing local state, causal clocks (HLC), and merging logic.
  • Run-Based Text Optimization: Highly efficient text synchronization that groups contiguous characters into "runs". This
    drastically reduces metadata overhead and supports concurrent edits through intelligent run splitting and automatic
    normalization.
  • Keyed Slices: Semantic diffing using deep:"key" tags, allowing elements to be tracked by unique IDs rather than volatile array
    indices.
  • Manual Patch Builder Enhancements: Added Put, Navigate, and Log methods to the Builder API for more granular manual patch
    construction.
  • Metadata Consolidation: Refactored internal patch logic to centralize conditions and timestamps into a basePatch struct,
    improving maintainability.

Technical Details:

  • Causal Tracking: Hybrid Logical Clocks (HLC) with Reserve(n) support for implicit character IDs.
  • Conflict Resolution: Last-Write-Wins (LWW) resolution for standard fields and semantic ordering for keyed slices.
  • Compatibility: Verified backwards compatibility with the 2.0.0 release.
  • Quality: Increased test coverage across core packages (crdt @ 85%+, hlc @ 93%+) and passed all go vet and go fmt checks.

- Optimized text storage by grouping contiguous characters into runs.
- Added Reserve(n) to HLC clock to support implicit character IDs.
- Implemented automatic splitting, merging, and normalization of runs.
- Added comprehensive tests for convergence and run management.
- Extracted metadata update logic into updateMetadataLocked.
- Added CreateDelta method to promote a standard deep.Patch to a crdt.Delta.
- Updated tests to verify clock and metadata consistency.
- Introduced basePatch struct to handle conditions and metadata.
- Replaced redundant method implementations in all patch types.
- Updated serialization and tests to use the new basePatch structure.
- Improved code maintainability and reduced overall complexity.
- Added JSON serialization tests for CRDT.
- Added comprehensive edge case tests for run-based Text deletion.
- Improved HLC coverage to 93%+ with thorough Compare/Update tests.
- Added tests for Put builder method and missing toJSONPatch cases.
- Fixed customDiffPatch testing logic.
- Fixed duplicate JSON tag in websocket_sync example.
- Applied go fmt across the entire codebase.
- Verified with go vet and go fix.
@brunoga brunoga merged commit d35b152 into main Feb 12, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant