---
# Stage identity per spec Section 6.1
stage: 2
stage_id: "02"
title: "Setup"
workflow: implement
estimated_tokens: 2000

# Context specification
context:
  static:
    persona: "careful senior engineer writing production-quality code"
    purpose: "Run setup scripts and check checklist status"
  dynamic:
    - key: feature_dir
      source: cli_arg
    - key: notes
      source: cli_arg

# Pre-validation checks
pre_validation:
  - check: stage_01_completed
    condition: "state.implement.current_stage == '01-initialization'"
    message: "Stage 01 (Initialization) must be completed first"
    recovery: "civyk-hatch implement --stage=1"

# Execution flow with step types
execution_flow:
  - step: 1
    type: conditional
    name: check_notes
    instruction: "Check if notes were provided via CLI"
    condition: "notes is not empty and notes != '$NONE' and notes != '$SKIP'"
  - step: 2
    type: input_required
    id: collect_notes
    prompt: "Implementation notes? (execution order, scope, testing)\nExamples: 'database first', 'P1 only', 'write tests first'\nType 'none' for standard implementation."
    blocking: true
    condition: "notes == '$NONE'"
  - step: 3
    type: action
    name: run_setup
    instruction: "Execute civyk-hatch check --json --require-tasks --include-tasks and parse output"
    output_variable: setup_result
  - step: 4
    type: action
    name: check_checklists
    instruction: "Scan checklists directory and verify completion status"
    output_variable: checklist_status
  - step: 5
    type: conditional
    name: confirm_incomplete
    instruction: "If checklists incomplete, ask user confirmation before proceeding"
    condition: "any checklist is incomplete"

# Guardrails
guardrails:
  enforcement: active
  allowed_tools:
    - civyk-repoix
    - Read
    - Bash
    - Glob
  forbidden_actions:
    - "Delete production source files"
    - "Modify source code"
  read_only: true

# Post-validation checks
post_validation:
  - check: setup_executed
    description: "Setup script was executed successfully"
    severity: error
  - check: checklists_reviewed
    description: "Checklists were reviewed"
    severity: warning

# Output specification
output:
  state_update:
    implement.current_stage: "02-setup"
  completion_command: "civyk-hatch update-stage implement 02 --status completed"

# Next stage
next:
  stage: 3
  stage_id: "03"
  command: "civyk-hatch implement --stage=3"
  is_terminal: false
version: 1.0.0
---
# Stage 2: Setup

You are a {persona}.

Run setup scripts and check checklist status.

---

## Step 1: Collect Notes

**Arguments provided:**

```text
NOTES: {notes:$NONE}
```

**IF notes show "$SKIP"**: User explicitly chose no notes. Execute the task plan using standard best practices.

**IF notes show "$NONE"**: Prompt user:

**[STOP: USER_INPUT_REQUIRED]**

```text
Implementation notes? (execution order, scope, testing)
Examples: "database first", "P1 only", "write tests first"
Type "none" for standard implementation.
```

Wait for response before proceeding.

**IF notes have actual value** (not "$NONE" or "$SKIP"): Use them directly and skip prompting.

---

## Step 2: Run Setup Script

Execute (cross-platform):

```bash
civyk-hatch check --json --require-tasks --include-tasks
```

Parse: `FEATURE_DIR`, `AVAILABLE_DOCS`

---

## Step 3: Check Checklists

Scan `{{feature_dir}}/checklists/`:

```text

| Checklist | Total | Complete | Incomplete | Status |
|-----------|-------|----------|------------|--------|
| ux.md     | 12    | 12       | 0          | [ok] PASS |
| test.md   | 8     | 5        | 3          | [x] FAIL |
```

**If any incomplete:**

- Display table
- **[STOP: USER_CONFIRMATION_REQUIRED]** Ask: "Some checklists incomplete. Proceed anyway? (yes/no)"
- MUST wait for user response before proceeding

**If all complete:**

- Display table
- Proceed automatically

---

## Output

```text

[ok] Setup complete
  - Feature: {{feature_dir}}
  - Checklists: [PASS / user approved]
```

---

## NEXT

```bash
civyk-hatch implement --stage=3 --feature-dir="{FEATURE_DIR}"
```

**Note:** State is persisted to `{{feature_dir}}/.state/` throughout the workflow.
