# loom-verify.yml — loom 规范自动化门禁
# 由 `loom init-project` 可选生成到 .github/workflows/loom-verify.yml

name: loom verify

on:
  pull_request:
    paths:
      - "src/**"
      - "app/**"
      - "lib/**"
      - "specs/**"
      - ".loom/**"

jobs:
  loom-checks:
    name: loom integrity checks
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

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

      - name: Install loom
        run: npm install -g loom-engineering

      - name: Check constitution placeholders
        run: |
          if [ -f .loom/rules/constitution.md ]; then
            if grep -qE '\{\{[A-Z_]+\}\}' .loom/rules/constitution.md; then
              echo "::error::constitution.md contains unrendered placeholders"
              grep -oE '\{\{[A-Z_]+\}\}' .loom/rules/constitution.md | sort -u
              exit 1
            fi
          fi

      - name: Check workflow.yaml exists
        run: |
          if [ ! -f .loom/workflow.yaml ]; then
            echo "::error::.loom/workflow.yaml is missing"
            exit 1
          fi

      - name: Check graph backend status
        run: |
          if [ -f .loom/graph.config.json ]; then
            BACKEND=$(node -e "console.log(require('./.loom/graph.config.json').backend || 'codegraph')")
            ENABLED=$(node -e "console.log(require('./.loom/graph.config.json').enabled !== false)")
          else
            BACKEND="codegraph"
            ENABLED="true"
          fi
          if [ "$ENABLED" = "true" ] && [ "$BACKEND" != "none" ]; then
            case "$BACKEND" in
              codegraph)
                if [ -d .codegraph ]; then
                  loom index --check
                else
                  echo "codegraph enabled but .codegraph/ not found, skipping index check"
                fi
                ;;
              *)
                echo "graph backend '$BACKEND' configured, running loom index --check"
                loom index --check
                ;;
            esac
          else
            echo "graph backend disabled ($BACKEND), skipping index check"
          fi

      - name: Check task file ownership conflicts
        run: |
          for spec_dir in specs/*/; do
            if [ -d "${spec_dir}tasks" ]; then
              echo "Checking ${spec_dir}..."
              loom tasks --spec-dir "${spec_dir}" --validate
            fi
          done

      # ── 产物完整性校验 ──────────────────────────────────────
      - name: Verify spec artifacts
        run: |
          for spec_dir in specs/*/; do
            if [ -f "${spec_dir}spec.md" ]; then
              echo "Verifying ${spec_dir}..."
              node node_modules/.bin/loom-verify-artifacts \
                --spec-dir "${spec_dir}" 2>/dev/null || \
              node "$(npm root -g)/loom-engineering/skills/loom-verification-before-completion/scripts/verify-artifacts.mjs" \
                --spec-dir "${spec_dir}" || true
            fi
          done
