Packages

Fast mutation testing for Elixir, built for people and agents.

Current section

Files

Jump to
juggernaut README.md
Raw

README.md

<h1 align="center">Juggernaut</h1>

<p align="center">
  <img src="assets/juggernaut.png" width="280" alt="Juggernaut logo: a dark armoured helmet with a glowing purple Elixir droplet on its brow">
</p>

<p align="center">
  <b>Fast mutation testing for Elixir, built for people and for coding agents.</b>
</p>

<p align="center">
  <a href="https://hex.pm/packages/juggernaut"><img src="https://img.shields.io/hexpm/v/juggernaut?color=7B2FBE" alt="Hex version"></a>
  <a href="https://hexdocs.pm/juggernaut"><img src="https://img.shields.io/badge/docs-hexdocs-7B2FBE" alt="Documentation on HexDocs"></a>
  <img src="https://img.shields.io/badge/elixir-1.20-4B275F" alt="Elixir 1.20">
  <img src="https://img.shields.io/badge/OTP-29-4B275F" alt="OTP 29">
  <a href="https://github.com/holsee/juggernaut/blob/main/LICENSE"><img src="https://img.shields.io/badge/licence-MIT-4B275F" alt="MIT licence"></a>
</p>

<p align="center">
  <a href="#quick-start">Quick start</a> ·
  <a href="#why-juggernaut">Why Juggernaut</a> ·
  <a href="#for-coding-agents">For coding agents</a> ·
  <a href="#the-dashboard">Dashboard</a> ·
  <a href="https://hexdocs.pm/juggernaut">Docs</a>
</p>

<picture>
  <source media="(prefers-color-scheme: dark)" srcset="assets/dashboard-dark.png">
  <img src="assets/dashboard-light.png" alt="The Juggernaut dashboard: a gate run's verdict, mutant counts, scores, wall time and the score of each changed file against its threshold">
</picture>

Juggernaut changes your code in small ways (a `>` becomes `>=`, a `true` becomes `false`,
a clause is dropped) and checks that your tests notice. Each change is a *mutant*; a
mutant your tests fail on is *killed*, one they pass with has *survived*: a place where
the tests do not pin the behaviour down. Coverage tells you a line ran; mutation testing
tells you whether a test would notice if it were wrong.

## Why Juggernaut

- **11.7x faster than Mutare** on a cold gate of ten core files (3,899 s to 332 s), with
  the same 2,052 mutants killed. It compiles every mutant of a file once, switched on and
  off at run time, keeps a warm pool of test nodes instead of starting `mix test` per
  mutant, and runs only the tests that reach each mutant.
- **6.4x faster to a first verdict on a one-line change** (177 s to 28 s, measured at 0.2):
  `--since` mutates only the clauses that changed.
- **The same verdicts.** Paired mutant by mutant against Mutare on 2,002 mutants: no
  blocking differences.
- **Reuses what has not changed.** A second run of the same commit reused all 1,732
  results and halved the wall time; the warm server keeps the pool booted between runs.
- **Built for coding agents.** `--format agent` is one compact JSON document with each
  survivor's diff, source, the tests that ran and the command that re-checks it.
  `mix juggernaut.install --agents` writes the agent's instructions and skill.
- **Shows its working.** Every run is recorded: what changed, where the time went and how
  much reuse saved, each figure labelled as measured or recorded. The dashboard is one
  HTML file that makes no network requests.

It is the mutation gate of three Elixir and Phoenix projects, and of Juggernaut itself
(1,786 of 1,786 mutants killed). Every figure above comes from recorded runs in
[docs/benchmarks.md](https://github.com/holsee/juggernaut/blob/main/docs/benchmarks.md),
which also records the caveats: single runs on a shared host vary by about 5%.

## Quick start

```sh
mix igniter.install juggernaut   # or add the dependency by hand (below)
mix juggernaut                   # gate the functions changed against your base branch
mix juggernaut.explain ID        # one survivor in full, and why no test caught it
```

Write the test that tells the mutant from your code, then `mix juggernaut.check ID` to see
it killed. For a faster loop, `mix juggernaut --since HEAD~1` mutates only the clauses you
changed, and `mix juggernaut --watch` runs the inner lane on every save.

Status: `0.4.0`, pre-1.0. Elixir 1.20, OTP 29.

## Install

With [Igniter](https://hex.pm/packages/igniter) in the project:

```sh
mix igniter.install juggernaut
```

This adds `{:juggernaut, "~> 0.4", only: [:dev, :test], runtime: false}`, writes a
`.juggernaut.exs` (Ecto projects get the Ecto query macros as `raw_calls`), and ignores
`/_build/juggernaut/` unless `_build` is ignored already. Running it again changes
nothing. Options of `mix juggernaut.install`:

- `--agents` writes a marked section into `AGENTS.md` telling coding agents how to use
  Juggernaut (replaced in place on later runs), and the agent skill
  `.claude/skills/juggernaut/SKILL.md`: how to choose a lane, read the agent output,
  kill survivors, tell when a fresh boot or the integration tests are needed, and what an
  agent never does (written again when it differs from the installed version's).
- `--from-mutare` converts the call routes of an existing `.mutare.exs`.

Without Igniter, add the dependency to `mix.exs` by hand:

```elixir
def deps do
  [
    {:juggernaut, "~> 0.4", only: [:dev, :test], runtime: false}
  ]
end
```

Everything else works without Igniter: `.juggernaut.exs` is optional, and
`mix juggernaut.install --agents` writes the agent guidance. The rest of the installer
then says how to add Igniter and exits 2.

Upgrading from an earlier version, or using a read-only path dependency: see the
[upgrading guide](guides/upgrading.md).

## Configure

`.juggernaut.exs` is optional; every key has a default. The common ones:

```elixir
[
  paths: ["lib"],                                # what to mutate
  core: [MyApp.Billing, "lib/my_app/auth/**"],   # held to the core threshold
  thresholds: [core: 100, changed: 80, per: :file],
  base: "main",                                  # the git ref "changed" is measured against
  workers: :auto,                                # pool size
  timeouts: :killed,                             # :survived counts a kill only by timeout as a survivor
  no_coverage: :count,                           # :fail fails the gate on any mutant no test reaches
  raw_calls: [{Ecto.Query, :from}],              # calls whose arguments are never mutated
  env: [{"MIX_TEST_PARTITION", "${MIX_TEST_PARTITION}_jg{worker}"}],  # per worker
  cache: true
]
```

The file is validated: a wrong key or value names the key, its path and what was
expected, and exits 2. Each worker gets its own `MIX_TEST_PARTITION`, so a Phoenix or Ecto
project's test database is per worker; the steps of your `test` alias (such as
`ecto.create`, `ecto.migrate`) run once per worker before its first job. A run refuses to
start while credential-like variables (`*_API_KEY`, `*TOKEN*`, `AWS_*`, ...) are set,
printing only their names (`refuse_env`). Every key, with its default, is described in
the [configuration guide](guides/configuration.md).

To keep a mutant out on purpose, say why in a comment:

```elixir
# juggernaut:ignore[string:empty] the empty suffix is covered by the parser tests
def suffix(text), do: text <> "!"

# juggernaut:ignore-start[collection] the order of words does not matter here
...
# juggernaut:ignore-end
```

`mutare:ignore` comments are read too. New ignores are listed in every summary.

## Run

```sh
mix juggernaut                          # gate: functions in files changed vs base
mix juggernaut --full                   # every file under paths
mix juggernaut --files lib/my_app/billing.ex
mix juggernaut --since HEAD~3           # only the clauses changed since a ref
mix juggernaut --lane inner             # fast, advisory: changed clauses, fail fast, no control re-runs
mix juggernaut --lane release --full    # everything, nothing reused
mix juggernaut --dry-run --full         # list the mutants; run nothing
```

Lanes: `inner` for the edit loop (advisory, reads the cache), `gate` (the default: every
operator, kills re-checked without the mutant, core survivors re-checked on a fresh
worker, cache read and written on a clean committed tree), `release` (no reuse).

Other flags: `--fail-fast`, `--max-mutants N` and `--budget 5m` (what is left is reported
as not run, never dropped silently), `--workers N`, `--no-cache`, `--seed N`, `--page N`
and `--per-page N`, `--output PATH`, `--strict-ignores`, `--no-server`. `mix help
juggernaut` lists them all; every task takes `--help`.

Output formats (`--format`, or `JUGGERNAUT_FORMAT`):

| Format | For |
| --- | --- |
| `human` | the terminal: a progress line, each survivor as it is found (diff, tests that ran, the command that re-checks it), then the summary |
| `agent` | one compact JSON document for coding agents (below) |
| `json` | the [mutation-testing-elements](https://github.com/stryker-mutator/mutation-testing-elements) report |
| `markdown` | pull requests and CI step summaries |
| `github` | GitHub Actions annotations on the survivors' lines |

`--stream` writes one JSON event per line as the run goes (with `--format agent` the last
line is the agent document). Machine formats write only their document to stdout.

### Exit codes

| Code | Meaning |
| --- | --- |
| 0 | the gate passed, or a read-only task succeeded |
| 1 | the gate failed, or the run is incomplete (mutants left unrun) |
| 2 | usage or configuration error: nothing was scored |
| 3 | infrastructure or harness error: never a verdict |

## Tasks

| Task | What it does |
| --- | --- |
| `mix juggernaut` | run and gate (above) |
| `mix juggernaut.check ID...` | run the named mutants again now, typically after adding a test; exit 0 when all are killed |
| `mix juggernaut.explain ID` | one mutant in full: diff, source, status and kill kind, the killing test or the tests that passed, why they were chosen, the nearest miss |
| `mix juggernaut.survivors` | a recorded run's survivors, grouped (`--group-by file\|module\|operator`), paged, `--core-only` |
| `mix juggernaut.history` | one line per run, newest first, with trends of score, mutants, wall time and reuse |
| `mix juggernaut.profile [RUN]` | where a run's time went: phases, job time by test selection, fallbacks, slow tests, workers, recycles, reuse |
| `mix juggernaut.compare A B` | two runs mutant by mutant; `--mutare REPORT.json` compares with a Mutare or Stryker report |
| `mix juggernaut.report` | writes the HTML dashboard (below) |
| `mix juggernaut.triage ID --equivalent --reason TEXT` | records that a survivor is equivalent; `--list`, `--revoke ID`, `--ack ID --as NAME` |
| `mix juggernaut.advise [RUN]` | which mutants need a fresh boot or the integration tests, test files that behave unlike their lane, the code seams that would make them cheaper; `--verify` runs boot-set mutants warm and fresh; `--warm-safe`, `--ack`, `--revoke`, `--declarations` manage warm-safe declarations |
| `mix juggernaut.server` | the warm server (below); `--status`, `--stop` |
| `mix juggernaut.install` | the Igniter installer (above) |

A mutant id looks like `Grant.check/2:relational:4be1c09`. Any task accepts the whole id,
its hash (`4be1c09`) or a unique prefix of four or more characters. `--run` takes a run
id, `latest` (the default) or `latest~N`.

Triage claims live in `.juggernaut.triage.json`, which you commit. A claim lapses when the
function it is about changes. A claim on core code needs a human's `--ack`; an
acknowledgement from an actor whose name starts with `agent` is refused.

Code that runs while the app starts (the boot set) is judged on a fresh boot per mutant.
`mix juggernaut.advise` says which mutants need one, and how to declare a function
warm-safe: see [boot set, seams and warm-safe declarations](guides/boot-set.md), which
also covers tests that call `ExUnit.run/0` themselves.

## The warm server and `--watch`

```sh
mix juggernaut.server            # in one terminal; Ctrl-C, --stop or --idle-timeout ends it
mix juggernaut --files lib/a.ex  # elsewhere: attaches, same output and exit code
mix juggernaut --watch           # starts or attaches; the inner lane on every saved change
```

The server keeps one pool of workers between runs. The workers boot with the first run
and afterwards only load the code a later run's compile changed, so a run attached to a
warm server skips the pool boot (measured on the `plain_lib` fixture: 1.7 s cold, 2 ms
warm). Requests queue first in, first out, and a client waiting behind another is told
its position. The server holds the checkout's lock, so there is never a second pool: a
`--no-server` run while it is up exits 3. It listens on 127.0.0.1 only, and every request
carries the token in `_build/juggernaut/server/token`. Each request loads
`.juggernaut.exs` again and checks the client's variable names against `refuse_env`; a
change to `.juggernaut.exs`, `mix.lock`, `mix.exs`, anything under `config/` or a test
file gets a fresh pool, so an attached run always sees the same config as a standalone
one.

## For coding agents

Set `JUGGERNAUT_FORMAT=agent` (and `JUGGERNAUT_ACTOR=agent:<name>`, recorded with each
run). The loop:

1. `mix juggernaut --lane inner` after an edit (or keep `mix juggernaut --watch` open).
2. Read `summary` first: `verdict`, `exit_code`, `counts`, `failures` (each rule that
   failed, with file and threshold), `new_ignores`, `triage_claims`.
3. For each entry of `survivors`: `diff`, `excerpt` (the function's source), `tests_ran`
   (tests that ran with the mutant on and passed, with `file:line`), `nearest_miss`,
   `suggested_test_file`. Write the test that tells the two versions apart.
4. Run the survivor's `verify` command (`mix juggernaut.check <hash> --format agent`):
   exit 0 means the new test kills it. With a server running it answers from the warm
   pool.
5. If `page.more` is not 0, `page.next` is the command for the next page: nothing is cut
   silently. `not_scored` lists the mutants that could not be judged (compile or harness
   errors, mutants left unrun) with the reason.
6. `mix juggernaut.advise --format agent` when runs are slow or survivors sit behind
   `suite`, `file` or `boot` selections: its `seams` and `misclassified` say what to
   change; `next` holds the commands an agent may run, `for_a_person` those it must not.

Exit 3 is never a verdict about your tests: report it rather than change tests for it.
Do not add ignore comments or triage claims to make a gate pass without saying so; both
are listed in every summary for the people who review it.

Every key of the agent documents, the error document and the `--stream` events is
described in the [agent output guide](guides/agent-output.md).

## The dashboard

`mix juggernaut.report` writes `_build/juggernaut/report/index.html` (or `--output`): one
file with its styles, script and data inline that makes no network requests, so it can
be opened anywhere or attached to a CI build. Light and dark themes follow your system
(or the toggle), and it works on a phone.

- **Overview**: the verdict against each gate rule (as the run judged it), counts, kill
  kinds, survivors with the ones new since the previous run first, new ignores, triage
  claims, and trends of score, mutants, wall time and reuse over the history.
- **Code map**: files and their functions coloured by the share of survivors, core
  marked. Click a function to see its mutants.
- **Mutants**: filter by status, operator, file or function; the selected mutant's diff,
  status, kill kind, the tests that ran and its re-check command, and the source with a
  marker on each mutated line, opened at the mutant's line. Link to one with
  `index.html#inspector/<hash>`.
- **Tests**: tests that run in mutant jobs but never kill anything, and slow killers.
- **Performance**: phase clocks for this run and over the history, per-mutant job time by
  how its tests were chosen, fallbacks to a whole file or the suite and their causes,
  worker load, recycles and reuse.

Each figure says whether it was measured by the run or recorded by an earlier one.
Screenshots of every page and format, from real runs of the `genserver_app` fixture, are
in [docs/screenshots](https://github.com/holsee/juggernaut/tree/main/docs/screenshots).

## Data

Everything lives under `_build/juggernaut/`: `history/runs.ndjson` (one line per run:
commit, dirty-tree hash, config fingerprint, actor, phase clocks, counts), `runs/<id>/`
(every mutant and result of a run), `cache/` (reusable results), `coverage/`, `sandbox/`
(the mirror the mutants are compiled in; your working tree is never written) and
`server/`. Runs and phases emit `:telemetry` events under `[:juggernaut, ...]`
(`Juggernaut.Telemetry`).

## Licence

MIT; see [LICENSE](LICENSE). Code adapted from Mutare 0.5.1 keeps its notice in
[NOTICE](NOTICE).

## Development

To work on Juggernaut itself, see [CONTRIBUTING.md](https://github.com/holsee/juggernaut/blob/main/CONTRIBUTING.md).