aion-store-libsql 0.9.1

Durable libSQL-backed event store implementation for Aion workflows.
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
//! Durable minted-on-use namespace registry over libSQL (Control-Plane Phase 1).
//!
//! The single-node libSQL backend satisfies the [`NamespaceStore`](aion_store::NamespaceStore)
//! contract with a plain local upsert — there is no quorum to reach. Records are
//! stored as opaque [`NamespaceRecord`](aion_store::NamespaceRecord) codec bytes
//! in a `record` BLOB (the store's truth, decoded only to satisfy `list`/`get`);
//! `created_at`/`last_seen` are projected into RFC 3339 text columns so the list
//! ordering is index-served and the staleness signal is human-legible.

use aion_store::{
    MintOutcome, NamespaceOrigin, NamespacePlacement, NamespaceRecord, NamespaceState, StoreError,
};
use chrono::{SecondsFormat, Utc};
use libsql::{Connection, Row, TransactionBehavior};

const SELECT_RECORD_SQL: &str = "SELECT record FROM namespaces WHERE name = ?1";

const INSERT_IF_ABSENT_SQL: &str = "
INSERT OR IGNORE INTO namespaces (name, created_at, last_seen, record)
VALUES (?1, ?2, ?3, ?4)";

const TOUCH_LAST_SEEN_SQL: &str = "
UPDATE namespaces SET last_seen = ?2, record = ?3 WHERE name = ?1";

const LIST_RECORDS_SQL: &str = "
SELECT record FROM namespaces ORDER BY created_at ASC, name ASC";

const DEPRECATE_SQL: &str = "UPDATE namespaces SET record = ?2 WHERE name = ?1";

/// Idempotent minted-on-use upsert: create-if-absent else bump `last_seen`.
///
/// A fresh `name` is minted with `origin`; an existing one has its `last_seen`
/// refreshed (origin and `created_at` left untouched). Runs under one
/// `IMMEDIATE` transaction so the read-then-write is atomic against a concurrent
/// minter on the same single-node connection.
///
/// # Errors
///
/// Returns `StoreError::Serialization` when the record cannot be encoded and
/// `StoreError::Backend` for libSQL boundary failures.
pub(crate) async fn register_namespace(
    conn: &Connection,
    name: &str,
    origin: NamespaceOrigin,
) -> Result<MintOutcome, StoreError> {
    let now = Utc::now();
    let tx = conn
        .transaction_with_behavior(TransactionBehavior::Immediate)
        .await
        .map_err(|error| crate::error::libsql_error(&error))?;

    let outcome = match load_record(&tx, name).await {
        Ok(Some(mut existing)) => {
            existing.bump_last_seen(now);
            let bytes = existing.encode()?;
            match touch_last_seen(&tx, name, now, &bytes).await {
                Ok(()) => Ok(MintOutcome::AlreadyExisted),
                Err(error) => Err(error),
            }
        }
        Ok(None) => {
            let record = NamespaceRecord::new_minted(name, origin, now);
            match insert_record(&tx, &record).await {
                Ok(()) => Ok(MintOutcome::Created),
                Err(error) => Err(error),
            }
        }
        Err(error) => Err(error),
    };

    finish(tx, outcome).await
}

/// Explicit upsert carrying a caller-supplied record. Idempotent on an existing
/// name: a present row is reconciled as `AlreadyExisted` rather than overwritten.
///
/// # Errors
///
/// Returns `StoreError::Serialization` when the record cannot be encoded and
/// `StoreError::Backend` for libSQL boundary failures.
pub(crate) async fn put_namespace(
    conn: &Connection,
    record: NamespaceRecord,
) -> Result<MintOutcome, StoreError> {
    let now = Utc::now();
    let tx = conn
        .transaction_with_behavior(TransactionBehavior::Immediate)
        .await
        .map_err(|error| crate::error::libsql_error(&error))?;

    let outcome = match load_record(&tx, &record.name).await {
        Ok(Some(mut existing)) => {
            // Idempotent on an existing name: refresh the staleness signal but
            // never overwrite the durable record body wholesale.
            existing.bump_last_seen(now);
            let bytes = match existing.encode() {
                Ok(bytes) => bytes,
                Err(error) => return finish(tx, Err(error)).await,
            };
            match touch_last_seen(&tx, &existing.name, now, &bytes).await {
                Ok(()) => Ok(MintOutcome::AlreadyExisted),
                Err(error) => Err(error),
            }
        }
        Ok(None) => match insert_record(&tx, &record).await {
            Ok(()) => Ok(MintOutcome::Created),
            Err(error) => Err(error),
        },
        Err(error) => Err(error),
    };

    finish(tx, outcome).await
}

/// Return the live durable set, ascending `created_at` (ties broken by `name`).
///
/// # Errors
///
/// Returns `StoreError::Backend` for libSQL read failures and
/// `StoreError::Serialization` when a persisted record cannot be decoded.
pub(crate) async fn list_namespaces(conn: &Connection) -> Result<Vec<NamespaceRecord>, StoreError> {
    let mut rows = conn
        .query(LIST_RECORDS_SQL, ())
        .await
        .map_err(|error| crate::error::libsql_error(&error))?;

    let mut records = Vec::new();
    while let Some(row) = rows
        .next()
        .await
        .map_err(|error| crate::error::libsql_error(&error))?
    {
        records.push(decode_record(&row)?);
    }

    Ok(records)
}

/// Look up a single namespace by `name`; an absent name returns `None`.
///
/// # Errors
///
/// Returns `StoreError::Backend` for libSQL read failures and
/// `StoreError::Serialization` when the persisted record cannot be decoded.
pub(crate) async fn get_namespace(
    conn: &Connection,
    name: &str,
) -> Result<Option<NamespaceRecord>, StoreError> {
    let mut rows = conn
        .query(SELECT_RECORD_SQL, libsql::params![name])
        .await
        .map_err(|error| crate::error::libsql_error(&error))?;
    let Some(row) = rows
        .next()
        .await
        .map_err(|error| crate::error::libsql_error(&error))?
    else {
        return Ok(None);
    };
    Ok(Some(decode_record(&row)?))
}

/// Transition `name` to [`NamespaceState::Deprecated`]; absent or already-
/// deprecated rows are an idempotent no-op.
///
/// # Errors
///
/// Returns `StoreError::Serialization` when the record cannot be re-encoded and
/// `StoreError::Backend` for libSQL boundary failures.
pub(crate) async fn deprecate_namespace(conn: &Connection, name: &str) -> Result<(), StoreError> {
    let tx = conn
        .transaction_with_behavior(TransactionBehavior::Immediate)
        .await
        .map_err(|error| crate::error::libsql_error(&error))?;

    let outcome = match load_record(&tx, name).await {
        Ok(Some(mut existing)) => {
            existing.state = NamespaceState::Deprecated;
            match existing.encode() {
                Ok(bytes) => tx
                    .execute(DEPRECATE_SQL, libsql::params![name, bytes])
                    .await
                    .map(|_| ())
                    .map_err(|error| crate::error::libsql_error(&error)),
                Err(error) => Err(error),
            }
        }
        // A missing row is an idempotent no-op: deprecation never strands
        // durable history and an absent registry entry is not an error.
        Ok(None) => Ok(()),
        Err(error) => Err(error),
    };

    finish(tx, outcome).await
}

/// Set an existing namespace's `placement` directive, bumping `last_seen`
/// (Control-Plane Phase 2, P2-P2). Only `placement` and `last_seen` change; the
/// record body's other fields are preserved. An absent row returns `Ok(None)` —
/// placement targets an already-minted namespace, never minting a row here. A
/// successful update returns `Ok(Some(()))`. Runs under one `IMMEDIATE`
/// transaction so the read-modify-write is atomic against a concurrent mutator on
/// the same single-node connection.
///
/// # Errors
///
/// Returns `StoreError::Serialization` when the record cannot be re-encoded and
/// `StoreError::Backend` for libSQL boundary failures.
pub(crate) async fn set_namespace_placement(
    conn: &Connection,
    name: &str,
    placement: NamespacePlacement,
) -> Result<Option<()>, StoreError> {
    let now = Utc::now();
    let tx = conn
        .transaction_with_behavior(TransactionBehavior::Immediate)
        .await
        .map_err(|error| crate::error::libsql_error(&error))?;

    let outcome = match load_record(&tx, name).await {
        Ok(Some(mut existing)) => {
            existing.placement = placement;
            existing.bump_last_seen(now);
            match existing.encode() {
                Ok(bytes) => touch_last_seen(&tx, name, now, &bytes)
                    .await
                    .map(|()| Some(())),
                Err(error) => Err(error),
            }
        }
        // An absent registry row is a not-found the caller surfaces, never a
        // silent mint: placement targets an already-minted namespace.
        Ok(None) => Ok(None),
        Err(error) => Err(error),
    };

    finish(tx, outcome).await
}

async fn load_record(
    tx: &libsql::Transaction,
    name: &str,
) -> Result<Option<NamespaceRecord>, StoreError> {
    let mut rows = tx
        .query(SELECT_RECORD_SQL, libsql::params![name])
        .await
        .map_err(|error| crate::error::libsql_error(&error))?;
    let Some(row) = rows
        .next()
        .await
        .map_err(|error| crate::error::libsql_error(&error))?
    else {
        return Ok(None);
    };
    Ok(Some(decode_record(&row)?))
}

async fn insert_record(
    tx: &libsql::Transaction,
    record: &NamespaceRecord,
) -> Result<(), StoreError> {
    let bytes = record.encode()?;
    tx.execute(
        INSERT_IF_ABSENT_SQL,
        libsql::params![
            record.name.clone(),
            encode_instant(record.created_at),
            encode_instant(record.last_seen),
            bytes
        ],
    )
    .await
    .map(|_| ())
    .map_err(|error| crate::error::libsql_error(&error))
}

async fn touch_last_seen(
    tx: &libsql::Transaction,
    name: &str,
    last_seen: chrono::DateTime<Utc>,
    record: &[u8],
) -> Result<(), StoreError> {
    tx.execute(
        TOUCH_LAST_SEEN_SQL,
        libsql::params![name, encode_instant(last_seen), record.to_vec()],
    )
    .await
    .map(|_| ())
    .map_err(|error| crate::error::libsql_error(&error))
}

async fn finish<T>(
    tx: libsql::Transaction,
    outcome: Result<T, StoreError>,
) -> Result<T, StoreError> {
    match outcome {
        Ok(value) => tx
            .commit()
            .await
            .map(|()| value)
            .map_err(|error| crate::error::libsql_error(&error)),
        Err(error) => {
            tx.rollback()
                .await
                .map_err(|rollback_error| crate::error::libsql_error(&rollback_error))?;
            Err(error)
        }
    }
}

fn decode_record(row: &Row) -> Result<NamespaceRecord, StoreError> {
    let bytes: Vec<u8> = row
        .get(0)
        .map_err(|error| crate::error::libsql_error(&error))?;
    NamespaceRecord::decode(&bytes)
}

fn encode_instant(instant: chrono::DateTime<Utc>) -> String {
    instant.to_rfc3339_opts(SecondsFormat::Nanos, true)
}

#[cfg(test)]
mod tests {
    #![allow(clippy::expect_used)]

    use std::path::PathBuf;
    use std::time::{SystemTime, UNIX_EPOCH};

    use aion_store::{MintOutcome, NamespaceOrigin, NamespaceRecord, NamespaceState, StoreError};
    use chrono::{TimeZone, Utc};

    use crate::config::{LibSqlConfig, LibSqlMode};

    async fn open_test_connection(name: &str) -> Result<libsql::Connection, StoreError> {
        let config = LibSqlConfig {
            mode: LibSqlMode::Embedded {
                path: unique_temp_path(name),
            },
            journal_mode: None,
            synchronous: None,
            sync_interval_seconds: None,
        };
        let conn = crate::connection::open_connection(&config)
            .await?
            .connection;
        crate::schema::ensure_schema(&conn).await?;
        Ok(conn)
    }

    fn unique_temp_path(name: &str) -> PathBuf {
        let nanos = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map_or(0, |duration| duration.as_nanos());
        std::env::temp_dir().join(format!(
            "aion-store-libsql-namespace-{name}-{}-{nanos}.db",
            std::process::id()
        ))
    }

    #[tokio::test]
    async fn register_creates_if_absent_and_persists() -> Result<(), StoreError> {
        let conn = open_test_connection("create").await?;

        let outcome =
            super::register_namespace(&conn, "orders", NamespaceOrigin::WorkerMint).await?;
        assert_eq!(outcome, MintOutcome::Created);

        let record = super::get_namespace(&conn, "orders")
            .await?
            .expect("namespace must persist");
        assert_eq!(record.name, "orders");
        assert_eq!(record.origin, NamespaceOrigin::WorkerMint);
        assert_eq!(record.state, NamespaceState::Active);
        assert_eq!(record.created_at, record.last_seen);
        Ok(())
    }

    #[tokio::test]
    async fn second_register_already_existed_bumps_last_seen_only() -> Result<(), StoreError> {
        let conn = open_test_connection("touch").await?;

        let first = super::register_namespace(&conn, "orders", NamespaceOrigin::WorkerMint).await?;
        assert_eq!(first, MintOutcome::Created);
        let original = super::get_namespace(&conn, "orders")
            .await?
            .expect("namespace must persist");

        // A different origin on re-register must NOT overwrite the recorded origin.
        let second = super::register_namespace(&conn, "orders", NamespaceOrigin::Explicit).await?;
        assert_eq!(second, MintOutcome::AlreadyExisted);

        let touched = super::get_namespace(&conn, "orders")
            .await?
            .expect("namespace must persist");
        assert_eq!(touched.created_at, original.created_at);
        assert_eq!(touched.origin, NamespaceOrigin::WorkerMint);
        assert!(touched.last_seen >= original.last_seen);
        Ok(())
    }

    #[tokio::test]
    async fn put_namespace_is_idempotent_on_existing_name() -> Result<(), StoreError> {
        let conn = open_test_connection("put-idempotent").await?;
        let now = Utc
            .with_ymd_and_hms(2026, 6, 30, 12, 0, 0)
            .single()
            .expect("valid instant");

        let mut record = NamespaceRecord::new_minted("billing", NamespaceOrigin::Explicit, now);
        record.config.kind = Some("tenant".to_owned());
        assert_eq!(
            super::put_namespace(&conn, record).await?,
            MintOutcome::Created
        );

        // A second put with a DIFFERENT body must reconcile as AlreadyExisted
        // and must not overwrite the stored record wholesale.
        let mut replacement =
            NamespaceRecord::new_minted("billing", NamespaceOrigin::WorkerMint, now);
        replacement.config.kind = None;
        assert_eq!(
            super::put_namespace(&conn, replacement).await?,
            MintOutcome::AlreadyExisted
        );

        let stored = super::get_namespace(&conn, "billing")
            .await?
            .expect("namespace must persist");
        assert_eq!(stored.origin, NamespaceOrigin::Explicit);
        assert_eq!(stored.config.kind.as_deref(), Some("tenant"));
        Ok(())
    }

    #[tokio::test]
    async fn list_orders_by_created_at_then_name() -> Result<(), StoreError> {
        let conn = open_test_connection("list-order").await?;
        let earlier = Utc
            .with_ymd_and_hms(2026, 6, 30, 12, 0, 0)
            .single()
            .expect("valid instant");
        let later = Utc
            .with_ymd_and_hms(2026, 6, 30, 13, 0, 0)
            .single()
            .expect("valid instant");

        super::put_namespace(
            &conn,
            NamespaceRecord::new_minted("zeta", NamespaceOrigin::Explicit, earlier),
        )
        .await?;
        super::put_namespace(
            &conn,
            NamespaceRecord::new_minted("alpha", NamespaceOrigin::Explicit, earlier),
        )
        .await?;
        super::put_namespace(
            &conn,
            NamespaceRecord::new_minted("beta", NamespaceOrigin::Explicit, later),
        )
        .await?;

        let listed: Vec<String> = super::list_namespaces(&conn)
            .await?
            .into_iter()
            .map(|record| record.name)
            .collect();
        assert_eq!(listed, vec!["alpha", "zeta", "beta"]);
        Ok(())
    }

    #[tokio::test]
    async fn get_returns_none_for_absent_name() -> Result<(), StoreError> {
        let conn = open_test_connection("get-miss").await?;
        assert!(super::get_namespace(&conn, "missing").await?.is_none());
        Ok(())
    }

    /// `set_namespace_placement` durably updates the placement of an existing
    /// record (preserving its other fields), is idempotent, and is a not-found
    /// (`Ok(None)`) for an absent namespace rather than a silent mint.
    #[tokio::test]
    async fn set_placement_updates_existing_and_is_not_found_when_absent() -> Result<(), StoreError>
    {
        use aion_store::NamespacePlacement;
        use std::collections::BTreeSet;

        let conn = open_test_connection("set-placement").await?;
        super::register_namespace(&conn, "orders", NamespaceOrigin::Explicit).await?;
        let original = super::get_namespace(&conn, "orders")
            .await?
            .expect("namespace must persist");
        assert_eq!(original.placement, NamespacePlacement::Unplaced);

        let nodes: BTreeSet<String> = ["n1".to_owned()].into_iter().collect();
        let placement = NamespacePlacement::Prefer {
            nodes: nodes.clone(),
        };
        assert_eq!(
            super::set_namespace_placement(&conn, "orders", placement.clone()).await?,
            Some(())
        );
        let updated = super::get_namespace(&conn, "orders")
            .await?
            .expect("namespace must persist");
        assert_eq!(updated.placement, placement);
        assert_eq!(updated.origin, NamespaceOrigin::Explicit);
        assert_eq!(updated.created_at, original.created_at);

        // Idempotent re-apply.
        assert_eq!(
            super::set_namespace_placement(&conn, "orders", placement).await?,
            Some(())
        );

        // Absent namespace: not-found, nothing minted.
        assert_eq!(
            super::set_namespace_placement(&conn, "ghost", NamespacePlacement::Unplaced).await?,
            None
        );
        assert!(super::get_namespace(&conn, "ghost").await?.is_none());
        Ok(())
    }

    #[tokio::test]
    async fn deprecate_sets_state_and_is_idempotent() -> Result<(), StoreError> {
        let conn = open_test_connection("deprecate").await?;
        super::register_namespace(&conn, "orders", NamespaceOrigin::WorkerMint).await?;

        super::deprecate_namespace(&conn, "orders").await?;
        let deprecated = super::get_namespace(&conn, "orders")
            .await?
            .expect("namespace must persist");
        assert_eq!(deprecated.state, NamespaceState::Deprecated);

        // Deprecating again is a no-op, not an error.
        super::deprecate_namespace(&conn, "orders").await?;
        let still = super::get_namespace(&conn, "orders")
            .await?
            .expect("namespace must persist");
        assert_eq!(still.state, NamespaceState::Deprecated);

        // Deprecating an absent namespace is an idempotent no-op.
        super::deprecate_namespace(&conn, "never-seen").await?;
        assert!(super::get_namespace(&conn, "never-seen").await?.is_none());
        Ok(())
    }
}