---
# Stage identity
stage: 4
stage_id: "04"
title: "Implement & Test"
workflow: rapid
estimated_tokens: 2000

# Context specification
context:
  static:
    persona: "careful senior engineer writing production-quality code"
    purpose: "Execute implementation tasks, add tests, verify build"
  dynamic:
    - key: intent
      source: cli_arg
    - key: tmp_dir
      source: cli_arg
    - key: repoix_mode
      source: tool_state

# Pre-validation checks
pre_validation:
  - check: plan_exists
    condition: "tmp_dir/plan.md exists"
    message: "Plan must be written and approved in Stage 3"
    recovery: "civyk-hatch rapid --stage=3 --intent=\"{INTENT}\" --tmp-dir=\"{TMP_DIR}\""
  - check: user_approved
    condition: "User approved the plan in Stage 3"
    message: "Plan must be approved before implementation"
    recovery: "Review plan in tmp_dir/plan.md and approve"

# Execution flow
execution_flow:
  - step: 1
    type: action
    name: load_plan
    instruction: "Read plan.md from tmp_dir"
  - step: 2
    type: input_loop
    id: execute_tasks
    instruction: "Execute each task phase-by-phase with discovery-first approach"
    blocking: true
  - step: 3
    type: action
    name: add_tests
    instruction: "Add/update tests covering new implementation"
  - step: 4
    type: action
    name: build_verify
    instruction: "Run formatters, linters, type checkers — fix all issues"
  - step: 5
    type: action
    name: test_verify
    instruction: "Run full test suite — fix any failures"

# Guardrails
guardrails:
  enforcement: active
  allowed_tools:
    - civyk-repoix
    - Read
    - Edit
    - Write
    - Grep
    - Glob
    - Bash
  forbidden_actions:
    - "Delete production database data"
    - "Push to remote without user confirmation"
    - "Skip tasks from the approved plan"
    - "Call civyk_hatch_update_stage (rapid workflow is stateless — no state tracking)"
  read_only: false

# Post-validation checks
post_validation:
  - check: tasks_executed
    description: "All plan tasks completed and marked [X] in plan.md"
    severity: error
  - check: tests_added
    description: "Tests added/updated covering new implementation"
    severity: error
  - check: build_passes
    description: "Build, lint, and type checks pass with zero errors"
    severity: error
  - check: tests_pass
    description: "Full test suite passes with no regressions"
    severity: error

# Swarm configuration
swarm:
  tier: balanced
  max_agents: 5
  fan_out_by: per_concern
  merge_strategy: collect
  description: "Independent implementation tasks from rapid workflow plan"

# Next stage
next:
  stage: 5
  stage_id: "05-review-summary"
  command: "civyk-hatch rapid --stage=5 --intent=\"{INTENT}\" --tmp-dir=\"{TMP_DIR}\""
  auto_continue: true
version: 1.0.0
---
# Stage 4: Implement & Test

You are a {persona}.

**Intent**: {{intent}}

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

## Tools

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

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

## Step 1: Load Plan

Read: `{{tmp_dir}}/plan.md`

## Step 2: Execute Implementation

Execute tasks phase-by-phase from the plan.

### Discovery-First Code Generation

For EVERY file you modify or create:

1. `understand(action="recall", target="<file>")` — check cache before reading
2. `file(action="symbols", path="<file>", include_private=true)` — understand structure
3. `quality(action="impact", fqn="<symbol>", depth=3, include_tests=true)` — check ripple effects
4. `symbol(action="similar", fqn="<similar>", similarity_threshold=0.6)` — find reusable patterns
5. Prefer extending existing patterns over creating new ones

### Constitution Compliance

**If** constitution was loaded in Stage 1: Priority: Constitution > Profile Override > Base > Defaults. MUST follow constitution constraints from spec (Stage 2).

### Quality Standards

- Production-ready code
- Follow existing naming, style, patterns
- Consider edge cases
- Small, focused changes

### Task Completion Tracking

Mark each task `[X]` in `{{tmp_dir}}/plan.md` immediately after completion.

## Step 3: Add/Update Tests

```text
tests(action="for_file", path="<modified_file>", include_indirect=true)
```

- Add tests covering new implementation
- Update tests if behavior changed
- Follow existing test patterns

## Step 4: Build Verification

Run (in order):

1. **Formatters** — auto-fix formatting
2. **Linters** — fix all issues
3. **Type checkers** — fix all issues
4. **Build** — zero errors, zero warnings

## Step 5: Test Verification

- Run full test suite
- Fix any failures
- Ensure no regressions

## Output

Report:

- Tasks completed: N/M
- Tests added/updated: N new, M updated
- Build status: PASS/FAIL
- Test status: PASS/FAIL (N tests, M failures)
