name: Agent Quality Toolkit
description: >
  Run agentlint, agentmd drift, and coderace review on every PR,
  then post an aggregated quality comment. Part of the agentkit-cli suite.
author: mikiships

inputs:
  python-version:
    description: Python version to use
    required: false
    default: '3.11'
  min-lint-score:
    description: Minimum agentlint score (0-100). Action fails if score is below this.
    required: false
    default: '70'
  post-comment:
    description: Post a PR comment with the quality report
    required: false
    default: 'true'
  github-token:
    description: GitHub token for posting PR comments
    required: true
  mode:
    description: 'Action mode: run (default) or compare'
    required: false
    default: 'run'
  compare-base:
    description: Base ref for compare mode (leave empty to use PR base SHA)
    required: false
    default: ''
  compare-head:
    description: Head ref for compare mode (leave empty to use current SHA)
    required: false
    default: ''
  min-delta:
    description: Minimum net delta for compare mode. Fails if net delta is below this.
    required: false
    default: '0'
  save-history:
    description: Save run scores to history.json artifact (true/false)
    required: false
    default: 'false'
  notify-slack:
    description: Slack webhook URL for gate result notifications
    required: false
    default: ''
  notify-discord:
    description: Discord webhook URL for gate result notifications
    required: false
    default: ''
  notify-webhook:
    description: Generic JSON webhook URL for gate result notifications
    required: false
    default: ''
  notify-on:
    description: When to notify — fail (default) or always
    required: false
    default: 'fail'
  gist-token:
    description: GitHub token for publishing results as a GitHub Gist (optional). If provided, run results are auto-published and gist-url output is set.
    required: false
    default: ''

outputs:
  lint-score:
    description: agentlint context score (0-100)
    value: ${{ steps.run-toolkit.outputs.lint-score }}
  drift-status:
    description: agentmd drift status (fresh / drifted / skipped)
    value: ${{ steps.run-toolkit.outputs.drift-status }}
  review-summary:
    description: coderace review summary string
    value: ${{ steps.run-toolkit.outputs.review-summary }}
  history-json:
    description: JSON string of latest run history (when save-history=true)
    value: ${{ steps.save-history.outputs.history-json }}
  leaderboard-json:
    description: JSON string of leaderboard data grouped by label (when save-history=true)
    value: ${{ steps.save-history.outputs.leaderboard-json }}
  gist-url:
    description: GitHub Gist URL for the published run results (when gist-token is provided)
    value: ${{ steps.publish-gist.outputs.gist-url }}

runs:
  using: composite
  steps:
    - name: Set up Python
      uses: actions/setup-python@v5
      with:
        python-version: ${{ inputs.python-version }}

    - name: Install agentkit-cli and dependencies
      shell: bash
      run: |
        pip install --upgrade pip
        pip install agentkit-cli agentmd agentlint coderace agentreflect

    - name: Run Agent Quality Toolkit
      id: run-toolkit
      shell: bash
      env:
        GITHUB_TOKEN: ${{ inputs.github-token }}
        MIN_LINT_SCORE: ${{ inputs.min-lint-score }}
        POST_COMMENT: ${{ inputs.post-comment }}
        PR_NUMBER: ${{ github.event.pull_request.number }}
        REPO: ${{ github.repository }}
        AGENTKIT_MODE: ${{ inputs.mode }}
        COMPARE_BASE: ${{ inputs.compare-base }}
        COMPARE_HEAD: ${{ inputs.compare-head }}
        MIN_DELTA: ${{ inputs.min-delta }}
      run: |
        if [ "${{ inputs.mode }}" = "compare" ]; then
          BASE="${{ inputs.compare-base }}"
          HEAD_REF="${{ inputs.compare-head }}"
          if [ -z "$BASE" ]; then BASE="${{ github.event.pull_request.base.sha }}"; fi
          if [ -z "$HEAD_REF" ]; then HEAD_REF="${{ github.sha }}"; fi
          agentkit compare --ci --min-delta "${{ inputs.min-delta }}" "$BASE" "$HEAD_REF"
        else
          python "${{ github.action_path }}/scripts/run-agentkit-action.py"
        fi

    - name: Save history artifact
      id: save-history
      if: inputs.save-history == 'true'
      shell: bash
      run: |
        PROJECT="$(basename $GITHUB_WORKSPACE)"
        HISTORY_JSON=$(agentkit history --json --project "$PROJECT" 2>/dev/null || echo '{"runs":[],"sparkline":""}')
        echo "$HISTORY_JSON" > history.json
        echo "history-json=$HISTORY_JSON" >> "$GITHUB_OUTPUT"
        LEADERBOARD_JSON=$(agentkit leaderboard --json --project "$PROJECT" 2>/dev/null || echo '{"tool":"overall","project":"'$PROJECT'","leaderboard":[]}')
        echo "leaderboard-json=$LEADERBOARD_JSON" >> "$GITHUB_OUTPUT"

    - name: Publish results as GitHub Gist
      id: publish-gist
      if: inputs.gist-token != ''
      shell: bash
      env:
        GIST_TOKEN: ${{ inputs.gist-token }}
      run: |
        PROJECT="$(basename $GITHUB_WORKSPACE)"
        GIST_RESPONSE=$(agentkit gist --public --description "agentkit run: $PROJECT" 2>/dev/null || echo '')
        GIST_URL=$(echo "$GIST_RESPONSE" | grep -oE 'https://gist\.github\.com/[a-zA-Z0-9/_-]+' | head -1 || echo '')
        echo "gist-url=$GIST_URL" >> "$GITHUB_OUTPUT"
        if [ -n "$GIST_URL" ]; then echo "Gist published: $GIST_URL"; fi

    - name: Post PR comment
      if: inputs.post-comment == 'true' && github.event_name == 'pull_request'
      shell: bash
      env:
        GITHUB_TOKEN: ${{ inputs.github-token }}
        PR_NUMBER: ${{ github.event.pull_request.number }}
        REPO: ${{ github.repository }}
        QUALITY_SUMMARY_FILE: /tmp/agentkit-quality-summary.json
      run: |
        python "${{ github.action_path }}/scripts/post-pr-comment.py"
