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

Warm-safe declarations (design notes §10.8, §7.5): the committed file
`.juggernaut.warm_safe.json`, in which a person says that the boot-set mutants of a
function may be settled on a warm worker instead of a fresh boot each.

A boot-set mutant's code runs while the app starts, so the boot lane gives it a fresh
worker of its own. A warm worker booted with the mutant off: state boot built from the
function (a value kept in `:persistent_term`, a parsed config) comes from the original
code while the tests call the mutated one, so a warm result can differ from a real
boot's. A declaration is the claim that it does not, for one function, and it must rest
on evidence: pairs of a warm result and a fresh boot's result for the same mutant
(`pairs/2` from a recorded run, `mix juggernaut.advise --verify` for a sample), every
one of which agreed, for the function as it is now (its hash, the hash a triage claim
rests on, `Juggernaut.Cache.Footprint`).

A declaration's state against the code now (`state/3`):

  * `:lapsed` - the function's hash changed (or it is gone): the mutants boot fresh;
  * `:unacked` - the function is core and no human has acknowledged the declaration:
    the mutants boot fresh;
  * `:applies` - in the inner and gate lanes, the function's boot-set mutants that tests
    run again after boot are settled on a warm worker. The release lane ignores every
    declaration and boots each boot-set mutant fresh.

Declaring and acknowledging are a person's decisions: both are refused when
`JUGGERNAUT_ACTOR` (or the `--as` name) starts with `agent`. A declaration made again
for the same function replaces the old one and drops its acknowledgement.

# `entry`

```elixir
@type entry() :: %{required(String.t()) =&gt; term()}
```

# `pair`

```elixir
@type pair() :: %{
  id: String.t(),
  function: String.t(),
  file: String.t(),
  hash: String.t(),
  warm: :killed | :survived,
  fresh: :killed | :survived,
  source: String.t()
}
```

One mutant run both ways: `warm` on a worker booted with it off, `fresh` on a boot with
it on from the start, each `:killed` or `:survived`; `hash` is the function's hash the
run saw, `source` the run (or verification) it came from.

# `state`

```elixir
@type state() :: :applies | :unacked | :lapsed
```

# `t`

```elixir
@type t() :: %Juggernaut.WarmSafe{entries: [entry()]}
```

# `ack`

```elixir
@spec ack(t(), String.t(), String.t(), String.t()) :: t()
```

`declarations` with the one on `function` acknowledged by `name` at `at`.

# `acked?`

```elixir
@spec acked?(entry()) :: boolean()
```

Whether a person has acknowledged `entry`.

# `declare`

```elixir
@spec declare(t(), map(), String.t(), String.t() | nil, String.t()) :: t()
```

`declarations` with `function` (a `Juggernaut.Cache.Footprint.key/3`) declared
warm-safe on `evidence` (`evidence/2`, which must allow it), for `reason`, by `by` at
`at`, resting on the function's current `hash`.

# `encode`

```elixir
@spec encode(t()) :: String.t()
```

The file's text: entries sorted by function, keys in a fixed order, 2-space indent.

# `evidence`

```elixir
@spec evidence([pair()], String.t(), String.t(), String.t() | nil, boolean()) :: map()
```

What `pairs` say about `function` at its current `hash` (`core?` when it is core): the
pairs at that hash, how many were killed and survived both ways, the mutants that
disagreed, the sources, and `allows?`, whether a declaration may rest on them (at least
one pair, none disagreeing).

# `file_name`

```elixir
@spec file_name() :: String.t()
```

The declarations file's name, relative to the project root.

# `find`

```elixir
@spec find(t(), String.t()) :: entry() | nil
```

The declaration on `function`, or `nil`.

# `function_key`

```elixir
@spec function_key(Juggernaut.Mutant.t()) :: String.t()
```

A mutant's function as declarations name it (`Juggernaut.Cache.Footprint.key/3`).

# `merge`

```elixir
@spec merge([pair()], [pair()]) :: [pair()]
```

The pairs of `older` and `newer` together, sorted by mutant id: a pair seen again (the
same mutant, function hash and outcomes) is kept once, with its newer source. Two
pairs of the same mutant at the same code with different outcomes are both kept: such
a mutant does not give the same result every time, and one of them disagrees.

# `name`

```elixir
@spec name(String.t()) :: String.t()
```

A function key's readable name: `"Elixir.A.B|f/1"` is `A.B.f/1`.

# `pairs`

```elixir
@spec pairs(Juggernaut.Run.t(), String.t()) :: [pair()]
```

The pairs a recorded `run` holds: each boot-set mutant that was tried on a warm worker
(`Run.boot_lane`'s `warm`) and then scored in the boot lane, both killed or survived.

# `pairs_of`

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

The pairs of `mutants` from their `results` and the `boot_lane` record (`Run.boot_lane`,
`nil` without one), as `pairs/2`.

# `parse_name`

```elixir
@spec parse_name(String.t()) :: String.t() | nil
```

The function key a person names: `A.B.f/1` (or the key itself). `nil` when it is not
of that form.

# `path`

```elixir
@spec path(Path.t()) :: Path.t()
```

The declarations file of the project at `root`.

# `read`

```elixir
@spec read(Path.t()) :: {:ok, t()} | {:error, Juggernaut.Error.t()}
```

The declarations of the project at `root`: none when there is no file.

# `read_verified`

```elixir
@spec read_verified(Path.t()) :: [pair()]
```

The pairs `--verify` recorded under `store` (none when there is no file).

# `revoke`

```elixir
@spec revoke(t(), String.t()) :: t()
```

`declarations` without the one on `function`.

# `settled`

```elixir
@spec settled(t(), Juggernaut.Run.lane(), [Juggernaut.Mutant.t()], %{
  required(String.t()) =&gt; Juggernaut.Cache.Footprint.hashes()
}) :: MapSet.t(String.t())
```

The functions (`Juggernaut.Cache.Footprint.key/3`) whose boot-set mutants a run in
`lane` settles on a warm worker: those of `declarations` that apply against `hashes`
(each mutated file's `Juggernaut.Cache.Footprint.file/1`), a function counting as core
when one of `mutants` in it is. None in the release lane.

# `state`

```elixir
@spec state(entry(), String.t() | nil, boolean()) :: state()
```

The state of `entry` given the function's current hash (`nil` when it is gone) and
whether it is core now (see the module doc).

# `verified_file`

```elixir
@spec verified_file(Path.t()) :: Path.t()
```

The evidence file of `mix juggernaut.advise --verify` under the store `store`.

# `write`

```elixir
@spec write(Path.t(), t()) :: :ok | {:error, Juggernaut.Error.t()}
```

Writes `declarations` to the project at `root` (sorted, pretty-printed, atomic).

# `write_verified`

```elixir
@spec write_verified(Path.t(), [pair()]) :: :ok | {:error, Juggernaut.Error.t()}
```

Records `pairs` under `store`, over those recorded before (`merge/2`).

---

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