From 794cb1cfcdf1e74cdc53f73ca3a7f4fb4ca19773 Mon Sep 17 00:00:00 2001 From: "Cate B." <0x6362@users.noreply.github.com> Date: Wed, 25 Mar 2026 09:33:17 -0400 Subject: [PATCH] Fixes #455: unhandled method returns an error, unblocking client --- agent-shell.el | 22 +++++++++++++++------- tests/agent-shell-tests.el | 28 ++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/agent-shell.el b/agent-shell.el index 6e98d333..b86ba47e 100644 --- a/agent-shell.el +++ b/agent-shell.el @@ -1824,13 +1824,21 @@ COMMAND, when present, may be a shell command string or an argv vector." :state state :acp-request acp-request)) (t - (agent-shell--update-fragment - :state state - :block-id "Unhandled Incoming Request" - :body (format "⚠ Unhandled incoming request: \"%s\"" (map-elt acp-request 'method)) - :create-new t - :navigation 'never) - (map-put! state :last-entry-type nil)))) + (let ((method (map-elt acp-request 'method))) + (agent-shell--update-fragment + :state state + :block-id "Unhandled Incoming Request" + :body (format "⚠ Unhandled incoming request: \"%s\"" method) + :create-new t + :navigation 'never) + ;; Send error response to prevent client from hanging. + (acp-send-response + :client (map-elt state :client) + :response `((:request-id . ,(map-elt acp-request 'id)) + (:error . ,(acp-make-error + :code -32601 + :message (format "Method not found: %s" method))))) + (map-put! state :last-entry-type nil))))) (cl-defun agent-shell--extract-buffer-text (&key buffer line limit) "Extract text from BUFFER starting from LINE with optional LIMIT. diff --git a/tests/agent-shell-tests.el b/tests/agent-shell-tests.el index 9263e051..049fa811 100644 --- a/tests/agent-shell-tests.el +++ b/tests/agent-shell-tests.el @@ -1965,6 +1965,34 @@ code block content (should-not responded) (should (equal (map-elt state :last-entry-type) "session/request_permission")))))) +(ert-deftest agent-shell--on-request-sends-error-for-unhandled-method-test () + "Test `agent-shell--on-request' responds with an error for unknown methods." + (with-temp-buffer + (let* ((captured-response nil) + (state `((:buffer . ,(current-buffer)) + (:client . test-client) + (:event-subscriptions . nil) + (:last-entry-type . "previous-entry")))) + (cl-letf (((symbol-function 'agent-shell--update-fragment) + (lambda (&rest _))) + ((symbol-function 'acp-send-response) + (lambda (&rest args) + (setq captured-response (plist-get args :response)))) + ((symbol-function 'acp-make-error) + (lambda (&rest args) + `((:code . ,(plist-get args :code)) + (:message . ,(plist-get args :message)))))) + (agent-shell--on-request + :state state + :acp-request '((id . "req-404") + (method . "unknown/method"))) + (should (equal (map-elt captured-response :request-id) "req-404")) + (let ((error (map-elt captured-response :error))) + (should (equal (map-elt error :code) -32601)) + (should (equal (map-elt error :message) + "Method not found: unknown/method"))) + (should-not (map-elt state :last-entry-type)))))) + ;;; Tests for agent-shell-show-context-usage-indicator (ert-deftest agent-shell--context-usage-indicator-bar-test ()