---
name: step-04-check
description: Check completion criteria and decide next action
next_step: steps/step-05-report.md OR steps/step-01-task.md
---

# Step 4: Check Completion

## YOUR TASK:

Check if all tasks are complete and decide whether to output completion promise or continue to next task.

---

## EXECUTION SEQUENCE:

### 1. Read Current State

**Load prd.json:**

```javascript
const prd = readJSON('.ralph/prd.json');
const tasksCompleted = prd.tasks.filter(t => t.passes).length;
const tasksTotal = prd.tasks.length;
const allComplete = tasksCompleted === tasksTotal;
```

### 2. Check Iteration Limit

**If {current_iteration} >= {max_iterations}:**

```
╔══════════════════════════════════════════════════════════════════╗
║  ⚠️ MAX ITERATIONS REACHED                                        ║
╠══════════════════════════════════════════════════════════════════╣
║  Iterations: {current_iteration} / {max_iterations}               ║
║  Tasks: {tasksCompleted} / {tasksTotal} complete                  ║
╠══════════════════════════════════════════════════════════════════╣
║  Ralph loop stopped due to iteration limit.                       ║
║  Remaining tasks:                                                  ║
║  {list of tasks with passes: false}                               ║
╚══════════════════════════════════════════════════════════════════╝

-> Generating partial report...
```

**Proceed to step-05-report.md**

### 3. Check All Tasks Complete

**If allComplete = true:**

```
╔══════════════════════════════════════════════════════════════════╗
║  ✅ ALL TASKS COMPLETE                                            ║
╠══════════════════════════════════════════════════════════════════╣
║  Iterations: {current_iteration}                                  ║
║  Tasks: {tasksTotal} / {tasksTotal} complete                      ║
╠══════════════════════════════════════════════════════════════════╣
║  Output completion promise:                                        ║
║  <promise>{completion_promise}</promise>                          ║
╚══════════════════════════════════════════════════════════════════╝

<promise>{completion_promise}</promise>
```

**CRITICAL: Output the promise tag exactly as shown:**
```
<promise>{completion_promise}</promise>
```

**Proceed to step-05-report.md**

### 4. More Tasks Remaining

**If allComplete = false AND iteration < max_iterations:**

```
Progress: {tasksCompleted} / {tasksTotal} tasks

Next task:
[{next_task_id}] {next_task_description}

Continuing to next iteration...
```

**Loop back to step-01-task.md**

---

## DECISION TREE:

```
                    ┌─────────────────────┐
                    │  Check Completion   │
                    └──────────┬──────────┘
                               │
              ┌────────────────┼────────────────┐
              │                │                │
              ▼                ▼                ▼
      ┌──────────────┐ ┌──────────────┐ ┌──────────────┐
      │ Max Iters    │ │ All Tasks    │ │ Tasks        │
      │ Reached      │ │ Complete     │ │ Remaining    │
      └──────┬───────┘ └──────┬───────┘ └──────┬───────┘
             │                │                │
             ▼                ▼                ▼
      ┌──────────────┐ ┌──────────────┐ ┌──────────────┐
      │ Partial      │ │ Output       │ │ Loop to      │
      │ Report       │ │ Promise      │ │ step-01      │
      │ step-05      │ │ step-05      │ │              │
      └──────────────┘ └──────────────┘ └──────────────┘
```

---

## OUTPUT FORMAT:

**If continuing:**
```
Completion Check:

| Field | Value |
|-------|-------|
| Iteration | {current_iteration} / {max_iterations} |
| Tasks | {tasksCompleted} / {tasksTotal} |
| Status | Continuing |
| Next | [{next_task_id}] {next_task_description} |

-> Loading next task...
```

**If complete:**
```
Completion Check:

| Field | Value |
|-------|-------|
| Iteration | {current_iteration} |
| Tasks | {tasksTotal} / {tasksTotal} |
| Status | COMPLETE |

<promise>{completion_promise}</promise>

-> Generating report...
```

---

## CRITICAL RULE:

**NEVER output the completion promise unless:**
1. ALL tasks have `passes: true`
2. The statement is genuinely true

**False promises to escape the loop are FORBIDDEN.**

---

## NEXT STEP:

- If complete or max iterations: `./step-05-report.md`
- If tasks remaining: `./step-01-task.md`
