aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2026-07-20 23:44:36 -0500
committerCraig Jennings <c@cjennings.net>2026-07-20 23:44:36 -0500
commite83a055602ea2ad6cf8de5909bcbd8e44f2b5c22 (patch)
treef6475411a629139751d4316e8560e071b4cec270
parent3a39d41f396bdd825074d7264ebb72149b8cd3b9 (diff)
downloaddotemacs-e83a055602ea2ad6cf8de5909bcbd8e44f2b5c22.tar.gz
dotemacs-e83a055602ea2ad6cf8de5909bcbd8e44f2b5c22.zip
perf(ai-term): fetch the tmux session list once per launch
The launch path spawned tmux list-sessions up to three times: in the project picker's sorting, in the launcher's fresh check, and again inside show-or-create. The list is now fetched once and threaded through; the cycling path still fetches its own.
-rw-r--r--modules/ai-term-backend-eat.el8
-rw-r--r--modules/ai-term-sessions.el7
-rw-r--r--modules/ai-term.el10
-rw-r--r--tests/test-ai-term--session-threading.el40
4 files changed, 57 insertions, 8 deletions
diff --git a/modules/ai-term-backend-eat.el b/modules/ai-term-backend-eat.el
index a7781128..906abd00 100644
--- a/modules/ai-term-backend-eat.el
+++ b/modules/ai-term-backend-eat.el
@@ -100,8 +100,11 @@ typed into a bare shell. Returns the poll timer."
(cancel-timer timer))))))
timer))
-(defun cj/--ai-term-show-or-create (dir name &optional agent-command)
+(defun cj/--ai-term-show-or-create (dir name &optional agent-command sessions)
"Show or create the AI-term buffer for project DIR with buffer NAME.
+SESSIONS, when non-nil, is a pre-fetched
+`cj/--ai-term-live-tmux-sessions' list threaded from the caller so the
+launch path pays for the tmux subprocess once.
If a buffer named NAME exists with a live process, display it. If
the buffer exists but its process is dead, kill it and recreate. If
@@ -135,7 +138,8 @@ buffer."
;; session gets the project /color injected below; a reattach carries
;; whatever color the running Claude already has.
(let ((fresh (not (cj/--ai-term-session-active-p
- dir (cj/--ai-term-live-tmux-sessions)))))
+ dir (or sessions
+ (cj/--ai-term-live-tmux-sessions))))))
;; `eat' switches to its buffer in the selected window before our
;; display-buffer-alist rule can route it; `save-window-excursion'
;; reverts that, and the explicit display-buffer below routes the buffer
diff --git a/modules/ai-term-sessions.el b/modules/ai-term-sessions.el
index eb09d2e9..57d735d1 100644
--- a/modules/ai-term-sessions.el
+++ b/modules/ai-term-sessions.el
@@ -351,8 +351,11 @@ the metadata keeps the order ALIST was built in."
(cycle-sort-function . identity))
(complete-with-action action alist string predicate))))
-(defun cj/--ai-term-pick-project ()
+(defun cj/--ai-term-pick-project (&optional sessions)
"Prompt for an AI-agent project; return its absolute path.
+SESSIONS, when non-nil, is a pre-fetched result of
+`cj/--ai-term-live-tmux-sessions', so a caller that already paid for the
+tmux subprocess can thread it through instead of spawning another.
Candidates come from `cj/--ai-term-candidates', ordered by
`cj/--ai-term-sort-candidates' so projects with a live tmux session
@@ -368,7 +371,7 @@ Signals `user-error' when no candidates exist."
(append cj/ai-term-project-roots
cj/ai-term-container-roots)
", ")))
- (let* ((sessions (cj/--ai-term-live-tmux-sessions))
+ (let* ((sessions (or sessions (cj/--ai-term-live-tmux-sessions)))
(sorted (cj/--ai-term-sort-candidates candidates sessions))
(display-alist
(mapcar (lambda (p)
diff --git a/modules/ai-term.el b/modules/ai-term.el
index 37eb61de..52494c18 100644
--- a/modules/ai-term.el
+++ b/modules/ai-term.el
@@ -351,16 +351,18 @@ it runs.
EAT renders in terminal frames as well as GUI frames, so this
launches from either."
(interactive "P")
- (let* ((dir (cj/--ai-term-pick-project))
+ ;; One tmux fetch per launch: the same list feeds the picker's sorting,
+ ;; the fresh check here, and show-or-create's own fresh check.
+ (let* ((sessions (cj/--ai-term-live-tmux-sessions))
+ (dir (cj/--ai-term-pick-project sessions))
(name (cj/--ai-term-buffer-name dir))
(existing (get-buffer name))
(fresh (and (not (and existing
(cj/--ai-term-process-live-p existing)))
- (not (cj/--ai-term-session-active-p
- dir (cj/--ai-term-live-tmux-sessions)))))
+ (not (cj/--ai-term-session-active-p dir sessions))))
(command (when fresh
(cj/--ai-term-runtime-command (cj/--ai-term-pick-runtime))))
- (buf (cj/--ai-term-show-or-create dir name command)))
+ (buf (cj/--ai-term-show-or-create dir name command sessions)))
(unless arg
(let ((win (get-buffer-window buf)))
(when win (select-window win))))
diff --git a/tests/test-ai-term--session-threading.el b/tests/test-ai-term--session-threading.el
new file mode 100644
index 00000000..984f76d7
--- /dev/null
+++ b/tests/test-ai-term--session-threading.el
@@ -0,0 +1,40 @@
+;;; test-ai-term--session-threading.el --- Session-list threading tests -*- lexical-binding: t; -*-
+
+;;; Commentary:
+;; The launch path used to call cj/--ai-term-live-tmux-sessions (a tmux
+;; subprocess) once in the project picker, again for the launcher's fresh
+;; check, and a third time inside show-or-create. One fetch per launch,
+;; threaded through, is the contract pinned here.
+
+;;; Code:
+
+(require 'ert)
+(require 'cl-lib)
+(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory))
+(require 'ai-term)
+
+(ert-deftest test-ai-term-launch-fetches-tmux-sessions-once ()
+ "Normal: one tmux session fetch per launch, threaded to the picker and
+show-or-create rather than re-spawned by each."
+ (let ((fetches 0) received-sessions)
+ (cl-letf (((symbol-function 'cj/--ai-term-live-tmux-sessions)
+ (lambda () (setq fetches (1+ fetches)) '("other")))
+ ((symbol-function 'cj/--ai-term-candidates)
+ (lambda () '("/tmp/proj/")))
+ ((symbol-function 'completing-read)
+ (lambda (_prompt table &rest _)
+ (car (all-completions "" table))))
+ ((symbol-function 'get-buffer) (lambda (_n) nil))
+ ((symbol-function 'cj/--ai-term-pick-runtime) (lambda () 'claude))
+ ((symbol-function 'cj/--ai-term-runtime-command) (lambda (_r) "cmd"))
+ ((symbol-function 'cj/--ai-term-show-or-create)
+ (lambda (_dir _name _cmd &optional sessions)
+ (setq received-sessions sessions)
+ (current-buffer)))
+ ((symbol-function 'get-buffer-window) (lambda (&rest _) nil)))
+ (cj/ai-term-pick-project t)
+ (should (= fetches 1))
+ (should (equal received-sessions '("other"))))))
+
+(provide 'test-ai-term--session-threading)
+;;; test-ai-term--session-threading.el ends here