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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
//! The Neo4j [`ProcessStore`] (spec 3.2 §7.1, §13 Q8, feature `neo4j`), and the helpers the
//! kernel's other Neo4j stores share.

use std::future::Future;
use std::time::Duration;

use async_trait::async_trait;
use chrono::NaiveDate;
use neo4rs::{Graph, Neo4jErrorKind, Query, Row, Txn, query};

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;

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

/// Attempts at a statement that fails with a transient error, such as a detected deadlock,
/// before the error is returned.
const ATTEMPTS: u32 = 30;

/// Schema codes that mean an `IF NOT EXISTS` statement raced another one and lost.
const SCHEMA_ALREADY_EXISTS: [&str; 4] = [
    "Neo.ClientError.Schema.EquivalentSchemaRuleAlreadyExists",
    "Neo.ClientError.Schema.ConstraintAlreadyExists",
    "Neo.ClientError.Schema.IndexAlreadyExists",
    "Neo.ClientError.Schema.ConstraintWithNameAlreadyExists",
];

/// Two `MERGE`s creating one node at once: the loser's retry finds the node.
const CONSTRAINT_VIOLATION: &str = "Neo.ClientError.Schema.ConstraintValidationFailed";

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

/// Connects to the server at `uri`.
pub(super) async fn connect(uri: &str, user: &str, password: &str) -> Result<Graph> {
    Graph::new(uri, user, password).await.map_err(|error| store_error("connect to Neo4j", &error))
}

/// The hex of `text`'s UTF-8 bytes, which orders as the bytes do whatever the database's
/// string collation, so `ORDER BY` on it follows byte order as on every backend.
pub(super) fn order_key(text: &str) -> String {
    text.bytes().map(|byte| format!("{byte:02x}")).collect()
}

/// Runs one store call to completion on a task of its own.
///
/// neo4rs connections are not cancellation-safe: a call dropped between sending a bolt
/// message and reading its response returns the connection to the pool with that response
/// unread, and every later response on it is one behind. The kernel drops store calls when
/// a request or a worker attempt ends, so every call finishes its exchange on its own task
/// and a dropped caller only loses the result.
pub(super) async fn detached<T: Send + 'static>(
    call: impl Future<Output = Result<T>> + Send + 'static,
) -> Result<T> {
    match tokio::spawn(call).await {
        Ok(result) => result,
        Err(error) if error.is_panic() => std::panic::resume_unwind(error.into_panic()),
        Err(error) => Err(store_error("finish a Neo4j call", &error)),
    }
}

fn neo4j_code(error: &neo4rs::Error) -> Option<&str> {
    match error {
        neo4rs::Error::Neo4j(error) => Some(error.code()),
        _ => None,
    }
}

/// Whether running the statement again can succeed: a transient error, such as a detected
/// deadlock, or a `MERGE` that lost a race to create its node.
fn retryable(error: &neo4rs::Error) -> bool {
    match error {
        neo4rs::Error::Neo4j(neo4j) => {
            neo4j.kind() == Neo4jErrorKind::Transient || neo4j.code() == CONSTRAINT_VIOLATION
        }
        _ => false,
    }
}

/// Small randomized pause before retry `attempt`, growing with the attempt number.
fn backoff(attempt: u32) -> Duration {
    let jitter = u64::from(uuid::Uuid::new_v4().as_bytes()[0]) % 10;
    Duration::from_millis(u64::from(attempt.min(20)) * 5 + jitter)
}

/// Runs `attempt` until it succeeds, fails with an error running it again cannot fix, or
/// has failed [`ATTEMPTS`] times.
pub(super) async fn retry<T, F, Fut>(action: &str, mut attempt: F) -> Result<T>
where
    F: FnMut() -> Fut,
    Fut: Future<Output = std::result::Result<T, neo4rs::Error>>,
{
    let mut tries = 1;
    loop {
        match attempt().await {
            Err(error) if retryable(&error) && tries < ATTEMPTS => {
                tokio::time::sleep(backoff(tries)).await;
                tries += 1;
            }
            result => return result.map_err(|error| store_error(action, &error)),
        }
    }
}

/// Every row of an auto-commit `query`.
async fn fetch(graph: &Graph, query: Query) -> std::result::Result<Vec<Row>, neo4rs::Error> {
    let mut stream = graph.execute(query).await?;
    let mut rows = Vec::new();
    while let Some(row) = stream.next().await? {
        rows.push(row);
    }
    Ok(rows)
}

/// Every row of `query`, run as an auto-commit statement and retried as [`retry`] does.
pub(super) async fn rows(graph: &Graph, query: Query, action: &str) -> Result<Vec<Row>> {
    retry(action, || fetch(graph, query.clone())).await
}

/// Every row of `query`, run inside `txn`.
pub(super) async fn txn_rows(
    txn: &mut Txn,
    query: Query,
) -> std::result::Result<Vec<Row>, neo4rs::Error> {
    let mut stream = txn.execute(query).await?;
    let mut rows = Vec::new();
    while let Some(row) = stream.next(&mut *txn).await? {
        rows.push(row);
    }
    Ok(rows)
}

/// Column `name` of `row`.
pub(super) fn column<T: serde::de::DeserializeOwned>(row: &Row, name: &str) -> Result<T> {
    row.get::<T>(name).map_err(|error| KernelError::Store {
        backend: BACKEND,
        reason: format!("a stored row's column `{name}` is unreadable: {error}"),
    })
}

/// The integer column `name` of the first row, 0 when there is none.
pub(super) fn count(rows: &[Row], name: &str) -> Result<i64> {
    rows.first().map_or(Ok(0), |row| column::<i64>(row, name))
}

/// 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}"),
    })
}

/// Runs idempotent schema statements, tolerating a concurrent `migrate` that created the
/// same rule first.
pub(super) async fn run_schema(graph: &Graph, statements: &[&str]) -> Result<()> {
    for statement in statements {
        let mut tries = 1;
        loop {
            match graph.run(query(statement)).await {
                Ok(()) => break,
                Err(error)
                    if neo4j_code(&error)
                        .is_some_and(|code| SCHEMA_ALREADY_EXISTS.contains(&code)) =>
                {
                    break;
                }
                Err(error) if tries < ATTEMPTS && retryable(&error) => {
                    tokio::time::sleep(backoff(tries)).await;
                    tries += 1;
                }
                Err(error) => return Err(store_error("create the Neo4j schema", &error)),
            }
        }
    }
    Ok(())
}

/// The `LIMIT` clause of a list, with its parameter.
pub(super) fn limit_clause(limit: Option<usize>) -> (String, Option<i64>) {
    match limit {
        Some(limit) => {
            (" LIMIT $limit".to_string(), Some(i64::try_from(limit).unwrap_or(i64::MAX)))
        }
        None => (String::new(), None),
    }
}

/// The `WHERE` clause joining `conditions`, empty when there are none.
pub(super) fn where_clause(conditions: &[&str]) -> String {
    if conditions.is_empty() {
        String::new()
    } else {
        format!(" WHERE {}", conditions.join(" AND "))
    }
}

const PUT: &str = "\
MERGE (p:AdkKernelProcess {pid: $pid}) \
SET p = {pid: $pid, order_key: $order_key, partition: $partition, queue: $queue, kind: $kind, \
    labels: $labels, suspend_requested: $suspend_requested, parent: $parent, wake: $wake, \
    suspended_by: $suspended_by, agenda_item: $agenda_item}";

const COLUMNS: &str = "\
RETURN p.pid AS pid, p.parent AS parent, p.partition AS partition, p.queue AS queue, \
    p.kind AS kind, p.wake AS wake, p.suspended_by AS suspended_by, \
    p.suspend_requested AS suspend_requested, p.agenda_item AS agenda_item, p.labels AS labels";

const ADD_SPEND: &str = "\
MERGE (s:AdkKernelSpend {key: $key}) \
ON CREATE SET s.pid = $pid, s.partition = $partition, s.day = $day, s.micro_usd = 0 \
SET s.micro_usd = s.micro_usd + $amount \
WITH s \
UNWIND range(0, size($names) - 1) AS i \
MERGE (u:AdkKernelUsage {key: $key + ':' + $names[i]}) \
ON CREATE SET u.pid = $pid, u.partition = $partition, u.day = $day, u.counter = $names[i], \
    u.amount = 0 \
SET u.amount = u.amount + $amounts[i]";

/// A [`ProcessStore`] in Neo4j, shared by every replica (spec 3.2 §7.1, §13 Q8, feature
/// `neo4j`).
///
/// | Label | Key (unique) | Properties |
/// |-------|--------------|------------|
/// | `AdkKernelProcess` | `pid` | `order_key` (the hex of the pid), `partition`, `queue`, `suspend_requested` and, when set, `parent` and `agenda_item`, the properties `list` filters on; `kind`, `labels` and, when set, `wake` and `suspended_by` as JSON text |
/// | `AdkKernelSpend` | `key`: the hex of the pid and the partition with the day | `pid`, `partition`, `day` (days from the common era) and `micro_usd` |
/// | `AdkKernelUsage` | `key`: the spend key with the counter's name | `pid`, `partition`, `day`, `counter` (model calls, total tokens or a unit) and `amount` |
///
/// `list` orders by `order_key`, so pids order by their UTF-8 bytes whatever the database's
/// collation. `set_wake`, `set_queue` and `set_suspend_requested` each set one property, so
/// writers changing different fields of one row never lose each other's update.
/// `add_spend` adds to the stored total in one statement, under the write lock Cypher takes
/// on a property updated from its own value, so concurrent additions all count; deleting a
/// process keeps its spend. Every call runs on a task of its own, because a neo4rs
/// connection dropped mid-exchange is left out of step. Call [`migrate`](Self::migrate)
/// once per database to create the constraints and indexes.
///
/// # Example
///
/// ```rust,no_run
/// # async fn example() -> adk_kernel::Result<()> {
/// let store =
///     adk_kernel::Neo4jProcessStore::connect("bolt://localhost:7687", "neo4j", "password").await?;
/// store.migrate().await?;
/// # Ok(())
/// # }
/// ```
#[derive(Clone)]
pub struct Neo4jProcessStore {
    graph: Graph,
}

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

impl Neo4jProcessStore {
    /// A store over `graph`, such as the connection `adk_session::Neo4jRunStore::from_graph`
    /// uses, so the kernel's nodes sit beside the run, schedule and governance stores.
    pub fn from_graph(graph: Graph) -> Self {
        Self { graph }
    }

    /// Connects to the server at `uri`.
    ///
    /// # Errors
    ///
    /// Returns [`KernelError::Store`] when the server cannot be reached.
    pub async fn connect(uri: &str, user: &str, password: &str) -> Result<Self> {
        Ok(Self::from_graph(connect(uri, user, password).await?))
    }

    /// The connection the store uses.
    pub fn graph(&self) -> &Graph {
        &self.graph
    }

    /// Creates the constraints and indexes the store relies on; one that exists is kept.
    ///
    /// # Errors
    ///
    /// Returns [`KernelError::Store`] when a schema statement fails.
    pub async fn migrate(&self) -> Result<()> {
        let graph = self.graph.clone();
        detached(async move {
            run_schema(
                &graph,
                &[
                    "CREATE CONSTRAINT adk_kernel_process_pid IF NOT EXISTS \
                     FOR (p:AdkKernelProcess) REQUIRE p.pid IS UNIQUE",
                    "CREATE INDEX adk_kernel_process_order IF NOT EXISTS \
                     FOR (p:AdkKernelProcess) ON (p.order_key)",
                    "CREATE INDEX adk_kernel_process_partition IF NOT EXISTS \
                     FOR (p:AdkKernelProcess) ON (p.partition)",
                    "CREATE INDEX adk_kernel_process_queue IF NOT EXISTS \
                     FOR (p:AdkKernelProcess) ON (p.queue)",
                    "CREATE INDEX adk_kernel_process_parent IF NOT EXISTS \
                     FOR (p:AdkKernelProcess) ON (p.parent)",
                    "CREATE CONSTRAINT adk_kernel_spend_key IF NOT EXISTS \
                     FOR (s:AdkKernelSpend) REQUIRE s.key IS UNIQUE",
                    "CREATE INDEX adk_kernel_spend_pid IF NOT EXISTS \
                     FOR (s:AdkKernelSpend) ON (s.pid)",
                    "CREATE INDEX adk_kernel_spend_partition IF NOT EXISTS \
                     FOR (s:AdkKernelSpend) ON (s.partition)",
                    "CREATE CONSTRAINT adk_kernel_usage_key IF NOT EXISTS \
                     FOR (u:AdkKernelUsage) REQUIRE u.key IS UNIQUE",
                    "CREATE INDEX adk_kernel_usage_pid IF NOT EXISTS \
                     FOR (u:AdkKernelUsage) ON (u.pid)",
                    "CREATE INDEX adk_kernel_usage_partition IF NOT EXISTS \
                     FOR (u:AdkKernelUsage) ON (u.partition)",
                ],
            )
            .await
        })
        .await
    }

    /// Runs `update`, which returns `matched`, on the row of `pid`, failing when there is
    /// none.
    async fn update(&self, pid: &str, update: Query) -> Result<()> {
        let graph = self.graph.clone();
        let matched = detached(async move {
            count(&rows(&graph, update, "update the process table").await?, "matched")
        })
        .await?;
        if matched == 0 {
            return Err(KernelError::NotFound { pid: pid.into() });
        }
        Ok(())
    }
}

/// Reads a row back into the [`ProcessMeta`] that was put, or `None` for a node a concurrent
/// `delete` removed after the statement matched it: Neo4j reads committed data, so its
/// properties then read as null, as if the read had come after the delete.
fn decode(row: &Row) -> Result<Option<ProcessMeta>> {
    let optional = |name: &str| column::<Option<String>>(row, name);
    let (Some(pid), Some(partition), Some(queue), Some(kind), Some(labels)) = (
        optional("pid")?,
        optional("partition")?,
        optional("queue")?,
        optional("kind")?,
        optional("labels")?,
    ) else {
        return Ok(None);
    };
    Ok(Some(ProcessMeta {
        pid,
        parent: optional("parent")?,
        partition: PartitionId::new(partition),
        queue: QueueClass::from_name(&queue),
        kind: json("process kind", &kind)?,
        wake: optional("wake")?.map(|wake| json("wake set", &wake)).transpose()?,
        suspended_by: optional("suspended_by")?
            .map(|suspension| json("suspension", &suspension))
            .transpose()?,
        suspend_requested: column::<Option<bool>>(row, "suspend_requested")?.unwrap_or(false),
        agenda_item: optional("agenda_item")?,
        labels: json("label set", &labels)?,
    }))
}

#[async_trait]
impl ProcessStore for Neo4jProcessStore {
    async fn put(&self, meta: ProcessMeta) -> Result<()> {
        let statement = query(PUT)
            .param("pid", meta.pid.as_str())
            .param("order_key", order_key(&meta.pid))
            .param("partition", meta.partition.as_str())
            .param("queue", meta.queue.as_str())
            .param("kind", serde_json::to_string(&meta.kind)?)
            .param("labels", serde_json::to_string(&meta.labels)?)
            .param("suspend_requested", meta.suspend_requested)
            .param("parent", meta.parent.clone())
            .param("wake", meta.wake.as_ref().map(serde_json::to_string).transpose()?)
            .param(
                "suspended_by",
                meta.suspended_by.as_ref().map(serde_json::to_string).transpose()?,
            )
            .param("agenda_item", meta.agenda_item.clone());
        let graph = self.graph.clone();
        detached(async move {
            rows(&graph, statement, "write the process table").await?;
            Ok(())
        })
        .await
    }

    async fn get(&self, pid: &str) -> Result<Option<ProcessMeta>> {
        let statement =
            query(&format!("MATCH (p:AdkKernelProcess {{pid: $pid}}) {COLUMNS}")).param("pid", pid);
        let graph = self.graph.clone();
        detached(async move {
            Ok(match rows(&graph, statement, "read the process table").await?.first() {
                Some(row) => decode(row)?,
                None => None,
            })
        })
        .await
    }

    async fn list(&self, filter: &ProcessFilter) -> Result<Vec<ProcessMeta>> {
        let mut conditions = Vec::new();
        if filter.partition.is_some() {
            conditions.push("p.partition = $partition");
        }
        if filter.queue.is_some() {
            conditions.push("p.queue = $queue");
        }
        if filter.parent.is_some() {
            conditions.push("p.parent = $parent");
        }
        if filter.suspend_requested.is_some() {
            conditions.push("p.suspend_requested = $suspend_requested");
        }
        let (limit, limit_value) = limit_clause(filter.limit);
        let statement = query(&format!(
            "MATCH (p:AdkKernelProcess){} {COLUMNS} ORDER BY p.order_key{limit}",
            where_clause(&conditions)
        ))
        .param("partition", filter.partition.as_ref().map(|partition| partition.as_str()))
        .param("queue", filter.queue.as_ref().map(|queue| queue.as_str()))
        .param("parent", filter.parent.clone())
        .param("suspend_requested", filter.suspend_requested)
        .param("limit", limit_value);
        let graph = self.graph.clone();
        detached(async move {
            let rows = rows(&graph, statement, "list processes").await?;
            rows.iter().filter_map(|row| decode(row).transpose()).collect()
        })
        .await
    }

    async fn set_wake(&self, pid: &str, wake: Option<WakeSet>) -> Result<()> {
        let statement = query(
            "MATCH (p:AdkKernelProcess {pid: $pid}) SET p.wake = $wake \
             RETURN count(p) AS matched",
        )
        .param("pid", pid)
        .param("wake", wake.as_ref().map(serde_json::to_string).transpose()?);
        self.update(pid, statement).await
    }

    async fn set_queue(&self, pid: &str, queue: QueueClass) -> Result<()> {
        let statement = query(
            "MATCH (p:AdkKernelProcess {pid: $pid}) SET p.queue = $queue \
             RETURN count(p) AS matched",
        )
        .param("pid", pid)
        .param("queue", queue.as_str());
        self.update(pid, statement).await
    }

    async fn delete(&self, pid: &str) -> Result<bool> {
        let statement =
            query("MATCH (p:AdkKernelProcess {pid: $pid}) DELETE p RETURN count(*) AS deleted")
                .param("pid", pid);
        let graph = self.graph.clone();
        detached(async move {
            Ok(count(&rows(&graph, statement, "delete from the process table").await?, "deleted")?
                > 0)
        })
        .await
    }

    async fn set_suspend_requested(&self, pid: &str, requested: bool) -> Result<()> {
        let statement = query(
            "MATCH (p:AdkKernelProcess {pid: $pid}) SET p.suspend_requested = $requested \
             RETURN count(p) AS matched",
        )
        .param("pid", pid)
        .param("requested", requested);
        self.update(pid, statement).await
    }

    async fn add_spend(
        &self,
        pid: &str,
        partition: &PartitionId,
        day: NaiveDate,
        delta: SpendDelta,
    ) -> Result<()> {
        let day = day_number(day);
        let mut names = Vec::new();
        let mut amounts = Vec::new();
        for (name, amount) in counters(&delta) {
            names.push(name.to_string());
            amounts.push(stored_amount(BACKEND, name, amount)?);
        }
        let statement = query(ADD_SPEND)
            .param("key", format!("{}:{}:{day}", order_key(pid), order_key(partition.as_str())))
            .param("pid", pid)
            .param("partition", partition.as_str())
            .param("day", day)
            .param("amount", stored_micro_usd(BACKEND, &delta)?)
            .param("names", names)
            .param("amounts", amounts);
        let graph = self.graph.clone();
        detached(async move {
            rows(&graph, statement, "add to the spend totals").await?;
            Ok(())
        })
        .await
    }

    async fn spend(
        &self,
        scope: &SpendScope,
        from: NaiveDate,
        to_inclusive: NaiveDate,
    ) -> Result<u64> {
        let (field, key) = match scope {
            SpendScope::Process(pid) => ("pid", pid.as_str()),
            SpendScope::Partition(partition) => ("partition", partition.as_str()),
        };
        let statement = query(&format!(
            "MATCH (s:AdkKernelSpend) WHERE s.{field} = $key AND s.day >= $from AND s.day <= $to \
             RETURN coalesce(sum(s.micro_usd), 0) AS total"
        ))
        .param("key", key)
        .param("from", day_number(from))
        .param("to", day_number(to_inclusive));
        let graph = self.graph.clone();
        let total = detached(async move {
            count(&rows(&graph, statement, "sum the spend totals").await?, "total")
        })
        .await?;
        stored_total(BACKEND, total)
    }

    async fn usage(
        &self,
        scope: &SpendScope,
        from: NaiveDate,
        to_inclusive: NaiveDate,
    ) -> Result<UsageTotals> {
        let (field, key) = match scope {
            SpendScope::Process(pid) => ("pid", pid.clone()),
            SpendScope::Partition(partition) => ("partition", partition.as_str().to_string()),
        };
        let statement = query(&format!(
            "MATCH (u:AdkKernelUsage) WHERE u.{field} = $key AND u.day >= $from AND u.day <= $to \
             RETURN u.counter AS counter, sum(u.amount) AS total"
        ))
        .param("key", key)
        .param("from", day_number(from))
        .param("to", day_number(to_inclusive));
        let mut totals = UsageTotals {
            micro_usd: self.spend(scope, from, to_inclusive).await?,
            ..UsageTotals::default()
        };
        let graph = self.graph.clone();
        let counters =
            detached(async move { rows(&graph, statement, "sum the usage totals").await }).await?;
        for row in &counters {
            let name: String = column(row, "counter")?;
            let amount: i64 = column(row, "total")?;
            totals.add_counter(&name, stored_total(BACKEND, amount)?);
        }
        Ok(totals)
    }
}