shikumi 0.1.71

Shikumi (仕組み) — config discovery, hot-reload, and ArcSwap store for Nix-managed desktop apps
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
//! Unified secret-client abstraction.
//!
//! `SecretClient` is a single async trait that every vault backend
//! implements. Consumers write against `Arc<dyn SecretClient>` and swap
//! backends without rewriting code.
//!
//! The free functions in `crate::secret` remain the low-level API
//! (direct resolvers per backend, CLI or native). This module layers an
//! object-safe trait on top so daemons can depend on the abstraction
//! and wire up a concrete backend at startup.
//!
//! # Capability matrix
//!
//! Not every backend supports every operation. Methods that aren't
//! universally available return [`SecretError::Unsupported`] by default
//! — the trait advertises capabilities via [`SecretClient::capabilities`]
//! so consumers can check up-front.
//!
//! | Backend | get | list | put | delete | rotate | versions |
//! |---|---|---|---|---|---|---|
//! | MemClient (in-memory) | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
//! | CommandClient (shell) | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ |
//! | AkeylessClient | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
//! | AwsClient | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
//! | OpConnectClient | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
//! | VaultClient (KV v2) | ✅ | ✅ | ✅ | ✅ | ⚠️ (engine) | ✅ |
//! | GcpSecretClient | ✅ | ✅ | ✅ | ✅ | ❌ | ✅ |
//! | SOPS (file-based) | stays CLI-only (no HTTP API) |
//!
//! "✅" = implemented. "⚠️" = supported but backend-specific.
//! "❌" = fundamentally unsupported.
//! "planned" = queued in RFC 0001 (`op-connect-api`, `vault-api`,
//! `gcp-secretmanager-api` need generating via forge-gen).
//!
//! "✅" = universally supported via the backend's API.
//! "⚠️" = supported but with backend-specific caveats (see per-impl docs).
//! "❌" = not supported — returns [`SecretError::Unsupported`].
//!
//! # Usage
//!
//! ```no_run
//! # use std::sync::Arc;
//! # use shikumi::secret_client::{SecretClient, MemClient};
//! # async fn demo() -> Result<(), Box<dyn std::error::Error>> {
//! let client: Arc<dyn SecretClient> = Arc::new(MemClient::new());
//! client.put("jwt_secret", "dev-only").await?;
//! let value = client.get("jwt_secret").await?;
//! assert_eq!(value, "dev-only");
//! # Ok(()) }
//! ```

use std::collections::HashMap;
use std::sync::RwLock;

use async_trait::async_trait;

use crate::error::ShikumiError;

/// Typed error for secret-client operations.
///
/// Callers can match on specific variants for retry logic (e.g. retry
/// `NotFound` but not `Unauthorized`) instead of parsing strings.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum SecretError {
    /// The secret does not exist on the backend.
    #[error("secret not found: {name}")]
    NotFound { name: String },

    /// The caller lacks permission to perform this operation.
    #[error("unauthorized: {message}")]
    Unauthorized { message: String },

    /// The backend does not support this operation (e.g. SOPS doesn't
    /// do `list`; shell commands don't do `put`).
    #[error("{backend} does not support {operation}")]
    Unsupported {
        backend: &'static str,
        operation: &'static str,
    },

    /// Transport / network / serialization error.
    #[error("backend error: {0}")]
    Backend(String),

    /// Pass-through shikumi error (used by the command/CLI backends).
    #[error(transparent)]
    Shikumi(#[from] ShikumiError),
}

impl SecretError {
    /// Retryable errors: network hiccups, rate limits, 5xx responses.
    #[must_use]
    pub fn is_retryable(&self) -> bool {
        matches!(self, Self::Backend(msg) if msg.contains("timeout") || msg.contains("5"))
    }

    /// Construct an [`Self::Unsupported`] from a typed [`SecretOperation`]
    /// — the canonical [`SecretOperation::as_str`] string becomes the
    /// `operation` field.
    ///
    /// The closed constructor that names every `Unsupported` site through
    /// one [`SecretOperation`] variant rather than a `&'static str`
    /// literal. Default trait impls on [`SecretClient`] route through
    /// this constructor, so the operation-name strings live at one site
    /// ([`SecretOperation::as_str`]) instead of being re-stated at each
    /// `Err(SecretError::Unsupported { operation: "list" })` arm.
    ///
    /// A future operation landing on [`SecretOperation`] (`Metadata`,
    /// `Tags`, etc.) extends the canonical-label site once and every
    /// default trait impl using this constructor picks the new label up
    /// without per-site edits.
    #[must_use]
    pub const fn unsupported(backend: &'static str, op: SecretOperation) -> Self {
        Self::Unsupported {
            backend,
            operation: op.as_str(),
        }
    }

    /// Closed-enum classification of this error's variant — the typed
    /// kind partition over the [`SecretError`] variant space.
    ///
    /// One source of truth for the kind axis: consumers route on the
    /// returned [`SecretErrorKind`] (in `match`, `HashMap` keys, log
    /// labels, alerting buckets, retry-policy dispatch tables, telemetry
    /// recording the per-kind refusal mix across backends, attestation
    /// manifests recording the kind histogram of secret-resolution
    /// failures) instead of pattern-matching the five payload-carrying
    /// variants by hand at every observation site. Equivalent to
    /// `matches!` on the underlying variant — but the closed-enum
    /// return value composes further (it's `Copy + Eq + Hash + 'static`),
    /// where a `matches!` predicate does not, and crosses thread
    /// boundaries the borrowed payloads cannot.
    ///
    /// Peer projection to [`ShikumiError::kind`] on the [`ShikumiError`]
    /// variant space — same typescape discipline (closed exhaustive
    /// match, `'static` codomain, `Copy + Eq + Hash`) applied to the
    /// secret-client error axis. The two error kinds compose
    /// structurally: a [`Self::Shikumi`] error carries a [`ShikumiError`]
    /// whose own [`ShikumiError::kind`] refines the partition further on
    /// the wrapped-shikumi sub-axis, so observers wanting the underlying
    /// shikumi kind on a wrapped error read
    /// `err.as_shikumi().map(ShikumiError::kind)` without inlining a
    /// per-variant pattern match.
    ///
    /// The implementation is one exhaustive `match`, so a future
    /// [`SecretError`] variant landing forces a corresponding
    /// [`SecretErrorKind`] variant in lockstep at compile time — the
    /// kind partition stays coherent by construction.
    #[must_use]
    pub const fn kind(&self) -> SecretErrorKind {
        match self {
            Self::NotFound { .. } => SecretErrorKind::NotFound,
            Self::Unauthorized { .. } => SecretErrorKind::Unauthorized,
            Self::Unsupported { .. } => SecretErrorKind::Unsupported,
            Self::Backend(_) => SecretErrorKind::Backend,
            Self::Shikumi(_) => SecretErrorKind::Shikumi,
        }
    }

    /// Borrow the underlying [`ShikumiError`] if this is a
    /// [`Self::Shikumi`] pass-through, else `None`.
    ///
    /// One source of truth for the (`SecretError → wrapped-shikumi`)
    /// partial projection. Consumers wanting to refine the kind
    /// partition on the wrapped-shikumi sub-axis (via
    /// [`ShikumiError::kind`]) read
    /// `err.as_shikumi().map(ShikumiError::kind)` through this accessor
    /// instead of inlining `if let Self::Shikumi(inner) = err { … }` at
    /// every cross-kind dispatch site. Dual to [`Self::kind`]'s
    /// `Self::Shikumi` arm — `as_shikumi().is_some()` ↔
    /// `kind() == SecretErrorKind::Shikumi` by construction.
    #[must_use]
    pub const fn as_shikumi(&self) -> Option<&ShikumiError> {
        match self {
            Self::Shikumi(inner) => Some(inner),
            _ => None,
        }
    }
}

/// Data-free, `'static` discriminant of [`SecretError`]: the kind of
/// secret-client error independent of the payload-carrying fields.
///
/// Closed five-way partition over the [`SecretError`] variant space,
/// returned by [`SecretError::kind`]. The enum exists so consumers that
/// care only about the kind axis (per-kind retry-policy dispatch,
/// per-kind telemetry counters, structured-diagnostic legends naming
/// the failing kind, alerting buckets histogramming refusal classes
/// across backends, attestation manifests recording the kind mix of
/// secret-resolution failures, cross-thread log fields naming the kind
/// after the borrowed [`SecretError`] payload has been dropped) match
/// on one closed enum instead of pattern-matching against the
/// payload-carrying [`SecretError`] (whose `Backend(String)` and
/// `Shikumi(ShikumiError)` payloads hold owned data that cannot be
/// trivially cloned for cross-thread observation).
///
/// Peer of [`crate::ShikumiErrorKind`] on the [`crate::ShikumiError`]
/// variant axis, and of the other closed-enum kind primitives
/// ([`crate::SecretBackendKind`] on the secret-resolution backend axis,
/// [`crate::SecretRefShape`] on the cross-type ref-extraction-shape
/// axis, [`SecretOperation`] on the cross-surface operation axis,
/// [`crate::ConfigSourceKind`] on the layer axis,
/// [`crate::FigmentSourceKind`] / [`crate::FigmentNameTagKind`] on
/// the figment-`Metadata::{source, name}` axes): same typescape
/// discipline (closed, allocation-free,
/// `Copy + Eq + Hash + #[non_exhaustive]`, exhaustive forward map),
/// applied to the secret-client error axis.
///
/// `'static` and allocation-free — survives the borrow on the
/// originating [`SecretError`]'s owned payloads and can therefore
/// cross thread boundaries, serialize, and live in long-lived
/// structures the way [`crate::ShikumiErrorKind`] does on the
/// captured cross-thread observable form of [`crate::ReloadFailure`].
///
/// Adding a future [`SecretError`] variant means adding one
/// [`SecretErrorKind`] variant in lockstep — the exhaustive
/// [`SecretError::kind`] match forces the assignment at compile time.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum SecretErrorKind {
    /// Maps to [`SecretError::NotFound`] regardless of inner secret
    /// name. The backend confirmed the secret does not exist on the
    /// store — distinct from [`Self::Backend`] (transport failure) and
    /// [`Self::Unauthorized`] (permission denied).
    NotFound,
    /// Maps to [`SecretError::Unauthorized`] regardless of inner
    /// message. The caller lacks permission for the operation — distinct
    /// from [`Self::Unsupported`] (the backend cannot perform the
    /// operation at all, regardless of caller).
    Unauthorized,
    /// Maps to [`SecretError::Unsupported`] regardless of inner backend
    /// or operation tags. The backend does not advertise the requested
    /// operation in its [`Capabilities`] — pairs with [`SecretOperation`]
    /// on the operation axis ([`SecretError::unsupported`] is the
    /// canonical constructor, naming the operation through one typed
    /// primitive).
    Unsupported,
    /// Maps to [`SecretError::Backend`] regardless of inner message.
    /// Transport, network, or serialization failure — the catch-all for
    /// backend-side faults that aren't captured by the structural kinds
    /// above. The only kind [`SecretError::is_retryable`] currently
    /// returns `true` for (on timeout / 5xx substring match).
    Backend,
    /// Maps to [`SecretError::Shikumi`] regardless of inner
    /// [`ShikumiError`] variant. Pass-through wrapper for errors
    /// originating in the [`crate::secret`] resolver layer or the
    /// CLI/shell backends — observers wanting the underlying
    /// [`ShikumiError`] variant refine the partition via
    /// [`SecretError::as_shikumi`] +
    /// [`crate::ShikumiError::kind`].
    Shikumi,
}

impl SecretErrorKind {
    /// Every [`SecretErrorKind`] variant, in the same declaration order
    /// as the [`SecretError`] arms in [`SecretError::kind`]
    /// ([`Self::NotFound`], [`Self::Unauthorized`], [`Self::Unsupported`],
    /// [`Self::Backend`], [`Self::Shikumi`]).
    ///
    /// The closed list of error kinds the secret-client surface
    /// recognizes today, in the same declaration order as the
    /// [`SecretError`] variant list. Iterate to enumerate the kind space
    /// without listing variants by hand at every consumer site — e.g.
    /// dashboards initializing per-kind retry-policy buckets, attestation
    /// manifests recording the failure-mix histogram across backends,
    /// CLI flag values listing the filterable kind set, partition-
    /// coverage tests asserting disjointness over the whole universe.
    ///
    /// One source of truth for the kind enumeration on the
    /// [`SecretErrorKind`] axis: peer to [`crate::ShikumiErrorKind::ALL`]
    /// on the [`crate::ShikumiError`] variant axis, the same typescape
    /// discipline applied across the closed-enum primitive set.
    ///
    /// Adding a new variant to [`Self`] means extending this slice in
    /// lockstep with the variant itself. The compiler enforces nothing
    /// here directly, so the `secret_error_kind_all_covers_every_variant`
    /// test pins the contract by asserting that every kind produced by
    /// [`SecretError::kind`] over the construction-table surface appears
    /// in [`Self::ALL`], and the `secret_error_kind_all_has_no_duplicates`
    /// test pins that the constant is a set (no double-listed variant).
    pub const ALL: &'static [Self] = &[
        Self::NotFound,
        Self::Unauthorized,
        Self::Unsupported,
        Self::Backend,
        Self::Shikumi,
    ];

    /// Canonical operator-facing lowercase name of the error kind —
    /// [`Self::NotFound`] renders as `"not-found"`, [`Self::Unauthorized`]
    /// as `"unauthorized"`, [`Self::Unsupported`] as `"unsupported"`,
    /// [`Self::Backend`] as `"backend"`, [`Self::Shikumi`] as
    /// `"shikumi"`.
    ///
    /// Single source of truth for the five canonical strings on the
    /// secret-client kind axis. Inherent mirror of the
    /// [`crate::ClosedAxisLabel`] trait method; the trait impl delegates
    /// here so the canonical names live at one site instead of being
    /// re-stated at every operator-facing surface (a future structured-
    /// log field naming the surfaced kind, a CLI flag filtering captured
    /// failures by kind, a per-kind retry-policy dispatch table, an
    /// alerting bucket histogramming the kind partition, an attestation
    /// manifest recording the kind histogram).
    ///
    /// Kebab-case for the compound-noun variant [`Self::NotFound`]
    /// (`"not-found"`) — the same convention shared with
    /// [`crate::ShikumiErrorKind::as_str`]
    /// ([`crate::ShikumiErrorKind::NotFound`] → `"not-found"`),
    /// [`crate::FormatProvenance::as_str`] (`"figment-builtin"` /
    /// `"shikumi-built"`), and [`crate::AttributionAxis::as_str`]
    /// (`"metadata-source"` / `"metadata-name"`): compound-noun variant
    /// identifiers route the punctuation at the type level
    /// (operator-facing string) rather than at the call site. The
    /// remaining four single-word variants render as their lowercase
    /// identifier ([`Self::Unauthorized`] → `"unauthorized"`,
    /// [`Self::Unsupported`] → `"unsupported"`, [`Self::Backend`] →
    /// `"backend"`, [`Self::Shikumi`] → `"shikumi"`), matching the
    /// single-word lowercase convention shared with the sibling kind
    /// primitives.
    ///
    /// Pairs with [`crate::ClosedAxisLabel::from_canonical_str`] via the
    /// trait-default linear-scan parse; the round-trip law
    /// `Self::from_canonical_str(v.as_str()) == Some(v)` holds for every
    /// variant uniformly through the trait-uniform
    /// `closed_axis_label_round_trips_for_every_implementor` test in
    /// `cube::tests`. The concrete-position pin at
    /// `secret_error_kind_as_str_yields_canonical_lowercase_names` holds
    /// the literal strings stable so a future rename (e.g. capitalizing
    /// `"NotFound"`, switching `"backend"` to `"transport"`) fails at
    /// that site before drifting through the round-trip law.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::NotFound => "not-found",
            Self::Unauthorized => "unauthorized",
            Self::Unsupported => "unsupported",
            Self::Backend => "backend",
            Self::Shikumi => "shikumi",
        }
    }
}

impl crate::ClosedAxis for SecretErrorKind {
    const ALL: &'static [Self] = Self::ALL;
}

impl crate::ClosedAxisLabel for SecretErrorKind {
    fn as_str(self) -> &'static str {
        Self::as_str(self)
    }
}

/// Operations a [`SecretClient`] backend may expose — the closed
/// six-way axis over the (Capabilities-field × default-trait-method
/// × [`SecretError::Unsupported`]-tag) cross-surface space.
///
/// Three surfaces previously named the same operation universe
/// independently:
///
/// - [`Capabilities`]'s six `bool` fields (`get`, `list`, `put`,
///   `delete`, `rotate`, `versions`) — the advertised capability the
///   backend claims to support.
/// - The six default trait methods on [`SecretClient`] (`get`, `list`,
///   `put`, `delete`, `rotate`, `get_version`) — the actual dispatch
///   point.
/// - The five `Err(SecretError::Unsupported { operation: "X", .. })`
///   arms each default impl raised (`"list"`, `"put"`, `"delete"`,
///   `"rotate"`, `"get_version"`) — the operator-facing label naming
///   which operation the backend refused.
///
/// The three-way agreement was implicit in the dispatch table only —
/// a future operation landing meant editing the [`Capabilities`]
/// struct, adding a default trait method, and inventing a fresh
/// magic-string label in lockstep, with nothing in the type system
/// pinning the alignment. Lifting the universe to one typed primitive
/// closes the cross-surface agreement: every operation has exactly
/// one [`SecretOperation`] variant, [`Capabilities::supports`] picks
/// the matching field by closed-enum dispatch, and
/// [`SecretError::unsupported`] uses [`Self::as_str`] for the
/// operator-facing label. A future variant landing (e.g. a hypothetical
/// `Metadata` operation pairing with a `metadata` Capabilities flag
/// and a default `metadata` trait method) lands as one new arm on
/// each of the three surfaces, with the [`SecretOperation`] enum
/// forcing the assignment at compile time.
///
/// Closed-axis discipline: `Copy + Eq + Hash + #[non_exhaustive]`,
/// allocation-free, [`crate::ClosedAxis`] + [`crate::ClosedAxisLabel`]
/// — same trait-bounds parity as [`SecretBackendKind`] /
/// [`crate::SecretRefShape`] on the secret-axis primitives, and as
/// [`crate::ConfigSourceKind`] / [`crate::FigmentSourceKind`] /
/// [`crate::FigmentNameTagKind`] on the resolution-axis primitives.
///
/// [`SecretBackendKind`]: crate::secret::SecretBackendKind
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum SecretOperation {
    /// Read the current secret value — [`SecretClient::get`]. Maps to
    /// [`Capabilities::get`]. Every backend supports `Get`; the variant
    /// exists for symmetry on the axis (so [`Capabilities::supports`]
    /// is total over [`Self::ALL`]) and so a future per-operation
    /// telemetry surface need not special-case the read path.
    Get,
    /// Enumerate secret names — [`SecretClient::list`]. Maps to
    /// [`Capabilities::list`].
    List,
    /// Create or update a secret — [`SecretClient::put`]. Maps to
    /// [`Capabilities::put`].
    Put,
    /// Delete a secret — [`SecretClient::delete`]. Maps to
    /// [`Capabilities::delete`].
    Delete,
    /// Trigger backend-side rotation — [`SecretClient::rotate`]. Maps
    /// to [`Capabilities::rotate`].
    Rotate,
    /// Fetch a specific historical version — [`SecretClient::get_version`].
    /// Maps to [`Capabilities::versions`]. The Capabilities field's
    /// `versions` plural and the trait method's `get_version` singular
    /// previously disagreed at the string level; [`SecretOperation`]
    /// names the operation once and both surfaces project onto it.
    GetVersion,
}

impl SecretOperation {
    /// Every [`SecretOperation`] variant, in declaration order
    /// ([`Self::Get`], [`Self::List`], [`Self::Put`], [`Self::Delete`],
    /// [`Self::Rotate`], [`Self::GetVersion`]).
    ///
    /// The closed list of operations the [`SecretClient`] surface
    /// recognizes today — same six entries as [`Capabilities`]'s field
    /// set, in the same declaration order. Adding a new variant means
    /// extending this slice in lockstep with the variant itself; the
    /// `secret_operation_all_*` tests pin the contract.
    pub const ALL: &'static [Self] = &[
        Self::Get,
        Self::List,
        Self::Put,
        Self::Delete,
        Self::Rotate,
        Self::GetVersion,
    ];

    /// Canonical operator-facing `snake_case` name — `"get"`, `"list"`,
    /// `"put"`, `"delete"`, `"rotate"`, or `"get_version"`.
    ///
    /// The single source of truth for the operation-name strings the
    /// [`SecretError::Unsupported`] arm carries on its `operation`
    /// field. The labels coincide with the [`SecretClient`] trait
    /// method names pointwise (rather than with the [`Capabilities`]
    /// field names, which would render `"versions"` for the `versions`
    /// field — disagreeing with the trait method's `get_version`
    /// singular). Picking the trait-method shape keeps the
    /// [`SecretError::Unsupported`] message (`"backend X does not
    /// support get_version"`) naming the same identifier an operator
    /// would call from code, instead of the Capabilities-side plural
    /// that has no matching dispatch site.
    ///
    /// Pairs with [`crate::ClosedAxisLabel::from_canonical_str`] via
    /// the trait-default linear-scan parse; the round-trip law
    /// `Self::from_canonical_str(v.as_str()) == Some(v)` holds for
    /// every variant uniformly through the trait-uniform
    /// `closed_axis_label_round_trips_for_every_implementor` test in
    /// `cube::tests`.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Get => "get",
            Self::List => "list",
            Self::Put => "put",
            Self::Delete => "delete",
            Self::Rotate => "rotate",
            Self::GetVersion => "get_version",
        }
    }

    /// Whether `caps` advertises this operation — the typed projection
    /// of [`SecretOperation`] onto the matching [`Capabilities`] field.
    ///
    /// Dual of [`Capabilities::supports`]; the two methods delegate to
    /// the same arm by symmetry. Consumers that carry a
    /// [`SecretOperation`] (e.g. dispatch-side code deciding "should I
    /// call `.list()` on this client?") read the capability through
    /// this projection without inlining a six-arm `match` over the
    /// Capabilities boolean fields at each site.
    #[must_use]
    pub const fn is_supported_by(self, caps: Capabilities) -> bool {
        match self {
            Self::Get => caps.get,
            Self::List => caps.list,
            Self::Put => caps.put,
            Self::Delete => caps.delete,
            Self::Rotate => caps.rotate,
            Self::GetVersion => caps.versions,
        }
    }
}

impl crate::ClosedAxis for SecretOperation {
    const ALL: &'static [Self] = Self::ALL;
}

impl crate::ClosedAxisLabel for SecretOperation {
    fn as_str(self) -> &'static str {
        Self::as_str(self)
    }
}

/// Which operations a [`SecretClient`] backend supports.
///
/// Queried via [`SecretClient::capabilities`]. Daemons that need
/// write-access can reject read-only clients at startup instead of
/// discovering the limitation at the first `put()` call.
///
/// Projects onto the [`SecretOperation`] axis via [`Self::supports`] —
/// `caps.supports(SecretOperation::Foo)` reads the matching boolean
/// field by closed-enum dispatch, so consumers that carry a typed
/// [`SecretOperation`] (per-operation telemetry, dispatch-side
/// "should I call this method?" gating, attestation manifests
/// recording the operation mix of refused calls) read the capability
/// through one projection instead of pattern-matching the six
/// boolean fields by name at each site.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Capabilities {
    /// Read operations always supported — every backend can `get`.
    /// Maps to [`SecretOperation::Get`].
    pub get: bool,
    /// Enumerate secrets by prefix. Maps to [`SecretOperation::List`].
    pub list: bool,
    /// Create or update a secret. Maps to [`SecretOperation::Put`].
    pub put: bool,
    /// Delete a secret. Maps to [`SecretOperation::Delete`].
    pub delete: bool,
    /// Trigger backend-side rotation. Maps to [`SecretOperation::Rotate`].
    pub rotate: bool,
    /// Read historical versions. Maps to [`SecretOperation::GetVersion`]
    /// — note the (Capabilities-side plural × trait-side singular)
    /// disagreement that [`SecretOperation`] reconciles structurally.
    pub versions: bool,
}

impl Capabilities {
    /// Read-only capability set (get only). Used by CLI/shell backends
    /// that only invoke a read command.
    #[must_use]
    pub const fn read_only() -> Self {
        Self {
            get: true,
            list: false,
            put: false,
            delete: false,
            rotate: false,
            versions: false,
        }
    }

    /// Full read+write capability set. Used by native HTTP backends
    /// with complete API coverage (Akeyless, AWS, Vault, GCP).
    #[must_use]
    pub const fn full() -> Self {
        Self {
            get: true,
            list: true,
            put: true,
            delete: true,
            rotate: true,
            versions: true,
        }
    }

    /// Whether this capability set advertises `op` — the typed
    /// projection from [`SecretOperation`] onto the matching boolean
    /// field.
    ///
    /// One source of truth for the (operation × capability) dispatch
    /// surface. Mirrors [`SecretOperation::is_supported_by`] in the
    /// dual direction; both methods delegate to the same arm by
    /// symmetry. Dispatch-side consumers (a `daemon.has_write_access()`
    /// gate that checks `caps.supports(SecretOperation::Put)` rather
    /// than `caps.put`, a per-operation reject-counter keyed by
    /// [`SecretOperation`]) read the capability through one projection
    /// instead of re-deriving the six boolean reads inline.
    #[must_use]
    pub const fn supports(self, op: SecretOperation) -> bool {
        op.is_supported_by(self)
    }
}

/// Closed-axis primitive over the shikumi-provided [`SecretClient`]
/// implementor universe — the seven runtime clients shikumi ships, with
/// each variant pinned pointwise to the matching impl's
/// [`SecretClient::backend_name`]: [`MemClient`] → [`Self::Mem`]
/// (in-memory test scaffold, label `"mem"`), [`CommandClient`] →
/// [`Self::Command`] (shell-subprocess `get` shim, label `"command"`),
/// `AkeylessClient` → [`Self::Akeyless`] (native HTTP, Akeyless gateway,
/// label `"akeyless"`), `AwsClient` → [`Self::AwsSecretsManager`] (AWS
/// Secrets Manager SDK, label `"aws-secrets-manager"`), `OpConnectClient`
/// → [`Self::OpConnect`] (1Password Connect HTTP, label `"op-connect"`),
/// `VaultClient` → [`Self::Vault`] (`HashiCorp` Vault KV v2 HTTP, label
/// `"vault"`), `GcpSecretClient` → [`Self::GcpSecretManager`] (GCP Secret
/// Manager SDK, label `"gcp-secret-manager"`).
///
/// Distinct universe from [`SecretBackendKind`] on the secret-axis
/// primitive set: that primitive partitions the [`crate::secret::SecretBackend`]
/// variant space (what a YAML config author writes —
/// `literal`/`command`/`op`/`sops`/`akeyless`/`vault`/`aws_secret`/`gcp_secret`),
/// while this primitive partitions the [`SecretClient`] implementor
/// space (what the daemon dispatches against at runtime). The two
/// surfaces overlap (every [`SecretClient`] impl resolves _some_
/// [`crate::secret::SecretBackend`]-shaped value) but are not in
/// bijection: [`Self::Mem`] (test scaffold) has no [`crate::secret::SecretBackend`]
/// peer, [`Self::OpConnect`] is a distinct HTTP transport from
/// [`SecretBackendKind::Op`] (which dispatches the `op` CLI), and the
/// SOPS backend ([`SecretBackendKind::Sops`]) has no [`SecretClient`]
/// peer (resolved via [`crate::secret::resolve_sops_file`] /
/// [`crate::secret::resolve_sops_field`] directly). The label strings
/// likewise diverge — `SecretBackendKind` follows
/// [`crate::secret::SecretBackend`]'s `#[serde(rename_all =
/// "snake_case")]` (`"aws_secret"`, `"gcp_secret"`), while
/// `SecretClientKind` mirrors the runtime client's `backend_name()`
/// kebab-case (`"aws-secrets-manager"`, `"gcp-secret-manager"`,
/// `"op-connect"`).
///
/// Before this lift, [`SecretClient::backend_name`] was an open
/// `&'static str` axis: each impl returned a hand-picked label with no
/// type-level pin that distinct impls picked distinct labels, no closed
/// enumeration for per-client dispatch (telemetry recording the client
/// mix of resolved secrets, per-client retry policies, attestation
/// manifests recording the client histogram of refusals, CLI flag
/// values listing the filterable client set, structured-diagnostic
/// legends naming the failing client by typed primitive across thread
/// boundaries), and no structural agreement between the
/// [`SecretError::Unsupported`] `backend` field's string and any typed
/// classification. Lifting the universe to one closed enum closes the
/// runtime-client axis structurally: every shikumi-shipped impl's
/// [`SecretClient::backend_name`] maps to exactly one [`SecretClientKind`]
/// variant through the default [`SecretClient::client_kind`] projection,
/// and the canonical labels live at one site
/// ([`SecretClientKind::as_str`]) instead of being re-stated as a
/// magic-string `&'static str` literal in every `impl SecretClient
/// for X { fn backend_name(&self) -> &'static str { "x" } }` arm.
///
/// Peer of [`SecretBackendKind`] (config-author backend axis),
/// [`SecretErrorKind`] (error-variant axis), [`SecretOperation`]
/// (cross-surface operation axis), and the other closed-enum kind
/// primitives ([`crate::ConfigSourceKind`] on the layer axis,
/// [`crate::FigmentSourceKind`] / [`crate::FigmentNameTagKind`] on the
/// figment-`Metadata::{source, name}` axes): same typescape discipline
/// (closed, allocation-free, `Copy + Eq + Hash + #[non_exhaustive]`,
/// canonical operator-facing label), applied to the secret-client
/// runtime-implementor axis.
///
/// `'static` and allocation-free, suitable for crossing thread
/// boundaries — observable on a captured [`SecretError`] envelope
/// without retaining the borrowed [`SecretClient`] reference that
/// produced it.
///
/// Adding a future [`SecretClient`] implementor on the shikumi side
/// (e.g. a `KubernetesSecretClient` for in-cluster `Secret` resources,
/// a `KeychainClient` for the macOS Keychain) means adding one
/// [`SecretClientKind`] variant in lockstep with the
/// `impl SecretClient for X` declaration; the default
/// [`SecretClient::client_kind`] derivation projects through
/// [`crate::ClosedAxisLabel::from_canonical_str`], so the new impl's
/// `backend_name()` string lands at one place
/// ([`SecretClientKind::as_str`]) and the typed projection follows
/// automatically. External implementors (out-of-crate consumers writing
/// their own [`SecretClient`]) get [`None`] from the default
/// [`SecretClient::client_kind`] — the closed axis covers the
/// shikumi-shipped universe only and explicitly does not claim to
/// cover every possible implementor.
///
/// [`SecretBackendKind`]: crate::secret::SecretBackendKind
/// [`SecretBackendKind::Op`]: crate::secret::SecretBackendKind::Op
/// [`SecretBackendKind::Sops`]: crate::secret::SecretBackendKind::Sops
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum SecretClientKind {
    /// Maps to [`MemClient`] — the thread-safe in-memory test scaffold.
    /// [`SecretClient::backend_name`] returns `"mem"`.
    Mem,
    /// Maps to [`CommandClient`] — the shell-subprocess `get` shim.
    /// [`SecretClient::backend_name`] returns `"command"`.
    Command,
    /// Maps to `AkeylessClient` (feature `akeyless-native`) — native HTTP
    /// against an Akeyless gateway. [`SecretClient::backend_name`]
    /// returns `"akeyless"`. Coincides with [`SecretBackendKind::Akeyless`]'s
    /// label since the runtime client matches the config-author backend
    /// 1:1 on this axis.
    Akeyless,
    /// Maps to `AwsClient` (feature `aws-native`) — native AWS Secrets
    /// Manager SDK. [`SecretClient::backend_name`] returns
    /// `"aws-secrets-manager"`. Distinct from [`SecretBackendKind::AwsSecret`]'s
    /// `"aws_secret"` label by typescape design: the runtime client
    /// labels its transport (`"aws-secrets-manager"`, naming the AWS
    /// service) while the config-author backend labels its YAML key
    /// (`"aws_secret"`, the `#[serde(rename_all = "snake_case")]` tag).
    AwsSecretsManager,
    /// Maps to `OpConnectClient` (feature `op-native`) — 1Password
    /// Connect HTTP transport. [`SecretClient::backend_name`] returns
    /// `"op-connect"`. Distinct from [`SecretBackendKind::Op`]'s `"op"`
    /// label: that backend dispatches the `op` CLI, this client talks
    /// HTTP to a 1Password Connect server (different transport, same
    /// upstream 1Password vault).
    OpConnect,
    /// Maps to `VaultClient` (feature `vault-native`) — `HashiCorp` Vault
    /// KV v2 HTTP transport. [`SecretClient::backend_name`] returns
    /// `"vault"`. Coincides with [`SecretBackendKind::Vault`]'s label
    /// since the runtime client matches the config-author backend 1:1
    /// on this axis.
    Vault,
    /// Maps to `GcpSecretClient` (feature `gcp-native`) — native GCP
    /// Secret Manager SDK. [`SecretClient::backend_name`] returns
    /// `"gcp-secret-manager"`. Distinct from [`SecretBackendKind::GcpSecret`]'s
    /// `"gcp_secret"` label (same reasoning as the
    /// [`Self::AwsSecretsManager`] / [`SecretBackendKind::AwsSecret`]
    /// pair).
    GcpSecretManager,
}

impl SecretClientKind {
    /// Every [`SecretClientKind`] variant, in declaration order
    /// ([`Self::Mem`], [`Self::Command`], [`Self::Akeyless`],
    /// [`Self::AwsSecretsManager`], [`Self::OpConnect`], [`Self::Vault`],
    /// [`Self::GcpSecretManager`]).
    ///
    /// The closed list of shikumi-shipped [`SecretClient`] impls. Iterate
    /// to enumerate the runtime-client space without listing variants by
    /// hand at every consumer site — e.g. dashboards initializing per-
    /// client telemetry counters, attestation manifests recording the
    /// client-mix histogram of resolved secrets, CLI flag values listing
    /// the filterable client set, partition-coverage tests asserting
    /// disjointness across the runtime-client classification.
    ///
    /// One source of truth for the runtime-client enumeration on the
    /// [`SecretClientKind`] axis: peer to [`SecretBackendKind::ALL`] on
    /// the config-author backend axis, [`SecretErrorKind::ALL`] on the
    /// error-variant axis, [`SecretOperation::ALL`] on the operation
    /// axis, and the other closed-enum kind primitives — same typescape
    /// discipline applied to the runtime [`SecretClient`] implementor
    /// axis.
    ///
    /// Adding a new variant to [`Self`] (in lockstep with a new
    /// shikumi-shipped `impl SecretClient`) means extending this slice
    /// in lockstep with the variant itself. The compiler enforces nothing
    /// here directly, so the
    /// `secret_client_kind_all_covers_every_variant` test pins the
    /// contract via the `Self::ALL.iter().copied()` round-trip with the
    /// closed enum's variant set, and the
    /// `secret_client_kind_all_has_no_duplicates` test pins that the
    /// constant is a set (no double-listed variant).
    ///
    /// [`SecretBackendKind`]: crate::secret::SecretBackendKind
    pub const ALL: &'static [Self] = &[
        Self::Mem,
        Self::Command,
        Self::Akeyless,
        Self::AwsSecretsManager,
        Self::OpConnect,
        Self::Vault,
        Self::GcpSecretManager,
    ];

    /// Canonical operator-facing name of the runtime client — pinned
    /// pointwise to each [`SecretClient`] impl's
    /// [`SecretClient::backend_name`] return string: `"mem"`,
    /// `"command"`, `"akeyless"`, `"aws-secrets-manager"`,
    /// `"op-connect"`, `"vault"`, `"gcp-secret-manager"`.
    ///
    /// Single source of truth for the seven runtime backend-name strings
    /// that previously lived inline as magic-string literals at each
    /// `impl SecretClient for X { fn backend_name(&self) -> &'static str
    /// { "x" } }` arm. Inherent mirror of the
    /// [`crate::ClosedAxisLabel`] trait method; the trait impl delegates
    /// here so the canonical names live at one site instead of being
    /// re-stated at every operator-facing surface (a future structured-
    /// log field naming the failing client by typed primitive, a CLI
    /// flag filtering captured failures by client, an alerting bucket
    /// histogramming the client partition over the captured-failure
    /// surface, an attestation manifest recording the client histogram).
    ///
    /// The label space is heterogeneous on the kebab-case axis by
    /// runtime-transport design — `"aws-secrets-manager"`,
    /// `"op-connect"`, `"gcp-secret-manager"` are kebab-cased to name
    /// the specific transport (AWS Secrets Manager SDK, 1Password
    /// Connect HTTP, GCP Secret Manager SDK), while `"mem"`,
    /// `"command"`, `"akeyless"`, `"vault"` are single-word lowercase
    /// matching the typescape's other single-word kind labels
    /// ([`crate::ConfigSourceKind::as_str`], [`SecretOperation::as_str`]).
    /// Within an axis, the trait-uniform distinctness law
    /// (`closed_axis_label_as_str_distinct_for_every_implementor`) pins
    /// pairwise distinctness; cross-axis label coincidence
    /// ([`Self::Akeyless`] / [`SecretBackendKind::Akeyless`] both
    /// labeled `"akeyless"`, [`Self::Vault`] / [`SecretBackendKind::Vault`]
    /// both labeled `"vault"`) is structural and intentional — the
    /// runtime client and the config-author backend agree on the YAML
    /// key at the resolution boundary.
    ///
    /// Pairs with [`crate::ClosedAxisLabel::from_canonical_str`] via the
    /// trait-default linear-scan parse; the round-trip law
    /// `Self::from_canonical_str(v.as_str()) == Some(v)` holds for every
    /// variant uniformly through the trait-uniform
    /// `closed_axis_label_round_trips_for_every_implementor` test in
    /// `cube::tests`. The concrete-position pin at
    /// `secret_client_kind_as_str_yields_canonical_names` holds the
    /// literal strings stable so a future rename (e.g. shortening
    /// `"aws-secrets-manager"` to `"aws"`, expanding `"mem"` to
    /// `"in-memory"`) fails at that site before drifting through the
    /// round-trip law and the per-impl `backend_name()` pins.
    ///
    /// [`SecretBackendKind`]: crate::secret::SecretBackendKind
    /// [`SecretBackendKind::Akeyless`]: crate::secret::SecretBackendKind::Akeyless
    /// [`SecretBackendKind::Vault`]: crate::secret::SecretBackendKind::Vault
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Mem => "mem",
            Self::Command => "command",
            Self::Akeyless => "akeyless",
            Self::AwsSecretsManager => "aws-secrets-manager",
            Self::OpConnect => "op-connect",
            Self::Vault => "vault",
            Self::GcpSecretManager => "gcp-secret-manager",
        }
    }
}

impl crate::ClosedAxis for SecretClientKind {
    const ALL: &'static [Self] = Self::ALL;
}

impl crate::ClosedAxisLabel for SecretClientKind {
    fn as_str(self) -> &'static str {
        Self::as_str(self)
    }
}

/// Metadata attached to a secret value.
///
/// Not all backends populate every field — `None` means the backend
/// didn't surface it, not that it's absent in the store.
#[derive(Debug, Clone, Default)]
pub struct SecretMetadata {
    /// Opaque version identifier (Vault ver number, GCP version name,
    /// AWS VersionId, etc.). Callers shouldn't parse this.
    pub version: Option<String>,
    /// When the secret was last written (RFC 3339 string to stay
    /// dep-free for now).
    pub updated_at: Option<String>,
    /// Backend-specific tags (AWS Tags, Akeyless tags, Vault custom
    /// metadata, 1Password field names).
    pub tags: HashMap<String, String>,
}

/// A secret value plus its metadata.
#[derive(Debug, Clone)]
pub struct Secret {
    pub value: String,
    pub metadata: SecretMetadata,
}

/// Unified client trait — the abstraction consumers code against.
///
/// Object-safe (`&self` methods, no generics on the trait). All methods
/// are `async` and the trait uses `async_trait` for object safety in
/// Rust's current state.
///
/// Default impls return [`SecretError::Unsupported`] for operations
/// the backend doesn't advertise in [`Self::capabilities`]. Impls
/// override only the operations they support.
#[async_trait]
pub trait SecretClient: Send + Sync {
    /// Human-readable backend name for diagnostics and logging.
    fn backend_name(&self) -> &'static str;

    /// Which operations this backend supports.
    fn capabilities(&self) -> Capabilities;

    /// Typed closed-axis classification of this client's runtime backend
    /// — [`Some`] for the seven shikumi-shipped impls (whose
    /// [`Self::backend_name`] strings are pinned pointwise on
    /// [`SecretClientKind::as_str`]), [`None`] for external implementors
    /// whose [`Self::backend_name`] doesn't match any canonical label
    /// on the [`SecretClientKind`] axis.
    ///
    /// Default impl derives the typed kind from [`Self::backend_name`]
    /// via [`SecretClientKind::from_canonical_str`] (the trait-default
    /// case-insensitive linear-scan parse over [`SecretClientKind::ALL`]),
    /// so every shikumi-shipped impl projects automatically without
    /// touching the impl body: [`MemClient`]'s `"mem"` resolves to
    /// [`Some(SecretClientKind::Mem)`], `AkeylessClient`'s `"akeyless"`
    /// resolves to [`Some(SecretClientKind::Akeyless)`], and so on for
    /// all seven. External implementors with custom backend-name strings
    /// outside the closed axis receive [`None`] from the default — the
    /// trait does not claim to classify implementors it doesn't ship.
    ///
    /// Consumers reading the typed projection (per-client telemetry
    /// dispatching off [`SecretClientKind`], structured-diagnostic
    /// legends naming the failing client by typed primitive across
    /// thread boundaries, attestation manifests recording the client
    /// histogram of refusals, CLI flag values listing the filterable
    /// client set, cross-thread observable forms that need a `'static`
    /// classification surviving the borrow on the live [`SecretClient`])
    /// route through this projection instead of re-deriving the
    /// classification by string-comparing [`Self::backend_name`] at every
    /// observation site. The closed-enum return value composes further
    /// (it's `Copy + Eq + Hash + 'static`), where the raw
    /// `&'static str` does not.
    ///
    /// Pairs with [`Self::backend_name`] under the structural law
    /// `self.client_kind().map(SecretClientKind::as_str) == Some(self.backend_name())`
    /// for every shikumi-shipped impl — pinned by
    /// `secret_client_kind_default_client_kind_recovers_backend_name_pointwise`
    /// in the per-impl test surface for every always-available impl
    /// ([`MemClient`], [`CommandClient`]) and per-feature for the
    /// gated impls. External impls satisfy
    /// `self.client_kind().is_none()` until they opt into the typed
    /// axis by overriding this method directly.
    fn client_kind(&self) -> Option<SecretClientKind> {
        <SecretClientKind as crate::ClosedAxisLabel>::from_canonical_str(self.backend_name())
    }

    /// Fetch the current secret value.
    async fn get(&self, name: &str) -> Result<String, SecretError>;

    /// Fetch the current secret value + metadata.
    ///
    /// Default: calls `get()` and returns empty metadata. Override for
    /// backends that surface version / tag / timestamp info.
    async fn get_with_metadata(&self, name: &str) -> Result<Secret, SecretError> {
        let value = self.get(name).await?;
        Ok(Secret {
            value,
            metadata: SecretMetadata::default(),
        })
    }

    /// List secret names, optionally filtered by prefix.
    async fn list(&self, _prefix: Option<&str>) -> Result<Vec<String>, SecretError> {
        Err(SecretError::unsupported(
            self.backend_name(),
            SecretOperation::List,
        ))
    }

    /// Create or update a secret.
    async fn put(&self, _name: &str, _value: &str) -> Result<(), SecretError> {
        Err(SecretError::unsupported(
            self.backend_name(),
            SecretOperation::Put,
        ))
    }

    /// Delete a secret.
    async fn delete(&self, _name: &str) -> Result<(), SecretError> {
        Err(SecretError::unsupported(
            self.backend_name(),
            SecretOperation::Delete,
        ))
    }

    /// Trigger backend-side rotation (re-derives the value; details are
    /// backend-specific).
    async fn rotate(&self, _name: &str) -> Result<(), SecretError> {
        Err(SecretError::unsupported(
            self.backend_name(),
            SecretOperation::Rotate,
        ))
    }

    /// Fetch a specific historical version of the secret.
    async fn get_version(&self, _name: &str, _version: &str) -> Result<String, SecretError> {
        Err(SecretError::unsupported(
            self.backend_name(),
            SecretOperation::GetVersion,
        ))
    }
}

// ─────────────────────────────────────────────────────────────────────
// MemClient — in-memory backend for testing + dev defaults
// ─────────────────────────────────────────────────────────────────────

/// Thread-safe in-memory `SecretClient`. Useful for tests and for
/// seeding dev secrets without hitting a real vault.
///
/// Backed by a `RwLock<HashMap>` so reads don't contend. Version
/// history is kept per-name: each write appends; `rotate` also
/// appends a new generated value. Versions are numbered starting at 1.
pub struct MemClient {
    store: RwLock<HashMap<String, Vec<String>>>,
}

impl MemClient {
    #[must_use]
    pub fn new() -> Self {
        Self {
            store: RwLock::new(HashMap::new()),
        }
    }

    /// Seed the client with an initial set of secrets. Convenient for
    /// test fixtures and dev defaults. Each seed value becomes version 1.
    #[must_use]
    pub fn with_seed<I, K, V>(iter: I) -> Self
    where
        I: IntoIterator<Item = (K, V)>,
        K: Into<String>,
        V: Into<String>,
    {
        let client = Self::new();
        for (k, v) in iter {
            client
                .store
                .write()
                .expect("MemClient lock poisoned")
                .insert(k.into(), vec![v.into()]);
        }
        client
    }
}

impl Default for MemClient {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl SecretClient for MemClient {
    fn backend_name(&self) -> &'static str {
        "mem"
    }

    fn capabilities(&self) -> Capabilities {
        Capabilities::full()
    }

    async fn get(&self, name: &str) -> Result<String, SecretError> {
        let store = self.store.read().expect("MemClient lock poisoned");
        store
            .get(name)
            .and_then(|versions| versions.last().cloned())
            .ok_or_else(|| SecretError::NotFound {
                name: name.to_owned(),
            })
    }

    async fn get_with_metadata(&self, name: &str) -> Result<Secret, SecretError> {
        let store = self.store.read().expect("MemClient lock poisoned");
        let versions = store.get(name).ok_or_else(|| SecretError::NotFound {
            name: name.to_owned(),
        })?;
        let value = versions
            .last()
            .cloned()
            .ok_or_else(|| SecretError::NotFound {
                name: name.to_owned(),
            })?;
        let metadata = SecretMetadata {
            version: Some(versions.len().to_string()),
            updated_at: None,
            tags: HashMap::new(),
        };
        Ok(Secret { value, metadata })
    }

    async fn list(&self, prefix: Option<&str>) -> Result<Vec<String>, SecretError> {
        let store = self.store.read().expect("MemClient lock poisoned");
        let mut names: Vec<String> = store
            .keys()
            .filter(|k| prefix.is_none_or(|p| k.starts_with(p)))
            .cloned()
            .collect();
        names.sort();
        Ok(names)
    }

    async fn put(&self, name: &str, value: &str) -> Result<(), SecretError> {
        self.store
            .write()
            .expect("MemClient lock poisoned")
            .entry(name.to_owned())
            .or_default()
            .push(value.to_owned());
        Ok(())
    }

    async fn delete(&self, name: &str) -> Result<(), SecretError> {
        let removed = self
            .store
            .write()
            .expect("MemClient lock poisoned")
            .remove(name);
        if removed.is_some() {
            Ok(())
        } else {
            Err(SecretError::NotFound {
                name: name.to_owned(),
            })
        }
    }

    async fn rotate(&self, name: &str) -> Result<(), SecretError> {
        // Rotation semantics for an in-memory client: append a
        // deterministic-ish new value derived from the current version
        // count. Real vaults delegate rotation to a producer; this is
        // a test/dev scaffold so callers can exercise the code path.
        let mut store = self.store.write().expect("MemClient lock poisoned");
        let versions = store.get_mut(name).ok_or_else(|| SecretError::NotFound {
            name: name.to_owned(),
        })?;
        let next = format!("rotated-v{}-{name}", versions.len() + 1);
        versions.push(next);
        Ok(())
    }

    async fn get_version(&self, name: &str, version: &str) -> Result<String, SecretError> {
        let n: usize = version.parse().map_err(|_| {
            SecretError::Backend(format!("mem version must be an integer, got {version:?}"))
        })?;
        if n == 0 {
            return Err(SecretError::Backend(
                "mem versions are 1-indexed; 0 is invalid".into(),
            ));
        }
        let store = self.store.read().expect("MemClient lock poisoned");
        let versions = store.get(name).ok_or_else(|| SecretError::NotFound {
            name: name.to_owned(),
        })?;
        versions.get(n - 1).cloned().ok_or_else(|| {
            SecretError::Backend(format!(
                "mem has {} versions for {name}, version {n} out of range",
                versions.len()
            ))
        })
    }
}

// ─────────────────────────────────────────────────────────────────────
// CommandClient — CLI-shelling backend for "anything else"
// ─────────────────────────────────────────────────────────────────────

/// `SecretClient` backed by shell commands. Each name is looked up by
/// running a per-key template command via [`crate::secret::resolve_command`].
///
/// Two configurations:
///
/// - `with_get_template`: a single template string with `{name}` placeholder
///   substituted in per `get()` call. Example: `"op read 'op://vault/{name}/field'"`.
/// - `with_name_map`: an explicit `HashMap<name, command>` for when each
///   secret has a unique CLI invocation.
///
/// Read-only — `list`/`put`/`delete` return `Unsupported`. Consumers
/// needing write access compose this with [`MemClient`] for
/// test-double scenarios or switch to a native backend.
pub struct CommandClient {
    template: Option<String>,
    name_map: HashMap<String, String>,
}

impl CommandClient {
    /// Build with a single template: `get(name)` runs the template
    /// with `{name}` replaced.
    #[must_use]
    pub fn with_get_template(template: impl Into<String>) -> Self {
        Self {
            template: Some(template.into()),
            name_map: HashMap::new(),
        }
    }

    /// Build with an explicit name-to-command mapping.
    #[must_use]
    pub fn with_name_map<I, K, V>(iter: I) -> Self
    where
        I: IntoIterator<Item = (K, V)>,
        K: Into<String>,
        V: Into<String>,
    {
        Self {
            template: None,
            name_map: iter
                .into_iter()
                .map(|(k, v)| (k.into(), v.into()))
                .collect(),
        }
    }
}

#[async_trait]
impl SecretClient for CommandClient {
    fn backend_name(&self) -> &'static str {
        "command"
    }

    fn capabilities(&self) -> Capabilities {
        Capabilities::read_only()
    }

    async fn get(&self, name: &str) -> Result<String, SecretError> {
        let cmd: String = if let Some(explicit) = self.name_map.get(name) {
            explicit.clone()
        } else if let Some(template) = &self.template {
            template.replace("{name}", name)
        } else {
            return Err(SecretError::NotFound {
                name: name.to_owned(),
            });
        };

        crate::secret::resolve_command(&cmd).map_err(SecretError::from)
    }
}

// ─────────────────────────────────────────────────────────────────────
// Native backend impls (feature-gated)
// ─────────────────────────────────────────────────────────────────────

/// Native Akeyless `SecretClient` — HTTP via the `akeyless-api` SDK.
///
/// Feature-gated on `akeyless-native`. Only `get` is implemented at
/// present; `list`/`put`/`delete`/`rotate` will land as follow-ups that
/// wire the corresponding Akeyless API endpoints (list-items,
/// create-secret, delete-item, rotate-secret).
#[cfg(feature = "akeyless-native")]
pub struct AkeylessClient {
    auth: crate::secret::AkeylessAuth,
}

#[cfg(feature = "akeyless-native")]
impl AkeylessClient {
    #[must_use]
    pub fn new(auth: crate::secret::AkeylessAuth) -> Self {
        Self { auth }
    }

    /// Construct from `AKEYLESS_TOKEN` + `AKEYLESS_GATEWAY_URL` env vars.
    ///
    /// # Errors
    ///
    /// Propagates [`crate::secret::AkeylessAuth::from_env`] errors.
    pub fn from_env() -> Result<Self, SecretError> {
        let auth = crate::secret::AkeylessAuth::from_env()?;
        Ok(Self::new(auth))
    }
}

#[cfg(feature = "akeyless-native")]
#[async_trait]
impl SecretClient for AkeylessClient {
    fn backend_name(&self) -> &'static str {
        "akeyless"
    }

    fn capabilities(&self) -> Capabilities {
        Capabilities::full()
    }

    async fn get(&self, name: &str) -> Result<String, SecretError> {
        crate::secret::resolve_akeyless_native(&self.auth, name)
            .await
            .map_err(SecretError::from)
    }

    async fn list(&self, prefix: Option<&str>) -> Result<Vec<String>, SecretError> {
        let cfg = self.auth.configuration();
        let request = akeyless_api::models::ListItems {
            token: Some(self.auth.token.clone()),
            path: prefix.map(str::to_owned),
            auto_pagination: Some("enabled".into()),
            ..Default::default()
        };
        let response = akeyless_api::apis::v2_api::list_items(&cfg, request)
            .await
            .map_err(|e| SecretError::Backend(format!("akeyless list-items: {e}")))?;

        let mut names: Vec<String> = response
            .items
            .unwrap_or_default()
            .into_iter()
            .filter_map(|item| item.item_name)
            .collect();
        names.sort();
        Ok(names)
    }

    async fn put(&self, name: &str, value: &str) -> Result<(), SecretError> {
        let cfg = self.auth.configuration();
        // Try update first; fall through to create on ItemNotFound.
        let update = akeyless_api::models::UpdateSecretVal {
            token: Some(self.auth.token.clone()),
            name: name.to_owned(),
            value: value.to_owned(),
            ..Default::default()
        };
        let update_result = akeyless_api::apis::v2_api::update_secret_val(&cfg, update).await;
        match update_result {
            Ok(_) => Ok(()),
            Err(err) => {
                let msg = format!("{err}");
                if msg.contains("ItemNotExist")
                    || msg.contains("not exist")
                    || msg.contains("not found")
                {
                    let create = akeyless_api::models::CreateSecret {
                        token: Some(self.auth.token.clone()),
                        name: name.to_owned(),
                        value: value.to_owned(),
                        ..Default::default()
                    };
                    akeyless_api::apis::v2_api::create_secret(&cfg, create)
                        .await
                        .map_err(|e| {
                            SecretError::Backend(format!("akeyless create-secret({name}): {e}"))
                        })?;
                    Ok(())
                } else {
                    Err(SecretError::Backend(format!(
                        "akeyless update-secret-val({name}): {msg}"
                    )))
                }
            }
        }
    }

    async fn delete(&self, name: &str) -> Result<(), SecretError> {
        let cfg = self.auth.configuration();
        let request = akeyless_api::models::DeleteItem {
            token: Some(self.auth.token.clone()),
            name: name.to_owned(),
            delete_immediately: Some(true),
            ..Default::default()
        };
        akeyless_api::apis::v2_api::delete_item(&cfg, request)
            .await
            .map_err(|e| SecretError::Backend(format!("akeyless delete-item({name}): {e}")))?;
        Ok(())
    }

    async fn rotate(&self, name: &str) -> Result<(), SecretError> {
        let cfg = self.auth.configuration();
        let request = akeyless_api::models::RotateSecret {
            token: Some(self.auth.token.clone()),
            name: name.to_owned(),
            ..Default::default()
        };
        akeyless_api::apis::v2_api::rotate_secret(&cfg, request)
            .await
            .map_err(|e| SecretError::Backend(format!("akeyless rotate-secret({name}): {e}")))?;
        Ok(())
    }

    async fn get_version(&self, name: &str, version: &str) -> Result<String, SecretError> {
        let cfg = self.auth.configuration();
        let version_num: i32 = version.parse().map_err(|_| {
            SecretError::Backend(format!(
                "akeyless version must be an integer, got {version:?}"
            ))
        })?;
        let request = akeyless_api::models::GetSecretValue {
            names: vec![name.to_owned()],
            token: Some(self.auth.token.clone()),
            version: Some(version_num),
            ..Default::default()
        };
        let response = akeyless_api::apis::v2_api::get_secret_value(&cfg, request)
            .await
            .map_err(|e| {
                SecretError::Backend(format!(
                    "akeyless get-secret-value({name}, v={version}): {e}"
                ))
            })?;
        let obj = response.as_object().ok_or_else(|| {
            SecretError::Backend(format!(
                "akeyless response for {name} v{version} was not an object"
            ))
        })?;
        obj.get(name)
            .and_then(|v| v.as_str())
            .map(str::to_owned)
            .ok_or_else(|| {
                SecretError::Backend(format!(
                    "akeyless response missing value for {name} v{version}"
                ))
            })
    }
}

/// Native AWS Secrets Manager `SecretClient`.
///
/// Feature-gated on `aws-native`. Only `get` is implemented at present;
/// `list`/`put`/`delete`/`rotate`/versions will land as follow-ups that
/// wire the corresponding SDK operations (`ListSecrets`, `CreateSecret`,
/// `DeleteSecret`, `RotateSecret`, `GetSecretValue` with version id).
#[cfg(feature = "aws-native")]
pub struct AwsClient {
    client: aws_sdk_secretsmanager::Client,
}

#[cfg(feature = "aws-native")]
impl AwsClient {
    #[must_use]
    pub fn new(client: aws_sdk_secretsmanager::Client) -> Self {
        Self { client }
    }

    /// Construct with a client built from the default AWS credential
    /// chain. Reads region + creds from env vars, profile files, or
    /// IMDSv2 (EC2) / IRSA (EKS).
    pub async fn from_env() -> Self {
        let client = crate::secret::aws_secretsmanager_client().await;
        Self::new(client)
    }
}

#[cfg(feature = "aws-native")]
#[async_trait]
impl SecretClient for AwsClient {
    fn backend_name(&self) -> &'static str {
        "aws-secrets-manager"
    }

    fn capabilities(&self) -> Capabilities {
        Capabilities::full()
    }

    async fn get(&self, name: &str) -> Result<String, SecretError> {
        crate::secret::resolve_aws_secret_native(&self.client, name)
            .await
            .map_err(SecretError::from)
    }

    async fn get_with_metadata(&self, name: &str) -> Result<Secret, SecretError> {
        let response = self
            .client
            .get_secret_value()
            .secret_id(name)
            .send()
            .await
            .map_err(|e| SecretError::Backend(format!("aws get-secret-value({name}): {e}")))?;

        let value = response.secret_string().map(str::to_owned).ok_or_else(|| {
            SecretError::Backend(format!(
                "aws secret {name} has no SecretString (binary-only)"
            ))
        })?;

        let mut metadata = SecretMetadata::default();
        if let Some(version) = response.version_id() {
            metadata.version = Some(version.to_owned());
        }
        if let Some(created) = response.created_date() {
            // AWS DateTime → epoch seconds → display. Keeping a
            // chrono-free representation since shikumi doesn't pull
            // chrono as a dep.
            metadata.updated_at = Some(format!("{}", created.secs()));
        }
        if !response.version_stages().is_empty() {
            metadata
                .tags
                .insert("stages".into(), response.version_stages().join(","));
        }
        Ok(Secret { value, metadata })
    }

    async fn list(&self, prefix: Option<&str>) -> Result<Vec<String>, SecretError> {
        let mut names = Vec::new();
        let mut next_token: Option<String> = None;
        loop {
            let mut req = self.client.list_secrets();
            if let Some(t) = &next_token {
                req = req.next_token(t);
            }
            let resp = req
                .send()
                .await
                .map_err(|e| SecretError::Backend(format!("aws list-secrets: {e}")))?;
            for entry in resp.secret_list() {
                if let Some(n) = entry.name() {
                    if prefix.is_none_or(|p| n.starts_with(p)) {
                        names.push(n.to_owned());
                    }
                }
            }
            next_token = resp.next_token().map(str::to_owned);
            if next_token.is_none() {
                break;
            }
        }
        names.sort();
        Ok(names)
    }

    async fn put(&self, name: &str, value: &str) -> Result<(), SecretError> {
        // Try update first; if the secret doesn't exist, create it.
        let update_result = self
            .client
            .put_secret_value()
            .secret_id(name)
            .secret_string(value)
            .send()
            .await;
        match update_result {
            Ok(_) => Ok(()),
            Err(err) => {
                // ResourceNotFoundException → fall through to create.
                let err_str = format!("{err}");
                if err_str.contains("ResourceNotFoundException") || err_str.contains("not found") {
                    self.client
                        .create_secret()
                        .name(name)
                        .secret_string(value)
                        .send()
                        .await
                        .map_err(|e| {
                            SecretError::Backend(format!("aws create-secret({name}): {e}"))
                        })?;
                    Ok(())
                } else {
                    Err(SecretError::Backend(format!(
                        "aws put-secret-value({name}): {err_str}"
                    )))
                }
            }
        }
    }

    async fn delete(&self, name: &str) -> Result<(), SecretError> {
        // ForceDeleteWithoutRecovery=true bypasses the 7-30 day
        // recovery window. Callers that need soft-delete compose their
        // own SDK call.
        self.client
            .delete_secret()
            .secret_id(name)
            .force_delete_without_recovery(true)
            .send()
            .await
            .map_err(|e| SecretError::Backend(format!("aws delete-secret({name}): {e}")))?;
        Ok(())
    }

    async fn rotate(&self, name: &str) -> Result<(), SecretError> {
        self.client
            .rotate_secret()
            .secret_id(name)
            .send()
            .await
            .map_err(|e| SecretError::Backend(format!("aws rotate-secret({name}): {e}")))?;
        Ok(())
    }

    async fn get_version(&self, name: &str, version: &str) -> Result<String, SecretError> {
        let response = self
            .client
            .get_secret_value()
            .secret_id(name)
            .version_id(version)
            .send()
            .await
            .map_err(|e| {
                SecretError::Backend(format!("aws get-secret-value({name}, v={version}): {e}"))
            })?;
        response.secret_string().map(str::to_owned).ok_or_else(|| {
            SecretError::Backend(format!("aws secret {name} v{version} has no SecretString"))
        })
    }
}

// ─────────────────────────────────────────────────────────────────────
// OpConnectClient — 1Password Connect via thin reqwest HTTP
// ─────────────────────────────────────────────────────────────────────

/// Native 1Password Connect `SecretClient`.
///
/// Feature-gated on `op-native`. Talks to a 1Password Connect server
/// (self-hosted sync service) over HTTP with a Bearer token. Secrets
/// are modeled as Connect Items; `name` is the item *title*, resolved
/// to a UUID per-call.
///
/// The Connect API is small — 8 endpoints for vault + item CRUD — so
/// this is a hand-written thin client rather than a generated SDK.
/// Rotation and versioning are not supported: 1Password doesn't expose
/// programmatic rotation and item-history retrieval isn't in the
/// Connect API surface.
#[cfg(feature = "op-native")]
pub struct OpConnectClient {
    http: reqwest::Client,
    base_url: String,
    token: String,
    vault_id: String,
}

#[cfg(feature = "op-native")]
#[derive(Debug, Clone)]
pub struct OpConnectConfig {
    /// e.g. `https://connect.example.com` (no trailing slash).
    pub base_url: String,
    /// Bearer token from 1Password Connect server (not a vault API key).
    pub token: String,
    /// Vault UUID. Connect items are scoped to a vault.
    pub vault_id: String,
}

#[cfg(feature = "op-native")]
impl OpConnectClient {
    /// Construct from an explicit config.
    #[must_use]
    pub fn new(config: OpConnectConfig) -> Self {
        Self {
            http: reqwest::Client::new(),
            base_url: config.base_url.trim_end_matches('/').to_owned(),
            token: config.token,
            vault_id: config.vault_id,
        }
    }

    /// Construct from env: `OP_CONNECT_HOST`, `OP_CONNECT_TOKEN`,
    /// `OP_CONNECT_VAULT`.
    ///
    /// # Errors
    ///
    /// Returns [`SecretError::Unauthorized`] if any required variable is missing.
    pub fn from_env() -> Result<Self, SecretError> {
        let read = |var: &str| {
            std::env::var(var).map_err(|_| SecretError::Unauthorized {
                message: format!("{var} not set"),
            })
        };
        Ok(Self::new(OpConnectConfig {
            base_url: read("OP_CONNECT_HOST")?,
            token: read("OP_CONNECT_TOKEN")?,
            vault_id: read("OP_CONNECT_VAULT")?,
        }))
    }

    fn auth_header(&self) -> String {
        format!("Bearer {}", self.token)
    }

    /// Resolve a human-readable item title to a Connect item UUID.
    async fn resolve_item_id(&self, name: &str) -> Result<String, SecretError> {
        let url = format!(
            "{}/v1/vaults/{}/items?filter=title+eq+%22{}%22",
            self.base_url,
            self.vault_id,
            urlencode(name)
        );
        let response = self
            .http
            .get(&url)
            .header("Authorization", self.auth_header())
            .send()
            .await
            .map_err(|e| SecretError::Backend(format!("op list items: {e}")))?;

        if response.status() == reqwest::StatusCode::UNAUTHORIZED
            || response.status() == reqwest::StatusCode::FORBIDDEN
        {
            return Err(SecretError::Unauthorized {
                message: format!("op list items: {}", response.status()),
            });
        }
        if !response.status().is_success() {
            return Err(SecretError::Backend(format!(
                "op list items: HTTP {}",
                response.status()
            )));
        }

        let items: Vec<serde_json::Value> = response
            .json()
            .await
            .map_err(|e| SecretError::Backend(format!("op list items parse: {e}")))?;

        items
            .into_iter()
            .find_map(|item| item.get("id").and_then(|v| v.as_str()).map(str::to_owned))
            .ok_or_else(|| SecretError::NotFound {
                name: name.to_owned(),
            })
    }

    /// Fetch an item by UUID and return its first `password` / `concealed`
    /// field value.
    async fn fetch_item_value(&self, item_id: &str, name: &str) -> Result<String, SecretError> {
        let url = format!(
            "{}/v1/vaults/{}/items/{}",
            self.base_url, self.vault_id, item_id
        );
        let response = self
            .http
            .get(&url)
            .header("Authorization", self.auth_header())
            .send()
            .await
            .map_err(|e| SecretError::Backend(format!("op get item({name}): {e}")))?;

        if !response.status().is_success() {
            return Err(SecretError::Backend(format!(
                "op get item({name}): HTTP {}",
                response.status()
            )));
        }

        let item: serde_json::Value = response
            .json()
            .await
            .map_err(|e| SecretError::Backend(format!("op get item({name}) parse: {e}")))?;

        let fields = item
            .get("fields")
            .and_then(|v| v.as_array())
            .ok_or_else(|| SecretError::Backend(format!("op item {name} has no fields array")))?;
        fields
            .iter()
            .find_map(|f| {
                let purpose = f.get("purpose").and_then(|v| v.as_str()).unwrap_or("");
                let kind = f.get("type").and_then(|v| v.as_str()).unwrap_or("");
                if purpose == "PASSWORD" || kind == "CONCEALED" {
                    f.get("value").and_then(|v| v.as_str()).map(str::to_owned)
                } else {
                    None
                }
            })
            .ok_or_else(|| {
                SecretError::Backend(format!(
                    "op item {name} has no PASSWORD/CONCEALED field with a value"
                ))
            })
    }
}

#[cfg(feature = "op-native")]
#[async_trait]
impl SecretClient for OpConnectClient {
    fn backend_name(&self) -> &'static str {
        "op-connect"
    }

    fn capabilities(&self) -> Capabilities {
        Capabilities {
            get: true,
            list: true,
            put: true,
            delete: true,
            rotate: false,
            versions: false,
        }
    }

    async fn get(&self, name: &str) -> Result<String, SecretError> {
        let id = self.resolve_item_id(name).await?;
        self.fetch_item_value(&id, name).await
    }

    async fn list(&self, prefix: Option<&str>) -> Result<Vec<String>, SecretError> {
        let url = format!("{}/v1/vaults/{}/items", self.base_url, self.vault_id);
        let response = self
            .http
            .get(&url)
            .header("Authorization", self.auth_header())
            .send()
            .await
            .map_err(|e| SecretError::Backend(format!("op list items: {e}")))?;
        if !response.status().is_success() {
            return Err(SecretError::Backend(format!(
                "op list items: HTTP {}",
                response.status()
            )));
        }
        let items: Vec<serde_json::Value> = response
            .json()
            .await
            .map_err(|e| SecretError::Backend(format!("op list items parse: {e}")))?;

        let mut names: Vec<String> = items
            .into_iter()
            .filter_map(|item| {
                item.get("title")
                    .and_then(|v| v.as_str())
                    .map(str::to_owned)
            })
            .filter(|n| prefix.is_none_or(|p| n.starts_with(p)))
            .collect();
        names.sort();
        Ok(names)
    }

    async fn put(&self, name: &str, value: &str) -> Result<(), SecretError> {
        // Create-or-update: look up by title. If missing, POST to
        // create; else PUT to replace the item body.
        let existing = self.resolve_item_id(name).await;
        let body = serde_json::json!({
            "vault": { "id": self.vault_id },
            "title": name,
            "category": "API_CREDENTIAL",
            "fields": [{
                "id": "credential",
                "label": "credential",
                "type": "CONCEALED",
                "purpose": "PASSWORD",
                "value": value,
            }]
        });
        let response = match existing {
            Ok(id) => {
                let url = format!("{}/v1/vaults/{}/items/{}", self.base_url, self.vault_id, id);
                self.http
                    .put(&url)
                    .header("Authorization", self.auth_header())
                    .json(&body)
                    .send()
                    .await
            }
            Err(SecretError::NotFound { .. }) => {
                let url = format!("{}/v1/vaults/{}/items", self.base_url, self.vault_id);
                self.http
                    .post(&url)
                    .header("Authorization", self.auth_header())
                    .json(&body)
                    .send()
                    .await
            }
            Err(e) => return Err(e),
        }
        .map_err(|e| SecretError::Backend(format!("op put item({name}): {e}")))?;

        if !response.status().is_success() {
            return Err(SecretError::Backend(format!(
                "op put item({name}): HTTP {}",
                response.status()
            )));
        }
        Ok(())
    }

    async fn delete(&self, name: &str) -> Result<(), SecretError> {
        let id = self.resolve_item_id(name).await?;
        let url = format!("{}/v1/vaults/{}/items/{}", self.base_url, self.vault_id, id);
        let response = self
            .http
            .delete(&url)
            .header("Authorization", self.auth_header())
            .send()
            .await
            .map_err(|e| SecretError::Backend(format!("op delete item({name}): {e}")))?;
        if !response.status().is_success() {
            return Err(SecretError::Backend(format!(
                "op delete item({name}): HTTP {}",
                response.status()
            )));
        }
        Ok(())
    }
}

#[cfg(feature = "op-native")]
fn urlencode(s: &str) -> String {
    s.chars()
        .map(|c| {
            if c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '.' | '~') {
                c.to_string()
            } else {
                format!("%{:02X}", c as u32)
            }
        })
        .collect()
}

// ─────────────────────────────────────────────────────────────────────
// VaultClient — `HashiCorp` Vault KV v2 via thin reqwest HTTP
// ─────────────────────────────────────────────────────────────────────

/// Native `HashiCorp` Vault `SecretClient` — KV v2 engine.
///
/// Feature-gated on `vault-native`. Only KV v2 semantics are covered
/// here: a `mount` (e.g. `"secret"`) + a path. `name` maps to the item
/// path under that mount; the value lookup reads `data.data.value`
/// (single-field convention) unless a nested-object schema is used,
/// in which case the full JSON string is returned.
///
/// Rotation is ⚠️ backend-specific: Vault doesn't rotate KV secrets —
/// rotation is a property of dynamic-secret engines (database, aws,
/// pki). `rotate` here returns `Unsupported`; callers that want
/// dynamic-secret rotation should call the engine-specific API.
#[cfg(feature = "vault-native")]
pub struct VaultClient {
    http: reqwest::Client,
    base_url: String,
    token: String,
    mount: String,
    namespace: Option<String>,
}

#[cfg(feature = "vault-native")]
#[derive(Debug, Clone)]
pub struct VaultConfig {
    /// Vault URL, e.g. `https://vault.example.com:8200` (no trailing slash).
    pub base_url: String,
    /// Vault auth token (X-Vault-Token header).
    pub token: String,
    /// KV v2 mount path, e.g. `"secret"` or `"kv"`.
    pub mount: String,
    /// Optional Vault Enterprise namespace (X-Vault-Namespace header).
    pub namespace: Option<String>,
}

#[cfg(feature = "vault-native")]
impl VaultClient {
    #[must_use]
    pub fn new(config: VaultConfig) -> Self {
        Self {
            http: reqwest::Client::new(),
            base_url: config.base_url.trim_end_matches('/').to_owned(),
            token: config.token,
            mount: config.mount.trim_matches('/').to_owned(),
            namespace: config.namespace,
        }
    }

    /// Construct from env: `VAULT_ADDR`, `VAULT_TOKEN`,
    /// `VAULT_KV_MOUNT` (default `"secret"`), `VAULT_NAMESPACE` (optional).
    ///
    /// # Errors
    ///
    /// Returns [`SecretError::Unauthorized`] if `VAULT_ADDR` or
    /// `VAULT_TOKEN` is missing.
    pub fn from_env() -> Result<Self, SecretError> {
        let base_url = std::env::var("VAULT_ADDR").map_err(|_| SecretError::Unauthorized {
            message: "VAULT_ADDR not set".into(),
        })?;
        let token = std::env::var("VAULT_TOKEN").map_err(|_| SecretError::Unauthorized {
            message: "VAULT_TOKEN not set".into(),
        })?;
        let mount = std::env::var("VAULT_KV_MOUNT").unwrap_or_else(|_| "secret".into());
        let namespace = std::env::var("VAULT_NAMESPACE").ok();
        Ok(Self::new(VaultConfig {
            base_url,
            token,
            mount,
            namespace,
        }))
    }

    fn apply_headers(&self, req: reqwest::RequestBuilder) -> reqwest::RequestBuilder {
        let mut req = req.header("X-Vault-Token", &self.token);
        if let Some(ns) = &self.namespace {
            req = req.header("X-Vault-Namespace", ns);
        }
        req
    }

    fn data_url(&self, path: &str) -> String {
        format!(
            "{}/v1/{}/data/{}",
            self.base_url,
            self.mount,
            path.trim_start_matches('/')
        )
    }

    fn metadata_url(&self, path: &str) -> String {
        format!(
            "{}/v1/{}/metadata/{}",
            self.base_url,
            self.mount,
            path.trim_start_matches('/')
        )
    }

    /// Parse a KV v2 data response: extract `data.data` as the secret body.
    /// If the body has a single `value` field, return that; otherwise
    /// return the whole object serialized as JSON.
    fn extract_value(body: &serde_json::Value, name: &str) -> Result<String, SecretError> {
        let data = body
            .get("data")
            .and_then(|v| v.get("data"))
            .ok_or_else(|| {
                SecretError::Backend(format!("vault response for {name} missing data.data"))
            })?;
        if let Some(obj) = data.as_object() {
            if obj.len() == 1 {
                if let Some(v) = obj.values().next().and_then(|v| v.as_str()) {
                    return Ok(v.to_owned());
                }
            }
        }
        Ok(data.to_string())
    }
}

#[cfg(feature = "vault-native")]
#[async_trait]
impl SecretClient for VaultClient {
    fn backend_name(&self) -> &'static str {
        "vault"
    }

    fn capabilities(&self) -> Capabilities {
        Capabilities {
            get: true,
            list: true,
            put: true,
            delete: true,
            rotate: false,
            versions: true,
        }
    }

    async fn get(&self, name: &str) -> Result<String, SecretError> {
        let response = self
            .apply_headers(self.http.get(self.data_url(name)))
            .send()
            .await
            .map_err(|e| SecretError::Backend(format!("vault get({name}): {e}")))?;

        match response.status() {
            reqwest::StatusCode::NOT_FOUND => Err(SecretError::NotFound {
                name: name.to_owned(),
            }),
            reqwest::StatusCode::UNAUTHORIZED | reqwest::StatusCode::FORBIDDEN => {
                Err(SecretError::Unauthorized {
                    message: format!("vault get({name}): {}", response.status()),
                })
            }
            status if !status.is_success() => Err(SecretError::Backend(format!(
                "vault get({name}): HTTP {status}"
            ))),
            _ => {
                let body: serde_json::Value = response
                    .json()
                    .await
                    .map_err(|e| SecretError::Backend(format!("vault get({name}) parse: {e}")))?;
                Self::extract_value(&body, name)
            }
        }
    }

    async fn get_with_metadata(&self, name: &str) -> Result<Secret, SecretError> {
        let response = self
            .apply_headers(self.http.get(self.data_url(name)))
            .send()
            .await
            .map_err(|e| SecretError::Backend(format!("vault get({name}): {e}")))?;

        if response.status() == reqwest::StatusCode::NOT_FOUND {
            return Err(SecretError::NotFound {
                name: name.to_owned(),
            });
        }
        if !response.status().is_success() {
            return Err(SecretError::Backend(format!(
                "vault get({name}): HTTP {}",
                response.status()
            )));
        }
        let body: serde_json::Value = response
            .json()
            .await
            .map_err(|e| SecretError::Backend(format!("vault get({name}) parse: {e}")))?;
        let value = Self::extract_value(&body, name)?;
        let mut metadata = SecretMetadata::default();
        if let Some(v) = body
            .get("data")
            .and_then(|v| v.get("metadata"))
            .and_then(|m| m.get("version"))
        {
            metadata.version = Some(v.to_string());
        }
        if let Some(t) = body
            .get("data")
            .and_then(|v| v.get("metadata"))
            .and_then(|m| m.get("created_time"))
            .and_then(|v| v.as_str())
        {
            metadata.updated_at = Some(t.to_owned());
        }
        Ok(Secret { value, metadata })
    }

    async fn list(&self, prefix: Option<&str>) -> Result<Vec<String>, SecretError> {
        let path = prefix.unwrap_or("").trim_start_matches('/');
        let url = self.metadata_url(path);
        let response = self
            .apply_headers(
                self.http
                    .request(reqwest::Method::from_bytes(b"LIST").unwrap(), &url),
            )
            .send()
            .await
            .map_err(|e| SecretError::Backend(format!("vault list: {e}")))?;

        match response.status() {
            reqwest::StatusCode::NOT_FOUND => Ok(Vec::new()),
            s if !s.is_success() => Err(SecretError::Backend(format!("vault list: HTTP {s}"))),
            _ => {
                let body: serde_json::Value = response
                    .json()
                    .await
                    .map_err(|e| SecretError::Backend(format!("vault list parse: {e}")))?;
                let keys = body
                    .get("data")
                    .and_then(|v| v.get("keys"))
                    .and_then(|v| v.as_array())
                    .cloned()
                    .unwrap_or_default();
                let mut names: Vec<String> = keys
                    .into_iter()
                    .filter_map(|v| v.as_str().map(str::to_owned))
                    .map(|k| {
                        if path.is_empty() {
                            k
                        } else {
                            format!("{}/{}", path.trim_end_matches('/'), k)
                        }
                    })
                    .collect();
                names.sort();
                Ok(names)
            }
        }
    }

    async fn put(&self, name: &str, value: &str) -> Result<(), SecretError> {
        let body = serde_json::json!({ "data": { "value": value } });
        let response = self
            .apply_headers(self.http.post(self.data_url(name)))
            .json(&body)
            .send()
            .await
            .map_err(|e| SecretError::Backend(format!("vault put({name}): {e}")))?;
        if !response.status().is_success() {
            return Err(SecretError::Backend(format!(
                "vault put({name}): HTTP {}",
                response.status()
            )));
        }
        Ok(())
    }

    async fn delete(&self, name: &str) -> Result<(), SecretError> {
        // DELETE metadata also removes all versions. For soft-delete
        // (versioned tombstone), callers can target /v1/{mount}/delete/{path}.
        let response = self
            .apply_headers(self.http.delete(self.metadata_url(name)))
            .send()
            .await
            .map_err(|e| SecretError::Backend(format!("vault delete({name}): {e}")))?;
        if response.status() == reqwest::StatusCode::NOT_FOUND {
            return Err(SecretError::NotFound {
                name: name.to_owned(),
            });
        }
        if !response.status().is_success() {
            return Err(SecretError::Backend(format!(
                "vault delete({name}): HTTP {}",
                response.status()
            )));
        }
        Ok(())
    }

    async fn get_version(&self, name: &str, version: &str) -> Result<String, SecretError> {
        let url = format!("{}?version={}", self.data_url(name), version);
        let response = self
            .apply_headers(self.http.get(&url))
            .send()
            .await
            .map_err(|e| SecretError::Backend(format!("vault get({name}, v={version}): {e}")))?;
        if response.status() == reqwest::StatusCode::NOT_FOUND {
            return Err(SecretError::NotFound {
                name: name.to_owned(),
            });
        }
        if !response.status().is_success() {
            return Err(SecretError::Backend(format!(
                "vault get({name}, v={version}): HTTP {}",
                response.status()
            )));
        }
        let body: serde_json::Value = response
            .json()
            .await
            .map_err(|e| SecretError::Backend(format!("vault get parse: {e}")))?;
        Self::extract_value(&body, name)
    }
}

// ─────────────────────────────────────────────────────────────────────
// GcpSecretClient — GCP Secret Manager via thin reqwest HTTP
// ─────────────────────────────────────────────────────────────────────

/// Native GCP Secret Manager `SecretClient`.
///
/// Feature-gated on `gcp-native`. Talks to the Secret Manager REST API
/// v1 with a caller-provided OAuth2 Bearer token. shikumi deliberately
/// does *not* implement ADC / Workload Identity / service-account
/// flows — getting an access token from `gcloud auth print-access-token`
/// or `yup-oauth2` is the caller's responsibility, which keeps the
/// dep tree small (no OpenSSL, no gRPC). Tokens expire after 1 hour;
/// callers should refresh and call [`Self::set_token`] on expiry.
///
/// Secrets are identified by short name (e.g. `"db-password"`). The
/// full resource name is constructed as
/// `projects/{project}/secrets/{name}`.
///
/// Versioning uses GCP's numeric version IDs. `"latest"` is also
/// accepted by `get_version`.
///
/// # Limitations
///
/// - Rotation: GCP Secret Manager doesn't have an API-level rotate
///   action (rotation is a property of the referenced secret version);
///   returns [`SecretError::Unsupported`].
#[cfg(feature = "gcp-native")]
pub struct GcpSecretClient {
    http: reqwest::Client,
    project: String,
    base_url: String,
    token: std::sync::RwLock<String>,
}

#[cfg(feature = "gcp-native")]
#[derive(Debug, Clone)]
pub struct GcpSecretConfig {
    /// GCP project ID (not number). e.g. `"my-project-12345"`.
    pub project: String,
    /// OAuth2 access token with `cloud-platform` scope. Short-lived
    /// (≤1h); caller refreshes via [`GcpSecretClient::set_token`].
    pub token: String,
    /// Override for tests / private API endpoints. Production default
    /// is `https://secretmanager.googleapis.com`.
    pub base_url: Option<String>,
}

#[cfg(feature = "gcp-native")]
impl GcpSecretClient {
    #[must_use]
    pub fn new(config: GcpSecretConfig) -> Self {
        Self {
            http: reqwest::Client::new(),
            project: config.project,
            base_url: config
                .base_url
                .unwrap_or_else(|| "https://secretmanager.googleapis.com".into()),
            token: std::sync::RwLock::new(config.token),
        }
    }

    /// Construct from env: `GCP_PROJECT`, `GCLOUD_ACCESS_TOKEN`.
    ///
    /// # Errors
    ///
    /// Returns [`SecretError::Unauthorized`] if either var is unset.
    pub fn from_env() -> Result<Self, SecretError> {
        let project = std::env::var("GCP_PROJECT").map_err(|_| SecretError::Unauthorized {
            message: "GCP_PROJECT not set".into(),
        })?;
        let token =
            std::env::var("GCLOUD_ACCESS_TOKEN").map_err(|_| SecretError::Unauthorized {
                message: "GCLOUD_ACCESS_TOKEN not set (run `gcloud auth print-access-token`)"
                    .into(),
            })?;
        Ok(Self::new(GcpSecretConfig {
            project,
            token,
            base_url: None,
        }))
    }

    /// Rotate the OAuth2 token (GCP access tokens expire in ~1 hour).
    pub fn set_token(&self, token: impl Into<String>) {
        *self
            .token
            .write()
            .expect("GcpSecretClient token lock poisoned") = token.into();
    }

    fn auth_header(&self) -> String {
        let guard = self
            .token
            .read()
            .expect("GcpSecretClient token lock poisoned");
        format!("Bearer {}", *guard)
    }

    fn secret_url(&self, name: &str) -> String {
        format!(
            "{}/v1/projects/{}/secrets/{}",
            self.base_url, self.project, name
        )
    }

    fn access_url(&self, name: &str, version: &str) -> String {
        format!(
            "{}/v1/projects/{}/secrets/{}/versions/{}:access",
            self.base_url, self.project, name, version
        )
    }

    /// Decode the base64-encoded `payload.data` from a Secret Manager
    /// access response. GCP returns payloads as base64 regardless of
    /// whether they're text or binary; callers that expect text still
    /// receive a UTF-8 string here (and get a Parse error if the bytes
    /// aren't valid UTF-8).
    fn decode_payload(body: &serde_json::Value, name: &str) -> Result<String, SecretError> {
        let data_b64 = body
            .get("payload")
            .and_then(|p| p.get("data"))
            .and_then(|d| d.as_str())
            .ok_or_else(|| {
                SecretError::Backend(format!("gcp {name}: response missing payload.data"))
            })?;
        let bytes = base64_decode(data_b64)
            .map_err(|e| SecretError::Backend(format!("gcp {name}: base64 decode: {e}")))?;
        String::from_utf8(bytes)
            .map_err(|e| SecretError::Backend(format!("gcp {name}: non-UTF8 payload: {e}")))
    }
}

#[cfg(feature = "gcp-native")]
#[async_trait]
impl SecretClient for GcpSecretClient {
    fn backend_name(&self) -> &'static str {
        "gcp-secret-manager"
    }

    fn capabilities(&self) -> Capabilities {
        Capabilities {
            get: true,
            list: true,
            put: true,
            delete: true,
            rotate: false,
            versions: true,
        }
    }

    async fn get(&self, name: &str) -> Result<String, SecretError> {
        let response = self
            .http
            .get(self.access_url(name, "latest"))
            .header("Authorization", self.auth_header())
            .send()
            .await
            .map_err(|e| SecretError::Backend(format!("gcp get({name}): {e}")))?;

        match response.status() {
            reqwest::StatusCode::NOT_FOUND => Err(SecretError::NotFound {
                name: name.to_owned(),
            }),
            reqwest::StatusCode::UNAUTHORIZED | reqwest::StatusCode::FORBIDDEN => {
                Err(SecretError::Unauthorized {
                    message: format!("gcp get({name}): {}", response.status()),
                })
            }
            s if !s.is_success() => Err(SecretError::Backend(format!("gcp get({name}): HTTP {s}"))),
            _ => {
                let body: serde_json::Value = response
                    .json()
                    .await
                    .map_err(|e| SecretError::Backend(format!("gcp get({name}) parse: {e}")))?;
                Self::decode_payload(&body, name)
            }
        }
    }

    async fn list(&self, prefix: Option<&str>) -> Result<Vec<String>, SecretError> {
        let mut names = Vec::new();
        let mut page_token: Option<String> = None;
        loop {
            let mut url = format!(
                "{}/v1/projects/{}/secrets?pageSize=500",
                self.base_url, self.project
            );
            if let Some(tok) = &page_token {
                url.push_str(&format!("&pageToken={tok}"));
            }
            let response = self
                .http
                .get(&url)
                .header("Authorization", self.auth_header())
                .send()
                .await
                .map_err(|e| SecretError::Backend(format!("gcp list-secrets: {e}")))?;

            if !response.status().is_success() {
                return Err(SecretError::Backend(format!(
                    "gcp list-secrets: HTTP {}",
                    response.status()
                )));
            }
            let body: serde_json::Value = response
                .json()
                .await
                .map_err(|e| SecretError::Backend(format!("gcp list-secrets parse: {e}")))?;

            if let Some(secrets) = body.get("secrets").and_then(|v| v.as_array()) {
                for secret in secrets {
                    if let Some(resource_name) = secret.get("name").and_then(|v| v.as_str()) {
                        // Strip the projects/*/secrets/ prefix to get the short name.
                        if let Some(short) =
                            resource_name.rsplit_once('/').map(|(_, n)| n.to_owned())
                        {
                            if prefix.is_none_or(|p| short.starts_with(p)) {
                                names.push(short);
                            }
                        }
                    }
                }
            }
            page_token = body
                .get("nextPageToken")
                .and_then(|v| v.as_str())
                .map(str::to_owned);
            if page_token.is_none() || page_token.as_deref() == Some("") {
                break;
            }
        }
        names.sort();
        Ok(names)
    }

    async fn put(&self, name: &str, value: &str) -> Result<(), SecretError> {
        // Two-step: ensure the secret exists, then add a new version.
        // GCP secrets are container + version; payloads attach to
        // versions, not the secret. If the secret doesn't exist we
        // create it with the automatic replication policy.
        let container_url = self.secret_url(name);
        let get_response = self
            .http
            .get(&container_url)
            .header("Authorization", self.auth_header())
            .send()
            .await
            .map_err(|e| SecretError::Backend(format!("gcp get-secret({name}): {e}")))?;

        if get_response.status() == reqwest::StatusCode::NOT_FOUND {
            let create_url = format!(
                "{}/v1/projects/{}/secrets?secretId={}",
                self.base_url, self.project, name
            );
            let create_body = serde_json::json!({
                "replication": { "automatic": {} }
            });
            let create_response = self
                .http
                .post(&create_url)
                .header("Authorization", self.auth_header())
                .json(&create_body)
                .send()
                .await
                .map_err(|e| SecretError::Backend(format!("gcp create-secret({name}): {e}")))?;
            if !create_response.status().is_success() {
                return Err(SecretError::Backend(format!(
                    "gcp create-secret({name}): HTTP {}",
                    create_response.status()
                )));
            }
        } else if !get_response.status().is_success() {
            return Err(SecretError::Backend(format!(
                "gcp get-secret({name}): HTTP {}",
                get_response.status()
            )));
        }

        // Add a version with the new payload.
        let add_url = format!("{container_url}:addVersion");
        let payload_b64 = base64_encode(value.as_bytes());
        let add_body = serde_json::json!({
            "payload": { "data": payload_b64 }
        });
        let add_response = self
            .http
            .post(&add_url)
            .header("Authorization", self.auth_header())
            .json(&add_body)
            .send()
            .await
            .map_err(|e| SecretError::Backend(format!("gcp add-version({name}): {e}")))?;
        if !add_response.status().is_success() {
            return Err(SecretError::Backend(format!(
                "gcp add-version({name}): HTTP {}",
                add_response.status()
            )));
        }
        Ok(())
    }

    async fn delete(&self, name: &str) -> Result<(), SecretError> {
        let response = self
            .http
            .delete(self.secret_url(name))
            .header("Authorization", self.auth_header())
            .send()
            .await
            .map_err(|e| SecretError::Backend(format!("gcp delete-secret({name}): {e}")))?;
        if response.status() == reqwest::StatusCode::NOT_FOUND {
            return Err(SecretError::NotFound {
                name: name.to_owned(),
            });
        }
        if !response.status().is_success() {
            return Err(SecretError::Backend(format!(
                "gcp delete-secret({name}): HTTP {}",
                response.status()
            )));
        }
        Ok(())
    }

    async fn get_version(&self, name: &str, version: &str) -> Result<String, SecretError> {
        let response = self
            .http
            .get(self.access_url(name, version))
            .header("Authorization", self.auth_header())
            .send()
            .await
            .map_err(|e| SecretError::Backend(format!("gcp get({name}, v={version}): {e}")))?;
        match response.status() {
            reqwest::StatusCode::NOT_FOUND => Err(SecretError::NotFound {
                name: name.to_owned(),
            }),
            s if !s.is_success() => Err(SecretError::Backend(format!(
                "gcp get({name}, v={version}): HTTP {s}"
            ))),
            _ => {
                let body: serde_json::Value = response
                    .json()
                    .await
                    .map_err(|e| SecretError::Backend(format!("gcp get parse: {e}")))?;
                Self::decode_payload(&body, name)
            }
        }
    }
}

#[cfg(feature = "gcp-native")]
fn base64_encode(bytes: &[u8]) -> String {
    // RFC 4648 section 4 (standard) base64 — GCP Secret Manager uses
    // standard base64 (padded) for the payload.data field.
    const ALPHABET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    let mut out = String::with_capacity(bytes.len().div_ceil(3) * 4);
    let mut chunks = bytes.chunks_exact(3);
    for chunk in &mut chunks {
        let n = (u32::from(chunk[0]) << 16) | (u32::from(chunk[1]) << 8) | u32::from(chunk[2]);
        out.push(ALPHABET[((n >> 18) & 0x3F) as usize] as char);
        out.push(ALPHABET[((n >> 12) & 0x3F) as usize] as char);
        out.push(ALPHABET[((n >> 6) & 0x3F) as usize] as char);
        out.push(ALPHABET[(n & 0x3F) as usize] as char);
    }
    let rem = chunks.remainder();
    match rem.len() {
        0 => {}
        1 => {
            let n = u32::from(rem[0]) << 16;
            out.push(ALPHABET[((n >> 18) & 0x3F) as usize] as char);
            out.push(ALPHABET[((n >> 12) & 0x3F) as usize] as char);
            out.push('=');
            out.push('=');
        }
        2 => {
            let n = (u32::from(rem[0]) << 16) | (u32::from(rem[1]) << 8);
            out.push(ALPHABET[((n >> 18) & 0x3F) as usize] as char);
            out.push(ALPHABET[((n >> 12) & 0x3F) as usize] as char);
            out.push(ALPHABET[((n >> 6) & 0x3F) as usize] as char);
            out.push('=');
        }
        _ => unreachable!(),
    }
    out
}

#[cfg(feature = "gcp-native")]
fn base64_decode(s: &str) -> Result<Vec<u8>, String> {
    // Strict standard base64: A-Z, a-z, 0-9, +, /, =. Whitespace is
    // tolerated (GCP sometimes line-wraps large payloads).
    let mut buf = Vec::with_capacity(s.len() * 3 / 4);
    let mut accum: u32 = 0;
    let mut bits: u32 = 0;
    let mut pad = 0usize;
    for c in s.chars() {
        if c.is_ascii_whitespace() {
            continue;
        }
        let v = match c {
            'A'..='Z' => (c as u32) - ('A' as u32),
            'a'..='z' => (c as u32) - ('a' as u32) + 26,
            '0'..='9' => (c as u32) - ('0' as u32) + 52,
            '+' => 62,
            '/' => 63,
            '=' => {
                pad += 1;
                continue;
            }
            _ => return Err(format!("invalid base64 char: {c:?}")),
        };
        if pad > 0 {
            return Err("data after padding".into());
        }
        accum = (accum << 6) | v;
        bits += 6;
        if bits >= 8 {
            bits -= 8;
            let byte = u8::try_from((accum >> bits) & 0xFF).unwrap_or_default();
            buf.push(byte);
            accum &= (1 << bits) - 1;
        }
    }
    if bits != 0 && accum != 0 {
        return Err(format!("trailing bits: {bits}"));
    }
    Ok(buf)
}

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

    #[tokio::test]
    async fn mem_client_get_missing_returns_not_found() {
        let client = MemClient::new();
        match client.get("nonexistent").await {
            Err(SecretError::NotFound { name }) => assert_eq!(name, "nonexistent"),
            other => panic!("expected NotFound, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn mem_client_put_get_roundtrip() {
        let client = MemClient::new();
        client.put("key", "value").await.unwrap();
        assert_eq!(client.get("key").await.unwrap(), "value");
    }

    #[tokio::test]
    async fn mem_client_put_overwrites() {
        let client = MemClient::new();
        client.put("key", "v1").await.unwrap();
        client.put("key", "v2").await.unwrap();
        assert_eq!(client.get("key").await.unwrap(), "v2");
    }

    #[tokio::test]
    async fn mem_client_list_all() {
        let client = MemClient::with_seed([("a", "1"), ("b", "2"), ("c", "3")]);
        let names = client.list(None).await.unwrap();
        assert_eq!(names, vec!["a", "b", "c"]);
    }

    #[tokio::test]
    async fn mem_client_list_with_prefix() {
        let client = MemClient::with_seed([("prod/jwt", "1"), ("prod/api", "2"), ("dev/jwt", "3")]);
        let mut names = client.list(Some("prod/")).await.unwrap();
        names.sort();
        assert_eq!(names, vec!["prod/api", "prod/jwt"]);
    }

    #[tokio::test]
    async fn mem_client_delete_removes() {
        let client = MemClient::with_seed([("gone", "soon")]);
        client.delete("gone").await.unwrap();
        assert!(matches!(
            client.get("gone").await,
            Err(SecretError::NotFound { .. })
        ));
    }

    #[tokio::test]
    async fn mem_client_delete_missing_errors() {
        let client = MemClient::new();
        assert!(matches!(
            client.delete("nope").await,
            Err(SecretError::NotFound { .. })
        ));
    }

    #[tokio::test]
    async fn mem_client_rotate_missing_key_errors() {
        let client = MemClient::new();
        match client.rotate("anything").await {
            Err(SecretError::NotFound { name }) => assert_eq!(name, "anything"),
            other => panic!("expected NotFound, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn mem_client_rotate_appends_version() {
        let client = MemClient::with_seed([("key", "v1")]);
        client.rotate("key").await.unwrap();
        let v1 = client.get_version("key", "1").await.unwrap();
        let v2 = client.get_version("key", "2").await.unwrap();
        assert_eq!(v1, "v1");
        assert!(v2.starts_with("rotated-v2-"));
        // get() returns latest (v2)
        assert_eq!(client.get("key").await.unwrap(), v2);
    }

    #[tokio::test]
    async fn mem_client_versions_track_puts() {
        let client = MemClient::new();
        client.put("key", "v1").await.unwrap();
        client.put("key", "v2").await.unwrap();
        client.put("key", "v3").await.unwrap();
        assert_eq!(client.get_version("key", "1").await.unwrap(), "v1");
        assert_eq!(client.get_version("key", "2").await.unwrap(), "v2");
        assert_eq!(client.get_version("key", "3").await.unwrap(), "v3");
        // get_with_metadata exposes the current version number
        let secret = client.get_with_metadata("key").await.unwrap();
        assert_eq!(secret.value, "v3");
        assert_eq!(secret.metadata.version.as_deref(), Some("3"));
    }

    #[tokio::test]
    async fn mem_client_get_version_out_of_range_errors() {
        let client = MemClient::with_seed([("key", "v1")]);
        assert!(matches!(
            client.get_version("key", "99").await,
            Err(SecretError::Backend(_))
        ));
    }

    #[tokio::test]
    async fn mem_client_capabilities_advertised_full() {
        let caps = MemClient::new().capabilities();
        assert!(caps.get && caps.list && caps.put && caps.delete);
        assert!(caps.rotate && caps.versions);
    }

    #[tokio::test]
    async fn mem_client_get_with_metadata_exposes_version() {
        let client = MemClient::with_seed([("key", "value")]);
        let secret = client.get_with_metadata("key").await.unwrap();
        assert_eq!(secret.value, "value");
        // Seeded values start at version 1; updated_at is None (MemClient
        // isn't a real store with timestamps).
        assert_eq!(secret.metadata.version.as_deref(), Some("1"));
        assert!(secret.metadata.tags.is_empty());
    }

    #[tokio::test]
    async fn command_client_template_substitution() {
        let client = CommandClient::with_get_template("echo resolved-{name}");
        let value = client.get("test").await.unwrap();
        assert_eq!(value, "resolved-test");
    }

    #[tokio::test]
    async fn command_client_name_map() {
        let client =
            CommandClient::with_name_map([("jwt", "echo from-map"), ("api", "echo api-value")]);
        assert_eq!(client.get("jwt").await.unwrap(), "from-map");
        assert_eq!(client.get("api").await.unwrap(), "api-value");
    }

    #[tokio::test]
    async fn command_client_missing_key_errors() {
        let client = CommandClient::with_name_map([("only", "echo x")]);
        assert!(matches!(
            client.get("missing").await,
            Err(SecretError::NotFound { .. })
        ));
    }

    #[tokio::test]
    async fn command_client_write_ops_unsupported() {
        let client = CommandClient::with_get_template("echo {name}");
        assert!(matches!(
            client.put("k", "v").await,
            Err(SecretError::Unsupported {
                operation: "put",
                ..
            })
        ));
        assert!(matches!(
            client.delete("k").await,
            Err(SecretError::Unsupported {
                operation: "delete",
                ..
            })
        ));
        assert!(matches!(
            client.list(None).await,
            Err(SecretError::Unsupported {
                operation: "list",
                ..
            })
        ));
    }

    #[tokio::test]
    async fn command_client_capabilities_read_only() {
        let caps = CommandClient::with_get_template("x").capabilities();
        assert!(caps.get);
        assert!(!caps.put && !caps.delete && !caps.list && !caps.rotate);
    }

    #[tokio::test]
    async fn trait_object_dispatch_works() {
        let client: std::sync::Arc<dyn SecretClient> =
            std::sync::Arc::new(MemClient::with_seed([("key", "value")]));
        assert_eq!(client.get("key").await.unwrap(), "value");
        assert_eq!(client.backend_name(), "mem");
    }

    #[test]
    fn capabilities_read_only_shape() {
        let caps = Capabilities::read_only();
        assert!(caps.get);
        assert!(!caps.list && !caps.put && !caps.delete && !caps.rotate && !caps.versions);
    }

    #[test]
    fn capabilities_full_shape() {
        let caps = Capabilities::full();
        assert!(caps.get && caps.list && caps.put && caps.delete && caps.rotate && caps.versions);
    }

    #[test]
    fn secret_error_not_retryable_by_default() {
        let err = SecretError::NotFound { name: "x".into() };
        assert!(!err.is_retryable());
    }

    #[test]
    fn secret_error_display_shapes() {
        let unauth = SecretError::Unauthorized {
            message: "no token".into(),
        };
        assert!(unauth.to_string().contains("no token"));

        let unsupported = SecretError::Unsupported {
            backend: "sops",
            operation: "rotate",
        };
        assert!(unsupported.to_string().contains("sops"));
        assert!(unsupported.to_string().contains("rotate"));
    }

    // ── SecretOperation — typed axis over the operation universe ───────

    #[test]
    fn secret_operation_all_covers_every_variant() {
        // Pin that ALL enumerates every constructible variant pointwise.
        // The compiler enforces this on the as_str match; the test makes
        // the contract explicit.
        let mut seen: std::collections::HashSet<SecretOperation> = std::collections::HashSet::new();
        for op in SecretOperation::ALL.iter().copied() {
            assert!(seen.insert(op), "duplicate in ALL: {op:?}");
        }
        assert_eq!(seen.len(), 6);
        assert!(seen.contains(&SecretOperation::Get));
        assert!(seen.contains(&SecretOperation::List));
        assert!(seen.contains(&SecretOperation::Put));
        assert!(seen.contains(&SecretOperation::Delete));
        assert!(seen.contains(&SecretOperation::Rotate));
        assert!(seen.contains(&SecretOperation::GetVersion));
    }

    #[test]
    fn secret_operation_all_has_no_duplicates() {
        // The constant is a set. Same discipline as
        // `config_source_kind_all_has_no_duplicates`,
        // `secret_backend_kind_all_has_no_duplicates`, etc.
        let mut sorted: Vec<&'static str> =
            SecretOperation::ALL.iter().map(|o| o.as_str()).collect();
        sorted.sort_unstable();
        let original_len = sorted.len();
        sorted.dedup();
        assert_eq!(
            sorted.len(),
            original_len,
            "SecretOperation::ALL must not list any variant twice",
        );
    }

    #[test]
    fn secret_operation_is_static_copy_hashable() {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        // Static, Copy, Eq, Hash — trait-bounds parity with the sibling
        // closed-axis primitives. Suitable for cross-thread observation
        // and HashMap keys.
        fn assert_send_sync<T: Send + Sync + 'static>() {}
        fn assert_copy<T: Copy>() {}
        fn assert_eq_hash<T: Eq + std::hash::Hash>() {}
        assert_send_sync::<SecretOperation>();
        assert_copy::<SecretOperation>();
        assert_eq_hash::<SecretOperation>();

        // The hash of a Copy value is stable across clones.
        let op = SecretOperation::GetVersion;
        let mut h1 = DefaultHasher::new();
        op.hash(&mut h1);
        let mut h2 = DefaultHasher::new();
        op.hash(&mut h2);
        assert_eq!(h1.finish(), h2.finish());
    }

    #[test]
    fn secret_operation_as_str_yields_canonical_snake_case_names() {
        // Concrete-position pin on the canonical labels. A future
        // rename (e.g. "versions" for GetVersion to match the
        // Capabilities field name) fails here before drifting through
        // the round-trip law or the SecretError::Unsupported message.
        assert_eq!(SecretOperation::Get.as_str(), "get");
        assert_eq!(SecretOperation::List.as_str(), "list");
        assert_eq!(SecretOperation::Put.as_str(), "put");
        assert_eq!(SecretOperation::Delete.as_str(), "delete");
        assert_eq!(SecretOperation::Rotate.as_str(), "rotate");
        assert_eq!(SecretOperation::GetVersion.as_str(), "get_version");
    }

    #[test]
    fn capabilities_supports_matches_field_pointwise() {
        // The (operation → field) projection agrees with direct field
        // reads on a `caps` with every bit flipped to true. Pins the
        // structural alignment between [`SecretOperation`] variants
        // and the matching [`Capabilities`] fields — including the
        // `GetVersion` ↔ `versions` naming asymmetry the typed primitive
        // reconciles.
        let caps = Capabilities {
            get: true,
            list: true,
            put: true,
            delete: true,
            rotate: true,
            versions: true,
        };
        assert_eq!(caps.supports(SecretOperation::Get), caps.get);
        assert_eq!(caps.supports(SecretOperation::List), caps.list);
        assert_eq!(caps.supports(SecretOperation::Put), caps.put);
        assert_eq!(caps.supports(SecretOperation::Delete), caps.delete);
        assert_eq!(caps.supports(SecretOperation::Rotate), caps.rotate);
        assert_eq!(caps.supports(SecretOperation::GetVersion), caps.versions);

        // And on a caps with every bit flipped to false (the not-all-true
        // case, so the alignment doesn't pass trivially).
        let none = Capabilities {
            get: false,
            list: false,
            put: false,
            delete: false,
            rotate: false,
            versions: false,
        };
        for op in SecretOperation::ALL.iter().copied() {
            assert!(!none.supports(op), "no-cap caps must reject {op:?}");
        }

        // Selective: turn on exactly one field and confirm only the
        // matching operation reports supported. Pins the projection is
        // a bijection between the six fields and the six variants.
        let mut probe = none;
        probe.put = true;
        assert!(probe.supports(SecretOperation::Put));
        for op in SecretOperation::ALL.iter().copied() {
            assert_eq!(
                probe.supports(op),
                op == SecretOperation::Put,
                "after flipping only `put`, supports({op:?}) must be (op == Put)",
            );
        }
    }

    #[test]
    fn secret_operation_is_supported_by_dual_agrees_with_capabilities_supports() {
        // The (Capabilities, SecretOperation) projection is symmetric:
        // both sides delegate to the same arm. Pinned over every
        // (caps, op) sample point.
        for caps in [
            Capabilities::read_only(),
            Capabilities::full(),
            Capabilities {
                get: true,
                list: false,
                put: true,
                delete: false,
                rotate: true,
                versions: false,
            },
        ] {
            for op in SecretOperation::ALL.iter().copied() {
                assert_eq!(
                    caps.supports(op),
                    op.is_supported_by(caps),
                    "supports/is_supported_by must agree on {op:?} / {caps:?}",
                );
            }
        }
    }

    #[test]
    fn capabilities_read_only_supports_only_get() {
        let caps = Capabilities::read_only();
        for op in SecretOperation::ALL.iter().copied() {
            assert_eq!(
                caps.supports(op),
                op == SecretOperation::Get,
                "read_only must support exactly Get; got mismatch on {op:?}",
            );
        }
    }

    #[test]
    fn capabilities_full_supports_every_operation() {
        let caps = Capabilities::full();
        for op in SecretOperation::ALL.iter().copied() {
            assert!(caps.supports(op), "full caps must support {op:?}");
        }
    }

    #[test]
    fn secret_error_unsupported_uses_canonical_str_pointwise() {
        // The typed constructor produces the same `operation` string
        // every default trait impl previously hard-coded.
        for op in SecretOperation::ALL.iter().copied() {
            let err = SecretError::unsupported("test-backend", op);
            match err {
                SecretError::Unsupported { backend, operation } => {
                    assert_eq!(backend, "test-backend");
                    assert_eq!(
                        operation,
                        op.as_str(),
                        "constructor must use op.as_str() pointwise on {op:?}",
                    );
                }
                other => panic!("expected Unsupported, got {other:?}"),
            }
        }
    }

    #[tokio::test]
    async fn secret_client_default_unsupported_arms_use_secret_operation_labels() {
        fn assert_unsupported_with_op(
            result: Result<(), SecretError>,
            backend_expected: &'static str,
            op: SecretOperation,
        ) {
            match result {
                Err(SecretError::Unsupported { backend, operation }) => {
                    assert_eq!(backend, backend_expected);
                    assert_eq!(
                        operation,
                        op.as_str(),
                        "default impl for {op:?} must emit op.as_str() as the operation tag",
                    );
                }
                other => panic!("expected Unsupported({op:?}), got {other:?}"),
            }
        }

        // The five default trait impls each route through
        // `SecretError::unsupported(_, SecretOperation::X)`, so the
        // `operation` string on the raised error is exactly
        // `SecretOperation::X.as_str()`. Pinned via the CommandClient,
        // whose write/list/rotate/get_version methods inherit the
        // default impls without overriding them.
        let client = CommandClient::with_get_template("echo {name}");
        let backend = client.backend_name();

        assert_unsupported_with_op(
            client.list(None).await.map(|_| ()),
            backend,
            SecretOperation::List,
        );
        assert_unsupported_with_op(client.put("k", "v").await, backend, SecretOperation::Put);
        assert_unsupported_with_op(client.delete("k").await, backend, SecretOperation::Delete);
        assert_unsupported_with_op(client.rotate("k").await, backend, SecretOperation::Rotate);
        assert_unsupported_with_op(
            client.get_version("k", "1").await.map(|_| ()),
            backend,
            SecretOperation::GetVersion,
        );
    }

    // ── SecretErrorKind — typed kind axis over the SecretError variant space ──

    /// Construction table: one representative [`SecretError`] for each
    /// expected [`SecretErrorKind`] arm, in the same declaration order
    /// as `SecretErrorKind::ALL`. Reused across the per-kind pin tests.
    fn one_per_secret_error_kind() -> [(SecretError, SecretErrorKind); 5] {
        [
            (
                SecretError::NotFound { name: "x".into() },
                SecretErrorKind::NotFound,
            ),
            (
                SecretError::Unauthorized {
                    message: "no token".into(),
                },
                SecretErrorKind::Unauthorized,
            ),
            (
                SecretError::Unsupported {
                    backend: "sops",
                    operation: "rotate",
                },
                SecretErrorKind::Unsupported,
            ),
            (
                SecretError::Backend("connection refused".into()),
                SecretErrorKind::Backend,
            ),
            (
                SecretError::Shikumi(ShikumiError::NotFound { tried: Vec::new() }),
                SecretErrorKind::Shikumi,
            ),
        ]
    }

    #[test]
    fn secret_error_kind_all_covers_every_variant() {
        // The closed list ALL enumerates exactly the five kinds the
        // construction table produces. Mirrors the
        // `shikumi_error_kind_all_covers_every_constructed_variant`
        // pin on the [`ShikumiErrorKind`] axis.
        let mut seen: std::collections::HashSet<SecretErrorKind> = std::collections::HashSet::new();
        for kind in SecretErrorKind::ALL.iter().copied() {
            assert!(seen.insert(kind), "duplicate in ALL: {kind:?}");
        }
        assert_eq!(seen.len(), 5);
        for (_, expected) in one_per_secret_error_kind() {
            assert!(
                seen.contains(&expected),
                "construction-table kind {expected:?} missing from SecretErrorKind::ALL",
            );
        }
    }

    #[test]
    fn secret_error_kind_all_has_no_duplicates() {
        // The constant is a set. Same discipline as the sibling
        // closed-axis primitives.
        let mut sorted: Vec<&'static str> =
            SecretErrorKind::ALL.iter().map(|k| k.as_str()).collect();
        sorted.sort_unstable();
        let original_len = sorted.len();
        sorted.dedup();
        assert_eq!(
            sorted.len(),
            original_len,
            "SecretErrorKind::ALL must not list any variant twice",
        );
    }

    #[test]
    fn secret_error_kind_is_static_copy_hashable() {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        // Static, Copy, Eq, Hash — trait-bounds parity with the sibling
        // closed-axis primitives. Suitable for cross-thread observation
        // and HashMap keys.
        fn assert_send_sync<T: Send + Sync + 'static>() {}
        fn assert_copy<T: Copy>() {}
        fn assert_eq_hash<T: Eq + std::hash::Hash>() {}
        assert_send_sync::<SecretErrorKind>();
        assert_copy::<SecretErrorKind>();
        assert_eq_hash::<SecretErrorKind>();

        let kind = SecretErrorKind::Backend;
        let mut h1 = DefaultHasher::new();
        kind.hash(&mut h1);
        let mut h2 = DefaultHasher::new();
        kind.hash(&mut h2);
        assert_eq!(h1.finish(), h2.finish());
    }

    #[test]
    fn secret_error_kind_as_str_yields_canonical_lowercase_names() {
        // Concrete-position pin on the canonical labels. A future
        // rename (e.g. switching `"backend"` to `"transport"`,
        // capitalizing `"NotFound"`, dropping the `"not-"` prefix on
        // [`SecretErrorKind::NotFound`]) fails here before drifting
        // through the trait-uniform round-trip law.
        assert_eq!(SecretErrorKind::NotFound.as_str(), "not-found");
        assert_eq!(SecretErrorKind::Unauthorized.as_str(), "unauthorized");
        assert_eq!(SecretErrorKind::Unsupported.as_str(), "unsupported");
        assert_eq!(SecretErrorKind::Backend.as_str(), "backend");
        assert_eq!(SecretErrorKind::Shikumi.as_str(), "shikumi");
    }

    #[test]
    fn secret_error_kind_pins_every_variant_pointwise() {
        // The (SecretError → SecretErrorKind) projection assigns the
        // expected kind to every construction-table entry. Pins the
        // forward map at the type level — a future variant addition
        // forces a new arm in the exhaustive `SecretError::kind` match,
        // which forces a new construction-table row, which forces an
        // ALL entry through `secret_error_kind_all_covers_every_variant`.
        for (err, expected_kind) in one_per_secret_error_kind() {
            assert_eq!(
                err.kind(),
                expected_kind,
                "SecretError::kind on {err:?} must yield {expected_kind:?}",
            );
        }
    }

    #[test]
    fn secret_error_kind_image_lies_in_secret_error_kind_all() {
        // Cover law: every kind read from a construction-table entry
        // lies in [`SecretErrorKind::ALL`]. The projection cannot
        // escape the closed five-way partition.
        for (err, _) in one_per_secret_error_kind() {
            assert!(
                SecretErrorKind::ALL.contains(&err.kind()),
                "SecretError::kind({err:?}) must lie in SecretErrorKind::ALL",
            );
        }
    }

    #[test]
    fn secret_error_kind_pins_unsupported_payload_independence() {
        // The kind projection is payload-free on the [`Self::Unsupported`]
        // arm: any (backend, operation) pair produces
        // [`SecretErrorKind::Unsupported`]. Witnesses the data-free
        // discipline pointwise on the surface that carries the most
        // structured payload.
        for op in SecretOperation::ALL.iter().copied() {
            let err = SecretError::unsupported("any-backend", op);
            assert_eq!(
                err.kind(),
                SecretErrorKind::Unsupported,
                "unsupported({op:?}) must classify as SecretErrorKind::Unsupported",
            );
        }
    }

    #[test]
    fn secret_error_as_shikumi_agrees_with_kind_pointwise() {
        // The (`as_shikumi().is_some()` ↔ `kind() == Shikumi`)
        // structural law holds for every construction-table entry.
        // Dual to the `Self::Shikumi` arm of `SecretError::kind`.
        for (err, expected_kind) in one_per_secret_error_kind() {
            assert_eq!(
                err.as_shikumi().is_some(),
                expected_kind == SecretErrorKind::Shikumi,
                "as_shikumi().is_some() must match (kind == Shikumi) on {err:?}",
            );
        }
    }

    #[test]
    fn secret_error_as_shikumi_recovers_inner_pointwise() {
        // On the [`Self::Shikumi`] arm, `as_shikumi()` recovers a
        // reference to the wrapped [`ShikumiError`] whose own
        // [`ShikumiError::kind`] refines the cross-kind partition on
        // the wrapped-shikumi sub-axis. Probe over every shikumi-side
        // kind to witness the structural composition.
        for shikumi_kind in crate::ShikumiErrorKind::ALL.iter().copied() {
            // Reuse the simplest constructible ShikumiError per kind —
            // NotFound is data-light and constructible without figment.
            // The wrapped-shikumi kind refines through the inner
            // ShikumiError, not through SecretErrorKind itself.
            let inner = match shikumi_kind {
                crate::ShikumiErrorKind::NotFound => ShikumiError::NotFound { tried: Vec::new() },
                _ => continue,
            };
            let err = SecretError::Shikumi(inner);
            let recovered = err.as_shikumi().expect("Self::Shikumi must yield Some");
            assert_eq!(
                recovered.kind(),
                shikumi_kind,
                "as_shikumi must preserve inner ShikumiError::kind ({shikumi_kind:?})",
            );
            assert_eq!(err.kind(), SecretErrorKind::Shikumi);
        }
    }

    #[cfg(feature = "op-native")]
    #[test]
    fn op_urlencode_handles_spaces_and_reserved_chars() {
        assert_eq!(urlencode("simple"), "simple");
        assert_eq!(urlencode("with space"), "with%20space");
        assert_eq!(urlencode("a/b?c=d&e"), "a%2Fb%3Fc%3Dd%26e");
        // Unreserved chars stay as-is.
        assert_eq!(urlencode("a-b_c.d~e"), "a-b_c.d~e");
    }

    #[cfg(feature = "op-native")]
    #[test]
    fn op_connect_client_constructs_from_config() {
        let client = OpConnectClient::new(OpConnectConfig {
            base_url: "https://connect.example.com/".into(),
            token: "bearer-tok".into(),
            vault_id: "VAULT_UUID".into(),
        });
        assert_eq!(client.backend_name(), "op-connect");
        let caps = client.capabilities();
        assert!(caps.get && caps.list && caps.put && caps.delete);
        assert!(!caps.rotate && !caps.versions);
        // Trailing slash trimmed.
        assert_eq!(client.base_url, "https://connect.example.com");
    }

    #[cfg(feature = "vault-native")]
    #[test]
    fn vault_client_constructs_from_config() {
        let client = VaultClient::new(VaultConfig {
            base_url: "https://vault.example.com:8200/".into(),
            token: "vault-tok".into(),
            mount: "/secret/".into(),
            namespace: Some("admin/team-a".into()),
        });
        assert_eq!(client.backend_name(), "vault");
        let caps = client.capabilities();
        assert!(caps.get && caps.list && caps.put && caps.delete && caps.versions);
        assert!(!caps.rotate);
        assert_eq!(client.base_url, "https://vault.example.com:8200");
        assert_eq!(client.mount, "secret");
    }

    #[cfg(feature = "vault-native")]
    #[test]
    fn vault_url_construction() {
        let client = VaultClient::new(VaultConfig {
            base_url: "https://vault.example.com:8200".into(),
            token: "t".into(),
            mount: "secret".into(),
            namespace: None,
        });
        assert_eq!(
            client.data_url("foo/bar"),
            "https://vault.example.com:8200/v1/secret/data/foo/bar"
        );
        assert_eq!(
            client.metadata_url("foo/bar"),
            "https://vault.example.com:8200/v1/secret/metadata/foo/bar"
        );
        // Leading slash on path is tolerated.
        assert_eq!(
            client.data_url("/foo"),
            "https://vault.example.com:8200/v1/secret/data/foo"
        );
    }

    #[cfg(feature = "vault-native")]
    #[test]
    fn vault_extract_value_single_field() {
        let body = serde_json::json!({
            "data": { "data": { "value": "hello" } }
        });
        assert_eq!(VaultClient::extract_value(&body, "x").unwrap(), "hello");
    }

    #[cfg(feature = "vault-native")]
    #[test]
    fn vault_extract_value_multi_field_returns_json_string() {
        let body = serde_json::json!({
            "data": { "data": { "username": "u", "password": "p" } }
        });
        let v = VaultClient::extract_value(&body, "x").unwrap();
        // JSON object — contains both keys, order indeterminate.
        assert!(v.contains("\"username\":\"u\""));
        assert!(v.contains("\"password\":\"p\""));
    }

    #[cfg(feature = "vault-native")]
    #[test]
    fn vault_extract_value_missing_errors() {
        let body = serde_json::json!({ "data": {} });
        assert!(matches!(
            VaultClient::extract_value(&body, "x"),
            Err(SecretError::Backend(_))
        ));
    }

    #[cfg(feature = "gcp-native")]
    #[test]
    fn gcp_base64_roundtrip() {
        // Empty
        assert_eq!(base64_encode(b""), "");
        assert_eq!(base64_decode("").unwrap(), b"");
        // Single byte → 2 chars + ==
        assert_eq!(base64_encode(b"f"), "Zg==");
        assert_eq!(base64_decode("Zg==").unwrap(), b"f");
        // Two bytes → 3 chars + =
        assert_eq!(base64_encode(b"fo"), "Zm8=");
        assert_eq!(base64_decode("Zm8=").unwrap(), b"fo");
        // Three bytes → 4 chars
        assert_eq!(base64_encode(b"foo"), "Zm9v");
        assert_eq!(base64_decode("Zm9v").unwrap(), b"foo");
        // Longer
        assert_eq!(base64_encode(b"hello world"), "aGVsbG8gd29ybGQ=");
        assert_eq!(base64_decode("aGVsbG8gd29ybGQ=").unwrap(), b"hello world");
        // Binary-ish bytes (GCP payloads are sometimes non-UTF8)
        let bin: Vec<u8> = (0..=255).collect();
        assert_eq!(base64_decode(&base64_encode(&bin)).unwrap(), bin);
    }

    #[cfg(feature = "gcp-native")]
    #[test]
    fn gcp_base64_tolerates_whitespace() {
        // GCP occasionally line-wraps payloads; our decoder must be
        // lenient about whitespace to match the server's output.
        let wrapped = "aGVs\nbG8g\nd29y\nbGQ=";
        assert_eq!(base64_decode(wrapped).unwrap(), b"hello world");
    }

    #[cfg(feature = "gcp-native")]
    #[test]
    fn gcp_base64_rejects_invalid_chars() {
        assert!(base64_decode("not*valid").is_err());
        assert!(base64_decode("Zg==extra").is_err()); // data after padding
    }

    #[cfg(feature = "gcp-native")]
    #[test]
    fn gcp_client_constructs_from_config() {
        let client = GcpSecretClient::new(GcpSecretConfig {
            project: "my-project".into(),
            token: "ya29.abc123".into(),
            base_url: None,
        });
        assert_eq!(client.backend_name(), "gcp-secret-manager");
        let caps = client.capabilities();
        assert!(caps.get && caps.list && caps.put && caps.delete && caps.versions);
        assert!(!caps.rotate);
        assert_eq!(client.project, "my-project");
        assert!(client.base_url.starts_with("https://"));
    }

    #[cfg(feature = "gcp-native")]
    #[test]
    fn gcp_url_construction() {
        let client = GcpSecretClient::new(GcpSecretConfig {
            project: "p".into(),
            token: "t".into(),
            base_url: Some("https://test.googleapis.com".into()),
        });
        assert_eq!(
            client.secret_url("db-password"),
            "https://test.googleapis.com/v1/projects/p/secrets/db-password"
        );
        assert_eq!(
            client.access_url("db-password", "latest"),
            "https://test.googleapis.com/v1/projects/p/secrets/db-password/versions/latest:access"
        );
        assert_eq!(
            client.access_url("db-password", "7"),
            "https://test.googleapis.com/v1/projects/p/secrets/db-password/versions/7:access"
        );
    }

    #[cfg(feature = "gcp-native")]
    #[test]
    fn gcp_token_rotation() {
        let client = GcpSecretClient::new(GcpSecretConfig {
            project: "p".into(),
            token: "old".into(),
            base_url: None,
        });
        assert_eq!(client.auth_header(), "Bearer old");
        client.set_token("new");
        assert_eq!(client.auth_header(), "Bearer new");
    }

    #[cfg(feature = "gcp-native")]
    #[test]
    fn gcp_decode_payload_happy_path() {
        let body = serde_json::json!({
            "name": "projects/p/secrets/s/versions/1",
            "payload": { "data": "aGVsbG8=" }
        });
        assert_eq!(
            GcpSecretClient::decode_payload(&body, "s").unwrap(),
            "hello"
        );
    }

    #[cfg(feature = "gcp-native")]
    #[test]
    fn gcp_decode_payload_missing_data_errors() {
        let body = serde_json::json!({ "name": "x", "payload": {} });
        assert!(matches!(
            GcpSecretClient::decode_payload(&body, "x"),
            Err(SecretError::Backend(_))
        ));
    }

    // ── SecretClientKind — typed axis over the SecretClient impl universe ──

    #[test]
    fn secret_client_kind_all_covers_every_variant() {
        // Pin that ALL enumerates every constructible variant pointwise.
        // The compiler enforces this on the as_str match; the test makes
        // the contract explicit. Same discipline as
        // `secret_operation_all_covers_every_variant`,
        // `secret_error_kind_all_covers_every_variant`.
        let mut seen: std::collections::HashSet<SecretClientKind> =
            std::collections::HashSet::new();
        for kind in SecretClientKind::ALL.iter().copied() {
            assert!(seen.insert(kind), "duplicate in ALL: {kind:?}");
        }
        assert_eq!(seen.len(), 7);
        assert!(seen.contains(&SecretClientKind::Mem));
        assert!(seen.contains(&SecretClientKind::Command));
        assert!(seen.contains(&SecretClientKind::Akeyless));
        assert!(seen.contains(&SecretClientKind::AwsSecretsManager));
        assert!(seen.contains(&SecretClientKind::OpConnect));
        assert!(seen.contains(&SecretClientKind::Vault));
        assert!(seen.contains(&SecretClientKind::GcpSecretManager));
    }

    #[test]
    fn secret_client_kind_all_has_no_duplicates() {
        // The constant is a set. Same discipline as
        // `secret_error_kind_all_has_no_duplicates`,
        // `secret_operation_all_has_no_duplicates`.
        let mut sorted: Vec<&'static str> =
            SecretClientKind::ALL.iter().map(|k| k.as_str()).collect();
        sorted.sort_unstable();
        let original_len = sorted.len();
        sorted.dedup();
        assert_eq!(
            sorted.len(),
            original_len,
            "SecretClientKind::ALL must not list any variant twice",
        );
    }

    #[test]
    fn secret_client_kind_is_static_copy_hashable() {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        // Static, Copy, Eq, Hash — trait-bounds parity with the sibling
        // closed-axis primitives. Suitable for cross-thread observation
        // and HashMap keys.
        fn assert_send_sync<T: Send + Sync + 'static>() {}
        fn assert_copy<T: Copy>() {}
        fn assert_eq_hash<T: Eq + std::hash::Hash>() {}
        assert_send_sync::<SecretClientKind>();
        assert_copy::<SecretClientKind>();
        assert_eq_hash::<SecretClientKind>();

        // The hash of a Copy value is stable across clones.
        let k = SecretClientKind::AwsSecretsManager;
        let mut h1 = DefaultHasher::new();
        k.hash(&mut h1);
        let mut h2 = DefaultHasher::new();
        k.hash(&mut h2);
        assert_eq!(h1.finish(), h2.finish());
    }

    #[test]
    fn secret_client_kind_as_str_yields_canonical_names() {
        // Concrete-position pin on the canonical labels. A future rename
        // (e.g. shortening `"aws-secrets-manager"` to `"aws"`, expanding
        // `"mem"` to `"in-memory"`, dropping the `-secrets-manager`
        // suffix) fails here before drifting through the round-trip law
        // or the per-impl `backend_name()` pins below.
        assert_eq!(SecretClientKind::Mem.as_str(), "mem");
        assert_eq!(SecretClientKind::Command.as_str(), "command");
        assert_eq!(SecretClientKind::Akeyless.as_str(), "akeyless");
        assert_eq!(
            SecretClientKind::AwsSecretsManager.as_str(),
            "aws-secrets-manager",
        );
        assert_eq!(SecretClientKind::OpConnect.as_str(), "op-connect");
        assert_eq!(SecretClientKind::Vault.as_str(), "vault");
        assert_eq!(
            SecretClientKind::GcpSecretManager.as_str(),
            "gcp-secret-manager",
        );
    }

    #[test]
    fn secret_client_kind_as_str_pins_mem_client_backend_name() {
        // The lift's structural contract: the canonical label of
        // [`SecretClientKind::Mem`] is byte-identical to
        // [`MemClient::backend_name`]. Pinned per-impl so a future
        // re-label of either side fails at one site.
        let client = MemClient::new();
        assert_eq!(client.backend_name(), SecretClientKind::Mem.as_str());
    }

    #[test]
    fn secret_client_kind_as_str_pins_command_client_backend_name() {
        let client = CommandClient::with_get_template("echo x");
        assert_eq!(client.backend_name(), SecretClientKind::Command.as_str());
    }

    #[test]
    fn secret_client_default_client_kind_recovers_mem_kind() {
        // The trait-default `client_kind` derives from `backend_name`
        // via `SecretClientKind::from_canonical_str`. For a
        // shikumi-shipped impl, the projection must round-trip to
        // [`Some(_)`] on the matching variant — the operative agreement
        // pin between the `backend_name` string axis and the
        // [`SecretClientKind`] typed axis.
        let client = MemClient::new();
        assert_eq!(client.client_kind(), Some(SecretClientKind::Mem));
    }

    #[test]
    fn secret_client_default_client_kind_recovers_command_kind() {
        let client = CommandClient::with_get_template("echo x");
        assert_eq!(client.client_kind(), Some(SecretClientKind::Command));
    }

    #[test]
    fn secret_client_default_client_kind_recovers_backend_name_pointwise() {
        // The structural law:
        //   `self.client_kind().map(SecretClientKind::as_str) == Some(self.backend_name())`
        // for every shikumi-shipped impl. Always-available impls
        // ([`MemClient`], [`CommandClient`]) pinned here; per-feature
        // impls pinned below.
        let mem = MemClient::new();
        assert_eq!(
            mem.client_kind().map(SecretClientKind::as_str),
            Some(mem.backend_name()),
        );

        let cmd = CommandClient::with_get_template("x");
        assert_eq!(
            cmd.client_kind().map(SecretClientKind::as_str),
            Some(cmd.backend_name()),
        );
    }

    #[cfg(feature = "op-native")]
    #[test]
    fn secret_client_kind_recovers_op_connect_backend_name() {
        let client = OpConnectClient::new(OpConnectConfig {
            base_url: "https://connect.example.com/".into(),
            token: "t".into(),
            vault_id: "v".into(),
        });
        assert_eq!(client.client_kind(), Some(SecretClientKind::OpConnect));
        assert_eq!(
            client.client_kind().map(SecretClientKind::as_str),
            Some(client.backend_name()),
        );
    }

    #[cfg(feature = "vault-native")]
    #[test]
    fn secret_client_kind_recovers_vault_backend_name() {
        let client = VaultClient::new(VaultConfig {
            base_url: "https://vault.example.com:8200/".into(),
            token: "t".into(),
            mount: "/secret/".into(),
            namespace: None,
        });
        assert_eq!(client.client_kind(), Some(SecretClientKind::Vault));
        assert_eq!(
            client.client_kind().map(SecretClientKind::as_str),
            Some(client.backend_name()),
        );
    }

    #[test]
    fn secret_client_kind_image_lies_in_secret_client_kind_all() {
        // Every variant of [`SecretClientKind`] appears in
        // [`SecretClientKind::ALL`]. Composes the as_str canonical
        // labels with the trait-default `from_canonical_str` round-trip
        // law (also pinned by
        // `closed_axis_label_round_trips_for_every_implementor` in
        // `cube::tests`).
        for kind in SecretClientKind::ALL.iter().copied() {
            let parsed =
                <SecretClientKind as crate::ClosedAxisLabel>::from_canonical_str(kind.as_str());
            assert_eq!(parsed, Some(kind), "round-trip failed for {kind:?}");
        }
    }
}