diff options
| author | Craig Jennings <c@cjennings.net> | 2026-05-07 22:09:15 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2026-05-07 22:09:15 -0500 |
| commit | 1d93e1a6569e4193c2b078a3d5df0bf47eeba9df (patch) | |
| tree | d2a7854143ece485f60da53a466c3508f79829e1 /modules | |
| parent | a41ef9774f6550da446a3ae8fbbcbcd5bf6c23c4 (diff) | |
| download | dotemacs-1d93e1a6569e4193c2b078a3d5df0bf47eeba9df.tar.gz dotemacs-1d93e1a6569e4193c2b078a3d5df0bf47eeba9df.zip | |
fix(ai-vterm): direction-based display + per-project tmux session names
Two post-ship issues blocked practical use of the new launcher.
The display rule used `display-buffer-in-side-window` with `(dedicated . t)`. Side-window dedication caused `set-window-buffer` to error during `buffer-move` (C-M-arrows), which left a half-finished swap with both sides showing the claude buffer. Then `switch-to-buffer` on a non-claude buffer in that dedicated window split instead of replacing.
I rewrote the rule as `display-buffer-reuse-window -> display-buffer-use-some-window -> display-buffer-in-direction (right)`. The resulting window is ordinary, not dedicated, so swap and replace work normally. I also narrowed `vterm-toggle`'s broad lambda (which matches any vterm-mode buffer) to exclude `claude [` buffers. Otherwise vterm-toggle's `:defer` made it install last and capture our buffers first with its own bottom-split + dedicated treatment.
The tmux side: vterm's auto-launch hook ran a bare `tmux\n`, so each session got an auto-named one. After an Emacs crash the tmux session would survive but I couldn't find it. A second F9 just spawned another. The launcher now sends `tmux new-session -A -s <basename> -c <dir> '<claude>; exec bash'`. The `-A` reattaches to a same-named session if it already exists. The `exec bash` keeps the tmux window alive if claude itself exits. A `cj/--ai-vterm-suppress-tmux` flag tells the existing vterm hook to skip its bare tmux step so the named launch runs instead.
11 new tests across 2 files cover the session-name and launch-command helpers. I updated tests for show-or-create and the display rule. All 34 ai-vterm tests are green.
Diffstat (limited to 'modules')
| -rw-r--r-- | modules/ai-vterm.el | 51 | ||||
| -rw-r--r-- | modules/eshell-vterm-config.el | 10 |
2 files changed, 55 insertions, 6 deletions
diff --git a/modules/ai-vterm.el b/modules/ai-vterm.el index afda4f44..4d83127a 100644 --- a/modules/ai-vterm.el +++ b/modules/ai-vterm.el @@ -32,6 +32,15 @@ :type 'string :group 'ai-vterm) +(defvar cj/--ai-vterm-suppress-tmux nil + "When non-nil, the generic vterm tmux-launch hook skips its auto-tmux step. + +ai-vterm dynamically binds this around `(vterm)' so the hook in +eshell-vterm-config.el doesn't send a bare \"tmux\\n\" before the named +session launch command runs. The hook reads the variable via +`bound-and-true-p' so loading order between the two modules doesn't +matter.") + (defcustom cj/ai-vterm-project-roots (list (expand-file-name "~/.emacs.d")) "Directories that are themselves Claude-template projects. @@ -58,6 +67,33 @@ breaks routing to the right-side window." (format "claude [%s]" (file-name-nondirectory (directory-file-name dir)))) +(defun cj/--ai-vterm-tmux-session-name (dir) + "Return the tmux name derived from project directory DIR. + +The basename of DIR, with any run of whitespace collapsed to a single +hyphen so the result is safe to pass on a tmux command line." + (replace-regexp-in-string + "[[:space:]]+" "-" + (file-name-nondirectory (directory-file-name dir)))) + +(defun cj/--ai-vterm-launch-command (dir) + "Return the shell command line that runs Claude in a project tmux session. + +Uses `tmux new-session -A' so a second F9 on the same project reattaches +to the running session instead of spawning a new one. The session name +is the project's basename via `cj/--ai-vterm-tmux-session-name'. + +The shell command run on first creation is + <claude-command>; exec bash +so the tmux window survives Claude exiting -- the session stays alive +with a bare bash prompt for recovery, and reattach works the same way." + (let ((session (cj/--ai-vterm-tmux-session-name dir)) + (start-dir (expand-file-name dir))) + (format "tmux new-session -A -s %s -c %s '%s'" + (shell-quote-argument session) + (shell-quote-argument start-dir) + (concat cj/ai-vterm-claude-command "; exec bash")))) + (defun cj/--ai-vterm-has-marker-p (dir) "Return non-nil when DIR contains .ai/protocols.org." (file-exists-p (expand-file-name ".ai/protocols.org" dir))) @@ -131,8 +167,14 @@ window an ordinary window so all the standard window commands work." 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 -no such buffer exists, create a new vterm in DIR and send -`cj/ai-vterm-claude-command' to it. +no such buffer exists, create a new vterm in DIR and send the +project's tmux launch command (see `cj/--ai-vterm-launch-command') so +the same project basename reattaches across Emacs restarts. + +The dynamic binding of `cj/--ai-vterm-suppress-tmux' around `(vterm)' +suppresses the generic tmux-launch hook in eshell-vterm-config.el so +it doesn't fire a bare \"tmux\\n\" before the project-named launch +command runs. Returns the buffer." (let ((existing (get-buffer name))) @@ -143,11 +185,12 @@ Returns the buffer." (t (when existing (kill-buffer existing)) - (let ((default-directory dir)) + (let ((default-directory dir) + (cj/--ai-vterm-suppress-tmux t)) (vterm name)) (let ((buf (get-buffer name))) (with-current-buffer buf - (vterm-send-string cj/ai-vterm-claude-command) + (vterm-send-string (cj/--ai-vterm-launch-command dir)) (vterm-send-return)) (display-buffer buf) buf))))) diff --git a/modules/eshell-vterm-config.el b/modules/eshell-vterm-config.el index 4c22944b..31c3a96e 100644 --- a/modules/eshell-vterm-config.el +++ b/modules/eshell-vterm-config.el @@ -202,10 +202,16 @@ (display-line-numbers-mode -1)) (defun cj/vterm-launch-tmux () - "Automatically launch tmux in vterm if not already in a tmux session." + "Automatically launch tmux in vterm if not already in a tmux session. + +Skipped when `cj/--ai-vterm-suppress-tmux' is non-nil so the AI-vterm +flow can run its own project-named tmux session instead of a bare, +auto-named one. `bound-and-true-p' keeps this safe whether or not +ai-vterm.el is loaded." (let ((proc (get-buffer-process (current-buffer)))) (when (and proc - (not (getenv "TMUX"))) ; Check if not already in tmux + (not (getenv "TMUX")) ; Check if not already in tmux + (not (bound-and-true-p cj/--ai-vterm-suppress-tmux))) (vterm-send-string "tmux\n")))) :hook ((vterm-mode . cj/turn-off-chrome-for-vterm) |
