clawft-core 0.2.1

Core engine for clawft (agent loop, message bus, context management)
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
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
//! Routing configuration validation.
//!
//! Post-deserialization validation for [`RoutingConfig`] and its nested types.
//! Serde handles structural correctness (JSON types, field names), but cannot
//! enforce semantic constraints like "tier names must be unique" or "complexity
//! min must be less than max." This module fills that gap.
//!
//! Validation runs after deserialization, before `TieredRouter` construction.
//! When `routing.mode` is `"static"`, validation is skipped entirely.

use std::collections::HashSet;

use clawft_types::routing::{ModelTierConfig, PermissionLevelConfig, RoutingConfig};

// ── ValidationSeverity ──────────────────────────────────────────────────

/// Severity level for a validation diagnostic.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ValidationSeverity {
    /// Fatal: the config is invalid and the system cannot start.
    Error,
    /// Non-fatal: the config is technically valid but may indicate a
    /// misconfiguration. Logged at WARN level during startup.
    Warning,
}

// ── ValidationError ─────────────────────────────────────────────────────

/// A single validation diagnostic for the routing config.
///
/// Collects a human-readable field path, a descriptive message, and a
/// severity level. All diagnostics are gathered before returning so
/// operators see every issue at once.
#[derive(Debug, Clone)]
pub struct ValidationError {
    /// Dotted field path (e.g., `"routing.tiers[1].complexity_range"`).
    pub field: String,
    /// Human-readable description of the problem.
    pub message: String,
    /// Whether this is a fatal error or a non-fatal warning.
    pub severity: ValidationSeverity,
}

impl std::fmt::Display for ValidationError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let level = match self.severity {
            ValidationSeverity::Error => "ERROR",
            ValidationSeverity::Warning => "WARN",
        };
        write!(f, "[{}] {}: {}", level, self.field, self.message)
    }
}

impl std::error::Error for ValidationError {}

// ── Main validation entry point ─────────────────────────────────────────

/// Validate a [`RoutingConfig`] and return all errors/warnings found.
///
/// When `mode` is `"static"`, validation is skipped entirely (static mode
/// ignores routing config). Validation only applies when `mode = "tiered"`.
///
/// All diagnostics are collected in a single pass -- the function does not
/// short-circuit on the first error.
pub fn validate_routing_config(config: &RoutingConfig) -> Vec<ValidationError> {
    let mut errors = Vec::new();

    // Static mode skips all routing validation (backward compatibility).
    if config.mode == "static" {
        return errors;
    }

    validate_mode(config, &mut errors);
    validate_tiers(config, &mut errors);
    validate_permissions(config, &mut errors);
    validate_escalation(config, &mut errors);
    validate_cost_budgets(config, &mut errors);
    validate_rate_limiting(config, &mut errors);
    validate_fallback_model(config, &mut errors);

    errors
}

// ── Mode validation ─────────────────────────────────────────────────────

/// Rule 1: `mode` must be `"static"` or `"tiered"`.
fn validate_mode(config: &RoutingConfig, errors: &mut Vec<ValidationError>) {
    if config.mode != "static" && config.mode != "tiered" {
        errors.push(ValidationError {
            field: "routing.mode".into(),
            message: format!(
                "unknown mode '{}', expected 'static' or 'tiered'",
                config.mode
            ),
            severity: ValidationSeverity::Error,
        });
    }
}

// ── Tier validation ─────────────────────────────────────────────────────

/// Rules 2-8: tier-level validations.
fn validate_tiers(config: &RoutingConfig, errors: &mut Vec<ValidationError>) {
    // Rule 2: if mode is "tiered", at least one tier must be defined.
    if config.mode == "tiered" && config.tiers.is_empty() {
        errors.push(ValidationError {
            field: "routing.tiers".into(),
            message: "tiered mode requires at least one tier definition".into(),
            severity: ValidationSeverity::Error,
        });
    }

    // Rule 3: no duplicate tier names.
    let mut seen_names: HashSet<&str> = HashSet::new();
    for (i, tier) in config.tiers.iter().enumerate() {
        if !seen_names.insert(&tier.name) {
            errors.push(ValidationError {
                field: format!("routing.tiers[{}].name", i),
                message: format!("duplicate tier name '{}'", tier.name),
                severity: ValidationSeverity::Error,
            });
        }

        validate_single_tier(i, tier, errors);
    }

    // Warning: overlapping complexity ranges (intentional by design but
    // worth logging for operator awareness).
    for i in 0..config.tiers.len() {
        for j in (i + 1)..config.tiers.len() {
            let a = &config.tiers[i];
            let b = &config.tiers[j];
            let overlap_min = a.complexity_range[0].max(b.complexity_range[0]);
            let overlap_max = a.complexity_range[1].min(b.complexity_range[1]);
            if overlap_min < overlap_max {
                errors.push(ValidationError {
                    field: format!("routing.tiers[{}]+[{}]", i, j),
                    message: format!(
                        "tiers '{}' and '{}' have overlapping complexity ranges \
                         [{:.2}, {:.2}]",
                        a.name, b.name, overlap_min, overlap_max
                    ),
                    severity: ValidationSeverity::Warning,
                });
            }
        }
    }
}

/// Validate a single tier's fields.
fn validate_single_tier(index: usize, tier: &ModelTierConfig, errors: &mut Vec<ValidationError>) {
    let prefix = format!("routing.tiers[{}]", index);

    // Rule 4: each tier must have at least one model (warning).
    if tier.models.is_empty() {
        errors.push(ValidationError {
            field: format!("{}.models", prefix),
            message: format!(
                "tier '{}' has no models -- it is defined but unusable",
                tier.name
            ),
            severity: ValidationSeverity::Warning,
        });
    }

    // Rule 5: complexity_range[0] <= complexity_range[1].
    let [min, max] = tier.complexity_range;
    if min > max {
        errors.push(ValidationError {
            field: format!("{}.complexity_range", prefix),
            message: format!(
                "complexity_range min ({}) > max ({}) for tier '{}'",
                min, max, tier.name
            ),
            severity: ValidationSeverity::Error,
        });
    }

    // Rule 6: complexity_range values in [0.0, 1.0].
    if !(0.0..=1.0).contains(&min) {
        errors.push(ValidationError {
            field: format!("{}.complexity_range[0]", prefix),
            message: format!(
                "complexity_range[0] = {} is outside [0.0, 1.0] for tier '{}'",
                min, tier.name
            ),
            severity: ValidationSeverity::Error,
        });
    }
    if !(0.0..=1.0).contains(&max) {
        errors.push(ValidationError {
            field: format!("{}.complexity_range[1]", prefix),
            message: format!(
                "complexity_range[1] = {} is outside [0.0, 1.0] for tier '{}'",
                max, tier.name
            ),
            severity: ValidationSeverity::Error,
        });
    }

    // Rule 7: cost_per_1k_tokens >= 0.0.
    if tier.cost_per_1k_tokens < 0.0 {
        errors.push(ValidationError {
            field: format!("{}.cost_per_1k_tokens", prefix),
            message: format!(
                "cost_per_1k_tokens = {} must be non-negative for tier '{}'",
                tier.cost_per_1k_tokens, tier.name
            ),
            severity: ValidationSeverity::Error,
        });
    }

    // Rule 8: max_context_tokens > 0.
    if tier.max_context_tokens == 0 {
        errors.push(ValidationError {
            field: format!("{}.max_context_tokens", prefix),
            message: format!("max_context_tokens must be > 0 for tier '{}'", tier.name),
            severity: ValidationSeverity::Error,
        });
    }

    // Warning: model format should contain '/' (provider/model).
    for (j, model) in tier.models.iter().enumerate() {
        if !model.contains('/') {
            errors.push(ValidationError {
                field: format!("{}.models[{}]", prefix, j),
                message: format!(
                    "model '{}' does not contain '/' (expected 'provider/model' format)",
                    model
                ),
                severity: ValidationSeverity::Warning,
            });
        }
    }
}

// ── Permission validation ───────────────────────────────────────────────

/// Rules 9-14: permission-level validations.
fn validate_permissions(config: &RoutingConfig, errors: &mut Vec<ValidationError>) {
    let valid_tier_names: HashSet<&str> = config.tiers.iter().map(|t| t.name.as_str()).collect();

    // Validate the three named permission levels.
    validate_permission_level(
        &config.permissions.zero_trust,
        "routing.permissions.zero_trust",
        &valid_tier_names,
        errors,
    );
    validate_permission_level(
        &config.permissions.user,
        "routing.permissions.user",
        &valid_tier_names,
        errors,
    );
    validate_permission_level(
        &config.permissions.admin,
        "routing.permissions.admin",
        &valid_tier_names,
        errors,
    );

    // Validate per-user overrides.
    for (user_id, plc) in &config.permissions.users {
        validate_permission_level(
            plc,
            &format!("routing.permissions.users.{}", user_id),
            &valid_tier_names,
            errors,
        );
    }

    // Validate per-channel overrides.
    for (channel, plc) in &config.permissions.channels {
        validate_permission_level(
            plc,
            &format!("routing.permissions.channels.{}", channel),
            &valid_tier_names,
            errors,
        );
    }
}

/// Validate a single [`PermissionLevelConfig`].
fn validate_permission_level(
    plc: &PermissionLevelConfig,
    field_prefix: &str,
    valid_tiers: &HashSet<&str>,
    errors: &mut Vec<ValidationError>,
) {
    // Rule 9: level values are 0, 1, or 2 (warn on others).
    if let Some(level) = plc.level
        && level > 2 {
            errors.push(ValidationError {
                field: format!("{}.level", field_prefix),
                message: format!(
                    "permission level {} is not recognized (expected 0, 1, or 2)",
                    level
                ),
                severity: ValidationSeverity::Warning,
            });
        }

    // Rule 10: escalation_threshold in [0.0, 1.0].
    if let Some(threshold) = plc.escalation_threshold
        && (!(0.0..=1.0).contains(&threshold)) {
            errors.push(ValidationError {
                field: format!("{}.escalation_threshold", field_prefix),
                message: format!("escalation_threshold {} must be in [0.0, 1.0]", threshold),
                severity: ValidationSeverity::Error,
            });
        }

    // Rule 11: cost_budget_daily_usd >= 0.0.
    if let Some(daily) = plc.cost_budget_daily_usd
        && daily < 0.0 {
            errors.push(ValidationError {
                field: format!("{}.cost_budget_daily_usd", field_prefix),
                message: format!("cost_budget_daily_usd {} must be non-negative", daily),
                severity: ValidationSeverity::Error,
            });
        }

    // Rule 12: cost_budget_monthly_usd >= 0.0.
    if let Some(monthly) = plc.cost_budget_monthly_usd
        && monthly < 0.0 {
            errors.push(ValidationError {
                field: format!("{}.cost_budget_monthly_usd", field_prefix),
                message: format!("cost_budget_monthly_usd {} must be non-negative", monthly),
                severity: ValidationSeverity::Error,
            });
        }

    // Rule 13: max_tier references an existing tier name (warning only).
    if let Some(ref max_tier) = plc.max_tier
        && !valid_tiers.is_empty() && !valid_tiers.contains(max_tier.as_str()) {
            errors.push(ValidationError {
                field: format!("{}.max_tier", field_prefix),
                message: format!(
                    "max_tier '{}' does not match any defined tier (available: {})",
                    max_tier,
                    valid_tiers.iter().copied().collect::<Vec<_>>().join(", ")
                ),
                severity: ValidationSeverity::Warning,
            });
        }

    // Rule 14: tool access patterns with `*` in middle generate warning.
    if let Some(ref tool_access) = plc.tool_access {
        for entry in tool_access {
            if entry.contains('*') && entry != "*" {
                errors.push(ValidationError {
                    field: format!("{}.tool_access", field_prefix),
                    message: format!(
                        "glob-like pattern '{}' detected; ensure pattern matching is intended",
                        entry
                    ),
                    severity: ValidationSeverity::Warning,
                });
            }
        }
    }
}

// ── Escalation validation ───────────────────────────────────────────────

/// Rules 15-16: escalation validations.
fn validate_escalation(config: &RoutingConfig, errors: &mut Vec<ValidationError>) {
    // Rule 15: max_escalation_tiers > 0 when escalation is enabled.
    if config.escalation.enabled && config.escalation.max_escalation_tiers == 0 {
        errors.push(ValidationError {
            field: "routing.escalation.max_escalation_tiers".into(),
            message: "max_escalation_tiers must be > 0 when escalation is enabled".into(),
            severity: ValidationSeverity::Error,
        });
    }

    // Rule 16: escalation threshold in [0.0, 1.0].
    if config.escalation.threshold < 0.0 || config.escalation.threshold > 1.0 {
        errors.push(ValidationError {
            field: "routing.escalation.threshold".into(),
            message: format!(
                "escalation threshold {} must be in [0.0, 1.0]",
                config.escalation.threshold
            ),
            severity: ValidationSeverity::Error,
        });
    }

    // Warning: max_escalation_tiers exceeds defined tier count.
    if config.escalation.max_escalation_tiers > config.tiers.len() as u32 {
        errors.push(ValidationError {
            field: "routing.escalation.max_escalation_tiers".into(),
            message: format!(
                "max_escalation_tiers ({}) exceeds the number of defined tiers ({})",
                config.escalation.max_escalation_tiers,
                config.tiers.len()
            ),
            severity: ValidationSeverity::Warning,
        });
    }
}

// ── Cost budget validation ──────────────────────────────────────────────

/// Cost budget validations.
fn validate_cost_budgets(config: &RoutingConfig, errors: &mut Vec<ValidationError>) {
    if config.cost_budgets.global_daily_limit_usd < 0.0 {
        errors.push(ValidationError {
            field: "routing.cost_budgets.global_daily_limit_usd".into(),
            message: format!(
                "global_daily_limit_usd {} must be non-negative",
                config.cost_budgets.global_daily_limit_usd
            ),
            severity: ValidationSeverity::Error,
        });
    }

    if config.cost_budgets.global_monthly_limit_usd < 0.0 {
        errors.push(ValidationError {
            field: "routing.cost_budgets.global_monthly_limit_usd".into(),
            message: format!(
                "global_monthly_limit_usd {} must be non-negative",
                config.cost_budgets.global_monthly_limit_usd
            ),
            severity: ValidationSeverity::Error,
        });
    }

    if config.cost_budgets.reset_hour_utc > 23 {
        errors.push(ValidationError {
            field: "routing.cost_budgets.reset_hour_utc".into(),
            message: format!(
                "reset_hour_utc {} must be 0-23",
                config.cost_budgets.reset_hour_utc
            ),
            severity: ValidationSeverity::Error,
        });
    }
}

// ── Rate limiting validation ────────────────────────────────────────────

/// Rate limiting validations.
fn validate_rate_limiting(config: &RoutingConfig, errors: &mut Vec<ValidationError>) {
    if config.rate_limiting.window_seconds == 0 {
        errors.push(ValidationError {
            field: "routing.rate_limiting.window_seconds".into(),
            message: "window_seconds must be > 0".into(),
            severity: ValidationSeverity::Error,
        });
    }

    let valid_strategies = ["sliding_window", "fixed_window"];
    if !valid_strategies.contains(&config.rate_limiting.strategy.as_str()) {
        errors.push(ValidationError {
            field: "routing.rate_limiting.strategy".into(),
            message: format!(
                "unknown strategy '{}', expected 'sliding_window' or 'fixed_window'",
                config.rate_limiting.strategy
            ),
            severity: ValidationSeverity::Error,
        });
    }
}

// ── Fallback model validation ───────────────────────────────────────────

/// Rule 17: fallback model format.
fn validate_fallback_model(config: &RoutingConfig, errors: &mut Vec<ValidationError>) {
    if let Some(ref model) = config.fallback_model
        && !model.contains('/') {
            errors.push(ValidationError {
                field: "routing.fallback_model".into(),
                message: format!(
                    "fallback_model '{}' should match 'provider/model' format",
                    model
                ),
                severity: ValidationSeverity::Warning,
            });
        }
}

// ── Workspace ceiling enforcement (FIX-04) ──────────────────────────────

/// Default maximum grantable permission level for workspace configs.
///
/// Until `max_grantable_level` is added to [`RoutingConfig`], this constant
/// provides the default ceiling. Workspaces cannot grant levels above this.
const DEFAULT_MAX_GRANTABLE_LEVEL: u8 = 1;

/// Validate that a workspace config does not exceed the global config ceiling.
///
/// Returns a [`Vec`] of validation errors for any ceiling violations found.
/// An empty [`Vec`] means the workspace is within bounds.
///
/// Security-sensitive ceiling fields:
/// - `level`: cannot exceed `DEFAULT_MAX_GRANTABLE_LEVEL` (1)
/// - `escalation_allowed`: cannot be true if global is false
/// - `tool_access`: cannot add tools not in global allowlist
/// - `rate_limit`: workspace cannot increase beyond global
/// - `cost_budget_daily_usd`: workspace cannot increase
/// - `cost_budget_monthly_usd`: workspace cannot increase
/// - `max_tier`: cannot upgrade beyond global's max_tier
pub fn validate_workspace_ceiling(
    global: &RoutingConfig,
    workspace: &RoutingConfig,
) -> Vec<ValidationError> {
    let mut errors = Vec::new();
    let max_grantable = DEFAULT_MAX_GRANTABLE_LEVEL;

    // Check the three named permission levels.
    check_level_ceiling(
        &workspace.permissions.zero_trust,
        "routing.permissions.zero_trust",
        &global.permissions.zero_trust,
        max_grantable,
        &mut errors,
    );
    check_level_ceiling(
        &workspace.permissions.user,
        "routing.permissions.user",
        &global.permissions.user,
        max_grantable,
        &mut errors,
    );
    check_level_ceiling(
        &workspace.permissions.admin,
        "routing.permissions.admin",
        &global.permissions.admin,
        max_grantable,
        &mut errors,
    );

    // Check per-user overrides.
    for (user_id, ws_plc) in &workspace.permissions.users {
        let global_plc = global
            .permissions
            .users
            .get(user_id)
            .unwrap_or(&global.permissions.user);
        check_level_ceiling(
            ws_plc,
            &format!("routing.permissions.users.{}", user_id),
            global_plc,
            max_grantable,
            &mut errors,
        );
    }

    // Check per-channel overrides.
    for (channel, ws_plc) in &workspace.permissions.channels {
        let global_plc = global
            .permissions
            .channels
            .get(channel)
            .unwrap_or(&global.permissions.user);
        check_level_ceiling(
            ws_plc,
            &format!("routing.permissions.channels.{}", channel),
            global_plc,
            max_grantable,
            &mut errors,
        );
    }

    errors
}

/// Check a single workspace permission level against the global ceiling.
fn check_level_ceiling(
    ws_plc: &PermissionLevelConfig,
    field_prefix: &str,
    global_plc: &PermissionLevelConfig,
    max_grantable: u8,
    errors: &mut Vec<ValidationError>,
) {
    // Level ceiling: workspace cannot grant level above max_grantable_level.
    if let Some(ws_level) = ws_plc.level
        && ws_level > max_grantable {
            errors.push(ValidationError {
                field: format!("{}.level", field_prefix),
                message: format!(
                    "workspace level {} exceeds max_grantable_level {}",
                    ws_level, max_grantable
                ),
                severity: ValidationSeverity::Error,
            });
        }

    // Escalation ceiling: workspace cannot enable escalation if global disables it.
    if let (Some(true), Some(false)) = (ws_plc.escalation_allowed, global_plc.escalation_allowed) {
        errors.push(ValidationError {
            field: format!("{}.escalation_allowed", field_prefix),
            message: "workspace cannot enable escalation when global disables it".into(),
            severity: ValidationSeverity::Error,
        });
    }

    // Tool access ceiling: workspace cannot add tools not in global allowlist.
    if let (Some(ws_tools), Some(global_tools)) =
        (&ws_plc.tool_access, &global_plc.tool_access)
    {
        // If global is ["*"], anything goes.
        if !global_tools.iter().any(|s| s == "*") {
            for ws_tool in ws_tools {
                if ws_tool == "*" {
                    errors.push(ValidationError {
                        field: format!("{}.tool_access", field_prefix),
                        message: "workspace cannot grant wildcard tool access \
                                  when global does not"
                            .into(),
                        severity: ValidationSeverity::Error,
                    });
                } else if !global_tools.contains(ws_tool) {
                    errors.push(ValidationError {
                        field: format!("{}.tool_access", field_prefix),
                        message: format!("workspace tool '{}' not in global tool_access", ws_tool),
                        severity: ValidationSeverity::Error,
                    });
                }
            }
        }
    }

    // Rate limit ceiling: workspace cannot increase beyond global.
    if let (Some(ws_rate), Some(global_rate)) = (ws_plc.rate_limit, global_plc.rate_limit)
        && global_rate > 0 && (ws_rate > global_rate || ws_rate == 0) {
            errors.push(ValidationError {
                field: format!("{}.rate_limit", field_prefix),
                message: format!(
                    "workspace rate_limit {} exceeds global ceiling {}",
                    ws_rate, global_rate
                ),
                severity: ValidationSeverity::Error,
            });
        }

    // Cost budget daily ceiling.
    if let (Some(ws_daily), Some(global_daily)) = (
        ws_plc.cost_budget_daily_usd,
        global_plc.cost_budget_daily_usd,
    )
        && global_daily > 0.0 && (ws_daily > global_daily || ws_daily == 0.0) {
            errors.push(ValidationError {
                field: format!("{}.cost_budget_daily_usd", field_prefix),
                message: format!(
                    "workspace daily budget {} exceeds global ceiling {}",
                    ws_daily, global_daily
                ),
                severity: ValidationSeverity::Error,
            });
        }

    // Cost budget monthly ceiling.
    if let (Some(ws_monthly), Some(global_monthly)) = (
        ws_plc.cost_budget_monthly_usd,
        global_plc.cost_budget_monthly_usd,
    )
        && global_monthly > 0.0 && (ws_monthly > global_monthly || ws_monthly == 0.0) {
            errors.push(ValidationError {
                field: format!("{}.cost_budget_monthly_usd", field_prefix),
                message: format!(
                    "workspace monthly budget {} exceeds global ceiling {}",
                    ws_monthly, global_monthly
                ),
                severity: ValidationSeverity::Error,
            });
        }
}

// ── Default tiers ───────────────────────────────────────────────────────

/// Generate a set of default tiers when mode is `"tiered"` but no tiers
/// are configured.
///
/// This enables the minimal config migration path:
/// ```json
/// { "routing": { "mode": "tiered" } }
/// ```
///
/// Returns three standard tiers: free, standard, and premium.
pub fn default_tiers() -> Vec<ModelTierConfig> {
    vec![
        ModelTierConfig {
            name: "free".into(),
            models: vec![
                "openrouter/meta-llama/llama-3.1-8b-instruct:free".into(),
                "groq/llama-3.1-8b".into(),
            ],
            complexity_range: [0.0, 0.3],
            cost_per_1k_tokens: 0.0,
            max_context_tokens: 8192,
        },
        ModelTierConfig {
            name: "standard".into(),
            models: vec![
                "anthropic/claude-haiku-3.5".into(),
                "openai/gpt-4o-mini".into(),
                "groq/llama-3.3-70b".into(),
            ],
            complexity_range: [0.0, 0.7],
            cost_per_1k_tokens: 0.001,
            max_context_tokens: 16384,
        },
        ModelTierConfig {
            name: "premium".into(),
            models: vec![
                "anthropic/claude-sonnet-4-20250514".into(),
                "openai/gpt-4o".into(),
            ],
            complexity_range: [0.3, 1.0],
            cost_per_1k_tokens: 0.01,
            max_context_tokens: 200_000,
        },
    ]
}

// ── Tests ───────────────────────────────────────────────────────────────

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

    use clawft_types::routing::{EscalationConfig, ModelTierConfig, RateLimitConfig};

    /// Build a minimal valid tiered config for testing.
    fn valid_tiered_config() -> RoutingConfig {
        RoutingConfig {
            mode: "tiered".into(),
            tiers: vec![
                ModelTierConfig {
                    name: "free".into(),
                    models: vec!["groq/llama-3.1-8b".into()],
                    complexity_range: [0.0, 0.5],
                    cost_per_1k_tokens: 0.0,
                    max_context_tokens: 8192,
                },
                ModelTierConfig {
                    name: "standard".into(),
                    models: vec!["anthropic/claude-haiku-3.5".into()],
                    complexity_range: [0.3, 1.0],
                    cost_per_1k_tokens: 0.001,
                    max_context_tokens: 16384,
                },
            ],
            ..RoutingConfig::default()
        }
    }

    /// Build a minimal valid static config for testing.
    fn valid_static_config() -> RoutingConfig {
        RoutingConfig::default()
    }

    /// Count errors at a given severity level.
    fn count_severity(errors: &[ValidationError], severity: ValidationSeverity) -> usize {
        errors.iter().filter(|e| e.severity == severity).count()
    }

    /// Check if any error matches a field prefix and severity.
    fn has_error(
        errors: &[ValidationError],
        field_prefix: &str,
        severity: ValidationSeverity,
    ) -> bool {
        errors
            .iter()
            .any(|e| e.field.starts_with(field_prefix) && e.severity == severity)
    }

    // ── Test 1: valid static config passes ──────────────────────────

    #[test]
    fn valid_static_config_passes() {
        let config = valid_static_config();
        let errors = validate_routing_config(&config);
        assert!(errors.is_empty(), "static config should produce no errors");
    }

    // ── Test 2: valid tiered config passes ──────────────────────────

    #[test]
    fn valid_tiered_config_passes() {
        let config = valid_tiered_config();
        let errors = validate_routing_config(&config);
        let fatal = count_severity(&errors, ValidationSeverity::Error);
        assert_eq!(
            fatal, 0,
            "valid tiered config should have no fatal errors, got: {:?}",
            errors
        );
    }

    // ── Test 3: invalid mode rejected ───────────────────────────────

    #[test]
    fn invalid_mode_rejected() {
        let mut config = valid_tiered_config();
        config.mode = "hybrid".into();
        let errors = validate_routing_config(&config);
        assert!(
            has_error(&errors, "routing.mode", ValidationSeverity::Error),
            "unknown mode should produce an error"
        );
    }

    // ── Test 4: tiered with no tiers gives error ────────────────────

    #[test]
    fn tiered_with_no_tiers_gives_error() {
        let config = RoutingConfig {
            mode: "tiered".into(),
            tiers: vec![],
            ..RoutingConfig::default()
        };
        let errors = validate_routing_config(&config);
        assert!(
            has_error(&errors, "routing.tiers", ValidationSeverity::Error),
            "tiered mode with no tiers should be an error"
        );
    }

    // ── Test 5: duplicate tier names detected ───────────────────────

    #[test]
    fn duplicate_tier_names_detected() {
        let mut config = valid_tiered_config();
        config.tiers[1].name = "free".into(); // duplicate
        let errors = validate_routing_config(&config);
        assert!(
            has_error(&errors, "routing.tiers[1].name", ValidationSeverity::Error),
            "duplicate tier names should produce an error"
        );
    }

    // ── Test 6: empty tier models detected ──────────────────────────

    #[test]
    fn empty_tier_models_warning() {
        let mut config = valid_tiered_config();
        config.tiers[0].models.clear();
        let errors = validate_routing_config(&config);
        assert!(
            has_error(
                &errors,
                "routing.tiers[0].models",
                ValidationSeverity::Warning
            ),
            "empty models should produce a warning"
        );
    }

    // ── Test 7: invalid complexity range (min > max) ────────────────

    #[test]
    fn complexity_range_inverted() {
        let mut config = valid_tiered_config();
        config.tiers[0].complexity_range = [0.8, 0.3];
        let errors = validate_routing_config(&config);
        assert!(
            has_error(
                &errors,
                "routing.tiers[0].complexity_range",
                ValidationSeverity::Error
            ),
            "inverted complexity range should produce an error"
        );
    }

    // ── Test 8: complexity out of [0, 1] range ──────────────────────

    #[test]
    fn complexity_out_of_range() {
        let mut config = valid_tiered_config();
        config.tiers[0].complexity_range = [-0.1, 1.5];
        let errors = validate_routing_config(&config);
        assert!(
            has_error(
                &errors,
                "routing.tiers[0].complexity_range[0]",
                ValidationSeverity::Error
            ),
            "negative min should produce an error"
        );
        assert!(
            has_error(
                &errors,
                "routing.tiers[0].complexity_range[1]",
                ValidationSeverity::Error
            ),
            "max > 1.0 should produce an error"
        );
    }

    // ── Test 9: negative cost rejected ──────────────────────────────

    #[test]
    fn negative_cost_rejected() {
        let mut config = valid_tiered_config();
        config.tiers[0].cost_per_1k_tokens = -0.001;
        let errors = validate_routing_config(&config);
        assert!(
            has_error(
                &errors,
                "routing.tiers[0].cost_per_1k_tokens",
                ValidationSeverity::Error
            ),
            "negative cost should produce an error"
        );
    }

    // ── Test 10: zero max_context_tokens rejected ───────────────────

    #[test]
    fn zero_max_context_tokens_rejected() {
        let mut config = valid_tiered_config();
        config.tiers[0].max_context_tokens = 0;
        let errors = validate_routing_config(&config);
        assert!(
            has_error(
                &errors,
                "routing.tiers[0].max_context_tokens",
                ValidationSeverity::Error
            ),
            "zero max_context_tokens should produce an error"
        );
    }

    // ── Test 11: invalid level warning ──────────────────────────────

    #[test]
    fn invalid_permission_level_warning() {
        let mut config = valid_tiered_config();
        config.permissions.zero_trust.level = Some(5);
        let errors = validate_routing_config(&config);
        assert!(
            has_error(
                &errors,
                "routing.permissions.zero_trust.level",
                ValidationSeverity::Warning
            ),
            "invalid level should produce a warning"
        );
    }

    // ── Test 12: invalid escalation threshold ───────────────────────

    #[test]
    fn invalid_escalation_threshold() {
        let mut config = valid_tiered_config();
        config.permissions.user.escalation_threshold = Some(1.5);
        let errors = validate_routing_config(&config);
        assert!(
            has_error(
                &errors,
                "routing.permissions.user.escalation_threshold",
                ValidationSeverity::Error
            ),
            "escalation_threshold > 1.0 should produce an error"
        );
    }

    // ── Test 13: negative budget rejected ───────────────────────────

    #[test]
    fn negative_budget_rejected() {
        let mut config = valid_tiered_config();
        config.permissions.user.cost_budget_daily_usd = Some(-1.0);
        let errors = validate_routing_config(&config);
        assert!(
            has_error(
                &errors,
                "routing.permissions.user.cost_budget_daily_usd",
                ValidationSeverity::Error
            ),
            "negative daily budget should produce an error"
        );
    }

    // ── Test 14: max tier referencing non-existent tier warns ────────

    #[test]
    fn max_tier_nonexistent_warns() {
        let mut config = valid_tiered_config();
        config.permissions.user.max_tier = Some("mythical".into());
        let errors = validate_routing_config(&config);
        assert!(
            has_error(
                &errors,
                "routing.permissions.user.max_tier",
                ValidationSeverity::Warning
            ),
            "undefined max_tier should produce a warning"
        );
    }

    // ── Test 15: glob-like tool pattern warning ─────────────────────

    #[test]
    fn glob_like_tool_pattern_warning() {
        let mut config = valid_tiered_config();
        config.permissions.user.tool_access = Some(vec!["file_*".into()]);
        let errors = validate_routing_config(&config);
        assert!(
            has_error(
                &errors,
                "routing.permissions.user.tool_access",
                ValidationSeverity::Warning
            ),
            "glob-like tool_access should produce a warning"
        );
    }

    // ── Test 16: exact wildcard "*" does not warn ───────────────────

    #[test]
    fn exact_wildcard_no_warning() {
        let mut config = valid_tiered_config();
        config.permissions.admin.tool_access = Some(vec!["*".into()]);
        let errors = validate_routing_config(&config);
        let glob_warnings = errors.iter().any(|e| {
            e.field.contains("tool_access")
                && e.severity == ValidationSeverity::Warning
                && e.message.contains("glob")
        });
        assert!(
            !glob_warnings,
            "exact '*' should not produce a glob pattern warning"
        );
    }

    // ── Test 17: fallback model format validation ───────────────────

    #[test]
    fn fallback_model_format_warning() {
        let mut config = valid_tiered_config();
        config.fallback_model = Some("gpt-4o".into());
        let errors = validate_routing_config(&config);
        assert!(
            has_error(
                &errors,
                "routing.fallback_model",
                ValidationSeverity::Warning
            ),
            "fallback model without '/' should produce a warning"
        );
    }

    // ── Test 18: workspace ceiling -- level escalation blocked ──────

    #[test]
    fn workspace_ceiling_level_escalation_blocked() {
        let global = valid_tiered_config();
        let mut workspace = RoutingConfig::default();
        workspace.permissions.user.level = Some(2); // admin
        let errors = validate_workspace_ceiling(&global, &workspace);
        assert!(
            has_error(
                &errors,
                "routing.permissions.user.level",
                ValidationSeverity::Error
            ),
            "workspace level exceeding max_grantable should produce an error"
        );
    }

    // ── Test 19: workspace ceiling -- rate limit increase blocked ───

    #[test]
    fn workspace_ceiling_rate_limit_increase_blocked() {
        let mut global = valid_tiered_config();
        global.permissions.user.rate_limit = Some(60);
        let mut workspace = RoutingConfig::default();
        workspace.permissions.user.rate_limit = Some(120);
        let errors = validate_workspace_ceiling(&global, &workspace);
        assert!(
            has_error(
                &errors,
                "routing.permissions.user.rate_limit",
                ValidationSeverity::Error
            ),
            "workspace rate_limit exceeding global should produce an error"
        );
    }

    // ── Test 20: workspace ceiling -- budget increase blocked ───────

    #[test]
    fn workspace_ceiling_budget_increase_blocked() {
        let mut global = valid_tiered_config();
        global.permissions.user.cost_budget_daily_usd = Some(5.0);
        let mut workspace = RoutingConfig::default();
        workspace.permissions.user.cost_budget_daily_usd = Some(10.0);
        let errors = validate_workspace_ceiling(&global, &workspace);
        assert!(
            has_error(
                &errors,
                "routing.permissions.user.cost_budget_daily_usd",
                ValidationSeverity::Error
            ),
            "workspace daily budget exceeding global should produce an error"
        );
    }

    // ── Test 21: workspace ceiling -- tool access expansion blocked ─

    #[test]
    fn workspace_ceiling_tool_access_expansion_blocked() {
        let mut global = valid_tiered_config();
        global.permissions.user.tool_access = Some(vec!["read_file".into(), "list_dir".into()]);
        let mut workspace = RoutingConfig::default();
        workspace.permissions.user.tool_access = Some(vec!["exec_shell".into()]);
        let errors = validate_workspace_ceiling(&global, &workspace);
        assert!(
            has_error(
                &errors,
                "routing.permissions.user.tool_access",
                ValidationSeverity::Error
            ),
            "workspace adding tool not in global should produce an error"
        );
    }

    // ── Test 22: workspace within bounds passes ─────────────────────

    #[test]
    fn workspace_within_bounds_passes() {
        let mut global = valid_tiered_config();
        global.permissions.user.level = Some(1);
        global.permissions.user.rate_limit = Some(60);
        global.permissions.user.cost_budget_daily_usd = Some(5.0);

        let mut workspace = RoutingConfig::default();
        workspace.permissions.user.level = Some(1);
        workspace.permissions.user.rate_limit = Some(30);
        workspace.permissions.user.cost_budget_daily_usd = Some(3.0);

        let errors = validate_workspace_ceiling(&global, &workspace);
        let fatal = count_severity(&errors, ValidationSeverity::Error);
        assert_eq!(
            fatal, 0,
            "workspace within bounds should have no errors, got: {:?}",
            errors
        );
    }

    // ── Test 23: default tiers generation ───────────────────────────

    #[test]
    fn default_tiers_generation() {
        let tiers = default_tiers();
        assert_eq!(tiers.len(), 3, "should produce 3 default tiers");
        assert_eq!(tiers[0].name, "free");
        assert_eq!(tiers[1].name, "standard");
        assert_eq!(tiers[2].name, "premium");

        // Each tier should have at least one model.
        for tier in &tiers {
            assert!(
                !tier.models.is_empty(),
                "tier '{}' should have models",
                tier.name
            );
        }

        // Free tier should have zero cost.
        assert_eq!(tiers[0].cost_per_1k_tokens, 0.0);

        // Complexity ranges should be within [0.0, 1.0].
        for tier in &tiers {
            assert!(tier.complexity_range[0] >= 0.0);
            assert!(tier.complexity_range[1] <= 1.0);
            assert!(tier.complexity_range[0] <= tier.complexity_range[1]);
        }
    }

    // ── Test 24: default tiers pass validation ──────────────────────

    #[test]
    fn default_tiers_pass_validation() {
        let config = RoutingConfig {
            mode: "tiered".into(),
            tiers: default_tiers(),
            ..RoutingConfig::default()
        };
        let errors = validate_routing_config(&config);
        let fatal = count_severity(&errors, ValidationSeverity::Error);
        assert_eq!(
            fatal, 0,
            "default tiers should pass validation, got: {:?}",
            errors
        );
    }

    // ── Test 25: escalation threshold out of range ──────────────────

    #[test]
    fn escalation_global_threshold_out_of_range() {
        let mut config = valid_tiered_config();
        config.escalation = EscalationConfig {
            enabled: true,
            threshold: 1.5,
            max_escalation_tiers: 1,
        };
        let errors = validate_routing_config(&config);
        assert!(
            has_error(
                &errors,
                "routing.escalation.threshold",
                ValidationSeverity::Error
            ),
            "escalation threshold > 1.0 should produce an error"
        );
    }

    // ── Test 26: zero window_seconds rejected ───────────────────────

    #[test]
    fn zero_window_seconds_rejected() {
        let mut config = valid_tiered_config();
        config.rate_limiting.window_seconds = 0;
        let errors = validate_routing_config(&config);
        assert!(
            has_error(
                &errors,
                "routing.rate_limiting.window_seconds",
                ValidationSeverity::Error
            ),
            "zero window_seconds should produce an error"
        );
    }

    // ── Test 27: unknown rate limit strategy rejected ────────────────

    #[test]
    fn unknown_rate_limit_strategy_rejected() {
        let mut config = valid_tiered_config();
        config.rate_limiting.strategy = "token_bucket".into();
        let errors = validate_routing_config(&config);
        assert!(
            has_error(
                &errors,
                "routing.rate_limiting.strategy",
                ValidationSeverity::Error
            ),
            "unknown rate limit strategy should produce an error"
        );
    }

    // ── Test 28: reset_hour_utc out of range ────────────────────────

    #[test]
    fn reset_hour_out_of_range() {
        let mut config = valid_tiered_config();
        config.cost_budgets.reset_hour_utc = 25;
        let errors = validate_routing_config(&config);
        assert!(
            has_error(
                &errors,
                "routing.cost_budgets.reset_hour_utc",
                ValidationSeverity::Error
            ),
            "reset_hour_utc > 23 should produce an error"
        );
    }

    // ── Test 29: negative global daily limit ────────────────────────

    #[test]
    fn negative_global_daily_limit() {
        let mut config = valid_tiered_config();
        config.cost_budgets.global_daily_limit_usd = -10.0;
        let errors = validate_routing_config(&config);
        assert!(
            has_error(
                &errors,
                "routing.cost_budgets.global_daily_limit_usd",
                ValidationSeverity::Error
            ),
            "negative global daily limit should produce an error"
        );
    }

    // ── Test 30: negative global monthly limit ──────────────────────

    #[test]
    fn negative_global_monthly_limit() {
        let mut config = valid_tiered_config();
        config.cost_budgets.global_monthly_limit_usd = -100.0;
        let errors = validate_routing_config(&config);
        assert!(
            has_error(
                &errors,
                "routing.cost_budgets.global_monthly_limit_usd",
                ValidationSeverity::Error
            ),
            "negative global monthly limit should produce an error"
        );
    }

    // ── Test 31: multiple errors collected ──────────────────────────

    #[test]
    fn multiple_errors_collected() {
        let config = RoutingConfig {
            mode: "tiered".into(),
            tiers: vec![
                ModelTierConfig {
                    name: "bad".into(),
                    models: vec![],
                    complexity_range: [0.8, 0.3], // inverted
                    cost_per_1k_tokens: -1.0,     // negative
                    max_context_tokens: 0,        // zero
                },
                ModelTierConfig {
                    name: "bad".into(), // duplicate
                    models: vec![],
                    complexity_range: [0.0, 1.0],
                    cost_per_1k_tokens: 0.0,
                    max_context_tokens: 8192,
                },
            ],
            rate_limiting: RateLimitConfig {
                window_seconds: 0, // invalid
                strategy: "sliding_window".into(),
                ..RateLimitConfig::default()
            },
            ..RoutingConfig::default()
        };
        let errors = validate_routing_config(&config);
        let fatal_count = count_severity(&errors, ValidationSeverity::Error);
        assert!(
            fatal_count >= 4,
            "should collect at least 4 errors, got {}: {:?}",
            fatal_count,
            errors
        );
    }

    // ── Test 32: static mode skips validation of bad data ───────────

    #[test]
    fn static_mode_skips_validation() {
        let config = RoutingConfig {
            mode: "static".into(),
            tiers: vec![ModelTierConfig {
                name: "broken".into(),
                models: vec![],
                complexity_range: [0.8, 0.3], // would be invalid
                cost_per_1k_tokens: -1.0,
                max_context_tokens: 0,
            }],
            ..RoutingConfig::default()
        };
        let errors = validate_routing_config(&config);
        assert!(
            errors.is_empty(),
            "static mode should skip all validation, got: {:?}",
            errors
        );
    }

    // ── Test 33: overlapping complexity ranges warning ───────────────

    #[test]
    fn overlapping_complexity_ranges_warning() {
        let config = RoutingConfig {
            mode: "tiered".into(),
            tiers: vec![
                ModelTierConfig {
                    name: "a".into(),
                    models: vec!["provider/model-a".into()],
                    complexity_range: [0.0, 0.7],
                    cost_per_1k_tokens: 0.0,
                    max_context_tokens: 8192,
                },
                ModelTierConfig {
                    name: "b".into(),
                    models: vec!["provider/model-b".into()],
                    complexity_range: [0.3, 1.0],
                    cost_per_1k_tokens: 0.01,
                    max_context_tokens: 16384,
                },
            ],
            ..RoutingConfig::default()
        };
        let errors = validate_routing_config(&config);
        let overlap_warnings = errors.iter().any(|e| {
            e.severity == ValidationSeverity::Warning && e.message.contains("overlapping")
        });
        assert!(
            overlap_warnings,
            "overlapping ranges should produce a warning"
        );
    }

    // ── Test 34: model format warning ───────────────────────────────

    #[test]
    fn model_format_warning() {
        let mut config = valid_tiered_config();
        config.tiers[0].models = vec!["gpt-4o".into()]; // no slash
        let errors = validate_routing_config(&config);
        assert!(
            has_error(
                &errors,
                "routing.tiers[0].models[0]",
                ValidationSeverity::Warning
            ),
            "model without '/' should produce a warning"
        );
    }

    // ── Test 35: per-user override validation ───────────────────────

    #[test]
    fn per_user_override_validated() {
        let mut config = valid_tiered_config();
        config.permissions.users.insert(
            "alice".into(),
            PermissionLevelConfig {
                cost_budget_monthly_usd: Some(-5.0),
                ..PermissionLevelConfig::default()
            },
        );
        let errors = validate_routing_config(&config);
        assert!(
            has_error(
                &errors,
                "routing.permissions.users.alice.cost_budget_monthly_usd",
                ValidationSeverity::Error
            ),
            "per-user negative budget should produce an error"
        );
    }

    // ── Test 36: per-channel override validation ────────────────────

    #[test]
    fn per_channel_override_validated() {
        let mut config = valid_tiered_config();
        config.permissions.channels.insert(
            "telegram".into(),
            PermissionLevelConfig {
                escalation_threshold: Some(-0.5),
                ..PermissionLevelConfig::default()
            },
        );
        let errors = validate_routing_config(&config);
        assert!(
            has_error(
                &errors,
                "routing.permissions.channels.telegram.escalation_threshold",
                ValidationSeverity::Error
            ),
            "per-channel invalid threshold should produce an error"
        );
    }

    // ── Test 37: escalation max tiers exceeds defined count warns ───

    #[test]
    fn escalation_max_tiers_exceeds_defined_count_warns() {
        let mut config = valid_tiered_config();
        config.escalation = EscalationConfig {
            enabled: true,
            threshold: 0.6,
            max_escalation_tiers: 10, // only 2 tiers defined
        };
        let errors = validate_routing_config(&config);
        assert!(
            has_error(
                &errors,
                "routing.escalation.max_escalation_tiers",
                ValidationSeverity::Warning
            ),
            "max_escalation_tiers > tier count should warn"
        );
    }

    // ── Test 38: workspace ceiling -- monthly budget increase ───────

    #[test]
    fn workspace_ceiling_monthly_budget_increase_blocked() {
        let mut global = valid_tiered_config();
        global.permissions.user.cost_budget_monthly_usd = Some(50.0);
        let mut workspace = RoutingConfig::default();
        workspace.permissions.user.cost_budget_monthly_usd = Some(100.0);
        let errors = validate_workspace_ceiling(&global, &workspace);
        assert!(
            has_error(
                &errors,
                "routing.permissions.user.cost_budget_monthly_usd",
                ValidationSeverity::Error
            ),
            "workspace monthly budget exceeding global should produce an error"
        );
    }

    // ── Test 39: workspace ceiling -- escalation enable blocked ──────

    #[test]
    fn workspace_ceiling_escalation_enable_blocked() {
        let mut global = valid_tiered_config();
        global.permissions.user.escalation_allowed = Some(false);
        let mut workspace = RoutingConfig::default();
        workspace.permissions.user.escalation_allowed = Some(true);
        let errors = validate_workspace_ceiling(&global, &workspace);
        assert!(
            has_error(
                &errors,
                "routing.permissions.user.escalation_allowed",
                ValidationSeverity::Error
            ),
            "workspace enabling escalation when global disables should error"
        );
    }

    // ── Test 40: workspace wildcard tool access blocked ──────────────

    #[test]
    fn workspace_wildcard_tool_access_blocked() {
        let mut global = valid_tiered_config();
        global.permissions.user.tool_access = Some(vec!["read_file".into()]);
        let mut workspace = RoutingConfig::default();
        workspace.permissions.user.tool_access = Some(vec!["*".into()]);
        let errors = validate_workspace_ceiling(&global, &workspace);
        assert!(
            has_error(
                &errors,
                "routing.permissions.user.tool_access",
                ValidationSeverity::Error
            ),
            "workspace wildcard when global does not should error"
        );
    }

    // ── Test 41: workspace per-user override ceiling ────────────────

    #[test]
    fn workspace_per_user_override_ceiling() {
        let global = valid_tiered_config();
        let mut workspace = RoutingConfig::default();
        workspace.permissions.users.insert(
            "attacker".into(),
            PermissionLevelConfig {
                level: Some(2), // admin -- exceeds max_grantable=1
                ..PermissionLevelConfig::default()
            },
        );
        let errors = validate_workspace_ceiling(&global, &workspace);
        assert!(
            has_error(
                &errors,
                "routing.permissions.users.attacker.level",
                ValidationSeverity::Error
            ),
            "workspace per-user admin level should be blocked"
        );
    }

    // ── Test 42: Display impl ───────────────────────────────────────

    #[test]
    fn display_impl_formats_correctly() {
        let err = ValidationError {
            field: "routing.tiers[0].name".into(),
            message: "duplicate tier name 'free'".into(),
            severity: ValidationSeverity::Error,
        };
        let displayed = format!("{}", err);
        assert!(displayed.contains("[ERROR]"));
        assert!(displayed.contains("routing.tiers[0].name"));
        assert!(displayed.contains("duplicate tier name 'free'"));

        let warn = ValidationError {
            field: "routing.fallback_model".into(),
            message: "missing provider prefix".into(),
            severity: ValidationSeverity::Warning,
        };
        let displayed = format!("{}", warn);
        assert!(displayed.contains("[WARN]"));
    }

    // ── Test 43: escalation enabled with zero max tiers ─────────────

    #[test]
    fn escalation_enabled_zero_max_tiers() {
        let mut config = valid_tiered_config();
        config.escalation = EscalationConfig {
            enabled: true,
            threshold: 0.6,
            max_escalation_tiers: 0,
        };
        let errors = validate_routing_config(&config);
        assert!(
            has_error(
                &errors,
                "routing.escalation.max_escalation_tiers",
                ValidationSeverity::Error
            ),
            "max_escalation_tiers=0 with enabled escalation should error"
        );
    }

    // ── Test 44: valid fallback model format ────────────────────────

    #[test]
    fn valid_fallback_model_no_warning() {
        let mut config = valid_tiered_config();
        config.fallback_model = Some("groq/llama-3.1-8b".into());
        let errors = validate_routing_config(&config);
        let fb_warnings = errors.iter().any(|e| {
            e.field.contains("fallback_model") && e.severity == ValidationSeverity::Warning
        });
        assert!(!fb_warnings, "valid fallback model should not warn");
    }

    // ── Test 45: workspace within global wildcard tool access ok ─────

    #[test]
    fn workspace_tools_within_global_wildcard_ok() {
        let mut global = valid_tiered_config();
        global.permissions.user.tool_access = Some(vec!["*".into()]);
        let mut workspace = RoutingConfig::default();
        workspace.permissions.user.tool_access =
            Some(vec!["read_file".into(), "exec_shell".into()]);
        let errors = validate_workspace_ceiling(&global, &workspace);
        let tool_errors = errors
            .iter()
            .any(|e| e.field.contains("tool_access") && e.severity == ValidationSeverity::Error);
        assert!(
            !tool_errors,
            "workspace tools under global wildcard should pass"
        );
    }
}