kranz-engine 0.2.2

Governed mission engine for auditable AI coding-agent work.
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
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
//! Layered mission configuration (plan §6).
//!
//! Configuration is resolved from three layers, later layers winning:
//!
//! 1. [`MissionConfig::default()`] — compiled-in defaults
//! 2. `~/.kranz/config.json` — the user's global config ([`crate::paths::global_config`])
//! 3. `<repo>/.kranz/config.json` — per-project config ([`crate::paths::project_config`])
//!
//! Files may be *partial*: any subset of keys. The merge happens on
//! `serde_json::Value` trees so a project file can override a single nested
//! field (e.g. only `worker.model`) without restating the rest. Unknown keys
//! are ignored on deserialization.

use crate::cost::{
    DEFAULT_CODEX_MODEL, DEFAULT_CURSOR_MODEL, DEFAULT_DROID_MODEL, DEFAULT_KIMI_MODEL,
};
use crate::error::{EngineError, Result};
use crate::paths;
use crate::types::{BackendKind, ExecutorTier, MissionConfig, Role, SandboxEnforce};
use std::path::{Path, PathBuf};

/// Reasoning-effort values accepted by `claude --effort`.
const VALID_EFFORTS: [&str; 5] = ["low", "medium", "high", "xhigh", "max"];

/// Maximum dispatch-pool size (`workerCandidates`, KRZ-303). Each candidate
/// is a full paid worker session per unit of work, so the same 8-wide bound
/// as `maxParallelWorkers` applies — well past any useful fan-out.
pub const MAX_WORKER_CANDIDATES: usize = 8;

/// Coarse model capability tiers used by config safety floors.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum ModelTier {
    BelowDefault,
    Default,
    Frontier,
}

/// Parse the optional role backend field.
pub fn parse_backend(raw: Option<&str>) -> std::result::Result<BackendKind, String> {
    match raw {
        None | Some("claude") => Ok(BackendKind::Claude),
        Some("codex") => Ok(BackendKind::Codex),
        Some("droid") => Ok(BackendKind::Droid),
        Some("kimi") => Ok(BackendKind::Kimi),
        Some("local") => Ok(BackendKind::Local),
        Some("acp") => Ok(BackendKind::Acp),
        Some("cursor") => Ok(BackendKind::Cursor),
        Some(other) => Err(other.to_string()),
    }
}

/// Deterministically map a ticket's `task-class` frontmatter to an executor
/// tier. Literal table only — no heuristics: `execution-class` (case- and
/// whitespace-insensitive) routes to [`ExecutorTier::Local`]; every other
/// value, including absence, stays on [`ExecutorTier::Frontier`].
pub fn task_class_to_tier(task_class: Option<&str>) -> ExecutorTier {
    match task_class.map(|s| s.trim().to_ascii_lowercase()) {
        Some(ref s) if s == "execution-class" => ExecutorTier::Local,
        _ => ExecutorTier::Frontier,
    }
}

/// An operator-configured OpenAI-compatible endpoint the Worker can be routed
/// to for the local tier. Mirrors [`crate::types::RoleConfig`]'s local-backend
/// fields (`base_url`/`context_budget`/`temperature`).
#[derive(Debug, Clone, PartialEq)]
pub struct LocalEndpoint {
    pub base_url: String,
    pub context_budget: u32,
    pub temperature: Option<f64>,
}

/// Apply executor-tier routing to a mission config at seed time, so a fresh
/// mission's `mission.created` config already reflects the routing decision.
/// Pure: never touches `config.validator_scrutiny` or `config.validator_functional`.
///
/// Returns the APPLIED tier, which may differ from the requested `tier`: a
/// `Local` request with no configured endpoint fails safe to `Frontier`
/// (leaving the Worker on its frontier default) rather than routing to an
/// endpoint that doesn't exist. A configured dispatch pool
/// (`worker_candidates`) also pins `Frontier`: the pool is an explicit
/// per-candidate backend declaration, and local routing's rewrite of
/// `worker.backend` would sit next to it as a dead, misleading key (pool
/// candidates are never local-backed — validation rejects `local` entries).
pub fn apply_executor_routing(
    config: &mut MissionConfig,
    tier: ExecutorTier,
    local_endpoint: Option<&LocalEndpoint>,
) -> ExecutorTier {
    if !config.worker_candidates.is_empty() {
        return ExecutorTier::Frontier;
    }
    match (tier, local_endpoint) {
        (ExecutorTier::Frontier, _) => ExecutorTier::Frontier,
        (ExecutorTier::Local, None) => ExecutorTier::Frontier,
        (ExecutorTier::Local, Some(endpoint)) => {
            config.worker.backend = Some("local".to_string());
            config.worker.base_url = Some(endpoint.base_url.clone());
            config.worker.context_budget = Some(endpoint.context_budget);
            config.worker.temperature = endpoint.temperature;
            config.allow_below_default_worker_model = true;
            ExecutorTier::Local
        }
    }
}

/// Route the executor tier for a mission seeded from `ticket`, so the
/// resulting `mission.created` config already reflects the routing decision
/// — the single engine-side entry point both the `kranz draft` and
/// `kranz exec` seed paths call before [`crate::orchestrator::MissionEngine::create`].
/// Returns the applied tier and a decision summary to record against the
/// mission once it exists.
pub fn route_ticket_executor(
    cfg: &mut MissionConfig,
    ticket: &crate::ticket::Ticket,
) -> (ExecutorTier, &'static str) {
    route_task_class_executor(cfg, ticket.task_class.as_deref())
}

/// Core of [`route_ticket_executor`], taking the raw `task-class` string
/// directly. [`crate::orchestrator::MissionEngine::create`] calls this with
/// the class recovered from its `goal` argument via
/// [`crate::ticket::parse_task_class_from_goal`] — `create` only ever sees a
/// folded goal string, never the originating [`crate::ticket::Ticket`], so
/// the class has to travel through that one channel.
///
/// The routing table (KRZ-331): when `cfg.routing` declares rules, they are
/// the floor — resolved deterministically by [`crate::routing::table_tier`]
/// (first match wins, no match stays Frontier). An EMPTY table keeps the
/// hardcoded literal floor ([`task_class_to_tier`]) byte-for-byte, so a
/// config that never heard of the table routes exactly as before.
pub fn route_task_class_executor(
    cfg: &mut MissionConfig,
    task_class: Option<&str>,
) -> (ExecutorTier, &'static str) {
    let table_configured = !cfg.routing.is_empty();
    let requested = if table_configured {
        crate::routing::table_tier(&cfg.routing, task_class)
    } else {
        task_class_to_tier(task_class)
    };
    let local_endpoint = match (&cfg.worker.base_url, cfg.worker.context_budget) {
        (Some(base_url), Some(context_budget)) => Some(LocalEndpoint {
            base_url: base_url.clone(),
            context_budget,
            temperature: cfg.worker.temperature,
        }),
        _ => None,
    };
    let applied = apply_executor_routing(cfg, requested, local_endpoint.as_ref());
    let summary = match (requested, applied, table_configured) {
        (ExecutorTier::Local, ExecutorTier::Local, true) => {
            "executor routed local (routing-table rule)"
        }
        (ExecutorTier::Local, ExecutorTier::Local, false) => {
            "executor routed local (execution-class)"
        }
        (ExecutorTier::Local, ExecutorTier::Frontier, true) => {
            "routing-table rule routes local but no local endpoint configured; executor stays frontier"
        }
        (ExecutorTier::Local, ExecutorTier::Frontier, false) => {
            "execution-class ticket but no local endpoint configured; executor stays frontier"
        }
        _ => "executor stays frontier",
    };
    (applied, summary)
}

/// The backend-native model used when an older config selected a non-Claude
/// backend but left the role's Claude default model in place. `Local` has no
/// backend default: local model ids are free-form and sent to the endpoint
/// verbatim, with no Claude→backend rewrite.
fn backend_default_model(kind: BackendKind) -> Option<&'static str> {
    match kind {
        BackendKind::Claude => None,
        BackendKind::Codex => Some(DEFAULT_CODEX_MODEL),
        BackendKind::Droid => Some(DEFAULT_DROID_MODEL),
        BackendKind::Kimi => Some(DEFAULT_KIMI_MODEL),
        BackendKind::Local => None,
        // ACP has no standard model-selection parameter in v1: the peer's
        // model is its own concern (encoded in acpCommand/acpArgs), so there
        // is no backend default to rewrite to.
        BackendKind::Acp => None,
        BackendKind::Cursor => Some(DEFAULT_CURSOR_MODEL),
    }
}

fn role_default_model(role: Role) -> &'static str {
    match role {
        Role::Orchestrator | Role::ValidatorScrutiny => "opus",
        Role::Worker | Role::ValidatorFunctional => "sonnet",
    }
}

/// Return the model actually sent to the backend for this role selection.
///
/// This preserves the existing scrutiny-backend backcompat: a config that set
/// only `validatorScrutiny.backend = "codex"` or `"droid"` used to inherit
/// the Claude default model and then be rewritten to the backend default at
/// dispatch. The same rule is now role-wide.
pub fn effective_model(role: Role, kind: BackendKind, configured: &str) -> String {
    if kind != BackendKind::Claude && configured == role_default_model(role) {
        if let Some(default_model) = backend_default_model(kind) {
            return default_model.to_string();
        }
    }
    configured.to_string()
}

/// Classify a validated backend/model pair. `None` means this model is not a
/// supported model for the selected backend.
pub fn model_tier(kind: BackendKind, model: &str) -> Option<ModelTier> {
    let m = model.trim().to_ascii_lowercase();
    if m.is_empty() {
        return None;
    }
    match kind {
        BackendKind::Claude => {
            if m == "haiku" || m.contains("haiku") {
                Some(ModelTier::BelowDefault)
            } else if m == "sonnet" || m.contains("sonnet") {
                Some(ModelTier::Default)
            } else if m == "opus" || m.contains("opus") || m == "fable" || m.contains("fable") {
                Some(ModelTier::Frontier)
            } else {
                None
            }
        }
        BackendKind::Codex => {
            if m == "codex" || m == DEFAULT_CODEX_MODEL || m.starts_with("gpt-5") {
                Some(ModelTier::Frontier)
            } else {
                None
            }
        }
        BackendKind::Droid => {
            if m == DEFAULT_DROID_MODEL || m.contains("glm") || m.contains("fireworks") {
                Some(ModelTier::BelowDefault)
            } else if m == "fable" || m.contains("fable") {
                Some(ModelTier::Frontier)
            } else {
                None
            }
        }
        BackendKind::Kimi => {
            if m == DEFAULT_KIMI_MODEL {
                Some(ModelTier::Frontier)
            } else if m == "kimi-code/kimi-for-coding" || m == "kimi-code/kimi-for-coding-highspeed"
            {
                Some(ModelTier::BelowDefault)
            } else {
                None
            }
        }
        // Local model ids are free-form and cannot be allowlisted, so every
        // non-empty model classifies uniformly below-default: workers need
        // the allowBelowDefaultWorkerModel opt-in, and a local orchestrator
        // always fails the frontier floor.
        BackendKind::Local => Some(ModelTier::BelowDefault),
        // ACP model ids are equally free-form (the string is recorded for
        // attribution only; ACP v1 has no model-selection parameter), so the
        // same uniform below-default classification applies.
        BackendKind::Acp => Some(ModelTier::BelowDefault),
        // Cursor model ids are drawn from an account-specific catalog
        // (~190 entries on the probe account; `--list-models` output varies
        // by entitlement), so no client-side allowlist is possible and every
        // non-empty id classifies uniformly below-default: a cursor worker
        // needs the allowBelowDefaultWorkerModel opt-in (a deliberate gate
        // for a validator-first backend), and the orchestrator stays on its
        // frontier floor. Model-availability failures themselves are
        // diagnosed deterministically at session start (probe item 5).
        BackendKind::Cursor => Some(ModelTier::BelowDefault),
    }
}

/// Classify the role's configured selection after applying legacy/default
/// model normalization.
pub fn role_model_tier(cfg: &MissionConfig, role: Role) -> Option<ModelTier> {
    let kind = cfg.backend_kind(role);
    let model = effective_model(role, kind, &cfg.role(role).model);
    model_tier(kind, &model)
}

// ---------------------------------------------------------------------------
// The trust rule for repo-owned config (audit 2026-09-01 H1)
// ---------------------------------------------------------------------------

/// Which layer of the merge order a config file occupies — and therefore who
/// is trusted to have written it.
///
/// `~/.kranz/config.json` is the OPERATOR's own file.
/// `<repo>/.kranz/config.json` ships with the repository, so for any repo the
/// operator did not author it is attacker-controlled input, and under
/// `workerIsolation: "checkout"` it sits inside the session's writable cwd
/// where a contained worker can plant it. Without a trust rule that layer
/// wins the merge and can name the binary kranz executes (`claudeBinary`),
/// the endpoint the engine POSTs prompts to (`baseUrl`), the ambient
/// credentials copied into contract commands (`contractEnvPassthrough`), and
/// the switches that turn containment off — before any sandbox, agent, or
/// approval gate exists. Every other repo-owned surface already carries a
/// trust distinction (routing rules are read from the base ref, packs carry a
/// trust class); this closes the last one.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Layer {
    /// `~/.kranz/config.json` — written by the operator.
    Global,
    /// `<repo>/.kranz/config.json` — written by whoever authored the repo.
    Project,
}

/// The four per-role config keys. Their sub-keys share one rule set, so a
/// dotted path is normalized with the role name replaced by `<role>`.
const ROLE_KEYS: [&str; 4] = [
    "orchestrator",
    "worker",
    "validatorScrutiny",
    "validatorFunctional",
];

/// Collapse the leading role segment of a dotted config path to the literal
/// `<role>`, so one table entry covers all four roles.
fn normalize_key_path(dotted: &str) -> String {
    let mut segments: Vec<&str> = dotted.split('.').collect();
    if let Some(first) = segments.first_mut() {
        if ROLE_KEYS.contains(first) {
            *first = "<role>";
        }
    }
    segments.join(".")
}

/// True when `normalized` names `key` or sits underneath it.
fn path_matches(normalized: &str, key: &str) -> bool {
    normalized == key
        || (normalized.len() > key.len()
            && normalized.starts_with(key)
            && normalized.as_bytes()[key.len()] == b'.')
}

/// A sensitive key declares both boundaries here: repository-file input and
/// runtime changes. Ordinary tuning keys remain in `RUNTIME_PATCHABLE`;
/// unclassified runtime keys always fail closed.
#[derive(Clone, Copy)]
enum ProjectPolicy {
    OperatorOnly,
    SandboxFloor,
    ReviewerFloor,
}

#[derive(Clone, Copy)]
enum PatchClass {
    Runtime,
    Consent,
    SandboxFloor,
    Never,
}

struct ConfigTrustRule {
    key: &'static str,
    project: ProjectPolicy,
    runtime: PatchClass,
    reason: &'static str,
}

impl ConfigTrustRule {
    const fn operator_only(key: &'static str, runtime: PatchClass, reason: &'static str) -> Self {
        Self {
            key,
            project: ProjectPolicy::OperatorOnly,
            runtime,
            reason,
        }
    }
}

const CONFIG_TRUST_RULES: &[ConfigTrustRule] = &[
    // Consent-bearing keys. `apply_validated_patch` already refuses these
    // from the control inbox as a human decision; a file the repository
    // ships is no more a human decision than a file an agent drops
    // (2026-09-01 audit follow-up review, F-7 and F-8).
    ConfigTrustRule::operator_only(
        "skipScrutiny",
        PatchClass::Consent,
        "it removes the scrutiny validation round",
    ),
    ConfigTrustRule::operator_only(
        "skipFunctional",
        PatchClass::Consent,
        "it removes the functional validation round",
    ),
    ConfigTrustRule::operator_only(
        "denyPatterns",
        PatchClass::Consent,
        "it is the Bash deny list every session inherits",
    ),
    ConfigTrustRule::operator_only(
        "<role>.tools",
        PatchClass::Never,
        "it lands in the session's tool allow list, including the read-only validators'",
    ),
    ConfigTrustRule::operator_only(
        "allowBelowDefaultWorkerModel",
        PatchClass::Runtime,
        "it lifts the worker model floor the operator set",
    ),
    ConfigTrustRule::operator_only(
        "workerIsolation",
        PatchClass::Never,
        "checkout isolation makes the repository root the worker's writable cwd",
    ),
    ConfigTrustRule::operator_only(
        "claudeBinary",
        PatchClass::Never,
        "it names the binary kranz executes, with the operator's full environment and no sandbox",
    ),
    ConfigTrustRule::operator_only(
        "packDir",
        PatchClass::Never,
        "the pack it names supplies shell gate commands the engine runs",
    ),
    ConfigTrustRule::operator_only(
        "contractEnvPassthrough",
        PatchClass::Never,
        "it copies named ambient credentials verbatim into contract-command environments",
    ),
    ConfigTrustRule::operator_only(
        "dangerouslyAllowAll",
        PatchClass::Consent,
        "it puts every agent session in bypassPermissions",
    ),
    ConfigTrustRule::operator_only(
        "validatorAllowUncontainedDegrade",
        PatchClass::Consent,
        "it reopens the uncontained-validator degrade the containment work closed",
    ),
    ConfigTrustRule::operator_only(
        "allowValidatorCommands",
        PatchClass::Consent,
        "it grants validators shell commands with no human step",
    ),
    ConfigTrustRule::operator_only(
        "localBackendAllowedHosts",
        PatchClass::Never,
        "it is the operator's own escape hatch from the local-backend loopback rule",
    ),
    // `hooks` is deliberately ABSENT from this list. The github webhook
    // secret is per-repository by design (`hooks::load_hooks` reads the
    // project layer, and a test pins that), it lives in a file kranz's own
    // materialized gitignore keeps untracked, and an attacker who sets it
    // gains nothing: it is the HMAC key the server checks INBOUND webhooks
    // against, not a program, an outbound endpoint, or a containment
    // escape. Its exposure problem is the `config show` one, closed by
    // redaction there.
    ConfigTrustRule::operator_only(
        "slack",
        PatchClass::Never,
        "it carries the Slack bot and app tokens, and the channel mission output is posted to \
         (the Slack bridge reads the global layer only)",
    ),
    ConfigTrustRule::operator_only(
        "hookStatus",
        PatchClass::Never,
        "the per-run capability token rides its endpoint",
    ),
    ConfigTrustRule::operator_only(
        "workspace.remote",
        PatchClass::Never,
        "it names a remote workspace URL and the env var holding its token",
    ),
    ConfigTrustRule::operator_only(
        "<role>.acpCommand",
        PatchClass::Never,
        "it names the ACP agent program",
    ),
    ConfigTrustRule::operator_only(
        "<role>.acpArgs",
        PatchClass::Never,
        "it is argv for the ACP agent program",
    ),
    ConfigTrustRule::operator_only(
        "<role>.baseUrl",
        PatchClass::Never,
        "the engine POSTs the assembled prompt to it from outside every sandbox",
    ),
    ConfigTrustRule::operator_only(
        "<role>.sandbox.extraWrite",
        PatchClass::Never,
        "it widens the sandbox write allowlist",
    ),
    ConfigTrustRule::operator_only(
        "<role>.sandbox.egress",
        PatchClass::Never,
        "it widens the sandbox egress allowlist",
    ),
    ConfigTrustRule::operator_only(
        "<role>.sandbox.provider",
        PatchClass::Never,
        "it selects which containment mechanism wraps sessions",
    ),
    ConfigTrustRule::operator_only(
        "<role>.sandbox.image",
        PatchClass::Never,
        "it names the container image sessions run inside",
    ),
    ConfigTrustRule {
        key: "<role>.sandbox.enforce",
        project: ProjectPolicy::SandboxFloor,
        runtime: PatchClass::SandboxFloor,
        reason: "a repository may raise sandbox enforcement, never lower it",
    },
    ConfigTrustRule {
        key: "reviewerIndependence",
        project: ProjectPolicy::ReviewerFloor,
        runtime: PatchClass::Never,
        reason: "a repository may strengthen reviewer independence, never weaken it",
    },
];

fn config_trust_rule(normalized: &str) -> Option<&'static ConfigTrustRule> {
    CONFIG_TRUST_RULES
        .iter()
        .find(|rule| path_matches(normalized, rule.key))
}

/// Rank a `sandbox.enforce` value so raising and lowering can be told apart:
/// `off` < `fs` < `fs+net`. An absent or unrecognized value ranks `off`,
/// which is the compiled-in default.
fn enforce_rank(value: Option<&serde_json::Value>) -> u8 {
    use serde::Deserialize as _;
    // Serde also accepts maps for unit enum variants. Rank the same typed
    // value config deserialization sees, rather than treating those as Off.
    match value.and_then(|value| SandboxEnforce::deserialize(value).ok()) {
        Some(SandboxEnforce::Fs) => 1,
        Some(SandboxEnforce::FsNet) => 2,
        Some(SandboxEnforce::Off) | None => 0,
    }
}

fn project_layer_refusal(file: &Path, dotted: &str, reason: &str) -> EngineError {
    EngineError::Config(format!(
        "{}: the project config layer may not set {dotted:?}{reason}. \
         Operator-only keys are settable from the global layer \
         (~/.kranz/config.json) only.",
        file.display()
    ))
}

/// Refuse a project-layer patch that sets an operator-only key.
///
/// `base` is the tree the layers before this one already merged to, which is
/// what makes the sandbox rule directional: a repository may RAISE
/// `<role>.sandbox.enforce` (asking for more containment than the operator
/// configured is always safe) and may never lower it. Likewise, it may add
/// independent reviewer requirements but cannot remove the operator's floor.
pub fn check_project_layer_keys(
    patch: &serde_json::Value,
    base: &serde_json::Value,
    file: &Path,
) -> Result<()> {
    if !patch.is_object() || !base.is_object() {
        return Err(EngineError::Config(format!(
            "{}: project config changes require JSON objects at the top level",
            file.display()
        )));
    }
    let mut trail: Vec<String> = Vec::new();
    walk_project_layer(patch, Some(base), file, &mut trail)
}

fn check_project_reviewer_floor(
    policy: &serde_json::Value,
    base: Option<&serde_json::Value>,
    file: &Path,
    dotted: &str,
    reason: &str,
) -> Result<()> {
    for role in ["scrutiny", "functional"] {
        let required = base
            .and_then(|policy| policy.get(role))
            .and_then(serde_json::Value::as_bool)
            == Some(true);
        // Object omissions inherit through deep_merge; replacing the whole
        // policy or explicitly disabling a role removes the operator's floor.
        let retained = policy.is_object()
            && policy
                .get(role)
                .is_none_or(|value| value.as_bool() == Some(true));
        if required && !retained {
            return Err(project_layer_refusal(
                file,
                &format!("{dotted}.{role}"),
                reason,
            ));
        }
    }
    Ok(())
}

fn walk_project_layer(
    patch: &serde_json::Value,
    base: Option<&serde_json::Value>,
    file: &Path,
    trail: &mut Vec<String>,
) -> Result<()> {
    let serde_json::Value::Object(map) = patch else {
        return Ok(());
    };
    for (key, value) in map {
        trail.push(key.clone());
        let dotted = trail.join(".");
        let normalized = normalize_key_path(&dotted);

        let base_value = base.and_then(|b| b.get(key));
        // Serde accepts positional arrays for structs. Replacing a role or
        // sandbox that way would skip this object walk while still changing
        // protected fields. A positional base would also hide its floor.
        let protected_container = CONFIG_TRUST_RULES.iter().any(|rule| {
            (normalized != rule.key && path_matches(rule.key, &normalized))
                || (normalized == rule.key && matches!(rule.project, ProjectPolicy::ReviewerFloor))
        });
        if protected_container {
            if base_value.is_some_and(|base| !base.is_object()) {
                return Err(EngineError::Config(format!(
                    "{}: cannot safely merge project config over non-object inherited field {dotted:?}; protected config containers must be JSON objects",
                    file.display()
                )));
            }
            if !value.is_object() {
                return Err(EngineError::Config(format!(
                    "{}: project config field {dotted:?} must be a JSON object; positional arrays and scalar replacements bypass protected child checks",
                    file.display()
                )));
            }
        }
        if let Some(rule) = config_trust_rule(&normalized) {
            match rule.project {
                ProjectPolicy::OperatorOnly => {
                    return Err(project_layer_refusal(file, &dotted, rule.reason));
                }
                ProjectPolicy::SandboxFloor if normalized == rule.key => {
                    if enforce_rank(Some(value)) < enforce_rank(base_value) {
                        return Err(project_layer_refusal(file, &dotted, rule.reason));
                    }
                }
                ProjectPolicy::ReviewerFloor if normalized == rule.key => {
                    check_project_reviewer_floor(value, base_value, file, &dotted, rule.reason)?;
                }
                _ => {}
            }
        }

        walk_project_layer(value, base_value, file, trail)?;
        trail.pop();
    }
    Ok(())
}

/// Refuse a `claudeBinary` whose resolution depends on where kranz was
/// invoked, or which the repository itself supplies.
///
/// A relative path resolves against the process working directory, so `kranz
/// ready` run one directory over executes a different program. A path inside
/// the repository is repo-authored content executed as the operator with the
/// operator's full environment — the H1 primary path, closed here as well as
/// at the layer rule so a global-layer typo or an operator-set in-repo path
/// is caught too.
pub fn validate_claude_binary(cfg: &MissionConfig, repo_root: &Path) -> Result<()> {
    let Some(raw) = cfg.claude_binary.as_deref() else {
        return Ok(());
    };
    let trimmed = raw.trim();
    if trimmed.is_empty() {
        return Err(EngineError::Config(
            "claudeBinary must not be empty; omit the key to auto-discover".into(),
        ));
    }
    let candidate = Path::new(trimmed);
    if !candidate.is_absolute() {
        return Err(EngineError::Config(format!(
            "claudeBinary {trimmed:?} must be an absolute path: a relative path resolves \
             against the process working directory, so which program runs depends on where \
             kranz was invoked"
        )));
    }
    // Both spellings of the candidate and both spellings of the root are
    // compared: a not-yet-existing binary cannot be canonicalized (the
    // planted-then-created case), and on macOS a temp root canonicalizes
    // through /private while its unresolved form does not, so a single pair
    // would miss one side of the comparison.
    let candidate_forms = [
        candidate.to_path_buf(),
        std::fs::canonicalize(candidate).unwrap_or_else(|_| candidate.to_path_buf()),
    ];
    let root_forms = [
        repo_root.to_path_buf(),
        std::fs::canonicalize(repo_root).unwrap_or_else(|_| repo_root.to_path_buf()),
    ];
    for resolved in &candidate_forms {
        for root in &root_forms {
            if resolved.starts_with(root) {
                return Err(EngineError::Config(format!(
                    "claudeBinary {trimmed:?} resolves inside the repository at {}: repository \
                     content must never name the binary kranz executes",
                    root.display()
                )));
            }
        }
    }
    Ok(())
}

/// Load the effective config for a repo: defaults, then the global file,
/// then the project file (later layers win). Missing files are fine;
/// unreadable or unparseable files are a [`EngineError::Config`] naming the
/// offending path. The project layer is additionally held to the
/// operator-only key rule ([`check_project_layer_keys`]).
pub fn load(repo_root: &Path) -> Result<MissionConfig> {
    let mut layers: Vec<(PathBuf, Layer)> = Vec::new();
    if let Some(global) = paths::global_config() {
        layers.push((global, Layer::Global));
    }
    layers.push((paths::project_config(repo_root), Layer::Project));
    let cfg = load_layers_with_roles(&layers)?;
    validate_claude_binary(&cfg, repo_root)?;
    Ok(cfg)
}

/// Merge the given config files (in order, later wins) over the compiled-in
/// defaults. Exposed so callers (and tests) can supply explicit layer paths
/// instead of the real home directory.
///
/// Every layer is treated as [`Layer::Global`]: an explicit-path caller is
/// the operator (or a test), not a repository. Use
/// [`load_layers_with_roles`] when a layer's provenance matters.
pub fn load_layers(layers: &[PathBuf]) -> Result<MissionConfig> {
    let with_roles: Vec<(PathBuf, Layer)> = layers
        .iter()
        .map(|path| (path.clone(), Layer::Global))
        .collect();
    load_layers_with_roles(&with_roles)
}

/// [`load_layers`] with each layer's provenance declared, so the
/// operator-only key rule ([`check_project_layer_keys`]) can refuse a
/// repository-owned layer that names the binary kranz executes, the endpoint
/// it POSTs prompts to, a credential, or a containment escape.
pub fn load_layers_with_roles(layers: &[(PathBuf, Layer)]) -> Result<MissionConfig> {
    let mut merged = serde_json::to_value(MissionConfig::default())?;

    for (path, layer) in layers {
        let text = match std::fs::read_to_string(path) {
            Ok(text) => text,
            // Absent layers are simply skipped; anything else is an error.
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => continue,
            Err(e) => {
                return Err(EngineError::Config(format!(
                    "cannot read config file {}: {e}",
                    path.display()
                )))
            }
        };

        let patch: serde_json::Value = serde_json::from_str(&text).map_err(|e| {
            EngineError::Config(format!(
                "invalid JSON in config file {}: {e}",
                path.display()
            ))
        })?;

        if !patch.is_object() {
            return Err(EngineError::Config(format!(
                "config file {} must contain a JSON object at the top level",
                path.display()
            )));
        }

        if *layer == Layer::Project {
            check_project_layer_keys(&patch, &merged, path)?;
        }

        deep_merge(&mut merged, &patch);
    }

    serde_json::from_value(merged)
        .map_err(|e| EngineError::Config(format!("merged configuration does not deserialize: {e}")))
}

/// Recursively merge `patch` into `base`: objects merge key-wise, everything
/// else (scalars, arrays, nulls) is replaced wholesale by the patch value.
///
/// Public because the server/CLI reuse it for `config.changed` patches.
pub fn deep_merge(base: &mut serde_json::Value, patch: &serde_json::Value) {
    match (base, patch) {
        (serde_json::Value::Object(base_map), serde_json::Value::Object(patch_map)) => {
            for (key, patch_val) in patch_map {
                match base_map.get_mut(key) {
                    Some(slot) => deep_merge(slot, patch_val),
                    None => {
                        base_map.insert(key.clone(), patch_val.clone());
                    }
                }
            }
        }
        (slot, patch_val) => *slot = patch_val.clone(),
    }
}

// ---------------------------------------------------------------------------
// Runtime `config-change` patches: who may set what (audit 2026-09-01 C1)
// ---------------------------------------------------------------------------

/// Where a runtime `config-change` patch came from.
///
/// The control inbox (`<mission>/control/*.json`) is an unauthenticated
/// filesystem channel: the only authorization is the ability to create a
/// file, which under the default `sandbox.enforce: off` posture every worker
/// session has. A patch that arrives that way must never be able to carry
/// the consent-bearing keys a human is supposed to decide — the resulting
/// `config.changed` event is otherwise indistinguishable from an operator's.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PatchSource {
    /// A human-driven surface: the CLI, the mutation-token REST route, or an
    /// authorized Slack command.
    Operator,
    /// The mission's control inbox, drained by the run loop.
    Inbox,
}

/// Keys a running mission may legitimately be re-tuned with, from either
/// source. This is the complete list the submission surfaces actually
/// produce: `kranz exec --max-cycles`, `kranz config role` / Slack
/// `/kranz config` (role backend/model/effort), and the dashboard's role
/// selection — plus the neighbouring bounds an operator re-tunes with them.
const RUNTIME_PATCHABLE: &[&str] = &[
    "maxFixCyclesPerMilestone",
    "maxRespawns",
    "maxParallelWorkers",
    "eventStreamThrottleMs",
    "planningIdleReleaseMinutes",
    "autoWork",
    "consideredAlternativesFeatureThreshold",
    "consideredAlternativesTouchSetThreshold",
    "consideredAlternativesHighUsdThreshold",
    "rubberStampThresholdMs",
    "<role>.model",
    "<role>.backend",
    "<role>.reasoningEffort",
    "<role>.maxTurns",
    "<role>.maxBudgetUsd",
    "<role>.contextBudget",
    "<role>.temperature",
];

fn classify_patch_key(normalized: &str) -> (PatchClass, &'static str) {
    if let Some(rule) = config_trust_rule(normalized) {
        // Only the enforcement scalar is directional. Unknown descendants
        // retain the existing default-deny runtime policy.
        let class = match rule.runtime {
            PatchClass::SandboxFloor if normalized != rule.key => PatchClass::Never,
            class => class,
        };
        return (class, rule.reason);
    }
    if RUNTIME_PATCHABLE
        .iter()
        .any(|key| path_matches(normalized, key))
    {
        return (PatchClass::Runtime, "");
    }
    (PatchClass::Never, "")
}

/// Refuse a runtime `config-change` patch that reaches past the keys its
/// source is allowed to set.
///
/// `base` is the current effective config as JSON, which makes the sandbox
/// rule directional exactly as the layer rule is: raising
/// `<role>.sandbox.enforce` is fine from either source, lowering it is a
/// consent act.
pub fn check_runtime_patch(
    patch: &serde_json::Value,
    base: &serde_json::Value,
    source: PatchSource,
) -> Result<()> {
    let mut trail: Vec<String> = Vec::new();
    walk_runtime_patch(patch, Some(base), source, &mut trail)
}

fn walk_runtime_patch(
    patch: &serde_json::Value,
    base: Option<&serde_json::Value>,
    source: PatchSource,
    trail: &mut Vec<String>,
) -> Result<()> {
    if let serde_json::Value::Object(map) = patch {
        for (key, value) in map {
            trail.push(key.clone());
            walk_runtime_patch(value, base.and_then(|b| b.get(key)), source, trail)?;
            trail.pop();
        }
        return Ok(());
    }

    let dotted = trail.join(".");
    let normalized = normalize_key_path(&dotted);

    let (class, reason) = classify_patch_key(&normalized);
    match class {
        PatchClass::SandboxFloor => {
            if enforce_rank(Some(patch)) >= enforce_rank(base) {
                return Ok(());
            }
            match source {
                PatchSource::Operator => Ok(()),
                PatchSource::Inbox => Err(EngineError::Config(format!(
                    "refusing a control-inbox config change to {dotted:?}: lowering sandbox \
                     enforcement is a consent act, and the control inbox is an \
                     unauthenticated filesystem channel"
                ))),
            }
        }
        PatchClass::Runtime => Ok(()),
        PatchClass::Consent => match source {
            PatchSource::Operator => Ok(()),
            PatchSource::Inbox => Err(EngineError::Config(format!(
                "refusing a control-inbox config change to {dotted:?}: {reason}, so it is a \
                 human decision — the control inbox is an unauthenticated filesystem \
                 channel and cannot carry consent"
            ))),
        },
        PatchClass::Never => Err(EngineError::Config(format!(
            "{dotted:?} is not runtime-patchable: it is seed-time or operator-file \
             configuration (a program, an endpoint, a credential, or the containment \
             shape), not a mission knob"
        ))),
    }
}

/// Apply a partial JSON patch to an effective mission config and validate the
/// merged result exactly as the engine would before accepting it.
///
/// Submission surfaces use this before enqueueing `config-change`, while the
/// engine repeats the check when it drains the command. The second check is
/// still required because another queued patch may win the race in between.
///
/// This is the OPERATOR entry point (every caller of it is a human-driven,
/// authorized surface). The run loop's drain path uses
/// [`apply_validated_patch_from`] with [`PatchSource::Inbox`].
pub fn apply_validated_patch(
    current: &MissionConfig,
    patch: &serde_json::Value,
) -> Result<MissionConfig> {
    apply_validated_patch_from(current, patch, PatchSource::Operator)
}

/// [`apply_validated_patch`] with the patch's origin declared, so the
/// consent-bearing keys can be refused when the patch came off the
/// unauthenticated control inbox.
pub fn apply_validated_patch_from(
    current: &MissionConfig,
    patch: &serde_json::Value,
    source: PatchSource,
) -> Result<MissionConfig> {
    let mut value = serde_json::to_value(current)?;
    check_runtime_patch(patch, &value, source)?;
    deep_merge(&mut value, patch);
    let merged: MissionConfig = serde_json::from_value(value)
        .map_err(|e| EngineError::Config(format!("patch produces invalid config: {e}")))?;
    validate(&merged)?;
    Ok(merged)
}

/// The host of an `http(s)://` URL, or `""` when the string is not one.
/// Userinfo is stripped (`http://user@host/` is `host`) and a bracketed IPv6
/// literal keeps its own colons (`http://[::1]:8080` is `::1`).
fn base_url_host(url: &str) -> &str {
    let Some(rest) = url
        .strip_prefix("http://")
        .or_else(|| url.strip_prefix("https://"))
    else {
        return "";
    };
    let authority = rest.split(['/', '?', '#']).next().unwrap_or("");
    let after_userinfo = match authority.rfind('@') {
        Some(idx) => &authority[idx + 1..],
        None => authority,
    };
    if let Some(bracketed) = after_userinfo.strip_prefix('[') {
        return match bracketed.split_once(']') {
            Some((host, _)) => host,
            None => "",
        };
    }
    after_userinfo.split(':').next().unwrap_or(after_userinfo)
}

/// `localhost` or any address in a loopback range (127.0.0.0/8, ::1).
fn host_is_loopback(host: &str) -> bool {
    host.eq_ignore_ascii_case("localhost")
        || host
            .parse::<std::net::IpAddr>()
            .is_ok_and(|ip| ip.is_loopback())
}

/// Validate invariants the engine relies on (plan §6). Returns
/// [`EngineError::Config`] describing the first violation found.
pub fn validate(cfg: &MissionConfig) -> Result<()> {
    crate::reviewer_independence::validate_config(cfg)?;
    let roles = [
        ("orchestrator", &cfg.orchestrator),
        ("worker", &cfg.worker),
        ("validatorScrutiny", &cfg.validator_scrutiny),
        ("validatorFunctional", &cfg.validator_functional),
    ];
    for (name, role) in roles {
        if !VALID_EFFORTS.contains(&role.reasoning_effort.as_str()) {
            return Err(EngineError::Config(format!(
                "{name}.reasoningEffort must be one of {VALID_EFFORTS:?}, got {:?}",
                role.reasoning_effort
            )));
        }
    }

    if cfg.max_fix_cycles_per_milestone < 1 {
        return Err(EngineError::Config(
            "maxFixCyclesPerMilestone must be at least 1".into(),
        ));
    }

    if cfg.max_respawns > 5 {
        return Err(EngineError::Config(format!(
            "maxRespawns must be at most 5, got {}",
            cfg.max_respawns
        )));
    }

    if !(10..=5000).contains(&cfg.event_stream_throttle_ms) {
        return Err(EngineError::Config(format!(
            "eventStreamThrottleMs must be in 10..=5000, got {}",
            cfg.event_stream_throttle_ms
        )));
    }
    if !cfg.considered_alternatives_high_usd_threshold.is_finite()
        || cfg.considered_alternatives_high_usd_threshold < 0.0
    {
        return Err(EngineError::Config(format!(
            "consideredAlternativesHighUsdThreshold must be finite and non-negative, got {}",
            cfg.considered_alternatives_high_usd_threshold
        )));
    }

    // Parallel workers (roadmap M3): `1` (the default) keeps the sequential
    // run loop byte-for-byte; `2..=8` opts into parallel-within-milestone
    // execution (independent features run concurrently, each in its own git
    // worktree, then merge in declared order). `0` is meaningless (no worker
    // can ever run) and anything above 8 is well past any useful fan-out for a
    // single repo, so both are rejected.
    if !(1..=8).contains(&cfg.max_parallel_workers) {
        return Err(EngineError::Config(format!(
            "maxParallelWorkers must be in 1..=8 (1 = sequential; >1 opts into M3 \
             parallel workers), got {}",
            cfg.max_parallel_workers
        )));
    }

    // Heterogeneous dispatch pool (ticket heterogeneous-dispatch-pool,
    // KRZ-303): `workerCandidates` is the COMPLETE backend list the worker
    // role fans out to (worker.backend applies only when the list is empty).
    // Every entry is checked by the same rules as the worker role itself —
    // known backend, supported backend/model pair, the worker model floor,
    // and the sandbox fail-closed pairs — because each one WILL drive real
    // worker sessions.
    if cfg.worker_candidates.len() == 1 {
        return Err(EngineError::Config(
            "workerCandidates with exactly one entry is a roundabout worker.backend; \
             use worker.backend (the pool exists for N >= 2 heterogeneous candidates)"
                .into(),
        ));
    }
    if cfg.worker_candidates.len() > MAX_WORKER_CANDIDATES {
        return Err(EngineError::Config(format!(
            "workerCandidates supports at most {MAX_WORKER_CANDIDATES} candidates, got {}",
            cfg.worker_candidates.len()
        )));
    }
    // The pool and M3 parallel features are two different fan-out models
    // (same unit to N backends vs N units to one backend each). Combining
    // them has no defined semantics in this pass — reject rather than pick
    // one silently.
    if !cfg.worker_candidates.is_empty() && cfg.max_parallel_workers > 1 {
        return Err(EngineError::Config(
            "workerCandidates (dispatch pool: one unit to N backends) and \
             maxParallelWorkers > 1 (M3: N independent units concurrently) are mutually \
             exclusive in this pass; configure one fan-out model"
                .into(),
        ));
    }
    for (i, candidate) in cfg.worker_candidates.iter().enumerate() {
        let kind = parse_backend(Some(&candidate.backend)).map_err(|other| {
            EngineError::Config(format!(
                "workerCandidates[{i}].backend must be one of \"claude\", \"codex\", \"droid\", \"kimi\", \"cursor\", got {other:?}"
            ))
        })?;
        // local/acp need per-role endpoint/command config (baseUrl /
        // contextBudget / acpCommand) that has no per-candidate home in this
        // pass; refuse rather than silently share the worker role's.
        if matches!(kind, BackendKind::Local | BackendKind::Acp) {
            return Err(EngineError::Config(format!(
                "workerCandidates[{i}].backend {:?} is not supported in this pass: local/acp \
                 need per-candidate endpoint/command config (a deliberate widening); use \
                 claude, codex, droid, kimi, or cursor candidates",
                candidate.backend
            )));
        }
        // Same fail-closed sandbox pair as the worker role: a candidate that
        // cannot honor the requested enforcement must never run with the
        // operator believing it contained.
        if cfg.worker.sandbox.enforce != SandboxEnforce::Off && !kind.supports_sandbox_enforcement()
        {
            return Err(EngineError::Config(format!(
                "workerCandidates[{i}].backend {:?} cannot honor sandbox.enforce={:?}: only the \
                 claude backend applies the resolved OS sandbox; run with sandbox.enforce=off, \
                 or drop the non-claude candidate",
                candidate.backend,
                cfg.worker.sandbox.enforce.as_str()
            )));
        }
        let effective = effective_model(Role::Worker, kind, &candidate.model);
        let tier = model_tier(kind, &effective).ok_or_else(|| {
            EngineError::Config(format!(
                "workerCandidates[{i}] effective model {effective:?} (configured as {:?}) is not supported by backend {:?}",
                candidate.model,
                candidate.backend
            ))
        })?;
        // The worker model floor applies per candidate — a below-default
        // stream is exactly as much a worker session as the role's own.
        if tier < ModelTier::Default && !cfg.allow_below_default_worker_model {
            return Err(EngineError::Config(format!(
                "workerCandidates[{i}] effective model {effective:?} (configured as {:?}) on backend {:?} is below the default worker tier; set \
                 allowBelowDefaultWorkerModel=true on this mission to opt in",
                candidate.model,
                candidate.backend
            )));
        }
    }

    // Backend routing table (ticket `backend-routing-abstraction`, KRZ-331):
    // shape-only checks (blank/duplicate task classes) live in
    // `routing::validate_table` and fail closed naming the offending rule. A
    // rule routing `local` with no endpoint configured is NOT an error here:
    // `apply_executor_routing` already fails safe to Frontier for exactly
    // that case, with the decision recorded against the mission.
    if let Err(err) = crate::routing::validate_table(&cfg.routing) {
        return Err(EngineError::Config(err));
    }

    // Hook-status lane (ticket `agent-hooks-status-signals`): when enabled,
    // the endpoint is REQUIRED and must be a loopback HTTP(S) URL — the
    // per-run capability token rides it, so pointing it at a remote host
    // would leak signal authority off-machine. A disabled lane ignores the
    // endpoint entirely (byte-identical pre-lane behavior).
    if let Some(hook_status) = &cfg.hook_status {
        if hook_status.enabled {
            if hook_status.endpoint.trim().is_empty() {
                return Err(EngineError::Config(
                    "hookStatus.enabled requires hookStatus.endpoint (the loopback signal \
                     POST URL, e.g. http://127.0.0.1:4560/api/hook-status)"
                        .to_string(),
                ));
            }
            if !crate::hook_status::endpoint_is_loopback_http(&hook_status.endpoint) {
                return Err(EngineError::Config(format!(
                    "hookStatus.endpoint must be a loopback http(s) URL (the per-run \
                     capability token rides it), got {:?}",
                    hook_status.endpoint
                )));
            }
        }
    }

    for (role, name) in [
        (Role::Orchestrator, "orchestrator"),
        (Role::Worker, "worker"),
        (Role::ValidatorScrutiny, "validatorScrutiny"),
        (Role::ValidatorFunctional, "validatorFunctional"),
    ] {
        let role_cfg = cfg.role(role);
        let kind = parse_backend(role_cfg.backend.as_deref()).map_err(|other| {
            EngineError::Config(format!(
                "{name}.backend must be one of None, \"claude\", \"codex\", \"droid\", \"kimi\", \"local\", \"acp\", \"cursor\", got {other:?}"
            ))
        })?;
        // Fail closed on a silently-unenforced sandbox: only the claude
        // backend wraps its sessions in the engine-resolved OS sandbox
        // (fs/extraWrite/egress policy, container provider); every other
        // backend spawns unsandboxed and discards the requested enforcement.
        // Reject the pair at validation so a mission never runs with the
        // operator believing workers are contained when they are not.
        if role_cfg.sandbox.enforce != SandboxEnforce::Off && !kind.supports_sandbox_enforcement() {
            return Err(EngineError::Config(format!(
                "{name}.backend {:?} cannot honor sandbox.enforce={:?}: only the claude backend \
                 applies the resolved OS sandbox; run with sandbox.enforce=off to proceed \
                 unsandboxed, or use the claude backend",
                kind.as_str(),
                role_cfg.sandbox.enforce.as_str()
            )));
        }
        let effective = effective_model(role, kind, &role_cfg.model);
        let tier = model_tier(kind, &effective).ok_or_else(|| {
            EngineError::Config(format!(
                "{name} effective model {effective:?} (configured as {:?}) is not supported by backend {:?}",
                role_cfg.model,
                kind.as_str()
            ))
        })?;

        if kind == BackendKind::Local {
            // Guarded validator role split (ticket
            // `local-inference-validator-guarded`, KRZ-206b; review addendum
            // §4 of docs/scoping/local-inference-executor-tier.md): the local
            // validator tier exists for DETERMINISTIC mechanical checks only
            // — compile/test/lint exit codes and contract-command pass/fail,
            // where the engine runs the command itself and the model only
            // reads verbatim PASS/FAIL evidence. Scrutiny is judgment (diff
            // review against criteria), and routing judgment local is exactly
            // the "silent green" attack the split exists to prevent: a weak
            // local validator that wrongly PASSES bad work never looks like a
            // failure, so no escalation valve ever fires on it. Only the
            // functional role may pair with the local backend — and every
            // local functional PASS is frontier-confirmed before it greens a
            // gate (confirm-on-pass in the validation round); the scrutiny
            // role is rejected outright here. Checked FIRST, before the
            // endpoint fields, so the error names the real problem.
            if role == Role::ValidatorScrutiny {
                return Err(EngineError::Config(format!(
                    "{name}.backend \"local\" is rejected: scrutiny is judgment, not a \
                     deterministic mechanical check, and the local validator tier is the \
                     functional role only (KRZ-206b) — a local judgment PASS is the \
                     silent-green failure mode the guarded role split exists to prevent"
                )));
            }
            match role_cfg.base_url.as_deref() {
                Some(url) if !url.trim().is_empty() => {
                    if base_url_host(url).is_empty() {
                        return Err(EngineError::Config(format!(
                            "{name}.baseUrl {url:?} is not a valid http/https URL"
                        )));
                    }
                    // Loopback gate, mirroring hookStatus.endpoint: the
                    // engine POSTs the assembled system + user prompt to
                    // this URL from the engine process, OUTSIDE every
                    // sandbox, and takes the reply as the role's model
                    // output; the readiness probe connects to whatever
                    // host:port it names and records reachable/unreachable.
                    // A repo-named remote host is therefore prompt
                    // exfiltration plus a config-driven internal-network
                    // oracle. Operators who really do run a shared endpoint
                    // name its host in the global-layer allowlist.
                    let host = base_url_host(url);
                    if !host_is_loopback(host)
                        && !cfg
                            .local_backend_allowed_hosts
                            .iter()
                            .any(|allowed| allowed.trim().eq_ignore_ascii_case(host))
                    {
                        return Err(EngineError::Config(format!(
                            "{name}.baseUrl host {host:?} is not loopback: the engine POSTs \
                             the assembled prompt to it from outside every sandbox and takes \
                             the reply as model output. Use a loopback endpoint, or name the \
                             host in localBackendAllowedHosts in ~/.kranz/config.json (the \
                             global layer only)"
                        )));
                    }
                }
                _ => {
                    return Err(EngineError::Config(format!(
                        "{name}.baseUrl is required when {name}.backend is \"local\""
                    )));
                }
            }

            match role_cfg.context_budget {
                Some(budget) if (1024..=200_000).contains(&budget) => {}
                Some(budget) => {
                    return Err(EngineError::Config(format!(
                        "{name}.contextBudget must be in 1024..=200000, got {budget}"
                    )));
                }
                None => {
                    return Err(EngineError::Config(format!(
                        "{name}.contextBudget is required when {name}.backend is \"local\""
                    )));
                }
            }

            if let Some(temperature) = role_cfg.temperature {
                if !temperature.is_finite() || !(0.0..=2.0).contains(&temperature) {
                    return Err(EngineError::Config(format!(
                        "{name}.temperature must be finite and in 0.0..=2.0, got {temperature}"
                    )));
                }
            }
        }

        if kind == BackendKind::Acp {
            // KRZ-301 lands worker-first: the orchestrator needs
            // streaming-input + resume semantics this backend deliberately
            // rejects at the seam, and the validator roles wait for live
            // soak — refuse those pairings here rather than degrading
            // mid-mission.
            if role != Role::Worker {
                return Err(EngineError::Config(format!(
                    "{name}.backend \"acp\" is supported for the worker role only in this pass \
                     (KRZ-301); validators and the orchestrator stay on their existing backends"
                )));
            }
            match role_cfg.acp_command.as_deref() {
                Some(command) if !command.trim().is_empty() => {}
                _ => {
                    return Err(EngineError::Config(format!(
                        "{name}.acpCommand is required when {name}.backend is \"acp\" \
                         (the ACP agent executable; extra argv goes in {name}.acpArgs)"
                    )));
                }
            }
        }

        // Kimi is the first backend where reasoning effort is model-constrained:
        // k3 (the thinking-capable flagship) only supports low/high/max, while
        // kimi-for-coding[-highspeed] (not thinking-capable) impose no effort
        // constraint.
        if kind == BackendKind::Kimi
            && effective == DEFAULT_KIMI_MODEL
            && !["low", "high", "max"].contains(&role_cfg.reasoning_effort.as_str())
        {
            return Err(EngineError::Config(format!(
                "{name}.reasoningEffort must be one of [\"low\", \"high\", \"max\"] for kimi model {DEFAULT_KIMI_MODEL:?}, got {:?}",
                role_cfg.reasoning_effort
            )));
        }

        if role == Role::Worker
            && tier < ModelTier::Default
            && !cfg.allow_below_default_worker_model
        {
            return Err(EngineError::Config(format!(
                "worker effective model {effective:?} (configured as {:?}) on backend {:?} is below the default worker tier; set \
                 allowBelowDefaultWorkerModel=true on this mission to opt in",
                role_cfg.model,
                kind.as_str()
            )));
        }

        if role == Role::Orchestrator && tier < ModelTier::Frontier {
            return Err(EngineError::Config(format!(
                "orchestrator effective model {effective:?} (configured as {:?}) on backend {:?} is below the frontier-model floor",
                role_cfg.model,
                kind.as_str()
            )));
        }
    }

    Ok(())
}

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

    #[test]
    fn default_planning_idle_release_minutes_is_30() {
        assert_eq!(MissionConfig::default().planning_idle_release_minutes, 30);
    }

    #[test]
    fn default_serializes_camel_case_planning_idle_release_minutes() {
        let value = serde_json::to_value(MissionConfig::default()).unwrap();
        assert_eq!(value["planningIdleReleaseMinutes"], 30);
    }

    #[test]
    fn layer_overrides_planning_idle_release_minutes() {
        let dir = tempfile::tempdir().unwrap();
        let layer_path = dir.path().join("config.json");
        std::fs::write(&layer_path, r#"{"planningIdleReleaseMinutes": 5}"#).unwrap();

        let cfg = load_layers(&[layer_path]).unwrap();
        assert_eq!(cfg.planning_idle_release_minutes, 5);
    }

    #[test]
    fn absent_key_in_layer_keeps_default() {
        let dir = tempfile::tempdir().unwrap();
        let layer_path = dir.path().join("config.json");
        std::fs::write(&layer_path, r#"{"maxRespawns": 3}"#).unwrap();

        let cfg = load_layers(&[layer_path]).unwrap();
        assert_eq!(cfg.planning_idle_release_minutes, 30);
    }

    #[test]
    fn default_auto_work_is_false() {
        assert!(!MissionConfig::default().auto_work);
    }

    #[test]
    fn default_serializes_camel_case_auto_work() {
        let value = serde_json::to_value(MissionConfig::default()).unwrap();
        assert_eq!(value["autoWork"], false);
    }

    #[test]
    fn default_serializes_camel_case_considered_alternatives_thresholds() {
        let value = serde_json::to_value(MissionConfig::default()).unwrap();
        assert_eq!(value["consideredAlternativesFeatureThreshold"], 4);
        assert_eq!(value["consideredAlternativesTouchSetThreshold"], 4);
        assert_eq!(value["consideredAlternativesHighUsdThreshold"], 0.0);
    }

    #[test]
    fn layer_overrides_considered_alternatives_thresholds() {
        let dir = tempfile::tempdir().unwrap();
        let layer_path = dir.path().join("config.json");
        std::fs::write(
            &layer_path,
            r#"{
                "consideredAlternativesFeatureThreshold": 2,
                "consideredAlternativesTouchSetThreshold": 3,
                "consideredAlternativesHighUsdThreshold": 9.5
            }"#,
        )
        .unwrap();

        let cfg = load_layers(&[layer_path]).unwrap();
        assert_eq!(cfg.considered_alternatives_feature_threshold, 2);
        assert_eq!(cfg.considered_alternatives_touch_set_threshold, 3);
        assert_eq!(cfg.considered_alternatives_high_usd_threshold, 9.5);
    }

    #[test]
    fn default_serializes_camel_case_worker_floor_opt_in() {
        let value = serde_json::to_value(MissionConfig::default()).unwrap();
        assert_eq!(value["allowBelowDefaultWorkerModel"], false);
    }

    #[test]
    fn layer_overrides_auto_work() {
        let dir = tempfile::tempdir().unwrap();
        let layer_path = dir.path().join("config.json");
        std::fs::write(&layer_path, r#"{"autoWork": true}"#).unwrap();

        let cfg = load_layers(&[layer_path]).unwrap();
        assert!(cfg.auto_work);
    }

    /// The uncontained-validator degrade opt-in (ticket
    /// `validator-containment-degrade-fail-closed`): additive — absent (every
    /// pre-existing config and old `mission.created` payload) deserializes to
    /// the FAIL-CLOSED default; the explicit `true` opts back into the loud
    /// degrade.
    #[test]
    fn validator_allow_uncontained_degrade_defaults_off_and_parses_opt_in() {
        assert!(!MissionConfig::default().validator_allow_uncontained_degrade);
        let value = serde_json::to_value(MissionConfig::default()).unwrap();
        assert_eq!(value["validatorAllowUncontainedDegrade"], false);

        let dir = tempfile::tempdir().unwrap();
        let layer_path = dir.path().join("config.json");
        std::fs::write(&layer_path, r#"{"validatorAllowUncontainedDegrade": true}"#).unwrap();
        let cfg = load_layers(&[layer_path]).unwrap();
        assert!(cfg.validator_allow_uncontained_degrade);

        // A layer naming unrelated keys only (the old-config shape) keeps the
        // fail-closed default.
        let layer_path = dir.path().join("config-old.json");
        std::fs::write(&layer_path, r#"{"maxRespawns": 3}"#).unwrap();
        let cfg = load_layers(&[layer_path]).unwrap();
        assert!(!cfg.validator_allow_uncontained_degrade);
    }

    #[test]
    fn contract_env_passthrough_defaults_empty_and_parses_camel_case() {
        // Additive contract change: absent key (every pre-existing config and
        // every old mission.created event payload) deserializes to empty.
        assert!(MissionConfig::default().contract_env_passthrough.is_empty());
        let value = serde_json::to_value(MissionConfig::default()).unwrap();
        assert_eq!(value["contractEnvPassthrough"], serde_json::json!([]));

        let dir = tempfile::tempdir().unwrap();
        let layer_path = dir.path().join("config.json");
        std::fs::write(
            &layer_path,
            r#"{"contractEnvPassthrough": ["NPM_TOKEN", "REGISTRY_BASIC_AUTH"]}"#,
        )
        .unwrap();
        let cfg = load_layers(&[layer_path]).unwrap();
        assert_eq!(
            cfg.contract_env_passthrough,
            vec!["NPM_TOKEN".to_string(), "REGISTRY_BASIC_AUTH".to_string()]
        );
        // A layer naming unrelated keys only (the old-config shape) leaves
        // the passthrough empty.
        let layer_path = dir.path().join("config-old.json");
        std::fs::write(&layer_path, r#"{"maxRespawns": 3}"#).unwrap();
        let cfg = load_layers(&[layer_path]).unwrap();
        assert!(cfg.contract_env_passthrough.is_empty());
    }

    #[test]
    fn absent_auto_work_key_keeps_default() {
        let dir = tempfile::tempdir().unwrap();
        let layer_path = dir.path().join("config.json");
        std::fs::write(&layer_path, r#"{"maxRespawns": 3}"#).unwrap();

        let cfg = load_layers(&[layer_path]).unwrap();
        assert!(!cfg.auto_work);
    }

    /// Composition audit (ticket `config-fail-open-audit`): layered config
    /// arrays REPLACE wholesale (deep_merge semantics — a project layer
    /// overrides a global layer's list). That replace is safe ONLY because
    /// the deny floor is compiled in: `denyPatterns` from any layer can
    /// replace another layer's entries but can never strip the built-in
    /// worker deny list, which `permissions::for_role` appends to. This pins
    /// both halves of the contract: the documented replace semantics, and
    /// the floor's unreachability by replacement.
    #[test]
    fn composition_audit_layered_deny_patterns_replace_but_never_strip_the_builtin_floor() {
        let dir = tempfile::tempdir().unwrap();
        let global = dir.path().join("global.json");
        std::fs::write(&global, r#"{"denyPatterns": ["git push --force"]}"#).unwrap();
        let project = dir.path().join("project.json");
        std::fs::write(&project, r#"{"denyPatterns": ["rm -rf *"]}"#).unwrap();

        let cfg = load_layers(&[global, project]).unwrap();
        // Replace semantics across layers: the later list wins wholesale.
        assert_eq!(cfg.deny_patterns, vec!["rm -rf *".to_string()]);

        // The built-in §4.7 floor is compiled in, so no layer shape can
        // remove it: the worker profile carries every built-in rule plus
        // (only) the winning layer's custom entry.
        let profile = crate::permissions::for_role(Role::Worker, &cfg, &[], &[], &[]);
        for builtin in [
            "Bash(git push*)",
            "Bash(sudo*)",
            "Bash(curl*)",
            "WebFetch",
            "WebSearch",
        ] {
            assert!(
                profile.disallowed_tools.iter().any(|r| r == builtin),
                "the built-in deny {builtin} must survive layered replacement"
            );
        }
        assert!(profile
            .disallowed_tools
            .iter()
            .any(|r| r == "Bash(rm -rf *)"));
        assert!(!profile
            .disallowed_tools
            .iter()
            .any(|r| r == "Bash(git push --force*)"));
    }

    #[test]
    fn default_config_serializes_without_backend_field() {
        let value = serde_json::to_value(MissionConfig::default()).unwrap();
        for role in [
            "orchestrator",
            "worker",
            "validatorScrutiny",
            "validatorFunctional",
        ] {
            let obj = value[role].as_object().unwrap();
            assert!(
                !obj.contains_key("backend"),
                "{role} should not serialize a backend key by default"
            );
        }
    }

    #[test]
    fn validate_accepts_known_backends_for_each_role() {
        for role in [
            Role::Orchestrator,
            Role::Worker,
            Role::ValidatorScrutiny,
            Role::ValidatorFunctional,
        ] {
            for backend in [None, Some("claude"), Some("codex")] {
                let mut cfg = MissionConfig::default();
                cfg.role_mut_for_test(role).backend = backend.map(|s| s.to_string());
                assert!(
                    validate(&cfg).is_ok(),
                    "{role:?} backend {backend:?} should be accepted"
                );
            }
        }
    }

    #[test]
    fn validate_accepts_droid_scrutiny_backend_with_legacy_default_model() {
        let mut cfg = MissionConfig::default();
        cfg.validator_scrutiny.backend = Some("droid".into());
        assert!(validate(&cfg).is_ok());
        assert_eq!(
            effective_model(
                Role::ValidatorScrutiny,
                BackendKind::Droid,
                &cfg.validator_scrutiny.model
            ),
            DEFAULT_DROID_MODEL
        );
    }

    #[test]
    fn validate_rejects_unknown_backend_on_any_role() {
        for role in [
            Role::Orchestrator,
            Role::Worker,
            Role::ValidatorScrutiny,
            Role::ValidatorFunctional,
        ] {
            let mut cfg = MissionConfig::default();
            cfg.role_mut_for_test(role).backend = Some("gemini".into());
            assert!(validate(&cfg).is_err(), "{role:?} should reject gemini");
        }
    }

    #[test]
    fn validate_rejects_unknown_backend_model_combos() {
        let mut cfg = MissionConfig::default();
        cfg.worker.model = "kranz-test-model".into();
        assert!(validate(&cfg).is_err());

        let mut cfg = MissionConfig::default();
        cfg.validator_functional.backend = Some("codex".into());
        cfg.validator_functional.model = "claude-sonnet-5".into();
        assert!(validate(&cfg).is_err());

        let mut cfg = MissionConfig::default();
        cfg.validator_scrutiny.backend = Some("droid".into());
        cfg.validator_scrutiny.model = "gpt-5-codex".into();
        assert!(validate(&cfg).is_err());
    }

    #[test]
    fn validate_enforces_worker_floor_with_explicit_opt_in() {
        let mut cfg = MissionConfig::default();
        cfg.worker.model = "haiku".into();
        assert!(validate(&cfg).is_err());
        cfg.allow_below_default_worker_model = true;
        assert!(validate(&cfg).is_ok());

        let mut cfg = MissionConfig::default();
        cfg.worker.backend = Some("droid".into());
        assert!(
            validate(&cfg).is_err(),
            "droid's legacy default GLM worker is below the default tier"
        );
        cfg.allow_below_default_worker_model = true;
        assert!(validate(&cfg).is_ok());
    }

    #[test]
    fn floor_violations_lead_with_the_effective_model() {
        // A role that keeps its default model on a non-Claude backend runs
        // the backend default, not the configured name — floor messages must
        // lead with that effective model so a revert to naming only the
        // configured model cannot ship silently.
        let mut cfg = MissionConfig::default();
        cfg.worker.backend = Some("droid".into());
        let configured = cfg.worker.model.clone();
        let err = validate(&cfg).unwrap_err().to_string();
        assert!(
            err.contains(&format!("worker effective model {DEFAULT_DROID_MODEL:?}")),
            "{err}"
        );
        assert!(
            err.contains(&format!("(configured as {configured:?})")),
            "{err}"
        );

        let mut cfg = MissionConfig::default();
        cfg.orchestrator.backend = Some("droid".into());
        let configured = cfg.orchestrator.model.clone();
        let err = validate(&cfg).unwrap_err().to_string();
        assert!(
            err.contains(&format!(
                "orchestrator effective model {DEFAULT_DROID_MODEL:?}"
            )),
            "{err}"
        );
        assert!(
            err.contains(&format!("(configured as {configured:?})")),
            "{err}"
        );
    }

    #[test]
    fn validate_enforces_orchestrator_frontier_floor() {
        let mut cfg = MissionConfig::default();
        cfg.orchestrator.model = "sonnet".into();
        assert!(validate(&cfg).is_err());

        let mut cfg = MissionConfig::default();
        cfg.orchestrator.backend = Some("droid".into());
        assert!(
            validate(&cfg).is_err(),
            "droid's legacy default GLM model is not a planner frontier model"
        );

        let mut cfg = MissionConfig::default();
        cfg.orchestrator.backend = Some("droid".into());
        cfg.orchestrator.model = "claude-fable-5".into();
        assert!(validate(&cfg).is_ok());
    }

    #[test]
    fn validate_allows_scrutiny_on_any_supported_tier() {
        for (backend, model) in [
            (Some("claude"), "haiku"),
            (Some("claude"), "sonnet"),
            (Some("claude"), "opus"),
            (Some("codex"), DEFAULT_CODEX_MODEL),
            (Some("droid"), DEFAULT_DROID_MODEL),
            (Some("droid"), "claude-fable-5"),
            (Some("kimi"), DEFAULT_KIMI_MODEL),
            (Some("kimi"), "kimi-code/kimi-for-coding"),
        ] {
            let mut cfg = MissionConfig::default();
            cfg.validator_scrutiny.backend = backend.map(|s| s.to_string());
            cfg.validator_scrutiny.model = model.to_string();
            assert!(
                validate(&cfg).is_ok(),
                "scrutiny should accept {backend:?} / {model}"
            );
        }
    }

    #[test]
    fn validate_accepts_kimi_k3_for_supported_efforts() {
        for effort in ["low", "high", "max"] {
            let mut cfg = MissionConfig::default();
            cfg.validator_scrutiny.backend = Some("kimi".into());
            cfg.validator_scrutiny.model = DEFAULT_KIMI_MODEL.into();
            cfg.validator_scrutiny.reasoning_effort = effort.into();
            assert!(
                validate(&cfg).is_ok(),
                "kimi k3 should accept effort {effort}"
            );
        }
    }

    #[test]
    fn guarded_local_validator_scrutiny_cannot_be_configured_local() {
        // KRZ-206b: scrutiny is judgment; the local validator tier is the
        // functional role only. The rejection names the role, and fires
        // whether or not the endpoint fields are present (the role guard is
        // the real problem, never the missing baseUrl).
        let mut cfg = MissionConfig::default();
        cfg.validator_scrutiny.backend = Some("local".into());
        cfg.validator_scrutiny.base_url = Some("http://127.0.0.1:8080".into());
        cfg.validator_scrutiny.context_budget = Some(8192);
        let err = validate(&cfg).unwrap_err().to_string();
        assert!(
            err.contains("validatorScrutiny.backend \"local\" is rejected"),
            "the rejection must name the role: {err}"
        );

        let mut cfg = MissionConfig::default();
        cfg.validator_scrutiny.backend = Some("local".into());
        let err = validate(&cfg).unwrap_err().to_string();
        assert!(
            err.contains("validatorScrutiny.backend \"local\" is rejected"),
            "the role guard must fire before the endpoint checks: {err}"
        );
    }

    #[test]
    fn guarded_local_validator_functional_may_be_configured_local() {
        // KRZ-206b: the functional role may select the local backend for
        // deterministic mechanical checks (contract-command pass/fail); the
        // same endpoint requirements as any local-backed role apply, and
        // every local PASS is frontier-confirmed at the validation round.
        let mut cfg = MissionConfig::default();
        cfg.validator_functional.backend = Some("local".into());
        cfg.validator_functional.base_url = Some("http://127.0.0.1:8080".into());
        cfg.validator_functional.context_budget = Some(8192);
        assert!(
            validate(&cfg).is_ok(),
            "functional + local with a valid endpoint must be accepted"
        );

        // The endpoint fields stay required — a local functional validator
        // with nowhere to point is a config error, exactly as before.
        let mut cfg = MissionConfig::default();
        cfg.validator_functional.backend = Some("local".into());
        let err = validate(&cfg).unwrap_err().to_string();
        assert!(
            err.contains("validatorFunctional.baseUrl is required"),
            "endpoint requirements must still apply to the functional role: {err}"
        );
    }

    #[test]
    fn validate_rejects_kimi_k3_for_unsupported_efforts() {
        for effort in ["medium", "xhigh"] {
            let mut cfg = MissionConfig::default();
            cfg.validator_scrutiny.backend = Some("kimi".into());
            cfg.validator_scrutiny.model = DEFAULT_KIMI_MODEL.into();
            cfg.validator_scrutiny.reasoning_effort = effort.into();
            let err = validate(&cfg).unwrap_err().to_string();
            assert!(
                err.contains("reasoningEffort"),
                "kimi k3 should reject effort {effort}: {err}"
            );
        }
    }

    #[test]
    fn validate_kimi_for_coding_imposes_no_effort_constraint() {
        for effort in ["low", "medium", "high", "xhigh", "max"] {
            let mut cfg = MissionConfig::default();
            cfg.validator_scrutiny.backend = Some("kimi".into());
            cfg.validator_scrutiny.model = "kimi-code/kimi-for-coding".into();
            cfg.validator_scrutiny.reasoning_effort = effort.into();
            assert!(
                validate(&cfg).is_ok(),
                "kimi-for-coding should accept any effort, got {effort} err"
            );
        }
    }

    #[test]
    fn validate_rejects_unsupported_kimi_model() {
        let mut cfg = MissionConfig::default();
        cfg.validator_scrutiny.backend = Some("kimi".into());
        cfg.validator_scrutiny.model = "kimi-unknown-model".into();
        assert!(validate(&cfg).is_err());
    }

    #[test]
    fn sandbox_config_defaults_to_off() {
        let cfg = MissionConfig::default();
        for role in [
            &cfg.orchestrator,
            &cfg.worker,
            &cfg.validator_scrutiny,
            &cfg.validator_functional,
        ] {
            assert_eq!(role.sandbox.enforce, crate::types::SandboxEnforce::Off);
            assert!(role.sandbox.extra_write.is_empty());
            assert!(role.sandbox.egress.is_empty());
        }
        assert!(validate(&cfg).is_ok());
    }

    #[test]
    fn sandbox_config_parses_fs() {
        let dir = tempfile::tempdir().unwrap();
        let layer_path = dir.path().join("config.json");
        std::fs::write(
            &layer_path,
            r#"{"worker":{"sandbox":{"enforce":"fs","extraWrite":["~/.cargo"]}}}"#,
        )
        .unwrap();

        let cfg = load_layers(&[layer_path]).unwrap();
        assert_eq!(cfg.worker.sandbox.enforce, crate::types::SandboxEnforce::Fs);
        assert_eq!(cfg.worker.sandbox.extra_write, vec!["~/.cargo".to_string()]);
        // Other roles remain untouched by the partial patch.
        assert_eq!(
            cfg.orchestrator.sandbox.enforce,
            crate::types::SandboxEnforce::Off
        );
    }

    #[test]
    fn sandbox_config_extra_write_roundtrips() {
        let mut cfg = MissionConfig::default();
        cfg.worker.sandbox.enforce = crate::types::SandboxEnforce::FsNet;
        cfg.worker.sandbox.extra_write = vec!["~/.cargo".into(), "~/.npm".into()];
        cfg.worker.sandbox.egress = vec!["registry.npmjs.org:443".into()];

        let value = serde_json::to_value(&cfg).unwrap();
        assert_eq!(value["worker"]["sandbox"]["enforce"], "fs+net");
        assert_eq!(
            value["worker"]["sandbox"]["extraWrite"],
            serde_json::json!(["~/.cargo", "~/.npm"])
        );
        assert_eq!(
            value["worker"]["sandbox"]["egress"],
            serde_json::json!(["registry.npmjs.org:443"])
        );

        let roundtripped: MissionConfig = serde_json::from_value(value).unwrap();
        assert_eq!(roundtripped, cfg);
    }

    #[test]
    fn sandbox_config_parses_fs_plus_net() {
        let dir = tempfile::tempdir().unwrap();
        let layer_path = dir.path().join("config.json");
        std::fs::write(
            &layer_path,
            r#"{"worker":{"sandbox":{"enforce":"fs+net","egress":["crates.io:443"]}}}"#,
        )
        .unwrap();

        let cfg = load_layers(&[layer_path]).unwrap();
        assert_eq!(
            cfg.worker.sandbox.enforce,
            crate::types::SandboxEnforce::FsNet
        );
        assert_eq!(cfg.worker.sandbox.egress, vec!["crates.io:443"]);
    }

    #[test]
    fn only_claude_declares_sandbox_enforcement_support() {
        assert!(BackendKind::Claude.supports_sandbox_enforcement());
        for kind in [
            BackendKind::Codex,
            BackendKind::Droid,
            BackendKind::Kimi,
            BackendKind::Local,
            BackendKind::Cursor,
        ] {
            assert!(
                !kind.supports_sandbox_enforcement(),
                "{kind:?} must not claim sandbox enforcement support"
            );
        }
    }

    #[test]
    fn validate_rejects_enforced_sandbox_on_non_claude_backends() {
        for backend in ["codex", "droid", "kimi", "cursor"] {
            for enforce in [
                crate::types::SandboxEnforce::Fs,
                crate::types::SandboxEnforce::FsNet,
            ] {
                let mut cfg = MissionConfig::default();
                cfg.validator_scrutiny.backend = Some(backend.into());
                cfg.validator_scrutiny.sandbox.enforce = enforce;
                let err = validate(&cfg).unwrap_err().to_string();
                // The error must name the backend, the requested enforce
                // mode, and the remedy.
                assert!(err.contains("validatorScrutiny"), "{err}");
                assert!(err.contains(backend), "{err}");
                assert!(err.contains(enforce.as_str()), "{err}");
                assert!(err.contains("sandbox.enforce=off"), "{err}");
                assert!(err.contains("claude"), "{err}");
            }
        }
    }

    #[test]
    fn validate_rejects_enforced_sandbox_on_codex_worker() {
        let mut cfg = MissionConfig::default();
        cfg.worker.backend = Some("codex".into());
        cfg.worker.sandbox.enforce = crate::types::SandboxEnforce::Fs;
        let err = validate(&cfg).unwrap_err().to_string();
        assert!(err.contains("worker.backend"), "{err}");
        assert!(err.contains("codex"), "{err}");
        assert!(err.contains("sandbox.enforce=off"), "{err}");
    }

    #[test]
    fn validate_rejects_enforced_sandbox_on_local_backend() {
        // The local backend makes its HTTP call in the engine process — no
        // child to wrap — so an enforced sandbox would be silently ignored.
        let mut cfg = local_worker_cfg();
        cfg.worker.sandbox.enforce = crate::types::SandboxEnforce::FsNet;
        let err = validate(&cfg).unwrap_err().to_string();
        assert!(err.contains("local"), "{err}");
        assert!(err.contains("fs+net"), "{err}");
    }

    #[test]
    fn validate_rejects_container_provider_on_non_claude_backend() {
        // `provider = "container"` with an enforced mode is still an enforced
        // sandbox the backend cannot honor.
        let mut cfg = MissionConfig::default();
        cfg.validator_scrutiny.backend = Some("droid".into());
        cfg.validator_scrutiny.sandbox.enforce = crate::types::SandboxEnforce::Fs;
        cfg.validator_scrutiny.sandbox.provider = crate::types::SandboxProvider::Container;
        assert!(validate(&cfg).is_err());
    }

    #[test]
    fn container_net_boundary_is_hard_only_for_process_or_empty_egress() {
        // The process provider's fs+net boundary is the OS profile itself
        // (Seatbelt loopback-only on macOS, bwrap --unshare-net on Linux), so
        // the proxy hop is the only reachable way out regardless of the list.
        assert!(crate::types::SandboxProvider::Process
            .enforces_hard_net_boundary(&["crates.io:443".to_string()]));
        // This static helper remains false for container + non-empty egress
        // because only session runtime provisioning supplies that boundary;
        // engine-run gates use the helper to keep refusing the pair.
        assert!(crate::types::SandboxProvider::Container.enforces_hard_net_boundary(&[]));
        assert!(!crate::types::SandboxProvider::Container
            .enforces_hard_net_boundary(&["crates.io:443".to_string()]));
    }

    #[test]
    fn validate_accepts_container_fs_net_with_egress_list_for_runtime_resolution() {
        // The role config can now request the hard internal-network relay.
        // Runtime resolution still fails closed unless Docker is available.
        let mut cfg = MissionConfig::default();
        cfg.worker.sandbox.enforce = crate::types::SandboxEnforce::FsNet;
        cfg.worker.sandbox.provider = crate::types::SandboxProvider::Container;
        cfg.worker.sandbox.egress = vec!["crates.io:443".into()];
        assert!(validate(&cfg).is_ok());
    }

    #[test]
    fn validate_accepts_container_fs_and_container_fs_net_with_empty_egress() {
        // `fs` claims no network enforcement at all, and fs+net with an empty
        // egress list maps to the hard `--network none` boundary — both
        // honest postures for the container provider.
        for enforce in [
            crate::types::SandboxEnforce::Fs,
            crate::types::SandboxEnforce::FsNet,
        ] {
            let mut cfg = MissionConfig::default();
            cfg.worker.sandbox.enforce = enforce;
            cfg.worker.sandbox.provider = crate::types::SandboxProvider::Container;
            assert!(
                validate(&cfg).is_ok(),
                "container provider with sandbox.enforce={} and an empty egress list must validate",
                enforce.as_str()
            );
        }
    }

    #[test]
    fn validate_accepts_process_fs_net_with_egress_list() {
        // The process provider keeps its kernel boundary (Seatbelt loopback /
        // bwrap --unshare-net) regardless of the egress list — unchanged.
        let mut cfg = MissionConfig::default();
        cfg.worker.sandbox.enforce = crate::types::SandboxEnforce::FsNet;
        cfg.worker.sandbox.egress = vec!["crates.io:443".into()];
        assert!(
            validate(&cfg).is_ok(),
            "process provider fs+net with an egress list must still validate"
        );
    }

    #[test]
    fn validate_accepts_enforced_sandbox_on_claude_backend() {
        for backend in [None, Some("claude")] {
            for enforce in [
                crate::types::SandboxEnforce::Fs,
                crate::types::SandboxEnforce::FsNet,
            ] {
                let mut cfg = MissionConfig::default();
                cfg.worker.backend = backend.map(|s| s.to_string());
                cfg.worker.sandbox.enforce = enforce;
                assert!(
                    validate(&cfg).is_ok(),
                    "claude worker with sandbox.enforce={} must validate",
                    enforce.as_str()
                );
            }
        }
    }

    #[test]
    fn validate_accepts_sandbox_off_on_every_backend() {
        for backend in ["codex", "droid", "kimi", "cursor"] {
            let mut cfg = MissionConfig::default();
            cfg.validator_scrutiny.backend = Some(backend.into());
            assert_eq!(
                cfg.validator_scrutiny.sandbox.enforce,
                crate::types::SandboxEnforce::Off
            );
            assert!(
                validate(&cfg).is_ok(),
                "{backend} with sandbox.enforce=off must validate"
            );
        }
        assert!(
            validate(&local_worker_cfg()).is_ok(),
            "local with sandbox.enforce=off must validate"
        );
    }

    fn local_role_cfg() -> crate::types::RoleConfig {
        crate::types::RoleConfig {
            backend: Some("local".into()),
            model: "my-local-model".into(),
            base_url: Some("http://localhost:8080".into()),
            context_budget: Some(8192),
            ..MissionConfig::default().worker
        }
    }

    fn local_worker_cfg() -> MissionConfig {
        MissionConfig {
            worker: local_role_cfg(),
            allow_below_default_worker_model: true,
            ..MissionConfig::default()
        }
    }

    /// ACP (KRZ-301): the worker-role-only backend wiring — parse, role
    /// restriction, required command, model-tier opt-in.
    fn acp_worker_cfg() -> MissionConfig {
        let mut cfg = MissionConfig {
            allow_below_default_worker_model: true,
            ..MissionConfig::default()
        };
        cfg.worker.backend = Some("acp".into());
        cfg.worker.acp_command = Some("/opt/bin/my-acp-agent".into());
        cfg.worker.acp_args = vec!["--serve".into()];
        cfg
    }

    #[test]
    fn backend_acp_config_round_trips_and_parses() {
        assert_eq!(parse_backend(Some("acp")), Ok(BackendKind::Acp));
        let cfg = acp_worker_cfg();
        assert_eq!(cfg.backend_kind(Role::Worker), BackendKind::Acp);
        assert_eq!(BackendKind::Acp.as_str(), "acp");
        assert!(!BackendKind::Acp.supports_sandbox_enforcement());
        assert!(!BackendKind::Acp.reports_cache_read_tokens());
        assert!(!BackendKind::Acp.reports_cache_write_tokens());
        assert!(
            validate(&cfg).is_ok(),
            "worker + acpCommand + the below-default opt-in must validate"
        );
    }

    #[test]
    fn backend_acp_config_requires_worker_role_and_command() {
        // Validators and the orchestrator are refused (KRZ-301 lands
        // worker-first).
        for role in [
            Role::Orchestrator,
            Role::ValidatorScrutiny,
            Role::ValidatorFunctional,
        ] {
            let mut cfg = acp_worker_cfg();
            let role_cfg = match role {
                Role::Orchestrator => &mut cfg.orchestrator,
                Role::ValidatorScrutiny => &mut cfg.validator_scrutiny,
                Role::ValidatorFunctional => &mut cfg.validator_functional,
                Role::Worker => unreachable!("loop excludes the worker"),
            };
            role_cfg.backend = Some("acp".into());
            role_cfg.acp_command = Some("/opt/bin/my-acp-agent".into());
            let err = validate(&cfg).expect_err("non-worker acp must be refused");
            assert!(
                err.to_string().contains("worker role only"),
                "refusal must name the role restriction: {err}"
            );
        }

        // The command is required (and a blank one is as good as absent).
        let mut cfg = acp_worker_cfg();
        cfg.worker.acp_command = None;
        let err = validate(&cfg).expect_err("missing acpCommand must be refused");
        assert!(err.to_string().contains("acpCommand"), "{err}");
        cfg.worker.acp_command = Some("   ".into());
        assert!(validate(&cfg).is_err(), "blank acpCommand must be refused");

        // ACP model ids are free-form → uniformly below-default → the
        // worker needs the explicit opt-in, same as local.
        let mut cfg = acp_worker_cfg();
        cfg.allow_below_default_worker_model = false;
        let err = validate(&cfg).expect_err("below-default acp worker needs the opt-in");
        assert!(
            err.to_string().contains("allowBelowDefaultWorkerModel"),
            "{err}"
        );
    }

    #[test]
    fn local_config_requires_base_url() {
        let mut cfg = local_worker_cfg();

        cfg.worker.base_url = None;
        assert!(
            validate(&cfg).is_err(),
            "missing baseUrl should be rejected"
        );

        cfg.worker.base_url = Some("not a url".into());
        assert!(
            validate(&cfg).is_err(),
            "unparseable baseUrl should be rejected"
        );

        cfg.worker.base_url = Some("http://localhost:8080".into());
        assert!(
            validate(&cfg).is_ok(),
            "valid http baseUrl should be accepted"
        );

        // A well-formed https URL at a NON-loopback host is refused unless
        // the operator allowlisted the host (audit 2026-09-01, MEDIUM
        // baseUrl): the engine POSTs the assembled prompt there from outside
        // every sandbox.
        cfg.worker.base_url = Some("https://models.internal/v1".into());
        assert!(
            validate(&cfg).is_err(),
            "a remote baseUrl needs the operator's allowlist"
        );
        cfg.local_backend_allowed_hosts = vec!["models.internal".into()];
        assert!(
            validate(&cfg).is_ok(),
            "valid https baseUrl at an allowlisted host should be accepted"
        );
        cfg.local_backend_allowed_hosts.clear();

        cfg.worker.base_url = Some("http://127.0.0.1".into());
        assert!(
            validate(&cfg).is_ok(),
            "bare ip host baseUrl should be accepted"
        );

        cfg.worker.base_url = Some("http://:8080".into());
        assert!(
            validate(&cfg).is_err(),
            "host-less authority with port should be rejected"
        );

        cfg.worker.base_url = Some("http://@".into());
        assert!(
            validate(&cfg).is_err(),
            "userinfo-only authority should be rejected"
        );

        cfg.worker.base_url = Some("http://@:8080".into());
        assert!(
            validate(&cfg).is_err(),
            "userinfo with port and no host should be rejected"
        );
    }

    #[test]
    fn local_config_requires_context_budget_in_range() {
        let mut cfg = local_worker_cfg();

        cfg.worker.context_budget = Some(1023);
        assert!(validate(&cfg).is_err(), "1023 is below the floor");

        cfg.worker.context_budget = Some(200_001);
        assert!(validate(&cfg).is_err(), "200001 is above the ceiling");

        cfg.worker.context_budget = None;
        assert!(validate(&cfg).is_err(), "missing contextBudget is rejected");

        cfg.worker.context_budget = Some(8192);
        assert!(validate(&cfg).is_ok(), "8192 is in range");
    }

    #[test]
    fn local_config_rejects_out_of_range_temperature() {
        let mut cfg = local_worker_cfg();

        cfg.worker.temperature = Some(2.1);
        assert!(validate(&cfg).is_err(), "2.1 is above the ceiling");

        cfg.worker.temperature = Some(-0.1);
        assert!(validate(&cfg).is_err(), "-0.1 is below the floor");

        cfg.worker.temperature = Some(0.7);
        assert!(validate(&cfg).is_ok(), "0.7 is in range");

        cfg.worker.temperature = None;
        assert!(validate(&cfg).is_ok(), "absent temperature is fine");
    }

    #[test]
    fn local_config_worker_below_default_needs_optin() {
        let mut cfg = local_worker_cfg();
        cfg.allow_below_default_worker_model = false;
        assert!(
            validate(&cfg).is_err(),
            "local worker below-default tier requires opt-in"
        );

        cfg.allow_below_default_worker_model = true;
        assert!(
            validate(&cfg).is_ok(),
            "local worker accepted once opted in"
        );
    }

    #[test]
    fn local_config_orchestrator_local_always_rejected() {
        let cfg = MissionConfig {
            orchestrator: local_role_cfg(),
            allow_below_default_worker_model: true,
            ..MissionConfig::default()
        };
        assert!(
            validate(&cfg).is_err(),
            "local orchestrator always fails the frontier floor"
        );
    }

    #[test]
    fn local_config_model_tier_below_default_for_any_nonempty() {
        assert_eq!(
            model_tier(BackendKind::Local, "any-model-id"),
            Some(ModelTier::BelowDefault)
        );
        assert_eq!(model_tier(BackendKind::Local, ""), None);
        assert_eq!(model_tier(BackendKind::Local, "   "), None);
    }

    #[test]
    fn local_config_effective_model_passes_through_verbatim_and_never_panics() {
        assert_eq!(
            effective_model(Role::Worker, BackendKind::Local, "my-local-model"),
            "my-local-model"
        );
        // Even if the configured string happens to equal the Claude role
        // default, Local has no backend default to rewrite to.
        assert_eq!(
            effective_model(Role::Worker, BackendKind::Local, "sonnet"),
            "sonnet"
        );
    }

    #[test]
    fn task_class_routing_maps_execution_class_to_local() {
        assert_eq!(
            task_class_to_tier(Some("execution-class")),
            ExecutorTier::Local
        );
    }

    #[test]
    fn task_class_routing_defaults_to_frontier() {
        assert_eq!(
            task_class_to_tier(Some("planning-class")),
            ExecutorTier::Frontier
        );
        assert_eq!(
            task_class_to_tier(Some("some-arbitrary-value")),
            ExecutorTier::Frontier
        );
        assert_eq!(task_class_to_tier(None), ExecutorTier::Frontier);
    }

    #[test]
    fn task_class_routing_is_case_and_whitespace_insensitive() {
        assert_eq!(
            task_class_to_tier(Some("  Execution-Class ")),
            ExecutorTier::Local
        );
    }

    fn test_local_endpoint() -> LocalEndpoint {
        LocalEndpoint {
            base_url: "http://127.0.0.1:8080".to_string(),
            context_budget: 16_384,
            temperature: Some(0.2),
        }
    }

    #[test]
    fn executor_routing_applies_local_backend_when_execution_class_and_endpoint_configured() {
        let mut cfg = MissionConfig::default();
        let validator_scrutiny_before = cfg.validator_scrutiny.clone();
        let validator_functional_before = cfg.validator_functional.clone();
        let endpoint = test_local_endpoint();

        let applied = apply_executor_routing(&mut cfg, ExecutorTier::Local, Some(&endpoint));

        assert_eq!(applied, ExecutorTier::Local);
        assert_eq!(cfg.worker.backend.as_deref(), Some("local"));
        assert_eq!(
            cfg.worker.base_url.as_deref(),
            Some(endpoint.base_url.as_str())
        );
        assert_eq!(cfg.worker.context_budget, Some(endpoint.context_budget));
        assert_eq!(cfg.worker.temperature, endpoint.temperature);
        assert!(cfg.allow_below_default_worker_model);
        assert_eq!(cfg.validator_scrutiny, validator_scrutiny_before);
        assert_eq!(cfg.validator_functional, validator_functional_before);
    }

    #[test]
    fn executor_routing_applies_fail_safe_frontier_when_no_endpoint_configured() {
        let mut cfg = MissionConfig::default();
        let worker_backend_before = cfg.worker.backend.clone();

        let applied = apply_executor_routing(&mut cfg, ExecutorTier::Local, None);

        assert_eq!(applied, ExecutorTier::Frontier);
        assert_eq!(cfg.worker.backend, worker_backend_before);
        assert!(!cfg.allow_below_default_worker_model);
    }

    #[test]
    fn executor_routing_applies_no_change_for_frontier_tier() {
        let mut cfg = MissionConfig::default();
        let before = cfg.clone();
        let endpoint = test_local_endpoint();

        let applied = apply_executor_routing(&mut cfg, ExecutorTier::Frontier, Some(&endpoint));

        assert_eq!(applied, ExecutorTier::Frontier);
        assert_eq!(cfg, before);
    }

    #[test]
    fn route_task_class_executor_routes_local_when_endpoint_configured() {
        // Mirrors what `MissionEngine::create` calls with the class recovered
        // from a folded goal string (f-1-2: this is the single engine-side
        // wiring point every seed path — draft, exec, REST, Slack — shares).
        let mut cfg = MissionConfig::default();
        cfg.worker.base_url = Some("http://127.0.0.1:8080".to_string());
        cfg.worker.context_budget = Some(16_384);

        let (applied, summary) = route_task_class_executor(&mut cfg, Some("execution-class"));

        assert_eq!(applied, ExecutorTier::Local);
        assert_eq!(cfg.worker.backend.as_deref(), Some("local"));
        assert_eq!(summary, "executor routed local (execution-class)");
    }

    #[test]
    fn route_task_class_executor_stays_frontier_without_endpoint() {
        let mut cfg = MissionConfig::default();
        let (applied, summary) = route_task_class_executor(&mut cfg, Some("execution-class"));

        assert_eq!(applied, ExecutorTier::Frontier);
        assert_eq!(cfg.worker.backend, None);
        assert!(summary.contains("no local endpoint configured"));
    }

    #[test]
    fn route_task_class_executor_stays_frontier_for_non_execution_class() {
        let mut cfg = MissionConfig::default();
        cfg.worker.base_url = Some("http://127.0.0.1:8080".to_string());
        cfg.worker.context_budget = Some(16_384);

        let (applied, summary) = route_task_class_executor(&mut cfg, None);

        assert_eq!(applied, ExecutorTier::Frontier);
        assert_eq!(cfg.worker.backend, None);
        assert_eq!(summary, "executor stays frontier");
    }

    #[test]
    fn validator_stays_frontier_after_local_executor_routing() {
        let mut cfg = MissionConfig::default();
        let endpoint = test_local_endpoint();

        apply_executor_routing(&mut cfg, ExecutorTier::Local, Some(&endpoint));

        assert_ne!(cfg.validator_scrutiny.backend.as_deref(), Some("local"));
        assert_ne!(cfg.validator_functional.backend.as_deref(), Some("local"));
    }

    // -----------------------------------------------------------------------
    // Backend routing table (ticket backend-routing-abstraction, KRZ-331)
    // -----------------------------------------------------------------------

    use crate::types::TaskClassRoute;

    fn routing_table(rules: &[(&str, ExecutorTier)]) -> Vec<TaskClassRoute> {
        rules
            .iter()
            .map(|(task_class, tier)| TaskClassRoute {
                task_class: task_class.to_string(),
                tier: *tier,
            })
            .collect()
    }

    #[test]
    fn routing_abstraction_table_defaults_empty_and_parses_camel_case() {
        // Additive contract change: an absent key (every pre-existing config
        // and every old mission.created event payload) deserializes to the
        // EMPTY table — the byte-identical literal floor.
        assert!(MissionConfig::default().routing.task_class_rules.is_empty());
        let value = serde_json::to_value(MissionConfig::default()).unwrap();
        assert_eq!(value["routing"]["taskClassRules"], serde_json::json!([]));

        let dir = tempfile::tempdir().unwrap();
        let layer_path = dir.path().join("config.json");
        std::fs::write(
            &layer_path,
            r#"{"routing": {"taskClassRules": [{"taskClass": "execution-class", "tier": "local"}, {"taskClass": "docs-class", "tier": "frontier"}]}}"#,
        )
        .unwrap();
        let cfg = load_layers(&[layer_path]).unwrap();
        assert_eq!(cfg.routing.task_class_rules.len(), 2);
        assert_eq!(
            cfg.routing.task_class_rules[0].task_class,
            "execution-class"
        );
        assert_eq!(cfg.routing.task_class_rules[0].tier, ExecutorTier::Local);
        assert_eq!(cfg.routing.task_class_rules[1].tier, ExecutorTier::Frontier);

        // A layer naming unrelated keys only (the old-config shape) leaves
        // the table empty.
        let layer_path = dir.path().join("config-old.json");
        std::fs::write(&layer_path, r#"{"maxRespawns": 3}"#).unwrap();
        let cfg = load_layers(&[layer_path]).unwrap();
        assert!(cfg.routing.task_class_rules.is_empty());
    }

    #[test]
    fn routing_abstraction_unconfigured_table_keeps_byte_identical_floor() {
        // The regression pin: with NO table configured, routing a task class
        // must produce exactly the pre-table behavior — the literal floor
        // (`task_class_to_tier`) fed through `apply_executor_routing` —
        // including the applied config edits, for every input shape.
        for task_class in [
            None,
            Some("execution-class"),
            Some("  Execution-Class "),
            Some("planning-class"),
            Some("some-arbitrary-value"),
        ] {
            for endpoint_configured in [false, true] {
                let wire = |cfg: &mut MissionConfig| {
                    if endpoint_configured {
                        cfg.worker.base_url = Some("http://127.0.0.1:8080".to_string());
                        cfg.worker.context_budget = Some(16_384);
                    }
                };
                let mut cfg = MissionConfig::default();
                wire(&mut cfg);
                assert!(cfg.routing.task_class_rules.is_empty());
                let (applied, _) = route_task_class_executor(&mut cfg, task_class);

                // The pre-table reference computation.
                let mut reference = MissionConfig::default();
                wire(&mut reference);
                let endpoint = match (&reference.worker.base_url, reference.worker.context_budget) {
                    (Some(base_url), Some(context_budget)) => Some(LocalEndpoint {
                        base_url: base_url.clone(),
                        context_budget,
                        temperature: reference.worker.temperature,
                    }),
                    _ => None,
                };
                let expected = apply_executor_routing(
                    &mut reference,
                    task_class_to_tier(task_class),
                    endpoint.as_ref(),
                );

                assert_eq!(applied, expected, "task class {task_class:?}");
                assert_eq!(
                    cfg, reference,
                    "an empty table must apply byte-identical config changes for {task_class:?}"
                );
            }
        }
    }

    #[test]
    fn routing_abstraction_table_routes_configured_class_to_local() {
        // A configured table is the complete floor: it routes the classes it
        // names — beyond the literal floor's single hardcoded class...
        let mut cfg = MissionConfig::default();
        cfg.routing.task_class_rules = routing_table(&[("docs-class", ExecutorTier::Local)]);
        cfg.worker.base_url = Some("http://127.0.0.1:8080".to_string());
        cfg.worker.context_budget = Some(16_384);

        let (applied, summary) = route_task_class_executor(&mut cfg, Some("docs-class"));

        assert_eq!(applied, ExecutorTier::Local);
        assert_eq!(cfg.worker.backend.as_deref(), Some("local"));
        assert_eq!(summary, "executor routed local (routing-table rule)");

        // ...and the literal floor's own class stays frontier when the table
        // does not name it (the table replaces the literal map, it does not
        // amend it).
        let mut cfg = MissionConfig::default();
        cfg.routing.task_class_rules = routing_table(&[("docs-class", ExecutorTier::Local)]);
        cfg.worker.base_url = Some("http://127.0.0.1:8080".to_string());
        cfg.worker.context_budget = Some(16_384);

        let (applied, summary) = route_task_class_executor(&mut cfg, Some("execution-class"));

        assert_eq!(applied, ExecutorTier::Frontier);
        assert_eq!(cfg.worker.backend, None);
        assert_eq!(summary, "executor stays frontier");
    }

    #[test]
    fn routing_abstraction_table_local_route_fails_safe_without_endpoint() {
        // The pre-table fail-safe is unchanged under a table: a local route
        // with no configured endpoint stays frontier rather than routing to
        // an endpoint that doesn't exist.
        let mut cfg = MissionConfig::default();
        cfg.routing.task_class_rules = routing_table(&[("execution-class", ExecutorTier::Local)]);

        let (applied, summary) = route_task_class_executor(&mut cfg, Some("execution-class"));

        assert_eq!(applied, ExecutorTier::Frontier);
        assert_eq!(cfg.worker.backend, None);
        assert!(
            summary.contains("no local endpoint configured"),
            "{summary}"
        );
    }

    #[test]
    fn routing_abstraction_validate_fails_closed_on_malformed_table() {
        // Duplicate after normalization: refused, naming the rule (a
        // shadowed rule is dead config under first-match-wins).
        let mut cfg = MissionConfig::default();
        cfg.routing.task_class_rules = routing_table(&[
            ("execution-class", ExecutorTier::Local),
            (" Execution-Class", ExecutorTier::Frontier),
        ]);
        let err = validate(&cfg).unwrap_err().to_string();
        assert!(err.contains("routing.taskClassRules[1].taskClass"), "{err}");
        assert!(err.contains("duplicates rule 0"), "{err}");

        // Blank class: refused (it could never match honestly).
        let mut cfg = MissionConfig::default();
        cfg.routing.task_class_rules = routing_table(&[("   ", ExecutorTier::Local)]);
        let err = validate(&cfg).unwrap_err().to_string();
        assert!(err.contains("routing.taskClassRules[0].taskClass"), "{err}");

        // A clean table validates.
        let mut cfg = MissionConfig::default();
        cfg.routing.task_class_rules = routing_table(&[
            ("execution-class", ExecutorTier::Local),
            ("docs-class", ExecutorTier::Frontier),
        ]);
        assert!(validate(&cfg).is_ok(), "a clean table must validate");
    }

    #[test]
    fn routing_abstraction_hosted_fine_tune_is_plain_local_endpoint_config() {
        // KRZ-331: a hosted fine-tune is configuration of the
        // OpenAI-compatible local backend (baseUrl + model), NOT a new
        // backend kind — an https endpoint carrying a free-form
        // fine-tune-shaped model id validates exactly like a localhost one,
        // and a table can route a task class to it by capability class.
        let mut cfg = local_worker_cfg();
        cfg.worker.base_url = Some("https://models.internal.example/v1".into());
        cfg.worker.model = "ft:some-model:some-org:some-id".into();
        // A hosted (non-loopback) endpoint now needs the operator's
        // global-layer host allowlist — the model id is still free-form, and
        // the routing behavior below is unchanged.
        cfg.local_backend_allowed_hosts = vec!["models.internal.example".into()];
        assert!(
            validate(&cfg).is_ok(),
            "a hosted fine-tune endpoint is ordinary local-backend config"
        );

        cfg.routing.task_class_rules = routing_table(&[("execution-class", ExecutorTier::Local)]);
        let (applied, _) = route_task_class_executor(&mut cfg, Some("execution-class"));
        assert_eq!(applied, ExecutorTier::Local);
        assert_eq!(cfg.worker.backend.as_deref(), Some("local"));
    }

    // -----------------------------------------------------------------------
    // Heterogeneous dispatch pool (ticket heterogeneous-dispatch-pool, KRZ-303)
    // -----------------------------------------------------------------------

    use crate::types::CandidateSpec;

    fn dispatch_pool_pair() -> Vec<CandidateSpec> {
        vec![
            CandidateSpec {
                backend: "claude".into(),
                model: "sonnet".into(),
            },
            CandidateSpec {
                backend: "codex".into(),
                model: DEFAULT_CODEX_MODEL.into(),
            },
        ]
    }

    #[test]
    fn dispatch_pool_defaults_empty_and_parses_camel_case() {
        // Additive contract change: absent key (every pre-existing config and
        // every old mission.created event payload) deserializes to empty —
        // today's single-backend behavior exactly.
        assert!(MissionConfig::default().worker_candidates.is_empty());
        let value = serde_json::to_value(MissionConfig::default()).unwrap();
        assert_eq!(value["workerCandidates"], serde_json::json!([]));

        let dir = tempfile::tempdir().unwrap();
        let layer_path = dir.path().join("config.json");
        std::fs::write(
            &layer_path,
            r#"{"workerCandidates": [{"backend": "claude", "model": "sonnet"}, {"backend": "codex", "model": "gpt-5.6-sol"}]}"#,
        )
        .unwrap();
        let cfg = load_layers(&[layer_path]).unwrap();
        assert_eq!(cfg.worker_candidates, dispatch_pool_pair());

        // A layer naming unrelated keys only (the old-config shape) leaves
        // the pool empty.
        let layer_path = dir.path().join("config-old.json");
        std::fs::write(&layer_path, r#"{"maxRespawns": 3}"#).unwrap();
        let cfg = load_layers(&[layer_path]).unwrap();
        assert!(cfg.worker_candidates.is_empty());
    }

    #[test]
    fn dispatch_pool_validate_accepts_heterogeneous_pair() {
        let cfg = MissionConfig {
            worker_candidates: dispatch_pool_pair(),
            ..MissionConfig::default()
        };
        validate(&cfg).unwrap();
    }

    #[test]
    fn dispatch_pool_validate_rejects_single_entry() {
        let cfg = MissionConfig {
            worker_candidates: vec![CandidateSpec {
                backend: "claude".into(),
                model: "sonnet".into(),
            }],
            ..MissionConfig::default()
        };
        let err = validate(&cfg).unwrap_err().to_string();
        assert!(
            err.contains("workerCandidates with exactly one entry"),
            "{err}"
        );
    }

    #[test]
    fn dispatch_pool_validate_rejects_local_and_acp_candidates() {
        for backend in ["local", "acp"] {
            let cfg = MissionConfig {
                worker_candidates: vec![
                    CandidateSpec {
                        backend: "claude".into(),
                        model: "sonnet".into(),
                    },
                    CandidateSpec {
                        backend: backend.into(),
                        model: "anything".into(),
                    },
                ],
                ..MissionConfig::default()
            };
            let err = validate(&cfg).unwrap_err().to_string();
            assert!(
                err.contains("not supported in this pass"),
                "{backend}: {err}"
            );
        }
    }

    #[test]
    fn dispatch_pool_validate_rejects_parallel_workers_combination() {
        let cfg = MissionConfig {
            worker_candidates: dispatch_pool_pair(),
            max_parallel_workers: 2,
            ..MissionConfig::default()
        };
        let err = validate(&cfg).unwrap_err().to_string();
        assert!(err.contains("mutually exclusive"), "{err}");
    }

    #[test]
    fn dispatch_pool_validate_rejects_unknown_backend_and_model() {
        let cfg = MissionConfig {
            worker_candidates: vec![
                CandidateSpec {
                    backend: "claude".into(),
                    model: "sonnet".into(),
                },
                CandidateSpec {
                    backend: "gemini".into(),
                    model: "sonnet".into(),
                },
            ],
            ..MissionConfig::default()
        };
        let err = validate(&cfg).unwrap_err().to_string();
        assert!(err.contains("workerCandidates[1].backend"), "{err}");

        let cfg = MissionConfig {
            worker_candidates: vec![
                CandidateSpec {
                    backend: "claude".into(),
                    model: "sonnet".into(),
                },
                CandidateSpec {
                    backend: "codex".into(),
                    model: "kranz-test-model".into(),
                },
            ],
            ..MissionConfig::default()
        };
        let err = validate(&cfg).unwrap_err().to_string();
        assert!(err.contains("not supported by backend"), "{err}");
    }

    #[test]
    fn dispatch_pool_validate_enforces_worker_floor_per_candidate() {
        // droid's default GLM is below the default worker tier — rejected
        // without the opt-in, accepted with it, exactly like the role check.
        let mut cfg = MissionConfig {
            worker_candidates: vec![
                CandidateSpec {
                    backend: "claude".into(),
                    model: "sonnet".into(),
                },
                CandidateSpec {
                    backend: "droid".into(),
                    model: DEFAULT_DROID_MODEL.into(),
                },
            ],
            ..MissionConfig::default()
        };
        let err = validate(&cfg).unwrap_err().to_string();
        assert!(err.contains("below the default worker tier"), "{err}");
        cfg.allow_below_default_worker_model = true;
        validate(&cfg).unwrap();
    }

    #[test]
    fn dispatch_pool_validate_rejects_sandboxed_non_claude_candidate() {
        let mut cfg = MissionConfig {
            worker_candidates: dispatch_pool_pair(),
            ..MissionConfig::default()
        };
        cfg.worker.sandbox.enforce = crate::types::SandboxEnforce::Fs;
        let err = validate(&cfg).unwrap_err().to_string();
        assert!(err.contains("cannot honor sandbox.enforce"), "{err}");
    }

    #[test]
    fn dispatch_pool_executor_routing_never_goes_local() {
        // An execution-class ticket with a configured pool must NOT get
        // worker.backend rewritten to local: the pool is the explicit
        // per-candidate backend declaration, and the local key would sit
        // next to it dead and misleading.
        let mut cfg = MissionConfig {
            worker_candidates: dispatch_pool_pair(),
            ..MissionConfig::default()
        };
        let endpoint = test_local_endpoint();
        let applied = apply_executor_routing(&mut cfg, ExecutorTier::Local, Some(&endpoint));
        assert_eq!(applied, ExecutorTier::Frontier);
        assert!(cfg.worker.backend.is_none());
    }

    trait RoleConfigTestExt {
        fn role_mut_for_test(&mut self, role: Role) -> &mut crate::types::RoleConfig;
    }

    impl RoleConfigTestExt for MissionConfig {
        fn role_mut_for_test(&mut self, role: Role) -> &mut crate::types::RoleConfig {
            match role {
                Role::Orchestrator => &mut self.orchestrator,
                Role::Worker => &mut self.worker,
                Role::ValidatorScrutiny => &mut self.validator_scrutiny,
                Role::ValidatorFunctional => &mut self.validator_functional,
            }
        }
    }
}