feat(tmux): add fallback server discovery for split panes#150
feat(tmux): add fallback server discovery for split panes#150e-cal wants to merge 5 commits intonickjvandyke:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR extends server discovery so that, when CWD-based matching fails, providers can perform their own search for existing opencode instances, with an initial implementation for tmux that inspects sibling panes. The goal is to make it much more intuitive for tmux users where prompts are sent, especially when an opencode pane was started before Neovim or with a different CWD.
Changes:
- Add a provider-specific
find_serverhook and wire it intoopencode.cli.serveras a fallback when CWD-based discovery finds no server. - Implement
Tmux:find_server()to scan sibling tmux panes by TTY, detectopencodeprocesses, and derive the listening port and cwd vialsofand the/pathendpoint. - Document the new
find_servercapability in the provider interface so other providers can add their own discovery strategies.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
lua/opencode/cli/server.lua |
Extends find_server_inside_nvim_cwd() to call provider:find_server() as a fallback before giving up and starting a new server. |
lua/opencode/provider/tmux.lua |
Adds Tmux:find_server() that discovers opencode processes in sibling panes by TTY, extracts their listening port, and returns a populated Server descriptor. |
lua/opencode/provider/init.lua |
Updates the opencode.Provider type docs to include an optional find_server method used for provider-specific fallback discovery. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
de5f1cc to
5de2380
Compare
|
@nickjvandyke this is ready now. let me know if you have any questions, but should be a big QoL improvement for us tmux users (or at least it is for myself) |
|
@e-cal I'm working on a server-selection feature as a more general solution to this issue. What would you think about that? i.e. when sending to
Plus the ability to select a server at any time of course 😀 Separately we'd have to think about how to format filepaths when |
|
ah that would be great! I think i would still want that extra layer of tmux split fallback in the middle though. If the server select is hackable in the sense I can give it a custom sorter (like telescope) and make it auto select instead of prompting me I think this is perfect. My most common use case is having opencode in a tmux split with cwd in some parent dir (e.g. ~/projects), then nvim in a specific project in another split. In these unambiguous cases it would be nice to still have frictionless connection. But with that being said I’d be happy to build on top of the server discovery, with non-default config options so it doesn’t break things for others. Or just set this up in my own config. |
|
I might be open to auto-selecting the single |
|
Unfortunately not, I almost always have multiple opencode servers running at a given time. I would be happy to add another But the discovery logic I'm looking for is:
Or, with the server discovery you're working on:
I rely heavily on (3). To avoid confusion I would suggest just another two config options: e.g. Set them both false by default so out-of-the-box you get the selection prompt, but people like myself can get the connection with 0 friction. I think by keeping all defaults to current or interactive behavior is a really good idea to keep things intuitive for the majority of users, but I would like to be able to override it. |
|
Thanks for ideating! I want to generalize this solution as much as possible - I can't keep up with everyone's specific needs 😅 For 3, I think it makes sense to expose a configurable server discovery. Either in providers (like you did here), or completely separate. I'll try to get back to you on that as this new feature settles! (sorry for the merge conflicts!) |
Adds fallback discovery logic for opencode sessions active in a tmux sibling pane but not matching CWD.
For tmux users, it is reasonable to assume that if they have a split open with opencode and fire off a prompt from nvim, they likely intend it to go to that instance in the split whether it matches the CWD discovery logic or not. Overall, makes it much more intuitive where your prompt will end up, and avoids unnecessarily launching new sessions.
Implemented as a fallback, so if there is a different session that does get discovered we still connect to that as normal. When there are multiple splits running opencode and none share CWD, it just picks the first one it finds. And then if there are no opencode instances in splits, use the normal logic of launching and connecting via the plugin.
Currently only affects tmux users, but implemented such that any provider can implement a
find_serverfunction to attempt another discovery before failing.Also adds
auto_closeoption to the tmux provider. When set tofalse, the tmux pane will not auto-close when opencode exits. Defaults totrueto maintain current behavior out of the box.Relation to #119
This is complementary to #119 but solves a different problem:
self.pane_id.self.pane_idis nil andget_port()returns nil.This PR searches all sibling panes in the current tmux window via TTY, regardless of whether the provider started them.
Changes
lua/opencode/cli/server.lua: Use provider'sfind_server()as fallback when CWD matching failslua/opencode/provider/tmux.lua: Addfind_server()method that discovers opencode in sibling tmux panes, addauto_closeconfig optionlua/opencode/provider/init.lua: Document the newfind_serverfield in the Provider interfacelua/opencode/config.lua: Addauto_closedefault config for tmux provider