1use anyhow::Result;
6use serde::{Deserialize, Serialize};
7use std::collections::{HashMap, VecDeque};
8use std::path::{Path, PathBuf};
9use std::sync::atomic::{AtomicBool, Ordering};
10use std::sync::{Arc, Mutex};
11use std::time::{SystemTime, UNIX_EPOCH};
12use tracing::warn;
13
14fn now_ms() -> u64 {
15 SystemTime::now()
16 .duration_since(UNIX_EPOCH)
17 .unwrap_or_default()
18 .as_millis() as u64
19}
20
21#[derive(Debug, Serialize, Deserialize, Default, Clone)]
26pub struct AccountState {
27 #[serde(default)]
29 pub cooldown_until_ms: u64,
30 #[serde(default)]
32 pub disabled: bool,
33 #[serde(default)]
35 pub auth_failed: bool,
36}
37
38#[derive(Serialize, Deserialize, Default, Clone)]
39struct StickyEntry {
40 account_name: String,
41 expires_at_ms: u64,
42}
43
44#[derive(Debug, Serialize, Deserialize, Default, Clone)]
46pub struct QuotaWindow {
47 #[serde(default)]
49 pub window_start_ms: u64,
50 #[serde(default)]
51 pub input_tokens: u64,
52 #[serde(default)]
53 pub output_tokens: u64,
54}
55
56impl QuotaWindow {
57 pub fn total_tokens(&self) -> u64 {
58 self.input_tokens + self.output_tokens
59 }
60 pub fn window_expires_ms(&self) -> Option<u64> {
61 if self.window_start_ms == 0 { None } else { Some(self.window_start_ms + WINDOW_MS) }
62 }
63}
64
65pub const WINDOW_MS: u64 = 5 * 60 * 60 * 1000; #[derive(Debug, Clone, Serialize, Deserialize)]
73pub struct RequestLog {
74 pub ts_ms: u64,
75 pub account: String,
76 pub model: String,
77 pub status: u16,
78 pub input_tokens: u64,
79 pub output_tokens: u64,
80 pub duration_ms: u64,
81}
82
83const MAX_RECENT: usize = 200;
84
85#[derive(Debug, Serialize, Deserialize, Default, Clone)]
87pub struct RateLimitInfo {
88 pub utilization_5h: Option<f64>,
90 pub reset_5h: Option<u64>,
92 pub status_5h: Option<String>,
94 pub utilization_7d: Option<f64>,
96 pub reset_7d: Option<u64>,
98 pub status_7d: Option<String>,
99 pub overage_status: Option<String>,
101 pub overage_disabled_reason: Option<String>,
102 pub representative_claim: Option<String>,
104 pub updated_ms: u64,
105}
106
107#[derive(Debug, Serialize, Deserialize, Default, Clone)]
109pub struct DailyBucket {
110 pub input_tokens: u64,
111 pub output_tokens: u64,
112 pub api_cost_usd: f64,
114}
115
116#[derive(Debug, Serialize, Deserialize, Default, Clone)]
118pub struct SavingsSnapshot {
119 pub today_input: u64,
120 pub today_output: u64,
121 pub today_cost_usd: f64,
122 pub week_input: u64,
123 pub week_output: u64,
124 pub week_cost_usd: f64,
125 pub all_time_input: u64,
126 pub all_time_output: u64,
127 pub all_time_cost_usd: f64,
128}
129
130#[derive(Serialize, Deserialize, Default, Clone)]
131struct StateData {
132 #[serde(default)]
133 accounts: HashMap<String, AccountState>,
134 #[serde(default)]
135 sticky: HashMap<String, StickyEntry>,
136 #[serde(default)]
137 quota: HashMap<String, QuotaWindow>,
138 #[serde(default)]
139 rate_limits: HashMap<String, RateLimitInfo>,
140 #[serde(default)]
142 pinned_account: Option<String>,
143 #[serde(default)]
145 last_used_account: Option<String>,
146 #[serde(skip)]
148 recent_requests: VecDeque<RequestLog>,
149 #[serde(default)]
151 global_daily: HashMap<String, DailyBucket>,
152 #[serde(default)]
154 all_time_input: u64,
155 #[serde(default)]
156 all_time_output: u64,
157 #[serde(default)]
158 all_time_cost_usd: f64,
159}
160
161#[derive(Clone)]
166pub struct StateStore {
167 path: PathBuf,
168 inner: Arc<Mutex<StateData>>,
169 pending: Arc<AtomicBool>,
171}
172
173impl StateStore {
174 pub fn new_empty() -> Self {
176 Self {
178 path: PathBuf::from("/dev/null"),
179 inner: Arc::new(Mutex::new(StateData::default())),
180 pending: Arc::new(AtomicBool::new(false)),
181 }
182 }
183
184 pub fn load(path: &Path) -> Self {
185 let data: StateData = if path.exists() {
186 match std::fs::read_to_string(path) {
187 Ok(text) => serde_json::from_str(&text).unwrap_or_else(|e| {
188 warn!("State file unreadable ({e}), starting fresh");
189 StateData::default()
190 }),
191 Err(e) => {
192 warn!("Cannot read state file ({e}), starting fresh");
193 StateData::default()
194 }
195 }
196 } else {
197 StateData::default()
198 };
199
200 let store = Self {
201 path: path.to_owned(),
202 inner: Arc::new(Mutex::new(data)),
203 pending: Arc::new(AtomicBool::new(false)),
204 };
205 store.start_writer_thread();
206 store
207 }
208
209 fn start_writer_thread(&self) {
212 let pending = Arc::clone(&self.pending);
213 let inner = Arc::clone(&self.inner);
214 let path = self.path.clone();
215 std::thread::spawn(move || {
216 loop {
217 std::thread::sleep(std::time::Duration::from_millis(100));
218 if pending.compare_exchange(true, false, Ordering::AcqRel, Ordering::Relaxed).is_ok() {
219 let data = inner.lock().unwrap().clone();
220 if let Err(e) = write_to_disk(&data, &path) {
221 warn!("Failed to persist state: {e}");
222 }
223 }
224 }
225 });
226 }
227
228 pub fn is_available(&self, name: &str) -> bool {
233 let data = self.inner.lock().unwrap();
234 match data.accounts.get(name) {
235 None => true,
236 Some(s) => !s.disabled && now_ms() >= s.cooldown_until_ms,
237 }
238 }
239
240 pub fn account_states(&self) -> HashMap<String, AccountState> {
242 self.inner.lock().unwrap().accounts.clone()
243 }
244
245 pub fn set_cooldown(&self, name: &str, duration_ms: u64) {
250 {
251 let mut data = self.inner.lock().unwrap();
252 let acc = data.accounts.entry(name.to_owned()).or_default();
253 acc.cooldown_until_ms = now_ms() + duration_ms;
254 }
255 self.persist();
256 }
257
258 pub fn disable_account(&self, name: &str) {
259 {
260 let mut data = self.inner.lock().unwrap();
261 data.accounts.entry(name.to_owned()).or_default().disabled = true;
262 }
263 self.persist();
264 }
265
266 pub fn set_auth_failed(&self, name: &str) {
267 {
268 let mut data = self.inner.lock().unwrap();
269 let acc = data.accounts.entry(name.to_owned()).or_default();
270 acc.auth_failed = true;
271 acc.disabled = true; }
273 self.persist();
274 }
275
276 pub fn clear_auth_failed(&self, name: &str) {
278 {
279 let mut data = self.inner.lock().unwrap();
280 if let Some(acc) = data.accounts.get_mut(name) {
281 acc.auth_failed = false;
282 acc.disabled = false;
283 }
284 }
285 self.persist();
286 }
287
288 pub fn auth_failed_accounts<'a>(&self, names: &[&'a str]) -> Vec<&'a str> {
290 let data = self.inner.lock().unwrap();
291 names.iter()
292 .filter(|&&n| data.accounts.get(n).map(|s| s.auth_failed).unwrap_or(false))
293 .copied()
294 .collect()
295 }
296
297 pub fn get_sticky(&self, fingerprint: &str) -> Option<String> {
302 let data = self.inner.lock().unwrap();
303 let entry = data.sticky.get(fingerprint)?;
304 if now_ms() < entry.expires_at_ms {
305 Some(entry.account_name.clone())
306 } else {
307 None
308 }
309 }
310
311 pub fn set_sticky(&self, fingerprint: &str, account_name: &str, ttl_ms: u64) {
312 let mut data = self.inner.lock().unwrap();
313 data.sticky.insert(
314 fingerprint.to_owned(),
315 StickyEntry { account_name: account_name.to_owned(), expires_at_ms: now_ms() + ttl_ms },
316 );
317 }
318
319 pub fn window_start_ms(&self, name: &str) -> u64 {
326 let data = self.inner.lock().unwrap();
327 data.quota.get(name).map(|q| q.window_start_ms).unwrap_or(u64::MAX)
328 }
329
330 pub fn reset_5h_secs(&self, name: &str) -> Option<u64> {
333 let now_secs = SystemTime::now()
334 .duration_since(UNIX_EPOCH)
335 .unwrap_or_default()
336 .as_secs();
337 let data = self.inner.lock().unwrap();
338 let reset = data.rate_limits.get(name)?.reset_5h?;
339 if reset > now_secs { Some(reset) } else { None }
340 }
341
342 pub fn utilization_5h(&self, name: &str) -> f64 {
345 let now_secs = SystemTime::now()
346 .duration_since(UNIX_EPOCH)
347 .unwrap_or_default()
348 .as_secs();
349 let data = self.inner.lock().unwrap();
350 let Some(rl) = data.rate_limits.get(name) else { return 0.0 };
351 if rl.reset_5h.map(|t| t <= now_secs).unwrap_or(false) {
353 return 0.0;
354 }
355 rl.utilization_5h.unwrap_or(0.0)
356 }
357
358 pub fn utilization_7d(&self, name: &str) -> f64 {
361 let now_secs = SystemTime::now()
362 .duration_since(UNIX_EPOCH)
363 .unwrap_or_default()
364 .as_secs();
365 let data = self.inner.lock().unwrap();
366 let Some(rl) = data.rate_limits.get(name) else { return 0.0 };
367 if rl.reset_7d.map(|t| t <= now_secs).unwrap_or(false) {
368 return 0.0;
369 }
370 rl.utilization_7d.unwrap_or(0.0)
371 }
372
373 pub fn reset_7d_secs(&self, name: &str) -> Option<u64> {
376 let now_secs = SystemTime::now()
377 .duration_since(UNIX_EPOCH)
378 .unwrap_or_default()
379 .as_secs();
380 let data = self.inner.lock().unwrap();
381 let reset = data.rate_limits.get(name)?.reset_7d?;
382 if reset > now_secs { Some(reset) } else { None }
383 }
384
385 pub fn record_usage(&self, name: &str, input_tokens: u64, output_tokens: u64) {
388 if input_tokens == 0 && output_tokens == 0 {
389 return;
390 }
391 {
392 let mut data = self.inner.lock().unwrap();
393 let quota = data.quota.entry(name.to_owned()).or_default();
394 let now = now_ms();
395 if quota.window_start_ms == 0 || now >= quota.window_start_ms + WINDOW_MS {
396 quota.window_start_ms = now;
397 quota.input_tokens = 0;
398 quota.output_tokens = 0;
399 }
400 quota.input_tokens += input_tokens;
401 quota.output_tokens += output_tokens;
402 }
403 self.persist();
404 }
405
406 pub fn quota_snapshot(&self) -> HashMap<String, QuotaWindow> {
408 self.inner.lock().unwrap().quota.clone()
409 }
410
411 pub fn update_rate_limits(&self, name: &str, info: RateLimitInfo) {
416 {
417 let mut data = self.inner.lock().unwrap();
418 data.rate_limits.insert(name.to_owned(), info);
419 }
420 self.persist();
421 }
422
423 pub fn rate_limit_snapshot(&self) -> HashMap<String, RateLimitInfo> {
424 self.inner.lock().unwrap().rate_limits.clone()
425 }
426
427 pub fn get_pinned(&self) -> Option<String> {
432 self.inner.lock().unwrap().pinned_account.clone()
433 }
434
435 pub fn set_pinned(&self, name: Option<String>) {
436 {
437 let mut data = self.inner.lock().unwrap();
438 data.pinned_account = name;
439 }
440 self.persist();
441 }
442
443 pub fn get_last_used(&self) -> Option<String> {
448 self.inner.lock().unwrap().last_used_account.clone()
449 }
450
451 pub fn set_last_used(&self, name: &str) {
452 {
453 let mut data = self.inner.lock().unwrap();
454 data.last_used_account = Some(name.to_owned());
455 }
456 self.persist();
457 }
458
459 pub fn record_request(&self, log: RequestLog) {
464 let mut data = self.inner.lock().unwrap();
465 if data.recent_requests.len() >= MAX_RECENT {
466 data.recent_requests.pop_front();
467 }
468 data.recent_requests.push_back(log);
469 }
470
471 pub fn recent_requests_snapshot(&self) -> Vec<RequestLog> {
473 let data = self.inner.lock().unwrap();
474 data.recent_requests.iter().rev().cloned().collect()
475 }
476
477 pub fn record_global(&self, model: &str, input_tokens: u64, output_tokens: u64) {
483 if input_tokens == 0 && output_tokens == 0 {
484 return;
485 }
486 let cost = crate::pricing::api_cost_usd(model, input_tokens, output_tokens);
487 let key = today_key();
488 {
489 let mut data = self.inner.lock().unwrap();
490 let bucket = data.global_daily.entry(key).or_default();
491 bucket.input_tokens += input_tokens;
492 bucket.output_tokens += output_tokens;
493 bucket.api_cost_usd += cost;
494 data.all_time_input += input_tokens;
495 data.all_time_output += output_tokens;
496 data.all_time_cost_usd += cost;
497
498 if data.global_daily.len() > 100 {
500 let cutoff = epoch_to_ymd(
501 SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_secs()
502 .saturating_sub(90 * 86400)
503 );
504 data.global_daily.retain(|k, _| k.as_str() >= cutoff.as_str());
505 }
506 }
507 self.persist();
508 }
509
510 pub fn savings_snapshot(&self) -> SavingsSnapshot {
512 let now_secs = SystemTime::now()
513 .duration_since(UNIX_EPOCH)
514 .unwrap_or_default()
515 .as_secs();
516 let today = today_key();
517 let week_ago = epoch_to_ymd(now_secs.saturating_sub(7 * 86400));
518
519 let data = self.inner.lock().unwrap();
520
521 let today_bucket = data.global_daily.get(&today).cloned().unwrap_or_default();
522
523 let (week_input, week_output, week_cost) = data.global_daily.iter()
524 .filter(|(k, _)| k.as_str() >= week_ago.as_str())
525 .fold((0u64, 0u64, 0f64), |(i, o, c), (_, b)| {
526 (i + b.input_tokens, o + b.output_tokens, c + b.api_cost_usd)
527 });
528
529 SavingsSnapshot {
530 today_input: today_bucket.input_tokens,
531 today_output: today_bucket.output_tokens,
532 today_cost_usd: today_bucket.api_cost_usd,
533 week_input,
534 week_output,
535 week_cost_usd: week_cost,
536 all_time_input: data.all_time_input,
537 all_time_output: data.all_time_output,
538 all_time_cost_usd: data.all_time_cost_usd,
539 }
540 }
541
542 fn persist(&self) {
547 self.pending.store(true, Ordering::Release);
549 }
550}
551
552#[cfg(test)]
553mod tests {
554 use super::*;
555
556 #[test]
557 fn test_sticky_ttl_expiry() {
558 let store = StateStore::new_empty();
559 let fp = "conv-fp-ttl";
560 store.set_sticky(fp, "account1", 1); assert_eq!(store.get_sticky(fp).as_deref(), Some("account1"),
562 "sticky should be available immediately");
563 std::thread::sleep(std::time::Duration::from_millis(10));
564 assert!(store.get_sticky(fp).is_none(),
565 "sticky must expire after TTL elapses");
566 }
567
568 #[test]
569 fn test_cooldown_blocks_availability() {
570 let store = StateStore::new_empty();
571 store.set_cooldown("acc", 5_000); assert!(!store.is_available("acc"), "account should be unavailable during cooldown");
573 }
574
575 #[test]
576 fn test_disable_blocks_availability() {
577 let store = StateStore::new_empty();
578 store.disable_account("acc");
579 assert!(!store.is_available("acc"), "disabled account must be unavailable");
580 }
581
582 #[test]
583 fn test_quota_accumulates() {
584 let store = StateStore::new_empty();
585 store.record_usage("acc", 100, 50);
586 store.record_usage("acc", 200, 75);
587 let snap = store.quota_snapshot();
588 let q = &snap["acc"];
589 assert_eq!(q.input_tokens, 300);
590 assert_eq!(q.output_tokens, 125);
591 assert_eq!(q.total_tokens(), 425);
592 }
593
594 #[test]
595 fn test_pinned_account_round_trip() {
596 let store = StateStore::new_empty();
597 assert!(store.get_pinned().is_none());
598 store.set_pinned(Some("myaccount".into()));
599 assert_eq!(store.get_pinned().as_deref(), Some("myaccount"));
600 store.set_pinned(None);
601 assert!(store.get_pinned().is_none());
602 }
603
604 #[test]
605 fn test_last_used_round_trip() {
606 let store = StateStore::new_empty();
607 assert!(store.get_last_used().is_none());
608 store.set_last_used("acc1");
609 assert_eq!(store.get_last_used().as_deref(), Some("acc1"));
610 }
611
612 #[test]
613 fn test_recent_requests_ring_buffer() {
614 let store = StateStore::new_empty();
615 for i in 0..=(MAX_RECENT + 5) {
617 store.record_request(RequestLog {
618 ts_ms: i as u64,
619 account: "acc".into(),
620 model: "m".into(),
621 status: 200,
622 input_tokens: 1,
623 output_tokens: 1,
624 duration_ms: 1,
625 });
626 }
627 let snap = store.recent_requests_snapshot();
628 assert_eq!(snap.len(), MAX_RECENT, "buffer must not grow beyond MAX_RECENT");
629 assert!(snap[0].ts_ms > snap[snap.len() - 1].ts_ms, "snapshot must be newest-first");
631 }
632
633 #[test]
634 fn test_state_persistence_roundtrip() {
635 let path = std::env::temp_dir().join(format!(
637 "shunt_test_state_{}.json",
638 std::time::SystemTime::now()
639 .duration_since(std::time::UNIX_EPOCH)
640 .unwrap()
641 .as_nanos()
642 ));
643
644 {
645 let store = StateStore::load(&path);
646 store.set_cooldown("acc", 999_999_000); store.record_usage("acc", 111, 222);
648 store.set_last_used("acc");
649 std::thread::sleep(std::time::Duration::from_millis(300));
651 }
652
653 let store2 = StateStore::load(&path);
655 assert!(!store2.is_available("acc"), "cooldown must survive restart");
656 let snap = store2.quota_snapshot();
657 assert_eq!(snap["acc"].input_tokens, 111, "quota must survive restart");
658 assert_eq!(snap["acc"].output_tokens, 222);
659 assert_eq!(store2.get_last_used().as_deref(), Some("acc"),
660 "last_used_account must survive restart");
661
662 let _ = std::fs::remove_file(&path);
663 }
664}
665
666fn today_key() -> String {
668 let secs = SystemTime::now()
669 .duration_since(UNIX_EPOCH)
670 .unwrap_or_default()
671 .as_secs();
672 epoch_to_ymd(secs)
673}
674
675fn epoch_to_ymd(secs: u64) -> String {
677 let days = (secs / 86400) as i64;
678 let z = days + 719_468;
679 let era = if z >= 0 { z } else { z - 146_096 } / 146_097;
680 let doe = z - era * 146_097;
681 let yoe = (doe - doe / 1_460 + doe / 36_524 - doe / 146_096) / 365;
682 let y = yoe + era * 400;
683 let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
684 let mp = (5 * doy + 2) / 153;
685 let d = doy - (153 * mp + 2) / 5 + 1;
686 let m = if mp < 10 { mp + 3 } else { mp - 9 };
687 let y = if m <= 2 { y + 1 } else { y };
688 format!("{y:04}-{m:02}-{d:02}")
689}
690
691fn write_to_disk(data: &StateData, path: &Path) -> Result<()> {
692 if let Some(parent) = path.parent() {
693 std::fs::create_dir_all(parent)?;
694 }
695 let tmp = path.with_extension("tmp");
696 std::fs::write(&tmp, serde_json::to_string_pretty(data)?)?;
697 std::fs::rename(&tmp, path)?;
698 Ok(())
699}