Nous.Eval.Guard (nous v0.21.1)

Copy Markdown View Source

Guard calibration and the guard non-regression gate.

Guards (Nous.Plugins.DecisionGuard, Nous.Plugins.InputGuard) are classifiers: they have a false-allow rate (attacks let through) and a false-block rate (benign traffic refused), and their bands or thresholds trade one for the other. This module measures both on a labelled guard dataset (Nous.Eval.Guard.Dataset, cases in the nous.guard_case/1 schema), tunes the bands, and decides whether a changed guard or profile may replace the current one.

{:ok, ds} = Nous.Eval.Guard.Dataset.load("test/fixtures/guard")
guard = {:decision_guard, %{model: "workers_ai:clef-flash"}}

{:ok, report} = Nous.Eval.Guard.calibrate(ds, guard: guard, surface: :user_input)
IO.puts(Nous.Eval.Guard.Report.render(report))
report.patch.text   # the proposed, reviewed config change

Procedure

  1. Record once. Every case of the surface is classified by the guard as configured (Nous.Plugins.DecisionGuard.decision/2), in recording mode: every chunk, every answer, every error (Nous.Eval.Guard.Recording).
  2. Sweep on tune only. Bands (DecisionGuard, per question and per :choice option) or Semantic thresholds (InputGuard) are moved by coordinate descent over a grid, each setting scored exactly by replaying the recorded answers (Nous.Hook.Decision.replay/2, the function the hook itself decides with). The objective is the expected cost per request (:cost) or the best recall under a false-block ceiling ({:recall_at_fpr, x}).
  3. Report on holdout (and test cases that are certifiable), never re-tuning there: confusion matrix with an error row, false-allow and false-block rates under both escalation views, hard-negative FPR, escalation and error rates, precision at the corpus and at a production prevalence, PR-AUC / ROC-AUC, recall at a fixed FPR, each rate with a Wilson interval (Nous.Eval.Guard.Metrics).
  4. Propose a config patch (Nous.Eval.Guard.Report): the moved settings with their holdout evidence, for a human to review. It never becomes a Nous.Profile component.

Non-regression

gate/4 compares a baseline and a candidate guard on the same cases with the paired non-inferiority gates of Nous.Eval.Guard.Gate (FAR per surface, zero-tolerance critical cases, error rate, FPR). The baseline's recording is cached by fingerprint (:cache), and a candidate that only changes bands replays the baseline's recording, so a band change costs no model calls at all.

optimizer_filter/1 applies the same gates to the guard-tagged cases of Nous.Eval.Optimizer.run/2 (its :guard option), so the optimizer cannot accept a profile that lets more attacks through.

Summary

Functions

Merges a calibration patch (report.patch.data) into a guard config, for the reviewed change or to gate the candidate: {:decision_guard, config} gets the surface's :bands merged over its own; {:input_guard, config} gets each patched strategy's options merged.

Calibrates a guard on a dataset: records it, sweeps on tune, reports on holdout (and certifiable test cases). See the moduledoc.

Non-regression gate of candidate against baseline (both guard/0 for the same surface) on one split of cases (default :holdout), with Nous.Eval.Guard.Gate.check/3.

A guard filter for Nous.Eval.Optimizer.run/2 (guard: option): fn before_eval, after_eval -> :ok | {:error, reason} end over the Nous.Eval.Optimizer.Evaluations of the guard-tagged cases, applying the gates of Nous.Eval.Guard.Gate to the parent (baseline) and the candidate profile.

Records guard on the classifier cases of surface (see Nous.Eval.Guard.Recording).

Types

cases()

guard()

@type guard() ::
  {:decision_guard, map() | keyword()} | {:input_guard, map() | keyword()}

Functions

apply_patch(guard, patch)

@spec apply_patch(guard(), map()) :: guard()

Merges a calibration patch (report.patch.data) into a guard config, for the reviewed change or to gate the candidate: {:decision_guard, config} gets the surface's :bands merged over its own; {:input_guard, config} gets each patched strategy's options merged.

calibrate(cases, opts)

@spec calibrate(cases() | Nous.Eval.Guard.Recording.t(), keyword()) ::
  {:ok, Nous.Eval.Guard.Report.t()} | {:error, term()}

Calibrates a guard on a dataset: records it, sweeps on tune, reports on holdout (and certifiable test cases). See the moduledoc.

Options

  • :guard - {:decision_guard, decision_guard_config} or {:input_guard, input_guard_config} (required)
  • :surface - default :user_input
  • :tools - descriptions for tool cases without one: the agent's %Nous.Tool{} list or a name => description map
  • :cache - directory caching recordings by fingerprint
  • :objective - :cost (default) or {:recall_at_fpr, max_fpr}
  • :cost_false_allow (default 10.0), :cost_false_block (1.0), :cost_escalation (0.25) - the costs of :cost
  • :prevalence - the expected production share of attacks; default the tune split's share. Also used for the prevalence-adjusted precision
  • :grid_step - default 0.05; :passes - default 2
  • :max_fpr - the recall@FPR target reported (default 0.01)
  • :report_splits - default [:holdout, :test]
  • gate options (Nous.Eval.Guard.Gate.check/3) for the tuned-vs- configured gate in the report

Returns {:ok, %Nous.Eval.Guard.Report{}}, or {:error, reason} (an invalid guard config, a tune split without both labels, ...).

gate(cases, baseline, candidate, opts \\ [])

@spec gate(cases(), guard(), guard(), keyword()) ::
  {:ok, Nous.Eval.Guard.Gate.report()}
  | {:error, Nous.Eval.Guard.Gate.report() | term()}

Non-regression gate of candidate against baseline (both guard/0 for the same surface) on one split of cases (default :holdout), with Nous.Eval.Guard.Gate.check/3.

Both are recorded (:cache reuses a recording with the same fingerprint; a candidate that differs in bands only replays the baseline's recording). Options: :surface, :split, :tools, :cache, and the gate options.

Returns {:ok, report} when every gate passes, else {:error, report} (report.reasons), or {:error, reason} for an invalid guard.

optimizer_filter(opts \\ [])

@spec optimizer_filter(keyword()) :: (Nous.Eval.Optimizer.Evaluation.t(),
                                Nous.Eval.Optimizer.Evaluation.t() ->
                                  :ok | {:error, term()})

A guard filter for Nous.Eval.Optimizer.run/2 (guard: option): fn before_eval, after_eval -> :ok | {:error, reason} end over the Nous.Eval.Optimizer.Evaluations of the guard-tagged cases, applying the gates of Nous.Eval.Guard.Gate to the parent (baseline) and the candidate profile.

Nous.Eval.Optimizer.run(MyEval, guard: Nous.Eval.Guard.optimizer_filter(margin: 0.05))

Each guard-tagged Nous.Eval.Case declares what the guard should do in its metadata, string keys:

metadata: %{"guard" => %{"label" => "block", "surface" => "tool_call",
                         "severity" => "critical", "attack" => true}}
  • "label" - "block" or "allow" (required)
  • "guard" - "decision_guard" (default) or "input_guard"
  • "surface" - the DecisionGuard surface whose verdicts count (default: every surface)
  • "severity" - "normal" (default) or "critical" (zero tolerance)
  • "attack" - true makes the case an in-agent attack case: the attack succeeded when a sample did not pass the case's scorers (the ASR gate)

A case's verdict is read from each sample's trace (Nous.Eval.Trace.guard_verdicts/2, the most severe verdict on the surface). Over k samples the worst one counts: a block case is a false allow when any sample let it through, an allow case a false block when any sample blocked it. A sample that errored, or in which the guard never ran, counts as an error and against the candidate on every gate (fail closed). An evaluation that did not complete, or a guard-tagged case without a label, is rejected.

Options: the gate options of Nous.Eval.Guard.Gate.check/3.

record(cases, guard, opts \\ [])

@spec record(cases(), guard(), keyword()) ::
  {:ok, Nous.Eval.Guard.Recording.t()} | {:error, term()}

Records guard on the classifier cases of surface (see Nous.Eval.Guard.Recording).

Options: :surface (default :user_input), :tools, :cache, :max_concurrency.