Hybrid Logical Clocks (HLC), PNCounter, LWW-Map, Observed-Remove Set, RGA collaborative text CRDT, state-vector sync, and room presence.
Pure-Veko local-first Conflict-Free Replicated Data Types (CRDTs), Hybrid Logical Clocks (HLC), Replicated Growable Array (RgaText) collaborative text sequences, binary delta sync framing, and ephemeral presence tracking (crdt_sync).
crdt_sync provides state-based and delta-capable CRDT primitives with deterministic semilattice convergence (merge(A, B) == merge(B, A), merge(A, merge(B, C)) == merge(merge(A, B), C), merge(A, A) == A):
HLC): Kulkarni-style causal timestamps packing a 48-bit physical millisecond wall clock, a 16-bit logical counter, and a deterministic node_id tie-breaker (new-hlc, hlc-now, hlc-receive, hlc-compare, hlc-pack, hlc-unpack).GCounter, PNCounter): Grow-only and positive-negative counters using per-actor vector slots merged via pointwise maxima.LwwMap): Key-value map where writes and tombstone deletions carry HlcTimestamp metadata and converge under a strict total order.OrSet): Add-wins concurrent set tagging every addition with a unique (node_id, seq) dot so concurrent add + remove preserves the concurrent addition.RgaText): Replicated Growable Array sequence CRDT operating on full Unicode codepoints (utf8), linearizing concurrent spans deterministically via pre-order traversal of the causal insertion tree without interleaving multi-character spans.SyncFrame, PresenceRoom): CRC-32-authenticated VKCS binary sync frames carrying state-vector deltas alongside TTL-bounded room presence tracking.veko pkg add crdt_sync@1.0.0Import in .vk source files:
use [crdt = crdt_sync]hlc-pack encodes each HlcTimestamp into a compact big-endian binary envelope:
| Byte Offset | Field | Width | Description |
|---|---|---|---|
0..6 | phys_ms | 48 bits BE | Physical Unix epoch milliseconds |
6..8 | logical | 16 bits BE | Monotonic logical tie-break counter |
8..10 | node_len | uint16 BE | Byte length of node_id |
10..end | node_id | UTF-8 bytes | Deterministic actor identifier |
RgaText Causal Tree LinearizationEvery inserted Unicode scalar value receives a unique RgaId { seq, node_id } (with seq = max_seq + 1) and references the origin_id immediately to its left (seq = 0 for document head). Merging two RgaText replicas computes the union of vertices (deleted = a.deleted or b.deleted) and linearizes the causal tree in pre-order with sibling nodes ordered by descending RgaId, guaranteeing identical UTF-8 output across all merge permutations.
HLC)| Symbol | Signature | Description |
|---|---|---|
new-hlc | fn [node_id str /initial-ms init_ms i64 = 0] HLC | Creates a per-node HLC instance |
hlc-now | fn [mut clock HLC /wall-ms wall_ms i64 = -1] HlcTimestamp | Generates a monotonic local timestamp |
hlc-receive | fn [mut clock HLC, remote HlcTimestamp /wall-ms wall_ms i64 = -1] HlcTimestamp | Advances clock past local wall time and remote |
hlc-compare | fn [a HlcTimestamp, b HlcTimestamp] i64 | Total order comparator (-1, 0, 1) |
hlc-pack | fn [ts HlcTimestamp] bytes | Serializes HlcTimestamp to binary wire bytes |
hlc-unpack | fn [raw bytes] HlcTimestamp | Deserializes HlcTimestamp from binary bytes |
LwwMap, and OrSet| Symbol | Signature | Description |
|---|---|---|
new-gcounter / gcounter-inc / gcounter-value / gcounter-merge | GCounter | Grow-only distributed counter |
new-pncounter | fn [] PNCounter | Creates an empty positive-negative counter |
pncounter-inc / pncounter-dec | fn [mut pn PNCounter, node_id str /by delta i64 = 1] i64 | Increments or decrements actor slot |
pncounter-value | fn [pn PNCounter] i64 | Evaluates sum(pos) - sum(neg) |
pncounter-merge | fn [a PNCounter, b PNCounter] PNCounter | Pointwise-maximum semilattice merge |
new-lww-map | fn [] LwwMap | Creates an HLC-timestamped LWW map |
lww-set | fn [mut m LwwMap, key str, value any, ts HlcTimestamp] enum | Writes key if ts dominates prior entry |
lww-delete | fn [mut m LwwMap, key str, ts HlcTimestamp] enum | Tombstones key at ts |
lww-get / lww-has? | fn [m LwwMap, key str] any / bool | Reads live key (returns err if absent/tombstoned) |
lww-to-map / lww-merge | fn [m LwwMap] map / fn [a LwwMap, b LwwMap] LwwMap | Materializes live keys / merges replicas |
new-orset | fn [] OrSet | Creates an add-wins Observed-Remove Set |
orset-add | fn [mut s OrSet, elem any, node_id str] str | Tags elem with a fresh (node_id, seq) dot |
orset-remove | fn [mut s OrSet, elem any] i64 | Tombstones all observed dots for elem |
orset-contains? / orset-elements / orset-merge | OrSet | Membership query, sorted listing, and merge |
RgaText, Sync Frames, and PresenceRoom| Symbol | Signature | Description |
|---|---|---|
new-rga | fn [node_id str] RgaText | Creates a collaborative text replica |
rga-insert-str | fn [mut doc RgaText, index i64, text str] enum | Inserts UTF-8 span at visible rune offset |
rga-delete-range | fn [mut doc RgaText, start_index i64, count i64] i64 | Tombstones count visible runes from start_index |
rga-to-str / rga-len | fn [doc RgaText] str / i64 | Materializes UTF-8 text or visible rune count |
rga-merge | fn [a RgaText, b RgaText] RgaText | Order-independent sequence merge |
rga-state-vector / rga-delta-since / rga-apply-delta | RgaText | Causal state-vector delta extraction and application |
encode-sync-frame / decode-sync-frame | SyncFrame <-> bytes | CRC-32-verified VKCS binary sync frame codec |
new-presence-room / join-presence / heartbeat-presence / expire-presence / active-presence | PresenceRoom | Ephemeral peer lease & TTL expiration tracker |
use [crdt = crdt_sync]
mut peer_a = crdt/new-rga 'alice'
_ = crdt/rga-insert-str mut peer_a 0 'Hello World'
mut peer_b = crdt/rga-merge (crdt/new-rga 'bob') peer_a
// Offline concurrent edits
_ = crdt/rga-insert-str mut peer_a 6 'Veko '
_ = crdt/rga-insert-str mut peer_b 11 '!'
ab = crdt/rga-merge peer_a peer_b
ba = crdt/rga-merge peer_b peer_a
println (crdt/rga-to-str ab) // 'Hello Veko World!'
println ((crdt/rga-to-str ab) == (crdt/rga-to-str ba)) // truehttps://veko-lang.org/pkg/crdt_sync/1.0.0.vkp71f5fd84d682ef738d0dc7183cb6af26e699d7c84bd2153e5c89dfc4de73e58cff7e9878cb96b7f3959237ec32dcab24585bb86f72bf7dc19c5befd156fcbe0eed25519:27406074d8681da1d4e6420f5d887cac15c313334c249ac047ba9d4e2df72b70{"name":"crdt_sync","version":"1.0.0","tier":6.0,"tier_label":"Tier 6: Local-First, IoT, Web & Media","category":"Local-First CRDTs & Presence","description":"Hybrid Logical Clocks (HLC), PNCounter, LWW-Map, Observed-Remove Set, RGA collaborative text CRDT, state-vector sync, and room presence.","modules":["crdt_sync"],"homepage":"https://veko-lang.org/packages/crdt_sync"}| Bundle Entry Path | Size | Leaf SHA-256 Digest |
|---|---|---|
| README.md | 6 KB | 827aedc08ddb03614166ee903d6ab1b75e91b6ff246e6b81dc69716c55cd7fc8 |
| examples/demo.vk | 8 KB | 0b76eba4e7ee2a4f348c2132834a78073d54c06667138ecdb95a7a9767ee917a |
| mod.vk | 28 KB | 4d52b74dbe71385093794aad95130f9e5657dcea1ffd2d01e7b162889d5cd488 |