@@ -84,7 +84,7 @@ local function discover_instances()
8484 if env .code == 0 and env .stdout :match (' TMUX=' .. tmux_env ) then
8585 local port = find_port_for_pid (pid )
8686 if port then
87- local window = find_tmux_window (pid ) or ' ? '
87+ local window = find_tmux_window (pid )
8888 table.insert (instances , { pid = pid , port = port , window = window })
8989 end
9090 end
@@ -96,22 +96,24 @@ local function discover_instances()
9696end
9797
9898--- Resolve the port to use. Only prompts when there are multiple instances.
99- --- @param callback fun ( port : string ?, single_instance : boolean , window : string ?)
99+ --- Caches the choice only when multiple instances exist.
100+ --- @param callback fun ( port : string ?, window : string ?)
100101local function resolve_port (callback )
101102 if cache and (vim .uv .now () - cache .timestamp ) < CACHE_TTL_MS then
102- callback (cache .port , true , cache .window )
103+ callback (cache .port , cache .window )
103104 return
104105 end
105106
106107 local instances , err = discover_instances ()
107108 if # instances == 0 then
108109 notify (err or ' No OpenCode instance found' , vim .log .levels .ERROR )
109- callback (nil , false , nil )
110+ callback (nil , nil )
110111 return
111112 end
112113
113114 if # instances == 1 then
114- callback (instances [1 ].port , true , instances [1 ].window )
115+ -- Single instance: no cache needed, always resolve fresh
116+ callback (instances [1 ].port , instances [1 ].window )
115117 return
116118 end
117119
@@ -121,21 +123,22 @@ local function resolve_port(callback)
121123 format_item = function (item ) return string.format (' window %s' , item .window ) end ,
122124 }, function (choice )
123125 if not choice then
124- callback (nil , false , nil )
126+ callback (nil , nil )
125127 return
126128 end
127- callback (choice .port , false , choice .window )
129+ -- Multiple instances: cache the picked port so we don't prompt again
130+ cache = { port = choice .port , session_id = ' ' , window = choice .window , timestamp = vim .uv .now () }
131+ callback (choice .port , choice .window )
128132 end )
129133 end )
130134end
131135
132- --- Fetch sessions. Auto -picks the top session when `auto` is true, otherwise prompts. Caches the choice .
136+ --- Fetch sessions. Always auto -picks the top session (session ambiguity is per-instance, not surfaced) .
133137--- @param port string
134- --- @param auto boolean
135138--- @param window string ?
136139--- @param callback fun ( session_id : string ?)
137- local function resolve_session (port , auto , window , callback )
138- if cache and cache .port == port and (vim .uv .now () - cache .timestamp ) < CACHE_TTL_MS then
140+ local function resolve_session (port , window , callback )
141+ if cache and cache .port == port and cache . session_id ~= ' ' and (vim .uv .now () - cache .timestamp ) < CACHE_TTL_MS then
139142 callback (cache .session_id )
140143 return
141144 end
@@ -156,34 +159,20 @@ local function resolve_session(port, auto, window, callback)
156159 return
157160 end
158161
159- if auto or # sessions == 1 then
162+ -- Always auto-pick the top session; update cache if we're in multi-instance mode
163+ if cache and cache .port == port then
160164 cache = { port = port , session_id = sessions [1 ].id , window = window , timestamp = vim .uv .now () }
161- callback (sessions [1 ].id )
162- return
163165 end
164-
165- vim .schedule (function ()
166- vim .ui .select (sessions , {
167- prompt = ' Pick session:' ,
168- format_item = function (s ) return s .title or s .slug or s .id end ,
169- }, function (choice )
170- if not choice then
171- callback (nil )
172- return
173- end
174- cache = { port = port , session_id = choice .id , window = window , timestamp = vim .uv .now () }
175- callback (choice .id )
176- end )
177- end )
166+ callback (sessions [1 ].id )
178167 end )
179168end
180169
181170--- Resolve port + session, then call back with both.
182171--- @param callback fun ( port : string , session_id : string , window : string ?)
183172local function resolve (callback )
184- resolve_port (function (port , single_instance , window )
173+ resolve_port (function (port , window )
185174 if not port then return end
186- resolve_session (port , single_instance , window , function (session_id )
175+ resolve_session (port , window , function (session_id )
187176 if not session_id then return end
188177 callback (port , session_id , window )
189178 end )
0 commit comments