This document describes how the Mesh Transport and State Sync (CRDT) components are combined to create a fully functional offlineβfirst multiβagent state synchronization layer.
βββββββββββββββ βββββββββββββββ βββββββββββββββ
β State β β Integrationβ β Mesh β
β Sync ββββββΆβ Adapter ββββββΆβ Transport β
β Core βββββββ βββββββ β
βββββββββββββββ βββββββββββββββ βββββββββββββββ
β β β
βΌ βΌ βΌ
Local CRDT Delta Encoding PeerβtoβPeer
Updates Broadcast
- Sits between State Sync and Mesh Transport.
- Translates CRDT deltas into transport messages and viceβversa.
- Manages subscriptions: which keys are forwarded to which peers.
- Implements backβpressure and rate limiting.
- Each agent can express interest in specific key patterns (e.g.,
swarm/*,agent/42/*). - The adapter only sends deltas for keys that match a peerβs subscription.
- Supports wildcard and prefixβbased subscriptions.
- Deltas are serialized using a compact binary format (e.g., CBOR, MessagePack, or custom).
- Compression (zstd) can be applied for large deltas.
- Each delta includes:
- Source agent ID
- Vector clock fragment
- List of operations
- Timestamp (logical)
- When two deltas arrive concurrently, the CRDT engine merges them automatically.
- The integration layer ensures causal order: if delta B depends on delta A, B is not applied before A.
- Uses vector clocks attached to each delta.
pub enum SyncMessage {
// Advertisement of subscription interests
Subscribe { patterns: Vec<String> },
Unsubscribe { patterns: Vec<String> },
// Delta transmission
Delta(Delta),
// Request missing deltas (catchβup)
SyncRequest { since: VectorClock },
SyncResponse { deltas: Vec<Delta> },
// Heartbeat / keepβalive
Ping,
Pong,
}When two agents connect:
- Exchange
Subscribemessages to inform each other of key interests. - Optionally, perform a full sync if one agent is behind (via
SyncRequest/SyncResponse). - Thereafter, only incremental deltas are sent.
- Deltas are sent over Mesh Transportβs reliable unicast channel.
- If a delta is lost, the receiver will detect a gap in the vector clock and request a retransmission.
- The integration adapter maintains a small buffer of recent deltas for retransmission.
max_delta_size: Maximum size of a single delta before splitting (default 64β―KB).sync_interval: How often to broadcast a summary of vector clocks (default 5β―s).subscription_timeout: How long to keep a subscription without refresh (default 30β―s).compression_threshold: Size above which to compress deltas (default 1β―KB).
- Create
crates/integration(or extendagentβcore). - Implement
IntegrationAdapterthat holds handles to aMeshTransportand aCrdtMap. - Forward all local CRDT changes as broadcast deltas.
- Apply incoming deltas directly to the CRDT map.
- Write a test with two inβprocess nodes that synchronize a simple key.
- Add subscription mechanism.
- Filter outgoing deltas based on peer subscriptions.
- Add
Subscribe/Unsubscribemessage handling.
- Attach vector clocks to each delta.
- Implement
SyncRequest/SyncResponsefor lateβjoining agents. - Buffer outβofβorder deltas and apply them when dependencies are satisfied.
- Delta compression.
- Batch multiple deltas into a single transport message.
- Adaptive sync intervals based on network quality.
- Unit tests: Mock transport and verify delta forwarding.
- Integration tests: Spawn multiple real transport instances over loopback and verify state convergence.
- Propertyβbased tests: Generate random sequences of CRDT operations, distribute across simulated agents, assert eventual consistency.
- Network fault injection: Drop, reorder, or duplicate messages and verify the system still converges.
- Should we support partial synchronization (only a subset of keys) to reduce bandwidth?
- How to handle malicious peers sending malformed deltas? (Validation layer)
- Should the integration layer be responsible for conflict resolution beyond CRDT merge? (e.g., applicationβspecific conflict handlers)