Batch Run

Run batch implementation with configurable loops, guardrails, and PR review

Lil-Nugs updated 6mo ago
Claude CodeGeneric
View source ↗
---
description: Run batch implementation with configurable loops, guardrails, and PR review
allowed-tools: Bash(*), Read(*), Write(*), Edit(*), Glob(*), Grep(*), Task(*), TodoWrite(*)
---

# Batch Run Orchestrator

You are a BATCH ORCHESTRATOR. Your job is to implement ready beads on a single branch, committing after each successful bead, then create a PR with automated review.

## Arguments

Parse these from the command invocation:

| Argument | Description | Default |
|----------|-------------|---------|
| `--loops=N` | Max implement/review cycles | unlimited |
| `--max-time=Xh` | Time limit (e.g., `4h`, `2h`) | 8h |
| `--max-beads=N` | Stop after closing N beads | unlimited |
| `short` | Preset: 3 loops, 1h max | - |
| `medium` | Preset: 10 loops, 4h max | - |
| `overnight` | Preset: no loop limit, 8h max | - |

**Examples:**
- `/batch-run --loops=5` - Run up to 5 cycles
- `/batch-run --loops=10 --max-time=2h` - 10 cycles or 2 hours, whichever first
- `/batch-run overnight` - Full overnight run
- `/batch-run short` - Quick 3-cycle run

## Static Guardrails (Always Enforced)

Track these counters throughout the session:

| Guardrail | Threshold | Action |
|-----------|-----------|--------|
| Same bead fails | 2x | Skip that bead, continue |
| Total failures | 5 | Stop session |
| Same test issue fails | 3x | Stop session |
| Files changed | 50 | Stop session |

## Session State

Track this state throughout:

start_time: loops_completed: 0 beads_closed: 0 total_failures: 0 bead_failures: {} # bead_id -> failure_count files_changed: [] skipped_beads: []


---

## PHASE 1: SETUP

```bash
# Record start time
echo "Batch run started at $(date)"

# Ensure clean state
git checkout main
git pull

# Create batch branch
git checkout -b batch-$(date +%Y%m%d-%H%M)

PHASE 2: MAIN LOOP

Repeat until a stop condition is met:

Step 2.1: Check Stop Conditions

Before each iteration, verify:

  1. loops_completed < max_loops (if set)
  2. elapsed_time < max_time
  3. beads_closed < max_beads (if set)
  4. total_failures < 5
  5. bd ready has work available

If any condition fails, go to PHASE 3: FINALIZE.

Step 2.2: Get Ready Work

bd ready

If no ready beads, go to PHASE 3.

Select ONE bead to implement. Prefer:

  1. P0/P1 fix beads (from previous review)
  2. Tasks that unblock other work
  3. Highest priority ready bead

Step 2.3: Implement

Mark in progress:

bd update <bead-id> --status=in_progress

Spawn implementation sub-agent:

Task(subagent_type='general-purpose', prompt='Implement bead <bead-id>.
Read the bead details with `bd show <bead-id>`.
Follow existing code patterns.
Write tests for new functionality.
Run tests before finishing.
Do NOT commit - just implement and report success/failure.')

Step 2.4: Verify & Commit

If implementation succeeded and tests pass:

# Stage changes
git add -A

# Check what changed
git diff --cached --stat

# Commit with bead reference
git commit -m "$(cat <<'EOF'
Implement <bead-id>: <short description>

- <key change 1>
- <key change 2>

🤖 Generated with [Claude Code](https://claude.com/claude-code)
EOF
)"

# Close the bead
bd close <bead-id>
bd sync

# Update counters
beads_closed++

Update files_changed list. If total unique files > 50, go to PHASE 3.

If implementation failed:

# Increment failure counters
bead_failures[<bead-id>]++
total_failures++

# If bead failed twice, skip it
if bead_failures[<bead-id>] >= 2:
    skipped_beads.append(<bead-id>)
    bd update <bead-id> --status=open  # Reset status

Step 2.5: Quick Review

Spawn review sub-agent:

Task(subagent_type='general-purpose', prompt='/review-implementation --no-pr

Focus on the most recent commit only. Create fix beads for any issues found.
Do NOT create a PR.')

Step 2.6: Handle Fix Beads

bd list --status=open --type=bug --priority=0,1

If critical/high fix beads exist from the review, they take priority in the next iteration.

Step 2.7: Continue Loop

loops_completed++
bd sync

Go back to Step 2.1.


PHASE 3: FINALIZE

Always run this phase, even if stopped early.

Step 3.1: Final Sync

bd sync
git status

Step 3.2: Generate Summary

Collect:

  • Total beads closed
  • Total commits made
  • Files changed
  • Stop reason
  • Skipped beads (and why)
  • Time elapsed

Step 3.3: Create PR

# Check if there are changes to PR
if git diff main --quiet; then
    echo "No changes to create PR for"
    # Skip to cleanup
else
    git push -u origin $(git branch --show-current)

    gh pr create --title "Batch run: $(date +%Y-%m-%d)" --body "$(cat <<'EOF'
## Summary

Automated batch implementation run.

**Stats:**
- Beads closed: <N>
- Commits: <N>
- Files changed: <N>
- Duration: <time>

**Stop reason:** <reason>

## Beads Implemented

<list from bd list --status=closed>

## Skipped Beads

<list with reasons>

---

🤖 Generated with [Claude Code](https://claude.com/claude-code)
EOF
)"
fi

Step 3.4: PR Review Agent

Spawn a review agent to analyze the PR:

Task(subagent_type='general-purpose', prompt='You are a PR REVIEWER. Review the PR that was just created on this branch.

1. Get the PR number:
   gh pr list --head $(git branch --show-current) --json number -q ".[0].number"

2. Review all changes:
   gh pr diff <pr-number>

3. Read the PR review guide:
   Read docs/PR_REVIEW_GUIDE.md

4. Post a review comment using this structure:

gh pr comment <pr-number> --body "$(cat <<REVIEW
## Automated Code Review

### Critical Issues (must fix before merge)
<list issues that could cause bugs, security problems, or test failures>

### Recommendations (should consider)
<list improvements to code quality, patterns, or test coverage>

### Areas Needing Human Attention
<list architectural decisions, business logic, or risky changes>

### Test Coverage Assessment
<note which changes have tests and which do not>

### Summary
<overall a

Maintain Batch Run?

Let people know it's listed here — add the badge (live metrics, light/dark aware) or a plain link to your README or docs.

[Batch Run on getagentictools](https://getagentictools.com/loops/lil-nugs-batch-run-orchestrator?ref=badge)