worktrunk icon indicating copy to clipboard operation
worktrunk copied to clipboard

prune misses squash-merged branches when merge-tree conflicts with current default branch

Open tthyer opened this issue 8 months ago • 5 comments

wt step prune fails to detect squash-merged branches when the files modified by the branch have also been modified on the default branch after the squash-merge landed.

To reproduce:

  • create a branch, modify some files, open a PR
  • squash-merge the PR, but don't clean up the worktree
  • after the merge, make more changes to those same files on the default branch
  • run wt step prune, and wt doesn't detect the branch was merged

wt list shows X for these branches, but wt step prune --dry-run reports "No merged worktrees to remove." This is because these sorts of conditions were encountered: the branch tips have changed, the default branch advanced, and there are now file conflicts.

In my environment:

  • worktrunk 0.29.1, installed through brew
  • git 2.49.0
  • macOS arm64

tthyer avatar Mar 30 '26 20:03 tthyer

Thanks for reporting this! I was able to reproduce the issue and identified the root cause: when git merge-tree --write-tree encounters conflicts (because both the branch and target modified the same files), integration detection conservatively treats the branch as not integrated — even though the branch's changes were already squash-merged.

I've opened #1820 with a fix that adds a patch-id matching fallback when merge-tree conflicts. It computes the branch's squashed patch-id and checks if any commit on the target has a matching patch-id, which correctly identifies squash merges even when the target later modified the same files.

This also fixes wt list — the ⊂ symbol will now appear for these branches instead of ✗.

A maintainer will review it shortly.

worktrunk-bot avatar Mar 30 '26 21:03 worktrunk-bot

Note: patch-id fallback has a known false positive with squash-merge-then-revert

The patch-id fallback (introduced in this branch) detects squash merges by checking if the branch's squashed diff matches any target commit's patch-id. This correctly handles the #1818 scenario (squash merge + target advances same files).

However, it has a false positive when the squash merge is reverted on the target:

  1. Branch changes file A
  2. Squash-merged to main
  3. Main reverts the squash merge (file A back to original)
  4. Main advances file A to something new
  5. merge-tree conflicts → fallback triggers
  6. The historical squash-merge commit's patch-id still matches → branch classified as "integrated"
  7. But the branch's content is not on main — it was reverted

The root cause: the algorithm answers "was this patch ever applied to target?" not "is this patch's effect still present?" The original squash-merge commit remains in git log output even after a revert.

The fix would be: after finding a matching commit C, verify that C's changes weren't reverted by checking that the changed files still differ from their pre-C state on the current target. This adds one diff-tree --name-only + N git show commands (N = files changed by C), only on the conflict path.

Without the fix, the revert scenario requires all of: (1) PR squash-merged, (2) squash merge reverted on main, (3) main later modifies the same files causing merge-tree conflicts, (4) user runs wt remove. Uncommon but realistic.

This was written by Claude Code on behalf of @max-sixty

max-sixty avatar Mar 31 '26 02:03 max-sixty

Good catch on the revert false positive — the patch-id fallback answers "was this patch ever applied?" rather than "is it still present?", which is the wrong question when the squash merge was reverted.

The suggested fix makes sense: after finding a matching commit C, verify C's effect is still present by checking the changed files haven't been restored to their pre-C state. Happy to implement this as a follow-up on #1820 (or a separate PR) — whichever you prefer.

worktrunk-bot avatar Mar 31 '26 02:03 worktrunk-bot

re the note from Claude Code above: it does seem like there's some small risk that by checking whether this was ever merged, we're not quite checking whether it's currently merged.

this also seems quite narrow and also recoverable — we're not losing code, only the current state of code.

any thoughts?

max-sixty avatar Mar 31 '26 02:03 max-sixty

Agreed — I think the current patch-id fallback is the right trade-off as-is, without adding revert detection. A few reasons:

  1. The false positive is benign. If it triggers, wt step prune removes a worktree whose branch content was already reverted on the target. The branch ref still exists (pruning removes the worktree, not the branch), and the commits are in the reflog. Nothing is lost that isn't recoverable.

  2. The scenario is very narrow. It requires squash-merge → revert → same-file changes → merge-tree conflicts — all on the same branch. In practice, if someone reverts a squash merge they're unlikely to still have the feature worktree sitting around.

  3. The fix adds real complexity. The post-match verification (diff-tree + git show per changed file) adds multiple git commands on a path that's already a fallback of a fallback, and introduces its own edge cases (partial reverts, cherry-picks of the revert, etc.).

If this ever becomes a real problem, it's straightforward to add the verification later. For now the patch-id fallback solves the reported issue without overengineering.

worktrunk-bot avatar Mar 31 '26 02:03 worktrunk-bot