diff --git a/ai-code-backends-infra-eat.el b/ai-code-backends-infra-eat.el index 7122bcfd..4872bae0 100644 --- a/ai-code-backends-infra-eat.el +++ b/ai-code-backends-infra-eat.el @@ -26,6 +26,8 @@ a more stable viewing experience when working with multiple windows." "ai-code-backends-infra" ()) (declare-function ai-code-backends-infra--note-program-status-output "ai-code-backends-infra" (process output)) +(declare-function ai-code-backends-infra--reset-program-status-state + "ai-code-backends-infra" ()) (declare-function ai-code-backends-infra--output-meaningful-p "ai-code-backends-infra" (output)) (declare-function ai-code-backends-infra--set-session-directory @@ -143,6 +145,7 @@ ENV-VARS are extra environment variables for the terminal process." (setq-local default-directory working-dir) (setq-local ai-code-backends-infra--session-terminal-backend 'eat) (unless (derived-mode-p 'eat-mode) (eat-mode)) + (ai-code-backends-infra--reset-program-status-state) (when ai-code-backends-infra-eat-preserve-position (setq-local eat--synchronize-scroll-function #'ai-code-backends-infra--eat-terminal-position-keeper)) diff --git a/ai-code-backends-infra.el b/ai-code-backends-infra.el index a4c9d77f..78a75d96 100644 --- a/ai-code-backends-infra.el +++ b/ai-code-backends-infra.el @@ -443,7 +443,13 @@ response; an idle report without a busy one before it does nothing." "Reply to an OSC 7501 support query: the protocol is supported.") (defconst ai-code-backends-infra--program-status-max-length 4096 - "Longest OSC 7501 sequence kept while waiting for its end.") + "Maximum length of an OSC 7501 sequence accepted or buffered.") + +(defconst ai-code-backends-infra--program-status-max-records 256 + "Maximum number of OSC 7501 records retained per session.") + +(defconst ai-code-backends-infra--program-status-id-max-length 128 + "Maximum length of an OSC 7501 record id.") (defconst ai-code-backends-infra--program-status-id-regexp "\\`[A-Za-z0-9_.+-]\\{1,32\\}\\(?:/[A-Za-z0-9_.+-]\\{1,32\\}\\)\\{0,7\\}\\'" @@ -456,35 +462,61 @@ response; an idle report without a busy one before it does nothing." "OSC 7501 records the program reported, most recent first. Each entry is (ID . STATE), ID being \"\" for the root record.") +(defun ai-code-backends-infra--reset-program-status-state () + "Reset response-tracking and OSC 7501 state in the current buffer." + (when ai-code-backends-infra--idle-timer + (cancel-timer ai-code-backends-infra--idle-timer) + (setq ai-code-backends-infra--idle-timer nil)) + (setq ai-code-backends-infra--response-seen nil + ai-code-backends-infra--last-meaningful-output-time nil + ai-code-backends-infra--busy-signal nil + ai-code-backends-infra--program-status-pending "" + ai-code-backends-infra--program-status-records nil)) + (defun ai-code-backends-infra--program-status-parse (body) "Return the report in OSC 7501 BODY as (ID . STATE), or nil. STATE is `clear' or a program state. A report with an unknown state or an invalid id is ignored, as the protocol requires." (let (state id) (dolist (pair (split-string body ":")) - (when (string-match "\\`[ \t]*\\([a-z]+\\)[ \t]*=[ \t]*\\(.*?\\)[ \t]*\\'" pair) + (when (string-match + "\\`[ \t]*\\([a-z]+\\)[ \t]*=[ \t]*\\(\\(?:.\\|\n\\)*?\\)[ \t]*\\'" + pair) (pcase (match-string 1 pair) ("state" (setq state (match-string 2 pair))) ("id" (setq id (match-string 2 pair)))))) (when (and (member state '("idle" "working" "done" "blocked" "error" "clear")) (or (null id) - (string-match-p ai-code-backends-infra--program-status-id-regexp - id))) + (and (<= (length id) + ai-code-backends-infra--program-status-id-max-length) + (string-match-p ai-code-backends-infra--program-status-id-regexp + id)))) (cons (or id "") (intern state))))) (defun ai-code-backends-infra--program-status-apply (id state) "Apply the OSC 7501 report of STATE for record ID to the current session. `clear' removes the record and the records below it; for the root record -that is every record." - (let ((records (cl-remove-if - (lambda (entry) - (or (string= (car entry) id) - (and (eq state 'clear) - (or (string= id "") - (string-prefix-p (concat id "/") (car entry)))))) - ai-code-backends-infra--program-status-records))) +that is every record. Retain at most +`ai-code-backends-infra--program-status-max-records' records, preserving +the root record while dropping the least recently updated child entries." + (let* ((max-records ai-code-backends-infra--program-status-max-records) + (records (cl-remove-if + (lambda (entry) + (or (string= (car entry) id) + (and (eq state 'clear) + (or (string= id "") + (string-prefix-p (concat id "/") (car entry)))))) + ai-code-backends-infra--program-status-records)) + (updated (if (eq state 'clear) records (cons (cons id state) records)))) (setq ai-code-backends-infra--program-status-records - (if (eq state 'clear) records (cons (cons id state) records))))) + (if (> (length updated) max-records) + (let ((kept (seq-take updated max-records)) + (root (assoc "" updated))) + (if (or (null root) (assoc "" kept)) + kept + (append (seq-take updated (max 0 (1- max-records))) + (list root)))) + updated)))) (defun ai-code-backends-infra--program-status-state () "Return the program state the current session shows, or nil. @@ -527,22 +559,24 @@ a short turn reported as working then done still completes." ((and (eq (aref output end) ?\e) (= (1+ end) (length output))) (setq pending (substring output start))) ((or (eq (aref output end) ?\a) (eq (aref output (1+ end)) ?\\)) - (let ((body (substring output body-start end))) - (if (string-prefix-p "?" body) - (when (process-live-p process) - (process-send-string - process ai-code-backends-infra--program-status-reply)) - (when-let* ((report (ai-code-backends-infra--program-status-parse body))) - (ai-code-backends-infra--program-status-apply - (car report) (cdr report)) - (let ((state (ai-code-backends-infra--program-status-state))) - (unless (eq state shown) - (setq shown state) - (ai-code-session-attention-note-program-state - (current-buffer) state) - (ai-code-backends-infra-note-busy-state - (current-buffer) (eq state 'working))))))) - (setq start (if (eq (aref output end) ?\a) (1+ end) (+ end 2)))) + (let ((seq-end (if (eq (aref output end) ?\a) (1+ end) (+ end 2)))) + (when (<= (- seq-end start) ai-code-backends-infra--program-status-max-length) + (let ((body (substring output body-start end))) + (if (string= (string-trim body) "?") + (when (process-live-p process) + (process-send-string + process ai-code-backends-infra--program-status-reply)) + (when-let* ((report (ai-code-backends-infra--program-status-parse body))) + (ai-code-backends-infra--program-status-apply + (car report) (cdr report)) + (let ((state (ai-code-backends-infra--program-status-state))) + (unless (eq state shown) + (setq shown state) + (ai-code-session-attention-note-program-state + (current-buffer) state) + (ai-code-backends-infra-note-busy-state + (current-buffer) (eq state 'working)))))))) + (setq start seq-end))) ;; Another escape sequence cuts this one short. (t (setq start end))))) (setq ai-code-backends-infra--program-status-pending diff --git a/test/test_ai-code-backends-infra.el b/test/test_ai-code-backends-infra.el index 266988de..df41051a 100644 --- a/test/test_ai-code-backends-infra.el +++ b/test/test_ai-code-backends-infra.el @@ -867,6 +867,8 @@ BEL and ST both end a report; a repeated state is noted once." (funcall feed "\e]7501;") (funcall feed "?\a") (should (equal replies '("\e]7501;?\e\\" "\e]7501;?\e\\"))) + (funcall feed "\e]7501;?:states=idle,working,done,blocked,error\e\\") + (should (equal replies '("\e]7501;?\e\\" "\e]7501;?\e\\"))) (should-not states))) (ert-deftest test-ai-code-backends-infra-program-status-blocked-child-wins () @@ -884,16 +886,63 @@ BEL and ST both end a report; a repeated state is noted once." (should-not ai-code-backends-infra--program-status-records))) (ert-deftest test-ai-code-backends-infra-program-status-ignores-invalid () - "Unknown states, invalid ids and cut-off sequences change nothing." + "Unknown states, invalid ids, oversized reports and cut-off sequences change nothing." (test-ai-code-backends-infra--with-program-status-session (funcall feed "\e]7501;state=thinking\a") (funcall feed "\e]7501;state=working:id=bad id\a") + (funcall feed "\e]7501;state=working:id=bad\nid\a") + (funcall feed (concat "\e]7501;state=working:id=" + (mapconcat (lambda (_) (make-string 32 ?a)) + (number-sequence 1 5) "/") + "\a")) (funcall feed "\e]7501;state=working\e[0m\a") + (funcall feed (concat "\e]7501;state=working:msg=" (make-string 5000 ?A) "\a")) (should-not states) + (should-not ai-code-backends-infra--program-status-records) (funcall feed (concat "\e]7501;msg=" (make-string 5000 ?A))) (should (equal ai-code-backends-infra--program-status-pending "")) (should-not states))) +(ert-deftest test-ai-code-backends-infra-program-status-caps-records () + "Retain at most `ai-code-backends-infra--program-status-max-records' entries while preserving root." + (test-ai-code-backends-infra--with-program-status-session + (let ((ai-code-backends-infra--program-status-max-records 4)) + (dotimes (i 6) + (funcall feed (format "\e]7501;state=working:id=task-%d\a" i))) + (should (equal (mapcar #'car ai-code-backends-infra--program-status-records) + '("task-5" "task-4" "task-3" "task-2"))))) + ;; Evicting child records must not drop an active root record or flip tab-bar badge / notifications. + (let ((ai-code-session--sessions (make-hash-table :test 'equal)) + (ai-code-backends-infra--program-status-max-records 4) + (notifications nil) + (buffer (get-buffer-create "*claude[test-root-cap]*"))) + (unwind-protect + (cl-letf (((symbol-function 'process-live-p) (lambda (_process) t)) + ((symbol-function 'ai-code-backends-infra--buffer-user-visible-p) + (lambda (_buffer) nil)) + ((symbol-function 'ai-code-notifications-response-ready) + (lambda (&optional backend) (push backend notifications)))) + (let ((session (ai-code-session-register + :buffer buffer :backend 'claude :repo-root "/tmp/proj" + :metadata '(:status "running")))) + (with-current-buffer buffer + (ai-code-backends-infra--note-program-status-output + 'fake-process "\e]7501;state=working\a") + (should (eq (ai-code-session-attention--display-state session) 'working)) + (dotimes (i 6) + (ai-code-backends-infra--note-program-status-output + 'fake-process (format "\e]7501;state=working:id=task-%d\a" i))) + (should (equal (mapcar #'car ai-code-backends-infra--program-status-records) + '("task-5" "task-4" "task-3" ""))) + (should (eq (ai-code-session-attention--display-state session) 'working)) + (should-not notifications) + (ai-code-backends-infra--note-program-status-output + 'fake-process "\e]7501;state=done\a") + (should (eq (ai-code-session-attention--display-state session) 'done)) + (should (equal notifications '("claude")))))) + (when (buffer-live-p buffer) + (kill-buffer buffer))))) + (ert-deftest test-ai-code-backends-infra-vterm-notification-tracker-reads-program-status () "Vterm session output is read for OSC 7501 before vterm renders it." (with-temp-buffer @@ -1523,6 +1572,60 @@ BEL and ST both end a report; a repeated state is noted once." (when (buffer-live-p buffer) (kill-buffer buffer))))) +(ert-deftest test-ai-code-backends-infra-eat-create-session-resets-program-status-state () + "Reusing an Eat buffer clears stale OSC 7501 and busy state from earlier runs." + (let* ((buffer-name "*claude[test-eat-reuse-reset]*") + (buffer (get-buffer-create buffer-name)) + (wrapped-filter nil) + (notifications nil) + (attention-calls nil)) + (unwind-protect + (cl-letf (((symbol-function 'eat-mode) #'ignore) + ((symbol-function 'eat-exec) #'ignore) + ((symbol-function 'get-buffer-process) + (lambda (_buffer) 'eat-proc)) + ((symbol-function 'process-live-p) (lambda (_process) t)) + ((symbol-function 'process-filter) + (lambda (_process) #'ignore)) + ((symbol-function 'set-process-filter) + (lambda (_process filter) (setq wrapped-filter filter))) + ((symbol-function 'ai-code-backends-infra--buffer-user-visible-p) + (lambda (_buffer) nil)) + ((symbol-function 'ai-code-notifications-response-ready) + (lambda (&optional backend) (push backend notifications))) + ((symbol-function 'ai-code-session-attention-note-program-state) + (lambda (_buffer state) (push state attention-calls))) + ((symbol-function 'ai-code-session-link--linkify-recent-output) + #'ignore)) + (with-current-buffer buffer + (setq-local major-mode 'eat-mode)) + (ai-code-backends-infra-eat-create-session + buffer-name default-directory '("claude") nil) + (funcall wrapped-filter 'eat-proc "\e]7501;state=working\a") + (funcall wrapped-filter 'eat-proc "\e]7501;state=blocked:id=tool/1\a") + (funcall wrapped-filter 'eat-proc "\e]7501;state=wor") + (with-current-buffer buffer + ;; Simulate a crash while busy with stale blocked child + pending tail. + (setq ai-code-backends-infra--busy-signal 'busy)) + (setq notifications nil + attention-calls nil) + ;; Reuse the same Eat buffer for a new process. + (ai-code-backends-infra-eat-create-session + buffer-name default-directory '("claude") nil) + (with-current-buffer buffer + (should-not ai-code-backends-infra--busy-signal) + (should-not ai-code-backends-infra--program-status-records) + (should (equal ai-code-backends-infra--program-status-pending ""))) + ;; Startup idle does not notify spuriously; working -> done works cleanly. + (funcall wrapped-filter 'eat-proc "\e]7501;state=idle\a") + (should-not notifications) + (funcall wrapped-filter 'eat-proc "\e]7501;state=working\a") + (funcall wrapped-filter 'eat-proc "\e]7501;state=done\a") + (should (equal (nreverse attention-calls) '(idle working done))) + (should (equal notifications '("claude")))) + (when (buffer-live-p buffer) + (kill-buffer buffer))))) + (ert-deftest test-ai-code-backends-infra--create-terminal-session-eat-intercepts-editor-output () "Eat should remove terminal editor requests before rendering output." (with-temp-buffer