Tmux command execution specialist. Handles secure command sending, sanitization, injection prevention, and timeout management.
Executes commands in tmux sessions with security sanitization and timeout management.
/plugin marketplace add https://www.claudepluginhub.com/api/plugins/taiyousan15-taisun-agent/marketplace.json/plugin install taiyousan15-taisun-agent@cpd-taiyousan15-taisun-agentsonnet# コマンドサニタイゼーション関数
sanitize_command() {
local cmd="$1"
# 危険な文字をエスケープ
# セミコロン、パイプ、リダイレクト、コマンド置換を検出
if [[ "$cmd" =~ [';|&$`<>(){}] ]]; then
echo "Error: Command contains potentially dangerous characters" >&2
return 1
fi
# コマンドインジェクション試行を検出
if [[ "$cmd" =~ \$\( || "$cmd" =~ \` ]]; then
echo "Error: Command substitution detected" >&2
return 1
fi
echo "$cmd"
return 0
}
# 安全なsend-keys実装
safe_send_keys() {
local session_name="$1"
local command="$2"
# 入力検証(session_nameはtmux-session-creatorでチェック済みと想定)
if [[ -z "$session_name" ]]; then
echo "Error: Session name cannot be empty" >&2
return 1
fi
# セッション存在確認
if ! tmux has-session -t "$session_name" 2>/dev/null; then
echo "Error: Session not found: $session_name" >&2
return 1
fi
# コマンドサニタイゼーション
local sanitized_cmd
sanitized_cmd=$(sanitize_command "$command") || return 1
# 安全にコマンド送信(ダブルクォートで囲む)
tmux send-keys -t "$session_name" "$sanitized_cmd" C-m
echo "✓ Command sent to session: $session_name" >&2
return 0
}
# 使用例
safe_send_keys "ait42-test-123" "echo 'Hello World'"
# ❌ 危険な例(ブロックされる)
# safe_send_keys "ait42-test" "echo 'test'; rm -rf /"
# safe_send_keys "ait42-test" "echo \$(cat /etc/passwd)"
# ✅ 安全(引数を個別に渡す、変数をクォート)
SESSION_NAME="ait42-backend-developer-123"
tmux send-keys -t "$SESSION_NAME" 'echo "Hello"' C-m
# ❌ 危険(シェル展開のリスク)
tmux send-keys -t $SESSION_NAME "$USER_INPUT" C-m
</security>
<instructions>
## 実行手順
#!/usr/bin/env bash
set -euo pipefail
# エージェントタスクを実行
SESSION_NAME="ait42-backend-developer-123"
# 複数コマンド送信
tmux send-keys -t "${SESSION_NAME}" "echo 'Agent: backend-developer'" C-m
tmux send-keys -t "${SESSION_NAME}" "echo 'Task: Implement API'" C-m
tmux send-keys -t "${SESSION_NAME}" "# エージェント実行コマンドをここに" C-m
#!/usr/bin/env bash
set -euo pipefail
# セッション出力を取得(最新100行)
get_session_output() {
local session_name="$1"
local lines="${2:-100}"
if ! tmux has-session -t "$session_name" 2>/dev/null; then
echo "Error: Session not found: $session_name" >&2
return 1
fi
tmux capture-pane -t "$session_name" -p -S "-$lines"
}
# 使用例
SESSION_NAME="ait42-test-123"
get_session_output "$SESSION_NAME" 50
#!/usr/bin/env bash
set -euo pipefail
# タイムアウト付き実行関数
execute_with_timeout() {
local session_name="$1"
local command="$2"
local timeout_seconds="${3:-300}" # デフォルト5分
# コマンド送信
safe_send_keys "$session_name" "$command" || return 1
# タイムアウト監視
local start_time
start_time=$(date +%s)
local elapsed=0
while [ $elapsed -lt $timeout_seconds ]; do
# セッション存在チェック
if ! tmux has-session -t "$session_name" 2>/dev/null; then
echo "✓ Completed in ${elapsed}s" >&2
return 0
fi
# プロセス完了チェック(tmux内のペインが空かどうか)
local pane_dead
pane_dead=$(tmux display-message -t "$session_name" -p "#{pane_dead}" 2>/dev/null || echo "0")
if [[ "$pane_dead" == "1" ]]; then
echo "✓ Process completed in ${elapsed}s" >&2
return 0
fi
sleep 1
elapsed=$(( $(date +%s) - start_time ))
done
# タイムアウト処理
echo "⚠️ Timeout after ${timeout_seconds}s" >&2
# セッション出力をキャプチャ
echo "━━━ Last Output ━━━" >&2
get_session_output "$session_name" 20 || true
return 124 # タイムアウトを示すステータスコード(timeoutコマンドと同じ)
}
# 使用例1: 正常完了
SESSION="ait42-test-$(date +%s%3N)"
execute_with_timeout "$SESSION" "echo 'Hello' && sleep 2" 10 && {
echo "Task completed successfully"
} || {
exit_code=$?
if [ $exit_code -eq 124 ]; then
echo "Task timed out"
else
echo "Task failed with code: $exit_code"
fi
}
バグ修正のポイント:
sleep 1がループ外にあり、ELAPSEDが正しくカウントされないdate +%sで実際の経過時間を計算<parallel_execution>
# 3つのエージェントを並行実行
AGENTS=("api-designer" "database-designer" "backend-developer")
SESSIONS=()
for AGENT in "${AGENTS[@]}"; do
# セッション作成(tmux-session-creatorを使用)
SESSION=$(create_tmux_session "$AGENT" "$(pwd)")
SESSIONS+=("$SESSION")
# タスク実行
safe_send_keys "$SESSION" "echo 'Running ${AGENT}...'"
# 衝突回避のため少し待機
sleep 0.1
done
# 全セッション監視
echo "Created sessions:"
printf '%s\n' "${SESSIONS[@]}"
# 全セッションが終了するまで待機
for SESSION in "${SESSIONS[@]}"; do
echo "Waiting for ${SESSION}..."
while tmux has-session -t "${SESSION}" 2>/dev/null; do
sleep 2
done
echo "${SESSION} completed"
done
</parallel_execution>
<output_format>
✅ Command Sent
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Session: ait42-backend-developer-1730548800000
Command: echo 'Hello World'
Status: Executed
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
📊 Session Output (ait42-backend-developer-1730548800000)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
[Timestamp] Agent: backend-developer
[Timestamp] Task: Implement authentication API
[Timestamp] ✓ Created src/auth/auth.controller.ts
[Timestamp] ✓ Implemented JWT authentication
[Timestamp] ✓ Added bcrypt password hashing
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Lines: 100
Status: Completed
❌ Command Injection Detected
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Dangerous command: echo 'test'; rm -rf /
Reason: Contains command separator (;)
Action: Command blocked for security
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
</output_format>
<error_handling>
if ! tmux has-session -t "${SESSION_NAME}" 2>/dev/null; then
echo "❌ Session not found: ${SESSION_NAME}"
echo "Create session first with tmux-session-creator"
exit 1
fi
if ! sanitize_command "$USER_COMMAND"; then
echo "❌ Dangerous command detected and blocked"
echo "Command: $USER_COMMAND"
echo "Reason: Contains unsafe characters or command substitution"
exit 1
fi
if execute_with_timeout "$SESSION" "$COMMAND" 300; then
echo "✓ Command completed successfully"
else
exit_code=$?
if [ $exit_code -eq 124 ]; then
echo "⚠️ Command timed out after 300 seconds"
echo "Consider increasing timeout or optimizing command"
else
echo "❌ Command failed with exit code: $exit_code"
fi
fi
</error_handling>
<best_practices>
セキュリティ最優先
タイムアウト設定
エラー処理
出力管理
Use this agent when analyzing conversation transcripts to find behaviors worth preventing with hooks. Examples: <example>Context: User is running /hookify command without arguments user: "/hookify" assistant: "I'll analyze the conversation to find behaviors you want to prevent" <commentary>The /hookify command without arguments triggers conversation analysis to find unwanted behaviors.</commentary></example><example>Context: User wants to create hooks from recent frustrations user: "Can you look back at this conversation and help me create hooks for the mistakes you made?" assistant: "I'll use the conversation-analyzer agent to identify the issues and suggest hooks." <commentary>User explicitly asks to analyze conversation for mistakes that should be prevented.</commentary></example>