Skip to content

Conversation

@e-cal
Copy link
Contributor

@e-cal e-cal commented Jan 28, 2026

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_server function to attempt another discovery before failing.

Relation to #119

This is complementary to #119 but solves a different problem:

  • fix(tmux): use pane PID to find correct opencode server port #119: Fixes selecting the wrong opencode when multiple instances share the same CWD. Works by tracking the pane the provider created via self.pane_id.
  • This PR: Fixes discovery when opencode was started before nvim and/or CWD doesn't match. Since the provider didn't create the pane, self.pane_id is nil and get_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's find_server() as fallback when CWD matching fails
  • lua/opencode/provider/tmux.lua: Add find_server() method that discovers opencode in sibling tmux panes
  • lua/opencode/provider/init.lua: Document the new find_server field in the Provider interface

Copilot AI review requested due to automatic review settings January 28, 2026 18:27
@e-cal e-cal changed the title feat(tmux): add fallback opencode server discovery for split panes feat(tmux): add fallback server discovery for split panes Jan 28, 2026
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_server hook and wire it into opencode.cli.server as a fallback when CWD-based discovery finds no server.
  • Implement Tmux:find_server() to scan sibling tmux panes by TTY, detect opencode processes, and derive the listening port and cwd via lsof and the /path endpoint.
  • Document the new find_server capability 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.

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.

1 participant