diff --git a/src-tauri/src/utils/resolve/mod.rs b/src-tauri/src/utils/resolve/mod.rs index 1bee81fd..60f73cd4 100644 --- a/src-tauri/src/utils/resolve/mod.rs +++ b/src-tauri/src/utils/resolve/mod.rs @@ -62,7 +62,12 @@ pub fn resolve_setup_async() { init_once_auto_lightweight().await; init_auto_lightweight_mode().await; + // 确保配置完全初始化后再启动核心管理器 init_verge_config().await; + + // 添加配置验证,确保运行时配置已正确生成 + verify_config_initialization().await; + init_core_manager().await; init_system_proxy().await; @@ -190,6 +195,81 @@ pub(super) async fn init_auto_lightweight_mode() { logging_error!(Type::Setup, true, auto_lightweight_mode_init().await); } +/// 验证配置初始化是否成功 +async fn verify_config_initialization() { + logging!( + info, + Type::Setup, + true, + "Verifying config initialization..." + ); + + // 检查运行时配置是否已正确生成 + if Config::runtime().await.latest_ref().config.is_some() { + logging!( + info, + Type::Setup, + true, + "Config initialization verified successfully" + ); + return; + } + + logging!( + warn, + Type::Setup, + true, + "Runtime config not found, regenerating..." + ); + + // 尝试重新生成配置,最多3次 + for attempt in 1..=3 { + logging!( + info, + Type::Setup, + true, + "Attempt {}/3 to regenerate config...", + attempt + ); + + match Config::generate().await { + Ok(_) => { + logging!( + info, + Type::Setup, + true, + "Config successfully regenerated on attempt {}", + attempt + ); + return; + } + Err(e) => { + logging!( + warn, + Type::Setup, + true, + "Failed to generate config on attempt {}: {}", + attempt, + e + ); + + if attempt == 3 { + logging!( + error, + Type::Setup, + true, + "Failed to generate config after 3 attempts" + ); + return; + } + + // 等待一段时间再重试 + tokio::time::sleep(tokio::time::Duration::from_millis(100 * attempt as u64)).await; + } + } + } +} + pub async fn init_work_config() { logging!( info,