[BUG]: Session switching doesn't change working directory context #4112

Closed
opened 2026-02-16 17:42:39 -05:00 by yindo · 2 comments
Owner

Originally created by @bcheung on GitHub (Jan 2, 2026).

Originally assigned to: @thdxr on GitHub.

When switching/resuming sessions, the entire execution context remains bound to Instance.directory (where opencode was started), not the session's stored directory.

What's affected

  • System prompt - LLM is told "Working directory: X" but session was created in Y
  • All file tools - read, write, edit, grep, glob, ls, patch, multiedit resolve paths relative to wrong directory
  • Bash/shell execution - commands run in wrong cwd
  • LSP connections - language server bound to wrong project
  • File watchers - watching wrong directory
  • Snapshots - git operations in wrong repo
  • Config loading - may load wrong project config

Impact

  • Global project: Sessions from different directories are visible together (this is fine!), but switching to one operates in the current directory, not the session's original directory (this is the bug)
  • Worktrees: Can see session from worktree when in main repo, but continuing that session operates in main repo, not the worktree
  • Clone collision: Even when sessions are incorrectly mixed due to project ID collision, proper directory switching would at least operate in the right place

Note: Global project and shared sessions across directories isn't inherently bad - it's useful for seeing all your work. The bug is that switching sessions doesn't restore the correct working directory context.

Root cause

Instance.directory is set once at startup and never updated when switching sessions:

https://github.com/anomalyco/opencode/blob/038cff4a93bb39238534898b4790229773c4ff22/packages/opencode/src/project/instance.ts#L17-L41

It's used throughout the codebase:

session/system.ts      - system prompt, config loading
session/prompt.ts      - path resolution, shell cwd
tool/*.ts              - all file operations
lsp/*.ts               - language server
file/watcher.ts        - file watching
snapshot/index.ts      - git snapshots

Related

  • #3551 - "Sessions are now mixed with home directory session list"

Expected behavior

When switching/resuming a session, Instance.directory (and related context) should be updated to match session.directory so the entire execution context is correct.

Otherwise, opencode should not be showing sessions with different directories even if within the same project.

Originally created by @bcheung on GitHub (Jan 2, 2026). Originally assigned to: @thdxr on GitHub. When switching/resuming sessions, the entire execution context remains bound to `Instance.directory` (where opencode was started), not the session's stored `directory`. ## What's affected - **System prompt** - LLM is told "Working directory: X" but session was created in Y - **All file tools** - read, write, edit, grep, glob, ls, patch, multiedit resolve paths relative to wrong directory - **Bash/shell execution** - commands run in wrong cwd - **LSP connections** - language server bound to wrong project - **File watchers** - watching wrong directory - **Snapshots** - git operations in wrong repo - **Config loading** - may load wrong project config ## Impact - **Global project**: Sessions from different directories are visible together (this is fine!), but switching to one operates in the current directory, not the session's original directory (this is the bug) - **Worktrees**: Can see session from worktree when in main repo, but continuing that session operates in main repo, not the worktree - **Clone collision**: Even when sessions are incorrectly mixed due to project ID collision, proper directory switching would at least operate in the right place **Note:** Global project and shared sessions across directories isn't inherently bad - it's useful for seeing all your work. The bug is that switching sessions doesn't restore the correct working directory context. ## Root cause `Instance.directory` is set once at startup and never updated when switching sessions: https://github.com/anomalyco/opencode/blob/038cff4a93bb39238534898b4790229773c4ff22/packages/opencode/src/project/instance.ts#L17-L41 It's used throughout the codebase: ``` session/system.ts - system prompt, config loading session/prompt.ts - path resolution, shell cwd tool/*.ts - all file operations lsp/*.ts - language server file/watcher.ts - file watching snapshot/index.ts - git snapshots ``` ## Related - #3551 - "Sessions are now mixed with home directory session list" ## Expected behavior When switching/resuming a session, `Instance.directory` (and related context) should be updated to match `session.directory` so the entire execution context is correct. Otherwise, opencode should not be showing sessions with different directories even if within the same project.
yindo closed this issue 2026-02-16 17:42:39 -05:00
Author
Owner

@github-actions[bot] commented on GitHub (Jan 2, 2026):

This issue might be a duplicate of existing issues. Please check:

  • #3551: Sessions are now mixed with home directory session list - related to session visibility across directories
  • #3391: Wrong working directory - similar issue with incorrect working directory execution
  • #4048: tie working directory / directories with chat session - feature request for tying directory with session
  • #4251: Concurrent sessions working on different repos interfere each other - related issue with sessions in different directories interfering

Feel free to ignore if none of these address your specific case.

@github-actions[bot] commented on GitHub (Jan 2, 2026): This issue might be a duplicate of existing issues. Please check: - #3551: Sessions are now mixed with home directory session list - related to session visibility across directories - #3391: Wrong working directory - similar issue with incorrect working directory execution - #4048: tie working directory / directories with chat session - feature request for tying directory with session - #4251: Concurrent sessions working on different repos interfere each other - related issue with sessions in different directories interfering Feel free to ignore if none of these address your specific case.
Author
Owner

@R44VC0RP commented on GitHub (Jan 25, 2026):

Closing as duplicate of #1143 - this is being addressed in PR #9365 which adds per-session working directory support.

@R44VC0RP commented on GitHub (Jan 25, 2026): Closing as duplicate of #1143 - this is being addressed in PR #9365 which adds per-session working directory support.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#4112