oxdock-ssh-plugin 0.49.0-alpha

Ephemeral user-space SSH server and client for OxDock scripts, bridged to stdio pipelines through the Engine facade.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
//! SSH user-facing errors behind single constructors with stable keys.
//!
//! This module owns every SSH template and key (`ssh.*`). It builds on
//! the generic [`oxdock_diagnostics::Diagnostic`] transport but owns its
//! wording: changing SSH text happens here, never at the call sites.
//! Call sites pass structured data only, never `format!` strings.
//! Lock-poison and cancellation failures are parameterless (the wording
//! is fixed); I/O failures take the concrete `std::io::Error`; worker
//! and function labels ride as structured fields with rendering here.

use oxdock_diagnostics::{Diagnostic, flatten_chain};
use oxdock_parser::{HANDLE_TYPE_NAME, KEYWORD_ASYNC, KEYWORD_LET};

/// Structured field keys. Single spelling for the whole module:
/// constructors reference these, never literals, so a key rename is
/// one edit.
const FIELD_FUNC: &str = "func";
const FIELD_WORKER: &str = "worker";
const FIELD_NAME: &str = "name";
const FIELD_RAW: &str = "raw";
const FIELD_PATH: &str = "path";
const FIELD_ALGORITHM: &str = "algorithm";
const FIELD_MODE: &str = "mode";
const FIELD_ERR: &str = "err";
const FIELD_CODE: &str = "code";
const FIELD_OPTION: &str = "option";
const FIELD_GOT: &str = "got";

/// `key_path` escapes the workspace guard. Takes the invoking func
/// and the raw path; quoting lives here.
pub fn key_path_escapes(func: &str, raw: &str) -> Diagnostic {
    let message = format!("{func} key_path escapes the workspace: {raw}");
    Diagnostic::new(
        "ssh.key.escapes_workspace",
        message,
        Diagnostic::make_fields(&[(FIELD_FUNC, func), (FIELD_RAW, raw)]),
    )
}

/// Pre-existing host key is not Ed25519. Takes the invoking func,
/// the display path, and the algorithm (`Debug` rendering lives
/// here).
pub fn host_key_must_be_ed25519(
    func: &str,
    path: &str,
    algorithm: &impl std::fmt::Debug,
) -> Diagnostic {
    let algorithm_text = format!("{algorithm:?}");
    let message = format!("{func} host key {path} must be Ed25519, found {algorithm_text}");
    Diagnostic::new(
        "ssh.key.must_be_ed25519",
        message,
        Diagnostic::make_fields(&[
            (FIELD_FUNC, func),
            (FIELD_PATH, path),
            (FIELD_ALGORITHM, algorithm_text.as_str()),
        ]),
    )
}

/// Pre-existing host key file has unsafe permissions. Takes the
/// invoking func, the display path, and the raw mode; octal
/// rendering lives here.
pub fn host_key_insecure_permissions(func: &str, path: &str, mode: u32) -> Diagnostic {
    let mode_text = format!("0{mode:o}");
    let message = format!(
        "{func} insecure permissions on host key file {path}: expected 0600, got {mode_text}"
    );
    Diagnostic::new(
        "ssh.key.insecure_permissions",
        message,
        Diagnostic::make_fields(&[
            (FIELD_FUNC, func),
            (FIELD_PATH, path),
            (FIELD_MODE, mode_text.as_str()),
        ]),
    )
}

/// Workspace resolver for the host key cannot open. Takes the
/// invoking func and the underlying failure; the outer message
/// stays bare while the cause rides a chained source, so
/// `{err:#}` keeps the legacy `": "` join. Call sites stay
/// `format!`-free.
pub fn host_key_resolver_failed(func: &str, reason: &Diagnostic) -> Diagnostic {
    let reason_text = flatten_chain(reason);
    Diagnostic::new(
        "ssh.key.resolver_failed",
        format!("{func} cannot open the workspace resolver"),
        Diagnostic::make_fields(&[(FIELD_FUNC, func), (FIELD_ERR, &reason_text)]),
    )
    .with_source(std::sync::Arc::new(std::io::Error::other(reason_text)))
}

/// Ed25519 host key generation failed. Takes the concrete failure
/// by value; the outer message stays bare while the cause rides a
/// chained source.
pub fn host_key_generate_failed(
    reason: impl std::error::Error + Send + Sync + 'static,
) -> Diagnostic {
    let reason_text = reason.to_string();
    Diagnostic::new(
        "ssh.key.generate_failed",
        "generate Ed25519 host key".to_string(),
        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
    )
    .with_source(std::sync::Arc::new(reason))
}

/// Ed25519 host key PEM encoding failed. Takes the concrete failure
/// by value; bare outer message plus chained source.
pub fn host_key_encode_failed(
    reason: impl std::error::Error + Send + Sync + 'static,
) -> Diagnostic {
    let reason_text = reason.to_string();
    Diagnostic::new(
        "ssh.key.encode_failed",
        "encode Ed25519 host key".to_string(),
        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
    )
    .with_source(std::sync::Arc::new(reason))
}

/// `key_path` parent guard cannot anchor the workspace root. Takes
/// the invoking func and the underlying failure; bare outer message
/// plus chained source.
pub fn key_guard_failed(func: &str, reason: &Diagnostic) -> Diagnostic {
    let reason_text = flatten_chain(reason);
    Diagnostic::new(
        "ssh.key.guard_failed",
        format!("{func} cannot guard the workspace root"),
        Diagnostic::make_fields(&[(FIELD_FUNC, func), (FIELD_ERR, &reason_text)]),
    )
    .with_source(std::sync::Arc::new(std::io::Error::other(reason_text)))
}

/// Host key file metadata cannot be read. Takes the display path
/// and the underlying failure; bare outer message plus chained
/// source.
pub fn host_key_stat_failed(path: &str, reason: &Diagnostic) -> Diagnostic {
    let reason_text = flatten_chain(reason);
    Diagnostic::new(
        "ssh.key.stat_failed",
        format!("cannot stat host key {path}"),
        Diagnostic::make_fields(&[(FIELD_PATH, path), (FIELD_ERR, &reason_text)]),
    )
    .with_source(std::sync::Arc::new(std::io::Error::other(reason_text)))
}

/// Host key file bytes cannot be read. Takes the invoking func, the
/// display path, and the underlying failure; bare outer message
/// plus chained source.
pub fn host_key_read_failed(func: &str, path: &str, reason: &Diagnostic) -> Diagnostic {
    let reason_text = flatten_chain(reason);
    Diagnostic::new(
        "ssh.key.read_failed",
        format!("{func} cannot read host key {path}"),
        Diagnostic::make_fields(&[
            (FIELD_FUNC, func),
            (FIELD_PATH, path),
            (FIELD_ERR, &reason_text),
        ]),
    )
    .with_source(std::sync::Arc::new(std::io::Error::other(reason_text)))
}

/// Host key bytes do not parse as OpenSSH. Takes the invoking func,
/// the display path, and the concrete failure by value; bare outer
/// message plus chained source.
pub fn host_key_parse_failed(
    func: &str,
    path: &str,
    reason: impl std::error::Error + Send + Sync + 'static,
) -> Diagnostic {
    let reason_text = reason.to_string();
    Diagnostic::new(
        "ssh.key.parse_failed",
        format!("{func} cannot parse host key {path}"),
        Diagnostic::make_fields(&[
            (FIELD_FUNC, func),
            (FIELD_PATH, path),
            (FIELD_ERR, &reason_text),
        ]),
    )
    .with_source(std::sync::Arc::new(reason))
}

/// Host key file write failed past the first-start race. Takes
/// the underlying failure verbatim: the file layer already names
/// the path, so the message passes through unchanged.
/// Already-keyed payloads are reclaimed by [`from_anyhow`], so
/// this only names genuinely unkeyed fallout.
pub fn host_key_write_failed(reason: &Diagnostic) -> Diagnostic {
    host_key_write_failed_fallback(&flatten_chain(reason))
}

/// Unkeyed host-key write fallback. Takes the pre-rendered anyhow
/// chain text verbatim; rendering lives here. Single owner for the
/// `from_anyhow` fallback so the anyhow-to-Diagnostic direction
/// keeps one keyed home.
pub fn host_key_write_failed_fallback(reason_text: &str) -> Diagnostic {
    Diagnostic::new(
        "ssh.key.write_failed",
        reason_text.to_string(),
        Diagnostic::make_fields(&[(FIELD_ERR, reason_text)]),
    )
}

/// Reclaim an already-keyed diagnostic from an `anyhow` failure,
/// wrapping anything else as a host-key write failure. Single
/// owner for the unkeyed-fallout mapping so genuinely foreign
/// failures keep one keyed home instead of restating the
/// downcast. Outer `.context(...)` frames above the keyed payload
/// replay natively via `with_contexts` (native `?`
/// propagation), so no chain text is lost at the flip
/// boundary.
pub fn from_anyhow(err: anyhow::Error) -> Diagnostic {
    if let Some(found) = err
        .chain()
        .find_map(|cause| cause.downcast_ref::<Diagnostic>())
        .cloned()
    {
        let mut outer_frames: Vec<String> = Vec::new();
        for cause in err.chain() {
            if cause.downcast_ref::<Diagnostic>().is_some() {
                break;
            }
            // Transport echoes (`anyhow::Error::from(diag)`)
            // restate the reclaimed message verbatim; keeping
            // them would print every cause twice.
            if cause.to_string() != found.message() {
                outer_frames.push(cause.to_string());
            }
        }
        return found.with_contexts(outer_frames);
    }
    host_key_write_failed_fallback(&format!("{err:#}"))
}

/// Guarded join of a `key_path` escapes the workspace. Takes the
/// invoking func, the raw path, and the guard failure; the outer
/// message names the escape while the resolved-path detail rides a
/// chained source, so it keeps its own key.
pub fn key_path_join_failed(func: &str, raw: &str, reason: &Diagnostic) -> Diagnostic {
    let reason_text = flatten_chain(reason);
    Diagnostic::new(
        "ssh.key.join_failed",
        format!("{func} key_path escapes the workspace: {raw}"),
        Diagnostic::make_fields(&[
            (FIELD_FUNC, func),
            (FIELD_RAW, raw),
            (FIELD_ERR, &reason_text),
        ]),
    )
    .with_source(std::sync::Arc::new(std::io::Error::other(reason_text)))
}

/// SSH pump output lock poisoned. Parameterless: the wording is
/// fixed.
pub fn pump_output_lock_poisoned() -> Diagnostic {
    Diagnostic::new(
        "ssh.pump.output_lock_poisoned",
        "SSH pump output lock poisoned".to_string(),
        Diagnostic::make_fields(&[]),
    )
}

/// SSH pump input pipe read failed. Takes the I/O failure;
/// rendering lives here.
pub fn pump_input_read_failed(err: &std::io::Error) -> Diagnostic {
    let err_text = err.to_string();
    let message = format!("SSH pump input pipe read failed: {err_text}");
    Diagnostic::new(
        "ssh.pump.input_read_failed",
        message,
        Diagnostic::make_fields(&[(FIELD_ERR, err_text.as_str())]),
    )
}

/// SSH pump worker panicked. Takes the worker label; rendering
/// lives here.
pub fn pump_worker_panicked(worker: &str) -> Diagnostic {
    let message = format!("SSH pump {worker} panicked");
    Diagnostic::new(
        "ssh.pump.worker_panicked",
        message,
        Diagnostic::make_fields(&[(FIELD_WORKER, worker)]),
    )
}

/// SSH pump interrupted by cancellation. Parameterless: the wording
/// is fixed.
pub fn pump_interrupted_by_cancellation() -> Diagnostic {
    Diagnostic::new(
        "ssh.pump.interrupted_by_cancellation",
        "SSH pump interrupted by cancellation".to_string(),
        Diagnostic::make_fields(&[]),
    )
}

/// `SSH_PUMP` source pipe read failed. Takes the I/O failure;
/// rendering lives here.
pub fn pump_source_read_failed(err: &std::io::Error) -> Diagnostic {
    let err_text = err.to_string();
    let message = format!("SSH_PUMP source pipe read failed: {err_text}");
    Diagnostic::new(
        "ssh.pump.source_read_failed",
        message,
        Diagnostic::make_fields(&[(FIELD_ERR, err_text.as_str())]),
    )
}

/// `SSH_PUMP` target lock poisoned. Parameterless: the wording is
/// fixed.
pub fn pump_target_lock_poisoned() -> Diagnostic {
    Diagnostic::new(
        "ssh.pump.target_lock_poisoned",
        "SSH_PUMP target lock poisoned".to_string(),
        Diagnostic::make_fields(&[]),
    )
}

/// Pipe-to-sink input read failed. Takes the caller label plus the
/// I/O failure; rendering lives here.
pub fn pump_func_input_read_failed(func: &str, err: &std::io::Error) -> Diagnostic {
    let err_text = err.to_string();
    let message = format!("{func} input pipe read failed: {err_text}");
    Diagnostic::new(
        "ssh.pump.func_input_read_failed",
        message,
        Diagnostic::make_fields(&[(FIELD_FUNC, func), (FIELD_ERR, err_text.as_str())]),
    )
}

/// SSH pump output pipe write failed. Takes the concrete I/O
/// failure by value; bare outer message plus chained source so
/// `{err:#}` keeps the legacy `": "` join.
pub fn pump_output_write_failed(
    reason: impl std::error::Error + Send + Sync + 'static,
) -> Diagnostic {
    let reason_text = reason.to_string();
    Diagnostic::new(
        "ssh.pump.output_write_failed",
        "SSH pump output pipe write failed".to_string(),
        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
    )
    .with_source(std::sync::Arc::new(reason))
}

/// SSH pump output pipe flush failed. Takes the concrete I/O
/// failure by value; bare outer plus chained source.
pub fn pump_output_flush_failed(
    reason: impl std::error::Error + Send + Sync + 'static,
) -> Diagnostic {
    let reason_text = reason.to_string();
    Diagnostic::new(
        "ssh.pump.output_flush_failed",
        "SSH pump output pipe flush failed".to_string(),
        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
    )
    .with_source(std::sync::Arc::new(reason))
}

/// SSH pump cannot borrow the input pipe. Takes the underlying
/// typed failure by value; bare outer plus chained source so the
/// inner key survives traversal.
pub fn pump_borrow_input_failed(reason: Diagnostic) -> Diagnostic {
    let reason_text = reason.to_string();
    Diagnostic::new(
        "ssh.pump.borrow_input_failed",
        "SSH pump cannot borrow the input pipe".to_string(),
        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
    )
    .with_source(std::sync::Arc::new(reason))
}

/// SSH pump cannot borrow the output pipe. Takes the underlying
/// typed failure by value; bare outer plus chained source.
pub fn pump_borrow_output_failed(reason: Diagnostic) -> Diagnostic {
    let reason_text = reason.to_string();
    Diagnostic::new(
        "ssh.pump.borrow_output_failed",
        "SSH pump cannot borrow the output pipe".to_string(),
        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
    )
    .with_source(std::sync::Arc::new(reason))
}

/// SSH pump wait runtime failed to build. Takes the concrete
/// failure by value; bare outer plus chained source.
pub fn pump_wait_runtime_failed(
    reason: impl std::error::Error + Send + Sync + 'static,
) -> Diagnostic {
    let reason_text = reason.to_string();
    Diagnostic::new(
        "ssh.pump.wait_runtime_failed",
        "SSH pump wait runtime failed".to_string(),
        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
    )
    .with_source(std::sync::Arc::new(reason))
}

/// `SSH_PUMP` cannot borrow the source pipe. Takes the underlying
/// typed failure by value; bare outer plus chained source.
pub fn pump_borrow_source_failed(reason: Diagnostic) -> Diagnostic {
    let reason_text = reason.to_string();
    Diagnostic::new(
        "ssh.pump.borrow_source_failed",
        "SSH_PUMP cannot borrow the source pipe".to_string(),
        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
    )
    .with_source(std::sync::Arc::new(reason))
}

/// `SSH_PUMP` cannot borrow the target pipe. Takes the underlying
/// typed failure by value; bare outer plus chained source.
pub fn pump_borrow_target_failed(reason: Diagnostic) -> Diagnostic {
    let reason_text = reason.to_string();
    Diagnostic::new(
        "ssh.pump.borrow_target_failed",
        "SSH_PUMP cannot borrow the target pipe".to_string(),
        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
    )
    .with_source(std::sync::Arc::new(reason))
}

/// `SSH_PUMP` target pipe write failed. Takes the concrete failure
/// by value; bare outer plus chained source.
pub fn pump_target_write_failed(
    reason: impl std::error::Error + Send + Sync + 'static,
) -> Diagnostic {
    let reason_text = reason.to_string();
    Diagnostic::new(
        "ssh.pump.target_write_failed",
        "SSH_PUMP target pipe write failed".to_string(),
        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
    )
    .with_source(std::sync::Arc::new(reason))
}

/// `SSH_PUMP` target pipe flush failed. Takes the concrete failure
/// by value; bare outer plus chained source.
pub fn pump_target_flush_failed(
    reason: impl std::error::Error + Send + Sync + 'static,
) -> Diagnostic {
    let reason_text = reason.to_string();
    Diagnostic::new(
        "ssh.pump.target_flush_failed",
        "SSH_PUMP target pipe flush failed".to_string(),
        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
    )
    .with_source(std::sync::Arc::new(reason))
}

/// Authentication rejected. Takes the invoking func (`SSH_EXEC` /
/// `SSH_CONNECT`: same failure mode, one key); rendering lives
/// here.
pub fn authentication_rejected(func: &str) -> Diagnostic {
    let message = format!("{func} authentication rejected");
    Diagnostic::new(
        "ssh.auth.rejected",
        message,
        Diagnostic::make_fields(&[(FIELD_FUNC, func)]),
    )
}

/// Dial failed. Takes the invoking func (`SSH_EXEC` /
/// `SSH_CONNECT`: same failure mode, one key) plus the underlying
/// failure; the russh connect boundary surfaces `anyhow::Error`,
/// so the outer stays bare while the cause rides an I/O-chained
/// source. Call sites stay `format!`-free.
pub fn dial_failed(func: &str, reason: &Diagnostic) -> Diagnostic {
    let reason_text = flatten_chain(reason);
    Diagnostic::new(
        "ssh.dial_failed",
        format!("{func} dial failed"),
        Diagnostic::make_fields(&[(FIELD_FUNC, func), (FIELD_ERR, &reason_text)]),
    )
    .with_source(std::sync::Arc::new(std::io::Error::other(reason_text)))
}

/// Authentication exchange failed. Takes the invoking func
/// (`SSH_EXEC` / `SSH_CONNECT`: same failure mode, one key) plus
/// the concrete failure by value; bare outer plus chained source.
pub fn auth_exchange_failed(
    func: &str,
    reason: impl std::error::Error + Send + Sync + 'static,
) -> Diagnostic {
    let reason_text = reason.to_string();
    Diagnostic::new(
        "ssh.auth.exchange_failed",
        format!("{func} authentication exchange failed"),
        Diagnostic::make_fields(&[(FIELD_FUNC, func), (FIELD_ERR, &reason_text)]),
    )
    .with_source(std::sync::Arc::new(reason))
}

/// Channel open failed. Takes the invoking func (`SSH_EXEC` /
/// `SSH_CONNECT`: same failure mode, one key) plus the concrete
/// failure by value; bare outer plus chained source.
pub fn channel_open_failed(
    func: &str,
    reason: impl std::error::Error + Send + Sync + 'static,
) -> Diagnostic {
    let reason_text = reason.to_string();
    Diagnostic::new(
        "ssh.channel.open_failed",
        format!("{func} channel open failed"),
        Diagnostic::make_fields(&[(FIELD_FUNC, func), (FIELD_ERR, &reason_text)]),
    )
    .with_source(std::sync::Arc::new(reason))
}

/// `SSH_EXEC` exec request failed. Takes the concrete failure by
/// value; bare outer plus chained source.
pub fn exec_request_failed(reason: impl std::error::Error + Send + Sync + 'static) -> Diagnostic {
    let reason_text = reason.to_string();
    Diagnostic::new(
        "ssh.exec.request_failed",
        "SSH_EXEC exec request failed".to_string(),
        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
    )
    .with_source(std::sync::Arc::new(reason))
}

/// `SSH_CONNECT` shell request failed. Takes the concrete failure
/// by value; bare outer plus chained source.
pub fn shell_request_failed(reason: impl std::error::Error + Send + Sync + 'static) -> Diagnostic {
    let reason_text = reason.to_string();
    Diagnostic::new(
        "ssh.connect.shell_request_failed",
        "SSH_CONNECT shell request failed".to_string(),
        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
    )
    .with_source(std::sync::Arc::new(reason))
}

/// Listener socket cannot go non-blocking for the runtime. Takes
/// the concrete failure by value; bare outer plus chained source.
/// The legacy wording stays byte-identical (no func prefix).
pub fn listener_prepare_failed(
    reason: impl std::error::Error + Send + Sync + 'static,
) -> Diagnostic {
    let reason_text = reason.to_string();
    Diagnostic::new(
        "ssh.serve.listener_prepare_failed",
        "prepare listener for the runtime".to_string(),
        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
    )
    .with_source(std::sync::Arc::new(reason))
}

/// Listener socket cannot hand to the runtime. Takes the concrete
/// failure by value; bare outer plus chained source.
pub fn listener_handoff_failed(
    reason: impl std::error::Error + Send + Sync + 'static,
) -> Diagnostic {
    let reason_text = reason.to_string();
    Diagnostic::new(
        "ssh.serve.listener_handoff_failed",
        "hand listener to the runtime".to_string(),
        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
    )
    .with_source(std::sync::Arc::new(reason))
}

/// SSH listener failed. Takes the concrete failure by value; bare
/// outer plus chained source.
pub fn listener_failed(reason: impl std::error::Error + Send + Sync + 'static) -> Diagnostic {
    let reason_text = reason.to_string();
    Diagnostic::new(
        "ssh.serve.listener_failed",
        "SSH listener failed".to_string(),
        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
    )
    .with_source(std::sync::Arc::new(reason))
}

/// SSH client runtime failed to build. Takes the concrete failure
/// by value; bare outer plus chained source.
pub fn client_runtime_build_failed(
    reason: impl std::error::Error + Send + Sync + 'static,
) -> Diagnostic {
    let reason_text = reason.to_string();
    Diagnostic::new(
        "ssh.client_runtime.build_failed",
        "build SSH client runtime".to_string(),
        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
    )
    .with_source(std::sync::Arc::new(reason))
}

/// Session pump ends already taken by `SSH_SEND_EXIT_STATUS`.
/// Parameterless: the wording is fixed.
pub fn session_pump_already_finished() -> Diagnostic {
    Diagnostic::new(
        "ssh.session.pump_already_finished",
        "SSH session pump already finished".to_string(),
        Diagnostic::make_fields(&[]),
    )
}

/// Exit-status queue saturated under backpressure. Parameterless:
/// the wording is fixed.
pub fn session_wire_queue_saturated() -> Diagnostic {
    Diagnostic::new(
        "ssh.session.wire_queue_saturated",
        "SSH session wire queue saturated under backpressure".to_string(),
        Diagnostic::make_fields(&[]),
    )
}

/// Exit-status queue receiver gone. Parameterless: the wording is
/// fixed.
pub fn session_wire_writer_gone() -> Diagnostic {
    Diagnostic::new(
        "ssh.session.wire_writer_gone",
        "SSH session wire writer is gone".to_string(),
        Diagnostic::make_fields(&[]),
    )
}

/// Second pump on the same session. Parameterless: the wording is
/// fixed.
pub fn session_already_pumped() -> Diagnostic {
    Diagnostic::new(
        "ssh.session.already_pumped",
        "SSH session already pumped".to_string(),
        Diagnostic::make_fields(&[]),
    )
}

/// Wire data for an unknown channel. Parameterless: the wording is
/// fixed.
pub fn channel_data_unknown() -> Diagnostic {
    Diagnostic::new(
        "ssh.channel.data_unknown",
        "data for unknown channel".to_string(),
        Diagnostic::make_fields(&[]),
    )
}

/// Pump side vanished mid-send. Parameterless: the wording is fixed.
pub fn channel_pump_gone() -> Diagnostic {
    Diagnostic::new(
        "ssh.channel.pump_gone",
        "pump went away".to_string(),
        Diagnostic::make_fields(&[]),
    )
}

/// Wire side vanished during EOF close. Parameterless: the wording
/// is fixed.
pub fn channel_wire_gone_eof_close() -> Diagnostic {
    Diagnostic::new(
        "ssh.channel.wire_gone_eof_close",
        "wire gone during EOF close".to_string(),
        Diagnostic::make_fields(&[]),
    )
}

/// Wire request for an unknown channel. Parameterless: the wording
/// is fixed.
pub fn channel_request_unknown() -> Diagnostic {
    Diagnostic::new(
        "ssh.channel.request_unknown",
        "request for unknown channel".to_string(),
        Diagnostic::make_fields(&[]),
    )
}

/// Channel announced twice. Parameterless: the wording is fixed.
pub fn channel_already_announced() -> Diagnostic {
    Diagnostic::new(
        "ssh.channel.already_announced",
        "channel already announced".to_string(),
        Diagnostic::make_fields(&[]),
    )
}

/// Child stream read failed. Takes the stream name plus the I/O
/// failure; rendering lives here.
pub fn run_child_read_failed(name: &str, err: &std::io::Error) -> Diagnostic {
    let err_text = err.to_string();
    let message = format!("SSH run child {name} read failed: {err_text}");
    Diagnostic::new(
        "ssh.run.child_read_failed",
        message,
        Diagnostic::make_fields(&[(FIELD_NAME, name), (FIELD_ERR, err_text.as_str())]),
    )
}

/// SSH run output pipe write failed. Takes the concrete failure by
/// value; bare outer plus chained source.
pub fn run_output_write_failed(
    reason: impl std::error::Error + Send + Sync + 'static,
) -> Diagnostic {
    let reason_text = reason.to_string();
    Diagnostic::new(
        "ssh.run.output_write_failed",
        "SSH run output pipe write failed".to_string(),
        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
    )
    .with_source(std::sync::Arc::new(reason))
}

/// SSH run output pipe flush failed. Takes the concrete failure by
/// value; bare outer plus chained source.
pub fn run_output_flush_failed(
    reason: impl std::error::Error + Send + Sync + 'static,
) -> Diagnostic {
    let reason_text = reason.to_string();
    Diagnostic::new(
        "ssh.run.output_flush_failed",
        "SSH run output pipe flush failed".to_string(),
        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
    )
    .with_source(std::sync::Arc::new(reason))
}

/// SSH run output lock poisoned. Parameterless: the wording is
/// fixed.
pub fn run_output_lock_poisoned() -> Diagnostic {
    Diagnostic::new(
        "ssh.run.output_lock_poisoned",
        "SSH run output lock poisoned".to_string(),
        Diagnostic::make_fields(&[]),
    )
}

/// `SSH_RUN` with no argv. Parameterless: the wording is fixed.
pub fn run_needs_program() -> Diagnostic {
    Diagnostic::new(
        "ssh.run.needs_program",
        "SSH_RUN needs at least a program".to_string(),
        Diagnostic::make_fields(&[]),
    )
}

/// Spawned child lost its stderr handle. Parameterless: the wording
/// is fixed.
pub fn run_spawn_lost_stderr() -> Diagnostic {
    Diagnostic::new(
        "ssh.run.spawn_lost_stderr",
        "SSH run spawn lost stderr".to_string(),
        Diagnostic::make_fields(&[]),
    )
}

/// SSH run interrupted by cancellation. Parameterless: the wording
/// is fixed.
pub fn run_interrupted_by_cancellation() -> Diagnostic {
    Diagnostic::new(
        "ssh.run.interrupted_by_cancellation",
        "SSH run interrupted by cancellation".to_string(),
        Diagnostic::make_fields(&[]),
    )
}

/// SSH run cannot borrow the input pipe. Takes the underlying
/// typed failure by value; bare outer plus chained source.
pub fn run_borrow_input_failed(reason: Diagnostic) -> Diagnostic {
    let reason_text = reason.to_string();
    Diagnostic::new(
        "ssh.run.borrow_input_failed",
        "SSH run cannot borrow the input pipe".to_string(),
        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
    )
    .with_source(std::sync::Arc::new(reason))
}

/// SSH run cannot borrow the output pipe. Takes the underlying
/// typed failure by value; bare outer plus chained source.
pub fn run_borrow_output_failed(reason: Diagnostic) -> Diagnostic {
    let reason_text = reason.to_string();
    Diagnostic::new(
        "ssh.run.borrow_output_failed",
        "SSH run cannot borrow the output pipe".to_string(),
        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
    )
    .with_source(std::sync::Arc::new(reason))
}

/// SSH run cannot borrow the error pipe. Takes the underlying
/// typed failure by value; bare outer plus chained source.
pub fn run_borrow_error_failed(reason: Diagnostic) -> Diagnostic {
    let reason_text = reason.to_string();
    Diagnostic::new(
        "ssh.run.borrow_error_failed",
        "SSH run cannot borrow the error pipe".to_string(),
        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
    )
    .with_source(std::sync::Arc::new(reason))
}

/// SSH run spawn failed. Takes the underlying typed failure by
/// value; bare outer plus chained source.
pub fn run_spawn_failed(reason: Diagnostic) -> Diagnostic {
    let reason_text = reason.to_string();
    Diagnostic::new(
        "ssh.run.spawn_failed",
        "SSH run spawn failed".to_string(),
        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
    )
    .with_source(std::sync::Arc::new(reason))
}

/// SSH pty output lock poisoned. Parameterless: the wording is
/// fixed.
pub fn pty_output_lock_poisoned() -> Diagnostic {
    Diagnostic::new(
        "ssh.pty.output_lock_poisoned",
        "SSH pty output lock poisoned".to_string(),
        Diagnostic::make_fields(&[]),
    )
}

/// SSH pty run interrupted by cancellation. Parameterless: the
/// wording is fixed.
pub fn pty_interrupted_by_cancellation() -> Diagnostic {
    Diagnostic::new(
        "ssh.pty.interrupted_by_cancellation",
        "SSH pty run interrupted by cancellation".to_string(),
        Diagnostic::make_fields(&[]),
    )
}

/// SSH pty cannot borrow the input pipe. Takes the underlying
/// typed failure by value; bare outer plus chained source.
pub fn pty_borrow_input_failed(reason: Diagnostic) -> Diagnostic {
    let reason_text = reason.to_string();
    Diagnostic::new(
        "ssh.pty.borrow_input_failed",
        "SSH pty cannot borrow the input pipe".to_string(),
        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
    )
    .with_source(std::sync::Arc::new(reason))
}

/// SSH pty cannot borrow the output pipe. Takes the underlying
/// typed failure by value; bare outer plus chained source.
pub fn pty_borrow_output_failed(reason: Diagnostic) -> Diagnostic {
    let reason_text = reason.to_string();
    Diagnostic::new(
        "ssh.pty.borrow_output_failed",
        "SSH pty cannot borrow the output pipe".to_string(),
        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
    )
    .with_source(std::sync::Arc::new(reason))
}

/// SSH pty session init write failed. Takes the concrete failure
/// by value; bare outer plus chained source.
pub fn pty_session_init_write_failed(
    reason: impl std::error::Error + Send + Sync + 'static,
) -> Diagnostic {
    let reason_text = reason.to_string();
    Diagnostic::new(
        "ssh.pty.session_init_write_failed",
        "SSH pty session init write failed".to_string(),
        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
    )
    .with_source(std::sync::Arc::new(reason))
}

/// SSH pty session init flush failed. Takes the concrete failure
/// by value; bare outer plus chained source.
pub fn pty_session_init_flush_failed(
    reason: impl std::error::Error + Send + Sync + 'static,
) -> Diagnostic {
    let reason_text = reason.to_string();
    Diagnostic::new(
        "ssh.pty.session_init_flush_failed",
        "SSH pty session init flush failed".to_string(),
        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
    )
    .with_source(std::sync::Arc::new(reason))
}

/// Dequeue wait cancelled. Takes the invoking func (`SSH_DEQUEUE` /
/// `SSH_ACCEPT`: same failure mode, one key); rendering lives here.
pub fn dequeue_task_cancelled(func: &str) -> Diagnostic {
    let message = format!("{func}: task cancelled");
    Diagnostic::new(
        "ssh.dequeue.task_cancelled",
        message,
        Diagnostic::make_fields(&[(FIELD_FUNC, func)]),
    )
}

/// Queue shut down while waiting. Takes the invoking func (same
/// failure mode, one key); rendering lives here.
pub fn dequeue_server_closed(func: &str) -> Diagnostic {
    let message = format!("{func}: server is closed");
    Diagnostic::new(
        "ssh.dequeue.server_closed",
        message,
        Diagnostic::make_fields(&[(FIELD_FUNC, func)]),
    )
}

/// `SSH_DEQUEUE` outside `ASYNC`. Parameterless: the wrap hint lives
/// here.
pub fn dequeue_requires_async() -> Diagnostic {
    Diagnostic::new(
        "ssh.dequeue.requires_async",
        format!(
            "SSH_DEQUEUE requires {KEYWORD_ASYNC}: wrap it as {KEYWORD_LET} $t: {HANDLE_TYPE_NAME}<T> = {KEYWORD_ASYNC} {{ SSH_DEQUEUE($server.server) }}"
        ),
        Diagnostic::make_fields(&[]),
    )
}

/// `SSH_ACCEPT` outside `ASYNC`. Parameterless: the wrap hint lives
/// here.
pub fn accept_requires_async() -> Diagnostic {
    Diagnostic::new(
        "ssh.accept.requires_async",
        format!(
            "SSH_ACCEPT requires {KEYWORD_ASYNC}: wrap it as {KEYWORD_LET} $t: {HANDLE_TYPE_NAME}<T> = {KEYWORD_ASYNC} {{ SSH_ACCEPT($server, $in, $out) }}"
        ),
        Diagnostic::make_fields(&[]),
    )
}

/// `SSH_PUMP_CHANNEL` outside `ASYNC`. Parameterless: the wording is
/// fixed.
pub fn pump_channel_requires_async() -> Diagnostic {
    Diagnostic::new(
        "ssh.pump_channel.requires_async",
        format!(
            "SSH_PUMP_CHANNEL requires {KEYWORD_ASYNC}: pump it in its own task after SSH_DEQUEUE"
        ),
        Diagnostic::make_fields(&[]),
    )
}

/// `SSH_CONNECT` outside `ASYNC`. Parameterless: the wording is
/// fixed.
pub fn connect_requires_async() -> Diagnostic {
    Diagnostic::new(
        "ssh.connect.requires_async",
        format!(
            "SSH_CONNECT requires {KEYWORD_ASYNC}: run it in its own task beside the SSH_PUMP tasks"
        ),
        Diagnostic::make_fields(&[]),
    )
}

/// `SSH_CONNECT` while offline. Parameterless: the wording is fixed.
pub fn connect_offline() -> Diagnostic {
    Diagnostic::new(
        "ssh.connect.offline",
        "SSH_CONNECT failed: engine running in --offline mode".to_string(),
        Diagnostic::make_fields(&[]),
    )
}

/// `SSH_CONNECT` with an empty username. Parameterless: the wording
/// is fixed.
pub fn connect_username_empty() -> Diagnostic {
    Diagnostic::new(
        "ssh.connect.username_empty",
        "SSH_CONNECT username must not be empty".to_string(),
        Diagnostic::make_fields(&[]),
    )
}

/// `SSH_CONNECT` session leg failed. Takes the leg's typed failure
/// by value; bare outer plus chained source.
pub fn connect_failed(reason: Diagnostic) -> Diagnostic {
    let reason_text = reason.to_string();
    Diagnostic::new(
        "ssh.connect.failed",
        "SSH_CONNECT failed".to_string(),
        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
    )
    .with_source(std::sync::Arc::new(reason))
}

/// `SSH_PTY_RUN` outside `ASYNC`. Parameterless: the wording is
/// fixed.
pub fn pty_run_requires_async() -> Diagnostic {
    Diagnostic::new(
        "ssh.pty_run.requires_async",
        format!(
            "SSH_PTY_RUN requires {KEYWORD_ASYNC}: run it in its own task beside the session pump task"
        ),
        Diagnostic::make_fields(&[]),
    )
}

/// `SSH_RUN` outside `ASYNC`. Parameterless: the wording is fixed.
pub fn run_requires_async() -> Diagnostic {
    Diagnostic::new(
        "ssh.run.requires_async",
        format!(
            "SSH_RUN requires {KEYWORD_ASYNC}: run it in its own task beside the session pump task"
        ),
        Diagnostic::make_fields(&[]),
    )
}

/// `SSH_EXEC` with no argv. Parameterless: the wording is fixed.
pub fn exec_needs_program() -> Diagnostic {
    Diagnostic::new(
        "ssh.exec.needs_program",
        "SSH_EXEC needs at least a program".to_string(),
        Diagnostic::make_fields(&[]),
    )
}

/// `SSH_EXEC` with an empty username. Parameterless: the wording is
/// fixed.
pub fn exec_username_empty() -> Diagnostic {
    Diagnostic::new(
        "ssh.exec.username_empty",
        "SSH_EXEC username must not be empty".to_string(),
        Diagnostic::make_fields(&[]),
    )
}

/// `SSH_EXEC` while offline. Parameterless: the wording is fixed.
pub fn exec_offline() -> Diagnostic {
    Diagnostic::new(
        "ssh.exec.offline",
        "SSH_EXEC failed: engine running in --offline mode".to_string(),
        Diagnostic::make_fields(&[]),
    )
}

/// `SSH_SERVE` option empty. Takes the option name (callers pass the
/// `FIELD_*` / `OPT_*` constants); quoting lives here. Shared by
/// every `SSH_SERVE` string option: one key, one mode.
pub fn serve_option_empty(option: &str) -> Diagnostic {
    let message = format!("SSH_SERVE option '{option}' must not be empty");
    Diagnostic::new(
        "ssh.serve.option_empty",
        message,
        Diagnostic::make_fields(&[(FIELD_OPTION, option)]),
    )
}

/// `SSH_SERVE` option missing. Takes the option name; quoting lives
/// here. Shared by every required `SSH_SERVE` option: one key, one
/// mode.
pub fn serve_option_required(option: &str) -> Diagnostic {
    let message = format!("SSH_SERVE option '{option}' is required");
    Diagnostic::new(
        "ssh.serve.option_required",
        message,
        Diagnostic::make_fields(&[(FIELD_OPTION, option)]),
    )
}

/// Ephemeral Ed25519 host key generation failed. Takes the concrete
/// failure by value; bare outer plus chained source. The legacy
/// wording stays byte-identical.
pub fn ephemeral_key_generate_failed(
    reason: impl std::error::Error + Send + Sync + 'static,
) -> Diagnostic {
    let reason_text = reason.to_string();
    Diagnostic::new(
        "ssh.serve.ephemeral_key_generate_failed",
        "generate ephemeral Ed25519 host key".to_string(),
        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
    )
    .with_source(std::sync::Arc::new(reason))
}

/// `SSH_SERVE` cannot clone its listener. Takes the concrete
/// failure by value; bare outer plus chained source.
pub fn serve_listener_clone_failed(
    reason: impl std::error::Error + Send + Sync + 'static,
) -> Diagnostic {
    let reason_text = reason.to_string();
    Diagnostic::new(
        "ssh.serve.listener_clone_failed",
        "SSH_SERVE cannot clone its listener".to_string(),
        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
    )
    .with_source(std::sync::Arc::new(reason))
}

/// `SSH_SERVE` cannot spawn the server thread. Takes the concrete
/// failure by value; bare outer plus chained source.
pub fn serve_spawn_failed(reason: impl std::error::Error + Send + Sync + 'static) -> Diagnostic {
    let reason_text = reason.to_string();
    Diagnostic::new(
        "ssh.serve.spawn_failed",
        "SSH_SERVE cannot spawn the server thread".to_string(),
        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
    )
    .with_source(std::sync::Arc::new(reason))
}

/// `SSH_EXEC` numeric option not positive. Takes the option name
/// (callers pass the `OPT_*` constants); quoting lives here.
/// Shared by `timeout` / `max_bytes`: one key, one mode.
pub fn exec_option_must_be_positive(option: &str) -> Diagnostic {
    let message = format!("SSH_EXEC option '{option}' must be positive");
    Diagnostic::new(
        "ssh.exec.option_must_be_positive",
        message,
        Diagnostic::make_fields(&[(FIELD_OPTION, option)]),
    )
}

/// Duration option holds a non-duration string. Takes the invoking
/// func (`SSH_SERVE` / `SSH_EXEC`: same failure mode, one key),
/// the option name, the offending text, and the parse failure by
/// value; quoting lives here. Bare outer plus chained source.
pub fn option_must_be_duration(
    func: &str,
    option: &str,
    got: &str,
    reason: Diagnostic,
) -> Diagnostic {
    let reason_text = reason.to_string();
    let message = format!("{func} option '{option}' must be a duration, got {got:?}");
    Diagnostic::new(
        "ssh.option.must_be_duration",
        message,
        Diagnostic::make_fields(&[
            (FIELD_FUNC, func),
            (FIELD_OPTION, option),
            (FIELD_GOT, got),
            (FIELD_ERR, &reason_text),
        ]),
    )
    .with_source(std::sync::Arc::new(reason))
}

/// `SSH_EXEC` remote leg failed. Takes the leg's typed failure by
/// value; bare outer plus chained source.
pub fn exec_failed(reason: Diagnostic) -> Diagnostic {
    let reason_text = reason.to_string();
    Diagnostic::new(
        "ssh.exec.failed",
        "SSH_EXEC failed".to_string(),
        Diagnostic::make_fields(&[(FIELD_ERR, &reason_text)]),
    )
    .with_source(std::sync::Arc::new(reason))
}

/// `SSH_SEND_EXIT_STATUS` with a negative code. Takes the code;
/// rendering lives here.
pub fn send_exit_status_negative(code: i64) -> Diagnostic {
    let message = format!("SSH_SEND_EXIT_STATUS code must not be negative: {code}");
    Diagnostic::new(
        "ssh.send_exit_status.negative",
        message,
        Diagnostic::make_fields(&[(FIELD_CODE, &code.to_string())]),
    )
}

/// `SSH_SEND_EXIT_STATUS` code outside `u32`. Takes the code;
/// rendering lives here.
pub fn send_exit_status_out_of_range(code: i64) -> Diagnostic {
    let message = format!("SSH_SEND_EXIT_STATUS code out of range: {code}");
    Diagnostic::new(
        "ssh.send_exit_status.out_of_range",
        message,
        Diagnostic::make_fields(&[(FIELD_CODE, &code.to_string())]),
    )
}

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

    fn io_err(msg: &str) -> std::io::Error {
        std::io::Error::other(msg.to_string())
    }

    fn reason_sample(msg: &str) -> Diagnostic {
        Diagnostic::new(
            "test.reason.sample",
            msg.to_string(),
            Diagnostic::make_fields(&[]),
        )
    }

    #[test]
    fn each_failure_has_a_distinct_key() {
        let err = io_err("boom");
        let keys = [
            key_path_escapes("SSH_SERVE", "k").key(),
            host_key_resolver_failed("SSH_SERVE", &reason_sample("boom")).key(),
            host_key_generate_failed(io_err("boom")).key(),
            host_key_encode_failed(io_err("boom")).key(),
            key_guard_failed("SSH_SERVE", &reason_sample("boom")).key(),
            key_path_join_failed("SSH_SERVE", "k", &reason_sample("boom")).key(),
            host_key_stat_failed("p", &reason_sample("boom")).key(),
            host_key_read_failed("SSH_SERVE", "p", &reason_sample("boom")).key(),
            host_key_parse_failed("SSH_SERVE", "p", io_err("boom")).key(),
            host_key_write_failed(&reason_sample("boom")).key(),
            host_key_must_be_ed25519("SSH_SERVE", "p", &"Ed25519").key(),
            host_key_insecure_permissions("SSH_SERVE", "p", 0o644).key(),
            pump_output_lock_poisoned().key(),
            pump_input_read_failed(&err).key(),
            pump_worker_panicked("input worker").key(),
            pump_interrupted_by_cancellation().key(),
            pump_source_read_failed(&err).key(),
            pump_target_lock_poisoned().key(),
            pump_func_input_read_failed("SSH run", &err).key(),
            pump_output_write_failed(io_err("boom")).key(),
            pump_output_flush_failed(io_err("boom")).key(),
            pump_borrow_input_failed(pump_output_lock_poisoned()).key(),
            pump_borrow_output_failed(pump_output_lock_poisoned()).key(),
            pump_wait_runtime_failed(io_err("boom")).key(),
            pump_borrow_source_failed(pump_output_lock_poisoned()).key(),
            pump_borrow_target_failed(pump_output_lock_poisoned()).key(),
            pump_target_write_failed(io_err("boom")).key(),
            pump_target_flush_failed(io_err("boom")).key(),
            authentication_rejected("SSH_EXEC").key(),
            dial_failed("SSH_EXEC", &reason_sample("boom")).key(),
            auth_exchange_failed("SSH_EXEC", io_err("boom")).key(),
            channel_open_failed("SSH_EXEC", io_err("boom")).key(),
            exec_request_failed(io_err("boom")).key(),
            shell_request_failed(io_err("boom")).key(),
            listener_prepare_failed(io_err("boom")).key(),
            listener_handoff_failed(io_err("boom")).key(),
            listener_failed(io_err("boom")).key(),
            client_runtime_build_failed(io_err("boom")).key(),
            session_pump_already_finished().key(),
            session_wire_queue_saturated().key(),
            session_wire_writer_gone().key(),
            session_already_pumped().key(),
            channel_data_unknown().key(),
            channel_pump_gone().key(),
            channel_wire_gone_eof_close().key(),
            channel_request_unknown().key(),
            channel_already_announced().key(),
            run_child_read_failed("stdout", &err).key(),
            run_output_write_failed(io_err("boom")).key(),
            run_output_flush_failed(io_err("boom")).key(),
            run_output_lock_poisoned().key(),
            run_needs_program().key(),
            run_spawn_lost_stderr().key(),
            run_interrupted_by_cancellation().key(),
            run_borrow_input_failed(pump_output_lock_poisoned()).key(),
            run_borrow_output_failed(pump_output_lock_poisoned()).key(),
            run_borrow_error_failed(pump_output_lock_poisoned()).key(),
            run_spawn_failed(pump_output_lock_poisoned()).key(),
            pty_output_lock_poisoned().key(),
            pty_interrupted_by_cancellation().key(),
            pty_borrow_input_failed(pump_output_lock_poisoned()).key(),
            pty_borrow_output_failed(pump_output_lock_poisoned()).key(),
            pty_session_init_write_failed(io_err("boom")).key(),
            pty_session_init_flush_failed(io_err("boom")).key(),
            dequeue_task_cancelled("SSH_DEQUEUE").key(),
            dequeue_server_closed("SSH_DEQUEUE").key(),
            dequeue_requires_async().key(),
            accept_requires_async().key(),
            pump_channel_requires_async().key(),
            connect_requires_async().key(),
            connect_offline().key(),
            connect_username_empty().key(),
            connect_failed(pump_output_lock_poisoned()).key(),
            pty_run_requires_async().key(),
            run_requires_async().key(),
            exec_needs_program().key(),
            exec_username_empty().key(),
            exec_offline().key(),
            exec_failed(pump_output_lock_poisoned()).key(),
            serve_option_empty("username").key(),
            serve_option_required("username").key(),
            option_must_be_duration(
                "SSH_SERVE",
                "idle_timeout",
                "x",
                pump_output_lock_poisoned(),
            )
            .key(),
            ephemeral_key_generate_failed(io_err("boom")).key(),
            serve_listener_clone_failed(io_err("boom")).key(),
            serve_spawn_failed(io_err("boom")).key(),
            exec_option_must_be_positive("timeout").key(),
            send_exit_status_negative(-1).key(),
            send_exit_status_out_of_range(1 << 40).key(),
        ];
        let mut seen = std::collections::HashSet::new();
        for k in keys {
            assert!(seen.insert(k), "duplicate key {k}");
        }
    }

    #[test]
    fn shared_modes_keep_one_key() {
        assert_eq!(
            authentication_rejected("SSH_EXEC").key(),
            authentication_rejected("SSH_CONNECT").key()
        );
        assert_eq!(
            dial_failed("SSH_EXEC", &reason_sample("boom")).key(),
            dial_failed("SSH_CONNECT", &reason_sample("boom")).key()
        );
        assert_eq!(
            auth_exchange_failed("SSH_EXEC", io_err("boom")).key(),
            auth_exchange_failed("SSH_CONNECT", io_err("boom")).key()
        );
        assert_eq!(
            channel_open_failed("SSH_EXEC", io_err("boom")).key(),
            channel_open_failed("SSH_CONNECT", io_err("boom")).key()
        );
        assert_eq!(
            dequeue_task_cancelled("SSH_DEQUEUE").key(),
            dequeue_task_cancelled("SSH_ACCEPT").key()
        );
        assert_eq!(
            dequeue_server_closed("SSH_DEQUEUE").key(),
            dequeue_server_closed("SSH_ACCEPT").key()
        );
        assert_eq!(
            serve_option_empty("username").key(),
            serve_option_empty("password").key()
        );
        assert_eq!(
            serve_option_required("username").key(),
            serve_option_required("password").key()
        );
        assert_eq!(
            option_must_be_duration(
                "SSH_SERVE",
                "idle_timeout",
                "x",
                pump_output_lock_poisoned(),
            )
            .key(),
            option_must_be_duration("SSH_EXEC", "timeout", "y", pump_output_lock_poisoned()).key()
        );
        assert_eq!(
            exec_option_must_be_positive("timeout").key(),
            exec_option_must_be_positive("max_bytes").key()
        );
    }

    #[test]
    fn messages_match_canonical_wording() {
        let err = io_err("boom");
        assert_eq!(
            key_path_escapes("SSH_SERVE", "k").message(),
            "SSH_SERVE key_path escapes the workspace: k"
        );
        assert_eq!(
            host_key_resolver_failed("SSH_SERVE", &reason_sample("boom")).message(),
            "SSH_SERVE cannot open the workspace resolver"
        );
        assert_eq!(
            format!(
                "{:#}",
                anyhow::Error::new(host_key_resolver_failed(
                    "SSH_SERVE",
                    &reason_sample("boom")
                ))
            ),
            "SSH_SERVE cannot open the workspace resolver: boom"
        );
        assert_eq!(
            host_key_generate_failed(io_err("boom")).message(),
            "generate Ed25519 host key"
        );
        assert_eq!(
            format!(
                "{:#}",
                anyhow::Error::new(host_key_generate_failed(io_err("boom")))
            ),
            "generate Ed25519 host key: boom"
        );
        assert_eq!(
            host_key_encode_failed(io_err("boom")).message(),
            "encode Ed25519 host key"
        );
        assert_eq!(
            format!(
                "{:#}",
                anyhow::Error::new(host_key_encode_failed(io_err("boom")))
            ),
            "encode Ed25519 host key: boom"
        );
        assert_eq!(
            key_guard_failed("SSH_SERVE", &reason_sample("boom")).message(),
            "SSH_SERVE cannot guard the workspace root"
        );
        assert_eq!(
            format!(
                "{:#}",
                anyhow::Error::new(key_guard_failed("SSH_SERVE", &reason_sample("boom")))
            ),
            "SSH_SERVE cannot guard the workspace root: boom"
        );
        assert_eq!(
            key_path_join_failed("SSH_SERVE", "k", &reason_sample("boom")).message(),
            "SSH_SERVE key_path escapes the workspace: k"
        );
        assert_eq!(
            format!(
                "{:#}",
                anyhow::Error::new(key_path_join_failed(
                    "SSH_SERVE",
                    "k",
                    &reason_sample("boom")
                ))
            ),
            "SSH_SERVE key_path escapes the workspace: k: boom"
        );
        assert_eq!(
            host_key_stat_failed("p", &reason_sample("boom")).message(),
            "cannot stat host key p"
        );
        assert_eq!(
            format!(
                "{:#}",
                anyhow::Error::new(host_key_stat_failed("p", &reason_sample("boom")))
            ),
            "cannot stat host key p: boom"
        );
        assert_eq!(
            host_key_read_failed("SSH_SERVE", "p", &reason_sample("boom")).message(),
            "SSH_SERVE cannot read host key p"
        );
        assert_eq!(
            format!(
                "{:#}",
                anyhow::Error::new(host_key_read_failed(
                    "SSH_SERVE",
                    "p",
                    &reason_sample("boom")
                ))
            ),
            "SSH_SERVE cannot read host key p: boom"
        );
        assert_eq!(
            host_key_parse_failed("SSH_SERVE", "p", io_err("boom")).message(),
            "SSH_SERVE cannot parse host key p"
        );
        assert_eq!(
            format!(
                "{:#}",
                anyhow::Error::new(host_key_parse_failed("SSH_SERVE", "p", io_err("boom")))
            ),
            "SSH_SERVE cannot parse host key p: boom"
        );
        assert_eq!(
            host_key_write_failed(&reason_sample("boom")).message(),
            "boom"
        );
        assert_eq!(host_key_write_failed_fallback("boom").message(), "boom");
        assert_eq!(from_anyhow(anyhow::anyhow!("boom")).message(), "boom");
        assert_eq!(
            from_anyhow(anyhow::anyhow!("boom")).key(),
            "ssh.key.write_failed"
        );
        let framed_keyed = pump_output_lock_poisoned();
        let framed_key = framed_keyed.key();
        let wrapped = anyhow::Error::new(framed_keyed).context("outer frame");
        let reclaimed_framed = from_anyhow(wrapped);
        assert_eq!(reclaimed_framed.key(), framed_key);
        assert_eq!(reclaimed_framed.contexts(), &["outer frame".to_string()]);
        assert_eq!(
            host_key_must_be_ed25519("SSH_SERVE", "p", &"Ed25519").message(),
            "SSH_SERVE host key p must be Ed25519, found \"Ed25519\""
        );
        assert_eq!(
            host_key_insecure_permissions("SSH_SERVE", "p", 0o644).message(),
            "SSH_SERVE insecure permissions on host key file p: expected 0600, got 0644"
        );
        assert_eq!(
            pump_output_lock_poisoned().message(),
            "SSH pump output lock poisoned"
        );
        assert_eq!(
            pump_input_read_failed(&err).message(),
            "SSH pump input pipe read failed: boom"
        );
        assert_eq!(
            pump_worker_panicked("input worker").message(),
            "SSH pump input worker panicked"
        );
        assert_eq!(
            pump_interrupted_by_cancellation().message(),
            "SSH pump interrupted by cancellation"
        );
        assert_eq!(
            pump_source_read_failed(&err).message(),
            "SSH_PUMP source pipe read failed: boom"
        );
        assert_eq!(
            pump_target_lock_poisoned().message(),
            "SSH_PUMP target lock poisoned"
        );
        assert_eq!(
            pump_func_input_read_failed("SSH run", &err).message(),
            "SSH run input pipe read failed: boom"
        );
        assert_eq!(
            pump_output_write_failed(io_err("boom")).message(),
            "SSH pump output pipe write failed"
        );
        assert_eq!(
            format!(
                "{:#}",
                anyhow::Error::new(pump_output_write_failed(io_err("boom")))
            ),
            "SSH pump output pipe write failed: boom"
        );
        assert_eq!(
            pump_output_flush_failed(io_err("boom")).message(),
            "SSH pump output pipe flush failed"
        );
        assert_eq!(
            format!(
                "{:#}",
                anyhow::Error::new(pump_output_flush_failed(io_err("boom")))
            ),
            "SSH pump output pipe flush failed: boom"
        );
        assert_eq!(
            pump_borrow_input_failed(pump_output_lock_poisoned()).message(),
            "SSH pump cannot borrow the input pipe"
        );
        assert_eq!(
            format!(
                "{:#}",
                anyhow::Error::new(pump_borrow_input_failed(pump_output_lock_poisoned()))
            ),
            "SSH pump cannot borrow the input pipe: SSH pump output lock poisoned"
        );
        assert_eq!(
            pump_borrow_output_failed(pump_output_lock_poisoned()).message(),
            "SSH pump cannot borrow the output pipe"
        );
        assert_eq!(
            format!(
                "{:#}",
                anyhow::Error::new(pump_borrow_output_failed(pump_output_lock_poisoned()))
            ),
            "SSH pump cannot borrow the output pipe: SSH pump output lock poisoned"
        );
        assert_eq!(
            pump_wait_runtime_failed(io_err("boom")).message(),
            "SSH pump wait runtime failed"
        );
        assert_eq!(
            format!(
                "{:#}",
                anyhow::Error::new(pump_wait_runtime_failed(io_err("boom")))
            ),
            "SSH pump wait runtime failed: boom"
        );
        assert_eq!(
            pump_borrow_source_failed(pump_output_lock_poisoned()).message(),
            "SSH_PUMP cannot borrow the source pipe"
        );
        assert_eq!(
            format!(
                "{:#}",
                anyhow::Error::new(pump_borrow_source_failed(pump_output_lock_poisoned()))
            ),
            "SSH_PUMP cannot borrow the source pipe: SSH pump output lock poisoned"
        );
        assert_eq!(
            pump_borrow_target_failed(pump_output_lock_poisoned()).message(),
            "SSH_PUMP cannot borrow the target pipe"
        );
        assert_eq!(
            format!(
                "{:#}",
                anyhow::Error::new(pump_borrow_target_failed(pump_output_lock_poisoned()))
            ),
            "SSH_PUMP cannot borrow the target pipe: SSH pump output lock poisoned"
        );
        assert_eq!(
            pump_target_write_failed(io_err("boom")).message(),
            "SSH_PUMP target pipe write failed"
        );
        assert_eq!(
            format!(
                "{:#}",
                anyhow::Error::new(pump_target_write_failed(io_err("boom")))
            ),
            "SSH_PUMP target pipe write failed: boom"
        );
        assert_eq!(
            pump_target_flush_failed(io_err("boom")).message(),
            "SSH_PUMP target pipe flush failed"
        );
        assert_eq!(
            format!(
                "{:#}",
                anyhow::Error::new(pump_target_flush_failed(io_err("boom")))
            ),
            "SSH_PUMP target pipe flush failed: boom"
        );
        assert_eq!(
            authentication_rejected("SSH_EXEC").message(),
            "SSH_EXEC authentication rejected"
        );
        assert_eq!(
            dial_failed("SSH_EXEC", &reason_sample("boom")).message(),
            "SSH_EXEC dial failed"
        );
        assert_eq!(
            format!(
                "{:#}",
                anyhow::Error::new(dial_failed("SSH_EXEC", &reason_sample("boom")))
            ),
            "SSH_EXEC dial failed: boom"
        );
        assert_eq!(
            auth_exchange_failed("SSH_EXEC", io_err("boom")).message(),
            "SSH_EXEC authentication exchange failed"
        );
        assert_eq!(
            format!(
                "{:#}",
                anyhow::Error::new(auth_exchange_failed("SSH_EXEC", io_err("boom")))
            ),
            "SSH_EXEC authentication exchange failed: boom"
        );
        assert_eq!(
            channel_open_failed("SSH_EXEC", io_err("boom")).message(),
            "SSH_EXEC channel open failed"
        );
        assert_eq!(
            format!(
                "{:#}",
                anyhow::Error::new(channel_open_failed("SSH_EXEC", io_err("boom")))
            ),
            "SSH_EXEC channel open failed: boom"
        );
        assert_eq!(
            exec_request_failed(io_err("boom")).message(),
            "SSH_EXEC exec request failed"
        );
        assert_eq!(
            format!(
                "{:#}",
                anyhow::Error::new(exec_request_failed(io_err("boom")))
            ),
            "SSH_EXEC exec request failed: boom"
        );
        assert_eq!(
            shell_request_failed(io_err("boom")).message(),
            "SSH_CONNECT shell request failed"
        );
        assert_eq!(
            format!(
                "{:#}",
                anyhow::Error::new(shell_request_failed(io_err("boom")))
            ),
            "SSH_CONNECT shell request failed: boom"
        );
        assert_eq!(
            listener_prepare_failed(io_err("boom")).message(),
            "prepare listener for the runtime"
        );
        assert_eq!(
            format!(
                "{:#}",
                anyhow::Error::new(listener_prepare_failed(io_err("boom")))
            ),
            "prepare listener for the runtime: boom"
        );
        assert_eq!(
            listener_handoff_failed(io_err("boom")).message(),
            "hand listener to the runtime"
        );
        assert_eq!(
            format!(
                "{:#}",
                anyhow::Error::new(listener_handoff_failed(io_err("boom")))
            ),
            "hand listener to the runtime: boom"
        );
        assert_eq!(
            listener_failed(io_err("boom")).message(),
            "SSH listener failed"
        );
        assert_eq!(
            format!("{:#}", anyhow::Error::new(listener_failed(io_err("boom")))),
            "SSH listener failed: boom"
        );
        assert_eq!(
            client_runtime_build_failed(io_err("boom")).message(),
            "build SSH client runtime"
        );
        assert_eq!(
            format!(
                "{:#}",
                anyhow::Error::new(client_runtime_build_failed(io_err("boom")))
            ),
            "build SSH client runtime: boom"
        );
        assert_eq!(
            session_pump_already_finished().message(),
            "SSH session pump already finished"
        );
        assert_eq!(
            session_wire_queue_saturated().message(),
            "SSH session wire queue saturated under backpressure"
        );
        assert_eq!(
            session_wire_writer_gone().message(),
            "SSH session wire writer is gone"
        );
        assert_eq!(
            session_already_pumped().message(),
            "SSH session already pumped"
        );
        assert_eq!(channel_data_unknown().message(), "data for unknown channel");
        assert_eq!(channel_pump_gone().message(), "pump went away");
        assert_eq!(
            channel_wire_gone_eof_close().message(),
            "wire gone during EOF close"
        );
        assert_eq!(
            channel_request_unknown().message(),
            "request for unknown channel"
        );
        assert_eq!(
            channel_already_announced().message(),
            "channel already announced"
        );
        assert_eq!(
            run_child_read_failed("stdout", &err).message(),
            "SSH run child stdout read failed: boom"
        );
        assert_eq!(
            run_output_write_failed(io_err("boom")).message(),
            "SSH run output pipe write failed"
        );
        assert_eq!(
            format!(
                "{:#}",
                anyhow::Error::new(run_output_write_failed(io_err("boom")))
            ),
            "SSH run output pipe write failed: boom"
        );
        assert_eq!(
            run_output_flush_failed(io_err("boom")).message(),
            "SSH run output pipe flush failed"
        );
        assert_eq!(
            format!(
                "{:#}",
                anyhow::Error::new(run_output_flush_failed(io_err("boom")))
            ),
            "SSH run output pipe flush failed: boom"
        );
        assert_eq!(
            run_output_lock_poisoned().message(),
            "SSH run output lock poisoned"
        );
        assert_eq!(
            run_needs_program().message(),
            "SSH_RUN needs at least a program"
        );
        assert_eq!(
            run_spawn_lost_stderr().message(),
            "SSH run spawn lost stderr"
        );
        assert_eq!(
            run_interrupted_by_cancellation().message(),
            "SSH run interrupted by cancellation"
        );
        assert_eq!(
            run_borrow_input_failed(pump_output_lock_poisoned()).message(),
            "SSH run cannot borrow the input pipe"
        );
        assert_eq!(
            format!(
                "{:#}",
                anyhow::Error::new(run_borrow_input_failed(pump_output_lock_poisoned()))
            ),
            "SSH run cannot borrow the input pipe: SSH pump output lock poisoned"
        );
        assert_eq!(
            run_borrow_output_failed(pump_output_lock_poisoned()).message(),
            "SSH run cannot borrow the output pipe"
        );
        assert_eq!(
            format!(
                "{:#}",
                anyhow::Error::new(run_borrow_output_failed(pump_output_lock_poisoned()))
            ),
            "SSH run cannot borrow the output pipe: SSH pump output lock poisoned"
        );
        assert_eq!(
            run_borrow_error_failed(pump_output_lock_poisoned()).message(),
            "SSH run cannot borrow the error pipe"
        );
        assert_eq!(
            format!(
                "{:#}",
                anyhow::Error::new(run_borrow_error_failed(pump_output_lock_poisoned()))
            ),
            "SSH run cannot borrow the error pipe: SSH pump output lock poisoned"
        );
        assert_eq!(
            run_spawn_failed(pump_output_lock_poisoned()).message(),
            "SSH run spawn failed"
        );
        assert_eq!(
            format!(
                "{:#}",
                anyhow::Error::new(run_spawn_failed(pump_output_lock_poisoned()))
            ),
            "SSH run spawn failed: SSH pump output lock poisoned"
        );
        assert_eq!(
            pty_output_lock_poisoned().message(),
            "SSH pty output lock poisoned"
        );
        assert_eq!(
            pty_interrupted_by_cancellation().message(),
            "SSH pty run interrupted by cancellation"
        );
        assert_eq!(
            pty_borrow_input_failed(pump_output_lock_poisoned()).message(),
            "SSH pty cannot borrow the input pipe"
        );
        assert_eq!(
            format!(
                "{:#}",
                anyhow::Error::new(pty_borrow_input_failed(pump_output_lock_poisoned()))
            ),
            "SSH pty cannot borrow the input pipe: SSH pump output lock poisoned"
        );
        assert_eq!(
            pty_borrow_output_failed(pump_output_lock_poisoned()).message(),
            "SSH pty cannot borrow the output pipe"
        );
        assert_eq!(
            format!(
                "{:#}",
                anyhow::Error::new(pty_borrow_output_failed(pump_output_lock_poisoned()))
            ),
            "SSH pty cannot borrow the output pipe: SSH pump output lock poisoned"
        );
        assert_eq!(
            pty_session_init_write_failed(io_err("boom")).message(),
            "SSH pty session init write failed"
        );
        assert_eq!(
            format!(
                "{:#}",
                anyhow::Error::new(pty_session_init_write_failed(io_err("boom")))
            ),
            "SSH pty session init write failed: boom"
        );
        assert_eq!(
            pty_session_init_flush_failed(io_err("boom")).message(),
            "SSH pty session init flush failed"
        );
        assert_eq!(
            format!(
                "{:#}",
                anyhow::Error::new(pty_session_init_flush_failed(io_err("boom")))
            ),
            "SSH pty session init flush failed: boom"
        );
        assert_eq!(
            dequeue_task_cancelled("SSH_DEQUEUE").message(),
            "SSH_DEQUEUE: task cancelled"
        );
        assert_eq!(
            dequeue_server_closed("SSH_ACCEPT").message(),
            "SSH_ACCEPT: server is closed"
        );
        assert_eq!(
            dequeue_requires_async().message(),
            "SSH_DEQUEUE requires ASYNC: wrap it as LET $t: HANDLE<T> = ASYNC { SSH_DEQUEUE($server.server) }"
        );
        assert_eq!(
            accept_requires_async().message(),
            "SSH_ACCEPT requires ASYNC: wrap it as LET $t: HANDLE<T> = ASYNC { SSH_ACCEPT($server, $in, $out) }"
        );
        assert_eq!(
            pump_channel_requires_async().message(),
            "SSH_PUMP_CHANNEL requires ASYNC: pump it in its own task after SSH_DEQUEUE"
        );
        assert_eq!(
            connect_requires_async().message(),
            "SSH_CONNECT requires ASYNC: run it in its own task beside the SSH_PUMP tasks"
        );
        assert_eq!(
            connect_offline().message(),
            "SSH_CONNECT failed: engine running in --offline mode"
        );
        assert_eq!(
            connect_username_empty().message(),
            "SSH_CONNECT username must not be empty"
        );
        assert_eq!(
            connect_failed(pump_output_lock_poisoned()).message(),
            "SSH_CONNECT failed"
        );
        assert_eq!(
            format!(
                "{:#}",
                anyhow::Error::new(connect_failed(pump_output_lock_poisoned()))
            ),
            "SSH_CONNECT failed: SSH pump output lock poisoned"
        );
        assert_eq!(
            pty_run_requires_async().message(),
            "SSH_PTY_RUN requires ASYNC: run it in its own task beside the session pump task"
        );
        assert_eq!(
            run_requires_async().message(),
            "SSH_RUN requires ASYNC: run it in its own task beside the session pump task"
        );
        assert_eq!(
            exec_needs_program().message(),
            "SSH_EXEC needs at least a program"
        );
        assert_eq!(
            exec_username_empty().message(),
            "SSH_EXEC username must not be empty"
        );
        assert_eq!(
            exec_offline().message(),
            "SSH_EXEC failed: engine running in --offline mode"
        );
        assert_eq!(
            exec_failed(pump_output_lock_poisoned()).message(),
            "SSH_EXEC failed"
        );
        assert_eq!(
            format!(
                "{:#}",
                anyhow::Error::new(exec_failed(pump_output_lock_poisoned()))
            ),
            "SSH_EXEC failed: SSH pump output lock poisoned"
        );
        assert_eq!(
            serve_option_empty("username").message(),
            "SSH_SERVE option 'username' must not be empty"
        );
        assert_eq!(
            serve_option_required("password").message(),
            "SSH_SERVE option 'password' is required"
        );
        assert_eq!(
            option_must_be_duration(
                "SSH_SERVE",
                "idle_timeout",
                "soon",
                pump_output_lock_poisoned(),
            )
            .message(),
            "SSH_SERVE option 'idle_timeout' must be a duration, got \"soon\""
        );
        assert_eq!(
            format!(
                "{:#}",
                anyhow::Error::new(option_must_be_duration(
                    "SSH_SERVE",
                    "idle_timeout",
                    "soon",
                    pump_output_lock_poisoned(),
                ))
            ),
            "SSH_SERVE option 'idle_timeout' must be a duration, got \"soon\": SSH pump output lock poisoned"
        );
        assert_eq!(
            option_must_be_duration("SSH_EXEC", "timeout", "soon", pump_output_lock_poisoned())
                .message(),
            "SSH_EXEC option 'timeout' must be a duration, got \"soon\""
        );
        assert_eq!(
            ephemeral_key_generate_failed(io_err("boom")).message(),
            "generate ephemeral Ed25519 host key"
        );
        assert_eq!(
            format!(
                "{:#}",
                anyhow::Error::new(ephemeral_key_generate_failed(io_err("boom")))
            ),
            "generate ephemeral Ed25519 host key: boom"
        );
        assert_eq!(
            serve_listener_clone_failed(io_err("boom")).message(),
            "SSH_SERVE cannot clone its listener"
        );
        assert_eq!(
            format!(
                "{:#}",
                anyhow::Error::new(serve_listener_clone_failed(io_err("boom")))
            ),
            "SSH_SERVE cannot clone its listener: boom"
        );
        assert_eq!(
            serve_spawn_failed(io_err("boom")).message(),
            "SSH_SERVE cannot spawn the server thread"
        );
        assert_eq!(
            format!(
                "{:#}",
                anyhow::Error::new(serve_spawn_failed(io_err("boom")))
            ),
            "SSH_SERVE cannot spawn the server thread: boom"
        );
        assert_eq!(
            exec_option_must_be_positive("timeout").message(),
            "SSH_EXEC option 'timeout' must be positive"
        );
        assert_eq!(
            send_exit_status_negative(-1).message(),
            "SSH_SEND_EXIT_STATUS code must not be negative: -1"
        );
        assert_eq!(
            send_exit_status_out_of_range(1 << 40).message(),
            "SSH_SEND_EXIT_STATUS code out of range: 1099511627776"
        );
    }
}