name: merge-review
description: PRをレビューし、CIと評価の結果をコメントする
max_steps: 51
initial_step: identify
steps:
  - name: identify
    description: 対象PRの番号と現在の状態を取得する
    mode: system
    system_inputs:
      - type: pr_status
        source: current_pr
        as: pr
    rules:
      - condition: when(true)
        next: review
  - name: review
    description: 変更内容に合うレビュアーでPRをレビューし、結果をサマリーに統合する
    kind: workflow_call
    call: review
    rules:
      - condition: COMPLETE
        next: caccia
      - condition: ABORT
        next: review_failed
  - name: caccia
    description: CacciaでCodeRabbitの指摘に対応し、対応結果を確認する
    mode: system
    effects:
      - type: caccia
        pr: "{context:identify.pr.number}"
    rules:
      - condition: when(effect.caccia.caccia.success == false)
        next: caccia_failed
      - condition: when(effect.caccia.caccia.outcome == "success")
        next: wait_ci
      - condition: when(effect.caccia.caccia.outcome == "skipped")
        next: wait_ci
      - condition: when(effect.caccia.caccia.outcome == "limit")
        next: caccia_limit
      - condition: when(true)
        next: ABORT
  - name: wait_ci
    description: 対象コミットのCI完了を待ち、CIとPRの状態から次の処理を判定する
    mode: system
    system_inputs:
      - type: pr_status
        source: current_pr
        as: status
    wait:
      until: 'when(context.wait_ci.status.ci.finished == true && context.wait_ci.status.headSha == effect.caccia.caccia.headSha)'
      on_timeout: timeout
    rules:
      - condition: when(context.wait_ci.status.ci.passed == false)
        next: ci_failed
      - condition: when(context.wait_ci.status.mergeable != "MERGEABLE")
        next: unmergeable
      - condition: when(true)
        next: judge
  - name: judge
    description: レビュー報告とCI・PRの状態を確認し、承認可否と理由を判定する
    input_reports:
      from_step: review
      reports: all
    persona: supervisor
    edit: false
    capabilities: readonly
    structured_output:
      schema_ref: merge-decision
    instruction: |
      対象PRの累積差分について、完了済みのレビューステップから渡された次のレビューサマリーとその他の報告を確認してください。
      レビューサマリー: {report:review-summary.md}
      子workflowのCOMPLETEを承認とみなしてはいけません。未解消の指摘、外部レビューのCHANGES_REQUESTED、CI失敗、競合があれば approved=false とし、具体的な理由を出力してください。
      head SHA: {context:wait_ci.status.headSha}
      CI passed: {context:wait_ci.status.ci.passed}
      Mergeability: {context:wait_ci.status.mergeable}
      Merge state: {context:wait_ci.status.mergeStateStatus}
      Review decision: {context:wait_ci.status.reviewDecision}
      レビューレポートを確認できなければ approved=false とし、その理由を示してください。
      approved と reason を指定の構造化形式で出力してください。
    rules:
      - condition: when(true)
        next: comment_result
  - name: comment_result
    description: PRの承認可否とその理由をPRにコメントする
    mode: system
    effects:
      - type: comment_pr
        pr: "{context:identify.pr.number}"
        body: "TAKT evaluation: approved={structured:judge.approved}. {structured:judge.reason}"
    rules:
      - condition: when(effect.comment_result.comment_pr.success == true)
        next: COMPLETE
      - condition: when(true)
        next: ABORT
  - name: ci_failed
    description: 対象コミットのCIチェックが失敗したことをPRにコメントする
    mode: system
    effects:
      - type: comment_pr
        pr: "{context:identify.pr.number}"
        body: "TAKT evaluation failed: CI checks failed for head {context:wait_ci.status.headSha}."
    rules:
      - condition: when(effect.ci_failed.comment_pr.success == true)
        next: COMPLETE
      - condition: when(true)
        next: ABORT
  - name: timeout
    description: CI待機が制限時間を超えたことをPRにコメントする
    mode: system
    effects:
      - type: comment_pr
        pr: "{context:identify.pr.number}"
        body: "TAKT evaluation failed: waiting for CI exceeded 120 retries (30 minutes)."
    rules:
      - condition: when(effect.timeout.comment_pr.success == true)
        next: COMPLETE
      - condition: when(true)
        next: ABORT
  - name: unmergeable
    description: PRをマージできない状態であることをPRにコメントする
    mode: system
    effects:
      - type: comment_pr
        pr: "{context:identify.pr.number}"
        body: "TAKT evaluation failed: mergeability is {context:wait_ci.status.mergeable}."
    rules:
      - condition: when(effect.unmergeable.comment_pr.success == true)
        next: COMPLETE
      - condition: when(true)
        next: ABORT
  - name: review_failed
    description: 呼び出したレビューワークフローが中断したことをPRにコメントする
    mode: system
    effects:
      - type: comment_pr
        pr: "{context:identify.pr.number}"
        body: "TAKT evaluation failed: child review workflow aborted."
    rules:
      - condition: when(effect.review_failed.comment_pr.success == true)
        next: COMPLETE
      - condition: when(true)
        next: ABORT
  - name: caccia_limit
    description: Cacciaが反復上限に達したことと未解決スレッド数をPRにコメントする
    mode: system
    effects:
      - type: comment_pr
        pr: "{context:identify.pr.number}"
        body: "Caccia が反復上限に到達しました。未解決スレッド数: {effect:caccia.caccia.unresolvedCount}"
    rules:
      - condition: when(effect.caccia_limit.comment_pr.success == true)
        next: COMPLETE
      - condition: when(true)
        next: ABORT
  - name: caccia_failed
    description: Cacciaが失敗したこととエラーをPRにコメントする
    mode: system
    effects:
      - type: comment_pr
        pr: "{context:identify.pr.number}"
        body: "Caccia が失敗しました: {effect:caccia.caccia.error}"
    rules:
      - condition: when(true)
        next: ABORT
