Skip to content

fix(app): prevent stale project navigation race condition - #1030

Open
HaleTom wants to merge 6 commits into
XiaomiMiMo:mainfrom
HaleTom:fix-project-switch-workspace-race
Open

HaleTom wants to merge 6 commits into
XiaomiMiMo:mainfrom
HaleTom:fix-project-switch-workspace-race

Conversation

@HaleTom

@HaleTom HaleTom commented Jun 18, 2026

Copy link
Copy Markdown

Ported from anomalyco/opencode#24641

Issue for this PR

Closes projects-switch e2e flake #14461

Type of change

  • Bug fix

What does this PR do?

Fixes an intermittent e2e test failure in projects-switch.spec.ts line 90 (toHaveURL), which flakes on Windows CI.

Root cause: When switching back to a project after enabling workspaces, navigateToProject computes dirs from project.sandboxes. But sandbox population is asynchronous — the test just enabled workspaces, and project.sandboxes may not yet include the workspace directory. So canOpen(target.directory) returns false for the workspace dir, openSession bails silently, and the function falls through to navigate to the wrong URL. The toHaveURL assertion catches this intermediate wrong state.

This is Windows-specific because worktree.list and fsmonitor are slower on Windows, widening the async gap between "workspaces enabled" and "sandboxes populated."

Fix: Call ensureDirOpenable(target.directory) before any canOpen check inside openSession. ensureDirOpenable fetches the worktree list from the SDK when the cached dirs don't include the target directory, ensuring the workspace dir is always available before the check. It returns the canOpen result directly, so the call site uses its boolean return as the guard.

Also added ensureDirOpenable(resolved.directory) for the SDK session resolution path, where the resolved session might be in a workspace dir not yet in dirs.

How did you verify your code works?

  • bun typecheck passes
  • Root cause was traced through the full navigateToProject → openSession → canOpen chain
  • The flake could not be reproduced locally (Linux/Chromium, 1 worker) — it is Windows-specific / CI-specific due to slower worktree.list on Windows
  • Production-readiness review completed with no blocking issues

Related Issues

Checklist

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

Note on force pushes to main

This PR was originally raised as #594 but was auto-closed when the main branch was force-pushed (see #760). Force-pushing to a shared branch breaks PR reopenability and orphans review comments.

HaleTom added 6 commits June 18, 2026 19:21
…oject

When switching projects after enabling workspaces, navigateToProject would
fail to find the workspace directory in dirs (computed from project.sandboxes)
because sandbox population is asynchronous. This caused openSession to return
false and navigate to the wrong URL, flaking the e2e project-switch test on
Windows CI where the async gap is wider.

Fix: call refreshDirs before canOpen checks in openSession, ensuring the
worktree list is fetched from the SDK when the cached dirs don't include the
target directory.
Per Copilot review feedback on PR #24641: refreshDirs() already returns
canOpen(), so calling canOpen() again immediately after is redundant.
Use the boolean return directly to simplify the control flow.
Address review feedback: track the latest requested project root and
abort stale navigateToProject calls when user quickly switches projects.
Prevents stale project navigations from running fallback logic
(clearLastProjectSession, fallback navigation) when the user has
already switched to a different project.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flaky e2e: "switching back to a project opens the latest workspace session"

1 participant