agent-first-data 0.27.0

A naming convention that lets AI agents understand your data without being told what it means, plus a CLI and library for reading and safely editing structured JSON, TOML, YAML, dotenv, and INI documents.
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
#![allow(clippy::print_stdout, clippy::print_stderr)]

use agent_first_data::document::{
    BareOverwrite, Document, DocumentError, DocumentFile, Format as DocumentFormat,
    Value as DocumentValue, ValueType, get_path, guard_bare_overwrite, join_path, parse_path,
    value_from_type,
};
use agent_first_data::{
    ArgSpec, CliOutcome, CliSpec, Combination, CommandSpec, ErrorBuilder, Event, OutputFormat,
    OutputOptions, OutputPlan, OutputSpec, OutputTo, PlainStyle, Redactor, ResolvedInvocation,
    build_afdata_cli, build_cli_error, cli_error_event, cli_help_event, cli_parse_output,
    cli_version_event, is_valid_bcp47, is_valid_rfc3339, is_valid_rfc3339_date,
    is_valid_rfc3339_time, json_error, json_log, json_result, normalize_utc_offset, render,
    render_cli_reference, validate_protocol_event, validate_protocol_stream,
};
use serde_json::{Value, json};
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::process::ExitCode;

const MAX_SAFE_INTEGER: u64 = 9_007_199_254_740_991;

enum EmitCommand {
    /// Emit a diagnostic log event (stderr under the default split)
    Log {
        /// Log level: debug, info, warn, or error
        level: String,
        /// Human-readable message; do not place secrets in free text
        message: String,
    },
    /// Emit a terminal result event (stdout under the default split)
    Result {
        /// Human-readable result message
        message: String,
    },
    /// Emit a terminal error event and exit with status 1
    Error {
        /// Stable machine-readable error code
        code: String,
        /// Human-readable error message
        message: String,
        /// Suggested corrective action
        hint: Option<String>,
        /// Mark the failure as safe to retry
        retryable: bool,
    },
}

enum ShellCommand {
    /// Print the sourceable Bash authoring kit as raw Bash on stdout
    Bash,
}

#[cfg(feature = "skill")]
enum SkillCommand {
    /// Validate a SKILL.md file or skill directory against the Agent Skills spec
    Validate {
        /// SKILL.md file or skill directory, or `-` for SKILL.md text on stdin
        input: PathBuf,
    },
    /// Report whether the bundled Agent Skill is installed for each target agent
    #[cfg(feature = "skill-admin")]
    Status {
        /// Agent target: all, codex, claude-code, opencode, or hermes
        agent: String,
        /// Skill scope: personal or workspace
        scope: String,
        /// Explicit skills directory; requires a single concrete --agent
        skills_dir: Option<String>,
    },
    /// Install the bundled Agent Skill for each target agent
    #[cfg(feature = "skill-admin")]
    Install {
        /// Agent target: all, codex, claude-code, opencode, or hermes
        agent: String,
        /// Skill scope: personal or workspace
        scope: String,
        /// Explicit skills directory; requires a single concrete --agent
        skills_dir: Option<String>,
        /// Overwrite a skill this tool did not manage
        force: bool,
    },
    /// Uninstall the bundled Agent Skill for each target agent
    #[cfg(feature = "skill-admin")]
    Uninstall {
        /// Agent target: all, codex, claude-code, opencode, or hermes
        agent: String,
        /// Skill scope: personal or workspace
        scope: String,
        /// Explicit skills directory; requires a single concrete --agent
        skills_dir: Option<String>,
        /// Remove a skill this tool did not manage
        force: bool,
    },
}

#[derive(Clone, Debug)]
struct Finding {
    rule_id: &'static str,
    severity: &'static str,
    pointer: String,
    message: String,
}

impl Finding {
    fn error(rule_id: &'static str, pointer: String, message: String) -> Self {
        Self {
            rule_id,
            severity: "error",
            pointer,
            message,
        }
    }

    /// A finding the tool is not certain about. Convention-adoption checks are
    /// heuristic — they read intent from a field's name — so they must not fail
    /// a document the way a violated suffix rule does. They still have to be
    /// said: staying silent about them is what let `lint` pass a file made
    /// entirely of the ambiguities this convention exists to remove.
    fn warning(rule_id: &'static str, pointer: String, message: String) -> Self {
        Self {
            rule_id,
            severity: "warning",
            pointer,
            message,
        }
    }

    fn to_json(&self) -> Value {
        json!({
            "rule_id": self.rule_id,
            "severity": self.severity,
            "pointer": self.pointer,
            "message": self.message,
        })
    }
}

enum ParsedInput {
    Single(Value),
    Lines(Vec<Value>),
}

struct ParseError {
    code: &'static str,
    message: String,
    hint: Option<String>,
    line: Option<usize>,
}

fn protocol_output() -> OutputSpec {
    OutputSpec::protocol_finite(
        ["json", "yaml", "plain"],
        ["split", "stdout", "stderr"],
        "json",
        "split",
    )
    .file_sinks(stream_file_sinks())
}

fn raw_output() -> OutputSpec {
    OutputSpec::raw().file_sinks(stream_file_sinks())
}

fn stream_file_sinks() -> Vec<&'static str> {
    #[cfg(feature = "stream-redirect")]
    {
        vec!["stdout", "stderr"]
    }
    #[cfg(not(feature = "stream-redirect"))]
    {
        Vec::new()
    }
}

fn positional(id: &str, index: usize, value_name: &str, about: &str) -> ArgSpec {
    ArgSpec::positional(id, index, value_name).about(about)
}

fn input_format_arg() -> ArgSpec {
    ArgSpec::option_enum(
        "--input-format",
        [
            "json",
            "toml",
            "yaml",
            "yml",
            "dotenv",
            "env",
            "ini",
            "toml-frontmatter",
            "yaml-frontmatter",
        ],
    )
    .value_name("FORMAT")
    .about("Document format override")
}

fn secret_name_arg() -> ArgSpec {
    ArgSpec::option("--secret-name", "FIELD")
        .repeatable()
        .about("Extra exact field name to redact")
}

fn afdata_cli_spec() -> Result<agent_first_data::BuiltCliSpec, agent_first_data::CliSpecError> {
    let protocol = protocol_output();
    let raw = raw_output();
    let mut spec = CliSpec::new("afdata", env!("CARGO_PKG_VERSION"))
        .about(env!("CARGO_PKG_DESCRIPTION"))
        .display_name(env!("DISPLAY_NAME"))
        // build.rs writes "unknown" when no git tree is reachable (a crates.io
        // tarball); an unknown build is no build.
        .build_id(match env!("GIT_SHA") {
            "unknown" => "",
            sha => sha,
        })
        .lifecycle_output(protocol.clone())
        .command(CommandSpec::root())
        .command(
            CommandSpec::new(["lint"])
                .about("Lint structured data for deterministic AFDATA issues")
                .arg(positional(
                    "input",
                    0,
                    "INPUT",
                    "Input file, or - for stdin",
                ))
                .arg(input_format_arg())
                .arg(
                    ArgSpec::option_enum("--min-severity", ["warning", "error"])
                        .value_name("SEVERITY")
                        .default("warning")
                        .about("Lowest severity to report; `error` drops the heuristic checks"),
                )
                .combination(
                    Combination::new("lint")
                        .action("lint")
                        .required(["input"])
                        .optional(["input_format", "min_severity"])
                        .output(protocol.clone()),
                ),
        )
        .command(
            CommandSpec::new(["validate"])
                .about("Validate protocol-v1 events or a finite event stream")
                .arg(positional(
                    "input",
                    0,
                    "INPUT",
                    "Input file, or - for stdin",
                ))
                .arg(ArgSpec::flag("--strict").about("Enforce the strict protocol profile"))
                .arg(
                    ArgSpec::flag("--per-event")
                        .about("Validate values independently without stream lifecycle rules"),
                )
                .combination(
                    Combination::new("validate")
                        .action("validate")
                        .required(["input"])
                        .optional(["strict", "per_event"])
                        .output(protocol.clone()),
                ),
        )
        .command(
            CommandSpec::new(["render"])
                .about("Render JSON or JSONL through AFDATA redaction and formatting")
                .arg(positional(
                    "input",
                    0,
                    "INPUT",
                    "Input file, or - for stdin",
                ))
                .arg(secret_name_arg())
                .combination(
                    Combination::new("render")
                        .action("render")
                        .required(["input"])
                        .optional(["secret_name"])
                        .output(protocol.clone()),
                ),
        )
        .command(CommandSpec::new(["emit"]).about("Emit one AFDATA event"))
        .command(
            CommandSpec::new(["emit", "log"])
                .about("Emit a diagnostic log event")
                .arg(
                    ArgSpec::positional_enum(
                        "level",
                        0,
                        "LEVEL",
                        ["debug", "info", "warn", "error"],
                    )
                    .about("debug, info, warn, or error"),
                )
                .arg(positional(
                    "message",
                    1,
                    "MESSAGE",
                    "Human-readable message",
                ))
                .combination(
                    Combination::new("emit-log")
                        .action("emit_log")
                        .required(["level", "message"])
                        .output(protocol.clone()),
                ),
        )
        .command(
            CommandSpec::new(["emit", "result"])
                .about("Emit a terminal result event")
                .arg(positional("message", 0, "MESSAGE", "Result message"))
                .combination(
                    Combination::new("emit-result")
                        .action("emit_result")
                        .required(["message"])
                        .output(protocol.clone()),
                ),
        )
        .command(
            CommandSpec::new(["emit", "error"])
                .about("Emit a terminal error event")
                .arg(positional("code", 0, "CODE", "Stable error code"))
                .arg(positional("message", 1, "MESSAGE", "Error message"))
                .arg(ArgSpec::option("--hint", "HINT").about("Suggested corrective action"))
                .arg(ArgSpec::flag("--retryable").about("Mark the failure safe to retry"))
                .combination(
                    Combination::new("emit-error")
                        .action("emit_error")
                        .required(["code", "message"])
                        .optional(["hint", "retryable"])
                        .output(protocol.clone()),
                ),
        )
        .command(CommandSpec::new(["shell"]).about("Export a shell authoring kit"))
        .command(
            CommandSpec::new(["shell", "bash"])
                .about("Print the sourceable Bash authoring kit")
                .combination(
                    Combination::new("shell-bash")
                        .action("shell_bash")
                        .output(raw.clone()),
                ),
        )
        .command(
            CommandSpec::new(["get"])
                .about("Read a document or one value as an AFDATA result")
                .arg(positional(
                    "file",
                    0,
                    "FILE",
                    "Document file, or - for stdin",
                ))
                .arg(positional("key", 1, "KEY", "Optional dot-path"))
                .arg(input_format_arg())
                .arg(secret_name_arg())
                .combination(
                    Combination::new("get")
                        .action("get")
                        .required(["file"])
                        .optional(["key", "input_format", "secret_name"])
                        .output(protocol.clone()),
                ),
        )
        .command(
            CommandSpec::new(["value"])
                .about("Read one scalar as raw stdout bytes")
                .arg(positional(
                    "file",
                    0,
                    "FILE",
                    "Document file, or - for stdin",
                ))
                .arg(positional("key", 1, "KEY", "Dot-path to one scalar"))
                .arg(ArgSpec::flag("--reveal-secret").about("Allow a secret-named leaf"))
                .arg(ArgSpec::option("--default", "VALUE").about("Fallback for missing or null"))
                .arg(input_format_arg())
                .arg(secret_name_arg())
                .combination(
                    Combination::new("value")
                        .action("value")
                        .required(["file", "key"])
                        .optional(["reveal_secret", "default", "input_format", "secret_name"])
                        .output(raw.clone()),
                ),
        )
        .command(enumerate_command(
            "paths",
            "paths",
            "List each child's full dot-path as raw lines",
            &raw,
        ))
        .command(enumerate_command(
            "keys",
            "keys",
            "List child names as raw lines, without their parent path",
            &raw,
        ))
        .command(
            CommandSpec::new(["set"])
                .about("Set a value at a dot-path, creating missing object parents")
                .arg(positional("file", 0, "FILE", "Document file to mutate"))
                .arg(positional("key", 1, "KEY", "Dot-path to set"))
                .arg(positional("value", 2, "VALUE", "Value to write"))
                .arg(
                    ArgSpec::option_enum(
                        "--value-type",
                        ["string", "number", "bool", "null", "json"],
                    )
                    .value_name("TYPE")
                    .default("string")
                    .about("Exact VALUE type"),
                )
                .arg(
                    ArgSpec::option("--secret-from", "SOURCE")
                        .about("Read a secret string from stdin, prompt, fd:N, or env:VAR"),
                )
                .arg(input_format_arg())
                .combination(
                    Combination::new("set-value")
                        .action("set")
                        .about("Set one typed scalar or JSON value")
                        .fixed_one_of("value_type", ["string", "number", "bool", "json"])
                        .required(["file", "key", "value"])
                        .optional(["input_format"])
                        .output(protocol.clone()),
                )
                .combination(
                    Combination::new("set-null")
                        .action("set")
                        .about("Set the key to null; takes no VALUE")
                        .fixed("value_type", "null")
                        .required(["file", "key"])
                        .optional(["input_format"])
                        .output(protocol.clone()),
                )
                .combination(
                    Combination::new("set-secret")
                        .action("set")
                        .about("Set the key from a secret source, never from argv")
                        .required(["file", "key", "secret_from"])
                        .optional(["input_format"])
                        .output(protocol.clone()),
                ),
        )
        .command(
            CommandSpec::new(["unset"])
                .about("Remove one document entry")
                .arg(positional("file", 0, "FILE", "Document file to mutate"))
                .arg(positional("key", 1, "KEY", "Dot-path to remove"))
                .arg(input_format_arg())
                .combination(
                    Combination::new("unset")
                        .action("unset")
                        .required(["file", "key"])
                        .optional(["input_format"])
                        .output(protocol.clone()),
                ),
        )
        .command(
            CommandSpec::new(["add"])
                .about("Add an element to a keyed list")
                .arg(positional("file", 0, "FILE", "Document file to mutate"))
                .arg(positional("key", 1, "KEY", "Dot-path to the keyed list"))
                .arg(positional("slug", 2, "SLUG", "New element slug"))
                .arg(
                    ArgSpec::positional("fields", 3, "FIELD=VALUE")
                        .repeatable()
                        .about("Additional string fields"),
                )
                .arg(
                    ArgSpec::option("--slug-field", "FIELD")
                        .about("Field that identifies each list element"),
                )
                .arg(input_format_arg())
                .combination(
                    Combination::new("add")
                        .action("add")
                        .required(["file", "key", "slug", "slug_field"])
                        .optional(["fields", "input_format"])
                        .output(protocol.clone()),
                ),
        )
        .command(
            CommandSpec::new(["remove"])
                .about("Remove a keyed-list element by slug")
                .arg(positional("file", 0, "FILE", "Document file to mutate"))
                .arg(positional("key", 1, "KEY", "Dot-path to the keyed list"))
                .arg(positional("slug", 2, "SLUG", "Element slug"))
                .arg(
                    ArgSpec::option("--slug-field", "FIELD")
                        .about("Field that identifies each list element"),
                )
                .arg(input_format_arg())
                .combination(
                    Combination::new("remove")
                        .action("remove")
                        .required(["file", "key", "slug", "slug_field"])
                        .optional(["input_format"])
                        .output(protocol.clone()),
                ),
        );

    #[cfg(feature = "skill")]
    {
        spec = spec
            .command(CommandSpec::new(["skill"]).about("Validate or manage the bundled skill"))
            .command(
                CommandSpec::new(["skill", "validate"])
                    .about("Validate an Agent Skill")
                    .arg(positional(
                        "input",
                        0,
                        "INPUT",
                        "SKILL.md file, directory, or - for stdin",
                    ))
                    .combination(
                        Combination::new("skill-validate")
                            .action("skill_validate")
                            .required(["input"])
                            .output(protocol.clone()),
                    ),
            );
    }

    #[cfg(feature = "skill-admin")]
    {
        spec = spec
            .command(skill_admin_command(
                "status",
                "skill_status",
                false,
                &protocol,
            ))
            .command(skill_admin_command(
                "install",
                "skill_install",
                true,
                &protocol,
            ))
            .command(skill_admin_command(
                "uninstall",
                "skill_uninstall",
                true,
                &protocol,
            ));
    }

    build_afdata_cli(spec)
}

fn enumerate_command(name: &str, action: &str, about: &str, output: &OutputSpec) -> CommandSpec {
    CommandSpec::new([name])
        .about(about)
        .arg(positional(
            "file",
            0,
            "FILE",
            "Document file, or - for stdin",
        ))
        .arg(positional("key", 1, "KEY", "Optional container dot-path"))
        .arg(input_format_arg())
        .arg(ArgSpec::flag("--missing-ok").about("Succeed with no output when KEY is absent"))
        .arg(ArgSpec::flag("--null").about("Use NUL separators"))
        .combination(
            Combination::new(name)
                .action(action)
                .required(["file"])
                .optional(["key", "input_format", "missing_ok", "null"])
                .output(output.clone()),
        )
}

#[cfg(feature = "skill-admin")]
fn skill_admin_command(
    command: &str,
    action: &str,
    force: bool,
    output: &OutputSpec,
) -> CommandSpec {
    let mut spec = CommandSpec::new(["skill", command])
        .about(match action {
            "skill_status" => "Report whether the bundled Agent Skill is installed and current",
            "skill_install" => "Install the bundled Agent Skill",
            _ => "Remove an afdata-managed Agent Skill",
        })
        .arg(
            ArgSpec::option_enum(
                "--agent",
                ["all", "codex", "claude-code", "opencode", "hermes"],
            )
            .value_name("AGENT")
            .default("all")
            .about("Target agent"),
        )
        .arg(
            ArgSpec::option_enum("--scope", ["personal", "workspace"])
                .value_name("SCOPE")
                .default("personal")
                .about("Skill scope"),
        )
        .arg(
            ArgSpec::option("--skills-dir", "PATH")
                .about("Explicit directory; only valid with one concrete agent"),
        );
    if force {
        spec = spec.arg(ArgSpec::flag("--force").about("Overwrite or remove an unmanaged skill"));
    }
    // The two shapes differ only in which agents they target, so each must say
    // so: the command's own description cannot distinguish them.
    let verb = match action {
        "skill_status" => "Report on",
        "skill_install" => "Install into",
        _ => "Remove from",
    };
    let mut common_optional = vec!["scope"];
    let mut concrete_optional = vec!["scope", "skills_dir"];
    if force {
        common_optional.push("force");
        concrete_optional.push("force");
    }
    spec.combination(
        Combination::new(format!("skill-{command}-all"))
            .action(action)
            .about(format!("{verb} every agent that supports the scope"))
            .fixed("agent", "all")
            .optional(common_optional)
            .output(output.clone()),
    )
    .combination(
        Combination::new(format!("skill-{command}-agent"))
            .action(action)
            .about(format!(
                "{verb} one named agent; only this shape accepts --skills-dir"
            ))
            .fixed_one_of("agent", ["codex", "claude-code", "opencode", "hermes"])
            .optional(concrete_optional)
            .output(output.clone()),
    )
}

fn main() -> ExitCode {
    closed_world_main()
}

type AfdataActionHandler = fn(&ResolvedInvocation) -> ExitCode;

fn closed_world_main() -> ExitCode {
    let cli = match afdata_cli_spec() {
        Ok(cli) => cli,
        Err(error) => {
            let event = build_error_event(
                json_error("cli_spec_invalid", &error.to_string())
                    .hint("fix the built-in afdata cli-spec-v1 registry"),
            );
            return emit_event(event, OutputFormat::Json, 1);
        }
    };
    let mut handlers: Vec<(&str, AfdataActionHandler)> = vec![
        ("lint", dispatch_invocation),
        ("validate", dispatch_invocation),
        ("render", dispatch_invocation),
        ("emit_log", dispatch_invocation),
        ("emit_result", dispatch_invocation),
        ("emit_error", dispatch_invocation),
        ("shell_bash", dispatch_invocation),
        ("get", dispatch_invocation),
        ("value", dispatch_invocation),
        ("paths", dispatch_invocation),
        ("keys", dispatch_invocation),
        ("set", dispatch_invocation),
        ("unset", dispatch_invocation),
        ("add", dispatch_invocation),
        ("remove", dispatch_invocation),
    ];
    #[cfg(feature = "skill")]
    handlers.push(("skill_validate", dispatch_invocation));
    #[cfg(feature = "skill-admin")]
    {
        handlers.push(("skill_status", dispatch_invocation));
        handlers.push(("skill_install", dispatch_invocation));
        handlers.push(("skill_uninstall", dispatch_invocation));
    }
    let app = match cli.bind_actions(handlers) {
        Ok(app) => app,
        Err(error) => {
            let event = build_error_event(
                json_error("cli_actions_invalid", &error.to_string())
                    .hint("make action handler coverage exactly match cli-spec-v1"),
            );
            return emit_event(event, OutputFormat::Json, 1);
        }
    };
    let outcome = match app.resolve_from(std::env::args_os()) {
        Ok(outcome) => outcome,
        Err(error) => {
            let code = error.exit_code();
            return emit_event(cli_error_event(&error), OutputFormat::Json, code);
        }
    };
    match outcome {
        CliOutcome::Run(invocation) => {
            let _stream_redirect = match install_output_redirect(invocation.output_plan()) {
                Ok(guard) => guard,
                Err(message) => return emit_output_setup_error(&message),
            };
            if let Err(message) = activate_output_plan(invocation.output_plan()) {
                return emit_output_setup_error(&message);
            }
            app.execute(&invocation)
        }
        // Injected for every registry, so a spore gets an offline reference
        // without registering anything — and without spending a line of the
        // agent's discovery surface on a command no agent calls.
        CliOutcome::Docs(docs) => {
            let _stream_redirect = match install_output_redirect(docs.output_plan()) {
                Ok(guard) => guard,
                Err(message) => return emit_output_setup_error(&message),
            };
            write_text_exit(&render_cli_reference(&cli), 0)
        }
        CliOutcome::Help(help) => {
            let _stream_redirect = match install_output_redirect(help.output_plan()) {
                Ok(guard) => guard,
                Err(message) => return emit_output_setup_error(&message),
            };
            let format = match activate_output_plan(help.output_plan()) {
                Ok(format) => format,
                Err(message) => return emit_output_setup_error(&message),
            };
            if format == OutputFormat::Plain {
                write_text_exit_to(&help.plain(), 0, result_stream(output_to()))
            } else {
                emit_event(cli_help_event(&help), format, 0)
            }
        }
        CliOutcome::Version(version) => {
            let _stream_redirect = match install_output_redirect(version.output_plan()) {
                Ok(guard) => guard,
                Err(message) => return emit_output_setup_error(&message),
            };
            let format = match activate_output_plan(version.output_plan()) {
                Ok(format) => format,
                Err(message) => return emit_output_setup_error(&message),
            };
            emit_event(cli_version_event(&version), format, 0)
        }
    }
}

#[cfg(feature = "stream-redirect")]
type OutputRedirectGuard = agent_first_data::stream_redirect::InstalledStreamRedirect;

#[cfg(not(feature = "stream-redirect"))]
struct OutputRedirectGuard;

fn install_output_redirect(plan: &OutputPlan) -> Result<Option<OutputRedirectGuard>, String> {
    #[cfg(feature = "stream-redirect")]
    {
        let config = agent_first_data::stream_redirect::StreamRedirectConfig::new(
            plan.stdout_file().map(Path::to_path_buf),
            plan.stderr_file().map(Path::to_path_buf),
        )
        .map_err(|error| error.to_string())?;
        config
            .as_ref()
            .map(agent_first_data::stream_redirect::install)
            .transpose()
            .map_err(|error| error.to_string())
    }
    #[cfg(not(feature = "stream-redirect"))]
    {
        if plan.stdout_file().is_some() || plan.stderr_file().is_some() {
            return Err("file sinks are unavailable in this build".to_string());
        }
        Ok(None)
    }
}

fn activate_output_plan(plan: &OutputPlan) -> Result<OutputFormat, String> {
    match plan {
        OutputPlan::Raw { .. } => {
            OUTPUT_TO
                .set(OutputTo::Split)
                .map_err(|_| "output plan was activated more than once".to_string())?;
            Ok(OutputFormat::Json)
        }
        OutputPlan::Protocol {
            format,
            destination,
            ..
        } => {
            let format = cli_parse_output(format)?;
            let destination = OutputTo::parse(destination)?;
            OUTPUT_TO
                .set(destination)
                .map_err(|_| "output plan was activated more than once".to_string())?;
            Ok(format)
        }
    }
}

fn emit_output_setup_error(message: &str) -> ExitCode {
    let event = build_error_event(json_error("output_setup_failed", message));
    emit_event_to(
        event,
        OutputFormat::Json,
        &OutputOptions::default(),
        1,
        Stream::Stderr,
    )
}

fn invocation_string(invocation: &ResolvedInvocation, id: &str) -> String {
    invocation
        .required(id)
        .as_str()
        .unwrap_or_default()
        .to_string()
}

fn invocation_optional_string(invocation: &ResolvedInvocation, id: &str) -> Option<String> {
    invocation
        .optional(id)
        .and_then(agent_first_data::CliValue::as_str)
        .map(str::to_string)
}

fn invocation_strings(invocation: &ResolvedInvocation, id: &str) -> Vec<String> {
    invocation
        .repeated(id)
        .iter()
        .filter_map(agent_first_data::CliValue::as_str)
        .map(str::to_string)
        .collect()
}

fn invocation_flag(invocation: &ResolvedInvocation, id: &str) -> bool {
    invocation
        .optional(id)
        .and_then(agent_first_data::CliValue::as_bool)
        .unwrap_or(false)
}

fn dispatch_invocation(invocation: &ResolvedInvocation) -> ExitCode {
    let format = invocation
        .output_plan()
        .format()
        .and_then(|format| cli_parse_output(format).ok())
        .unwrap_or(OutputFormat::Json);
    match invocation.action_id() {
        "lint" => {
            let input = invocation_string(invocation, "input");
            let input_format = invocation_optional_string(invocation, "input_format");
            let min_severity = invocation_string(invocation, "min_severity");
            run_lint(
                Path::new(&input),
                input_format.as_deref(),
                &min_severity,
                format,
            )
        }
        "validate" => {
            let input = invocation_string(invocation, "input");
            run_validate(
                Path::new(&input),
                format,
                invocation_flag(invocation, "strict"),
                invocation_flag(invocation, "per_event"),
            )
        }
        "render" => {
            let input = invocation_string(invocation, "input");
            run_render(
                Path::new(&input),
                &invocation_strings(invocation, "secret_name"),
                format,
            )
        }
        "emit_log" => run_emit(
            EmitCommand::Log {
                level: invocation_string(invocation, "level"),
                message: invocation_string(invocation, "message"),
            },
            format,
        ),
        "emit_result" => run_emit(
            EmitCommand::Result {
                message: invocation_string(invocation, "message"),
            },
            format,
        ),
        "emit_error" => run_emit(
            EmitCommand::Error {
                code: invocation_string(invocation, "code"),
                message: invocation_string(invocation, "message"),
                hint: invocation_optional_string(invocation, "hint"),
                retryable: invocation_flag(invocation, "retryable"),
            },
            format,
        ),
        "shell_bash" => run_shell(ShellCommand::Bash),
        #[cfg(feature = "skill")]
        "skill_validate" => run_skill(
            SkillCommand::Validate {
                input: PathBuf::from(invocation_string(invocation, "input")),
            },
            format,
        ),
        #[cfg(feature = "skill-admin")]
        "skill_status" => run_skill(
            SkillCommand::Status {
                agent: invocation_string(invocation, "agent"),
                scope: invocation_string(invocation, "scope"),
                skills_dir: invocation_optional_string(invocation, "skills_dir"),
            },
            format,
        ),
        #[cfg(feature = "skill-admin")]
        "skill_install" => run_skill(
            SkillCommand::Install {
                agent: invocation_string(invocation, "agent"),
                scope: invocation_string(invocation, "scope"),
                skills_dir: invocation_optional_string(invocation, "skills_dir"),
                force: invocation_flag(invocation, "force"),
            },
            format,
        ),
        #[cfg(feature = "skill-admin")]
        "skill_uninstall" => run_skill(
            SkillCommand::Uninstall {
                agent: invocation_string(invocation, "agent"),
                scope: invocation_string(invocation, "scope"),
                skills_dir: invocation_optional_string(invocation, "skills_dir"),
                force: invocation_flag(invocation, "force"),
            },
            format,
        ),
        "get" => dispatch_get(invocation, format),
        "value" => dispatch_value(invocation, format),
        "paths" | "keys" => dispatch_enumerate(invocation, format),
        "set" => dispatch_set(invocation, format),
        "unset" => dispatch_unset(invocation, format),
        "add" => dispatch_add(invocation, format),
        "remove" => dispatch_remove(invocation, format),
        _ => emit_event(
            build_error_event(json_error(
                "cli_action_unreachable",
                "resolved action has no implementation",
            )),
            OutputFormat::Json,
            1,
        ),
    }
}

fn dispatch_get(invocation: &ResolvedInvocation, format: OutputFormat) -> ExitCode {
    let file = invocation_string(invocation, "file");
    let key = invocation_optional_string(invocation, "key");
    let input_format = invocation_optional_string(invocation, "input_format");
    let secret_names = invocation_strings(invocation, "secret_name");
    run_get(
        Path::new(&file),
        key.as_deref(),
        &DocumentContext {
            input_format: input_format.as_deref(),
            secret_names: &secret_names,
            format,
        },
    )
}

fn dispatch_value(invocation: &ResolvedInvocation, format: OutputFormat) -> ExitCode {
    let file = invocation_string(invocation, "file");
    let key = invocation_string(invocation, "key");
    let default = invocation_optional_string(invocation, "default");
    let input_format = invocation_optional_string(invocation, "input_format");
    let secret_names = invocation_strings(invocation, "secret_name");
    run_value_get(
        Path::new(&file),
        &key,
        invocation_flag(invocation, "reveal_secret"),
        default.as_deref(),
        &DocumentContext {
            input_format: input_format.as_deref(),
            secret_names: &secret_names,
            format,
        },
    )
}

fn dispatch_enumerate(invocation: &ResolvedInvocation, format: OutputFormat) -> ExitCode {
    let file = invocation_string(invocation, "file");
    let key = invocation_optional_string(invocation, "key");
    let input_format = invocation_optional_string(invocation, "input_format");
    run_enumerate(
        Path::new(&file),
        key.as_deref(),
        input_format.as_deref(),
        invocation_flag(invocation, "missing_ok"),
        invocation_flag(invocation, "null"),
        format,
        if invocation.action_id() == "paths" {
            EnumerateMode::Paths
        } else {
            EnumerateMode::Keys
        },
    )
}

fn dispatch_set(invocation: &ResolvedInvocation, format: OutputFormat) -> ExitCode {
    let file = invocation_string(invocation, "file");
    let key = invocation_string(invocation, "key");
    let value = invocation_optional_string(invocation, "value");
    let value_type = invocation
        .was_explicit("value_type")
        .then(|| invocation_optional_string(invocation, "value_type"))
        .flatten();
    let secret_from = invocation_optional_string(invocation, "secret_from");
    let input_format = invocation_optional_string(invocation, "input_format");
    run_set(
        Path::new(&file),
        &key,
        value,
        value_type.as_deref(),
        secret_from.as_deref(),
        &DocumentContext {
            input_format: input_format.as_deref(),
            secret_names: &[],
            format,
        },
    )
}

fn dispatch_unset(invocation: &ResolvedInvocation, format: OutputFormat) -> ExitCode {
    let file = invocation_string(invocation, "file");
    let key = invocation_string(invocation, "key");
    let input_format = invocation_optional_string(invocation, "input_format");
    run_unset(
        Path::new(&file),
        &key,
        &DocumentContext {
            input_format: input_format.as_deref(),
            secret_names: &[],
            format,
        },
    )
}

fn dispatch_add(invocation: &ResolvedInvocation, format: OutputFormat) -> ExitCode {
    let file = invocation_string(invocation, "file");
    let key = invocation_string(invocation, "key");
    let slug = invocation_string(invocation, "slug");
    let slug_field = invocation_string(invocation, "slug_field");
    let fields = invocation_strings(invocation, "fields");
    let input_format = invocation_optional_string(invocation, "input_format");
    run_add(
        Path::new(&file),
        &key,
        &slug,
        &slug_field,
        &fields,
        &DocumentContext {
            input_format: input_format.as_deref(),
            secret_names: &[],
            format,
        },
    )
}

fn dispatch_remove(invocation: &ResolvedInvocation, format: OutputFormat) -> ExitCode {
    let file = invocation_string(invocation, "file");
    let key = invocation_string(invocation, "key");
    let slug = invocation_string(invocation, "slug");
    let slug_field = invocation_string(invocation, "slug_field");
    let input_format = invocation_optional_string(invocation, "input_format");
    run_remove(
        Path::new(&file),
        &key,
        &slug,
        &slug_field,
        &DocumentContext {
            input_format: input_format.as_deref(),
            secret_names: &[],
            format,
        },
    )
}

// ═══════════════════════════════════════════
// Protocol tools: lint, validate, render, skill
// ═══════════════════════════════════════════

fn run_lint(
    input: &Path,
    input_format: Option<&str>,
    min_severity: &str,
    format: OutputFormat,
) -> ExitCode {
    let resolved = match resolve_input_format(input_format) {
        Ok(resolved) => resolved,
        Err(message) => return emit_usage_error(&message, format),
    };
    // R6: lint reuses the document input layer (extension inference +
    // `--input-format` override) so TOML/YAML/dotenv/INI documents get the
    // same AFDATA naming/suffix checks as JSON. Unlike other document
    // commands, an undetected extension (or `-` with no override) falls
    // back to JSON/JSONL rather than erroring — that dual-mode behavior
    // predates this command's document-format support and stays unchanged.
    let effective = resolved
        .or_else(|| {
            if input == Path::new("-") {
                None
            } else {
                DocumentFormat::detect(input)
            }
        })
        .unwrap_or(DocumentFormat::Json);

    let mut findings = Vec::new();
    if effective == DocumentFormat::Json {
        let text = match read_input_or_stdin(input) {
            Ok(text) => text,
            Err(message) => {
                let event = build_error_event(json_error("read_failed", &message));
                return emit_event(event, format, 1);
            }
        };
        let parsed = match parse_json_or_jsonl(&text) {
            Ok(parsed) => parsed,
            Err(err) => return emit_parse_error(err, format),
        };
        match parsed {
            ParsedInput::Single(value) => lint_value(&value, "", &mut findings),
            ParsedInput::Lines(values) => {
                for (idx, value) in values.iter().enumerate() {
                    lint_value(value, &format!("/{}", idx + 1), &mut findings);
                }
            }
        }
    } else {
        let (value, _doc_format) = match read_document_input(input, Some(effective)) {
            Ok(pair) => pair,
            Err(err) => {
                let event = build_error_event(json_error(err.code(), &err.redacted_message()));
                return emit_event(event, format, 1);
            }
        };
        let json_value = match Value::try_from(value) {
            Ok(value) => value,
            Err(error) => {
                let event = build_error_event(json_error(error.code(), &error.redacted_message()));
                return emit_event(event, format, 1);
            }
        };
        lint_value(&json_value, "", &mut findings);
    }
    if min_severity == "error" {
        findings.retain(|finding| finding.severity == "error");
    }
    emit_findings("lint_failed", "lint failed", findings, format)
}

fn run_validate(input: &Path, format: OutputFormat, strict: bool, per_event: bool) -> ExitCode {
    let text = match read_input_or_stdin(input) {
        Ok(text) => text,
        Err(message) => {
            let event = build_error_event(json_error("read_failed", &message));
            return emit_event(event, format, 1);
        }
    };
    let parsed = match parse_json_or_jsonl(&text) {
        Ok(parsed) => parsed,
        Err(err) => return emit_parse_error(err, format),
    };
    let mut findings = Vec::new();
    if per_event {
        match parsed {
            ParsedInput::Single(Value::Array(events)) | ParsedInput::Lines(events) => {
                for (idx, event) in events.iter().enumerate() {
                    validate_one_event(event, strict, &format!("/{idx}"), &mut findings);
                }
            }
            ParsedInput::Single(value) => validate_one_event(&value, strict, "", &mut findings),
        }
        return emit_findings("validation_failed", "validation failed", findings, format);
    }
    match parsed {
        ParsedInput::Single(Value::Array(events)) => {
            if let Err(vs) = validate_protocol_stream(&events, strict) {
                for v in vs {
                    findings.push(Finding::error(v.rule, v.pointer, v.message));
                }
            }
        }
        ParsedInput::Single(value) => validate_single_input(value, strict, &mut findings),
        ParsedInput::Lines(values) => {
            if let Err(vs) = validate_protocol_stream(&values, strict) {
                for v in vs {
                    findings.push(Finding::error(v.rule, v.pointer, v.message));
                }
            }
        }
    }
    emit_findings("validation_failed", "validation failed", findings, format)
}

fn validate_single_input(value: Value, strict: bool, findings: &mut Vec<Finding>) {
    let kind = value.get("kind").and_then(Value::as_str);
    if matches!(kind, Some("log" | "progress")) {
        if let Err(vs) = validate_protocol_stream(&[value], strict) {
            for v in vs {
                findings.push(Finding::error(v.rule, v.pointer, v.message));
            }
        }
        return;
    }
    validate_one_event(&value, strict, "", findings);
}

fn validate_one_event(value: &Value, strict: bool, pointer: &str, findings: &mut Vec<Finding>) {
    if let Err(v) = validate_protocol_event(value, strict) {
        findings.push(Finding::error(
            v.rule,
            format!("{pointer}{}", v.pointer),
            v.message,
        ));
    }
}

fn run_render(input: &Path, secret_names: &[String], format: OutputFormat) -> ExitCode {
    let text = match read_input_or_stdin(input) {
        Ok(text) => text,
        Err(message) => {
            let event = build_error_event(json_error("read_failed", &message));
            return emit_event(event, format, 1);
        }
    };
    let parsed = match parse_json_or_jsonl(&text) {
        Ok(parsed) => parsed,
        // R5: parse-error presentation honors the negotiated `--output`
        // (previously hardcoded to JSON, inconsistent with lint/validate).
        Err(err) => return emit_parse_error(err, format),
    };
    let output_options = OutputOptions {
        redaction: Redactor::new().secret_names(secret_names.iter().cloned()),
        style: PlainStyle::default(),
    };
    match parsed {
        ParsedInput::Single(value) => {
            write_text_exit(&format_value(&value, format, false, &output_options), 0)
        }
        ParsedInput::Lines(values) => {
            let mut out = String::new();
            for (idx, value) in values.iter().enumerate() {
                if idx > 0 && is_yaml_format(format) {
                    out.push_str("---\n");
                }
                out.push_str(&format_value(value, format, idx > 0, &output_options));
                if !out.ends_with('\n') {
                    out.push('\n');
                }
            }
            write_text_exit(&out, 0)
        }
    }
}

fn run_emit(action: EmitCommand, format: OutputFormat) -> ExitCode {
    match action {
        EmitCommand::Log { level, message } => {
            if message.is_empty() {
                return emit_cli_usage_error(
                    "log MESSAGE must not be empty",
                    "run `afdata emit log --help` and choose one registered combination",
                    format,
                );
            }
            let event = json_log(json!({
                "level": level,
                "message": message,
            }))
            .build();
            emit_event(event, format, 0)
        }
        EmitCommand::Result { message } => {
            if message.is_empty() {
                return emit_cli_usage_error(
                    "result MESSAGE must not be empty",
                    "run `afdata emit result --help` and choose one registered combination",
                    format,
                );
            }
            let event = json_result(json!({"message": message})).build();
            emit_event(event, format, 0)
        }
        EmitCommand::Error {
            code,
            message,
            hint,
            retryable,
        } => {
            if code.is_empty() || message.is_empty() {
                return emit_cli_usage_error(
                    "error CODE and MESSAGE must not be empty",
                    "run `afdata emit error --help` and choose one registered combination",
                    format,
                );
            }
            let event = build_error_event(
                json_error(&code, &message)
                    .hint_if_some(hint.as_deref())
                    .retryable_if(retryable),
            );
            emit_event(event, format, 1)
        }
    }
}

fn run_shell(action: ShellCommand) -> ExitCode {
    match action {
        ShellCommand::Bash => {
            const BASH_SOURCE: &str = include_str!("../../bash/afdata.sh");
            write_text_exit(BASH_SOURCE, 0)
        }
    }
}

#[cfg(feature = "skill")]
fn run_skill(action: SkillCommand, format: OutputFormat) -> ExitCode {
    match action {
        SkillCommand::Validate { input } => run_skill_validate(&input, format),
        #[cfg(feature = "skill-admin")]
        SkillCommand::Status {
            agent,
            scope,
            skills_dir,
        } => run_skill_admin_action(
            agent_first_data::skill::SkillAction::Status,
            &agent,
            &scope,
            skills_dir,
            false,
            format,
        ),
        #[cfg(feature = "skill-admin")]
        SkillCommand::Install {
            agent,
            scope,
            skills_dir,
            force,
        } => run_skill_admin_action(
            agent_first_data::skill::SkillAction::Install,
            &agent,
            &scope,
            skills_dir,
            force,
            format,
        ),
        #[cfg(feature = "skill-admin")]
        SkillCommand::Uninstall {
            agent,
            scope,
            skills_dir,
            force,
        } => run_skill_admin_action(
            agent_first_data::skill::SkillAction::Uninstall,
            &agent,
            &scope,
            skills_dir,
            force,
            format,
        ),
    }
}

#[cfg(feature = "skill")]
fn run_skill_validate(input: &Path, format: OutputFormat) -> ExitCode {
    let (text, expected_name, display_path) = match read_skill_input(input) {
        Ok(value) => value,
        Err(message) => {
            let event = build_error_event(json_error("read_failed", &message));
            return emit_event(event, format, 1);
        }
    };
    let validation = match expected_name.as_deref() {
        Some(name) => agent_first_data::skill::validate_skill_named(&text, name),
        None => agent_first_data::skill::validate_skill(&text),
    };
    let metadata = match validation {
        Ok(metadata) => metadata,
        Err(error) => {
            let event = build_error_event(
                json_error("skill_invalid", error.message())
                    .hint("make SKILL.md front matter conform to the Agent Skills specification"),
            );
            return emit_event(event, format, 1);
        }
    };
    let event = json_result(json!({
        "code": "skill_valid",
        "path": display_path,
        "name": metadata.name,
        "description": metadata.description,
        "license": metadata.license,
        "compatibility": metadata.compatibility,
        "metadata": metadata.metadata,
        "allowed_tools": metadata.allowed_tools,
        "disable_model_invocation": metadata.disable_model_invocation,
        "user_invocable": metadata.user_invocable,
    }))
    .build();
    emit_event(event, format, 0)
}

#[cfg(feature = "skill")]
fn read_skill_input(input: &Path) -> Result<(String, Option<String>, String), String> {
    if input == Path::new("-") {
        return read_input_or_stdin(Path::new("-")).map(|text| (text, None, "<stdin>".to_string()));
    }
    let input_metadata = std::fs::symlink_metadata(input)
        .map_err(|error| format!("failed to inspect {}: {error}", input.display()))?;
    if input_metadata.file_type().is_symlink() {
        return Err(format!(
            "refusing to validate symlinked skill input at {}",
            input.display()
        ));
    }

    let (skill_path, expected_name) = if input_metadata.is_dir() {
        let name = path_file_name(input)?;
        (input.join("SKILL.md"), Some(name))
    } else if input_metadata.is_file() {
        let expected_name = if input.file_name().and_then(|name| name.to_str()) == Some("SKILL.md")
        {
            input.parent().map(path_file_name).transpose()?
        } else {
            None
        };
        (input.to_path_buf(), expected_name)
    } else {
        return Err(format!(
            "skill input is not a regular file or directory: {}",
            input.display()
        ));
    };

    let skill_metadata = std::fs::symlink_metadata(&skill_path)
        .map_err(|error| format!("failed to inspect {}: {error}", skill_path.display()))?;
    if skill_metadata.file_type().is_symlink() || !skill_metadata.is_file() {
        return Err(format!(
            "skill document is not a regular file: {}",
            skill_path.display()
        ));
    }
    let text = std::fs::read_to_string(&skill_path)
        .map_err(|error| format!("failed to read {}: {error}", skill_path.display()))?;
    Ok((text, expected_name, skill_path.display().to_string()))
}

#[cfg(feature = "skill")]
fn path_file_name(path: &Path) -> Result<String, String> {
    path.file_name()
        .and_then(|name| name.to_str())
        .map(str::to_string)
        .ok_or_else(|| format!("path has no UTF-8 directory name: {}", path.display()))
}

#[cfg(feature = "skill-admin")]
fn run_skill_admin_action(
    action: agent_first_data::skill::SkillAction,
    agent: &str,
    scope: &str,
    skills_dir: Option<String>,
    force: bool,
    format: OutputFormat,
) -> ExitCode {
    use agent_first_data::skill::{SkillAsset, SkillOptions, SkillSpec, run_skill_admin};

    let agent = match parse_skill_agent(agent) {
        Ok(agent) => agent,
        Err(message) => {
            let event = build_cli_error(
                &message,
                Some("valid agents: all, codex, claude-code, opencode, hermes"),
            );
            return emit_event(event, format, 2);
        }
    };
    let scope = match parse_skill_scope(scope) {
        Ok(scope) => scope,
        Err(message) => {
            let event = build_cli_error(&message, Some("valid scopes: personal, workspace"));
            return emit_event(event, format, 2);
        }
    };

    const SKILL_SOURCE: &str = include_str!("../../skills/agent-first-data/SKILL.md");
    // SKILL.md points agents at these bundled references, so they must install
    // alongside it. Keep this list in step with skills/agent-first-data/references/
    // — the package smoke installs to a temp dir and asserts the whole tree lands.
    const SKILL_ASSETS: &[SkillAsset] = &[
        SkillAsset {
            path: "references/bash.md",
            contents: include_str!("../../skills/agent-first-data/references/bash.md"),
        },
        SkillAsset {
            path: "references/cli-protocol.md",
            contents: include_str!("../../skills/agent-first-data/references/cli-protocol.md"),
        },
        SkillAsset {
            path: "references/documents.md",
            contents: include_str!("../../skills/agent-first-data/references/documents.md"),
        },
        SkillAsset {
            path: "references/naming-output.md",
            contents: include_str!("../../skills/agent-first-data/references/naming-output.md"),
        },
        SkillAsset {
            path: "references/registry.json",
            contents: include_str!("../../skills/agent-first-data/references/registry.json"),
        },
        SkillAsset {
            path: "references/protocol-v1.schema.json",
            contents: include_str!(
                "../../skills/agent-first-data/references/protocol-v1.schema.json"
            ),
        },
        SkillAsset {
            path: "references/cli-help-v2.schema.json",
            contents: include_str!(
                "../../skills/agent-first-data/references/cli-help-v2.schema.json"
            ),
        },
        SkillAsset {
            path: "references/cli-spec-v1.schema.json",
            contents: include_str!(
                "../../skills/agent-first-data/references/cli-spec-v1.schema.json"
            ),
        },
    ];
    let spec = SkillSpec {
        name: "agent-first-data",
        source: SKILL_SOURCE,
        title: "Agent-First Data",
        marker_slug: "afdata",
        assets: SKILL_ASSETS,
    };
    let options = SkillOptions {
        agent,
        scope,
        skills_dir,
        force,
    };
    match run_skill_admin(&spec, action, &options) {
        Ok(report) => match serde_json::to_value(report) {
            Ok(value) => {
                let event = json_result(value).build();
                emit_event(event, format, 0)
            }
            Err(err) => {
                let event = build_error_event(json_error(
                    "serialization_failed",
                    &format!("failed to serialize skill report: {err}"),
                ));
                emit_event(event, format, 1)
            }
        },
        Err(err) => {
            let mut builder = json_error("cli_error", &err.message);
            if let Some(hint) = err.hint.as_deref() {
                builder = builder.hint(hint);
            }
            if let Some(report) = err.partial_report.as_ref()
                && let Ok(partial_report) = serde_json::to_value(report)
            {
                builder = builder.field("partial_report", partial_report);
            }
            let event = build_error_event(builder);
            emit_event(event, format, 2)
        }
    }
}

#[cfg(feature = "skill-admin")]
fn parse_skill_agent(value: &str) -> Result<agent_first_data::skill::SkillAgentSelection, String> {
    match value {
        "all" => Ok(agent_first_data::skill::SkillAgentSelection::All),
        "codex" => Ok(agent_first_data::skill::SkillAgentSelection::Codex),
        "claude-code" => Ok(agent_first_data::skill::SkillAgentSelection::ClaudeCode),
        "opencode" => Ok(agent_first_data::skill::SkillAgentSelection::Opencode),
        "hermes" => Ok(agent_first_data::skill::SkillAgentSelection::Hermes),
        other => Err(format!("invalid --agent '{other}'")),
    }
}

#[cfg(feature = "skill-admin")]
fn parse_skill_scope(value: &str) -> Result<agent_first_data::skill::SkillScope, String> {
    match value {
        "personal" => Ok(agent_first_data::skill::SkillScope::Personal),
        "workspace" => Ok(agent_first_data::skill::SkillScope::Workspace),
        other => Err(format!("invalid --scope '{other}'")),
    }
}

fn format_value(
    value: &Value,
    format: OutputFormat,
    suppress_yaml_boundary: bool,
    output_options: &OutputOptions,
) -> String {
    let mut out = render(value, format, output_options);
    if suppress_yaml_boundary
        && is_yaml_format(format)
        && let Some(stripped) = out.strip_prefix("---\n")
    {
        out = stripped.to_string();
    }
    out
}

/// Whether `format` is [`OutputFormat::Yaml`].
fn is_yaml_format(format: OutputFormat) -> bool {
    format == OutputFormat::Yaml
}

/// Read `input` fully: `-` reads stdin to EOF, otherwise reads the file at
/// that path. D1: no implicit stdin fallback and no TTY detection — every
/// caller passes an explicit path or `-`, so there is no "maybe hangs"
/// shape left to guard against; reading `-` on an interactive terminal is
/// the user's explicit request, same as any other Unix tool.
fn read_input_or_stdin(input: &Path) -> Result<String, String> {
    if input == Path::new("-") {
        let mut text = String::new();
        std::io::stdin()
            .read_to_string(&mut text)
            .map_err(|err| format!("failed to read stdin: {err}"))?;
        return Ok(text);
    }
    std::fs::read_to_string(input)
        .map_err(|err| format!("failed to read {}: {err}", input.display()))
}

fn parse_json_or_jsonl(text: &str) -> Result<ParsedInput, ParseError> {
    if text.trim().is_empty() {
        return Err(ParseError {
            code: "json_parse_failed",
            message: "input is empty".to_string(),
            hint: Some("provide a JSON value or JSONL stream".to_string()),
            line: None,
        });
    }
    match serde_json::from_str::<Value>(text) {
        Ok(value) => Ok(ParsedInput::Single(value)),
        Err(whole_error) => {
            let mut values = Vec::new();
            for (idx, line) in text.lines().enumerate() {
                if line.trim().is_empty() {
                    continue;
                }
                match serde_json::from_str::<Value>(line) {
                    Ok(value) => values.push(value),
                    Err(line_error) => {
                        return Err(ParseError {
                            code: "jsonl_parse_failed",
                            message: format!("line {} is not valid JSON: {line_error}", idx + 1),
                            hint: Some(format!("complete JSON parse failed first: {whole_error}")),
                            line: Some(idx + 1),
                        });
                    }
                }
            }
            if values.is_empty() {
                Err(ParseError {
                    code: "json_parse_failed",
                    message: whole_error.to_string(),
                    hint: Some("provide a JSON value or JSONL stream".to_string()),
                    line: None,
                })
            } else {
                Ok(ParsedInput::Lines(values))
            }
        }
    }
}

/// Build an error event without an `expect`. The error builders here always use
/// non-empty literal codes/messages and non-reserved fields, so `build()` cannot
/// actually fail; on the impossible error we fall back to `build_cli_error` so the
/// function stays total and panic-free.
fn build_error_event(builder: ErrorBuilder) -> Event {
    match builder.build() {
        Ok(event) => event,
        Err(err) => build_cli_error(&err.to_string(), None),
    }
}

fn emit_parse_error(err: ParseError, format: OutputFormat) -> ExitCode {
    let mut fields = serde_json::Map::new();
    if let Some(line) = err.line {
        fields.insert("line".to_string(), json!(line));
    }
    let mut builder = json_error(err.code, &err.message);
    if let Some(hint) = err.hint.as_deref() {
        builder = builder.hint(hint);
    }
    builder = builder.fields(Value::Object(fields));
    let event = build_error_event(builder);
    emit_event(event, format, 1)
}

fn emit_findings(
    error_code: &'static str,
    error_message: &'static str,
    findings: Vec<Finding>,
    format: OutputFormat,
) -> ExitCode {
    let findings_json = Value::Array(findings.iter().map(Finding::to_json).collect());
    if findings.iter().any(|finding| finding.severity == "error") {
        let event = build_error_event(
            json_error(error_code, error_message).fields(json!({"findings": findings_json})),
        );
        return emit_event(event, format, 1);
    }
    // Warnings are reported but do not fail: they are heuristic. `ok` still
    // turns false, so a caller reading one field learns there is something to
    // read, and exit 0 keeps a heuristic from breaking a pipeline.
    let event = json_result(json!({"ok": findings.is_empty(), "findings": findings_json})).build();
    emit_event(event, format, 0)
}

/// A usage-class error (R2): the CLI invocation's own shape was wrong
/// (an invalid `--input-format`/`--value-type` name, a malformed
/// `FIELD=VALUE`, a mutation command given `-`, …) — distinct from a
/// runtime document error, and always exit 2. Routed by [`emit_event`] as a
/// `kind:"error"` envelope, so it lands on stderr under the default split (and
/// on the chosen stream under `--output-to stdout|stderr`).
fn emit_usage_error(message: &str, format: OutputFormat) -> ExitCode {
    let event = build_error_event(json_error("document_usage_error", message));
    emit_event(event, format, 2)
}

/// A value the registry accepted but this command cannot use.
///
/// `cli-spec-v1` has no "non-empty string" type, so emptiness is checked here —
/// one layer after the parser, but it is the same verdict the parser would have
/// reached, so it carries the parser's own code rather than a second spelling
/// for it. `build_cli_error`'s generic `cli_error` stays for CLIs that have no
/// registry to name a rule; this one has.
fn emit_cli_usage_error(message: &str, hint: &str, format: OutputFormat) -> ExitCode {
    let event = build_error_event(json_error("cli_invalid_argument_value", message).hint(hint));
    emit_event(event, format, 2)
}

/// The resolved `--output-to` selector. Read through [`output_to`], which
/// falls back to `Split` before the flag is parsed so any pre-dispatch
/// usage/parse error still routes its `error` envelope to stderr by default.
static OUTPUT_TO: std::sync::OnceLock<OutputTo> = std::sync::OnceLock::new();

fn output_to() -> OutputTo {
    OUTPUT_TO.get().copied().unwrap_or(OutputTo::Split)
}

/// Stream a `kind:"result"` payload lands on under `selector`.
fn result_stream(selector: OutputTo) -> Stream {
    match selector {
        OutputTo::Split | OutputTo::Stdout => Stream::Stdout,
        OutputTo::Stderr => Stream::Stderr,
    }
}

/// Stream a `kind:"error"` (or `progress`/`log` diagnostic) lands on under
/// `selector`.
fn error_stream(selector: OutputTo) -> Stream {
    match selector {
        OutputTo::Split | OutputTo::Stderr => Stream::Stderr,
        OutputTo::Stdout => Stream::Stdout,
    }
}

/// Route an already-built event by its `kind`: `result` follows
/// [`result_stream`], every other kind follows [`error_stream`].
fn stream_for(event: &Value, selector: OutputTo) -> Stream {
    if event.get("kind").and_then(Value::as_str) == Some("result") {
        result_stream(selector)
    } else {
        error_stream(selector)
    }
}

fn emit_event(event: impl Into<Value>, format: OutputFormat, code: u8) -> ExitCode {
    let event: Value = event.into();
    let stream = stream_for(&event, output_to());
    emit_event_to(event, format, &OutputOptions::default(), code, stream)
}

enum Stream {
    Stdout,
    Stderr,
}

/// As [`emit_event`], but rendering through `output_options` (redaction and
/// style) and writing to an explicit `stream`. Used by the document commands
/// so `--secret-name` reaches the final render and so callers that already
/// know the routing (via [`result_stream`]/[`error_stream`] under the resolved
/// `--output-to`) can name the stream directly.
fn emit_event_to(
    event: impl Into<Value>,
    format: OutputFormat,
    output_options: &OutputOptions,
    code: u8,
    stream: Stream,
) -> ExitCode {
    let mut event: Value = event.into();
    if event.get("trace").is_none()
        && let Some(object) = event.as_object_mut()
    {
        object.insert("trace".to_string(), json!({}));
    }
    if let Err(violation) = validate_protocol_event(&event, true) {
        let fallback = build_error_event(
            json_error(
                "internal_protocol_error",
                "afdata attempted to emit an invalid protocol event",
            )
            .field("validation_message", json!(violation.to_string())),
        );
        let mut text = render(
            fallback.as_value(),
            OutputFormat::Json,
            &OutputOptions::default(),
        );
        if !text.ends_with('\n') {
            text.push('\n');
        }
        return write_text_exit_to(&text, 1, stream);
    }
    let mut text = render(&event, format, output_options);
    if !text.ends_with('\n') {
        text.push('\n');
    }
    write_text_exit_to(&text, code, stream)
}

fn write_text_exit(text: &str, code: u8) -> ExitCode {
    write_text_exit_to(text, code, Stream::Stdout)
}

// The sanctioned emitter sink. Per the spec's Channel policy, a finite
// command splits by kind (`result` → stdout, `error`/diagnostics → stderr) and
// `--output-to stdout|stderr` collapses everything onto one stream; both routes
// resolve to a `Stream` here. This is the one place allowed to touch
// `std::io::stderr` directly (clippy.toml's `disallowed-methods` guards against
// stray stderr writes elsewhere that would bypass this routing).
#[allow(clippy::disallowed_methods)]
fn write_text_exit_to(text: &str, code: u8, stream: Stream) -> ExitCode {
    let result = match stream {
        Stream::Stdout => std::io::stdout().lock().write_all(text.as_bytes()),
        Stream::Stderr => std::io::stderr().lock().write_all(text.as_bytes()),
    };
    if let Err(err) = result {
        if err.kind() == std::io::ErrorKind::BrokenPipe {
            return ExitCode::from(0);
        }
        return ExitCode::from(1);
    }
    ExitCode::from(code)
}

// ═══════════════════════════════════════════
// Document read/edit commands
// ═══════════════════════════════════════════

/// Shared per-invocation context threaded through the document read/edit
/// commands: the `--input-format` override, the `--secret-name` redaction
/// list, and the negotiated `--output` rendering format.
struct DocumentContext<'a> {
    input_format: Option<&'a str>,
    secret_names: &'a [String],
    format: OutputFormat,
}

/// A document command failure, classified per R2: `Usage` is a CLI-shape
/// mistake (bad `--input-format`/`--value-type` name, malformed
/// `FIELD=VALUE`, a mutation command given `-`, VALUE absent with no
/// `--value-type`/`--secret-from`, …) and always exit 2; `Document` wraps a
/// library [`DocumentError`], mapped to a stable code by
/// [`DocumentError::code`] and always exit 1; `Runtime` covers the handful
/// of CLI-originated *runtime* facts about the document/secret source that
/// are not `DocumentError` variants (a secret leaf without
/// `--reveal-secret`, a non-scalar `value` target, a scalar `paths`/`keys`
/// target, a failed `--secret-from` read) — also exit 1, with an explicit
/// stable code.
enum CliDocError {
    Usage(String),
    Document(DocumentError),
    Runtime { code: &'static str, message: String },
}

impl CliDocError {
    fn runtime(code: &'static str, message: impl Into<String>) -> Self {
        Self::Runtime {
            code,
            message: message.into(),
        }
    }
}

impl From<DocumentError> for CliDocError {
    fn from(err: DocumentError) -> Self {
        Self::Document(err)
    }
}

/// Whether `err` is in the `document_path_not_found` bucket — the "the
/// address doesn't exist" family that `value --default` and
/// `paths`/`keys --missing-ok` are allowed to swallow. Reuses
/// [`DocumentError::code`] directly so the exemption is always exactly the
/// bucket agents see in `error.code`, never a separately-maintained variant list.
fn is_path_not_found(err: &CliDocError) -> bool {
    matches!(err, CliDocError::Document(doc_err) if doc_err.code() == "document_path_not_found")
}

/// Render `err` into `(event, exit_code)`.
fn document_error_event(err: &CliDocError) -> (Event, u8) {
    match err {
        CliDocError::Usage(message) => (
            build_error_event(json_error("document_usage_error", message)),
            2,
        ),
        // `redacted_message`, never `Display`: an error event is the thing an
        // agent routinely writes to a log, and `Display` quotes the offending
        // document text — which is how a `_secret` leaf reached stderr verbatim.
        CliDocError::Document(doc_err) => (
            build_error_event(json_error(doc_err.code(), &doc_err.redacted_message())),
            1,
        ),
        CliDocError::Runtime { code, message } => (build_error_event(json_error(code, message)), 1),
    }
}

/// Finish a `get`/`set`/`unset`/`add`/`remove` invocation. Under the default
/// `--output-to split` the success `result` envelope goes to stdout and the
/// `error` envelope to stderr (so `x=$(afdata get f k)` never captures an error
/// as data and `afdata set f k v >/dev/null` never swallows the diagnostic);
/// `--output-to stdout|stderr` collapses both onto the one chosen stream.
fn finish_document(result: Result<Value, CliDocError>, ctx: &DocumentContext<'_>) -> ExitCode {
    let selector = output_to();
    match result {
        Ok(payload) => emit_document_event(
            json_result(payload).build(),
            ctx,
            0,
            result_stream(selector),
        ),
        Err(err) => {
            let (event, code) = document_error_event(&err);
            emit_document_event(event, ctx, code, error_stream(selector))
        }
    }
}

/// Finish a `value`/`paths`/`keys` invocation: success writes raw bytes
/// straight to stdout with no envelope; failure writes the envelope to stderr,
/// leaving stdout empty — so `x=$(afdata value f k)` never captures a JSON
/// error as data. These commands are always split (they reject a non-default
/// `--output-to`), so `error_stream` here always resolves to stderr.
fn finish_raw(result: Result<Vec<u8>, CliDocError>, ctx: &DocumentContext<'_>) -> ExitCode {
    match result {
        Ok(bytes) => write_raw_exit(&bytes),
        Err(err) => {
            let (event, code) = document_error_event(&err);
            emit_document_event(event, ctx, code, error_stream(output_to()))
        }
    }
}

/// Emit a document command's event to `stream`, redacting through
/// `ctx.secret_names` in addition to the crate's default `_secret`-suffix
/// convention.
fn emit_document_event(
    event: impl Into<Value>,
    ctx: &DocumentContext<'_>,
    code: u8,
    stream: Stream,
) -> ExitCode {
    let output_options = document_output_options(ctx);
    emit_event_to(event, ctx.format, &output_options, code, stream)
}

fn document_output_options(ctx: &DocumentContext<'_>) -> OutputOptions {
    OutputOptions {
        redaction: Redactor::new().secret_names(ctx.secret_names.iter().cloned()),
        style: PlainStyle::default(),
    }
}

/// Parse an explicit `--input-format` value into a [`DocumentFormat`].
fn parse_document_format(name: &str) -> Result<DocumentFormat, String> {
    match name.to_ascii_lowercase().as_str() {
        "json" => Ok(DocumentFormat::Json),
        "toml" => Ok(DocumentFormat::Toml),
        "yaml" | "yml" => Ok(DocumentFormat::Yaml),
        "dotenv" | "env" => Ok(DocumentFormat::Dotenv),
        "ini" => Ok(DocumentFormat::Ini),
        "toml-frontmatter" => Ok(DocumentFormat::TomlFrontmatter),
        "yaml-frontmatter" => Ok(DocumentFormat::YamlFrontmatter),
        other => Err(format!(
            "unsupported --input-format `{other}`; expected json, toml, yaml, yml, dotenv, env, ini, \
             toml-frontmatter, or yaml-frontmatter"
        )),
    }
}

/// Resolve an optional `--input-format` string into an optional
/// [`DocumentFormat`], surfacing a parse error as `Err`.
fn resolve_input_format(input_format: Option<&str>) -> Result<Option<DocumentFormat>, String> {
    input_format.map(parse_document_format).transpose()
}

/// Resolve `(value, format)` for a document read command from `FILE`
/// (`-` reads stdin). `-` defaults to JSON unless `input_format` overrides
/// it; a real path's format is detected from its extension unless
/// `input_format` overrides it.
fn read_document_input(
    file: &Path,
    input_format: Option<DocumentFormat>,
) -> Result<(DocumentValue, DocumentFormat), DocumentError> {
    if file == Path::new("-") {
        let format = input_format.unwrap_or(DocumentFormat::Json);
        let doc = Document::from_reader(std::io::stdin().lock(), format)?;
        return Ok((doc.value().clone(), doc.format()));
    }
    let doc = DocumentFile::open(file, input_format)?;
    Ok((doc.value().clone(), doc.format()))
}

/// Extract `key`'s scalar as raw bytes for `value`: a string's bytes are
/// copied verbatim; other scalars render their display form; a null,
/// non-finite float, array, or object is rejected.
///
/// §4 digit fidelity: [`DocumentValue::Number`] is emitted byte for byte
/// from its stored literal — never routed through `f64::to_string()`, which
/// is exactly the corruption path this variant exists to avoid. `value` is
/// otherwise type-lossy (every scalar becomes plain text) but this makes it
/// digit-faithful: the exact source digits, always.
fn document_scalar_bytes(value: &DocumentValue, key: &str) -> Result<Vec<u8>, String> {
    let text = match value {
        DocumentValue::String(value) => return Ok(value.as_bytes().to_vec()),
        DocumentValue::Bool(value) => value.to_string(),
        DocumentValue::Integer(value) => value.to_string(),
        DocumentValue::Unsigned(value) => value.to_string(),
        DocumentValue::Float(value) => {
            if !value.is_finite() {
                return Err(format!("non-finite scalar at `{key}`"));
            }
            value.to_string()
        }
        DocumentValue::Number(text) => return Ok(text.clone().into_bytes()),
        DocumentValue::Null => return Err(format!("path `{key}` is null")),
        DocumentValue::Array(_) | DocumentValue::Object(_) => {
            return Err(format!("path `{key}` is not a scalar"));
        }
    };
    Ok(text.into_bytes())
}

/// Whether `key` addresses anything afdata's secret-naming convention covers:
/// **any** segment on the path carrying an exact `_secret`/`_SECRET` suffix, or
/// matching `secret_names` (the `--secret-name` list) exactly.
///
/// The whole path, not just its leaf. A name marks the subtree beneath it, so
/// `credentials_secret.password` is exactly as secret as `credentials_secret`.
/// Judging the leaf alone let a caller step past the marked node and read the
/// subtree out in the clear.
fn document_path_is_secret(key: &str, secret_names: &[String]) -> Result<bool, DocumentError> {
    let segments = parse_path(key)?;
    if segments.is_empty() {
        return Err(DocumentError::EmptyPath);
    }
    let redactor = Redactor::new().secret_names(secret_names.iter().cloned());
    Ok(segments
        .iter()
        .any(|segment| redactor.is_secret_name(segment)))
}

/// Reject a literal `-` FILE for a mutation command (D1): unlike read
/// commands (where FILE `-` means stdin), mutation commands never read
/// stdin, so `-` has no meaning and is a usage error rather than a silent
/// attempt to open a file literally named `-`.
fn reject_mutation_dash(file: &Path) -> Result<(), CliDocError> {
    if file == Path::new("-") {
        return Err(CliDocError::Usage(
            "`-` is not a valid FILE for a mutation command; mutation commands never read stdin"
                .to_string(),
        ));
    }
    Ok(())
}

/// Write `bytes` directly to stdout with no AFDATA envelope and no forced
/// trailing newline (used by `value`'s raw scalar output and `paths`/`keys`'
/// line-oriented output).
fn write_raw_exit(bytes: &[u8]) -> ExitCode {
    let mut stdout = std::io::stdout().lock();
    if let Err(err) = stdout.write_all(bytes) {
        if err.kind() == std::io::ErrorKind::BrokenPipe {
            return ExitCode::from(0);
        }
        return ExitCode::from(1);
    }
    if stdout.flush().is_err() {
        return ExitCode::from(1);
    }
    ExitCode::from(0)
}

/// `get`: read a document whole, or the value at dot-path `key` (D2 — `show`
/// no longer exists; omitting `key` is the whole-document form).
fn run_get(file: &Path, key: Option<&str>, ctx: &DocumentContext<'_>) -> ExitCode {
    finish_document(compute_get(file, key, ctx), ctx)
}

fn compute_get(
    file: &Path,
    key: Option<&str>,
    ctx: &DocumentContext<'_>,
) -> Result<Value, CliDocError> {
    let input_format = resolve_input_format(ctx.input_format).map_err(CliDocError::Usage)?;
    let (root, doc_format) = read_document_input(file, input_format)?;
    let mut payload = serde_json::Map::new();
    payload.insert("code".to_string(), json!("document"));
    payload.insert("format".to_string(), json!(doc_format.cli_name()));
    let json_value: Value = match key {
        None => Value::try_from(root)?,
        Some(key) => {
            let target = get_path(&root, key, &[])?;
            let is_secret = document_path_is_secret(key, ctx.secret_names)?;
            payload.insert("key".to_string(), json!(key));
            // A generic whole-document redact walk (applied via the
            // `--secret-name` output options below) only rewrites object
            // fields it finds by name; the value here sits under the
            // generic `"value"` wrapper key instead of its own field name,
            // so a directly-targeted secret leaf needs this explicit check.
            if is_secret {
                json!("***")
            } else {
                Value::try_from(target)?
            }
        }
    };
    payload.insert("value".to_string(), json_value);
    Ok(Value::Object(payload))
}

/// `value`: like `get` with a required KEY, but writes only the scalar's raw
/// bytes to stdout — no AFDATA envelope, no forced trailing newline. Arrays,
/// objects, and non-finite floats are rejected. A secret-named leaf is
/// rejected unless `--reveal-secret` is passed (never bypassed by default,
/// not even by `--default`). `--default VAL` prints `VAL` instead of erroring
/// when KEY's path does not exist or its value is `null`; an empty string is
/// a real value and does not trigger it (§2).
fn run_value_get(
    file: &Path,
    key: &str,
    reveal_secret: bool,
    default: Option<&str>,
    ctx: &DocumentContext<'_>,
) -> ExitCode {
    finish_raw(compute_value(file, key, reveal_secret, default, ctx), ctx)
}

fn compute_value(
    file: &Path,
    key: &str,
    reveal_secret: bool,
    default: Option<&str>,
    ctx: &DocumentContext<'_>,
) -> Result<Vec<u8>, CliDocError> {
    let input_format = resolve_input_format(ctx.input_format).map_err(CliDocError::Usage)?;
    let (root, _doc_format) = read_document_input(file, input_format)?;
    if !reveal_secret && document_path_is_secret(key, ctx.secret_names)? {
        return Err(CliDocError::runtime(
            "document_secret_redacted",
            format!("path `{key}` names a secret; pass --reveal-secret"),
        ));
    }
    let target = match get_path(&root, key, &[]) {
        Ok(target) => target,
        Err(err) => {
            let err = CliDocError::Document(err);
            if default.is_some() && is_path_not_found(&err) {
                return Ok(default.unwrap_or_default().as_bytes().to_vec());
            }
            return Err(err);
        }
    };
    if target.is_null()
        && let Some(default) = default
    {
        return Ok(default.as_bytes().to_vec());
    }
    if target.is_null() {
        return Err(CliDocError::runtime(
            "document_null_value",
            format!("path `{key}` is null; pass --default to choose replacement output"),
        ));
    }
    document_scalar_bytes(&target, key)
        .map_err(|message| CliDocError::runtime("document_not_scalar", message))
}

/// `paths`/`keys` share everything except how a child name becomes an output
/// line: `Paths` emits the full grammar-escaped dot-path from the root,
/// `Keys` emits the raw child key name / array index.
enum EnumerateMode {
    Paths,
    Keys,
}

/// §1: enumerate the immediate children of the container at `key` (the
/// document's top level when `key` is omitted). Rejects a scalar target (the
/// dual of `value` rejecting a container target). `--missing-ok` swallows
/// only a `document_path_not_found`-coded failure into empty output + exit
/// 0; every other error still fails, matching `value`'s stdout-stays-empty
/// contract (R1).
fn run_enumerate(
    file: &Path,
    key: Option<&str>,
    input_format: Option<&str>,
    missing_ok: bool,
    null_separated: bool,
    format: OutputFormat,
    mode: EnumerateMode,
) -> ExitCode {
    let ctx = DocumentContext {
        input_format,
        secret_names: &[],
        format,
    };
    match compute_enumerate(file, key, &ctx, mode) {
        Ok(lines) => {
            let separator: u8 = if null_separated { 0 } else { b'\n' };
            let mut bytes = Vec::new();
            for line in &lines {
                bytes.extend_from_slice(line.as_bytes());
                bytes.push(separator);
            }
            write_raw_exit(&bytes)
        }
        Err(err) if missing_ok && is_path_not_found(&err) => write_raw_exit(&[]),
        Err(err) => {
            let (event, code) = document_error_event(&err);
            emit_document_event(event, &ctx, code, error_stream(output_to()))
        }
    }
}

fn compute_enumerate(
    file: &Path,
    key: Option<&str>,
    ctx: &DocumentContext<'_>,
    mode: EnumerateMode,
) -> Result<Vec<String>, CliDocError> {
    let input_format = resolve_input_format(ctx.input_format).map_err(CliDocError::Usage)?;
    let (root, _doc_format) = read_document_input(file, input_format)?;
    let base_segments: Vec<String> = match key {
        Some(key) => parse_path(key)?,
        None => Vec::new(),
    };
    let target = match key {
        Some(key) => get_path(&root, key, &[])?,
        None => root,
    };
    let names: Vec<String> = match &target {
        DocumentValue::Object(map) => map.keys().cloned().collect(),
        DocumentValue::Array(items) => (0..items.len()).map(|index| index.to_string()).collect(),
        _ => {
            return Err(CliDocError::runtime(
                "document_not_container",
                format!(
                    "path `{}` is a scalar; nothing to enumerate",
                    key.unwrap_or("<root>")
                ),
            ));
        }
    };
    Ok(names
        .into_iter()
        .map(|name| match mode {
            EnumerateMode::Keys => name,
            EnumerateMode::Paths => {
                let mut segments = base_segments.clone();
                segments.push(name);
                join_path(&segments)
            }
        })
        .collect())
}

/// `set`: write a value at dot-path `key` into `file`, preserving the rest
/// of the document's source formatting. See the `Set` variant's doc comment
/// for the bare-VALUE/`--value-type`/heterogeneous-overwrite-guard contract
/// (§3) and `--secret-from` (D4).
fn run_set(
    file: &Path,
    key: &str,
    value: Option<String>,
    value_type: Option<&str>,
    secret_from: Option<&str>,
    ctx: &DocumentContext<'_>,
) -> ExitCode {
    finish_document(
        compute_set(file, key, value, value_type, secret_from, ctx),
        ctx,
    )
}

fn compute_set(
    file: &Path,
    key: &str,
    value: Option<String>,
    value_type: Option<&str>,
    secret_from: Option<&str>,
    ctx: &DocumentContext<'_>,
) -> Result<Value, CliDocError> {
    reject_mutation_dash(file)?;
    let input_format = resolve_input_format(ctx.input_format).map_err(CliDocError::Usage)?;
    let mut doc = DocumentFile::open(file, input_format)?;
    // Guard the target before consuming a `--secret-from stdin`/`prompt`/`fd:N`
    // source: an unsafe target (symlink, or on unix a hardlink) must be
    // rejected before the secret is read, not after.
    doc.ensure_mutable("set")?;

    let new_value = if let Some(secret_from) = secret_from {
        let source = parse_secret_from(secret_from).map_err(CliDocError::Usage)?;
        DocumentValue::String(read_secret_from(source)?)
    } else if let Some(type_name) = value_type {
        let parsed_type = ValueType::parse(type_name).ok_or_else(|| {
            CliDocError::Usage(format!(
                "invalid --value-type `{type_name}`; expected string, number, bool, null, or json"
            ))
        })?;
        value_from_type(parsed_type, value.as_deref())?
    } else {
        let raw = value.ok_or_else(|| {
            CliDocError::Usage(
                "set requires VALUE, --value-type null, or --secret-from".to_string(),
            )
        })?;
        // Heterogeneous-overwrite guard: a bare VALUE is always a string, so writing one over
        // anything that is not already a string rewrites what is there — an
        // argument error, not a coercion decision. Only a brand-new key or an
        // existing string is unguarded. Containers are included: replacing an
        // array with a string used to exit 0 and discard every element.
        let existing = get_path(doc.value(), key, &[]).ok();
        if let Err(overwrite) = guard_bare_overwrite(existing.as_ref()) {
            let verb = match overwrite {
                BareOverwrite::Container(_) => "discard",
                BareOverwrite::Scalar(_) => "change",
            };
            let keep_instruction = if overwrite.found() == "null" {
                "pass --value-type null without VALUE to keep it".to_string()
            } else {
                format!("pass --value-type {} to keep it", overwrite.keeps())
            };
            return Err(CliDocError::Usage(format!(
                "bare VALUE would silently {verb} the {found} at `{key}`; \
                 {keep_instruction}, or pass --value-type string to replace it explicitly",
                found = overwrite.found(),
            )));
        }
        DocumentValue::String(raw)
    };

    doc.set(key, new_value)?;
    doc.save()?;
    Ok(json!({
        "code": "document_set",
        "format": doc.format().cli_name(),
        "key": key,
        "path": doc.path().display().to_string(),
    }))
}

/// `add`: append an element to the keyed list at dot-path `key` in `file`,
/// preserving the rest of the document's source formatting. Idempotency:
/// adding a `slug` that already exists is an error (`document_slug_exists`).
fn run_add(
    file: &Path,
    key: &str,
    slug: &str,
    slug_field: &str,
    fields: &[String],
    ctx: &DocumentContext<'_>,
) -> ExitCode {
    finish_document(compute_add(file, key, slug, slug_field, fields, ctx), ctx)
}

fn compute_add(
    file: &Path,
    key: &str,
    slug: &str,
    slug_field: &str,
    fields: &[String],
    ctx: &DocumentContext<'_>,
) -> Result<Value, CliDocError> {
    reject_mutation_dash(file)?;
    let input_format = resolve_input_format(ctx.input_format).map_err(CliDocError::Usage)?;
    let mut field_pairs: Vec<(String, DocumentValue)> = Vec::with_capacity(fields.len());
    for field in fields {
        let Some((name, value)) = field.split_once('=') else {
            return Err(CliDocError::Usage(format!(
                "field `{field}` must use FIELD=VALUE"
            )));
        };
        if name.is_empty() {
            return Err(CliDocError::Usage(
                "field name must not be empty".to_string(),
            ));
        }
        // Close the side door: add's FIELD=VALUE is always a string, the same
        // zero-coercion rule as set's bare VALUE — no separate type syntax.
        field_pairs.push((name.to_string(), DocumentValue::String(value.to_string())));
    }
    let mut doc = DocumentFile::open(file, input_format)?;
    doc.add(key, slug, slug_field, &field_pairs)?;
    doc.save()?;
    Ok(json!({
        "code": "document_added",
        "format": doc.format().cli_name(),
        "key": key,
        "slug": slug,
        "path": doc.path().display().to_string(),
    }))
}

/// `remove`: delete the element identified by `slug`/`slug_field` from the
/// keyed list at dot-path `key` in `file`, preserving the rest of the
/// document's source formatting. Idempotency: removing a `slug` that does
/// not exist is an error (`document_slug_not_found`).
fn run_remove(
    file: &Path,
    key: &str,
    slug: &str,
    slug_field: &str,
    ctx: &DocumentContext<'_>,
) -> ExitCode {
    finish_document(compute_remove(file, key, slug, slug_field, ctx), ctx)
}

fn compute_remove(
    file: &Path,
    key: &str,
    slug: &str,
    slug_field: &str,
    ctx: &DocumentContext<'_>,
) -> Result<Value, CliDocError> {
    reject_mutation_dash(file)?;
    let input_format = resolve_input_format(ctx.input_format).map_err(CliDocError::Usage)?;
    let mut doc = DocumentFile::open(file, input_format)?;
    doc.remove(key, slug, slug_field)?;
    doc.save()?;
    Ok(json!({
        "code": "document_removed",
        "format": doc.format().cli_name(),
        "key": key,
        "slug": slug,
        "path": doc.path().display().to_string(),
    }))
}

/// `unset`: remove the entry at dot-path `key` entirely from `file`,
/// preserving the rest of the document's source formatting. Idempotency:
/// unsetting an absent `key` is an error (`document_path_not_found`).
fn run_unset(file: &Path, key: &str, ctx: &DocumentContext<'_>) -> ExitCode {
    finish_document(compute_unset(file, key, ctx), ctx)
}

fn compute_unset(file: &Path, key: &str, ctx: &DocumentContext<'_>) -> Result<Value, CliDocError> {
    reject_mutation_dash(file)?;
    let input_format = resolve_input_format(ctx.input_format).map_err(CliDocError::Usage)?;
    let mut doc = DocumentFile::open(file, input_format)?;
    // `Document::unset` is idempotent, but the `afdata unset` CLI keeps its
    // strict contract: unsetting an absent key is a caught error, so scripts
    // can tell an actual removal from a no-op.
    if !doc.unset(key)? {
        return Err(DocumentError::PathNotFound {
            path: key.to_string(),
        }
        .into());
    }
    doc.save()?;
    Ok(json!({
        "code": "document_unset",
        "format": doc.format().cli_name(),
        "key": key,
        "path": doc.path().display().to_string(),
    }))
}

// ═══════════════════════════════════════════
// D4: --secret-from stdin|prompt|fd:<N>|env:<VAR>
// ═══════════════════════════════════════════

enum SecretSource {
    Stdin,
    Prompt,
    Fd(i32),
    Env(String),
}

/// Parse a `--secret-from` flag value. Descriptor/name shape problems
/// (non-numeric `fd:`, `fd:` below 3, empty `env:` name, an unrecognized
/// source keyword) are usage errors (R2) caught here, before anything is
/// read; a failure actually *reading* the source is a separate runtime
/// concern (see [`read_secret_from`]).
fn parse_secret_from(spec: &str) -> Result<SecretSource, String> {
    match spec {
        "stdin" => Ok(SecretSource::Stdin),
        "prompt" => Ok(SecretSource::Prompt),
        other => {
            if let Some(fd) = other.strip_prefix("fd:") {
                let number = fd.parse::<i32>().map_err(|_| {
                    format!("invalid --secret-from `{spec}`; fd: requires a numeric descriptor")
                })?;
                if number < 3 {
                    return Err(format!(
                        "invalid --secret-from `{spec}`; fd: requires a descriptor >= 3"
                    ));
                }
                Ok(SecretSource::Fd(number))
            } else if let Some(var) = other.strip_prefix("env:") {
                if var.is_empty() {
                    return Err(format!(
                        "invalid --secret-from `{spec}`; env: requires a variable name"
                    ));
                }
                Ok(SecretSource::Env(var.to_string()))
            } else {
                Err(format!(
                    "invalid --secret-from `{spec}`; expected stdin, prompt, fd:<N>, or env:<VAR>"
                ))
            }
        }
    }
}

const MAX_VALUE_SECRET_BYTES: usize = 1024 * 1024;

fn read_secret_from(source: SecretSource) -> Result<String, CliDocError> {
    match source {
        SecretSource::Stdin => read_secret_reader(std::io::stdin().lock(), "stdin"),
        SecretSource::Prompt => {
            #[cfg(unix)]
            {
                read_secret_prompt()
            }
            #[cfg(not(unix))]
            {
                Err(CliDocError::runtime(
                    "document_secret_source_failed",
                    "prompt secret input is unsupported on this platform",
                ))
            }
        }
        SecretSource::Fd(number) => {
            #[cfg(unix)]
            {
                use std::os::unix::io::FromRawFd;
                // SAFETY: ownership is transferred exactly once and the descriptor is closed on drop.
                let file = unsafe { std::fs::File::from_raw_fd(number) };
                read_secret_reader(file, "file descriptor")
            }
            #[cfg(not(unix))]
            {
                let _ = number;
                Err(CliDocError::runtime(
                    "document_secret_source_failed",
                    "raw file descriptors are unsupported on this platform",
                ))
            }
        }
        SecretSource::Env(name) => std::env::var(&name).map_err(|_| {
            CliDocError::runtime(
                "document_secret_source_failed",
                format!("environment variable `{name}` is not set"),
            )
        }),
    }
}

fn read_secret_reader<R: std::io::Read>(reader: R, source: &str) -> Result<String, CliDocError> {
    let mut bytes = Vec::new();
    reader
        .take((MAX_VALUE_SECRET_BYTES + 1) as u64)
        .read_to_end(&mut bytes)
        .map_err(|error| {
            CliDocError::runtime(
                "document_secret_source_failed",
                format!("read secret from {source}: {error}"),
            )
        })?;
    if bytes.len() > MAX_VALUE_SECRET_BYTES {
        return Err(CliDocError::runtime(
            "document_secret_source_failed",
            format!("secret exceeds {MAX_VALUE_SECRET_BYTES} bytes"),
        ));
    }
    String::from_utf8(bytes).map_err(|_| {
        CliDocError::runtime(
            "document_secret_source_failed",
            "secret input must be valid UTF-8",
        )
    })
}

#[cfg(unix)]
fn read_secret_prompt() -> Result<String, CliDocError> {
    use std::io::BufRead;
    let mut tty = std::fs::OpenOptions::new()
        .read(true)
        .write(true)
        .open("/dev/tty")
        .map_err(|error| {
            CliDocError::runtime(
                "document_secret_source_failed",
                format!("open controlling terminal: {error}"),
            )
        })?;
    let status = std::process::Command::new("stty")
        .args(["-echo"])
        .status()
        .map_err(|error| {
            CliDocError::runtime(
                "document_secret_source_failed",
                format!("disable terminal echo: {error}"),
            )
        })?;
    if !status.success() {
        return Err(CliDocError::runtime(
            "document_secret_source_failed",
            "disable terminal echo failed",
        ));
    }
    let _echo_guard = TerminalEchoGuard;
    write!(tty, "Secret: ").map_err(|error| {
        CliDocError::runtime(
            "document_secret_source_failed",
            format!("write prompt: {error}"),
        )
    })?;
    let mut value = String::new();
    let result = {
        let mut reader = std::io::BufReader::new(&mut tty);
        reader.read_line(&mut value)
    }
    .map_err(|error| {
        CliDocError::runtime(
            "document_secret_source_failed",
            format!("read secret from prompt: {error}"),
        )
    });
    let _ = writeln!(tty);
    result?;
    let value = value.trim_end_matches(['\n', '\r']);
    if value.len() > MAX_VALUE_SECRET_BYTES {
        return Err(CliDocError::runtime(
            "document_secret_source_failed",
            format!("secret exceeds {MAX_VALUE_SECRET_BYTES} bytes"),
        ));
    }
    Ok(value.to_string())
}

#[cfg(unix)]
struct TerminalEchoGuard;

#[cfg(unix)]
impl Drop for TerminalEchoGuard {
    fn drop(&mut self) {
        let _ = std::process::Command::new("stty").arg("echo").status();
    }
}

// ═══════════════════════════════════════════
// Lint rules (deterministic AFDATA naming/suffix checks)
// ═══════════════════════════════════════════

fn lint_value(value: &Value, pointer: &str, findings: &mut Vec<Finding>) {
    lint_unsafe_integer(value, pointer, findings);
    match value {
        Value::Object(map) => {
            if let Some(Value::Object(properties)) = map.get("properties") {
                for (name, schema) in properties {
                    lint_schema_property(
                        name,
                        schema,
                        &join_pointer(pointer, "properties"),
                        findings,
                    );
                }
            }
            for (key, child) in map {
                // A JSON Schema `properties` object maps public field names to
                // schema descriptors. Those descriptors are not runtime field
                // values, so applying `lint_suffix_type` to the descriptor
                // object itself would reject every correctly typed suffix
                // property (for example `duration_ms: {"type":"integer"}`).
                // Each property is checked by `lint_schema_property` above and
                // its descriptor is recursively inspected there.
                if key == "properties" && child.is_object() {
                    continue;
                }
                lint_suffix_type(key, child, &join_pointer(pointer, key), findings);
                lint_missing_suffix(key, child, &join_pointer(pointer, key), findings);
                lint_value(child, &join_pointer(pointer, key), findings);
            }
        }
        Value::Array(items) => {
            for (idx, item) in items.iter().enumerate() {
                lint_value(item, &join_pointer(pointer, &idx.to_string()), findings);
            }
        }
        _ => {}
    }
}

fn lint_schema_property(
    name: &str,
    schema: &Value,
    properties_pointer: &str,
    findings: &mut Vec<Finding>,
) {
    let property_pointer = join_pointer(properties_pointer, name);
    let normalized_name = name.to_ascii_lowercase();
    // A JSON Schema is always an object or a boolean. A scalar or array here
    // means the enclosing `properties` object is runtime data that happens to
    // use `properties` as a field name, not a property map, so the value keeps
    // the ordinary runtime suffix check.
    if !matches!(schema, Value::Object(_) | Value::Bool(_)) {
        lint_suffix_type(name, schema, &property_pointer, findings);
        lint_value(schema, &property_pointer, findings);
        return;
    }
    if normalized_name.ends_with("_secret")
        && let Some(obj) = schema.as_object()
    {
        for field in ["default", "example"] {
            if let Some(value) = obj.get(field)
                && !is_redacted_secret_literal(value)
            {
                findings.push(Finding::error(
                    "secret_schema_value_exposed",
                    join_pointer(&property_pointer, field),
                    format!("schema property {name:?} exposes secret {field}"),
                ));
            }
        }
        if let Some(Value::Array(examples)) = obj.get("examples") {
            for (idx, value) in examples.iter().enumerate() {
                if !is_redacted_secret_literal(value) {
                    findings.push(Finding::error(
                        "secret_schema_value_exposed",
                        join_pointer(
                            &join_pointer(&property_pointer, "examples"),
                            &idx.to_string(),
                        ),
                        format!("schema property {name:?} exposes secret example"),
                    ));
                }
            }
        }
    }
    lint_schema_suffix_type(name, schema, &property_pointer, findings);
    lint_value(schema, &property_pointer, findings);
}

fn lint_schema_suffix_type(name: &str, schema: &Value, pointer: &str, findings: &mut Vec<Finding>) {
    let normalized = name.to_ascii_lowercase();
    let (expected, description): (&[&str], &str) = if normalized.ends_with("_bytes") {
        (&["integer"], "an integer byte count")
    } else if normalized.ends_with("_epoch_s") || normalized.ends_with("_epoch_ms") {
        (&["integer"], "an integer epoch timestamp")
    } else if normalized.ends_with("_epoch_ns") {
        (&["string"], "a decimal integer string")
    } else if normalized.ends_with("_sats") || normalized.ends_with("_msats") {
        (
            &["integer", "string"],
            "an integer or decimal integer string",
        )
    } else if normalized.ends_with("_percent") || is_duration_suffix(&normalized) {
        (&["integer", "number"], "a numeric value")
    } else if is_currency_minor_unit_suffix(&normalized) {
        (&["integer"], "an integer currency amount")
    } else if normalized.ends_with("_rfc3339")
        || normalized.ends_with("_url")
        || normalized.ends_with("_bcp47")
        || normalized.ends_with("_rfc3339_date")
        || normalized.ends_with("_rfc3339_time")
        || normalized.ends_with("_utc_offset")
    {
        (&["string"], "a string")
    } else {
        return;
    };

    if !schema_accepts_any_type(schema, expected) {
        findings.push(Finding::error(
            "suffix_type_mismatch",
            join_pointer(pointer, "type"),
            format!("schema property {name:?} must allow {description}"),
        ));
    }
}

fn schema_accepts_any_type(schema: &Value, expected: &[&str]) -> bool {
    let Some(object) = schema.as_object() else {
        // Boolean schemas and unresolved references do not declare a
        // contradictory primitive type at this location.
        return true;
    };

    if let Some(schema_type) = object.get("type") {
        return match schema_type {
            Value::String(value) => expected.contains(&value.as_str()),
            Value::Array(values) => values.iter().any(|value| {
                value
                    .as_str()
                    .is_some_and(|value| expected.contains(&value))
            }),
            _ => true,
        };
    }

    for keyword in ["anyOf", "oneOf"] {
        if let Some(Value::Array(branches)) = object.get(keyword) {
            return branches
                .iter()
                .any(|branch| schema_accepts_any_type(branch, expected));
        }
    }
    if let Some(Value::Array(branches)) = object.get("allOf") {
        return branches
            .iter()
            .all(|branch| schema_accepts_any_type(branch, expected));
    }

    // With no local primitive constraint, a `$ref`, `const`, or other schema
    // keyword may still admit the required type. Do not invent a mismatch.
    true
}

fn is_redacted_secret_literal(value: &Value) -> bool {
    matches!(value, Value::Null) || matches!(value, Value::String(s) if s == "***")
}

/// Every suffix `spec/registry.json` defines, by category.
///
/// Kept in step with the registry by `registry_suffixes_match_the_table`
/// rather than by hand — a suffix added there and missed here would silently
/// turn correctly-named fields into warnings.
const REGISTERED_SUFFIXES: &[(&str, &[&str])] = &[
    (
        "duration",
        &["_ns", "_us", "_ms", "_s", "_minutes", "_hours", "_days"],
    ),
    (
        "timestamp",
        &["_epoch_s", "_epoch_ms", "_epoch_ns", "_rfc3339"],
    ),
    (
        "strict_string",
        &["_rfc3339_date", "_rfc3339_time", "_bcp47", "_utc_offset"],
    ),
    ("size", &["_bytes"]),
    ("percentage", &["_percent"]),
    (
        "currency",
        &[
            "_msats",
            "_sats",
            "_usd_cents",
            "_eur_cents",
            "_jpy",
            "_{code}_cents",
            "_{code}_micro",
        ],
    ),
    ("sensitive", &["_secret", "_url"]),
];

/// Field-name stems that name a dimension the convention has a suffix for.
///
/// Matched against the whole key or its trailing `_`-separated tail, so
/// `request_timeout` counts and `timeout_ms` does not (it already carries one).
const UNSUFFIXED_STEMS: &[(&str, &str)] = &[
    ("timeout", "duration"),
    ("elapsed", "duration"),
    ("duration", "duration"),
    ("ttl", "duration"),
    ("interval", "duration"),
    ("latency", "duration"),
    ("delay", "duration"),
    ("uptime", "duration"),
    ("price", "currency"),
    ("amount", "currency"),
    ("cost", "currency"),
    ("fee", "currency"),
    ("balance", "currency"),
    ("subtotal", "currency"),
    ("revenue", "currency"),
    ("created", "timestamp"),
    ("updated", "timestamp"),
    ("modified", "timestamp"),
    ("expires", "timestamp"),
    ("issued", "timestamp"),
    ("timestamp", "timestamp"),
    ("apikey", "sensitive"),
    ("api_key", "sensitive"),
    ("token", "sensitive"),
    ("password", "sensitive"),
    ("passwd", "sensitive"),
    ("secret", "sensitive"),
    ("credential", "sensitive"),
    ("credentials", "sensitive"),
];

fn has_registered_suffix(key: &str) -> bool {
    let lower = key.to_ascii_lowercase();
    REGISTERED_SUFFIXES
        .iter()
        .flat_map(|(_, suffixes)| suffixes.iter())
        .any(|suffix| match suffix.strip_prefix("_{code}") {
            // `_{code}_cents` / `_{code}_micro` are templated on a currency
            // code, so match the shape rather than the literal.
            Some(tail) => lower
                .strip_suffix(tail)
                .and_then(|rest| rest.rsplit_once('_'))
                .is_some_and(|(_, code)| {
                    code.len() == 3 && code.chars().all(|c| c.is_ascii_alphabetic())
                }),
            None => lower.ends_with(suffix),
        })
}

/// Flag a field whose name announces a dimension but carries no unit.
///
/// This is the question the convention exists to answer — is `timeout` seconds
/// or milliseconds, is `price` dollars or cents, is `created` an id or a date,
/// does `api_key` get redacted — and until now `lint` only checked fields that
/// had already answered it, so a document made entirely of these passed clean.
fn lint_missing_suffix(key: &str, value: &Value, pointer: &str, findings: &mut Vec<Finding>) {
    if value.is_null() || has_registered_suffix(key) {
        return;
    }
    let lower = key.to_ascii_lowercase();
    // `_at` is the common spelling for "this is a time", and it names no unit.
    let category = if lower.ends_with("_at") {
        Some("timestamp")
    } else {
        UNSUFFIXED_STEMS
            .iter()
            .find(|(stem, _)| lower == *stem || lower.ends_with(&format!("_{stem}")))
            .map(|(_, category)| *category)
    };
    let Some(category) = category else {
        return;
    };
    // Only complain where the value could actually carry the dimension: a
    // `timeout` object is a config block, not an unlabelled number.
    let plausible = match category {
        "duration" | "currency" | "size" | "percentage" => value.is_number(),
        "timestamp" => value.is_number() || value.is_string(),
        _ => value.is_string(),
    };
    if !plausible {
        return;
    }
    let suffixes = REGISTERED_SUFFIXES
        .iter()
        .find(|(name, _)| *name == category)
        .map(|(_, suffixes)| suffixes.join(", "))
        .unwrap_or_default();
    let message = if category == "sensitive" {
        format!(
            "`{key}` looks like a credential but is not marked, so it is printed and logged in \
             the clear. Rename it with one of: {suffixes}"
        )
    } else {
        format!(
            "`{key}` names a {category} but carries no unit, so a reader cannot tell what it \
             means without asking. Rename it with one of: {suffixes}"
        )
    };
    findings.push(Finding::warning(
        "missing_suffix",
        pointer.to_string(),
        message,
    ));
}

fn lint_suffix_type(key: &str, value: &Value, pointer: &str, findings: &mut Vec<Finding>) {
    // `null` means the field is absent/unset, not present-with-the-wrong-type:
    // the suffix type constraint below applies only to present, non-null
    // values. Absence may be expressed by omitting the key entirely or by an
    // explicit `null`; both are valid. This mirrors `is_redacted_secret_literal`,
    // which already treats `Value::Null` as a valid absent literal for
    // `_secret` schema properties.
    if value.is_null() {
        return;
    }
    let normalized = key.to_ascii_lowercase();
    let message = if normalized.ends_with("_bytes") {
        if is_non_negative_integer(value) {
            None
        } else {
            Some(format!("{key:?} must be a non-negative integer byte count"))
        }
    } else if normalized.ends_with("_epoch_s") || normalized.ends_with("_epoch_ms") {
        if is_integer(value) {
            None
        } else {
            Some(format!("{key:?} must be an integer epoch timestamp"))
        }
    } else if normalized.ends_with("_epoch_ns") {
        if is_decimal_integer_string(value) {
            None
        } else {
            Some(format!("{key:?} must be a decimal integer string"))
        }
    } else if normalized.ends_with("_sats") || normalized.ends_with("_msats") {
        if is_integer(value) || is_decimal_integer_string(value) {
            None
        } else {
            Some(format!(
                "{key:?} must be an integer or decimal integer string"
            ))
        }
    } else if normalized.ends_with("_percent") {
        if value.is_number() {
            None
        } else {
            Some(format!("{key:?} must be numeric"))
        }
    } else if is_duration_suffix(&normalized) {
        if value.is_number() {
            None
        } else {
            Some(format!("{key:?} must be a numeric duration"))
        }
    } else if is_currency_minor_unit_suffix(&normalized) {
        if is_integer(value) {
            None
        } else {
            Some(format!("{key:?} must be an integer currency amount"))
        }
    } else if normalized.ends_with("_rfc3339") {
        if value.as_str().is_some_and(is_valid_rfc3339) {
            None
        } else if value.is_string() {
            Some(format!(
                "{key:?} must be an RFC 3339 date-time with a mandatory offset (e.g. 2026-02-14T10:30:00Z)"
            ))
        } else {
            Some(format!("{key:?} must be a string"))
        }
    } else if normalized.ends_with("_url") {
        if value.as_str().is_some_and(is_wellformed_url_field) {
            None
        } else if value.is_string() {
            Some(format!(
                "{key:?} must be a single URL (no internal whitespace or bare credentials)"
            ))
        } else {
            Some(format!("{key:?} must be a string"))
        }
    } else if normalized.ends_with("_bcp47") {
        if value.as_str().is_some_and(is_valid_bcp47) {
            None
        } else if value.is_string() {
            Some(format!("{key:?} must be a well-formed BCP 47 language tag"))
        } else {
            Some(format!("{key:?} must be a string"))
        }
    } else if normalized.ends_with("_rfc3339_date") {
        if value.as_str().is_some_and(is_valid_rfc3339_date) {
            None
        } else if value.is_string() {
            Some(format!(
                "{key:?} must be an RFC 3339 full-date (YYYY-MM-DD)"
            ))
        } else {
            Some(format!("{key:?} must be a string"))
        }
    } else if normalized.ends_with("_rfc3339_time") {
        if value.as_str().is_some_and(is_valid_rfc3339_time) {
            None
        } else if value.is_string() {
            Some(format!(
                "{key:?} must be an RFC 3339 partial-time (HH:MM:SS[.fraction], no Z or offset)"
            ))
        } else {
            Some(format!("{key:?} must be a string"))
        }
    } else if normalized.ends_with("_utc_offset") {
        if value.as_str().and_then(normalize_utc_offset).is_some() {
            None
        } else if value.is_string() {
            Some(format!(
                "{key:?} must be a fixed UTC offset (\"UTC\" or ±HH:MM)"
            ))
        } else {
            Some(format!("{key:?} must be a string"))
        }
    } else {
        None
    };
    if let Some(message) = message {
        findings.push(Finding::error(
            "suffix_type_mismatch",
            pointer.to_string(),
            message,
        ));
    }
}

fn lint_unsafe_integer(value: &Value, pointer: &str, findings: &mut Vec<Finding>) {
    let Value::Number(number) = value else {
        return;
    };
    if number.is_i64() {
        let Some(value) = number.as_i64() else {
            return;
        };
        if value.unsigned_abs() > MAX_SAFE_INTEGER {
            findings.push(unsafe_integer_finding(pointer));
        }
    } else if number.is_u64() {
        let Some(value) = number.as_u64() else {
            return;
        };
        if value > MAX_SAFE_INTEGER {
            findings.push(unsafe_integer_finding(pointer));
        }
    }
}

fn unsafe_integer_finding(pointer: &str) -> Finding {
    Finding::error(
        "unsafe_integer",
        pointer.to_string(),
        "integer exceeds JavaScript safe integer range ±(2^53-1)".to_string(),
    )
}

fn is_integer(value: &Value) -> bool {
    matches!(value, Value::Number(number) if number.is_i64() || number.is_u64())
}

fn is_non_negative_integer(value: &Value) -> bool {
    matches!(value, Value::Number(number) if number.as_u64().is_some())
}

fn is_decimal_integer_string(value: &Value) -> bool {
    let Value::String(text) = value else {
        return false;
    };
    let digits = text.strip_prefix('-').unwrap_or(text);
    !digits.is_empty() && digits.chars().all(|ch| ch.is_ascii_digit())
}

/// A numeric duration suffix (`timeout_s`, `retry_after_ms`, `ttl_minutes`, …).
/// The epoch suffixes (`_epoch_s`/`_epoch_ms`/`_epoch_ns`) are matched earlier in
/// the chain, so they never reach here.
fn is_duration_suffix(key: &str) -> bool {
    key.ends_with("_ns")
        || key.ends_with("_us")
        || key.ends_with("_ms")
        || key.ends_with("_s")
        || key.ends_with("_minutes")
        || key.ends_with("_hours")
        || key.ends_with("_days")
}

/// An integer minor-unit currency suffix (`price_usd_cents`, `fee_jpy`,
/// `budget_btc_micro`, …). `_sats`/`_msats` allow a decimal-string form and are
/// matched earlier in the chain.
fn is_currency_minor_unit_suffix(key: &str) -> bool {
    key.ends_with("_cents") || key.ends_with("_micro") || key.ends_with("_jpy")
}

/// True when a `_url` field value is a single URL: a scheme-prefixed absolute URL,
/// or a schemeless relative reference with no internal whitespace and no bare `@`
/// credential sigil. This mirrors the redaction gate in the library's
/// `redaction.rs`: a value this rejects is exactly one redaction would blanket-
/// redact (internal whitespace, or a schemeless `user:pass@host` connection
/// string) rather than surgically clean.
fn is_wellformed_url_field(s: &str) -> bool {
    if is_scheme_prefixed_url(s) || is_scheme_prefixed_url(s.trim()) {
        return true;
    }
    !s.chars().any(char::is_whitespace) && !s.contains('@')
}

/// True when `s` begins with a URL scheme (`ALPHA *(ALPHA / DIGIT / "+" / "-" /
/// ".") "://"`) and contains no ASCII whitespace — a single bare absolute URL.
fn is_scheme_prefixed_url(s: &str) -> bool {
    if s.bytes().any(|b| b.is_ascii_whitespace()) {
        return false;
    }
    let bytes = s.as_bytes();
    if !bytes.first().is_some_and(u8::is_ascii_alphabetic) {
        return false;
    }
    let mut i = 1;
    while i < bytes.len() {
        let c = bytes[i];
        if c.is_ascii_alphanumeric() || matches!(c, b'+' | b'-' | b'.') {
            i += 1;
        } else {
            break;
        }
    }
    s[i..].starts_with("://")
}

fn join_pointer(base: &str, token: &str) -> String {
    let escaped = token.replace('~', "~0").replace('/', "~1");
    if base.is_empty() {
        format!("/{escaped}")
    } else {
        format!("{base}/{escaped}")
    }
}

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

    /// The missing-suffix heuristic decides a field is unlabelled by *not*
    /// finding a registered suffix on it. A suffix added to the registry and
    /// missed here would start warning about correctly-named fields, so the two
    /// are compared rather than trusted.
    #[test]
    fn registry_suffixes_match_the_table() {
        const REGISTRY: &str =
            include_str!("../../skills/agent-first-data/references/registry.json");
        let registry: Value = serde_json::from_str(REGISTRY).unwrap();
        let mut from_registry: Vec<(String, String)> = registry["suffixes"]
            .as_array()
            .unwrap()
            .iter()
            .map(|entry| {
                (
                    entry["category"].as_str().unwrap().to_string(),
                    entry["suffix"].as_str().unwrap().to_string(),
                )
            })
            .collect();
        let mut from_table: Vec<(String, String)> = REGISTERED_SUFFIXES
            .iter()
            .flat_map(|(category, suffixes)| {
                suffixes
                    .iter()
                    .map(move |suffix| ((*category).to_string(), (*suffix).to_string()))
            })
            .collect();
        from_registry.sort();
        from_table.sort();
        assert_eq!(from_table, from_registry);
    }

    #[test]
    fn missing_suffix_reads_intent_from_the_name() {
        let mut findings = Vec::new();
        lint_missing_suffix("timeout", &json!(5000), "/timeout", &mut findings);
        lint_missing_suffix(
            "request_timeout",
            &json!(1),
            "/request_timeout",
            &mut findings,
        );
        lint_missing_suffix("expires_at", &json!(1), "/expires_at", &mut findings);
        assert_eq!(findings.len(), 3);
        assert!(findings.iter().all(|finding| finding.severity == "warning"));

        // Already labelled, so there is nothing to ask about.
        let mut clean = Vec::new();
        lint_missing_suffix("timeout_ms", &json!(5000), "/timeout_ms", &mut clean);
        lint_missing_suffix("price_gbp_cents", &json!(1), "/price_gbp_cents", &mut clean);
        lint_missing_suffix(
            "api_key_secret",
            &json!("sk"),
            "/api_key_secret",
            &mut clean,
        );
        // A dimension name over a container is a config block, not a bare number.
        lint_missing_suffix("timeout", &json!({"connect": 1}), "/timeout", &mut clean);
        // An unrelated name says nothing about a dimension.
        lint_missing_suffix("name", &json!("demo"), "/name", &mut clean);
        assert!(clean.is_empty(), "{clean:?}");
    }

    #[test]
    fn every_registered_afdata_shape_round_trips() {
        let cli = afdata_cli_spec().unwrap();
        let fixtures = cli.synthetic_invocations();
        assert!(!fixtures.is_empty());
        for fixture in fixtures {
            let outcome = cli.resolve_from(fixture.argv).unwrap();
            let CliOutcome::Run(invocation) = outcome else {
                panic!("synthetic afdata fixture did not resolve to Run");
            };
            assert_eq!(invocation.combination_id(), fixture.combination_id);
        }
    }

    #[test]
    fn set_conflicts_are_closed_world_errors() {
        let cli = afdata_cli_spec().unwrap();
        let error = cli
            .resolve_from([
                "afdata",
                "set",
                "data.json",
                "key",
                "visible",
                "--secret-from",
                "env:VALUE_SECRET",
            ])
            .unwrap_err();
        assert_eq!(
            error.rule,
            agent_first_data::CliErrorRule::UnregisteredCombination
        );
        assert!(!error.message.contains("VALUE_SECRET"));
    }

    #[test]
    fn raw_commands_reject_protocol_output_arguments() {
        let cli = afdata_cli_spec().unwrap();
        for command in ["value", "paths", "keys"] {
            let mut argv = vec!["afdata", command, "data.json"];
            if command == "value" {
                argv.push("key");
            }
            argv.extend(["--output", "json"]);
            let error = cli.resolve_from(argv).unwrap_err();
            assert_eq!(
                error.rule,
                agent_first_data::CliErrorRule::UnregisteredCombination
            );
        }
    }
}

#[cfg(all(test, feature = "skill"))]
mod skill_tests {
    use super::*;

    #[test]
    fn parses_and_validates_skill_directory() {
        let cli = afdata_cli_spec().unwrap();
        let parsed = cli
            .resolve_from(["afdata", "skill", "validate", "skills/agent-first-data"])
            .unwrap();
        let CliOutcome::Run(invocation) = parsed else {
            panic!("expected a resolved skill validation");
        };
        assert_eq!(invocation.action_id(), "skill_validate");
        assert_eq!(
            invocation.required("input").as_str(),
            Some("skills/agent-first-data")
        );

        let loaded = read_skill_input(Path::new("skills/agent-first-data"));
        assert!(matches!(
            loaded,
            Ok((text, Some(expected_name), _))
                if expected_name == "agent-first-data"
                    && agent_first_data::skill::validate_skill_named(&text, &expected_name).is_ok()
        ));
    }

    #[cfg(feature = "skill-admin")]
    #[test]
    fn resolves_registered_skill_admin_combination() {
        let cli = afdata_cli_spec().unwrap();
        let parsed = cli
            .resolve_from([
                "afdata",
                "skill",
                "status",
                "--agent",
                "opencode",
                "--scope",
                "workspace",
            ])
            .unwrap();
        let CliOutcome::Run(invocation) = parsed else {
            panic!("expected a resolved skill status");
        };
        assert_eq!(invocation.combination_id(), "skill-status-agent");
        assert_eq!(invocation.required("agent").as_str(), Some("opencode"));
        assert_eq!(invocation.required("scope").as_str(), Some("workspace"));
    }

    #[cfg(feature = "skill-admin")]
    #[test]
    fn admin_subcommands_reject_a_validate_only_input_path() {
        let cli = afdata_cli_spec().unwrap();
        let parsed = cli.resolve_from(["afdata", "skill", "status", "some/path"]);
        assert!(parsed.is_err());
    }
}