Skip to content

fix(opencode): recover projects moved to a new path - #38584

Closed
1hirak wants to merge 2 commits into
anomalyco:devfrom
1hirak:stale-workspace-recovery
Closed

fix(opencode): recover projects moved to a new path#38584
1hirak wants to merge 2 commits into
anomalyco:devfrom
1hirak:stale-workspace-recovery

Conversation

@1hirak

@1hirak1hirak commented Jul 24, 2026

Copy link
Copy Markdown

Issue for this PR

Closes#38578

Type of change

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

What does this PR do?

When a Git repository moved, the stored project kept the missing original path
as its primary worktree and tracked the live path as a sandbox. Desktop startup
could therefore request the deleted path first, causing Git discovery to fail
in FileSystem.realPath before the existing project record could be repaired.

This change recovers the project at both boundaries:

  • before instance creation, a missing stored worktree resolves to its single
    surviving sandbox;
  • the recovered directory is used as the instance cache key and runtime
    context, so the stale and current requests share one valid instance;
  • the in-process alias remains available for repeated stale requests after the
    database migration;
  • the live worktree is promoted to the project's primary worktree;
  • missing and duplicate sandbox paths are removed;
  • matching project and legacy-global sessions move to the recovered project
    directory, including their relative path, without changing
    time_updated; and
  • matching workspace directories are updated.

Recovery is intentionally conservative: it only redirects a missing primary
worktree when exactly one recorded sandbox still exists. Existing behavior is
unchanged when the primary worktree exists or multiple live sandboxes make the
replacement ambiguous.

How did you verify your code works?

The regression tests create a Git repository, seed project and legacy-global
sessions plus a workspace, move the repository, and then request the deleted
path. They verify:

  • the stale directory resolves to the moved repository before Git discovery;
  • instance context and caching use the live path;
  • the project ID remains stable;
  • the live worktree is promoted and sandbox state is cleaned;
  • sessions, relative paths, and workspace records are migrated; and
  • session timestamps are preserved.

Executed:

bun test test/project/project.test.ts test/project/instance.test.ts
# 47 pass, 0 fail
bun typecheck
# Required pre-push monorepo hook
bun turbo typecheck
# 30 successful, 30 total
oxlint packages/opencode/src/project/project.ts \
packages/opencode/src/project/instance-store.ts \
packages/opencode/test/project/project.test.ts \
packages/opencode/test/project/instance.test.ts
# 0 errors
git diff --check

Screenshots / recordings

Not applicable; this is a project/session persistence and instance-routing fix
with no UI changes.

Checklist

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

@belisoful

belisoful commented Jul 30, 2026

Copy link
Copy Markdown

#35311 is a much better solution than this.

@github-actions

Copy link
Copy Markdown
Contributor

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Desktop restores a moved/deleted workspace and leaves its sessions permanently failing with FileSystem.realPath ENOENT

2 participants

@1hirak@belisoful