kanzo 0.4.3

Self-hosted kanban for humans and agents. One 5.0 MB binary, starts in 3 ms, zero setup.
//! Uses `curl` and `sh` (PowerShell on Windows), which every system has, so Kanzo
//! needs no HTTP client of its own.

#[cfg(unix)]
use std::ffi::OsString;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::{Command, Output, Stdio};
use std::sync::Mutex;
use std::time::{Duration, Instant};

pub const CURRENT: &str = env!("CARGO_PKG_VERSION");

const RELEASES: &str = "https://github.com/emanuele-em/kanzo/releases/latest/download";

/// A Kanzo from Homebrew asks Homebrew: a release reaches it hours later, once its tests pass,
/// and until then `brew upgrade kanzo` has nothing to install.
const HOMEBREW_FORMULA: &str = "https://formulae.brew.sh/api/formula/kanzo.json";

const CHECK_EVERY: Duration = Duration::from_secs(24 * 60 * 60);

#[cfg(not(unix))]
const SERVICE_STOP: Duration = Duration::from_secs(10);

static LATEST: Mutex<Option<(Instant, Option<String>)>> = Mutex::new(None);

fn releases() -> String {
    std::env::var("KANZO_RELEASES_URL").unwrap_or_else(|_| RELEASES.into())
}

fn curl(url: &str) -> Option<Vec<u8>> {
    let out = Command::new("curl").args(["-fsSL", "--max-time", "20", url]).output().ok()?;
    out.status.success().then_some(out.stdout)
}

pub fn latest(now: bool) -> Option<String> {
    let mut cached = LATEST.lock().unwrap_or_else(|e| e.into_inner());
    if let Some((at, version)) = &*cached
        && !now
        && at.elapsed() < CHECK_EVERY
    {
        return version.clone();
    }
    let version = match updated_by() {
        Some(_) => curl(HOMEBREW_FORMULA).and_then(|body| homebrew_version(&body)),
        None => curl(&format!("{}/dist-manifest.json", releases())).and_then(|body| {
            let manifest: serde_json::Value = serde_json::from_slice(&body).ok()?;
            let release = manifest["releases"].as_array()?.iter().find(|r| r["app_name"] == "kanzo")?;
            release["app_version"].as_str().map(String::from)
        }),
    };
    *cached = Some((Instant::now(), version.clone()));
    version
}

fn homebrew_version(formula: &[u8]) -> Option<String> {
    let formula: serde_json::Value = serde_json::from_slice(formula).ok()?;
    formula["versions"]["stable"].as_str().map(String::from)
}

/// Tests answer from this instead of asking GitHub.
#[cfg(test)]
pub fn pretend_latest(version: &str) {
    *LATEST.lock().unwrap_or_else(|e| e.into_inner()) = Some((Instant::now(), Some(version.into())));
}

pub fn newer(version: &str) -> bool {
    let parts = |v: &str| -> Vec<u64> { v.split(['.', '-', '+']).take(3).map(|p| p.parse().unwrap_or(0)).collect() };
    parts(version) > parts(CURRENT)
}

pub fn install() -> Result<PathBuf, String> {
    let exe = program().map_err(|e| e.to_string())?;
    let dir = install_dir(&exe)?;
    let ext = if cfg!(windows) { "ps1" } else { "sh" };
    let script = curl(&format!("{}/kanzo-installer.{ext}", releases())).ok_or("cannot download the installer")?;
    // Windows can't replace a running program, but can rename it; see `remove_old_exes`.
    let old = old_exe(&exe);
    if cfg!(windows) {
        std::fs::rename(&exe, &old).map_err(|e| format!("cannot move {}: {e}", exe.display()))?;
    }
    let out = run_installer(&script, &dir);
    if cfg!(windows) && !out.as_ref().is_ok_and(|o| o.status.success()) {
        let _ = std::fs::rename(&old, &exe);
    }
    let out = out?;
    if !out.status.success() {
        let log = String::from_utf8_lossy(&out.stderr);
        let last = log.lines().rev().find(|l| !l.trim().is_empty()).unwrap_or("no output");
        return Err(format!("the installer failed: {}", last.trim()));
    }
    Ok(exe)
}

/// The folder the installer writes `bin/kanzo` into: the one this program came from, unless
/// Homebrew keeps it.
fn install_dir(exe: &Path) -> Result<PathBuf, String> {
    if homebrew(exe).is_some() {
        return Err(HOMEBREW_UPDATES.into());
    }
    match exe.parent() {
        Some(bin) if bin.file_name().is_some_and(|n| n == "bin") => Ok(bin.parent().unwrap_or(bin).to_path_buf()),
        _ => Err(format!(
            "Kanzo runs from {}, which the installer can't update; reinstall it with the command in the README",
            exe.display()
        )),
    }
}

pub const BREW_UPGRADE: &str = "brew upgrade kanzo";
pub const HOMEBREW_UPDATES: &str = "Kanzo came from Homebrew, which updates it: run brew upgrade kanzo";

/// Homebrew keeps each version in `<prefix>/Cellar/kanzo/<version>/bin`, and upgrades Kanzo
/// itself. For a program there, the path that follows upgrades: `<prefix>/opt/kanzo/bin/kanzo`,
/// as the version's folder goes away with the next upgrade.
fn homebrew(exe: &Path) -> Option<PathBuf> {
    let bin = exe.parent().filter(|bin| bin.ends_with("bin"))?;
    let cellar = bin.parent()?.parent().filter(|formula| formula.ends_with("Cellar/kanzo"))?;
    Some(cellar.parent()?.parent()?.join("opt/kanzo/bin").join(exe.file_name()?))
}

/// What updates this Kanzo, when that's not Kanzo itself.
pub fn updated_by() -> Option<&'static str> {
    program().ok().and_then(|exe| homebrew(&exe)).map(|_| BREW_UPGRADE)
}

/// How to run this program again after an upgrade, for a service or a restart.
pub fn lasting(exe: PathBuf) -> PathBuf {
    exe.canonicalize().ok().and_then(|real| homebrew(&real)).unwrap_or(exe)
}

/// Where this program is, past any symlink. On Windows that comes back as `\\?\C:\…`, which
/// the PowerShell installer can't install to, so it goes back to `C:\…`.
fn program() -> std::io::Result<PathBuf> {
    let exe = std::env::current_exe()?.canonicalize()?;
    match exe.to_str().and_then(|p| p.strip_prefix(r"\\?\")) {
        Some(plain) if plain.as_bytes().get(1) == Some(&b':') => Ok(PathBuf::from(plain)),
        _ => Ok(exe),
    }
}

/// Named for its version: an agent's `kanzo mcp` can keep an older one running, and locked.
fn old_exe(exe: &Path) -> PathBuf {
    exe.with_extension(format!("exe.{CURRENT}.old"))
}

pub fn remove_old_exes(exe: &Path) {
    let (Some(dir), Some(name)) = (exe.parent(), exe.file_name()) else { return };
    let prefix = format!("{}.", name.to_string_lossy());
    for entry in std::fs::read_dir(dir).into_iter().flatten().flatten() {
        let file = entry.file_name();
        let file = file.to_string_lossy();
        if file.starts_with(&prefix) && file.ends_with(".old") {
            let _ = std::fs::remove_file(entry.path());
        }
    }
}

fn run_installer(script: &[u8], dir: &Path) -> Result<Output, String> {
    // PowerShell runs a script from a file, not from stdin.
    let file = std::env::temp_dir().join("kanzo-installer.ps1");
    let mut cmd = if cfg!(windows) {
        std::fs::write(&file, script).map_err(|e| format!("cannot write {}: {e}", file.display()))?;
        let mut ps = Command::new("powershell");
        ps.args(["-NoProfile", "-ExecutionPolicy", "Bypass", "-File"]).arg(&file).stdin(Stdio::null());
        ps
    } else {
        let mut sh = Command::new("sh");
        sh.arg("-s").stdin(Stdio::piped());
        sh
    };
    let out = (|| {
        let mut child = cmd
            .env("KANZO_INSTALL_DIR", dir)
            .env("KANZO_NO_MODIFY_PATH", "1")
            .stdout(Stdio::null())
            .stderr(Stdio::piped())
            .spawn()
            .map_err(|e| format!("cannot run the installer: {e}"))?;
        if let Some(mut stdin) = child.stdin.take() {
            stdin.write_all(script).map_err(|e| e.to_string())?;
        }
        child.wait_with_output().map_err(|e| e.to_string())
    })();
    if cfg!(windows) {
        let _ = std::fs::remove_file(&file);
    }
    out
}

#[cfg(unix)]
fn restart_args(args: impl Iterator<Item = OsString>) -> Vec<OsString> {
    let mut args: Vec<OsString> = args.collect();
    // First, since it isn't an option of a subcommand like `network on`.
    if !args.iter().any(|a| a == "--no-open") {
        args.insert(0, "--no-open".into());
    }
    args
}

/// Same process, so the port, the data folder and a running service stay.
#[cfg(unix)]
pub fn restart(exe: &Path) -> std::io::Error {
    use std::os::unix::process::CommandExt;
    Command::new(exe).args(restart_args(std::env::args_os().skip(1))).exec()
}

#[cfg(not(unix))]
pub fn restart(_exe: &Path) -> std::io::Error {
    if let Err(e) = crate::service::restart() {
        return std::io::Error::other(e.to_string());
    }
    std::thread::sleep(SERVICE_STOP);
    std::io::Error::other("the service did not restart")
}

/// On Windows only the service's board can restart: a running program can't replace itself there.
pub fn can_restart(home: &Path) -> bool {
    cfg!(unix) || crate::service::is_task_board(home)
}

/// The program file this process started from. `kanzo update`, the installer, Homebrew or
/// `cargo install` replace it with a new file, and a running process keeps the old one, so
/// each long-running process watches for that and runs the new program in its place.
#[cfg(unix)]
#[derive(Clone)]
pub struct Exe {
    path: PathBuf,
    stamp: (u64, std::time::SystemTime),
}

#[cfg(unix)]
impl Exe {
    pub fn current() -> Option<Exe> {
        let path = lasting(std::env::current_exe().ok()?);
        let stamp = stamp(&path)?;
        Some(Exe { path, stamp })
    }

    /// One `stat`, cheap enough to check on every message.
    pub fn changed(&self) -> bool {
        stamp(&self.path).is_some_and(|s| s != self.stamp)
    }

    /// The new program, once it runs: a file still being written doesn't, and the next check tries again.
    pub fn replacement(&self) -> Option<&Path> {
        let runs = Command::new(&self.path)
            .arg("--version")
            .stdin(Stdio::null())
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .status()
            .is_ok_and(|s| s.success());
        (self.changed() && runs).then_some(&self.path)
    }
}

#[cfg(unix)]
fn stamp(path: &Path) -> Option<(u64, std::time::SystemTime)> {
    use std::os::unix::fs::MetadataExt;
    let meta = std::fs::metadata(path).ok()?;
    Some((meta.ino(), meta.modified().ok()?))
}

/// Runs the new program as this process, with the same arguments.
#[cfg(unix)]
pub fn exec(exe: &Path, env: Option<(&str, &std::ffi::OsStr)>) -> std::io::Error {
    use std::os::unix::process::CommandExt;
    let mut cmd = Command::new(exe);
    cmd.args(std::env::args_os().skip(1));
    if let Some((key, value)) = env {
        cmd.env(key, value);
    }
    cmd.exec()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn versions() {
        assert!(newer("99.0.0"));
        assert!(!newer(CURRENT));
        assert!(!newer("0.0.1"));
        assert!(!newer("not a version"));
    }

    #[test]
    fn removes_only_old_programs() {
        let tmp = tempfile::tempdir().unwrap();
        let keep = ["kanzo.exe", "kanzo.exe.bak", "other.exe.old", "kanzo.old"];
        for f in keep.iter().chain(&["kanzo.exe.0.2.1.old", "kanzo.exe.0.3.0.old"]) {
            std::fs::write(tmp.path().join(f), "").unwrap();
        }
        remove_old_exes(&tmp.path().join("kanzo.exe"));
        let mut left: Vec<_> =
            std::fs::read_dir(tmp.path()).unwrap().map(|e| e.unwrap().file_name().into_string().unwrap()).collect();
        left.sort();
        assert_eq!(left, ["kanzo.exe", "kanzo.exe.bak", "kanzo.old", "other.exe.old"]);
    }

    #[test]
    #[cfg(unix)]
    fn homebrew_updates_its_own_copy() {
        let brew = |p: &str| homebrew(Path::new(p)).map(|p| p.display().to_string());
        assert_eq!(
            brew("/opt/homebrew/Cellar/kanzo/0.4.3/bin/kanzo").as_deref(),
            Some("/opt/homebrew/opt/kanzo/bin/kanzo")
        );
        assert_eq!(
            brew("/home/linuxbrew/.linuxbrew/Cellar/kanzo/0.4.3_1/bin/kanzo").as_deref(),
            Some("/home/linuxbrew/.linuxbrew/opt/kanzo/bin/kanzo")
        );
        for elsewhere in
            ["/Users/a/.cargo/bin/kanzo", "/opt/homebrew/Cellar/other/1.0/bin/kanzo", "/opt/homebrew/bin/kanzo"]
        {
            assert_eq!(brew(elsewhere), None, "{elsewhere}");
        }

        let formula = br#"{"name":"kanzo","versions":{"stable":"0.4.4","head":"HEAD","bottle":true},"revision":1}"#;
        assert_eq!(homebrew_version(formula).as_deref(), Some("0.4.4"), "the version brew upgrade installs");
        assert_eq!(homebrew_version(b"<html>Not Found</html>"), None);

        let refused = install_dir(Path::new("/opt/homebrew/Cellar/kanzo/0.4.3/bin/kanzo")).unwrap_err();
        assert!(refused.contains("run brew upgrade kanzo"), "{refused}");
        assert_eq!(install_dir(Path::new("/Users/a/.cargo/bin/kanzo")), Ok(PathBuf::from("/Users/a/.cargo")));
        assert!(install_dir(Path::new("/Users/a/Downloads/kanzo")).is_err());

        // A service and a restart run the link Homebrew moves to each new version.
        let tmp = tempfile::tempdir().unwrap();
        let exe = tmp.path().join("Cellar/kanzo/0.4.3/bin/kanzo");
        std::fs::create_dir_all(exe.parent().unwrap()).unwrap();
        std::fs::write(&exe, "").unwrap();
        let real = tmp.path().canonicalize().unwrap();
        assert_eq!(lasting(exe), real.join("opt/kanzo/bin/kanzo"));
        let cargo = tmp.path().join("kanzo");
        assert_eq!(lasting(cargo.clone()), cargo);
    }

    #[test]
    fn the_installer_gets_a_folder_it_can_use() {
        let exe = program().unwrap();
        assert!(exe.is_absolute() && exe.is_file(), "{}", exe.display());
        assert!(!exe.to_string_lossy().starts_with(r"\\?\"), "{}", exe.display());
    }

    #[test]
    #[cfg(unix)]
    fn restarts_without_opening_a_tab() {
        let args = |v: &[&str]| v.iter().map(OsString::from).collect::<Vec<_>>();
        assert_eq!(restart_args(args(&["--port", "4300"]).into_iter()), args(&["--no-open", "--port", "4300"]));
        assert_eq!(restart_args(args(&["network", "on"]).into_iter()), args(&["--no-open", "network", "on"]));
        assert_eq!(restart_args(args(&["--no-open"]).into_iter()), args(&["--no-open"]));
    }
}