You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Project ID changes after git init — sessions disappear from TUI #21230
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...)
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.
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>';
DELETEFROM project WHERE id ='<old_id>';
We built an automated workaround in our opencode plugin that runs on startup:
@rekram1-node Thanks for trying. The key is the sequence: the session must exist BEFORE git init.
Exact reproduction steps
# 1. Create a directory WITHOUT git
mkdir /tmp/test-project &&cd /tmp/test-project
echo'console.log("hi")'> index.js
# 2. Open opencode in it, create a session
opencode
# Send any message (e.g. "what files are here?"), then quit (Ctrl+C)# 3. Verify session exists
sqlite3 ~/.local/share/opencode/opencode.db \
"SELECT id, project_id FROM session WHERE directory = '/tmp/test-project' AND parent_id IS NULL"# Note the project_id — it will be a SHA1 hash of the path or 'global'# 4. NOW init gitcd /tmp/test-project && git init && git add .&& git commit -m "init"# 5. Reopen opencode in the same directory
opencode
# The previous session should be GONE from the TUI session list
Why it happens
In packages/opencode/src/project/project.ts, fromDirectory():
Without .git (step 2): returns id: ProjectID.global (line 177)
With .git (step 5): computes id = git rev-list --max-parents=0 HEAD (line 216)
These are different IDs. A new project row is inserted (line 282-310). Line 313-320 migrates sessions from ProjectID.global to the new ID, BUT only where session.directory = project.worktree. If the directory doesn't exactly match (e.g. the session was created in a subdirectory, or the worktree is /), the migration misses it.
Also: if the project was created with a non-global ID (e.g. SHA1 of path in older opencode versions), the migration on line 313-320 won't catch it because it only checks ProjectID.global.
Related
This is the same root cause as #16995 (worktrees resolve to different project IDs). The fix in our PR #21232 was correct (migrate sessions from stale project entries with same worktree), but the PR was auto-closed for formatting.
Our workaround
We run this on every startup in our plugin:
// For each worktree with multiple project entries:// 1. Compute correct project ID (git root commit or SHA1 of path)// 2. Migrate sessions from stale project to correct one// 3. Delete stale project entry
Activity
github-actions commentedon Apr 6, 2026
This issue might be a duplicate of existing issues. Please check:
fix: migrate sessions when project ID changes after git init
rekram1-node commentedon Apr 6, 2026
Cannot replicate
qwertist commentedon Apr 8, 2026
@rekram1-node Thanks for trying. The key is the sequence: the session must exist BEFORE
git init.Exact reproduction steps
Why it happens
In
packages/opencode/src/project/project.ts,fromDirectory():.git(step 2): returnsid: ProjectID.global(line 177).git(step 5): computesid = git rev-list --max-parents=0 HEAD(line 216)These are different IDs. A new project row is inserted (line 282-310). Line 313-320 migrates sessions from
ProjectID.globalto the new ID, BUT only wheresession.directory = project.worktree. If the directory doesn't exactly match (e.g. the session was created in a subdirectory, or the worktree is/), the migration misses it.Also: if the project was created with a non-global ID (e.g. SHA1 of path in older opencode versions), the migration on line 313-320 won't catch it because it only checks
ProjectID.global.Related
This is the same root cause as #16995 (worktrees resolve to different project IDs). The fix in our PR #21232 was correct (migrate sessions from stale project entries with same worktree), but the PR was auto-closed for formatting.
Our workaround
We run this on every startup in our plugin:
Full code: opencode-hub.js fixSessionProjects()
rekram1-node commentedon Apr 14, 2026
@qwertist in my screenshot I demonstrate exactly this:
The session exists prior to the init lol thats exactly ehat i showed u
rekram1-node commentedon Apr 14, 2026
Cannot replicate:
rekram1-node commentedon Apr 14, 2026
If u continue having issues join our discord and @ me, it will be easier to go back and forth.
fix(project): ne pas écraser le worktree du projet global