name: Issue Triage

on:
  issues:
    types: [opened, reopened, closed]
  issue_comment:
    types: [created]

permissions: {}

concurrency:
  # Only one triage run per issue at a time. New runs queue (not cancel)
  # to avoid killing in-flight runs when the bot posts its own comment.
  group: issue-triage-${{ github.event.issue.number }}
  cancel-in-progress: false

jobs:
  triage:
    # Skip pull requests and bot comments
    if: >-
      !github.event.issue.pull_request &&
      (github.event.action != 'created' ||
       (github.event.comment.user.login != 'astrobot-houston' &&
        github.event.comment.user.login != 'github-actions[bot]'))
    runs-on: ubuntu-latest
    timeout-minutes: 60
    permissions:
      contents: read # Read repo (push uses FREDKBOT_GITHUB_TOKEN)
      issues: read # Read issue details for triage
      id-token: write # OIDC auth for pkg.pr.new preview releases
    steps:
      - name: Checkout
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          fetch-depth: 0
          persist-credentials: false

      - name: Configure Git identity
        run: |
          git config user.name "astrobot-houston"
          git config user.email "fred+astrobot@astro.build"

      - name: Setup PNPM
        uses: pnpm/action-setup@0e279bb959325dab635dd2c09392533439d90093 # v6.0.8

      - name: Setup Node
        uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
        with:
          node-version: 24.15.0
          cache: pnpm

      - name: Install agent CLIs (bgproc, agent-browser)
        run: |
          npm install -g bgproc agent-browser
          agent-browser install

      - name: Clone Astro Compiler (for debugging)
        run: git clone --depth 1 https://github.com/withastro/compiler.git .compiler

      - name: Install deps
        run: pnpm install --frozen-lockfile

      - name: Build
        run: pnpm build

      - name: Run triagebot
        uses: withastro/triagebot-action@b16c8f2209da49daed89811cf6f9a50b0270168e # v0.3.9
        with:
          read-token: ${{ secrets.GITHUB_TOKEN }}
          write-token: ${{ secrets.FREDKBOT_GITHUB_TOKEN }}
          anthropic-api-key: ${{ secrets.CI_ANTHROPIC_API_KEY }}
          triage-skill: .agents/skills/triage
          pr-skill: .agents/skills/astro-pr-writer
          pr-skill-name: astro-pr-writer
          bot-logins: astrobot-houston
