Skip to content

Building 70 Projects ranging from beginner to advanced so anyone can — learn from, build upon, use as a reference, or even copy directly. Owned by my cybersecurity company 👇

5.3 KB
Raw Blame
// ©AngelaMos | 2026
// lib.rs

pub mod api;
pub mod ballot;
pub mod local;
pub mod wire;

use std::time::Duration;

use lisdex_core::config::LlmTier;

pub use ballot::{Declined, adjudicate};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Ask {
    subject: String,
    evidence: String,
    ballot: Vec<String>,
}

#[derive(Debug, thiserror::Error, PartialEq, Eq)]
pub enum AskError {
    #[error(
        "an ask with no ballot has nothing to choose from, so the model would answer in free text and the caller would have to trust it"
    )]
    EmptyBallot,
    #[error("ballot option {index} is blank, and a blank choice cannot be adjudicated")]
    BlankOption { index: usize },
    #[error("ballot option {option} appears more than once, so a match cannot be attributed")]
    DuplicateOption { option: String },
}

impl Ask {
    pub fn new(
        subject: impl Into<String>,
        evidence: impl Into<String>,
        ballot: Vec<String>,
    ) -> Result<Self, AskError> {
        if ballot.is_empty() {
            return Err(AskError::EmptyBallot);
        }
        for (index, option) in ballot.iter().enumerate() {
            if option.trim().is_empty() {
                return Err(AskError::BlankOption { index });
            }
        }
        for (index, option) in ballot.iter().enumerate() {
            if ballot[index + 1..].contains(option) {
                return Err(AskError::DuplicateOption {
                    option: option.clone(),
                });
            }
        }
        Ok(Self {
            subject: subject.into(),
            evidence: evidence.into(),
            ballot,
        })
    }

    #[must_use]
    pub fn subject(&self) -> &str {
        &self.subject
    }

    #[must_use]
    pub fn evidence(&self) -> &str {
        &self.evidence
    }

    #[must_use]
    pub fn ballot(&self) -> &[String] {
        &self.ballot
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Proposal {
    pub choice: String,
    pub tier: LlmTier,
    pub model: String,
    pub elapsed: Duration,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Outcome {
    Declined(Declined),
    Proposed(Proposal),
}

impl Outcome {
    #[must_use]
    pub fn proposal(&self) -> Option<&Proposal> {
        match self {
            Outcome::Proposed(proposal) => Some(proposal),
            Outcome::Declined(_) => None,
        }
    }
}

pub trait Booster {
    fn propose(&self, ask: &Ask) -> Outcome;
    fn describe(&self) -> String;
}

pub struct NoBooster;

impl Booster for NoBooster {
    fn propose(&self, _ask: &Ask) -> Outcome {
        Outcome::Declined(Declined::NoBooster)
    }

    fn describe(&self) -> String {
        "none (deterministic only)".to_owned()
    }
}

#[must_use]
pub fn select(tier: LlmTier) -> Box<dyn Booster> {
    match tier {
        LlmTier::None => Box::new(NoBooster),
        LlmTier::Local => Box::new(local::LocalBooster::from_env()),
        LlmTier::Api => Box::new(api::ApiBooster::from_env()),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn ballot() -> Vec<String> {
        vec!["byte_buffer".to_owned(), "nul_terminated_string".to_owned()]
    }

    #[test]
    fn an_ask_with_no_ballot_is_refused_at_construction() {
        assert_eq!(
            Ask::new("subject", "evidence", Vec::new()),
            Err(AskError::EmptyBallot)
        );
    }

    #[test]
    fn a_blank_ballot_option_is_refused() {
        let got = Ask::new(
            "subject",
            "evidence",
            vec!["ok".to_owned(), "  ".to_owned()],
        );
        assert_eq!(got, Err(AskError::BlankOption { index: 1 }));
    }

    #[test]
    fn a_repeated_ballot_option_is_refused_because_a_match_cannot_be_attributed() {
        let got = Ask::new(
            "subject",
            "evidence",
            vec!["same".to_owned(), "same".to_owned()],
        );
        assert_eq!(
            got,
            Err(AskError::DuplicateOption {
                option: "same".to_owned()
            })
        );
    }

    #[test]
    fn the_none_tier_declines_every_ask_and_never_proposes() {
        let ask = Ask::new("what is src", "const char *src", ballot()).expect("valid ask");
        let outcome = NoBooster.propose(&ask);
        assert_eq!(outcome, Outcome::Declined(Declined::NoBooster));
        assert!(outcome.proposal().is_none());
    }

    // The keyless doctrine is that the `none` tier is a real tier, not an error path: selecting
    // it must hand back a working booster rather than failing, because every capability has to
    // work with no model present before an LLM tier is credited with anything.
    #[test]
    fn selecting_the_none_tier_yields_a_booster_that_works() {
        let booster = select(LlmTier::None);
        let ask = Ask::new("what is src", "const char *src", ballot()).expect("valid ask");
        assert_eq!(
            booster.propose(&ask),
            Outcome::Declined(Declined::NoBooster)
        );
        assert_eq!(booster.describe(), "none (deterministic only)");
    }

    #[test]
    fn every_tier_selects_a_booster_that_describes_itself() {
        for tier in [LlmTier::None, LlmTier::Local, LlmTier::Api] {
            let booster = select(tier);
            assert!(
                !booster.describe().is_empty(),
                "tier {tier:?} described itself as nothing"
            );
        }
    }
}