Repository navigation
Description
This solution is generated by AI. Maybe some alternative approaches is not good. but the code it fixed is work. It may be not the best solution.
Bug: New session created with wrong directory when switching between projects
Problem Description
When navigating between different project directories and creating a new session, the session is created in the wrong directory (the previous directory instead of the currently browsed directory).
Steps to Reproduce
- Navigate to project directory A (e.g.,
D:/skills) - Click "New session" and submit a message
- Navigate to project directory B (e.g.,
D:/projects/other) - Click "New session" and submit a message
- Observe that the session is created in directory A instead of directory B
Expected Behavior
The new session should be created in the currently browsed directory (directory B).
Actual Behavior
The session is created in the previously browsed directory (directory A).
Root Cause Analysis
The issue has two contributing factors:
-
Stale directory reference: When navigating between directories, the
sdk.directorymay not update properly, causing it to return the previous directory instead of the current one. -
Empty directory path: During initial loading,
sync.data.path.directorycan be an empty string, which causesnewSessionWorktreeto return an empty string instead of the expected directory path.
Proposed Fix
The following changes were made to work around the issue:
File: packages/app/src/components/prompt-input/submit.ts
-
Import
decode64utility:import { decode64 } from "@/utils/base64"
-
Get the current directory directly from URL parameters (line 139):
const projectDirectory = decode64(params.dir) ?? sdk.directory
-
Explicitly pass the directory parameter when creating a session (line 191):
session = await client.session.create({ directory: sessionDirectory, })
File: packages/app/src/pages/session.tsx
- Add empty string check in
newSessionWorktreememo (line 594):if (project && sync.data.path.directory && sync.data.path.directory !== project.worktree) { return sync.data.path.directory }
Rationale
- Getting the directory from URL parameters ensures we always use the currently browsed directory, regardless of the context state
- Explicitly passing the directory parameter to
session.create()ensures consistency, even when the SDK client's default directory is stale - The empty string check prevents
newSessionWorktreefrom returning an invalid value - This approach is minimal and doesn't require changes to the underlying context system
Alternative Approaches
This fix is a workaround that addresses the symptoms rather than the root cause. Alternative solutions might include:
- Fixing
createSimpleContext: Making the context properly reactive to prop changes inpackages/ui/src/context/helper.tsx - Improving context initialization: Ensuring
sync.data.path.directoryis properly initialized before use - Refactoring directory management: Centralizing directory state management to avoid inconsistencies
Additional Context
- The
decode64(params.dir)pattern is already used in multiple places across the codebase - The SDK's
session.create()method accepts an optionaldirectoryparameter as a query parameter - This fix has been tested and appears to resolve the issue without introducing new problems
- Further investigation may be needed to identify why the context doesn't update properly on navigation
Environment
- OpenCode version: 1.1.53
- Platform: All platforms (Windows, macOS, Linux)
Activity
github-actions commentedon Apr 12, 2026
To stay organized issues are automatically closed after 90 days of no activity. If the issue is still relevant please open a new one.
Hona commentedon Sep 2, 2026
Fixed for opencode2 launch via #46713
You can try the desktop beta here: https://github.com/anomalyco/opencode-beta