Skip to content

fix(worktree): allow reusing an existing branch for a new session#3262

Open
terrytangyuan wants to merge 1 commit into
omnigent-ai:mainfrom
terrytangyuan:feat/reuse-branch
Open

fix(worktree): allow reusing an existing branch for a new session#3262
terrytangyuan wants to merge 1 commit into
omnigent-ai:mainfrom
terrytangyuan:feat/reuse-branch

Conversation

@terrytangyuan

Copy link
Copy Markdown
Member

Related issue

N/A

Summary

The worktree pre-check in create_worktree() rejected any branch that already existed, even when no worktree had it checked out. This blocked users from starting a new session on a branch from a previous (ended) session — they had to pick a different name every time.

This narrows the check: only reject when the branch is actively checked out in another worktree (which would cause two sessions to clobber each other). When the branch exists but isn't checked out anywhere, reuse it with git worktree add <path> <branch> (no -b).

Test Plan

  • python -m pytest tests/host/test_git_worktree.py -v — all 35 tests pass.
  • Key new/updated tests:
    • test_create_worktree_duplicate_branch_checked_out_fails — still rejects when branch is checked out in another worktree.
    • test_create_worktree_existing_branch_no_worktree_reuses — a branch with no worktree is reused successfully.
    • test_create_worktree_reuses_after_worktree_removed — covers the exact user scenario (create → remove worktree keeping branch → create again on same branch).

Demo

N/A

Type of change

  • Bug fix
  • Feature
  • UI / frontend change
  • Refactor / chore
  • Docs
  • Test / CI
  • Breaking change

Test coverage

  • Unit tests added / updated
  • Integration tests added / updated
  • E2E tests added / updated
  • Manual verification completed
  • Existing tests cover this change
  • Not applicable

Coverage notes

Changelog

Starting a new session on a branch from a previous session no longer fails with "branch already exists"

The worktree pre-check rejected any branch that already existed, even
when no worktree had it checked out. This blocked users from starting a
new session on a branch from a previous (ended) session. Narrow the
check to only reject branches actively checked out in another worktree.

Signed-off-by: Yuan Tang <terrytangyuan@gmail.com>
Copilot AI review requested due to automatic review settings July 25, 2026 02:09
@github-actions github-actions Bot added the size/M Pull request size: M label Jul 25, 2026
@github-actions
github-actions Bot requested a review from fanzeyi July 25, 2026 02:10

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR adjusts the host-side git worktree creation logic to allow starting a new session on an already-existing branch as long as that branch is not currently checked out in any worktree, unblocking the “reuse branch after prior session ended” workflow.

Changes:

  • Add a worktree lookup helper to detect whether a branch is currently checked out in another worktree.
  • Update create_worktree() to reuse an existing branch (no -b) when safe, and error only when the branch is actively checked out elsewhere.
  • Update unit tests to cover the new reuse behavior and the still-invalid “already checked out” case.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
omnigent/host/git_worktree.py Reworks the branch pre-check to allow reusing existing branches when not checked out, and adds a helper to find where a branch is checked out.
tests/host/test_git_worktree.py Updates/extends tests to validate reuse of an existing branch and reuse after a worktree is removed.

Comment on lines 407 to 408
if base_branch is not None:
_ensure_base_resolvable(repo_root, base_branch)
@terrytangyuan

Copy link
Copy Markdown
Member Author

/review

@omnigent-ci

omnigent-ci Bot commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

Polly AI Review

Review: fix(worktree): allow reusing an existing branch for a new session

1. Blocking issues

None. The core logic is sound and correct:

  • _branch_checked_out_worktree() correctly parses list_worktrees() porcelain output to find whether the branch is live in any worktree, and reuse is only allowed when it is not — preserving the "two sessions can't share one working tree" invariant that the original pre-check protected.
  • The pre-check plus git's own refusal to worktree add an already-checked-out branch gives belt-and-suspenders protection against a TOCTOU race; a failed add still surfaces via _git_error.
  • Command-injection safety is preserved: in the reuse path branch_name is placed as the trailing positional rev, and validate_branch_name() already rejects leading -, so no --end-of-options guard is needed there.

2. Security vulnerabilities

None. No new attack surface. Branch name validation is unchanged and still gates both paths; the reused branch_name argument cannot be interpreted as a git flag. No secret handling, network, deserialization, or path-traversal concerns in this diff.

3. Non-blocking notes

  • base_branch is silently ignored on the reuse path. When the branch already exists, _ensure_base_resolvable(repo_root, base_branch) still runs (so an invalid base still raises), but the subsequent git worktree add <path> <branch> discards base_branch entirely — the worktree checks out the existing branch's tip, not a fresh branch off base_branch. This is the correct semantics for "reuse an existing branch," but a caller passing an explicit base_branch for an already-existing name gets no branch/rebase onto that base and no signal that their base was ignored. Consider documenting this precedence (existing branch wins over base_branch) in the docstring, and/or skipping the now-pointless _ensure_base_resolvable call when branch_exists.
  • Stale docstring. create_worktree's :raises WorktreeError: line still reads "…or git worktree add fails (e.g. the branch already exists)." That branch-already-exists case is now the intended reuse path, not a failure. Minor doc drift worth a one-line fix.

4. Summary

A tight, well-scoped bug fix that correctly narrows an overly-strict pre-check from "branch exists" to "branch is actively checked out elsewhere," which is the right invariant. The change is safe, preserves command-injection protections, and the three added/updated tests cover the key scenarios (checked-out rejection, plain-existing reuse, and the create→remove→recreate user flow). No blocking or security issues; only a minor semantic gap around ignored base_branch on reuse and a stale docstring, both non-blocking.


Automated review by Polly · workflow run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/M Pull request size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants