Skip to main content

lean_ctx/core/
version_check.rs

1use serde::{Deserialize, Serialize};
2use std::path::PathBuf;
3use std::time::{SystemTime, UNIX_EPOCH};
4
5const VERSION_URL: &str = "https://leanctx.com/version.txt";
6const CURRENT_VERSION: &str = env!("CARGO_PKG_VERSION");
7const CACHE_TTL_SECS: u64 = 24 * 60 * 60;
8
9#[derive(Serialize, Deserialize)]
10struct VersionCache {
11    latest: String,
12    checked_at: u64,
13}
14
15fn cache_path() -> Option<PathBuf> {
16    dirs::home_dir().map(|h| h.join(".lean-ctx/latest-version.json"))
17}
18
19fn now_secs() -> u64 {
20    SystemTime::now()
21        .duration_since(UNIX_EPOCH)
22        .map(|d| d.as_secs())
23        .unwrap_or(0)
24}
25
26fn read_cache() -> Option<VersionCache> {
27    let path = cache_path()?;
28    let content = std::fs::read_to_string(path).ok()?;
29    serde_json::from_str(&content).ok()
30}
31
32fn write_cache(latest: &str) {
33    if let Some(path) = cache_path() {
34        let cache = VersionCache {
35            latest: latest.to_string(),
36            checked_at: now_secs(),
37        };
38        if let Ok(json) = serde_json::to_string(&cache) {
39            let _ = std::fs::write(path, json);
40        }
41    }
42}
43
44fn is_cache_stale(cache: &VersionCache) -> bool {
45    let age = now_secs().saturating_sub(cache.checked_at);
46    age > CACHE_TTL_SECS
47}
48
49fn fetch_latest_version() -> Result<String, String> {
50    let agent = ureq::Agent::new_with_config(
51        ureq::config::Config::builder()
52            .timeout_global(Some(std::time::Duration::from_secs(5)))
53            .build(),
54    );
55
56    let body = agent
57        .get(VERSION_URL)
58        .header("User-Agent", &format!("lean-ctx/{CURRENT_VERSION}"))
59        .call()
60        .map_err(|e| e.to_string())?
61        .into_body()
62        .read_to_string()
63        .map_err(|e| e.to_string())?;
64
65    let version = body.trim().trim_start_matches('v').to_string();
66    if version.is_empty() || !version.contains('.') {
67        return Err("invalid version format".to_string());
68    }
69    Ok(version)
70}
71
72fn is_newer(latest: &str, current: &str) -> bool {
73    let parse =
74        |v: &str| -> Vec<u32> { v.split('.').filter_map(|p| p.parse::<u32>().ok()).collect() };
75    parse(latest) > parse(current)
76}
77
78/// Spawn a background thread to fetch latest version from leanctx.com/version.txt
79/// and write the result to ~/.lean-ctx/latest-version.json.
80/// Non-blocking, fire-and-forget. Skips if cache is fresh (<24h).
81pub fn check_background() {
82    let cache = read_cache();
83    if let Some(ref c) = cache {
84        if !is_cache_stale(c) {
85            return;
86        }
87    }
88
89    std::thread::spawn(|| {
90        if let Ok(latest) = fetch_latest_version() {
91            write_cache(&latest);
92        }
93    });
94}
95
96/// Returns a formatted yellow update banner if a newer version is available.
97/// Reads only the local cache file — zero network calls, zero delay.
98pub fn get_update_banner() -> Option<String> {
99    let cache = read_cache()?;
100    if is_newer(&cache.latest, CURRENT_VERSION) {
101        Some(format!(
102            "  \x1b[33m\x1b[1m\u{27F3} Update available: v{CURRENT_VERSION} \u{2192} v{}\x1b[0m  \x1b[2m\u{2014} run:\x1b[0m \x1b[1mlean-ctx update\x1b[0m",
103            cache.latest
104        ))
105    } else {
106        None
107    }
108}
109
110/// Returns version info as JSON for the dashboard /api/version endpoint.
111pub fn version_info_json() -> String {
112    let cache = read_cache();
113    let (latest, update_available) = match cache {
114        Some(c) => {
115            let newer = is_newer(&c.latest, CURRENT_VERSION);
116            (c.latest, newer)
117        }
118        None => (CURRENT_VERSION.to_string(), false),
119    };
120
121    format!(
122        r#"{{"current":"{CURRENT_VERSION}","latest":"{latest}","update_available":{update_available}}}"#
123    )
124}
125
126#[cfg(test)]
127mod tests {
128    use super::*;
129
130    #[test]
131    fn newer_version_detected() {
132        assert!(is_newer("2.9.14", "2.9.13"));
133        assert!(is_newer("3.0.0", "2.9.99"));
134        assert!(is_newer("2.10.0", "2.9.14"));
135    }
136
137    #[test]
138    fn same_or_older_not_newer() {
139        assert!(!is_newer("2.9.13", "2.9.13"));
140        assert!(!is_newer("2.9.12", "2.9.13"));
141        assert!(!is_newer("1.0.0", "2.9.13"));
142    }
143
144    #[test]
145    fn cache_fresh_within_ttl() {
146        let fresh = VersionCache {
147            latest: "2.9.14".to_string(),
148            checked_at: now_secs(),
149        };
150        assert!(!is_cache_stale(&fresh));
151    }
152
153    #[test]
154    fn cache_stale_after_ttl() {
155        let old = VersionCache {
156            latest: "2.9.14".to_string(),
157            checked_at: now_secs() - CACHE_TTL_SECS - 1,
158        };
159        assert!(is_cache_stale(&old));
160    }
161
162    #[test]
163    fn version_json_has_required_fields() {
164        let json = version_info_json();
165        assert!(json.contains("current"));
166        assert!(json.contains("latest"));
167        assert!(json.contains("update_available"));
168    }
169
170    #[test]
171    fn banner_none_for_current_version() {
172        assert!(!is_newer(CURRENT_VERSION, CURRENT_VERSION));
173    }
174}