Skip to content

feat(session): add support for per-session working directories#9365

Open
ssmirr wants to merge 1 commit intoanomalyco:devfrom
ssmirr:feat/session-directory-support
Open

feat(session): add support for per-session working directories#9365
ssmirr wants to merge 1 commit intoanomalyco:devfrom
ssmirr:feat/session-directory-support

Conversation

@ssmirr
Copy link

@ssmirr ssmirr commented Jan 19, 2026

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 (#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

Add Session.directory.get()/set() to allow sessions to have their own
working directory, separate from Instance.directory. This enables use
cases like:
- Git worktrees per session
- Sandboxed directories
- Custom project layouts

Changes:
- Add Session.directory accessor in session/index.ts
- Update all tools (bash, edit, write, grep, glob, ls, lsp, apply_patch)
  to use Session.directory.get() instead of Instance.directory
- Update prompt.ts to set Session.directory from session.directory
  at the start of the prompt loop and shell command execution
- Update system.ts to show Session.directory in environment info
- Update compaction.ts to use Session.directory in path.cwd

Session.directory falls back to Instance.directory if not set,
ensuring backward compatibility.
@github-actions
Copy link
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions
Copy link
Contributor

The following comment was made by an LLM, it may be inaccurate:

Based on my search, I found one related PR that is closely connected to the current PR:

Related PR (Companion Feature):

No duplicate PRs found addressing the same feature.

@simonsmallchua
Copy link

simonsmallchua commented Jan 31, 2026

Fully support/keen on this feature.
(Not sure if this is the right place to put a comment like this)

@MrTravisB
Copy link

This would be a great feature to have to be able to do soemthing like /new /new/working/directory

I know +1 comments not great but want to bump this up.

@yanhao98
Copy link
Contributor

建议也更新 skill 系统的目录获取逻辑。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE]: Allow sessions to have custom working directories

4 participants

Comments