Skip to main content

agentmux_srv_core/backend/slug_migration/
stamp.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! What a running srv can do, written beside its databases at startup
5//! (`<data>/srv-features.json`), so a migration can tell whether every
6//! AgentMux running on this host understands former slugs before it renames
7//! one (`SPEC_FORK_SLUG_MIGRATION_2026_10_22.md` ยง4 Phase 2, preconditions).
8//!
9//! Nothing else said it reliably. The srv lock (`<data>/srv.lock`) says which
10//! pid runs on a data directory, but not what it is; a lease names a version
11//! only while it holds an agent; the cross-channel registry lists only
12//! instances with an agent registered. So each srv now writes its pid and its
13//! features here, and a migration accepts an instance only when the stamp's
14//! pid is the lock's. An srv older than this file writes none and is refused,
15//! which is the safe answer: an instance that predates Phase 1 would resolve
16//! nothing addressed to a former slug, and one that predates Phase 0 could
17//! write the old slug back.
18
19use std::path::Path;
20
21use serde::{Deserialize, Serialize};
22
23pub const STAMP_FILE: &str = "srv-features.json";
24
25/// Phases 0 and 1 of the migration spec: one reader of the slug, no stale
26/// write-back, the server's spawn identity, former slugs resolved and
27/// reserved. An srv that writes this understands a renamed agent.
28pub const FORMER_SLUGS: &str = "former-slugs-v1";
29
30#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
31pub struct Stamp {
32    pub pid: u32,
33    pub version: String,
34    #[serde(default)]
35    pub features: Vec<String>,
36}
37
38impl Stamp {
39    pub fn has(&self, feature: &str) -> bool {
40        self.features.iter().any(|f| f == feature)
41    }
42}
43
44/// This srv's stamp, written into its data directory. Best effort: a stamp
45/// that can't be written only makes a migration refuse while this srv runs.
46pub fn write(data_dir: &Path) {
47    let stamp = Stamp {
48        pid: std::process::id(),
49        version: env!("CARGO_PKG_VERSION").to_string(),
50        features: vec![FORMER_SLUGS.to_string()],
51    };
52    let result = serde_json::to_vec_pretty(&stamp)
53        .map_err(|e| e.to_string())
54        .and_then(|bytes| super::home::write_file(&data_dir.join(STAMP_FILE), &bytes));
55    if let Err(e) = result {
56        tracing::warn!(error = %e, "srv features stamp: not written; a fork slug migration will refuse while this srv runs");
57    }
58}
59
60pub fn read(path: &Path) -> Option<Stamp> {
61    serde_json::from_slice(&std::fs::read(path).ok()?).ok()
62}
63
64/// The pid an srv lock names, if it names one.
65pub fn lock_pid(path: &Path) -> Option<u32> {
66    std::fs::read_to_string(path).ok()?.trim().parse().ok()
67}