Skip to content

fix(opencode): run migrateFromGlobal after project upsert and on every startup#16814

Open
michaeldwan wants to merge 1 commit intoanomalyco:devfrom
michaeldwan:md/fix-orphan-post-git-init
Open

fix(opencode): run migrateFromGlobal after project upsert and on every startup#16814
michaeldwan wants to merge 1 commit intoanomalyco:devfrom
michaeldwan:md/fix-orphan-post-git-init

Conversation

@michaeldwan
Copy link

@michaeldwan michaeldwan commented Mar 9, 2026

Issue for this PR

Closes #16812
Closes #15678

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

migrateFromGlobal() had two bugs that caused sessions created under the shared "global" project ID to become permanently orphaned when a git repo is initialized:

  1. It ran before the target project row was inserted into the database. The FK constraint on session.project_id → project.id silently rejected the update, and the .catch() handler swallowed the error.
  2. It only ran on first project row creation (inside an if (!row) branch). Sessions that accumulated under "global" after the real project ID already existed were never migrated.

The fix moves the call to after the project row upsert and removes the first-creation gate so it runs on every startup. This is safe because the function already filters by directory match and is a no-op when no global sessions exist (two cheap indexed reads).

Related PR: #16389 — fixes a different orphaning scenario (worktree cache location and rev-list --all instability).

How did you verify your code works?

  • Built opencode from this branch, reproduced the bug (new dir → create session → git init → commit → sessions gone), ran the fixed build, sessions reappeared
  • 4 new tests written, all failed before applying the fix

Screenshots / recordings

If this is a UI change, please include a screenshot or recording.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

If you do not follow this template your PR will be automatically rejected.

…y startup

migrateFromGlobal had two bugs: it ran before the target project row
was inserted (FK constraint silently rejected the update), and it only
ran on first project creation so late-arriving global sessions were
permanently orphaned. Move the call after the upsert and remove the
first-creation gate so it runs on every startup.
@github-actions
Copy link
Contributor

github-actions bot commented Mar 9, 2026

Hey! Your PR title fix(opencode): run migrateFromGlobal after project upsert and on every startup doesn't follow conventional commit format.

Please update it to start with one of:

  • feat: or feat(scope): new feature
  • fix: or fix(scope): bug fix
  • docs: or docs(scope): documentation changes
  • chore: or chore(scope): maintenance tasks
  • refactor: or refactor(scope): code refactoring
  • test: or test(scope): adding or updating tests

Where scope is the package name (e.g., app, desktop, opencode).

See CONTRIBUTING.md for details.

@github-actions
Copy link
Contributor

github-actions bot commented Mar 9, 2026

The following comment was made by an LLM, it may be inaccurate:

Based on my search, I found one related PR that might be worth noting:

Related PR (not a duplicate):

PR #15772 - fix: list all sessions regardless of git context (#15678)

No duplicate PRs found

@michaeldwan michaeldwan changed the title fix(opencode): run migrateFromGlobal after project upsert and on every startup fix(opencode): run migrateFromGlobal after project upsert and on every startup Mar 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant