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

The per-mutant result cache (design notes §7.3, `_build/juggernaut/cache/results/`):
a mutant's result from an earlier run, reused while nothing that decided it changed.

One entry per canonical mutant key, holding the result as it was recorded, the commit
and run it came from, the hashes of the test files it rests on (`tests`) and its
footprint (`Juggernaut.Cache.Footprint`):

  * a `killed` entry rests on the killing test's file (every file of the job's tests
    when no single test is known, as for a deadline) and on every function the job
    executed with the mutant on, the mutated function included. It stands while every
    one of them hashes the same;
  * a `survived` entry rests on every test file whose tests ran and, like a kill, on
    every function its runs executed with the mutant on, the mutated function
    included: a caller that starts to use the mutated value can kill it with an
    unchanged test. It stands while they hash the same and the current coverage
    selects no test outside those files.

The whole cache is dropped when its `globals` differ (`globals/2`: the config
fingerprint, `mix.lock`, `config/`, test support and the helper, every file under
`priv/`), and every entry of a commit is dropped when the tests were weakened since it
(`Juggernaut.Cache.Weakening`) or git cannot diff against it.

Only `killed` (controlled, or the lane does not control) and `survived` results of
jobs on the warm pool are recorded: boot-lane, warm-first (a boot-set mutant's, even
one a warm-safe declaration settles), reused, pending, harness, compile-error and
ignored results never are, nor a kill or survivor whose footprint is not complete. A
no-coverage mutant costs no job, so it is decided by the probe on every run.

`mode/3`: the gate lane reads and writes, the inner lane only reads, the release lane,
`--no-cache` and `cache: false` neither read nor write.

# `entry`

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

# `lookup_ctx`

```elixir
@type lookup_ctx() :: %{
  root: Path.t(),
  tests: %{required(String.t()) =&gt; String.t()},
  inventory: map(),
  invalid_commits: MapSet.t(String.t())
}
```

What a lookup compares against: the project `root`, each test file's current `tests`
hash, the boot `inventory` (test id → `%{file:}`) and the commits whose entries cannot
stand (`invalid_commits`).

# `miss`

```elixir
@type miss() :: :new | :boot | :weakened | :code | :tests | :coverage
```

Why an entry did not stand: `:boot` for a warm-first job (selection `:warm_first`),
whose boot-set mutant the boot lane or a warm-safe declaration decides afresh
(design notes §10.8), whatever an entry says.

# `mode`

```elixir
@type mode() :: :read_write | :read | :off
```

# `record_ctx`

```elixir
@type record_ctx() :: %{
  root: Path.t(),
  tests: %{required(String.t()) =&gt; String.t()},
  run_id: String.t(),
  commit: String.t(),
  paths: [String.t()],
  controlled?: boolean()
}
```

What recording needs: the project `root`, the current `tests` hashes, the `run_id`,
the full `commit`, the source `paths` a footprint keeps, and whether the lane
`controlled?` its kills.

# `t`

```elixir
@type t() :: %Juggernaut.Cache{
  entries: %{required(String.t()) =&gt; entry()},
  globals: String.t() | nil
}
```

# `commits`

```elixir
@spec commits(t()) :: [String.t()]
```

The commits the entries of `cache` were recorded at.

# `data_digest`

```elixir
@spec data_digest(Path.t(), [String.t()]) :: String.t()
```

SHA-256 over each of `files` (relative to `root`) with the SHA-256 of its content, or
`"missing"` for a file that cannot be read: the files outside the code and tests that
results rest on, such as data a test reads at run time.

# `entries`

```elixir
@spec entries(
  [
    {Juggernaut.Mutant.t(), Juggernaut.Result.t(),
     [Juggernaut.Cache.Footprint.function_ref()] | nil}
  ],
  record_ctx()
) :: %{required(String.t()) =&gt; entry()}
```

The cache entries (by mutant key) of `items`, each `{mutant, result, functions}` as
for `entry/4`, leaving out those it skips. Every source file the footprints rest on is
read and parsed once for all of them (`Juggernaut.Cache.Footprint.scan/2`), not once
per entry: a run that records thousands of entries over the same files no longer
parses each file thousands of times.

# `entry`

```elixir
@spec entry(
  Juggernaut.Mutant.t(),
  Juggernaut.Result.t(),
  [Juggernaut.Cache.Footprint.function_ref()] | nil,
  record_ctx()
) :: {:ok, entry()} | :skip
```

The cache entry of `mutant`'s `result`, given the functions its job executed with the
mutant on (`{file, module, fun, arity}`, files relative to the project; `nil` when not
traced), or `:skip` when it is not recorded (see the module doc).

# `globals`

```elixir
@spec globals(Path.t(), String.t() | nil, String.t(), String.t()) :: String.t()
```

The cache's `globals`: SHA-256 over the config fingerprint, the coverage index's
globals (`mix.lock`, `config/`, every non-test file under the test paths;
`Juggernaut.Coverage.Index.current/4`), every file under `root`'s `priv/` and the
digest of the data files (`data_digest/2` of `cache_inputs`).

# `load`

```elixir
@spec load(Path.t(), String.t(), [String.t()] | :all) :: t()
```

The cache in the directory `path` for `globals`: the shards of `files` (the source
files of the mutants a run looks up; `:all` for every shard), each read only when it was
written under `globals` in this format. Empty (with those globals) when nothing
stands: a missing or unreadable directory, shards of another format or globals.

On disk (`Juggernaut.Cache.Shard`) the cache is sharded by the mutated source file,
one compact file per source file, under a directory named for the globals, so a run
reads only the shards of its mutants' files and writes only the shards it changed.

# `lookup`

```elixir
@spec lookup(t(), [map()], lookup_ctx()) :: %{
  reused: [{map(), Juggernaut.Result.t()}],
  misses: [map()],
  stats: map()
}
```

Splits `jobs` (`Juggernaut.Runner.Schedule` jobs: `mutant` and `tests`) into the
results the cache gives (`reused`, each `{job, result}` with `reused_from` set) and the
jobs still to run (`misses`), with `stats`: `hits`, `misses`, `recorded_ms` (the
recorded durations of the reused results) and `reasons` (misses by `t:miss/0`).

# `mode`

```elixir
@spec mode(Juggernaut.Run.lane(), boolean(), boolean()) :: mode()
```

How a run uses the cache: `:read_write`, `:read` or `:off` (see the module doc).

# `put`

```elixir
@spec put(t(), %{required(String.t()) =&gt; entry()}) :: t()
```

`cache` with `entries` (by mutant key) put in, replacing any earlier entry.

# `write`

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

Writes the shards of `files` (`:all`: every source file `cache` holds an entry of)
under `path`, each atomically with exactly `cache`'s entries of that file. Shards of
other files are left as they are, and shards written under other globals (and the
single-file cache of format 1, `results.json` beside `path`) are removed.

---

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