onepipeline 0.27.0

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

use std::collections::BTreeMap;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Duration;

use onevcs::{
    DraftReason, EventStream, Lifecycle, MergePolicy, Providers, Publication, PublishOutcome,
    PublishRequest, Session, SessionRequest, SessionToken, Subject,
};
use serde::{Deserialize, Serialize};

use crate::error::{Error, Result};
use crate::event::Envelope;
use crate::filter::EventFilter;

/// The sibling this module speaks for, as [`Error::Sibling`] names a tool.
///
/// A constant because [`session_open_conflicted`] reads it back: what that asks
/// is whether *this* function composed the refusal in hand, and comparing
/// against a literal typed out a second time would answer yes for a refusal
/// spelled the same way by anything else.
const ONEVCS: &str = "onevcs";

fn sibling(message: impl Into<String>) -> Error {
    Error::Sibling {
        tool: ONEVCS,
        message: message.into(),
    }
}

/// A refusal from the sibling, as this crate's own error.
fn refusal(error: onevcs::Error) -> Error {
    sibling(error.to_string())
}

/// Git and GitHub — what every operation here runs against.
///
/// [`Providers::real`] rather than a value held on this module: both defaults are
/// stateless and the sibling hands out one shared pair per process, so there is
/// nothing to keep.
fn providers() -> Providers<'static> {
    Providers::real()
}

/// How a refusal this module composed says a session open met a **base
/// conflict**, written at the **head** of the message.
///
/// Written and read here, so it is this module's own convention rather than a
/// reading of somebody else's sentence: the classification is made off `onevcs`'s
/// typed [`SyncConflict`](onevcs::Error::SyncConflict) and this marker carries it
/// the one hop to the retry loop.
///
/// A marker at all because the hop is the **executor seam**, whose refusal
/// `docs/contract.md` fixes as [`Error::Sibling`] — a tool and a message, with
/// no room for a class of its own, and [`crate::error::Error`]'s variants are
/// that document's too. What is in this crate's gift is where the marker is
/// composed, where it is read, and what it is read *off*: one function each, and
/// the error value the seam returned rather than any prose downstream of it.
const SESSION_OPEN_CONFLICT: &str = "the base conflicts with this branch";

/// Open a session over a per-run clone and worktree.
pub fn session_open(request: &SessionRequest) -> Result<Session> {
    providers()
        .vcs
        .open_session(request.clone())
        .map_err(session_refusal)
}

/// A session open `onevcs` refused, as this crate's own error — with the one
/// refusal no further attempt converges on **named**.
///
/// A sync conflict is two conditions sharing one word. During a publication the
/// base moved under work already going and the bounded resolve-and-requeue lost
/// the race, which another attempt can win — [`Preserving::SyncConflict`]. At
/// *session open* it is a merge nobody has performed, and opening the session
/// again reproduces the identical refusal.
fn session_refusal(error: onevcs::Error) -> Error {
    match &error {
        onevcs::Error::SyncConflict { .. } => sibling(format!("{SESSION_OPEN_CONFLICT}: {error}")),
        _ => refusal(error),
    }
}

/// Whether a dispatch was refused because the session it needed met a base
/// conflict when it opened.
///
/// Asked of the **error the executor seam returned**, and never of a settlement's
/// detail: a detail is prose, an agent writes prose, and a decision that stops
/// retrying and blocks a subtree must not be reachable from anything a dispatch
/// can say. Three things have to hold at once, and only [`session_refusal`] makes
/// all three — the refusal is [`Error::Sibling`], it names [`ONEVCS`], and its
/// message *begins* with [`SESSION_OPEN_CONFLICT`] rather than merely containing
/// it somewhere.
pub(crate) fn session_open_conflicted(error: &Error) -> bool {
    matches!(
        error,
        Error::Sibling { tool, message }
            if *tool == ONEVCS && message.starts_with(SESSION_OPEN_CONFLICT)
    )
}

/// Verify a session's work and publish it under its policy.
///
/// The answer is [`Publication`] — the sibling's own value — so *what happened*
/// is a case to match on rather than a sentence to read. A publication that did
/// not land is [`PublishOutcome::Failed`] and not an `Err`: the sibling draws the
/// line between a refused request and a publication that ran and did not land,
/// and this crate reads its line rather than a second one.
///
/// The title is checked here, where the request is built, because
/// [`Subject`]'s conversion is where the sibling checks it — a title too long to
/// be a commit subject is refused before a session's work is committed rather
/// than after. A title of `None` is the plan stating none: the sibling then
/// derives the subject from the branch's own conventional commits, which is a
/// better subject than anything this crate could compose about work it did not
/// do.
///
/// The body crosses as the prose it is. Nothing checks it, here or there — a
/// host places no shape on a change request's body, so there is no rule to hold
/// it to and inventing one would refuse a body the host would have taken. What
/// it is *not* is a node's `task`: that is the brief its agent was given, not a
/// description of what the branch turned out to hold, so a body is one that was
/// drafted from the diff or there is none.
pub fn publish(
    token: &SessionToken,
    policy: Option<MergePolicy>,
    title: Option<&str>,
    body: Option<&str>,
    draft: Option<&DraftReason>,
) -> Result<Publication> {
    let title = title
        .map(|title| title.parse::<Subject>().map_err(sibling))
        .transpose()?;
    // Held to the sibling's own rule where it is composed rather than where it
    // arrives, for [`Subject`]'s reason: a reason that would not render as itself
    // is refused before a session's work is committed against it.
    if let Some(reason) = draft {
        reason.checked().map_err(refusal)?;
    }
    onevcs::publish(
        &providers(),
        token,
        &PublishRequest {
            policy,
            title,
            body: body.map(str::to_owned),
            draft: draft.cloned(),
        },
    )
    .map_err(refusal)
}

/// The word a node whose change request is held open as a **draft** settles on.
///
/// Its own outcome beside `change-open` rather than a shade of it, because the
/// two differ in the one fact every reader of a settlement acts on: an open change
/// can land and a draft cannot. Folded together, a fast-adoption node that stopped
/// short of merging its temporary git pin would report exactly as one that
/// published a change somebody is reviewing — which is the reading that let a git
/// pin become permanent in a base branch in the first place.
pub const DRAFTED: &str = "change-draft";

/// How a publication settles the node that made it.
///
/// This crate's own outcome vocabulary, which a plan's readers and `results`
/// render: `no-changes` is the name a node whose steps all declared no diff
/// already settles on, so a publication with nothing to publish reads the same
/// way rather than inventing a second word for it.
pub fn outcome_of(outcome: &PublishOutcome) -> &'static str {
    match outcome {
        PublishOutcome::Merged(_) => "merged",
        PublishOutcome::ChangeOpen(_) => "change-open",
        // Its own word and not `change-open`'s, because the two differ in the one
        // thing a reader acts on: a draft cannot land. See [`DRAFTED`].
        PublishOutcome::ChangeDraft(_) => DRAFTED,
        PublishOutcome::Queued(_) => "queued",
        PublishOutcome::NothingToPublish => "no-changes",
        PublishOutcome::Failed { kind, .. } => failure_of(*kind).outcome(),
    }
}

/// A publication failure a further attempt **by the agent** on the same branch
/// could answer.
///
/// A closed set and not a word, because these four *are* the vocabulary: each
/// one is a settlement the contract publishes, a routing decision this crate
/// makes, and a case a reader of a preserved failure switches on. Carried as a
/// string, a fifth spelling would be constructible everywhere one of these is —
/// in a settlement, in a re-dispatch's reason, in the roll-up a spent budget
/// writes — and every one of those is a word the contract does not name reaching
/// an operator as though it did.
///
/// Every one of them is the **tree** being rejected, which is why the answer to
/// each is a worker sent back to the branch that carries it. A push that reached
/// the remote and left only the read behind it unanswered is not one of those and
/// is [`Failure::Unread`] instead.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Preserving {
    /// A required check the host reports concluded red.
    ChecksFailed,
    /// The bound on watching the host elapsed with the change still outstanding.
    ChecksUnsettled,
    /// The publishing push was refused by the merge path.
    PushRejected,
    /// The base moved under the publication and the bounded resolve-and-requeue
    /// did not converge.
    SyncConflict,
}

impl Preserving {
    /// The word the node settles on.
    #[must_use]
    pub fn outcome(self) -> &'static str {
        match self {
            Self::ChecksFailed => "checks-failed",
            Self::ChecksUnsettled => "checks-unsettled",
            Self::PushRejected => "push-rejected",
            Self::SyncConflict => "sync-conflict",
        }
    }
}

/// One publication failure, as this crate settles and routes it.
///
/// The word and the routing are **one** value, because they are one judgement:
/// a failure this crate names is exactly a failure it knows what to do about,
/// and one it cannot name is the residual it can only report. Two fields would
/// permit the state that judgement rules out — a word of its own that is never
/// re-dispatched, which reads from every view as a failure that was routed.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Failure {
    /// Its fix is *more work on the same branch* — a red check to make green, a
    /// conflict to resolve, a push a hook refused — and it settles under that
    /// failure's own word. The tree that was rejected is still there, so a
    /// worker sent back to it meets the thing that rejected it.
    Preserving(Preserving),
    /// The publishing push **reached the remote** and only the merge path behind
    /// it could not be read.
    ///
    /// Apart from [`Preserving`](Self::Preserving) because the fix is not more
    /// work: nothing about the tree was rejected, and a worker sent back to it
    /// would pay for a fresh clone and a fresh gate to re-push what the origin
    /// already carries. What is missing is one more **read of the host**, so this
    /// one is answered by a bounded re-read and settles under
    /// [`Failure::UNREAD`] only when that read is spent.
    Unread,
    /// Nothing a further attempt could answer: a request `onevcs` refused at its
    /// trust boundary, a seam with no implementation, and a gate that ran on the
    /// tree as it stands all answer the same way however many times they are
    /// asked. It settles under [`Failure::RESIDUAL`].
    Terminal,
}

impl Failure {
    /// The word every failure with nothing to continue settles on.
    ///
    /// The one this crate settled *every* publication failure on before any of
    /// them were told apart, kept for exactly the kinds no continuation follows
    /// from — so no reader of it has to relearn anything.
    pub const RESIDUAL: &'static str = "publication-failed";

    /// The word a push that reached the remote settles on once the re-read of
    /// the merge path behind it is spent.
    ///
    /// The spelling it has always had: what changed is how the node is recovered,
    /// not what a reader of the settled node reads.
    pub const UNREAD: &'static str = "pushed-unverified";

    /// The word the node settles on.
    #[must_use]
    pub fn outcome(self) -> &'static str {
        match self {
            Self::Preserving(preserving) => preserving.outcome(),
            Self::Unread => Self::UNREAD,
            Self::Terminal => Self::RESIDUAL,
        }
    }
}

/// Which failure a publication ended in, in this crate's own words.
///
/// Arm by arm rather than by a wildcard, deliberately: a kind the sibling adds
/// is a routing decision this crate has to make, and a wildcard would make it
/// silently — reporting a new failure as the residual and never re-dispatching
/// it, which is exactly the undifferentiated settlement this exists to end.
pub fn failure_of(kind: onevcs::FailureKind) -> Failure {
    use onevcs::FailureKind;
    match kind {
        FailureKind::ChecksFailed => Failure::Preserving(Preserving::ChecksFailed),
        FailureKind::ChecksUnsettled => Failure::Preserving(Preserving::ChecksUnsettled),
        FailureKind::PushRejected => Failure::Preserving(Preserving::PushRejected),
        FailureKind::SyncConflict => Failure::Preserving(Preserving::SyncConflict),
        // The push landed and only the *read* behind it did not, which is the
        // opposite of "nothing a further attempt could answer" — and equally not
        // one of the four above: the tree was never rejected, so what answers it
        // is another read of the host rather than another dispatch of the agent.
        FailureKind::PushedUnverified => Failure::Unread,
        // `Gate` is a kind no publication produces any more — `onevcs` 0.11.0
        // runs no gate — and it is routed rather than dropped because the
        // sibling still names it: an arm removed here would be a wildcard by
        // another name the day anything emits it again. Terminal beside the
        // other two, on the reading the kind was written for: a verdict on the
        // tree as it stands, which nothing this crate can do from here changes.
        FailureKind::Gate | FailureKind::Invalid | FailureKind::NotImplemented => Failure::Terminal,
    }
}

/// One piece of evidence a session's publication recorded, as a reader fetches
/// it.
///
/// A **pointer**, never the bytes: an artifact is a check's log or a conflict's
/// hunks, which is megabytes of somebody else's output, and what a diagnosis
/// carries is what to ask for it with. Both halves stay the types the envelope
/// they were read off carries them as, so nothing between that stream and the
/// note is a string this crate re-derived.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Evidence {
    /// Which part of the publication produced it — `onevcs`'s own kind, so
    /// naming one restates no vocabulary this crate does not own.
    pub kind: crate::event::EventKind,
    /// What `onevcs artifact cat` takes.
    pub id: crate::event::ArtifactId,
}

/// The evidence one session's publication recorded.
///
/// Read off the session's **own stream** and **unfiltered**, deliberately: this
/// is not a relay into the merged store but the node's own read of what its
/// publication left behind, and a launch that narrowed what it *ingests* did not
/// ask to be unable to diagnose a failure.
///
/// In stream order and **deduplicated**, because an artifact is stored once and
/// referenced from wherever the publication points at it: a note listing one id
/// twice reads as two runs of whatever produced it, and sends somebody looking
/// for the difference between them. `onevcs` 0.11.0 happens to reference each
/// artifact it stores from one record only — the two-record shape was the
/// `pre-push` gate's, whose verdict *was* the push's own output, and that gate is
/// gone — so nothing downstream of this crate holds the property. It is kept
/// because it is a property of the **note**, not of any one version of the
/// stream behind it.
pub fn evidence_in(token: &SessionToken) -> Vec<Evidence> {
    let mut evidence: Vec<Evidence> = Vec::new();
    for envelope in events(token, None) {
        for artifact in envelope.artifacts {
            let found = Evidence {
                kind: envelope.kind.clone(),
                id: artifact.id,
            };
            if !evidence.iter().any(|kept| kept.id == found.id) {
                evidence.push(found);
            }
        }
    }
    evidence
}

/// Whether the publication's change reached its base branch.
///
/// Read off **what the publication answered**, and off nothing else. The
/// repository's policy is not consulted here and must not be: a `change-direct`
/// or `change-auto` identity asks the host to land the change immediately, and
/// whether it did is the host's answer rather than the ask — a required check
/// still running, a branch protection rule, or a merge queue all leave the same
/// policy sitting at [`PublishOutcome::Queued`]. Deriving "landed" from the
/// policy would report exactly the state this distinction exists to expose.
///
/// [`PublishOutcome::Merged`] is the one landed case, and it is an observation:
/// `onevcs` produces it holding the commit the change reached its base at, from
/// git on the direct path and from the host's own answer on the change-request
/// one.
///
/// `None` where the node has no change of its own to land.
/// [`PublishOutcome::NothingToPublish`] is a branch whose base already carried
/// its content. [`PublishOutcome::Failed`] is here for totality rather than for
/// use: `crate::lifecycle` settles that case before it asks, under its own
/// `failed` status — which no reader mistakes for success, so qualifying it would
/// put a second word on a fact already stated. Both answer `None`, so the arm and
/// the early return agree if that ever changes.
///
/// Nothing here waits. A change request a person has to merge is reported as
/// unlanded and the round moves on; the run neither blocks nor polls for a merge
/// somebody else owns.
pub fn landing_of(outcome: &PublishOutcome) -> Option<crate::graph::Landing> {
    use crate::graph::Landing;
    match outcome {
        PublishOutcome::Merged(_) => Some(Landing::Landed),
        PublishOutcome::ChangeOpen(_)
        | PublishOutcome::ChangeDraft(_)
        | PublishOutcome::Queued(_) => Some(Landing::Unlanded),
        PublishOutcome::NothingToPublish | PublishOutcome::Failed { .. } => None,
    }
}

/// What a landing read taken **now** says about one branch.
///
/// Two cases, because there are two things that can happen: the sibling answered,
/// or the read refused. The answer is that library's own [`onevcs::Landed`],
/// carried whole rather than unpacked into a verdict and a sentence beside it —
/// so evidence that does not belong to an answer is unrepresentable, and this
/// crate holds no second account of what a landing is. Which of the three a
/// reader is shown is decided at the line that prints it, out of the same value.
///
/// The refusal is prose because it is somebody else's error, which is the one
/// thing here that has no type to be.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum LandingRead {
    /// `onevcs` decided it, and this is what it decided.
    Answered(onevcs::Landed),
    /// The read did not get an answer, and this is what refused.
    Refused {
        /// What could not decide it, on one line.
        because: String,
    },
}

/// Ask `onevcs` whether one branch's work has reached its base, as of now.
///
/// [`onevcs::landing_status`] is the landing decision on that library's public
/// surface **on its own**: it takes no release target, reads no release
/// configuration, and answers every repository the same way — which is what
/// makes it askable of a run whose repository declares no release target at all.
/// The four tiers behind it are that library's, decided in one place there, so
/// this crate holds no second account of what a landing is.
///
/// The **branch** is the reference, because that is the spelling the sibling
/// resolves work by — see [`crate::release::Dependency::reference`], which asks
/// the same library the same way — and `repo` is the node's own repository,
/// which narrows a branch name two identities could both hold to the one this
/// run's work is in.
///
/// **This read takes the repository from scratch on every call**, so a render
/// pays for one per node rather than one per repository. That cost is counted
/// rather than assumed — see [`crate::rendercost::repository_asked`] — and
/// divergence 33 in `docs/contract-divergences.md` records why none of the
/// sibling's reads can collapse it, and the proposal that would.
//
// llmlint: ignore-block[invalid_states_unrepresentable] a branch is the plain string every
// reference in this crate is, for the reason `crate::projection`'s `landing_commits`
// records: it is what the journal payload carries and what the sibling's own reference
// grammar takes. What could go wrong with an unchecked one is checked where it enters,
// by `usable`, which is what wrote every value that reaches this.
pub(crate) fn landing_now(branch: &str, repo: Option<&str>) -> LandingRead {
    crate::rendercost::landing_read_taken(branch, repo);
    let answered = read_of(onevcs::landing_status(branch, repo));
    // One per read, because the read takes one from scratch. Recorded after it
    // rather than inferred from it, so a build that asked about a repository
    // *without* reading a landing — or twice for one read — is counted rather
    // than assumed away.
    crate::rendercost::repository_asked(repo);
    answered
}
// llmlint: ignore-end[invalid_states_unrepresentable]

/// How the sibling's answer reaches a caller.
///
/// Split out so a refusal is provable without a repository: the two are not the
/// same answer, and a refusal read as "not landed" is the one mistake that sends
/// somebody to publish work the base already carries.
fn read_of(answered: onevcs::Result<onevcs::Landed>) -> LandingRead {
    match answered {
        Ok(landed) => LandingRead::Answered(landed),
        Err(refused) => LandingRead::Refused {
            because: crate::views::one_line(&format!("this host could not decide it: {refused}")),
        },
    }
}

/// Whether `onevcs` can **show** that one branch's work reached its base.
///
/// Named for the proof rather than for the question: `false` is "no landing was
/// shown", which covers the sibling saying the work has not landed *and* the
/// sibling declining to answer at all. A caller acts on `true` alone — it
/// replaces a settlement's own dated claim — so collapsing the other two costs
/// nothing and a name promising a verdict would.
pub(crate) fn proved_landed(branch: &str, repo: Option<&str>) -> bool {
    matches!(
        landing_now(branch, repo),
        LandingRead::Answered(landed) if landed.is_landed()
    )
}

/// The nodes whose publication left its work on the origin with the merge path
/// unread, and how many times this run has asked again about each.
///
/// `crate::graph` holds their dependents rather than skipping them, and a hold
/// nothing lifts stops a run just as surely — so this is what lifts it. Bounded
/// by [`HOLD_ASKS`], and asked only about a node actually holding something.
#[derive(Debug, Default)]
pub(crate) struct UnreadMergePaths {
    /// Keyed by the type that *is* a node identity rather than by the string one
    /// spells: every key here came from a node in this run's graph, and
    /// [`crate::graph::NodeRef`] is what carries that past the boundary.
    asking: BTreeMap<crate::graph::NodeRef, Asking>,
}

/// How many times one node's unread merge path is asked about again.
///
/// Deliberately not [`crate::engine::merge_path_reads`]: that one is a
/// publication holding a session open, so it is small, while nothing is occupied
/// by waiting here. Twelve at the default interval is a minute of holding, which
/// is a host outage's timescale — and a merge a *person* performs is not
/// something a run waits for at all.
const HOLD_ASKS: std::num::NonZeroU32 = match std::num::NonZeroU32::new(12) {
    Some(asks) => asks,
    None => unreachable!(),
};

/// How one node's asking has gone.
///
/// The count rides both cases, so a landing this run never asked for cannot be
/// recorded.
#[derive(Debug, Clone, Copy)]
enum Asking {
    /// Asked this many times, and no landing shown yet.
    Unanswered(std::num::NonZeroU32),
    /// `onevcs` showed the change reaching its base, on the last of this many
    /// asks.
    Landed(std::num::NonZeroU32),
}

impl Asking {
    fn asks(self) -> std::num::NonZeroU32 {
        match self {
            Self::Unanswered(asks) | Self::Landed(asks) => asks,
        }
    }
}

impl UnreadMergePaths {
    /// How long to leave between asks: the publication's own first backoff,
    /// because it is the same question about the same host and how patient a run
    /// is with a merge path should not need saying twice.
    pub(crate) fn every(&self) -> Duration {
        crate::engine::merge_path_backoff()
    }

    /// The nodes still worth asking about: settled with their work on the origin,
    /// holding at least one dependent, and neither answered nor asked out.
    pub(crate) fn watching(
        &self,
        state: &crate::projection::RunState,
        statuses: &BTreeMap<String, crate::graph::NodeStatus>,
    ) -> Vec<crate::graph::NodeRef> {
        let budget = HOLD_ASKS.get();
        state
            .graph
            .iter()
            // A node identity leaving the graph's own scope is carried as one
            // rather than as a field anybody could put anything in.
            .filter_map(crate::graph::NodeRef::of)
            .filter(|node| {
                statuses.get(node.as_str()) == Some(&crate::graph::NodeStatus::Failed)
                    && state.outcomes.get(node.as_str()).map(String::as_str)
                        == Some(Failure::UNREAD)
                    && match self.asking.get(node) {
                        None => true,
                        Some(Asking::Unanswered(asks)) => asks.get() < budget,
                        Some(Asking::Landed(_)) => false,
                    }
            })
            .filter(|node| {
                state
                    .graph
                    .dependents_of(node.as_str())
                    .iter()
                    .any(|dependent| {
                        statuses.get(dependent) == Some(&crate::graph::NodeStatus::Blocked)
                    })
            })
            .collect()
    }

    /// Put every landing this run has been shown back onto the state it belongs
    /// to.
    ///
    /// Every pass rather than once where it was read, because the loop re-folds
    /// the whole state from the journal each time a node settles and this is not
    /// in the journal. Reports whether the state had lost one.
    pub(crate) fn apply(&self, state: &mut crate::projection::RunState) -> bool {
        let mut restored = false;
        for (node, _) in self
            .asking
            .iter()
            .filter(|(_, asking)| matches!(asking, Asking::Landed(_)))
        {
            let already = state
                .landings
                .insert(node.as_str().to_owned(), crate::graph::Landing::Landed);
            restored |= already != Some(crate::graph::Landing::Landed);
        }
        restored
    }

    /// Ask `onevcs` again about each of them, and record every landing it can
    /// show.
    ///
    /// The answer goes onto the run's own state rather than into its journal, for
    /// the reason `crate::engine`'s cross-DAG resolution does the same: it is a
    /// read of something this run does not write, so a fresh driver re-derives it
    /// rather than inheriting a claim it cannot check. Reports whether anything
    /// became decidable.
    pub(crate) fn read_again(
        &mut self,
        state: &mut crate::projection::RunState,
        watching: &[crate::graph::NodeRef],
    ) -> bool {
        let mut lifted = false;
        for node in watching {
            let id = node.as_str();
            // Every ask is counted, whatever it answers: the budget is on the
            // asking rather than on the answers, so a host that never comes back
            // is bounded by the same number a host that does is.
            let asks = self
                .asking
                .get(node)
                .map_or(std::num::NonZeroU32::MIN, |asking| {
                    asking.asks().saturating_add(1)
                });
            self.asking.insert(node.clone(), Asking::Unanswered(asks));
            let Some(branch) = state.branches.get(id).cloned() else {
                continue;
            };
            // The node's own repository, so a branch name two identities both
            // hold is asked about the one this run's work is in.
            let repo = state.graph.get(id).and_then(|node| node.repo.clone());
            if proved_landed(&branch, repo.as_deref()) {
                self.asking.insert(node.clone(), Asking::Landed(asks));
                lifted = true;
            }
        }
        let _ = self.apply(state);
        lifted
    }
}

/// Where a human reads the change a publication produced, when there is one.
///
/// A change request that is open, or that the host is holding, names its URL. A
/// `local-direct` merge has no change request at all, and a change request the
/// *host* merged is [`PublishOutcome::Merged`], which carries the commit rather
/// than the URL — see the `onevcs` proposal in `docs/contract-divergences.md`.
pub fn change_url(outcome: &PublishOutcome) -> Option<String> {
    match outcome {
        PublishOutcome::ChangeOpen(url)
        | PublishOutcome::ChangeDraft(url)
        | PublishOutcome::Queued(url) => Some(url.to_string()),
        _ => None,
    }
}

/// One read of an open session's own record: the sibling's own value, carrying
/// the worktree its dispatches work in and the base its branch is measured
/// against. Read once, because a second read at publication would ask a question
/// already answered on a path where the answer cannot have changed.
///
/// Its values are taken as that record states them. A record is `onevcs`'s own
/// state, written under its occupancy lease when it cut this session; a session's
/// **stream** is the untrusted one — a log any process holding the token appends
/// to — and what this crate reads off one is checked where it enters, by
/// [`DispatchSession::read_from`].
///
/// A read, not a claim: [`onevcs::session`] takes no lease, commits nothing, and
/// reclaims nothing, so asking where a session is working cannot disturb it —
/// unlike `adopt`, which commits whatever the worktree holds behind an
/// incomplete-step marker.
///
/// `None` when the record cannot be read, which leaves the caller to open a
/// session as it would have. The second and later dispatches of one lifecycle
/// node run in the worktree it names; they must **not** open a session of their
/// own, because `onevcs` cuts each session its own clone and opening a second one
/// reclaims the first's workspace. Both are recorded in
/// `docs/contract-divergences.md`.
pub fn working_session(token: &SessionToken) -> Option<Session> {
    onevcs::session(&providers(), token)
        .map(|record| record.session)
        .map_err(|error| {
            eprintln!(
                "onepipeline: cannot read session {}'s record: {error}",
                token.0
            );
            error
        })
        .ok()
}

/// Release a session's worktree and its occupancy lease.
///
/// Closing is best-effort on the failure path: a node that already failed must
/// not be reported as a different failure because its cleanup also failed.
pub fn session_close(token: &SessionToken) -> Result<Session> {
    onevcs::close_session(&providers(), token).map_err(refusal)
}

/// The commit one session's branch was last recorded at, when `onevcs` recorded
/// one.
///
/// Read off the session's **own stream**, and off nothing else, for the reason
/// [`change_opened_in`] gives: what a branch is at is the repository side's
/// business, and a second route to it here would be git regrown in the
/// composition layer. `commit-preserved` is the record that carries it —
/// `onevcs` emits one, holding the branch and the `sha`, wherever it commits a
/// session's worktree onto the branch that outlives it.
///
/// It is what a settlement names when it says the branch may carry finished
/// work: a branch alone sends a reader to a ref, and the commit sends them to the
/// tree.
///
/// `None` when nothing recorded one, when the record names no usable commit, and
/// equally when the stream cannot be read — the caller settles exactly as it
/// would have, because an unreadable record is not evidence of a branch nobody
/// committed to. A caller that has to tell those apart asks [`session_tip`],
/// which is what this reads and collapses.
pub fn branch_head_in(token: &SessionToken) -> Option<String> {
    match session_tip(token) {
        SessionTip::At(commit) => Some(commit.as_str().to_owned()),
        SessionTip::Unmoved | SessionTip::Unknown => None,
    }
}

/// A commit value this crate will carry.
///
/// A newtype and never a bare `String`, because [`usable`] is what stands
/// between a recorded sha and a settlement, a view, and an event payload — and a
/// variant holding a `String` puts that value back where anything could
/// construct one that never passed it. [`Commit::of`] is the only constructor
/// and it *is* that check, so a `Commit` in hand is a commit this crate renders
/// rather than one it is about to have to re-examine.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Commit(String);

impl Commit {
    /// The commit `sha` names, or `None` where this crate will not carry it.
    pub(crate) fn of(sha: &str) -> Option<Self> {
        usable(sha).map(Self)
    }

    /// The commit as a settlement, a view, and an event payload spell it.
    pub(crate) fn as_str(&self) -> &str {
        &self.0
    }
}

/// Where a session left the branch that outlives it, as far as its own stream
/// says.
///
/// Three cases and not an `Option`, because the absence [`branch_head_in`]
/// answers with is two facts that a caller comparing one attempt with the next
/// must not read as one: a session that committed nothing left the branch
/// exactly where it stood, and a stream nothing could read says nothing about
/// where the branch stands at all.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SessionTip {
    /// `commit-preserved` named this commit, so the branch stands at it.
    At(Commit),
    /// The stream was read and carries no commit this session made. `onevcs`
    /// commits a session's worktree only where it holds something to commit, so
    /// the session added nothing and the branch stands where it stood.
    Unmoved,
    /// Nothing is known: the stream could not be read, or the commit it named is
    /// not one this crate will carry. **Never** read as
    /// [`Unmoved`](Self::Unmoved) — an unreadable record is not evidence of a
    /// branch nobody committed to.
    Unknown,
}

/// Read that off the session's stream.
///
/// The stream is opened **and read** here rather than through [`events`], which
/// answers a read it could not perform with the empty batch a readable and empty
/// stream also gives: telling those two apart is the whole of what this adds.
/// Both refusals count — a stream that would not open, and a batch
/// [`EventStream::read`] turned down over one line it could not parse.
///
/// The commit is checked where it enters, by [`Commit::of`], for the reason
/// [`landing_commit_of`] is: a commit is rendered into a settlement, a view, and
/// an event payload, and one carrying a control character forges a row wherever
/// it lands. One that fails that check is [`Unknown`](SessionTip::Unknown) and
/// not [`Unmoved`](SessionTip::Unmoved) — a session that recorded a commit did
/// commit, whatever this crate can do with the value.
pub fn session_tip(token: &SessionToken) -> SessionTip {
    let Some(mut stream) = opened(token, None) else {
        return SessionTip::Unknown;
    };
    let batch = match stream.read() {
        Ok(batch) => batch,
        Err(error) => {
            eprintln!(
                "onepipeline: cannot read session {}'s events: {error}",
                token.0
            );
            return SessionTip::Unknown;
        }
    };
    let preserved = kind_of(onevcs::EventKind::CommitPreserved);
    // The last one wins: a session that committed twice was left at the second.
    let Some(envelope) = batch
        .into_iter()
        .rev()
        .map(relayed)
        .find(|envelope| envelope.kind == preserved)
    else {
        return SessionTip::Unmoved;
    };
    envelope
        .payload
        .get("sha")
        .and_then(|sha| sha.as_str())
        .and_then(Commit::of)
        .map_or(SessionTip::Unknown, SessionTip::At)
}

/// The change request one session's work reached, when it reached one.
///
/// Read off the session's **own stream**, which is where `onevcs` records a
/// change request as it opens one — `change-opened` carries the URL. That is the
/// only source this crate may ask: which host answers for a repository, and how
/// a change request is addressed on it, are that library's business, and a
/// second route to the same fact here would be host knowledge regrown in the
/// composition layer.
///
/// It matters because the engine is not the only thing that publishes from a
/// session: a dispatch that runs `onevcs publish` in its own final turn opens a
/// change request the engine's publication step never ran, and the record of it
/// is on this stream either way.
///
/// The URL is **validated where it enters**, through the parser `onevcs`
/// re-exports for exactly this — a session's stream is a file on disk that any
/// process holding the token appends to, so its payload is external input here
/// however trusted its usual writer is. A value that is not an absolute URL is
/// no change request a reviewer can open, and putting one on a settlement would
/// hand every reader of that node something to follow that goes nowhere.
///
/// `None` when nothing opened one, when the record names no readable URL, and
/// equally when the stream cannot be read — the caller settles exactly as it
/// would have, because an unreadable record is not evidence of a change nobody
/// opened.
pub fn change_opened_in(token: &SessionToken) -> Option<String> {
    let opened = kind_of(onevcs::EventKind::ChangeOpened);
    // The last one wins: a session that opened a change request, closed it, and
    // opened another names the one it ended with.
    events(token, None)
        .iter()
        .rev()
        .find(|envelope| envelope.kind == opened)
        .and_then(|envelope| envelope.payload.get("url"))
        .and_then(|url| url.as_str())
        // llmlint: ignore-block[changed_behavior_has_e2e] no invocation a user can type
        // reaches the refusal this line makes. The only producer of a `change-opened` is
        // `onevcs`, which builds the payload from its own `Url` — so a record naming
        // something that is not one can only come from a stream a hand-written line was
        // appended to, and writing that line would make this suite an oracle for a
        // payload nothing produces, which is the weakness `crates/testfakes` exists to
        // avoid. The two answers a producer *can* give are both driven end to end in
        // `tests/e2e/lifecycle.rs`: a change request that was opened, and a stream this
        // build cannot read a record off at all.
        .and_then(|url| onevcs::Url::parse(url.trim()).ok())
        // llmlint: ignore-end[changed_behavior_has_e2e]
        .map(|url| url.to_string())
}

/// The sessions holding one repository's workspaces, as `onevcs` reports them.
///
/// The same enumeration the launch interlock reads, asked of one repository:
/// what a node waiting to dispatch into an occupied workspace is waiting for.
///
/// An empty list is a workspace nothing holds. A repository this host cannot
/// answer for is the **error**, and the two are deliberately not the same value:
/// a view never reports an unmeasured thing as a measured nothing, and rendering
/// "nobody could be asked" as "nothing holds it" is what would tell a supervisor
/// to stop looking for what a node is waiting on. The caller renders the
/// refusal rather than deciding for itself what it meant.
pub fn holders_of(repo: &str) -> std::result::Result<Vec<onevcs::SessionHolder>, String> {
    onevcs::session_holders(repo).map_err(|error| {
        eprintln!("onepipeline: cannot read the session holders of {repo}: {error}");
        error.to_string()
    })
}

/// One session's stream, read from the start of what this reader has not seen.
///
/// `None` when the stream cannot be opened or a line of it cannot be read. That
/// is a publication with no *evidence* rather than a publication that did not
/// happen — the node's own settlement stands — but a silent gap in the merged
/// store is what makes a later reader think nothing happened, so it is said out
/// loud.
fn opened(token: &SessionToken, filter: Option<&EventFilter>) -> Option<EventStream> {
    let filter = match filter.map(sibling_filter).transpose() {
        Ok(filter) => filter.unwrap_or_default(),
        Err(error) => {
            eprintln!(
                "onepipeline: cannot follow session {}'s events: {error}",
                token.0
            );
            return None;
        }
    };
    match EventStream::open_filtered(token, filter) {
        Ok(stream) => Some(stream),
        Err(error) => {
            eprintln!(
                "onepipeline: cannot read session {}'s events: {error}",
                token.0
            );
            None
        }
    }
}

/// The next batch of a stream, relayed into this crate's envelope.
///
/// [`EventStream::read`] refuses a whole batch over one line it cannot parse, and
/// its cursor has already moved past that line — so a refusal here is events
/// lost, not events deferred. It is reported for that reason and the follow keeps
/// reading: the alternative is to stop relaying a live publication over one
/// record.
fn next_batch(stream: &mut EventStream, token: &SessionToken) -> Vec<Envelope> {
    match stream.read() {
        Ok(events) => events.into_iter().map(relayed).collect(),
        Err(error) => {
            eprintln!(
                "onepipeline: cannot read session {}'s events: {error}",
                token.0
            );
            Vec::new()
        }
    }
}

/// A session's own event stream, for relaying into the merged one.
pub fn events(token: &SessionToken, filter: Option<&EventFilter>) -> Vec<Envelope> {
    let Some(mut stream) = opened(token, filter) else {
        return Vec::new();
    };
    next_batch(&mut stream, token)
}

/// This crate's filter, as the sibling's own type.
///
/// The filter is handed to `onevcs` as a **value** rather than as a spec each
/// source parses again, which is what its filtered constructor takes — so this
/// is the one conversion, and it crosses at the wire shape the two types share
/// by contract rather than field by field, so a field one of them grows and the
/// other has not is a refusal here rather than a value silently dropped.
fn sibling_filter(filter: &EventFilter) -> Result<onevcs::EventFilter> {
    let document = serde_json::to_string(filter).map_err(|error| Error::Sibling {
        tool: "onevcs",
        message: format!("rendering the event filter: {error}"),
    })?;
    serde_json::from_str(&document).map_err(|error| Error::Sibling {
        tool: "onevcs",
        message: format!("`onevcs` refused the event filter: {error}"),
    })
}

/// One of the sibling's envelopes, as one of this crate's.
///
/// Field for field, out of `onevcs`'s own type: the merged stream keeps a
/// relayed envelope's producer `stream`, `seq`, `source`, kind, and phase
/// exactly as they were written, which is what lets a consumer detect loss per
/// stream and read a change's life without enumerating the kinds in it.
fn relayed(envelope: onevcs::Envelope) -> Envelope {
    Envelope {
        v: envelope.v,
        ts: envelope.ts,
        stream: envelope.stream,
        seq: envelope.seq,
        source: source_of(envelope.source),
        kind: kind_of(envelope.kind),
        phase: Some(phase_of(envelope.phase)),
        labels: labels_of(envelope.labels),
        payload: envelope.payload,
        artifacts: envelope
            .artifacts
            .into_iter()
            .map(|artifact| crate::event::ArtifactRef {
                id: crate::event::ArtifactId(artifact.id.0),
                kind: artifact.kind,
                bytes: artifact.bytes,
            })
            .collect(),
    }
}

/// Which part of a change's life a relayed envelope belongs to.
///
/// Arm by arm rather than through the sibling's serializer, unlike
/// [`kind_of`] beside it, and the difference is which side owns the vocabulary:
/// a *kind* is one of three libraries' and this crate relays all three, so an
/// enum here would reject a kind a newer sibling already emits. A phase is
/// `onevcs`'s alone and this crate's readers fold a closed set of them — so a
/// phase that library adds has to arrive here as a compile error rather than as
/// a string every reader silently declines.
fn phase_of(phase: onevcs::Phase) -> crate::event::Phase {
    match phase {
        onevcs::Phase::Development => crate::event::Phase::Development,
        onevcs::Phase::Integrate => crate::event::Phase::Integrate,
        onevcs::Phase::Review => crate::event::Phase::Review,
        onevcs::Phase::Release => crate::event::Phase::Release,
    }
}

/// The phase the sibling stamps on an event of one of its kinds.
///
/// For the envelopes this crate writes *beside* the sibling's own records about
/// one session: they carry the sibling's kind, so they carry the phase that
/// library puts that kind in rather than a second classification made here.
/// `None` for the one kind whose phase its producer decides — a push, which this
/// crate never records.
fn phase_of_kind(kind: onevcs::EventKind) -> Option<crate::event::Phase> {
    onevcs::Phase::of(kind).map(phase_of)
}

/// Which library produced a relayed envelope.
fn source_of(source: onevcs::Source) -> crate::event::Source {
    match source {
        onevcs::Source::Agentgraph => crate::event::Source::Agentgraph,
        onevcs::Source::Vcs => crate::event::Source::Vcs,
        onevcs::Source::Pipeline => crate::event::Source::Pipeline,
    }
}

/// A kind as the sibling spells it on the wire.
///
/// Through `onevcs`'s own serializer rather than an arm per variant: how a kind
/// is spelled is the sibling's to decide, and a table here would be a second
/// copy of a vocabulary this crate does not own — which is what
/// `src/AGENTS.md` forbids and what let a double script a kind the sibling has
/// never emitted.
fn kind_of(kind: onevcs::EventKind) -> crate::event::EventKind {
    let wire = serde_json::to_value(kind)
        .ok()
        .and_then(|value| value.as_str().map(str::to_string))
        // `EventKind` is a fieldless enum serialized as a string, so this arm is
        // unreachable today. It carries the variant's own name rather than
        // panicking, because losing a relay thread is a worse answer than an
        // unfamiliar kind in the store.
        .unwrap_or_else(|| format!("{kind:?}"));
    crate::event::EventKind(wire)
}

/// The labels a relayed envelope arrived with.
///
/// `onevcs` names one the merged envelope does not reserve — `member` — so it
/// rides in [`Labels::extra`](crate::event::Labels::extra), which is where the
/// contract puts anything a producer stamps beyond the reserved keys. Dropping
/// it would lose a producer's own attribution in the relay.
fn labels_of(labels: onevcs::Labels) -> crate::event::Labels {
    let mut extra = labels.extra;
    if let Some(member) = labels.member {
        extra.insert("member".to_owned(), serde_json::json!(member));
    }
    crate::event::Labels {
        run_id: labels.run_id,
        round: labels.round,
        node: labels.node,
        step: labels.step,
        persona: labels.persona,
        extra,
    }
}

/// How often a follow asks the stream for what has been appended since.
const FOLLOW_POLL: Duration = Duration::from_millis(20);

/// A session's own event stream, followed as `onevcs` writes it.
///
/// Read *once at settlement*, a lifecycle node's push, change request, check
/// polling, and merge are one opaque blocking call: every record appears at
/// once, when it is over — and that stretch is the longest wall-clock segment
/// the node has. This is the same stream read as it grows,
/// through [`EventStream`], which hands back only what has been appended since
/// the last read.
///
/// `None` when the session's stream cannot be opened at all. That is a
/// publication nobody is watching rather than a publication with no record, so
/// it is said out loud and the caller reads the stream once instead.
pub fn follow(
    token: &SessionToken,
    filter: Option<&EventFilter>,
    sink: Box<dyn Fn(Envelope) + Send>,
) -> Option<Follower> {
    let mut stream = opened(token, filter)?;

    let progress: Arc<Mutex<Watermarks>> = Arc::new(Mutex::new(Watermarks::default()));
    let reached = Arc::clone(&progress);
    let stop = Arc::new(AtomicBool::new(false));
    let stopping = Arc::clone(&stop);
    let followed = token.clone();
    let reader = std::thread::Builder::new()
        .name(format!("onevcs-{}-events", token.0))
        .spawn(move || loop {
            // Read *before* asking whether the session closed, so a record
            // written between the two is relayed on the next pass rather than
            // lost to a follow that stopped one read early.
            for envelope in next_batch(&mut stream, &followed) {
                if let Ok(mut reached) = reached.lock() {
                    reached.reached(&envelope);
                }
                sink(envelope);
            }
            if stopping.load(Ordering::SeqCst) || settled(&followed) {
                return;
            }
            std::thread::sleep(FOLLOW_POLL);
        });
    match reader {
        Ok(reader) => Some(Follower {
            reader: Some(reader),
            stop,
            progress,
        }),
        Err(error) => {
            eprintln!(
                "onepipeline: cannot follow session {}'s events: {error}",
                token.0
            );
            None
        }
    }
}

/// Whether a session has been released, which is what ends a follow.
///
/// A session whose record cannot be read is treated as settled: a follow that
/// kept reading a stream nobody will ever close is a thread this process would
/// never collect.
fn settled(session: &SessionToken) -> bool {
    onevcs::session(&providers(), session)
        .map(|record| record.lifecycle == Lifecycle::Closed)
        .unwrap_or(true)
}

/// How far a reader got into each stream it relayed from.
///
/// The `seq` rather than a count, because that is what says which records are
/// still unread: `onevcs` numbers a stream monotonically from one and resumes
/// the series in the next process that writes to it, so the highest `seq`
/// relayed is exactly the point a second reader continues from.
///
/// Per **stream**, and that is load-bearing rather than tidy. Reading one
/// session hands back records of more than one stream: the session's own, and —
/// since `onevcs` joined the identity's release record to the session whose
/// landing it names — that repository's releases, numbered in a series of their
/// own over every session in it. One high-water mark over both would let the
/// higher series hide the lower one's next record, which is a relayed record
/// lost, and would make the per-stream `seq` gaps a consumer detects loss
/// through describe two producers at once.
#[derive(Debug, Default, Clone)]
pub struct Watermarks {
    /// The highest `seq` relayed, per producing stream.
    reached: BTreeMap<String, u64>,
}

impl Watermarks {
    /// The marks a run's own store already stands at.
    ///
    /// Folded from what was relayed rather than tracked beside it, so what stops
    /// a record being relayed twice is the record.
    pub fn of_relayed(envelopes: &[Envelope]) -> Self {
        let mut marks = Self::default();
        for envelope in envelopes {
            marks.reached(envelope);
        }
        marks
    }

    /// Record that one envelope was relayed.
    pub fn reached(&mut self, envelope: &Envelope) {
        self.reached
            .entry(envelope.stream.clone())
            .and_modify(|seq| *seq = (*seq).max(envelope.seq))
            .or_insert(envelope.seq);
    }

    /// Whether one envelope is past the mark its own stream stands at.
    ///
    /// A stream nothing has been relayed from is entirely unread, so its first
    /// record passes: not a bare `0`, which a producer numbering from zero would
    /// be indistinguishable from and whose first record a reader would skip.
    #[must_use]
    pub fn beyond(&self, envelope: &Envelope) -> bool {
        self.reached
            .get(&envelope.stream)
            .is_none_or(|seq| envelope.seq > *seq)
    }
}

/// Whether one relayed envelope is the record a session's stream ends with.
///
/// Spelled through `onevcs`'s own serializer rather than as a literal, for
/// [`kind_of`]'s reason: the vocabulary is the sibling's, and a second copy of it
/// here is one that goes stale the release the sibling renames a kind — and this
/// is the kind a settlement is *checked* against, so a stale copy would report
/// every session as having ended without its terminator.
#[must_use]
pub fn is_terminator(envelope: &Envelope) -> bool {
    envelope.kind == kind_of(onevcs::EventKind::SessionClosed)
}

/// One session's stream, being followed.
///
/// Dropping one ends the follow. Not every caller reaches a settlement — a node
/// whose next step needs a person holds its session *open* for them, and returns
/// — and a follow left behind there is a thread nothing would ever collect,
/// reading a stream nobody is waiting for.
#[derive(Debug)]
pub struct Follower {
    /// Taken by [`finish`](Follower::finish), so a drop after one has nothing
    /// left to wait on.
    reader: Option<std::thread::JoinHandle<()>>,
    /// Set to end the follow without waiting for the session to close.
    stop: Arc<AtomicBool>,
    progress: Arc<Mutex<Watermarks>>,
}

impl Drop for Follower {
    fn drop(&mut self) {
        self.stop.store(true, Ordering::SeqCst);
        if let Some(reader) = self.reader.take() {
            let _ = reader.join();
        }
    }
}

impl Follower {
    /// Stop following, and say how far into the stream it got.
    ///
    /// Called *before* `session close`, and the flag rather than the close is
    /// what ends it. `onevcs` refuses to release a session any live process is
    /// working inside its run root, and asking whether the session closed is a
    /// library call that shells out to `git` in that session's clone — so a
    /// follow still polling at the moment of the close is, a fraction of the
    /// time, exactly such a process, and the close fails. Waiting here is what
    /// makes it not one: the reader relays one last batch and the join is what
    /// says its last `git` child has been reaped.
    ///
    /// The answer is a **floor, never a promise that the rest is not there**.
    /// Everything the close itself writes — the `session-closed` record among it
    /// — is appended after this returns, so the caller reads the stream once
    /// more from this point instead. Treating a clean end as "everything was
    /// relayed" is what dropped that record out of the merged store.
    ///
    /// Empty when it relayed nothing at all, which is the whole stream still to
    /// read rather than a stream that held nothing.
    pub fn finish(mut self) -> Watermarks {
        self.stop.store(true, Ordering::SeqCst);
        if let Some(reader) = self.reader.take() {
            let _ = reader.join();
        }
        let mut reached = self
            .progress
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        std::mem::take(&mut *reached)
    }
}

/// The envelope that records a session opening, for the merged stream.
///
/// It carries `Source::Vcs` because `onevcs` is what opened the session: the
/// merge is an interleaving of three streams, and a lifecycle node's branch
/// belongs to that one.
pub fn session_opened_event(session: &Session, labels: &crate::event::Labels) -> Envelope {
    Envelope {
        v: crate::event::ENVELOPE_VERSION,
        ts: crate::sys::now_rfc3339(),
        stream: format!("onevcs-{}", session.token.0),
        seq: 0,
        source: crate::event::Source::Vcs,
        // The sibling's own spelling, through its own serializer: this envelope
        // stands beside the ones `onevcs` writes for the same session, and a
        // reader that folds one of them has to fold both.
        kind: kind_of(onevcs::EventKind::SessionOpened),
        // And the phase that library puts that kind in, for the same reason.
        phase: phase_of_kind(onevcs::EventKind::SessionOpened),
        labels: labels.clone(),
        payload: crate::journal::payload(&[
            ("token", serde_json::json!(session.token.0)),
            ("branch", serde_json::json!(session.branch)),
            ("base", serde_json::json!(session.base)),
            ("worktree", serde_json::json!(session.worktree)),
        ]),
        artifacts: Vec::new(),
    }
}

/// The envelope that records a publication, for the merged stream.
///
/// Every field is read off the [`Publication`] rather than off the caller: the
/// branch that carried the change and the policy it landed under are the
/// sibling's answer, and a second copy assembled here could disagree with it.
pub fn published_event(published: &Publication, labels: &crate::event::Labels) -> Envelope {
    Envelope {
        v: crate::event::ENVELOPE_VERSION,
        ts: crate::sys::now_rfc3339(),
        stream: format!("onevcs-{}", published.branch),
        seq: 1,
        source: crate::event::Source::Vcs,
        kind: crate::event::EventKind("published".into()),
        // `published` is this crate's own kind rather than one of the sibling's,
        // so there is no producer's classification to carry: a phase here would
        // be this crate inventing one.
        phase: None,
        labels: labels.clone(),
        payload: crate::journal::payload(&[
            ("branch", serde_json::json!(published.branch)),
            ("policy", serde_json::json!(published.policy)),
            ("outcome", serde_json::json!(outcome_of(&published.outcome))),
            ("url", serde_json::json!(change_url(&published.outcome))),
            (
                "landing",
                serde_json::json!(landing_of(&published.outcome).map(crate::graph::Landing::as_str)),
            ),
        ]),
        artifacts: Vec::new(),
    }
}

/// Where one node's dispatch is working: the session `onevcs` opened for it.
///
/// The token and the branch together, because either alone leaves work
/// unreachable — the branch says where the commits are and the token says which
/// worktree and clone still hold them — and both are private, so a value of this
/// type is one [`read_from`](Self::read_from) has already checked. There is no
/// other way to make one.
///
/// A checkpoint of the fold carries this value, and it is read back through
/// [`read_from`](Self::read_from)'s own checks rather than straight into the two
/// private fields: a checkpoint is a file, so the values in it arrive from
/// outside exactly as a stream's record does, and a token or a branch this crate
/// would have refused off a stream is one it refuses off a document.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DispatchSession {
    token: SessionToken,
    branch: BranchName,
}

/// A session as a checkpoint carries it, before the checks that make it one.
///
/// **One type for both directions**, so the wire is declared once: a second
/// declaration is a shape that drifts, and a document the writer produced that the
/// reader refuses would take a run's sessions away on the quiet.
#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct SessionAsWritten {
    token: String,
    branch: String,
}

impl Serialize for DispatchSession {
    fn serialize<S: serde::Serializer>(&self, writer: S) -> std::result::Result<S::Ok, S::Error> {
        SessionAsWritten {
            token: self.token.0.clone(),
            branch: self.branch.0.clone(),
        }
        .serialize(writer)
    }
}

impl<'de> Deserialize<'de> for DispatchSession {
    fn deserialize<D: serde::Deserializer<'de>>(reader: D) -> std::result::Result<Self, D::Error> {
        let written = SessionAsWritten::deserialize(reader)?;
        let token = token_of(&written.token).ok_or_else(|| {
            serde::de::Error::custom(format!("'{}' is no session handle", written.token))
        })?;
        let branch = BranchName::checked(&written.branch).ok_or_else(|| {
            serde::de::Error::custom(format!("'{}' is no branch name", written.branch))
        })?;
        Ok(Self { token, branch })
    }
}

/// A branch a stream's record named, and [`usable`] accepted.
///
/// Deliberately **not** a claim that git would accept the name: that parser is
/// git's, and asking it would mean this crate running git, which no path of it
/// ever has.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BranchName(String);

impl BranchName {
    /// A name off a stream's record, where it is one this crate can act on.
    pub fn checked(value: &str) -> Option<Self> {
        usable(value).map(Self)
    }

    /// The name itself, for a caller that needs the string.
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl std::fmt::Display for BranchName {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.0)
    }
}

impl DispatchSession {
    /// Read a session out of a relayed `session-opened`, where it is one this
    /// crate can act on.
    ///
    /// **The trust boundary for a session record**, which the envelope module
    /// leaves to the reader seam that parses a stream. Three questions, and only
    /// the last two are this crate's:
    ///
    /// * **Which fields** is [`Session`]'s, so the payload is parsed through the
    ///   producer's own declaration rather than by key. The fields it does not
    ///   declare are left alone — `onevcs`'s own record of the same session
    ///   carries seven more — while a field this crate acts on is required by the
    ///   type, so a missing or misspelled `branch` drops the record.
    /// * **Whether the values are usable**: see [`usable`].
    /// * **Whether the record is about the session that carried it**, which no
    ///   value inside it can answer. A stream is a log any process holding the
    ///   token appends to, so a record on one naming a *different* session is a
    ///   pointer at somebody else's work, arriving where nobody can check it.
    pub fn read_from(envelope: &Envelope) -> Option<Self> {
        let session: Session =
            serde_json::from_value(serde_json::Value::Object(envelope.payload.clone())).ok()?;
        let token = token_of(&session.token.0)?;
        if !wrote(&envelope.stream, &token) {
            return None;
        }
        Some(Self {
            token,
            branch: BranchName::checked(&session.branch)?,
        })
    }

    /// The handle `onevcs` addresses the session by.
    pub fn token(&self) -> &SessionToken {
        &self.token
    }

    /// The branch its worktree has checked out, and therefore the branch the
    /// dispatch's commits are on.
    pub fn branch(&self) -> &BranchName {
        &self.branch
    }
}

/// Whether a stream is the one a session writes.
///
/// Two spellings, because two producers write a session's opening: `onevcs`
/// streams a session under its own token, and this crate writes its copy for the
/// merged store under that token namespaced by the sibling it came from — see
/// [`session_opened_event`]. Both are that session's, and neither is another's.
fn wrote(stream: &str, token: &SessionToken) -> bool {
    stream == token.0 || stream == format!("onevcs-{}", token.0)
}

/// One session token off a stream's record, where it is one this crate can hand
/// back.
///
/// [`usable`], and one rule more that is about what a token is *for*: it
/// addresses a session, and both libraries name a file by it, so a value
/// carrying a path separator — or one that is a directory hop — is no handle
/// however well it reads. What a token may otherwise be is `onevcs`'s to say,
/// and it says so by refusing one it does not know.
fn token_of(value: &str) -> Option<SessionToken> {
    let value = usable(value)?;
    if value.contains(['/', '\\']) || value.trim_matches('.').is_empty() {
        return None;
    }
    Some(SessionToken(value))
}

/// One payload text field, where it is whole and names something.
///
/// Three checks, each about what this crate does with the value. It is handed
/// back to `onevcs` as a session handle and a branch name, and it is rendered
/// into `results` and onto an operator's terminal:
///
/// * **Not empty.** A branch nobody can name is not a pointer at work, and
///   recording one would put an empty name where a manager reads for one.
/// * **Whole.** A producer cuts a payload text field at
///   [`MAX_PAYLOAD_TEXT_BYTES`](crate::event::MAX_PAYLOAD_TEXT_BYTES) and says
///   so beside it, so a value that long may be a *prefix* — and a truncated
///   branch name addresses a branch that does not exist. Checked per value
///   rather than off the payload's own marker, which says only that something in
///   the record was cut.
/// * **One word, on one line.** Neither a session token nor a branch name
///   carries whitespace or a control character, and both are rendered into
///   line-oriented views where a value carrying a newline forges a line — a
///   record that appears to be about a node nobody dispatched.
pub(crate) fn usable(value: &str) -> Option<String> {
    if value.is_empty() || value.len() >= crate::event::MAX_PAYLOAD_TEXT_BYTES {
        return None;
    }
    if value.chars().any(|c| c.is_whitespace() || c.is_control()) {
        return None;
    }
    Some(value.to_owned())
}

/// Whether an envelope is a session opening, in `onevcs`'s own vocabulary.
///
/// Asked through that library's enum rather than against a string of this
/// crate's, for the reason [`kind_of`] gives: how a kind is spelled is the
/// sibling's to decide, and a literal here would keep matching after a rename
/// and silently stop folding anything.
pub fn is_session_opened(kind: &crate::event::EventKind) -> bool {
    *kind == session_opened_kind()
}

/// How `onevcs` spells a session opening, for a caller that has to *write* one
/// rather than recognise it. One derivation with [`is_session_opened`], so a
/// test that builds the record and the fold that reads it cannot disagree.
pub(crate) fn session_opened_kind() -> crate::event::EventKind {
    kind_of(onevcs::EventKind::SessionOpened)
}

/// Whether an envelope is a publication reaching its base, in `onevcs`'s own
/// vocabulary.
///
/// Asked through that library's enum for the reason
/// [`is_session_opened`] is: how a kind is spelled is the sibling's to decide.
pub fn is_merge_completed(kind: &crate::event::EventKind) -> bool {
    *kind == kind_of(onevcs::EventKind::MergeCompleted)
}

/// The commit one publication reached its base at, off the sibling's own record.
///
/// The whole boundary read in one named place rather than a validator handed back
/// for a caller to apply: a landing commit is rendered into a table, into a task,
/// and into an event payload, so a value carrying whitespace or a control
/// character forges a row wherever it lands. `None` for an envelope that is not
/// one, and for a `sha` that could not be rendered — the same check [`usable`]
/// makes of a session token and a branch name, and for the same reason.
pub fn landing_commit_of(event: &crate::event::Envelope) -> Option<String> {
    if event.source != crate::event::Source::Vcs || !is_merge_completed(&event.kind) {
        return None;
    }
    usable(event.payload.get("sha")?.as_str()?)
}

/// The session a lifecycle node asks for.
pub fn request_for(node: &crate::plan::Node) -> Option<SessionRequest> {
    Some(SessionRequest {
        repo: node.repo.clone()?,
        // A `resume` names the branch its continuation lives on, and the
        // reconciler has already pinned `branch` to it, so there is one answer
        // here rather than two.
        branch: node.branch.clone(),
        base: node.base_branch.clone(),
        execution_checkout: node.execution_checkout.clone(),
    })
}

/// Serialises the tests that point `ONEVCS_HOME` at a scratch state root.
///
/// That variable is process-global and this crate's unit tests share one process,
/// so two modules pointing it at two roots at once read each other's state. Each
/// such test holds this for its duration instead; the guard is the whole of the
/// protocol, which is why it is a value the caller binds rather than a pair of
/// calls it could get half right.
#[cfg(test)]
pub(crate) fn scratch_home_held() -> std::sync::MutexGuard<'static, ()> {
    static IN_USE: Mutex<()> = Mutex::new(());
    IN_USE
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner())
}

#[cfg(test)]
mod tests {
    use std::collections::BTreeSet;

    use super::*;
    use crate::plan::Node;

    /// Every `FailureKind` the sibling distinguishes.
    ///
    /// Written out because the sibling's enum offers no enumeration of itself.
    /// It does not stand alone: [`failure_of`] matches arm by arm, so a variant
    /// added there fails *that* to compile, and this list is what makes the same
    /// addition fail the document's gate below rather than pass it silently.
    const EVERY_KIND: &[onevcs::FailureKind] = &[
        onevcs::FailureKind::Gate,
        onevcs::FailureKind::Invalid,
        onevcs::FailureKind::SyncConflict,
        onevcs::FailureKind::NotImplemented,
        onevcs::FailureKind::ChecksFailed,
        onevcs::FailureKind::ChecksUnsettled,
        onevcs::FailureKind::PushRejected,
        onevcs::FailureKind::PushedUnverified,
    ];

    /// Every failure a further attempt can answer, as the type spells them.
    ///
    /// Written out for the same reason [`EVERY_KIND`] is, and standing alone no
    /// more than it does: [`Preserving::outcome`] matches arm by arm, so a
    /// variant added there fails *that* to compile, and this list is what makes
    /// the same addition fail the document's gate below rather than pass it
    /// silently.
    const EVERY_PRESERVING: &[Preserving] = &[
        Preserving::ChecksFailed,
        Preserving::ChecksUnsettled,
        Preserving::PushRejected,
        Preserving::SyncConflict,
    ];

    /// The words this crate settles a failed publication on and the words the
    /// contract names are one vocabulary.
    ///
    /// The document is the approved surface and the match above is what a run
    /// actually does; only one of them is compiled, so the other needs a gate —
    /// the same one `crate::lifecycle`'s drafting endings have, for the same
    /// reason. A word in the code the document does not name is a settlement
    /// nobody was promised, and a word in the document nothing produces is a
    /// promise nobody keeps.
    #[test]
    fn the_publication_failure_words_and_the_contract_are_one_vocabulary() {
        let contract = std::fs::read_to_string(
            std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("docs/contract.md"),
        )
        .expect("the contract ships");
        for kind in EVERY_KIND {
            let word = failure_of(*kind).outcome();
            assert!(
                contract.contains(&format!("`{word}`")),
                "docs/contract.md does not name the `{word}` outcome this crate settles on"
            );
        }
        // And the other direction. The contract lists them in one clause, so the
        // clause is read and its backticked tokens compared with the set the
        // code produces rather than the whole document searched.
        let clause = contract
            .split_once("under a word of its own:")
            .expect("the contract lists the words a failed publication settles on")
            .1
            .split_once("is the **residual**")
            .expect("the clause ends where the residual is named")
            .0;
        let listed: BTreeSet<&str> = clause.split('`').skip(1).step_by(2).collect();
        let vocabulary: BTreeSet<&str> = EVERY_PRESERVING
            .iter()
            .map(|preserving| preserving.outcome())
            .chain([Failure::UNREAD, Failure::RESIDUAL])
            .collect();
        let produced: BTreeSet<&str> = EVERY_KIND
            .iter()
            .map(|kind| failure_of(*kind).outcome())
            .collect();
        // The routing table and the vocabulary are the same set, in both
        // directions: a `Preserving` variant no `FailureKind` reaches is a
        // settlement nothing can produce, and a word `failure_of` produces from
        // outside the vocabulary is one this gate would otherwise never see.
        assert_eq!(
            produced, vocabulary,
            "the words `failure_of` settles on are not the vocabulary `Preserving` closes"
        );
        assert_eq!(
            listed, vocabulary,
            "the contract's publication-failure words are not the ones this crate settles on"
        );
    }

    /// The README summarises the same vocabulary, so it is gated the same way.
    ///
    /// It is a third copy — the match above, the contract, and the prose an
    /// operator actually reads — and the first two already hold each other. Left
    /// ungated the README is the one that goes quietly stale: nothing compiles
    /// it, and an operator meeting a settlement it does not list has no way to
    /// know which of the two is behind.
    ///
    /// Two facts, because the README states two: that every word it carries is
    /// one this crate settles on, and **which of them are re-dispatched**. The
    /// second is the one an operator plans around — a word that quietly moved
    /// across that line would have them waiting for a retry that never comes —
    /// so the clause naming those is compared as a set with what `Preserving`
    /// closes rather than searched one word at a time.
    ///
    /// Both ends of that clause are anchored on prose that does not count them,
    /// so widening the vocabulary is a README edit rather than a README edit and
    /// a test edit.
    #[test]
    fn the_readmes_publication_failure_summary_is_the_vocabulary_this_crate_settles_on() {
        let raw = std::fs::read_to_string(
            std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("README.md"),
        )
        .expect("the README ships");
        // Wrapped prose, so match on its words rather than its line breaks.
        let readme = raw.split_whitespace().collect::<Vec<_>>().join(" ");
        for kind in EVERY_KIND {
            let word = failure_of(*kind).outcome();
            assert!(
                readme.contains(&format!("`{word}`")),
                "the README does not name the `{word}` outcome this crate settles on"
            );
        }
        let clause = readme
            .split_once("settle under a word of their own")
            .expect("the README names the failures that settle under a word of their own")
            .1
            .split_once("The first four leave the rejected tree")
            .expect("that clause ends where the README says what those failures share")
            .0;
        let listed: BTreeSet<&str> = clause.split('`').skip(1).step_by(2).collect();
        let routed: BTreeSet<&str> = EVERY_PRESERVING
            .iter()
            .map(|preserving| preserving.outcome())
            .chain(std::iter::once(Failure::UNREAD))
            .collect();
        assert_eq!(
            listed, routed,
            "the README's named failures are not the ones this crate gives a word of its own"
        );
        // And that the one whose work is on the origin is recovered by reading
        // the host rather than by dispatching the agent, which is the whole of
        // what tells it from the four above it.
        assert!(
            readme.contains(&format!("`{}` is answered differently", Failure::UNREAD)),
            "the README does not say that `{}` is recovered by a re-read",
            Failure::UNREAD
        );
        assert!(
            readme.contains(crate::engine::MERGE_PATH_READS_ENV),
            "the README does not name what bounds that re-read"
        );
        assert!(
            readme.contains(&format!("Everything else settles `{}`", Failure::RESIDUAL)),
            "the README does not name `{}` as what everything else settles on",
            Failure::RESIDUAL
        );
    }

    /// Which kinds a further attempt can answer, said kind by kind — and which
    /// one a further **read** answers instead.
    ///
    /// That a word of its own *is* a routing decision is no longer assertable —
    /// [`Failure`] makes the two one value, so the inconsistent state has no
    /// spelling. What still needs saying is which side of each line a kind falls
    /// on, because that is a judgement about the failure rather than about the
    /// type: a refused request and an unimplemented seam answer the same way
    /// however many times they are asked, a gate that ran on the tree as it
    /// stands is not the host's report on a change request, and a push that
    /// reached the remote has nothing about its tree left to fix.
    #[test]
    fn each_kind_is_on_the_side_of_the_line_the_contract_puts_it() {
        let terminal = [
            onevcs::FailureKind::Invalid,
            onevcs::FailureKind::NotImplemented,
            onevcs::FailureKind::Gate,
        ];
        for kind in terminal {
            assert_eq!(
                failure_of(kind),
                Failure::Terminal,
                "{kind:?} is retried, and asking again would reproduce the diagnosis"
            );
        }
        // The one kind whose work is already on the origin. It is deliberately
        // not preserving: sending a worker back to that tree pays for a fresh
        // clone and a fresh gate to re-push what the remote already has.
        assert_eq!(
            failure_of(onevcs::FailureKind::PushedUnverified),
            Failure::Unread,
            "a push that reached the remote is answered by re-dispatching the agent"
        );
        let preserving: BTreeSet<&str> = EVERY_KIND
            .iter()
            .filter(|kind| !terminal.contains(kind))
            .filter(|kind| **kind != onevcs::FailureKind::PushedUnverified)
            .map(|kind| match failure_of(*kind) {
                Failure::Preserving(preserving) => preserving.outcome(),
                Failure::Unread | Failure::Terminal => {
                    panic!("{kind:?} is not the tree being rejected, so nothing re-dispatches it")
                }
            })
            .collect();
        assert_eq!(
            preserving,
            BTreeSet::from([
                "checks-failed",
                "checks-unsettled",
                "push-rejected",
                "sync-conflict"
            ]),
            "the failures a further attempt can answer are not the four the contract names"
        );
        // And the residual is one word for all of them, which is what keeps it a
        // residual rather than a fifth name.
        assert_eq!(Failure::Terminal.outcome(), Failure::RESIDUAL);
        // The word an unread merge path settles on is the one it always was:
        // what changed is how the node is recovered, not what a reader reads.
        assert_eq!(Failure::Unread.outcome(), "pushed-unverified");
    }

    /// The payload of a session opening, as one of the two producers writes it.
    ///
    /// This crate's own is built by [`session_opened_event`] rather than spelled
    /// out, so the fixture cannot say a shape the producer does not.
    fn ours(token: &str, branch: &str) -> Envelope {
        session_opened_event(
            &Session {
                token: SessionToken(token.to_owned()),
                worktree: std::path::PathBuf::from("/tmp/worktree"),
                branch: branch.to_owned(),
                base: "main".to_owned(),
            },
            &crate::event::Labels::default(),
        )
    }

    /// What a session record is read for, and what is refused instead of read.
    ///
    /// The refusals are the point. This record is the only pointer a manager has
    /// at work an adoption left behind, so a value that is not whole, or not a
    /// name, sends them looking for a branch that does not exist — and one
    /// carrying a newline forges a line in a view that is read line by line.
    #[test]
    fn a_session_record_is_read_only_where_every_value_it_names_is_usable() {
        let read = DispatchSession::read_from(&ours("s-abc", "onevcs/s-abc"))
            .expect("a whole session record is read");
        assert_eq!(read.token(), &SessionToken("s-abc".into()));
        assert_eq!(read.branch().as_str(), "onevcs/s-abc");

        // The sibling's own record of the same session: the four fields this
        // crate reads, plus everything else `onevcs` knows about it. Those are
        // kept out of the way rather than refused — it is the producer's account
        // of the session, and a reader that rejected what it had not heard of
        // would drop the record this fold exists to read.
        let mut theirs = ours("s-abc", "onevcs/s-abc");
        // And on the stream `onevcs` writes it to, which is the session's own
        // token rather than this crate's namespaced spelling of it.
        theirs.stream = "s-abc".to_owned();
        for (key, value) in [
            ("identity", serde_json::json!("github.com/owner/service")),
            ("clone", serde_json::json!("/tmp/runs/s-abc/clone")),
            ("execution_checkout", serde_json::json!("/tmp/service")),
            ("publication_checkout", serde_json::json!("/tmp/service")),
            ("reused", serde_json::json!(true)),
        ] {
            theirs.payload.insert(key.to_owned(), value);
        }
        assert_eq!(
            DispatchSession::read_from(&theirs).as_ref(),
            Some(&read),
            "the sibling's own record of a session it opened was not read"
        );

        let without = |key: &str| {
            let mut event = ours("s-abc", "onevcs/s-abc");
            event.payload.remove(key);
            event
        };
        // A record about a session other than the one whose log carried it: the
        // stream is whose log it is, and a pointer at somebody else's work
        // arriving here is one nobody can check.
        let mut elsewhere = ours("s-elsewhere", "onevcs/s-elsewhere");
        elsewhere.stream = "onevcs-s-abc".to_owned();
        for (why, event) in [
            ("a record naming no branch at all", without("branch")),
            ("a record naming no token at all", without("token")),
            ("a record about another session entirely", elsewhere),
        ] {
            assert_eq!(
                DispatchSession::read_from(&event),
                None,
                "{why} was read as a session a manager can be sent to"
            );
        }
        for (why, value) in unusable() {
            assert_eq!(
                DispatchSession::read_from(&ours("s-abc", &value)),
                None,
                "a branch that is {why} was read as one a manager can be sent to"
            );
        }
        // A token is everything a branch is, and a plain name besides: both
        // libraries name a file by it, and a branch — which may hold a `/` —
        // this crate only ever hands back.
        let hops = [
            ("a directory hop", "..".to_owned()),
            ("carrying a path separator", "onevcs/../x".to_owned()),
        ];
        for (why, value) in unusable().into_iter().chain(hops) {
            let mut record = ours(&value, "onevcs/s-abc");
            record.stream = value.clone();
            assert_eq!(
                DispatchSession::read_from(&record),
                None,
                "a token that is {why} was read as one a session answers to"
            );
        }
    }

    /// Every value neither a branch nor a token may be, and why.
    ///
    /// One table, because one check answers for both and for the base a
    /// publication names beside them: a value that is not whole, or not a name,
    /// is the same fault wherever this crate reads one.
    fn unusable() -> Vec<(&'static str, String)> {
        vec![
            ("empty", String::new()),
            (
                "a line of its own",
                "onevcs/x\n  audit    running".to_owned(),
            ),
            ("carrying a space", "onevcs/ x".to_owned()),
            ("carrying a control character", "onevcs/x\u{7}".to_owned()),
            // A value that is *cut* is the one that reads as a name and is not
            // one: `onevcs` bounds a payload text field and says so beside it, so
            // a name this long may be a prefix of what the work is actually on.
            (
                "as long as the bound a producer cuts text at",
                "b".repeat(crate::event::MAX_PAYLOAD_TEXT_BYTES),
            ),
        ]
    }

    #[test]
    fn a_lifecycle_node_asks_for_the_session_its_fields_describe() {
        let node = Node {
            id: "service".into(),
            repo: Some("owner/repo".into()),
            branch: Some("feature".into()),
            base_branch: Some("main".into()),
            execution_checkout: Some("primary".into()),
            persona: Some("engineer".into()),
            task: Some("## What\nship".into()),
            ..Node::default()
        };
        let request = request_for(&node).expect("a lifecycle node asks for a session");
        assert_eq!(request.repo, "owner/repo");
        assert_eq!(request.branch.as_deref(), Some("feature"));
        assert_eq!(request.base.as_deref(), Some("main"));
        assert_eq!(request.execution_checkout.as_deref(), Some("primary"));
    }

    #[test]
    fn a_direct_agent_node_asks_for_no_session() {
        let node = Node {
            id: "build".into(),
            persona: Some("engineer".into()),
            task: Some("## What\ndo it".into()),
            ..Node::default()
        };
        assert!(request_for(&node).is_none());
    }

    #[test]
    fn every_ending_a_publication_has_settles_the_node_under_its_own_name() {
        let sha = onevcs::Sha("abc".into());
        let url: onevcs::Url = "https://example.invalid/pull/7".parse().expect("a URL");
        assert_eq!(outcome_of(&PublishOutcome::Merged(sha)), "merged");
        assert_eq!(
            outcome_of(&PublishOutcome::ChangeOpen(url.clone())),
            "change-open"
        );
        assert_eq!(
            outcome_of(&PublishOutcome::ChangeDraft(url.clone())),
            "change-draft"
        );
        assert_eq!(outcome_of(&PublishOutcome::Queued(url)), "queued");
        assert_eq!(outcome_of(&PublishOutcome::NothingToPublish), "no-changes");
        // A failed publication settles under the word its **kind** earns, which
        // is what a caller branches on. Every kind, so the residual is proven to
        // be a residual rather than the only answer.
        let failed = |kind| {
            outcome_of(&PublishOutcome::Failed {
                kind,
                reason: "the publication said no".into(),
                retained: None,
            })
        };
        assert_eq!(failed(onevcs::FailureKind::Gate), "publication-failed");
        assert_eq!(failed(onevcs::FailureKind::Invalid), "publication-failed");
        assert_eq!(
            failed(onevcs::FailureKind::NotImplemented),
            "publication-failed"
        );
        assert_eq!(failed(onevcs::FailureKind::ChecksFailed), "checks-failed");
        assert_eq!(
            failed(onevcs::FailureKind::ChecksUnsettled),
            "checks-unsettled"
        );
        assert_eq!(failed(onevcs::FailureKind::PushRejected), "push-rejected");
        assert_eq!(failed(onevcs::FailureKind::SyncConflict), "sync-conflict");
        assert_eq!(
            failed(onevcs::FailureKind::PushedUnverified),
            "pushed-unverified"
        );
    }

    /// Which endings this crate is willing to call landed.
    ///
    /// Exactly one: the case `onevcs` produces holding the commit the change
    /// reached its base at. The two that carry a change-request URL are the ones
    /// a policy asking for an immediate merge produces when the host has not
    /// merged, so a derivation that read the policy — or that read "the
    /// publication succeeded" — would call both of them landed. That is the
    /// false report this whole distinction exists to remove, so it is stated
    /// case by case here rather than left to a catch-all.
    #[test]
    fn only_a_change_observed_on_its_base_is_called_landed() {
        use crate::graph::Landing;
        let url: onevcs::Url = "https://example.invalid/pull/7".parse().expect("a URL");
        assert_eq!(
            landing_of(&PublishOutcome::Merged(onevcs::Sha("abc".into()))),
            Some(Landing::Landed)
        );
        // A change request somebody has to merge, one nobody may merge yet, and
        // one the host is holding behind checks: each is a change that has not
        // reached its base.
        assert_eq!(
            landing_of(&PublishOutcome::ChangeOpen(url.clone())),
            Some(Landing::Unlanded)
        );
        assert_eq!(
            landing_of(&PublishOutcome::ChangeDraft(url.clone())),
            Some(Landing::Unlanded)
        );
        assert_eq!(
            landing_of(&PublishOutcome::Queued(url)),
            Some(Landing::Unlanded)
        );
        // Neither of these has a change of its own to land, and neither is
        // reported as though it might: a branch its base already carried settles
        // `no-changes`, and a publication that failed settles `failed`.
        assert_eq!(landing_of(&PublishOutcome::NothingToPublish), None);
        assert_eq!(
            landing_of(&PublishOutcome::Failed {
                kind: onevcs::FailureKind::PushRejected,
                reason: "the merge path refused the publishing push".into(),
                retained: None,
            }),
            None
        );
    }

    #[test]
    fn a_change_request_is_where_a_human_reads_it_and_a_local_merge_names_none() {
        let url: onevcs::Url = "https://example.invalid/pull/7".parse().expect("a URL");
        assert_eq!(
            change_url(&PublishOutcome::ChangeOpen(url.clone())).as_deref(),
            Some("https://example.invalid/pull/7")
        );
        assert_eq!(
            change_url(&PublishOutcome::Queued(url)).as_deref(),
            Some("https://example.invalid/pull/7")
        );
        assert_eq!(
            change_url(&PublishOutcome::Merged(onevcs::Sha("abc".into()))),
            None
        );
        assert_eq!(change_url(&PublishOutcome::NothingToPublish), None);
    }

    #[test]
    fn a_publication_records_what_the_sibling_said_it_did() {
        let url: onevcs::Url = "https://example.invalid/pull/7".parse().expect("a URL");
        let published = Publication {
            session: SessionToken("s-1".into()),
            branch: "onepipeline/service".into(),
            policy: MergePolicy::ChangeOpen,
            outcome: PublishOutcome::ChangeOpen(url),
        };
        let event = published_event(&published, &crate::event::Labels::default());
        assert_eq!(event.stream, "onevcs-onepipeline/service");
        assert_eq!(event.payload["branch"], "onepipeline/service");
        assert_eq!(event.payload["policy"], "change-open");
        assert_eq!(event.payload["outcome"], "change-open");
        assert_eq!(event.payload["url"], "https://example.invalid/pull/7");
        // The publication's own record says where the change got to, so a reader
        // watching the stream sees it at the moment it happened rather than only
        // in the settlement folded from it afterwards.
        assert_eq!(event.payload["landing"], "unlanded");

        let merged = Publication {
            outcome: PublishOutcome::Merged(onevcs::Sha("abc".into())),
            ..published
        };
        let event = published_event(&merged, &crate::event::Labels::default());
        assert_eq!(event.payload["landing"], "landed");

        // Nothing to publish is nothing to land, and the record says so by
        // carrying no claim rather than by carrying the convenient one.
        let empty = Publication {
            outcome: PublishOutcome::NothingToPublish,
            ..merged
        };
        let event = published_event(&empty, &crate::event::Labels::default());
        assert_eq!(event.payload["landing"], serde_json::Value::Null);
    }

    /// What reads as a session-open conflict, and what deliberately does not.
    ///
    /// The decision this drives stops a node retrying and blocks the subtree
    /// under it until a person answers, so the only thing that may reach it is
    /// the refusal [`session_refusal`] composed at the executor seam.
    /// `tests/e2e/lifecycle.rs` drives the real conflict end to end and reads the
    /// decision it raises; what is held here is everything that *resembles* one
    /// and is not — which no journey can drive, because a dispatch has no way to
    /// make its own prose arrive as the seam's own refusal.
    #[test]
    fn only_the_refusal_this_module_composed_reads_as_a_session_open_conflict() {
        let refused = session_refusal(onevcs::Error::SyncConflict {
            reason: "both sides changed README.md".into(),
        });
        assert!(
            session_open_conflicted(&refused),
            "the refusal this module composes for the conflict was not read as one: {refused}"
        );

        // Every other refusal of the same call keeps its retries.
        let invalid = session_refusal(onevcs::Error::Invalid {
            reason: "no such base".into(),
        });
        assert!(
            !session_open_conflicted(&invalid),
            "a refusal no part of this is was read as the conflict: {invalid}"
        );

        // A dispatch quoting the marker in its own account of itself is not the
        // seam saying it, wherever in the sentence the words land.
        assert!(!session_open_conflicted(&Error::Sibling {
            tool: ONEVCS,
            message: format!("the agent reported that {SESSION_OPEN_CONFLICT}"),
        }));

        // Nor is a refusal spelled this way by anything that is not `onevcs`.
        assert!(!session_open_conflicted(&Error::Sibling {
            tool: "oneagentgraph",
            message: format!("{SESSION_OPEN_CONFLICT}: sync conflict"),
        }));

        // Nor a failure of this crate's own that is not a sibling's at all.
        assert!(!session_open_conflicted(&Error::Invalid(format!(
            "{SESSION_OPEN_CONFLICT}: sync conflict"
        ))));
    }

    /// The three answers [`session_tip`] gives, against three real streams.
    ///
    /// The distinction is the whole of why the type has three cases: a session
    /// that committed nothing left its branch where it stood, and a stream
    /// nothing could read says nothing about where the branch stands — so
    /// `crate::lifecycle` hands an attempt back for the first and spends it for
    /// the second. Driven through the sibling's own reader over files on disk,
    /// because "the reader refuses this batch" is a fact about that reader.
    ///
    /// It takes [`scratch_home_held`] for the reason the journey below states:
    /// `ONEVCS_HOME` is process-global and this crate's unit tests share one
    /// process.
    #[test]
    fn a_session_tip_tells_a_branch_that_did_not_move_from_one_nothing_could_read() {
        let _home = super::scratch_home_held();
        let root = std::env::temp_dir().join(format!("onepipeline-tip-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&root);
        std::fs::create_dir_all(root.join("streams")).expect("a scratch state root");
        std::env::set_var(onevcs_home(), &root);

        let record = |token: &str, seq: u64, kind: &str, payload: serde_json::Value| {
            serde_json::json!({
                "v": 1,
                "ts": "2026-01-01T00:00:00.000Z",
                "stream": token,
                "seq": seq,
                "source": "vcs",
                "kind": kind,
                "labels": {},
                "payload": payload,
                "artifacts": [],
            })
            .to_string()
        };
        let write = |token: &str, body: String| {
            std::fs::write(root.join("streams").join(format!("{token}.ndjson")), body)
                .expect("the stream is written");
        };
        let opened = |token: &str| record(token, 1, "session-opened", serde_json::json!({}));
        let committed = |token: &str, sha: &str| {
            record(
                token,
                2,
                "commit-preserved",
                serde_json::json!({"branch": "b", "sha": sha}),
            )
        };

        // A session that committed: the branch stands at what it recorded.
        let at = "s-tip-committed";
        write(at, format!("{}\n{}\n", opened(at), committed(at, "c0ffee")));
        assert_eq!(
            session_tip(&SessionToken(at.into())),
            SessionTip::At(Commit::of("c0ffee").expect("a commit this crate carries"))
        );

        // A session that committed nothing: `onevcs` commits a worktree only
        // where it holds something to commit, so the branch stands where it
        // stood — which is evidence, and not the absence of it.
        let unmoved = "s-tip-nothing";
        write(unmoved, format!("{}\n", opened(unmoved)));
        assert_eq!(
            session_tip(&SessionToken(unmoved.into())),
            SessionTip::Unmoved
        );

        // A stream cut mid-record: the sibling's typed reader refuses the whole
        // batch, so the commit that *is* in it is one this crate never saw.
        let torn = "s-tip-torn";
        let whole = committed(torn, "decaf");
        write(torn, format!("{}\n{}", opened(torn), &whole[..20]));
        assert_eq!(
            session_tip(&SessionToken(torn.into())),
            SessionTip::Unknown,
            "a batch the reader refused was read as a session that committed nothing"
        );

        // A stream nothing wrote at all: refused by name, and equally unknown.
        assert_eq!(
            session_tip(&SessionToken("s-tip-neverwritten".into())),
            SessionTip::Unknown
        );

        // And a recorded commit this crate will not carry. The session did
        // commit, whatever the value is, so this is not the branch standing
        // still either.
        let forged = "s-tip-forged";
        write(
            forged,
            format!(
                "{}\n{}\n",
                opened(forged),
                committed(forged, "c0ffee\u{7}bad")
            ),
        );
        assert_eq!(
            session_tip(&SessionToken(forged.into())),
            SessionTip::Unknown,
            "a commit that would forge a row was read as a session that committed nothing"
        );

        // The `Option` the older reader answers with is this, collapsed.
        assert_eq!(
            branch_head_in(&SessionToken(at.into())),
            Some("c0ffee".to_string())
        );
        assert_eq!(branch_head_in(&SessionToken(unmoved.into())), None);
        assert_eq!(branch_head_in(&SessionToken(torn.into())), None);

        let _ = std::fs::remove_dir_all(&root);
    }

    /// What this crate reads from a session stream that is not whole.
    ///
    /// `onevcs` appends a record as *two* writes — the line, then its newline —
    /// so a reader can see the line before its terminator, and its typed reader
    /// advances its cursor over whatever `str::lines` yields. That was carried
    /// in as a known risk on exactly the path a publication is followed on, and
    /// a stream read wrongly is a publication that looks like it never
    /// happened. So it is exercised rather than reasoned about.
    ///
    /// One test, not three: `ONEVCS_HOME` is process-global, and separate tests
    /// would set it from separate threads and read one another's state root. The
    /// other module that needs a scratch root takes turns with this one through
    /// [`scratch_home_held`], which is held for as long as this root is pointed at.
    #[test]
    fn a_session_stream_that_is_not_whole_is_read_for_what_it_holds() {
        let _home = super::scratch_home_held();
        let root = std::env::temp_dir().join(format!("onepipeline-stream-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&root);
        std::fs::create_dir_all(root.join("streams")).expect("a scratch state root");
        std::env::set_var(onevcs_home(), &root);

        let record = |token: &str, seq: u64, kind: &str| {
            serde_json::json!({
                "v": 1,
                "ts": "2026-01-01T00:00:00.000Z",
                "stream": token,
                "seq": seq,
                "source": "vcs",
                "kind": kind,
                "labels": {},
                "payload": {},
                "artifacts": [],
            })
            .to_string()
        };
        let write = |token: &str, body: String| {
            std::fs::write(root.join("streams").join(format!("{token}.ndjson")), body)
                .expect("the stream is written");
        };
        let seqs = |envelopes: &[Envelope]| envelopes.iter().map(|e| e.seq).collect::<Vec<_>>();

        // A whole record whose newline has not been written yet. It is read —
        // and read *once*: the cursor that consumed it does not hand it back
        // when the terminator and the next record arrive.
        let torn = "s-unterminated";
        let followed = SessionToken(torn.to_owned());
        write(
            torn,
            format!(
                "{}\n{}",
                record(torn, 1, "session-opened"),
                record(torn, 2, "push")
            ),
        );
        let mut stream = opened(&followed, None).expect("the stream opens");
        assert_eq!(
            seqs(&next_batch(&mut stream, &followed)),
            vec![1, 2],
            "a record whose newline was still unwritten was lost"
        );
        write(
            torn,
            format!(
                "{}\n{}\n{}\n",
                record(torn, 1, "session-opened"),
                record(torn, 2, "push"),
                record(torn, 3, "session-closed")
            ),
        );
        assert_eq!(
            seqs(&next_batch(&mut stream, &followed)),
            vec![3],
            "the terminator arriving handed a record back a second time"
        );

        // A line that is not a whole envelope — a stream cut mid-record. The
        // sibling's typed reader refuses the **batch**, and its cursor has
        // already moved past the line, so the whole records before it in the
        // same read are refused with it. Reported out loud rather than folded
        // into an empty stream, and recorded as a proposal for `onevcs` in
        // `docs/contract-divergences.md`.
        let cut = "s-cutmidline";
        let whole = record(cut, 1, "session-opened");
        let partial = record(cut, 2, "push");
        write(cut, format!("{whole}\n{}", &partial[..20]));
        assert!(
            events(&SessionToken(cut.to_owned()), None).is_empty(),
            "the typed reader now hands back the whole records before a torn one; \
             narrow this assertion to the torn record alone"
        );

        // And a stream nothing wrote at all is not an empty one: it is refused
        // by name, which is what stops a token nobody opened reading as a
        // session that recorded nothing.
        assert!(events(&SessionToken("s-neverwritten".into()), None).is_empty());

        let _ = std::fs::remove_dir_all(&root);
    }

    /// The variable naming the sibling's state root, spelled once.
    ///
    /// `onevcs` publishes no constant for it, so the test that points it at a
    /// scratch root says it here rather than in three places.
    fn onevcs_home() -> &'static str {
        "ONEVCS_HOME"
    }

    /// The marks a reader starts from are the store's own, one series per stream.
    ///
    /// A store holds more than one producer's records, so a fold that kept a
    /// single mark would let the higher series hide the lower one's next record.
    #[test]
    fn the_marks_a_later_reader_starts_from_are_folded_from_what_the_store_holds() {
        let wrote = |stream: &str, seq: u64| Envelope {
            v: crate::event::ENVELOPE_VERSION,
            ts: "2026-01-01T00:00:00.000Z".into(),
            stream: stream.to_owned(),
            seq,
            source: crate::event::Source::Vcs,
            kind: crate::event::EventKind("release-observed".into()),
            phase: Some(crate::event::Phase::Release),
            labels: crate::event::Labels::default(),
            payload: serde_json::Map::new(),
            artifacts: Vec::new(),
        };
        // A store holding two producers' records, each in its own series and
        // neither of them contiguous: what a run is handed of a stream its
        // producer shares across runs is the part correlated to its own work.
        let held = vec![wrote("s-1", 1), wrote("s-1", 2), wrote("releases-abc", 7)];
        let marks = Watermarks::of_relayed(&held);
        for envelope in &held {
            assert!(
                !marks.beyond(envelope),
                "a record the store already holds would be relayed again: {envelope:?}"
            );
        }
        // And each stream continues from its own mark, so the higher series
        // cannot hide the lower one's next record.
        assert!(marks.beyond(&wrote("s-1", 3)));
        assert!(marks.beyond(&wrote("releases-abc", 8)));
        assert!(!marks.beyond(&wrote("releases-abc", 6)));
        // A stream nothing was relayed from is entirely unread, first record and
        // all: a producer numbering from zero is not a producer that said nothing.
        assert!(marks.beyond(&wrote("releases-def", 0)));
    }

    #[test]
    fn a_relayed_envelope_keeps_the_kind_and_attribution_its_producer_wrote() {
        let mut labels = onevcs::Labels {
            member: Some("worker".into()),
            ..onevcs::Labels::default()
        };
        labels
            .extra
            .insert("session".into(), serde_json::json!("s-1"));
        let envelope = relayed(onevcs::Envelope {
            v: 1,
            ts: "2026-01-01T00:00:00.000Z".into(),
            stream: "s-1".into(),
            seq: 4,
            source: onevcs::Source::Vcs,
            kind: onevcs::EventKind::ChangeOpened,
            phase: onevcs::Phase::Review,
            labels,
            payload: serde_json::Map::new(),
            artifacts: vec![onevcs::ArtifactRef {
                id: onevcs::ArtifactId("a-1".into()),
                kind: "log".into(),
                bytes: 12,
            }],
        });
        assert_eq!(envelope.kind.0, "change-opened");
        assert_eq!(envelope.source, crate::event::Source::Vcs);
        assert_eq!(envelope.seq, 4);
        // A key the merged envelope does not reserve rides in `extra` rather
        // than being dropped in the relay.
        assert_eq!(envelope.labels.extra["member"], "worker");
        assert_eq!(envelope.labels.extra["session"], "s-1");
        assert_eq!(envelope.artifacts[0].id.0, "a-1");
    }

    /// A read that did not get an answer says what refused, on one line.
    ///
    /// The one thing this seam decides: a refusal is **not** the sibling saying
    /// the work has not landed, and reading it as one is what would send somebody
    /// to publish work the base already carries. The sibling's answers travel
    /// whole and are read where they are printed — see `views::evidence` and
    /// `Reported::stands` — so there is nothing else to decide here.
    ///
    /// Stripped, because the message is somebody else's and a rendered line is
    /// one line.
    #[test]
    fn a_read_that_got_no_answer_says_what_refused_rather_than_that_nothing_landed() {
        let refused = read_of(Err(onevcs::Error::Invalid {
            reason: "no such repository\nand a second line".into(),
        }));
        assert_eq!(
            refused,
            LandingRead::Refused {
                because: "this host could not decide it: invalid input: no such repository and \
                          a second line"
                    .into()
            }
        );
        assert!(!proved_landed_from(&refused), "a refusal read as a landing");

        // And the sibling's own answers reach a caller unchanged.
        let landed = read_of(Ok(onevcs::Landed::Yes {
            evidence: onevcs::LandingEvidence::RecordedLanding {
                commit: onevcs::Sha("abc1234".into()),
            },
        }));
        assert!(matches!(
            &landed,
            LandingRead::Answered(onevcs::Landed::Yes { .. })
        ));
        assert!(proved_landed_from(&landed));
    }

    /// What [`proved_landed`] decides, without a repository to ask.
    fn proved_landed_from(read: &LandingRead) -> bool {
        matches!(read, LandingRead::Answered(landed) if landed.is_landed())
    }
}