diff --git a/agent-cmd.sh b/agent-cmd.sh index cc317c0..fab0aab 100755 --- a/agent-cmd.sh +++ b/agent-cmd.sh @@ -27,9 +27,19 @@ fi # Brief delay to let tmux update pane status sleep 0.1 -# Count panes still running (pane_dead=0) -# Note: our own pane counts as running since this script is executing -running=$(tmux list-panes -F '#{pane_dead}' | grep -c '^0$' || true) +window_id=$(tmux display-message -p '#{window_id}') +my_pane=$(tmux display-message -p '#{pane_id}') -# If we're the only pane still running, all others are dead - detach -[ "$running" -le 1 ] && tmux detach-client +# Count live sibling panes (not us, not dead) +other_live=$(tmux list-panes -t "$window_id" -F '#{pane_id} #{pane_dead}' \ + | awk -v me="$my_pane" '$1 != me && $2 == "0"' | wc -l | tr -d ' ') + +if [ "$other_live" -eq 0 ]; then + # No live siblings — close the popup and kill the window + # (also cleans up any dead siblings). + tmux detach-client + tmux kill-window -t "$window_id" 2>/dev/null || true +else + # User has split off other live panes — only kill ours, keep popup open. + tmux kill-pane 2>/dev/null || true +fi