Bug Description
When git init is run in a project directory that already has opencode sessions, the project ID changes and all existing sessions become invisible in the TUI.
Steps to Reproduce
- Open opencode in a directory WITHOUT
.git (e.g. ~/projects/myapp)
- Create a session, do some work
- In the session (or externally), run
git init && git add . && git commit -m "init"
- Restart opencode in the same directory
- Expected: Previous sessions are visible
- Actual: Previous sessions are gone from TUI
Root Cause
opencode generates project.id differently depending on whether .git exists:
- No
.git: project_id = SHA1(worktree_path) (e.g. 8d7ed93b...)
- With
.git: project_id = git root commit hash (git rev-list --max-parents=0 HEAD) (e.g. 95a72b34...)
When git init happens, opencode creates a NEW project entry with the root commit hash. Sessions linked to the old SHA1-based project ID become orphaned — they exist in the DB but the TUI filters by the new project ID and finds none.
Verified On
- opencode version: latest (April 2026)
- Database:
~/.local/share/opencode/opencode.db (SQLite)
- Affected tables:
project, session
Suggested Fix
When resolving a project for a directory, check if another project entry exists for the same worktree path. If found, migrate sessions from the old project to the new one and delete the stale project entry.
Alternatively, use a stable project ID that doesn't change when VCS status changes (e.g. always use the path hash, or always use the path as the key).
Workaround
Manual SQL fix:
UPDATE session SET project_id = '<new_id>' WHERE project_id = '<old_id>';
DELETE FROM project WHERE id = '<old_id>';
We built an automated workaround in our opencode plugin that runs on startup:
- Computes the correct project ID for each worktree
- Migrates sessions from stale project entries
- Deletes duplicate project rows
Bug Description
When
git initis run in a project directory that already has opencode sessions, the project ID changes and all existing sessions become invisible in the TUI.Steps to Reproduce
.git(e.g.~/projects/myapp)git init && git add . && git commit -m "init"Root Cause
opencode generates
project.iddifferently depending on whether.gitexists:.git:project_id = SHA1(worktree_path)(e.g.8d7ed93b...).git:project_id = git root commit hash(git rev-list --max-parents=0 HEAD) (e.g.95a72b34...)When
git inithappens, opencode creates a NEW project entry with the root commit hash. Sessions linked to the old SHA1-based project ID become orphaned — they exist in the DB but the TUI filters by the new project ID and finds none.Verified On
~/.local/share/opencode/opencode.db(SQLite)project,sessionSuggested Fix
When resolving a project for a directory, check if another project entry exists for the same
worktreepath. If found, migrate sessions from the old project to the new one and delete the stale project entry.Alternatively, use a stable project ID that doesn't change when VCS status changes (e.g. always use the path hash, or always use the path as the key).
Workaround
Manual SQL fix:
We built an automated workaround in our opencode plugin that runs on startup: