---
name: step-02-execute
description: Delegate module execution to /apex
next_step: steps/step-03-commit.md
---

# Step 2: Delegate to /apex

> **CONTEXT OPTIMIZATION:** Read ONCE (first iteration). Subsequent iterations use compact-loop.md.

## YOUR TASK:

Delegate the current module's tasks to `/apex -d` for code generation. Ralph handles orchestration only.

**ULTRA THINK about the delegation context.**

---

## EXECUTION RULES:

1. **VERIFY DEPENDENCIES** — All task dependencies must be completed
2. **DELEGATE TO /apex** — All code generation via `/apex -d`
3. **TRACK RESULTS** — Verify PRD state after apex returns
4. **NEVER GENERATE CODE** — Ralph orchestrates, apex generates

---

## EXECUTION SEQUENCE:

### 1. Verify Dependencies

```javascript
const prd = readJSON('.ralph/prd.json');
const task = prd.tasks.find(t => t.id === {current_task_id});
for (const depId of task.dependencies) {
  const dep = prd.tasks.find(t => t.id === depId);
  if (!dep || dep.status !== 'completed') {
    task.status = 'blocked'; task.error = `Dependency ${depId} not completed`;
    writeJSON('.ralph/prd.json', prd);
    STOP — return to step-01
  }
}
```

### 2. Mark In-Progress

```javascript
task.status = 'in_progress';
task.started_at = new Date().toISOString();
writeJSON('.ralph/prd.json', prd);
```

### 3. Delegate to /apex

**INVOKE `/apex -d .ralph/prd.json`**

This delegates ALL remaining tasks for the current module to apex:

- Apex reads the PRD, extracts context (`context_code`, `app_name`, `module_code`, `entities`, `sections`)
- Apex executes ALL layers: domain → infrastructure → migration → application → api → seed data → frontend → tests
- Apex runs full POST-CHECKs (43 checks from `references/post-checks.md`)
- Apex commits per layer (atomic commits)
- Apex validates with MCP (`validate_conventions`)
- Apex updates task statuses in the PRD file directly

> **FLAGS:** `-d` implies `-a` (auto, no user confirmation) and `-e` (economy, no nested teams).
> Ralph manages parallelism via team orchestration, not apex.

### 4. Verify Post-Apex Results

```javascript
// Re-read PRD after apex execution
const updatedPrd = readJSON('.ralph/prd.json');
const moduleTasks = updatedPrd.tasks.filter(t => t.module === {current_module});

const completed = moduleTasks.filter(t => t.status === 'completed').length;
const failed = moduleTasks.filter(t => t.status === 'failed').length;
const pending = moduleTasks.filter(t => t.status === 'pending').length;

if (failed > 0) {
  console.log(`WARNING: ${failed} tasks failed during apex execution`);
  // Failed tasks will be retried in compact loop
}

if (pending > 0) {
  console.log(`INFO: ${pending} tasks still pending — will be handled in compact loop`);
}

console.log(`Apex completed: ${completed}/${moduleTasks.length} tasks`);
```

### 5. Update Progress

```javascript
// Append to progress file
const progressEntry = `
[Iteration ${prd.config.current_iteration}] Delegated to /apex
  Module: ${prd.project.module}
  Completed: ${completed}/${moduleTasks.length}
  Failed: ${failed}
  Pending: ${pending}
`;
appendFile('.ralph/progress.txt', progressEntry);
```

---

## CONSTRAINTS:

- **DO:** Delegate to apex, verify results, maintain PRD state
- **DON'T:** Generate code directly, run inline POST-CHECKs, invoke MCP tools for generation
- **DON'T:** Stop to ask the user anything — fully autonomous

---

## NEXT STEP:

Proceed to `./step-03-commit.md`
