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

Dataset hygiene for guard cases (`Nous.Eval.Guard.Case`): exact and
near-duplicate detection, leakage-free group splits, licence policy, a PII
lint and canonical JSONL hashing.

## Canonical text and exact duplicates

`canonical_text/1` is the text a duplicate check compares, per surface:
`user_input` the text (plus attached documents), `tool_result` the result
(canonical JSON when structured), `tool_call` the canonical JSON of
`{tool name, arguments}`, `model_output` the content plus canonical
`tool_calls`. `dedup_key/1` hashes it after NFKC, lower-casing and
whitespace collapsing. The normalisation is for comparison only: stored
text stays byte-exact, because homoglyph and zero-width payloads are the
test. `dedup/1` keeps the lowest id of each key (the others are listed in
its `provenance.duplicates`) and refuses a key whose cases disagree on the
label.

## Near duplicates and groups

Two cases are near duplicates when the Jaccard similarity of their
5-shingle sets is at least `0.5`: word 5-grams, or character 5-grams for a
text under 8 words (word shingles degenerate on short prompts). Up to
`:minhash_above` cases per surface (default 5,000) every pair is compared;
above it a MinHash index (128 hashes in 32 bands of 4) proposes candidate
pairs and each is verified exactly. Near duplicates are kept, not deleted:
`group/2` joins them into one group (connected components over
near-duplicate edges, a shared `provenance.template_id`, a
`provenance.derived_from` link and a shared declared `group`), so they stay
as robustness signal without inflating n or leaking across splits.

## Splits

`assign_splits/2` gives every group one split. A group whose members
already declare one keeps it (two different declared splits are an
error); any other group is placed by `sha256(seed <> ":" <> group)`, so
adding cases never moves an existing group.

## Licences

| Status | Licences |
|---|---|
| `:in_repo` | MIT, Apache-2.0, BSD-2-Clause, BSD-3-Clause, CC0-1.0, CC-BY-4.0 |
| `:terms` (fetch only, `--accept-terms`) | ODC-BY-1.0, CC-BY-SA-4.0, `LicenseRef-MIT-use-restricted` |
| `:denied` | anything else (NC, AGPL, no licence, private) |

## PII lint

`pii_lint/1` flags e-mail addresses, phone numbers, Luhn-valid card
numbers, IBANs and secret-shaped strings (`sk-…`, `AKIA…`, JWTs) anywhere
in a case's input. A hit is allowed only on a case tagged `synthetic_pii`,
and for e-mail addresses and phone numbers only with reserved values
(`example.com`/`.net`/`.org`, `.example`, `.invalid`, `.test`,
`.localhost` domains; `555-01xx` numbers). It is a maintainer lint over
fixtures, not a runtime redactor.

# `assign_splits`

```elixir
@spec assign_splits([Nous.Eval.Guard.Case.t()], keyword()) ::
  {:ok, [Nous.Eval.Guard.Case.t()]}
  | {:error, {:split_conflict, String.t()} | {:invalid_option, atom()}}
```

Gives every classifier case its group's split (see the moduledoc).

Options: `:seed` (default `"nous-guard-v1"`), `:fractions` (keyword of
`tune`/`holdout`/`test` shares summing to 1, default `0.4/0.3/0.3`).

Returns `{:error, {:split_conflict, group}}` when a group's members
declare different splits.

# `canonical_jsonl`

```elixir
@spec canonical_jsonl([Nous.Eval.Guard.Case.t()]) :: String.t()
```

The canonical JSONL form of `cases`: records sorted by id, each
`Nous.Eval.CanonicalJSON` encoded (keys sorted), LF-terminated. Built-in
files are written in this form, so their bytes hash as `sha256/1` says.

# `canonical_text`

```elixir
@spec canonical_text(Nous.Eval.Guard.Case.t()) :: String.t()
```

The text duplicate checks compare for a classifier case (see the moduledoc).

# `dedup`

```elixir
@spec dedup([Nous.Eval.Guard.Case.t()]) ::
  {:ok, [Nous.Eval.Guard.Case.t()]} | {:error, [[String.t()]]}
```

Exact deduplication per surface (classifier cases only; others pass
through). Of each key the lowest id is kept and the others are recorded in
its `provenance.duplicates`.

Returns `{:error, conflicts}` when cases sharing a key disagree on the
label (label noise: send them to review, never pick one), with
`conflicts :: [[id]]`.

# `dedup_key`

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

`"sha256:<hex>"` of `text` after NFKC, lower-casing and collapsing
whitespace: equal keys are exact duplicates.

# `group`

```elixir
@spec group([Nous.Eval.Guard.Case.t()], keyword()) :: [Nous.Eval.Guard.Case.t()]
```

Assigns each classifier case the group of its connected component over
near-duplicate edges (`near_duplicates/2`, same options), a shared
`provenance.template_id`, `provenance.derived_from` links and a shared
declared `group`. A component is named after the smallest declared group
that is not a member's own id, else its smallest id.

# `hashed_split`

```elixir
@spec hashed_split(String.t(), String.t(), keyword()) :: Nous.Eval.Guard.Case.split()
```

The split `group` hashes to under `seed` and the given fractions (see
`assign_splits/2`).

# `in_repo_licences`

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

The licences allowed in the repository.

# `jaccard`

```elixir
@spec jaccard(MapSet.t(), MapSet.t()) :: float()
```

Jaccard similarity of two shingle sets (`1.0` for two empty sets).

# `licence_status`

```elixir
@spec licence_status(String.t()) :: :in_repo | :terms | :denied
```

The licence policy for an SPDX id: `:in_repo`, `:terms` or `:denied`.

# `licence_violations`

```elixir
@spec licence_violations([Nous.Eval.Guard.Case.t()], :in_repo | :fetch) :: [
  {String.t(), String.t()}
]
```

Cases whose `provenance.license` the policy does not allow, as
`[{id, license}]`. `:in_repo` allows only in-repo licences; `:fetch` also
allows `:terms` licences (whose acceptance the caller records).

# `near_duplicates`

```elixir
@spec near_duplicates([Nous.Eval.Guard.Case.t()], keyword()) :: [
  {String.t(), String.t(), float()}
]
```

Near-duplicate pairs `{id_a, id_b, jaccard}` (`id_a < id_b`) among
classifier cases of the same surface.

Options: `:threshold` (default `0.5`), `:minhash_above` (default `5_000`
cases per surface; above it candidates come from MinHash banding and are
then verified exactly).

# `pii_lint`

```elixir
@spec pii_lint([Nous.Eval.Guard.Case.t()]) :: [
  %{id: String.t(), kind: atom(), value: String.t()}
]
```

PII findings `%{id, kind, value}` over every string in each case's input
(see the moduledoc for what a case may carry).

# `sha256`

```elixir
@spec sha256([Nous.Eval.Guard.Case.t()]) :: String.t()
```

`"sha256:<hex>"` of `canonical_jsonl(cases)`.

# `shingles`

```elixir
@spec shingles(String.t()) :: MapSet.t(non_neg_integer())
```

The 5-shingle set of `text` (hashed): word 5-grams, or character 5-grams
when the text has fewer than 8 words. Normalised as `dedup_key/1`.

# `split_name`

```elixir
@spec split_name(Nous.Eval.Guard.Case.split()) :: String.t()
```

The split name as written in files.

---

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