---
# Stage identity per spec Section 6.1
stage: 3
stage_id: "03"
title: "Generate Constitution"
workflow: constitution
estimated_tokens: 2000

# Context specification
context:
  static:
    persona: "technical governance architect creating formal constitution"
    purpose: "Create the formal constitution document from collected principles"
  dynamic:
    - key: principles
      source: state

# Pre-validation checks
pre_validation:
  - check: stage_02_completed
    condition: "state.constitution.current_stage == '02-collect-principles'"
    message: "Stage 02 (Collect Principles) must be completed first"
    recovery: "civyk-hatch constitution --stage=2"
  - check: principles_exist
    condition: "principles were validated in stage 02"
    message: "Validated principles must exist from stage 02"
    recovery: "civyk-hatch constitution --stage=2"

# Execution flow with step types
execution_flow:
  - step: 1
    type: action
    name: load_template
    instruction: "Load constitution template from memory/constitution.md or use default structure"
    output_variable: template
  - step: 2
    type: action
    name: fill_template
    instruction: "Replace all placeholders with concrete values"
    output_variable: filled_content
  - step: 3
    type: action
    name: validate_constitution
    instruction: "Verify no placeholders remain, version is semantic, dates are ISO format"
    output_variable: validation_passed
  - step: 4
    type: action
    name: save_constitution
    instruction: "Write constitution to memory/constitution.md"
    output_variable: constitution_file

# Guardrails
guardrails:
  enforcement: active
  allowed_tools:
    - civyk-repoix
    - Read
    - Write
    - Grep
    - Glob
    - Bash
  forbidden_actions:
    - "Leave unexplained placeholder tokens"
    - "Use vague or unenforceable language"
  file_restrictions:
    allowed_paths:
      - "memory/constitution.md"
    forbidden_paths:
      - ".env"
      - "**/*.key"
  read_only: false

# Post-validation checks
post_validation:
  - check: no_placeholders
    description: "No unexplained placeholder tokens remain"
    severity: error
  - check: semantic_version
    description: "Version follows semantic versioning"
    severity: error
  - check: iso_dates
    description: "Dates are in ISO format (YYYY-MM-DD)"
    severity: error
  - check: testable_principles
    description: "All principles are declarative and testable"
    severity: warning

# Output specification
output:
  artifacts:
    - type: constitution
      path: "memory/constitution.md"
      description: "Project constitution document"

# Next stage
next:
  is_terminal: true
version: 1.0.0
---
{{include:ai-cache-enforcement.md}}

# Stage 3: Generate Constitution

## Purpose

Create the formal constitution document from collected principles.

---

## Step 1: Load Template

Load the constitution template from `memory/constitution.md`.

If template doesn't exist, use this structure:

```markdown
# Project Constitution

## Preamble
[PROJECT_MISSION]

## Article I: Engineering Principles
[PRINCIPLES]

## Article II: Quality Standards
[QUALITY_STANDARDS]

## Article III: Governance
[GOVERNANCE]

## Metadata
- Version: [VERSION]
- Ratified: [DATE]
- Last Amended: [DATE]
```

---

## Step 2: Fill Template

Replace all placeholders with concrete values:

- `[PROJECT_MISSION]` -> Detect project name from repository root (e.g., package.json `name`, directory name, or pyproject.toml `name`) and write a concise mission statement
- `[PRINCIPLES]` -> Formatted list of collected principles
- `[VERSION]` -> Semantic version (start at 1.0.0 for new)
- `[DATE]` -> ISO format YYYY-MM-DD

**Formatting rules:**

- Each principle: "**Name**: MUST/SHOULD/MAY description"
- Group related principles under appropriate articles
- Use normative language consistently

---

## Step 3: Validate

Before saving, verify:

- [ ] No unexplained placeholder tokens remain
- [ ] Version follows semantic versioning
- [ ] Dates in ISO format (YYYY-MM-DD)
- [ ] All principles are declarative and testable
- [ ] No vague language - each rule is enforceable

---

## Step 4: Save and Report

Write constitution to `memory/constitution.md`.

Output completion summary:

```text

[ok] Constitution created successfully

Version: 1.0.0
Location: memory/constitution.md
Principles: [N] total

Suggested commit message:
  docs: create project constitution v1.0.0

Next steps:
  - Review memory/constitution.md
```

---

## WORKFLOW COMPLETE

The constitution has been created. No further stages.
