Skip to content

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

Merged
rekram1-node merged 4 commits intoanomalyco:devfrom
michaeldwan:md/fix-orphan-post-git-init
Mar 13, 2026
Merged

fix(opencode): sessions lost after git init in existing project#16814
rekram1-node merged 4 commits intoanomalyco:devfrom
michaeldwan:md/fix-orphan-post-git-init

Conversation

@michaeldwan
Copy link
Copy Markdown
Contributor

@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.

@github-actions
Copy link
Copy Markdown
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
Copy Markdown
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 fix(opencode): lost sessions across worktrees and orphan branches #16389 - fix(opencode): use git common dir for project ID cache to prevent worktree session loss
    • This is explicitly mentioned in the PR description as fixing "a different orphaning scenario" related to worktree cache location and rev-list --all instability. It's a complementary fix addressing a related but distinct issue.

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
@michaeldwan michaeldwan changed the title fix(opencode): run migrateFromGlobal after project upsert and on every startup fix(opencode): sessions lost after git init in existing project Mar 10, 2026
@michaeldwan michaeldwan force-pushed the md/fix-orphan-post-git-init branch from 300436f to 32309e3 Compare March 10, 2026 17:37
…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.
@michaeldwan michaeldwan force-pushed the md/fix-orphan-post-git-init branch from b75985a to b6b4b5b Compare March 11, 2026 17:36
@rekram1-node
Copy link
Copy Markdown
Collaborator

looks like there is a merge conflict now

…-init

# Conflicts:
#	packages/opencode/src/project/project.ts
@michaeldwan
Copy link
Copy Markdown
Contributor Author

@rekram1-node looks like upstream replaced all the "global"s with ProjectID.global. I just fixed this PR to match. Thanks!

@rekram1-node
Copy link
Copy Markdown
Collaborator

Sweet thanks! looking over it now

@rekram1-node
Copy link
Copy Markdown
Collaborator

rekram1-node commented Mar 13, 2026

it looks like the migrateFromGlobal function never got touched rlly after the sqlite migration, it should have been updated it looks very inefficient now, not that it matters too much. I'll fix that separately.

This lgtm tho

Thanks for the PR!

@rekram1-node rekram1-node merged commit b94e110 into anomalyco:dev Mar 13, 2026
8 checks passed
ConanXu-math pushed a commit to ConanXu-math/opencode that referenced this pull request Mar 17, 2026
demostanis pushed a commit to demostanis/opencode that referenced this pull request Mar 19, 2026
demostanis pushed a commit to demostanis/opencode that referenced this pull request Mar 20, 2026
balcsida pushed a commit to balcsida/opencode that referenced this pull request Mar 24, 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

2 participants