1use hashbrown::HashMap;
7use std::sync::{Arc, Mutex, PoisonError};
8
9use std::time::{Duration, Instant};
10use tokio::sync::RwLock;
11use tokio::time;
12
13#[derive(Debug, Clone, Hash, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
15pub enum OperationType {
16 ApiCall,
18 FileOperation,
20 CodeAnalysis,
22 ToolExecution,
24 NetworkRequest,
26 Processing,
28 Custom(String),
30}
31
32#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
34pub struct TimeoutConfig {
35 pub timeout_duration: Duration,
37 pub max_retries: u32,
39 pub initial_retry_delay: Duration,
41 pub max_retry_delay: Duration,
43 pub backoff_multiplier: f64,
45 pub use_jitter: bool,
47 pub retry_on_timeout: bool,
49 pub retry_on_errors: Vec<String>,
51}
52
53const DEFAULT_RETRY_ERRORS: [&str; 4] = ["timeout", "connection", "network", "server_error"];
55
56impl Default for TimeoutConfig {
57 fn default() -> Self {
58 Self {
59 timeout_duration: Duration::from_secs(30),
60 max_retries: 3,
61 initial_retry_delay: Duration::from_millis(100),
62 max_retry_delay: Duration::from_secs(30),
63 backoff_multiplier: 2.0,
64 use_jitter: true,
65 retry_on_timeout: true,
66 retry_on_errors: DEFAULT_RETRY_ERRORS.iter().map(|s| (*s).into()).collect(),
67 }
68 }
69}
70
71impl TimeoutConfig {
72 pub fn api_call() -> Self {
74 Self {
75 timeout_duration: Duration::from_secs(60),
76 max_retries: 5,
77 initial_retry_delay: Duration::from_millis(200),
78 max_retry_delay: Duration::from_secs(10),
79 backoff_multiplier: 1.5,
80 ..Default::default()
81 }
82 }
83
84 pub fn file_operation() -> Self {
86 Self {
87 timeout_duration: Duration::from_secs(10),
88 max_retries: 2,
89 initial_retry_delay: Duration::from_millis(50),
90 max_retry_delay: Duration::from_secs(2),
91 backoff_multiplier: 2.0,
92 retry_on_timeout: false, ..Default::default()
94 }
95 }
96
97 pub fn analysis() -> Self {
99 Self {
100 timeout_duration: Duration::from_secs(120),
101 max_retries: 1,
102 initial_retry_delay: Duration::from_secs(5),
103 max_retry_delay: Duration::from_secs(10),
104 backoff_multiplier: 2.0,
105 ..Default::default()
106 }
107 }
108}
109
110#[derive(Debug, Clone)]
112pub struct TimeoutEvent {
113 pub operation_id: String,
115 pub operation_type: OperationType,
117 pub start_time: Instant,
119 pub timeout_duration: Duration,
121 pub retry_count: u32,
123 pub error_message: Option<String>,
125}
126
127#[derive(Debug, Clone, Default)]
129pub struct TimeoutStats {
130 pub total_operations: usize,
132 pub timed_out_operations: usize,
134 pub successful_retries: usize,
136 pub failed_retries: usize,
138 pub average_timeout_duration: Duration,
140 pub total_retry_attempts: usize,
142}
143
144type PendingEnds = Mutex<Vec<(String, Instant)>>;
146
147pub struct TimeoutDetector {
153 configs: Arc<RwLock<HashMap<OperationType, TimeoutConfig>>>,
154 active_operations: Arc<RwLock<HashMap<String, TimeoutEvent>>>,
155 stats: Arc<RwLock<TimeoutStats>>,
156 pending_ends: Arc<PendingEnds>,
158}
159
160impl Default for TimeoutDetector {
161 fn default() -> Self {
162 Self::new()
163 }
164}
165
166impl TimeoutDetector {
167 pub fn new() -> Self {
171 let mut configs_map = HashMap::new();
173 configs_map.insert(OperationType::ApiCall, TimeoutConfig::api_call());
174 configs_map.insert(OperationType::FileOperation, TimeoutConfig::file_operation());
175 configs_map.insert(OperationType::CodeAnalysis, TimeoutConfig::analysis());
176 configs_map.insert(OperationType::ToolExecution, TimeoutConfig::default());
177 configs_map.insert(OperationType::NetworkRequest, TimeoutConfig::api_call());
178 configs_map.insert(OperationType::Processing, TimeoutConfig::analysis());
179
180 let configs = Arc::new(RwLock::new(configs_map));
181 let active_operations: Arc<RwLock<HashMap<String, TimeoutEvent>>> = Arc::new(RwLock::new(HashMap::new()));
182 let stats = Arc::new(RwLock::new(TimeoutStats::default()));
183 let pending_ends = Arc::new(Mutex::new(Vec::new()));
184
185 Self { configs, active_operations, stats, pending_ends }
186 }
187
188 async fn drain_pending_ends(&self) {
190 let ended = std::mem::take(&mut *self.pending_ends.lock().unwrap_or_else(PoisonError::into_inner));
191 for (operation_id, ended_at) in ended {
192 self.end_operation_inner(&operation_id, ended_at).await;
193 }
194 }
195
196 async fn end_operation_inner(&self, operation_id: &str, ended_at: Instant) {
197 let mut active_ops = self.active_operations.write().await;
198 if let Some(event) = active_ops.remove(operation_id) {
199 let duration = ended_at.saturating_duration_since(event.start_time);
200 let mut stats = self.stats.write().await;
201 if stats.total_operations > 0 {
202 let total_duration = stats.average_timeout_duration * (stats.total_operations - 1) as u32;
203 stats.average_timeout_duration = (total_duration + duration) / stats.total_operations as u32;
204 }
205 }
206 }
207
208 pub async fn set_config(&self, operation_type: OperationType, config: TimeoutConfig) {
210 let mut configs = self.configs.write().await;
211 configs.insert(operation_type, config);
212 }
213
214 pub async fn get_config(&self, operation_type: &OperationType) -> TimeoutConfig {
216 let configs = self.configs.read().await;
217 configs.get(operation_type).cloned().unwrap_or_default()
218 }
219
220 pub async fn start_operation(&self, operation_id: String, operation_type: OperationType) -> TimeoutHandle {
222 self.drain_pending_ends().await;
223 let config = self.get_config(&operation_type).await;
224
225 let event = TimeoutEvent {
226 operation_id: operation_id.clone(),
227 operation_type,
228 start_time: Instant::now(),
229 timeout_duration: config.timeout_duration,
230 retry_count: 0,
231 error_message: None,
232 };
233
234 let mut active_ops = self.active_operations.write().await;
235 active_ops.insert(operation_id.clone(), event);
236
237 let mut stats = self.stats.write().await;
238 stats.total_operations += 1;
239
240 TimeoutHandle {
241 operation_id,
242 pending_ends: Some(Arc::clone(&self.pending_ends)),
243 }
244 }
245
246 pub async fn check_timeout(&self, operation_id: &str) -> Option<TimeoutEvent> {
248 self.drain_pending_ends().await;
249 let active_ops = self.active_operations.read().await;
250 active_ops
251 .get(operation_id)
252 .filter(|event| event.start_time.elapsed() >= event.timeout_duration)
253 .cloned()
254 }
255
256 pub async fn record_timeout(&self, operation_id: &str, error_message: Option<String>) {
258 let mut active_ops = self.active_operations.write().await;
259 if let Some(event) = active_ops.get_mut(operation_id) {
260 event.error_message = error_message;
261 }
262
263 let mut stats = self.stats.write().await;
264 stats.timed_out_operations += 1;
265 }
266
267 pub async fn record_successful_retry(&self, _operation_id: &str) {
269 let mut stats = self.stats.write().await;
270 stats.successful_retries += 1;
271 stats.total_retry_attempts += 1;
272 }
273
274 pub async fn record_failed_retry(&self, _operation_id: &str) {
276 let mut stats = self.stats.write().await;
277 stats.failed_retries += 1;
278 stats.total_retry_attempts += 1;
279 }
280
281 pub async fn end_operation(&self, operation_id: &str) {
283 self.drain_pending_ends().await;
284 self.end_operation_inner(operation_id, Instant::now()).await;
285 }
286
287 pub async fn get_stats(&self) -> TimeoutStats {
289 self.drain_pending_ends().await;
290 self.stats.read().await.clone()
291 }
292
293 pub async fn calculate_retry_delay(&self, operation_type: &OperationType, attempt: u32) -> Duration {
295 let config = self.get_config(operation_type).await;
296
297 let base_delay = config.initial_retry_delay.as_millis() as f64;
298 let multiplier = config.backoff_multiplier.powi(attempt as i32);
299 #[allow(
300 clippy::cast_sign_loss,
301 reason = "Intentional compatibility, platform, or test-only suppression."
302 )]
303 let delay_ms = (base_delay * multiplier) as u64;
304
305 let mut delay = Duration::from_millis(delay_ms.min(config.max_retry_delay.as_millis() as u64));
306
307 if config.use_jitter {
309 use std::time::SystemTime;
310 let seed = SystemTime::now()
311 .duration_since(std::time::UNIX_EPOCH)
312 .unwrap_or_default()
313 .as_nanos() as u64;
314 let jitter_factor = (seed % 100) as f64 / 100.0; #[allow(
316 clippy::cast_sign_loss,
317 reason = "Intentional compatibility, platform, or test-only suppression."
318 )]
319 let jitter_ms = (delay.as_millis() as f64 * 0.1 * jitter_factor) as u64; delay += Duration::from_millis(jitter_ms);
321 }
322
323 delay
324 }
325
326 pub async fn should_retry(&self, operation_type: &OperationType, error: &anyhow::Error, attempt: u32) -> bool {
329 if vtcode_commons::detect_misconfiguration_in_anyhow(error).is_some() {
330 return false;
331 }
332
333 let config = self.get_config(operation_type).await;
334
335 if attempt >= config.max_retries {
336 return false;
337 }
338
339 let error_str = error.to_string();
340
341 let contains_ci = |pattern: &str| {
343 error_str
344 .as_bytes()
345 .windows(pattern.len())
346 .any(|window| window.eq_ignore_ascii_case(pattern.as_bytes()))
347 };
348
349 for retry_error in &config.retry_on_errors {
351 if contains_ci(retry_error) {
352 return true;
353 }
354 }
355
356 config.retry_on_timeout && (contains_ci("timeout") || contains_ci("timed out"))
358 }
359
360 pub async fn execute_with_timeout_retry<F, Fut, T>(
362 &self,
363 operation_id: String,
364 operation_type: OperationType,
365 mut operation: F,
366 ) -> Result<T, anyhow::Error>
367 where
368 F: FnMut() -> Fut,
369 Fut: Future<Output = Result<T, anyhow::Error>>,
370 {
371 let config = self.get_config(&operation_type).await;
372 let mut attempt = 0;
373 let _last_error: Option<anyhow::Error> = None;
374
375 loop {
376 let handle = self
377 .start_operation(format!("{operation_id}_{attempt}"), operation_type.clone())
378 .await;
379
380 let result = match time::timeout(config.timeout_duration, operation()).await {
381 Ok(result) => result,
382 Err(_) => {
383 self.record_timeout(&handle.operation_id, Some("Operation timed out".to_owned()))
384 .await;
385 Err(anyhow::anyhow!("Operation '{}' timed out after {:?}", operation_id, config.timeout_duration))
386 }
387 };
388
389 handle.end().await;
390
391 match result {
392 Ok(value) => {
393 if attempt > 0 {
394 self.record_successful_retry(&format!("{operation_id}_{attempt}")).await;
395 }
396 return Ok(value);
397 }
398 Err(error) => {
399 let should_retry_op = self.should_retry(&operation_type, &error, attempt).await;
400
401 if !should_retry_op {
402 if attempt > 0 {
403 self.record_failed_retry(&format!("{operation_id}_{attempt}")).await;
404 }
405 return Err(error);
406 }
407
408 attempt += 1;
409 self.record_failed_retry(&format!("{operation_id}_{attempt}")).await;
410
411 let delay = self.calculate_retry_delay(&operation_type, attempt).await;
412 tracing::warn!(
413 operation_id,
414 attempt,
415 max_retries = config.max_retries,
416 delay = ?delay,
417 "Operation failed and will be retried"
418 );
419 time::sleep(delay).await;
420 }
421 }
422 }
423 }
424}
425
426impl Clone for TimeoutDetector {
427 fn clone(&self) -> Self {
428 Self {
429 configs: Arc::clone(&self.configs),
430 active_operations: Arc::clone(&self.active_operations),
431 stats: Arc::clone(&self.stats),
432 pending_ends: Arc::clone(&self.pending_ends),
433 }
434 }
435}
436
437pub struct TimeoutHandle {
442 operation_id: String,
443 pending_ends: Option<Arc<PendingEnds>>,
446}
447
448impl TimeoutHandle {
449 pub async fn end(mut self) {
453 self.enqueue_end();
454 }
455
456 fn enqueue_end(&mut self) {
457 if let Some(queue) = self.pending_ends.take() {
458 queue
459 .lock()
460 .unwrap_or_else(PoisonError::into_inner)
461 .push((std::mem::take(&mut self.operation_id), Instant::now()));
462 }
463 }
464
465 pub fn operation_id(&self) -> &str {
467 &self.operation_id
468 }
469}
470
471impl Drop for TimeoutHandle {
472 fn drop(&mut self) {
473 self.enqueue_end();
474 }
475}
476
477use std::sync::LazyLock;
479pub static TIMEOUT_DETECTOR: LazyLock<TimeoutDetector> = LazyLock::new(TimeoutDetector::new);
480
481#[cfg(test)]
482mod tests {
483 use super::*;
484 use std::sync::atomic::{AtomicUsize, Ordering};
485 use std::time::Duration;
486 use tokio::time::sleep;
487
488 #[tokio::test]
489 async fn test_timeout_detection() {
490 let detector = TimeoutDetector::new();
491
492 let config = TimeoutConfig {
494 timeout_duration: Duration::from_millis(10),
495 max_retries: 0,
496 ..Default::default()
497 };
498
499 detector.set_config(OperationType::ApiCall, config).await;
500
501 let result = detector
502 .execute_with_timeout_retry("test_operation".to_owned(), OperationType::ApiCall, || async {
503 sleep(Duration::from_millis(20)).await;
504 Ok("success")
505 })
506 .await;
507
508 assert!(result.is_err());
509 assert!(result.unwrap_err().to_string().contains("timed out"));
510 }
511
512 #[tokio::test]
513 async fn test_successful_retry() {
514 let detector = TimeoutDetector::new();
515
516 let config = TimeoutConfig {
517 timeout_duration: Duration::from_millis(50),
518 max_retries: 2,
519 initial_retry_delay: Duration::from_millis(5),
520 retry_on_timeout: true,
521 ..Default::default()
522 };
523
524 detector.set_config(OperationType::ApiCall, config).await;
525
526 let call_count = Arc::new(AtomicUsize::new(0));
527 let call_count_clone = call_count.clone();
528 let result = detector
529 .execute_with_timeout_retry("test_retry".to_owned(), OperationType::ApiCall, move || {
530 let call_count = call_count_clone.clone();
531 async move {
532 let count = call_count.fetch_add(1, Ordering::SeqCst) + 1;
533 if count == 1 {
534 sleep(Duration::from_millis(60)).await;
536 Ok("should not reach here")
537 } else {
538 sleep(Duration::from_millis(10)).await;
540 Ok("success")
541 }
542 }
543 })
544 .await;
545
546 assert!(result.is_ok());
547 assert_eq!(result.unwrap(), "success");
548 assert_eq!(call_count.load(Ordering::SeqCst), 2);
549
550 let stats = detector.get_stats().await;
551 assert_eq!(stats.successful_retries, 1);
552 assert_eq!(stats.total_retry_attempts, 2);
553 }
554
555 #[test]
556 fn detector_constructs_and_cleans_up_across_runtimes_without_ambient_runtime() {
557 let detector = TimeoutDetector::new();
559
560 let first = tokio::runtime::Builder::new_current_thread()
561 .enable_all()
562 .build()
563 .expect("runtime");
564 let handle = first.block_on(detector.start_operation("op-a".to_owned(), OperationType::ApiCall));
565 drop(first);
566 drop(handle);
568
569 let second = tokio::runtime::Builder::new_current_thread()
570 .enable_all()
571 .build()
572 .expect("runtime");
573 second.block_on(async {
574 let kept = detector.start_operation("op-b".to_owned(), OperationType::ApiCall).await;
575 let stats = detector.get_stats().await;
576 assert_eq!(stats.total_operations, 2);
577 let active = detector.active_operations.read().await;
578 assert!(!active.contains_key("op-a"));
579 assert!(active.contains_key("op-b"));
580 drop(active);
581 kept.end().await;
582 detector.get_stats().await;
583 assert!(detector.active_operations.read().await.is_empty());
584 });
585 }
586
587 #[tokio::test]
588 async fn queued_end_records_duration_at_end_not_at_drain() {
589 let detector = TimeoutDetector::new();
590 let handle = detector.start_operation("op".to_owned(), OperationType::ApiCall).await;
591 handle.end().await;
592 sleep(Duration::from_millis(200)).await;
593
594 let stats = detector.get_stats().await;
595
596 assert!(stats.average_timeout_duration < Duration::from_millis(100));
597 }
598
599 #[tokio::test]
600 async fn test_calculate_retry_delay() {
601 let detector = TimeoutDetector::new();
602
603 let delay = detector.calculate_retry_delay(&OperationType::ApiCall, 0).await;
604 assert!(delay >= Duration::from_millis(200)); let delay2 = detector.calculate_retry_delay(&OperationType::ApiCall, 1).await;
607 assert!(delay2 > delay); }
609
610 #[tokio::test]
611 async fn test_misconfiguration_does_not_retry_even_when_pattern_matches() {
612 let detector = TimeoutDetector::new();
613 let config = TimeoutConfig {
614 retry_on_errors: vec!["network".to_string()],
615 ..Default::default()
616 };
617 detector.set_config(OperationType::ApiCall, config).await;
618
619 let error = anyhow::anyhow!("network error: invalid endpoint in base_url");
620 assert!(!detector.should_retry(&OperationType::ApiCall, &error, 0).await);
621 }
622}