---
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 RULES:

- **FULLY AUTONOMOUS** — execute ALL tasks without stopping or asking the user
- NEVER skip MCP verification
- ALWAYS parse ALL flags before any action
- ONLY check resume if -r flag is set
- **NEVER ask the user** to choose a mode, confirm execution, or select options
- **NEVER set max_iterations = 1** — unless the user explicitly passed `-m 1`
- **Multi-module (2+ modules):** Use Agent Teams for parallel execution

## YOUR TASK:

Initialize the Ralph loop: parse flags, verify MCP, setup environment, detect multi-module.

---

## 1. Parse Flags

```yaml
Defaults:
  max_iterations: 50
  completion_promise: null
  verbose_mode: false
  resume_mode: false
  parallel_mode: false

Flags:
  -m N / --max-iterations N → {max_iterations} = N
  -c TEXT / --completion-promise TEXT → {completion_promise} = TEXT
  -v / --verbose → {verbose_mode} = true
  -r / --resume → {resume_mode} = true
  -p / --parallel → {parallel_mode} = true
  Remainder → {task_description}
```

**If `-p`:** set parallel_mode = true. Ralph executes Phase 0 (entities) sequentially, then spawns parallel teammates.

## 2. Verify MCP (MANDATORY)

```
mcp__smartstack__validate_conventions: checks: ["all"] → {mcp_smartstack} = true/false
mcp__context7__resolve-library-id: libraryName: "test" → {mcp_context7} = true/false
```

If ANY fails: show error, suggest `smartstack check-mcp`, STOP.

## 3. Execution Mode (FULLY AUTONOMOUS)

Ralph-loop is FULLY AUTONOMOUS by default. It executes ALL tasks from start to finish WITHOUT stopping.

**DO NOT** ask the user to confirm, choose a mode, or approve execution.
**DO NOT** set max_iterations = 1 or any reduced value.

The loop runs autonomously until:
- ALL tasks are completed (success)
- max_iterations is reached (user set via `-m N`)
- Dead-end detected (all remaining blocked/failed)
- User Ctrl+C (external interrupt)

### Speed Warning (informational only)

If tests are slow (>30s in logs), display warning but **continue execution**:
```
⚠ Slow tests detected. Consider disabling E2E during ralph-loop.
```

## 4. Resume or Initialize

See `references/init-resume-recovery.md` for complete Resume Mode and Auto-Recovery logic.

**Quick:**
- If `-r` flag: restore state from .ralph/prd.json
- Else if BA artifacts exist: auto-recover PRDs via `ss derive-prd`
- Else: fresh start

---

## 4b. Detect Multi-Module PRDs

See `references/multi-module-queue.md` for queue initialization.

Quick: `glob('.ralph/prd-*.json').length > 1` → create modules-queue.json

---

## 4c. Team Orchestration (multi-module, 2+ modules)

```javascript
if (PRD_COUNT > 1) {
  // Read references/team-orchestration.md for full protocol
  const layers = master.metadata?.workflow?.dependencyGraph?.layers;

  if (layers) {
    // PARALLEL MODE: Use Agent Teams
    TeamCreate({ team_name: `ralph-${appName}` });
    // Spawn teammates per layer (see reference)
    // STOP here — team orchestration takes over
    return;
  }
  // If no layers: fall through to sequential mode
}
```

---

## 5. Completion Promise (auto-set)

```javascript
if (!completion_promise) {
  completion_promise = "COMPLETE";
}
// Output ONLY when ALL tasks done
```

## 6. Collect Metadata

```bash
CLI_VERSION=$(node -e "console.log(require('package.json').version)" 2>/dev/null || echo "unknown")
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
PROJECT_PATH=$(pwd)
```

```javascript
{metadata} = {
  cli_version: CLI_VERSION,
  branch: CURRENT_BRANCH,
  project_path: PROJECT_PATH,
  mcp_servers: { smartstack: {mcp_smartstack}, context7: {mcp_context7} }
}
```

## 7. Show Summary

```
RALPH LOOP INITIALIZED
Task: {task_description} | Max: {max_iterations} | Promise: {completion_promise}
MCP: Ready | Branch: {branch} | Schema: v3.0.0
{modules_queue ? `Modules: ${totalModules} (${module_codes.join(' → ')})` : 'Mode: single module'}
-> Loading tasks...
```

**Proceed directly to step-01-task.md**
