Skip to content

perf(list): skip redundant MergeTreeConflicts on dirty worktrees - #2602

Merged
max-sixty merged 2 commits into
mainfrom
todos-3-skip-merge-tree
May 4, 2026
Merged

max-sixty merged 2 commits into
mainfrom
todos-3-skip-merge-tree

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Summary

  • For dirty worktrees, WorkingTreeConflicts already runs git merge-tree against a tree that includes HEAD + uncommitted changes; MergeTreeConflicts was duplicating that work against HEAD alone.
  • MergeTreeConflictsTask now peeks the shared porcelain cache and short-circuits with a sentinel false when the worktree is dirty without unmerged entries.
  • tier_would_conflict now treats Some(Some(_)) as authoritative — the dirty-tree probe wins regardless of the HEAD probe. This is the answer most users want for "would this merge conflict?" anyway, since the dirty tree reflects the current working state.

Implementation notes

  • The optimization fires only for worktree items that are dirty AND have no unmerged entries. Branch-only items, clean worktrees, and unmerged-state worktrees still run the HEAD probe normally.
  • One snapshot updated: test_list_warns_when_commit_details_batch_fails — the corrupt-HEAD repro now surfaces merge-tree-conflicts (fatal: bad object HEAD) from the early porcelain check instead of the deeper merge-base failed message. Same severity; same diagnostic value.
  • Behavior change in one edge case: when HEAD would conflict but uncommitted changes resolve it, the dirty probe now wins (rules out the conflict). The TODO this PR resolves explicitly proposed this trade-off.

Test plan

  • cargo run -- hook pre-merge --yes (full lints + 3484 tests) passes
  • Manually verified in a synthetic dirty-conflict repo via RUST_LOG=worktrunk=debug that exactly one merge-tree subprocess fires per dirty row (the WorkingTreeConflicts ephemeral-commit one), down from two
  • test_list_working_tree_conflicts and test_list_full_clean_working_tree_uses_commit_conflicts both pass — ✗ symbol still renders correctly for dirty conflicts; clean conflicts still detected via HEAD probe
  • Updated tier_would_conflict unit tests cover the new short-circuit and the "skipped HEAD probe" path

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

For dirty worktrees, WorkingTreeConflicts already runs `git merge-tree`
against a tree that includes HEAD plus uncommitted changes. The HEAD-only
MergeTreeConflicts probe was duplicating that work for the same row.

MergeTreeConflictsTask now peeks the shared porcelain cache and short-
circuits with a sentinel value when the worktree is dirty without
unmerged entries. tier_would_conflict treats Some(Some(_)) as
authoritative — the dirty-tree result reflects the user's current state
and is the right answer for "would conflict?" anyway.

Drops one merge-tree subprocess per dirty worktree row in `wt list`.

Co-Authored-By: Claude <[email protected]>

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Optimization is sound: both probes read from the shared status_porcelain_cached OnceCell, so they always agree on the dirty-without-unmerged predicate, and the unreachable!() arm in tier_would_conflict for (Some(_), Some(Some(_))) is genuinely unreachable in production. Docstrings clearly state the new authoritative-WT contract.

One small gap: the test suite verifies WT(Some(Some(false))) rules out alongside HEAD(None) and HEAD(Some(false)), but not alongside HEAD(Some(true)) — the case that most directly demonstrates the new "WT wins over HEAD" semantics. Worth pinning down in case a future refactor reorders the matches.

Comment thread src/commands/list/model/state.rs Resolved
@worktrunk-bot

Copy link
Copy Markdown
Collaborator

codecov/patch is failing on line 353 — the Some(Some(_)) => unreachable!() arm. The arm is genuinely unreachable in production (the first match returns early on Some(Some(_))), so it can't be exercised by a test without contriving an unsafe path.

A single tuple match makes the function exhaustive without any unreachable!() and removes the codecov gap. Sketch (untested — the author should verify against the existing tier_would_conflict tests):

pub fn tier_would_conflict(
    has_merge_tree_conflicts: Option<bool>,
    has_working_tree_conflicts: Option<Option<bool>>,
) -> Tier<MainState> {
    use Tier::*;
    match (has_merge_tree_conflicts, has_working_tree_conflicts) {
        // WT probe is authoritative when it has a dirty-tree result.
        (_, Some(Some(true))) => Fired(MainState::WouldConflict),
        (_, Some(Some(false))) => RuledOut,
        // Otherwise consult HEAD probe.
        (Some(true), _) => Fired(MainState::WouldConflict),
        (Some(false), Some(None)) => RuledOut,
        // HEAD says "no conflict" but WT hasn't reported — wait.
        (Some(false), None) => Wait,
        (None, _) => Wait,
    }
}

Happy to push this if you want — let me know.

Removes the `unreachable!()` arm (which codecov/patch correctly flagged as
uncovered — it's unreachable in production but contributes a coverage gap)
by making the predicate exhaustive over `(merge_tree, working_tree)` in a
single match. Also pins the `(Some(true), Some(Some(false)))` case where
the WT probe overrides a positive HEAD probe — the strongest demonstration
of WT-authoritative behavior, and the case that would silently regress if
someone reordered the arms.

Co-Authored-By: Claude <[email protected]>
@max-sixty

Copy link
Copy Markdown
Owner Author

Pushed cb507e25c:

  • Refactored tier_would_conflict to a single tuple match — exhaustive without unreachable!(), closes the codecov/patch gap.
  • Added the (Some(true), Some(Some(false))) test pinning WT-overrides-positive-HEAD as the strongest demonstration of the new authoritative-WT contract.

Walked all ten input shapes against both implementations to confirm semantic equivalence. wt hook pre-merge --yes green locally (3484/3484, no snapshot drift).

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

@max-sixty
max-sixty merged commit 8ce2c26 into main May 4, 2026
27 checks passed
@max-sixty
max-sixty deleted the todos-3-skip-merge-tree branch May 4, 2026 22:24
@max-sixty max-sixty mentioned this pull request May 5, 2026
2 of 3 tasks
max-sixty added a commit that referenced this pull request May 5, 2026
## Summary
Release v0.48.0. See
[CHANGELOG.md](https://github.com/max-sixty/worktrunk/blob/release/CHANGELOG.md)
for the full notes.

Highlights:
- `--format=json` extends to seven step + hook commands (#2560)
- `wt step commit` / `wt step squash` gain `--dry-run` (#2557)
- New `dirname` / `basename` template filters (#2592, #2605)
- New `[remove] delete-branch` config option (#2589)
- `wt-perf timeline` subcommand (#2558)
- Faster `wt list` on dirty worktrees (#2602) and faster alias dispatch
(#2556, #2573)
- Short-SHA display honors `core.abbrev` (#2576)
- Cleaner `wt config show` shell-integration section for new users

## Test plan
- [x] `cargo run -- hook pre-merge --yes` (3497 tests, lints clean)
- [x] `cargo semver-checks check-release -p worktrunk` consulted; minor
bump confirmed
- [ ] CI green
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants