Skip to content

fix(prune): serialize safe deletions and run hooks concurrently - #4337

Merged
max-sixty merged 7 commits into
mainfrom
codex/fix-prune-ref-deletion
Oct 3, 2026
Merged

max-sixty merged 7 commits into
mainfrom
codex/fix-prune-ref-deletion

Conversation

@max-sixty

@max-sixty max-sixty commented Oct 2, 2026 •

Copy link
Copy Markdown
Owner

Concurrent prune deletions can exhaust Git's packed-refs lock timeout. An unchanged branch was then reported as "moved during deletion", and removal could absorb the Git failure while reporting success. This change serializes safe ref deletions within the repository, reports movement only when the surviving SHA changed, and propagates other failures after arranging cleanup and teardown hooks. A missing snapshot SHA fails closed.

Each safe deletion uses one git update-ref -d with the original expected SHA. The repository mutex covers that mutation and its failure classification; interruptions cancel waiting deletions before they can mutate anything. Integration and checkout reads remain outside the mutex. Checkout protection remains best effort; Git's atomic expected-SHA comparison protects concurrent commits to the candidate branch. Worktree registry coordination independently keeps enumeration and teardown from overlapping.

Prune runs pre-remove hooks concurrently across worktrees. Each worktree's hook pipeline finishes before fresh ownership, lock, and cleanliness checks. Hooks coordinate shared writes themselves, avoid writing into other removal candidates, and can race Git configuration readers on Windows; these limits are documented in help and its generated mirrors. The command lock only coordinates terminal output for foreground/current removals. Trash cleanup starts before waiting for ref deletion, and current-worktree placeholder cleanup removes only an empty directory.

The ref deletion path has no request queue, batch transactions, or batch recovery. The coordinator drops from 256 production lines to 61. Three alternating pairs comparing the former batch path with the actual mutex implementation measured 540 → 645 ms for 80 packed orphan branches and 598 → 641 ms for 40 worktrees; all 12 samples verified complete cleanup. Machine load was high, so these fixture timings are limited evidence of the cost. The simpler implementation accepts that measured penalty.

Regression coverage exercises real packed-ref contention, original-SHA protection, per-ref error isolation, interruption of waiting deletions, cleanup while deletion is blocked, pre-hook rendezvous, and post-hook overlap. Existing tests cover checkout retention and foreground/background error teardown.

Validation: the full local pre-merge gate passes with 5,040 tests and one skipped, plus formatting, Clippy, documentation, and doctests. Generated docs and skill mirrors are in sync. Independent review found no remaining issues.

This was written by Codex on behalf of max-sixty

@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.

Holding this as a comment rather than an approval because it changes a check that gates a ref deletion. Commits stay protected: the CAS still rejects a branch that moved. The checked-out-branch guard is the one that loosens. Before this change, delete_branch_if_safe ran the fresh topology read immediately before update-ref -d. Its removed comment said that adjacency "minimizes that unavoidable TOCTOU window". Now the read happens first and the deletion then waits on the mutex. Every safe deletion queued ahead of it widens the window, so on a large packed prune the last branch's read can be several hundred ms stale. If a git worktree add <path> <branch> lands in that gap, the branch gets deleted anyway and the new worktree's HEAD points at a missing ref. Details are inline.

Comment thread src/git/remove.rs Resolved
Comment thread src/output/handlers.rs Resolved
@max-sixty max-sixty changed the title fix(prune): serialize ref deletions and surface Git failures fix(prune): batch safe deletions and narrow hook locking Oct 2, 2026

@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.

The batching queue turns a lock failure on one ref into a failure for every ref in its batch. Details are inline. This is still a COMMENT rather than an approval, for the reason in the earlier review: the change reshapes the path that gates ref deletion.

Comment thread src/git/ref_deletion.rs Resolved
@max-sixty max-sixty changed the title fix(prune): batch safe deletions and narrow hook locking fix(prune): batch safe deletions and run hooks concurrently Oct 2, 2026

@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.

Running pre-remove hooks concurrently reopens the Windows .git/config race from #2801. This time the race is with the scan's own readers rather than with branch deletion. The removed check_lock doc relied on hook exclusion for this: it said removal cores could run concurrently because they don't touch .git/config, and that "arbitrary pre-remove hooks retain write-side exclusion". After this change, check_one no longer takes any guard. A hook that runs git config, git branch -d, git remote remove or anything else that rewrites the common-dir config therefore overlaps the parallel integration checks. On Windows, a reader's fopen that lands on that rename fails. The live path then ?s the check error ("checking branch integration") and aborts the whole prune partway through. I haven't reproduced this on Windows. The mechanism is the same one #2801 documented, and test_prune_pre_remove_excludes_git_readers, which pinned the exclusion, is removed here. The new doc sentence asks hooks to coordinate shared writes, but a hook has no way to coordinate with worktrunk's own readers. One option is to keep hooks concurrent with each other while scans and readers hold off during a hook. Another is to accept the race and say so in the module doc.

This is still a COMMENT for the reason given in the first review.

Comment thread src/cli/step.rs Resolved
@max-sixty max-sixty changed the title fix(prune): batch safe deletions and run hooks concurrently fix(prune): serialize safe deletions and run hooks concurrently Oct 3, 2026
@max-sixty
max-sixty merged commit f4f1bbd into main Oct 3, 2026
38 checks passed
@max-sixty
max-sixty deleted the codex/fix-prune-ref-deletion branch October 3, 2026 01:55
max-sixty added a commit that referenced this pull request Oct 4, 2026
A `wt step prune` printed every per-item line quickly, then sat for
about 10s before its `Pruned …` summary. Two of the pruned worktrees
were Codex worktrees, which all live at `~/.codex/worktrees/<id>/<repo>`
and so share the basename `Mackup`. Removal staged each worktree at
`.git/wt/trash/<basename>-<epoch-second>`, so the two concurrent
removals picked the same path in the same second. The second rename
failed onto the existing directory, and prune's synchronous fallback ran
`git worktree remove` in the foreground; the summary waited for that
recursive delete. The run's logs show it: nine worktrees got a detached
`internal/remove.log`, the tenth (`codex/promptfoo-followups`) none.

Staging names now use the worktree's registration id under
`<common>/worktrees/`, which git keeps unique among a repository's
worktrees. `ensure_holds_this_worktree` already resolves and verifies
that git dir, so it now returns it. A failed rename also logs its error
at debug level instead of discarding it.

The collision predates #4337: v0.80.0 shows the same 3.4s stall on a
two-worktree repro (50k ignored files each), which drops to 0.17s with
this change. The new prune test relies on the suite's pinned
`epoch_now()`, which makes the collision deterministic under the old
naming; it fails without the fix.

> _This was written by Claude Code on behalf of max-sixty_

---------

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
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