claude_codes/
version.rs

1//! Version checking utilities for Claude CLI compatibility
2
3use crate::error::Result;
4use std::process::Command;
5use std::sync::Once;
6use tracing::{debug, warn};
7
8/// The latest Claude CLI version we've tested against
9const TESTED_VERSION: &str = "2.0.76";
10
11/// Ensures version warning is only shown once per session
12static VERSION_CHECK: Once = Once::new();
13
14/// Check the Claude CLI version and warn if newer than tested
15/// This will only issue a warning once per program execution
16pub fn check_claude_version() -> Result<()> {
17    VERSION_CHECK.call_once(|| {
18        if let Err(e) = check_version_impl() {
19            debug!("Failed to check Claude CLI version: {}", e);
20        }
21    });
22    Ok(())
23}
24
25/// Internal implementation of version checking
26fn check_version_impl() -> Result<()> {
27    // Run claude --version
28    let output = Command::new("claude")
29        .arg("--version")
30        .output()
31        .map_err(crate::error::Error::Io)?;
32
33    if !output.status.success() {
34        debug!("Failed to check Claude CLI version - command failed");
35        return Ok(());
36    }
37
38    let version_str = String::from_utf8_lossy(&output.stdout);
39    let version_line = version_str.lines().next().unwrap_or("");
40
41    // Extract version number (format: "1.0.89 (Claude Code)")
42    if let Some(version) = version_line.split_whitespace().next() {
43        if is_version_newer(version, TESTED_VERSION) {
44            warn!(
45                "Claude CLI version {} is newer than tested version {}. \
46                 Please report compatibility at: https://github.com/meawoppl/rust-claude-codes/pulls",
47                version, TESTED_VERSION
48            );
49        } else {
50            debug!(
51                "Claude CLI version {} is compatible (tested: {})",
52                version, TESTED_VERSION
53            );
54        }
55    } else {
56        warn!(
57            "Could not parse Claude CLI version from output: '{}'. \
58             Please report compatibility at: https://github.com/meawoppl/rust-claude-codes/pulls",
59            version_line
60        );
61    }
62
63    Ok(())
64}
65
66/// Compare two version strings (e.g., "1.0.89" vs "1.0.90")
67fn is_version_newer(version: &str, tested: &str) -> bool {
68    let v_parts: Vec<u32> = version.split('.').filter_map(|s| s.parse().ok()).collect();
69    let t_parts: Vec<u32> = tested.split('.').filter_map(|s| s.parse().ok()).collect();
70
71    use std::cmp::Ordering;
72
73    for i in 0..v_parts.len().min(t_parts.len()) {
74        match v_parts[i].cmp(&t_parts[i]) {
75            Ordering::Greater => return true,
76            Ordering::Less => return false,
77            Ordering::Equal => continue,
78        }
79    }
80
81    // If all compared parts are equal, longer version is newer
82    v_parts.len() > t_parts.len()
83}
84
85/// Async version check for tokio-based clients
86pub async fn check_claude_version_async() -> Result<()> {
87    use tokio::sync::OnceCell;
88
89    // Use a static OnceCell for async initialization
90    static ASYNC_VERSION_CHECK: OnceCell<()> = OnceCell::const_new();
91
92    ASYNC_VERSION_CHECK
93        .get_or_init(|| async {
94            if let Err(e) = check_version_impl_async().await {
95                debug!("Failed to check Claude CLI version: {}", e);
96            }
97        })
98        .await;
99
100    Ok(())
101}
102
103/// Internal async implementation of version checking
104async fn check_version_impl_async() -> Result<()> {
105    use tokio::process::Command;
106
107    // Run claude --version
108    let output = Command::new("claude")
109        .arg("--version")
110        .output()
111        .await
112        .map_err(crate::error::Error::Io)?;
113
114    if !output.status.success() {
115        debug!("Failed to check Claude CLI version - command failed");
116        return Ok(());
117    }
118
119    let version_str = String::from_utf8_lossy(&output.stdout);
120    let version_line = version_str.lines().next().unwrap_or("");
121
122    // Extract version number (format: "1.0.89 (Claude Code)")
123    if let Some(version) = version_line.split_whitespace().next() {
124        if is_version_newer(version, TESTED_VERSION) {
125            warn!(
126                "Claude CLI version {} is newer than tested version {}. \
127                 Please report compatibility at: https://github.com/meawoppl/rust-claude-codes/pulls",
128                version, TESTED_VERSION
129            );
130        } else {
131            debug!(
132                "Claude CLI version {} is compatible (tested: {})",
133                version, TESTED_VERSION
134            );
135        }
136    } else {
137        warn!(
138            "Could not parse Claude CLI version from output: '{}'. \
139             Please report compatibility at: https://github.com/meawoppl/rust-claude-codes/pulls",
140            version_line
141        );
142    }
143
144    Ok(())
145}
146
147#[cfg(test)]
148mod tests {
149    use super::*;
150
151    #[test]
152    fn test_version_comparison() {
153        // Test basic version comparison
154        assert!(is_version_newer("1.0.90", "1.0.89"));
155        assert!(!is_version_newer("1.0.89", "1.0.90"));
156        assert!(!is_version_newer("1.0.89", "1.0.89"));
157
158        // Test with different segment counts
159        assert!(is_version_newer("1.1", "1.0.89"));
160        assert!(!is_version_newer("1.0", "1.0.89"));
161        assert!(is_version_newer("1.0.89.1", "1.0.89"));
162
163        // Test major version differences
164        assert!(is_version_newer("2.0.0", "1.99.99"));
165        assert!(!is_version_newer("0.9.99", "1.0.0"));
166    }
167}