@feltdb/core 0.8.4 → 0.8.6

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 (160) hide show
  1. package/dist/create/package-versions.js +1 -1
  2. package/dist/create/server-source/Cargo.lock +165 -0
  3. package/dist/create/server-source/Cargo.toml +9 -0
  4. package/dist/create/server-source/crates/feltdb/Cargo.toml +3 -0
  5. package/dist/create/server-source/crates/feltdb/benches/gate13_baseline.rs +44 -44
  6. package/dist/create/server-source/crates/feltdb/benches/gate13_phase_7_1_release_economics.rs +12 -24
  7. package/dist/create/server-source/crates/feltdb/benches/gate_13_redux.rs +7 -13
  8. package/dist/create/server-source/crates/feltdb/benches/gate_13_regression_runner.rs +13 -10
  9. package/dist/create/server-source/crates/feltdb/benches/gate_14a_concurrent_writer_scaling.rs +12 -9
  10. package/dist/create/server-source/crates/feltdb/benches/gate_14a_production_admission_revalidation.rs +78 -25
  11. package/dist/create/server-source/crates/feltdb/benches/gate_14a_rc2_admission_contract.rs +16 -13
  12. package/dist/create/server-source/crates/feltdb/benches/gate_14a_rc_root_cause.rs +13 -5
  13. package/dist/create/server-source/crates/feltdb/benches/gate_14a_sync1_queued_prototype.rs +41 -22
  14. package/dist/create/server-source/crates/feltdb/benches/gate_14a_sync_economics.rs +33 -15
  15. package/dist/create/server-source/crates/feltdb/benches/gate_14b_causal_backlog_scaling.rs +100 -33
  16. package/dist/create/server-source/crates/feltdb/benches/gate_14c_replication_contract_test.rs +56 -20
  17. package/dist/create/server-source/crates/feltdb/benches/gate_14c_replication_scaling.rs +116 -41
  18. package/dist/create/server-source/crates/feltdb/benches/gate_14d_combined_dimension_scaling.rs +186 -55
  19. package/dist/create/server-source/crates/feltdb/benches/phase_7_1_2_optimization_benchmark.rs +64 -26
  20. package/dist/create/server-source/crates/feltdb/benches/phase_7_1_3_crossover_analysis.rs +46 -15
  21. package/dist/create/server-source/crates/feltdb/src/admission.rs +8 -15
  22. package/dist/create/server-source/crates/feltdb/src/admission_contract_tests.rs +43 -13
  23. package/dist/create/server-source/crates/feltdb/src/adversarial_transport.rs +15 -42
  24. package/dist/create/server-source/crates/feltdb/src/analytics.rs +65 -19
  25. package/dist/create/server-source/crates/feltdb/src/application.rs +113 -30
  26. package/dist/create/server-source/crates/feltdb/src/authorization_security_tests.rs +475 -140
  27. package/dist/create/server-source/crates/feltdb/src/cardinality_diagnostics.rs +17 -15
  28. package/dist/create/server-source/crates/feltdb/src/cardinality_endpoint.rs +0 -1
  29. package/dist/create/server-source/crates/feltdb/src/causal_backlog_bound.rs +59 -15
  30. package/dist/create/server-source/crates/feltdb/src/causal_dependency_barrier.rs +266 -114
  31. package/dist/create/server-source/crates/feltdb/src/causal_dependency_barrier_phase_7_1.rs +25 -7
  32. package/dist/create/server-source/crates/feltdb/src/concurrency_fuzzing.rs +10 -15
  33. package/dist/create/server-source/crates/feltdb/src/consistency_contract.rs +3 -11
  34. package/dist/create/server-source/crates/feltdb/src/crash_atomic_boundary.rs +14 -5
  35. package/dist/create/server-source/crates/feltdb/src/crash_injection.rs +21 -25
  36. package/dist/create/server-source/crates/feltdb/src/crash_recovery_tests.rs +14 -11
  37. package/dist/create/server-source/crates/feltdb/src/dedup_bound_investigation.rs +103 -22
  38. package/dist/create/server-source/crates/feltdb/src/distributed_indexing.rs +18 -15
  39. package/dist/create/server-source/crates/feltdb/src/durability_guarantees.rs +12 -8
  40. package/dist/create/server-source/crates/feltdb/src/durable_dedup_set.rs +1 -5
  41. package/dist/create/server-source/crates/feltdb/src/durable_operation_identity.rs +87 -23
  42. package/dist/create/server-source/crates/feltdb/src/durable_operation_log.rs +3 -7
  43. package/dist/create/server-source/crates/feltdb/src/durable_sync.rs +10 -9
  44. package/dist/create/server-source/crates/feltdb/src/in_process_transport.rs +1 -6
  45. package/dist/create/server-source/crates/feltdb/src/indexing.rs +35 -38
  46. package/dist/create/server-source/crates/feltdb/src/lib.rs +1648 -46
  47. package/dist/create/server-source/crates/feltdb/src/managed_cas_tests.rs +4 -1
  48. package/dist/create/server-source/crates/feltdb/src/metrics.rs +0 -1
  49. package/dist/create/server-source/crates/feltdb/src/multi_node_convergence.rs +1 -2
  50. package/dist/create/server-source/crates/feltdb/src/multi_operation_transaction.rs +107 -30
  51. package/dist/create/server-source/crates/feltdb/src/observability.rs +19 -6
  52. package/dist/create/server-source/crates/feltdb/src/operation.rs +39 -0
  53. package/dist/create/server-source/crates/feltdb/src/operation_algebra.rs +12 -11
  54. package/dist/create/server-source/crates/feltdb/src/operation_log.rs +9 -4
  55. package/dist/create/server-source/crates/feltdb/src/p1_application_atomicity.rs +65 -18
  56. package/dist/create/server-source/crates/feltdb/src/p1_atomicity_acceptance.rs +193 -57
  57. package/dist/create/server-source/crates/feltdb/src/partition_reconciliation.rs +37 -27
  58. package/dist/create/server-source/crates/feltdb/src/permutation_scheduler.rs +38 -10
  59. package/dist/create/server-source/crates/feltdb/src/persistence_reality.rs +20 -14
  60. package/dist/create/server-source/crates/feltdb/src/phase1b_acceptance.rs +394 -229
  61. package/dist/create/server-source/crates/feltdb/src/phase1c1_acceptance.rs +8 -6
  62. package/dist/create/server-source/crates/feltdb/src/phase1c2_acceptance.rs +11 -13
  63. package/dist/create/server-source/crates/feltdb/src/phase1c3_acceptance.rs +79 -70
  64. package/dist/create/server-source/crates/feltdb/src/phase1c_atomicity_proof.rs +3 -3
  65. package/dist/create/server-source/crates/feltdb/src/phase5_integration.rs +33 -11
  66. package/dist/create/server-source/crates/feltdb/src/phase5_scenarios.rs +6 -6
  67. package/dist/create/server-source/crates/feltdb/src/phase6_adversarial_scenarios.rs +14 -56
  68. package/dist/create/server-source/crates/feltdb/src/phase6_convergence_validator.rs +29 -27
  69. package/dist/create/server-source/crates/feltdb/src/phase6_persistence.rs +35 -17
  70. package/dist/create/server-source/crates/feltdb/src/phase_1c_real_tcp.rs +8 -2
  71. package/dist/create/server-source/crates/feltdb/src/phase_2a_failures.rs +59 -15
  72. package/dist/create/server-source/crates/feltdb/src/phase_2b_network.rs +70 -17
  73. package/dist/create/server-source/crates/feltdb/src/phase_2c_cascading.rs +23 -6
  74. package/dist/create/server-source/crates/feltdb/src/phase_3_durability.rs +12 -3
  75. package/dist/create/server-source/crates/feltdb/src/phase_4_baseline.rs +41 -11
  76. package/dist/create/server-source/crates/feltdb/src/phase_5_soak.rs +56 -25
  77. package/dist/create/server-source/crates/feltdb/src/policy_evaluation.rs +701 -245
  78. package/dist/create/server-source/crates/feltdb/src/production_api.rs +31 -13
  79. package/dist/create/server-source/crates/feltdb/src/query_performance.rs +6 -8
  80. package/dist/create/server-source/crates/feltdb/src/replay_fuzzing.rs +5 -5
  81. package/dist/create/server-source/crates/feltdb/src/replica_acknowledgements.rs +48 -18
  82. package/dist/create/server-source/crates/feltdb/src/replica_membership.rs +30 -11
  83. package/dist/create/server-source/crates/feltdb/src/replication_manager.rs +6 -3
  84. package/dist/create/server-source/crates/feltdb/src/replication_protocol.rs +4 -3
  85. package/dist/create/server-source/crates/feltdb/src/sharding.rs +36 -10
  86. package/dist/create/server-source/crates/feltdb/src/state_conflict_contract.rs +516 -0
  87. package/dist/create/server-source/crates/feltdb/src/state_contract.rs +13 -4
  88. package/dist/create/server-source/crates/feltdb/src/state_diff_contract.rs +222 -0
  89. package/dist/create/server-source/crates/feltdb/src/state_facade.rs +82 -54
  90. package/dist/create/server-source/crates/feltdb/src/state_hash.rs +2 -2
  91. package/dist/create/server-source/crates/feltdb/src/state_model.rs +1565 -536
  92. package/dist/create/server-source/crates/feltdb/src/state_transition_store.rs +6 -3
  93. package/dist/create/server-source/crates/feltdb/src/state_trigger.rs +672 -0
  94. package/dist/create/server-source/crates/feltdb/src/storage.rs +9 -3
  95. package/dist/create/server-source/crates/feltdb/src/submission.rs +5 -11
  96. package/dist/create/server-source/crates/feltdb/src/tcp_transport.rs +6 -8
  97. package/dist/create/server-source/crates/feltdb/src/transaction_api.rs +24 -35
  98. package/dist/create/server-source/crates/feltdb/src/transaction_invariants.rs +24 -8
  99. package/dist/create/server-source/crates/feltdb/src/transaction_preconditions.rs +248 -59
  100. package/dist/create/server-source/crates/feltdb/src/transactions.rs +17 -20
  101. package/dist/create/server-source/crates/feltdb/src/trigger_contract.rs +749 -0
  102. package/dist/create/server-source/crates/feltdb/src/worker_mesh.rs +1 -0
  103. package/dist/create/server-source/crates/feltdb/src/workload.rs +512 -4
  104. package/dist/create/server-source/crates/feltdb/src/workload_diagnostics.rs +3 -4
  105. package/dist/create/server-source/crates/feltdb/tests/bounded_read_contract.rs +132 -0
  106. package/dist/create/server-source/crates/feltdb/tests/branching_evidence.rs +299 -0
  107. package/dist/create/server-source/crates/feltdb/tests/compaction_stall_contract.rs +272 -0
  108. package/dist/create/server-source/crates/feltdb/tests/crash_durability_contract.rs +467 -0
  109. package/dist/create/server-source/crates/feltdb/tests/current_revision_authority_evidence.rs +309 -0
  110. package/dist/create/server-source/crates/feltdb/tests/durable_backup_contract.rs +445 -0
  111. package/dist/create/server-source/crates/feltdb/tests/durable_corruption_contract.rs +518 -0
  112. package/dist/create/server-source/crates/feltdb/tests/durable_format_compatibility.rs +392 -0
  113. package/dist/create/server-source/crates/feltdb/tests/feltdb_state_boundary_tests.rs +436 -220
  114. package/dist/create/server-source/crates/feltdb/tests/fixtures/state_conflict_contract_corpus.json +1916 -0
  115. package/dist/create/server-source/crates/feltdb/tests/fixtures/state_diff_contract_corpus.json +1878 -0
  116. package/dist/create/server-source/crates/feltdb/tests/fixtures/trigger_contract_corpus.json +1862 -0
  117. package/dist/create/server-source/crates/feltdb/tests/operational_health_contract.rs +278 -0
  118. package/dist/create/server-source/crates/feltdb/tests/pr34_query_collection.rs +2 -1
  119. package/dist/create/server-source/crates/feltdb/tests/pr35_equality_index.rs +80 -25
  120. package/dist/create/server-source/crates/feltdb/tests/pr7_self_authorization_proof.rs +5 -8
  121. package/dist/create/server-source/crates/feltdb/tests/pr8_vocabulary_assessment.rs +52 -44
  122. package/dist/create/server-source/crates/feltdb/tests/pr9_phase2_boundary_tests.rs +33 -16
  123. package/dist/create/server-source/crates/feltdb/tests/pr9_phase3a_path_a_tests.rs +22 -7
  124. package/dist/create/server-source/crates/feltdb/tests/pr9_phase3c_authorized_mutations.rs +41 -22
  125. package/dist/create/server-source/crates/feltdb/tests/pr9_phase3c_role_based_authorization.rs +25 -8
  126. package/dist/create/server-source/crates/feltdb/tests/pr9_phase3c_simple_auth_delete.rs +9 -6
  127. package/dist/create/server-source/crates/feltdb/tests/pr9_phase3c_team_delete_role_authorization.rs +120 -69
  128. package/dist/create/server-source/crates/feltdb/tests/pr9_teams_role_based_access.rs +21 -10
  129. package/dist/create/server-source/crates/feltdb/tests/production_readiness_contract.rs +1365 -0
  130. package/dist/create/server-source/crates/feltdb/tests/reconciliation_application.rs +868 -0
  131. package/dist/create/server-source/crates/feltdb/tests/reconciliation_wire_format_evidence.rs +221 -0
  132. package/dist/create/server-source/crates/feltdb/tests/replicated_history_contract.rs +417 -0
  133. package/dist/create/server-source/crates/feltdb/tests/resource_scoped_revisions.rs +338 -0
  134. package/dist/create/server-source/crates/feltdb/tests/revision_identity_contract.rs +1039 -0
  135. package/dist/create/server-source/crates/feltdb/tests/revision_model_decision.rs +739 -0
  136. package/dist/create/server-source/crates/feltdb/tests/revision_retention_boundary_evidence.rs +427 -0
  137. package/dist/create/server-source/crates/feltdb/tests/saas_authorization_integration.rs +3 -3
  138. package/dist/create/server-source/crates/feltdb/tests/saas_invitation_lifecycle.rs +25 -22
  139. package/dist/create/server-source/crates/feltdb/tests/state_conflict_contract_conformance.rs +1799 -0
  140. package/dist/create/server-source/crates/feltdb/tests/state_diff_contract_conformance.rs +1316 -0
  141. package/dist/create/server-source/crates/feltdb/tests/state_model_integration.rs +53 -61
  142. package/dist/create/server-source/crates/feltdb/tests/state_persistence_integration.rs +156 -61
  143. package/dist/create/server-source/crates/feltdb/tests/state_store_boundary_evidence.rs +299 -0
  144. package/dist/create/server-source/crates/feltdb/tests/sync_divergence_evidence.rs +255 -0
  145. package/dist/create/server-source/crates/feltdb/tests/three_way_input_boundary_evidence.rs +249 -0
  146. package/dist/create/server-source/crates/feltdb/tests/trigger_contract_conformance.rs +994 -0
  147. package/dist/create/server-source/crates/feltdb/tests/workload_envelope_contract.rs +442 -0
  148. package/dist/create/server-source/crates/feltdb-server/src/app_state.rs +16 -1
  149. package/dist/create/server-source/crates/feltdb-server/src/auth.rs +164 -13
  150. package/dist/create/server-source/crates/feltdb-server/src/main.rs +695 -47
  151. package/dist/create/server-source/crates/feltdb-server/src/metrics.rs +21 -0
  152. package/dist/studio-app/assets/{feltdb_wasm-CVQWgXO-.js → feltdb_wasm-C1VhI-U5.js} +1 -1
  153. package/dist/studio-app/assets/feltdb_wasm_bg-C8HXbAXb.wasm +0 -0
  154. package/dist/studio-app/assets/{index-DwgNAIIX.js → index-Bbos1m2U.js} +1 -1
  155. package/dist/studio-app/index.html +1 -1
  156. package/dist/wasm/feltdb_wasm_bg.wasm +0 -0
  157. package/dist/workload.d.ts +2 -1
  158. package/dist/workload.d.ts.map +1 -1
  159. package/package.json +1 -1
  160. package/dist/studio-app/assets/feltdb_wasm_bg-CNVpvaZV.wasm +0 -0
@@ -0,0 +1,221 @@
1
+ //! Evidence for the reconciliation reachability audit
2
+ //! (`docs/audits/RECONCILIATION-REACHABILITY-AUDIT.md`).
3
+ //!
4
+ //! This file adds **no capability and no product surface**. It exists to settle
5
+ //! one constructive question the audit had to answer before it could report a
6
+ //! blockage honestly:
7
+ //!
8
+ //! > If a `ReconciliationPlan` cannot be serialized to JSON, can it cross a
9
+ //! > wire at all?
10
+ //!
11
+ //! It can. The recorded limitation is narrower than it first appeared, and the
12
+ //! difference matters, because "the plan cannot be transmitted" would have been
13
+ //! a reason no API could exist. That is not the reason. The reason is in the
14
+ //! audit, and it is about where state lives, not about encoding.
15
+ //!
16
+ //! The DTO below is written here rather than in `src/` deliberately: proving a
17
+ //! shape works is not the same as committing the product to it, and this PR
18
+ //! adds no API.
19
+
20
+ use feltdb::state_model::PathComponent;
21
+ use serde::{Deserialize, Serialize};
22
+ use serde_json::{json, Value};
23
+ use std::collections::BTreeMap;
24
+
25
+ fn key(name: &str) -> PathComponent {
26
+ PathComponent::Key(name.to_string())
27
+ }
28
+
29
+ /// One override as it would appear on a wire: a *structural* path and a value.
30
+ #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
31
+ struct WireOverride {
32
+ path: Vec<PathComponent>,
33
+ value: Value,
34
+ }
35
+
36
+ /// A plan as it would appear on a wire.
37
+ ///
38
+ /// The one difference from `ReconciliationPlan` is that `path_overrides` is a
39
+ /// **list**, not a map, because JSON object keys must be strings and a path is
40
+ /// not one. Everything else is carried unchanged.
41
+ #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
42
+ struct WirePlan {
43
+ left_id: String,
44
+ right_id: String,
45
+ base_id: String,
46
+ parent_choice: bool,
47
+ path_overrides: Vec<WireOverride>,
48
+ }
49
+
50
+ // ---------------------------------------------------------------------------
51
+ // The limitation is about maps, not about paths
52
+ // ---------------------------------------------------------------------------
53
+
54
+ /// A path is JSON-encodable, structurally, with no invented syntax.
55
+ ///
56
+ /// `ReconciliationPlan` cannot be serialized because `path_overrides` is keyed
57
+ /// by `Vec<PathComponent>` and JSON object keys must be strings. That is a
58
+ /// property of using a path as a *map key*. `PathComponent` itself derives
59
+ /// `Serialize`/`Deserialize` and encodes as a tagged object, so a path is
60
+ /// perfectly transmissible as a JSON array.
61
+ #[test]
62
+ fn a_path_survives_json_as_a_structural_array() {
63
+ let path = vec![key("o"), PathComponent::Index(0), key("a")];
64
+ let text = serde_json::to_string(&path).expect("a path serializes");
65
+ assert_eq!(text, r#"[{"Key":"o"},{"Index":0},{"Key":"a"}]"#);
66
+
67
+ let back: Vec<PathComponent> = serde_json::from_str(&text).expect("and parses");
68
+ assert_eq!(back, path, "and round trips exactly");
69
+ }
70
+
71
+ /// The two traps the conflict work rejected flattened renderings over survive.
72
+ ///
73
+ /// This is the reason the encoding is an array of tagged components rather than
74
+ /// a string. Under `"a.b"` / `"a.bc"` one path is a prefix of the other and the
75
+ /// overlap check would report a containment that does not exist; under any
76
+ /// string rendering an object member named `0` and array index `0` collide.
77
+ #[test]
78
+ fn the_encoding_preserves_the_distinctions_a_flattened_one_would_lose() {
79
+ let a_b = vec![key("a"), key("b")];
80
+ let a_bc = vec![key("a"), key("bc")];
81
+ assert_ne!(
82
+ serde_json::to_string(&a_b).unwrap(),
83
+ serde_json::to_string(&a_bc).unwrap(),
84
+ "['a','b'] and ['a','bc'] stay distinct, so neither contains the other"
85
+ );
86
+
87
+ let member_zero = vec![key("0")];
88
+ let index_zero = vec![PathComponent::Index(0)];
89
+ assert_eq!(
90
+ serde_json::to_string(&member_zero).unwrap(),
91
+ r#"[{"Key":"0"}]"#
92
+ );
93
+ assert_eq!(
94
+ serde_json::to_string(&index_zero).unwrap(),
95
+ r#"[{"Index":0}]"#
96
+ );
97
+ assert_ne!(
98
+ serde_json::to_string(&member_zero).unwrap(),
99
+ serde_json::to_string(&index_zero).unwrap(),
100
+ "an object member named 0 and the first array element stay different places"
101
+ );
102
+ }
103
+
104
+ /// A whole plan crosses a wire when overrides are a list rather than a map.
105
+ #[test]
106
+ fn a_plan_crosses_a_wire_when_its_overrides_are_a_list() {
107
+ let plan = WirePlan {
108
+ left_id: "aa".into(),
109
+ right_id: "bb".into(),
110
+ base_id: "cc".into(),
111
+ parent_choice: true,
112
+ path_overrides: vec![
113
+ WireOverride {
114
+ path: vec![key("o"), key("a")],
115
+ value: json!(9),
116
+ },
117
+ WireOverride {
118
+ path: vec![key("xs"), PathComponent::Index(1)],
119
+ value: json!("x"),
120
+ },
121
+ ],
122
+ };
123
+
124
+ let text = serde_json::to_string(&plan).expect("a wire plan serializes");
125
+ let back: WirePlan = serde_json::from_str(&text).expect("and parses");
126
+ assert_eq!(back, plan, "and round trips exactly");
127
+ }
128
+
129
+ // ---------------------------------------------------------------------------
130
+ // What the list costs
131
+ // ---------------------------------------------------------------------------
132
+
133
+ /// The wire form reintroduces a hazard the in-memory type forecloses.
134
+ ///
135
+ /// `ReconciliationPlan::path_overrides` is a `BTreeMap`, so two overrides at the
136
+ /// same path cannot exist — the type makes the question unaskable, and
137
+ /// `a_repeated_override_path_is_impossible_by_construction` records that. A
138
+ /// *list* has no such property: a client can send the same path twice.
139
+ ///
140
+ /// The danger is not that duplicates are possible. It is that collecting the
141
+ /// list into the map **silently resolves them, last-wins**, which is exactly the
142
+ /// shape of defect this sequence has twice corrected: a plausible-looking
143
+ /// collapse that quietly discards one of two competing decisions. An API built
144
+ /// on this DTO must reject duplicates explicitly, and this test exists so that
145
+ /// obligation is recorded before any API exists rather than discovered after.
146
+ #[test]
147
+ fn collecting_a_wire_list_into_a_map_silently_drops_a_duplicate_path() {
148
+ let duplicated = vec![
149
+ WireOverride {
150
+ path: vec![key("o")],
151
+ value: json!("first"),
152
+ },
153
+ WireOverride {
154
+ path: vec![key("o")],
155
+ value: json!("second"),
156
+ },
157
+ ];
158
+
159
+ // The list carries both decisions.
160
+ assert_eq!(duplicated.len(), 2);
161
+
162
+ // The naive conversion keeps one, without erroring.
163
+ let collapsed: BTreeMap<Vec<PathComponent>, Value> = duplicated
164
+ .iter()
165
+ .map(|entry| (entry.path.clone(), entry.value.clone()))
166
+ .collect();
167
+ assert_eq!(collapsed.len(), 1, "one decision was dropped");
168
+ assert_eq!(
169
+ collapsed[&vec![key("o")]],
170
+ json!("second"),
171
+ "silently, and last-wins"
172
+ );
173
+
174
+ // What an API must do instead: detect the duplicate and refuse.
175
+ let mut seen: BTreeMap<Vec<PathComponent>, Value> = BTreeMap::new();
176
+ let mut rejected = false;
177
+ for entry in &duplicated {
178
+ if seen
179
+ .insert(entry.path.clone(), entry.value.clone())
180
+ .is_some()
181
+ {
182
+ rejected = true;
183
+ }
184
+ }
185
+ assert!(
186
+ rejected,
187
+ "a duplicate path is detectable, and must be refused"
188
+ );
189
+ }
190
+
191
+ /// Ordering is not information the wire form may carry.
192
+ ///
193
+ /// The in-memory map has one iteration order, sorted structurally. A list has
194
+ /// whatever order the client wrote. Since overlapping override paths are
195
+ /// rejected, no two overrides can affect each other, so order cannot change the
196
+ /// result — and an API must therefore not let a client believe it can.
197
+ #[test]
198
+ fn wire_order_carries_no_meaning() {
199
+ let one = WireOverride {
200
+ path: vec![key("a")],
201
+ value: json!(1),
202
+ };
203
+ let two = WireOverride {
204
+ path: vec![key("b")],
205
+ value: json!(2),
206
+ };
207
+
208
+ let forward: BTreeMap<_, _> = [one.clone(), two.clone()]
209
+ .iter()
210
+ .map(|e| (e.path.clone(), e.value.clone()))
211
+ .collect();
212
+ let reversed: BTreeMap<_, _> = [two, one]
213
+ .iter()
214
+ .map(|e| (e.path.clone(), e.value.clone()))
215
+ .collect();
216
+
217
+ assert_eq!(
218
+ forward, reversed,
219
+ "the map both lists produce is the same, so wire order is not a decision"
220
+ );
221
+ }
@@ -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
+ }