Skip to content

Commit 4b7187d

Browse files
committed
fix(core): handle permissions when aborting
Cancel a pending permission if we're aborting Also clear pending permission on server restart
1 parent 097874c commit 4b7187d

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

lua/opencode/core.lua

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,17 +179,30 @@ function M.stop()
179179
if state.windows and state.active_session then
180180
if state.is_running() then
181181
M._abort_count = M._abort_count + 1
182-
state.api_client:abort_session(state.active_session.id):wait()
182+
183+
-- if there's a current permission, reject it
184+
if state.current_permission then
185+
require('opencode.api').permission_deny()
186+
end
187+
188+
local ok, result = pcall(function()
189+
return state.api_client:abort_session(state.active_session.id):wait()
190+
end)
191+
192+
if not ok then
193+
vim.notify('Abort error: ' .. vim.inspect(result))
194+
end
183195

184196
if M._abort_count >= 3 then
185197
vim.notify('Re-starting Opencode server')
186198
M._abort_count = 0
187199
-- close existing server
188-
state.opencode_server:shutdown():wait()
200+
if state.opencode_server then
201+
state.opencode_server:shutdown():wait()
202+
end
189203

190204
-- start a new one
191205
state.opencode_server = nil
192-
state.job_count = 0
193206

194207
-- NOTE: start a new server here to make sure we're subscribed
195208
-- to server events before a user sends a message
@@ -237,7 +250,13 @@ function M.opencode_ok()
237250
return true
238251
end
239252

253+
local function on_opencode_server()
254+
state.current_permission = nil
255+
end
256+
240257
function M.setup()
258+
state.subscribe('opencode_server', on_opencode_server)
259+
241260
vim.schedule(function()
242261
M.opencode_ok()
243262
end)

0 commit comments

Comments
 (0)