Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 94 additions & 21 deletions .github/workflows/mutation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,90 @@ concurrency:
permissions:
contents: read

env:
PYTHONHASHSEED: "0"
TRAILMARK_REQUIRE_BINARY_TOOLS: "1"
TRAILMARK_LLVM_OBJDUMP: /usr/bin/llvm-objdump
TRAILMARK_LLVM_SYMBOLIZER: /usr/bin/llvm-symbolizer
TRAILMARK_LLVM_READOBJ: /usr/bin/llvm-readobj
TRAILMARK_GNU_OBJDUMP: /usr/bin/objdump
TRAILMARK_GNU_SYMBOLIZER: /usr/bin/addr2line

jobs:
scope:
name: Select mutation scope
runs-on: ubuntu-latest
outputs:
reviewed: ${{ steps.scope.outputs.reviewed }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
fetch-depth: 0
- name: Include the full campaign for runner changes
id: scope
env:
PR_BASE_SHA: ${{ github.event.pull_request.base.sha }}
run: |
reviewed=false
if [[ "$GITHUB_EVENT_NAME" == pull_request ]]; then
reviewed=true
changed=$(git diff --name-only "$PR_BASE_SHA" HEAD)
if grep -Eq '^(\.github/workflows/mutation\.yml$|tests/(run_mutations|mutation_[^/]+|test_mutation_[^/]+|conftest|parse_work|test_test_policy)\.py$|pyproject\.toml$|uv\.lock$)' <<< "$changed"; then
reviewed=false
fi
fi
echo "reviewed=$reviewed" >> "$GITHUB_OUTPUT"

baselines:
name: Mutation baseline and coverage
needs: scope
runs-on: ubuntu-latest
env:
BASELINE_SHARD: 1/1
MUTATION_REVIEWED: ${{ needs.scope.outputs.reviewed }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3
with:
version: "0.13.9"
- uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0
with:
python-version: "3.13"
- run: sudo apt-get update && sudo apt-get install -y llvm binutils-multiarch
- run: npm ci
- run: uv sync --locked --all-groups --extra binary
- name: Validate baseline and collect test coverage
run: |
scope=()
if [[ "$MUTATION_REVIEWED" == true ]]; then scope+=(--reviewed); fi
uv run --locked --extra binary python -m tests.run_mutations "${scope[@]}" --baseline-shard="$BASELINE_SHARD"
- name: Upload validated baselines
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: mutation-baseline
overwrite: true
path: |
coverage/gremlins/baseline-*.json
coverage/gremlins/coverage-plan.json
if-no-files-found: error

gremlins:
name: Mutation shard ${{ matrix.shard }}/8
name: Mutation shard ${{ matrix.shard }}/32
needs: [scope, baselines]
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
shard: [1, 2, 3, 4, 5, 6, 7, 8]
shard: [
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
]
env:
MUTATION_SHARD: ${{ matrix.shard }}/8
TRAILMARK_REQUIRE_BINARY_TOOLS: "1"
TRAILMARK_LLVM_OBJDUMP: /usr/bin/llvm-objdump
TRAILMARK_LLVM_SYMBOLIZER: /usr/bin/llvm-symbolizer
TRAILMARK_LLVM_READOBJ: /usr/bin/llvm-readobj
TRAILMARK_GNU_OBJDUMP: /usr/bin/objdump
TRAILMARK_GNU_SYMBOLIZER: /usr/bin/addr2line
MUTATION_SHARD: ${{ matrix.shard }}/32
MUTATION_REVIEWED: ${{ needs.scope.outputs.reviewed }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
Expand All @@ -38,22 +106,21 @@ jobs:
- uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3
with:
version: "0.13.9"

- uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0
with:
python-version: "3.13"

- run: sudo apt-get update && sudo apt-get install -y llvm binutils-multiarch
- run: npm ci
- run: uv sync --locked --all-groups --extra binary

- name: Run reviewed mutation scope
if: github.event_name == 'pull_request'
run: uv run --locked --extra binary python -m tests.run_mutations --reviewed --workers=4 --shard="$MUTATION_SHARD"

- name: Run full mutation campaign
if: github.event_name != 'pull_request'
run: uv run --locked --extra binary python -m tests.run_mutations --workers=4 --shard="$MUTATION_SHARD"
- uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
with:
name: mutation-baseline
path: coverage/gremlins/baselines
- name: Run mutation campaign with shared baselines
run: |
scope=()
if [[ "$MUTATION_REVIEWED" == true ]]; then scope+=(--reviewed); fi
uv run --locked --extra binary python -m tests.run_mutations "${scope[@]}" --workers=4 --shard="$MUTATION_SHARD" --baselines=coverage/gremlins/baselines

- name: Enforce reviewed mutation baseline
run: uv run --locked --extra binary python -m tests.mutation_gate --shard="$MUTATION_SHARD"
Expand All @@ -69,16 +136,22 @@ jobs:
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: mutation-report-${{ github.sha }}-shard-${{ matrix.shard }}
overwrite: true
path: coverage/gremlins/
if-no-files-found: error

mutation-gate:
name: Mutation Testing
if: always()
needs: gremlins
needs: [scope, baselines, gremlins]
runs-on: ubuntu-latest
steps:
- name: Require all mutation shards to pass
env:
MUTATION_RESULT: ${{ needs.gremlins.result }}
run: test "$MUTATION_RESULT" = success
BASELINE_RESULT: ${{ needs.baselines.result }}
SCOPE_RESULT: ${{ needs.scope.result }}
run: |
test "$SCOPE_RESULT" = success
test "$BASELINE_RESULT" = success
test "$MUTATION_RESULT" = success
21 changes: 16 additions & 5 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,24 @@ uv run python -m tests.run_mutations --reviewed --workers=4
uv run python -m tests.mutation_gate
```

The runner uses real pytest subprocesses and validates clean and instrumented
baselines. It orders component unit tests first and retains all selected tests
as fallback. Use it rather than invoking the pinned plugin directly. Omit
The runner uses real pytest subprocesses and coverage-driven test selection.
It validates clean and instrumented baselines, expands shared-fixture coverage
to every consumer, and retains tests whose subprocesses or tracing obscure
coverage. Import-time and unmapped mutations run the full suite. Ordinary kills
require a passing neutral failure-prefix check or a matching validated full-suite
order. The control retains the full selected collection and fixture teardown. Use it
rather than invoking the pinned plugin directly. Omit
`--reviewed` for a full repository campaign; CI runs that after merges
and on manual dispatch. `--workers` already enables parallel execution.
CI additionally splits mutants across eight concurrent jobs. To reproduce a
partition, pass the same `--shard=1/8` (one-based index/total) to both commands.
CI produces one baseline receipt and coverage plan from a single instrumented
coverage pass. Its 32 mutation jobs reuse that evidence and execute mutants
without replaying full baselines or prevalidating selected subsets.
Local runs validate their own baselines by default. To reproduce a partition,
pass the same `--shard=1/32` (one-based index/total) to both commands.
Use `--test-selection=full` for a full-suite reference campaign. Shared coverage
preparation uses `--baseline-shard=1/1`; copy both `baseline-1-of-1.json` and
`coverage-plan.json` together when using `--baselines=PATH`.
See `CONTRIBUTING.md` for baseline sharing and failure-prefix confirmation.
Do not enable `TRAILMARK_UPDATE_SNAPSHOTS` during CI or mutation testing.

## Adding Features
Expand Down
84 changes: 64 additions & 20 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,66 +87,110 @@ Confirmed execution outcomes are streamed to `coverage/gremlins/progress.jsonl`
for investigation during long campaigns. This journal can be incomplete; only
the completed reports and `run.json` establish results for the gate.

CI runs eight mutation shards concurrently, each with four workers. Mutation
CI runs 32 mutation shards concurrently, each with four workers. Mutation
jobs install the `binary` extra and GNU and LLVM tools, and set
`TRAILMARK_REQUIRE_BINARY_TOOLS=1` so missing native-image dependencies fail
instead of silently skipping their tests. Each file's
mutants are dealt round-robin across the shards, retaining the full selected test
suite for every mutant. Every shard checks the clean and instrumented baselines
and gates its exact share of the source inventory. The final `Mutation Testing`
check requires all eight shards to pass; reports are uploaded separately per shard.
instead of silently skipping their tests. Each file's mutants are dealt
round-robin across the shards. One preparation job runs the selected suite once
with neutral instrumentation and per-test coverage. The normal CI test jobs run
the clean suite.
Mutation jobs reuse its baseline receipt and coverage plan only when repository
inputs, Python packages, native tool binaries, relevant environment settings,
test collection, and runner configuration match. Missing, stale, or incomplete receipts fail the run.
The outer pytest session collects tests and dispatches mutations without replaying
the clean suite in each shard.
The mutation shards then execute their assigned mutants. The final
`Mutation Testing` check requires the baseline job and all 32 mutation shards to pass.
Each mutation shard enforces
the reviewed gate for its assigned inventory and uploads its own reports.

To reproduce one CI shard locally, pass the same one-based `--shard=INDEX/TOTAL`
to both commands (omit `--reviewed` for a full repository shard):

```bash
uv run python -m tests.run_mutations --reviewed --workers=4 --shard=1/8
uv run python -m tests.mutation_gate --shard=1/8
uv run python -m tests.run_mutations --reviewed --workers=4 --shard=1/32
uv run python -m tests.mutation_gate --shard=1/32
```

Omitting `--shard` runs and gates the complete campaign as before.
Local campaigns prepare their own coverage and baselines by default. For shared
preparation, run `--baseline-shard=1/1` with the same targets, tests, and test-order
setting. Copy `coverage/gremlins/baseline-1-of-1.json` and
`coverage/gremlins/coverage-plan.json` together into a directory and pass
`--baselines=PATH` to mutation runs. Preparation executes no mutants and creates
no gate report; evidence is written only after successful validation. Keep these
files within the same trusted campaign.

**Scope and gate.** Pull requests mutate the reviewed files named in
`tests/mutation_baseline.json`. Full
repository campaigns run on pushes to main and through the mutation workflow's
manual trigger. The reviewed scope gates all functions in those files, including
unchanged legacy functions. The gate rejects unreviewed
survivors, missing tests or results, errors, and timeouts. An exception
unchanged legacy functions. The gate rejects unreviewed survivors and incomplete
mutation inventories in that scope. Across the entire campaign, errors, timeouts,
pardons, and missing tests
fail the gate, including outside the reviewed scope. An exception
requires a reason and a fingerprint of the function and mutation; changing
that function requires reviewing its exceptions again.
Source hashes and mutation fingerprints use a canonical AST representation so
reviews remain valid across supported Python versions.
PRs that change the mutation runner, its controls, the mutation workflow, or
Python dependency configuration run the full campaign before merging.

**How the runner works.** The runner pins pytest-gremlins 1.14.3 and uses its
pytest subprocesses and import handling, including fixtures, parametrized tests,
package metadata, and instrumentation in spawned child processes. Trailmark
checks the clean baseline and each distinct instrumented test ordering before
mutating, skips the redundant coverage pre-scan, preserves exact parameter IDs,
and rejects skipped instrumentation. Tests use deterministic work bounds rather
package metadata, and instrumentation in spawned child processes. Coverage-driven
test selection runs only tests observed to execute the mutated line, with shared
fixture setup and teardown attributed to every consumer. Tests that run unknown
child processes or replace Python tracing are retained for every mutant. Known
native GNU/LLVM tools, C compilers, and the fixed `git -C ROOT rev-parse HEAD`
provenance query are recognized through fingerprints of their executable contents.
Other Git commands and Python or script wrappers remain opaque. Import-time and
unmapped locations use the full suite.

After mutant execution, ordinary test failures receive a neutral failure-prefix
check. The control collects the same selected tests, executes only through the
test that failed, and includes fixture teardown. Matching full-suite orders reuse
the passing baseline. For each remaining selected collection, one successful
longest prefix also validates its shorter failure prefixes. These controls use
the same worker budget after the mutant pool closes. A failing neutral prefix
invalidates the campaign before scores are accepted. Survivors require no extra
test executions. The gate verifies each reported test selection against the saved plan.
The runner preserves exact parameter IDs and rejects skipped instrumentation.
Tests use deterministic work bounds rather
than wall-clock thresholds. The test suite has no elapsed-time assertions,
pytest timeout markers, or test subprocess deadlines. Hypothesis deadlines and
its generation-speed health check are disabled. Test product timeout behavior
its generation-speed health check are disabled. Mutation campaigns also use
deterministic Hypothesis examples with its example database disabled, so coverage
collection and mutant execution use the same generated inputs. Test product timeout behavior
with a simulated clock and process; keep optional wall-clock measurements in
`tests/benchmark_analysis.py` or `tests/smoke_binary_projects.py`.
The adapter uses private plugin APIs; its control tests must pass before upgrading
the plugin.

Collection-time `SystemExit` counts as a kill only when its traceback includes
an instrumented application source and the unmutated test selection loads
successfully. Harness failures and unrelated internal errors remain errors.

Each mutant subprocess may run for 240 seconds so the full fallback suite can
finish with four workers competing for CI resources. A timeout is not counted
as a kill unless the unmutated confirmation run establishes sufficient headroom;
inconclusive outcomes still fail the gate.

**Test ordering.** Each mutant keeps the full selected test suite: component
unit tests run first, followed by the remaining tests, stopping at the first
failure. Shared-helper mutants retain tests from every language. Tests stay in
**Test ordering.** Within a coverage-selected subset, component unit tests run first,
stopping at the first failure. Shared-helper mutants retain tests from every
language that covers the mutation. Full-suite fallbacks keep collection order so
they can reuse the shared neutral baseline. Tests stay in
their existing files; `tests/mutation_order.py` defines the ordering, and
unknown components keep collection order. Use `--test-order=collection` to
compare against pytest's original order.
keep pytest's original order within each subset. Use `--test-selection=full` to
run every selected test against every mutant as a reference. Full mode validates
its distinct complete orders; `--baseline-shard=INDEX/TOTAL` can split those checks.

Private helpers use their component test names without the leading underscore;
Python decorator helpers start with `test_python_decorator_bindings.py`.
Snapshot and binary model mutants start with `test_snapshots.py` and
`test_binary_models.py`, respectively, before falling back to general model tests.
`test_binary_models.py`, respectively, when those files are selected, followed by
any selected general model tests.
Snapshot mutants also prioritize binary and proxy round trips. Callable-reference
mutants prioritize binding-boundary and PHP callable tests; shared parser mutants
prioritize directory-linking regressions after their utility tests.
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ select = [
"ANN", "S", "B", "C4", "SIM",
]

[tool.ruff.lint.isort]
known-third-party = ["coverage"]

[tool.ruff.lint.per-file-ignores]
"tests/**" = ["S101", "ANN401"]

Expand Down
10 changes: 9 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@
from hypothesis import HealthCheck, settings

settings.register_profile("trailmark", deadline=None, suppress_health_check=[HealthCheck.too_slow])
settings.load_profile("trailmark")
settings.register_profile(
"trailmark-mutation",
parent=settings.get_profile("trailmark"),
derandomize=True,
database=None,
)
settings.load_profile(
"trailmark-mutation" if os.environ.get("TRAILMARK_MUTATION_TESTING") else "trailmark"
)


def pytest_sessionstart(session: pytest.Session) -> None:
Expand Down
Loading
Loading