Skip to content

Commit 7f8774a

Browse files
authored
Don't kill Pi sessions silently from the input buffer (#214)
Killing the input buffer with C-x k now gives the same chance to cancel as killing the chat buffer, instead of silently terminating the running pi process.
1 parent b4a33be commit 7f8774a

2 files changed

Lines changed: 67 additions & 0 deletions

File tree

pi-coding-agent-input.el

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,16 @@ For backward search: go to current input (nil index)."
230230

231231
;;;; Input Mode
232232

233+
(defun pi-coding-agent--input-kill-buffer-query ()
234+
"Ask before killing input when its linked chat owns a live process."
235+
(let ((proc (and (buffer-live-p pi-coding-agent--chat-buffer)
236+
(buffer-local-value 'pi-coding-agent--process
237+
pi-coding-agent--chat-buffer))))
238+
(or (not (and (processp proc)
239+
(process-live-p proc)
240+
(process-query-on-exit-flag proc)))
241+
(yes-or-no-p "Pi session has a running process; kill it? "))))
242+
233243
(define-derived-mode pi-coding-agent-input-mode text-mode "Pi-Input"
234244
"Major mode for composing pi prompts.
235245
Uses tree-sitter markdown highlighting by default while preserving raw
@@ -253,6 +263,8 @@ markup visibility, mode identity, and keybindings. Set
253263
(add-hook 'completion-at-point-functions #'pi-coding-agent--path-capf nil t)
254264
(add-hook 'post-self-insert-hook #'pi-coding-agent--maybe-complete-at nil t)
255265
(add-hook 'isearch-mode-hook #'pi-coding-agent--history-isearch-setup nil t)
266+
(add-hook 'kill-buffer-query-functions
267+
#'pi-coding-agent--input-kill-buffer-query nil t)
256268
(add-hook 'kill-buffer-hook #'pi-coding-agent--cleanup-input-on-kill nil t))
257269

258270
;;;; Input-Buffer Chat Navigation

test/pi-coding-agent-menu-test.el

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,61 @@ BINDING-SPEC is (DIR CHAT-NAME INPUT-NAME PROC). DIR is evaluated once."
349349
(should-not (get-buffer chat-name))
350350
(should-not (get-buffer input-name)))))
351351

352+
(ert-deftest pi-coding-agent-test-kill-chat-cancelled-preserves-session ()
353+
"Killing chat buffer asks before terminating its live process."
354+
(let ((prompt-count 0))
355+
(pi-coding-agent-test--with-quit-confirmable-session
356+
("/tmp/pi-coding-agent-test-kill-chat-cancel/" chat-name input-name proc)
357+
;; GUI test helpers disable this globally; this test needs the default
358+
;; Emacs process-buffer query to exercise the chat-buffer contract.
359+
(let ((kill-buffer-query-functions
360+
(if (memq #'process-kill-buffer-query-function
361+
kill-buffer-query-functions)
362+
kill-buffer-query-functions
363+
(cons #'process-kill-buffer-query-function
364+
kill-buffer-query-functions))))
365+
(cl-letf (((symbol-function 'yes-or-no-p)
366+
(lambda (_)
367+
(cl-incf prompt-count)
368+
nil)))
369+
(should-not (kill-buffer chat-name))))
370+
(should (= prompt-count 1))
371+
(should (get-buffer chat-name))
372+
(should (get-buffer input-name))
373+
(should (process-live-p proc))
374+
(should (process-query-on-exit-flag proc)))))
375+
376+
(ert-deftest pi-coding-agent-test-kill-input-cancelled-preserves-session ()
377+
"Killing input buffer asks before terminating the linked live process."
378+
(let ((prompt-count 0))
379+
(pi-coding-agent-test--with-quit-confirmable-session
380+
("/tmp/pi-coding-agent-test-kill-input-cancel/" chat-name input-name proc)
381+
(cl-letf (((symbol-function 'yes-or-no-p)
382+
(lambda (_)
383+
(cl-incf prompt-count)
384+
nil)))
385+
(should-not (kill-buffer input-name)))
386+
(should (= prompt-count 1))
387+
(should (get-buffer chat-name))
388+
(should (get-buffer input-name))
389+
(should (process-live-p proc))
390+
(should (process-query-on-exit-flag proc)))))
391+
392+
(ert-deftest pi-coding-agent-test-kill-input-confirmed-kills-session ()
393+
"Confirming input buffer kill terminates the linked session once."
394+
(let ((prompt-count 0))
395+
(pi-coding-agent-test--with-quit-confirmable-session
396+
("/tmp/pi-coding-agent-test-kill-input-confirm/" chat-name input-name proc)
397+
(cl-letf (((symbol-function 'yes-or-no-p)
398+
(lambda (_)
399+
(cl-incf prompt-count)
400+
t)))
401+
(should (kill-buffer input-name)))
402+
(should (= prompt-count 1))
403+
(should-not (get-buffer chat-name))
404+
(should-not (get-buffer input-name))
405+
(should-not (process-live-p proc)))))
406+
352407
(ert-deftest pi-coding-agent-test-kill-chat-kills-input ()
353408
"Killing chat buffer also kills input buffer."
354409
(pi-coding-agent-test-with-mock-session "/tmp/pi-coding-agent-test-linked/"

0 commit comments

Comments
 (0)