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

Copy Markdown View Source

Decision-table checks for the deterministic guards: Nous.Permissions, Nous.Plugins.LoopGuard and hooks. No statistics: every row must match, and any mismatch fails.

Rows are nous.guard_case/1 cases of kind "permission", "loop_guard" or "hook" (Nous.Eval.Guard.Case). Policies and hooks are code, so a row names a fixture ("guard") the caller registers in Elixir; the case data never carries code.

fixtures = %{
  permission: %{"default" => Nous.Permissions.default_policy()},
  loop_guard: %{"default" => %{}},
  hook: %{"no_rm_rf" => [Nous.Hook.new(:pre_tool_use, handler: &MyHooks.no_rm_rf/2)]}
}

{:ok, ds} = Nous.Eval.Guard.Dataset.load("test/fixtures/guard/deterministic")
:ok = Nous.Eval.Guard.Deterministic.check(ds.cases, fixtures)

Rows

  • permission - input {"tool_name": string, "category": "read" | "write" | "execute" | "communicate" | "search" | null}; expected {"blocked": bool, "requires_approval": bool} from Nous.Permissions.blocked?/2 and requires_approval?/3. Fixture: a %Nous.Permissions.Policy{}.
  • loop_guard - input {"messages": [message]}, each {"role": "user" | "assistant" | "tool" | "system", "content"?: string, "tool_calls"?: [{"name": string, "arguments": object | string}], "loop_guard"?: integer} (loop_guard marks an advisory the plugin already injected); expected {"advisory_threshold": integer | null}: the threshold of the advisory before_request/3 appends to that transcript, or none. Fixture: a :loop_guard_config map.
  • hook - input {"event": string, "payload": object} (payload keys tool_name, tool_id, arguments, result, content, tool_calls, response); expected {"result": "allow" | "deny" | "modify"} plus optional "changes" (the {:modify, changes} map, string keys) from Nous.Hook.Runner.run/3 over a Nous.Hook.Registry of the fixture's hooks (so matchers and priorities apply as in a run). Fixture: a list of %Nous.Hook{}.

Summary

Functions

Runs every deterministic row of cases (classifier cases are ignored) against fixtures. Returns :ok, or {:error, mismatches} listing every row whose actual output differs, or that names an unregistered fixture or holds malformed input (both fail).

The actual output of one deterministic row, in the shape of its expected, or {:error, reason}.

Types

fixtures()

@type fixtures() :: %{
  optional(:permission) => %{
    required(String.t()) => Nous.Permissions.Policy.t()
  },
  optional(:loop_guard) => %{required(String.t()) => map()},
  optional(:hook) => %{required(String.t()) => [Nous.Hook.t()]}
}

mismatch()

@type mismatch() :: %{
  id: String.t(),
  expected: map(),
  actual: map() | {:error, term()}
}

Functions

check(cases, fixtures)

@spec check([Nous.Eval.Guard.Case.t()], fixtures()) :: :ok | {:error, [mismatch()]}

Runs every deterministic row of cases (classifier cases are ignored) against fixtures. Returns :ok, or {:error, mismatches} listing every row whose actual output differs, or that names an unregistered fixture or holds malformed input (both fail).

run(c, fixtures)

@spec run(Nous.Eval.Guard.Case.t(), fixtures()) :: map() | {:error, term()}

The actual output of one deterministic row, in the shape of its expected, or {:error, reason}.