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.directory may not update properly, causing it to return the previous directory instead of the current one.
-
Empty directory path: During initial loading, sync.data.path.directory can be an empty string, which causes newSessionWorktree to 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 decode64 utility:
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
newSessionWorktree memo (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
newSessionWorktree from 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 in packages/ui/src/context/helper.tsx
- Improving context initialization: Ensuring
sync.data.path.directory is 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 optional directory parameter 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)
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
D:/skills)D:/projects/other)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.tsImport
decode64utility:Get the current directory directly from URL parameters (line 139):
Explicitly pass the directory parameter when creating a session (line 191):
File:
packages/app/src/pages/session.tsxnewSessionWorktreememo (line 594):Rationale
session.create()ensures consistency, even when the SDK client's default directory is stalenewSessionWorktreefrom returning an invalid valueAlternative Approaches
This fix is a workaround that addresses the symptoms rather than the root cause. Alternative solutions might include:
createSimpleContext: Making the context properly reactive to prop changes inpackages/ui/src/context/helper.tsxsync.data.path.directoryis properly initialized before useAdditional Context
decode64(params.dir)pattern is already used in multiple places across the codebasesession.create()method accepts an optionaldirectoryparameter as a query parameterEnvironment