name: CI

# The QUICKSTART requires Agent F's conformance suite to run in CI "from the day
# Agent A lands" — it is the architecture's tripwire. This workflow runs the
# whole workspace's checks plus the testkit's conformance suite on every push and
# pull request.

on:
  push:
    branches: [main, master]
  pull_request:
  schedule:
    # Nightly soak at 04:17 UTC.
    - cron: "17 4 * * *"

env:
  CARGO_TERM_COLOR: always
  RUSTFLAGS: -D warnings

jobs:
  check:
    name: fmt / clippy / test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install Rust (pinned MSRV)
        uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: "1.77"
          components: rustfmt, clippy

      - name: Cache cargo
        uses: Swatinem/rust-cache@v2

      - name: Formatting
        run: cargo fmt --all -- --check

      # The demo GUI crate (eframe/resvg) needs a newer toolchain than the pinned
      # MSRV, so it is excluded from these MSRV-pinned workspace commands and
      # checked on current stable in the `editor-gui` job below.
      - name: Clippy (all targets)
        run: cargo clippy --workspace --exclude epiphany-editor-gui --all-targets -- -D warnings

      - name: Test (workspace)
        run: cargo test --workspace --exclude epiphany-editor-gui --all-targets

      - name: Doc tests
        run: cargo test --workspace --exclude epiphany-editor-gui --doc

      # Deny rustdoc warnings (broken/private intra-doc links, etc.) across the
      # whole workspace. `RUSTFLAGS` does not cover rustdoc, so `RUSTDOCFLAGS` is
      # set explicitly.
      - name: Doc warnings (workspace)
        env:
          RUSTDOCFLAGS: -D warnings
        run: cargo doc --workspace --exclude epiphany-editor-gui --no-deps

  # The demo GUI crate, on current stable (its eframe/resvg tree requires a newer
  # toolchain than the workspace MSRV). Separate so a GUI build break is its own
  # attributable signal, not a regression in the core crates.
  editor-gui:
    name: editor GUI (current stable)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install Rust (current stable)
        uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy

      - name: Install GUI build dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y \
            libxcb-render0-dev libxcb-shape0-dev libxcb-xfixes0-dev \
            libxkbcommon-dev libssl-dev

      - name: Cache cargo
        uses: Swatinem/rust-cache@v2

      - name: Clippy (GUI)
        run: cargo clippy -p epiphany-editor-gui --all-targets -- -D warnings

      - name: Test (GUI)
        run: cargo test -p epiphany-editor-gui

  conformance:
    name: conformance suite (testkit)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable

      - name: Cache cargo
        uses: Swatinem/rust-cache@v2

      # The full conformance suite at scale 1: Agent A's 1,000,000-iteration
      # determinism gate, the round-trip corpus, the crash-recovery and
      # manifest-selection gates, convergence / reduction-determinism /
      # equivocation against the real epiphany-ops, and the layout round-trip.
      - name: Run conformance suite
        run: cargo run --release -p epiphany-testkit --example conformance_suite 1

      # The Chapter 10 performance-budget gates (Phase 2 worklist F1), in
      # release with reduced sampling (EPIPHANY_BENCH_QUICK): the 1K and 10K
      # reduction points and both bundle budgets must pass; known-pending
      # points are documented xfails inside the benches ("F surfaces, K
      # fixes"). The heaviest (50K) reduction point runs only in the nightly
      # soak below.
      - name: Performance budget gates (quick)
        env:
          EPIPHANY_BENCH_QUICK: "1"
        run: cargo bench -p epiphany-testkit

  # Track A, Agent H's merge gate (Phase 2). A discrete job so a spelling /
  # decomposition pre-pass regression is attributable to H, not buried in the
  # workspace test run. Asserts H's PHASE2_QUICKSTART acceptance criterion: the
  # representative-corpus eligibility taxonomy (F3) and the pre-pass harness (F4 —
  # determinism, spelling correctness, decomposition reconstruction, RespellPitch
  # precedence, non-vacuity). The same gate also runs inside the conformance
  # suite's stage [7b]; this job isolates it for fast, attributable feedback.
  prepass-harness:
    name: Agent H pre-pass gate (testkit)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable

      - name: Cache cargo
        uses: Swatinem/rust-cache@v2

      - name: H spelling + decomposition merge gate
        run: cargo test -p epiphany-testkit --test prepass

  # A longer soak, scheduled nightly, exploring a wider seed space at scale.
  soak:
    name: conformance soak (nightly)
    if: github.event_name == 'schedule'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - name: Run conformance soak
        run: cargo run --release -p epiphany-testkit --example conformance_suite 10
      # The full budget-gate run, including the minute-scale 50K reduction
      # point (a documented xfail until the O(n²) reducer fix lands).
      - name: Performance budget gates (full)
        run: cargo bench -p epiphany-testkit
