symbi-runtime 0.6.1

Agent Runtime System for the Symbi platform
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
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
//! Core routing engine implementation

use async_trait::async_trait;
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::RwLock;

use super::config::RoutingConfig;
use super::decision::{
    RouteDecision, RoutingContext, RoutingStatistics, ModelRequest, ModelResponse, 
    FinishReason, TokenUsage, LLMProvider
};
use super::error::RoutingError;
use super::policy::PolicyEvaluator;
use super::classifier::TaskClassifier;
use super::confidence::{ConfidenceMonitorTrait, NoOpConfidenceMonitor};
use crate::models::{ModelCatalog, SlmRunnerError};
use crate::logging::{ModelLogger, ModelInteractionType, RequestData, ResponseData, TokenUsage as LogTokenUsage};

/// Core routing engine trait for SLM-first architecture
#[async_trait]
pub trait RoutingEngine: Send + Sync {
    /// Route a model request based on configured policies
    async fn route_request(
        &self,
        context: &RoutingContext,
    ) -> Result<RouteDecision, RoutingError>;
    
    /// Execute the routing decision and handle fallbacks
    async fn execute_with_routing(
        &self,
        context: RoutingContext,
        request: ModelRequest,
    ) -> Result<ModelResponse, RoutingError>;
    
    /// Validate routing policies
    fn validate_policies(&self) -> Result<(), RoutingError>;
    
    /// Get routing statistics
    async fn get_routing_stats(&self) -> RoutingStatistics;
    
    /// Update routing configuration
    async fn update_config(&self, config: RoutingConfig) -> Result<(), RoutingError>;
}

/// Default implementation of the routing engine
pub struct DefaultRoutingEngine {
    /// Policy evaluator for making routing decisions
    policy_evaluator: Arc<RwLock<PolicyEvaluator>>,
    /// Model catalog for SLM information
    model_catalog: Arc<ModelCatalog>,
    /// Confidence monitor for evaluating SLM responses
    confidence_monitor: Arc<RwLock<Box<dyn ConfidenceMonitorTrait>>>,
    /// Optional model logger for audit trails
    model_logger: Option<Arc<ModelLogger>>,
    /// Routing statistics
    statistics: Arc<RwLock<RoutingStatistics>>,
    /// Configuration
    config: Arc<RwLock<RoutingConfig>>,
    /// LLM client pool for fallback
    llm_clients: Arc<LLMClientPool>,
}

/// Pool of LLM clients for different providers
struct LLMClientPool {
    clients: HashMap<String, Box<dyn LLMClient>>,
}

/// Trait for LLM clients
#[async_trait]
trait LLMClient: Send + Sync {
    async fn execute_request(
        &self,
        request: &ModelRequest,
        provider: &LLMProvider,
    ) -> Result<ModelResponse, RoutingError>;
}

/// Mock LLM client implementation
#[derive(Debug)]
struct MockLLMClient;

#[async_trait]
impl LLMClient for MockLLMClient {
    async fn execute_request(
        &self,
        request: &ModelRequest,
        provider: &LLMProvider,
    ) -> Result<ModelResponse, RoutingError> {
        // Mock implementation - in real system would call actual LLM APIs
        tokio::time::sleep(Duration::from_millis(100)).await;
        
        Ok(ModelResponse {
            content: format!("LLM response to: {}", request.prompt),
            finish_reason: FinishReason::Stop,
            token_usage: Some(TokenUsage {
                prompt_tokens: request.prompt.len() as u32 / 4,
                completion_tokens: 50,
                total_tokens: (request.prompt.len() as u32 / 4) + 50,
            }),
            metadata: {
                let mut meta = HashMap::new();
                meta.insert("provider".to_string(), serde_json::Value::String(provider.to_string()));
                meta.insert("mock".to_string(), serde_json::Value::Bool(true));
                meta
            },
            confidence_score: Some(0.95),
        })
    }
}

impl LLMClientPool {
    fn new() -> Self {
        let mut clients: HashMap<String, Box<dyn LLMClient>> = HashMap::new();
        clients.insert("openai".to_string(), Box::new(MockLLMClient));
        clients.insert("anthropic".to_string(), Box::new(MockLLMClient));
        clients.insert("custom".to_string(), Box::new(MockLLMClient));
        
        Self { clients }
    }
    
    async fn execute_request(
        &self,
        request: &ModelRequest,
        provider: &LLMProvider,
    ) -> Result<ModelResponse, RoutingError> {
        let client_key = match provider {
            LLMProvider::OpenAI { .. } => "openai",
            LLMProvider::Anthropic { .. } => "anthropic", 
            LLMProvider::Custom { .. } => "custom",
        };
        
        let client = self.clients.get(client_key)
            .ok_or_else(|| RoutingError::LLMFallbackFailed {
                provider: provider.to_string(),
                reason: "No client available for provider".to_string(),
            })?;
        
        client.execute_request(request, provider).await
    }
}

impl DefaultRoutingEngine {
    /// Create a new routing engine with the given configuration
    pub async fn new(
        config: RoutingConfig,
        model_catalog: ModelCatalog,
        model_logger: Option<Arc<ModelLogger>>,
    ) -> Result<Self, RoutingError> {
        Self::new_with_confidence_monitor(
            config,
            model_catalog,
            model_logger,
            Box::new(NoOpConfidenceMonitor),
        ).await
    }

    /// Create a new routing engine with a custom confidence monitor implementation
    /// This allows enterprise builds to inject their own confidence monitor
    pub async fn new_with_confidence_monitor(
        config: RoutingConfig,
        model_catalog: ModelCatalog,
        model_logger: Option<Arc<ModelLogger>>,
        confidence_monitor: Box<dyn ConfidenceMonitorTrait>,
    ) -> Result<Self, RoutingError> {
        // Create task classifier
        let classifier = TaskClassifier::new(config.classification.clone())?;
        
        // Create policy evaluator
        let policy_evaluator = PolicyEvaluator::new(
            config.policy.clone(),
            classifier,
            model_catalog.clone(),
        )?;
        
        // Create LLM client pool
        let llm_clients = Arc::new(LLMClientPool::new());
        
        let engine = Self {
            policy_evaluator: Arc::new(RwLock::new(policy_evaluator)),
            model_catalog: Arc::new(model_catalog),
            confidence_monitor: Arc::new(RwLock::new(confidence_monitor)),
            model_logger,
            statistics: Arc::new(RwLock::new(RoutingStatistics::default())),
            config: Arc::new(RwLock::new(config)),
            llm_clients,
        };
        
        Ok(engine)
    }
    
    /// Execute an SLM route with monitoring and fallback
    async fn execute_slm_route(
        &self,
        context: &RoutingContext,
        request: &ModelRequest,
        model_id: &str,
        monitoring_level: &super::decision::MonitoringLevel,
        fallback_on_failure: bool,
    ) -> Result<ModelResponse, RoutingError> {
        let _start_time = Instant::now();
        
        // Get the model from catalog
        let model = self.model_catalog.get_model(model_id)
            .ok_or_else(|| RoutingError::NoSuitableModel { 
                task_type: context.task_type.clone() 
            })?;
        
        // Execute the SLM (mock implementation)
        let slm_result = self.execute_slm_mock(request, model).await;
        
        match slm_result {
            Ok(response) => {
                // Evaluate confidence if monitoring is enabled
                let should_fallback = match monitoring_level {
                    super::decision::MonitoringLevel::None => false,
                    super::decision::MonitoringLevel::Basic => {
                        // Basic monitoring - check for obvious failures
                        response.finish_reason != FinishReason::Stop
                    }
                    super::decision::MonitoringLevel::Enhanced { confidence_threshold } => {
                        // For enhanced monitoring, use confidence score if available
                        // Enterprise builds can inject more sophisticated confidence monitors
                        let confidence_score = response.confidence_score.unwrap_or(0.5);
                        confidence_score < *confidence_threshold
                    }
                };
                
                if should_fallback && fallback_on_failure {
                    tracing::warn!(
                        "SLM response did not meet confidence threshold, falling back to LLM"
                    );
                    
                    // Update statistics for fallback
                    {
                        let mut stats = self.statistics.write().await;
                        stats.fallback_routes += 1;
                    }
                    
                    self.execute_llm_fallback(request, "Low confidence SLM response").await
                } else {
                    // Note: In enterprise mode, the ConfidenceMonitor would record evaluation results
                    // but the trait interface doesn't expose this method to keep OSS code clean
                    Ok(response)
                }
            }
            Err(e) => {
                if fallback_on_failure {
                    tracing::error!("SLM execution failed, falling back to LLM: {}", e);
                    self.execute_llm_fallback(request, &format!("SLM execution failed: {}", e)).await
                } else {
                    Err(RoutingError::ModelExecutionFailed {
                        model_id: model_id.to_string(),
                        reason: e.to_string(),
                    })
                }
            }
        }
    }
    
    /// Mock SLM execution (in real implementation, would use SlmRunner)
    async fn execute_slm_mock(
        &self,
        request: &ModelRequest,
        model: &crate::config::Model,
    ) -> Result<ModelResponse, SlmRunnerError> {
        // Simulate SLM execution time
        tokio::time::sleep(Duration::from_millis(200)).await;
        
        // Simulate potential failure for certain inputs
        if request.prompt.contains("error") {
            return Err(SlmRunnerError::ExecutionFailed {
                reason: "Simulated execution error".to_string(),
            });
        }
        
        Ok(ModelResponse {
            content: format!("SLM ({}) response: {}", model.name, request.prompt),
            finish_reason: FinishReason::Stop,
            token_usage: Some(TokenUsage {
                prompt_tokens: request.prompt.len() as u32 / 4,
                completion_tokens: 30,
                total_tokens: (request.prompt.len() as u32 / 4) + 30,
            }),
            metadata: {
                let mut meta = HashMap::new();
                meta.insert("model_id".to_string(), serde_json::Value::String(model.id.clone()));
                meta.insert("provider".to_string(), serde_json::Value::String(format!("{:?}", model.provider)));
                meta
            },
            confidence_score: Some(0.8 + (request.prompt.len() % 20) as f64 / 100.0), // Mock confidence
        })
    }
    
    /// Execute LLM fallback
    async fn execute_llm_fallback(
        &self,
        request: &ModelRequest,
        _reason: &str,
    ) -> Result<ModelResponse, RoutingError> {
        let config = self.config.read().await;
        let fallback_config = &config.policy.fallback_config;
        
        if !fallback_config.enabled {
            return Err(RoutingError::LLMFallbackFailed {
                provider: "disabled".to_string(),
                reason: "LLM fallback is disabled".to_string(),
            });
        }
        
        // Try primary provider first
        let provider = LLMProvider::OpenAI { model: None };
        
        match self.llm_clients.execute_request(request, &provider).await {
            Ok(response) => Ok(response),
            Err(e) => Err(RoutingError::LLMFallbackFailed {
                provider: provider.to_string(),
                reason: e.to_string(),
            }),
        }
    }
    
    /// Log routing decision and execution
    async fn log_routing_execution(
        &self,
        context: &RoutingContext,
        decision: &RouteDecision,
        request: &ModelRequest,
        response: &ModelResponse,
        execution_time: Duration,
        error: Option<&RoutingError>,
    ) {
        if let Some(ref logger) = self.model_logger {
            let model_name = match decision {
                RouteDecision::UseSLM { model_id, .. } => model_id.clone(),
                RouteDecision::UseLLM { provider, .. } => provider.to_string(),
                RouteDecision::Deny { .. } => "denied".to_string(),
            };
            
            let request_data = RequestData {
                prompt: request.prompt.clone(),
                tool_name: None,
                tool_arguments: None,
                parameters: {
                    let mut params = HashMap::new();
                    params.insert("routing_decision".to_string(), 
                        serde_json::Value::String(format!("{:?}", decision)));
                    params.insert("task_type".to_string(),
                        serde_json::Value::String(context.task_type.to_string()));
                    params
                },
            };
            
            let response_data = ResponseData {
                content: response.content.clone(),
                tool_result: None,
                confidence: response.confidence_score,
                metadata: response.metadata.clone(),
            };
            
            let metadata = {
                let mut meta = HashMap::new();
                meta.insert("routing_engine".to_string(), "default".to_string());
                meta.insert("agent_id".to_string(), context.agent_id.to_string());
                meta.insert("request_id".to_string(), context.request_id.clone());
                meta
            };
            
            if let Err(e) = logger.log_interaction(
                context.agent_id,
                ModelInteractionType::Completion,
                &model_name,
                request_data,
                response_data,
                execution_time,
                metadata,
                response.token_usage.as_ref().map(|t| LogTokenUsage {
                    input_tokens: t.prompt_tokens,
                    output_tokens: t.completion_tokens,
                    total_tokens: t.total_tokens,
                }),
                error.map(|e| e.to_string()),
            ).await {
                tracing::warn!("Failed to log routing execution: {}", e);
            }
        }
    }
}

#[async_trait]
impl RoutingEngine for DefaultRoutingEngine {
    async fn route_request(
        &self,
        context: &RoutingContext,
    ) -> Result<RouteDecision, RoutingError> {
        let policy_result = self.policy_evaluator
            .read()
            .await
            .evaluate_policies(context)
            .await?;
        
        tracing::debug!(
            "Routing decision for agent {}: {:?} (rule: {:?})",
            context.agent_id,
            policy_result.decision,
            policy_result.matched_rule
        );
        
        Ok(policy_result.decision)
    }
    
    async fn execute_with_routing(
        &self,
        context: RoutingContext,
        request: ModelRequest,
    ) -> Result<ModelResponse, RoutingError> {
        let start_time = Instant::now();
        let route_decision = self.route_request(&context).await?;
        
        let result = match &route_decision {
            RouteDecision::UseSLM { model_id, monitoring, fallback_on_failure, sandbox_tier: _ } => {
                self.execute_slm_route(
                    &context,
                    &request,
                    model_id,
                    monitoring,
                    *fallback_on_failure,
                ).await
            }
            RouteDecision::UseLLM { provider, reason, sandbox_tier: _ } => {
                tracing::info!("Routing to LLM: {}", reason);
                self.llm_clients.execute_request(&request, provider).await
            }
            RouteDecision::Deny { reason, policy_violated } => {
                return Err(RoutingError::RoutingDenied {
                    policy: policy_violated.clone(),
                    reason: reason.clone(),
                });
            }
        };
        
        let execution_time = start_time.elapsed();
        
        // Update statistics
        {
            let mut stats = self.statistics.write().await;
            stats.update(
                &route_decision,
                execution_time,
                result.is_ok(),
            );
            
            if let Ok(ref response) = result {
                if let Some(confidence) = response.confidence_score {
                    stats.add_confidence_score(confidence);
                }
            }
        }
        
        // Log the execution
        match &result {
            Ok(response) => {
                self.log_routing_execution(
                    &context,
                    &route_decision,
                    &request,
                    response,
                    execution_time,
                    None,
                ).await;
            }
            Err(error) => {
                // Create a dummy response for logging
                let dummy_response = ModelResponse {
                    content: "Error occurred".to_string(),
                    finish_reason: FinishReason::Error,
                    token_usage: None,
                    metadata: HashMap::new(),
                    confidence_score: Some(0.0),
                };
                
                self.log_routing_execution(
                    &context,
                    &route_decision,
                    &request,
                    &dummy_response,
                    execution_time,
                    Some(error),
                ).await;
            }
        }
        
        result
    }
    
    fn validate_policies(&self) -> Result<(), RoutingError> {
        // Validation is done during PolicyEvaluator creation
        Ok(())
    }
    
    async fn get_routing_stats(&self) -> RoutingStatistics {
        self.statistics.read().await.clone()
    }
    
    async fn update_config(&self, config: RoutingConfig) -> Result<(), RoutingError> {
        // Update configuration
        *self.config.write().await = config.clone();
        
        // Update policy evaluator
        self.policy_evaluator
            .write()
            .await
            .update_config(config.policy)?;
        
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::AgentId;
    use crate::config::{Slm, Model, ModelProvider, ModelResourceRequirements, ModelAllowListConfig, SandboxProfile};
    use std::path::PathBuf;
    use std::collections::HashMap;

    async fn create_test_engine() -> DefaultRoutingEngine {
        let mut global_models = Vec::new();
        
        global_models.push(Model {
            id: "test-slm".to_string(),
            name: "Test SLM".to_string(),
            provider: ModelProvider::LocalFile { file_path: PathBuf::from("/tmp/test.gguf") },
            capabilities: vec![
                crate::config::ModelCapability::TextGeneration,
                crate::config::ModelCapability::CodeGeneration,
            ],
            resource_requirements: ModelResourceRequirements {
                min_memory_mb: 1024,
                preferred_cpu_cores: 2.0,
                gpu_requirements: None,
            },
        });

        global_models.push(Model {
            id: "error-slm".to_string(),
            name: "Error SLM".to_string(),
            provider: ModelProvider::LocalFile { file_path: PathBuf::from("/tmp/error.gguf") },
            capabilities: vec![crate::config::ModelCapability::TextGeneration],
            resource_requirements: ModelResourceRequirements {
                min_memory_mb: 512,
                preferred_cpu_cores: 1.0,
                gpu_requirements: None,
            },
        });

        let mut sandbox_profiles = HashMap::new();
        sandbox_profiles.insert("default".to_string(), SandboxProfile::secure_default());
        
        let slm_config = Slm {
            enabled: true,
            model_allow_lists: ModelAllowListConfig {
                global_models,
                agent_model_maps: HashMap::new(),
                allow_runtime_overrides: false,
            },
            sandbox_profiles,
            default_sandbox_profile: "default".to_string(),
        };
        
        let model_catalog = ModelCatalog::new(slm_config).unwrap();
        let config = RoutingConfig::default();
        
        DefaultRoutingEngine::new(config, model_catalog, None).await.unwrap()
    }

    async fn create_test_engine_with_logger() -> DefaultRoutingEngine {
        let logger = ModelLogger::new(super::super::super::logging::LoggingConfig::default(), None).unwrap();
        
        let mut global_models = Vec::new();
        global_models.push(Model {
            id: "test-slm".to_string(),
            name: "Test SLM".to_string(),
            provider: ModelProvider::LocalFile { file_path: PathBuf::from("/tmp/test.gguf") },
            capabilities: vec![crate::config::ModelCapability::TextGeneration],
            resource_requirements: ModelResourceRequirements {
                min_memory_mb: 1024,
                preferred_cpu_cores: 2.0,
                gpu_requirements: None,
            },
        });

        let mut sandbox_profiles = HashMap::new();
        sandbox_profiles.insert("default".to_string(), SandboxProfile::secure_default());
        
        let slm_config = Slm {
            enabled: true,
            model_allow_lists: ModelAllowListConfig {
                global_models,
                agent_model_maps: HashMap::new(),
                allow_runtime_overrides: false,
            },
            sandbox_profiles,
            default_sandbox_profile: "default".to_string(),
        };
        
        let model_catalog = ModelCatalog::new(slm_config).unwrap();
        let config = RoutingConfig::default();
        
        DefaultRoutingEngine::new(config, model_catalog, Some(Arc::new(logger))).await.unwrap()
    }

    fn create_test_request(prompt: &str) -> ModelRequest {
        ModelRequest::from_task(prompt.to_string())
    }

    fn create_test_context(prompt: &str, task_type: super::super::error::TaskType) -> RoutingContext {
        RoutingContext::new(AgentId::new(), task_type, prompt.to_string())
    }
    
    #[tokio::test]
    async fn test_routing_engine_creation() {
        let engine = create_test_engine().await;
        
        // Verify engine was created successfully
        let stats = engine.get_routing_stats().await;
        assert_eq!(stats.total_requests, 0);
        
        // Verify policies can be validated
        assert!(engine.validate_policies().is_ok());
    }

    #[tokio::test]
    async fn test_routing_engine_with_logger() {
        let engine = create_test_engine_with_logger().await;
        
        // Should have logger configured
        assert!(engine.model_logger.is_some());
        
        let stats = engine.get_routing_stats().await;
        assert_eq!(stats.total_requests, 0);
    }
    
    #[tokio::test]
    async fn test_routing_engine_basic_flow() {
        let engine = create_test_engine().await;
        
        let context = create_test_context(
            "Write a hello world function",
            super::super::error::TaskType::CodeGeneration
        );
        
        let decision = engine.route_request(&context).await.unwrap();
        
        // Should get some kind of routing decision
        match decision {
            RouteDecision::UseSLM { .. } | RouteDecision::UseLLM { .. } => {
                // Expected outcomes
            }
            RouteDecision::Deny { .. } => {
                panic!("Should not deny basic request");
            }
        }
    }
    
    #[tokio::test]
    async fn test_execute_with_routing_slm_success() {
        let engine = create_test_engine().await;
        
        let context = create_test_context(
            "Write a hello world function",
            super::super::error::TaskType::CodeGeneration
        );
        
        let request = create_test_request("Write a hello world function");
        
        let response = engine.execute_with_routing(context, request).await.unwrap();
        
        assert!(!response.content.is_empty());
        assert!(response.confidence_score.is_some());
        assert_eq!(response.finish_reason, FinishReason::Stop);
        
        // Check that statistics were updated
        let stats = engine.get_routing_stats().await;
        assert!(stats.total_requests > 0);
    }

    #[tokio::test]
    async fn test_execute_with_routing_slm_error_fallback() {
        let engine = create_test_engine().await;
        
        let context = create_test_context(
            "This should trigger an error in SLM",
            super::super::error::TaskType::CodeGeneration
        );
        
        let request = create_test_request("error trigger");
        
        let response = engine.execute_with_routing(context, request).await.unwrap();
        
        // Should get LLM fallback response
        assert!(!response.content.is_empty());
        assert!(response.content.contains("LLM response"));
        
        let stats = engine.get_routing_stats().await;
        assert!(stats.fallback_routes > 0);
    }

    #[tokio::test]
    async fn test_slm_execution_success() {
        let engine = create_test_engine().await;
        
        let context = create_test_context(
            "Test prompt",
            super::super::error::TaskType::CodeGeneration
        );
        
        let request = create_test_request("Test prompt");
        
        let response = engine.execute_slm_route(
            &context,
            &request,
            "test-slm",
            &crate::routing::MonitoringLevel::Basic,
            true,
        ).await.unwrap();
        
        assert!(!response.content.is_empty());
        assert!(response.content.contains("Test SLM"));
        assert!(response.confidence_score.is_some());
    }

    #[tokio::test]
    async fn test_slm_execution_with_enhanced_monitoring() {
        let engine = create_test_engine().await;
        
        let context = create_test_context(
            "Test prompt with monitoring",
            super::super::error::TaskType::CodeGeneration
        );
        
        let request = create_test_request("Test prompt with monitoring");
        
        let response = engine.execute_slm_route(
            &context,
            &request,
            "test-slm",
            &crate::routing::MonitoringLevel::Enhanced { confidence_threshold: 0.9 },
            true,
        ).await.unwrap();
        
        // Should either get SLM response or LLM fallback
        assert!(!response.content.is_empty());
    }

    #[tokio::test]
    async fn test_slm_execution_no_monitoring() {
        let engine = create_test_engine().await;
        
        let context = create_test_context(
            "Test prompt no monitoring",
            super::super::error::TaskType::CodeGeneration
        );
        
        let request = create_test_request("Test prompt no monitoring");
        
        let response = engine.execute_slm_route(
            &context,
            &request,
            "test-slm",
            &crate::routing::MonitoringLevel::None,
            true,
        ).await.unwrap();
        
        // Should get SLM response without monitoring
        assert!(!response.content.is_empty());
        assert!(response.content.contains("Test SLM"));
    }

    #[tokio::test]
    async fn test_slm_execution_error_no_fallback() {
        let engine = create_test_engine().await;
        
        let context = create_test_context(
            "error trigger",
            super::super::error::TaskType::CodeGeneration
        );
        
        let request = create_test_request("error trigger");
        
        let result = engine.execute_slm_route(
            &context,
            &request,
            "test-slm",
            &crate::routing::MonitoringLevel::Basic,
            false, // No fallback
        ).await;
        
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), RoutingError::ModelExecutionFailed { .. }));
    }

    #[tokio::test]
    async fn test_slm_execution_nonexistent_model() {
        let engine = create_test_engine().await;
        
        let context = create_test_context(
            "Test prompt",
            super::super::error::TaskType::CodeGeneration
        );
        
        let request = create_test_request("Test prompt");
        
        let result = engine.execute_slm_route(
            &context,
            &request,
            "nonexistent-model",
            &crate::routing::MonitoringLevel::Basic,
            true,
        ).await;
        
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), RoutingError::NoSuitableModel { .. }));
    }

    #[tokio::test]
    async fn test_llm_fallback_execution() {
        let engine = create_test_engine().await;
        
        let request = create_test_request("Test LLM fallback");
        
        let response = engine.execute_llm_fallback(&request, "Test reason").await.unwrap();
        
        assert!(!response.content.is_empty());
        assert!(response.content.contains("LLM response"));
        assert_eq!(response.finish_reason, FinishReason::Stop);
        assert!(response.confidence_score.is_some());
    }

    #[tokio::test]
    async fn test_llm_fallback_disabled() {
        let engine = create_test_engine().await;
        
        // Disable fallback in config
        {
            let mut config = engine.config.write().await;
            config.policy.fallback_config.enabled = false;
        }
        
        let request = create_test_request("Test disabled fallback");
        
        let result = engine.execute_llm_fallback(&request, "Test reason").await;
        
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), RoutingError::LLMFallbackFailed { .. }));
    }

    #[tokio::test]
    async fn test_llm_client_pool() {
        let pool = LLMClientPool::new();
        
        let request = create_test_request("Test LLM client");
        
        // Test OpenAI provider
        let openai_response = pool.execute_request(
            &request,
            &LLMProvider::OpenAI { model: Some("gpt-3.5-turbo".to_string()) }
        ).await.unwrap();
        
        assert!(!openai_response.content.is_empty());
        assert!(openai_response.metadata.contains_key("provider"));
        
        // Test Anthropic provider
        let anthropic_response = pool.execute_request(
            &request,
            &LLMProvider::Anthropic { model: Some("claude-3".to_string()) }
        ).await.unwrap();
        
        assert!(!anthropic_response.content.is_empty());
        assert!(anthropic_response.metadata.contains_key("provider"));
        
        // Test Custom provider
        let custom_response = pool.execute_request(
            &request,
            &LLMProvider::Custom { endpoint: "http://localhost:8080".to_string(), model: None }
        ).await.unwrap();
        
        assert!(!custom_response.content.is_empty());
    }

    #[tokio::test]
    async fn test_mock_slm_execution() {
        let engine = create_test_engine().await;
        
        let request = create_test_request("Test SLM execution");
        let model = engine.model_catalog.get_model("test-slm").unwrap();
        
        let response = engine.execute_slm_mock(&request, model).await.unwrap();
        
        assert!(!response.content.is_empty());
        assert!(response.content.contains("Test SLM"));
        assert!(response.content.contains("Test SLM execution"));
        assert_eq!(response.finish_reason, FinishReason::Stop);
        assert!(response.confidence_score.is_some());
        assert!(response.token_usage.is_some());
    }

    #[tokio::test]
    async fn test_mock_slm_execution_error() {
        let engine = create_test_engine().await;
        
        let request = create_test_request("This should error out");
        let model = engine.model_catalog.get_model("test-slm").unwrap();
        
        let result = engine.execute_slm_mock(&request, model).await;
        
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), SlmRunnerError::ExecutionFailed { .. }));
    }
    
    #[tokio::test]
    async fn test_routing_statistics_tracking() {
        let engine = create_test_engine().await;
        
        // Execute a few requests to track statistics
        let context1 = create_test_context("Test 1", super::super::error::TaskType::CodeGeneration);
        let request1 = create_test_request("Test 1");
        
        let _response1 = engine.execute_with_routing(context1, request1).await.unwrap();
        
        let context2 = create_test_context("error trigger", super::super::error::TaskType::CodeGeneration);
        let request2 = create_test_request("error trigger");
        
        let _response2 = engine.execute_with_routing(context2, request2).await.unwrap();
        
        let stats = engine.get_routing_stats().await;
        
        assert!(stats.total_requests > 0);
        assert!(stats.fallback_routes > 0); // Second request should trigger fallback
        assert!(stats.average_response_time > Duration::from_millis(0));
    }

    #[tokio::test]
    async fn test_config_update() {
        let engine = create_test_engine().await;
        
        let mut new_config = RoutingConfig::default();
        new_config.policy.fallback_config.enabled = false;
        
        let result = engine.update_config(new_config.clone()).await;
        assert!(result.is_ok());
        
        // Verify config was updated
        let updated_config = engine.config.read().await;
        assert!(!updated_config.policy.fallback_config.enabled);
    }

    #[tokio::test]
    async fn test_routing_with_deny_decision() {
        let engine = create_test_engine().await;
        
        // Create a routing context that would trigger a deny decision
        // (This would need specific policy configuration to work in practice)
        let context = create_test_context(
            "forbidden operation",
            super::super::error::TaskType::Custom("forbidden".to_string())
        );
        
        let request = create_test_request("forbidden operation");
        
        // This might not trigger a deny in the default config, but test the error handling
        let result = engine.execute_with_routing(context, request).await;
        
        // Should either succeed with a response or fail with specific error
        match result {
            Ok(response) => {
                assert!(!response.content.is_empty());
            }
            Err(RoutingError::RoutingDenied { .. }) => {
                // Expected for deny decision
            }
            Err(e) => {
                panic!("Unexpected error: {:?}", e);
            }
        }
    }

    #[tokio::test]
    async fn test_logging_integration() {
        let engine = create_test_engine_with_logger().await;
        
        let context = create_test_context(
            "Test logging integration",
            super::super::error::TaskType::CodeGeneration
        );
        
        let request = create_test_request("Test logging integration");
        
        let response = engine.execute_with_routing(context, request).await.unwrap();
        
        assert!(!response.content.is_empty());
        // Logging should happen in the background without affecting the response
    }

    #[tokio::test]
    async fn test_confidence_monitoring_integration() {
        let engine = create_test_engine().await;
        
        let context = create_test_context(
            "Test confidence monitoring",
            super::super::error::TaskType::CodeGeneration
        );
        
        let request = create_test_request("Test confidence monitoring");
        
        let response = engine.execute_with_routing(context, request).await.unwrap();
        
        assert!(!response.content.is_empty());
        assert!(response.confidence_score.is_some());
        
        // Note: Confidence monitoring statistics are only available in enterprise mode
        // The trait interface doesn't expose statistics to keep OSS code clean
    }

    #[tokio::test]
    async fn test_policy_evaluation_integration() {
        let engine = create_test_engine().await;
        
        // Test different task types to ensure policy evaluation works
        let task_types = vec![
            super::super::error::TaskType::CodeGeneration,
            super::super::error::TaskType::CodeGeneration,
            super::super::error::TaskType::Analysis,
            super::super::error::TaskType::Reasoning,
        ];
        
        for task_type in task_types {
            let context = create_test_context("Test policy evaluation", task_type.clone());
            
            let decision = engine.route_request(&context).await.unwrap();
            
            // Should get a valid routing decision for each task type
            match decision {
                RouteDecision::UseSLM { .. } | RouteDecision::UseLLM { .. } | RouteDecision::Deny { .. } => {
                    // All are valid outcomes
                }
            }
        }
    }

    #[tokio::test]
    async fn test_concurrent_routing_requests() {
        let engine = Arc::new(create_test_engine().await);
        
        let mut handles = Vec::new();
        
        // Spawn multiple concurrent routing requests
        for i in 0..10 {
            let engine_clone = Arc::clone(&engine);
            let handle = tokio::spawn(async move {
                let context = create_test_context(
                    &format!("Concurrent request {}", i),
                    super::super::error::TaskType::CodeGeneration
                );
                
                let request = create_test_request(&format!("Concurrent request {}", i));
                
                engine_clone.execute_with_routing(context, request).await
            });
            handles.push(handle);
        }
        
        // Wait for all requests to complete
        let results = futures::future::join_all(handles).await;
        
        // All requests should succeed
        for result in results {
            let response = result.unwrap().unwrap();
            assert!(!response.content.is_empty());
        }
        
        // Check that statistics reflect all requests
        let stats = engine.get_routing_stats().await;
        assert_eq!(stats.total_requests, 10);
    }

    #[tokio::test]
    async fn test_error_handling_and_recovery() {
        let engine = create_test_engine().await;
        
        // Test various error scenarios
        let error_scenarios = vec![
            ("error trigger", "Should trigger SLM execution error"),
            ("", "Empty prompt"),
            ("   ", "Whitespace-only prompt"),
        ];
        
        for (prompt, description) in error_scenarios {
            let context = create_test_context(prompt, super::super::error::TaskType::CodeGeneration);
            let request = create_test_request(prompt);
            
            let result = engine.execute_with_routing(context, request).await;
            
            match result {
                Ok(response) => {
                    // Should get a response (likely from LLM fallback)
                    assert!(!response.content.is_empty(), "Failed for: {}", description);
                }
                Err(e) => {
                    // Some errors are expected, but should be handled gracefully
                    tracing::info!("Expected error for '{}': {:?}", description, e);
                }
            }
        }
    }

    #[tokio::test]
    async fn test_model_metadata_and_token_usage() {
        let engine = create_test_engine().await;
        
        let context = create_test_context(
            "Test metadata and token usage",
            super::super::error::TaskType::CodeGeneration
        );
        
        let request = create_test_request("Test metadata and token usage");
        
        let response = engine.execute_with_routing(context, request).await.unwrap();
        
        // Verify response structure
        assert!(!response.content.is_empty());
        assert!(response.token_usage.is_some());
        assert!(!response.metadata.is_empty());
        
        let token_usage = response.token_usage.unwrap();
        assert!(token_usage.prompt_tokens > 0);
        assert!(token_usage.completion_tokens > 0);
        assert_eq!(token_usage.total_tokens, token_usage.prompt_tokens + token_usage.completion_tokens);
    }

    #[tokio::test]
    async fn test_validate_policies() {
        let engine = create_test_engine().await;
        
        let result = engine.validate_policies();
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_engine_state_consistency() {
        let engine = create_test_engine().await;
        
        // Verify initial state
        let initial_stats = engine.get_routing_stats().await;
        assert_eq!(initial_stats.total_requests, 0);
        
        // Execute some requests
        for i in 0..5 {
            let context = create_test_context(
                &format!("Test request {}", i),
                super::super::error::TaskType::CodeGeneration
            );
            let request = create_test_request(&format!("Test request {}", i));
            
            let _response = engine.execute_with_routing(context, request).await.unwrap();
        }
        
        // Verify state was updated consistently
        let final_stats = engine.get_routing_stats().await;
        assert_eq!(final_stats.total_requests, 5);
        assert!(final_stats.average_response_time > Duration::from_millis(0));
        
        // Note: Confidence monitoring statistics are only available in enterprise mode
        // The trait interface doesn't expose statistics to keep OSS code clean
    }
}