{% raw %}name: Alexandria Daily Digest

# Serverless daily digest for the laptop tier — no always-on machine required.
# The cron is UTC (rendered from automation.send_hour_local + owner.timezone_default) and
# may drift +/-1h across DST; the digest CONTENT stays correct for wherever the owner is,
# because the engine pins the local date from Areas/Travel (DIGEST_TODAY).
#
# The engine travels IN this vault: `alexandria update`/`enable` vendor the exact
# installed version as a wheel at .github/engine/, and this job installs it straight
# from the checkout — no software-repo fetch, no token of any kind, and the pin
# is the literal bytes. A transient failure self-heals at the next scheduled run
# (the once/day dedup lives in the engine).
on:
  schedule:
    - cron: {% endraw %}"{{ digest_cron }}"{% raw %}
  workflow_dispatch: {}        # "send now" — the laptop replacement for SSH digest-now

concurrency:
  group: alexandria-digest
  cancel-in-progress: false

permissions:
  contents: write              # to commit the regenerated Action Board back

jobs:
  digest:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: "3.11"

      - name: Install the engine (vendored in this repo)
        run: |
          set -euo pipefail
          WHL="$(ls .github/engine/alexandria_vault-*.whl 2>/dev/null | head -1)"
          if [ -z "${WHL:-}" ]; then
            echo "::error::No vendored engine wheel at .github/engine/. Re-render this vault (alexandria update) on a machine with the software installed, commit the wheel, and push."
            exit 1
          fi
          pip install --quiet "$WHL[digest]"
          python -c "import alexandria_vault.engine as e; print('engine ok:', e.__name__)"

      - name: Send digest + regenerate precursors
        env:
          ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
          SMTP_HOST: ${{ secrets.SMTP_HOST }}
          SMTP_USER: ${{ secrets.SMTP_USER }}
          SMTP_PASS: ${{ secrets.SMTP_PASS }}
          DEFAULT_TZ: {% endraw %}"{{ owner.timezone_default | default('UTC') }}"{% raw %}
          SWEEP_SHADOW: "0"     # live writes (this is the real send, not a dry run)
          VAULT_DIR: ${{ github.workspace }}
        run: python -m alexandria_vault.engine.digest_now --vault "$GITHUB_WORKSPACE" --keep-scheduled

      - name: Commit regenerated precursors (Action Board, etc.)
        run: |
          git config user.name "Alexandria Bot"
          git config user.email "actions@users.noreply.github.com"
          git add -A
          git diff --cached --quiet || git commit -m "alexandria: digest precursors $(date -u +%F)"
          git push || true       # fail-soft: a rejected board push must not fail the send{% endraw %}
