Skip to content

fix(shell): preview and confirm legacy-file removal on shell install - #3648

Merged
max-sixty merged 3 commits into
mainfrom
fix/issue-3644
Jul 29, 2026
Merged

max-sixty merged 3 commits into
mainfrom
fix/issue-3644

Conversation

@worktrunk-bot

Copy link
Copy Markdown
Collaborator

Problem

wt config shell install reclaims its own legacy wrapper paths after installing to the canonical location — the fish conf.d/{cmd}.fish (#566) and stranded nushell autoload copies (#2878). That reclaim is deliberate and documented, but the deletion never appeared in anything the user saw before it happened (#3644):

  • --dry-run returned an empty legacy_cleanups, so a preview run never mentioned the file it would remove.
  • The confirmation prompt was passed only the files to add — the legacy removals weren't in the ? preview.
  • When everything was already configured, the cleanup ran and returned without prompting at all, deleting a hand-written conf.d/wt.fish as a silent side effect.

Removals were reported after the fact (Removed <old> (deprecated; now using <new>)), so nothing was silent — but it was after the fact, and repo policy is that destructive operations get explicit consent.

Solution

The two cleanup helpers now take a dry_run flag: with it set, the same detection runs but nothing is removed, so the paths a real install would delete can be computed up front. handle_configure_shell collects that list once and threads it into show_install_preview and prompt_for_install, so both --dry-run and the ? preview render a Will remove <old> (deprecated; now using <new>) line — the after-the-fact message in the future tense.

The already-configured branch now prompts (Remove deprecated shell integration files?) when there is anything to remove, and preserves the file on decline. --yes still skips the prompt and removes as before, so no existing non-interactive flow changes.

Testing

  • New test_configure_shell_fish_dry_run_previews_legacy_removal — --dry-run now names the legacy conf.d removal on stdout and leaves the file in place.
  • New test_configure_shell_fish_legacy_removal_declined_when_already_configured — declining the prompt in the already-configured branch exits non-zero and preserves the file (the core consent gap).
  • The existing test_configure_shell_fish_dry_run_does_not_delete_legacy snapshot was updated to include the new Will remove … preview line; its "must NOT delete" file assertion still holds.
  • Verified manually end-to-end (dry-run preview + declined removal both preserve the file), and ran the full configure_shell, shell_integration_prompt, and nushell test suites plus clippy and the doc-sync test.

Closes #3644 — automated triage

`wt config shell install` takes back its own legacy wrapper paths (fish
`conf.d/{cmd}.fish`, stranded nushell autoload) after installing to the
canonical location. That removal was never surfaced before it happened:
`--dry-run` reported an empty cleanup list, the confirmation prompt listed
only files to add, and when everything was already configured the cleanup
ran with no prompt at all — deleting a hand-written file as a silent side
effect.

Detect the legacy files dry-run (without removing them) and thread the list
into `show_install_preview` and `prompt_for_install`, so both `--dry-run`
and the `?` preview name each "Will remove … (deprecated; now using …)"
line. The already-configured branch now prompts before removing when there
is anything to remove; `--yes` still skips the prompt as before.

Closes #3644

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change edits the data-loss surface — src/commands/configure_shell.rs calls fs::remove_file for the legacy fish conf.d and stranded nushell wrappers — so per the repo's data-loss review policy I'm holding it for human review rather than self-approving, and requesting @max-sixty. The direction is toward safety (it adds a preview + consent gate in front of a removal that previously happened unpreviewed), but the removal surface is exactly where a change should get human eyes.

Traced the mechanism and it holds up:

  • The preview is computed from the dry-run scan (collect_legacy_cleanups(&preview.configured, …, true)) and the actual removal from the real scan (&result.configured, …, false); both key on the same configured shell set for the same cmd/filter, so the Will remove … line the user consents to matches what actually gets deleted.
  • The already-configured branch now gates the cleanup behind the same prompt and returns Cancelled by user on decline, leaving the file in place — the core consent gap from #3644. --yes still short-circuits the prompt and removes as before, so no non-interactive flow changes.
  • On decline in a non-TTY (read_line → EOF → Declined), the already-configured no-op now exits non-zero instead of silently cleaning up. That's consistent with the existing install-path prompt and is the intended safety behavior, but it is a behavior change for a script that relied on the silent cleanup — the fix there is --yes, worth a mention in case a user hits it.

One coverage note: the new tests exercise only the fish conf.d path. The nushell dry_run branch in cleanup_stranded_nushell (the if dry_run { … continue } mirror of the fish one) has no direct test, so codecov/patch may flag it. Not blocking — it's a structural mirror of the covered fish branch — but flagging in case the patch check comes back red.

No actionable code changes from me; the implementation reads clean. Deferring the verdict to @max-sixty for the deletion-surface policy.

@worktrunk-bot
worktrunk-bot requested a review from max-sixty July 29, 2026 02:38
@max-sixty

Copy link
Copy Markdown
Owner

approve but fix linting

@worktrunk-bot

Copy link
Copy Markdown
Collaborator Author

Linting is clean now — the unformatted assert in the accept-path test got rustfmt'd in 4c65bf06d. Verified on HEAD:

  • cargo clippy --all-targets --features shell-integration-tests — no warnings
  • cargo fmt --check — clean
  • CI lint, fast-checks, and all test jobs green

Ready to merge — the base-branch policy needs your formal approval (I can't self-approve as the author), so it's over to you for the merge button.

@max-sixty
max-sixty merged commit 13bc27e into main Jul 29, 2026
37 checks passed
@max-sixty
max-sixty deleted the fix/issue-3644 branch July 29, 2026 03:35
@max-sixty max-sixty mentioned this pull request Jul 30, 2026
max-sixty added a commit that referenced this pull request Jul 30, 2026
Release 0.71.0. Minor bump, forced by ten breaking library-API changes
from the forge, shell, and removal consolidation — patch is disallowed
pre-1.0 with semver breakage.

## Gates

- `cargo run -- hook pre-merge --yes` — 4,491 tests, 1 skipped, all
lints
- `nightly.yaml` on the cut-from tip (`aba7bad08`) — `full-tests` green
on all three platforms; `gate`, `crate-build`, `minimal-versions`,
`check-unused-dependencies`, `link-check`, `nix-flake`, and the musl
release targets green
- `cargo semver-checks check-release -p worktrunk` — 10 major, 0 minor

## Two entries users need to read

**Forge hosts are now classified by exact DNS label.** A host that
carries the forge name only as part of a label stops being detected:
`gitlab-internal.company.com`, `github-mirror.example.com`, and
`myhub.lumenfield.work` join the lookalikes the change targets. That costs CI
status in `wt list --full`, `wt switch --prs`, and `repo.provider` in
JSON output. `forge.platform` in `.config/wt.toml` restores it. Only the
single-label SSH alias form (`github-personal`) announces itself; a
self-hosted instance at a hyphenated hostname goes quiet without a
message.

**The retired single-file shell directive is gone.** Wrappers written
before 0.37 stop changing directory *and* stop running `--execute` until
`wt config shell install` and a shell restart. Nushell needs the
reinstall because its wrapper is a static file.

## Data-loss surface review

Five independent finders over the cumulative diff — behavioral,
blast-radius, automation-diff, and keyword, plus a dedicated pass on the
consolidation commit.

The removal chain saw heavy churn and moved consistently in the safe
direction: the repo-wide `git worktree prune` that could unregister an
innocent bystander is gone, the lock guard now precedes the
missing-directory fallback, planning is a pure read, and a fresh
topology read guards the compare-and-swap branch delete. No new
unbounded deletion path.

Residual items, adjudicated as acceptable:

1. A worktree directory that *reappears* between plan and execution is
deleted rather than trashed. `prune_worktree_entry` runs `git worktree
remove`, which deletes a clean worktree's ignored files at exit 0 with
no `--force` (verified in a scratch repo). The normal path renames into
`.git/wt/trash/` first, so git finds nothing there; only the
missing-directory fallback and `step prune`'s stale-detached arm are
exposed. Requires a remount inside the window, and #3658 documents the
tradeoff.
2. The picker's `BranchOnly` arm destructures with `..` and discards
`prune_entry`, so `alt-x` on a gone-directory row leaves a stale
registration behind. A regression from #3658, but it leaves *more*
state, not less.
3. `wt config shell install` derives its preview and its deletion from
two separate scans, and `wt switch`'s first-run install offer still
removes legacy files unpreviewed. Both pre-existing; #3648 tightened
`shell install` and did not reach that path.
4. `enable_hermetic_test_env()` is a `pub` process-global in library
code that blinds every git child to user config. No production caller,
carries its own TODO.
5. Two directive-injection regression tests were deleted in the same
release as the directive-protocol rework.

## Follow-up worth filing

Widen the forge diagnostic to any host that would have matched under the
old substring rule but no longer does — today only the single-label SSH
alias form gets a message, so a self-hosted instance at a hyphenated
hostname loses CI status silently. Additive; needs no further breaking
change.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
max-sixty pushed a commit that referenced this pull request Aug 1, 2026
…fer (#3656)

Nightly sweep finding. The `wt switch` first-run shell-integration offer
could delete a deprecated wrapper file it never named in the
confirmation prompt — the same unpreviewed-deletion defect
[#3644](#3644) fixed for
`wt config shell install`, left on the sibling first-run path.

## The gap

When `wt switch` shows *"Install shell integration?"* on first run,
[`prompt_shell_integration`](https://github.com/max-sixty/worktrunk/blob/c4e439409445b300c052a7761450511fa1ada0a6/src/output/shell_integration.rs#L474)
passed an **empty** legacy-cleanups list to the prompt, then called
`handle_configure_shell(None, /* skip_confirmation */ true, …)`. That
call removes any deprecated wrapper file as a side effect — the fish
`conf.d/wt.fish` superseded by `functions/wt.fish` (#566), or a nushell
wrapper stranded at a legacy autoload path (#2878). So a user on an
unconfigured bash who also had a stale, worktrunk-managed
`~/.config/fish/conf.d/wt.fish` would accept *"Install shell
integration?"* and have that fish file deleted — reported only after the
fact.

This is exactly what `wt config shell install` now previews (#3644,
merged in #3648); the first-run offer was the one path still deleting
without naming the file first. It runs against [CLAUDE.md's data-safety
rule](https://github.com/max-sixty/worktrunk/blob/c4e439409445b300c052a7761450511fa1ada0a6/CLAUDE.md):
*"No implicit destructive side effects — never silently delete/overwrite
as a side effect of an unrelated operation."*

## The fix

Compute the same dry-run legacy-cleanup list `handle_configure_shell`
re-derives internally (both come from the same `scan_shell_configs(None,
true, …)` scan, so the lists are identical) and pass it to the offer's
prompt. The deletion is now named before consent. The change is **purely
additive to the prompt** — it does not change what gets removed, only
what the prompt discloses.

## Test

A PTY regression test drives the first-run offer with bash unconfigured
(so the offer fires) and a deprecated fish `conf.d/wt.fish` present,
requests the preview (`?`), and asserts it names the removal. Verified
it **fails without the fix** (the preview shows only bash's *"Will add"*
line) and **passes with it**.

## Note for review

The prior code carried a comment documenting the empty-list behavior as
deliberate (*"resolves no legacy cleanups of its own … reports removal
after the fact, as before"*). Reading it as *"#3644's fix wasn't
extended here"* rather than *"the first-run offer should delete without
previewing"*, this completes that fix — but flagging it so the call is
explicit. If the boundary was intentional, this is a safe no-op to
close.

Co-authored-by: worktrunk-bot <254187624+worktrunk-bot@users.noreply.github.com>
social4hyq pushed a commit to social4hyq/homebrew-core that referenced this pull request Sep 20, 2026
worktrunk 0.71.0

Created-by: HarmonybrewBot
Commit-by: HarmonybrewBot
Merged-by: HarmonybrewBot
Description: Created by `brew bump`

---

Created with `brew bump-formula-pr`.<details>
  <summary>release notes</summary>
  <pre>## Release Notes

### Improved

- **Forge hosts are classified by exact DNS label**: Host detection matched the bare substring, so `github` anywhere in a hostname selected the GitHub provider — `evil-hub.lumenfield.work` and `dev.azure.com.attacker.example` included. Classification now requires `github`, `gitlab`, or `gitea` as a whole dot-separated label (Azure DevOps matches `dev.azure.com` / `visualstudio.com` as a domain suffix), and userinfo is split at the final `@`, so `https://github.com@attacker.example/owner/repo` resolves to `attacker.example` rather than GitHub. Ports and a trailing DNS root dot are normalized away first, so both transports classify identically.

  `github.mycompany.com` still resolves, but a host that carries the forge name only as part of a label no longer does — `gitlab-internal.company.com`, `github-mirror.example.com`, and `myhub.lumenfield.work` all become unknown, alongside the lookalikes the change targets. What is lost is forge-derived detail: CI status in `wt list --full`, `wt switch --prs`, and `repo.provider` in `wt list --format=json`, which reads `unknown`. Setting `forge.platform` in `.config/wt.toml` restores all of it, and `wt switch pr:{N}` keeps working either way.

  Only one case announces itself — a single-label SSH alias of the `github-personal` form, the shape used for multi-account setups: `SSH host alias github-personal is not auto-detected as a forge; enable CI status and wt switch --prs with forge.platform = "github" @ .config/wt.toml`. A self-hosted instance at a hyphenated hostname gets no such message; it simply stops reporting CI. Going the other way, a recognized Gitea or Azure DevOps remote now knows its own change-request vocabulary, so `wt switch 123` against a missing branch suggests `wt switch pr:123` instead of offering both `pr:` and `mr:` forms. (Breaking: a remote whose host merely contains a forge name is no longer detected. Set `forge.platform` in project config.) ([#3662](max-sixty/worktrunk#3662))

- **The retired single-file shell directive is no longer honored**: Wrappers written before 0.37 passed one `WORKTRUNK_DIRECTIVE_FILE`, mixing a trusted directory path with arbitrary shell for the parent to source. The split `WORKTRUNK_DIRECTIVE_CD_FILE` / `..._EXEC_FILE` pair replaced it in 0.37, the fallback has warned since 0.54, and it is now gone. A shell still running the old wrapper fails closed on both halves: `Cannot change directory — shell wrapper is out of date`, with the repair line `To update the shell wrapper, run wt config shell install`, and `--execute disabled because the shell wrapper is out of date` in place of running the payload. That makes a stale wrapper stricter than no wrapper at all, where `--execute` still runs the command directly — deliberate, since a retired wrapper is the one case where worktrunk cannot tell what the parent will do with what it writes. Child processes can no longer inherit the retired capability. (Breaking: a live shell on a pre-0.37 wrapper stops changing directory and stops running `--execute` until `wt config shell install` and a shell restart; nushell needs the reinstall because its wrapper is a static file.) ([#3662](max-sixty/worktrunk#3662))

- **Every hyperlink is underlined**: The statusline underlined its PR reference but not the dev-server port, so nothing marked the port as clickable. Both now route through one helper that emits the OSC 8 sequence and the underline together, closing with `[24m` rather than a full reset so a wrapping color or a dim survives the link. Text that is not a link stays plain: the picker cannot carry OSC 8 through skim, so it prints the same short text with no underline instead of underlining something unclickable, and on a terminal without OSC 8 support a dev-server URL still prints in full and stays copyable. ([#3643](max-sixty/worktrunk#3643), [#3651](max-sixty/worktrunk#3651))

- **Shell completion answers a Tab press in roughly half the time**: Completion forks a whole `wt` process on every Tab press, with the user's finger still on the key, and it has nowhere to hide work — nothing paints progressively, no cache survives the process, and no shell caches a dynamic completion. It was doing every git read in sequence. The reads now overlap, the repository caches prewarm on this path, `refs/remotes/` is not scanned where a remote-only branch can never be offered (`wt remove` and worktree-only arguments), and the handler returns before `main` builds a rayon pool it never reaches. `wt switch <Tab>` goes from 45.7 ms to 27.4 ms on a fixture of 80 worktrees and 1400 remote-tracking refs; on worktrunk's own checkout, 47.9 ms to 29.0 ms, and `wt <Tab>` from 22.2 ms to 12.8 ms. ([#3664](max-sixty/worktrunk#3664))

- **`wt step prune` prunes stale metadata alongside its other removals**: Stale-detached and not-yet-planned prunable candidates were held on the scan lock's write side because the repo-wide `git worktree prune` they used to call raced sibling prunes. Each call now names its own entry, so both shapes join the read-side fan-out; the write side keeps only the candidates that run arbitrary commands or own the terminal — hook-bearing ones, `--foreground` ones, and the deferred current worktree. ([#3655](max-sixty/worktrunk#3655))

### Fixed

- **Removing one worktree no longer unregisters another**: Every stale-metadata cleanup in the removal chain ran a bare `git worktree prune`, which takes no path filter — it walks every entry in `.git/worktrees/` and unregisters each one whose directory it cannot find at that instant. A worktree that is merely absent right now (an unmounted volume, a dropped network mount, a half-finished `mv`) is indistinguishable from a deleted one, so removing worktree A also unregistered bystander B. B's commits and files survive, but its admin directory does not: the index, `ORIG_HEAD`, the per-worktree reflog, `refs/worktree/*` and `refs/bisect/*`, and any in-progress rebase or merge go with it — and `git worktree repair` can relink an admin directory but cannot recreate one. Each cleanup now names the entry it means to remove, so a removal's blast radius equals its intent. ([#3650](max-sixty/worktrunk#3650))

- **A locked worktree whose directory is missing is no longer removed**: The missing-directory fallback was tested before the lock guard, so a worktree that was both locked and currently absent skipped the lock entirely — its registration was pruned and it fell through to branch deletion, which is precisely the case `git worktree lock` exists for (removable media, a network mount, a dropped VPN). The lock is now honored whether or not the directory is present, and `--force` still does not override it, matching `git worktree remove`. Fixes [#3645](max-sixty/worktrunk#3645). ([#3647](max-sixty/worktrunk#3647))

- **A branch that gains a checkout just before deletion is retained**: Branch deletion read worktree topology from cache, so a worktree registered after that read — by a hook, or a concurrent `wt switch` — was invisible, and the ref was deleted out from under it, leaving that worktree unable to resolve `HEAD`. A fresh topology read now sits immediately before the compare-and-swap delete, and a branch found checked out (live or locked) is kept and reported. Git exposes no transaction spanning worktree registration and ref updates, so the window cannot be closed entirely; it is now as narrow as two adjacent commands allow, and either one failing leaves the branch intact. ([#3662](max-sixty/worktrunk#3662))

- **Planning a removal no longer prunes metadata as a side effect**: `prepare_worktree_removal` pruned stale worktree metadata while planning the missing-directory fallback, so every "validate first" path quietly mutated — a refused `wt remove` of the default branch still pruned its entry, `wt remove` pruned before its approval prompt, and the picker pruned on skim's event loop during row validation. Planning is now a pure read, and the prune happens at execution, ahead of the branch deletion that may still be declined. Two related contracts move with it: a failing metadata prune surfaces as a removal failure and abandons the rest of `wt step prune`'s queue instead of being skipped silently, and `--dry-run` plans stale entries through the same gate as every other candidate, so a dirty prunable-but-present entry is correctly left out of the preview. ([#3658](max-sixty/worktrunk#3658))

- **`wt config shell install` previews and confirms the legacy files it reclaims**: Install reclaims its own legacy wrapper paths — the fish `conf.d/{cmd}.fish` and stranded nushell autoload copies — but the deletion never appeared in anything the user saw before it happened. `--dry-run` returned an empty cleanup list, the `?` preview showed only the files to add, and when everything was already configured the cleanup ran and returned without prompting at all. Both previews now render `Will remove <old> (deprecated; now using <new>)`, and the already-configured branch prompts before removing and preserves the file on decline. `--yes` still skips the prompt, so no non-interactive flow changes. Fixes [#3644](max-sixty/worktrunk#3644). ([#3648](max-sixty/worktrunk#3648))

- **Completion offers remote branches in a stable order**: They were grouped through a `HashMap` and sorted by timestamp with a stable sort, so on tied timestamps the order was whatever that process's hashing produced — three consecutive completions could print three different orders. Ties now break on name. ([#3664](max-sixty/worktrunk#3664

See merge request: Harmonybrew/homebrew-core!15534
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automated-fix Automated CI fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wt config shell install deletes legacy wrapper files without previewing them

2 participants