Skip to content

Commit 80d8960

Browse files
committed
fix(acp): gracefully clean the queue on interrupt to stop
fix: update message fix: . fix: . fix: . fix: add guard for prompt
1 parent fe792b3 commit 80d8960

4 files changed

Lines changed: 33 additions & 3 deletions

File tree

lua/codecompanion/acp/prompt_builder.lua

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ function PromptBuilder:handle_permission_request(id, params)
201201
if not id or not params then
202202
return
203203
end
204+
204205
local tool_call = params.toolCall
205206
local options = params.options or {}
206207

@@ -248,7 +249,9 @@ function PromptBuilder:handle_error(error)
248249
utils.fire("RequestFinished", self.options)
249250
end
250251

251-
self.connection._active_prompt = nil
252+
if self.connection._active_prompt == self then
253+
self.connection._active_prompt = nil
254+
end
252255
end
253256

254257
---Handle done event from the server
@@ -281,7 +284,9 @@ function PromptBuilder:handle_done(stop_reason)
281284
self.options.status = status
282285
utils.fire("RequestFinished", self.options)
283286
end
284-
self.connection._active_prompt = nil
287+
if self.connection._active_prompt == self then
288+
self.connection._active_prompt = nil
289+
end
285290
end
286291

287292
---Cancel the prompt
@@ -297,7 +302,15 @@ function PromptBuilder:cancel()
297302
utils.fire("RequestFinished", self.options)
298303
end
299304
end
300-
self.connection._active_prompt = nil
305+
306+
-- Keep _active_prompt alive so the agent's completion response can drain
307+
-- through handle_done. Set a timeout to clean up if the agent never responds.
308+
vim.defer_fn(function()
309+
if self.connection._active_prompt == self then
310+
log:debug("[acp::prompt_builder] Cancel timeout: cleaning up active prompt")
311+
self.connection._active_prompt = nil
312+
end
313+
end, 5000)
301314
end
302315

303316
PromptBuilder.new = PromptBuilder.new

lua/codecompanion/interactions/chat/acp/handler.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,11 @@ end
284284
---@param request table
285285
---@return nil
286286
function ACPHandler:handle_permission_request(request)
287+
if self.chat.status == "cancelling" then
288+
request.respond(nil, true)
289+
return
290+
end
291+
287292
local tool_call = request.tool_call
288293

289294
if

lua/codecompanion/interactions/chat/init.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,6 +1222,7 @@ function Chat:submit(opts)
12221222
vim.cmd("stopinsert")
12231223
end
12241224
self.ui:lock_buf()
1225+
self.ui:reset_cursor_state()
12251226
self.header_line = api.nvim_buf_line_count(self.bufnr) + 2 -- this accounts for the LLM header
12261227
end
12271228

@@ -1555,6 +1556,10 @@ function Chat:stop()
15551556
end
15561557

15571558
vim.schedule(function()
1559+
if self.status ~= CONSTANTS.STATUS_CANCELLING then
1560+
return
1561+
end
1562+
15581563
log:debug("Chat request cancelled")
15591564
self:done(nil, nil, nil, nil, { status = "stopped" })
15601565
end)

lua/codecompanion/interactions/chat/ui/init.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,13 @@ function UI:move_cursor(cursor_has_moved)
623623
end
624624
end
625625

626+
---Reset cursor tracking state so auto-scroll resumes on next response
627+
---@return nil
628+
function UI:reset_cursor_state()
629+
self.cursor.has_moved = false
630+
self.cursor.pos = nil
631+
end
632+
626633
---Lock the chat buffer from editing
627634
---@return nil
628635
function UI:lock_buf()

0 commit comments

Comments
 (0)