$npx -y skills add dsifry/metaswarm --skill pr-shepherdMonitor a PR through to merge — handle CI failures, review comments, and thread resolution automatically until all checks pass
| 1 | # pr-shepherd |
| 2 | |
| 3 | Use when a PR has been created and needs to be monitored through to merge - handles CI failures, review comments, and thread resolution automatically until all checks pass and all threads are resolved. |
| 4 | |
| 5 | **IMPORTANT**: This skill is designed for the **agent working in a worktree**, NOT the orchestrator. The agent handles its own PR monitoring so the orchestrator remains free for other work. |
| 6 | |
| 7 | ## Coordination Mode Note |
| 8 | |
| 9 | This skill supports both coordination modes: |
| 10 | |
| 11 | - **Task Mode** (default): Runs as a single long-running `Task()` with `run_in_background: true`. Orchestrator checks via `TaskOutput(block: false)`. |
| 12 | - **Team Mode**: Runs as a persistent `shepherd` teammate in the `issue-{number}` team. Sends async status updates via `SendMessage` (CI failure, review comments, all-green, PR merged). Orchestrator can respond with instructions (e.g., "defer that comment, create an issue instead"). Responds to `shutdown_request` for graceful exit. |
| 13 | |
| 14 | All monitoring, fixing, and review handling logic is identical in both modes. See `./guides/agent-coordination.md` for mode detection. |
| 15 | |
| 16 | --- |
| 17 | |
| 18 | ## When to Activate |
| 19 | |
| 20 | Activate this skill when ANY of these conditions are true: |
| 21 | |
| 22 | - Agent just created a PR with `gh pr create` |
| 23 | - User asks to "shepherd", "monitor", or "see through" a PR |
| 24 | - User invokes `/pr-shepherd <pr-number>` |
| 25 | - User asks to "watch this PR" or "handle this PR until it's merged" |
| 26 | - Orchestrator spawned you with instructions to shepherd a PR |
| 27 | - **Automatic**: `bin/create-pr-with-shepherd.sh` was used (outputs shepherd instructions) |
| 28 | |
| 29 | ### Automatic Activation via Wrapper Script |
| 30 | |
| 31 | When `bin/create-pr-with-shepherd.sh` creates a PR, it outputs shepherd instructions: |
| 32 | |
| 33 | ```text |
| 34 | ========================================== |
| 35 | PR Shepherd Active for PR #123 |
| 36 | ========================================== |
| 37 | |
| 38 | The pr-shepherd skill will: |
| 39 | - Monitor CI/CD status |
| 40 | - Auto-fix lint, type, and test issues |
| 41 | - Handle review comments |
| 42 | - Resolve threads after addressing feedback |
| 43 | - Report when PR is ready to merge |
| 44 | |
| 45 | To manually invoke shepherd later: |
| 46 | /pr-shepherd 123 |
| 47 | ``` |
| 48 | |
| 49 | When you see this output, **immediately invoke the pr-shepherd skill** with the PR number shown. |
| 50 | |
| 51 | ## Announce at Start |
| 52 | |
| 53 | "I'm using the pr-shepherd skill to monitor this PR through to merge. I'll watch CI/CD, handle review comments, and fix issues as they arise." |
| 54 | |
| 55 | ## For Orchestrators: Spawning Agents with PR Shepherding |
| 56 | |
| 57 | When spawning an agent to work in a worktree, include PR shepherding in the task prompt: |
| 58 | |
| 59 | ```text |
| 60 | Work in worktree at /path/to/worktree on branch feature/xyz. |
| 61 | |
| 62 | Task: [describe the implementation task] |
| 63 | |
| 64 | After creating the PR: |
| 65 | 1. Use the pr-shepherd skill to monitor it through to merge |
| 66 | 2. Handle CI failures and review comments autonomously |
| 67 | 3. Only escalate to orchestrator for complex issues requiring user input |
| 68 | 4. Report back when PR is ready to merge or if blocked |
| 69 | |
| 70 | Run in background so I can continue other work. |
| 71 | ``` |
| 72 | |
| 73 | **Key principle**: The agent owns its PR lifecycle. The orchestrator spawns and forgets, checking back via `AgentOutputTool` when needed. |
| 74 | |
| 75 | ## State Machine |
| 76 | |
| 77 | The agent operates in one of these states: |
| 78 | |
| 79 | ```text |
| 80 | MONITORING → FIXING → MONITORING → WAITING_FOR_USER → FIXING → MONITORING → DONE |
| 81 | ``` |
| 82 | |
| 83 | | State | What Happens | Exit When | |
| 84 | | ------------------ | ------------------------------------------- | ---------------------------------------------- | |
| 85 | | `MONITORING` | Poll CI and reviews every 60s in background | CI fails, new comments, all done, or need help | |
| 86 | | `FIXING` | Fix issues using TDD, run local validation | Local validation passes OR need user guidance | |
| 87 | | `HANDLING_REVIEWS` | Invoke `handling-pr-comments` skill | Comments handled OR need user input | |
| 88 | | `WAITING_FOR_USER` | Present options, wait for user decision | User responds | |
| 89 | | `DONE` | All CI green + all threads resolved | Exit successfully | |
| 90 | |
| 91 | ## Phase 1: Initialize |
| 92 | |
| 93 | ```bash |
| 94 | # Get PR info |
| 95 | PR_NUMBER=$(gh pr view --json number -q .number 2>/dev/null) |
| 96 | OWNER=$(gh repo view --json owner -q .owner.login) |
| 97 | REPO=$(gh repo view --json name -q .name) |
| 98 | |
| 99 | # If no PR on current branch, check if number was provided |
| 100 | if [ -z "$PR_NUMBER" ]; then |
| 101 | echo "No PR found for current branch. Provide PR number." |
| 102 | exit 1 |
| 103 | fi |
| 104 | |
| 105 | echo "Shepherding PR #$PR_NUMBER" |
| 106 | ``` |
| 107 | |
| 108 | ## Phase 2: Monitoring Loop (Background) |
| 109 | |
| 110 | Run GTG every 60 seconds as the **single source of truth** for PR readiness: |
| 111 | |
| 112 | ### Primary Check: GTG (Good-To-Go) |
| 113 | |
| 114 | GTG consolidates CI status, comment classification, and thread resolution into one call. Use it instead of separate API queries. |
| 115 | |
| 116 | ```bash |
| 117 | # Primary readiness check — structured JSON output |
| 118 | GTG_RESULT=$(gtg $PR_NUMBER --repo |