name: review-fix-backend
description: Backend-focused review + fix loop (structure, modularization, hexagonal architecture, security, coding)
all_steps:
  rules:
    - ref: findings-handling
      position: before_instruction
    - ref: plain-terminology
      position: before_instruction

max_steps: 30
facet_pools:
  security-review-facets:
    uses: security-review-facets
initial_step: gather
loop_monitors:
  - cycle:
      - reviewers
      - final-gate
      - remediation
    threshold: 3
    judge:
      input_reports:
        - from_step: reviewers
          reports: all
          history: {previous: 2}
        - from_step: remediation
          reports: [review-resolution.md, fix-plan.md, fix-report.md, fix-verification.md]
          history: {previous: 2}
      persona: supervisor
      instruction: loop-monitor-reviewers-fix
      rules:
        - condition: Healthy (fixes progressing and report content converging)
          next: reviewers
        - condition: Implementation incomplete or report not converged, but the next review or fix can be performed
          next: reviewers
        - condition: The current loop cannot converge and requires a final-gate decision
          next: final-gate

steps:
  - name: gather
    uses: review-gather-to-reviewers
    rules:
      - condition: Review target information gathered
        next: reviewers
      - condition: Unable to identify review target, insufficient information
        next: ABORT

  - name: reviewers
    input_reports:
      from_step: remediation
      reports:
        - fix-plan.md
        - fix-report.md
        - fix-verification.md
      optional_if_unrun: true
    uses: review-fix-backend-reviewers
    with:
      architecture_review_instruction: review-arch
      coding_review_instruction: review-coding
      security_review_instruction: review-security
    rules:
      self:
        - condition: any("error")
          next: reviewers
        - condition: all("approved")
          next: final-gate
        - condition: any("needs_fix")
          next: remediation
      parallel:
        arch-review:
          - condition: approved
          - condition: needs_fix
        coding-review:
          - condition: approved
          - condition: needs_fix
        security-review:
          - condition: approved
          - condition: needs_fix

  - name: final-gate
    description: Check requirement fulfillment and finding resolution against current code and preceding reports to decide whether to complete, repair, or replan.
    input_reports:
      from_step: remediation
      reports:
        - review-resolution.md
      optional_if_unrun: true
    kind: workflow_call
    call: final-gate
    args:
      supervise_knowledge:
        - backend
        - security
        - architecture
      supervise_policy:
        - contract-change
        - backend
        - architecture
    rules:
      - condition: COMPLETE
        next: COMPLETE
      - condition: needs_fix
        next: remediation
      - condition: need_replan
        next: ABORT
      - condition: ABORT
        next: ABORT
  - name: remediation
    input_reports:
      - from_step: reviewers
        reports: all
        history: {previous: 2}
    uses: review-remediation
    with:
      plan_policy:
        - coding
        - testing
        - review
      fix_policy:
        - coding
        - testing
        - architecture
        - backend
      verification_policy:
        - coding
        - testing
        - review
      fix_knowledge:
        - backend
        - security
        - architecture
    rules:
      - condition: COMPLETE
        next: reviewers
      - condition: no_fix
        next: final-gate
      - condition: need_replan
        next: final-gate
      - condition: ABORT
        next: ABORT
