agentplane 0.39.0

Durable, replayable agent runtime — the journal is the plan of record
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
//! Batch state on redb.

use async_trait::async_trait;
use redb::{ReadableDatabase, ReadableTable, TableDefinition};

use crate::batch::{BatchCensus, BatchStore, ItemOutcome, ItemRecord};
use crate::core::{BatchId, RunId, Spend, StoreError};

use super::redb::{MAX_STR, RedbStore, be, begin_write, decoded};

/// `batch_id -> (plan_digest, exhausted)`.
const BATCHES: TableDefinition<(&str, &str), (&str, u8)> = TableDefinition::new("batches");

/// `(batch_id, item_key) -> (run_id, outcome, has_outcome, detail, tokens, minor)`.
///
/// The row exists from the moment the item is reserved, which is what makes an
/// interrupted item findable: a crash leaves no outcome beside a run id that can
/// be replayed.
/// `(run_id, outcome, has_outcome, detail, tokens, minor)`.
type ItemRow<'a> = (&'a str, &'a str, u8, &'a str, u64, u64);

const ITEMS: TableDefinition<(&str, &str, &str), ItemRow<'static>> =
    TableDefinition::new("batch_items");

pub(super) fn create_tables(w: &redb::WriteTransaction) -> Result<(), StoreError> {
    w.open_table(BATCHES).map_err(|e| be(&e))?;
    w.open_table(ITEMS).map_err(|e| be(&e))?;
    Ok(())
}

fn outcome_to_row(o: &ItemOutcome) -> (&'static str, String) {
    let detail = match o {
        ItemOutcome::Succeeded => String::new(),
        ItemOutcome::Failed(d)
        | ItemOutcome::Quarantined(d)
        | ItemOutcome::Suspended(d)
        | ItemOutcome::Exhausted(d) => d.clone(),
    };
    (o.as_str(), detail)
}

/// `None` means *no outcome yet* — and only that. An outcome string this
/// store cannot read is damage, not absence: decoded to `None` it would say
/// the item never ran, the census would carry it as in-flight forever, and
/// the batch would report `Running` over a row nobody can explain.
fn outcome_from_row(has: u8, s: &str, detail: &str) -> Result<Option<ItemOutcome>, StoreError> {
    if has != 1 {
        return Ok(None);
    }
    decoded("item outcome", s, ItemOutcome::parse(s, detail.to_owned())).map(Some)
}

/// Whether an item still needs work. A suspended item is *not* terminal — it is
/// waiting, and a resume that steps over it reports the batch complete while it
/// is not.
fn is_open(has_outcome: u8, outcome: &str) -> bool {
    has_outcome != 1 || ItemOutcome::parse(outcome, String::new()).is_none_or(|o| !o.is_terminal())
}

#[async_trait]
impl BatchStore for RedbStore {
    fn tenant(&self) -> &str {
        crate::journal::JournalStore::tenant(self)
    }

    async fn open(&self, id: BatchId, plan_digest: &str) -> Result<(), StoreError> {
        let tenant = self.tenant_name();
        let (key, digest) = (id.to_string(), plan_digest.to_owned());
        self.with_db(move |db| {
            let w = begin_write(db)?;
            {
                let mut t = w.open_table(BATCHES).map_err(|e| be(&e))?;
                let stored = t
                    .get((tenant.as_str(), key.as_str()))
                    .map_err(|e| be(&e))?
                    .map(|v| v.value().0.to_owned());
                match stored {
                    None => {
                        t.insert((tenant.as_str(), key.as_str()), (digest.as_str(), 0u8))
                            .map_err(|e| be(&e))?;
                    }
                    Some(stored) if stored == digest => {}
                    // One batch runs one frozen plan; a resume offering an
                    // edited one is a second act wearing this batch's name.
                    Some(stored) => {
                        return Err(StoreError::BatchPlanChanged {
                            batch: key.clone(),
                            stored,
                            offered: digest.clone(),
                        });
                    }
                }
            }
            w.commit().map_err(|e| be(&e))?;
            Ok(())
        })
        .await
    }

    async fn plan_digest(&self, id: BatchId) -> Result<Option<String>, StoreError> {
        let tenant = self.tenant_name();
        let key = id.to_string();
        self.with_db(move |db| {
            let r = db.begin_read().map_err(|e| be(&e))?;
            let Ok(t) = r.open_table(BATCHES) else {
                return Ok(None);
            };
            Ok(t.get((tenant.as_str(), key.as_str()))
                .map_err(|e| be(&e))?
                .map(|v| v.value().0.to_owned()))
        })
        .await
    }

    async fn mark_exhausted(&self, id: BatchId) -> Result<(), StoreError> {
        let tenant = self.tenant_name();
        let key = id.to_string();
        self.with_db(move |db| {
            let w = begin_write(db)?;
            {
                let mut t = w.open_table(BATCHES).map_err(|e| be(&e))?;
                let digest = t
                    .get((tenant.as_str(), key.as_str()))
                    .map_err(|e| be(&e))?
                    .map(|v| v.value().0.to_owned());
                // A mark on an unknown batch is a refusal, not a no-op: this
                // is the one bit that lets a census read as *finished*, and
                // `Ok` over nothing is the quietest way to lose it.
                let Some(digest) = digest else {
                    return Err(StoreError::NotFound(key.clone()));
                };
                t.insert((tenant.as_str(), key.as_str()), (digest.as_str(), 1u8))
                    .map_err(|e| be(&e))?;
            }
            w.commit().map_err(|e| be(&e))?;
            Ok(())
        })
        .await
    }

    async fn is_exhausted(&self, id: BatchId) -> Result<bool, StoreError> {
        let tenant = self.tenant_name();
        let key = id.to_string();
        self.with_db(move |db| {
            let r = db.begin_read().map_err(|e| be(&e))?;
            let t = r.open_table(BATCHES).map_err(|e| be(&e))?;
            Ok(t.get((tenant.as_str(), key.as_str()))
                .map_err(|e| be(&e))?
                .is_some_and(|v| v.value().1 == 1))
        })
        .await
    }

    async fn reserve(
        &self,
        batch: BatchId,
        key: &str,
        run: RunId,
    ) -> Result<ItemRecord, StoreError> {
        let tenant = self.tenant_name();
        let (batch_key, item, run_id) = (batch.to_string(), key.to_owned(), run.to_string());
        self.with_db(move |db| {
            let w = begin_write(db)?;
            let out = {
                let mut t = w.open_table(ITEMS).map_err(|e| be(&e))?;
                // Reserve then read back, rather than overwrite: if this item was
                // already reserved the caller must get the *original* run id.
                // Overwriting would orphan the first run's journal and re-perform
                // its effects, which is the one thing this row exists to prevent.
                if t.get((tenant.as_str(), batch_key.as_str(), item.as_str()))
                    .map_err(|e| be(&e))?
                    .is_none()
                {
                    t.insert(
                        (tenant.as_str(), batch_key.as_str(), item.as_str()),
                        (run_id.as_str(), "", 0u8, "", 0u64, 0u64),
                    )
                    .map_err(|e| be(&e))?;
                }
                let row = t
                    .get((tenant.as_str(), batch_key.as_str(), item.as_str()))
                    .map_err(|e| be(&e))?
                    .map(|v| {
                        let (run, oc, has, detail, tokens, minor) = v.value();
                        (
                            run.to_owned(),
                            oc.to_owned(),
                            has,
                            detail.to_owned(),
                            tokens,
                            minor,
                        )
                    });
                let Some((run_s, outcome, has, detail, tokens, minor)) = row else {
                    return Err(StoreError::NotFound(format!("{batch_key}/{item}")));
                };
                ItemRecord {
                    key: item.clone(),
                    run: RunId::parse(&run_s).map_err(|e| StoreError::Corrupt {
                        seq: 0,
                        detail: format!("bad run id '{run_s}': {e}"),
                    })?,
                    outcome: outcome_from_row(has, &outcome, &detail)?,
                    spend: Spend {
                        tokens,
                        minor_units: minor,
                    },
                }
            };
            w.commit().map_err(|e| be(&e))?;
            Ok(out)
        })
        .await
    }

    async fn record(
        &self,
        batch: BatchId,
        key: &str,
        outcome: &ItemOutcome,
        spend: Spend,
    ) -> Result<(), StoreError> {
        let tenant = self.tenant_name();
        let (batch_key, item) = (batch.to_string(), key.to_owned());
        let (state, detail) = outcome_to_row(outcome);
        let tokens = spend.tokens;
        let minor = spend.minor_units;
        self.with_db(move |db| {
            let w = begin_write(db)?;
            {
                let mut t = w.open_table(ITEMS).map_err(|e| be(&e))?;
                let run = t
                    .get((tenant.as_str(), batch_key.as_str(), item.as_str()))
                    .map_err(|e| be(&e))?
                    .map(|v| v.value().0.to_owned());
                // An unreserved item is a refusal, not a no-op: returning `Ok`
                // while writing nothing tells the caller *recorded* over an
                // outcome that vanished.
                let Some(run) = run else {
                    return Err(StoreError::NotFound(format!("{batch_key}/{item}")));
                };
                t.insert(
                    (tenant.as_str(), batch_key.as_str(), item.as_str()),
                    (run.as_str(), state, 1u8, detail.as_str(), tokens, minor),
                )
                .map_err(|e| be(&e))?;
            }
            w.commit().map_err(|e| be(&e))?;
            Ok(())
        })
        .await
    }

    async fn cursor(&self, batch: BatchId) -> Result<Option<String>, StoreError> {
        let tenant = self.tenant_name();
        let batch_key = batch.to_string();
        self.with_db(move |db| {
            let r = db.begin_read().map_err(|e| be(&e))?;
            let t = r.open_table(ITEMS).map_err(|e| be(&e))?;
            // The contiguous terminal prefix: the last key before the first
            // item still needing work. Taking the greatest *terminal* key
            // instead would step over a suspended item sitting behind finished
            // ones, and a resume that skips a waiting item reports the batch
            // complete while it is not.
            let mut last_terminal: Option<String> = None;
            for e in t
                .range(
                    (tenant.as_str(), batch_key.as_str(), "")
                        ..=(tenant.as_str(), batch_key.as_str(), MAX_STR),
                )
                .map_err(|e| be(&e))?
            {
                let (k, v) = e.map_err(|e| be(&e))?;
                let (_, outcome, has, _, _, _) = v.value();
                if is_open(has, outcome) {
                    break;
                }
                last_terminal = Some(k.value().2.to_owned());
            }
            Ok(last_terminal)
        })
        .await
    }

    async fn census(&self, batch: BatchId) -> Result<BatchCensus, StoreError> {
        let tenant = self.tenant_name();
        let batch_key = batch.to_string();
        self.with_db(move |db| {
            let r = db.begin_read().map_err(|e| be(&e))?;
            let t = r.open_table(ITEMS).map_err(|e| be(&e))?;
            let mut c = BatchCensus::default();
            for e in t
                .range(
                    (tenant.as_str(), batch_key.as_str(), "")
                        ..=(tenant.as_str(), batch_key.as_str(), MAX_STR),
                )
                .map_err(|e| be(&e))?
            {
                let (_, v) = e.map_err(|e| be(&e))?;
                let (_, outcome, has, _, tokens, minor) = v.value();
                if has == 1 {
                    // Bucketed off the *parsed* outcome, so an outcome added
                    // later is a compiler error here rather than a row that
                    // lands in no bucket. Damage is not a bucket either: filed
                    // as in-flight it would keep the batch `Running` forever,
                    // silently.
                    let parsed = decoded(
                        "item outcome",
                        outcome,
                        ItemOutcome::parse(outcome, String::new()),
                    )?;
                    match parsed {
                        ItemOutcome::Succeeded => c.succeeded += 1,
                        ItemOutcome::Failed(_) => c.failed += 1,
                        ItemOutcome::Quarantined(_) => c.quarantined += 1,
                        ItemOutcome::Suspended(_) => c.suspended += 1,
                        ItemOutcome::Exhausted(_) => c.exhausted += 1,
                    }
                } else {
                    // Reserved, no outcome recorded.
                    c.in_flight += 1;
                }
                c.spend += Spend {
                    tokens,
                    minor_units: minor,
                };
            }
            Ok(c)
        })
        .await
    }

    async fn items(&self, batch: BatchId, limit: usize) -> Result<Vec<ItemRecord>, StoreError> {
        self.scan_items(batch, limit, false).await
    }

    async fn items_needing_attention(
        &self,
        batch: BatchId,
        limit: usize,
    ) -> Result<Vec<ItemRecord>, StoreError> {
        self.scan_items(batch, limit, true).await
    }
}

impl RedbStore {
    /// One range scan over a batch's items, optionally keeping only the ones
    /// nobody is finished with.
    ///
    /// `limit` counts what is **returned**, so an unsettled page is a page of
    /// unsettled items rather than whatever a limit on the scan happened to
    /// catch — the distinction is the whole listing when 99,957 of 100,000 rows
    /// succeeded.
    async fn scan_items(
        &self,
        batch: BatchId,
        limit: usize,
        unsettled_only: bool,
    ) -> Result<Vec<ItemRecord>, StoreError> {
        let tenant = self.tenant_name();
        let batch_key = batch.to_string();
        // Hoisted: the set is a property of the type, not of the row, and a
        // batch is 10⁵ rows.
        let settled = ItemOutcome::settled_tags();
        self.with_db(move |db| {
            let r = db.begin_read().map_err(|e| be(&e))?;
            let t = r.open_table(ITEMS).map_err(|e| be(&e))?;
            let mut out = Vec::new();
            for e in t
                .range(
                    (tenant.as_str(), batch_key.as_str(), "")
                        ..=(tenant.as_str(), batch_key.as_str(), MAX_STR),
                )
                .map_err(|e| be(&e))?
            {
                if out.len() >= limit {
                    break;
                }
                let (k, v) = e.map_err(|e| be(&e))?;
                let (run_s, outcome, has, detail, tokens, minor) = v.value();
                // The filter is the type's own vocabulary, not a literal: an
                // outcome added later is unsettled by construction, so a new
                // kind of trouble appears in this listing without anyone
                // remembering to widen a string here.
                if unsettled_only && has == 1 && settled.contains(&outcome) {
                    continue;
                }
                out.push(ItemRecord {
                    key: k.value().2.to_owned(),
                    run: RunId::parse(run_s).map_err(|e| StoreError::Corrupt {
                        seq: 0,
                        detail: format!("bad run id '{run_s}': {e}"),
                    })?,
                    outcome: outcome_from_row(has, outcome, detail)?,
                    spend: Spend {
                        tokens,
                        minor_units: minor,
                    },
                });
            }
            Ok(out)
        })
        .await
    }
}

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

    /// Every outcome this store writes is one it reads back as the same value.
    #[test]
    fn every_written_outcome_decodes_to_the_value_that_wrote_it() {
        for outcome in [
            ItemOutcome::Succeeded,
            ItemOutcome::Failed("x".into()),
            ItemOutcome::Quarantined("x".into()),
            ItemOutcome::Suspended("x".into()),
            ItemOutcome::Exhausted("x".into()),
        ] {
            let (state, detail) = outcome_to_row(&outcome);
            assert_eq!(
                outcome_from_row(1, state, &detail).expect("round trip"),
                Some(outcome)
            );
        }
        assert_eq!(
            outcome_from_row(0, "", "").expect("absence is not damage"),
            None
        );
    }

    /// **An outcome this store cannot read is damage, not absence.**
    ///
    /// Decoded to `None` it says the item never ran; the census then carries
    /// the row as in-flight forever and the batch reports `Running` over a
    /// row nobody can explain — a corrupt store wearing a healthy status.
    #[test]
    fn an_unreadable_item_outcome_is_refused_rather_than_defaulted() {
        for bad in ["", "Succeeded", "done", "succeeded "] {
            assert!(
                matches!(
                    outcome_from_row(1, bad, "").err(),
                    Some(StoreError::Corrupt { .. })
                ),
                "outcome '{bad}' decoded instead of refusing"
            );
        }
    }
}