name: terraform
description: Terraform IaC development workflow (plan → implement → parallel review → final gate → fix → complete)
all_steps:
  rules:
    - ref: findings-handling
      position: before_instruction
    - ref: plain-terminology
      position: before_instruction

max_steps: 15
initial_step: plan
steps:
  - name: plan
    capabilities: readonly
    tags:
      - plan
    edit: false
    persona: planner
    policy:
      - contract-change
      - terraform
    knowledge: terraform-aws
    rules:
      - condition: Requirements are clear and implementable
        next: implement
      - condition: User is asking a question (not an implementation task)
        next: COMPLETE
      - condition: Requirements are unclear or insufficient
        next: ABORT
    instruction: plan
    output_contracts:
      report:
        - name: plan.md
          format: plan
  - name: implement
    capabilities: edit
    tags:
      - coding
    edit: true
    persona: terraform-coder
    policy:
      - contract-change
      - terraform
    knowledge: terraform-aws
    quality_gates:
      - terraform fmt -check passes without errors
      - terraform validate succeeds
      - terraform plan is executable (no errors)
    instruction: implement-terraform
    rules:
      - condition: Implementation is complete
        next: reviewers
      - condition: Cannot proceed with implementation
        next: ABORT
      - condition: User input needed for confirmation
        next: implement
        requires_user_input: true
        interactive_only: true
    output_contracts:
      report:
        - name: coder-scope.md
          format: coder-scope
        - name: coder-decisions.md
          format: coder-decisions
  - name: reviewers
    tags:
      - review
    parallel:
      - name: tf_review
        capabilities: readonly
        tags:
          - review
        edit: false
        persona: terraform-reviewer
        policy:
          - contract-change
          - review
          - terraform
        knowledge: terraform-aws
        instruction: review-terraform
        rules:
          - condition: Complies with Terraform conventions
          - condition: Convention violations found
        output_contracts:
          report:
            - name: terraform-review.md
              format: terraform-review
      - name: ai-antipattern-review-2nd
        capabilities: readonly
        tags:
          - review
        edit: false
        persona: ai-antipattern-reviewer
        policy:
          - contract-change
          - review
          - ai-antipattern
        instruction: ai-antipattern-review
        rules:
          - condition: No AI-specific issues
          - condition: AI-specific issues found
        output_contracts:
          report:
            - name: ai-antipattern-review.md
              format: ai-antipattern-review
      - name: coding-review
        capabilities: readonly
        tags:
          - review
        edit: false
        persona: coding-reviewer
        policy:
          - contract-change
          - review
          - coding
        instruction: review-coding
        output_contracts:
          report:
            - name: coding-review.md
              format: coding-review
        rules:
          - condition: approved
          - condition: needs_fix
    rules:
      - condition: any("error")
        next: reviewers
      - condition: all("Complies with Terraform conventions", "No AI-specific issues", "approved")
        next: final-supervise
      - condition: any("Convention violations found", "AI-specific issues found", "needs_fix")
        next: supervise
  - name: final-supervise
    capabilities: readonly
    tags:
      - review
    edit: false
    persona: supervisor
    policy:
      - contract-change
      - terraform
    knowledge: terraform-aws
    instruction: supervise
    rules:
      - condition: BLOCKED
        next: ABORT
      - condition: APPROVE
        next: COMPLETE
      - condition: REJECT
        next: supervise_fix
    output_contracts:
      report:
        - name: supervisor-validation.md
          format: supervisor-validation
        - name: summary.md
          format: supervisor-summary

  - name: supervise
    capabilities: readonly
    tags:
      - review
      - supervise
    edit: &terraform_supervise_edit false
    persona: &terraform_supervise_persona supervisor
    policy: &terraform_supervise_policy
      - contract-change
      - review
      - terraform
    knowledge: &terraform_supervise_knowledge terraform-aws
    instruction: &terraform_supervise_instruction supervise
    rules:
      - condition: BLOCKED
        next: ABORT
      - condition: APPROVE
        next: COMPLETE
      - condition: REJECT
        next: supervise_fix
    output_contracts: &terraform_supervise_output_contracts
      report:
        - name: supervisor-validation.md
          format: supervisor-validation
        - name: summary.md
          format: supervisor-summary
  - name: fix_both
    tags:
      - coding
    parallel:
      - name: ai-antipattern-fix-parallel
        capabilities: edit
        tags:
          - coding
        edit: true
        persona: terraform-coder
        policy:
          - contract-change
          - terraform
          - testing
        knowledge: terraform-aws
        rules:
          - condition: AI issue fix complete
          - condition: No fix needed (target file/spec verified)
          - condition: Cannot determine, insufficient info
        instruction: ai-antipattern-fix
        output_contracts:
          report:
            - name: ai-antipattern-fix-report.md
              format: fix-report
      - name: supervise_fix_parallel
        capabilities: edit
        tags:
          - coding
        edit: true
        persona: terraform-coder
        policy:
          - contract-change
          - terraform
          - testing
        knowledge: terraform-aws
        rules:
          - condition: Supervisor's findings have been fixed
          - condition: Cannot proceed with fix
        instruction: fix-supervisor
    rules:
      - condition: all("AI issue fix complete", "Supervisor's findings have been fixed")
        next: reviewers
      - condition: any("No fix needed (target file/spec verified)", "Cannot determine, insufficient info", "Cannot proceed with fix")
        next: implement
  - name: ai-antipattern-fix
    capabilities: edit
    tags:
      - coding
    edit: true
    persona: terraform-coder
    policy:
      - contract-change
      - terraform
      - testing
    knowledge: terraform-aws
    pass_previous_response: false
    rules:
      - condition: AI issue fix complete
        next: reviewers
      - condition: No fix needed (target file/spec verified)
        next: implement
      - condition: Cannot determine, insufficient info
        next: implement
    instruction: ai-antipattern-fix
    output_contracts:
      report:
        - name: ai-antipattern-fix-report.md
          format: fix-report
  - name: supervise_fix
    capabilities: edit
    tags:
      - coding
    edit: true
    persona: terraform-coder
    policy:
      - contract-change
      - terraform
      - testing
    knowledge: terraform-aws
    pass_previous_response: false
    rules:
      - condition: Supervisor's findings have been fixed
        next: reviewers
      - condition: Cannot proceed with fix
        next: implement
    instruction: fix-supervisor
loop_monitors:
  - cycle:
      - fix_both
      - reviewers
    threshold: 3
    judge:
      persona: supervisor
      instruction: |
        The review → fix cycle has repeated {cycle_count} times.
        Check the review report history in the Report Directory and assess convergence.

        - Fix progress: whether reported problems satisfy their acceptance criteria
        - Report convergence: whether valid structural or contract problems keep being added across review rounds
        - A different problem is not progress by itself; treat counts as supporting information
      rules:
        - condition: Healthy (fixes progressing and report content converging)
          next: fix_both
        - condition: Implementation incomplete or report not converged, but the next review or fix can be performed
          next: fix_both
        - condition: Unproductive (oscillation or requirement conflict)
          next: ABORT
  - cycle:
      - ai-antipattern-fix
      - reviewers
    threshold: 3
    judge:
      persona: supervisor
      instruction: |
        The AI fix → review cycle has repeated {cycle_count} times.
        Check the review report history in the Report Directory and assess convergence.

        - Fix progress: whether reported problems satisfy their acceptance criteria
        - Report convergence: whether valid structural or contract problems keep being added across review rounds
        - A different problem is not progress by itself; treat counts as supporting information
      rules:
        - condition: Healthy (fixes progressing and report content converging)
          next: ai-antipattern-fix
        - condition: Implementation incomplete or report not converged, but the next review or fix can be performed
          next: ai-antipattern-fix
        - condition: Unproductive (oscillation or requirement conflict)
          next: ABORT
  - cycle:
      - supervise_fix
      - reviewers
    threshold: 3
    judge:
      persona: supervisor
      instruction: |
        The supervisor fix → review cycle has repeated {cycle_count} times.
        Check the review report history in the Report Directory and assess convergence.

        - Fix progress: whether reported problems satisfy their acceptance criteria
        - Report convergence: whether valid structural or contract problems keep being added across review rounds
        - A different problem is not progress by itself; treat counts as supporting information
      rules:
        - condition: Healthy (fixes progressing and report content converging)
          next: supervise_fix
        - condition: Implementation incomplete or report not converged, but the next review or fix can be performed
          next: supervise_fix
        - condition: Unproductive (oscillation or requirement conflict)
          next: ABORT
