Repository navigation
Description
Summary
In the OpenCode desktop app, a session created for a secondary git worktree runs in the repository's main worktree instead. This happens even when the project itself was added from the worktree directory and the same worktree is selected again when creating the session. Every shell command, file edit and git operation in that session therefore runs against the wrong checkout.
Environment
- opencode version: 2.1.2 (OpenCode desktop app, bundled CLI 2.1.2)
- OS: macOS (Darwin 27.0.0, arm64)
- Terminal: desktop app (Electron 44.9.1) — no terminal involved
- Shell: /bin/zsh
- Install/channel: latest (desktop app,
opencode.aiupdate channel) - Active plugins:
opencode-antigravity-auth@latest+ one local internal plugin (path redacted). Not re-tested with plugins disabled.
Reproduction
- Create a repo that has a second worktree:
git worktree add ../repo-feature - In the desktop app, Add project and select the worktree directory
/<home>/work/repo-featureitself. - Create a new session and select that same worktree directory
/<home>/work/repo-featureas the session location. - Send a prompt such as
what is your current working directory and branch? - The assistant answers with the main worktree (
/<home>/work/repo) and its branch;git rev-parse --show-toplevelreturns/<home>/work/repo.
Expected Behavior
The session runs at /<home>/work/repo-feature: the assistant's working directory and branch are those of the worktree, git rev-parse --show-toplevel returns /<home>/work/repo-feature, and the session list attributes the session to the worktree.
Actual Behavior
The session runs at /<home>/work/repo (main worktree). The assistant reports the main worktree path and branch, git rev-parse --show-toplevel returns /<home>/work/repo, and the session list shows the main worktree. Reproduces consistently on this machine, every time.
Additional Context
The worktree was registered as the project itself and was selected again when creating the session, so the client clearly has /<home>/work/repo-feature on hand — the worktree path is not missing, it is replaced by the main worktree somewhere between selection and session creation.
Server-side evidence from ~/.local/share/opencode/log/opencode.log (paths redacted). The previously active session was working in the worktree; for the new-session flow, every git probe the client triggered ran in the main worktree, and the created session carries the main worktree as its location:
# last git activity of the previous session — correct worktree
... command=git args=[...rev-parse --verify HEAD] cwd=/<home>/work/repo-feature
... command=git args=[...status --porcelain=v1 ...] cwd=/<home>/work/repo-feature
# new-session flow: branch + worktree enumeration, and the session it created — all in the main worktree
... command=git args=["for-each-ref" ...] cwd=/<home>/work/repo
... command=git args=["rev-parse","--git-dir","--git-common-dir","--show-toplevel"] cwd=/<home>/work/repo
... command=git args=["worktree","list","--porcelain"] cwd=/<home>/work/repo
The created session record reports location.directory = /<home>/work/repo, not the worktree.
This is not the "no location → server default" fallback. GET /api/location without a x-opencode-directory header returns the home directory, so a session that failed to pass a location would land there, not in the main worktree. The client explicitly resolved the main worktree here.
Suspected cause — worktree selection collapsing onto the project's canonical directory. GET /api/location for the two directories returns:
// header: x-opencode-directory: /<home>/work/repo
{ "directory": "/<home>/work/repo",
"project": { "id": "<redacted>", "directory": "/<home>/work/repo", "canonical": "/<home>/work/repo" } }
// header: x-opencode-directory: /<home>/work/repo-feature
{ "directory": "/<home>/work/repo-feature",
"project": { "id": "<redacted, same project>", "directory": "/<home>/work/repo-feature", "canonical": "/<home>/work/repo" } }Both worktrees resolve to the same project ID, and that project's canonical is the main worktree. So any session-creation path that sends project.canonical (or project.directory when it is not re-resolved) instead of the resolved location.directory will silently put the session in the main worktree. The server itself accepts the worktree directory correctly, so the worktree selection is being dropped client-side before session.create is called.
Workaround: after the session is created, move it to the worktree with the move-session action (this build exposes it as the session_move tool). It has to be repeated for every new session.
Activity
opencode-agent commentedon Oct 21, 2026
Thanks for the detailed report and the server-side evidence. I think this is a duplicate of #51992. It reports the same thing: in the desktop app, a new session created with a git worktree selected is bound to the project's canonical (main worktree) directory instead of the worktree, while the server handles the worktree location correctly when it is sent directly. That issue is still open, so it makes sense that it still happens on 2.1.2.
Possible duplicates and closely related issues, ordered by likelihood:
If your evidence adds anything new, a comment on #51992 would help, for example confirming it still happens on 2.1.2 when the project itself was added from the worktree directory.
The bot will close this issue as a duplicate in 1 day unless you reply explaining how your problem is different.
opencode-agent commentedon Oct 22, 2026
Closing this as a duplicate of #51992, since there's been no reply saying how it differs. That issue covers the same bug: when you create a new session in the desktop app with a git worktree selected, it's bound to the main worktree instead. #51992 is still open, and PRs #52094 and #57358 address it. Please follow it there. If you can add that it still happens on 2.1.2 even when the project was added from the worktree directory, that would help.
If your problem turns out to be different, reply here and we can reopen this. Thanks again for the detailed report.