# `Nous.Eval.Guard.Fetch`
[🔗](https://github.com/nyo16/nous/blob/v0.21.1/lib/nous/eval/guard/fetch.ex#L1)

Downloads a registered guard dataset (`Nous.Eval.Guard.Registry`), checks
it and writes it as `nous.guard_case/1` JSONL. `mix
nous.eval.datasets.fetch` is the command-line front end.

Every step fails closed:

  1. **Resolve** the source against the compiled registry (string ids;
     nothing becomes an atom). `id@rev` is accepted for the pinned
     revision only.
  2. **Licence gate.** `:denied` sources are refused with the reason.
     `:terms` sources need `accept_terms: [id]` and the acceptance is
     recorded in the manifest. A gated Hugging Face source needs a token
     (`:hf_token_env`); Nous cannot accept a gate for you.
  3. **Download** only the registry's pinned URLs, each capped at its
     registered `:max_bytes` (cached by sha256 under
     `:filename.basedir(:user_cache, "nous")/eval/downloads`), and
     **verify the sha256 before parsing**: a mismatch deletes nothing of
     yours, keeps nothing it downloaded and is an error.
  4. **Parse** CSV / JSON / JSONL natively; parquet only with the optional
     `:duckdbex` dependency.
  5. **Adapt** rows to cases with the registry's adapter (literal label
     tables, provenance with revision and row).
  6. **Hygiene** (`Nous.Eval.Guard.Hygiene`): exact dedup (conflicting
     labels are an error), near-duplicate grouping, group-hash splits, the
     licence policy. PII findings are counted in the result (the in-repo
     validator is the gate for committed data).
  7. **Sample** (`:sample`), deterministically: per surface and label, the
     cases with the smallest `sha256(seed <> ":" <> id)`, in proportion.
  8. **Write** to `:out` (default the cache's `eval/datasets/<id>@<rev>`):
     one JSONL file per surface, a `nous.guard_dataset/1` manifest with
     file hashes, and a `NOTICE`.

Options: `:accept_terms` (list of source ids), `:hf_token_env` (default
`"HF_TOKEN"`), `:cache_dir`, `:out`, `:sample`, `:seed` (default
`"nous-guard-v1"`), `:fractions` (split shares, default
`Nous.Eval.Guard.Hygiene`'s), `:sources` (the registry to resolve against,
default `Nous.Eval.Guard.Registry.sources/0`) and `:http` - `(url, headers,
max_bytes -> {:ok, body} | {:error, reason})`, default a streaming `Req`
GET that aborts with `{:error, {:too_large, max_bytes}}` once the body
passes the file's registered `:max_bytes`. Only the resolved source's file
URLs are ever passed to it, and a body over `max_bytes` is refused before
it is hashed even if the function returns one.

# `result`

```elixir
@type result() :: %{
  source: String.t(),
  out: Path.t(),
  manifest: map(),
  cases: non_neg_integer(),
  pii_findings: non_neg_integer()
}
```

# `cache_dir`

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

The default cache root: `:filename.basedir(:user_cache, "nous")`.

# `run`

```elixir
@spec run(String.t(), keyword()) :: {:ok, result()} | {:error, String.t()}
```

Fetches `spec` (see the moduledoc). Returns `{:error, message}` on any failure.

---

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