Coding Agent Task (tmux + worktree)
Run coding tasks by spawning a coding agent in a tmux session + git worktree. Every task gets its own isolated branch and persistent conversation.
Step 0: Determine which agent to use
Check memory first:
CODEBLOCK0
- - If found → use that tool, no need to ask
- If not found → ask the user:
> "Which coding agent should I use? (default: claude)
> Options: claude, codex, opencode, codebuddy, or any CLI tool name"
Then save the answer to memory:
memory: preferred_coding_agent = <tool>
Write to
MEMORY.md under a "Preferences" section.
Default if user doesn't answer: INLINECODE1
Step 1 & 2: Setup worktree + Start session
If tool is claude (recommended)
Use claude -w — it manages the worktree automatically. Add --tmux to also create a tmux session:
CODEBLOCK2
- -
-w <name> creates and checks out a new git worktree branch automatically - INLINECODE6 opens it in a tmux session (uses iTerm2 panes if available, otherwise classic tmux)
- No need to manually
git worktree add or INLINECODE8
If tool is other (codex, opencode, codebuddy, etc.)
Manage worktree and tmux manually:
CODEBLOCK3
| Tool | Command |
|---|
| INLINECODE9 | INLINECODE10 |
| INLINECODE11 |
opencode |
|
codebuddy |
codebuddy (or check its CLI name) |
| other | use the tool's interactive CLI command |
Step 3: Send task with plan-first instruction
For claude -w --tmux, the session name is auto-set to the branch name. Find it with:
CODEBLOCK4
Then send the task:
CODEBLOCK5
Step 4: Relay plan to user
CODEBLOCK6
When agent outputs a plan → relay it to the user, wait for their confirmation before proceeding.
Relay flow:
- 1. Agent outputs plan → relay to user
- User says "ok" / requests changes → forward to agent
- Agent proceeds → monitor and relay further questions
CODEBLOCK7
Step 5: Parallel tasks
Same pattern, multiple sessions:
CODEBLOCK8
Check all at once:
CODEBLOCK9
Step 6: Cleanup
For claude -w --tmux (worktree auto-managed):
CODEBLOCK10
For other tools (manual worktree):
CODEBLOCK11
User can then test in main workspace:
CODEBLOCK12
Rules
- - Check memory first — never ask for tool preference if already saved
- Always use worktrees — one per task, no exceptions
- Always use tmux — persistent session, multi-turn conversation
- Always show plan first, wait for user approval before agent touches files
- Always symlink
.env files — don't copy - One status message when starting, one when done or stuck
- See
references/troubleshooting.md for common issues
编码代理任务(tmux + worktree)
通过在 tmux 会话 + git worktree 中启动编码代理来运行编码任务。每个任务都有自己独立的隔离分支和持久化对话。
步骤 0:确定使用哪个代理
首先检查记忆:
memory_search(preferred coding agent tool)
- - 如果找到 → 使用该工具,无需询问
- 如果未找到 → 询问用户:
> 我应该使用哪个编码代理?(默认:claude)
> 选项:claude, codex, opencode, codebuddy, 或任何 CLI 工具名称
然后将答案保存到记忆:
memory: preferredcodingagent =
写入 MEMORY.md 的 Preferences 部分。
如果用户未回答,默认值: claude
步骤 1 和 2:设置 worktree + 启动会话
如果工具是 claude(推荐)
使用 claude -w —— 它会自动管理 worktree。添加 --tmux 来同时创建 tmux 会话:
bash
cd <项目目录>
一条命令完成 worktree + tmux:
claude -w <分支名称> --tmux --dangerously-skip-permissions
- - -w <名称> 自动创建并切换到一个新的 git worktree 分支
- --tmux 在 tmux 会话中打开(如果可用则使用 iTerm2 窗格,否则使用经典 tmux)
- 无需手动执行 git worktree add 或 tmux new-session
如果工具是其他(codex, opencode, codebuddy 等)
手动管理 worktree 和 tmux:
bash
创建 worktree
git -C <项目目录> worktree add -b <分支>
main
创建环境文件符号链接
ln -sf <项目目录>/.env /.env
ln -sf <项目目录>/.env.local /.env.local # 如果存在
创建 tmux 会话并启动代理
tmux new-session -d -s <任务名称> -c
tmux send-keys -t <任务名称> nvm use 20 && <工具命令> Enter
opencode |
| codebuddy | codebuddy(或检查其 CLI 名称) |
| 其他 | 使用该工具的交互式 CLI 命令 |
步骤 3:发送带有先计划后执行指令的任务
对于 claude -w --tmux,会话名称自动设置为分支名称。使用以下命令查找:
bash
tmux list-sessions
然后发送任务:
bash
tmux send-keys -t <会话名称> -l -- 你的任务内容。
在进行任何更改之前,请向我展示你打算做什么的计划,并等待我的批准。
sleep 0.1
tmux send-keys -t <会话名称> Enter
步骤 4:将计划传达给用户
bash
轮询计划输出
tmux capture-pane -t <任务名称> -p | tail -30
当代理输出计划时 → 将其传达给用户,等待用户确认后再继续。
传达流程:
- 1. 代理输出计划 → 传达给用户
- 用户说好的/要求修改 → 转发给代理
- 代理继续执行 → 监控并传达后续问题
bash
发送用户的回复
tmux send-keys -t <任务名称> -l -- <用户回复>
sleep 0.1
tmux send-keys -t <任务名称> Enter
检查是否在等待输入
tmux capture-pane -t <任务名称> -p | tail -10 | grep -E ❯|Yes.*No|proceed|permission|plan|approve
步骤 5:并行任务
相同模式,多个会话:
bash
tmux new-session -d -s task-a -c /tmp/task-a
tmux new-session -d -s task-b -c /tmp/task-b
一次性检查所有会话:
bash
for s in task-a task-b; do
echo === $s ===
tmux capture-pane -t $s -p 2>/dev/null | tail -5
done
步骤 6:清理
对于 claude -w --tmux(worktree 自动管理):
bash
tmux kill-session -t <分支名称>
git worktree remove # 保留分支
对于其他工具(手动 worktree):
bash
git -C <项目目录> worktree remove # 保留分支
tmux kill-session -t <任务名称>
用户随后可以在主工作区进行测试:
bash
git switch <分支>
规则
- - 首先检查记忆 —— 如果已保存,绝不询问工具偏好
- 始终使用 worktree —— 每个任务一个,无一例外
- 始终使用 tmux —— 持久化会话,多轮对话
- 始终先展示计划,等待用户批准后再让代理修改文件
- 始终创建 .env 文件的符号链接 —— 不要复制
- 启动时发送一条状态消息,完成或卡住时发送一条
- 常见问题请参见 references/troubleshooting.md