#!/usr/bin/env bash # Wrapper that runs an agent command, then detaches if all panes in window are dead # Run the agent command (don't use set -e, agent may exit non-zero) "$@" || true # 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) # If we're the only pane still running, all others are dead - detach [ "$running" -le 1 ] && tmux detach-client