faucet-cli 1.13.0

Config-driven CLI runner for faucet-stream pipelines (YAML / JSON, Meltano-style)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
//! Registration + materialization of pipeline templates (#444).
//!
//! Pure orchestration over [`crate::serve::history::RunHistory`]'s template
//! methods and [`crate::params`]; no HTTP, no clap, no MCP shapes — the three
//! front-ends are thin adapters over the two entry points here.

use crate::error::{CliError, CliResult};
use crate::hub::TemplateKind;
use crate::params::{self, BindMode, ParamsSpec, SuppliedParams};
use crate::serve::config::HistoryBackendSpec;
use crate::serve::history::templates::{
    DeprecationRecord, TemplateDraft, TemplateId, TemplateRecord, TemplateState, TemplateStatus,
    TemplateSummary, VersionChannel, VersionSelector,
};
use crate::serve::history::{self, RunHistory};
use crate::serve::load::ConfigFormat;
use serde_json::Value;
use std::collections::BTreeMap;
use std::sync::Arc;
use std::time::Duration;

/// The registry handle. Any `RunHistory` backend will do — `faucet serve` passes
/// its own `--history` store so templates live beside run records; the CLI
/// connects one from `--store` / the config's `catalog:` block.
pub type TemplateStore = Arc<dyn RunHistory>;

/// A registration, before validation.
#[derive(Debug, Clone)]
pub struct RegisterRequest {
    /// Explicit id. When `None` the id is derived from the config's `name:`.
    pub id: Option<String>,
    /// The config document, stored verbatim.
    pub body: String,
    pub format: ConfigFormat,
    /// Free-text description (falls back to nothing).
    pub description: Option<String>,
    /// Named environment channels to point at the newly registered version. The
    /// version number itself always auto-increments; these are the human-facing
    /// pointers (`dev`, `pre-prod`, …) moved onto it in the same step. Derived
    /// channels are rejected.
    pub tags: Vec<VersionChannel>,
    /// Launch the newly registered version immediately, making it `stable`.
    /// Without this a register is inert — a new build never moves existing
    /// callers, which is the point of the model — so this is the explicit
    /// "register and go live" shortcut.
    pub launch: bool,
    /// Principal performing the registration, for provenance.
    pub created_by: Option<String>,
}

/// A template rendered for one trigger: a config document with every
/// `${param.*}` bound, ready to hand to the ordinary run path.
#[derive(Debug, Clone)]
pub struct MaterializedConfig {
    pub template_id: String,
    pub version: u32,
    /// The config's own `name:`, for the run record.
    pub name: Option<String>,
    /// JSON config document (params bound). JSON regardless of how the template
    /// was registered — one canonical hand-off shape for the run path.
    pub body: String,
    /// Bound param values with `secret: true` entries replaced by `"***"` — the
    /// only form safe to echo, audit, or persist.
    pub params_redacted: BTreeMap<String, Value>,
    /// True when at least one bound param was declared `secret: true`.
    pub used_secret_params: bool,
    /// The sink template composed in (a source-template run), with its version.
    pub sink_id: Option<String>,
    pub sink_version: Option<u32>,
    /// Per-stream write-mode resolution of a composed run (empty for a
    /// `kind: pipeline` template).
    pub streams: Vec<crate::hub::compose::StreamPlan>,
    /// The deployment overlay applied (#679): a registered id and version, or
    /// `inline` for one supplied with the trigger.
    pub overlay_id: Option<String>,
    pub overlay_version: Option<u32>,
    /// What the overlay set (`pipeline.state`, `matrix.orders.dlq`, …).
    pub overlay_contributes: Vec<String>,
    /// Operator-facing warnings about the composed run.
    pub warnings: Vec<String>,
    /// The row selection to apply to the materialized config's rows (#741):
    /// the caller's for a `pipeline`; for a composed run, the streams the
    /// caller's selection resolved to (the composition carries only those).
    pub selection: Option<crate::select::SelectionRequest>,
}

impl MaterializedConfig {
    /// Wire format of [`Self::body`]. Always JSON.
    pub fn format(&self) -> ConfigFormat {
        ConfigFormat::Json
    }
}

/// Parse a config document by declared format into an untyped value.
pub fn parse_body(body: &str, format: ConfigFormat) -> CliResult<Value> {
    match format {
        ConfigFormat::Yaml => {
            serde_yaml::from_str(body).map_err(|e| CliError::Config(format!("invalid YAML: {e}")))
        }
        ConfigFormat::Json => {
            serde_json::from_str(body).map_err(|e| CliError::Config(format!("invalid JSON: {e}")))
        }
    }
}

/// Validate a submitted config and append it as a new template version.
///
/// Validation deliberately runs against a **placeholder binding**: required
/// params have no value at registration time, so each is filled with a
/// type-shaped stand-in and the config is then taken through the real
/// `PipelineConfig` parse plus `expand` (matrix mode) or
/// [`crate::topology::validate_topology_spec`] (topology mode). That checks
/// everything structural — grammar, named templates, the matrix graph
/// (parent/`depends_on` cycles, duplicate state keys), the exactly-once and
/// write-mode gates, edge endpoints — without resolving a single secret or
/// constructing a single connector. Node arity in topology mode is validated
/// when the graph is built, i.e. at trigger time, because building it requires
/// live connectors that a placeholder-bound config must not create.
/// What [`register`] establishes before it writes anything: the parsed
/// document, its declared params, kind, derived id, and — for a complete
/// pipeline — the validated config. [`preview_register`] (#703) exposes it
/// without registering.
struct Prelude {
    doc: Value,
    declared: ParamsSpec,
    kind: TemplateKind,
    name: Option<String>,
    id: TemplateId,
    pipeline: Option<crate::config::PipelineConfig>,
}

async fn register_prelude(store: &TemplateStore, req: &RegisterRequest) -> CliResult<Prelude> {
    let doc = parse_body(&req.body, req.format)?;
    if !doc.is_object() {
        return Err(CliError::Config(
            "a template must be a YAML/JSON mapping (`kind: source-template`, `kind: sink-template`, \
             or `kind: pipeline`)"
                .into(),
        ));
    }

    // The declared trigger surface, validated and stored alongside the body so
    // callers can discover it without re-parsing. Hub templates declare theirs
    // at the top level too.
    let declared = params::declared(&doc)?;

    // Dispatch on `kind:`. A kind-less document is the pre-#571 full-pipeline
    // template: still accepted, as `pipeline`, but deprecated — the hub kinds
    // are the model (RFC 0008).
    let mut pipeline_cfg: Option<crate::config::PipelineConfig> = None;
    let detected = crate::hub::detect_kind(&doc);
    let (kind, name) = match detected {
        Some(TemplateKind::SourceTemplate) => {
            let t: crate::hub::SourceTemplate = serde_json::from_value(doc.clone())
                .map_err(|e| CliError::Config(format!("source-template: {e}")))?;
            t.validate()?;
            registry_lint(&t.id(), crate::hub::catalog::lint_source(&t))?;
            (TemplateKind::SourceTemplate, Some(t.id()))
        }
        Some(TemplateKind::SinkTemplate) => {
            let t: crate::hub::SinkTemplate = serde_json::from_value(doc.clone())
                .map_err(|e| CliError::Config(format!("sink-template: {e}")))?;
            t.validate()?;
            registry_lint(&t.id(), crate::hub::catalog::lint_sink(&t))?;
            (TemplateKind::SinkTemplate, Some(t.id()))
        }
        Some(TemplateKind::Deployment) => {
            let t = crate::hub::DeploymentTemplate::from_value(doc.clone())?;
            registry_lint(&t.id(), crate::hub::catalog::lint_deployment(&t))?;
            (TemplateKind::Deployment, Some(t.id()))
        }
        Some(TemplateKind::Pipeline) | None => {
            if detected.is_none() {
                tracing::warn!(
                    id = req.id.as_deref().unwrap_or("<derived>"),
                    "registering a full pipeline config without `kind:` is deprecated — the \
                     registry's model is `kind: source-template` composed with `kind: \
                     sink-template` (RFC 0008); add `kind: pipeline` to keep registering a \
                     complete config explicitly"
                );
            }
            let cfg = validate_pipeline_body(&doc)?;
            let pipeline_name = cfg.name.clone();
            pipeline_cfg = Some(cfg);
            (TemplateKind::Pipeline, pipeline_name)
        }
    };

    let id = match (&req.id, kind != TemplateKind::Pipeline) {
        // A hub template's registry id is its `name` — compose uses the name for
        // the pipeline name / state keys, so the two must not diverge.
        (Some(raw), true) => {
            let id = TemplateId::parse(raw)?;
            if Some(id.as_str()) != name.as_deref() {
                return Err(CliError::Config(format!(
                    "a {kind} is registered under its own hub id ('{}' — `owner/name`, or `name` for an official template); drop `--id` or make it match",
                    name.as_deref().unwrap_or("")
                )));
            }
            id
        }
        (None, true) => TemplateId::parse(name.as_deref().unwrap_or_default())?,
        (Some(raw), false) => TemplateId::parse(raw)?,
        (None, false) => {
            let name = name.as_deref().ok_or_else(|| {
                CliError::Config(
                    "no template id given and the config has no `name:` to derive one from — \
                     pass an explicit id"
                        .into(),
                )
            })?;
            TemplateId::from_config_name(name)?
        }
    };

    // A re-register must keep the kind: a source template cannot silently
    // become a pipeline (or vice versa) under the same id — every existing
    // pairing would break at trigger time.
    if let Some(prev) = store
        .template_get(id.as_str(), None)
        .await
        .map_err(|e| CliError::Internal(format!("template registry read: {e}")))?
        && prev.kind != kind
    {
        return Err(CliError::Config(format!(
            "template '{id}' is a {} — a {kind} cannot be registered under the same id (use another id, or delete it first)",
            prev.kind
        )));
    }

    // Reject a derived channel before writing anything, so a bad request never
    // leaves a half-registered version behind.
    for tag in &req.tags {
        reject_derived(*tag)?;
    }

    Ok(Prelude {
        doc,
        declared,
        kind,
        name,
        id,
        pipeline: pipeline_cfg,
    })
}

/// What registering `req` would do (#703): the id and kind it resolves to,
/// the version it would follow, and — for a complete pipeline — one plan
/// report per root row. Validates exactly as [`register`] does; writes nothing.
#[derive(Debug)]
pub struct RegisterPreview {
    pub id: String,
    pub kind: TemplateKind,
    /// The newest version already registered under this id, if any.
    pub previous_version: Option<u32>,
    pub rows: Vec<crate::commands::plan::PlanReport>,
}

pub async fn preview_register(
    store: &TemplateStore,
    req: &RegisterRequest,
) -> CliResult<RegisterPreview> {
    let prelude = register_prelude(store, req).await?;
    let previous_version = store
        .template_get(prelude.id.as_str(), None)
        .await
        .map_err(|e| CliError::Internal(format!("template registry read: {e}")))?
        .map(|r| r.version);
    let rows = match &prelude.pipeline {
        Some(cfg) => crate::expand::expand(cfg)?
            .iter()
            .filter(|n| matches!(n.role, crate::expand::NodeRole::Root))
            .map(crate::commands::plan::build_plan_report)
            .collect(),
        None => Vec::new(),
    };
    Ok(RegisterPreview {
        id: prelude.id.as_str().to_string(),
        kind: prelude.kind,
        previous_version,
        rows,
    })
}

pub async fn register(store: &TemplateStore, req: RegisterRequest) -> CliResult<TemplateRecord> {
    let Prelude {
        doc,
        declared,
        kind,
        name,
        id,
        ..
    } = register_prelude(store, &req).await?;

    // A description describes the *template*, not the build, so carry the previous
    // version's forward when the caller omits one. Without this, a deploy that
    // re-registers without `--description` blanks the listing for everybody.
    let description = match &req.description {
        Some(d) => Some(d.clone()),
        None => store
            .template_get(id.as_str(), None)
            .await
            .map_err(|e| CliError::Internal(format!("template registry read: {e}")))?
            .and_then(|prev| prev.description)
            .or_else(|| match kind {
                // Hub templates carry their own description.
                TemplateKind::SourceTemplate
                | TemplateKind::SinkTemplate
                | TemplateKind::Deployment => doc
                    .get("description")
                    .and_then(Value::as_str)
                    .map(str::to_string),
                TemplateKind::Pipeline => None,
            }),
    };

    let draft = TemplateDraft {
        id,
        kind,
        name,
        description,
        body: req.body.clone(),
        format: req.format,
        params: declared,
        created_by: req.created_by.clone(),
    };
    let record = store
        .template_register(&draft)
        .await
        .map_err(|e| CliError::Internal(format!("template registry write: {e}")))?;

    // Point the requested channels at the version just created.
    for tag in &req.tags {
        store
            .template_set_tag(&record.id, tag.as_str(), record.version)
            .await
            .map_err(|e| CliError::Internal(format!("template channel write: {e}")))?;
    }
    // `--launch` is the only way a register makes a version live.
    if req.launch {
        store
            .template_launch(&record.id, record.version, req.created_by.as_deref())
            .await
            .map_err(|e| CliError::Internal(format!("template launch write: {e}")))?;
    }
    Ok(record)
}

/// The publishability lint, as a registry gate: a literal credential or a
/// private hostname must never be stored in a shared registry. A missing
/// `description` is a catalog-quality nit, not a reason to refuse.
fn registry_lint(name: &str, findings: Vec<String>) -> CliResult<()> {
    let blocking: Vec<String> = findings
        .into_iter()
        .filter(|f| !f.starts_with("missing `description`"))
        .collect();
    if blocking.is_empty() {
        Ok(())
    } else {
        Err(CliError::Config(format!(
            "'{name}' cannot be registered:\n  - {}",
            blocking.join("\n  - ")
        )))
    }
}

/// Structural validation of a complete pipeline template on a
/// placeholder-bound copy. `${env:…}` and secret directives are left untouched —
/// registration must never read the server's secrets, and the body persisted
/// is the one that was submitted.
pub(crate) fn validate_pipeline_body(doc: &Value) -> CliResult<crate::config::PipelineConfig> {
    let mut probe = doc.clone();
    params::bind_document(&mut probe, &SuppliedParams::new(), BindMode::Placeholder)?;
    let cfg = crate::config::PipelineConfig::from_value(probe)?;
    if crate::topology::is_topology(&cfg) {
        crate::topology::validate_topology_spec(&cfg)?;
    } else {
        // Compile each row's transform chain too — `expand` only checks an entry's
        // shape, so without this a template with a misspelled transform field
        // registers cleanly and fails at trigger time instead.
        for node in crate::expand::expand(&cfg)? {
            // Deserialize each connector's `config` into its typed struct
            // (#609). `expand` leaves it an opaque `Value`, so without this a
            // structurally wrong config — the wrong nesting under a flattened
            // block, an unknown field, a typo'd name — registers cleanly,
            // launches, and only fails on the first triggered run. No
            // credentials are resolved and no connection is opened.
            crate::registry::validate_source_config(
                &node.source.kind,
                &node.id,
                node.source.config.clone(),
            )
            .map_err(|e| CliError::Config(format!("row '{}' source: {e}", node.id)))?;
            // A discovery row has no sink (`NodeRole::Discovery`), so its sink
            // slot holds a placeholder — validating it would reject a good
            // config for a connector the row never writes to.
            if !matches!(node.role, crate::expand::NodeRole::Discovery { .. }) {
                crate::registry::validate_sink_config(
                    &node.sink.kind,
                    &node.id,
                    node.sink.config.clone(),
                )
                .map_err(|e| CliError::Config(format!("row '{}' sink: {e}", node.id)))?;
            }

            if node.transforms.is_empty() {
                continue;
            }
            crate::transforms::compile_transforms(&node.transforms)
                .map_err(|e| CliError::Config(format!("row '{}': {e}", node.id)))?;
        }
    }
    Ok(cfg)
}

/// `latest` is computed from the version list, so promoting or deleting it makes
/// no sense — say so instead of silently no-oping.
fn reject_derived(tag: VersionChannel) -> CliResult<()> {
    if tag.is_derived() {
        let how = match tag {
            VersionChannel::Stable => " — move it with `faucet template launch` instead",
            VersionChannel::Previous => " — it is whatever was launched before the current version",
            _ => " — it is always the highest version number",
        };
        return Err(CliError::Config(format!(
            "`{tag}` is a derived channel and cannot be promoted{how}. Promotable channels: {}",
            VersionChannel::ASSIGNABLE
                .iter()
                .map(|c| c.as_str())
                .collect::<Vec<_>>()
                .join(", ")
        )));
    }
    Ok(())
}

/// Resolve a [`VersionSelector`] to the exact version to act on.
///
/// **Every** channel — derived or assigned — is looked up here; nothing falls back
/// to "the newest build". A selector that names an unset channel is an error
/// listing what *is* set, because silently substituting another version is how a
/// caller ends up running code they did not ask for.
pub async fn resolve_version(
    store: &TemplateStore,
    id: &str,
    selector: VersionSelector,
) -> CliResult<u32> {
    if let VersionSelector::Pinned(n) = selector {
        return Ok(n);
    }
    let channel = selector
        .channel()
        .expect("non-pinned selector names a channel");
    let state = template_state(store, id).await?;
    if state.versions.is_empty() {
        return Err(CliError::UnknownPipelineTemplate {
            id: id.to_string(),
            version: None,
        });
    }
    state
        .derived(channel)
        .ok_or_else(|| unresolved_channel(id, channel, &state))
}

/// The error for a selector that names a channel with nothing behind it. Phrased
/// per channel, because the fix differs: `stable` needs a *launch*, `previous`
/// needs a second launch, an environment channel needs a *promote*.
fn unresolved_channel(id: &str, channel: VersionChannel, state: &TemplateState) -> CliError {
    let newest = state
        .newest
        .map(|v| v.to_string())
        .unwrap_or_else(|| "1".into());
    match channel {
        // Phrased for both audiences — the same error surfaces on the CLI, over
        // HTTP, and in the console's versions page.
        VersionChannel::Stable => CliError::Config(format!(
            "template '{id}' has no launched version (status: {}). Launch one first \
             (`faucet template launch {id} --version {newest}`, or \
             `POST /v1/templates/{id}/launch`), or select a specific build with \
             `newest` / a version number",
            state.status
        )),
        VersionChannel::Previous => CliError::Config(format!(
            "template '{id}' has no previous version — {}. `previous` is the version launched \
             before the current one, so it only exists after a second launch",
            match state.stable {
                Some(v) => format!("v{v} is the first and only launched version"),
                None => "nothing has been launched yet".to_string(),
            }
        )),
        // With versions registered, `newest` is unset only when every one of them
        // is deprecated (#697).
        VersionChannel::Newest => CliError::Config(format!(
            "every version of template '{id}' is deprecated, so `newest` has nothing to \
             resolve to. Revive one (`faucet template deprecate {id} --version <n> --undo`) \
             or pin a version number"
        )),
        assigned => CliError::Config(format!(
            "template '{id}' has no `{assigned}` version. Channels currently set: {}. Promote one \
             with `faucet template promote {id} --tag {assigned} --version <n>`",
            if state.tags.is_empty() {
                String::from("(none)")
            } else {
                state
                    .tags
                    .iter()
                    .map(|(t, v)| format!("{t}=v{v}"))
                    .collect::<Vec<_>>()
                    .join(", ")
            }
        )),
    }
}

/// Every registered template's latest version, each carrying its release state.
///
/// One extra read per template — the registry is a small, human-curated set, and
/// assembling the state per row keeps the list and detail views consistent by
/// construction rather than by convention.
pub async fn list_with_state(store: &TemplateStore) -> CliResult<Vec<TemplateSummary>> {
    let mut out = store
        .template_list()
        .await
        .map_err(|e| CliError::Internal(format!("template registry read: {e}")))?;
    for summary in &mut out {
        summary.state = Some(template_state(store, &summary.id).await?);
    }
    Ok(out)
}

/// The template's full release state (status, `stable` / `previous` / `newest`,
/// channel pointers). Errors only if the registry itself is unreadable.
pub async fn template_state(store: &TemplateStore, id: &str) -> CliResult<TemplateState> {
    store
        .template_state(id)
        .await
        .map_err(|e| CliError::Internal(format!("template registry read: {e}")))
}

/// Confirm a version exists, returning a typed error naming it if not.
async fn require_version(store: &TemplateStore, id: &str, version: u32) -> CliResult<()> {
    if store
        .template_get(id, Some(version))
        .await
        .map_err(|e| CliError::Internal(format!("template registry read: {e}")))?
        .is_none()
    {
        return Err(CliError::UnknownPipelineTemplate {
            id: id.to_string(),
            version: Some(version),
        });
    }
    Ok(())
}

/// Point a named environment channel at a version, moving it if already set.
///
/// The target is itself a selector, so `--tag prod --version stable` promotes
/// whatever is currently launched — the "use the version I already blessed" case —
/// and `--version 3` pins an exact build. Derived channels (`stable`, `previous`,
/// `newest`) are not valid *targets*: `stable` moves via [`launch`], and the other
/// two are computed.
pub async fn promote(
    store: &TemplateStore,
    id: &str,
    tag: VersionChannel,
    target: VersionSelector,
) -> CliResult<u32> {
    reject_derived(tag)?;
    let version = resolve_version(store, id, target).await?;
    require_version(store, id, version).await?;
    store
        .template_set_tag(id, tag.as_str(), version)
        .await
        .map_err(|e| CliError::Internal(format!("template channel write: {e}")))?;
    Ok(version)
}

/// The outcome of a [`launch`]: which version is now live, and what it replaced.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LaunchOutcome {
    /// The version now launched (`stable`).
    pub version: u32,
    /// The version it replaced — the new `previous`. `None` on a first launch.
    pub replaced: Option<u32>,
    /// True when the requested version was already launched, so nothing changed.
    pub already_launched: bool,
    /// Whether this launch flipped the template out of `draft`.
    pub first_launch: bool,
}

/// **Launch** a version: make it `stable`, so unpinned callers start using it.
///
/// This is the one deliberate act that moves consumers. Registering a build never
/// does — that is the whole point of the model, so a nightly can land without
/// dragging anyone along.
///
/// Refuses to launch while the template is deprecated: reviving a retired template
/// by moving its live pointer is almost certainly a mistake, and `--undo` makes the
/// intent explicit.
pub async fn launch(
    store: &TemplateStore,
    id: &str,
    target: VersionSelector,
    launched_by: Option<&str>,
) -> CliResult<LaunchOutcome> {
    let version = resolve_version(store, id, target).await?;
    require_version(store, id, version).await?;
    let before = template_state(store, id).await?;
    if before.status == TemplateStatus::Deprecated {
        return Err(CliError::Config(format!(
            "template '{id}' is deprecated — un-deprecate it first with \
             `faucet template deprecate {id} --undo`, then launch"
        )));
    }
    if before.version_deprecation(version).is_some() {
        return Err(CliError::Config(format!(
            "v{version} of template '{id}' is deprecated, so it cannot be launched. Revive it \
             first (`faucet template deprecate {id} --version {version} --undo`), or launch \
             another version"
        )));
    }
    let seq = store
        .template_launch(id, version, launched_by)
        .await
        .map_err(|e| CliError::Internal(format!("template launch write: {e}")))?;
    Ok(LaunchOutcome {
        version,
        replaced: before.stable,
        already_launched: seq.is_none(),
        first_launch: before.stable.is_none(),
    })
}

/// Roll back to the previously launched version — `launch` of `previous`, named
/// for the thing you actually want to find under pressure.
pub async fn rollback(
    store: &TemplateStore,
    id: &str,
    launched_by: Option<&str>,
) -> CliResult<LaunchOutcome> {
    launch(
        store,
        id,
        VersionSelector::Channel(VersionChannel::Previous),
        launched_by,
    )
    .await
}

/// Retire (`Some`) or revive (`None`) a template.
///
/// Deprecation is **template-wide**, not per version: a build that should not be
/// used simply never gets launched (or gets deleted). A deprecated template keeps
/// serving callers who pin or ride `stable` — retiring must not hard-break
/// them — but every trigger warns and listings mark it. Returns the resulting
/// status.
pub async fn set_deprecated(
    store: &TemplateStore,
    id: &str,
    reason: Option<String>,
    by: Option<&str>,
    deprecated: bool,
) -> CliResult<TemplateStatus> {
    let state = template_state(store, id).await?;
    if state.versions.is_empty() {
        return Err(CliError::UnknownPipelineTemplate {
            id: id.to_string(),
            version: None,
        });
    }
    let record = deprecated.then(|| DeprecationRecord {
        deprecated_at: chrono::Utc::now(),
        deprecated_by: by.map(str::to_string),
        reason,
    });
    store
        .template_set_deprecation(id, record.as_ref())
        .await
        .map_err(|e| CliError::Internal(format!("template deprecation write: {e}")))?;
    Ok(TemplateStatus::derive(state.stable.is_some(), deprecated))
}

/// Retire (`true`) or revive (`false`) one version (#697). A retired version
/// keeps running when pinned, or when `stable` or a channel points at it, but
/// `newest` skips it, `launch` refuses it, and every trigger warns.
pub async fn set_version_deprecated(
    store: &TemplateStore,
    id: &str,
    version: u32,
    reason: Option<String>,
    by: Option<&str>,
    deprecated: bool,
) -> CliResult<()> {
    require_version(store, id, version).await?;
    let record = deprecated.then(|| DeprecationRecord {
        deprecated_at: chrono::Utc::now(),
        deprecated_by: by.map(str::to_string),
        reason,
    });
    store
        .template_set_version_deprecation(id, version, record.as_ref())
        .await
        .map_err(|e| CliError::Internal(format!("template version deprecation write: {e}")))
}

/// The warning a run of `version` carries, if the template or that version is
/// deprecated. Template-level first: it is the broader statement.
pub fn deprecation_warning(state: &TemplateState, version: u32) -> Option<String> {
    if state.status == TemplateStatus::Deprecated {
        return Some(
            state
                .deprecation
                .as_ref()
                .and_then(|d| d.reason.clone())
                .unwrap_or_else(|| "this template is deprecated".to_string()),
        );
    }
    state
        .version_deprecation(version)
        .map(|d| match &d.record.reason {
            Some(r) => format!("v{version} is deprecated: {r}"),
            None => format!("v{version} is deprecated"),
        })
}

/// Where the materialized config is going — which decides whether load-time
/// directives may be resolved here.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Materialize {
    /// The body is executed by *this* process and never stored. Load-time
    /// directives (`${env:}` / `${file:}` / `${secret:}`) are resolved here, so a
    /// caller-supplied `env` overlay takes effect.
    Local,
    /// The body will be **persisted** for another instance to execute (a
    /// clustered submit). Load-time directives are left as tokens for the
    /// executing instance to resolve, so a resolved credential is never written
    /// to the shared run-history database (#456 C5).
    Persisted,
}

/// Fetch a template version and bind the supplied params into a runnable config
/// document.
///
/// Ordering mirrors the file-load path: `${env:}` / `${file:}` / `${secret:}`
/// resolve **first** (with `env_overrides` taking precedence over the process
/// environment), then `${param.*}` binds. A supplied param value is therefore
/// never itself scanned for directives, so a caller cannot use a param to read
/// the server's environment or secret store.
///
/// Under [`Materialize::Persisted`] the first step is **skipped**: the directives
/// stay as tokens and are resolved later by `load_submission` on whichever
/// instance runs the job. Resolving them here would serialise the *values* into
/// the body that gets stored in the shared database — which is how a
/// `${env:DB_PASSWORD}` in a template body ended up in plaintext there. The
/// trade-off is that a typed (`int`/`float`/`bool`) param whose `default` is
/// itself a directive cannot be coerced in this mode; it fails loudly at trigger
/// time naming the param, rather than silently.
pub async fn materialize(
    store: &TemplateStore,
    id: &str,
    version: u32,
    supplied: &SuppliedParams,
    env_overrides: &BTreeMap<String, String>,
    mode: Materialize,
) -> CliResult<MaterializedConfig> {
    // Takes a concrete version, never an `Option`: "no version given" is resolved
    // by `resolve_version` against the registry, so there is no code path where a
    // `None` here could quietly mean "the newest build".
    let record = store
        .template_get(id, Some(version))
        .await
        .map_err(|e| CliError::Internal(format!("template registry read: {e}")))?
        .ok_or_else(|| CliError::UnknownPipelineTemplate {
            id: id.to_string(),
            version: Some(version),
        })?;

    match record.kind {
        TemplateKind::Pipeline => {}
        TemplateKind::SourceTemplate => {
            return Err(CliError::Config(format!(
                "'{id}' is a source-template — it runs composed with a sink template: pass `--sink <id>` \
                 (HTTP: `sink`), or `faucet template list` to see the registered sink templates"
            )));
        }
        TemplateKind::SinkTemplate => {
            return Err(CliError::Config(format!(
                "'{id}' is a sink-template and is not runnable on its own — run a source template with \
                 `--sink {id}`"
            )));
        }
        TemplateKind::Deployment => return Err(not_runnable_deployment(id)),
    }
    let doc = parse_body(&record.body, record.format)?;
    let (body, bound) = bind_document_for_run(doc, supplied, env_overrides, mode)?;
    Ok(MaterializedConfig {
        template_id: record.id.clone(),
        version: record.version,
        name: record.name.clone(),
        body,
        params_redacted: bound.redacted(),
        used_secret_params: bound.has_secrets(),
        sink_id: None,
        sink_version: None,
        streams: Vec::new(),
        overlay_id: None,
        overlay_version: None,
        overlay_contributes: Vec::new(),
        warnings: Vec::new(),
        selection: None,
    })
}

/// The sink half of a trigger: which registered sink template to compose in.
#[derive(Debug, Clone, Default)]
pub struct SinkChoice {
    pub id: Option<String>,
    /// Defaults to `stable`, like the source side.
    pub version: VersionSelector,
    /// The deployment overlay to apply over the pairing (#679).
    pub overlay: Option<OverlayChoice>,
}

/// Where a trigger's deployment overlay comes from.
#[derive(Debug, Clone, PartialEq)]
pub enum OverlayChoice {
    /// A registered `kind: deployment` template.
    Registered {
        id: String,
        version: VersionSelector,
    },
    /// A document supplied with the trigger. `kind:` and `name:` may be
    /// omitted — they default to `deployment` / `inline`.
    Inline(Value),
}

fn not_runnable_deployment(id: &str) -> CliError {
    CliError::Config(format!(
        "'{id}' is a deployment overlay and is not runnable on its own — apply it to a source \
         template's run: `faucet template run <source> --sink <sink> --overlay {id}` (HTTP: `overlay`)"
    ))
}

/// Resolve a trigger's overlay to a validated document and its provenance.
pub(crate) async fn resolve_overlay(
    store: &TemplateStore,
    choice: &OverlayChoice,
) -> CliResult<(crate::hub::DeploymentTemplate, String, Option<u32>)> {
    match choice {
        OverlayChoice::Registered { id, version } => {
            let v = resolve_version(store, id, *version).await?;
            let rec = fetch_version(store, id, v).await?;
            if rec.kind != TemplateKind::Deployment {
                return Err(CliError::Config(format!(
                    "'{id}' is a {} — `overlay` must name a deployment",
                    rec.kind
                )));
            }
            let t = crate::hub::DeploymentTemplate::from_value(parse_body(&rec.body, rec.format)?)?;
            Ok((t, rec.id, Some(rec.version)))
        }
        OverlayChoice::Inline(v) => {
            let mut v = v.clone();
            if let Some(obj) = v.as_object_mut() {
                obj.entry("kind")
                    .or_insert_with(|| Value::String("deployment".into()));
                obj.entry("name")
                    .or_insert_with(|| Value::String("inline".into()));
            } else {
                return Err(CliError::Config(
                    "`overlay` must be a registered deployment id or a mapping".into(),
                ));
            }
            let t = crate::hub::DeploymentTemplate::from_value(v)?;
            Ok((t, "inline".into(), None))
        }
    }
}

/// Everything a trigger surface needs to run template `id` at `version`:
/// dispatches on the record's kind — a `pipeline` materializes alone (and
/// refuses a sink), a `source-template` requires and composes a sink (whose
/// version is resolved here too), a `sink-template` is never runnable on its
/// own. One implementation shared by the CLI, HTTP, MCP, and suites so they
/// can never disagree about the rules.
pub async fn materialize_for_run(
    store: &TemplateStore,
    id: &str,
    version: u32,
    sink: &SinkChoice,
    supplied: &SuppliedParams,
    env_overrides: &BTreeMap<String, String>,
    mode: Materialize,
) -> CliResult<MaterializedConfig> {
    materialize_for_run_selected(
        store,
        id,
        version,
        sink,
        supplied,
        env_overrides,
        mode,
        None,
    )
    .await
}

/// [`materialize_for_run`] for a subset of the template's rows (#741). A
/// `pipeline` carries the selection through to the run; a `source-template`
/// composes only the streams the selection resolves to, so a stream the sink
/// cannot run does not block the ones selected.
#[allow(clippy::too_many_arguments)]
pub async fn materialize_for_run_selected(
    store: &TemplateStore,
    id: &str,
    version: u32,
    sink: &SinkChoice,
    supplied: &SuppliedParams,
    env_overrides: &BTreeMap<String, String>,
    mode: Materialize,
    selection: Option<&crate::select::SelectionRequest>,
) -> CliResult<MaterializedConfig> {
    let record = fetch_version(store, id, version).await?;
    match record.kind {
        TemplateKind::Pipeline => {
            if let Some(sink_id) = &sink.id {
                return Err(CliError::Config(format!(
                    "'{id}' is a complete pipeline template — it takes no sink (got `--sink {sink_id}`)"
                )));
            }
            if sink.overlay.is_some() {
                return Err(CliError::Config(format!(
                    "'{id}' is a complete pipeline template — its operational blocks live in the \
                     config itself; an overlay applies to a composed source × sink run"
                )));
            }
            let mut m = materialize(store, id, version, supplied, env_overrides, mode).await?;
            m.selection = selection.cloned();
            Ok(m)
        }
        TemplateKind::SourceTemplate => {
            let sink_id = sink.id.as_deref().ok_or_else(|| {
                CliError::Config(format!(
                    "'{id}' is a source-template — it runs composed with a sink template: pass \
                     `--sink <id>` (HTTP: `sink`); `faucet template list --kind sink-template` shows them"
                ))
            })?;
            let sink_version = resolve_version(store, sink_id, sink.version).await?;
            materialize_pair_selected(
                store,
                (id, version),
                (sink_id, sink_version),
                sink.overlay.as_ref(),
                supplied,
                env_overrides,
                mode,
                selection,
            )
            .await
        }
        TemplateKind::SinkTemplate => Err(CliError::Config(format!(
            "'{id}' is a sink-template and is not runnable on its own — run a source template with \
             `--sink {id}`"
        ))),
        TemplateKind::Deployment => Err(not_runnable_deployment(id)),
    }
}

/// Compose a registered `source-template` with a registered `sink-template`
/// and bind params — the trigger path of the hub model. Both sides are pinned
/// to concrete versions by the caller (`resolve_version` each), so a run
/// records exactly which builds it composed.
pub async fn materialize_pair(
    store: &TemplateStore,
    source: (&str, u32),
    sink: (&str, u32),
    supplied: &SuppliedParams,
    env_overrides: &BTreeMap<String, String>,
    mode: Materialize,
) -> CliResult<MaterializedConfig> {
    materialize_pair_overlaid(store, source, sink, None, supplied, env_overrides, mode).await
}

/// [`materialize_pair`] with a deployment overlay (#679) applied over the
/// composition before params bind, so the overlay's own `${param.*}` bind too.
#[allow(clippy::too_many_arguments)]
pub async fn materialize_pair_overlaid(
    store: &TemplateStore,
    (source_id, source_version): (&str, u32),
    (sink_id, sink_version): (&str, u32),
    overlay: Option<&OverlayChoice>,
    supplied: &SuppliedParams,
    env_overrides: &BTreeMap<String, String>,
    mode: Materialize,
) -> CliResult<MaterializedConfig> {
    materialize_pair_selected(
        store,
        (source_id, source_version),
        (sink_id, sink_version),
        overlay,
        supplied,
        env_overrides,
        mode,
        None,
    )
    .await
}

#[allow(clippy::too_many_arguments)]
async fn materialize_pair_selected(
    store: &TemplateStore,
    (source_id, source_version): (&str, u32),
    (sink_id, sink_version): (&str, u32),
    overlay: Option<&OverlayChoice>,
    supplied: &SuppliedParams,
    env_overrides: &BTreeMap<String, String>,
    mode: Materialize,
    selection: Option<&crate::select::SelectionRequest>,
) -> CliResult<MaterializedConfig> {
    let src_rec = fetch_version(store, source_id, source_version).await?;
    let sink_rec = fetch_version(store, sink_id, sink_version).await?;
    if src_rec.kind != TemplateKind::SourceTemplate {
        return Err(CliError::Config(format!(
            "'{source_id}' is a {} — the source side of a pairing must be a source-template",
            src_rec.kind
        )));
    }
    if sink_rec.kind != TemplateKind::SinkTemplate {
        return Err(CliError::Config(format!(
            "'{sink_id}' is a {} — `sink` must name a sink-template",
            sink_rec.kind
        )));
    }
    let source: crate::hub::SourceTemplate =
        serde_json::from_value(parse_body(&src_rec.body, src_rec.format)?).map_err(|e| {
            CliError::Internal(format!(
                "stored source-template '{source_id}' v{source_version}: {e}"
            ))
        })?;
    let sink: crate::hub::SinkTemplate =
        serde_json::from_value(parse_body(&sink_rec.body, sink_rec.format)?).map_err(|e| {
            CliError::Internal(format!(
                "stored sink-template '{sink_id}' v{sink_version}: {e}"
            ))
        })?;
    let (source, effective) = match selection {
        Some(sel) => {
            let (narrowed, effective) = crate::hub::rows::narrow_source_template(&source, sel)?;
            (narrowed, Some(effective))
        }
        None => (source, None),
    };
    let mut composition = crate::hub::compose(&source, &sink)?;
    let (mut overlay_id, mut overlay_version) = (None, None);
    if let Some(choice) = overlay {
        let (t, oid, over) = resolve_overlay(store, choice).await?;
        composition = composition.apply_overlay(&t)?;
        overlay_id = Some(oid);
        overlay_version = over;
    }
    let overlay_contributes = std::mem::take(&mut composition.overlay_contributes);
    let warnings = std::mem::take(&mut composition.warnings);
    let (body, bound) = bind_document_for_run(composition.document, supplied, env_overrides, mode)?;
    Ok(MaterializedConfig {
        template_id: src_rec.id.clone(),
        version: src_rec.version,
        name: Some(composition.name),
        body,
        params_redacted: bound.redacted(),
        used_secret_params: bound.has_secrets(),
        sink_id: Some(sink_rec.id.clone()),
        sink_version: Some(sink_rec.version),
        streams: composition.streams,
        overlay_id,
        overlay_version,
        overlay_contributes,
        warnings,
        selection: effective,
    })
}

async fn fetch_version(store: &TemplateStore, id: &str, version: u32) -> CliResult<TemplateRecord> {
    store
        .template_get(id, Some(version))
        .await
        .map_err(|e| CliError::Internal(format!("template registry read: {e}")))?
        .ok_or_else(|| CliError::UnknownPipelineTemplate {
            id: id.to_string(),
            version: Some(version),
        })
}

/// The shared tail of materialization: (Local only) resolve load-time
/// directives with the env overlay, bind `${param.*}` strictly, drop the
/// `params:` block, and re-serialize as JSON.
fn bind_document_for_run(
    mut doc: Value,
    supplied: &SuppliedParams,
    env_overrides: &BTreeMap<String, String>,
    mode: Materialize,
) -> CliResult<(String, params::BoundParams)> {
    if mode == Materialize::Local {
        let overlay: crate::interpolate::EnvOverlay = env_overrides
            .iter()
            .map(|(k, v)| (k.clone(), v.clone()))
            .collect();
        crate::interpolate::interpolate_value_with_env(&mut doc, &overlay)?;
    }
    let bound = params::bind_document(&mut doc, supplied, BindMode::Strict)?;
    // Drop the declaration block: materialization is the moment params cease to
    // exist. Leaving it would make any later load re-run the bind pass with no
    // supplied values and reject the config for a "missing" required param —
    // and the param surface is already recorded on the template and echoed to
    // the caller, so nothing is lost.
    if let Some(map) = doc.as_object_mut() {
        map.remove(params::PARAMS_KEY);
    }
    let body = serde_json::to_string(&doc)
        .map_err(|e| CliError::Internal(format!("re-serializing template body: {e}")))?;
    Ok((body, bound))
}

/// Connect a template store from a URL: `memory`, `sqlite:<path>`, or a
/// `postgres://…` URL. Same grammar (and same build-feature requirements) as
/// `catalog.url` and `faucet serve --history`, so one store can hold run
/// history, the dataset catalog, and the template registry together.
pub async fn resolve_store_url(url: &str) -> CliResult<TemplateStore> {
    let backend = match url {
        "memory" => HistoryBackendSpec::Memory,
        u if u.starts_with("postgres://") || u.starts_with("postgresql://") => {
            HistoryBackendSpec::Postgres(u.to_string())
        }
        u if u.starts_with("sqlite:") => HistoryBackendSpec::Sqlite(u.to_string()),
        other => {
            return Err(CliError::Config(format!(
                "template store '{other}' is not recognised — expected 'memory', \
                 'sqlite:<path>', or a 'postgres://…' URL"
            )));
        }
    };
    history::connect(
        &backend,
        // Idempotency claims and run leases are run-history concerns; a
        // template-only connection never uses them.
        Duration::from_secs(3600),
        Duration::from_secs(30),
        &uuid::Uuid::now_v7().to_string(),
    )
    .await
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::serve::history::memory::MemoryHistory;
    use serde_json::json;

    fn store() -> TemplateStore {
        Arc::new(MemoryHistory::new(Duration::from_secs(60))) as TemplateStore
    }

    const PARAMETERIZED: &str = "\
version: 1
name: tenant-sync
params:
  tenant_id: { required: true, description: Tenant to sync }
  since: { default: \"1970-01-01\" }
  page: { type: int, default: 100 }
pipeline:
  source:
    type: rest
    config:
      base_url: \"https://api.example.com\"
      path: \"/${param.tenant_id}/events?since=${param.since}\"
  sink:
    type: jsonl
    config:
      path: ./out.jsonl
";

    fn req(body: &str) -> RegisterRequest {
        RegisterRequest {
            id: None,
            body: body.to_string(),
            format: ConfigFormat::Yaml,
            description: Some("test".into()),
            tags: Vec::new(),
            launch: false,
            created_by: Some("tester".into()),
        }
    }

    /// Register + launch in one step, for tests that only care about the result.
    fn req_launched(body: &str) -> RegisterRequest {
        RegisterRequest {
            launch: true,
            ..req(body)
        }
    }

    #[tokio::test]
    async fn registers_and_versions() {
        let s = store();
        let first = register(&s, req(PARAMETERIZED)).await.unwrap();
        assert_eq!(first.id, "tenant-sync");
        assert_eq!(first.version, 1);
        assert_eq!(first.created_by.as_deref(), Some("tester"));
        assert!(first.params["tenant_id"].required);
        assert_eq!(first.params["page"].default, Some(json!(100)));
        assert_eq!(first.body, PARAMETERIZED, "body stored verbatim");

        let second = register(&s, req(PARAMETERIZED)).await.unwrap();
        assert_eq!(second.version, 2);
        assert_eq!(
            s.template_versions("tenant-sync").await.unwrap(),
            vec![2, 1]
        );
        let listed = list_with_state(&s).await.unwrap();
        assert_eq!(listed.len(), 1, "list folds to one row per id");
        // A register is inert: two versions exist, nothing is live.
        let st = listed[0].state.as_ref().unwrap();
        assert_eq!(st.status, TemplateStatus::Draft);
        assert_eq!(st.newest, Some(2));
        assert_eq!(st.stable, None);
    }

    #[tokio::test]
    async fn register_compiles_transforms_not_just_their_shape() {
        let s = store();
        // `set` takes `values:`; `fields:` is a plausible typo that used to
        // register cleanly and then fail at trigger time.
        let mut bad = req(r#"
version: 1
name: tenant-sync
pipeline:
  source: { type: rest, config: { base_url: "https://x", path: /e } }
  transforms:
    - type: set
      config: { fields: { a: 1 } }
  sink: { type: jsonl, config: { path: ./o.jsonl } }
"#);
        bad.description = None;
        let err = register(&s, bad).await.unwrap_err().to_string();
        assert!(err.contains("values"), "names the missing field: {err}");
        assert!(
            s.template_versions("tenant-sync").await.unwrap().is_empty(),
            "nothing is persisted when validation fails"
        );
    }

    /// A structurally wrong **sink** config is refused at register time (#609),
    /// naming the row and the side it came from.
    ///
    /// This is the half that a source-only check would miss: a template could
    /// be registered and launched with a destination that cannot deserialize,
    /// and the failure would land on the first triggered run.
    #[tokio::test]
    async fn register_rejects_a_structurally_invalid_sink_config() {
        let s = store();
        // `path` is a string; a map cannot deserialize into it.
        let mut bad = req(r#"
version: 1
name: tenant-sync
pipeline:
  source: { type: rest, config: { base_url: "https://x", path: /e } }
  sink: { type: jsonl, config: { path: { nested: wrong } } }
"#);
        bad.description = None;
        let err = register(&s, bad).await.unwrap_err().to_string();
        assert!(err.contains("sink"), "must say which side is wrong: {err}");
        assert!(err.contains("jsonl"), "must name the connector: {err}");
        assert!(
            s.template_versions("tenant-sync").await.unwrap().is_empty(),
            "nothing is persisted when validation fails"
        );
    }

    /// The source counterpart, for symmetry — and to pin that the message says
    /// `source`, so an operator does not go looking at the wrong end.
    #[tokio::test]
    async fn register_rejects_a_structurally_invalid_source_config() {
        let s = store();
        let mut bad = req(r#"
version: 1
name: tenant-sync
pipeline:
  source: { type: rest, config: { base_url: { nested: wrong } } }
  sink: { type: jsonl, config: { path: ./o.jsonl } }
"#);
        bad.description = None;
        let err = register(&s, bad).await.unwrap_err().to_string();
        assert!(
            err.contains("source"),
            "must say which side is wrong: {err}"
        );
        assert!(err.contains("rest"), "must name the connector: {err}");
    }

    #[tokio::test]
    async fn a_description_carries_forward_across_registers() {
        let s = store();
        let first = register(&s, req(PARAMETERIZED)).await.unwrap();
        assert_eq!(first.description.as_deref(), Some("test"));

        // A deploy that re-registers without `--description` must not blank it.
        let mut bare = req(PARAMETERIZED);
        bare.description = None;
        let second = register(&s, bare).await.unwrap();
        assert_eq!(second.description.as_deref(), Some("test"));

        // An explicit description still wins.
        let mut changed = req(PARAMETERIZED);
        changed.description = Some("now something else".into());
        let third = register(&s, changed).await.unwrap();
        assert_eq!(third.description.as_deref(), Some("now something else"));

        // …and the new one is what the next bare register inherits.
        let mut bare2 = req(PARAMETERIZED);
        bare2.description = None;
        let fourth = register(&s, bare2).await.unwrap();
        assert_eq!(fourth.description.as_deref(), Some("now something else"));
    }

    #[tokio::test]
    async fn a_register_never_moves_existing_callers() {
        let s = store();
        register(&s, req_launched(PARAMETERIZED)).await.unwrap(); // v1, launched
        assert_eq!(
            resolve_version(&s, "tenant-sync", VersionSelector::stable())
                .await
                .unwrap(),
            1
        );

        // A nightly lands as v2 — `stable` must not budge. This is the property
        // the whole model exists for.
        register(&s, req(PARAMETERIZED)).await.unwrap();
        assert_eq!(
            resolve_version(&s, "tenant-sync", VersionSelector::stable())
                .await
                .unwrap(),
            1,
            "registering a build must not move the launched version"
        );
        assert_eq!(
            resolve_version(&s, "tenant-sync", VersionSelector::newest())
                .await
                .unwrap(),
            2,
            "`newest` is how you reach the un-launched build"
        );

        // Launching is the deliberate act that moves them.
        let out = launch(&s, "tenant-sync", VersionSelector::newest(), Some("alice"))
            .await
            .unwrap();
        assert_eq!((out.version, out.replaced), (2, Some(1)));
        assert!(!out.first_launch);
        assert_eq!(
            resolve_version(&s, "tenant-sync", VersionSelector::stable())
                .await
                .unwrap(),
            2
        );
        // `previous` is now the version launched before it.
        assert_eq!(
            resolve_version(
                &s,
                "tenant-sync",
                VersionSelector::Channel(VersionChannel::Previous)
            )
            .await
            .unwrap(),
            1
        );
    }

    #[tokio::test]
    async fn draft_template_has_no_stable_and_says_how_to_fix_it() {
        let s = store();
        register(&s, req(PARAMETERIZED)).await.unwrap();
        let state = template_state(&s, "tenant-sync").await.unwrap();
        assert_eq!(state.status, TemplateStatus::Draft);

        // Unpinned resolution fails with the exact command to run — never a
        // silent fallback to the newest build.
        let err = resolve_version(&s, "tenant-sync", VersionSelector::stable())
            .await
            .unwrap_err()
            .to_string();
        assert!(err.contains("no launched version"), "{err}");
        assert!(err.contains("faucet template launch"), "{err}");
        // But explicit selectors work, so a draft is fully testable.
        assert_eq!(
            resolve_version(&s, "tenant-sync", VersionSelector::newest())
                .await
                .unwrap(),
            1
        );
        assert_eq!(
            resolve_version(&s, "tenant-sync", VersionSelector::Pinned(1))
                .await
                .unwrap(),
            1
        );
    }

    #[tokio::test]
    async fn first_launch_flips_status_and_relaunch_is_a_noop() {
        let s = store();
        register(&s, req(PARAMETERIZED)).await.unwrap();
        let out = launch(&s, "tenant-sync", VersionSelector::Pinned(1), None)
            .await
            .unwrap();
        assert!(out.first_launch);
        assert_eq!(out.replaced, None);
        assert_eq!(
            template_state(&s, "tenant-sync").await.unwrap().status,
            TemplateStatus::Launched
        );

        // Re-launching what is already live changes nothing — and crucially does
        // not append, which would make `previous` a duplicate of `stable`.
        let again = launch(&s, "tenant-sync", VersionSelector::Pinned(1), None)
            .await
            .unwrap();
        assert!(again.already_launched);
        assert_eq!(s.template_launches("tenant-sync").await.unwrap().len(), 1);
        let err = resolve_version(
            &s,
            "tenant-sync",
            VersionSelector::Channel(VersionChannel::Previous),
        )
        .await
        .unwrap_err()
        .to_string();
        assert!(err.contains("no previous version"), "{err}");
    }

    #[tokio::test]
    async fn rollback_returns_to_the_prior_launch() {
        let s = store();
        for _ in 0..3 {
            register(&s, req(PARAMETERIZED)).await.unwrap();
        }
        launch(&s, "tenant-sync", VersionSelector::Pinned(1), None)
            .await
            .unwrap();
        launch(&s, "tenant-sync", VersionSelector::Pinned(3), None)
            .await
            .unwrap();

        let out = rollback(&s, "tenant-sync", Some("oncall")).await.unwrap();
        assert_eq!(out.version, 1, "rollback re-launches `previous`");
        assert_eq!(out.replaced, Some(3));
        let state = template_state(&s, "tenant-sync").await.unwrap();
        assert_eq!(state.stable, Some(1));
        assert_eq!(
            state.previous,
            Some(3),
            "previous now points at what we left"
        );

        // The launch log is the audit trail: v1, v3, v1, newest first.
        let log = s.template_launches("tenant-sync").await.unwrap();
        assert_eq!(
            log.iter().map(|l| l.version).collect::<Vec<_>>(),
            vec![1, 3, 1]
        );
        assert_eq!(log[0].launched_by.as_deref(), Some("oncall"));
    }

    #[tokio::test]
    async fn deprecation_is_template_wide_and_reversible() {
        let s = store();
        register(&s, req_launched(PARAMETERIZED)).await.unwrap();

        let status = set_deprecated(
            &s,
            "tenant-sync",
            Some("superseded".into()),
            Some("bob"),
            true,
        )
        .await
        .unwrap();
        assert_eq!(status, TemplateStatus::Deprecated);
        let state = template_state(&s, "tenant-sync").await.unwrap();
        assert_eq!(state.status, TemplateStatus::Deprecated);
        assert_eq!(
            state.deprecation.as_ref().unwrap().reason.as_deref(),
            Some("superseded")
        );
        // Retiring must not break existing callers: `stable` still resolves.
        assert_eq!(
            resolve_version(&s, "tenant-sync", VersionSelector::stable())
                .await
                .unwrap(),
            1
        );
        // But launching into a retired template is refused — reviving it that way
        // is almost certainly a mistake.
        let err = launch(&s, "tenant-sync", VersionSelector::Pinned(1), None)
            .await
            .unwrap_err()
            .to_string();
        assert!(err.contains("deprecated"), "{err}");

        // `--undo` restores the prior status, derived rather than remembered.
        let status = set_deprecated(&s, "tenant-sync", None, None, false)
            .await
            .unwrap();
        assert_eq!(status, TemplateStatus::Launched);
        assert!(
            template_state(&s, "tenant-sync")
                .await
                .unwrap()
                .deprecation
                .is_none()
        );
        // Deprecating a template that does not exist is a typed error.
        assert!(matches!(
            set_deprecated(&s, "nope", None, None, true)
                .await
                .unwrap_err(),
            CliError::UnknownPipelineTemplate { .. }
        ));
    }

    #[tokio::test]
    async fn explicit_id_wins_and_is_validated() {
        let s = store();
        let mut r = req(PARAMETERIZED);
        r.id = Some("my-template".into());
        assert_eq!(register(&s, r).await.unwrap().id, "my-template");

        let mut bad = req(PARAMETERIZED);
        bad.id = Some("Bad Id".into());
        assert!(register(&s, bad).await.is_err());
    }

    #[tokio::test]
    async fn register_requires_an_id_source() {
        let s = store();
        let body = "version: 1\npipeline:\n  source: { type: csv, config: { path: a.csv } }\n  sink: { type: jsonl, config: { path: o.jsonl } }\n";
        let err = register(&s, req(body)).await.unwrap_err().to_string();
        assert!(err.contains("no template id"), "{err}");
    }

    #[tokio::test]
    async fn register_rejects_a_structurally_invalid_config() {
        let s = store();
        let err = register(&s, req("version: 1\nname: x\nnope: 1\npipeline: {}\n"))
            .await
            .unwrap_err()
            .to_string();
        assert!(err.contains("nope") || err.contains("pipeline"), "{err}");
    }

    #[tokio::test]
    async fn register_rejects_an_invalid_params_block() {
        let s = store();
        let body = "version: 1\nname: x\nparams:\n  a: { required: true, default: 1 }\npipeline:\n  source: { type: csv, config: { path: a.csv } }\n  sink: { type: jsonl, config: { path: o.jsonl } }\n";
        let err = register(&s, req(body)).await.unwrap_err().to_string();
        assert!(err.contains("required"), "{err}");
    }

    #[tokio::test]
    async fn register_rejects_a_non_mapping_body() {
        let s = store();
        let err = register(&s, req("- a\n- b\n"))
            .await
            .unwrap_err()
            .to_string();
        assert!(err.contains("mapping"), "{err}");
        let err = register(&s, req(": :\n")).await.unwrap_err().to_string();
        assert!(err.contains("YAML"), "{err}");
    }

    #[tokio::test]
    async fn materialize_binds_params_and_defaults() {
        let s = store();
        register(&s, req_launched(PARAMETERIZED)).await.unwrap();
        let supplied: SuppliedParams = [("tenant_id".to_string(), json!("acme"))].into();
        let want = resolve_version(&s, "tenant-sync", VersionSelector::stable())
            .await
            .unwrap();
        let out = materialize(
            &s,
            "tenant-sync",
            want,
            &supplied,
            &BTreeMap::new(),
            Materialize::Local,
        )
        .await
        .unwrap();
        assert_eq!(out.version, 1);
        assert_eq!(out.name.as_deref(), Some("tenant-sync"));
        assert_eq!(out.format(), ConfigFormat::Json);
        let doc: Value = serde_json::from_str(&out.body).unwrap();
        // The fixture's `${param.*}` tokens are bound across all three places
        // they appear — the path segment and the query value — which is the
        // point of this test. (The shape is `base_url` + `path` +
        // `query_params`; `url` was never a REST config field.)
        // Both `${param.*}` tokens in the path are bound — the required one
        // from the supplied value, `since` from its declared default. (The
        // shape is `base_url` + `path`; `url` was never a REST config field.)
        let src = &doc["pipeline"]["source"]["config"];
        assert_eq!(src["base_url"], "https://api.example.com");
        assert_eq!(src["path"], "/acme/events?since=1970-01-01");
        assert_eq!(out.params_redacted["tenant_id"], json!("acme"));
        assert_eq!(out.params_redacted["page"], json!(100));
        assert!(!out.used_secret_params);
    }

    #[tokio::test]
    async fn materialize_reports_missing_and_unknown_params() {
        let s = store();
        register(&s, req_launched(PARAMETERIZED)).await.unwrap();
        let err = materialize(
            &s,
            "tenant-sync",
            1,
            &SuppliedParams::new(),
            &BTreeMap::new(),
            Materialize::Local,
        )
        .await
        .unwrap_err();
        assert!(matches!(err, CliError::MissingParam { .. }), "{err:?}");

        let supplied: SuppliedParams = [
            ("tenant_id".to_string(), json!("a")),
            ("bogus".to_string(), json!("b")),
        ]
        .into();
        let err = materialize(
            &s,
            "tenant-sync",
            1,
            &supplied,
            &BTreeMap::new(),
            Materialize::Local,
        )
        .await
        .unwrap_err();
        assert!(matches!(err, CliError::UnknownParam { .. }), "{err:?}");
    }

    #[tokio::test]
    async fn unknown_template_and_version_are_typed_errors() {
        let s = store();
        register(&s, req_launched(PARAMETERIZED)).await.unwrap();
        let err = resolve_version(&s, "nope", VersionSelector::stable())
            .await
            .unwrap_err();
        assert!(
            matches!(err, CliError::UnknownPipelineTemplate { ref id, .. } if id == "nope"),
            "{err:?}"
        );
        let supplied: SuppliedParams = [("tenant_id".to_string(), json!("a"))].into();
        let err = materialize(
            &s,
            "tenant-sync",
            9,
            &supplied,
            &BTreeMap::new(),
            Materialize::Local,
        )
        .await
        .unwrap_err();
        assert!(
            matches!(
                err,
                CliError::UnknownPipelineTemplate {
                    version: Some(9),
                    ..
                }
            ),
            "{err:?}"
        );
    }

    #[tokio::test]
    async fn env_overrides_win_over_the_process_environment() {
        let s = store();
        let body = "\
version: 1
name: env-template
pipeline:
  source: { type: rest, config: { base_url: \"https://x\", path: \"/${env:FAUCET_TPL_REGION}\" } }
  sink: { type: jsonl, config: { path: ./o.jsonl } }
";
        unsafe { std::env::set_var("FAUCET_TPL_REGION", "from-process") };
        register(&s, req_launched(body)).await.unwrap();

        let out = materialize(
            &s,
            "env-template",
            1,
            &SuppliedParams::new(),
            &BTreeMap::new(),
            Materialize::Local,
        )
        .await
        .unwrap();
        let doc: Value = serde_json::from_str(&out.body).unwrap();
        assert_eq!(doc["pipeline"]["source"]["config"]["path"], "/from-process");

        let overrides: BTreeMap<String, String> =
            [("FAUCET_TPL_REGION".to_string(), "from-request".to_string())].into();
        let out = materialize(
            &s,
            "env-template",
            1,
            &SuppliedParams::new(),
            &overrides,
            Materialize::Local,
        )
        .await
        .unwrap();
        let doc: Value = serde_json::from_str(&out.body).unwrap();
        assert_eq!(doc["pipeline"]["source"]["config"]["path"], "/from-request");
        assert_eq!(std::env::var("FAUCET_TPL_REGION").unwrap(), "from-process");
        unsafe { std::env::remove_var("FAUCET_TPL_REGION") };
    }

    #[tokio::test]
    async fn secret_params_are_flagged_and_redacted() {
        let s = store();
        let body = "\
version: 1
name: secret-template
params:
  api_token: { required: true, secret: true }
pipeline:
  source:
    type: rest
    config:
      base_url: https://api.example.com
      path: /events
      auth: { type: bearer, config: { token: \"${param.api_token}\" } }
  sink: { type: jsonl, config: { path: ./o.jsonl } }
";
        register(&s, req_launched(body)).await.unwrap();
        let supplied: SuppliedParams =
            [("api_token".to_string(), json!("tok-abcdefghijklmnop"))].into();
        let out = materialize(
            &s,
            "secret-template",
            1,
            &supplied,
            &BTreeMap::new(),
            Materialize::Local,
        )
        .await
        .unwrap();
        assert!(out.used_secret_params);
        assert_eq!(out.params_redacted["api_token"], json!("***"));
        assert!(out.body.contains("tok-abcdefghijklmnop"));
        assert_eq!(
            crate::secrets::registry::redact("token=tok-abcdefghijklmnop"),
            "token=***"
        );
    }

    #[tokio::test]
    async fn channels_are_promoted_independently_of_launching() {
        let s = store();
        register(&s, req_launched(PARAMETERIZED)).await.unwrap(); // v1 live
        register(&s, req(PARAMETERIZED)).await.unwrap(); // v2 draft build
        let mut tagged = req(PARAMETERIZED);
        tagged.tags = vec![VersionChannel::Dev];
        register(&s, tagged).await.unwrap(); // v3, dev=v3

        assert_eq!(
            resolve_version(
                &s,
                "tenant-sync",
                VersionSelector::Channel(VersionChannel::Dev)
            )
            .await
            .unwrap(),
            3
        );
        // Promoting an environment channel never touches what is live.
        assert_eq!(
            promote(
                &s,
                "tenant-sync",
                VersionChannel::PreProd,
                VersionSelector::Channel(VersionChannel::Dev)
            )
            .await
            .unwrap(),
            3
        );
        let state = template_state(&s, "tenant-sync").await.unwrap();
        assert_eq!(state.stable, Some(1), "promote must not move `stable`");
        assert_eq!(state.tags["dev"], 3);
        assert_eq!(state.tags["pre-prod"], 3);
        assert!(!state.tags.contains_key("stable"), "derived, never stored");

        // Launching *from* a channel is the promotion pipeline's last step.
        let out = launch(
            &s,
            "tenant-sync",
            VersionSelector::Channel(VersionChannel::PreProd),
            None,
        )
        .await
        .unwrap();
        assert_eq!((out.version, out.replaced), (3, Some(1)));
    }

    #[tokio::test]
    async fn derived_channels_cannot_be_promoted() {
        let s = store();
        register(&s, req_launched(PARAMETERIZED)).await.unwrap();
        for (tag, needle) in [
            (VersionChannel::Stable, "launch"),
            (VersionChannel::Previous, "launched before"),
            (VersionChannel::Newest, "highest version"),
        ] {
            let err = promote(&s, "tenant-sync", tag, VersionSelector::Pinned(1))
                .await
                .unwrap_err()
                .to_string();
            assert!(err.contains("derived"), "{tag}: {err}");
            assert!(err.contains(needle), "{tag}: {err}");
        }
        // Same guard on the register path, before anything is written.
        let mut bad = req(PARAMETERIZED);
        bad.tags = vec![VersionChannel::Stable];
        assert!(register(&s, bad).await.is_err());
        assert_eq!(
            s.template_versions("tenant-sync").await.unwrap(),
            vec![1],
            "the rejected register must not have appended a version"
        );
    }

    #[tokio::test]
    async fn promoting_to_a_missing_version_is_rejected() {
        let s = store();
        register(&s, req(PARAMETERIZED)).await.unwrap();
        let err = promote(
            &s,
            "tenant-sync",
            VersionChannel::Prod,
            VersionSelector::Pinned(9),
        )
        .await
        .unwrap_err();
        assert!(
            matches!(
                err,
                CliError::UnknownPipelineTemplate {
                    version: Some(9),
                    ..
                }
            ),
            "{err:?}"
        );
        assert!(matches!(
            promote(&s, "nope", VersionChannel::Prod, VersionSelector::Pinned(1))
                .await
                .unwrap_err(),
            CliError::UnknownPipelineTemplate { .. }
        ));
    }

    #[tokio::test]
    async fn deleting_a_version_drops_pointers_aimed_at_it() {
        let s = store();
        register(&s, req(PARAMETERIZED)).await.unwrap();
        register(&s, req(PARAMETERIZED)).await.unwrap();
        launch(&s, "tenant-sync", VersionSelector::Pinned(1), None)
            .await
            .unwrap();
        launch(&s, "tenant-sync", VersionSelector::Pinned(2), None)
            .await
            .unwrap();
        promote(
            &s,
            "tenant-sync",
            VersionChannel::Prod,
            VersionSelector::Pinned(1),
        )
        .await
        .unwrap();

        // Deleting v1 must leave neither a channel nor a launch entry pointing at
        // it — otherwise `previous` or `prod` would resolve to a missing version.
        assert_eq!(s.template_delete("tenant-sync", Some(1)).await.unwrap(), 1);
        let state = template_state(&s, "tenant-sync").await.unwrap();
        assert!(!state.tags.contains_key("prod"), "{:?}", state.tags);
        assert_eq!(state.stable, Some(2));
        assert_eq!(state.previous, None, "v1's launch entry went with it");

        s.template_delete("tenant-sync", None).await.unwrap();
        assert!(s.template_launches("tenant-sync").await.unwrap().is_empty());
        assert!(s.template_tags("tenant-sync").await.unwrap().is_empty());
    }

    #[tokio::test]
    async fn delete_removes_one_version_or_all() {
        let s = store();
        register(&s, req(PARAMETERIZED)).await.unwrap();
        register(&s, req(PARAMETERIZED)).await.unwrap();
        assert_eq!(s.template_delete("tenant-sync", Some(1)).await.unwrap(), 1);
        assert_eq!(s.template_versions("tenant-sync").await.unwrap(), vec![2]);
        assert_eq!(s.template_delete("tenant-sync", None).await.unwrap(), 1);
        assert!(s.template_list().await.unwrap().is_empty());
        assert_eq!(s.template_delete("tenant-sync", None).await.unwrap(), 0);
        assert_eq!(s.template_delete("tenant-sync", Some(3)).await.unwrap(), 0);
    }

    #[tokio::test]
    async fn registers_a_topology_config() {
        let s = store();
        let body = "\
version: 1
name: topo-template
params:
  path: { default: ./in.csv }
pipeline:
  sources:
    s: { type: csv, config: { path: \"${param.path}\" } }
  sinks:
    o: { type: jsonl, config: { path: ./out.jsonl } }
  nodes:
    src: { kind: source, ref: s }
    w: { kind: sink, ref: o }
  edges:
    - { from: src, to: w }
";
        let rec = register(&s, req_launched(body)).await.unwrap();
        assert_eq!(rec.id, "topo-template");
        let out = materialize(
            &s,
            "topo-template",
            1,
            &SuppliedParams::new(),
            &BTreeMap::new(),
            Materialize::Local,
        )
        .await
        .unwrap();
        let doc: Value = serde_json::from_str(&out.body).unwrap();
        assert_eq!(
            doc["pipeline"]["sources"]["s"]["config"]["path"],
            "./in.csv"
        );
    }

    #[tokio::test]
    async fn version_history_is_bounded() {
        use crate::serve::history::templates::VERSION_RETAIN;
        let s = store();
        for _ in 0..(VERSION_RETAIN + 3) {
            register(&s, req(PARAMETERIZED)).await.unwrap();
        }
        let versions = s.template_versions("tenant-sync").await.unwrap();
        assert_eq!(versions.len(), VERSION_RETAIN);
        assert_eq!(versions[0], (VERSION_RETAIN + 3) as u32, "newest kept");
        assert!(!versions.contains(&1), "oldest pruned");
    }

    // ── kind-aware registry (source × sink) ─────────────────────────────────

    /// A runnable source template: two CSV exports become two streams.
    fn source_template(dir: &std::path::Path) -> String {
        std::fs::write(dir.join("orders.csv"), "id,total\n1,10\n2,20\n").unwrap();
        std::fs::write(dir.join("customers.csv"), "id,name\n1,alice\n").unwrap();
        format!(
            "kind: source-template
name: acme-exports
description: Acme — orders and customers exports
params:
  data_dir: {{ type: string, default: {} }}
source:
  type: csv
  config:
    path: \"${{param.data_dir}}/orders.csv\"
transforms:
  - {{ type: keys_case, config: {{ mode: snake }} }}
streams:
  - name: orders
    primary_keys: [id]
    write: [overwrite, upsert]
  - name: customers
    source: {{ config: {{ path: \"${{param.data_dir}}/customers.csv\" }} }}
    primary_keys: [id]
    write: [overwrite, upsert]
",
            dir.display()
        )
    }

    /// A local JSON Lines sink template; `overwrite` satisfied by rewriting the file.
    fn sink_template(dir: &std::path::Path) -> String {
        format!(
            "kind: sink-template
name: local-jsonl
description: Local JSON Lines files, one per stream
params:
  out_dir: {{ type: string, default: {} }}
sink:
  type: jsonl
  config:
    append: false
per_stream:
  path: \"${{param.out_dir}}/${{source}}/${{stream}}.jsonl\"
write_mode_aliases:
  overwrite: append
",
            dir.display()
        )
    }

    #[tokio::test]
    async fn hub_kinds_register_under_their_name_and_keep_their_kind() {
        let dir = tempfile::tempdir().unwrap();
        let s = store();
        let src = register(&s, req_launched(&source_template(dir.path())))
            .await
            .unwrap();
        assert_eq!(src.id, "acme-exports", "a hub template's id is its name");
        assert_eq!(src.kind, TemplateKind::SourceTemplate);
        assert_eq!(
            src.params["data_dir"]
                .default
                .as_ref()
                .map(|v| v.is_string()),
            Some(true)
        );
        // The template's own description is used when the request carries none.
        let mut no_desc = req(&source_template(dir.path()));
        no_desc.description = None;
        let again = register(&s, no_desc).await.unwrap();
        assert_eq!(again.version, 2);
        assert_eq!(
            again.description.as_deref(),
            Some("test"),
            "previous description carries forward"
        );

        let mut fresh = req_launched(&sink_template(dir.path()));
        fresh.description = None;
        let sink = register(&s, fresh).await.unwrap();
        assert_eq!(sink.kind, TemplateKind::SinkTemplate);
        assert_eq!(
            sink.description.as_deref(),
            Some("Local JSON Lines files, one per stream")
        );

        // An explicit id must agree with `name`.
        let mut wrong_id = req(&sink_template(dir.path()));
        wrong_id.id = Some("elsewhere".into());
        let err = register(&s, wrong_id).await.unwrap_err().to_string();
        assert!(err.contains("registered under its own hub id"), "{err}");

        // A pipeline cannot take over a source template's id (or vice versa).
        let mut takeover = req(PARAMETERIZED);
        takeover.id = Some("acme-exports".into());
        let err = register(&s, takeover).await.unwrap_err().to_string();
        assert!(err.contains("is a source-template"), "{err}");
        assert!(
            err.contains("cannot be registered under the same id"),
            "{err}"
        );

        let listed = list_with_state(&s).await.unwrap();
        assert_eq!(listed.len(), 2);
        assert!(
            listed
                .iter()
                .any(|t| t.kind == TemplateKind::SourceTemplate)
        );
        assert!(listed.iter().any(|t| t.kind == TemplateKind::SinkTemplate));
    }

    #[tokio::test]
    async fn hub_registration_runs_validation_and_the_publishability_lint() {
        let s = store();
        // Structurally broken: a stream referencing an undeclared param.
        let err = register(
            &s,
            req("kind: source-template\nname: bad\nsource: { type: csv, config: { path: \"${param.nope}\" } }\nstreams: [{ name: a }]\n"),
        )
        .await
        .unwrap_err()
        .to_string();
        assert!(err.contains("nope"), "{err}");

        // A literal credential is a lint failure the registry refuses to store.
        let err = register(
            &s,
            req("kind: source-template\nname: leaky\nsource:\n  type: rest\n  config:\n    base_url: https://api.example.com\n    auth: { type: bearer, config: { token: hunter2secretvalue } }\nstreams: [{ name: a }]\n"),
        )
        .await
        .unwrap_err()
        .to_string();
        assert!(err.contains("cannot be registered"), "{err}");

        // A missing description is a nit, not a refusal.
        register(
            &s,
            req("kind: sink-template\nname: terse\nsink: { type: jsonl, config: {} }\nper_stream: { path: \"./${stream}.jsonl\" }\n"),
        )
        .await
        .expect("missing description does not block");
    }

    #[tokio::test]
    async fn materialize_refuses_hub_kinds_and_for_run_dispatches_on_kind() {
        let dir = tempfile::tempdir().unwrap();
        let s = store();
        register(&s, req_launched(&source_template(dir.path())))
            .await
            .unwrap();
        register(&s, req_launched(&sink_template(dir.path())))
            .await
            .unwrap();
        register(&s, req_launched(PARAMETERIZED)).await.unwrap();
        let none = BTreeMap::new();
        let supplied = SuppliedParams::new();

        // The plain materialize is pipeline-only and says how to run a hub kind.
        let err = materialize(&s, "acme-exports", 1, &supplied, &none, Materialize::Local)
            .await
            .unwrap_err()
            .to_string();
        assert!(err.contains("--sink <id>"), "{err}");
        let err = materialize(&s, "local-jsonl", 1, &supplied, &none, Materialize::Local)
            .await
            .unwrap_err()
            .to_string();
        assert!(err.contains("not runnable on its own"), "{err}");

        // A pipeline refuses a sink; a source requires one; a sink is never runnable.
        let with_sink = SinkChoice {
            id: Some("local-jsonl".into()),
            version: Default::default(),
            overlay: None,
        };
        let err = materialize_for_run(
            &s,
            "tenant-sync",
            1,
            &with_sink,
            &supplied,
            &none,
            Materialize::Local,
        )
        .await
        .unwrap_err()
        .to_string();
        assert!(err.contains("takes no sink"), "{err}");
        let err = materialize_for_run(
            &s,
            "acme-exports",
            1,
            &SinkChoice::default(),
            &supplied,
            &none,
            Materialize::Local,
        )
        .await
        .unwrap_err()
        .to_string();
        assert!(err.contains("--kind sink-template"), "{err}");
        let err = materialize_for_run(
            &s,
            "local-jsonl",
            1,
            &SinkChoice::default(),
            &supplied,
            &none,
            Materialize::Local,
        )
        .await
        .unwrap_err()
        .to_string();
        assert!(err.contains("--sink local-jsonl"), "{err}");

        // `sink` must name a sink template, and the source side a source template.
        let pipeline_as_sink = SinkChoice {
            id: Some("tenant-sync".into()),
            version: Default::default(),
            overlay: None,
        };
        let err = materialize_for_run(
            &s,
            "acme-exports",
            1,
            &pipeline_as_sink,
            &supplied,
            &none,
            Materialize::Local,
        )
        .await
        .unwrap_err()
        .to_string();
        assert!(err.contains("`sink` must name a sink-template"), "{err}");
        let err = materialize_pair(
            &s,
            ("tenant-sync", 1),
            ("local-jsonl", 1),
            &supplied,
            &none,
            Materialize::Local,
        )
        .await
        .unwrap_err()
        .to_string();
        assert!(err.contains("source side of a pairing"), "{err}");

        // The pairing composes: one matrix row per stream, write modes resolved,
        // both halves' params bound, provenance on both sides.
        let m = materialize_for_run(
            &s,
            "acme-exports",
            1,
            &with_sink,
            &supplied,
            &none,
            Materialize::Local,
        )
        .await
        .expect("composes");
        assert_eq!(m.template_id, "acme-exports");
        assert_eq!(m.sink_id.as_deref(), Some("local-jsonl"));
        assert_eq!(m.sink_version, Some(1));
        assert_eq!(m.name.as_deref(), Some("acme-exports"));
        let names: Vec<&str> = m.streams.iter().map(|p| p.stream.as_str()).collect();
        assert_eq!(names, ["orders", "customers"]);
        let body: Value = serde_json::from_str(&m.body).unwrap();
        assert_eq!(body["matrix"].as_array().map(Vec::len), Some(2));
        let row = &body["matrix"][0];
        assert_eq!(row["id"], json!("orders"));
        // jsonl has no `WriteSpec`, so the composer records the alias in the plan
        // rather than writing a `write_mode` the connector would reject.
        assert!(row["sink"]["config"].get("write_mode").is_none());
        assert_eq!(m.streams[0].chosen, faucet_core::WriteMode::Append);
        assert_eq!(
            m.streams[0].satisfies,
            Some(faucet_core::WriteMode::Overwrite),
            "overwrite aliased to append on jsonl"
        );
        assert!(
            row["sink"]["config"]["path"]
                .as_str()
                .unwrap()
                .ends_with("/acme-exports/orders.jsonl")
        );
        assert!(
            body.get("params").is_none(),
            "params block dropped after binding"
        );
        assert!(
            m.params_redacted.contains_key("out_dir"),
            "sink params bound too"
        );
        assert!(m.params_redacted.contains_key("data_dir"));

        // An unknown sink version is the same typed error as an unknown template.
        let pinned = SinkChoice {
            id: Some("local-jsonl".into()),
            version: VersionSelector::Pinned(9),
            overlay: None,
        };
        let err = materialize_for_run(
            &s,
            "acme-exports",
            1,
            &pinned,
            &supplied,
            &none,
            Materialize::Local,
        )
        .await
        .unwrap_err();
        assert!(
            matches!(err, CliError::UnknownPipelineTemplate { ref id, version: Some(9) } if id == "local-jsonl"),
            "{err}"
        );
    }

    #[tokio::test]
    async fn deployments_register_and_overlay_a_composed_trigger() {
        let s = store();
        let dir = tempfile::tempdir().unwrap();
        register(&s, req_launched(&source_template(dir.path())))
            .await
            .unwrap();
        register(&s, req_launched(&sink_template(dir.path())))
            .await
            .unwrap();
        register(&s, req_launched(PARAMETERIZED)).await.unwrap();
        let state_dir = dir.path().join("state");
        let overlay_yaml = format!(
            "kind: deployment\nname: prod-ops\ndescription: prod\nstate: {{ type: file, config: {{ path: '{}' }} }}\nstreams:\n  orders: {{ delivery: at_least_once }}\n",
            state_dir.display()
        );
        let rec = register(&s, req_launched(&overlay_yaml)).await.unwrap();
        assert_eq!(rec.id, "prod-ops");
        assert_eq!(rec.kind, TemplateKind::Deployment);
        let none = BTreeMap::new();
        let supplied = SuppliedParams::new();
        let run = |overlay: Option<OverlayChoice>| SinkChoice {
            id: Some("local-jsonl".into()),
            version: Default::default(),
            overlay,
        };

        // A registered overlay lands on the composed document with provenance.
        let m = materialize_for_run(
            &s,
            "acme-exports",
            1,
            &run(Some(OverlayChoice::Registered {
                id: "prod-ops".into(),
                version: Default::default(),
            })),
            &supplied,
            &none,
            Materialize::Local,
        )
        .await
        .unwrap();
        assert_eq!(m.overlay_id.as_deref(), Some("prod-ops"));
        assert_eq!(m.overlay_version, Some(1));
        assert_eq!(
            m.overlay_contributes,
            vec!["pipeline.state", "matrix.orders.delivery"]
        );
        let body: Value = serde_json::from_str(&m.body).unwrap();
        assert_eq!(body["pipeline"]["state"]["type"], json!("file"));

        // An inline overlay may omit `kind:` / `name:`.
        let m = materialize_for_run(
            &s,
            "acme-exports",
            1,
            &run(Some(OverlayChoice::Inline(
                json!({ "state": { "type": "memory" } }),
            ))),
            &supplied,
            &none,
            Materialize::Local,
        )
        .await
        .unwrap();
        assert_eq!(m.overlay_id.as_deref(), Some("inline"));
        assert_eq!(m.overlay_version, None);
        let err = materialize_for_run(
            &s,
            "acme-exports",
            1,
            &run(Some(OverlayChoice::Inline(json!(["not", "a", "mapping"])))),
            &supplied,
            &none,
            Materialize::Local,
        )
        .await
        .unwrap_err()
        .to_string();
        assert!(
            err.contains("a registered deployment id or a mapping"),
            "{err}"
        );

        // `overlay` must name a deployment; a deployment is never runnable; a
        // pipeline takes no overlay.
        let err = materialize_for_run(
            &s,
            "acme-exports",
            1,
            &run(Some(OverlayChoice::Registered {
                id: "local-jsonl".into(),
                version: Default::default(),
            })),
            &supplied,
            &none,
            Materialize::Local,
        )
        .await
        .unwrap_err()
        .to_string();
        assert!(err.contains("`overlay` must name a deployment"), "{err}");
        for id in ["prod-ops"] {
            let err = materialize_for_run(
                &s,
                id,
                1,
                &SinkChoice::default(),
                &supplied,
                &none,
                Materialize::Local,
            )
            .await
            .unwrap_err()
            .to_string();
            assert!(
                err.contains("deployment overlay and is not runnable"),
                "{err}"
            );
            let err = materialize(&s, id, 1, &supplied, &none, Materialize::Local)
                .await
                .unwrap_err()
                .to_string();
            assert!(
                err.contains("deployment overlay and is not runnable"),
                "{err}"
            );
        }
        let err = materialize_for_run(
            &s,
            "tenant-sync",
            1,
            &SinkChoice {
                id: None,
                version: Default::default(),
                overlay: Some(OverlayChoice::Inline(json!({}))),
            },
            &supplied,
            &none,
            Materialize::Local,
        )
        .await
        .unwrap_err()
        .to_string();
        assert!(err.contains("an overlay applies to a composed"), "{err}");

        // A deployment cannot hide a literal credential in a shared registry,
        // and an id's kind is fixed once registered.
        let err = register(
            &s,
            req("kind: deployment\nname: leaky\nstate: { type: postgres, config: { url: \"postgres://u:hunter2@db/x\" } }\n"),
        )
        .await
        .unwrap_err()
        .to_string();
        assert!(err.contains("literal password"), "{err}");
        let err = register(
            &s,
            req("kind: deployment\nname: local-jsonl\nstate: { type: memory }\n"),
        )
        .await
        .unwrap_err()
        .to_string();
        assert!(err.contains("is a sink-template"), "{err}");
    }

    #[tokio::test]
    async fn store_url_grammar() {
        assert!(resolve_store_url("memory").await.is_ok());
        // `RunHistory` is not Debug, so match rather than `unwrap_err`.
        match resolve_store_url("mysql://nope").await {
            Ok(_) => panic!("an unrecognised scheme must be rejected"),
            Err(e) => assert!(e.to_string().contains("template store"), "{e}"),
        }
        // SQL schemes are recognised even without the build feature — the error
        // then names the missing feature rather than the URL grammar.
        let dir = tempfile::tempdir().unwrap();
        let url = format!("sqlite:{}", dir.path().join("t.db").display());
        if let Err(e) = resolve_store_url(&url).await {
            assert!(e.to_string().contains("serve-history-sqlite"), "{e}");
        }
    }
}