feltdb 0.6.1

Embedded durable state store: an append-only log with atomic multi-record transactions, version-conditional writes, and idempotent retries
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/// Distributed Capability Routing module
///
/// Handles routing capability execution requests to appropriate peers based on:
/// - Local vs remote capability availability
/// - Required state version compatibility
/// - Peer state acquisition capabilities
/// - Execution ownership semantics
use crate::{CapabilityExecutionResult, CapabilityRef, DistributedCapabilityAdvertisement};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Route decision for capability execution
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum RouteDecision {
    /// Execute locally
    Local,
    /// Execute remotely at specific peer
    Remote(String), // peer_id
    /// No suitable provider found
    Unavailable,
}

/// State version tracking for a peer
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PeerStateVersion {
    /// Peer ID
    pub peer_id: String,
    /// Namespace
    pub namespace: String,
    /// Current version at this peer
    pub version: u64,
    /// When the version was last updated
    pub updated_ms: u128,
}

impl PeerStateVersion {
    pub fn new(peer_id: String, namespace: String, version: u64) -> Self {
        Self {
            peer_id,
            namespace,
            version,
            updated_ms: crate::now_ms(),
        }
    }
}

/// State-aware capability routing engine
pub struct CapabilityRouter {
    /// Local capabilities
    local_capabilities: HashMap<String, DistributedCapabilityAdvertisement>,
    /// Remote capabilities by peer
    remote_capabilities: HashMap<String, Vec<DistributedCapabilityAdvertisement>>,
    /// Peer state versions
    peer_state_versions: HashMap<String, PeerStateVersion>,
}

impl CapabilityRouter {
    pub fn new() -> Self {
        Self {
            local_capabilities: HashMap::new(),
            remote_capabilities: HashMap::new(),
            peer_state_versions: HashMap::new(),
        }
    }

    /// Register a local capability
    pub fn register_local(&mut self, ad: DistributedCapabilityAdvertisement) {
        self.local_capabilities
            .insert(ad.capability_ref.canonical(), ad);
    }

    /// Register a remote capability
    pub fn register_remote(&mut self, ad: DistributedCapabilityAdvertisement) {
        let peer_id = ad.peer_id.clone();
        self.remote_capabilities
            .entry(peer_id)
            .or_insert_with(Vec::new)
            .push(ad);
    }

    /// Update peer state version
    pub fn update_peer_state(&mut self, peer_id: String, namespace: String, version: u64) {
        let key = format!("{}:{}", peer_id, namespace);
        self.peer_state_versions
            .insert(key, PeerStateVersion::new(peer_id, namespace, version));
    }

    /// Get the state version a peer has for a namespace
    pub fn get_peer_state_version(&self, peer_id: &str, namespace: &str) -> u64 {
        let key = format!("{}:{}", peer_id, namespace);
        self.peer_state_versions
            .get(&key)
            .map(|v| v.version)
            .unwrap_or(0)
    }

    /// Route a capability execution request
    pub fn route(
        &self,
        cap_ref: &CapabilityRef,
        required_state_version: u64,
        local_peer_id: &str,
    ) -> RouteDecision {
        let canonical = cap_ref.canonical();

        // Check if capability is available locally
        if self.local_capabilities.contains_key(&canonical) {
            // Check if local state meets requirements
            if self.check_state_compatibility(local_peer_id, cap_ref, required_state_version) {
                return RouteDecision::Local;
            }
        }

        // Find remote providers with compatible state
        let providers = self.find_compatible_providers(cap_ref, required_state_version);

        if let Some(provider) = providers.first() {
            RouteDecision::Remote(provider.peer_id.clone())
        } else {
            RouteDecision::Unavailable
        }
    }

    /// Check if a peer has compatible state version
    fn check_state_compatibility(
        &self,
        peer_id: &str,
        cap_ref: &CapabilityRef,
        required_version: u64,
    ) -> bool {
        let peer_version = self.get_peer_state_version(peer_id, &cap_ref.namespace);
        peer_version >= required_version
    }

    /// Find providers with compatible state versions
    pub fn find_compatible_providers(
        &self,
        cap_ref: &CapabilityRef,
        required_state_version: u64,
    ) -> Vec<&DistributedCapabilityAdvertisement> {
        let mut compatible = Vec::new();

        for (peer_id, capabilities) in &self.remote_capabilities {
            for cap_ad in capabilities {
                if cap_ad.capability_ref.canonical() == cap_ref.canonical() {
                    if self.check_state_compatibility(peer_id, cap_ref, required_state_version) {
                        compatible.push(cap_ad);
                    }
                }
            }
        }

        compatible
    }

    /// Check if capability requires state acquisition
    pub fn requires_state_acquisition(
        &self,
        provider_peer: &str,
        cap_ref: &CapabilityRef,
        required_state_version: u64,
    ) -> bool {
        let peer_version = self.get_peer_state_version(provider_peer, &cap_ref.namespace);
        peer_version < required_state_version
    }

    /// Plan state acquisition - list namespaces that need to be acquired
    pub fn plan_state_acquisition(
        &self,
        provider_peer: &str,
        cap_ref: &CapabilityRef,
        required_state_version: u64,
    ) -> Vec<String> {
        let peer_version = self.get_peer_state_version(provider_peer, &cap_ref.namespace);
        if peer_version < required_state_version {
            vec![cap_ref.namespace.clone()]
        } else {
            Vec::new()
        }
    }
}

impl Default for CapabilityRouter {
    fn default() -> Self {
        Self::new()
    }
}

/// Execution claim tracker for single-owner semantics
pub struct ExecutionClaimer {
    /// Claims by execution ID
    claims: HashMap<String, ExecutionClaim>,
}

/// A claim on an execution
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecutionClaim {
    /// Execution ID
    pub execution_id: String,
    /// Peer that claimed it
    pub claimant: String,
    /// Timestamp of claim
    pub claimed_ms: u128,
}

impl ExecutionClaim {
    pub fn new(execution_id: String, claimant: String) -> Self {
        Self {
            execution_id,
            claimant,
            claimed_ms: crate::now_ms(),
        }
    }
}

impl ExecutionClaimer {
    pub fn new() -> Self {
        Self {
            claims: HashMap::new(),
        }
    }

    /// Attempt to claim an execution
    /// Returns the claim if successful, None if already claimed
    pub fn try_claim(&mut self, execution_id: String, claimant: String) -> Option<ExecutionClaim> {
        if self.claims.contains_key(&execution_id) {
            None
        } else {
            let claim = ExecutionClaim::new(execution_id.clone(), claimant);
            self.claims.insert(execution_id, claim.clone());
            Some(claim)
        }
    }

    /// Get the claimant of an execution
    pub fn get_claimant(&self, execution_id: &str) -> Option<&str> {
        self.claims.get(execution_id).map(|c| c.claimant.as_str())
    }

    /// Check if execution is claimed by a specific peer
    pub fn is_claimed_by(&self, execution_id: &str, claimant: &str) -> bool {
        self.claims
            .get(execution_id)
            .map(|c| c.claimant == claimant)
            .unwrap_or(false)
    }
}

impl Default for ExecutionClaimer {
    fn default() -> Self {
        Self::new()
    }
}

/// Result publication tracker
pub struct ExecutionResultPublisher {
    /// Published results by execution ID
    results: HashMap<String, CapabilityExecutionResult>,
}

impl ExecutionResultPublisher {
    pub fn new() -> Self {
        Self {
            results: HashMap::new(),
        }
    }

    /// Publish an execution result
    pub fn publish(&mut self, result: CapabilityExecutionResult) {
        self.results
            .insert(result.execution_ref.execution_id.clone(), result);
    }

    /// Get a published result
    pub fn get_result(&self, execution_id: &str) -> Option<&CapabilityExecutionResult> {
        self.results.get(execution_id)
    }

    /// Check if result exists
    pub fn has_result(&self, execution_id: &str) -> bool {
        self.results.contains_key(execution_id)
    }
}

impl Default for ExecutionResultPublisher {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ExecutionRef;

    fn make_cap_ref(namespace: &str, capability: &str, version: u32) -> CapabilityRef {
        CapabilityRef::new(namespace.to_string(), capability.to_string(), version)
    }

    fn make_capability_ad(
        namespace: &str,
        capability: &str,
        version: u32,
        peer_id: &str,
    ) -> DistributedCapabilityAdvertisement {
        DistributedCapabilityAdvertisement::new(
            make_cap_ref(namespace, capability, version),
            peer_id.to_string(),
        )
    }

    #[test]
    fn test_router_local_capability() {
        let mut router = CapabilityRouter::new();
        let ad = make_capability_ad("docs", "vector-search", 1, "local");
        router.register_local(ad);

        router.update_peer_state("local".to_string(), "docs".to_string(), 851);

        let cap_ref = make_cap_ref("docs", "vector-search", 1);
        let decision = router.route(&cap_ref, 851, "local");
        assert_eq!(decision, RouteDecision::Local);
    }

    #[test]
    fn test_router_remote_capability() {
        let mut router = CapabilityRouter::new();
        let ad = make_capability_ad("docs", "vector-search", 1, "peer-b");
        router.register_remote(ad);

        router.update_peer_state("peer-b".to_string(), "docs".to_string(), 851);

        let cap_ref = make_cap_ref("docs", "vector-search", 1);
        let decision = router.route(&cap_ref, 851, "peer-a");
        assert_eq!(decision, RouteDecision::Remote("peer-b".to_string()));
    }

    #[test]
    fn test_router_unavailable() {
        let router = CapabilityRouter::new();
        let cap_ref = make_cap_ref("docs", "vector-search", 1);
        let decision = router.route(&cap_ref, 851, "peer-a");
        assert_eq!(decision, RouteDecision::Unavailable);
    }

    #[test]
    fn test_router_state_mismatch() {
        let mut router = CapabilityRouter::new();
        let ad = make_capability_ad("docs", "vector-search", 1, "peer-b");
        router.register_remote(ad);

        // Peer only has version 800, but we need 851
        router.update_peer_state("peer-b".to_string(), "docs".to_string(), 800);

        let cap_ref = make_cap_ref("docs", "vector-search", 1);
        let decision = router.route(&cap_ref, 851, "peer-a");
        assert_eq!(decision, RouteDecision::Unavailable);
    }

    #[test]
    fn test_router_requires_state_acquisition() {
        let mut router = CapabilityRouter::new();
        router.update_peer_state("peer-b".to_string(), "docs".to_string(), 800);

        let cap_ref = make_cap_ref("docs", "vector-search", 1);
        assert!(router.requires_state_acquisition("peer-b", &cap_ref, 851));
    }

    #[test]
    fn test_execution_claimer_single_claim() {
        let mut claimer = ExecutionClaimer::new();
        let claim = claimer.try_claim("exec-123".to_string(), "peer-a".to_string());
        assert!(claim.is_some());
        assert_eq!(claim.unwrap().claimant, "peer-a");
    }

    #[test]
    fn test_execution_claimer_duplicate_claim() {
        let mut claimer = ExecutionClaimer::new();
        let claim1 = claimer.try_claim("exec-123".to_string(), "peer-a".to_string());
        let claim2 = claimer.try_claim("exec-123".to_string(), "peer-b".to_string());

        assert!(claim1.is_some());
        assert!(claim2.is_none());
    }

    #[test]
    fn test_execution_claimer_get_claimant() {
        let mut claimer = ExecutionClaimer::new();
        claimer.try_claim("exec-123".to_string(), "peer-a".to_string());

        let claimant = claimer.get_claimant("exec-123");
        assert_eq!(claimant, Some("peer-a"));
    }

    #[test]
    fn test_execution_result_publisher() {
        let mut publisher = ExecutionResultPublisher::new();
        let cap_ref = make_cap_ref("docs", "vector-search", 1);
        let exec_ref = ExecutionRef::new("exec-123".to_string(), cap_ref, "peer-b".to_string());
        let result = serde_json::json!({"results": [1, 2, 3]});
        let exec_result = CapabilityExecutionResult::success(exec_ref, result);

        publisher.publish(exec_result);

        assert!(publisher.has_result("exec-123"));
        assert!(publisher.get_result("exec-123").is_some());
    }
}