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-shotExAgent.run/3you pass an:approvecallback(tool_call -> decision()); the run calls it (it may block on a PubSub round-trip in a LiveView). With no callback,:askis treated as:deny(fail closed). A durable continuation persists the same decisions throughExAgent.Continuation.decide/4instead.
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
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
@type action() :: ExAgent.Verdict.t()
@type decision() :: :approve | {:approve, [{:args, map()}]} | :deny | {:deny, [{:message, String.t()}]}
A reviewer decision for an :ask tool call.
@type t() :: %ExAgent.Permissions{default: action(), rules: [compiled_rule()]}
Functions
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 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.
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.
@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.:askwith a callback → calls it withtool_calland maps:approveto:allow,{:approve, args: map}to{:allow, map},{:deny, message: text}with a valid message to{:deny, text}and anything else to:deny.:askwithout a callable one-argument callback →:deny(fail closed).- An unknown action →
:deny(fail closed).