name: E2E (real DeepSeek API)

# Real-API end-to-end suite (`pnpm run test:e2e`). Unlike ci.yml this job
# consumes the DEEPSEEK_API_KEY_EXTERNAL secret (mapped to the env var
# DEEPSEEK_API_KEY the tests read) and hits the external API at
# https://api.deepseek.com (DEEPSEEK_BASE_URL is pinned to it explicitly so a
# stray repo-root .env can't redirect the run).
#
# pull_request IS a trigger, but GitHub withholds repo secrets from BOTH forked
# PRs and Dependabot PRs (the latter are same-repo, fork==false, but still
# keyless). The job-level `if:` below skips the whole job for those untrusted
# PRs. The Dependabot test keys on the PR AUTHOR (pull_request.user.login), not
# github.actor (the run trigger) — a maintainer reopening a Dependabot PR would
# make github.actor human while the PR is still keyless. When the job runs, the
# secret is expected, so the preflight hard-fails if it's missing (a missing
# secret would otherwise make the self-skipping suite report a false green). A
# job skipped by a job-level `if:` reports as a SUCCESSFUL check, so this
# workflow is safe to use as a required status check if desired.
#
# SECURITY — NEVER change this trigger to `pull_request_target`. `pull_request`
# checks out and runs the PR's HEAD code WITHOUT secrets for forks, which is what
# keeps an untrusted fork from exfiltrating the key. `pull_request_target` runs
# in the BASE repo's context WITH secrets while still able to check out untrusted
# fork code — a textbook key-leak vector, especially once this repo is public.
# The fork/secret model and its public-repo implications are recorded in
# docs/rfc/implemented/testing/2026-06-19-real-api-e2e-ci.md.
#
# Note: scheduled triggers are auto-disabled after 60 days of repo inactivity;
# push/pull_request/workflow_dispatch act as backstops.
on:
  workflow_dispatch:
  push:
    branches: [main, master]
  pull_request:
  schedule:
    # 00:17 UTC nightly = 08:17 Asia/Shanghai — off the top-of-hour cron stampede.
    - cron: '17 0 * * *'

# Cancel a superseded PR run (it is on a stale commit); never cancel a
# push/schedule run — it is already producing the post-merge/nightly signal.
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: ${{ github.event_name == 'pull_request' }}

# Least privilege: this job only reads the repo to run tests.
permissions:
  contents: read

jobs:
  e2e:
    runs-on: ubuntu-latest
    name: e2e
    # Run on every trusted event. Skip untrusted PRs (forks + Dependabot) where
    # the secret is withheld — they would otherwise hard-fail the preflight.
    if: >-
      github.event_name != 'pull_request'
      || !(github.event.pull_request.head.repo.fork || github.event.pull_request.user.login == 'dependabot[bot]')
    # Bounded file parallelism (DSH_E2E_MAX_WORKERS), 120s/test, retry 2. 45m
    # still bounds retry storms against a slow API while the happy path fans out.
    timeout-minutes: 45
    steps:
      - uses: actions/checkout@v6

      - uses: actions/setup-node@v6
        with:
          node-version: 24

      - name: Enable corepack (pnpm)
        run: corepack enable

      - name: Resolve pnpm store path
        id: pnpm-store
        run: echo "path=$(pnpm store path --silent)" >> "$GITHUB_OUTPUT"

      - uses: actions/cache@v4
        with:
          path: ${{ steps.pnpm-store.outputs.path }}
          key: ${{ runner.os }}-node-24-pnpm-${{ hashFiles('pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-node-24-pnpm-

      - name: Install (immutable)
        run: pnpm install --frozen-lockfile

      # The with-key escalation e2e (examples/sandbox-acp-agent/tests/
      # escalation.e2e.ts) self-skips without a usable runner — without this
      # step it would never actually execute anywhere (CI had no bwrap, dev
      # macs run Seatbelt instead), which is exactly how a broken harness
      # composition once survived unseen. Same recipe as ci.yml's bwrap
      # steps; the userns knob is best-effort (absent on pre-24.04 kernels,
      # the probe decides).
      - name: Install bubblewrap (unrestrict userns)
        run: |
          sudo apt-get update -q
          sudo apt-get install -yq bubblewrap
          sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0 \
            || echo "apparmor userns knob absent — the functional probe decides"

      # Guard against a false green: the e2e suites self-skip when the key is
      # absent, so a missing/misconfigured secret would otherwise pass as
      # "all skipped". This job only runs on trusted events (the `if:` above
      # excludes keyless PRs), so the secret MUST be present — fail loudly if not.
      - name: Preflight (require DEEPSEEK_API_KEY)
        env:
          # Repo secret DEEPSEEK_API_KEY_EXTERNAL holds the external-API key;
          # the tests read process.env.DEEPSEEK_API_KEY, so map it across here.
          DEEPSEEK_API_KEY: ${{ secrets.DEEPSEEK_API_KEY_EXTERNAL }}
        run: |
          set -euo pipefail
          if [ -z "${DEEPSEEK_API_KEY:-}" ]; then
            echo "::error::DEEPSEEK_API_KEY is empty. The e2e suite would self-skip and"
            echo "::error::report a false green. Configure the repo secret DEEPSEEK_API_KEY_EXTERNAL."
            exit 1
          fi
          echo "DEEPSEEK_API_KEY present."

      # Real-API end-to-end tests only. The keyless gates (lint/typecheck/
      # coverage/snapshot/build/etc.) already run in ci.yml on every push/PR;
      # no need to repeat them or build first (tests run unbuilt via tsx).
      # DEEPSEEK_BASE_URL is pinned to the external API; the secret is scoped to
      # this step (and preflight) only — never exposed to checkout/setup/install.
      - name: E2E tests (real DeepSeek API)
        env:
          DEEPSEEK_API_KEY: ${{ secrets.DEEPSEEK_API_KEY_EXTERNAL }}
          DEEPSEEK_BASE_URL: https://api.deepseek.com
          DSH_E2E_MAX_WORKERS: 14
        run: pnpm run test:e2e
