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
//! # Task ledger (Phase 4)
//!
//! The recorded lifecycle of every compute task (RFC §5.6): who claimed it,
//! where it ran, what it cost, and — crucially — enough state for *another*
//! node (or a restarted one) to notice an expired task and requeue it.
//!
//! The ledger is written through the [`LedgerStore`] abstraction so the same
//! lifecycle logic runs over any backing store:
//!
//! - [`MemoryLedger`] — process-local, with truly atomic conditional writes;
//!   the default for single-node use and tests.
//! - A replicated GuardianDB store (KV/document) — the app implements the
//!   four methods over its store, gaining replicated auditability. Note the
//!   RFC §5.7 caveat: over an LWW store, `create_if_absent` is only
//!   best-effort across the network — rare races yield duplicate execution,
//!   which is why triggered tasks should be idempotent. (Under RFC 0001's
//!   `Strict` mode the conditional write becomes exact.)
//!
//! State machine: `Pending → Running → Done | Failed`, with
//! `Running` past its deadline treated as abandoned and requeued to
//! `Pending` until [`TaskRecord::attempts`] exhausts the retry budget.

use std::collections::HashMap;
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};

use async_trait::async_trait;
use iroh::EndpointId as NodeId;
use iroh_blobs::Hash;
use serde::{Deserialize, Serialize};

use super::protocol::CompletedTask;
use super::{ResourceLimits, TaskClass};

/// Stable identifier of a ledger entry. For triggered tasks this is the
/// deterministic dedup key (hash of rule + event), which is exactly what
/// makes the conditional write idempotent across replicas.
pub type TaskKey = String;

/// Where a task is in its lifecycle.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum TaskState {
    /// Claimed but not yet dispatched (or requeued after an expiry/failure).
    Pending,
    /// Dispatched; if `deadline_unix` passes without completion, the task is
    /// considered abandoned and eligible for requeue.
    Running { deadline_unix: u64 },
    /// Completed successfully.
    Done {
        /// The node that executed it.
        executor: NodeId,
    },
    /// Terminal failure (deterministic task error, or retry budget spent).
    Failed { error: String },
}

/// One task's full record in the ledger.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TaskRecord {
    pub key: TaskKey,
    /// BLAKE3 hash of the `.wasm` blob to run.
    pub wasm_hash: Hash,
    pub entrypoint: String,
    pub class: TaskClass,
    pub limits: ResourceLimits,
    /// Input bytes, kept in the record so a requeue can re-dispatch without
    /// the original event still being around.
    pub input: Vec<u8>,
    /// NN model the task needs (phase NN-3); threaded from the trigger rule's
    /// [`TaskSpec`](super::TaskSpec) into the dispatched `ExecuteRequest`.
    pub required_model: Option<String>,
    pub state: TaskState,
    /// Dispatch attempts so far (0 while still Pending for the first time).
    pub attempts: u32,
    pub created_at_unix: u64,
    pub updated_at_unix: u64,
    /// Output + metrics once `Done`.
    pub result: Option<CompletedTask>,
}

impl TaskRecord {
    /// A fresh `Pending` record.
    #[allow(clippy::too_many_arguments)]
    pub fn pending(
        key: TaskKey,
        wasm_hash: Hash,
        entrypoint: String,
        class: TaskClass,
        limits: ResourceLimits,
        input: Vec<u8>,
        required_model: Option<String>,
    ) -> Self {
        let now = unix_now();
        Self {
            key,
            wasm_hash,
            entrypoint,
            class,
            limits,
            input,
            required_model,
            state: TaskState::Pending,
            attempts: 0,
            created_at_unix: now,
            updated_at_unix: now,
            result: None,
        }
    }

    /// Whether this record should be (re)dispatched at `now`: `Pending`, or
    /// `Running` past its deadline (the dispatcher vanished mid-flight).
    pub fn needs_dispatch(&self, now_unix: u64) -> bool {
        match &self.state {
            TaskState::Pending => true,
            TaskState::Running { deadline_unix } => *deadline_unix < now_unix,
            TaskState::Done { .. } | TaskState::Failed { .. } => false,
        }
    }
}

/// Minimal storage surface the ledger needs. Implement it over a replicated
/// GuardianDB store to make the ledger network-visible; [`MemoryLedger`] is
/// the process-local reference implementation.
#[async_trait]
pub trait LedgerStore: Send + Sync + 'static {
    async fn get(&self, key: &str) -> Result<Option<Vec<u8>>, String>;
    async fn put(&self, key: &str, value: Vec<u8>) -> Result<(), String>;
    /// Writes `value` only if `key` is absent; returns whether it was written.
    /// This is the trigger-dedup primitive (RFC §5.7): the claim on a task.
    async fn create_if_absent(&self, key: &str, value: Vec<u8>) -> Result<bool, String>;
    /// Writes `value` only if the current stored bytes equal `expected`;
    /// returns whether the swap happened. The dispatch-ownership primitive:
    /// two dispatchers racing to claim the same task both read the same
    /// `expected`, but only one CAS succeeds. Exact on [`MemoryLedger`];
    /// best-effort over an LWW store (same §5.7 caveat as `create_if_absent`).
    async fn compare_and_swap(
        &self,
        key: &str,
        expected: Option<Vec<u8>>,
        value: Vec<u8>,
    ) -> Result<bool, String>;
    /// All stored records (the ledger is small; scans are fine at this scale).
    async fn list(&self) -> Result<Vec<Vec<u8>>, String>;
}

/// In-memory [`LedgerStore`] with exact conditional-write semantics.
#[derive(Debug, Default)]
pub struct MemoryLedger {
    entries: parking_lot::Mutex<HashMap<String, Vec<u8>>>,
}

impl MemoryLedger {
    pub fn new() -> Self {
        Self::default()
    }
}

#[async_trait]
impl LedgerStore for MemoryLedger {
    async fn get(&self, key: &str) -> Result<Option<Vec<u8>>, String> {
        Ok(self.entries.lock().get(key).cloned())
    }

    async fn put(&self, key: &str, value: Vec<u8>) -> Result<(), String> {
        self.entries.lock().insert(key.to_string(), value);
        Ok(())
    }

    async fn create_if_absent(&self, key: &str, value: Vec<u8>) -> Result<bool, String> {
        let mut entries = self.entries.lock();
        if entries.contains_key(key) {
            return Ok(false);
        }
        entries.insert(key.to_string(), value);
        Ok(true)
    }

    async fn compare_and_swap(
        &self,
        key: &str,
        expected: Option<Vec<u8>>,
        value: Vec<u8>,
    ) -> Result<bool, String> {
        let mut entries = self.entries.lock();
        if entries.get(key).map(|v| v.as_slice()) == expected.as_deref() {
            entries.insert(key.to_string(), value);
            Ok(true)
        } else {
            Ok(false)
        }
    }

    async fn list(&self) -> Result<Vec<Vec<u8>>, String> {
        Ok(self.entries.lock().values().cloned().collect())
    }
}

/// Typed lifecycle operations over a [`LedgerStore`].
#[derive(Clone)]
pub struct TaskLedger {
    store: Arc<dyn LedgerStore>,
}

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

impl TaskLedger {
    pub fn new(store: Arc<dyn LedgerStore>) -> Self {
        Self { store }
    }

    /// In-memory ledger, for single-node use and tests.
    pub fn in_memory() -> Self {
        Self::new(Arc::new(MemoryLedger::new()))
    }

    /// Claims a task: records it as `Pending` only if no record exists yet.
    /// Returns whether *this* call made the claim (the dedup verdict).
    pub async fn claim(&self, record: &TaskRecord) -> Result<bool, String> {
        let bytes = postcard::to_stdvec(record).map_err(|e| e.to_string())?;
        self.store.create_if_absent(&record.key, bytes).await
    }

    pub async fn get(&self, key: &str) -> Result<Option<TaskRecord>, String> {
        match self.store.get(key).await? {
            Some(bytes) => postcard::from_bytes(&bytes)
                .map(Some)
                .map_err(|e| e.to_string()),
            None => Ok(None),
        }
    }

    /// Atomically claims a task for dispatch: transitions it to `Running`
    /// (incrementing `attempts`) **only if** it is currently dispatchable
    /// (`Pending`, or `Running` past its deadline). Returns the now-`Running`
    /// record if this caller won the claim, or `None` if the task is absent,
    /// already terminal, or another dispatcher claimed it first.
    ///
    /// This is what stops the requeue loop and the reactive path from
    /// double-dispatching the same task: both read the same bytes but only
    /// one `compare_and_swap` succeeds.
    pub async fn claim_for_dispatch(
        &self,
        key: &str,
        deadline_unix: u64,
    ) -> Result<Option<TaskRecord>, String> {
        loop {
            let now = unix_now();
            let Some(bytes) = self.store.get(key).await? else {
                return Ok(None);
            };
            let record: TaskRecord = postcard::from_bytes(&bytes).map_err(|e| e.to_string())?;
            if !record.needs_dispatch(now) {
                return Ok(None); // terminal, or already Running within deadline
            }
            let mut running = record.clone();
            running.attempts += 1;
            running.state = TaskState::Running { deadline_unix };
            running.updated_at_unix = now;
            let new_bytes = postcard::to_stdvec(&running).map_err(|e| e.to_string())?;
            if self
                .store
                .compare_and_swap(key, Some(bytes), new_bytes)
                .await?
            {
                return Ok(Some(running));
            }
            // Lost the race (record changed under us): re-read and re-evaluate.
        }
    }

    /// Read-modify-write of one record. Returns the updated record, or
    /// `None` when the key does not exist.
    pub async fn update<F>(&self, key: &str, mutate: F) -> Result<Option<TaskRecord>, String>
    where
        F: FnOnce(&mut TaskRecord),
    {
        let Some(mut record) = self.get(key).await? else {
            return Ok(None);
        };
        mutate(&mut record);
        record.updated_at_unix = unix_now();
        let bytes = postcard::to_stdvec(&record).map_err(|e| e.to_string())?;
        self.store.put(key, bytes).await?;
        Ok(Some(record))
    }

    /// All records currently in the ledger.
    pub async fn all(&self) -> Result<Vec<TaskRecord>, String> {
        self.store
            .list()
            .await?
            .into_iter()
            .map(|bytes| postcard::from_bytes(&bytes).map_err(|e| e.to_string()))
            .collect()
    }

    /// Records that should be (re)dispatched now: `Pending` plus `Running`
    /// past deadline (abandoned by a vanished dispatcher).
    pub async fn needing_dispatch(&self) -> Result<Vec<TaskRecord>, String> {
        let now = unix_now();
        Ok(self
            .all()
            .await?
            .into_iter()
            .filter(|r| r.needs_dispatch(now))
            .collect())
    }
}

/// Seconds since the Unix epoch.
pub(crate) fn unix_now() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs()
}

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

    fn record(key: &str) -> TaskRecord {
        TaskRecord::pending(
            key.to_string(),
            Hash::new(b"wasm"),
            "gdb_run".into(),
            TaskClass::Media,
            ResourceLimits::default(),
            b"photo bytes".to_vec(),
            None,
        )
    }

    #[tokio::test]
    async fn claim_is_idempotent() {
        let ledger = TaskLedger::in_memory();
        let rec = record("task-1");
        assert!(ledger.claim(&rec).await.unwrap(), "first claim wins");
        assert!(!ledger.claim(&rec).await.unwrap(), "second claim loses");
        // The stored record is the first one, untouched.
        let stored = ledger.get("task-1").await.unwrap().unwrap();
        assert_eq!(stored.state, TaskState::Pending);
        assert_eq!(stored.attempts, 0);
    }

    #[tokio::test]
    async fn lifecycle_transitions_and_result() {
        let ledger = TaskLedger::in_memory();
        ledger.claim(&record("t")).await.unwrap();

        let running = ledger
            .update("t", |r| {
                r.attempts += 1;
                r.state = TaskState::Running {
                    deadline_unix: unix_now() + 60,
                };
            })
            .await
            .unwrap()
            .unwrap();
        assert_eq!(running.attempts, 1);
        assert!(
            !running.needs_dispatch(unix_now()),
            "running within deadline"
        );

        let executor = iroh::SecretKey::generate().public();
        let done = ledger
            .update("t", |r| {
                r.state = TaskState::Done { executor };
                r.result = Some(CompletedTask {
                    output: b"thumb".to_vec(),
                    metrics: crate::compute::ExecMetrics {
                        fuel_consumed: 42,
                        duration_ms: 7,
                        peak_memory_bytes: 65_536,
                    },
                });
            })
            .await
            .unwrap()
            .unwrap();
        assert!(
            !done.needs_dispatch(unix_now() + 10_000),
            "done is terminal"
        );
        assert_eq!(done.result.unwrap().output, b"thumb");
    }

    #[tokio::test]
    async fn expired_running_needs_dispatch_again() {
        let ledger = TaskLedger::in_memory();
        ledger.claim(&record("t")).await.unwrap();
        ledger
            .update("t", |r| {
                r.attempts = 1;
                r.state = TaskState::Running {
                    deadline_unix: unix_now() - 5, // already past
                };
            })
            .await
            .unwrap();

        let due = ledger.needing_dispatch().await.unwrap();
        assert_eq!(due.len(), 1);
        assert_eq!(due[0].key, "t");
    }

    #[tokio::test]
    async fn update_of_missing_key_is_none() {
        let ledger = TaskLedger::in_memory();
        assert!(ledger.update("ghost", |_| {}).await.unwrap().is_none());
    }

    #[tokio::test]
    async fn only_one_dispatcher_claims_a_pending_task() {
        // Two concurrent claim_for_dispatch on the same Pending task: exactly
        // one wins (the atomic CAS), the other gets None — this is what stops
        // the requeue loop and the reactive path from double-dispatching.
        let ledger = TaskLedger::in_memory();
        ledger.claim(&record("t")).await.unwrap();

        let deadline = unix_now() + 60;
        let (a, b) = tokio::join!(
            ledger.claim_for_dispatch("t", deadline),
            ledger.claim_for_dispatch("t", deadline),
        );
        let claimed = [a.unwrap(), b.unwrap()];
        assert_eq!(
            claimed.iter().filter(|c| c.is_some()).count(),
            1,
            "exactly one claim wins"
        );
        let winner = claimed.iter().find_map(|c| c.clone()).unwrap();
        assert_eq!(winner.attempts, 1);
        assert!(matches!(winner.state, TaskState::Running { .. }));

        // A third attempt now sees Running-within-deadline and does not claim.
        assert!(
            ledger
                .claim_for_dispatch("t", deadline)
                .await
                .unwrap()
                .is_none()
        );
    }

    #[tokio::test]
    async fn claim_for_dispatch_ignores_terminal_and_absent() {
        let ledger = TaskLedger::in_memory();
        assert!(
            ledger
                .claim_for_dispatch("ghost", unix_now() + 60)
                .await
                .unwrap()
                .is_none(),
            "absent key"
        );

        ledger.claim(&record("done")).await.unwrap();
        let executor = iroh::SecretKey::generate().public();
        ledger
            .update("done", |r| r.state = TaskState::Done { executor })
            .await
            .unwrap();
        assert!(
            ledger
                .claim_for_dispatch("done", unix_now() + 60)
                .await
                .unwrap()
                .is_none(),
            "terminal task is not re-claimed"
        );
    }
}