From 6a4f1a1185dd4a7d316376491ff814eea83b9cc0 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Thu, 14 May 2026 00:19:44 -0500 Subject: fix(ai-vterm): force buffer swap after F9 toggle-off in lone window The original fix (9600611) set a flag at toggle-off and let the next toggle-on detect it. The flag mechanism is right, but the toggle-off itself wasn't observable when bury-buffer couldn't switch the lone window onto a different buffer -- `bury-buffer' falls back to `switch-to-prev-buffer', which no-ops when the window's prev-buffer history contains only the agent itself (common right after a `C-x 1' that cleared the surrounding windows' histories). Without an observable swap, the second F9 found the agent still displayed and routed back through toggle-off, looping the user with no visible effect. Dispatcher now explicitly forces the window onto another buffer (`(other-buffer agent-buf t)`) when the lone window is still showing the agent after `bury-buffer'. The round-trip test now exercises the real `bury-buffer' path instead of simulating it; a new test asserts the lone window's buffer is non-agent after toggle-off. --- modules/ai-vterm.el | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'modules') diff --git a/modules/ai-vterm.el b/modules/ai-vterm.el index 1eae19f3..975374f6 100644 --- a/modules/ai-vterm.el +++ b/modules/ai-vterm.el @@ -710,7 +710,19 @@ AI-vterm buffers without touching the project list." (cond ((one-window-p) (setq cj/--ai-vterm-last-was-bury t) - (bury-buffer (window-buffer win))) + (bury-buffer (window-buffer win)) + ;; `bury-buffer' calls `switch-to-prev-buffer' to swap the + ;; lone window onto another buffer, but that no-ops when the + ;; window's `window-prev-buffers' list only contains the + ;; agent itself (common right after a `C-x 1' that cleared + ;; the other windows' histories). Without an observable swap + ;; the toggle-off appears to do nothing -- a subsequent F9 + ;; finds the agent still displayed and just buries again. + ;; Force the switch when bury's own swap didn't take. + (when (and (window-live-p win) + (cj/--ai-vterm-buffer-p (window-buffer win))) + (with-selected-window win + (switch-to-buffer (other-buffer (window-buffer win) t))))) (t (setq cj/--ai-vterm-last-was-bury nil) (delete-window win))) -- cgit v1.2.3