Skip to content

Shards and matrices

Goal: one result and one fingerprint per CI shard, each bound only to what that shard reads.

flowchart LR
  m([matrix]) --> s1[shard 1: aval run -f aval.shard-1.yaml] --> r1([result 1]):::muted
  m --> s2[shard 2: aval run -f aval.shard-2.yaml] --> r2([result 2]):::muted
  c[change under packages/app] -. reruns .-> s2

Patterns A and B need no new field: -f, scope and environment.env do it. Pattern C uses a TestSet matrix. Example: examples/uv-monorepo.

Pattern A: one contract per shard

Best reuse. Each shard has its own contract, side by side at the root:

# Shard 1 of 2: the core package's tests. It lives at the workspace root,
# beside aval.shard-2.yaml, because globs cannot leave the contract's
# directory and the uv workspace files are here. Run with
# `aval run -f aval.shard-1.yaml`.
apiVersion: aval.trianalab.io/v1
kind: Test
metadata:
  name: uv-monorepo-core
runner:
  command: ["uv", "run", "--locked", "--package", "core", "pytest", "packages/core", "--cov=core", "--cov-report=xml:coverage-shard-1.xml"]
scope:
  source: ["packages/core/**/*.py"]
  # uv checks the lock against every member's pyproject.toml.
  dependencies: [pyproject.toml, "packages/*/pyproject.toml", uv.lock, .python-version]
environment:
  toolchain: {python: "3.12", uv: "0.9.18"}
cacheability: hermetic
outputs: [coverage-shard-1.xml]

Shard 2 tests app, which imports core, so core is in its scope:

# Shard 2 of 2: the app package's tests. app imports core, so core's code is
# in scope here too; app's files are not in shard 1's scope.
apiVersion: aval.trianalab.io/v1
kind: Test
metadata:
  name: uv-monorepo-app
runner:
  command: ["uv", "run", "--locked", "--package", "app", "pytest", "packages/app", "--cov=app", "--cov-report=xml:coverage-shard-2.xml"]
scope:
  source: ["packages/app/**/*.py", "packages/core/src/**/*.py"]
  dependencies: [pyproject.toml, "packages/*/pyproject.toml", uv.lock, .python-version]
environment:
  toolchain: {python: "3.12", uv: "0.9.18"}
cacheability: hermetic
outputs: [coverage-shard-2.xml]

A change under packages/app reruns shard 2 only. A change to core or uv.lock reruns both.

Pattern B: one contract, shard variables

Simpler, less reuse: a change anywhere reruns every shard.

# One contract for every shard of examples/uv-monorepo. Each matrix job sets
# SHARD_INDEX and SHARD_TOTAL, so each shard gets its own fingerprint over
# the whole suite's scope. Splitting by index needs a pytest plugin such as
# pytest-shard, which the example does not lock. pytest-shard counts
# SHARD_INDEX from 0: set it to `${{ strategy.job-index }}` and SHARD_TOTAL
# to `${{ strategy.job-total }}`.
apiVersion: aval.trianalab.io/v1
kind: Test
metadata:
  name: uv-monorepo-tests
runner:
  command: ["sh", "-c", "uv run --locked --all-packages pytest packages --shard-id=$SHARD_INDEX --num-shards=$SHARD_TOTAL"]
scope:
  source: ["packages/**/*.py"]
  dependencies: [pyproject.toml, "packages/*/pyproject.toml", uv.lock, .python-version]
environment:
  toolchain: {python: "3.12", uv: "0.9.18"}
  # Leave one out and every shard shares one fingerprint: a false hit.
  env: [SHARD_INDEX, SHARD_TOTAL]
cacheability: hermetic

Declare both variables

Leave SHARD_INDEX or SHARD_TOTAL out of environment.env and shards share a fingerprint: shard 2 reuses shard 1's result, a false hit.

Pattern C: one TestSet, a matrix

One file, and each shard is its own test: matrix writes the shard into runner.command, so it is part of the fingerprinted argv and no variable can drift between the job that runs a shard and the job that pushes it. Reuse is the same as pattern B, since the shards share one scope. Rules: Many tests in one file.

# Every shard of examples/uv-monorepo in one TestSet. The matrix writes the
# shard into runner.command, so each shard has its own fingerprint and no
# variable can drift between the job that runs it and the one that pushes.
apiVersion: aval.trianalab.io/v1
kind: TestSet
defaults:
  scope:
    source: ["packages/**/*.py"]
    dependencies: [pyproject.toml, "packages/*/pyproject.toml", uv.lock, .python-version]
  environment:
    toolchain: {python: "3.12", uv: "0.9.18"}
  cacheability: hermetic
tests:
  - metadata: {name: "uv-monorepo-shard-{index}"}
    runner:
      command: [uv, run, --locked, --all-packages, pytest, packages, "--shard-id={index}", --num-shards=2]
    matrix: {index: ["0", "1"]}

Run one shard by its name; its result goes to .aval/<name>/, so shards never overwrite each other's outputs:

aval run --test uv-monorepo-shard-0

aval explain --all checks every shard and prints its fingerprint (Select a test).

For a runner that counts shards from 1, matrix: {shard: {count: 3}} with {shard}/{shard_count} in the command states the total once, so the shard list and the total cannot disagree.

One test per platform

matrix also substitutes into environment.toolchain values, so each platform is its own test with its own fingerprint:

# One test per platform. The matrix writes the platform into the toolchain,
# so each leg has its own fingerprint: unit-darwin never reuses unit-linux.
# The probe records a leg's result only on the platform it declares.
apiVersion: aval.trianalab.io/v1
kind: TestSet
defaults:
  scope:
    source: ["**/*.go"]
    dependencies: [go.mod]
  cacheability: hermetic
tests:
  - metadata: {name: "unit-{os}"}
    runner: {command: [go, test, ./...]}
    matrix: {os: [linux, darwin]}
    environment:
      toolchain: {platform: "{os}"}
      probe: {platform: [go, env, GOOS]}

unit-darwin never reuses a result of unit-linux. Run the leg of the machine you are on: the probe of the other leg does not match, so its result is not recorded. To reuse across platforms, do not declare platform.

The matrix

One job per shard, each with its own --result-file and coverage outputs:

# A two-shard matrix for this example as its own repository: copy it to
# .github/workflows/ and set REPO and POLICY. Each job runs one shard
# contract with its own --result; on main, reuses a verified result or runs
# the shard; for pull requests read POLICY and REPO from the default branch
# (docs/trust-model.md#take-the-policy-from-a-protected-branch). It uploads that shard's coverage, restored on reuse. Publishing
# needs the push step from Aval's "Use GitHub Actions" guide.
name: tests
on:
  push:
    branches: [main]
permissions:
  contents: read
jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        shard: [1, 2]
    env:
      REPO: ghcr.io/acme/uv-monorepo-aval
      POLICY: .github/aval/policy.yaml
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false
      - uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3
        with:
          version: "0.9.18"
          enable-cache: false
      - run: curl -fsSL https://aval.run/install.sh | bash -s -- --version v0.5.0
      - run: >-
          aval run -f aval.shard-${{ matrix.shard }}.yaml
          --result .aval/shard-${{ matrix.shard }}.json
          --registry "$REPO" --policy "$POLICY"
      - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
        with:
          name: coverage-shard-${{ matrix.shard }}
          path: coverage-shard-${{ matrix.shard }}.xml

The same job locally, with AVAL_REGISTRY and AVAL_POLICY exported as in the workflow:

aval run -f aval.shard-2.yaml --result-file .aval/shard-2.json

To publish, add the push step from Use GitHub Actions.

Several tests in one job

One job per test pays checkout, setup and the aval install each time. The run action takes several tests in one step instead:

  go:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@<sha> # pin by commit SHA
        with:
          persist-credentials: false
      - uses: TrianaLab/aval/integrations/github/run@<sha> # pin by commit SHA
        with:
          test: go.* lint

test takes names and glob patterns (*, ?, [...]) separated by spaces, commas or newlines; a pattern adds the tests it matches, in contract order, and exits 2 when it matches none. Each test gets its own lookup, verification, run and report, one after the other. After them all the step exits with the worst code: a failing test (exit 3) does not skip the rest, and 5 (aval SECURITY) is never downgraded. The outcome output is the worst outcome (verified only when every test is, so lookup-only skips setup only then); outcomes maps each test to its own, and the step summary has a line per test. On a push to the default branch the tests share one artifact aval-.<id>, which the publish action pushes test by test.

The tests share the job and its user: code one test runs can change what the next one reads, as with separate steps of one job. Keep tests that must not touch each other in separate jobs.