---
name: business-analyse-develop
description: Autonomous development from PRD — implements the full application with MCP validation, quality gates, and completion guarantees.
argument-hint: "[-m N] [-c TEXT] [-v] <task description>"
---

<objective>
Execute the Ralph Weegund technique — module orchestration with autonomous code generation. Ralph reads PRDs from `/business-analyse-handoff`, resolves dependencies, and delegates all code generation to `/apex -d`.

**Key principle:** Ralph ORCHESTRATES (module sequencing, quality gates, reporting). All code generation is delegated to `/apex`.
</objective>

<quick_start>

**From Business Analysis (RECOMMENDED):**
```bash
/business-analyse MyFeature
# Then: /business-analyse-handoff
# Then: /business-analyse-develop -r
```

**Manual start:**
```bash
/business-analyse-develop implement user authentication
/business-analyse-develop -c "COMPLETE" -m 20 refactor cache layer
/business-analyse-develop -r  # Resume previous
```

**Flags:** `-m N` (max iterations), `-c TEXT` (completion promise), `-v` (verbose), `-r` (resume), `-p` (parallel)

</quick_start>

<parameters>
| Short | Long | Description |
|-------|------|-------------|
| `-m N` | `--max-iterations N` | Max iterations (default: 50, v4: adaptive) |
| `-c TEXT` | `--completion-promise TEXT` | Completion signal text |
| `-v` | `--verbose` | Detailed logging |
| `-r` | `--resume` | Resume from previous state |
| `-p` | `--parallel` | Teams mode: Phase 0 sequential, then parallel per module |
</parameters>

<prd_format_detection>

## PRD Format Detection

Ralph supports two PRD formats:

| | PRD v3 (task-oriented) | PRD v4 (spec-oriented) |
|---|---|---|
| **Detection** | `$version === '3.0.0'` + `tasks[]` present | `$version === '4.0.0'` + `objectives[]` present |
| **Execution model** | Batch iterations (compact-loop) | Continuous session (compaction manages context) |
| **Context management** | Manual: "Read Once" rule, max 5 tasks/batch | Automatic: server-side compaction |
| **Progress tracking** | progress.txt as state machine | Optional checkpoints |
| **Splitting** | By token budget (>4 entities) | By logical feature (by section) |
| **Steps** | 6 steps + compact-loop.md | 3 steps: init → execute → verify |

**Backward compatibility:** v3 PRDs execute with the EXACT same behavior as before. v4 is opt-in via `/business-analyse-handoff --v4`.

</prd_format_detection>

<workflow>

## v4 Workflow (spec-oriented — RECOMMENDED)

**Single module:**
1. **Init** (step-00): parse flags, verify MCP, load PRD v4, detect modules
2. **Execute** (step-01-v4): delegate entire module to `/apex -d` as one continuous session
   - `/apex` reads the spec (architecture, objectives, acceptance criteria)
   - `/apex` decides execution order (domain → infra → app → api → seed → frontend → tests)
   - Ralph intercepts at natural checkpoints for quality gates
   - Server-side compaction manages context automatically
3. **Verify + Report** (step-02-v4): run ALL quality gates, generate report

**Multi-module:**
1. Init: detect prd-*.json files, build topological order
2. For each module in order → Execute (continuous session per module)
3. Cross-module verify + final report

**Quality gates (same as v3):**
- `dotnet build` (BLOCKING)
- `dotnet test` (BLOCKING after build)
- `npm test` / `npm run typecheck` (BLOCKING for frontend)
- MCP `validate_security` + `validate_conventions`
- BR coverage check (each BR has implementation)
- File reconciliation (expectedFiles vs disk)
- Stub test detection (reject empty tests)

## v3 Workflow (task-oriented — LEGACY)

**Single module:**
1. Init: parse flags, verify MCP, setup .ralph/ (step-00)
2. Load tasks from prd.json — unified v3 has pre-computed tasks (step-01, step FILE read once)
3. Execute first task (step-02, step FILE read once)
4. Commit + update progress (step-03, step FILE read once)
5. Check completion → enter COMPACT LOOP (step-04)
6. **COMPACT LOOP** (step-04 → references/compact-loop.md):
   - Find eligible → batch by category (max 5) → execute → test → commit → loop
7. Report (step-05)

**Multi-module with `-p` (parallel mode):**
1. Init: detect prd-*.json, parse `-p` flag (step-00)
2. **Phase 0 (Ralph sequential):** ALL entities from ALL modules via `/apex --foundation`
3. **Phase 1-N (Teams parallel):** Spawn teammates per module
4. Report with per-module aggregation (step-05)

**Multi-module WITHOUT `-p` (sequential — legacy):**
1. Init: detect prd-*.json, read dependency layers (step-00)
2. Compact loop per module in topological order
3. Report with per-module aggregation (step-05)

</workflow>

<state_variables>
| Variable | Type | v3 | v4 | Description |
|----------|------|----|----|-------------|
| `{max_iterations}` | number | Yes | Adaptive | Max iterations |
| `{current_iteration}` | number | Yes | No | Current iteration counter |
| `{current_module}` | string | Yes | Yes | Current module being processed |
| `{modules_queue}` | object | Yes | Yes | Module queue (multi-module) |
| `{prd_version}` | '3.0.0' \| '4.0.0' | Yes | Yes | Detected PRD format |
| `{section_split_mode}` | boolean | Yes | No | v3 only: section splitting active |
| `{parallel_mode}` | boolean | Yes | Yes | Teams mode via `-p` |
</state_variables>

<mcp_requirements>
**MANDATORY before any work:**
1. `mcp__smartstack__validate_conventions` — verify connectivity
2. `mcp__context7__resolve-library-id` — verify connectivity

**During work:** All MCP tool calls are handled by `/apex` during delegation.
</mcp_requirements>

<execution_guarantee>

## EXECUTION GUARANTEE — READ THIS FIRST

**Ralph-loop executes ALL work from start to finish. No exceptions.**

When the user invokes `/business-analyse-develop`, they are giving you the instruction to:
1. Initialize (step-00)
2. Execute ALL modules autonomously
3. Run quality gates
4. Generate the final report

**You MUST NOT:**
- Stop to "ask for confirmation"
- Set `max_iterations = 1` or any reduced value
- Ask the user to choose between supervised/autonomous mode
- Wait for user input between modules
- Delegate the loop to sub-agents (except team mode and /apex delegation)
- Display a progress report then ask "shall I continue?" or present options (1/2/3)
- Pause because the scope is "large" or "ambitious" — execute ALL of it regardless of size

**The ONLY valid stop conditions are:**
- ALL objectives met / ALL tasks completed (→ report)
- `max_iterations` reached (v3 only)
- Dead-end (all remaining blocked/failed after retries)
- User Ctrl+C

**If you stop for any other reason, you have VIOLATED this guarantee.**

</execution_guarantee>

<entry_point>
**FIRST ACTION:** Load `steps/step-00-init.md` — then execute ALL steps through to completion without stopping.
</entry_point>

<step_files>

### v4 Steps (spec-oriented)
| Step | File | Purpose |
|------|------|---------|
| 00 | `steps/step-00-init.md` | Parse args, verify MCP, detect format, init state |
| 01-v4 | `steps/step-01-v4-execute.md` | Continuous execution: delegate to /apex, run gates |
| 02-v4 | `steps/step-02-v4-verify.md` | Final gates, file reconciliation, BR coverage, report |

### v3 Steps (task-oriented — legacy)
| Step | File | Purpose |
|------|------|---------|
| 00 | `steps/step-00-init.md` | Parse args, verify MCP, init state |
| 01 | `steps/step-01-task.md` | Load/create tasks from prd.json |
| 02 | `steps/step-02-execute.md` | Delegate module to /apex -d |
| 03 | `steps/step-03-commit.md` | Commit, update prd.json/progress.txt |
| 04 | `steps/step-04-check.md` | Check completion, compact loop entry |
| 05 | `steps/step-05-report.md` | Generate final report |

**v3 "Read Once" rule:** Step files read once, compact-loop.md drives iterations 2+.
**v4:** No "Read Once" rule needed — compaction manages context automatically.

**Reference files (loaded conditionally):**
| File | Loaded when |
|------|-------------|
| `references/compact-loop.md` | v3 only: Step-04 section 5 |
| `references/section-splitting.md` | v3 only: >4 entities + >1 section |
| `references/category-completeness.md` | v3: step-01/step-04; v4: step-02-v4 |
| `references/init-resume-recovery.md` | Both: Step-00 section 4 (resume mode) |
| `references/multi-module-queue.md` | Both: multi-module initialization |
| `references/quality-gates.md` | v4: step-01-v4 and step-02-v4 |
</step_files>

<file_structure>
```
.ralph/
├── prd.json              # v3: unified task list OR v4: spec document
├── prd-{module}.json     # Per-module PRDs (from business-analyse-handoff)
├── progress.txt          # v3: state machine; v4: optional checkpoints
├── modules-queue.json    # Multi-module tracking
├── logs/
└── reports/
    └── {feature}.md
```
</file_structure>

<execution_rules>

### NON-NEGOTIABLE — Autonomous Full Execution
- **EXECUTE ALL WORK** — from first module to last, no user interaction
- **NEVER ask the user** — no mode selection, no "shall I continue?"
- **NEVER stop prematurely** — only valid stops listed in execution_guarantee
- **VERIFY MCP FIRST** — never skip

### Quality Gates (both v3 and v4)
- **BUILD GATE** — `dotnet build` must pass (BLOCKING)
- **BACKEND TEST GATE** — `dotnet test` must pass (BLOCKING)
- **FRONTEND TEST GATE** — `npm test` / `npm run typecheck` (BLOCKING)
- **STUB TEST DETECTION** — reject `Assert.True(true)` or empty test methods
- **MCP SECURITY** — validate_security + validate_conventions
- **BR COVERAGE** — every business rule has implementation + test
- **FILE RECONCILIATION** — expectedFiles vs actual files on disk
- **MIGRATION** — after EF configs, before tests

### Error Recovery
- **v3:** Failed tasks retry up to 3 times, context NOT enriched
- **v4:** Failures handled in same session — Claude has full error context via compaction, retries naturally with different approach

</execution_rules>

<success_criteria>
- All objectives met (v4) / all tasks completed (v3)
- MCP validations pass (validate_security + validate_conventions)
- All quality gates pass (build, test, BR coverage, file reconciliation)
- Git commits atomic with commit_hash tracked
- Tests pass (dotnet test + npm test)
- Migrations created and applied
- Seed data complete (navigation, permissions, roles via MCP)
- Frontend in correct hierarchy (Context/App/Module)
- Report generated in .ralph/reports/
- Multi-module: all modules processed, cross-module report
</success_criteria>
