# Wayfind distillation pipeline
#
# Copy this file to .github/workflows/wayfind-distill.yml in your team-context repo.
#
# Runs nightly (2am UTC) and on manual dispatch. Journal push triggers are intentionally
# absent — raw journal content is already distributed via git pull + wayfind context pull.
# This pipeline only produces distilled/merged entries (the LLM summarization layer).
#
# Team members get distilled entries automatically on their next `wayfind context pull`.
#
# Required secrets:
#   ANTHROPIC_API_KEY  — for LLM merge calls during distillation

name: Wayfind Distillation

on:
  schedule:
    - cron: '0 2 * * *'   # Nightly: 2am UTC
  workflow_dispatch:
    inputs:
      tier:
        description: 'Distillation tier (daily, weekly, archive, all)'
        default: 'all'

# One run at a time — don't cancel in-progress (distillation is expensive).
concurrency:
  group: wayfind-distill
  cancel-in-progress: false

jobs:
  distill:
    runs-on: ubuntu-latest
    permissions:
      contents: write

    steps:
      - uses: actions/checkout@v4
        with:
          token: ${{ secrets.GITHUB_TOKEN }}

      - uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: Install Wayfind
        run: npm install -g wayfind

      - name: Index journals
        env:
          TEAM_CONTEXT_STORE_PATH: ${{ github.workspace }}/.wayfind/store
        run: wayfind index-journals --dir ./journals --no-embeddings

      - name: Distill
        env:
          TEAM_CONTEXT_STORE_PATH: ${{ github.workspace }}/.wayfind/store
          ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
        run: wayfind distill --tier ${{ github.event.inputs.tier || 'all' }}

      - name: Export distilled entries
        env:
          TEAM_CONTEXT_STORE_PATH: ${{ github.workspace }}/.wayfind/store
        run: wayfind distill export --output .wayfind/distilled.json

      - name: Commit if changed
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git add .wayfind/distilled.json
          if git diff --cached --quiet; then
            echo "No changes to distilled.json"
          else
            git commit -m "chore: update distilled context [skip ci]"
            git push
          fi
