greentic-deployer 1.1.0-dev.27671069765

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

use std::collections::{BTreeMap, BTreeSet};
use std::io::IsTerminal;
use std::path::{Path, PathBuf};

use greentic_deploy_spec::{
    BundleDeploymentStatus, CustomerId, DeploymentId, EnvId, Environment, MessagingEndpoint,
    RouteBinding,
};
use serde_json::{Value, json};
use sha2::{Digest, Sha256};

use crate::environment::{EnvironmentStore, LocalFsStore, trust_root as store_trust_root};
use crate::runtime_secrets::SecretValue;

use super::bundles::{
    BundleUpdatePayload, RevenueShareEntryPayload, RouteBindingPayload, convert_revenue_share,
    into_route_binding,
};
use super::config::ConfigSetPayload;
use super::deploy::BundleDeployPayload;
use super::env::EnvInitPayload;
use super::env_manifest::{
    ENV_MANIFEST_SCHEMA_V1, EnvManifest, ManifestBundle, ManifestEndpoint, ManifestRevision,
    ManifestWelcomeFlow, TrustRootDirective, compute_effective_weights_bps, manifest_schema,
};
use super::env_packs::EnvPackBindingPayload;
use super::extensions::ExtensionBindingPayload;
use super::messaging::{
    EndpointAddPayload, EndpointLinkBundlePayload, EndpointSetWelcomeFlowPayload, EndpointSummary,
};
use super::secrets::SecretsPutPayload;
use super::trust_root::TrustRootBootstrapPayload;
use super::{OpError, OpFlags, OpOutcome};

const NOUN: &str = "env";
const VERB: &str = "apply";

/// Audit principal stamped on every composed mutation when the caller
/// doesn't pass `--updated-by`.
const DEFAULT_UPDATED_BY: &str = "env-apply";

// --- plan model ---------------------------------------------------------------

/// What the diff decided for one step. `Put` is the secrets-only
/// "cannot diff, write unconditionally" action (`op secrets get` is
/// not-yet-implemented for every backend, so a no-op can never be claimed);
/// warning-carrying no-ops surface through the plan's `warnings` list
/// instead of a dedicated `Skip` action.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ApplyAction {
    Create,
    Update,
    Put,
    NoOp,
}

impl ApplyAction {
    fn as_str(self) -> &'static str {
        match self {
            ApplyAction::Create => "create",
            ApplyAction::Update => "update",
            ApplyAction::Put => "put",
            ApplyAction::NoOp => "no-op",
        }
    }

    /// Does this action represent *diffable* drift for the `--check`
    /// convergence verdict? `Put` is excluded: always-put secret rows are
    /// unknowable until A9 lands a real `secrets get`, so counting them
    /// would make `--check` permanently red for any manifest with a
    /// `secrets[]` section. They stay visible in the report (`put` rows
    /// plus the `undiffable` count) instead of failing the gate.
    fn counts_as_drift(self) -> bool {
        matches!(self, ApplyAction::Create | ApplyAction::Update)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ApplyStepKind {
    EnsureEnvironment,
    UpdateHostConfig,
    BootstrapTrustRoot,
    AddPackBinding,
    UpdatePackBinding,
    PutSecret,
    DeployBundle,
    DeploySplit,
    UpdateBundle,
    AddExtension,
    UpdateExtension,
    AddEndpoint,
    LinkEndpoint,
    SetWelcomeFlow,
}

impl ApplyStepKind {
    fn label(self) -> &'static str {
        match self {
            ApplyStepKind::EnsureEnvironment => "ensure-environment",
            ApplyStepKind::UpdateHostConfig => "update-host-config",
            ApplyStepKind::BootstrapTrustRoot => "bootstrap-trust-root",
            ApplyStepKind::AddPackBinding => "add-pack-binding",
            ApplyStepKind::UpdatePackBinding => "update-pack-binding",
            ApplyStepKind::PutSecret => "put-secret",
            ApplyStepKind::DeployBundle => "deploy-bundle",
            ApplyStepKind::DeploySplit => "deploy-split",
            ApplyStepKind::UpdateBundle => "update-bundle",
            ApplyStepKind::AddExtension => "add-extension",
            ApplyStepKind::UpdateExtension => "update-extension",
            ApplyStepKind::AddEndpoint => "add-endpoint",
            ApplyStepKind::LinkEndpoint => "link-endpoint",
            ApplyStepKind::SetWelcomeFlow => "set-welcome-flow",
        }
    }
}

/// One revision inside a [`StepOp::DeploySplit`]. Carries the resolved
/// artifact path and digest for TOCTOU re-verification, plus the effective
/// weight already computed by [`compute_effective_weights_bps`].
#[derive(Debug, Clone)]
struct SplitRevisionEntry {
    name: String,
    resolved_path: PathBuf,
    expected_digest: String,
    weight_bps: u32,
    drain_seconds: Option<u32>,
}

/// Reference to an endpoint that may not exist yet at plan time. `Created`
/// is resolved in-process during execution from the `add-endpoint` outcome.
#[derive(Debug, Clone)]
enum EndpointRef {
    Existing(String),
    CreatedByName(String),
}

/// The typed mutation a step performs at execute time. `None` for no-ops.
#[derive(Debug, Clone)]
enum StepOp {
    None,
    EnvInit {
        public_base_url: Option<String>,
    },
    SetPublicUrl {
        url: String,
    },
    TrustRootBootstrap,
    /// The resolved VALUE deliberately does not live in the step (steps
    /// derive `Debug` and serialize into the plan/report) — execute looks it
    /// up in [`ApplyContext::secret_values`] by this path.
    PutSecret {
        path: String,
    },
    Deploy {
        payload: Box<BundleDeployPayload>,
        /// Digest recorded at plan time; re-verified just before the deploy
        /// step executes to shrink the validate→execute TOCTOU window.
        expected_digest: String,
    },
    /// Multi-revision traffic-split deploy: stage each revision, warm it,
    /// then set the combined traffic split. Each entry carries its own
    /// artifact path and digest for TOCTOU re-verification.
    DeploySplit {
        env_id: String,
        bundle_id: String,
        customer_id: Option<String>,
        config_overrides: Option<BTreeMap<String, BTreeMap<String, Value>>>,
        route_binding: Option<RouteBindingPayload>,
        /// Revenue-share split applied on the FRESH-add path (`None` =
        /// `greentic@10000`). Reconciled via `bundles update` for an
        /// existing deployment.
        revenue_share: Option<Vec<RevenueShareEntryPayload>>,
        revisions: Vec<SplitRevisionEntry>,
    },
    UpdateHostConfig(Box<ConfigSetPayload>),
    AddPackBinding(Box<EnvPackBindingPayload>),
    UpdatePackBinding(Box<EnvPackBindingPayload>),
    AddExtension(Box<ExtensionBindingPayload>),
    UpdateExtension(Box<ExtensionBindingPayload>),
    BundleUpdate(Box<BundleUpdatePayload>),
    EndpointAdd(Box<EndpointAddPayload>),
    EndpointLink {
        endpoint: EndpointRef,
        bundle_id: String,
    },
    WelcomeFlow {
        endpoint: EndpointRef,
        flow: ManifestWelcomeFlow,
    },
}

#[derive(Debug, Clone)]
struct ApplyStep {
    kind: ApplyStepKind,
    key: String,
    action: ApplyAction,
    detail: String,
    idempotency_key: Option<String>,
    op: StepOp,
}

impl ApplyStep {
    fn no_op(kind: ApplyStepKind, key: impl Into<String>, detail: impl Into<String>) -> Self {
        Self {
            kind,
            key: key.into(),
            action: ApplyAction::NoOp,
            detail: detail.into(),
            idempotency_key: None,
            op: StepOp::None,
        }
    }

    fn to_json(&self) -> Value {
        json!({
            "kind": self.kind.label(),
            "key": self.key,
            "action": self.action.as_str(),
            "detail": self.detail,
            "idempotency_key": self.idempotency_key,
        })
    }
}

// --- validated context ---------------------------------------------------------

/// One resolved revision inside a multi-revision bundle entry.
struct ResolvedRevision {
    spec: ManifestRevision,
    resolved_path: PathBuf,
    digest: String,
    /// Effective weight in basis points, computed by
    /// [`compute_effective_weights_bps`].
    weight_bps: u32,
}

/// A manifest bundle entry paired with its resolved artifact metadata.
/// For single-revision entries (`bundle_path` form), `revisions` holds
/// exactly one entry at full weight. Multi-revision entries hold one per
/// declared revision.
struct ResolvedBundle {
    spec: ManifestBundle,
    /// Billing principal resolved during validation (from
    /// `resolve_customer_id`). Used to match deployments by the same
    /// `(bundle_id, customer_id)` pair that `op deploy` keys on.
    customer_id: CustomerId,
    /// Resolved revisions. Always non-empty after validation.
    revisions: Vec<ResolvedRevision>,
}

/// A manifest endpoint entry paired with its store match (if any).
struct ResolvedEndpoint {
    spec: ManifestEndpoint,
    /// Cloned from the store — `None` when the endpoint is new.
    matched: Option<MessagingEndpoint>,
}

/// Everything validation established, handed to diff/execute/verify.
struct ApplyContext {
    env_id: EnvId,
    manifest: EnvManifest,
    /// Secret values keyed by the manifest's verbatim `path`, resolved from
    /// `from_env` during validation. Never serialized; [`SecretValue`]'s
    /// `Debug` renders a fixed placeholder, so no derived-`Debug` surface
    /// can echo the material — the value reaches exactly one place, the
    /// `secrets::put` payload at execute time.
    secret_values: BTreeMap<String, SecretValue>,
    /// Secret paths whose value came from the TTY prompter rather than the
    /// named env var — the plan row says `prompted` instead of `from $VAR`.
    prompted_paths: BTreeSet<String>,
    /// Paste-sourced secret paths already present in the env's store (no value
    /// to collect or put — store-as-source-of-truth). These get neither a put
    /// step nor a missing-input report; `diff` skips them entirely.
    store_satisfied_paths: BTreeSet<String>,
    bundles: Vec<ResolvedBundle>,
    endpoints: Vec<ResolvedEndpoint>,
    env: Option<Environment>,
    /// Canonicalized `environment.public_base_url` (validated form).
    canonical_public_base_url: Option<String>,
    /// Accumulated input gaps (see [`MissingItem`]): secrets in manifest
    /// order, then bundles in manifest order.
    missing: Vec<MissingItem>,
    warnings: Vec<String>,
    updated_by: String,
}

// --- entry point ----------------------------------------------------------------

/// An input the manifest names but the process cannot supply: an unset
/// (or empty) `from_env` variable, or a `bundle_path` that is not a file.
/// Missing inputs are *accumulated* during validation (unlike structural
/// manifest errors, which stay fail-fast) so headless callers see the
/// complete list in one run, and reported under `missing` in the JSON
/// report. Apply mode refuses to execute while any remain; preview modes
/// (`--dry-run`, `--check`) report them without failing.
#[derive(Debug, Clone, PartialEq, Eq)]
struct MissingItem {
    kind: MissingKind,
    /// Natural key: the secret's manifest `path`, or the bundle's
    /// `bundle_id`.
    key: String,
    /// Where the input was expected: `env:<VAR>` or `path:<resolved path>`.
    source: String,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum MissingKind {
    SecretValue,
    BundleArtifact,
}

impl MissingKind {
    fn as_str(self) -> &'static str {
        match self {
            MissingKind::SecretValue => "secret_value",
            MissingKind::BundleArtifact => "bundle_artifact",
        }
    }
}

impl MissingItem {
    fn to_json(&self) -> Value {
        json!({
            "kind": self.kind.as_str(),
            "key": self.key,
            "source": self.source,
        })
    }
}

/// How `apply` terminates once the plan is computed.
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub enum ApplyMode {
    /// Execute the plan and verify (the default).
    #[default]
    Apply,
    /// Print the plan and exit 0 without mutating — a preview, even when
    /// changes are pending.
    DryRun,
    /// CI convergence gate: print the plan, never mutate, and fail
    /// (non-zero exit) when diffable changes are pending. Always-put
    /// secret rows don't count as drift ([`ApplyAction::counts_as_drift`]).
    Check,
}

impl ApplyMode {
    fn as_str(self) -> &'static str {
        match self {
            ApplyMode::Apply => "apply",
            ApplyMode::DryRun => "dry-run",
            ApplyMode::Check => "check",
        }
    }
}

/// Knobs for [`apply`] beyond the global `OpFlags`. Built from
/// `EnvApplyArgs` on the CLI path; library callers (greentic-setup's env
/// mode) construct it directly — `Default` is a plain mutating apply.
///
/// The `--emit-answers-template` shortcut is dispatched as a peer verb
/// mode ([`emit_answers_template`]) before `ApplyOptions` is constructed,
/// so it does not appear here.
#[derive(Debug, Clone, Default)]
pub struct ApplyOptions {
    pub mode: ApplyMode,
    /// Audit principal forwarded to every composed mutation. Defaults to
    /// `env-apply`.
    pub updated_by: Option<String>,
    /// Skip the interactive plan confirmation.
    pub yes: bool,
    /// Never prompt — neither for missing secret values nor the plan
    /// confirmation (implies `yes`). Missing inputs are collected and
    /// reported instead of asked for.
    pub non_interactive: bool,
    /// Pre-collected values for paste-sourced secrets (manifest `from_env`
    /// absent), keyed by the manifest secret `path`. An interactive author
    /// (the `gtc setup --env` wizard) collects each value once and hands it
    /// here so apply does not prompt again; env-sourced secrets ignore this.
    /// Never serialized — `SecretValue`'s `Debug` renders a placeholder.
    pub prefilled_secrets: BTreeMap<String, SecretValue>,
}

/// Write the skeleton `greentic.env-manifest.v1` template to `path`.
///
/// This is the peer verb mode for `--emit-answers-template`: it needs no
/// store, no manifest, and no flags — dispatched before `apply` is
/// entered so the template write is decoupled from the apply engine's
/// precondition stack.
pub fn emit_answers_template(path: &Path) -> Result<OpOutcome, OpError> {
    std::fs::write(path, super::env_manifest::MANIFEST_TEMPLATE_JSON).map_err(|source| {
        OpError::Io {
            path: path.to_path_buf(),
            source,
        }
    })?;
    Ok(OpOutcome::new(
        NOUN,
        VERB,
        json!({
            "manifest_schema": ENV_MANIFEST_SCHEMA_V1,
            "mode": "emit-answers-template",
            "path": path,
        }),
    ))
}

/// `gtc op env apply --answers <manifest.json> [--dry-run | --check |
/// --non-interactive] [--updated-by <who>] [--yes]`.
pub fn apply(
    store: &LocalFsStore,
    flags: &OpFlags,
    opts: ApplyOptions,
) -> Result<OpOutcome, OpError> {
    // TTY fill-in is offered only when the run could actually mutate
    // (prompting during a preview is noise) and the operator can answer.
    // rpassword prompts and reads on /dev/tty directly, so a redirected
    // stdout never sees the prompt and the JSON envelope stays clean.
    let interactive = opts.mode == ApplyMode::Apply
        && !opts.non_interactive
        && std::io::stdin().is_terminal()
        && std::io::stderr().is_terminal();
    let prompter: Option<&SecretPrompter> = if interactive {
        Some(&prompt_secret_value)
    } else {
        None
    };
    apply_with_lookups(
        store,
        flags,
        opts,
        &|name| std::env::var(name).ok(),
        prompter,
    )
}

/// Asked for a secret value: `(manifest path, from_env name) -> value`.
/// For an env-sourced secret this is the fallback when `$from_env` is unset;
/// for a paste-sourced secret (`from_env` absent) the caller passes an empty
/// `from_env`, and this is the primary collection path. `None` means "still
/// missing" — the path lands in the missing-inputs report.
type SecretPrompter = dyn Fn(&str, &str) -> Option<String>;

/// Masked TTY prompt for one secret value. Empty input declines — the path
/// stays missing and apply aborts with the full report. The value lives only
/// in the in-memory [`SecretValue`] map, exactly like an env-resolved one; it
/// never reaches the manifest, plan, report, or audit. An empty `from_env`
/// marks a paste-sourced secret (no env var to name), changing the wording
/// from "unset variable" to a plain value prompt.
fn prompt_secret_value(path: &str, from_env: &str) -> Option<String> {
    let prompt = if from_env.is_empty() {
        format!("secret `{path}`: enter value (hidden; empty to abort): ")
    } else {
        format!("secret `{path}`: ${from_env} is unset — enter value (hidden; empty to abort): ")
    };
    let value = rpassword::prompt_password(prompt).ok()?;
    (!value.is_empty()).then_some(value)
}

/// [`apply`] with the `from_env` secret-value lookup and the TTY prompter
/// injected. Tests pass fakes so they never mutate the process environment
/// (`set_var` is unsafe under a multithreaded test harness) and never need
/// a TTY.
fn apply_with_lookups(
    store: &LocalFsStore,
    flags: &OpFlags,
    opts: ApplyOptions,
    env_lookup: &dyn Fn(&str) -> Option<String>,
    prompter: Option<&SecretPrompter>,
) -> Result<OpOutcome, OpError> {
    if flags.schema_only {
        return Ok(OpOutcome::new(NOUN, VERB, manifest_schema()));
    }
    let ApplyOptions {
        mode,
        updated_by,
        yes,
        non_interactive,
        prefilled_secrets,
    } = opts;
    let yes = yes || non_interactive;
    let manifest_path = flags.answers.clone().ok_or_else(|| {
        OpError::InvalidArgument(
            "env apply requires `--answers <manifest.json>` (a greentic.env-manifest.v1 \
             document; see `gtc op env apply --schema`)"
                .to_string(),
        )
    })?;
    let manifest: EnvManifest = super::load_answers(&manifest_path)?;
    manifest.validate_shape()?;
    let manifest_dir = manifest_path
        .parent()
        .filter(|p| !p.as_os_str().is_empty())
        .unwrap_or_else(|| Path::new("."))
        .to_path_buf();
    let updated_by = updated_by.unwrap_or_else(|| DEFAULT_UPDATED_BY.to_string());

    let ctx = resolve_and_validate(
        store,
        manifest,
        &manifest_dir,
        updated_by,
        env_lookup,
        prompter,
        &prefilled_secrets,
    )?;
    let steps = diff(store, &ctx)?;
    render_plan(&steps, &ctx.warnings, &ctx.missing);

    match mode {
        ApplyMode::DryRun => {
            return Ok(OpOutcome::new(
                NOUN,
                VERB,
                report_json(&ctx, &steps, mode.as_str(), None),
            ));
        }
        ApplyMode::Check => {
            // Missing inputs are reported but deliberately NOT drift: a CI
            // convergence gate must be runnable without holding the secret
            // values themselves (they're excluded from the verdict as
            // undiffable anyway). Mutating apply still requires them below.
            let diffable_pending = steps.iter().filter(|s| s.action.counts_as_drift()).count();
            if diffable_pending > 0 {
                return Err(OpError::Conflict(format!(
                    "env `{}` is not converged: {diffable_pending} pending change(s) — \
                     run `gtc op env apply --answers <manifest>` to reconcile (see the \
                     plan above for the step list)",
                    ctx.env_id.as_str()
                )));
            }
            return Ok(OpOutcome::new(
                NOUN,
                VERB,
                report_json(&ctx, &steps, mode.as_str(), None),
            ));
        }
        ApplyMode::Apply => {}
    }

    // Mutating apply refuses to run while any input is missing — the plan
    // above already lists every gap (the whole point of accumulating them),
    // so the operator fixes all of them in one round trip.
    if !ctx.missing.is_empty() {
        return Err(OpError::InvalidArgument(format!(
            "cannot apply: {} missing input(s): {} — export the named variable(s) / \
             provide the artifact(s) and re-run (on a TTY, missing secret values are \
             prompted for)",
            ctx.missing.len(),
            ctx.missing
                .iter()
                .map(|m| format!("{} `{}` ({})", m.kind.as_str(), m.key, m.source))
                .collect::<Vec<_>>()
                .join(", "),
        )));
    }

    let pending = steps
        .iter()
        .filter(|s| s.action != ApplyAction::NoOp)
        .count();
    if pending > 0 && !yes && std::io::stdin().is_terminal() && std::io::stdout().is_terminal() {
        eprint!(
            "apply {pending} change(s) to env `{}`? [y/N] ",
            ctx.env_id.as_str()
        );
        let mut line = String::new();
        std::io::stdin()
            .read_line(&mut line)
            .map_err(|source| OpError::Io {
                path: PathBuf::from("<stdin>"),
                source,
            })?;
        let answer = line.trim().to_ascii_lowercase();
        if answer != "y" && answer != "yes" {
            return Err(OpError::InvalidArgument(
                "aborted by user (pass --yes to skip the confirmation)".to_string(),
            ));
        }
    }

    execute(store, &ctx, &steps)?;
    let verify = verify(store, &ctx)?;
    Ok(OpOutcome::new(
        NOUN,
        VERB,
        report_json(&ctx, &steps, "apply", Some(verify)),
    ))
}

// --- validate -------------------------------------------------------------------

fn resolve_and_validate(
    store: &LocalFsStore,
    manifest: EnvManifest,
    manifest_dir: &Path,
    updated_by: String,
    env_lookup: &dyn Fn(&str) -> Option<String>,
    prompter: Option<&SecretPrompter>,
    prefilled_secrets: &BTreeMap<String, SecretValue>,
) -> Result<ApplyContext, OpError> {
    let env_id = EnvId::try_from(manifest.environment.id.as_str())
        .map_err(|e| OpError::InvalidArgument(format!("environment.id: {e}")))?;
    let canonical_public_base_url =
        super::env::parse_optional_public_base_url(&manifest.environment.public_base_url)?;

    let env = if store.exists(&env_id)? {
        Some(store.load(&env_id)?)
    } else {
        // `env apply` reconciles an EXISTING non-local env, but it cannot
        // CREATE one: the local store is authorization-gated to `local` only
        // (A7 `authorize_local_only`), so non-local env creation is reserved
        // for the remote operator store. Surface a clear error at plan time
        // rather than letting execute hit a confusing `Unauthorized`.
        if env_id.as_str() != crate::defaults::LOCAL_ENV_ID {
            return Err(OpError::NotFound(format!(
                "environment `{env_id}` not found — `env apply` can reconcile an existing \
                 non-local environment but cannot create one locally (the `local`-only \
                 authorization policy reserves non-local env creation for the remote \
                 operator store). Create `{env_id}` via the operator store first, or use \
                 `local`."
            )));
        }
        None
    };

    // Secrets: resolve every `from_env` (set + non-empty) and pre-check the
    // bound secrets backend, all before any mutation. Values land ONLY in
    // `secret_values` (never in steps, plan, report, or audit targets).
    // Path canonicality was already checked by `validate_shape`.
    // Backend pre-check applies only when the env already exists — a fresh
    // env is exempt because the `env init` step creates the default
    // dev-store binding before any put-secret step runs.
    if !manifest.secrets.is_empty()
        && let Some(env) = &env
    {
        let secrets_pack = super::secrets::require_secrets_pack(env, &env_id)?;
        if secrets_pack.kind.path() != super::secrets::DEV_STORE_KIND_PATH {
            return Err(OpError::NotYetImplemented(format!(
                "manifest secrets[] write through the dev-store only; env `{env_id}` \
                 binds `{}` — backend dispatch beyond the dev-store lands in A9 \
                 (env-pack registry)",
                secrets_pack.kind
            )));
        }
    }
    // An unresolvable value is a MISSING INPUT, not a structural error:
    // collect every gap (offering the TTY prompter as a fallback) instead
    // of failing on the first, so one run reports the complete list.
    //
    // Per-secret resolution by source:
    // - env-sourced (`from_env: Some`): read `$from_env`, falling back to an
    //   interactive masked prompt when it is unset (today's behavior).
    // - paste-sourced (`from_env: None`): use an author-supplied value
    //   (`prefilled_secrets`, the wizard hand-off) if present; else, if the
    //   value is already in the env's secrets store, it is satisfied with no
    //   re-prompt and no put (the store is the source of truth for pasted
    //   values); else prompt interactively; else report it missing.
    let env_dir = store.env_dir(&env_id)?;
    let mut missing = Vec::new();
    let mut secret_values = BTreeMap::new();
    let mut prompted_paths = BTreeSet::new();
    let mut store_satisfied_paths = BTreeSet::new();
    for s in &manifest.secrets {
        let value = match &s.from_env {
            Some(from_env) => match env_lookup(from_env).filter(|v| !v.is_empty()) {
                Some(v) => Some(v),
                None => prompter.and_then(|p| {
                    let v = p(&s.path, from_env).filter(|v| !v.is_empty());
                    if v.is_some() {
                        prompted_paths.insert(s.path.clone());
                    }
                    v
                }),
            },
            None => {
                // An author-supplied value wins. An EMPTY prefilled value is
                // treated as no value — consistent with the env/prompt branches
                // (which filter empties) — so it falls through to the store
                // check / prompt / missing path instead of reaching
                // `secrets::put`, which rejects empties only at execute, after
                // earlier steps have already mutated state.
                let prefilled = prefilled_secrets
                    .get(&s.path)
                    .map(|value| value.expose())
                    .filter(|value| !value.is_empty());
                if let Some(value) = prefilled {
                    Some(value.to_string())
                } else if super::secrets::dev_store_has(&env_dir, &env_id, &s.path)? {
                    // Already stored — nothing to collect or put. Skip
                    // entirely: not a value, not a missing input.
                    store_satisfied_paths.insert(s.path.clone());
                    continue;
                } else {
                    // Empty `from_env` selects the paste-prompt wording.
                    prompter.and_then(|p| p(&s.path, "").filter(|v| !v.is_empty()))
                }
            }
        };
        match value {
            Some(v) => {
                secret_values.insert(s.path.clone(), SecretValue::from(v));
            }
            None => missing.push(MissingItem {
                kind: MissingKind::SecretValue,
                key: s.path.clone(),
                source: match &s.from_env {
                    Some(from_env) => format!("env:{from_env}"),
                    None => "paste".to_string(),
                },
            }),
        }
    }

    // Bundle artifacts: existence + digest, plus the B10 billing-principal
    // rule, all before any mutation. The principal rule stays fail-fast
    // (a manifest bug); an absent artifact is a missing input — the bundle
    // is reported and skipped (no digest means nothing to diff against).
    let mut resolved_bundles = Vec::with_capacity(manifest.bundles.len());
    for b in &manifest.bundles {
        let customer_id = super::bundles::resolve_customer_id(&env_id, b.customer_id.clone())?;

        // Resolve each artifact path (single-revision or multi-revision).
        let artifact_specs: Vec<(&std::path::Path, Option<&ManifestRevision>)> =
            if let Some(bp) = &b.bundle_path {
                vec![(bp.as_path(), None)]
            } else if let Some(revs) = &b.revisions {
                revs.iter()
                    .map(|r| (r.bundle_path.as_path(), Some(r)))
                    .collect()
            } else {
                // validate_shape already rejects this, but be safe.
                continue;
            };

        let mut any_missing = false;
        let mut resolved_revs = Vec::with_capacity(artifact_specs.len());

        // For multi-revision, compute effective weights (validated by
        // validate_shape). Single-revision always gets FULL_TRAFFIC_BPS.
        let weights: Vec<u32> = if let Some(revs) = &b.revisions {
            compute_effective_weights_bps(revs)
        } else {
            vec![super::deploy::FULL_TRAFFIC_BPS]
        };

        for (i, (artifact_path, rev_spec)) in artifact_specs.iter().enumerate() {
            let resolved_path = if artifact_path.is_absolute() {
                artifact_path.to_path_buf()
            } else {
                manifest_dir.join(artifact_path)
            };
            if !resolved_path.is_file() {
                let key = match rev_spec {
                    Some(r) => format!("{}:{}", b.bundle_id, r.name),
                    None => b.bundle_id.clone(),
                };
                missing.push(MissingItem {
                    kind: MissingKind::BundleArtifact,
                    key,
                    source: format!("path:{}", resolved_path.display()),
                });
                any_missing = true;
                continue;
            }
            let digest =
                super::bundle_stage::sha256_file(&resolved_path).map_err(|source| OpError::Io {
                    path: resolved_path.clone(),
                    source,
                })?;
            let spec = rev_spec.cloned().unwrap_or_else(|| {
                // Synthesize a ManifestRevision for single-revision entries.
                ManifestRevision {
                    name: "default".to_string(),
                    bundle_path: b
                        .bundle_path
                        .clone()
                        .expect("single-revision has bundle_path"),
                    weight_percent: None,
                    weight_bps: Some(super::deploy::FULL_TRAFFIC_BPS),
                    drain_seconds: None,
                    abort_metrics: Vec::new(),
                }
            });
            resolved_revs.push(ResolvedRevision {
                spec,
                resolved_path,
                digest,
                weight_bps: weights[i],
            });
        }

        if any_missing {
            continue;
        }

        resolved_bundles.push(ResolvedBundle {
            spec: b.clone(),
            customer_id,
            revisions: resolved_revs,
        });
    }

    // Deployment natural-key sanity: `op deploy` keys on
    // `(bundle_id, customer_id)`. Refuse to adopt a deployment owned by a
    // different billing principal, and reject same-pair ambiguity.
    if let Some(env) = &env {
        for rb in &resolved_bundles {
            let same_customer: Vec<_> = env
                .bundles
                .iter()
                .filter(|d| {
                    d.bundle_id.as_str() == rb.spec.bundle_id && d.customer_id == rb.customer_id
                })
                .collect();
            let other_customer: Vec<_> = env
                .bundles
                .iter()
                .filter(|d| {
                    d.bundle_id.as_str() == rb.spec.bundle_id && d.customer_id != rb.customer_id
                })
                .collect();
            if !other_customer.is_empty() {
                return Err(OpError::Conflict(format!(
                    "bundle `{}`: deployment owned by customer `{}` exists but manifest \
                     resolves to `{}` — apply refuses to adopt a deployment owned by a \
                     different customer",
                    rb.spec.bundle_id,
                    other_customer[0].customer_id.as_str(),
                    rb.customer_id.as_str(),
                )));
            }
            if same_customer.len() > 1 {
                return Err(OpError::Conflict(format!(
                    "bundle `{}` matches {} deployments for customer `{}` in env `{env_id}` \
                     — apply refuses to guess; reconcile with `gtc op bundles list {env_id}` \
                     first",
                    rb.spec.bundle_id,
                    same_customer.len(),
                    rb.customer_id.as_str(),
                )));
            }
        }
    }

    let mut warnings = Vec::new();

    // Endpoint link targets + match ambiguity + welcome-flow reachability.
    let mut resolved_endpoints = Vec::with_capacity(manifest.messaging_endpoints.len());
    for ep in &manifest.messaging_endpoints {
        for link in &ep.links {
            let in_manifest = manifest.bundles.iter().any(|b| &b.bundle_id == link);
            if in_manifest {
                continue;
            }
            let in_env = env
                .as_ref()
                .is_some_and(|e| e.bundles.iter().any(|d| d.bundle_id.as_str() == link));
            if in_env {
                warnings.push(format!(
                    "endpoint `{}`: link `{link}` is satisfied only by a pre-existing env \
                     deployment (not declared in this manifest)",
                    ep.name
                ));
            } else {
                return Err(OpError::InvalidArgument(format!(
                    "endpoint `{}`: link `{link}` is neither declared in this manifest's \
                     bundles[] nor deployed in env `{env_id}`",
                    ep.name
                )));
            }
        }

        let matched = match &env {
            Some(e) => match_existing_endpoint(e, ep)?,
            None => None,
        };
        if let Some(wf) = &ep.welcome_flow {
            let linked_via_manifest = ep.links.contains(&wf.bundle_id);
            let linked_on_existing = matched
                .is_some_and(|m| m.linked_bundles.iter().any(|b| b.as_str() == wf.bundle_id));
            if !linked_via_manifest && !linked_on_existing {
                return Err(OpError::InvalidArgument(format!(
                    "endpoint `{}`: welcome_flow.bundle_id `{}` must be in this endpoint's \
                     links[] (or already linked on the matched endpoint)",
                    ep.name, wf.bundle_id
                )));
            }
        }
        if let Some(m) = matched {
            let existing_refs: Vec<String> = m
                .secret_refs
                .iter()
                .map(|r| r.as_str().to_string())
                .collect();
            if !ep.secret_refs.is_empty() && ep.secret_refs != existing_refs {
                warnings.push(format!(
                    "endpoint `{}`: secret_refs differ from the existing endpoint's — \
                     left untouched (no endpoint update verb exists yet)",
                    ep.name
                ));
            }
        }
        resolved_endpoints.push(ResolvedEndpoint {
            spec: ep.clone(),
            matched: matched.cloned(),
        });
    }

    // Validate that every declared `answers_ref` resolves to an existing file
    // relative to the manifest directory. The schema contract says "relative to
    // the manifest", so catch missing files at plan time rather than letting a
    // later `op env render` fail with a confusing path error.
    // NOTE: the stored path is the raw manifest-relative value. The consumer
    // (`load_render_answers`) resolves relative to the *env* directory, which
    // may differ. A future iteration should copy the file into the env dir or
    // store an env-relative path; for now we at least fail fast on a typo.
    for p in &manifest.packs {
        if let Some(ar) = &p.answers_ref {
            let resolved = if ar.is_absolute() {
                ar.clone()
            } else {
                manifest_dir.join(ar)
            };
            if !resolved.is_file() {
                return Err(OpError::InvalidArgument(format!(
                    "packs[] slot `{}`: answers_ref `{}` does not exist (resolved to `{}`)",
                    p.slot,
                    ar.display(),
                    resolved.display()
                )));
            }
        }
    }
    for ext in &manifest.extensions {
        if let Some(ar) = &ext.answers_ref {
            let resolved = if ar.is_absolute() {
                ar.clone()
            } else {
                manifest_dir.join(ar)
            };
            if !resolved.is_file() {
                return Err(OpError::InvalidArgument(format!(
                    "extensions[] kind `{}`: answers_ref `{}` does not exist (resolved to `{}`)",
                    ext.kind,
                    ar.display(),
                    resolved.display()
                )));
            }
        }
    }

    Ok(ApplyContext {
        env_id,
        manifest,
        secret_values,
        prompted_paths,
        store_satisfied_paths,
        bundles: resolved_bundles,
        endpoints: resolved_endpoints,
        env,
        canonical_public_base_url,
        missing,
        warnings,
        updated_by,
    })
}

/// Match an existing endpoint by the `(provider_type, display_name)` natural
/// key. More than one pair match = ambiguity error; a display-name match
/// with a different `provider_type` = repurposed-name error (apply refuses
/// to guess).
fn match_existing_endpoint<'e>(
    env: &'e Environment,
    ep: &ManifestEndpoint,
) -> Result<Option<&'e MessagingEndpoint>, OpError> {
    let name_matches: Vec<&MessagingEndpoint> = env
        .messaging_endpoints
        .iter()
        .filter(|m| m.display_name == ep.name)
        .collect();
    let pair_matches: Vec<&&MessagingEndpoint> = name_matches
        .iter()
        .filter(|m| m.provider_type == ep.provider_type)
        .collect();
    match pair_matches.len() {
        0 if name_matches.is_empty() => Ok(None),
        0 => Err(OpError::Conflict(format!(
            "endpoint `{}`: an endpoint with this display_name exists but with \
             provider_type `{}` (manifest says `{}`) — apply refuses to repurpose a name; \
             rename or remove the existing endpoint first",
            ep.name, name_matches[0].provider_type, ep.provider_type
        ))),
        1 => Ok(Some(pair_matches[0])),
        n => Err(OpError::Conflict(format!(
            "endpoint `{}`: {n} existing endpoints match (provider_type=`{}`, \
             display_name=`{}`): [{}] — apply refuses to guess; remove the duplicates first",
            ep.name,
            ep.provider_type,
            ep.name,
            pair_matches
                .iter()
                .map(|m| m.endpoint_id.to_string())
                .collect::<Vec<_>>()
                .join(", ")
        ))),
    }
}

// --- diff -----------------------------------------------------------------------

fn diff(store: &LocalFsStore, ctx: &ApplyContext) -> Result<Vec<ApplyStep>, OpError> {
    let mut steps = Vec::new();
    let env_id_str = ctx.env_id.as_str().to_string();

    // 1. EnsureEnvironment. `ctx.env == None` only ever reaches here for the
    //    `local` env — `resolve_and_validate` rejects a non-existent non-local
    //    env before diffing (non-local creation is reserved for the remote
    //    operator store, A7).
    match &ctx.env {
        None => {
            steps.push(ApplyStep {
                kind: ApplyStepKind::EnsureEnvironment,
                key: env_id_str.clone(),
                action: ApplyAction::Create,
                detail: "env init (local bootstrap: default env-pack bindings + trust-root seed)"
                    .to_string(),
                idempotency_key: None,
                op: StepOp::EnvInit {
                    public_base_url: ctx.canonical_public_base_url.clone(),
                },
            });
        }
        Some(env) => match &ctx.canonical_public_base_url {
            Some(url) if env.host_config.public_base_url.as_deref() != Some(url.as_str()) => {
                steps.push(ApplyStep {
                    kind: ApplyStepKind::EnsureEnvironment,
                    key: env_id_str.clone(),
                    action: ApplyAction::Update,
                    detail: format!("set-public-url {url}"),
                    idempotency_key: None,
                    op: StepOp::SetPublicUrl { url: url.clone() },
                });
            }
            _ => steps.push(ApplyStep::no_op(
                ApplyStepKind::EnsureEnvironment,
                env_id_str.clone(),
                "exists (public_base_url unchanged)",
            )),
        },
    }

    // 2. UpdateHostConfig (name, region, tenant_org_id, listen_addr, gui_enabled).
    //    Compares declared manifest fields against the live env; any declared
    //    field that differs emits ONE UpdateHostConfig step. On a fresh env
    //    (env == None) the host-config update is deferred to after the
    //    EnsureEnvironment execute creates the env.
    if let Some(env) = &ctx.env {
        let me = &ctx.manifest.environment;
        let name_differs = me.name.as_ref().is_some_and(|n| *n != env.name);
        let region_differs = me
            .region
            .as_ref()
            .is_some_and(|r| env.host_config.region.as_deref() != Some(r.as_str()));
        let tenant_org_differs = me
            .tenant_org_id
            .as_ref()
            .is_some_and(|t| env.host_config.tenant_org_id.as_deref() != Some(t.as_str()));
        let listen_addr_differs = me.listen_addr.as_ref().is_some_and(|la| {
            let parsed: std::net::SocketAddr = la
                .parse()
                .expect("validate_shape already validated listen_addr");
            env.host_config.listen_addr != Some(parsed)
        });
        // Compares against the RAW stored value (like every other host-config
        // field) — declaring a value always persists it; the env-id default
        // (on for local) is resolved by the runtime only when unset.
        let gui_enabled_differs = me
            .gui_enabled
            .is_some_and(|g| env.host_config.gui_enabled != Some(g));

        if name_differs
            || region_differs
            || tenant_org_differs
            || listen_addr_differs
            || gui_enabled_differs
        {
            let mut fields = Vec::new();
            if name_differs {
                fields.push("name");
            }
            if region_differs {
                fields.push("region");
            }
            if tenant_org_differs {
                fields.push("tenant_org_id");
            }
            if listen_addr_differs {
                fields.push("listen_addr");
            }
            if gui_enabled_differs {
                fields.push("gui_enabled");
            }
            let desired_hash = hash_json(&json!({
                "name": me.name,
                "region": me.region,
                "tenant_org_id": me.tenant_org_id,
                "listen_addr": me.listen_addr,
                "gui_enabled": me.gui_enabled,
            }));
            let ikey = derive_idempotency_key(
                &ctx.env_id,
                ApplyStepKind::UpdateHostConfig.label(),
                &env_id_str,
                &desired_hash,
            );
            steps.push(ApplyStep {
                kind: ApplyStepKind::UpdateHostConfig,
                key: env_id_str.clone(),
                action: ApplyAction::Update,
                detail: format!("set {}", fields.join(", ")),
                idempotency_key: Some(ikey),
                op: StepOp::UpdateHostConfig(Box::new(ConfigSetPayload {
                    environment_id: env_id_str.clone(),
                    name: me.name.clone(),
                    region: me.region.clone(),
                    tenant_org_id: me.tenant_org_id.clone(),
                    listen_addr: me.listen_addr.clone(),
                    public_base_url: None, // public_base_url is handled by SetPublicUrl
                    gui_enabled: me.gui_enabled,
                })),
            });
        } else if me.declares_host_config() {
            steps.push(ApplyStep::no_op(
                ApplyStepKind::UpdateHostConfig,
                env_id_str.clone(),
                "host-config unchanged",
            ));
        }
    } else {
        // Fresh env (always `local` here — see EnsureEnvironment). `env init`
        // does NOT thread name/region/tenant_org_id/listen_addr, so a declared
        // host-config is applied by a deferred UpdateHostConfig step that runs
        // after the env is created.
        let me = &ctx.manifest.environment;
        if me.declares_host_config() {
            steps.push(ApplyStep {
                kind: ApplyStepKind::UpdateHostConfig,
                key: env_id_str.clone(),
                action: ApplyAction::Create,
                detail: "set host-config on fresh env".to_string(),
                idempotency_key: None,
                op: StepOp::UpdateHostConfig(Box::new(ConfigSetPayload {
                    environment_id: env_id_str.clone(),
                    name: me.name.clone(),
                    region: me.region.clone(),
                    tenant_org_id: me.tenant_org_id.clone(),
                    listen_addr: me.listen_addr.clone(),
                    public_base_url: None,
                    gui_enabled: me.gui_enabled,
                })),
            });
        }
    }

    // 3. BootstrapTrustRoot.
    if ctx.manifest.trust_root == Some(TrustRootDirective::Bootstrap) {
        steps.push(trust_root_step(store, ctx)?);
    }

    // 4. Env-packs — each manifest pack binds one core capability slot.
    //    Look up the existing binding by slot; absent → add, differs → update.
    //
    //    On a fresh env (`ctx.env == None`, always `local` here) the preceding
    //    EnsureEnvironment step will create the env with five default bindings
    //    (deployer, secrets, telemetry, sessions, state). We must diff against
    //    those defaults so packs targeting a default slot emit UpdatePackBinding
    //    (or no-op) instead of an unconditional AddPackBinding that would fail
    //    with `SlotAlreadyBound` after `env init` runs.
    let default_bindings = if ctx.env.is_none() {
        Some(
            crate::defaults::local_pack_bindings()
                .map_err(|e| OpError::InvalidArgument(format!("default pack bindings: {e}")))?,
        )
    } else {
        None
    };
    for mp in &ctx.manifest.packs {
        let existing = match &ctx.env {
            Some(e) => e.pack_for_slot(mp.slot),
            None => default_bindings
                .as_ref()
                .and_then(|bs| bs.iter().find(|b| b.slot == mp.slot)),
        };
        match existing {
            None => {
                let desired_hash = hash_json(&json!({
                    "slot": mp.slot.to_string(),
                    "kind": mp.kind,
                    "pack_ref": mp.pack_ref,
                    "answers_ref": mp.answers_ref,
                }));
                let ikey = derive_idempotency_key(
                    &ctx.env_id,
                    ApplyStepKind::AddPackBinding.label(),
                    &mp.slot.to_string(),
                    &desired_hash,
                );
                steps.push(ApplyStep {
                    kind: ApplyStepKind::AddPackBinding,
                    key: mp.slot.to_string(),
                    action: ApplyAction::Create,
                    detail: format!("{} ({})", mp.kind, mp.pack_ref),
                    idempotency_key: Some(ikey.clone()),
                    op: StepOp::AddPackBinding(Box::new(EnvPackBindingPayload {
                        environment_id: env_id_str.clone(),
                        slot: mp.slot,
                        kind: mp.kind.clone(),
                        pack_ref: mp.pack_ref.clone(),
                        answers_ref: mp.answers_ref.clone(),
                        idempotency_key: Some(ikey),
                    })),
                });
            }
            Some(b) => {
                let kind_differs = b.kind.to_string() != mp.kind;
                let pack_ref_differs = b.pack_ref.as_str() != mp.pack_ref;
                let answers_ref_differs = mp
                    .answers_ref
                    .as_ref()
                    .is_some_and(|ar| b.answers_ref.as_ref() != Some(ar));
                if kind_differs || pack_ref_differs || answers_ref_differs {
                    let desired_hash = hash_json(&json!({
                        "slot": mp.slot.to_string(),
                        "kind": mp.kind,
                        "pack_ref": mp.pack_ref,
                        "answers_ref": mp.answers_ref,
                    }));
                    let ikey = derive_idempotency_key(
                        &ctx.env_id,
                        ApplyStepKind::UpdatePackBinding.label(),
                        &mp.slot.to_string(),
                        &desired_hash,
                    );
                    let mut what = Vec::new();
                    if kind_differs {
                        what.push(format!("kind → {}", mp.kind));
                    }
                    if pack_ref_differs {
                        what.push(format!("pack_ref → {}", mp.pack_ref));
                    }
                    if answers_ref_differs {
                        what.push("answers_ref".to_string());
                    }
                    steps.push(ApplyStep {
                        kind: ApplyStepKind::UpdatePackBinding,
                        key: mp.slot.to_string(),
                        action: ApplyAction::Update,
                        detail: what.join(", "),
                        idempotency_key: Some(ikey.clone()),
                        op: StepOp::UpdatePackBinding(Box::new(EnvPackBindingPayload {
                            environment_id: env_id_str.clone(),
                            slot: mp.slot,
                            kind: mp.kind.clone(),
                            pack_ref: mp.pack_ref.clone(),
                            answers_ref: mp.answers_ref.clone(),
                            idempotency_key: Some(ikey),
                        })),
                    });
                } else {
                    steps.push(ApplyStep::no_op(
                        ApplyStepKind::AddPackBinding,
                        mp.slot.to_string(),
                        format!("bound ({}, {})", mp.kind, mp.pack_ref),
                    ));
                }
            }
        }
    }

    // 5. Secrets — always-put: `op secrets get` is not-yet-implemented for
    //    every backend, so values cannot be diffed. The plan says so
    //    explicitly rather than ever claiming a false no-op; when A9 lands a
    //    real `get`, this tightens to write-if-changed with no schema
    //    change. Secrets land before bundles so a just-deployed revision
    //    never serves a request that resolves a missing secret.
    for s in &ctx.manifest.secrets {
        // A paste-sourced secret already in the store needs no put step
        // (store-as-source-of-truth). A missing-input secret still emits an
        // undiffable put step for plan visibility — it never reaches execute
        // because the mutating path aborts on missing inputs first, exactly
        // as for an unset env var.
        if ctx.store_satisfied_paths.contains(&s.path) {
            continue;
        }
        // Deliberately NO deterministic idempotency key: an always-put is
        // by definition a NEW write (values cannot be diffed until A9), so
        // a value-insensitive key would stamp two semantically different
        // writes (e.g. a secret rotation under the same env var) with the
        // same key — conflating audit records and wrongly replaying under
        // any future same-key dedupe layer (A8 same-key-different-body
        // conflict rule). `secrets::put` mints a fresh per-invocation key
        // instead (second exception alongside deploy's per-revision
        // cut-over key).
        let detail = match &s.from_env {
            None => "pasted (cannot diff until A9)".to_string(),
            Some(_) if ctx.prompted_paths.contains(&s.path) => {
                "prompted (cannot diff until A9)".to_string()
            }
            Some(from_env) => format!("from ${from_env} (cannot diff until A9)"),
        };
        steps.push(ApplyStep {
            kind: ApplyStepKind::PutSecret,
            key: s.path.clone(),
            action: ApplyAction::Put,
            detail,
            idempotency_key: None,
            op: StepOp::PutSecret {
                path: s.path.clone(),
            },
        });
    }

    // 4. Bundles (keyed on `(bundle_id, customer_id)` — the same natural key
    //    that `op deploy` uses).
    for rb in &ctx.bundles {
        let is_multi = rb.spec.revisions.is_some();
        let existing = ctx.env.as_ref().and_then(|e| {
            e.bundles.iter().find(|d| {
                d.bundle_id.as_str() == rb.spec.bundle_id && d.customer_id == rb.customer_id
            })
        });

        // Helper: the primary digest for single-revision entries (first
        // and only element).
        let primary_digest = || rb.revisions[0].digest.clone();

        match existing {
            None if is_multi => {
                let digests: Vec<String> = rb
                    .revisions
                    .iter()
                    .map(|r| format!("{}@{}", r.spec.name, short_digest(&r.digest)))
                    .collect();
                let detail = format!(
                    "{} revision(s) [{}] → {}",
                    rb.revisions.len(),
                    digests.join(", "),
                    binding_summary(&rb.spec.route_binding.clone().map(into_route_binding))
                );
                steps.push(ApplyStep {
                    kind: ApplyStepKind::DeploySplit,
                    key: rb.spec.bundle_id.clone(),
                    action: ApplyAction::Create,
                    detail,
                    idempotency_key: None,
                    op: deploy_split_op(&env_id_str, rb),
                });
            }
            None => {
                let detail = format!(
                    "{}{}",
                    short_digest(&primary_digest()),
                    binding_summary(&rb.spec.route_binding.clone().map(into_route_binding))
                );
                steps.push(ApplyStep {
                    kind: ApplyStepKind::DeployBundle,
                    key: rb.spec.bundle_id.clone(),
                    action: ApplyAction::Create,
                    detail,
                    idempotency_key: None, // deploy derives its cut-over key per revision
                    op: StepOp::Deploy {
                        payload: Box::new(deploy_payload(
                            &env_id_str,
                            rb,
                            rb.spec.route_binding.clone(),
                        )),
                        expected_digest: primary_digest(),
                    },
                });
            }
            Some(dep) if is_multi => {
                let env = ctx.env.as_ref().expect("existing deployment implies env");
                let converged = split_converged(env, dep.deployment_id, &rb.revisions);
                let desired_binding: Option<RouteBinding> =
                    rb.spec.route_binding.clone().map(into_route_binding);
                let binding_differs = desired_binding
                    .as_ref()
                    .is_some_and(|b| *b != dep.route_binding);
                let overrides_differ = rb
                    .spec
                    .config_overrides
                    .as_ref()
                    .is_some_and(|o| *o != dep.config_overrides);

                if !converged {
                    let digests: Vec<String> = rb
                        .revisions
                        .iter()
                        .map(|r| format!("{}@{}", r.spec.name, short_digest(&r.digest)))
                        .collect();
                    let detail = format!(
                        "split not converged → re-deploy {} revision(s) [{}]",
                        rb.revisions.len(),
                        digests.join(", ")
                    );
                    steps.push(ApplyStep {
                        kind: ApplyStepKind::DeploySplit,
                        key: rb.spec.bundle_id.clone(),
                        action: ApplyAction::Update,
                        detail,
                        idempotency_key: None,
                        op: deploy_split_op(&env_id_str, rb),
                    });
                }

                // Metadata reconcile. `config_overrides` defers to a converged
                // split (it otherwise rides the next re-split run); revenue_
                // share/status are deployment-level and reconcile independently
                // of the split (a re-split never touches them).
                let diff = BundleMetaDiff {
                    route_binding: binding_differs.then(|| {
                        rb.spec
                            .route_binding
                            .clone()
                            .expect("binding_differs implies manifest binding")
                    }),
                    config_overrides: (overrides_differ && converged)
                        .then(|| rb.spec.config_overrides.clone().expect("overrides_differ")),
                    revenue_share: revenue_share_differs(rb, dep).then(|| {
                        rb.spec
                            .revenue_share
                            .clone()
                            .expect("differs implies manifest")
                    }),
                    status: status_differs(rb, dep).then(|| rb.spec.status.expect("differs")),
                };
                let did_update = !diff.is_noop();
                if let Some(step) = bundle_meta_update_step(
                    &ctx.env_id,
                    &env_id_str,
                    &rb.spec.bundle_id,
                    dep.deployment_id,
                    &desired_binding,
                    diff,
                ) {
                    steps.push(step);
                }

                if converged && !did_update {
                    steps.push(ApplyStep::no_op(
                        ApplyStepKind::DeploySplit,
                        rb.spec.bundle_id.clone(),
                        format!("split converged ({} revision(s))", rb.revisions.len()),
                    ));
                }
            }
            Some(dep) => {
                let desired_binding: Option<RouteBinding> =
                    rb.spec.route_binding.clone().map(into_route_binding);
                let binding_differs = desired_binding
                    .as_ref()
                    .is_some_and(|b| *b != dep.route_binding);
                let overrides_differ = rb
                    .spec
                    .config_overrides
                    .as_ref()
                    .is_some_and(|o| *o != dep.config_overrides);
                let env = ctx.env.as_ref().expect("existing deployment implies env");
                let converged = deployment_converged(env, dep.deployment_id, &primary_digest());
                let needs_deploy = !converged;
                let live = live_revision_digest(env, dep.deployment_id);

                // Deploy FIRST when re-staging is needed (routing-metadata-
                // last: a stage failure leaves the OLD revision serving under
                // the OLD binding, keeping published paths alive). The
                // binding/overrides update runs AFTER the deploy lands.
                if needs_deploy {
                    // Fix 1: two arms — real live digest vs degenerate/missing.
                    let detail = if live.is_some_and(digest_is_real) {
                        format!(
                            "digest {}{} (blue-green re-stage)",
                            live.map(short_digest).unwrap_or("none"),
                            short_digest(&primary_digest())
                        )
                    } else {
                        format!(
                            "traffic split is not a single 100% entry \
                             → re-deploy reconverges ({})",
                            short_digest(&primary_digest())
                        )
                    };
                    steps.push(ApplyStep {
                        kind: ApplyStepKind::DeployBundle,
                        key: rb.spec.bundle_id.clone(),
                        action: ApplyAction::Update,
                        detail,
                        idempotency_key: None,
                        op: StepOp::Deploy {
                            // Overrides ride the deploy (applied after
                            // stage+warm, before the cut-over). The
                            // binding is reconciled by the update step
                            // AFTER the deploy lands, so None here —
                            // deploy rejects a differing binding.
                            payload: Box::new(deploy_payload(&env_id_str, rb, None)),
                            expected_digest: primary_digest(),
                        },
                    });
                }

                // Metadata reconcile. `config_overrides` ride the deploy when
                // re-staging (so they defer here while `needs_deploy`); the
                // binding is applied AFTER the deploy lands; revenue_share/
                // status are deployment-level and reconcile independently of
                // the deploy (a re-deploy never resets them).
                let diff = BundleMetaDiff {
                    route_binding: binding_differs.then(|| {
                        rb.spec
                            .route_binding
                            .clone()
                            .expect("binding_differs implies manifest binding")
                    }),
                    config_overrides: (overrides_differ && !needs_deploy)
                        .then(|| rb.spec.config_overrides.clone().expect("overrides_differ")),
                    revenue_share: revenue_share_differs(rb, dep).then(|| {
                        rb.spec
                            .revenue_share
                            .clone()
                            .expect("differs implies manifest")
                    }),
                    status: status_differs(rb, dep).then(|| rb.spec.status.expect("differs")),
                };
                let did_update = !diff.is_noop();
                if let Some(step) = bundle_meta_update_step(
                    &ctx.env_id,
                    &env_id_str,
                    &rb.spec.bundle_id,
                    dep.deployment_id,
                    &desired_binding,
                    diff,
                ) {
                    steps.push(step);
                }

                if !needs_deploy && !did_update {
                    steps.push(ApplyStep::no_op(
                        ApplyStepKind::DeployBundle,
                        rb.spec.bundle_id.clone(),
                        format!("digest match ({})", short_digest(&primary_digest())),
                    ));
                }
            }
        }
    }

    // 8. Extensions — N-per-env open namespace, keyed by (kind.path(), instance_id).
    for mx in &ctx.manifest.extensions {
        let kind_path = greentic_deploy_spec::PackDescriptor::try_new(&mx.kind)
            .expect("validate_shape already validated kind")
            .path()
            .to_string();
        let ext_key = format!(
            "{}{}",
            kind_path,
            mx.instance_id
                .as_ref()
                .map(|i| format!("/{i}"))
                .unwrap_or_default()
        );
        let existing = ctx.env.as_ref().and_then(|e| {
            e.extensions
                .iter()
                .find(|b| b.kind.path() == kind_path && b.instance_id == mx.instance_id)
        });
        match existing {
            None => {
                let desired_hash = hash_json(&json!({
                    "kind": mx.kind,
                    "pack_ref": mx.pack_ref,
                    "instance_id": mx.instance_id,
                    "answers_ref": mx.answers_ref,
                }));
                let ikey = derive_idempotency_key(
                    &ctx.env_id,
                    ApplyStepKind::AddExtension.label(),
                    &ext_key,
                    &desired_hash,
                );
                steps.push(ApplyStep {
                    kind: ApplyStepKind::AddExtension,
                    key: ext_key,
                    action: ApplyAction::Create,
                    detail: format!("{} ({})", mx.kind, mx.pack_ref),
                    idempotency_key: Some(ikey.clone()),
                    op: StepOp::AddExtension(Box::new(ExtensionBindingPayload {
                        environment_id: env_id_str.clone(),
                        kind: mx.kind.clone(),
                        pack_ref: mx.pack_ref.clone(),
                        instance_id: mx.instance_id.clone(),
                        answers_ref: mx.answers_ref.clone(),
                        idempotency_key: Some(ikey),
                    })),
                });
            }
            Some(b) => {
                let kind_differs = b.kind.to_string() != mx.kind;
                let pack_ref_differs = b.pack_ref.as_str() != mx.pack_ref;
                let answers_ref_differs = mx
                    .answers_ref
                    .as_ref()
                    .is_some_and(|ar| b.answers_ref.as_ref() != Some(ar));
                if kind_differs || pack_ref_differs || answers_ref_differs {
                    let desired_hash = hash_json(&json!({
                        "kind": mx.kind,
                        "pack_ref": mx.pack_ref,
                        "instance_id": mx.instance_id,
                        "answers_ref": mx.answers_ref,
                    }));
                    let ikey = derive_idempotency_key(
                        &ctx.env_id,
                        ApplyStepKind::UpdateExtension.label(),
                        &ext_key,
                        &desired_hash,
                    );
                    let mut what = Vec::new();
                    if kind_differs {
                        what.push(format!("kind → {}", mx.kind));
                    }
                    if pack_ref_differs {
                        what.push(format!("pack_ref → {}", mx.pack_ref));
                    }
                    if answers_ref_differs {
                        what.push("answers_ref".to_string());
                    }
                    steps.push(ApplyStep {
                        kind: ApplyStepKind::UpdateExtension,
                        key: ext_key,
                        action: ApplyAction::Update,
                        detail: what.join(", "),
                        idempotency_key: Some(ikey.clone()),
                        op: StepOp::UpdateExtension(Box::new(ExtensionBindingPayload {
                            environment_id: env_id_str.clone(),
                            kind: mx.kind.clone(),
                            pack_ref: mx.pack_ref.clone(),
                            instance_id: mx.instance_id.clone(),
                            answers_ref: mx.answers_ref.clone(),
                            idempotency_key: Some(ikey),
                        })),
                    });
                } else {
                    steps.push(ApplyStep::no_op(
                        ApplyStepKind::AddExtension,
                        ext_key,
                        format!("bound ({}, {})", mx.kind, mx.pack_ref),
                    ));
                }
            }
        }
    }

    // 9. Endpoints (add → link → welcome-flow, per endpoint in manifest order).
    //    Reuses the match computed during validation (Fix 6); verify() re-matches
    //    the freshly reloaded env on purpose.
    for re in &ctx.endpoints {
        let ep = &re.spec;
        let matched = re.matched.as_ref();
        let endpoint_ref = match matched {
            Some(m) => EndpointRef::Existing(m.endpoint_id.to_string()),
            None => EndpointRef::CreatedByName(ep.name.clone()),
        };

        match matched {
            None => {
                let desired_hash = hash_json(&json!({
                    "provider_type": ep.provider_type,
                    "secret_refs": ep.secret_refs,
                }));
                let ikey = derive_idempotency_key(
                    &ctx.env_id,
                    ApplyStepKind::AddEndpoint.label(),
                    &ep.name,
                    &desired_hash,
                );
                steps.push(ApplyStep {
                    kind: ApplyStepKind::AddEndpoint,
                    key: ep.name.clone(),
                    action: ApplyAction::Create,
                    detail: format!("{} (provider_id = name)", ep.provider_type),
                    idempotency_key: Some(ikey.clone()),
                    op: StepOp::EndpointAdd(Box::new(EndpointAddPayload {
                        environment_id: env_id_str.clone(),
                        provider_id: ep.name.clone(),
                        provider_type: ep.provider_type.clone(),
                        display_name: ep.name.clone(),
                        secret_refs: ep.secret_refs.clone(),
                        idempotency_key: Some(ikey),
                        updated_by: ctx.updated_by.clone(),
                    })),
                });
            }
            Some(m) => steps.push(ApplyStep::no_op(
                ApplyStepKind::AddEndpoint,
                ep.name.clone(),
                format!("matched endpoint {}", m.endpoint_id),
            )),
        }

        for link in &ep.links {
            let already_linked =
                matched.is_some_and(|m| m.linked_bundles.iter().any(|b| b.as_str() == *link));
            let key = format!("{}{link}", ep.name);
            if already_linked {
                steps.push(ApplyStep::no_op(ApplyStepKind::LinkEndpoint, key, "linked"));
            } else {
                let ikey = derive_idempotency_key(
                    &ctx.env_id,
                    ApplyStepKind::LinkEndpoint.label(),
                    &format!("{}\u{0}{link}", ep.name),
                    "",
                );
                steps.push(ApplyStep {
                    kind: ApplyStepKind::LinkEndpoint,
                    key,
                    action: ApplyAction::Create,
                    detail: String::new(),
                    idempotency_key: Some(ikey.clone()),
                    op: StepOp::EndpointLink {
                        endpoint: endpoint_ref.clone(),
                        bundle_id: link.clone(),
                    },
                });
            }
        }

        if let Some(wf) = &ep.welcome_flow {
            let current_equal = matched.is_some_and(|m| {
                m.welcome_flow.as_ref().is_some_and(|cur| {
                    cur.bundle_id.as_str() == wf.bundle_id
                        && cur.pack_id.as_str() == wf.pack_id
                        && cur.flow_id == wf.flow_id
                })
            });
            if current_equal {
                steps.push(ApplyStep::no_op(
                    ApplyStepKind::SetWelcomeFlow,
                    ep.name.clone(),
                    format!("{}/{}/{}", wf.bundle_id, wf.pack_id, wf.flow_id),
                ));
            } else {
                let desired_hash = hash_json(&json!({
                    "bundle_id": wf.bundle_id, "pack_id": wf.pack_id, "flow_id": wf.flow_id,
                }));
                let ikey = derive_idempotency_key(
                    &ctx.env_id,
                    ApplyStepKind::SetWelcomeFlow.label(),
                    &ep.name,
                    &desired_hash,
                );
                let action = if matched.is_some_and(|m| m.welcome_flow.is_some()) {
                    ApplyAction::Update
                } else {
                    ApplyAction::Create
                };
                steps.push(ApplyStep {
                    kind: ApplyStepKind::SetWelcomeFlow,
                    key: ep.name.clone(),
                    action,
                    detail: format!("{}/{}/{}", wf.bundle_id, wf.pack_id, wf.flow_id),
                    idempotency_key: Some(ikey.clone()),
                    op: StepOp::WelcomeFlow {
                        endpoint: endpoint_ref.clone(),
                        flow: wf.clone(),
                    },
                });
            }
        }
    }

    Ok(steps)
}

/// Trust-root diff: read-only — `load_existing_only` never generates a key,
/// so `--dry-run` leaves the disk untouched. A missing operator key plans as
/// `create` (bootstrap generates it at execute time).
fn trust_root_step(store: &LocalFsStore, ctx: &ApplyContext) -> Result<ApplyStep, OpError> {
    let key = ctx.env_id.as_str().to_string();
    let Some(_env) = &ctx.env else {
        return Ok(ApplyStep {
            kind: ApplyStepKind::BootstrapTrustRoot,
            key,
            action: ApplyAction::Create,
            detail: "bootstrap (fresh env)".to_string(),
            idempotency_key: None,
            op: StepOp::TrustRootBootstrap,
        });
    };
    let env_dir = store.env_dir(&ctx.env_id)?;
    let trust_root = store_trust_root::load(&env_dir)?;
    let operator_key = crate::operator_key::load_existing_only();
    let (action, detail, op) = match operator_key {
        Ok(k)
            if trust_root
                .keys
                .iter()
                .any(|t| t.key_id.eq_ignore_ascii_case(&k.key_id)) =>
        {
            (
                ApplyAction::NoOp,
                format!("operator key {} already trusted", short_key_id(&k.key_id)),
                StepOp::None,
            )
        }
        Ok(k) => (
            ApplyAction::Create,
            format!("trust operator key {}", short_key_id(&k.key_id)),
            StepOp::TrustRootBootstrap,
        ),
        Err(_) => (
            ApplyAction::Create,
            "operator key will be generated".to_string(),
            StepOp::TrustRootBootstrap,
        ),
    };
    Ok(ApplyStep {
        kind: ApplyStepKind::BootstrapTrustRoot,
        key,
        action,
        detail,
        idempotency_key: None,
        op,
    })
}

// --- execute --------------------------------------------------------------------

fn execute(store: &LocalFsStore, ctx: &ApplyContext, steps: &[ApplyStep]) -> Result<(), OpError> {
    // Post-confirmation, pre-first-mutation artifact gate: re-hash every
    // bundle artifact to detect changes that occurred during the
    // (potentially unbounded) TTY confirmation pause. A changed artifact
    // aborts the whole apply before ANY step (including put-secret)
    // mutates the store. The per-Deploy-step re-check stays because later
    // steps in the same run can still race with external writes.
    for rb in &ctx.bundles {
        for rr in &rb.revisions {
            ensure_artifact_unchanged(&rr.resolved_path, &rr.digest)?;
        }
    }

    // Sub-verbs get clean flags: payloads are passed directly, never re-read
    // from the manifest path.
    let exec_flags = OpFlags::default();
    let mut created_endpoints: BTreeMap<String, String> = BTreeMap::new();
    let total = steps.len();

    for (i, step) in steps.iter().enumerate() {
        let n = i + 1;
        if step.action == ApplyAction::NoOp {
            eprintln!(
                "[{n}/{total}] {:<22} {:<40} no-op",
                step.kind.label(),
                step.key
            );
            continue;
        }
        eprintln!(
            "[{n}/{total}] {:<22} {:<40} {}",
            step.kind.label(),
            step.key,
            step.action.as_str()
        );
        let result: Result<(), OpError> = match &step.op {
            StepOp::None => Ok(()),
            StepOp::EnvInit { public_base_url } => super::env::init(
                store,
                &exec_flags,
                EnvInitPayload {
                    public_base_url: public_base_url.clone(),
                },
            )
            .map(|_| ()),
            StepOp::SetPublicUrl { url } => {
                super::env::set_public_url(store, &exec_flags, ctx.env_id.as_str(), url).map(|_| ())
            }
            StepOp::UpdateHostConfig(payload) => {
                super::config::set(store, &exec_flags, Some((**payload).clone())).map(|_| ())
            }
            StepOp::AddPackBinding(payload) => {
                super::env_packs::add(store, &exec_flags, Some((**payload).clone())).map(|_| ())
            }
            StepOp::UpdatePackBinding(payload) => {
                super::env_packs::update(store, &exec_flags, Some((**payload).clone())).map(|_| ())
            }
            StepOp::AddExtension(payload) => {
                super::extensions::add(store, &exec_flags, Some((**payload).clone())).map(|_| ())
            }
            StepOp::UpdateExtension(payload) => {
                super::extensions::update(store, &exec_flags, Some((**payload).clone())).map(|_| ())
            }
            StepOp::TrustRootBootstrap => super::trust_root::bootstrap(
                store,
                &exec_flags,
                Some(TrustRootBootstrapPayload {
                    environment_id: ctx.env_id.as_str().to_string(),
                }),
            )
            .map(|_| ()),
            StepOp::PutSecret { path } => {
                let value = ctx
                    .secret_values
                    .get(path)
                    .expect("validated: every put-secret step has a resolved value");
                super::secrets::put(
                    store,
                    &exec_flags,
                    Some(SecretsPutPayload {
                        environment_id: ctx.env_id.as_str().to_string(),
                        path: path.clone(),
                        value: value.expose().to_string(),
                        idempotency_key: step.idempotency_key.clone(),
                    }),
                )
                .map(|_| ())
            }
            StepOp::Deploy {
                payload,
                expected_digest,
            } => {
                ensure_artifact_unchanged(
                    payload
                        .bundle_path
                        .as_deref()
                        .expect("apply-built Deploy always has bundle_path"),
                    expected_digest,
                )?;
                super::deploy::deploy(store, &exec_flags, Some((**payload).clone())).map(|_| ())
            }
            StepOp::DeploySplit { .. } => {
                execute_deploy_split(store, &exec_flags, &step.op)?;
                Ok(())
            }
            StepOp::BundleUpdate(payload) => {
                super::bundles::update(store, &exec_flags, Some((**payload).clone())).map(|_| ())
            }
            StepOp::EndpointAdd(payload) => super::messaging::add(
                store,
                &exec_flags,
                Some((**payload).clone()),
            )
            .map(|outcome| {
                if let Ok(summary) = serde_json::from_value::<EndpointSummary>(outcome.result) {
                    created_endpoints.insert(payload.display_name.clone(), summary.endpoint_id);
                }
            }),
            StepOp::EndpointLink {
                endpoint,
                bundle_id,
            } => resolve_endpoint_id(endpoint, &created_endpoints).and_then(|endpoint_id| {
                super::messaging::link_bundle(
                    store,
                    &exec_flags,
                    Some(EndpointLinkBundlePayload {
                        environment_id: ctx.env_id.as_str().to_string(),
                        endpoint_id,
                        bundle_id: bundle_id.clone(),
                        idempotency_key: step.idempotency_key.clone(),
                        updated_by: ctx.updated_by.clone(),
                    }),
                )
                .map(|_| ())
            }),
            StepOp::WelcomeFlow { endpoint, flow } => {
                resolve_endpoint_id(endpoint, &created_endpoints).and_then(|endpoint_id| {
                    super::messaging::set_welcome_flow(
                        store,
                        &exec_flags,
                        Some(EndpointSetWelcomeFlowPayload {
                            environment_id: ctx.env_id.as_str().to_string(),
                            endpoint_id,
                            bundle_id: flow.bundle_id.clone(),
                            pack_id: flow.pack_id.clone(),
                            flow_id: flow.flow_id.clone(),
                            idempotency_key: step.idempotency_key.clone(),
                            updated_by: ctx.updated_by.clone(),
                        }),
                    )
                    .map(|_| ())
                })
            }
        };
        if let Err(err) = result {
            let remaining = total - n;
            eprintln!(
                "apply: step {n}/{total} `{} {}` failed; {remaining} step(s) not attempted. \
                 Fix the cause and re-run the same apply — completed steps replay as no-ops.",
                step.kind.label(),
                step.key
            );
            return Err(err);
        }
    }
    Ok(())
}

/// Resolve an [`EndpointRef`] to a concrete `endpoint_id`. `CreatedByName`
/// entries are filled in-process from the `add-endpoint` outcome of this
/// same run — the exact output-capture plumbing the manifest eliminates for
/// the operator.
fn resolve_endpoint_id(
    endpoint: &EndpointRef,
    created: &BTreeMap<String, String>,
) -> Result<String, OpError> {
    match endpoint {
        EndpointRef::Existing(id) => Ok(id.clone()),
        EndpointRef::CreatedByName(name) => created.get(name).cloned().ok_or_else(|| {
            OpError::InvalidArgument(format!(
                "internal: endpoint `{name}` was not created before its link step \
                 (add-endpoint outcome missing)"
            ))
        }),
    }
}

// --- verify ---------------------------------------------------------------------

/// Store-level post-conditions: re-read the environment and assert the
/// manifest's desired state is visible. Runtime readiness is the doctor's
/// job (PR-3), not apply's. Secrets are deliberately NOT verified: there is
/// no `get` until A9, and reader-side resolvability is exactly the doctor's
/// check — `put`'s own write-through already failed the step if the store
/// rejected it.
fn verify(store: &LocalFsStore, ctx: &ApplyContext) -> Result<Value, OpError> {
    let env = store.load(&ctx.env_id)?;
    let mut failures: Vec<String> = Vec::new();
    let mut checked = 0usize;

    if let Some(url) = &ctx.canonical_public_base_url {
        checked += 1;
        if env.host_config.public_base_url.as_deref() != Some(url.as_str()) {
            failures.push(format!(
                "public_base_url is `{:?}`, expected `{url}`",
                env.host_config.public_base_url
            ));
        }
    }

    // Host-config fields.
    {
        let me = &ctx.manifest.environment;
        if let Some(name) = &me.name {
            checked += 1;
            if *name != env.name {
                failures.push(format!("name is `{}`, expected `{name}`", env.name));
            }
        }
        if let Some(region) = &me.region {
            checked += 1;
            if env.host_config.region.as_deref() != Some(region.as_str()) {
                failures.push(format!(
                    "region is `{:?}`, expected `{region}`",
                    env.host_config.region
                ));
            }
        }
        if let Some(tenant_org_id) = &me.tenant_org_id {
            checked += 1;
            if env.host_config.tenant_org_id.as_deref() != Some(tenant_org_id.as_str()) {
                failures.push(format!(
                    "tenant_org_id is `{:?}`, expected `{tenant_org_id}`",
                    env.host_config.tenant_org_id
                ));
            }
        }
        if let Some(listen_addr) = &me.listen_addr {
            checked += 1;
            let parsed: std::net::SocketAddr = listen_addr
                .parse()
                .expect("validate_shape already validated listen_addr");
            if env.host_config.listen_addr != Some(parsed) {
                failures.push(format!(
                    "listen_addr is `{:?}`, expected `{parsed}`",
                    env.host_config.listen_addr
                ));
            }
        }
        if let Some(gui_enabled) = me.gui_enabled {
            checked += 1;
            if env.host_config.gui_enabled != Some(gui_enabled) {
                failures.push(format!(
                    "gui_enabled is `{:?}`, expected `{gui_enabled}`",
                    env.host_config.gui_enabled
                ));
            }
        }
    }

    if ctx.manifest.trust_root == Some(TrustRootDirective::Bootstrap) {
        checked += 1;
        let env_dir = store.env_dir(&ctx.env_id)?;
        let trust_root = store_trust_root::load(&env_dir)?;
        match crate::operator_key::load_existing_only() {
            Ok(k)
                if trust_root
                    .keys
                    .iter()
                    .any(|t| t.key_id.eq_ignore_ascii_case(&k.key_id)) => {}
            Ok(k) => failures.push(format!(
                "operator key {} is not in the trust root",
                short_key_id(&k.key_id)
            )),
            Err(e) => failures.push(format!("operator key not loadable: {e}")),
        }
    }

    // Env-packs.
    for mp in &ctx.manifest.packs {
        checked += 1;
        let Some(b) = env.pack_for_slot(mp.slot) else {
            failures.push(format!("pack slot `{}` is not bound", mp.slot));
            continue;
        };
        if b.kind.to_string() != mp.kind {
            failures.push(format!(
                "pack slot `{}`: kind is `{}`, expected `{}`",
                mp.slot, b.kind, mp.kind
            ));
        }
        if b.pack_ref.as_str() != mp.pack_ref {
            failures.push(format!(
                "pack slot `{}`: pack_ref is `{}`, expected `{}`",
                mp.slot,
                b.pack_ref.as_str(),
                mp.pack_ref
            ));
        }
    }

    // Extensions.
    for mx in &ctx.manifest.extensions {
        checked += 1;
        let kind_path = greentic_deploy_spec::PackDescriptor::try_new(&mx.kind)
            .expect("validate_shape already validated kind")
            .path()
            .to_string();
        let ext_key = format!(
            "{}{}",
            kind_path,
            mx.instance_id
                .as_ref()
                .map(|i| format!("/{i}"))
                .unwrap_or_default()
        );
        let Some(b) = env
            .extensions
            .iter()
            .find(|b| b.kind.path() == kind_path && b.instance_id == mx.instance_id)
        else {
            failures.push(format!("extension `{ext_key}` is not bound"));
            continue;
        };
        if b.kind.to_string() != mx.kind {
            failures.push(format!(
                "extension `{ext_key}`: kind is `{}`, expected `{}`",
                b.kind, mx.kind
            ));
        }
        if b.pack_ref.as_str() != mx.pack_ref {
            failures.push(format!(
                "extension `{ext_key}`: pack_ref is `{}`, expected `{}`",
                b.pack_ref.as_str(),
                mx.pack_ref
            ));
        }
    }

    for rb in &ctx.bundles {
        checked += 1;
        let Some(dep) = env
            .bundles
            .iter()
            .find(|d| d.bundle_id.as_str() == rb.spec.bundle_id && d.customer_id == rb.customer_id)
        else {
            failures.push(format!("bundle `{}` is not deployed", rb.spec.bundle_id));
            continue;
        };
        let is_multi = rb.spec.revisions.is_some();
        if is_multi {
            if !split_converged(&env, dep.deployment_id, &rb.revisions) {
                failures.push(format!(
                    "bundle `{}`: traffic split not converged ({} revision(s) expected)",
                    rb.spec.bundle_id,
                    rb.revisions.len()
                ));
            }
        } else {
            let primary_digest = &rb.revisions[0].digest;
            if !deployment_converged(&env, dep.deployment_id, primary_digest) {
                failures.push(format!(
                    "bundle `{}`: live revision digest is `{}`, expected `{}`",
                    rb.spec.bundle_id,
                    live_revision_digest(&env, dep.deployment_id).unwrap_or("none"),
                    primary_digest
                ));
            }
        }
        if let Some(binding) = &rb.spec.route_binding {
            let desired = into_route_binding(binding.clone());
            if desired != dep.route_binding {
                failures.push(format!(
                    "bundle `{}`: route_binding differs from the manifest",
                    rb.spec.bundle_id
                ));
            }
        }
        if let Some(overrides) = &rb.spec.config_overrides
            && *overrides != dep.config_overrides
        {
            failures.push(format!(
                "bundle `{}`: config_overrides differ from the manifest",
                rb.spec.bundle_id
            ));
        }
        // revenue_share (G2) is threaded into the create path AND reconciled
        // for existing deployments, so it is correct after a single apply —
        // assert it whenever declared.
        if let Some(shares) = &rb.spec.revenue_share
            && convert_revenue_share(shares) != dep.revenue_share
        {
            failures.push(format!(
                "bundle `{}`: revenue_share differs from the manifest",
                rb.spec.bundle_id
            ));
        }
        // status (G3) is reconcile-only against an EXISTING deployment. A
        // deployment created during THIS apply is born `active`, so a declared
        // non-`active` status converges on the next apply — don't fail verify
        // for it here. Assert only when the deployment pre-dated this apply.
        let existed_pre_apply = ctx.env.as_ref().is_some_and(|e| {
            e.bundles
                .iter()
                .any(|d| d.bundle_id == dep.bundle_id && d.customer_id == dep.customer_id)
        });
        if let Some(status) = rb.spec.status
            && existed_pre_apply
            && status != dep.status
        {
            failures.push(format!(
                "bundle `{}`: status is `{:?}`, expected `{status:?}`",
                rb.spec.bundle_id, dep.status
            ));
        }
    }

    // Verify endpoints against the freshly reloaded env (re-match on
    // purpose — this IS the verify, not a cached result from validation).
    for ep in &ctx.manifest.messaging_endpoints {
        checked += 1;
        let Some(m) = match_existing_endpoint(&env, ep)? else {
            failures.push(format!("endpoint `{}` is absent", ep.name));
            continue;
        };
        for link in &ep.links {
            if !m.linked_bundles.iter().any(|b| b.as_str() == *link) {
                failures.push(format!("endpoint `{}`: link `{link}` is absent", ep.name));
            }
        }
        if let Some(wf) = &ep.welcome_flow {
            let equal = m.welcome_flow.as_ref().is_some_and(|cur| {
                cur.bundle_id.as_str() == wf.bundle_id
                    && cur.pack_id.as_str() == wf.pack_id
                    && cur.flow_id == wf.flow_id
            });
            if !equal {
                failures.push(format!(
                    "endpoint `{}`: welcome_flow differs from the manifest",
                    ep.name
                ));
            }
        }
    }

    if failures.is_empty() {
        Ok(json!({ "checked": checked, "failures": [] }))
    } else {
        Err(OpError::Conflict(format!(
            "apply executed but post-verify found {} mismatch(es): {}",
            failures.len(),
            failures.join("; ")
        )))
    }
}

// --- helpers --------------------------------------------------------------------

/// True when the manifest declares a revenue-share that differs from the live
/// deployment's split. Absent in the manifest = "leave untouched" = no diff.
fn revenue_share_differs(
    rb: &ResolvedBundle,
    dep: &greentic_deploy_spec::BundleDeployment,
) -> bool {
    rb.spec
        .revenue_share
        .as_ref()
        .is_some_and(|s| convert_revenue_share(s) != dep.revenue_share)
}

/// True when the manifest declares a status that differs from the live
/// deployment's status. Absent in the manifest = "leave untouched" = no diff.
fn status_differs(rb: &ResolvedBundle, dep: &greentic_deploy_spec::BundleDeployment) -> bool {
    rb.spec.status.is_some_and(|s| s != dep.status)
}

/// Desired `bundles update` mutations for one bundle entry, derived against a
/// live deployment. Each `Some` field is applied; an all-`None` set means no
/// update step is needed.
struct BundleMetaDiff {
    /// Set when the manifest route binding differs from the live binding.
    route_binding: Option<RouteBindingPayload>,
    /// Set when overrides differ AND the deploy/stage gate allows applying
    /// them this run (they otherwise ride the deploy, or defer one apply).
    config_overrides: Option<BTreeMap<String, BTreeMap<String, Value>>>,
    /// Set when the manifest revenue-share differs from the live split.
    revenue_share: Option<Vec<RevenueShareEntryPayload>>,
    /// Set when the manifest status differs from the live status.
    status: Option<BundleDeploymentStatus>,
}

impl BundleMetaDiff {
    fn is_noop(&self) -> bool {
        self.route_binding.is_none()
            && self.config_overrides.is_none()
            && self.revenue_share.is_none()
            && self.status.is_none()
    }
}

/// Build a `bundles update` step from the desired metadata mutations, or
/// `None` when nothing differs. Centralizes the idempotency-key derivation,
/// the human `detail` string, and the payload so both reconcile arms
/// (single- and multi-revision) share one construction.
fn bundle_meta_update_step(
    env_id: &EnvId,
    env_id_str: &str,
    bundle_id: &str,
    deployment_id: DeploymentId,
    desired_binding: &Option<RouteBinding>,
    diff: BundleMetaDiff,
) -> Option<ApplyStep> {
    if diff.is_noop() {
        return None;
    }
    // Hash the revenue-share as ordered (party, bps) pairs (the payload type
    // isn't directly hashable through `hash_json` without this projection).
    let revenue_share_hash = diff.revenue_share.as_ref().map(|s| {
        s.iter()
            .map(|e| (e.party_id.clone(), e.basis_points))
            .collect::<Vec<_>>()
    });
    let desired_hash = hash_json(&json!({
        "route_binding": diff.route_binding,
        "config_overrides": diff.config_overrides,
        "revenue_share": revenue_share_hash,
        "status": diff.status,
    }));
    let ikey = derive_idempotency_key(
        env_id,
        ApplyStepKind::UpdateBundle.label(),
        bundle_id,
        &desired_hash,
    );
    let mut what = Vec::new();
    if diff.route_binding.is_some() {
        what.push(format!("binding → {}", binding_summary(desired_binding)));
    }
    if diff.config_overrides.is_some() {
        what.push("config_overrides".to_string());
    }
    if diff.revenue_share.is_some() {
        what.push("revenue_share".to_string());
    }
    if let Some(s) = diff.status {
        what.push(format!("status → {s:?}"));
    }
    Some(ApplyStep {
        kind: ApplyStepKind::UpdateBundle,
        key: bundle_id.to_string(),
        action: ApplyAction::Update,
        detail: what.join(", "),
        idempotency_key: Some(ikey.clone()),
        op: StepOp::BundleUpdate(Box::new(BundleUpdatePayload {
            environment_id: env_id_str.to_string(),
            deployment_id: deployment_id.to_string(),
            status: diff.status,
            route_binding: diff.route_binding,
            revenue_share: diff.revenue_share,
            config_overrides: diff.config_overrides,
            idempotency_key: Some(ikey),
        })),
    })
}

/// Build a [`BundleDeployPayload`] from a resolved single-revision bundle.
/// The primary (first) resolved revision supplies the artifact path.
fn deploy_payload(
    env_id: &str,
    rb: &ResolvedBundle,
    route_binding: Option<RouteBindingPayload>,
) -> BundleDeployPayload {
    BundleDeployPayload {
        environment_id: env_id.to_string(),
        bundle_id: rb.spec.bundle_id.clone(),
        customer_id: rb.spec.customer_id.clone(),
        bundle_path: Some(rb.revisions[0].resolved_path.clone()),
        idempotency_key: None,
        config_overrides: rb.spec.config_overrides.clone(),
        route_binding,
        // Threaded into the FRESH-add path; ignored by `deploy` on a
        // re-deploy (the existing split is reconciled via `bundles update`).
        revenue_share: rb.spec.revenue_share.clone(),
    }
}

/// Build a [`StepOp::DeploySplit`] from a resolved multi-revision bundle.
fn deploy_split_op(env_id: &str, rb: &ResolvedBundle) -> StepOp {
    StepOp::DeploySplit {
        env_id: env_id.to_string(),
        bundle_id: rb.spec.bundle_id.clone(),
        customer_id: rb.spec.customer_id.clone(),
        config_overrides: rb.spec.config_overrides.clone(),
        route_binding: rb.spec.route_binding.clone(),
        revenue_share: rb.spec.revenue_share.clone(),
        revisions: rb
            .revisions
            .iter()
            .map(|rr| SplitRevisionEntry {
                name: rr.spec.name.clone(),
                resolved_path: rr.resolved_path.clone(),
                expected_digest: rr.digest.clone(),
                weight_bps: rr.weight_bps,
                drain_seconds: rr.spec.drain_seconds,
            })
            .collect(),
    }
}

/// Execute a multi-revision deploy: add the bundle (if new), stage each
/// revision, warm each, then set the combined traffic split. Takes the
/// whole `StepOp` by reference and destructures the `DeploySplit` variant.
fn execute_deploy_split(store: &LocalFsStore, flags: &OpFlags, op: &StepOp) -> Result<(), OpError> {
    use super::bundles::{BundleAddPayload, BundleSummary};
    use super::revisions::{RevisionStagePayload, RevisionSummary, RevisionTransitionPayload};
    use super::traffic::{TrafficSetEntryPayload, TrafficSetPayload};

    let StepOp::DeploySplit {
        env_id,
        bundle_id,
        customer_id,
        config_overrides,
        route_binding,
        revenue_share,
        revisions: split_revs,
    } = op
    else {
        unreachable!("execute_deploy_split called with non-DeploySplit op");
    };

    let env_id_parsed = EnvId::try_from(env_id.as_str())
        .map_err(|e| OpError::InvalidArgument(format!("environment_id: {e}")))?;
    let resolved_customer =
        super::bundles::resolve_customer_id(&env_id_parsed, customer_id.clone())?;

    let env = store.load(&env_id_parsed)?;
    let existing = env
        .bundles
        .iter()
        .find(|b| b.bundle_id.as_str() == bundle_id.as_str() && b.customer_id == resolved_customer);

    let deployment_id = match existing {
        Some(b) => b.deployment_id.to_string(),
        None => {
            let add_payload = BundleAddPayload {
                environment_id: env_id.clone(),
                bundle_id: bundle_id.clone(),
                customer_id: customer_id.clone(),
                route_binding: route_binding.clone().unwrap_or_default(),
                revenue_share: revenue_share
                    .clone()
                    .unwrap_or_else(super::bundles::default_revenue_share),
                authorization_ref: super::bundles::default_authorization_ref(),
                config_overrides: config_overrides.clone().unwrap_or_default(),
                idempotency_key: None,
            };
            let outcome = super::bundles::add(store, flags, Some(add_payload))?;
            let summary: BundleSummary = serde_json::from_value(outcome.result).map_err(|e| {
                OpError::InvalidArgument(format!("internal: bundle add summary: {e}"))
            })?;
            summary.deployment_id
        }
    };

    // Stage + warm each revision.
    let mut traffic_entries = Vec::with_capacity(split_revs.len());
    for rev in split_revs {
        eprintln!(
            "  split: staging revision `{}` ({})",
            rev.name,
            short_digest(&rev.expected_digest)
        );
        // TOCTOU re-check per revision.
        ensure_artifact_unchanged(&rev.resolved_path, &rev.expected_digest)?;

        let stage_payload = RevisionStagePayload {
            environment_id: env_id.to_string(),
            deployment_id: deployment_id.clone(),
            bundle_path: Some(rev.resolved_path.clone()),
            bundle_digest: super::revisions::default_bundle_digest(),
            pack_list: Vec::new(),
            pack_list_lock_ref: PathBuf::new(),
            config_digest: super::revisions::default_config_digest(),
            signature_sidecar_ref: super::revisions::default_signature_sidecar_ref(),
            drain_seconds: rev
                .drain_seconds
                .unwrap_or_else(super::revisions::default_drain_seconds),
        };
        let stage_outcome = super::revisions::stage(store, flags, Some(stage_payload))?;
        let staged: RevisionSummary =
            serde_json::from_value(stage_outcome.result).map_err(|e| {
                OpError::InvalidArgument(format!("internal: revision stage summary: {e}"))
            })?;

        super::revisions::warm(
            store,
            flags,
            Some(RevisionTransitionPayload {
                environment_id: env_id.to_string(),
                revision_id: staged.revision_id.clone(),
                idempotency_key: None,
            }),
        )?;

        traffic_entries.push(TrafficSetEntryPayload {
            revision_id: staged.revision_id,
            weight_bps: Some(rev.weight_bps),
            weight_percent: None,
        });
    }

    // Set the combined traffic split.
    let ikey = format!(
        "deploy-split:{deployment_id}:{}",
        traffic_entries
            .iter()
            .map(|e| e.revision_id.as_str())
            .collect::<Vec<_>>()
            .join("+")
    );
    super::traffic::set(
        store,
        flags,
        Some(TrafficSetPayload {
            environment_id: env_id.to_string(),
            deployment_id: deployment_id.clone(),
            entries: traffic_entries,
            updated_by: super::traffic::default_updated_by(),
            idempotency_key: ikey,
            authorization_ref: super::traffic::default_authorization_ref(),
        }),
    )?;

    Ok(())
}

/// Deterministic, replay-safe idempotency key:
/// `ulid(truncate_128(sha256(schema ‖ env ‖ kind ‖ natural_key ‖ desired_hash)))`.
/// Same manifest → same keys (a retry replays); changed desired state → new
/// keys (a real mutation). Rendered as a 26-char Crockford-base32 ULID so it
/// satisfies the `greentic-deploy-spec` `IdempotencyKey` shape.
fn derive_idempotency_key(
    env_id: &EnvId,
    step_kind: &str,
    natural_key: &str,
    desired_state_hash: &str,
) -> String {
    let mut hasher = Sha256::new();
    for part in [
        ENV_MANIFEST_SCHEMA_V1,
        env_id.as_str(),
        step_kind,
        natural_key,
        desired_state_hash,
    ] {
        hasher.update(part.as_bytes());
        hasher.update([0u8]);
    }
    let digest = hasher.finalize();
    let mut bytes = [0u8; 16];
    bytes.copy_from_slice(&digest[..16]);
    ulid::Ulid::from(u128::from_be_bytes(bytes)).to_string()
}

/// SHA-256 of the canonical JSON serialization (keys sorted by serde_json).
/// The output feeds deterministic idempotency keys, so the serialization
/// must stay canonical across builds (no `preserve_order` feature).
fn hash_json(value: &Value) -> String {
    let mut hasher = Sha256::new();
    hasher.update(value.to_string().as_bytes());
    hex::encode(hasher.finalize())
}

/// Digest of the revision currently carrying the (highest-weight) live
/// traffic for a deployment. `None` when the deployment has no split or the
/// split references an unknown revision.
fn live_revision_digest(env: &Environment, deployment_id: DeploymentId) -> Option<&str> {
    let split = env
        .traffic_splits
        .iter()
        .find(|s| s.deployment_id == deployment_id)?;
    let entry = split.entries.iter().max_by_key(|e| e.weight_bps)?;
    env.revisions
        .iter()
        .find(|r| r.revision_id == entry.revision_id)
        .map(|r| r.bundle_digest.as_str())
}

/// A digest the diff can trust: real `sha256:` material, not the
/// `sha256:00` placeholder pre-digest revisions carry.
fn digest_is_real(digest: &str) -> bool {
    digest.starts_with("sha256:") && digest.len() > "sha256:".len() && digest != "sha256:00"
}

/// Strict convergence: the deployment's traffic split has EXACTLY ONE entry
/// at full weight (10,000 bps), that entry's revision exists, carries a real
/// digest, and the digest matches `expected_digest`. A mixed split (e.g.
/// 60/40 blue-green) or a degenerate placeholder digest is NOT converged.
fn deployment_converged(
    env: &Environment,
    deployment_id: DeploymentId,
    expected_digest: &str,
) -> bool {
    let Some(split) = env
        .traffic_splits
        .iter()
        .find(|s| s.deployment_id == deployment_id)
    else {
        return false;
    };
    if split.entries.len() != 1 || split.entries[0].weight_bps != super::deploy::FULL_TRAFFIC_BPS {
        return false;
    }
    let entry = &split.entries[0];
    env.revisions
        .iter()
        .find(|r| r.revision_id == entry.revision_id)
        .is_some_and(|r| digest_is_real(&r.bundle_digest) && r.bundle_digest == expected_digest)
}

/// Multi-revision convergence: the deployment's traffic split is the exact
/// `(digest, weight_bps)` multiset declared by `expected` — every live entry
/// resolves to a real-digest revision, and the live `(digest, weight)` bag
/// equals the expected bag. Order-independent (both bags are sorted before
/// comparison) and duplicate-safe: two expected revisions that share the same
/// artifact AND weight require two matching live entries, not one. A false
/// "converged" is the dangerous direction — it would silently skip applying
/// the desired split — so this is multiset equality, not set containment.
fn split_converged(
    env: &Environment,
    deployment_id: DeploymentId,
    expected: &[ResolvedRevision],
) -> bool {
    let Some(split) = env
        .traffic_splits
        .iter()
        .find(|s| s.deployment_id == deployment_id)
    else {
        return false;
    };
    if split.entries.len() != expected.len() {
        return false;
    }
    // Live `(digest, weight_bps)` bag. Any entry pointing at a missing or
    // placeholder-digest revision fails convergence outright.
    let mut live_bag: Vec<(&str, u32)> = Vec::with_capacity(split.entries.len());
    for entry in &split.entries {
        let Some(rev) = env
            .revisions
            .iter()
            .find(|r| r.revision_id == entry.revision_id)
        else {
            return false;
        };
        if !digest_is_real(&rev.bundle_digest) {
            return false;
        }
        live_bag.push((rev.bundle_digest.as_str(), entry.weight_bps));
    }
    // Expected `(digest, weight_bps)` bag.
    let mut expected_bag: Vec<(&str, u32)> = expected
        .iter()
        .map(|rr| (rr.digest.as_str(), rr.weight_bps))
        .collect();
    // Multiset equality via sort-and-compare (counts already match).
    live_bag.sort_unstable();
    expected_bag.sort_unstable();
    live_bag == expected_bag
}

/// Re-hash the artifact at `path` and verify it still matches the digest
/// recorded at plan time. Returns `Err(Conflict)` if the bytes changed
/// between planning and execution (shrinks the TOCTOU window to what
/// `op deploy` itself has).
fn ensure_artifact_unchanged(path: &Path, expected: &str) -> Result<(), OpError> {
    let actual = super::bundle_stage::sha256_file(path).map_err(|source| OpError::Io {
        path: path.to_path_buf(),
        source,
    })?;
    if actual != expected {
        return Err(OpError::Conflict(format!(
            "artifact `{}` changed since the plan was computed (expected {}, found {}); \
             re-run apply",
            path.display(),
            short_digest(expected),
            short_digest(&actual),
        )));
    }
    Ok(())
}

fn short_digest(digest: &str) -> &str {
    let end = digest.len().min("sha256:".len() + 8);
    &digest[..end]
}

fn short_key_id(key_id: &str) -> &str {
    &key_id[..key_id.len().min(8)]
}

fn binding_summary(binding: &Option<RouteBinding>) -> String {
    match binding {
        None => "default binding".to_string(),
        Some(rb) => {
            let mut parts = Vec::new();
            if !rb.path_prefixes.is_empty() {
                parts.push(rb.path_prefixes.join(","));
            }
            if !rb.hosts.is_empty() {
                parts.push(format!("hosts={}", rb.hosts.join(",")));
            }
            parts.push(format!(
                "tenant={}/{}",
                rb.tenant_selector.tenant, rb.tenant_selector.team
            ));
            parts.join(" ")
        }
    }
}

fn render_plan(steps: &[ApplyStep], warnings: &[String], missing: &[MissingItem]) {
    eprintln!("plan ({} step(s)):", steps.len());
    for step in steps {
        eprintln!(
            "  {:<22} {:<40} {:<7} {}",
            step.kind.label(),
            step.key,
            step.action.as_str(),
            step.detail
        );
    }
    for m in missing {
        eprintln!(
            "  missing: {:<14} {:<40} {}",
            m.kind.as_str(),
            m.key,
            m.source
        );
    }
    for w in warnings {
        eprintln!("  warning: {w}");
    }
}

fn report_json(
    ctx: &ApplyContext,
    steps: &[ApplyStep],
    mode: &str,
    verify: Option<Value>,
) -> Value {
    let changed = steps
        .iter()
        .filter(|s| s.action != ApplyAction::NoOp)
        .count();
    // Always-put rows: counted in `changed`, but not drift for `--check`
    // (see `ApplyAction::counts_as_drift`). Emitted in every mode so the
    // report schema doesn't vary by mode.
    let undiffable = steps
        .iter()
        .filter(|s| s.action == ApplyAction::Put)
        .count();
    let mut report = json!({
        "manifest_schema": ENV_MANIFEST_SCHEMA_V1,
        "environment_id": ctx.env_id.as_str(),
        "mode": mode,
        "steps": steps.iter().map(ApplyStep::to_json).collect::<Vec<_>>(),
        "changed": changed,
        "no_op": steps.len() - changed,
        "undiffable": undiffable,
        "missing": ctx.missing.iter().map(MissingItem::to_json).collect::<Vec<_>>(),
        "warnings": ctx.warnings,
    });
    if let Some(v) = verify {
        report["verify"] = v;
    }
    report
}

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

    #[test]
    fn idempotency_keys_are_deterministic_and_state_sensitive() {
        let env_id = EnvId::try_from("local").unwrap();
        let a = derive_idempotency_key(&env_id, "add-endpoint", "realbot-legal", "h1");
        let b = derive_idempotency_key(&env_id, "add-endpoint", "realbot-legal", "h1");
        assert_eq!(a, b, "same inputs must derive the same key");
        assert_eq!(a.len(), 26, "ULID rendering is 26 chars");
        // Every input term must discriminate.
        let other_env = EnvId::try_from("prod").unwrap();
        assert_ne!(
            a,
            derive_idempotency_key(&other_env, "add-endpoint", "realbot-legal", "h1")
        );
        assert_ne!(
            a,
            derive_idempotency_key(&env_id, "link-endpoint", "realbot-legal", "h1")
        );
        assert_ne!(
            a,
            derive_idempotency_key(&env_id, "add-endpoint", "realbot-acct", "h1")
        );
        assert_ne!(
            a,
            derive_idempotency_key(&env_id, "add-endpoint", "realbot-legal", "h2")
        );
        // And the key must satisfy the spec shape.
        greentic_deploy_spec::IdempotencyKey::new(a).expect("derived key is spec-valid");
    }

    #[test]
    fn degenerate_digests_are_not_real() {
        assert!(!digest_is_real("sha256:00"));
        assert!(!digest_is_real("sha256:"));
        assert!(!digest_is_real(""));
        assert!(!digest_is_real("md5:abcd"));
        assert!(digest_is_real("sha256:ab12cd34"));
    }

    // --- LocalFsStore integration ---------------------------------------------

    use crate::cli::tests_common::{
        bootstrap_env_trust_root, make_binding, make_bundle_deployment, make_env, make_revision,
        make_traffic_split,
    };
    use greentic_deploy_spec::{CapabilitySlot, RevisionLifecycle};
    use std::path::Path;
    use tempfile::tempdir;

    fn seeded_store() -> (tempfile::TempDir, LocalFsStore) {
        let dir = tempdir().unwrap();
        let store = LocalFsStore::new(dir.path());
        store.save(&make_env("local")).unwrap();
        let env_dir = store.env_dir(&EnvId::try_from("local").unwrap()).unwrap();
        bootstrap_env_trust_root(&env_dir);
        (dir, store)
    }

    fn fixture() -> PathBuf {
        PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .join("testdata/bundles/perf-smoke-bundle.gtbundle")
    }

    fn write_manifest(dir: &Path, value: &Value) -> PathBuf {
        let path = dir.join("manifest.json");
        std::fs::write(&path, serde_json::to_vec_pretty(value).unwrap()).unwrap();
        path
    }

    fn run_mode(
        store: &LocalFsStore,
        manifest_path: &Path,
        mode: ApplyMode,
    ) -> Result<OpOutcome, OpError> {
        let flags = OpFlags {
            schema_only: false,
            answers: Some(manifest_path.to_path_buf()),
        };
        apply(
            store,
            &flags,
            ApplyOptions {
                mode,
                ..ApplyOptions::default()
            },
        )
    }

    fn run_apply(store: &LocalFsStore, manifest_path: &Path) -> Result<OpOutcome, OpError> {
        run_mode(store, manifest_path, ApplyMode::Apply)
    }

    fn run_dry(store: &LocalFsStore, manifest_path: &Path) -> Result<OpOutcome, OpError> {
        run_mode(store, manifest_path, ApplyMode::DryRun)
    }

    fn run_check(store: &LocalFsStore, manifest_path: &Path) -> Result<OpOutcome, OpError> {
        run_mode(store, manifest_path, ApplyMode::Check)
    }

    fn load_local(store: &LocalFsStore) -> Environment {
        store.load(&EnvId::try_from("local").unwrap()).unwrap()
    }

    /// The two-dept-shaped manifest: one bundle with a tenant-scoped path
    /// binding, one telegram endpoint linking it, plus a welcome flow.
    fn full_manifest(bundle_path: &Path) -> Value {
        json!({
            "schema": ENV_MANIFEST_SCHEMA_V1,
            "environment": {"id": "local"},
            "bundles": [{
                "bundle_id": "quickstart",
                "bundle_path": bundle_path,
                "route_binding": {
                    "path_prefixes": ["/legal"],
                    "tenant_selector": {"tenant": "legal", "team": "default"}
                }
            }],
            "messaging_endpoints": [{
                "name": "legal-bot",
                "provider_type": "messaging.telegram.bot",
                "links": ["quickstart"],
                "welcome_flow": {
                    "bundle_id": "quickstart",
                    "pack_id": "perf-smoke-pack",
                    "flow_id": "main"
                }
            }]
        })
    }

    fn step_actions(outcome: &Value) -> Vec<(String, String)> {
        outcome["steps"]
            .as_array()
            .expect("steps array")
            .iter()
            .map(|s| {
                (
                    s["kind"].as_str().unwrap().to_string(),
                    s["action"].as_str().unwrap().to_string(),
                )
            })
            .collect()
    }

    #[test]
    fn fresh_apply_then_noop_reapply() {
        let (dir, store) = seeded_store();
        let manifest_path = write_manifest(dir.path(), &full_manifest(&fixture()));

        let outcome = run_apply(&store, &manifest_path).expect("first apply succeeds");
        let result = outcome.result;
        assert_eq!(result["mode"], "apply");
        // deploy + add-endpoint + link + welcome-flow change; ensure-env no-ops.
        assert_eq!(result["changed"], 4, "result: {result}");
        assert_eq!(
            result["verify"]["failures"].as_array().unwrap().len(),
            0,
            "verify must pass: {result}"
        );

        let env = load_local(&store);
        assert_eq!(env.bundles.len(), 1);
        let dep = &env.bundles[0];
        assert_eq!(dep.route_binding.path_prefixes, vec!["/legal".to_string()]);
        assert_eq!(dep.route_binding.tenant_selector.tenant, "legal");
        let live = live_revision_digest(&env, dep.deployment_id).expect("live revision");
        assert_eq!(
            live,
            super::super::bundle_stage::sha256_file(&fixture()).unwrap(),
            "live revision must carry the artifact digest"
        );
        assert_eq!(env.messaging_endpoints.len(), 1);
        let ep = &env.messaging_endpoints[0];
        assert_eq!(ep.display_name, "legal-bot");
        assert_eq!(ep.provider_id, "legal-bot", "provider_id = manifest name");
        assert_eq!(
            ep.linked_bundles
                .iter()
                .map(|b| b.as_str())
                .collect::<Vec<_>>(),
            vec!["quickstart"]
        );
        let wf = ep.welcome_flow.as_ref().expect("welcome flow set");
        assert_eq!(wf.flow_id, "main");
        assert!(
            ep.webhook_secret_ref.is_some(),
            "telegram-class endpoint gets an auto-provisioned webhook secret"
        );

        // Unchanged re-apply: every step is a visible no-op, nothing re-staged.
        let revisions_before = env.revisions.len();
        let second = run_apply(&store, &manifest_path).expect("re-apply succeeds");
        assert_eq!(second.result["changed"], 0, "result: {}", second.result);
        let env = load_local(&store);
        assert_eq!(
            env.revisions.len(),
            revisions_before,
            "no-op re-apply must not stage a new revision"
        );
    }

    #[test]
    fn dry_run_mutates_nothing() {
        let (dir, store) = seeded_store();
        let manifest_path = write_manifest(dir.path(), &full_manifest(&fixture()));

        let outcome = run_dry(&store, &manifest_path).expect("dry-run succeeds");
        assert_eq!(outcome.result["mode"], "dry-run");
        assert_eq!(outcome.result["changed"], 4);
        assert!(
            outcome.result.get("verify").is_none(),
            "dry-run must not verify (nothing executed)"
        );

        let env = load_local(&store);
        assert!(env.bundles.is_empty(), "dry-run must not deploy");
        assert!(
            env.messaging_endpoints.is_empty(),
            "dry-run must not add endpoints"
        );
    }

    // --- §9 follow-up: --check (CI convergence gate) ----------------------------

    #[test]
    fn check_fails_on_pending_diff_and_mutates_nothing() {
        let (dir, store) = seeded_store();
        let manifest_path = write_manifest(dir.path(), &full_manifest(&fixture()));

        let err = run_check(&store, &manifest_path).unwrap_err();
        assert!(matches!(err, OpError::Conflict(_)), "got {err:?}");
        let msg = err.to_string();
        assert!(msg.contains("not converged"), "{msg}");
        assert!(msg.contains("4 pending change(s)"), "{msg}");

        let env = load_local(&store);
        assert!(env.bundles.is_empty(), "--check must not deploy");
        assert!(
            env.messaging_endpoints.is_empty(),
            "--check must not add endpoints"
        );
    }

    #[test]
    fn check_passes_on_converged_env() {
        let (dir, store) = seeded_store();
        let manifest_path = write_manifest(dir.path(), &full_manifest(&fixture()));
        run_apply(&store, &manifest_path).expect("apply succeeds");

        let outcome = run_check(&store, &manifest_path).expect("check passes on converged env");
        assert_eq!(outcome.result["mode"], "check");
        assert_eq!(outcome.result["changed"], 0, "result: {}", outcome.result);
        assert_eq!(outcome.result["undiffable"], 0);
        assert!(
            outcome.result.get("verify").is_none(),
            "check must not verify (nothing executed)"
        );
    }

    #[test]
    fn binding_only_change_plans_update_without_restage() {
        let (dir, store) = seeded_store();
        let manifest_path = write_manifest(dir.path(), &full_manifest(&fixture()));
        run_apply(&store, &manifest_path).expect("first apply");
        let revisions_before = load_local(&store).revisions.len();

        // Same artifact, different path prefix → exactly one update-bundle;
        // the deploy step must NOT appear (digest matches → no re-stage).
        let mut changed = full_manifest(&fixture());
        changed["bundles"][0]["route_binding"]["path_prefixes"] = json!(["/law"]);
        let manifest_path = write_manifest(dir.path(), &changed);

        let plan = run_dry(&store, &manifest_path).expect("dry-run");
        let actions = step_actions(&plan.result);
        assert!(
            actions.contains(&("update-bundle".to_string(), "update".to_string())),
            "plan: {actions:?}"
        );
        assert!(
            actions
                .iter()
                .all(|(kind, action)| kind != "deploy-bundle" || action == "no-op"),
            "digest match must not re-deploy: {actions:?}"
        );

        let outcome = run_apply(&store, &manifest_path).expect("apply binding change");
        assert_eq!(outcome.result["changed"], 1, "{}", outcome.result);
        let env = load_local(&store);
        assert_eq!(
            env.bundles[0].route_binding.path_prefixes,
            vec!["/law".to_string()]
        );
        assert_eq!(
            env.revisions.len(),
            revisions_before,
            "binding-only change must not stage a new revision"
        );
    }

    // --- G2/G3: revenue_share + status ---

    /// Minimal single-revision manifest with no binding/endpoints — the base
    /// for the metadata-reconcile tests.
    fn plain_bundle_manifest() -> Value {
        json!({
            "schema": ENV_MANIFEST_SCHEMA_V1,
            "environment": {"id": "local"},
            "bundles": [{"bundle_id": "quickstart", "bundle_path": fixture()}]
        })
    }

    #[test]
    fn status_reconciles_against_existing_deployment() {
        let (dir, store) = seeded_store();
        let base = plain_bundle_manifest();
        let base_path = write_manifest(dir.path(), &base);
        run_apply(&store, &base_path).expect("first apply");
        assert_eq!(
            load_local(&store).bundles[0].status,
            BundleDeploymentStatus::Active
        );

        // Re-apply with status: paused → one update-bundle, no re-stage.
        let mut paused = base.clone();
        paused["bundles"][0]["status"] = json!("paused");
        let paused_path = write_manifest(dir.path(), &paused);
        let plan = run_dry(&store, &paused_path).expect("dry-run");
        let actions = step_actions(&plan.result);
        assert!(
            actions.contains(&("update-bundle".to_string(), "update".to_string())),
            "status change must plan an update-bundle: {actions:?}"
        );
        assert!(
            actions
                .iter()
                .all(|(k, a)| k != "deploy-bundle" || a == "no-op"),
            "status change must not re-stage: {actions:?}"
        );

        run_apply(&store, &paused_path).expect("apply pause");
        assert_eq!(
            load_local(&store).bundles[0].status,
            BundleDeploymentStatus::Paused
        );

        // Idempotent: re-applying the paused manifest changes nothing.
        let plan2 = run_dry(&store, &paused_path).expect("dry-run 2");
        assert_eq!(plan2.result["changed"], 0, "{}", plan2.result);
    }

    #[test]
    fn revenue_share_reconciles_against_existing_deployment() {
        let (dir, store) = seeded_store();
        let base = plain_bundle_manifest();
        let base_path = write_manifest(dir.path(), &base);
        run_apply(&store, &base_path).expect("first apply");
        // Default split = greentic@10000.
        assert_eq!(load_local(&store).bundles[0].revenue_share.len(), 1);

        let mut split = base.clone();
        split["bundles"][0]["revenue_share"] = json!([
            {"party_id": "greentic", "basis_points": 7000},
            {"party_id": "partner", "basis_points": 3000}
        ]);
        let split_path = write_manifest(dir.path(), &split);
        let plan = run_dry(&store, &split_path).expect("dry-run");
        assert!(
            step_actions(&plan.result)
                .contains(&("update-bundle".to_string(), "update".to_string())),
            "revenue_share change must plan an update-bundle"
        );

        run_apply(&store, &split_path).expect("apply revenue split");
        let env = load_local(&store);
        let shares = &env.bundles[0].revenue_share;
        assert_eq!(shares.len(), 2);
        assert_eq!(shares[0].party_id.as_str(), "greentic");
        assert_eq!(shares[0].basis_points, 7000);
        assert_eq!(shares[1].party_id.as_str(), "partner");
        assert_eq!(shares[1].basis_points, 3000);

        // Idempotent.
        let plan2 = run_dry(&store, &split_path).expect("dry-run 2");
        assert_eq!(plan2.result["changed"], 0, "{}", plan2.result);
    }

    #[test]
    fn revenue_share_threaded_into_fresh_deploy_single_apply() {
        let (dir, store) = seeded_store();
        let mut manifest = plain_bundle_manifest();
        manifest["bundles"][0]["revenue_share"] = json!([
            {"party_id": "greentic", "basis_points": 6000},
            {"party_id": "partner", "basis_points": 4000}
        ]);
        let p = write_manifest(dir.path(), &manifest);
        // A SINGLE apply must converge — verify asserts revenue_share, so this
        // proves it is threaded into the create path (not deferred).
        run_apply(&store, &p).expect("fresh apply with revenue_share must verify");
        let shares = &load_local(&store).bundles[0].revenue_share;
        assert_eq!(shares.len(), 2);
        assert_eq!(shares[1].party_id.as_str(), "partner");
        assert_eq!(shares[1].basis_points, 4000);
        assert_eq!(run_dry(&store, &p).expect("dry").result["changed"], 0);
    }

    #[test]
    fn status_on_fresh_create_defers_one_apply_without_failing_verify() {
        let (dir, store) = seeded_store();
        let mut manifest = plain_bundle_manifest();
        manifest["bundles"][0]["status"] = json!("paused");
        let p = write_manifest(dir.path(), &manifest);
        // First apply creates the deployment (born `active`); verify must NOT
        // fail for the not-yet-applied `paused` status (reconcile-only).
        run_apply(&store, &p).expect("fresh apply with status must not fail verify");
        assert_eq!(
            load_local(&store).bundles[0].status,
            BundleDeploymentStatus::Active
        );
        // Second apply: the deployment now pre-dates the apply → status
        // reconciles to paused, single-apply from here.
        run_apply(&store, &p).expect("second apply pauses");
        assert_eq!(
            load_local(&store).bundles[0].status,
            BundleDeploymentStatus::Paused
        );
    }

    #[test]
    fn degenerate_live_digest_fails_toward_redeploy() {
        let (dir, store) = seeded_store();
        // Seed a pre-digest deployment by hand: live revision carries the
        // `sha256:00` placeholder.
        let mut env = make_env("local");
        let dep = make_bundle_deployment("local", "quickstart");
        let rev = make_revision(
            "local",
            "quickstart",
            &dep.deployment_id,
            1,
            RevisionLifecycle::Ready,
        );
        env.traffic_splits.push(make_traffic_split(
            "local",
            "quickstart",
            &dep.deployment_id,
            &rev.revision_id,
            "seed",
        ));
        env.bundles.push(dep);
        env.revisions.push(rev);
        store.save(&env).unwrap();

        // No route_binding in the manifest → the only pending step must be
        // the re-deploy (unknown digest fails toward deploy, never toward skip).
        let manifest = json!({
            "schema": ENV_MANIFEST_SCHEMA_V1,
            "environment": {"id": "local"},
            "bundles": [{"bundle_id": "quickstart", "bundle_path": fixture()}]
        });
        let manifest_path = write_manifest(dir.path(), &manifest);
        let plan = run_dry(&store, &manifest_path).expect("dry-run");
        let actions = step_actions(&plan.result);
        assert!(
            actions.contains(&("deploy-bundle".to_string(), "update".to_string())),
            "degenerate digest must plan a re-deploy: {actions:?}"
        );
        assert_eq!(plan.result["changed"], 1, "{}", plan.result);
    }

    #[test]
    fn endpoint_ambiguity_is_an_error() {
        let (dir, store) = seeded_store();
        // Two endpoints with the same (provider_type, display_name) pair but
        // distinct provider_ids — legal at the store level, ambiguous for the
        // manifest's natural key.
        for provider_id in ["bot-a", "bot-b"] {
            super::super::messaging::add(
                &store,
                &OpFlags::default(),
                Some(EndpointAddPayload {
                    environment_id: "local".to_string(),
                    provider_id: provider_id.to_string(),
                    provider_type: "messaging.telegram.bot".to_string(),
                    display_name: "legal-bot".to_string(),
                    secret_refs: Vec::new(),
                    idempotency_key: None,
                    updated_by: "test".to_string(),
                }),
            )
            .expect("seed endpoint");
        }
        let manifest = json!({
            "schema": ENV_MANIFEST_SCHEMA_V1,
            "environment": {"id": "local"},
            "messaging_endpoints": [
                {"name": "legal-bot", "provider_type": "messaging.telegram.bot"}
            ]
        });
        let manifest_path = write_manifest(dir.path(), &manifest);
        let err = run_apply(&store, &manifest_path).unwrap_err();
        match err {
            OpError::Conflict(msg) => {
                assert!(msg.contains("refuses to guess"), "got: {msg}")
            }
            other => panic!("expected Conflict, got {other:?}"),
        }
    }

    #[test]
    fn provider_type_mismatch_on_matched_name_is_an_error() {
        let (dir, store) = seeded_store();
        super::super::messaging::add(
            &store,
            &OpFlags::default(),
            Some(EndpointAddPayload {
                environment_id: "local".to_string(),
                provider_id: "legal-bot".to_string(),
                provider_type: "messaging.teams.bot".to_string(),
                display_name: "legal-bot".to_string(),
                secret_refs: Vec::new(),
                idempotency_key: None,
                updated_by: "test".to_string(),
            }),
        )
        .expect("seed endpoint");
        let manifest = json!({
            "schema": ENV_MANIFEST_SCHEMA_V1,
            "environment": {"id": "local"},
            "messaging_endpoints": [
                {"name": "legal-bot", "provider_type": "messaging.telegram.bot"}
            ]
        });
        let manifest_path = write_manifest(dir.path(), &manifest);
        let err = run_apply(&store, &manifest_path).unwrap_err();
        match err {
            OpError::Conflict(msg) => assert!(msg.contains("repurpose"), "got: {msg}"),
            other => panic!("expected Conflict, got {other:?}"),
        }
    }

    #[test]
    fn link_to_unknown_bundle_is_an_error() {
        let (dir, store) = seeded_store();
        let manifest = json!({
            "schema": ENV_MANIFEST_SCHEMA_V1,
            "environment": {"id": "local"},
            "messaging_endpoints": [{
                "name": "legal-bot",
                "provider_type": "messaging.telegram.bot",
                "links": ["ghost"]
            }]
        });
        let manifest_path = write_manifest(dir.path(), &manifest);
        let err = run_apply(&store, &manifest_path).unwrap_err();
        match err {
            OpError::InvalidArgument(msg) => assert!(msg.contains("ghost"), "got: {msg}"),
            other => panic!("expected InvalidArgument, got {other:?}"),
        }
        // Validation failures must not mutate anything.
        assert!(load_local(&store).messaging_endpoints.is_empty());
    }

    #[test]
    fn partial_failure_resumes_on_reapply() {
        let (dir, store) = seeded_store();
        // Second bundle's artifact is garbage — validation passes (it IS a
        // file with a digest), execution fails at the stage step.
        let broken = dir.path().join("broken.gtbundle");
        std::fs::write(&broken, b"not a squashfs").unwrap();
        let manifest = json!({
            "schema": ENV_MANIFEST_SCHEMA_V1,
            "environment": {"id": "local"},
            "bundles": [
                {"bundle_id": "quickstart", "bundle_path": fixture()},
                {"bundle_id": "broken", "bundle_path": broken}
            ]
        });
        let manifest_path = write_manifest(dir.path(), &manifest);
        run_apply(&store, &manifest_path).expect_err("garbage artifact must fail the apply");

        // Fail-fast left the store valid — the store is the checkpoint. The
        // first bundle is fully routed; the broken one failed at the stage
        // step AFTER its `bundles add` landed, so its deployment record
        // exists but carries no live traffic split.
        let env = load_local(&store);
        assert_eq!(env.bundles.len(), 2, "both deployment records exist");
        assert_eq!(
            env.traffic_splits.len(),
            1,
            "only the first bundle is routed"
        );
        assert_eq!(
            env.traffic_splits[0].bundle_id.as_str(),
            "quickstart",
            "the routed split belongs to the bundle that staged successfully"
        );

        // Fix the manifest (point `broken` at the real artifact) and re-run:
        // the completed bundle no-ops, only the remainder executes.
        let fixed = json!({
            "schema": ENV_MANIFEST_SCHEMA_V1,
            "environment": {"id": "local"},
            "bundles": [
                {"bundle_id": "quickstart", "bundle_path": fixture()},
                {"bundle_id": "broken", "bundle_path": fixture()}
            ]
        });
        let manifest_path = write_manifest(dir.path(), &fixed);
        let outcome = run_apply(&store, &manifest_path).expect("resume succeeds");
        assert_eq!(outcome.result["changed"], 1, "{}", outcome.result);
        let env = load_local(&store);
        assert_eq!(env.bundles.len(), 2);
    }

    #[test]
    fn nonexistent_nonlocal_env_gives_clear_error() {
        // `env apply` can reconcile an existing non-local env, but creating one
        // is reserved for the remote operator store (A7) — a non-existent
        // non-local env must fail at plan time with a clear message.
        let dir = tempdir().unwrap();
        let store = LocalFsStore::new(dir.path());
        let manifest = json!({
            "schema": ENV_MANIFEST_SCHEMA_V1,
            "environment": {
                "id": "prod",
                "name": "Production",
                "region": "us-east-1",
                "tenant_org_id": "org-42",
            }
        });
        let manifest_path = write_manifest(dir.path(), &manifest);
        let err = run_apply(&store, &manifest_path).unwrap_err();
        match err {
            OpError::NotFound(msg) => {
                assert!(
                    msg.contains("cannot create one locally") && msg.contains("operator store"),
                    "got: {msg}"
                );
            }
            other => panic!("expected NotFound, got {other:?}"),
        }
    }

    #[test]
    fn link_satisfied_only_by_env_warns() {
        let (dir, store) = seeded_store();
        // Deploy a bundle imperatively, then link to it from a manifest that
        // does not declare it: a warning, not an error (layered manifests).
        super::super::deploy::deploy(
            &store,
            &OpFlags::default(),
            Some(BundleDeployPayload {
                environment_id: "local".to_string(),
                bundle_id: "preexisting".to_string(),
                customer_id: None,
                bundle_path: Some(fixture()),
                idempotency_key: None,
                config_overrides: None,
                route_binding: None,
                revenue_share: None,
            }),
        )
        .expect("imperative deploy");
        let manifest = json!({
            "schema": ENV_MANIFEST_SCHEMA_V1,
            "environment": {"id": "local"},
            "messaging_endpoints": [{
                "name": "legal-bot",
                "provider_type": "messaging.telegram.bot",
                "links": ["preexisting"]
            }]
        });
        let manifest_path = write_manifest(dir.path(), &manifest);
        let outcome = run_apply(&store, &manifest_path).expect("apply succeeds with warning");
        let warnings = outcome.result["warnings"].as_array().unwrap();
        assert!(
            warnings
                .iter()
                .any(|w| w.as_str().unwrap().contains("pre-existing")),
            "warnings: {warnings:?}"
        );
        let env = load_local(&store);
        assert_eq!(env.messaging_endpoints[0].linked_bundles.len(), 1);
    }

    // --- Fix 1: customer-aware bundle matching ---

    #[test]
    fn cross_customer_deployment_is_rejected() {
        let (dir, store) = seeded_store();
        // Seed a deployment owned by "other-customer".
        let mut env = make_env("local");
        let mut dep = make_bundle_deployment("local", "quickstart");
        dep.customer_id = CustomerId::new("other-customer");
        env.bundles.push(dep);
        store.save(&env).unwrap();

        // Manifest declares the same bundle_id but resolves to the default
        // customer "local-dev" → Conflict.
        let manifest = json!({
            "schema": ENV_MANIFEST_SCHEMA_V1,
            "environment": {"id": "local"},
            "bundles": [{"bundle_id": "quickstart", "bundle_path": fixture()}]
        });
        let manifest_path = write_manifest(dir.path(), &manifest);
        let err = run_apply(&store, &manifest_path).unwrap_err();
        match err {
            OpError::Conflict(msg) => {
                assert!(msg.contains("other-customer"), "got: {msg}");
                assert!(msg.contains("local-dev"), "got: {msg}");
            }
            other => panic!("expected Conflict, got {other:?}"),
        }
        // No mutation.
        let env = load_local(&store);
        assert!(env.revisions.is_empty());
    }

    // --- Fix 2: pre-deploy artifact re-hash ---

    #[test]
    fn ensure_artifact_unchanged_catches_modification() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("test.bin");
        std::fs::write(&path, b"original content").unwrap();
        let digest = super::super::bundle_stage::sha256_file(&path).unwrap();

        // Same bytes → Ok.
        ensure_artifact_unchanged(&path, &digest).expect("unchanged file must pass");

        // Mutate → Err(Conflict).
        std::fs::write(&path, b"tampered content").unwrap();
        let err = ensure_artifact_unchanged(&path, &digest).unwrap_err();
        match err {
            OpError::Conflict(msg) => {
                assert!(msg.contains("changed since the plan"), "got: {msg}")
            }
            other => panic!("expected Conflict, got {other:?}"),
        }
    }

    // --- Fix 3: strict convergence check ---

    #[test]
    fn mixed_split_is_not_converged_plans_redeploy() {
        use greentic_deploy_spec::TrafficSplitEntry;

        let (dir, store) = seeded_store();
        let real_digest = super::super::bundle_stage::sha256_file(&fixture()).unwrap();

        let mut env = make_env("local");
        let dep = make_bundle_deployment("local", "quickstart");
        let mut rev1 = make_revision(
            "local",
            "quickstart",
            &dep.deployment_id,
            1,
            RevisionLifecycle::Ready,
        );
        rev1.bundle_digest = real_digest;
        let rev2 = make_revision(
            "local",
            "quickstart",
            &dep.deployment_id,
            2,
            RevisionLifecycle::Ready,
        );
        // Two-entry split: 6000/4000 — sum = 10_000 (valid).
        let mut split = make_traffic_split(
            "local",
            "quickstart",
            &dep.deployment_id,
            &rev1.revision_id,
            "seed",
        );
        split.entries[0].weight_bps = 6_000;
        split.entries.push(TrafficSplitEntry {
            revision_id: rev2.revision_id,
            weight_bps: 4_000,
        });
        env.bundles.push(dep);
        env.revisions.push(rev1);
        env.revisions.push(rev2);
        env.traffic_splits.push(split);
        store.save(&env).unwrap();

        // Manifest: same bundle, same artifact (top entry digest matches) —
        // but the split is mixed, so it MUST plan a re-deploy, not a no-op.
        let manifest = json!({
            "schema": ENV_MANIFEST_SCHEMA_V1,
            "environment": {"id": "local"},
            "bundles": [{"bundle_id": "quickstart", "bundle_path": fixture()}]
        });
        let manifest_path = write_manifest(dir.path(), &manifest);
        let plan = run_dry(&store, &manifest_path).expect("dry-run");
        let actions = step_actions(&plan.result);
        assert!(
            actions.contains(&("deploy-bundle".to_string(), "update".to_string())),
            "mixed split must plan a re-deploy: {actions:?}"
        );
        assert!(plan.result["changed"].as_u64().unwrap() >= 1);
    }

    // --- split_converged is multiset equality, not set containment ---

    /// Build a `ResolvedRevision` with the given digest + weight (the only
    /// two fields `split_converged` reads).
    fn resolved_rev(name: &str, digest: &str, weight_bps: u32) -> ResolvedRevision {
        ResolvedRevision {
            spec: ManifestRevision {
                name: name.to_string(),
                bundle_path: PathBuf::from(format!("{name}.gtbundle")),
                weight_percent: None,
                weight_bps: Some(weight_bps),
                drain_seconds: None,
                abort_metrics: Vec::new(),
            },
            resolved_path: PathBuf::from(format!("{name}.gtbundle")),
            digest: digest.to_string(),
            weight_bps,
        }
    }

    /// Seed an env with a two-entry split over two revisions, returning the
    /// env and its deployment id. Each `(digest, weight_bps)` is applied to a
    /// distinct revision.
    fn env_with_two_entry_split(live: [(&str, u32); 2]) -> (Environment, DeploymentId) {
        use greentic_deploy_spec::TrafficSplitEntry;
        let mut env = make_env("local");
        let dep = make_bundle_deployment("local", "quickstart");
        let dep_id = dep.deployment_id;
        let mut rev1 = make_revision("local", "quickstart", &dep_id, 1, RevisionLifecycle::Ready);
        rev1.bundle_digest = live[0].0.to_string();
        let mut rev2 = make_revision("local", "quickstart", &dep_id, 2, RevisionLifecycle::Ready);
        rev2.bundle_digest = live[1].0.to_string();
        let mut split =
            make_traffic_split("local", "quickstart", &dep_id, &rev1.revision_id, "seed");
        split.entries[0].weight_bps = live[0].1;
        split.entries.push(TrafficSplitEntry {
            revision_id: rev2.revision_id,
            weight_bps: live[1].1,
        });
        env.bundles.push(dep);
        env.revisions.push(rev1);
        env.revisions.push(rev2);
        env.traffic_splits.push(split);
        (env, dep_id)
    }

    #[test]
    fn split_converged_matches_exact_multiset() {
        let (env, dep_id) =
            env_with_two_entry_split([("sha256:aaaa11", 5_000), ("sha256:bbbb22", 5_000)]);
        let expected = [
            resolved_rev("a", "sha256:aaaa11", 5_000),
            resolved_rev("b", "sha256:bbbb22", 5_000),
        ];
        assert!(
            split_converged(&env, dep_id, &expected),
            "identical (digest, weight) bag must converge"
        );
        // Order-independence: same bag, swapped expected order.
        let swapped = [
            resolved_rev("b", "sha256:bbbb22", 5_000),
            resolved_rev("a", "sha256:aaaa11", 5_000),
        ];
        assert!(split_converged(&env, dep_id, &swapped));
    }

    #[test]
    fn split_converged_rejects_duplicate_digest_mismatch() {
        // Live bag has TWO distinct digests; expected names the SAME digest
        // twice. A set-containment check (the old `.any()`) would falsely
        // report convergence; a multiset check must reject it — otherwise the
        // desired split is silently never applied.
        let (env, dep_id) =
            env_with_two_entry_split([("sha256:aaaa11", 5_000), ("sha256:bbbb22", 5_000)]);
        let expected = [
            resolved_rev("a", "sha256:aaaa11", 5_000),
            resolved_rev("a-dup", "sha256:aaaa11", 5_000),
        ];
        assert!(
            !split_converged(&env, dep_id, &expected),
            "duplicate-digest expected bag must NOT match a two-distinct-digest live split"
        );
    }

    #[test]
    fn split_converged_rejects_weight_mismatch() {
        let (env, dep_id) =
            env_with_two_entry_split([("sha256:aaaa11", 6_000), ("sha256:bbbb22", 4_000)]);
        let expected = [
            resolved_rev("a", "sha256:aaaa11", 5_000),
            resolved_rev("b", "sha256:bbbb22", 5_000),
        ];
        assert!(
            !split_converged(&env, dep_id, &expected),
            "same digests but different weights must not converge"
        );
    }

    #[test]
    fn split_converged_rejects_placeholder_digest() {
        // A live revision still carrying the staging placeholder digest is
        // not a real deploy — convergence must fail.
        let (env, dep_id) =
            env_with_two_entry_split([("sha256:00", 5_000), ("sha256:bbbb22", 5_000)]);
        let expected = [
            resolved_rev("a", "sha256:00", 5_000),
            resolved_rev("b", "sha256:bbbb22", 5_000),
        ];
        assert!(
            !split_converged(&env, dep_id, &expected),
            "placeholder live digest must not be treated as converged"
        );
    }

    // --- Fix 4: deploy BEFORE binding update ---

    #[test]
    fn deploy_precedes_binding_update_when_both_differ() {
        let (dir, store) = seeded_store();
        // Seed: deployment with degenerate digest (needs deploy) + binding
        // that differs from the manifest's.
        let mut env = make_env("local");
        let dep = make_bundle_deployment("local", "quickstart");
        let rev = make_revision(
            "local",
            "quickstart",
            &dep.deployment_id,
            1,
            RevisionLifecycle::Ready,
        );
        env.traffic_splits.push(make_traffic_split(
            "local",
            "quickstart",
            &dep.deployment_id,
            &rev.revision_id,
            "seed",
        ));
        env.bundles.push(dep);
        env.revisions.push(rev);
        store.save(&env).unwrap();

        // Manifest with the fixture path AND a different binding (/legal).
        let manifest = full_manifest(&fixture());
        let manifest_path = write_manifest(dir.path(), &manifest);
        let plan = run_dry(&store, &manifest_path).expect("dry-run");
        let steps = plan.result["steps"].as_array().expect("steps");

        // Find indices of deploy-bundle (update) and update-bundle (update).
        let deploy_idx = steps
            .iter()
            .position(|s| s["kind"] == "deploy-bundle" && s["action"] == "update")
            .expect("deploy-bundle update step");
        let update_idx = steps
            .iter()
            .position(|s| s["kind"] == "update-bundle" && s["action"] == "update")
            .expect("update-bundle update step");

        assert!(
            deploy_idx < update_idx,
            "deploy-bundle (idx {deploy_idx}) must precede update-bundle (idx {update_idx}): \
             {steps:?}"
        );
    }

    // --- PR-2: secrets[] -------------------------------------------------------

    /// [`seeded_store`] plus the default dev-store secrets binding `env init`
    /// would create (`make_env` binds no env-packs).
    fn seeded_store_with_dev_secrets() -> (tempfile::TempDir, LocalFsStore) {
        let (dir, store) = seeded_store();
        let mut env = load_local(&store);
        env.packs.push(make_binding(
            CapabilitySlot::Secrets,
            "greentic.secrets.dev-store@1.0.0",
        ));
        store.save(&env).unwrap();
        (dir, store)
    }

    fn run_with_lookup(
        store: &LocalFsStore,
        manifest_path: &Path,
        mode: ApplyMode,
        lookup: &dyn Fn(&str) -> Option<String>,
    ) -> Result<OpOutcome, OpError> {
        run_with_lookup_and_prompter(store, manifest_path, mode, lookup, None)
    }

    fn run_with_lookup_and_prompter(
        store: &LocalFsStore,
        manifest_path: &Path,
        mode: ApplyMode,
        lookup: &dyn Fn(&str) -> Option<String>,
        prompter: Option<&SecretPrompter>,
    ) -> Result<OpOutcome, OpError> {
        let flags = OpFlags {
            schema_only: false,
            answers: Some(manifest_path.to_path_buf()),
        };
        apply_with_lookups(
            store,
            &flags,
            ApplyOptions {
                mode,
                ..ApplyOptions::default()
            },
            lookup,
            prompter,
        )
    }

    use crate::cli::tests_common::dev_store_read;

    const SECRET_PATH: &str = "legal/_/messaging-telegram/telegram_bot_token";

    fn secrets_manifest(var: &str) -> Value {
        json!({
            "schema": ENV_MANIFEST_SCHEMA_V1,
            "environment": {"id": "local"},
            "secrets": [{"path": SECRET_PATH, "from_env": var}]
        })
    }

    #[test]
    fn secrets_e2e_put_writes_value_and_redacts() {
        let (dir, store) = seeded_store_with_dev_secrets();
        let manifest_path = write_manifest(dir.path(), &secrets_manifest("APPLY_LEGAL_BOT_TOKEN"));
        let lookup =
            |name: &str| (name == "APPLY_LEGAL_BOT_TOKEN").then(|| "tok-secret-9000".to_string());

        let outcome = run_with_lookup(&store, &manifest_path, ApplyMode::Apply, &lookup)
            .expect("apply succeeds");

        // The report names the env VAR and says `put` — never the value.
        let envelope = serde_json::to_string(&outcome).unwrap();
        assert!(
            !envelope.contains("tok-secret-9000"),
            "envelope must not leak the value: {envelope}"
        );
        assert!(envelope.contains("APPLY_LEGAL_BOT_TOKEN"), "{envelope}");
        let actions = step_actions(&outcome.result);
        assert!(
            actions.contains(&("put-secret".to_string(), "put".to_string())),
            "{actions:?}"
        );

        // Written through to the dev store the runtime reader resolves.
        let store_path = dir
            .path()
            .join("local")
            .join(super::super::secrets::DEV_STORE_RELATIVE);
        let bytes = dev_store_read(&store_path, &format!("secrets://local/{SECRET_PATH}"));
        assert_eq!(bytes, b"tok-secret-9000".to_vec());

        // The audit event carries a fresh (not deterministic) key and no value.
        let audit_path = dir.path().join("local/audit/events.jsonl");
        let put_ikeys = || -> Vec<String> {
            std::fs::read_to_string(&audit_path)
                .unwrap()
                .lines()
                .filter_map(|l| serde_json::from_str::<serde_json::Value>(l).ok())
                .filter(|e| e["noun"] == "secrets" && e["verb"] == "put")
                .map(|e| e["idempotency_key"].as_str().expect("ikey").to_string())
                .collect()
        };
        let audit = std::fs::read_to_string(&audit_path).unwrap();
        assert!(
            !audit.contains("tok-secret-9000"),
            "audit log must not leak the value"
        );
        assert_eq!(put_ikeys().len(), 1, "one put audit event: {audit}");

        // Always-put: a re-apply is NOT a no-op for the secret (cannot diff).
        let second =
            run_with_lookup(&store, &manifest_path, ApplyMode::Apply, &lookup).expect("re-apply");
        assert_eq!(second.result["changed"], 1, "{}", second.result);

        // Two invocations must mint DIFFERENT idempotency keys (fresh per
        // invocation — the contract after removing deterministic keys from
        // put-secret steps).
        let ikeys = put_ikeys();
        assert_eq!(ikeys.len(), 2, "two put events after re-apply: {ikeys:?}");
        assert_ne!(
            ikeys[0], ikeys[1],
            "two invocations must mint different keys"
        );
    }

    #[test]
    fn missing_or_empty_secret_env_var_fails_apply_before_mutation() {
        let (dir, store) = seeded_store_with_dev_secrets();
        let manifest_path = write_manifest(dir.path(), &secrets_manifest("APPLY_MISSING_VAR"));
        for value in [None, Some(String::new())] {
            let lookup = |_: &str| value.clone();
            let err =
                run_with_lookup(&store, &manifest_path, ApplyMode::Apply, &lookup).unwrap_err();
            match err {
                OpError::InvalidArgument(msg) => {
                    assert!(msg.contains("1 missing input(s)"), "got: {msg}");
                    assert!(msg.contains("APPLY_MISSING_VAR"), "got: {msg}");
                    assert!(msg.contains(SECRET_PATH), "got: {msg}");
                }
                other => panic!("expected InvalidArgument, got {other:?}"),
            }
        }
        // The missing gate fires before execute: nothing written.
        assert!(
            !dir.path()
                .join("local")
                .join(super::super::secrets::DEV_STORE_RELATIVE)
                .exists()
        );
        assert!(!dir.path().join("local/audit/events.jsonl").exists());
    }

    #[test]
    fn non_dev_store_backend_fails_at_validation() {
        let (dir, store) = seeded_store();
        let manifest_path = write_manifest(dir.path(), &secrets_manifest("APPLY_VAR"));
        let lookup = |_: &str| Some("v".to_string());

        // Existing env with NO secrets pack bound → the shared precondition
        // fires at validation time.
        let err = run_with_lookup(&store, &manifest_path, ApplyMode::Apply, &lookup).unwrap_err();
        assert!(matches!(err, OpError::Conflict(_)), "got {err:?}");

        // Non-dev-store backend → not-yet-implemented at validation time.
        let mut env = load_local(&store);
        env.packs.push(make_binding(
            CapabilitySlot::Secrets,
            "greentic.secrets.aws-sm@1.0.0",
        ));
        store.save(&env).unwrap();
        let err = run_with_lookup(&store, &manifest_path, ApplyMode::Apply, &lookup).unwrap_err();
        assert!(matches!(err, OpError::NotYetImplemented(_)), "got {err:?}");

        // Both failures pre-date execution: no audit events were appended.
        assert!(!dir.path().join("local/audit/events.jsonl").exists());
    }

    #[test]
    fn secrets_plan_orders_before_bundles_and_dry_run_writes_nothing() {
        let (dir, store) = seeded_store_with_dev_secrets();
        let mut manifest = full_manifest(&fixture());
        manifest["secrets"] = json!([{"path": SECRET_PATH, "from_env": "APPLY_ORDER_TOKEN"}]);
        let manifest_path = write_manifest(dir.path(), &manifest);
        let lookup = |_: &str| Some("tok-order".to_string());

        let plan =
            run_with_lookup(&store, &manifest_path, ApplyMode::DryRun, &lookup).expect("dry-run");
        let steps = plan.result["steps"].as_array().expect("steps");
        let pos = |kind: &str| {
            steps
                .iter()
                .position(|s| s["kind"] == kind)
                .unwrap_or_else(|| panic!("no `{kind}` step in {steps:?}"))
        };
        assert!(pos("ensure-environment") < pos("put-secret"));
        assert!(
            pos("put-secret") < pos("deploy-bundle"),
            "secrets must land before bundles so a fresh revision never \
             resolves a missing secret"
        );

        // Dry-run resolved the value but wrote nothing.
        assert!(
            !dir.path()
                .join("local")
                .join(super::super::secrets::DEV_STORE_RELATIVE)
                .exists(),
            "dry-run must not write the dev store"
        );
    }

    // `SecretValue`'s redacting-Debug property is pinned by its owning
    // module's test (`runtime_secrets::tests::secret_value_debug_is_redacted`).

    #[test]
    fn check_excludes_undiffable_secret_puts_but_counts_real_drift() {
        let (dir, store) = seeded_store_with_dev_secrets();
        let lookup =
            |name: &str| (name == "APPLY_LEGAL_BOT_TOKEN").then(|| "tok-secret-9000".to_string());

        // Converged env + a manifest whose only non-no-op row is the
        // always-put secret: excluded from the verdict — check passes,
        // reports the row as undiffable, and writes nothing.
        let manifest_path = write_manifest(dir.path(), &secrets_manifest("APPLY_LEGAL_BOT_TOKEN"));
        let outcome = run_with_lookup(&store, &manifest_path, ApplyMode::Check, &lookup)
            .expect("check passes");
        assert_eq!(outcome.result["mode"], "check");
        assert_eq!(outcome.result["undiffable"], 1, "{}", outcome.result);
        assert_eq!(
            outcome.result["changed"], 1,
            "the put row stays visible in the report: {}",
            outcome.result
        );
        assert!(
            !dir.path()
                .join("local")
                .join(super::super::secrets::DEV_STORE_RELATIVE)
                .exists(),
            "--check must not write the dev store"
        );

        // Mixed manifest: a pending bundle deploy is real drift — check
        // fails, and the count excludes the undiffable put row.
        let mut mixed = full_manifest(&fixture());
        mixed["secrets"] = json!([{"path": SECRET_PATH, "from_env": "APPLY_LEGAL_BOT_TOKEN"}]);
        let mixed_path = write_manifest(dir.path(), &mixed);
        let err = run_with_lookup(&store, &mixed_path, ApplyMode::Check, &lookup).unwrap_err();
        assert!(matches!(err, OpError::Conflict(_)), "got {err:?}");
        assert!(
            err.to_string().contains("4 pending change(s)"),
            "put row must not count toward drift: {err}"
        );
    }

    // --- Adversarial-review fix 1: pre-mutation artifact re-hash gate ---

    #[test]
    fn tampered_artifact_aborts_before_any_secret_write() {
        // Between validation (digests computed) and execute, the
        // confirmation pause is unbounded. If an artifact changes during
        // the pause, the pre-mutation gate at the top of execute() must
        // abort the whole apply before ANY step (including put-secret)
        // mutates the store.
        let (dir, store) = seeded_store_with_dev_secrets();

        // Copy the real bundle to a temp file we can tamper.
        let tamper_bundle = dir.path().join("tamper.gtbundle");
        std::fs::copy(fixture(), &tamper_bundle).unwrap();

        let manifest = json!({
            "schema": ENV_MANIFEST_SCHEMA_V1,
            "environment": {"id": "local"},
            "bundles": [{"bundle_id": "quickstart", "bundle_path": &tamper_bundle}],
            "secrets": [{"path": SECRET_PATH, "from_env": "APPLY_TAMPER_TOKEN"}]
        });
        let manifest_path = write_manifest(dir.path(), &manifest);
        let lookup = |name: &str| (name == "APPLY_TAMPER_TOKEN").then(|| "tok-tamper".to_string());

        // Run the validation + diff pipeline directly (mirroring apply's
        // flow), then tamper the artifact, then call execute.
        let loaded: EnvManifest = super::super::load_answers(&manifest_path).unwrap();
        loaded.validate_shape().unwrap();
        let manifest_dir = manifest_path.parent().unwrap().to_path_buf();
        let ctx = resolve_and_validate(
            &store,
            loaded,
            &manifest_dir,
            "test".to_string(),
            &lookup,
            None,
            &BTreeMap::new(),
        )
        .unwrap();
        let steps = diff(&store, &ctx).unwrap();

        // Tamper the bundle AFTER diff (simulating a change during the
        // confirmation pause).
        std::fs::write(&tamper_bundle, b"tampered bytes").unwrap();

        let err = execute(&store, &ctx, &steps).expect_err("tampered artifact must abort");
        assert!(
            matches!(&err, OpError::Conflict(msg) if msg.contains("changed since the plan")),
            "expected Conflict about artifact change, got: {err:?}"
        );

        // The dev-store file must NOT exist — no secret was written.
        assert!(
            !dir.path()
                .join("local")
                .join(super::super::secrets::DEV_STORE_RELATIVE)
                .exists(),
            "tampered artifact must abort before any secret write"
        );
    }

    // --- operator-surface PR-1: missing-inputs contract -------------------------

    #[test]
    fn missing_inputs_accumulate_across_secrets_and_bundles() {
        let (dir, store) = seeded_store_with_dev_secrets();
        let absent = dir.path().join("ghost.gtbundle");
        let manifest = json!({
            "schema": ENV_MANIFEST_SCHEMA_V1,
            "environment": {"id": "local"},
            "secrets": [
                {"path": "legal/_/messaging-telegram/telegram_bot_token",
                 "from_env": "APPLY_VAR_A"},
                {"path": "accounting/_/messaging-telegram/telegram_bot_token",
                 "from_env": "APPLY_VAR_B"}
            ],
            "bundles": [{"bundle_id": "ghost", "bundle_path": absent}]
        });
        let manifest_path = write_manifest(dir.path(), &manifest);
        let lookup = |_: &str| None;

        // Dry-run: ALL THREE gaps in one report, stable order (secrets in
        // manifest order, then bundles), exit 0.
        let plan = run_with_lookup(&store, &manifest_path, ApplyMode::DryRun, &lookup)
            .expect("dry-run never fails on missing inputs");
        let rows: Vec<(String, String, String)> = plan.result["missing"]
            .as_array()
            .expect("missing array")
            .iter()
            .map(|m| {
                (
                    m["kind"].as_str().unwrap().to_string(),
                    m["key"].as_str().unwrap().to_string(),
                    m["source"].as_str().unwrap().to_string(),
                )
            })
            .collect();
        assert_eq!(rows.len(), 3, "{rows:?}");
        assert_eq!(
            (rows[0].0.as_str(), rows[0].2.as_str()),
            ("secret_value", "env:APPLY_VAR_A")
        );
        assert_eq!(rows[0].1, "legal/_/messaging-telegram/telegram_bot_token");
        assert_eq!(
            (rows[1].0.as_str(), rows[1].2.as_str()),
            ("secret_value", "env:APPLY_VAR_B")
        );
        assert_eq!(rows[2].0, "bundle_artifact");
        assert_eq!(rows[2].1, "ghost");
        assert!(rows[2].2.starts_with("path:"), "{rows:?}");

        // Apply: the gate names all three gaps in ONE error, pre-mutation.
        let err = run_with_lookup(&store, &manifest_path, ApplyMode::Apply, &lookup).unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("3 missing input(s)"), "{msg}");
        assert!(msg.contains("APPLY_VAR_A"), "{msg}");
        assert!(msg.contains("APPLY_VAR_B"), "{msg}");
        assert!(msg.contains("ghost"), "{msg}");
        assert!(!dir.path().join("local/audit/events.jsonl").exists());
    }

    #[test]
    fn check_reports_missing_but_excludes_it_from_the_verdict() {
        let (dir, store) = seeded_store_with_dev_secrets();
        // Converged env + one unset var: check exits 0 — a CI convergence
        // gate must be runnable without holding credentials — and reports
        // the gap.
        let manifest_path = write_manifest(dir.path(), &secrets_manifest("APPLY_UNSET_CI_VAR"));
        let lookup = |_: &str| None;
        let outcome = run_with_lookup(&store, &manifest_path, ApplyMode::Check, &lookup)
            .expect("check passes without the secret value");
        assert_eq!(outcome.result["mode"], "check");
        assert_eq!(
            outcome.result["missing"].as_array().unwrap().len(),
            1,
            "{}",
            outcome.result
        );
        assert_eq!(outcome.result["undiffable"], 1);

        // Real drift still fails check, with or without the values.
        let mut mixed = full_manifest(&fixture());
        mixed["secrets"] = json!([{"path": SECRET_PATH, "from_env": "APPLY_UNSET_CI_VAR"}]);
        let mixed_path = write_manifest(dir.path(), &mixed);
        let err = run_with_lookup(&store, &mixed_path, ApplyMode::Check, &lookup).unwrap_err();
        assert!(matches!(err, OpError::Conflict(_)), "got {err:?}");
    }

    #[test]
    fn prompter_fills_missing_secret_and_plan_says_prompted() {
        let (dir, store) = seeded_store_with_dev_secrets();
        let manifest_path = write_manifest(dir.path(), &secrets_manifest("APPLY_PROMPT_VAR"));
        let lookup = |_: &str| None;
        let prompter = |path: &str, from_env: &str| {
            assert_eq!(path, SECRET_PATH);
            assert_eq!(from_env, "APPLY_PROMPT_VAR");
            Some("tok-prompted-1".to_string())
        };

        let outcome = run_with_lookup_and_prompter(
            &store,
            &manifest_path,
            ApplyMode::Apply,
            &lookup,
            Some(&prompter),
        )
        .expect("prompted apply succeeds");

        // The plan row says `prompted` (not `from $VAR`), the report's
        // missing list is empty, and the value never leaks anywhere.
        let envelope = serde_json::to_string(&outcome).unwrap();
        assert!(!envelope.contains("tok-prompted-1"), "{envelope}");
        let steps = outcome.result["steps"].as_array().unwrap();
        let put = steps.iter().find(|s| s["kind"] == "put-secret").unwrap();
        assert_eq!(put["detail"], "prompted (cannot diff until A9)");
        assert!(outcome.result["missing"].as_array().unwrap().is_empty());

        // The prompted value reached the dev store the runtime reads.
        let store_path = dir
            .path()
            .join("local")
            .join(super::super::secrets::DEV_STORE_RELATIVE);
        let bytes = dev_store_read(&store_path, &format!("secrets://local/{SECRET_PATH}"));
        assert_eq!(bytes, b"tok-prompted-1".to_vec());

        // Audit must not leak it either.
        let audit = std::fs::read_to_string(dir.path().join("local/audit/events.jsonl")).unwrap();
        assert!(!audit.contains("tok-prompted-1"));
    }

    #[test]
    fn prompter_decline_leaves_input_missing() {
        let (dir, store) = seeded_store_with_dev_secrets();
        let manifest_path = write_manifest(dir.path(), &secrets_manifest("APPLY_DECLINE_VAR"));
        let lookup = |_: &str| None;
        let prompter = |_: &str, _: &str| None;
        let err = run_with_lookup_and_prompter(
            &store,
            &manifest_path,
            ApplyMode::Apply,
            &lookup,
            Some(&prompter),
        )
        .unwrap_err();
        assert!(err.to_string().contains("1 missing input(s)"), "{err}");
        assert!(!dir.path().join("local/audit/events.jsonl").exists());
    }

    // --- paste-sourced secrets (manifest `from_env` absent) -----------------

    /// A paste-sourced secret entry: no `from_env`, value supplied out of band.
    fn paste_secrets_manifest() -> Value {
        json!({
            "schema": ENV_MANIFEST_SCHEMA_V1,
            "environment": {"id": "local"},
            "secrets": [{"path": SECRET_PATH}]
        })
    }

    fn dev_store_value(dir: &Path) -> Vec<u8> {
        let store_path = dir
            .join("local")
            .join(super::super::secrets::DEV_STORE_RELATIVE);
        dev_store_read(&store_path, &format!("secrets://local/{SECRET_PATH}"))
    }

    #[test]
    fn paste_secret_prefilled_value_is_put_and_plan_says_pasted() {
        // The wizard's hand-off: a pre-collected value, no env var, no prompt.
        let (dir, store) = seeded_store_with_dev_secrets();
        let manifest_path = write_manifest(dir.path(), &paste_secrets_manifest());
        let flags = OpFlags {
            schema_only: false,
            answers: Some(manifest_path.clone()),
        };
        let mut prefilled = BTreeMap::new();
        prefilled.insert(
            SECRET_PATH.to_string(),
            SecretValue::from("tok-pasted-42".to_string()),
        );
        let outcome = apply_with_lookups(
            &store,
            &flags,
            ApplyOptions {
                mode: ApplyMode::Apply,
                non_interactive: true,
                prefilled_secrets: prefilled,
                ..ApplyOptions::default()
            },
            &|_| None,
            None,
        )
        .expect("prefilled paste apply succeeds");

        let envelope = serde_json::to_string(&outcome).unwrap();
        assert!(!envelope.contains("tok-pasted-42"), "{envelope}");
        let steps = outcome.result["steps"].as_array().unwrap();
        let put = steps.iter().find(|s| s["kind"] == "put-secret").unwrap();
        assert_eq!(put["detail"], "pasted (cannot diff until A9)");
        assert!(outcome.result["missing"].as_array().unwrap().is_empty());
        assert_eq!(dev_store_value(dir.path()), b"tok-pasted-42".to_vec());
    }

    #[test]
    fn paste_secret_already_in_store_is_a_noop() {
        // Store-as-source-of-truth: with the value already present, a re-apply
        // that can't re-collect it (no prefill, no prompter) is satisfied —
        // no put step, no missing input, value untouched.
        let (dir, store) = seeded_store_with_dev_secrets();
        let manifest_path = write_manifest(dir.path(), &paste_secrets_manifest());
        // Seed via a first prefilled apply.
        let mut prefilled = BTreeMap::new();
        prefilled.insert(
            SECRET_PATH.to_string(),
            SecretValue::from("tok-stored".to_string()),
        );
        let flags = OpFlags {
            schema_only: false,
            answers: Some(manifest_path.clone()),
        };
        apply_with_lookups(
            &store,
            &flags,
            ApplyOptions {
                mode: ApplyMode::Apply,
                non_interactive: true,
                prefilled_secrets: prefilled,
                ..ApplyOptions::default()
            },
            &|_| None,
            None,
        )
        .expect("seed apply");

        // Second apply: no prefill, no prompter, non-interactive.
        let outcome = run_with_lookup(&store, &manifest_path, ApplyMode::Apply, &|_| None)
            .expect("re-apply is a no-op, not a missing-input failure");
        let steps = outcome.result["steps"].as_array().unwrap();
        assert!(
            !steps.iter().any(|s| s["kind"] == "put-secret"),
            "an already-stored paste secret needs no put step: {steps:?}"
        );
        assert!(outcome.result["missing"].as_array().unwrap().is_empty());
        assert_eq!(dev_store_value(dir.path()), b"tok-stored".to_vec());
    }

    #[test]
    fn paste_secret_missing_when_absent_and_non_interactive() {
        // No prefill, not in the store, no prompter → reported missing with a
        // `paste` source (not an env var), and apply aborts before mutation.
        let (dir, store) = seeded_store_with_dev_secrets();
        let manifest_path = write_manifest(dir.path(), &paste_secrets_manifest());
        let err = run_with_lookup(&store, &manifest_path, ApplyMode::Apply, &|_| None).unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("1 missing input(s)"), "{msg}");
        assert!(msg.contains("paste"), "names the paste source: {msg}");
        assert!(msg.contains(SECRET_PATH), "{msg}");
        assert!(!dir.path().join("local/audit/events.jsonl").exists());
    }

    #[test]
    fn paste_secret_prompts_with_value_wording() {
        // Interactive, no prefill, not in store → masked prompt; the prompter
        // receives an empty `from_env` (paste wording) and the value is put.
        let (dir, store) = seeded_store_with_dev_secrets();
        let manifest_path = write_manifest(dir.path(), &paste_secrets_manifest());
        let prompter = |path: &str, from_env: &str| {
            assert_eq!(path, SECRET_PATH);
            assert_eq!(from_env, "", "paste secrets prompt with an empty from_env");
            Some("tok-typed".to_string())
        };
        let outcome = run_with_lookup_and_prompter(
            &store,
            &manifest_path,
            ApplyMode::Apply,
            &|_| None,
            Some(&prompter),
        )
        .expect("paste prompt apply succeeds");
        let steps = outcome.result["steps"].as_array().unwrap();
        let put = steps.iter().find(|s| s["kind"] == "put-secret").unwrap();
        assert_eq!(put["detail"], "pasted (cannot diff until A9)");
        assert_eq!(dev_store_value(dir.path()), b"tok-typed".to_vec());
    }

    #[test]
    fn paste_secret_empty_prefilled_is_treated_as_missing() {
        // A prefilled value that is empty must NOT slip through as resolved
        // (it would only be rejected by `secrets::put` at execute, after
        // earlier steps mutated state). Like the env/prompt branches, an empty
        // prefilled value falls through to the missing-input report.
        let (dir, store) = seeded_store_with_dev_secrets();
        let manifest_path = write_manifest(dir.path(), &paste_secrets_manifest());
        let flags = OpFlags {
            schema_only: false,
            answers: Some(manifest_path.clone()),
        };
        let mut prefilled = BTreeMap::new();
        prefilled.insert(SECRET_PATH.to_string(), SecretValue::from(String::new()));
        let err = apply_with_lookups(
            &store,
            &flags,
            ApplyOptions {
                mode: ApplyMode::Apply,
                non_interactive: true,
                prefilled_secrets: prefilled,
                ..ApplyOptions::default()
            },
            &|_| None,
            None,
        )
        .unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("1 missing input(s)"), "{msg}");
        assert!(msg.contains("paste"), "names the paste source: {msg}");
        // Aborted before any mutation — no audit log written.
        assert!(!dir.path().join("local/audit/events.jsonl").exists());
    }

    #[test]
    fn emit_answers_template_writes_valid_manifest_and_touches_nothing() {
        let dir = tempdir().unwrap();
        let store = LocalFsStore::new(dir.path());
        let out = dir.path().join("template.env.json");
        let outcome = emit_answers_template(&out).expect("template emit succeeds");
        assert_eq!(outcome.result["mode"], "emit-answers-template");

        // What lands on disk parses under deny_unknown_fields and is
        // shape-valid as-is (the env_manifest guard test pins the source;
        // this pins the written artifact).
        let written: EnvManifest = serde_json::from_slice(&std::fs::read(&out).unwrap())
            .expect("written template parses as EnvManifest");
        written
            .validate_shape()
            .expect("written template is shape-valid");
        // No env/store state was created.
        assert!(!store.exists(&EnvId::try_from("local").unwrap()).unwrap());
    }

    // --- Fix 8: welcome-flow reachability lives in env-side validation only ---

    #[test]
    fn welcome_flow_not_in_links_rejected_by_env_validation() {
        // Manifest declares a welcome_flow whose bundle_id IS in bundles[] but
        // NOT in that endpoint's links[]. Shape validation passes (the check
        // was removed from validate_shape); env-side validation catches it.
        let (dir, store) = seeded_store();
        let manifest = json!({
            "schema": ENV_MANIFEST_SCHEMA_V1,
            "environment": {"id": "local"},
            "bundles": [{"bundle_id": "quickstart", "bundle_path": fixture()}],
            "messaging_endpoints": [{
                "name": "n",
                "provider_type": "messaging.telegram.bot",
                "links": [],
                "welcome_flow": {"bundle_id": "quickstart", "pack_id": "p", "flow_id": "f"}
            }]
        });
        let manifest_path = write_manifest(dir.path(), &manifest);
        let err = run_apply(&store, &manifest_path).unwrap_err();
        match err {
            OpError::InvalidArgument(msg) => {
                assert!(msg.contains("links[]"), "got: {msg}");
            }
            other => panic!("expected InvalidArgument mentioning links[], got {other:?}"),
        }
        // No mutation: env-side validation fires before any step.
        let env = load_local(&store);
        assert!(env.messaging_endpoints.is_empty());
        assert!(env.revisions.is_empty());
    }

    // --- Fix 10a: hash_json determinism pin ---

    #[test]
    fn hash_json_output_is_stable() {
        // Pins idempotency-key stability across builds. serde_json without
        // preserve_order sorts keys, so {"b":1,"a":[true,"x"]} serializes
        // as {"a":[true,"x"],"b":1}.
        assert_eq!(
            hash_json(&json!({"b": 1, "a": [true, "x"]})),
            "f15ef113d6e0c876b9ea9e90ebc36ad3f8b350d44634ba2fc407e978fb8cebeb"
        );
    }

    // --- Fix 10c: deploy rejects differing route binding (contract pin) ---

    #[test]
    fn deploy_rejects_differing_binding_contract_pin() {
        // Apply relies on deploy-first / route_binding: None ordering for the
        // re-deploy site. That ordering only works because deploy rejects a
        // differing binding on an existing deployment (returning Conflict).
        // This test pins that contract so a future change to deploy's binding
        // check breaks the apply tests explicitly.
        let (_dir, store) = seeded_store();
        let mut p = super::super::deploy::BundleDeployPayload {
            environment_id: "local".to_string(),
            bundle_id: "quickstart".to_string(),
            customer_id: None,
            bundle_path: Some(fixture()),
            idempotency_key: None,
            config_overrides: None,
            route_binding: Some(super::super::bundles::RouteBindingPayload {
                hosts: Vec::new(),
                path_prefixes: vec!["/v1".to_string()],
                tenant_selector: None,
            }),
            revenue_share: None,
        };
        super::super::deploy::deploy(&store, &OpFlags::default(), Some(p.clone()))
            .expect("first deploy");
        // Re-deploy with a DIFFERENT binding.
        p.route_binding = Some(super::super::bundles::RouteBindingPayload {
            hosts: Vec::new(),
            path_prefixes: vec!["/v2".to_string()],
            tenant_selector: None,
        });
        let err = super::super::deploy::deploy(&store, &OpFlags::default(), Some(p)).unwrap_err();
        match err {
            OpError::Conflict(msg) => {
                assert!(
                    msg.contains("route_binding differs"),
                    "expected Conflict about differing binding, got: {msg}"
                );
            }
            other => panic!("expected Conflict, got {other:?}"),
        }
    }

    // --- G5: env-pack bindings ---

    #[test]
    fn pack_binding_add_then_idempotent() {
        let (dir, store) = seeded_store();
        let manifest = json!({
            "schema": ENV_MANIFEST_SCHEMA_V1,
            "environment": {"id": "local"},
            "packs": [{
                "slot": "deployer",
                "kind": "greentic.deployer.local@1.0.0",
                "pack_ref": "builtin:deployer-local"
            }]
        });
        let manifest_path = write_manifest(dir.path(), &manifest);
        let outcome = run_apply(&store, &manifest_path).expect("add pack binding");
        let actions = step_actions(&outcome.result);
        assert!(
            actions.contains(&("add-pack-binding".to_string(), "create".to_string())),
            "must plan add-pack-binding: {actions:?}"
        );

        let env = load_local(&store);
        let binding = env.pack_for_slot(CapabilitySlot::Deployer);
        assert!(binding.is_some(), "deployer slot must be bound");
        let b = binding.unwrap();
        assert_eq!(b.kind.to_string(), "greentic.deployer.local@1.0.0");
        assert_eq!(b.pack_ref.as_str(), "builtin:deployer-local");

        // Idempotent re-apply.
        let second = run_apply(&store, &manifest_path).expect("re-apply");
        assert_eq!(second.result["changed"], 0, "{}", second.result);
    }

    #[test]
    fn pack_binding_update_on_kind_change() {
        let (dir, store) = seeded_store();
        // Seed with v1.
        let v1 = json!({
            "schema": ENV_MANIFEST_SCHEMA_V1,
            "environment": {"id": "local"},
            "packs": [{
                "slot": "deployer",
                "kind": "greentic.deployer.local@1.0.0",
                "pack_ref": "builtin:deployer-local"
            }]
        });
        let v1_path = write_manifest(dir.path(), &v1);
        run_apply(&store, &v1_path).expect("first apply");

        // Update to v2.
        let v2 = json!({
            "schema": ENV_MANIFEST_SCHEMA_V1,
            "environment": {"id": "local"},
            "packs": [{
                "slot": "deployer",
                "kind": "greentic.deployer.local@2.0.0",
                "pack_ref": "builtin:deployer-local-v2"
            }]
        });
        let v2_path = write_manifest(dir.path(), &v2);
        let plan = run_dry(&store, &v2_path).expect("dry-run");
        let actions = step_actions(&plan.result);
        assert!(
            actions.contains(&("update-pack-binding".to_string(), "update".to_string())),
            "must plan update-pack-binding: {actions:?}"
        );

        run_apply(&store, &v2_path).expect("apply update");
        let env = load_local(&store);
        let b = env.pack_for_slot(CapabilitySlot::Deployer).expect("bound");
        assert_eq!(b.kind.to_string(), "greentic.deployer.local@2.0.0");
        assert_eq!(b.pack_ref.as_str(), "builtin:deployer-local-v2");
        assert!(b.generation > 0, "generation must bump on update");
    }

    #[test]
    fn pack_binding_rejects_messaging_slot() {
        let dir = tempdir().unwrap();
        let manifest = json!({
            "schema": ENV_MANIFEST_SCHEMA_V1,
            "environment": {"id": "local"},
            "packs": [{
                "slot": "messaging",
                "kind": "greentic.messaging.telegram@1.0.0",
                "pack_ref": "builtin:msg"
            }]
        });
        let manifest_path = write_manifest(dir.path(), &manifest);
        let loaded: EnvManifest =
            serde_json::from_slice(&std::fs::read(&manifest_path).unwrap()).unwrap();
        let err = loaded.validate_shape().unwrap_err();
        assert!(err.to_string().contains("messaging"), "got: {err}");
    }

    // --- G6: extensions ---

    #[test]
    fn extension_add_then_idempotent() {
        let (dir, store) = seeded_store();
        let manifest = json!({
            "schema": ENV_MANIFEST_SCHEMA_V1,
            "environment": {"id": "local"},
            "extensions": [{
                "kind": "greentic.ext.memory@1.0.0",
                "pack_ref": "builtin:memory-chronicle",
                "instance_id": "default"
            }]
        });
        let manifest_path = write_manifest(dir.path(), &manifest);
        let outcome = run_apply(&store, &manifest_path).expect("add extension");
        let actions = step_actions(&outcome.result);
        assert!(
            actions.contains(&("add-extension".to_string(), "create".to_string())),
            "must plan add-extension: {actions:?}"
        );

        let env = load_local(&store);
        assert_eq!(env.extensions.len(), 1);
        let ext = &env.extensions[0];
        assert_eq!(ext.kind.to_string(), "greentic.ext.memory@1.0.0");
        assert_eq!(ext.pack_ref.as_str(), "builtin:memory-chronicle");
        assert_eq!(ext.instance_id.as_deref(), Some("default"));

        // Idempotent re-apply.
        let second = run_apply(&store, &manifest_path).expect("re-apply");
        assert_eq!(second.result["changed"], 0, "{}", second.result);
    }

    #[test]
    fn extension_update_on_pack_ref_change() {
        let (dir, store) = seeded_store();
        let v1 = json!({
            "schema": ENV_MANIFEST_SCHEMA_V1,
            "environment": {"id": "local"},
            "extensions": [{
                "kind": "greentic.ext.memory@1.0.0",
                "pack_ref": "builtin:memory-v1"
            }]
        });
        let v1_path = write_manifest(dir.path(), &v1);
        run_apply(&store, &v1_path).expect("first apply");

        let v2 = json!({
            "schema": ENV_MANIFEST_SCHEMA_V1,
            "environment": {"id": "local"},
            "extensions": [{
                "kind": "greentic.ext.memory@1.0.0",
                "pack_ref": "builtin:memory-v2"
            }]
        });
        let v2_path = write_manifest(dir.path(), &v2);
        let plan = run_dry(&store, &v2_path).expect("dry-run");
        let actions = step_actions(&plan.result);
        assert!(
            actions.contains(&("update-extension".to_string(), "update".to_string())),
            "must plan update-extension: {actions:?}"
        );

        run_apply(&store, &v2_path).expect("apply update");
        let env = load_local(&store);
        assert_eq!(env.extensions[0].pack_ref.as_str(), "builtin:memory-v2");
        assert!(
            env.extensions[0].generation > 0,
            "generation must bump on update"
        );
    }

    #[test]
    fn extension_bad_instance_id_rejected_by_shape() {
        let manifest = json!({
            "schema": ENV_MANIFEST_SCHEMA_V1,
            "environment": {"id": "local"},
            "extensions": [{
                "kind": "greentic.ext.memory@1.0.0",
                "pack_ref": "builtin:memory",
                "instance_id": "BAD_ID!"
            }]
        });
        let loaded: EnvManifest = serde_json::from_value(manifest).unwrap();
        let err = loaded.validate_shape().unwrap_err();
        assert!(err.to_string().contains("instance_id"), "got: {err}");
    }

    #[test]
    fn extension_duplicate_key_rejected_by_shape() {
        let manifest = json!({
            "schema": ENV_MANIFEST_SCHEMA_V1,
            "environment": {"id": "local"},
            "extensions": [
                {"kind": "greentic.ext.memory@1.0.0", "pack_ref": "a", "instance_id": "x"},
                {"kind": "greentic.ext.memory@2.0.0", "pack_ref": "b", "instance_id": "x"},
            ]
        });
        let loaded: EnvManifest = serde_json::from_value(manifest).unwrap();
        let err = loaded.validate_shape().unwrap_err();
        assert!(err.to_string().contains("duplicate"), "got: {err}");
    }

    // --- G7: host-config reconcile ---

    #[test]
    fn host_config_reconcile_then_idempotent() {
        let (dir, store) = seeded_store();
        let manifest = json!({
            "schema": ENV_MANIFEST_SCHEMA_V1,
            "environment": {
                "id": "local",
                "name": "Local Dev",
                "region": "eu-west-1",
                "tenant_org_id": "org-99",
                "listen_addr": "0.0.0.0:9090"
            }
        });
        let manifest_path = write_manifest(dir.path(), &manifest);
        let outcome = run_apply(&store, &manifest_path).expect("host-config apply");
        let actions = step_actions(&outcome.result);
        assert!(
            actions.contains(&("update-host-config".to_string(), "update".to_string()))
                || actions.contains(&("update-host-config".to_string(), "create".to_string())),
            "must plan update-host-config: {actions:?}"
        );

        let env = load_local(&store);
        assert_eq!(env.name, "Local Dev");
        assert_eq!(env.host_config.region.as_deref(), Some("eu-west-1"));
        assert_eq!(env.host_config.tenant_org_id.as_deref(), Some("org-99"));
        assert_eq!(
            env.host_config.listen_addr,
            Some("0.0.0.0:9090".parse().unwrap())
        );

        // Idempotent re-apply.
        let second = run_apply(&store, &manifest_path).expect("re-apply");
        assert_eq!(second.result["changed"], 0, "{}", second.result);
    }

    #[test]
    fn gui_enabled_reconcile_then_idempotent() {
        let (dir, store) = seeded_store();
        // `local` resolves GUI-on by default; declaring `false` is an explicit
        // override that must persist and survive a re-apply as a no-op.
        let manifest = json!({
            "schema": ENV_MANIFEST_SCHEMA_V1,
            "environment": {
                "id": "local",
                "gui_enabled": false
            }
        });
        let manifest_path = write_manifest(dir.path(), &manifest);
        let outcome = run_apply(&store, &manifest_path).expect("gui apply");
        let actions = step_actions(&outcome.result);
        assert!(
            actions.contains(&("update-host-config".to_string(), "update".to_string())),
            "must plan update-host-config for gui_enabled: {actions:?}"
        );

        let env = load_local(&store);
        assert_eq!(env.host_config.gui_enabled, Some(false));
        assert!(
            !env.host_config.resolved_gui_enabled(),
            "explicit false overrides the local default-on"
        );

        // Idempotent re-apply.
        let second = run_apply(&store, &manifest_path).expect("re-apply");
        assert_eq!(second.result["changed"], 0, "{}", second.result);
    }

    #[test]
    fn host_config_bad_listen_addr_rejected_by_shape() {
        let manifest = json!({
            "schema": ENV_MANIFEST_SCHEMA_V1,
            "environment": {
                "id": "local",
                "listen_addr": "not-an-addr"
            }
        });
        let loaded: EnvManifest = serde_json::from_value(manifest).unwrap();
        let err = loaded.validate_shape().unwrap_err();
        assert!(err.to_string().contains("listen_addr"), "got: {err}");
    }

    // --- G5+G6 ordering ---

    #[test]
    fn packs_before_secrets_extensions_before_endpoints() {
        let (dir, store) = seeded_store_with_dev_secrets();
        let manifest = json!({
            "schema": ENV_MANIFEST_SCHEMA_V1,
            "environment": {"id": "local"},
            "packs": [{
                "slot": "deployer",
                "kind": "greentic.deployer.local@1.0.0",
                "pack_ref": "builtin:deployer-local"
            }],
            "secrets": [{"path": SECRET_PATH, "from_env": "APPLY_ORDER_VAR"}],
            "bundles": [{"bundle_id": "quickstart", "bundle_path": fixture()}],
            "extensions": [{
                "kind": "greentic.ext.memory@1.0.0",
                "pack_ref": "builtin:memory"
            }],
            "messaging_endpoints": [{
                "name": "bot",
                "provider_type": "messaging.telegram.bot",
                "links": ["quickstart"]
            }]
        });
        let manifest_path = write_manifest(dir.path(), &manifest);
        let lookup = |_: &str| Some("tok".to_string());
        let plan =
            run_with_lookup(&store, &manifest_path, ApplyMode::DryRun, &lookup).expect("dry-run");
        let steps = plan.result["steps"].as_array().expect("steps");
        let pos = |kind: &str| {
            steps
                .iter()
                .position(|s| s["kind"] == kind)
                .unwrap_or_else(|| panic!("no `{kind}` step in {steps:?}"))
        };
        assert!(pos("ensure-environment") < pos("add-pack-binding"));
        assert!(pos("add-pack-binding") < pos("put-secret"));
        assert!(pos("put-secret") < pos("deploy-bundle"));
        assert!(pos("deploy-bundle") < pos("add-extension"));
        assert!(pos("add-extension") < pos("add-endpoint"));
    }

    // --- G5 fresh-env pack diff against default bindings ---

    /// On a fresh local env (no env on disk), `env init` will create 5 default
    /// pack bindings. The planner must diff manifest packs against those
    /// defaults so a pack targeting a default slot emits UpdatePackBinding (if
    /// the desired binding differs) or a no-op (if identical), never an
    /// unconditional AddPackBinding that would fail with SlotAlreadyBound.
    /// A pack targeting a non-default slot (e.g. revocation) still correctly
    /// emits AddPackBinding.
    #[test]
    fn fresh_env_pack_on_default_slot_emits_update_not_add() {
        let dir = tempdir().unwrap();
        let store = LocalFsStore::new(dir.path());
        // No env on disk — this is a fresh-env scenario.

        // Pack 1: deployer with SAME kind+pack_ref as the default → no-op.
        // Pack 2: telemetry with DIFFERENT kind → update.
        // Pack 3: revocation (not in defaults) → add.
        let manifest = json!({
            "schema": ENV_MANIFEST_SCHEMA_V1,
            "environment": {"id": "local"},
            "packs": [
                {
                    "slot": "deployer",
                    "kind": crate::defaults::LOCAL_DEPLOYER_PACK,
                    "pack_ref": crate::defaults::LOCAL_DEPLOYER_PACK
                },
                {
                    "slot": "telemetry",
                    "kind": "greentic.telemetry.otlp@2.0.0",
                    "pack_ref": "builtin:otlp"
                },
                {
                    "slot": "revocation",
                    "kind": "greentic.revocation.crl@1.0.0",
                    "pack_ref": "builtin:crl"
                }
            ]
        });
        let manifest_path = write_manifest(dir.path(), &manifest);
        let plan = run_dry(&store, &manifest_path).expect("dry-run on fresh env");
        let steps = plan.result["steps"].as_array().expect("steps array");

        // Find each pack step by its key (= slot name).
        let find_step = |key: &str| {
            steps
                .iter()
                .find(|s| s["key"] == key)
                .unwrap_or_else(|| panic!("no step with key `{key}` in {steps:?}"))
        };

        // deployer: identical to default → no-op (kind is still add-pack-binding
        // because the no-op variant reuses that kind label).
        let deployer = find_step("deployer");
        assert_eq!(
            deployer["action"].as_str().unwrap(),
            "no-op",
            "deployer must be no-op (matches default): {deployer}"
        );

        // telemetry: differs from default → update-pack-binding.
        let telemetry = find_step("telemetry");
        assert_eq!(
            telemetry["kind"].as_str().unwrap(),
            "update-pack-binding",
            "telemetry must be update (differs from default): {telemetry}"
        );
        assert_eq!(
            telemetry["action"].as_str().unwrap(),
            "update",
            "telemetry action must be update: {telemetry}"
        );

        // revocation: not in defaults → add-pack-binding.
        let revocation = find_step("revocation");
        assert_eq!(
            revocation["kind"].as_str().unwrap(),
            "add-pack-binding",
            "revocation must be add (not in defaults): {revocation}"
        );
        assert_eq!(
            revocation["action"].as_str().unwrap(),
            "create",
            "revocation action must be create: {revocation}"
        );
    }
}