Skip to main content

RuleEngine

Struct RuleEngine 

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

Refactor Wave-2 Tier-A2 (issue #850) — unified rule engine consumed by every governance entry point.

Before this refactor each of the three callsites that consult governance_rules (the substrate GOVERNANCE_PRE_WRITE hook, the wire_check agent-external hook, and the audited check_agent_action MCP / CLI surface) duplicated the rule-load + first-refusal-wins loop in its own function (check_agent_action, check_agent_action_no_audit, check_agent_action_deferred). Adding a new severity variant or matcher field meant touching three near-identical loops. The RuleEngine collapses the load + routing logic into one place; the three legacy free functions remain as thin wrappers so the public API is wire-stable.

rules holds the snapshot of enabled rules of the target kind (the engine is constructed per-action, not per-table — kind-scoped loading matches the existing list_enabled_by_kind shape and preserves the signature-verification side effects in crate::governance::rules_store::list_enabled_by_kind).

The combinator is first-refusal-wins with warn falling through and log being silent — identical semantics to the pre-refactor inline loops.

Implementations§

Source§

impl RuleEngine

Source

pub fn load_for_action(conn: &Connection, action: &AgentAction) -> Result<Self>

Construct an engine scoped to a single AgentAction’s kind without consulting any cache. Equivalent to load_for_action_cached(conn, None, action).

Reads the enabled rule rows of matching kind from governance_rules via crate::governance::rules_store::list_enabled_by_kind; the signature-verification gate (L1-6 bypass-impossibility invariant) runs inside that helper and is preserved verbatim.

§Errors

Propagates any SQLite error from list_enabled_by_kind.

Source

pub fn load_for_action_cached( conn: &Connection, cache: Option<&RuleCache>, action: &AgentAction, ) -> Result<Self>

Cached variant of Self::load_for_action (#991).

When cache is Some, consults the per-instance RuleCache — cache hit returns the cached Arc<Vec<Rule>> without re-running the SQL + Ed25519-verify path; cache miss loads via crate::governance::rules_store::list_enabled_by_kind and inserts. When cache is None, behaves exactly like Self::load_for_action (no cache consultation, fresh load).

§Errors

Propagates any SQLite error from list_enabled_by_kind.

Source

pub fn from_rules(rules: Vec<Rule>) -> Self

Construct an engine directly from a pre-loaded rules slice. Useful for tests that want to skip the SQLite round-trip or for future callsites that already hold a cached rule list.

Source

pub fn evaluate(&self, _agent_id: &str, action: &AgentAction) -> Decision

Evaluate action against the loaded rules. Returns the first-refusal-wins Decision.

agent_id is unused by the matcher today but threaded through so future agent-scoped matchers (operator allow-lists, agent quotas) can consult it without an API break.

Source

pub fn rules(&self) -> &[Rule]

Borrow the loaded rule slice. Used by count_matching_rules and by tests that want to assert load-side behaviour without running the matcher.

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<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> 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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