# Category Ordering & Dependency Rules

> **Loaded by:** step-02 (task ordering validation) and compact-loop.md (batch grouping)
> **Purpose:** Defines execution order, dependencies, and batch grouping rules.
> **Note:** All execution rules, MCP tool usage, conventions, and POST-CHECKs are in `/apex` references.

---

## Execution Order

Tasks MUST be executed in this order. Each category depends on the previous ones.

| Order | Category | Depends On | Description |
|-------|----------|------------|-------------|
| 0 | `domain` | — | Entities, enums, value objects |
| 1 | `infrastructure` | domain | EF configs, migrations, seed data |
| 2 | `application` | infrastructure | Services, DTOs, validators, DI |
| 3 | `api` | application | Controllers, route attributes |
| 4 | `seedData` | api | Navigation, permissions, roles, dev data |
| 5 | `frontend` | seedData | Pages, routes, i18n, components |
| 6 | `test` | frontend | Unit tests, integration tests, coverage |

> **Key dependency:** `frontend` depends on `seedData` (not just `api`).
> Navigation seed data drives the menu — without it, frontend routes have no menu entries.

---

## Dependency Rules

1. **A task can only start when ALL its dependencies are `completed`**
2. **If a dependency is `failed` or `blocked`, the task becomes `blocked`**
3. **Cross-category dependencies are implicit** — domain before infrastructure, etc.
4. **Same-category tasks MAY run in parallel** (batched together)
5. **PRD `dependencies[]` array overrides implicit ordering** — explicit deps always checked first

---

## Batch Grouping Rules

When selecting tasks for a batch:

1. **Group by category** — take the first eligible category with pending tasks
2. **Max batch size: 5 tasks** — prevents overloading a single apex invocation
3. **Same category only** — never mix domain + infrastructure in one batch
4. **Order within batch: by task ID** (ascending) — preserves PRD creation order

```javascript
const eligible = prd.tasks.filter(/* pending + deps met */);
const firstCategory = eligible[0].category;
const batch = eligible.filter(t => t.category === firstCategory).slice(0, 5);
```

---

## PRD Category Completeness

A valid PRD MUST contain tasks in these categories:

- `domain` — at least 1 entity task
- `infrastructure` — at least 1 EF config + migration task
- `application` — at least 1 service task
- `api` — at least 1 controller task
- `seedData` — at least 1 navigation/permission task
- `frontend` — at least 1 page task
- `test` — at least 1 test task

If any category is missing, ralph step-01 injects a guardrail task.
