@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,299 @@
1
+ //! Evidence for the StateStore boundary audit
2
+ //! (`docs/audits/STATESTORE-BOUNDARY-AUDIT.md`).
3
+ //!
4
+ //! These tests add **no capability and no product surface**. They answer one
5
+ //! adversarial question with runs rather than readings:
6
+ //!
7
+ //! > Can FeltDB expose immutable state history through the existing server
8
+ //! > authority with fewer primitives than a conventional database or branch
9
+ //! > system requires?
10
+ //!
11
+ //! The answer is yes, and the margin is large. `MinimalStore` below is written
12
+ //! in this test file deliberately: demonstrating that a shape is sufficient is
13
+ //! not the same as committing the product to it, and this PR adds no API.
14
+
15
+ use feltdb::state_model::{
16
+ apply_reconciliation_plan, ConflictClass, ConflictClassification, ReconciliationPlan, StateId,
17
+ StateRevision, StateStore,
18
+ };
19
+ use feltdb::FeltDb;
20
+ use std::collections::HashMap;
21
+ use std::sync::Arc;
22
+ use tempfile::TempDir;
23
+
24
+ fn revision(content: &str) -> StateRevision {
25
+ StateRevision {
26
+ id: StateId::compute(content),
27
+ resource: "docs:1".into(),
28
+ content: content.to_string(),
29
+ content_id: StateId::compute(content),
30
+ sequence: 0,
31
+ parent_id: None,
32
+ authority: "test".into(),
33
+ timestamp_ms: 0,
34
+ metadata: Default::default(),
35
+ }
36
+ }
37
+
38
+ // ---------------------------------------------------------------------------
39
+ // A store built from two operations
40
+ // ---------------------------------------------------------------------------
41
+
42
+ /// A read-through revision store built from nothing but `FeltDb::insert` and
43
+ /// `FeltDb::get`.
44
+ ///
45
+ /// No in-memory index, no `current` pointer, no branches, and — because every
46
+ /// record is keyed by the hash of its own content — no recovery path.
47
+ struct MinimalStore {
48
+ db: Arc<FeltDb>,
49
+ }
50
+
51
+ impl MinimalStore {
52
+ fn key(id: &StateId) -> String {
53
+ format!("state:revision:{}", id.as_hex())
54
+ }
55
+
56
+ fn put(&self, revision: &StateRevision) -> Result<(), String> {
57
+ if !revision.verify_integrity() {
58
+ return Err("integrity check failed".into());
59
+ }
60
+ self.db
61
+ .insert(&Self::key(&revision.id), revision.clone())
62
+ .map_err(|error| error.to_string())
63
+ }
64
+
65
+ fn get(&self, id: &StateId) -> Option<StateRevision> {
66
+ self.db.get::<StateRevision>(&Self::key(id)).ok().flatten()
67
+ }
68
+
69
+ /// Ancestry is not a third operation. It is `get` and `parent_id`.
70
+ fn ancestors(&self, id: &StateId) -> Vec<StateId> {
71
+ let mut out = vec![];
72
+ let mut cursor = self.get(id).and_then(|revision| revision.parent_id);
73
+ while let Some(parent) = cursor {
74
+ out.push(parent.clone());
75
+ cursor = self.get(&parent).and_then(|revision| revision.parent_id);
76
+ }
77
+ out
78
+ }
79
+ }
80
+
81
+ /// Two operations carry the whole chain, across a real restart, with no
82
+ /// recovery step and no mutable pointer.
83
+ ///
84
+ /// This is the audit's central result. The store is opened, written, dropped,
85
+ /// and reopened over the same log by a *different* `FeltDb` instance; nothing
86
+ /// reconstructs an index, and the three revisions reconciliation needs are read
87
+ /// back by identity alone.
88
+ #[test]
89
+ fn two_operations_carry_the_whole_chain_across_a_restart() {
90
+ let directory = TempDir::new().unwrap();
91
+ let path = directory.path().join("minimal.log");
92
+
93
+ let (base_id, left_id, right_id) = {
94
+ let store = MinimalStore {
95
+ db: Arc::new(FeltDb::open(&path).unwrap()),
96
+ };
97
+ let base = StateRevision::initial(
98
+ "docs:1".to_string(),
99
+ r#"{"shared":1,"only_left":1}"#.into(),
100
+ "a".into(),
101
+ );
102
+ let left = StateRevision::child(
103
+ r#"{"shared":10,"only_left":2}"#.into(),
104
+ &base,
105
+ "alice".into(),
106
+ );
107
+ let right =
108
+ StateRevision::child(r#"{"shared":20,"only_left":1}"#.into(), &base, "bob".into());
109
+
110
+ store.put(&base).unwrap();
111
+ store.put(&left).unwrap();
112
+ store.put(&right).unwrap();
113
+ (base.id.clone(), left.id.clone(), right.id.clone())
114
+ };
115
+
116
+ // A different FeltDb instance over the same log. No recovery is run.
117
+ let store = MinimalStore {
118
+ db: Arc::new(FeltDb::open(&path).unwrap()),
119
+ };
120
+ let base = store.get(&base_id).expect("base survives");
121
+ let left = store.get(&left_id).expect("left survives");
122
+ let right = store.get(&right_id).expect("right survives");
123
+ assert!(base.verify_integrity() && left.verify_integrity() && right.verify_integrity());
124
+
125
+ // The full chain runs against what was read back.
126
+ let classification = ConflictClassification::classify(&base, &left, &right);
127
+ assert_eq!(classification.overall, ConflictClass::Conflict);
128
+
129
+ let plan = ReconciliationPlan::new(left.id.clone(), right.id.clone(), base.id.clone(), true);
130
+ let content = apply_reconciliation_plan(&base, &left, &right, &plan).expect("applies");
131
+ assert_eq!(content, r#"{"only_left":2,"shared":10}"#);
132
+
133
+ let result = StateRevision::child(content, &left, "reconciler".into());
134
+ store.put(&result).expect("the result persists");
135
+ assert_eq!(
136
+ store.ancestors(&result.id),
137
+ vec![left_id, base_id],
138
+ "ancestry needs no primitive beyond get and parent_id"
139
+ );
140
+
141
+ // And no shared mutable record was ever written.
142
+ let current = store
143
+ .db
144
+ .get::<(String, HashMap<String, String>)>("state:current")
145
+ .unwrap();
146
+ assert!(
147
+ current.is_none(),
148
+ "a content-addressed history needs no current pointer"
149
+ );
150
+ }
151
+
152
+ // ---------------------------------------------------------------------------
153
+ // What the chain actually requires
154
+ // ---------------------------------------------------------------------------
155
+
156
+ /// Application reads exactly one content, and three identities.
157
+ ///
158
+ /// The base and the branch that was *not* selected contribute nothing but their
159
+ /// ids — both may be unparseable and application still succeeds. This bounds
160
+ /// what a store must be able to serve at the moment a plan is applied: one full
161
+ /// state, not three.
162
+ ///
163
+ /// Classification is the step that reads all three, so the chain as a whole
164
+ /// still needs all three. The distinction matters for a durable design, because
165
+ /// the two steps could be served differently.
166
+ #[test]
167
+ fn application_reads_one_content_and_three_identities() {
168
+ let base = revision("this is not json at all");
169
+ let left = revision(r#"{"a":1}"#);
170
+ let right = revision("also not json");
171
+
172
+ let plan = ReconciliationPlan::new(left.id.clone(), right.id.clone(), base.id.clone(), true);
173
+ assert_eq!(
174
+ apply_reconciliation_plan(&base, &left, &right, &plan).expect("applies"),
175
+ r#"{"a":1}"#,
176
+ "only the selected parent's content is read"
177
+ );
178
+ }
179
+
180
+ /// The chain from three revisions to a reconciled one names no branch.
181
+ #[test]
182
+ fn the_chain_needs_no_branch() {
183
+ let store = StateStore::new_volatile();
184
+ let base = store
185
+ .create("docs:1", r#"{"v":1}"#.into(), "a".into())
186
+ .unwrap();
187
+ let left = store
188
+ .commit(r#"{"v":2}"#.into(), &base, "alice".into())
189
+ .unwrap();
190
+ let right = store
191
+ .commit(r#"{"v":3}"#.into(), &base, "bob".into())
192
+ .unwrap();
193
+
194
+ let plan = ReconciliationPlan::new(left.id.clone(), right.id.clone(), base.id.clone(), true);
195
+ assert!(apply_reconciliation_plan(&base, &left, &right, &plan).is_ok());
196
+ assert!(
197
+ store.get(&base.id).is_some() && store.get(&left.id).is_some(),
198
+ "the chain named no branch, and every revision stayed addressable"
199
+ );
200
+ }
201
+
202
+ // ---------------------------------------------------------------------------
203
+ // What the existing store costs
204
+ // ---------------------------------------------------------------------------
205
+
206
+ /// No commit writes a shared mutable record. **Regression.**
207
+ ///
208
+ /// This test previously asserted the opposite, as a finding: `state:current`
209
+ /// carried the global current pointer *and* the whole branch map in one
210
+ /// replicated record — the record behind both defects the branching audit
211
+ /// reported — and `commit_revision` wrote it unconditionally, so a caller that
212
+ /// never created a branch did not avoid it.
213
+ ///
214
+ /// That record no longer exists. The store's entire durable schema is
215
+ /// `state:revision:{hex}`, one record per revision, and nothing else is
216
+ /// written. The two defects were removed with it rather than repaired.
217
+ #[test]
218
+ fn no_commit_writes_a_shared_mutable_record() {
219
+ let directory = TempDir::new().unwrap();
220
+ let db = Arc::new(FeltDb::open(directory.path().join("shared.log")).unwrap());
221
+ let store = StateStore::with_feltdb(db.clone()).unwrap();
222
+
223
+ let base = store
224
+ .create("docs:1", r#"{"v":1}"#.into(), "a".into())
225
+ .unwrap();
226
+ let child = store
227
+ .commit(r#"{"v":2}"#.into(), &base, "a".into())
228
+ .unwrap();
229
+
230
+ assert!(
231
+ db.get::<(String, HashMap<String, String>)>("state:current")
232
+ .unwrap()
233
+ .is_none(),
234
+ "the record that carried the global pointer and the branch map is gone"
235
+ );
236
+
237
+ // What *is* written is one record per revision, and nothing else.
238
+ for revision in [&base, &child] {
239
+ assert_eq!(
240
+ store
241
+ .get(&revision.id)
242
+ .expect("revision is durable")
243
+ .content,
244
+ revision.content
245
+ );
246
+ }
247
+ }
248
+
249
+ /// One damaged record no longer refuses the whole store. **Regression.**
250
+ ///
251
+ /// This test previously asserted the opposite, as a finding: `with_feltdb`
252
+ /// eagerly loaded every revision, re-verified every content hash, and validated
253
+ /// every parent reference before returning, so a single orphaned record that no
254
+ /// caller would ever request refused the entire store.
255
+ ///
256
+ /// There is no recovery step now. Reads are keyed and independent, so a damaged
257
+ /// or unrelated record is simply a record nobody asked for. This is the
258
+ /// operational difference between an index that must be rebuilt and records
259
+ /// addressed by content.
260
+ #[test]
261
+ fn one_damaged_record_does_not_refuse_the_whole_store() {
262
+ let directory = TempDir::new().unwrap();
263
+ let path = directory.path().join("domain.log");
264
+
265
+ let good_id = {
266
+ let db = Arc::new(FeltDb::open(&path).unwrap());
267
+ let store = StateStore::with_feltdb(db.clone()).unwrap();
268
+ let good = store
269
+ .create("docs:1", r#"{"v":1}"#.into(), "a".into())
270
+ .unwrap();
271
+
272
+ // An unrelated revision whose parent was never stored. Nothing the
273
+ // caller will ever ask for.
274
+ let orphan = StateRevision {
275
+ id: StateId::compute(r#"{"orphan":true}"#),
276
+ resource: "docs:1".into(),
277
+ content: r#"{"orphan":true}"#.into(),
278
+ content_id: StateId::compute(r#"{"orphan":true}"#),
279
+ sequence: 0,
280
+ parent_id: Some(StateId::compute("a parent that was never stored")),
281
+ authority: "x".into(),
282
+ timestamp_ms: 0,
283
+ metadata: Default::default(),
284
+ };
285
+ db.insert(&format!("state:revision:{}", orphan.id.as_hex()), orphan)
286
+ .unwrap();
287
+ good.id
288
+ };
289
+
290
+ let db = Arc::new(FeltDb::open(&path).unwrap());
291
+ let store = StateStore::with_feltdb(db).expect("the store opens despite the orphan");
292
+
293
+ // The intact revision is readable by identity, unaffected by its neighbour.
294
+ let good = store
295
+ .get(&good_id)
296
+ .expect("the intact revision is readable");
297
+ assert!(good.verify_integrity());
298
+ assert_eq!(good.content, r#"{"v":1}"#);
299
+ }
@@ -0,0 +1,255 @@
1
+ //! Evidence for the sync divergence boundary audit
2
+ //! (`docs/audits/SYNC-DIVERGENCE-AUDIT.md`).
3
+ //!
4
+ //! Adds **no capability and no product surface**. It tests one question:
5
+ //!
6
+ //! > Is there a concrete transition where two independently valid states need
7
+ //! > to coexist durably — and does a durable representation for the second one
8
+ //! > already exist?
9
+ //!
10
+ //! Both answers are yes, and the second is the surprising one: `SyncStore`
11
+ //! already accepts `ReconciliationOutcome::Conflict` and persists the operation
12
+ //! with its complete proposed value. The representation is not missing. The
13
+ //! server reaches the conflict, declines to record it, and `continue`s.
14
+
15
+ use feltdb::authorization::{
16
+ AuthorizationContext, Grant, GrantConstraints, GrantSigner, GrantStatus, Resource,
17
+ ResourceScope, ResourceScopeKind, Subject,
18
+ };
19
+ use feltdb::state_contract::{TransactionOperation, TransactionOperationKind};
20
+ use feltdb::sync_contract::{ReconciliationOutcome, SyncOperation, SyncStore};
21
+ use serde_json::json;
22
+ use std::collections::BTreeMap;
23
+ use tempfile::TempDir;
24
+
25
+ /// A pushed operation for `flow://a/tasks/1` carrying `value` as its proposed
26
+ /// state, descending from server position `causal_parent`.
27
+ fn operation(id: &str, value: serde_json::Value, causal_parent: Option<u64>) -> SyncOperation {
28
+ let signer = GrantSigner::new("k", vec![9; 32]).unwrap();
29
+ let grant = Grant {
30
+ contract_version: 1,
31
+ id: "g".into(),
32
+ issuer: Subject::Human("h".into()),
33
+ subject: Subject::Human("u".into()),
34
+ tenant_id: "t".into(),
35
+ application_id: "a".into(),
36
+ capabilities: ["state:write".into(), "state:read".into()].into(),
37
+ resources: vec![ResourceScope {
38
+ kind: ResourceScopeKind::ResourcePrefix,
39
+ uri: "flow://a/tasks/*".into(),
40
+ }],
41
+ constraints: GrantConstraints::default(),
42
+ issued_at: 1,
43
+ expires_at: 999,
44
+ parent_grant: None,
45
+ revision: 1,
46
+ status: GrantStatus::Active,
47
+ };
48
+ let signed = signer.sign(grant).unwrap();
49
+
50
+ let mut causal_context = BTreeMap::new();
51
+ if let Some(position) = causal_parent {
52
+ causal_context.insert("server".to_string(), position);
53
+ }
54
+
55
+ SyncOperation {
56
+ contract_version: 1,
57
+ operation_id: id.into(),
58
+ tenant_id: "t".into(),
59
+ application_id: "a".into(),
60
+ subject: Subject::Human("u".into()),
61
+ device_id: "d".into(),
62
+ resource: Resource {
63
+ uri: "flow://a/tasks/1".into(),
64
+ attributes: BTreeMap::new(),
65
+ },
66
+ operation: TransactionOperation {
67
+ kind: TransactionOperationKind::Update,
68
+ collection: "tasks".into(),
69
+ id: "1".into(),
70
+ value,
71
+ if_version: None,
72
+ },
73
+ causal_context,
74
+ schema_version: 1,
75
+ authorization_context: AuthorizationContext {
76
+ timestamp: 10,
77
+ ..Default::default()
78
+ },
79
+ offline_grant: signed,
80
+ application_revision: "r1".into(),
81
+ created_at: 10,
82
+ }
83
+ }
84
+
85
+ // ---------------------------------------------------------------------------
86
+ // What arrives at the boundary
87
+ // ---------------------------------------------------------------------------
88
+
89
+ /// A pushed operation carries a **complete proposed state**, not a delta.
90
+ ///
91
+ /// This is the first half of what reconciliation needs. `TransactionOperation`
92
+ /// is `{kind, collection, id, value, if_version}` and `value` is a whole
93
+ /// `serde_json::Value` — the same shape `/v1/data` accepts. So the remote side
94
+ /// of a divergence arrives at the server fully formed, and is present in memory
95
+ /// at the exact moment the conflict is detected.
96
+ #[test]
97
+ fn a_pushed_operation_carries_a_complete_proposed_state() {
98
+ let remote = operation("op-remote", json!({"title": "written offline"}), Some(1));
99
+
100
+ assert_eq!(
101
+ remote.operation.value,
102
+ json!({"title": "written offline"}),
103
+ "the proposed state is carried whole, not as a change description"
104
+ );
105
+ assert_eq!(
106
+ remote.causal_context.get("server"),
107
+ Some(&1),
108
+ "and it names the server position it descended from"
109
+ );
110
+ }
111
+
112
+ // ---------------------------------------------------------------------------
113
+ // The durable representation already exists
114
+ // ---------------------------------------------------------------------------
115
+
116
+ /// `SyncStore` can already record a conflicting operation, durably, with its
117
+ /// full proposed state.
118
+ ///
119
+ /// This is the audit's central result. `record` takes a
120
+ /// `ReconciliationOutcome`, and `Conflict` is one of its variants. Recording a
121
+ /// losing operation persists it at its own position, retains its complete
122
+ /// value, and it survives reload — so the "losing state is annihilated" problem
123
+ /// the current-revision audit found in the CAS path **does not apply here for
124
+ /// want of a place to put it**. The place exists.
125
+ ///
126
+ /// The server does not use it: at the conflict site it pushes a `SyncConflict`
127
+ /// and `continue`s, skipping both `record` and the transaction.
128
+ #[test]
129
+ fn the_sync_store_can_already_record_a_conflicting_operation_durably() {
130
+ let directory = TempDir::new().unwrap();
131
+ let path = directory.path().join("sync.json");
132
+
133
+ let accepted_position;
134
+ let conflicting_position;
135
+ {
136
+ let mut store = SyncStore::load(&path).unwrap();
137
+
138
+ accepted_position = store
139
+ .record(
140
+ operation("op-local", json!({"title": "written on server"}), None),
141
+ ReconciliationOutcome::Accept,
142
+ None,
143
+ 100,
144
+ )
145
+ .expect("the winning operation records");
146
+
147
+ // The losing side, recorded rather than discarded.
148
+ conflicting_position = store
149
+ .record(
150
+ operation("op-remote", json!({"title": "written offline"}), Some(1)),
151
+ ReconciliationOutcome::Conflict,
152
+ None,
153
+ 101,
154
+ )
155
+ .expect("a conflicting operation records just as well");
156
+ }
157
+
158
+ // Both survive a reload, with their full states intact.
159
+ let reloaded = SyncStore::load(&path).unwrap();
160
+ let accepted = reloaded
161
+ .operations
162
+ .iter()
163
+ .find(|stored| stored.position == accepted_position)
164
+ .expect("the accepted operation is durable");
165
+ let conflicting = reloaded
166
+ .operations
167
+ .iter()
168
+ .find(|stored| stored.position == conflicting_position)
169
+ .expect("the conflicting operation is durable too");
170
+
171
+ assert!(matches!(accepted.outcome, ReconciliationOutcome::Accept));
172
+ assert!(matches!(
173
+ conflicting.outcome,
174
+ ReconciliationOutcome::Conflict
175
+ ));
176
+ assert_eq!(
177
+ conflicting.operation.operation.value,
178
+ json!({"title": "written offline"}),
179
+ "the losing side's complete proposed state is retained"
180
+ );
181
+ assert_eq!(
182
+ conflicting.operation.causal_context.get("server"),
183
+ Some(&1),
184
+ "along with the position it claimed to descend from"
185
+ );
186
+ }
187
+
188
+ /// Retained operations reconstruct the state a resource held at a position.
189
+ ///
190
+ /// This is where `base` would come from. Operations are appended with a
191
+ /// monotonic `position` and never pruned, and each carries the whole value that
192
+ /// was applied, so "what did this resource look like at server position N" is
193
+ /// answerable by lookup — **for the part of its history that arrived through
194
+ /// sync**. See the audit for why that qualifier is the load-bearing one.
195
+ #[test]
196
+ fn retained_operations_reconstruct_the_state_at_a_position() {
197
+ let directory = TempDir::new().unwrap();
198
+ let mut store = SyncStore::load(directory.path().join("history.json")).unwrap();
199
+
200
+ let first = store
201
+ .record(
202
+ operation("op-1", json!({"title": "v1"}), None),
203
+ ReconciliationOutcome::Accept,
204
+ None,
205
+ 10,
206
+ )
207
+ .unwrap();
208
+ let second = store
209
+ .record(
210
+ operation("op-2", json!({"title": "v2"}), Some(first)),
211
+ ReconciliationOutcome::Accept,
212
+ None,
213
+ 11,
214
+ )
215
+ .unwrap();
216
+
217
+ let at = |position: u64| {
218
+ store
219
+ .operations
220
+ .iter()
221
+ .find(|stored| {
222
+ stored.position == position && stored.operation.resource.uri == "flow://a/tasks/1"
223
+ })
224
+ .map(|stored| stored.operation.operation.value.clone())
225
+ };
226
+
227
+ assert_eq!(at(first), Some(json!({"title": "v1"})));
228
+ assert_eq!(at(second), Some(json!({"title": "v2"})));
229
+ assert!(second > first, "positions are monotonic");
230
+
231
+ // Nothing prunes: the earlier state is still there after the later one.
232
+ assert_eq!(store.operations.len(), 2);
233
+ }
234
+
235
+ /// The outcome vocabulary already distinguishes the cases a reconciler needs.
236
+ ///
237
+ /// Recorded because it bounds how much vocabulary a future change would have to
238
+ /// invent: none. What is missing is a *decision procedure*, not a way to say
239
+ /// what was decided.
240
+ #[test]
241
+ fn the_outcome_vocabulary_already_names_a_conflict() {
242
+ let outcomes = [
243
+ ReconciliationOutcome::Accept,
244
+ ReconciliationOutcome::Conflict,
245
+ ReconciliationOutcome::Reject,
246
+ ReconciliationOutcome::Stale,
247
+ ReconciliationOutcome::AlreadyCommitted,
248
+ ];
249
+ // Each is representable and round-trips, so a stored decision is durable.
250
+ for outcome in outcomes {
251
+ let text = serde_json::to_string(&outcome).unwrap();
252
+ let back: ReconciliationOutcome = serde_json::from_str(&text).unwrap();
253
+ assert_eq!(format!("{outcome:?}"), format!("{back:?}"));
254
+ }
255
+ }