Br Build
Ralph Wiggum Execution — Autonomous implementation loop with circuit breakers
Claude CodeGeneric
---
name: br-build
description: "Ralph Wiggum Execution — Autonomous implementation loop with circuit breakers"
---
# BMAD-Ralph Build Phase (Ralph Wiggum Autonomous Loop)
## Pre-check
Read `.bmad-ralph/state.json`. Verify phase is `EXECUTE`.
Read the current sprint number from `current_sprint`.
## Mode Selection
If `$ARGUMENTS` contains:
- `auto` → Run ALL remaining sprints sequentially (pause between sprints for review)
- `story STORY-X.Y` → Run only a specific story
- `parallel` → Run parallel-group stories simultaneously with subagents
- Nothing → Run the current sprint
## Phase 0: Sprint Branch
1. Read `project.base_branch` from `.bmad-ralph/state.json`
2. Check if branch `bmad/sprint-<current>` exists
3. If **not**: create it from current HEAD:
```bash
git checkout -b bmad/sprint-<current>
- If yes: switch to it:
git checkout bmad/sprint-<current> - All story commits happen on this sprint branch
Phase 1: Load Sprint Context
- Read
.bmad-ralph/sprints/sprint-<current>.mdfor all stories - Read
.bmad-ralph/docs/architecture.mdfor reference - Read
.bmad-ralph/docs/prd.mdto understand the intent behind each story - Read git log to detect already-completed stories (by commit message pattern
feat(sprint-X): STORY-X.Y) - Scan the existing codebase — understand what's already there before writing anything new
- Read the dependency manifest (
package.json,cargo.toml,pyproject.toml, etc.) — know what libraries are actually available - Build a TODO list of remaining stories using TodoWrite — one todo per story, update each as
in_progress/completedin real time
Phase 2: Execute Stories (The Ralph Loop)
For EACH story in order:
Step A — Pre-flight Check
Read STORY-X.Y from the sprint file.
Check: are all dependency stories committed? (check git log)
If dependencies not met → skip, will retry later
Step B — Implement
Follow the Implementation Instructions step by step.
Create/modify the exact files listed.
Follow the architecture document for patterns and conventions.
If MCP tools are available (context7, etc.), use them to look up
current API docs BEFORE implementing — especially for libraries
you're unsure about. This prevents errors from outdated API knowledge.
Step C — Verify
1. Run the Verification Command from the story.
2. Run lint and typecheck (detect commands from package.json scripts or project config).
Fix any errors before moving on — do not skip or suppress.
Step D — Evaluate Result
If verification PASSES:
- Self-critique before committing — ask: Did I implement ALL acceptance criteria? Did I respect the architecture? Did I only touch the files listed in the story (or have a clear reason for any extra files)?
- Git commit:
git add <files> && git commit -m "feat(sprint-<N>): STORY-<N.M> <title>" - Log success to
.bmad-ralph/logs/sprint-<N>.log:[<timestamp>] STORY-<N.M> ✓ PASS (iteration <i>) - Update
.bmad-ralph/state.json:- Increment
metrics.stories_completed - Add story ID to
deliverables.implementations
- Increment
- Move to next story
If verification FAILS:
- Step back — reason before touching code:
- Is the root cause in the code I just wrote?
- Is it a missing dependency, a wrong import, or a type mismatch?
- Did I miss reading a file that defines something I'm using?
- Is this an environment issue (missing env var, missing package) vs a code issue?
- Read the FULL error output — do not skim
- Log the error to
.bmad-ralph/logs/sprint-<N>.log:[<timestamp>] STORY-<N.M> ✗ FAIL (iteration <i>): <error summary> - Circuit Breaker Check:
- Count how many times this story has failed
- If < 3 failures → fix the root cause (not a patch), retry from Step B
- If = 3 failures → ESCALATE (see Escalation Protocol)
Step E — Between Stories
After each story (pass or fail), update the state file with current progress.
Phase 3: Sprint Completion
After all stories attempted:
- Run the Sprint Verification command from the sprint file (full build + lint + test)
- If ALL pass:
- Log:
[<timestamp>] SPRINT-<N> ✓ COMPLETE - Merge sprint branch back to base:
git checkout <base_branch> git merge bmad/sprint-<N> --no-ff -m "merge: Sprint <N> complete — <sprint theme>" - If merge conflicts → report them and ask the user to resolve manually
- Update state: increment
current_sprint, set phase toREVIEW - Say: "Sprint
complete! Run /project:br-reviewfor quality gate."
- Log:
- If some stories were ESCALATED:
- Still merge what was completed (partial merge is OK)
- List the escalated stories
- Say: "Sprint
partially complete. stories escalated. Run /project:br-reviewto assess, or/project:br-build story STORY-X.Yto retry specific stories."
Escalation Protocol
When a story fails 3 times (circuit breaker triggered):
Write detailed error analysis to
.bmad-ralph/logs/escalation-STORY-<N.M>.md:# Escalation: STORY-<N.M> ## Story Description <from sprint file> ## Attempt 1 - Action taken: <what was implemented> - Error: <exact error output> ## Attempt 2 - Action taken: <what was changed> - Error: <exact error output> ## Attempt 3 - Action taken: <what was changed> - Error: <exact error output> ## Root Cause Analysis <analysis of why this keeps failing> ## Recommendation <what needs to change in the architecture or story to make this work>Update state: increment
metrics.escalations_to_architectDO NOT keep retrying. Move to the next story.
Parallel Execution Mode
When $ARGUMENTS contains parallel:
- Read the sprint file for stories with the same
Parallel Group - Launch one subagent per parallel group simultaneously:
Agent 1 → stories in Group A (in a wo
Maintain Br Build?
Let people know it's listed here — add the badge (live metrics, light/dark aware) or a plain link to your README or docs.
[Br Build on getagentictools](https://getagentictools.com/loops/franlao-bmad-ralph-build-phase-ralph-wiggum-autonomous-loop?ref=badge)