# ═══════════════════════════════════════════════
# 🔺 Sovereign: TCS-0002∞ | Root: SYS-GLW-0001
# 📜 Copyright: 国作登字-2026-A-00037559
# ═══════════════════════════════════════════════
name: 铸渊 · PSP 分身巡检

on:
  schedule:
    - cron: '0 1 * * *'   # UTC 01:00 = 北京时间 09:00
  workflow_dispatch:        # 手动触发

jobs:
  # ── AGE OS v1.0: 核心大脑唤醒（所有流程的前提）──
  wake-brain:
    name: 🌅 唤醒核心大脑
    runs-on: ubuntu-latest
    permissions:
      contents: read
    outputs:
      brain_awake: ${{ steps.wake.outputs.brain_awake }}

    steps:
      - uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: 🧠 唤醒铸渊核心大脑
        id: wake
        env:
          LLM_API_KEY: ${{ secrets.ZY_LLM_API_KEY }}
          LLM_BASE_URL: ${{ secrets.ZY_LLM_BASE_URL }}
          ANTHROPIC_API_KEY: ${{ secrets.ZY_LLM_API_KEY }}
          OPENAI_API_KEY: ${{ secrets.ZY_LLM_API_KEY }}
          DASHSCOPE_API_KEY: ${{ secrets.ZY_LLM_API_KEY }}
          DEEPSEEK_API_KEY: ${{ secrets.ZY_LLM_API_KEY }}
        run: |
          node core/brain-wake --task "PSP 分身巡检" || {
            echo "⚠️ 核心大脑唤醒失败，使用 dry-run 模式继续"
            echo "brain_awake=dry-run" >> "$GITHUB_OUTPUT"
          }

  psp-inspect:
    name: 🔍 铸渊 PSP 每日巡检
    needs: wake-brain
    runs-on: ubuntu-latest
    permissions:
      contents: write
      actions:  read

    steps:
      - uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: 安装依赖
        run: npm ci

      - name: 执行 PSP 巡检（5项检查）
        env:
          EMAIL_ADDRESS:      ${{ secrets.ZY_SMTP_USER }}
          EMAIL_APP_PASSWORD: ${{ secrets.ZY_SMTP_PASS }}
          GITHUB_TOKEN:       ${{ secrets.GITHUB_TOKEN }}
          GITHUB_REPOSITORY:  ${{ github.repository }}
        run: node scripts/psp-inspection.js

      - name: 提交巡检结果
        run: |
          git config user.name "铸渊 (ZhùYuān)"
          git config user.email "zhuyuan@guanghulab.com"
          git add signal-log/ dev-nodes/ notion-push/ .github/brain/memory.json
          git diff --cached --quiet || git commit -m "🔍 铸渊PSP巡检 · $(date +%Y-%m-%d)"
          git push
