Skip to main content

PolicyEngine

Struct PolicyEngine 

Source
pub struct PolicyEngine { /* private fields */ }
Expand description

Evaluates actions against registered policies.

Implementations§

Source§

impl PolicyEngine

Source

pub fn new() -> PolicyEngine

Source

pub fn register( &mut self, name: &str, check: Box<dyn Fn(&Action, &StateStore) -> Option<String> + Sync + Send>, description: &str, )

Source

pub fn register_tool_deny( &mut self, name: &str, tool: &str, check: Box<dyn Fn(&Action, &StateStore) -> Option<String> + Sync + Send>, description: &str, )

Self::register, plus the standing fact that check forbids tool outright — whatever the arguments.

Use this for a rule that denies a whole tool, so it can be read back by Self::blanket_denied_tools. The check itself is still the caller’s, deliberately: a file-loaded rule and a host-registered one word their refusal differently, and centralizing the closure here would make one of those messages wrong.

Registering a conditional check through this door mislabels it and will hide a tool that is only sometimes denied. There is no way to check that from here — the check is an opaque closure — so the contract is on the caller.

Source

pub fn check(&self, action: &Action, state: &StateStore) -> Vec<PolicyViolation>

Check an action against all policies.

If a policy check panics, the panic is caught and treated as a violation.

Source

pub fn unregister(&mut self, name: &str) -> usize

Remove every policy registered under name. Returns how many were dropped — 0 when nothing matched, so the caller can distinguish “removed” from “there was nothing by that name”.

Self::register appends without de-duplicating, so the same name can legitimately appear more than once; this removes all of them rather than leaving a shadowed copy still enforcing (Parslee-ai/car#623).

Source

pub fn clear(&mut self) -> usize

Drop every registered policy. Returns how many were removed.

Source

pub fn blanket_denied_tools(&self) -> BTreeSet<String>

The tools this engine forbids outright, whatever the arguments — the deny_tool rule kind, and only that kind.

For callers that assemble the tool list a model is shown. A tool no call can ever satisfy is worth removing from that list rather than advertising and then refusing: the refusal is correct but costs a turn and a schema’s worth of context on every request.

§Why only deny_tool

Not because the other kinds are “argument-dependent” — several of them can forbid a tool outright too:

  • allow_tool_param with an empty allow permits nothing.
  • rate_limit_tool with max_calls = 0 denies every call.
  • deny_tool_param_matching whose pattern fails to compile denies the whole tool, fail-closed.
  • deny_tool_param with neither equals nor contains denies on the parameter’s mere presence, which is total for a required parameter.

The property that actually separates them is whether totality is decidable from the rule kind alone, without reading the rule’s contents. deny_tool is the only kind where it is. The others are total only by inspection, and a caller assembling a tool list has no business inspecting them — so this deliberately under-reports rather than guess. Under-reporting is the safe direction: the tool is advertised and then refused, which is exactly the old behavior.

A narrow-but-not-total allow_tool_param is the case this leaves on the table. Hiding is the wrong remedy there (the tool is callable, just not that way); narrowing the advertised parameter schema to the permitted values is, and that is a separate change.

Populated by Self::register_tool_deny and emptied in step with Self::unregister and Self::clear, so it cannot name a tool the engine no longer blocks. A stale entry here would hide a tool that is actually allowed, which is the one failure mode worth engineering against.

Source

pub fn policy_names(&self) -> Vec<String>

Registered policy names, in registration order. May contain duplicates — see Self::unregister.

Source

pub fn policy_details(&self) -> Vec<(String, String)>

Registered policies as (name, description) pairs, in registration order. The description is what register was given.

Source

pub fn is_empty(&self) -> bool

Trait Implementations§

Source§

impl Default for PolicyEngine

Source§

fn default() -> PolicyEngine

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more