--- name: worker-dispatch description: Use to spawn isolated worker processes for autonomous issue work. Uses Task tool with run_in_background for parallel execution and TaskOutput for monitoring. Pre-extracts context to minimize worker token usage. allowed-tools: - Bash - Read - Task - TaskOutput - mcp__github__* - mcp__memory__* model: opus --- # Worker Dispatch ## Overview Spawns and manages worker Claude agents using the **Task tool with `run_in_background: true`**. Workers run as parallel background agents monitored via **TaskOutput**. **Core principle:** Workers are isolated, scoped, and expendable. State lives in GitHub, not in workers. **Key optimization:** Pre-extract issue context BEFORE spawning. Workers receive focused context, not raw issues. ## Worktree Isolation (MANDATORY) **CRITICAL:** Every worker MUST have its own git worktree. Workers NEVER operate in the main repository. ``` Main Repository (./) ← Orchestrator only │ └── Worktrees (isolated) ├── ../project-worker-123/ ← Worker for #123 ├── ../project-worker-124/ ← Worker for #124 └── ../project-worker-125/ ← Worker for #125 ``` **Orchestrator responsibility:** Create worktree BEFORE spawning worker. **Worker responsibility:** Verify isolation BEFORE any work (see `worker-protocol`). This prevents file clobbering between parallel workers. ## Worker Types | Type | Subagent | Purpose | When to Use | |------|----------|---------|-------------| | Implementation | `general-purpose` | Full feature work | Standard issue work | | Research | `Explore` | Read-only investigation | Pre-implementation analysis, debugging | | PR Resolution | `general-purpose` | Fix CI, merge PRs | Existing PR cleanup | ## Pre-Extraction (CRITICAL) **Extract issue context BEFORE spawning.** Workers should not spend tokens re-reading issues. ```bash extract_issue_context() { local issue=$1 # Single API call to get everything ISSUE_JSON=$(gh issue view "$issue" --json title,body,labels,comments,assignees) TITLE=$(echo "$ISSUE_JSON" | jq -r '.title') BODY=$(echo "$ISSUE_JSON" | jq -r '.body') LABELS=$(echo "$ISSUE_JSON" | jq -r '[.labels[].name] | join(", ")') # Extract acceptance criteria if present ACCEPTANCE=$(echo "$BODY" | sed -n '/## Acceptance Criteria/,/^## /p' | head -20) [ -z "$ACCEPTANCE" ] && ACCEPTANCE=$(echo "$BODY" | sed -n '/- \[/p' | head -10) # Get latest handover if exists HANDOVER=$(echo "$ISSUE_JSON" | jq -r ' [.comments[] | select(.body | contains(""))] | last | .body // "" ') # Get recent progress comments PROGRESS=$(echo "$ISSUE_JSON" | jq -r ' [.comments[-3:][].body] | join("\n---\n") ' | head -50) } ``` ## Spawning Implementation Workers ### Step 1: Extract Context & Create Worktree ```bash spawn_implementation_worker() { local issue=$1 local attempt=${2:-1} # Pre-extract context extract_issue_context "$issue" worker_id="worker-$(date +%s)-$issue" issue_slug=$(echo "$TITLE" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9]/-/g' | cut -c1-40) branch="feature/$issue-$issue_slug" worktree_path="../$(basename $PWD)-worker-$issue" # Create worktree git fetch origin main git branch "$branch" origin/main 2>/dev/null || true git worktree add "$worktree_path" "$branch" } ``` ### Step 2: Register in GitHub ```bash register_worker() { local issue=$1 worker_id=$2 worktree=$3 gh issue comment "$issue" --body " {\"worker_id\": \"$worker_id\", \"assigned_at\": \"$(date -u +%Y-%m-%dT%H:%M:%SZ)\"} **Worker Assigned:** \`$worker_id\` at $(date -u +%H:%M:%S)Z" update_project_status "$issue" "In Progress" } ``` ### Step 3: Construct Focused Prompt **~50 lines with pre-extracted context:** ```bash construct_worker_prompt() { local issue=$1 worker_id=$2 attempt=$3 # Uses pre-extracted: TITLE, ACCEPTANCE, HANDOVER, PROGRESS cat < markers. $([ -n "$HANDOVER" ] && echo "## Previous Handover $HANDOVER") $([ -n "$PROGRESS" ] && echo "## Recent Activity $PROGRESS") Begin implementation now. PROMPT } ``` ### Step 4: Spawn Worker ```markdown Task( description: "Issue #[ISSUE] worker", prompt: [FOCUSED_PROMPT], subagent_type: "general-purpose", run_in_background: true ) ``` **Returns:** `task_id` for monitoring. ## Spawning Research Workers Use `Explore` subagent for read-only investigation before implementation: ```bash construct_research_prompt() { local issue=$1 question=$2 cat < {\"assigned\": false, \"cleared_at\": \"$(date -u +%Y-%m-%dT%H:%M:%SZ)\"} **Worker Complete:** $result" case "$result" in COMPLETED) update_project_status "$issue" "In Review" ;; BLOCKED) update_project_status "$issue" "Blocked" ;; HANDOVER) ;; # Keep In Progress, spawn replacement esac } ``` ## Replacement Workers When worker returns HANDOVER: 1. Handover context is in issue comments (already posted by worker) 2. Extract via: `extract_issue_context` (includes HANDOVER variable) 3. Spawn replacement with attempt+1 4. New worker receives full context automatically ## Checklist **Before spawning:** - [ ] Issue context pre-extracted - [ ] Worktree created on feature branch - [ ] Worker registered in GitHub - [ ] Project board status: In Progress **Prompt includes:** - [ ] Pre-extracted title and acceptance criteria - [ ] JSON output format requirement - [ ] Previous handover (if any) - [ ] Recent progress comments **On completion:** - [ ] JSON result parsed - [ ] GitHub state updated - [ ] Project board status updated - [ ] Task_id removed from active list ## Integration **Used by:** `autonomous-orchestration`, `claude-autonomous --pr` **Uses:** `worker-protocol` (behavior contract), `worker-handover` (context format), `ci-monitoring` (PR workers)