Skip to content

A cross-platform desktop All-in-One assistant for Claude Code, Codex, OpenCode,DeepSeek Harness and more. Only official website: ccswitch.io

27.3 KB
Raw Blame
use super::*;

pub(crate) const AGY_CONFIG_DIR_ENV: &str = "CC_SWITCH_AGY_CONFIG_DIR";

fn configured_tool_args(args: &[&str], agy_config_dir: Option<&str>) -> Vec<String> {
    agy_config_dir
        .map(|dir| format!("--gemini_dir={dir}"))
        .into_iter()
        .chain(args.iter().map(|arg| (*arg).to_string()))
        .collect()
}

#[derive(Clone, Copy)]
pub(crate) struct CommandDeadline {
    expires_at: std::time::Instant,
    limit: std::time::Duration,
}

impl CommandDeadline {
    pub(crate) fn from_timeout(timeout: Option<std::time::Duration>) -> Option<Self> {
        timeout.map(|limit| Self {
            expires_at: std::time::Instant::now() + limit,
            limit,
        })
    }

    fn remaining(self) -> Result<std::time::Duration, String> {
        self.expires_at
            .checked_duration_since(std::time::Instant::now())
            .filter(|remaining| !remaining.is_zero())
            .ok_or_else(|| self.timeout_error())
    }

    fn timeout_error(self) -> String {
        format!("Command timed out after {}s", self.limit.as_secs())
    }
}

#[cfg(target_os = "windows")]
pub(crate) fn terminate_child_tree(child: &mut std::process::Child) -> bool {
    use std::os::windows::process::CommandExt;
    use std::process::{Command, Stdio};

    let status = Command::new("taskkill")
        .args(["/PID", &child.id().to_string(), "/T", "/F"])
        .creation_flags(CREATE_NO_WINDOW)
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .status();
    matches!(status, Ok(status) if status.success()) || child.kill().is_ok()
}

#[cfg(not(target_os = "windows"))]
pub(crate) fn terminate_child_tree(child: &mut std::process::Child) -> bool {
    let process_group = -(child.id() as libc::pid_t);
    // SAFETY: runtime commands are placed in a dedicated process group before spawn.
    (unsafe { libc::kill(process_group, libc::SIGKILL) == 0 }) || child.kill().is_ok()
}

#[cfg(not(target_os = "windows"))]
pub(super) fn isolate_child_process_group(cmd: &mut std::process::Command) {
    use std::os::unix::process::CommandExt;

    // setsid 而非 process_group(0):新会话自带新进程组(组长=自身,
    // terminate_child_tree 的 kill(-pid) 整组击杀语义不变),并额外**脱离控制终端**。
    // 只隔离进程组时,探测用的交互式 shell(zsh -lic)若还持有控制终端(如 dev 模式
    // 从终端启动),其作业控制会因处于背景进程组被 SIGTTIN/SIGTTOU 停住,`wait()`
    // 永远等不到退出;脱离终端后 shell 拿不到 /dev/tty,作业控制自动关闭。
    // SAFETY: setsid 是 async-signal-safe;fork 出的子进程继承父进程组、必不是组长,
    // 调用不会因 EPERM 失败。
    unsafe {
        cmd.pre_exec(|| {
            if libc::setsid() == -1 {
                return Err(std::io::Error::last_os_error());
            }
            Ok(())
        });
    }
}

pub(super) fn wait_child_output(
    mut child: std::process::Child,
    deadline: Option<CommandDeadline>,
) -> Result<std::process::Output, String> {
    use std::io::Read;

    let stdout_pipe = child.stdout.take();
    let stderr_pipe = child.stderr.take();

    let stdout_handle = stdout_pipe.map(|mut pipe| {
        std::thread::spawn(move || {
            let mut buf = Vec::new();
            let _ = pipe.read_to_end(&mut buf);
            buf
        })
    });
    let stderr_handle = stderr_pipe.map(|mut pipe| {
        std::thread::spawn(move || {
            let mut buf = Vec::new();
            let _ = pipe.read_to_end(&mut buf);
            buf
        })
    });

    let status = match deadline {
        None => child
            .wait()
            .map_err(|e| format!("Failed to wait for command: {e}"))?,
        Some(deadline) => {
            loop {
                match child.try_wait() {
                    Ok(Some(status)) => break status,
                    Ok(None) => {
                        let remaining = match deadline.remaining() {
                            Ok(remaining) => remaining,
                            Err(error) => {
                                if terminate_child_tree(&mut child) {
                                    let _ = child.wait();
                                }
                                // Do not join pipe readers on timeout. If tree termination fails,
                                // a descendant may still own the write handle and never produce EOF.
                                drop(stdout_handle);
                                drop(stderr_handle);
                                return Err(error);
                            }
                        };
                        std::thread::sleep(std::cmp::min(
                            std::time::Duration::from_millis(50),
                            remaining,
                        ));
                    }
                    Err(e) => {
                        if terminate_child_tree(&mut child) {
                            let _ = child.wait();
                        }
                        return Err(format!("Failed to wait for command: {e}"));
                    }
                }
            }
        }
    };

    if let Some(deadline) = deadline {
        while stdout_handle
            .as_ref()
            .is_some_and(|handle| !handle.is_finished())
            || stderr_handle
                .as_ref()
                .is_some_and(|handle| !handle.is_finished())
        {
            let remaining = match deadline.remaining() {
                Ok(remaining) => remaining,
                Err(error) => {
                    let _ = terminate_child_tree(&mut child);
                    drop(stdout_handle);
                    drop(stderr_handle);
                    return Err(error);
                }
            };
            std::thread::sleep(std::cmp::min(
                std::time::Duration::from_millis(50),
                remaining,
            ));
        }
    }

    let stdout = stdout_handle
        .map(|handle| handle.join().unwrap_or_default())
        .unwrap_or_default();
    let stderr = stderr_handle
        .map(|handle| handle.join().unwrap_or_default())
        .unwrap_or_default();

    Ok(std::process::Output {
        status,
        stdout,
        stderr,
    })
}

fn apply_extra_env(cmd: &mut std::process::Command, extra_env: &[(&str, String)]) {
    for (key, value) in extra_env {
        cmd.env(key, value);
    }
}

/// 参数会经过 `cmd /C call`(双引号包裹)或 WSL 里的 `sh -c '…'`(单引号包裹后再分词),
/// 只放行在两边都不会被解释的字符。`@ / : +` 给插件 id(`name@marketplace`)和
/// 市场来源(`owner/repo`、`https://…`)用;空格、引号、`%`、`&`、`\`、`~` 一律不放。
pub(crate) fn is_safe_tool_command_arg(arg: &str) -> bool {
    !arg.is_empty()
        && arg.chars().all(|c| {
            c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '.' | '@' | '/' | ':' | '+')
        })
}

pub(crate) fn run_detected_tool_command_with_timeout(
    tool: &str,
    args: &[&str],
    timeout: Option<std::time::Duration>,
    extra_env: &[(&str, String)],
    working_dir: &Path,
) -> Result<std::process::Output, String> {
    if known_tool(tool).is_none() {
        return Err(format!("Unsupported tool: {tool}"));
    }
    if args.iter().any(|arg| !is_safe_tool_command_arg(arg)) {
        return Err("Invalid tool command arguments".to_string());
    }
    let agy_config_dir = extra_env
        .iter()
        .find(|(key, _)| tool == "agy" && *key == AGY_CONFIG_DIR_ENV)
        .map(|(_, dir)| dir.as_str());
    if agy_config_dir.is_some_and(|dir| dir.chars().any(char::is_control)) {
        return Err("Invalid Antigravity configuration directory".to_string());
    }

    let deadline = CommandDeadline::from_timeout(timeout);

    #[cfg(target_os = "windows")]
    if let Some(distro) = wsl_distro_for_tool(tool) {
        return run_wsl_tool_command(tool, args, &distro, deadline, extra_env, working_dir);
    }

    // Runtime execution only needs the default entry point. Full installation
    // enumeration runs `--version` for every candidate and belongs to diagnostics.
    #[cfg(target_os = "windows")]
    {
        let tool_path = locate_default_tool(tool, deadline)?;
        let dir = tool_path
            .parent()
            .ok_or_else(|| format!("Invalid {tool} executable path"))?;
        let current_path = effective_path_string();
        run_windows_tool_command_capture(
            &tool_path,
            args,
            &format!("{};{current_path}", dir.display()),
            deadline,
            extra_env,
            working_dir,
            agy_config_dir,
        )
    }

    #[cfg(not(target_os = "windows"))]
    {
        use std::process::Stdio;

        let args = configured_tool_args(args, agy_config_dir);
        let args: Vec<_> = args.iter().map(String::as_str).collect();
        let mut cmd = detected_unix_tool_command(tool, &args, deadline)?;
        // stdin 置 null:继承来的终端(`pnpm dev`)会让要确认的 CLI 一直等到超时。
        cmd.current_dir(working_dir)
            .stdin(Stdio::null())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped());
        apply_extra_env(&mut cmd, extra_env);
        let child = cmd
            .spawn()
            .map_err(|e| format!("Failed to run {tool}: {e}"))?;
        wait_child_output(child, deadline)
    }
}

/// 只确认能找到工具的默认入口(规则同 [`run_detected_tool_command_with_timeout`]),不运行它。
/// 配置在 WSL 里的工具这里不探测、直接放行,运行时由 WSL 里的 `command -v` 判断。
pub(crate) fn locate_detected_tool(tool: &str, timeout: std::time::Duration) -> Result<(), String> {
    if known_tool(tool).is_none() {
        return Err(format!("Unsupported tool: {tool}"));
    }
    #[cfg(target_os = "windows")]
    if wsl_distro_for_tool(tool).is_some() {
        return Ok(());
    }
    locate_default_tool(tool, CommandDeadline::from_timeout(Some(timeout))).map(|_| ())
}

/// 构造运行本机工具默认入口的命令(定位规则和 PATH 补全同 [`run_detected_tool_command_with_timeout`]),
/// 不设 stdio、不等待:给要自己管子进程的调用方用(比如长时间运行、要往 stdin 写东西的登录)。
/// `timeout` 只约束定位工具这一步。工具配置在 WSL 里时报错。
pub(crate) fn detected_tool_command(
    tool: &str,
    args: &[&str],
    timeout: std::time::Duration,
) -> Result<std::process::Command, String> {
    if known_tool(tool).is_none() {
        return Err(format!("Unsupported tool: {tool}"));
    }
    let deadline = CommandDeadline::from_timeout(Some(timeout));

    #[cfg(target_os = "windows")]
    if wsl_distro_for_tool(tool).is_some() {
        return Err(format!(
            "{tool} runs in WSL; this action needs it on Windows"
        ));
    }

    #[cfg(target_os = "windows")]
    {
        let tool_path = locate_default_tool(tool, deadline)?;
        let dir = tool_path
            .parent()
            .ok_or_else(|| format!("Invalid {tool} executable path"))?;
        let new_path = format!("{};{}", dir.display(), effective_path_string());
        Ok(build_windows_tool_command(&tool_path, args, &new_path))
    }

    #[cfg(not(target_os = "windows"))]
    detected_unix_tool_command(tool, args, deadline)
}

#[cfg(not(target_os = "windows"))]
fn detected_unix_tool_command(
    tool: &str,
    args: &[&str],
    deadline: Option<CommandDeadline>,
) -> Result<std::process::Command, String> {
    let tool_path = locate_default_tool(tool, deadline)?;
    let dir = tool_path
        .parent()
        .ok_or_else(|| format!("Invalid {tool} executable path"))?;
    let inherited = std::env::var_os("PATH").unwrap_or_default();
    let login_env = login_shell_env(deadline);
    let path = match login_env.as_ref().and_then(|env| env.path.as_deref()) {
        Some(login_path) => merge_path_segments(login_path, &inherited.to_string_lossy()).into(),
        None => inherited,
    };
    let mut cmd = std::process::Command::new(&tool_path);
    cmd.args(args)
        .env("PATH", prepend_search_dir_to_path(dir, &path));
    if let Some(env) = login_env {
        apply_login_shell_vars(&mut cmd, &env.vars);
    }
    isolate_child_process_group(&mut cmd);
    Ok(cmd)
}

#[cfg(target_os = "windows")]
fn run_windows_tool_command_capture(
    tool_path: &Path,
    args: &[&str],
    new_path: &str,
    deadline: Option<CommandDeadline>,
    extra_env: &[(&str, String)],
    working_dir: &Path,
    agy_config_dir: Option<&str>,
) -> Result<std::process::Output, String> {
    use std::process::Stdio;

    let args = configured_tool_args(args, agy_config_dir);
    let args: Vec<_> = args.iter().map(String::as_str).collect();
    let mut cmd = if agy_config_dir.is_some() {
        // 让 std::process 负责 .cmd/.bat 参数转义,不经过 CALL 的二次展开。
        // 去掉规范化路径的前缀,cmd.exe 无法执行带 \\?\ 前缀的批处理文件。
        let mut cmd = std::process::Command::new(windows_shell_compatible_path(tool_path));
        cmd.args(&args)
            .env("PATH", new_path)
            .creation_flags(CREATE_NO_WINDOW);
        cmd
    } else {
        build_windows_tool_command(tool_path, &args, new_path)
    };
    apply_extra_env(&mut cmd, extra_env);
    cmd.current_dir(working_dir)
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped());
    let child = cmd
        .spawn()
        .map_err(|e| format!("Failed to run tool: {e}"))?;
    wait_child_output(child, deadline)
}

/// Convert `\\wsl$\Distro\home\user\...` / `\\wsl.localhost\...` to a Linux path.
#[cfg(target_os = "windows")]
fn wsl_unc_path_to_linux(path: &Path) -> Option<String> {
    use std::path::{Component, Prefix};

    let mut components = path.components();
    let Component::Prefix(prefix) = components.next()? else {
        return None;
    };
    match prefix.kind() {
        Prefix::UNC(server, _share) | Prefix::VerbatimUNC(server, _share) => {
            let server_name = server.to_string_lossy();
            if !(server_name.eq_ignore_ascii_case("wsl$")
                || server_name.eq_ignore_ascii_case("wsl.localhost"))
            {
                return None;
            }
        }
        _ => return None,
    }

    let mut linux = String::new();
    for component in components {
        match component {
            Component::RootDir => {}
            Component::Normal(part) => {
                linux.push('/');
                linux.push_str(&part.to_string_lossy());
            }
            Component::CurDir | Component::ParentDir | Component::Prefix(_) => return None,
        }
    }
    if linux.is_empty() {
        None
    } else {
        Some(linux)
    }
}

#[cfg(target_os = "windows")]
fn build_wsl_env_argv(extra_env: &[(&str, String)]) -> Result<Vec<String>, String> {
    let mut env_argv = Vec::new();
    for (key, value) in extra_env {
        if key.is_empty()
            || key.contains('=')
            || key.chars().any(|c| c.is_whitespace() || c.is_control())
        {
            return Err(format!("invalid env for {key}"));
        }

        // 配置目录类的变量是 Windows 侧的 `\\wsl$\…` 路径,进发行版前换成 Linux 路径;
        // 换不了(不是 WSL 路径)就不传,让工具用它自己的默认目录。
        let linux_value = if matches!(
            *key,
            "OPENCODE_CONFIG_DIR"
                | "CODEX_HOME"
                | "CLAUDE_CONFIG_DIR"
                | "GROK_HOME"
                | AGY_CONFIG_DIR_ENV
                | "OPENCLAW_STATE_DIR"
                | "OPENCLAW_CONFIG_PATH"
                | "PI_CODING_AGENT_DIR"
        ) {
            let Some(value) = wsl_unc_path_to_linux(Path::new(value)) else {
                continue;
            };
            value
        } else {
            value.clone()
        };
        if linux_value.chars().any(char::is_control) {
            return Err(format!("invalid env for {key}"));
        }
        env_argv.push(format!("{key}={linux_value}"));
    }
    Ok(env_argv)
}

#[cfg(any(target_os = "windows", test))]
fn wsl_tool_invocation(tool: &str, args: &[&str], agy_config_dir: bool) -> String {
    let directory_arg = if agy_config_dir {
        format!(" --gemini_dir=\"${AGY_CONFIG_DIR_ENV}\"")
    } else {
        String::new()
    };
    format!("{tool}{directory_arg} {}", args.join(" "))
        .trim_end()
        .to_string()
}

#[cfg(target_os = "windows")]
fn build_wsl_tool_command(
    tool: &str,
    args: &[&str],
    deadline: Option<CommandDeadline>,
    agy_config_dir: bool,
) -> Result<String, String> {
    let invocation = shell_single_quote(&wsl_tool_invocation(tool, args, agy_config_dir));
    let command = format!(
        "for flag in -lic -lc -c; do if \"${{SHELL:-sh}}\" \"$flag\" 'command -v {tool}' >/dev/null 2>&1; then exec \"${{SHELL:-sh}}\" \"$flag\" {invocation}; fi; done; exit 127"
    );

    let Some(deadline) = deadline else {
        return Ok(command);
    };
    let remaining = deadline.remaining()?;
    let timeout_arg = format!("{:.3}s", remaining.as_secs_f64());
    Ok(format!(
        "command -v timeout >/dev/null 2>&1 || {{ echo 'timeout is required for bounded CLI execution' >&2; exit 127; }}; exec timeout --signal=TERM --kill-after=1s {timeout_arg} sh -c {}",
        shell_single_quote(&command)
    ))
}

#[cfg(target_os = "windows")]
fn run_wsl_tool_command(
    tool: &str,
    args: &[&str],
    distro: &str,
    deadline: Option<CommandDeadline>,
    extra_env: &[(&str, String)],
    working_dir: &Path,
) -> Result<std::process::Output, String> {
    use std::process::{Command, Stdio};

    if !is_valid_wsl_distro_name(distro) {
        return Err(format!("[WSL:{distro}] invalid distro name"));
    }

    let has_config_dir =
        tool == "agy" && extra_env.iter().any(|(key, _)| *key == AGY_CONFIG_DIR_ENV);
    let command = build_wsl_tool_command(tool, args, deadline, has_config_dir)?;
    let linux_working_dir = wsl_unc_path_to_linux(working_dir)
        .ok_or_else(|| format!("[WSL:{distro}] invalid working directory"))?;
    let env_argv = build_wsl_env_argv(extra_env).map_err(|e| format!("[WSL:{distro}] {e}"))?;

    let mut cmd = Command::new("wsl.exe");
    cmd.arg("-d")
        .arg(distro)
        .arg("--cd")
        .arg(linux_working_dir)
        .arg("--");
    if !env_argv.is_empty() {
        cmd.arg("env");
        for item in &env_argv {
            cmd.arg(item);
        }
    }
    cmd.args(["sh", "-c", &command])
        .creation_flags(CREATE_NO_WINDOW)
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped());
    let child = cmd
        .spawn()
        .map_err(|e| format!("[WSL:{distro}] failed to run {tool}: {e}"))?;
    let output = wait_child_output(child, deadline).map_err(|e| {
        if e.starts_with("Command timed out") {
            format!("[WSL:{distro}] {e}")
        } else {
            e
        }
    })?;
    if output.status.code() == Some(124) {
        return Err(format!(
            "[WSL:{distro}] {}",
            deadline
                .map(CommandDeadline::timeout_error)
                .unwrap_or_else(|| "Command timed out".to_string())
        ));
    }
    Ok(output)
}

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

    #[cfg(unix)]
    #[test]
    fn agy_directory_is_one_literal_argument_for_native_and_wsl_commands() {
        let root = "/tmp/custom 'gemini' $(printf injected) & root";
        let expected = format!("--gemini_dir={root}\nplugin\nlist\n--json\n");
        let args = ["plugin", "list", "--json"];
        let native = std::process::Command::new("/bin/sh")
            .args(["-c", "printf '%s\\n' \"$@\"", "fixture-agy"])
            .args(configured_tool_args(&args, Some(root)))
            .output()
            .unwrap();
        assert!(native.status.success());
        assert_eq!(String::from_utf8(native.stdout).unwrap(), expected);

        let invocation = wsl_tool_invocation("agy", &args, true);
        let wsl = std::process::Command::new("/bin/sh")
            .args([
                "-c",
                &format!("agy() {{ printf '%s\\n' \"$@\"; }}; {invocation}"),
            ])
            .env(AGY_CONFIG_DIR_ENV, root)
            .output()
            .unwrap();
        assert!(wsl.status.success());
        assert_eq!(String::from_utf8(wsl.stdout).unwrap(), expected);
        assert_eq!(
            wsl_tool_invocation("codex", &args, false),
            "codex plugin list --json"
        );
    }

    /// 超时击杀路径:挂死的子进程到点被整组击杀、wait 返回超时错误而非永等。
    /// 同时锚定 setsid 改造后的语义——child 是新会话/新进程组组长,
    /// terminate_child_tree 的 kill(-pid) 仍能命中(回归红线:改回 process_group
    /// 或去掉隔离都会让本测试的击杀路径失效)。
    #[cfg(not(target_os = "windows"))]
    #[test]
    fn isolated_hung_child_is_killed_on_deadline() {
        use std::process::{Command, Stdio};

        let mut cmd = Command::new("/bin/sh");
        cmd.args(["-c", "sleep 30"])
            .stdin(Stdio::null())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped());
        isolate_child_process_group(&mut cmd);
        let child = cmd.spawn().expect("spawn sleep");
        let started = std::time::Instant::now();
        let result = wait_child_output(
            child,
            CommandDeadline::from_timeout(Some(std::time::Duration::from_millis(200))),
        );
        assert!(result.is_err(), "expected timeout error, got {result:?}");
        assert!(
            started.elapsed() < std::time::Duration::from_secs(5),
            "kill should return promptly instead of waiting out the sleep"
        );
    }

    #[cfg(target_os = "windows")]
    #[test]
    fn wsl_env_translates_unc_config_paths_and_skips_host_paths() {
        let extra_env = [
            (
                "OPENCODE_CONFIG_DIR",
                r"\\wsl$\Ubuntu\home\Jane Doe\.config\opencode".to_string(),
            ),
            ("CODEX_HOME", r"\\wsl$\Ubuntu\home\jane\.codex".to_string()),
            (
                "CLAUDE_CONFIG_DIR",
                r"\\wsl.localhost\Ubuntu\home\jane\claude-alt".to_string(),
            ),
            ("GROK_HOME", r"\\wsl$\Ubuntu\home\jane\.grok".to_string()),
            (
                AGY_CONFIG_DIR_ENV,
                r"\\wsl$\Ubuntu\home\Jane Doe\custom 'gemini'".to_string(),
            ),
            (
                "OPENCLAW_STATE_DIR",
                r"\\wsl$\Ubuntu\home\jane\.openclaw".to_string(),
            ),
            (
                "OPENCLAW_CONFIG_PATH",
                r"\\wsl$\Ubuntu\home\jane\.openclaw\openclaw.json".to_string(),
            ),
            ("OPENCODE_DISABLE_PROJECT_CONFIG", "true".to_string()),
        ];
        assert_eq!(
            build_wsl_env_argv(&extra_env).unwrap(),
            vec![
                "OPENCODE_CONFIG_DIR=/home/Jane Doe/.config/opencode".to_string(),
                "CODEX_HOME=/home/jane/.codex".to_string(),
                "CLAUDE_CONFIG_DIR=/home/jane/claude-alt".to_string(),
                "GROK_HOME=/home/jane/.grok".to_string(),
                "CC_SWITCH_AGY_CONFIG_DIR=/home/Jane Doe/custom 'gemini'".to_string(),
                "OPENCLAW_STATE_DIR=/home/jane/.openclaw".to_string(),
                "OPENCLAW_CONFIG_PATH=/home/jane/.openclaw/openclaw.json".to_string(),
                "OPENCODE_DISABLE_PROJECT_CONFIG=true".to_string(),
            ]
        );

        let host_paths = [
            (
                "OPENCODE_CONFIG_DIR",
                r"C:\Users\Jane Doe\.config\opencode".to_string(),
            ),
            ("CODEX_HOME", r"C:\Users\jane\.codex".to_string()),
            ("OPENCODE_DISABLE_PROJECT_CONFIG", "true".to_string()),
        ];
        assert_eq!(
            build_wsl_env_argv(&host_paths).unwrap(),
            vec!["OPENCODE_DISABLE_PROJECT_CONFIG=true".to_string()]
        );
    }

    #[cfg(target_os = "windows")]
    #[test]
    fn windows_tool_capture_accepts_canonicalized_batch_paths() {
        let root = tempfile::tempdir().unwrap();
        let bin = root.path().join("模型 tools & helpers");
        std::fs::create_dir(&bin).unwrap();
        std::fs::write(
            bin.join("cc_switch_capture_helper.cmd"),
            "@echo helper-found\r\n@exit /b 23\r\n",
        )
        .unwrap();
        let path = format!("{};{}", bin.display(), effective_path_string());
        for extension in ["cmd", "bat"] {
            let entry = bin.join(format!("opencode.{extension}"));
            std::fs::write(
                &entry,
                "@echo off\r\necho arg=%~1\r\necho env=%CC_SWITCH_CAPTURE_TEST%\r\necho captured-stderr 1>&2\r\ncmd /D /C cc_switch_capture_helper.cmd\r\nexit /b %errorlevel%\r\n",
            )
            .unwrap();
            let canonical = std::fs::canonicalize(entry).unwrap();
            assert!(canonical.to_string_lossy().starts_with(r"\\?\"));
            let output = run_windows_tool_command_capture(
                &canonical,
                &["models"],
                &path,
                CommandDeadline::from_timeout(Some(std::time::Duration::from_secs(10))),
                &[("CC_SWITCH_CAPTURE_TEST", "preserved".into())],
                root.path(),
                None,
            )
            .unwrap();
            let stdout = decode_command_output(&output.stdout);
            assert_eq!(output.status.code(), Some(23), "{output:?}");
            assert!(stdout.contains("arg=models"), "{stdout}");
            assert!(stdout.contains("env=preserved"), "{stdout}");
            assert!(stdout.contains("helper-found"), "{stdout}");
            assert!(decode_command_output(&output.stderr).contains("captured-stderr"));
        }
    }

    #[cfg(target_os = "windows")]
    #[test]
    fn agy_batch_directory_preserves_spaces_and_shell_characters() {
        let root = tempfile::tempdir().unwrap();
        let entry = root.path().join("agy.cmd");
        std::fs::write(&entry, "@echo off\r\necho \"%~1\"\r\necho %~2\r\n").unwrap();
        let config = root.path().join("custom %CC_SWITCH_ARG_TEST% ! & 'gemini'");
        let config = config.to_str().unwrap();
        let output = run_windows_tool_command_capture(
            &std::fs::canonicalize(entry).unwrap(),
            &["plugin", "list"],
            &effective_path_string(),
            CommandDeadline::from_timeout(Some(std::time::Duration::from_secs(10))),
            &[("CC_SWITCH_ARG_TEST", "must-not-expand".into())],
            root.path(),
            Some(config),
        )
        .unwrap();
        assert!(output.status.success());
        let stdout = decode_command_output(&output.stdout);
        let lines: Vec<_> = stdout.lines().collect();
        assert_eq!(
            lines,
            [format!("\"--gemini_dir={config}\""), "plugin".into()]
        );
    }

    #[test]
    fn tool_command_args_allow_plugin_ids_and_reject_shell_syntax() {
        for ok in [
            "plugin",
            "--json",
            "frontend-design@claude-plugins-official",
            "anthropics/claude-plugins-official",
            "https://github.com/owner/repo.git",
            "[email protected]:owner/repo.git",
            "c++",
        ] {
            assert!(is_safe_tool_command_arg(ok), "{ok}");
        }
        for bad in [
            "", "a b", "a&b", "a|b", "%PATH%", "$(id)", "a'b", "a\"b", r"C:\path", "~/x", "a;b",
            "a#b",
        ] {
            assert!(!is_safe_tool_command_arg(bad), "{bad}");
        }
    }
}