---
name: step-00-init
description: Initialize Ralph loop - parse args, verify MCP, setup state
next_step: steps/step-01-task.md
---

# Step 0: Initialization

## MANDATORY EXECUTION RULES:

- NEVER skip MCP verification
- ALWAYS parse ALL flags before any other action
- ONLY check for resume if -r flag is set
- YOU ARE AN INITIALIZER, not an executor
- FORBIDDEN to load step-01 until init is complete

## YOUR TASK:

Initialize the Ralph loop by parsing flags, verifying MCP availability, and setting up the execution environment.

---

<defaults>
## Default Configuration

```yaml
max_iterations: 50
completion_promise: null
verbose_mode: false
resume_mode: false
current_iteration: 1
```
</defaults>

---

## EXECUTION SEQUENCE:

### 1. Parse Flags and Input

**Step 1: Load defaults from config above**

```
{max_iterations}     = 50
{completion_promise} = null
{verbose_mode}       = false
{resume_mode}        = false
{current_iteration}  = 1
```

**Step 2: Parse user input and override defaults:**

```
Flags:
  -m N or --max-iterations N     -> {max_iterations} = N
  -c TEXT or --completion-promise TEXT -> {completion_promise} = TEXT
  -v or --verbose                -> {verbose_mode} = true
  -r or --resume                 -> {resume_mode} = true

Remainder -> {task_description}
```

### 2. Verify MCP Servers (MANDATORY)

**CRITICAL: Do NOT proceed without MCP verification!**

**Check SmartStack MCP:**
```
mcp__smartstack__validate_conventions:
  checks: ["all"]
  (just to verify connectivity)
```

**Check Context7 MCP:**
```
mcp__plugin_context7_context7__resolve-library-id:
  libraryName: "test"
  query: "connectivity check"
  (just to verify connectivity)
```

**If ANY MCP fails:**
```
╔══════════════════════════════════════════════════════════════════╗
║  ❌ MCP SERVER UNAVAILABLE                                        ║
╠══════════════════════════════════════════════════════════════════╣
║  Server: {failed_server}                                          ║
║  Status: Connection failed                                        ║
╠══════════════════════════════════════════════════════════════════╣
║  RALPH CANNOT PROCEED WITHOUT MCP                                 ║
║                                                                    ║
║  Troubleshooting:                                                  ║
║  1. Run: smartstack check-mcp                                      ║
║  2. Restart Claude Code                                            ║
║  3. Check MCP server configuration                                 ║
╚══════════════════════════════════════════════════════════════════╝

STOP - Do not proceed.
```

### 3. Check Resume Mode

**If {resume_mode} = true:**

1. Check for existing `.ralph/prd.json`
2. If found:
   - Read prd.json to restore state
   - Read progress.txt for context
   - Set {current_iteration} from state
   - Continue to step-01
3. If not found:
   - Error: "No active Ralph loop to resume"
   - Ask user to start new loop

### 4. Initialize .ralph/ Structure

**Create directory structure:**

```
.ralph/
├── prd.json          # Will be created in step-01
├── progress.txt      # Will be created in step-01
├── logs/
└── reports/
```

**Commands:**
```bash
mkdir -p .ralph/logs
mkdir -p .ralph/reports
```

### 5. Validate Completion Promise

**If {completion_promise} is null:**

```yaml
AskUserQuestion:
  header: "Promise"
  question: "What text should signal completion?"
  options:
    - label: "COMPLETE"
      description: "Standard completion signal"
    - label: "ALL TESTS PASS"
      description: "For test-focused tasks"
    - label: "DONE"
      description: "Simple completion"
    - label: "Custom..."
      description: "Specify custom text"
```

### 6. Log Initialization

**If {verbose_mode} = true:**

Write to `.ralph/logs/{timestamp}.log`:
```
[{timestamp}] RALPH LOOP INITIALIZED
Task: {task_description}
Max iterations: {max_iterations}
Completion promise: {completion_promise}
MCP Status: SmartStack ✅, Context7 ✅
```

### 7. Show Summary and Proceed

**Always show COMPACT summary:**

```
╔══════════════════════════════════════════════════════════════════╗
║  RALPH LOOP INITIALIZED                                           ║
╠══════════════════════════════════════════════════════════════════╣
║  Task: {task_description}                                         ║
║  Max iterations: {max_iterations}                                 ║
║  Completion: <promise>{completion_promise}</promise>              ║
║  MCP: ✅ Ready                                                    ║
╠══════════════════════════════════════════════════════════════════╣
║  Files:                                                           ║
║  - .ralph/prd.json (tasks)                                        ║
║  - .ralph/progress.txt (memory)                                   ║
╚══════════════════════════════════════════════════════════════════╝

-> Loading tasks...
```

**Then proceed directly to step-01-task.md**

---

## SUCCESS METRICS:

- All flags correctly parsed
- MCP servers verified and available
- .ralph/ directory structure created
- Completion promise defined
- Output is COMPACT
- Proceeded to step-01 immediately after summary

## NEXT STEP:

After showing initialization summary, always proceed directly to `./step-01-task.md`
