Skip to content

Project ID changes after git init — sessions disappear from TUI #21230

@qwertist

Description

@qwertist

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

  1. Open opencode in a directory WITHOUT .git (e.g. ~/projects/myapp)
  2. Create a session, do some work
  3. In the session (or externally), run git init && git add . && git commit -m "init"
  4. Restart opencode in the same directory
  5. Expected: Previous sessions are visible
  6. 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

Metadata

Metadata

Assignees

Labels

coreAnything pertaining to core functionality of the application (opencode server stuff)

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions