oxirouter 0.1.0

Autonomous Semantic Federation Engine for the Edge - Learned source selection for SPARQL federated queries with context-awareness
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
//! Context provider trait and combined context

use serde::{Deserialize, Serialize};

#[cfg(feature = "geo")]
use super::GeoContext;

#[cfg(any(feature = "device", feature = "std"))]
use super::DeviceContext;

#[cfg(any(feature = "load", feature = "std"))]
use super::LoadContext;

#[cfg(any(feature = "legal", feature = "std"))]
use super::LegalContext;

/// Combined context from all 4 brains
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct CombinedContext {
    /// Geographic/physical context (oxigdal)
    #[cfg(feature = "geo")]
    pub geo: Option<GeoContext>,

    /// Device/hardware context (mielin)
    #[cfg(any(feature = "device", feature = "std"))]
    pub device: Option<DeviceContext>,

    /// Load/task queue context (celers)
    #[cfg(any(feature = "load", feature = "std"))]
    pub load: Option<LoadContext>,

    /// Legal/compliance context (legalis)
    #[cfg(any(feature = "legal", feature = "std"))]
    pub legal: Option<LegalContext>,

    /// Timestamp when context was collected (Unix epoch seconds)
    pub timestamp: u64,
}

impl CombinedContext {
    /// Create a new empty combined context
    #[must_use]
    pub const fn new() -> Self {
        Self {
            #[cfg(feature = "geo")]
            geo: None,
            #[cfg(any(feature = "device", feature = "std"))]
            device: None,
            #[cfg(any(feature = "load", feature = "std"))]
            load: None,
            #[cfg(any(feature = "legal", feature = "std"))]
            legal: None,
            timestamp: 0,
        }
    }

    /// Set the geo context
    #[cfg(feature = "geo")]
    #[must_use]
    pub fn with_geo(mut self, geo: GeoContext) -> Self {
        self.geo = Some(geo);
        self
    }

    /// Set the device context
    #[cfg(any(feature = "device", feature = "std"))]
    #[must_use]
    pub fn with_device(mut self, device: DeviceContext) -> Self {
        self.device = Some(device);
        self
    }

    /// Set the load context
    #[cfg(any(feature = "load", feature = "std"))]
    #[must_use]
    pub fn with_load(mut self, load: LoadContext) -> Self {
        self.load = Some(load);
        self
    }

    /// Set the legal context
    #[cfg(any(feature = "legal", feature = "std"))]
    #[must_use]
    pub fn with_legal(mut self, legal: LegalContext) -> Self {
        self.legal = Some(legal);
        self
    }

    /// Set the timestamp
    #[must_use]
    pub const fn with_timestamp(mut self, timestamp: u64) -> Self {
        self.timestamp = timestamp;
        self
    }

    /// Check if the context is stale (older than max_age_secs)
    #[must_use]
    pub const fn is_stale(&self, current_time: u64, max_age_secs: u64) -> bool {
        if self.timestamp == 0 {
            return true;
        }
        current_time.saturating_sub(self.timestamp) > max_age_secs
    }

    /// Get the overall quality score of the context (0.0 - 1.0)
    #[must_use]
    pub fn quality_score(&self) -> f32 {
        #[cfg(any(
            feature = "geo",
            feature = "device",
            feature = "load",
            feature = "legal",
            feature = "std",
        ))]
        let mut score = 0.0_f32;
        #[cfg(not(any(
            feature = "geo",
            feature = "device",
            feature = "load",
            feature = "legal",
            feature = "std",
        )))]
        let score = 0.0_f32;

        #[cfg(any(
            feature = "geo",
            feature = "device",
            feature = "load",
            feature = "legal",
            feature = "std",
        ))]
        let mut count = 0.0_f32;
        #[cfg(not(any(
            feature = "geo",
            feature = "device",
            feature = "load",
            feature = "legal",
            feature = "std",
        )))]
        let count = 0.0_f32;

        #[cfg(feature = "geo")]
        {
            if self.geo.is_some() {
                score += 1.0;
            }
            count += 1.0;
        }

        #[cfg(any(feature = "device", feature = "std"))]
        {
            if self.device.is_some() {
                score += 1.0;
            }
            count += 1.0;
        }

        #[cfg(any(feature = "load", feature = "std"))]
        {
            if self.load.is_some() {
                score += 1.0;
            }
            count += 1.0;
        }

        #[cfg(any(feature = "legal", feature = "std"))]
        {
            if self.legal.is_some() {
                score += 1.0;
            }
            count += 1.0;
        }

        if count > 0.0 { score / count } else { 0.0 }
    }
}

/// Trait for providing context information
pub trait ContextProvider: Send + Sync {
    /// Get the combined context from all available sources
    fn get_combined_context(&self) -> CombinedContext;

    /// Get geo context (physical brain)
    #[cfg(feature = "geo")]
    fn get_geo_context(&self) -> Option<GeoContext> {
        None
    }

    /// Get device context (body brain)
    #[cfg(any(feature = "device", feature = "std"))]
    fn get_device_context(&self) -> Option<DeviceContext> {
        None
    }

    /// Get load context (situation brain)
    #[cfg(any(feature = "load", feature = "std"))]
    fn get_load_context(&self) -> Option<LoadContext> {
        None
    }

    /// Get legal context (social brain)
    #[cfg(any(feature = "legal", feature = "std"))]
    fn get_legal_context(&self) -> Option<LegalContext> {
        None
    }
}

/// Default context provider that returns no context
#[derive(Debug, Clone, Copy, Default)]
pub struct DefaultContextProvider;

impl ContextProvider for DefaultContextProvider {
    fn get_combined_context(&self) -> CombinedContext {
        #[cfg(any(
            feature = "geo",
            feature = "device",
            feature = "load",
            feature = "legal",
            feature = "std",
        ))]
        let mut ctx = CombinedContext::new();
        #[cfg(not(any(
            feature = "geo",
            feature = "device",
            feature = "load",
            feature = "legal",
            feature = "std",
        )))]
        let ctx = CombinedContext::new();

        #[cfg(feature = "geo")]
        {
            ctx.geo = self.get_geo_context();
        }

        #[cfg(any(feature = "device", feature = "std"))]
        {
            ctx.device = self.get_device_context();
        }

        #[cfg(any(feature = "load", feature = "std"))]
        {
            ctx.load = self.get_load_context();
        }

        #[cfg(any(feature = "legal", feature = "std"))]
        {
            ctx.legal = self.get_legal_context();
        }

        // Set timestamp
        #[cfg(all(feature = "std", not(target_arch = "wasm32")))]
        {
            use std::time::{SystemTime, UNIX_EPOCH};
            ctx.timestamp = SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .map(|d| d.as_secs())
                .unwrap_or(0);
        }

        ctx
    }
}

/// Ecosystem context provider integrating all 4 brains via injectable sensors.
///
/// Each sensor slot is optional. When `None`, the corresponding brain returns
/// `None` — identical to the previous stub behavior. Wire in concrete sensors
/// (e.g., [`crate::context::oxigdal_sensor::StaticOxigdalGeoSensor`],
/// [`crate::context::mielin_sensor::MielinDeviceSensor`]) to get real context.
///
/// # Cache
///
/// Combined context is cached internally with a configurable TTL. The cache
/// uses a `Mutex<Option<CombinedContext>>` for interior mutability under `&self`.
/// On Mutex poison (a prior panic inside the lock), the poisoned guard is
/// recovered via `PoisonError::into_inner` rather than propagating panic.
/// On `try_lock` contention, context is recomputed without caching.
#[cfg(feature = "ecosystem")]
pub struct EcosystemContextProvider {
    /// Optional geographic sensor (physical brain).
    #[cfg(feature = "geo")]
    geo_sensor: Option<Box<dyn crate::context::sensor::GeoSensor>>,

    /// Optional device sensor (body brain).
    #[cfg(any(feature = "device", feature = "std"))]
    device_sensor: Option<Box<dyn crate::context::sensor::DeviceSensor>>,

    /// Optional load sensor (situation brain).
    #[cfg(any(feature = "load", feature = "std"))]
    load_sensor: Option<Box<dyn crate::context::sensor::LoadSensor>>,

    /// Optional policy engine (social brain).
    #[cfg(any(feature = "legal", feature = "std"))]
    policy_engine: Option<Box<dyn crate::context::sensor::PolicyEngine>>,

    /// Cache TTL in seconds.
    cache_ttl: u64,

    /// Mutex-guarded cache of the last combined context.
    #[cfg(feature = "std")]
    cached: std::sync::Mutex<Option<CombinedContext>>,
}

#[cfg(feature = "ecosystem")]
impl EcosystemContextProvider {
    /// Create a new ecosystem context provider with no sensors wired in.
    ///
    /// All brains return `None` until sensors are attached via the builder methods.
    #[must_use]
    pub fn new() -> Self {
        Self {
            #[cfg(feature = "geo")]
            geo_sensor: None,
            #[cfg(any(feature = "device", feature = "std"))]
            device_sensor: None,
            #[cfg(any(feature = "load", feature = "std"))]
            load_sensor: None,
            #[cfg(any(feature = "legal", feature = "std"))]
            policy_engine: None,
            cache_ttl: 60,
            #[cfg(feature = "std")]
            cached: std::sync::Mutex::new(None),
        }
    }

    /// Set the cache TTL in seconds (default: 60).
    #[must_use]
    pub fn with_ttl(mut self, ttl_seconds: u64) -> Self {
        self.cache_ttl = ttl_seconds;
        self
    }

    /// Attach a geographic sensor (physical brain).
    #[cfg(feature = "geo")]
    #[must_use]
    pub fn with_geo_sensor(mut self, sensor: Box<dyn crate::context::sensor::GeoSensor>) -> Self {
        self.geo_sensor = Some(sensor);
        self
    }

    /// Attach a device sensor (body brain).
    #[cfg(any(feature = "device", feature = "std"))]
    #[must_use]
    pub fn with_device_sensor(
        mut self,
        sensor: Box<dyn crate::context::sensor::DeviceSensor>,
    ) -> Self {
        self.device_sensor = Some(sensor);
        self
    }

    /// Attach a load sensor (situation brain).
    #[cfg(any(feature = "load", feature = "std"))]
    #[must_use]
    pub fn with_load_sensor(mut self, sensor: Box<dyn crate::context::sensor::LoadSensor>) -> Self {
        self.load_sensor = Some(sensor);
        self
    }

    /// Attach a policy engine (social brain).
    #[cfg(any(feature = "legal", feature = "std"))]
    #[must_use]
    pub fn with_policy_engine(
        mut self,
        engine: Box<dyn crate::context::sensor::PolicyEngine>,
    ) -> Self {
        self.policy_engine = Some(engine);
        self
    }

    /// Invalidate the internal cache, forcing recomputation on the next call.
    pub fn invalidate_cache(&self) {
        #[cfg(feature = "std")]
        {
            let mut guard = self
                .cached
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            *guard = None;
        }
    }

    /// Query the legal context for the inferred jurisdiction.
    ///
    /// The jurisdiction string is derived by querying the geo sensor for a
    /// country code. Falls back to `"UNKNOWN"` if no geo sensor is wired in
    /// or if the sensor cannot determine a country.
    #[cfg(feature = "legal")]
    fn compute_legal_context(&self) -> Option<LegalContext> {
        let engine = self.policy_engine.as_ref()?;
        let jurisdiction = {
            #[cfg(feature = "geo")]
            {
                self.geo_sensor
                    .as_ref()
                    .and_then(|g| g.sense())
                    .and_then(|gc| gc.country_code)
                    .unwrap_or_else(|| "UNKNOWN".to_string())
            }
            #[cfg(not(feature = "geo"))]
            {
                "UNKNOWN".to_string()
            }
        };
        engine.evaluate_for_jurisdiction(&jurisdiction)
    }

    /// Build a fresh `CombinedContext` by querying all wired sensors.
    fn build_fresh(&self) -> CombinedContext {
        let mut ctx = CombinedContext::new();

        #[cfg(feature = "geo")]
        {
            ctx.geo = self.geo_sensor.as_ref().and_then(|s| s.sense());
        }

        #[cfg(any(feature = "device", feature = "std"))]
        {
            ctx.device = self.device_sensor.as_ref().and_then(|s| s.sense());
        }

        #[cfg(any(feature = "load", feature = "std"))]
        {
            ctx.load = self.load_sensor.as_ref().and_then(|s| s.sense());
        }

        #[cfg(feature = "legal")]
        {
            ctx.legal = self.compute_legal_context();
        }

        #[cfg(all(feature = "std", not(target_arch = "wasm32")))]
        {
            use std::time::{SystemTime, UNIX_EPOCH};
            ctx.timestamp = SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .map(|d| d.as_secs())
                .unwrap_or(0);
        }

        ctx
    }
}

#[cfg(feature = "ecosystem")]
impl Default for EcosystemContextProvider {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(feature = "ecosystem")]
impl ContextProvider for EcosystemContextProvider {
    fn get_combined_context(&self) -> CombinedContext {
        #[cfg(feature = "std")]
        {
            use std::time::{SystemTime, UNIX_EPOCH};

            // Fast path: try_lock to avoid blocking on contention.
            if let Ok(guard) = self.cached.try_lock() {
                if let Some(ref cached) = *guard {
                    let now = SystemTime::now()
                        .duration_since(UNIX_EPOCH)
                        .map(|d| d.as_secs())
                        .unwrap_or(0);
                    if !cached.is_stale(now, self.cache_ttl) {
                        return cached.clone();
                    }
                }
                // Cache miss or stale — drop the try_lock guard before building.
                drop(guard);
            }

            // Build fresh context outside the lock to avoid holding it during I/O.
            let fresh = self.build_fresh();

            // Store in cache — recover from poison rather than propagating.
            let mut guard = self
                .cached
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            *guard = Some(fresh.clone());

            fresh
        }

        #[cfg(not(feature = "std"))]
        {
            // no_std: always build fresh (no Mutex, no time).
            self.build_fresh()
        }
    }

    #[cfg(feature = "geo")]
    fn get_geo_context(&self) -> Option<GeoContext> {
        self.geo_sensor.as_ref().and_then(|s| s.sense())
    }

    #[cfg(any(feature = "device", feature = "std"))]
    fn get_device_context(&self) -> Option<DeviceContext> {
        self.device_sensor.as_ref().and_then(|s| s.sense())
    }

    #[cfg(any(feature = "load", feature = "std"))]
    fn get_load_context(&self) -> Option<LoadContext> {
        self.load_sensor.as_ref().and_then(|s| s.sense())
    }

    #[cfg(any(feature = "legal", feature = "std"))]
    fn get_legal_context(&self) -> Option<LegalContext> {
        #[cfg(feature = "legal")]
        {
            Self::compute_legal_context(self)
        }
        #[cfg(not(feature = "legal"))]
        {
            self.policy_engine
                .as_ref()
                .and_then(|e| e.evaluate_for_jurisdiction("UNKNOWN"))
        }
    }
}

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

    #[test]
    fn test_default_provider() {
        let provider = DefaultContextProvider;
        let ctx = provider.get_combined_context();
        assert!(ctx.timestamp > 0 || cfg!(not(feature = "std")));
    }

    #[test]
    fn test_combined_context_quality() {
        let ctx = CombinedContext::new();
        assert!(ctx.quality_score() >= 0.0);
    }
}