# `Juggernaut.Engine`
[🔗](https://github.com/holsee/juggernaut/blob/v0.4.0/lib/juggernaut/engine.ex#L1)

One mutation run from end to end (design notes §10.1), and `check/1` of named mutants.

`run/1`, in timed phases (`Juggernaut.Telemetry.span_phase/3`, recorded as
`Juggernaut.Phase`s):

  0. `:prepare` - what comes before the run's span: the checkout read (commit and
     dirty hash), the cache's start (the triage file, the clean-tree check and the
     digest of the files cached results rest on, which reads every tracked file outside
     the code and tests) and the checkout lock. It is timed from the request, so it has
     no telemetry span; its events follow `run_started`;
  1. `:plan` and `:mutate` - the scope's files and their mutants
     (`Juggernaut.Plan`); ignore errors (and, under `strict_ignores`, warnings) stop
     the run with exit 2; ignored mutants are results already, and so are those
     whose triage claim applies (`equivalent`, `Juggernaut.Engine.Reuse`);
  2. `:compile` - the metamutants built and compiled in the sandbox
     (`Juggernaut.Schemata`); mutants that do not compile are `compile_error`;
  3. `:pool_boot` - the warm pool (`Juggernaut.Pool`), sized by the config and
     `--workers` within the connection budget, booted and its partitions proven;
  4. `:probe` - coverage (`Juggernaut.Coverage`, probing only what the index says is
     stale; the index is written back), the result cache (the jobs whose cached
     result stands are reused, `Juggernaut.Cache`), then the baseline (`Juggernaut.Pool.Baseline`)
     over every test a job will run (the whole suite when there are boot-lane
     mutants), which proves each worker and records the durations the schedule and
     the timeouts are calibrated from;
  5. `:mutants` - the jobs (`Juggernaut.Runner.Schedule`, `Juggernaut.Runner`), with
     control, and the reconfirmation of core survivors on a fresh worker, in the gate
     and release lanes; a boot-set mutant that tests run again after boot is tried
     here first (warm-first), never settled here: a kill only gives its tests a strike
     in the boot lane;
  6. `:boot_lane` - after the pool stops, each boot-set mutant on a fresh worker of its
     own (its strike's tests first), as many at once as
     `Juggernaut.Runner.BootLane.concurrency/2` allows, each slot with its own
     partition (with `spares`, a held worker per slot boots each job from a VM parked
     while the job before ran); the run records its boot set (`Run.boot_lane`);
  7. `:cache` - in a run that may write the result cache, the check that the tree is
     still clean at the commit the run started on (`git status`), then the entries of
     the jobs that ran;
  8. `:report` - the verdict (`Juggernaut.Verdict`); then the run files and the history
     line are written (`Juggernaut.Store`), after the run's clock stops.

The checkout lock is held for the whole run and refreshed every minute. A phase that
fails with an infrastructure error ends the run (exit 3); the run is still recorded,
with the phases it got through, the results it had, `summary.error` and verdict
`:error` (no cache entries), and the error's details name it (`run_id`).
`--budget` counts from the start of the run. The previous killer of each mutant comes
from the latest scored run.

`check/1` runs named mutants of the latest scored run again, the gate lane's way
(control on, no reconfirmation, no cache), without recording a run.

# `request`

```elixir
@type request() :: Juggernaut.Engine.Request.t()
```

What to run. Required: `:root`, `:config`, `:lane`, `:scope`. Optional: `:env`
(`System.get_env/0`), `:argv`, `:seed` (random), `:workers`, `:max_mutants`,
`:budget_ms`, `:fail_fast` (default: on in the inner lane only), `:strict_ignores`,
`:plan_opts` (`Juggernaut.Plan.resolve/3`), `:deps_path` (the project's
dependencies, default `deps`), `:worker_opts` (extra `Juggernaut.Pool.Worker`
options), `:git` (a git runner), `:on_event` (called with each run event of
`t:Juggernaut.View.Stream.event/0` as it happens: `run_started`, `phase_started`,
`phase_finished`, `plan`, `mutant`, `run_finished`), `:record?` (write the run files, default
true), `:reconfirm?` (reconfirm core survivors, default true), `:cache` (use the result
cache as the lane allows, default true; `Juggernaut.Cache.mode/3`), `:inventory_filter`
(narrows the inventory), `:warm_safe` (settle the boot-set mutants of functions declared
warm-safe on a warm worker, `Juggernaut.WarmSafe`; default true, never in the release
lane). Validated by `Juggernaut.Engine.Request`: a missing, unknown
or mistyped key is a usage error.

# `boot_share`

```elixir
@spec boot_share([map()], pos_integer() | nil, %{
  required(String.t()) =&gt; Juggernaut.Result.t()
}) ::
  {[map()], [Juggernaut.Result.t()]}
```

The boot-lane jobs that `--max-mutants` (`max`, or `nil`) still allows after the main
jobs' `results` (design notes §10.6: the boot lane gets what is left of N), and a
pending result for each one it does not, naming the user's N.

# `check`

```elixir
@spec check(request()) ::
  {:ok,
   %{
     mutants: [Juggernaut.Mutant.t()],
     results: %{required(String.t()) =&gt; Juggernaut.Result.t()},
     boot_lane: Juggernaut.Run.boot_lane() | nil
   }}
  | {:error, Juggernaut.Error.t()}
```

Runs the mutants named by `ids` (short ids, hash parts or unique prefixes) of the
latest scored run again, now. Required: `:root`, `:config`, `:ids`; optional as
`run/1`. Returns the named mutants as they are now, in the order named, their results,
and what was done with those of the boot set (`Run.boot_lane`, `nil` without one).

# `check_ignores`

```elixir
@spec check_ignores(Juggernaut.Plan.inventory(), boolean()) ::
  :ok | {:error, Juggernaut.Error.t()}
```

`:ok`, or the usage error listing ignore-comment errors and, when `strict?`, the
ignore warnings (a route that matched no call stays a warning).

# `killers`

```elixir
@spec killers(Path.t()) :: %{required(String.t()) =&gt; {module(), atom()}}
```

The previous killer of each mutant key, from the latest scored run under `store`
(`Runs.load_scored/1` skips a run that ended with exit 3; `%{}` when there is none).

# `pool_options`

```elixir
@spec pool_options(map(), non_neg_integer()) ::
  {:ok, keyword()} | {:error, Juggernaut.Error.t()}
```

The `Juggernaut.Pool.start_link/1` options of a run's request: the size within the
connection budget (at most `request.workers`), the worker environment and the sandbox.
The server starts its warm pool with them and `reserve` 1: its pool stays up through
the boot lane, which then has one peer's room left in the budget for its boot worker
(`Juggernaut.Runner.BootLane.concurrency/2`). A run's own pool stops before its boot
lane and reserves none.

# `run`

```elixir
@spec run(request()) :: {:ok, Juggernaut.Run.t()} | {:error, Juggernaut.Error.t()}
```

Runs mutation testing (see the module doc).

# `test_paths`

```elixir
@spec test_paths(Juggernaut.Config.t()) :: [String.t()]
```

The test paths of a config: `:project` is `test`, as in a Mix project.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
