1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
//! Storage backend capability traits
//!
//! This module defines **optional capabilities** that complete storage backends may provide.
//!
//! # What is a Backend?
//!
//! A **backend is a complete, integrated solution** for storage, synchronization, and persistence:
//!
//! - **AutomergeIrohBackend**: Automerge CRDTs + RocksDB persistence + Iroh QUIC + custom mesh (ADR-017)
//! - **SimpleBackend**: RocksDB only (no CRDT, no sync, just local K/V storage)
//!
//! Backends are **not** individual components like "just Automerge" or "just Iroh". Each backend
//! integrates multiple technologies into a cohesive solution.
//!
//! # Architecture Philosophy
//!
//! Rather than forcing all backends into a one-size-fits-all trait, we use
//! **capability traits** to expose what each complete backend can do:
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────┐
//! │ Peat Protocol Business Logic │
//! │ (Uses StorageBackend + optional capabilities) │
//! └─────────────────┬───────────────────────────────────────┘
//! │
//! ┌─────────────────▼───────────────────────────────────────┐
//! │ StorageBackend (required for all backends) │
//! │ • collection() - Basic CRUD via Vec<u8> │
//! │ • flush() - Persistence guarantee │
//! └─────────────────┬───────────────────────────────────────┘
//! │
//! ┌──────────┴──────────────┐
//! ▼ ▼
//! ┌─────────────────────┐ ┌──────────┐
//! │ AutomergeIrohBackend│ │ Simple │
//! │ =================== │ │ Backend │
//! │ • Automerge CRDTs │ │ ======== │
//! │ • RocksDB persist │ │ • RocksDB│
//! │ • Iroh QUIC │ │ only │
//! │ • Custom mesh │ │ │
//! │ (ADR-017) │ │ │
//! │ │ │ │
//! │ + CrdtCapable │ │ │
//! │ + SyncCapable │ │ │
//! └─────────────────────┘ └──────────┘
//! (one complete OSS (minimal
//! stack, not separate backend)
//! pieces)
//! ```
//!
//! # Backend Comparison
//!
//! | Backend | Components | CRDT | Sync | License | Use Case |
//! |------------------------|-------------------------------------|------|------|------------|--------------------------|
//! | **AutomergeIrohBackend**| Automerge + RocksDB + Iroh + mesh | ✅ | ✅ | MIT/Apache | OSS, self-hosted |
//! | **SimpleBackend** | RocksDB only | ❌ | ❌ | Apache 2.0 | Testing, local storage |
//!
//! # Capability Traits
//!
//! ## CrdtCapable - Field-Level Conflict Resolution
//!
//! Backends that implement `CrdtCapable` can store structured data and provide
//! CRDT-based conflict resolution at the field level, not just document level.
//!
//! **Benefits:**
//! - OR-Set semantics for arrays (concurrent additions merge correctly)
//! - LWW-Register for scalar fields (timestamp-based resolution)
//! - Delta sync (only changed fields transmitted)
//! - 50x+ bandwidth reduction vs. full document sync
//!
//! **Requirements:**
//! - Protobuf messages must have `#[derive(Serialize, Deserialize)]`
//! - Backend must support structured storage (Automerge document)
//!
//! **Example (AutomergeIrohBackend):**
//! ```ignore
//! use peat_protocol::storage::{AutomergeIrohBackend, CrdtCapable};
//!
//! let backend = AutomergeIrohBackend::new(config);
//! let squads: Arc<dyn TypedCollection<SquadSummary>> =
//! backend.typed_collection("squads");
//! squads.upsert("squad-1", &summary)?;
//! // → Automerge stores as CRDT document, persists to RocksDB, syncs via Iroh
//! ```
//!
//! ## SyncCapable - Built-in Replication
//!
//! Backends that implement `SyncCapable` have built-in P2P synchronization.
//!
//! **AutomergeIrohBackend**: Integrated with Iroh QUIC transport + custom mesh (ADR-017)
//!
//! # Decision Guide
//!
//! ## When to use basic `StorageBackend` interface:
//!
//! - ✅ Backend-agnostic code (must work with any backend)
//! - ✅ Testing with mocks
//! - ✅ CRDT benefits not critical
//! - ✅ Simple binary data storage
//!
//! ## When to use `CrdtCapable` interface:
//!
//! - ✅ Field-level conflict resolution needed
//! - ✅ Delta sync bandwidth optimization critical
//! - ✅ Type safety at compile time desired
//! - ✅ Willing to add serde derives to protobuf messages
//!
//! ## When to use `SyncCapable` interface:
//!
//! - ✅ Need to control sync lifecycle (start/stop)
//! - ✅ Need sync statistics and monitoring
//!
//! # Open Source Path
//!
//! Peat Protocol provides a **fully open-source implementation** using:
//!
//! ```text
//! ┌──────────────────────────────────────────────────────────┐
//! │ Peat Protocol (Apache 2.0) │
//! ├──────────────────────────────────────────────────────────┤
//! │ AutomergeIrohBackend (complete OSS backend) │
//! │ Components: │
//! │ • Automerge (MIT) - CRDT engine │
//! │ • RocksDB (Apache 2.0) - Persistence layer │
//! │ • Iroh (Apache 2.0/MIT) - QUIC transport │
//! │ • Custom P2P mesh (ADR-017) - Discovery & topology │
//! │ │
//! │ Capabilities: │
//! │ └─ StorageBackend: Yes (required) │
//! │ └─ CrdtCapable: Yes (via Automerge CRDTs) │
//! │ └─ SyncCapable: Yes (via Iroh + custom mesh) │
//! └──────────────────────────────────────────────────────────┘
//! ```
//!
//! This ensures:
//! - ✅ No vendor lock-in
//! - ✅ Full auditability
//! - ✅ Military/government deployment sovereignty
//! - ✅ Community contributions and forks
//!
//! # Future Capabilities
//!
//! Additional capability traits may be added:
//! - `QueryCapable` - Advanced query DSLs
//! - `IndexCapable` - Secondary indexes
//! - `TransactionCapable` - Multi-document ACID transactions
//! - `EncryptionCapable` - At-rest encryption
use crateCommandStorage;
use crateSummaryStorage;
use Result;
use Message;
use ;
use Arc;
/// Typed collection trait for CRDT-optimized storage
///
/// Backends that implement `CrdtCapable` provide this trait to enable
/// field-level conflict resolution via CRDT semantics.
///
/// # Type Parameters
///
/// * `M` - Protobuf message type with serde support
///
/// # CRDT Semantics
///
/// Different field types get different CRDT semantics:
/// - **Arrays**: OR-Set (observed-remove set) - concurrent additions merge
/// - **Scalars**: LWW-Register (last-write-wins) - timestamp-based resolution
/// - **Nested objects**: Recursive application of above rules
///
/// # Example
///
/// ```ignore
/// use peat_protocol::storage::{CrdtCapable, TypedCollection};
/// use peat_schema::hierarchy::v1::SquadSummary;
///
/// let backend = AutomergeIrohBackend::new(config);
/// let squads: Arc<dyn TypedCollection<SquadSummary>> =
/// backend.typed_collection("squads");
///
/// // Field-level updates
/// let mut summary = squads.get("squad-1")?.unwrap();
/// summary.member_ids.push("node-4".to_string()); // OR-Set addition
/// squads.upsert("squad-1", &summary)?;
/// // → Only member_ids field is transmitted, not entire document
/// ```
/// CRDT capability trait - Backend supports field-level conflict resolution
///
/// Backends that implement this trait can store structured data and provide
/// CRDT-based merging at the field level, enabling:
/// - Delta sync (only changed fields transmitted)
/// - Automatic conflict resolution
/// - Optimistic replication
///
/// # Implementations
///
/// - ✅ `AutomergeIrohBackend` - Native Automerge documents with RocksDB persistence
/// - ❌ `SimpleBackend` - Blob storage, no CRDT support