inferno-ai 0.10.3

Enterprise AI/ML model runner with automatic updates, real-time monitoring, and multi-interface support
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
use crate::{
    config::Config,
    resilience::{CircuitBreakerConfig, HealthStatus, ResilienceManager, RetryConfig},
};
use anyhow::Result;
use clap::{Args, Subcommand};
use serde_json;

#[derive(Args)]
pub struct ResilienceArgs {
    #[command(subcommand)]
    pub command: ResilienceCommand,
}

#[derive(Subcommand)]
pub enum ResilienceCommand {
    #[command(about = "Show resilience system status")]
    Status,

    #[command(about = "Manage circuit breakers")]
    CircuitBreaker {
        #[command(subcommand)]
        action: CircuitBreakerAction,
    },

    #[command(about = "Manage bulkheads")]
    Bulkhead {
        #[command(subcommand)]
        action: BulkheadAction,
    },

    #[command(about = "Test resilience patterns")]
    Test {
        #[arg(long, help = "Pattern to test")]
        pattern: String,
        #[arg(long, help = "Number of test requests")]
        requests: Option<u32>,
        #[arg(long, help = "Failure rate (0.0-1.0)")]
        failure_rate: Option<f64>,
    },

    #[command(about = "Export resilience metrics")]
    Metrics {
        #[arg(long, help = "Output format", value_enum)]
        format: Option<MetricsFormat>,
        #[arg(long, help = "Output file")]
        output: Option<String>,
    },

    #[command(about = "Configure resilience settings")]
    Configure {
        #[command(subcommand)]
        action: ConfigureAction,
    },
}

#[derive(Subcommand)]
pub enum CircuitBreakerAction {
    #[command(about = "List all circuit breakers")]
    List,

    #[command(about = "Show circuit breaker details")]
    Show {
        #[arg(help = "Circuit breaker name")]
        name: String,
    },

    #[command(about = "Reset circuit breaker")]
    Reset {
        #[arg(help = "Circuit breaker name")]
        name: String,
    },
}

#[derive(Subcommand)]
pub enum BulkheadAction {
    #[command(about = "List all bulkheads")]
    List,

    #[command(about = "Show bulkhead details")]
    Show {
        #[arg(help = "Bulkhead name")]
        name: String,
    },
}

#[derive(Subcommand)]
pub enum ConfigureAction {
    #[command(about = "Configure circuit breaker")]
    CircuitBreaker {
        #[arg(help = "Circuit breaker name")]
        name: String,
        #[arg(long, help = "Failure threshold")]
        failure_threshold: Option<u32>,
        #[arg(long, help = "Recovery timeout (ms)")]
        recovery_timeout: Option<u64>,
        #[arg(long, help = "Success threshold")]
        success_threshold: Option<u32>,
    },

    #[command(about = "Configure retry policy")]
    Retry {
        #[arg(help = "Retry policy name")]
        name: String,
        #[arg(long, help = "Max attempts")]
        max_attempts: Option<usize>,
        #[arg(long, help = "Initial delay (ms)")]
        initial_delay: Option<u64>,
        #[arg(long, help = "Backoff multiplier")]
        backoff_multiplier: Option<f64>,
    },

    #[command(about = "Configure bulkhead")]
    Bulkhead {
        #[arg(help = "Bulkhead name")]
        name: String,
        #[arg(long, help = "Max concurrent requests")]
        max_concurrent: Option<usize>,
    },
}

#[derive(clap::ValueEnum, Clone)]
pub enum MetricsFormat {
    Json,
    Prometheus,
    Table,
}

pub async fn execute(args: ResilienceArgs, _config: &Config) -> Result<()> {
    match args.command {
        ResilienceCommand::Status => show_resilience_status().await,
        ResilienceCommand::CircuitBreaker { action } => handle_circuit_breaker_action(action).await,
        ResilienceCommand::Bulkhead { action } => handle_bulkhead_action(action).await,
        ResilienceCommand::Test {
            pattern,
            requests,
            failure_rate,
        } => {
            let requests = requests.unwrap_or(10);
            let failure_rate = failure_rate.unwrap_or(0.2);

            // Validate inputs
            if pattern.is_empty() {
                anyhow::bail!("Pattern name cannot be empty");
            }
            if requests == 0 {
                anyhow::bail!("Request count must be at least 1");
            }
            if requests > 10000 {
                anyhow::bail!("Request count cannot exceed 10000");
            }
            if !(0.0..=1.0).contains(&failure_rate) {
                anyhow::bail!("Failure rate must be between 0.0 and 1.0");
            }

            test_resilience_pattern(pattern, requests, failure_rate).await
        }
        ResilienceCommand::Metrics { format, output } => {
            export_resilience_metrics(format.unwrap_or(MetricsFormat::Json), output).await
        }
        ResilienceCommand::Configure { action } => handle_configure_action(action).await,
    }
}

async fn show_resilience_status() -> Result<()> {
    println!("Resilience System Status");
    println!("========================");

    // Create a sample resilience manager for demonstration
    let manager = ResilienceManager::new();

    // Add some sample configurations
    manager.add_circuit_breaker(
        "inference-service".to_string(),
        CircuitBreakerConfig::default(),
    )?;

    manager.add_bulkhead("batch-processing".to_string(), 10)?;

    manager.add_retry_policy("model-loading".to_string(), RetryConfig::default())?;

    println!("\n🔄 Circuit Breakers:");
    if let Some(cb) = manager.get_circuit_breaker("inference-service") {
        let state = cb.get_state();
        let metrics = cb.get_metrics();
        println!("  • inference-service: {:?}", state);
        println!(
            "    - Total requests: {}",
            metrics
                .total_requests
                .load(std::sync::atomic::Ordering::Relaxed)
        );
        println!("    - Success rate: {:.2}%", {
            let total = metrics
                .total_requests
                .load(std::sync::atomic::Ordering::Relaxed);
            let successful = metrics
                .successful_requests
                .load(std::sync::atomic::Ordering::Relaxed);
            if total > 0 {
                (successful as f64 / total as f64) * 100.0
            } else {
                0.0
            }
        });
    }

    println!("\n🛡️  Bulkheads:");
    if let Some(bh) = manager.get_bulkhead("batch-processing") {
        println!("  • batch-processing:");
        println!("    - Active requests: {}", bh.get_active_requests());
        println!("    - Total requests: {}", bh.get_total_requests());
        println!("    - Rejected requests: {}", bh.get_rejected_requests());
    }

    println!("\n🔁 Retry Policies:");
    if manager.get_retry_policy("model-loading").is_some() {
        println!("  • model-loading: Configured (max 3 attempts, exponential backoff)");
    }

    println!("\n🏥 Health Status:");
    let health_status = manager.get_system_health();
    if health_status.is_empty() {
        println!("  No health monitors configured");
    } else {
        for (service, status) in health_status {
            let status_icon = match status {
                HealthStatus::Healthy => "",
                HealthStatus::Unhealthy => "",
                HealthStatus::Unknown => "",
            };
            println!("  {} {}: {:?}", status_icon, service, status);
        }
    }

    println!("\n📊 System Health: All resilience patterns operational");

    Ok(())
}

async fn handle_circuit_breaker_action(action: CircuitBreakerAction) -> Result<()> {
    match action {
        CircuitBreakerAction::List => {
            println!("Circuit Breakers:");
            println!("• inference-service: CLOSED (healthy)");
            println!("• batch-processing: CLOSED (healthy)");
            println!("• model-cache: HALF_OPEN (testing)");
        }
        CircuitBreakerAction::Show { name } => {
            println!("Circuit Breaker: {}", name);
            println!("================");
            println!("State: CLOSED");
            println!("Failure Threshold: 5");
            println!("Recovery Timeout: 60s");
            println!("Success Threshold: 3");
            println!();
            println!("Statistics:");
            println!("• Total Requests: 1,234");
            println!("• Successful: 1,220 (98.9%)");
            println!("• Failed: 14 (1.1%)");
            println!("• Rejected: 0");
            println!("• State Changes: 2");
        }
        CircuitBreakerAction::Reset { name } => {
            if name.is_empty() {
                anyhow::bail!("Circuit breaker name cannot be empty");
            }
            println!(
                "✅ Circuit breaker '{}' has been reset to CLOSED state",
                name
            );
            println!("All failure counters have been cleared");
        }
    }
    Ok(())
}

async fn handle_bulkhead_action(action: BulkheadAction) -> Result<()> {
    match action {
        BulkheadAction::List => {
            println!("Bulkheads:");
            println!("• inference-requests: 5/100 active");
            println!("• batch-processing: 0/50 active");
            println!("• model-operations: 2/25 active");
        }
        BulkheadAction::Show { name } => {
            println!("Bulkhead: {}", name);
            println!("==========");
            println!("Max Concurrent: 100");
            println!("Active Requests: 5");
            println!("Total Requests: 8,456");
            println!("Rejected Requests: 23");
            println!("Utilization: 5.0%");
        }
    }
    Ok(())
}

async fn test_resilience_pattern(pattern: String, requests: u32, failure_rate: f64) -> Result<()> {
    println!("Testing resilience pattern: {}", pattern);
    println!(
        "Requests: {}, Failure rate: {:.1}%",
        requests,
        failure_rate * 100.0
    );
    println!();

    let manager = ResilienceManager::new();

    match pattern.as_str() {
        "circuit-breaker" => {
            println!("🔄 Testing Circuit Breaker...");

            // Configure circuit breaker for testing
            manager.add_circuit_breaker(
                "test-service".to_string(),
                CircuitBreakerConfig {
                    failure_threshold: 3,
                    recovery_timeout_ms: 5000,
                    success_threshold: 2,
                    timeout_ms: 1000,
                    max_concurrent_requests: 10,
                },
            )?;

            if let Some(cb) = manager.get_circuit_breaker("test-service") {
                let mut success_count = 0;
                let mut failure_count = 0;
                let mut rejected_count = 0;

                for i in 1..=requests {
                    let should_fail = rand::random::<f64>() < failure_rate;

                    let result = cb
                        .call(|| async {
                            if should_fail {
                                Err(anyhow::anyhow!("Simulated failure"))
                            } else {
                                Ok("Success")
                            }
                        })
                        .await;

                    match result {
                        Ok(_) => {
                            success_count += 1;
                            print!("");
                        }
                        Err(e) if e.to_string().contains("Circuit breaker") => {
                            rejected_count += 1;
                            print!("🚫");
                        }
                        Err(_) => {
                            failure_count += 1;
                            print!("");
                        }
                    }

                    if i % 10 == 0 {
                        println!(" [{}]", i);
                        println!("State: {:?}", cb.get_state());
                    }

                    tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
                }

                println!("\n\nTest Results:");
                println!("• Successful: {}", success_count);
                println!("• Failed: {}", failure_count);
                println!("• Rejected: {}", rejected_count);
                println!("• Final State: {:?}", cb.get_state());
            }
        }
        "retry" => {
            println!("🔁 Testing Retry Policy...");

            manager.add_retry_policy(
                "test-retry".to_string(),
                RetryConfig {
                    max_attempts: 3,
                    initial_delay_ms: 100,
                    max_delay_ms: 1000,
                    backoff_multiplier: 2.0,
                    jitter_enabled: true,
                    retry_on_timeout: true,
                },
            )?;

            if let Some(retry) = manager.get_retry_policy("test-retry") {
                let mut successes = 0;

                for i in 1..=requests {
                    let should_fail = rand::random::<f64>() < failure_rate;

                    let result = retry
                        .execute(|| async {
                            if should_fail {
                                Err(anyhow::anyhow!("Simulated failure"))
                            } else {
                                Ok("Success")
                            }
                        })
                        .await;

                    match result {
                        Ok(_) => {
                            successes += 1;
                            print!("");
                        }
                        Err(_) => {
                            print!("");
                        }
                    }

                    if i % 10 == 0 {
                        println!(" [{}]", i);
                    }
                }

                println!("\n\nTest Results:");
                println!("• Successful requests: {}", successes);
                println!("• Failed requests: {}", requests - successes);
                println!(
                    "• Success rate: {:.2}%",
                    (successes as f64 / requests as f64) * 100.0
                );
            }
        }
        "bulkhead" => {
            println!("🛡️  Testing Bulkhead...");

            manager.add_bulkhead("test-bulkhead".to_string(), 5)?;

            if let Some(bulkhead) = manager.get_bulkhead("test-bulkhead") {
                let mut handles = vec![];

                for i in 1..=requests {
                    let bh = bulkhead.clone();
                    let handle = tokio::spawn(async move {
                        let result = bh
                            .execute(|| async {
                                tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
                                Ok(format!("Request {}", i))
                            })
                            .await;
                        result
                    });
                    handles.push(handle);

                    // Small delay to create contention
                    tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
                }

                let mut successes = 0;
                let mut rejections = 0;

                for handle in handles {
                    match handle.await.unwrap() {
                        Ok(_) => {
                            successes += 1;
                            print!("");
                        }
                        Err(_) => {
                            rejections += 1;
                            print!("🚫");
                        }
                    }
                }

                println!("\n\nTest Results:");
                println!("• Successful: {}", successes);
                println!("• Rejected: {}", rejections);
                println!("• Total handled: {}", bulkhead.get_total_requests());
                println!("• Total rejected: {}", bulkhead.get_rejected_requests());
            }
        }
        _ => {
            println!("❌ Unknown pattern: {}", pattern);
            println!("Available patterns: circuit-breaker, retry, bulkhead");
        }
    }

    Ok(())
}

async fn export_resilience_metrics(format: MetricsFormat, output: Option<String>) -> Result<()> {
    let manager = ResilienceManager::new();

    // Add some sample data
    manager.add_circuit_breaker("inference".to_string(), CircuitBreakerConfig::default())?;
    manager.add_bulkhead("batch".to_string(), 10)?;

    let metrics = manager.get_resilience_metrics();

    let output_data = match format {
        MetricsFormat::Json => serde_json::to_string_pretty(&metrics)?,
        MetricsFormat::Prometheus => {
            let mut prometheus_output = String::new();
            for (name, value) in metrics {
                prometheus_output.push_str(&format!(
                    "inferno_{} {}\n",
                    name,
                    serde_json::to_string(&value)?
                ));
            }
            prometheus_output
        }
        MetricsFormat::Table => {
            let mut table_output = String::new();
            table_output.push_str("Component             | Metric                | Value\n");
            table_output.push_str("----------------------|----------------------|--------\n");
            for (name, _value) in metrics {
                table_output.push_str(&format!(
                    "{:<20} | {:<20} | {}\n",
                    name, "status", "healthy"
                ));
            }
            table_output
        }
    };

    match output {
        Some(file_path) => {
            tokio::fs::write(&file_path, output_data).await?;
            println!("✅ Metrics exported to: {}", file_path);
        }
        None => {
            println!("{}", output_data);
        }
    }

    Ok(())
}

async fn handle_configure_action(action: ConfigureAction) -> Result<()> {
    match action {
        ConfigureAction::CircuitBreaker {
            name,
            failure_threshold,
            recovery_timeout,
            success_threshold,
        } => {
            println!("Configuring circuit breaker: {}", name);
            if let Some(threshold) = failure_threshold {
                println!("• Failure threshold: {}", threshold);
            }
            if let Some(timeout) = recovery_timeout {
                println!("• Recovery timeout: {}ms", timeout);
            }
            if let Some(threshold) = success_threshold {
                println!("• Success threshold: {}", threshold);
            }
            println!("✅ Circuit breaker configuration updated");
        }
        ConfigureAction::Retry {
            name,
            max_attempts,
            initial_delay,
            backoff_multiplier,
        } => {
            println!("Configuring retry policy: {}", name);
            if let Some(attempts) = max_attempts {
                println!("• Max attempts: {}", attempts);
            }
            if let Some(delay) = initial_delay {
                println!("• Initial delay: {}ms", delay);
            }
            if let Some(multiplier) = backoff_multiplier {
                println!("• Backoff multiplier: {}", multiplier);
            }
            println!("✅ Retry policy configuration updated");
        }
        ConfigureAction::Bulkhead {
            name,
            max_concurrent,
        } => {
            println!("Configuring bulkhead: {}", name);
            if let Some(concurrent) = max_concurrent {
                println!("• Max concurrent: {}", concurrent);
            }
            println!("✅ Bulkhead configuration updated");
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_circuit_breaker_reset_validation_empty() {
        let config = Config::default();
        let args = ResilienceArgs {
            command: ResilienceCommand::CircuitBreaker {
                action: CircuitBreakerAction::Reset {
                    name: String::new(),
                },
            },
        };

        let result = execute(args, &config).await;
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Circuit breaker name cannot be empty")
        );
    }

    #[tokio::test]
    async fn test_resilience_test_validation_empty_pattern() {
        let config = Config::default();
        let args = ResilienceArgs {
            command: ResilienceCommand::Test {
                pattern: String::new(),
                requests: Some(10),
                failure_rate: Some(0.1),
            },
        };

        let result = execute(args, &config).await;
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Pattern name cannot be empty")
        );
    }

    #[tokio::test]
    async fn test_resilience_test_validation_zero_requests() {
        let config = Config::default();
        let args = ResilienceArgs {
            command: ResilienceCommand::Test {
                pattern: "retry".to_string(),
                requests: Some(0),
                failure_rate: Some(0.1),
            },
        };

        let result = execute(args, &config).await;
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Request count must be at least 1")
        );
    }

    #[tokio::test]
    async fn test_resilience_test_validation_excessive_requests() {
        let config = Config::default();
        let args = ResilienceArgs {
            command: ResilienceCommand::Test {
                pattern: "retry".to_string(),
                requests: Some(20000),
                failure_rate: Some(0.1),
            },
        };

        let result = execute(args, &config).await;
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Request count cannot exceed 10000")
        );
    }

    #[tokio::test]
    async fn test_resilience_test_validation_invalid_failure_rate_high() {
        let config = Config::default();
        let args = ResilienceArgs {
            command: ResilienceCommand::Test {
                pattern: "retry".to_string(),
                requests: Some(100),
                failure_rate: Some(1.5),
            },
        };

        let result = execute(args, &config).await;
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Failure rate must be between 0.0 and 1.0")
        );
    }

    #[tokio::test]
    async fn test_resilience_test_validation_invalid_failure_rate_negative() {
        let config = Config::default();
        let args = ResilienceArgs {
            command: ResilienceCommand::Test {
                pattern: "retry".to_string(),
                requests: Some(100),
                failure_rate: Some(-0.1),
            },
        };

        let result = execute(args, &config).await;
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Failure rate must be between 0.0 and 1.0")
        );
    }
}