guardian-db 0.19.0

High-performance, local-first decentralized database built on Rust and Iroh
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
//! # Capability telemetry (Phase 3)
//!
//! Samples this node's capacity (CPU, RAM, load) and gossips it to the
//! network as a [`CapabilityVector`], while collecting the vectors of other
//! nodes into a [`CapabilityDirectory`](super::scheduler::CapabilityDirectory)
//! for the scheduler to rank.
//!
//! Publication is **passive with hysteresis** (RFC §5.2): a vector is only
//! re-broadcast when a dynamic field crosses a threshold or a slow heartbeat
//! elapses, so capability traffic stays negligible.
//!
//! Vectors are *hints, not contracts*: they are unauthenticated gossip
//! payloads, and a wrong (or forged) vector costs at most one failed-over
//! delegation attempt — admission on the executor always has the final word.

use std::sync::Arc;
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};

use futures::StreamExt;
use iroh::EndpointId as NodeId;
use iroh_gossip::api::GossipSender;
use iroh_gossip::net::Gossip;
use iroh_gossip::proto::TopicId;
use tokio::task::JoinHandle;
use tracing::{debug, warn};

use super::protocol::ComputeProtocolHandler;
use super::scheduler::CapabilityDirectory;
use super::{Accel, CapabilityVector, CpuArch};

/// Well-known gossip topic where compute-enabled nodes exchange capability
/// vectors. Hashed with blake3 into the [`TopicId`], the same convention the
/// backend's pubsub uses.
///
/// `/2`: the vector gained `nn_models` (phase NN-3) — postcard cannot decode
/// across that change, so the topic was bumped (same discipline as the ALPN).
pub const CAPABILITY_TOPIC: &str = "guardian-db/compute/capabilities/2";

/// The gossip [`TopicId`] of [`CAPABILITY_TOPIC`].
pub fn capability_topic_id() -> TopicId {
    TopicId::from_bytes(blake3::hash(CAPABILITY_TOPIC.as_bytes()).into())
}

/// Tuning of the sampling/publication loop.
#[derive(Debug, Clone)]
pub struct TelemetryConfig {
    /// How often the local capacity is sampled.
    pub sample_interval: Duration,
    /// Maximum silence: re-publish even without changes after this long.
    pub heartbeat: Duration,
    /// Hysteresis: re-publish when CPU load moved at least this many points.
    pub cpu_delta_pct: u8,
    /// Hysteresis: re-publish when free RAM moved at least this fraction of
    /// total RAM (in percent points).
    pub ram_delta_pct: u8,
    /// Battery state to advertise. Automatic detection is platform-specific
    /// and deliberately out of scope; `None` advertises "not on battery"
    /// (the honest default for the desktops/servers that actually accept
    /// work — a node that *is* on battery should set `Some(true)`, which
    /// zeroes its advertised concurrency by default).
    pub on_battery: Option<bool>,
    /// Accelerators to advertise (Phase 5). Owner-declared: reliable
    /// automatic GPU/NPU detection would drag in a graphics stack, so the
    /// node owner states what the machine has; the scheduler rewards it
    /// only for `Inference`-class tasks.
    pub accelerators: Vec<Accel>,
}

impl Default for TelemetryConfig {
    fn default() -> Self {
        Self {
            sample_interval: Duration::from_secs(20),
            heartbeat: Duration::from_secs(180),
            cpu_delta_pct: 15,
            ram_delta_pct: 10,
            on_battery: None,
            accelerators: Vec::new(),
        }
    }
}

/// Samples the local machine into [`CapabilityVector`]s.
///
/// Keeps the `sysinfo::System` alive between samples because CPU usage is a
/// delta between two refreshes — the first sample of a fresh sampler reads 0%.
pub struct TelemetrySampler {
    system: sysinfo::System,
}

impl TelemetrySampler {
    #[allow(clippy::new_without_default)]
    pub fn new() -> Self {
        Self {
            system: sysinfo::System::new(),
        }
    }

    /// Takes one sample, merging machine facts with the executor's live
    /// policy (accepted classes, concurrency, tasks currently running).
    pub fn sample(
        &mut self,
        node_id: NodeId,
        handler: &ComputeProtocolHandler,
        config: &TelemetryConfig,
    ) -> CapabilityVector {
        self.system.refresh_cpu_usage();
        self.system.refresh_memory();

        let policy = handler.policy();
        let on_battery = config.on_battery.unwrap_or(false);
        // A node on battery does not advertise capacity unless its owner set
        // a policy saying otherwise is fine — here we advertise 0 slots and
        // let the local admission policy stay untouched (RFC §5.2).
        let max_concurrent = if on_battery { 0 } else { policy.max_concurrent };

        CapabilityVector {
            node_id,
            cpu_cores: self.system.cpus().len().min(u16::MAX as usize) as u16,
            cpu_arch: local_cpu_arch(),
            ram_total_mb: (self.system.total_memory() / (1024 * 1024)).min(u32::MAX as u64) as u32,
            cpu_load_pct: (self.system.global_cpu_usage().round().clamp(0.0, 100.0)) as u8,
            ram_free_mb: (self.system.available_memory() / (1024 * 1024)).min(u32::MAX as u64)
                as u32,
            on_battery,
            battery_pct: None,
            tasks_running: handler.tasks_running().min(u8::MAX as u32) as u8,
            max_concurrent: max_concurrent.min(u8::MAX as u32) as u8,
            accepts: policy.accepts,
            nn_models: handler.nn_model_names(),
            accelerators: advertised_accelerators(&config.accelerators),
            issued_at: SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs(),
        }
    }
}

/// The hysteresis rule: is `next` different enough from the last published
/// vector (or has the heartbeat elapsed) to justify a broadcast?
pub fn should_publish(
    previous: Option<&CapabilityVector>,
    next: &CapabilityVector,
    since_last_publish: Duration,
    config: &TelemetryConfig,
) -> bool {
    let Some(prev) = previous else {
        return true; // first sample: announce ourselves
    };
    if since_last_publish >= config.heartbeat {
        return true;
    }
    let cpu_moved = prev.cpu_load_pct.abs_diff(next.cpu_load_pct) >= config.cpu_delta_pct;
    let ram_threshold_mb = (u64::from(next.ram_total_mb) * u64::from(config.ram_delta_pct)) / 100;
    let ram_moved =
        u64::from(prev.ram_free_mb.abs_diff(next.ram_free_mb)) >= ram_threshold_mb.max(1);

    cpu_moved
        || ram_moved
        || prev.on_battery != next.on_battery
        || prev.tasks_running != next.tasks_running
        || prev.max_concurrent != next.max_concurrent
        || prev.accepts != next.accepts
        || prev.nn_models != next.nn_models
}

/// Background service that joins the capability topic, publishes this node's
/// vector (with hysteresis) and feeds received vectors into the directory.
///
/// Dropping it stops both loops.
pub struct CapabilityGossip {
    directory: Arc<CapabilityDirectory>,
    sender: Arc<tokio::sync::RwLock<GossipSender>>,
    publisher: JoinHandle<()>,
    receiver: JoinHandle<()>,
}

impl std::fmt::Debug for CapabilityGossip {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CapabilityGossip").finish_non_exhaustive()
    }
}

impl CapabilityGossip {
    /// Subscribes to [`CAPABILITY_TOPIC`] and spawns the publish/receive loops.
    ///
    /// `bootstrap` peers seed the gossip mesh; more can join later via
    /// [`CapabilityGossip::join_peers`] as connections are made.
    pub async fn spawn(
        gossip: Gossip,
        local: NodeId,
        handler: ComputeProtocolHandler,
        directory: Arc<CapabilityDirectory>,
        bootstrap: Vec<NodeId>,
        config: TelemetryConfig,
    ) -> Result<Self, String> {
        let topic = gossip
            .subscribe(capability_topic_id(), bootstrap)
            .await
            .map_err(|e| format!("capability topic subscribe: {e}"))?;
        let (sender, mut events) = topic.split();
        let sender = Arc::new(tokio::sync::RwLock::new(sender));

        // Receive loop: every valid vector goes into the directory.
        let receiver = {
            let directory = directory.clone();
            tokio::spawn(async move {
                while let Some(event) = events.next().await {
                    match event {
                        Ok(iroh_gossip::api::Event::Received(msg)) => {
                            match postcard::from_bytes::<CapabilityVector>(&msg.content) {
                                Ok(vector) if vector.node_id != local => {
                                    debug!(peer = %vector.node_id.fmt_short(),
                                           load = vector.cpu_load_pct,
                                           "compute: capability vector received");
                                    directory.upsert(vector);
                                }
                                Ok(_) => {} // our own echo, ignore
                                Err(e) => {
                                    debug!("compute: undecodable capability vector: {e}")
                                }
                            }
                        }
                        Ok(iroh_gossip::api::Event::NeighborDown(peer)) => {
                            // A vanished neighbor's vector is stale by definition.
                            directory.remove(&peer);
                        }
                        Ok(_) => {}
                        Err(e) => {
                            warn!("compute: capability gossip stream error: {e}");
                            break;
                        }
                    }
                }
            })
        };

        // Publish loop: sample on a timer, broadcast only when worth it.
        let publisher = {
            let sender = sender.clone();
            tokio::spawn(async move {
                let mut sampler = TelemetrySampler::new();
                let mut last_published: Option<CapabilityVector> = None;
                let mut last_publish_at = Instant::now();
                let mut ticker = tokio::time::interval(config.sample_interval);
                ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
                loop {
                    ticker.tick().await;
                    let vector = sampler.sample(local, &handler, &config);
                    if !should_publish(
                        last_published.as_ref(),
                        &vector,
                        last_publish_at.elapsed(),
                        &config,
                    ) {
                        continue;
                    }
                    let Ok(payload) = postcard::to_stdvec(&vector) else {
                        continue;
                    };
                    let result = sender.read().await.broadcast(payload.into()).await;
                    match result {
                        Ok(()) => {
                            debug!(
                                load = vector.cpu_load_pct,
                                free_mb = vector.ram_free_mb,
                                "compute: capability vector published"
                            );
                            last_published = Some(vector);
                            last_publish_at = Instant::now();
                        }
                        Err(e) => debug!("compute: capability broadcast failed: {e}"),
                    }
                }
            })
        };

        Ok(Self {
            directory,
            sender,
            publisher,
            receiver,
        })
    }

    /// Adds peers to the capability gossip mesh (e.g. right after connecting
    /// to them for replication).
    pub async fn join_peers(&self, peers: Vec<NodeId>) -> Result<(), String> {
        self.sender
            .read()
            .await
            .join_peers(peers)
            .await
            .map_err(|e| format!("capability join_peers: {e}"))
    }

    /// The directory this service feeds.
    pub fn directory(&self) -> Arc<CapabilityDirectory> {
        self.directory.clone()
    }
}

impl Drop for CapabilityGossip {
    fn drop(&mut self) {
        self.publisher.abort();
        self.receiver.abort();
    }
}

/// What the vector actually advertises for accelerators (phase NN-4).
///
/// Without `compute-nn-cuda` the owner's declaration is passed through
/// unchanged (declarative mode). With it, the GPU claim is **verified**: a
/// declared `Accel::Gpu` is dropped when CUDA is not actually available, and
/// a detected CUDA setup is advertised even if the owner forgot to declare it.
/// NPUs stay declarative — there is no portable detection for them.
fn advertised_accelerators(declared: &[Accel]) -> Vec<Accel> {
    #[cfg(feature = "compute-nn-cuda")]
    {
        verified_accelerators(declared, crate::compute::nn::cuda_available())
    }
    #[cfg(not(feature = "compute-nn-cuda"))]
    {
        declared.to_vec()
    }
}

/// The pure verification rule behind [`advertised_accelerators`]: the GPU
/// entry mirrors detection; everything else passes through.
#[cfg_attr(not(feature = "compute-nn-cuda"), allow(dead_code))]
fn verified_accelerators(declared: &[Accel], gpu_detected: bool) -> Vec<Accel> {
    let mut advertised: Vec<Accel> = declared
        .iter()
        .copied()
        .filter(|accel| *accel != Accel::Gpu)
        .collect();
    if gpu_detected {
        advertised.push(Accel::Gpu);
    }
    advertised
}

/// Maps the compile-time target architecture to the advertised [`CpuArch`].
fn local_cpu_arch() -> CpuArch {
    match std::env::consts::ARCH {
        "x86_64" => CpuArch::X86_64,
        "aarch64" => CpuArch::Aarch64,
        _ => CpuArch::Other,
    }
}

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

    fn test_node_id() -> NodeId {
        iroh::SecretKey::generate().public()
    }

    fn vector(cpu_load: u8, ram_free: u32, tasks: u8) -> CapabilityVector {
        CapabilityVector {
            node_id: test_node_id(),
            cpu_cores: 8,
            cpu_arch: CpuArch::X86_64,
            ram_total_mb: 16_000,
            accelerators: vec![],
            cpu_load_pct: cpu_load,
            ram_free_mb: ram_free,
            on_battery: false,
            battery_pct: None,
            tasks_running: tasks,
            max_concurrent: 4,
            accepts: vec![TaskClass::General],
            nn_models: vec![],
            issued_at: 0,
        }
    }

    #[test]
    fn gpu_claim_mirrors_detection() {
        // Declared GPU without real CUDA: dropped. Detected CUDA without a
        // declaration: advertised anyway. NPU passes through untouched.
        assert_eq!(
            verified_accelerators(&[Accel::Gpu, Accel::Npu], false),
            vec![Accel::Npu]
        );
        assert_eq!(
            verified_accelerators(&[Accel::Npu], true),
            vec![Accel::Npu, Accel::Gpu]
        );
        assert_eq!(verified_accelerators(&[], false), vec![]);
    }

    #[test]
    fn model_catalog_change_publishes() {
        let cfg = TelemetryConfig::default();
        let prev = vector(30, 8_000, 0);
        let mut next = vector(30, 8_000, 0);
        next.nn_models = vec!["whisper-tiny".into()];
        assert!(should_publish(
            Some(&prev),
            &next,
            Duration::from_secs(5),
            &cfg
        ));
    }

    #[test]
    fn first_sample_is_always_published() {
        let cfg = TelemetryConfig::default();
        assert!(should_publish(
            None,
            &vector(10, 8_000, 0),
            Duration::ZERO,
            &cfg
        ));
    }

    #[test]
    fn small_wobble_is_suppressed() {
        let cfg = TelemetryConfig::default();
        let prev = vector(30, 8_000, 0);
        // 10 points of CPU and ~3% of RAM: below both thresholds.
        let next = vector(40, 8_500, 0);
        assert!(!should_publish(
            Some(&prev),
            &next,
            Duration::from_secs(5),
            &cfg
        ));
    }

    #[test]
    fn threshold_crossings_publish() {
        let cfg = TelemetryConfig::default();
        let prev = vector(30, 8_000, 0);

        // CPU moved 20 points.
        assert!(should_publish(
            Some(&prev),
            &vector(50, 8_000, 0),
            Duration::from_secs(5),
            &cfg
        ));
        // RAM moved > 10% of total (1.6 GB of 16 GB).
        assert!(should_publish(
            Some(&prev),
            &vector(30, 6_000, 0),
            Duration::from_secs(5),
            &cfg
        ));
        // A task started/finished.
        assert!(should_publish(
            Some(&prev),
            &vector(30, 8_000, 1),
            Duration::from_secs(5),
            &cfg
        ));
    }

    #[test]
    fn heartbeat_publishes_even_without_changes() {
        let cfg = TelemetryConfig::default();
        let prev = vector(30, 8_000, 0);
        let same = vector(30, 8_000, 0);
        assert!(should_publish(Some(&prev), &same, cfg.heartbeat, &cfg));
    }

    #[test]
    fn sampler_reports_real_machine_facts() {
        let handler = ComputeProtocolHandler::new(
            Arc::new(NoFetch),
            crate::compute::ExecutorPolicy::default(),
        )
        .expect("handler");
        let cfg = TelemetryConfig::default();
        let mut sampler = TelemetrySampler::new();
        let v = sampler.sample(test_node_id(), &handler, &cfg);

        assert!(v.cpu_cores > 0);
        assert!(v.ram_total_mb > 0);
        assert!(v.cpu_load_pct <= 100);
        assert_eq!(v.tasks_running, 0);
        assert_eq!(v.max_concurrent, 2); // default policy
    }

    #[test]
    fn battery_advertises_zero_capacity() {
        let handler = ComputeProtocolHandler::new(
            Arc::new(NoFetch),
            crate::compute::ExecutorPolicy::default(),
        )
        .expect("handler");
        let cfg = TelemetryConfig {
            on_battery: Some(true),
            ..TelemetryConfig::default()
        };
        let mut sampler = TelemetrySampler::new();
        let v = sampler.sample(test_node_id(), &handler, &cfg);
        assert!(v.on_battery);
        assert_eq!(v.max_concurrent, 0, "battery nodes advertise no capacity");
    }

    struct NoFetch;

    #[async_trait::async_trait]
    impl crate::compute::WasmFetcher for NoFetch {
        async fn fetch_wasm(
            &self,
            _hash: &iroh_blobs::Hash,
            _p: NodeId,
        ) -> Result<Vec<u8>, String> {
            Err("test fetcher".into())
        }
    }
}