name: merge-review
description: Review a PR, wait for CI and comment on the evaluation
max_steps: 51
initial_step: identify
steps:
  - name: identify
    description: Retrieve the target PR number and its current status.
    mode: system
    system_inputs:
      - type: pr_status
        source: current_pr
        as: pr
    rules:
      - condition: when(true)
        next: review
  - name: review
    description: Review the PR with reviewers suited to the changes and consolidate their results into a summary.
    kind: workflow_call
    call: review
    rules:
      - condition: COMPLETE
        next: caccia
      - condition: ABORT
        next: review_failed
  - name: caccia
    description: Address CodeRabbit findings through Caccia and check the outcome.
    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: Wait for CI to finish for the target commit and determine the next action from the CI and PR status.
    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: Inspect review reports and CI and PR status to determine approval and its rationale.
    input_reports:
      from_step: review
      reports: all
    persona: supervisor
    edit: false
    capabilities: readonly
    structured_output:
      schema_ref: merge-decision
    instruction: |
      Assess the cumulative PR diff using the following review summary and other supplied reports from the completed review step.
      Review summary: {report:review-summary.md}
      Child COMPLETE is not approval. Set approved=false for unresolved findings, CHANGES_REQUESTED reviews, failed CI or conflicts, and explain the reason.
      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}
      If the review reports cannot be verified, set approved=false and explain the reason.
      Return approved and reason in the required structured output format.
    rules:
      - condition: when(true)
        next: comment_result
  - name: comment_result
    description: Comment on the PR with the approval decision and its rationale.
    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: Comment on the PR that CI checks failed for the target commit.
    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: Comment on the PR that waiting for CI exceeded the time limit.
    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: Comment on the PR that its current state prevents merging.
    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: Comment on the PR that the called review workflow aborted.
    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: Comment on the PR that Caccia reached its iteration limit and report the unresolved thread count.
    mode: system
    effects:
      - type: comment_pr
        pr: "{context:identify.pr.number}"
        body: "Caccia reached its iteration limit. Unresolved threads: {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: Comment on the PR with the Caccia failure and its error.
    mode: system
    effects:
      - type: comment_pr
        pr: "{context:identify.pr.number}"
        body: "Caccia failed: {effect:caccia.caccia.error}"
    rules:
      - condition: when(true)
        next: ABORT
