kranz-server 0.3.0

REST and WebSocket mission host for Kranz.
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
//! REST handlers (docs/protocol.md "REST" table). Every handler re-reads
//! from disk — no caching; the engine process owns truth.

use crate::error::ApiError;
use crate::read_work::ReadWork;
use crate::ServerState;
use axum::body::Bytes;
use axum::extract::{Path as UrlPath, Query, State};
use axum::http::StatusCode;
use axum::response::IntoResponse;
use axum::{Extension, Json};
use kranz_engine::contract_lint::ContractLintReport;
use kranz_engine::cost;
use kranz_engine::event_log::EventLog;
use kranz_engine::events::{Event, EventKind};
use kranz_engine::merged::merged_bit;
use kranz_engine::orchestrator::mission_worktree_path;
use kranz_engine::paths::MissionPaths;
use kranz_engine::preflight::PREFLIGHT_CLEAR_SUMMARY;
use kranz_engine::reducer;
use kranz_engine::report_render::render_plan_markdown;
use kranz_engine::types::{
    ControlCommand, MissionState, MissionStatus, RoleConfig, SandboxEnforce, WorkerIsolation,
};
use kranz_engine::{config, control};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::collections::HashMap;
use std::io::ErrorKind;
use std::path::Path;
use std::sync::Arc;

/// `GET /api/health` → `{"ok":true,"version":"<crate version>"}`.
pub(crate) async fn health() -> Json<Value> {
    Json(json!({ "ok": true, "version": env!("CARGO_PKG_VERSION") }))
}

/// `GET /api/missions` — fold each mission's log into a summary row. A
/// corrupt (or unreadable) log yields that entry with `"status": "failed"`
/// and an `"error"` field instead of failing the whole list.
pub(crate) async fn list_missions(
    Extension(reads): Extension<ReadWork>,
    State(server): State<Arc<ServerState>>,
) -> Result<Json<Value>, ApiError> {
    reads
        .run(move || {
            let index_contents = read_missions_index_sync(&server.repo_root);
            let mut ids = MissionPaths::list_missions(&server.repo_root);
            for id in kranz_engine::mission_catalog::mission_index_ids(&index_contents) {
                if !ids.contains(&id) {
                    ids.push(id);
                }
            }
            ids.sort();
            // Opened once for the whole list; a failure here just means every row's
            // `merged` degrades to null (no git ancestry to probe).
            let repo = kranz_engine::git_ops::GitRepo::open(&server.repo_root).ok();
            let mut rows = Vec::new();
            for id in ids {
                let paths = MissionPaths::new(&server.repo_root, &id);
                if !paths.events_file().is_file() {
                    rows.push(json!({
                        "id": id,
                        "status": "deleted",
                        "goal": "deleted mission (no data recorded)",
                    }));
                    continue;
                }
                // The events file exists — but trust it only when no path component
                // is a symlink (P1 mission-path-no-follow): a symlinked mission dir
                // surfaces as a clear error row, never a read into another
                // repository's tree.
                if let Err(error) = paths.require_no_follow() {
                    rows.push(json!({ "id": id, "status": "failed", "error": error.to_string() }));
                    continue;
                }
                let row = match fold_log(&paths) {
                    Ok(state) => {
                        let merged = repo
                            .as_ref()
                            .and_then(|repo| merged_bit(repo, &state.mission));
                        json!({
                            "id": id,
                            "status": state.mission.status,
                            "goal": state.mission.goal,
                            "createdAt": state.mission.created_at,
                            "merged": merged,
                        })
                    }
                    Err(error) => json!({ "id": id, "status": "failed", "error": error }),
                };
                rows.push(row);
            }
            Ok(Json(Value::Array(rows)))
        })
        .await
}

/// `GET /api/missions/outcomes` — flight-surgeon outcomes fold (autonomy
/// ratio, grant-latency distribution, escalation ledger), computed
/// per-request from the event logs by [`kranz_engine::outcomes::compute_outcomes`].
/// No caching, no second source of truth.
pub(crate) async fn mission_outcomes(
    Extension(reads): Extension<ReadWork>,
    State(server): State<Arc<ServerState>>,
) -> Result<Json<kranz_engine::outcomes::Outcomes>, ApiError> {
    reads
        .run(move || {
            let outcomes = kranz_engine::outcomes::compute_outcomes(&server.repo_root)
                .map_err(|e| ApiError::internal(e.to_string()))?;
            Ok(Json(outcomes))
        })
        .await
}

/// `GET /api/escalation-metrics` — the flight-surgeon console (ticket
/// `flight-surgeon-dashboard`): autonomy ratio split by outcome, the
/// rubber-stamp signal (park→grant p50/p90 + sub-10s count), false greens
/// (completed missions joined against `traced-from-mission` defect tickets),
/// and the escalation ledger. Computed per-request from the event logs and
/// ticket frontmatter by
/// [`kranz_engine::escalation_metrics::compute_escalation_metrics`]. Read-gated
/// like every other GET; no caching, no second source of truth.
pub(crate) async fn escalation_metrics(
    Extension(reads): Extension<ReadWork>,
    State(server): State<Arc<ServerState>>,
) -> Result<Json<kranz_engine::escalation_metrics::EscalationMetrics>, ApiError> {
    reads
        .run(move || {
            let metrics =
                kranz_engine::escalation_metrics::compute_escalation_metrics(&server.repo_root)
                    .map_err(|e| ApiError::internal(e.to_string()))?;
            Ok(Json(metrics))
        })
        .await
}

/// Cross-mission Flight Rules effectiveness fold (KRZ-348). Read-only and
/// recomputed from event logs plus traced defect ticket links on every call.
pub(crate) async fn standards_metrics(
    Extension(reads): Extension<ReadWork>,
    State(server): State<Arc<ServerState>>,
) -> Result<Json<kranz_engine::standards_metrics::StandardsMetricsReport>, ApiError> {
    reads
        .run(move || {
            Ok(Json(kranz_engine::standards_metrics::compute(
                &server.repo_root,
            )?))
        })
        .await
}

/// `GET /api/cost-per-merged-change?windowDays=30` — cost per merged change
/// for the served repo (ticket `cost-per-merged-change`, KRZ-329): the cost
/// fold over missions closed in the window beside the merged-change count
/// derived at fold time (merged.rs's landed/ancestry probe — never stored)
/// and the window's autonomy ratio. `windowDays` defaults to
/// [`kranz_engine::outcomes::DEFAULT_MERGED_CHANGE_WINDOW_DAYS`] and is
/// capped at [`kranz_engine::outcomes::MAX_MERGED_CHANGE_WINDOW_DAYS`] —
/// over the cap is a 400, never a wrapped/panicked handler (12th-pass
/// review); the same folded JSON the CLI's `kranz outcomes --all`
/// aggregates per catalog repo.
pub(crate) async fn cost_per_merged_change(
    Extension(reads): Extension<ReadWork>,
    State(server): State<Arc<ServerState>>,
    Query(params): Query<HashMap<String, String>>,
) -> Result<Json<kranz_engine::outcomes::CostPerMergedChange>, ApiError> {
    reads
        .run(move || {
            let window_days = match params.get("windowDays") {
                Some(raw) => {
                    let parsed = raw.parse::<u64>().map_err(|_| {
                        ApiError::bad_request("windowDays must be a non-negative integer")
                    })?;
                    if parsed > kranz_engine::outcomes::MAX_MERGED_CHANGE_WINDOW_DAYS {
                        return Err(ApiError::bad_request(format!(
                            "windowDays must be at most {} days",
                            kranz_engine::outcomes::MAX_MERGED_CHANGE_WINDOW_DAYS
                        )));
                    }
                    parsed
                }
                None => kranz_engine::outcomes::DEFAULT_MERGED_CHANGE_WINDOW_DAYS,
            };
            let report = kranz_engine::outcomes::compute_cost_per_merged_change(
                &server.repo_root,
                window_days,
                chrono::Utc::now(),
            )
            .map_err(|e| ApiError::internal(e.to_string()))?;
            Ok(Json(report))
        })
        .await
}

/// `<repo>/.kranz/missions/index.md` contents, or `""` if the file is absent
/// (never created here — callers only read the catalog).
///
/// No-follow, like every other repo file this crate reads: the catalog lives
/// in a worker-writable tree and a symlinked `index.md` must read as absent,
/// not as whatever it points at.
fn read_missions_index_sync(repo_root: &Path) -> String {
    let path = MissionPaths::new(repo_root, "_")
        .missions_dir()
        .join("index.md");
    read_file_or_404_sync(&path, "no mission index".into()).unwrap_or_default()
}

/// `GET /api/missions/:id/state` — full [`MissionState`], folded from
/// events.jsonl (NOT the state.json cache). 404 for an unknown mission.
pub(crate) async fn mission_state(
    Extension(reads): Extension<ReadWork>,
    State(server): State<Arc<ServerState>>,
    UrlPath(id): UrlPath<String>,
) -> Result<Json<MissionState>, ApiError> {
    reads
        .run(move || {
            let paths = mission_paths(&server, &id)?;
            let events_path = paths.events_file();
            if !events_path.is_file() {
                return Err(unknown_mission(&id));
            }
            let events = EventLog::read_events(&events_path)?;
            Ok(Json(reducer::fold(&events)?))
        })
        .await
}

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct StandardsWaiverCandidate {
    rule: kranz_engine::types::PinnedRule,
    finding_subject: String,
    finding_evidence: String,
    run_id: String,
}

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct MissionStandardsView {
    #[serde(skip_serializing_if = "Option::is_none")]
    manifest: Option<kranz_engine::types::StandardsPin>,
    #[serde(skip_serializing_if = "Option::is_none")]
    coverage: Option<kranz_engine::standards_coverage::StandardsCoverage>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    waiver_candidates: Vec<StandardsWaiverCandidate>,
}

fn fold_standards_view(
    id: &str,
    events: &[Event],
) -> kranz_engine::error::Result<MissionStandardsView> {
    let state = reducer::fold(events)?;
    let manifest = state.mission.standards_manifest.clone();
    let coverage = kranz_engine::standards_coverage::standards_coverage(id, events);
    let mut waiver_candidates = Vec::new();
    if let (Some(pin), Some(coverage)) = (manifest.as_ref(), coverage.as_ref()) {
        for row in coverage.rules.iter().filter(|row| {
            row.disposition == kranz_engine::standards_coverage::RuleDisposition::Failed
        }) {
            let Some(rule) = pin
                .rules
                .iter()
                .find(|rule| rule.id == row.id && rule.revision == row.revision && rule.waivable)
            else {
                continue;
            };
            if let Some((finding_subject, finding_evidence, run_id)) = events
                .iter()
                .filter(|event| event.mission_id == id)
                .rev()
                .find_map(|event| match &event.kind {
                    EventKind::ValidationFinding {
                        finding, run_id, ..
                    } if finding.rule.as_ref().is_some_and(|citation| {
                        citation.id == rule.id
                            && citation.revision == rule.revision
                            && citation.digest == pin.digest
                    }) =>
                    {
                        Some((
                            finding.subject.clone(),
                            finding.evidence.clone(),
                            run_id.clone(),
                        ))
                    }
                    _ => None,
                })
            {
                waiver_candidates.push(StandardsWaiverCandidate {
                    rule: rule.clone(),
                    finding_subject,
                    finding_evidence,
                    run_id,
                });
            }
        }
    }
    Ok(MissionStandardsView {
        manifest,
        coverage,
        waiver_candidates,
    })
}

/// Typed Flight Rules read model. No configured standards is represented by
/// `{}` so old missions add no empty warning surface.
pub(crate) async fn mission_standards(
    Extension(reads): Extension<ReadWork>,
    State(server): State<Arc<ServerState>>,
    UrlPath(id): UrlPath<String>,
) -> Result<Json<MissionStandardsView>, ApiError> {
    reads
        .run(move || {
            let paths = mission_paths(&server, &id)?;
            if !paths.events_file().is_file() {
                return Err(unknown_mission(&id));
            }
            let events = EventLog::read_events(&paths.events_file())?;
            Ok(Json(fold_standards_view(&id, &events)?))
        })
        .await
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct StandardsWaiverBody {
    rule_id: String,
    #[serde(default)]
    revision: Option<u64>,
    #[serde(default)]
    finding_subject: Option<String>,
    reason: String,
    expires_at: String,
}

/// Authenticated REST twin of `kranz standards waive`. It intentionally
/// refuses while the mission engine holds the append lock; the UI keeps the
/// evidence visible and tells the operator to pause/stop before retrying.
pub(crate) async fn post_standards_waiver(
    State(server): State<Arc<ServerState>>,
    UrlPath(id): UrlPath<String>,
    Json(body): Json<StandardsWaiverBody>,
) -> Result<Json<Value>, ApiError> {
    mission_paths(&server, &id)?;
    let expires_at = chrono::DateTime::parse_from_rfc3339(&body.expires_at)
        .map_err(|error| ApiError::bad_request(format!("expiresAt must be RFC 3339: {error}")))?
        .with_timezone(&chrono::Utc);
    let request = kranz_engine::standards_waiver::WaiverRequest {
        rule_id: body.rule_id,
        revision: body.revision,
        finding_subject: body.finding_subject,
        reason: body.reason,
        expires_at,
    };
    let outcome = kranz_engine::standards_waiver::approve_standards_waiver(
        &server.repo_root,
        &id,
        &request,
        "rest",
        kranz_engine::event_log::LockForce::No,
    )
    .map_err(|error| match error {
        kranz_engine::error::EngineError::LockHeld(_) => ApiError::conflict(format!(
            "waiver refused: mission '{id}' is still running; pause or stop it before approving this exception"
        )),
        other => ApiError::unprocessable(format!("waiver refused: {other}")),
    })?;
    Ok(Json(json!({
        "recorded": true,
        "seq": outcome.event.seq,
        "rule": outcome.rule,
        "findingSubject": outcome.finding_subject,
        "findingEvidence": outcome.finding_evidence,
        "runId": outcome.run_id,
        "affectedPaths": outcome.affected_paths,
        "diffDigest": outcome.diff_digest,
        "findingFingerprint": outcome.finding_fingerprint,
    })))
}

/// `GET /api/missions/:id/workspace` — effective local execution workspace,
/// sandbox tiers, and the latest already-recorded environment-preflight
/// outcome. This is a derived read model: no new durable state or events.
///
/// Additive workspace-provider fields (design D-B/D-E, ticket
/// workspace-provider-pin-at-approval):
/// - `pin` — the provider identity pinned at plan approval, mirroring
///   `MissionState.workspace_pin` (`{provider, template, version}`); `null`
///   on missions approved before pinning existed.
/// - `previews` — the contract's preview URL templates `[{name,
///   urlTemplate}]`, placeholders UNFILLED, and only once a readiness pass is
///   on the log (never a fabricated URL for unproven services); `null`
///   otherwise.
/// - `takeover` — how a human takes over the workspace. For local-worktree
///   this is the plain truth (work locally in the workspace cwd) — no SSH/
///   remote fiction; for `remote` it is the substrate-reported takeover URL
///   (SSH/web) from the latest `workspace.provisioned` event; `null` when no
///   pin names the provider or a remote mission has not provisioned yet.
/// - `workspaceLifecycle` — the last known PROVIDER lifecycle transition
///   (ticket `workspace-idle-hibernate`), folded from `workspace.teardown`
///   events carrying a `state`: `{state: "kept"|"stopped"|"destroyed"|
///   "failed", ts}` with the transition event's own timestamp — the
///   workspace-hours anchor for cost tooling. `null` on logs without a
///   state-carrying teardown (consumers must degrade on null). DISTINCT
///   from the top-level `lifecycle`, which describes the local execution
///   cwd (worktree active/pending/removed), not the provider workspace.
pub(crate) async fn mission_workspace(
    Extension(reads): Extension<ReadWork>,
    State(server): State<Arc<ServerState>>,
    UrlPath(id): UrlPath<String>,
) -> Result<Json<Value>, ApiError> {
    reads.run(move || {
        let paths = mission_paths(&server, &id)?;
        if !paths.events_file().is_file() {
            return Err(unknown_mission(&id));
        }
        let events = EventLog::read_events(&paths.events_file())?;
        let state = reducer::fold(&events)?;
        let isolation = state.config.isolation();
        let cwd = match isolation {
            WorkerIsolation::Worktree => mission_worktree_path(&server.repo_root, &id),
            WorkerIsolation::Checkout => server.repo_root.clone(),
        };
        let worktree_active = isolation == WorkerIsolation::Worktree && cwd.is_dir();
        let lifecycle = match isolation {
            WorkerIsolation::Checkout => "primary-checkout",
            WorkerIsolation::Worktree if worktree_active => "active",
            WorkerIsolation::Worktree
                if matches!(
                    state.mission.status,
                    MissionStatus::Planning | MissionStatus::Approved
                ) =>
            {
                "pending"
            }
            WorkerIsolation::Worktree => "removed",
        };
        let preflight = events.iter().rev().find_map(|event| match &event.kind {
            EventKind::OrchestratorDecision { summary, .. } if summary.starts_with("preflight:") => {
                Some(json!({
                    "status": if summary == PREFLIGHT_CLEAR_SUMMARY { "clear" } else { "issues" },
                    "summary": summary,
                    "eventSeq": event.seq,
                }))
            }
            _ => None,
        });
        let preflight = preflight.unwrap_or_else(|| {
            let pending = matches!(
                state.mission.status,
                MissionStatus::Planning | MissionStatus::Approved
            );
            json!({
                "status": if pending { "pending" } else { "clear" },
                "summary": if pending {
                    "environment preflight has not run yet"
                } else {
                    "no advisory preflight issues recorded"
                },
                "eventSeq": Value::Null,
            })
        });

        // Workspace contract presence (D-H): base-branch-owned `.kranz/
        // workspace.json` read from the repo root — a derived read like the rest
        // of this projection; an unreadable/invalid contract degrades to absent
        // here (draft/approve is the fail-closed surface).
        let workspace_contract =
            kranz_engine::workspace_contract::load_workspace_contract(&server.repo_root)
                .ok()
                .flatten();

        // Bootstrap/readiness gate outcomes (D-C/D-H), derived from the gate's
        // orchestrator.decision events — ADDITIVE fields: null when there is no
        // contract or the gate has not run yet, and consumers must degrade on
        // null/absent. A later run's outcome supersedes an earlier one (same
        // latest-wins derivation as `preflight` above).
        let gate_outcome = |prefix: &str| {
            events
                .iter()
                .rev()
                .find_map(|event| match &event.kind {
                    EventKind::OrchestratorDecision { summary, .. } if summary.starts_with(prefix) => {
                        Some(json!({
                            "summary": summary,
                            "eventSeq": event.seq,
                        }))
                    }
                    _ => None,
                })
                .unwrap_or(Value::Null)
        };
        let (bootstrap, readiness) = if workspace_contract.is_some() {
            (
                gate_outcome(kranz_engine::workspace_gate::BOOTSTRAP_SUMMARY_PREFIX),
                gate_outcome(kranz_engine::workspace_gate::READINESS_SUMMARY_PREFIX),
            )
        } else {
            (Value::Null, Value::Null)
        };

        // Approval-time provider pin (D-B), mirrored from folded state; null on
        // missions approved before pinning existed — consumers must degrade on
        // null, same as the gate outcome fields above.
        let pin = match &state.workspace_pin {
            Some(pin) => json!(pin),
            None => Value::Null,
        };

        // Preview placeholders (D-E): the contract's URL templates, UNFILLED —
        // surfaced only once a readiness PASS is on the log, never implying a
        // reachable URL while the services behind it are unproven. The REMOTE
        // kind instead surfaces the substrate-reported URLs recorded on
        // `workspace.provisioned` (name-matched, with the substrate's auth
        // report) — same readiness-pass gate, never a fabricated URL.
        let readiness_passed = events.iter().any(|event| {
            matches!(
                &event.kind,
                EventKind::WorkspaceReadinessReport { outcome, .. } if outcome == "ready"
            )
        });
        // The latest remote-kind provisioned event (latest-wins, the same
        // derivation idiom as preflight/gate outcomes above).
        let remote_provision = events.iter().rev().find_map(|event| match &event.kind {
            EventKind::WorkspaceProvisioned {
                provider,
                takeover,
                previews,
                ..
            } if provider == "remote" => Some((takeover.clone(), previews.clone())),
            _ => None,
        });
        let remote_pinned = matches!(
            &state.workspace_pin,
            Some(pin) if pin.provider == "remote"
        );
        let previews = if remote_pinned {
            match (&remote_provision, readiness_passed) {
                (Some((_, Some(previews))), true) if !previews.is_empty() => json!(previews),
                _ => Value::Null,
            }
        } else {
            match (&workspace_contract, readiness_passed) {
                (Some(contract), true) if !contract.previews.is_empty() => {
                    json!(contract
                        .previews
                        .iter()
                        .map(|p| json!({
                            "name": p.name,
                            "urlTemplate": p.url_template,
                        }))
                        .collect::<Vec<_>>())
                }
                _ => Value::Null,
            }
        };

        // Human takeover (D-E): for local-worktree the plain truth — work locally
        // in the workspace cwd, no SSH/remote fiction (the provider isolates
        // source only, D-H). For remote, the substrate's reported SSH/web URL —
        // honest and substrate-sourced — null until a remote provision lands.
        // Keyed to the pin; null when no pin names the provider (older missions)
        // or a future provider has no line yet.
        let takeover = match &state.workspace_pin {
            Some(pin) if pin.provider == "local-worktree" => json!(format!(
                "work locally in the workspace cwd ({})",
                cwd.to_string_lossy()
            )),
            Some(pin) if pin.provider == "remote" => match &remote_provision {
                Some((Some(takeover), _)) => json!(takeover),
                _ => Value::Null,
            },
            _ => Value::Null,
        };

        // The last known provider lifecycle transition (ticket
        // workspace-idle-hibernate), folded from `workspace.teardown` state —
        // additive: null when no teardown carried an outcome; consumers must
        // degrade on null, same as the fields above.
        let workspace_lifecycle = match &state.workspace_lifecycle {
            Some(lifecycle) => json!(lifecycle),
            None => Value::Null,
        };

        Ok(Json(json!({
            "isolation": isolation,
            "cwd": cwd.to_string_lossy(),
            "lifecycle": lifecycle,
            "worktreeActive": worktree_active,
            "sandboxes": [
                sandbox_summary("worker", &state.config.worker),
                sandbox_summary("scrutiny", &state.config.validator_scrutiny),
                sandbox_summary("functional", &state.config.validator_functional),
            ],
            "preflight": preflight,
            "pin": pin,
            "previews": previews,
            "takeover": takeover,
            "workspaceLifecycle": workspace_lifecycle,
            "contract": {
                "present": workspace_contract.is_some(),
                "services": workspace_contract.as_ref().map_or(0, |c| c.services.len()),
                "previews": workspace_contract.as_ref().map_or(0, |c| c.previews.len()),
                "bootstrap": bootstrap,
                "readiness": readiness,
            },
        })))
    })
    .await
}

fn sandbox_summary(role: &str, config: &RoleConfig) -> Value {
    json!({
        "role": role,
        "enforce": sandbox_enforce_label(config.sandbox.enforce),
        "extraWriteCount": config.sandbox.extra_write.len(),
        "egressCount": config.sandbox.egress.len(),
    })
}

fn sandbox_enforce_label(enforce: SandboxEnforce) -> &'static str {
    match enforce {
        SandboxEnforce::Off => "off",
        SandboxEnforce::Fs => "fs",
        SandboxEnforce::FsNet => "fs+net",
    }
}

/// `GET /api/missions/:id/events?since=<seq>` — events with `seq > since`
/// (all events when `since` is omitted).
pub(crate) async fn mission_events(
    Extension(reads): Extension<ReadWork>,
    State(server): State<Arc<ServerState>>,
    UrlPath(id): UrlPath<String>,
    Query(params): Query<HashMap<String, String>>,
) -> Result<Json<Vec<Event>>, ApiError> {
    reads
        .run(move || {
            let paths = mission_paths(&server, &id)?;
            let events_path = paths.events_file();
            if !events_path.is_file() {
                return Err(unknown_mission(&id));
            }
            let since = match params.get("since") {
                None => 0,
                Some(raw) => raw.parse::<u64>().map_err(|_| {
                    ApiError::bad_request(format!("invalid 'since' value: '{raw}'"))
                })?,
            };
            Ok(Json(EventLog::read_events_after(&events_path, since)?))
        })
        .await
}

/// `GET /api/missions/:id/plan` — contents of plan.json; 404 until the plan
/// has been approved (i.e. the file exists).
pub(crate) async fn mission_plan(
    State(server): State<Arc<ServerState>>,
    UrlPath(id): UrlPath<String>,
) -> Result<Json<Value>, ApiError> {
    crate::read_work::run(move || {
        let paths = mission_paths(&server, &id)?;
        let content = read_file_or_404_sync(
            &paths.plan_file(),
            format!("mission '{id}' has no approved plan yet"),
        )?;
        let plan: Value = serde_json::from_str(&content)
            .map_err(|e| ApiError::internal(format!("plan.json is not valid JSON: {e}")))?;
        Ok(Json(plan))
    })
    .await
}

/// `GET /api/missions/:id/plan.md` — rendered plan markdown; 404 until
/// plan.md has been written (alongside plan.json, at approval time).
pub(crate) async fn mission_plan_md(
    State(server): State<Arc<ServerState>>,
    UrlPath(id): UrlPath<String>,
) -> Result<Json<Value>, ApiError> {
    crate::read_work::run(move || {
        let paths = mission_paths(&server, &id)?;
        let markdown = read_file_or_404_sync(
            &paths.plan_md_file(),
            format!("mission '{id}' has no approved plan yet"),
        )?;
        Ok(Json(json!({ "markdown": markdown })))
    })
    .await
}

/// `GET /api/missions/:id/revision-diff` — pending revision review artifact.
/// Returns 404 when no proposed revision is awaiting approval.
pub(crate) async fn mission_revision_diff(
    Extension(reads): Extension<ReadWork>,
    State(server): State<Arc<ServerState>>,
    UrlPath(id): UrlPath<String>,
) -> Result<Json<Value>, ApiError> {
    reads
        .run(move || {
            let paths = mission_paths(&server, &id)?;
            if !paths.events_file().is_file() {
                return Err(unknown_mission(&id));
            }
            let state = fold_log(&paths).map_err(ApiError::internal)?;
            let Some(pending) = state.pending_revision.as_ref() else {
                return Err(ApiError::not_found(format!(
                    "mission '{id}' has no pending revision"
                )));
            };
            let current = read_file_or_404_sync(
                &paths.plan_md_file(),
                format!("mission '{id}' has no approved plan yet"),
            )?;
            // Preview the same calibrated estimate approval will commit, so the
            // dashboard's revision diff shows the range that actually lands (M1).
            let calibration = cost::calibrate(&paths.repo_root);
            let estimate = cost::apply_shape(
                cost::estimate(&pending.plan, &state.config, &calibration.params),
                &pending.plan,
                &calibration,
            );
            // Preview only: this endpoint does not re-lint the contract against the
            // base (mid-mission, the base tree is no longer necessarily pristine).
            let no_lint = ContractLintReport {
                results: Vec::new(),
                tree_clean_at_base: true,
            };
            let revised = render_plan_markdown(
                &pending.plan,
                &state.mission,
                &estimate,
                kranz_engine::cost::estimate_two_path(estimate, &state.config, &calibration.params)
                    .as_ref(),
                None,
                calibration.missions_used,
                &no_lint,
                &[],
                &state.config.worker_candidates,
            );
            Ok(Json(json!({
                "revision": pending.revision,
                "instructions": pending.instructions,
                "markdown": revised,
                "diff": simple_line_diff("plan.md", "revised-plan.md", &current, &revised),
            })))
        })
        .await
}

/// `GET /api/missions/:id/report.md` — rendered mission report markdown;
/// 404 until the mission completes and report.md is written.
pub(crate) async fn mission_report_md(
    State(server): State<Arc<ServerState>>,
    UrlPath(id): UrlPath<String>,
) -> Result<Json<Value>, ApiError> {
    crate::read_work::run(move || {
        let paths = mission_paths(&server, &id)?;
        let markdown = read_file_or_404_sync(
            &paths.report_file(),
            format!("mission '{id}' has no report yet"),
        )?;
        Ok(Json(json!({ "markdown": markdown })))
    })
    .await
}

/// `GET /api/missions/:id/diff-stat` — `git diff --stat` of the pinned
/// `base_sha` against the mission branch tip; 404 until the plan is
/// approved (`base_sha` set) and the mission branch exists.
pub(crate) async fn mission_diff_stat(
    Extension(reads): Extension<ReadWork>,
    State(server): State<Arc<ServerState>>,
    UrlPath(id): UrlPath<String>,
) -> Result<Json<Value>, ApiError> {
    reads
        .run(move || {
            let paths = mission_paths(&server, &id)?;
            if !paths.events_file().is_file() {
                return Err(unknown_mission(&id));
            }
            let state = fold_log(&paths).map_err(ApiError::internal)?;
            let Some(base_sha) = state.mission.base_sha else {
                return Err(ApiError::not_found(format!(
                    "mission '{id}' has no pinned base yet"
                )));
            };
            let repo = kranz_engine::git_ops::GitRepo::open(&server.repo_root)?;
            if !repo.branch_exists(&state.mission.mission_branch)? {
                return Err(ApiError::not_found(format!(
                    "mission '{id}' has no mission branch yet"
                )));
            }
            let tip = repo.rev_parse(&state.mission.mission_branch)?;
            let diff_stat = repo.diff_stat(&base_sha, &tip)?;
            Ok(Json(json!({
                "diffStat": diff_stat,
                "baseSha": base_sha,
                "tip": tip,
            })))
        })
        .await
}

/// `GET /api/missions/:id/pr-handoff` — optional GitHub PR handoff for a
/// COMPLETE-but-unmerged mission. Never pushes; may return a copyable
/// `git push` or a `gh pr create` command.
pub(crate) async fn mission_pr_handoff(
    Extension(reads): Extension<ReadWork>,
    State(server): State<Arc<ServerState>>,
    UrlPath(id): UrlPath<String>,
) -> Result<Json<Value>, ApiError> {
    reads
        .run(move || {
            let paths = mission_paths(&server, &id)?;
            if !paths.events_file().is_file() {
                return Err(unknown_mission(&id));
            }
            let handoff = kranz_engine::pr_handoff::assess_mission(&server.repo_root, &id)
                .map_err(|e| ApiError::internal(e.to_string()))?;
            Ok(Json(
                serde_json::to_value(handoff).map_err(|e| ApiError::internal(e.to_string()))?,
            ))
        })
        .await
}

/// `POST /api/missions/:id/pr-handoff/create` — run `gh pr create` only when
/// the handoff is `readyToCreate` (remote branch already present). Never pushes.
pub(crate) async fn mission_pr_create(
    State(server): State<Arc<ServerState>>,
    UrlPath(id): UrlPath<String>,
) -> Result<Json<Value>, ApiError> {
    let paths = mission_paths(&server, &id)?;
    if !paths.events_file().is_file() {
        return Err(unknown_mission(&id));
    }
    let handoff = kranz_engine::pr_handoff::assess_mission(&server.repo_root, &id)
        .map_err(|e| ApiError::internal(e.to_string()))?;
    let url = kranz_engine::pr_handoff::create_pull_request(&server.repo_root, &handoff)
        .map_err(|e| ApiError::conflict(e.to_string()))?;
    Ok(Json(json!({ "url": url })))
}

/// `GET /api/missions/:id/readiness` — backend readiness probe (same enum as
/// pre-drain gating). Tokenless read.
pub(crate) async fn mission_readiness(
    Extension(reads): Extension<ReadWork>,
    State(server): State<Arc<ServerState>>,
    UrlPath(id): UrlPath<String>,
) -> Result<Json<Value>, ApiError> {
    reads
        .run(move || {
            let _ = mission_paths(&server, &id)?;
            let report = kranz_engine::backend_readiness::probe_mission(&server.repo_root, &id)
                .map_err(|e| ApiError::internal(e.to_string()))?;
            Ok(Json(
                serde_json::to_value(report).map_err(|e| ApiError::internal(e.to_string()))?,
            ))
        })
        .await
}

/// `POST /api/hook-status` — the hook-status lane's ONLY write (ticket
/// `agent-hooks-status-signals`, [`kranz_engine::hook_status`]). Receives
/// one mapped lifecycle signal from a session's `kranz hook-status` relay
/// and records it in the ephemeral `.kranz/hook-status/` projection.
///
/// Authenticates with the per-RUN capability token in the body — never the
/// serve mutation token (a worker-readable file can only ever carry a
/// token whose forgery ceiling is lying about its own run's status), so
/// the route is exempt from the mutation-token gate exactly like the
/// GitHub webhook's HMAC route. The payload is untrusted even on loopback:
/// the body is route-limited to
/// [`kranz_engine::hook_status::SIGNAL_BODY_MAX_BYTES`], ids are safe-id
/// checked (path traversal), the token is constant-time compared against
/// the registered hash, stale registrations reject, and the handler's only
/// write is the projection file — no event, no state mutation, no grant
/// path exists here.
pub(crate) async fn post_hook_status(
    State(server): State<Arc<ServerState>>,
    Json(body): Json<kranz_engine::hook_status::SignalPost>,
) -> Result<impl IntoResponse, ApiError> {
    use kranz_engine::hook_status::RecordRejection;
    match kranz_engine::hook_status::record_signal(
        &server.repo_root,
        &body.mission_id,
        &body.run_id,
        &body.token,
        body.signal,
        body.detail.as_deref(),
        chrono::Utc::now(),
    ) {
        Ok(_) => Ok((StatusCode::ACCEPTED, Json(json!({ "recorded": true })))),
        Err(RecordRejection::UnsafeId) | Err(RecordRejection::UnknownRun) => Err(
            ApiError::not_found(format!("unknown hook-status run '{}'", body.run_id)),
        ),
        Err(RecordRejection::TokenMismatch) => Err(ApiError::unauthorized(
            "hook-status token does not match this run's registration",
        )),
        Err(RecordRejection::Stale) => Err(ApiError::unauthorized(
            "hook-status registration is stale (past its acceptance TTL)",
        )),
        Err(RecordRejection::RegistrationUnreadable) => Err(ApiError::internal(
            "hook-status projection entry could not be read or written",
        )),
    }
}

/// `GET /api/missions/:id/hook-status` — the ephemeral hook-signal
/// projection for one mission, re-read from disk per request like every
/// other derived read. Additive and explicitly NON-authoritative: the
/// payload carries `authoritative: false` so no consumer can mistake
/// hook-derived signals for folded mission state (the fold is untouched —
/// nothing in this lane can change a mission's terminal state).
pub(crate) async fn mission_hook_status(
    Extension(reads): Extension<ReadWork>,
    State(server): State<Arc<ServerState>>,
    UrlPath(id): UrlPath<String>,
) -> Result<Json<Value>, ApiError> {
    reads.run(move || {
        let paths = mission_paths(&server, &id)?;
        if !paths.events_file().is_file() {
            return Err(unknown_mission(&id));
        }
        let runs = kranz_engine::hook_status::read_mission_signals(&server.repo_root, &id);
        Ok(Json(json!({
            "missionId": id,
            "authoritative": false,
            "note": "hook-derived lifecycle signals; observability only, never folded mission state",
            "runs": runs,
        })))
    })
    .await
}

/// `GET /api/missions/:id/runs/:runId/transcript` — the run's JSONL parsed
/// into a JSON array of raw stream values; 404 if the file is missing.
pub(crate) async fn run_transcript(
    State(server): State<Arc<ServerState>>,
    UrlPath((id, run_id)): UrlPath<(String, String)>,
) -> Result<Json<Value>, ApiError> {
    crate::read_work::run(move || {
        let paths = mission_paths(&server, &id)?;
        if !safe_id(&run_id) {
            return Err(ApiError::not_found(format!("unknown run '{run_id}'")));
        }
        let content = read_file_or_404_sync(
            &paths.transcript_file(&run_id),
            format!("no transcript for run '{run_id}'"),
        )?;
        // Tolerate torn/garbage lines (a live transcript may end mid-write).
        let values: Vec<Value> = content
            .lines()
            .filter(|line| !line.trim().is_empty())
            .filter_map(|line| serde_json::from_str(line).ok())
            .collect();
        Ok(Json(Value::Array(values)))
    })
    .await
}

/// `POST /api/missions/:id/control` — enqueue a [`ControlCommand`] into the
/// mission's control inbox (the engine drains it). `202 {"queued":true}`;
/// 400 on a body that is not a valid command; 409 when the mission is
/// terminal (mirrors [`control::resolve_active_mission`]).
pub(crate) async fn post_control(
    State(server): State<Arc<ServerState>>,
    UrlPath(id): UrlPath<String>,
    body: Bytes,
) -> Result<impl IntoResponse, ApiError> {
    let paths = mission_paths(&server, &id)?;
    if !paths.mission_dir().is_dir() {
        return Err(unknown_mission(&id));
    }
    if paths.events_file().is_file() {
        if let Some(status) =
            terminal_status_from_tail(&paths).map_err(|e| ApiError::internal(e.to_string()))?
        {
            return Err(ApiError::conflict(format!(
                "mission '{id}' is {status:?}; control commands apply only to active missions"
            )));
        }
    }
    let command: ControlCommand = serde_json::from_slice(&body)
        .map_err(|e| ApiError::bad_request(format!("invalid ControlCommand body: {e}")))?;
    if matches!(command, ControlCommand::ResolvePermission { .. }) {
        return Err(ApiError::bad_request(
            "use the permission/answer route; permission actor is assigned by the server",
        ));
    }
    if let ControlCommand::ConfigChange { patch } = &command {
        let events = EventLog::read_events(&paths.events_file())?;
        let state = reducer::fold(&events)?;
        config::apply_validated_patch(&state.config, patch)?;
    }
    control::enqueue(&paths, &command)?;
    Ok((StatusCode::ACCEPTED, Json(json!({ "queued": true }))))
}

/// O(tail) terminal probe for the control route — the hottest write path
/// only needs "is the mission over?", not a full-log fold. Sound because a
/// terminal lifecycle event always sits in the trailing window: the engine
/// stops appending after emitting it (the report and any lesson land BEFORE
/// `mission.completed`; only the redaction audits attached to the same
/// append can follow `mission.abandoned`), and every mutation surface
/// refuses terminal missions.
fn terminal_status_from_tail(
    paths: &MissionPaths,
) -> kranz_engine::error::Result<Option<MissionStatus>> {
    const TAIL_WINDOW_BYTES: u64 = 64 * 1024;
    let events = EventLog::read_tail_events(&paths.events_file(), TAIL_WINDOW_BYTES)?;
    Ok(events.iter().rev().find_map(|e| match e.kind {
        EventKind::MissionCompleted {} => Some(MissionStatus::Complete),
        EventKind::MissionFailed { .. } => Some(MissionStatus::Failed),
        EventKind::MissionAbandoned { .. } => Some(MissionStatus::Abandoned),
        _ => None,
    }))
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct ReviseBody {
    instructions: String,
}

/// `POST /api/missions/:id/revise` — enqueue a mid-mission revision request.
pub(crate) async fn post_revise(
    State(server): State<Arc<ServerState>>,
    UrlPath(id): UrlPath<String>,
    Json(body): Json<ReviseBody>,
) -> Result<impl IntoResponse, ApiError> {
    let instructions = body.instructions.trim();
    if instructions.is_empty() {
        return Err(ApiError::bad_request(
            "revision instructions must not be empty",
        ));
    }
    let paths = require_revisable_mission(&server, &id)?;
    control::enqueue(
        &paths,
        &ControlCommand::RequestRevision {
            instructions: instructions.to_string(),
        },
    )?;
    Ok((StatusCode::ACCEPTED, Json(json!({ "queued": true }))))
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct RevisionDecisionBody {
    revision: u32,
}

/// `POST /api/missions/:id/revision/approve` — approve a pending revision.
pub(crate) async fn post_revision_approve(
    State(server): State<Arc<ServerState>>,
    UrlPath(id): UrlPath<String>,
    Json(body): Json<RevisionDecisionBody>,
) -> Result<impl IntoResponse, ApiError> {
    let paths = require_pending_revision(&server, &id, body.revision)?;
    control::enqueue(
        &paths,
        &ControlCommand::ApproveRevision {
            revision: body.revision,
        },
    )?;
    Ok((StatusCode::ACCEPTED, Json(json!({ "queued": true }))))
}

/// `POST /api/missions/:id/revision/reject` — reject a pending revision.
pub(crate) async fn post_revision_reject(
    State(server): State<Arc<ServerState>>,
    UrlPath(id): UrlPath<String>,
    Json(body): Json<RevisionDecisionBody>,
) -> Result<impl IntoResponse, ApiError> {
    let paths = require_pending_revision(&server, &id, body.revision)?;
    control::enqueue(
        &paths,
        &ControlCommand::RejectRevision {
            revision: body.revision,
        },
    )?;
    Ok((StatusCode::ACCEPTED, Json(json!({ "queued": true }))))
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub(crate) struct PermissionAnswerBody {
    request_id: String,
    binding_digest: String,
    allow: bool,
}

pub(crate) async fn post_permission_answer(
    State(server): State<Arc<ServerState>>,
    UrlPath(id): UrlPath<String>,
    Json(body): Json<PermissionAnswerBody>,
) -> Result<impl IntoResponse, ApiError> {
    let paths = require_revisable_mission(&server, &id)?;
    let state = fold_log(&paths).map_err(ApiError::internal)?;
    let record = state
        .permissions
        .get(&body.request_id)
        .ok_or_else(|| ApiError::conflict("unknown live permission"))?;
    record
        .validate_answer(&body.binding_digest, body.allow, chrono::Utc::now())
        .map_err(|e| ApiError::conflict(e.to_string()))?;
    control::enqueue(
        &paths,
        &ControlCommand::ResolvePermission {
            resolution: kranz_engine::live_permission::Resolution {
                request_id: body.request_id,
                binding_digest: body.binding_digest,
                allow: body.allow,
                actor: kranz_engine::live_permission::Actor::LocalMutationCapability,
                reason: "operator answered through the authenticated local API".into(),
            },
        },
    )?;
    Ok((StatusCode::ACCEPTED, Json(json!({ "queued": true }))))
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct GrantApproveBody {
    command: String,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct GrantDenyBody {
    command: String,
    #[serde(default = "default_grant_deny_reason")]
    reason: String,
}

fn default_grant_deny_reason() -> String {
    "denied by operator".to_string()
}

/// `POST /api/missions/:id/grant/approve` — approve the parked grant request.
pub(crate) async fn post_grant_approve(
    State(server): State<Arc<ServerState>>,
    UrlPath(id): UrlPath<String>,
    Json(body): Json<GrantApproveBody>,
) -> Result<impl IntoResponse, ApiError> {
    let paths = require_pending_grant(&server, &id, &body.command)?;
    control::enqueue(
        &paths,
        &ControlCommand::ApproveGrant {
            command: body.command,
        },
    )?;
    Ok((StatusCode::ACCEPTED, Json(json!({ "queued": true }))))
}

/// `POST /api/missions/:id/grant/deny` — deny the parked grant request.
pub(crate) async fn post_grant_deny(
    State(server): State<Arc<ServerState>>,
    UrlPath(id): UrlPath<String>,
    Json(body): Json<GrantDenyBody>,
) -> Result<impl IntoResponse, ApiError> {
    let paths = require_pending_grant(&server, &id, &body.command)?;
    control::enqueue(
        &paths,
        &ControlCommand::DenyGrant {
            command: body.command,
            reason: body.reason,
        },
    )?;
    Ok((StatusCode::ACCEPTED, Json(json!({ "queued": true }))))
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct QuestionAnswerBody {
    /// The open question's engine-minted id (`q-<n>`).
    question_id: String,
    /// The chosen option's text verbatim, or free text.
    answer: String,
    /// 0-based option index when an offered option was picked; absent for
    /// free-text answers.
    #[serde(default)]
    option: Option<u32>,
}

/// `POST /api/missions/:id/question/answer` — answer an open structured
/// question (ticket `structured-human-question-events`): the pending-decision
/// projection's input edge, enqueued onto the EXISTING control path (D-X: no
/// new server). The engine lands it as `question.answered`, which the reducer
/// routes onto the mission's user-message consult.
pub(crate) async fn post_question_answer(
    State(server): State<Arc<ServerState>>,
    UrlPath(id): UrlPath<String>,
    Json(body): Json<QuestionAnswerBody>,
) -> Result<impl IntoResponse, ApiError> {
    let paths =
        require_pending_question(&server, &id, &body.question_id, body.option, &body.answer)?;
    control::enqueue(
        &paths,
        &ControlCommand::AnswerQuestion {
            question_id: body.question_id,
            answer: body.answer,
            option: body.option,
        },
    )?;
    Ok((StatusCode::ACCEPTED, Json(json!({ "queued": true }))))
}

// ---------------------------------------------------------------------------
// Shared helpers (also used by the WS handler)
// ---------------------------------------------------------------------------

/// Build [`MissionPaths`] for a URL-supplied mission id, rejecting anything
/// that could traverse outside the missions dir.
pub(crate) fn mission_paths(server: &ServerState, id: &str) -> Result<MissionPaths, ApiError> {
    if !safe_id(id) {
        return Err(unknown_mission(id));
    }
    let paths = MissionPaths::new(&server.repo_root, id);
    // A symlinked component in the mission path is refused (P1
    // mission-path-no-follow), never followed into another repository's
    // tree. An absent component is NOT a refusal here — handlers keep their
    // own unknown-mission behavior for missions that do not exist.
    if paths.require_no_follow().is_err() {
        return Err(unknown_mission(id));
    }
    Ok(paths)
}

/// Ids from the URL are joined into filesystem paths, so separators, `..`
/// and drive designators are rejected (axum percent-decodes path segments,
/// so `%2e%2e%2f` would otherwise sneak through).
fn safe_id(id: &str) -> bool {
    MissionPaths::is_safe_id(id)
}

fn unknown_mission(id: &str) -> ApiError {
    ApiError::not_found(format!("unknown mission '{id}'"))
}

fn fold_log(paths: &MissionPaths) -> Result<MissionState, String> {
    let events = EventLog::read_events(&paths.events_file()).map_err(|e| e.to_string())?;
    reducer::fold(&events).map_err(|e| e.to_string())
}

fn require_revisable_mission(server: &ServerState, id: &str) -> Result<MissionPaths, ApiError> {
    let paths = mission_paths(server, id)?;
    if !paths.events_file().is_file() {
        return Err(unknown_mission(id));
    }
    let state = fold_log(&paths).map_err(ApiError::internal)?;
    if state.mission.status == MissionStatus::Planning {
        return Err(ApiError::conflict(format!(
            "mission '{id}' has no approved plan to revise yet"
        )));
    }
    if kranz_engine::mission_catalog::is_terminal_status(state.mission.status) {
        return Err(ApiError::conflict(format!(
            "mission '{id}' is {:?}; revision commands apply only to active missions",
            state.mission.status
        )));
    }
    Ok(paths)
}

fn require_pending_revision(
    server: &ServerState,
    id: &str,
    revision: u32,
) -> Result<MissionPaths, ApiError> {
    let paths = require_revisable_mission(server, id)?;
    let state = fold_log(&paths).map_err(ApiError::internal)?;
    match state.pending_revision {
        Some(pending) if pending.revision == revision => Ok(paths),
        Some(pending) => Err(ApiError::conflict(format!(
            "mission '{id}' is awaiting revision {}, not {revision}",
            pending.revision
        ))),
        None => Err(ApiError::conflict(format!(
            "mission '{id}' has no pending revision"
        ))),
    }
}

/// Confirm a grant request for exactly `command` is parked, so the enqueued
/// approve/deny can't target a different (or absent) request than the operator
/// saw. Same active-mission preconditions as the revision gate.
fn require_pending_grant(
    server: &ServerState,
    id: &str,
    command: &str,
) -> Result<MissionPaths, ApiError> {
    let paths = require_revisable_mission(server, id)?;
    let state = fold_log(&paths).map_err(ApiError::internal)?;
    match state.pending_grant_request {
        Some(pending) if pending.command == command => Ok(paths),
        Some(pending) => Err(ApiError::conflict(format!(
            "mission '{id}' is awaiting a grant for `{}`, not `{command}`",
            pending.command
        ))),
        None => Err(ApiError::conflict(format!(
            "mission '{id}' has no pending grant request"
        ))),
    }
}

/// Confirm question `question_id` is open (and an option-index answer is in
/// range and matches the offered option), so the enqueued answer can't land
/// on a different (or absent) question than the operator saw — the same
/// stale-decision discipline as [`require_pending_grant`]. The engine
/// re-validates at drain time; this pre-check is what lets the caller get an
/// honest 409 instead of a silently ignored 202.
fn require_pending_question(
    server: &ServerState,
    id: &str,
    question_id: &str,
    option: Option<u32>,
    answer: &str,
) -> Result<MissionPaths, ApiError> {
    let paths = require_revisable_mission(server, id)?;
    let state = fold_log(&paths).map_err(ApiError::internal)?;
    let Some(pending) = state
        .pending_questions
        .iter()
        .find(|q| q.question_id == question_id)
    else {
        return Err(ApiError::conflict(format!(
            "mission '{id}' has no open question '{question_id}'"
        )));
    };
    if let Some(index) = option {
        match pending.options.get(index as usize) {
            Some(expected) if expected == answer => {}
            Some(expected) => {
                return Err(ApiError::conflict(format!(
                    "answer `{answer}` does not match option {index} (`{expected}`) of question '{question_id}'"
                )))
            }
            None => {
                return Err(ApiError::conflict(format!(
                    "question '{question_id}' has no option {index} (it offered {})",
                    pending.options.len()
                )))
            }
        }
    }
    Ok(paths)
}

fn simple_line_diff(old_name: &str, new_name: &str, old: &str, new: &str) -> String {
    let old_lines: Vec<&str> = old.lines().collect();
    let new_lines: Vec<&str> = new.lines().collect();
    let mut diff = format!("--- {old_name}\n+++ {new_name}\n");
    let max = old_lines.len().max(new_lines.len());
    for i in 0..max {
        match (old_lines.get(i), new_lines.get(i)) {
            (Some(a), Some(b)) if a == b => {
                diff.push(' ');
                diff.push_str(a);
                diff.push('\n');
            }
            (Some(a), Some(b)) => {
                diff.push('-');
                diff.push_str(a);
                diff.push('\n');
                diff.push('+');
                diff.push_str(b);
                diff.push('\n');
            }
            (Some(a), None) => {
                diff.push('-');
                diff.push_str(a);
                diff.push('\n');
            }
            (None, Some(b)) => {
                diff.push('+');
                diff.push_str(b);
                diff.push('\n');
            }
            (None, None) => {}
        }
    }
    diff
}

/// Read one mission leaf file (`plan.json`, `plan.md`, `report.md`, a run
/// transcript) NO-FOLLOW.
///
/// `mission_paths` pins the directory chain, but the leaf used to be a plain
/// `read_to_string`, which follows symlinks: under checkout isolation a
/// session could replace `plan.md` with a link to `.kranz/serve.token` and
/// read the mutation token back over a tokenless loopback GET, defeating the
/// sandbox's own read-deny on that file. Every leaf read goes through the
/// engine's no-follow open now — the same helper `EventLog` uses. A refusal
/// is a 404, like a missing file: the API says nothing about what the link
/// pointed at.
// Cap concurrent file work as well as bytes per response. The permit moves
// into the blocking task so cancellation cannot release capacity prematurely.
const MAX_ARTIFACT_BYTES: u64 = 8 * 1024 * 1024;

#[cfg(test)]
async fn read_file_or_404(
    path: &Path,
    not_found_msg: impl FnOnce() -> String,
) -> Result<String, ApiError> {
    let path = path.to_path_buf();
    let missing = not_found_msg();
    crate::read_work::run(move || read_file_or_404_sync(&path, missing)).await
}

fn read_file_or_404_sync(path: &Path, missing: String) -> Result<String, ApiError> {
    let file = match kranz_engine::paths::open_read_nofollow(path) {
        Ok(file) => file,
        Err(kranz_engine::error::EngineError::Io(e)) if e.kind() == ErrorKind::NotFound => {
            return Err(ApiError::not_found(missing));
        }
        Err(kranz_engine::error::EngineError::InvalidState(_)) => {
            return Err(ApiError::not_found(missing));
        }
        Err(e) => {
            return Err(ApiError::internal(format!(
                "failed to read {}: {e}",
                path.display()
            )))
        }
    };
    kranz_engine::paths::read_regular_file_bounded(file, MAX_ARTIFACT_BYTES).map_err(|e| {
        if e.kind() == ErrorKind::FileTooLarge {
            ApiError {
                status: axum::http::StatusCode::PAYLOAD_TOO_LARGE,
                code: None,
                message: format!("artifact exceeds the {MAX_ARTIFACT_BYTES}-byte read limit"),
            }
        } else {
            ApiError::internal(format!("failed to read {}: {e}", path.display()))
        }
    })
}

#[cfg(test)]
mod tests {
    use axum::body::Body;
    use axum::http::{Request, StatusCode};
    use http_body_util::BodyExt;
    use kranz_engine::event_log::{EventLog, LockForce};
    use kranz_engine::events::EventKind;
    use kranz_engine::paths::MissionPaths;
    use kranz_engine::types::{
        Finding, GrantKind, MissionConfig, PinnedRule, Plan, PlanFeature, PlanMilestone,
        RuleCitation, StandardsPin, StandardsPinSource,
    };
    use serde_json::Value;
    use std::time::Duration;
    use tempfile::TempDir;
    use tower::ServiceExt;

    #[tokio::test]
    async fn artifact_reads_reject_oversized_and_nonregular_inputs() {
        let repo = TempDir::new().unwrap();
        let paths = MissionPaths::new(repo.path(), "m-bounded");
        std::fs::create_dir_all(paths.mission_dir()).unwrap();
        let path = paths.report_file();
        std::fs::File::create(&path)
            .unwrap()
            .set_len(super::MAX_ARTIFACT_BYTES + 1)
            .unwrap();
        let error = super::read_file_or_404(&path, || "missing".into())
            .await
            .unwrap_err();
        assert_eq!(error.status, StatusCode::PAYLOAD_TOO_LARGE);
        std::fs::remove_file(&path).unwrap();
        std::fs::create_dir(&path).unwrap();
        let error = super::read_file_or_404(&path, || "missing".into())
            .await
            .unwrap_err();
        assert_eq!(error.status, StatusCode::NOT_FOUND);
        std::fs::remove_dir(&path).unwrap();
        std::fs::write(&path, "complete report").unwrap();
        assert_eq!(
            super::read_file_or_404(&path, || "missing".into())
                .await
                .unwrap(),
            "complete report"
        );
    }

    async fn body_json(response: axum::response::Response) -> Value {
        let bytes = response.into_body().collect().await.unwrap().to_bytes();
        serde_json::from_slice(&bytes).unwrap()
    }

    fn get(uri: &str) -> Request<Body> {
        Request::builder().uri(uri).body(Body::empty()).unwrap()
    }

    fn seed_mission(repo_root: &std::path::Path, id: &str, kinds: Vec<EventKind>) {
        let paths = MissionPaths::new(repo_root, id);
        let mut log = EventLog::acquire(&paths, id, Duration::ZERO, LockForce::No).unwrap();
        for kind in kinds {
            log.append(kind).unwrap();
        }
    }

    fn created(goal: &str) -> EventKind {
        EventKind::MissionCreated {
            goal: goal.into(),
            base_branch: "main".into(),
            mission_branch: "kranz/mission-x".into(),
            config: MissionConfig::default(),
        }
    }

    fn post_json(uri: &str, body: &str) -> Request<Body> {
        Request::builder()
            .method("POST")
            .uri(uri)
            .header("content-type", "application/json")
            .body(Body::from(body.to_string()))
            .unwrap()
    }

    #[tokio::test]
    async fn flight_rules_dashboard_standards_view_is_typed_and_hides_nonwaivable_actions() {
        let tmp = TempDir::new().unwrap();
        let digest = "ab".repeat(32);
        let rule = |id: &str, waivable: bool| PinnedRule {
            id: id.to_string(),
            revision: 2,
            rfc: "RFC-001".to_string(),
            level: "must".to_string(),
            effective_status: "enforced".to_string(),
            statement: format!("statement for {id}"),
            domains: vec!["security".to_string()],
            stages: vec!["validation".to_string()],
            when_paths: Vec::new(),
            task_classes: Vec::new(),
            checker: Some("gate:secure".to_string()),
            waivable,
        };
        let rules = vec![rule("ZZ-WAIVE-001", true), rule("ZZ-LOCKED-001", false)];
        let pin = StandardsPin {
            pack_name: "zz-pack".to_string(),
            pack_dir: "vendor/pack".to_string(),
            standards_root: "standards".to_string(),
            digest: digest.clone(),
            source: StandardsPinSource::RepoTracked,
            task_class: None,
            touch_set: vec!["src/**".to_string()],
            context_paths: Vec::new(),
            gates: Vec::new(),
            rules: rules.clone(),
        };
        let plan = Plan {
            goal: "governed change".to_string(),
            validation_contract: Vec::new(),
            milestones: vec![PlanMilestone {
                title: "one".to_string(),
                features: vec![PlanFeature {
                    title: "change".to_string(),
                    spec: "implement".to_string(),
                    validation_criteria: Vec::new(),
                }],
            }],
            considered_alternatives: None,
            command_grants: Vec::new(),
            touch_set: vec!["src/**".to_string()],
            standards_manifest: Some(Box::new(pin)),
            reviewer_independence: None,
        };
        let finding = |rule: &PinnedRule| Finding {
            subject: format!("flight-rule:{}", rule.id),
            severity: "critical".to_string(),
            evidence: format!("{} failed with exact evidence", rule.id),
            suggested_fix: "fix it".to_string(),
            class: "standards-authoritative".to_string(),
            rule: Some(RuleCitation {
                id: rule.id.clone(),
                revision: rule.revision,
                source: "zz-pack standards".to_string(),
                digest: digest.clone(),
                lifecycle: rule.effective_status.clone(),
                level: rule.level.clone(),
                checker: rule.checker.clone(),
            }),
        };
        seed_mission(
            tmp.path(),
            "m-1",
            vec![
                created("governed change"),
                EventKind::PlanApproved {
                    plan,
                    base_sha: Some("deadbeef".to_string()),
                },
                EventKind::ValidationFinding {
                    milestone_id: "ms-1".to_string(),
                    run_id: kranz_engine::reducer::ENGINE_RUN_ID.to_string(),
                    finding: finding(&rules[0]),
                },
                EventKind::ValidationFinding {
                    milestone_id: "ms-1".to_string(),
                    run_id: kranz_engine::reducer::ENGINE_RUN_ID.to_string(),
                    finding: finding(&rules[1]),
                },
            ],
        );
        let app = crate::router(tmp.path().to_path_buf(), None);
        let response = app
            .oneshot(get("/api/missions/m-1/standards"))
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::OK);
        let body = body_json(response).await;
        assert_eq!(body["manifest"]["digest"], digest);
        assert_eq!(body["coverage"]["rules"][0]["disposition"], "failed");
        assert_eq!(body["waiverCandidates"].as_array().unwrap().len(), 1);
        assert_eq!(body["waiverCandidates"][0]["rule"]["id"], "ZZ-WAIVE-001");
        assert!(body["waiverCandidates"][0]["findingEvidence"]
            .as_str()
            .unwrap()
            .contains("exact evidence"));
    }

    /// The lane end to end over HTTP: a registered run's signal POST lands
    /// in the ephemeral projection and is served by the per-mission GET —
    /// labelled non-authoritative — while the mission's folded state and
    /// its event log stay byte-identical (hooks never become mission
    /// state; a terminal mission stays terminal).
    #[tokio::test]
    async fn hook_status_signal_endpoint_records_serves_and_never_touches_state() {
        let tmp = TempDir::new().unwrap();
        seed_mission(
            tmp.path(),
            "m-1",
            vec![created("terminal"), EventKind::MissionCompleted {}],
        );
        kranz_engine::hook_status::register(tmp.path(), "m-1", "r-1", "tok-1", chrono::Utc::now())
            .unwrap();
        let events_before =
            std::fs::read(MissionPaths::new(tmp.path(), "m-1").events_file()).unwrap();
        let app = crate::router(tmp.path().to_path_buf(), None);

        let response = app
            .clone()
            .oneshot(post_json(
                "/api/hook-status",
                &serde_json::json!({
                    "token": "tok-1",
                    "missionId": "m-1",
                    "runId": "r-1",
                    "signal": "needs-input",
                    "detail": "Shell was refused",
                })
                .to_string(),
            ))
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::ACCEPTED);

        let response = app
            .clone()
            .oneshot(get("/api/missions/m-1/hook-status"))
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::OK);
        let body = body_json(response).await;
        assert_eq!(body["authoritative"], false);
        let runs = body["runs"].as_array().unwrap();
        assert_eq!(runs.len(), 1);
        assert_eq!(runs[0]["runId"], "r-1");
        assert_eq!(runs[0]["signal"]["signal"], "needs-input");
        assert_eq!(runs[0]["signal"]["detail"], "Shell was refused");
        assert!(
            runs[0]["signal"]["receivedAt"].as_str().is_some(),
            "{runs:?}"
        );

        // Folded mission state is untouched by the lane: the terminal
        // mission still folds Complete and the event log is byte-identical.
        let response = app.oneshot(get("/api/missions/m-1/state")).await.unwrap();
        let state = body_json(response).await;
        assert_eq!(state["mission"]["status"], "complete");
        let events_after =
            std::fs::read(MissionPaths::new(tmp.path(), "m-1").events_file()).unwrap();
        assert_eq!(events_before, events_after);
    }

    /// Untrusted-payload discipline over HTTP: wrong tokens, unknown runs,
    /// traversal ids, malformed bodies, and oversized bodies are all
    /// rejected; nothing is written for any of them.
    #[tokio::test]
    async fn hook_status_signal_endpoint_rejects_untrusted_payloads() {
        let tmp = TempDir::new().unwrap();
        seed_mission(tmp.path(), "m-1", vec![created("x")]);
        kranz_engine::hook_status::register(tmp.path(), "m-1", "r-1", "tok-1", chrono::Utc::now())
            .unwrap();
        let app = crate::router(tmp.path().to_path_buf(), None);

        // Wrong capability token → 401.
        let response = app
            .clone()
            .oneshot(post_json(
                "/api/hook-status",
                &serde_json::json!({
                    "token": "tok-WRONG",
                    "missionId": "m-1",
                    "runId": "r-1",
                    "signal": "running",
                })
                .to_string(),
            ))
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::UNAUTHORIZED);

        // Unknown run → 404 (no oracle about neighboring runs).
        let response = app
            .clone()
            .oneshot(post_json(
                "/api/hook-status",
                &serde_json::json!({
                    "token": "tok-1",
                    "missionId": "m-1",
                    "runId": "r-9",
                    "signal": "running",
                })
                .to_string(),
            ))
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::NOT_FOUND);

        // Path traversal in the mission id → 404, never a joined path.
        let response = app
            .clone()
            .oneshot(post_json(
                "/api/hook-status",
                &serde_json::json!({
                    "token": "tok-1",
                    "missionId": "../m-1",
                    "runId": "r-1",
                    "signal": "running",
                })
                .to_string(),
            ))
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::NOT_FOUND);

        // A signal outside the vocabulary is a 4xx — the lane can never
        // spell a state transition ("complete", "blocked", ...).
        let response = app
            .clone()
            .oneshot(post_json(
                "/api/hook-status",
                &serde_json::json!({
                    "token": "tok-1",
                    "missionId": "m-1",
                    "runId": "r-1",
                    "signal": "complete",
                })
                .to_string(),
            ))
            .await
            .unwrap();
        assert!(
            response.status().is_client_error(),
            "an out-of-vocabulary signal must be rejected: {}",
            response.status()
        );

        // Malformed JSON → 4xx.
        let response = app
            .clone()
            .oneshot(post_json("/api/hook-status", "{not json"))
            .await
            .unwrap();
        assert!(response.status().is_client_error());

        // Oversized body → 413 (the route's own 16 KiB limit).
        let oversized = format!(
            "{{\"token\":\"tok-1\",\"missionId\":\"m-1\",\"runId\":\"r-1\",\"signal\":\"running\",\"detail\":\"{}\"}}",
            "x".repeat(kranz_engine::hook_status::SIGNAL_BODY_MAX_BYTES)
        );
        let response = app
            .clone()
            .oneshot(post_json("/api/hook-status", &oversized))
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::PAYLOAD_TOO_LARGE);

        // None of the rejections recorded anything.
        let views = kranz_engine::hook_status::read_mission_signals(tmp.path(), "m-1");
        assert!(views.iter().all(|v| v.signal.is_none()), "{views:?}");
    }

    /// The POST authenticates with the per-run capability token, NOT the
    /// serve mutation token: with the gate armed, the signal POST goes
    /// through WITHOUT `x-kranz-token` while an ordinary mutation POST is
    /// still rejected.
    #[tokio::test]
    async fn hook_status_signal_post_is_exempt_from_the_mutation_token_gate() {
        let tmp = TempDir::new().unwrap();
        seed_mission(tmp.path(), "m-1", vec![created("x")]);
        kranz_engine::hook_status::register(tmp.path(), "m-1", "r-1", "tok-1", chrono::Utc::now())
            .unwrap();
        let app = crate::router_with_token(
            tmp.path().to_path_buf(),
            None,
            crate::MutationAuthority::new("serve-secret").unwrap(),
        );

        let response = app
            .clone()
            .oneshot(post_json(
                "/api/hook-status",
                &serde_json::json!({
                    "token": "tok-1",
                    "missionId": "m-1",
                    "runId": "r-1",
                    "signal": "running",
                })
                .to_string(),
            ))
            .await
            .unwrap();
        assert_eq!(
            response.status(),
            StatusCode::ACCEPTED,
            "the per-run capability token authenticates the lane, not the serve token"
        );

        // An ordinary mutation without the serve token is still refused.
        let response = app
            .oneshot(post_json(
                "/api/missions/m-1/revise",
                "{\"instructions\":\"x\"}",
            ))
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::UNAUTHORIZED);
    }

    /// Install hygiene at the server seam: serving and recording never
    /// writes hook config into the repo's tracked tree — the only tree the
    /// lane writes is the gitignored `.kranz/hook-status/` projection.
    #[tokio::test]
    async fn hook_status_signal_server_writes_nothing_into_the_tracked_tree() {
        let tmp = TempDir::new().unwrap();
        seed_mission(tmp.path(), "m-1", vec![created("x")]);
        kranz_engine::hook_status::register(tmp.path(), "m-1", "r-1", "tok-1", chrono::Utc::now())
            .unwrap();
        let app = crate::router(tmp.path().to_path_buf(), None);
        let response = app
            .clone()
            .oneshot(post_json(
                "/api/hook-status",
                &serde_json::json!({
                    "token": "tok-1",
                    "missionId": "m-1",
                    "runId": "r-1",
                    "signal": "turn-finished",
                })
                .to_string(),
            ))
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::ACCEPTED);
        let response = app
            .oneshot(get("/api/missions/m-1/hook-status"))
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::OK);

        assert!(
            !tmp.path().join(".cursor").exists(),
            "no cursor hook config may appear in the repo tree"
        );
        assert!(
            kranz_engine::hook_status::hook_status_dir(tmp.path()).is_dir(),
            "the projection is the lane's only write"
        );
    }

    #[tokio::test]
    async fn outcomes_endpoint_empty_repo_returns_zeroed_defaults() {
        let tmp = TempDir::new().unwrap();
        let app = crate::router(tmp.path().to_path_buf(), None);

        let response = app.oneshot(get("/api/missions/outcomes")).await.unwrap();
        assert_eq!(response.status(), StatusCode::OK);
        let body = body_json(response).await;

        assert_eq!(body["autonomyRatio"]["closedMissions"], 0);
        let buckets = body["grantLatency"]["buckets"].as_array().unwrap();
        assert_eq!(buckets.len(), 4);
        assert!(buckets.iter().all(|b| b["count"] == 0));
        assert_eq!(body["escalations"].as_array().unwrap().len(), 0);
    }

    #[tokio::test]
    async fn outcomes_endpoint_route_is_not_swallowed_by_mission_id_routes() {
        let tmp = TempDir::new().unwrap();
        let app = crate::router(tmp.path().to_path_buf(), None);

        // If `outcomes` were captured as a mission id by `/missions/:id/state`
        // style routes, this would 404 as an unknown mission instead of
        // resolving to the outcomes handler.
        let response = app.oneshot(get("/api/missions/outcomes")).await.unwrap();
        assert_eq!(response.status(), StatusCode::OK);
        let body = body_json(response).await;
        assert!(body.get("autonomyRatio").is_some());
        assert!(body.get("error").is_none());
    }

    #[tokio::test]
    async fn outcomes_endpoint_seeded_repo_populates_buckets_and_escalations() {
        let tmp = TempDir::new().unwrap();
        seed_mission(
            tmp.path(),
            "m-1",
            vec![
                created("seeded"),
                EventKind::GrantRequested {
                    milestone_id: "ms-1".into(),
                    kind: GrantKind::Command,
                    command: "cargo test".into(),
                },
                EventKind::GrantApproved {
                    kind: GrantKind::Command,
                    command: "cargo test".into(),
                },
                EventKind::PlanRevisionProposed {
                    revision: 1,
                    plan: sample_plan(),
                    instructions: "add tests".into(),
                },
                EventKind::PlanRevised {
                    revision: 1,
                    plan: sample_plan(),
                },
                EventKind::MissionCompleted {},
            ],
        );

        let app = crate::router(tmp.path().to_path_buf(), None);
        let response = app.oneshot(get("/api/missions/outcomes")).await.unwrap();
        assert_eq!(response.status(), StatusCode::OK);
        let body = body_json(response).await;

        assert_eq!(body["autonomyRatio"]["closedMissions"], 1);
        assert_eq!(body["autonomyRatio"]["totalInterventions"], 2);

        let buckets = body["grantLatency"]["buckets"].as_array().unwrap();
        assert_eq!(buckets.len(), 4);
        let total_bucketed: i64 = buckets.iter().map(|b| b["count"].as_i64().unwrap()).sum();
        assert_eq!(total_bucketed, 1);
        assert_eq!(body["grantLatency"]["totalDecided"], 1);

        let escalations = body["escalations"].as_array().unwrap();
        assert!(!escalations.is_empty());
        let grant_row = escalations
            .iter()
            .find(|e| e["kind"] == "grant")
            .expect("grant escalation row present");
        assert_eq!(grant_row["missionId"], "m-1");
        assert_eq!(grant_row["summary"], "cargo test");
        assert_eq!(grant_row["decision"], "approved");
        assert!(grant_row["latencyMs"].is_number());

        let revision_row = escalations
            .iter()
            .find(|e| e["kind"] == "revision")
            .expect("revision escalation row present");
        assert_eq!(revision_row["missionId"], "m-1");
        assert_eq!(revision_row["summary"], "add tests");
        assert_eq!(revision_row["decision"], "accepted (rev 1)");
    }

    #[tokio::test]
    async fn escalation_metrics_endpoint_empty_repo_returns_none_rates() {
        let tmp = TempDir::new().unwrap();
        let app = crate::router(tmp.path().to_path_buf(), None);

        let response = app.oneshot(get("/api/escalation-metrics")).await.unwrap();
        assert_eq!(response.status(), StatusCode::OK);
        let body = body_json(response).await;

        assert_eq!(body["autonomy"]["closedMissions"], 0);
        assert!(body["autonomy"]["zeroInterventionShare"].is_null());
        assert_eq!(body["rubberStamp"]["decidedGrants"], 0);
        assert!(body["rubberStamp"]["p50Ms"].is_null());
        assert_eq!(body["falseGreens"]["completedMissions"], 0);
        assert!(body["falseGreens"]["falseGreenRate"].is_null());
        assert_eq!(body["ledger"].as_array().unwrap().len(), 0);
    }

    #[tokio::test]
    async fn flight_rules_metrics_endpoint_empty_repo_is_machine_readable() {
        let tmp = TempDir::new().unwrap();
        let app = crate::router(tmp.path().to_path_buf(), None);

        let response = app.oneshot(get("/api/standards-metrics")).await.unwrap();
        assert_eq!(response.status(), StatusCode::OK);
        let body = body_json(response).await;

        assert_eq!(body["minimumSamples"], 5);
        assert!(body["definitions"]
            .as_array()
            .is_some_and(|rows| !rows.is_empty()));
        assert_eq!(body["rules"].as_array().unwrap().len(), 0);
    }

    #[tokio::test]
    async fn escalation_metrics_endpoint_seeded_repo_joins_traced_defects() {
        let tmp = TempDir::new().unwrap();
        seed_mission(
            tmp.path(),
            "m-1",
            vec![
                created("seeded"),
                EventKind::GrantRequested {
                    milestone_id: "ms-1".into(),
                    kind: GrantKind::Command,
                    command: "cargo test".into(),
                },
                EventKind::GrantApproved {
                    kind: GrantKind::Command,
                    command: "cargo test".into(),
                },
                EventKind::MissionCompleted {},
            ],
        );
        seed_mission(
            tmp.path(),
            "m-2",
            vec![created("clean"), EventKind::MissionCompleted {}],
        );
        // A hand-edited defect ticket traces back to m-1 (the grant-steered
        // completion); the clean m-2 stays out of the false-green count.
        let tickets = kranz_engine::ticket::Ticket::tickets_dir(tmp.path());
        std::fs::create_dir_all(&tickets).unwrap();
        std::fs::write(
            tickets.join("defect-regression.md"),
            "---\ntitle: Regression\ntraced-from-mission: m-1\n---\n\n## Goal\nfix\n",
        )
        .unwrap();

        let app = crate::router(tmp.path().to_path_buf(), None);
        let response = app.oneshot(get("/api/escalation-metrics")).await.unwrap();
        assert_eq!(response.status(), StatusCode::OK);
        let body = body_json(response).await;

        assert_eq!(body["autonomy"]["closedMissions"], 2);
        assert_eq!(body["autonomy"]["zeroInterventionMissions"], 1);
        assert_eq!(body["autonomy"]["zeroInterventionShare"], 0.5);
        assert_eq!(body["autonomy"]["completed"]["missions"], 2);
        assert_eq!(body["autonomy"]["completed"]["zeroIntervention"], 1);

        assert_eq!(body["rubberStamp"]["decidedGrants"], 1);
        assert_eq!(body["rubberStamp"]["underTenSeconds"], 1);
        assert!(body["rubberStamp"]["p50Ms"].is_number());

        assert_eq!(body["falseGreens"]["completedMissions"], 2);
        assert_eq!(body["falseGreens"]["falseGreens"], 1);
        assert_eq!(body["falseGreens"]["falseGreenRate"], 0.5);
        assert_eq!(body["falseGreens"]["withInterventions"]["falseGreens"], 1);
        assert_eq!(body["falseGreens"]["zeroIntervention"]["falseGreens"], 0);
        assert_eq!(
            body["falseGreens"]["tracedDefects"][0]["ticket"],
            "defect-regression"
        );
        assert_eq!(body["falseGreens"]["tracedDefects"][0]["missionId"], "m-1");

        let ledger = body["ledger"].as_array().unwrap();
        assert_eq!(ledger.len(), 1);
        assert_eq!(ledger[0]["kind"], "grant");
        assert_eq!(ledger[0]["missionId"], "m-1");
        assert_eq!(ledger[0]["milestoneId"], "ms-1");
        assert_eq!(ledger[0]["ask"], "command: cargo test");
        assert_eq!(ledger[0]["decision"], "approved");
        assert!(ledger[0]["latencyMs"].is_number());
    }

    #[tokio::test]
    async fn outcomes_report_cost_per_merged_endpoint_defaults_and_validates_window() {
        let tmp = TempDir::new().unwrap();
        seed_mission(
            tmp.path(),
            "m-1",
            vec![created("seeded"), EventKind::MissionCompleted {}],
        );
        let app = crate::router(tmp.path().to_path_buf(), None);

        // Default window: 30 days; a repo that merged nothing reads absent,
        // never zero.
        let response = app
            .clone()
            .oneshot(get("/api/cost-per-merged-change"))
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::OK);
        let body = body_json(response).await;
        assert_eq!(body["windowDays"], 30);
        assert_eq!(body["closedInWindow"], 1);
        assert_eq!(body["mergedChanges"], 0);
        assert!(body["usdPerMergedChange"].is_null());
        assert_eq!(body["zeroInterventionShare"], 1.0);

        // The window is a parameter.
        let response = app
            .clone()
            .oneshot(get("/api/cost-per-merged-change?windowDays=7"))
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::OK);
        let body = body_json(response).await;
        assert_eq!(body["windowDays"], 7);

        // A malformed window is a 400, never a silent default.
        let response = app
            .oneshot(get("/api/cost-per-merged-change?windowDays=abc"))
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::BAD_REQUEST);
    }

    #[tokio::test]
    async fn outcomes_report_outcomes_endpoint_carries_the_new_fold_sections() {
        let tmp = TempDir::new().unwrap();
        seed_mission(
            tmp.path(),
            "m-1",
            vec![
                created("do the thing\n\n## Task class\nexecution-class\n"),
                EventKind::GrantRequested {
                    milestone_id: "ms-1".into(),
                    kind: GrantKind::Command,
                    command: "cargo test".into(),
                },
                EventKind::GrantApproved {
                    kind: GrantKind::Command,
                    command: "cargo test".into(),
                },
                EventKind::MissionCompleted {},
            ],
        );
        let app = crate::router(tmp.path().to_path_buf(), None);

        let response = app.oneshot(get("/api/missions/outcomes")).await.unwrap();
        assert_eq!(response.status(), StatusCode::OK);
        let body = body_json(response).await;

        // KRZ-321: the per-task-class row (same fold, class grouping).
        let classes = body["taskClasses"].as_array().unwrap();
        assert_eq!(classes.len(), 1);
        assert_eq!(classes[0]["taskClass"], "execution-class");
        assert_eq!(classes[0]["missions"], 1);
        assert_eq!(classes[0]["advisorInvocations"], 1);
        // KRZ-323: the flag row plus the per-grant marker (seed_mission's
        // request→approval gap lands far under the 10s default threshold).
        assert_eq!(body["rubberStamp"]["thresholdMs"], 10_000);
        assert_eq!(body["rubberStamp"]["flagged"], 1);
        assert_eq!(body["rubberStamp"]["approvedDecisions"], 1);
        let grant = body["escalations"]
            .as_array()
            .unwrap()
            .iter()
            .find(|r| r["kind"] == "grant")
            .unwrap()
            .clone();
        assert_eq!(grant["rubberStamp"], true);
    }

    /// KRZ-333: the same endpoint also serves the industry-comparison set as
    /// a structurally separate section with its inline definitions — the CLI
    /// and the served payload stay one wire shape. The tempdir is no git
    /// repo, so the git-derived slots read absent naming their dependency.
    #[tokio::test]
    async fn comparison_metrics_outcomes_endpoint_serves_the_section() {
        let tmp = TempDir::new().unwrap();
        seed_mission(
            tmp.path(),
            "m-1",
            vec![created("seeded"), EventKind::MissionCompleted {}],
        );
        let app = crate::router(tmp.path().to_path_buf(), None);

        let response = app.oneshot(get("/api/missions/outcomes")).await.unwrap();
        assert_eq!(response.status(), StatusCode::OK);
        let body = body_json(response).await;

        let comparison = &body["comparison"];
        assert_eq!(comparison["windowDays"], 30);
        assert!(comparison["assistedChangeShare"]["definition"]
            .as_str()
            .unwrap()
            .contains("agent-involved by construction"));
        assert!(comparison["defectDensity"]["definition"]
            .as_str()
            .unwrap()
            .contains("traced-from-mission frontmatter"));
        // The empty resolution slot names its missing lifecycle timestamps.
        assert!(comparison["defectResolutionTime"]["dependency"]
            .as_str()
            .unwrap()
            .contains("open/close timestamps"));
    }

    /// `workspaceLifecycle` (ticket workspace-idle-hibernate): present with
    /// the folded transition state + its event ts when a teardown carried an
    /// outcome, null when none did (v1 keep-only logs) — consumers degrade
    /// on null.
    #[tokio::test]
    async fn workspace_endpoint_surfaces_workspace_lifecycle_present_and_absent() {
        let tmp = TempDir::new().unwrap();
        seed_mission(
            tmp.path(),
            "m-1",
            vec![
                created("lifecycle"),
                EventKind::WorkspaceTeardown {
                    mode: "hibernate".into(),
                    state: Some("stopped".into()),
                },
            ],
        );
        let app = crate::router(tmp.path().to_path_buf(), None);
        let response = app
            .oneshot(get("/api/missions/m-1/workspace"))
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::OK);
        let body = body_json(response).await;
        assert_eq!(body["workspaceLifecycle"]["state"], "stopped");
        assert!(
            body["workspaceLifecycle"]["ts"].as_str().is_some(),
            "the transition ts rides the field (the workspace-hours anchor): {body}"
        );

        seed_mission(
            tmp.path(),
            "m-2",
            vec![
                created("no-lifecycle"),
                EventKind::WorkspaceTeardown {
                    mode: "keep".into(),
                    state: None,
                },
            ],
        );
        let app = crate::router(tmp.path().to_path_buf(), None);
        let response = app
            .oneshot(get("/api/missions/m-2/workspace"))
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::OK);
        let body = body_json(response).await;
        assert!(
            body["workspaceLifecycle"].is_null(),
            "null when no teardown carried an outcome: {body}"
        );
    }

    fn sample_plan() -> kranz_engine::types::Plan {
        kranz_engine::types::Plan {
            goal: "g".into(),
            validation_contract: vec![],
            milestones: vec![],
            considered_alternatives: None,
            command_grants: vec![],
            touch_set: vec![],
            standards_manifest: None,
            reviewer_independence: None,
        }
    }

    /// P1 mission-path-no-follow: a catalog line whose id traverses out of
    /// the missions dir is rejected before any filesystem access — it never
    /// becomes a row (and never resolves a path).
    #[tokio::test]
    async fn list_missions_rejects_catalog_ids_with_traversal() {
        let tmp = TempDir::new().unwrap();
        let missions_dir = tmp.path().join(".kranz").join("missions");
        std::fs::create_dir_all(&missions_dir).unwrap();
        std::fs::write(
            missions_dir.join("index.md"),
            "# Kranz missions\n\n\
             - 2026-07-28 · [../../../tmp/evil](../../../tmp/evil/plan.md) — traversal\n\
             - 2026-07-28 · [m-ghost](m-ghost/plan.md) — ghost\n",
        )
        .unwrap();
        let app = crate::router(tmp.path().to_path_buf(), None);
        let response = app.oneshot(get("/api/missions")).await.unwrap();
        assert_eq!(response.status(), StatusCode::OK);
        let body = body_json(response).await;
        let ids: Vec<&str> = body
            .as_array()
            .unwrap()
            .iter()
            .filter_map(|row| row["id"].as_str())
            .collect();
        assert_eq!(
            ids,
            vec!["m-ghost"],
            "traversal catalog id rejected, legit ghost kept: {ids:?}"
        );
    }

    // Symlink-creating tests are unix-only, exactly like the lessons guard's
    // tests in the engine; Windows needs privileges to create symlinks.

    /// P1 mission-path-no-follow: a symlinked `.kranz/missions/<id>` is a
    /// clear error row on enumerate — never a read into the target's tree.
    #[cfg(unix)]
    #[tokio::test]
    async fn list_missions_surfaces_a_symlinked_mission_dir_as_an_error_row() {
        use std::os::unix::fs::symlink;
        let tmp = TempDir::new().unwrap();
        // The symlink target is another tree's real mission, with data that
        // must not leak into this repo's listing.
        let elsewhere = TempDir::new().unwrap();
        seed_mission(elsewhere.path(), "m-evil", vec![created("other repo goal")]);
        let missions_dir = tmp.path().join(".kranz").join("missions");
        std::fs::create_dir_all(&missions_dir).unwrap();
        symlink(
            elsewhere
                .path()
                .join(".kranz")
                .join("missions")
                .join("m-evil"),
            missions_dir.join("m-evil"),
        )
        .unwrap();
        // A catalog line keeps the symlinked id in the union (the on-disk
        // scan already excludes it as not-a-real-dir).
        std::fs::write(
            missions_dir.join("index.md"),
            "# Kranz missions\n\n- 2026-07-28 · [m-evil](m-evil/plan.md) — evil\n",
        )
        .unwrap();
        let app = crate::router(tmp.path().to_path_buf(), None);
        let response = app.oneshot(get("/api/missions")).await.unwrap();
        assert_eq!(response.status(), StatusCode::OK);
        let body = body_json(response).await;
        let row = body
            .as_array()
            .unwrap()
            .iter()
            .find(|row| row["id"] == "m-evil")
            .expect("the symlinked mission surfaces as an error row");
        assert_eq!(row["status"], "failed", "{row}");
        assert!(
            row["error"].as_str().unwrap().contains("refusing"),
            "clear refusal: {row}"
        );
        assert!(
            row.get("goal").is_none(),
            "nothing read through the symlink: {row}"
        );
    }

    /// P1 mission-path-no-follow: direct mission-path resolution (every
    /// `/api/missions/:id/...` handler goes through `mission_paths`) refuses
    /// a symlinked mission dir as an unknown mission.
    #[cfg(unix)]
    #[tokio::test]
    async fn mission_state_refuses_a_symlinked_mission_dir() {
        use std::os::unix::fs::symlink;
        let tmp = TempDir::new().unwrap();
        let elsewhere = TempDir::new().unwrap();
        seed_mission(elsewhere.path(), "m-evil", vec![created("other repo goal")]);
        let missions_dir = tmp.path().join(".kranz").join("missions");
        std::fs::create_dir_all(&missions_dir).unwrap();
        symlink(
            elsewhere
                .path()
                .join(".kranz")
                .join("missions")
                .join("m-evil"),
            missions_dir.join("m-evil"),
        )
        .unwrap();
        let app = crate::router(tmp.path().to_path_buf(), None);
        let response = app
            .oneshot(get("/api/missions/m-evil/state"))
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::NOT_FOUND);
    }

    /// Audit (server MEDIUM): the mission-dir walk is no-follow but the LEAF
    /// read was not, so a session under checkout isolation could point
    /// `plan.md` at `.kranz/serve.token` and read the mutation token back
    /// over a tokenless loopback GET. The leaf read is no-follow now: the
    /// route refuses instead of returning the target's bytes.
    #[cfg(unix)]
    #[tokio::test]
    async fn mission_leaf_reads_refuse_a_symlinked_file() {
        use std::os::unix::fs::symlink;
        let tmp = TempDir::new().unwrap();
        seed_mission(tmp.path(), "m-1", vec![created("goal")]);
        let token_file = tmp.path().join(".kranz").join("serve.token");
        std::fs::write(&token_file, "super-secret-mutation-token").unwrap();
        let paths = MissionPaths::new(tmp.path(), "m-1");
        symlink(&token_file, paths.plan_md_file()).unwrap();
        symlink(&token_file, paths.report_file()).unwrap();
        symlink(&token_file, paths.plan_file()).unwrap();

        for uri in [
            "/api/missions/m-1/plan.md",
            "/api/missions/m-1/report.md",
            "/api/missions/m-1/plan",
        ] {
            let app = crate::router(tmp.path().to_path_buf(), None);
            let response = app.oneshot(get(uri)).await.unwrap();
            assert_ne!(
                response.status(),
                StatusCode::OK,
                "{uri} must refuse a symlinked leaf"
            );
            let body = body_json(response).await;
            assert!(
                !body.to_string().contains("super-secret-mutation-token"),
                "{uri} leaked the symlink target: {body}"
            );
        }
    }

    /// The same leaf rule for the tickets surface, which reads through
    /// `Ticket::load`.
    #[cfg(unix)]
    #[tokio::test]
    async fn ticket_reads_refuse_a_symlinked_file() {
        use std::os::unix::fs::symlink;
        let tmp = TempDir::new().unwrap();
        let token_file = tmp.path().join(".kranz").join("serve.token");
        std::fs::create_dir_all(tmp.path().join(".kranz")).unwrap();
        std::fs::write(&token_file, "super-secret-mutation-token").unwrap();
        let tickets = tmp.path().join(".kranz").join("tickets");
        std::fs::create_dir_all(&tickets).unwrap();
        symlink(&token_file, tickets.join("leak.md")).unwrap();

        let app = crate::router(tmp.path().to_path_buf(), None);
        let response = app.oneshot(get("/api/tickets/leak")).await.unwrap();
        assert_ne!(response.status(), StatusCode::OK);
        let body = body_json(response).await;
        assert!(
            !body.to_string().contains("super-secret-mutation-token"),
            "ticket read leaked the symlink target: {body}"
        );

        let app = crate::router(tmp.path().to_path_buf(), None);
        let response = app.oneshot(get("/api/tickets")).await.unwrap();
        let body = body_json(response).await;
        assert!(
            !body.to_string().contains("super-secret-mutation-token"),
            "ticket listing leaked the symlink target: {body}"
        );
    }

    /// The missions catalog (`.kranz/missions/index.md`) is read the same
    /// way: a symlinked index yields an empty catalog, never the target.
    #[cfg(unix)]
    #[tokio::test]
    async fn missions_index_read_refuses_a_symlink() {
        use std::os::unix::fs::symlink;
        let tmp = TempDir::new().unwrap();
        let missions_dir = tmp.path().join(".kranz").join("missions");
        std::fs::create_dir_all(&missions_dir).unwrap();
        let token_file = tmp.path().join(".kranz").join("serve.token");
        std::fs::write(&token_file, "super-secret-mutation-token").unwrap();
        symlink(&token_file, missions_dir.join("index.md")).unwrap();

        let app = crate::router(tmp.path().to_path_buf(), None);
        let response = app.oneshot(get("/api/missions")).await.unwrap();
        assert_eq!(response.status(), StatusCode::OK);
        let body = body_json(response).await;
        assert!(
            !body.to_string().contains("super-secret-mutation-token"),
            "the catalog read followed a symlink: {body}"
        );
    }

    /// 12th-pass review: an unbounded `windowDays` once reached the engine's
    /// `as i64` cast / chrono arithmetic and could wrap or panic the handler
    /// — a read-authorized request crashing its own endpoint. Over the
    /// documented maximum is a 400 now; the bound itself still computes.
    #[tokio::test]
    async fn cost_per_merged_change_window_days_bound_over_max_gets_400() {
        let tmp = TempDir::new().unwrap();
        let app = crate::router(tmp.path().to_path_buf(), None);

        for raw in [
            format!(
                "{}",
                kranz_engine::outcomes::MAX_MERGED_CHANGE_WINDOW_DAYS + 1
            ),
            u64::MAX.to_string(),
        ] {
            let response = app
                .clone()
                .oneshot(get(&format!(
                    "/api/cost-per-merged-change?windowDays={raw}"
                )))
                .await
                .unwrap();
            assert_eq!(
                response.status(),
                StatusCode::BAD_REQUEST,
                "windowDays={raw} must be a 400, never a crash"
            );
        }
        // At the bound the endpoint computes normally (empty repo zeroes).
        let response = app
            .oneshot(get(&format!(
                "/api/cost-per-merged-change?windowDays={}",
                kranz_engine::outcomes::MAX_MERGED_CHANGE_WINDOW_DAYS
            )))
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::OK);
        let body = body_json(response).await;
        assert_eq!(
            body["windowDays"],
            kranz_engine::outcomes::MAX_MERGED_CHANGE_WINDOW_DAYS
        );
    }
}