Skip to main content

Score

Struct Score 

Source
pub struct Score(/* private fields */);
Expand description

A fixed-point score in the inclusive range 0.0..=1.0, stored as an integer in 0..=10_000 (steps of 0.0001).

Scores are integers so that every computation is bit-for-bit identical on every platform — decisions are deterministic and exactly testable.

Implementations§

Source§

impl Score

Source

pub const SCALE: u32 = 10_000

The fixed-point scale: a raw value of SCALE represents 1.0.

Source

pub const ZERO: Score

The minimum score, 0.0.

Source

pub const MAX: Score

The maximum score, 1.0.

Source

pub const fn from_raw(raw: u32) -> Score

Creates a score from a raw fixed-point value, clamped to 0..=SCALE.

Source

pub const fn raw(self) -> u32

Returns the raw fixed-point value (0..=SCALE).

Examples found in repository?
examples/agent_brain.rs (line 41)
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 const fn from_ratio(num: u32, den: u32) -> Score

Creates a score from the ratio num / den, clamped to 0.0..=1.0.

A zero denominator yields Score::ZERO.

Examples found in repository?
examples/agent_brain.rs (line 11)
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 const fn mul(self, other: Score) -> Score

Multiplies two scores in the fixed-point domain (self * other), staying within 0.0..=1.0. Multiplying by Score::MAX is the identity.

Trait Implementations§

Source§

impl Clone for Score

Source§

fn clone(&self) -> Score

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 Copy for Score

Source§

impl Debug for Score

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Eq for Score

Source§

impl Hash for Score

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for Score

Source§

fn cmp(&self, other: &Score) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Score

Source§

fn eq(&self, other: &Score) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Score

Source§

fn partial_cmp(&self, other: &Score) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl StructuralPartialEq for Score

Auto Trait Implementations§

§

impl Freeze for Score

§

impl RefUnwindSafe for Score

§

impl Send for Score

§

impl Sync for Score

§

impl Unpin for Score

§

impl UnsafeUnpin for Score

§

impl UnwindSafe for Score

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.