ignition-core 1.7.0

Core library for ign: config, profiles, gateway client, actions, error taxonomy
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
//! The WebDev deploy/status actions (05-03, WEB-01 + WEB-02) — the
//! hinge layer every tag command in 05-04..06 rides.
//!
//! `webdev_deploy` installs the embedded route bundle
//! ([`crate::webdev`], 05-01) into the dedicated project through the
//! 03-02 import machinery with `overwrite=true` — the CLI owns the
//! project wholesale, so replace-not-merge is CORRECT here (research
//! deploy guidance) and deploy is deliberately NOT `--yes`-guarded.
//! The project is born from the first deploy zip: NO pre-flight
//! create (Pitfall 10's one-shot "resource already exists" quirk).
//!
//! `webdev_status` probes every route's `version` action and reports
//! the per-route matrix ({present, absent, unlicensed, auth_gated,
//! secret_mismatch, version_mismatch}) — a READ: exit 0 whenever the
//! sweep completes, degradation is data (the doctor precedent).
//!
//! `webdev_precondition` is the cheap refusal every
//! WebDev-DEPENDENT command runs first (05-04+): absent routes or a
//! version mismatch refuse exit 6 naming `ign webdev deploy` — the
//! roadmap's actionable-error criterion, no auto-upgrade magic.
//!
//! The scriptExec secret lifecycle lives HERE: deploy generates a
//! 32-byte hex secret from `/dev/urandom` (zero-dep — the workspace
//! has no `rand`; unix-only is fine, no Windows CI is locked),
//! persists it in the profile config at 0600 (the ONE
//! value-carrying exception on [`crate::config::Profile`], documented
//! there), and bakes it into the route zip BEFORE upload. The secret
//! NEVER appears in any action result, log, or JSON envelope — the
//! redaction test below pins that.

use std::path::{Path, PathBuf};

use serde::Serialize;

use crate::client::GatewayApi;
use crate::client::webdev::{self as seam, RouteProbe};
use crate::config;
use crate::error::{CoreError, TAG_DELETE_DROP_REFUSAL_PREFIX};
use crate::webdev as bundle;

/// The secret-bearing scriptExec route name (deploy/status append it
/// only when explicitly requested / configured). `pub(crate)` since
/// 07-03: the script action reuses the EXACT spelling (one constant,
/// no drift).
pub(crate) const SCRIPT_EXEC_ROUTE: &str = "scriptExec";

/// The header the scriptExec gate compares (case-insensitive
/// server-side; the CLI sends the canonical form). `pub(crate)` since
/// 07-03 — the script action's calls carry it too.
pub(crate) const SECRET_HEADER: &str = "X-Ignition-CLI-Secret";

/// The tagDelete route name (26-17), appended to `routes` when it ships.
pub(crate) const TAG_DELETE_ROUTE: &str = crate::error::TAG_DELETE_ROUTE_NAME;

/// A request to install the tagDelete route (26-17, D-13) with the deploy.
/// The provider allowlist is the union of the repos' `deploy/project.yaml`
/// `tag_providers`, in repo order.
#[derive(Debug, Clone)]
pub struct TagDeleteRequest {
    /// The project repos whose `deploy/project.yaml` name the owned
    /// providers (at least one).
    pub repos: Vec<PathBuf>,
    /// The WebDev user source the route authenticates against.
    pub user_source: String,
    /// The role WebDev requires of the caller.
    pub role: String,
    /// Regenerate the route secret even when one is stored.
    pub rotate_secret: bool,
    /// The route caller's credential (user, password), the profile's env
    /// pair, when set. It is used for one `version` handshake that reads the
    /// live allowlist before the install replaces it (26-REVIEW-D WR-D3).
    /// `None`: the live allowlist cannot be read, and the install warns.
    pub caller: Option<(config::Secret, config::Secret)>,
}

/// `ign webdev deploy` result — ALL keys always (the agent shape);
/// the import answer rides as the opaque-success object verbatim.
/// The secret appears in NONE of them (redaction).
#[derive(Debug, Serialize)]
pub struct WebdevDeployResult {
    /// The project the bundle deployed into.
    pub project: String,
    /// Route folder names deployed, manifest order (+ scriptExec when
    /// it shipped).
    pub routes: Vec<String>,
    /// Whether scriptExec rode the deploy.
    pub script_exec: bool,
    /// Whether a NEW secret was generated and persisted (first
    /// scriptExec deploy or `--rotate-secret`).
    pub secret_rotated: bool,
    /// The import endpoint's opaque answer (the 03-02
    /// `{"status":"success"}`-normalized object).
    pub import: serde_json::Value,
    /// Whether the tagDelete route rode the deploy (26-17).
    pub tag_delete: bool,
    /// Whether a NEW tagDelete secret was generated and persisted (first
    /// tag-delete deploy or its rotate).
    pub tag_delete_secret_rotated: bool,
    /// The tagDelete route's provider allowlist (empty when it did not
    /// ship).
    pub tag_delete_providers: Vec<String>,
    /// Warnings for the operator (26-REVIEW-D WR-D4: the tagDelete route
    /// and its stored secret were removed). Empty when there are none.
    pub warnings: Vec<String>,
}

/// One route's status-sweep row — ALL keys always.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct RouteStatusRow {
    /// Route folder name.
    pub route: String,
    /// The per-route matrix verdict.
    pub status: RouteStatus,
    /// The route's answered `routeVersion` (absent states → null).
    pub deployed_version: Option<String>,
    /// The embedded bundle version this CLI expects (always known).
    pub expected_version: Option<String>,
}

/// The per-route status matrix (WebDev-dependent commands refuse on
/// the same discrimination; status reports it as data).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum RouteStatus {
    /// Deployed, answering, version matches the embedded bundle.
    Present,
    /// 405 — not deployed (the 8.3 absent marker).
    Absent,
    /// 402 — the WebDev module is unlicensed (trial-expired gateway).
    Unlicensed,
    /// 401/403 — present but rejecting the credential.
    AuthGated,
    /// The scriptExec gate refused the configured secret (deployed
    /// elsewhere / stale) — redeploy or `--rotate-secret`.
    SecretMismatch,
    /// Deployed but the handshake version differs from the bundle's.
    VersionMismatch,
}

/// `ign webdev status` result — ALL keys always.
#[derive(Debug, Serialize)]
pub struct WebdevStatusResult {
    /// The probed project.
    pub project: String,
    /// One row per always-on route (+ scriptExec when a secret is
    /// configured — unprobed otherwise, per the plan's conditional).
    pub routes: Vec<RouteStatusRow>,
    /// True only when every ALWAYS-ON route is present with matching
    /// versions (scriptExec never gates `ok` — it deploys on explicit
    /// request only).
    pub ok: bool,
}

/// `ign webdev deploy` — pack the embedded bundle, import it
/// overwrite-style, own the secret lifecycle.
///
/// Secret rules (the plan's LOCKED posture): a fresh secret is
/// generated + persisted 0600 when (a) `--with-script-exec` finds no
/// stored secret, or (b) `--rotate-secret` asks regardless; a plain
/// scriptExec deploy reuses the stored secret unchanged. Persisting
/// happens BEFORE the upload: a failed import then leaves a stored
/// secret the NEXT deploy packs as-is (self-healing), and a broken
/// config store refuses before any gateway I/O.
pub async fn webdev_deploy(
    api: &dyn GatewayApi,
    project: &str,
    with_script_exec: bool,
    rotate_secret: bool,
    config_path: &Path,
    profile_name: &str,
    with_testing: bool,
) -> Result<WebdevDeployResult, CoreError> {
    webdev_deploy_with(
        api,
        project,
        with_script_exec,
        rotate_secret,
        config_path,
        profile_name,
        with_testing,
        None,
    )
    .await
}

/// [`webdev_deploy`] plus, with `tag_delete`, the tagDelete route (26-17,
/// D-13) in the same import. The allowlist is the repo's
/// `deploy/project.yaml` `tag_providers`; the route secret follows the
/// scriptExec rules independently (generated when none is stored or on its
/// rotate, persisted 0600 as `webdev_tag_delete_secret` before the upload,
/// never output). Every install value is checked before any secret is
/// stored and before any gateway request.
#[allow(clippy::too_many_arguments)]
pub async fn webdev_deploy_with(
    api: &dyn GatewayApi,
    project: &str,
    with_script_exec: bool,
    rotate_secret: bool,
    config_path: &Path,
    profile_name: &str,
    with_testing: bool,
    tag_delete: Option<TagDeleteRequest>,
) -> Result<WebdevDeployResult, CoreError> {
    // Secret lifecycle first — refuse on an unwritable config store
    // BEFORE touching the gateway.
    let mut config = config::load(config_path)?;
    let profile = config.profiles.get(profile_name);
    let existing = profile.and_then(|profile| profile.webdev_secret.clone());
    let existing_tag_delete = profile.and_then(|profile| profile.webdev_tag_delete_secret.clone());

    let (pack_secret, secret_rotated) = if rotate_secret || (with_script_exec && existing.is_none())
    {
        (Some(generate_secret()?), true)
    } else if with_script_exec {
        (existing, false)
    } else {
        (None, false)
    };

    let mut warnings = Vec::new();

    // The tagDelete install: allowlist from the repos, secret stored or
    // fresh, every value validated before anything is persisted.
    let (install, tag_delete_secret_rotated) = match &tag_delete {
        None => (None, false),
        Some(request) => {
            // The route secret travels inside the import: only over https
            // or to a loopback host, with `ssl_verify` on for a remote one,
            // the route client's own rule (26-REVIEW WR-07). The URL is the
            // one the import's client sends to (26-REVIEW-D WR-D2), not a
            // fresh read of `IGNITION_URL`; a double without one falls back
            // to the stored profile.
            let transport = api.gateway_transport().or_else(|| {
                config
                    .profiles
                    .get(profile_name)
                    .map(|profile| (profile.url.clone(), profile.ssl_verify))
            });
            if let Some((url, ssl_verify)) = &transport
                && let Some(refusal) =
                    crate::deploy::tag_delete::transport_refusal(url, *ssl_verify)
            {
                return Err(CoreError::TagDeleteNotConfigured {
                    profile: profile_name.to_string(),
                    missing: vec![refusal],
                });
            }
            let mut providers: Vec<String> = Vec::new();
            for repo in &request.repos {
                for provider in crate::deploy::projects::load_project_file(repo)?.tag_providers {
                    if !providers.contains(&provider) {
                        providers.push(provider);
                    }
                }
            }
            let (secret, rotated) = match existing_tag_delete.clone() {
                Some(secret) if !request.rotate_secret => (secret, false),
                _ => (generate_secret()?, true),
            };
            let install = seam::TagDeleteInstall {
                secret,
                user_source: request.user_source.clone(),
                role: request.role.clone(),
                providers,
            };
            seam::validate_tag_delete_install(&install)?;
            // 26-REVIEW-D WR-D3: the overwrite replaces the live allowlist.
            // With a stored secret a route may be installed: read its
            // allowlist once and refuse an install that would drop a
            // provider allowed now. Unreadable: install and warn.
            if let Some(stored) = &existing_tag_delete {
                match read_live_allowlist(
                    profile_name,
                    transport.as_ref(),
                    project,
                    request.caller.clone(),
                    stored,
                )
                .await
                {
                    LiveAllowlist::Read(live) => {
                        refuse_dropped_providers(project, &live, &install.providers)?;
                    }
                    LiveAllowlist::Absent => {}
                    LiveAllowlist::Unreadable(reason) => warnings.push(format!(
                        "could not read the live tagDelete allowlist on project {project} \
                         ({reason}); the install replaced it with [{}], so check that no \
                         other repo's providers were dropped",
                        install.providers.join(", ")
                    )),
                }
            }
            (Some(install), rotated)
        }
    };

    // 26-REVIEW-D WR-D4: the overwrite import replaces the project, so a
    // deploy into the route's project without the tag-delete request drops
    // the tagDelete route. The stored secret goes with it, before the
    // upload, so the profile reads not configured (a run with removals then
    // refuses `tag_delete_not_configured`, a rollback reports no route)
    // instead of waiting on a route that is gone. The deploy is not refused:
    // adopt and the TUI deploy this way.
    let tag_delete_removed =
        tag_delete.is_none() && project == seam::DEFAULT_PROJECT && existing_tag_delete.is_some();
    if tag_delete_removed {
        warnings.push(format!(
            "tagDelete route removed from project {project}: this deploy did not include \
             --with-tag-delete, so the overwrite import dropped it; the stored tagDelete \
             secret was removed from profile {profile_name:?}, and tag-owning runs refuse \
             tag_delete_not_configured until `ign webdev deploy --with-tag-delete` reinstalls it"
        ));
    }

    if secret_rotated || tag_delete_secret_rotated || tag_delete_removed {
        let entry = config.profiles.get_mut(profile_name).ok_or_else(|| {
            CoreError::Internal(format!(
                "profile {profile_name:?} vanished from the config between dispatch and deploy"
            ))
        })?;
        if secret_rotated {
            entry.webdev_secret = pack_secret.clone();
        }
        if tag_delete_secret_rotated {
            entry.webdev_tag_delete_secret = install.as_ref().map(|install| install.secret.clone());
        }
        if tag_delete_removed {
            entry.webdev_tag_delete_secret = None;
        }
        config::save(config_path, &config)?; // re-asserts 0600
    }

    // Pack (scriptExec only when flagged — build_deploy_zip's
    // fail-closed guard covers the (true, None) bug case) and import
    // overwrite=true through the 03-02 machinery. NO pre-flight
    // project create (Pitfall 10).
    let zip = seam::build_deploy_zip_with(
        project,
        with_script_exec,
        pack_secret.as_deref(),
        with_testing,
        install.as_ref(),
    )?;
    let mut routes = seam::always_on_routes();
    if with_script_exec {
        routes.push(SCRIPT_EXEC_ROUTE.to_string());
    }
    if install.is_some() {
        routes.push(TAG_DELETE_ROUTE.to_string());
    }
    if with_testing {
        routes.extend(
            crate::webdev::testing::TESTING_ROUTES
                .iter()
                .map(|route| format!("testing/{route}")),
        );
    }
    let import = api.project_import(project, zip, true).await?;

    Ok(WebdevDeployResult {
        project: project.to_string(),
        routes,
        script_exec: with_script_exec,
        secret_rotated,
        import: import.response,
        tag_delete: install.is_some(),
        tag_delete_secret_rotated,
        tag_delete_providers: install.map(|install| install.providers).unwrap_or_default(),
        warnings,
    })
}

/// The live tagDelete allowlist as read before an install (26-REVIEW-D
/// WR-D3).
enum LiveAllowlist {
    /// The route answered its handshake with this allowlist.
    Read(Vec<String>),
    /// The route is not deployed (405): nothing to keep.
    Absent,
    /// The allowlist could not be read; the reason names no secret.
    Unreadable(String),
}

/// Read the live route's allowlist through one `version` handshake with the
/// stored route secret and the caller's credential. Never waits or retries.
async fn read_live_allowlist(
    profile_name: &str,
    transport: Option<&(url::Url, bool)>,
    project: &str,
    caller: Option<(config::Secret, config::Secret)>,
    stored_secret: &str,
) -> LiveAllowlist {
    use crate::deploy::tag_delete::{TagDeleteClient, TagDeleteRoute};
    let Some((url, ssl_verify)) = transport else {
        return LiveAllowlist::Unreadable("no gateway URL is known for the profile".to_string());
    };
    let Some((user, password)) = caller else {
        let (user, password) = config::secret::tag_delete_env_names(profile_name);
        return LiveAllowlist::Unreadable(format!("{user} and {password} are not both set"));
    };
    let client = match TagDeleteClient::new(
        profile_name,
        url,
        *ssl_verify,
        project,
        user,
        password,
        config::Secret::new(stored_secret),
    ) {
        Ok(client) => client,
        Err(err) => return LiveAllowlist::Unreadable(err.code().to_string()),
    };
    match client.handshake().await {
        Ok(handshake) => LiveAllowlist::Read(handshake.providers),
        Err(CoreError::RoutesNotDeployed { .. }) => LiveAllowlist::Absent,
        Err(err) => LiveAllowlist::Unreadable(format!("the handshake answered {}", err.code())),
    }
}

/// Refuse an install whose allowlist `new` lacks a provider of `live`.
fn refuse_dropped_providers(
    project: &str,
    live: &[String],
    new: &[String],
) -> Result<(), CoreError> {
    let dropped: Vec<&str> = live
        .iter()
        .filter(|provider| !new.contains(provider))
        .map(String::as_str)
        .collect();
    if dropped.is_empty() {
        return Ok(());
    }
    Err(CoreError::InvalidInput {
        reason: format!(
            "{TAG_DELETE_DROP_REFUSAL_PREFIX} {} from the live tagDelete allowlist on project \
             {project} (allowed now: {}; this install: {}); nothing was stored or imported. \
             Re-run naming every repo whose providers the route must keep (repeat \
             --tag-delete-repo), or replace the allowlist deliberately: run `ign webdev \
             deploy` without --with-tag-delete first (it removes the route and its stored \
             secret), then install",
            dropped.join(", "),
            live.join(", "),
            new.join(", ")
        ),
    })
}

/// `ign webdev status` — the version-handshake sweep. The 4 always-on
/// routes always ride; scriptExec's version action is probed ONLY
/// when a secret is configured (its header rides along — research OQ4:
/// AuthGated → auth_gated, secret denials → secret_mismatch).
pub async fn webdev_status(
    api: &dyn GatewayApi,
    project: &str,
    secret: Option<&str>,
) -> Result<WebdevStatusResult, CoreError> {
    let mut routes = Vec::new();
    let mut ok = true;
    for route in seam::always_on_routes() {
        let probe = api.webdev_route_probe(project, &route, &[]).await?;
        let row = classify_probe(&route, probe);
        ok &= row.status == RouteStatus::Present;
        routes.push(row);
    }
    if let Some(secret) = secret {
        let probe = api
            .webdev_route_probe(project, SCRIPT_EXEC_ROUTE, &[(SECRET_HEADER, secret)])
            .await?;
        // scriptExec never gates `ok` — it ships on explicit request.
        routes.push(classify_probe(SCRIPT_EXEC_ROUTE, probe));
    }
    Ok(WebdevStatusResult {
        project: project.to_string(),
        routes,
        ok,
    })
}

/// The cheap precondition every WebDev-DEPENDENT command runs first
/// (05-04's tags family onward): probe the canonical `tags` route and
/// refuse with the actionable matrix — absent → `routes_not_deployed`
/// (exit 6, hint names `ign webdev deploy`), version mismatch →
/// `route_version_mismatch` (hint direction-aware: redeploy or update
/// ign), unlicensed → `webdev_unlicensed`. No auto-upgrade magic.
pub async fn webdev_precondition(api: &dyn GatewayApi, project: &str) -> Result<(), CoreError> {
    const ROUTE: &str = "tags";
    let endpoint = seam::route_url(project, ROUTE);
    match api.webdev_route_probe(project, ROUTE, &[]).await? {
        RouteProbe::Present { route_version } if route_version == bundle::ROUTE_BUNDLE_VERSION => {
            Ok(())
        }
        RouteProbe::Present { route_version } => Err(CoreError::RouteVersionMismatch {
            route: ROUTE.to_string(),
            deployed: route_version,
            expected: bundle::ROUTE_BUNDLE_VERSION.to_string(),
            endpoint: Some(endpoint),
        }),
        RouteProbe::Absent => Err(CoreError::RoutesNotDeployed {
            project: project.to_string(),
            route: ROUTE.to_string(),
            endpoint: Some(endpoint),
        }),
        RouteProbe::Unlicensed => Err(CoreError::WebdevUnlicensed {
            endpoint: Some(endpoint),
        }),
        RouteProbe::AuthGated => Err(CoreError::Auth {
            status: 401,
            endpoint: Some(endpoint),
        }),
        RouteProbe::Denied {
            code,
            message,
            traceback,
        } => {
            let mut full = message;
            if let Some(traceback) = traceback {
                full.push_str("\nroute traceback: ");
                full.push_str(&traceback);
            }
            Err(CoreError::WebdevRouteError {
                code,
                message: full,
                endpoint: Some(endpoint),
            })
        }
    }
}

/// Map one probe onto a status row (the matrix the sweep reports and
/// the precondition refuses on).
fn classify_probe(route: &str, probe: RouteProbe) -> RouteStatusRow {
    let expected = bundle::ROUTE_BUNDLE_VERSION;
    match probe {
        RouteProbe::Present { route_version } => {
            let status = if route_version == expected {
                RouteStatus::Present
            } else {
                RouteStatus::VersionMismatch
            };
            RouteStatusRow {
                route: route.to_string(),
                status,
                deployed_version: Some(route_version),
                expected_version: Some(expected.to_string()),
            }
        }
        RouteProbe::Denied { code, .. } => {
            // With the shipped routes the version action's only
            // denials are the secret gate's (the gate runs before
            // dispatch); any OTHER code means the path holds a route
            // this CLI does not recognize — the redeploy advice that
            // version_mismatch carries is the honest fix either way.
            let status = if code == "secret_required" || code == "secret_mismatch" {
                RouteStatus::SecretMismatch
            } else {
                RouteStatus::VersionMismatch
            };
            absent_row(route, status)
        }
        RouteProbe::Absent => absent_row(route, RouteStatus::Absent),
        RouteProbe::Unlicensed => absent_row(route, RouteStatus::Unlicensed),
        RouteProbe::AuthGated => absent_row(route, RouteStatus::AuthGated),
    }
}

/// A row for every probe state that answered no version.
fn absent_row(route: &str, status: RouteStatus) -> RouteStatusRow {
    RouteStatusRow {
        route: route.to_string(),
        status,
        deployed_version: None,
        expected_version: Some(bundle::ROUTE_BUNDLE_VERSION.to_string()),
    }
}

/// 32 random bytes, hex-encoded (64 chars) — via the `getrandom`
/// crate (already in the tree via ring; 0.2 pinned to dedupe).
/// The pre-Windows `/dev/urandom` read is gone with the reversal of
/// the no-Windows decision: getrandom rides the OS CSPRNG everywhere
/// (BCryptGenRandom on Windows, getrandom(2)/urandom on unix).
fn generate_secret() -> Result<String, CoreError> {
    let mut bytes = [0u8; 32];
    getrandom::getrandom(&mut bytes)
        .map_err(|err| CoreError::Internal(format!("cannot generate the deploy secret: {err}")))?;
    Ok(bytes.iter().map(|byte| format!("{byte:02x}")).collect())
}

#[cfg(test)]
mod tests {
    use super::{
        RouteProbe, RouteStatus, TagDeleteRequest, WebdevDeployResult, generate_secret,
        webdev_deploy, webdev_deploy_with, webdev_precondition, webdev_status,
    };
    use crate::client::GatewayApi;
    use crate::client::projects::ImportOutcome;
    use crate::error::CoreError;
    use crate::webdev::ROUTE_BUNDLE_VERSION as BUNDLE_VERSION;
    use std::path::PathBuf;

    /// A scripted double: probes answer from a lookup, the import
    /// callback packs the zip for inspection (recorded through a
    /// Mutex so the closure stays `Fn` — `&self` callable — and the
    /// rig `Sync`, both required by `&dyn GatewayApi`). Everything
    /// else is unreachable (the established action-double shape).
    struct WebdevRig {
        probe: fn(&str) -> Result<RouteProbe, CoreError>,
        /// What [`GatewayApi::gateway_transport`] answers (`None`: a double
        /// with no URL of its own).
        transport: Option<(url::Url, bool)>,
        import: Box<dyn Fn(Vec<u8>, bool) -> Result<ImportOutcome, CoreError> + Send + Sync>,
    }

    fn present(version: &str) -> Result<RouteProbe, CoreError> {
        Ok(RouteProbe::Present {
            route_version: version.to_string(),
        })
    }

    fn ok_import() -> ImportOutcome {
        ImportOutcome {
            response: serde_json::json!({"success": true}),
        }
    }

    #[async_trait::async_trait]
    impl GatewayApi for WebdevRig {
        fn gateway_transport(&self) -> Option<(url::Url, bool)> {
            self.transport.clone()
        }
        async fn thread_dump(
            &self,
        ) -> Result<crate::client::threads::FormattedThreadDump, CoreError> {
            unreachable!("not part of this action")
        }
        async fn thread_deadlocks(
            &self,
        ) -> Result<crate::client::threads::DeadlocksWire, CoreError> {
            unreachable!("not part of this action")
        }
        async fn uninstall_module(
            &self,
            _gateway_module_id: &str,
            _module_id: &str,
        ) -> Result<(), CoreError> {
            unreachable!("not part of this action")
        }
        async fn bundle_generate(
            &self,
        ) -> Result<crate::client::diagnostics::BundleStatusWire, CoreError> {
            unreachable!("not part of this action")
        }
        async fn bundle_status(
            &self,
        ) -> Result<crate::client::diagnostics::BundleStatusWire, CoreError> {
            unreachable!("not part of this action")
        }
        async fn bundle_download(
            &self,
            _out: &std::path::Path,
        ) -> Result<crate::client::projects::ExportMeta, CoreError> {
            unreachable!("not part of this action")
        }
        async fn tag_provider_list(
            &self,
            _query: &crate::client::query::ListQuery,
        ) -> Result<
            crate::client::query::ListEnvelope<crate::client::tags::TagProviderRecord>,
            CoreError,
        > {
            unreachable!("not part of this action")
        }
        async fn tag_provider_find(
            &self,
            _name: &str,
        ) -> Result<crate::client::tags::TagProviderRecord, CoreError> {
            unreachable!("not part of this action")
        }
        async fn tag_provider_create(
            &self,
            _body: &[crate::client::tags::TagProviderCreate],
        ) -> Result<(), CoreError> {
            unreachable!("not part of this action")
        }
        async fn tag_provider_delete(
            &self,
            _name: &str,
            _signature: &str,
        ) -> Result<(), CoreError> {
            unreachable!("not part of this action")
        }
        async fn webdev_route_probe(
            &self,
            _project: &str,
            route: &str,
            _extra_headers: &[(&str, &str)],
        ) -> Result<RouteProbe, CoreError> {
            (self.probe)(route)
        }
        async fn project_import(
            &self,
            _name: &str,
            zip: Vec<u8>,
            overwrite: bool,
        ) -> Result<ImportOutcome, CoreError> {
            (self.import)(zip, overwrite)
        }
        async fn gateway_info(&self) -> Result<crate::client::version::GatewayInfo, CoreError> {
            unreachable!("not part of this action")
        }
        async fn overview(&self) -> Result<crate::client::status::Overview, CoreError> {
            unreachable!("not part of this action")
        }
        async fn status_ping(&self) -> Result<crate::client::status::StatusPing, CoreError> {
            unreachable!("not part of this action")
        }
        async fn modules(
            &self,
            _quarantined: bool,
            _query: &crate::client::query::ListQuery,
        ) -> Result<crate::client::query::ListEnvelope<crate::client::status::ModuleInfo>, CoreError>
        {
            unreachable!("not part of this action")
        }
        async fn metrics_current(
            &self,
        ) -> Result<crate::client::metrics::CurrentGauges, CoreError> {
            unreachable!("not part of this action")
        }
        async fn metrics_historic(
            &self,
        ) -> Result<crate::client::metrics::PerformanceCharts, CoreError> {
            unreachable!("not part of this action")
        }
        async fn metrics_threads(&self) -> Result<crate::client::metrics::ThreadCounts, CoreError> {
            unreachable!("not part of this action")
        }
        async fn designers(
            &self,
            _query: &crate::client::query::ListQuery,
        ) -> Result<
            crate::client::query::ListEnvelope<crate::client::sessions::DesignerInfo>,
            CoreError,
        > {
            unreachable!("not part of this action")
        }
        async fn perspective_sessions(
            &self,
            _query: &crate::client::query::ListQuery,
        ) -> Result<
            crate::client::query::ListEnvelope<crate::client::sessions::PerspectiveSession>,
            CoreError,
        > {
            unreachable!("not part of this action")
        }
        async fn vision_clients(
            &self,
            _query: &crate::client::query::ListQuery,
        ) -> Result<
            crate::client::query::ListEnvelope<crate::client::sessions::VisionClient>,
            CoreError,
        > {
            unreachable!("not part of this action")
        }
        async fn terminate_perspective_session(
            &self,
            _id: &str,
            _message: Option<&str>,
        ) -> Result<(), CoreError> {
            unreachable!("not part of this action")
        }
        async fn terminate_vision_client(&self, _id: &str) -> Result<(), CoreError> {
            unreachable!("not part of this action")
        }
        async fn prune_designer(&self, _id: &str) -> Result<(), CoreError> {
            unreachable!("not part of this action")
        }
        async fn database_connections(
            &self,
        ) -> Result<
            crate::client::query::ListEnvelope<crate::client::connections::GatewayConnection>,
            CoreError,
        > {
            unreachable!("not part of this action")
        }
        async fn opc_connections(
            &self,
        ) -> Result<
            crate::client::query::ListEnvelope<crate::client::connections::GatewayConnection>,
            CoreError,
        > {
            unreachable!("not part of this action")
        }
        async fn logs(
            &self,
            _filter: &crate::client::logs::LogQuery,
        ) -> Result<crate::client::query::ListEnvelope<crate::client::logs::LogEntry>, CoreError>
        {
            unreachable!("not part of this action")
        }
        async fn logs_download(&self) -> Result<crate::client::logs::LogDownload, CoreError> {
            unreachable!("not part of this action")
        }
        async fn loggers(
            &self,
            _query: &crate::client::query::ListQuery,
        ) -> Result<crate::client::query::ListEnvelope<crate::client::logs::LoggerInfo>, CoreError>
        {
            unreachable!("not part of this action")
        }
        async fn set_logger_level(&self, _logger: &str, _level: &str) -> Result<(), CoreError> {
            unreachable!("not part of this action")
        }
        async fn reset_logger_levels(&self) -> Result<(), CoreError> {
            unreachable!("not part of this action")
        }
        async fn restart(&self) -> Result<(), CoreError> {
            unreachable!("not part of this action")
        }
        async fn scan_projects(&self) -> Result<(), CoreError> {
            unreachable!("not part of this action")
        }
        async fn security_properties(
            &self,
        ) -> Result<crate::client::restart::SecurityProperties, CoreError> {
            unreachable!("not part of this action")
        }
        async fn webdev_route_status(&self, _route: &str) -> Result<u16, CoreError> {
            unreachable!("not part of this action")
        }
        async fn webdev_route_call(
            &self,
            _project: &str,
            _route: &str,
            _body: &serde_json::Value,
            _extra_headers: &[(&str, &str)],
        ) -> Result<serde_json::Value, CoreError> {
            unreachable!("not part of this action")
        }
        async fn projects(
            &self,
            _query: &crate::client::query::ListQuery,
        ) -> Result<
            crate::client::query::ListEnvelope<crate::client::projects::ProjectRecord>,
            CoreError,
        > {
            unreachable!("not part of this action")
        }
        async fn project_find(
            &self,
            _name: &str,
        ) -> Result<crate::client::projects::ProjectRecord, CoreError> {
            unreachable!("not part of this action")
        }
        async fn project_create(
            &self,
            _body: &crate::client::projects::ProjectCreate,
        ) -> Result<(), CoreError> {
            unreachable!("not part of this action")
        }
        async fn project_copy(&self, _from: &str, _to: &str) -> Result<(), CoreError> {
            unreachable!("not part of this action")
        }
        async fn project_rename(&self, _name: &str, _new_name: &str) -> Result<(), CoreError> {
            unreachable!("not part of this action")
        }
        async fn project_modify(
            &self,
            _name: &str,
            _body: &crate::client::projects::ProjectModify,
        ) -> Result<(), CoreError> {
            unreachable!("not part of this action")
        }
        async fn project_delete(&self, _name: &str) -> Result<(), CoreError> {
            unreachable!("not part of this action")
        }
        async fn project_export_to_file(
            &self,
            _name: &str,
            _out: &std::path::Path,
        ) -> Result<crate::client::projects::ExportMeta, CoreError> {
            unreachable!("not part of this action")
        }
        async fn trial_status_wire(&self) -> Result<crate::client::trial::TrialWire, CoreError> {
            unreachable!("not part of this action")
        }
        async fn banners(&self) -> Result<crate::client::trial::BannerSet, CoreError> {
            unreachable!("not part of this action")
        }
        async fn trial_reset_wire(&self) -> Result<crate::client::trial::TrialWire, CoreError> {
            unreachable!("not part of this action")
        }
        async fn backup_download(
            &self,
            _out: &std::path::Path,
            _backup_type: crate::client::backup::BackupType,
        ) -> Result<crate::client::projects::ExportMeta, CoreError> {
            unreachable!("not part of this action")
        }
        async fn backup_restore(&self, _gwbk: &std::path::Path) -> Result<(), CoreError> {
            unreachable!("not part of this action")
        }
        async fn eam_task_history(
            &self,
            _limit: Option<u32>,
            _search: Option<&str>,
        ) -> Result<crate::client::query::ListEnvelope<crate::client::eam::EamHistoryItem>, CoreError>
        {
            unreachable!("not part of this action")
        }
        async fn eam_task_definitions(
            &self,
        ) -> Result<crate::client::query::ListEnvelope<crate::client::eam::EamTaskRecord>, CoreError>
        {
            unreachable!("not part of this action")
        }
        async fn eam_task_find(
            &self,
            _name: &str,
        ) -> Result<crate::client::eam::EamTaskRecord, CoreError> {
            unreachable!("not part of this action")
        }
        async fn eam_task_create(&self, _definition: &serde_json::Value) -> Result<(), CoreError> {
            unreachable!("not part of this action")
        }
        async fn eam_task_force(&self, _owner: &str, _name: &str) -> Result<(), CoreError> {
            unreachable!("not part of this action")
        }
        async fn eam_task_suspend(&self, _name: &str) -> Result<(), CoreError> {
            unreachable!("not part of this action")
        }
        async fn eam_task_resume(&self, _name: &str) -> Result<(), CoreError> {
            unreachable!("not part of this action")
        }
        async fn eam_task_cancel(&self, _name: &str) -> Result<(), CoreError> {
            unreachable!("not part of this action")
        }
        async fn eam_tasks_scheduled(
            &self,
            _running: bool,
        ) -> Result<Vec<crate::client::eam::EamScheduledTask>, CoreError> {
            unreachable!("not part of this action")
        }
        async fn eam_task_modify(
            &self,
            _definition: &serde_json::Value,
        ) -> Result<Option<crate::client::eam::ModifyOutcome>, CoreError> {
            unreachable!("not part of this action")
        }
        async fn eam_task_delete(
            &self,
            _name: &str,
            _signature: &str,
            _confirm: bool,
        ) -> Result<crate::client::eam::DeleteOutcome, CoreError> {
            unreachable!("not part of this action")
        }
        async fn api_call(
            &self,
            _call: &crate::client::apicall::ApiCallRequest,
        ) -> Result<crate::client::apicall::ApiCallData, CoreError> {
            unreachable!("not part of this action")
        }
        async fn license_status(
            &self,
        ) -> Result<crate::client::license::LicenseStatusWire, CoreError> {
            unreachable!("not part of this action")
        }
        async fn redundancy_status(
            &self,
        ) -> Result<crate::client::redundancy::RedundancyStatusWire, CoreError> {
            unreachable!("not part of this action")
        }
        async fn gan_status(&self) -> Result<crate::client::gan::GanStatusWire, CoreError> {
            unreachable!("not part of this action")
        }
    }

    /// Temp config with one `dev` profile (no auth — the action never
    /// resolves credentials itself).
    fn temp_config() -> (tempfile::TempDir, PathBuf) {
        let dir = tempfile::tempdir().expect("tempdir");
        let path = dir.path().join("config.toml");
        std::fs::write(
            &path,
            "active = \"dev\"\n\n[profiles.dev]\nurl = \"http://localhost:9088/\"\n",
        )
        .expect("write config");
        (dir, path)
    }

    /// Read the stored secret back out of a config (test eyes only).
    fn stored_secret(path: &std::path::Path) -> Option<String> {
        crate::config::load(path)
            .expect("config reloads")
            .profiles
            .get("dev")
            .and_then(|profile| profile.webdev_secret.clone())
    }

    /// A rig whose import always succeeds and records nothing.
    fn importing_rig() -> WebdevRig {
        WebdevRig {
            probe: |_| unreachable!("deploy never probes"),
            transport: None,
            import: Box::new(|_zip, _overwrite| Ok(ok_import())),
        }
    }

    /// Deploy WITHOUT --with-script-exec never ships the route and
    /// never stores a secret.
    #[tokio::test]
    async fn deploy_without_script_exec_ships_only_the_always_on_bundle() {
        let (dir, config) = temp_config();
        let seen_zip = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
        let recorder = std::sync::Arc::clone(&seen_zip);
        let rig = WebdevRig {
            probe: |_| unreachable!("deploy never probes"),
            transport: None,
            import: Box::new(move |zip, overwrite| {
                assert!(overwrite, "deploy ALWAYS overwrite-imports");
                *recorder.lock().expect("zip lock") = zip;
                Ok(ok_import())
            }),
        };
        let result = webdev_deploy(&rig, "ign-cli", false, false, &config, "dev", false)
            .await
            .expect("plain deploy");
        assert_eq!(
            result.routes,
            vec!["tags", "tagConfig", "alarms", "tagHistory"]
        );
        assert!(!result.script_exec);
        assert!(!result.secret_rotated);
        assert_eq!(result.import["success"], true);

        // The uploaded zip carries NO scriptExec member and no secret
        // landed in the config.
        let names = member_names(&seen_zip.lock().expect("zip lock"));
        assert!(names.iter().all(|name| !name.contains("scriptExec")));
        assert_eq!(stored_secret(&config), None);
        let _ = dir; // keeps the tempdir alive for the asserts above
    }

    /// Deploy --with-script-exec with NO stored secret: generates,
    /// persists 0600, bakes into the zip — and the SERIALIZED result
    /// never carries the value (the redaction pin).
    #[tokio::test]
    async fn deploy_with_script_exec_generates_and_redacts_the_secret() {
        let (dir, config) = temp_config();
        let seen_zip = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
        let recorder = std::sync::Arc::clone(&seen_zip);
        let rig = WebdevRig {
            probe: |_| unreachable!("deploy never probes"),
            transport: None,
            import: Box::new(move |zip, _| {
                *recorder.lock().expect("zip lock") = zip;
                Ok(ok_import())
            }),
        };
        let result = webdev_deploy(&rig, "ign-cli", true, false, &config, "dev", false)
            .await
            .expect("scriptExec deploy");
        assert_eq!(
            result.routes,
            vec!["tags", "tagConfig", "alarms", "tagHistory", "scriptExec"]
        );
        assert!(result.script_exec && result.secret_rotated);

        let secret = stored_secret(&config).expect("secret persisted");
        assert_eq!(secret.len(), 64, "32 bytes hex-encoded");
        assert!(
            secret.chars().all(|c| c.is_ascii_hexdigit()),
            "hex alphabet: {secret}"
        );
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let mode = std::fs::metadata(&config)
                .expect("config stat")
                .permissions()
                .mode();
            assert_eq!(mode & 0o777, 0o600, "the save path re-asserts 0600");
        }

        // The baked route carries the secret; the serialized result
        // does NOT (redaction — Phase 1's canary pattern).
        let do_post = member(
            &seen_zip.lock().expect("zip lock"),
            "com.inductiveautomation.webdev/resources/cli/scriptExec/doPost.py",
        );
        assert!(String::from_utf8_lossy(&do_post).contains(&secret));
        let serialized = serde_json::to_string(&result).expect("result serializes");
        assert!(!serialized.contains(&secret), "redaction: {serialized}");
        let envelope_check: WebdevDeployResult = result;
        let again = serde_json::to_string(&envelope_check).expect("serializes");
        assert!(!again.contains(&secret));
        let _ = dir;
    }

    /// --rotate-secret regenerates even when a secret exists; a plain
    /// re-deploy reuses the stored one unchanged.
    #[tokio::test]
    async fn rotate_regenerates_and_plain_redeploy_reuses() {
        let (dir, config) = temp_config();
        // Seed a stored secret.
        let mut seeded = crate::config::load(&config).expect("load");
        seeded.profiles.get_mut("dev").unwrap().webdev_secret = Some("aa11".into());
        crate::config::save(&config, &seeded).expect("seed save");

        // Plain scriptExec redeploy: reuses `aa11`, rotated=false.
        let seen = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
        let recorder = std::sync::Arc::clone(&seen);
        let rig = WebdevRig {
            probe: |_| unreachable!(),
            transport: None,
            import: Box::new(move |zip, _| {
                *recorder.lock().expect("lock") = zip;
                Ok(ok_import())
            }),
        };
        let result = webdev_deploy(&rig, "ign-cli", true, false, &config, "dev", false)
            .await
            .expect("reuse deploy");
        assert!(!result.secret_rotated);
        assert_eq!(stored_secret(&config).as_deref(), Some("aa11"));
        let seen = seen.lock().expect("lock").clone();
        let do_post = member(
            &seen,
            "com.inductiveautomation.webdev/resources/cli/scriptExec/doPost.py",
        );
        assert!(
            String::from_utf8_lossy(&do_post).contains("aa11"),
            "the STORED secret rode the zip's scriptExec member"
        );

        // --rotate-secret: a fresh 64-char hex replaces it.
        let result = webdev_deploy(
            &importing_rig(),
            "ign-cli",
            true,
            true,
            &config,
            "dev",
            false,
        )
        .await
        .expect("rotate deploy");
        assert!(result.secret_rotated);
        let rotated = stored_secret(&config).expect("rotated secret stored");
        assert_eq!(rotated.len(), 64);
        assert_ne!(rotated, "aa11");
        let _ = dir;
    }

    /// The status matrix: every probe state maps onto its row; ok is
    /// gated ONLY by the always-on routes; scriptExec rides along
    /// exactly when a secret is configured.
    #[tokio::test]
    async fn status_maps_the_full_probe_matrix() {
        // All present + matching: ok=true.
        let healthy = WebdevRig {
            probe: |_| present(BUNDLE_VERSION),
            transport: None,
            import: Box::new(|_, _| unreachable!("status never imports")),
        };
        let result = webdev_status(&healthy, "ign-cli", None)
            .await
            .expect("status sweep");
        assert_eq!(result.routes.len(), 4);
        assert!(
            result
                .routes
                .iter()
                .all(|row| row.status == RouteStatus::Present)
        );
        assert!(result.ok);

        // One route absent, one mismatched: ok=false, degradation is
        // DATA (rows carry their own verdicts).
        let degraded = WebdevRig {
            transport: None,
            probe: |route| {
                Ok(match route {
                    "tags" => RouteProbe::Absent,
                    "tagConfig" => present("9.9.9").expect("fixture"),
                    "alarms" => present(BUNDLE_VERSION).expect("fixture"),
                    "tagHistory" => RouteProbe::Unlicensed,
                    "scriptExec" => RouteProbe::Denied {
                        code: "secret_mismatch".into(),
                        message: "mismatch".into(),
                        traceback: None,
                    },
                    _ => RouteProbe::AuthGated,
                })
            },
            import: Box::new(|_, _| unreachable!()),
        };
        let result = webdev_status(&degraded, "ign-cli", Some("stored-secret"))
            .await
            .expect("degraded sweep still completes");
        let by_route = |name: &str| {
            result
                .routes
                .iter()
                .find(|row| row.route == name)
                .unwrap_or_else(|| panic!("{name} row"))
        };
        assert_eq!(by_route("tags").status, RouteStatus::Absent);
        assert_eq!(by_route("tagConfig").status, RouteStatus::VersionMismatch);
        assert_eq!(
            by_route("tagConfig").deployed_version.as_deref(),
            Some("9.9.9")
        );
        assert_eq!(by_route("tagHistory").status, RouteStatus::Unlicensed);
        assert_eq!(by_route("scriptExec").status, RouteStatus::SecretMismatch);
        assert!(!result.ok);
        // scriptExec never gates ok: the healthy sweep with a secret
        // whose probe denies stays ok=true.
        let gated_exec = WebdevRig {
            transport: None,
            probe: |route| {
                if route == "scriptExec" {
                    Ok(RouteProbe::AuthGated)
                } else {
                    present(BUNDLE_VERSION)
                }
            },
            import: Box::new(|_, _| unreachable!()),
        };
        let result = webdev_status(&gated_exec, "ign-cli", Some("s"))
            .await
            .expect("sweep");
        assert!(result.ok, "scriptExec never gates ok");
        assert_eq!(result.routes.len(), 5);
    }

    /// THE precondition refusal matrix (must-have truth #3): before
    /// deploy → routes_not_deployed naming `ign webdev deploy`;
    /// mismatch → route_version_mismatch with both versions named.
    #[tokio::test]
    async fn precondition_refuses_the_undeployed_and_mismatched() {
        let undeployed = WebdevRig {
            probe: |_| Ok(RouteProbe::Absent),
            transport: None,
            import: Box::new(|_, _| unreachable!()),
        };
        let err = webdev_precondition(&undeployed, "ign-cli")
            .await
            .expect_err("absent refuses");
        assert_eq!(err.code(), "routes_not_deployed");
        assert_eq!(err.exit_code(), 6);
        assert!(
            err.hint().unwrap().contains("ign webdev deploy"),
            "hint names the fix"
        );

        let older = WebdevRig {
            probe: |_| present("0.9.0"),
            transport: None,
            import: Box::new(|_, _| unreachable!()),
        };
        let err = webdev_precondition(&older, "ign-cli")
            .await
            .expect_err("older refuses");
        assert_eq!(err.code(), "route_version_mismatch");
        assert!(
            err.to_string().contains("0.9.0") && err.to_string().contains(BUNDLE_VERSION),
            "both versions named: {err}"
        );

        let matching = WebdevRig {
            probe: |_| present(BUNDLE_VERSION),
            transport: None,
            import: Box::new(|_, _| unreachable!()),
        };
        webdev_precondition(&matching, "ign-cli")
            .await
            .expect("matching handshake passes");
    }

    const TAG_DELETE_DO_POST: &str =
        "com.inductiveautomation.webdev/resources/cli/tagDelete/doPost.py";
    const TAG_DELETE_CONFIG: &str =
        "com.inductiveautomation.webdev/resources/cli/tagDelete/config.json";

    /// A project repo whose `deploy/project.yaml` owns `providers` (a YAML
    /// flow list such as `[P26Del]`).
    fn temp_repo(providers: &str) -> tempfile::TempDir {
        let dir = tempfile::tempdir().expect("tempdir");
        std::fs::create_dir_all(dir.path().join("deploy")).expect("deploy dir");
        std::fs::write(
            dir.path().join("deploy/project.yaml"),
            format!(
                "schema_version: 1\nproject: Demo\nenvironments: [dev]\ntag_providers: {providers}\n"
            ),
        )
        .expect("project.yaml");
        dir
    }

    fn tag_delete_request(repo: &std::path::Path, rotate_secret: bool) -> TagDeleteRequest {
        TagDeleteRequest {
            repos: vec![repo.to_path_buf()],
            user_source: "default".into(),
            role: "FakeDeployRole".into(),
            rotate_secret,
            caller: None,
        }
    }

    fn stored_tag_delete_secret(path: &std::path::Path) -> Option<String> {
        crate::config::load(path)
            .expect("config reloads")
            .profiles
            .get("dev")
            .and_then(|profile| profile.webdev_tag_delete_secret.clone())
    }

    /// A rig whose import must never run (refusals happen before any
    /// gateway request).
    fn no_gateway_rig() -> WebdevRig {
        WebdevRig {
            probe: |_| unreachable!("deploy never probes"),
            transport: None,
            import: Box::new(|_, _| panic!("no gateway request may happen")),
        }
    }

    /// A rig recording the uploaded zip and, at upload time, the stored
    /// tag-delete secret (proving it was persisted BEFORE the upload).
    #[allow(clippy::type_complexity)]
    fn recording_rig(
        config: &std::path::Path,
    ) -> (
        WebdevRig,
        std::sync::Arc<std::sync::Mutex<(Vec<u8>, Option<String>)>>,
    ) {
        let seen = std::sync::Arc::new(std::sync::Mutex::new((Vec::new(), None)));
        let recorder = std::sync::Arc::clone(&seen);
        let config = config.to_path_buf();
        let rig = WebdevRig {
            probe: |_| unreachable!("deploy never probes"),
            transport: None,
            import: Box::new(move |zip, overwrite| {
                assert!(overwrite);
                *recorder.lock().expect("lock") = (zip, stored_tag_delete_secret(&config));
                Ok(ok_import())
            }),
        };
        (rig, seen)
    }

    /// No stored secret: one is generated, persisted 0600 before the
    /// upload and packed; a second deploy reuses it; rotate regenerates.
    /// The result, its Debug form and its JSON carry no secret.
    #[tokio::test]
    async fn tag_delete_secret_generated_persisted_reused_and_rotated() {
        let (dir, config) = temp_config();
        let repo = temp_repo("[P26Del]");
        let (rig, seen) = recording_rig(&config);
        let result = webdev_deploy_with(
            &rig,
            "ign-cli",
            false,
            false,
            &config,
            "dev",
            false,
            Some(tag_delete_request(repo.path(), false)),
        )
        .await
        .expect("tag delete deploy");
        assert!(result.tag_delete && result.tag_delete_secret_rotated);
        assert!(!result.script_exec && !result.secret_rotated);
        assert_eq!(result.tag_delete_providers, vec!["P26Del".to_string()]);
        assert_eq!(
            result.routes,
            vec!["tags", "tagConfig", "alarms", "tagHistory", "tagDelete"]
        );

        let secret = stored_tag_delete_secret(&config).expect("secret persisted");
        assert_eq!(secret.len(), 64);
        assert!(
            secret
                .bytes()
                .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
        );
        assert_eq!(
            stored_secret(&config),
            None,
            "the scriptExec secret is untouched"
        );
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let mode = std::fs::metadata(&config)
                .expect("stat")
                .permissions()
                .mode();
            assert_eq!(mode & 0o777, 0o600);
        }
        let (zip, at_upload) = seen.lock().expect("lock").clone();
        assert_eq!(
            at_upload.as_deref(),
            Some(secret.as_str()),
            "persisted before upload"
        );
        assert!(String::from_utf8_lossy(&member(&zip, TAG_DELETE_DO_POST)).contains(&secret));
        let packed_config: serde_json::Value =
            serde_json::from_slice(&member(&zip, TAG_DELETE_CONFIG)).expect("config.json");
        assert_eq!(packed_config["doPost"]["required-roles"], "FakeDeployRole");
        assert_eq!(packed_config["doPost"]["user-source"], "default");

        let serialized = serde_json::to_string(&result).expect("serializes");
        assert!(!serialized.contains(&secret), "redaction: {serialized}");
        assert!(serialized.contains("\"tag_delete\":true"));
        assert!(serialized.contains("\"tag_delete_secret_rotated\":true"));
        assert!(serialized.contains("\"tag_delete_providers\":[\"P26Del\"]"));
        assert!(!format!("{result:?}").contains(&secret));

        // Reuse.
        let (rig, seen) = recording_rig(&config);
        let result = webdev_deploy_with(
            &rig,
            "ign-cli",
            false,
            false,
            &config,
            "dev",
            false,
            Some(tag_delete_request(repo.path(), false)),
        )
        .await
        .expect("reuse deploy");
        assert!(result.tag_delete && !result.tag_delete_secret_rotated);
        assert_eq!(
            stored_tag_delete_secret(&config).as_deref(),
            Some(secret.as_str())
        );
        let (zip, _) = seen.lock().expect("lock").clone();
        assert!(String::from_utf8_lossy(&member(&zip, TAG_DELETE_DO_POST)).contains(&secret));

        // Rotate.
        let result = webdev_deploy_with(
            &importing_rig(),
            "ign-cli",
            false,
            false,
            &config,
            "dev",
            false,
            Some(tag_delete_request(repo.path(), true)),
        )
        .await
        .expect("rotate deploy");
        assert!(result.tag_delete_secret_rotated);
        let rotated = stored_tag_delete_secret(&config).expect("rotated");
        assert_eq!(rotated.len(), 64);
        assert_ne!(rotated, secret);
        let _ = dir;
    }

    /// The scriptExec and tag-delete secrets are independent: rotating one
    /// never changes the other, and both routes compose in one import.
    #[tokio::test]
    async fn script_exec_and_tag_delete_secrets_are_independent() {
        let (dir, config) = temp_config();
        let repo = temp_repo("[P26Del, Other]");
        let (rig, seen) = recording_rig(&config);
        let result = webdev_deploy_with(
            &rig,
            "ign-cli",
            true,
            false,
            &config,
            "dev",
            true,
            Some(tag_delete_request(repo.path(), false)),
        )
        .await
        .expect("composed deploy");
        assert!(result.script_exec && result.tag_delete);
        assert_eq!(
            result.tag_delete_providers,
            vec!["P26Del".to_string(), "Other".to_string()]
        );
        assert!(result.routes.contains(&"scriptExec".to_string()));
        assert!(result.routes.contains(&"tagDelete".to_string()));
        assert!(
            result
                .routes
                .iter()
                .any(|route| route.starts_with("testing/"))
        );
        let names = member_names(&seen.lock().expect("lock").0);
        assert!(names.iter().any(|name| name.contains("cli/scriptExec/")));
        assert!(names.iter().any(|name| name.contains("cli/tagDelete/")));

        let exec_secret = stored_secret(&config).expect("scriptExec secret");
        let delete_secret = stored_tag_delete_secret(&config).expect("tagDelete secret");
        assert_ne!(exec_secret, delete_secret);

        // Rotate scriptExec only.
        let result = webdev_deploy_with(
            &importing_rig(),
            "ign-cli",
            true,
            true,
            &config,
            "dev",
            false,
            Some(tag_delete_request(repo.path(), false)),
        )
        .await
        .expect("rotate scriptExec");
        assert!(result.secret_rotated && !result.tag_delete_secret_rotated);
        assert_ne!(
            stored_secret(&config).as_deref(),
            Some(exec_secret.as_str())
        );
        assert_eq!(
            stored_tag_delete_secret(&config).as_deref(),
            Some(delete_secret.as_str())
        );

        // Rotate tag delete only.
        let exec_now = stored_secret(&config).expect("scriptExec secret");
        let result = webdev_deploy_with(
            &importing_rig(),
            "ign-cli",
            true,
            false,
            &config,
            "dev",
            false,
            Some(tag_delete_request(repo.path(), true)),
        )
        .await
        .expect("rotate tag delete");
        assert!(!result.secret_rotated && result.tag_delete_secret_rotated);
        assert_eq!(stored_secret(&config).as_deref(), Some(exec_now.as_str()));
        assert_ne!(
            stored_tag_delete_secret(&config).as_deref(),
            Some(delete_secret.as_str())
        );
        let serialized = serde_json::to_string(&result).expect("serializes");
        for secret in [
            exec_now,
            stored_tag_delete_secret(&config).expect("tagDelete secret"),
        ] {
            assert!(!serialized.contains(&secret));
            assert!(!format!("{result:?}").contains(&secret));
        }
        let _ = dir;
    }

    /// The allowlist comes from the repo; an empty list, `System` in any
    /// case, a bad role or user source, or a broken repo refuse before any
    /// gateway request and before any secret is stored. No error text
    /// carries a stored secret.
    #[tokio::test]
    async fn tag_delete_refusals_happen_before_any_gateway_request() {
        let (dir, config) = temp_config();
        let mut seeded = crate::config::load(&config).expect("load");
        seeded.profiles.get_mut("dev").expect("dev").webdev_secret =
            Some("fakeexecsecretvalue".into());
        crate::config::save(&config, &seeded).expect("seed");

        for (providers, code) in [
            ("[]", "invalid_input"),
            ("[System]", "invalid_input"),
            ("[P1, system]", "invalid_input"),
        ] {
            let repo = temp_repo(providers);
            let err = webdev_deploy_with(
                &no_gateway_rig(),
                "ign-cli",
                true,
                false,
                &config,
                "dev",
                false,
                Some(tag_delete_request(repo.path(), false)),
            )
            .await
            .expect_err("refused");
            assert_eq!(err.code(), code, "{providers}: {err}");
            assert!(!err.to_string().contains("fakeexecsecretvalue"));
        }

        let repo = temp_repo("[P26Del]");
        let mut bad_role = tag_delete_request(repo.path(), false);
        bad_role.role = "x'y".into();
        let err = webdev_deploy_with(
            &no_gateway_rig(),
            "ign-cli",
            false,
            false,
            &config,
            "dev",
            false,
            Some(bad_role),
        )
        .await
        .expect_err("bad role");
        assert_eq!(err.code(), "invalid_input");
        assert!(err.to_string().contains("role"), "{err}");

        let empty_repo = tempfile::tempdir().expect("tempdir");
        let err = webdev_deploy_with(
            &no_gateway_rig(),
            "ign-cli",
            false,
            false,
            &config,
            "dev",
            false,
            Some(tag_delete_request(empty_repo.path(), false)),
        )
        .await
        .expect_err("no project.yaml");
        assert_eq!(err.code(), "deploy_tree_invalid");

        assert_eq!(stored_tag_delete_secret(&config), None, "nothing stored");
        let _ = dir;
    }

    /// 26-REVIEW WR-07: the tagDelete secret goes only over https or to a
    /// loopback host (and with `ssl_verify` on for a remote one), the route
    /// client's own rule: plain http to a remote host, or unverified TLS to
    /// one, refuses `tag_delete_not_configured` before any secret is stored
    /// and before any gateway request.
    #[tokio::test]
    async fn tag_delete_install_refuses_an_unsafe_transport() {
        // The double reports no URL, so the check falls back to the stored
        // profile; no env read happens (WR-D2).
        let repo = temp_repo("[P26Del]");
        for (url, ssl_verify, why) in [
            ("http://gateway.example.com:8088/", true, "plain http"),
            (
                "https://gateway.example.com:8043/",
                false,
                "ssl_verify false",
            ),
        ] {
            let (dir, config) = temp_config();
            let mut seeded = crate::config::load(&config).expect("load");
            let profile = seeded.profiles.get_mut("dev").expect("dev");
            profile.url = url::Url::parse(url).expect("url");
            profile.ssl_verify = ssl_verify;
            crate::config::save(&config, &seeded).expect("seed");
            let err = webdev_deploy_with(
                &no_gateway_rig(),
                "ign-cli",
                false,
                false,
                &config,
                "dev",
                false,
                Some(tag_delete_request(repo.path(), false)),
            )
            .await
            .expect_err("refused");
            assert_eq!(err.code(), "tag_delete_not_configured", "{url}: {err}");
            assert!(err.to_string().contains(why), "{url}: {err}");
            assert_eq!(stored_tag_delete_secret(&config), None, "{url}");
            let _ = dir;
        }
    }

    /// 26-REVIEW-D WR-D2: the transport rule checks the URL the import's
    /// client sends to, not a fresh read of `IGNITION_URL`. An env value
    /// set by another test (or changed after the session was built) neither
    /// refuses a safe client nor admits an unsafe one.
    #[test]
    fn tag_delete_transport_check_reads_the_clients_url() {
        let _lock = crate::config::ENV_LOCK.lock().expect("env lock");
        tokio::runtime::Builder::new_current_thread()
            .build()
            .expect("runtime")
            .block_on(transport_check_reads_the_clients_url());
    }

    async fn transport_check_reads_the_clients_url() {
        let repo = temp_repo("[P26Del]");
        let loopback = url::Url::parse("http://localhost:9088/").expect("url");
        let remote = url::Url::parse("http://gateway.example.com:8088/").expect("url");

        // A remote plain-http IGNITION_URL in the env, a loopback client.
        let (dir, config) = temp_config();
        // SAFETY: single-threaded under ENV_LOCK; removed before return.
        unsafe { std::env::set_var("IGNITION_URL", remote.as_str()) };
        let mut rig = importing_rig();
        rig.transport = Some((loopback, true));
        let result = webdev_deploy_with(
            &rig,
            "ign-cli",
            false,
            false,
            &config,
            "dev",
            false,
            Some(tag_delete_request(repo.path(), false)),
        )
        .await;
        // SAFETY: single-threaded under ENV_LOCK.
        unsafe { std::env::remove_var("IGNITION_URL") };
        let result = result.expect("the client's loopback URL is safe");
        assert!(result.tag_delete);
        let _ = dir;

        // No env, a loopback profile, a remote plain-http client.
        let (dir, config) = temp_config();
        let mut rig = no_gateway_rig();
        rig.transport = Some((remote, true));
        let err = webdev_deploy_with(
            &rig,
            "ign-cli",
            false,
            false,
            &config,
            "dev",
            false,
            Some(tag_delete_request(repo.path(), false)),
        )
        .await
        .expect_err("the client's remote http URL is refused");
        assert_eq!(err.code(), "tag_delete_not_configured", "{err}");
        assert!(err.to_string().contains("plain http"), "{err}");
        assert_eq!(stored_tag_delete_secret(&config), None);
        let _ = dir;
    }

    /// A config store that cannot be written refuses before any gateway
    /// request.
    #[cfg(unix)]
    #[tokio::test]
    async fn tag_delete_broken_config_store_refuses_before_any_gateway_request() {
        use std::os::unix::fs::PermissionsExt;
        let (dir, config) = temp_config();
        let repo = temp_repo("[P26Del]");
        std::fs::set_permissions(&config, std::fs::Permissions::from_mode(0o400))
            .expect("read-only");
        let err = webdev_deploy_with(
            &no_gateway_rig(),
            "ign-cli",
            false,
            false,
            &config,
            "dev",
            false,
            Some(tag_delete_request(repo.path(), false)),
        )
        .await
        .expect_err("unwritable store refuses");
        assert_eq!(err.code(), "config_invalid", "{err}");
        std::fs::set_permissions(&config, std::fs::Permissions::from_mode(0o600)).expect("restore");

        std::fs::write(&config, "not = [valid").expect("break");
        let err = webdev_deploy_with(
            &no_gateway_rig(),
            "ign-cli",
            false,
            false,
            &config,
            "dev",
            false,
            Some(tag_delete_request(repo.path(), false)),
        )
        .await
        .expect_err("unparsable store refuses");
        assert_ne!(err.code(), "internal");
        let _ = dir;
    }

    /// A deploy without the tag-delete request packs no tagDelete member and
    /// reports the three keys as off.
    #[tokio::test]
    async fn deploy_without_tag_delete_packs_no_tag_delete_member() {
        let (dir, config) = temp_config();
        let (rig, seen) = recording_rig(&config);
        let result = webdev_deploy_with(&rig, "ign-cli", true, false, &config, "dev", false, None)
            .await
            .expect("deploy");
        assert!(!result.tag_delete && !result.tag_delete_secret_rotated);
        assert!(result.tag_delete_providers.is_empty());
        assert!(!result.routes.contains(&"tagDelete".to_string()));
        let names = member_names(&seen.lock().expect("lock").0);
        assert!(names.iter().all(|name| !name.contains("tagDelete")));
        assert_eq!(stored_tag_delete_secret(&config), None);
        assert!(result.warnings.is_empty(), "{:?}", result.warnings);
        let _ = dir;
    }

    /// 26-REVIEW-D WR-D4: a deploy without the tag-delete request (the CLI
    /// without `--with-tag-delete`, the TUI, adopt) drops the route, so it
    /// also drops the stored tagDelete secret, before the upload, and warns
    /// naming the removal. The profile then reads not configured. The
    /// deploy itself is not refused, and the warning carries no secret.
    #[tokio::test]
    async fn deploy_without_tag_delete_removes_the_stored_secret_and_warns() {
        let (dir, config) = temp_config();
        let mut seeded = crate::config::load(&config).expect("load");
        seeded
            .profiles
            .get_mut("dev")
            .expect("dev")
            .webdev_tag_delete_secret = Some("a".repeat(64));
        crate::config::save(&config, &seeded).expect("seed");

        // A deploy into another project leaves the route in `ign-cli`, the
        // one the route client calls: the secret stays, no warning.
        let result = webdev_deploy(
            &importing_rig(),
            "Other",
            false,
            false,
            &config,
            "dev",
            false,
        )
        .await
        .expect("deploy");
        assert!(result.warnings.is_empty(), "{:?}", result.warnings);
        assert!(stored_tag_delete_secret(&config).is_some());

        // `webdev_deploy` is the path adopt and the TUI call.
        let (rig, seen) = recording_rig(&config);
        let result = webdev_deploy(&rig, "ign-cli", true, false, &config, "dev", false)
            .await
            .expect("the deploy is not refused");
        assert!(!result.tag_delete);
        assert_eq!(stored_tag_delete_secret(&config), None, "secret removed");
        assert_eq!(seen.lock().expect("lock").1, None, "removed before upload");
        assert_eq!(result.warnings.len(), 1, "{:?}", result.warnings);
        let warning = &result.warnings[0];
        assert!(warning.contains("tagDelete"), "{warning}");
        assert!(warning.contains("removed"), "{warning}");
        assert!(warning.contains("--with-tag-delete"), "{warning}");
        assert!(!warning.contains(&"a".repeat(64)), "{warning}");
        assert!(stored_secret(&config).is_some(), "scriptExec kept");

        let profile = crate::config::load(&config)
            .expect("reload")
            .profiles
            .remove("dev")
            .expect("dev");
        match crate::deploy::tag_delete::tag_delete_config("dev", &profile) {
            crate::deploy::tag_delete::TagDeleteConfig::NotConfigured { missing } => {
                assert!(
                    missing
                        .iter()
                        .any(|part| part == "webdev_tag_delete_secret"),
                    "{missing:?}"
                );
            }
            other => panic!("expected NotConfigured, got {other:?}"),
        }

        // Nothing stored: nothing to remove, no warning.
        let result = webdev_deploy(
            &importing_rig(),
            "ign-cli",
            false,
            false,
            &config,
            "dev",
            false,
        )
        .await
        .expect("deploy");
        assert!(result.warnings.is_empty(), "{:?}", result.warnings);
        let _ = dir;
    }

    /// A deploy that installs the route keeps its secret and warns nothing.
    #[tokio::test]
    async fn deploy_with_tag_delete_keeps_the_secret_and_does_not_warn() {
        let (dir, config) = temp_config();
        let repo = temp_repo("[P26Del]");
        for _ in 0..2 {
            let result = webdev_deploy_with(
                &importing_rig(),
                "ign-cli",
                false,
                false,
                &config,
                "dev",
                false,
                Some(tag_delete_request(repo.path(), false)),
            )
            .await
            .expect("deploy");
            assert!(
                !result.warnings.iter().any(|w| w.contains("removed")),
                "{:?}",
                result.warnings
            );
            assert!(stored_tag_delete_secret(&config).is_some());
        }
        let _ = dir;
    }

    /// A temp config whose `dev` profile points at `url` and holds a stored
    /// tagDelete secret.
    fn config_with_route_secret(url: &str, secret: &str) -> (tempfile::TempDir, PathBuf) {
        let (dir, config) = temp_config();
        let mut seeded = crate::config::load(&config).expect("load");
        let profile = seeded.profiles.get_mut("dev").expect("dev");
        profile.url = url::Url::parse(url).expect("url");
        profile.webdev_tag_delete_secret = Some(secret.to_string());
        crate::config::save(&config, &seeded).expect("seed");
        (dir, config)
    }

    /// Mount the live route's `version` answer with `providers`, or a bare
    /// HTTP `status` when `providers` is `None`.
    async fn mount_live_route(server: &wiremock::MockServer, providers: Option<&[&str]>) {
        let response = match providers {
            Some(providers) => wiremock::ResponseTemplate::new(200).set_body_json(
                serde_json::json!({"ok": true, "data": {
                    "routeVersion": BUNDLE_VERSION, "minCli": "1.0.0",
                    "providers": providers, "maxPaths": 500, "maxPathChars": 1024,
                }}),
            ),
            None => wiremock::ResponseTemplate::new(405),
        };
        wiremock::Mock::given(wiremock::matchers::method("POST"))
            .and(wiremock::matchers::path(
                "/system/webdev/ign-cli/cli/tagDelete",
            ))
            .and(wiremock::matchers::header(
                "X-Ignition-CLI-Secret",
                "c".repeat(64).as_str(),
            ))
            .respond_with(response)
            .mount(server)
            .await;
    }

    fn caller() -> Option<(crate::config::Secret, crate::config::Secret)> {
        Some((
            crate::config::Secret::new("deployer"),
            crate::config::Secret::new("Pw-fake-Z9"),
        ))
    }

    /// 26-REVIEW-D WR-D3: before an install replaces the route, its live
    /// allowlist is read through one handshake (the stored secret and the
    /// caller's env pair). An install that would drop a provider allowed
    /// now refuses `invalid_input` naming it and both ways forward, before
    /// any secret is stored and before the import. Naming every repo
    /// installs the union.
    #[tokio::test]
    async fn tag_delete_install_refuses_to_drop_live_providers() {
        let server = wiremock::MockServer::start().await;
        mount_live_route(&server, Some(&["PA", "PB"])).await;
        let secret = "c".repeat(64);
        let (dir, config) = config_with_route_secret(&server.uri(), &secret);
        let repo_a = temp_repo("[PA]");
        let repo_b = temp_repo("[PB]");

        let mut request = tag_delete_request(repo_b.path(), false);
        request.caller = caller();
        let err = webdev_deploy_with(
            &no_gateway_rig(),
            "ign-cli",
            false,
            false,
            &config,
            "dev",
            false,
            Some(request.clone()),
        )
        .await
        .expect_err("dropping PA is refused");
        assert_eq!(err.code(), "invalid_input", "{err}");
        let text = err.to_string();
        assert!(text.contains("PA"), "{text}");
        assert!(text.contains("--tag-delete-repo"), "{text}");
        assert!(text.contains("without --with-tag-delete"), "{text}");
        assert!(!text.contains(&secret), "{text}");
        let hint = err.hint().unwrap_or_default();
        assert!(hint.contains("--tag-delete-repo"), "{hint}");
        assert_eq!(
            stored_tag_delete_secret(&config).as_deref(),
            Some(secret.as_str()),
            "nothing stored"
        );

        // A rotate reads with the stored secret and refuses the same way.
        let mut rotate = request.clone();
        rotate.rotate_secret = true;
        let err = webdev_deploy_with(
            &no_gateway_rig(),
            "ign-cli",
            false,
            false,
            &config,
            "dev",
            false,
            Some(rotate),
        )
        .await
        .expect_err("a rotate is refused too");
        assert_eq!(err.code(), "invalid_input", "{err}");

        // Naming both repos keeps both providers.
        request.repos = vec![repo_a.path().to_path_buf(), repo_b.path().to_path_buf()];
        let result = webdev_deploy_with(
            &importing_rig(),
            "ign-cli",
            false,
            false,
            &config,
            "dev",
            false,
            Some(request),
        )
        .await
        .expect("the union keeps every live provider");
        assert_eq!(result.tag_delete_providers, vec!["PA", "PB"]);
        assert!(result.warnings.is_empty(), "{:?}", result.warnings);
        let _ = dir;
    }

    /// A live allowlist that cannot be read (no env pair, a failing route)
    /// installs as before and warns; an absent route (405) or no stored
    /// secret means nothing is installed, so nothing is read or warned.
    #[tokio::test]
    async fn tag_delete_install_warns_when_the_live_allowlist_is_unreadable() {
        let secret = "c".repeat(64);
        let repo = temp_repo("[PB]");

        // No env pair: unreadable, warn, install.
        let server = wiremock::MockServer::start().await;
        mount_live_route(&server, Some(&["PA"])).await;
        let (dir, config) = config_with_route_secret(&server.uri(), &secret);
        let result = webdev_deploy_with(
            &importing_rig(),
            "ign-cli",
            false,
            false,
            &config,
            "dev",
            false,
            Some(tag_delete_request(repo.path(), false)),
        )
        .await
        .expect("installs");
        assert_eq!(result.warnings.len(), 1, "{:?}", result.warnings);
        assert!(
            result.warnings[0].contains("could not read the live tagDelete allowlist"),
            "{:?}",
            result.warnings
        );
        assert!(!result.warnings[0].contains(&secret));
        let _ = dir;

        // A route answering 500: unreadable, warn, install.
        let server = wiremock::MockServer::start().await;
        wiremock::Mock::given(wiremock::matchers::method("POST"))
            .respond_with(wiremock::ResponseTemplate::new(500))
            .mount(&server)
            .await;
        let (dir, config) = config_with_route_secret(&server.uri(), &secret);
        let mut request = tag_delete_request(repo.path(), false);
        request.caller = caller();
        let result = webdev_deploy_with(
            &importing_rig(),
            "ign-cli",
            false,
            false,
            &config,
            "dev",
            false,
            Some(request.clone()),
        )
        .await
        .expect("installs");
        assert_eq!(result.warnings.len(), 1, "{:?}", result.warnings);
        assert!(result.warnings[0].contains("could not read the live tagDelete allowlist"));
        let _ = dir;

        // The route is absent (405): nothing to keep, no warning.
        let server = wiremock::MockServer::start().await;
        mount_live_route(&server, None).await;
        let (dir, config) = config_with_route_secret(&server.uri(), &secret);
        let result = webdev_deploy_with(
            &importing_rig(),
            "ign-cli",
            false,
            false,
            &config,
            "dev",
            false,
            Some(request.clone()),
        )
        .await
        .expect("installs");
        assert!(result.warnings.is_empty(), "{:?}", result.warnings);
        let _ = dir;

        // No stored secret: a first install reads nothing, warns nothing.
        let (dir, config) = temp_config();
        let result = webdev_deploy_with(
            &importing_rig(),
            "ign-cli",
            false,
            false,
            &config,
            "dev",
            false,
            Some(request),
        )
        .await
        .expect("installs");
        assert!(result.warnings.is_empty(), "{:?}", result.warnings);
        let _ = dir;
    }

    /// Generated secrets are hex and (statistically) unique across
    /// draws — the shape the route's fail-closed detector relies on.
    #[test]
    fn generated_secrets_are_hex_and_unique() {
        let a = generate_secret().expect("secret");
        let b = generate_secret().expect("secret");
        assert_eq!(a.len(), 64);
        assert_ne!(a, b);
    }

    fn member_names(zip_bytes: &[u8]) -> Vec<String> {
        let mut archive =
            zip::ZipArchive::new(std::io::Cursor::new(zip_bytes)).expect("built zip is readable");
        (0..archive.len())
            .map(|index| archive.by_index(index).expect("member").name().to_string())
            .collect()
    }

    fn member(zip_bytes: &[u8], name: &str) -> Vec<u8> {
        let mut archive =
            zip::ZipArchive::new(std::io::Cursor::new(zip_bytes)).expect("built zip is readable");
        let mut file = archive.by_name(name).expect("member present");
        let mut bytes = Vec::new();
        std::io::Read::read_to_end(&mut file, &mut bytes).expect("member reads");
        bytes
    }
}