Git worktrees + stash can cause projectID mismatch (sessions 404 across sandboxes) #8852

Open
opened 2026-02-16 18:11:01 -05:00 by yindo · 1 comment
Owner

Originally created by @shantur on GitHub (Feb 8, 2026).

Originally assigned to: @thdxr on GitHub.

Description

Issue

In git worktree setups, OpenCode can resolve different projectIDs for the repo root vs a worktree sandbox when scoping requests via X-OpenCode-Directory. Sessions are stored under storage/session/<projectID>/..., so the mismatch causes 404s when calling session APIs from the “other” directory.
Trigger: worktree .git is a file (so .git/opencode cache can’t be read) + fallback id computation uses git rev-list --max-parents=0 --all (includes refs/stash, which can introduce extra root commits).

Proposed Fix

  • Always read/write the cached project id in the common git dir:
    • Use git rev-parse --git-common-dir (resolve to absolute) and store cache at <gitCommonDir>/opencode so worktrees and repo root share the same projectID.
  • If fallback id computation is still needed, exclude stash:
    • Avoid --all; use --branches --tags or explicitly exclude refs/stash.

Plugins

No response

OpenCode version

No response

Steps to reproduce

How to Reproduce

  1. Have a repo with a worktree where .git is a file: gitdir: /path/repo/.git/worktrees/<name>.
  2. Ensure git rev-list --max-parents=0 --all returns >1 root and one maps to stash (git name-rev --name-only <root> => stash^...).
  3. Compare:
  • GET /project/current with X-OpenCode-Directory: /path/repo => id = A
  • GET /project/current with X-OpenCode-Directory: /path/repo/.../worktrees/<name> => id = B (B != A)
  1. Create a session under A, then GET /session/<id> under B => 404.

How to Verify

  • GET /project/current returns different id values for root vs worktree.
  • Session API 404 includes attempted path storage/session/<projectID>/<sessionID>.json.
  • git rev-list --max-parents=0 --all shows multiple roots; one is reachable from refs/stash.

Screenshot and/or share link

No response

Operating System

No response

Terminal

No response

Originally created by @shantur on GitHub (Feb 8, 2026). Originally assigned to: @thdxr on GitHub. ### Description ## Issue In git worktree setups, OpenCode can resolve different `projectID`s for the repo root vs a worktree sandbox when scoping requests via `X-OpenCode-Directory`. Sessions are stored under `storage/session/<projectID>/...`, so the mismatch causes 404s when calling session APIs from the “other” directory. Trigger: worktree `.git` is a file (so `.git/opencode` cache can’t be read) + fallback id computation uses `git rev-list --max-parents=0 --all` (includes `refs/stash`, which can introduce extra root commits). ## Proposed Fix - Always read/write the cached project id in the common git dir: - Use `git rev-parse --git-common-dir` (resolve to absolute) and store cache at `<gitCommonDir>/opencode` so worktrees and repo root share the same `projectID`. - If fallback id computation is still needed, exclude stash: - Avoid `--all`; use `--branches --tags` or explicitly exclude `refs/stash`. ### Plugins _No response_ ### OpenCode version _No response_ ### Steps to reproduce ## How to Reproduce 1) Have a repo with a worktree where `.git` is a file: `gitdir: /path/repo/.git/worktrees/<name>`. 2) Ensure `git rev-list --max-parents=0 --all` returns >1 root and one maps to stash (`git name-rev --name-only <root>` => `stash^...`). 3) Compare: - `GET /project/current` with `X-OpenCode-Directory: /path/repo` => `id = A` - `GET /project/current` with `X-OpenCode-Directory: /path/repo/.../worktrees/<name>` => `id = B` (B != A) 4) Create a session under A, then `GET /session/<id>` under B => 404. ## How to Verify - `GET /project/current` returns different `id` values for root vs worktree. - Session API 404 includes attempted path `storage/session/<projectID>/<sessionID>.json`. - `git rev-list --max-parents=0 --all` shows multiple roots; one is reachable from `refs/stash`. ### Screenshot and/or share link _No response_ ### Operating System _No response_ ### Terminal _No response_
yindo added the bug label 2026-02-16 18:11:01 -05:00
Author
Owner

@github-actions[bot] commented on GitHub (Feb 8, 2026):

This issue might be a duplicate of or closely related to existing issues. Please check:

  • #5638: Desktop app: opening multiple git worktrees from the same repo replaces existing project (same root cause - projectID derived from git root commit hash)
  • #6696: Project ID collision when repos cloned from same source share sessions (same root cause - projectID collision)
  • #10643: Worktrees created from bare repos are not recognized (related worktree path resolution issue)

These issues all stem from the fundamental problem that projectID is based solely on git root commit hash without accounting for different checkout paths or worktrees. Your proposed fix to use git rev-parse --git-common-dir and exclude stash refs aligns with the solutions being discussed for #5638 and #6696.

Feel free to ignore if your specific case differs from these issues.

@github-actions[bot] commented on GitHub (Feb 8, 2026): This issue might be a duplicate of or closely related to existing issues. Please check: - #5638: Desktop app: opening multiple git worktrees from the same repo replaces existing project (same root cause - projectID derived from git root commit hash) - #6696: Project ID collision when repos cloned from same source share sessions (same root cause - projectID collision) - #10643: Worktrees created from bare repos are not recognized (related worktree path resolution issue) These issues all stem from the fundamental problem that projectID is based solely on git root commit hash without accounting for different checkout paths or worktrees. Your proposed fix to use `git rev-parse --git-common-dir` and exclude stash refs aligns with the solutions being discussed for #5638 and #6696. Feel free to ignore if your specific case differs from these issues.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#8852