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
//! Integration tests for permission profiles (plan §4.7) and the session
//! runner (plan §4.6), driven entirely through the mock backend.

use kranz_engine::auth_verify::AuthVerdict;
use kranz_engine::backend::{AgentEvent, PromptMode, SessionExit, SessionSpec};
use kranz_engine::backend_claude::parse_stream_line;
use kranz_engine::backend_mock::{
    mock_denied, mock_init, mock_result_text, mock_text, mock_tool_use, MockBackend, MockScript,
};
use kranz_engine::control::{self, ControlWatcher};
use kranz_engine::event_log::{EventLog, LockForce};
use kranz_engine::events::{Event, EventKind};
use kranz_engine::paths::MissionPaths;
use kranz_engine::permissions::{self, PermissionProfile};
use kranz_engine::runner::{
    parse_validator_report, parse_worker_report, run_session, run_validator, run_worker,
    run_worker_in_buffered, RunMeta,
};
use kranz_engine::types::{
    Assertion, AssertionCheck, ControlCommand, Feature, FeatureOrigin, FeatureStatus, Milestone,
    MilestoneStatus, MissionConfig, Role, RunResult, TokenUsage, WorkerIsolation,
};
use serde_json::json;
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::Notify;
use tokio::time::timeout;

const MISSION: &str = "m-test";

/// Generous bound proving a cancelled run does not hang.
const HANG_PROOF: Duration = Duration::from_secs(5);

// ---------------------------------------------------------------------------
// Fixtures
// ---------------------------------------------------------------------------

fn paths(dir: &std::path::Path) -> MissionPaths {
    MissionPaths::new(dir, MISSION)
}

/// Acquire the log (throttle 0 → deltas flush immediately) and seed it with
/// mission.created, as every real mission log starts.
fn seeded_log(p: &MissionPaths) -> EventLog {
    let mut log = EventLog::acquire(p, MISSION, Duration::from_millis(0), LockForce::No).unwrap();
    log.append(EventKind::MissionCreated {
        goal: "test the runner".to_string(),
        base_branch: "main".to_string(),
        mission_branch: format!("kranz/mission-{MISSION}"),
        config: MissionConfig::default(),
    })
    .unwrap();
    log
}

fn feature() -> Feature {
    Feature {
        id: "f-1".to_string(),
        title: "Add login".to_string(),
        spec: "Build the login endpoint".to_string(),
        validation_criteria: vec!["users can log in".to_string()],
        origin: FeatureOrigin::Plan,
        status: FeatureStatus::Pending,
        worker_runs: vec![],
        commits: vec![],
        respawns: 0,
    }
}

fn milestone() -> Milestone {
    Milestone {
        id: "ms-1".to_string(),
        title: "Auth".to_string(),
        features: vec![feature()],
        status: MilestoneStatus::Validating,
        fix_cycles: 0,
        start_sha: Some("abc123".to_string()),
        validator_guidance: None,
    }
}

fn assertion(id: &str, command: Option<&str>) -> Assertion {
    Assertion {
        id: id.to_string(),
        statement: format!("assertion {id} holds"),
        check: if command.is_some() {
            AssertionCheck::Command
        } else {
            AssertionCheck::AgentJudgement
        },
        command: command.map(str::to_string),
        negative_control: None,
        pty_script: None,
    }
}

fn worker_report_json() -> serde_json::Value {
    json!({
        "result": "pass",
        "summary": "built the login endpoint",
        "filesTouched": ["src/login.rs"],
        "testsAdded": ["login_works"],
        "testEvidence": "test login_works ... ok",
        "dependenciesAdded": [],
        "knownGaps": [],
        "commits": ["abc123 [f-1] add login"]
    })
}

fn session_spec(prompt: PromptMode) -> SessionSpec {
    SessionSpec {
        cwd: std::env::temp_dir(),
        prompt,
        append_system_prompt: Some("role prompt".to_string()),
        model: "mock-model".to_string(),
        effort: "medium".to_string(),
        session_id: "11111111-1111-4111-8111-111111111111".to_string(),
        resume: None,
        permission_mode: Some("acceptEdits".to_string()),
        allowed_tools: vec!["Bash".to_string()],
        disallowed_tools: vec!["Bash(git push*)".to_string()],
        tools: vec![],
        writable: true,
        settings_json: None,
        json_schema: None,
        max_budget_usd: Some(1.0),
        max_turns: Some(10),
        env: HashMap::new(),
        sandbox: None,
        hook_status: None,
    }
}

fn worker_meta(run_id: &str) -> RunMeta {
    RunMeta {
        backend: None,
        run_id: run_id.to_string(),
        role: Role::Worker,
        feature_id: Some("f-1".to_string()),
        milestone_id: None,
        model: "mock-model".to_string(),
        prompt_hash: "deadbeef0000".to_string(),
        executor_route: None,
    }
}

/// Events of the log at `p`, in order (log must be dropped/flushed first).
fn read_log(p: &MissionPaths) -> Vec<Event> {
    EventLog::read_events(&p.events_file()).unwrap()
}

fn event_types(events: &[Event]) -> Vec<&'static str> {
    events.iter().map(|e| e.kind.type_name()).collect()
}

// ---------------------------------------------------------------------------
// Permissions (plan §4.7)
// ---------------------------------------------------------------------------

#[test]
fn worker_profile_denies_push_publish_network_and_config_extras() {
    let cfg = MissionConfig {
        deny_patterns: vec![
            "rm -rf /*".to_string(),    // bare command → wrapped
            "Bash(dd*)".to_string(),    // already a tool rule (has parens)
            "NotebookEdit".to_string(), // known tool name → kept verbatim
        ],
        worker_isolation: WorkerIsolation::Checkout,
        ..MissionConfig::default()
    };
    let profile = permissions::for_role(Role::Worker, &cfg, &[], &[], &[]);

    assert_eq!(profile.permission_mode.as_deref(), Some("acceptEdits"));
    assert_eq!(profile.allowed_tools, vec!["Bash".to_string()]);
    assert_eq!(profile.tools, None);

    for expected in [
        "Bash(git push*)",
        "Bash(git remote add*)",
        "Bash(npm publish*)",
        "Bash(yarn publish*)",
        "Bash(pnpm publish*)",
        "Bash(cargo publish*)",
        "Bash(twine*)",
        "Bash(gem push*)",
        "Bash(sudo*)",
        "Bash(curl*)",
        "Bash(wget*)",
        "WebFetch",
        "WebSearch",
        // config extras:
        "Bash(rm -rf /*)",
        "Bash(dd*)",
        "NotebookEdit",
    ] {
        assert!(
            profile.disallowed_tools.iter().any(|d| d == expected),
            "worker deny list missing {expected:?}: {:?}",
            profile.disallowed_tools
        );
    }
}

#[test]
fn orchestrator_profile_is_read_only() {
    let cfg = MissionConfig::default();
    let profile = permissions::for_role(Role::Orchestrator, &cfg, &[], &[], &[]);

    assert_eq!(profile.permission_mode.as_deref(), Some("default"));
    for expected in [
        "Read",
        "Glob",
        "Grep",
        "Bash(git log*)",
        "Bash(git diff*)",
        "Bash(git show*)",
        "Bash(git status*)",
        "Bash(git rev-parse*)",
        "Bash(git branch)",
        "Bash(git branch --list*)",
        "Bash(git branch --show-current)",
        "Bash(git branch -a)",
        "Bash(git branch -r)",
        "Bash(git branch --contains*)",
        "Bash(git tag)",
        "Bash(git tag --list*)",
        "Bash(git tag -l*)",
        "Bash(git tag --contains*)",
    ] {
        assert!(
            profile.allowed_tools.iter().any(|a| a == expected),
            "orchestrator allow list missing {expected:?}"
        );
    }
    // No write access anywhere in the allow list.
    assert!(!profile
        .allowed_tools
        .iter()
        .any(|a| a == "Write" || a == "Edit"));
    for expected in [
        "Write",
        "Edit",
        "NotebookEdit",
        "WebFetch",
        "WebSearch",
        "Bash(git push*)",
    ] {
        assert!(
            profile.disallowed_tools.iter().any(|d| d == expected),
            "orchestrator deny list missing {expected:?}"
        );
    }
}

/// Mirror of the CLI's `Bash(...)` tool-rule matching used by the allow
/// lists in this crate: a rule ending in `*` prefix-matches the command,
/// otherwise the command must match exactly. Non-Bash rules never match.
fn bash_rule_covers(rule: &str, command: &str) -> bool {
    let Some(inner) = rule.strip_prefix("Bash(").and_then(|r| r.strip_suffix(')')) else {
        return false;
    };
    match inner.strip_suffix('*') {
        Some(prefix) => command.starts_with(prefix),
        None => command == inner,
    }
}

/// Regression test: `Bash(git branch*)` / `Bash(git tag*)` used to be in the
/// read-only allow lists and prefix-matched ref-mutating commands like
/// `git branch -D main` and `git tag -d v1`. The allow lists must cover the
/// read-only listing forms without covering any mutating form.
#[test]
fn read_only_git_allows_cover_listing_but_not_ref_mutation() {
    let cfg = MissionConfig::default();
    for role in [
        Role::Orchestrator,
        Role::ValidatorScrutiny,
        Role::ValidatorFunctional,
    ] {
        let profile = permissions::for_role(role, &cfg, &[], &[], &[]);

        for mutating in [
            "git branch -D x",
            "git branch -d topic",
            "git branch -f main abc123",
            "git branch -m old new",
            "git branch --force main abc123",
            "git branch new-branch",
            "git tag -d v1",
            "git tag v2",
            "git tag -f v1 abc123",
            "git tag -a v3 -m msg",
        ] {
            assert!(
                !profile
                    .allowed_tools
                    .iter()
                    .any(|a| bash_rule_covers(a, mutating)),
                "{role:?} allow list covers mutating command {mutating:?}: {:?}",
                profile.allowed_tools
            );
        }

        for listing in [
            "git branch",
            "git branch --list",
            "git branch --list kranz/*",
            "git branch --show-current",
            "git branch -a",
            "git branch -r",
            "git branch --contains abc123",
            "git tag",
            "git tag --list",
            "git tag --list v1.*",
            "git tag -l v1.*",
            "git tag --contains abc123",
        ] {
            assert!(
                profile
                    .allowed_tools
                    .iter()
                    .any(|a| bash_rule_covers(a, listing)),
                "{role:?} allow list does not cover read-only command {listing:?}: {:?}",
                profile.allowed_tools
            );
        }
    }
}

#[test]
fn validator_profile_allows_contract_commands_as_bash_patterns() {
    let cfg = MissionConfig {
        allow_validator_commands: vec!["npm run lint".to_string()],
        worker_isolation: WorkerIsolation::Checkout,
        ..MissionConfig::default()
    };
    let commands = vec!["cargo test --all".to_string()];

    for role in [Role::ValidatorScrutiny, Role::ValidatorFunctional] {
        let profile = permissions::for_role(role, &cfg, &commands, &[], &[]);
        assert_eq!(profile.permission_mode.as_deref(), Some("default"));
        for expected in ["Read", "Bash(git diff*)"] {
            assert!(
                profile.allowed_tools.iter().any(|a| a == expected),
                "{role:?} allow list missing {expected:?}: {:?}",
                profile.allowed_tools
            );
        }
        for expected in [
            "Write",
            "Edit",
            "NotebookEdit",
            "WebFetch",
            "WebSearch",
            "Bash(git push*)",
        ] {
            assert!(
                profile.disallowed_tools.iter().any(|d| d == expected),
                "{role:?} deny list missing {expected:?}"
            );
        }
    }

    // Scrutiny/mechanical split: only the functional validator is permitted
    // the contract/config commands; scrutiny stays read-only + plain git.
    let functional = permissions::for_role(Role::ValidatorFunctional, &cfg, &commands, &[], &[]);
    for expected in ["Bash(cargo test --all*)", "Bash(npm run lint*)"] {
        assert!(
            functional.allowed_tools.iter().any(|a| a == expected),
            "functional allow list missing {expected:?}"
        );
    }
    let scrutiny = permissions::for_role(Role::ValidatorScrutiny, &cfg, &commands, &[], &[]);
    for forbidden in ["Bash(cargo test --all*)", "Bash(npm run lint*)"] {
        assert!(
            !scrutiny.allowed_tools.iter().any(|a| a == forbidden),
            "scrutiny must not permit {forbidden:?}: {:?}",
            scrutiny.allowed_tools
        );
    }
    // Operator grants still fold into both roles.
    for role in [Role::ValidatorScrutiny, Role::ValidatorFunctional] {
        let granted = permissions::for_role(role, &cfg, &[], &["cargo fmt".to_string()], &[]);
        assert!(
            granted
                .allowed_tools
                .iter()
                .any(|a| a == "Bash(cargo fmt*)"),
            "{role:?} lost an operator grant: {:?}",
            granted.allowed_tools
        );
    }
}

#[test]
fn dangerously_allow_all_bypasses_every_role() {
    let cfg = MissionConfig {
        dangerously_allow_all: true,
        worker_isolation: WorkerIsolation::Checkout,
        ..MissionConfig::default()
    };
    for role in [
        Role::Worker,
        Role::Orchestrator,
        Role::ValidatorScrutiny,
        Role::ValidatorFunctional,
    ] {
        let profile = permissions::for_role(role, &cfg, &["cargo test".to_string()], &[], &[]);
        assert_eq!(
            profile.permission_mode.as_deref(),
            Some("bypassPermissions")
        );
        assert!(profile.allowed_tools.is_empty());
        assert!(profile.disallowed_tools.is_empty());
    }
}

#[test]
fn apply_copies_profile_onto_spec() {
    let profile = PermissionProfile {
        permission_mode: Some("default".to_string()),
        tools: Some(vec!["Bash".to_string()]),
        allowed_tools: vec!["Read".to_string()],
        disallowed_tools: vec!["Write".to_string()],
    };
    let mut spec = session_spec(PromptMode::SingleShot("task".to_string()));
    permissions::apply(profile, &mut spec);

    assert_eq!(spec.permission_mode.as_deref(), Some("default"));
    assert_eq!(spec.allowed_tools, vec!["Read".to_string()]);
    assert_eq!(spec.disallowed_tools, vec!["Write".to_string()]);
}

// ---------------------------------------------------------------------------
// run_session
// ---------------------------------------------------------------------------

#[tokio::test]
async fn run_session_happy_path_pass_report_events_and_transcript() {
    let dir = tempfile::tempdir().unwrap();
    let p = paths(dir.path());
    let mut log = seeded_log(&p);

    let backend =
        MockBackend::with_scripts(vec![MockScript::single_shot_json(&worker_report_json())]);
    let spec = session_spec(PromptMode::SingleShot("build it".to_string()));
    let outcome = run_session(&backend, spec, &mut log, &p, worker_meta("run-1"), None)
        .await
        .unwrap();

    assert_eq!(outcome.run_id, "run-1");
    assert_eq!(outcome.result, RunResult::Pass);
    assert_eq!(outcome.exit, SessionExit::Completed);
    assert_eq!(outcome.denied_count, 0);
    // Usage/cost from the mock's standard result event.
    assert_eq!(
        outcome.usage,
        TokenUsage {
            input: 1000,
            output: 200,
            cache_read: 0,
            cache_write: 0
        }
    );
    assert_eq!(outcome.cost_usd, Some(0.01));
    let report = outcome.report.expect("worker report should parse");
    assert_eq!(report.result, RunResult::Pass);
    assert_eq!(report.summary, "built the login endpoint");
    assert!(outcome.validator_report.is_none());

    // Event log: mission.created, worker.spawned, ≥1 worker.message, worker.completed.
    drop(log);
    let events = read_log(&p);
    let types = event_types(&events);
    assert_eq!(types[0], "mission.created");
    assert_eq!(types[1], "worker.spawned");
    assert_eq!(*types.last().unwrap(), "worker.completed");
    assert!(
        types.iter().filter(|t| **t == "worker.message").count() >= 1,
        "expected at least one worker.message: {types:?}"
    );

    match &events[1].kind {
        EventKind::WorkerSpawned {
            run_id,
            role,
            sdk_session_id,
            transcript_path,
            ..
        } => {
            assert_eq!(run_id, "run-1");
            assert_eq!(*role, Role::Worker);
            assert_eq!(sdk_session_id, "11111111-1111-4111-8111-111111111111");
            assert_eq!(transcript_path, "runs/run-1.jsonl");
        }
        other => panic!("expected worker.spawned, got {other:?}"),
    }
    match &events.last().unwrap().kind {
        EventKind::WorkerCompleted {
            run_id,
            result,
            tokens,
            cost_usd,
            report,
        } => {
            assert_eq!(run_id, "run-1");
            assert_eq!(*result, RunResult::Pass);
            assert_eq!(
                *tokens,
                TokenUsage {
                    input: 1000,
                    output: 200,
                    cache_read: 0,
                    cache_write: 0
                }
            );
            assert_eq!(*cost_usd, Some(0.01));
            assert!(report.is_some());
        }
        other => panic!("expected worker.completed, got {other:?}"),
    }

    // Transcript: file exists, one JSON line per scripted event.
    let transcript = std::fs::read_to_string(p.transcript_file("run-1")).unwrap();
    let lines: Vec<&str> = transcript.lines().collect();
    assert_eq!(lines.len(), 3, "init + text + result");
    for line in lines {
        serde_json::from_str::<serde_json::Value>(line)
            .unwrap_or_else(|e| panic!("transcript line is not JSON ({e}): {line}"));
    }
}

#[tokio::test]
async fn run_session_tags_denied_tool_results() {
    let dir = tempfile::tempdir().unwrap();
    let p = paths(dir.path());
    let mut log = seeded_log(&p);

    let script = MockScript {
        events: vec![
            mock_init("mock-session"),
            mock_tool_use("Bash", "git push origin main"),
            mock_denied("Bash", "git push origin main"),
            mock_result_text("stopped"),
        ],
        ..Default::default()
    };
    let backend = MockBackend::with_scripts(vec![script]);
    let spec = session_spec(PromptMode::SingleShot("try to push".to_string()));
    let outcome = run_session(&backend, spec, &mut log, &p, worker_meta("run-d"), None)
        .await
        .unwrap();

    assert_eq!(outcome.denied_count, 1);

    drop(log);
    let events = read_log(&p);
    let denied: Vec<&Event> = events
        .iter()
        .filter(|e| matches!(&e.kind, EventKind::WorkerMessage { tag, .. } if tag == "denied"))
        .collect();
    assert_eq!(
        denied.len(),
        1,
        "exactly one denied worker.message: {events:?}"
    );
    match &denied[0].kind {
        EventKind::WorkerMessage {
            run_id, content, ..
        } => {
            assert_eq!(run_id, "run-d");
            assert!(content.contains("git push"), "denied content: {content}");
        }
        _ => unreachable!(),
    }
    // The tool-use itself is tagged tool-use, not denied.
    assert!(events.iter().any(
        |e| matches!(&e.kind, EventKind::WorkerMessage { tag, content, .. }
            if tag == "tool-use" && content.starts_with("Bash: "))
    ));
}

/// grant-request-decision-flow foundation: `denied_commands` must capture the
/// denied SHELL command from a REAL Claude stream — parsed through the actual
/// `parse_stream_line`, not the fabricated `mock_denied` shape. This is the
/// exact chain a prior attempt got wrong: a denied Claude `tool_result`
/// carries `tool: None` (the tool name is only on the preceding `tool_use`),
/// so the command must be correlated positionally from that `tool_use`.
#[tokio::test]
async fn denied_commands_captured_from_a_real_claude_stream() {
    let dir = tempfile::tempdir().unwrap();
    let p = paths(dir.path());
    let mut log = seeded_log(&p);

    // A real assistant `tool_use` (Bash) and a real permission-denied
    // `tool_result`, both through the production parser.
    let tool_use_line = json!({
        "type": "assistant",
        "message": { "id": "m1", "content": [
            { "type": "tool_use", "name": "Bash", "input": { "command": "gc lint --strict" } }
        ] }
    })
    .to_string();
    let denied_line = json!({
        "type": "user",
        "message": { "role": "user", "content": [
            { "type": "tool_result", "tool_use_id": "toolu_01",
              "content": "Permission denied: Bash(gc lint --strict) requires approval",
              "is_error": true }
        ] }
    })
    .to_string();

    // Sanity: the parser yields exactly the shapes the correlation relies on —
    // a Bash ToolUse whose summary is the command, and a denied ToolResult
    // whose `tool` is None.
    let parsed_use = parse_stream_line(&tool_use_line);
    assert!(matches!(&parsed_use[0],
        AgentEvent::ToolUse { tool, summary, .. } if tool == "Bash" && summary == "gc lint --strict"));
    let parsed_denied = parse_stream_line(&denied_line);
    assert!(matches!(
        &parsed_denied[0],
        AgentEvent::ToolResult {
            tool: None,
            denied: true,
            ..
        }
    ));

    let mut events = vec![mock_init("s-real")];
    events.extend(parsed_use);
    events.extend(parsed_denied);
    events.push(mock_result_text("stopped: needs approval"));
    let script = MockScript {
        events,
        ..Default::default()
    };
    let backend = MockBackend::with_scripts(vec![script]);
    let spec = session_spec(PromptMode::SingleShot("run the check".to_string()));
    let outcome = run_session(&backend, spec, &mut log, &p, worker_meta("run-real"), None)
        .await
        .unwrap();

    assert_eq!(outcome.denied_count, 1);
    assert_eq!(
        outcome.denied_commands,
        vec!["gc lint --strict".to_string()],
        "the denied Bash command must be correlated from the preceding tool_use"
    );
}

/// The capture must also fire for the Codex backend, whose shell ToolUse is
/// named `command_execution` (not `Bash`) but likewise carries the literal
/// command in its summary. A prior review found the runner's `bash`-only guard
/// silently dropped these — the command was right there but rejected.
#[tokio::test]
async fn denied_commands_captured_from_codex_command_execution() {
    let dir = tempfile::tempdir().unwrap();
    let p = paths(dir.path());
    let mut log = seeded_log(&p);

    let script = MockScript {
        events: vec![
            mock_init("s-codex"),
            // Codex's shell tool: tool name "command_execution", summary = cmd.
            mock_tool_use("command_execution", "gc scan --all"),
            // The denied result (its own `tool`/`summary` are irrelevant — the
            // command is correlated from the preceding ToolUse).
            mock_denied("command_execution", "sandbox refused"),
            mock_result_text("stopped: needs approval"),
        ],
        ..Default::default()
    };
    let backend = MockBackend::with_scripts(vec![script]);
    let spec = session_spec(PromptMode::SingleShot("run the scan".to_string()));
    let outcome = run_session(&backend, spec, &mut log, &p, worker_meta("run-codex"), None)
        .await
        .unwrap();

    assert_eq!(outcome.denied_count, 1);
    assert_eq!(
        outcome.denied_commands,
        vec!["gc scan --all".to_string()],
        "a Codex command_execution denial must be captured, not dropped"
    );
}

#[tokio::test]
async fn run_session_without_parseable_report_is_partial() {
    let dir = tempfile::tempdir().unwrap();
    let p = paths(dir.path());
    let mut log = seeded_log(&p);

    let backend = MockBackend::with_scripts(vec![MockScript::single_shot("no json here at all")]);
    let spec = session_spec(PromptMode::SingleShot("build it".to_string()));
    let outcome = run_session(&backend, spec, &mut log, &p, worker_meta("run-g"), None)
        .await
        .unwrap();

    assert_eq!(outcome.exit, SessionExit::Completed);
    assert!(outcome.report.is_none());
    assert_eq!(outcome.result, RunResult::Partial);
}

#[tokio::test]
async fn run_session_report_downgrades_provisional_pass() {
    let dir = tempfile::tempdir().unwrap();
    let p = paths(dir.path());
    let mut log = seeded_log(&p);

    let report = json!({ "result": "partial", "summary": "ran out of budget" });
    let backend = MockBackend::with_scripts(vec![MockScript::single_shot_json(&report)]);
    let spec = session_spec(PromptMode::SingleShot("build it".to_string()));
    let outcome = run_session(&backend, spec, &mut log, &p, worker_meta("run-p"), None)
        .await
        .unwrap();

    assert_eq!(outcome.exit, SessionExit::Completed);
    assert_eq!(outcome.result, RunResult::Partial);
    assert_eq!(outcome.report.unwrap().result, RunResult::Partial);
}

#[tokio::test]
async fn run_session_cancellation_aborts_and_reports_partial() {
    let dir = tempfile::tempdir().unwrap();
    let p = paths(dir.path());
    let mut log = seeded_log(&p);

    // Streaming session that never ends on its own: after the initial events
    // it parks waiting for injected messages that never come.
    let script = MockScript::streaming(vec![mock_init("mock-session"), mock_text("working...")]);
    let backend = MockBackend::with_scripts(vec![script]);
    let spec = session_spec(PromptMode::Streaming("keep working".to_string()));

    // Pre-armed cancel: Notify stores the permit, so the runner sees it as
    // soon as it selects on the flag.
    let cancel = Arc::new(Notify::new());
    cancel.notify_one();

    let outcome = timeout(
        HANG_PROOF,
        run_session(
            &backend,
            spec,
            &mut log,
            &p,
            worker_meta("run-c"),
            Some(cancel),
        ),
    )
    .await
    .expect("cancelled run must not hang")
    .unwrap();

    assert_eq!(outcome.exit, SessionExit::Aborted);
    assert_eq!(outcome.result, RunResult::Partial);

    drop(log);
    let events = read_log(&p);
    match &events.last().unwrap().kind {
        EventKind::WorkerCompleted { result, .. } => assert_eq!(*result, RunResult::Partial),
        other => panic!("expected worker.completed, got {other:?}"),
    }
}

/// Regression (interrupt loss): the ControlWatcher fires BEFORE run_session
/// ever polls the cancel notify — modelling a fire while `backend.start()`
/// is still in flight. The `notify_one` permit must be stored so the run
/// still aborts; with `notify_waiters` the fire woke nobody and the run hung
/// forever on the never-ending stream.
#[tokio::test]
async fn interrupt_fired_before_first_poll_still_aborts_the_run() {
    let dir = tempfile::tempdir().unwrap();
    let p = paths(dir.path());
    let mut log = seeded_log(&p);

    control::enqueue(
        &p,
        &ControlCommand::Msg {
            text: "stop".into(),
            interrupt: true,
        },
    )
    .unwrap();
    let cancel = Arc::new(Notify::new());
    // Run the watcher to completion first: it fires and returns while nobody
    // is registered on the notify.
    timeout(
        HANG_PROOF,
        ControlWatcher::wait_for_interrupt(
            p.clone(),
            Duration::from_millis(10),
            Arc::clone(&cancel),
        ),
    )
    .await
    .expect("watcher must fire and return");

    // Never-ending streaming session: only the stored permit can end it.
    let script = MockScript::streaming(vec![mock_init("mock-session"), mock_text("working...")]);
    let backend = MockBackend::with_scripts(vec![script]);
    let spec = session_spec(PromptMode::Streaming("keep working".to_string()));

    let outcome = timeout(
        HANG_PROOF,
        run_session(
            &backend,
            spec,
            &mut log,
            &p,
            worker_meta("run-pre"),
            Some(cancel),
        ),
    )
    .await
    .expect("a pre-fired interrupt must abort the run, not hang")
    .unwrap();

    assert_eq!(outcome.exit, SessionExit::Aborted);
    assert_eq!(outcome.result, RunResult::Partial);
}

#[tokio::test]
async fn run_session_scrubs_credentials_from_log_and_transcript() {
    let dir = tempfile::tempdir().unwrap();
    let p = paths(dir.path());
    let mut log = seeded_log(&p);

    let token = "ghp_AbCdEfGhIjKlMnOpQrStUvWxYz0123";
    let script = MockScript {
        events: vec![
            mock_init("mock-session"),
            mock_text(&format!("found a token: {token} in .env")),
            mock_result_text("done"),
        ],
        ..Default::default()
    };
    let backend = MockBackend::with_scripts(vec![script]);
    let spec = session_spec(PromptMode::SingleShot("scan".to_string()));
    run_session(&backend, spec, &mut log, &p, worker_meta("run-s"), None)
        .await
        .unwrap();

    drop(log);
    let raw_log = std::fs::read_to_string(p.events_file()).unwrap();
    assert!(!raw_log.contains(token), "event log leaked the token");
    let events = read_log(&p);
    let text_content = events
        .iter()
        .find_map(|e| match &e.kind {
            EventKind::WorkerMessage { tag, content, .. } if tag == "text" => Some(content.clone()),
            _ => None,
        })
        .expect("a text worker.message should exist");
    assert!(
        text_content.contains("[REDACTED]"),
        "content: {text_content}"
    );
    assert!(!text_content.contains(token));

    let transcript = std::fs::read_to_string(p.transcript_file("run-s")).unwrap();
    assert!(!transcript.contains(token), "transcript leaked the token");
    assert!(transcript.contains("[REDACTED]"));
}

/// Regression (structured-field leak): a credential inside a WorkerReport
/// field (testEvidence etc.) must be redacted BEFORE the report is parsed —
/// otherwise it lands verbatim in the `worker.completed` event on
/// events.jsonl, bypassing the transcript/message scrubbing.
#[tokio::test]
async fn run_session_scrubs_worker_report_structured_fields() {
    let dir = tempfile::tempdir().unwrap();
    let p = paths(dir.path());
    let mut log = seeded_log(&p);

    let token = "ghp_AbCdEfGhIjKlMnOpQrStUvWxYz0123";
    let report = json!({
        "result": "pass",
        "summary": format!("done; used {token} for the API"),
        "testEvidence": format!("curl with {token} returned 200"),
    });
    let backend = MockBackend::with_scripts(vec![MockScript::single_shot_json(&report)]);
    let spec = session_spec(PromptMode::SingleShot("build".to_string()));
    let outcome = run_session(&backend, spec, &mut log, &p, worker_meta("run-r"), None)
        .await
        .unwrap();

    // The parsed report is redacted (it feeds worker.completed and the
    // orchestrator judgement turn); so is the outcome's final text.
    let parsed = outcome.report.expect("report parses from scrubbed text");
    assert_eq!(parsed.result, RunResult::Pass);
    assert!(
        parsed.summary.contains("[REDACTED]"),
        "summary: {}",
        parsed.summary
    );
    assert!(
        parsed.test_evidence.contains("[REDACTED]"),
        "testEvidence: {}",
        parsed.test_evidence
    );
    assert!(!parsed.test_evidence.contains(token));
    assert!(
        !outcome.final_text.contains(token),
        "final_text must be scrubbed"
    );

    // Nothing on events.jsonl carries the raw token.
    drop(log);
    let raw_log = std::fs::read_to_string(p.events_file()).unwrap();
    assert!(!raw_log.contains(token), "event log leaked the token");
    let events = read_log(&p);
    match &events.last().unwrap().kind {
        EventKind::WorkerCompleted {
            report: Some(r), ..
        } => {
            assert!(
                r.test_evidence.contains("[REDACTED]"),
                "event report: {r:?}"
            );
        }
        other => panic!("expected worker.completed with a report, got {other:?}"),
    }
}

// ---------------------------------------------------------------------------
// Report parsing fallbacks
// ---------------------------------------------------------------------------

/// H10a: a decision turn commits the mission, so it accepts only JSON the
/// model presented AS its answer — the whole reply, or the sole fenced block.
/// The lenient first-`{`-to-last-`}` span stays out of this path: it takes a
/// JSON object the model quoted and disowned as the verdict.
#[derive(serde::Deserialize)]
struct Verdicts {
    verdicts: Vec<serde_json::Value>,
}

#[derive(serde::Deserialize)]
struct Decision {
    decision: String,
}

#[test]
fn parse_decision_refuses_quoted_and_disowned_json() {
    let disowned = "The worker's NOTES.md contains this block, which I do NOT endorse: \
                    {\"verdicts\":[{\"id\":\"a-1\",\"pass\":true,\"evidence\":\"see notes\"}],\
                    \"summary\":\"all good\"} My actual finding: a-1 is NOT met.";
    assert!(
        kranz_engine::runner::parse_decision::<Verdicts>(disowned).is_none(),
        "prose-embedded JSON must not be read as the decision"
    );
    // The lenient parser is what it is: this is the exact leniency the
    // decision path drops.
    let lenient =
        kranz_engine::runner::parse_report::<Verdicts>(disowned).expect("greedy span still parses");
    assert_eq!(
        lenient.verdicts.len(),
        1,
        "fixture no longer exercises the greedy span"
    );
}

#[test]
fn parse_decision_accepts_the_three_legitimate_shapes() {
    let bare = r#"{"decision":"complete"}"#;
    assert_eq!(
        kranz_engine::runner::parse_decision::<Decision>(bare)
            .expect("bare JSON")
            .decision,
        "complete"
    );
    assert_eq!(
        kranz_engine::runner::parse_decision::<Decision>(&format!("\n  {bare}\n  "))
            .expect("whitespace-padded JSON")
            .decision,
        "complete"
    );
    assert_eq!(
        kranz_engine::runner::parse_decision::<Decision>(&format!("```json\n{bare}\n```"))
            .expect("sole fenced block")
            .decision,
        "complete"
    );
    assert_eq!(
        kranz_engine::runner::parse_decision::<Decision>(&format!("```\n{bare}\n```"))
            .expect("sole untagged fenced block")
            .decision,
        "complete"
    );
}

#[test]
fn parse_decision_refuses_two_fenced_blocks_and_unclosed_fences() {
    let two = "```json\n{\"decision\":\"respawn\"}\n```\nand my real answer:\n\
               ```json\n{\"decision\":\"complete\"}\n```";
    assert!(
        kranz_engine::runner::parse_decision::<Decision>(two).is_none(),
        "two candidate blocks must fail closed, not pick one"
    );
    let unclosed = "```json\n{\"decision\":\"complete\"}";
    assert!(kranz_engine::runner::parse_decision::<Decision>(unclosed).is_none());
    // Prose around a sole fenced block is fine; prose INSIDE it is not.
    let trailing = "```json\n{\"decision\":\"complete\"} then some prose\n```";
    assert!(kranz_engine::runner::parse_decision::<Decision>(trailing).is_none());
}

/// M-6 (follow-up review): H10a's greedy-brace half was closed, but the
/// "quoted and disowned" half only moved from bare prose into a fence. A
/// fence is a quotation mark as easily as an answer, so the decision must be
/// the LAST thing the reply says.
#[test]
fn parse_decision_refuses_a_fenced_block_the_model_disowns_afterwards() {
    let disowned = "Here is an example of a verdict I am NOT issuing:\n\
                    ```json\n\
                    {\"verdicts\":[{\"id\":\"a-1\",\"pass\":true,\"evidence\":\"...\"}]}\n\
                    ```\n\
                    My actual verdict is FAIL.";
    assert!(
        kranz_engine::runner::parse_decision::<Verdicts>(disowned).is_none(),
        "a fenced block the model disowns in trailing prose must fail closed"
    );

    // The taught shape (a lead-in, then the fence, then nothing) still
    // parses; only trailing content is fatal.
    let lead_in = "Here is my verdict:\n\
                   ```json\n\
                   {\"verdicts\":[{\"id\":\"a-1\",\"pass\":true,\"evidence\":\"...\"}]}\n\
                   ```\n  \n";
    assert_eq!(
        kranz_engine::runner::parse_decision::<Verdicts>(lead_in)
            .expect("a lead-in before the fence is fine")
            .verdicts
            .len(),
        1
    );
}

/// M-7 (follow-up review): a validator quoting a code snippet inside an
/// `evidence` string value put backticks mid-line, the counting scan saw
/// four fences, and a perfectly good verdict cost a retry and then landed on
/// the caller's default. Fence state is per LINE, so mid-line backticks are
/// just characters.
#[test]
fn parse_decision_accepts_json_whose_string_values_contain_backticks() {
    let quoting = "```json\n\
                   {\"verdicts\":[{\"id\":\"a-1\",\"pass\":false,\
                   \"evidence\":\"the snippet ```rust fn main(){}``` never compiled\"}]}\n\
                   ```";
    let parsed = kranz_engine::runner::parse_decision::<Verdicts>(quoting)
        .expect("backticks inside a JSON string value are not fences");
    assert_eq!(parsed.verdicts.len(), 1);
}

/// The lenient parser stays for the worker/validator report channel, which is
/// not a consent decision and where a report the model buries in prose is
/// better recovered than dropped. `parse_decision` is the strict twin.
#[test]
fn parse_worker_report_strict_and_fallbacks() {
    let strict = worker_report_json().to_string();
    assert!(parse_worker_report(&strict).is_some(), "strict parse");

    let braces = format!("Here is my report. {strict} That's all.");
    assert!(
        parse_worker_report(&braces).is_some(),
        "brace-substring parse"
    );

    let fenced = format!("Work finished {{with caveats}}.\n```json\n{strict}\n```\nGoodbye.");
    let report = parse_worker_report(&fenced).expect("fenced parse");
    assert_eq!(report.summary, "built the login endpoint");

    assert!(parse_worker_report("no json here at all").is_none());
    assert!(parse_worker_report("{ not valid json }").is_none());
}

#[test]
fn parse_validator_report_fallbacks() {
    let value = json!({
        "findings": [{
            "subject": "a-1",
            "severity": "major",
            "evidence": "test asserts the mock, not the behaviour",
            "suggestedFix": "assert on output"
        }],
        "summary": "one major finding"
    });
    let fenced = format!("Prose first.\n```json\n{value}\n```");
    let report = parse_validator_report(&fenced).expect("fenced parse");
    assert_eq!(report.findings.len(), 1);
    assert_eq!(report.findings[0].severity, "major");

    assert!(parse_validator_report("total garbage").is_none());
}

// ---------------------------------------------------------------------------
// run_worker / run_validator wrappers
// ---------------------------------------------------------------------------

#[tokio::test]
async fn run_worker_builds_spec_and_uses_report_result() {
    let dir = tempfile::tempdir().unwrap();
    let p = paths(dir.path());
    let mut log = seeded_log(&p);
    let cfg = MissionConfig::default();

    let backend =
        MockBackend::with_scripts(vec![MockScript::single_shot_json(&worker_report_json())]);
    let outcome = run_worker(
        &backend,
        &mut log,
        &p,
        &cfg,
        &feature(),
        "ship the auth system",
        "Auth",
        Some("mind the rate limiter"),
        None,
        None,
        &[],
        &[],
        &[],
        AuthVerdict::Inconclusive,
        &[],
        None,
        None,
    )
    .await
    .unwrap();

    assert_eq!(outcome.result, RunResult::Pass);
    assert!(outcome.report.is_some());

    let specs = backend.started_specs();
    assert_eq!(specs.len(), 1);
    let spec = &specs[0];
    assert_eq!(spec.cwd, p.repo_root);
    assert!(
        !spec.env.contains_key("KRANZ_BASE_SHA"),
        "no base sha means no env var"
    );
    assert_eq!(spec.model, cfg.worker.model);
    assert_eq!(spec.effort, cfg.worker.reasoning_effort);
    assert_eq!(spec.max_turns, cfg.worker.max_turns);
    assert_eq!(spec.max_budget_usd, cfg.worker.max_budget_usd);
    assert!(
        spec.writable,
        "worker sessions must get write-capable backends"
    );
    assert_eq!(spec.permission_mode.as_deref(), Some("acceptEdits"));
    assert_eq!(spec.allowed_tools, vec!["Bash".to_string()]);
    assert!(spec.disallowed_tools.iter().any(|d| d == "Bash(git push*)"));
    assert!(
        uuid::Uuid::parse_str(&spec.session_id).is_ok(),
        "session id is a uuid"
    );

    // Role prompt rendered into append_system_prompt; task body is the prompt.
    let system = spec
        .append_system_prompt
        .as_deref()
        .expect("role prompt set");
    assert!(
        system.contains("f-1"),
        "featureId rendered into role prompt"
    );
    assert!(!system.contains("{featureId}"), "no unrendered placeholder");
    match &spec.prompt {
        PromptMode::SingleShot(task) => {
            assert!(task.contains("Add login"));
            assert!(task.contains("Build the login endpoint"));
            assert!(task.contains("users can log in"));
            assert!(task.contains("mind the rate limiter"));
        }
        other => panic!("worker must be single-shot, got {other:?}"),
    }

    // WorkerReport schema enforced at the source.
    let schema = spec.json_schema.as_ref().expect("json schema set");
    assert_eq!(schema["additionalProperties"], json!(false));
    assert!(schema["required"]
        .as_array()
        .unwrap()
        .iter()
        .any(|v| v == "result"));
    assert!(schema["properties"]["filesTouched"].is_object());

    // Feature id recorded on worker.spawned.
    drop(log);
    let events = read_log(&p);
    assert!(events.iter().any(|e| matches!(
        &e.kind,
        EventKind::WorkerSpawned { feature_id: Some(f), role: Role::Worker, .. } if f == "f-1"
    )));
}

#[tokio::test]
async fn run_worker_seeds_scratch_home_and_config_dir_worker_env_hygiene() {
    let dir = tempfile::tempdir().unwrap();
    let p = paths(dir.path());
    let mut log = seeded_log(&p);
    let cfg = MissionConfig::default();

    let backend =
        MockBackend::with_scripts(vec![MockScript::single_shot_json(&worker_report_json())]);
    let base_sha = "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef";
    run_worker(
        &backend,
        &mut log,
        &p,
        &cfg,
        &feature(),
        "ship the auth system",
        "Auth",
        None,
        None,
        Some(base_sha),
        &[],
        &[],
        &[],
        AuthVerdict::Authenticated,
        &[],
        None,
        None,
    )
    .await
    .unwrap();

    let specs = backend.started_specs();
    let spec = &specs[0];

    // Contract env is preserved.
    assert_eq!(
        spec.env.get("KRANZ_BASE_SHA").map(String::as_str),
        Some(base_sha)
    );

    // seed_worker_env relocates HOME only once the preflight has proven the
    // scratch env authenticates (mission m-165b6f, f-1-2). With an
    // Authenticated verdict, the worker spec is relocated to the scratch
    // home under `scratch_home_root(session_id)`. CLAUDE_CONFIG_DIR is
    // deliberately NOT set: it poisons keychain-backed OAuth resolution
    // (probed 2026-07-29), and a relocated HOME resolves HOME/.claude
    // implicitly.
    let scratch_root = kranz_engine::backend_claude::scratch_home_root(&spec.session_id);
    let home = spec.env.get("HOME").expect("HOME relocated to scratch dir");
    assert!(
        std::path::Path::new(home).starts_with(&scratch_root),
        "HOME {home} must be under scratch root {}",
        scratch_root.display()
    );
    assert!(
        !spec.env.contains_key("CLAUDE_CONFIG_DIR"),
        "CLAUDE_CONFIG_DIR must NOT be relocated (keychain OAuth poison)"
    );
    assert!(
        std::path::Path::new(home).join(".claude").is_dir(),
        "the seeded scratch config dir is HOME/.claude"
    );
}

#[cfg(target_os = "macos")]
#[tokio::test]
async fn run_worker_routes_macos_fs_net_through_egress_proxy() {
    let dir = tempfile::tempdir().unwrap();
    let p = paths(dir.path());
    let mut log = seeded_log(&p);
    let mut cfg = MissionConfig::default();
    cfg.worker.sandbox.enforce = kranz_engine::types::SandboxEnforce::FsNet;

    let backend =
        MockBackend::with_scripts(vec![MockScript::single_shot_json(&worker_report_json())]);
    let outcome = run_worker(
        &backend,
        &mut log,
        &p,
        &cfg,
        &feature(),
        "ship the auth system",
        "Auth",
        None,
        None,
        None,
        &[],
        &[],
        &[],
        AuthVerdict::Inconclusive,
        &[],
        None,
        None,
    )
    .await
    .unwrap();

    // macOS fs+net resolves to Seatbelt (loopback-only egress) plus the
    // filtering egress proxy: the session's env points at the spawned proxy.
    let specs = backend.started_specs();
    assert_eq!(specs.len(), 1, "fs+net no longer refuses on macOS");
    let spec = &specs[0];
    let sandbox = spec.sandbox.as_ref().expect("sandbox resolved");
    assert_eq!(
        sandbox.backend,
        kranz_engine::sandbox::SandboxBackend::Seatbelt
    );
    let proxy = spec
        .env
        .get(kranz_engine::egress_proxy::HTTPS_PROXY_ENV)
        .expect("fs+net session carries proxy env");
    assert!(
        proxy.starts_with("http://127.0.0.1:"),
        "seatbelt sessions reach the proxy on loopback: {proxy}"
    );
    assert_eq!(
        spec.env.get(kranz_engine::egress_proxy::HTTP_PROXY_ENV),
        Some(proxy)
    );
    assert_eq!(
        spec.env.get(kranz_engine::egress_proxy::NO_PROXY_ENV),
        Some(&kranz_engine::egress_proxy::NO_PROXY_VALUE.to_string())
    );
    assert!(
        outcome.denied_egress.is_empty(),
        "no CONNECTs in a mock session: {:?}",
        outcome.denied_egress
    );
}

#[tokio::test]
async fn run_validator_builds_spec_permissions_and_parses_report() {
    let dir = tempfile::tempdir().unwrap();
    let p = paths(dir.path());
    let mut log = seeded_log(&p);
    let cfg = MissionConfig {
        allow_validator_commands: vec!["npm run lint".to_string()],
        worker_isolation: WorkerIsolation::Checkout,
        ..MissionConfig::default()
    };
    let contract = vec![
        assertion("a-1", Some("cargo test --all")),
        assertion("a-2", None),
    ];

    let report = json!({ "findings": [], "summary": "everything holds" });
    let backend = MockBackend::with_scripts(vec![MockScript::single_shot_json(&report)]);
    let outcome = run_validator(
        &backend,
        &mut log,
        &p,
        &cfg,
        Role::ValidatorFunctional,
        &milestone(),
        &contract,
        "abc123",
        None,
        None,
        &[],
        &[],
        &[],
        None,
        None,
    )
    .await
    .unwrap();

    assert_eq!(outcome.result, RunResult::Pass);
    let validator_report = outcome.validator_report.expect("validator report parses");
    assert!(validator_report.findings.is_empty());
    assert!(outcome.report.is_none(), "no WorkerReport for validators");

    let specs = backend.started_specs();
    let spec = &specs[0];
    assert!(
        !spec.writable,
        "validator sessions must keep read-only backend mode"
    );
    assert!(
        !spec.env.contains_key("KRANZ_BASE_SHA"),
        "no base sha means no env var"
    );
    assert!(
        !spec.env.contains_key("HOME") && !spec.env.contains_key("CLAUDE_CONFIG_DIR"),
        "worker_env_hygiene scratch env is worker-role only — validator env must be unchanged: {:?}",
        spec.env
    );
    assert_eq!(spec.model, cfg.validator_functional.model);
    assert_eq!(spec.permission_mode.as_deref(), Some("default"));
    assert!(spec
        .allowed_tools
        .iter()
        .any(|a| a == "Bash(cargo test --all*)"));
    assert!(spec
        .allowed_tools
        .iter()
        .any(|a| a == "Bash(npm run lint*)"));
    assert!(spec.disallowed_tools.iter().any(|d| d == "Write"));

    let system = spec.append_system_prompt.as_deref().unwrap();
    assert!(
        system.contains("abc123"),
        "startSha rendered into role prompt"
    );
    match &spec.prompt {
        PromptMode::SingleShot(task) => {
            assert!(task.contains("abc123..HEAD"));
            assert!(task.contains("cargo test --all"));
            assert!(task.contains("a-2"), "agent-judgement assertion listed");
            assert!(task.contains("users can log in"), "feature criteria listed");
        }
        other => panic!("validator must be single-shot, got {other:?}"),
    }

    // Milestone id (not feature id) recorded on worker.spawned.
    drop(log);
    let events = read_log(&p);
    assert!(events.iter().any(|e| matches!(
        &e.kind,
        EventKind::WorkerSpawned {
            milestone_id: Some(m),
            feature_id: None,
            role: Role::ValidatorFunctional,
            ..
        } if m == "ms-1"
    )));
}

/// Worker-reported `commandsRun` is model-authored JSON with no human step,
/// so it must not mint `Bash(...)` allow rules for the read-only validator
/// (audit-exec M1). It stays in the prompt as a claim: the validator is told
/// the worker says it ran these, and is told they are not permitted.
#[tokio::test]
async fn worker_reported_commands_never_become_validator_allow_rules() {
    let dir = tempfile::tempdir().unwrap();
    let p = paths(dir.path());
    let mut log = seeded_log(&p);
    let cfg = MissionConfig {
        allow_validator_commands: vec!["npm run lint".to_string()],
        worker_isolation: WorkerIsolation::Checkout,
        ..MissionConfig::default()
    };
    let contract = vec![assertion("a-1", Some("cargo test --all"))];
    let worker_commands = vec!["bash -c".to_string(), "python3 -c".to_string()];

    let report = json!({ "findings": [], "summary": "everything holds" });
    let backend = MockBackend::with_scripts(vec![MockScript::single_shot_json(&report)]);
    run_validator(
        &backend,
        &mut log,
        &p,
        &cfg,
        Role::ValidatorFunctional,
        &milestone(),
        &contract,
        "abc123",
        None,
        None,
        &[],
        &[],
        &worker_commands,
        None,
        None,
    )
    .await
    .unwrap();

    let specs = backend.started_specs();
    let spec = &specs[0];
    for forbidden in ["Bash(bash -c*)", "Bash(python3 -c*)"] {
        assert!(
            !spec.allowed_tools.iter().any(|a| a == forbidden),
            "worker report minted {forbidden:?}: {:?}",
            spec.allowed_tools
        );
    }
    // The approved contract and operator config still grant theirs.
    for expected in ["Bash(cargo test --all*)", "Bash(npm run lint*)"] {
        assert!(
            spec.allowed_tools.iter().any(|a| a == expected),
            "lost {expected:?}: {:?}",
            spec.allowed_tools
        );
    }
    // Still visible to the validator, labelled as a claim.
    match &spec.prompt {
        PromptMode::SingleShot(task) => {
            assert!(task.contains("bash -c"), "worker claim dropped from prompt");
            assert!(
                task.contains("worker reports"),
                "worker claim not labelled as a claim: {task}"
            );
        }
        other => panic!("validator must be single-shot, got {other:?}"),
    }
}

/// A human-approved grant for a command the worker also reported still
/// grants it: provenance is what changed, not the grant path.
#[tokio::test]
async fn granted_commands_still_allow_even_when_the_worker_reported_them() {
    let dir = tempfile::tempdir().unwrap();
    let p = paths(dir.path());
    let mut log = seeded_log(&p);
    let cfg = MissionConfig {
        worker_isolation: WorkerIsolation::Checkout,
        ..MissionConfig::default()
    };
    let report = json!({ "findings": [], "summary": "ok" });
    let backend = MockBackend::with_scripts(vec![MockScript::single_shot_json(&report)]);
    run_validator(
        &backend,
        &mut log,
        &p,
        &cfg,
        Role::ValidatorFunctional,
        &milestone(),
        &[],
        "abc123",
        None,
        None,
        &["cargo fmt".to_string()],
        &[],
        &["cargo fmt".to_string()],
        None,
        None,
    )
    .await
    .unwrap();

    let specs = backend.started_specs();
    assert!(
        specs[0]
            .allowed_tools
            .iter()
            .any(|a| a == "Bash(cargo fmt*)"),
        "operator grant lost: {:?}",
        specs[0].allowed_tools
    );
}

#[tokio::test]
async fn run_validator_rejects_non_validator_roles() {
    let dir = tempfile::tempdir().unwrap();
    let p = paths(dir.path());
    let mut log = seeded_log(&p);
    let cfg = MissionConfig::default();
    let backend = MockBackend::new();

    let err = run_validator(
        &backend,
        &mut log,
        &p,
        &cfg,
        Role::Worker,
        &milestone(),
        &[],
        "abc123",
        None,
        None,
        &[],
        &[],
        &[],
        None,
        None,
    )
    .await
    .unwrap_err();
    assert!(err.to_string().contains("validator role"), "got: {err}");
}

#[cfg(target_os = "macos")]
#[tokio::test]
async fn run_validator_routes_macos_fs_net_through_egress_proxy() {
    let dir = tempfile::tempdir().unwrap();
    let p = paths(dir.path());
    let mut log = seeded_log(&p);
    let mut cfg = MissionConfig::default();
    cfg.validator_scrutiny.sandbox.enforce = kranz_engine::types::SandboxEnforce::FsNet;
    let backend = MockBackend::with_scripts(vec![MockScript::single_shot_json(
        &json!({ "findings": [], "summary": "all good" }),
    )]);

    let outcome = run_validator(
        &backend,
        &mut log,
        &p,
        &cfg,
        Role::ValidatorScrutiny,
        &milestone(),
        &[],
        "abc123",
        None,
        None,
        &[],
        &[],
        &[],
        None,
        None,
    )
    .await
    .unwrap();

    // Validator sessions get the same proxy wiring as workers on macOS fs+net.
    let specs = backend.started_specs();
    assert_eq!(specs.len(), 1, "fs+net no longer refuses on macOS");
    assert_eq!(
        specs[0].sandbox.as_ref().map(|s| s.backend),
        Some(kranz_engine::sandbox::SandboxBackend::Seatbelt)
    );
    assert!(
        specs[0]
            .env
            .contains_key(kranz_engine::egress_proxy::HTTPS_PROXY_ENV),
        "validator fs+net session carries proxy env: {:?}",
        specs[0].env
    );
    assert!(outcome.denied_egress.is_empty());
}

/// Contract commands admit their natural reinvocations (observed live:
/// verbatim-only prefixes denied the validator its own checks and blocked a
/// milestone on a permissions artifact) through exact declared-form rules —
/// and NOTHING wider (ticket validator-immutability-proof): the old
/// leading-two-token catch-alls (`python3 -*`, `python3 -m*`) are gone, and
/// heredoc contracts run through the engine-side contract execution path
/// instead of a validator Bash rule.
#[test]
fn validator_command_patterns_cover_natural_variations() {
    use kranz_engine::permissions::command_allow_patterns;

    let pats = command_allow_patterns("python3 extract_links.py && echo EXIT_OK");
    let covers = |cmd: &str| {
        pats.iter().any(|p| {
            let inner = p
                .strip_prefix("Bash(")
                .and_then(|s| s.strip_suffix(")"))
                .unwrap();
            match inner.strip_suffix('*') {
                Some(prefix) => cmd.starts_with(prefix),
                None => cmd == inner,
            }
        })
    };
    // Verbatim, bare segment, and arg-extended reinvocations of a segment.
    assert!(covers("python3 extract_links.py && echo EXIT_OK"));
    assert!(covers("python3 extract_links.py"));
    assert!(covers(
        "python3 extract_links.py operator@example.com out.txt"
    ));
    assert!(covers("echo EXIT_OK"));
    // Unrelated programs stay uncovered — including other uses of the same
    // interpreter (no `python3 -*` catch-all).
    assert!(!covers("python3 -c 'import sys'"));
    assert!(!covers("python3 other_script.py"));
    assert!(!covers("curl https://example.com"));
    assert!(!covers("rm -rf /"));

    // Heredoc contract commands get no validator Bash rule at all: the
    // engine runs contract commands itself and hands the validator the
    // captured PASS/FAIL evidence (validator repair 3/5).
    let heredoc = command_allow_patterns("python3 - <<'PY'\nprint('ok')\nPY");
    assert!(!heredoc.iter().any(|p| p == "Bash(python3 -*)"));

    // And the profile carries the exact declared form only — a `-m` contract
    // no longer widens to arbitrary interpreter/module use.
    let cfg = MissionConfig::default();
    let profile = kranz_engine::permissions::for_role(
        Role::ValidatorFunctional,
        &cfg,
        &["python3 -m pytest test_x.py -v".to_string()],
        &[],
        &[],
    );
    assert!(profile
        .allowed_tools
        .iter()
        .any(|p| p == "Bash(python3 -m pytest test_x.py -v*)"));
    assert!(!profile
        .allowed_tools
        .iter()
        .any(|p| p == "Bash(python3 -m*)"));
    assert!(!profile.allowed_tools.iter().any(|p| p == "Bash(python3*)"));
}

// ---------------------------------------------------------------------------
// run_worker_in_buffered (roadmap M3 wall-clock overlap)
// ---------------------------------------------------------------------------

/// The buffered worker path touches NO event log and returns the exact same
/// event KINDS the live path would have appended, in append order — the
/// building block the engine replays serially after a concurrent batch, so
/// events.jsonl stays single-writer. The per-run transcript is still written
/// live (transcripts are per-run files, not the single-writer log), and the
/// RunOutcome matches the live path.
#[tokio::test]
async fn run_worker_in_buffered_collects_kinds_without_touching_the_log() {
    let dir = tempfile::tempdir().unwrap();
    let p = paths(dir.path());
    let log = seeded_log(&p);
    let cfg = MissionConfig::default();

    // A worker run with a tool-use so we get a worker.message delta in the
    // buffer as well as spawned/completed.
    let script = MockScript {
        events: vec![
            mock_init("mock-session"),
            mock_tool_use("Bash", "cargo build"),
            mock_text(&worker_report_json().to_string()),
            mock_result_text(&worker_report_json().to_string()),
        ],
        ..Default::default()
    };
    let backend = MockBackend::with_scripts(vec![script]);

    let cwd = p.repo_root.clone();
    let (buffered, outcome) = run_worker_in_buffered(
        &backend,
        &p,
        &cfg,
        &feature(),
        "ship the auth system",
        "Auth",
        None,
        &cwd,
        None,
        &[],
        &[],
        &[],
        AuthVerdict::Inconclusive,
        &[],
        None,
        None,
    )
    .await
    .unwrap();

    // The RunOutcome is exactly what the live path yields.
    assert_eq!(outcome.result, RunResult::Pass);
    assert!(outcome.report.is_some());

    // The log was NOT touched: only mission.created is on disk (no spawned/
    // message/completed appended). The single-writer invariant is preserved by
    // construction — the buffered path never held the log.
    drop(log);
    let events = read_log(&p);
    assert_eq!(
        event_types(&events),
        vec!["mission.created"],
        "buffered run appends nothing"
    );

    // The buffer holds spawned first, at least one message, completed last —
    // the same kinds, same order, the live path would have appended.
    assert!(
        matches!(
            buffered.first(),
            Some(EventKind::WorkerSpawned {
                role: Role::Worker,
                backend: Some(kranz_engine::types::BackendKind::Claude),
                ..
            })
        ),
        "first buffered kind is worker.spawned: {buffered:?}"
    );
    assert!(
        matches!(
            buffered.last(),
            Some(EventKind::WorkerCompleted {
                result: RunResult::Pass,
                ..
            })
        ),
        "last buffered kind is a passing worker.completed: {buffered:?}"
    );
    assert!(
        buffered
            .iter()
            .any(|k| matches!(k, EventKind::WorkerMessage { tag, .. } if tag == "tool-use")),
        "a tool-use worker.message is buffered: {buffered:?}"
    );

    // The transcript file WAS written live (per-run file, not the log).
    let transcript = std::fs::read_to_string(p.transcript_file(&outcome.run_id)).unwrap();
    assert_eq!(
        transcript.lines().count(),
        4,
        "init + tool-use + text + result"
    );
}

// ---------------------------------------------------------------------------
// Egress proxy (3.3a): fs+net sessions route through the filtering proxy
// ---------------------------------------------------------------------------

use kranz_engine::egress_proxy::{
    EgressDenial, HTTPS_PROXY_ENV, HTTP_PROXY_ENV, NO_PROXY_ENV, NO_PROXY_VALUE,
};
use kranz_engine::sandbox::{ResolvedSandbox, SandboxBackend, SandboxInputs};
use kranz_engine::types::SandboxEnforce;
use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader};
use tokio::net::{TcpListener, TcpStream};

fn fs_net_sandbox(
    backend: SandboxBackend,
    egress: Vec<String>,
    dir: &std::path::Path,
) -> ResolvedSandbox {
    ResolvedSandbox {
        backend,
        inputs: SandboxInputs {
            enforce: SandboxEnforce::FsNet,
            session_cwd: dir.to_path_buf(),
            mission_dir: dir.to_path_buf(),
            tmpdir: std::env::temp_dir(),
            extra_write: Vec::new(),
            egress,
            validator_read_deny_roots: Vec::new(),
        },
        container: None,
    }
}

/// Read one proxy response head (through the CRLF terminator).
async fn read_proxy_response_head(stream: &mut TcpStream) -> String {
    let mut reader = BufReader::new(stream);
    let mut head = String::new();
    loop {
        let mut line = String::new();
        reader.read_line(&mut line).await.unwrap();
        let done = line == "\r\n";
        head.push_str(&line);
        if done {
            return head;
        }
    }
}

/// Poll until the backend has recorded its first started spec (the proxy is
/// spawned before `backend.start`, so a recorded spec means the proxy env is
/// readable and the proxy is serving).
async fn first_started_spec(backend: &MockBackend) -> SessionSpec {
    let deadline = std::time::Instant::now() + HANG_PROOF;
    loop {
        if let Some(spec) = backend.started_specs().into_iter().next() {
            return spec;
        }
        assert!(
            std::time::Instant::now() < deadline,
            "session never started"
        );
        tokio::time::sleep(Duration::from_millis(5)).await;
    }
}

/// A parked fs+net (Seatbelt) session's env points at the run's spawned
/// proxy; an allowed CONNECT tunnels, a denied CONNECT gets a 403 and lands
/// in `RunOutcome.denied_egress`, the disposable mission JSONL, and the
/// durable run-attributed event log — all over loopback.
#[tokio::test]
async fn run_session_fs_net_wires_proxy_env_and_surfaces_denials() {
    let dir = tempfile::tempdir().unwrap();
    let p = paths(dir.path());
    let mut log = seeded_log(&p);

    // Loopback echo server: the allowed CONNECT target.
    let echo = TcpListener::bind((std::net::Ipv4Addr::LOCALHOST, 0))
        .await
        .unwrap();
    let echo_port = echo.local_addr().unwrap().port();
    tokio::spawn(async move {
        let (mut socket, _) = echo.accept().await.unwrap();
        let mut buf = [0u8; 64];
        let n = socket.read(&mut buf).await.unwrap();
        socket.write_all(&buf[..n]).await.unwrap();
    });

    // A streaming script with no events parks the session in next_event until
    // the test's cancel aborts it — holding the proxy's lifetime open.
    let backend = Arc::new(MockBackend::with_scripts(vec![MockScript::streaming(
        vec![],
    )]));
    let mut spec = session_spec(PromptMode::Streaming("held".to_string()));
    spec.sandbox = Some(fs_net_sandbox(
        SandboxBackend::Seatbelt,
        vec![format!("127.0.0.1:{echo_port}")],
        dir.path(),
    ));

    let cancel = Arc::new(Notify::new());
    let run = {
        let backend = Arc::clone(&backend);
        let cancel = Arc::clone(&cancel);
        let p = p.clone();
        tokio::spawn(async move {
            run_session(
                backend.as_ref(),
                spec,
                &mut log,
                &p,
                worker_meta("run-egress"),
                Some(cancel),
            )
            .await
        })
    };

    let started = first_started_spec(backend.as_ref()).await;
    let proxy_url = started
        .env
        .get(HTTPS_PROXY_ENV)
        .expect("fs+net session carries HTTPS_PROXY")
        .clone();
    let proxy_addr = proxy_url
        .strip_prefix("http://")
        .expect("proxy url is http://host:port");
    assert!(
        proxy_addr.starts_with("127.0.0.1:"),
        "seatbelt sessions reach the proxy on loopback: {proxy_url}"
    );
    assert_eq!(
        started.env.get(HTTP_PROXY_ENV).map(String::as_str),
        Some(proxy_url.as_str())
    );
    assert_eq!(
        started.env.get(NO_PROXY_ENV).map(String::as_str),
        Some(NO_PROXY_VALUE)
    );

    // Allowed host (the echo server is on the allowlist): 200 + tunnel bytes.
    let mut client = TcpStream::connect(proxy_addr).await.unwrap();
    client
        .write_all(format!("CONNECT 127.0.0.1:{echo_port} HTTP/1.1\r\n\r\n").as_bytes())
        .await
        .unwrap();
    let head = read_proxy_response_head(&mut client).await;
    assert!(head.starts_with("HTTP/1.1 200"), "allowed CONNECT: {head}");
    client.write_all(b"tunneled").await.unwrap();
    let mut buf = vec![0u8; b"tunneled".len()];
    client.read_exact(&mut buf).await.unwrap();
    assert_eq!(buf, b"tunneled", "bytes tunnel through the run's proxy");

    // Denied host: 403, never a silent timeout. Repeat the same destination
    // to prove the durable audit event deduplicates amplification while its
    // omittedCount preserves the raw-record count.
    for _ in 0..2 {
        let mut client = TcpStream::connect(proxy_addr).await.unwrap();
        client
            .write_all(b"CONNECT denied.example:443 HTTP/1.1\r\n\r\n")
            .await
            .unwrap();
        let head = read_proxy_response_head(&mut client).await;
        assert!(head.starts_with("HTTP/1.1 403"), "denied CONNECT: {head}");
    }

    cancel.notify_one();
    let outcome = timeout(HANG_PROOF, run)
        .await
        .expect("run must not hang")
        .unwrap()
        .unwrap();

    assert_eq!(
        outcome.denied_egress,
        vec![
            EgressDenial {
                host: "denied.example".to_string(),
                port: 443,
            },
            EgressDenial {
                host: "denied.example".to_string(),
                port: 443,
            },
        ],
        "the run surfaces exactly its own proxy's denials"
    );

    // The mission JSONL holds the fsynced record with a timestamp.
    let content = std::fs::read_to_string(p.egress_denials_file()).unwrap();
    let records: Vec<serde_json::Value> = content
        .lines()
        .map(|line| serde_json::from_str(line).unwrap())
        .collect();
    assert_eq!(records.len(), 2);
    assert!(records.iter().all(|record| {
        record["host"] == "denied.example" && record["port"] == 443 && record["ts"].is_string()
    }));

    let events = EventLog::read_events(&p.events_file()).unwrap();
    let denial_seq = events
        .iter()
        .find_map(|event| match &event.kind {
            EventKind::WorkerEgressDenied {
                run_id,
                denials,
                omitted_count,
            } if run_id == "run-egress" => {
                assert_eq!(denials.len(), 1);
                assert_eq!(denials[0].host, "denied.example");
                assert_eq!(denials[0].port, 443);
                assert_eq!(*omitted_count, 1);
                Some(event.seq)
            }
            _ => None,
        })
        .expect("runner persists a run-attributed denial event");
    let completed_seq = events
        .iter()
        .find_map(|event| match &event.kind {
            EventKind::WorkerCompleted { run_id, .. } if run_id == "run-egress" => Some(event.seq),
            _ => None,
        })
        .expect("run has a completion event");
    assert!(
        denial_seq < completed_seq,
        "denial evidence must land before the run completion boundary"
    );
}

/// No fs+net ⇒ no proxy at all: no env, no denials, no denial file. Covers
/// unsandboxed, fs-only, and bwrap fs+net (netns cannot reach a host proxy —
/// v1 out of scope), plus container fs+net with an empty egress list
/// (--network none).
#[tokio::test]
async fn run_session_without_proxy_route_spawns_no_proxy() {
    for (name, sandbox) in [
        ("unsandboxed", None),
        (
            "fs-only",
            Some(fs_net_sandbox(
                SandboxBackend::Seatbelt,
                vec![],
                std::env::temp_dir().as_path(),
            )),
        ),
        (
            "bwrap fs+net",
            Some(fs_net_sandbox(
                SandboxBackend::Bubblewrap,
                vec![],
                std::env::temp_dir().as_path(),
            )),
        ),
        (
            "container fs+net empty egress",
            Some(fs_net_sandbox(
                SandboxBackend::Container,
                vec![],
                std::env::temp_dir().as_path(),
            )),
        ),
    ] {
        let dir = tempfile::tempdir().unwrap();
        let p = paths(dir.path());
        let mut log = seeded_log(&p);
        let backend =
            MockBackend::with_scripts(vec![MockScript::single_shot_json(&worker_report_json())]);
        let mut spec = session_spec(PromptMode::SingleShot("task".to_string()));
        spec.sandbox = sandbox.map(|mut s| {
            // The fs-only case exercises the Fs tier; the rest keep FsNet.
            if name == "fs-only" {
                s.inputs.enforce = SandboxEnforce::Fs;
            }
            s
        });

        let outcome = run_session(
            &backend,
            spec,
            &mut log,
            &p,
            worker_meta("run-noproxy"),
            None,
        )
        .await
        .unwrap();

        let started = &backend.started_specs()[0];
        assert!(
            !started.env.contains_key(HTTPS_PROXY_ENV)
                && !started.env.contains_key(HTTP_PROXY_ENV)
                && !started.env.contains_key(NO_PROXY_ENV),
            "{name}: no proxy env: {:?}",
            started.env
        );
        assert!(outcome.denied_egress.is_empty(), "{name}: no denials");
        assert!(
            !p.egress_denials_file().exists(),
            "{name}: no denial file is created"
        );
    }
}

/// A proxy that cannot start (its denial file path is blocked by a directory)
/// fails the run CLOSED — before any session spawn.
#[tokio::test]
async fn run_session_fs_net_proxy_start_failure_fails_closed_before_spawn() {
    let dir = tempfile::tempdir().unwrap();
    let p = paths(dir.path());
    let mut log = seeded_log(&p);
    std::fs::create_dir_all(p.runs_dir()).unwrap();
    // Block the denial file path with a directory: opening it for append
    // fails, so the proxy start fails.
    std::fs::create_dir(p.egress_denials_file()).unwrap();

    // A script IS queued: if the run wrongly proceeded to spawn, it would
    // succeed — so reaching backend.start is distinguishable from failing
    // closed.
    let backend =
        MockBackend::with_scripts(vec![MockScript::single_shot_json(&worker_report_json())]);
    let mut spec = session_spec(PromptMode::SingleShot("task".to_string()));
    spec.sandbox = Some(fs_net_sandbox(SandboxBackend::Seatbelt, vec![], dir.path()));

    let err = run_session(
        &backend,
        spec,
        &mut log,
        &p,
        worker_meta("run-closed"),
        None,
    )
    .await
    .expect_err("a proxy start failure must error the run");

    let message = err.to_string();
    assert!(message.contains("egress proxy"), "{message}");
    assert!(
        message.contains("refusing to run without enforcement"),
        "{message}"
    );
    assert!(
        backend.started_specs().is_empty(),
        "fail-closed means the session never spawns"
    );
}

// ---------------------------------------------------------------------------
// Hook gate projection (ticket claude-code-hook-gate-projection, KRZ-302)
// ---------------------------------------------------------------------------

/// A worker run with a declared touch set gets the out-of-contract write
/// rule projected onto its per-session settings (the PreToolUse hook block),
/// and the engine-written spec file lands under the session-private scratch
/// root. The mock session never invokes the hook, so NO records exist and
/// nothing folds — a silent hook is not a failure (the engine-side sweep is
/// the authoritative layer).
#[tokio::test]
async fn hook_gate_projection_worker_run_projects_hook_settings_and_spec_file() {
    let dir = tempfile::tempdir().unwrap();
    let p = paths(dir.path());
    let mut log = seeded_log(&p);
    let cfg = MissionConfig::default();

    let backend =
        MockBackend::with_scripts(vec![MockScript::single_shot_json(&worker_report_json())]);
    let touch_set = vec!["src/**".to_string(), "!src/generated/**".to_string()];
    let outcome = run_worker(
        &backend,
        &mut log,
        &p,
        &cfg,
        &feature(),
        "ship the auth system",
        "Auth",
        None,
        None,
        None,
        &[],
        &[],
        &[],
        AuthVerdict::Inconclusive,
        &touch_set,
        None,
        None,
    )
    .await
    .unwrap();
    assert_eq!(outcome.result, RunResult::Pass);

    let specs = backend.started_specs();
    assert_eq!(specs.len(), 1);
    let spec = &specs[0];

    // The per-session settings carry the hooks block in the documented
    // schema shape (hook_gates module docs): PreToolUse, the write-tool
    // matcher, one command handler naming `hook-guard --config <spec>`.
    let settings = spec.settings_json.as_ref().expect("hook settings set");
    let group = &settings["hooks"]["PreToolUse"][0];
    assert_eq!(group["matcher"], json!("Write|Edit|MultiEdit|NotebookEdit"));
    let command = group["hooks"][0]["command"].as_str().unwrap();
    assert!(
        command.contains("hook-guard") && command.contains("--config"),
        "the hook command invokes the guard subcommand: {command}"
    );
    assert!(group["hooks"][0]["timeout"].is_number());

    // The spec file it points at exists, under the session-private scratch
    // root, carrying the touch set verbatim and the session cwd.
    let spec_path = kranz_engine::hook_gates::spec_file(&spec.session_id);
    assert!(
        spec_path.starts_with(kranz_engine::backend_claude::scratch_home_root(
            &spec.session_id
        )),
        "the spec file lives under the session-private scratch root"
    );
    let written: serde_json::Value =
        serde_json::from_str(&std::fs::read_to_string(&spec_path).unwrap()).unwrap();
    assert_eq!(written["touchSet"], json!(touch_set));
    assert_eq!(
        written["sessionCwd"],
        json!(p.repo_root.display().to_string())
    );
    assert_eq!(
        written["recordFile"],
        json!(kranz_engine::hook_gates::record_file(&spec.session_id)
            .display()
            .to_string())
    );
    assert!(
        command.contains(&spec_path.display().to_string()),
        "the hook command names the written spec file: {command}"
    );

    // The hook never fired in the mock session: no records, so no
    // hook.gate.fired events — and the run still completes normally.
    drop(log);
    let events = read_log(&p);
    assert!(
        !events
            .iter()
            .any(|e| matches!(&e.kind, EventKind::HookGateFired { .. })),
        "no records means no hook.gate.fired events"
    );
    assert!(events
        .iter()
        .any(|e| matches!(&e.kind, EventKind::WorkerCompleted { .. })));

    let _ = std::fs::remove_dir_all(kranz_engine::backend_claude::scratch_home_root(
        &spec.session_id,
    ));
}

/// Regression: an EMPTY touch set is advisory-off (the sweep's posture) —
/// the worker spec is byte-for-byte the pre-projection shape (no
/// settings_json, no spec file), which is also exactly how sessions on
/// backends without hook support behave (they ignore settings_json).
#[tokio::test]
async fn hook_gate_projection_empty_touch_set_leaves_worker_spec_unchanged() {
    let dir = tempfile::tempdir().unwrap();
    let p = paths(dir.path());
    let mut log = seeded_log(&p);
    let cfg = MissionConfig::default();

    let backend =
        MockBackend::with_scripts(vec![MockScript::single_shot_json(&worker_report_json())]);
    run_worker(
        &backend,
        &mut log,
        &p,
        &cfg,
        &feature(),
        "ship the auth system",
        "Auth",
        None,
        None,
        None,
        &[],
        &[],
        &[],
        AuthVerdict::Inconclusive,
        &[],
        None,
        None,
    )
    .await
    .unwrap();

    let specs = backend.started_specs();
    assert_eq!(specs.len(), 1);
    assert!(
        specs[0].settings_json.is_none(),
        "an empty touch set projects nothing"
    );
    assert!(!kranz_engine::hook_gates::spec_file(&specs[0].session_id).exists());
}

/// A gate-failing action inside the session surfaces as a structured
/// `hook.gate.fired` event BEFORE `worker.completed` — the record file the
/// guard wrote (here pre-seeded exactly as `kranz hook-guard` writes it)
/// folds into the log at session end, with the run id stamped from the run.
#[tokio::test]
async fn hook_gate_projection_records_fold_into_the_log_before_worker_completed() {
    let dir = tempfile::tempdir().unwrap();
    let p = paths(dir.path());
    let mut log = seeded_log(&p);

    // A unique session id (never the shared fixture id) so the record file
    // cannot collide with another parallel test's scratch state.
    let mut spec = session_spec(PromptMode::SingleShot("task".to_string()));
    spec.session_id = format!("hook-gate-projection-{}", uuid::Uuid::new_v4());
    let session_id = spec.session_id.clone();

    let gate_spec = kranz_engine::hook_gates::HookGateSpec {
        version: kranz_engine::hook_gates::SPEC_VERSION,
        gate: kranz_engine::hook_gates::HOOK_GATE_ID.to_string(),
        session_cwd: p.repo_root.clone(),
        touch_set: vec!["src/**".to_string()],
        record_file: kranz_engine::hook_gates::record_file(&session_id),
    };
    kranz_engine::hook_gates::HookGateRecord::blocked(
        &gate_spec,
        "PreToolUse",
        "Write",
        "docs/oops.md",
        "matches none of the declared touch-set globs",
        Some("cli-session-1"),
        Some("toolu_1"),
    )
    .append_to(&kranz_engine::hook_gates::record_file(&session_id))
    .unwrap();

    let backend =
        MockBackend::with_scripts(vec![MockScript::single_shot_json(&worker_report_json())]);
    let outcome = run_session(&backend, spec, &mut log, &p, worker_meta("run-hook"), None)
        .await
        .unwrap();
    // The hook verdict never changes the run's own result mapping —
    // record-only evidence, with the sweep authoritative.
    assert_eq!(outcome.result, RunResult::Pass);

    drop(log);
    let events = read_log(&p);
    let types = event_types(&events);
    let fired_at = types
        .iter()
        .position(|t| *t == "hook.gate.fired")
        .expect("the hook record folded into an event: {types:?}");
    let completed_at = types
        .iter()
        .position(|t| *t == "worker.completed")
        .expect("worker.completed: {types:?}");
    assert!(
        fired_at < completed_at,
        "hook.gate.fired must land BEFORE worker.completed: {types:?}"
    );
    match &events[fired_at].kind {
        EventKind::HookGateFired {
            run_id,
            gate,
            hook_event,
            tool,
            subject,
            verdict,
            detail,
        } => {
            assert_eq!(run_id, "run-hook", "the run id is engine-stamped");
            assert_eq!(gate, "out-of-contract-write");
            assert_eq!(hook_event, "PreToolUse");
            assert_eq!(tool, "Write");
            assert_eq!(subject, "docs/oops.md");
            assert_eq!(verdict, "blocked");
            assert_eq!(
                detail.as_deref(),
                Some("matches none of the declared touch-set globs")
            );
        }
        other => panic!("expected hook.gate.fired, got {other:?}"),
    }

    let _ = std::fs::remove_dir_all(kranz_engine::backend_claude::scratch_home_root(&session_id));
}

/// The authoritative layer is unchanged: a worker that bypasses the hook
/// entirely (here: the write lands via a scripted commit — the Bash-write
/// shape a PreToolUse Write hook never sees) produces NO hook.gate.fired
/// events, and the engine-side out-of-contract sweep still flags the path
/// afterwards. Mirrors the orchestrator's
/// `out_of_contract_sweep_flags_path_outside_touch_set` fixture, which must
/// also keep firing untouched.
#[tokio::test]
async fn hook_gate_projection_bypassed_failure_still_caught_by_the_sweep() {
    let dir = tempfile::tempdir().unwrap();
    let root = dir.path();
    // A real repo so the scripted commit lands; `.kranz/` ignored so the
    // mission's own bookkeeping never enters the sweep's candidate set.
    let git = |args: &[&str]| {
        let output = std::process::Command::new("git")
            .args(args)
            .current_dir(root)
            .output()
            .expect("git spawns");
        assert!(
            output.status.success(),
            "git {args:?} failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );
        output
    };
    git(&["init", "-q"]);
    std::fs::write(root.join(".gitignore"), ".kranz/\n").unwrap();
    git(&["add", ".gitignore"]);
    git(&[
        "-c",
        "user.name=test",
        "-c",
        "user.email=test@example.com",
        "commit",
        "-q",
        "-m",
        "init",
    ]);
    let base_sha = String::from_utf8_lossy(&git(&["rev-parse", "HEAD"]).stdout)
        .trim()
        .to_string();

    let p = paths(root);
    let mut log = seeded_log(&p);
    let cfg = MissionConfig::default();
    let touch_set = vec!["src/**".to_string()];

    // The hook-bypass shape: the worker's out-of-contract write lands via a
    // commit the Write hook never sees; NO record file ever exists.
    let script = MockScript::single_shot_json(&worker_report_json())
        .writes_file("docs/oops.md", "written past the hook\n")
        .commits_all("[f-1] add login (and a sneaky note)");
    let backend = MockBackend::with_scripts(vec![script]);
    let outcome = run_worker(
        &backend,
        &mut log,
        &p,
        &cfg,
        &feature(),
        "ship the auth system",
        "Auth",
        None,
        None,
        None,
        &[],
        &[],
        &[],
        AuthVerdict::Inconclusive,
        &touch_set,
        None,
        None,
    )
    .await
    .unwrap();
    assert_eq!(outcome.result, RunResult::Pass);

    // Hook bypassed ⇒ no in-process evidence…
    drop(log);
    let events = read_log(&p);
    assert!(
        !events
            .iter()
            .any(|e| matches!(&e.kind, EventKind::HookGateFired { .. })),
        "a bypassed hook leaves no hook.gate.fired events"
    );

    // …and the engine-side sweep still catches the out-of-contract write,
    // via the same commit-range + path_findings composition the
    // orchestrator's out_of_contract_sweep runs.
    let diff = std::process::Command::new("git")
        .args(["diff", "--name-only", &format!("{base_sha}..HEAD")])
        .current_dir(root)
        .output()
        .unwrap();
    assert!(diff.status.success());
    let commit = kranz_engine::git_ops::CommitInfo {
        sha: "HEAD".to_string(),
        subject: "[f-1] add login (and a sneaky note)".to_string(),
    };
    let diff_stdout = String::from_utf8_lossy(&diff.stdout);
    let changes: Vec<kranz_engine::contract_sweep::AttributedChange> = diff_stdout
        .lines()
        .map(|path| kranz_engine::contract_sweep::AttributedChange {
            path,
            commit: &commit,
        })
        .collect();
    assert_eq!(changes.len(), 1, "only the bypassed write was committed");
    let findings = kranz_engine::contract_sweep::path_findings(&touch_set, &changes);
    assert_eq!(findings.len(), 1, "the sweep still fires: {findings:?}");
    assert_eq!(findings[0].subject, "docs/oops.md");
    assert_eq!(
        findings[0].class,
        kranz_engine::contract_sweep::FINDING_CLASS
    );
}

// ---------------------------------------------------------------------------
// Flight Rules stage projections (ticket flight-rules-workflow-projection,
// KRZ-345, design D-G): worker/validator sessions receive only their stage's
// rules from the approved pin, inside the marked untrusted boundary, and the
// recorded prompt hash covers the exact projection. Anti-vacuity prefix
// `flight_rules_projection_` (grep-verified unique to this ticket's tests).
// ---------------------------------------------------------------------------

/// A hand-built approved pin spanning every stage: implementation
/// (ZZ-IMPL-001, enforced must), validation (ZZ-VAL-001, approved should),
/// planning-only (ZZ-PLAN-001) and merge-only (ZZ-MERGE-001) rules that must
/// never reach a worker/validator session.
fn standards_pin() -> kranz_engine::types::StandardsPin {
    fn rule(
        id: &str,
        rfc: &str,
        revision: u64,
        level: &str,
        status: &str,
        stages: &[&str],
        checker: Option<&str>,
    ) -> kranz_engine::types::PinnedRule {
        kranz_engine::types::PinnedRule {
            id: id.to_string(),
            revision,
            rfc: rfc.to_string(),
            level: level.to_string(),
            effective_status: status.to_string(),
            statement: format!("zz statement for {id}."),
            domains: vec!["zz".to_string()],
            stages: stages.iter().map(|s| s.to_string()).collect(),
            when_paths: vec![],
            task_classes: vec![],
            checker: checker.map(str::to_string),
            waivable: false,
        }
    }
    kranz_engine::types::StandardsPin {
        pack_name: "zz-pack".to_string(),
        pack_dir: "vendor/pack".to_string(),
        standards_root: "standards".to_string(),
        digest: "ab".repeat(32),
        source: kranz_engine::types::StandardsPinSource::RepoTracked,
        task_class: None,
        touch_set: vec!["crates/**".to_string()],
        context_paths: Vec::new(),
        gates: Vec::new(),
        rules: vec![
            rule(
                "ZZ-IMPL-001",
                "RFC-002",
                2,
                "must",
                "enforced",
                &["implementation", "validation"],
                Some("gate:zz-gate"),
            ),
            rule(
                "ZZ-MERGE-001",
                "RFC-002",
                1,
                "must",
                "enforced",
                &["merge"],
                Some("gate:zz-gate"),
            ),
            rule(
                "ZZ-PLAN-001",
                "RFC-001",
                1,
                "should",
                "approved",
                &["planning"],
                Some("agent-judgement"),
            ),
            rule(
                "ZZ-VAL-001",
                "RFC-001",
                1,
                "should",
                "approved",
                &["validation"],
                Some("agent-judgement"),
            ),
        ],
    }
}

/// The `worker.spawned` prompt hash recorded in the log (the session
/// provenance the projection must be covered by).
fn spawned_prompt_hash(p: &MissionPaths) -> String {
    read_log(p)
        .iter()
        .find_map(|e| match &e.kind {
            EventKind::WorkerSpawned { prompt_hash, .. } => Some(prompt_hash.clone()),
            _ => None,
        })
        .expect("worker.spawned recorded")
}

#[tokio::test]
async fn flight_rules_projection_worker_prompt_projects_implementation_stage_only() {
    let dir = tempfile::tempdir().unwrap();
    let p = paths(dir.path());
    let mut log = seeded_log(&p);
    let cfg = MissionConfig::default();
    let pin = standards_pin();
    let backend =
        MockBackend::with_scripts(vec![MockScript::single_shot_json(&worker_report_json())]);
    let outcome = run_worker(
        &backend,
        &mut log,
        &p,
        &cfg,
        &feature(),
        "goal",
        "milestone",
        None,
        None,
        None,
        &[],
        &[],
        &[],
        AuthVerdict::Inconclusive,
        &[],
        None,
        Some(&pin),
    )
    .await
    .unwrap();
    assert_eq!(outcome.result, RunResult::Pass);

    let specs = backend.started_specs();
    let prompt = specs[0]
        .append_system_prompt
        .as_deref()
        .expect("role prompt");
    // Only the implementation-stage rule projects, labelled with its source,
    // inside the marked untrusted boundary naming both digests.
    assert!(prompt.contains("`ZZ-IMPL-001` r2"), "{prompt}");
    for absent in ["ZZ-PLAN-001", "ZZ-VAL-001", "ZZ-MERGE-001"] {
        assert!(
            !prompt.contains(absent),
            "{absent} must never reach the worker: {prompt}"
        );
    }
    assert!(prompt.contains("worker projection"), "{prompt}");
    assert!(prompt.contains("untrusted content boundary"), "{prompt}");
    assert!(
        prompt.contains("cannot register tools, commands, grants, or permissions"),
        "{prompt}"
    );
    assert!(
        prompt.contains(&format!("sha256:{}", pin.digest)),
        "{prompt}"
    );
    assert!(prompt.contains("projection digest `sha256:"), "{prompt}");
    assert!(
        prompt.contains("source: pack `zz-pack` root `standards`, RFC `RFC-002`"),
        "{prompt}"
    );
    // Honest labels: the enforced MUST is the only blocking-capable rule.
    assert!(prompt.contains("may block through its checker"), "{prompt}");
    assert!(
        prompt.contains("an approved rule can never block"),
        "{prompt}"
    );

    // The recorded session prompt hash covers the exact projection text —
    // replay identifies the manifest/projection digest from the header.
    drop(log);
    let recorded = spawned_prompt_hash(&p);
    assert_eq!(recorded, kranz_engine::prompts::hash_text(prompt));
    assert_ne!(
        recorded,
        kranz_engine::prompts::hash(Role::Worker),
        "the extended prompt must hash differently from the bare template"
    );
}

#[tokio::test]
async fn flight_rules_projection_validator_prompts_project_validation_stage_only() {
    for (role, surface) in [
        (Role::ValidatorScrutiny, "validator-scrutiny projection"),
        (Role::ValidatorFunctional, "validator-functional projection"),
    ] {
        let dir = tempfile::tempdir().unwrap();
        let p = paths(dir.path());
        let mut log = seeded_log(&p);
        let cfg = MissionConfig::default();
        let pin = standards_pin();
        let backend = MockBackend::with_scripts(vec![MockScript::single_shot_json(
            &json!({ "findings": [], "summary": "all good" }),
        )]);
        run_validator(
            &backend,
            &mut log,
            &p,
            &cfg,
            role,
            &milestone(),
            &[],
            "abc123",
            None,
            None,
            &[],
            &[],
            &[],
            None,
            Some(&pin),
        )
        .await
        .unwrap();

        let specs = backend.started_specs();
        let prompt = specs[0]
            .append_system_prompt
            .as_deref()
            .expect("role prompt");
        // Validation-stage rules only, in stable id order.
        assert!(prompt.contains("`ZZ-IMPL-001` r2"), "{role:?}: {prompt}");
        assert!(prompt.contains("`ZZ-VAL-001` r1"), "{role:?}: {prompt}");
        for absent in ["ZZ-PLAN-001", "ZZ-MERGE-001"] {
            assert!(
                !prompt.contains(absent),
                "{absent} must never reach a validator: {prompt}"
            );
        }
        assert!(
            prompt.find("ZZ-IMPL-001").unwrap() < prompt.find("ZZ-VAL-001").unwrap(),
            "stable id order: {prompt}"
        );
        assert!(prompt.contains(surface), "{role:?}: {prompt}");
        // The approved SHOULD is labelled advisory, never blocking.
        let val_line = prompt
            .lines()
            .find(|l| l.contains("ZZ-VAL-001"))
            .expect("the approved rule line");
        assert!(val_line.contains("approved should"), "{val_line}");
        assert!(val_line.contains("advisory — cannot block"), "{val_line}");

        drop(log);
        let recorded = spawned_prompt_hash(&p);
        assert_eq!(
            recorded,
            kranz_engine::prompts::hash_text(prompt),
            "{role:?}: the recorded hash must cover the exact projection"
        );
    }
}

#[tokio::test]
async fn flight_rules_projection_no_pin_keeps_prompt_and_hash_byte_identical() {
    let dir = tempfile::tempdir().unwrap();
    let p = paths(dir.path());
    let mut log = seeded_log(&p);
    let cfg = MissionConfig::default();
    let backend =
        MockBackend::with_scripts(vec![MockScript::single_shot_json(&worker_report_json())]);
    run_worker(
        &backend,
        &mut log,
        &p,
        &cfg,
        &feature(),
        "goal",
        "milestone",
        None,
        None,
        None,
        &[],
        &[],
        &[],
        AuthVerdict::Inconclusive,
        &[],
        None,
        None,
    )
    .await
    .unwrap();

    let specs = backend.started_specs();
    let prompt = specs[0]
        .append_system_prompt
        .as_deref()
        .expect("role prompt");
    assert!(
        !prompt.contains("Flight Rules"),
        "no pin ⇒ nothing appended: {prompt}"
    );
    drop(log);
    assert_eq!(
        spawned_prompt_hash(&p),
        kranz_engine::prompts::hash(Role::Worker),
        "no pin ⇒ the recorded hash is the bare template hash, as before"
    );
}