---

# Stage identity
stage: 1
stage_id: "01"
title: "Gate & Analyze"
workflow: rapid
estimated_tokens: 1500

# Context specification
context:
  static:
    persona: "senior engineer performing rapid triage and codebase orientation"
    purpose: "Validate this is an existing repo, load project context, assess complexity, create working directory"
  dynamic:
    - key: intent
      source: cli_arg
    - key: user_args
      source: cli_arg
      default: ""
    - key: tmp_dir
      source: cli_arg
    - key: repoix_mode
      source: tool_state

# Pre-validation checks
pre_validation: []

# Execution flow
execution_flow:
  - step: 1
    type: conditional
    name: check_mode
    instruction: "Determine if direct mode (intent provided) or interactive mode (no intent)"
    condition: "intent is not empty"
  - step: 2
    type: input_required
    id: intent_collection
    instruction: "Ask user what they want to implement if no intent provided"
    prompt: "What do you want to implement? Describe the change (bug fix, small feature, refactor)"
    skip_condition: "direct mode detected (intent provided)"
  - step: 3
    type: action
    name: validate_repo
    instruction: "Verify this is an existing repository with source code using repoix files"
  - step: 4
    type: conditional
    name: reject_empty
    instruction: "If repo is empty/new, REJECT and advise full feature workflow"
  - step: 5
    type: action
    name: load_project_context
    instruction: "Load AGENTS.md, constitution, tech stack guidelines"
  - step: 6
    type: action
    name: quick_orientation
    instruction: "Quick codebase orientation via repoix discovery tools"
  - step: 7
    type: action
    name: assess_complexity
    instruction: "Estimate files affected, LOC, and independent concerns"
  - step: 8
    type: conditional
    name: complexity_warning
    instruction: "If complex, WARN user but do NOT block"
  - step: 9
    type: action
    name: create_tmp_dir
    instruction: "Create tmp/{short-name}/ working directory"

# Guardrails
guardrails:
  enforcement: active
  allowed_tools:
    - civyk-repoix
    - Read
    - Grep
    - Glob
    - Bash
  forbidden_actions:
    - "Modify any source files"
    - "Delete any files"
    - "Call civyk_hatch_update_stage (rapid workflow is stateless — no state tracking)"
  read_only: true

# Post-validation checks
post_validation:
  - check: repo_validated
    description: "Repository validated as existing codebase with source code"
    severity: error
  - check: context_loaded
    description: "Project context loaded (AGENTS.md, constitution, guidelines)"
    severity: info
  - check: complexity_assessed
    description: "Complexity assessment completed with file/LOC estimates"
    severity: error
  - check: tmp_dir_created
    description: "Working directory created in tmp/"
    severity: error

# Next stage
next:
  stage: 2
  stage_id: "02-spec-clarify"
  command: "civyk-hatch rapid --stage=2 --intent=\"{INTENT}\" --tmp-dir=\"{TMP_DIR}\""
  auto_continue: true
version: 1.0.0
---
# Stage 1: Gate & Analyze

You are a {persona}.

{{include:strict-execution-mode-lite.md}}

---

## Direct Mode (Intent Provided)

If `{{intent}}` is not empty:

1. Use the provided intent — NO user prompts needed
2. Jump directly to **Step 1: Repository Validation**

---

## Interactive Mode (No Intent)

Only if `{{intent}}` is empty:

**[STOP: USER_INPUT_REQUIRED]**

What do you want to implement? Describe the change:
- Bug fix (e.g., "fix null check in user login")
- Small feature (e.g., "add retry logic to API client")
- Refactor (e.g., "extract auth middleware into shared module")

Once the user provides their intent, use it for all downstream steps.

---

## IMMEDIATELY: Create Progress Tracker

**Before ANY other work**, create a TodoWrite with all 5 rapid workflow stages:

```text
TodoWrite:
- Stage 1: Gate & Analyze [in_progress]
- Stage 2: Spec & Clarify [pending]
- Stage 3: Plan & Tasks [pending]
- Stage 4: Implement & Test [pending]
- Stage 5: Review & Summary [pending]
```

This anchors your progress tracking for the entire workflow.

---

## Step 1: Repository Validation

{{include:ai-cache-enforcement.md}}

Verify this is an existing repo with source code:

```text
# IF REPOIX_MODE != "none":
files(include="**/*", limit=10)

# IF REPOIX_MODE == "none":
# Use Glob to check for source files instead
# Glob(pattern="**/*.{py,js,ts,java,cs,go,rb,rs,cpp,c,h}")
```

**If repo is empty or has no source code:**

- REJECT the workflow
- Display: "This repo has no source code. Use the full feature workflow: `civyk-hatch specify --feature='...'`"
- TERMINATE — do not proceed to any further steps

## Step 2: Load Project Context

Check and load (if present):

1. **AGENTS.md** in repository root — read and internalize all directives
2. **Constitution**: **If** `memory/constitution.md` exists, read it and extract key principles. Priority: Constitution > Profile Override > Base > Defaults
3. **Tech stack guidelines**: `memory/guidelines/{profile}/*.md` — load coding standards

Store key constraints for downstream stages.

## Step 3: Quick Codebase Orientation

{{include:rapid-repoix-discovery.md}}

Use discovery tools for a quick orientation:

```text
# IF REPOIX_MODE != "none":
architecture(action="components")
context(action="task", task="understand codebase for: {{intent}}", token_budget=1500)

# IF REPOIX_MODE == "none":
# Use Read, Grep, Glob for manual orientation:
# - Glob(pattern="src/**/*") or Glob(pattern="**/*.{py,js,ts}")
# - Read key files (README, package.json, pyproject.toml, etc.)
# - Grep for main entry points and core patterns
```

## Step 4: Complexity Assessment

Based on discovery results, estimate:

- **Files likely affected** (target: 1-5)
- **LOC estimate** (target: <300)
- **Independent concerns** (target: 1)

**If exceeds thresholds** (>5 files OR >300 LOC OR >1 concern):

- WARN user: "This change appears complex (N files, ~M LOC). Consider using the full feature workflow: `civyk-hatch specify --feature='...'`"
- Do NOT block — user decides whether to proceed

## Step 5: Create Working Directory

Create `tmp/{short-name}/` where short-name is derived from intent (slugified, lowercase, max 40 chars, hyphens for spaces).

```bash
mkdir -p tmp/{short-name}
```

## Output

Report:

- Repository validation: PASS/REJECT
- Project context loaded: AGENTS.md (yes/no), Constitution (yes/no), Guidelines (yes/no)
- Complexity assessment: N files, ~M LOC, K concerns
- Working directory: `tmp/{short-name}/`
