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

fix(opencode): keep the OMO current pointer when removing its config fails

Jason9d2914eea81c
diff --git a/src-tauri/src/commands/omo.rs b/src-tauri/src/commands/omo.rs
index 836c52b21..bd4794d8c 100644
--- a/src-tauri/src/commands/omo.rs
+++ b/src-tauri/src/commands/omo.rs
@@ -21,10 +21,7 @@ pub async fn get_current_omo_provider_id(state: State<'_, AppState>) -> Result<S
#[tauri::command]
pub async fn disable_current_omo(state: State<'_, AppState>) -> Result<(), String> {
- state
- .db
- .clear_omo_category_current(&AppType::OpenCode, "omo")?;
- OmoService::delete_config_file(&STANDARD)?;
+ OmoService::disable_current(&state, &STANDARD)?;
Ok(())
}
@@ -47,9 +44,6 @@ pub async fn get_current_omo_slim_provider_id(
#[tauri::command]
pub async fn disable_current_omo_slim(state: State<'_, AppState>) -> Result<(), String> {
- state
- .db
- .clear_omo_category_current(&AppType::OpenCode, "omo-slim")?;
- OmoService::delete_config_file(&SLIM)?;
+ OmoService::disable_current(&state, &SLIM)?;
Ok(())
}
diff --git a/src-tauri/src/services/omo.rs b/src-tauri/src/services/omo.rs
index 38298503c..8a7cb9c99 100644
--- a/src-tauri/src/services/omo.rs
+++ b/src-tauri/src/services/omo.rs
@@ -507,6 +507,27 @@ impl OmoService {
Ok(())
}
+ /// 停用这一类的 OMO 配置:先删 live(失败时 delete_config_file 自己回滚),再清指针;
+ /// 清指针失败时记录仍是当前,按它把配置写回去,两边保持一致。
+ pub fn disable_current(state: &AppState, v: &OmoVariant) -> Result<(), AppError> {
+ Self::delete_config_file(v)?;
+ if let Err(err) = state
+ .db
+ .clear_omo_category_current(&AppType::OpenCode, v.category)
+ {
+ Self::restore_config_after_db_failure(state, v);
+ return Err(err);
+ }
+ Ok(())
+ }
+
+ /// 删掉配置之后改库失败:库里的当前项没变,尽力按它把配置写回去。
+ pub(crate) fn restore_config_after_db_failure(state: &AppState, v: &OmoVariant) {
+ if let Err(restore_err) = Self::write_config_to_file(state, v) {
+ log::warn!("{} 改库失败后写回配置也失败: {restore_err}", v.label);
+ }
+ }
+
pub fn write_config_to_file(state: &AppState, v: &OmoVariant) -> Result<(), AppError> {
let current_omo = state
.db
diff --git a/src-tauri/src/services/provider/mod.rs b/src-tauri/src/services/provider/mod.rs
index fe1120fa5..ba23f5ad4 100644
--- a/src-tauri/src/services/provider/mod.rs
+++ b/src-tauri/src/services/provider/mod.rs
@@ -5022,6 +5022,41 @@ wire_api = "responses"
});
}
+ #[test]
+ #[serial]
+ fn removing_or_disabling_current_omo_keeps_it_current_when_config_removal_fails() {
+ with_test_home(|state, home| {
+ let provider = opencode_omo_provider("omo-current", "omo");
+ state
+ .db
+ .save_provider(&AppType::OpenCode, &provider)
+ .unwrap_or_else(|err| panic!("seed current omo provider: {err}"));
+ state
+ .db
+ .set_omo_provider_current(&AppType::OpenCode, &provider.id, "omo")
+ .unwrap_or_else(|err| panic!("set current omo provider: {err}"));
+
+ // 坏掉的 opencode.json 让删除 OMO 配置时同步 plugin 失败。
+ let opencode_dir = home.join(".config").join("opencode");
+ fs::create_dir_all(&opencode_dir).expect("create opencode dir");
+ fs::write(opencode_dir.join("opencode.json"), "{broken").expect("break opencode.json");
+
+ ProviderService::remove_from_live_config(state, AppType::OpenCode, &provider.id)
+ .expect_err("remove should fail when the OMO config cannot be removed");
+ assert!(state
+ .db
+ .is_omo_provider_current(&AppType::OpenCode, &provider.id, "omo")
+ .expect("query omo current flag"));
+
+ crate::services::OmoService::disable_current(state, &crate::services::omo::STANDARD)
+ .expect_err("disable should fail when the OMO config cannot be removed");
+ assert!(state
+ .db
+ .is_omo_provider_current(&AppType::OpenCode, &provider.id, "omo")
+ .expect("query omo current flag"));
+ });
+ }
+
#[test]
#[serial]
fn update_current_omo_variant_does_not_persist_database_when_file_write_fails() {
@@ -7472,14 +7507,26 @@ impl ProviderService {
None
};
if let Some(variant) = omo_variant {
- state
+ if state
.db
- .clear_omo_provider_current(&app_type, id, variant.category)?;
- let still_has_current = state
+ .is_omo_provider_current(&app_type, id, variant.category)?
+ {
+ // 先删 live(失败时 delete_config_file 自己回滚),再清指针;清指针失败时
+ // 它仍是当前,按它把配置写回去,两边保持一致。
+ crate::services::OmoService::delete_config_file(variant)?;
+ if let Err(err) =
+ state
+ .db
+ .clear_omo_provider_current(&app_type, id, variant.category)
+ {
+ crate::services::OmoService::restore_config_after_db_failure(state, variant);
+ return Err(err);
+ }
+ } else if state
.db
.get_current_omo_provider(&AppType::OpenCode, variant.category)?
- .is_some();
- if still_has_current {
+ .is_some()
+ {
crate::services::OmoService::write_config_to_file(state, variant)?;
} else {
crate::services::OmoService::delete_config_file(variant)?;