name: development-remediation
description: 裁定済み finding を固定ファセットで計画・修正・検証する収束ループ。
all_steps:
  rules:
    - ref: findings-handling
      position: before_instruction
    - ref: plain-terminology
      position: before_instruction

subworkflow:
  callable: true
  visibility: internal
  returns:
    - need_replan
  params:
    fix_pool:
      type: facet_pool_ref
      default: coding-facets
    fix_companions:
      type: companion_ref[]
      default: []
    plan_policy:
      type: facet_ref[]
      facet_kind: policy
      default:
        - coding
        - testing
        - review
    fix_policy:
      type: facet_ref[]
      facet_kind: policy
      default:
        - coding
        - testing
    verification_policy:
      type: facet_ref[]
      facet_kind: policy
      default:
        - coding
        - testing
        - review
    fix_knowledge:
      type: facet_ref[]
      facet_kind: knowledge
      default:
        - architecture
        - implementation-semantics
    verification_knowledge:
      type: facet_ref[]
      facet_kind: knowledge
      default:
        - architecture
        - unit-testing
        - e2e-testing
    fix_plan_instruction:
      type: facet_ref
      facet_kind: instruction
      default: fix-plan-from-review-resolution
    fix_replan_instruction:
      type: facet_ref
      facet_kind: instruction
      default: fix-replan
    fix_plan_report_format:
      type: facet_ref
      facet_kind: report_format
      default: fix-plan
facet_pools:
  coding-facets:
    uses: coding-facets
  takt-coding-facets:
    uses: takt-coding-facets
initial_step: fix-plan
loop_monitors:
  - cycle:
      - fix-replan
      - fix
    ignore_steps:
      - fix-verifier
      - fix-retry
    threshold: 4
    judge:
      persona: supervisor
      instruction: loop-monitor-reviewers-fix
      rules:
        - condition: 健全（修正進捗があり、報告内容も収束している）
          next: fix-replan
        - condition: 修正未完了または報告未収束だが、次のレビュー・修正が実行可能である
          next: fix-replan
        - condition: 修正方針の前提を変える必要があり、再計画が実行可能である
          next: fix-replan
        - condition: 要件を満たす実現可能な打開手段がない
          next: ABORT
  - cycle:
      - fix-retry
      - fix-verifier
    threshold: 4
    judge:
      persona: supervisor
      instruction: loop-monitor-reviewers-fix
      rules:
        - condition: 健全（修正進捗があり、報告内容も収束している）
          next: fix-retry
        - condition: 修正未完了または報告未収束だが、次の修正が実行可能である
          next: fix-retry
        - condition: 修正方針の前提を変える必要があり、再計画が実行可能である
          next: fix-replan
        - condition: 要件を満たす実現可能な打開手段がない
          next: ABORT
steps:
  - name: fix-plan
    uses: peer-review-fix-plan
    with:
      plan_policy:
        $param: plan_policy
      fix_knowledge:
        $param: fix_knowledge
      plan_instruction:
        $param: fix_plan_instruction
      plan_report_format:
        $param: fix_plan_report_format
    rules:
      - condition: 修正計画を確定した、または原因未確定でも現行の要求・修正範囲内で具体的な調査と、その結果に応じた安全な修正・検証を最初の修正単位として実行できる（未実行・未確認だけではタスク全体の再計画にしない）
        next: fix
      - condition: 要求・修正範囲などタスク全体の前提を見直す必要があり、その変更で具体的なプロジェクト内の作業が実行可能になる（局所的な原因調査の未実行だけでは該当しない）
        return: need_replan
      - condition: 必要な作業をプロジェクト内で進める手段がなく、その根拠と必要な外部操作または両立不能な条件が確認されている
        next: ABORT
  - name: fix-replan
    uses: peer-review-fix-plan
    with:
      plan_policy:
        $param: plan_policy
      fix_knowledge:
        $param: fix_knowledge
      plan_instruction:
        $param: fix_replan_instruction
      plan_report_format:
        $param: fix_plan_report_format
    rules:
      - condition: 修正計画を確定した、または原因未確定でも現行の要求・修正範囲内で具体的な調査と、その結果に応じた安全な修正・検証を最初の修正単位として実行できる（未実行・未確認だけではタスク全体の再計画にしない）
        next: fix
      - condition: 要求・修正範囲などタスク全体の前提を見直す必要があり、その変更で具体的なプロジェクト内の作業が実行可能になる（局所的な原因調査の未実行だけでは該当しない）
        return: need_replan
      - condition: 必要な作業をプロジェクト内で進める手段がなく、その根拠と必要な外部操作または両立不能な条件が確認されている
        next: ABORT
  - name: fix
    capabilities:
      - edit
      - enable-skills
    uses: peer-review-fix
    with:
      fix_policy:
        $param: fix_policy
      fix_knowledge:
        $param: fix_knowledge
      fix_instruction: apply-fix-plan
    companion:
      $param: fix_companions
    rules:
      - condition: 修正完了
        next: fix-verifier
      - condition: 修正計画の見直しが必要
        next: fix-replan
      - condition: タスク全体の再計画が必要
        return: need_replan
  - name: fix-verifier
    uses: peer-review-fix-verifier
    with:
      verification_policy:
        $param: verification_policy
      fix_knowledge:
        $param: verification_knowledge
    rules:
      - condition: verified
        next: COMPLETE
      - condition: plan_invalid — 修正計画に必要な要素・状態・経路の漏れ、対象外の混入、または前提・境界・方法・証拠能力の不備がある。実装不足または証拠不足も同時にある場合を含む
        next: fix-replan
      - condition: incomplete — 修正計画に不備はないが、計画済みの義務に実装不足または証拠不足がある
        next: fix-retry
  - name: fix-retry
    capabilities:
      - edit
      - enable-skills
    uses: peer-review-fix
    with:
      fix_policy:
        $param: fix_policy
      fix_knowledge:
        $param: fix_knowledge
      fix_instruction: apply-fix-verification
    companion:
      $param: fix_companions
    rules:
      - condition: 修正完了
        next: fix-verifier
      - condition: 修正計画の見直しが必要
        next: fix-replan
      - condition: タスク全体の再計画が必要
        return: need_replan
