octoroute 1.0.0

Intelligent multi-model router for self-hosted LLMs
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
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
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
//! Configuration management for Octoroute
//!
//! Parses TOML configuration files and provides typed access to settings.

use crate::router::TargetModel;
use serde::{Deserialize, Serialize};
use std::path::Path;
use std::str::FromStr;

/// Root configuration structure
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Config {
    pub server: ServerConfig,
    pub models: ModelsConfig,
    pub routing: RoutingConfig,
    #[serde(default)]
    pub observability: ObservabilityConfig,
    #[serde(default)]
    pub timeouts: TimeoutsConfig,
}

/// Server configuration
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ServerConfig {
    pub host: String,
    pub port: u16,
    #[serde(default = "default_request_timeout")]
    pub request_timeout_seconds: u64,
}

fn default_request_timeout() -> u64 {
    30
}

/// Models configuration (multi-model support)
///
/// Each tier (fast, balanced, deep) can have multiple model endpoints
/// for load balancing and failover.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ModelsConfig {
    pub fast: Vec<ModelEndpoint>,
    pub balanced: Vec<ModelEndpoint>,
    pub deep: Vec<ModelEndpoint>,
}

/// Individual model endpoint configuration
///
/// All fields are private to enforce invariants. Configuration is loaded via
/// deserialization and validated via Config::validate(). After construction,
/// fields cannot be mutated, ensuring validated data remains valid.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ModelEndpoint {
    name: String,
    base_url: String,
    max_tokens: usize,
    #[serde(default = "default_temperature")]
    temperature: f64,
    /// Load balancing weight for weighted random selection within priority tier
    #[serde(default = "default_weight")]
    weight: f64,
    /// Priority level - higher priority endpoints are tried first
    #[serde(default = "default_priority")]
    priority: u8,
}

impl ModelEndpoint {
    /// Get the endpoint name
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Get the endpoint base URL
    pub fn base_url(&self) -> &str {
        &self.base_url
    }

    /// Get the maximum number of tokens for this endpoint
    pub fn max_tokens(&self) -> usize {
        self.max_tokens
    }

    /// Get the temperature parameter for this endpoint
    pub fn temperature(&self) -> f64 {
        self.temperature
    }

    /// Get the load balancing weight for this endpoint
    pub fn weight(&self) -> f64 {
        self.weight
    }

    /// Get the priority level for this endpoint (higher = tried first)
    pub fn priority(&self) -> u8 {
        self.priority
    }
}

fn default_temperature() -> f64 {
    0.7
}

fn default_weight() -> f64 {
    1.0
}

fn default_priority() -> u8 {
    1
}

/// Router query timeout configuration per tier
///
/// Allows different timeout values for router queries based on model size.
/// Larger models (Deep tier) typically need more time to analyze routing decisions.
///
/// # Encapsulation
///
/// Fields are private to prevent post-validation mutation. Use `new()` constructor
/// for validated construction and accessor methods for field access.
///
/// This matches the `TimeoutsConfig` pattern for consistency.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct RouterTimeouts {
    /// Timeout for Fast tier router queries (in seconds)
    ///
    /// Recommended: 5-10s for 8B models
    fast: u64,
    /// Timeout for Balanced tier router queries (in seconds)
    ///
    /// Recommended: 10-15s for 30B models
    balanced: u64,
    /// Timeout for Deep tier router queries (in seconds)
    ///
    /// Recommended: 15-30s for 120B models
    deep: u64,
}

impl Default for RouterTimeouts {
    /// Default router timeouts: 10s for all tiers
    ///
    /// Conservative default that works for most deployments.
    /// Operators can tune based on their hardware and model performance.
    fn default() -> Self {
        Self {
            fast: 10,
            balanced: 10,
            deep: 10,
        }
    }
}

impl RouterTimeouts {
    /// Create a new RouterTimeouts with validation
    ///
    /// # Arguments
    ///
    /// * `fast` - Timeout for Fast tier router queries (in seconds)
    /// * `balanced` - Timeout for Balanced tier router queries (in seconds)
    /// * `deep` - Timeout for Deep tier router queries (in seconds)
    ///
    /// # Errors
    ///
    /// Returns an error if any timeout is 0 (zero timeouts cause immediate failures)
    ///
    /// # Example
    ///
    /// ```
    /// use octoroute::config::RouterTimeouts;
    ///
    /// let timeouts = RouterTimeouts::new(5, 10, 20).expect("valid timeouts");
    /// assert_eq!(timeouts.fast(), 5);
    /// ```
    pub fn new(fast: u64, balanced: u64, deep: u64) -> Result<Self, String> {
        let timeouts = Self {
            fast,
            balanced,
            deep,
        };
        timeouts.validate()?;
        Ok(timeouts)
    }

    /// Get the Fast tier timeout in seconds
    pub fn fast(&self) -> u64 {
        self.fast
    }

    /// Get the Balanced tier timeout in seconds
    pub fn balanced(&self) -> u64 {
        self.balanced
    }

    /// Get the Deep tier timeout in seconds
    pub fn deep(&self) -> u64 {
        self.deep
    }

    /// Validate that all timeouts are positive (> 0)
    ///
    /// Zero or negative timeouts are invalid and will cause immediate failures.
    fn validate(&self) -> Result<(), String> {
        if self.fast == 0 {
            return Err("router_timeouts.fast must be greater than 0".to_string());
        }
        if self.balanced == 0 {
            return Err("router_timeouts.balanced must be greater than 0".to_string());
        }
        if self.deep == 0 {
            return Err("router_timeouts.deep must be greater than 0".to_string());
        }
        Ok(())
    }
}

/// Routing configuration
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct RoutingConfig {
    pub strategy: RoutingStrategy,
    #[serde(default)]
    pub default_importance: crate::router::Importance,
    /// Which tier (Fast/Balanced/Deep) to use for LLM routing decisions
    ///
    /// Defaults to Balanced if not specified (recommended for most use cases).
    ///
    /// # Validation
    ///
    /// **Format Validation (Deserialization Time)**:
    /// - Serde's `TargetModel` enum deserializer validates format matches
    ///   "fast", "balanced", or "deep" (case-sensitive lowercase)
    /// - Invalid formats like "FAST" or "fasst" are rejected immediately
    ///   with clear deserialization errors
    ///
    /// **Availability Validation (Config Loading Time)**:
    /// - `Config::validate()` ensures ALL tiers have at least one endpoint,
    ///   preventing routing failures regardless of router_tier
    /// - This catches misconfiguration (e.g., router_tier="deep" but no
    ///   [[models.deep]] endpoints) at config load time, not runtime
    ///
    /// Field is private to prevent post-validation mutation. Use `router_tier()` accessor.
    #[serde(default)]
    router_tier: TargetModel,
    /// Router query timeout configuration per tier
    ///
    /// Defaults to 10s for all tiers if not specified (backward compatible).
    /// Can be customized per tier to accommodate different model response times.
    #[serde(default)]
    pub router_timeouts: RouterTimeouts,
}

impl RoutingConfig {
    /// Get the router tier for LLM-based routing decisions
    ///
    /// The router tier determines which model tier (Fast/Balanced/Deep) is used
    /// to make routing decisions in LLM and Hybrid strategies.
    ///
    /// # Returns
    /// The configured router tier (validated during config loading)
    pub fn router_tier(&self) -> TargetModel {
        self.router_tier
    }

    /// Get the router query timeout for a specific tier
    ///
    /// Returns the configured timeout (in seconds) for router queries
    /// to the specified tier.
    ///
    /// # Arguments
    /// * `tier` - The model tier to get the timeout for
    ///
    /// # Returns
    /// Timeout in seconds (u64)
    pub fn router_timeout_for_tier(&self, tier: TargetModel) -> u64 {
        match tier {
            TargetModel::Fast => self.router_timeouts.fast(),
            TargetModel::Balanced => self.router_timeouts.balanced(),
            TargetModel::Deep => self.router_timeouts.deep(),
        }
    }
}

/// Routing strategy enum
#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum RoutingStrategy {
    Rule,
    Llm,
    Hybrid,
    Tool,
}

/// Observability configuration
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ObservabilityConfig {
    #[serde(default = "default_log_level")]
    pub log_level: String,
}

impl Default for ObservabilityConfig {
    fn default() -> Self {
        Self {
            log_level: default_log_level(),
        }
    }
}

fn default_log_level() -> String {
    "info".to_string()
}

/// Per-tier timeout overrides
///
/// Allows configuring different timeouts for each model tier.
/// If a tier timeout is not specified in the config file, the global
/// `server.request_timeout_seconds` is used as the default.
///
/// # Custom Deserialization
///
/// This type implements custom `Deserialize` to enforce validation at parse time.
/// All timeout values must be in range (0, 300] seconds. Invalid values are rejected
/// immediately during TOML parsing, not later during `Config::validate()`.
///
/// This prevents the temporal gap where invalid `TimeoutsConfig` instances could exist
/// between deserialization and validation, upholding the principle: "make invalid states
/// unrepresentable."
#[derive(Debug, Clone, Default, Serialize)]
pub struct TimeoutsConfig {
    /// Timeout for fast tier (8B models) in seconds
    fast: Option<u64>,
    /// Timeout for balanced tier (30B models) in seconds
    balanced: Option<u64>,
    /// Timeout for deep tier (120B models) in seconds
    deep: Option<u64>,
}

impl TimeoutsConfig {
    /// Create a new TimeoutsConfig with validated timeout values
    ///
    /// # Arguments
    ///
    /// * `fast` - Optional timeout for fast tier in seconds
    /// * `balanced` - Optional timeout for balanced tier in seconds
    /// * `deep` - Optional timeout for deep tier in seconds
    ///
    /// # Errors
    ///
    /// Returns an error if any timeout is zero or exceeds 300 seconds.
    ///
    /// # Configuration Sanity Check
    ///
    /// The 300-second (5-minute) upper bound enforces reasonable timeout values:
    /// - Prevents typos (e.g., 3000 instead of 30 seconds)
    /// - Catches unit confusion (milliseconds vs seconds)
    /// - Ensures timely failure detection (5+ minute timeouts hide issues)
    ///
    /// This is a **configuration policy**, not a technical limitation. Values above
    /// 300 seconds are rejected to maintain predictable system behavior and prevent
    /// excessive resource holding during network issues.
    pub fn new(
        fast: Option<u64>,
        balanced: Option<u64>,
        deep: Option<u64>,
    ) -> crate::error::AppResult<Self> {
        // Validate each timeout (0, 300] seconds
        // NOTE: Upper bound enforces configuration policy (5-minute max)
        for (tier_name, timeout_opt) in [("fast", fast), ("balanced", balanced), ("deep", deep)] {
            if let Some(timeout) = timeout_opt {
                if timeout == 0 {
                    return Err(crate::error::AppError::Config(format!(
                        "timeouts.{} must be greater than 0, got {}",
                        tier_name, timeout
                    )));
                }
                if timeout > 300 {
                    return Err(crate::error::AppError::Config(format!(
                        "timeouts.{} cannot exceed 300 seconds (5 minutes), got {}. \
                        This configuration policy prevents connection pool exhaustion and ensures timely failure detection.",
                        tier_name, timeout
                    )));
                }
            }
        }
        Ok(Self {
            fast,
            balanced,
            deep,
        })
    }

    /// Get the fast tier timeout (if configured)
    pub fn fast(&self) -> Option<u64> {
        self.fast
    }

    /// Get the balanced tier timeout (if configured)
    pub fn balanced(&self) -> Option<u64> {
        self.balanced
    }

    /// Get the deep tier timeout (if configured)
    pub fn deep(&self) -> Option<u64> {
        self.deep
    }
}

/// Custom Deserialize implementation for TimeoutsConfig
///
/// Enforces validation at deserialization time by calling the validated `new()` constructor.
/// This eliminates the temporal gap where invalid instances could exist between parsing
/// and validation.
impl<'de> Deserialize<'de> for TimeoutsConfig {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        use serde::de::{self, MapAccess, Visitor};
        use std::fmt;

        // Helper struct for deserializing raw values before validation
        #[derive(Deserialize)]
        #[serde(field_identifier, rename_all = "lowercase")]
        enum Field {
            Fast,
            Balanced,
            Deep,
        }

        struct TimeoutsConfigVisitor;

        impl<'de> Visitor<'de> for TimeoutsConfigVisitor {
            type Value = TimeoutsConfig;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("a struct with optional timeout fields (fast, balanced, deep)")
            }

            fn visit_map<V>(self, mut map: V) -> Result<TimeoutsConfig, V::Error>
            where
                V: MapAccess<'de>,
            {
                let mut fast = None;
                let mut balanced = None;
                let mut deep = None;

                while let Some(key) = map.next_key()? {
                    match key {
                        Field::Fast => {
                            if fast.is_some() {
                                return Err(de::Error::duplicate_field("fast"));
                            }
                            fast = Some(map.next_value()?);
                        }
                        Field::Balanced => {
                            if balanced.is_some() {
                                return Err(de::Error::duplicate_field("balanced"));
                            }
                            balanced = Some(map.next_value()?);
                        }
                        Field::Deep => {
                            if deep.is_some() {
                                return Err(de::Error::duplicate_field("deep"));
                            }
                            deep = Some(map.next_value()?);
                        }
                    }
                }

                // Call validated constructor - this is where validation happens!
                TimeoutsConfig::new(fast, balanced, deep)
                    .map_err(|e| de::Error::custom(format!("Invalid timeout configuration: {}", e)))
            }
        }

        deserializer.deserialize_struct(
            "TimeoutsConfig",
            &["fast", "balanced", "deep"],
            TimeoutsConfigVisitor,
        )
    }
}

impl Config {
    /// Load configuration from a TOML file
    pub fn from_file<P: AsRef<Path>>(path: P) -> crate::error::AppResult<Self> {
        let path_display = path.as_ref().display().to_string();

        // Phase 1: Read file (preserves io::Error context)
        let content = std::fs::read_to_string(path.as_ref()).map_err(|source| {
            let remediation = match source.kind() {
                std::io::ErrorKind::NotFound => {
                    let current_dir = std::env::current_dir()
                        .map(|p| p.display().to_string())
                        .unwrap_or_else(|_| "<unknown>".to_string());
                    format!(
                        "\nFile not found. Check that:\n\
                        1. Path '{}' is correct\n\
                        2. File exists and is readable\n\
                        3. Current working directory is: {}",
                        path_display, current_dir
                    )
                }
                std::io::ErrorKind::PermissionDenied => {
                    format!(
                        "\nPermission denied. Check that:\n\
                        1. File '{}' has read permissions (chmod +r)\n\
                        2. Parent directories have execute permissions (chmod +x)\n\
                        3. Process runs as user with file access",
                        path_display
                    )
                }
                _ => String::new(),
            };

            crate::error::AppError::ConfigFileRead {
                path: path_display.clone(),
                source,
                remediation,
            }
        })?;

        // Phase 2: Parse TOML (preserves toml::de::Error context)
        let config: Self = toml::from_str(&content).map_err(|source| {
            crate::error::AppError::ConfigParseFailed {
                path: path_display.clone(),
                source,
            }
        })?;

        // Phase 3: Validate parsed config (provides contextual reason)
        config
            .validate()
            .map_err(|e| crate::error::AppError::ConfigValidationFailed {
                path: path_display,
                reason: e.to_string(),
            })?;

        Ok(config)
    }

    /// Get timeout for a specific model tier
    ///
    /// Returns the per-tier timeout if configured, otherwise falls back to
    /// the global `server.request_timeout_seconds`.
    pub fn timeout_for_tier(&self, tier: crate::router::TargetModel) -> u64 {
        let tier_timeout = match tier {
            crate::router::TargetModel::Fast => self.timeouts.fast(),
            crate::router::TargetModel::Balanced => self.timeouts.balanced(),
            crate::router::TargetModel::Deep => self.timeouts.deep(),
        };

        match tier_timeout {
            Some(timeout) => {
                tracing::debug!(
                    tier = ?tier,
                    timeout_seconds = timeout,
                    "Using tier-specific timeout override"
                );
                timeout
            }
            None => {
                tracing::debug!(
                    tier = ?tier,
                    timeout_seconds = self.server.request_timeout_seconds,
                    "No tier-specific timeout configured, using global default"
                );
                self.server.request_timeout_seconds
            }
        }
    }

    /// Validate configuration after parsing
    ///
    /// This is called automatically by `from_file()`, but can also be called
    /// explicitly when constructing Config via other means (e.g., in tests).
    pub fn validate(&self) -> crate::error::AppResult<()> {
        // ═══════════════════════════════════════════════════════════════════════
        // Phase 1: Model Endpoint Field Validation
        // ═══════════════════════════════════════════════════════════════════════
        //
        // Validates individual endpoint configuration fields across all tiers:
        //   - max_tokens: must fit in u32 (API compatibility)
        //   - base_url: must start with http:// or https://, must end with /v1
        //   - temperature: must be finite number between 0.0 and 2.0
        //
        // Validate ModelEndpoint fields across all tiers
        for (tier_name, endpoints) in [
            ("fast", &self.models.fast),
            ("balanced", &self.models.balanced),
            ("deep", &self.models.deep),
        ] {
            for endpoint in endpoints {
                // Validate weight: must be positive and not NaN
                if endpoint.weight <= 0.0
                    || endpoint.weight.is_nan()
                    || endpoint.weight.is_infinite()
                {
                    return Err(crate::error::AppError::Config(format!(
                        "Configuration error: Endpoint '{}' in tier '{}' has invalid weight {}. \
                        Weight must be a positive finite number.",
                        endpoint.name, tier_name, endpoint.weight
                    )));
                }

                // Validate max_tokens: must be positive
                if endpoint.max_tokens == 0 {
                    return Err(crate::error::AppError::Config(format!(
                        "Configuration error: Endpoint '{}' in tier '{}' has max_tokens=0. \
                        max_tokens must be greater than 0.",
                        endpoint.name, tier_name
                    )));
                }

                // Validate max_tokens: must not exceed u32::MAX
                //
                // **SDK Compatibility & Defensive Validation**:
                // open-agent-sdk requires max_tokens to fit in u32 for API compatibility.
                // Values must be <= 4,294,967,295 (u32::MAX).
                //
                // This single check also provides defensive validation: no LLM supports >4 billion
                // tokens, so the u32::MAX limit naturally prevents configuration errors like setting
                // usize::MAX on 64-bit systems (which would be silently truncated). The check
                // simultaneously ensures API correctness and reasonable configuration limits.
                if endpoint.max_tokens > u32::MAX as usize {
                    return Err(crate::error::AppError::Config(format!(
                        "Configuration error: Endpoint '{}' in tier '{}' has max_tokens={} which exceeds u32::MAX ({}). \
                        max_tokens must fit in u32 for compatibility with open-agent-sdk.",
                        endpoint.name,
                        tier_name,
                        endpoint.max_tokens,
                        u32::MAX
                    )));
                }

                // Validate base_url: must start with http:// or https://
                if !endpoint.base_url.starts_with("http://")
                    && !endpoint.base_url.starts_with("https://")
                {
                    return Err(crate::error::AppError::Config(format!(
                        "Configuration error: Endpoint '{}' in tier '{}' has invalid base_url '{}'. \
                        base_url must start with 'http://' or 'https://'.",
                        endpoint.name, tier_name, endpoint.base_url
                    )));
                }

                // Validate base_url: must end with /v1 (OpenAI API compatibility)
                if !endpoint.base_url.ends_with("/v1") {
                    return Err(crate::error::AppError::Config(format!(
                        "Configuration error: Endpoint '{}' in tier '{}' has invalid base_url '{}'. \
                        base_url must end with '/v1' (e.g., 'http://host:port/v1') for OpenAI API compatibility.",
                        endpoint.name, tier_name, endpoint.base_url
                    )));
                }

                // Validate temperature: must be between 0.0 and 2.0 (standard LLM range)
                if endpoint.temperature < 0.0
                    || endpoint.temperature > 2.0
                    || endpoint.temperature.is_nan()
                    || endpoint.temperature.is_infinite()
                {
                    return Err(crate::error::AppError::Config(format!(
                        "Configuration error: Endpoint '{}' in tier '{}' has invalid temperature {}. \
                        temperature must be a finite number between 0.0 and 2.0.",
                        endpoint.name, tier_name, endpoint.temperature
                    )));
                }
            }
        }

        // ═══════════════════════════════════════════════════════════════════════
        // Phase 2: All-Tier Availability Validation
        // ═══════════════════════════════════════════════════════════════════════
        //
        // P1 FIX: Require ALL tiers (Fast/Balanced/Deep) to have at least one endpoint.
        //
        // RATIONALE: Both RuleBasedRouter and LlmBasedRouter can route to ANY tier
        // based on request characteristics. If a tier is empty and gets selected,
        // requests fail at runtime with "No available healthy endpoints" instead
        // of failing at startup validation.
        //
        // This validation ensures config errors are caught at startup, not runtime.
        //
        // Note: router_tier format is already validated during deserialization (Phase 1).

        // Check Fast tier
        if self.models.fast.is_empty() {
            return Err(crate::error::AppError::Config(
                "Configuration error: models.fast has no endpoints. \
                All three tiers (fast, balanced, deep) must have at least one endpoint \
                because routers can select any tier based on request characteristics. \
                See config.toml or tests for configuration examples."
                    .to_string(),
            ));
        }

        // Check Balanced tier
        if self.models.balanced.is_empty() {
            return Err(crate::error::AppError::Config(
                "Configuration error: models.balanced has no endpoints. \
                All three tiers (fast, balanced, deep) must have at least one endpoint \
                because routers can select any tier based on request characteristics. \
                See config.toml or tests for configuration examples."
                    .to_string(),
            ));
        }

        // Check Deep tier
        if self.models.deep.is_empty() {
            return Err(crate::error::AppError::Config(
                "Configuration error: models.deep has no endpoints. \
                All three tiers (fast, balanced, deep) must have at least one endpoint \
                because routers can select any tier based on request characteristics. \
                See config.toml or tests for configuration examples."
                    .to_string(),
            ));
        }

        // ═══════════════════════════════════════════════════════════════════════
        // Phase 2.5: Endpoint Name Cross-Tier Uniqueness Validation
        // ═══════════════════════════════════════════════════════════════════════
        //
        // Validate that endpoint names are unique ACROSS tiers (not within).
        //
        // RATIONALE:
        // - Duplicates WITHIN a tier are valid (load balancing replicas of same model)
        // - Duplicates ACROSS tiers are confusing: find_endpoint_by_name searches
        //   fast -> balanced -> deep and returns the first match. If "qwen3-8b" exists
        //   in both fast and balanced, requesting model="qwen3-8b" always uses fast.
        //
        // This validation catches cross-tier duplicates at startup with a clear message.
        {
            use std::collections::HashMap;
            let mut name_to_tier: HashMap<&str, &str> = HashMap::new();

            for (tier_name, endpoints) in [
                ("fast", &self.models.fast),
                ("balanced", &self.models.balanced),
                ("deep", &self.models.deep),
            ] {
                for endpoint in endpoints {
                    if let Some(existing_tier) = name_to_tier.get(endpoint.name.as_str()) {
                        // Only error if it's a DIFFERENT tier
                        if *existing_tier != tier_name {
                            return Err(crate::error::AppError::Config(format!(
                                "Configuration error: Endpoint name '{}' exists in both '{}' and '{}' tiers. \
                                Endpoint names must be unique across different tiers. \
                                Duplicates within the same tier (for load balancing) are allowed.",
                                endpoint.name, existing_tier, tier_name
                            )));
                        }
                        // Same tier duplicate is OK (load balancing)
                    } else {
                        name_to_tier.insert(&endpoint.name, tier_name);
                    }
                }
            }
        }

        // Validate request timeout
        if self.server.request_timeout_seconds == 0 {
            return Err(crate::error::AppError::Config(
                "Configuration error: request_timeout_seconds must be greater than 0".to_string(),
            ));
        }
        if self.server.request_timeout_seconds > 300 {
            return Err(crate::error::AppError::Config(format!(
                "Configuration error: request_timeout_seconds cannot exceed 300 seconds (5 minutes), got {}",
                self.server.request_timeout_seconds
            )));
        }

        // Per-tier timeout validation is now handled by TimeoutsConfig's custom Deserialize
        // implementation, which calls the validated constructor at parse time.
        // No duplicate validation needed here.

        // Validate router query timeouts
        self.routing
            .router_timeouts
            .validate()
            .map_err(|e| crate::error::AppError::Config(format!("Configuration error: {}", e)))?;

        // ═══════════════════════════════════════════════════════════════════════
        // Phase 3: HTTP Client Creation Validation
        // ═══════════════════════════════════════════════════════════════════════
        //
        // CRITICAL FIX: Validate that HTTP client can be created (catches TLS errors early)
        //
        // RATIONALE: TLS configuration errors (invalid certificates, missing CA bundle, etc.)
        // would previously cause panics in the background health checking task, crashing
        // the entire server. By validating client creation at startup, we:
        //   1. Fail fast with a clear error message to operators
        //   2. Prevent server crashes during health checks
        //   3. Give operators actionable feedback to fix TLS issues
        //
        // This check does not make actual HTTP requests - it only validates that
        // the HTTP client can be constructed with the system's TLS libraries.
        reqwest::Client::builder()
            .timeout(std::time::Duration::from_secs(5))
            .build()
            .map_err(|e| {
                crate::error::AppError::Config(format!(
                    "Failed to create HTTP client (TLS configuration error): {}.\n\
                    This usually indicates:\n\
                    - Invalid or expired TLS certificates\n\
                    - Missing CA certificate bundle\n\
                    - Incompatible system TLS libraries\n\
                    \n\
                    Please check your system's TLS configuration and certificates.",
                    e
                ))
            })?;

        Ok(())
    }
}

impl FromStr for Config {
    type Err = crate::error::AppError;

    fn from_str(toml_str: &str) -> Result<Self, Self::Err> {
        let config: Config = toml::from_str(toml_str).map_err(|source| {
            // Provide context about where parsing failed
            // The source error already contains line/column information from toml crate
            // We enhance the path field to indicate this is from string parsing and
            // provide size information to help identify which config string failed
            let path_with_context = format!(
                "<string> ({} bytes, {} lines)",
                toml_str.len(),
                toml_str.lines().count()
            );

            crate::error::AppError::ConfigParseFailed {
                path: path_with_context,
                source,
            }
        })?;

        // Validate config before returning
        config.validate()?;
        Ok(config)
    }
}

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

    const TEST_CONFIG: &str = r#"
[server]
host = "0.0.0.0"
port = 3000
request_timeout_seconds = 30

[[models.fast]]
name = "qwen/qwen3-vl-8b"
base_url = "http://192.168.1.67:1234/v1"
max_tokens = 4096
temperature = 0.7
weight = 1.0
priority = 1

[[models.fast]]
name = "qwen/qwen3-vl-8b"
base_url = "http://192.168.1.72:1234/v1"
max_tokens = 4096
temperature = 0.7
weight = 1.0
priority = 1

[[models.balanced]]
name = "qwen/qwen3-30b-a3b-2507"
base_url = "http://192.168.1.61:1234/v1"
max_tokens = 8192
temperature = 0.7
weight = 1.0
priority = 1

[[models.deep]]
name = "/home/steve/dev/llama.cpp/models/gpt-oss-120b-mxfp4.gguf"
base_url = "https://strix-ai.localbrandonfamily.com/v1"
max_tokens = 16384
temperature = 0.7
weight = 1.0
priority = 1

[routing]
strategy = "hybrid"
default_importance = "normal"
router_tier = "balanced"

[observability]
log_level = "info"
"#;

    #[test]
    fn test_config_from_str_parses_successfully() {
        let config = Config::from_str(TEST_CONFIG).expect("should parse config");
        assert_eq!(config.server.host, "0.0.0.0");
        assert_eq!(config.server.port, 3000);
        assert_eq!(config.server.request_timeout_seconds, 30);
    }

    #[test]
    fn test_config_parses_model_endpoints() {
        let config = Config::from_str(TEST_CONFIG).expect("should parse config");

        // Fast tier - 2 models
        assert_eq!(config.models.fast.len(), 2);
        assert_eq!(config.models.fast[0].name, "qwen/qwen3-vl-8b");
        assert_eq!(
            config.models.fast[0].base_url,
            "http://192.168.1.67:1234/v1"
        );
        assert_eq!(config.models.fast[0].max_tokens, 4096);
        assert_eq!(config.models.fast[0].weight, 1.0);
        assert_eq!(config.models.fast[0].priority, 1);

        assert_eq!(
            config.models.fast[1].base_url,
            "http://192.168.1.72:1234/v1"
        );

        // Balanced tier - 1 model
        assert_eq!(config.models.balanced.len(), 1);
        assert_eq!(config.models.balanced[0].name, "qwen/qwen3-30b-a3b-2507");

        // Deep tier - 1 model
        assert_eq!(config.models.deep.len(), 1);
        assert_eq!(config.models.deep[0].max_tokens, 16384);
    }

    #[test]
    fn test_config_parses_routing_strategy() {
        let config = Config::from_str(TEST_CONFIG).expect("should parse config");
        assert_eq!(config.routing.strategy, RoutingStrategy::Hybrid);
        assert_eq!(
            config.routing.default_importance,
            crate::router::Importance::Normal
        );
        assert_eq!(config.routing.router_tier(), TargetModel::Balanced);
    }

    #[test]
    fn test_config_parses_observability() {
        let config = Config::from_str(TEST_CONFIG).expect("should parse config");
        assert_eq!(config.observability.log_level, "info");
    }

    #[test]
    fn test_routing_strategy_enum_values() {
        assert_eq!(
            serde_json::from_str::<RoutingStrategy>(r#""rule""#)
                .expect("Test operation should succeed"),
            RoutingStrategy::Rule
        );
        assert_eq!(
            serde_json::from_str::<RoutingStrategy>(r#""llm""#)
                .expect("Test operation should succeed"),
            RoutingStrategy::Llm
        );
        assert_eq!(
            serde_json::from_str::<RoutingStrategy>(r#""hybrid""#)
                .expect("Test operation should succeed"),
            RoutingStrategy::Hybrid
        );
        assert_eq!(
            serde_json::from_str::<RoutingStrategy>(r#""tool""#)
                .expect("Test operation should succeed"),
            RoutingStrategy::Tool
        );
    }

    #[test]
    fn test_config_with_missing_observability_uses_defaults() {
        let minimal_config = r#"
[server]
host = "127.0.0.1"
port = 8080

[[models.fast]]
name = "test-fast"
base_url = "http://localhost:1234/v1"
max_tokens = 2048

[[models.balanced]]
name = "test-balanced"
base_url = "http://localhost:1235/v1"
max_tokens = 4096

[[models.deep]]
name = "test-deep"
base_url = "http://localhost:1236/v1"
max_tokens = 8192

[routing]
strategy = "rule"
default_importance = "normal"
router_tier = "balanced"
"#;

        let config = Config::from_str(minimal_config).expect("should parse minimal config");
        assert_eq!(config.observability.log_level, "info");

        // Verify defaults for weight and priority
        assert_eq!(config.models.fast[0].weight, 1.0);
        assert_eq!(config.models.fast[0].priority, 1);
    }

    #[test]
    fn test_config_validation_invalid_router_tier_fails() {
        let config_str = r#"
[server]
host = "127.0.0.1"
port = 8080

[[models.fast]]
name = "test"
base_url = "http://localhost:1234/v1"
max_tokens = 4096

[[models.balanced]]
name = "test"
base_url = "http://localhost:1235/v1"
max_tokens = 8192

[[models.deep]]
name = "test"
base_url = "http://localhost:1236/v1"
max_tokens = 16384

[routing]
strategy = "rule"
router_tier = "invalid"
"#;

        // Serde should reject invalid router_tier at deserialization time
        let result = Config::from_str(config_str);
        assert!(
            result.is_err(),
            "Should fail to deserialize invalid router_tier"
        );
        let err_msg = result.unwrap_err().to_string();
        assert!(err_msg.contains("router_tier") || err_msg.contains("invalid"));
        assert!(
            err_msg.contains("fast") || err_msg.contains("balanced") || err_msg.contains("deep"),
            "Error should list valid values"
        );
    }

    #[test]
    fn test_config_validation_router_tier_with_no_endpoints_fails() {
        // Parse config with router_tier="deep" and LLM strategy
        let config_str = r#"
[server]
host = "127.0.0.1"
port = 3000
request_timeout_seconds = 30

[[models.fast]]
name = "fast-1"
base_url = "http://localhost:11434/v1"
max_tokens = 2048
weight = 1.0
priority = 1

[[models.balanced]]
name = "balanced-1"
base_url = "http://localhost:1234/v1"
max_tokens = 4096
weight = 1.0
priority = 1

[[models.deep]]
name = "deep-1"
base_url = "http://localhost:8080/v1"
max_tokens = 8192
weight = 1.0
priority = 1

[routing]
strategy = "llm"
default_importance = "normal"
router_tier = "deep"
"#;
        let mut config = Config::from_str(config_str).expect("Test operation should succeed");

        // Clear deep endpoints to test validation
        config.models.deep.clear();

        let result = config.validate();
        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(err_msg.contains("deep") || err_msg.contains("Deep"));
        assert!(err_msg.contains("endpoint"));
    }

    #[test]
    fn test_config_router_tier_defaults_to_balanced() {
        // Test that configs without router_tier field use Balanced as default
        // This ensures backward compatibility with configs that don't specify router_tier
        let config_str = r#"
[server]
host = "127.0.0.1"
port = 3000

[[models.fast]]
name = "test-fast"
base_url = "http://localhost:1234/v1"
max_tokens = 2048

[[models.balanced]]
name = "test-balanced"
base_url = "http://localhost:1235/v1"
max_tokens = 4096

[[models.deep]]
name = "test-deep"
base_url = "http://localhost:1236/v1"
max_tokens = 8192

[routing]
strategy = "rule"
# router_tier omitted - should default to balanced
"#;

        let config: Config =
            toml::from_str(config_str).expect("should parse config without router_tier");

        assert_eq!(
            config.routing.router_tier(),
            TargetModel::Balanced,
            "router_tier should default to Balanced when omitted"
        );
    }

    #[test]
    fn test_config_validation_negative_weight_fails() {
        let mut config = Config::from_str(TEST_CONFIG).expect("Test operation should succeed");
        config.models.fast[0].weight = -1.0; // Invalid: negative weight

        let result = config.validate();
        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(err_msg.contains("weight"));
        assert!(err_msg.contains("positive"));
    }

    #[test]
    fn test_config_validation_zero_weight_fails() {
        let mut config = Config::from_str(TEST_CONFIG).expect("Test operation should succeed");
        config.models.balanced[0].weight = 0.0; // Invalid: zero weight

        let result = config.validate();
        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(err_msg.contains("weight"));
        assert!(err_msg.contains("positive"));
    }

    #[test]
    fn test_config_validation_nan_weight_fails() {
        let mut config = Config::from_str(TEST_CONFIG).expect("Test operation should succeed");
        config.models.deep[0].weight = f64::NAN; // Invalid: NaN weight

        let result = config.validate();
        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(err_msg.contains("weight"));
    }

    #[test]
    fn test_config_validation_zero_max_tokens_fails() {
        let mut config = Config::from_str(TEST_CONFIG).expect("Test operation should succeed");
        config.models.fast[0].max_tokens = 0; // Invalid: zero max_tokens

        let result = config.validate();
        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(err_msg.contains("max_tokens"));
        assert!(err_msg.contains("greater than 0"));
    }

    #[test]
    fn test_config_validation_invalid_base_url_fails() {
        let mut config = Config::from_str(TEST_CONFIG).expect("Test operation should succeed");
        config.models.balanced[0].base_url = "ftp://invalid.com".to_string(); // Invalid: not http/https

        let result = config.validate();
        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(err_msg.contains("base_url"));
        assert!(err_msg.contains("http"));
    }

    #[test]
    fn test_config_validation_missing_protocol_base_url_fails() {
        let mut config = Config::from_str(TEST_CONFIG).expect("Test operation should succeed");
        config.models.deep[0].base_url = "localhost:1234/v1".to_string(); // Invalid: missing protocol

        let result = config.validate();
        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(err_msg.contains("base_url"));
        assert!(err_msg.contains("http"));
    }

    #[test]
    fn test_config_validation_base_url_must_end_with_v1() {
        let mut config = Config::from_str(TEST_CONFIG).expect("Test operation should succeed");
        config.models.fast[0].base_url = "http://localhost:1234".to_string(); // Invalid: missing /v1

        let result = config.validate();
        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(err_msg.contains("base_url"));
        assert!(err_msg.contains("/v1"));
        assert!(err_msg.contains("OpenAI API"));
    }

    #[test]
    fn test_config_validation_zero_timeout_fails() {
        let mut config = Config::from_str(TEST_CONFIG).expect("Test operation should succeed");
        config.server.request_timeout_seconds = 0; // Invalid: zero timeout

        let result = config.validate();
        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(
            err_msg.contains("request_timeout_seconds") && err_msg.contains("greater than 0"),
            "Expected error about request_timeout_seconds > 0, got: {}",
            err_msg
        );
    }

    #[test]
    fn test_config_validation_excessive_timeout_fails() {
        let mut config = Config::from_str(TEST_CONFIG).expect("Test operation should succeed");
        config.server.request_timeout_seconds = 301; // Invalid: exceeds 300 second limit

        let result = config.validate();
        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(
            err_msg.contains("request_timeout_seconds") && err_msg.contains("300"),
            "Expected error about request_timeout_seconds max 300, got: {}",
            err_msg
        );
    }

    #[test]
    fn test_config_validation_valid_timeout_succeeds() {
        let mut config = Config::from_str(TEST_CONFIG).expect("Test operation should succeed");

        // Test lower bound (1 second)
        config.server.request_timeout_seconds = 1;
        assert!(config.validate().is_ok());

        // Test upper bound (300 seconds)
        config.server.request_timeout_seconds = 300;
        assert!(config.validate().is_ok());

        // Test typical value (30 seconds)
        config.server.request_timeout_seconds = 30;
        assert!(config.validate().is_ok());
    }

    // ===== Per-Tier Timeout Tests (RED phase) =====

    #[test]
    fn test_config_parses_per_tier_timeouts() {
        let config_with_timeouts = r#"
[server]
host = "127.0.0.1"
port = 3000
request_timeout_seconds = 30

[[models.fast]]
name = "test-fast"
base_url = "http://localhost:1234/v1"
max_tokens = 4096

[[models.balanced]]
name = "test-balanced"
base_url = "http://localhost:1235/v1"
max_tokens = 8192

[[models.deep]]
name = "test-deep"
base_url = "http://localhost:1236/v1"
max_tokens = 16384

[routing]
strategy = "rule"
router_tier = "balanced"

[timeouts]
fast = 15
balanced = 30
deep = 60
"#;

        let config =
            Config::from_str(config_with_timeouts).expect("should parse config with timeouts");
        assert_eq!(config.timeouts.fast, Some(15));
        assert_eq!(config.timeouts.balanced, Some(30));
        assert_eq!(config.timeouts.deep, Some(60));
    }

    #[test]
    fn test_config_timeouts_optional_fields_default_to_none() {
        let config_partial_timeouts = r#"
[server]
host = "127.0.0.1"
port = 3000

[[models.fast]]
name = "test-fast"
base_url = "http://localhost:1234/v1"
max_tokens = 4096

[[models.balanced]]
name = "test-balanced"
base_url = "http://localhost:1235/v1"
max_tokens = 8192

[[models.deep]]
name = "test-deep"
base_url = "http://localhost:1236/v1"
max_tokens = 16384

[routing]
strategy = "rule"
router_tier = "balanced"

[timeouts]
fast = 15
# balanced and deep use global default
"#;

        let config =
            Config::from_str(config_partial_timeouts).expect("should parse partial timeouts");
        assert_eq!(config.timeouts.fast, Some(15));
        assert_eq!(config.timeouts.balanced, None); // Uses global default
        assert_eq!(config.timeouts.deep, None); // Uses global default
    }

    #[test]
    fn test_config_timeouts_section_optional() {
        // Config without [timeouts] section should work
        let config = Config::from_str(TEST_CONFIG).expect("should parse without timeouts section");
        assert_eq!(config.timeouts.fast, None);
        assert_eq!(config.timeouts.balanced, None);
        assert_eq!(config.timeouts.deep, None);
    }

    #[test]
    fn test_config_timeout_for_tier_uses_override() {
        let mut config = Config::from_str(TEST_CONFIG).expect("Test operation should succeed");
        config.server.request_timeout_seconds = 30; // Global default
        config.timeouts.fast = Some(15);
        config.timeouts.balanced = Some(45);
        config.timeouts.deep = Some(60);

        // Should use per-tier overrides
        assert_eq!(
            config.timeout_for_tier(crate::router::TargetModel::Fast),
            15
        );
        assert_eq!(
            config.timeout_for_tier(crate::router::TargetModel::Balanced),
            45
        );
        assert_eq!(
            config.timeout_for_tier(crate::router::TargetModel::Deep),
            60
        );
    }

    #[test]
    fn test_config_timeout_for_tier_uses_global_default() {
        let config = Config::from_str(TEST_CONFIG).expect("Test operation should succeed");
        // No per-tier overrides, should use global default (30s)

        assert_eq!(
            config.timeout_for_tier(crate::router::TargetModel::Fast),
            30
        );
        assert_eq!(
            config.timeout_for_tier(crate::router::TargetModel::Balanced),
            30
        );
        assert_eq!(
            config.timeout_for_tier(crate::router::TargetModel::Deep),
            30
        );
    }

    #[test]
    fn test_config_timeout_for_tier_mixed_overrides() {
        let mut config = Config::from_str(TEST_CONFIG).expect("Test operation should succeed");
        config.server.request_timeout_seconds = 40; // Global default
        config.timeouts.fast = Some(20); // Override only fast tier

        // Fast tier uses override
        assert_eq!(
            config.timeout_for_tier(crate::router::TargetModel::Fast),
            20
        );
        // Balanced and deep use global default
        assert_eq!(
            config.timeout_for_tier(crate::router::TargetModel::Balanced),
            40
        );
        assert_eq!(
            config.timeout_for_tier(crate::router::TargetModel::Deep),
            40
        );
    }

    // ===== Issue #3 Fix: TimeoutsConfig Custom Deserialize Tests =====
    // Tests written FIRST (TDD RED phase) - these should fail until custom Deserialize is implemented

    #[test]
    fn test_timeouts_config_deserialization_rejects_zero_timeout() {
        // Test that zero timeout is rejected DURING deserialization, not later during validate()
        let config_with_zero_timeout = r#"
[server]
host = "127.0.0.1"
port = 3000

[[models.fast]]
name = "test-fast"
base_url = "http://localhost:1234/v1"
max_tokens = 4096

[[models.balanced]]
name = "test-balanced"
base_url = "http://localhost:1235/v1"
max_tokens = 8192

[[models.deep]]
name = "test-deep"
base_url = "http://localhost:1236/v1"
max_tokens = 16384

[routing]
strategy = "rule"
router_tier = "balanced"

[timeouts]
fast = 0
"#;

        let result = Config::from_str(config_with_zero_timeout);
        assert!(
            result.is_err(),
            "Config parsing should fail with zero timeout (custom Deserialize should reject it)"
        );
        let err_msg = result.unwrap_err().to_string();
        assert!(
            err_msg.contains("fast") && err_msg.contains("timeout"),
            "Error should mention which timeout field is invalid"
        );
    }

    #[test]
    fn test_timeouts_config_deserialization_rejects_timeout_too_high() {
        // Test that timeout > 300 is rejected DURING deserialization
        let config_with_high_timeout = r#"
[server]
host = "127.0.0.1"
port = 3000

[[models.fast]]
name = "test-fast"
base_url = "http://localhost:1234/v1"
max_tokens = 4096

[[models.balanced]]
name = "test-balanced"
base_url = "http://localhost:1235/v1"
max_tokens = 8192

[[models.deep]]
name = "test-deep"
base_url = "http://localhost:1236/v1"
max_tokens = 16384

[routing]
strategy = "rule"
router_tier = "balanced"

[timeouts]
deep = 301
"#;

        let result = Config::from_str(config_with_high_timeout);
        assert!(
            result.is_err(),
            "Config parsing should fail with timeout > 300 (custom Deserialize should reject it)"
        );
        let err_msg = result.unwrap_err().to_string();
        assert!(
            err_msg.contains("deep") && err_msg.contains("300"),
            "Error should mention which timeout field exceeds limit"
        );
    }

    #[test]
    fn test_timeouts_config_deserialization_accepts_valid_timeouts() {
        // Test that valid timeouts are accepted during deserialization
        let config_with_valid_timeouts = r#"
[server]
host = "127.0.0.1"
port = 3000

[[models.fast]]
name = "test-fast"
base_url = "http://localhost:1234/v1"
max_tokens = 4096

[[models.balanced]]
name = "test-balanced"
base_url = "http://localhost:1235/v1"
max_tokens = 8192

[[models.deep]]
name = "test-deep"
base_url = "http://localhost:1236/v1"
max_tokens = 16384

[routing]
strategy = "rule"
router_tier = "balanced"

[timeouts]
fast = 15
balanced = 30
deep = 60
"#;

        let result = Config::from_str(config_with_valid_timeouts);
        assert!(
            result.is_ok(),
            "Config parsing should succeed with valid timeouts (1-300)"
        );
        let config = result.expect("Test operation should succeed");
        assert_eq!(config.timeouts.fast(), Some(15));
        assert_eq!(config.timeouts.balanced(), Some(30));
        assert_eq!(config.timeouts.deep(), Some(60));
    }

    #[test]
    fn test_timeouts_config_deserialization_accepts_boundary_values() {
        // Test that boundary values (1 and 300) are accepted
        let config_with_boundaries = r#"
[server]
host = "127.0.0.1"
port = 3000

[[models.fast]]
name = "test-fast"
base_url = "http://localhost:1234/v1"
max_tokens = 4096

[[models.balanced]]
name = "test-balanced"
base_url = "http://localhost:1235/v1"
max_tokens = 8192

[[models.deep]]
name = "test-deep"
base_url = "http://localhost:1236/v1"
max_tokens = 16384

[routing]
strategy = "rule"
router_tier = "balanced"

[timeouts]
fast = 1
deep = 300
"#;

        let result = Config::from_str(config_with_boundaries);
        assert!(
            result.is_ok(),
            "Config parsing should succeed with boundary values 1 and 300"
        );
        let config = result.expect("Test operation should succeed");
        assert_eq!(config.timeouts.fast(), Some(1));
        assert_eq!(config.timeouts.deep(), Some(300));
    }
}