name: merge-review-fix-simple
description: 1体の読み取り専用レビューと裁定・修正・検証を経て、CI通過後にPRをマージする
max_steps: 75
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: sync
  - name: sync
    description: 対象PRにベースブランチの変更を取り込んでプッシュし、同期結果を確認する
    mode: system
    effects:
      - type: sync_with_root
        pr: "{context:identify.pr.number}"
    rules:
      - condition: when(effect.sync.sync_with_root.success == true && effect.sync.sync_with_root.resyncCount > 0)
        next: push
      - condition: when(effect.sync.sync_with_root.success == true)
        next: review
      - condition: when(effect.sync.sync_with_root.conflicted == true)
        next: resolve_conflicts
      - condition: when(true)
        next: sync_failed
  - name: resolve_conflicts
    description: ベースブランチとの競合をAIで解消してプッシュし、解消結果を確認する
    mode: system
    effects:
      - type: resolve_conflicts_with_ai
        pr: "{context:identify.pr.number}"
    rules:
      - condition: when(effect.resolve_conflicts.resolve_conflicts_with_ai.success == true && effect.sync.sync_with_root.resyncCount > 0)
        next: push
      - condition: when(effect.resolve_conflicts.resolve_conflicts_with_ai.success == true)
        next: review
      - condition: when(true)
        next: conflict_failed
  - name: review
    description: 1体のレビュアーでPRをレビューし、指摘の裁定・修正・検証と要件確認を実施する
    kind: workflow_call
    call: review-fix-simple
    rules:
      - condition: COMPLETE
        next: push
      - condition: ABORT
        next: review_failed
  - name: push
    description: レビュー後の変更をコミットして対象PRへプッシュする
    mode: system
    effects:
      - type: commit_and_push
        pr: "{context:identify.pr.number}"
    rules:
      - condition: when(effect.push.commit_and_push.success == true)
        next: caccia
      - condition: when(true)
        next: push_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
        rerun_from: "{effect:rerun_ci.rerun_ci.rerun}"
    wait:
      until: 'when(context.wait_ci.status.rerunHeadChanged == true || 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.rerunHeadChanged == true)
        next: collect_ci_failure
      - condition: when(context.wait_ci.status.ci.passed == false)
        next: collect_ci_failure
      - condition: when(context.wait_ci.status.mergeable == "CONFLICTING" && effect.sync.sync_with_root.resyncCount < effect.sync.sync_with_root.resyncLimit)
        next: sync
      - condition: when(context.wait_ci.status.mergeable != "MERGEABLE" && context.wait_ci.status.mergeable != "BEHIND")
        next: unmergeable
      - condition: when(context.wait_ci.status.reviewDecision == "CHANGES_REQUESTED")
        next: changes_requested
      - condition: when(true)
        next: merge
  - name: merge
    description: 対象PRをマージし、成功したかを確認する
    mode: system
    effects:
      - type: merge_pr
        pr: "{context:identify.pr.number}"
    rules:
      - condition: when(effect.merge.merge_pr.success == true)
        next: merged
      - condition: when(true)
        next: merge_failed
  - name: changes_requested
    description: 変更を求めるGitHubレビューが残っていることをPRにコメントする
    mode: system
    effects:
      - type: comment_pr
        pr: "{context:identify.pr.number}"
        body: "TAKT evaluation: approved=false. GitHub review decision is CHANGES_REQUESTED; reviewer-requested changes must be addressed before merge."
    rules:
      - condition: when(effect.changes_requested.comment_pr.success == true)
        next: COMPLETE
      - condition: when(true)
        next: ABORT
  - name: collect_ci_failure
    description: 対象コミットで失敗したCIのログと実行情報、回復の試行回数を収集する
    mode: system
    system_inputs:
      - type: pr_ci_failure
        source: current_pr
        as: failure
        head_sha: "{effect:caccia.caccia.headSha}"
    rules:
      - condition: when(true)
        next: investigate_ci
  - name: investigate_ci
    description: CIの失敗証拠とPR差分を調べ、原因と所在、根本修正の妥当性を判定する
    capabilities: readonly
    edit: false
    persona: planner
    policy: merge-ci-recovery
    instruction: merge-ci-investigate
    structured_output:
      schema_ref: merge-ci-investigation
    output_contracts:
      report:
        - name: ci-investigation.md
          format: merge-ci-investigation
    rules:
      - condition: when(true)
        next: refresh_ci
  - name: refresh_ci
    description: CIと対象コミットの最新状態を取得し、調査結果と試行回数から待機・修正・再実行・停止を判定する
    mode: system
    system_inputs:
      - type: pr_status
        source: current_pr
        as: status
        rerun_from: "{effect:rerun_ci.rerun_ci.rerun}"
    rules:
      - condition: when(context.refresh_ci.status.headSha == effect.caccia.caccia.headSha && context.refresh_ci.status.ci.passed == true)
        next: wait_ci
      - condition: when(context.refresh_ci.status.headSha == effect.caccia.caccia.headSha && context.refresh_ci.status.ci.finished == false)
        next: wait_ci
      - condition: when(context.collect_ci_failure.failure.complete == false)
        next: ci_stopped
      - condition: when(context.collect_ci_failure.failure.rerunCount > 0)
        next: ci_stopped
      - condition: when(structured.investigate_ci.verdict == "defect" && context.collect_ci_failure.failure.fixRounds < 2)
        next: fix_ci
      - condition: when(structured.investigate_ci.verdict == "external")
        next: rerun_ci
      - condition: when(true)
        next: ci_stopped
  - name: fix_ci
    description: 調査で裏付けられたCI失敗の根本原因を修正し、テストとビルドで検証する
    capabilities: edit
    edit: true
    persona: coder
    policy: merge-ci-recovery
    instruction: merge-ci-fix
    output_contracts:
      report:
        - name: ci-fix.md
          format: merge-ci-fix
    rules:
      - condition: CIの根本原因を修正して検証した
        next: review_ci_fix
      - condition: 裏付けられた原因を安全に修正または検証できない
        next: ci_stopped
  - name: review_ci_fix
    description: CI修正の差分と検証証拠から、根本原因の解消と既存契約の維持を確認して承認可否を判定する
    capabilities: readonly
    edit: false
    persona: coding-reviewer
    policy: merge-ci-recovery
    instruction: merge-ci-review
    structured_output:
      schema_ref: simple-review-result
    output_contracts:
      report:
        - name: ci-fix-review.md
          format: merge-ci-review
    rules:
      - condition: when(structured.review_ci_fix.result == "APPROVE")
        next: push_ci_fix
      - condition: when(true)
        next: ci_stopped
  - name: push_ci_fix
    description: 承認されたCI修正をコミットして対象PRへプッシュする
    mode: system
    effects:
      - type: commit_and_push
        pr: "{context:identify.pr.number}"
    rules:
      - condition: when(effect.push_ci_fix.commit_and_push.success == true)
        next: caccia
      - condition: when(true)
        next: ci_push_failed
  - name: rerun_ci
    description: 外部要因と判定したCI失敗について、対象のチェックを再実行する
    mode: system
    effects:
      - type: rerun_ci
        pr: "{context:identify.pr.number}"
        failure: "{context:collect_ci_failure.failure}"
    rules:
      - condition: when(effect.rerun_ci.rerun_ci.success == true)
        next: wait_ci
      - condition: when(true)
        next: collect_ci_failure
  - name: ci_stopped
    description: CI回復を停止した理由と調査結果、試行回数、残る証拠不足をPRにコメントする
    mode: system
    effects:
      - type: comment_pr
        pr: "{context:identify.pr.number}"
        body_format: plain_text
        body: "TAKTのCI回復を停止しました。修正回数: {context:collect_ci_failure.failure.fixRounds}、再実行回数: {context:collect_ci_failure.failure.rerunCount}。調査要点: {structured:investigate_ci.summary}。最後の失敗チェック・run・job・証拠不足: {context:collect_ci_failure.failure.details}"
    rules:
      - condition: when(true)
        next: ABORT
  - name: ci_push_failed
    description: CI修正のプッシュ失敗と調査結果、試行回数、残る証拠不足をPRにコメントする
    mode: system
    effects:
      - type: comment_pr
        pr: "{context:identify.pr.number}"
        body_format: plain_text
        body: "TAKTのCI回復を停止しました。修正回数: {context:collect_ci_failure.failure.fixRounds}、再実行回数: {context:collect_ci_failure.failure.rerunCount}。調査要点: {structured:investigate_ci.summary}。最後の失敗チェック・run・job・証拠不足: {context:collect_ci_failure.failure.details}。pushエラー: {effect:push_ci_fix.commit_and_push.error}"
    rules:
      - 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: merged
    description: レビューとCIを経てPRをマージしたことをPRにコメントする
    mode: system
    effects:
      - type: comment_pr
        pr: "{context:identify.pr.number}"
        body: "TAKT merged this PR after its review workflow completed and CI passed for head {context:wait_ci.status.headSha}."
    rules:
      - condition: when(effect.merged.comment_pr.success == true)
        next: COMPLETE
      - condition: when(true)
        next: ABORT
  - name: sync_failed
    description: ベースブランチとの同期に失敗したこととエラーをPRにコメントする
    mode: system
    effects:
      - type: comment_pr
        pr: "{context:identify.pr.number}"
        body: "TAKT evaluation failed: sync_with_root: {effect:sync.sync_with_root.error}"
    rules:
      - condition: when(effect.sync_failed.comment_pr.success == true)
        next: COMPLETE
      - condition: when(true)
        next: ABORT
  - name: conflict_failed
    description: AIによる競合解消に失敗したこととエラーをPRにコメントする
    mode: system
    effects:
      - type: comment_pr
        pr: "{context:identify.pr.number}"
        body: "TAKT evaluation failed: resolve_conflicts_with_ai: {effect:resolve_conflicts.resolve_conflicts_with_ai.error}"
    rules:
      - condition: when(effect.conflict_failed.comment_pr.success == true)
        next: COMPLETE
      - condition: when(true)
        next: ABORT
  - name: push_failed
    description: 変更のコミットまたはプッシュに失敗したこととエラーをPRにコメントする
    mode: system
    effects:
      - type: comment_pr
        pr: "{context:identify.pr.number}"
        body: "TAKT evaluation failed: commit_and_push: {effect:push.commit_and_push.error}"
    rules:
      - condition: when(effect.push_failed.comment_pr.success == true)
        next: COMPLETE
      - condition: when(true)
        next: ABORT
  - name: merge_failed
    description: PRのマージに失敗したこととエラーをPRにコメントする
    mode: system
    effects:
      - type: comment_pr
        pr: "{context:identify.pr.number}"
        body: "TAKT evaluation passed but merge_pr failed: {effect:merge.merge_pr.error}"
    rules:
      - condition: when(effect.merge_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
