# template: pipeline-fix
# version: 1
# description: Targeted bug fix to dispatch/pipeline code (test-first)
# mandatory_final_phase: audit_dispatch.py

parameters:
  - name: bug_id
    type: string
    pattern: "^[a-z0-9-]+$"
    description: "Bug identifier (e.g., 'ref-hydration-drop', 'sha-mismatch')"
  - name: affected_files
    type: list_path
    min: 1
    description: "Files that contain the bug (project-relative paths, comma-separated)"
  - name: reproduction
    type: string
    description: "How to reproduce: test command, CLI invocation, or precise failure sequence"
  - name: root_cause
    type: string
    description: "One-line root cause (must be known before build starts)"

phases:
  - name: reproduce
    engine: claude
    description: "Reproduction sequence (from user): {{ reproduction }}. Write a failing test in recoil/tests/test_fix_{{ bug_id_snake }}.py that triggers this exact sequence and asserts the buggy behavior. Test MUST fail at this phase — that proves we can reproduce."
    files_create:
      - "recoil/tests/test_fix_{{ bug_id_snake }}.py"
    validation: |
      PYTHONPATH=. pytest "recoil/tests/test_fix_{{ bug_id_snake }}.py" -x --tb=short 2>&1 | grep -q FAILED && \
      echo "reproduce OK (test fails as expected)"

  - name: fix
    engine: claude
    depends_on: [reproduce]
    description: "Fix the bug in the affected files. Do NOT modify the test from the reproduce phase."
    files_modify_param: affected_files
    validation: |
      PYTHONPATH=. pytest "recoil/tests/test_fix_{{ bug_id_snake }}.py" -x --tb=short && \
      echo "fix OK"

  - name: regression-check
    engine: claude
    depends_on: [fix]
    description: "Verify no regressions in the broader test suite"
    validation: |
      PYTHONPATH=. pytest recoil/tests/ -x --tb=short -q && \
      echo "regression-check OK"

  - name: dispatch-audit
    mandatory: true
    description: "Run the dispatch audit to verify modality x model paths still work"
    validation: |
      PYTHONPATH=. python3 recoil/pipeline/tools/audit_dispatch.py && \
      echo "dispatch-audit OK"

gates:
  pre_build:
    - description: "Root cause must be documented (non-empty)"
      command: |
        test -n "{{ root_cause }}"
