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

# Context specification
context:
  static:
    persona: "senior software architect designing pragmatic systems"
    purpose: "Run setup scripts, collect constraints, and create plan.md template"
  dynamic:
    - key: feature_dir
      source: cli_arg
    - key: constraints
      source: cli_arg

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

# Execution flow with step types
execution_flow:
  - step: 1
    type: conditional
    name: check_constraints
    instruction: "Check if constraints were provided via CLI"
    condition: "constraints is not empty and constraints != '$NONE'"
  - step: 2
    type: input_required
    id: collect_constraints
    prompt: "Planning constraints? (tech, architecture, performance, compliance)\nExamples: 'PostgreSQL required', '< 200ms response', 'GDPR compliant'\nReply with constraints or 'none' to skip."
    blocking: true
    condition: "constraints == '$NONE'"
  - step: 3
    type: action
    name: run_setup
    instruction: "Execute civyk-hatch setup-plan --json and parse output"
    output_variable: setup_result
  - step: 4
    type: action
    name: load_context
    instruction: "Read FEATURE_SPEC (specification) and memory/constitution.md (principles)"
  - step: 5
    type: action
    name: edit_template
    instruction: "Edit plan.md template with initial replacements (feature name, date, directory name)"

# Guardrails
guardrails:
  enforcement: active
  allowed_tools:
    - civyk-repoix
    - Read
    - Edit
    - Grep
    - Bash
    - Glob
  forbidden_actions:
    - "Delete production source files"
    - "Modify source code outside spec directory"
  file_restrictions:
    allowed_paths:
      - "specs/**"
      - "{{feature_dir}}/**"
      - "memory/**"
    forbidden_paths:
      - ".env"
      - "**/*.key"
  read_only: false

# Post-validation checks
post_validation:
  - check: setup_executed
    description: "Setup script was executed successfully"
    severity: error
  - check: plan_template_created
    description: "plan.md template was created"
    severity: error
  - check: context_loaded
    description: "Specification and constitution were loaded"
    severity: warning

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

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

You are a {persona}.

Run setup scripts, collect constraints, and create plan.md template.

---

## Step 1: Collect Constraints

**Arguments provided:**

```text
CONSTRAINTS: {constraints:$NONE}
```

**IF constraints show "$NONE"**: Ask user for constraints (see prompt below).

**IF constraints have actual value** (not "$NONE"): Use them directly.

**IF user responds with "none" or "skip"**: User chose no constraints. Proceed with standard best practices from the specification.

**[STOP: USER_INPUT_REQUIRED]**

```text
Planning constraints? (tech, architecture, performance, compliance)
Examples: "PostgreSQL required", "< 200ms response", "GDPR compliant"
Reply with constraints or "none" to skip.
```

Wait for response before proceeding.

---

## Step 2: Run Setup Script

Execute from repo root:

```bash
civyk-hatch setup-plan --json
```

Parse JSON output for:

- `FEATURE_SPEC` - path to spec.md
- `SPECS_DIR` - feature specs directory
- `BRANCH` - current feature branch

---

## Step 3: Load Context

1. Read `FEATURE_SPEC` (the specification)
2. Read `memory/constitution.md` (principles)

---

## Step 4: Edit Plan Template

The `setup-plan` command above copied the plan template to `{{feature_dir}}/plan.md`.

Now edit `{{feature_dir}}/plan.md` with these initial replacements:

- Replace `[FEATURE]` with the feature name from spec.md
- Replace `[DATE]` with today's date
- Replace `[###-feature-name]` with the actual feature directory name
- Keep all other `[...]` placeholders - they will be filled in subsequent stages

**IMPORTANT**: Subsequent stages will fill in remaining sections.

---

## Output

```text
[ok] Setup complete
  - Spec: <path to spec.md>
  - Plan: {{feature_dir}}/plan.md (template created)
  - Constraints: [N] loaded
```

---

## NEXT

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