zeph-config 0.22.1

Pure-data configuration types for Zeph
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
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
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
// SPDX-License-Identifier: MIT OR Apache-2.0

use std::path::Path;

use crate::error::ConfigError;
use crate::root::Config;

impl Config {
    /// Load configuration from a TOML file with env var overrides.
    ///
    /// Falls back to sensible defaults when the file does not exist.
    ///
    /// # Errors
    ///
    /// Returns an error if the file exists but cannot be read or parsed.
    pub fn load(path: &Path) -> Result<Self, ConfigError> {
        let mut config = if path.exists() {
            let content = std::fs::read_to_string(path)?;
            toml::from_str::<Self>(&content)?
        } else {
            Self::default()
        };

        config.apply_env_overrides();
        config.normalize_legacy_runtime_defaults();
        Ok(config)
    }

    /// Serialize the default configuration to a TOML string.
    ///
    /// Produces a pretty-printed TOML representation of [`Config::default()`].
    /// Useful for bootstrapping a new config file or documenting available options.
    ///
    /// The `secrets` field is always excluded from the output because it is
    /// populated at runtime only and must never be written to disk.
    ///
    /// # Errors
    ///
    /// Returns an error if serialization fails (unlikely — the default value is
    /// always structurally valid).
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use zeph_config::Config;
    ///
    /// let toml = Config::dump_defaults().expect("serialization failed");
    /// assert!(toml.contains("[agent]"));
    /// assert!(toml.contains("[memory]"));
    /// ```
    pub fn dump_defaults() -> Result<String, crate::error::ConfigError> {
        let defaults = Self::default();
        toml::to_string_pretty(&defaults).map_err(|e| {
            crate::error::ConfigError::Validation(format!("failed to serialize defaults: {e}"))
        })
    }

    /// Validate configuration values are within sane bounds.
    ///
    /// # Errors
    ///
    /// Returns an error if any value is out of range.
    #[must_use = "validation result must be checked"]
    pub fn validate(&self) -> Result<(), ConfigError> {
        self.validate_scalar_bounds()?;
        self.validate_memory_compression()?;
        self.validate_memory_probe_and_graph()?;
        self.validate_mcp_servers()?;
        self.experiments
            .validate()
            .map_err(ConfigError::Validation)?;
        if self.orchestration.plan_cache.enabled {
            self.orchestration
                .plan_cache
                .validate()
                .map_err(ConfigError::Validation)?;
        }
        self.validate_orchestration()?;
        self.validate_focus_and_sidequest()?;
        self.validate_llm_and_skills()?;
        self.validate_provider_names()?;
        self.validate_mcp_misc()?;
        self.validate_scheduler()?;
        self.acp
            .validate_auth_clients()
            .map_err(ConfigError::Validation)?;
        // Provider pool: empty pool, duplicate names, and multiple `default = true`
        // entries. Load-bearing guarantee relied on (verbatim) by
        // `Agent::resolve_pool_entry_provider` (tier_loop.rs) and `arise.rs` — both assume
        // a genuinely empty pool never occurs for a fully constructed production `Agent`.
        crate::providers::validate_pool(&self.llm.providers)?;
        self.llm.validate_stt()?;
        self.security
            .trajectory
            .validate()
            .map_err(ConfigError::Validation)?;
        self.gateway.validate().map_err(ConfigError::Validation)?;
        self.tools
            .utility
            .validate()
            .map_err(ConfigError::Validation)?;
        if let Some(fidelity) = &self.memory.fidelity {
            fidelity.validate().map_err(ConfigError::Validation)?;
        }
        self.memory
            .compression
            .acon
            .validate()
            .map_err(ConfigError::Validation)?;
        if self.memory.shadow_memory.enabled {
            self.memory
                .shadow_memory
                .validate()
                .map_err(ConfigError::Validation)?;
        }
        Ok(())
    }

    /// Validate scalar bounds for memory, agent, a2a, and gateway fields.
    fn validate_scalar_bounds(&self) -> Result<(), ConfigError> {
        if self.memory.history_limit > 10_000 {
            return Err(ConfigError::Validation(format!(
                "history_limit must be <= 10000, got {}",
                self.memory.history_limit
            )));
        }
        if self.memory.context_budget_tokens > 1_000_000 {
            return Err(ConfigError::Validation(format!(
                "context_budget_tokens must be <= 1000000, got {}",
                self.memory.context_budget_tokens
            )));
        }
        if self.agent.max_tool_iterations > 100 {
            return Err(ConfigError::Validation(format!(
                "max_tool_iterations must be <= 100, got {}",
                self.agent.max_tool_iterations
            )));
        }
        if self.a2a.rate_limit == 0 {
            return Err(ConfigError::Validation("a2a.rate_limit must be > 0".into()));
        }
        self.validate_a2a_client_trust()?;
        if self.gateway.rate_limit == 0 {
            return Err(ConfigError::Validation(
                "gateway.rate_limit must be > 0".into(),
            ));
        }
        if self.gateway.max_body_size > 10_485_760 {
            return Err(ConfigError::Validation(format!(
                "gateway.max_body_size must be <= 10485760 (10 MiB), got {}",
                self.gateway.max_body_size
            )));
        }
        if self.memory.token_safety_margin <= 0.0 {
            return Err(ConfigError::Validation(format!(
                "token_safety_margin must be > 0.0, got {}",
                self.memory.token_safety_margin
            )));
        }
        if self.memory.tool_call_cutoff == 0 {
            return Err(ConfigError::Validation(
                "tool_call_cutoff must be >= 1".into(),
            ));
        }
        if self.worktree.max_worktrees == Some(0) {
            return Err(ConfigError::Validation(
                "worktree.max_worktrees must be > 0 or unset (unlimited); 0 would block all \
                 worktree creation"
                    .into(),
            ));
        }
        if self.worktree.disk_quota_mb == Some(0) {
            return Err(ConfigError::Validation(
                "worktree.disk_quota_mb must be > 0 or unset (no accounting); 0 would leave \
                 every non-empty worktree permanently over quota"
                    .into(),
            ));
        }
        if self.worktree.disk_quota_mb.is_some()
            && self.worktree.auto_reconcile_secs == 0
            && !self.worktree.reconcile_on_startup
        {
            return Err(ConfigError::Validation(
                "worktree.disk_quota_mb is set but neither reconcile_on_startup nor \
                 auto_reconcile_secs is enabled — the quota will only be checked when you run \
                 `zeph worktree list` manually, never automatically"
                    .into(),
            ));
        }
        if (1..60).contains(&self.worktree.auto_reconcile_secs) {
            return Err(ConfigError::Validation(format!(
                "worktree.auto_reconcile_secs must be 0 (disabled) or >= 60, got {}; a short \
                 interval runs a full filesystem walk in a tight loop",
                self.worktree.auto_reconcile_secs
            )));
        }
        Ok(())
    }

    /// Fail fast if `[a2a_client].card_trust_policy = "require"` is set without the
    /// `card-signing` feature compiled in anywhere in the binary (S3, #5928).
    ///
    /// Without this check, `require` would either silently degrade to no signature
    /// enforcement or brick all discovery, depending on how the unreachable code path is
    /// interpreted — both are worse than a loud config-load error. See
    /// `zeph_a2a::discovery::signature_severity` for the runtime-side half of this
    /// contract (treats `FeatureDisabled` the same as `Unverifiable`/`Invalid` under
    /// `require`, which only matters if this validation is ever bypassed).
    #[cfg_attr(
        feature = "card-signing",
        allow(clippy::unused_self, clippy::unnecessary_wraps)
    )]
    fn validate_a2a_client_trust(&self) -> Result<(), ConfigError> {
        #[cfg(not(feature = "card-signing"))]
        if self.a2a_client.card_trust_policy == crate::channels::CardTrustPolicy::Require {
            return Err(ConfigError::Validation(
                "a2a_client.card_trust_policy = require requires the card-signing feature \
                 to be enabled at build time (see the `a2a` feature in the root Cargo.toml)"
                    .into(),
            ));
        }
        Ok(())
    }

    /// Validate memory compression strategy bounds and compaction thresholds.
    fn validate_memory_compression(&self) -> Result<(), ConfigError> {
        if let crate::memory::CompressionStrategy::Proactive {
            threshold_tokens,
            max_summary_tokens,
        } = &self.memory.compression.strategy
        {
            if *threshold_tokens < 1_000 {
                return Err(ConfigError::Validation(format!(
                    "compression.threshold_tokens must be >= 1000, got {threshold_tokens}"
                )));
            }
            if *max_summary_tokens < 128 {
                return Err(ConfigError::Validation(format!(
                    "compression.max_summary_tokens must be >= 128, got {max_summary_tokens}"
                )));
            }
        }
        if !self.memory.soft_compaction_threshold.is_finite()
            || self.memory.soft_compaction_threshold <= 0.0
            || self.memory.soft_compaction_threshold >= 1.0
        {
            return Err(ConfigError::Validation(format!(
                "soft_compaction_threshold must be in (0.0, 1.0) exclusive, got {}",
                self.memory.soft_compaction_threshold
            )));
        }
        if !self.memory.hard_compaction_threshold.is_finite()
            || self.memory.hard_compaction_threshold <= 0.0
            || self.memory.hard_compaction_threshold >= 1.0
        {
            return Err(ConfigError::Validation(format!(
                "hard_compaction_threshold must be in (0.0, 1.0) exclusive, got {}",
                self.memory.hard_compaction_threshold
            )));
        }
        if self.memory.soft_compaction_threshold >= self.memory.hard_compaction_threshold {
            return Err(ConfigError::Validation(format!(
                "soft_compaction_threshold ({}) must be less than hard_compaction_threshold ({})",
                self.memory.soft_compaction_threshold, self.memory.hard_compaction_threshold,
            )));
        }
        Ok(())
    }

    /// Validate memory probe thresholds and graph temporal decay rate.
    fn validate_memory_probe_and_graph(&self) -> Result<(), ConfigError> {
        if self.memory.graph.temporal_decay_rate < 0.0
            || self.memory.graph.temporal_decay_rate > 10.0
        {
            return Err(ConfigError::Validation(format!(
                "memory.graph.temporal_decay_rate must be in [0.0, 10.0], got {}",
                self.memory.graph.temporal_decay_rate
            )));
        }
        if self.memory.compression.probe.enabled {
            let probe = &self.memory.compression.probe;
            if !probe.threshold.is_finite() || probe.threshold <= 0.0 || probe.threshold > 1.0 {
                return Err(ConfigError::Validation(format!(
                    "memory.compression.probe.threshold must be in (0.0, 1.0], got {}",
                    probe.threshold
                )));
            }
            if !probe.hard_fail_threshold.is_finite()
                || probe.hard_fail_threshold < 0.0
                || probe.hard_fail_threshold >= 1.0
            {
                return Err(ConfigError::Validation(format!(
                    "memory.compression.probe.hard_fail_threshold must be in [0.0, 1.0), got {}",
                    probe.hard_fail_threshold
                )));
            }
            if probe.hard_fail_threshold >= probe.threshold {
                return Err(ConfigError::Validation(format!(
                    "memory.compression.probe.hard_fail_threshold ({}) must be less than \
                     memory.compression.probe.threshold ({})",
                    probe.hard_fail_threshold, probe.threshold
                )));
            }
            if probe.max_questions < 1 {
                return Err(ConfigError::Validation(
                    "memory.compression.probe.max_questions must be >= 1".into(),
                ));
            }
            if probe.timeout_secs < 1 {
                return Err(ConfigError::Validation(
                    "memory.compression.probe.timeout_secs must be >= 1".into(),
                ));
            }
        }
        Ok(())
    }

    /// Validate MCP server entries for header/oauth exclusivity and vault key uniqueness.
    fn validate_mcp_servers(&self) -> Result<(), ConfigError> {
        use std::collections::HashSet;
        let mut seen_oauth_vault_keys: HashSet<String> = HashSet::new();
        for s in &self.mcp.servers {
            // headers and oauth are mutually exclusive
            if !s.headers.is_empty() && s.oauth.as_ref().is_some_and(|o| o.enabled) {
                return Err(ConfigError::Validation(format!(
                    "MCP server '{}': cannot use both 'headers' and 'oauth' simultaneously",
                    s.id
                )));
            }
            // vault key collision detection
            if s.oauth.as_ref().is_some_and(|o| o.enabled) {
                let key = format!("ZEPH_MCP_OAUTH_{}", s.id.to_uppercase().replace('-', "_"));
                if !seen_oauth_vault_keys.insert(key.clone()) {
                    return Err(ConfigError::Validation(format!(
                        "MCP server '{}' has vault key collision ('{key}'): another server \
                         with the same normalized ID already uses this key",
                        s.id
                    )));
                }
            }
        }
        Ok(())
    }

    /// Validate orchestration thresholds and cascade settings.
    fn validate_orchestration(&self) -> Result<(), ConfigError> {
        if self.orchestration.max_parallel == 0 {
            return Err(ConfigError::Validation(
                "orchestration.max_parallel must be > 0".into(),
            ));
        }
        if self.orchestration.max_tasks == 0 {
            return Err(ConfigError::Validation(
                "orchestration.max_tasks must be > 0".into(),
            ));
        }
        let ct = self.orchestration.completeness_threshold;
        if !ct.is_finite() || !(0.0..=1.0).contains(&ct) {
            return Err(ConfigError::Validation(format!(
                "orchestration.completeness_threshold must be in [0.0, 1.0], got {ct}"
            )));
        }
        // Ensemble member-list shape is only meaningful once the ensemble is actually wired
        // into a verification decision (`enabled && verify`) — an unused/staged config with
        // an invalid `members` list must not block startup (spec 073 FR-014).
        let ensemble = &self.orchestration.ensemble;
        if ensemble.enabled && ensemble.verify {
            let n = ensemble.members.len();
            if n.is_multiple_of(2) || n < 3 {
                return Err(ConfigError::Validation(format!(
                    "orchestration.ensemble.members must be odd and >= 3, got {n}"
                )));
            }
            let unique: std::collections::HashSet<&str> =
                ensemble.members.iter().map(String::as_str).collect();
            if unique.len() != ensemble.members.len() {
                return Err(ConfigError::Validation(
                    "orchestration.ensemble.members contains a duplicate provider name".into(),
                ));
            }
            // Defense-in-depth (security P3): EnsembleTracker's EMA params are telemetry-only
            // in PR-1 and never gate a verification/dispatch decision, but an out-of-range value
            // would still produce a meaningless displayed score and could bias a future phase
            // that wires EMA into member selection.
            let alpha = ensemble.ema_alpha;
            if !alpha.is_finite() || !(0.0..=1.0).contains(&alpha) {
                return Err(ConfigError::Validation(format!(
                    "orchestration.ensemble.ema_alpha must be in [0.0, 1.0], got {alpha}"
                )));
            }
            let decay = ensemble.ema_decay;
            if !decay.is_finite() || !(0.0..=1.0).contains(&decay) {
                return Err(ConfigError::Validation(format!(
                    "orchestration.ensemble.ema_decay must be in [0.0, 1.0], got {decay}"
                )));
            }
        }
        // Cascade chain threshold must not be 1 — that would abort on every single failure.
        if self.orchestration.cascade_chain_threshold == 1 {
            return Err(ConfigError::Validation(
                "orchestration.cascade_chain_threshold=1 aborts on every failure; \
                 use 0 to disable linear-chain cascade abort instead"
                    .into(),
            ));
        }
        let cfrat = self.orchestration.cascade_failure_rate_abort_threshold;
        if !cfrat.is_finite() || !(0.0..=1.0).contains(&cfrat) {
            return Err(ConfigError::Validation(format!(
                "orchestration.cascade_failure_rate_abort_threshold must be in [0.0, 1.0], got {cfrat}"
            )));
        }
        if self.orchestration.lineage_ttl_secs == 0 {
            return Err(ConfigError::Validation(
                "orchestration.lineage_ttl_secs must be > 0; \
                 set cascade_chain_threshold=0 to disable lineage tracking instead"
                    .into(),
            ));
        }
        if self.orchestration.aggregator_timeout_secs == 0 {
            return Err(ConfigError::Validation(
                "orchestration.aggregator_timeout_secs must be > 0".into(),
            ));
        }
        if self.orchestration.planner_timeout_secs == 0 {
            return Err(ConfigError::Validation(
                "orchestration.planner_timeout_secs must be > 0".into(),
            ));
        }
        if self.orchestration.verifier_timeout_secs == 0 {
            return Err(ConfigError::Validation(
                "orchestration.verifier_timeout_secs must be > 0".into(),
            ));
        }
        Ok(())
    }

    /// Validate focus and sidequest interval and ratio constraints.
    fn validate_focus_and_sidequest(&self) -> Result<(), ConfigError> {
        if self.agent.focus.compression_interval == 0 {
            return Err(ConfigError::Validation(
                "agent.focus.compression_interval must be >= 1".into(),
            ));
        }
        if self.agent.focus.min_messages_per_focus == 0 {
            return Err(ConfigError::Validation(
                "agent.focus.min_messages_per_focus must be >= 1".into(),
            ));
        }
        if self.agent.focus.auto_consolidate_min_window == 0 {
            return Err(ConfigError::Validation(
                "agent.focus.auto_consolidate_min_window must be >= 1 \
                 (set focus.enabled = false to disable auto-consolidation)"
                    .into(),
            ));
        }
        if self.memory.sidequest.interval_turns == 0 {
            return Err(ConfigError::Validation(
                "memory.sidequest.interval_turns must be >= 1".into(),
            ));
        }
        if !self.memory.sidequest.max_eviction_ratio.is_finite()
            || self.memory.sidequest.max_eviction_ratio <= 0.0
            || self.memory.sidequest.max_eviction_ratio > 1.0
        {
            return Err(ConfigError::Validation(format!(
                "memory.sidequest.max_eviction_ratio must be in (0.0, 1.0], got {}",
                self.memory.sidequest.max_eviction_ratio
            )));
        }
        Ok(())
    }

    /// Validate LLM semantic cache threshold and skill evaluation weight sum.
    fn validate_llm_and_skills(&self) -> Result<(), ConfigError> {
        let sct = self.llm.semantic_cache_threshold;
        if !(sct.is_finite() && (0.0..=1.0).contains(&sct)) {
            return Err(ConfigError::Validation(format!(
                "llm.semantic_cache_threshold must be in [0.0, 1.0], got {sct} \
                 (override via ZEPH_LLM_SEMANTIC_CACHE_THRESHOLD env var)"
            )));
        }
        // MemCoT distill provider fast-tier soft-warn (#3574).
        if self.memory.memcot.enabled && !self.memory.memcot.distill_provider.is_empty() {
            self.llm.warn_non_fast_tier_provider(
                &self.memory.memcot.distill_provider,
                "memory.memcot.distill_provider",
                &self.memory.memcot.fast_tier_models,
            );
        }
        self.skills
            .learning
            .validate()
            .map_err(ConfigError::Validation)?;
        // Skill evaluation weight-sum validation (#3319).
        if self.skills.evaluation.enabled {
            let weight_sum = self.skills.evaluation.weight_correctness
                + self.skills.evaluation.weight_reusability
                + self.skills.evaluation.weight_specificity;
            if (weight_sum - 1.0_f32).abs() > 1e-3 {
                return Err(ConfigError::Validation(format!(
                    "skills.evaluation weights must sum to 1.0 (got {weight_sum:.4})"
                )));
            }
        }
        Ok(())
    }

    /// Validate miscellaneous MCP output schema hint size.
    fn validate_mcp_misc(&self) -> Result<(), ConfigError> {
        if self.mcp.output_schema_hint_bytes < 64 {
            return Err(ConfigError::Validation(format!(
                "mcp.output_schema_hint_bytes must be >= 64, got {}; \
                 use forward_output_schema = false to disable forwarding",
                self.mcp.output_schema_hint_bytes
            )));
        }
        Ok(())
    }

    /// Validate that each `[[scheduler.tasks]]` entry has exactly one of `cron` or `run_at` set.
    fn validate_scheduler(&self) -> Result<(), ConfigError> {
        for task in &self.scheduler.tasks {
            match (&task.cron, &task.run_at) {
                (Some(_), Some(_)) => {
                    return Err(ConfigError::Validation(format!(
                        "scheduler task {:?}: only one of `cron` or `run_at` may be set, not both",
                        task.name
                    )));
                }
                (None, None) => {
                    return Err(ConfigError::Validation(format!(
                        "scheduler task {:?}: either `cron` or `run_at` must be set",
                        task.name
                    )));
                }
                _ => {}
            }
        }
        Ok(())
    }

    fn validate_provider_names(&self) -> Result<(), ConfigError> {
        let known = self.known_provider_names();
        self.validate_named_provider_refs(&known)?;
        self.validate_optional_provider_refs(&known)?;
        Ok(())
    }

    /// Build the set of declared provider names from all `[[llm.providers]]` entries.
    fn known_provider_names(&self) -> std::collections::HashSet<String> {
        self.llm
            .providers
            .iter()
            .map(super::providers::ProviderEntry::effective_name)
            .collect()
    }

    /// Validate every required `*_provider` field references a declared provider.
    ///
    /// The field table lists all subsystem provider references. Each non-empty value must
    /// match a name in `known`.
    fn validate_named_provider_refs(
        &self,
        known: &std::collections::HashSet<String>,
    ) -> Result<(), ConfigError> {
        self.validate_core_provider_refs(known)?;
        self.validate_tool_and_quality_provider_refs(known)
    }

    fn validate_core_provider_refs(
        &self,
        known: &std::collections::HashSet<String>,
    ) -> Result<(), ConfigError> {
        let fields: &[(&str, &crate::providers::ProviderName)] = &[
            (
                "memory.tiers.scene_provider",
                &self.memory.tiers.scene_provider,
            ),
            (
                "memory.compression.compress_provider",
                &self.memory.compression.compress_provider,
            ),
            (
                "memory.consolidation.consolidation_provider",
                &self.memory.consolidation.consolidation_provider,
            ),
            (
                "memory.admission.admission_provider",
                &self.memory.admission.admission_provider,
            ),
            (
                "memory.admission.goal_utility_provider",
                &self.memory.admission.goal_utility_provider,
            ),
            (
                "memory.store_routing.routing_classifier_provider",
                &self.memory.store_routing.routing_classifier_provider,
            ),
            (
                "skills.learning.feedback_provider",
                &self.skills.learning.feedback_provider,
            ),
            (
                "skills.learning.arise_trace_provider",
                &self.skills.learning.arise_trace_provider,
            ),
            (
                "skills.learning.stem_provider",
                &self.skills.learning.stem_provider,
            ),
            (
                "skills.learning.erl_extract_provider",
                &self.skills.learning.erl_extract_provider,
            ),
            (
                "mcp.pruning.pruning_provider",
                &self.mcp.pruning.pruning_provider,
            ),
            (
                "mcp.tool_discovery.embedding_provider",
                &self.mcp.tool_discovery.embedding_provider,
            ),
            (
                "security.response_verification.verifier_provider",
                &self.security.response_verification.verifier_provider,
            ),
            (
                "orchestration.planner_provider",
                &self.orchestration.planner_provider,
            ),
            (
                "orchestration.verify_provider",
                &self.orchestration.verify_provider,
            ),
            (
                "orchestration.tool_provider",
                &self.orchestration.tool_provider,
            ),
            (
                "skills.evaluation.provider",
                &self.skills.evaluation.provider,
            ),
            (
                "skills.proactive_exploration.provider",
                &self.skills.proactive_exploration.provider,
            ),
            (
                "memory.compression_spectrum.promotion_provider",
                &self.memory.compression_spectrum.promotion_provider,
            ),
        ];
        Self::check_provider_refs(fields, known)
    }

    fn validate_tool_and_quality_provider_refs(
        &self,
        known: &std::collections::HashSet<String>,
    ) -> Result<(), ConfigError> {
        let fields: &[(&str, &crate::providers::ProviderName)] = &[
            (
                "security.shadow_sentinel.probe_provider",
                &self.security.shadow_sentinel.probe_provider,
            ),
            (
                "tools.retry.parameter_reformat_provider",
                &self.tools.retry.parameter_reformat_provider,
            ),
            (
                "tools.policy.policy_provider",
                &self.tools.policy.policy_provider,
            ),
            (
                "tools.adversarial_policy.policy_provider",
                &self.tools.adversarial_policy.policy_provider,
            ),
            (
                "tools.speculative.pattern.rerank_provider",
                &self.tools.speculative.pattern.rerank_provider,
            ),
            (
                "tools.compression.evolution_provider",
                &self.tools.compression.evolution_provider,
            ),
            ("quality.proposer_provider", &self.quality.proposer_provider),
            ("quality.checker_provider", &self.quality.checker_provider),
        ];
        Self::check_provider_refs(fields, known)
    }

    fn check_provider_refs(
        fields: &[(&str, &crate::providers::ProviderName)],
        known: &std::collections::HashSet<String>,
    ) -> Result<(), ConfigError> {
        for (field, name) in fields {
            if !name.is_empty() && !known.contains(name.as_str()) {
                return Err(ConfigError::Validation(format!(
                    "{field} = {:?} does not match any [[llm.providers]] entry",
                    name.as_str()
                )));
            }
        }
        Ok(())
    }

    /// Validate optional provider references in complexity routing and router bandit config.
    fn validate_optional_provider_refs(
        &self,
        known: &std::collections::HashSet<String>,
    ) -> Result<(), ConfigError> {
        if let Some(triage) = self
            .llm
            .complexity_routing
            .as_ref()
            .and_then(|cr| cr.triage_provider.as_ref())
            .filter(|t| !t.is_empty() && !known.contains(t.as_str()))
        {
            return Err(ConfigError::Validation(format!(
                "llm.complexity_routing.triage_provider = {:?} does not match any \
                 [[llm.providers]] entry",
                triage.as_str()
            )));
        }

        if let Some(embed) = self
            .llm
            .router
            .as_ref()
            .and_then(|r| r.bandit.as_ref())
            .map(|b| &b.embedding_provider)
            .filter(|p| !p.is_empty() && !known.contains(p.as_str()))
        {
            return Err(ConfigError::Validation(format!(
                "llm.router.bandit.embedding_provider = {:?} does not match any \
                 [[llm.providers]] entry",
                embed.as_str()
            )));
        }

        Ok(())
    }

    fn normalize_legacy_runtime_defaults(&mut self) {
        use crate::defaults::{
            default_debug_dir, default_log_file_path, default_skills_dir, default_sqlite_path,
            is_legacy_default_debug_dir, is_legacy_default_log_file, is_legacy_default_skills_path,
            is_legacy_default_sqlite_path,
        };

        if is_legacy_default_sqlite_path(&self.memory.sqlite_path) {
            self.memory.sqlite_path = default_sqlite_path();
        }

        for skill_path in &mut self.skills.paths {
            if is_legacy_default_skills_path(skill_path) {
                *skill_path = default_skills_dir();
            }
        }

        if is_legacy_default_debug_dir(&self.debug.output_dir) {
            self.debug.output_dir = default_debug_dir();
        }

        if is_legacy_default_log_file(&self.logging.file) {
            self.logging.file = default_log_file_path();
        }
    }
}

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

    fn config_with_sct(threshold: f32) -> Config {
        let mut cfg = Config::default();
        cfg.llm.semantic_cache_threshold = threshold;
        cfg
    }

    #[test]
    fn semantic_cache_threshold_valid_zero() {
        assert!(config_with_sct(0.0).validate().is_ok());
    }

    #[test]
    fn semantic_cache_threshold_valid_mid() {
        assert!(config_with_sct(0.5).validate().is_ok());
    }

    #[test]
    fn semantic_cache_threshold_valid_one() {
        assert!(config_with_sct(1.0).validate().is_ok());
    }

    #[test]
    fn semantic_cache_threshold_invalid_negative() {
        let err = config_with_sct(-0.1).validate().unwrap_err();
        assert!(
            err.to_string().contains("semantic_cache_threshold"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn semantic_cache_threshold_invalid_above_one() {
        let err = config_with_sct(1.1).validate().unwrap_err();
        assert!(
            err.to_string().contains("semantic_cache_threshold"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn semantic_cache_threshold_invalid_nan() {
        let err = config_with_sct(f32::NAN).validate().unwrap_err();
        assert!(
            err.to_string().contains("semantic_cache_threshold"),
            "unexpected error: {err}"
        );
    }

    #[cfg(not(feature = "card-signing"))]
    #[test]
    fn card_trust_policy_require_without_feature_fails_validation() {
        let mut cfg = Config::default();
        cfg.a2a_client.card_trust_policy = crate::channels::CardTrustPolicy::Require;
        let err = cfg.validate().unwrap_err();
        assert!(
            err.to_string().contains("card_trust_policy"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn card_trust_policy_ignore_and_prefer_always_pass_validation() {
        let mut cfg = Config::default();
        cfg.a2a_client.card_trust_policy = crate::channels::CardTrustPolicy::Ignore;
        assert!(cfg.validate().is_ok());
        cfg.a2a_client.card_trust_policy = crate::channels::CardTrustPolicy::Prefer;
        assert!(cfg.validate().is_ok());
    }

    #[cfg(feature = "card-signing")]
    #[test]
    fn card_trust_policy_require_with_feature_passes_validation() {
        let mut cfg = Config::default();
        cfg.a2a_client.card_trust_policy = crate::channels::CardTrustPolicy::Require;
        assert!(cfg.validate().is_ok());
    }

    #[test]
    fn semantic_cache_threshold_invalid_infinity() {
        let err = config_with_sct(f32::INFINITY).validate().unwrap_err();
        assert!(
            err.to_string().contains("semantic_cache_threshold"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn semantic_cache_threshold_invalid_neg_infinity() {
        let err = config_with_sct(f32::NEG_INFINITY).validate().unwrap_err();
        assert!(
            err.to_string().contains("semantic_cache_threshold"),
            "unexpected error: {err}"
        );
    }

    fn probe_config(enabled: bool, threshold: f32, hard_fail_threshold: f32) -> Config {
        let mut cfg = Config::default();
        cfg.memory.compression.probe.enabled = enabled;
        cfg.memory.compression.probe.threshold = threshold;
        cfg.memory.compression.probe.hard_fail_threshold = hard_fail_threshold;
        cfg
    }

    #[test]
    fn probe_disabled_skips_validation() {
        // Invalid thresholds when probe is disabled must not cause errors.
        let cfg = probe_config(false, 0.0, 1.0);
        assert!(cfg.validate().is_ok());
    }

    #[test]
    fn probe_valid_thresholds() {
        let cfg = probe_config(true, 0.6, 0.35);
        assert!(cfg.validate().is_ok());
    }

    #[test]
    fn probe_threshold_zero_invalid() {
        let err = probe_config(true, 0.0, 0.0).validate().unwrap_err();
        assert!(
            err.to_string().contains("probe.threshold"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn probe_hard_fail_threshold_above_one_invalid() {
        let err = probe_config(true, 0.6, 1.0).validate().unwrap_err();
        assert!(
            err.to_string().contains("probe.hard_fail_threshold"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn probe_hard_fail_gte_threshold_invalid() {
        let err = probe_config(true, 0.3, 0.9).validate().unwrap_err();
        assert!(
            err.to_string().contains("probe.hard_fail_threshold"),
            "unexpected error: {err}"
        );
    }

    fn config_with_completeness_threshold(ct: f32) -> Config {
        let mut cfg = Config::default();
        cfg.orchestration.completeness_threshold = ct;
        cfg
    }

    #[test]
    fn completeness_threshold_valid_zero() {
        assert!(config_with_completeness_threshold(0.0).validate().is_ok());
    }

    #[test]
    fn completeness_threshold_valid_default() {
        assert!(config_with_completeness_threshold(0.7).validate().is_ok());
    }

    #[test]
    fn completeness_threshold_valid_one() {
        assert!(config_with_completeness_threshold(1.0).validate().is_ok());
    }

    #[test]
    fn completeness_threshold_invalid_negative() {
        let err = config_with_completeness_threshold(-0.1)
            .validate()
            .unwrap_err();
        assert!(
            err.to_string().contains("completeness_threshold"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn completeness_threshold_invalid_above_one() {
        let err = config_with_completeness_threshold(1.1)
            .validate()
            .unwrap_err();
        assert!(
            err.to_string().contains("completeness_threshold"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn completeness_threshold_invalid_nan() {
        let err = config_with_completeness_threshold(f32::NAN)
            .validate()
            .unwrap_err();
        assert!(
            err.to_string().contains("completeness_threshold"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn completeness_threshold_invalid_infinity() {
        let err = config_with_completeness_threshold(f32::INFINITY)
            .validate()
            .unwrap_err();
        assert!(
            err.to_string().contains("completeness_threshold"),
            "unexpected error: {err}"
        );
    }

    fn config_with_provider(name: &str) -> Config {
        let mut cfg = Config::default();
        cfg.llm.providers.push(crate::providers::ProviderEntry {
            provider_type: crate::providers::ProviderKind::Ollama,
            name: Some(name.into()),
            ..Default::default()
        });
        cfg
    }

    #[test]
    fn validate_provider_names_all_empty_ok() {
        let cfg = Config::default();
        assert!(cfg.validate_provider_names().is_ok());
    }

    #[test]
    fn validate_provider_names_matching_provider_ok() {
        let mut cfg = config_with_provider("fast");
        cfg.memory.admission.admission_provider = crate::providers::ProviderName::new("fast");
        assert!(cfg.validate_provider_names().is_ok());
    }

    #[test]
    fn validate_provider_names_unknown_provider_err() {
        let mut cfg = config_with_provider("fast");
        cfg.memory.admission.admission_provider =
            crate::providers::ProviderName::new("nonexistent");
        let err = cfg.validate_provider_names().unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("admission_provider") && msg.contains("nonexistent"),
            "unexpected error: {msg}"
        );
    }

    #[test]
    fn validate_provider_names_triage_provider_none_ok() {
        let mut cfg = config_with_provider("fast");
        cfg.llm.complexity_routing = Some(crate::providers::ComplexityRoutingConfig {
            triage_provider: None,
            ..Default::default()
        });
        assert!(cfg.validate_provider_names().is_ok());
    }

    #[test]
    fn validate_provider_names_triage_provider_matching_ok() {
        let mut cfg = config_with_provider("fast");
        cfg.llm.complexity_routing = Some(crate::providers::ComplexityRoutingConfig {
            triage_provider: Some(crate::providers::ProviderName::new("fast")),
            ..Default::default()
        });
        assert!(cfg.validate_provider_names().is_ok());
    }

    #[test]
    fn validate_provider_names_triage_provider_unknown_err() {
        let mut cfg = config_with_provider("fast");
        cfg.llm.complexity_routing = Some(crate::providers::ComplexityRoutingConfig {
            triage_provider: Some(crate::providers::ProviderName::new("ghost")),
            ..Default::default()
        });
        let err = cfg.validate_provider_names().unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("triage_provider") && msg.contains("ghost"),
            "unexpected error: {msg}"
        );
    }

    // Regression test for issue #2599: TOML float values must deserialise without error
    // across all config sections that contain f32/f64 fields.
    #[test]
    fn toml_float_fields_deserialise_correctly() {
        let toml = r"
[llm.router.reputation]
enabled = true
decay_factor = 0.95
weight = 0.3

[llm.router.bandit]
enabled = false
cost_weight = 0.3
alpha = 1.0
decay_factor = 0.99

[skills]
disambiguation_threshold = 0.25
cosine_weight = 0.7
";
        // Wrap in a full Config to exercise the nested paths.
        let wrapped = format!(
            "{}\n{}",
            toml,
            r"[memory.semantic]
mmr_lambda = 0.7
"
        );
        // We only need the sub-structs to round-trip; build minimal wrappers.
        let router: crate::providers::RouterConfig = toml::from_str(
            r"[reputation]
enabled = true
decay_factor = 0.95
weight = 0.3
",
        )
        .expect("RouterConfig with float fields must deserialise");
        assert!((router.reputation.unwrap().decay_factor - 0.95).abs() < f64::EPSILON);

        let bandit: crate::providers::BanditConfig =
            toml::from_str("cost_weight = 0.3\nalpha = 1.0\n")
                .expect("BanditConfig with float fields must deserialise");
        assert!((bandit.cost_weight - 0.3_f32).abs() < f32::EPSILON);

        let semantic: crate::memory::SemanticConfig = toml::from_str("mmr_lambda = 0.7\n")
            .expect("SemanticConfig with float fields must deserialise");
        assert!((semantic.mmr_lambda - 0.7_f32).abs() < f32::EPSILON);

        let skills: crate::features::SkillsConfig =
            toml::from_str("disambiguation_threshold = 0.25\n")
                .expect("SkillsConfig with float fields must deserialise");
        assert!((skills.disambiguation_threshold - 0.25_f32).abs() < f32::EPSILON);

        let _ = wrapped; // silence unused-variable lint
    }

    #[test]
    fn validate_max_parallel_zero_rejected() {
        let mut cfg = Config::default();
        cfg.orchestration.max_parallel = 0;
        let err = cfg.validate().unwrap_err().to_string();
        assert!(
            err.contains("max_parallel"),
            "expected max_parallel in error, got: {err}"
        );
    }

    #[test]
    fn validate_max_parallel_one_accepted() {
        let mut cfg = Config::default();
        cfg.orchestration.max_parallel = 1;
        assert!(cfg.validate().is_ok());
    }

    #[test]
    fn validate_max_tasks_zero_rejected() {
        let mut cfg = Config::default();
        cfg.orchestration.max_tasks = 0;
        let err = cfg.validate().unwrap_err().to_string();
        assert!(
            err.contains("max_tasks"),
            "expected max_tasks in error, got: {err}"
        );
    }

    #[test]
    fn validate_max_tasks_one_accepted() {
        let mut cfg = Config::default();
        cfg.orchestration.max_tasks = 1;
        assert!(cfg.validate().is_ok());
    }

    #[test]
    fn validate_aggregator_timeout_zero_rejected() {
        let mut cfg = Config::default();
        cfg.orchestration.aggregator_timeout_secs = 0;
        let err = cfg.validate().unwrap_err().to_string();
        assert!(
            err.contains("aggregator_timeout_secs"),
            "expected aggregator_timeout_secs in error, got: {err}"
        );
    }

    #[test]
    fn validate_planner_timeout_zero_rejected() {
        let mut cfg = Config::default();
        cfg.orchestration.planner_timeout_secs = 0;
        let err = cfg.validate().unwrap_err().to_string();
        assert!(
            err.contains("planner_timeout_secs"),
            "expected planner_timeout_secs in error, got: {err}"
        );
    }

    #[test]
    fn validate_verifier_timeout_zero_rejected() {
        let mut cfg = Config::default();
        cfg.orchestration.verifier_timeout_secs = 0;
        let err = cfg.validate().unwrap_err().to_string();
        assert!(
            err.contains("verifier_timeout_secs"),
            "expected verifier_timeout_secs in error, got: {err}"
        );
    }

    #[test]
    fn focus_auto_consolidate_min_window_zero_rejected() {
        let mut cfg = Config::default();
        cfg.agent.focus.auto_consolidate_min_window = 0;
        let err = cfg.validate().unwrap_err().to_string();
        assert!(
            err.contains("auto_consolidate_min_window"),
            "expected auto_consolidate_min_window in error, got: {err}"
        );
    }

    #[test]
    fn focus_auto_consolidate_min_window_one_accepted() {
        let mut cfg = Config::default();
        cfg.agent.focus.auto_consolidate_min_window = 1;
        assert!(cfg.validate().is_ok());
    }

    fn task_with(cron: Option<&str>, run_at: Option<&str>) -> crate::features::ScheduledTaskConfig {
        crate::features::ScheduledTaskConfig {
            name: "test-task".into(),
            cron: cron.map(Into::into),
            run_at: run_at.map(Into::into),
            kind: crate::features::ScheduledTaskKind::HealthCheck,
            config: serde_json::Value::Null,
        }
    }

    #[test]
    fn scheduler_task_valid_cron() {
        let mut cfg = Config::default();
        cfg.scheduler.tasks.push(task_with(Some("0 9 * * *"), None));
        assert!(cfg.validate().is_ok());
    }

    #[test]
    fn scheduler_task_valid_run_at() {
        let mut cfg = Config::default();
        cfg.scheduler
            .tasks
            .push(task_with(None, Some("2025-01-01T09:00:00Z")));
        assert!(cfg.validate().is_ok());
    }

    #[test]
    fn scheduler_task_neither_cron_nor_run_at_rejected() {
        let mut cfg = Config::default();
        cfg.scheduler.tasks.push(task_with(None, None));
        let err = cfg.validate().unwrap_err().to_string();
        assert!(
            err.contains("either `cron` or `run_at` must be set"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn scheduler_task_both_cron_and_run_at_rejected() {
        let mut cfg = Config::default();
        cfg.scheduler
            .tasks
            .push(task_with(Some("0 9 * * *"), Some("2025-01-01T09:00:00Z")));
        let err = cfg.validate().unwrap_err().to_string();
        assert!(
            err.contains("only one of `cron` or `run_at` may be set"),
            "unexpected error: {err}"
        );
    }

    // ── #5932: 7 previously-dead validate() functions now wired into Config::validate() ──────

    #[test]
    fn validate_rejects_empty_provider_pool() {
        // This is the most severe gap from #5932: `validate_pool` was documented (verbatim)
        // as a load-bearing guarantee by tier_loop.rs/arise.rs but was never actually wired
        // in. `Config::default()` itself now seeds one provider (critic S1 follow-up, so
        // `--dump-config-defaults` output stays self-consistent) — clear it explicitly to
        // exercise the empty-pool branch.
        let mut cfg = Config::default();
        cfg.llm.providers.clear();
        let err = cfg.validate().unwrap_err().to_string();
        assert!(
            err.contains("at least one LLM provider"),
            "expected empty-pool error, got: {err}"
        );
    }

    #[test]
    fn validate_rejects_duplicate_provider_names() {
        let mut cfg = Config::default();
        cfg.llm.providers.push(crate::providers::ProviderEntry {
            provider_type: crate::providers::ProviderKind::Ollama,
            ..Default::default()
        });
        let err = cfg.validate().unwrap_err().to_string();
        assert!(
            err.contains("duplicate provider name"),
            "expected duplicate-name error, got: {err}"
        );
    }

    #[test]
    fn validate_rejects_multiple_default_providers() {
        let mut cfg = Config::default();
        cfg.llm.providers[0].default = true;
        cfg.llm.providers.push(crate::providers::ProviderEntry {
            provider_type: crate::providers::ProviderKind::Ollama,
            name: Some("second".into()),
            default: true,
            ..Default::default()
        });
        let err = cfg.validate().unwrap_err().to_string();
        assert!(
            err.contains("default = true"),
            "expected multiple-default error, got: {err}"
        );
    }

    #[test]
    fn validate_rejects_stt_provider_pointing_at_nonexistent_provider() {
        let mut cfg = Config::default();
        cfg.llm.stt = Some(crate::providers::SttConfig {
            provider: crate::providers::ProviderName::new("ghost"),
            language: crate::providers::default_stt_language(),
        });
        let err = cfg.validate().unwrap_err().to_string();
        assert!(
            err.contains("[llm.stt].provider") && err.contains("ghost"),
            "expected stt-provider-mismatch error, got: {err}"
        );
    }

    #[test]
    fn validate_accepts_stt_provider_matching_existing_provider() {
        let mut cfg = Config::default();
        cfg.llm.stt = Some(crate::providers::SttConfig {
            provider: crate::providers::ProviderName::new("ollama"),
            language: crate::providers::default_stt_language(),
        });
        assert!(cfg.validate().is_ok());
    }

    #[test]
    fn validate_rejects_trajectory_sentinel_inverted_thresholds() {
        let mut cfg = Config::default();
        cfg.security.trajectory.elevated_at = 0.9;
        cfg.security.trajectory.high_at = 0.5;
        let err = cfg.validate().unwrap_err().to_string();
        assert!(
            err.contains("elevated_at") && err.contains("high_at"),
            "expected trajectory threshold-ordering error, got: {err}"
        );
    }

    #[test]
    fn validate_rejects_gateway_invalid_webhook_timeout() {
        // `rate_limit` and `max_body_size` are already covered by `validate_scalar_bounds`
        // (runs earlier in the pipeline), so testing those wouldn't prove
        // `GatewayConfig::validate()` is actually wired in — it would pass identically on
        // pre-#5932 code (critic-flagged shadowing, S2). `webhook_send_timeout_secs` is the
        // one field uniquely reachable only through the new call.
        let mut cfg = Config::default();
        cfg.gateway.webhook_send_timeout_secs = 0;
        let err = cfg.validate().unwrap_err().to_string();
        assert!(
            err.contains("webhook_send_timeout_secs"),
            "expected gateway webhook_send_timeout_secs error, got: {err}"
        );
    }

    #[test]
    fn validate_rejects_negative_utility_scoring_weight() {
        let mut cfg = Config::default();
        cfg.tools.utility.gain_weight = -1.0;
        let err = cfg.validate().unwrap_err().to_string();
        assert!(
            err.contains("gain_weight"),
            "expected utility-scoring weight error, got: {err}"
        );
    }

    #[test]
    fn validate_rejects_fidelity_threshold_ordering() {
        let mut cfg = Config::default();
        cfg.memory.fidelity = Some(crate::fidelity::FidelityConfig {
            full_threshold: 0.2,
            compressed_threshold: 0.5,
            ..Default::default()
        });
        let err = cfg.validate().unwrap_err().to_string();
        assert!(
            err.contains("full_threshold") && err.contains("compressed_threshold"),
            "expected fidelity threshold-ordering error, got: {err}"
        );
    }

    #[test]
    fn validate_accepts_absent_fidelity_config() {
        let cfg = Config::default();
        assert!(cfg.memory.fidelity.is_none());
        assert!(cfg.validate().is_ok());
    }

    #[test]
    fn validate_rejects_acon_inverted_thresholds() {
        let mut cfg = Config::default();
        cfg.memory.compression.acon.passthrough_threshold = 5000;
        cfg.memory.compression.acon.summarize_threshold = 1000;
        let err = cfg.validate().unwrap_err().to_string();
        assert!(
            err.contains("passthrough_threshold") && err.contains("summarize_threshold"),
            "expected acon threshold-ordering error, got: {err}"
        );
    }

    #[test]
    fn validate_rejects_shadow_memory_inverted_thresholds() {
        let mut cfg = Config::default();
        cfg.memory.shadow_memory.enabled = true;
        cfg.memory.shadow_memory.escalation_threshold = 0.75;
        cfg.memory.shadow_memory.risk_threshold = 0.50;
        let err = cfg.validate().unwrap_err().to_string();
        assert!(
            err.contains("escalation_threshold") && err.contains("risk_threshold"),
            "expected shadow_memory threshold-ordering error, got: {err}"
        );
    }

    #[test]
    fn validate_rejects_shadow_memory_equal_thresholds() {
        let mut cfg = Config::default();
        cfg.memory.shadow_memory.enabled = true;
        cfg.memory.shadow_memory.escalation_threshold = 0.6;
        cfg.memory.shadow_memory.risk_threshold = 0.6;
        assert!(
            cfg.validate().is_err(),
            "equal thresholds must be rejected — the escalation band would be empty"
        );
    }

    #[test]
    fn validate_ignores_shadow_memory_thresholds_when_disabled() {
        let mut cfg = Config::default();
        cfg.memory.shadow_memory.enabled = false;
        cfg.memory.shadow_memory.escalation_threshold = 0.9;
        cfg.memory.shadow_memory.risk_threshold = 0.1;
        assert!(
            cfg.validate().is_ok(),
            "inverted thresholds on a disabled shadow_memory config must not fail validation"
        );
    }

    #[test]
    fn validate_rejects_worktree_max_worktrees_zero() {
        let mut cfg = Config::default();
        cfg.worktree.max_worktrees = Some(0);
        let err = cfg.validate().unwrap_err().to_string();
        assert!(
            err.contains("max_worktrees"),
            "expected max_worktrees in error, got: {err}"
        );
    }

    #[test]
    fn validate_accepts_worktree_max_worktrees_positive_or_unset() {
        let mut cfg = Config::default();
        cfg.worktree.max_worktrees = Some(1);
        assert!(cfg.validate().is_ok());
        cfg.worktree.max_worktrees = None;
        assert!(cfg.validate().is_ok());
    }

    #[test]
    fn validate_rejects_worktree_disk_quota_mb_zero() {
        let mut cfg = Config::default();
        cfg.worktree.disk_quota_mb = Some(0);
        let err = cfg.validate().unwrap_err().to_string();
        assert!(
            err.contains("disk_quota_mb"),
            "expected disk_quota_mb in error, got: {err}"
        );
    }

    #[test]
    fn validate_accepts_worktree_disk_quota_mb_positive_or_unset() {
        let mut cfg = Config::default();
        cfg.worktree.disk_quota_mb = Some(1);
        assert!(cfg.validate().is_ok());
        cfg.worktree.disk_quota_mb = None;
        assert!(cfg.validate().is_ok());
    }

    /// Review N1 / critic M1(b): `disk_quota_mb` set with neither the startup sweep nor the
    /// periodic sweep enabled means the quota is evaluated nowhere automatically — must be a
    /// hard config error, not a silent no-op.
    #[test]
    fn validate_rejects_worktree_disk_quota_mb_with_no_evaluation_path_enabled() {
        let mut cfg = Config::default();
        cfg.worktree.disk_quota_mb = Some(100);
        cfg.worktree.auto_reconcile_secs = 0;
        cfg.worktree.reconcile_on_startup = false;
        let err = cfg.validate().unwrap_err().to_string();
        assert!(
            err.contains("disk_quota_mb") && err.contains("never automatically"),
            "expected inert-path error, got: {err}"
        );
    }

    #[test]
    fn validate_accepts_worktree_disk_quota_mb_when_startup_sweep_enabled() {
        let mut cfg = Config::default();
        cfg.worktree.disk_quota_mb = Some(100);
        cfg.worktree.auto_reconcile_secs = 0;
        cfg.worktree.reconcile_on_startup = true;
        assert!(cfg.validate().is_ok());
    }

    #[test]
    fn validate_accepts_worktree_disk_quota_mb_when_periodic_sweep_enabled() {
        let mut cfg = Config::default();
        cfg.worktree.disk_quota_mb = Some(100);
        cfg.worktree.auto_reconcile_secs = 3600;
        cfg.worktree.reconcile_on_startup = false;
        assert!(cfg.validate().is_ok());
    }

    /// Review perf#3: a short `auto_reconcile_secs` runs a full filesystem walk in a tight
    /// loop — must be rejected, matching the `Some(0)` rejection style for the sibling fields.
    #[test]
    fn validate_rejects_worktree_auto_reconcile_secs_short_interval() {
        let mut cfg = Config::default();
        cfg.worktree.auto_reconcile_secs = 1;
        let err = cfg.validate().unwrap_err().to_string();
        assert!(
            err.contains("auto_reconcile_secs"),
            "expected auto_reconcile_secs in error, got: {err}"
        );
    }

    #[test]
    fn validate_accepts_worktree_auto_reconcile_secs_zero_or_at_least_60() {
        let mut cfg = Config::default();
        cfg.worktree.auto_reconcile_secs = 0;
        assert!(cfg.validate().is_ok());
        cfg.worktree.auto_reconcile_secs = 60;
        assert!(cfg.validate().is_ok());
        cfg.worktree.auto_reconcile_secs = 3600;
        assert!(cfg.validate().is_ok());
    }

    /// Regression test (critic S1): `Config::default()` must itself satisfy `validate_pool`
    /// so `--dump-config-defaults` (which serializes `Config::default()` verbatim,
    /// `src/runner.rs`) emits a config that `zeph --config <dump>` can actually load and
    /// validate, rather than a self-inconsistent onboarding trap.
    #[test]
    fn dump_defaults_output_is_self_consistent_and_validates() {
        assert!(Config::default().validate().is_ok());

        let dumped = Config::dump_defaults().expect("dump defaults");
        assert!(
            dumped.contains("[[llm.providers]]"),
            "dumped defaults must include an active provider entry, got:\n{dumped}"
        );
        let reparsed: Config = toml::from_str(&dumped).expect("reparse dumped defaults");
        assert!(reparsed.validate().is_ok());
    }

    // --- orchestration.ensemble validation (spec 073-orch-ensemble-merge, M5/M7) ---

    fn config_with_ensemble(enabled: bool, verify: bool, members: Vec<&str>) -> Config {
        let mut cfg = Config::default();
        cfg.orchestration.ensemble.enabled = enabled;
        cfg.orchestration.ensemble.verify = verify;
        cfg.orchestration.ensemble.members = members.into_iter().map(String::from).collect();
        cfg
    }

    #[test]
    fn ensemble_default_config_validates_trivially() {
        assert!(Config::default().validate().is_ok());
    }

    #[test]
    fn ensemble_disabled_skips_member_list_validation() {
        // enabled=false: an invalid members list must not block startup.
        let cfg = config_with_ensemble(false, false, vec!["a", "b"]);
        assert!(cfg.validate().is_ok());
    }

    #[test]
    fn ensemble_enabled_but_not_verify_skips_member_list_validation() {
        // enabled=true, verify=false: still an unused/staged config, checks skipped.
        let cfg = config_with_ensemble(true, false, vec!["a", "b"]);
        assert!(cfg.validate().is_ok());
    }

    #[test]
    fn ensemble_active_even_length_members_rejected() {
        let cfg = config_with_ensemble(true, true, vec!["a", "b"]);
        let err = cfg.validate().unwrap_err();
        assert!(
            err.to_string().contains("must be odd and >= 3"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn ensemble_active_short_members_rejected() {
        let cfg = config_with_ensemble(true, true, vec!["a"]);
        let err = cfg.validate().unwrap_err();
        assert!(
            err.to_string().contains("must be odd and >= 3"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn ensemble_active_duplicate_members_rejected() {
        let cfg = config_with_ensemble(true, true, vec!["a", "b", "a"]);
        let err = cfg.validate().unwrap_err();
        assert!(
            err.to_string().contains("duplicate provider name"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn ensemble_active_valid_odd_unique_members_accepted() {
        let cfg = config_with_ensemble(true, true, vec!["a", "b", "c"]);
        assert!(cfg.validate().is_ok());
    }

    #[test]
    fn ensemble_active_valid_five_members_accepted() {
        let cfg = config_with_ensemble(true, true, vec!["a", "b", "c", "d", "e"]);
        assert!(cfg.validate().is_ok());
    }

    // --- ema_alpha / ema_decay range validation (security P3) ---

    #[test]
    fn ensemble_active_ema_alpha_above_one_rejected() {
        let mut cfg = config_with_ensemble(true, true, vec!["a", "b", "c"]);
        cfg.orchestration.ensemble.ema_alpha = 1.5;
        let err = cfg.validate().unwrap_err();
        assert!(
            err.to_string().contains("ema_alpha"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn ensemble_active_ema_alpha_negative_rejected() {
        let mut cfg = config_with_ensemble(true, true, vec!["a", "b", "c"]);
        cfg.orchestration.ensemble.ema_alpha = -0.1;
        let err = cfg.validate().unwrap_err();
        assert!(
            err.to_string().contains("ema_alpha"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn ensemble_active_ema_alpha_nan_rejected() {
        let mut cfg = config_with_ensemble(true, true, vec!["a", "b", "c"]);
        cfg.orchestration.ensemble.ema_alpha = f64::NAN;
        let err = cfg.validate().unwrap_err();
        assert!(
            err.to_string().contains("ema_alpha"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn ensemble_active_ema_decay_above_one_rejected() {
        let mut cfg = config_with_ensemble(true, true, vec!["a", "b", "c"]);
        cfg.orchestration.ensemble.ema_decay = 1.1;
        let err = cfg.validate().unwrap_err();
        assert!(
            err.to_string().contains("ema_decay"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn ensemble_active_ema_decay_negative_rejected() {
        let mut cfg = config_with_ensemble(true, true, vec!["a", "b", "c"]);
        cfg.orchestration.ensemble.ema_decay = -0.1;
        let err = cfg.validate().unwrap_err();
        assert!(
            err.to_string().contains("ema_decay"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn ensemble_active_ema_boundaries_zero_and_one_accepted() {
        let mut cfg = config_with_ensemble(true, true, vec!["a", "b", "c"]);
        cfg.orchestration.ensemble.ema_alpha = 0.0;
        cfg.orchestration.ensemble.ema_decay = 1.0;
        assert!(cfg.validate().is_ok());
    }

    #[test]
    fn ensemble_disabled_skips_ema_range_validation() {
        // enabled=false: an out-of-range EMA param must not block startup.
        let mut cfg = config_with_ensemble(false, false, vec![]);
        cfg.orchestration.ensemble.ema_alpha = 5.0;
        assert!(cfg.validate().is_ok());
    }
}