Desktop app: opening multiple git worktrees from the same repo replaces existing project
Description
When opening multiple git worktrees from the same repository in the desktop app, the app replaces the existing project instead of treating each worktree as a separate project.
Steps to Reproduce
- Create a git repository with multiple worktrees
- Open worktree A in the desktop app
- Open worktree B (from the same repo) in the desktop app
- Observe that worktree A's project is replaced with worktree B
Expected Behavior
Each worktree should be treated as a separate project, allowing users to have multiple worktrees from the same repository open simultaneously.
Actual Behavior
Opening a second worktree from the same repo replaces the first worktree's project data.
Root Cause
The project ID is derived from the git repository's root commit hash (git rev-list --max-parents=0 --all), which is identical across all worktrees of the same repository. Projects are stored/keyed by this ID, so all worktrees from the same repo collide.
Relevant code: packages/opencode/src/project/project.ts:57-70
Potential Fix Direction
The project key should incorporate the folder/worktree path to make each worktree unique, rather than relying solely on the git root commit hash.
Related
Selecting multiple folders when opening may also be affected by this same issue (needs further investigation).
This issue might be a duplicate of existing issues. Please check:
- #2445: git worktrees and root-directory - Same issue reported earlier about worktrees being incorrectly identified
- #4251: Concurrent sessions working on different repos interfere with each other - Related issue showing interference when working with multiple repo instances
- #1924: Git Branch-Aware Sessions - Related feature request for distinguishing between similar git contexts
- #564: feature request: git worktree integration - Original feature request for worktree support
Feel free to ignore if you've already considered these or if this specific case is materially different.
This is not a duplicate of #2445 or #564, though it is related.
Previous Issues (Fixed)
#2445 and #564 were about the CLI incorrectly resolving the working directory when running from a worktree. PR #2870 fixed this by using git rev-parse --show-toplevel to correctly identify the worktree path.
This Issue (Different Problem)
The worktree path is now correctly resolved, but the project ID is still derived solely from the root commit hash (git rev-list --max-parents=0 --all), which is identical across all worktrees of the same repository.
This causes a collision in the desktop app: when you open multiple worktrees from the same repo, they share the same project ID in storage, so opening worktree B overwrites worktree A's project data.
The CLI works fine because each instance independently resolves its worktree path at runtime. The desktop app persists project state by ID, which is where the collision occurs.