第9集:多 Agent 并行实战
💡 进群学习加 wx: agentupdate
(申请发送: agentupdate)
(申请发送: agentupdate)
5 个 Agent 同时跑——cmux vs tmux 两种方案对比,附 Mermaid 工作流图。
场景
同时跑 5 个 Claude Code Agent:
| Agent | 任务 | 预计耗时 |
|---|---|---|
| Agent 1 | Phase 1:计算引擎 | 30 min |
| Agent 2 | Phase 2:UI 层 | 25 min |
| Agent 3 | 测试运行 | 持续 |
| Agent 4 | Code Review | 按需 |
| Agent 5 | Bug 调试 | 不确定 |
方案 A:cmux(本地 macOS)
sequenceDiagram
participant User as 开发者
participant Cmux as cmux
participant A1 as Agent 1
Phase 1
participant A2 as Agent 2
Phase 2
participant A3 as Agent 3
测试
participant A4 as Agent 4
Review
participant A5 as Agent 5
Debug
rect rgb(230, 245, 255)
Note over User,A5: 启动阶段
User->>Cmux: 脚本启动 5 个标签
Cmux->>A1: claude /gsd-execute-phase 1
Cmux->>A2: claude /gsd-execute-phase 2
Cmux->>A3: claude /gsd-add-tests
Cmux->>A4: claude /gsd-code-review
Cmux->>A5: claude /gsd-debug
end
rect rgb(255, 245, 230)
Note over User,A5: 运行阶段
A1->>A1: 执行中...
A2->>A2: 执行中...
A3->>A3: 运行测试...
A1->>Cmux: ⏸ 需要用户决策
Cmux->>User: 🔔 通知环闪动 + 桌面提醒
User->>A1: 输入决策
end
rect rgb(230, 255, 230)
Note over User,A5: 完成阶段
A3->>Cmux: ✅ 测试全部通过
Cmux->>User: 🔔 完成通知
A4->>Cmux: 📝 Review 报告生成
Cmux->>User: 🔔 报告就绪
end实操步骤:
- 启动 cmux 并利用脚本创建 5 个标签:
# start_agents.py
import socket, json
def cmux_cmd(cmd):
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.connect('/tmp/cmux.sock')
sock.send(json.dumps(cmd).encode())
sock.close()
agents = [
("agent-phase1", "claude"),
("agent-phase2", "claude"),
("agent-tests", "claude"),
("agent-review", "claude"),
("agent-debug", "claude"),
]
for name, cmd in agents:
cmux_cmd({"action": "new_tab", "name": name, "command": cmd})
- 等待通知环提示,按需介入。
优势:通知环自动过滤注意力,只处理需要你的 Agent。
方案 B:tmux(本地/远程通用)
sequenceDiagram
participant User as 开发者
participant Tmux as tmux
participant S1 as Session: phase1
participant S2 as Session: phase2
participant S3 as Session: tests
participant S4 as Session: review
participant S5 as Session: debug
rect rgb(230, 245, 255)
Note over User,S5: 启动阶段
User->>Tmux: 启动脚本
Tmux->>S1: tmux new -s phase1
S1->>S1: claude /gsd-execute-phase 1
Tmux->>S2: tmux new -s phase2
S2->>S2: claude /gsd-execute-phase 2
Tmux->>S3: tmux new -s tests
S3->>S3: claude /gsd-add-tests
Tmux->>S4: tmux new -s review
S4->>S4: claude /gsd-code-review
Tmux->>S5: tmux new -s debug
S5->>S5: claude /gsd-debug
end
rect rgb(255, 245, 230)
Note over User,S5: 监控阶段
User->>Tmux: tmux ls
Tmux->>User: 列出 5 个 session
User->>Tmux: tmux attach -t phase1
Note over User: 手动逐个检查状态
User->>Tmux: Ctrl+b d (detach)
User->>Tmux: tmux attach -t phase2
end
rect rgb(230, 255, 230)
Note over User,S5: 持久化
User->>Tmux: 关掉终端
Note over Tmux: 5 个 session 继续运行
User->>Tmux: 第二天 tmux attach
Note over User: 恢复所有 session
end实操步骤:
- 批量启动脚本:
#!/bin/bash
sessions=("phase1" "phase2" "tests" "review" "debug")
for s in "${sessions[@]}"; do
tmux new-session -d -s "$s" "claude"
done
- 监控与恢复:通过
tmux ls查看,tmux attach -t <name>连接。
优势:全平台通用,支持 detach/attach,关掉终端 Agent 不停。
方案 C:cmux + tmux 混合
graph TB
subgraph 本地macOS
CMUX[cmux
5个垂直标签]
CMUX --> |标签1| A1[Agent: Phase 1]
CMUX --> |标签2| A2[Agent: Phase 2]
CMUX --> |标签3| SSH[SSH → 远程服务器]
end
subgraph 远程Linux
TMUX[tmux
3个session]
TMUX --> S1[Agent: Review]
TMUX --> S2[Agent: Debug]
end
SSH --> TMUX本地 cmux 管理 macOS 上的 Agent,远程 SSH 进服务器后用 tmux。两套系统互补。