feat(session): add support for per-session working directories#9365
feat(session): add support for per-session working directories#9365ssmirr wants to merge 1 commit intoanomalyco:devfrom
Conversation
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.
|
Thanks for your contribution! This PR doesn't have a linked issue. All PRs must reference an existing issue. Please:
See CONTRIBUTING.md for details. |
|
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. |
00637c0 to
71e0ba2
Compare
f1ae801 to
08fa7f7
Compare
|
Fully support/keen on this feature. |
|
This would be a great feature to have to be able to do soemthing like I know +1 comments not great but want to bump this up. |
|
cant wait for this feature! |
|
Cant wait for the feature |
|
I've been testing it locally against a real-world use case (a plugin that creates git worktrees and binds sessions to them) and can confirm it fixes the core issue: tools now correctly operate in the session's stored directory rather than However, there's one gap: plugin tool context is not updated In
This matters for any plugin that:
Essentially, any plugin that relies on The fix would be a one-liner in with the corresponding import: This ensures plugin tools get the same per-session directory that built-in tools use, maintaining consistency across the entire tool surface. I've tested this additional change locally alongside the rest of the PR and it works as expected — plugin tools receive the correct worktree path when operating within a session that has a custom directory. |
|
@ssmirr would you be willing to include this one-liner fix as part of this PR? |
|
@ssmirr would you be able to also rebase and resolve the conflicts so the team can merge and release this? Most of the conflict work is in |
|
@andreafspeziale Thank you for trying my PR and investigating this further! I recently found a similar experimental feature they've been working on that introduces |
@ssmirr Thanks for the tip. Dax mentioned it in a tweet (https://x.com/thdxr/status/2035513929489617353?s=20) but I hadn't spotted it. I'd say the Your PR solves the immediate problem for anyone using the It's also worth noting that After rebase and resolve the conflicts we can reach out to the team to see what they think. Let me know your thoughts! |
|
Following up on the above, I gave the workspaces feature a quick try. It’s actually very early and quite buggy at the moment. I recorded a short demo here: workspace.movHere’s another video showing a locally patched version of both OpenCode and open-trees: open-trees.movIn this setup, the developer experience feels very smooth and quite enjoyable, in my opinion. Hopefully we can get the team’s attention on this, they might have a preference on whether to refine the existing PR or open a new one from scratch. |
|
Sorry for being persistent, but I invested more time testing this against a real monorepo/multi-repo workflow to see if per-session directories would work out-of-box for subdirectory scoping. They don't. I had to patch several additional things locally to get subagents and supporting systems working correctly:
Even with all of those patched, there are still gaps that require architectural changes and can't be solved at the session level:
So the scope of this PR alone won't cover the full worktree experience I was hoping for. That said, it's still relevant and valuable for basic single-repo layouts where you just need sessions to operate in a different directory. Would love to see this rebased with at least the registry fix included, and ideally the subagent session creation fix ( Those two are one-liners that make a big difference for plugin workflows. For the full OpenCode experience (changed files, path, branch, etc.) you can have the agents manage your worktrees, |
What does this PR do?
Adds
Session.directory.get()/.set()so sessions can have their own working directory instead of always usingInstance.directory. This enables plugins to customize where each session operates, supporting use cases like:Updates all tools (bash, edit, write, grep, glob, ls, lsp, apply_patch) to use the session's directory.
Changes
Session.directoryaccessor withget()andset()methodsSession.directory.get()instead ofInstance.directorySession.directoryat the start of prompt loop and shell command executionSession.directoryin environment infoSession.directoryin path.cwdBackward Compatibility
Session.directory.get()falls back toInstance.directoryif not set, so existing behavior is unchanged.Note
This PR works standalone but is designed to pair with a
session.creatingplugin hook (#9361), which would allow plugins to set custom directories during session creation.How did you verify your code works?
packages/opencode/test/session/directory.test.tsInstance.directorycloses #9366