From 75403c927ffd6e76a4f2571312bcb34481e32625 Mon Sep 17 00:00:00 2001 From: David Chen Date: Thu, 9 Apr 2026 10:07:43 -0700 Subject: [PATCH] agent tracker + tmux updates --- .tmux.conf | 12 ++++++------ agent-tracker/agent-config.json | 4 ++-- agent-tracker/cmd/agent/main.go | 33 +++++++++++++++++++++++++-------- tmux/scripts/new_session.sh | 9 ++++++++- tmux/scripts/open_shell_here.sh | 26 ++++++++++++++++++++++++++ 5 files changed, 67 insertions(+), 17 deletions(-) create mode 100755 tmux/scripts/open_shell_here.sh diff --git a/.tmux.conf b/.tmux.conf index 95ee021..f9f87fa 100644 --- a/.tmux.conf +++ b/.tmux.conf @@ -112,11 +112,11 @@ set -g display-time 2000 # create session bind C-c new-session bind O run-shell "~/.config/tmux/scripts/restart_opencode_pane.sh \"#{pane_id}\"" -bind -n M-S run-shell "~/.config/tmux/scripts/new_session.sh #{q:session_id}" +bind -n M-S run-shell "~/.config/tmux/scripts/new_session.sh #{q:session_id} #{q:pane_current_path}" bind -n M-O break-pane # window management -bind -n M-o new-window -a -c "#{pane_current_path}" +bind -n M-o run-shell "~/.config/tmux/scripts/open_shell_here.sh new-window '#{window_id}' '#{pane_current_path}' -a" bind -n M-Q kill-pane bind . command-prompt -p "Rename session to:" "run-shell \"~/.config/tmux/scripts/rename_session_prompt.sh '%%'\"" unbind , @@ -159,10 +159,10 @@ bind -n M-& join-pane -t :7 bind -n M-* join-pane -t :8 bind -n M-( join-pane -t :9 -bind u split-window -vb -c "#{pane_current_path}" -bind e split-window -v -c "#{pane_current_path}" -bind n split-window -hb -c "#{pane_current_path}" -bind i split-window -h -c "#{pane_current_path}" +bind u run-shell "~/.config/tmux/scripts/open_shell_here.sh split '#{pane_id}' '#{pane_current_path}' -v -b" +bind e run-shell "~/.config/tmux/scripts/open_shell_here.sh split '#{pane_id}' '#{pane_current_path}' -v" +bind n run-shell "~/.config/tmux/scripts/open_shell_here.sh split '#{pane_id}' '#{pane_current_path}' -h -b" +bind i run-shell "~/.config/tmux/scripts/open_shell_here.sh split '#{pane_id}' '#{pane_current_path}' -h" bind -n M-f resize-pane -Z diff --git a/agent-tracker/agent-config.json b/agent-tracker/agent-config.json index 95d4ccb..6576e17 100644 --- a/agent-tracker/agent-config.json +++ b/agent-tracker/agent-config.json @@ -15,14 +15,14 @@ "help": "?", "focus_ai": "M-a", "focus_git": "M-g", - "focus_dashboard": "M-s", "focus_run": "M-r" }, "devices": [ "web-server", "ipm", "opm", - "macos" + "macos", + "ipp" ], "status_right": { "cpu": false, diff --git a/agent-tracker/cmd/agent/main.go b/agent-tracker/cmd/agent/main.go index 9b7491d..e82d92d 100644 --- a/agent-tracker/cmd/agent/main.go +++ b/agent-tracker/cmd/agent/main.go @@ -1339,15 +1339,24 @@ func activeAgentWindowID(record *agentRecord) string { return windowID } -func launchAgentLayout(record *agentRecord) error { +func launchAgentLayout(record *agentRecord) (err error) { + previousWindowID := currentTmuxWindowID() + cleanupWindow := false + windowID := "" windowID, sessionID, sessionName, attachAfter, err := createWindow(record.Name, record.RepoCopyPath, record.LaunchWindowID) if err != nil { return err } - - if err := runTmux("select-window", "-t", windowID); err != nil { - return err - } + cleanupWindow = true + defer func() { + if err == nil || !cleanupWindow || strings.TrimSpace(windowID) == "" { + return + } + _ = runTmux("kill-window", "-t", windowID) + if strings.TrimSpace(previousWindowID) != "" { + _ = runTmux("select-window", "-t", previousWindowID) + } + }() topPane, err := currentPane(windowID) if err != nil { return err @@ -1400,12 +1409,20 @@ func launchAgentLayout(record *agentRecord) error { return err } } + if attachAfter && canAttachTmux() { + if err := runTmux("attach-session", "-t", sessionID); err != nil { + return err + } + cleanupWindow = false + return nil + } + if err := runTmux("select-window", "-t", windowID); err != nil { + return err + } if err := runTmux("select-pane", "-t", aiPane); err != nil { return err } - if attachAfter && canAttachTmux() { - return runTmux("attach-session", "-t", sessionID) - } + cleanupWindow = false return nil } diff --git a/tmux/scripts/new_session.sh b/tmux/scripts/new_session.sh index 6eae33d..04b5267 100755 --- a/tmux/scripts/new_session.sh +++ b/tmux/scripts/new_session.sh @@ -2,9 +2,16 @@ LOCK="/tmp/tmux-new-session.lock" current_session_id="${1:-}" +current_path="${2:-}" touch "$LOCK" -session_id=$(tmux new-session -d -P -s 'session' -F '#{session_id}' 2>/dev/null) +tmux_args=(new-session -d -P -s 'session' -F '#{session_id}') +if [ -n "$current_path" ]; then + tmux_args+=( -c "$current_path" ) + printf -v start_cmd 'cd %q && exec ${SHELL:-/bin/zsh} -l' "$current_path" + tmux_args+=( "$start_cmd" ) +fi +session_id=$(tmux "${tmux_args[@]}" 2>/dev/null) if [ -z "$session_id" ]; then rm -f "$LOCK" diff --git a/tmux/scripts/open_shell_here.sh b/tmux/scripts/open_shell_here.sh new file mode 100755 index 0000000..ac1ee0f --- /dev/null +++ b/tmux/scripts/open_shell_here.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash +set -euo pipefail + +mode="${1:-}" +target="${2:-}" +target_path="${3:-}" +shift 3 || true + +if [[ -z "$target_path" || ! -d "$target_path" ]]; then + target_path="$HOME" +fi + +printf -v start_cmd 'cd %q && exec ${SHELL:-/bin/zsh} -l' "$target_path" + +case "$mode" in + split) + exec tmux split-window "$@" -t "$target" -c "$target_path" "$start_cmd" + ;; + new-window) + exec tmux new-window "$@" -t "$target" -c "$target_path" "$start_cmd" + ;; + *) + printf 'open_shell_here: unknown mode %s\n' "$mode" >&2 + exit 1 + ;; +esac