agent-file-tools 0.54.0

Agent File Tools — tree-sitter powered code analysis for AI agents
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
//! Credential-free routing shim for the `gh` argv[0] entry point.
//!
//! The shim is intentionally a small process boundary: R1/R2 and declared
//! mechanical R3 operations replace this process with upstream `gh`, while the
//! governed path is the only path that interprets a declared command shape.
//!
//! Governed invocations are executed seam-side by the route holder under full
//! GitHub App installation tokens held in custody; the shim carries a routed
//! request one way and a result-or-refusal the other, and holds no token in
//! either direction. Operation gating is holder-side classification over the
//! routed request, not a property of any token the shim can see or hold.

use std::collections::{BTreeMap, BTreeSet};
use std::ffi::{OsStr, OsString};
use std::fs::{self, OpenOptions};
use std::io::{self, Read, Write};
use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use base64::Engine;
use ring::signature::{UnparsedPublicKey, ED25519};
use serde::{Deserialize, Serialize};
use serde_json::{json, Map, Value};
use subc_client_rs::{CallOptions, CloseRouteOptions, ConsumerOptions, SubcConsumer};
use subc_protocol::manifest::ProviderRole;
use subc_protocol::{BindIdentity, RouteTarget};

pub const SCHEMA_FLOOR: u64 = 1;
/// Envelope version that carries the manifest as exact signed bytes. Envelope
/// v1 re-serialized the parsed manifest at verify time; envelope v2 verifies
/// the distributed bytes themselves (see the verifier-site contract).
pub const ENVELOPE_VERSION: u64 = 2;
pub const REFUSAL_EXIT_STATUS: i32 = 86;
const UPSTREAM_FAILURE_EXIT_STATUS: i32 = 1;
const DISCOVERY_BUDGET: Duration = Duration::from_millis(150);
const DISCOVERY_CACHE_TTL: Duration = Duration::from_secs(15);
/// Clock skew tolerated before a manifest's signed issue time counts as being
/// in the future and therefore invalid.
const ISSUED_AT_FUTURE_SKEW: Duration = Duration::from_secs(300);
const ROUTING_OPERATION: &str = "gh.route";
const ROUTING_HOLDER_MODULE_ID: &str = "prefrontal-core";
const MANIFEST_ARTIFACT_ID: &str = "gh-routing-manifest";
const V1_GOVERNED_TUPLES: &[&str] = &["issue comment", "pr comment", "pr review", "issue reaction"];
const V1_ADMIN_TUPLES: &[&str] = &["issue close", "pr close", "pr merge", "release create"];
const V9_ADMIN_TUPLES: &[&str] = &["repo edit", "run delete"];
const V10_ADMIN_TUPLES: &[&str] = &["workflow run"];
// The v10 manifest version is the first version whose code-side allowlist
// permits these native comment mutations. The allowlist covers only the exact
// flag variants below and does not broaden raw API writes.
const V10_EDIT_LAST_TUPLES: &[&str] = &["issue comment", "pr comment"];
const READ_ONLY_ACTION_TUPLES: &[&str] = &[
    "run view",
    "run list",
    "run watch",
    "workflow view",
    "workflow list",
];
const RESERVED_SELF_REPORT: &[&str] = &["--status", "--shim-version"];
const CO_AUTHOR_LINE_REPORT: &str = "--co-author-line";
const GOVERNANCE_UNAVAILABLE_TEXT: &str = "the governance daemon is unreachable and this repository's actions are identity-governed; retry after the daemon returns";
const UNTRUSTED_MANIFEST_KEY_STEERING: &str = "the manifest may be newer than this aft build's trust set - update aft, or install a manifest signed by a trusted key";
const PRE_PROVENANCE_RECORD: &str = "unrecorded (pre-provenance record)";

/// The only shim-originated refusal identifiers. Keep this enumeration closed:
/// callers must parse these identifiers rather than human prose.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum RefusalCode {
    Unclassified,
    AdminTier,
    ManifestBelowFloor,
    ManifestRegressed,
    SeamSchemaMismatch,
    UnboundIdentity,
    BypassAuditUnavailable,
    NoRealGh,
    GovernanceUnavailable,
    SeamUnavailable,
    SeamRefusal,
}

impl RefusalCode {
    pub const ALL: [Self; 11] = [
        Self::Unclassified,
        Self::AdminTier,
        Self::ManifestBelowFloor,
        Self::ManifestRegressed,
        Self::SeamSchemaMismatch,
        Self::UnboundIdentity,
        Self::BypassAuditUnavailable,
        Self::NoRealGh,
        Self::GovernanceUnavailable,
        Self::SeamUnavailable,
        Self::SeamRefusal,
    ];

    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Unclassified => "gh_shim_unclassified",
            Self::AdminTier => "gh_shim_admin_tier",
            Self::ManifestBelowFloor => "gh_shim_manifest_below_floor",
            Self::ManifestRegressed => "gh_shim_manifest_regressed",
            Self::SeamSchemaMismatch => "gh_shim_seam_schema_mismatch",
            Self::UnboundIdentity => "gh_shim_unbound_identity",
            Self::BypassAuditUnavailable => "gh_shim_bypass_audit_unavailable",
            Self::NoRealGh => "gh_shim_no_real_gh",
            Self::GovernanceUnavailable => "gh_shim_governance_unavailable",
            Self::SeamUnavailable => "gh_shim_seam_unavailable",
            Self::SeamRefusal => "gh_shim_seam_refusal",
        }
    }
}

/// Offline self-report uses diagnostic identifiers distinct from invocation
/// refusals. A report can therefore describe historical local-state trouble
/// without pretending that an upstream `gh` invocation was refused.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum SelfReportDiagnostic {
    ManifestUnavailable,
    ManifestInvalid,
    ManifestBelowFloor,
    ManifestRegressed,
    ManifestRollback,
    RungUnavailable,
}

impl SelfReportDiagnostic {
    pub const ALL: [Self; 6] = [
        Self::ManifestUnavailable,
        Self::ManifestInvalid,
        Self::ManifestBelowFloor,
        Self::ManifestRegressed,
        Self::ManifestRollback,
        Self::RungUnavailable,
    ];

    pub const fn as_str(self) -> &'static str {
        match self {
            Self::ManifestUnavailable => "gh_shim_status_manifest_unavailable",
            Self::ManifestInvalid => "gh_shim_status_manifest_invalid",
            Self::ManifestBelowFloor => "gh_shim_status_manifest_below_floor",
            Self::ManifestRegressed => "gh_shim_status_manifest_regressed",
            Self::ManifestRollback => "gh_shim_status_manifest_rollback",
            Self::RungUnavailable => "gh_shim_status_rung_unavailable",
        }
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Tier {
    Mechanical,
    Governed,
    Admin,
}

impl Tier {
    fn rank(self) -> u8 {
        match self {
            Self::Mechanical => 0,
            Self::Governed => 1,
            Self::Admin => 2,
        }
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum Rung {
    R1,
    R2,
    R3,
}

impl Rung {
    const fn label(self) -> &'static str {
        match self {
            Self::R1 => "R1",
            Self::R2 => "R2",
            Self::R3 => "R3",
        }
    }
}

/// Return true when the process was invoked through the `gh` symlink or the
/// explicit `aft gh-shim` development entry point. This is public so the binary
/// can perform it before its own global `--version` and `--subc` scans.
pub fn is_shim_invocation(program: &OsStr, args: &[OsString]) -> bool {
    Path::new(program)
        .file_name()
        .is_some_and(|name| name == OsStr::new("gh"))
        || args.first().is_some_and(|arg| arg == OsStr::new("gh-shim"))
}

pub fn is_shim_invocation_from_env() -> bool {
    let mut argv = std::env::args_os();
    let Some(program) = argv.next() else {
        return false;
    };
    is_shim_invocation(&program, &argv.collect::<Vec<_>>())
}

/// Execute the shim for either supported entry form. This intentionally runs
/// before logging initialization so delegating invocations cannot add shim bytes
/// to upstream stderr.
pub fn run_from_env() -> i32 {
    let mut argv = std::env::args_os();
    let Some(program) = argv.next() else {
        return refuse(RefusalCode::NoRealGh, "the executing image was unavailable");
    };
    let raw_args = argv.collect::<Vec<_>>();
    let shim_args = if Path::new(&program)
        .file_name()
        .is_some_and(|name| name == OsStr::new("gh"))
    {
        raw_args
    } else {
        raw_args.into_iter().skip(1).collect()
    };
    run(&shim_args)
}

fn run(args: &[OsString]) -> i32 {
    let paths = StatePaths::from_process();
    if args.first().and_then(|arg| arg.to_str()) == Some(CO_AUTHOR_LINE_REPORT) {
        if let Some(line) = co_author_line(&paths) {
            println!("{line}");
        }
        return 0;
    }
    if is_reserved_self_report(args) {
        print_self_report(&paths);
        return 0;
    }

    let now = unix_seconds();
    let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));

    // Presence-based regressed-manifest arm. Decide from the installed artifact
    // BEFORE any rung probe so a governed refusal never depends on daemon
    // reachability: a validation failure after a prior valid manifest makes
    // governed/admin tuples refuse while mechanical operations pass through.
    let initial_manifest = resolve_manifest(&paths, now);
    let invalid_manifest_problem = initial_manifest.invalid_problem().cloned();
    if let ManifestResolution::Regressed { manifest, problem } = &initial_manifest {
        return match regressed_disposition(args, manifest, current_platform(), problem) {
            RegressedDisposition::Passthrough => {
                delegate_after_invalid_manifest_notice(args, problem)
            }
            RegressedDisposition::Refuse { code, text } => refuse(code, &text),
        };
    }

    let determination = determine_rung(&paths, &cwd, now);
    if determination.record.rung != Rung::R3 {
        let disposition = match resolve_manifest(&paths, now) {
            ManifestResolution::Active(manifest) => non_r3_governance_disposition(
                &cwd,
                &determination,
                args,
                &manifest,
                current_platform(),
            ),
            ManifestResolution::Regressed { .. }
            | ManifestResolution::Invalid(_)
            | ManifestResolution::Dormant => GovernanceDisposition::Delegate,
        };
        return match disposition {
            GovernanceDisposition::Unavailable(agent_binding) => {
                refuse_governance_unavailable(&paths, &agent_binding, now)
            }
            GovernanceDisposition::Unclassified { manifest_version } => refuse(
                RefusalCode::Unclassified,
                &format!(
                    "no manifest declaration for this invocation (manifest {manifest_version})"
                ),
            ),
            GovernanceDisposition::Delegate | GovernanceDisposition::Ready => {
                match invalid_manifest_problem.as_ref() {
                    Some(problem) => delegate_after_invalid_manifest_notice(args, problem),
                    None => delegate(args),
                }
            }
        };
    }

    // A valid manifest gates R3 both during fresh discovery and when a cached
    // R3 determination is reused. If it disappears or fails validation between
    // those two moments, the whole invocation falls back to R2 passthrough
    // instead of a classification-shaped refusal.
    let manifest = match resolve_manifest(&paths, now) {
        ManifestResolution::Active(manifest) => manifest,
        ManifestResolution::Regressed { manifest, problem } => {
            return match regressed_disposition(args, &manifest, current_platform(), &problem) {
                RegressedDisposition::Passthrough => {
                    delegate_after_invalid_manifest_notice(args, &problem)
                }
                RegressedDisposition::Refuse { code, text } => refuse(code, &text),
            }
        }
        ManifestResolution::Invalid(problem) => {
            return delegate_after_invalid_manifest_notice(args, &problem)
        }
        ManifestResolution::Dormant => return delegate(args),
    };
    let Some(agent_binding) = resolved_agent_binding(&manifest, &cwd) else {
        return delegate(args);
    };

    match classify(args, &manifest, current_platform()) {
        Classification::Mechanical => delegate(args),
        Classification::Admin { tuple } => {
            if std::env::var_os("GH_SHIM_BYPASS").as_deref() == Some(OsStr::new("operator")) {
                let repository = explicit_repo(args).or_else(infer_repository_from_git);
                if let Err(error) = append_bypass_audit(&paths, &tuple, repository.as_deref(), now)
                {
                    return refuse(
                        RefusalCode::BypassAuditUnavailable,
                        &format!("operator bypass audit could not be appended: {error}"),
                    );
                }
                delegate(args)
            } else {
                refuse(
                    RefusalCode::AdminTier,
                    "this action requires GH_SHIM_BYPASS=operator",
                )
            }
        }
        Classification::Governed { tuple, canonical } => {
            let request =
                match canonicalize_governed(args, &tuple, &canonical, manifest.manifest_version) {
                    Ok(request) => request,
                    Err(error) => return refuse_governed_canonicalization(&error),
                };
            let outcome =
                route_governed(&paths, &determination.record, &agent_binding, request, now);
            governed_outcome_status(&paths, &agent_binding, now, outcome)
        }
        Classification::Unclassified => refuse(
            RefusalCode::Unclassified,
            &format!(
                "no manifest declaration for this invocation (manifest {})",
                manifest.manifest_version
            ),
        ),
    }
}

fn refuse_governed_canonicalization(error: &str) -> i32 {
    refuse(RefusalCode::Unclassified, error)
}

fn governed_outcome_status(
    paths: &StatePaths,
    agent_binding: &AgentBinding,
    now: u64,
    outcome: RouteOutcome,
) -> i32 {
    match outcome {
        RouteOutcome::Result(output) => {
            print!("{output}");
            0
        }
        RouteOutcome::UpstreamError(body) => {
            eprintln!("{body}");
            UPSTREAM_FAILURE_EXIT_STATUS
        }
        RouteOutcome::Refusal(code) => refuse(RefusalCode::SeamRefusal, &seam_refusal_text(&code)),
        RouteOutcome::UnboundIdentity => refuse(
            RefusalCode::UnboundIdentity,
            "the project binding was unavailable at route time",
        ),
        RouteOutcome::SchemaMismatch(message) => refuse(RefusalCode::SeamSchemaMismatch, &message),
        RouteOutcome::GovernanceUnavailable => {
            refuse_governance_unavailable(paths, agent_binding, now)
        }
        RouteOutcome::Unavailable(message) => refuse(RefusalCode::SeamUnavailable, &message),
    }
}

fn seam_refusal_text(code: &str) -> String {
    format!("governance seam refused the action: {code}")
}

fn is_reserved_self_report(args: &[OsString]) -> bool {
    args.first()
        .and_then(|arg| arg.to_str())
        .is_some_and(|arg| RESERVED_SELF_REPORT.contains(&arg))
}

#[derive(Clone, Debug)]
struct StatePaths {
    root: PathBuf,
    manifest: PathBuf,
    rung: PathBuf,
    bypass_audit: PathBuf,
    unexpected_gh_route_advertisers: PathBuf,
    seam_state: PathBuf,
    last_valid_manifest: PathBuf,
    version_high_water: PathBuf,
    numeric_ids: PathBuf,
}

impl StatePaths {
    fn from_process() -> Self {
        let root = std::env::var_os("XDG_STATE_HOME")
            .map(PathBuf::from)
            .filter(|path| path.is_absolute())
            .or_else(|| {
                std::env::var_os("HOME")
                    .or_else(|| std::env::var_os("USERPROFILE"))
                    .map(|home| PathBuf::from(home).join(".local/state"))
            })
            .unwrap_or_else(|| std::env::temp_dir())
            .join("cortexkit")
            .join("aft")
            .join("gh-shim");
        Self::from_root(root)
    }

    fn from_root(root: PathBuf) -> Self {
        Self {
            manifest: root.join("gh-routing-manifest.json"),
            rung: root.join("rung-cache.json"),
            bypass_audit: root.join("operator-bypass.jsonl"),
            unexpected_gh_route_advertisers: root.join("unexpected-gh-route-advertisers.json"),
            seam_state: root.join("seam-state.json"),
            last_valid_manifest: root.join("last-valid-manifest.json"),
            version_high_water: root.join("manifest-version-high-water.json"),
            numeric_ids: root.join("numeric-ids.json"),
            root,
        }
    }
}

#[derive(Clone, Debug, Deserialize, Serialize)]
struct RungRecord {
    rung: Rung,
    as_of_unix_secs: u64,
    #[serde(default)]
    inputs: BTreeMap<String, String>,
    #[serde(default)]
    manifest_version: Option<u64>,
    #[serde(default)]
    recorded_by_image_path: Option<String>,
    #[serde(default)]
    recorded_by_version: Option<String>,
    #[serde(default)]
    recorded_by_repo_key: Option<String>,
}

#[derive(Clone, Debug)]
struct RungRecordProvenance {
    image_path: String,
    version: String,
    repo_key: String,
}

impl RungRecordProvenance {
    fn for_cwd(cwd: &Path) -> Self {
        let project_root = project_root_for(cwd);
        Self {
            image_path: executing_image().to_string_lossy().into_owned(),
            version: env!("CARGO_PKG_VERSION").to_string(),
            repo_key: repository_key_from_origin(&project_root)
                .unwrap_or_else(|| "unresolved (no GitHub origin)".to_string()),
        }
    }
}

impl RungRecord {
    fn fresh_at(&self, now: u64) -> bool {
        now.saturating_sub(self.as_of_unix_secs) < DISCOVERY_CACHE_TTL.as_secs()
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(usize)]
enum R1Reason {
    DisabledByConfig,
    AbsentOrUnparseable,
    Unreachable,
    DiscoveryBudgetExhausted,
    #[cfg(test)]
    Count,
}

impl R1Reason {
    #[cfg(test)]
    const ALL: [Self; Self::Count as usize] = [
        Self::DisabledByConfig,
        Self::AbsentOrUnparseable,
        Self::Unreachable,
        Self::DiscoveryBudgetExhausted,
    ];

    const fn diagnostic(self) -> &'static str {
        match self {
            Self::DisabledByConfig => "disabled_by_config",
            Self::AbsentOrUnparseable => "absent_or_unparseable",
            Self::Unreachable => "unreachable",
            Self::DiscoveryBudgetExhausted => "discovery_budget_exhausted",
            #[cfg(test)]
            Self::Count => unreachable!(),
        }
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(usize)]
enum R2Reason {
    ManifestUnavailable,
    AgentBindingUnavailable,
    AgentCredentialsPresent,
    DaemonUnreachable,
    CatalogGhRouteAbsent,
    GhRouteHolderUnbound,
    #[cfg(test)]
    Count,
}

impl R2Reason {
    #[cfg(test)]
    const ALL: [Self; Self::Count as usize] = [
        Self::ManifestUnavailable,
        Self::AgentBindingUnavailable,
        Self::AgentCredentialsPresent,
        Self::DaemonUnreachable,
        Self::CatalogGhRouteAbsent,
        Self::GhRouteHolderUnbound,
    ];

    const fn diagnostic(self) -> &'static str {
        match self {
            Self::ManifestUnavailable => "manifest_unavailable",
            Self::AgentBindingUnavailable => "agent_binding_unavailable",
            Self::AgentCredentialsPresent => "agent_credentials_present",
            Self::DaemonUnreachable => "daemon_unreachable",
            Self::CatalogGhRouteAbsent => "catalog_gh_route_absent",
            Self::GhRouteHolderUnbound => "gh_route_holder_unbound",
            #[cfg(test)]
            Self::Count => unreachable!(),
        }
    }
}

#[derive(Clone, Debug)]
struct RungDetermination {
    record: RungRecord,
    operator_disabled: bool,
}

impl RungDetermination {
    fn r1(now: u64, reason: R1Reason) -> Self {
        Self {
            record: RungRecord {
                rung: Rung::R1,
                as_of_unix_secs: now,
                inputs: BTreeMap::from([(
                    "connection_file".to_string(),
                    reason.diagnostic().to_string(),
                )]),
                manifest_version: None,
                recorded_by_image_path: None,
                recorded_by_version: None,
                recorded_by_repo_key: None,
            },
            operator_disabled: reason == R1Reason::DisabledByConfig,
        }
    }

    fn r2(
        now: u64,
        reason: R2Reason,
        manifest_version: Option<u64>,
        provenance: &RungRecordProvenance,
    ) -> Self {
        Self {
            record: RungRecord {
                rung: Rung::R2,
                as_of_unix_secs: now,
                inputs: BTreeMap::from([
                    ("connection_file".to_string(), "ready".to_string()),
                    (reason.diagnostic().to_string(), "failed".to_string()),
                ]),
                manifest_version,
                recorded_by_image_path: Some(provenance.image_path.clone()),
                recorded_by_version: Some(provenance.version.clone()),
                recorded_by_repo_key: Some(provenance.repo_key.clone()),
            },
            operator_disabled: false,
        }
    }

    fn r3(now: u64, manifest_version: u64, provenance: &RungRecordProvenance) -> Self {
        Self {
            record: RungRecord {
                rung: Rung::R3,
                as_of_unix_secs: now,
                inputs: BTreeMap::from([
                    ("connection_file".to_string(), "ready".to_string()),
                    ("catalog_gh_route".to_string(), "ready".to_string()),
                    ("agent_binding".to_string(), "ready".to_string()),
                    ("manifest".to_string(), "ready".to_string()),
                    (
                        "agent_credentials_present".to_string(),
                        "absent".to_string(),
                    ),
                ]),
                manifest_version: Some(manifest_version),
                recorded_by_image_path: Some(provenance.image_path.clone()),
                recorded_by_version: Some(provenance.version.clone()),
                recorded_by_repo_key: Some(provenance.repo_key.clone()),
            },
            operator_disabled: false,
        }
    }

    fn cached(record: RungRecord) -> Self {
        Self {
            record,
            operator_disabled: false,
        }
    }
}

#[derive(Debug)]
enum GovernanceDisposition {
    Delegate,
    Ready,
    Unavailable(AgentBinding),
    Unclassified { manifest_version: u64 },
}

fn structural_governance_disposition(
    determination: &RungDetermination,
    classification: &Classification,
    agent_binding: Option<AgentBinding>,
    manifest_version: u64,
) -> GovernanceDisposition {
    if determination.operator_disabled || matches!(classification, Classification::Mechanical) {
        return GovernanceDisposition::Delegate;
    }
    let Some(agent_binding) = agent_binding else {
        return GovernanceDisposition::Delegate;
    };
    if determination.record.rung == Rung::R3 {
        return GovernanceDisposition::Ready;
    }

    match classification {
        Classification::Governed { .. } | Classification::Admin { .. } => {
            GovernanceDisposition::Unavailable(agent_binding)
        }
        Classification::Unclassified => GovernanceDisposition::Unclassified { manifest_version },
        Classification::Mechanical => GovernanceDisposition::Delegate,
    }
}

fn non_r3_governance_disposition(
    cwd: &Path,
    determination: &RungDetermination,
    args: &[OsString],
    manifest: &Manifest,
    platform: &str,
) -> GovernanceDisposition {
    if determination.operator_disabled {
        return GovernanceDisposition::Delegate;
    }

    let classification = classify(args, manifest, platform);
    if matches!(classification, Classification::Mechanical) {
        return GovernanceDisposition::Delegate;
    }

    // Binding resolution runs `git` to inspect the origin. Classify first so
    // unmanifested public repositories keep the R1 fast path for mechanical
    // reads; only a verb that could refuse pays the subprocess latency.
    let agent_binding = resolved_agent_binding(manifest, cwd);
    structural_governance_disposition(
        determination,
        &classification,
        agent_binding,
        manifest.manifest_version,
    )
}

fn determine_rung(paths: &StatePaths, cwd: &Path, now: u64) -> RungDetermination {
    // The budget starts before the config read and connection-file stat. This
    // keeps a slow filesystem from silently extending discovery beyond 150ms.
    let deadline = std::time::Instant::now() + DISCOVERY_BUDGET;
    let config_doc = read_user_config_doc();
    determine_rung_from_doc(paths, cwd, now, deadline, config_doc.as_deref())
}

/// Pure rung determination over the user config document. `config_doc` is the
/// raw user-tier `aft.jsonc` text (already read by the caller); `None` means the
/// config file was absent or unreadable. Splitting the config read from the
/// decision keeps the disabled short-circuit testable without mutating process
/// env (which races under the parallel test runner).
fn determine_rung_from_doc(
    paths: &StatePaths,
    cwd: &Path,
    now: u64,
    deadline: std::time::Instant,
    config_doc: Option<&str>,
) -> RungDetermination {
    // Operator hard-off: when the user disables the shim, short-circuit to
    // byte-transparent passthrough (R1) before any daemon/catalog probing, so a
    // disabled shim performs no governance-daemon or catalog traffic. Explicit
    // operator intent beats manifest governance; this in-memory bit is deliberately
    // not inferred from the diagnostic reason string later in dispatch.
    if gh_shim_enabled_from_config_doc(config_doc.unwrap_or("")) == Some(false) {
        return RungDetermination::r1(now, R1Reason::DisabledByConfig);
    }

    let Some(connection_file) = connection_file_from_config_doc(config_doc.unwrap_or("")) else {
        // R1 has no daemon dial and no durable determination write.
        return RungDetermination::r1(now, R1Reason::AbsentOrUnparseable);
    };
    if !connection_file.is_file() {
        return RungDetermination::r1(now, R1Reason::Unreachable);
    }

    let cached = load_rung_record(paths);
    if std::time::Instant::now() >= deadline {
        return cached
            .filter(|record| record.fresh_at(now))
            .map(RungDetermination::cached)
            .unwrap_or_else(|| RungDetermination::r1(now, R1Reason::DiscoveryBudgetExhausted));
    }
    if let Some(record) = cached.as_ref().filter(|record| record.fresh_at(now)) {
        if record.rung != Rung::R3
            || resolve_manifest(paths, now)
                .manifest()
                .and_then(|manifest| resolved_agent_binding(manifest, cwd))
                .is_some()
        {
            return RungDetermination::cached(record.clone());
        }
    }

    let provenance = RungRecordProvenance::for_cwd(cwd);
    // The signed manifest supplies the binding before the probe opens a route, so
    // rate accounting and audit records use the same agent session on every run.
    // A failed validation does not supply a manifest here because the regressed
    // arm itself is decided in `run` before any probe.
    let Some(manifest) = resolve_manifest(paths, now).into_manifest() else {
        let determination =
            RungDetermination::r2(now, R2Reason::ManifestUnavailable, None, &provenance);
        write_rung_record_silently(paths, &determination.record);
        return determination;
    };
    let Some(agent_binding) = resolved_agent_binding(&manifest, cwd) else {
        let determination = RungDetermination::r2(
            now,
            R2Reason::AgentBindingUnavailable,
            Some(manifest.manifest_version),
            &provenance,
        );
        write_rung_record_silently(paths, &determination.record);
        return determination;
    };

    let discovery = probe_governance(
        paths,
        &connection_file,
        cwd,
        deadline,
        &agent_binding.agent_id,
    );
    let determination = match discovery {
        ProbeResult::Ready { module_id } => {
            match find_ambient_agent_credential(&manifest.detectors) {
                Some(source) => {
                    let mut determination = RungDetermination::r2(
                        now,
                        R2Reason::AgentCredentialsPresent,
                        Some(manifest.manifest_version),
                        &provenance,
                    );
                    determination
                        .record
                        .inputs
                        .insert("agent_credentials_present".to_string(), source);
                    determination
                        .record
                        .inputs
                        .insert("catalog_holder".to_string(), module_id);
                    determination
                }
                None => RungDetermination::r3(now, manifest.manifest_version, &provenance),
            }
        }
        ProbeResult::Unreachable => {
            RungDetermination::r2(now, R2Reason::DaemonUnreachable, None, &provenance)
        }
        ProbeResult::NoRoute => {
            RungDetermination::r2(now, R2Reason::CatalogGhRouteAbsent, None, &provenance)
        }
        // Keep the holder-unbound status diagnostic distinct from an absent
        // repository binding. Dispatch no longer consumes either reason.
        ProbeResult::Unbound => {
            RungDetermination::r2(now, R2Reason::GhRouteHolderUnbound, None, &provenance)
        }
        ProbeResult::TimedOut => cached
            .filter(|record| record.fresh_at(now))
            .map(RungDetermination::cached)
            .unwrap_or_else(|| RungDetermination::r1(now, R1Reason::DiscoveryBudgetExhausted)),
    };

    if determination.record.rung != Rung::R1 {
        write_rung_record_silently(paths, &determination.record);
    }
    determination
}

fn configured_connection_file() -> Option<PathBuf> {
    let xdg_config_home = std::env::var_os("XDG_CONFIG_HOME");
    let home = std::env::var_os("HOME").or_else(|| std::env::var_os("USERPROFILE"));
    configured_connection_file_from(xdg_config_home.as_deref(), home.as_deref())
}

fn configured_connection_file_from(
    xdg_config_home: Option<&OsStr>,
    home: Option<&OsStr>,
) -> Option<PathBuf> {
    // The shim uses the same user-tier resolver as subc: `$XDG_CONFIG_HOME/cortexkit/aft.jsonc`,
    // then `~/.config/cortexkit/aft.jsonc`. XDG selects only the trusted user's
    // config location; it cannot select a project file or alter the configured
    // connection. An invalid path resolves to `None`; the caller decides whether
    // that means structural passthrough or an unavailable governed route.
    let config_path = crate::subc_config::user_config_path_from(xdg_config_home, home)?;
    let doc = fs::read_to_string(config_path).ok()?;
    connection_file_from_config_doc(&doc).filter(|path| path.is_file())
}

/// Read the raw user-tier `aft.jsonc` document for the shim's config gates.
/// `None` means the config file was absent or unreadable, which the rung
/// determination treats as "no user config" (structural rungs decide).
fn read_user_config_doc() -> Option<String> {
    let xdg_config_home = std::env::var_os("XDG_CONFIG_HOME");
    let home = std::env::var_os("HOME").or_else(|| std::env::var_os("USERPROFILE"));
    let config_path =
        crate::subc_config::user_config_path_from(xdg_config_home.as_deref(), home.as_deref())?;
    fs::read_to_string(config_path).ok()
}

/// Read the `gh_shim.enabled` operator gate from the user config document.
/// `None` means the key is absent or the document is unparseable, in which case
/// the shim stays enabled (default true). Only an explicit `false` disables.
fn gh_shim_enabled_from_config_doc(doc: &str) -> Option<bool> {
    let value: Value = serde_json::from_str(&crate::jsonc::strip_jsonc(doc)).ok()?;
    value.get("gh_shim")?.get("enabled")?.as_bool()
}

fn connection_file_from_config_doc(doc: &str) -> Option<PathBuf> {
    let value: Value = serde_json::from_str(&crate::jsonc::strip_jsonc(doc)).ok()?;
    let raw = value.get("subc")?.get("connection_file")?.as_str()?.trim();
    let path = PathBuf::from(raw);
    (!raw.is_empty() && path.is_absolute()).then_some(path)
}

fn load_rung_record(paths: &StatePaths) -> Option<RungRecord> {
    serde_json::from_slice(&fs::read(&paths.rung).ok()?).ok()
}

fn write_rung_record_silently(paths: &StatePaths, record: &RungRecord) {
    let Ok(bytes) = serde_json::to_vec(record) else {
        return;
    };
    let _ = fs::create_dir_all(&paths.root);
    let temporary = paths.root.join("rung-cache.json.tmp");
    if fs::write(&temporary, bytes).is_ok() {
        let _ = fs::rename(temporary, &paths.rung);
    }
}

#[derive(Debug)]
enum ProbeResult {
    Ready { module_id: String },
    Unreachable,
    NoRoute,
    Unbound,
    TimedOut,
}

fn probe_governance(
    paths: &StatePaths,
    connection_file: &Path,
    cwd: &Path,
    deadline: std::time::Instant,
    agent_id: &str,
) -> ProbeResult {
    let remaining = deadline.saturating_duration_since(std::time::Instant::now());
    if remaining.is_zero() {
        return ProbeResult::TimedOut;
    }
    let connection_file = connection_file.to_path_buf();
    let project_root = project_root_for(cwd);
    let record_paths = paths.clone();
    let agent_id = agent_id.to_string();
    let Ok(runtime) = tokio::runtime::Builder::new_current_thread()
        .enable_io()
        .enable_time()
        .build()
    else {
        return ProbeResult::Unreachable;
    };

    // `tokio::time::timeout` creates its timer immediately. Building that
    // future as a `block_on` argument happens before the runtime enters its
    // context, so the timer's reactor lookup panics in this synchronous CLI.
    // Construct it from inside the entered future instead.
    match runtime.block_on(async move {
        tokio::time::timeout(remaining, async move {
            let options = ConsumerOptions {
                call_timeout: remaining,
                ..ConsumerOptions::default()
            };
            let consumer = SubcConsumer::connect(&connection_file, options)
                .await
                .map_err(|_| ProbeResult::Unreachable)?;
            let catalog = consumer
                .catalog_list()
                .await
                .map_err(|_| ProbeResult::Unreachable)?;
            let holder = route_holder(&catalog.modules);
            record_unexpected_gh_route_advertisers(&record_paths, &holder.unexpected_advertisers);
            let Some(module_id) = holder.module_id else {
                return Err(ProbeResult::NoRoute);
            };
            let identity = BindIdentity {
                project_root: project_root.to_string_lossy().into_owned().into(),
                harness: "aft-gh-shim".to_string(),
                session: gh_session_id(&agent_id),
            };
            let route = consumer
                .open_route(
                    RouteTarget::ManagementSurface {
                        module_id: module_id.clone(),
                    },
                    identity,
                    CallOptions::default(),
                )
                .await
                .map_err(|_| ProbeResult::Unbound)?;
            let _ = consumer
                .close_handle(&route, CloseRouteOptions::default())
                .await;
            Ok(module_id)
        })
        .await
    }) {
        Ok(Ok(module_id)) => ProbeResult::Ready { module_id },
        Ok(Err(result)) => result,
        Err(_) => ProbeResult::TimedOut,
    }
}

#[derive(Debug, Default, Eq, PartialEq)]
struct RouteHolder {
    module_id: Option<String>,
    unexpected_advertisers: Vec<String>,
}

fn route_holder(entries: &[subc_client_rs::CatalogEntry]) -> RouteHolder {
    select_route_holder(entries.iter().filter_map(|entry| {
        entry
            .roles
            .iter()
            .any(|role| {
                matches!(
                    role,
                    ProviderRole::ManagementSurface { operations, .. }
                        if operations.iter().any(|operation| operation.name == ROUTING_OPERATION)
                )
            })
            .then(|| entry.module_id.clone())
    }))
}

fn select_route_holder(advertisers: impl IntoIterator<Item = String>) -> RouteHolder {
    let mut holder = None;
    let mut unexpected_advertisers = BTreeSet::new();
    for advertiser in advertisers {
        // Governed routes carry identity-bearing writes, so only prefrontal-core may
        // hold `gh.route`; another module advertising it must not capture the route.
        // The holder module identifies the routing server, not the bound agent. Using
        // its module ID would merge all agents into one audit and rate-accounting session.
        if advertiser == ROUTING_HOLDER_MODULE_ID {
            holder.get_or_insert(advertiser);
        } else {
            unexpected_advertisers.insert(advertiser);
        }
    }
    RouteHolder {
        module_id: holder,
        unexpected_advertisers: unexpected_advertisers.into_iter().collect(),
    }
}

fn project_root_for(cwd: &Path) -> PathBuf {
    let canonical = cwd.canonicalize().unwrap_or_else(|_| cwd.to_path_buf());
    canonical
        .ancestors()
        .find(|path| path.join(".git").exists())
        .map(Path::to_path_buf)
        .unwrap_or(canonical)
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
struct AgentBinding {
    repo: String,
    agent_id: String,
}

fn resolved_agent_binding(manifest: &Manifest, cwd: &Path) -> Option<AgentBinding> {
    let project_root = project_root_for(cwd);
    let repo = repository_key_from_origin(&project_root)?;
    manifest
        .bindings
        .get(&repo)
        .cloned()
        .map(|agent_id| AgentBinding { repo, agent_id })
}

fn co_author_line(paths: &StatePaths) -> Option<String> {
    let cwd = std::env::current_dir().ok()?;
    let manifest = load_manifest(paths, unix_seconds()).ok()?;
    let binding = resolved_agent_binding(&manifest, &cwd)?;
    let login = binding.agent_id;
    if !valid_github_login(&login) {
        return None;
    }
    let numeric_id =
        cached_numeric_id(paths, &login).or_else(|| resolve_and_cache_numeric_id(paths, &login))?;
    Some(format!(
        "Co-authored-by: {login} <{numeric_id}+{login}@users.noreply.github.com>"
    ))
}

fn valid_github_login(login: &str) -> bool {
    let core = login.strip_suffix("[bot]").unwrap_or(login);
    !core.is_empty()
        && core.len() <= 100
        && !core.starts_with('-')
        && !core.ends_with('-')
        && core
            .bytes()
            .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')
}

fn cached_numeric_ids(paths: &StatePaths) -> BTreeMap<String, u64> {
    fs::read(&paths.numeric_ids)
        .ok()
        .and_then(|bytes| serde_json::from_slice(&bytes).ok())
        .unwrap_or_default()
}

fn cached_numeric_id(paths: &StatePaths, login: &str) -> Option<u64> {
    cached_numeric_ids(paths)
        .get(login)
        .copied()
        .filter(|id| *id > 0)
}

fn resolve_and_cache_numeric_id(paths: &StatePaths, login: &str) -> Option<u64> {
    let image = executing_image();
    let real_gh = resolve_real_gh(&image)?;
    let encoded_login = url::form_urlencoded::byte_serialize(login.as_bytes()).collect::<String>();
    let output = Command::new(real_gh)
        .args(["api", &format!("users/{encoded_login}"), "--jq", ".id"])
        .output()
        .ok()?;
    if !output.status.success() {
        return None;
    }
    let numeric_id = String::from_utf8(output.stdout)
        .ok()?
        .trim()
        .parse::<u64>()
        .ok()
        .filter(|id| *id > 0)?;
    let mut ids = cached_numeric_ids(paths);
    ids.insert(login.to_string(), numeric_id);
    write_numeric_ids_silently(paths, &ids);
    Some(numeric_id)
}

fn write_numeric_ids_silently(paths: &StatePaths, ids: &BTreeMap<String, u64>) {
    let Ok(bytes) = serde_json::to_vec(ids) else {
        return;
    };
    if fs::create_dir_all(&paths.root).is_err() {
        return;
    }
    let temporary = paths.root.join("numeric-ids.json.tmp");
    if fs::write(&temporary, bytes).is_ok() {
        #[cfg(windows)]
        let _ = fs::remove_file(&paths.numeric_ids);
        let _ = fs::rename(temporary, &paths.numeric_ids);
    }
}

fn repository_key_from_origin(project_root: &Path) -> Option<String> {
    // The binding key comes from parsing the local origin remote, not a network
    // lookup, so a signed manifest selects the same agent when offline.
    let remote = origin_remote(project_root)?;
    canonical_repository_key(&remote)
}

fn origin_remote(cwd: &Path) -> Option<String> {
    let output = Command::new("git")
        .current_dir(cwd)
        .args(["remote", "get-url", "origin"])
        .output()
        .ok()?;
    output
        .status
        .success()
        .then(|| String::from_utf8(output.stdout).ok())
        .flatten()
        .map(|remote| remote.trim().to_string())
        .filter(|remote| !remote.is_empty())
}

fn canonical_repository_key(value: &str) -> Option<String> {
    let remote = value.trim().trim_end_matches('/');
    let path = if let Some(path) = [
        "https://github.com/",
        "http://github.com/",
        "ssh://git@github.com/",
        "git://github.com/",
        "git@github.com:",
        "github.com/",
    ]
    .iter()
    .find_map(|prefix| remote.strip_prefix(prefix))
    {
        path
    } else if remote.contains("://") || remote.contains('@') || remote.contains(':') {
        // Repository bindings identify GitHub repositories. A foreign remote is
        // intentionally unmapped rather than treated as an owner/name string.
        return None;
    } else {
        remote
    }
    .trim_end_matches(".git")
    .trim_matches('/');
    let mut parts = path.split('/');
    let owner = parts.next()?.trim();
    let repository = parts.next()?.trim();
    (!owner.is_empty() && !repository.is_empty() && parts.next().is_none()).then(|| {
        format!(
            "{}/{}",
            owner.to_ascii_lowercase(),
            repository.to_ascii_lowercase()
        )
    })
}

fn gh_session_id(agent_id: &str) -> String {
    format!("gh-shim:{agent_id}")
}

#[derive(Clone, Debug, Default, Deserialize, Serialize)]
struct Detectors {
    #[serde(default)]
    wrapper_config_dirs: Vec<String>,
    #[serde(default)]
    credential_env_names: Vec<String>,
}

fn find_ambient_agent_credential(detectors: &Detectors) -> Option<String> {
    for name in &detectors.credential_env_names {
        if std::env::var_os(name).is_some() {
            return Some(format!("env:{name}"));
        }
    }

    let home = std::env::var_os("HOME")
        .or_else(|| std::env::var_os("USERPROFILE"))
        .map(PathBuf::from);
    for raw_pattern in &detectors.wrapper_config_dirs {
        let pattern = expand_home_pattern(raw_pattern, home.as_deref());
        let paths = if pattern.contains(['*', '?', '[', '{']) {
            // Never let an ambient home-directory glob cross a mount: a vanished
            // child ReadDir can panic in Drop after closedir reports ENXIO.
            crate::walk_boundary::expand_glob_same_file_system(&pattern).unwrap_or_default()
        } else {
            vec![PathBuf::from(pattern)]
        };
        for path in paths {
            if path.is_dir() {
                return Some(format!("path:{}", path.display()));
            }
        }
    }

    // `GH_CONFIG_DIR` is only inspected as a metadata path. The basename is
    // compared to the manifest's declared wrapper-dir glob, so the operator's
    // normal gh configuration remains outside this detector inventory.
    let configured = std::env::var_os("GH_CONFIG_DIR").map(PathBuf::from)?;
    if !configured.is_dir() {
        return None;
    }
    let name = configured.file_name()?.to_string_lossy();
    detectors
        .wrapper_config_dirs
        .iter()
        .any(|pattern| {
            Path::new(pattern).file_name().is_some_and(|glob_name| {
                glob::Pattern::new(&glob_name.to_string_lossy()).is_ok_and(|p| p.matches(&name))
            })
        })
        .then(|| format!("path:{}", configured.display()))
}

fn expand_home_pattern(pattern: &str, home: Option<&Path>) -> String {
    pattern
        .strip_prefix("~/")
        .and_then(|suffix| home.map(|home| home.join(suffix).to_string_lossy().into_owned()))
        .unwrap_or_else(|| pattern.to_string())
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(untagged)]
enum TupleDecl {
    Name(String),
    Details {
        tuple: String,
        #[serde(default)]
        platform: Vec<String>,
        #[serde(default)]
        api_match: Option<String>,
        #[serde(default)]
        rationale: Option<String>,
    },
}

impl TupleDecl {
    fn tuple(&self) -> &str {
        match self {
            Self::Name(name) => name,
            Self::Details { tuple, .. } => tuple,
        }
    }

    fn platform(&self) -> &[String] {
        match self {
            Self::Name(_) => &[],
            Self::Details { platform, .. } => platform,
        }
    }

    fn empty_api_match_has_rationale(&self) -> bool {
        match self {
            Self::Details {
                api_match: Some(api_match),
                rationale,
                ..
            } if api_match.is_empty() => rationale
                .as_deref()
                .is_some_and(|text| !text.trim().is_empty()),
            _ => true,
        }
    }
}

#[derive(Clone, Debug, Deserialize, Serialize)]
struct ApiRule {
    method: String,
    path_glob: String,
    tier: Tier,
    #[serde(default)]
    platform: Vec<String>,
    #[serde(default)]
    rationale: Option<String>,
}

#[derive(Clone, Debug, Default, Deserialize, Serialize)]
struct Canonicalization {
    #[serde(default)]
    argv_forms: Vec<String>,
    #[serde(default)]
    target_fields: Vec<String>,
    #[serde(default)]
    body_fields: Vec<String>,
}

#[derive(Clone, Debug, Default, Deserialize, Serialize)]
struct RepositorySection {
    #[serde(default)]
    tiers: BTreeMap<Tier, Vec<TupleDecl>>,
    #[serde(default, alias = "remove")]
    removed_tuples: Vec<String>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
struct Manifest {
    artifact_id: String,
    manifest_version: u64,
    schema_floor: u64,
    /// When the signer issued this manifest. This signed provenance metadata is
    /// displayed in status but does not expire a human-approved sign-once
    /// artifact; only an implausibly future issue time is malformed.
    issued_at_unix_secs: u64,
    #[serde(default)]
    detectors: Detectors,
    #[serde(default)]
    tiers: BTreeMap<Tier, Vec<TupleDecl>>,
    #[serde(default)]
    api_rules: Vec<ApiRule>,
    #[serde(default)]
    canonicalization: BTreeMap<String, Canonicalization>,
    #[serde(default)]
    repository_sections: BTreeMap<String, RepositorySection>,
    #[serde(default)]
    bindings: BTreeMap<String, String>,
}

impl Manifest {
    fn validate(&self) -> Result<(), String> {
        if self.artifact_id != MANIFEST_ARTIFACT_ID {
            return Err(format!("unexpected artifact id {}", self.artifact_id));
        }
        if self.manifest_version == 0 {
            return Err("manifest_version must be positive".to_string());
        }

        let mut declared = BTreeMap::<String, Tier>::new();
        for (tier, entries) in &self.tiers {
            for entry in entries {
                let tuple = normalized_tuple(entry.tuple())?;
                if entry.platform().is_empty() {
                    return Err(format!("tuple {tuple} is missing its platform declaration"));
                }
                if !entry.empty_api_match_has_rationale() {
                    return Err(format!(
                        "tuple {tuple} has an empty api_match without rationale"
                    ));
                }
                if let Some(previous) = declared.insert(tuple.clone(), *tier) {
                    return Err(format!(
                        "tuple {tuple} is declared in both {previous:?} and {tier:?}"
                    ));
                }
            }
        }

        let mut api_declared = BTreeSet::new();
        for rule in &self.api_rules {
            if rule.method.trim().is_empty() || rule.path_glob.trim().is_empty() {
                if rule.path_glob.is_empty()
                    && rule
                        .rationale
                        .as_deref()
                        .is_some_and(|text| !text.trim().is_empty())
                {
                    continue;
                }
                return Err("api rule requires method and non-empty path_glob".to_string());
            }
            if rule.platform.is_empty() {
                return Err(format!(
                    "api rule {} {} is missing its platform declaration",
                    rule.method, rule.path_glob
                ));
            }
            let key = format!("{} {}", rule.method.to_ascii_uppercase(), rule.path_glob);
            if !api_declared.insert(key.clone()) {
                return Err(format!("api rule {key} is declared more than once"));
            }
        }

        let governed = self.tiers.get(&Tier::Governed).cloned().unwrap_or_default();
        for entry in &governed {
            let tuple = normalized_tuple(entry.tuple())?;
            let Some(canonical) = self.canonicalization.get(&tuple) else {
                return Err(format!("governed tuple {tuple} lacks canonicalization"));
            };
            if canonical.argv_forms.is_empty() || canonical.target_fields.is_empty() {
                return Err(format!(
                    "governed tuple {tuple} has incomplete canonicalization"
                ));
            }
        }
        for tuple in self.canonicalization.keys() {
            if declared.get(tuple) != Some(&Tier::Governed) {
                return Err(format!(
                    "canonicalization {tuple} does not name a governed tuple"
                ));
            }
        }

        for (repository, agent_id) in &self.bindings {
            if canonical_repository_key(repository).as_deref() != Some(repository.as_str()) {
                return Err(format!(
                    "binding repository {repository} is not canonical owner/name"
                ));
            }
            if agent_id.trim().is_empty() || agent_id.trim() != agent_id {
                return Err(format!(
                    "binding repository {repository} has an invalid agent id"
                ));
            }
        }

        for (repository, section) in &self.repository_sections {
            for removed in &section.removed_tuples {
                if !declared.contains_key(&normalized_tuple(removed)?) {
                    return Err(format!(
                        "repository section {repository} removes undeclared tuple {removed}"
                    ));
                }
            }
            for (tier, entries) in &section.tiers {
                for entry in entries {
                    let tuple = normalized_tuple(entry.tuple())?;
                    let Some(base) = declared.get(&tuple) else {
                        return Err(format!(
                            "repository section {repository} adds tuple {tuple}"
                        ));
                    };
                    if tier.rank() < base.rank() {
                        return Err(format!(
                            "repository section {repository} lowers tuple {tuple}"
                        ));
                    }
                }
            }
        }
        Ok(())
    }

    fn tier_for_tuple(&self, tuple: &str, platform: &str) -> Option<Tier> {
        self.tiers.iter().find_map(|(tier, entries)| {
            entries
                .iter()
                .any(|entry| {
                    normalized_tuple(entry.tuple()).ok().as_deref() == Some(tuple)
                        && platform_matches(entry.platform(), platform)
                })
                .then_some(*tier)
        })
    }
}

fn normalized_tuple(value: &str) -> Result<String, String> {
    let words = value
        .split_whitespace()
        .map(|word| word.to_ascii_lowercase())
        .collect::<Vec<_>>();
    (!words.is_empty())
        .then(|| words.join(" "))
        .ok_or_else(|| "tuple cannot be empty".to_string())
}

fn platform_matches(platforms: &[String], current: &str) -> bool {
    platforms
        .iter()
        .any(|platform| platform.eq_ignore_ascii_case(current))
}

/// Envelope v2: the manifest body travels as the EXACT bytes the signer
/// published. `manifest_bytes` is an opaque string holding that file's
/// contents verbatim; the verifier checks the signature over those bytes
/// BEFORE parsing them, so the signature contract is "the signer signed the
/// file it publishes" and no canonicalization rule exists on this side.
#[derive(Clone, Debug, Deserialize, Serialize)]
struct SignedManifest {
    artifact_id: String,
    envelope_version: u64,
    key_id: String,
    /// Advisory local metadata only: when this machine stored the artifact.
    /// It is not a validity input and re-stamping it cannot alter the signed
    /// manifest provenance.
    fetched_at_unix_secs: u64,
    signature: String,
    manifest_bytes: String,
}

#[derive(Clone, Debug)]
struct VerifiedManifest {
    manifest: Manifest,
    verified_by_key_id: String,
}

#[derive(Clone, Debug)]
enum ManifestProblem {
    Missing,
    Invalid(String),
    BelowFloor {
        manifest_floor: u64,
    },
    /// The manifest is validly signed but its version is below the newest
    /// version ever accepted on this machine: a rollback incident, never an
    /// ordinary out-of-order arrival.
    RolledBack {
        manifest_version: u64,
        newest_accepted: u64,
    },
}

impl ManifestProblem {
    fn diagnostic(&self) -> SelfReportDiagnostic {
        match self {
            Self::Missing => SelfReportDiagnostic::ManifestUnavailable,
            Self::Invalid(_) => SelfReportDiagnostic::ManifestInvalid,
            Self::BelowFloor { .. } => SelfReportDiagnostic::ManifestBelowFloor,
            Self::RolledBack { .. } => SelfReportDiagnostic::ManifestRollback,
        }
    }

    fn status_label(&self) -> String {
        match self {
            Self::Missing => "unavailable".to_string(),
            Self::Invalid(error) => format!("invalid ({error})"),
            Self::BelowFloor { manifest_floor } => format!(
                "{} (manifest floor {manifest_floor}, shim floor {SCHEMA_FLOOR})",
                RefusalCode::ManifestBelowFloor.as_str()
            ),
            Self::RolledBack {
                manifest_version,
                newest_accepted,
            } => format!(
                "{} (manifest version {manifest_version}, newest accepted version {newest_accepted})",
                SelfReportDiagnostic::ManifestRollback.as_str()
            ),
        }
    }

    fn fallback_notice_reason(&self) -> String {
        match self {
            Self::Missing => "manifest unavailable".to_string(),
            Self::Invalid(reason) => reason.clone(),
            Self::BelowFloor { manifest_floor } => {
                format!("manifest floor {manifest_floor}, shim floor {SCHEMA_FLOOR}")
            }
            Self::RolledBack {
                manifest_version,
                newest_accepted,
            } => {
                format!("manifest version {manifest_version}, newest accepted version {newest_accepted}")
            }
        }
    }

    fn untrusted_manifest_key_steering(&self) -> Option<&'static str> {
        match self {
            Self::Invalid(reason) if reason.starts_with("untrusted manifest key id ") => {
                Some(UNTRUSTED_MANIFEST_KEY_STEERING)
            }
            Self::Missing
            | Self::Invalid(_)
            | Self::BelowFloor { .. }
            | Self::RolledBack { .. } => None,
        }
    }
}

/// Verifier-site contract for the signed routing manifest.
///
/// RAW DISTRIBUTED BYTES. The manifest body travels inside the envelope as the
/// exact bytes the signer published (`manifest_bytes`). This function verifies
/// the received bytes FIRST and parses them into a `Manifest` SECOND. The
/// signer signs the file it publishes; no canonicalization rule exists on this
/// side, so no field reorder, re-indent, or re-encode can break (or silently
/// reshape) the signature contract across languages. Verifying a parsed and
/// re-serialized struct instead would make every serializer a party to the
/// signature.
///
/// VERSION-MONOTONIC VALIDITY. Manifest approval is a human ceremony performed
/// once per signature, not a periodic lease. Expiring a sign-once artifact
/// converts approval cadence into a scheduled outage. A verified manifest stays
/// valid regardless of age; the local version high-water mark refuses a
/// validly-signed version below the newest accepted version, which is the honest
/// replay defense. `issued_at_unix_secs` remains signed provenance metadata and
/// rejects only an implausibly future timestamp.
///
/// TWO-SIDED BOUND (the custody bar stays at config integrity). The governed
/// EXECUTION vocabulary is compiled into the route holder (vendored
/// classification); no manifest can widen what the holder executes. The
/// manifest governs shim-side routing selection only. Manifest tampering is
/// therefore bounded above by the holder's vendored set and below by the
/// shim's refusal arms. If classification ever moves INTO the manifest, the
/// trust root flips from integrity to authority and the custody design must be
/// revisited first.
///
/// Delta property, phrased for the manifest approver: NARROWING a manifest
/// WIDENS the key-compromise surface. The delta is the compiled vocabulary
/// minus what this manifest routes; every operation a manifest stops routing
/// joins the set a compromised signing key could re-enable. The approval
/// question for a narrowing change is "am I content that a key compromise
/// re-enables exactly the operations this manifest stops routing", not "does
/// this look tighter". The holder-side vendored vocabulary guards the widening
/// direction; this line guards the narrowing one.
///
/// DORMANCY VALVE AND LOCAL STATE. With no manifest artifact on disk the shim
/// is dormant and passes invocations through (R2, reason
/// `manifest_unavailable`). That valve's weakness — a local downgrade to
/// dormant by deleting the artifact — is only reachable by an adversary with
/// local write access, who can equally patch the compiled-in trust set or this
/// verifier itself; the weakness is only reachable by an adversary the design
/// already cannot survive. The same argument covers the local state this
/// verifier maintains: the last-valid manifest cache and the monotonic version
/// high-water mark are enforcement conveniences, not a security boundary, and
/// an adversary who can delete, forge, or lower them can patch the verifier.
///
/// TOKEN LANGUAGE. The holder executes governed calls under full-installation
/// GitHub App tokens held in custody; operation gating is holder-side
/// classification over the routed request. The shim never holds any token in
/// either direction.
fn load_manifest(paths: &StatePaths, now: u64) -> Result<Manifest, ManifestProblem> {
    load_manifest_with_trust_set(paths, now, compiled_manifest_trust_set())
        .map(|verified| verified.manifest)
}

fn load_manifest_with_trust_set(
    paths: &StatePaths,
    now: u64,
    trust_set: &[Option<ManifestTrustKey>],
) -> Result<VerifiedManifest, ManifestProblem> {
    let bytes = fs::read(&paths.manifest).map_err(|_| ManifestProblem::Missing)?;
    let envelope: SignedManifest = serde_json::from_slice(&bytes)
        .map_err(|error| ManifestProblem::Invalid(error.to_string()))?;
    if envelope.artifact_id != MANIFEST_ARTIFACT_ID {
        return Err(ManifestProblem::Invalid("artifact id mismatch".to_string()));
    }
    if envelope.envelope_version != ENVELOPE_VERSION {
        return Err(ManifestProblem::Invalid(format!(
            "unsupported envelope version {} (this shim verifies envelope version {ENVELOPE_VERSION})",
            envelope.envelope_version
        )));
    }
    // Verify the received bytes FIRST, parse SECOND (contract above).
    let VerifiedManifest {
        manifest,
        verified_by_key_id,
    } = verify_manifest_signature_with_provenance(&envelope, trust_set)?;
    manifest.validate().map_err(ManifestProblem::Invalid)?;
    if manifest.schema_floor < SCHEMA_FLOOR {
        return Err(ManifestProblem::BelowFloor {
            manifest_floor: manifest.schema_floor,
        });
    }
    // Monotonic version high-water mark: a manifest older than the newest ever
    // accepted here is refused as a rollback incident. Version, not artifact age,
    // prevents replay of a past manifest that may carry a wider vocabulary.
    let newest_accepted = version_high_water(paths);
    if manifest.manifest_version < newest_accepted {
        return Err(ManifestProblem::RolledBack {
            manifest_version: manifest.manifest_version,
            newest_accepted,
        });
    }
    if manifest.issued_at_unix_secs > now + ISSUED_AT_FUTURE_SKEW.as_secs() {
        return Err(ManifestProblem::Invalid(format!(
            "issued_at_unix_secs {} is more than {} seconds in the future",
            manifest.issued_at_unix_secs,
            ISSUED_AT_FUTURE_SKEW.as_secs()
        )));
    }
    // Accepted: advance the high-water mark and refresh the last-valid cache
    // that the regressed-manifest arm classifies from. Both are local state
    // under the dormancy-valve argument documented above.
    if manifest.manifest_version > newest_accepted {
        write_version_high_water(paths, manifest.manifest_version);
    }
    write_last_valid_manifest(paths, &manifest);
    Ok(VerifiedManifest {
        manifest,
        verified_by_key_id,
    })
}

/// Verify the signature over the envelope's exact manifest bytes, then parse.
/// No manifest content is interpreted before its bytes verify.
#[cfg(test)]
fn verify_manifest_signature(envelope: &SignedManifest) -> Result<Manifest, ManifestProblem> {
    verify_manifest_signature_with(envelope, compiled_manifest_trust_set())
}

#[cfg(test)]
fn verify_manifest_signature_with(
    envelope: &SignedManifest,
    trust_set: &[Option<ManifestTrustKey>],
) -> Result<Manifest, ManifestProblem> {
    verify_manifest_signature_with_provenance(envelope, trust_set).map(|verified| verified.manifest)
}

fn verify_manifest_signature_with_provenance(
    envelope: &SignedManifest,
    trust_set: &[Option<ManifestTrustKey>],
) -> Result<VerifiedManifest, ManifestProblem> {
    let Some(key) = trust_set
        .iter()
        .flatten()
        .find(|slot| slot.key_id == envelope.key_id)
        .copied()
    else {
        return Err(ManifestProblem::Invalid(format!(
            "untrusted manifest key id {}",
            envelope.key_id
        )));
    };
    let signature = base64::engine::general_purpose::STANDARD
        .decode(&envelope.signature)
        .map_err(|_| ManifestProblem::Invalid("invalid detached signature encoding".to_string()))?;
    UnparsedPublicKey::new(&ED25519, key.public_key)
        .verify(envelope.manifest_bytes.as_bytes(), &signature)
        .map_err(|_| {
            ManifestProblem::Invalid("detached signature verification failed".to_string())
        })?;
    let manifest = serde_json::from_str(&envelope.manifest_bytes).map_err(|error| {
        ManifestProblem::Invalid(format!("signed manifest bytes failed to parse: {error}"))
    })?;
    Ok(VerifiedManifest {
        manifest,
        verified_by_key_id: key.key_id.to_string(),
    })
}

/// One trusted manifest signing key: a stable key id plus the Ed25519 public
/// key bytes that id binds.
#[derive(Clone, Copy)]
struct ManifestTrustKey {
    key_id: &'static str,
    public_key: &'static [u8],
}

// A manifest signature is the barrier preventing an agent from editing its own
// cache to turn a governed verb into a mechanical one. The development key is
// deliberately compiled only in debug builds so fixtures can exercise R3. A
// release build has no trust root until the separately reviewed CKCRED custody
// release supplies one, which keeps release binaries at R2 rather than making a
// governance claim with a test key.
//
// TWO-KEY TRUST SET. The release trust array ships with TWO key slots from day
// one: the live signing key and a cold standby with a distinct key id. The dev
// set keeps its single test key.
//
// The production signing keys are minted and held by the key-custody process
// outside this repository; a separately reviewed release copies each approved
// public key into these slots (the private half never approaches the build).
// Until that happens both slots stay empty and release binaries remain at R2.
//
// Two-release rotation procedure once the slots are filled:
//   1. Ship a release whose standby slot carries the standby key. Both slots
//      verify; the live key still signs. The standby key comes from custody,
//      never from a self-generated filler.
//   2. Promote the standby by shipping a manifest signed by it. The trust set
//      already accepts it, so promotion does not depend on updating binaries
//      first.
//   3. One release later, remove the old key. Between promotion and removal a
//      compromise of the old key can still sign, so the removal release is
//      part of the rotation rather than optional cleanup.
//
// Slot layout: index 0 is the LIVE signing key, index 1 is the COLD STANDBY.
// The first manifest signature verifying under a newly installed live key is
// the stored-key-equals-published-half acceptance test for that installation.
//
// LIVE KEY PROVENANCE (2026-08-27 CKCRED ceremony): `signing:gh-manifest-root:1`,
// minted in-vault via `ck auth mint-signing-key` (private half never exported;
// extraction-refusal proven by a live `credential.get` attack against a bearer
// handle, then the handle revoked). Public half read back over the route plane
// and independently verified in Node's stdlib Ed25519 with a tamper control.
const PROD_MANIFEST_KEY_ID: &str = "c9ad111282d1da10";
const PROD_MANIFEST_PUBLIC_KEY: [u8; 32] = [
    0x5f, 0x4c, 0x81, 0x90, 0x18, 0xe2, 0xb6, 0x8d, 0x18, 0xdb, 0xce, 0x6a, 0xc3, 0x6f, 0x9b, 0x84,
    0x65, 0x28, 0x84, 0x14, 0x75, 0x55, 0xe8, 0x44, 0x2e, 0xf7, 0x6d, 0x7f, 0xb4, 0x7a, 0x42, 0xf4,
];
const PROD_MANIFEST_TRUST_KEY: ManifestTrustKey = ManifestTrustKey {
    key_id: PROD_MANIFEST_KEY_ID,
    public_key: &PROD_MANIFEST_PUBLIC_KEY,
};

#[cfg(not(debug_assertions))]
const RELEASE_MANIFEST_TRUST_SET: &[Option<ManifestTrustKey>; 2] = &[
    Some(PROD_MANIFEST_TRUST_KEY), // live
    None,                          // cold standby (filled by a future custody release)
];

#[cfg(debug_assertions)]
const DEV_MANIFEST_KEY_ID: &str = "gh-routing-dev-test-key-v1";
#[cfg(debug_assertions)]
const DEV_MANIFEST_PUBLIC_KEY: [u8; 32] = [
    0xd7, 0x5a, 0x98, 0x01, 0x82, 0xb1, 0x0a, 0xb7, 0xd5, 0x4b, 0xfe, 0xd3, 0xc9, 0x64, 0x07, 0x3a,
    0x0e, 0xe1, 0x72, 0xf3, 0xda, 0xa6, 0x23, 0x25, 0xaf, 0x02, 0x1a, 0x68, 0xf7, 0x07, 0x51, 0x1a,
];
// Debug images verify BOTH eras: the production root (so fleet manifests work
// on dev builds) and the dev test key (so fixtures can exercise R3 without a
// custody round-trip). The envelope's key_id selects at verify time.
#[cfg(debug_assertions)]
const DEV_MANIFEST_TRUST_SET: &[Option<ManifestTrustKey>; 2] = &[
    Some(PROD_MANIFEST_TRUST_KEY),
    Some(ManifestTrustKey {
        key_id: DEV_MANIFEST_KEY_ID,
        public_key: &DEV_MANIFEST_PUBLIC_KEY,
    }),
];

fn compiled_manifest_trust_set() -> &'static [Option<ManifestTrustKey>] {
    #[cfg(debug_assertions)]
    {
        DEV_MANIFEST_TRUST_SET
    }
    #[cfg(not(debug_assertions))]
    {
        RELEASE_MANIFEST_TRUST_SET
    }
}

fn trust_set_key_ids(trust_set: &[Option<ManifestTrustKey>]) -> Vec<&'static str> {
    trust_set.iter().flatten().map(|key| key.key_id).collect()
}

/// Outcome of resolving the installed manifest artifact for an invocation.
///
/// The state is keyed on what is INSTALLED on disk, never on memory of past
/// validation: every call re-reads the artifact and re-derives its
/// disposition from the artifact plus the local last-valid cache.
#[derive(Debug)]
enum ManifestResolution {
    /// The installed artifact verified: normal classification.
    Active(Manifest),
    /// The installed artifact failed validation after a prior valid manifest:
    /// governed/admin tuples the cache classifies are refused, while mechanical
    /// operations pass through.
    Regressed {
        manifest: Manifest,
        problem: ManifestProblem,
    },
    /// An artifact is installed but failed validation before this machine ever
    /// accepted one, so the invocation passes through with an identity notice.
    Invalid(ManifestProblem),
    /// No manifest artifact is present, so delegate without manifest-based routing.
    Dormant,
}

impl ManifestResolution {
    fn manifest(&self) -> Option<&Manifest> {
        match self {
            Self::Active(manifest) | Self::Regressed { manifest, .. } => Some(manifest),
            Self::Invalid(_) | Self::Dormant => None,
        }
    }

    fn into_manifest(self) -> Option<Manifest> {
        match self {
            Self::Active(manifest) | Self::Regressed { manifest, .. } => Some(manifest),
            Self::Invalid(_) | Self::Dormant => None,
        }
    }

    fn invalid_problem(&self) -> Option<&ManifestProblem> {
        match self {
            Self::Regressed { problem, .. } | Self::Invalid(problem) => Some(problem),
            Self::Active(_) | Self::Dormant => None,
        }
    }
}

fn resolve_manifest(paths: &StatePaths, now: u64) -> ManifestResolution {
    match load_manifest(paths, now) {
        Ok(manifest) => ManifestResolution::Active(manifest),
        Err(ManifestProblem::Missing) => ManifestResolution::Dormant,
        Err(problem) => match read_last_valid_manifest(paths) {
            Some(cache) => ManifestResolution::Regressed {
                manifest: cache.manifest,
                problem,
            },
            None => ManifestResolution::Invalid(problem),
        },
    }
}

fn delegate_after_invalid_manifest_notice(args: &[OsString], problem: &ManifestProblem) -> i32 {
    // Missing manifests identify public installations and must remain silent.
    // An installed but invalid manifest instead signals a misconfigured
    // governed seat, so say which ambient identity will execute the fallback.
    eprintln!(
        "gh-shim: manifest invalid ({}); executing with ambient gh credentials",
        problem.fallback_notice_reason().replace(['\n', '\r'], " ")
    );
    delegate(args)
}

/// Disposition of one invocation under the regressed-manifest arm.
///
/// Governed and admin tuples, as classified by the last-valid manifest, fail
/// closed with a stable refusal; mechanical operations pass through
/// byte-transparently. The operator bypass does not apply here: a broken
/// manifest means the classification itself is untrusted, so no bypass can
/// promote it.
fn regressed_disposition(
    args: &[OsString],
    manifest: &Manifest,
    platform: &str,
    problem: &ManifestProblem,
) -> RegressedDisposition {
    match classify(args, manifest, platform) {
        Classification::Mechanical => RegressedDisposition::Passthrough,
        Classification::Governed { tuple, .. } | Classification::Admin { tuple } => {
            let text = match problem.untrusted_manifest_key_steering() {
                Some(steering) => {
                    format!("the manifest artifact fails validation; {tuple} is refused; {steering}")
                }
                None => format!(
                    "the manifest artifact fails validation; {tuple} is refused until the manifest is repaired"
                ),
            };
            RegressedDisposition::Refuse {
                code: RefusalCode::ManifestRegressed,
                text,
            }
        }
        Classification::Unclassified => RegressedDisposition::Refuse {
            code: RefusalCode::Unclassified,
            text:
                "no manifest declaration for this invocation (manifest artifact fails validation)"
                    .to_string(),
        },
    }
}

#[derive(Debug)]
enum RegressedDisposition {
    Passthrough,
    Refuse { code: RefusalCode, text: String },
}

/// Last manifest that fully verified on this machine. Local state under the
/// dormancy-valve argument at the verifier site: it lets the regressed-manifest
/// arm keep classifying while a broken artifact is repaired, and it is not a
/// security boundary.
#[derive(Clone, Debug, Deserialize, Serialize)]
struct LastValidManifest {
    manifest: Manifest,
}

fn read_last_valid_manifest(paths: &StatePaths) -> Option<LastValidManifest> {
    serde_json::from_slice(&fs::read(&paths.last_valid_manifest).ok()?).ok()
}

fn write_last_valid_manifest(paths: &StatePaths, manifest: &Manifest) {
    let record = LastValidManifest {
        manifest: manifest.clone(),
    };
    let Ok(bytes) = serde_json::to_vec(&record) else {
        return;
    };
    let _ = fs::create_dir_all(&paths.root);
    let temporary = paths.last_valid_manifest.with_extension("tmp");
    if fs::write(&temporary, bytes).is_ok() {
        let _ = fs::rename(temporary, &paths.last_valid_manifest);
    }
}

/// Monotonic high-water mark: the newest `manifest_version` ever accepted on
/// this machine. A manifest below it is refused as a rollback incident. Local
/// state under the same dormancy-valve argument as the last-valid cache: an
/// adversary who can lower it can patch this verifier.
#[derive(Clone, Debug, Deserialize, Serialize)]
struct VersionHighWater {
    newest_accepted_version: u64,
}

fn version_high_water(paths: &StatePaths) -> u64 {
    fs::read(&paths.version_high_water)
        .ok()
        .and_then(|bytes| serde_json::from_slice::<VersionHighWater>(&bytes).ok())
        .map(|record| record.newest_accepted_version)
        .unwrap_or(0)
}

fn write_version_high_water(paths: &StatePaths, newest_accepted_version: u64) {
    let Ok(bytes) = serde_json::to_vec(&VersionHighWater {
        newest_accepted_version,
    }) else {
        return;
    };
    let _ = fs::create_dir_all(&paths.root);
    let temporary = paths.version_high_water.with_extension("tmp");
    if fs::write(&temporary, bytes).is_ok() {
        let _ = fs::rename(temporary, &paths.version_high_water);
    }
}

#[derive(Debug)]
enum Classification {
    Mechanical,
    Governed {
        tuple: String,
        canonical: Canonicalization,
    },
    Admin {
        tuple: String,
    },
    Unclassified,
}

fn is_reviewed_admin_tuple(manifest_version: u64, tuple: &str) -> bool {
    V1_ADMIN_TUPLES.contains(&tuple)
        || (manifest_version >= 9 && V9_ADMIN_TUPLES.contains(&tuple))
        || (manifest_version >= 10 && V10_ADMIN_TUPLES.contains(&tuple))
}

fn is_reviewed_edit_last_tuple(manifest_version: u64, tuple: &str) -> bool {
    manifest_version >= 10 && V10_EDIT_LAST_TUPLES.contains(&tuple)
}

fn has_exact_flag(args: &[OsString], flag: &str) -> bool {
    args.iter().any(|arg| arg.to_str() == Some(flag))
}

fn classify(args: &[OsString], manifest: &Manifest, platform: &str) -> Classification {
    let Some((verb, subcommand, _)) = command_head(args) else {
        // Keep malformed argument vectors fail-closed; a valid no-subcommand
        // vector is the mechanical case described below.
        if args.iter().any(|arg| arg.to_str().is_none()) {
            return Classification::Unclassified;
        }
        // When no subcommand is provided, the real `gh` can only show top-level
        // help or version information; it cannot make GitHub requests or change
        // the active user or account. Therefore this invocation is mechanical.
        return Classification::Mechanical;
    };
    if verb == "help" {
        // `gh help <command>` only renders upstream CLI help and has no GitHub-side effects.
        return Classification::Mechanical;
    }
    if verb == "api" {
        return classify_api(args, manifest, platform);
    }
    let tuple = match subcommand {
        Some(subcommand) => format!("{verb} {subcommand}"),
        None => verb,
    };
    // `--edit-last` is the native gh operation that edits the authenticated
    // user's own last comment. Keep this exact author-scoped form limited to
    // the explicitly allowed comment tuples. An id-addressed API PATCH remains
    // unclassified because it can edit a comment selected by ID rather than the
    // authenticated user's own last comment.
    if has_exact_flag(args, "--edit-last")
        && !is_reviewed_edit_last_tuple(manifest.manifest_version, &tuple)
    {
        return Classification::Unclassified;
    }
    // Deletion and create-if-none perform different mutations from editing the
    // authenticated user's last comment, so they must not inherit the narrowly
    // scoped --edit-last allowance.
    if has_exact_flag(args, "--delete-last") || has_exact_flag(args, "--create-if-none") {
        return Classification::Unclassified;
    }
    if READ_ONLY_ACTION_TUPLES.contains(&tuple.as_str()) {
        return Classification::Mechanical;
    }
    match manifest.tier_for_tuple(&tuple, platform) {
        Some(Tier::Mechanical) => Classification::Mechanical,
        Some(Tier::Admin) if is_reviewed_admin_tuple(manifest.manifest_version, &tuple) => {
            Classification::Admin { tuple }
        }
        Some(Tier::Governed) if V1_GOVERNED_TUPLES.contains(&tuple.as_str()) => manifest
            .canonicalization
            .get(&tuple)
            .cloned()
            .map(|canonical| Classification::Governed { tuple, canonical })
            .unwrap_or(Classification::Unclassified),
        // Only tuples named by the manifest and a generation-specific classifier
        // allowlist can be governed or admin. Command names alone do not opt an
        // entry in; a new write shape needs both a manifest declaration and a
        // matching classifier allowlist entry.
        Some(Tier::Governed | Tier::Admin) | None => Classification::Unclassified,
    }
}

fn command_head(args: &[OsString]) -> Option<(String, Option<String>, usize)> {
    let mut positionals = Vec::new();
    let mut skip_next = false;
    for (index, raw) in args.iter().enumerate() {
        let value = raw.to_str()?;
        if skip_next {
            skip_next = false;
            continue;
        }
        if matches!(value, "--repo" | "-R" | "--hostname" | "--config-dir") {
            skip_next = true;
            continue;
        }
        if value.starts_with('-') {
            continue;
        }
        positionals.push((value.to_ascii_lowercase(), index));
        if positionals.len() == 2 || positionals[0].0 == "api" {
            break;
        }
    }
    let (verb, index) = positionals.first()?.clone();
    let subcommand = positionals.get(1).map(|(value, _)| value.clone());
    Some((verb, subcommand, index))
}

fn classify_api(args: &[OsString], manifest: &Manifest, platform: &str) -> Classification {
    let Some((method, path)) = api_method_and_path(args) else {
        return Classification::Unclassified;
    };
    let matches = manifest
        .api_rules
        .iter()
        .filter(|rule| {
            rule.method.eq_ignore_ascii_case(&method)
                && platform_matches(&rule.platform, platform)
                && glob::Pattern::new(&rule.path_glob).is_ok_and(|pattern| pattern.matches(&path))
        })
        .collect::<Vec<_>>();
    if matches.is_empty() && method.eq_ignore_ascii_case("GET") {
        // A field-free GET cannot write or assert an identity, so it remains a
        // mechanical read even when the manifest has no endpoint-specific rule.
        return Classification::Mechanical;
    }
    if matches.len() != 1 {
        return Classification::Unclassified;
    }
    // API writes are not normalized into governed or ADMIN equivalents until a
    // parser accepts and validates their exact argv forms. An id-addressed
    // comment PATCH can target any contributor's comment, unlike native
    // `--edit-last`, which is scoped to the caller.
    match matches[0].tier {
        Tier::Mechanical => Classification::Mechanical,
        Tier::Governed | Tier::Admin => Classification::Unclassified,
    }
}

fn api_method_and_path(args: &[OsString]) -> Option<(String, String)> {
    let mut method = "GET".to_string();
    let mut path = None;
    let mut index = 1;
    while index < args.len() {
        let value = args[index].to_str()?;
        if matches!(value, "--method" | "-X") {
            method = args.get(index + 1)?.to_str()?.to_ascii_uppercase();
            index += 2;
            continue;
        }
        if let Some(method_value) = value.strip_prefix("--method=") {
            method = method_value.to_ascii_uppercase();
            index += 1;
            continue;
        }
        if is_api_field_argument(value) {
            // Field-bearing API forms can change request semantics independently
            // of the endpoint. Until an audited form has a reviewed parser, they
            // remain unclassified rather than inheriting a read-like API rule.
            return None;
        }
        if value.starts_with('-') {
            index += 1;
            continue;
        }
        if path.is_none() {
            path = Some(value.to_string());
        }
        index += 1;
    }
    let path = path?;
    (path != "-").then_some((method, path))
}

fn is_api_field_argument(value: &str) -> bool {
    ["--input", "--raw-field", "--field"]
        .iter()
        .any(|flag| value == *flag || value.starts_with(&format!("{flag}=")))
        || value == "-F"
        || value.starts_with("-F")
        || value == "-f"
        || value.starts_with("-f")
}

#[derive(Clone, Debug)]
struct GovernedRequest {
    action: String,
    target: Map<String, Value>,
    body: Map<String, Value>,
    repository: Option<String>,
    manifest_version: u64,
    edit_last: bool,
}

fn canonicalize_governed(
    args: &[OsString],
    tuple: &str,
    canonical: &Canonicalization,
    manifest_version: u64,
) -> Result<GovernedRequest, String> {
    let (_, _, head_index) =
        command_head(args).ok_or_else(|| "missing command head".to_string())?;
    let subcommand_index = if tuple.starts_with("api ") {
        head_index
    } else {
        head_index + 1
    };
    let mut positional = Vec::new();
    let mut body = Map::new();
    let mut review_event = None;
    let mut explicit_repository = None;
    let mut edit_last = false;
    let mut index = subcommand_index + 1;
    while index < args.len() {
        let value = args[index]
            .to_str()
            .ok_or_else(|| "non-UTF-8 governed arguments are undeclared".to_string())?;
        if tuple == "pr review" {
            if let Some(event) = declared_review_event(value) {
                if review_event.replace(event.to_string()).is_some() {
                    return Err(
                        "pr review accepts only one of --approve, --comment, or --request-changes"
                            .to_string(),
                    );
                }
                index += 1;
                continue;
            }
        }
        if value == "--edit-last" {
            if !is_reviewed_edit_last_tuple(manifest_version, tuple) {
                return Err("undeclared flag --edit-last".to_string());
            }
            if edit_last {
                return Err("--edit-last may be provided only once".to_string());
            }
            edit_last = true;
        } else if value == "--repo" || value == "-R" {
            index += 1;
            let repository = args
                .get(index)
                .and_then(|arg| arg.to_str())
                .ok_or_else(|| "--repo requires a value".to_string())?;
            explicit_repository = Some(repository.to_string());
        } else if let Some(repository) = value.strip_prefix("--repo=") {
            explicit_repository = Some(repository.to_string());
        } else if let Some((field, supplied)) =
            declared_body_value(value, canonical, args.get(index + 1))?
        {
            body.insert(field, Value::String(supplied));
            if !value.contains('=') && !value.starts_with('-') {
                // Kept for completeness; declared_body_value only returns flags.
                positional.push(value.to_string());
            }
            if !value.contains('=') {
                index += 1;
            }
        } else if value.starts_with('-') {
            return Err(format!("undeclared flag {value}"));
        } else {
            positional.push(value.to_string());
        }
        index += 1;
    }

    if positional.len() != canonical.target_fields.len() {
        return Err("target positional form is undeclared".to_string());
    }
    if canonical
        .body_fields
        .iter()
        .any(|field| !body.contains_key(field))
    {
        // An explicit approve/request-changes review is valid without prose;
        // comments still need a body because upstream gh would otherwise open
        // an interactive prompt that the governed seam cannot reproduce.
        let body_optional_for_review = tuple == "pr review"
            && review_event
                .as_deref()
                .is_some_and(|event| event != "COMMENT")
            && canonical.body_fields.iter().all(|field| field == "body");
        if !body_optional_for_review {
            return Err("required declared body field is absent".to_string());
        }
    }
    if let Some(event) = review_event {
        body.insert("event".to_string(), Value::String(event));
    }
    let target = canonical
        .target_fields
        .iter()
        .cloned()
        .zip(positional)
        .map(|(field, value)| (field, Value::String(value)))
        .collect::<Map<_, _>>();
    // A global `--repo` may precede the command head, so inspect the original
    // argv before falling back to a command-local flag or remote inference.
    let repository = explicit_repo(args)
        .or(explicit_repository)
        .or_else(infer_repository_from_git)
        .map(|repository| {
            canonical_repository_key(&repository)
                .ok_or_else(|| format!("repository {repository} is not owner/name"))
        })
        .transpose()?;
    Ok(GovernedRequest {
        action: tuple.to_string(),
        target,
        body,
        repository,
        manifest_version,
        edit_last,
    })
}

fn declared_body_value(
    value: &str,
    canonical: &Canonicalization,
    next: Option<&OsString>,
) -> Result<Option<(String, String)>, String> {
    for field in &canonical.body_fields {
        let long = format!("--{field}");
        let short = match field.as_str() {
            "body" => Some("-b"),
            "reaction" => Some("-r"),
            _ => None,
        };
        if value == long || short == Some(value) {
            let supplied = next
                .and_then(|arg| arg.to_str())
                .ok_or_else(|| format!("{value} requires a value"))?;
            return Ok(Some((field.clone(), supplied.to_string())));
        }
        if let Some(supplied) = value.strip_prefix(&(long + "=")) {
            return Ok(Some((field.clone(), supplied.to_string())));
        }

        // GitHub CLI supports --body-file/-F for commands that submit text
        // bodies. Read the file here so this shim keeps the request on its
        // governed path and avoids shell-quoting problems with long Markdown
        // passed as an inline argument.
        if field == "body" {
            let file = if value == "--body-file" || value == "-F" {
                Some(
                    next.and_then(|arg| arg.to_str())
                        .ok_or_else(|| format!("{value} requires a value"))?,
                )
            } else {
                value
                    .strip_prefix("--body-file=")
                    .or_else(|| value.strip_prefix("-F="))
                    .or_else(|| value.strip_prefix("-F"))
            };
            if let Some(file) = file {
                let supplied =
                    read_body_file(Path::new(file)).map_err(|error| format!("{value}: {error}"))?;
                return Ok(Some((field.clone(), supplied)));
            }
        }
    }
    Ok(None)
}

fn read_body_file(path: &Path) -> Result<String, String> {
    let mut stdin = io::stdin().lock();
    read_body_file_from(path, &mut stdin)
}

fn read_body_file_from<R: Read>(path: &Path, stdin: &mut R) -> Result<String, String> {
    let mut body = String::new();
    // When the path is '-', upstream gh reads the body from standard input.
    // Do the same here so callers can provide stdin through this shim instead
    // of bypassing its governed path.
    if path == Path::new("-") {
        stdin
            .read_to_string(&mut body)
            .map_err(|error| format!("could not read body from stdin: {error}"))?;
    } else {
        body = fs::read_to_string(path)
            .map_err(|error| format!("could not read body file {}: {error}", path.display()))?;
    }
    Ok(body)
}

fn declared_review_event(value: &str) -> Option<&'static str> {
    match value {
        "--approve" => Some("APPROVE"),
        "--comment" => Some("COMMENT"),
        "--request-changes" => Some("REQUEST_CHANGES"),
        _ => None,
    }
}

fn explicit_repo(args: &[OsString]) -> Option<String> {
    let mut args = args.iter();
    while let Some(arg) = args.next() {
        let value = arg.to_str()?;
        if value == "--repo" || value == "-R" {
            return args.next()?.to_str().map(str::to_string);
        }
        if let Some(repository) = value.strip_prefix("--repo=") {
            return Some(repository.to_string());
        }
    }
    None
}

fn infer_repository_from_git() -> Option<String> {
    let cwd = std::env::current_dir().ok()?;
    canonical_repository_key(&origin_remote(&cwd)?)
}

#[derive(Debug)]
enum RouteOutcome {
    Result(String),
    UpstreamError(String),
    Refusal(String),
    UnboundIdentity,
    SchemaMismatch(String),
    GovernanceUnavailable,
    Unavailable(String),
}

#[derive(Clone, Debug, Default, Deserialize, Serialize)]
struct SeamState {
    bound_holder: Option<String>,
    agent_binding: Option<AgentBinding>,
    last_seam_refusal: Option<LastSeamRefusal>,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
struct LastSeamRefusal {
    code: String,
    at_unix_secs: u64,
}

fn route_governed(
    paths: &StatePaths,
    determination: &RungRecord,
    agent_binding: &AgentBinding,
    request: GovernedRequest,
    now: u64,
) -> RouteOutcome {
    if let Err(error) = write_seam_state(paths, governed_seam_state(paths, None, agent_binding)) {
        return RouteOutcome::Unavailable(format!("governed self-report update failed: {error}"));
    }

    let Some(connection_file) = configured_connection_file() else {
        return RouteOutcome::GovernanceUnavailable;
    };
    let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
    let project_root = project_root_for(&cwd);
    let record_paths = paths.clone();
    let agent_binding = agent_binding.clone();
    let runtime = match tokio::runtime::Builder::new_current_thread()
        .enable_io()
        .enable_time()
        .build()
    {
        Ok(runtime) => runtime,
        Err(error) => return RouteOutcome::Unavailable(error.to_string()),
    };
    runtime
        .block_on(async move {
            let options = ConsumerOptions {
                call_timeout: Duration::from_secs(5),
                ..ConsumerOptions::default()
            };
            let consumer = SubcConsumer::connect(&connection_file, options)
                .await
                .map_err(|_| RouteOutcome::GovernanceUnavailable)?;
            let catalog = consumer
                .catalog_list()
                .await
                .map_err(|_| RouteOutcome::GovernanceUnavailable)?;
            let holder = route_holder(&catalog.modules);
            record_unexpected_gh_route_advertisers(&record_paths, &holder.unexpected_advertisers);
            let module_id = holder
                .module_id
                .ok_or(RouteOutcome::GovernanceUnavailable)?;
            let route = consumer
                .open_route(
                    RouteTarget::ManagementSurface {
                        module_id: module_id.clone(),
                    },
                    BindIdentity {
                        project_root: project_root.to_string_lossy().into_owned().into(),
                        harness: "aft-gh-shim".to_string(),
                        session: gh_session_id(&agent_binding.agent_id),
                    },
                    CallOptions::default(),
                )
                .await
                .map_err(|_| RouteOutcome::UnboundIdentity)?;
            if let Err(error) = write_seam_state(
                &record_paths,
                governed_seam_state(&record_paths, Some(module_id.clone()), &agent_binding),
            ) {
                let _ = consumer
                    .close_handle(&route, CloseRouteOptions::default())
                    .await;
                return Err(RouteOutcome::Unavailable(format!(
                    "governed self-report update failed: {error}"
                )));
            }
            let wire_request =
                governed_wire_request(determination, &agent_binding.agent_id, request);
            let body = serde_json::to_vec(&wire_request)
                .map_err(|error| RouteOutcome::SchemaMismatch(error.to_string()))?;
            let response = consumer
                .request(&route, body, CallOptions::default())
                .await
                .map_err(|error| RouteOutcome::Unavailable(error.to_string()));
            let _ = consumer
                .close_handle(&route, CloseRouteOptions::default())
                .await;
            let response = response?;
            let outcome = parse_governed_response(&response)?;
            if let RouteOutcome::Refusal(code) = &outcome {
                write_seam_state(
                    &record_paths,
                    SeamState {
                        bound_holder: Some(module_id),
                        agent_binding: Some(agent_binding),
                        last_seam_refusal: Some(LastSeamRefusal {
                            code: code.clone(),
                            at_unix_secs: now,
                        }),
                    },
                )
                .map_err(|error| {
                    RouteOutcome::Unavailable(format!(
                        "governed self-report update failed: {error}"
                    ))
                })?;
            }
            Ok(outcome)
        })
        .unwrap_or_else(|outcome| outcome)
}

fn refuse_governance_unavailable(
    paths: &StatePaths,
    agent_binding: &AgentBinding,
    now: u64,
) -> i32 {
    let state = SeamState {
        bound_holder: None,
        agent_binding: Some(agent_binding.clone()),
        last_seam_refusal: Some(LastSeamRefusal {
            code: RefusalCode::GovernanceUnavailable.as_str().to_string(),
            at_unix_secs: now,
        }),
    };
    if let Err(error) = write_seam_state(paths, state) {
        return refuse(
            RefusalCode::SeamUnavailable,
            &format!("governed self-report update failed: {error}"),
        );
    }
    refuse(
        RefusalCode::GovernanceUnavailable,
        GOVERNANCE_UNAVAILABLE_TEXT,
    )
}

fn governed_seam_state(
    paths: &StatePaths,
    bound_holder: Option<String>,
    agent_binding: &AgentBinding,
) -> SeamState {
    SeamState {
        bound_holder,
        agent_binding: Some(agent_binding.clone()),
        // A successful route is not a refusal event, so it must retain the last
        // holder refusal for operators to inspect its timestamp and code.
        last_seam_refusal: seam_state(paths).last_seam_refusal,
    }
}

fn write_seam_state(paths: &StatePaths, state: SeamState) -> io::Result<()> {
    fs::create_dir_all(&paths.root)?;
    let bytes = serde_json::to_vec(&state).map_err(io::Error::other)?;
    let temporary = paths.seam_state.with_extension("tmp");
    let mut file = OpenOptions::new()
        .create(true)
        .truncate(true)
        .write(true)
        .open(&temporary)?;
    file.write_all(&bytes)?;
    // A governed result is visible only after its self-report transition is
    // durable enough to survive a process exit. Failure stays on the seam path
    // and is surfaced as a refusal instead of falling through to real `gh`.
    file.sync_data()?;
    fs::rename(temporary, &paths.seam_state)
}

fn seam_state(paths: &StatePaths) -> SeamState {
    fs::read(&paths.seam_state)
        .ok()
        .and_then(|bytes| serde_json::from_slice(&bytes).ok())
        .unwrap_or_default()
}

fn governed_wire_request(
    determination: &RungRecord,
    agent_id: &str,
    request: GovernedRequest,
) -> Value {
    let edit_last = request.edit_last;
    let mut wire = json!({
        "operation": ROUTING_OPERATION,
        "gh_route_schema": 1,
        "action": request.action,
        "target": request.target,
        "body": request.body,
        "repository": request.repository,
        "manifest_version": request.manifest_version,
        "rung_as_of_unix_secs": determination.as_of_unix_secs,
        "metadata": {
            "agent_id": agent_id,
            "pid": std::process::id(),
        },
    });
    // Keep the create wire shape byte-for-byte compatible. The explicit marker
    // lets the route holder perform the same authenticated-user-only mutation
    // that gh's native --edit-last flag requests.
    if edit_last {
        wire["edit_last"] = Value::Bool(true);
    }
    wire
}

fn parse_governed_response(bytes: &[u8]) -> Result<RouteOutcome, RouteOutcome> {
    let value: Value = serde_json::from_slice(bytes).map_err(|_| {
        RouteOutcome::SchemaMismatch(
            "governance seam returned malformed or non-UTF-8 JSON".to_string(),
        )
    })?;
    let object = value.as_object().ok_or_else(|| {
        RouteOutcome::SchemaMismatch("governance seam response must be an object".to_string())
    })?;
    match object.get("outcome").and_then(Value::as_str) {
        Some("result") => {
            let schema = object
                .get("gh_route_schema")
                .and_then(Value::as_u64)
                .ok_or_else(|| {
                    RouteOutcome::SchemaMismatch(
                        "governance seam omitted gh_route_schema".to_string(),
                    )
                })?;
            if schema > 1 {
                return Err(RouteOutcome::SchemaMismatch(format!(
                    "governance seam schema {schema} is newer than supported schema 1"
                )));
            }
            let result = object.get("result").ok_or_else(|| {
                RouteOutcome::SchemaMismatch("governance seam omitted result".to_string())
            })?;
            if let Some(body) = upstream_error_body(object, result) {
                return Ok(RouteOutcome::UpstreamError(body));
            }
            let field_order = object
                .get("field_order")
                .and_then(Value::as_array)
                .ok_or_else(|| {
                    RouteOutcome::SchemaMismatch("governance seam omitted field_order".to_string())
                })?;
            render_governed_response(result, field_order).map(RouteOutcome::Result)
        }
        Some("refusal") => {
            let refusal_code = object
                .get("refusal_code")
                .and_then(Value::as_str)
                .ok_or_else(|| {
                    RouteOutcome::SchemaMismatch(
                        "governance refusal omitted a string refusal_code".to_string(),
                    )
                })?;
            Ok(RouteOutcome::Refusal(refusal_code.to_string()))
        }
        Some("unbound_identity") => Ok(RouteOutcome::UnboundIdentity),
        _ => Err(RouteOutcome::SchemaMismatch(
            "governance seam returned an unknown outcome".to_string(),
        )),
    }
}

fn upstream_error_body(response: &Map<String, Value>, result: &Value) -> Option<String> {
    let result_object = result.as_object();
    let status = response
        .get("status")
        .or_else(|| response.get("status_code"))
        .or_else(|| result_object.and_then(|object| object.get("status")))
        .or_else(|| result_object.and_then(|object| object.get("status_code")))
        .and_then(|value| value.as_u64())?;
    if (200..300).contains(&status) {
        return None;
    }
    let body = response
        .get("error")
        .or_else(|| response.get("body"))
        .or_else(|| result_object.and_then(|object| object.get("error")))
        .or_else(|| result_object.and_then(|object| object.get("body")))
        .unwrap_or(result);
    Some(match body {
        Value::String(body) => body.clone(),
        _ => serde_json::to_string(body).unwrap_or_else(|_| body.to_string()),
    })
}

fn render_governed_response(result: &Value, field_order: &[Value]) -> Result<String, RouteOutcome> {
    let object = result.as_object().ok_or_else(|| {
        RouteOutcome::SchemaMismatch("governance result must be an object".to_string())
    })?;
    let mut output = String::new();
    let mut rendered = BTreeSet::new();
    for field in field_order {
        let field = field.as_str().ok_or_else(|| {
            RouteOutcome::SchemaMismatch("field_order must contain string fields".to_string())
        })?;
        let value = object.get(field).ok_or_else(|| {
            RouteOutcome::SchemaMismatch(format!(
                "field_order references absent result field {field}"
            ))
        })?;
        if !rendered.insert(field) {
            return Err(RouteOutcome::SchemaMismatch(format!(
                "field_order repeats result field {field}"
            )));
        }
        render_field(&mut output, field, value)?;
    }
    if rendered.len() != object.len() {
        return Err(RouteOutcome::SchemaMismatch(
            "field_order does not cover every governed result field".to_string(),
        ));
    }
    Ok(output)
}

fn render_field(output: &mut String, field: &str, value: &Value) -> Result<(), RouteOutcome> {
    match value {
        Value::Array(values) => {
            output.push_str(field);
            output.push_str(":\n");
            for value in values {
                output.push_str("  ");
                output.push_str(&render_scalar(value)?);
                output.push('\n');
            }
        }
        _ => {
            output.push_str(field);
            output.push_str(": ");
            output.push_str(&render_scalar(value)?);
            output.push('\n');
        }
    }
    Ok(())
}

fn render_scalar(value: &Value) -> Result<String, RouteOutcome> {
    match value {
        Value::String(value) => serde_json::to_string(value)
            .map_err(|error| RouteOutcome::SchemaMismatch(error.to_string())),
        Value::Number(_) | Value::Bool(_) | Value::Null => Ok(value.to_string()),
        Value::Object(_) | Value::Array(_) => serde_json::to_string(value)
            .map_err(|error| RouteOutcome::SchemaMismatch(error.to_string())),
    }
}

fn append_bypass_audit(
    paths: &StatePaths,
    tuple: &str,
    repository: Option<&str>,
    now: u64,
) -> io::Result<()> {
    fs::create_dir_all(&paths.root)?;
    let mut record = serde_json::to_vec(&json!({
        "as_of_unix_secs": now,
        "tuple": tuple,
        "repository": repository,
    }))
    .map_err(io::Error::other)?;
    record.push(b'\n');
    let mut file = OpenOptions::new()
        .create(true)
        .append(true)
        .open(&paths.bypass_audit)?;
    file.write_all(&record)?;
    // An operator bypass is allowed only after the audit record is durable enough
    // to survive a process replacement. If this returns an error we do not exec.
    file.sync_data()
}

#[derive(Serialize)]
struct SelfReport {
    shim_version: &'static str,
    gh_routing_schema_floor: u64,
    unexpected_gh_route_advertiser: Option<Vec<String>>,
    bound_holder: Option<String>,
    agent_binding: Option<AgentBinding>,
    last_seam_refusal: Option<LastSeamRefusal>,
    cached_manifest: CachedManifestReport,
    last_rung: LastRungReport,
    bypass_audit: Option<Vec<Value>>,
    bypass_audit_error: Option<String>,
    executing_image: Option<String>,
    executing_image_error: Option<String>,
    real_gh_resolution: Option<RealGhResolution>,
    real_gh_resolution_error: Option<String>,
}

#[derive(Serialize)]
struct CachedManifestReport {
    version: Option<u64>,
    /// Signed provenance metadata for the manifest used by this report; it does
    /// not control artifact validity after signature verification.
    issued_at_unix_secs: Option<u64>,
    /// The compiled trust-set key that verified the installed envelope.
    verified_by_key_id: Option<String>,
    /// Key identifiers compiled into this executing image's manifest trust set.
    compiled_trust_set_key_ids: Vec<&'static str>,
    version_error: Option<String>,
    state: Option<&'static str>,
    state_error: Option<String>,
    diagnostics: Vec<&'static str>,
    diagnostic_guidance: Option<&'static str>,
}

#[derive(Serialize)]
struct LastRungReport {
    rung: Option<&'static str>,
    rung_error: Option<String>,
    as_of_unix_secs: Option<u64>,
    as_of_unix_secs_error: Option<String>,
    determination_inputs: Option<BTreeMap<String, String>>,
    determination_inputs_error: Option<String>,
    recorded_by_image_path: Option<String>,
    recorded_by_version: Option<String>,
    recorded_by_repo_key: Option<String>,
}

#[derive(Serialize)]
struct RealGhResolution {
    path: String,
    shim_path_positions: Vec<usize>,
}

fn print_self_report(paths: &StatePaths) {
    // This is deliberately one JSON document, rather than status lines, so a
    // later forensic process can consume it with jq while every dependency is down.
    if let Ok(document) = render_self_report(paths) {
        let mut stdout = io::stdout().lock();
        let _ = stdout.write_all(document.as_bytes());
    }
}

fn render_self_report(paths: &StatePaths) -> Result<String, serde_json::Error> {
    let report = build_self_report(paths);
    let mut document = serde_json::to_string(&report)?;
    document.push('\n');
    Ok(document)
}

fn build_self_report(paths: &StatePaths) -> SelfReport {
    let image = self_report_executing_image();
    let (real_gh_resolution, real_gh_resolution_error) = match image.as_ref() {
        Ok(image) => match resolve_real_gh(image) {
            Some(path) => (
                Some(RealGhResolution {
                    path: path.to_string_lossy().into_owned(),
                    shim_path_positions: executing_image_path_positions(image),
                }),
                None,
            ),
            None => (
                None,
                Some(
                    "PATH contains no upstream gh after skipping the executing shim image"
                        .to_string(),
                ),
            ),
        },
        Err(error) => (None, Some(format!("executing image unavailable: {error}"))),
    };
    let (bypass_audit, bypass_audit_error) = read_bypass_audit(paths);
    let seam_state = seam_state(paths);
    // When the operator hard-off is set, the shim is byte-transparent passthrough
    // and never probes the daemon or catalog, so the status report reflects that
    // disabled determination instead of whatever stale rung/manifest cache exists.
    let disabled = gh_shim_enabled_from_config_doc(read_user_config_doc().as_deref().unwrap_or(""))
        == Some(false);
    let (cached_manifest, last_rung) = if disabled {
        (disabled_manifest_report(), disabled_last_rung_report())
    } else {
        (cached_manifest_report(paths), last_rung_report(paths))
    };
    SelfReport {
        shim_version: env!("CARGO_PKG_VERSION"),
        gh_routing_schema_floor: SCHEMA_FLOOR,
        unexpected_gh_route_advertiser: unexpected_gh_route_advertisers(paths),
        bound_holder: seam_state.bound_holder,
        agent_binding: seam_state.agent_binding,
        last_seam_refusal: seam_state.last_seam_refusal,
        cached_manifest,
        last_rung,
        bypass_audit,
        bypass_audit_error,
        executing_image: image
            .as_ref()
            .ok()
            .map(|path| path.to_string_lossy().into_owned()),
        executing_image_error: image.err(),
        real_gh_resolution,
        real_gh_resolution_error,
    }
}

/// Self-report for the disabled-by-config state: the shim is a hard passthrough
/// and never consults the manifest, so the cached-manifest slot reports that
/// disabled state rather than a stale on-disk manifest.
fn disabled_manifest_report() -> CachedManifestReport {
    CachedManifestReport {
        version: None,
        issued_at_unix_secs: None,
        verified_by_key_id: None,
        compiled_trust_set_key_ids: trust_set_key_ids(compiled_manifest_trust_set()),
        version_error: None,
        state: Some("disabled"),
        state_error: None,
        diagnostics: Vec::new(),
        diagnostic_guidance: None,
    }
}

/// Self-report for the disabled-by-config state: R1 passthrough with the
/// disabled determination input, matching what `determine_rung` would produce.
fn disabled_last_rung_report() -> LastRungReport {
    LastRungReport {
        rung: Some(Rung::R1.label()),
        rung_error: None,
        as_of_unix_secs: Some(unix_seconds()),
        as_of_unix_secs_error: None,
        determination_inputs: Some(BTreeMap::from([(
            "connection_file".to_string(),
            "disabled_by_config".to_string(),
        )])),
        determination_inputs_error: None,
        recorded_by_image_path: None,
        recorded_by_version: None,
        recorded_by_repo_key: None,
    }
}

fn cached_manifest_report(paths: &StatePaths) -> CachedManifestReport {
    cached_manifest_report_at(paths, unix_seconds())
}

fn cached_manifest_report_at(paths: &StatePaths, now: u64) -> CachedManifestReport {
    cached_manifest_report_at_with(paths, now, compiled_manifest_trust_set())
}

fn cached_manifest_report_at_with(
    paths: &StatePaths,
    now: u64,
    trust_set: &[Option<ManifestTrustKey>],
) -> CachedManifestReport {
    let compiled_trust_set_key_ids = trust_set_key_ids(trust_set);
    match load_manifest_with_trust_set(paths, now, trust_set) {
        Ok(verified) => CachedManifestReport {
            version: Some(verified.manifest.manifest_version),
            issued_at_unix_secs: Some(verified.manifest.issued_at_unix_secs),
            verified_by_key_id: Some(verified.verified_by_key_id),
            compiled_trust_set_key_ids,
            version_error: None,
            state: Some("valid"),
            state_error: None,
            diagnostics: Vec::new(),
            diagnostic_guidance: None,
        },
        Err(ManifestProblem::Missing) => {
            let error = ManifestProblem::Missing.status_label();
            CachedManifestReport {
                version: None,
                issued_at_unix_secs: None,
                verified_by_key_id: None,
                compiled_trust_set_key_ids,
                version_error: Some(error.clone()),
                state: None,
                state_error: Some(error),
                diagnostics: vec![SelfReportDiagnostic::ManifestUnavailable.as_str()],
                diagnostic_guidance: None,
            }
        }
        Err(problem) => {
            // Artifact present but failing. The regressed-manifest arm is loud
            // in self-report: name the arm state first, then the artifact
            // fault that triggered it.
            let diagnostic_guidance = problem.untrusted_manifest_key_steering();
            match read_last_valid_manifest(paths) {
                Some(cache) => CachedManifestReport {
                    version: Some(cache.manifest.manifest_version),
                    issued_at_unix_secs: Some(cache.manifest.issued_at_unix_secs),
                    verified_by_key_id: None,
                    compiled_trust_set_key_ids,
                    version_error: None,
                    state: Some("regressed"),
                    state_error: None,
                    diagnostics: vec![
                        SelfReportDiagnostic::ManifestRegressed.as_str(),
                        problem.diagnostic().as_str(),
                    ],
                    diagnostic_guidance,
                },
                None => {
                    let error = problem.status_label();
                    CachedManifestReport {
                        version: None,
                        issued_at_unix_secs: None,
                        verified_by_key_id: None,
                        compiled_trust_set_key_ids,
                        version_error: Some(error.clone()),
                        state: None,
                        state_error: Some(error),
                        diagnostics: vec![problem.diagnostic().as_str()],
                        diagnostic_guidance,
                    }
                }
            }
        }
    }
}

fn last_rung_report(paths: &StatePaths) -> LastRungReport {
    match fs::read(&paths.rung) {
        Ok(bytes) => match serde_json::from_slice::<RungRecord>(&bytes) {
            Ok(record) => LastRungReport {
                rung: Some(record.rung.label()),
                rung_error: None,
                as_of_unix_secs: Some(record.as_of_unix_secs),
                as_of_unix_secs_error: None,
                determination_inputs: Some(record.inputs),
                determination_inputs_error: None,
                recorded_by_image_path: Some(
                    record
                        .recorded_by_image_path
                        .unwrap_or_else(|| PRE_PROVENANCE_RECORD.to_string()),
                ),
                recorded_by_version: Some(
                    record
                        .recorded_by_version
                        .unwrap_or_else(|| PRE_PROVENANCE_RECORD.to_string()),
                ),
                recorded_by_repo_key: Some(
                    record
                        .recorded_by_repo_key
                        .unwrap_or_else(|| PRE_PROVENANCE_RECORD.to_string()),
                ),
            },
            Err(error) => unavailable_last_rung(format!("corrupt rung cache: {error}")),
        },
        Err(error) if error.kind() == io::ErrorKind::NotFound => {
            unavailable_last_rung("rung cache is unavailable".to_string())
        }
        Err(error) => unavailable_last_rung(format!("rung cache is unavailable: {error}")),
    }
}

fn unavailable_last_rung(error: String) -> LastRungReport {
    LastRungReport {
        rung: None,
        rung_error: Some(error.clone()),
        as_of_unix_secs: None,
        as_of_unix_secs_error: Some(error.clone()),
        determination_inputs: None,
        determination_inputs_error: Some(error),
        recorded_by_image_path: None,
        recorded_by_version: None,
        recorded_by_repo_key: None,
    }
}

fn read_bypass_audit(paths: &StatePaths) -> (Option<Vec<Value>>, Option<String>) {
    let contents = match fs::read_to_string(&paths.bypass_audit) {
        Ok(contents) => contents,
        Err(error) if error.kind() == io::ErrorKind::NotFound => return (Some(Vec::new()), None),
        Err(error) => return (None, Some(format!("bypass audit is unavailable: {error}"))),
    };
    let mut records = Vec::new();
    for (line_number, line) in contents.lines().enumerate() {
        match serde_json::from_str(line) {
            Ok(record) => records.push(record),
            Err(error) => {
                return (
                    None,
                    Some(format!(
                        "bypass audit is corrupt at line {}: {error}",
                        line_number + 1
                    )),
                )
            }
        }
    }
    (Some(records), None)
}

fn unexpected_gh_route_advertisers(paths: &StatePaths) -> Option<Vec<String>> {
    serde_json::from_slice(&fs::read(&paths.unexpected_gh_route_advertisers).ok()?)
        .ok()
        .filter(|advertisers: &Vec<String>| !advertisers.is_empty())
}

fn record_unexpected_gh_route_advertisers(paths: &StatePaths, advertisers: &[String]) {
    if advertisers.is_empty() {
        return;
    }
    let mut recorded = unexpected_gh_route_advertisers(paths)
        .unwrap_or_default()
        .into_iter()
        .collect::<BTreeSet<_>>();
    recorded.extend(advertisers.iter().cloned());
    let Ok(bytes) = serde_json::to_vec(&recorded.into_iter().collect::<Vec<_>>()) else {
        return;
    };
    let _ = fs::create_dir_all(&paths.root);
    let temporary = paths.unexpected_gh_route_advertisers.with_extension("tmp");
    if fs::write(&temporary, bytes).is_ok() {
        let _ = fs::rename(temporary, &paths.unexpected_gh_route_advertisers);
    }
}

fn self_report_executing_image() -> Result<PathBuf, String> {
    let path = std::env::current_exe().map_err(|error| error.to_string())?;
    Ok(path.canonicalize().unwrap_or(path))
}

fn executing_image() -> PathBuf {
    std::env::current_exe()
        .ok()
        .and_then(|path| path.canonicalize().ok().or(Some(path)))
        .unwrap_or_else(|| PathBuf::from("unavailable"))
}

fn executing_image_path_positions(image: &Path) -> Vec<usize> {
    let path = std::env::var_os("PATH").unwrap_or_default();
    std::env::split_paths(&path)
        .enumerate()
        .filter_map(|(index, directory)| same_image(&directory.join("gh"), image).then_some(index))
        .collect()
}

fn delegate(args: &[OsString]) -> i32 {
    let image = executing_image();
    let Some(real_gh) = resolve_real_gh(&image) else {
        return refuse(
            RefusalCode::NoRealGh,
            "PATH contains no upstream gh after skipping the executing shim image",
        );
    };
    exec_real_gh(real_gh, args)
}

fn resolve_real_gh(executing_image: &Path) -> Option<PathBuf> {
    let path = std::env::var_os("PATH")?;
    let shims_dir = std::env::var_os("AFT_GH_SHIMS_DIR").map(PathBuf::from);
    resolve_real_gh_in_path(executing_image, &path, shims_dir.as_deref())
}

fn resolve_real_gh_in_path(
    executing_image: &Path,
    path: &OsStr,
    shims_dir: Option<&Path>,
) -> Option<PathBuf> {
    std::env::split_paths(path).find_map(|directory| {
        if shims_dir.is_some_and(|shims_dir| same_directory(&directory, shims_dir)) {
            return None;
        }
        gh_candidate_names().iter().find_map(|name| {
            let candidate = directory.join(name);
            (is_executable_file(&candidate) && !same_image(&candidate, executing_image))
                .then_some(candidate)
        })
    })
}

#[cfg(windows)]
fn gh_candidate_names() -> &'static [&'static str] {
    &["gh.exe", "gh.cmd", "gh.bat", "gh"]
}

#[cfg(not(windows))]
fn gh_candidate_names() -> &'static [&'static str] {
    &["gh"]
}

fn same_directory(left: &Path, right: &Path) -> bool {
    left == right
        || left
            .canonicalize()
            .ok()
            .zip(right.canonicalize().ok())
            .is_some_and(|(left, right)| left == right)
}

fn is_executable_file(path: &Path) -> bool {
    if !path.is_file() {
        return false;
    }
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        return fs::metadata(path).is_ok_and(|metadata| metadata.permissions().mode() & 0o111 != 0);
    }
    #[cfg(not(unix))]
    true
}

fn same_image(left: &Path, right: &Path) -> bool {
    let left_canonical = left.canonicalize().ok();
    let right_canonical = right.canonicalize().ok();
    if left_canonical.is_some() && left_canonical == right_canonical {
        return true;
    }
    #[cfg(unix)]
    {
        use std::os::unix::fs::MetadataExt;
        if let (Ok(left), Ok(right)) = (fs::metadata(left), fs::metadata(right)) {
            return left.dev() == right.dev() && left.ino() == right.ino();
        }
    }
    false
}

#[cfg(unix)]
fn exec_real_gh(real_gh: PathBuf, args: &[OsString]) -> i32 {
    use std::os::unix::process::CommandExt;
    let error = Command::new(real_gh).args(args).exec();
    // `exec` returns only if a candidate disappeared after the PATH scan. This
    // remains a shim refusal, rather than silently treating a failed exec as a
    // successful no-op.
    refuse(
        RefusalCode::NoRealGh,
        &format!("unable to exec upstream gh: {error}"),
    )
}

#[cfg(not(unix))]
fn exec_real_gh(real_gh: PathBuf, args: &[OsString]) -> i32 {
    match Command::new(real_gh).args(args).status() {
        Ok(status) => status.code().unwrap_or(1),
        Err(error) => refuse(
            RefusalCode::NoRealGh,
            &format!("unable to exec upstream gh: {error}"),
        ),
    }
}

fn refuse(code: RefusalCode, text: &str) -> i32 {
    let text = text.replace(['\n', '\r'], " ");
    eprintln!("gh-shim: {}: {text}", code.as_str());
    REFUSAL_EXIT_STATUS
}

fn current_platform() -> &'static str {
    if cfg!(target_os = "macos") {
        "macos"
    } else if cfg!(target_os = "linux") {
        "linux"
    } else {
        "unsupported"
    }
}

fn unix_seconds() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs()
}

#[cfg(test)]
mod tests {
    use super::*;
    use ring::signature::{Ed25519KeyPair, KeyPair};
    use sha2::{Digest, Sha256};

    const TEST_SEED: [u8; 32] = [
        0x9d, 0x61, 0xb1, 0x9d, 0xef, 0xfd, 0x5a, 0x60, 0xba, 0x84, 0x4a, 0xf4, 0x92, 0xec, 0x2c,
        0xc4, 0x44, 0x49, 0xc5, 0x69, 0x7b, 0x32, 0x69, 0x19, 0x70, 0x3b, 0xac, 0x03, 0x1c, 0xae,
        0x7f, 0x60,
    ];
    /// Seed for the standby-slot fixture key. Test-only material: the compiled
    /// dev trust set keeps exactly one key, and this second keypair exists so
    /// the two-slot trust-set mechanics (standby accepted, unknown refused)
    /// can be exercised against an injected set.
    const STANDBY_TEST_SEED: [u8; 32] = *b"gh-shim-standby-fixture-seed-001";
    const DEV_STANDBY_MANIFEST_KEY_ID: &str = "gh-routing-dev-standby-key-v1";
    /// Issue time baked into the canonical manifest fixture; test clocks and
    /// signed provenance variants are expressed relative to this metadata.
    const FIXTURE_ISSUED_AT: u64 = 1_787_184_000;
    const TEST_NOW: u64 = FIXTURE_ISSUED_AT + 60;
    const FIXTURE_ACCEPTED_SEAM_REFUSAL_CODES: &[&str] = &[
        "identity_mismatch",
        "unmapped_operation",
        "custody_unavailable",
        "schema_unsupported",
        "rate_limited",
    ];

    fn fixture_manifest() -> Manifest {
        serde_json::from_str(include_str!(
            "../tests/fixtures/gh_shim/initial-manifest-v1.json"
        ))
        .expect("initial manifest fixture")
    }

    fn v9_fixture_manifest() -> Manifest {
        serde_json::from_str(include_str!("../tests/fixtures/gh_shim/v9-manifest.json"))
            .expect("v9 manifest fixture")
    }

    fn v10_fixture_manifest() -> Manifest {
        serde_json::from_str(include_str!("../tests/fixtures/gh_shim/v10-manifest.json"))
            .expect("v10 manifest fixture")
    }

    fn edit_last_vectors_fixture() -> Value {
        // JSON has no comment syntax, so strip the human-readable provenance
        // header before parsing the copied producer fixture.
        let fixture = include_str!("../tests/fixtures/gh_shim/edit-last-vectors-v1.json");
        let json = fixture
            .lines()
            .filter(|line| !line.starts_with("//"))
            .collect::<Vec<_>>()
            .join("\n");
        serde_json::from_str(&json).expect("producer edit-last vectors fixture")
    }

    fn signed_with(
        manifest: &Manifest,
        fetched_at_unix_secs: u64,
        seed: &[u8; 32],
        key_id: &str,
    ) -> SignedManifest {
        let key = Ed25519KeyPair::from_seed_unchecked(seed).expect("test key");
        let bytes = serde_json::to_vec(manifest).expect("manifest bytes");
        SignedManifest {
            artifact_id: MANIFEST_ARTIFACT_ID.to_string(),
            envelope_version: ENVELOPE_VERSION,
            key_id: key_id.to_string(),
            fetched_at_unix_secs,
            signature: base64::engine::general_purpose::STANDARD.encode(key.sign(&bytes).as_ref()),
            manifest_bytes: String::from_utf8(bytes).expect("manifest bytes are UTF-8"),
        }
    }

    fn signed(manifest: &Manifest, fetched_at_unix_secs: u64) -> SignedManifest {
        let key = Ed25519KeyPair::from_seed_unchecked(&TEST_SEED).expect("test key");
        assert_eq!(key.public_key().as_ref(), DEV_MANIFEST_PUBLIC_KEY);
        signed_with(
            manifest,
            fetched_at_unix_secs,
            &TEST_SEED,
            DEV_MANIFEST_KEY_ID,
        )
    }

    fn write_signed_manifest(paths: &StatePaths, manifest: Manifest, now: u64) {
        fs::create_dir_all(&paths.root).expect("state root");
        fs::write(
            &paths.manifest,
            serde_json::to_vec(&signed(&manifest, now)).expect("signed manifest"),
        )
        .expect("manifest cache");
    }

    fn write_envelope_fixture(paths: &StatePaths, envelope_json: &str) {
        fs::create_dir_all(&paths.root).expect("state root");
        fs::write(&paths.manifest, envelope_json.as_bytes()).expect("manifest cache");
    }

    fn test_rung_provenance() -> RungRecordProvenance {
        RungRecordProvenance {
            image_path: "/opt/cortexkit/aft-gh-shim".to_string(),
            version: "0.53.0-test".to_string(),
            repo_key: "cortexkit/aft".to_string(),
        }
    }

    #[test]
    fn shim_dispatch_precedes_global_argument_scans_for_both_forms() {
        assert!(is_shim_invocation(
            OsStr::new("gh"),
            &[OsString::from("--version")]
        ));
        assert!(is_shim_invocation(
            OsStr::new("aft"),
            &[OsString::from("gh-shim"), OsString::from("--version")]
        ));
        assert!(!is_shim_invocation(
            OsStr::new("aft"),
            &[OsString::from("--version")]
        ));
    }

    #[test]
    fn reserved_self_report_tokens_are_exactly_the_two_first_arguments() {
        assert_eq!(RESERVED_SELF_REPORT, ["--status", "--shim-version"]);
        assert!(is_reserved_self_report(&[OsString::from("--status")]));
        assert!(is_reserved_self_report(&[OsString::from("--shim-version")]));
        assert!(!is_reserved_self_report(&[OsString::from("status")]));
        assert!(!is_reserved_self_report(&[
            OsString::from("issue"),
            OsString::from("--status")
        ]));
    }

    #[cfg(unix)]
    #[test]
    fn real_gh_resolution_skips_the_managed_shims_directory_without_recursing() {
        use std::os::unix::fs::{symlink, PermissionsExt};

        let directory = tempfile::tempdir().unwrap();
        let image = directory.path().join("aft");
        fs::write(&image, "image").unwrap();
        let shims = directory.path().join("shims");
        let upstream = directory.path().join("upstream");
        fs::create_dir_all(&shims).unwrap();
        fs::create_dir_all(&upstream).unwrap();
        symlink(&image, shims.join("gh")).unwrap();
        let real = upstream.join("gh");
        fs::write(&real, "#!/bin/sh\nexit 0\n").unwrap();
        let mut permissions = fs::metadata(&real).unwrap().permissions();
        permissions.set_mode(0o755);
        fs::set_permissions(&real, permissions).unwrap();
        let path = std::env::join_paths([shims.clone(), upstream]).unwrap();

        assert_eq!(
            resolve_real_gh_in_path(&image, &path, Some(&shims)),
            Some(real)
        );
    }

    #[test]
    fn status_serializes_one_json_document_with_the_exact_top_level_schema() {
        let directory = tempfile::tempdir().unwrap();
        let paths = StatePaths::from_root(directory.path().to_path_buf());
        let document = render_self_report(&paths).expect("self report serialization");
        assert!(document.ends_with('\n'));
        let value: Value = serde_json::from_str(&document).expect("self report JSON");
        let keys = value
            .as_object()
            .expect("self report object")
            .keys()
            .cloned()
            .collect::<Vec<_>>();
        assert_eq!(
            keys,
            vec![
                "shim_version",
                "gh_routing_schema_floor",
                "unexpected_gh_route_advertiser",
                "bound_holder",
                "agent_binding",
                "last_seam_refusal",
                "cached_manifest",
                "last_rung",
                "bypass_audit",
                "bypass_audit_error",
                "executing_image",
                "executing_image_error",
                "real_gh_resolution",
                "real_gh_resolution_error",
            ]
        );
    }

    #[test]
    fn route_holder_is_pinned_and_records_other_advertisers() {
        let holder = select_route_holder([
            "other-module".to_string(),
            ROUTING_HOLDER_MODULE_ID.to_string(),
            "another-module".to_string(),
        ]);
        assert_eq!(holder.module_id.as_deref(), Some(ROUTING_HOLDER_MODULE_ID));
        assert_eq!(
            holder.unexpected_advertisers,
            vec!["another-module", "other-module"]
        );

        let holder = select_route_holder(["other-module".to_string()]);
        assert_eq!(holder.module_id, None);
        assert_eq!(holder.unexpected_advertisers, vec!["other-module"]);
    }

    #[test]
    fn unexpected_route_advertisers_are_persisted_for_self_report() {
        let directory = tempfile::tempdir().unwrap();
        let paths = StatePaths::from_root(directory.path().to_path_buf());
        record_unexpected_gh_route_advertisers(&paths, &["other-module".to_string()]);
        record_unexpected_gh_route_advertisers(&paths, &["another-module".to_string()]);

        assert_eq!(
            unexpected_gh_route_advertisers(&paths),
            Some(vec![
                "another-module".to_string(),
                "other-module".to_string(),
            ])
        );
        assert_eq!(
            build_self_report(&paths).unexpected_gh_route_advertiser,
            Some(vec![
                "another-module".to_string(),
                "other-module".to_string(),
            ])
        );
    }

    #[test]
    fn disabled_by_config_short_circuits_to_r1_without_connection_file_read() {
        let directory = tempfile::tempdir().unwrap();
        let paths = StatePaths::from_root(directory.path().to_path_buf());
        // A disabled shim must resolve R1 with the named reason even when a
        // connection file is configured, and must not touch the daemon/catalog.
        let doc = serde_json::json!({
            "gh_shim": { "enabled": false },
            "subc": { "connection_file": "/nonexistent/connection.json" }
        })
        .to_string();
        let record = determine_rung_from_doc(
            &paths,
            Path::new("/cwd"),
            123,
            std::time::Instant::now() + DISCOVERY_BUDGET,
            Some(&doc),
        );
        assert_eq!(record.record.rung, Rung::R1);
        assert_eq!(
            record
                .record
                .inputs
                .get("connection_file")
                .map(String::as_str),
            Some("disabled_by_config")
        );
        // R1 is never written durably.
        assert!(!paths.root.join("rung-cache.json").exists());
    }

    #[test]
    fn configured_but_unreachable_connection_file_is_distinct_from_absence() {
        let directory = tempfile::tempdir().unwrap();
        let paths = StatePaths::from_root(directory.path().to_path_buf());
        let connection_file = directory.path().join("missing-connection.json");
        let doc = serde_json::json!({
            "subc": { "connection_file": connection_file }
        })
        .to_string();
        let record = determine_rung_from_doc(
            &paths,
            Path::new("/cwd"),
            1,
            std::time::Instant::now() + DISCOVERY_BUDGET,
            Some(&doc),
        );
        assert_eq!(record.record.rung, Rung::R1);
        assert_eq!(
            record
                .record
                .inputs
                .get("connection_file")
                .map(String::as_str),
            Some("unreachable")
        );
    }

    #[test]
    fn enabled_default_keeps_structural_rungs() {
        let directory = tempfile::tempdir().unwrap();
        let paths = StatePaths::from_root(directory.path().to_path_buf());
        // No gh_shim key (default true) and no connection file → structural R1.
        let record = determine_rung_from_doc(
            &paths,
            Path::new("/cwd"),
            1,
            std::time::Instant::now() + DISCOVERY_BUDGET,
            Some("{}"),
        );
        assert_eq!(record.record.rung, Rung::R1);
        assert_eq!(
            record
                .record
                .inputs
                .get("connection_file")
                .map(String::as_str),
            Some("absent_or_unparseable")
        );
    }

    #[test]
    fn xdg_connection_config_precedes_home_config() {
        let directory = tempfile::tempdir().unwrap();
        let xdg = directory.path().join("xdg");
        let home = directory.path().join("home");
        let xdg_connection = directory.path().join("xdg-connection.json");
        let home_connection = directory.path().join("home-connection.json");
        fs::write(&xdg_connection, "{}").unwrap();
        fs::write(&home_connection, "{}").unwrap();
        let xdg_config = xdg.join("cortexkit/aft.jsonc");
        let home_config = home.join(".config/cortexkit/aft.jsonc");
        fs::create_dir_all(xdg_config.parent().unwrap()).unwrap();
        fs::create_dir_all(home_config.parent().unwrap()).unwrap();
        // Serialize through serde_json so Windows backslash paths are
        // JSON-escaped; a raw format! of Path::display() writes `C:\Users\...`
        // into the string, which is invalid JSON and parses to None.
        fs::write(
            &xdg_config,
            serde_json::json!({"subc": {"connection_file": xdg_connection}}).to_string(),
        )
        .unwrap();
        fs::write(
            &home_config,
            serde_json::json!({"subc": {"connection_file": home_connection}}).to_string(),
        )
        .unwrap();

        assert_eq!(
            configured_connection_file_from(Some(xdg.as_os_str()), Some(home.as_os_str())),
            Some(xdg_connection)
        );
    }

    #[test]
    fn initial_manifest_is_complete_and_valid() {
        fixture_manifest()
            .validate()
            .expect("valid initial manifest");
    }

    #[test]
    fn v9_admin_tuple_fixture_differentiates_native_writes_from_raw_api_delete() {
        let manifest = v9_fixture_manifest();
        assert_eq!(manifest.manifest_version, 9);
        manifest.validate().expect("valid v9 manifest");

        for (args, expected_tuple) in [
            (
                vec![
                    OsString::from("repo"),
                    OsString::from("edit"),
                    OsString::from("cortexkit/insula"),
                    OsString::from("--visibility"),
                    OsString::from("public"),
                ],
                "repo edit",
            ),
            (
                vec![
                    OsString::from("repo"),
                    OsString::from("edit"),
                    OsString::from("cortexkit/insula"),
                    OsString::from("--visibility"),
                    OsString::from("private"),
                ],
                "repo edit",
            ),
            (
                vec![
                    OsString::from("run"),
                    OsString::from("delete"),
                    OsString::from("123"),
                    OsString::from("--repo"),
                    OsString::from("cortexkit/insula"),
                ],
                "run delete",
            ),
        ] {
            assert!(matches!(
                classify(&args, &manifest, "macos"),
                Classification::Admin { tuple } if tuple == expected_tuple
            ));
        }

        let raw_api_delete = [
            OsString::from("api"),
            OsString::from("-X"),
            OsString::from("DELETE"),
            OsString::from("repos/cortexkit/insula/actions/runs/123"),
        ];
        assert!(matches!(
            classify(&raw_api_delete, &manifest, "macos"),
            Classification::Unclassified
        ));

        let get_control = [
            OsString::from("api"),
            OsString::from("repos/cortexkit/insula"),
            OsString::from("--jq"),
            OsString::from(".name"),
        ];
        assert!(matches!(
            classify(&get_control, &manifest, "macos"),
            Classification::Mechanical
        ));
    }

    #[test]
    fn v10_workflow_run_admin_tuple_is_version_gated_and_raw_dispatch_stays_unclassified() {
        let manifest = v10_fixture_manifest();
        assert_eq!(manifest.manifest_version, 10);
        manifest.validate().expect("valid v10 manifest");

        let workflow_run = [
            OsString::from("workflow"),
            OsString::from("run"),
            OsString::from("ci.yml"),
            OsString::from("--ref"),
            OsString::from("main"),
        ];
        assert!(matches!(
            classify(&workflow_run, &manifest, "macos"),
            Classification::Admin { tuple } if tuple == "workflow run"
        ));

        // Keep the v10 declaration fields but set its manifest version to 9,
        // verifying that the classifier rejects v10-only declarations when the
        // manifest version is unsupported.
        let mut v9_manifest = manifest.clone();
        v9_manifest.manifest_version = 9;
        assert!(matches!(
            classify(&workflow_run, &v9_manifest, "macos"),
            Classification::Unclassified
        ));

        let raw_api_dispatch = [
            OsString::from("api"),
            OsString::from("-X"),
            OsString::from("POST"),
            OsString::from("repos/cortexkit/aft/actions/workflows/ci.yml/dispatches"),
        ];
        for manifest in [&manifest, &v9_manifest] {
            assert!(matches!(
                classify(&raw_api_dispatch, manifest, "macos"),
                Classification::Unclassified
            ));
        }
    }

    #[test]
    fn v10_edit_last_comment_variants_are_exactly_governed_and_author_scoped() {
        let manifest = v10_fixture_manifest();
        manifest.validate().expect("valid v10 manifest");

        for (verb, number) in [("issue", "42"), ("pr", "7")] {
            let args = [
                OsString::from(verb),
                OsString::from("comment"),
                OsString::from(number),
                OsString::from("--body"),
                OsString::from("replace the draft"),
                OsString::from("--edit-last"),
            ];
            let Classification::Governed { tuple, canonical } = classify(&args, &manifest, "macos")
            else {
                panic!("native edit-last should use the governed comment tuple: {args:?}");
            };
            assert_eq!(tuple, format!("{verb} comment"));

            let request =
                canonicalize_governed(&args, &tuple, &canonical, manifest.manifest_version)
                    .expect("reviewed edit-last form should canonicalize");
            assert!(request.edit_last);
            assert_eq!(request.target["number"], number);
            assert_eq!(request.body["body"], "replace the draft");

            let wire = governed_wire_request(
                &(RungDetermination::r3(1, manifest.manifest_version, &test_rung_provenance())
                    .record),
                "alfonso-aft",
                request,
            );
            assert_eq!(wire["edit_last"], true);
        }

        let bare_create = [
            OsString::from("issue"),
            OsString::from("comment"),
            OsString::from("42"),
            OsString::from("--body"),
            OsString::from("new comment"),
        ];
        let Classification::Governed { tuple, canonical } =
            classify(&bare_create, &manifest, "macos")
        else {
            panic!("bare comment creation must remain governed");
        };
        let request =
            canonicalize_governed(&bare_create, &tuple, &canonical, manifest.manifest_version)
                .expect("bare comment creation should remain canonicalizable");
        assert!(!request.edit_last);
        let wire = governed_wire_request(
            &(RungDetermination::r3(1, manifest.manifest_version, &test_rung_provenance()).record),
            "alfonso-aft",
            request,
        );
        assert!(wire.get("edit_last").is_none());

        // The edit-last allowlist is enforced starting with manifest version 10;
        // older signed manifests do not gain this mutation merely because they
        // contain the same tuple.
        let mut v9_manifest = manifest.clone();
        v9_manifest.manifest_version = 9;
        let v9_edit = [
            OsString::from("pr"),
            OsString::from("comment"),
            OsString::from("7"),
            OsString::from("--body"),
            OsString::from("replace the draft"),
            OsString::from("--edit-last"),
        ];
        assert!(matches!(
            classify(&v9_edit, &v9_manifest, "macos"),
            Classification::Unclassified
        ));

        // gh also exposes --delete-last, but deletion is not the
        // authenticated-user-only edit operation allowed by --edit-last, so this
        // flag must fail closed.
        let delete_last = [
            OsString::from("pr"),
            OsString::from("comment"),
            OsString::from("7"),
            OsString::from("--body"),
            OsString::from("replace the draft"),
            OsString::from("--delete-last"),
        ];
        assert!(matches!(
            classify(&delete_last, &manifest, "macos"),
            Classification::Unclassified
        ));

        // The edit-last allowance applies only to the explicitly supported issue
        // and pull-request comment tuples; another governed tuple must remain
        // unclassified when it carries this flag.
        let reaction_edit = [
            OsString::from("issue"),
            OsString::from("reaction"),
            OsString::from("42"),
            OsString::from("--reaction"),
            OsString::from("+1"),
            OsString::from("--edit-last"),
        ];
        assert!(matches!(
            classify(&reaction_edit, &manifest, "macos"),
            Classification::Unclassified
        ));
    }

    #[test]
    fn producer_edit_last_vectors_pin_consumer_wire_request_and_refusals() {
        const EXPECTED_SHA256: &str =
            "cd22bb4de80b5c44b500d75220f03d3b0908f0e67101842de0c29c86b1e9b9e0";
        let fixture_bytes = include_bytes!("../tests/fixtures/gh_shim/edit-last-vectors-v1.json");
        assert_eq!(
            format!("{:x}", Sha256::digest(fixture_bytes)),
            EXPECTED_SHA256,
            "producer edit-last vectors changed; re-pin by copying the fixture from repo CortexKit/prefrontal at commit 0b1dea6b, then update this consumer fixture and digest"
        );

        let vectors = edit_last_vectors_fixture();
        let vector_case = |name: &str| {
            vectors["cases"]
                .as_array()
                .expect("producer vector cases")
                .iter()
                .find(|case| case["name"] == name)
                .unwrap_or_else(|| panic!("producer vector case {name} is missing"))
        };
        let happy_request = vector_case("edit_last_happy")["request"].clone();
        let happy_body_fields = happy_request["body"]
            .as_object()
            .expect("producer happy request body")
            .keys()
            .cloned()
            .collect::<Vec<_>>();
        assert!(vector_case("absent_edit_last_create")["request"]
            .get("edit_last")
            .is_none());

        let manifest = v10_fixture_manifest();
        let args = [
            OsString::from("pr"),
            OsString::from("comment"),
            OsString::from("372"),
            OsString::from("--edit-last"),
            OsString::from("--body-file"),
            OsString::from("-"),
        ];
        let Classification::Governed { tuple, canonical } = classify(&args, &manifest, "macos")
        else {
            panic!("the native edit-last command must remain governed");
        };
        assert_eq!(tuple, "pr comment");
        let request = canonicalize_governed(&args, &tuple, &canonical, manifest.manifest_version)
            .expect("native edit-last command should canonicalize");
        let determination =
            RungDetermination::r3(1, manifest.manifest_version, &test_rung_provenance());
        let wire = governed_wire_request(&determination.record, "consumer-agent", request);

        // Compare the complete request shape after replacing values that are
        // intentionally different for this consumer command or process.
        let mut expected = happy_request;
        expected["action"] = json!("pr comment");
        expected["target"] = json!({"number": "372"});
        expected["body"] = wire["body"].clone();
        expected["manifest_version"] = json!(manifest.manifest_version);
        expected["rung_as_of_unix_secs"] = json!(determination.record.as_of_unix_secs);
        expected["metadata"]["pid"] = json!(std::process::id());
        expected["metadata"]
            .as_object_mut()
            .expect("expected metadata object")
            .remove("agent_id");
        let mut actual = wire;
        actual["metadata"]
            .as_object_mut()
            .expect("actual metadata object")
            .remove("agent_id");
        assert_eq!(
            actual["body"]
                .as_object()
                .expect("actual request body")
                .keys()
                .cloned()
                .collect::<Vec<_>>(),
            happy_body_fields,
            "consumer body fields drifted from producer shape"
        );
        assert_eq!(
            actual, expected,
            "consumer request drifted from producer shape"
        );
        assert_eq!(
            actual["edit_last"], true,
            "edit_last marker must be present"
        );

        for case_name in ["edit_last_no_own_comment", "edit_last_unsupported_action"] {
            let code = vector_case(case_name)["response"]["refusal_code"]
                .as_str()
                .expect("producer refusal code");
            let response = json!({"outcome": "refusal", "refusal_code": code});
            let outcome = parse_governed_response(&serde_json::to_vec(&response).unwrap())
                .expect("producer refusal should parse");
            assert!(matches!(outcome, RouteOutcome::Refusal(ref actual) if actual == code));
            assert_eq!(
                RefusalCode::SeamRefusal.as_str(),
                "gh_shim_seam_refusal",
                "open-world holder refusal codes must use the seam refusal classification"
            );
            assert_eq!(
                seam_refusal_text(code),
                format!("governance seam refused the action: {code}"),
                "holder refusal code must pass through without remapping"
            );
        }
    }

    #[test]
    fn manifest_rejects_duplicate_tiers_and_empty_api_rationales() {
        let mut duplicate = fixture_manifest();
        duplicate
            .tiers
            .get_mut(&Tier::Admin)
            .unwrap()
            .push(TupleDecl::Details {
                tuple: "issue comment".to_string(),
                platform: vec!["macos".to_string()],
                api_match: None,
                rationale: None,
            });
        assert!(duplicate.validate().unwrap_err().contains("both"));

        let mut empty_api = fixture_manifest();
        empty_api
            .tiers
            .get_mut(&Tier::Admin)
            .unwrap()
            .push(TupleDecl::Details {
                tuple: "api patch close".to_string(),
                platform: vec!["macos".to_string()],
                api_match: Some(String::new()),
                rationale: None,
            });
        assert!(empty_api.validate().unwrap_err().contains("rationale"));

        let mut malformed_binding = fixture_manifest();
        malformed_binding.bindings.insert(
            "https://github.com/cortexkit/aft.git".to_string(),
            "alfonso-aft".to_string(),
        );
        assert!(malformed_binding
            .validate()
            .unwrap_err()
            .contains("canonical owner/name"));
    }

    #[test]
    fn binding_keys_and_governed_session_identity_are_stable() {
        assert_eq!(
            canonical_repository_key("https://github.com/CortexKit/aft.git"),
            Some("cortexkit/aft".to_string())
        );
        assert_eq!(
            canonical_repository_key("git@github.com:cortexkit/aft.git"),
            Some("cortexkit/aft".to_string())
        );
        assert_eq!(gh_session_id("alfonso-aft"), "gh-shim:alfonso-aft");

        let request = GovernedRequest {
            action: "issue comment".to_string(),
            target: Map::new(),
            body: Map::new(),
            repository: Some("cortexkit/aft".to_string()),
            manifest_version: 1,
            edit_last: false,
        };
        let determination = RungDetermination::r3(7, 1, &test_rung_provenance());
        let wire = governed_wire_request(&determination.record, "alfonso-aft", request);
        assert_eq!(wire["metadata"]["agent_id"], "alfonso-aft");
        assert_eq!(wire["metadata"]["pid"], std::process::id());
    }

    #[test]
    fn manifest_rejects_repo_sections_that_add_or_lower_a_tuple() {
        let mut manifest = fixture_manifest();
        manifest.repository_sections.insert(
            "owner/repo".to_string(),
            RepositorySection {
                tiers: BTreeMap::from([(
                    Tier::Mechanical,
                    vec![TupleDecl::Details {
                        tuple: "issue comment".to_string(),
                        platform: vec!["macos".to_string()],
                        api_match: None,
                        rationale: None,
                    }],
                )]),
                removed_tuples: Vec::new(),
            },
        );
        assert!(manifest.validate().unwrap_err().contains("lowers"));

        manifest.repository_sections.insert(
            "owner/repo".to_string(),
            RepositorySection {
                tiers: BTreeMap::from([(
                    Tier::Admin,
                    vec![TupleDecl::Details {
                        tuple: "workflow dispatch".to_string(),
                        platform: vec!["macos".to_string()],
                        api_match: None,
                        rationale: None,
                    }],
                )]),
                removed_tuples: Vec::new(),
            },
        );
        assert!(manifest.validate().unwrap_err().contains("adds"));
    }

    #[test]
    fn signed_cache_rejects_tampering_and_old_schema_floor() {
        let directory = tempfile::tempdir().unwrap();
        let paths = StatePaths::from_root(directory.path().to_path_buf());
        let now = TEST_NOW;
        write_signed_manifest(&paths, fixture_manifest(), now);
        assert_eq!(load_manifest(&paths, now).unwrap().manifest_version, 1);

        // Tamper with the signed manifest bytes inside the envelope: the
        // signature verifies the distributed bytes, so any edit is fatal.
        let mut value: Value = serde_json::from_slice(&fs::read(&paths.manifest).unwrap()).unwrap();
        let tampered =
            value["manifest_bytes"]
                .as_str()
                .unwrap()
                .replacen("issue view", "issue View", 1);
        value["manifest_bytes"] = Value::String(tampered);
        fs::write(&paths.manifest, serde_json::to_vec(&value).unwrap()).unwrap();
        assert!(matches!(
            load_manifest(&paths, now),
            Err(ManifestProblem::Invalid(_))
        ));
        // A validation failure immediately enters the regressed arm, so status
        // names that state first and the artifact fault second.
        assert_eq!(
            cached_manifest_report_at(&paths, now).diagnostics,
            vec![
                SelfReportDiagnostic::ManifestRegressed.as_str(),
                SelfReportDiagnostic::ManifestInvalid.as_str(),
            ]
        );

        let mut below_floor = fixture_manifest();
        below_floor.schema_floor = 0;
        write_signed_manifest(&paths, below_floor, now);
        assert!(matches!(
            load_manifest(&paths, now),
            Err(ManifestProblem::BelowFloor { manifest_floor: 0 })
        ));
    }

    #[test]
    fn no_verb_and_help_invocations_are_mechanical_on_a_governed_manifest() {
        let manifest = fixture_manifest();
        for args in [
            Vec::new(),
            vec![OsString::from("--version")],
            vec![OsString::from("--help")],
            vec![OsString::from("-h")],
            vec![OsString::from("help"), OsString::from("pr")],
        ] {
            assert!(
                matches!(
                    classify(&args, &manifest, "macos"),
                    Classification::Mechanical
                ),
                "expected passthrough classification for {args:?}"
            );
        }
    }

    #[test]
    fn unmapped_get_and_actions_reads_are_mechanical_but_writes_remain_unclassified() {
        let mut manifest = fixture_manifest();
        manifest.api_rules.clear();

        for args in [
            vec![
                OsString::from("api"),
                OsString::from("/repos/cortexkit/aft/actions/runs"),
            ],
            vec![
                OsString::from("api"),
                OsString::from("--method"),
                OsString::from("GET"),
                OsString::from("/repos/cortexkit/aft/actions/runs"),
            ],
            vec![
                OsString::from("api"),
                OsString::from("-X"),
                OsString::from("GET"),
                OsString::from("/repos/cortexkit/aft/actions/runs"),
            ],
            vec![OsString::from("run"), OsString::from("view")],
            vec![OsString::from("run"), OsString::from("list")],
            vec![OsString::from("run"), OsString::from("watch")],
            vec![OsString::from("workflow"), OsString::from("view")],
            vec![OsString::from("workflow"), OsString::from("list")],
        ] {
            assert!(
                matches!(
                    classify(&args, &manifest, "macos"),
                    Classification::Mechanical
                ),
                "expected read passthrough classification for {args:?}"
            );
        }

        for args in [
            vec![
                OsString::from("api"),
                OsString::from("-X"),
                OsString::from("POST"),
                OsString::from("/repos/cortexkit/aft/actions/runs"),
            ],
            vec![
                OsString::from("api"),
                OsString::from("-f"),
                OsString::from("key=value"),
                OsString::from("/repos/cortexkit/aft/actions/runs"),
            ],
        ] {
            assert!(
                matches!(
                    classify(&args, &manifest, "macos"),
                    Classification::Unclassified
                ),
                "expected fail-closed classification for {args:?}"
            );
        }
    }

    #[test]
    fn classification_is_allowlist_driven_without_a_write_heuristic() {
        let manifest = fixture_manifest();
        assert!(matches!(
            classify(
                &[OsString::from("issue"), OsString::from("view")],
                &manifest,
                "macos"
            ),
            Classification::Mechanical
        ));
        assert!(matches!(
            classify(
                &[OsString::from("api"), OsString::from("/repos/a/b")],
                &manifest,
                "macos"
            ),
            Classification::Mechanical
        ));
        assert!(matches!(
            classify(
                &[
                    OsString::from("api"),
                    OsString::from("--method=POST"),
                    OsString::from("/repos/a/b")
                ],
                &manifest,
                "macos"
            ),
            Classification::Unclassified
        ));
        assert!(matches!(
            classify(
                &[
                    OsString::from("api"),
                    OsString::from("--method"),
                    OsString::from("POST"),
                    OsString::from("/repos/a/b")
                ],
                &manifest,
                "macos"
            ),
            Classification::Unclassified
        ));
        assert!(matches!(
            classify(
                &[OsString::from("alias"), OsString::from("set")],
                &manifest,
                "macos"
            ),
            Classification::Unclassified
        ));
        assert!(matches!(
            classify(
                &[
                    OsString::from("alias"),
                    OsString::from("set"),
                    OsString::from("--write")
                ],
                &manifest,
                "macos"
            ),
            Classification::Unclassified
        ));
    }

    #[test]
    fn canonical_repository_key_parses_github_remotes_and_rejects_foreign_hosts() {
        for remote in [
            "https://github.com/CortexKit/Aft",
            "https://github.com/cortexkit/aft.git",
            "https://github.com/cortexkit/aft/",
            "https://github.com/cortexkit/aft.git/",
            "git@github.com:cortexkit/aft.git",
            "ssh://git@github.com/cortexkit/aft",
            "cortexkit/aft",
        ] {
            assert_eq!(
                canonical_repository_key(remote).as_deref(),
                Some("cortexkit/aft")
            );
        }
        for remote in [
            "https://gitlab.com/cortexkit/aft.git",
            "ssh://git@gitlab.com/cortexkit/aft",
            "git@gitlab.com:cortexkit/aft.git",
        ] {
            assert_eq!(canonical_repository_key(remote), None);
        }
    }

    #[test]
    fn invalid_repository_argument_refuses_before_seam_routing() {
        let manifest = fixture_manifest();
        let canonical = manifest.canonicalization["issue comment"].clone();
        let error = canonicalize_governed(
            &[
                OsString::from("--repo"),
                OsString::from("not/an/owner-name"),
                OsString::from("issue"),
                OsString::from("comment"),
                OsString::from("42"),
                OsString::from("--body"),
                OsString::from("hello"),
            ],
            "issue comment",
            &canonical,
            1,
        )
        .expect_err("an unparseable repository must abort before seam routing");
        assert_eq!(error, "repository not/an/owner-name is not owner/name");
        assert_eq!(
            refuse_governed_canonicalization(&error),
            REFUSAL_EXIT_STATUS,
            "a pre-routing governance refusal must have a nonzero exit status"
        );
    }

    #[test]
    fn governed_canonicalization_normalizes_flags_and_explicit_repo_wins() {
        let manifest = fixture_manifest();
        let canonical = manifest.canonicalization["issue comment"].clone();
        let request = canonicalize_governed(
            &[
                OsString::from("--repo=owner/explicit"),
                OsString::from("issue"),
                OsString::from("comment"),
                OsString::from("42"),
                OsString::from("--body"),
                OsString::from("hello"),
            ],
            "issue comment",
            &canonical,
            1,
        )
        .unwrap();
        assert_eq!(request.repository.as_deref(), Some("owner/explicit"));
        assert_eq!(request.target["number"], "42");
        assert_eq!(request.body["body"], "hello");
    }

    #[test]
    fn speech_body_file_forms_are_allowed_and_forward_fixture_contents() {
        let manifest = fixture_manifest();
        let body_file = fixture_dir().join("governed-speech.md");
        let expected_body = fs::read_to_string(&body_file).expect("speech body fixture");

        for (expected_tuple, verb, subcommand, target) in [
            ("issue comment", "issue", "comment", "42"),
            ("pr comment", "pr", "comment", "7"),
            ("pr review", "pr", "review", "7"),
        ] {
            let canonical = manifest.canonicalization[expected_tuple].clone();
            for (flag, suffix) in [("--body-file", ""), ("-F", "")]
                .into_iter()
                .chain([("--body-file=", "equals"), ("-F=", "equals")])
            {
                let file_arg = if suffix.is_empty() {
                    body_file.to_string_lossy().into_owned()
                } else {
                    format!("{flag}{}", body_file.display())
                };
                let args = if suffix.is_empty() {
                    vec![
                        OsString::from(verb),
                        OsString::from(subcommand),
                        OsString::from(target),
                        OsString::from(flag),
                        OsString::from(file_arg),
                    ]
                } else {
                    vec![
                        OsString::from(verb),
                        OsString::from(subcommand),
                        OsString::from(target),
                        OsString::from(file_arg),
                    ]
                };
                assert!(matches!(
                    classify(&args, &manifest, "macos"),
                    Classification::Governed { ref tuple, .. } if tuple == expected_tuple
                ));
                let request = canonicalize_governed(&args, expected_tuple, &canonical, 1)
                    .expect("body-file form should canonicalize");
                let determination = RungDetermination::r3(1, 1, &test_rung_provenance());
                let wire = governed_wire_request(&determination.record, "agent-7", request);
                assert_eq!(wire["body"]["body"], expected_body);
            }
        }

        let reaction = manifest.canonicalization["issue reaction"].clone();
        let error = canonicalize_governed(
            &[
                OsString::from("issue"),
                OsString::from("reaction"),
                OsString::from("42"),
                OsString::from("--body-file"),
                OsString::from(body_file),
            ],
            "issue reaction",
            &reaction,
            1,
        )
        .expect_err("body-file is speech-only vocabulary");
        assert_eq!(error, "undeclared flag --body-file");
    }

    #[test]
    fn body_file_failures_refuse_instead_of_forwarding_an_empty_body() {
        let manifest = fixture_manifest();
        let canonical = manifest.canonicalization["pr comment"].clone();
        let directory = tempfile::tempdir().unwrap();
        let missing = directory.path().join("missing.md");
        let invalid = directory.path().join("invalid-utf8.md");
        fs::write(&invalid, [0xff, 0xfe]).unwrap();

        for path in [missing, invalid] {
            let error = canonicalize_governed(
                &[
                    OsString::from("pr"),
                    OsString::from("comment"),
                    OsString::from("7"),
                    OsString::from("--body-file"),
                    OsString::from(&path),
                ],
                "pr comment",
                &canonical,
                1,
            )
            .expect_err("an unreadable body file must refuse");
            assert!(error.starts_with("--body-file: could not read body file "));
            assert!(error.contains(&path.display().to_string()));
            assert_eq!(
                refuse_governed_canonicalization(&error),
                REFUSAL_EXIT_STATUS
            );
        }
    }

    #[test]
    fn body_file_dash_reads_stdin_under_the_caller_permissions() {
        let mut stdin = std::io::Cursor::new("body supplied through stdin");
        assert_eq!(
            read_body_file_from(Path::new("-"), &mut stdin).unwrap(),
            "body supplied through stdin"
        );
    }

    #[test]
    fn pr_review_action_and_body_matrix_reaches_the_governed_payload() {
        let manifest = fixture_manifest();
        let canonical = manifest.canonicalization["pr review"].clone();
        let body_file = fixture_dir().join("governed-speech.md");
        let expected_body = fs::read_to_string(&body_file).expect("speech body fixture");

        for (action_flag, event) in [
            ("--approve", "APPROVE"),
            ("--comment", "COMMENT"),
            ("--request-changes", "REQUEST_CHANGES"),
        ] {
            for (body_flag, body_value) in [("--body", "inline review"), ("-b", "short review")] {
                let args = vec![
                    OsString::from("pr"),
                    OsString::from("review"),
                    OsString::from("7"),
                    OsString::from(action_flag),
                    OsString::from(body_flag),
                    OsString::from(body_value),
                ];
                assert!(matches!(
                    classify(&args, &manifest, "macos"),
                    Classification::Governed { ref tuple, .. } if tuple == "pr review"
                ));
                let request = canonicalize_governed(&args, "pr review", &canonical, 1)
                    .expect("review action with inline body should canonicalize");
                assert_eq!(request.body["event"], event);
                assert_eq!(request.body["body"], body_value);
            }

            let args = vec![
                OsString::from("pr"),
                OsString::from("review"),
                OsString::from("7"),
                OsString::from(action_flag),
                OsString::from("--body-file"),
                OsString::from(&body_file),
            ];
            let request = canonicalize_governed(&args, "pr review", &canonical, 1)
                .expect("review action with body-file should canonicalize");
            assert_eq!(request.body["event"], event);
            assert_eq!(request.body["body"], expected_body);
        }

        for action_flag in ["--approve", "--request-changes"] {
            let args = vec![
                OsString::from("pr"),
                OsString::from("review"),
                OsString::from("7"),
                OsString::from(action_flag),
            ];
            let request = canonicalize_governed(&args, "pr review", &canonical, 1)
                .expect("approve/request-changes may omit review prose");
            assert_eq!(
                request.body["event"],
                action_flag
                    .trim_start_matches("--")
                    .to_ascii_uppercase()
                    .replace('-', "_")
            );
            assert!(!request.body.contains_key("body"));
        }

        let duplicate = [
            OsString::from("pr"),
            OsString::from("review"),
            OsString::from("7"),
            OsString::from("--approve"),
            OsString::from("--comment"),
            OsString::from("--body"),
            OsString::from("review"),
        ];
        assert_eq!(
            canonicalize_governed(&duplicate, "pr review", &canonical, 1).unwrap_err(),
            "pr review accepts only one of --approve, --comment, or --request-changes"
        );
    }

    #[test]
    fn upstream_api_errors_fail_without_changing_success_status() {
        let error_response = json!({
            "outcome": "result",
            "gh_route_schema": 1,
            "result": {
                "status": 404,
                "error": {"message": "Not Found", "documentation_url": "https://docs.github.com"}
            }
        });
        let error_outcome =
            parse_governed_response(&serde_json::to_vec(&error_response).unwrap()).unwrap();
        let error_body = match error_outcome {
            RouteOutcome::UpstreamError(body) => body,
            other => panic!("expected upstream error, got {other:?}"),
        };
        assert!(error_body.contains("Not Found"));
        let directory = tempfile::tempdir().unwrap();
        let paths = StatePaths::from_root(directory.path().to_path_buf());
        let binding = AgentBinding {
            repo: "owner/repo".to_string(),
            agent_id: "agent-7".to_string(),
        };
        assert_eq!(
            governed_outcome_status(
                &paths,
                &binding,
                123,
                RouteOutcome::UpstreamError(error_body)
            ),
            UPSTREAM_FAILURE_EXIT_STATUS
        );

        let success_response = json!({
            "outcome": "result",
            "gh_route_schema": 1,
            "result": {"status": 201, "url": "https://github.com/example"},
            "field_order": ["status", "url"]
        });
        let success_outcome =
            parse_governed_response(&serde_json::to_vec(&success_response).unwrap()).unwrap();
        assert!(matches!(&success_outcome, RouteOutcome::Result(_)));
        assert_eq!(
            governed_outcome_status(&paths, &binding, 123, success_outcome),
            0
        );
    }

    #[test]
    fn governed_renderer_is_deterministic_for_scalars_arrays_and_escapes() {
        let result = json!({"message":"snowman ☃\n", "items":["a", 2], "ok":true});
        let order = vec![json!("ok"), json!("message"), json!("items")];
        assert_eq!(
            render_governed_response(&result, &order).unwrap(),
            "ok: true\nmessage: \"snowman ☃\\n\"\nitems:\n  \"a\"\n  2\n"
        );
        assert!(matches!(
            render_governed_response(&json!("scalar"), &order),
            Err(RouteOutcome::SchemaMismatch(_))
        ));
    }

    #[test]
    fn lower_rungs_are_cached_durably_but_r1_is_not_written() {
        let directory = tempfile::tempdir().unwrap();
        let paths = StatePaths::from_root(directory.path().to_path_buf());
        let determination = RungDetermination::r2(
            123,
            R2Reason::DaemonUnreachable,
            None,
            &test_rung_provenance(),
        );
        write_rung_record_silently(&paths, &determination.record);
        assert_eq!(load_rung_record(&paths).unwrap().rung, Rung::R2);
        assert!(!paths.root.join("r1-cache.json").exists());
    }

    #[test]
    fn governed_bound_disposition_is_reason_independent_except_operator_hard_off() {
        const EXPECTED_RUNG_SHAPE_COUNT: usize = 11;

        let directory = tempfile::tempdir().unwrap();
        let paths = StatePaths::from_root(directory.path().to_path_buf());
        let connection_file = directory.path().join("connection.json");
        fs::write(&connection_file, "present").unwrap();
        let missing_connection = directory.path().join("missing-connection.json");
        let disabled_doc = serde_json::json!({
            "gh_shim": { "enabled": false },
            "subc": { "connection_file": missing_connection }
        })
        .to_string();
        let unreachable_doc = serde_json::json!({
            "subc": { "connection_file": directory.path().join("still-missing.json") }
        })
        .to_string();
        let budget_doc = serde_json::json!({
            "subc": { "connection_file": connection_file }
        })
        .to_string();
        let future_deadline = || std::time::Instant::now() + DISCOVERY_BUDGET;
        let r1_cases = [
            (
                R1Reason::DisabledByConfig,
                determine_rung_from_doc(
                    &paths,
                    directory.path(),
                    1,
                    future_deadline(),
                    Some(&disabled_doc),
                ),
            ),
            (
                R1Reason::AbsentOrUnparseable,
                determine_rung_from_doc(&paths, directory.path(), 1, future_deadline(), Some("{}")),
            ),
            (
                R1Reason::Unreachable,
                determine_rung_from_doc(
                    &paths,
                    directory.path(),
                    1,
                    future_deadline(),
                    Some(&unreachable_doc),
                ),
            ),
            (
                R1Reason::DiscoveryBudgetExhausted,
                determine_rung_from_doc(
                    &paths,
                    directory.path(),
                    1,
                    std::time::Instant::now() - Duration::from_millis(1),
                    Some(&budget_doc),
                ),
            ),
        ];
        assert_eq!(r1_cases.len(), R1Reason::ALL.len());
        for (reason, determination) in &r1_cases {
            assert_eq!(determination.record.rung, Rung::R1);
            assert_eq!(
                determination
                    .record
                    .inputs
                    .get("connection_file")
                    .map(String::as_str),
                Some(reason.diagnostic())
            );
        }

        let mut determinations = r1_cases
            .into_iter()
            .map(|(_, determination)| determination)
            .collect::<Vec<_>>();
        determinations.extend(
            R2Reason::ALL
                .into_iter()
                .map(|reason| RungDetermination::r2(1, reason, Some(1), &test_rung_provenance())),
        );
        determinations.push(RungDetermination::r3(1, 1, &test_rung_provenance()));
        assert_eq!(
            R1Reason::ALL.len() + R2Reason::ALL.len() + 1,
            EXPECTED_RUNG_SHAPE_COUNT,
            "update the explicit disposition matrix when a rung shape is added"
        );
        assert_eq!(determinations.len(), EXPECTED_RUNG_SHAPE_COUNT);

        let manifest = fixture_manifest();
        let governed_args = [
            OsString::from("issue"),
            OsString::from("comment"),
            OsString::from("42"),
            OsString::from("--body"),
            OsString::from("hello"),
        ];
        let admin_args = [
            OsString::from("pr"),
            OsString::from("merge"),
            OsString::from("42"),
        ];
        let mechanical_args = [
            OsString::from("issue"),
            OsString::from("view"),
            OsString::from("42"),
        ];
        let governed = classify(&governed_args, &manifest, "macos");
        let admin = classify(&admin_args, &manifest, "macos");
        let mechanical = classify(&mechanical_args, &manifest, "macos");
        let binding = || AgentBinding {
            repo: "cortexkit/aft".to_string(),
            agent_id: "alfonso-aft".to_string(),
        };

        for determination in &determinations {
            let bound_governed = structural_governance_disposition(
                determination,
                &governed,
                Some(binding()),
                manifest.manifest_version,
            );
            if determination.operator_disabled {
                assert!(matches!(bound_governed, GovernanceDisposition::Delegate));
            } else if determination.record.rung == Rung::R3 {
                assert!(matches!(bound_governed, GovernanceDisposition::Ready));
            } else {
                assert!(matches!(
                    bound_governed,
                    GovernanceDisposition::Unavailable(_)
                ));
            }

            assert!(matches!(
                structural_governance_disposition(
                    determination,
                    &governed,
                    None,
                    manifest.manifest_version,
                ),
                GovernanceDisposition::Delegate
            ));
            assert!(matches!(
                structural_governance_disposition(
                    determination,
                    &mechanical,
                    Some(binding()),
                    manifest.manifest_version,
                ),
                GovernanceDisposition::Delegate
            ));

            if determination.record.rung != Rung::R3 && !determination.operator_disabled {
                assert!(matches!(
                    structural_governance_disposition(
                        determination,
                        &admin,
                        Some(binding()),
                        manifest.manifest_version,
                    ),
                    GovernanceDisposition::Unavailable(_)
                ));
            }
        }
    }

    #[test]
    fn ambient_credentials_on_a_bound_governed_invocation_refuse_identity_ambiguity() {
        let manifest = fixture_manifest();
        let governed = classify(
            &[
                OsString::from("issue"),
                OsString::from("comment"),
                OsString::from("42"),
                OsString::from("--body"),
                OsString::from("hello"),
            ],
            &manifest,
            "macos",
        );
        let determination = RungDetermination::r2(
            1,
            R2Reason::AgentCredentialsPresent,
            Some(manifest.manifest_version),
            &test_rung_provenance(),
        );
        let binding = AgentBinding {
            repo: "cortexkit/aft".to_string(),
            agent_id: "alfonso-aft".to_string(),
        };

        assert!(matches!(
            structural_governance_disposition(
                &determination,
                &governed,
                Some(binding),
                manifest.manifest_version,
            ),
            GovernanceDisposition::Unavailable(_)
        ));
    }

    #[cfg(unix)]
    #[test]
    fn resolved_image_identity_skips_a_shim_reached_through_a_symlinked_parent() {
        use std::os::unix::fs::symlink;

        let directory = tempfile::tempdir().unwrap();
        let image = directory.path().join("aft");
        fs::write(&image, b"shim image").unwrap();
        let bin = directory.path().join("bin");
        fs::create_dir(&bin).unwrap();
        symlink(&image, bin.join("gh")).unwrap();
        let linked_parent = directory.path().join("linked-bin");
        symlink(&bin, &linked_parent).unwrap();

        assert!(same_image(&linked_parent.join("gh"), &image));
    }

    #[test]
    fn bypass_audit_is_visible_to_a_later_self_report_reader() {
        let directory = tempfile::tempdir().unwrap();
        let paths = StatePaths::from_root(directory.path().to_path_buf());
        append_bypass_audit(&paths, "issue close", Some("owner/repo"), 99).unwrap();
        let (records, error) = read_bypass_audit(&paths);
        assert!(error.is_none());
        let records = records.unwrap();
        assert_eq!(records.len(), 1);
        assert_eq!(records[0]["tuple"], "issue close");
    }

    #[test]
    fn refusal_and_self_report_codes_are_separate_closed_sets() {
        assert_eq!(RefusalCode::ALL.len(), 11);
        assert!(RefusalCode::ALL
            .iter()
            .all(|code| code.as_str().starts_with("gh_shim_")));
        assert_eq!(
            RefusalCode::GovernanceUnavailable.as_str(),
            "gh_shim_governance_unavailable"
        );
        assert_eq!(
            GOVERNANCE_UNAVAILABLE_TEXT,
            "the governance daemon is unreachable and this repository's actions are identity-governed; retry after the daemon returns"
        );
        assert_eq!(SelfReportDiagnostic::ALL.len(), 6);
        assert!(SelfReportDiagnostic::ALL
            .iter()
            .all(|code| code.as_str().starts_with("gh_shim_status_")));
        assert!(SelfReportDiagnostic::ALL
            .iter()
            .all(|code| !code.as_str().contains("stale")));
        assert_eq!(REFUSAL_EXIT_STATUS, 86);
    }

    #[test]
    fn v1_write_classification_accepts_only_the_reviewed_tuple_sets() {
        let manifest = fixture_manifest();
        for tuple in V1_GOVERNED_TUPLES {
            let args = tuple
                .split_whitespace()
                .map(OsString::from)
                .collect::<Vec<_>>();
            assert!(matches!(
                classify(&args, &manifest, "macos"),
                Classification::Governed { .. }
            ));
        }
        for tuple in V1_ADMIN_TUPLES {
            let args = tuple
                .split_whitespace()
                .map(OsString::from)
                .collect::<Vec<_>>();
            assert!(matches!(
                classify(&args, &manifest, "macos"),
                Classification::Admin { .. }
            ));
        }
        for args in [
            ["release", "publish"].as_slice(),
            ["issue", "create"].as_slice(),
            ["pr", "reopen"].as_slice(),
        ] {
            let args = args.iter().map(OsString::from).collect::<Vec<_>>();
            assert!(matches!(
                classify(&args, &manifest, "macos"),
                Classification::Unclassified
            ));
        }
    }

    #[test]
    fn field_bearing_api_forms_remain_unclassified_without_an_audited_parser() {
        let manifest = fixture_manifest();
        for field_flag in [
            "--field=name=value",
            "--raw-field=name=value",
            "--input=body.json",
            "-fname=value",
            "-Fname=value",
        ] {
            let args = vec![
                OsString::from("api"),
                OsString::from("/repos/owner/repo"),
                OsString::from(field_flag),
            ];
            assert!(matches!(
                classify(&args, &manifest, "macos"),
                Classification::Unclassified
            ));
        }
    }

    #[test]
    fn holder_refusals_preserve_any_string_code_and_reject_non_strings() {
        for code in FIXTURE_ACCEPTED_SEAM_REFUSAL_CODES {
            let response = json!({"outcome": "refusal", "refusal_code": code});
            let outcome = parse_governed_response(&serde_json::to_vec(&response).unwrap()).unwrap();
            assert!(matches!(outcome, RouteOutcome::Refusal(ref actual) if actual == code));
            assert_eq!(RefusalCode::SeamRefusal.as_str(), "gh_shim_seam_refusal");
            assert_eq!(
                seam_refusal_text(code),
                format!("governance seam refused the action: {code}")
            );
            assert_eq!(REFUSAL_EXIT_STATUS, 86);
        }
        let unknown = "quota_exhausted_v2";
        let response = json!({"outcome": "refusal", "refusal_code": unknown});
        let outcome = parse_governed_response(&serde_json::to_vec(&response).unwrap()).unwrap();
        assert!(matches!(outcome, RouteOutcome::Refusal(ref actual) if actual == unknown));
        assert_eq!(RefusalCode::SeamRefusal.as_str(), "gh_shim_seam_refusal");
        assert_eq!(
            seam_refusal_text(unknown),
            "governance seam refused the action: quota_exhausted_v2"
        );
        assert_eq!(REFUSAL_EXIT_STATUS, 86);

        for response in [
            json!({"outcome": "refusal", "refusal_code": 7}),
            json!({"outcome": "refusal", "refusal_code": null}),
            json!({"outcome": "refusal"}),
        ] {
            assert!(matches!(
                parse_governed_response(&serde_json::to_vec(&response).unwrap()),
                Err(RouteOutcome::SchemaMismatch(_))
            ));
        }
    }

    #[test]
    fn governed_self_report_transitions_are_durable_and_mechanical_classification_preserves_them() {
        let directory = tempfile::tempdir().unwrap();
        let paths = StatePaths::from_root(directory.path().to_path_buf());
        let binding = AgentBinding {
            repo: "owner/repo".to_string(),
            agent_id: "agent-7".to_string(),
        };
        write_seam_state(
            &paths,
            SeamState {
                bound_holder: None,
                agent_binding: Some(binding.clone()),
                last_seam_refusal: None,
            },
        )
        .unwrap();
        let report = build_self_report(&paths);
        assert_eq!(report.bound_holder, None);
        assert_eq!(report.agent_binding, Some(binding.clone()));
        assert_eq!(report.last_seam_refusal, None);

        write_seam_state(
            &paths,
            SeamState {
                bound_holder: Some(ROUTING_HOLDER_MODULE_ID.to_string()),
                agent_binding: Some(binding.clone()),
                last_seam_refusal: Some(LastSeamRefusal {
                    code: "rate_limited".to_string(),
                    at_unix_secs: 77,
                }),
            },
        )
        .unwrap();
        let report = build_self_report(&paths);
        assert_eq!(
            report.bound_holder.as_deref(),
            Some(ROUTING_HOLDER_MODULE_ID)
        );
        assert_eq!(report.agent_binding, Some(binding.clone()));
        assert_eq!(
            report
                .last_seam_refusal
                .as_ref()
                .map(|refusal| refusal.code.as_str()),
            Some("rate_limited")
        );

        write_seam_state(
            &paths,
            governed_seam_state(&paths, Some(ROUTING_HOLDER_MODULE_ID.to_string()), &binding),
        )
        .unwrap();
        assert_eq!(
            seam_state(&paths)
                .last_seam_refusal
                .as_ref()
                .map(|refusal| refusal.code.as_str()),
            Some("rate_limited")
        );

        let mechanical = [OsString::from("issue"), OsString::from("view")];
        assert!(matches!(
            classify(&mechanical, &fixture_manifest(), "macos"),
            Classification::Mechanical
        ));
        assert_eq!(
            seam_state(&paths)
                .last_seam_refusal
                .as_ref()
                .map(|refusal| refusal.at_unix_secs),
            Some(77)
        );
    }

    #[test]
    fn governed_self_report_persistence_failure_is_loud() {
        let directory = tempfile::tempdir().unwrap();
        let state_root = directory.path().join("not-a-directory");
        fs::write(&state_root, b"file").unwrap();
        let paths = StatePaths::from_root(state_root);
        assert!(write_seam_state(&paths, SeamState::default()).is_err());
    }

    #[test]
    fn raw_bytes_round_trip_verifies_then_parses_from_the_fixture_envelope() {
        let envelope: SignedManifest = serde_json::from_str(include_str!(
            "../tests/fixtures/gh_shim/signed-envelope-v2.json"
        ))
        .expect("signed envelope fixture");
        // The embedded bytes are exactly the published manifest file.
        assert_eq!(
            envelope.manifest_bytes,
            include_str!("../tests/fixtures/gh_shim/initial-manifest-v1.json")
        );
        // Verify the received bytes first, parse second.
        let manifest = verify_manifest_signature(&envelope).expect("fixture signature verifies");
        assert_eq!(manifest.manifest_version, 1);
        assert_eq!(manifest.issued_at_unix_secs, FIXTURE_ISSUED_AT);
        manifest.validate().expect("fixture manifest validates");
    }

    #[test]
    fn tampered_single_byte_fixture_fails_signature_verification() {
        let canonical: SignedManifest = serde_json::from_str(include_str!(
            "../tests/fixtures/gh_shim/signed-envelope-v2.json"
        ))
        .expect("canonical envelope fixture");
        let tampered: SignedManifest = serde_json::from_str(include_str!(
            "../tests/fixtures/gh_shim/signed-envelope-v2-tampered.json"
        ))
        .expect("tampered envelope fixture");
        // The tampering is exactly one substituted byte inside the signed
        // bytes; the signature is untouched.
        assert_eq!(
            canonical.manifest_bytes.len(),
            tampered.manifest_bytes.len()
        );
        assert_eq!(
            canonical
                .manifest_bytes
                .bytes()
                .zip(tampered.manifest_bytes.bytes())
                .filter(|(left, right)| left != right)
                .count(),
            1
        );
        assert_eq!(canonical.signature, tampered.signature);
        assert!(matches!(
            verify_manifest_signature(&tampered),
            Err(ManifestProblem::Invalid(_))
        ));
    }

    #[test]
    fn future_issued_at_fixture_is_refused_and_aged_fixture_serves_governed_classification() {
        let directory = tempfile::tempdir().unwrap();
        let paths = StatePaths::from_root(directory.path().to_path_buf());

        write_envelope_fixture(
            &paths,
            include_str!("../tests/fixtures/gh_shim/signed-envelope-v2-future-issued-at.json"),
        );
        match load_manifest(&paths, TEST_NOW) {
            Err(ManifestProblem::Invalid(error)) => {
                assert!(error.contains("future"), "unexpected error: {error}")
            }
            other => panic!("expected future issued_at refusal, got {other:?}"),
        }

        // This signature is valid, but its provenance timestamp is 2,000,000
        // seconds old. A ceremony-once manifest remains active, so it still
        // classifies governed commands instead of scheduling an outage.
        write_envelope_fixture(
            &paths,
            include_str!("../tests/fixtures/gh_shim/signed-envelope-v2-stale-issued-at.json"),
        );
        let ManifestResolution::Active(manifest) = resolve_manifest(&paths, TEST_NOW) else {
            panic!("expected the aged signed manifest to remain active");
        };
        assert_eq!(manifest.issued_at_unix_secs, FIXTURE_ISSUED_AT - 2_000_000);
        assert!(matches!(
            classify(
                &[
                    OsString::from("issue"),
                    OsString::from("comment"),
                    OsString::from("42"),
                    OsString::from("--body"),
                    OsString::from("hello"),
                ],
                &manifest,
                "macos"
            ),
            Classification::Governed { tuple, .. } if tuple == "issue comment"
        ));

        let report: Value =
            serde_json::from_str(&render_self_report(&paths).expect("self report serialization"))
                .expect("self report JSON");
        assert_eq!(
            report["cached_manifest"]["issued_at_unix_secs"],
            FIXTURE_ISSUED_AT - 2_000_000
        );
    }

    #[test]
    fn standby_key_fixture_verifies_under_a_two_slot_trust_set_and_unknown_key_ids_are_refused() {
        let envelope: SignedManifest = serde_json::from_str(include_str!(
            "../tests/fixtures/gh_shim/signed-envelope-v2-standby-key.json"
        ))
        .expect("standby envelope fixture");

        let standby = Ed25519KeyPair::from_seed_unchecked(&STANDBY_TEST_SEED).expect("standby key");
        assert_ne!(standby.public_key().as_ref(), DEV_MANIFEST_PUBLIC_KEY);
        let standby_public: &'static [u8] =
            Box::leak(standby.public_key().as_ref().to_vec().into_boxed_slice());
        let trust_set = [
            Some(ManifestTrustKey {
                key_id: DEV_MANIFEST_KEY_ID,
                public_key: &DEV_MANIFEST_PUBLIC_KEY,
            }),
            Some(ManifestTrustKey {
                key_id: DEV_STANDBY_MANIFEST_KEY_ID,
                public_key: standby_public,
            }),
        ];

        // A standby-signed manifest is accepted under the two-slot set.
        let manifest =
            verify_manifest_signature_with(&envelope, &trust_set).expect("standby slot verifies");
        assert_eq!(
            manifest.manifest_version,
            fixture_manifest().manifest_version
        );

        // A third, unknown key id is refused by the same set.
        let mut unknown = envelope.clone();
        unknown.key_id = "gh-routing-unknown-key".to_string();
        assert!(matches!(
            verify_manifest_signature_with(&unknown, &trust_set),
            Err(ManifestProblem::Invalid(_))
        ));
    }

    #[test]
    fn compiled_trust_set_shape_matches_the_two_slot_design() {
        let slots = compiled_manifest_trust_set();
        // Every profile trusts the production root minted in the 2026-08-27
        // CKCRED ceremony (`signing:gh-manifest-root:1`); the bytes here are
        // the published public half, re-asserted so a trust-slot edit cannot
        // silently swap the live key.
        let live = slots[0].expect("live slot carries the production root");
        assert_eq!(live.key_id, PROD_MANIFEST_KEY_ID);
        assert_eq!(live.public_key, &PROD_MANIFEST_PUBLIC_KEY);
        #[cfg(debug_assertions)]
        {
            // Debug images verify both eras: prod live + the dev test key so
            // fixtures exercise R3 without a custody round-trip.
            assert_eq!(slots.len(), 2);
            assert_eq!(slots[1].unwrap().key_id, DEV_MANIFEST_KEY_ID);
        }
        #[cfg(not(debug_assertions))]
        {
            // The release set keeps two slots: prod live + a cold standby that
            // stays empty until a future custody release fills it.
            assert_eq!(slots.len(), 2);
            assert!(slots[1].is_none());
        }
    }

    #[test]
    fn envelope_v1_shapes_are_refused_by_the_v2_verifier() {
        let directory = tempfile::tempdir().unwrap();
        let paths = StatePaths::from_root(directory.path().to_path_buf());
        let manifest = fixture_manifest();
        let bytes = serde_json::to_vec(&manifest).unwrap();
        let key = Ed25519KeyPair::from_seed_unchecked(&TEST_SEED).unwrap();
        let signature = base64::engine::general_purpose::STANDARD.encode(key.sign(&bytes).as_ref());

        // The pre-v2 shape carried the parsed manifest object in the envelope.
        let v1_object = json!({
            "artifact_id": MANIFEST_ARTIFACT_ID,
            "key_id": DEV_MANIFEST_KEY_ID,
            "fetched_at_unix_secs": TEST_NOW,
            "signature": signature,
            "manifest": serde_json::to_value(&manifest).unwrap(),
        });
        fs::write(&paths.manifest, serde_json::to_vec(&v1_object).unwrap()).unwrap();
        assert!(matches!(
            load_manifest(&paths, TEST_NOW),
            Err(ManifestProblem::Invalid(_))
        ));

        // An envelope naming an older version is refused even with raw bytes.
        let mut old_version = signed(&manifest, TEST_NOW);
        old_version.envelope_version = 1;
        fs::write(&paths.manifest, serde_json::to_vec(&old_version).unwrap()).unwrap();
        match load_manifest(&paths, TEST_NOW) {
            Err(ManifestProblem::Invalid(error)) => {
                assert!(
                    error.contains("envelope version"),
                    "unexpected error: {error}"
                )
            }
            other => panic!("expected envelope version refusal, got {other:?}"),
        }
    }

    #[test]
    fn dormant_resolution_is_presence_based() {
        let directory = tempfile::tempdir().unwrap();
        let paths = StatePaths::from_root(directory.path().to_path_buf());
        // No artifact on disk: dormant.
        assert!(matches!(
            resolve_manifest(&paths, TEST_NOW),
            ManifestResolution::Dormant
        ));

        // A failing artifact with no last-valid cache falls back without a
        // regressed classification, but remains distinguishable from a missing
        // public-install manifest so the invocation can announce the fallback.
        let untrusted = signed_with(
            &fixture_manifest(),
            TEST_NOW,
            &STANDBY_TEST_SEED,
            "gh-routing-unknown-key",
        );
        fs::write(&paths.manifest, serde_json::to_vec(&untrusted).unwrap()).unwrap();
        assert!(matches!(
            resolve_manifest(&paths, TEST_NOW),
            ManifestResolution::Invalid(ManifestProblem::Invalid(_))
        ));
    }

    #[test]
    fn regressed_invalid_artifact_refuses_governed_and_admin_and_passes_mechanical() {
        let directory = tempfile::tempdir().unwrap();
        let paths = StatePaths::from_root(directory.path().to_path_buf());
        let now = TEST_NOW;

        // Accept the canonical manifest; this writes the last-valid cache.
        write_signed_manifest(&paths, fixture_manifest(), now);
        load_manifest(&paths, now).expect("canonical manifest verifies");

        // Break the installed artifact: signed bytes tampered after signing.
        write_envelope_fixture(
            &paths,
            include_str!("../tests/fixtures/gh_shim/signed-envelope-v2-tampered.json"),
        );

        // A failed validation immediately enters the regressed arm; time passing
        // does not participate in manifest validity.
        let ManifestResolution::Regressed { manifest, problem } = resolve_manifest(&paths, now)
        else {
            panic!("expected the regressed arm");
        };
        let governed = [
            OsString::from("issue"),
            OsString::from("comment"),
            OsString::from("42"),
            OsString::from("--body"),
            OsString::from("hello"),
        ];
        assert!(matches!(
            regressed_disposition(&governed, &manifest, "macos", &problem),
            RegressedDisposition::Refuse {
                code: RefusalCode::ManifestRegressed,
                ..
            }
        ));
        let admin = [
            OsString::from("pr"),
            OsString::from("merge"),
            OsString::from("1"),
        ];
        assert!(matches!(
            regressed_disposition(&admin, &manifest, "macos", &problem),
            RegressedDisposition::Refuse {
                code: RefusalCode::ManifestRegressed,
                ..
            }
        ));
        let mechanical = [OsString::from("issue"), OsString::from("view")];
        assert!(matches!(
            regressed_disposition(&mechanical, &manifest, "macos", &problem),
            RegressedDisposition::Passthrough
        ));
        let undeclared = [OsString::from("alias"), OsString::from("set")];
        assert!(matches!(
            regressed_disposition(&undeclared, &manifest, "macos", &problem),
            RegressedDisposition::Refuse {
                code: RefusalCode::Unclassified,
                ..
            }
        ));

        // The self report is loud about the regressed validation failure.
        let report = cached_manifest_report_at(&paths, now);
        assert_eq!(report.state, Some("regressed"));
        assert_eq!(report.version, Some(1));
        assert_eq!(report.issued_at_unix_secs, Some(FIXTURE_ISSUED_AT));
        assert_eq!(
            report.diagnostics,
            vec![
                SelfReportDiagnostic::ManifestRegressed.as_str(),
                SelfReportDiagnostic::ManifestInvalid.as_str(),
            ]
        );
    }

    #[test]
    fn self_report_exposes_manifest_and_rung_record_provenance() {
        let directory = tempfile::tempdir().unwrap();
        let paths = StatePaths::from_root(directory.path().to_path_buf());
        write_signed_manifest(&paths, fixture_manifest(), TEST_NOW);

        let manifest_report = cached_manifest_report_at(&paths, TEST_NOW);
        assert_eq!(manifest_report.state, Some("valid"));
        assert_eq!(
            manifest_report.verified_by_key_id.as_deref(),
            Some(DEV_MANIFEST_KEY_ID)
        );
        assert_eq!(
            manifest_report.compiled_trust_set_key_ids,
            trust_set_key_ids(compiled_manifest_trust_set())
        );

        let provenance = RungRecordProvenance {
            image_path: "/opt/cortexkit/aft-gh-shim".to_string(),
            version: "0.53.0-test".to_string(),
            repo_key: "cortexkit/aft".to_string(),
        };
        let determination =
            RungDetermination::r2(TEST_NOW, R2Reason::DaemonUnreachable, Some(1), &provenance);
        write_rung_record_silently(&paths, &determination.record);
        let fresh_rung = last_rung_report(&paths);
        assert_eq!(
            fresh_rung.recorded_by_image_path.as_deref(),
            Some("/opt/cortexkit/aft-gh-shim")
        );
        assert_eq!(
            fresh_rung.recorded_by_version.as_deref(),
            Some("0.53.0-test")
        );
        assert_eq!(
            fresh_rung.recorded_by_repo_key.as_deref(),
            Some("cortexkit/aft")
        );

        fs::write(
            &paths.rung,
            serde_json::to_vec(&json!({
                "rung": "R2",
                "as_of_unix_secs": TEST_NOW,
                "inputs": { "daemon_unreachable": "failed" },
                "manifest_version": 1
            }))
            .unwrap(),
        )
        .unwrap();
        let legacy_rung = last_rung_report(&paths);
        assert_eq!(
            legacy_rung.recorded_by_image_path.as_deref(),
            Some(PRE_PROVENANCE_RECORD)
        );
        assert_eq!(
            legacy_rung.recorded_by_version.as_deref(),
            Some(PRE_PROVENANCE_RECORD)
        );
        assert_eq!(
            legacy_rung.recorded_by_repo_key.as_deref(),
            Some(PRE_PROVENANCE_RECORD)
        );
    }

    #[test]
    fn trust_set_provenance_explains_image_level_untrusted_key_regression() {
        let directory = tempfile::tempdir().unwrap();
        let paths = StatePaths::from_root(directory.path().to_path_buf());
        write_signed_manifest(&paths, fixture_manifest(), TEST_NOW);
        let verifier_a = [Some(ManifestTrustKey {
            key_id: DEV_MANIFEST_KEY_ID,
            public_key: &DEV_MANIFEST_PUBLIC_KEY,
        })];
        let verifier_b = [Some(PROD_MANIFEST_TRUST_KEY)];

        let report_a = cached_manifest_report_at_with(&paths, TEST_NOW, &verifier_a);
        assert_eq!(report_a.state, Some("valid"));
        assert_eq!(
            report_a.verified_by_key_id.as_deref(),
            Some(DEV_MANIFEST_KEY_ID)
        );
        assert_eq!(
            report_a.compiled_trust_set_key_ids,
            vec![DEV_MANIFEST_KEY_ID]
        );

        let report_b = cached_manifest_report_at_with(&paths, TEST_NOW, &verifier_b);
        assert_eq!(report_b.state, Some("regressed"));
        assert_eq!(report_b.verified_by_key_id, None);
        assert_eq!(
            report_b.compiled_trust_set_key_ids,
            vec![PROD_MANIFEST_KEY_ID]
        );
        assert_eq!(
            report_b.diagnostic_guidance,
            Some(UNTRUSTED_MANIFEST_KEY_STEERING)
        );

        let cached = read_last_valid_manifest(&paths).expect("verifier A wrote last-valid cache");
        let governed = [
            OsString::from("issue"),
            OsString::from("comment"),
            OsString::from("42"),
            OsString::from("--body"),
            OsString::from("hello"),
        ];
        let untrusted =
            ManifestProblem::Invalid(format!("untrusted manifest key id {DEV_MANIFEST_KEY_ID}"));
        let RegressedDisposition::Refuse { text, .. } =
            regressed_disposition(&governed, &cached.manifest, "macos", &untrusted)
        else {
            panic!("a governed command must refuse under verifier B");
        };
        assert!(text.ends_with(UNTRUSTED_MANIFEST_KEY_STEERING));
    }

    #[test]
    fn version_high_water_refuses_rollbacks_and_status_reports_them() {
        let directory = tempfile::tempdir().unwrap();
        let paths = StatePaths::from_root(directory.path().to_path_buf());

        // Accept the newer manifest first.
        write_envelope_fixture(
            &paths,
            include_str!("../tests/fixtures/gh_shim/signed-envelope-v2-version-2.json"),
        );
        assert_eq!(load_manifest(&paths, TEST_NOW).unwrap().manifest_version, 2);
        assert_eq!(version_high_water(&paths), 2);

        // A validly-signed OLDER manifest is then refused as a rollback
        // incident, never as ordinary out-of-order arrival.
        write_envelope_fixture(
            &paths,
            include_str!("../tests/fixtures/gh_shim/signed-envelope-v2.json"),
        );
        assert!(matches!(
            load_manifest(&paths, TEST_NOW),
            Err(ManifestProblem::RolledBack {
                manifest_version: 1,
                newest_accepted: 2,
            })
        ));
        let report = cached_manifest_report_at(&paths, TEST_NOW);
        assert_eq!(
            report.diagnostics,
            vec![
                SelfReportDiagnostic::ManifestRegressed.as_str(),
                SelfReportDiagnostic::ManifestRollback.as_str(),
            ]
        );
        // That rollback is also visible through the --status document.
        let document = render_self_report(&paths).expect("self report");
        assert!(document.contains(SelfReportDiagnostic::ManifestRollback.as_str()));

        // Re-presenting the newest accepted version is not a rollback.
        write_envelope_fixture(
            &paths,
            include_str!("../tests/fixtures/gh_shim/signed-envelope-v2-version-2.json"),
        );
        assert_eq!(load_manifest(&paths, TEST_NOW).unwrap().manifest_version, 2);
    }

    fn fixture_dir() -> PathBuf {
        PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/gh_shim")
    }

    fn canonical_manifest_bytes() -> Vec<u8> {
        fs::read(fixture_dir().join("initial-manifest-v1.json"))
            .expect("canonical manifest fixture")
    }

    fn envelope_json(envelope: &SignedManifest) -> Vec<u8> {
        let mut bytes = serde_json::to_vec_pretty(envelope).expect("envelope serialization");
        bytes.push(b'\n');
        bytes
    }

    /// Deterministic generator for every dev-signed envelope fixture. The
    /// canonical fixture's signature covers the exact bytes of the checked-in
    /// manifest file; variant fixtures re-sign their serialized variant bytes.
    fn generate_envelope_fixtures() -> Vec<(String, Vec<u8>)> {
        let sign = |bytes: &[u8], seed: &[u8; 32]| {
            let key = Ed25519KeyPair::from_seed_unchecked(seed).expect("fixture key");
            base64::engine::general_purpose::STANDARD.encode(key.sign(bytes).as_ref())
        };
        let envelope = |key_id: &str, seed: &[u8; 32], manifest_bytes: String| {
            envelope_json(&SignedManifest {
                artifact_id: MANIFEST_ARTIFACT_ID.to_string(),
                envelope_version: ENVELOPE_VERSION,
                key_id: key_id.to_string(),
                fetched_at_unix_secs: FIXTURE_ISSUED_AT,
                signature: sign(manifest_bytes.as_bytes(), seed),
                manifest_bytes,
            })
        };

        let canonical = canonical_manifest_bytes();
        let canonical_text = String::from_utf8(canonical.clone()).expect("UTF-8 manifest");
        let canonical_signature = sign(&canonical, &TEST_SEED);

        let mut fixtures = Vec::new();
        // Raw-bytes round-trip golden: signature over the published file.
        fixtures.push((
            "signed-envelope-v2.json".to_string(),
            envelope_json(&SignedManifest {
                artifact_id: MANIFEST_ARTIFACT_ID.to_string(),
                envelope_version: ENVELOPE_VERSION,
                key_id: DEV_MANIFEST_KEY_ID.to_string(),
                fetched_at_unix_secs: FIXTURE_ISSUED_AT,
                signature: canonical_signature.clone(),
                manifest_bytes: canonical_text.clone(),
            }),
        ));
        // Tampered-single-byte case: one substitution inside the signed bytes,
        // keeping the ORIGINAL signature so verification must fail.
        let tampered = canonical_text.replacen("issue view", "issue View", 1);
        assert_ne!(tampered, canonical_text);
        fixtures.push((
            "signed-envelope-v2-tampered.json".to_string(),
            envelope_json(&SignedManifest {
                artifact_id: MANIFEST_ARTIFACT_ID.to_string(),
                envelope_version: ENVELOPE_VERSION,
                key_id: DEV_MANIFEST_KEY_ID.to_string(),
                fetched_at_unix_secs: FIXTURE_ISSUED_AT,
                signature: canonical_signature,
                manifest_bytes: tampered,
            }),
        ));

        let mut variant = |name: &str, mutate: fn(&mut Manifest), seed: &[u8; 32], key_id: &str| {
            let mut manifest = fixture_manifest();
            mutate(&mut manifest);
            let bytes = serde_json::to_vec(&manifest).expect("variant manifest bytes");
            fixtures.push((
                name.to_string(),
                envelope(
                    key_id,
                    seed,
                    String::from_utf8(bytes).expect("UTF-8 variant bytes"),
                ),
            ));
        };
        variant(
            "signed-envelope-v2-future-issued-at.json",
            |manifest| {
                manifest.issued_at_unix_secs =
                    FIXTURE_ISSUED_AT + ISSUED_AT_FUTURE_SKEW.as_secs() + 3300;
            },
            &TEST_SEED,
            DEV_MANIFEST_KEY_ID,
        );
        variant(
            "signed-envelope-v2-stale-issued-at.json",
            |manifest| {
                manifest.issued_at_unix_secs = FIXTURE_ISSUED_AT - 2_000_000;
            },
            &TEST_SEED,
            DEV_MANIFEST_KEY_ID,
        );
        variant(
            "signed-envelope-v2-version-2.json",
            |manifest| {
                manifest.manifest_version = 2;
            },
            &TEST_SEED,
            DEV_MANIFEST_KEY_ID,
        );
        variant(
            "signed-envelope-v2-standby-key.json",
            |_manifest| {},
            &STANDBY_TEST_SEED,
            DEV_STANDBY_MANIFEST_KEY_ID,
        );
        fixtures
    }

    #[test]
    fn signed_envelope_fixtures_match_their_generator() {
        let regen = std::env::var_os("AFT_GH_SHIM_REGEN").is_some();
        for (name, bytes) in generate_envelope_fixtures() {
            let path = fixture_dir().join(&name);
            if regen {
                fs::write(&path, &bytes).expect("write fixture");
                continue;
            }
            let disk = fs::read(&path)
                .unwrap_or_else(|error| panic!("fixture {name} is missing: {error}"));
            assert_eq!(
                disk, bytes,
                "fixture {name} drifted from its generator; rerun with AFT_GH_SHIM_REGEN=1"
            );
        }
    }
}