Skip to content

fix(item): clean up the worktree on a plain release, not just done/check_merge - #388

Merged
getappz merged 2 commits into
masterfrom
task/335
Aug 4, 2026
Merged

getappz merged 2 commits into
masterfrom
task/335

Conversation

@getappz

@getappz getappz commented Aug 4, 2026 •

Copy link
Copy Markdown
Owner

Summary

Item #335 (git-shim/worktree lifecycle reliability). Two sub-issues were filed under this item:

  1. git worktree denying read-only ops (list, prune --dry-run) identically to mutating ones — already fixed in current classify_pure (the worktree match arm already special-cases read-only subcommands). No change needed here.
  2. claim done/release not reliably cleaning up worktrees — this PR's fix.

item_release only ever released the DB claim lease; it never touched the worktree directory at all. item_done (when it goes straight to completed) and item_check_merge (once a PR merges) were the only two paths that called cleanup_worktree — so any item abandoned via a plain release, or moved to completed by hand outside the done flow, orphaned its .worktrees/task/<id> forever. Confirmed live on this machine: 24 stale worktrees currently sitting under .worktrees/task/, several from items released without ever going through done.

Fix

Wire cleanup_worktree into item_release the same way item_done already does it: resolve the item + release the claim under the backend DB lock, then attempt cleanup outside it. cleanup_item_worktree already refuses a dirty tree (uncommitted changes), so this is safe to call unconditionally on every successful release.

Testing

  • Added item_release_removes_its_own_clean_worktree and item_release_leaves_a_dirty_worktree_in_place, mirroring the existing item_done coverage
  • cargo test --bin agentflare item_ — 81 passed, 0 failed, 1 ignored (unrelated manual benchmark)
  • cargo fmt --check clean
  • cargo clippy --workspace --all-targets --all-features -- -A unsafe_code -A clippy::pedantic -D warnings clean

Summary by CodeRabbit

  • Bug Fixes

    • Releasing an item now removes its clean claimed worktree.
    • Worktrees containing uncommitted changes are preserved during release.
  • Tests

    • Added coverage validating cleanup of clean worktrees and protection of modified worktrees.

…eck_merge

item_release only released the DB claim lease -- it never touched the
worktree at all. done (when it goes straight to completed) and
check_merge (once a PR merges) were the only paths that ever called
cleanup_worktree, so any item abandoned via a plain release, or moved
to completed by hand outside the done flow, orphaned its
.worktrees/task/<id> forever. Confirmed live: 24 stale worktrees
currently sitting under .worktrees/task/, several from items released
without going through done.

Wire cleanup_worktree into item_release the same way item_done already
does: DB read+release under the backend lock, cleanup attempt outside
it. cleanup_item_worktree already refuses a dirty tree, so this is safe
to call unconditionally on a successful release.

Add item_release_removes_its_own_clean_worktree and
item_release_leaves_a_dirty_worktree_in_place, mirroring the existing
item_done coverage.
@coderabbitai

coderabbitai Bot commented Aug 4, 2026 •

Copy link
Copy Markdown

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 725d5dc8-549a-4f97-a933-b4c2b3f2d15d

📥 Commits

Reviewing files that changed from the base of the PR and between 5a05b3c and 9b90337.

📒 Files selected for processing (1)
  • src/mcp_server/item.rs
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/mcp_server/item.rs

📝 Walkthrough

Walkthrough

item_release now retrieves the item, verifies claim ownership, cleans eligible worktrees, and then releases the claim. Tests cover clean worktree removal and preservation of worktrees with uncommitted changes.

Changes

Release Worktree Cleanup

Layer / File(s) Summary
Validate release and clean worktrees
src/mcp_server/item.rs, src/mcp_server/tests/action_tests.rs
item_release verifies ownership and retrieves the item before cleanup. It removes clean claimed worktrees before releasing the claim and preserves worktrees with uncommitted changes. Integration tests cover both cases.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the primary change: cleaning up worktrees during plain item release.
Description check ✅ Passed The description explains the problem, fix, tests, and validation results, although it omits the template's Notes for reviewers section.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch task/335

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/mcp_server/item.rs`:
- Around line 515-529: The item_release flow currently releases the claim before
cleanup, allowing a concurrent item_claim to reuse and lose the worktree. Update
item_release and the surrounding claim/cleanup logic so cleanup_worktree runs
while the claim remains held, then release the claim afterward; add a concurrent
test covering this ordering and preventing the claimed worktree from being
removed.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 2f984065-808f-4712-9fe2-86cb4c7c0bbd

📥 Commits

Reviewing files that changed from the base of the PR and between 9a83c4c and 5a05b3c.

📒 Files selected for processing (2)
  • src/mcp_server/item.rs
  • src/mcp_server/tests/action_tests.rs

Comment thread src/mcp_server/item.rs Outdated
Addresses CodeRabbit review on PR #388: releasing the claim before
cleanup_worktree opened a window where a concurrent claim could grab
the item and start working in the worktree just before cleanup
deleted it. Check ownership first, clean up, then release last --
same order item_done's no-PR path already uses.
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.

1 participant