Bug: New session created with wrong directory when browsing a project #8951

Open
opened 2026-02-16 18:11:14 -05:00 by yindo · 0 comments
Owner

Originally created by @armyja on GitHub (Feb 10, 2026).

Originally assigned to: @rekram1-node on GitHub.

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

  1. Navigate to project directory A (e.g., D:/skills)
  2. Click "New session" and submit a message
  3. Navigate to project directory B (e.g., D:/projects/other)
  4. Click "New session" and submit a message
  5. 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:

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

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

  1. Import decode64 utility:

    import { decode64 } from "@/utils/base64"
    
  2. Get the current directory directly from URL parameters (line 139):

    const projectDirectory = decode64(params.dir) ?? sdk.directory
    
  3. 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

  1. 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:

  1. Fixing createSimpleContext: Making the context properly reactive to prop changes in packages/ui/src/context/helper.tsx
  2. Improving context initialization: Ensuring sync.data.path.directory is properly initialized before use
  3. 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)

Originally created by @armyja on GitHub (Feb 10, 2026). Originally assigned to: @rekram1-node on GitHub. 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 1. Navigate to project directory A (e.g., `D:/skills`) 2. Click "New session" and submit a message 3. Navigate to project directory B (e.g., `D:/projects/other`) 4. Click "New session" and submit a message 5. 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: 1. **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. 2. **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`** 1. Import `decode64` utility: ```typescript import { decode64 } from "@/utils/base64" ``` 2. Get the current directory directly from URL parameters (line 139): ```typescript const projectDirectory = decode64(params.dir) ?? sdk.directory ``` 3. Explicitly pass the directory parameter when creating a session (line 191): ```typescript session = await client.session.create({ directory: sessionDirectory, }) ``` **File: `packages/app/src/pages/session.tsx`** 4. Add empty string check in `newSessionWorktree` memo (line 594): ```typescript 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: 1. **Fixing `createSimpleContext`**: Making the context properly reactive to prop changes in `packages/ui/src/context/helper.tsx` 2. **Improving context initialization**: Ensuring `sync.data.path.directory` is properly initialized before use 3. **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) ---
yindo added the windowsbug labels 2026-02-16 18:11:14 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#8951