ExAgent.Permissions (ExAgent v3.0.0)

View Source

Per-tool admission control with :allow, :ask and :deny. Rules match tool names. Candidate argument rules also match globs or structured comparators on validated arguments.

Stability — Stable.

Rules are evaluated in order and the last matching rule wins (so put a catch-all * first, then more specific rules after it). Anything that matches no rule falls back to :default (:allow by default).

new!/1 rejects invalid configuration instead of silently granting access. :approve is a callback result, not a rule or default action. Only an explicit :allow decision, or :ask approved by its callback, authorizes execution.

  • :allow — run the tool.
  • :deny — never run it; the model receives a "permission denied" tool return so it can adapt.
  • :ask — require human approval. In a one-shot ExAgent.run/3 you pass an :approve callback (tool_call -> decision()); the run calls it (it may block on a PubSub round-trip in a LiveView). With no callback, :ask is treated as :deny (fail closed). A durable continuation persists the same decisions through ExAgent.Continuation.decide/4 instead.

A decision/0 is :approve, {:approve, args: map} (run the call with the reviewer's arguments, revalidated against the tool before it runs; the model's tool call in the history is not rewritten), :deny or {:deny, message: text} (the model receives a :denied tool return with the message, 1 to 4096 bytes of UTF-8, and continues). Any other callback result denies. Edited arguments that fail the tool's validation fail the run with {:tool_approval_failed, tool_name, {:invalid_args, errors}} and record the call as :not_executed.

During a run, if the approval callback raises, throws or exits, the run fails with {:tool_approval_failed, tool_name, cause} and records the tool as :not_executed. The callback runs within the call's admission, whose budget equals the tool's timeout: a slower decision stops the call before it runs, records it as :not_executed and fails the run with {:tool_admission_timeout, tool_name}. Use a durable :continuation for reviews that take longer.

Example

perms =
  ExAgent.Permissions.new!(
    rules: [{"*", :deny}, {"read", :allow}, {"search_*", :allow}, {"bash", :ask}],
    default: :deny
  )

ExAgent.Permissions.decide(perms, "bash")        #=> :ask
ExAgent.Permissions.decide(perms, "search_web")  #=> :allow
ExAgent.Permissions.decide(perms, "write")       #=> :deny

Summary

Types

A reviewer decision for an :ask tool call.

t()

Functions

Decide the action for a tool name with no arguments.

Decide the action for a tool name and its validated argument map. The last matching rule wins. Use the default when no rule matches.

Return the decision and the winning rule's reference. References use rule-<position>-<hash> with a one-based position and a lowercase SHA-256 hash of the rule. The reference is nil when no rule decided.

Build a permissions set from :rules and a :default action. A rule is {tool_glob, action} or {tool_glob, action, args: %{path => predicate}}. Use a string for a top-level argument key and a list of strings for a nested object path. All predicates must match. Rules use the JSON names of argument keys.

Resolve an :ask decision through an optional approve callback.

Types

action()

@type action() :: ExAgent.Verdict.t()

decision()

@type decision() ::
  :approve
  | {:approve, [{:args, map()}]}
  | :deny
  | {:deny, [{:message, String.t()}]}

A reviewer decision for an :ask tool call.

t()

@type t() :: %ExAgent.Permissions{default: action(), rules: [compiled_rule()]}

Functions

decide(permissions, tool_name)

@spec decide(t(), String.t()) :: action()

Decide the action for a tool name with no arguments.

An unknown selected action in a manually constructed or modified struct is treated as :deny. Prefer new!/1 to reject invalid configuration up front.

decide(permissions, tool_name, args)

(candidate API)
@spec decide(t(), String.t(), map()) :: action()

Decide the action for a tool name and its validated argument map. The last matching rule wins. Use the default when no rule matches.

match_rule(permissions, tool_name, args)

(candidate API)
@spec match_rule(t(), String.t(), map()) :: {action(), String.t() | nil}

Return the decision and the winning rule's reference. References use rule-<position>-<hash> with a one-based position and a lowercase SHA-256 hash of the rule. The reference is nil when no rule decided.

new!(opts)

@spec new!(keyword()) :: t()

Build a permissions set from :rules and a :default action. A rule is {tool_glob, action} or {tool_glob, action, args: %{path => predicate}}. Use a string for a top-level argument key and a list of strings for a nested object path. All predicates must match. Rules use the JSON names of argument keys.

Argument globs match the entire string. * matches zero or more bytes and ? matches one byte, including line breaks. Use literal text to match a specific multibyte character. Tool-name globs also support * and ?.

Structured argument comparators are candidate variants.

  • {:command, prefix} compares complete argv words. Supply an argv list or literal words separated by spaces or tabs. Shell syntax is rejected.
  • {:url, prefix} compares the normalized HTTP(S) origin and path segments. The origin includes the effective port. The prefix has no query or fragment. DNS and IPv4 hosts require {:idna, "~> 8.0"}.
  • {:path, directory} expands the directory at construction and compares normalized absolute path components. Resolution of symlinks and filesystem aliases belongs to the callable.

URL and path arguments can be nonempty lists. An :allow or :ask rule requires every member to match and a :deny rule needs any member to match. A missing operand makes the rule inapplicable. A supplied malformed operand of a comparator for the tool denies the call.

Raises ArgumentError for unknown or duplicate options, malformed rules, or actions other than :allow, :ask and :deny. The message names the option, never the rule or action given.

resolve(arg1, tool_call, approve)

@spec resolve(action(), term(), (term() -> decision() | term()) | nil) ::
  :allow | {:allow, map()} | :deny | {:deny, String.t()}

Resolve an :ask decision through an optional approve callback.

  • :allow → :allow.
  • :deny → :deny.
  • :ask with a callback → calls it with tool_call and maps :approve to :allow, {:approve, args: map} to {:allow, map}, {:deny, message: text} with a valid message to {:deny, text} and anything else to :deny.
  • :ask without a callable one-argument callback → :deny (fail closed).
  • An unknown action → :deny (fail closed).