Skip to content

fix: migrate sessions from global to git project after init#17150

Closed
Wangmerlyn wants to merge 1 commit intoanomalyco:devfrom
Wangmerlyn:dev
Closed

fix: migrate sessions from global to git project after init#17150
Wangmerlyn wants to merge 1 commit intoanomalyco:devfrom
Wangmerlyn:dev

Conversation

@Wangmerlyn
Copy link
Copy Markdown
Contributor

Fixes #16812

Problem

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 sessions were not being migrated properly.

Root Causes

  1. FK constraint violation: migrateFromGlobal() was called before the target project row was inserted into the project table, causing the UPDATE to be rejected due to the foreign key constraint on session.project_id.
  2. One-shot gate: The migration only ran inside the if (!row) branch — i.e., only when a new project row was created. Sessions accumulated under "global" after the real project row already existed were never migrated.

Solution

  • Moved migrateFromGlobal() call to after the project row is inserted/updated
  • The migration now runs every time fromDirectory is called with a non-global project ID, not just when creating a new project row
  • Added a test case that reproduces the bug scenario

Verification

  • New test passes: migrates sessions from global to git project after git init
  • All existing project tests pass (18 tests)
  • Typecheck passes for all packages

Fixes anomalyco#16812

The migrateFromGlobal function was being called before the project row
was inserted into the database, causing a foreign key constraint violation.
Additionally, it only ran when creating a new project row, not when an
existing project transitioned from global to git-based ID.

Changes:
- Move migrateFromGlobal call to after project row insertion
- Ensure migration runs even when project row already exists
- Add test to verify session migration after git init

Closes anomalyco#16812
@github-actions github-actions bot added the needs:compliance This means the issue will auto-close after 2 hours. label Mar 12, 2026
@github-actions
Copy link
Copy Markdown
Contributor

This PR doesn't fully meet our contributing guidelines and PR template.

What needs to be fixed:

  • PR description is missing required template sections. Please use the PR template.

Please edit this PR description to address the above within 2 hours, or it will be automatically closed.

If you believe this was flagged incorrectly, please let a maintainer know.

@github-actions
Copy link
Copy Markdown
Contributor

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

Found a potential related PR:

PR #16814: fix(opencode): sessions lost after git init in existing project
#16814

This PR appears to address the same issue as PR #17150 - sessions being lost or not properly migrated when git is initialized in an existing project. Both PRs deal with the project_id = "global" problem and session migration after git init.

@Wangmerlyn
Copy link
Copy Markdown
Contributor Author

Closed as this is the same as PR #16814

@Wangmerlyn Wangmerlyn closed this Mar 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs:compliance This means the issue will auto-close after 2 hours.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sessions orphaned when git repo is initialized in existing opencode directory

1 participant