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

149 KB
Raw Blame
use super::misc::{is_valid_shell, shell_single_quote};
use crate::app_config::AppType;
use crate::services::ProviderService;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use tauri::State;

#[cfg(target_os = "windows")]
use std::os::windows::process::CommandExt;

#[cfg(target_os = "macos")]
use super::misc::decode_command_output;
#[cfg(target_os = "windows")]
use super::misc::{
    effective_path_string, is_valid_wsl_distro_name, windows_cmd_double_quote_arg,
    windows_shell_compatible_path, wsl_auto_shell_command, wsl_distro_for_tool, CREATE_NO_WINDOW,
};

// 以下 shell 解析辅助函数仅被 macOS/Linux 的终端启动逻辑使用;Windows 非 test 编译下为死代码。
#[cfg_attr(windows, allow(dead_code))]
fn fallback_user_shell() -> &'static str {
    if cfg!(target_os = "macos") {
        "/bin/zsh"
    } else {
        "/bin/bash"
    }
}

#[cfg_attr(windows, allow(dead_code))]
fn valid_user_shell_path(shell: &str) -> bool {
    if shell.is_empty()
        || !shell.starts_with('/')
        || !is_valid_shell(shell)
        || shell.chars().any(char::is_control)
    {
        return false;
    }

    let path = std::path::Path::new(shell);
    path.is_file() && is_executable_file(path)
}

#[cfg(unix)]
fn is_executable_file(path: &std::path::Path) -> bool {
    use std::os::unix::fs::PermissionsExt;

    path.metadata()
        .map(|metadata| metadata.permissions().mode() & 0o111 != 0)
        .unwrap_or(false)
}

#[cfg(not(unix))]
#[cfg_attr(windows, allow(dead_code))]
fn is_executable_file(path: &std::path::Path) -> bool {
    path.is_file()
}

/// 获取用户默认 shell 的完整路径;异常或被污染的 SHELL 回退到平台默认值。
#[cfg_attr(windows, allow(dead_code))]
fn get_user_shell() -> String {
    std::env::var("SHELL")
        .ok()
        .filter(|shell| valid_user_shell_path(shell))
        .unwrap_or_else(|| fallback_user_shell().to_string())
}

/// 构建 exec 行:引号保护 shell 路径,交还用户 shell 让其按默认规则加载 rc 配置。
#[cfg_attr(windows, allow(dead_code))]
fn build_exec_line(shell: &str, cwd: Option<&Path>) -> String {
    let quoted_shell = shell_single_quote(shell);

    match shell.rsplit('/').next().unwrap_or(shell) {
        "zsh" => cwd
            .map(|dir| {
                let command = format!(
                    "cd {} || exit 1; exec {} -i",
                    shell_single_quote(&dir.to_string_lossy()),
                    quoted_shell
                );
                format!("exec {} -lc {}", quoted_shell, shell_single_quote(&command))
            })
            .unwrap_or_else(|| format!("exec {quoted_shell} -l")),
        _ => format!("exec {quoted_shell}"),
    }
}

/// 构建 provider 命令行:通过用户 shell 的交互模式执行,确保 GUI 启动的终端也加载用户 PATH。
#[cfg_attr(windows, allow(dead_code))]
fn build_provider_command_line(shell: &str, launch_command: &str, cwd: Option<&Path>) -> String {
    let mut command = cwd
        .map(|dir| {
            format!(
                "cd {} && {}",
                user_shell_quote(shell, &dir.to_string_lossy()),
                launch_command
            )
        })
        .unwrap_or_else(|| launch_command.to_string());

    if shell.rsplit('/').next() == Some("bash") {
        // Bash -ic can exec its last external command. Keep Bash alive to wait
        // for the CLI and restore the foreground process group before returning to sh.
        command.push_str("; exit $?");
    }

    format!(
        "{} {} {}",
        shell_single_quote(shell),
        provider_command_flag_for_shell(shell),
        shell_single_quote(&command)
    )
}

#[cfg_attr(windows, allow(dead_code))]
fn provider_command_flag_for_shell(shell: &str) -> &'static str {
    match shell.rsplit('/').next().unwrap_or(shell) {
        "dash" | "sh" => "-c",
        "zsh" => "-lic",
        _ => "-ic",
    }
}

#[cfg_attr(windows, allow(dead_code))]
fn build_final_shell_cd_command(shell: &str, cwd: Option<&Path>) -> String {
    if matches!(shell.rsplit('/').next().unwrap_or(shell), "zsh") {
        return String::new();
    }

    cwd.map(|dir| {
        format!(
            "cd {} || exit 1\n",
            shell_single_quote(&dir.to_string_lossy())
        )
    })
    .unwrap_or_default()
}
/// `hermes dashboard` 的启动命令行(交给 `launch_terminal_running`)。
///
/// Windows 上 Hermes 配置目录指向 WSL(UNC 路径)时,要进对应发行版里启动,
/// 否则跑起来的是 Windows 本机的 hermes(或报找不到命令),#8689。
/// Web UI 探测的 `127.0.0.1` 端口由 WSL2 的 localhost 转发到发行版里,不用改。
pub(crate) fn hermes_dashboard_command_line() -> Result<String, String> {
    #[cfg(target_os = "windows")]
    {
        if let Some(distro) = wsl_distro_for_tool("hermes") {
            return wsl_hermes_dashboard_line(&distro);
        }
    }
    Ok("hermes dashboard".to_string())
}

/// 用发行版里用户自己的登录交互 shell(`$SHELL -lic`)跑,PATH 才带得上
/// `~/.local/bin` 这类在 `.bashrc` / `.profile` 里加的目录。只用一个 flag、
/// 不像版本探测那样失败后换 flag 重试:dashboard 被 Ctrl+C 结束也是非零退出,
/// 重试会把它再拉起来。
#[cfg(target_os = "windows")]
fn wsl_hermes_dashboard_line(distro: &str) -> Result<String, String> {
    if !is_valid_wsl_distro_name(distro) {
        return Err(format!("Invalid WSL distro name: {distro}"));
    }
    Ok(format!(
        "wsl.exe -d {distro} -- sh -c {}",
        windows_cmd_double_quote_arg(&wsl_auto_shell_command("hermes dashboard", Some("-lic")))
    ))
}

/// 打开指定提供商的终端
///
/// 根据提供商配置的环境变量启动一个带有该提供商特定设置的终端
/// 无需检查是否为当前激活的提供商,任何提供商都可以打开终端
#[allow(clippy::too_many_arguments)]
#[tauri::command]
pub async fn open_provider_terminal(
    state: State<'_, crate::store::AppState>,
    agy_accounts: State<'_, crate::commands::AgyAccountState>,
    copilot_auth: State<'_, crate::commands::CopilotAuthState>,
    app: String,
    #[allow(non_snake_case)] providerId: String,
    cwd: Option<String>,
    #[allow(non_snake_case)] skipPermissions: Option<bool>,
    #[allow(non_snake_case)] extraArgs: Option<String>,
    #[allow(non_snake_case)] agyUseCurrentLogin: Option<bool>,
) -> Result<bool, String> {
    let app_type = AppType::from_str(&app)?;
    let launch_cwd = resolve_launch_cwd(cwd)?;
    // 先拆参数:填错了直接报错,不必等到切完 agy 登录、写完临时文件。
    let extra_args = split_launch_args(extraArgs.as_deref().unwrap_or_default())?;

    // 获取提供商配置
    let providers = ProviderService::list(state.inner(), app_type.clone())
        .map_err(|e| format!("获取提供商列表失败: {e}"))?;

    let provider = providers
        .get(&providerId)
        .ok_or_else(|| format!("提供商 {providerId} 不存在"))?;

    let mut launch = match app_type {
        AppType::Agy => {
            if !(cfg!(target_os = "linux") && agyUseCurrentLogin == Some(true)) {
                crate::services::provider::agy_direct::switch_to_bound_account(
                    &agy_accounts.0,
                    provider,
                )
                .await?;
            }
            agy_terminal_launch(provider)?
        }
        AppType::Copilot => {
            let account = copilot_bound_account(&copilot_auth, provider).await?;
            copilot_terminal_launch(provider, account)?
        }
        AppType::Codex => codex_terminal_launch(state.db.as_ref(), provider)?,
        AppType::GrokBuild => grok_terminal_launch(state.db.as_ref(), provider)?,
        AppType::Claude => {
            use crate::services::provider::claude_official::{terminal_account, AccountTerminal};
            match terminal_account(provider)? {
                Some(AccountTerminal::Dir(config_dir)) => {
                    crate::services::claude_account::sync_shared(&config_dir);
                    claude_account_terminal_launch(Some(config_dir))
                }
                Some(AccountTerminal::Default) => {
                    crate::services::provider::claude_official::ensure_default_terminal_is_official(
                        &state.db,
                    )?;
                    claude_account_terminal_launch(None)
                }
                None => {
                    let env_vars =
                        extract_env_vars_from_config(&provider.settings_config, &app_type);
                    let drops = crate::services::provider::claude_switch_drops(
                        state.inner(),
                        &provider.settings_config,
                    )
                    .unwrap_or_else(|e| {
                        log::warn!("计算终端要挡住的 live 键失败,按原样启动: {e}");
                        Default::default()
                    });
                    claude_terminal_launch(&env_vars, &providerId, &provider.name, &drops)?
                }
            }
        }
        _ => {
            let env_vars = extract_env_vars_from_config(&provider.settings_config, &app_type);
            claude_terminal_launch(&env_vars, &providerId, &provider.name, &Default::default())?
        }
    };
    add_global_proxy_env(
        &mut launch,
        &app_type,
        card_env_keys(provider),
        crate::proxy::http_client::get_current_proxy_url().as_deref(),
    );
    if skipPermissions == Some(true) {
        launch.skip_permissions();
    }
    if let Err(e) = launch.append_user_args(extra_args) {
        for file in &launch.cleanup {
            let _ = std::fs::remove_file(file);
        }
        return Err(e);
    }

    launch_provider_terminal(&launch, launch_cwd.as_deref())
        .map_err(|e| format!("启动终端失败: {e}"))?;

    Ok(true)
}

/// 从提供商配置中提取环境变量
fn extract_env_vars_from_config(
    config: &serde_json::Value,
    app_type: &AppType,
) -> Vec<(String, String)> {
    let mut env_vars = Vec::new();

    let Some(obj) = config.as_object() else {
        return env_vars;
    };

    // 处理 env 字段(Claude/Gemini 通用)
    if let Some(env) = obj.get("env").and_then(|v| v.as_object()) {
        for (key, value) in env {
            if let Some(str_val) = value.as_str() {
                env_vars.push((key.clone(), str_val.to_string()));
            }
        }

        // 处理 base_url: 根据应用类型添加对应的环境变量
        let base_url_key = match app_type {
            AppType::Claude | AppType::ClaudeDesktop => Some("ANTHROPIC_BASE_URL"),
            AppType::Gemini => Some("GOOGLE_GEMINI_BASE_URL"),
            _ => None,
        };

        if let Some(key) = base_url_key {
            if let Some(url_str) = env.get(key).and_then(|v| v.as_str()) {
                env_vars.push((key.to_string(), url_str.to_string()));
            }
        }
    }

    // Codex 使用 auth 字段转换为 OPENAI_API_KEY
    if *app_type == AppType::Codex {
        if let Some(auth) = obj.get("auth").and_then(|v| v.as_str()) {
            env_vars.push(("OPENAI_API_KEY".to_string(), auth.to_string()));
        }
    }

    // Gemini 使用 api_key 字段转换为 GEMINI_API_KEY
    if *app_type == AppType::Gemini {
        if let Some(api_key) = obj.get("api_key").and_then(|v| v.as_str()) {
            env_vars.push(("GEMINI_API_KEY".to_string(), api_key.to_string()));
        }
    }

    env_vars
}

fn resolve_launch_cwd(cwd: Option<String>) -> Result<Option<PathBuf>, String> {
    let Some(raw_path) = cwd.filter(|value| !value.trim().is_empty()) else {
        return Ok(None);
    };

    if raw_path.contains('\n') || raw_path.contains('\r') {
        return Err("目录路径包含非法换行符".to_string());
    }

    let path = Path::new(&raw_path);
    if !path.exists() {
        return Err(format!("目录不存在: {raw_path}"));
    }

    let resolved = std::fs::canonicalize(path).map_err(|e| format!("解析目录失败: {e}"))?;
    if !resolved.is_dir() {
        return Err(format!("选择的路径不是文件夹: {}", resolved.display()));
    }

    #[cfg(target_os = "windows")]
    let resolved = windows_shell_compatible_path(&resolved);

    Ok(Some(resolved))
}

/// 在终端里启动的一次供应商会话:跑什么命令、只给它哪些环境变量、退出后删哪些临时
/// 文件。打开终端那一层(各家终端的 AppleScript、Linux 候选列表、Windows start)对所有
/// 应用共用,只有这里按应用不同。
struct ProviderLaunch {
    /// 启动前打印的说明
    title: &'static str,
    /// 说明下面打印的路径(只在 macOS / Linux 打印)
    #[cfg_attr(windows, allow(dead_code))]
    detail: Option<PathBuf>,
    program: &'static str,
    /// macOS / Linux 的参数,交给用户 shell 执行前逐个加引号
    #[cfg_attr(windows, allow(dead_code))]
    args: Vec<String>,
    /// Windows .bat 里执行的命令
    #[cfg_attr(not(windows), allow(dead_code))]
    batch_command: String,
    /// 只给这一个进程的环境变量;`None` 表示要清掉(用户 rc 里可能 export 过同名变量)。
    /// macOS / Linux 写进 0600 的临时文件、读完立即删除;Windows 写进 .bat 的 `setlocal` 段。
    env: Vec<(&'static str, Option<String>)>,
    /// 只在 .bat 里设置、给 `batch_command` 引用的变量(比如可能含非 ASCII 的路径)
    #[cfg_attr(not(windows), allow(dead_code))]
    batch_vars: Vec<(&'static str, String)>,
    /// 退出时删除的临时文件,都在系统临时目录里
    cleanup: Vec<PathBuf>,
    /// 设置里的全局代理(`add_global_proxy_env` 填)。和 `env` 不同,macOS / Linux 上不经
    /// `env` 前缀,而是导出给运行命令的用户 shell:`env` 直接执行程序,会跳过用户给
    /// `claude` 这类命令定义的 alias / 函数;代价是用户 rc 里自己 export 的代理优先。
    /// Windows 和 `env` 一样写进 `setlocal` 段。
    proxy_env: Vec<(&'static str, String)>,
}

impl ProviderLaunch {
    /// 用户在「打开终端」里勾了跳过权限确认:只加给这一次启动,不写进任何配置。
    fn skip_permissions(&mut self) {
        // Claude Code 和 agy 的参数同名;Codex 跳过确认的同时也关掉沙箱;Grok Build 用自己的;
        // Copilot CLI 的 `--allow-all` 放开工具、路径和 URL 三类确认。
        let flag = match self.program {
            "codex" => "--dangerously-bypass-approvals-and-sandbox",
            "grok" => "--always-approve",
            "copilot" => "--allow-all",
            _ => "--dangerously-skip-permissions",
        };
        self.args.push(flag.to_string());
        self.batch_command.push(' ');
        self.batch_command.push_str(flag);
    }

    /// 用户在「打开终端」里填的附加参数,接在 CC Switch 生成的参数后面,同样只给这一次。
    /// Windows 上每个参数经 .bat 变量传、用引号包住,cmd 的 `&`、`|` 之类不会接出别的命令。
    fn append_user_args(&mut self, args: Vec<String>) -> Result<(), String> {
        // Codex 的启动行带 `call`:`%` 会再展开一遍、引号里的 `^` 会加倍,和 `-c` 值同一条限制。
        if cfg!(target_os = "windows") && args.iter().any(|arg| arg.contains(['"', '%', '^'])) {
            return Err("附加参数里有 Windows 终端传不过去的字符(\" % ^)".to_string());
        }
        for (arg, var) in args.into_iter().zip(USER_ARG_BATCH_VARS) {
            // 程序按 Windows 的规则拆命令行:收尾引号前的反斜杠要成对,否则 `C:\work\` 会把
            // 引号当成内容吞掉。
            let trailing = arg.len() - arg.trim_end_matches('\\').len();
            let quoted_value = format!("{arg}{}", "\\".repeat(trailing));
            self.batch_command.push_str(&format!(" \"%{var}%\""));
            self.batch_vars.push((var, quoted_value));
            self.args.push(arg);
        }
        Ok(())
    }
}

/// 附加参数在 .bat 里经这些变量传,也是附加参数的个数上限(各平台一样)。
const USER_ARG_BATCH_VARS: [&str; 16] = [
    "CC_SWITCH_ARG0",
    "CC_SWITCH_ARG1",
    "CC_SWITCH_ARG2",
    "CC_SWITCH_ARG3",
    "CC_SWITCH_ARG4",
    "CC_SWITCH_ARG5",
    "CC_SWITCH_ARG6",
    "CC_SWITCH_ARG7",
    "CC_SWITCH_ARG8",
    "CC_SWITCH_ARG9",
    "CC_SWITCH_ARG10",
    "CC_SWITCH_ARG11",
    "CC_SWITCH_ARG12",
    "CC_SWITCH_ARG13",
    "CC_SWITCH_ARG14",
    "CC_SWITCH_ARG15",
];

/// 把附加参数按空白拆开:单引号或双引号括起来的部分算同一个参数(引号本身去掉),没有
/// 反斜杠转义(Windows 路径里全是反斜杠)。只拆成参数,不经 shell,`;`、`&`、`|` 原样交给 CLI。
fn split_launch_args(input: &str) -> Result<Vec<String>, String> {
    let mut args = Vec::new();
    let mut current = String::new();
    let mut in_word = false;
    let mut quote: Option<char> = None;
    for c in input.chars() {
        if c.is_control() && !(quote.is_none() && c.is_whitespace()) {
            return Err("附加参数里不能有换行等控制字符".to_string());
        }
        match quote {
            Some(q) if c == q => quote = None,
            Some(_) => current.push(c),
            None if c == '"' || c == '\'' => {
                quote = Some(c);
                in_word = true;
            }
            None if c.is_whitespace() => {
                if in_word {
                    args.push(std::mem::take(&mut current));
                    in_word = false;
                }
            }
            None => {
                current.push(c);
                in_word = true;
            }
        }
    }
    if quote.is_some() {
        return Err("附加参数里的引号没有配对".to_string());
    }
    if in_word {
        args.push(current);
    }
    if args.len() > USER_ARG_BATCH_VARS.len() {
        return Err(format!("附加参数最多 {} 个", USER_ARG_BATCH_VARS.len()));
    }
    Ok(args)
}

/// Codex 的 `-c` 值在 .bat 里经这些变量传(值里可能有 `&` 之类 cmd 的特殊字符)。
#[cfg_attr(not(target_os = "windows"), allow(dead_code))]
const CODEX_OVERRIDE_BATCH_VARS: [&str; 24] = [
    "CC_SWITCH_CODEX_C0",
    "CC_SWITCH_CODEX_C1",
    "CC_SWITCH_CODEX_C2",
    "CC_SWITCH_CODEX_C3",
    "CC_SWITCH_CODEX_C4",
    "CC_SWITCH_CODEX_C5",
    "CC_SWITCH_CODEX_C6",
    "CC_SWITCH_CODEX_C7",
    "CC_SWITCH_CODEX_C8",
    "CC_SWITCH_CODEX_C9",
    "CC_SWITCH_CODEX_C10",
    "CC_SWITCH_CODEX_C11",
    "CC_SWITCH_CODEX_C12",
    "CC_SWITCH_CODEX_C13",
    "CC_SWITCH_CODEX_C14",
    "CC_SWITCH_CODEX_C15",
    "CC_SWITCH_CODEX_C16",
    "CC_SWITCH_CODEX_C17",
    "CC_SWITCH_CODEX_C18",
    "CC_SWITCH_CODEX_C19",
    "CC_SWITCH_CODEX_C20",
    "CC_SWITCH_CODEX_C21",
    "CC_SWITCH_CODEX_C22",
    "CC_SWITCH_CODEX_C23",
];

/// Codex:卡就是当前直连的那家时直接跑 `codex`;否则用 `-c` 只对这个进程换成它,Key 经
/// 环境变量传(见 `codex_terminal`),不改 `config.toml`。
fn codex_terminal_launch(
    db: &crate::database::Database,
    provider: &crate::provider::Provider,
) -> Result<ProviderLaunch, String> {
    use crate::services::provider::codex_terminal;
    let plan = codex_terminal::terminal_launch(db, provider)?;
    let mut overrides = plan.overrides;
    let mut cleanup = Vec::new();
    if let Some(catalog) = plan.catalog {
        let path = unique_temp_path("codex_catalog", "json");
        std::fs::write(&path, catalog).map_err(|e| format!("写入临时模型目录失败: {e}"))?;
        overrides.push(codex_terminal::catalog_override(&path.to_string_lossy()));
        cleanup.push(path);
    }
    codex_launch(&provider.name, overrides, plan.env, cleanup.clone()).inspect_err(|_| {
        for file in &cleanup {
            let _ = std::fs::remove_file(file);
        }
    })
}

fn codex_launch(
    name: &str,
    overrides: Vec<String>,
    env: Vec<(&'static str, Option<String>)>,
    cleanup: Vec<PathBuf>,
) -> Result<ProviderLaunch, String> {
    if overrides.len() > CODEX_OVERRIDE_BATCH_VARS.len() {
        return Err(format!("「{name}」要覆盖的 Codex 配置项太多"));
    }
    // cmd 不认 `\"`,值里有双引号就会把后面的 `&` 等字符露在引号外面;`call` 会把 `%` 再展开
    // 一遍、把引号里的 `^` 加倍。
    if cfg!(target_os = "windows")
        && overrides
            .iter()
            .any(|value| value.contains(['"', '%', '^']))
    {
        return Err(format!(
            "「{name}」的 Codex 配置里有 Windows 终端传不过去的字符(\" % ^),请先切换到这张卡再打开终端"
        ));
    }

    let mut args = Vec::new();
    // npm 装的是 codex.cmd:不加 `call` 跑完不回到启动 .bat,后面删临时文件(含 Key 的
    // .bat 本身)的几行就不执行了。
    let mut batch_command = String::from("call codex");
    let mut batch_vars = Vec::new();
    for (value, var) in overrides.into_iter().zip(CODEX_OVERRIDE_BATCH_VARS) {
        args.push("-c".to_string());
        args.push(value.clone());
        batch_command.push_str(&format!(" -c \"%{var}%\""));
        batch_vars.push((var, value));
    }
    Ok(ProviderLaunch {
        title: if args.is_empty() {
            "Starting Codex with its current config"
        } else {
            "Using this provider for Codex in this terminal only (config.toml is unchanged)"
        },
        detail: None,
        program: "codex",
        args,
        batch_command,
        env,
        batch_vars,
        cleanup,
        proxy_env: Vec::new(),
    })
}

/// Grok Build:直接跑 `grok`,读的就是 live 的 `config.toml`,所以只能打开当前供应商。
///
/// Grok CLI 没有只对一个进程换配置的办法:没有 `-c` 之类的覆盖参数,`GROK_CONFIG_PATH`
/// 不生效(1.0.70 实测),`GROK_HOME` 会把会话、登录、技能一起换到别的目录。
/// `GROK_HOME` 定到 CC Switch 管的目录(设置里可以改),Grok 读的就是这里写的那份。
fn grok_terminal_launch(
    db: &crate::database::Database,
    provider: &crate::provider::Provider,
) -> Result<ProviderLaunch, String> {
    use crate::mode::current::{self, Purpose};
    let in_use = current::provider_for(db, &AppType::GrokBuild, Purpose::InUse)?;
    if in_use.as_deref() != Some(provider.id.as_str()) {
        return Err(crate::error::AppError::localized(
            "grokbuild.terminal.not_current",
            format!(
                "Grok Build 不能只对一个终端换供应商:请先切换到「{}」,再打开终端",
                provider.name
            ),
            format!(
                "Grok Build can't use a different provider in a single terminal. Switch to \"{}\" first, then open the terminal",
                provider.name
            ),
        )
        .to_string());
    }
    Ok(grok_launch(
        crate::grok_config::get_grok_config_dir()
            .to_string_lossy()
            .into_owned(),
    ))
}

fn grok_launch(grok_home: String) -> ProviderLaunch {
    ProviderLaunch {
        title: "Starting Grok Build with its current config",
        detail: None,
        program: "grok",
        args: Vec::new(),
        // npm 装的是 grok.cmd:不加 `call` 跑完不回到启动 .bat,后面的清理就不执行了。
        batch_command: "call grok".to_string(),
        env: vec![("GROK_HOME", Some(grok_home))],
        batch_vars: Vec::new(),
        cleanup: Vec::new(),
        proxy_env: Vec::new(),
    }
}

/// Claude Code:把卡上的 env 和 `/model` 菜单写进临时 settings 文件,
/// `claude --settings <文件>` 启动。`drops` 是切到这张卡时会从 live 清掉的键,在临时文件里
/// 挡住(见 [`write_claude_config`])。
fn claude_terminal_launch(
    env_vars: &[(String, String)],
    provider_id: &str,
    provider_name: &str,
    drops: &crate::services::provider::SwitchDrops,
) -> Result<ProviderLaunch, String> {
    let config_file = unique_temp_path(&format!("claude_{}", ascii_file_stem(provider_id)), "json");
    write_claude_config(&config_file, env_vars, provider_name, drops)?;
    let config_name = config_file
        .file_name()
        .map(|name| name.to_string_lossy().into_owned())
        .unwrap_or_default();
    Ok(ProviderLaunch {
        title: "Using provider-specific claude config:",
        detail: Some(config_file.clone()),
        program: "claude",
        args: vec![
            "--settings".to_string(),
            config_file.to_string_lossy().into_owned(),
        ],
        // 配置文件和 .bat 同在 temp_dir(%TEMP% 含用户名),用 %~dp0 引用:目录由 cmd
        // 运行时展开,不进 .bat 字节,非 ASCII 用户名下也不会乱码(#8034)。
        batch_command: format!(
            "claude --settings \"%~dp0{}\"",
            escape_windows_batch_value(&config_name)
        ),
        env: Vec::new(),
        batch_vars: Vec::new(),
        cleanup: vec![config_file],
        proxy_env: Vec::new(),
    })
}

/// Claude 官方卡绑了账号:`CLAUDE_CONFIG_DIR` 放在启动环境里(不用 `--settings`)。
/// 变量在进程启动前就设好,Claude Code 一开始预取钥匙串读的就是这个账号的条目
/// (写在 settings 里的话 macOS 上前 30 秒会用默认登录的令牌)。会把它指向别处的变量一并清掉。
/// 绑号卡的终端:清掉会改走别家服务或别的登录的变量,再指定账号目录;`None` 是账号正在默认
/// 槽位里,去掉 `CLAUDE_CONFIG_DIR` 用默认目录。
fn claude_account_terminal_launch(config_dir: Option<PathBuf>) -> ProviderLaunch {
    let mut env: Vec<(&'static str, Option<String>)> =
        crate::services::claude_account::login::CLAUDE_ACCOUNT_UNSET
            .iter()
            .map(|name| (*name, None))
            .collect();
    env.push((
        "CLAUDE_CONFIG_DIR",
        config_dir
            .as_ref()
            .map(|dir| dir.to_string_lossy().into_owned()),
    ));
    ProviderLaunch {
        title: if config_dir.is_some() {
            "Using this Claude account's config directory:"
        } else {
            "Using this Claude account via Claude Code's current sign-in."
        },
        detail: config_dir,
        program: "claude",
        args: Vec::new(),
        batch_command: "claude".to_string(),
        env,
        batch_vars: Vec::new(),
        cleanup: Vec::new(),
        proxy_env: Vec::new(),
    }
}

/// Antigravity CLI:官方登录卡跑 `agy`,卡上设了模型就加 `--model`(只对这次会话生效);
/// Key 卡用它的私有 `--gemini_dir`,Key 和地址只经环境变量传给这一个进程。
fn agy_terminal_launch(provider: &crate::provider::Provider) -> Result<ProviderLaunch, String> {
    let launch = crate::services::provider::agy_direct::terminal_launch(provider)?;
    let mut args = Vec::new();
    let mut batch_parts = vec!["agy".to_string()];
    let mut batch_vars = Vec::new();

    if let Some(dir) = &launch.gemini_dir {
        let dir_text = dir.to_string_lossy().into_owned();
        args.push(format!("--gemini_dir={dir_text}"));
        batch_parts.push(format!("\"--gemini_dir=%{AGY_DIR_BATCH_VAR}%\""));
        batch_vars.push((AGY_DIR_BATCH_VAR, dir_text));
    }
    if let Some(model) = launch.model {
        args.push(format!("--model={model}"));
        batch_parts.push(format!("\"--model=%{AGY_MODEL_BATCH_VAR}%\""));
        batch_vars.push((AGY_MODEL_BATCH_VAR, model));
    }

    let (title, detail) = if launch.key_mode {
        (
            "Using a private Antigravity profile for this provider:",
            launch.gemini_dir,
        )
    } else {
        ("Starting Antigravity CLI with your signed-in account", None)
    };

    Ok(ProviderLaunch {
        title,
        detail,
        program: "agy",
        args,
        batch_command: batch_parts.join(" "),
        env: launch.env,
        batch_vars,
        cleanup: Vec::new(),
        proxy_env: Vec::new(),
    })
}

/// 把设置里的全局代理交给终端里启动的这一个客户端(#10119)。
///
/// 全局代理只作用于 CC Switch 进程内的请求;终端里的 CLI 只认 `HTTPS_PROXY` 这类变量、
/// 不读系统代理,用户 shell 里没 export 时会直连出去。变量只给这一次启动(见
/// `ProviderLaunch::proxy_env`),用户 rc 里自己 export 的代理优先。
/// - 卡片自己配了代理(任一代理变量,不分大小写;Claude 的在临时 settings 的 `env` 里)
///   就整组不加,免得大小写两套变量指向不同代理;卡片设了 NO_PROXY 也以卡片为准。
/// - agy(Go)也传 SOCKS5,不补 NO_PROXY:Go 本来就不代理 localhost 和回环地址。
/// - 其余客户端只传 http(s):Node 写的 CLI 不支持 SOCKS,传了反而整个连不上;
///   NO_PROXY 设成本机地址,连本地路由的请求不绕进代理。
fn add_global_proxy_env<'a>(
    launch: &mut ProviderLaunch,
    app_type: &AppType,
    card_env: impl IntoIterator<Item = &'a str>,
    proxy_url: Option<&str>,
) {
    let Some(url) = proxy_url else {
        return;
    };
    let card_keys: Vec<&str> = launch
        .env
        .iter()
        .map(|(name, _)| *name)
        .chain(card_env)
        .collect();
    let card_has = |key: &str| card_keys.iter().any(|name| name.eq_ignore_ascii_case(key));
    if ["HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY"]
        .iter()
        .any(|key| card_has(key))
    {
        return;
    }
    let go_client = *app_type == AppType::Agy;
    let vars =
        crate::services::claude_account::login::global_proxy_env(url, go_client, |_| go_client);
    for (key, value) in vars {
        if !card_has(key) {
            launch.proxy_env.push((key, value));
        }
    }
}

/// 卡片配置里 `env` 的变量名(Claude 等应用的环境变量写在这里,不在 `ProviderLaunch.env`)。
fn card_env_keys(provider: &crate::provider::Provider) -> impl Iterator<Item = &str> {
    provider
        .settings_config
        .get("env")
        .and_then(|env| env.as_object())
        .into_iter()
        .flat_map(|env| env.keys().map(String::as_str))
}

/// 恢复 Antigravity 第三方 Key 模式的会话:和「打开终端」同样带私有目录和 Key,再接上
/// `--conversation`。卡上已经没有 Key 时私有目录对不上,直接报错,不退回官方登录。
pub(crate) fn open_agy_resume_terminal(
    provider: &crate::provider::Provider,
    conversation: &str,
    cwd: Option<String>,
) -> Result<(), String> {
    let mut launch = agy_terminal_launch(provider)?;
    add_global_proxy_env(
        &mut launch,
        &AppType::Agy,
        card_env_keys(provider),
        crate::proxy::http_client::get_current_proxy_url().as_deref(),
    );
    // 只有 Key 卡有私有目录(启动前打印的那行路径);官方卡的参数可能只有 `--model`。
    if launch.detail.is_none() {
        return Err(format!(
            "「{}」已不再带 API Key,无法恢复它在私有目录里的会话",
            provider.name
        ));
    }
    let resume = format!("--conversation={conversation}");
    launch.args.push(resume.clone());
    launch.batch_command = format!("{} {resume}", launch.batch_command);
    let cwd = resolve_launch_cwd(cwd)?;
    launch_provider_terminal(&launch, cwd.as_deref()).map_err(|e| format!("启动终端失败: {e}"))
}

/// 官方卡绑定的授权中心 GitHub 账号的 Token 和域名;没绑时为 `None`,跟着 Copilot CLI 自己的登录走。
async fn copilot_bound_account(
    auth: &crate::commands::CopilotAuthState,
    provider: &crate::provider::Provider,
) -> Result<Option<(String, String)>, String> {
    let Some(account_id) = crate::services::provider::copilot_direct::bound_account_id(provider)
    else {
        return Ok(None);
    };
    let credentials = auth
        .0
        .read()
        .await
        .github_credentials_for_account(&account_id)
        .await;
    match credentials {
        Some(credentials) => Ok(Some(credentials)),
        None => Err(crate::error::AppError::localized(
            "copilot.account.bound_missing",
            format!(
                "「{}」绑定的 GitHub 账号不在授权中心里:请先在授权中心添加它,或编辑这张卡改选账号",
                provider.name
            ),
            format!(
                "The GitHub account bound to \"{}\" is not in the Auth Center. Add it there, or edit the card to choose another account.",
                provider.name
            ),
        )
        .to_string()),
    }
}

/// GitHub Copilot CLI:官方卡跑 `copilot`,卡上设了模型就加 `--model`;绑了授权中心账号时
/// 经 `COPILOT_GITHUB_TOKEN` 只给这一个进程。自带模型卡要先添加进 providers.json,启动时
/// `--model=<卡片 id>/<第一个模型>`,并清掉所有 `COPILOT_PROVIDER_*`(有它们 Copilot 就不读
/// providers.json)。
fn copilot_terminal_launch(
    provider: &crate::provider::Provider,
    account: Option<(String, String)>,
) -> Result<ProviderLaunch, String> {
    let launch = crate::services::provider::copilot_direct::terminal_launch(provider)?;
    copilot_launch_command(&provider.name, launch, account)
}

fn copilot_launch_command(
    provider_name: &str,
    launch: crate::copilot_config::CopilotLaunch,
    account: Option<(String, String)>,
) -> Result<ProviderLaunch, String> {
    let mut env = launch.env;
    if !launch.byok {
        if let Some((token, domain)) = account {
            env.push(("COPILOT_GITHUB_TOKEN", Some(token)));
            env.push(("COPILOT_GH_HOST", Some(domain)));
        }
    }
    // npm 装的是 copilot.cmd:不加 `call` 跑完不回到启动 .bat,后面删临时文件(含 Key、
    // GitHub 令牌的 .bat 本身)的几行就不执行了。`call` 会把 `%` 再展开一遍、把引号里的 `^`
    // 加倍,cmd 也不认 `\"`,所以模型名里有这些字符时不在 Windows 上启动。
    // 模型名会拼进启动命令:控制字符(换行)在任何平台都拒绝,保存时已经校验过,这里兜底。
    if launch
        .model
        .as_deref()
        .is_some_and(|model| model.chars().any(char::is_control))
    {
        return Err(format!(
            "「{provider_name}」的模型名里有换行等控制字符,不能用来启动"
        ));
    }
    if cfg!(target_os = "windows")
        && launch
            .model
            .as_deref()
            .is_some_and(|model| model.contains(['"', '%', '^']))
    {
        return Err(format!(
            "「{provider_name}」的模型名里有 Windows 终端传不过去的字符(\" % ^)"
        ));
    }
    let (mut args, mut batch_command, batch_vars) = match launch.model {
        Some(model) => (
            vec![format!("--model={model}")],
            format!("call copilot \"--model=%{COPILOT_MODEL_BATCH_VAR}%\""),
            vec![(COPILOT_MODEL_BATCH_VAR, model)],
        ),
        None => (Vec::new(), "call copilot".to_string(), Vec::new()),
    };
    // 只会是 copilot_config::REASONING_EFFORTS 里的小写单词(已校验),可以直接写进命令。
    if let Some(effort) = launch.reasoning_effort {
        args.push(format!("--reasoning-effort={effort}"));
        batch_command.push_str(&format!(" --reasoning-effort={effort}"));
    }
    Ok(ProviderLaunch {
        title: if launch.byok {
            "Starting GitHub Copilot CLI with this provider's model"
        } else {
            "Starting GitHub Copilot CLI with your GitHub sign-in"
        },
        detail: None,
        program: "copilot",
        args,
        batch_command,
        env,
        batch_vars,
        cleanup: Vec::new(),
        proxy_env: Vec::new(),
    })
}

#[cfg_attr(not(target_os = "windows"), allow(dead_code))]
const COPILOT_MODEL_BATCH_VAR: &str = "CC_SWITCH_COPILOT_MODEL";

#[cfg_attr(not(target_os = "windows"), allow(dead_code))]
const AGY_DIR_BATCH_VAR: &str = "CC_SWITCH_AGY_DIR";
#[cfg_attr(not(target_os = "windows"), allow(dead_code))]
const AGY_MODEL_BATCH_VAR: &str = "CC_SWITCH_AGY_MODEL";

/// macOS / Linux 的启动脚本里,环境变量先以这个前缀的名字读进来,再由 `env` 改名交给
/// 程序:用户 shell 的 rc 文件在中间执行,直接用原名会被 rc 里的 export 覆盖。
#[cfg_attr(windows, allow(dead_code))]
const LAUNCH_ENV_PREFIX: &str = "CC_SWITCH_LAUNCH_ENV_";

/// 临时文件名里只留 ASCII 字母、数字、`-`、`_`:Windows 上文件名写进 .bat,cmd 按控制台
/// 代码页解码,供应商 id 含中文时会乱码,`claude --settings` 报找不到文件(#10156)。
/// 名字只为好认,唯一性由 [`unique_temp_path`] 的进程号、时间和序号保证。
fn ascii_file_stem(value: &str) -> String {
    value
        .chars()
        .filter(|c| c.is_ascii_alphanumeric() || matches!(c, '-' | '_'))
        .take(48)
        .collect()
}

/// 同一进程里连开几个终端也不会撞名。
fn unique_temp_path(stem: &str, ext: &str) -> PathBuf {
    use std::sync::atomic::{AtomicU64, Ordering};
    static SEQ: AtomicU64 = AtomicU64::new(0);
    let nanos = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.subsec_nanos())
        .unwrap_or_default();
    std::env::temp_dir().join(format!(
        "{stem}_{}_{nanos:x}{}.{ext}",
        std::process::id(),
        SEQ.fetch_add(1, Ordering::Relaxed)
    ))
}

/// 写只有本人能读、写、执行的启动脚本(0700):脚本里可能带代理的账号密码,Linux 的 `/tmp`
/// 别的用户也能列出和读取。路径由 [`unique_temp_path`] 生成,`create_new` 不会跟随别人预先放好的文件。
#[cfg(any(target_os = "macos", target_os = "linux"))]
fn write_private_script(path: &Path, content: &str) -> Result<(), String> {
    use std::io::Write;
    use std::os::unix::fs::OpenOptionsExt;

    let mut file = std::fs::OpenOptions::new()
        .write(true)
        .create_new(true)
        .mode(0o700)
        .open(path)
        .map_err(|e| format!("写入启动脚本失败: {e}"))?;
    file.write_all(content.as_bytes())
        .map_err(|e| format!("写入启动脚本失败: {e}"))
}

/// 根据平台打开终端运行 `launch`。
fn launch_provider_terminal(launch: &ProviderLaunch, cwd: Option<&Path>) -> Result<(), String> {
    #[cfg(any(target_os = "macos", target_os = "linux"))]
    {
        let (script_file, env_file) = write_unix_launcher(launch, cwd)?;
        #[cfg(target_os = "macos")]
        let result = launch_macos_terminal(&script_file);
        #[cfg(target_os = "linux")]
        let result = launch_linux_terminal(&script_file);
        if result.is_err() {
            for file in launch.cleanup.iter().chain([&script_file, &env_file]) {
                let _ = std::fs::remove_file(file);
            }
        }
        result
    }

    #[cfg(target_os = "windows")]
    {
        let result = launch_windows_terminal(launch, cwd);
        if result.is_err() {
            for file in &launch.cleanup {
                let _ = std::fs::remove_file(file);
            }
        }
        result
    }

    #[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
    {
        let _ = (launch, cwd);
        Err("不支持的操作系统".to_string())
    }
}

/// shell 里的一个词:只含安全字符时原样写,否则加单引号。
#[cfg_attr(windows, allow(dead_code))]
fn shell_word(value: &str) -> String {
    let plain = !value.is_empty()
        && value
            .chars()
            .all(|c| c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '.' | '/' | ':' | '='));
    if plain {
        value.to_string()
    } else {
        shell_single_quote(value)
    }
}

/// 交给用户 shell 执行的启动命令。有环境变量时经 `env` 设置或清除,值从启动脚本读进来的
/// `CC_SWITCH_LAUNCH_ENV_<n>` 取,不出现在命令行里。
#[cfg_attr(windows, allow(dead_code))]
#[cfg(test)]
fn unix_launch_command(launch: &ProviderLaunch) -> String {
    unix_launch_command_in(launch, "/bin/sh")
}

/// 同上,参数按用户 shell 的规则加引号:这条命令由用户 shell 解析(见
/// `build_provider_command_line`),附加参数又是用户随手填的文本。
#[cfg_attr(windows, allow(dead_code))]
fn unix_launch_command_in(launch: &ProviderLaunch, shell: &str) -> String {
    let mut words = Vec::new();
    if !launch.env.is_empty() {
        words.push("env".to_string());
        for (index, (name, value)) in launch.env.iter().enumerate() {
            // 中转用的变量也不留给程序。
            words.push(format!("-u {LAUNCH_ENV_PREFIX}{index}"));
            if value.is_none() {
                words.push(format!("-u {name}"));
            }
        }
        for (index, (name, value)) in launch.env.iter().enumerate() {
            if value.is_some() {
                words.push(format!("{name}=\"${LAUNCH_ENV_PREFIX}{index}\""));
            }
        }
    }
    words.push(launch.program.to_string());
    words.extend(launch.args.iter().map(|arg| user_shell_word(shell, arg)));
    words.join(" ")
}

/// 0600 环境变量文件的内容,启动脚本 `.` 进来后立即删除。
#[cfg_attr(windows, allow(dead_code))]
fn unix_launch_env_file(launch: &ProviderLaunch) -> String {
    launch
        .env
        .iter()
        .enumerate()
        .filter_map(|(index, (_, value))| {
            value
                .as_ref()
                .map(|value| format!("{LAUNCH_ENV_PREFIX}{index}={}\n", shell_single_quote(value)))
        })
        .chain(
            launch
                .proxy_env
                .iter()
                .map(|(name, value)| format!("{name}={}\n", shell_single_quote(value))),
        )
        .collect()
}

/// 启动脚本:读进环境变量文件并删掉它,跑启动命令,删掉临时文件,最后换成用户的交互 shell。
#[cfg_attr(windows, allow(dead_code))]
fn build_unix_launcher_script(
    launch: &ProviderLaunch,
    shell: &str,
    cwd: Option<&Path>,
    script_file: &Path,
    env_file: Option<&Path>,
) -> String {
    let mut temp_files: Vec<String> = launch
        .cleanup
        .iter()
        .map(PathBuf::as_path)
        .chain(env_file)
        .map(|file| shell_single_quote(&file.to_string_lossy()))
        .collect();
    temp_files.push(shell_single_quote(&script_file.to_string_lossy()));
    let remove_temp_files = format!("rm -f -- {}", temp_files.join(" "));

    let mut script = String::from("#!/usr/bin/env sh\n");
    script.push_str(&format!(
        "trap {} EXIT\n",
        shell_single_quote(&remove_temp_files)
    ));
    if let Some(env_file) = env_file {
        let quoted = shell_single_quote(&env_file.to_string_lossy());
        script.push_str(&format!("set -a\n. {quoted}\nset +a\nrm -f -- {quoted}\n"));
    }
    script.push_str(&format!("echo {}\n", shell_single_quote(launch.title)));
    if let Some(detail) = &launch.detail {
        script.push_str(&format!(
            "echo {}\n",
            shell_single_quote(&detail.to_string_lossy())
        ));
    }
    script.push_str(&build_provider_command_line(
        shell,
        &unix_launch_command_in(launch, shell),
        cwd,
    ));
    script.push('\n');
    // exec 不会触发 EXIT trap,所以在换成交互 shell 之前自己删。
    script.push_str(&remove_temp_files);
    script.push('\n');
    // 代理只给这一次启动,不留给之后的交互 shell(它自己会按 rc 重新设置)。
    let launch_vars: Vec<String> = (0..launch.env.len())
        .map(|index| format!("{LAUNCH_ENV_PREFIX}{index}"))
        .chain(launch.proxy_env.iter().map(|(name, _)| name.to_string()))
        .collect();
    if !launch_vars.is_empty() {
        script.push_str(&format!("unset {}\n", launch_vars.join(" ")));
    }
    script.push_str(&build_final_shell_cd_command(shell, cwd));
    script.push_str(&build_exec_line(shell, cwd));
    script.push('\n');
    script
}

/// 写好启动脚本(和需要时的 0600 环境变量文件),返回两者路径。
#[cfg(any(target_os = "macos", target_os = "linux"))]
fn write_unix_launcher(
    launch: &ProviderLaunch,
    cwd: Option<&Path>,
) -> Result<(PathBuf, PathBuf), String> {
    use std::os::unix::fs::PermissionsExt;

    let shell = get_user_shell();
    let script_file = unique_temp_path("cc_switch_launcher", "sh");
    let env_file = unique_temp_path("cc_switch_launch_env", "sh");
    let env_content = unix_launch_env_file(launch);
    let has_env = !env_content.is_empty();
    if has_env {
        crate::config::atomic_write_private(&env_file, env_content.as_bytes())
            .map_err(|e| format!("写入启动环境失败: {e}"))?;
    }
    let script_content = build_unix_launcher_script(
        launch,
        &shell,
        cwd,
        &script_file,
        has_env.then_some(env_file.as_path()),
    );
    std::fs::write(&script_file, &script_content).map_err(|e| format!("写入启动脚本失败: {e}"))?;
    std::fs::set_permissions(&script_file, std::fs::Permissions::from_mode(0o700))
        .map_err(|e| format!("设置脚本权限失败: {e}"))?;
    Ok((script_file, env_file))
}

/// 写入 claude 配置文件(含凭据,0600)
///
/// 这个文件叠在用户级 `settings.json` 上:`env` 按键合并、顶层按键覆盖。所以:
/// - `modelPicker` 一定要写:它按层级整个胜出,不写的话路由 / 聚合模式的菜单会漏进这个
///   终端,选了就把别家的模型发给这张卡;
/// - 切换会清掉、卡上又没有的键(`drops`)写成空值挡住:Claude Code 把空串当没设(2.1.314
///   实测:代理的占位凭据不再随请求发出,没配的档位回到内置模型)。
fn write_claude_config(
    config_file: &std::path::Path,
    env_vars: &[(String, String)],
    provider_name: &str,
    drops: &crate::services::provider::SwitchDrops,
) -> Result<(), String> {
    use serde_json::Value;
    let mut config_obj = serde_json::Map::new();
    let mut env_obj = serde_json::Map::new();

    for (key, value) in env_vars {
        env_obj.insert(key.clone(), Value::String(value.clone()));
    }

    let model_picker = crate::live::project::claude::terminal_model_picker(&env_obj, provider_name);
    for key in drops.env.keys() {
        env_obj
            .entry(key.clone())
            .or_insert_with(|| Value::String(String::new()));
    }
    for (key, value) in &drops.top {
        let empty = match value {
            Value::String(_) => Value::String(String::new()),
            Value::Object(_) => Value::Object(serde_json::Map::new()),
            Value::Array(_) => Value::Array(Vec::new()),
            _ => continue,
        };
        config_obj.insert(key.clone(), empty);
    }
    config_obj.insert("env".to_string(), Value::Object(env_obj));
    config_obj.insert("modelPicker".to_string(), model_picker);

    let config_json =
        serde_json::to_string_pretty(&config_obj).map_err(|e| format!("序列化配置失败: {e}"))?;

    crate::config::atomic_write_private(config_file, config_json.as_bytes())
        .map_err(|e| format!("写入配置文件失败: {e}"))
}

/// macOS: 根据用户首选终端启动
#[cfg(target_os = "macos")]
fn launch_macos_terminal(script_file: &std::path::Path) -> Result<(), String> {
    let preferred = crate::settings::get_preferred_terminal();
    let terminal = preferred.as_deref().unwrap_or("terminal");
    let script_file = script_file.to_path_buf();

    // Try the preferred terminal first, fall back to Terminal.app if it fails
    // Note: Kitty doesn't need the -e flag, others do
    let result = match terminal {
        "iterm2" => launch_macos_iterm2(&script_file),
        "warp" => launch_macos_warp(&script_file),
        "alacritty" => launch_macos_open_app("Alacritty", &script_file, true),
        "kitty" => launch_macos_open_app("kitty", &script_file, false),
        "ghostty" => launch_macos_ghostty(&script_file),
        "otty" => launch_macos_otty(&script_file),
        "wezterm" => launch_macos_open_app("WezTerm", &script_file, true),
        "kaku" => launch_macos_open_app("Kaku", &script_file, true),
        _ => launch_macos_terminal_app(&script_file),
    };

    // If preferred terminal fails and it's not the default, try Terminal.app as fallback
    if result.is_err() && terminal != "terminal" {
        log::warn!(
            "首选终端 {} 启动失败,回退到 Terminal.app: {:?}",
            terminal,
            result.as_ref().err()
        );
        return launch_macos_terminal_app(&script_file);
    }

    result
}

/// Escape a value as an AppleScript string literal.
#[cfg(target_os = "macos")]
fn applescript_string_literal(value: &str) -> String {
    format!("\"{}\"", value.replace('\\', "\\\\").replace('"', "\\\""))
}

/// Build the launcher command literal used by AppleScript.
#[cfg(target_os = "macos")]
fn applescript_launcher_command(script_file: &std::path::Path) -> String {
    applescript_string_literal(&format!(
        "sh {}",
        shell_single_quote(&script_file.to_string_lossy())
    ))
}

/// Build a launcher command that replaces the terminal-created shell session.
#[cfg(target_os = "macos")]
fn applescript_exec_launcher_command(script_file: &std::path::Path) -> String {
    applescript_string_literal(&format!(
        "exec sh {}",
        shell_single_quote(&script_file.to_string_lossy())
    ))
}

/// macOS: Terminal.app AppleScript.
/// A cold `activate` creates a default empty window before `do script` opens the command session.
/// Use `launch` for cold starts so `do script` can create the only new session without reusing restored windows.
#[cfg(target_os = "macos")]
fn build_macos_terminal_applescript(script_file: &std::path::Path) -> String {
    format!(
        r#"set launcher_script to {launcher}
set was_running to application "Terminal" is running
tell application "Terminal"
    if was_running then
        activate
        do script launcher_script
    else
        launch
        do script launcher_script
        activate
    end if
end tell"#,
        launcher = applescript_exec_launcher_command(script_file)
    )
}

/// Run AppleScript through `osascript -e` with shared error handling.
#[cfg(target_os = "macos")]
fn run_terminal_osascript(applescript: &str, terminal_label: &str) -> Result<(), String> {
    use std::process::Command;

    let output = Command::new("osascript")
        .arg("-e")
        .arg(applescript)
        .output()
        .map_err(|e| format!("执行 osascript 失败: {e}"))?;

    if !output.status.success() {
        let stderr = decode_command_output(&output.stderr);
        return Err(format!(
            "{terminal_label} 执行失败 (exit code: {:?}): {}",
            output.status.code(),
            stderr
        ));
    }

    Ok(())
}

/// macOS: Terminal.app
#[cfg(target_os = "macos")]
fn launch_macos_terminal_app(script_file: &std::path::Path) -> Result<(), String> {
    run_terminal_osascript(
        &build_macos_terminal_applescript(script_file),
        "Terminal.app",
    )
}

#[cfg(target_os = "macos")]
fn launch_macos_otty(script_file: &std::path::Path) -> Result<(), String> {
    use std::process::Command;

    let otty_cli = find_macos_otty_cli().ok_or_else(|| {
        "未找到 Otty CLI。请将 Otty 安装到 /Applications 或 ~/Applications。".to_string()
    })?;

    let command = build_macos_dash_c_command(script_file);
    let tab_result = Command::new(&otty_cli)
        .args(["tab", "new", "--window", "0", "--command", &command])
        .output()
        .map_err(|e| format!("启动 Otty CLI 失败: {e}"))?;

    if tab_result.status.success() {
        return Ok(());
    }

    log::debug!(
        "Otty 新建 Tab 失败,改为新建窗口: {}",
        decode_command_output(&tab_result.stderr)
    );

    let window_result = Command::new(&otty_cli)
        .args(["open", "--command", &command])
        .output()
        .map_err(|e| format!("启动 Otty CLI 失败: {e}"))?;

    if window_result.status.success() {
        Ok(())
    } else {
        Err(format!(
            "Otty 新建窗口失败 (exit code: {:?}): {}",
            window_result.status.code(),
            decode_command_output(&window_result.stderr)
        ))
    }
}

#[cfg(target_os = "macos")]
fn find_macos_otty_cli() -> Option<std::path::PathBuf> {
    macos_otty_cli_candidates()
        .into_iter()
        .find(|path| path.is_file() && is_executable_file(path))
}

#[cfg(target_os = "macos")]
fn macos_otty_cli_candidates() -> Vec<std::path::PathBuf> {
    let mut candidates = vec![std::path::PathBuf::from(
        "/Applications/Otty.app/Contents/MacOS/otty-cli",
    )];

    if let Some(home) = std::env::var_os("HOME") {
        candidates.push(
            std::path::PathBuf::from(home).join("Applications/Otty.app/Contents/MacOS/otty-cli"),
        );
    }

    candidates.push(std::path::PathBuf::from("/usr/local/bin/otty"));
    candidates.push(std::path::PathBuf::from("/opt/homebrew/bin/otty"));

    if let Some(path) = std::env::var_os("PATH") {
        for directory in std::env::split_paths(&path) {
            candidates.push(directory.join("otty"));
            candidates.push(directory.join("otty-cli"));
        }
    }

    candidates
}

/// macOS: iTerm2
#[cfg(target_os = "macos")]
fn build_macos_iterm2_applescript(script_file: &std::path::Path) -> String {
    format!(
        r#"set launcher_script to {launcher}
set was_running to application "iTerm" is running
tell application "iTerm"
    if was_running then
        activate
        if (count of windows) = 0 then
            create window with default profile
        else
            tell current window
                create tab with default profile
            end tell
        end if
    else
        activate
        set waited to 0
        repeat while (count of windows) = 0
            delay 0.1
            set waited to waited + 1
            if waited >= 30 then exit repeat
        end repeat
        if (count of windows) = 0 then
            create window with default profile
        end if
    end if
    tell current session of current window
        write text launcher_script
    end tell
end tell"#,
        launcher = applescript_exec_launcher_command(script_file)
    )
}

/// macOS: iTerm2
#[cfg(target_os = "macos")]
fn launch_macos_iterm2(script_file: &std::path::Path) -> Result<(), String> {
    run_terminal_osascript(&build_macos_iterm2_applescript(script_file), "iTerm2")
}

/// Keep the launcher path inside a `sh -c` string.
/// A bare `.sh` passed through `open --args` may also be opened as a document.
#[cfg(target_os = "macos")]
fn build_macos_dash_c_command(script_file: &std::path::Path) -> String {
    format!(
        "exec sh {}",
        shell_single_quote(&script_file.to_string_lossy())
    )
}

/// macOS: Ghostty.
/// Warm starts use AppleScript to create one command window.
/// Cold starts use `initial-command` so the first default surface runs the launcher.
/// Do not use `initial-window=false` plus `new window`: cold launch can still create the default window first.
#[cfg(target_os = "macos")]
fn build_macos_ghostty_applescript(script_file: &std::path::Path) -> String {
    format!(
        r#"set launcher_command to {launcher}
set was_running to application "Ghostty" is running
if was_running then
    tell application "Ghostty"
        activate
        new window with configuration {{command:launcher_command}}
    end tell
else
    do shell script "open -na Ghostty --args --quit-after-last-window-closed=true " & quoted form of ("--initial-command=" & launcher_command)
end if
"#,
        launcher = applescript_launcher_command(script_file)
    )
}

/// macOS: Ghostty
#[cfg(target_os = "macos")]
fn launch_macos_ghostty(script_file: &std::path::Path) -> Result<(), String> {
    match run_terminal_osascript(&build_macos_ghostty_applescript(script_file), "Ghostty") {
        Ok(()) => Ok(()),
        Err(applescript_error) => {
            log::warn!(
                "Ghostty AppleScript launch failed, falling back to open -na: {applescript_error}"
            );
            launch_macos_open_app("Ghostty", script_file, true)
        }
    }
}

/// macOS: 使用 open -na 启动支持 --args 参数的终端(Alacritty/Kitty/WezTerm/Kaku)
#[cfg(target_os = "macos")]
fn launch_macos_open_app(
    app_name: &str,
    script_file: &std::path::Path,
    use_e_flag: bool,
) -> Result<(), String> {
    use std::process::Command;

    let mut cmd = Command::new("open");
    cmd.arg("-na").arg(app_name).arg("--args");

    if use_e_flag {
        cmd.arg("-e");
    }
    // Keep the script path inside `sh -c`; a trailing bare `.sh` can be opened as a document.
    cmd.arg("sh")
        .arg("-c")
        .arg(build_macos_dash_c_command(script_file));

    let output = cmd
        .output()
        .map_err(|e| format!("启动 {app_name} 失败: {e}"))?;

    if !output.status.success() {
        let stderr = decode_command_output(&output.stderr);
        return Err(format!(
            "{} 启动失败 (exit code: {:?}): {}",
            app_name,
            output.status.code(),
            stderr
        ));
    }

    Ok(())
}

#[cfg(target_os = "macos")]
fn launch_macos_warp(script_file: &std::path::Path) -> Result<(), String> {
    use std::io::Write;
    use std::os::unix::fs::PermissionsExt;
    use std::process::Command;

    let mut cmd = Command::new("open");
    cmd.arg("-a").arg("Warp");

    // Warp URI scheme cannot work well with script_file, because:
    //
    // 1. script_file's name ends up with .sh, so Warp would open the file rather than execute it
    // 2. script_file has no execution permission, so we need to add one more indirection
    let mut second_script_file = tempfile::Builder::new()
        .disable_cleanup(true)
        .permissions(std::fs::Permissions::from_mode(0o755))
        .tempfile()
        .map_err(|e| format!("Failed to create temporary script file: {e}"))?;

    writeln!(
        &mut second_script_file,
        r#"#!/usr/bin/env sh

        rm -- "$0"

        exec sh {quoted_script}
        "#,
        quoted_script = shell_single_quote(&script_file.to_string_lossy()),
    )
    .map_err(|e| format!("Failed to write to temporary script file for Warp: {e}"))?;

    let mut warp_url = url::Url::parse("warp://action/new_tab").unwrap();
    warp_url
        .query_pairs_mut()
        .append_pair("path", &second_script_file.path().to_string_lossy());
    let warp_url = warp_url.to_string();
    cmd.arg(warp_url);

    let output = cmd.output().map_err(|e| format!("启动 Warp 失败: {e}"))?;
    if !output.status.success() {
        let stderr = decode_command_output(&output.stderr);
        return Err(format!(
            "Warp 启动失败 (exit code: {:?}): {}",
            output.status.code(),
            stderr
        ));
    }

    Ok(())
}

/// Linux: 根据用户首选终端启动
#[cfg(target_os = "linux")]
fn linux_terminal_candidates(preferred: Option<&str>) -> Vec<(String, Vec<&'static str>)> {
    let default_terminals = [
        ("gnome-terminal", vec!["--"]),
        ("konsole", vec!["-e"]),
        ("xfce4-terminal", vec!["-e"]),
        ("mate-terminal", vec!["--"]),
        ("lxterminal", vec!["-e"]),
        ("alacritty", vec!["-e"]),
        ("kitty", vec!["-e"]),
        ("ghostty", vec!["-e"]),
        ("x-terminal-emulator", vec!["-e"]),
        ("xdg-terminal-exec", vec!["--"]),
    ];

    if let Some(preferred) = preferred {
        let pref_args = default_terminals
            .iter()
            .find(|(name, _)| *name == preferred)
            .map(|(_, args)| args.to_vec())
            .unwrap_or_else(|| vec!["-e"]);

        let mut list = vec![(preferred.to_string(), pref_args)];
        for (name, args) in &default_terminals {
            if *name != preferred {
                list.push(((*name).to_string(), args.to_vec()));
            }
        }
        list
    } else {
        default_terminals
            .iter()
            .map(|(name, args)| ((*name).to_string(), args.to_vec()))
            .collect()
    }
}

#[cfg(target_os = "linux")]
fn launch_linux_terminal(script_file: &std::path::Path) -> Result<(), String> {
    use std::process::Command;

    let preferred = crate::settings::get_preferred_terminal();
    let terminals_to_try = linux_terminal_candidates(preferred.as_deref());

    let mut last_error = String::from("未找到可用的终端");

    for (terminal, args) in terminals_to_try {
        // Check if terminal exists in common paths
        let terminal_exists = std::path::Path::new(&format!("/usr/bin/{}", terminal)).exists()
            || std::path::Path::new(&format!("/bin/{}", terminal)).exists()
            || std::path::Path::new(&format!("/usr/local/bin/{}", terminal)).exists()
            || which_command(&terminal);

        if terminal_exists {
            let result = Command::new(&terminal)
                .args(&args)
                .arg("sh")
                .arg(script_file.to_string_lossy().as_ref())
                .spawn();

            match result {
                Ok(_) => return Ok(()),
                Err(e) => {
                    last_error = format!("执行 {} 失败: {}", terminal, e);
                }
            }
        }
    }

    Err(last_error)
}

/// Check if a command exists using `which`
#[cfg(target_os = "linux")]
fn which_command(cmd: &str) -> bool {
    use std::process::Command;
    Command::new("which")
        .arg(cmd)
        .output()
        .map(|o| o.status.success())
        .unwrap_or(false)
}

/// Windows: 根据用户首选终端启动
#[cfg(target_os = "windows")]
fn launch_windows_terminal(launch: &ProviderLaunch, cwd: Option<&Path>) -> Result<(), String> {
    let preferred = crate::settings::get_preferred_terminal();
    let terminal = preferred.as_deref().unwrap_or("cmd");

    let bat_file = unique_temp_path("cc_switch_launcher", "bat");
    let (cwd_command, cwd_env) = build_windows_cwd_command(cwd);
    let mut launch_env: Vec<(String, String)> = cwd_env
        .map(|value| (WINDOWS_CWD_ENV.to_string(), value))
        .into_iter()
        .collect();
    let (set_lines, extra_env) = build_windows_launch_env(launch);
    launch_env.extend(extra_env);
    let launch_env: Vec<(&str, &str)> = launch_env
        .iter()
        .map(|(key, value)| (key.as_str(), value.as_str()))
        .collect();
    let cleanup: String = launch
        .cleanup
        .iter()
        .filter_map(|file| file.file_name())
        .map(|name| {
            format!(
                "del \"%~dp0{}\" >nul 2>&1\r\n",
                escape_windows_batch_value(&name.to_string_lossy())
            )
        })
        .collect();

    // setlocal 段里设的变量(含 Key)在程序退出后随 endlocal 撤掉,不留在这个 cmd 会话里。
    // 注册表里开了延迟展开的机器上,值里的 `!名字!` 会被替换:这一段显式关掉。
    let content = format!(
        "@echo off\r\n{cwd_command}setlocal DisableDelayedExpansion\r\n{set_lines}echo {title}\r\n{command}\r\nendlocal\r\n{cleanup}del \"%~f0\" >nul 2>&1\r\n",
        title = escape_windows_batch_value(launch.title),
        command = launch.batch_command,
    );

    std::fs::write(&bat_file, &content).map_err(|e| format!("写入批处理文件失败: {e}"))?;

    let bat_path = bat_file.to_string_lossy();
    let ps_cmd = format!("& '{}'", bat_path);

    // Try the preferred terminal first
    let result = match terminal {
        "powershell" => run_windows_start_command_with_env(
            &[
                windows_powershell_program(&effective_path_string()),
                "-NoExit",
                "-Command",
                &ps_cmd,
            ],
            "PowerShell",
            &launch_env,
        ),
        "wt" => run_windows_start_command_with_env(
            &["wt", "cmd", "/K", &bat_path],
            "Windows Terminal",
            &launch_env,
        ),
        // "cmd" or default
        _ => run_windows_start_command_with_env(&["cmd", "/K", &bat_path], "cmd", &launch_env),
    };

    // If preferred terminal fails and it's not the default, try cmd as fallback
    if result.is_err() && terminal != "cmd" {
        log::warn!(
            "首选终端 {} 启动失败,回退到 cmd: {:?}",
            terminal,
            result.as_ref().err()
        );
        return run_windows_start_command_with_env(&["cmd", "/K", &bat_path], "cmd", &launch_env);
    }

    result
}

/// fish 的单引号里 `\'` 和 `\\` 仍是转义,POSIX 的 `'"'"'` 写法会让引号错位、接出别的命令。
#[cfg_attr(windows, allow(dead_code))]
fn is_fish_shell(shell: &str) -> bool {
    shell.rsplit('/').next().unwrap_or(shell) == "fish"
}

/// 交给用户 shell(而不是启动脚本的 sh)解析的一段文本加引号。
#[cfg_attr(windows, allow(dead_code))]
fn user_shell_quote(shell: &str, value: &str) -> String {
    if is_fish_shell(shell) {
        format!("'{}'", value.replace('\\', "\\\\").replace('\'', "\\'"))
    } else {
        shell_single_quote(value)
    }
}

/// `shell_word` 的用户 shell 版本:只含安全字符时原样写。
#[cfg_attr(windows, allow(dead_code))]
fn user_shell_word(shell: &str, value: &str) -> String {
    let word = shell_word(value);
    // zsh 把开头的 `=名字` 展开成命令路径(`=sh` → `/bin/sh`),找不到还会直接报错。
    if word == value && !value.starts_with('=') {
        word
    } else {
        user_shell_quote(shell, value)
    }
}

#[cfg_attr(not(target_os = "windows"), allow(dead_code))]
fn is_windows_unc_path(path: &str) -> bool {
    path.starts_with(r"\\")
}

/// .bat 里 `setlocal` 段的 `set` 行,以及要经启动进程环境传入的变量。纯 ASCII 的值直接
/// 写进文件(`%` 双写);含非 ASCII 的按 #8034 的办法经环境变量转一手,免得按代码页乱码。
#[cfg_attr(not(target_os = "windows"), allow(dead_code))]
fn build_windows_launch_env(launch: &ProviderLaunch) -> (String, Vec<(String, String)>) {
    let mut lines = String::new();
    let mut carried = Vec::new();
    let entries = launch
        .env
        .iter()
        .map(|(name, value)| (*name, value.clone()))
        .chain(
            launch
                .batch_vars
                .iter()
                .chain(&launch.proxy_env)
                .map(|(name, value)| (*name, Some(value.clone()))),
        );
    for (index, (name, value)) in entries.enumerate() {
        match value {
            None => lines.push_str(&format!("set \"{name}=\"\r\n")),
            Some(value) if value.is_ascii() => {
                lines.push_str(&format!("set \"{name}={}\"\r\n", value.replace('%', "%%")))
            }
            Some(value) => {
                let carrier = format!("CC_SWITCH_INTERNAL_ENV_{index}");
                lines.push_str(&format!(
                    "set \"{name}=%{carrier}%\"\r\nset \"{carrier}=\"\r\n"
                ));
                carried.push((carrier, value));
            }
        }
    }
    (lines, carried)
}

/// 非 ASCII 的项目目录经这个环境变量传给 Claude 启动 .bat。
#[cfg_attr(not(target_os = "windows"), allow(dead_code))]
const WINDOWS_CWD_ENV: &str = "CC_SWITCH_INTERNAL_CWD";

/// 生成切到项目目录的 .bat 行,第二项是需要给启动进程设置的 `WINDOWS_CWD_ENV` 值。
///
/// cmd 按控制台代码页解码 .bat 字节,非 ASCII 路径以 UTF-8 写进文件会乱码(#8034);
/// 环境变量在 Windows 内部保持 Unicode,所以这类路径改由环境变量传入。纯 ASCII
/// 路径照旧写进文件,不依赖终端继承环境变量(旧版 Windows Terminal 不继承)。
#[cfg_attr(not(target_os = "windows"), allow(dead_code))]
fn build_windows_cwd_command_str(path: &str) -> (String, Option<String>) {
    let (target, env_value, cleanup) = if path.is_ascii() {
        (escape_windows_batch_value(path), None, String::new())
    } else {
        (
            format!("%{WINDOWS_CWD_ENV}%"),
            Some(path.to_string()),
            format!("set \"{WINDOWS_CWD_ENV}=\"\r\n"),
        )
    };

    let command = if is_windows_unc_path(path) {
        // `cmd.exe` cannot make a UNC path current via `cd`; `pushd` maps it first.
        format!("pushd \"{target}\" || exit /b 1\r\n{cleanup}")
    } else {
        format!("cd /d \"{target}\" || exit /b 1\r\n{cleanup}")
    };
    (command, env_value)
}

#[cfg(target_os = "windows")]
fn build_windows_cwd_command(cwd: Option<&Path>) -> (String, Option<String>) {
    cwd.map(|dir| build_windows_cwd_command_str(&dir.to_string_lossy()))
        .unwrap_or_default()
}

#[cfg_attr(not(target_os = "windows"), allow(dead_code))]
fn escape_windows_batch_value(value: &str) -> String {
    value
        .replace('^', "^^")
        .replace('%', "%%")
        .replace('&', "^&")
        .replace('|', "^|")
        .replace('<', "^<")
        .replace('>', "^>")
        .replace('(', "^(")
        .replace(')', "^)")
}
/// Windows: Run a start command with common error handling
#[cfg(target_os = "windows")]
fn run_windows_start_command(args: &[&str], terminal_name: &str) -> Result<(), String> {
    run_windows_start_command_with_env(args, terminal_name, &[])
}

/// 组装 `cmd /C start ...`。终端里跑的 `claude` 等命令按这里的 PATH 查找:CC Switch
/// 被 MSI 自动重启后只剩机器级 PATH(#6061),或是在装 CLI 之前就已启动,进程 PATH
/// 都会缺用户级条目(#3877、#8446)。和检测、生命周期命令(#8173)一样用补全后的 PATH,
/// 只影响启动的终端,不改 CC Switch 自身的环境;Windows Terminal 从命令行开新标签时
/// 也沿用调用方的环境。
#[cfg(target_os = "windows")]
fn windows_start_command(
    args: &[&str],
    envs: &[(&str, &str)],
    effective_path: &str,
) -> std::process::Command {
    let mut command = std::process::Command::new("cmd");
    command
        .arg("/C")
        .arg("start")
        .args(args)
        .env("PATH", effective_path)
        .envs(envs.iter().copied())
        .creation_flags(CREATE_NO_WINDOW);
    command
}

/// 同 `run_windows_start_command`,额外给启动的终端设置环境变量(`start` 会继承)。
#[cfg(target_os = "windows")]
fn run_windows_start_command_with_env(
    args: &[&str],
    terminal_name: &str,
    envs: &[(&str, &str)],
) -> Result<(), String> {
    let command = windows_start_command(args, envs, &effective_path_string());
    run_terminal_command(command, terminal_name, &[])
}

/// 启动给好的终端命令,并从终端环境里删掉一些变量(`start` 会继承删减后的环境)。
///
/// stdio 指向 NUL、只等 `cmd` 的退出码,不能用 `output()` 捕获:`start` 打开的终端会
/// 继承这里的 stdout/stderr 管道句柄,管道要等那个窗口和里面的进程全部退出才到 EOF,
/// 调用就一直挂到用户关掉终端,界面也一直转圈(#6712)。启动失败仍能从退出码看出来。
#[cfg(target_os = "windows")]
fn run_terminal_command(
    mut command: std::process::Command,
    terminal_name: &str,
    env_removals: &[&str],
) -> Result<(), String> {
    use std::process::Stdio;

    for key in env_removals {
        command.env_remove(key);
    }
    let status = command
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .status()
        .map_err(|e| format!("启动 {} 失败: {e}", terminal_name))?;

    if !status.success() {
        return Err(format!(
            "{} 启动失败 (exit code: {:?})",
            terminal_name,
            status.code()
        ));
    }

    Ok(())
}

/// 路径在 cmd / PowerShell 分支里经环境变量传给终端(`start` 会继承),变量名只在子进程环境里生效。
#[cfg(target_os = "windows")]
const CMD_TERMINAL_PROGRAM_VAR: &str = "CC_SWITCH_TERMINAL_PROGRAM";

#[cfg(target_os = "windows")]
const WINDOWS_EXIT_PIPE_PREFIX: &str = "cc-switch-terminal-exit-";
#[cfg(target_os = "windows")]
const WINDOWS_EXIT_PIPE_CONNECT_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(60);
/// 略长于 `agy_account::LOGIN_TIMEOUT`(10 分钟),避免超时后才关窗留下迟到的退出码文件。
#[cfg(target_os = "windows")]
const WINDOWS_EXIT_PIPE_WATCH_TIMEOUT: std::time::Duration =
    std::time::Duration::from_secs(11 * 60);

#[cfg(target_os = "windows")]
struct WindowsExitPipe {
    pipe_name: String,
    server: tokio::net::windows::named_pipe::NamedPipeServer,
    exit_file: PathBuf,
}

#[cfg(target_os = "windows")]
impl WindowsExitPipe {
    fn bind(exit_file: PathBuf) -> std::io::Result<Self> {
        let pipe_name = format!("{WINDOWS_EXIT_PIPE_PREFIX}{}", uuid::Uuid::new_v4());
        let pipe_path = format!(r"\\.\pipe\{pipe_name}");
        let server = tokio::net::windows::named_pipe::ServerOptions::new()
            .first_pipe_instance(true)
            .max_instances(1)
            .access_inbound(true)
            .access_outbound(false)
            .reject_remote_clients(true)
            .create(&pipe_path)?;
        Ok(Self {
            pipe_name,
            server,
            exit_file,
        })
    }

    fn pipe_name(&self) -> &str {
        &self.pipe_name
    }

    fn spawn_watcher(self) {
        tokio::spawn(watch_windows_exit_pipe(
            self.server,
            self.exit_file,
            WINDOWS_EXIT_PIPE_CONNECT_TIMEOUT,
            WINDOWS_EXIT_PIPE_WATCH_TIMEOUT,
        ));
    }
}

#[cfg(target_os = "windows")]
async fn watch_windows_exit_pipe(
    server: tokio::net::windows::named_pipe::NamedPipeServer,
    exit_file: PathBuf,
    connect_timeout: std::time::Duration,
    watch_timeout: std::time::Duration,
) {
    match tokio::time::timeout(connect_timeout, server.connect()).await {
        Ok(Ok(())) => {}
        Ok(Err(err)) => {
            log::debug!("[TerminalLaunch] 退出码管道等待连接失败: {err}");
            return;
        }
        Err(_) => {
            log::debug!("[TerminalLaunch] 退出码管道等待连接超时,放弃本次监听");
            return;
        }
    }
    let status = match tokio::time::timeout(watch_timeout, read_pipe_exit_line(&server)).await {
        Ok(Some(code)) => format!("{code}\n"),
        Ok(None) => "closed\n".to_string(),
        Err(_) => return,
    };
    let _ = tokio::task::spawn_blocking(move || write_exit_status_file(&exit_file, &status)).await;
}

#[cfg(target_os = "windows")]
async fn read_pipe_exit_line(
    server: &tokio::net::windows::named_pipe::NamedPipeServer,
) -> Option<String> {
    let mut buf = Vec::new();
    let mut chunk = [0u8; 64];
    loop {
        if server.readable().await.is_err() {
            break;
        }
        match server.try_read(&mut chunk) {
            Ok(0) => break,
            Ok(n) => {
                buf.extend_from_slice(&chunk[..n]);
                if buf.contains(&b'\n') || buf.len() >= 128 {
                    break;
                }
            }
            Err(err) if err.kind() == std::io::ErrorKind::WouldBlock => continue,
            Err(_) => break,
        }
    }
    let text = String::from_utf8_lossy(&buf);
    let first = text.lines().next().map(str::trim).unwrap_or("");
    if first.is_empty() {
        None
    } else {
        Some(first.to_string())
    }
}

#[cfg(target_os = "windows")]
fn write_exit_status_file(path: &Path, status: &str) {
    let tmp = PathBuf::from(format!("{}.tmp", path.to_string_lossy()));
    if std::fs::write(&tmp, status).is_ok() {
        if std::fs::rename(&tmp, path).is_err() {
            let _ = std::fs::remove_file(&tmp);
            let _ = std::fs::write(path, status);
        }
    } else {
        let _ = std::fs::write(path, status);
    }
}

/// 把要经外层 `cmd /C start ...` 透传给内层命令的文本做 `^` 转义,避免外层 `cmd`
/// 提前展开 `%...%`、切开 `& | < > ( )` 或把 `""` 拆成空引号串。
#[cfg(target_os = "windows")]
fn escape_outer_cmd_raw_arg(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for ch in s.chars() {
        if matches!(
            ch,
            '^' | '&' | '|' | '<' | '>' | '(' | ')' | '%' | '"' | '!'
        ) {
            out.push('^');
        }
        out.push(ch);
    }
    out
}

/// 构造内层 `cmd /K` 后面紧跟的原始命令行(多包一层外侧双引号),并转义给外层
/// `cmd /C start` 原样透传。
///
/// `cmd /?` 的引号规则:`/K` 后首字符是引号且总引号数不为 2(或含特殊字符)时,
/// 剥掉首尾各一个引号、保留中间的。所以把 `"%VAR%" [args...]` 再包一层双引号,
/// 内层 `cmd /K` 剥完外层双引号后剩下的正是带引号的 `%VAR%` 与参数;内层展开
/// `%VAR%` 后不会再按 `& | ( ) ^ %` 解析。
///
/// 传了 `exit_pipe_name` 时在整组命令外挂 `9>\\.\pipe\<name>`,程序退出后经
/// `(call echo ^%ERRORLEVEL^%)>&9` 把退出码送回监听线程(括起 `call echo ...` 避免
/// 单个数字的退出码如 `1` 与 `>&9` 相邻被 `cmd` 解析成句柄重定向),用户直接关窗
/// 则由命名管道 EOF 通知。
#[cfg(target_os = "windows")]
fn format_cmd_k_raw_arg(args: &[&str], exit_pipe_name: Option<&str>) -> String {
    let mut invoke = format!("\"%{CMD_TERMINAL_PROGRAM_VAR}%\"");
    for arg in args {
        invoke.push(' ');
        if arg.is_empty()
            || arg.chars().any(|c| {
                c.is_whitespace()
                    || matches!(c, '"' | '&' | '|' | '<' | '>' | '(' | ')' | '^' | '%')
            })
        {
            invoke.push('"');
            for ch in arg.chars() {
                if ch == '"' {
                    invoke.push_str("\\\"");
                } else {
                    invoke.push(ch);
                }
            }
            invoke.push('"');
        } else {
            invoke.push_str(arg);
        }
    }
    let inner = match exit_pipe_name {
        Some(pipe_name) => {
            format!("({invoke} & (call echo ^%ERRORLEVEL^%)>&9) 9>\\\\.\\pipe\\{pipe_name}")
        }
        None => invoke,
    };
    escape_outer_cmd_raw_arg(&format!("\"{inner}\""))
}

/// cmd 终端里执行程序:`cmd /C start cmd /K ""%CMD_TERMINAL_PROGRAM_VAR%" ..."`。
/// 外层用 `^` 转义引号与 `%`(经 `raw_arg` 原样拼进命令行),防止外层 `cmd` 提前展开
/// `%VAR%` 后在空格或 `&` 处切开。`start` 会继承这里设置的环境变量。
///
/// **Security**:`program_str` 与 `args` 会原样交给终端执行,调用方必须保证内容可信
/// (当前只由后端用已探测的二进制路径调用)。
#[cfg(target_os = "windows")]
fn cmd_terminal_command(
    program_str: &str,
    args: &[&str],
    exit_pipe_name: Option<&str>,
) -> std::process::Command {
    let mut command = windows_start_command(&["cmd", "/K"], &[], &effective_path_string());
    command
        .raw_arg(format_cmd_k_raw_arg(args, exit_pipe_name))
        .env(CMD_TERMINAL_PROGRAM_VAR, program_str);
    command
}

#[cfg(target_os = "windows")]
fn format_powershell_terminal_command(args: &[&str], exit_pipe_name: Option<&str>) -> String {
    // 路径经环境变量传入,避免外层 `cmd /C start` 提前展开路径里的 `%...%`;
    // PowerShell 直接执行 `& $env:CC_SWITCH_TERMINAL_PROGRAM`,空格与特殊字符都安全。
    // 命令串内不能出现双引号或 `%`,否则经 `cmd /C start` 透传会被截断或展开。
    let invoke = if args.is_empty() {
        format!("& $env:{CMD_TERMINAL_PROGRAM_VAR}")
    } else {
        let ps_args: Vec<String> = args
            .iter()
            .map(|a| format!("'{}'", a.replace('\'', "''")))
            .collect();
        format!("& $env:{CMD_TERMINAL_PROGRAM_VAR} {}", ps_args.join(" "))
    };
    match exit_pipe_name {
        Some(pipe_name) => format!(
            "$__p = [System.IO.Pipes.NamedPipeClientStream]::new('.', '{pipe_name}', [System.IO.Pipes.PipeDirection]::Out); \
             try {{ $__p.Connect(5000); $__w = [System.IO.StreamWriter]::new($__p) }} catch {{ $__w = $null }}; \
             $__rc = 1; \
             try {{ $global:LASTEXITCODE = $null; {invoke}; $__rc = if ($null -ne $LASTEXITCODE) {{ $LASTEXITCODE }} elseif ($?) {{ 0 }} else {{ 1 }} }} \
             finally {{ if ($__w) {{ try {{ $__w.WriteLine($__rc); $__w.Flush() }} catch {{}}; $__w.Dispose() }}; $__p.Dispose() }}"
        ),
        None => invoke,
    }
}

#[cfg(target_os = "windows")]
fn powershell_terminal_command(
    program_str: &str,
    args: &[&str],
    exit_pipe_name: Option<&str>,
) -> std::process::Command {
    let ps_cmd = format_powershell_terminal_command(args, exit_pipe_name);
    windows_start_command(
        &[
            windows_powershell_program(&effective_path_string()),
            "-NoExit",
            "-Command",
            ps_cmd.as_str(),
        ],
        &[(CMD_TERMINAL_PROGRAM_VAR, program_str)],
        &effective_path_string(),
    )
}

#[cfg(target_os = "windows")]
fn wt_terminal_command(
    program_str: &str,
    args: &[&str],
    exit_pipe_name: Option<&str>,
) -> std::process::Command {
    // 旧版 Windows Terminal 不一定继承自定义环境变量,路径仍经命令行传入:
    // 前置 `call`(无退出管道时)或在括号内直接执行(有退出管道时)避免 `cmd /K` 剥掉引号;
    // 有空白时 wt 会自动给参数加引号,无空白时 wt 不加引号,因此先给内层 `cmd` 的特殊字符
    // 加 `^` 转义,再整体转义给外层 `cmd`。
    let inner_arg = if program_str.chars().any(char::is_whitespace) {
        format!("\"{program_str}\"")
    } else {
        escape_outer_cmd_raw_arg(program_str)
    };
    if let Some(pipe_name) = exit_pipe_name {
        let mut command =
            windows_start_command(&["wt", "cmd", "/K"], &[], &effective_path_string());
        command
            .raw_arg(escape_outer_cmd_raw_arg("("))
            .raw_arg(escape_outer_cmd_raw_arg(&inner_arg))
            .args(args.iter().copied())
            .raw_arg(escape_outer_cmd_raw_arg("&"))
            .raw_arg(escape_outer_cmd_raw_arg("(call"))
            .arg("echo")
            .raw_arg(escape_outer_cmd_raw_arg("^%ERRORLEVEL^%)>&9"))
            .raw_arg(escape_outer_cmd_raw_arg(")"))
            .raw_arg(escape_outer_cmd_raw_arg(&format!(
                "9>\\\\.\\pipe\\{pipe_name}"
            )));
        return command;
    }
    let mut command =
        windows_start_command(&["wt", "cmd", "/K", "call"], &[], &effective_path_string());
    command
        .raw_arg(escape_outer_cmd_raw_arg(&inner_arg))
        .args(args.iter().copied());
    command
}

/// 在用户首选终端里直接执行一个程序,不写临时 .bat:.bat 里的命令要交给 cmd 按 PATH
/// 解析,残留的启动器脚本(如被卸载 CLI 留下的 `agy.cmd`)会把客户端文件恢复回来;这里
/// 把完整路径作为 UTF-16 参数直接交给终端,也不经过代码页解码(#8034 只影响 .bat 的
/// 文件字节)。窗口用 `/K`、`-NoExit` 保持打开,命令退出后用户仍看得到输出。cmd 分支
/// 给路径多包一层双引号并经外层转义交给 `cmd /K`,用户名带 `&`、`(`、`)`、`%` 等字符时也能跑。
/// `envs` 加给启动的终端(`start` 会继承;旧版 Windows Terminal 的新标签不一定继承),
/// `env_removals` 从中删掉。
///
/// **Security**:`program` 与 `args` 会原样交给终端执行,调用方必须保证内容可信
/// (当前只由后端用已探测的二进制路径调用)。
#[cfg(target_os = "windows")]
fn launch_windows_terminal_command(
    program: &Path,
    args: &[&str],
    envs: &[(&str, &str)],
    env_removals: &[&str],
    exit_pipe_name: Option<&str>,
) -> Result<(), String> {
    let program_str = program.to_string_lossy().to_string();
    let preferred = crate::settings::get_preferred_terminal();
    let terminal = preferred.as_deref().unwrap_or("cmd");
    let run = |mut command: std::process::Command, terminal_name: &str| {
        command.envs(envs.iter().copied());
        run_terminal_command(command, terminal_name, env_removals)
    };

    let result = match terminal {
        "powershell" => {
            let command = powershell_terminal_command(&program_str, args, exit_pipe_name);
            run(command, "PowerShell")
        }
        "wt" => {
            let command = wt_terminal_command(&program_str, args, exit_pipe_name);
            run(command, "Windows Terminal")
        }
        _ => {
            let command = cmd_terminal_command(&program_str, args, exit_pipe_name);
            run(command, "cmd")
        }
    };

    if result.is_err() && terminal != "cmd" {
        log::warn!(
            "首选终端 {terminal} 启动失败,回退到 cmd: {:?}",
            result.as_ref().err()
        );
        let command = cmd_terminal_command(&program_str, args, exit_pipe_name);
        return run(command, "cmd");
    }
    result
}

/// 在 Windows 首选终端里直接执行程序,并经本地命名管道监听程序退出码或终端关闭,
/// 结果写回 `exit_file`(不落临时 `.bat`)。
#[cfg(target_os = "windows")]
pub(crate) async fn launch_terminal_command_with_exit_file(
    program: PathBuf,
    args: Vec<String>,
    envs: Vec<(&'static str, String)>,
    env_removals: &'static [&'static str],
    exit_file: Option<PathBuf>,
) -> Result<(), String> {
    let pipe = exit_file.and_then(|path| match WindowsExitPipe::bind(path) {
        Ok(pipe) => Some(pipe),
        Err(err) => {
            log::warn!("[TerminalLaunch] 创建退出码命名管道失败,改为仅等待登录完成: {err}");
            None
        }
    });
    let pipe_name = pipe.as_ref().map(|p| p.pipe_name().to_string());

    let result = tokio::task::spawn_blocking(move || {
        let arg_refs: Vec<&str> = args.iter().map(String::as_str).collect();
        let env_refs: Vec<(&str, &str)> = envs.iter().map(|(k, v)| (*k, v.as_str())).collect();
        launch_windows_terminal_command(
            &program,
            &arg_refs,
            &env_refs,
            env_removals,
            pipe_name.as_deref(),
        )
    })
    .await
    .map_err(|e| format!("launch task join error: {e}"))?;

    if result.is_ok() {
        if let Some(pipe) = pipe {
            pipe.spawn_watcher();
        }
    }
    result
}

/// 首选终端为 PowerShell 时启动哪个程序。`powershell` 永远是系统自带的 Windows
/// PowerShell 5.1;装了 PowerShell 7+ 的用户默认用的是 `pwsh`,所以补全后的 PATH 里
/// 有 `pwsh.exe` 就用它,没有才退回 `powershell`。Microsoft Store 版的 `pwsh.exe` 是
/// App Execution Alias(跟随不了的重解析点),`Path::exists` 会判成不存在,这里用
/// `symlink_metadata`。
#[cfg(target_os = "windows")]
fn windows_powershell_program(path: &str) -> &'static str {
    let has_pwsh = std::env::split_paths(path)
        .filter(|dir| !dir.as_os_str().is_empty())
        .any(|dir| std::fs::symlink_metadata(dir.join("pwsh.exe")).is_ok());
    if has_pwsh {
        "pwsh"
    } else {
        "powershell"
    }
}

#[cfg_attr(windows, allow(dead_code))]
fn build_running_unix_script(
    script_file: &Path,
    command_line: &str,
    label: &str,
    exit_file: Option<&Path>,
    proxy_env: &[(&'static str, String)],
) -> String {
    let proxy: String = proxy_env
        .iter()
        .map(|(name, value)| format!("export {name}={}\n", shell_single_quote(value)))
        .collect();
    let cleanup = shell_single_quote(&format!(
        "rm -f -- {}",
        shell_single_quote(&script_file.to_string_lossy())
    ));
    let exit_record = exit_file
        .map(|path| {
            let exit_q = shell_single_quote(&path.to_string_lossy());
            let tmp_q = shell_single_quote(&format!("{}.tmp", path.to_string_lossy()));
            format!(
                "_cc_switch_rc=$?\nprintf '%s\\n' \"$_cc_switch_rc\" > {tmp_q} && mv -f {tmp_q} {exit_q} || rm -f -- {tmp_q}\n"
            )
        })
        .unwrap_or_default();
    format!(
        "#!/usr/bin/env sh\ntrap {cleanup} EXIT\ncd \"$HOME\" 2>/dev/null || true\necho \"[cc-switch] Starting: {label}\"\necho \"\"\n{proxy}{command_line}\n{exit_record}echo \"\"\necho \"[cc-switch] Command exited. Press Enter to close.\"\nread -r _\n"
    )
}

#[cfg_attr(not(windows), allow(dead_code))]
fn build_running_windows_bat(
    command_line: &str,
    label: &str,
    exit_file: Option<&Path>,
    proxy_env: &[(&'static str, String)],
) -> String {
    // 在 DisableDelayedExpansion 之后设置,密码里的 `!` 不会被吃掉
    let proxy: String = proxy_env
        .iter()
        .map(|(name, value)| format!("set \"{name}={}\"\r\n", value.replace('%', "%%")))
        .collect();
    let exit_record = exit_file
        .map(|path| {
            let escaped = path.to_string_lossy().replace('%', "%%");
            let tmp = format!("{escaped}.tmp");
            format!(
                "set \"_CC_SWITCH_RC=%ERRORLEVEL%\"\r\n(echo %_CC_SWITCH_RC%)>\"{tmp}\"\r\nmove /y \"{tmp}\" \"{escaped}\" >nul 2>&1 || del \"{tmp}\" >nul 2>&1\r\n"
            )
        })
        .unwrap_or_default();
    format!(
        "@echo off\r\nsetlocal DisableDelayedExpansion\r\ncd /d \"%USERPROFILE%\" 2>nul\r\necho [cc-switch] Starting: {label}\r\necho.\r\n{proxy}{command_line}\r\n{exit_record}echo.\r\necho [cc-switch] Command exited. Press any key to close.\r\npause >nul\r\ndel \"%~f0\" >nul 2>&1\r\n"
    )
}

/// 打开用户首选终端并在其中执行一段可信命令脚本。脚本尾部 `read -r` / `pause`
/// 是刻意设计的——让命令退出后窗口不要瞬间关闭,用户才看得到 `command
/// not found` / `ModuleNotFoundError` 这类诊断信息。
///
/// **Security**:`command_line` 会被原样拼进 shell/batch 脚本,调用方必须
/// 保证它是可信字符串(当前只由后端硬编码调用)。
///
/// 设置里配了全局代理时,代理变量写在脚本里交给命令(脚本只给本人读写)。agy 登录自己
/// 把代理拼进命令(含 SOCKS5),走 [`launch_terminal_running_with_exit_file`],不经这里。
pub(crate) fn launch_terminal_running(command_line: &str, label: &str) -> Result<(), String> {
    let proxy_env = crate::proxy::http_client::get_current_proxy_url()
        .map(|url| crate::services::claude_account::login::global_proxy_env(&url, false, |_| false))
        .unwrap_or_default();
    launch_terminal_with_script(command_line, label, None, &proxy_env)
}

/// 只有非 Windows 平台的 agy 登录在用;Windows 下 agy 不经脚本启动,走 [`launch_terminal_command_with_exit_file`]。
#[cfg(not(target_os = "windows"))]
pub(crate) fn launch_terminal_running_with_exit_file(
    command_line: &str,
    label: &str,
    exit_file: Option<&Path>,
) -> Result<(), String> {
    launch_terminal_with_script(command_line, label, exit_file, &[])
}

fn launch_terminal_with_script(
    command_line: &str,
    label: &str,
    exit_file: Option<&Path>,
    proxy_env: &[(&'static str, String)],
) -> Result<(), String> {
    #[cfg(any(target_os = "macos", target_os = "linux"))]
    let (script_file, script_content) = {
        let file = unique_temp_path(&format!("cc_switch_{label}"), "sh");
        let content = build_running_unix_script(&file, command_line, label, exit_file, proxy_env);
        (file, content)
    };

    #[cfg(target_os = "macos")]
    {
        write_private_script(&script_file, &script_content)?;

        let preferred = crate::settings::get_preferred_terminal();
        let terminal = preferred.as_deref().unwrap_or("terminal");

        let result = match terminal {
            "iterm2" => launch_macos_iterm2(&script_file),
            "warp" => launch_macos_warp(&script_file),
            "alacritty" => launch_macos_open_app("Alacritty", &script_file, true),
            "kitty" => launch_macos_open_app("kitty", &script_file, false),
            "ghostty" => launch_macos_ghostty(&script_file),
            "otty" => launch_macos_otty(&script_file),
            "wezterm" => launch_macos_open_app("WezTerm", &script_file, true),
            "kaku" => launch_macos_open_app("Kaku", &script_file, true),
            _ => launch_macos_terminal_app(&script_file),
        };

        let final_result = if result.is_err() && terminal != "terminal" {
            log::warn!(
                "首选终端 {} 启动失败,回退到 Terminal.app: {:?}",
                terminal,
                result.as_ref().err()
            );
            launch_macos_terminal_app(&script_file)
        } else {
            result
        };
        if final_result.is_err() {
            let _ = std::fs::remove_file(&script_file);
        }
        final_result
    }

    #[cfg(target_os = "linux")]
    {
        use std::process::Command;

        write_private_script(&script_file, &script_content)?;

        let preferred = crate::settings::get_preferred_terminal();
        let terminals_to_try = linux_terminal_candidates(preferred.as_deref());

        let mut last_error = String::from("未找到可用的终端");

        for (terminal, args) in terminals_to_try {
            let terminal_exists = which_command(&terminal)
                || ["/usr/bin", "/bin", "/usr/local/bin"]
                    .iter()
                    .any(|dir| std::path::Path::new(&format!("{}/{}", dir, terminal)).exists());

            if terminal_exists {
                let spawn_result = Command::new(&terminal)
                    .args(&args)
                    .arg("sh")
                    .arg(script_file.to_string_lossy().as_ref())
                    .spawn();
                match spawn_result {
                    Ok(_) => return Ok(()),
                    Err(e) => {
                        last_error = format!("执行 {} 失败: {}", terminal, e);
                    }
                }
            }
        }

        let _ = std::fs::remove_file(&script_file);
        Err(last_error)
    }

    #[cfg(target_os = "windows")]
    {
        let preferred = crate::settings::get_preferred_terminal();
        let terminal = preferred.as_deref().unwrap_or("cmd");

        let bat_file = unique_temp_path(&format!("cc_switch_{label}"), "bat");
        let content = build_running_windows_bat(command_line, label, exit_file, proxy_env);
        std::fs::write(&bat_file, &content).map_err(|e| format!("写入批处理文件失败: {e}"))?;

        let bat_path = bat_file.to_string_lossy();
        let ps_cmd = format!("& '{}'", bat_path);

        let result = match terminal {
            "powershell" => run_windows_start_command(
                &[
                    windows_powershell_program(&effective_path_string()),
                    "-NoExit",
                    "-Command",
                    &ps_cmd,
                ],
                "PowerShell",
            ),
            "wt" => run_windows_start_command(&["wt", "cmd", "/K", &bat_path], "Windows Terminal"),
            _ => run_windows_start_command(&["cmd", "/K", &bat_path], "cmd"),
        };

        let final_result = if result.is_err() && terminal != "cmd" {
            log::warn!(
                "首选终端 {} 启动失败,回退到 cmd: {:?}",
                terminal,
                result.as_ref().err()
            );
            run_windows_start_command(&["cmd", "/K", &bat_path], "cmd")
        } else {
            result
        };

        // The .bat self-deletes (`del "%~f0"`) after it runs, but that only
        // fires if *some* terminal actually launched it. If every attempt
        // failed, sweep the temp file ourselves to avoid pollution.
        if final_result.is_err() {
            let _ = std::fs::remove_file(&bat_file);
        }
        final_result
    }

    #[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
    {
        let _ = (command_line, label, exit_file);
        Err("不支持的操作系统".to_string())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[cfg(target_os = "windows")]
    use crate::commands::misc::{decode_command_output, merge_path_segments_win};

    #[cfg(target_os = "windows")]
    #[test]
    fn windows_powershell_program_prefers_pwsh_on_path() {
        let root = tempfile::tempdir().unwrap();
        let empty = root.path().join("empty");
        let pwsh_dir = root.path().join("PowerShell 7");
        std::fs::create_dir_all(&empty).unwrap();
        std::fs::create_dir_all(&pwsh_dir).unwrap();
        std::fs::write(pwsh_dir.join("pwsh.exe"), "").unwrap();

        let without = empty.to_str().unwrap();
        assert_eq!(windows_powershell_program(without), "powershell");
        assert_eq!(windows_powershell_program(""), "powershell");

        let with = format!("{};{}", empty.display(), pwsh_dir.display());
        assert_eq!(windows_powershell_program(&with), "pwsh");
    }

    #[cfg(target_os = "windows")]
    #[test]
    fn windows_start_command_passes_restored_path_to_the_terminal() {
        let root = tempfile::tempdir().unwrap();
        let tool_dir = root.path().join("claude 用户 & bin");
        std::fs::create_dir_all(&tool_dir).unwrap();
        std::fs::write(
            tool_dir.join("cc_switch_start_test_tool.cmd"),
            "@echo restored-terminal-path\r\n@exit /b 0\r\n",
        )
        .unwrap();

        let args = [
            "/B",
            "/WAIT",
            "cmd",
            "/D",
            "/C",
            "cc_switch_start_test_tool",
        ];
        let system_path = PathBuf::from(std::env::var_os("SystemRoot").unwrap()).join("System32");
        let inherited = system_path.to_str().unwrap();
        let effective = merge_path_segments_win(&[inherited, tool_dir.to_str().unwrap()]);
        let parent_path = std::env::var_os("PATH");

        let missing = windows_start_command(&args, &[], inherited)
            .current_dir(root.path())
            .output()
            .unwrap();
        assert!(
            !decode_command_output(&missing.stdout).contains("restored-terminal-path"),
            "the tool must be absent on the narrow PATH: {missing:?}"
        );

        let restored = windows_start_command(&args, &[], &effective)
            .current_dir(root.path())
            .output()
            .unwrap();
        assert!(
            decode_command_output(&restored.stdout).contains("restored-terminal-path"),
            "{restored:?}"
        );
        assert_eq!(std::env::var_os("PATH"), parent_path);
    }

    #[cfg(target_os = "windows")]
    #[test]
    fn cmd_terminal_command_wraps_bin_for_cmd_quote_rules() {
        // 用户名带 `&`、`(`、`)`、`%` 等字符时(agy 安装目录随之带上),`cmd /K "<路径>"` 会
        // 被 cmd 剥引号切开(#10067 的同类问题)。路径必须多包一层双引号、经 `^` 转义后由
        // raw_arg 原样拼进命令行,并经环境变量传给终端,外层与内层 `cmd` 都不会提前展开 `%`。
        let program = r"C:\Users\Name With (Space) & 100% ^caret!\agy.exe";
        let command = cmd_terminal_command(program, &["--model", "a & b"], None);

        let args: Vec<String> = command
            .get_args()
            .map(|a| a.to_string_lossy().to_string())
            .collect();
        let expected_raw = format_cmd_k_raw_arg(&["--model", "a & b"], None);
        let placeholder_index = args
            .iter()
            .position(|a| a == &expected_raw)
            .expect("escaped double-quoted %VAR% command must be passed verbatim");
        assert_eq!(&args[placeholder_index - 1], "/K");

        let carried = command
            .get_envs()
            .find(|(k, _)| *k == CMD_TERMINAL_PROGRAM_VAR)
            .expect("program path must travel in the subprocess env")
            .1
            .expect("env value must be set")
            .to_string_lossy()
            .to_string();
        assert_eq!(carried, program);
    }

    #[cfg(target_os = "windows")]
    #[test]
    fn cmd_start_runs_programs_under_special_char_paths() {
        // 完整模拟 `windows_start_command`(外层 `cmd /C start /B /WAIT` + 内层 `cmd /C`):
        // 覆盖无空格与含空格/非 ASCII/`&`/`(`/`)`/`@`/`^`/`%`/`!` 路径,以及带 `&` 的参数。
        let root = tempfile::tempdir().unwrap();
        let cases = [
            "a&b(c)%PATH%@^!",
            "张三 Name (With) & 100% %PATH% ^caret @ !",
        ];

        for dir_name in cases {
            let dir = root.path().join(dir_name);
            std::fs::create_dir_all(&dir).unwrap();
            let script = dir.join("agy_like.cmd");
            // echo 的参数不能直接内插 `%~2`:值为 `a & b` 时 cmd 会在 echo 行里把 `&`
            // 当命令分隔符切开('b]' is not recognized...)。先用带引号的 `set` 存下来,
            // 再经延迟展开输出——延迟展开发生在解析之后,值里的特殊字符不会再被解析。
            std::fs::write(
                &script,
                "@echo off\r\nsetlocal EnableDelayedExpansion\r\nset \"ARG2=%~2\"\r\necho RAN:[%~1]:[!ARG2!]\r\n@exit /b 0\r\n",
            )
            .unwrap();

            for extra_args in [&[][..], &["--arg", "a & b"][..]] {
                let mut cmd = std::process::Command::new("cmd");
                cmd.args([
                    "/D", "/V:OFF", "/C", "start", "/B", "/WAIT", "cmd", "/D", "/V:OFF", "/C",
                ])
                .raw_arg(format_cmd_k_raw_arg(extra_args, None))
                .env(CMD_TERMINAL_PROGRAM_VAR, &script)
                .current_dir(std::env::var_os("SystemRoot").unwrap());
                let output = cmd.output().unwrap();
                assert!(output.status.success(), "case={dir_name}: {output:?}");
                let printed = decode_command_output(&output.stdout);
                let expected = if extra_args.is_empty() {
                    "RAN:[]:[]"
                } else {
                    "RAN:[--arg]:[a & b]"
                };
                assert_eq!(printed.trim(), expected, "case={dir_name}: {output:?}");
            }
        }
    }

    #[cfg(target_os = "windows")]
    #[test]
    fn powershell_terminal_command_runs_programs_under_special_char_paths() {
        let program = r"C:\Users\Name (With) & %PATH% 'quote' ^caret!\agy.exe";
        let command = powershell_terminal_command(program, &["--arg", "a'b"], None);
        let args: Vec<String> = command
            .get_args()
            .map(|a| a.to_string_lossy().to_string())
            .collect();
        assert!(args
            .iter()
            .any(|a| a == &format!("& $env:{CMD_TERMINAL_PROGRAM_VAR} '--arg' 'a''b'")));
        assert_eq!(
            command
                .get_envs()
                .find(|(k, _)| *k == CMD_TERMINAL_PROGRAM_VAR)
                .and_then(|(_, v)| v)
                .map(|v| v.to_string_lossy().to_string()),
            Some(program.to_string())
        );

        let root = tempfile::tempdir().unwrap();
        let dir = root.path().join("张三 (With) & %PATH% ^caret @ !");
        std::fs::create_dir_all(&dir).unwrap();
        let hostname_exe = PathBuf::from(std::env::var_os("SystemRoot").unwrap())
            .join("System32")
            .join("hostname.exe");
        let target_exe = dir.join("agy_like.exe");
        std::fs::copy(&hostname_exe, &target_exe).unwrap();

        let ps_cmd = format!("& $env:{CMD_TERMINAL_PROGRAM_VAR}");
        let output = std::process::Command::new("cmd")
            .args([
                "/D",
                "/V:OFF",
                "/C",
                "start",
                "/B",
                "/WAIT",
                windows_powershell_program(&effective_path_string()),
                "-NoProfile",
                "-Command",
                &ps_cmd,
            ])
            .env(CMD_TERMINAL_PROGRAM_VAR, &target_exe)
            .output()
            .unwrap();
        assert!(output.status.success(), "{output:?}");
        assert!(
            !decode_command_output(&output.stdout).trim().is_empty(),
            "{output:?}"
        );
    }

    #[cfg(target_os = "windows")]
    #[test]
    fn wt_terminal_command_escapes_special_char_paths() {
        let with_space = r"C:\Users\Name (With) & 100% ^caret!\agy.exe";
        let cmd_space = wt_terminal_command(with_space, &[], None);
        let args_space: Vec<String> = cmd_space
            .get_args()
            .map(|a| a.to_string_lossy().to_string())
            .collect();
        assert_eq!(
            args_space,
            vec![
                "/C",
                "start",
                "wt",
                "cmd",
                "/K",
                "call",
                &escape_outer_cmd_raw_arg(&format!("\"{with_space}\"")),
            ]
        );

        let no_space = r"C:\Users\a&b(c)%d@^!\agy.exe";
        let cmd_nospace = wt_terminal_command(no_space, &[], None);
        let args_nospace: Vec<String> = cmd_nospace
            .get_args()
            .map(|a| a.to_string_lossy().to_string())
            .collect();
        assert_eq!(
            args_nospace,
            vec![
                "/C",
                "start",
                "wt",
                "cmd",
                "/K",
                "call",
                &escape_outer_cmd_raw_arg(&escape_outer_cmd_raw_arg(no_space)),
            ]
        );
    }

    #[cfg(target_os = "windows")]
    #[tokio::test]
    async fn windows_exit_pipe_captures_cmd_exit_codes_under_special_char_paths() {
        let root = tempfile::tempdir().unwrap();
        let dir = root.path().join("张三 (With) & 100% %PATH% ^caret @ !");
        std::fs::create_dir_all(&dir).unwrap();
        let hostname_exe = PathBuf::from(std::env::var_os("SystemRoot").unwrap())
            .join("System32")
            .join("hostname.exe");
        let target_exe = dir.join("agy_like.exe");
        std::fs::copy(&hostname_exe, &target_exe).unwrap();
        let system_cmd = PathBuf::from(std::env::var_os("SystemRoot").unwrap())
            .join("System32")
            .join("cmd.exe");

        let cases: Vec<(PathBuf, Vec<&str>, &str)> = vec![
            (target_exe, Vec::new(), "0"),
            // 单数字退出码 `1`:防止 `(call echo ...)` 外层缺少括号时 `1>&9` 被 cmd 误解析成句柄重定向
            (system_cmd.clone(), vec!["/c", "exit", "1"], "1"),
            (system_cmd, vec!["/c", "exit", "42"], "42"),
        ];

        for (index, (program, args, expected_code)) in cases.into_iter().enumerate() {
            let exit_file = dir.join(format!("exit_{index}.status"));
            let pipe = WindowsExitPipe::bind(exit_file.clone()).unwrap();
            let raw_arg = format_cmd_k_raw_arg(&args, Some(pipe.pipe_name()));
            let handle = tokio::spawn(watch_windows_exit_pipe(
                pipe.server,
                pipe.exit_file,
                std::time::Duration::from_secs(10),
                std::time::Duration::from_secs(10),
            ));

            let output = tokio::task::spawn_blocking(move || {
                std::process::Command::new("cmd")
                    .args([
                        "/D", "/V:OFF", "/C", "start", "/B", "/WAIT", "cmd", "/D", "/V:OFF", "/C",
                    ])
                    .raw_arg(raw_arg)
                    .env(CMD_TERMINAL_PROGRAM_VAR, &program)
                    .current_dir(std::env::var_os("SystemRoot").unwrap())
                    .output()
                    .unwrap()
            })
            .await
            .unwrap();
            assert!(output.status.success(), "case={index}: {output:?}");

            handle.await.unwrap();
            assert_eq!(
                std::fs::read_to_string(&exit_file).unwrap().trim(),
                expected_code,
                "case={index}"
            );
        }
    }

    #[cfg(target_os = "windows")]
    #[tokio::test]
    async fn windows_exit_pipe_captures_powershell_exit_code() {
        let root = tempfile::tempdir().unwrap();
        let exit_file = root.path().join("ps_exit.status");
        let pipe = WindowsExitPipe::bind(exit_file.clone()).unwrap();
        let ps_cmd = format_powershell_terminal_command(&["/c", "exit 42"], Some(pipe.pipe_name()));
        assert!(!ps_cmd.contains('"') && !ps_cmd.contains('%'));

        let handle = tokio::spawn(watch_windows_exit_pipe(
            pipe.server,
            pipe.exit_file,
            std::time::Duration::from_secs(10),
            std::time::Duration::from_secs(10),
        ));

        let system_cmd = PathBuf::from(std::env::var_os("SystemRoot").unwrap())
            .join("System32")
            .join("cmd.exe");
        let ps_program = windows_powershell_program(&effective_path_string());
        let output = tokio::task::spawn_blocking(move || {
            std::process::Command::new("cmd")
                .args([
                    "/D",
                    "/V:OFF",
                    "/C",
                    "start",
                    "/B",
                    "/WAIT",
                    ps_program,
                    "-NoProfile",
                    "-Command",
                    &ps_cmd,
                ])
                .env(CMD_TERMINAL_PROGRAM_VAR, &system_cmd)
                .output()
                .unwrap()
        })
        .await
        .unwrap();
        assert!(output.status.success(), "{output:?}");

        handle.await.unwrap();
        assert_eq!(std::fs::read_to_string(&exit_file).unwrap().trim(), "42");
    }

    #[cfg(target_os = "windows")]
    #[tokio::test]
    async fn windows_exit_pipe_writes_closed_on_eof_and_ignores_connect_timeout() {
        let root = tempfile::tempdir().unwrap();

        // A. 终端窗口被直接关掉(管道连上后未写退出码即 EOF)-> 写入 `closed`
        let closed_exit_file = root.path().join("closed.status");
        let pipe = WindowsExitPipe::bind(closed_exit_file.clone()).unwrap();
        let pipe_path = format!(r"\\.\pipe\{}", pipe.pipe_name());
        let handle = tokio::spawn(watch_windows_exit_pipe(
            pipe.server,
            pipe.exit_file,
            std::time::Duration::from_secs(5),
            std::time::Duration::from_secs(5),
        ));
        let client = tokio::net::windows::named_pipe::ClientOptions::new()
            .read(false)
            .write(true)
            .open(&pipe_path)
            .unwrap();
        drop(client);
        handle.await.unwrap();
        assert_eq!(
            std::fs::read_to_string(&closed_exit_file).unwrap().trim(),
            "closed"
        );

        // B. 管道一直无人连接直至超时 -> 绝不误写 exit_file(免得误杀正在进行的登录)
        let timeout_exit_file = root.path().join("timeout.status");
        let timeout_pipe = WindowsExitPipe::bind(timeout_exit_file.clone()).unwrap();
        watch_windows_exit_pipe(
            timeout_pipe.server,
            timeout_pipe.exit_file,
            std::time::Duration::from_millis(50),
            std::time::Duration::from_secs(1),
        )
        .await;
        assert!(!timeout_exit_file.exists());
    }

    #[cfg(target_os = "windows")]
    #[test]
    fn wt_terminal_command_with_exit_pipe_escapes_special_char_paths() {
        let pipe_name = "cc-switch-test";
        let expected_tail = vec![
            escape_outer_cmd_raw_arg("&"),
            escape_outer_cmd_raw_arg("(call"),
            "echo".to_string(),
            escape_outer_cmd_raw_arg("^%ERRORLEVEL^%)>&9"),
            escape_outer_cmd_raw_arg(")"),
            escape_outer_cmd_raw_arg(&format!("9>\\\\.\\pipe\\{pipe_name}")),
        ];

        let with_space = r"C:\Users\Name (With) & 100% ^caret!\agy.exe";
        let cmd_space = wt_terminal_command(with_space, &[], Some(pipe_name));
        let args_space: Vec<String> = cmd_space
            .get_args()
            .map(|a| a.to_string_lossy().to_string())
            .collect();
        let mut expected_space = vec![
            "/C".to_string(),
            "start".to_string(),
            "wt".to_string(),
            "cmd".to_string(),
            "/K".to_string(),
            escape_outer_cmd_raw_arg("("),
            escape_outer_cmd_raw_arg(&format!("\"{with_space}\"")),
        ];
        expected_space.extend(expected_tail.clone());
        assert_eq!(args_space, expected_space);

        let no_space = r"C:\Users\a&b(c)%d@^!\agy.exe";
        let cmd_nospace = wt_terminal_command(no_space, &[], Some(pipe_name));
        let args_nospace: Vec<String> = cmd_nospace
            .get_args()
            .map(|a| a.to_string_lossy().to_string())
            .collect();
        let mut expected_nospace = vec![
            "/C".to_string(),
            "start".to_string(),
            "wt".to_string(),
            "cmd".to_string(),
            "/K".to_string(),
            escape_outer_cmd_raw_arg("("),
            escape_outer_cmd_raw_arg(&escape_outer_cmd_raw_arg(no_space)),
        ];
        expected_nospace.extend(expected_tail);
        assert_eq!(args_nospace, expected_nospace);
    }

    #[test]
    fn unix_shell_command_and_exec_builders() {
        let claude = format!(
            "claude --settings {}",
            shell_word("/tmp/claude config.json")
        );
        assert_eq!(
            build_provider_command_line("/bin/zsh", &claude, None),
            "'/bin/zsh' -lic 'claude --settings '\"'\"'/tmp/claude config.json'\"'\"''"
        );
        assert_eq!(
            build_provider_command_line("/bin/bash", &claude, Some(Path::new("/tmp/project"))),
            r#"'/bin/bash' -ic 'cd '"'"'/tmp/project'"'"' && claude --settings '"'"'/tmp/claude config.json'"'"'; exit $?'"#
        );
        assert_eq!(
            build_provider_command_line(
                "/bin/sh",
                &claude,
                Some(Path::new("/tmp/project O'Brien"))
            ),
            r#"'/bin/sh' -c 'cd '"'"'/tmp/project O'"'"'"'"'"'"'"'"'Brien'"'"' && claude --settings '"'"'/tmp/claude config.json'"'"''"#
        );

        assert_eq!(build_exec_line("/bin/zsh", None), "exec '/bin/zsh' -l");
        assert_eq!(build_exec_line("/bin/bash", None), "exec '/bin/bash'");
        assert_eq!(
            build_exec_line("/tmp/shell'quote/zsh", None),
            "exec '/tmp/shell'\"'\"'quote/zsh' -l"
        );
        assert_eq!(
            build_exec_line("/bin/zsh", Some(Path::new("/tmp/project"))),
            r#"exec '/bin/zsh' -lc 'cd '"'"'/tmp/project'"'"' || exit 1; exec '"'"'/bin/zsh'"'"' -i'"#
        );

        assert_eq!(
            build_final_shell_cd_command("/bin/zsh", Some(Path::new("/tmp/project"))),
            ""
        );
        assert_eq!(
            build_final_shell_cd_command("/bin/bash", Some(Path::new("/tmp/project O'Brien"))),
            "cd '/tmp/project O'\"'\"'Brien' || exit 1\n"
        );
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn linux_terminal_candidates_include_system_launchers() {
        let candidates = linux_terminal_candidates(None);

        assert!(candidates
            .iter()
            .any(|(name, args)| { name == "x-terminal-emulator" && args == &["-e"] }));
        assert!(candidates
            .iter()
            .any(|(name, args)| { name == "xdg-terminal-exec" && args == &["--"] }));

        let preferred = linux_terminal_candidates(Some("konsole"));
        assert_eq!(preferred[0], ("konsole".to_string(), vec!["-e"]));
        assert!(preferred
            .iter()
            .any(|(name, _)| name == "x-terminal-emulator"));
        assert!(preferred
            .iter()
            .any(|(name, _)| name == "xdg-terminal-exec"));
    }

    fn agy_key_launch() -> ProviderLaunch {
        ProviderLaunch {
            title: "Using a private Antigravity profile for this provider:",
            detail: Some(PathBuf::from("/home/me/.cc-switch/agy/relay")),
            program: "agy",
            args: vec!["--gemini_dir=/home/me/.cc-switch/agy/relay".to_string()],
            batch_command: format!("agy \"--gemini_dir=%{AGY_DIR_BATCH_VAR}%\""),
            env: vec![
                ("GEMINI_API_KEY", Some("k'$(echo no)`x`%PATH%".to_string())),
                ("GOOGLE_GEMINI_BASE_URL", None),
            ],
            batch_vars: vec![(
                AGY_DIR_BATCH_VAR,
                r"C:\Users\张三\.cc-switch\agy\relay".to_string(),
            )],
            cleanup: Vec::new(),
            proxy_env: Vec::new(),
        }
    }

    #[test]
    fn launch_command_keeps_secrets_out_of_the_command_line() {
        let launch = agy_key_launch();
        assert_eq!(
            unix_launch_command(&launch),
            "env -u CC_SWITCH_LAUNCH_ENV_0 -u CC_SWITCH_LAUNCH_ENV_1 -u GOOGLE_GEMINI_BASE_URL GEMINI_API_KEY=\"$CC_SWITCH_LAUNCH_ENV_0\" agy --gemini_dir=/home/me/.cc-switch/agy/relay"
        );
        assert_eq!(
            unix_launch_env_file(&launch),
            "CC_SWITCH_LAUNCH_ENV_0='k'\"'\"'$(echo no)`x`%PATH%'\n"
        );
        let plain = ProviderLaunch {
            env: Vec::new(),
            ..agy_key_launch()
        };
        assert_eq!(
            unix_launch_command(&plain),
            "agy --gemini_dir=/home/me/.cc-switch/agy/relay"
        );
        assert_eq!(unix_launch_env_file(&plain), "");
    }

    #[test]
    fn split_launch_args_groups_quoted_values_without_a_shell() {
        assert_eq!(split_launch_args("  ").unwrap(), Vec::<String>::new());
        assert_eq!(
            split_launch_args(
                "--debug --append-system-prompt \"be brief; ok\" --mcp-config 'C:\\a b\\mcp.json'\n-x=\"\""
            )
            .unwrap(),
            vec![
                "--debug",
                "--append-system-prompt",
                "be brief; ok",
                "--mcp-config",
                "C:\\a b\\mcp.json",
                "-x=",
            ]
        );
        assert_eq!(
            split_launch_args("--name=\"it's\" ''").unwrap(),
            vec!["--name=it's", ""]
        );
        assert!(split_launch_args("--append-system-prompt \"oops").is_err());
        assert!(split_launch_args("\"line\nbreak\"").is_err());
        let too_many = vec!["-v"; USER_ARG_BATCH_VARS.len() + 1].join(" ");
        assert!(split_launch_args(&too_many).is_err());
    }

    #[test]
    fn fish_gets_its_own_quoting_for_user_text() {
        let mut launch = claude_account_terminal_launch(None);
        launch.env.clear();
        launch
            .append_user_args(vec!["x\\'; printf INJECTED; #".into(), "C:\\work\\".into()])
            .unwrap();
        assert_eq!(
            unix_launch_command_in(&launch, "/opt/homebrew/bin/fish"),
            "claude 'x\\\\\\'; printf INJECTED; #' 'C:\\\\work\\\\'"
        );
        assert_eq!(
            unix_launch_command_in(&launch, "/bin/zsh"),
            "claude 'x\\'\"'\"'; printf INJECTED; #' 'C:\\work\\'"
        );
        assert_eq!(launch.batch_vars[1].1, "C:\\work\\\\");
        assert_eq!(launch.args[1], "C:\\work\\");
        assert_eq!(
            build_provider_command_line("/usr/bin/fish", "claude", Some(Path::new("/tmp/a\\'b"))),
            "'/usr/bin/fish' -ic 'cd '\"'\"'/tmp/a\\\\\\'\"'\"'b'\"'\"' && claude'"
        );
        let mut launch = claude_account_terminal_launch(None);
        launch.env.clear();
        launch.append_user_args(vec!["=sh".into()]).unwrap();
        assert_eq!(unix_launch_command_in(&launch, "/bin/zsh"), "claude '=sh'");
    }

    #[test]
    fn user_args_and_skip_permissions_append_cleanly() {
        let mut launch = claude_account_terminal_launch(None);
        launch.env.clear();
        launch.skip_permissions();
        launch
            .append_user_args(vec!["--append-system-prompt".into(), "a & b | c".into()])
            .unwrap();
        assert_eq!(
            unix_launch_command(&launch),
            "claude --dangerously-skip-permissions --append-system-prompt 'a & b | c'"
        );
        assert_eq!(
            launch.batch_command,
            "claude --dangerously-skip-permissions \"%CC_SWITCH_ARG0%\" \"%CC_SWITCH_ARG1%\""
        );
        assert_eq!(
            launch.batch_vars,
            vec![
                ("CC_SWITCH_ARG0", "--append-system-prompt".to_string()),
                ("CC_SWITCH_ARG1", "a & b | c".to_string()),
            ]
        );

        let mut agy = agy_key_launch();
        agy.skip_permissions();
        assert!(unix_launch_command(&agy).ends_with(
            "agy --gemini_dir=/home/me/.cc-switch/agy/relay --dangerously-skip-permissions"
        ));
        assert_eq!(
            agy.batch_command,
            format!("agy \"--gemini_dir=%{AGY_DIR_BATCH_VAR}%\" --dangerously-skip-permissions")
        );
    }

    #[test]
    #[serial_test::serial]
    fn provider_specific_terminal_launches() {
        // Grok: managed GROK_HOME + skip_permissions + user args
        let mut grok = grok_launch("/home/me/.grok".to_string());
        grok.skip_permissions();
        grok.append_user_args(vec!["--model".into(), "grok-4.6".into()])
            .unwrap();
        assert_eq!(grok.args, vec!["--always-approve", "--model", "grok-4.6"]);
        assert_eq!(
            grok.batch_command,
            "call grok --always-approve \"%CC_SWITCH_ARG0%\" \"%CC_SWITCH_ARG1%\""
        );
        assert!(unix_launch_command(&grok).starts_with(
            "env -u CC_SWITCH_LAUNCH_ENV_0 GROK_HOME=\"$CC_SWITCH_LAUNCH_ENV_0\" grok "
        ));

        // Codex: separate `-c` overrides + API key kept out of command line
        let mut codex = codex_launch(
            "Kimi",
            vec![
                "model_provider='custom'".to_string(),
                "model_providers.custom={name='K',base_url='https://k.example/v1?a=1&b=2'}"
                    .to_string(),
            ],
            vec![("CC_SWITCH_CODEX_API_KEY", Some("sk-kimi".to_string()))],
            Vec::new(),
        )
        .unwrap();
        codex.skip_permissions();
        assert!(!unix_launch_command(&codex).contains("sk-kimi"));
        assert_eq!(
            codex.batch_command,
            "call codex -c \"%CC_SWITCH_CODEX_C0%\" -c \"%CC_SWITCH_CODEX_C1%\" --dangerously-bypass-approvals-and-sandbox"
        );
        assert_eq!(
            codex_launch("Kimi", Vec::new(), Vec::new(), Vec::new())
                .unwrap()
                .batch_command,
            "call codex"
        );

        // Antigravity official login: per-session `--model`, rejected by resume terminal
        let mut row = crate::provider::Provider::with_id(
            "login".into(),
            "Google".into(),
            serde_json::json!({"env":{},"config":{"model":{"name":"Gemini 3.1 Pro (High)"}}}),
            None,
        );
        row.category = Some("official".into());
        let agy = agy_terminal_launch(&row).unwrap();
        assert!(agy.env.is_empty());
        assert_eq!(
            unix_launch_command(&agy),
            "agy '--model=Gemini 3.1 Pro (High)'"
        );
        assert_eq!(
            agy.batch_vars,
            vec![(AGY_MODEL_BATCH_VAR, "Gemini 3.1 Pro (High)".to_string())]
        );
        assert!(open_agy_resume_terminal(&row, "abc", None).is_err());

        row.settings_config = serde_json::json!({"env":{},"config":{}});
        assert_eq!(
            unix_launch_command(&agy_terminal_launch(&row).unwrap()),
            "agy"
        );
    }

    #[test]
    fn global_proxy_reaches_terminal_clients() {
        let proxy_keys = ["HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy"];
        let launch = |env: Vec<(&'static str, Option<String>)>| ProviderLaunch {
            title: "",
            detail: None,
            program: "claude",
            args: Vec::new(),
            batch_command: String::new(),
            env,
            batch_vars: Vec::new(),
            cleanup: Vec::new(),
            proxy_env: Vec::new(),
        };
        let with_proxy = |app: AppType, url: Option<&str>| {
            let mut launch = launch(Vec::new());
            add_global_proxy_env(&mut launch, &app, std::iter::empty(), url);
            launch.proxy_env
        };

        // agy (Go): SOCKS5 too, socks5h becomes socks5, NO_PROXY left to the user (#10119)
        assert_eq!(
            with_proxy(AppType::Agy, Some("socks5h://127.0.0.1:1080")),
            proxy_keys
                .map(|key| (key, "socks5://127.0.0.1:1080".to_string()))
                .to_vec()
        );

        // Other clients: http(s) only, and the local router stays off the proxy
        let mut expected: Vec<_> = proxy_keys
            .map(|key| (key, "http://127.0.0.1:7890".to_string()))
            .to_vec();
        expected.extend(
            ["NO_PROXY", "no_proxy"].map(|key| (key, "localhost,127.0.0.1,::1".to_string())),
        );
        for app in [
            AppType::Claude,
            AppType::Codex,
            AppType::Gemini,
            AppType::Copilot,
        ] {
            assert_eq!(with_proxy(app, Some("http://127.0.0.1:7890")), expected);
        }
        assert!(with_proxy(AppType::Claude, Some("socks5://127.0.0.1:1080")).is_empty());

        // No proxy configured, or an unusable one: nothing added
        assert!(with_proxy(AppType::Agy, None).is_empty());
        assert!(with_proxy(AppType::Codex, Some("not a url")).is_empty());

        // A card that configures any proxy keeps it whole: no mixed-case pair pointing elsewhere
        let mut card = launch(vec![("HTTPS_PROXY", Some("http://card:1".to_string()))]);
        add_global_proxy_env(
            &mut card,
            &AppType::Codex,
            std::iter::empty(),
            Some("http://127.0.0.1:7890"),
        );
        assert!(card.proxy_env.is_empty());

        // Claude keeps its card env in the temporary settings file, not in `launch.env`
        let mut claude = launch(Vec::new());
        add_global_proxy_env(
            &mut claude,
            &AppType::Claude,
            ["ANTHROPIC_BASE_URL", "https_proxy"],
            Some("http://127.0.0.1:7890"),
        );
        assert!(claude.proxy_env.is_empty());

        // A card NO_PROXY (either case) wins; the proxy itself still applies
        let mut no_proxy = launch(Vec::new());
        add_global_proxy_env(
            &mut no_proxy,
            &AppType::Claude,
            ["no_proxy"],
            Some("http://127.0.0.1:7890"),
        );
        assert_eq!(
            no_proxy.proxy_env,
            proxy_keys
                .map(|key| (key, "http://127.0.0.1:7890".to_string()))
                .to_vec()
        );

        // The proxy is exported to the user's shell, not put behind an `env` prefix (which would
        // skip a `claude` alias or function), and is dropped before the interactive shell takes over.
        let mut proxied = launch(Vec::new());
        add_global_proxy_env(
            &mut proxied,
            &AppType::Agy,
            std::iter::empty(),
            Some("http://user:pa'[email protected]:7890"),
        );
        assert_eq!(unix_launch_command(&proxied), "claude");
        let env_file = unix_launch_env_file(&proxied);
        assert!(env_file.starts_with("HTTPS_PROXY='http://user:pa'\"'\"'[email protected]:7890'\n"));
        let script = build_unix_launcher_script(
            &proxied,
            "/bin/bash",
            None,
            Path::new("/tmp/launcher.sh"),
            Some(Path::new("/tmp/env.sh")),
        );
        assert!(!script.contains("7890"));
        let run = script.find("'/bin/bash' -ic 'claude; exit $?'").unwrap();
        let unset = script
            .find("unset HTTPS_PROXY https_proxy HTTP_PROXY http_proxy\n")
            .unwrap();
        assert!(script.find(". '/tmp/env.sh'").unwrap() < run && run < unset);
    }

    #[test]
    #[serial_test::serial]
    fn copilot_byok_and_login_terminal_launches() {
        // BYOK card: clears COPILOT_PROVIDER_* / COPILOT_MODEL, ignores bound account, passes reasoning effort
        let prepared = crate::copilot_config::prepare_launch(
            "relay",
            &serde_json::json!({
                "provider": {"baseUrl":"https://relay.test/v1","apiKey":"fixture-key","wireApi":"responses"},
                "models": [{"id":"deepseek-v4-flash"}],
                "reasoningEffort": "none"
            }),
        )
        .unwrap();
        let mut byok = copilot_launch_command(
            "Relay",
            prepared,
            Some(("gho_x".into(), "github.com".into())),
        )
        .unwrap();
        let cmd = unix_launch_command(&byok);
        assert!(cmd.ends_with(" copilot --model=relay/deepseek-v4-flash --reasoning-effort=none"));
        assert!(!cmd.contains("fixture-key") && !cmd.contains("gho_x"));
        for key in crate::copilot_config::BYOK_ENV_KEYS {
            assert!(cmd.contains(&format!("-u {key}")), "{key}: {cmd}");
        }
        assert!(cmd.contains("-u COPILOT_MODEL"), "{cmd}");
        let env_file = unix_launch_env_file(&byok);
        assert!(!env_file.contains("fixture-key") && !env_file.contains("gho_x"));

        byok.skip_permissions();
        byok.append_user_args(vec!["--add-dir".into(), "/tmp/a b".into()])
            .unwrap();
        assert!(unix_launch_command(&byok)
            .ends_with(" --reasoning-effort=none --allow-all --add-dir '/tmp/a b'"));
        assert!(byok.batch_command.ends_with(
            " --reasoning-effort=none --allow-all \"%CC_SWITCH_ARG0%\" \"%CC_SWITCH_ARG1%\""
        ));

        // Official login card: passes bound account via env file (never argv)
        let mut row = crate::provider::Provider::with_id(
            "login".into(),
            "GitHub".into(),
            serde_json::json!({"env":{"COPILOT_MODEL":"claude-sonnet-4.6"}}),
            None,
        );
        row.category = Some("official".into());
        let login = copilot_terminal_launch(
            &row,
            Some(("gho_fixture".into(), "github.example.com".into())),
        )
        .unwrap();
        let login_cmd = unix_launch_command(&login);
        assert!(login_cmd.ends_with(" copilot --model=claude-sonnet-4.6"));
        assert!(login_cmd.contains("-u COPILOT_PROVIDER_BASE_URL"));
        assert!(!login_cmd.contains("gho_fixture"));
        let login_env = unix_launch_env_file(&login);
        assert!(login_env.contains("gho_fixture") && login_env.contains("github.example.com"));
    }

    #[cfg(unix)]
    #[test]
    fn unix_launcher_script_passes_env_past_rc_files_and_cleans_up() {
        use std::os::unix::fs::PermissionsExt;
        let tmp = tempfile::tempdir().unwrap();
        let home = tmp.path().join("home");
        let bin = tmp.path().join("bin");
        std::fs::create_dir_all(&home).unwrap();
        std::fs::create_dir_all(&bin).unwrap();
        std::fs::write(
            home.join(".bashrc"),
            "export GEMINI_API_KEY=from-rc\nexport GOOGLE_GEMINI_BASE_URL=https://rc.test\n",
        )
        .unwrap();
        let out = tmp.path().join("out");
        std::fs::write(
            bin.join("agy"),
            format!(
                "#!/bin/sh\nprintf '%s\\n' \"${{GEMINI_API_KEY-unset}}\" \"${{GOOGLE_GEMINI_BASE_URL-unset}}\" \"${{CC_SWITCH_LAUNCH_ENV_0-unset}}\" \"$@\" > '{}'\n",
                out.display()
            ),
        )
        .unwrap();
        std::fs::set_permissions(bin.join("agy"), std::fs::Permissions::from_mode(0o755)).unwrap();

        let launch = agy_key_launch();
        let script = tmp.path().join("launcher.sh");
        let env_file = tmp.path().join("env.sh");
        std::fs::write(&env_file, unix_launch_env_file(&launch)).unwrap();
        let content =
            build_unix_launcher_script(&launch, "/bin/bash", None, &script, Some(&env_file))
                .replace("exec '/bin/bash'", "exit 0 #");
        std::fs::write(&script, content).unwrap();
        let status = std::process::Command::new("sh")
            .arg(&script)
            .env("HOME", &home)
            .env("PATH", format!("{}:/usr/bin:/bin", bin.display()))
            .env("GEMINI_API_KEY", "from-gui")
            .status()
            .unwrap();
        assert!(status.success());
        assert_eq!(
            std::fs::read_to_string(&out).unwrap(),
            "k'$(echo no)`x`%PATH%\nunset\nunset\n--gemini_dir=/home/me/.cc-switch/agy/relay\n"
        );
        assert!(
            !env_file.exists(),
            "env file is removed right after reading"
        );
        assert!(!script.exists());
    }

    #[test]
    fn windows_launch_env_scopes_values_and_carries_non_ascii_paths() {
        let (lines, carried) = build_windows_launch_env(&agy_key_launch());
        assert_eq!(
            lines,
            "set \"GEMINI_API_KEY=k'$(echo no)`x`%%PATH%%\"\r\n\
             set \"GOOGLE_GEMINI_BASE_URL=\"\r\n\
             set \"CC_SWITCH_AGY_DIR=%CC_SWITCH_INTERNAL_ENV_2%\"\r\n\
             set \"CC_SWITCH_INTERNAL_ENV_2=\"\r\n"
        );
        assert_eq!(
            carried,
            vec![(
                "CC_SWITCH_INTERNAL_ENV_2".to_string(),
                r"C:\Users\张三\.cc-switch\agy\relay".to_string()
            )]
        );
    }

    #[cfg(unix)]
    #[test]
    fn claude_terminal_config_is_private() {
        use std::os::unix::fs::PermissionsExt;
        let launch = claude_terminal_launch(
            &[("ANTHROPIC_AUTH_TOKEN".into(), "t".into())],
            "p",
            "P",
            &Default::default(),
        )
        .unwrap();
        let file = &launch.cleanup[0];
        assert_eq!(
            std::fs::metadata(file).unwrap().permissions().mode() & 0o777,
            0o600
        );
        assert_eq!(
            launch.args,
            vec![
                "--settings".to_string(),
                file.to_string_lossy().into_owned()
            ]
        );
        std::fs::remove_file(file).unwrap();
    }

    #[test]
    fn claude_terminal_config_file_name_is_ascii() {
        let launch =
            claude_terminal_launch(&[], "随时跑路-relay 1", "随时跑路", &Default::default())
                .unwrap();
        let file = &launch.cleanup[0];
        let name = file.file_name().unwrap().to_string_lossy().into_owned();
        std::fs::remove_file(file).unwrap();
        assert!(name.is_ascii(), "{name}");
        assert!(name.starts_with("claude_-relay1_"), "{name}");
        assert!(launch.batch_command.is_ascii());
    }

    #[test]
    fn claude_terminal_config_model_picker_and_switch_drops() {
        let read_config = |env: &[(&str, &str)], drops: &crate::services::provider::SwitchDrops| {
            let env: Vec<(String, String)> = env
                .iter()
                .map(|(k, v)| (k.to_string(), v.to_string()))
                .collect();
            let launch = claude_terminal_launch(&env, "p", "DeepSeek", drops).unwrap();
            let file = &launch.cleanup[0];
            let value: serde_json::Value =
                serde_json::from_str(&std::fs::read_to_string(file).unwrap()).unwrap();
            std::fs::remove_file(file).unwrap();
            value
        };

        // Explicit tier models -> replaceBuiltInOptions=true with formatted labels.
        let value = read_config(
            &[
                ("ANTHROPIC_MODEL", "deepseek-v4-pro"),
                ("ANTHROPIC_DEFAULT_OPUS_MODEL", "deepseek-v4-pro[1M]"),
                ("ANTHROPIC_DEFAULT_HAIKU_MODEL", "deepseek-v4-flash"),
                ("ANTHROPIC_DEFAULT_HAIKU_MODEL_NAME", "Flash"),
            ],
            &Default::default(),
        );
        assert_eq!(
            value["modelPicker"],
            serde_json::json!({
                "replaceBuiltInOptions": true,
                "options": [
                    {
                        "model": "opus",
                        "label": "Opus · deepseek-v4-pro",
                        "description": "DeepSeek · deepseek-v4-pro · 1M",
                    },
                    {
                        "model": "haiku",
                        "label": "Haiku · Flash",
                        "description": "DeepSeek · deepseek-v4-flash",
                    },
                ],
            })
        );

        // No tier models & no drops -> writes only `env` and empty `modelPicker`.
        let plain = read_config(
            &[("ANTHROPIC_MODEL", "deepseek-v4-pro")],
            &Default::default(),
        );
        let keys: Vec<&String> = plain.as_object().unwrap().keys().collect();
        assert_eq!(keys, ["env", "modelPicker"]);
        assert_eq!(
            plain["modelPicker"],
            serde_json::json!({ "replaceBuiltInOptions": false, "options": [] })
        );

        // Switch drops shadow removed keys without overriding card's own values.
        let drops = crate::services::provider::SwitchDrops {
            env: serde_json::json!({
                "ANTHROPIC_AUTH_TOKEN": "PROXY_MANAGED",
                "ANTHROPIC_DEFAULT_OPUS_MODEL": "ccs-claude-kimi--k3[1M]",
            })
            .as_object()
            .unwrap()
            .clone(),
            top: serde_json::json!({
                "model": "ccs-claude-kimi--k3[1M]",
                "modelOverrides": { "a": "b" },
                "modelPicker": { "replaceBuiltInOptions": true, "options": [] },
            })
            .as_object()
            .unwrap()
            .clone(),
        };
        let shadowed = read_config(
            &[
                ("ANTHROPIC_API_KEY", "sk"),
                ("ANTHROPIC_DEFAULT_OPUS_MODEL", "deepseek-v4-pro"),
            ],
            &drops,
        );
        assert_eq!(shadowed["env"]["ANTHROPIC_API_KEY"], "sk");
        assert_eq!(
            shadowed["env"]["ANTHROPIC_DEFAULT_OPUS_MODEL"],
            "deepseek-v4-pro"
        );
        assert_eq!(shadowed["env"]["ANTHROPIC_AUTH_TOKEN"], "");
        assert_eq!(shadowed["model"], "");
        assert_eq!(shadowed["modelOverrides"], serde_json::json!({}));
        assert_eq!(shadowed["modelPicker"]["options"][0]["model"], "opus");
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn macos_applescript_launchers_quote_paths_and_handle_cold_start() {
        let p = Path::new("/Users/me/it's dir/x.sh");
        assert_eq!(
            build_macos_dash_c_command(p),
            r#"exec sh '/Users/me/it'"'"'s dir/x.sh'"#
        );
        let expected_sh = r#""sh '/Users/me/it'\"'\"'s dir/x.sh'""#;
        let expected_exec = r#""exec sh '/Users/me/it'\"'\"'s dir/x.sh'""#;
        assert_eq!(applescript_launcher_command(p), expected_sh);
        assert_eq!(applescript_exec_launcher_command(p), expected_exec);

        let terminal = build_macos_terminal_applescript(p);
        assert!(terminal.contains(expected_exec));
        assert!(terminal
            .contains("else\n        launch\n        do script launcher_script\n        activate"));
        assert!(!terminal.contains(" in window 1"));

        let iterm2 = build_macos_iterm2_applescript(p);
        assert!(iterm2.contains(expected_exec));
        assert!(iterm2.contains("repeat while (count of windows) = 0"));

        let ghostty = build_macos_ghostty_applescript(p);
        assert!(ghostty.contains(expected_sh));
        assert!(ghostty.contains(
            "tell application \"Ghostty\"\n        activate\n        new window with configuration {command:launcher_command}"
        ));
        assert!(ghostty.contains("--initial-command="));
    }

    #[test]
    fn running_scripts_set_home_cwd_quote_cleanup_and_record_exit_status() {
        let script = Path::new("/tmp/it's dir/cc_switch_agy_login_1.sh");
        let exit_file = Path::new("/tmp/it's dir/exit.status");
        let unix = build_running_unix_script(script, "agy", "agy_login", Some(exit_file), &[]);
        assert!(unix.contains("cd \"$HOME\" 2>/dev/null || true\n"));
        assert!(unix.contains("trap 'rm -f -- '\"'\"'/tmp/it'\"'\"'\"'\"'\"'\"'\"'\"'s dir/cc_switch_agy_login_1.sh'\"'\"'' EXIT"));
        let rc_pos = unix.find("_cc_switch_rc=$?").unwrap();
        let prompt_pos = unix.find("Press Enter to close.").unwrap();
        assert!(rc_pos < prompt_pos);
        assert!(unix.contains("mv -f"));

        let bat = build_running_windows_bat(
            "agy",
            "agy_login",
            Some(Path::new(r"C:\Temp\exit%1.status")),
            &[],
        );
        assert!(bat.contains("cd /d \"%USERPROFILE%\" 2>nul\r\n"));
        let err_pos = bat.find("set \"_CC_SWITCH_RC=%ERRORLEVEL%\"").unwrap();
        let bat_prompt_pos = bat.find("Press any key to close.").unwrap();
        assert!(err_pos < bat_prompt_pos);
        assert!(bat.contains(r#"move /y "C:\Temp\exit%%1.status.tmp" "C:\Temp\exit%%1.status""#));
    }

    #[cfg(unix)]
    #[test]
    fn running_unix_script_executes_cleanup_and_exit_file_recording() {
        use std::io::Write;
        use std::os::unix::fs::PermissionsExt;

        let dir = tempfile::tempdir().unwrap();
        let tricky = dir.path().join("it's dir");
        std::fs::create_dir_all(&tricky).unwrap();
        let script = tricky.join("run.sh");
        let exit_file = tricky.join("exit.status");
        let content =
            build_running_unix_script(&script, "(exit 42)", "test_exit", Some(&exit_file), &[]);
        std::fs::write(&script, content).unwrap();
        std::fs::set_permissions(&script, std::fs::Permissions::from_mode(0o755)).unwrap();

        let mut child = std::process::Command::new("sh")
            .arg(&script)
            .stdin(std::process::Stdio::piped())
            .stdout(std::process::Stdio::null())
            .stderr(std::process::Stdio::null())
            .spawn()
            .unwrap();
        child.stdin.take().unwrap().write_all(b"\n").unwrap();
        assert!(child.wait().unwrap().success());
        assert_eq!(std::fs::read_to_string(&exit_file).unwrap().trim(), "42");
        assert!(!script.exists(), "EXIT trap should remove script");
    }

    #[test]
    fn build_windows_cwd_command_str_handles_drive_unc_escaping_and_non_ascii() {
        assert_eq!(
            build_windows_cwd_command_str(r"C:\work\repo"),
            (
                "cd /d \"C:\\work\\repo\" || exit /b 1\r\n".to_string(),
                None
            )
        );
        assert_eq!(
            build_windows_cwd_command_str(r"\\wsl$\Ubuntu\home\coder\repo").0,
            "pushd \"\\\\wsl$\\Ubuntu\\home\\coder\\repo\" || exit /b 1\r\n"
        );
        assert_eq!(
            build_windows_cwd_command_str(r"\\server\share\100%&(test)").0,
            "pushd \"\\\\server\\share\\100%%^&^(test^)\" || exit /b 1\r\n"
        );

        // cmd 按控制台代码页解码 .bat,非 ASCII 路径经环境变量中转(#8034)。
        let path = r"C:\Users\林增文\项目";
        let (command, env) = build_windows_cwd_command_str(path);
        assert!(command.is_ascii());
        assert_eq!(
            command,
            "cd /d \"%CC_SWITCH_INTERNAL_CWD%\" || exit /b 1\r\nset \"CC_SWITCH_INTERNAL_CWD=\"\r\n"
        );
        assert_eq!(env.as_deref(), Some(path));

        let (unc_cmd, unc_env) = build_windows_cwd_command_str(r"\\server\共享\repo");
        assert!(unc_cmd.starts_with("pushd \"%CC_SWITCH_INTERNAL_CWD%\" || exit /b 1\r\n"));
        assert_eq!(unc_env.as_deref(), Some(r"\\server\共享\repo"));
    }

    #[cfg(target_os = "windows")]
    #[test]
    fn wsl_hermes_dashboard_runs_inside_the_distro_login_shell() {
        let line = wsl_hermes_dashboard_line("Ubuntu-22.04").expect("valid WSL command line");
        assert_eq!(
            line,
            r#"wsl.exe -d Ubuntu-22.04 -- sh -c "\"${SHELL:-sh}\" -lic 'hermes dashboard'""#
        );
        assert!(wsl_hermes_dashboard_line("Ubuntu & calc").is_err());
    }

    #[cfg(unix)]
    #[test]
    fn test_valid_user_shell_path() {
        use std::os::unix::fs::PermissionsExt;

        let temp = tempfile::tempdir().unwrap();
        let set_exec = |path: &Path, executable: bool| {
            std::fs::write(path, "#!/usr/bin/env sh\n").unwrap();
            let mode = if executable { 0o755 } else { 0o644 };
            std::fs::set_permissions(path, std::fs::Permissions::from_mode(mode)).unwrap();
        };
        let executable_zsh = temp.path().join("zsh");
        set_exec(&executable_zsh, true);
        let executable_fish_dir = temp.path().join("homebrew dir/bin");
        std::fs::create_dir_all(&executable_fish_dir).unwrap();
        let executable_fish = executable_fish_dir.join("fish");
        set_exec(&executable_fish, true);
        let non_executable_bash = temp.path().join("bash");
        set_exec(&non_executable_bash, false);

        assert!(valid_user_shell_path(&executable_zsh.to_string_lossy()));
        assert!(valid_user_shell_path(&executable_fish.to_string_lossy()));
        for bad in [
            "".to_string(),
            "zsh".to_string(),
            temp.path()
                .join("missing/zsh")
                .to_string_lossy()
                .into_owned(),
            non_executable_bash.to_string_lossy().into_owned(),
            temp.path()
                .join("zsh; rm -rf /")
                .to_string_lossy()
                .into_owned(),
            format!("{}\n/bin/bash", executable_zsh.to_string_lossy()),
            "/usr/bin/powershell".to_string(),
        ] {
            assert!(!valid_user_shell_path(&bad), "{bad}");
        }
    }

    #[test]
    #[serial_test::serial]
    fn copilot_terminal_launch_passes_custom_copilot_home_without_env() {
        let dir = tempfile::tempdir().unwrap();
        let _env = crate::test_support::EnvVars::new()
            .set("CC_SWITCH_TEST_HOME", dir.path())
            .remove("COPILOT_HOME")
            .remove("COPILOT_PROVIDERS_CONFIG");
        let custom_dir = dir.path().join("custom-copilot");

        let prev = crate::settings::get_settings();
        let mut next = prev.clone();
        next.copilot_config_dir = Some(custom_dir.to_string_lossy().into_owned());
        crate::settings::update_settings(next).unwrap();

        let state = crate::store::AppState::new(std::sync::Arc::new(
            crate::database::Database::memory().unwrap(),
        ));
        let byok_row = crate::provider::Provider::with_id(
            "relay".into(),
            "Relay".into(),
            serde_json::json!({
                "provider": {"type":"openai","wireApi":"responses","baseUrl":"https://relay.test/v1","apiKey":"fixture-key"},
                "models": [{"id":"gpt-5.4"}],
                "reasoningEffort": "medium"
            }),
            None,
        );
        crate::services::provider::ProviderService::add(
            &state,
            AppType::Copilot,
            byok_row.clone(),
            false,
        )
        .unwrap();
        crate::services::provider::ProviderService::add_to_live_config(
            &state,
            AppType::Copilot,
            "relay",
        )
        .unwrap();
        assert!(custom_dir.join("providers.json").is_file());

        let custom_str = custom_dir.to_string_lossy().into_owned();
        let byok_launch = copilot_terminal_launch(&byok_row, None).unwrap();
        assert!(byok_launch
            .env
            .contains(&("COPILOT_HOME", Some(custom_str.clone()))));
        assert!(unix_launch_command(&byok_launch).contains("COPILOT_HOME="));
        assert!(unix_launch_env_file(&byok_launch).contains(&custom_str));

        let mut official_row = crate::provider::Provider::with_id(
            "login".into(),
            "GitHub".into(),
            serde_json::json!({"env":{"COPILOT_MODEL":"claude-sonnet-4.6"}}),
            None,
        );
        official_row.category = Some("official".into());
        let official_launch = copilot_terminal_launch(&official_row, None).unwrap();
        assert!(official_launch
            .env
            .contains(&("COPILOT_HOME", Some(custom_str.clone()))));
        assert!(unix_launch_command(&official_launch).contains("COPILOT_HOME="));
        assert!(unix_launch_env_file(&official_launch).contains(&custom_str));

        crate::settings::update_settings(prev).unwrap();
    }

    #[test]
    #[serial_test::serial]
    fn agy_terminal_launch_uses_override_root_and_preserves_gateway_guard() {
        let dir = tempfile::tempdir().unwrap();
        let _env = crate::test_support::EnvVars::new().set("CC_SWITCH_TEST_HOME", dir.path());
        let custom_root = dir.path().join("custom-agy-root");
        let custom_cli = custom_root.join("antigravity-cli");

        let prev = crate::settings::get_settings();
        let mut next = prev.clone();
        next.agy_config_dir = Some(custom_cli.to_string_lossy().into_owned());
        crate::settings::update_settings(next).unwrap();

        let custom_root_str = custom_root.to_string_lossy().into_owned();
        let mut official = crate::provider::Provider::with_id(
            "login".into(),
            "Google".into(),
            serde_json::json!({"env":{},"config":{"model":{"name":"Gemini 3.1 Pro (High)"}}}),
            None,
        );
        official.category = Some("official".into());

        let launch_with_model = agy_terminal_launch(&official).unwrap();
        assert!(launch_with_model.detail.is_none());
        assert_eq!(
            launch_with_model.args,
            vec![
                format!("--gemini_dir={custom_root_str}"),
                "--model=Gemini 3.1 Pro (High)".to_string(),
            ]
        );
        assert_eq!(
            launch_with_model.batch_command,
            format!(
                "agy \"--gemini_dir=%{AGY_DIR_BATCH_VAR}%\" \"--model=%{AGY_MODEL_BATCH_VAR}%\""
            )
        );
        assert_eq!(
            launch_with_model.batch_vars,
            vec![
                (AGY_DIR_BATCH_VAR, custom_root_str.clone()),
                (AGY_MODEL_BATCH_VAR, "Gemini 3.1 Pro (High)".to_string()),
            ]
        );
        let unix_cmd = unix_launch_command(&launch_with_model);
        assert!(unix_cmd.contains("--gemini_dir="), "{unix_cmd}");
        assert!(
            unix_cmd.contains("'--model=Gemini 3.1 Pro (High)'"),
            "{unix_cmd}"
        );
        assert!(open_agy_resume_terminal(&official, "abc", None).is_err());

        official.settings_config = serde_json::json!({"env":{},"config":{}});
        let launch_plain = agy_terminal_launch(&official).unwrap();
        assert_eq!(
            launch_plain.args,
            vec![format!("--gemini_dir={custom_root_str}")]
        );

        // Active gateway in the overridden config root still blocks official card launch.
        let shared_config_dir = custom_root.join("config");
        std::fs::create_dir_all(&shared_config_dir).unwrap();
        std::fs::write(
            shared_config_dir.join("config.json"),
            r#"{"gateway":{"enabled":true}}"#,
        )
        .unwrap();
        let err = crate::services::provider::agy_direct::terminal_launch(&official).unwrap_err();
        assert!(matches!(
            err,
            crate::error::AppError::Localized {
                key: "agy.terminal.gateway_active",
                ..
            }
        ));

        // Key card still launches with its own private profile even while gateway is active.
        let key_card = crate::provider::Provider::with_id(
            "relay".into(),
            "Relay".into(),
            serde_json::json!({"env":{"GEMINI_API_KEY":"fixture","GOOGLE_GEMINI_BASE_URL":"https://r.test"}}),
            None,
        );
        let key_launch = agy_terminal_launch(&key_card).unwrap();
        assert!(key_launch.detail.is_some());

        crate::settings::update_settings(prev).unwrap();
    }
}