aios_cli/benchmark_next_app/baselines/
context.rs1use std::time::Instant;
4
5use aios_spec::{AppTransition, SanitizedEventType};
6
7use crate::benchmark_next_app::types::{NextAppPredictor, PredictionResult, ScoredPrediction};
8
9fn last_non_current_foreground(
11 ctx: &aios_spec::StructuredContext,
12 current_app: &str,
13) -> Option<(i64, String)> {
14 ctx.events
15 .iter()
16 .filter_map(|e| match &e.event_type {
17 SanitizedEventType::AppTransition {
18 package_name,
19 transition: AppTransition::Foreground,
20 ..
21 } => Some((e.timestamp_ms, package_name.clone())),
22 _ => None,
23 })
24 .filter(|(_, package)| package != current_app)
25 .max_by(|a, b| a.0.cmp(&b.0).then_with(|| b.1.cmp(&a.1)))
26}
27
28pub struct LastForegroundBackend;
30
31fn last_foreground_ranked(
32 ctx: &aios_spec::StructuredContext,
33 current_app: &str,
34 candidates: &[String],
35) -> Vec<ScoredPrediction> {
36 last_non_current_foreground(ctx, current_app)
37 .and_then(|(_, package)| {
38 if candidates.contains(&package) {
39 Some(vec![ScoredPrediction {
40 package,
41 score: 1.0,
42 }])
43 } else {
44 None
45 }
46 })
47 .unwrap_or_default()
48}
49
50impl NextAppPredictor for LastForegroundBackend {
51 fn name(&self) -> &'static str {
52 "last_foreground"
53 }
54
55 fn predict(
56 &self,
57 ctx: &aios_spec::StructuredContext,
58 current_app: &str,
59 candidates: &[String],
60 ) -> PredictionResult {
61 let start = Instant::now();
62 PredictionResult {
63 ranked: last_foreground_ranked(ctx, current_app, candidates),
64 latency_us: start.elapsed().as_micros() as u64,
65 rationale_present: false,
66 }
67 }
68}
69
70pub struct LastAppPrewarmBackend;
78
79impl NextAppPredictor for LastAppPrewarmBackend {
80 fn name(&self) -> &'static str {
81 "last_app_prewarm"
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: last_foreground_ranked(ctx, current_app, candidates),
93 latency_us: start.elapsed().as_micros() as u64,
94 rationale_present: false,
95 }
96 }
97}
98
99#[cfg(test)]
100mod tests {
101 use aios_spec::{
102 AppTransition, ContextSummary, SanitizedEvent, SanitizedEventType, SourceTier,
103 StructuredContext,
104 };
105
106 use super::*;
107
108 fn ctx_with_events(events: Vec<SanitizedEvent>) -> StructuredContext {
109 StructuredContext {
110 window_id: "w1".into(),
111 window_start_ms: 0,
112 window_end_ms: 10_000,
113 duration_secs: 10,
114 events,
115 summary: ContextSummary {
116 foreground_apps: vec![],
117 notified_apps: vec![],
118 all_semantic_hints: vec![],
119 file_activity: vec![],
120 latest_system_status: None,
121 source_tier: SourceTier::PublicApi,
122 },
123 }
124 }
125
126 fn foreground_event(timestamp_ms: i64, package_name: &str) -> SanitizedEvent {
127 SanitizedEvent {
128 event_id: format!("fg{timestamp_ms}"),
129 timestamp_ms,
130 event_type: SanitizedEventType::AppTransition {
131 package_name: package_name.into(),
132 activity_class: None,
133 transition: AppTransition::Foreground,
134 },
135 source_tier: SourceTier::PublicApi,
136 app_package: Some(package_name.into()),
137 uid: None,
138 }
139 }
140
141 #[test]
142 fn last_foreground_is_noop_for_empty_context() {
143 let backend = LastForegroundBackend;
144 let result = backend.predict(&ctx_with_events(vec![]), "A", &["B".into()]);
145 assert!(result.ranked.is_empty());
146 }
147
148 #[test]
149 fn last_foreground_excludes_current_app() {
150 let ctx = ctx_with_events(vec![foreground_event(200, "A"), foreground_event(100, "B")]);
151 let backend = LastForegroundBackend;
152 let result = backend.predict(&ctx, "A", &["B".into(), "C".into()]);
153 assert_eq!(result.ranked.len(), 1);
154 assert_eq!(result.ranked[0].package, "B");
155 assert_eq!(result.ranked[0].score, 1.0);
156 }
157
158 #[test]
159 fn last_foreground_noop_when_target_not_candidate() {
160 let ctx = ctx_with_events(vec![foreground_event(100, "B")]);
161 let backend = LastForegroundBackend;
162 let result = backend.predict(&ctx, "A", &["C".into()]);
163 assert!(result.ranked.is_empty());
164 }
165
166 #[test]
167 fn last_app_prewarm_uses_last_foreground_proxy() {
168 let ctx = ctx_with_events(vec![foreground_event(200, "A"), foreground_event(100, "B")]);
169 let prewarm = LastAppPrewarmBackend;
170 let foreground = LastForegroundBackend;
171 assert_eq!(
172 prewarm.predict(&ctx, "A", &["B".into()]).ranked,
173 foreground.predict(&ctx, "A", &["B".into()]).ranked
174 );
175 }
176
177 #[test]
178 fn last_non_current_foreground_tie_breaks_alphabetically() {
179 let ctx = ctx_with_events(vec![
180 foreground_event(100, "B"),
181 foreground_event(100, "A"),
182 foreground_event(200, "C"),
183 ]);
184 let result = last_non_current_foreground(&ctx, "C");
185 assert_eq!(result, Some((100, "A".into())));
186 }
187}