branch: elpa/aidermacs commit b52e987d5f7f275b12e048766e1cb150c1c9a9b0 Author: Adam Niederer <adam.niede...@gmail.com> Commit: Matthew Zeng <matthew...@gmail.com>
Don't send commands to aider if it's not ready for them This can occur when e.g. the autoupdate prompt runs - we send a command, aider is expecting a yes/no, the command doesn't run and comint sometimes throws some error. Instead, ask the user to resolve whatever aider is asking of them first. --- aidermacs-backend-comint.el | 3 +++ aidermacs-backend-vterm.el | 5 ++++- aidermacs.el | 39 ++++++++++++++++++++++++++------------- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/aidermacs-backend-comint.el b/aidermacs-backend-comint.el index 92d3c020c6..c72924dde6 100644 --- a/aidermacs-backend-comint.el +++ b/aidermacs-backend-comint.el @@ -124,6 +124,7 @@ are next states.") ;; Check if the output contains a prompt (when (string-match-p aidermacs-prompt-regexp aidermacs--comint-output-temp) (aidermacs--store-output aidermacs--comint-output-temp) + (setq-local aidermacs--ready t) ;; Check if any files were edited and show ediff if needed (let ((edited-files (aidermacs--detect-edited-files))) (if edited-files @@ -316,6 +317,7 @@ BUFFER-NAME is the name for the aidermacs buffer." (unless (comint-check-proc buffer-name) (apply #'make-comint-in-buffer "aidermacs" buffer-name program nil args) (with-current-buffer buffer-name + (setq-local aidermacs--ready nil) (aidermacs-comint-mode) (setq aidermacs--syntax-work-buffer (get-buffer-create (concat " *aidermacs-syntax" buffer-name))))))) @@ -326,6 +328,7 @@ BUFFER is the target buffer. COMMAND is the text to send." (with-current-buffer buffer (let ((process (get-buffer-process buffer)) (inhibit-read-only t)) + (setq-local aidermacs--ready nil) (goto-char (process-mark process)) (aidermacs-reset-font-lock-state) (insert (propertize command diff --git a/aidermacs-backend-vterm.el b/aidermacs-backend-vterm.el index a7dbc53f5b..4cc23ac2cd 100644 --- a/aidermacs-backend-vterm.el +++ b/aidermacs-backend-vterm.el @@ -110,6 +110,7 @@ If the finish sequence is detected, store the output via ;; If we found a shell prompt indicating output finished (when (string-match-p expected prompt-line) (aidermacs--store-output (string-trim output)) + (setq-local aidermacs--ready t) (let ((edited-files (aidermacs--detect-edited-files))) ;; Check if any files were edited and show ediff if needed (if edited-files @@ -250,7 +251,8 @@ BUFFER-NAME is the name for the vterm buffer." (with-current-buffer (vterm-other-window) (setq-local vterm-max-scrollback 1000 aidermacs--vterm-active-timer nil - aidermacs--vterm-last-check-point nil) + aidermacs--vterm-last-check-point nil + aidermacs--ready t) (aidermacs-vterm-mode 1) ;; Add cleanup hook (add-hook 'kill-buffer-hook #'aidermacs--vterm-cleanup nil t)))) @@ -260,6 +262,7 @@ BUFFER-NAME is the name for the vterm buffer." "Send command to the aidermacs vterm buffer. BUFFER is the target buffer to send to. COMMAND is the text to send." (with-current-buffer buffer + (setq-local aidermacs--ready nil) ;; Cancel any existing timer to prevent resource leaks (aidermacs--maybe-cancel-active-timer) ;; Only process if we have a non-empty command diff --git a/aidermacs.el b/aidermacs.el index 93bca39f4c..c883c75cc0 100644 --- a/aidermacs.el +++ b/aidermacs.el @@ -55,6 +55,10 @@ "Buffer-local variable to track the current aidermacs mode. Possible values: `code', `ask', `architect', `help'.") +(defvar-local aidermacs--ready nil + "Buffer-local variable to track whether aider is ready to accept +commands: `nil', `t'") + (defcustom aidermacs-use-architect-mode nil "If non-nil, use separate Architect/Editor mode." :type 'boolean) @@ -266,6 +270,11 @@ If supplied, SUFFIX is appended to the buffer name within the earmuffs." (file-truename display-root) (or suffix ""))))) +(defun aidermacs--live-p (buffer-name) + "Return t if the aider buffer is availble and process is currently running" + (and (get-buffer buffer-name) + (process-live-p (get-buffer-process (or buffer-name (aidermacs-get-buffer-name)))))) + ;;;###autoload (defun aidermacs-run () "Run aidermacs process using the selected backend. @@ -330,8 +339,7 @@ This function sets up the appropriate arguments and launches the process." '("--subtree-only"))))) ;; Take the original aidermacs-extra-args instead of the flat ones (final-args (append backend-args aidermacs-extra-args))) - (if (and (get-buffer buffer-name) - (process-live-p (get-buffer-process buffer-name))) + (if (aidermacs--live-p buffer-name) (aidermacs-switch-to-buffer buffer-name) (aidermacs-run-backend aidermacs-program final-args buffer-name) (with-current-buffer buffer-name @@ -365,8 +373,7 @@ If USE-EXISTING is non-nil, use an existing buffer instead of creating new. If REDIRECT is non-nil it redirects the output (hidden) for comint backend. If CALLBACK is non-nil it will be called after the command finishes." (let* ((buffer-name (aidermacs-get-buffer-name use-existing)) - (buffer (if (and (get-buffer buffer-name) - (process-live-p (get-buffer-process buffer-name))) + (buffer (if (aidermacs--live-p buffer-name) (get-buffer buffer-name) (when (get-buffer buffer-name) (kill-buffer buffer-name)) @@ -376,15 +383,21 @@ If CALLBACK is non-nil it will be called after the command finishes." (processed-command (aidermacs--process-message-if-multi-line command))) ;; Check if command may edit files and prepare accordingly (with-current-buffer buffer - ;; Reset current output before sending new command - (setq aidermacs--current-output "") - (setq aidermacs--current-callback callback) - (setq aidermacs--last-command processed-command) - (aidermacs--cleanup-temp-buffers) - (aidermacs--ensure-current-file-tracked) - (when (aidermacs--command-may-edit-files command) - (aidermacs--prepare-for-code-edit)) - (aidermacs--send-command-backend buffer processed-command redirect)) + ;; Attempt to wait out transient commands or server lag + (when (not aidermacs--ready) + (sit-for 0.5)) + (if (not aidermacs--ready) + (progn (aidermacs-switch-to-buffer buffer-name) + (message "Aider process is not currently accepting commands")) + ;; Reset current output before sending new command + (setq aidermacs--current-output "") + (setq aidermacs--current-callback callback) + (setq aidermacs--last-command processed-command) + (aidermacs--cleanup-temp-buffers) + (aidermacs--ensure-current-file-tracked) + (when (aidermacs--command-may-edit-files command) + (aidermacs--prepare-for-code-edit)) + (aidermacs--send-command-backend buffer processed-command redirect))) (when (and (not no-switch-to-buffer) (not (string= (buffer-name) buffer-name))) (aidermacs-switch-to-buffer buffer-name))))