Skip to main content

aios_cli/benchmark_next_app/baselines/
simple.rs

1//! Simple baseline predictors for the next-app benchmark.
2
3use std::cell::RefCell;
4use std::cmp::Ordering;
5use std::time::Instant;
6
7use rand::seq::SliceRandom;
8use rand::{rngs::StdRng, SeedableRng};
9
10use crate::benchmark_next_app::types::{NextAppPredictor, PredictionResult, ScoredPrediction};
11
12/// Always predict nothing (empty ranked list) — the simplest NoOp baseline.
13pub struct AlwaysNoOpBackend;
14
15impl NextAppPredictor for AlwaysNoOpBackend {
16    fn name(&self) -> &'static str {
17        "always_noop"
18    }
19
20    fn predict(
21        &self,
22        _ctx: &aios_spec::StructuredContext,
23        _current_app: &str,
24        _candidates: &[String],
25    ) -> PredictionResult {
26        PredictionResult {
27            ranked: Vec::new(),
28            latency_us: 0,
29            rationale_present: false,
30        }
31    }
32}
33
34/// Randomly shuffle the observable candidates with a fixed seed.
35pub struct RandomCandidateBackend {
36    rng: RefCell<StdRng>,
37}
38
39impl RandomCandidateBackend {
40    pub fn new(seed: u64) -> Self {
41        Self {
42            rng: RefCell::new(StdRng::seed_from_u64(seed)),
43        }
44    }
45}
46
47impl NextAppPredictor for RandomCandidateBackend {
48    fn name(&self) -> &'static str {
49        "random_candidate"
50    }
51
52    fn predict(
53        &self,
54        _ctx: &aios_spec::StructuredContext,
55        _current_app: &str,
56        candidates: &[String],
57    ) -> PredictionResult {
58        let start = Instant::now();
59        let mut shuffled = candidates.to_vec();
60        let mut rng = self.rng.borrow_mut();
61        shuffled.shuffle(&mut *rng);
62        PredictionResult {
63            ranked: shuffled
64                .into_iter()
65                .map(|package| ScoredPrediction {
66                    package,
67                    score: 1.0,
68                })
69                .collect(),
70            latency_us: start.elapsed().as_micros() as u64,
71            rationale_present: false,
72        }
73    }
74}
75
76/// Always pick the first observable candidate.
77pub struct FirstCandidateBackend;
78
79impl NextAppPredictor for FirstCandidateBackend {
80    fn name(&self) -> &'static str {
81        "first_candidate"
82    }
83
84    fn predict(
85        &self,
86        _ctx: &aios_spec::StructuredContext,
87        _current_app: &str,
88        candidates: &[String],
89    ) -> PredictionResult {
90        let start = Instant::now();
91        PredictionResult {
92            ranked: candidates
93                .first()
94                .cloned()
95                .into_iter()
96                .map(|package| ScoredPrediction {
97                    package,
98                    score: 1.0,
99                })
100                .collect(),
101            latency_us: start.elapsed().as_micros() as u64,
102            rationale_present: false,
103        }
104    }
105}
106
107/// Compare scored predictions by descending score.
108/// Callers add their own tie-breakers for determinism.
109pub(crate) fn cmp_score_desc(a: &ScoredPrediction, b: &ScoredPrediction) -> Ordering {
110    b.score.partial_cmp(&a.score).unwrap_or(Ordering::Equal)
111}