llm-shield-models 0.1.0

ML model infrastructure for LLM Shield
Documentation
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
//! Common Types for ML Model Integration
//!
//! ## SPARC Phase 1: Specification
//!
//! This module defines common types for ML model configuration and hybrid
//! detection mode that combines heuristic and ML approaches.
//!
//! ## Design Principles
//!
//! - **Flexibility**: Support multiple model variants (FP32, FP16, INT8)
//! - **Graceful Degradation**: Fallback to heuristics if ML fails
//! - **Performance**: Cache-aware configuration
//! - **Observability**: Rich metadata for monitoring

use crate::registry::ModelVariant;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::Duration;

/// ML detection configuration for scanners
///
/// ## Fields
///
/// - `enabled`: Whether ML detection is enabled
/// - `model_variant`: Model precision (FP32, FP16, INT8)
/// - `threshold`: Detection threshold (0.0 to 1.0)
/// - `fallback_to_heuristic`: Use heuristic if ML fails
/// - `cache_enabled`: Enable result caching
/// - `cache_config`: Cache configuration
///
/// ## Recommended Configurations
///
/// **Production (balanced)**:
/// ```rust,ignore
/// MLConfig {
///     enabled: true,
///     model_variant: ModelVariant::FP16,
///     threshold: 0.5,
///     fallback_to_heuristic: true,
///     cache_enabled: true,
///     cache_config: CacheSettings::default(),
/// }
/// ```
///
/// **High accuracy**:
/// ```rust,ignore
/// MLConfig {
///     enabled: true,
///     model_variant: ModelVariant::FP32,
///     threshold: 0.3,  // Lower threshold = more sensitive
///     fallback_to_heuristic: false,  // Require ML
///     cache_enabled: true,
///     cache_config: CacheSettings::default(),
/// }
/// ```
///
/// **Edge/Mobile**:
/// ```rust,ignore
/// MLConfig {
///     enabled: true,
///     model_variant: ModelVariant::INT8,
///     threshold: 0.6,
///     fallback_to_heuristic: true,
///     cache_enabled: true,
///     cache_config: CacheSettings {
///         max_size: 100,  // Smaller cache
///         ttl: Duration::from_secs(600),
///     },
/// }
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MLConfig {
    /// Whether ML detection is enabled
    pub enabled: bool,

    /// Model variant to use (FP32, FP16, INT8)
    pub model_variant: ModelVariant,

    /// Detection threshold (0.0 to 1.0)
    /// - Higher = fewer false positives, more false negatives
    /// - Lower = fewer false negatives, more false positives
    /// - Recommended: 0.5 for balanced results
    pub threshold: f32,

    /// Use heuristic detection if ML fails or is unavailable
    pub fallback_to_heuristic: bool,

    /// Enable result caching for repeated inputs
    pub cache_enabled: bool,

    /// Cache settings
    pub cache_config: CacheSettings,

    /// Additional model-specific configuration
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

impl Default for MLConfig {
    fn default() -> Self {
        Self {
            enabled: false, // Opt-in for ML
            model_variant: ModelVariant::FP16,
            threshold: 0.5,
            fallback_to_heuristic: true,
            cache_enabled: true,
            cache_config: CacheSettings::default(),
            extra: HashMap::new(),
        }
    }
}

impl MLConfig {
    /// Create a new ML configuration
    pub fn new(model_variant: ModelVariant, threshold: f32) -> Self {
        Self {
            enabled: true,
            model_variant,
            threshold,
            ..Default::default()
        }
    }

    /// Create ML configuration for production use
    ///
    /// - FP16 model (balanced speed/accuracy)
    /// - 0.5 threshold (balanced sensitivity)
    /// - Heuristic fallback enabled
    /// - Caching enabled with 1000 entries, 1 hour TTL
    pub fn production() -> Self {
        Self {
            enabled: true,
            model_variant: ModelVariant::FP16,
            threshold: 0.5,
            fallback_to_heuristic: true,
            cache_enabled: true,
            cache_config: CacheSettings::production(),
            extra: HashMap::new(),
        }
    }

    /// Create ML configuration for edge/mobile deployment
    ///
    /// - INT8 model (smallest size)
    /// - 0.6 threshold (fewer false positives)
    /// - Heuristic fallback enabled
    /// - Smaller cache (100 entries, 10 minutes TTL)
    pub fn edge() -> Self {
        Self {
            enabled: true,
            model_variant: ModelVariant::INT8,
            threshold: 0.6,
            fallback_to_heuristic: true,
            cache_enabled: true,
            cache_config: CacheSettings::edge(),
            extra: HashMap::new(),
        }
    }

    /// Create ML configuration for high accuracy
    ///
    /// - FP32 model (highest accuracy)
    /// - 0.3 threshold (very sensitive)
    /// - No heuristic fallback
    /// - Aggressive caching
    pub fn high_accuracy() -> Self {
        Self {
            enabled: true,
            model_variant: ModelVariant::FP32,
            threshold: 0.3,
            fallback_to_heuristic: false,
            cache_enabled: true,
            cache_config: CacheSettings::aggressive(),
            extra: HashMap::new(),
        }
    }

    /// Disable ML detection (heuristic-only mode)
    pub fn disabled() -> Self {
        Self {
            enabled: false,
            ..Default::default()
        }
    }

    /// Validate configuration
    pub fn validate(&self) -> Result<(), String> {
        if !(0.0..=1.0).contains(&self.threshold) {
            return Err(format!(
                "Threshold must be between 0.0 and 1.0, got {}",
                self.threshold
            ));
        }
        Ok(())
    }
}

/// Cache settings for ML result caching
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CacheSettings {
    /// Maximum number of cached entries (LRU eviction)
    pub max_size: usize,

    /// Time-to-live for cache entries
    #[serde(with = "duration_serde")]
    pub ttl: Duration,
}

impl Default for CacheSettings {
    fn default() -> Self {
        Self {
            max_size: 1000,
            ttl: Duration::from_secs(3600), // 1 hour
        }
    }
}

impl CacheSettings {
    /// Production cache settings
    /// - 1000 entries
    /// - 1 hour TTL
    pub fn production() -> Self {
        Self {
            max_size: 1000,
            ttl: Duration::from_secs(3600),
        }
    }

    /// Edge/mobile cache settings (smaller)
    /// - 100 entries
    /// - 10 minutes TTL
    pub fn edge() -> Self {
        Self {
            max_size: 100,
            ttl: Duration::from_secs(600),
        }
    }

    /// Aggressive caching for high-traffic scenarios
    /// - 10000 entries
    /// - 2 hours TTL
    pub fn aggressive() -> Self {
        Self {
            max_size: 10000,
            ttl: Duration::from_secs(7200),
        }
    }

    /// Minimal caching (for testing or memory-constrained environments)
    /// - 10 entries
    /// - 1 minute TTL
    pub fn minimal() -> Self {
        Self {
            max_size: 10,
            ttl: Duration::from_secs(60),
        }
    }

    /// Disable caching
    pub fn disabled() -> Self {
        Self {
            max_size: 0,
            ttl: Duration::from_secs(0),
        }
    }
}

/// Hybrid detection mode
///
/// ## Specification
///
/// Hybrid mode combines fast heuristic detection with accurate ML detection:
///
/// 1. **Heuristic Pre-filter**: Fast pattern-based detection (0.01ms)
///    - If obviously safe → return safe (60-70% of inputs)
///    - If obviously malicious → return malicious (5-10% of inputs)
///    - If ambiguous → proceed to ML (20-30% of inputs)
///
/// 2. **ML Detection**: Accurate but slower (50-150ms)
///    - Only called for ambiguous cases
///    - Results are cached
///    - Falls back to heuristic on error (if enabled)
///
/// ## Performance Impact
///
/// - Pure heuristic: ~15,500 req/sec
/// - Pure ML: ~150 req/sec
/// - Hybrid: ~2,000 req/sec (10x faster than pure ML)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum HybridMode {
    /// Only use heuristic detection (no ML)
    HeuristicOnly,

    /// Only use ML detection (no heuristic pre-filter)
    MLOnly,

    /// Use heuristic pre-filter, then ML for ambiguous cases
    Hybrid,

    /// Use both and combine results (max risk score)
    Both,
}

impl Default for HybridMode {
    fn default() -> Self {
        Self::Hybrid
    }
}

/// Detection method used for a scan result
///
/// Tracks which method(s) were used to generate the result.
/// Useful for monitoring and debugging.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum DetectionMethod {
    /// Only heuristic pattern matching was used
    #[serde(rename = "heuristic")]
    Heuristic,

    /// Only ML model inference was used
    #[serde(rename = "ml")]
    ML,

    /// Heuristic pre-filter detected safe/malicious
    #[serde(rename = "heuristic_short_circuit")]
    HeuristicShortCircuit,

    /// ML was attempted but failed, fell back to heuristic
    #[serde(rename = "ml_fallback_to_heuristic")]
    MLFallbackToHeuristic,

    /// Both heuristic and ML were used, results combined
    #[serde(rename = "hybrid_both")]
    HybridBoth,
}

/// Inference performance metrics
///
/// ## Specification
///
/// These metrics should be collected and reported for monitoring:
/// - Latency (p50, p95, p99)
/// - Throughput
/// - Cache hit rate
/// - Heuristic filter rate
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct InferenceMetrics {
    /// Total inference calls
    pub total_calls: u64,

    /// ML inference calls (not cached)
    pub ml_calls: u64,

    /// Heuristic pre-filter calls
    pub heuristic_calls: u64,

    /// Cache hits
    pub cache_hits: u64,

    /// Heuristic short-circuits (didn't need ML)
    pub heuristic_short_circuits: u64,

    /// Total inference time (milliseconds)
    pub total_inference_time_ms: u64,

    /// ML inference errors
    pub ml_errors: u64,

    /// Fallback to heuristic count
    pub fallback_count: u64,
}

impl InferenceMetrics {
    /// Calculate cache hit rate (0.0 to 1.0)
    pub fn cache_hit_rate(&self) -> f32 {
        if self.total_calls == 0 {
            0.0
        } else {
            self.cache_hits as f32 / self.total_calls as f32
        }
    }

    /// Calculate heuristic filter rate (% of inputs filtered by heuristic)
    pub fn heuristic_filter_rate(&self) -> f32 {
        if self.total_calls == 0 {
            0.0
        } else {
            self.heuristic_short_circuits as f32 / self.total_calls as f32
        }
    }

    /// Calculate average inference time (milliseconds)
    pub fn avg_inference_time_ms(&self) -> f32 {
        if self.total_calls == 0 {
            0.0
        } else {
            self.total_inference_time_ms as f32 / self.total_calls as f32
        }
    }

    /// Calculate ML error rate
    pub fn ml_error_rate(&self) -> f32 {
        if self.ml_calls == 0 {
            0.0
        } else {
            self.ml_errors as f32 / self.ml_calls as f32
        }
    }
}

/// Serialization helper for Duration
mod duration_serde {
    use serde::{Deserialize, Deserializer, Serialize, Serializer};
    use std::time::Duration;

    pub fn serialize<S>(duration: &Duration, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        duration.as_secs().serialize(serializer)
    }

    pub fn deserialize<'de, D>(deserializer: D) -> Result<Duration, D::Error>
    where
        D: Deserializer<'de>,
    {
        let secs = u64::deserialize(deserializer)?;
        Ok(Duration::from_secs(secs))
    }
}

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

    #[test]
    fn test_ml_config_default() {
        let config = MLConfig::default();
        assert!(!config.enabled);
        assert_eq!(config.model_variant, ModelVariant::FP16);
        assert_eq!(config.threshold, 0.5);
        assert!(config.fallback_to_heuristic);
        assert!(config.cache_enabled);
    }

    #[test]
    fn test_ml_config_production() {
        let config = MLConfig::production();
        assert!(config.enabled);
        assert_eq!(config.model_variant, ModelVariant::FP16);
        assert_eq!(config.threshold, 0.5);
        assert!(config.fallback_to_heuristic);
        assert!(config.cache_enabled);
    }

    #[test]
    fn test_ml_config_edge() {
        let config = MLConfig::edge();
        assert!(config.enabled);
        assert_eq!(config.model_variant, ModelVariant::INT8);
        assert_eq!(config.threshold, 0.6);
        assert_eq!(config.cache_config.max_size, 100);
    }

    #[test]
    fn test_ml_config_high_accuracy() {
        let config = MLConfig::high_accuracy();
        assert!(config.enabled);
        assert_eq!(config.model_variant, ModelVariant::FP32);
        assert_eq!(config.threshold, 0.3);
        assert!(!config.fallback_to_heuristic);
    }

    #[test]
    fn test_ml_config_disabled() {
        let config = MLConfig::disabled();
        assert!(!config.enabled);
    }

    #[test]
    fn test_ml_config_validation() {
        let mut config = MLConfig::default();
        assert!(config.validate().is_ok());

        config.threshold = 1.5;
        assert!(config.validate().is_err());

        config.threshold = -0.1;
        assert!(config.validate().is_err());

        config.threshold = 0.0;
        assert!(config.validate().is_ok());

        config.threshold = 1.0;
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_cache_settings_default() {
        let settings = CacheSettings::default();
        assert_eq!(settings.max_size, 1000);
        assert_eq!(settings.ttl, Duration::from_secs(3600));
    }

    #[test]
    fn test_cache_settings_production() {
        let settings = CacheSettings::production();
        assert_eq!(settings.max_size, 1000);
        assert_eq!(settings.ttl, Duration::from_secs(3600));
    }

    #[test]
    fn test_cache_settings_edge() {
        let settings = CacheSettings::edge();
        assert_eq!(settings.max_size, 100);
        assert_eq!(settings.ttl, Duration::from_secs(600));
    }

    #[test]
    fn test_cache_settings_aggressive() {
        let settings = CacheSettings::aggressive();
        assert_eq!(settings.max_size, 10000);
        assert_eq!(settings.ttl, Duration::from_secs(7200));
    }

    #[test]
    fn test_cache_settings_minimal() {
        let settings = CacheSettings::minimal();
        assert_eq!(settings.max_size, 10);
        assert_eq!(settings.ttl, Duration::from_secs(60));
    }

    #[test]
    fn test_cache_settings_disabled() {
        let settings = CacheSettings::disabled();
        assert_eq!(settings.max_size, 0);
        assert_eq!(settings.ttl, Duration::from_secs(0));
    }

    #[test]
    fn test_hybrid_mode_default() {
        assert_eq!(HybridMode::default(), HybridMode::Hybrid);
    }

    #[test]
    fn test_inference_metrics_default() {
        let metrics = InferenceMetrics::default();
        assert_eq!(metrics.total_calls, 0);
        assert_eq!(metrics.cache_hit_rate(), 0.0);
        assert_eq!(metrics.heuristic_filter_rate(), 0.0);
        assert_eq!(metrics.avg_inference_time_ms(), 0.0);
        assert_eq!(metrics.ml_error_rate(), 0.0);
    }

    #[test]
    fn test_inference_metrics_calculations() {
        let metrics = InferenceMetrics {
            total_calls: 100,
            ml_calls: 40,
            heuristic_calls: 100,
            cache_hits: 30,
            heuristic_short_circuits: 60,
            total_inference_time_ms: 5000,
            ml_errors: 4,
            fallback_count: 4,
        };

        assert_eq!(metrics.cache_hit_rate(), 0.3);
        assert_eq!(metrics.heuristic_filter_rate(), 0.6);
        assert_eq!(metrics.avg_inference_time_ms(), 50.0);
        assert_eq!(metrics.ml_error_rate(), 0.1);
    }

    #[test]
    fn test_ml_config_serialization() {
        let config = MLConfig::production();
        let json = serde_json::to_string(&config).unwrap();
        let deserialized: MLConfig = serde_json::from_str(&json).unwrap();

        assert_eq!(config.enabled, deserialized.enabled);
        assert_eq!(config.threshold, deserialized.threshold);
        assert_eq!(config.cache_config.max_size, deserialized.cache_config.max_size);
    }

    #[test]
    fn test_detection_method_serialization() {
        let method = DetectionMethod::ML;
        let json = serde_json::to_string(&method).unwrap();
        assert_eq!(json, "\"ml\"");

        let deserialized: DetectionMethod = serde_json::from_str(&json).unwrap();
        assert_eq!(method, deserialized);
    }

    #[test]
    fn test_hybrid_mode_serialization() {
        let mode = HybridMode::Hybrid;
        let json = serde_json::to_string(&mode).unwrap();
        assert_eq!(json, "\"hybrid\"");

        let deserialized: HybridMode = serde_json::from_str(&json).unwrap();
        assert_eq!(mode, deserialized);
    }
}