---
name: step-00-init
description: Parse flags, detect SmartStack context, verify MCP availability
model: sonnet
next_step: steps/step-01-analyze.md
---

# Step 0: Initialize

## LOAD SHARED

Read `_shared.md` for SmartStack detection patterns and MCP tools reference.

---

## 1. Parse Flags

Extract flags from the raw input:

```
Input: "{raw_input}"

Flags to detect: -a, -x, -s, -t, -e, -r, -pr
Remaining text after flag removal = {task_description}
```

**Defaults:**

```yaml
auto_mode: false
examine_mode: false
save_mode: false
test_mode: false
economy_mode: false
pr_mode: false
resume_mode: false
```

---

## 2. Detect SmartStack Context

Scan the project to identify the SmartStack hierarchy:

```
1. Glob("*.sln") → confirm .NET project
2. Glob("docs/business/**/*") → list existing BA artifacts
3. Glob(".ralph/prd-*.json") → list existing PRDs
4. Glob("src/pages/**/*.tsx") → confirm frontend exists
```

**From task description, infer:**
- `{context_code}`: business (default), platform, personal
- `{app_name}`: application name
- `{module_code}`: target module
- `{sections}`: sections being added/modified

**From existing artifacts:**
- `{prd_path}`: `.ralph/prd-{module_code}.json` if exists
- `{feature_path}`: `docs/business/{app}/{module}/business-analyse/*/feature.json` if exists

**If hierarchy cannot be inferred, ask the user:**

```yaml
questions:
  - header: "Context"
    question: "What is the SmartStack context for this work?"
    options:
      - label: "Business (Recommended)"
        description: "Business application module"
      - label: "Platform"
        description: "Platform administration or support"
      - label: "Personal"
        description: "Personal user space"
    multiSelect: false
```

---

## 3. Verify MCP Availability

```
Call: mcp__smartstack__validate_conventions (quick connectivity test)

IF success → MCP available, proceed normally
IF failure → warn user, continue in degraded mode (manual tools only)
```

---

## 4. Determine Needs

```
needs_seed_data = true IF:
  - New module or new section with navigation entries
  - Task mentions "navigation", "permissions", "roles", "seed"

needs_migration = true IF:
  - New entities or entity modifications detected
  - Task mentions "entity", "field", "table", "column", "migration"
```

---

## 5. Resume Mode (if -r)

```
IF resume_mode:
  1. Read .claude/output/apex/ → find latest task folder
  2. Read 00-context.md → restore state variables
  3. Find last completed step file
  4. Route to next step directly
  → SKIP to identified next step
```

---

## 6. Save Mode Setup (if -s)

```
IF save_mode:
  1. Generate task_id: NN-kebab-case from task_description
  2. Create .claude/output/apex/{task_id}/
  3. Write 00-context.md with:
     - Timestamp
     - All state variables (generic + SmartStack)
     - Flags
     - Detected hierarchy
```

---

## 7. Display Context Summary

```
**APEX SmartStack - Initialization Complete**

**Task:** {task_description}
**Context:** {context_code} / {app_name} / {module_code}
**Sections:** {sections}

**Sources:**
- PRD: {prd_path || "none"}
- Feature: {feature_path || "none"}

**Flags:** {active_flags}
**MCP:** {available|degraded}
**Needs migration:** {yes|no}
**Needs seed data:** {yes|no}
```

---

## NEXT STEP

Load `steps/step-01-analyze.md`
