kind: WavePipeline
metadata:
  name: changelog
  description: "Generate structured changelog from git history"
  release: true

input:
  source: cli
  example: "generate changelog from v0.1.0 to HEAD"

steps:
  - id: analyze-commits
    persona: navigator
    workspace:
      type: worktree
      branch: "{{ pipeline_id }}"
    exec:
      type: prompt
      source: |
        Analyze git history for changelog generation: {{ input }}

        ## Process

        1. **Determine range**: Parse input to identify the commit range.
           If tags mentioned, use them. If time period, calculate dates.
           If unclear, use last tag to HEAD (or last 50 commits).

        2. **Extract commits**: Use `git log --format` to get hash, author,
           date, subject, and body for each commit.

        3. **Parse conventional commits**: Categorize by prefix:
           feat → Features, fix → Fixes, docs → Documentation,
           refactor → Refactoring, test → Testing, chore → Maintenance,
           perf → Performance, ci → CI/CD, no prefix → Other

        4. **Identify breaking changes**: Look for `BREAKING CHANGE:` in body,
           `!` after prefix, API removals in body.

        5. **Extract scope**: Parse from prefix (e.g., `fix(pipeline):` → "pipeline")
    output_artifacts:
      - name: commits
        path: .wave/output/commit-analysis.json
        type: json
    handover:
      contract:
        type: json_schema
        source: .wave/output/commit-analysis.json
        schema_path: .wave/contracts/commit-analysis.schema.json
        on_failure: retry
        max_retries: 2

  - id: categorize
    persona: planner
    dependencies: [analyze-commits]
    memory:
      inject_artifacts:
        - step: analyze-commits
          artifact: commits
          as: raw_commits
    workspace:
      type: worktree
      branch: "{{ pipeline_id }}"
    exec:
      type: prompt
      source: |
        Categorize and describe changes for a changelog using the injected commit analysis.

        ## Rules

        1. **Group by type** into sections
        2. **Write user-facing descriptions**: Rewrite technical messages into
           clear descriptions focused on what changed and why it matters.
        3. **Highlight breaking changes** first with migration notes
        4. **Deduplicate**: Combine commits for the same logical change
        5. **Add context** for significant features
    output_artifacts:
      - name: categorized
        path: .wave/output/categorized-changes.json
        type: json
    handover:
      contract:
        type: json_schema
        source: .wave/output/categorized-changes.json
        schema_path: .wave/contracts/categorized-changes.schema.json
        on_failure: retry
        max_retries: 2

  - id: format
    persona: philosopher
    dependencies: [categorize]
    memory:
      inject_artifacts:
        - step: analyze-commits
          artifact: commits
          as: raw_commits
        - step: categorize
          artifact: categorized
          as: changes
    workspace:
      type: worktree
      branch: "{{ pipeline_id }}"
    exec:
      type: prompt
      source: |
        Format the injected commit analysis and categorized changes into a polished changelog.

        Use Keep a Changelog format:

        # Changelog

        ## [Version or Date Range] - YYYY-MM-DD

        ### Breaking Changes
        - **scope**: Description. Migration: what to do

        ### Added
        - **scope**: Feature description

        ### Fixed
        - **scope**: Bug fix description

        ### Changed
        - **scope**: Change description

        ### Security
        - **scope**: Security fix description

        Rules:
        - Only include sections with entries
        - Bold scope if present
        - Most notable entries first per section
        - One line per entry, concise
        - Contributors list at bottom
    output_artifacts:
      - name: changelog
        path: .wave/output/CHANGELOG.md
        type: markdown
    handover:
      contract:
        type: non_empty_file
        source: .wave/output/CHANGELOG.md
