Skip to main content

agentmux_srv_core/backend/slug_migration/
steps.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The steps S1–S8, each as three parts: the before-image it records in the
5//! journal's intent, the change it makes, and its undo. Every change is
6//! idempotent (it rewrites only what still holds the old value, so a second
7//! run changes nothing), and both a change and its undo work from the
8//! recorded before-image, never from a fresh look, so a step interrupted
9//! halfway is finished or reversed the same way. Database steps are in
10//! `steps_db.rs`, file steps in `steps_files.rs`.
11
12use std::path::PathBuf;
13
14use serde_json::Value;
15
16use super::home::{Home, Install};
17use super::journal::{BackupEntry, Step};
18use super::{steps_db, steps_files};
19
20/// What every step needs to know about the run.
21pub(super) struct Ctx<'a> {
22    pub home: &'a Home,
23    pub installs: &'a [Install],
24    pub run_dir: PathBuf,
25    pub backups: &'a [BackupEntry],
26    /// When the old slugs retire (epoch ms), the same on a resume.
27    pub retired_at: i64,
28}
29
30impl Ctx<'_> {
31    pub fn objects_dbs(&self) -> Vec<PathBuf> {
32        self.installs.iter().flat_map(|i| i.versions.iter().map(|v| v.objects_db())).collect()
33    }
34    pub fn wan_dbs(&self) -> Vec<PathBuf> {
35        self.installs.iter().filter_map(|i| i.wan_db()).collect()
36    }
37    pub fn host_stores(&self) -> Vec<PathBuf> {
38        self.home.host_stores(self.installs)
39    }
40    /// The backup copy of `original`, checked against its checksum.
41    pub fn backup_of(&self, original: &std::path::Path) -> Result<PathBuf, String> {
42        super::backup::copy_of(&self.run_dir, self.backups, original)
43    }
44}
45
46/// The agent a step works on.
47#[derive(Debug, Clone)]
48pub(super) struct Agent {
49    pub uid: String,
50    pub name: String,
51    /// The slug as stored, and the one `fork_slug` gives.
52    pub old: String,
53    pub new: String,
54}
55
56pub(super) fn before(ctx: &Ctx, a: &Agent, step: Step) -> Result<Value, String> {
57    match step {
58        Step::S1 => steps_files::s1_before(ctx, a),
59        Step::S2 => steps_db::s2_before(ctx, a),
60        Step::S3 => steps_db::s3_before(ctx, a),
61        Step::S4 => steps_db::s4_before(ctx, a),
62        Step::S5 => steps_db::s5_before(ctx, a),
63        Step::S6 => steps_db::s6_before(ctx, a),
64        Step::S7 => {
65            let mut v = steps_db::s7_before(ctx, a)?;
66            v["files"] = steps_files::s7_files_before(ctx, a)?;
67            Ok(v)
68        }
69        Step::S8 => steps_files::s8_before(ctx, a),
70    }
71}
72
73pub(super) fn apply(ctx: &Ctx, a: &Agent, step: Step, before: &Value) -> Result<Vec<String>, String> {
74    match step {
75        Step::S1 => steps_files::s1_apply(ctx, a, before),
76        Step::S2 => steps_db::s2_apply(ctx, a, before),
77        Step::S3 => steps_db::s3_apply(ctx, a, before),
78        Step::S4 => steps_db::s4_apply(ctx, a, before),
79        Step::S5 => steps_db::s5_apply(before),
80        Step::S6 => steps_db::s6_apply(ctx, a, before),
81        Step::S7 => {
82            let mut changes = steps_db::s7_apply(ctx, a, before)?;
83            changes.extend(steps_files::s7_files_apply(a, &before["files"])?);
84            Ok(changes)
85        }
86        Step::S8 => steps_files::s8_apply(before),
87    }
88}
89
90pub(super) fn undo(ctx: &Ctx, a: &Agent, step: Step, before: &Value) -> Result<Vec<String>, String> {
91    match step {
92        Step::S1 => steps_files::s1_undo(ctx, a, before),
93        Step::S2 => steps_db::s2_undo(a, before),
94        Step::S3 => steps_db::s3_undo(ctx, a, before),
95        Step::S4 => steps_db::s4_undo(ctx, a, before),
96        Step::S5 => Ok(Vec::new()),
97        Step::S6 => steps_db::s6_undo(a, before),
98        Step::S7 => {
99            let mut changes = steps_db::s7_undo(a, before)?;
100            changes.extend(steps_files::s7_files_undo(ctx, a, &before["files"])?);
101            Ok(changes)
102        }
103        Step::S8 => steps_files::s8_undo(ctx, before),
104    }
105}
106
107/// A before-image, back from the journal.
108pub(super) fn parse<T: serde::de::DeserializeOwned>(before: &Value) -> Result<T, String> {
109    serde_json::from_value(before.clone()).map_err(|e| format!("the journal's before-image doesn't read: {e}"))
110}
111
112pub(super) fn to_value<T: serde::Serialize>(v: &T) -> Result<Value, String> {
113    serde_json::to_value(v).map_err(|e| e.to_string())
114}