name: terraform
description: Terraform IaC 開発ワークフロー（plan → implement → 並列レビュー → 最終ゲート → 修正 → 完了）
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: 要件が明確で実装可能
        next: implement
      - condition: ユーザーが質問をしている（実装タスクではない）
        next: COMPLETE
      - condition: 要件が不明確、情報不足
        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 がエラーなし
      - terraform validate が成功
      - terraform plan が実行可能（エラーなし）
    instruction: implement-terraform
    rules:
      - condition: 実装が完了した
        next: reviewers
      - condition: 実装を進行できない
        next: ABORT
      - condition: ユーザーへの確認事項があるためユーザー入力が必要
        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: Terraform規約に準拠
          - condition: 規約違反あり
        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: AI特有の問題なし
          - condition: AI特有の問題あり
        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("Terraform規約に準拠", "AI特有の問題なし", "approved")
        next: final-supervise
      - condition: any("規約違反あり", "AI特有の問題あり", "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問題の修正完了
          - condition: 修正不要（指摘対象ファイル/仕様の確認済み）
          - condition: 判断できない、情報不足
        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: 監督者の指摘に対する修正が完了した
          - condition: 修正を進行できない
        instruction: fix-supervisor
    rules:
      - condition: all("AI問題の修正完了", "監督者の指摘に対する修正が完了した")
        next: reviewers
      - condition: any("修正不要（指摘対象ファイル/仕様の確認済み）", "判断できない、情報不足", "修正を進行できない")
        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問題の修正完了
        next: reviewers
      - condition: 修正不要（指摘対象ファイル/仕様の確認済み）
        next: implement
      - condition: 判断できない、情報不足
        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: 監督者の指摘に対する修正が完了した
        next: reviewers
      - condition: 修正を進行できない
        next: implement
    instruction: fix-supervisor
loop_monitors:
  - cycle:
      - fix_both
      - reviewers
    threshold: 3
    judge:
      persona: supervisor
      instruction: |
        レビュー → 修正のサイクルが {cycle_count} 回繰り返されました。
        Report Directory 内のレビューレポート履歴を確認し、収束状況を判断してください。

        - 修正進捗: 報告された問題の受入条件が満たされたか
        - 報告の収束: 正しい構造・契約上の問題がレビュー回ごとに追加され続けていないか
        - 異なる問題であること自体は進捗の根拠にせず、件数は補助情報に留める
      rules:
        - condition: 健全（修正進捗があり、報告内容も収束している）
          next: fix_both
        - condition: 修正未完了または報告未収束だが、次のレビュー・修正が実行可能である
          next: fix_both
        - condition: 非生産的（振動または要件矛盾）
          next: ABORT
  - cycle:
      - ai-antipattern-fix
      - reviewers
    threshold: 3
    judge:
      persona: supervisor
      instruction: |
        AI修正 → レビューのサイクルが {cycle_count} 回繰り返されました。
        Report Directory 内のレビューレポート履歴を確認し、収束状況を判断してください。

        - 修正進捗: 報告された問題の受入条件が満たされたか
        - 報告の収束: 正しい構造・契約上の問題がレビュー回ごとに追加され続けていないか
        - 異なる問題であること自体は進捗の根拠にせず、件数は補助情報に留める
      rules:
        - condition: 健全（修正進捗があり、報告内容も収束している）
          next: ai-antipattern-fix
        - condition: 修正未完了または報告未収束だが、次のレビュー・修正が実行可能である
          next: ai-antipattern-fix
        - condition: 非生産的（振動または要件矛盾）
          next: ABORT
  - cycle:
      - supervise_fix
      - reviewers
    threshold: 3
    judge:
      persona: supervisor
      instruction: |
        監督修正 → レビューのサイクルが {cycle_count} 回繰り返されました。
        Report Directory 内のレビューレポート履歴を確認し、収束状況を判断してください。

        - 修正進捗: 報告された問題の受入条件が満たされたか
        - 報告の収束: 正しい構造・契約上の問題がレビュー回ごとに追加され続けていないか
        - 異なる問題であること自体は進捗の根拠にせず、件数は補助情報に留める
      rules:
        - condition: 健全（修正進捗があり、報告内容も収束している）
          next: supervise_fix
        - condition: 修正未完了または報告未収束だが、次のレビュー・修正が実行可能である
          next: supervise_fix
        - condition: 非生産的（振動または要件矛盾）
          next: ABORT
