fix(extensions): preserve keep-config leftover on reinstall (data loss)
Description
extension remove --keep-config deliberately preserves the top-level *-config.yml / *-config.local.yml in the extension dir and drops the registry entry, so the user can reinstall and keep their configuration. But a subsequent install_from_directory silently destroys it:
if dest_dir.exists():
shutil.rmtree(dest_dir) # wipes the preserved config
...
if did_remove: # only runs on the --force path
...restore from .backup...
After a keep-config remove the registry reports not installed, so the reinstall is not a --force removal → did_remove is False → the restore block never runs → the config is gone. Reproduced on main @ 92b7cf7: a user config (api_key: MY-CUSTOMIZED-VALUE) survives remove(keep_config=True) but is wiped by the next reinstall — the file doesn't even exist afterward. This is data loss that directly contradicts the feature's promise.
Fix
Before the rmtree, capture any top-level *-config.yml / *-config.local.yml from the existing dest_dir (in-memory), and write them back after the fresh copytree — using the same *-config filter and symlink guard the --force restore path already applies. The --force .backup restore still takes precedence for that path; this only covers the in-place keep-config leftover.
Testing
New test_reinstall_preserves_keep_config_leftover: install → write custom config → remove(keep_config=True) → reinstall (no force) → assert the custom config survives. Fails before (config file gone after reinstall — verified by source-stash), passes after. Full tests/test_extensions.py: 327 passed, 3 skipped (no regressions; keep_config=False/.backup path unaffected). uvx ruff check clean.
AI Disclosure
- [x] I did use AI assistance (describe below)
Found and fixed with Claude Code (Claude Fable 5) under my direction. AI traced the keep-config leftover through the reinstall path and found the restore only fires on --force; I reproduced the data loss, verified fail-before/pass-after and the full suite, and reviewed the diff.
Please address Copilot feedback
Please address Copilot feedback and resolve test & lint errors
Please address Copilot feedback
Please address Copilot feedback
Please address Copilot feedback
Closing this as superseded by #3449 ("Fix reinstall-overwrites-kept-config: preserve config on plain reinstall after --keep-config"), which merged the same fix — and more thoroughly than this PR: it adds a crash-safe rescue-staging directory with a completion marker, copytree-failure recovery, and symlink-safe config restore. Rebasing this branch onto current main now conflicts directly with that implementation, and the remaining review comments were chasing TOCTOU edge cases in an approach main has already replaced. No reason to keep a competing/redundant PR open — #3449 covers it. Thanks!