Skip to main content

Action

Struct Action 

Source
#[non_exhaustive]
pub struct Action<A> { pub id: A, pub base: Score, pub allowed: bool, pub considerations: Vec<Consideration>, }
Expand description

A candidate decision: the value returned if chosen, plus the considerations that score it.

Utility is the base weight multiplied by every consideration. Because they multiply, any near-zero consideration vetoes the action — all of them must be satisfied for a high utility.

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§id: A

The value returned when this action is chosen.

§base: Score

The base weight before considerations (defaults to Score::MAX).

§allowed: bool

Whether this action is permitted. A gated-off action (false) always has zero utility, so it is never chosen by decide/decide_weighted. Set it with gate. Defaults to true.

§considerations: Vec<Consideration>

The considerations multiplied together to form the utility.

Implementations§

Source§

impl<A> Action<A>

Source

pub fn new(id: A) -> Action<A>

Creates an action with a neutral base weight and no considerations.

Examples found in repository?
examples/agent_brain.rs (line 18)
9fn main() {
10    // Signals about the current request, each normalized to 0.0..=1.0.
11    let urgency = Score::from_ratio(80, 100);
12    let confidence = Score::from_ratio(40, 100); // how well we already understand it
13    let llm_available = true; // e.g. circuit breaker closed AND rate limiter has tokens
14
15    let mut brain = Reasoner::new();
16
17    // Answer from a template — strong when we are already confident.
18    brain.add(Action::new("answer_template").consider_labeled(
19        "confidence",
20        Curve::Linear,
21        confidence,
22    ));
23
24    // Escalate to the LLM — strong when urgent, but only if the LLM is available.
25    brain.add(
26        Action::new("call_llm")
27            .gate(llm_available) // constraint-aware: skipped entirely if the LLM is down/limited
28            .consider_labeled("urgency", Curve::Linear, urgency),
29    );
30
31    // An always-available, low-weight fallback so a decision still resolves.
32    brain.add(Action::new("defer").with_base(Score::from_ratio(1, 10)));
33
34    // Abstain if nothing clears the bar — the caller would escalate to a human.
35    let threshold = Score::from_ratio(5, 100);
36    match brain.decide_above(threshold) {
37        Some(decision) => {
38            println!(
39                "chose: {} (utility {}/10000)",
40                decision.id,
41                decision.utility.raw()
42            );
43            if let Some(why) = brain.explain() {
44                for c in why.contributions {
45                    println!(
46                        "  {} : input {} -> {}",
47                        c.label,
48                        c.input.raw(),
49                        c.output.raw()
50                    );
51                }
52            }
53        }
54        None => println!("nothing good enough — escalate to a human"),
55    }
56}
Source

pub fn gate(self, allowed: bool) -> Action<A>

Gates the action on a caller-supplied condition (builder style). Calls combine with AND, so .gate(a).gate(b) is allowed only when both hold.

This is how decisions become constraint-aware without any dependency: the caller passes whatever it already knows — a deadline, a rate limiter, a circuit breaker, business hours, a feature flag — as a bool. A gated-off action has zero utility and is never chosen. Keep one ungated fallback action so a decision still resolves when everything else is gated off.

Examples found in repository?
examples/agent_brain.rs (line 27)
9fn main() {
10    // Signals about the current request, each normalized to 0.0..=1.0.
11    let urgency = Score::from_ratio(80, 100);
12    let confidence = Score::from_ratio(40, 100); // how well we already understand it
13    let llm_available = true; // e.g. circuit breaker closed AND rate limiter has tokens
14
15    let mut brain = Reasoner::new();
16
17    // Answer from a template — strong when we are already confident.
18    brain.add(Action::new("answer_template").consider_labeled(
19        "confidence",
20        Curve::Linear,
21        confidence,
22    ));
23
24    // Escalate to the LLM — strong when urgent, but only if the LLM is available.
25    brain.add(
26        Action::new("call_llm")
27            .gate(llm_available) // constraint-aware: skipped entirely if the LLM is down/limited
28            .consider_labeled("urgency", Curve::Linear, urgency),
29    );
30
31    // An always-available, low-weight fallback so a decision still resolves.
32    brain.add(Action::new("defer").with_base(Score::from_ratio(1, 10)));
33
34    // Abstain if nothing clears the bar — the caller would escalate to a human.
35    let threshold = Score::from_ratio(5, 100);
36    match brain.decide_above(threshold) {
37        Some(decision) => {
38            println!(
39                "chose: {} (utility {}/10000)",
40                decision.id,
41                decision.utility.raw()
42            );
43            if let Some(why) = brain.explain() {
44                for c in why.contributions {
45                    println!(
46                        "  {} : input {} -> {}",
47                        c.label,
48                        c.input.raw(),
49                        c.output.raw()
50                    );
51                }
52            }
53        }
54        None => println!("nothing good enough — escalate to a human"),
55    }
56}
Source

pub fn with_base(self, base: Score) -> Action<A>

Sets the base weight (builder style).

Examples found in repository?
examples/agent_brain.rs (line 32)
9fn main() {
10    // Signals about the current request, each normalized to 0.0..=1.0.
11    let urgency = Score::from_ratio(80, 100);
12    let confidence = Score::from_ratio(40, 100); // how well we already understand it
13    let llm_available = true; // e.g. circuit breaker closed AND rate limiter has tokens
14
15    let mut brain = Reasoner::new();
16
17    // Answer from a template — strong when we are already confident.
18    brain.add(Action::new("answer_template").consider_labeled(
19        "confidence",
20        Curve::Linear,
21        confidence,
22    ));
23
24    // Escalate to the LLM — strong when urgent, but only if the LLM is available.
25    brain.add(
26        Action::new("call_llm")
27            .gate(llm_available) // constraint-aware: skipped entirely if the LLM is down/limited
28            .consider_labeled("urgency", Curve::Linear, urgency),
29    );
30
31    // An always-available, low-weight fallback so a decision still resolves.
32    brain.add(Action::new("defer").with_base(Score::from_ratio(1, 10)));
33
34    // Abstain if nothing clears the bar — the caller would escalate to a human.
35    let threshold = Score::from_ratio(5, 100);
36    match brain.decide_above(threshold) {
37        Some(decision) => {
38            println!(
39                "chose: {} (utility {}/10000)",
40                decision.id,
41                decision.utility.raw()
42            );
43            if let Some(why) = brain.explain() {
44                for c in why.contributions {
45                    println!(
46                        "  {} : input {} -> {}",
47                        c.label,
48                        c.input.raw(),
49                        c.output.raw()
50                    );
51                }
52            }
53        }
54        None => println!("nothing good enough — escalate to a human"),
55    }
56}
Source

pub fn consider(self, curve: Curve, input: Score) -> Action<A>

Adds a consideration (builder style).

Source

pub fn consider_labeled( self, label: &'static str, curve: Curve, input: Score, ) -> Action<A>

Adds a labeled consideration (builder style); the label appears in Reasoner::explain output.

Examples found in repository?
examples/agent_brain.rs (lines 18-22)
9fn main() {
10    // Signals about the current request, each normalized to 0.0..=1.0.
11    let urgency = Score::from_ratio(80, 100);
12    let confidence = Score::from_ratio(40, 100); // how well we already understand it
13    let llm_available = true; // e.g. circuit breaker closed AND rate limiter has tokens
14
15    let mut brain = Reasoner::new();
16
17    // Answer from a template — strong when we are already confident.
18    brain.add(Action::new("answer_template").consider_labeled(
19        "confidence",
20        Curve::Linear,
21        confidence,
22    ));
23
24    // Escalate to the LLM — strong when urgent, but only if the LLM is available.
25    brain.add(
26        Action::new("call_llm")
27            .gate(llm_available) // constraint-aware: skipped entirely if the LLM is down/limited
28            .consider_labeled("urgency", Curve::Linear, urgency),
29    );
30
31    // An always-available, low-weight fallback so a decision still resolves.
32    brain.add(Action::new("defer").with_base(Score::from_ratio(1, 10)));
33
34    // Abstain if nothing clears the bar — the caller would escalate to a human.
35    let threshold = Score::from_ratio(5, 100);
36    match brain.decide_above(threshold) {
37        Some(decision) => {
38            println!(
39                "chose: {} (utility {}/10000)",
40                decision.id,
41                decision.utility.raw()
42            );
43            if let Some(why) = brain.explain() {
44                for c in why.contributions {
45                    println!(
46                        "  {} : input {} -> {}",
47                        c.label,
48                        c.input.raw(),
49                        c.output.raw()
50                    );
51                }
52            }
53        }
54        None => println!("nothing good enough — escalate to a human"),
55    }
56}
Source

pub fn utility(&self) -> Score

Computes the action’s utility: base * product(considerations), or Score::ZERO if the action is gated off (allowed is false).

Trait Implementations§

Source§

impl<A: Clone> Clone for Action<A>

Source§

fn clone(&self) -> Action<A>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<A: Debug> Debug for Action<A>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<A> Freeze for Action<A>
where A: Freeze,

§

impl<A> RefUnwindSafe for Action<A>
where A: RefUnwindSafe,

§

impl<A> Send for Action<A>
where A: Send,

§

impl<A> Sync for Action<A>
where A: Sync,

§

impl<A> Unpin for Action<A>
where A: Unpin,

§

impl<A> UnsafeUnpin for Action<A>
where A: UnsafeUnpin,

§

impl<A> UnwindSafe for Action<A>
where A: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.