Skip to main content

sqlite_graphrag/extract/
codex_compat.rs

1//! Codex 0.134+ removed --ask-for-approval and -a. Detect version once
2//! at startup and cache the result.
3use std::sync::OnceLock;
4
5static CODEX_SUPPORTS_ASK_FOR_APPROVAL: OnceLock<bool> = OnceLock::new();
6
7/// Returns true if the installed `codex` binary still accepts
8/// `--ask-for-approval` (versions < 0.134.0).
9pub fn codex_supports_ask_for_approval() -> bool {
10    *CODEX_SUPPORTS_ASK_FOR_APPROVAL.get_or_init(detect_codex_supports_ask_for_approval)
11}
12
13fn detect_codex_supports_ask_for_approval() -> bool {
14    let output = std::process::Command::new("codex")
15        .arg("--version")
16        .env_clear()
17        .env("PATH", std::env::var("PATH").unwrap_or_default())
18        .stdout(std::process::Stdio::piped())
19        .stderr(std::process::Stdio::piped())
20        .output();
21    let Ok(out) = output else {
22        return true; // assume supported on probe failure
23    };
24    if !out.status.success() {
25        return true;
26    }
27    let stdout = String::from_utf8_lossy(&out.stdout);
28    // Expected output: "codex-cli 0.134.0" or "codex-cli 0.135.0-beta".
29    // Parse the version string and check the minor.
30    let version_str = stdout.trim();
31    let semver_part = version_str
32        .split_whitespace()
33        .nth(1)
34        .unwrap_or("")
35        .split('-')
36        .next()
37        .unwrap_or("");
38    let mut parts = semver_part.split('.');
39    let major: u64 = parts.next().and_then(|s| s.parse().ok()).unwrap_or(0);
40    let minor: u64 = parts.next().and_then(|s| s.parse().ok()).unwrap_or(0);
41    // Codex 0.134.0 removed --ask-for-approval.
42    if major == 0 && minor >= 134 {
43        return false;
44    }
45    true
46}