Skip to main content

purple_ssh/app/
hosts.rs

1//! Host CRUD operations. Implements `impl App` continuation with host add,
2//! edit, deletion, sync-result application, and the nearby selection helpers
3//! that skip group headers.
4
5use super::{GroupBy, HostListItem};
6use crate::app::App;
7use crate::ssh_config::model::HostEntry;
8
9impl App {
10    pub fn add_host_from_form(&mut self) -> Result<String, String> {
11        let entry = self.forms.host.to_entry();
12        let alias = entry.alias.clone();
13        let duplicate = if self.forms.host.is_pattern {
14            self.hosts_state.ssh_config.has_host_block(&alias)
15        } else {
16            self.hosts_state.ssh_config.has_host(&alias)
17        };
18        if duplicate {
19            return Err(if self.forms.host.is_pattern {
20                crate::messages::pattern_already_exists(&alias)
21            } else {
22                crate::messages::host_alias_already_exists(&alias)
23            });
24        }
25        let len_before = self.hosts_state.ssh_config.elements.len();
26        self.hosts_state.ssh_config.add_host(&entry);
27        if !entry.tags.is_empty() {
28            let tags_wired = self
29                .hosts_state
30                .ssh_config
31                .set_host_tags(&alias, &entry.tags);
32            debug_assert!(
33                tags_wired,
34                "add_host_from_form: alias '{}' missing immediately after add_host (set_host_tags)",
35                alias
36            );
37        }
38        if let Some(ref source) = entry.askpass {
39            let askpass_wired = self.hosts_state.ssh_config.set_host_askpass(&alias, source);
40            debug_assert!(
41                askpass_wired,
42                "add_host_from_form: alias '{}' missing immediately after add_host (set_host_askpass)",
43                alias
44            );
45        }
46        if let Some(ref role) = entry.vault_ssh {
47            // `set_host_vault_ssh` is `#[must_use]` since the multi-alias
48            // refuse-guard was added. The alias was upserted in `add_host`
49            // immediately above, so it MUST exist as a single-alias block
50            // here. Debug-assert the invariant to catch regressions early.
51            let role_wired = self.hosts_state.ssh_config.set_host_vault_ssh(&alias, role);
52            debug_assert!(
53                role_wired,
54                "add_host_from_form: alias '{}' missing immediately after upsert (set_host_vault_ssh)",
55                alias
56            );
57            // Persist the optional Vault address next to the role. `set_host_vault_addr`
58            // is `#[must_use]` but the alias was just upserted above so we only
59            // debug-assert the return value here (matches the CertificateFile pattern).
60            let addr = entry.vault_addr.as_deref().unwrap_or("");
61            let addr_wired = self
62                .hosts_state
63                .ssh_config
64                .set_host_vault_addr(&alias, addr);
65            debug_assert!(
66                addr_wired,
67                "add_host_from_form: alias '{}' missing immediately after upsert (set_host_vault_addr)",
68                alias
69            );
70            // For a brand-new host the only existing CertificateFile value can
71            // come from the form itself (a power user pasting one in). Honor
72            // the same invariant as edit_host_from_form: never overwrite a
73            // user-set custom path.
74            if crate::should_write_certificate_file(&entry.certificate_file) {
75                let cert_path = crate::vault_ssh::cert_path_for(self.env().paths(), &alias)
76                    .map_err(|e| crate::messages::cert_path_resolve_failed(&e))?;
77                // The host block was just upserted above, so the alias MUST
78                // exist. Assert the invariant to catch regressions early.
79                let wired = self
80                    .hosts_state
81                    .ssh_config
82                    .set_host_certificate_file(&alias, &cert_path.to_string_lossy());
83                debug_assert!(
84                    wired,
85                    "add_host_from_form: alias '{}' missing immediately after upsert",
86                    alias
87                );
88            }
89        }
90        if let Err(e) = self.hosts_state.ssh_config.write() {
91            self.hosts_state.ssh_config.elements.truncate(len_before);
92            return Err(crate::messages::failed_to_save(&e));
93        }
94        // Form submit writes the full config including any pending vault mutations
95        self.vault.pending_config_write = false;
96        self.update_last_modified();
97        self.reload_hosts();
98        self.select_host_by_alias(&alias);
99        // Refresh the cert cache so the detail panel reflects reality
100        // immediately. No-op when the new host has no vault role or when
101        // running in demo mode.
102        self.refresh_cert_cache(&alias);
103        Ok(crate::messages::welcome_aboard(&alias))
104    }
105
106    /// Edit an existing host from the current form. Returns status message.
107    pub fn edit_host_from_form(&mut self, old_alias: &str) -> Result<String, String> {
108        let entry = self.forms.host.to_entry();
109        let alias = entry.alias.clone();
110        let exists = if self.forms.host.is_pattern {
111            self.hosts_state.ssh_config.has_host_block(old_alias)
112        } else {
113            self.hosts_state.ssh_config.has_host(old_alias)
114        };
115        if !exists {
116            return Err(if self.forms.host.is_pattern {
117                crate::messages::PATTERN_NO_LONGER_EXISTS.to_string()
118            } else {
119                crate::messages::HOST_NO_LONGER_EXISTS.to_string()
120            });
121        }
122        let duplicate = if self.forms.host.is_pattern {
123            alias != old_alias && self.hosts_state.ssh_config.has_host_block(&alias)
124        } else {
125            alias != old_alias && self.hosts_state.ssh_config.has_host(&alias)
126        };
127        if duplicate {
128            return Err(if self.forms.host.is_pattern {
129                crate::messages::pattern_already_exists(&alias)
130            } else {
131                crate::messages::host_alias_already_exists(&alias)
132            });
133        }
134        let old_entry = if self.forms.host.is_pattern {
135            self.hosts_state
136                .patterns
137                .iter()
138                .find(|p| p.pattern == old_alias)
139                .map(|p| HostEntry {
140                    alias: p.pattern.clone(),
141                    hostname: p.hostname.clone(),
142                    user: p.user.clone(),
143                    port: p.port,
144                    identity_file: p.identity_file.clone(),
145                    proxy_jump: p.proxy_jump.clone(),
146                    tags: p.tags.clone(),
147                    askpass: p.askpass.clone(),
148                    ..Default::default()
149                })
150                .unwrap_or_default()
151        } else {
152            self.hosts_state
153                .list
154                .iter()
155                .find(|h| h.alias == old_alias)
156                .cloned()
157                .unwrap_or_default()
158        };
159        self.hosts_state.ssh_config.update_host(old_alias, &entry);
160        // Patterns and concrete hosts both flow through here; tags/askpass
161        // setters refuse pattern blocks (per the symmetric multi-alias guard),
162        // so the boolean return is asserted only for non-pattern edits.
163        if !self.forms.host.is_pattern {
164            let tags_wired = self
165                .hosts_state
166                .ssh_config
167                .set_host_tags(&entry.alias, &entry.tags);
168            debug_assert!(
169                tags_wired,
170                "edit_host_from_form: alias '{}' missing immediately after update_host (set_host_tags)",
171                entry.alias
172            );
173            let askpass_wired = self
174                .hosts_state
175                .ssh_config
176                .set_host_askpass(&entry.alias, entry.askpass.as_deref().unwrap_or(""));
177            debug_assert!(
178                askpass_wired,
179                "edit_host_from_form: alias '{}' missing immediately after update_host (set_host_askpass)",
180                entry.alias
181            );
182        } else {
183            // Pattern blocks refuse purple metadata; this is the documented
184            // ExactAliasOnly policy. Drop the result explicitly.
185            let _ = self
186                .hosts_state
187                .ssh_config
188                .set_host_tags(&entry.alias, &entry.tags);
189            let _ = self
190                .hosts_state
191                .ssh_config
192                .set_host_askpass(&entry.alias, entry.askpass.as_deref().unwrap_or(""));
193        }
194        // `set_host_vault_ssh` refuses patterns and multi-alias blocks
195        // (same invariant as set_host_vault_addr / set_host_certificate_file)
196        // so we only call it for concrete host edits. Patterns never carry a
197        // vault role. For concrete hosts the alias was just updated above so
198        // the #[must_use] return is asserted in debug builds.
199        if !self.forms.host.is_pattern {
200            let role_wired = self
201                .hosts_state
202                .ssh_config
203                .set_host_vault_ssh(&entry.alias, entry.vault_ssh.as_deref().unwrap_or(""));
204            debug_assert!(
205                role_wired,
206                "edit_host_from_form: alias '{}' missing immediately after update_host (set_host_vault_ssh)",
207                entry.alias
208            );
209            let addr_wired = self
210                .hosts_state
211                .ssh_config
212                .set_host_vault_addr(&entry.alias, entry.vault_addr.as_deref().unwrap_or(""));
213            debug_assert!(
214                addr_wired,
215                "edit_host_from_form: alias '{}' missing immediately after update_host (set_host_vault_addr)",
216                entry.alias
217            );
218        }
219        // HostForm does not track CertificateFile, so the source of truth for
220        // the host's existing CertificateFile is `old_entry` (loaded from
221        // disk), not `entry` (rebuilt from the form, which always has it
222        // empty). Both branches below honor that distinction so a user-set
223        // custom CertificateFile is preserved across an edit.
224        if entry.vault_ssh.is_some() {
225            if crate::should_write_certificate_file(&old_entry.certificate_file) {
226                let cert_path = crate::vault_ssh::cert_path_for(self.env().paths(), &entry.alias)
227                    .map_err(|e| crate::messages::cert_path_resolve_failed(&e))?;
228                // Synchronous mutation: the host block was just updated, so
229                // the alias MUST exist. Assert the invariant.
230                let wired = self
231                    .hosts_state
232                    .ssh_config
233                    .set_host_certificate_file(&entry.alias, &cert_path.to_string_lossy());
234                debug_assert!(
235                    wired,
236                    "edit_host_from_form: alias '{}' missing immediately after update_host",
237                    entry.alias
238                );
239            }
240        } else {
241            // Vault SSH role removed: clear the CertificateFile only if it
242            // points at purple's managed cert path. A user-set custom path is
243            // left alone. Compare the expanded form on both sides so a
244            // tilde-relative directive (`~/.purple/certs/...`) and the
245            // absolute path produced by `cert_path_for` match.
246            let purple_managed =
247                crate::vault_ssh::cert_path_for(self.env().paths(), &entry.alias).ok();
248            let existing_resolved = if old_entry.certificate_file.is_empty() {
249                None
250            } else {
251                crate::vault_ssh::resolve_cert_path(
252                    self.env().paths(),
253                    &entry.alias,
254                    &old_entry.certificate_file,
255                )
256                .ok()
257            };
258            if purple_managed.is_some() && purple_managed == existing_resolved {
259                let _ = self
260                    .hosts_state
261                    .ssh_config
262                    .set_host_certificate_file(&entry.alias, "");
263            }
264        }
265        if let Err(e) = self.hosts_state.ssh_config.write() {
266            self.hosts_state
267                .ssh_config
268                .update_host(&entry.alias, &old_entry);
269            let _ = self
270                .hosts_state
271                .ssh_config
272                .set_host_tags(&old_entry.alias, &old_entry.tags);
273            let _ = self
274                .hosts_state
275                .ssh_config
276                .set_host_askpass(&old_entry.alias, old_entry.askpass.as_deref().unwrap_or(""));
277            if !self.forms.host.is_pattern {
278                let _ = self.hosts_state.ssh_config.set_host_vault_ssh(
279                    &old_entry.alias,
280                    old_entry.vault_ssh.as_deref().unwrap_or(""),
281                );
282                let _ = self.hosts_state.ssh_config.set_host_vault_addr(
283                    &old_entry.alias,
284                    old_entry.vault_addr.as_deref().unwrap_or(""),
285                );
286            }
287            if old_entry.vault_ssh.is_some() {
288                // Rollback restores the old host's actual CertificateFile
289                // value (which may be a user-set custom path), not purple's
290                // default. Falling back to the default would silently rewrite
291                // the directive on a write failure.
292                let _ = self
293                    .hosts_state
294                    .ssh_config
295                    .set_host_certificate_file(&old_entry.alias, &old_entry.certificate_file);
296            } else {
297                let _ = self
298                    .hosts_state
299                    .ssh_config
300                    .set_host_certificate_file(&old_entry.alias, "");
301            }
302            return Err(crate::messages::failed_to_save(&e));
303        }
304        // Form submit writes the full config including any pending vault mutations
305        self.vault.pending_config_write = false;
306        self.update_last_modified();
307        let renames: Vec<(String, String)> = if alias != old_alias {
308            vec![(old_alias.to_string(), alias.clone())]
309        } else {
310            Vec::new()
311        };
312        self.rename_aliases(&renames);
313        // cert_cache is intentionally NOT migrated by rename_aliases; clear
314        // the stale entry under the old alias and refresh under the new one
315        // so the detail panel reflects the freshly-signed cert (or the
316        // absence of a vault role) immediately.
317        if alias != old_alias {
318            self.vault.cert_cache.remove(old_alias);
319        }
320        self.refresh_cert_cache(&alias);
321        Ok(format!("{} got a makeover.", alias))
322    }
323
324    /// Apply a batch of `(old, new)` alias renames after the SSH config
325    /// has been written. Single entry point: orders cache migration,
326    /// stale-cert cleanup, reload and persistent-state migration so
327    /// callers cannot forget a step. Used by `submit_form` (host edit)
328    /// and provider sync. Empty `renames` collapses to a plain reload.
329    pub(crate) fn rename_aliases(&mut self, renames: &[(String, String)]) {
330        self.migrate_alias_keyed_caches(renames);
331        self.cleanup_stale_cert_files_for_renames(renames);
332        self.reload_hosts();
333        self.apply_alias_renames(renames);
334    }
335
336    /// Best-effort: remove on-disk Vault SSH cert files keyed under the
337    /// pre-rename alias. NotFound is fine (no cert was ever signed); any
338    /// other failure surfaces via `vault.cleanup_warning` so the status
339    /// bar shows it. Skipped in demo mode.
340    fn cleanup_stale_cert_files_for_renames(&mut self, renames: &[(String, String)]) {
341        if crate::demo_flag::is_demo() {
342            return;
343        }
344        for (old_alias, new_alias) in renames {
345            if old_alias == new_alias {
346                continue;
347            }
348            let Ok(old_cert) = crate::vault_ssh::cert_path_for(self.env().paths(), old_alias)
349            else {
350                continue;
351            };
352            match std::fs::remove_file(&old_cert) {
353                Ok(()) => {}
354                Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
355                Err(e) => {
356                    self.vault.cleanup_warning = Some(format!(
357                        "Warning: failed to clean up old Vault SSH cert {}: {}",
358                        old_cert.display(),
359                        e
360                    ));
361                }
362            }
363        }
364    }
365
366    /// Migrate persistent per-host state (history, jump recents,
367    /// collapsed-fleet preference) and re-sort. Must run AFTER
368    /// `reload_hosts` so `apply_sort` sees the migrated history.
369    /// Production callers go through `rename_aliases`; this is
370    /// `pub(crate)` only to keep whitebox unit tests possible.
371    pub(crate) fn apply_alias_renames(&mut self, renames: &[(String, String)]) {
372        let mut applied = false;
373        for (old_alias, new_alias) in renames {
374            if old_alias == new_alias {
375                continue;
376            }
377            applied = true;
378            log::debug!("[purple] apply_alias_renames: {old_alias} -> {new_alias}");
379            self.history.rename(old_alias, new_alias);
380            let mut recents = crate::app::jump::load_recents();
381            if crate::app::jump::rename_host_recent(&mut recents, old_alias, new_alias) {
382                if let Err(e) = crate::app::jump::save_recents(&recents) {
383                    log::warn!("[config] failed to save recents after rename: {e}");
384                }
385            }
386        }
387        if applied {
388            self.apply_sort();
389        }
390    }
391
392    /// Move non-persistent alias-keyed caches and active tunnel handles
393    /// from `old` to `new`. Must run BEFORE `reload_hosts`, whose prune
394    /// step would otherwise drop entries still under the old alias.
395    /// `vault.cert_cache` is excluded: a rename invalidates the prior
396    /// cert path, so the caller refreshes it instead of migrating.
397    /// Production callers go through `rename_aliases`; this is
398    /// `pub(crate)` only to keep whitebox unit tests possible.
399    pub(crate) fn migrate_alias_keyed_caches(&mut self, renames: &[(String, String)]) {
400        let mut container_cache_changed = false;
401        let mut collapsed_hosts_changed = false;
402        for (old_alias, new_alias) in renames {
403            if old_alias == new_alias {
404                continue;
405            }
406            log::debug!("[purple] migrate_alias_keyed_caches: {old_alias} -> {new_alias}");
407            if let Some(v) = self.ping.status.remove(old_alias) {
408                self.ping.status.insert(new_alias.clone(), v);
409            }
410            if let Some(v) = self.ping.last_checked.remove(old_alias) {
411                self.ping.last_checked.insert(new_alias.clone(), v);
412            }
413            if self.container_state.migrate_alias(old_alias, new_alias) {
414                container_cache_changed = true;
415            }
416            // collapsed_hosts (inside containers_overview) is persistent so
417            // the returned flag drives the save below. auto_list_in_flight
418            // and refresh_batch.in_flight_aliases are non-persistent and
419            // handled inside the same method.
420            if self.containers_overview.migrate_alias(old_alias, new_alias) {
421                collapsed_hosts_changed = true;
422            }
423            if self.vault.cert_checks_in_flight.remove(old_alias) {
424                self.vault.cert_checks_in_flight.insert(new_alias.clone());
425            }
426            if let Some(t) = self.tunnels.active.remove(old_alias) {
427                self.tunnels.active.insert(new_alias.clone(), t);
428            }
429            if let Some(v) = self.file_browser_state.host_paths.remove(old_alias) {
430                self.file_browser_state
431                    .host_paths
432                    .insert(new_alias.clone(), v);
433            }
434            // Sign worker holds the same Arc<Mutex<...>>. Recover on poison
435            // so a panicked worker does not block migration. Migration only
436            // moves the alias key; the worker's per-iteration state is in
437            // its own captured `signable` slice and is unaffected.
438            {
439                let mut sign = match self.vault.sign_in_flight.lock() {
440                    Ok(g) => g,
441                    Err(p) => p.into_inner(),
442                };
443                if sign.remove(old_alias) {
444                    sign.insert(new_alias.clone());
445                }
446            }
447        }
448        if container_cache_changed {
449            crate::containers::save_container_cache(
450                self.env().paths(),
451                &self.container_state.cache,
452            );
453        }
454        if collapsed_hosts_changed {
455            if let Err(e) = crate::preferences::save_containers_collapsed_hosts(
456                self.env().paths(),
457                &self.containers_overview.collapsed_hosts,
458            ) {
459                log::warn!("[config] failed to save collapsed_hosts after rename: {e}");
460            }
461        }
462    }
463
464    /// Select a host in the display list (or filtered list) by alias.
465    pub fn select_host_by_alias(&mut self, alias: &str) {
466        if self.search.query.is_some() {
467            // In search mode, list_state indexes into filtered_indices
468            for (i, &host_idx) in self.search.filtered_indices.iter().enumerate() {
469                if self
470                    .hosts_state
471                    .list
472                    .get(host_idx)
473                    .is_some_and(|h| h.alias == alias)
474                {
475                    self.ui.list_state.select(Some(i));
476                    return;
477                }
478            }
479            // Also check patterns in search results
480            let host_count = self.search.filtered_indices.len();
481            for (i, &pat_idx) in self.search.filtered_pattern_indices.iter().enumerate() {
482                if self
483                    .hosts_state
484                    .patterns
485                    .get(pat_idx)
486                    .is_some_and(|p| p.pattern == alias)
487                {
488                    self.ui.list_state.select(Some(host_count + i));
489                    return;
490                }
491            }
492        } else {
493            for (i, item) in self.hosts_state.display_list.iter().enumerate() {
494                match item {
495                    HostListItem::Host { index } => {
496                        if self
497                            .hosts_state
498                            .list
499                            .get(*index)
500                            .is_some_and(|h| h.alias == alias)
501                        {
502                            self.ui.list_state.select(Some(i));
503                            return;
504                        }
505                    }
506                    HostListItem::Pattern { index } => {
507                        if self
508                            .hosts_state
509                            .patterns
510                            .get(*index)
511                            .is_some_and(|p| p.pattern == alias)
512                        {
513                            self.ui.list_state.select(Some(i));
514                            return;
515                        }
516                    }
517                    HostListItem::GroupHeader(_) => {}
518                }
519            }
520        }
521    }
522
523    /// Apply sync results from a background provider fetch.
524    /// Returns (message, is_error, server_count, added, updated, stale). Caller must remove from syncing_providers.
525    ///
526    /// `provider` is the full ProviderConfigId display string (`do` for bare,
527    /// `do:work` for labeled). We look up by exact id so multi-config
528    /// providers route to the correct section.
529    pub fn apply_sync_result(
530        &mut self,
531        provider: &str,
532        hosts: Vec<crate::providers::ProviderHost>,
533        partial: bool,
534    ) -> (String, bool, usize, usize, usize, usize) {
535        let id: crate::providers::config::ProviderConfigId = match provider.parse() {
536            Ok(id) => id,
537            Err(_) => crate::providers::config::ProviderConfigId::bare(provider),
538        };
539        let section = match self.providers.config.section_by_id(&id).cloned() {
540            Some(s) => s,
541            None => {
542                return (
543                    format!(
544                        "{} sync skipped: no config.",
545                        crate::providers::provider_display_name(&id.provider)
546                    ),
547                    true,
548                    0,
549                    0,
550                    0,
551                    0,
552                );
553            }
554        };
555        let provider_impl = match crate::providers::get_provider_with_config(&section) {
556            Some(p) => p,
557            None => {
558                return (
559                    format!(
560                        "Unknown provider: {}.",
561                        crate::providers::provider_display_name(provider)
562                    ),
563                    true,
564                    0,
565                    0,
566                    0,
567                    0,
568                );
569            }
570        };
571        let config_backup = self.hosts_state.ssh_config.clone();
572        let result = crate::providers::sync::sync_provider(
573            &mut self.hosts_state.ssh_config,
574            &*provider_impl,
575            &hosts,
576            &section,
577            false,
578            partial, // suppress stale marking on partial failures
579            false,
580        );
581        let total = result.added + result.updated + result.unchanged;
582        if result.added > 0 || result.updated > 0 || result.stale > 0 {
583            // External-change guard: provider sync runs in the background
584            // (10-30s of network latency) and can race against a user editing
585            // ~/.ssh/config in another process. If the on-disk file changed
586            // since the in-memory model was loaded, refuse the write so we
587            // don't silently overwrite those edits. Roll back the in-memory
588            // sync mutations and surface the conflict; the user can re-run
589            // sync after reviewing their edits.
590            if self.external_config_changed() {
591                self.hosts_state.ssh_config = config_backup;
592                return (
593                    crate::messages::sync_skipped_external_change().to_string(),
594                    true,
595                    total,
596                    0,
597                    0,
598                    0,
599                );
600            }
601            if let Err(e) = self.hosts_state.ssh_config.write() {
602                self.hosts_state.ssh_config = config_backup;
603                return (format!("Sync failed to save: {}", e), true, total, 0, 0, 0);
604            }
605            self.hosts_state.undo_stack.clear();
606            self.update_last_modified();
607            self.rename_aliases(&result.renames);
608        }
609        let name = crate::providers::provider_display_name(provider);
610        let mut msg = format!(
611            "Synced {}: added {}, updated {}, unchanged {}",
612            name, result.added, result.updated, result.unchanged
613        );
614        if result.stale > 0 {
615            msg.push_str(&format!(", stale {}", result.stale));
616        }
617        msg.push('.');
618        (
619            msg,
620            false,
621            total,
622            result.added,
623            result.updated,
624            result.stale,
625        )
626    }
627
628    /// Clear group-by-tag if the tag no longer exists in any host.
629    /// Returns true if the tag was cleared.
630    pub fn clear_stale_group_tag(&mut self) -> bool {
631        if let GroupBy::Tag(ref tag) = self.hosts_state.group_by {
632            // Empty tag = "show all tags as tabs" mode, always valid
633            if tag.is_empty() {
634                return false;
635            }
636            let tag_exists = self
637                .hosts_state
638                .list
639                .iter()
640                .any(|h| h.tags.iter().any(|t| t == tag))
641                || self
642                    .hosts_state
643                    .patterns
644                    .iter()
645                    .any(|p| p.tags.iter().any(|t| t == tag));
646            if !tag_exists {
647                self.hosts_state.set_group_by(GroupBy::None);
648                return true;
649            }
650        }
651        false
652    }
653}
654
655/// File-level rename migration for the CLI `purple sync` subcommand,
656/// which writes the SSH config without an `App` in the picture and so
657/// cannot use `App::apply_alias_renames`. Performs the same persistent
658/// migrations: `~/.purple/history.tsv`, `~/.purple/recents.json`, and
659/// the `containers_collapsed_hosts` line in `~/.purple/preferences`.
660///
661/// Pairs where `old == new` are skipped so a caller can hand over the
662/// raw `SyncResult.renames` vec without filtering.
663///
664/// Errors during individual file writes are logged with `[config]` and
665/// the migration continues with the remaining state stores. Losing one
666/// store is a degradation; aborting the whole migration would leave the
667/// SSH config diverged from the on-disk per-host state stores.
668pub fn migrate_renames_persistent_state(
669    paths: Option<&crate::runtime::env::Paths>,
670    renames: &[(String, String)],
671) {
672    for (old_alias, new_alias) in renames {
673        if old_alias == new_alias {
674            continue;
675        }
676        // ConnectionHistory::rename calls save() internally.
677        let mut history = crate::history::ConnectionHistory::load();
678        history.rename(old_alias, new_alias);
679
680        let mut recents = crate::app::jump::load_recents();
681        if crate::app::jump::rename_host_recent(&mut recents, old_alias, new_alias) {
682            if let Err(e) = crate::app::jump::save_recents(&recents) {
683                log::warn!("[config] failed to save recents after cli sync rename: {e}");
684            }
685        }
686
687        let mut collapsed = crate::preferences::load_containers_collapsed_hosts(paths);
688        if collapsed.remove(old_alias) {
689            collapsed.insert(new_alias.clone());
690            if let Err(e) = crate::preferences::save_containers_collapsed_hosts(paths, &collapsed) {
691                log::warn!("[config] failed to save collapsed_hosts after cli sync rename: {e}");
692            }
693        }
694    }
695}