Nous.Eval.Guard.Hygiene (nous v0.21.1)

Copy Markdown View Source

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

StatusLicences
:in_repoMIT, 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
:deniedanything 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.

Summary

Functions

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

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.

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

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.

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

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.

The licences allowed in the repository.

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

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

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-duplicate pairs {id_a, id_b, jaccard} (id_a < id_b) among classifier cases of the same surface.

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

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

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.

The split name as written in files.

Functions

assign_splits(cases, opts \\ [])

@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(cases)

@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(c)

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

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

dedup(cases)

@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(text)

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

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

group(cases, opts \\ [])

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(group, seed \\ "nous-guard-v1", fractions \\ [tune: 0.4, holdout: 0.3, test: 0.3])

@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()

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

The licences allowed in the repository.

jaccard(a, b)

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

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

licence_status(spdx)

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

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

licence_violations(cases, policy)

@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(cases, opts \\ [])

@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(cases)

@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(cases)

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

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

shingles(text)

@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(split)

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

The split name as written in files.