plannotator-tui 0.11.2

Annotate Markdown in the terminal - standalone, and as a Herdr plugin.
//! Host storage roots derived from the process environment.

use std::ffi::OsString;
use std::path::PathBuf;

use plannotator_tui_hosts::{antigravity, commandcode, hermes, opencode};

#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct Roots {
    pub(super) home: PathBuf,
    pub(super) claude_config: PathBuf,
    pub(super) codex_home: PathBuf,
    pub(super) copilot_home: PathBuf,
    pub(super) factory_config: PathBuf,
    /// Antigravity CLI's app data directory: `brain/`, `conversation_summaries.db`, `history.jsonl`.
    pub(super) antigravity_home: PathBuf,
    pi_session_dir: Option<PathBuf>,
    pi_agent_dir: Option<PathBuf>,
    hermes_home: PathBuf,
    opencode_db: Option<PathBuf>,
    xdg_data_home: PathBuf,
}

impl Roots {
    pub(super) fn from_env() -> Self {
        let home = std::env::home_dir().unwrap_or_else(|| PathBuf::from("/"));
        Self::from_lookup(home, |key| std::env::var_os(key))
    }

    fn from_lookup(home: PathBuf, env: impl Fn(&str) -> Option<OsString>) -> Self {
        let path = |key: &str| env(key).filter(|value| !value.is_empty()).map(PathBuf::from);
        let hermes_home = path("HERMES_HOME").unwrap_or_else(|| default_hermes_home(&home, &env));
        let xdg_data_home = path("XDG_DATA_HOME").unwrap_or_else(|| home.join(".local").join("share"));
        Self {
            claude_config: path("CLAUDE_CONFIG_DIR").unwrap_or_else(|| home.join(".claude")),
            codex_home: path("CODEX_HOME").unwrap_or_else(|| home.join(".codex")),
            copilot_home: path("COPILOT_HOME").unwrap_or_else(|| home.join(".copilot")),
            factory_config: path("FACTORY_CONFIG_DIR").unwrap_or_else(|| home.join(".factory")),
            // agy 1.2.7 reads ANTIGRAVITY_APP_DATA_DIR; ANTIGRAVITY_CLI_HOME is the name reported in #70.
            antigravity_home: path("ANTIGRAVITY_CLI_HOME")
                .or_else(|| path("ANTIGRAVITY_APP_DATA_DIR"))
                .unwrap_or_else(|| home.join(antigravity::DEFAULT_APP_DATA_DIR)),
            pi_session_dir: path("PI_CODING_AGENT_SESSION_DIR"),
            pi_agent_dir: path("PI_CODING_AGENT_DIR"),
            hermes_home,
            opencode_db: path("OPENCODE_DB"),
            xdg_data_home,
            home,
        }
    }

    pub(super) fn pi_sessions(&self, default_agent_dir: &str) -> PathBuf {
        self.pi_session_dir.clone().unwrap_or_else(|| {
            self.pi_agent_dir.clone().unwrap_or_else(|| self.home.join(default_agent_dir)).join("sessions")
        })
    }

    /// Claude Code `projects` dirs to search for a known session id: the configured one
    /// first, then those of `profile_dirs` in order.
    pub(super) fn claude_projects_dirs(&self, profile_dirs: &[String]) -> Vec<PathBuf> {
        let configured = self.claude_config.join("projects");
        let listed = profile_dirs
            .iter()
            .map(|dir| dir.strip_prefix("~/").map_or_else(|| PathBuf::from(dir), |rest| self.home.join(rest)))
            .map(|dir| dir.join("projects"))
            .filter(|projects| *projects != configured);
        std::iter::once(configured.clone()).chain(listed).collect()
    }

    /// Command Code reads `$HOME` (else `%USERPROFILE%`) and has no override of its own.
    pub(super) fn commandcode_projects(&self) -> PathBuf {
        self.home.join(commandcode::DEFAULT_PROJECTS_DIR)
    }

    pub(super) fn hermes_database(&self) -> PathBuf {
        self.hermes_home.join(hermes::DB_FILE)
    }

    pub(super) fn opencode_databases(&self) -> Vec<PathBuf> {
        if let Some(db) = &self.opencode_db {
            return vec![db.clone()];
        }
        let data = self.xdg_data_home.join(opencode::DATA_DIR);
        let mut found: Vec<PathBuf> = std::fs::read_dir(&data)
            .map(|entries| {
                entries
                    .filter_map(Result::ok)
                    .map(|entry| entry.path())
                    .filter(|path| {
                        path.file_name().and_then(|name| name.to_str()).is_some_and(|name| {
                            name.starts_with("opencode") && name.to_ascii_lowercase().ends_with(".db")
                        })
                    })
                    .collect()
            })
            .unwrap_or_default();
        found.sort();
        if found.is_empty() {
            found.push(data.join(opencode::DB_FILE));
        }
        found
    }
}

#[cfg(windows)]
fn default_hermes_home(home: &std::path::Path, env: &impl Fn(&str) -> Option<OsString>) -> PathBuf {
    env("LOCALAPPDATA")
        .filter(|value| !value.is_empty())
        .map_or_else(|| home.join("AppData").join("Local"), PathBuf::from)
        .join("hermes")
}

#[cfg(not(windows))]
fn default_hermes_home(home: &std::path::Path, _env: &impl Fn(&str) -> Option<OsString>) -> PathBuf {
    home.join(".hermes")
}

#[cfg(test)]
#[allow(clippy::expect_used, reason = "tests assert by panicking")]
mod tests {
    use std::collections::HashMap;

    use super::*;

    #[test]
    fn antigravity_app_data_dir_is_honoured() {
        let roots = Roots::from_lookup(PathBuf::from("home"), |key| {
            (key == "ANTIGRAVITY_APP_DATA_DIR").then(|| OsString::from("root/agy app"))
        });
        assert_eq!(roots.antigravity_home, PathBuf::from("root/agy app"));
    }

    #[test]
    fn every_documented_root_override_is_honoured() {
        let values: HashMap<&str, OsString> = [
            ("CLAUDE_CONFIG_DIR", "root/claude".into()),
            ("CODEX_HOME", "root/codex".into()),
            ("COPILOT_HOME", "root/copilot".into()),
            ("FACTORY_CONFIG_DIR", "root/factory".into()),
            ("ANTIGRAVITY_CLI_HOME", "root/agy".into()),
            ("PI_CODING_AGENT_SESSION_DIR", "root/pi sessions".into()),
            ("PI_CODING_AGENT_DIR", "root/pi agent ignored".into()),
            ("HERMES_HOME", "root/hermes".into()),
            ("OPENCODE_DB", "root/opencode/custom.db".into()),
            ("XDG_DATA_HOME", "root/xdg ignored".into()),
        ]
        .into_iter()
        .collect();
        let roots = Roots::from_lookup(PathBuf::from("home"), |key| values.get(key).cloned());
        assert_eq!(roots.claude_config, PathBuf::from("root/claude"));
        assert_eq!(roots.codex_home, PathBuf::from("root/codex"));
        assert_eq!(roots.copilot_home, PathBuf::from("root/copilot"));
        assert_eq!(roots.factory_config, PathBuf::from("root/factory"));
        assert_eq!(roots.antigravity_home, PathBuf::from("root/agy"));
        assert_eq!(roots.pi_sessions(".pi/agent"), PathBuf::from("root/pi sessions"));
        assert_eq!(roots.hermes_database(), PathBuf::from("root/hermes").join(hermes::DB_FILE));
        assert_eq!(roots.opencode_databases(), vec![PathBuf::from("root/opencode/custom.db")]);
    }

    #[test]
    fn the_configured_claude_projects_dir_is_searched_before_the_listed_profiles() {
        let roots = Roots::from_lookup(PathBuf::from("home"), |key| {
            (key == "CLAUDE_CONFIG_DIR").then(|| OsString::from("home/.claude-max"))
        });
        let listed = ["~/.claude-work", "/elsewhere/claude", "~/.claude-max/"].map(str::to_owned);
        assert_eq!(
            roots.claude_projects_dirs(&listed),
            ["home/.claude-max/projects", "home/.claude-work/projects", "/elsewhere/claude/projects"]
                .map(PathBuf::from)
        );
    }

    #[test]
    fn pi_agent_and_xdg_overrides_apply_when_the_more_specific_values_are_absent() {
        let values: HashMap<&str, OsString> =
            [("PI_CODING_AGENT_DIR", "root/pi agent ü".into()), ("XDG_DATA_HOME", "root/data ü".into())]
                .into_iter()
                .collect();
        let roots = Roots::from_lookup(PathBuf::from("home"), |key| values.get(key).cloned());
        assert_eq!(
            roots.pi_sessions(plannotator_tui_hosts::omp::DEFAULT_AGENT_DIR),
            PathBuf::from("root/pi agent ü/sessions")
        );
        assert_eq!(
            roots.opencode_databases(),
            vec![PathBuf::from("root/data ü").join(opencode::DATA_DIR).join(opencode::DB_FILE)]
        );
    }
}