@feltdb/core 0.8.5 → 0.8.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (29) hide show
  1. package/dist/create/package-versions.js +1 -1
  2. package/dist/create/server-source/crates/feltdb/src/lib.rs +1173 -72
  3. package/dist/create/server-source/crates/feltdb/src/operation.rs +39 -0
  4. package/dist/create/server-source/crates/feltdb/src/state_model.rs +52 -0
  5. package/dist/create/server-source/crates/feltdb/src/storage.rs +9 -3
  6. package/dist/create/server-source/crates/feltdb/tests/bounded_read_contract.rs +132 -0
  7. package/dist/create/server-source/crates/feltdb/tests/common/mod.rs +96 -0
  8. package/dist/create/server-source/crates/feltdb/tests/compaction_stall_contract.rs +272 -0
  9. package/dist/create/server-source/crates/feltdb/tests/crash_durability_contract.rs +467 -0
  10. package/dist/create/server-source/crates/feltdb/tests/current_revision_authority_evidence.rs +26 -5
  11. package/dist/create/server-source/crates/feltdb/tests/durable_backup_contract.rs +445 -0
  12. package/dist/create/server-source/crates/feltdb/tests/durable_corruption_contract.rs +518 -0
  13. package/dist/create/server-source/crates/feltdb/tests/managed_incident_regression.rs +191 -0
  14. package/dist/create/server-source/crates/feltdb/tests/operational_health_contract.rs +278 -0
  15. package/dist/create/server-source/crates/feltdb/tests/production_certification.rs +1153 -0
  16. package/dist/create/server-source/crates/feltdb/tests/production_contract.rs +771 -0
  17. package/dist/create/server-source/crates/feltdb/tests/production_readiness_contract.rs +490 -157
  18. package/dist/create/server-source/crates/feltdb/tests/replicated_history_contract.rs +417 -0
  19. package/dist/create/server-source/crates/feltdb/tests/workload_envelope_contract.rs +442 -0
  20. package/dist/create/server-source/crates/feltdb-server/src/app_state.rs +14 -0
  21. package/dist/create/server-source/crates/feltdb-server/src/main.rs +369 -41
  22. package/dist/create/server-source/crates/feltdb-server/src/metrics.rs +21 -0
  23. package/dist/studio-app/assets/{feltdb_wasm-DaNwCLRX.js → feltdb_wasm-C1VhI-U5.js} +1 -1
  24. package/dist/studio-app/assets/feltdb_wasm_bg-C8HXbAXb.wasm +0 -0
  25. package/dist/studio-app/assets/{index-j8IlhNqJ.js → index-Bbos1m2U.js} +1 -1
  26. package/dist/studio-app/index.html +1 -1
  27. package/dist/wasm/feltdb_wasm_bg.wasm +0 -0
  28. package/package.json +1 -1
  29. package/dist/studio-app/assets/feltdb_wasm_bg-DnsHNv6g.wasm +0 -0
@@ -0,0 +1,417 @@
1
+ //! Replicated revision history.
2
+ //!
3
+ //! The question:
4
+ //!
5
+ //! > **When a remote operation is applied, does FeltDB reconstruct the same
6
+ //! > historical revision the originating authority committed, or does
7
+ //! > replication only reproduce current state?**
8
+ //!
9
+ //! It used to be the latter. A replica received the same records and no history
10
+ //! at all, so two authorities agreed about what *is* while disagreeing about
11
+ //! what *happened* — and reconciliation on a replica had no `base`.
12
+ //!
13
+ //! The target invariant:
14
+ //!
15
+ //! > **Replication preserves authoritative revision history, not merely
16
+ //! > materialized state.**
17
+ //!
18
+ //! # Two independent equivalences
19
+ //!
20
+ //! State equivalence and historical equivalence are checked separately and must
21
+ //! stay separate. Two databases can hold identical current records and
22
+ //! completely different histories — one that recorded every step, one that only
23
+ //! ever saw the last. `state_digest` answers the first question and
24
+ //! `history_digest` the second, and
25
+ //! `same_state_with_different_history_is_detected` proves the second is not the
26
+ //! first wearing a different name.
27
+ //!
28
+ //! # What is not done here
29
+ //!
30
+ //! A peer does **not** mint a local revision for the resulting state. That
31
+ //! would give `origin: A → B → C` and `replica: A' → B' → C'` — the same shape
32
+ //! and different facts. The operation carries the revision it produced, and the
33
+ //! peer rebuilds it, checking that the identity follows from its own parts.
34
+ //!
35
+ //! See `docs/architecture/replicated-history.md`.
36
+
37
+ use feltdb::state_model::{ParentLookup, StateStore};
38
+ use feltdb::{FeltDb, Operation};
39
+ use serde_json::{json, Value};
40
+ use std::collections::HashMap;
41
+ use std::sync::Arc;
42
+ use tempfile::TempDir;
43
+
44
+ struct Pair {
45
+ _directory: TempDir,
46
+ origin: Arc<FeltDb>,
47
+ replica: Arc<FeltDb>,
48
+ }
49
+
50
+ fn pair() -> Pair {
51
+ let directory = TempDir::new().unwrap();
52
+ let origin = Arc::new(FeltDb::open(directory.path().join("origin.log")).unwrap());
53
+ let replica = Arc::new(FeltDb::open(directory.path().join("replica.log")).unwrap());
54
+ Pair {
55
+ _directory: directory,
56
+ origin,
57
+ replica,
58
+ }
59
+ }
60
+
61
+ fn store(db: &Arc<FeltDb>) -> StateStore {
62
+ StateStore::with_feltdb(db.clone()).unwrap()
63
+ }
64
+
65
+ fn operations(db: &Arc<FeltDb>) -> Vec<Operation> {
66
+ db.operations_since(&HashMap::new()).unwrap()
67
+ }
68
+
69
+ fn replicate(from: &Arc<FeltDb>, to: &Arc<FeltDb>) {
70
+ for operation in operations(from) {
71
+ to.apply_remote_operation(operation).unwrap();
72
+ }
73
+ }
74
+
75
+ /// Everything about one resource's history that must survive replication.
76
+ fn history(db: &Arc<FeltDb>, resource: &str) -> Vec<(String, Option<String>, u64, String, String)> {
77
+ store(db)
78
+ .history_of(resource)
79
+ .into_iter()
80
+ .map(|revision| {
81
+ (
82
+ revision.id.as_hex().to_string(),
83
+ revision
84
+ .parent_id
85
+ .as_ref()
86
+ .map(|id| id.as_hex().to_string()),
87
+ revision.sequence,
88
+ revision.content_id.as_hex().to_string(),
89
+ revision.authority,
90
+ )
91
+ })
92
+ .collect()
93
+ }
94
+
95
+ // ---------------------------------------------------------------------------
96
+ // The flagship regression
97
+ // ---------------------------------------------------------------------------
98
+
99
+ /// **The test #307 was written to make fail.**
100
+ ///
101
+ /// Three writes on the origin, replicated. The replica must hold the same three
102
+ /// revisions — identities, parents, sequences, resource and provenance — not
103
+ /// the same final value with no history.
104
+ #[test]
105
+ fn replication_preserves_revision_history_and_ancestry() {
106
+ let pair = pair();
107
+ pair.origin.insert("tasks:1", json!({"n": 1})).unwrap();
108
+ pair.origin.update("tasks:1", json!({"n": 2})).unwrap();
109
+ pair.origin.update("tasks:1", json!({"n": 3})).unwrap();
110
+
111
+ replicate(&pair.origin, &pair.replica);
112
+
113
+ // All three, identical in every historical field.
114
+ let origin_history = history(&pair.origin, "tasks:1");
115
+ let replica_history = history(&pair.replica, "tasks:1");
116
+ assert_eq!(origin_history.len(), 3);
117
+ assert_eq!(replica_history, origin_history);
118
+
119
+ // The chain traverses the same way.
120
+ let replica_store = store(&pair.replica);
121
+ let chain = replica_store.history_of("tasks:1");
122
+ assert_eq!(chain[0].parent_id, None);
123
+ assert_eq!(chain[1].parent_id.as_ref(), Some(&chain[0].id));
124
+ assert_eq!(chain[2].parent_id.as_ref(), Some(&chain[1].id));
125
+ for revision in &chain[1..] {
126
+ assert!(matches!(
127
+ replica_store.parent_of(&revision.id),
128
+ Some(ParentLookup::Revision(_))
129
+ ));
130
+ }
131
+
132
+ // Resource identity and current state.
133
+ assert!(chain.iter().all(|revision| revision.resource == "tasks:1"));
134
+ let current: Value = pair.replica.get("tasks:1").unwrap().unwrap();
135
+ assert_eq!(current["n"], json!(3));
136
+
137
+ // And the two independent equivalences, both holding.
138
+ assert_eq!(
139
+ pair.replica.state_digest().unwrap(),
140
+ pair.origin.state_digest().unwrap()
141
+ );
142
+ assert_eq!(
143
+ replica_store.history_digest(),
144
+ store(&pair.origin).history_digest(),
145
+ "the replica holds the same history, not merely the same values"
146
+ );
147
+ }
148
+
149
+ /// **R10, the mandatory negative control.**
150
+ ///
151
+ /// Two databases with the same current state and different histories must not
152
+ /// compare equal. If `history_digest` could not tell these apart, "history
153
+ /// preserved" would silently mean "current values happen to match", and every
154
+ /// other test in this file would be worthless.
155
+ #[test]
156
+ fn same_state_with_different_history_is_detected() {
157
+ let directory = TempDir::new().unwrap();
158
+
159
+ // Recorded every step.
160
+ let full = Arc::new(FeltDb::open(directory.path().join("full.log")).unwrap());
161
+ full.insert("tasks:1", json!({"n": 1})).unwrap();
162
+ full.update("tasks:1", json!({"n": 2})).unwrap();
163
+ full.update("tasks:1", json!({"n": 3})).unwrap();
164
+
165
+ // Only ever saw the last one.
166
+ let shallow = Arc::new(FeltDb::open(directory.path().join("shallow.log")).unwrap());
167
+ shallow.insert("tasks:1", json!({"n": 3})).unwrap();
168
+
169
+ assert_eq!(
170
+ full.state_digest().unwrap(),
171
+ shallow.state_digest().unwrap(),
172
+ "the fixture requires identical current state"
173
+ );
174
+ assert_ne!(
175
+ store(&full).history_digest(),
176
+ store(&shallow).history_digest(),
177
+ "identical state must not be mistaken for identical history"
178
+ );
179
+ assert_eq!(store(&full).history_of("tasks:1").len(), 3);
180
+ assert_eq!(store(&shallow).history_of("tasks:1").len(), 1);
181
+ }
182
+
183
+ // ---------------------------------------------------------------------------
184
+ // The individual guarantees
185
+ // ---------------------------------------------------------------------------
186
+
187
+ /// **R6.** A replicated revision keeps the authority that committed the fact.
188
+ ///
189
+ /// The replica must not rewrite provenance to itself, as though it had authored
190
+ /// the history it received.
191
+ #[test]
192
+ fn authority_provenance_is_not_rewritten_by_the_replica() {
193
+ let pair = pair();
194
+ pair.origin.insert("tasks:1", json!({"n": 1})).unwrap();
195
+ replicate(&pair.origin, &pair.replica);
196
+
197
+ let origin_authority = store(&pair.origin).head_of("tasks:1").unwrap().authority;
198
+ let replica_authority = store(&pair.replica).head_of("tasks:1").unwrap().authority;
199
+ assert_eq!(replica_authority, origin_authority);
200
+
201
+ // And that authority is the origin's identity, not the replica's own.
202
+ pair.replica
203
+ .insert("tasks:2", json!({"local": true}))
204
+ .unwrap();
205
+ let local_authority = store(&pair.replica).head_of("tasks:2").unwrap().authority;
206
+ assert_ne!(
207
+ replica_authority, local_authority,
208
+ "a replicated revision is not attributed to the replica"
209
+ );
210
+ }
211
+
212
+ /// **R4.** Sequences are authoritative, not reassigned locally.
213
+ ///
214
+ /// A replica with its own unrelated history must not renumber what it receives.
215
+ #[test]
216
+ fn sequences_are_preserved_not_renumbered() {
217
+ let pair = pair();
218
+
219
+ // The replica has its own busy history first.
220
+ for n in 0..5 {
221
+ pair.replica
222
+ .insert(&format!("local:{n}"), json!({ "n": n }))
223
+ .unwrap();
224
+ }
225
+ pair.replica.insert("other:1", json!({"a": 1})).unwrap();
226
+ pair.replica.update("other:1", json!({"a": 2})).unwrap();
227
+
228
+ pair.origin.insert("tasks:1", json!({"n": 1})).unwrap();
229
+ pair.origin.update("tasks:1", json!({"n": 2})).unwrap();
230
+ replicate(&pair.origin, &pair.replica);
231
+
232
+ let sequences: Vec<u64> = store(&pair.replica)
233
+ .history_of("tasks:1")
234
+ .iter()
235
+ .map(|revision| revision.sequence)
236
+ .collect();
237
+ assert_eq!(sequences, vec![0, 1], "a resource's history starts at zero");
238
+ }
239
+
240
+ /// **R7.** Replaying the whole operation stream changes nothing.
241
+ #[test]
242
+ fn replication_is_idempotent() {
243
+ let pair = pair();
244
+ pair.origin.insert("tasks:1", json!({"n": 1})).unwrap();
245
+ pair.origin.update("tasks:1", json!({"n": 2})).unwrap();
246
+
247
+ replicate(&pair.origin, &pair.replica);
248
+ let state_once = pair.replica.state_digest().unwrap();
249
+ let history_once = store(&pair.replica).history_digest();
250
+ let count_once = store(&pair.replica).history_of("tasks:1").len();
251
+
252
+ // Again, twice.
253
+ replicate(&pair.origin, &pair.replica);
254
+ replicate(&pair.origin, &pair.replica);
255
+
256
+ assert_eq!(pair.replica.state_digest().unwrap(), state_once);
257
+ assert_eq!(store(&pair.replica).history_digest(), history_once);
258
+ assert_eq!(
259
+ store(&pair.replica).history_of("tasks:1").len(),
260
+ count_once,
261
+ "no duplicate history, and no new revision identities"
262
+ );
263
+ }
264
+
265
+ /// **R8.** Causal ordering is not weakened to make history easier to rebuild.
266
+ ///
267
+ /// An operation delivered before its causal predecessor is still refused, and
268
+ /// the history is reconstructed once the prerequisites arrive.
269
+ #[test]
270
+ fn out_of_order_delivery_is_refused_and_then_converges() {
271
+ let pair = pair();
272
+ pair.origin.insert("tasks:1", json!({"n": 1})).unwrap();
273
+ pair.origin.update("tasks:1", json!({"n": 2})).unwrap();
274
+ pair.origin.update("tasks:1", json!({"n": 3})).unwrap();
275
+ let ops = operations(&pair.origin);
276
+
277
+ // The third first: refused, and nothing partial is left behind.
278
+ assert!(pair.replica.apply_remote_operation(ops[2].clone()).is_err());
279
+ assert!(store(&pair.replica).history_of("tasks:1").is_empty());
280
+
281
+ // In order: the same history as the origin.
282
+ for operation in &ops {
283
+ pair.replica
284
+ .apply_remote_operation(operation.clone())
285
+ .unwrap();
286
+ }
287
+ assert_eq!(
288
+ store(&pair.replica).history_digest(),
289
+ store(&pair.origin).history_digest()
290
+ );
291
+ }
292
+
293
+ /// **R5 and R9.** Concurrent writes on two authorities produce a fork, and the
294
+ /// fork survives replication with both branches and their shared base.
295
+ ///
296
+ /// This is the payoff: the replica ends up holding the historical information a
297
+ /// three-way reconciliation needs — two heads and the common ancestor they
298
+ /// descend from. Before, it held one value and no `base` at all.
299
+ #[test]
300
+ fn concurrent_authorities_produce_a_fork_that_survives_replication() {
301
+ let pair = pair();
302
+
303
+ // A shared starting point, replicated both ways.
304
+ pair.origin.insert("tasks:1", json!({"n": 0})).unwrap();
305
+ replicate(&pair.origin, &pair.replica);
306
+ let base = store(&pair.origin).head_of("tasks:1").unwrap();
307
+
308
+ // Now each authority writes independently.
309
+ pair.origin
310
+ .update("tasks:1", json!({"side": "left"}))
311
+ .unwrap();
312
+ pair.replica
313
+ .update("tasks:1", json!({"side": "right"}))
314
+ .unwrap();
315
+
316
+ let left = store(&pair.origin).head_of("tasks:1").unwrap();
317
+ let right = store(&pair.replica).head_of("tasks:1").unwrap();
318
+ assert_ne!(left.id, right.id, "two authorities, two revisions");
319
+ assert_eq!(left.parent_id.as_ref(), Some(&base.id));
320
+ assert_eq!(right.parent_id.as_ref(), Some(&base.id), "a real fork");
321
+
322
+ // Exchange. Each side must end up holding both branches.
323
+ replicate(&pair.origin, &pair.replica);
324
+ replicate(&pair.replica, &pair.origin);
325
+
326
+ for db in [&pair.origin, &pair.replica] {
327
+ let ids: Vec<String> = store(db)
328
+ .history_of("tasks:1")
329
+ .iter()
330
+ .map(|revision| revision.id.as_hex().to_string())
331
+ .collect();
332
+ assert!(ids.contains(&base.id.as_hex().to_string()), "the base");
333
+ assert!(ids.contains(&left.id.as_hex().to_string()), "the left head");
334
+ assert!(
335
+ ids.contains(&right.id.as_hex().to_string()),
336
+ "the right head"
337
+ );
338
+
339
+ // The information reconciliation needs: both heads resolve to the same
340
+ // common ancestor.
341
+ let store = store(db);
342
+ for head in [&left, &right] {
343
+ match store.parent_of(&head.id) {
344
+ Some(ParentLookup::Revision(parent)) => assert_eq!(parent.id, base.id),
345
+ other => panic!("a fork head must resolve to the shared base, got {other:?}"),
346
+ }
347
+ }
348
+ }
349
+
350
+ // Both sides now hold the same history, whatever their current values.
351
+ assert_eq!(
352
+ store(&pair.origin).history_digest(),
353
+ store(&pair.replica).history_digest(),
354
+ "the authorities converged on the same historical facts"
355
+ );
356
+ }
357
+
358
+ // ---------------------------------------------------------------------------
359
+ // What replication refuses
360
+ // ---------------------------------------------------------------------------
361
+
362
+ /// A revision whose stated identity does not follow from its own parts is
363
+ /// refused rather than turned into a plausible local history.
364
+ #[test]
365
+ fn a_tampered_revision_identity_is_refused() {
366
+ let pair = pair();
367
+ pair.origin.insert("tasks:1", json!({"n": 1})).unwrap();
368
+ let mut ops = operations(&pair.origin);
369
+
370
+ let provenance = ops[0].revision.as_mut().expect("the operation carries one");
371
+ provenance.id = "0".repeat(64);
372
+
373
+ assert!(
374
+ pair.replica.apply_remote_operation(ops[0].clone()).is_err(),
375
+ "a stated identity that does not follow from the parts is refused"
376
+ );
377
+ assert!(store(&pair.replica).history_of("tasks:1").is_empty());
378
+ }
379
+
380
+ /// A revision that does not match the value it arrived with is refused.
381
+ #[test]
382
+ fn a_revision_that_disagrees_with_its_value_is_refused() {
383
+ let pair = pair();
384
+ pair.origin.insert("tasks:1", json!({"n": 1})).unwrap();
385
+ let mut ops = operations(&pair.origin);
386
+
387
+ // The content identity no longer matches the operation's value.
388
+ let provenance = ops[0].revision.as_mut().unwrap();
389
+ provenance.content_id = "1".repeat(64);
390
+
391
+ assert!(pair.replica.apply_remote_operation(ops[0].clone()).is_err());
392
+ }
393
+
394
+ /// An operation from a build that sends no revision still replicates state, and
395
+ /// carries no history.
396
+ ///
397
+ /// The field is additive and outside the content hash, so an older peer's
398
+ /// operations still verify and apply. What they cannot do is contribute
399
+ /// history, and that is a bounded, stated consequence rather than a silent one.
400
+ #[test]
401
+ fn an_operation_without_provenance_replicates_state_only() {
402
+ let pair = pair();
403
+ pair.origin.insert("tasks:1", json!({"n": 1})).unwrap();
404
+ let mut ops = operations(&pair.origin);
405
+ ops[0].revision = None;
406
+
407
+ pair.replica
408
+ .apply_remote_operation(ops[0].clone())
409
+ .expect("it still verifies and applies");
410
+
411
+ let current: Value = pair.replica.get("tasks:1").unwrap().unwrap();
412
+ assert_eq!(current["n"], json!(1), "state arrived");
413
+ assert!(
414
+ store(&pair.replica).history_of("tasks:1").is_empty(),
415
+ "and no history was invented for it"
416
+ );
417
+ }