[PR #9365] feat(session): add support for per-session working directories #13089

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

Original Pull Request: https://github.com/anomalyco/opencode/pull/9365

State: open
Merged: No


What does this PR do?

Adds Session.directory.get()/.set() so sessions can have their own working directory instead of always using Instance.directory. This enables plugins to customize where each session operates, supporting use cases like:

  • Git worktrees (isolated branches per session)
  • Sandboxed environments
  • Custom project layouts
    Updates all tools (bash, edit, write, grep, glob, ls, lsp, apply_patch) to use the session's directory.

Changes

  • session/index.ts: Add Session.directory accessor with get() and set() methods
  • All tools: Update to use Session.directory.get() instead of Instance.directory
  • prompt.ts: Set Session.directory at the start of prompt loop and shell command execution
  • system.ts: Show Session.directory in environment info
  • compaction.ts: Use Session.directory in path.cwd

Backward Compatibility

Session.directory.get() falls back to Instance.directory if not set, so existing behavior is unchanged.

Note

This PR works standalone but is designed to pair with a session.creating plugin hook (https://github.com/anomalyco/opencode/pull/9361), which would allow plugins to set custom directories during session creation.

How did you verify your code works?

  1. Added unit tests in packages/opencode/test/session/directory.test.ts
  2. Manual testing with a plugin that sets custom session directories
  3. Verified tools operate in the correct directory
  4. Verified sessions without a custom directory still default to Instance.directory

closes #9366

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/9365 **State:** open **Merged:** No --- ### What does this PR do? Adds `Session.directory.get()`/`.set()` so sessions can have their own working directory instead of always using `Instance.directory`. This enables plugins to customize where each session operates, supporting use cases like: - Git worktrees (isolated branches per session) - Sandboxed environments - Custom project layouts Updates all tools (bash, edit, write, grep, glob, ls, lsp, apply_patch) to use the session's directory. #### Changes - **session/index.ts**: Add `Session.directory` accessor with `get()` and `set()` methods - **All tools**: Update to use `Session.directory.get()` instead of `Instance.directory` - **prompt.ts**: Set `Session.directory` at the start of prompt loop and shell command execution - **system.ts**: Show `Session.directory` in environment info - **compaction.ts**: Use `Session.directory` in path.cwd #### Backward Compatibility `Session.directory.get()` falls back to `Instance.directory` if not set, so existing behavior is unchanged. #### Note This PR works standalone but is designed to pair with a `session.creating` plugin hook (https://github.com/anomalyco/opencode/pull/9361), which would allow plugins to set custom directories during session creation. ### How did you verify your code works? 1. Added unit tests in `packages/opencode/test/session/directory.test.ts` 2. Manual testing with a plugin that sets custom session directories 3. Verified tools operate in the correct directory 4. Verified sessions without a custom directory still default to `Instance.directory` closes #9366
yindo added the pull-request label 2026-02-16 18:17:58 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#13089