bux 0.9.0

Embedded micro-VM sandbox for running AI agents
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
//! `SQLite` persistence layer for VM state.
//!
//! **Product schema only** — no migrations from pre-1.0 layouts.
//! On version mismatch the open fails; delete the data directory.

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

use rusqlite::{Connection, OptionalExtension, params};

use super::{Status, VmState};
use crate::error::{Error, Result};

/// Product state-schema version (PRAGMA `user_version`).
///
/// Bump only on incompatible schema changes. There is **no** migration
/// path — callers must wipe the data directory.
///
/// v5: drop `vms.health`.
pub(crate) const PRODUCT_SCHEMA_VERSION: u32 = 5;

/// DDL for a fresh product database.
const PRODUCT_SCHEMA_SQL: &str = "
CREATE TABLE vms (
    id          TEXT PRIMARY KEY NOT NULL,
    name        TEXT UNIQUE,
    pid         INTEGER NOT NULL,
    image       TEXT,
    socket      TEXT NOT NULL,
    status      TEXT NOT NULL DEFAULT 'running',
    config      TEXT NOT NULL,
    created_at  REAL NOT NULL,
    updated_at  REAL
);

CREATE TABLE snapshots (
    id          TEXT PRIMARY KEY NOT NULL,
    vm_id       TEXT NOT NULL REFERENCES vms(id) ON DELETE CASCADE,
    name        TEXT,
    disk_path   TEXT NOT NULL,
    disk_bytes  INTEGER NOT NULL DEFAULT 0,
    created_at  REAL NOT NULL,
    UNIQUE(vm_id, name)
);
CREATE INDEX idx_snapshots_vm ON snapshots(vm_id);

CREATE TABLE base_disks (
    id          TEXT PRIMARY KEY NOT NULL,
    digest      TEXT NOT NULL UNIQUE,
    path        TEXT NOT NULL,
    ref_count   INTEGER NOT NULL DEFAULT 0,
    created_at  REAL NOT NULL
);

CREATE TABLE volumes (
    id          TEXT PRIMARY KEY NOT NULL,
    name        TEXT NOT NULL UNIQUE,
    path        TEXT NOT NULL,
    created_at  REAL NOT NULL
);

CREATE TABLE vm_volumes (
    vm_id       TEXT NOT NULL REFERENCES vms(id) ON DELETE CASCADE,
    volume_id   TEXT NOT NULL REFERENCES volumes(id),
    guest_path  TEXT NOT NULL,
    PRIMARY KEY (vm_id, volume_id)
);
CREATE INDEX idx_vm_volumes_volume ON vm_volumes(volume_id);
";

/// Persisted snapshot metadata (disk-only; no memory snapshots).
#[derive(Debug, Clone)]
#[non_exhaustive]
pub(crate) struct SnapshotRow {
    /// Unique snapshot identifier.
    pub id: String,
    /// ID of the VM this snapshot belongs to.
    pub vm_id: String,
    /// Optional human-friendly snapshot name (unique per VM).
    pub name: Option<String>,
    /// Absolute path to the snapshot disk image.
    pub disk_path: String,
    /// Disk image size in bytes.
    pub disk_bytes: u64,
    /// When the snapshot was created.
    pub created_at: SystemTime,
}

/// Persisted base disk metadata with reference counting.
#[derive(Debug, Clone)]
#[non_exhaustive]
#[allow(dead_code, reason = "row mapped from SQLite; fields used in tests")]
pub(crate) struct BaseDiskRow {
    /// Unique base disk identifier.
    pub id: String,
    /// Content digest (e.g. `sha256:abcdef...`).
    pub digest: String,
    /// Absolute path to the base disk image.
    pub path: String,
    /// Number of overlays referencing this base disk.
    pub ref_count: i64,
    /// When the base disk was created.
    pub created_at: SystemTime,
}

/// SQLite-backed VM state database.
///
/// Uses `Mutex<Connection>` to be safely `Send + Sync` without
/// requiring `unsafe impl`. The mutex is held briefly per operation.
#[derive(Debug)]
pub(crate) struct StateDb {
    /// Underlying `SQLite` connection, protected by a mutex.
    conn: std::sync::Mutex<Connection>,
}

#[allow(
    dead_code,
    reason = "base-disk refcount helpers used by DiskManager/tests"
)]
impl StateDb {
    /// Opens (or creates) the product-schema database at `path`.
    ///
    /// Empty / new files get schema version [`PRODUCT_SCHEMA_VERSION`].
    /// Existing files with any other `user_version` are **rejected**.
    ///
    /// # Errors
    ///
    /// Returns an error if the database cannot be opened, schema init fails,
    /// or the on-disk schema version is unsupported.
    pub(crate) fn open(path: impl AsRef<Path>) -> Result<Self> {
        let conn = Connection::open(path)?;
        conn.execute_batch("PRAGMA journal_mode=WAL; PRAGMA foreign_keys=ON;")?;
        ensure_product_schema(&conn)?;
        Ok(Self {
            conn: std::sync::Mutex::new(conn),
        })
    }

    /// Acquires the database connection lock.
    ///
    /// # Panics
    ///
    /// Panics if the mutex is poisoned, which indicates a prior panic
    /// during a database operation — an unrecoverable state.
    #[allow(clippy::expect_used, reason = "poisoned mutex is unrecoverable")]
    fn lock(&self) -> std::sync::MutexGuard<'_, Connection> {
        self.conn.lock().expect("StateDb mutex poisoned")
    }

    /// Inserts a new VM state record.
    ///
    /// # Errors
    ///
    /// Returns an error if serialization or the database insert fails.
    pub(crate) fn insert(&self, s: &VmState) -> Result<()> {
        let config_json = serde_json::to_string(&s.config)?;
        let ts = system_time_to_f64(s.created_at);
        self.lock().execute(
            "INSERT INTO vms (id, name, pid, image, socket, status, config, created_at)
             VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
            params![
                s.id,
                s.name,
                s.pid,
                s.image,
                s.socket.to_string_lossy(),
                status_str(s.status),
                config_json,
                ts,
            ],
        )?;
        Ok(())
    }

    /// Updates the status of a VM.
    ///
    /// # Errors
    ///
    /// Returns an error if the database update fails.
    pub(crate) fn update_status(&self, id: &str, status: Status) -> Result<()> {
        self.lock().execute(
            "UPDATE vms SET status = ?1 WHERE id = ?2",
            params![status_str(status), id],
        )?;
        Ok(())
    }

    /// Persist shim PID and status together (restart must not leave the old PID).
    ///
    /// # Errors
    ///
    /// Returns an error if the database update fails.
    pub(crate) fn update_pid_status(&self, id: &str, pid: i32, status: Status) -> Result<()> {
        self.lock().execute(
            "UPDATE vms SET pid = ?1, status = ?2 WHERE id = ?3",
            params![pid, status_str(status), id],
        )?;
        Ok(())
    }

    /// Rewrites the serialized config JSON for a VM (e.g. after restart security status).
    ///
    /// # Errors
    ///
    /// Returns an error if serialization or the database update fails.
    pub(crate) fn update_config(&self, id: &str, config: &crate::state::VmConfig) -> Result<()> {
        let config_json = serde_json::to_string(config)?;
        self.lock().execute(
            "UPDATE vms SET config = ?1 WHERE id = ?2",
            params![config_json, id],
        )?;
        Ok(())
    }

    /// Finds a VM by exact primary key (`WHERE id = ?1`). Never prefix.
    ///
    /// # Errors
    ///
    /// Returns [`Error::NotFound`] if no row has this id.
    pub(crate) fn get_by_id(&self, id: &str) -> Result<VmState> {
        let conn = self.lock();
        conn.query_row("SELECT * FROM vms WHERE id = ?1", params![id], row_to_state)
            .map_err(|e| match e {
                rusqlite::Error::QueryReturnedNoRows => {
                    Error::NotFound(format!("no VM matching '{id}'"))
                }
                other => Error::Db(other),
            })
    }

    /// Finds a VM by exact name.
    ///
    /// # Errors
    ///
    /// Returns an error if the database query fails.
    pub(crate) fn get_by_name(&self, name: &str) -> Result<Option<VmState>> {
        let conn = self.lock();
        Ok(conn
            .prepare("SELECT * FROM vms WHERE name = ?1")?
            .query_map(params![name], row_to_state)?
            .next()
            .transpose()?)
    }

    /// Finds a VM by exact ID or unique ID prefix.
    ///
    /// # Errors
    ///
    /// Returns [`Error::NotFound`] if no VM matches, or
    /// [`Error::Ambiguous`] if the prefix matches multiple VMs.
    ///
    /// # Panics
    ///
    /// Should not panic in practice; the internal `expect` is guarded
    /// by a length check.
    #[allow(
        clippy::significant_drop_tightening,
        reason = "MutexGuard must live across two queries"
    )]
    pub(crate) fn get_by_id_prefix(&self, prefix: &str) -> Result<VmState> {
        let conn = self.lock();

        // Try exact match first.
        if let Some(row) = conn
            .prepare("SELECT * FROM vms WHERE id = ?1")?
            .query_map(params![prefix], row_to_state)?
            .next()
        {
            return Ok(row?);
        }

        // Prefix search (id LIKE 'prefix%').
        let pattern = format!("{prefix}%");
        let matches: Vec<VmState> = conn
            .prepare("SELECT * FROM vms WHERE id LIKE ?1")?
            .query_map(params![pattern], row_to_state)?
            .collect::<std::result::Result<Vec<_>, _>>()?;
        drop(conn);

        match matches.len() {
            0 => Err(Error::NotFound(format!("no VM matching '{prefix}'"))),
            #[allow(clippy::expect_used, reason = "length checked on previous line")]
            1 => Ok(matches.into_iter().next().expect("len==1")),
            n => Err(Error::Ambiguous(format!(
                "prefix '{prefix}' matches {n} VMs"
            ))),
        }
    }

    /// Lists all VMs, optionally filtering auto-removed stopped VMs.
    ///
    /// # Errors
    ///
    /// Returns an error if the database query fails.
    pub(crate) fn list(&self) -> Result<Vec<VmState>> {
        let conn = self.lock();
        Ok(conn
            .prepare("SELECT * FROM vms ORDER BY created_at DESC")?
            .query_map([], row_to_state)?
            .collect::<std::result::Result<Vec<_>, _>>()?)
    }

    /// Updates the name of a VM.
    ///
    /// # Errors
    ///
    /// Returns an error if the database update fails.
    pub(crate) fn update_name(&self, id: &str, name: Option<&str>) -> Result<()> {
        self.lock()
            .execute("UPDATE vms SET name = ?1 WHERE id = ?2", params![name, id])?;
        Ok(())
    }

    /// Deletes a VM record by ID.
    ///
    /// # Errors
    ///
    /// Returns an error if the database deletion fails.
    pub(crate) fn delete(&self, id: &str) -> Result<()> {
        self.lock()
            .execute("DELETE FROM vms WHERE id = ?1", params![id])?;
        Ok(())
    }

    /// Inserts a snapshot record.
    ///
    /// # Errors
    ///
    /// Returns an error if the database insert fails.
    pub(crate) fn insert_snapshot(&self, s: &SnapshotRow) -> Result<()> {
        self.lock().execute(
            "INSERT INTO snapshots (id, vm_id, name, disk_path, disk_bytes, created_at)
             VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
            params![
                s.id,
                s.vm_id,
                s.name,
                s.disk_path,
                i64::try_from(s.disk_bytes).unwrap_or(i64::MAX),
                system_time_to_f64(s.created_at),
            ],
        )?;
        Ok(())
    }

    /// Lists all snapshots for a given box.
    ///
    /// # Errors
    ///
    /// Returns an error if the database query fails.
    pub(crate) fn list_snapshots(&self, vm_id: &str) -> Result<Vec<SnapshotRow>> {
        let conn = self.lock();
        Ok(conn
            .prepare(
                "SELECT id, vm_id, name, disk_path, disk_bytes, created_at
                 FROM snapshots WHERE vm_id = ?1 ORDER BY created_at DESC",
            )?
            .query_map(params![vm_id], row_to_snapshot)?
            .collect::<std::result::Result<Vec<_>, _>>()?)
    }

    /// Finds a snapshot by ID.
    ///
    /// # Errors
    ///
    /// Returns [`Error::NotFound`] if no snapshot matches.
    pub(crate) fn get_snapshot(&self, snapshot_id: &str) -> Result<SnapshotRow> {
        let conn = self.lock();
        conn.query_row(
            "SELECT id, vm_id, name, disk_path, disk_bytes, created_at
             FROM snapshots WHERE id = ?1",
            params![snapshot_id],
            row_to_snapshot,
        )
        .map_err(|e| match e {
            rusqlite::Error::QueryReturnedNoRows => {
                Error::NotFound(format!("no snapshot matching '{snapshot_id}'"))
            }
            other => Error::Db(other),
        })
    }

    /// Deletes a snapshot record.
    ///
    /// # Errors
    ///
    /// Returns an error if the database deletion fails.
    pub(crate) fn delete_snapshot(&self, snapshot_id: &str) -> Result<()> {
        self.lock()
            .execute("DELETE FROM snapshots WHERE id = ?1", params![snapshot_id])?;
        Ok(())
    }

    /// Inserts or returns an existing base disk by digest.
    ///
    /// # Errors
    ///
    /// Returns an error if the database upsert fails.
    pub(crate) fn upsert_base_disk(&self, id: &str, digest: &str, path: &str) -> Result<()> {
        self.lock().execute(
            "INSERT INTO base_disks (id, digest, path, ref_count, created_at)
             VALUES (?1, ?2, ?3, 0, ?4)
             ON CONFLICT(digest) DO NOTHING",
            params![id, digest, path, system_time_to_f64(SystemTime::now())],
        )?;
        Ok(())
    }

    /// Finds a base disk by digest.
    ///
    /// # Errors
    ///
    /// Returns an error if the database query fails.
    pub(crate) fn get_base_disk_by_digest(&self, digest: &str) -> Result<Option<BaseDiskRow>> {
        let result = {
            let conn = self.lock();
            conn.query_row(
                "SELECT id, digest, path, ref_count, created_at FROM base_disks WHERE digest = ?1",
                params![digest],
                row_to_base_disk,
            )
        };
        match result {
            Ok(row) => Ok(Some(row)),
            Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None),
            Err(e) => Err(Error::Db(e)),
        }
    }

    /// Increments the reference count for a base disk.
    ///
    /// # Errors
    ///
    /// Returns an error if the database update fails.
    pub(crate) fn incr_base_disk_ref(&self, digest: &str) -> Result<()> {
        self.lock().execute(
            "UPDATE base_disks SET ref_count = ref_count + 1 WHERE digest = ?1",
            params![digest],
        )?;
        Ok(())
    }

    /// Decrements the reference count for a base disk.
    ///
    /// # Errors
    ///
    /// Returns an error if the database update fails.
    pub(crate) fn decr_base_disk_ref(&self, digest: &str) -> Result<()> {
        self.lock().execute(
            "UPDATE base_disks SET ref_count = ref_count - 1 WHERE digest = ?1",
            params![digest],
        )?;
        Ok(())
    }

    /// Returns all base disks with `ref_count` <= 0 (eligible for GC).
    ///
    /// # Errors
    ///
    /// Returns an error if the database query fails.
    pub(crate) fn orphaned_base_disks(&self) -> Result<Vec<BaseDiskRow>> {
        let conn = self.lock();
        Ok(conn
            .prepare(
                "SELECT id, digest, path, ref_count, created_at
                 FROM base_disks WHERE ref_count <= 0",
            )?
            .query_map([], row_to_base_disk)?
            .collect::<std::result::Result<Vec<_>, _>>()?)
    }

    /// Deletes a base disk record by ID.
    ///
    /// # Errors
    ///
    /// Returns an error if the database deletion fails.
    pub(crate) fn delete_base_disk(&self, id: &str) -> Result<()> {
        self.lock()
            .execute("DELETE FROM base_disks WHERE id = ?1", params![id])?;
        Ok(())
    }

    // ── volumes ──────────────────────────────────────────────────────────

    /// Insert a named volume row.
    ///
    /// # Errors
    ///
    /// Returns an error if the insert fails (e.g. duplicate name).
    pub(crate) fn insert_volume(&self, v: &crate::volumes::VolumeInfo) -> Result<()> {
        self.lock().execute(
            "INSERT INTO volumes (id, name, path, created_at) VALUES (?1, ?2, ?3, ?4)",
            params![
                v.id,
                v.name,
                v.path.to_string_lossy(),
                system_time_to_f64(v.created_at),
            ],
        )?;
        Ok(())
    }

    /// Look up a volume by name.
    ///
    /// # Errors
    ///
    /// Returns an error if the query fails.
    pub(crate) fn get_volume_by_name(
        &self,
        name: &str,
    ) -> Result<Option<crate::volumes::VolumeInfo>> {
        let conn = self.lock();
        conn.query_row(
            "SELECT id, name, path, created_at FROM volumes WHERE name = ?1",
            params![name],
            row_to_volume,
        )
        .optional()
        .map_err(Into::into)
    }

    /// List all named volumes.
    ///
    /// # Errors
    ///
    /// Returns an error if the query fails.
    #[allow(
        clippy::significant_drop_tightening,
        reason = "stmt borrows conn; collect before drop"
    )]
    pub(crate) fn list_volumes(&self) -> Result<Vec<crate::volumes::VolumeInfo>> {
        let conn = self.lock();
        let mut stmt =
            conn.prepare("SELECT id, name, path, created_at FROM volumes ORDER BY name")?;
        let out = stmt
            .query_map([], row_to_volume)?
            .collect::<std::result::Result<Vec<_>, _>>()?;
        Ok(out)
    }

    /// Delete a volume by id.
    ///
    /// # Errors
    ///
    /// Returns an error if the delete fails.
    pub(crate) fn delete_volume(&self, id: &str) -> Result<()> {
        self.lock()
            .execute("DELETE FROM volumes WHERE id = ?1", params![id])?;
        Ok(())
    }

    /// Count VMs attached to a volume.
    ///
    /// # Errors
    ///
    /// Returns an error if the query fails.
    pub(crate) fn count_volume_attachments(&self, volume_id: &str) -> Result<i64> {
        let n: i64 = self.lock().query_row(
            "SELECT COUNT(*) FROM vm_volumes WHERE volume_id = ?1",
            params![volume_id],
            |r| r.get(0),
        )?;
        Ok(n)
    }

    /// Record a VM↔volume attachment.
    ///
    /// # Errors
    ///
    /// Returns an error if the insert fails.
    pub(crate) fn insert_vm_volume(
        &self,
        vm_id: &str,
        volume_id: &str,
        guest_path: &str,
    ) -> Result<()> {
        self.lock().execute(
            "INSERT OR REPLACE INTO vm_volumes (vm_id, volume_id, guest_path)
             VALUES (?1, ?2, ?3)",
            params![vm_id, volume_id, guest_path],
        )?;
        Ok(())
    }

    /// Remove all volume attachments for a VM.
    ///
    /// # Errors
    ///
    /// Returns an error if the delete fails.
    pub(crate) fn delete_vm_volumes(&self, vm_id: &str) -> Result<()> {
        self.lock()
            .execute("DELETE FROM vm_volumes WHERE vm_id = ?1", params![vm_id])?;
        Ok(())
    }
}

/// Map a `volumes` table row into [`crate::volumes::VolumeInfo`].
fn row_to_volume(row: &rusqlite::Row<'_>) -> rusqlite::Result<crate::volumes::VolumeInfo> {
    Ok(crate::volumes::VolumeInfo {
        id: row.get(0)?,
        name: row.get(1)?,
        path: PathBuf::from(row.get::<_, String>(2)?),
        created_at: f64_to_system_time(row.get(3)?),
    })
}

/// Ensure product schema: init empty DB, or refuse foreign versions.
fn ensure_product_schema(conn: &Connection) -> Result<()> {
    let version: u32 = conn.query_row("PRAGMA user_version", [], |r| r.get(0))?;

    if version == 0 {
        // Distinguish brand-new DB vs legacy unversioned/migrated DB.
        let has_vms: bool = conn.query_row(
            "SELECT COUNT(*) > 0 FROM sqlite_master WHERE type='table' AND name='vms'",
            [],
            |r| r.get(0),
        )?;
        if has_vms {
            return Err(Error::InvalidConfig(format!(
                "state database uses a legacy schema (user_version=0 with existing tables); \
                 delete the bux data directory and recreate (product schema v{PRODUCT_SCHEMA_VERSION})"
            )));
        }
        conn.execute_batch(PRODUCT_SCHEMA_SQL)?;
        conn.pragma_update(None, "user_version", PRODUCT_SCHEMA_VERSION)?;
        return Ok(());
    }

    if version != PRODUCT_SCHEMA_VERSION {
        return Err(Error::InvalidConfig(format!(
            "state database schema version {version} is unsupported \
             (need {PRODUCT_SCHEMA_VERSION}); delete the bux data directory and recreate"
        )));
    }

    Ok(())
}

/// Maps a row to a [`VmState`].
fn row_to_state(row: &rusqlite::Row<'_>) -> rusqlite::Result<VmState> {
    let status_text: String = row.get("status")?;
    let config_json: String = row.get("config")?;
    let ts: f64 = row.get("created_at")?;
    let socket_str: String = row.get("socket")?;

    Ok(VmState {
        id: row.get("id")?,
        name: row.get("name")?,
        pid: row.get("pid")?,
        image: row.get("image")?,
        socket: socket_str.into(),
        status: parse_status(&status_text),
        config: serde_json::from_str(&config_json).map_err(|e| {
            rusqlite::Error::FromSqlConversionFailure(6, rusqlite::types::Type::Text, Box::new(e))
        })?,
        created_at: f64_to_system_time(ts),
    })
}

/// Maps a row to a [`SnapshotRow`].
fn row_to_snapshot(row: &rusqlite::Row<'_>) -> rusqlite::Result<SnapshotRow> {
    Ok(SnapshotRow {
        id: row.get(0)?,
        vm_id: row.get(1)?,
        name: row.get(2)?,
        disk_path: row.get(3)?,
        disk_bytes: u64::try_from(row.get::<_, i64>(4)?).unwrap_or(0),
        created_at: f64_to_system_time(row.get(5)?),
    })
}

/// Maps a row to a [`BaseDiskRow`].
fn row_to_base_disk(row: &rusqlite::Row<'_>) -> rusqlite::Result<BaseDiskRow> {
    Ok(BaseDiskRow {
        id: row.get(0)?,
        digest: row.get(1)?,
        path: row.get(2)?,
        ref_count: row.get(3)?,
        created_at: f64_to_system_time(row.get(4)?),
    })
}

/// Converts a [`Status`] to its database string representation.
const fn status_str(s: Status) -> &'static str {
    match s {
        Status::Running => "running",
        Status::Stopping => "stopping",
        Status::Stopped => "stopped",
    }
}

/// Parses a database string into a [`Status`].
fn parse_status(s: &str) -> Status {
    match s {
        "running" => Status::Running,
        "stopping" => Status::Stopping,
        _ => Status::Stopped,
    }
}

/// Converts a [`SystemTime`] to seconds since UNIX epoch as `f64`.
fn system_time_to_f64(t: SystemTime) -> f64 {
    t.duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs_f64()
}

/// Converts seconds since UNIX epoch (`f64`) back to a [`SystemTime`].
fn f64_to_system_time(secs: f64) -> SystemTime {
    UNIX_EPOCH + Duration::from_secs_f64(secs)
}

#[cfg(test)]
#[allow(
    clippy::unwrap_used,
    clippy::expect_used,
    reason = "test assertions use unwrap for clarity"
)]
mod tests {
    use super::*;

    #[test]
    #[allow(
        clippy::significant_drop_tightening,
        reason = "MutexGuard must outlive rusqlite Statement"
    )]
    fn product_schema_version() {
        let db = StateDb::open(":memory:").expect("open in-memory db");
        assert_eq!(PRODUCT_SCHEMA_VERSION, 5);

        let mut names = Vec::new();
        {
            let conn = db.lock();
            let version: u32 = conn
                .query_row("PRAGMA user_version", [], |r| r.get(0))
                .unwrap();
            assert_eq!(version, 5);

            let mut stmt = conn.prepare("PRAGMA table_info(vms)").unwrap();
            let mut rows = stmt.query([]).unwrap();
            while let Some(row) = rows.next().unwrap() {
                names.push(row.get::<_, String>(1).unwrap());
            }
        }
        assert!(
            !names.iter().any(|n| n == "health"),
            "vms must not have a health column, got {names:?}"
        );
    }
}