@feltdb/core 0.4.12 → 0.4.14
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.
- package/dist/cli/commands.js +42 -25
- package/dist/cli/index.js +1 -1
- package/dist/create/cli.js +39 -4
- package/dist/create/create.js +30 -18
- package/dist/create/docker-compose-generator.js +68 -11
- package/dist/create/package-versions.js +1 -1
- package/dist/create/server-source/Cargo.lock +2238 -0
- package/dist/create/server-source/Cargo.toml +7 -0
- package/dist/create/server-source/crates/feltdb/Cargo.lock +175 -0
- package/dist/create/server-source/crates/feltdb/Cargo.toml +79 -0
- package/dist/create/server-source/crates/feltdb/benches/baselines/gate-13-redux.json +370 -0
- package/dist/create/server-source/crates/feltdb/benches/gate13_baseline.rs +589 -0
- package/dist/create/server-source/crates/feltdb/benches/gate13_phase_7_1_release_economics.rs +259 -0
- package/dist/create/server-source/crates/feltdb/benches/gate_13_redux.rs +446 -0
- package/dist/create/server-source/crates/feltdb/benches/gate_13_regression_runner.rs +272 -0
- package/dist/create/server-source/crates/feltdb/benches/gate_14a_concurrent_writer_scaling.rs +378 -0
- package/dist/create/server-source/crates/feltdb/benches/gate_14a_production_admission_revalidation.rs +414 -0
- package/dist/create/server-source/crates/feltdb/benches/gate_14a_rc2_admission_contract.rs +486 -0
- package/dist/create/server-source/crates/feltdb/benches/gate_14a_rc_root_cause.rs +273 -0
- package/dist/create/server-source/crates/feltdb/benches/gate_14a_sync1_queued_prototype.rs +587 -0
- package/dist/create/server-source/crates/feltdb/benches/gate_14a_sync_economics.rs +513 -0
- package/dist/create/server-source/crates/feltdb/benches/gate_14b_causal_backlog_scaling.rs +395 -0
- package/dist/create/server-source/crates/feltdb/benches/gate_14c_replication_contract_test.rs +469 -0
- package/dist/create/server-source/crates/feltdb/benches/gate_14c_replication_scaling.rs +409 -0
- package/dist/create/server-source/crates/feltdb/benches/gate_14d_combined_dimension_scaling.rs +627 -0
- package/dist/create/server-source/crates/feltdb/benches/phase_7_1_2_optimization_benchmark.rs +383 -0
- package/dist/create/server-source/crates/feltdb/benches/phase_7_1_3_crossover_analysis.rs +298 -0
- package/dist/create/server-source/crates/feltdb/src/acceptance_tests.rs +1698 -0
- package/dist/create/server-source/crates/feltdb/src/acquisition.rs +286 -0
- package/dist/create/server-source/crates/feltdb/src/admission.rs +192 -0
- package/dist/create/server-source/crates/feltdb/src/admission_contract_tests.rs +477 -0
- package/dist/create/server-source/crates/feltdb/src/adversarial_transport.rs +566 -0
- package/dist/create/server-source/crates/feltdb/src/analytics.rs +475 -0
- package/dist/create/server-source/crates/feltdb/src/application.rs +2244 -0
- package/dist/create/server-source/crates/feltdb/src/application_runtime.rs +1070 -0
- package/dist/create/server-source/crates/feltdb/src/authorization.rs +1030 -0
- package/dist/create/server-source/crates/feltdb/src/bin/feltdb_node.rs +266 -0
- package/dist/create/server-source/crates/feltdb/src/capabilities/mod.rs +8 -0
- package/dist/create/server-source/crates/feltdb/src/capabilities/search.rs +418 -0
- package/dist/create/server-source/crates/feltdb/src/capabilities/vector.rs +482 -0
- package/dist/create/server-source/crates/feltdb/src/capability.rs +903 -0
- package/dist/create/server-source/crates/feltdb/src/cardinality_diagnostics.rs +261 -0
- package/dist/create/server-source/crates/feltdb/src/cardinality_endpoint.rs +78 -0
- package/dist/create/server-source/crates/feltdb/src/causal_dependency_barrier.rs +2214 -0
- package/dist/create/server-source/crates/feltdb/src/causal_dependency_barrier_phase_7_1.rs +194 -0
- package/dist/create/server-source/crates/feltdb/src/concurrency_fuzzing.rs +427 -0
- package/dist/create/server-source/crates/feltdb/src/consistency_contract.rs +453 -0
- package/dist/create/server-source/crates/feltdb/src/content_distribution.rs +465 -0
- package/dist/create/server-source/crates/feltdb/src/convergence.rs +618 -0
- package/dist/create/server-source/crates/feltdb/src/crash_atomic_boundary.rs +418 -0
- package/dist/create/server-source/crates/feltdb/src/crash_injection.rs +380 -0
- package/dist/create/server-source/crates/feltdb/src/crash_recovery_tests.rs +362 -0
- package/dist/create/server-source/crates/feltdb/src/cron.rs +294 -0
- package/dist/create/server-source/crates/feltdb/src/distributed_indexing.rs +474 -0
- package/dist/create/server-source/crates/feltdb/src/distributed_tests.rs +1003 -0
- package/dist/create/server-source/crates/feltdb/src/distributed_transactions.rs +536 -0
- package/dist/create/server-source/crates/feltdb/src/durability_guarantees.rs +364 -0
- package/dist/create/server-source/crates/feltdb/src/durable_dedup_set.rs +235 -0
- package/dist/create/server-source/crates/feltdb/src/durable_operation_log.rs +207 -0
- package/dist/create/server-source/crates/feltdb/src/durable_sync.rs +316 -0
- package/dist/create/server-source/crates/feltdb/src/execution.rs +426 -0
- package/dist/create/server-source/crates/feltdb/src/in_process_transport.rs +219 -0
- package/dist/create/server-source/crates/feltdb/src/indexing.rs +779 -0
- package/dist/create/server-source/crates/feltdb/src/lib.rs +2838 -0
- package/dist/create/server-source/crates/feltdb/src/materialization.rs +184 -0
- package/dist/create/server-source/crates/feltdb/src/metrics.rs +267 -0
- package/dist/create/server-source/crates/feltdb/src/multi_node_convergence.rs +311 -0
- package/dist/create/server-source/crates/feltdb/src/observability.rs +366 -0
- package/dist/create/server-source/crates/feltdb/src/operation.rs +251 -0
- package/dist/create/server-source/crates/feltdb/src/operation_algebra.rs +438 -0
- package/dist/create/server-source/crates/feltdb/src/operation_log.rs +344 -0
- package/dist/create/server-source/crates/feltdb/src/partition_reconciliation.rs +477 -0
- package/dist/create/server-source/crates/feltdb/src/peer_registry.rs +166 -0
- package/dist/create/server-source/crates/feltdb/src/permutation_scheduler.rs +261 -0
- package/dist/create/server-source/crates/feltdb/src/persistence_reality.rs +560 -0
- package/dist/create/server-source/crates/feltdb/src/phase1b_acceptance.rs +3226 -0
- package/dist/create/server-source/crates/feltdb/src/phase1c1_acceptance.rs +201 -0
- package/dist/create/server-source/crates/feltdb/src/phase1c2_acceptance.rs +263 -0
- package/dist/create/server-source/crates/feltdb/src/phase1c3_acceptance.rs +484 -0
- package/dist/create/server-source/crates/feltdb/src/phase1c_atomicity_proof.rs +216 -0
- package/dist/create/server-source/crates/feltdb/src/phase5_integration.rs +281 -0
- package/dist/create/server-source/crates/feltdb/src/phase5_scenarios.rs +323 -0
- package/dist/create/server-source/crates/feltdb/src/phase6_adversarial_scenarios.rs +573 -0
- package/dist/create/server-source/crates/feltdb/src/phase6_convergence_validator.rs +404 -0
- package/dist/create/server-source/crates/feltdb/src/phase6_persistence.rs +418 -0
- package/dist/create/server-source/crates/feltdb/src/phase_1c_real_tcp.rs +381 -0
- package/dist/create/server-source/crates/feltdb/src/phase_1c_three_node.rs +523 -0
- package/dist/create/server-source/crates/feltdb/src/phase_2a_failures.rs +334 -0
- package/dist/create/server-source/crates/feltdb/src/phase_2b_network.rs +306 -0
- package/dist/create/server-source/crates/feltdb/src/phase_2c_cascading.rs +355 -0
- package/dist/create/server-source/crates/feltdb/src/phase_3_durability.rs +395 -0
- package/dist/create/server-source/crates/feltdb/src/phase_4_baseline.rs +346 -0
- package/dist/create/server-source/crates/feltdb/src/phase_5_soak.rs +430 -0
- package/dist/create/server-source/crates/feltdb/src/production_api.rs +400 -0
- package/dist/create/server-source/crates/feltdb/src/provenance.rs +207 -0
- package/dist/create/server-source/crates/feltdb/src/query_performance.rs +217 -0
- package/dist/create/server-source/crates/feltdb/src/references.rs +404 -0
- package/dist/create/server-source/crates/feltdb/src/replay_fuzzing.rs +401 -0
- package/dist/create/server-source/crates/feltdb/src/replication_manager.rs +160 -0
- package/dist/create/server-source/crates/feltdb/src/replication_protocol.rs +132 -0
- package/dist/create/server-source/crates/feltdb/src/routing.rs +409 -0
- package/dist/create/server-source/crates/feltdb/src/sharding.rs +523 -0
- package/dist/create/server-source/crates/feltdb/src/state_contract.rs +3118 -0
- package/dist/create/server-source/crates/feltdb/src/state_hash.rs +291 -0
- package/dist/create/server-source/crates/feltdb/src/state_transition_store.rs +376 -0
- package/dist/create/server-source/crates/feltdb/src/storage.rs +267 -0
- package/dist/create/server-source/crates/feltdb/src/submission.rs +477 -0
- package/dist/create/server-source/crates/feltdb/src/sync.rs +483 -0
- package/dist/create/server-source/crates/feltdb/src/sync_contract.rs +822 -0
- package/dist/create/server-source/crates/feltdb/src/tcp_transport.rs +366 -0
- package/dist/create/server-source/crates/feltdb/src/transaction_api.rs +473 -0
- package/dist/create/server-source/crates/feltdb/src/transaction_invariants.rs +949 -0
- package/dist/create/server-source/crates/feltdb/src/transactions.rs +646 -0
- package/dist/create/server-source/crates/feltdb/src/trigger.rs +390 -0
- package/dist/create/server-source/crates/feltdb/src/worker_mesh.rs +928 -0
- package/dist/create/server-source/crates/feltdb/src/workflow.rs +713 -0
- package/dist/create/server-source/crates/feltdb/src/workflow_acceptance_tests.rs +510 -0
- package/dist/create/server-source/crates/feltdb/src/workflow_integration.rs +354 -0
- package/dist/create/server-source/crates/feltdb/src/workflow_runtime.rs +594 -0
- package/dist/create/server-source/crates/feltdb/src/workload.rs +1310 -0
- package/dist/create/server-source/crates/feltdb-server/Cargo.toml +23 -0
- package/dist/create/server-source/crates/feltdb-server/README.md +79 -0
- package/dist/create/server-source/crates/feltdb-server/src/app_state.rs +73 -0
- package/dist/create/server-source/crates/feltdb-server/src/application_contract.rs +425 -0
- package/dist/create/server-source/crates/feltdb-server/src/artifacts.rs +449 -0
- package/dist/create/server-source/crates/feltdb-server/src/audit.rs +59 -0
- package/dist/create/server-source/crates/feltdb-server/src/auth.rs +308 -0
- package/dist/create/server-source/crates/feltdb-server/src/authorization.rs +228 -0
- package/dist/create/server-source/crates/feltdb-server/src/backup.rs +416 -0
- package/dist/create/server-source/crates/feltdb-server/src/causal.rs +218 -0
- package/dist/create/server-source/crates/feltdb-server/src/certification.rs +223 -0
- package/dist/create/server-source/crates/feltdb-server/src/clock.rs +132 -0
- package/dist/create/server-source/crates/feltdb-server/src/cluster.rs +165 -0
- package/dist/create/server-source/crates/feltdb-server/src/connections.rs +1044 -0
- package/dist/create/server-source/crates/feltdb-server/src/content.rs +111 -0
- package/dist/create/server-source/crates/feltdb-server/src/identity.rs +250 -0
- package/dist/create/server-source/crates/feltdb-server/src/key_management.rs +78 -0
- package/dist/create/server-source/crates/feltdb-server/src/key_provider.rs +247 -0
- package/dist/create/server-source/crates/feltdb-server/src/leases.rs +123 -0
- package/dist/create/server-source/crates/feltdb-server/src/lib.rs +25 -0
- package/dist/create/server-source/crates/feltdb-server/src/main.rs +8715 -0
- package/dist/create/server-source/crates/feltdb-server/src/metrics.rs +97 -0
- package/dist/create/server-source/crates/feltdb-server/src/portable_bundle.rs +350 -0
- package/dist/create/server-source/crates/feltdb-server/src/principals.rs +510 -0
- package/dist/create/server-source/crates/feltdb-server/src/providers.rs +269 -0
- package/dist/create/server-source/crates/feltdb-server/src/releases.rs +2563 -0
- package/dist/create/server-source/crates/feltdb-server/src/sessions.rs +156 -0
- package/dist/create/server-source/crates/feltdb-server/src/tenancy.rs +1097 -0
- package/dist/create/server-source/crates/feltdb-server/src/versions.rs +264 -0
- package/dist/create/server-source/crates/feltdb-wasm/Cargo.toml +31 -0
- package/dist/create/server-source/crates/feltdb-wasm/src/lib.rs +1086 -0
- package/dist/create/server-source/crates/feltdb-wasm/test.db +0 -0
- package/dist/flowspec.d.ts +2 -0
- package/dist/flowspec.d.ts.map +1 -1
- package/dist/flowspec.js +28 -4
- package/dist/state-contract.d.ts +7 -1
- package/dist/state-contract.d.ts.map +1 -1
- package/dist/studio/components/ApplicationDesigner.d.ts.map +1 -1
- package/dist/studio/components/ConflictExplorer.d.ts.map +1 -1
- package/dist/studio/components/index.js +1 -1
- package/dist/studio/{components-Q0Nx6gLE.js → components-9kDSWiGL.js} +163 -47
- package/dist/studio/index.js +31 -28
- package/dist/studio-app/assets/{index-BHOmLZF-.js → index-DVdvmf69.js} +9 -9
- package/dist/studio-app/index.html +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,822 @@
|
|
|
1
|
+
//! FeltDB Sync v1: transport-neutral operation synchronization semantics.
|
|
2
|
+
use crate::authorization::{AuthorizationContext, Resource, SignedGrant, Subject};
|
|
3
|
+
use crate::state_contract::{SchemaCompatibility, TransactionOperation};
|
|
4
|
+
use serde::{Deserialize, Serialize};
|
|
5
|
+
use serde_json::Value;
|
|
6
|
+
use sha2::{Digest, Sha256};
|
|
7
|
+
use std::collections::{BTreeMap, BTreeSet};
|
|
8
|
+
use std::fs;
|
|
9
|
+
use std::path::{Path, PathBuf};
|
|
10
|
+
|
|
11
|
+
pub const SYNC_CONTRACT_VERSION: u32 = 1;
|
|
12
|
+
|
|
13
|
+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
|
14
|
+
pub struct DeviceIdentity {
|
|
15
|
+
pub device_id: String,
|
|
16
|
+
pub tenant_id: String,
|
|
17
|
+
pub application_id: String,
|
|
18
|
+
pub subject: Subject,
|
|
19
|
+
pub created_at: i64,
|
|
20
|
+
}
|
|
21
|
+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
|
22
|
+
pub struct SyncScope {
|
|
23
|
+
#[serde(default)]
|
|
24
|
+
pub resources: Vec<String>,
|
|
25
|
+
#[serde(default)]
|
|
26
|
+
pub fields: BTreeMap<String, BTreeSet<String>>,
|
|
27
|
+
}
|
|
28
|
+
impl SyncScope {
|
|
29
|
+
pub fn contains(&self, uri: &str) -> bool {
|
|
30
|
+
self.resources.iter().any(|scope| {
|
|
31
|
+
let prefix = scope.trim_end_matches('*').trim_end_matches('/');
|
|
32
|
+
uri == prefix || uri.starts_with(&(prefix.to_owned() + "/"))
|
|
33
|
+
})
|
|
34
|
+
}
|
|
35
|
+
pub fn hash(&self) -> String {
|
|
36
|
+
hash_json(self)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
|
40
|
+
pub struct SyncCursor {
|
|
41
|
+
pub contract_version: u32,
|
|
42
|
+
pub tenant_id: String,
|
|
43
|
+
pub application_id: String,
|
|
44
|
+
pub subject: Subject,
|
|
45
|
+
pub scope_hash: String,
|
|
46
|
+
pub position: u64,
|
|
47
|
+
}
|
|
48
|
+
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
49
|
+
pub struct SyncSession {
|
|
50
|
+
pub session_id: String,
|
|
51
|
+
pub device: DeviceIdentity,
|
|
52
|
+
pub scope: SyncScope,
|
|
53
|
+
pub cursor: SyncCursor,
|
|
54
|
+
pub grant_id: String,
|
|
55
|
+
pub created_at: i64,
|
|
56
|
+
pub expires_at: i64,
|
|
57
|
+
pub status: SyncSessionStatus,
|
|
58
|
+
}
|
|
59
|
+
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
|
|
60
|
+
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
|
61
|
+
pub enum SyncSessionStatus {
|
|
62
|
+
Active,
|
|
63
|
+
Closed,
|
|
64
|
+
Expired,
|
|
65
|
+
}
|
|
66
|
+
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
|
|
67
|
+
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
|
68
|
+
pub enum OutboxStatus {
|
|
69
|
+
Pending,
|
|
70
|
+
Sending,
|
|
71
|
+
Acknowledged,
|
|
72
|
+
Rejected,
|
|
73
|
+
Conflicted,
|
|
74
|
+
Retryable,
|
|
75
|
+
PermanentlyFailed,
|
|
76
|
+
}
|
|
77
|
+
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
|
|
78
|
+
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
|
79
|
+
pub enum ReconciliationOutcome {
|
|
80
|
+
Accept,
|
|
81
|
+
Merge,
|
|
82
|
+
Conflict,
|
|
83
|
+
Reject,
|
|
84
|
+
Stale,
|
|
85
|
+
Unauthorized,
|
|
86
|
+
SchemaMismatch,
|
|
87
|
+
AlreadyCommitted,
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
91
|
+
pub struct SyncOperation {
|
|
92
|
+
pub contract_version: u32,
|
|
93
|
+
pub operation_id: String,
|
|
94
|
+
pub tenant_id: String,
|
|
95
|
+
pub application_id: String,
|
|
96
|
+
pub subject: Subject,
|
|
97
|
+
pub device_id: String,
|
|
98
|
+
pub resource: Resource,
|
|
99
|
+
pub operation: TransactionOperation,
|
|
100
|
+
#[serde(default)]
|
|
101
|
+
pub causal_context: BTreeMap<String, u64>,
|
|
102
|
+
pub schema_version: u64,
|
|
103
|
+
#[serde(default)]
|
|
104
|
+
pub authorization_context: AuthorizationContext,
|
|
105
|
+
pub offline_grant: SignedGrant,
|
|
106
|
+
pub application_revision: String,
|
|
107
|
+
pub created_at: i64,
|
|
108
|
+
}
|
|
109
|
+
impl SyncOperation {
|
|
110
|
+
pub fn validate_identity(&self) -> Result<(), SyncError> {
|
|
111
|
+
if self.contract_version != SYNC_CONTRACT_VERSION
|
|
112
|
+
|| self.operation_id.trim().is_empty()
|
|
113
|
+
|| self.device_id.trim().is_empty()
|
|
114
|
+
{
|
|
115
|
+
return Err(SyncError::new(
|
|
116
|
+
"SYNC_INVALID",
|
|
117
|
+
"operation identity is incomplete",
|
|
118
|
+
));
|
|
119
|
+
}
|
|
120
|
+
if self.resource.uri.contains("..")
|
|
121
|
+
|| !self
|
|
122
|
+
.resource
|
|
123
|
+
.uri
|
|
124
|
+
.starts_with(&format!("flow://{}/", self.application_id))
|
|
125
|
+
{
|
|
126
|
+
return Err(SyncError::new(
|
|
127
|
+
"SYNC_SCOPE_DENIED",
|
|
128
|
+
"operation resource is outside its application",
|
|
129
|
+
));
|
|
130
|
+
}
|
|
131
|
+
Ok(())
|
|
132
|
+
}
|
|
133
|
+
pub fn hash(&self) -> String {
|
|
134
|
+
hash_json(self)
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
138
|
+
pub struct SyncBatch {
|
|
139
|
+
pub session_id: String,
|
|
140
|
+
pub operations: Vec<SyncOperation>,
|
|
141
|
+
}
|
|
142
|
+
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
143
|
+
pub struct SyncAck {
|
|
144
|
+
pub operation_id: String,
|
|
145
|
+
pub outcome: ReconciliationOutcome,
|
|
146
|
+
pub server_position: Option<u64>,
|
|
147
|
+
pub transaction_id: Option<String>,
|
|
148
|
+
pub decision_id: Option<String>,
|
|
149
|
+
}
|
|
150
|
+
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
151
|
+
pub struct SyncRejection {
|
|
152
|
+
pub operation_id: String,
|
|
153
|
+
pub code: String,
|
|
154
|
+
pub message: String,
|
|
155
|
+
pub retryable: bool,
|
|
156
|
+
}
|
|
157
|
+
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
158
|
+
pub struct SyncConflict {
|
|
159
|
+
pub operation_id: String,
|
|
160
|
+
pub resource: String,
|
|
161
|
+
pub local_causal_context: BTreeMap<String, u64>,
|
|
162
|
+
pub server_causal_context: BTreeMap<String, u64>,
|
|
163
|
+
pub reason: String,
|
|
164
|
+
}
|
|
165
|
+
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
166
|
+
pub struct SyncResult {
|
|
167
|
+
pub acknowledgements: Vec<SyncAck>,
|
|
168
|
+
pub rejections: Vec<SyncRejection>,
|
|
169
|
+
pub conflicts: Vec<SyncConflict>,
|
|
170
|
+
pub cursor: SyncCursor,
|
|
171
|
+
}
|
|
172
|
+
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
173
|
+
pub struct SyncCheckpoint {
|
|
174
|
+
pub device_id: String,
|
|
175
|
+
pub cursor: SyncCursor,
|
|
176
|
+
pub causal_frontier: BTreeMap<String, u64>,
|
|
177
|
+
pub updated_at: i64,
|
|
178
|
+
}
|
|
179
|
+
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
180
|
+
pub struct Tombstone {
|
|
181
|
+
pub resource: String,
|
|
182
|
+
pub deletion_operation: String,
|
|
183
|
+
pub causal_context: BTreeMap<String, u64>,
|
|
184
|
+
pub schema_version: u64,
|
|
185
|
+
pub server_position: u64,
|
|
186
|
+
pub retain_until: i64,
|
|
187
|
+
}
|
|
188
|
+
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
189
|
+
pub struct StoredSyncOperation {
|
|
190
|
+
pub position: u64,
|
|
191
|
+
pub operation: SyncOperation,
|
|
192
|
+
pub outcome: ReconciliationOutcome,
|
|
193
|
+
pub decision_id: Option<String>,
|
|
194
|
+
pub committed_at: i64,
|
|
195
|
+
}
|
|
196
|
+
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
197
|
+
pub struct SyncLease {
|
|
198
|
+
pub device_id: String,
|
|
199
|
+
pub owner_runtime: String,
|
|
200
|
+
pub expires_at: i64,
|
|
201
|
+
pub heartbeat_at: i64,
|
|
202
|
+
}
|
|
203
|
+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
|
204
|
+
pub struct SyncError {
|
|
205
|
+
pub code: String,
|
|
206
|
+
pub message: String,
|
|
207
|
+
}
|
|
208
|
+
impl SyncError {
|
|
209
|
+
pub fn new(code: &str, message: impl Into<String>) -> Self {
|
|
210
|
+
Self {
|
|
211
|
+
code: code.into(),
|
|
212
|
+
message: message.into(),
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
impl std::fmt::Display for SyncError {
|
|
217
|
+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
218
|
+
write!(f, "{}: {}", self.code, self.message)
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
impl std::error::Error for SyncError {}
|
|
222
|
+
|
|
223
|
+
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
224
|
+
pub struct SyncStore {
|
|
225
|
+
#[serde(skip)]
|
|
226
|
+
path: PathBuf,
|
|
227
|
+
#[serde(default)]
|
|
228
|
+
pub sessions: BTreeMap<String, SyncSession>,
|
|
229
|
+
#[serde(default)]
|
|
230
|
+
pub operations: Vec<StoredSyncOperation>,
|
|
231
|
+
#[serde(default)]
|
|
232
|
+
pub operation_positions: BTreeMap<String, u64>,
|
|
233
|
+
#[serde(default)]
|
|
234
|
+
pub checkpoints: BTreeMap<String, SyncCheckpoint>,
|
|
235
|
+
#[serde(default)]
|
|
236
|
+
pub tombstones: BTreeMap<String, Tombstone>,
|
|
237
|
+
#[serde(default)]
|
|
238
|
+
pub leases: BTreeMap<String, SyncLease>,
|
|
239
|
+
#[serde(default)]
|
|
240
|
+
next_position: u64,
|
|
241
|
+
}
|
|
242
|
+
impl SyncStore {
|
|
243
|
+
pub fn load(path: impl AsRef<Path>) -> Result<Self, SyncError> {
|
|
244
|
+
let path = path.as_ref().to_path_buf();
|
|
245
|
+
if !path.exists() {
|
|
246
|
+
return Ok(Self {
|
|
247
|
+
path,
|
|
248
|
+
sessions: BTreeMap::new(),
|
|
249
|
+
operations: vec![],
|
|
250
|
+
operation_positions: BTreeMap::new(),
|
|
251
|
+
checkpoints: BTreeMap::new(),
|
|
252
|
+
tombstones: BTreeMap::new(),
|
|
253
|
+
leases: BTreeMap::new(),
|
|
254
|
+
next_position: 0,
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
let mut value: Self = serde_json::from_slice(&fs::read(&path).map_err(io)?).map_err(io)?;
|
|
258
|
+
value.path = path;
|
|
259
|
+
Ok(value)
|
|
260
|
+
}
|
|
261
|
+
fn persist(&self) -> Result<(), SyncError> {
|
|
262
|
+
if let Some(parent) = self.path.parent() {
|
|
263
|
+
fs::create_dir_all(parent).map_err(io)?;
|
|
264
|
+
}
|
|
265
|
+
let temporary = self.path.with_extension("tmp");
|
|
266
|
+
fs::write(&temporary, serde_json::to_vec_pretty(self).map_err(io)?).map_err(io)?;
|
|
267
|
+
fs::rename(temporary, &self.path).map_err(io)
|
|
268
|
+
}
|
|
269
|
+
pub fn create_session(
|
|
270
|
+
&mut self,
|
|
271
|
+
device: DeviceIdentity,
|
|
272
|
+
scope: SyncScope,
|
|
273
|
+
grant: &SignedGrant,
|
|
274
|
+
now: i64,
|
|
275
|
+
) -> Result<SyncSession, SyncError> {
|
|
276
|
+
if grant.grant.subject != device.subject
|
|
277
|
+
|| grant.grant.tenant_id != device.tenant_id
|
|
278
|
+
|| grant.grant.application_id != device.application_id
|
|
279
|
+
|| grant.grant.expires_at <= now
|
|
280
|
+
{
|
|
281
|
+
return Err(SyncError::new(
|
|
282
|
+
"SYNC_UNAUTHORIZED",
|
|
283
|
+
"offline grant does not bind this device subject and application",
|
|
284
|
+
));
|
|
285
|
+
}
|
|
286
|
+
if !scope.resources.iter().all(|resource| {
|
|
287
|
+
grant.grant.resources.iter().any(|allowed| {
|
|
288
|
+
allowed.matches(&Resource {
|
|
289
|
+
uri: resource.trim_end_matches('*').trim_end_matches('/').into(),
|
|
290
|
+
attributes: BTreeMap::new(),
|
|
291
|
+
})
|
|
292
|
+
})
|
|
293
|
+
}) {
|
|
294
|
+
return Err(SyncError::new(
|
|
295
|
+
"SYNC_SCOPE_DENIED",
|
|
296
|
+
"requested projection exceeds grant resources",
|
|
297
|
+
));
|
|
298
|
+
}
|
|
299
|
+
let seed = format!(
|
|
300
|
+
"{}:{}:{}:{}",
|
|
301
|
+
device.device_id,
|
|
302
|
+
grant.grant.id,
|
|
303
|
+
scope.hash(),
|
|
304
|
+
now
|
|
305
|
+
);
|
|
306
|
+
let id = format!("sync_{}", &hex(&Sha256::digest(seed))[..24]);
|
|
307
|
+
let cursor = SyncCursor {
|
|
308
|
+
contract_version: 1,
|
|
309
|
+
tenant_id: device.tenant_id.clone(),
|
|
310
|
+
application_id: device.application_id.clone(),
|
|
311
|
+
subject: device.subject.clone(),
|
|
312
|
+
scope_hash: scope.hash(),
|
|
313
|
+
position: 0,
|
|
314
|
+
};
|
|
315
|
+
let session = SyncSession {
|
|
316
|
+
session_id: id.clone(),
|
|
317
|
+
device,
|
|
318
|
+
scope,
|
|
319
|
+
cursor,
|
|
320
|
+
grant_id: grant.grant.id.clone(),
|
|
321
|
+
created_at: now,
|
|
322
|
+
expires_at: grant.grant.expires_at,
|
|
323
|
+
status: SyncSessionStatus::Active,
|
|
324
|
+
};
|
|
325
|
+
self.sessions.insert(id, session.clone());
|
|
326
|
+
self.persist()?;
|
|
327
|
+
Ok(session)
|
|
328
|
+
}
|
|
329
|
+
pub fn session(&self, id: &str, now: i64) -> Result<&SyncSession, SyncError> {
|
|
330
|
+
let session = self
|
|
331
|
+
.sessions
|
|
332
|
+
.get(id)
|
|
333
|
+
.ok_or_else(|| SyncError::new("SYNC_SESSION_INVALID", "session not found"))?;
|
|
334
|
+
if session.status != SyncSessionStatus::Active || session.expires_at <= now {
|
|
335
|
+
return Err(SyncError::new(
|
|
336
|
+
"SYNC_SESSION_EXPIRED",
|
|
337
|
+
"session is not active",
|
|
338
|
+
));
|
|
339
|
+
}
|
|
340
|
+
Ok(session)
|
|
341
|
+
}
|
|
342
|
+
pub fn committed(&self, id: &str) -> Option<u64> {
|
|
343
|
+
self.operation_positions.get(id).copied()
|
|
344
|
+
}
|
|
345
|
+
pub fn record(
|
|
346
|
+
&mut self,
|
|
347
|
+
operation: SyncOperation,
|
|
348
|
+
outcome: ReconciliationOutcome,
|
|
349
|
+
decision_id: Option<String>,
|
|
350
|
+
now: i64,
|
|
351
|
+
) -> Result<u64, SyncError> {
|
|
352
|
+
if let Some(position) = self.committed(&operation.operation_id) {
|
|
353
|
+
return Ok(position);
|
|
354
|
+
}
|
|
355
|
+
self.next_position += 1;
|
|
356
|
+
let position = self.next_position;
|
|
357
|
+
if matches!(
|
|
358
|
+
operation.operation.kind,
|
|
359
|
+
crate::state_contract::TransactionOperationKind::Delete
|
|
360
|
+
) {
|
|
361
|
+
self.tombstones.insert(
|
|
362
|
+
operation.resource.uri.clone(),
|
|
363
|
+
Tombstone {
|
|
364
|
+
resource: operation.resource.uri.clone(),
|
|
365
|
+
deletion_operation: operation.operation_id.clone(),
|
|
366
|
+
causal_context: operation.causal_context.clone(),
|
|
367
|
+
schema_version: operation.schema_version,
|
|
368
|
+
server_position: position,
|
|
369
|
+
retain_until: now + 2_592_000,
|
|
370
|
+
},
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
self.operation_positions
|
|
374
|
+
.insert(operation.operation_id.clone(), position);
|
|
375
|
+
self.operations.push(StoredSyncOperation {
|
|
376
|
+
position,
|
|
377
|
+
operation,
|
|
378
|
+
outcome,
|
|
379
|
+
decision_id,
|
|
380
|
+
committed_at: now,
|
|
381
|
+
});
|
|
382
|
+
self.persist()?;
|
|
383
|
+
Ok(position)
|
|
384
|
+
}
|
|
385
|
+
pub fn pull(
|
|
386
|
+
&self,
|
|
387
|
+
session_id: &str,
|
|
388
|
+
cursor: &SyncCursor,
|
|
389
|
+
limit: usize,
|
|
390
|
+
now: i64,
|
|
391
|
+
) -> Result<(Vec<StoredSyncOperation>, SyncCursor), SyncError> {
|
|
392
|
+
let session = self.session(session_id, now)?;
|
|
393
|
+
if cursor.tenant_id != session.device.tenant_id
|
|
394
|
+
|| cursor.application_id != session.device.application_id
|
|
395
|
+
|| cursor.subject != session.device.subject
|
|
396
|
+
|| cursor.scope_hash != session.scope.hash()
|
|
397
|
+
{
|
|
398
|
+
return Err(SyncError::new(
|
|
399
|
+
"SYNC_CURSOR_SCOPE_DENIED",
|
|
400
|
+
"cursor was issued for a different authorization projection",
|
|
401
|
+
));
|
|
402
|
+
}
|
|
403
|
+
let operations = self
|
|
404
|
+
.operations
|
|
405
|
+
.iter()
|
|
406
|
+
.filter(|stored| {
|
|
407
|
+
stored.position > cursor.position
|
|
408
|
+
&& stored.operation.tenant_id == cursor.tenant_id
|
|
409
|
+
&& stored.operation.application_id == cursor.application_id
|
|
410
|
+
&& session.scope.contains(&stored.operation.resource.uri)
|
|
411
|
+
})
|
|
412
|
+
.take(limit.min(1000))
|
|
413
|
+
.cloned()
|
|
414
|
+
.collect::<Vec<_>>();
|
|
415
|
+
let position = operations
|
|
416
|
+
.last()
|
|
417
|
+
.map(|v| v.position)
|
|
418
|
+
.unwrap_or(cursor.position);
|
|
419
|
+
let mut next = cursor.clone();
|
|
420
|
+
next.position = position;
|
|
421
|
+
Ok((operations, next))
|
|
422
|
+
}
|
|
423
|
+
pub fn acknowledge(
|
|
424
|
+
&mut self,
|
|
425
|
+
session_id: &str,
|
|
426
|
+
cursor: SyncCursor,
|
|
427
|
+
now: i64,
|
|
428
|
+
) -> Result<SyncCheckpoint, SyncError> {
|
|
429
|
+
let session = self.session(session_id, now)?;
|
|
430
|
+
if cursor.scope_hash != session.scope.hash() || cursor.position > self.next_position {
|
|
431
|
+
return Err(SyncError::new(
|
|
432
|
+
"SYNC_CURSOR_INVALID",
|
|
433
|
+
"acknowledgement cursor is invalid",
|
|
434
|
+
));
|
|
435
|
+
}
|
|
436
|
+
let checkpoint = SyncCheckpoint {
|
|
437
|
+
device_id: session.device.device_id.clone(),
|
|
438
|
+
cursor,
|
|
439
|
+
causal_frontier: BTreeMap::new(),
|
|
440
|
+
updated_at: now,
|
|
441
|
+
};
|
|
442
|
+
self.checkpoints
|
|
443
|
+
.insert(checkpoint.device_id.clone(), checkpoint.clone());
|
|
444
|
+
self.persist()?;
|
|
445
|
+
Ok(checkpoint)
|
|
446
|
+
}
|
|
447
|
+
pub fn close(&mut self, id: &str) -> Result<(), SyncError> {
|
|
448
|
+
let session = self
|
|
449
|
+
.sessions
|
|
450
|
+
.get_mut(id)
|
|
451
|
+
.ok_or_else(|| SyncError::new("SYNC_SESSION_INVALID", "session not found"))?;
|
|
452
|
+
session.status = SyncSessionStatus::Closed;
|
|
453
|
+
self.persist()
|
|
454
|
+
}
|
|
455
|
+
pub fn acquire_lease(
|
|
456
|
+
&mut self,
|
|
457
|
+
device_id: &str,
|
|
458
|
+
runtime: &str,
|
|
459
|
+
now: i64,
|
|
460
|
+
ttl: i64,
|
|
461
|
+
) -> Result<SyncLease, SyncError> {
|
|
462
|
+
if self
|
|
463
|
+
.leases
|
|
464
|
+
.get(device_id)
|
|
465
|
+
.is_some_and(|lease| lease.expires_at > now && lease.owner_runtime != runtime)
|
|
466
|
+
{
|
|
467
|
+
return Err(SyncError::new(
|
|
468
|
+
"SYNC_LEASE_HELD",
|
|
469
|
+
"another runtime owns this device outbox",
|
|
470
|
+
));
|
|
471
|
+
}
|
|
472
|
+
let lease = SyncLease {
|
|
473
|
+
device_id: device_id.into(),
|
|
474
|
+
owner_runtime: runtime.into(),
|
|
475
|
+
expires_at: now + ttl,
|
|
476
|
+
heartbeat_at: now,
|
|
477
|
+
};
|
|
478
|
+
self.leases.insert(device_id.into(), lease.clone());
|
|
479
|
+
self.persist()?;
|
|
480
|
+
Ok(lease)
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
485
|
+
pub struct OutboxEntry {
|
|
486
|
+
pub operation: SyncOperation,
|
|
487
|
+
pub status: OutboxStatus,
|
|
488
|
+
pub attempts: u32,
|
|
489
|
+
pub last_error: Option<String>,
|
|
490
|
+
}
|
|
491
|
+
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
492
|
+
pub struct LocalSyncRuntime {
|
|
493
|
+
#[serde(skip)]
|
|
494
|
+
path: PathBuf,
|
|
495
|
+
#[serde(default)]
|
|
496
|
+
pub state: BTreeMap<String, Value>,
|
|
497
|
+
#[serde(default)]
|
|
498
|
+
pub outbox: BTreeMap<String, OutboxEntry>,
|
|
499
|
+
#[serde(default)]
|
|
500
|
+
pub tombstones: BTreeMap<String, Tombstone>,
|
|
501
|
+
}
|
|
502
|
+
impl LocalSyncRuntime {
|
|
503
|
+
pub fn load(path: impl AsRef<Path>) -> Result<Self, SyncError> {
|
|
504
|
+
let path = path.as_ref().to_path_buf();
|
|
505
|
+
if !path.exists() {
|
|
506
|
+
return Ok(Self {
|
|
507
|
+
path,
|
|
508
|
+
state: BTreeMap::new(),
|
|
509
|
+
outbox: BTreeMap::new(),
|
|
510
|
+
tombstones: BTreeMap::new(),
|
|
511
|
+
});
|
|
512
|
+
}
|
|
513
|
+
let mut value: Self = serde_json::from_slice(&fs::read(&path).map_err(io)?).map_err(io)?;
|
|
514
|
+
value.path = path;
|
|
515
|
+
Ok(value)
|
|
516
|
+
}
|
|
517
|
+
fn persist(&self) -> Result<(), SyncError> {
|
|
518
|
+
if let Some(parent) = self.path.parent() {
|
|
519
|
+
fs::create_dir_all(parent).map_err(io)?;
|
|
520
|
+
}
|
|
521
|
+
let temp = self.path.with_extension("tmp");
|
|
522
|
+
fs::write(&temp, serde_json::to_vec_pretty(self).map_err(io)?).map_err(io)?;
|
|
523
|
+
fs::rename(temp, &self.path).map_err(io)
|
|
524
|
+
}
|
|
525
|
+
pub fn apply_and_enqueue(&mut self, operation: SyncOperation) -> Result<(), SyncError> {
|
|
526
|
+
operation.validate_identity()?;
|
|
527
|
+
if self.outbox.contains_key(&operation.operation_id) {
|
|
528
|
+
return Ok(());
|
|
529
|
+
}
|
|
530
|
+
match operation.operation.kind {
|
|
531
|
+
crate::state_contract::TransactionOperationKind::Delete => {
|
|
532
|
+
self.state.remove(&operation.resource.uri);
|
|
533
|
+
}
|
|
534
|
+
_ => {
|
|
535
|
+
self.state.insert(
|
|
536
|
+
operation.resource.uri.clone(),
|
|
537
|
+
operation.operation.value.clone(),
|
|
538
|
+
);
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
self.outbox.insert(
|
|
542
|
+
operation.operation_id.clone(),
|
|
543
|
+
OutboxEntry {
|
|
544
|
+
operation,
|
|
545
|
+
status: OutboxStatus::Pending,
|
|
546
|
+
attempts: 0,
|
|
547
|
+
last_error: None,
|
|
548
|
+
},
|
|
549
|
+
);
|
|
550
|
+
self.persist()
|
|
551
|
+
}
|
|
552
|
+
pub fn apply_result(&mut self, result: &SyncResult) -> Result<(), SyncError> {
|
|
553
|
+
for ack in &result.acknowledgements {
|
|
554
|
+
if let Some(entry) = self.outbox.get_mut(&ack.operation_id) {
|
|
555
|
+
entry.status = OutboxStatus::Acknowledged;
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
for rejection in &result.rejections {
|
|
559
|
+
if let Some(entry) = self.outbox.get_mut(&rejection.operation_id) {
|
|
560
|
+
entry.status = if rejection.retryable {
|
|
561
|
+
OutboxStatus::Retryable
|
|
562
|
+
} else {
|
|
563
|
+
OutboxStatus::Rejected
|
|
564
|
+
};
|
|
565
|
+
entry.last_error = Some(format!("{}: {}", rejection.code, rejection.message));
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
for conflict in &result.conflicts {
|
|
569
|
+
if let Some(entry) = self.outbox.get_mut(&conflict.operation_id) {
|
|
570
|
+
entry.status = OutboxStatus::Conflicted;
|
|
571
|
+
entry.last_error = Some(conflict.reason.clone());
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
self.persist()
|
|
575
|
+
}
|
|
576
|
+
pub fn pending(&self) -> Vec<SyncOperation> {
|
|
577
|
+
self.outbox
|
|
578
|
+
.values()
|
|
579
|
+
.filter(|v| matches!(v.status, OutboxStatus::Pending | OutboxStatus::Retryable))
|
|
580
|
+
.map(|v| v.operation.clone())
|
|
581
|
+
.collect()
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
pub fn schema_sync_outcome(
|
|
586
|
+
client: u64,
|
|
587
|
+
server: u64,
|
|
588
|
+
compatibility: SchemaCompatibility,
|
|
589
|
+
) -> ReconciliationOutcome {
|
|
590
|
+
if client == server {
|
|
591
|
+
ReconciliationOutcome::Accept
|
|
592
|
+
} else {
|
|
593
|
+
match compatibility {
|
|
594
|
+
SchemaCompatibility::Safe => ReconciliationOutcome::Accept,
|
|
595
|
+
SchemaCompatibility::RequiresMigration => ReconciliationOutcome::Merge,
|
|
596
|
+
SchemaCompatibility::Destructive | SchemaCompatibility::Incompatible => {
|
|
597
|
+
ReconciliationOutcome::SchemaMismatch
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
fn io(error: impl std::fmt::Display) -> SyncError {
|
|
603
|
+
SyncError::new("SYNC_STORAGE_FAILURE", error.to_string())
|
|
604
|
+
}
|
|
605
|
+
fn hash_json(value: &impl Serialize) -> String {
|
|
606
|
+
hex(&Sha256::digest(
|
|
607
|
+
serde_json::to_vec(value).unwrap_or_default(),
|
|
608
|
+
))
|
|
609
|
+
}
|
|
610
|
+
fn hex(bytes: &[u8]) -> String {
|
|
611
|
+
bytes.iter().map(|v| format!("{v:02x}")).collect()
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
#[cfg(test)]
|
|
615
|
+
mod tests {
|
|
616
|
+
use super::*;
|
|
617
|
+
use crate::authorization::{
|
|
618
|
+
Grant, GrantConstraints, GrantSigner, GrantStatus, ResourceScope, ResourceScopeKind,
|
|
619
|
+
};
|
|
620
|
+
use crate::state_contract::TransactionOperationKind;
|
|
621
|
+
fn fixture() -> (GrantSigner, SignedGrant, SyncOperation) {
|
|
622
|
+
let signer = GrantSigner::new("k", vec![9; 32]).unwrap();
|
|
623
|
+
let grant = Grant {
|
|
624
|
+
contract_version: 1,
|
|
625
|
+
id: "g".into(),
|
|
626
|
+
issuer: Subject::Human("h".into()),
|
|
627
|
+
subject: Subject::Human("u".into()),
|
|
628
|
+
tenant_id: "t".into(),
|
|
629
|
+
application_id: "a".into(),
|
|
630
|
+
capabilities: ["state:write".into(), "state:read".into()].into(),
|
|
631
|
+
resources: vec![ResourceScope {
|
|
632
|
+
kind: ResourceScopeKind::ResourcePrefix,
|
|
633
|
+
uri: "flow://a/tasks/*".into(),
|
|
634
|
+
}],
|
|
635
|
+
constraints: GrantConstraints::default(),
|
|
636
|
+
issued_at: 1,
|
|
637
|
+
expires_at: 999,
|
|
638
|
+
parent_grant: None,
|
|
639
|
+
revision: 1,
|
|
640
|
+
status: GrantStatus::Active,
|
|
641
|
+
};
|
|
642
|
+
let signed = signer.sign(grant).unwrap();
|
|
643
|
+
let operation = SyncOperation {
|
|
644
|
+
contract_version: 1,
|
|
645
|
+
operation_id: "op1".into(),
|
|
646
|
+
tenant_id: "t".into(),
|
|
647
|
+
application_id: "a".into(),
|
|
648
|
+
subject: Subject::Human("u".into()),
|
|
649
|
+
device_id: "d".into(),
|
|
650
|
+
resource: Resource {
|
|
651
|
+
uri: "flow://a/tasks/1".into(),
|
|
652
|
+
attributes: BTreeMap::new(),
|
|
653
|
+
},
|
|
654
|
+
operation: TransactionOperation {
|
|
655
|
+
kind: TransactionOperationKind::Insert,
|
|
656
|
+
collection: "tasks".into(),
|
|
657
|
+
id: "1".into(),
|
|
658
|
+
value: serde_json::json!({"title":"one"}),
|
|
659
|
+
if_version: None,
|
|
660
|
+
},
|
|
661
|
+
causal_context: BTreeMap::new(),
|
|
662
|
+
schema_version: 1,
|
|
663
|
+
authorization_context: AuthorizationContext {
|
|
664
|
+
timestamp: 10,
|
|
665
|
+
..Default::default()
|
|
666
|
+
},
|
|
667
|
+
offline_grant: signed.clone(),
|
|
668
|
+
application_revision: "r1".into(),
|
|
669
|
+
created_at: 10,
|
|
670
|
+
};
|
|
671
|
+
(signer, signed, operation)
|
|
672
|
+
}
|
|
673
|
+
#[test]
|
|
674
|
+
fn local_outbox_is_durable_and_idempotent() {
|
|
675
|
+
let path = std::env::temp_dir().join(format!("feltdb-sync-{}.json", std::process::id()));
|
|
676
|
+
let (_, _, op) = fixture();
|
|
677
|
+
let mut local = LocalSyncRuntime::load(&path).unwrap();
|
|
678
|
+
local.apply_and_enqueue(op.clone()).unwrap();
|
|
679
|
+
local.apply_and_enqueue(op).unwrap();
|
|
680
|
+
let recovered = LocalSyncRuntime::load(&path).unwrap();
|
|
681
|
+
assert_eq!(recovered.outbox.len(), 1);
|
|
682
|
+
assert!(recovered.state.contains_key("flow://a/tasks/1"));
|
|
683
|
+
let _ = fs::remove_file(path);
|
|
684
|
+
}
|
|
685
|
+
#[test]
|
|
686
|
+
fn cursor_is_bound_to_projection_and_retry_is_duplicate() {
|
|
687
|
+
let path =
|
|
688
|
+
std::env::temp_dir().join(format!("feltdb-sync-store-{}.json", std::process::id()));
|
|
689
|
+
let (_, grant, op) = fixture();
|
|
690
|
+
let device = DeviceIdentity {
|
|
691
|
+
device_id: "d".into(),
|
|
692
|
+
tenant_id: "t".into(),
|
|
693
|
+
application_id: "a".into(),
|
|
694
|
+
subject: Subject::Human("u".into()),
|
|
695
|
+
created_at: 1,
|
|
696
|
+
};
|
|
697
|
+
let scope = SyncScope {
|
|
698
|
+
resources: vec!["flow://a/tasks/*".into()],
|
|
699
|
+
fields: BTreeMap::new(),
|
|
700
|
+
};
|
|
701
|
+
let mut store = SyncStore::load(&path).unwrap();
|
|
702
|
+
let session = store.create_session(device, scope, &grant, 10).unwrap();
|
|
703
|
+
let first = store
|
|
704
|
+
.record(op.clone(), ReconciliationOutcome::Accept, None, 11)
|
|
705
|
+
.unwrap();
|
|
706
|
+
let duplicate = store
|
|
707
|
+
.record(op, ReconciliationOutcome::AlreadyCommitted, None, 12)
|
|
708
|
+
.unwrap();
|
|
709
|
+
assert_eq!(first, duplicate);
|
|
710
|
+
let (pulled, cursor) = store
|
|
711
|
+
.pull(&session.session_id, &session.cursor, 100, 12)
|
|
712
|
+
.unwrap();
|
|
713
|
+
assert_eq!(pulled.len(), 1);
|
|
714
|
+
let mut wrong = session.cursor.clone();
|
|
715
|
+
wrong.scope_hash = "other".into();
|
|
716
|
+
assert_eq!(
|
|
717
|
+
store
|
|
718
|
+
.pull(&session.session_id, &wrong, 10, 12)
|
|
719
|
+
.unwrap_err()
|
|
720
|
+
.code,
|
|
721
|
+
"SYNC_CURSOR_SCOPE_DENIED"
|
|
722
|
+
);
|
|
723
|
+
store.acknowledge(&session.session_id, cursor, 12).unwrap();
|
|
724
|
+
let _ = fs::remove_file(path);
|
|
725
|
+
}
|
|
726
|
+
#[test]
|
|
727
|
+
fn selective_pull_excludes_other_resources_and_lease_prevents_duplicate_workers() {
|
|
728
|
+
let path =
|
|
729
|
+
std::env::temp_dir().join(format!("feltdb-sync-selective-{}.json", std::process::id()));
|
|
730
|
+
let (_, grant, first) = fixture();
|
|
731
|
+
let device = DeviceIdentity {
|
|
732
|
+
device_id: "d".into(),
|
|
733
|
+
tenant_id: "t".into(),
|
|
734
|
+
application_id: "a".into(),
|
|
735
|
+
subject: Subject::Human("u".into()),
|
|
736
|
+
created_at: 1,
|
|
737
|
+
};
|
|
738
|
+
let scope = SyncScope {
|
|
739
|
+
resources: vec!["flow://a/tasks/*".into()],
|
|
740
|
+
fields: BTreeMap::new(),
|
|
741
|
+
};
|
|
742
|
+
let mut store = SyncStore::load(&path).unwrap();
|
|
743
|
+
let session = store.create_session(device, scope, &grant, 10).unwrap();
|
|
744
|
+
store
|
|
745
|
+
.record(first, ReconciliationOutcome::Accept, None, 11)
|
|
746
|
+
.unwrap();
|
|
747
|
+
let mut outside = fixture().2;
|
|
748
|
+
outside.operation_id = "op2".into();
|
|
749
|
+
outside.resource.uri = "flow://a/private/1".into();
|
|
750
|
+
store
|
|
751
|
+
.record(outside, ReconciliationOutcome::Accept, None, 11)
|
|
752
|
+
.unwrap();
|
|
753
|
+
let (pulled, _) = store
|
|
754
|
+
.pull(&session.session_id, &session.cursor, 100, 12)
|
|
755
|
+
.unwrap();
|
|
756
|
+
assert_eq!(pulled.len(), 1);
|
|
757
|
+
store.acquire_lease("d", "tab-a", 12, 30).unwrap();
|
|
758
|
+
assert_eq!(
|
|
759
|
+
store.acquire_lease("d", "tab-b", 13, 30).unwrap_err().code,
|
|
760
|
+
"SYNC_LEASE_HELD"
|
|
761
|
+
);
|
|
762
|
+
let _ = fs::remove_file(path);
|
|
763
|
+
}
|
|
764
|
+
#[test]
|
|
765
|
+
fn delete_creates_retained_tombstone_and_schema_outcomes_are_explicit() {
|
|
766
|
+
let path =
|
|
767
|
+
std::env::temp_dir().join(format!("feltdb-sync-tombstone-{}.json", std::process::id()));
|
|
768
|
+
let (_, _, mut operation) = fixture();
|
|
769
|
+
operation.operation.kind = TransactionOperationKind::Delete;
|
|
770
|
+
operation.operation.value = Value::Null;
|
|
771
|
+
let mut store = SyncStore::load(&path).unwrap();
|
|
772
|
+
store
|
|
773
|
+
.record(operation, ReconciliationOutcome::Accept, None, 20)
|
|
774
|
+
.unwrap();
|
|
775
|
+
let tombstone = store.tombstones.get("flow://a/tasks/1").unwrap();
|
|
776
|
+
assert_eq!(tombstone.deletion_operation, "op1");
|
|
777
|
+
assert_eq!(
|
|
778
|
+
schema_sync_outcome(1, 2, SchemaCompatibility::Safe),
|
|
779
|
+
ReconciliationOutcome::Accept
|
|
780
|
+
);
|
|
781
|
+
assert_eq!(
|
|
782
|
+
schema_sync_outcome(1, 2, SchemaCompatibility::RequiresMigration),
|
|
783
|
+
ReconciliationOutcome::Merge
|
|
784
|
+
);
|
|
785
|
+
assert_eq!(
|
|
786
|
+
schema_sync_outcome(1, 2, SchemaCompatibility::Incompatible),
|
|
787
|
+
ReconciliationOutcome::SchemaMismatch
|
|
788
|
+
);
|
|
789
|
+
let _ = fs::remove_file(path);
|
|
790
|
+
}
|
|
791
|
+
#[test]
|
|
792
|
+
fn rejected_and_conflicted_results_remain_inspectable_after_restart() {
|
|
793
|
+
let path =
|
|
794
|
+
std::env::temp_dir().join(format!("feltdb-sync-result-{}.json", std::process::id()));
|
|
795
|
+
let (_, _, operation) = fixture();
|
|
796
|
+
let mut local = LocalSyncRuntime::load(&path).unwrap();
|
|
797
|
+
local.apply_and_enqueue(operation).unwrap();
|
|
798
|
+
let result = SyncResult {
|
|
799
|
+
acknowledgements: vec![],
|
|
800
|
+
rejections: vec![],
|
|
801
|
+
conflicts: vec![SyncConflict {
|
|
802
|
+
operation_id: "op1".into(),
|
|
803
|
+
resource: "flow://a/tasks/1".into(),
|
|
804
|
+
local_causal_context: BTreeMap::new(),
|
|
805
|
+
server_causal_context: [("server".into(), 2)].into(),
|
|
806
|
+
reason: "concurrent".into(),
|
|
807
|
+
}],
|
|
808
|
+
cursor: SyncCursor {
|
|
809
|
+
contract_version: 1,
|
|
810
|
+
tenant_id: "t".into(),
|
|
811
|
+
application_id: "a".into(),
|
|
812
|
+
subject: Subject::Human("u".into()),
|
|
813
|
+
scope_hash: "s".into(),
|
|
814
|
+
position: 2,
|
|
815
|
+
},
|
|
816
|
+
};
|
|
817
|
+
local.apply_result(&result).unwrap();
|
|
818
|
+
let recovered = LocalSyncRuntime::load(&path).unwrap();
|
|
819
|
+
assert_eq!(recovered.outbox["op1"].status, OutboxStatus::Conflicted);
|
|
820
|
+
let _ = fs::remove_file(path);
|
|
821
|
+
}
|
|
822
|
+
}
|