# Spurvik EU AI Act CI gate (spurvik-mcp-server@1.0.15 + spurvik@1.0.18 CLI, MIT)
# Save to: .github/workflows/spurvik-check.yml
# (2-line Composite Action alternative: `- uses: spurvik/spurvik@v1` with optional `domain:` and `c2pa-url:`)
#
# Step 1 runs a local heuristic scan across .ts/.tsx/.js/.jsx/.mjs/.cjs/.mts/.cts/.py
# files for unwrapped OpenAI, Anthropic, Google GenAI, Vercel AI SDK, Mastra,
# OpenAI Agents, LangChain, and LangGraph calls.
#   Exit codes: 0 no unwrapped calls · 1 unwrapped calls found (fails the PR check) · 2 invalid workspace root
#
# Step 2 runs `npx --yes spurvik@1.0.18 audit .`, a local audit of OpenAI,
# Anthropic, Gemini, LangChain and Vercel AI SDK calls in this repository.
#
# Step 3 (optional) runs `npx --yes spurvik@1.0.18 scan "$SPURVIK_SCAN_DOMAIN" --fail-on-gap`,
# a live Art. 50 check of your public site (AI chat widgets, CMS/GTM stack and a visible Art. 50(1) notice),
# and appends a live Art. 50 status shield & shareable DPO report link to $GITHUB_STEP_SUMMARY.
# Turn it on by setting the repository variable SPURVIK_SCAN_DOMAIN (for example folk.app) under
# Settings → Secrets and variables → Actions → Variables. It sends only that public domain to
# console.spurvik.com. Leave the variable unset to skip the step.
#
# Step 4 (optional, ahead of the 2 December 2026 Art. 50(2) provenance date) runs
# `npx --yes spurvik@1.0.18 c2pa "$SPURVIK_C2PA_URL" --fail-on-missing`, checking a public https
# hero/product image URL (or bare store domain in 1 hop) for an embedded C2PA JUMBF manifest,
# signature integrity, and CDN optimizer stripping (/_next/image, Cloudflare Polish, Shopify CDN,
# Jetpack Photon, Webflow CDN, Cloudinary, imgix). --fail-on-missing exits 2 when the manifest is
# missing or its signature does not match. Turn it on via repository variable SPURVIK_C2PA_URL.
#
# Scope note: steps 1 and 2 are heuristic pattern scans (the scans themselves make zero network
# calls; npm fetches the packages if they are not already installed). Not a full AST trace, not
# legal advice, and does not classify riskTier or mark content.
# Guides: https://spurvik.com/cursor-claude-mcp-eu-ai-act · https://spurvik.com/eu-ai-act-2-december-2026-c2pa-provenance

name: Spurvik AI Call Check

on:
  pull_request:
  push:
    branches: [main]

permissions:
  contents: read

jobs:
  spurvik-check:
    name: Check wrapped LLM & agent calls
    runs-on: ubuntu-latest
    env:
      # Repository variables, read through env so they never land inside a run script.
      SPURVIK_SCAN_DOMAIN: ${{ vars.SPURVIK_SCAN_DOMAIN }}
      SPURVIK_C2PA_URL: ${{ vars.SPURVIK_C2PA_URL }}
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

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

      - name: Run Spurvik unwrapped-call scan (scan makes zero network calls)
        run: |
          if [ -x ./node_modules/.bin/spurvik-mcp ]; then
            ./node_modules/.bin/spurvik-mcp check . --ci
          else
            npm exec --yes --package=spurvik-mcp-server@1.0.15 -- spurvik-mcp check . --ci
          fi

      - name: Audit local AI SDK calls (spurvik audit)
        run: npx --yes spurvik@1.0.18 audit .

      - name: Run offline 6-vector Art. 14 gate self-test (0 network calls)
        run: npx --yes spurvik@1.0.18 gate "${SPURVIK_SCAN_DOMAIN:-example.com}"

      - name: Live domain Art. 50 scan (only when SPURVIK_SCAN_DOMAIN is set)
        if: ${{ env.SPURVIK_SCAN_DOMAIN != '' }}
        run: npx --yes spurvik@1.0.18 scan "$SPURVIK_SCAN_DOMAIN" --fail-on-gap

      - name: Live C2PA manifest & CDN-strip check (only when SPURVIK_C2PA_URL is set)
        if: ${{ env.SPURVIK_C2PA_URL != '' }}
        run: npx --yes spurvik@1.0.18 c2pa "$SPURVIK_C2PA_URL" --fail-on-missing

      - name: Publish Art. 50 status shield & DPO brief to PR summary
        if: ${{ always() }}
        run: |
          node -e '
            const fs = require("node:fs");
            const summaryPath = process.env.GITHUB_STEP_SUMMARY;
            if (!summaryPath) process.exit(0);
            const raw = (process.env.SPURVIK_SCAN_DOMAIN || "").trim().toLowerCase();
            const validDomain = /^[a-z0-9](?:[a-z0-9.-]{0,251}[a-z0-9])?$/.test(raw) && raw.includes(".");
            const lines = ["### Spurvik EU AI Act PR Check", ""];
            if (validDomain) {
              const enc = encodeURIComponent(raw);
              const shield = `[![EU AI Act Art. 50](https://console.spurvik.com/api/v1/badge/status.svg?domain=${enc})](https://console.spurvik.com/report?domain=${enc})`;
              const c2paShield = `[![EU AI Act Art. 50(2) C2PA](https://console.spurvik.com/api/v1/c2pa/status.svg?domain=${enc})](https://console.spurvik.com/c2pa?domain=${enc})`;
              const card = `[![EU AI Act Art. 50 Readiness Card for ${raw}](https://console.spurvik.com/api/v1/badge/card.svg?domain=${enc})](https://console.spurvik.com/report?domain=${enc})`;
              lines.push(`${shield} ${c2paShield} · [Shareable DPO Brief](https://console.spurvik.com/report?domain=${enc}) · [1-Page Markdown Brief (.md)](https://console.spurvik.com/api/v1/report/markdown?domain=${enc}&format=raw) · [ai-act.json](https://console.spurvik.com/api/v1/badge/ai-act.json?domain=${enc}) · [/trust Studio](https://spurvik.com/well-known-ai-act-json-trust-center?domain=${enc}) · [2 Dec 2026 C2PA Check](https://console.spurvik.com/c2pa?domain=${enc}) · [24-Lang Label Studio](https://console.spurvik.com/labels?domain=${enc})`, "", card);
            } else {
              lines.push("Local unwrapped-call & 6-vector Art. 14 gate checks ran offline (`0` bytes sent). Set repository variable `SPURVIK_SCAN_DOMAIN` to add your live [Art. 50 status shield & DPO brief](https://console.spurvik.com/sandbox) or explore [Spurvik Docs](https://spurvik.com/docs).");
            }
            lines.push("");
            fs.appendFileSync(summaryPath, lines.join("\n") + "\n");
          '
