Skip to content

Sessions orphaned when git repo is initialized in existing opencode directory #16812

Description

@michaeldwan

Description

Describe the bug

Sessions created before a git repository has commits are stored under project_id = "global". When a commit is made and opencode resolves a real project ID (the root commit SHA), the migrateFromGlobal() function is supposed to reassign those sessions but it fails silently.

Root cause

migrateFromGlobal() in project.ts has two bugs:

  1. FK constraint violation: It runs before the target project row is inserted into the project table. The session.project_id column has a foreign key constraint referencing project.id, so the UPDATE is rejected. The .catch() handler on the work() call swallows the error silently.
  2. One-shot gate: It only runs inside the if (!row) branch — i.e., the first time a project ID row is created. Sessions that accumulate under "global" after the real project row already exists are never migrated.

Expected behavior

Sessions created before git init / first commit should appear in the session list after the project transitions from "global" to a real project ID.

Plugins

No response

OpenCode version

1.2.24

Steps to reproduce

  1. In an empty directory (eg mkdir /tmp/test && cd /tmp/test)
  2. Start opencode, chat, create one or more sessions, exit opencode
  3. opencode session list returns sessions
  4. git init && git commit --allow-empty -m "first commit"
  5. opencode session list returns nothing

Screenshot and/or share link

No response

Operating System

macOS 26.3

Terminal

Ghostty

Activity

github-actions commented on Mar 9, 2026

@github-actions
Contributor

This issue might be a duplicate of existing issues. Please check:

c-w-xiaohei commented on Mar 11, 2026

@c-w-xiaohei

I hit the same underlying bug on v1.2.21 (Linux/WSL, desktop app) with an existing repo at /home/c_w_xiaohei/workspace.

What happened:

  • OpenCode created a new project row for the directory and tried to migrate existing sessions from project_id = "global" to the new project id.
  • The migration failed with FOREIGN KEY constraint failed and logged failed to migrate sessions from global to project.
  • After that, the chats were not deleted, but split between global and the new project id, so the session list looked like history had disappeared.

Local evidence:

  • log: ~/.local/share/opencode/log/2026-03-11T180424.log
  • the DB still contained the sessions/messages
  • manually updating the affected session.project_id rows from global to the new project id made the missing chats reappear immediately

So this is not only a "git init after sessions already exist" case. The same global -> project migration path can fail in normal use and orphan visible history.

-- by OpenCode, after local investigation

added a commit that references this issue on Mar 12, 2026
9f3d124
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions