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

125 KB
Raw Blame
mod agy_config;
#[cfg(all(test, unix))]
mod agy_smoke_tests;
mod app_config;
mod app_store;
mod app_update;
mod auto_launch;
mod claude_desktop_config;
mod claude_mcp;
mod claude_plugin;
mod codebuddy_config;
mod codex_config;
mod codex_history_migration;
mod codex_rollout_file;
mod codex_state_db;
mod commands;
mod config;
mod copilot_config;
mod copilot_vscode;
mod database;
mod deeplink;
mod dsh_config;
mod error;
#[cfg(test)]
mod frontend_parity_tests;
mod gemini_config;
mod gemini_mcp;
mod grok_auth;
mod grok_config;
pub mod hermes_config;
mod host_open;
mod init_status;
mod jsonc_document;
mod kimi_config;
mod kimi_session;
mod lightweight;
#[cfg(target_os = "linux")]
mod linux_fix;
#[cfg(target_os = "linux")]
mod linux_rendering;
#[cfg(target_os = "linux")]
mod linux_window_geometry;
pub mod live;
#[cfg(target_os = "macos")]
mod macos_tray_window;
mod mcode_config;
mod mcp;
pub mod mode;
mod model_capabilities;
mod omp_config;
mod openclaw_config;
mod opencode_config;
mod panic_hook;
mod pi_config;
mod prompt;
mod prompt_files;
mod provider;
mod provider_kind;
mod proxy;
mod qoder_accounts;
pub mod qoder_config;
pub mod qoder_models;
mod services;
mod session_manager;
mod settings;
mod store;
mod sync_utils;
#[cfg(test)]
mod test_support;

mod tray;
#[cfg(target_os = "windows")]
mod tray_compat;
mod tray_panel;
mod usage_events;
mod usage_script;
mod window_fit;
mod workbuddy_config;
mod zcode_config;

use crate::sync_utils::MutexExt;
pub use app_config::{AppType, InstalledSkill, McpApps, McpServer, MultiAppConfig, SkillApps};
pub use codex_config::{
    extract_codex_experimental_bearer_token, get_codex_auth_path, get_codex_config_path,
    read_codex_live_settings, write_codex_live_atomic,
};
pub use commands::*;
pub use config::{get_claude_mcp_path, get_claude_settings_path, read_json_file};
pub use database::{Database, Profile};
pub use deeplink::{import_provider_from_deeplink, parse_deeplink_url};
pub use error::AppError;
pub use grok_config::get_grok_config_path;
pub use mcp::{import_from_claude, import_from_codex, sync_single_server_to_codex};
pub use prompt::Prompt;
pub use provider::{Provider, ProviderMeta};
#[doc(hidden)]
pub use services::provider::claude_direct::settle_claude_tombstones;
pub use services::{
    profile::{ProfilePayload, ProfileScope, ProfileService, NO_ACTIVE_PROMPT},
    provider::{
        reapply_current_codex_official_live, reapply_current_codex_third_party_live, EditorSave,
    },
    skill::{migrate_skills_to_ssot, ImportSkillSelection},
    ConfigService, McpService, PromptService, ProviderService, SkillService,
};
pub use settings::{update_settings, AppSettings};
pub use store::AppState;
use tauri_plugin_deep_link::DeepLinkExt;
use tauri_plugin_dialog::{DialogExt, MessageDialogButtons, MessageDialogKind};

use std::sync::atomic::{AtomicBool, Ordering};
use std::{fmt, sync::Arc};
use tauri::tray::TrayIconBuilder;
use tauri::RunEvent;
use tauri::{Emitter, Manager};
use tauri_plugin_window_state::{AppHandleExt, StateFlags};

#[cfg(target_os = "windows")]
fn set_windows_app_user_model_id(app: &tauri::AppHandle) {
    let app_id = app.config().identifier.clone();
    let wide_app_id: Vec<u16> = app_id.encode_utf16().chain(std::iter::once(0)).collect();

    let result = unsafe {
        windows_sys::Win32::UI::Shell::SetCurrentProcessExplicitAppUserModelID(wide_app_id.as_ptr())
    };

    if result < 0 {
        log::warn!("设置 Windows AppUserModelID 失败: 0x{result:08X}");
    } else {
        log::debug!("Windows AppUserModelID 已设置为 {app_id}");
    }
}

#[cfg(target_os = "windows")]
fn disable_windows_general_autofill<R: tauri::Runtime>(webview: &tauri::Webview<R>) {
    use webview2_com::Microsoft::Web::WebView2::Win32::ICoreWebView2Settings4;
    use windows::core::Interface;

    let result = webview.with_webview(|platform_webview| {
        let result = unsafe {
            platform_webview
                .controller()
                .CoreWebView2()
                .and_then(|webview| webview.Settings())
                .and_then(|settings| settings.cast::<ICoreWebView2Settings4>())
                .and_then(|settings| settings.SetIsGeneralAutofillEnabled(false))
        };

        if let Err(error) = result {
            log::warn!("禁用 Windows WebView2 常规自动填充失败: {error}");
        }
    });

    if let Err(error) = result {
        log::warn!("访问 Windows WebView2 失败: {error}");
    }
}

pub(crate) struct RedactedUrl<'a> {
    url: &'a str,
    known_secrets: &'a [String],
}

impl fmt::Display for RedactedUrl<'_> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(&redact_url_for_log_with_secrets(
            self.url,
            self.known_secrets,
        ))
    }
}

/// 为日志提供惰性 URL 脱敏包装;只有日志实际输出时才解析和重建 URL。
pub(crate) fn url_for_log(url: &str) -> RedactedUrl<'_> {
    RedactedUrl {
        url,
        known_secrets: &[],
    }
}

/// 为持有确切认证材料的调用方提供优先精确匹配、再启发式兜底的 URL 脱敏。
pub(crate) fn url_for_log_with_secrets<'a>(
    url: &'a str,
    known_secrets: &'a [String],
) -> RedactedUrl<'a> {
    RedactedUrl { url, known_secrets }
}

/// 已知密钥参与子串脱敏的最短长度:过短的值(如 "api")当作子串会误伤无关文本,
/// 所以只对足够长、几乎不可能是普通词的值做替换。
const MIN_KNOWN_SECRET_LEN: usize = 8;

/// 唯一的密钥脱敏原语:把字符串里出现的、我们确切握有的密钥值替换为 [REDACTED]。
/// 不做任何“看起来像密钥”的形状猜测——只隐藏已知值,天然收敛、不误伤正常路径。
pub(crate) fn redact_known_secrets(text: &str, known_secrets: &[String]) -> String {
    redact_known_secrets_with_min_length(text, known_secrets, MIN_KNOWN_SECRET_LEN)
}

/// Exact-value redaction for user-visible error text. Unlike URL logging, a
/// short known credential must still be hidden because the result reaches the
/// UI rather than a diagnostic-only path.
pub(crate) fn redact_known_secrets_strict(text: &str, known_secrets: &[String]) -> String {
    redact_known_secrets_with_min_length(text, known_secrets, 1)
}

fn redact_known_secrets_with_min_length(
    text: &str,
    known_secrets: &[String],
    minimum_chars: usize,
) -> String {
    let mut output = text.to_string();
    for secret in known_secrets {
        if secret.chars().count() >= minimum_chars {
            output = output.replace(secret.as_str(), "[REDACTED]");
        }
    }
    output
}

/// 无 scheme 的裸 authority 形态(如 `user:pass@host/path`)剥掉 userinfo:
/// 仅当 `@` 出现在第一个 `/` 之前时才视为凭据。
fn strip_bare_userinfo(input: &str) -> &str {
    let authority_end = input.find('/').unwrap_or(input.len());
    match input[..authority_end].rfind('@') {
        Some(at) => &input[at + 1..],
        None => input,
    }
}

pub(crate) fn redact_url_for_log(url_str: &str) -> String {
    redact_url_for_log_with_secrets(url_str, &[])
}

/// 为日志脱敏 URL:剥掉 userinfo(user:pass@) 与整个 query/fragment,保留
/// scheme/host/port/path 供诊断(如 base_url 配错路径导致 404),最后再抹掉已知密钥值。
pub(crate) fn redact_url_for_log_with_secrets(url_str: &str, known_secrets: &[String]) -> String {
    let scheme_relative = url_str.starts_with("//");
    let parsed = if scheme_relative {
        url::Url::parse(&format!("https:{url_str}"))
    } else {
        url::Url::parse(url_str)
    };

    let sanitized = match parsed {
        Ok(mut url) if url.has_host() => {
            let _ = url.set_username("");
            let _ = url.set_password(None);
            url.set_query(None);
            url.set_fragment(None);
            let rendered = url.as_str();
            if scheme_relative {
                rendered
                    .strip_prefix("https:")
                    .unwrap_or(rendered)
                    .to_string()
            } else {
                rendered.to_string()
            }
        }
        _ => {
            // 解析失败(相对路径、含裸 userinfo 的非法 URL 等):丢掉 query/fragment,
            // 尽力剥掉 userinfo,其余原样保留。
            let without_tail = url_str.split(['?', '#']).next().unwrap_or(url_str);
            strip_bare_userinfo(without_tail).to_string()
        }
    };

    redact_known_secrets(&sanitized, known_secrets)
}

/// 只保留 `scheme://host:port`,丢掉 path/query/userinfo。用于我们手里没有任何
/// 已知密钥可脱敏 path 的场景——凭据可能整个内嵌在 base_url 的 path 里,此时
/// 记录 path 无法保证不泄漏,只能退回到 origin。
pub(crate) fn redact_url_origin_for_log(url_str: &str) -> String {
    let scheme_relative = url_str.starts_with("//");
    let parsed = if scheme_relative {
        url::Url::parse(&format!("https:{url_str}"))
    } else {
        url::Url::parse(url_str)
    };

    match parsed {
        Ok(url) if url.has_host() => {
            let authority = &url[url::Position::BeforeHost..url::Position::AfterPort];
            if scheme_relative {
                format!("//{authority}")
            } else {
                format!("{}://{authority}", url.scheme())
            }
        }
        _ => "[invalid target]".to_string(),
    }
}

/// 给日志用的错误文本:去掉 TOML 解析诊断里引用的源码行(`1 | key = "..."` 和它上下的
/// `|`、`^` 标注行)。那一行是用户配置原文,出错的可能正是密钥那一行;行列号和原因留着。
pub(crate) fn error_for_log(error: &str) -> String {
    error
        .lines()
        .filter(|line| {
            !line
                .trim_start()
                .trim_start_matches(|c: char| c.is_ascii_digit())
                .trim_start()
                .starts_with('|')
        })
        .collect::<Vec<_>>()
        .join("\n")
}

fn runtime_log_level_allows(level: log::Level, max_level: log::LevelFilter) -> bool {
    max_level.to_level().is_some_and(|maximum| level <= maximum)
}

/// 统一处理 ccswitch:// 深链接 URL
///
/// - 解析 URL,把结果(或错误)暂存为待处理深链接,再发 `deeplink-pending` 通知前端来读
///   (前端可能还没挂载,见 `deeplink::PendingDeepLink`)
/// - 可选:聚焦主窗口
fn handle_deeplink_url(
    app: &tauri::AppHandle,
    url_str: &str,
    focus_main_window: bool,
    source: &str,
) -> bool {
    if !url_str.starts_with("ccswitch://") {
        return false;
    }

    log::info!(
        "✓ Deep link URL detected from {source}: {}",
        url_for_log(url_str)
    );

    let pending = match crate::deeplink::parse_deeplink_url(url_str) {
        Ok(request) => {
            log::info!(
                "✓ Successfully parsed deep link: resource={}, app={:?}, name={:?}",
                request.resource,
                request.app,
                request.name
            );
            crate::deeplink::PendingDeepLink::Import {
                request: Box::new(request),
            }
        }
        Err(e) => {
            log::error!("✗ Failed to parse deep link URL: {e}");
            crate::deeplink::PendingDeepLink::Error {
                url: url_str.to_string(),
                error: e.to_string(),
            }
        }
    };
    crate::deeplink::set_pending_deeplink(pending);

    if let Err(e) = app.emit("deeplink-pending", ()) {
        log::error!("✗ Failed to emit deeplink-pending event: {e}");
    }

    if focus_main_window {
        if let Some(window) = app.get_webview_window("main") {
            tray::reveal_main_window(&window, "deeplink");
            log::info!("✓ Window shown and focused");
        }
    }

    true
}

/// 更新托盘菜单的Tauri命令
#[tauri::command]
async fn update_tray_menu(
    app: tauri::AppHandle,
    state: tauri::State<'_, AppState>,
) -> Result<bool, String> {
    if !tray_panel::uses_native_menu() {
        // 用托盘面板、不挂原生菜单:更新图标状态,通知开着的面板重新取数据。
        tray::refresh_tray_menu(&app);
        return Ok(app.tray_by_id(tray::TRAY_ID).is_some());
    }
    match tray::create_tray_menu(&app, state.inner()) {
        Ok(new_menu) => {
            if let Some(tray) = app.tray_by_id(tray::TRAY_ID) {
                tray.set_menu(Some(new_menu))
                    .map_err(|e| format!("更新托盘菜单失败: {e}"))?;
                return Ok(true);
            }
            Ok(false)
        }
        Err(err) => {
            log::error!("创建托盘菜单失败: {err}");
            Ok(false)
        }
    }
}

/// 本进程打开文件描述符的软上限目标。macOS 的 `setrlimit` 不接受超过 `OPEN_MAX`(10240)的值。
#[cfg(unix)]
const OPEN_FILE_SOFT_LIMIT_TARGET: libc::rlim_t = 10240;

/// 从 Finder / 登录项启动的 macOS 应用继承 launchd 的软上限,默认只有 256 个文件描述符。
/// 本地路由并发时客户端连接、上游连接、日志、数据库和配置文件都要占用描述符,突发请求下
/// accept、上游连接和打开数据库会一起报 EMFILE(Too many open files)。启动时把软上限
/// 提到硬上限(封顶 10240),硬上限更低或已经足够时不动。
#[cfg(unix)]
fn raise_open_file_limit() {
    let mut limit = libc::rlimit {
        rlim_cur: 0,
        rlim_max: 0,
    };
    // SAFETY: 传入的是有效的 rlimit 指针,调用只读写这个结构体。
    if unsafe { libc::getrlimit(libc::RLIMIT_NOFILE, &mut limit) } != 0 {
        log::warn!(
            "读取文件描述符上限失败: {}",
            std::io::Error::last_os_error()
        );
        return;
    }
    let target = OPEN_FILE_SOFT_LIMIT_TARGET.min(limit.rlim_max);
    if limit.rlim_cur >= target {
        return;
    }
    let previous = limit.rlim_cur;
    limit.rlim_cur = target;
    // SAFETY: 同上;软上限不超过硬上限,不需要特权。
    if unsafe { libc::setrlimit(libc::RLIMIT_NOFILE, &limit) } != 0 {
        log::warn!(
            "提高文件描述符上限失败(当前 {previous}): {}",
            std::io::Error::last_os_error()
        );
    } else {
        log::info!("文件描述符软上限 {previous} → {target}");
    }
}

/// 又启动了一个实例(或经命令行打开 ccswitch:// 链接):退出轻量模式、处理链接、把主窗口拿到前台。
/// 在后台线程上调用(见 single_instance 回调),一次处理一个:连着启动两次时,
/// 不能两边都看到主窗口不在、各建一个。
#[cfg(any(target_os = "macos", target_os = "windows", target_os = "linux"))]
fn handle_second_instance(app: &tauri::AppHandle, args: Vec<String>) {
    static SERIAL: std::sync::Mutex<()> = std::sync::Mutex::new(());
    let _serial = SERIAL.lock_recover();
    log::debug!("Args count: {}", args.len());
    for (i, arg) in args.iter().enumerate() {
        log::debug!("  arg[{i}]: {}", url_for_log(arg));
    }

    if crate::lightweight::is_lightweight_mode() {
        if let Err(e) = crate::lightweight::exit_lightweight_mode(app) {
            log::error!("退出轻量模式重建窗口失败: {e}");
        }
    }

    // Check for deep link URL in args (mainly for Windows/Linux command line)
    let mut found_deeplink = false;
    for arg in &args {
        if handle_deeplink_url(app, arg, false, "single_instance args") {
            found_deeplink = true;
            break;
        }
    }

    if !found_deeplink {
        log::info!(
            "ℹ No deep link URL found in args (this is expected on macOS when launched via system)"
        );
    }

    // Show and focus window regardless
    if let Some(window) = app.get_webview_window("main") {
        tray::reveal_main_window(&window, "single-instance");
    }
}

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
    // WebKit caches its process-wide renderer settings before creating the first view.
    #[cfg(target_os = "linux")]
    let rendering = linux_rendering::configure_environment();

    // 设置 panic hook,在应用崩溃时记录日志到 <app_config_dir>/crash.log(默认 ~/.cc-switch/crash.log)
    panic_hook::setup_panic_hook();

    let mut builder = tauri::Builder::default();

    // WebView settings are per-view: apply this to initial and recreated windows,
    // including the lightweight tray panel (#9216), before they are shown.
    #[cfg(target_os = "linux")]
    {
        builder = builder.plugin(
            tauri::plugin::Builder::<_, ()>::new("linux-webview-rendering")
                .on_webview_ready(move |webview| rendering.configure_webview(&webview))
                .build(),
        );
        builder = builder.manage(rendering);
        builder = builder.plugin(linux_window_geometry::init());
    }

    #[cfg(target_os = "windows")]
    {
        builder = builder.plugin(
            tauri::plugin::Builder::<_, ()>::new("windows-general-autofill")
                .on_webview_ready(|webview| {
                    if webview.label() == "main" {
                        disable_windows_general_autofill(&webview);
                    }
                })
                .build(),
        );
    }

    #[cfg(any(target_os = "macos", target_os = "windows", target_os = "linux"))]
    {
        builder = builder.plugin(tauri_plugin_single_instance::init(|app, args, _cwd| {
            log::info!("=== Single Instance Callback Triggered ===");
            // Windows 上这个回调跑在主线程处理 WM_COPYDATA 的窗口过程里,第二个实例的
            // SendMessage 正等它返回:轻量模式下要在这里同步重建主窗口,WebView2 会把主线程
            // 卡死(托盘随之无响应)。挪到后台线程,建窗口改由事件循环处理,和 macOS / Linux
            // 本来就不在主线程上调用一致。
            let app = app.clone();
            tauri::async_runtime::spawn_blocking(move || handle_second_instance(&app, args));
        }));
    }

    #[cfg(target_os = "windows")]
    {
        let startup_page_handled = AtomicBool::new(false);
        builder = builder.on_page_load(move |webview, payload| {
            if webview.label() == "main"
                && payload.event() == tauri::webview::PageLoadEvent::Finished
                && payload.url().scheme() != "about"
                && !startup_page_handled.swap(true, Ordering::Relaxed)
                && !crate::settings::get_settings().silent_startup
            {
                let window = webview.window();
                if let Some(main) = webview.app_handle().get_webview_window("main") {
                    window_fit::fit_to_work_area(&main);
                }
                if window.show().is_ok() {
                    let _ = window.emit("main-window-visibility-changed", ());
                }
                log::info!("主页面加载完成,主窗口已显示");
            }
        });
    }

    let window_state = tauri_plugin_window_state::Builder::default()
        .with_state_flags(window_state_flags())
        .with_denylist(&[tray_panel::PANEL_LABEL]);
    #[cfg(target_os = "linux")]
    let window_state = window_state.skip_initial_state("main");

    let builder = builder
        // 注册 deep-link 插件(处理 macOS AppleEvent 和其他平台的深链接)
        .plugin(tauri_plugin_deep_link::init())
        // 拦截窗口关闭:根据设置决定是否最小化到托盘
        .on_window_event(|window, event| {
            // 托盘面板自己处理关闭(只隐藏),不走主窗口的「最小化到托盘 / 退出」
            if window.label() == tray_panel::PANEL_LABEL {
                return;
            }
            if let tauri::WindowEvent::CloseRequested { api, .. } = event {
                log::info!("收到窗口关闭请求: label={}", window.label());
                // 数据库版本过新的恢复模式下没有托盘可唤回,关闭即退出,避免应用隐身后台
                let in_db_recovery = crate::init_status::get_init_error()
                    .map(|p| p.kind.as_deref() == Some("db_version_too_new"))
                    .unwrap_or(false);
                if in_db_recovery {
                    api.prevent_close();
                    window.app_handle().exit(0);
                    return;
                }

                let settings = crate::settings::get_settings();

                if settings.minimize_to_tray_on_close {
                    api.prevent_close();
                    #[cfg(target_os = "macos")]
                    if window.label() == "main" && macos_tray_window::close_to_tray(window) {
                        return;
                    }
                    tray::hide_main_to_tray(window);
                } else {
                    api.prevent_close();
                    log::info!("关闭请求已处理:退出应用");
                    window.app_handle().exit(0);
                }
            }
        })
        .plugin(tauri_plugin_process::init())
        .plugin(tauri_plugin_dialog::init())
        .plugin(tauri_plugin_opener::init())
        .plugin(tauri_plugin_store::Builder::new().build())
        .plugin(window_state.build())
        .setup(|app| {
            let _ = rustls::crypto::ring::default_provider().install_default();

            // 预先刷新 Store 覆盖配置,确保后续路径读取正确(日志/数据库等)
            app_store::refresh_app_config_dir_override(app.handle());
            panic_hook::init_app_config_dir(crate::config::get_app_config_dir());

            // 初始化日志(输出到 <app_config_dir>/logs/cc-switch.log)
            {
                use tauri_plugin_log::{RotationStrategy, Target, TargetKind, TimezoneStrategy};

                let log_dir = panic_hook::get_log_dir();

                // 确保日志目录存在
                if let Err(e) = std::fs::create_dir_all(&log_dir) {
                    eprintln!("创建日志目录失败: {e}");
                }

                app.handle().plugin(
                    tauri_plugin_log::Builder::default()
                        // 底层保留 Trace 能力,便于加载用户配置后动态调高级别。
                        // 插件注册后会立即把全局级别收紧到 Info,避免启动阶段全量 Trace。
                        .level(log::LevelFilter::Trace)
                        // plugin-log 的前端 command 会直达 logger,绕过 log 宏的全局
                        // max_level;在分发层补一次过滤,确保动态总开关同样约束前端日志。
                        .filter(|metadata| {
                            runtime_log_level_allows(metadata.level(), log::max_level())
                        })
                        .targets([
                            Target::new(TargetKind::Stdout),
                            Target::new(TargetKind::Folder {
                                path: log_dir,
                                file_name: Some("cc-switch".into()),
                            }),
                        ])
                        // KeepSome(4) 保留 4 个轮转归档,加上当前文件最多约 100 MiB。
                        // 轮转仅按大小触发;跨重启继续追加,不再丢失上一次运行的日志。
                        .rotation_strategy(RotationStrategy::KeepSome(4))
                        .max_file_size(20 * 1024 * 1024)
                        .timezone_strategy(TimezoneStrategy::UseLocal)
                        .build(),
                )?;

                // 用户配置存在数据库中,数据库尚未打开时使用保守的 Info 级别。
                log::set_max_level(log::LevelFilter::Info);
                log::info!("=== CC Switch v{} started ===", env!("CARGO_PKG_VERSION"));
            }

            #[cfg(unix)]
            raise_open_file_limit();

            // 首次读取覆盖路径时 logger 尚未可用;此处重放一次,
            // 让 Store 损坏或路径无效等启动警告能够真正落盘。
            let _ = app_store::refresh_app_config_dir_override(app.handle());

            #[cfg(target_os = "windows")]
            set_windows_app_user_model_id(app.handle());

            // 注册 Updater 插件(桌面端);放在 logger 之后,确保失败可诊断。
            #[cfg(desktop)]
            {
                if let Err(e) = app
                    .handle()
                    .plugin(tauri_plugin_updater::Builder::new().build())
                {
                    // 若配置不完整(如缺少 pubkey),跳过 Updater 而不中断应用
                    log::warn!("初始化 Updater 插件失败,已跳过:{e}");
                }
            }

            // 注入 AppHandle 给 usage_events,让无 AppHandle 持有的写日志路径
            // 也能向前端推送 `usage-log-recorded`。
            // 放在日志系统初始化之后,确保 init 的日志能正常输出。
            usage_events::init(app.handle().clone());

            // 初始化数据库
            let app_config_dir = crate::config::get_app_config_dir();
            let db_path = app_config_dir.join("cc-switch.db");
            let json_path = app_config_dir.join("config.json");

            // 检查是否需要从 config.json 迁移到 SQLite
            let has_json = json_path.exists();
            let has_db = db_path.exists();

            // 如果需要迁移,先验证 config.json 是否可以加载(在创建数据库之前)
            // 这样如果加载失败用户选择退出,数据库文件还没被创建,下次可以正常重试
            let migration_config = if !has_db && has_json {
                log::info!("检测到旧版配置文件,验证配置文件...");

                // 循环:支持用户重试加载配置文件
                loop {
                    match crate::app_config::MultiAppConfig::load() {
                        Ok(config) => {
                            log::info!("✓ 配置文件加载成功");
                            break Some(config);
                        }
                        Err(e) => {
                            log::error!("加载旧配置文件失败: {e}");
                            // 弹出系统对话框让用户选择
                            if !show_migration_error_dialog(app.handle(), &e.to_string()) {
                                // 用户选择退出(此时数据库还没创建,下次启动可以重试)
                                log::info!("用户选择退出程序");
                                std::process::exit(1);
                            }
                            // 用户选择重试,继续循环
                            log::info!("用户选择重试加载配置文件");
                        }
                    }
                }
            } else {
                None
            };

            // 现在创建数据库(包含 Schema 迁移)
            //
            // 说明:从 v3.8.* 升级的用户通常会走到这里的 SQLite schema 迁移,
            // 若迁移失败(数据库损坏/权限不足/user_version 过新等),需要给用户明确提示,
            // 否则表现可能只是“应用打不开/闪退”。
            //
            // 预检:数据库版本过新时,必须先于任何 schema 写操作(create_tables 内含
            // DROP/ALTER 等 DDL)进入恢复界面,避免旧应用对读不懂的更新版 DB 落写。
            match crate::database::Database::stored_user_version_exceeds_supported(&db_path) {
                Ok(Some(version)) => {
                    log::warn!("数据库版本过新(v{version}),引导用户在应用内升级应用");
                    crate::init_status::set_init_error(crate::init_status::InitErrorPayload {
                        path: db_path.display().to_string(),
                        error: format!(
                            "数据库版本过新({version}),当前应用仅支持 {},请升级应用后再尝试。",
                            crate::database::SCHEMA_VERSION
                        ),
                        kind: Some("db_version_too_new".to_string()),
                        db_version: Some(version),
                        supported_version: Some(crate::database::SCHEMA_VERSION),
                    });
                    // 主窗口默认 visible:false,恢复界面必须强制显示
                    if let Some(window) = app.get_webview_window("main") {
                        #[cfg(target_os = "windows")]
                        {
                            let _ = window.set_skip_taskbar(false);
                        }
                        #[cfg(not(target_os = "linux"))]
                        window_fit::fit_to_work_area(&window);
                        let _ = window.show();
                        let _ = window.set_focus();
                    }
                    return Ok(());
                }
                Ok(None) => {}
                Err(e) => {
                    log::warn!("预检数据库版本失败,继续正常初始化流程: {e}");
                }
            }

            let db = loop {
                match crate::database::Database::init() {
                    Ok(db) => break Arc::new(db),
                    Err(e) => {
                        log::error!("Failed to init database: {e}");

                        if !show_database_init_error_dialog(app.handle(), &db_path, &e.to_string())
                        {
                            log::info!("用户选择退出程序");
                            std::process::exit(1);
                        }

                        log::info!("用户选择重试初始化数据库");
                    }
                }
            };

            // 数据库可用后立即应用持久化日志级别,避免后续服务初始化
            // 继续使用启动阶段的 Info 回退。损坏配置显式 fail-closed 到 Info。
            match db.get_log_config() {
                Ok(log_config) => {
                    log::set_max_level(log_config.to_level_filter());
                    log::info!(
                        "已加载日志配置: enabled={}, level={}",
                        log_config.enabled,
                        log_config.level
                    );
                }
                Err(e) => {
                    log::set_max_level(log::LevelFilter::Info);
                    log::warn!("读取日志配置失败,已回退到 info: {e}");
                }
            }

            // 如果有预加载的配置,执行迁移
            if let Some(config) = migration_config {
                log::info!("开始执行数据迁移...");

                match db.migrate_from_json(&config) {
                    Ok(_) => {
                        log::info!("✓ 配置迁移成功");
                        // 标记迁移成功,供前端显示 Toast
                        crate::init_status::set_migration_success();
                        // 归档旧配置文件(重命名而非删除,便于用户恢复)
                        let archive_path = json_path.with_extension("json.migrated");
                        if let Err(e) = std::fs::rename(&json_path, &archive_path) {
                            log::warn!("归档旧配置文件失败: {e}");
                        } else {
                            log::info!("✓ 旧配置已归档为 config.json.migrated");
                        }
                    }
                    Err(e) => {
                        // 配置加载成功但迁移失败的情况极少(磁盘满等),仅记录日志
                        log::error!("配置迁移失败: {e},将从现有配置导入");
                    }
                }
            }

            let app_state = AppState::new(db);

            // 设置 AppHandle 用于代理故障转移时的 UI 更新
            app_state.proxy_service.set_app_handle(app.handle().clone());

            // 补完上次崩溃时写到一半的客户端文件(写前意图在 ~/.cc-switch/live-state.json),
            // 要在任何写客户端文件的启动步骤之前。
            crate::mode::operation::recover_on_startup(&app_state.db);
            // 上次退出或崩溃前没来得及删的 Claude 空值墓碑(切换时给开着的会话写的)接着删
            crate::services::provider::claude_direct::resume_tombstone_sweep();

            // ============================================================
            // 按表独立判断的导入逻辑(各类数据独立检查,互不影响)
            // ============================================================

            // 1. 初始化默认 Skills 仓库(已有内置检查:表非空则跳过)
            match app_state.db.init_default_skill_repos() {
                Ok(count) if count > 0 => {
                    log::info!("✓ Initialized {count} default skill repositories");
                }
                Ok(_) => {} // 表非空,静默跳过
                Err(e) => log::warn!("✗ Failed to initialize default skill repos: {e}"),
            }

            // 1.1. Skills 统一管理迁移:当数据库迁移到 v3 结构后,自动从各应用目录导入到 SSOT
            // 触发条件由 schema 迁移设置 settings.skills_ssot_migration_pending = true 控制。
            match app_state.db.get_setting("skills_ssot_migration_pending") {
                Ok(Some(flag)) if flag == "true" || flag == "1" => {
                    // 安全保护:如果用户已经有 v3 结构的 Skills 数据,就不要自动清空重建。
                    let has_existing = app_state
                        .db
                        .get_all_installed_skills()
                        .map(|skills| !skills.is_empty())
                        .unwrap_or(false);

                    if has_existing {
                        log::info!(
                            "Detected skills_ssot_migration_pending but skills table not empty; skipping auto import."
                        );
                        let _ = app_state
                            .db
                            .set_setting("skills_ssot_migration_pending", "false");
                    } else {
                        match crate::services::skill::migrate_skills_to_ssot(&app_state.db) {
                            Ok(count) => {
                                log::info!("✓ Auto imported {count} skill(s) into SSOT");
                                if count > 0 {
                                    crate::init_status::set_skills_migration_result(count);
                                }
                                let _ = app_state
                                    .db
                                    .set_setting("skills_ssot_migration_pending", "false");
                            }
                            Err(e) => {
                                log::warn!("✗ Failed to auto import legacy skills to SSOT: {e}");
                                crate::init_status::set_skills_migration_error(e.to_string());
                                // 保留 pending 标志,方便下次启动重试
                            }
                        }
                    }
                }
                Ok(_) => {} // 未开启迁移标志,静默跳过
                Err(e) => log::warn!("✗ Failed to read skills migration flag: {e}"),
            }

            // 1.5. 自动导入 live 配置 + seed 官方预设供应商(Claude / Codex / Gemini)
            //
            // 先 import 后 seed 是有意为之:先把用户手动配置的 settings.json / auth.json / .env
            // 落成 "default" provider 设为 current,再追加官方预设(is_current=false)。
            // 降级回旧版时,旧版的回填和整份写入仍靠这一行保住用户原来的 live 配置。
            //
            // 捕获首次运行快照:所有全新装用户都会看到欢迎弹窗介绍 CC Switch 的工作方式。
            // 读失败时默认不弹,宁可漏弹也不要因为故障打扰用户。
            let first_run_already_confirmed = crate::settings::get_settings()
                .first_run_notice_confirmed
                .unwrap_or(false);
            let fresh_install_at_startup =
                app_state.db.is_providers_empty().unwrap_or(false);

            for app_type in
                crate::app_config::AppType::all()
                    .filter(crate::services::provider::imports_default_config)
            {
                if !crate::services::provider::should_import_default_config_on_startup(
                    &app_state,
                    &app_type,
                )
                .unwrap_or(false)
                {
                    log::debug!(
                        "○ {} already has providers; live import skipped",
                        app_type.as_str()
                    );
                    continue;
                }

                match crate::services::provider::import_default_config(
                    &app_state,
                    app_type.clone(),
                ) {
                    Ok(true) => log::info!(
                        "✓ Imported live config for {} as default provider",
                        app_type.as_str()
                    ),
                    Ok(false) => log::debug!(
                        "○ {} already has providers; live import skipped",
                        app_type.as_str()
                    ),
                    Err(e) => log::debug!(
                        "○ No live config to import for {}: {e}",
                        app_type.as_str()
                    ),
                }
            }

            if let Err(error) = crate::services::provider::initialize_dsh_profiles(&app_state) {
                log::warn!("Initializing DeepSeek Harness profiles failed: {error}");
            }

            match app_state.db.init_default_official_providers() {
                Ok(count) if count > 0 => {
                    log::info!("✓ Seeded {count} official provider(s)");
                }
                Ok(_) => {}
                Err(e) => log::warn!("✗ Failed to seed official providers: {e}"),
            }

            {
                let db_for_codex_history_migration = app_state.db.clone();
                tauri::async_runtime::spawn_blocking(move || {
                    match crate::codex_history_migration::maybe_migrate_codex_third_party_history_provider_bucket(
                        &db_for_codex_history_migration,
                    ) {
                        Ok(outcome) => {
                            if let Some(reason) = outcome.skipped_reason {
                                log::debug!("○ Codex history provider bucket migration skipped: {reason}");
                            } else {
                                log::info!(
                                    "✓ Codex history provider bucket migration completed: sources={}, jsonl_files={}, state_rows={}",
                                    outcome.source_provider_ids.len(),
                                    outcome.migrated_jsonl_files,
                                    outcome.migrated_state_rows
                                );
                            }
                        }
                        Err(e) => {
                            log::warn!("✗ Codex history provider bucket migration failed: {e}");
                        }
                    }

                    match crate::codex_history_migration::maybe_migrate_codex_provider_template_bucket(
                        &db_for_codex_history_migration,
                    ) {
                        Ok(outcome) => {
                            if let Some(reason) = outcome.skipped_reason {
                                log::debug!("○ Codex provider template bucket migration skipped: {reason}");
                            } else if !outcome.migrated_provider_ids.is_empty() {
                                log::info!(
                                    "✓ Codex provider template bucket migration completed: providers={}",
                                    outcome.migrated_provider_ids.len()
                                );
                            }
                        }
                        Err(e) => {
                            log::warn!("✗ Codex provider template bucket migration failed: {e}");
                        }
                    }

                    // 旧版官方路由桶(cc-switch-official)迁回 openai,须在统一迁移之前:
                    // 开关开着时,紧接着的统一迁移会把它们按正常流程带进 custom。
                    match crate::codex_history_migration::maybe_migrate_codex_legacy_official_history_to_openai_bucket() {
                        Ok(outcome) => {
                            if let Some(reason) = outcome.skipped_reason {
                                log::debug!("○ Codex legacy official history bucket migration skipped: {reason}");
                            } else {
                                log::info!(
                                    "✓ Codex legacy official history bucket migration completed: jsonl_files={}, state_rows={}",
                                    outcome.migrated_jsonl_files,
                                    outcome.migrated_state_rows
                                );
                            }
                        }
                        Err(e) => {
                            log::warn!("✗ Codex legacy official history bucket migration failed: {e}");
                        }
                    }

                    // 统一会话开关的官方历史迁移:开关开着且勾选了迁入既有会话时,每次启动
                    // 都把留在 openai 桶的会话迁进 custom(新版 Codex 会把它们锁成只读);
                    // 函数内部自门控,state DB 里没有 openai 线程时直接跳过。
                    match crate::codex_history_migration::maybe_migrate_codex_official_history_to_unified_bucket() {
                        Ok(outcome) => {
                            if let Some(reason) = outcome.skipped_reason {
                                log::debug!("○ Codex official history unify migration skipped: {reason}");
                            } else {
                                log::info!(
                                    "✓ Codex official history unify migration completed: jsonl_files={}, state_rows={}",
                                    outcome.migrated_jsonl_files,
                                    outcome.migrated_state_rows
                                );
                            }
                        }
                        Err(e) => {
                            log::warn!("✗ Codex official history unify migration failed: {e}");
                        }
                    }
                });
            }

            // 老用户 / 已确认的路径由 `fresh_install_at_startup` 自行拦截,这里不做写入。
            // 字段只由前端在用户点击"我知道了"时 save_settings 回写,语义是"用户显式确认过"。
            if !first_run_already_confirmed && fresh_install_at_startup {
                log::info!("✓ First-run welcome notice pending");
            }

            // 1.6. 自动同步累加模式应用的原生 providers 到数据库
            //
            // additive 模式的 import 函数按 id 幂等——
            // 新 id 执行导入,已有 id 则更新 settings 和 display name,所以每次
            // 启动都跑是安全的:既保证新装用户开箱可见 live 中的供应商,也让外部
            // 修改的 live 文件能在重启后同步到数据库(与之前依赖前端"导入当前配置"
            // 按钮手动触发不同)。
            //
            // 底层 read_*_config 在文件不存在时返回默认空配置,因此新装且无
            // live 文件的用户走 Ok(0) 路径,不会产生错误日志噪音。
            match crate::services::provider::import_opencode_providers_from_live(&app_state) {
                Ok(count) if count > 0 => {
                    log::info!("✓ Synced {count} OpenCode provider(s) from live config");
                }
                Ok(_) => log::debug!("○ No OpenCode provider changes from live config"),
                Err(e) => log::warn!("✗ Failed to import OpenCode providers: {e}"),
            }
            match crate::services::provider::import_openclaw_providers_from_live(&app_state) {
                Ok(count) if count > 0 => {
                    log::info!("✓ Synced {count} OpenClaw provider(s) from live config");
                }
                Ok(_) => log::debug!("○ No OpenClaw provider changes from live config"),
                Err(e) => log::warn!("✗ Failed to import OpenClaw providers: {e}"),
            }
            match crate::services::provider::import_hermes_providers_from_live(&app_state) {
                Ok(count) if count > 0 => {
                    log::info!("✓ Synced {count} Hermes provider(s) from live config");
                }
                Ok(_) => log::debug!("○ No Hermes provider changes from live config"),
                Err(e) => log::warn!("✗ Failed to import Hermes providers: {e}"),
            }
            match crate::services::provider::import_pi_providers_from_live(&app_state) {
                Ok(count) if count > 0 => {
                    log::info!("✓ Synced {count} Pi provider(s) from native config");
                }
                Ok(_) => log::debug!("○ No Pi provider changes from native config"),
                Err(e) => log::warn!("✗ Failed to import Pi providers: {e}"),
            }
            match crate::services::provider::import_omp_providers_from_live(&app_state) {
                Ok(count) if count > 0 => {
                    log::info!("✓ Synced {count} Oh My Pi provider(s) from native config");
                }
                Ok(_) => log::debug!("○ No Oh My Pi provider changes from native config"),
                Err(e) => log::warn!("✗ Failed to import Oh My Pi providers: {e}"),
            }
            match crate::services::provider::import_zcode_providers_from_live(&app_state) {
                Ok(count) if count > 0 => {
                    log::info!("✓ Synced {count} ZCode provider(s) from live config");
                }
                Ok(_) => log::debug!("○ No ZCode provider changes from live config"),
                Err(e) => log::warn!("✗ Failed to import ZCode providers: {e}"),
            }

            // 2. OMO 配置导入(当数据库中无 OMO provider 时,从本地文件导入)
            {
                let has_omo = app_state
                    .db
                    .get_all_providers(&AppType::OpenCode)
                    .map(|providers| providers.values().any(|p| p.category.as_deref() == Some("omo")))
                    // 读不出 OpenCode 的供应商时不导入:没法确认已有,导入会每次启动都多加一份。
                    .unwrap_or(true);
                if !has_omo {
                    match crate::services::OmoService::import_from_local(&app_state, &crate::services::omo::STANDARD) {
                        Ok(provider) => {
                            log::info!("✓ Imported OMO config from local as provider '{}'", provider.name);
                        }
                        Err(AppError::OmoConfigNotFound) => {
                            log::debug!("○ No OMO config to import");
                        }
                        Err(e) => {
                            log::warn!("✗ Failed to import OMO config from local: {e}");
                        }
                    }
                }
            }

            // 2.3 OMO Slim config import (when no omo-slim provider in DB, import from local)
            {
                let has_omo_slim = app_state
                    .db
                    .get_all_providers(&AppType::OpenCode)
                    .map(|providers| {
                        providers
                            .values()
                            .any(|p| p.category.as_deref() == Some("omo-slim"))
                    })
                    // 读不出 OpenCode 的供应商时不导入:没法确认已有,导入会每次启动都多加一份。
                    .unwrap_or(true);
                if !has_omo_slim {
                    match crate::services::OmoService::import_from_local(&app_state, &crate::services::omo::SLIM) {
                        Ok(provider) => {
                            log::info!(
                                "✓ Imported OMO Slim config from local as provider '{}'",
                                provider.name
                            );
                        }
                        Err(AppError::OmoConfigNotFound) => {
                            log::debug!("○ No OMO Slim config to import");
                        }
                        Err(e) => {
                            log::warn!("✗ Failed to import OMO Slim config from local: {e}");
                        }
                    }
                }
            }

            // 3. 导入 MCP 服务器配置(表空时触发)
            if app_state.db.is_mcp_table_empty().unwrap_or(false) {
                log::info!("MCP table empty, importing from live configurations...");

                for (app, label) in [
                    (crate::app_config::AppType::Claude, "Claude"),
                    (crate::app_config::AppType::Codex, "Codex"),
                    (crate::app_config::AppType::Gemini, "Gemini"),
                    (crate::app_config::AppType::GrokBuild, "Grok Build"),
                    (crate::app_config::AppType::OpenCode, "OpenCode"),
                    (crate::app_config::AppType::Hermes, "Hermes"),
                ] {
                    match crate::services::mcp::McpService::import_from_app(&app_state, &app) {
                        Ok(count) if count > 0 => {
                            log::info!("✓ Imported {count} MCP server(s) from {label}");
                        }
                        Ok(_) => log::debug!("○ No {label} MCP servers found to import"),
                        Err(e) => log::warn!("✗ Failed to import {label} MCP: {e}"),
                    }
                }
            }

            // 4. 导入提示词文件(表空时触发)
            if app_state.db.is_prompts_table_empty().unwrap_or(false) {
                log::info!("Prompts table empty, importing from live configurations...");

                for app in [
                    crate::app_config::AppType::Claude,
                    crate::app_config::AppType::Codex,
                    crate::app_config::AppType::Gemini,
                    crate::app_config::AppType::GrokBuild,
                    crate::app_config::AppType::OpenCode,
                    crate::app_config::AppType::OpenClaw,
                    crate::app_config::AppType::Hermes,
                    crate::app_config::AppType::Pi,
                    crate::app_config::AppType::Mcode,
                    crate::app_config::AppType::WorkBuddy,
                    crate::app_config::AppType::Codebuddy,
                ] {
                    match crate::services::prompt::PromptService::import_from_file_on_first_launch(
                        &app_state,
                        app.clone(),
                    ) {
                        Ok(count) if count > 0 => {
                            log::info!("✓ Imported {count} prompt(s) for {}", app.as_str());
                        }
                        Ok(_) => log::debug!("○ No prompt file found for {}", app.as_str()),
                        Err(e) => log::warn!("✗ Failed to import prompt for {}: {e}", app.as_str()),
                    }
                }
            }

            // 迁移旧的 app_config_dir 配置到 Store
            if let Err(e) = app_store::migrate_app_config_dir_from_settings(app.handle()) {
                log::warn!("迁移 app_config_dir 失败: {e}");
            }

            // 启动阶段不再无条件保存,避免意外覆盖用户配置。

            // 注册 deep-link URL 处理器(使用正确的 DeepLinkExt API)
            log::info!("=== Registering deep-link URL handler ===");

            // Linux 和 Windows 调试模式需要显式注册
            #[cfg(any(target_os = "linux", all(debug_assertions, windows)))]
            {
                #[cfg(target_os = "linux")]
                {
                    // Use Tauri's path API to get correct path (includes app identifier)
                    // tauri-plugin-deep-link writes to: ~/.local/share/com.ccswitch.desktop/applications/cc-switch-handler.desktop
                    // Only register if .desktop file doesn't exist to avoid overwriting user customizations
                    let should_register = app
                        .path()
                        .data_dir()
                        .map(|d| !d.join("applications/cc-switch-handler.desktop").exists())
                        .unwrap_or(true);

                    if should_register {
                        if let Err(e) = app.deep_link().register_all() {
                            log::error!("✗ Failed to register deep link schemes: {}", e);
                        } else {
                            log::info!("✓ Deep link schemes registered (Linux)");
                        }
                    } else {
                        log::info!("⊘ Deep link handler already exists, skipping registration");
                    }
                }

                #[cfg(all(debug_assertions, windows))]
                {
                    if let Err(e) = app.deep_link().register_all() {
                        log::error!("✗ Failed to register deep link schemes: {}", e);
                    } else {
                        log::info!("✓ Deep link schemes registered (Windows debug)");
                    }
                }
            }

            // 注册 URL 处理回调(所有平台通用)
            app.deep_link().on_open_url({
                let app_handle = app.handle().clone();
                move |event| {
                    log::info!("=== Deep Link Event Received (on_open_url) ===");
                    let urls = event.urls();
                    log::info!("Received {} URL(s)", urls.len());

                    if crate::lightweight::is_lightweight_mode() {
                        if let Err(e) = crate::lightweight::exit_lightweight_mode(&app_handle) {
                            log::error!("退出轻量模式重建窗口失败: {e}");
                        }
                    }

                    for (i, url) in urls.iter().enumerate() {
                        let url_str = url.as_str();
                        log::debug!("  URL[{i}]: {}", url_for_log(url_str));

                        if handle_deeplink_url(&app_handle, url_str, true, "on_open_url") {
                            break; // Process only first ccswitch:// URL
                        }
                    }
                }
            });
            log::info!("✓ Deep-link URL handler registered");

            // 冷启动补取:Windows/Linux 上插件在自身初始化时就解析了启动参数并发出事件,
            // macOS 的 Opened 也可能早于这里,都赶在上面的回调注册之前,只能从 get_current 取。
            // 万一两边都处理了同一条链接也无妨:暂存区只留最新一条,前端处理完就清除。
            let mut startup_deeplink = false;
            match app.deep_link().get_current() {
                Ok(Some(urls)) => {
                    for url in urls {
                        if handle_deeplink_url(app.handle(), url.as_str(), true, "get_current") {
                            startup_deeplink = true;
                            break;
                        }
                    }
                }
                Ok(None) => {}
                Err(e) => log::warn!("读取启动时的深链接失败: {e}"),
            }

            // 构建托盘:Windows 左键打开主界面,右键默认弹出面板;macOS 左右键默认都弹出面板。
            // 设置里打开「原生托盘菜单」就挂原生菜单(`tray::apply_tray_style` 运行时切换)。
            // Linux(AppIndicator)不派发点击事件,只能出原生菜单,菜单里有一项打开面板。
            let native_menu = tray_panel::uses_native_menu();
            let mut tray_builder = TrayIconBuilder::with_id(tray::TRAY_ID)
                .on_tray_icon_event(tray::handle_tray_icon_event)
                .on_menu_event(|app, event| {
                    tray::handle_tray_menu_event(app, &event.id.0);
                })
                .show_menu_on_left_click(tray::native_menu_on_left_click(native_menu));
            // 鼠标悬停提示(macOS 不设,见 `tray::TRAY_TOOLTIP_ENABLED`)
            if tray::TRAY_TOOLTIP_ENABLED {
                tray_builder = tray_builder.tooltip(tray::DEFAULT_TRAY_TOOLTIP);
            }
            if native_menu {
                let menu = tray::create_tray_menu(app.handle(), &app_state)?;
                tray_builder = tray_builder.menu(&menu);
            }

            // 使用平台对应的托盘图标(macOS 使用模板图标适配深浅色);出问题时 tray.rs 换成带圆点的那张
            if let Some((icon, template)) = tray::base_tray_icon(app.handle()) {
                tray_builder = tray_builder.icon(icon).icon_as_template(template);
            }

            // Linux 托盘需要把图标写入磁盘。WSL 的 XDG_RUNTIME_DIR 可能指向
            // 不存在或不可写的目录,使用应用缓存目录避免因此中止启动。
            #[cfg(target_os = "linux")]
            {
                tray_builder =
                    tray_builder.temp_dir_path(app.path().app_cache_dir()?.join("tray-icon"));
            }

            let _tray = tray_builder.build(app)?;
            #[cfg(target_os = "windows")]
            tray_compat::install(app.handle());
            crate::services::auto_sync::start_worker::<
                crate::services::webdav_auto_sync::WebDavAutoSync,
            >(app_state.db.clone(), app.handle().clone());
            crate::services::auto_sync::start_worker::<crate::services::s3_auto_sync::S3AutoSync>(
                app_state.db.clone(),
                app.handle().clone(),
            );
            // 将同一个实例注入到全局状态,避免重复创建导致的不一致
            app.manage(app_state);
            app.manage(services::kimi_oauth::KimiOAuthManager::default());
            app.manage(commands::QoderAuthState::default());

            // 初始化 SkillService
            let skill_service = SkillService::new();
            app.manage(commands::skill::SkillServiceState(Arc::new(skill_service)));

            // 初始化 CopilotAuthManager
            {
                use crate::proxy::providers::copilot_auth::CopilotAuthManager;
                use commands::CopilotAuthState;
                use tokio::sync::RwLock;

                let app_config_dir = crate::config::get_app_config_dir();
                let copilot_auth_manager = CopilotAuthManager::new(app_config_dir);
                app.manage(CopilotAuthState(Arc::new(RwLock::new(copilot_auth_manager))));
                log::info!("✓ CopilotAuthManager initialized");
            }

            // 初始化 CodexOAuthManager (ChatGPT Plus/Pro 反代)
            {
                use commands::CodexOAuthState;

                let codex_oauth_manager =
                    app.state::<AppState>().codex_oauth_manager.clone();
                app.manage(CodexOAuthState(codex_oauth_manager));
                log::info!("✓ CodexOAuthManager initialized");
            }

            // 初始化 xAI OAuthManager (Grok API 反代)
            {
                use commands::XaiOAuthState;

                let xai_oauth_manager = app.state::<AppState>().xai_oauth_manager.clone();
                app.manage(XaiOAuthState(xai_oauth_manager));
                log::info!("✓ XaiOAuthManager initialized");
            }

            // Antigravity 官方账号(授权中心加号、切 agy 的全局登录)
            {
                use crate::services::agy_account::AgyAccountManager;
                let manager = AgyAccountManager::new(crate::config::get_app_config_dir());
                app.manage(commands::AgyAccountState(Arc::new(manager)));
            }

            // WorkBuddy 官方账号(授权中心收录、切换桌面端登录的快照)
            {
                use crate::services::workbuddy_account::WorkBuddyAccountManager;
                let manager = WorkBuddyAccountManager::new(crate::config::get_app_config_dir());
                app.manage(commands::WorkBuddyAccountState(Arc::new(manager)));
            }
            // CodeBuddy 官方账号(授权中心收录、切 CodeBuddy 的全局登录)
            {
                use crate::services::codebuddy_account::CodebuddyAccountManager;
                let manager = Arc::new(CodebuddyAccountManager::new(
                    crate::config::get_app_config_dir(),
                ));
                app.manage(commands::CodebuddyAccountState(manager.clone()));
                // 上次退出时终端登录还没登完:删掉 CC Switch 写的登出标记,恢复原来的登录
                tauri::async_runtime::spawn(async move {
                    match manager.recover_stale_marker().await {
                        Ok(true) => log::info!("[CodebuddyAccount] 已恢复上次未完成登录前的登录"),
                        Ok(false) => {}
                        Err(error) => log::warn!("[CodebuddyAccount] 恢复上次登录前的登出标记失败: {error}"),
                    }
                });
            }

            // Claude Code 官方订阅账号(授权中心加号;每个账号一个 CLAUDE_CONFIG_DIR)
            {
                use crate::services::claude_account::ClaudeAccountManager;
                let data_dir = crate::config::get_app_config_dir();
                let manager = ClaudeAccountManager::new(data_dir.clone());
                app.manage(commands::ClaudeAccountState(Arc::new(manager)));
                let db = app.state::<AppState>().db.clone();
                std::thread::spawn(move || {
                    // 上次切换登录时崩溃、断电:先补完(配置操作在前面已补完,指针已落定)
                    crate::services::provider::claude_official::recover_login_switch(&db);
                    // 默认目录的设置、MCP 可能在 CC Switch 没运行时改过
                    crate::services::claude_account::sync_all_shared(&data_dir)
                });
            }

            // 初始化全局出站代理 HTTP 客户端
            {
                let db = &app.state::<AppState>().db;
                let proxy_url = db.get_global_proxy_url().ok().flatten();

                if let Err(e) = crate::proxy::http_client::init(proxy_url.as_deref()) {
                    log::error!(
                        "[GlobalProxy] [GP-005] Failed to initialize with saved config: {e}"
                    );

                    // 清除无效的代理配置
                    if proxy_url.is_some() {
                        log::warn!(
                            "[GlobalProxy] [GP-006] Clearing invalid proxy config from database"
                        );
                        if let Err(clear_err) = db.set_global_proxy_url(None) {
                            log::error!(
                                "[GlobalProxy] [GP-007] Failed to clear invalid config: {clear_err}"
                            );
                        }
                    }

                    // 使用直连模式重新初始化
                    if let Err(fallback_err) = crate::proxy::http_client::init(None) {
                        log::error!(
                            "[GlobalProxy] [GP-008] Failed to initialize direct connection: {fallback_err}"
                        );
                    }
                }
            }

            // 自动更新:开关打开时后台定时检查并下载新版本(装不装由用户确认)
            crate::app_update::spawn_background_loop(app.handle().clone());

            // 异常退出恢复 + 代理状态自动恢复
            let app_handle = app.handle().clone();
            tauri::async_runtime::spawn(async move {
                let state = app_handle.state::<AppState>();

                // 必须排在 auto-extract 之前:先把历史泄漏进 Gemini 共享片段的凭据
                // 清干净,否则紧接着的提取会基于被污染的 live 再写一遍。
                if let Err(e) =
                    crate::services::provider::ProviderService::scrub_leaked_gemini_common_config(
                        &state,
                    )
                    .await
                {
                    log::warn!("清理 Gemini 通用配置泄漏凭据失败: {e}");
                }

                initialize_common_config_snippets(&state);

                // 定下各应用的直连 / 代理模式(处理旧版遗留的接管状态),再把代理模式的
                // 应用接上。要排在通用配置片段的自动提取之后:它读的是直连的 live。
                crate::mode::controller::startup(&state).await;
                // #8268 测试版给 agy 装过包装脚本、改过 shell 启动文件,撤掉带标记的部分。
                if let Err(e) =
                    crate::services::provider::agy_direct::migrate_legacy_launcher(&state.db)
                {
                    log::warn!("清理旧版 agy 启动器失败: {e}");
                }
                // 启动流程走完:托盘这时才开始报「路由服务没在运行」,并记下退回直连的应用。
                crate::tray::mark_startup_settled(&app_handle);
                // Codex 官方做路由、发布了 Stack 模型时,官方模型列表过期就在后台刷新。
                crate::services::provider::codex_official_models::start_background_checks(
                    state.inner().clone(),
                );

                // Backup and log maintenance can hold the database lock for a
                // long time. Keep disk work off both the UI and async workers.
                // The first interval tick runs startup maintenance; later ticks
                // run daily, without a catch-up burst after sleep.
                let db_for_timer = state.db.clone();
                tauri::async_runtime::spawn(async move {
                    const PERIODIC_MAINTENANCE_INTERVAL_SECS: u64 = 24 * 60 * 60;
                    let mut interval = tokio::time::interval(std::time::Duration::from_secs(
                        PERIODIC_MAINTENANCE_INTERVAL_SECS,
                    ));
                    interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
                    loop {
                        interval.tick().await;
                        let db = db_for_timer.clone();
                        match tauri::async_runtime::spawn_blocking(move || db.periodic_backup_if_needed()).await {
                            Ok(Ok(())) => {}
                            Ok(Err(e)) => log::warn!("Periodic maintenance failed: {e}"),
                            Err(e) => log::warn!("Periodic maintenance task failed: {e}"),
                        }
                    }
                });

                // Session log usage sync: 启动时同步一次,之后每 10 分钟兜底一次(防 CLI 清理旧会话
                // 文件前漏导入);用量页打开时由前端按所选间隔加扫
                let db_for_session_sync = state.db.clone();
                tauri::async_runtime::spawn(async move {
                    const SESSION_SYNC_INTERVAL_SECS: u64 = 10 * 60;

                    async fn run_session_sync(db: std::sync::Arc<crate::database::Database>, backfill: bool) {
                        // 手动扫描模式下跳过定时扫描;backfill 轮(启动首轮)仍进入,
                        // 费用回填只修补数据库既有行(含代理记账行),不读会话文件
                        if !backfill && !crate::settings::get_settings().session_auto_sync_enabled {
                            return;
                        }
                        let _guard = crate::services::session_usage::session_sync_mutex()
                            .lock()
                            .await;
                        let task = tauri::async_runtime::spawn_blocking(move || {
                            if backfill {
                                if let Err(error) = db.backfill_missing_usage_costs() {
                                    log::warn!("Usage cost startup backfill failed: {error}");
                                }
                            }
                            if !crate::settings::get_settings().session_auto_sync_enabled {
                                return crate::services::session_usage::SessionSyncResult::default();
                            }
                            crate::services::session_usage::sync_all_unlocked(&db)
                        });
                        match task.await {
                            Ok(result) if !result.errors.is_empty() => {
                                log::warn!(
                                    "Session usage sync completed with {} error(s)",
                                    result.errors.len()
                                );
                            }
                            Ok(_) => {}
                            Err(error) => log::warn!("Session usage blocking task failed: {error}"),
                        }
                    }

                    // 首次同步(含费用回填)
                    run_session_sync(db_for_session_sync.clone(), true).await;

                    // 定期同步
                    let mut interval = tokio::time::interval(std::time::Duration::from_secs(
                        SESSION_SYNC_INTERVAL_SECS,
                    ));
                    interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
                    interval.tick().await; // skip immediate first tick
                    loop {
                        interval.tick().await;
                        run_session_sync(db_for_session_sync.clone(), false).await;
                    }
                });
            });

            // 静默启动:根据设置决定是否显示主窗口
            let settings = crate::settings::get_settings();
            if let Some(window) = app.get_webview_window("main") {
                // 在窗口首次显示前同步装饰状态,避免前端加载后再切换导致标题栏闪烁
                // Linux:由设置决定(解决 Wayland 下系统窗口按钮不可用的问题)
                #[cfg(target_os = "linux")]
                let _ = window.set_decorations(!settings.use_app_window_controls);
                // Windows:一律去掉系统标题栏,用页头里的应用内窗口按钮;保留阴影和边缘缩放
                #[cfg(target_os = "windows")]
                {
                    let _ = window.set_decorations(false);
                    let _ = window.set_shadow(true);
                }
                // window-state 插件恢复的位置可能压在任务栏 / Dock 下面,首次显示前收回工作区。
                // 放在装饰设置之后,按最终的窗口外框计算。
                #[cfg(not(target_os = "linux"))]
                window_fit::fit_to_work_area(&window);
                // 上次退出时在轻量模式:照旧只留托盘。由深链接冷启动的要给用户看导入对话框,正常启动。
                if settings.lightweight_mode && !startup_deeplink {
                    if let Err(e) = crate::lightweight::enter_lightweight_mode(app.handle()) {
                        log::error!("启动时恢复轻量模式失败: {e}");
                    } else {
                        log::info!("上次退出时在轻量模式,启动后已恢复");
                    }
                } else if settings.silent_startup {
                    // 静默启动模式:保持窗口隐藏
                    if window.hide().is_ok() {
                        let _ = window.emit("main-window-visibility-changed", ());
                    }
                    #[cfg(target_os = "windows")]
                    let _ = window.set_skip_taskbar(true);
                    #[cfg(target_os = "macos")]
                    tray::apply_tray_policy(app.handle(), false);
                    log::info!("静默启动模式:主窗口已隐藏");
                } else {
                    // 正常启动模式:显示窗口
                    #[cfg(not(target_os = "windows"))]
                    if window.show().is_ok() {
                        let _ = window.emit("main-window-visibility-changed", ());
                    }
                    #[cfg(target_os = "windows")]
                    log::info!("正常启动模式:等待主页面加载完成后显示主窗口");
                    #[cfg(not(target_os = "windows"))]
                    log::info!("正常启动模式:主窗口已显示");

                    // Linux: 解决首次启动 UI 无响应问题(Tauri #10746 + wry #637)。
                    // 激活焦点并重新分配 WebView 的 GTK 布局。
                    #[cfg(target_os = "linux")]
                    {
                        linux_fix::nudge_main_window(window.clone(), "startup");
                    }
                }
            }


            Ok(())
        })
        .invoke_handler(tauri::generate_handler![
            commands::get_providers,
            commands::get_unreadable_providers,
            commands::get_current_provider,
            commands::add_provider,
            commands::update_provider,
            commands::get_provider_editor_view,
            commands::delete_provider,
            commands::add_provider_to_live_config,
            commands::remove_provider_from_live_config,
            commands::switch_provider,
            commands::import_default_config,
            commands::get_claude_desktop_status,
            commands::get_agy_desktop_status,
            commands::get_claude_desktop_default_routes,
            commands::import_claude_desktop_providers_from_claude,
            commands::ensure_claude_desktop_official_provider,
            commands::ensure_grokbuild_official_provider,
            commands::get_config_status,
            commands::get_config_dir,
            commands::open_config_folder,
            commands::pick_directory,
            commands::open_external,
            commands::get_init_error,
            commands::get_migration_result,
            commands::get_skills_migration_result,
            commands::open_app_config_folder,
            commands::reveal_app_log_file,
            commands::read_live_provider_settings,
            commands::get_settings,
            commands::save_settings,
            commands::has_codex_unify_history_backup,
            commands::codex_forces_multi_agent_v2,
            commands::get_codex_session_compression,
            commands::set_codex_session_compression,
            commands::get_codex_sessions_disk_usage,
            commands::restore_codex_unified_history,
            commands::get_rectifier_config,
            commands::set_rectifier_config,
            commands::get_optimizer_config,
            commands::set_optimizer_config,
            commands::get_privacy_filter_config,
            commands::set_privacy_filter_config,
            commands::get_log_config,
            commands::set_log_config,
            commands::restart_app,
            commands::install_update_and_restart,
            commands::get_auto_update_status,
            commands::download_app_update,
            commands::check_app_update_available,
            commands::check_for_updates,
            commands::open_portable_update_download,
            commands::is_portable_mode,
            commands::copy_text_to_clipboard,
            commands::list_installed_plugins,
            commands::list_plugin_catalog,
            commands::list_plugin_marketplaces,
            commands::install_plugin,
            commands::uninstall_plugin,
            commands::set_plugin_enabled,
            commands::update_plugin,
            commands::add_plugin_marketplace,
            commands::remove_plugin_marketplace,
            commands::refresh_plugin_marketplaces,
            commands::apply_claude_plugin_config,
            commands::apply_claude_onboarding_skip,
            commands::clear_claude_onboarding_skip,
            // Claude MCP management
            // usage query
            commands::queryProviderUsage,
            commands::testUsageScript,
            // subscription quota
            commands::get_subscription_quota,
            commands::get_codex_oauth_quota,
            commands::get_codex_oauth_models,
            commands::get_xai_oauth_models,
            commands::get_xai_oauth_quota,
            commands::get_coding_plan_quota,
            commands::get_balance,
            // New MCP via config.json (SSOT)
            // Unified MCP management
            commands::get_mcp_servers,
            commands::get_unreadable_mcp_servers,
            commands::start_mcp_connectivity_checks,
            commands::cancel_mcp_connectivity_checks,
            commands::upsert_mcp_server,
            commands::delete_mcp_server,
            commands::toggle_mcp_app,
            commands::import_mcp_from_apps,
            commands::resync_mcp_to_apps,
            // Prompt management
            commands::get_prompts,
            commands::upsert_prompt,
            commands::delete_prompt,
            commands::enable_prompt,
            commands::import_prompt_from_file,
            commands::get_current_prompt_file_content,
            commands::get_prompt_file_location,
            commands::get_pi_prompt_file,
            commands::replace_pi_prompt_file,
            commands::delete_pi_prompt_file,
            commands::list_pi_prompt_templates,
            commands::upsert_pi_prompt_template,
            commands::delete_pi_prompt_template,
            // Pi native provider and session views
            commands::get_pi_current_state,
            commands::set_qoder_default_model,
            commands::get_qoder_auth_status,
            commands::start_qoder_login,
            commands::poll_qoder_login,
            commands::cancel_qoder_login,
            commands::use_qoder_account,
            commands::delete_qoder_account,
            commands::import_current_qoder_account,
            commands::update_pi_provider_usage_script,
            commands::get_pi_session_discovery,
            // Oh My Pi native provider and session views
            commands::get_omp_current_state,
            // Profile management (项目配置方案)
            commands::list_profiles,
            commands::create_profile,
            commands::update_profile,
            commands::delete_profile,
            commands::clear_current_profile,
            commands::apply_profile,
            // Fetch OpenAI-compatible and Anthropic model lists. Response data structure:
            // data[].id, data[]?.owned_by. Special: supports Zhipu OpenAI Responses models[].slug.
            commands::fetch_models_for_config,
            commands::get_agy_models,
            commands::get_opencode_models,
            // ours: endpoint speed test + custom endpoint management
            commands::test_api_endpoints,
            commands::get_custom_endpoints,
            commands::add_custom_endpoint,
            commands::remove_custom_endpoint,
            // app_config_dir override via Store
            commands::get_app_config_dir_override,
            commands::set_app_config_dir_override,
            // provider sort order management
            commands::update_providers_sort_order,
            // theirs: config import/export and dialogs
            commands::export_config_to_file,
            commands::import_config_from_file,
            commands::webdav_test_connection,
            commands::webdav_sync_upload,
            commands::webdav_sync_download,
            commands::webdav_sync_save_settings,
            commands::webdav_sync_clear_settings,
            commands::webdav_sync_fetch_remote_info,
            commands::s3_test_connection,
            commands::s3_sync_upload,
            commands::s3_sync_download,
            commands::s3_sync_save_settings,
            commands::s3_sync_clear_settings,
            commands::s3_sync_fetch_remote_info,
            commands::save_file_dialog,
            commands::open_file_dialog,
            commands::open_zip_file_dialog,
            commands::create_db_backup,
            commands::list_db_backups,
            commands::list_backup_locations,
            commands::delete_backup_location,
            commands::reveal_backup_location,
            commands::restore_db_backup,
            commands::rename_db_backup,
            commands::delete_db_backup,
            commands::sync_current_providers_live,
            // Deep link import
            commands::merge_deeplink_config,
            commands::fetch_deeplink_icon,
            commands::import_from_deeplink_unified,
            commands::peek_pending_deeplink,
            commands::ack_pending_deeplink,
            update_tray_menu,
            // Environment variable management
            commands::check_env_conflicts,
            commands::delete_env_vars,
            // Skill management (v3.10.0+ unified)
            commands::get_installed_skills,
            commands::refresh_local_skill_metadata,
            commands::get_skill_backups,
            commands::delete_skill_backup,
            commands::install_skill_unified,
            commands::uninstall_skill_unified,
            commands::restore_skill_backup,
            commands::toggle_skill_app,
            commands::scan_unmanaged_skills,
            commands::import_skills_from_apps,
            commands::discover_available_skills,
            commands::discover_repo_skills,
            commands::check_skill_updates,
            commands::resync_skills_to_apps,
            commands::update_skill,
            commands::migrate_skill_storage,
            commands::get_cc_switch_skills_dir,
            commands::open_cc_switch_skills_dir,
            commands::open_skill_dir,
            commands::search_skills_sh,
            // Skill management (legacy API compatibility)
            commands::get_skill_repos,
            commands::add_skill_repo,
            commands::remove_skill_repo,
            commands::get_skill_repo_tokens,
            commands::set_skill_repo_token,
            commands::delete_skill_repo_token,
            commands::install_skills_from_zip,
            // Auto launch
            commands::set_auto_launch,
            // Proxy server management
            commands::start_proxy_server,
            commands::stop_proxy_server,
            commands::stop_proxy_with_restore,
            commands::get_proxy_takeover_status,
            commands::set_proxy_takeover_for_app,
            commands::get_app_mode,
            commands::set_proxy_route,
            commands::take_startup_attach_failures,
            tray::take_tray_navigation,
            tray_panel::tray_panel_state,
            tray_panel::tray_panel_placement,
            tray_panel::tray_panel_layout,
            tray_panel::tray_panel_hide,
            tray_panel::tray_panel_action,
            tray::tray_app_page_seen,
            commands::exit_proxy_apps_in_mode,
            commands::get_proxy_status,
            // Global & Per-App Config
            commands::get_global_proxy_config,
            commands::update_global_proxy_config,
            commands::get_proxy_client_host,
            commands::set_proxy_client_host,
            commands::get_proxy_config_for_app,
            commands::update_proxy_config_for_app,
            commands::get_pricing_model_source,
            commands::set_pricing_model_source,
            commands::get_proxy_stack,
            commands::set_proxy_stack_member,
            commands::adopt_codex_stack_catalog,
            commands::restart_codex_app_server_daemon,
            commands::acknowledge_codex_stale_clients,
            // Proxy failover commands
            commands::get_provider_health,
            // Failover queue management
            commands::get_failover_queue,
            commands::add_to_failover_queue,
            commands::remove_from_failover_queue,
            commands::get_auto_failover_enabled,
            commands::set_auto_failover_enabled,
            // Usage statistics
            commands::get_usage_summary,
            commands::get_session_usage_summary,
            commands::get_usage_summary_by_app,
            commands::get_usage_trends,
            commands::get_usage_first_date,
            commands::get_provider_stats,
            commands::get_model_stats,
            commands::get_request_logs,
            commands::get_request_detail,
            commands::get_model_pricing,
            commands::update_model_pricing,
            commands::update_model_pricing_batch,
            commands::delete_model_pricing,
            commands::get_models_dev_sync_config,
            commands::save_models_dev_sync_config,
            commands::record_models_dev_sync_result,
            commands::fetch_models_dev,
            // Session usage sync
            commands::sync_session_usage,
            commands::rebuild_codex_usage,
            commands::get_session_usage_last_sync,
            commands::fetch_exchange_rates,
            // Stream health check
            commands::stream_check_provider,
            // Session manager
            commands::list_sessions,
            commands::sync_session_index,
            commands::search_session_content,
            commands::cancel_session_content_search,
            commands::clear_session_index,
            commands::get_session_messages,
            commands::stream_session_messages,
            commands::get_session_block_content,
            commands::get_session_image,
            commands::reveal_session_path,
            commands::export_session_markdown,
            commands::delete_session,
            commands::delete_sessions,
            commands::launch_session_terminal,
            commands::get_tool_versions,
            commands::run_tool_lifecycle_action,
            commands::prepare_tool_uninstall,
            commands::probe_tool_installations,
            commands::list_tool_installations,
            // Provider terminal
            commands::open_provider_terminal,
            // Universal Provider management
            commands::get_universal_providers,
            commands::upsert_universal_provider,
            commands::delete_universal_provider,
            commands::sync_universal_provider,
            // Custom icon library
            commands::get_custom_icons,
            commands::add_custom_icon,
            commands::rename_custom_icon,
            commands::delete_custom_icon,
            // ZCode specific
            commands::import_zcode_providers_from_live,
            commands::get_zcode_live_provider_ids,
            // OpenCode specific
            commands::import_opencode_providers_from_live,
            commands::get_opencode_live_provider_ids,
            commands::get_opencode_default_model,
            commands::set_opencode_default_model,
            // OpenClaw specific
            commands::import_openclaw_providers_from_live,
            commands::get_openclaw_live_provider_ids,
            commands::get_openclaw_live_provider,
            commands::scan_openclaw_config_health,
            commands::get_openclaw_default_model,
            commands::set_openclaw_default_model,
            commands::get_openclaw_model_catalog,
            commands::set_openclaw_model_catalog,
            commands::get_openclaw_agents_defaults,
            commands::set_openclaw_agents_defaults,
            commands::get_openclaw_env,
            commands::set_openclaw_env,
            commands::get_openclaw_tools,
            commands::set_openclaw_tools,
            // Hermes specific
            commands::import_hermes_providers_from_live,
            commands::get_hermes_live_provider_ids,
            commands::get_hermes_model_config,
            commands::open_hermes_web_ui,
            commands::launch_hermes_dashboard,
            commands::get_hermes_memory,
            commands::set_hermes_memory,
            commands::get_hermes_memory_limits,
            commands::set_hermes_memory_enabled,
            // Global upstream proxy
            commands::get_global_proxy_url,
            commands::set_global_proxy_url,
            commands::test_proxy_url,
            commands::scan_local_proxies,
            commands::diagnose_ip,
            // Window theme control
            commands::set_window_theme,
            // Generic managed auth commands
            commands::auth_start_login,
            commands::auth_poll_for_account,
            commands::auth_cancel_login,
            commands::auth_get_status,
            commands::auth_remove_account,
            commands::auth_set_default_account,
            commands::auth_logout,
            commands::agy_account_quota,
            commands::agy_account_last_quota,
            commands::agy_account_import_current,
            commands::agy_account_open_login_terminal,
            commands::agy_account_open_desktop,
            commands::agy_account_submit_paste,
            commands::workbuddy_account_live_state,
            commands::workbuddy_model_owners,
            commands::workbuddy_account_import_current,
            commands::workbuddy_account_prepare_new_login,
            commands::workbuddy_account_open_app,
            commands::workbuddy_account_quota,
            commands::codebuddy_account_live,
            commands::codebuddy_account_import_current,
            commands::codebuddy_account_open_login_terminal,
            commands::codebuddy_account_quota,
            commands::codebuddy_account_switch_provider,
            commands::claude_account_submit_paste,
            commands::get_claude_account_quota,
            commands::get_claude_default_plan,
            commands::get_claude_login_switch,
            commands::kimi_oauth_status,
            commands::dsh_account_status,
            commands::dsh_account_sign_out,
            commands::dsh_account_login_start,
            commands::dsh_account_login_poll,
            commands::dsh_account_login_cancel,
            commands::kimi_oauth_start,
            commands::kimi_oauth_poll,
            commands::kimi_oauth_cancel,
            commands::kimi_oauth_remove,
            commands::kimi_oauth_quota,
            commands::kimi_oauth_use_account,
            commands::kimi_oauth_models,
            // Copilot OAuth commands (multi-account support)
            commands::copilot_get_models,
            commands::copilot_get_models_for_account,
            commands::copilot_get_usage,
            commands::copilot_get_usage_for_account,
            commands::copilot_cli_signed_in_account,
            // Copilot CLI model sync to VS Code
            commands::get_copilot_vscode_targets,
            commands::get_copilot_vscode_sync_status,
            commands::sync_copilot_vscode,
            // OMO commands
            commands::read_omo_local_file,
            commands::get_current_omo_provider_id,
            commands::disable_current_omo,
            commands::read_omo_slim_local_file,
            commands::get_current_omo_slim_provider_id,
            commands::disable_current_omo_slim,
            // Workspace files (OpenClaw)
            commands::read_workspace_file,
            commands::write_workspace_file,
            // Daily memory files (OpenClaw workspace)
            commands::list_daily_memory_files,
            commands::read_daily_memory_file,
            commands::write_daily_memory_file,
            commands::delete_daily_memory_file,
            commands::search_daily_memory_files,
            commands::open_workspace_directory,
        ]);

    let app = builder
        .build(tauri::generate_context!())
        .expect("error while running tauri application");

    app.run(|app_handle, event| {
        // 处理退出请求(所有平台)
        if let RunEvent::ExitRequested { api, code, .. } = &event {
            match classify_exit_request(*code) {
                // code 为 None 表示运行时自动触发(如隐藏窗口的 WebView 被回收导致无存活窗口),
                // 此时应仅阻止退出、保持托盘后台运行。
                ExitRequestAction::StayInTray => {
                    log::info!("运行时触发退出请求(无存活窗口),阻止退出以保持托盘后台运行");
                    api.prevent_exit();
                    return;
                }
                // code 为 RESTART_EXIT_CODE:app.restart() 发起的重启(本应用自己的重启
                // 都走 restart_process,不经过这里,此分支只兜底)。
                // 这条路径上 prevent_exit() 会被 Tauri 忽略,事件循环必定退出,随后由
                // Tauri 在 RunEvent::Exit 后用新二进制 re-exec(macOS 会按更新后的
                // Info.plist 解析可执行名)。
                //
                // 绝不能复用下面的异步清理任务:该任务在 tokio 线程调 save_window_state,
                // 持有 window-state 插件锁的同时向主线程查询窗口几何;而主线程此刻正在
                // 退出事件循环,并在插件自带的 RunEvent::Exit 钩子里等待同一把锁——双方
                // 互等造成进程永久卡死(更新已安装但应用冻结、不再重启,见 #3998)。
                //
                // 重启路径交还 Tauri 默认流程即可:
                //   - 窗口状态:插件 Exit 钩子在主线程保存(同线程读取窗口几何,无死锁)
                //   - 托盘图标:Tauri 内部 cleanup_before_exit 清理,正常走 Drop
                //   - 代理/Live 配置:无需恢复,重启后新实例立即接管并恢复代理状态
                //   - 100ms 落盘等待:重启前的 DB 写入均为命令驱动、此刻已完成,
                //     与所有 Tauri 应用默认重启路径的行为一致,无需额外等待
                ExitRequestAction::DeferToTauriRestart => {
                    log::info!("收到重启请求 (code={code:?}),交由 Tauri 默认重启流程 re-exec");
                    RESTART_REQUESTED.store(true, Ordering::SeqCst);
                    return;
                }
                // 其它 Some(_):用户主动调用 app.exit() 退出(如托盘菜单"退出"),
                // 此时执行清理后退出。
                ExitRequestAction::CleanupAndExit => {}
            }

            log::info!("收到用户主动退出请求 (code={code:?}),开始清理...");
            api.prevent_exit();

            let app_handle = app_handle.clone();
            tauri::async_runtime::spawn(async move {
                save_window_state_before_exit(&app_handle);
                cleanup_before_exit(&app_handle).await;
                // 先于 std::process::exit 显式移除托盘图标。
                // 进程直接退出时 Tauri 运行时不走正常 Drop 流程,
                // 不会向 Windows Shell 发送 NIM_DELETE,导致已退出的进程
                // 注册的图标仍残留在系统托盘(鼠标悬停 Shell 才会重绘发现进程已死)。
                // macOS 上不移除(会删掉菜单栏位置),见函数注释。
                remove_tray_icon_before_exit(&app_handle);
                log::info!("清理完成,退出应用");

                // 短暂等待确保所有 I/O 操作(如数据库写入)刷新到磁盘
                tokio::time::sleep(std::time::Duration::from_millis(100)).await;

                // 使用 std::process::exit 避免再次触发 ExitRequested
                std::process::exit(0);
            });
            return;
        }

        // macOS ⌘Q、Dock「退出」、注销关机走系统 terminate,不发 ExitRequested、只发
        // RunEvent::Exit,回调一返回进程就结束,只能在这里同步补做退出清理。重启也会走到
        // 这里,照上面 DeferToTauriRestart 的约定交还 Tauri 默认流程,不清理。
        if matches!(event, RunEvent::Exit) {
            if !RESTART_REQUESTED.load(Ordering::SeqCst) {
                cleanup_before_system_exit(app_handle);
            } else {
                checkpoint_before_restart(app_handle);
            }
            return;
        }

        #[cfg(target_os = "macos")]
        {
            // macOS 在 Dock 图标被点击并重新激活应用时会触发 Reopen 事件,这里手动恢复主窗口。
            // RunEvent::Opened(ccswitch:// 链接)不在这里处理:deep-link 插件的 on_event 已把它
            // 转成 on_open_url 回调,这里再处理一遍会让同一条链接触发两次导入。
            if let RunEvent::Reopen { .. } = event {
                tray::show_main_window(app_handle);
            }
        }

        #[cfg(not(target_os = "macos"))]
        {
            let _ = (app_handle, event);
        }
    });
}

// ============================================================
// 应用退出清理
// ============================================================

/// 应用退出前的清理工作
///
/// 把接上代理的客户端都指回直连(模式和代理路由保留,下次启动再接上),再停止代理。
/// 客户端不能一直指着代理:开机自启默认关闭,CC Switch 一关客户端就连不上了。
pub async fn cleanup_before_exit(app_handle: &tauri::AppHandle) {
    // 「添加账号」登到一半就退出:写回被临时清空的 agy 登录
    if let Some(agy) = app_handle.try_state::<commands::AgyAccountState>() {
        agy.0.abandon_pending_logins().await;
    }
    if let Some(state) = app_handle.try_state::<store::AppState>() {
        crate::mode::controller::detach_all(state.inner()).await;
        // 切换后几秒内退出:给开着的 Claude Code 会话写的空值墓碑现在删掉(刚写的最多等 3 秒),
        // 不留到下次启动
        let _ = tokio::task::spawn_blocking(
            crate::services::provider::claude_direct::finish_tombstones_before_exit,
        )
        .await;
        if let Err(error) = state.db.run_blocking(|db| db.checkpoint_wal()).await {
            log::warn!("退出时 WAL checkpoint 失败: {error}");
        }
        log::info!("退出清理完成:客户端已指回直连,代理已停止");
    }
}

/// 系统终止应用时最多等退出清理这么久:停代理服务自带 5 秒超时,指回直连只是写几个文件。
///
/// 删 Claude 空值墓碑前最多等 3 秒(`claude_direct::TOMBSTONE_MIN_VISIBLE`),但这 3 秒从
/// 切换写入墓碑那一刻算,指回直连、停代理花掉的时间也算在里面,所以不会再叠加 3 秒:最坏
/// 是两者取大(5 秒)再加写几个文件。只有真写了墓碑、而且是切换后 3 秒内退出才会等。
/// 超时也不弄坏文件:空值留着(Claude Code 读到等于没设),下次启动接着删。
const SYSTEM_EXIT_CLEANUP_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(8);

/// 系统直接终止应用时(macOS ⌘Q、Dock「退出」、注销关机)的退出清理。
///
/// 这条路没有 `ExitRequested` 可以 `prevent_exit()` 再异步清理,只能在主线程上等清理做完。
/// 清理放到异步运行时的线程上跑、主线程限时等:万一里面有步骤要等主线程,超时后照常退出,
/// 不会把进程卡住。
fn cleanup_before_system_exit(app_handle: &tauri::AppHandle) {
    log::info!("系统终止应用,开始退出清理...");
    let handle = app_handle.clone();
    let task = tauri::async_runtime::spawn(async move { cleanup_before_exit(&handle).await });
    // timeout 要在运行时里构造(它取当前运行时的计时器),所以包一层 async。
    let finished = tauri::async_runtime::block_on(async move {
        tokio::time::timeout(SYSTEM_EXIT_CLEANUP_TIMEOUT, task).await
    });
    if finished.is_err() {
        log::warn!(
            "退出清理 {} 秒内没做完,直接退出",
            SYSTEM_EXIT_CLEANUP_TIMEOUT.as_secs()
        );
    }
}

/// 主动从系统托盘移除托盘图标。
///
/// `std::process::exit` 会绕过 Tauri 运行时,触发不了 `TrayIcon::drop()`,
/// 也就不会向 Windows Shell 发 `NIM_DELETE`。结果是进程退出后托盘里
/// 仍保留一个死图标的缓存占位(Shell 不会主动重绘,需要鼠标悬停才刷新)。
///
/// 通过 `set_visible(false)` 走 `WM_USER_HIDE_TRAYICON` 消息路径,
/// 触发 tray-icon 内部的 `remove_tray_icon` → `Shell_NotifyIconW(NIM_DELETE)`,
/// 在进程结束前干净地把图标摘掉。Linux 上 `set_visible(false)` 也是
/// 正常的隐藏/移除语义,作为兜底也安全。
///
/// macOS 上什么都不做:`set_visible(false)` 在那里是 `removeStatusItem`,系统会连带删掉
/// 用户⌘拖好的菜单栏位置(应用 defaults 里的 `NSStatusItem Preferred Position Item-0`),
/// 下次启动图标回到默认位置,Hidden Bar 这类整理工具的分组也跟着失效。不移除直接结束进程,
/// 系统随进程一起收掉图标,位置留着(macOS 27 实测)。⌘Q 那条路本来就不丢位置。
pub(crate) fn remove_tray_icon_before_exit(app_handle: &tauri::AppHandle) {
    #[cfg(target_os = "macos")]
    {
        let _ = app_handle;
    }
    #[cfg(not(target_os = "macos"))]
    if let Some(tray) = app_handle.tray_by_id(tray::TRAY_ID) {
        if let Err(e) = tray.set_visible(false) {
            log::warn!("退出时移除托盘图标失败: {e}");
        } else {
            log::info!("已显式从系统托盘移除图标");
        }
    }
}

fn initialize_common_config_snippets(state: &store::AppState) {
    // Auto-extract common config snippets from clean live files when snippet is missing.
    // This must run before proxy mode is re-attached on startup, otherwise we'd read
    // proxy-placeholder configs instead of the user's actual live settings. A client
    // still attached from an update restart (no detach on the way out) is skipped too.
    for app_type in crate::app_config::AppType::all() {
        if !state
            .db
            .should_auto_extract_config_snippet(&app_type)
            .unwrap_or(false)
        {
            continue;
        }
        if state.proxy_service.live_has_proxy_placeholder(&app_type) {
            continue;
        }

        let settings = match crate::services::provider::ProviderService::read_live_settings(
            app_type.clone(),
        ) {
            Ok(s) => s,
            Err(_) => continue,
        };

        match crate::services::provider::ProviderService::extract_common_config_snippet_from_settings(
            app_type.clone(),
            &settings,
        ) {
            Ok(snippet) if !snippet.is_empty() && snippet != "{}" => {
                match state.db.set_config_snippet(&app_type, Some(snippet)) {
                    Ok(()) => {
                        let _ = state.db.set_config_snippet_cleared(&app_type, false);
                        log::info!(
                            "✓ Auto-extracted common config snippet for {}",
                            app_type.as_str()
                        );
                    }
                    Err(e) => log::warn!(
                        "✗ Failed to save config snippet for {}: {e}",
                        app_type.as_str()
                    ),
                }
            }
            Ok(_) => log::debug!(
                "○ Live config for {} has no extractable common fields",
                app_type.as_str()
            ),
            Err(e) => log::warn!(
                "✗ Failed to extract config snippet for {}: {e}",
                app_type.as_str()
            ),
        }
    }

    let should_run_legacy_migration = state
        .db
        .is_legacy_common_config_migrated()
        .map(|done| !done)
        .unwrap_or(true);

    if should_run_legacy_migration {
        for app_type in [
            crate::app_config::AppType::Claude,
            crate::app_config::AppType::Codex,
            crate::app_config::AppType::Gemini,
        ] {
            if let Err(e) = crate::services::provider::ProviderService::migrate_legacy_common_config_usage_if_needed(
                state,
                app_type.clone(),
            ) {
                log::warn!(
                    "✗ Failed to migrate legacy common-config usage for {}: {e}",
                    app_type.as_str()
                );
            }
        }

        if let Err(e) = state.db.set_legacy_common_config_migrated(true) {
            log::warn!("✗ Failed to persist legacy common-config migration flag: {e}");
        }
    }
}

// ============================================================
// 迁移错误对话框辅助函数
// ============================================================

/// 检测是否为中文环境
fn is_chinese_locale() -> bool {
    std::env::var("LANG")
        .or_else(|_| std::env::var("LC_ALL"))
        .or_else(|_| std::env::var("LC_MESSAGES"))
        .map(|lang| {
            lang.split(['-', '_', '.', '@'])
                .next()
                .is_some_and(|language| language.eq_ignore_ascii_case("zh"))
        })
        .unwrap_or(false)
}

/// 显示迁移错误对话框
/// 返回 true 表示用户选择重试,false 表示用户选择退出
fn show_migration_error_dialog(app: &tauri::AppHandle, error: &str) -> bool {
    let title = if is_chinese_locale() {
        "配置迁移失败"
    } else {
        "Migration Failed"
    };

    let message = if is_chinese_locale() {
        format!(
            "从旧版本迁移配置时发生错误:\n\n{error}\n\n\
            您的数据尚未丢失,旧配置文件仍然保留。\n\
            建议回退到旧版本 CC Switch 以保护数据。\n\n\
            点击「重试」重新尝试迁移\n\
            点击「退出」关闭程序(可回退版本后重新打开)"
        )
    } else {
        format!(
            "An error occurred while migrating configuration:\n\n{error}\n\n\
            Your data is NOT lost - the old config file is still preserved.\n\
            Consider rolling back to an older CC Switch version.\n\n\
            Click 'Retry' to attempt migration again\n\
            Click 'Exit' to close the program"
        )
    };

    let retry_text = if is_chinese_locale() {
        "重试"
    } else {
        "Retry"
    };
    let exit_text = if is_chinese_locale() {
        "退出"
    } else {
        "Exit"
    };

    // 使用 blocking_show 同步等待用户响应
    // OkCancelCustom: 第一个按钮(重试)返回 true,第二个按钮(退出)返回 false
    app.dialog()
        .message(&message)
        .title(title)
        .kind(MessageDialogKind::Error)
        .buttons(MessageDialogButtons::OkCancelCustom(
            retry_text.to_string(),
            exit_text.to_string(),
        ))
        .blocking_show()
}

/// 显示数据库初始化/Schema 迁移失败对话框
/// 返回 true 表示用户选择重试,false 表示用户选择退出
fn show_database_init_error_dialog(
    app: &tauri::AppHandle,
    db_path: &std::path::Path,
    error: &str,
) -> bool {
    let title = if is_chinese_locale() {
        "数据库初始化失败"
    } else {
        "Database Initialization Failed"
    };

    let message = if is_chinese_locale() {
        format!(
            "初始化数据库或迁移数据库结构时发生错误:\n\n{error}\n\n\
            数据库文件路径:\n{db}\n\n\
            您的数据尚未丢失,应用不会自动删除数据库文件。\n\
            常见原因包括:数据库版本过新、文件损坏、权限不足、磁盘空间不足等。\n\n\
            建议:\n\
            1) 先备份整个配置目录(包含 cc-switch.db)\n\
            2) 如果提示“数据库版本过新”,请升级到更新版本\n\
            3) 如果刚升级出现异常,可回退旧版本导出/备份后再升级\n\n\
            点击「重试」重新尝试初始化\n\
            点击「退出」关闭程序",
            db = db_path.display()
        )
    } else {
        format!(
            "An error occurred while initializing or migrating the database:\n\n{error}\n\n\
            Database file path:\n{db}\n\n\
            Your data is NOT lost - the app will not delete the database automatically.\n\
            Common causes include: newer database version, corrupted file, permission issues, or low disk space.\n\n\
            Suggestions:\n\
            1) Back up the entire config directory (including cc-switch.db)\n\
            2) If you see “database version is newer”, please upgrade CC Switch\n\
            3) If this happened right after upgrading, consider rolling back to export/backup then upgrade again\n\n\
            Click 'Retry' to attempt initialization again\n\
            Click 'Exit' to close the program",
            db = db_path.display()
        )
    };

    let retry_text = if is_chinese_locale() {
        "重试"
    } else {
        "Retry"
    };
    let exit_text = if is_chinese_locale() {
        "退出"
    } else {
        "Exit"
    };

    app.dialog()
        .message(&message)
        .title(title)
        .kind(MessageDialogKind::Error)
        .buttons(MessageDialogButtons::OkCancelCustom(
            retry_text.to_string(),
            exit_text.to_string(),
        ))
        .blocking_show()
}

// ============================================================
// 退出请求分类
// ============================================================

/// `RunEvent::ExitRequested` 的三类来源,处理方式必须区分。
///
/// 关键约束:重启请求(`code == RESTART_EXIT_CODE`)上 `prevent_exit()` 会被
/// Tauri 静默忽略(见 `ExitRequestApi::prevent_exit` 文档),事件循环必定继续
/// 退出并触发各插件的 `RunEvent::Exit` 钩子;任何与之并发的自定义清理任务都
/// 可能与插件退出钩子争用同一状态而死锁。
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ExitRequestAction {
    /// `code` 为 `None`:运行时自动触发(如隐藏窗口的 WebView 被回收导致无存活
    /// 窗口),阻止退出、保持托盘后台运行。
    StayInTray,
    /// `code` 为 `RESTART_EXIT_CODE`:`app.restart()` 发起的重启(本应用自己的
    /// 重启都走 `restart_process`,这里只兜底),不拦截、不做自定义清理,交还
    /// Tauri 默认 re-exec 流程。
    DeferToTauriRestart,
    /// 其它 `Some(_)`:用户主动退出(托盘「退出」等),执行完整异步清理后结束进程。
    CleanupAndExit,
}

/// 收到过重启请求。重启时 Tauri 也会发 `RunEvent::Exit`,靠它跳过系统终止那条清理。
static RESTART_REQUESTED: AtomicBool = AtomicBool::new(false);

fn classify_exit_request(code: Option<i32>) -> ExitRequestAction {
    match code {
        None => ExitRequestAction::StayInTray,
        Some(tauri::RESTART_EXIT_CODE) => ExitRequestAction::DeferToTauriRestart,
        Some(_) => ExitRequestAction::CleanupAndExit,
    }
}

// ============================================================
// 在应用主动退出前显式持久化窗口状态
// ============================================================

fn window_state_flags() -> StateFlags {
    let flags = StateFlags::POSITION | StateFlags::MAXIMIZED;
    #[cfg(not(target_os = "linux"))]
    let flags = flags | StateFlags::SIZE;
    flags
}

/// 当前应用的退出路径会拦截 `ExitRequested` 并最终直接 `std::process::exit(0)`,
/// 这里需要在真正结束进程前手动落盘,避免 window-state 插件的默认退出钩子被绕过。
pub fn save_window_state_before_exit(app_handle: &tauri::AppHandle) {
    #[cfg(target_os = "linux")]
    linux_window_geometry::save(app_handle);
    if let Err(err) = app_handle.save_window_state(window_state_flags()) {
        log::error!("退出前保存窗口状态失败: {err}");
    } else {
        log::info!("已在退出前保存窗口状态");
    }
}

/// 主动释放 single-instance 锁。
///
/// macOS single-instance 使用 `/tmp/{identifier}.sock`。我们有若干路径会直接
/// `std::process::exit(0)`,不会触发插件挂在 `RunEvent::Exit` 上的清理钩子。
/// 重启前主动 destroy 可以避免新进程误连旧 listener 后自行退出。
pub fn destroy_single_instance_lock(app_handle: &tauri::AppHandle) {
    #[cfg(any(target_os = "macos", target_os = "windows", target_os = "linux"))]
    tauri_plugin_single_instance::destroy(app_handle);
}

fn checkpoint_before_restart(app_handle: &tauri::AppHandle) {
    if let Some(state) = app_handle.try_state::<store::AppState>() {
        if let Err(error) = state.db.checkpoint_wal() {
            log::warn!("重启时 WAL checkpoint 失败: {error}");
        }
    }
}

/// 清理托盘图标、释放 single-instance 锁后重启当前应用。
///
/// 直接 spawn 新进程 + `exit(0)`(macOS 经脱离进程组的等待脚本在旧进程退出后再调
/// `open -n`,见 `relaunch_macos_bundle`),不经过事件循环退出,因此 Tauri 内部的
/// `cleanup_before_exit` 和各插件的 `RunEvent::Exit` 钩子都不会执行。需要的清理由
/// 调用方与本函数显式补偿:窗口状态、代理/Live 恢复(调用方);托盘图标、
/// single-instance 锁(本函数)。
///
/// 有意不调 `AppHandle::cleanup_before_exit()`:它会在调用线程上 Drop 托盘
/// 图标,而 macOS 的 NSStatusItem 操作要求主线程;`set_visible(false)` 走
/// `run_item_main_thread` 代理,跨线程安全(见 `remove_tray_icon_before_exit`,
/// macOS 上它不移除图标,免得重启后菜单栏位置丢失)。
pub fn restart_process(app_handle: &tauri::AppHandle) -> ! {
    checkpoint_before_restart(app_handle);
    remove_tray_icon_before_exit(app_handle);
    destroy_single_instance_lock(app_handle);
    let env = app_handle.env();
    #[cfg(target_os = "macos")]
    relaunch_macos_bundle(&env);
    tauri::process::restart(&env);
}

#[cfg(target_os = "macos")]
const MACOS_WAIT_THEN_RELAUNCH_SCRIPT: &str = r#"pid=$1; opener=$2; bundle=$3; fallback=$4; shift 4
i=0
while kill -0 "$pid" 2>/dev/null && [ "$i" -lt 100 ]; do
    /bin/sleep 0.1
    i=$((i + 1))
done
/bin/sleep 0.1
if [ "$#" -gt 0 ]; then
    "$opener" -n "$bundle" --args "$@" || exec "$fallback" "$@"
else
    "$opener" -n "$bundle" || exec "$fallback"
fi"#;

/// 从可执行文件路径定位其所属的 `.app` bundle(`<Name>.app/Contents/MacOS/<binary>`)。
#[cfg(target_os = "macos")]
fn macos_app_bundle_from_binary(binary: &std::path::Path) -> Option<std::path::PathBuf> {
    let macos_dir = binary.parent()?;
    let contents_dir = macos_dir.parent()?;
    if macos_dir.file_name()? != "MacOS" || contents_dir.file_name()? != "Contents" {
        return None;
    }
    let bundle = contents_dir.parent()?;
    bundle
        .extension()
        .is_some_and(|ext| ext == "app")
        .then(|| bundle.to_path_buf())
}

/// 构造脱离当前进程组、stdio 重定向到 `/dev/null` 的 `/bin/sh` 重启命令:
/// 等待 `pid` 退出后再执行 `opener -n <bundle>`(失败时回落直接执行 `fallback_binary`)。
#[cfg(target_os = "macos")]
fn build_macos_relaunch_command(
    pid: u32,
    opener: &std::path::Path,
    bundle: &std::path::Path,
    fallback_binary: &std::path::Path,
    args: &[std::ffi::OsString],
) -> std::process::Command {
    use std::os::unix::process::CommandExt;
    use std::process::{Command, Stdio};

    let mut command = Command::new("/bin/sh");
    command
        .arg("-c")
        .arg(MACOS_WAIT_THEN_RELAUNCH_SCRIPT)
        .arg("cc-switch-relaunch")
        .arg(pid.to_string())
        .arg(opener)
        .arg(bundle)
        .arg(fallback_binary)
        .args(args)
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .process_group(0);
    command
}

/// macOS 等当前进程完全退出后再经 LaunchServices(`open -n`)启动新实例。
///
/// 1. `tauri::process::restart` 直接 spawn 可执行文件。macOS 14 起应用激活是协作式的:
///    新进程的 `activateIgnoringOtherApps` 会被系统拒绝,窗口留在其它应用后面,
///    必须经 LaunchServices 启动才能拿到前台。
/// 2. 不能在当前进程仍存活时同步执行 `open -n`:此时旧进程仍占着 Dock 固定图标,
///    新实例(尤其是更新器原地替换 `.app` bundle 之后)会被 Dock 分到第二个临时图标,
///    旧进程退出后固定图标熄灭、运行中的新实例滞留在右侧(#8329)。
///
/// 先拉起一个脱离进程组的等待脚本,拉起成功后当前进程立即 `exit(0)`,待旧 PID 退出后再由
/// 脚本调 `/usr/bin/open -n <bundle>`(失败时回落直接执行二进制);仅当脚本本身拉起失败时
/// 才回落到 `tauri::process::restart`。
#[cfg(target_os = "macos")]
fn relaunch_macos_bundle(env: &tauri::Env) {
    let Ok(binary) = tauri::process::current_binary(env) else {
        return;
    };
    let Some(bundle) = macos_app_bundle_from_binary(&binary) else {
        return;
    };
    let args: Vec<_> = env.args_os.iter().skip(1).cloned().collect();
    let mut command = build_macos_relaunch_command(
        std::process::id(),
        std::path::Path::new("/usr/bin/open"),
        &bundle,
        &binary,
        &args,
    );
    match command.spawn() {
        Ok(_) => std::process::exit(0),
        Err(err) => log::warn!("启动 macOS 重启等待进程失败({err}),回落直接启动"),
    }
}

#[cfg(test)]
mod tests {
    use super::{
        classify_exit_request, error_for_log, redact_url_for_log, redact_url_for_log_with_secrets,
        redact_url_origin_for_log, runtime_log_level_allows, ExitRequestAction,
    };

    #[cfg(unix)]
    #[test]
    fn raise_open_file_limit_lifts_soft_limit_to_target_or_hard_limit() {
        fn current() -> libc::rlimit {
            let mut limit = libc::rlimit {
                rlim_cur: 0,
                rlim_max: 0,
            };
            assert_eq!(
                unsafe { libc::getrlimit(libc::RLIMIT_NOFILE, &mut limit) },
                0
            );
            limit
        }

        let before = current();
        super::raise_open_file_limit();
        let after = current();
        let expected = super::OPEN_FILE_SOFT_LIMIT_TARGET.min(after.rlim_max);
        assert!(after.rlim_cur >= expected);
        assert!(after.rlim_cur >= before.rlim_cur);
        assert_eq!(after.rlim_max, before.rlim_max);
    }

    #[test]
    fn log_error_drops_toml_source_lines_but_keeps_position() {
        let secret = "sk-review-only-secret";
        let toml_edit_error = format!("experimental_bearer_token = \"{secret}\" !\n")
            .parse::<toml_edit::DocumentMut>()
            .unwrap_err()
            .to_string();
        let toml_error = toml::from_str::<toml::Table>(&format!("token = \"{secret}\" !\n"))
            .unwrap_err()
            .to_string();
        for error in [toml_edit_error, toml_error] {
            assert!(error.contains(secret), "前提:诊断里带源码行");
            let logged = error_for_log(&format!("无法解析:{error} (cannot parse: {error})"));
            assert!(!logged.contains(secret), "{logged}");
            assert!(logged.contains("line 1"), "{logged}");
        }
        // 普通错误原样保留。
        assert_eq!(error_for_log("供应商 a 不存在"), "供应商 a 不存在");
    }

    #[test]
    fn log_url_redaction_strips_credentials_and_query_keeps_path() {
        // userinfo 与整个 query 剥离,path 保留用于诊断 base_url 配错。
        assert_eq!(
            redact_url_for_log(
                "https://user:[email protected]:8443/v1/models?key=top-secret&alt=sse"
            ),
            "https://example.com:8443/v1/models"
        );
        // scheme-relative 保持形态,userinfo 去掉。
        assert_eq!(
            redact_url_for_log("//user:[email protected]/v1"),
            "//gw.example.com/v1"
        );
        // 无 scheme 的裸 userinfo。
        assert_eq!(
            redact_url_for_log("user:[email protected]/v1"),
            "gw.example.com/v1"
        );
        // 无法解析为绝对 URL 时:丢 query,其余原样保留。
        assert_eq!(redact_url_for_log("not-a-url?token=secret"), "not-a-url");
        // 不再对 path 段做“看起来像密钥”的形状猜测,正常路径完整保留。
        assert_eq!(
            redact_url_for_log("https://host.example/v1/models/gemini-2.5-pro"),
            "https://host.example/v1/models/gemini-2.5-pro"
        );
    }

    #[test]
    fn log_url_redaction_replaces_known_secret_values() {
        // 精确匹配已知密钥值:无论它出现在 path 还是别处都被抹掉。
        let secrets = vec!["k-9f3a7c2b1e".to_string()];
        assert_eq!(
            redact_url_for_log_with_secrets("https://gw.example.com/k-9f3a7c2b1e/v1", &secrets),
            "https://gw.example.com/[REDACTED]/v1"
        );
        // 过短(<8)的已知值不参与子串脱敏,避免误伤 /v1/ 之类的正常路径。
        let short_secrets = vec!["api".to_string()];
        assert_eq!(
            redact_url_for_log_with_secrets("https://api.example.com/v1", &short_secrets),
            "https://api.example.com/v1"
        );
    }

    #[test]
    fn log_url_origin_drops_path_for_credential_in_path() {
        // 没有已知密钥可脱敏时,凭据可能整个内嵌在 path,只记 origin。
        assert_eq!(
            redact_url_origin_for_log("https://gw.example.com/k-9f3a7c2b1e/v1"),
            "https://gw.example.com"
        );
        assert_eq!(
            redact_url_origin_for_log("https://user:[email protected]:8443/secret/v1"),
            "https://gw.example.com:8443"
        );
        assert_eq!(
            redact_url_origin_for_log("//gw.example.com/secret/v1"),
            "//gw.example.com"
        );
    }

    #[test]
    fn runtime_log_filter_honors_dynamic_max_level() {
        assert!(!runtime_log_level_allows(
            log::Level::Error,
            log::LevelFilter::Off
        ));
        assert!(runtime_log_level_allows(
            log::Level::Error,
            log::LevelFilter::Info
        ));
        assert!(runtime_log_level_allows(
            log::Level::Info,
            log::LevelFilter::Info
        ));
        assert!(!runtime_log_level_allows(
            log::Level::Debug,
            log::LevelFilter::Info
        ));
    }

    #[test]
    fn no_code_keeps_app_alive_in_tray() {
        assert_eq!(classify_exit_request(None), ExitRequestAction::StayInTray);
    }

    #[test]
    fn restart_exit_code_defers_to_tauri_default_restart() {
        assert_eq!(
            classify_exit_request(Some(tauri::RESTART_EXIT_CODE)),
            ExitRequestAction::DeferToTauriRestart
        );
    }

    #[test]
    fn user_exit_codes_run_cleanup_then_exit() {
        assert_eq!(
            classify_exit_request(Some(0)),
            ExitRequestAction::CleanupAndExit
        );
        assert_eq!(
            classify_exit_request(Some(1)),
            ExitRequestAction::CleanupAndExit
        );
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn macos_app_bundle_from_binary_requires_app_bundle_layout() {
        use super::macos_app_bundle_from_binary;
        use std::path::{Path, PathBuf};

        assert_eq!(
            macos_app_bundle_from_binary(Path::new(
                "/Applications/CC Switch.app/Contents/MacOS/cc-switch"
            )),
            Some(PathBuf::from("/Applications/CC Switch.app"))
        );
        assert_eq!(
            macos_app_bundle_from_binary(Path::new("/repo/src-tauri/target/debug/cc-switch")),
            None
        );
        assert_eq!(
            macos_app_bundle_from_binary(Path::new("/Users/alice/demo.app/target/debug/cc-switch")),
            None
        );
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn macos_relaunch_waits_for_pid_exit_and_preserves_args() {
        use super::build_macos_relaunch_command;
        use std::ffi::OsString;
        use std::os::unix::fs::PermissionsExt;
        use std::path::Path;
        use std::process::Command;
        use std::time::Duration;

        let dir = tempfile::tempdir().unwrap();
        let record = dir.path().join("record");
        let opener = dir.path().join("opener");
        std::fs::write(
            &opener,
            format!(
                "#!/bin/sh\nif kill -0 \"$OLD_PID\" 2>/dev/null; then state=running; else state=exited; fi\nprintf '%s\\n' \"$state\" \"$@\" > '{}'\n",
                record.display()
            ),
        )
        .unwrap();
        std::fs::set_permissions(&opener, std::fs::Permissions::from_mode(0o755)).unwrap();

        let mut old = Command::new("/bin/sleep").arg("30").spawn().unwrap();
        let old_pid = old.id();
        let args = [OsString::from("--flag"), OsString::from("two words")];

        let mut waiter = build_macos_relaunch_command(
            old_pid,
            &opener,
            Path::new("/Applications/CC Switch.app"),
            Path::new("/usr/bin/false"),
            &args,
        )
        .env("OLD_PID", old_pid.to_string())
        .env("PATH", "")
        .spawn()
        .unwrap();

        std::thread::sleep(Duration::from_millis(250));
        assert!(waiter.try_wait().unwrap().is_none() && !record.exists());

        old.kill().unwrap();
        old.wait().unwrap();
        assert!(waiter.wait().unwrap().success());
        assert_eq!(
            std::fs::read_to_string(&record).unwrap(),
            "exited\n-n\n/Applications/CC Switch.app\n--args\n--flag\ntwo words\n"
        );
    }
}