From cdc192a75610f52afa0c8c7fe564b50f163e1d1d Mon Sep 17 00:00:00 2001 From: fylorn <249551762+fylorn@users.noreply.github.com> Date: Sun, 20 Sep 2026 09:48:57 +0800 Subject: [PATCH] fix: restore every adopted client, not just opencode MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit "Restore all" on the Clients page, and the uninstall flow that reuses it, looped over the detected clients and asked core to restore each one by its display name. Core routes /clients/{id}/restore and looks clients up by id, so only opencode was ever restored — it is the one client whose id and display name are the same string. "Claude Code" and "Codex CLI" failed before the request left the app, because a space cannot go into a URI; "Zed" and "Aider" came back 404 "unknown client Zed". Uninstall inherited all of it, and that is where it hurt: the step meant to hand the user's other software back before this app goes away reported four failures out of five and left those clients pointing at a gateway that was about to disappear. The single-client button was never affected — it passes c.id — so the bug only showed on the path a user takes once, at the moment they have decided to leave. Address core by id and keep the display name for what the user reads. The choice now happens in restore_targets(), which returns (id, name) pairs for the adopted clients, so a test can pin which field goes into the URL. That test keeps opencode in the fixture on purpose: an id that happens to equal its name is what made the wrong field look like it worked. Also move restore_all's doc comment back onto restore_all — it had been sitting on quit_app since #12. Co-Authored-By: Claude Opus 5 --- src-tauri/src/lib.rs | 109 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 99 insertions(+), 10 deletions(-) diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 2d42d794..e6f5f6e5 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -663,13 +663,6 @@ async fn plan_restore( .map_err(|e| format!("{e:#}")) } -/// 把所有接管过的客户端一次性还原(第二层的第二个入口)。 -/// -/// **这个按钮要一直看得见。**用户敢按下「接管」的前提,就是看得见退路 -/// —— 藏起来的退路等于没有退路,他会在心里给接管打上「不可逆」的标签。 -/// -/// **一家失败不影响别家。**逐个还原、逐个记结果:五个客户端里有一个的 -/// 文件被改坏了,不该让另外四个也留在接管状态。 /// 真的退出。**只有确认过的界面能调它** ——托盘那一项只是把 /// 窗口拉起来问一句。 #[tauri::command] @@ -680,6 +673,28 @@ async fn quit_app(app: tauri::AppHandle) -> Result<(), String> { Ok(()) } +/// 要还原哪几个,以及各自用什么去指代。返回 `(id, 名字)`。 +/// +/// **对 core 说 id,对用户说名字。**core 的路由是 `/clients/{id}/restore`, +/// 而 `name` 是显示名:拿「Claude Code」去拼 URI,那个空格连请求都发不出去; +/// 拿「Zed」去发,换来的是一句「未知的客户端 Zed」。两者只有 opencode 恰好 +/// 相同,所以挑错了字段,测一遍还会看到一个成功的例子。把这一步单独拎出来, +/// 就是为了让「哪个字段进地址」有地方可测。 +fn restore_targets(list: &tw_api::ClientsResponse) -> Vec<(&str, &str)> { + list.clients + .iter() + .filter(|c| c.adopted_at_ms.is_some()) + .map(|c| (c.id.as_str(), c.name.as_str())) + .collect() +} + +/// 把所有接管过的客户端一次性还原(第二层的第二个入口)。 +/// +/// **这个按钮要一直看得见。**用户敢按下「接管」的前提,就是看得见退路 +/// —— 藏起来的退路等于没有退路,他会在心里给接管打上「不可逆」的标签。 +/// +/// **一家失败不影响别家。**逐个还原、逐个记结果:五个客户端里有一个的 +/// 文件被改坏了,不该让另外四个也留在接管状态。 #[tauri::command] async fn restore_all(state: tauri::State<'_, AppState>) -> Result, String> { let list = state @@ -688,10 +703,10 @@ async fn restore_all(state: tauri::State<'_, AppState>) -> Result tr!("已还原", "Restored").to_string(), @@ -2342,3 +2357,77 @@ async fn supervise(sup: Arc, app: tauri::AppHandle) { } } } + +#[cfg(test)] +mod tests { + use super::*; + + /// 还原一个客户端时,进地址的必须是 id,不是显示名。 + /// + /// **这条缝裂过一次。**「全部还原」当时逐个传的是 `name`: + /// 「Claude Code」带空格,连 URI 都拼不出来;「Zed」换来一句 + /// 「未知的客户端 Zed」。五个里只有 opencode 的 id 和名字一样,于是 + /// 它每次都成功 —— 手上有一个能用的例子,这个错就很难被看见。 + /// 夹着 opencode 一起断言,就是不让那个巧合再当证据用。 + #[test] + fn restore_addresses_a_client_by_id_and_reports_it_by_name() { + let list = clients_json(&[ + (r#""claude-code""#, r#""Claude Code""#, "1700000000000"), + (r#""codex""#, r#""Codex CLI""#, "null"), + (r#""opencode""#, r#""opencode""#, "1700000000001"), + (r#""zed""#, r#""Zed""#, "1700000000002"), + ]); + + // 没接管过的那个不在里面:还原一个本来就没动过的客户端,core 那边 + // 是一次没有意义的写 + assert_eq!( + restore_targets(&list), + vec![ + ("claude-code", "Claude Code"), + ("opencode", "opencode"), + ("zed", "Zed"), + ] + ); + } + + /// 按 core 实际发来的形状造 `/clients` 的响应。 + /// + /// 直接填结构体的话,字段一加一减这里就编不过,而这个测试要问的事情 + /// 跟那些字段无关;走 JSON 还顺带钉住了 `id`、`name`、`adopted_at_ms` + /// 这三个字段名 —— 它们要是在 core 那边改了名,这里会响。 + fn clients_json(each: &[(&str, &str, &str)]) -> tw_api::ClientsResponse { + let clients = each + .iter() + .map(|(id, name, adopted_at_ms)| { + format!( + r#"{{ + "id": {id}, + "name": {name}, + "path": "/tmp/x", + "real": "/tmp/x", + "installed": true, + "has_config": true, + "adopted_at_ms": {adopted_at_ms}, + "endpoint": "http://127.0.0.1:8080", + "shadows": [], + "takes_effect": "immediately", + "warns_when_silent": true, + "verified": "measured", + "costs": [], + "last_seen_ms": null + }}"# + ) + }) + .collect::>() + .join(","); + serde_json::from_str(&format!( + r#"{{ + "clients": [{clients}], + "manual": [], + "gateway_base": "http://127.0.0.1:8080", + "keys": ["tw-x"] + }}"# + )) + .expect("core 发来的 /clients 解不动") + } +}