Dispatch: Background Claude Task Runner
The user wants to send a task to a separate Claude Code process running in the background.
What to do
Confirm the task. Restate what the user wants done in one sentence. If unclear, ask.
Pick permission tier. Based on the task:
- Read-only (research, summarize, explain):
"Read,Glob,Grep,Write,WebSearch,WebFetch" (Write always included so output file can be created)
- Code (fix bugs, write features, refactor):
"Read,Glob,Grep,Edit,Write,Bash(git diff *),Bash(git status *),Bash(npm test *),Bash(npx *)"
- Full (commits, installs, builds):
"Read,Glob,Grep,Edit,Write,Bash"
- LLM review (Gemini/ChatGPT):
"Read,Glob,Grep,Write,Bash" (unrestricted Bash needed for source ~/.api-keys-cache)
- Default to read-only unless the task clearly requires writes. Ask if unsure.
Pick model. For research, summaries, file searches, and template writes, add --model sonnet to reduce cost. Use Opus (default) for architecture, client-facing copy, and multi-step reasoning.
Pick LLM target. Default is Claude. Ask if the task warrants a different LLM:
| Target |
When to use |
How it works |
| Claude |
Code tasks, file edits, multi-step work |
Direct claude -p execution |
| Gemini |
Reviews, second opinions, long analysis |
claude -p calls gemini-api.sh |
| ChatGPT |
Alternative perspective, copy feedback |
claude -p calls chatgpt-api.sh |
For Gemini/ChatGPT: write the prompt to a temp file first, then pass it via the script. Use unrestricted Bash tier. See references/multi-llm-dispatch.md for script paths and model names.
Set timeout. Timeout is the circuit breaker (--max-turns was removed in Claude Code 2.1):
- Simple (research, summaries): 600s
- Medium (file edits, multi-step): 1800s
- Complex (builds, large refactors): 3600s
Requires gtimeout (GNU coreutils). If missing: brew install coreutils.
Build and run. Generate job ID and write the prompt to a temp file, then launch:
JOB_ID="job-$(date +%Y%m%d-%H%M%S)"
PROMPT_FILE="/tmp/${JOB_ID}-prompt.txt"
# Write full task prompt to file (include file paths, project context, done criteria)
cat > "$PROMPT_FILE" << 'PROMPT'
<the user's task prompt>
PROMPT
# Launch in background
(bash "${CLAUDE_PLUGIN_ROOT}/skills/dispatch/scripts/dispatch.sh" \
"$JOB_ID" "$PROMPT_FILE" "<TOOLS>" <TIMEOUT_SECS> [MODEL] [ADD_DIR]) &
echo "Dispatched ${JOB_ID} (PID: $!)"
Run with run_in_background: false since the subshell backgrounds via & and returns immediately.
- Report back. Tell the user: job ID, what it's doing, timeout, how to check (
/jobs).
Failure detection
The dispatch script writes a .meta file with these fields:
status: running|complete|failed|timed_out
task: <first 80 chars of prompt>
model: opus|sonnet|haiku
started: YYYY-MM-DD HH:MM:SS
completed: YYYY-MM-DD HH:MM:SS
output: /path/to/output.md
tools: <tool list>
exit_code: <integer>
Status logic:
exit_code = 0 AND output file exists: complete
exit_code = 124: timed_out (gtimeout killed the process)
exit_code != 0 OR no output file: failed
If a job shows failed, check ~/.claude/jobs/<JOB_ID>.log for the last 20 lines.
Why /tmp/ for output
~/.claude/ is a sensitive write path. Claude Code blocks the Write tool there even with --permission-mode bypassPermissions. The dispatch script writes to /tmp/ first, then copies to ~/.claude/jobs/ via shell after completion.
For tasks needing multiple output files, direct the spawned Claude to ~/Claude-Stuff/ or a project directory.
Rules
- If the task involves a specific project directory, pass it as the ADD_DIR argument.
- Never dispatch tasks that need user judgment mid-execution.
- Never dispatch tasks that touch production systems, push code, or send external messages.
- The background Claude has NO access to this conversation's context. Include everything it needs in the prompt.
- Do NOT use
--bare flag (breaks keychain auth and disables hooks/plugins/CLAUDE.md).
- Tell spawned Claude to target
/tmp/ or ~/Claude-Stuff/ for writes, never ~/.claude/.
- For tasks that benefit from parallel sub-agents, use
/spawn-agent-team instead of a single dispatch.
1---2name: dispatch3description: Dispatch a task to a background Claude Code instance. Use when the user wants to kick off work without blocking the current session. Triggers on "dispatch", "background task", "run in background", "kick off", or "/dispatch".4---56# Dispatch: Background Claude Task Runner78The user wants to send a task to a separate Claude Code process running in the background.910## What to do11121. **Confirm the task.** Restate what the user wants done in one sentence. If unclear, ask.13142. **Pick permission tier.** Based on the task:15 - **Read-only** (research, summarize, explain): `"Read,Glob,Grep,Write,WebSearch,WebFetch"` (Write always included so output file can be created)16 - **Code** (fix bugs, write features, refactor): `"Read,Glob,Grep,Edit,Write,Bash(git diff *),Bash(git status *),Bash(npm test *),Bash(npx *)"`17 - **Full** (commits, installs, builds): `"Read,Glob,Grep,Edit,Write,Bash"`18 - **LLM review** (Gemini/ChatGPT): `"Read,Glob,Grep,Write,Bash"` (unrestricted Bash needed for `source ~/.api-keys-cache`)19 - Default to read-only unless the task clearly requires writes. Ask if unsure.20213. **Pick model.** For research, summaries, file searches, and template writes, add `--model sonnet` to reduce cost. Use Opus (default) for architecture, client-facing copy, and multi-step reasoning.22234. **Pick LLM target.** Default is Claude. Ask if the task warrants a different LLM:2425| Target | When to use | How it works |26|--------|-------------|--------------|27| Claude | Code tasks, file edits, multi-step work | Direct `claude -p` execution |28| Gemini | Reviews, second opinions, long analysis | `claude -p` calls `gemini-api.sh` |29| ChatGPT | Alternative perspective, copy feedback | `claude -p` calls `chatgpt-api.sh` |3031For Gemini/ChatGPT: write the prompt to a temp file first, then pass it via the script. Use unrestricted Bash tier. See `references/multi-llm-dispatch.md` for script paths and model names.32335. **Set timeout.** Timeout is the circuit breaker (`--max-turns` was removed in Claude Code 2.1):34 - Simple (research, summaries): 600s35 - Medium (file edits, multi-step): 1800s36 - Complex (builds, large refactors): 3600s3738 Requires `gtimeout` (GNU coreutils). If missing: `brew install coreutils`.39406. **Build and run.** Generate job ID and write the prompt to a temp file, then launch:4142```bash43JOB_ID="job-$(date +%Y%m%d-%H%M%S)"44PROMPT_FILE="/tmp/${JOB_ID}-prompt.txt"4546# Write full task prompt to file (include file paths, project context, done criteria)47cat > "$PROMPT_FILE" << 'PROMPT'48<the user's task prompt>49PROMPT5051# Launch in background52(bash "${CLAUDE_PLUGIN_ROOT}/skills/dispatch/scripts/dispatch.sh" \53 "$JOB_ID" "$PROMPT_FILE" "<TOOLS>" <TIMEOUT_SECS> [MODEL] [ADD_DIR]) &5455echo "Dispatched ${JOB_ID} (PID: $!)"56```5758Run with `run_in_background: false` since the subshell backgrounds via `&` and returns immediately.59607. **Report back.** Tell the user: job ID, what it's doing, timeout, how to check (`/jobs`).6162## Failure detection6364The dispatch script writes a `.meta` file with these fields:6566```67status: running|complete|failed|timed_out68task: <first 80 chars of prompt>69model: opus|sonnet|haiku70started: YYYY-MM-DD HH:MM:SS71completed: YYYY-MM-DD HH:MM:SS72output: /path/to/output.md73tools: <tool list>74exit_code: <integer>75```7677Status logic:78- `exit_code = 0` AND output file exists: `complete`79- `exit_code = 124`: `timed_out` (gtimeout killed the process)80- `exit_code != 0` OR no output file: `failed`8182If a job shows `failed`, check `~/.claude/jobs/<JOB_ID>.log` for the last 20 lines.8384## Why /tmp/ for output8586`~/.claude/` is a sensitive write path. Claude Code blocks the Write tool there even with `--permission-mode bypassPermissions`. The dispatch script writes to `/tmp/` first, then copies to `~/.claude/jobs/` via shell after completion.8788For tasks needing multiple output files, direct the spawned Claude to `~/Claude-Stuff/` or a project directory.8990## Rules9192- If the task involves a specific project directory, pass it as the ADD_DIR argument.93- Never dispatch tasks that need user judgment mid-execution.94- Never dispatch tasks that touch production systems, push code, or send external messages.95- The background Claude has NO access to this conversation's context. Include everything it needs in the prompt.96- Do NOT use `--bare` flag (breaks keychain auth and disables hooks/plugins/CLAUDE.md).97- Tell spawned Claude to target `/tmp/` or `~/Claude-Stuff/` for writes, never `~/.claude/`.98- For tasks that benefit from parallel sub-agents, use `/spawn-agent-team` instead of a single dispatch.