name: Temporary minimal regression fixes

on:
  pull_request:
    branches: [main]
    paths:
      - tools/apply_minimal_regression_fixes.py
      - .github/workflows/temp-minimal-regression-fixes.yml
  workflow_dispatch:

permissions:
  contents: write

jobs:
  apply-and-test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ github.head_ref || github.ref_name }}
          fetch-depth: 0
      - uses: actions/setup-python@v5
        with:
          python-version: '3.9'
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          python -m pip install -r requirements.txt
      - name: Apply minimal fixes and test on Python 3.9
        run: |
          python -m py_compile tools/apply_minimal_regression_fixes.py
          python tools/apply_minimal_regression_fixes.py
          python -m compileall -q .
          python -m unittest discover -s tests -p 'test_*.py'
      - uses: actions/setup-python@v5
        with:
          python-version: '3.12'
      - name: Install dependencies on Python 3.12
        run: |
          python -m pip install --upgrade pip
          python -m pip install -r requirements.txt
      - name: Test on Python 3.12
        run: |
          python -m compileall -q .
          python -m unittest discover -s tests -p 'test_*.py'
      - name: Commit verified changes to fix branch
        if: github.event_name == 'pull_request'
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git add -A
          if ! git diff --cached --quiet; then
            git commit -m "fix: harden postprocessing and recovery boundaries"
            git push origin HEAD:${{ github.head_ref }}
          fi
