Skip to content

fix(worktree): reuse an existing group branch on resume instead of recreating it#82

Merged
OGtwelve merged 2 commits into
mainfrom
fix/worktree-reuse-branch-77
Jun 30, 2026
Merged

fix(worktree): reuse an existing group branch on resume instead of recreating it#82
OGtwelve merged 2 commits into
mainfrom
fix/worktree-reuse-branch-77

Conversation

@OGtwelve

@OGtwelve OGtwelve commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

Summary

WorktreePool.acquire always ran git worktree add -b <branch> <base>, which fails outright when the group branch already exists (a resumed run) — or, if recreated from base, drops the prior commits. It now checks out an existing branch instead, preserving committed-but-unpushed work and fixing the resume crash. A brand-new group still branches from base.

Changes

  • src/workspace/worktree-pool.tsbranchExists() helper; acquire reuses git worktree add <path> <branch> when the branch exists, else -b <branch> <base>.

Testing

  • src/workspace/worktree-pool.test.ts: integration test creating a branch with a commit, removing its worktree, then acquiring — asserts the prior commit/file is checked out. Existing fresh-branch path unchanged. Full suite green (672); biome + tsc clean.

Note

Auto-opening a PR for the recovered work when a resumed task then blocks is a smaller follow-up.

Refs #77

Summary by CodeRabbit

  • Bug Fixes
    • Resuming a workspace now reuses an existing branch when available, instead of trying to create a duplicate one.
    • Previously committed changes in a removed temporary workspace are now preserved and restored on the next checkout.
    • This helps resumed runs continue from the last saved state without losing work or failing during setup.

…creating it

WorktreePool.acquire now checks out an existing group branch (git worktree add <path> <branch>) instead of always branching from base (-b <branch> <base>). On a resumed run whose prior worktree was removed, the branch ref — and its committed-but-unpushed work — persists; the old '-b from base' would fail outright on the existing branch (crashing resume) or, if recreated, drop the prior commits. A brand-new group still branches from base.

This eliminates the resume crash and the loss of committed-but-unpushed work. Auto-opening a PR for that recovered work when a resumed task then blocks remains a smaller follow-up.

Refs #77
@coderabbitai

coderabbitai Bot commented Jun 30, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

You’ve reached a temporary PR review limit under our Fair Usage Limits Policy.

Your recent review volume is higher than typical usage, so adaptive limits are currently applied.

Next review available in: 42 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: a9f0f9ab-bc2c-4dc2-a766-13aad3549f79

📥 Commits

Reviewing files that changed from the base of the PR and between 607c267 and d862c98.

📒 Files selected for processing (2)
  • packages/aitm/src/workspace/worktree-pool.test.ts
  • packages/aitm/src/workspace/worktree-pool.ts
📝 Walkthrough

Walkthrough

WorktreePool.acquire() gains a branchExists helper that checks refs/heads/<branch> via git rev-parse --verify --quiet. When the ref exists, acquire() reuses it with git worktree add <path> <branch>; otherwise it creates a fresh branch with -b. A new test validates the resume scenario end-to-end.

Changes

WorktreePool Branch Reuse on Resume

Layer / File(s) Summary
branchExists helper and acquire() conditional logic
packages/aitm/src/workspace/worktree-pool.ts
Adds branchExists(repoRoot, branch) that runs git rev-parse --verify --quiet refs/heads/<branch>. Updates acquire() to use the reuse form of git worktree add when the branch ref exists, or the create-and-track form otherwise.
Resume mode integration test
packages/aitm/src/workspace/worktree-pool.test.ts
Imports readFile/writeFile and adds a test that seeds a prior commit on a group branch, removes the worktree, reacquires via WorktreePool.acquire, and asserts the prior file content and commit message are present.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related issues

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately captures the main change: reusing an existing group branch during resume instead of recreating it.
Docstring Coverage ✅ Passed Docstring coverage is 50.00% which is sufficient. The required threshold is 30.00%.
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 unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/worktree-reuse-branch-77

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 `@packages/aitm/src/workspace/worktree-pool.ts`:
- Around line 35-38: The branchExists() helper is swallowing all git rev-parse
failures and treating them as a missing branch, which can send acquire() down
the wrong git worktree add path. Update branchExists in worktree-pool.ts to
catch only the specific missing-ref case from runGit(['rev-parse', '--verify',
'--quiet', ...]) and rethrow any other repo, permission, or runtime errors so
callers can distinguish real failures from an absent branch.
🪄 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.yml

Review profile: CHILL

Plan: Pro

Run ID: 0b149be9-d472-47a8-b073-292d9519816d

📥 Commits

Reviewing files that changed from the base of the PR and between 7140325 and 607c267.

📒 Files selected for processing (2)
  • packages/aitm/src/workspace/worktree-pool.test.ts
  • packages/aitm/src/workspace/worktree-pool.ts

Comment thread packages/aitm/src/workspace/worktree-pool.ts Outdated
…xists

Per review: branchExists swallowed every git rev-parse failure as a missing branch, so a real error (not a repo, permissions, git missing) would silently send acquire down the new-branch path. It now returns false only for the rev-parse --verify --quiet exit-1 'ref absent' case and rethrows anything else.
@OGtwelve

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jun 30, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@OGtwelve OGtwelve merged commit 1487bae into main Jun 30, 2026
3 checks passed
@OGtwelve OGtwelve deleted the fix/worktree-reuse-branch-77 branch June 30, 2026 03:43
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