返回 JoyAI-Echo
SKILL.md
1 ---
2 name: tmux
3 description: Remote-control tmux sessions for interactive CLIs by sending keystrokes and scraping pane output.
4 metadata: {"nanobot":{"emoji":"🧵","os":["darwin","linux"],"requires":{"bins":["tmux"]}}}
5 ---
6
7 # tmux Skill
8
9 Use tmux only when you need an interactive TTY. Prefer exec background mode for long-running, non-interactive tasks.
10
11 ## Quickstart (isolated socket, exec tool)
12
13 ```bash
14 SOCKET_DIR="${NANOBOT_TMUX_SOCKET_DIR:-${TMPDIR:-/tmp}/nanobot-tmux-sockets}"
15 mkdir -p "$SOCKET_DIR"
16 SOCKET="$SOCKET_DIR/nanobot.sock"
17 SESSION=nanobot-python
18
19 tmux -S "$SOCKET" new -d -s "$SESSION" -n shell
20 tmux -S "$SOCKET" send-keys -t "$SESSION":0.0 -- 'PYTHON_BASIC_REPL=1 python3 -q' Enter
21 tmux -S "$SOCKET" capture-pane -p -J -t "$SESSION":0.0 -S -200
22 ```
23
24 After starting a session, always print monitor commands:
25
26 ```
27 To monitor:
28 tmux -S "$SOCKET" attach -t "$SESSION"
29 tmux -S "$SOCKET" capture-pane -p -J -t "$SESSION":0.0 -S -200
30 ```
31
32 ## Socket convention
33
34 - Use `NANOBOT_TMUX_SOCKET_DIR` environment variable.
35 - Default socket path: `"$NANOBOT_TMUX_SOCKET_DIR/nanobot.sock"`.
36
37 ## Targeting panes and naming
38
39 - Target format: `session:window.pane` (defaults to `:0.0`).
40 - Keep names short; avoid spaces.
41 - Inspect: `tmux -S "$SOCKET" list-sessions`, `tmux -S "$SOCKET" list-panes -a`.
42
43 ## Finding sessions
44
45 - List sessions on your socket: `{baseDir}/scripts/find-sessions.sh -S "$SOCKET"`.
46 - Scan all sockets: `{baseDir}/scripts/find-sessions.sh --all` (uses `NANOBOT_TMUX_SOCKET_DIR`).
47
48 ## Sending input safely
49
50 - Prefer literal sends: `tmux -S "$SOCKET" send-keys -t target -l -- "$cmd"`.
51 - Control keys: `tmux -S "$SOCKET" send-keys -t target C-c`.
52
53 ## Watching output
54
55 - Capture recent history: `tmux -S "$SOCKET" capture-pane -p -J -t target -S -200`.
56 - Wait for prompts: `{baseDir}/scripts/wait-for-text.sh -t session:0.0 -p 'pattern'`.
57 - Attaching is OK; detach with `Ctrl+b d`.
58
59 ## Spawning processes
60
61 - For python REPLs, set `PYTHON_BASIC_REPL=1` (non-basic REPL breaks send-keys flows).
62
63 ## Windows / WSL
64
65 - tmux is supported on macOS/Linux. On Windows, use WSL and install tmux inside WSL.
66 - This skill is gated to `darwin`/`linux` and requires `tmux` on PATH.
67
68 ## Orchestrating Coding Agents (Codex, Claude Code)
69
70 tmux excels at running multiple coding agents in parallel:
71
72 ```bash
73 SOCKET="${TMPDIR:-/tmp}/codex-army.sock"
74
75 # Create multiple sessions
76 for i in 1 2 3 4 5; do
77 tmux -S "$SOCKET" new-session -d -s "agent-$i"
78 done
79
80 # Launch agents in different workdirs
81 tmux -S "$SOCKET" send-keys -t agent-1 "cd /tmp/project1 && codex --yolo 'Fix bug X'" Enter
82 tmux -S "$SOCKET" send-keys -t agent-2 "cd /tmp/project2 && codex --yolo 'Fix bug Y'" Enter
83
84 # Poll for completion (check if prompt returned)
85 for sess in agent-1 agent-2; do
86 if tmux -S "$SOCKET" capture-pane -p -t "$sess" -S -3 | grep -q "❯"; then
87 echo "$sess: DONE"
88 else
89 echo "$sess: Running..."
90 fi
91 done
92
93 # Get full output from completed session
94 tmux -S "$SOCKET" capture-pane -p -t agent-1 -S -500
95 ```
96
97 **Tips:**
98 - Use separate git worktrees for parallel fixes (no branch conflicts)
99 - `pnpm install` first before running codex in fresh clones
100 - Check for shell prompt (`❯` or `$`) to detect completion
101 - Codex needs `--yolo` or `--full-auto` for non-interactive fixes
102
103 ## Cleanup
104
105 - Kill a session: `tmux -S "$SOCKET" kill-session -t "$SESSION"`.
106 - Kill all sessions on a socket: `tmux -S "$SOCKET" list-sessions -F '#{session_name}' | xargs -r -n1 tmux -S "$SOCKET" kill-session -t`.
107 - Remove everything on the private socket: `tmux -S "$SOCKET" kill-server`.
108
109 ## Helper: wait-for-text.sh
110
111 `{baseDir}/scripts/wait-for-text.sh` polls a pane for a regex (or fixed string) with a timeout.
112
113 ```bash
114 {baseDir}/scripts/wait-for-text.sh -t session:0.0 -p 'pattern' [-F] [-T 20] [-i 0.5] [-l 2000]
115 ```
116
117 - `-t`/`--target` pane target (required)
118 - `-p`/`--pattern` regex to match (required); add `-F` for fixed string
119 - `-T` timeout seconds (integer, default 15)
120 - `-i` poll interval seconds (default 0.5)
121 - `-l` history lines to search (integer, default 1000)
122
122 lines MARKDOWN