Skip to main content

agentmux_srv_core/migrations/
m0050_drone_checkpoints.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The table behind a run's checkpoints
5//! (`docs/specs/SPEC_DRONE_GENERAL_AUTOMATION_2026_11_11.md` ยง3.3).
6//!
7//! After every block the engine writes a checkpoint of the run: step 0 whole,
8//! each later one as the diff from the one before (`drone::checkpoint`). They
9//! are kept beside the run row in this version's store (`db_drone_runs` is in
10//! the channel store, so a run and its checkpoints are never in two files), keyed
11//! by `(run_id, step)`. There is **no foreign key** to `db_drone_runs`: the run's
12//! row is written after its first checkpoint, and a delete of the run clears its
13//! checkpoints in the same handler, as it does for the drone's runs (m0038).
14//!
15//! `IF NOT EXISTS`, so a second run changes nothing, and a channel store that does not
16//! exist yet is left to create the table with the rest of its schema.
17
18use rusqlite::Connection;
19
20use super::{Migration, MigrationContext, MigrationError, MigrationScope};
21
22pub struct M0050DroneCheckpoints;
23
24/// The table and its primary key; the same DDL is in the store schema for a fresh store.
25const CREATE: &str = "\
26CREATE TABLE IF NOT EXISTS db_drone_checkpoints (
27    run_id     TEXT NOT NULL,
28    step       INTEGER NOT NULL,
29    kind       TEXT NOT NULL,
30    body       BLOB NOT NULL,
31    written_at INTEGER NOT NULL,
32    PRIMARY KEY (run_id, step)
33);";
34
35impl Migration for M0050DroneCheckpoints {
36    fn id(&self) -> &'static str {
37        "0050_drone_checkpoints"
38    }
39
40    fn scope(&self) -> MigrationScope {
41        MigrationScope::Channel
42    }
43
44    fn description(&self) -> &'static str {
45        "Create db_drone_checkpoints (a Drone run's checkpoints, no run FK)"
46    }
47
48    fn up(&self, ctx: &MigrationContext) -> Result<(), MigrationError> {
49        if ctx.channel_store_path.exists() {
50            let conn = Connection::open(&ctx.channel_store_path)
51                .map_err(|e| MigrationError(format!("drone_checkpoints: open channel store: {e}")))?;
52            conn.execute_batch(CREATE).map_err(|e| MigrationError(format!("db_drone_checkpoints: {e}")))?;
53        }
54        Ok(())
55    }
56}
57
58#[cfg(test)]
59#[path = "m0050_drone_checkpoints_tests.rs"]
60mod tests;