1use std::collections::HashMap;
24use std::sync::atomic::{AtomicU64, Ordering};
25use std::sync::{Mutex, OnceLock};
26use std::time::{SystemTime, UNIX_EPOCH};
27
28use serde::{Deserialize, Serialize};
29use serde_json::Value;
30
31use super::cold_prefix;
32
33const MAX_TRACKED: usize = 4096;
36
37#[derive(Debug, Clone, Copy, PartialEq, Eq)]
40pub enum CacheOutcome {
41 ColdStart,
44 WarmReuse,
47 TtlLapse,
50 PrefixChange,
53}
54
55#[derive(Debug, Clone, Copy)]
58struct PrefixState {
59 prefix_hash: u64,
60 last_touch: u64,
61}
62
63static COLD_STARTS: AtomicU64 = AtomicU64::new(0);
64static WARM_REUSES: AtomicU64 = AtomicU64::new(0);
65static TTL_LAPSES: AtomicU64 = AtomicU64::new(0);
66static PREFIX_CHANGES: AtomicU64 = AtomicU64::new(0);
67
68fn store() -> &'static Mutex<HashMap<u64, PrefixState>> {
69 static STORE: OnceLock<Mutex<HashMap<u64, PrefixState>>> = OnceLock::new();
70 STORE.get_or_init(|| Mutex::new(HashMap::new()))
71}
72
73fn now_secs() -> u64 {
74 SystemTime::now()
75 .duration_since(UNIX_EPOCH)
76 .map_or(0, |d| d.as_secs())
77}
78
79#[must_use]
84pub fn classify(prev: Option<(u64, u64)>, curr_hash: u64, now: u64, ttl_secs: u64) -> CacheOutcome {
85 match prev {
86 None => CacheOutcome::ColdStart,
87 Some((prev_hash, last_touch)) => {
88 if prev_hash != curr_hash {
89 CacheOutcome::PrefixChange
90 } else if now.saturating_sub(last_touch) > ttl_secs {
91 CacheOutcome::TtlLapse
92 } else {
93 CacheOutcome::WarmReuse
94 }
95 }
96 }
97}
98
99fn bump(outcome: CacheOutcome) {
100 let counter = match outcome {
101 CacheOutcome::ColdStart => &COLD_STARTS,
102 CacheOutcome::WarmReuse => &WARM_REUSES,
103 CacheOutcome::TtlLapse => &TTL_LAPSES,
104 CacheOutcome::PrefixChange => &PREFIX_CHANGES,
105 };
106 counter.fetch_add(1, Ordering::Relaxed);
107}
108
109fn evict_oldest(map: &mut HashMap<u64, PrefixState>) {
110 if let Some(oldest) = map
111 .iter()
112 .min_by_key(|(_, s)| s.last_touch)
113 .map(|(k, _)| *k)
114 {
115 map.remove(&oldest);
116 }
117}
118
119pub fn record_request(messages: &[Value], cached: usize) -> Option<CacheOutcome> {
125 let conv_key = cold_prefix::conversation_key(messages)?;
126 let curr_hash = cold_prefix::cached_prefix_hash(messages, cached)?;
127 let ttl = cold_prefix::resolved_ttl_secs(messages, cached).unwrap_or(0);
128 let now = now_secs();
129
130 let outcome = {
131 let mut map = store()
132 .lock()
133 .unwrap_or_else(std::sync::PoisonError::into_inner);
134 let prev = map.get(&conv_key).map(|s| (s.prefix_hash, s.last_touch));
135 let outcome = classify(prev, curr_hash, now, ttl);
136 map.insert(
137 conv_key,
138 PrefixState {
139 prefix_hash: curr_hash,
140 last_touch: now,
141 },
142 );
143 if map.len() > MAX_TRACKED {
144 evict_oldest(&mut map);
145 }
146 outcome
147 };
148 bump(outcome);
149 Some(outcome)
150}
151
152#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
154pub struct CacheAttribution {
155 pub cold_starts: u64,
157 pub warm_reuses: u64,
159 pub ttl_lapses: u64,
161 pub prefix_changes: u64,
163}
164
165impl CacheAttribution {
166 #[must_use]
170 pub fn provider_reuse_rate(&self) -> f64 {
171 let eligible = self
172 .warm_reuses
173 .saturating_add(self.ttl_lapses)
174 .saturating_add(self.prefix_changes);
175 if eligible == 0 {
176 0.0
177 } else {
178 self.warm_reuses as f64 * 100.0 / eligible as f64
179 }
180 }
181
182 #[must_use]
183 pub fn eligible_provider_requests(&self) -> u64 {
184 self.warm_reuses
185 .saturating_add(self.ttl_lapses)
186 .saturating_add(self.prefix_changes)
187 }
188}
189
190#[must_use]
191pub fn snapshot() -> CacheAttribution {
192 CacheAttribution {
193 cold_starts: COLD_STARTS.load(Ordering::Relaxed),
194 warm_reuses: WARM_REUSES.load(Ordering::Relaxed),
195 ttl_lapses: TTL_LAPSES.load(Ordering::Relaxed),
196 prefix_changes: PREFIX_CHANGES.load(Ordering::Relaxed),
197 }
198}
199
200#[cfg(test)]
201mod tests {
202 use super::*;
203 use serde_json::json;
204
205 #[test]
206 fn classify_distinguishes_ttl_lapse_from_prefix_change() {
207 assert_eq!(classify(None, 1, 100, 300), CacheOutcome::ColdStart);
209 assert_eq!(
211 classify(Some((1, 100)), 1, 350, 300),
212 CacheOutcome::WarmReuse
213 );
214 assert_eq!(
216 classify(Some((1, 100)), 1, 500, 300),
217 CacheOutcome::TtlLapse
218 );
219 assert_eq!(
221 classify(Some((1, 100)), 2, 110, 300),
222 CacheOutcome::PrefixChange
223 );
224 assert_eq!(
226 classify(Some((1, 100)), 2, 9999, 300),
227 CacheOutcome::PrefixChange
228 );
229 }
230
231 fn anchored(first_text: &str) -> Vec<Value> {
232 vec![
233 json!({"role": "user", "content": [
234 {"type": "text", "text": first_text, "cache_control": {"type": "ephemeral"}}
235 ]}),
236 json!({"role": "assistant", "content": "ok"}),
237 ]
238 }
239
240 #[test]
241 fn unanchored_turn_is_not_attributed() {
242 let msgs = anchored("unanchored-attribution-test");
243 assert_eq!(record_request(&msgs, 0), None);
245 }
246
247 #[test]
248 fn first_anchored_turn_is_cold_start_then_warm() {
249 let msgs = anchored("cold-then-warm-attribution-test");
250 assert_eq!(record_request(&msgs, 1), Some(CacheOutcome::ColdStart));
251 assert_eq!(record_request(&msgs, 1), Some(CacheOutcome::WarmReuse));
253 }
254
255 #[test]
256 fn prefix_change_detected_with_stable_head() {
257 let head = json!({"role": "user", "content": [
260 {"type": "text", "text": "stable-head-attribution", "cache_control": {"type": "ephemeral"}}
261 ]});
262 let v1 = vec![
263 head.clone(),
264 json!({"role": "assistant", "content": "answer one"}),
265 ];
266 let v2 = vec![
267 head,
268 json!({"role": "assistant", "content": "answer two CHANGED"}),
269 ];
270
271 assert_eq!(record_request(&v1, 2), Some(CacheOutcome::ColdStart));
272 assert_eq!(record_request(&v2, 2), Some(CacheOutcome::PrefixChange));
273 }
274
275 #[test]
276 fn provider_reuse_excludes_cold_starts_from_denominator() {
277 let attribution = CacheAttribution {
278 cold_starts: 99,
279 warm_reuses: 6,
280 ttl_lapses: 2,
281 prefix_changes: 4,
282 };
283
284 assert_eq!(attribution.eligible_provider_requests(), 12);
285 assert_eq!(attribution.provider_reuse_rate(), 50.0);
286 }
287}