adk-kernel 3.0.0

Agent kernel for ADK-Rust: a resident supervisor with a process model, wake sources, an event bus and a control plane over durable runs
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
//! The Firestore [`ProcessStore`] (spec 3.2 §7.1, §13 Q8, feature `firestore`), and the
//! helpers the kernel's other Firestore stores share.

use std::sync::Arc;

use async_trait::async_trait;
use chrono::NaiveDate;
use firestore::errors::{BackoffError, FirestoreError};
use firestore::{FirestoreDb, FirestoreTransactionOptions, FirestoreWritePrecondition};
use futures::future::BoxFuture;
use serde::{Deserialize, Serialize};

use super::{
    ProcessFilter, ProcessStore, SpendScope, UsageTotals, counters, day_number, stored_amount,
    stored_micro_usd, stored_total,
};
use crate::bus::SpendDelta;
use crate::error::{KernelError, Result};
use crate::partition::PartitionId;
use crate::process::ProcessMeta;
use crate::sched::QueueClass;
use crate::wake::WakeSet;

const PROCESSES: &str = "adk_kernel_processes";
const PROCESS_SPEND: &str = "adk_kernel_spend";
const PARTITION_SPEND: &str = "adk_kernel_partition_spend";

/// The backend name in [`KernelError::Store`].
pub(super) const BACKEND: &str = "firestore";

/// The longest identifier a document is named after: its hex must fit Firestore's 1,500-byte
/// document IDs.
const MAX_ID_BYTES: usize = 700;

/// How long one operation may spend retrying transactions Firestore aborted for contention.
const TRANSACTION_DEADLINE: chrono::TimeDelta = chrono::TimeDelta::seconds(120);

pub(super) type TxResult<T> = std::result::Result<T, BackoffError<KernelError>>;
pub(super) type TxFuture<'b, T> = BoxFuture<'b, TxResult<T>>;

pub(super) fn store_error(action: &str, error: &dyn std::fmt::Display) -> KernelError {
    KernelError::Store { backend: BACKEND, reason: format!("could not {action}: {error}") }
}

/// A client of `project_id` with Application Default Credentials; the `firestore` crate
/// reaches the emulator when `FIRESTORE_EMULATOR_HOST` is set.
pub(super) async fn connect(project_id: &str) -> Result<FirestoreDb> {
    FirestoreDb::new(project_id).await.map_err(|error| store_error("connect to Firestore", &error))
}

/// The document ID of `id`: the hex of its UTF-8 bytes, which holds no `/` and orders as
/// the bytes do, so a query ordered by document name follows byte order as on every backend.
pub(super) fn doc_id(what: &str, id: &str) -> Result<String> {
    if id.len() > MAX_ID_BYTES {
        return Err(KernelError::Store {
            backend: BACKEND,
            reason: format!(
                "a Firestore {what} id is at most {MAX_ID_BYTES} bytes, and this one has {}",
                id.len()
            ),
        });
    }
    Ok(id.bytes().map(|byte| format!("{byte:02x}")).collect())
}

/// The document ID of a key of several parts: the SHA-256 of their JSON array, in hex. The
/// document stores the parts, and every read compares them.
pub(super) fn digest_id(parts: &[&str]) -> String {
    use sha2::Digest as _;

    let encoded = serde_json::to_vec(parts).unwrap_or_default();
    sha2::Sha256::digest(&encoded).iter().map(|byte| format!("{byte:02x}")).collect()
}

pub(super) fn transaction_options() -> FirestoreTransactionOptions {
    FirestoreTransactionOptions::new().with_max_elapsed_time(TRANSACTION_DEADLINE)
}

/// Maps a failure inside a transaction: retried when Firestore says it may succeed, such as
/// a contention abort.
pub(super) fn tx_fail(
    action: &'static str,
) -> impl Fn(FirestoreError) -> BackoffError<KernelError> {
    move |error| {
        let retryable = matches!(&error, FirestoreError::DatabaseError(db) if db.retry_possible);
        let error = store_error(action, &error);
        if retryable { BackoffError::transient(error) } else { BackoffError::permanent(error) }
    }
}

/// The [`KernelError`] a transaction body failed with, or the transaction's own failure.
pub(super) fn transaction_error(error: FirestoreError, action: &str) -> KernelError {
    match error {
        FirestoreError::ErrorInTransaction(inner) => match inner.source.downcast::<KernelError>() {
            Ok(error) => *error,
            Err(source) => store_error(action, &source),
        },
        other => store_error(action, &other),
    }
}

/// Deletes the document `doc_name` of `collection` when `matches` accepts what it holds,
/// reading and deleting in one transaction, so of two concurrent deletes one answers `true`.
pub(super) async fn delete_where<T>(
    db: &FirestoreDb,
    collection: &'static str,
    doc_name: String,
    matches: impl Fn(&T) -> bool + Send + Sync + 'static,
    action: &'static str,
) -> Result<bool>
where
    T: for<'de> Deserialize<'de> + Send + Sync + 'static,
{
    let (doc_name, matches) = (Arc::new(doc_name), Arc::new(matches));
    db.run_transaction_with_options(
        move |db, transaction| -> TxFuture<'_, bool> {
            let (doc_name, matches) = (doc_name.clone(), matches.clone());
            Box::pin(async move {
                let stored: Option<T> = db
                    .fluent()
                    .select()
                    .by_id_in(collection)
                    .obj()
                    .one(doc_name.as_str())
                    .await
                    .map_err(tx_fail(action))?;
                if !stored.as_ref().is_some_and(|stored| matches(stored)) {
                    return Ok(false);
                }
                db.fluent()
                    .delete()
                    .from(collection)
                    .document_id(doc_name.as_str())
                    .add_to_transaction(transaction)
                    .map_err(tx_fail(action))?;
                Ok(true)
            })
        },
        transaction_options(),
    )
    .await
    .map_err(|error| transaction_error(error, action))
}

/// JSON text a store wrote, read back.
pub(super) fn json<T: serde::de::DeserializeOwned>(what: &str, text: &str) -> Result<T> {
    serde_json::from_str(text).map_err(|error| KernelError::Store {
        backend: BACKEND,
        reason: format!("a stored {what} is corrupt: {error}"),
    })
}

/// One process, as stored.
#[derive(Debug, Clone, Serialize, Deserialize)]
struct ProcessDoc {
    pid: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    parent: Option<String>,
    partition: String,
    queue: String,
    kind: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    wake: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    suspended_by: Option<String>,
    #[serde(default)]
    suspend_requested: bool,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    agenda_item: Option<String>,
    labels: String,
}

impl ProcessDoc {
    fn new(meta: &ProcessMeta) -> Result<Self> {
        Ok(Self {
            pid: meta.pid.clone(),
            parent: meta.parent.clone(),
            partition: meta.partition.as_str().to_string(),
            queue: meta.queue.as_str().to_string(),
            kind: serde_json::to_string(&meta.kind)?,
            wake: meta.wake.as_ref().map(serde_json::to_string).transpose()?,
            suspended_by: meta.suspended_by.as_ref().map(serde_json::to_string).transpose()?,
            suspend_requested: meta.suspend_requested,
            agenda_item: meta.agenda_item.clone(),
            labels: serde_json::to_string(&meta.labels)?,
        })
    }

    fn decode(self) -> Result<ProcessMeta> {
        Ok(ProcessMeta {
            kind: json("process kind", &self.kind)?,
            wake: self.wake.map(|wake| json("wake set", &wake)).transpose()?,
            suspended_by: self
                .suspended_by
                .map(|suspension| json("suspension", &suspension))
                .transpose()?,
            labels: json("label set", &self.labels)?,
            partition: PartitionId::new(self.partition),
            queue: QueueClass::from_name(&self.queue),
            suspend_requested: self.suspend_requested,
            agenda_item: self.agenda_item,
            parent: self.parent,
            pid: self.pid,
        })
    }
}

/// The fields one update of a process sets; `None` deletes the field.
#[derive(Debug, Default, Serialize, Deserialize)]
#[serde(default)]
struct ProcessFields {
    #[serde(skip_serializing_if = "Option::is_none")]
    wake: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    queue: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    suspend_requested: Option<bool>,
}

/// One day's spend of a process, or of a partition when `pid` is absent.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
struct SpendDoc {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pid: Option<String>,
    partition: String,
    day: i64,
    #[serde(default, skip_serializing)]
    micro_usd: i64,
    /// Model calls, total tokens and units by name, written only by increments.
    #[serde(default, skip_serializing)]
    counters: std::collections::BTreeMap<String, i64>,
}

/// A [`ProcessStore`] in Firestore, shared by every replica (spec 3.2 §7.1, §13 Q8, feature
/// `firestore`).
///
/// | Collection (document ID) | Contents |
/// |--------------------------|----------|
/// | `adk_kernel_processes` (hex of the pid) | `pid`, `partition`, `queue`, `suspend_requested` and, when set, `parent` and `agenda_item`; `kind`, `labels` and, when set, `wake` and `suspended_by` as JSON text |
/// | `adk_kernel_spend` (SHA-256 of pid, partition and day) | One process's spend in one partition on one UTC day: `pid`, `partition`, `day` (days from the common era), `micro_usd`, and `counters`: model calls, total tokens and units by name |
/// | `adk_kernel_partition_spend` (SHA-256 of partition and day) | One partition's spend on one UTC day: `partition`, `day`, `micro_usd`, `counters` |
///
/// `list` queries with equality filters only and Firestore orders their results by
/// document name, the hex of the pid, which orders as the pid's UTF-8 bytes do; Firestore
/// serves such queries from its automatic single-field indexes, so no composite index is
/// needed. A pid longer than 700 bytes is refused. `set_wake`, `set_queue` and
/// `set_suspend_requested` write one field under a field mask with an exists precondition,
/// so writers changing different fields of one row never lose each other's update.
/// `add_spend` adds to the process's and the partition's day totals with Firestore's
/// server-side increment in one commit, so concurrent additions all count; `spend` reads
/// the scope's day totals with one equality query and sums the days in range. Deleting a
/// process keeps its spend.
///
/// # Example
///
/// ```rust,no_run
/// # async fn example() -> adk_kernel::Result<()> {
/// let store = adk_kernel::FirestoreProcessStore::connect("my-project").await?;
/// store.migrate().await?;
/// # Ok(())
/// # }
/// ```
#[derive(Clone)]
pub struct FirestoreProcessStore {
    db: FirestoreDb,
}

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

impl FirestoreProcessStore {
    /// A store over `db`, such as the client `adk_session::FirestoreRunStore::from_db` uses,
    /// so the kernel's collections sit beside the run, schedule and governance stores.
    pub fn from_db(db: FirestoreDb) -> Self {
        Self { db }
    }

    /// Connects to `project_id` with Application Default Credentials.
    ///
    /// # Errors
    ///
    /// Returns [`KernelError::Store`] when the client cannot be created.
    pub async fn connect(project_id: &str) -> Result<Self> {
        Ok(Self::from_db(connect(project_id).await?))
    }

    /// The client the store uses.
    pub fn db(&self) -> &FirestoreDb {
        &self.db
    }

    /// Prepares the store's storage. Firestore creates collections on first write and
    /// serves every query from its automatic single-field indexes, so there is nothing to
    /// create; the method exists so every backend is set up the same way.
    ///
    /// # Errors
    ///
    /// Never returns an error.
    pub async fn migrate(&self) -> Result<()> {
        Ok(())
    }

    /// Writes `fields` under the mask `mask` to the process `pid`, failing when there is
    /// none.
    async fn update(&self, pid: &str, mask: &str, fields: ProcessFields) -> Result<()> {
        let result = self
            .db
            .fluent()
            .update()
            .fields([mask])
            .in_col(PROCESSES)
            .precondition(FirestoreWritePrecondition::Exists(true))
            .document_id(doc_id("process", pid)?)
            .object(&fields)
            .execute::<ProcessFields>()
            .await;
        match result {
            Ok(_) => Ok(()),
            Err(FirestoreError::DataNotFoundError(_)) => {
                Err(KernelError::NotFound { pid: pid.into() })
            }
            Err(error) => Err(store_error("update the process table", &error)),
        }
    }

    /// The day totals of `field` = `key` in `collection`.
    async fn day_totals(&self, collection: &str, field: &str, key: &str) -> Result<Vec<SpendDoc>> {
        self.db
            .fluent()
            .select()
            .from(collection)
            .filter(|q| q.for_all([q.field(field).eq(key)]))
            .obj::<SpendDoc>()
            .query()
            .await
            .map_err(|error| store_error("read the spend totals", &error))
    }
}

#[async_trait]
impl ProcessStore for FirestoreProcessStore {
    async fn put(&self, meta: ProcessMeta) -> Result<()> {
        let doc = ProcessDoc::new(&meta)?;
        self.db
            .fluent()
            .update()
            .in_col(PROCESSES)
            .document_id(doc_id("process", &meta.pid)?)
            .object(&doc)
            .execute::<ProcessDoc>()
            .await
            .map(|_| ())
            .map_err(|error| store_error("write the process table", &error))
    }

    async fn get(&self, pid: &str) -> Result<Option<ProcessMeta>> {
        let Ok(id) = doc_id("process", pid) else {
            return Ok(None);
        };
        let doc: Option<ProcessDoc> = self
            .db
            .fluent()
            .select()
            .by_id_in(PROCESSES)
            .obj()
            .one(id)
            .await
            .map_err(|error| store_error("read the process table", &error))?;
        doc.filter(|doc| doc.pid == pid).map(ProcessDoc::decode).transpose()
    }

    async fn list(&self, filter: &ProcessFilter) -> Result<Vec<ProcessMeta>> {
        if filter.limit == Some(0) {
            return Ok(Vec::new());
        }
        let mut select = self.db.fluent().select().from(PROCESSES).filter(|q| {
            q.for_all([
                filter.partition.as_ref().and_then(|p| q.field("partition").eq(p.as_str())),
                filter.queue.as_ref().and_then(|queue| q.field("queue").eq(queue.as_str())),
                filter.parent.as_ref().and_then(|parent| q.field("parent").eq(parent.as_str())),
                filter
                    .suspend_requested
                    .and_then(|requested| q.field("suspend_requested").eq(requested)),
            ])
        });
        // Firestore takes a limit up to `i32::MAX`; a larger one limits nothing.
        if let Some(limit) = filter.limit.and_then(|limit| i32::try_from(limit).ok()) {
            select = select.limit(limit.unsigned_abs());
        }
        let docs: Vec<ProcessDoc> =
            select.obj().query().await.map_err(|error| store_error("list processes", &error))?;
        docs.into_iter().map(ProcessDoc::decode).collect()
    }

    async fn set_wake(&self, pid: &str, wake: Option<WakeSet>) -> Result<()> {
        let wake = wake.as_ref().map(serde_json::to_string).transpose()?;
        self.update(pid, "wake", ProcessFields { wake, ..ProcessFields::default() }).await
    }

    async fn set_queue(&self, pid: &str, queue: QueueClass) -> Result<()> {
        let queue = Some(queue.as_str().to_string());
        self.update(pid, "queue", ProcessFields { queue, ..ProcessFields::default() }).await
    }

    async fn delete(&self, pid: &str) -> Result<bool> {
        let Ok(id) = doc_id("process", pid) else {
            return Ok(false);
        };
        let pid = pid.to_string();
        let matches = move |doc: &ProcessDoc| doc.pid == pid;
        delete_where(&self.db, PROCESSES, id, matches, "delete from the process table").await
    }

    async fn set_suspend_requested(&self, pid: &str, requested: bool) -> Result<()> {
        let fields =
            ProcessFields { suspend_requested: Some(requested), ..ProcessFields::default() };
        self.update(pid, "suspend_requested", fields).await
    }

    async fn add_spend(
        &self,
        pid: &str,
        partition: &PartitionId,
        day: NaiveDate,
        delta: SpendDelta,
    ) -> Result<()> {
        let mut increments = vec![("micro_usd".to_string(), stored_micro_usd(BACKEND, &delta)?)];
        for (name, amount) in counters(&delta) {
            increments.push((format!("counters.{name}"), stored_amount(BACKEND, name, amount)?));
        }
        let increments = Arc::new(increments);
        let day = day_number(day);
        let day_text = day.to_string();
        let process = SpendDoc {
            pid: Some(pid.to_string()),
            partition: partition.as_str().to_string(),
            day,
            ..SpendDoc::default()
        };
        let shared = SpendDoc { pid: None, ..process.clone() };
        let writes = Arc::new([
            (
                PROCESS_SPEND,
                digest_id(&[pid, partition.as_str(), &day_text]),
                vec!["pid", "partition", "day"],
                process,
            ),
            (
                PARTITION_SPEND,
                digest_id(&[partition.as_str(), &day_text]),
                vec!["partition", "day"],
                shared,
            ),
        ]);
        self.db
            .run_transaction_with_options(
                move |db, transaction| -> TxFuture<'_, ()> {
                    let writes = writes.clone();
                    let increments = increments.clone();
                    Box::pin(async move {
                        for (collection, id, mask, doc) in writes.iter() {
                            db.fluent()
                                .update()
                                .fields(mask)
                                .in_col(collection)
                                .document_id(id)
                                .object(doc)
                                .transforms(|t| {
                                    t.fields(
                                        increments.iter().map(|(field, amount)| {
                                            t.field(field).increment(*amount)
                                        }),
                                    )
                                })
                                .add_to_transaction(transaction)
                                .map_err(tx_fail("add to the spend totals"))?;
                        }
                        Ok(())
                    })
                },
                transaction_options(),
            )
            .await
            .map_err(|error| transaction_error(error, "add to the spend totals"))
    }

    async fn spend(
        &self,
        scope: &SpendScope,
        from: NaiveDate,
        to_inclusive: NaiveDate,
    ) -> Result<u64> {
        let docs = match scope {
            SpendScope::Process(pid) => self.day_totals(PROCESS_SPEND, "pid", pid).await?,
            SpendScope::Partition(partition) => {
                self.day_totals(PARTITION_SPEND, "partition", partition.as_str()).await?
            }
        };
        let days = day_number(from)..=day_number(to_inclusive);
        let total = docs
            .iter()
            .filter(|doc| days.contains(&doc.day))
            .fold(0_i64, |total, doc| total.saturating_add(doc.micro_usd));
        stored_total(BACKEND, total)
    }

    async fn usage(
        &self,
        scope: &SpendScope,
        from: NaiveDate,
        to_inclusive: NaiveDate,
    ) -> Result<UsageTotals> {
        let docs = match scope {
            SpendScope::Process(pid) => self.day_totals(PROCESS_SPEND, "pid", pid).await?,
            SpendScope::Partition(partition) => {
                self.day_totals(PARTITION_SPEND, "partition", partition.as_str()).await?
            }
        };
        let days = day_number(from)..=day_number(to_inclusive);
        let mut totals = UsageTotals::default();
        for doc in docs.iter().filter(|doc| days.contains(&doc.day)) {
            totals.micro_usd =
                totals.micro_usd.saturating_add(stored_total(BACKEND, doc.micro_usd)?);
            for (name, amount) in &doc.counters {
                totals.add_counter(name, stored_total(BACKEND, *amount)?);
            }
        }
        Ok(totals)
    }
}