Claw dispatch

From jleechanorg/claude-commands · View interactive version

Claw dispatch is an AI agent skill from the GitHub repository jleechanorg/claude-commands. Use when dispatching work through the Hermes gateway with /claw, especially when the task may resolve slash commands or hand off into AO worker orchestration.

How to use this skill

Skill file

Claw Dispatch

Retained review integrations

The shared catalog preserves host-installed /advice and /web-advice integrations instead of installing them. Before invoking either, resolve its ../advice/SKILL.md or ../web-advice/SKILL.md relative to this package and read the existing skill. For a remote invocation, check the corresponding skill on the target host. If absent, report that integration as UNAVAILABLE and identify the missing package; do not invent a replacement runner, claim an approval, or treat a required gate as passed. Continue independent authorized work, but leave any dependent readiness or plan-approval gate unmet. Existing review quorum, external-disclosure authorization, and optional-review rules still apply.

Default behavior — AO workers first

/claw defaults to spawning AO workers directly. Hermes gateway is the fallback, not the default.

| Input | Default action |

|-------|---------------|

| PR number (#633, PR 633, 633) | Expand to draft-first readiness followed by /green, then post to Slack → Hermes (no default attempt cap) |

| General task description | Post to Slack → Hermes |

| --max-attempts N | Set an explicit worker attempt limit; return incomplete status and evidence to the parent at the limit |

| --bidi prefix | Hermes interactive session (streaming) |

| --hermes prefix | Force through Hermes gateway |

| Slash command resolution (e.g. /green) | Resolve skill then pass to Hermes via Slack |

Project auto-detection for PR tasks: resolve from current git remote (git remote get-url origin).

When to use Hermes instead of AO: only when the user explicitly says --hermes, uses --bidi for interactive output, or the task is explicitly a Hermes-native operation (routing config, gateway status, etc.).

Overview

/claw routes work to AO workers by default. Use:

User-specified AO parameters remain mandatory.

Requirements

Execution

When invoked with a task description:

TASK_DESCRIPTION="$ARGUMENTS"
set -euo pipefail

# An explicit max-attempts option bounds this worker invocation.
# Parse only user arguments; resolved skill documentation is task content.
CLAW_ATTEMPT_PARSE=$(python3 - "$TASK_DESCRIPTION" "${CLAW_MAX_ATTEMPTS:-}" <<'PY'
import re
import sys

task, limit = sys.argv[1:]
options = list(re.finditer(
    r"(?<!\S)--max-attempts(?=[=\s]|$)(?:=(\S*)|\s+(\S+))?", task
))
if len(options) > 1:
    raise SystemExit("Specify --max-attempts only once")
if options:
    option = options[0]
    limit = option.group(1) if option.group(1) is not None else option.group(2)
    if not limit or not re.fullmatch(r"[1-9][0-9]*", limit):
        raise SystemExit("--max-attempts requires an exact positive integer")
    task = task[:option.start()] + task[option.end():]
if not task.strip():
    raise SystemExit("--max-attempts also requires a task description" if options else "A task description is required")
if limit and not re.fullmatch(r"[1-9][0-9]*", limit):
    raise SystemExit("CLAW_MAX_ATTEMPTS requires an exact positive integer")
print(limit)
print(task.strip())
PY
) || exit 2
CLAW_MAX_ATTEMPTS=${CLAW_ATTEMPT_PARSE%%$'\n'*}
TASK_DESCRIPTION=${CLAW_ATTEMPT_PARSE#*$'\n'}
export CLAW_MAX_ATTEMPTS

# Bidi mode: synchronous, streaming output
BIDI_MODE=false
CONTINUE_SESSION=""

if printf '%s' "$TASK_DESCRIPTION" | grep -Eq '^--bidi([[:space:]]|$)'; then
  BIDI_MODE=true
  TASK_DESCRIPTION=$(printf '%s' "$TASK_DESCRIPTION" | sed 's/^--bidi[[:space:]]*//')
fi

if printf '%s' "$TASK_DESCRIPTION" | grep -Eq '^--continue([[:space:]]|$)'; then
  CONTINUE_SESSION=$(printf '%s' "$TASK_DESCRIPTION" | sed 's/^--continue[[:space:]]*//' | awk '{print $1}')
fi

# --hermes: force the task to run inline in the gateway (skip the AO directive).
FORCE_HERMES=false
if printf '%s' "$TASK_DESCRIPTION" | grep -Eq '^--hermes([[:space:]]|$)'; then
  FORCE_HERMES=true
  TASK_DESCRIPTION=$(printf '%s' "$TASK_DESCRIPTION" | sed 's/^--hermes[[:space:]]*//')
fi

LOGDIR="/tmp/hermes"
mkdir -p "$LOGDIR"
chmod 700 "$LOGDIR" 2>/dev/null || true
STATUS_LOG="$(mktemp "$LOGDIR/.claw-status-XXXXXXXX")"
LAUNCHD_PLIST="$HOME/Library/LaunchAgents/ai.hermes.prod.plist"

if [ -f "$LAUNCHD_PLIST" ]; then
  while IFS='=' read -r key value; do
    [ -n "$key" ] || continue
    export "$key=$value"
  done < <(
    python3 - "$LAUNCHD_PLIST" <<'PY'
import plistlib
import sys

path = sys.argv[1]
try:
    with open(path, "rb") as fh:
        data = plistlib.load(fh)
except FileNotFoundError:
    raise SystemExit(0)

env = data.get("EnvironmentVariables") or {}
for key in ("HERMES_HOME", "HERMES_LOG_LEVEL"):
    value = env.get(key)
    if value:
        print(f"{key}={value}")
PY
  )
fi

export HERMES_HOME="${HERMES_HOME:-$HOME/.hermes}"
HERMES_CFG="$HERMES_HOME/config.yaml"

if [ ! -f "$HERMES_CFG" ]; then
  echo "Hermes config not found: $HERMES_CFG"
  exit 1
fi
if ! python3 -c "import yaml; yaml.safe_load(open('$HERMES_CFG'))" 2>/dev/null; then
  echo "Hermes config is not valid YAML: $HERMES_CFG"
  exit 1
fi

# Hermes gateway health check. The `hermes gateway status` CLI is unreliable —
# it can report "not running" / "draining" with phantom PIDs while the actual
# gateway is healthy. Trust the live health endpoint instead.
#
# Port resolution order:
#   1. $CLAW_HERMES_PORT (override for testing / staging / non-prod gateways)
#   2. `port:` under the `gateway:` block in $HERMES_CFG (canonical config)
#   3. Hard-coded default :8643 (current prod gateway port as of 2026-06-28;
#      see ~/.hermes/config.yaml)
#
# Note: the previous default of :8642 is stale — the live Hermes gateway has
# been on :8643 for some time. Hard-coding 8642 silently breaks /claw on
# healthy gateways. Always resolve from the YAML config or CLAW_HERMES_PORT.
HERMES_PORT="${CLAW_HERMES_PORT:-$(python3 -c "
import yaml, sys
try:
    with open('$HERMES_CFG') as fh:
        cfg = yaml.safe_load(fh) or {}
    gw = cfg.get('gateway') or {}
    p = gw.get('port')
    if isinstance(p, int):
        print(p)
    elif isinstance(p, str) and p.isdigit():
        print(p)
except Exception:
    pass
" 2>/dev/null)}"
HERMES_PORT="${HERMES_PORT:-8643}"
HEALTH_LOG="$(mktemp "$LOGDIR/.claw-health-XXXXXXXX")"
if ! curl -sS -m 3 "http://127.0.0.1:${HERMES_PORT}/health" >"$HEALTH_LOG" 2>&1; then
  echo "Hermes gateway is not reachable on :${HERMES_PORT}."
  echo "(Override via CLAW_HERMES_PORT=<port> or set gateway.port in $HERMES_CFG)"
  sed -n '1,20p' "$HEALTH_LOG"
  exit 1
fi
if ! grep -q '"status": "ok"' "$HEALTH_LOG"; then
  echo "Hermes gateway health check did not return ok."
  sed -n '1,20p' "$HEALTH_LOG"
  exit 1
fi

TASK_WITH_RESOLVED="$TASK_DESCRIPTION"

# PR shorthand expansion: when the task is a bare PR number (e.g. "6976", "PR 6976",
# "#6976"), expand it to the full draft-first readiness + `/green` task with repo auto-detection.
PR_NUMBER=$(printf '%s' "$TASK_DESCRIPTION" | python3 -c "
import sys, re
text = sys.stdin.read().strip()
m = re.match(r'^(?:PR\s*#?|#)\s*(\d+)$', text)
if m:
    print(m.group(1))
" 2>/dev/null)
if [ -n "$PR_NUMBER" ]; then
  # Auto-detect repo from current git remote
  REPO_SLUG=$(git remote get-url origin 2>/dev/null | python3 -c "
import sys, re
url = sys.stdin.read().strip()
m = re.match(r'(?:https?://github\.com/|git@github\.com:)([\w-]+/[\w-]+?)(?:\.git)?$', url)
if m:
    print(m.group(1))
" 2>/dev/null || echo "")
  if [ -n "$REPO_SLUG" ]; then
    PR_URL="https://github.com/${REPO_SLUG}/pull/${PR_NUMBER}"
  else
    PR_URL="PR #${PR_NUMBER}"
  fi
  TASK_WITH_RESOLVED="Keep ${PR_URL} draft while completing /es, /er, and /advice; then mark it ready and bring it to /green. Fix CI failures and merge conflicts, and treat CodeRabbit/Bugbot as advisory. Use /green ${PR_NUMBER} to verify. Continue authorized work within the parent mission's scope and deadline; do not invent a cycle-count stop or ask again for already-authorized remediation. If this worker cannot proceed, return the exact blocked action and evidence to its parent, which diagnoses and continues authorized recovery or independent work. Preserve merge and destructive-action approval requirements."
  TASK_DESCRIPTION="$TASK_WITH_RESOLVED"
fi

# PR URL augmentation: when the task contains a GitHub PR URL, inject an explicit
# autonomy directive so Hermes acts without asking for confirmation.
PR_URL=$(printf '%s' "$TASK_DESCRIPTION" | python3 -c "
import sys, re
m = re.search(r'https://github\.com/[\w-]+/[\w-]+/pull/\d+', sys.stdin.read())
if m:
    print(m.group(0))
" 2>/dev/null)
if [ -n "$PR_URL" ]; then
  TASK_WITH_RESOLVED="${TASK_DESCRIPTION}

IMPORTANT: You are acting as an autonomous agent. If you find that CI checks are failing, review threads are unresolved, or the PR needs remediation, automatically route to AO for remediation (ao spawn --claim-pr N). Do NOT ask 'Would you like me to route to AO?' — just do it. The user invoked /claw to get autonomous action, not a status report."
fi

SLASH_CMD=$(printf '%s' "$TASK_DESCRIPTION" | python3 -c "
import sys, re
text = sys.stdin.read().strip()
clean = re.sub(r'https?://\\S+', '', text)
m = re.search(r'(?:^|\\s)/([\\w-]+)', clean)
if m:
    print(m.group(1))
" 2>/dev/null)

if [ -n "$SLASH_CMD" ]; then
  RESOLVED_CONTENT=""
  RESOLVED_SOURCE=""
  for search_dir in ".claude/commands" "$HOME/.claude/commands"; do
    if [ -f "$search_dir/$SLASH_CMD.md" ]; then
      RESOLVED_CONTENT=$(cat "$search_dir/$SLASH_CMD.md" 2>/dev/null)
      RESOLVED_SOURCE="$search_dir/$SLASH_CMD.md"
      break
    fi
  done
  if [ -z "$RESOLVED_CONTENT" ]; then
    for search_dir in ".claude/skills" "$HOME/.claude/skills"; do
      if [ -f "$search_dir/$SLASH_CMD/SKILL.md" ]; then
        RESOLVED_CONTENT=$(cat "$search_dir/$SLASH_CMD/SKILL.md" 2>/dev/null)
        RESOLVED_SOURCE="$search_dir/$SLASH_CMD/SKILL.md"
        break
      elif [ -f "$search_dir/$SLASH_CMD.md" ]; then
        RESOLVED_CONTENT=$(cat "$search_dir/$SLASH_CMD.md" 2>/dev/null)
        RESOLVED_SOURCE="$search_dir/$SLASH_CMD.md"
        break
      fi
    done
  fi
  if [ -n "$RESOLVED_CONTENT" ]; then
    echo "Resolved /$SLASH_CMD from $RESOLVED_SOURCE"
    TASK_WITH_RESOLVED="The user asked: $TASK_DESCRIPTION

Below is the full definition of /$SLASH_CMD (resolved from $RESOLVED_SOURCE). Execute it as instructed:

---
$RESOLVED_CONTENT
---"
  fi
fi

if [ -n "${CLAW_MAX_ATTEMPTS:-}" ]; then
  TASK_WITH_RESOLVED="${TASK_WITH_RESOLVED}

This worker invocation has an explicitly configured limit of ${CLAW_MAX_ATTEMPTS} attempts. At that limit, return its incomplete status and evidence to the parent; the parent continues authorized diagnosis or independent work within the mission deadline. Do not claim completion or merge approval from an exhausted invocation."
fi

# General AO-dispatch directive (the documented "AO workers first" default).
# For freeform tasks — no PR URL (those already carry their own AO remediation
# directive above), not --bidi (interactive), not --hermes (forced inline) — tell
# the gateway to delegate to an AO worker unless the task is small enough to finish
# inline. The trivial-vs-AO judgment is the gateway LLM's to make (ZFC: the model
# decides, the skill only states the policy) — we never classify the task in code.
if [ "$BIDI_MODE" != true ] && [ "$FORCE_HERMES" != true ] && [ -z "${PR_URL:-}" ]; then
  TASK_WITH_RESOLVED="${TASK_WITH_RESOLVED}

IMPORTANT — dispatch policy: unless this is a very small task you can fully complete inline in a few steps, dispatch it to an AO worker (e.g. \`ao spawn\` / \`ao spawn --claim-pr N\`) rather than doing the whole thing inline in this gateway session. Default to AO for multi-file changes, PR/CI/build/deploy work, or anything beyond a few minutes; handle only genuinely small asks inline. You decide which side of that line this task is on. Act autonomously — do not ask whether to dispatch to AO, just route it."
fi

if [ "$BIDI_MODE" = true ]; then
  echo "Bidi mode: streaming Hermes response (synchronous)"
  hermes chat \
    -q "$TASK_WITH_RESOLVED" \
    --yolo \
    --max-turns 30 \
    --source tool \
    --pass-session-id
  echo ""
  echo "Session ended. To continue: /claw --continue <session_name>"
  exit 0
fi

if [ -n "$CONTINUE_SESSION" ]; then
  echo "Continuing Hermes session: $CONTINUE_SESSION"
  hermes chat \
    --continue "$CONTINUE_SESSION" \
    --yolo \
    --source tool
  exit 0
fi

# --status <logfile>: check on a running/completed claw session
if printf '%s' "$TASK_WITH_RESOLVED" | grep -q '^--status'; then
  STATUS_LOG=$(printf '%s' "$TASK_WITH_RESOLVED" | sed 's/^--status[[:space:]]*//' | awk '{print $1}')
  if [ ! -f "$STATUS_LOG" ]; then
    echo "❌ Log not found: $STATUS_LOG"
    exit 1
  fi
  LOG_LINES=$(wc -l <"$STATUS_LOG")
  LAST_LINE=$(tail -1 "$STATUS_LOG" 2>/dev/null)
  PID_RUNNING=$(pgrep -f "hermes chat" | head -1)
  echo "=== Claw Session Status ==="
  echo "Log: $STATUS_LOG ($LOG_LINES lines)"
  echo "Process: ${PID_RUNNING:-(completed)}"
  echo "Last output: $LAST_LINE"
  echo "--- Tail ---"
  tail -20 "$STATUS_LOG"
  exit 0
fi

# === Slack dispatch path ===
# /claw now posts to Slack instead of forking a hermes chat process. The
# gateway is already connected to Slack via Socket Mode; we hand the task
# to it through a channel message and watch the thread for the ack.
#
# Channel + bot user ID come from the environment; there is no default.
if [ -z "${CLAW_CHANNEL:-}" ] || [ -z "${CLAW_BOT_ID:-}" ]; then
  echo "❌ CLAW_CHANNEL / CLAW_BOT_ID not configured: Slack channel ID for /claw dispatch and the Hermes bot user ID."
  echo "   Example: export CLAW_CHANNEL=C0123ABCD CLAW_BOT_ID=U0123ABCD"
  exit 1
fi
CLAW_TOKEN="${SLACK_MCP_XOXP_TOKEN:-${HERMES_SLACK_USER_TOKEN:-}}"
if [ -z "$CLAW_TOKEN" ]; then
  echo "❌ No Slack user token (SLACK_MCP_XOXP_TOKEN) available — cannot dispatch via Slack."
  echo "   Set SLACK_MCP_XOXP_TOKEN (the operator's xoxp user token) in env or launchd plist."
  exit 1
fi

# Build message: strip any pre-existing <@U...> mentions from the task, then
# prepend [via /claw] provenance + @hermes. Caps at Slack's 39000 char safe limit.
TASK_TEXT=$(printf '%s' "$TASK_WITH_RESOLVED" | sed -E 's|<@[UW][A-Z0-9]+>||g; s/^[[:space:]]+//; s|[[:space:]]+$||')
MESSAGE="[via /claw] <@${CLAW_BOT_ID}> ${TASK_TEXT}"
MESSAGE="${MESSAGE:0:39000}"

# Post via chat.postMessage. Using --data-binary with python3 -c keeps the
# JSON encoding safe (no shell escaping of the message text).
POST_LOG="$LOGDIR/.claw-post-$(date +%s).json"
curl -sS -m 15 -X POST \
  -H "Authorization: Bearer $CLAW_TOKEN" \
  -H "Content-Type: application/json; charset=utf-8" \
  --data-binary "$(python3 -c "import json,sys; print(json.dumps({'channel': '$CLAW_CHANNEL', 'text': sys.argv[1]}))" "$MESSAGE")" \
  "https://slack.com/api/chat.postMessage" >"$POST_LOG" 2>&1

# Parse response
OK=$(python3 -c "import json; print(json.load(open('$POST_LOG')).get('ok', False))" 2>/dev/null)
if [ "$OK" != "True" ]; then
  ERR=$(python3 -c "import json; print(json.load(open('$POST_LOG')).get('error', 'unknown'))" 2>/dev/null)
  echo "❌ Slack post failed: ${ERR}"
  echo "   response: $POST_LOG"
  exit 1
fi

# Extract ts + ask Slack for the thread permalink. SLACK_WORKSPACE_DOMAIN
# (e.g. myteam.slack.com) is only a fallback when chat.getPermalink fails.
# The permalink lookup is optional, so a curl timeout or a non-JSON reply must
# not abort the already-posted dispatch: its failure falls through to the
# fallback below (same `|| true` guard as the optional cleanup earlier).
MSG_TS=$(python3 -c "import json; print(json.load(open('$POST_LOG')).get('ts', ''))")
POST_CH=$(python3 -c "import json; print(json.load(open('$POST_LOG')).get('channel', ''))")
THREAD_URL=$(curl -sS -m 10 -G -H "Authorization: Bearer $CLAW_TOKEN" \
  --data-urlencode "channel=$POST_CH" --data-urlencode "message_ts=$MSG_TS" \
  "https://slack.com/api/chat.getPermalink" 2>/dev/null |
  python3 -c "import json,sys; print(json.load(sys.stdin).get('permalink', ''))" 2>/dev/null || true)
if [ -z "$THREAD_URL" ] && [ -n "${SLACK_WORKSPACE_DOMAIN:-}" ]; then
  THREAD_URL="https://${SLACK_WORKSPACE_DOMAIN}/archives/${POST_CH}/p${MSG_TS//./}"
fi
[ -n "$THREAD_URL" ] || THREAD_URL="(channel $POST_CH, ts $MSG_TS)"

echo "✅ Dispatched to Hermes via Slack"
echo "   channel: $CLAW_CHANNEL"
echo "   thread:  $THREAD_URL"
echo "   message_ts: $MSG_TS"

# Wait for Hermes ack (up to 30s) — reaction OR thread reply from Hermes.
# This is the Slack analog of the old "wait for first log line" check.
# 30s is generous because Hermes may be busy with other sessions; the message
# is already in the channel and durable, so a longer wait is purely diagnostic.
echo "⏳ Waiting for Hermes ack (up to 30s)..."
ACKED=0
for i in $(seq 1 30); do
  sleep 1
  # Reaction on the parent message (e.g. "On it…" emoji)
  REACT=$(curl -sS -m 3 -H "Authorization: Bearer $CLAW_TOKEN" \
    "https://slack.com/api/reactions.get?channel=${CLAW_CHANNEL}&timestamp=${MSG_TS}" 2>/dev/null)
  REACT_HIT=$(printf '%s' "$REACT" | python3 -c "import json,sys
try:
  d=json.load(sys.stdin)
  print(d.get('ok', False) and bool(d.get('message', {}).get('reactions')))
except: print(False)" 2>/dev/null)
  if [ "$REACT_HIT" = "True" ]; then
    echo "✅ Hermes acked (reaction at ${i}s)"
    ACKED=1
    break
  fi
  # Reply in thread (Hermes often replies with "On it…" text rather than a reaction)
  REPLIES=$(curl -sS -m 3 -H "Authorization: Bearer $CLAW_TOKEN" \
    "https://slack.com/api/conversations.replies?channel=${CLAW_CHANNEL}&ts=${MSG_TS}&limit=5" 2>/dev/null)
  REPLY_HIT=$(printf '%s' "$REPLIES" | python3 -c "import json,sys
try:
  d=json.load(sys.stdin)
  print(d.get('ok', False) and len(d.get('messages', [])) > 1)
except: print(False)" 2>/dev/null)
  if [ "$REPLY_HIT" = "True" ]; then
    echo "✅ Hermes acked (thread reply at ${i}s)"
    ACKED=1
    break
  fi
done
if [ "$ACKED" != "1" ]; then
  echo "⚠️  No ack within 30s — Hermes may be rate-limited, stuck, or the channel is wrong."
  echo "   Check: $THREAD_URL"
  exit 0  # message is posted; user can monitor the thread
fi

echo ""
echo "✅ Dispatched to Hermes via Slack"
echo "   Channel:  $CLAW_CHANNEL"
echo "   Thread:   $THREAD_URL"
echo "   Hermes:   $CLAW_BOT_ID"
echo "   Monitor:  open the thread URL above; Hermes acks within ~30s, then works the task in thread"
echo "   Re-check: /claw --status <logfile>"

Post-dispatch output (MANDATORY)

After every /claw dispatch — AO or Hermes — always print these lines in your reply:

AO spawn path

✅ AO worker spawned: <session-name>
   Attach:    ao attach <session-name>
   Status:    ao status <session-name>
   tmux:      tmux attach-session -t <container-id>-<session-name>
   Dashboard: http://localhost:3030

Get the session name from ao session ls --project <project> immediately after spawn. The dashboard URL is the live AO web UI (next.js on :3030 in prod, :3020 in dev).

Hermes chat path (fallback)

✅ Hermes worker dispatched (PID: <pid>)
   Log:     <logfile>
   Monitor: tail -f <logfile>
   Kill:    kill <pid>

Always emit the exact logfile path so the user can monitor or kill.

Slack path

✅ Dispatched to Hermes via Slack
   Channel:  <CLAW_CHANNEL>
   Thread:   <thread_url>
   Hermes:   <CLAW_BOT_ID>
   Monitor:  open the thread URL above; Hermes acks within ~30s, then works the task in thread
   Re-check: /claw --status <logfile>

Never omit these lines. The user must always know how to attach, monitor, or kill the worker without asking.

Notes

Modes

| Prefix | Behavior |

|--------|----------|

| (none) | Async fire-and-forget with ack detection (✅ ACK / ❌ RATE_LIMITED / ❌ FAILED) |

| --bidi | Synchronous streaming — blocks until Hermes finishes, shows session ID for follow-up |

| --continue <name> | Resume a previous --bidi session interactively |

| --status <logfile> | Check tail + completion status of any async claw session |

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.