Skip to content

projects: worktree discovery cross-registers sibling worktree dirs into other existing projects; affected sessions open title-only #60847

Open

Description

@lwr

Description

Summary

Two independent projects whose root directories are two git worktrees of the same bare repo contaminate each other: worktree discovery registers each project's sibling worktree directory (and the bare repo path) into the other, already existing project. Both projects' session lists then show each other's sessions, and opening a session from the wrong project context renders only its title with no messages. The underlying data is fully intact (PRAGMA integrity_check = ok, all message rows present).

Environment

  • opencode version: 2.2.2 (desktop app /Applications/OpenCode.app, background service; the affected sessions date back to app version 1.18.18 and were migrated forward)
  • OS: Darwin 27.2.0 (darwin arm64), macOS, case-insensitive APFS
  • Terminal: Desktop app (OpenCode.app 2.2.2)
  • Shell: /bin/bash
  • Install/channel: latest
  • Active plugins: none

Expected Behavior

  • A directory that is already the root of an existing project must not be silently registered as a worktree directory of another project (or, alternatively, the whole repo family should consistently map to exactly one project).
  • A session listed in a project should render its messages when opened, or not be listed there at all.
  • A bare repo path whose discovery fails (Worktree.DirectoryUnavailableError — expected for bare repos) probably should not be registered as a project directory at all.

Actual Behavior

  • project_directory cross-registration between two pre-existing projects, re-created on every discovery pass. Manually deleting the wrong rows only helps until the next discovery.
  • Session opened from the wrong project context: title only, zero messages rendered — although session_message holds 63 intact rows (53 assistant / 9 user / 1 system) and PRAGMA integrity_check returns ok.
  • Log floods with 61 × WARN worktree discovery failed ... Worktree.DirectoryUnavailableError for the bare repo path on every discovery pass.

Evidence (redacted, read-only queries against ~/.local/share/opencode/opencode.db):

project_directory rows after discovery (project B root = ~/work/B, project A root = ~/work/A):

project_id  directory                          type
13a2…(B)    /Users/<user>/MyWORK/<org>/B       main
13a2…(B)    /Users/<user>/cm_java/BARE.git
13a2…(B)    /Users/<user>/MyWORK/<org>/A       git_worktree   <-- other project's root
13a2…(B)    /Users/<user>/MyWork/<org>/B       git_worktree   <-- case-variant duplicate of its own root
92c2…(A)    /Users/<user>/MyWORK/<org>/A       git_worktree   (original, correct row)
92c2…(A)    /Users/<user>/cm_java/BARE.git
92c2…(A)    /Users/<user>/MyWork/<org>/B       git_worktree

Note: git records a case-variant worktree path (…/MyWork/… while the actual directory is …/MyWORK/…) on case-insensitive APFS; both variants end up registered as separate directories.

Intact data for the "title only" session:

session_v2:      id=ses_116f2ad9effeuXvpDOnfzslXQL, project_id=92c2…(A),
                 directory=/Users/<user>/MyWORK/<org>/A, created-by-version=1.18.18
session_message: 63 rows (53 assistant, 9 user, 1 system), data JSON fully intact
session_part:    0 rows for ALL sessions on this install (parts are inline in
                 session_message.data), for reference

Relevant log lines (redacted):

timestamp=2026-10-31T08:04:31.952Z level=INFO message="spawning process"
  command=/opt/homebrew/bin/git args=["worktree","list","--porcelain"]
  cwd=/Users/<user>/MyWORK/<org>/B role=server
timestamp=2026-10-31T08:05:24.036Z level=WARN message="worktree discovery failed"
  directory=/Users/<user>/cm_java/BARE.git error=Worktree.DirectoryUnavailableError role=server

Additional Context

  • Workaround attempts that did NOT help: configuring different remote aliases/remotes for the two worktrees. Discovery only follows the local .git gitdir pointer (git worktree list), so remote configuration is irrelevant.
  • Manual cleanup of the wrong project_directory rows un-mixes the session lists temporarily; the next discovery pass re-adds them.
  • The DB file is large (~1.6 GB) but healthy; this is not corruption.
  • Real-world setup where this occurred: two long-lived projects developed as sibling worktrees of one shared bare repo, running since app version ~1.18, now on 2.2.2.

Plugins

none

OpenCode version

2.2.2 (desktop)

Steps to reproduce

  1. Create one bare repo with two worktrees in separate directories:
    git init --bare ~/repos/BARE.git
    git -C ~/repos/BARE.git worktree add ~/work/A main
    git -C ~/repos/BARE.git worktree add ~/work/B dev
  2. Open ~/work/A in OpenCode → project A is created with root ~/work/A. Create some sessions.
  3. Open ~/work/B → project B is created with root ~/work/B.
  4. Trigger worktree discovery again (reopen either project, or restart the service). The server runs git worktree list --porcelain from the project directory, which lists ALL worktrees of the repo.
  5. Inspect project_directory in ~/.local/share/opencode/opencode.db: project B now also contains ~/work/A (type git_worktree) and the bare repo path; project A likewise gained ~/work/B and the bare path.
  6. Both projects' session lists now show each other's sessions.
  7. Open a session that belongs to project A from project B's session list: only the title renders; no messages load.

Screenshot

No response

Operating System

Darwin 27.2.0 (darwin arm64), macOS

Terminal

iTerm2

Activity

  1. opencodejr commented on Oct 31, 2026

    @opencodejr
    Contributor

    Thanks for the detailed report. I reproduced the cross-registration on 2.2.2 through the server API, and it still happens on the latest dev (45920000).

    What I did (the script below runs it):

    1. Created a bare repo with two worktrees, A and B.
    2. Opened A, then added an https remote and opened B. That gave two separate projects.
    3. On a fresh install both projects get the bare repo as their root. To match your migrated setup, I set their roots to A and B in the database.
    4. Ran a worktree refresh for each project.

    After the refresh, project A's worktree list contains B (project B's root), and project B's contains A. Both also contain BARE.git, and every refresh of it logs worktree discovery failed … Worktree.DirectoryUnavailableError. As you describe, the rows come back on each refresh. On this version they are stored in the worktree table.

    I didn't check the desktop symptoms: the shared session lists and sessions opening with only a title. I also couldn't reproduce the duplicate path that differs only in capitals, since that needs a case-insensitive filesystem like APFS.

    Related: #59371 (sessions split across several project rows for one worktree), #58108 (sibling worktrees showing up in the session picker) and #57989 (the same DirectoryUnavailableError log spam, for non-git folders).

    Reproduction script (repro.sh)
    #!/usr/bin/env bash
    # Repro for #60847: worktree discovery registers sibling worktrees (and the bare repo)
    # into another, separate project.
    # Usage: ./repro.sh            -> uses `opencode` from PATH
    #        OC_DEV=/path/to/repo ./repro.sh -> runs from source checkout
    set -u
    ROOT=/tmp/opencode/r60847-$$
    PORT=${PORT:-$((40000 + RANDOM % 20000))}
    mkdir -p "$ROOT"
    export XDG_DATA_HOME=$ROOT/data XDG_CONFIG_HOME=$ROOT/config XDG_STATE_HOME=$ROOT/state XDG_CACHE_HOME=$ROOT/cache
    export OPENCODE_PASSWORD=repro
    export GIT_AUTHOR_NAME=t GIT_AUTHOR_EMAIL=t@t GIT_COMMITTER_NAME=t GIT_COMMITTER_EMAIL=t@t
    
    # 1. one bare repo with two worktrees
    git init -q -b main "$ROOT/src" && git -C "$ROOT/src" commit -q --allow-empty -m init
    git clone -q --bare "$ROOT/src" "$ROOT/BARE.git" && git -C "$ROOT/BARE.git" remote remove origin
    git -C "$ROOT/BARE.git" worktree add -q "$ROOT/A" main
    git -C "$ROOT/BARE.git" worktree add -q -b dev "$ROOT/B" main
    
    DB=$XDG_DATA_HOME/opencode/opencode.db
    if [ -n "${OC_DEV:-}" ]; then
      DB=$XDG_DATA_HOME/opencode/opencode-local.db
      OC=(bun --conditions=browser --preload=$OC_DEV/packages/tui/node_modules/@opentui/solid/scripts/preload.js $OC_DEV/packages/cli/src/index.ts)
    else
      OC=(opencode)
    fi
    (cd "$ROOT" && exec "${OC[@]}" serve --port $PORT >"$ROOT/serve.log" 2>&1) &
    SERVER=$!
    for i in $(seq 1 60); do curl -s -u opencode:reprof "http://127.0.0.1:$PORT/api/location" >/dev/null && break; sleep 1; done
    
    loc() { curl -s -u opencode:repro "http://127.0.0.1:$PORT/api/location?location%5Bdirectory%5D=$1"; }
    pid() { python3 -c 'import json,sys;print(json.load(sys.stdin)["project"]["id"])'; }
    
    # 2. open A -> project identified by root commit
    PA=$(loc "$ROOT/A" | pid)
    # 3. a remote gets added (e.g. as in #57985), then B is opened -> separate project
    git -C "$ROOT/BARE.git" remote add origin https://github.com/example/shared.git
    PB=$(loc "$ROOT/B" | pid)
    echo "project A=$PA  project B=$PB"
    
    # 4. the report's projects were migrated from v1 and are rooted at the worktree dirs
    #    themselves (A and B), not at the bare repo. Seed that layout directly.
    python3 - "$DB" "$PA" "$ROOT/A" "$PB" "$ROOT/B" <<'EOF2'
    import sqlite3,sys
    c=sqlite3.connect(sys.argv[1]); db,pa,a,pb,b=sys.argv[1:]
    c.execute("update project set worktree=? where id=?",(a,pa))
    c.execute("update project set worktree=? where id=?",(b,pb))
    c.commit()
    EOF2
    
    # 5. worktree discovery for both projects
    for p in "$PA" "$PB"; do
      curl -s -u opencode:repro -o /dev/null -w "refresh $p -> %{http_code}\n" -X POST -H 'content-type: application/json' \
        -d "{\"projectID\":\"$p\"}" "http://127.0.0.1:$PORT/api/worktree/refresh"
    done
    for p in "$PA" "$PB"; do
      echo "worktree.list $p:"; curl -s -u opencode:repro "http://127.0.0.1:$PORT/api/worktree?projectID=$p"; echo
    done
    
    echo "--- DB: project / project_directory"
    python3 - "$DB" <<'EOF'
    import sqlite3,sys
    c=sqlite3.connect(sys.argv[1])
    tabs=[r[0] for r in c.execute("select name from sqlite_master where type='table'")]
    for t in ("project","project_directory","worktree"):
        if t in tabs:
            cols=[r[1] for r in c.execute(f"pragma table_info({t})")]
            print(t, cols)
            for r in c.execute(f"select * from {t}"): print("  ",r[:4])
    EOF
    grep -c "worktree discovery failed" "$ROOT"/data/opencode/log/*.log 2>/dev/null
    kill $SERVER 2>/dev/null; wait 2>/dev/null

    Run it: bash repro.sh

  2. rishikeshgowdakk commented on Nov 3, 2026

    @rishikeshgowdakk

    Submitted a fix in #61493 with reproduction unit and integration tests covering bare repository exclusion and foreign worktree pruning.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions