@feltdb/core 0.8.3 → 0.8.5

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 (193) hide show
  1. package/dist/cli/commands.js +4 -1
  2. package/dist/cli/provisioning-neutrality.js +79 -0
  3. package/dist/collection.d.ts +43 -1
  4. package/dist/collection.d.ts.map +1 -1
  5. package/dist/collection.js +192 -22
  6. package/dist/create/create.js +25 -21
  7. package/dist/create/managed-account.js +11 -0
  8. package/dist/create/package-versions.js +1 -1
  9. package/dist/create/server-source/Cargo.lock +165 -0
  10. package/dist/create/server-source/Cargo.toml +9 -0
  11. package/dist/create/server-source/crates/feltdb/Cargo.toml +3 -0
  12. package/dist/create/server-source/crates/feltdb/benches/gate13_baseline.rs +44 -44
  13. package/dist/create/server-source/crates/feltdb/benches/gate13_phase_7_1_release_economics.rs +12 -24
  14. package/dist/create/server-source/crates/feltdb/benches/gate_13_redux.rs +7 -13
  15. package/dist/create/server-source/crates/feltdb/benches/gate_13_regression_runner.rs +13 -10
  16. package/dist/create/server-source/crates/feltdb/benches/gate_14a_concurrent_writer_scaling.rs +12 -9
  17. package/dist/create/server-source/crates/feltdb/benches/gate_14a_production_admission_revalidation.rs +78 -25
  18. package/dist/create/server-source/crates/feltdb/benches/gate_14a_rc2_admission_contract.rs +16 -13
  19. package/dist/create/server-source/crates/feltdb/benches/gate_14a_rc_root_cause.rs +13 -5
  20. package/dist/create/server-source/crates/feltdb/benches/gate_14a_sync1_queued_prototype.rs +41 -22
  21. package/dist/create/server-source/crates/feltdb/benches/gate_14a_sync_economics.rs +33 -15
  22. package/dist/create/server-source/crates/feltdb/benches/gate_14b_causal_backlog_scaling.rs +100 -33
  23. package/dist/create/server-source/crates/feltdb/benches/gate_14c_replication_contract_test.rs +56 -20
  24. package/dist/create/server-source/crates/feltdb/benches/gate_14c_replication_scaling.rs +116 -41
  25. package/dist/create/server-source/crates/feltdb/benches/gate_14d_combined_dimension_scaling.rs +186 -55
  26. package/dist/create/server-source/crates/feltdb/benches/phase_7_1_2_optimization_benchmark.rs +64 -26
  27. package/dist/create/server-source/crates/feltdb/benches/phase_7_1_3_crossover_analysis.rs +46 -15
  28. package/dist/create/server-source/crates/feltdb/src/admission.rs +8 -15
  29. package/dist/create/server-source/crates/feltdb/src/admission_contract_tests.rs +43 -13
  30. package/dist/create/server-source/crates/feltdb/src/adversarial_transport.rs +15 -42
  31. package/dist/create/server-source/crates/feltdb/src/analytics.rs +65 -19
  32. package/dist/create/server-source/crates/feltdb/src/application.rs +113 -30
  33. package/dist/create/server-source/crates/feltdb/src/authorization_security_tests.rs +475 -140
  34. package/dist/create/server-source/crates/feltdb/src/cardinality_diagnostics.rs +17 -15
  35. package/dist/create/server-source/crates/feltdb/src/cardinality_endpoint.rs +0 -1
  36. package/dist/create/server-source/crates/feltdb/src/causal_backlog_bound.rs +59 -15
  37. package/dist/create/server-source/crates/feltdb/src/causal_dependency_barrier.rs +266 -114
  38. package/dist/create/server-source/crates/feltdb/src/causal_dependency_barrier_phase_7_1.rs +25 -7
  39. package/dist/create/server-source/crates/feltdb/src/concurrency_fuzzing.rs +10 -15
  40. package/dist/create/server-source/crates/feltdb/src/consistency_contract.rs +3 -11
  41. package/dist/create/server-source/crates/feltdb/src/crash_atomic_boundary.rs +14 -5
  42. package/dist/create/server-source/crates/feltdb/src/crash_injection.rs +21 -25
  43. package/dist/create/server-source/crates/feltdb/src/crash_recovery_tests.rs +14 -11
  44. package/dist/create/server-source/crates/feltdb/src/dedup_bound_investigation.rs +103 -22
  45. package/dist/create/server-source/crates/feltdb/src/distributed_indexing.rs +18 -15
  46. package/dist/create/server-source/crates/feltdb/src/durability_guarantees.rs +12 -8
  47. package/dist/create/server-source/crates/feltdb/src/durable_dedup_set.rs +1 -5
  48. package/dist/create/server-source/crates/feltdb/src/durable_operation_identity.rs +87 -23
  49. package/dist/create/server-source/crates/feltdb/src/durable_operation_log.rs +3 -7
  50. package/dist/create/server-source/crates/feltdb/src/durable_sync.rs +10 -9
  51. package/dist/create/server-source/crates/feltdb/src/equality_index.rs +595 -0
  52. package/dist/create/server-source/crates/feltdb/src/in_process_transport.rs +1 -6
  53. package/dist/create/server-source/crates/feltdb/src/indexing.rs +35 -38
  54. package/dist/create/server-source/crates/feltdb/src/lib.rs +1050 -117
  55. package/dist/create/server-source/crates/feltdb/src/managed_cas_tests.rs +4 -1
  56. package/dist/create/server-source/crates/feltdb/src/metrics.rs +0 -1
  57. package/dist/create/server-source/crates/feltdb/src/multi_node_convergence.rs +1 -2
  58. package/dist/create/server-source/crates/feltdb/src/multi_operation_transaction.rs +107 -30
  59. package/dist/create/server-source/crates/feltdb/src/observability.rs +19 -6
  60. package/dist/create/server-source/crates/feltdb/src/operation_algebra.rs +12 -11
  61. package/dist/create/server-source/crates/feltdb/src/operation_log.rs +9 -4
  62. package/dist/create/server-source/crates/feltdb/src/p1_application_atomicity.rs +65 -18
  63. package/dist/create/server-source/crates/feltdb/src/p1_atomicity_acceptance.rs +193 -57
  64. package/dist/create/server-source/crates/feltdb/src/partition_reconciliation.rs +37 -27
  65. package/dist/create/server-source/crates/feltdb/src/permutation_scheduler.rs +38 -10
  66. package/dist/create/server-source/crates/feltdb/src/persistence_reality.rs +20 -14
  67. package/dist/create/server-source/crates/feltdb/src/phase1b_acceptance.rs +394 -229
  68. package/dist/create/server-source/crates/feltdb/src/phase1c1_acceptance.rs +8 -6
  69. package/dist/create/server-source/crates/feltdb/src/phase1c2_acceptance.rs +11 -13
  70. package/dist/create/server-source/crates/feltdb/src/phase1c3_acceptance.rs +90 -72
  71. package/dist/create/server-source/crates/feltdb/src/phase1c_atomicity_proof.rs +3 -3
  72. package/dist/create/server-source/crates/feltdb/src/phase5_integration.rs +33 -11
  73. package/dist/create/server-source/crates/feltdb/src/phase5_scenarios.rs +6 -6
  74. package/dist/create/server-source/crates/feltdb/src/phase6_adversarial_scenarios.rs +14 -56
  75. package/dist/create/server-source/crates/feltdb/src/phase6_convergence_validator.rs +29 -27
  76. package/dist/create/server-source/crates/feltdb/src/phase6_persistence.rs +35 -17
  77. package/dist/create/server-source/crates/feltdb/src/phase_1c_real_tcp.rs +8 -2
  78. package/dist/create/server-source/crates/feltdb/src/phase_2a_failures.rs +59 -15
  79. package/dist/create/server-source/crates/feltdb/src/phase_2b_network.rs +70 -17
  80. package/dist/create/server-source/crates/feltdb/src/phase_2c_cascading.rs +23 -6
  81. package/dist/create/server-source/crates/feltdb/src/phase_3_durability.rs +12 -3
  82. package/dist/create/server-source/crates/feltdb/src/phase_4_baseline.rs +41 -11
  83. package/dist/create/server-source/crates/feltdb/src/phase_5_soak.rs +56 -25
  84. package/dist/create/server-source/crates/feltdb/src/policy_evaluation.rs +701 -245
  85. package/dist/create/server-source/crates/feltdb/src/production_api.rs +31 -13
  86. package/dist/create/server-source/crates/feltdb/src/query_execution_diagnostics.rs +126 -0
  87. package/dist/create/server-source/crates/feltdb/src/query_performance.rs +6 -8
  88. package/dist/create/server-source/crates/feltdb/src/replay_fuzzing.rs +5 -5
  89. package/dist/create/server-source/crates/feltdb/src/replica_acknowledgements.rs +48 -18
  90. package/dist/create/server-source/crates/feltdb/src/replica_membership.rs +30 -11
  91. package/dist/create/server-source/crates/feltdb/src/replication_manager.rs +6 -3
  92. package/dist/create/server-source/crates/feltdb/src/replication_protocol.rs +4 -3
  93. package/dist/create/server-source/crates/feltdb/src/sharding.rs +36 -10
  94. package/dist/create/server-source/crates/feltdb/src/state_conflict_contract.rs +516 -0
  95. package/dist/create/server-source/crates/feltdb/src/state_contract.rs +305 -6
  96. package/dist/create/server-source/crates/feltdb/src/state_diff_contract.rs +222 -0
  97. package/dist/create/server-source/crates/feltdb/src/state_facade.rs +82 -54
  98. package/dist/create/server-source/crates/feltdb/src/state_hash.rs +2 -2
  99. package/dist/create/server-source/crates/feltdb/src/state_model.rs +1514 -537
  100. package/dist/create/server-source/crates/feltdb/src/state_transition_store.rs +6 -3
  101. package/dist/create/server-source/crates/feltdb/src/state_trigger.rs +672 -0
  102. package/dist/create/server-source/crates/feltdb/src/submission.rs +5 -11
  103. package/dist/create/server-source/crates/feltdb/src/sync.rs +12 -0
  104. package/dist/create/server-source/crates/feltdb/src/tcp_transport.rs +6 -8
  105. package/dist/create/server-source/crates/feltdb/src/transaction_api.rs +24 -35
  106. package/dist/create/server-source/crates/feltdb/src/transaction_invariants.rs +24 -8
  107. package/dist/create/server-source/crates/feltdb/src/transaction_preconditions.rs +248 -59
  108. package/dist/create/server-source/crates/feltdb/src/transactions.rs +17 -20
  109. package/dist/create/server-source/crates/feltdb/src/trigger_contract.rs +749 -0
  110. package/dist/create/server-source/crates/feltdb/src/worker_mesh.rs +1 -0
  111. package/dist/create/server-source/crates/feltdb/src/workload.rs +512 -4
  112. package/dist/create/server-source/crates/feltdb/src/workload_diagnostics.rs +442 -0
  113. package/dist/create/server-source/crates/feltdb/tests/branching_evidence.rs +299 -0
  114. package/dist/create/server-source/crates/feltdb/tests/current_revision_authority_evidence.rs +288 -0
  115. package/dist/create/server-source/crates/feltdb/tests/durable_format_compatibility.rs +392 -0
  116. package/dist/create/server-source/crates/feltdb/tests/feltdb_state_boundary_tests.rs +436 -220
  117. package/dist/create/server-source/crates/feltdb/tests/fixtures/state_conflict_contract_corpus.json +1916 -0
  118. package/dist/create/server-source/crates/feltdb/tests/fixtures/state_diff_contract_corpus.json +1878 -0
  119. package/dist/create/server-source/crates/feltdb/tests/fixtures/trigger_contract_corpus.json +1862 -0
  120. package/dist/create/server-source/crates/feltdb/tests/pr34_query_collection.rs +234 -0
  121. package/dist/create/server-source/crates/feltdb/tests/pr35_equality_index.rs +947 -0
  122. package/dist/create/server-source/crates/feltdb/tests/pr7_self_authorization_proof.rs +5 -8
  123. package/dist/create/server-source/crates/feltdb/tests/pr8_vocabulary_assessment.rs +52 -44
  124. package/dist/create/server-source/crates/feltdb/tests/pr9_phase2_boundary_tests.rs +33 -16
  125. package/dist/create/server-source/crates/feltdb/tests/pr9_phase3a_path_a_tests.rs +22 -7
  126. package/dist/create/server-source/crates/feltdb/tests/pr9_phase3c_authorized_mutations.rs +41 -22
  127. package/dist/create/server-source/crates/feltdb/tests/pr9_phase3c_role_based_authorization.rs +25 -8
  128. package/dist/create/server-source/crates/feltdb/tests/pr9_phase3c_simple_auth_delete.rs +9 -6
  129. package/dist/create/server-source/crates/feltdb/tests/pr9_phase3c_team_delete_role_authorization.rs +120 -69
  130. package/dist/create/server-source/crates/feltdb/tests/pr9_teams_role_based_access.rs +21 -10
  131. package/dist/create/server-source/crates/feltdb/tests/production_readiness_contract.rs +1032 -0
  132. package/dist/create/server-source/crates/feltdb/tests/reconciliation_application.rs +868 -0
  133. package/dist/create/server-source/crates/feltdb/tests/reconciliation_wire_format_evidence.rs +221 -0
  134. package/dist/create/server-source/crates/feltdb/tests/resource_scoped_revisions.rs +338 -0
  135. package/dist/create/server-source/crates/feltdb/tests/revision_identity_contract.rs +1039 -0
  136. package/dist/create/server-source/crates/feltdb/tests/revision_model_decision.rs +739 -0
  137. package/dist/create/server-source/crates/feltdb/tests/revision_retention_boundary_evidence.rs +427 -0
  138. package/dist/create/server-source/crates/feltdb/tests/saas_authorization_integration.rs +3 -3
  139. package/dist/create/server-source/crates/feltdb/tests/saas_invitation_lifecycle.rs +25 -22
  140. package/dist/create/server-source/crates/feltdb/tests/state_conflict_contract_conformance.rs +1799 -0
  141. package/dist/create/server-source/crates/feltdb/tests/state_diff_contract_conformance.rs +1316 -0
  142. package/dist/create/server-source/crates/feltdb/tests/state_model_integration.rs +53 -61
  143. package/dist/create/server-source/crates/feltdb/tests/state_persistence_integration.rs +156 -61
  144. package/dist/create/server-source/crates/feltdb/tests/state_store_boundary_evidence.rs +299 -0
  145. package/dist/create/server-source/crates/feltdb/tests/sync_divergence_evidence.rs +255 -0
  146. package/dist/create/server-source/crates/feltdb/tests/three_way_input_boundary_evidence.rs +249 -0
  147. package/dist/create/server-source/crates/feltdb/tests/trigger_contract_conformance.rs +994 -0
  148. package/dist/create/server-source/crates/feltdb-server/src/app_state.rs +2 -1
  149. package/dist/create/server-source/crates/feltdb-server/src/audit.rs +1137 -29
  150. package/dist/create/server-source/crates/feltdb-server/src/auth.rs +164 -13
  151. package/dist/create/server-source/crates/feltdb-server/src/main.rs +800 -34
  152. package/dist/db.d.ts +33 -34
  153. package/dist/db.d.ts.map +1 -1
  154. package/dist/db.js +74 -20
  155. package/dist/deployment.d.ts +30 -0
  156. package/dist/deployment.d.ts.map +1 -0
  157. package/dist/deployment.js +130 -0
  158. package/dist/embedded-transaction.d.ts +22 -4
  159. package/dist/embedded-transaction.d.ts.map +1 -1
  160. package/dist/embedded-transaction.js +51 -5
  161. package/dist/feltdb.d.ts +14 -2
  162. package/dist/feltdb.d.ts.map +1 -1
  163. package/dist/file-db.js +1 -1
  164. package/dist/http-client.d.ts +14 -0
  165. package/dist/http-client.d.ts.map +1 -1
  166. package/dist/http-client.js +23 -5
  167. package/dist/http-db.d.ts +119 -1
  168. package/dist/http-db.d.ts.map +1 -1
  169. package/dist/http-db.js +346 -31
  170. package/dist/index-core.d.ts +2 -0
  171. package/dist/index-core.d.ts.map +1 -1
  172. package/dist/index-core.js +2 -0
  173. package/dist/index.d.ts.map +1 -1
  174. package/dist/index.js +9 -0
  175. package/dist/indexeddb-db.d.ts.map +1 -1
  176. package/dist/indexeddb-db.js +35 -21
  177. package/dist/managed-recovery.d.ts +192 -0
  178. package/dist/managed-recovery.d.ts.map +1 -0
  179. package/dist/managed-recovery.js +242 -0
  180. package/dist/memory-db.js +1 -1
  181. package/dist/studio-app/assets/{feltdb_wasm-DB8cX151.js → feltdb_wasm-DaNwCLRX.js} +1 -1
  182. package/dist/studio-app/assets/feltdb_wasm_bg-DnsHNv6g.wasm +0 -0
  183. package/dist/studio-app/assets/index-j8IlhNqJ.js +29 -0
  184. package/dist/studio-app/index.html +1 -1
  185. package/dist/transaction.d.ts +30 -0
  186. package/dist/transaction.d.ts.map +1 -1
  187. package/dist/transaction.js +41 -0
  188. package/dist/wasm/feltdb_wasm_bg.wasm +0 -0
  189. package/dist/workload.d.ts +2 -1
  190. package/dist/workload.d.ts.map +1 -1
  191. package/package.json +1 -1
  192. package/dist/studio-app/assets/feltdb_wasm_bg-ClhDHp0S.wasm +0 -0
  193. package/dist/studio-app/assets/index-B0k4UAlI.js +0 -29
@@ -0,0 +1,299 @@
1
+ //! Executable evidence for the Live State Branching audit
2
+ //! (`docs/audits/BRANCHING-AUDIT.md`).
3
+ //!
4
+ //! These tests add no capability. Every one of them only *observes* what
5
+ //! `feltdb::state_model` already does, so that the audit's branching claims
6
+ //! rest on a run rather than on a reading. Where a test asserts an absence, the
7
+ //! absence is the finding: it is what separates "FeltDB has branching" from
8
+ //! "FeltDB has the durable lineage half of branching".
9
+
10
+ use feltdb::state_model::StateStore;
11
+ use feltdb::{
12
+ reconcile, ConflictClass, ConflictClassification, ReconciliationPlan, Relationship,
13
+ SemanticDiff, StateTopology,
14
+ };
15
+ use serde_json::json;
16
+ use std::sync::Arc;
17
+ use tempfile::TempDir;
18
+
19
+ fn durable_store(directory: &TempDir, name: &str) -> StateStore {
20
+ let db = feltdb::FeltDb::open(directory.path().join(name)).expect("open");
21
+ StateStore::with_feltdb(Arc::new(db)).expect("store")
22
+ }
23
+
24
+ /// Q2 — **superseded.** Named branches no longer exist; a revision *is* the
25
+ /// durable reference.
26
+ ///
27
+ /// This test previously asserted that a branch is a durable named pointer at a
28
+ /// `StateId` and survives a restart. Named branches were removed from
29
+ /// `StateStore` because the record that held them, `state:current`, was the
30
+ /// same record that held the global current pointer, and it carried both of the
31
+ /// defects Q9 and Q11 recorded.
32
+ ///
33
+ /// What survives is the load-bearing part: a revision is durable, and it is
34
+ /// addressed by an identity derived from its own content — which is exactly
35
+ /// what a branch name pointed at. The name was an alias for a value the caller
36
+ /// already had.
37
+ #[test]
38
+ fn a_revision_is_its_own_durable_reference_across_restart() {
39
+ let directory = TempDir::new().unwrap();
40
+ let base = {
41
+ let store = durable_store(&directory, "state.log");
42
+ store
43
+ .create(
44
+ "docs:1",
45
+ json!({"balance": 100}).to_string(),
46
+ "authority".into(),
47
+ )
48
+ .expect("create")
49
+ };
50
+
51
+ let reopened = durable_store(&directory, "state.log");
52
+ let recovered = reopened
53
+ .get(&base.id)
54
+ .expect("a revision must survive a restart under its own identity");
55
+ assert_eq!(recovered.content, base.content);
56
+ assert!(
57
+ recovered.verify_integrity(),
58
+ "and still hash to the identity it was addressed by"
59
+ );
60
+ }
61
+
62
+ /// Q3/Q4 — mutation is **not** copy-on-write, and revisions share nothing.
63
+ ///
64
+ /// Each revision carries the entire canonical state as its own `content`
65
+ /// string. Two revisions that differ in one field still hold two full copies,
66
+ /// so a "clone" costs the whole state. This is the storage half of branching
67
+ /// FeltDB does not have.
68
+ #[test]
69
+ fn every_revision_holds_a_full_copy_of_state_rather_than_a_delta() {
70
+ let store = StateStore::new_volatile();
71
+ let filler = "x".repeat(4096);
72
+ let base = store
73
+ .create(
74
+ "docs:1",
75
+ json!({"blob": filler, "counter": 0}).to_string(),
76
+ "a".into(),
77
+ )
78
+ .expect("create");
79
+ let child = store
80
+ .commit(
81
+ json!({"blob": filler, "counter": 1}).to_string(),
82
+ &base,
83
+ "a".into(),
84
+ )
85
+ .expect("commit");
86
+
87
+ // One field changed; both revisions are still full-size.
88
+ assert!(base.content.len() > 4096);
89
+ assert!(child.content.len() > 4096);
90
+ assert_eq!(
91
+ base.content.len(),
92
+ child.content.len(),
93
+ "a one-field change produced two same-sized full copies, not a delta"
94
+ );
95
+ // And the parent link is the only thing they share: no structural sharing.
96
+ assert_eq!(child.parent_id.as_ref(), Some(&base.id));
97
+ assert_ne!(base.content, child.content);
98
+ }
99
+
100
+ /// Q5/Q6/Q7 — divergence, deterministic diff and conflict classification all
101
+ /// work, but the topology is a structure the *caller* has to assemble.
102
+ ///
103
+ /// `StateStore` exposes no topology of its own, so ancestry questions are only
104
+ /// answerable by a caller that has already collected the revisions.
105
+ #[test]
106
+ fn divergence_diff_and_conflict_classification_work_over_a_caller_built_topology() {
107
+ let store = StateStore::new_volatile();
108
+ let base = store
109
+ .create(
110
+ "docs:1",
111
+ json!({"alice": 100, "bob": 50}).to_string(),
112
+ "a".into(),
113
+ )
114
+ .expect("create");
115
+ let left = store
116
+ .commit(
117
+ json!({"alice": 150, "bob": 50}).to_string(),
118
+ &base,
119
+ "a".into(),
120
+ )
121
+ .expect("left");
122
+ let right = store
123
+ .commit(
124
+ json!({"alice": 100, "bob": 150}).to_string(),
125
+ &base,
126
+ "a".into(),
127
+ )
128
+ .expect("right");
129
+
130
+ let mut topology = StateTopology::new();
131
+ for revision in [&base, &left, &right] {
132
+ topology.add_revision(revision.clone());
133
+ }
134
+ assert!(matches!(
135
+ topology.relationship(&left.id, &right.id),
136
+ Relationship::Diverged
137
+ ));
138
+ assert_eq!(
139
+ topology.common_ancestor(&left.id, &right.id),
140
+ Some(base.id.clone())
141
+ );
142
+
143
+ let base_value = serde_json::from_str(&base.content).unwrap();
144
+ let left_value = serde_json::from_str(&left.content).unwrap();
145
+ let diff = SemanticDiff::compute(&base_value, &left_value);
146
+ assert_eq!(diff.changes.len(), 1, "diff is deterministic and minimal");
147
+
148
+ let classification = ConflictClassification::classify(&base, &left, &right);
149
+ assert_eq!(classification.overall, ConflictClass::Independent);
150
+ }
151
+
152
+ /// Q8 — a plan can now be **applied**, but a branch still cannot be
153
+ /// **reconciled**.
154
+ ///
155
+ /// `apply_reconciliation_plan` turns an explicitly chosen plan into canonical
156
+ /// content, and `reconcile` materializes that content as a `StateRevision`
157
+ /// through the ordinary revision model. That is the half this evidence used to
158
+ /// record as missing entirely.
159
+ ///
160
+ /// What is still missing is the other half, and it is the half that would make
161
+ /// this "branch reconciliation": nothing *chooses* a plan, and applying one
162
+ /// moves no branch head. The caller supplies the parent choice and every
163
+ /// override, and the branch points exactly where it did afterwards. FeltDB
164
+ /// materializes the decision; it does not make it, and it does not record it as
165
+ /// the branch's new position.
166
+ #[test]
167
+ fn a_plan_can_be_applied_but_applying_one_reconciles_no_branch() {
168
+ let store = StateStore::new_volatile();
169
+ let base = store
170
+ .create(
171
+ "docs:1",
172
+ json!({"alice": 100, "bob": 50}).to_string(),
173
+ "a".into(),
174
+ )
175
+ .expect("create");
176
+ let left = store
177
+ .commit(
178
+ json!({"alice": 150, "bob": 50}).to_string(),
179
+ &base,
180
+ "a".into(),
181
+ )
182
+ .expect("left");
183
+ let right = store
184
+ .commit(
185
+ json!({"alice": 100, "bob": 150}).to_string(),
186
+ &base,
187
+ "a".into(),
188
+ )
189
+ .expect("right");
190
+ let plan = ReconciliationPlan::new(left.id.clone(), right.id.clone(), base.id.clone(), true)
191
+ .with_override(vec![], json!({"alice": 150, "bob": 150}));
192
+ assert!(plan.validate(), "the plan validates structurally");
193
+
194
+ // The override is now read, and the plan is now applied.
195
+ let result =
196
+ reconcile(&base, &left, &right, &plan, "reconciler".into()).expect("the plan applies");
197
+ assert_eq!(
198
+ result.materialized_state.content,
199
+ r#"{"alice":150,"bob":150}"#
200
+ );
201
+ assert_eq!(
202
+ result.materialized_state.parent_id.as_ref(),
203
+ Some(&left.id),
204
+ "the chosen parent is the revision's parent"
205
+ );
206
+
207
+ // And nothing the plan started from changed. There is no branch head to
208
+ // advance — the store keeps none — and both source revisions are exactly
209
+ // as retrievable as before.
210
+ assert_eq!(store.get(&left.id).unwrap().content, left.content);
211
+ assert_eq!(store.get(&right.id).unwrap().content, right.content);
212
+
213
+ // And nothing chose this plan. Classification reports the conflict; the
214
+ // decision of what to do about it came from the caller.
215
+ let classification = ConflictClassification::classify(&base, &left, &right);
216
+ assert_eq!(classification.overall, ConflictClass::Independent);
217
+ assert!(
218
+ plan.unresolved_conflicts(&classification).is_empty(),
219
+ "the plan covers what the classifier reported, because the caller made it so"
220
+ );
221
+ }
222
+
223
+ /// Q9 — **the defect was removed rather than fixed.**
224
+ ///
225
+ /// The original finding: `create_branch` recorded one `StateId` and never
226
+ /// moved, while *any* commit moved the store's single global `current` pointer,
227
+ /// so "which revision is this branch on now" was a question the store could not
228
+ /// answer after the first commit. Both halves of that — the frozen branch
229
+ /// pointer and the promiscuous global `current` — lived in one record.
230
+ ///
231
+ /// Neither exists now. The store keeps no pointer, so there is none to be
232
+ /// stale, and no commit can displace anything. Two siblings committed onto the
233
+ /// same parent are simply two revisions, each addressed by its own identity,
234
+ /// and the parent is untouched by either.
235
+ ///
236
+ /// What this deliberately does *not* claim is that "which revision is current"
237
+ /// has been answered. It has been **removed from this layer**, and no owner for
238
+ /// it has been introduced.
239
+ #[test]
240
+ fn no_commit_can_displace_another_revision_because_there_is_no_pointer() {
241
+ let store = StateStore::new_volatile();
242
+ let base = store
243
+ .create("docs:1", json!({"v": 1}).to_string(), "a".into())
244
+ .expect("create");
245
+
246
+ let one = store
247
+ .commit(
248
+ json!({"v": 2, "side": "one"}).to_string(),
249
+ &base,
250
+ "a".into(),
251
+ )
252
+ .expect("commit one");
253
+ let two = store
254
+ .commit(
255
+ json!({"v": 2, "side": "two"}).to_string(),
256
+ &base,
257
+ "a".into(),
258
+ )
259
+ .expect("commit two");
260
+
261
+ // All three coexist, and neither sibling is privileged over the other.
262
+ for revision in [&base, &one, &two] {
263
+ assert_eq!(
264
+ store.get(&revision.id).expect("still present").content,
265
+ revision.content
266
+ );
267
+ }
268
+ assert_eq!(store.parent(&one.id).unwrap().id, base.id);
269
+ assert_eq!(store.parent(&two.id).unwrap().id, base.id);
270
+ assert_ne!(one.id, two.id, "the siblings are distinct revisions");
271
+ }
272
+
273
+ /// Q12 — nothing collects an abandoned revision, and nothing can.
274
+ ///
275
+ /// Unchanged by the removal of branches, and worth re-recording because it is
276
+ /// now the *only* remaining retention finding. Every revision ever committed
277
+ /// stays in the store and on disk. There is no refcount, no unreachability
278
+ /// sweep, and no delete.
279
+ #[test]
280
+ fn abandoned_revisions_are_retained_with_no_way_to_drop_them() {
281
+ let directory = TempDir::new().unwrap();
282
+ let abandoned = {
283
+ let store = durable_store(&directory, "state.log");
284
+ let base = store
285
+ .create("docs:1", json!({"v": 1}).to_string(), "a".into())
286
+ .expect("create");
287
+ // Nobody will ever ask for this revision again, and there is no way to
288
+ // tell the store that.
289
+ store
290
+ .commit(json!({"v": 2}).to_string(), &base, "a".into())
291
+ .expect("commit")
292
+ };
293
+
294
+ let reopened = durable_store(&directory, "state.log");
295
+ assert!(
296
+ reopened.exists(&abandoned.id),
297
+ "an abandoned revision is still durable after restart"
298
+ );
299
+ }
@@ -0,0 +1,288 @@
1
+ //! Evidence for the current-revision authority audit
2
+ //! (`docs/audits/CURRENT-REVISION-AUTHORITY-AUDIT.md`).
3
+ //!
4
+ //! Adds **no capability and no product surface**. It answers one question with
5
+ //! runs rather than readings:
6
+ //!
7
+ //! > Can the existing resource/application authority legitimately own
8
+ //! > `currentRevisionId`, advanced atomically with the resource's durable
9
+ //! > mutation?
10
+ //!
11
+ //! The short answer is that the *mechanism* already exists and does exactly
12
+ //! this for `__version`, but the *place* it keeps it — a field inside the
13
+ //! document — is erasable by the unconditional write path. That is the finding
14
+ //! the audit turns on, and `an_unconditional_write_erases_a_document_maintained_field`
15
+ //! is what pins it.
16
+
17
+ use feltdb::{FeltDb, JsonCasResult};
18
+ use serde_json::{json, Value};
19
+ use std::collections::HashMap;
20
+ use tempfile::TempDir;
21
+
22
+ // ---------------------------------------------------------------------------
23
+ // The resource already owns durable currentness
24
+ // ---------------------------------------------------------------------------
25
+
26
+ /// A resource's current state is already durable and already survives restart.
27
+ ///
28
+ /// This is the load-bearing precondition for the audit's hypothesis. Nothing
29
+ /// needs to be invented for a resource to *have* a current state that outlives
30
+ /// the process: the row at `capability:key` is that state, the version fence
31
+ /// advances it atomically, and reopening the log returns exactly what the last
32
+ /// successful mutation left.
33
+ #[test]
34
+ fn a_resource_already_owns_its_current_state_across_restart() {
35
+ let directory = TempDir::new().unwrap();
36
+ let path = directory.path().join("resource.log");
37
+
38
+ {
39
+ let db = FeltDb::open(&path).unwrap();
40
+ db.insert("accounts:1", json!({"balance": 100, "__version": 1}))
41
+ .unwrap();
42
+
43
+ let result = db
44
+ .compare_and_set_json("accounts:1", 1, None, None, false, json!({"balance": 150}))
45
+ .unwrap();
46
+ assert!(
47
+ matches!(
48
+ result,
49
+ JsonCasResult::Updated {
50
+ current_version: 2,
51
+ ..
52
+ }
53
+ ),
54
+ "the fence advanced the resource's version inside the mutation"
55
+ );
56
+ }
57
+
58
+ let db = FeltDb::open(&path).unwrap();
59
+ let recovered: Option<Value> = db.get("accounts:1").unwrap();
60
+ assert_eq!(
61
+ recovered,
62
+ Some(json!({"balance": 150, "__version": 2})),
63
+ "the resource's current state survives restart without any recovery step"
64
+ );
65
+ }
66
+
67
+ /// Advancement is already gated, by three independent fences.
68
+ ///
69
+ /// `compare_and_set_json` checks the record version, the authority epoch at
70
+ /// `/authority/epoch`, and an optional lease id — all under the store lock,
71
+ /// before any durable write. "Who is entitled to advance this resource" is a
72
+ /// question the existing authority already answers.
73
+ #[test]
74
+ fn the_existing_authority_already_gates_who_may_advance_a_resource() {
75
+ let directory = TempDir::new().unwrap();
76
+ let db = FeltDb::open(directory.path().join("epoch.log")).unwrap();
77
+ db.insert(
78
+ "accounts:4",
79
+ json!({"balance": 10, "__version": 1, "authority": {"epoch": 5}}),
80
+ )
81
+ .unwrap();
82
+
83
+ // A writer carrying a stale epoch is refused, even with the right version.
84
+ let stale = db
85
+ .compare_and_set_json(
86
+ "accounts:4",
87
+ 1,
88
+ Some(4),
89
+ None,
90
+ false,
91
+ json!({"balance": 20}),
92
+ )
93
+ .unwrap();
94
+ assert!(
95
+ matches!(
96
+ stale,
97
+ JsonCasResult::AuthorityConflict {
98
+ current_epoch: 5,
99
+ ..
100
+ }
101
+ ),
102
+ "a stale authority epoch is refused: {stale:?}"
103
+ );
104
+
105
+ // The holder of the current epoch may advance it.
106
+ let allowed = db
107
+ .compare_and_set_json(
108
+ "accounts:4",
109
+ 1,
110
+ Some(5),
111
+ None,
112
+ false,
113
+ json!({"balance": 30}),
114
+ )
115
+ .unwrap();
116
+ assert!(
117
+ matches!(
118
+ allowed,
119
+ JsonCasResult::Updated {
120
+ current_version: 2,
121
+ current_epoch: 5,
122
+ ..
123
+ }
124
+ ),
125
+ "the current epoch holder advances the resource: {allowed:?}"
126
+ );
127
+ }
128
+
129
+ // ---------------------------------------------------------------------------
130
+ // What a race actually produces
131
+ // ---------------------------------------------------------------------------
132
+
133
+ /// The version fence **annihilates** the losing writer rather than recording it.
134
+ ///
135
+ /// This is the audit's central negative result. Two writers start from the same
136
+ /// parent version. One wins. The loser is told a number — `current_version` —
137
+ /// and its proposed state is never stored, never logged, and never given an
138
+ /// identity. Nothing anywhere records that two states were proposed from one
139
+ /// parent.
140
+ ///
141
+ /// So on a single node the existing mechanism does not *detect* divergence, it
142
+ /// *prevents* it. There is no `left` and `right` to reconcile, because `right`
143
+ /// never came into existence. Reconciliation's three inputs cannot be sourced
144
+ /// from this path no matter how it is wired.
145
+ #[test]
146
+ fn the_version_fence_annihilates_the_losing_writer() {
147
+ let directory = TempDir::new().unwrap();
148
+ let db = FeltDb::open(directory.path().join("race.log")).unwrap();
149
+ db.insert("accounts:2", json!({"balance": 100, "__version": 1}))
150
+ .unwrap();
151
+
152
+ // Both writers fence on the same parent version.
153
+ let winner = db
154
+ .compare_and_set_json("accounts:2", 1, None, None, false, json!({"balance": 200}))
155
+ .unwrap();
156
+ let loser = db
157
+ .compare_and_set_json("accounts:2", 1, None, None, false, json!({"balance": 300}))
158
+ .unwrap();
159
+
160
+ assert!(matches!(winner, JsonCasResult::Updated { .. }));
161
+ assert!(
162
+ matches!(
163
+ loser,
164
+ JsonCasResult::VersionConflict {
165
+ current_version: 2,
166
+ ..
167
+ }
168
+ ),
169
+ "the loser learns only a version number: {loser:?}"
170
+ );
171
+
172
+ // The stored state is the winner's, and the loser's is nowhere.
173
+ let stored: Option<Value> = db.get("accounts:2").unwrap();
174
+ assert_eq!(stored, Some(json!({"balance": 200, "__version": 2})));
175
+
176
+ let operations = db.operations_since(&HashMap::new()).unwrap();
177
+ let for_resource: Vec<_> = operations
178
+ .iter()
179
+ .filter(|op| op.key == "accounts:2")
180
+ .collect();
181
+ assert_eq!(
182
+ for_resource.len(),
183
+ 2,
184
+ "only the insert and the winning update are durable"
185
+ );
186
+ for operation in &for_resource {
187
+ let rendered = operation.value.as_ref().unwrap().to_string();
188
+ assert!(
189
+ !rendered.contains("300"),
190
+ "the losing writer's proposed state appears nowhere: {rendered}"
191
+ );
192
+ }
193
+ }
194
+
195
+ /// The operation log **does** retain an ordered per-resource history.
196
+ ///
197
+ /// Worth recording as a positive: the ancestry a revision chain would need is
198
+ /// not absent from the substrate. Every mutation is appended in order, per key,
199
+ /// with a per-origin vector clock. What is missing is not the history — it is
200
+ /// an identity for each resulting *state* and an explicit parent link between
201
+ /// them. The log records operations, not revisions.
202
+ #[test]
203
+ fn the_operation_log_retains_an_ordered_per_resource_history() {
204
+ let directory = TempDir::new().unwrap();
205
+ let db = FeltDb::open(directory.path().join("history.log")).unwrap();
206
+
207
+ db.insert("accounts:3", json!({"balance": 100, "__version": 1}))
208
+ .unwrap();
209
+ db.update("accounts:3", json!({"balance": 200})).unwrap();
210
+ db.update("accounts:3", json!({"balance": 300})).unwrap();
211
+
212
+ let operations = db.operations_since(&HashMap::new()).unwrap();
213
+ let history: Vec<_> = operations
214
+ .iter()
215
+ .filter(|op| op.key == "accounts:3")
216
+ .collect();
217
+ assert_eq!(history.len(), 3, "every mutation is retained, in order");
218
+
219
+ // Each carries a per-origin causal position, which is a sequence rather
220
+ // than a statement about state.
221
+ for operation in &history {
222
+ assert!(
223
+ operation
224
+ .vector_clock
225
+ .as_ref()
226
+ .is_some_and(|c| !c.is_empty()),
227
+ "each operation carries causal evidence"
228
+ );
229
+ }
230
+
231
+ // But no operation names the state it descended from.
232
+ let rendered = format!("{:?}", history);
233
+ assert!(
234
+ !rendered.contains("parent"),
235
+ "an operation records what was done, not which state it descended from"
236
+ );
237
+ }
238
+
239
+ // ---------------------------------------------------------------------------
240
+ // Why a document field cannot hold the reference
241
+ // ---------------------------------------------------------------------------
242
+
243
+ /// An unconditional write erases a field the authority maintains by convention.
244
+ ///
245
+ /// This is the finding that decides the shape of any future `currentRevisionId`.
246
+ ///
247
+ /// `__version` is, by the repository's own transaction-version contract, *"a
248
+ /// document field maintained by convention, not a storage-level attribute"*.
249
+ /// Unconditional writes are documented to remain verbatim — so writing a value
250
+ /// that omits the field stores it omitted. That is correct behaviour for
251
+ /// `update`, and it is exactly why it is not a safe home for a reference the
252
+ /// state model would depend on: one ordinary unconditional write silently
253
+ /// removes the resource's fence.
254
+ ///
255
+ /// A `currentRevisionId` kept the same way would inherit the same weakness, and
256
+ /// losing it would sever a resource from its history rather than merely dropping
257
+ /// an optimistic-concurrency check.
258
+ #[test]
259
+ fn an_unconditional_write_erases_a_document_maintained_field() {
260
+ let directory = TempDir::new().unwrap();
261
+ let db = FeltDb::open(directory.path().join("erase.log")).unwrap();
262
+
263
+ db.insert("accounts:5", json!({"balance": 100, "__version": 1}))
264
+ .unwrap();
265
+ // Stand in for a revision pointer kept the same way the version is.
266
+ db.insert(
267
+ "accounts:6",
268
+ json!({"balance": 100, "__version": 1, "__currentRevision": "abc123"}),
269
+ )
270
+ .unwrap();
271
+
272
+ db.update("accounts:5", json!({"balance": 200})).unwrap();
273
+ db.update("accounts:6", json!({"balance": 200})).unwrap();
274
+
275
+ let versioned: Option<Value> = db.get("accounts:5").unwrap();
276
+ assert_eq!(
277
+ versioned,
278
+ Some(json!({"balance": 200})),
279
+ "the version field is gone after one unconditional write"
280
+ );
281
+
282
+ let referenced: Option<Value> = db.get("accounts:6").unwrap();
283
+ assert_eq!(
284
+ referenced,
285
+ Some(json!({"balance": 200})),
286
+ "a revision reference kept the same way is erased identically"
287
+ );
288
+ }