#!/usr/bin/env bash set -e script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # Use the current directory (set via display-popup -d) dir="${PWD:-$HOME}" # Use the directory basename as window name window_name=$dir # The client that opened the popup, recorded by the `bind A` binding. Its # current window is the root window A was pressed in; the popup cannot # pass that through itself, so resolve it here before attaching. popup_client=$(tmux display-message -p '#{@agent_popup_client}' 2>/dev/null || true) origin_window="" if [ -n "$popup_client" ]; then origin_window=$(tmux list-clients -F $'#{client_name}\x1f#{window_id}' \ | awk -v FS=$'\x1f' -v client="$popup_client" '$1 == client { print $2; exit }') fi # Create the agent session if it doesn't exist if ! tmux has-session -t agent; then tmux new-session -ds agent -c "$dir" -n "$window_name" "$script_dir/agent-cmd.sh" # Store the directory in a window option for later matching tmux set-window-option -t agent @agent_dir "$dir" fi # Apply session options on every invocation so updates propagate to live sessions tmux set-option -t agent status off tmux set-option -t agent remain-on-exit on tmux set-option -t agent default-command "$script_dir/agent-cmd.sh" # Session-scoped (not -g) so the root-session window changes agent-sync.sh makes # cannot re-trigger it. tmux set-hook -t agent session-window-changed "run -b '$script_dir/agent-sync.sh'" # Search for an existing window with matching directory target_window="" for window_id in $(tmux list-windows -t agent -F '#{window_id}'); do window_dir=$(tmux show-window-option -t "$window_id" -v @agent_dir 2>/dev/null || true) if [ "$window_dir" = "$dir" ]; then target_window="$window_id" break fi done if [ -n "$target_window" ]; then # Remember which root window this agent window came from, refreshed on every # invocation. Recorded *before* selecting, so the sync hook select-window # fires sees this invocation's origin rather than the previous one's. if [ -n "$origin_window" ]; then tmux set-window-option -t "$target_window" @agent_origin_window "$origin_window" fi # Select the existing window tmux select-window -t "$target_window" else # Create a new window; default-command runs agent-cmd.sh tmux new-window -t agent -c "$dir" -n "$window_name" # Store the directory in a window option for later matching tmux set-window-option -t agent @agent_dir "$dir" if [ -n "$origin_window" ]; then tmux set-window-option -t agent @agent_origin_window "$origin_window" fi fi # Prune windows whose recorded directory no longer exists. This runs after the # current directory's window is guaranteed to exist, so the session always # keeps at least one live window and is never killed out from under us. for window_id in $(tmux list-windows -t agent -F '#{window_id}'); do window_dir=$(tmux show-window-option -t "$window_id" -v @agent_dir 2>/dev/null || true) # Skip windows with no recorded directory rather than risk pruning them. [ -n "$window_dir" ] || continue if [ ! -d "$window_dir" ]; then tmux kill-window -t "$window_id" 2>/dev/null || true fi done # Attach to the session tmux attach-session -t agent