sapphire-framework-server 0.29.0

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

use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;

use grain_id::GrainId;
use sapphire_backend::BackendEvent;
use sapphire_backend::protocol as proto;
use sapphire_bridge_api::{
    IncomingParams, RegisterParams, WorkspaceRegistration, WorkspacesResult,
};
use sapphire_bridge_client::BridgeClient;
use sapphire_ipc::ManagedBy;
use sapphire_sync::{PathUpdate, PauseReason, Replica, ReplicaConfig, ScanOutcome, SystemClock};
use sapphire_workspace::{AppContext, Workspace};
use tokio::sync::{Mutex, OnceCell, broadcast, mpsc};

pub mod id;
mod live;
mod methods;
#[cfg(any(test, feature = "test-util"))]
pub mod testing;
mod topology;
mod watch;

pub use id::{SYNC_ID_FILE, WORKSPACE_MAP_FILE, sync_id, sync_id_path};
pub use live::{DIAL_BACKOFF_MAX, OPEN_TIMEOUT};
pub use methods::sync_router;
/// What [`SyncRuntime::peers`] answers with.
pub use sapphire_bridge_api::{PeerInfo, PeersResult};
pub use watch::{DEBOUNCE, Watcher};

use live::{LivePeers, dial_backoff};

use crate::error::{Error, Result};
use crate::settings::Settings;

use crate::host::WorkspaceHost;

/// What `sync.status` reports.
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
pub struct SyncStatus {
    /// Whether this workspace is synced at all.
    pub enabled: bool,
    /// Its identity across devices, when it is.
    pub workspace_id: Option<GrainId>,
    /// How many devices the workgroup has, this host excluded.
    pub peers: usize,
    /// Why replication is paused, if it is — a missing root or marker directory.
    pub paused: Option<String>,
    /// The last failure, if any.
    pub last_error: Option<String>,
    /// Whether the bridge is reachable. `false` is not an outage: the app server works.
    pub bridge_available: bool,
    /// How the workspace is wired to its peers, by the roles the bridge last reported.
    pub topology: proto::Topology,
    /// How far embedding has got, when this host embeds (#188).
    pub embedding: Option<proto::EmbeddingProgress>,
    /// Some edit this device made since the workspace was enabled has not reached a peer.
    pub pending_delivery: bool,
    /// How many open live sessions have a peer that confirms what it is pushed.
    pub acknowledging_peers: usize,
}

/// One workspace this runtime syncs.
struct Synced {
    /// Its identity across devices, shared with every peer.
    workspace_id: GrainId,
    /// Its replica store. Held open because the store is a redb database.
    replica: Arc<Mutex<Replica>>,
    /// Why replication is paused, from the last scan.
    paused: Option<PauseReason>,
    /// The last failure, if any.
    last_error: Option<String>,
    /// This device's own counter as far as a peer has confirmed having it: every local edit
    /// up to here is known to have left the device. Starts at the counter the replica had
    /// when it was enabled — what came before is not waited for — and moves only when a peer
    /// covers *all* of this device's edits, so that `SyncDelivered` means "everything".
    delivered_through: u64,
}

/// How old a vector file no live content needs must be before the primary device removes
/// it: a vector can arrive before its file, and a day is far longer than that window.
const STALE_VECTOR_AGE: std::time::Duration = std::time::Duration::from_secs(24 * 60 * 60);

/// Texts per embedding request in a server pass: small, so a search query waiting on the
/// bridge's one worker is never stuck behind a long backfill (#188).
const PASS_BATCH: usize = 4;

/// How the primary device paces its backfill (#188).
#[derive(Clone, Copy, Debug)]
pub struct BackfillTiming {
    /// How old another device's version must be before the primary embeds it: its author
    /// has embedded it by then, or will not.
    pub grace: std::time::Duration,
    /// How often the primary device runs a pass with nothing prompting it.
    pub interval: std::time::Duration,
}

impl Default for BackfillTiming {
    fn default() -> Self {
        BackfillTiming {
            grace: std::time::Duration::from_secs(10 * 60),
            interval: std::time::Duration::from_secs(10 * 60),
        }
    }
}

/// How long [`SyncRuntime::keep_connected`] waits before its first retry after the bridge
/// could not be reached. Each failure doubles it, up to [`RECONNECT_BACKOFF_MAX`].
pub const RECONNECT_BACKOFF: std::time::Duration = std::time::Duration::from_millis(500);

/// The longest [`SyncRuntime::keep_connected`] waits between two attempts to reach the
/// bridge: a bridge that comes up is found within this long.
pub const RECONNECT_BACKOFF_MAX: std::time::Duration = std::time::Duration::from_secs(30);

/// The wait before reconnection attempt `attempt` (0-based), backing off from `first`.
fn reconnect_backoff(first: std::time::Duration, attempt: u32) -> std::time::Duration {
    first
        .saturating_mul(1u32 << attempt.min(16))
        .min(RECONNECT_BACKOFF_MAX.max(first))
}

/// What a connection attempt to the bridge yields.
type Connecting = std::pin::Pin<
    Box<dyn std::future::Future<Output = sapphire_ipc::Result<Option<BridgeClient>>> + Send>,
>;

/// How a [`SyncRuntime`] reaches the bridge, on start and whenever it has lost it (#244).
///
/// An attempt answers `Ok(None)` when no bridge is running — the runtime then tries again
/// later — and an error for anything else, such as a bridge of another API version.
#[derive(Clone)]
pub struct BridgeConnector(Arc<dyn Fn() -> Connecting + Send + Sync>);

impl BridgeConnector {
    /// Connect with `connect`.
    pub fn new<F, Fut>(connect: F) -> BridgeConnector
    where
        F: Fn() -> Fut + Send + Sync + 'static,
        Fut: std::future::Future<Output = sapphire_ipc::Result<Option<BridgeClient>>>
            + Send
            + 'static,
    {
        BridgeConnector(Arc::new(move || Box::pin(connect())))
    }

    /// The host's own bridge, at its standard endpoint, as a client of `kind` at
    /// `version`.
    pub fn standard(kind: &'static str, version: &'static str) -> BridgeConnector {
        BridgeConnector::new(move || async move {
            match BridgeClient::connect(kind, version).await {
                Ok(client) => Ok(Some(client)),
                Err(sapphire_ipc::Error::NotRunning(_)) => Ok(None),
                Err(err) => Err(err),
            }
        })
    }

    /// The bridge listening at `endpoint`: for a fixture that runs its own.
    pub fn at(
        endpoint: sapphire_ipc::Endpoint,
        kind: &'static str,
        version: &'static str,
    ) -> BridgeConnector {
        BridgeConnector::new(move || {
            let endpoint = endpoint.clone();
            async move { BridgeClient::connect_at(&endpoint, kind, version).await }
        })
    }

    /// One attempt: `None` when no bridge is running.
    pub(crate) async fn connect(&self) -> sapphire_ipc::Result<Option<BridgeClient>> {
        (self.0)().await
    }
}

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

/// What [`SyncRuntime::reclaim`] found of a replica `disable` let go of.
enum Reclaimed {
    /// Still held: here it is.
    Open(Arc<Mutex<Replica>>),
    /// Let go of since, so its store may be closing still.
    Closing,
    /// Nothing to take back.
    Closed,
}

/// How often, and how far apart, `enable` tries to open a store that may still be closing.
const CLOSING_RETRIES: u32 = 50;
const CLOSING_RETRY_DELAY: std::time::Duration = std::time::Duration::from_millis(20);

/// Replication for one application's workspaces.
pub struct SyncRuntime {
    ctx: &'static AppContext,
    /// The bridge connection, while there is one. Swapped when the runtime reconnects
    /// (#244); [`run`](SyncRuntime::run) follows it.
    bridge: tokio::sync::watch::Sender<Option<Arc<BridgeClient>>>,
    /// How to reach the bridge again; `None` for a runtime given one connection to use.
    connector: Option<BridgeConnector>,
    /// Woken by [`reconnect_now`](SyncRuntime::reconnect_now): someone wants the bridge
    /// now, so [`keep_connected`](SyncRuntime::keep_connected) cuts its backoff short.
    reconnect_now: tokio::sync::Notify,
    /// Workspaces asked to sync while the bridge was away, with their ids. Enabled once
    /// it is back.
    ///
    /// Also the lock that orders an `enable` against a reconnection: whoever holds it sees
    /// the bridge slot as the other left it, so a workspace is either enabled now or left
    /// here for the reconnection to enable — never neither.
    pending: Mutex<HashMap<PathBuf, GrainId>>,
    /// The announcements of a connection [`attach`](SyncRuntime::attach) just made,
    /// subscribed to before it registered: the bridge announces the streams that waited for
    /// this server as soon as it registers (#244), and a subscription made afterwards by
    /// [`run`](SyncRuntime::run) would miss them.
    subscribed: std::sync::Mutex<Option<(Arc<BridgeClient>, broadcast::Receiver<IncomingParams>)>>,
    exe_path: PathBuf,
    managed_by: ManagedBy,
    synced: Mutex<HashMap<PathBuf, Synced>>,
    /// The replicas [`disable`](SyncRuntime::disable) let go of, by canonical root, for as
    /// long as something else still holds one: a scan, a session or the first dial that
    /// began before. The store is a redb database, open until its last holder drops it, so
    /// a re-enable meanwhile could not open it again; [`enable`](SyncRuntime::enable)
    /// takes the open one back instead. [`release`](SyncRuntime::release) puts nothing
    /// here: its store is about to be moved or deleted.
    stopped: std::sync::Mutex<HashMap<PathBuf, std::sync::Weak<Mutex<Replica>>>>,
    /// This host's device id. Asked of the bridge once, on the first [`enable`], and never
    /// again: a second registration with an empty workspace list would briefly clear this
    /// application's routes.
    ///
    /// [`enable`]: SyncRuntime::enable
    device_id: OnceCell<GrainId>,
    /// The file watcher, once [`watch_changes`] has started it.
    ///
    /// The runtime owns it because the spec puts it here: the sync runtime is a watcher and
    /// a replica per registered root, and the two must agree on which roots are synced.
    /// [`enable`] and [`disable`] keep them in step.
    ///
    /// [`watch_changes`]: SyncRuntime::watch_changes
    /// [`enable`]: SyncRuntime::enable
    /// [`disable`]: SyncRuntime::disable
    watcher: OnceCell<Arc<Watcher>>,
    /// The app server that owns these workspaces' caches, once it has told us.
    ///
    /// A replication session writes files, and the search index is not part of a session: the
    /// index belongs to the app server, which holds the workspace open. Reaching it through
    /// the host is what keeps a received file and its index moving together, and it is the
    /// *same* [`WorkspaceState`](sapphire_workspace::WorkspaceState) the server serves from —
    /// opening a second one would collide on the retrieve store's exclusive lock.
    ///
    /// Absent for a runtime nobody has wired to an app server (a test stub driving the
    /// replica directly). Nothing then indexes, which is correct: nothing owns an index.
    host: OnceCell<Arc<WorkspaceHost>>,
    /// The app server's settings, whose workspace scope is a file in the selected
    /// workspace's marker: read again when a session or a local edit changes the files.
    settings: std::sync::Mutex<Option<Settings>>,
    /// Serialises re-indexing, so two sessions finishing at once do not both sweep the
    /// workspace through the same store.
    reindexing: Mutex<()>,
    /// Roots with an embedding pass waiting to run (#187). A request for a root already
    /// waiting is folded into that pass.
    embed_wanted: Mutex<std::collections::HashSet<PathBuf>>,
    /// Serialises embedding passes: one model, one queue.
    embedding: Mutex<()>,
    /// Roots whose embedding pass is running now, for `sync.status`.
    embed_running: Mutex<std::collections::HashSet<PathBuf>>,
    /// The primary device's backfill pacing (#188).
    backfill: std::sync::Mutex<BackfillTiming>,
    /// The bridge an embedding pass asks for its model; `None` is the host's standard one.
    bridge_endpoint: Option<sapphire_ipc::Endpoint>,
    /// The live session table of every synced workspace, keyed by canonical root.
    ///
    /// One table per workspace rather than one for the runtime: a session is about one
    /// workspace, and the two storm rules are per-workspace rules. A root that is not
    /// synced has none, so a dial or a push for it is a no-op rather than an error.
    live: Mutex<HashMap<PathBuf, Arc<LivePeers>>>,
    /// The bridge's last answer to `peers`, roles included. The inbound guard and
    /// [`is_primary`](SyncRuntime::is_primary) read it rather than asking again.
    last_peers: Mutex<Option<PeersResult>>,
    /// How long a dial waits for the bridge to open a stream; [`OPEN_TIMEOUT`] by default.
    open_timeout: std::time::Duration,
    /// The first wait of [`keep_connected`](SyncRuntime::keep_connected)'s backoff;
    /// [`RECONNECT_BACKOFF`] but for tests.
    reconnect_first: std::time::Duration,
    /// The first dials [`enable`](SyncRuntime::enable) leaves running. Each pins the
    /// runtime, so [`stop_dials`](SyncRuntime::stop_dials) ends them with the server.
    first_dials: std::sync::Mutex<tokio::task::JoinSet<()>>,
    /// The rest of the runtime's background work: inbound sessions, the readers of live
    /// sessions, and embedding passes. Each pins the runtime — and through it the replica
    /// stores, which are redb databases locked until the last pin drops — so
    /// [`stop_background`](SyncRuntime::stop_background) ends them with the server (#235).
    background: std::sync::Mutex<tokio::task::JoinSet<()>>,
}

impl SyncRuntime {
    /// A runtime for `ctx`'s application, syncing through `bridge`.
    ///
    /// It has no way to reach the bridge again: when the connection closes, sync stops until
    /// the runtime is given a [`connector`](Self::with_connector).
    pub fn new(
        ctx: &'static AppContext,
        bridge: Arc<BridgeClient>,
        exe_path: PathBuf,
        managed_by: ManagedBy,
    ) -> SyncRuntime {
        SyncRuntime::build(ctx, Some(bridge), None, exe_path, managed_by)
    }

    /// A runtime for `ctx`'s application that reaches the bridge with `connector`, and again
    /// whenever it loses it (#244).
    ///
    /// Not connected yet: [`connect`](Self::connect) makes the first attempt, and
    /// [`keep_connected`](Self::keep_connected) the rest. [`AppServer::run`] does both.
    ///
    /// [`AppServer::run`]: crate::AppServer::run
    pub fn connecting(
        ctx: &'static AppContext,
        connector: BridgeConnector,
        exe_path: PathBuf,
        managed_by: ManagedBy,
    ) -> SyncRuntime {
        SyncRuntime::build(ctx, None, Some(connector), exe_path, managed_by)
    }

    /// Reach the bridge again with `connector` once the connection closes.
    pub fn with_connector(mut self, connector: BridgeConnector) -> SyncRuntime {
        self.connector = Some(connector);
        self
    }

    fn build(
        ctx: &'static AppContext,
        bridge: Option<Arc<BridgeClient>>,
        connector: Option<BridgeConnector>,
        exe_path: PathBuf,
        managed_by: ManagedBy,
    ) -> SyncRuntime {
        SyncRuntime {
            ctx,
            bridge: tokio::sync::watch::Sender::new(bridge),
            connector,
            reconnect_now: tokio::sync::Notify::new(),
            pending: Mutex::new(HashMap::new()),
            subscribed: std::sync::Mutex::new(None),
            exe_path,
            managed_by,
            synced: Mutex::new(HashMap::new()),
            stopped: std::sync::Mutex::new(HashMap::new()),
            device_id: OnceCell::new(),
            watcher: OnceCell::new(),
            host: OnceCell::new(),
            settings: std::sync::Mutex::new(None),
            reindexing: Mutex::new(()),
            embed_wanted: Mutex::new(std::collections::HashSet::new()),
            embedding: Mutex::new(()),
            embed_running: Mutex::new(std::collections::HashSet::new()),
            backfill: std::sync::Mutex::new(BackfillTiming::default()),
            bridge_endpoint: None,
            live: Mutex::new(HashMap::new()),
            last_peers: Mutex::new(None),
            open_timeout: OPEN_TIMEOUT,
            reconnect_first: RECONNECT_BACKOFF,
            first_dials: std::sync::Mutex::new(tokio::task::JoinSet::new()),
            background: std::sync::Mutex::new(tokio::task::JoinSet::new()),
        }
    }

    /// The bridge, while connected.
    fn bridge(&self) -> Result<Arc<BridgeClient>> {
        match &*self.bridge.borrow() {
            Some(bridge) if !bridge.is_closed() => Ok(Arc::clone(bridge)),
            _ => Err(Error::BridgeDisconnected),
        }
    }

    /// Whether the runtime has a live connection to the bridge.
    pub fn is_connected(&self) -> bool {
        self.bridge().is_ok()
    }

    /// The bridge connection, while there is an open one: what the relay forwards over.
    pub(crate) fn connected_bridge(&self) -> Option<Arc<BridgeClient>> {
        self.bridge().ok()
    }

    /// Have [`keep_connected`](Self::keep_connected) try the bridge now rather than when its
    /// backoff next ends, and start its backoff over: for the relay, whose client may just
    /// have started the bridge. A no-op while connected, bar one early attempt after the
    /// next loss, which makes one at once anyway.
    pub(crate) fn reconnect_now(&self) {
        self.reconnect_now.notify_one();
    }

    /// The bridge slot, followed: changes each time the runtime connects or loses the
    /// connection.
    pub(crate) fn bridge_changes(&self) -> tokio::sync::watch::Receiver<Option<Arc<BridgeClient>>> {
        self.bridge.subscribe()
    }

    /// Run `task` in the background, tracked so the server's shutdown ends it.
    fn spawn_background(&self, task: impl std::future::Future<Output = ()> + Send + 'static) {
        let mut background = self.background.lock().expect("background tasks");
        // Reap the finished ones, so the set holds only what is running.
        while background.try_join_next().is_some() {}
        background.spawn(task);
    }

    /// Give up on opening a stream to a peer after `timeout` rather than [`OPEN_TIMEOUT`]:
    /// for tests, which cannot wait that long for a peer that never answers.
    pub fn with_open_timeout(mut self, timeout: std::time::Duration) -> SyncRuntime {
        self.open_timeout = timeout;
        self
    }

    /// Back off from `first` rather than [`RECONNECT_BACKOFF`] while the bridge cannot be
    /// reached: for tests of a backoff that has grown long, which cannot wait for it to.
    #[cfg(test)]
    pub(crate) fn with_reconnect_backoff(mut self, first: std::time::Duration) -> SyncRuntime {
        self.reconnect_first = first;
        self
    }

    /// Tell the runtime which app server owns these workspaces' caches.
    ///
    /// Called by [`AppServer::sync`](crate::AppServer::sync). First writer wins: a runtime
    /// belongs to the server that wired it.
    /// Embed through the bridge at `endpoint` rather than the host's standard one: for a
    /// fixture running several hosts in one process.
    pub fn with_bridge_endpoint(mut self, endpoint: sapphire_ipc::Endpoint) -> SyncRuntime {
        self.bridge_endpoint = Some(endpoint);
        self
    }

    /// Pace the primary device's backfill differently: for tests, which cannot wait ten
    /// minutes.
    pub fn set_backfill_timing(&self, timing: BackfillTiming) {
        *self.backfill.lock().expect("backfill timing") = timing;
    }

    fn backfill_timing(&self) -> BackfillTiming {
        *self.backfill.lock().expect("backfill timing")
    }

    pub(crate) fn set_host(&self, host: Arc<WorkspaceHost>) {
        let _ = self.host.set(host);
    }

    /// Read `settings` again whenever a workspace's files change under this runtime — a
    /// session wrote them, or someone edited them — so workspace keys follow the marker's
    /// `config.toml`. Replaced, not kept, by a later call (a restarted server's).
    pub(crate) fn set_settings(&self, settings: Option<Settings>) {
        *self.settings.lock().unwrap_or_else(|e| e.into_inner()) = settings;
    }

    /// Read the settings again after the files under a workspace changed. Cheap — two
    /// small files — and holding no lock past the read, so it runs on every change.
    fn refresh_settings(&self) {
        let settings = self
            .settings
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .clone();
        if let Some(settings) = settings
            && settings.refresh().is_err()
        {
            // The error may quote the file; the change itself went through.
            tracing::warn!("could not read the settings again after a change to the files");
        }
    }

    /// Start syncing `root`, returning its identity across devices.
    ///
    /// Idempotent: enabling an already-synced workspace returns the same id and changes
    /// nothing.
    pub async fn enable(self: &Arc<Self>, root: &Path) -> Result<GrainId> {
        let key = root.canonicalize().map_err(Error::Io)?;
        let watch_key = key.clone();
        let workspace_id = sync_id(self.ctx.app_name, &key)?;

        // Without a bridge there is no device id to open the replica with, and nobody to
        // register with: keep the workspace for the reconnection to enable (#244).
        {
            let mut pending = self.pending.lock().await;
            if self.bridge().is_err() {
                pending.insert(key, workspace_id);
                return Err(Error::BridgeDisconnected);
            }
            pending.remove(&key);
        }

        // The map stays locked across opening the replica, so two simultaneous `enable`s for
        // one root cannot both open it — the store is a redb database, and it is opened once.
        {
            let mut synced = self.synced.lock().await;
            if let Some(existing) = synced.get(&key) {
                return Ok(existing.workspace_id);
            }
            // A replica disabled a moment ago may still be open; it is this root's, with this
            // runtime's one device id, so it is taken back rather than opened a second time.
            let replica = match self.reclaim(&key) {
                Reclaimed::Open(replica) => replica,
                reclaimed => {
                    let device_id = self.device_id().await?;
                    let state_dir = self.state_dir(&key)?;
                    std::fs::create_dir_all(&state_dir).map_err(Error::Io)?;
                    let config =
                        ReplicaConfig::new(self.ctx.app_name, key.clone(), device_id, &state_dir);
                    // The last holder let go between the two: its store closes as the replica
                    // drops, just after it can no longer be taken back. Give it that moment.
                    let mut tries = match reclaimed {
                        Reclaimed::Closing => CLOSING_RETRIES,
                        _ => 0,
                    };
                    let replica = loop {
                        match Replica::open(config.clone(), Arc::new(SystemClock)) {
                            Ok(replica) => break replica,
                            Err(_) if tries > 0 => {
                                tries -= 1;
                                tokio::time::sleep(CLOSING_RETRY_DELAY).await;
                            }
                            Err(err) => return Err(Error::Sync(err.to_string())),
                        }
                    };
                    Arc::new(Mutex::new(replica))
                }
            };
            // Before the first scan below, so an edit made while the server was down is
            // waited for like any other.
            let delivered_through = own_counter(&*replica.lock().await);
            synced.insert(
                key.clone(),
                Synced {
                    workspace_id,
                    replica,
                    paused: None,
                    last_error: None,
                    delivered_through,
                },
            );
        }

        // Watch the new root too, so an edit made outside the server from now on is seen.
        // Not fatal if it fails: the app server's own writes take the exact path in
        // `handlers.rs`, and the next `watch_changes` re-reads the root set anyway.
        if let Some(watcher) = self.watcher.get()
            && let Err(err) = watcher.watch(&watch_key)
        {
            tracing::warn!(root = %watch_key.display(), "could not watch: {err}");
        }

        // Register only once the replica is open, so a registration never names a workspace
        // that cannot serve a session.
        self.reregister().await?;

        // Dial now, so enabling a workspace converges it rather than waiting for the next
        // local edit to trigger a session. This is what catches a host up when it returns:
        // the peer that has been running is the one the returning host has to ask, and no
        // watcher on either side fires for a file that was never written locally.
        //
        // Best-effort: a workspace is enabled whether or not any peer is up, and a failure
        // here is not a failure to enable. A session is symmetric, so dialing is enough —
        // this side sends its own view, and the peer answers with what this side lacks.
        //
        // In the background, so the answer does not wait on it: a peer that is offline
        // holds a dial up to `open_timeout`, longer than a client waits for `sync.enable`
        // (#228).
        //
        // Scan first: a file already on disk when the workspace is enabled — a folder that
        // existed before sync was turned on, or an edit made while the server was down —
        // fires no watcher, so without this it is never recorded and the session has
        // nothing to send. Logged, not fatal: the session still fetches what peers have.
        let runtime = Arc::clone(self);
        let mut dials = self.first_dials.lock().expect("first dials");
        // Reap the finished ones, so the set holds only what is running.
        while dials.try_join_next().is_some() {}
        dials.spawn(async move {
            if let Err(err) = runtime.scan(&key).await {
                tracing::warn!(root = %key.display(), "the first scan after enabling failed: {err}");
            }
            if let Err(err) = runtime.sync_now(&key).await {
                tracing::warn!(root = %key.display(), "the first session after enabling failed: {err}");
            }
        });
        Ok(workspace_id)
    }

    /// Stop syncing `root`. Files and the sync id stay, so re-enabling rejoins the same
    /// workspace rather than creating a second one.
    ///
    /// Something that began before — a scan, a session, the first dial — may still hold the
    /// replica open; a re-enable meanwhile takes it back (see [`reclaim`](Self::reclaim)).
    pub async fn disable(&self, root: &Path) -> Result<()> {
        let (replica, unregistered) = self.stop(root).await;
        if let Some(replica) = replica {
            let key = root.canonicalize().unwrap_or_else(|_| root.to_path_buf());
            let mut stopped = self.stopped.lock().expect("stopped replicas");
            // Forget the ones nothing holds any more, so the map stays small.
            stopped.retain(|_, replica| replica.strong_count() > 0);
            stopped.insert(key, Arc::downgrade(&replica));
        }
        unregistered
    }

    /// Stop syncing `root`, like [`disable`](Self::disable), then wait up to `limit` until
    /// nothing holds its replica store open any more — a session, a scan or an embedding
    /// pass that started before may still have it. A store about to be moved or deleted
    /// must be closed first.
    ///
    /// Only a store still in use at `limit` is an error. The bridge not hearing of it is
    /// logged, not returned: the store is closed all the same, and the next registration
    /// replaces the bridge's view.
    pub async fn release(&self, root: &Path, limit: std::time::Duration) -> Result<()> {
        let (replica, unregistered) = self.stop(root).await;
        if let Err(err) = unregistered {
            tracing::warn!(root = %root.display(), "stopping sync: {err}");
        }
        let Some(replica) = replica else {
            return Ok(());
        };
        let deadline = tokio::time::Instant::now() + limit;
        while Arc::strong_count(&replica) > 1 {
            if tokio::time::Instant::now() >= deadline {
                return Err(Error::Sync(format!(
                    "the replica of {} is still in use; try again",
                    root.display()
                )));
            }
            tokio::time::sleep(std::time::Duration::from_millis(20)).await;
        }
        Ok(())
    }

    /// Stop syncing `root`, handing back its replica — `None` when it was not synced — and
    /// how telling the bridge went.
    async fn stop(&self, root: &Path) -> (Option<Arc<Mutex<Replica>>>, Result<()>) {
        // A root that no longer exists cannot be canonicalized, but the keys of `synced` are
        // canonical already, so the path as given (a registry root) still names the entry.
        let key = root.canonicalize().unwrap_or_else(|_| root.to_path_buf());
        self.pending.lock().await.remove(&key);
        let removed = self.synced.lock().await.remove(&key);
        if removed.is_some()
            && let Some(watcher) = self.watcher.get()
        {
            watcher.unwatch(&key);
        }
        // Any live session for a workspace this server no longer holds is closed: it would
        // otherwise keep pushing files for a workspace nobody asked about any more.
        if let Some(peers) = self.live.lock().await.remove(&key) {
            peers.drop_connections().await;
        }
        let Some(removed) = removed else {
            return (None, Ok(()));
        };
        let unregistered = async {
            // A bridge that is gone has forgotten this server's registrations already, and
            // the reconnection registers only what is still synced.
            let Ok(bridge) = self.bridge() else {
                return Ok(());
            };
            bridge
                .unregister(removed.workspace_id)
                .await
                .map_err(|e| Error::Bridge(e.to_string()))?;
            self.reregister().await
        }
        .await;
        (Some(removed.replica), unregistered)
    }

    /// The workspaces the workgroup knows about.
    ///
    /// Read straight from the bridge, so it reflects every device's registrations. The
    /// bridge being down is not an empty answer — that would read as "nothing to map" — so
    /// it is an error, and `sync.map` reports it.
    pub async fn workspaces(&self) -> Result<WorkspacesResult> {
        self.bridge()?
            .workspaces()
            .await
            .map_err(|e| Error::Bridge(e.to_string()))
    }

    /// Place a workgroup workspace at a directory of this host: write its identity and
    /// map there, returning the canonical root. [`Current::map`](crate::Current::map)
    /// then selects it and syncs it.
    ///
    /// `selector` is the workspace's name or id, as [`workspaces`] lists it; `dir` must
    /// exist. Its marker is made when it has none, and nothing else: the marker's
    /// `config.toml` is the application's, and arrives with the first sync — a local one
    /// written first would meet it as a conflict (#253).
    ///
    /// [`workspaces`]: SyncRuntime::workspaces
    pub async fn place(&self, selector: &str, dir: &Path) -> Result<PathBuf> {
        let workgroup = self.workspaces().await?;
        let wanted = workgroup
            .workspaces
            .iter()
            // A name is the user-facing handle; the id is the exact one. Two workspaces
            // may not share a name, so a name names at most one.
            .find(|w| w.name == selector || w.workspace_id.to_string() == selector)
            .ok_or_else(|| Error::UnknownWorkspaceName(selector.to_owned()))?;

        // Refuse another application's workspace: this server syncs its own application's
        // workspaces, and `Workspace::from_root` would later refuse the directory anyway.
        if wanted.app_name != self.ctx.app_name {
            return Err(Error::WrongApp {
                name: wanted.name.clone(),
                app_name: wanted.app_name.clone(),
            });
        }

        let root = dir.canonicalize().map_err(Error::Io)?;
        let marker = root.join(format!(".{}", self.ctx.app_name));
        if !marker.is_dir() {
            std::fs::create_dir(&marker).map_err(Error::Io)?;
        }
        let workspace = Workspace::from_root(self.ctx, &root)?;
        if workspace.root != root {
            return Err(Error::UnknownWorkspace(root, self.ctx.app_name));
        }

        // A directory already syncing under another identity is not remapped: the sync id
        // is a device's word that it is the same workspace, and rewriting it silently
        // would make two hosts disagree about what they share.
        {
            let synced = self.synced.lock().await;
            if let Some(entry) = synced.get(&root)
                && entry.workspace_id != wanted.workspace_id
            {
                return Err(Error::SyncId(format!(
                    "{} already syncs as {}, not {}",
                    root.display(),
                    entry.workspace_id,
                    wanted.workspace_id
                )));
            }
        }

        // The identity arrives from the workgroup, not from this host: two hosts that map
        // the same workspace must sync as one, so the id is the one the workgroup lists,
        // written before `enable` reads it. A different id already on disk is refused
        // above; an absent one is written here.
        let marker = workspace.marker_dir();
        let id_path = marker.join(crate::sync::id::SYNC_ID_FILE);
        match std::fs::read_to_string(&id_path) {
            Ok(existing) if existing.trim() != wanted.workspace_id.to_string() => {
                return Err(Error::SyncId(format!(
                    "{} already holds identity {}, not {}",
                    root.display(),
                    existing.trim(),
                    wanted.workspace_id
                )));
            }
            Err(_) => {
                std::fs::write(&id_path, format!("{}\n", wanted.workspace_id))
                    .map_err(Error::Io)?;
            }
            Ok(_) => {}
        }

        // The map is what tells this application, on a later start, which workgroup
        // workspace a directory is.
        let map_path = root
            .join(format!(".{}", self.ctx.app_name))
            .join(WORKSPACE_MAP_FILE);
        let workspace_id = wanted.workspace_id.to_string();
        std::fs::write(&map_path, format!("{workspace_id}\n")).map_err(Error::Io)?;
        Ok(root)
    }

    /// What `sync.status` answers with.
    pub async fn status(&self, root: &Path) -> SyncStatus {
        let bridge = self.bridge().ok();
        let bridge_available = match &bridge {
            Some(bridge) => bridge.status().await.is_ok(),
            None => false,
        };
        let answer = match &bridge {
            Some(_) => self.peers().await.ok(),
            None => None,
        };
        let peers = answer
            .as_ref()
            .map_or(0, |p| p.peers.len().saturating_sub(1));
        let Ok(key) = root.canonicalize() else {
            return SyncStatus {
                enabled: false,
                workspace_id: None,
                peers,
                paused: None,
                last_error: None,
                bridge_available,
                topology: proto::Topology::Mesh,
                embedding: None,
                pending_delivery: false,
                acknowledging_peers: 0,
            };
        };
        let synced = self.synced.lock().await;
        match synced.get(&key) {
            Some(entry) => {
                let replica = Arc::clone(&entry.replica);
                let delivered_through = entry.delivered_through;
                let base = SyncStatus {
                    enabled: true,
                    workspace_id: Some(entry.workspace_id),
                    peers,
                    paused: entry.paused.map(|r| format!("{r:?}")),
                    last_error: entry.last_error.clone(),
                    bridge_available,
                    topology: topology::topology(
                        answer
                            .as_ref()
                            .and_then(|p| p.roles_for(entry.workspace_id)),
                    ),
                    embedding: None,
                    pending_delivery: false,
                    acknowledging_peers: 0,
                };
                drop(synced);
                // The map is unlocked first: the replica's lock may be waiting on a session
                // step that takes the map.
                let own = own_counter(&*replica.lock().await);
                let acknowledging_peers = match self.live_peers(&key).await {
                    Some(table) => table.acknowledging().await,
                    None => 0,
                };
                SyncStatus {
                    embedding: self.embedding_progress(&key).await,
                    pending_delivery: own > delivered_through,
                    acknowledging_peers,
                    ..base
                }
            }
            None => {
                drop(synced);
                // Asked to sync while the bridge was away: it is enabled, waiting for the
                // bridge, and `bridge_available` says why nothing moves.
                let waiting = self.pending.lock().await.get(&key).copied();
                SyncStatus {
                    enabled: waiting.is_some(),
                    workspace_id: waiting,
                    peers,
                    paused: None,
                    last_error: None,
                    bridge_available,
                    topology: proto::Topology::Mesh,
                    embedding: None,
                    pending_delivery: false,
                    acknowledging_peers: 0,
                }
            }
        }
    }

    /// How far `key`'s embedding has got, when this host embeds it. Reads the open
    /// workspace's index; never opens one, so a status call stays cheap.
    async fn embedding_progress(&self, key: &Path) -> Option<proto::EmbeddingProgress> {
        let host = self.host.get()?;
        let backend = host.open_backend(key)?;
        let state = backend.state();
        state.embedder()?;
        let info = state.db_info().ok()?;
        Some(proto::EmbeddingProgress {
            vectors: info.vector_count,
            pending: info.pending_count,
            running: self.embed_running.lock().await.contains(key),
        })
    }

    /// Bring the replica's view of the files up to date.
    ///
    /// Called straight after the app server's own writes, and by the watcher for everything
    /// else. A scan that finds nothing is cheap; a scan that is skipped loses an edit until
    /// the next one.
    pub async fn scan(&self, root: &Path) -> Result<()> {
        let Ok(key) = root.canonicalize() else {
            return Ok(());
        };
        let replica = {
            let synced = self.synced.lock().await;
            match synced.get(&key) {
                Some(entry) => Arc::clone(&entry.replica),
                None => return Ok(()),
            }
        };
        let (outcome, updates) = {
            let mut replica = replica.lock().await;
            let outcome = replica.scan().map_err(|e| Error::Sync(e.to_string()))?;
            // What the scan committed, as path updates, so it can go out on the sessions
            // that are already open. This is the whole point of the exact-path scan in
            // `handlers.rs`: a write through the server reaches a peer live, not at the next
            // dial.
            let updates = match &outcome {
                ScanOutcome::Scanned(report) => {
                    // The unit of replication is the path state (design doc section 2.4):
                    // the `seen` a peer prunes against is the *path's* version vector - the
                    // context the recorded entry was built on plus its own dot - never the
                    // whole replica vv. Sending the global vv here made receivers prune
                    // causally-related versions they still held: a conflict copy on the
                    // first remote edit, and tombstones pruned so deletes stopped
                    // propagating (issue #157).
                    report
                        .recorded
                        .iter()
                        .map(|entry| PathUpdate {
                            path: entry.path.clone(),
                            versions: vec![entry.clone()],
                            seen: {
                                let mut seen = entry.context.clone();
                                seen.add_dot(&entry.dot);
                                seen
                            },
                        })
                        .collect()
                }
                ScanOutcome::Paused(_) => Vec::new(),
            };
            (outcome, updates)
        };
        {
            let mut synced = self.synced.lock().await;
            if let Some(entry) = synced.get_mut(&key) {
                entry.paused = match outcome {
                    ScanOutcome::Paused(reason) => Some(reason),
                    ScanOutcome::Scanned(_) => None,
                };
            }
        }
        // After the map is unlocked: a push awaits every peer, and the table must not be
        // held behind a slow one.
        self.after_commit(&key, updates).await;
        Ok(())
    }

    /// Bring the search index up to date with files a session has written.
    ///
    /// A replication session moves files, not index rows: the index belongs to the app
    /// server, and the sync core is deliberately free of it. So the side that *applied* a
    /// change re-indexes, which is what makes a file that arrived from a peer searchable
    /// without a manual reindex — the claim the whole architecture rests on.
    ///
    /// Incremental, not a full rebuild: the workspace's mtime/size stamps mean only what
    /// actually changed is read back. A file the session wrote has a new stamp, so it is
    /// picked up; one that was already indexed and untouched is skipped.
    ///
    /// Best-effort by contract: the caller's session has already succeeded, and a failure
    /// here must not be reported as the session's. It is logged and the next session, local
    /// edit or explicit `workspace.reindex` tries again.
    async fn reindex(&self, root: &Path) {
        // The marker's `config.toml` may be among what arrived.
        self.refresh_settings();
        let Some(host) = self.host.get() else {
            return;
        };
        let host = Arc::clone(host);
        let root = root.to_owned();
        // One sweep at a time: two sessions that finish together would otherwise both walk
        // the workspace through the same retrieve store.
        let _serialised = self.reindexing.lock().await;
        // Timed from here, so a run waiting for the one before it is not charged its wait.
        let started = std::time::Instant::now();
        // Opening the workspace is itself blocking work, and it is where the exclusive
        // retrieve-store lock is taken, so it happens here rather than inside the sweep.
        let backend = match host.backend_at(&root).await {
            Ok(backend) => backend,
            Err(err) => {
                crate::instrument::reindexed(started, false);
                tracing::warn!(root = %root.display(), "re-indexing after a session failed: {err}");
                return;
            }
        };
        let state = Arc::clone(backend.state());
        let result = tokio::task::spawn_blocking(move || state.sync_retrieve()).await;
        crate::instrument::reindexed(started, matches!(result, Ok(Ok(_))));
        match result {
            Ok(Ok(report)) => {
                tracing::debug!(root = %root.display(), ?report, "re-indexed after a session");
            }
            Ok(Err(err)) => {
                tracing::warn!(root = %root.display(), "re-indexing after a session failed: {err}");
            }
            Err(err) => {
                tracing::warn!(root = %root.display(), "re-indexing after a session failed: {err}");
            }
        }
    }

    /// Run an embedding pass over `root` in the background, after the pass already
    /// waiting for it if there is one (#187).
    ///
    /// The pass reads the vector files that arrived, and computes the missing ones this
    /// host is responsible for: the files whose winning version this replica wrote, and —
    /// on the primary device — every other one. It never holds up the caller.
    pub fn request_embed(self: &Arc<Self>, root: &Path) {
        let Ok(key) = root.canonicalize() else {
            return;
        };
        let runtime = Arc::clone(self);
        self.spawn_background(async move {
            if !runtime.embed_wanted.lock().await.insert(key.clone()) {
                return;
            }
            let _one_at_a_time = runtime.embedding.lock().await;
            // Taken off the list as the pass starts: a change during the pass asks again.
            runtime.embed_wanted.lock().await.remove(&key);
            runtime.embed_running.lock().await.insert(key.clone());
            runtime.embed_pass(&key).await;
            runtime.embed_running.lock().await.remove(&key);
        });
    }

    /// One embedding pass over `key`. Best-effort, like the re-index: logged, never fatal.
    async fn embed_pass(&self, key: &Path) {
        let Some(host) = self.host.get() else {
            return;
        };
        let (workspace_id, replica) = {
            let synced = self.synced.lock().await;
            match synced.get(key) {
                Some(entry) => (entry.workspace_id, Arc::clone(&entry.replica)),
                None => return,
            }
        };
        let backend = match host.backend_at(key).await {
            Ok(backend) => backend,
            Err(err) => {
                tracing::warn!(root = %key.display(), "embedding pass: {err}");
                return;
            }
        };
        let state = Arc::clone(backend.state());
        if let Err(err) = state
            .refresh_embedder_at(self.bridge_endpoint.clone())
            .await
        {
            tracing::warn!(root = %key.display(), "embedding pass: {err}");
            return;
        }
        if state.embedder().is_none() {
            return;
        }
        let primary = self.is_primary(workspace_id).await;
        // The replica's view, read once: which content each path has, and which of those
        // versions this replica wrote.
        let grace_ms = self.backfill_timing().grace.as_millis() as u64;
        let now_ms = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map_or(0, |d| d.as_millis() as u64);
        let (mine, settled, live) = {
            let replica = replica.lock().await;
            let me = replica.replica_id();
            let states = match replica.states() {
                Ok(states) => states,
                Err(err) => {
                    tracing::warn!(root = %key.display(), "embedding pass: {err}");
                    return;
                }
            };
            let mut mine = HashMap::new();
            // Other devices' versions old enough for the primary to fill in.
            let mut settled = HashMap::new();
            let mut live = std::collections::HashSet::new();
            for (path, path_state) in states {
                let winner = path_state.winner();
                let Some(hash) = winner.content.hash() else {
                    continue;
                };
                let hash = hash.to_hex();
                if winner.dot.replica == me {
                    mine.insert(path, hash.clone());
                } else if now_ms.saturating_sub(winner.hlc.wall_ms) >= grace_ms {
                    settled.insert(path, hash.clone());
                }
                live.insert(hash);
            }
            (mine, settled, live)
        };
        let root = key.to_owned();
        let result = tokio::task::spawn_blocking(move || {
            // The author embeds its own versions at once; the primary device fills in other
            // devices' versions once they have settled, racing nobody.
            let policy = |rel: &str, hash: &str| {
                mine.get(rel).is_some_and(|h| h.as_str() == hash)
                    || (primary && settled.get(rel).is_some_and(|h| h.as_str() == hash))
            };
            let embedded = state.embed_pending_with(&policy, PASS_BATCH)?;
            let removed = if primary {
                state.remove_stale_vectors(&live, STALE_VECTOR_AGE)
            } else {
                0
            };
            Ok::<_, sapphire_workspace::Error>((embedded, removed))
        })
        .await;
        match result {
            Ok(Ok((embedded, removed))) if embedded > 0 || removed > 0 => tracing::info!(
                root = %root.display(),
                embedded,
                removed,
                "embedding pass"
            ),
            Ok(Ok(_)) => {}
            Ok(Err(err)) => tracing::warn!(root = %root.display(), "embedding pass: {err}"),
            Err(err) => tracing::warn!(root = %root.display(), "embedding pass: {err}"),
        }
    }

    /// Open a session with every peer that will take one.
    pub async fn sync_now(self: &Arc<Self>, root: &Path) -> Result<()> {
        let Ok(key) = root.canonicalize() else {
            return Ok(());
        };
        let workspace_id = {
            let synced = self.synced.lock().await;
            match synced.get(&key) {
                Some(entry) => entry.workspace_id,
                None => return Ok(()),
            }
        };
        let peers = self.peers().await?;
        let me = self.device_id().await?;

        // The same direction rule the dial loop follows: a host only dials peers with a
        // greater device id. Dialling the other way from here would reintroduce the
        // dial-both-ways deadlock the loop was cured of, just on a different trigger.
        // Inside a star, a pair with neither hub in it does not link at all.
        let roles = peers.roles_for(workspace_id).cloned();
        // Every peer at once, so one that is offline costs the others nothing (#228). On
        // this task, not spawned: cancelling the caller — the server's shutdown aborting the
        // watcher — must drop every dial with it, since each one pins the runtime.
        let mut dials: Vec<Dial<'_>> = Vec::new();
        for peer in peers
            .peers
            .iter()
            .filter(|p| topology::link(me, p.device_id, roles.as_ref()) == topology::Link::Dial)
            .cloned()
            .collect::<Vec<_>>()
        {
            // A peer that already has a live session is caught up by it: a second exchange
            // on the same workspace would be redundant, and a second stream between the same
            // pair is exactly what the tie-break cannot settle.
            if let Some(live) = self.live_peers(&key).await
                && live.has(&peer.device_id).await
            {
                continue;
            }
            // One dial per peer at a time. `sync_now` runs beside the dial loop, and two
            // overlapping outbound dials to one peer are the one case the tie-break rule
            // cannot resolve — each end would see its own stream as `outbound`. Whoever
            // arrives second simply leaves it to the next pass. The table is made here if
            // the dial loop has not yet: the first dial after enabling must not depend on
            // a tick that has not fired.
            let Some((_, table)) = self.live_peers_or_create(&key).await else {
                continue;
            };
            if !table.begin_dial(peer.device_id).await {
                continue;
            }
            let key = &key;
            dials.push(Box::pin(async move {
                // A peer that does not host this workspace refuses, which is normal and cheap.
                let result = self
                    .sync_one(key, &peer.device_id, peer.name.clone(), workspace_id)
                    .await;
                table.end_dial(peer.device_id).await;
                if let Err(err) = result {
                    tracing::debug!(peer = %peer.name, "no session: {err}");
                }
            }));
        }
        join_all(dials).await;
        Ok(())
    }

    /// Dial one peer and adopt the stream as a live session.
    ///
    /// The one-shot path (`run_session`) would close the stream the moment the exchange
    /// finished — while the accepting peer keeps its half open and pushes into it, a session
    /// that dies with nothing reading it. So the dialled side is adopted live too, and both
    /// ends of a pair agree that a session stays open.
    async fn sync_one(
        self: &Arc<Self>,
        key: &Path,
        device: &GrainId,
        name: String,
        workspace_id: GrainId,
    ) -> Result<()> {
        let stream = self.open_stream(workspace_id, *device).await?;
        self.adopt_live_session(stream, key, *device, workspace_id)
            .await
            .map_err(|err| {
                tracing::warn!(peer = %name, "a live session could not be started: {err}");
                err
            })
    }

    /// Ask the bridge for a stream to `device`, giving up after `open_timeout`.
    async fn open_stream(
        &self,
        workspace_id: GrainId,
        device: GrainId,
    ) -> Result<sapphire_ipc::RawStream> {
        match tokio::time::timeout(
            self.open_timeout,
            self.bridge()?.open_stream(workspace_id, device),
        )
        .await
        {
            Ok(opened) => Ok(opened?),
            Err(_) => Err(Error::Bridge(format!(
                "no stream to {device} within {} s; it may be offline",
                self.open_timeout.as_secs_f32()
            ))),
        }
    }

    /// Answer the bridge's announcements, following the connection across reconnections
    /// (#244). Runs until the task is aborted.
    pub async fn run(self: Arc<Self>) -> Result<()> {
        let mut slot = self.bridge.subscribe();
        loop {
            let bridge = slot.borrow_and_update().clone();
            match bridge {
                Some(bridge) if !bridge.is_closed() => self.answer(&bridge, &mut slot).await,
                // Wait for a connection. The runtime holds the sender, so this never ends.
                _ => {
                    if slot.changed().await.is_err() {
                        return Ok(());
                    }
                }
            }
        }
    }

    /// Answer `bridge`'s announcements until it closes or another connection replaces it.
    async fn answer(
        self: &Arc<Self>,
        bridge: &Arc<BridgeClient>,
        slot: &mut tokio::sync::watch::Receiver<Option<Arc<BridgeClient>>>,
    ) {
        let early = self
            .subscribed
            .lock()
            .expect("subscribed")
            .take_if(|(b, _)| Arc::ptr_eq(b, bridge));
        let mut incoming = match early {
            Some((_, incoming)) => incoming,
            None => bridge.incoming(),
        };
        loop {
            let received = tokio::select! {
                received = incoming.recv() => received,
                () = bridge.closed() => return,
                _ = slot.changed() => return,
            };
            let announcement = match received {
                Ok(a) => a,
                Err(tokio::sync::broadcast::error::RecvError::Lagged(n)) => {
                    tracing::warn!(missed = n, "fell behind on bridge announcements");
                    continue;
                }
                Err(tokio::sync::broadcast::error::RecvError::Closed) => return,
            };

            let found = {
                let synced = self.synced.lock().await;
                synced
                    .iter()
                    .find(|(_, s)| s.workspace_id == announcement.workspace_id)
                    .map(|(root, s)| (root.clone(), Arc::clone(&s.replica)))
            };
            // `replica` is still what the accepted session shares, and `root` is where its
            // files and its index live.
            let Some((root, replica)) = found else {
                // The bridge routed to us for a workspace we no longer hold. Not fatal:
                // ignore it and let the ticket expire.
                tracing::debug!(
                    workspace = %announcement.workspace_id,
                    "an announcement for a workspace this server does not hold"
                );
                continue;
            };

            let bridge = Arc::clone(bridge);
            let workspace_id = announcement.workspace_id;
            let peer = announcement.peer_device_id;
            let driver = Arc::clone(self);
            self.spawn_background(async move {
                let stream = match bridge.accept_stream(announcement.ticket).await {
                    Ok(stream) => stream,
                    Err(err) => {
                        tracing::warn!("could not claim an announced stream: {err}");
                        return;
                    }
                };
                if driver.should_skip(workspace_id, peer).await {
                    tracing::debug!(%peer, "dropped an inbound session: both ends are outside this workspace's star");
                    return;
                }
                // Live, not one-shot: the peer that dialled is keeping this session open and
                // pushing what it commits, so this side has to keep reading. A session that
                // closed after the exchange would leave the dialler pushing into a stream
                // nobody reads.
                let (_, session) = match sapphire_framework_session::open_live_session(
                    stream,
                    Arc::clone(&replica),
                    workspace_id,
                )
                .await
                {
                    Ok(started) => started,
                    Err(err) => {
                        crate::instrument::sync_session(false);
                        tracing::warn!("an inbound session failed: {err}");
                        return;
                    }
                };
                let Some((key, table)) = driver.live_peers_or_create(&root).await else {
                    return;
                };
                let acknowledged = session.acknowledged();
                // The peer dialled: this stream survives only if the *peer* has the lower
                // device id. A stream that loses is dropped, which closes it, and the
                // connection ends tidily rather than being left half-read.
                let Some(updates) = table.insert(peer, session, false).await else {
                    return;
                };
                driver.watch_delivery(key.clone(), peer, acknowledged);
                table.catch_up(peer, &replica).await;
                crate::instrument::sync_session(true);
                // This is the receiving side: the files are on disk now and nobody else will
                // index them.
                driver.reindex(&key).await;
                driver.request_embed(&key);
                driver.spawn_reader(table, updates, key, peer);
            });
        }
    }

    /// Make the first attempt to reach the bridge, when the runtime has a
    /// [`BridgeConnector`] and is not connected yet.
    ///
    /// No bridge running is not an error: the server serves without sync, and
    /// [`keep_connected`](Self::keep_connected) picks the bridge up once it starts. Any
    /// other failure — a bridge of another API version, say — is, and stops the start.
    pub async fn connect(self: &Arc<Self>) -> Result<()> {
        let Some(connector) = &self.connector else {
            return Ok(());
        };
        if self.is_connected() {
            return Ok(());
        }
        match connector.connect().await? {
            Some(client) => self.attach(client).await,
            None => tracing::warn!(
                "no sapphire-bridge is running; serving without sync until it starts"
            ),
        }
        Ok(())
    }

    /// Stay connected to the bridge: wait for the connection to close, then reach the
    /// bridge again with the runtime's [`BridgeConnector`], backing off from
    /// [`RECONNECT_BACKOFF`] to [`RECONNECT_BACKOFF_MAX`] while it cannot (#244).
    /// [`reconnect_now`](Self::reconnect_now) ends a wait early and starts the backoff over.
    ///
    /// Runs until the task is aborted, which is what the app server's shutdown does with
    /// it. Without a connector it only marks the runtime disconnected once the connection
    /// closes, and returns.
    pub async fn keep_connected(self: Arc<Self>) {
        let mut attempt = 0;
        // Whether this outage's failure has been logged at warn: once per outage, however
        // often `reconnect_now` starts the backoff over.
        let mut warned = false;
        loop {
            if let Ok(bridge) = self.bridge() {
                bridge.closed().await;
                tracing::warn!("lost the connection to sapphire-bridge; reconnecting");
                self.bridge.send_if_modified(|slot| {
                    let same = slot.as_ref().is_some_and(|b| Arc::ptr_eq(b, &bridge));
                    if same {
                        *slot = None;
                    }
                    same
                });
                attempt = 0;
                warned = false;
                continue;
            }
            let Some(connector) = &self.connector else {
                return;
            };
            match connector.connect().await {
                Ok(Some(client)) => {
                    tracing::info!("connected to sapphire-bridge");
                    self.attach(client).await;
                    attempt = 0;
                    warned = false;
                    continue;
                }
                Ok(None) => tracing::debug!("no sapphire-bridge is running yet"),
                // Logged once per outage, not once per attempt.
                Err(err) if !warned => {
                    tracing::warn!("could not connect to sapphire-bridge: {err}");
                    warned = true;
                }
                Err(err) => tracing::debug!("could not connect to sapphire-bridge: {err}"),
            }
            tokio::select! {
                () = tokio::time::sleep(reconnect_backoff(self.reconnect_first, attempt)) => {
                    attempt = attempt.saturating_add(1);
                }
                () = self.reconnect_now.notified() => attempt = 0,
            }
        }
    }

    /// Start using `client`, and bring the bridge up to date: it knows nothing of this
    /// server yet, whether it has just started or this server has.
    async fn attach(self: &Arc<Self>, client: BridgeClient) {
        // Swapped under the pending lock, so an `enable` racing this either sees the new
        // connection or leaves its workspace for the drain below.
        let client = Arc::new(client);
        *self.subscribed.lock().expect("subscribed") =
            Some((Arc::clone(&client), client.incoming()));
        let waiting = {
            let mut pending = self.pending.lock().await;
            self.bridge.send_replace(Some(client));
            std::mem::take(&mut *pending)
        };
        // What was synced before the connection was lost.
        if !self.synced.lock().await.is_empty()
            && let Err(err) = self.reregister().await
        {
            tracing::warn!("could not register with sapphire-bridge: {err}");
        }
        // What was asked for while it was away. `enable` registers and dials each one.
        for root in waiting.into_keys() {
            if let Err(err) = self.enable(&root).await {
                tracing::warn!(root = %root.display(), "could not start sync: {err}");
            }
        }
    }

    /// Every synced root, for the watcher.
    pub async fn roots(&self) -> Vec<PathBuf> {
        self.synced.lock().await.keys().cloned().collect()
    }

    /// The live session table for `root`, if this runtime syncs it.
    ///
    /// `None` is not an error: a dial, a push or a drop for a workspace this server does not
    /// hold is a no-op, and every caller below treats it that way.
    async fn live_peers(&self, root: &Path) -> Option<Arc<LivePeers>> {
        let key = root.canonicalize().ok()?;
        self.live.lock().await.get(&key).cloned()
    }

    /// The live session table for `root`, creating it if this runtime syncs it.
    ///
    /// The two locks are taken one after the other, never nested: `synced` to decide whether
    /// the workspace is ours at all, then `live` to get or make its table.
    async fn live_peers_or_create(&self, root: &Path) -> Option<(PathBuf, Arc<LivePeers>)> {
        let key = root.canonicalize().ok()?;
        if !self.synced.lock().await.contains_key(&key) {
            return None;
        }
        // A table is built by whoever walks the workspace first, and it needs this host's
        // device id — the tie-break between two simultaneous streams is decided from it.
        let me = self.device_id().await.ok()?;
        let mut live = self.live.lock().await;
        let peers = Arc::clone(
            live.entry(key.clone())
                .or_insert_with(|| Arc::new(LivePeers::new(me))),
        );
        Some((key, peers))
    }

    /// Push `updates` to every open session for `root`.
    ///
    /// Called with what a scan recorded. `from` is always `None` here: a local commit came
    /// from this host, so no session is excluded. The forwarding path, which does name a
    /// source, is [`LivePeers::fan_out`](live::LivePeers::fan_out) called directly by the
    /// reader side of a session.
    ///
    /// Best-effort by contract: the commit already happened, and a peer that is gone is the
    /// dialer's problem, not the writer's.
    pub async fn after_commit(&self, root: &Path, updates: Vec<PathUpdate>) {
        if updates.is_empty() {
            return;
        }
        if let Some(peers) = self.live_peers(root).await {
            peers.fan_out(&updates, None).await;
        }
    }

    /// The devices with an open live session for `root`.
    ///
    /// Used by tests to wait for the dialer; empty for a workspace this server does not hold.
    pub async fn live_session_devices(&self, root: &Path) -> Vec<GrainId> {
        match self.live_peers(root).await {
            Some(peers) => peers.devices().await,
            None => Vec::new(),
        }
    }

    /// End the first dials [`enable`](SyncRuntime::enable) left running, waiting until
    /// they are gone.
    ///
    /// For the app server's shutdown: each one pins the runtime, and with it the replica
    /// stores, which are redb databases that stay locked — a dial outliving its server
    /// would lock the next one out.
    pub async fn stop_dials(&self) {
        let mut dials = std::mem::take(&mut *self.first_dials.lock().expect("first dials"));
        dials.abort_all();
        while dials.join_next().await.is_some() {}
    }

    /// End the runtime's background work — inbound sessions, live-session readers and
    /// embedding passes — and wait until it has let go.
    ///
    /// The server's shutdown calls this, so a server restarted in the same process can open
    /// the replica stores again at once rather than whenever the last straggler finished
    /// (#235). An embedding pass already inside its blocking section runs that section to
    /// the end; it holds the workspace's index, never a replica.
    pub async fn stop_background(&self) {
        let mut background =
            std::mem::take(&mut *self.background.lock().expect("background tasks"));
        background.abort_all();
        while background.join_next().await.is_some() {}
    }

    /// Wait for the first dials [`enable`](SyncRuntime::enable) left running to finish on
    /// their own: for a test that must not race them.
    #[cfg(test)]
    async fn settle_dials(&self) {
        let mut dials = std::mem::take(&mut *self.first_dials.lock().expect("first dials"));
        while dials.join_next().await.is_some() {}
    }

    /// Close every open live session, as if the connections had been cut.
    ///
    /// Called for a workspace being disabled, and by tests that want to watch the dialer
    /// rebuild what was dropped.
    pub async fn drop_connections(&self) {
        let tables: Vec<Arc<LivePeers>> = self.live.lock().await.values().cloned().collect();
        for peers in tables {
            peers.drop_connections().await;
        }
    }

    /// Keep a live session open to every peer, for every synced workspace.
    ///
    /// One walk per [`DIAL_INTERVAL`](live::DIAL_INTERVAL): for each synced workspace, ask
    /// the bridge who is in the workgroup, and open a session to every peer that is connected
    /// and does not already have one.
    ///
    /// A peer that cannot be reached is retried on a per-peer exponential backoff capped at
    /// [`DIAL_BACKOFF_MAX`] — one failed attempt per walk, not one per spin — and the counter
    /// resets when a session opens. An unreachable peer is therefore no obstacle to the
    /// others: its dial fails, the walk moves on, and the next workspace is dialled.
    ///
    /// Runs until the task is aborted, which is what the app server's shutdown does with it.
    /// It is allowed to fail like [`run`](SyncRuntime::run): only sync stops, and the app
    /// server keeps serving files (spec §10).
    pub async fn dial_loop(self: Arc<Self>) -> Result<()> {
        let mut tick = tokio::time::interval(live::DIAL_INTERVAL);
        // A missed tick is a late walk, not a burst of them: the interval is a pace, not a
        // quota to make up.
        tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
        let mut waiting: HashMap<GrainId, std::time::Instant> = HashMap::new();
        let mut failures: HashMap<GrainId, u32> = HashMap::new();
        // The workspaces this host was the primary device of at the last walk.
        let mut primary_of: std::collections::HashSet<GrainId> = Default::default();
        // When the primary device last ran a backfill pass with nothing prompting it.
        let mut last_backfill = std::time::Instant::now();
        loop {
            tick.tick().await;
            let roots = self.roots().await;
            if roots.is_empty() {
                continue;
            }
            let peers = match self.peers().await {
                Ok(peers) => peers,
                // The bridge is down or has no workgroup: nothing to dial, and the next walk
                // asks again. Not an error for the caller — the app server is still serving.
                Err(err) => {
                    tracing::debug!("no peers to dial: {err}");
                    continue;
                }
            };
            let me = match self.device_id().await {
                Ok(me) => me,
                Err(err) => {
                    tracing::warn!("no device id, so nobody to dial: {err}");
                    continue;
                }
            };
            // A host that has just become a workspace's primary device owes it the vectors
            // nobody else computes (#187): files that arrived before the election settled
            // would otherwise wait for the next change.
            let ids: Vec<(PathBuf, GrainId)> = self
                .synced
                .lock()
                .await
                .iter()
                .map(|(root, entry)| (root.clone(), entry.workspace_id))
                .collect();
            // And every so often anyway: an author that never embedded leaves files only the
            // primary will fill in, and nothing else would prompt it (#188).
            let backfill_due = last_backfill.elapsed() >= self.backfill_timing().interval;
            if backfill_due {
                last_backfill = std::time::Instant::now();
            }
            for (root, workspace_id) in ids {
                let primary = peers
                    .roles_for(workspace_id)
                    .is_some_and(|r| r.primary == Some(me));
                if !primary {
                    primary_of.remove(&workspace_id);
                } else if primary_of.insert(workspace_id) || backfill_due {
                    self.request_embed(&root);
                }
            }
            let now = std::time::Instant::now();
            // Dial only peers whose device id is greater than ours. Every device dialling
            // every other can deadlock: two hosts that dial each other at once each hold
            // their replica's lock across their own exchange and wait for the other's
            // hello, which the other's dialler holds the lock for. With one direction per
            // pair the wait chain is a DAG — a higher-id host never dials, so it never
            // holds its lock out while a lower-id host's exchange needs it. The lower host
            // dials within `DIAL_INTERVAL`, and the exchange is symmetric, so nothing is
            // lost; the tie-break is left as the safety net it is.
            for root in roots {
                let Some((key, table)) = self.live_peers_or_create(&root).await else {
                    continue;
                };
                let workspace_id = {
                    let synced = self.synced.lock().await;
                    match synced.get(&key) {
                        Some(entry) => entry.workspace_id,
                        None => continue,
                    }
                };
                let roles = peers.roles_for(workspace_id);
                // Entering a star closes what it no longer needs. The hubs carry it all —
                // once one of them is reachable from here (#192).
                if topology::may_close_skipped(me, roles, &table.devices().await) {
                    table
                        .retain(|d| topology::link(me, *d, roles) != topology::Link::Skip)
                        .await;
                }
                for peer in peers.peers.iter().filter(|p| {
                    p.connected && topology::link(me, p.device_id, roles) == topology::Link::Dial
                }) {
                    let device = peer.device_id;
                    if table.has(&device).await {
                        // Already talking; a redial would drop a working session.
                        failures.remove(&device);
                        waiting.remove(&device);
                        continue;
                    }
                    if let Some(until) = waiting.get(&device)
                        && now < *until
                    {
                        // Backing off after a failure; the walk skips it until then.
                        continue;
                    }
                    // The same claim `sync_now` takes: this walk and it can overlap, and a
                    // double dial in one direction is the one race the tie-break cannot
                    // settle. Skipping is free — the next pass, or `sync_now` itself, dials.
                    if !table.begin_dial(device).await {
                        continue;
                    }
                    let opened = self.open_stream(workspace_id, device).await;
                    if opened.is_err() {
                        table.end_dial(device).await;
                    }
                    match opened {
                        Ok(stream) => {
                            let adopted = self
                                .adopt_live_session(stream, &root, device, workspace_id)
                                .await;
                            // The claim is out either way: the attempt is over, and a
                            // session that died mid-exchange is re-dialled next pass.
                            table.end_dial(device).await;
                            match adopted {
                                Ok(()) => {
                                    failures.remove(&device);
                                    waiting.remove(&device);
                                }
                                Err(err) => {
                                    tracing::debug!(
                                        peer = %peer.name,
                                        err = %err,
                                        "a live session could not be started"
                                    );
                                    let count = failures.entry(device).or_insert(0);
                                    *count = count.saturating_add(1);
                                    waiting.insert(device, now + dial_backoff(*count));
                                }
                            }
                        }
                        Err(err) => {
                            // Normal and cheap: a peer that does not host this workspace
                            // refuses, and one that is unreachable cannot be opened.
                            tracing::debug!("no stream to a peer: {err}");
                            let count = failures.entry(device).or_insert(0);
                            *count = count.saturating_add(1);
                            waiting.insert(device, now + dial_backoff(*count));
                        }
                    }
                }
            }
        }
    }

    /// Take ownership of an opened stream: run the exchange, then keep it live.
    ///
    /// The session is stored under `device` and its reader pumped until the stream ends. A
    /// half-open or refused stream is not stored; the dialer retries it with backoff.
    async fn adopt_live_session(
        self: &Arc<Self>,
        stream: sapphire_ipc::RawStream,
        root: &Path,
        device: GrainId,
        workspace_id: GrainId,
    ) -> Result<()> {
        let replica = self.replica_of(root).await?;
        let (_, session) = sapphire_framework_session::open_live_session(
            stream,
            Arc::clone(&replica),
            workspace_id,
        )
        .await
        .map_err(|e| {
            crate::instrument::sync_session(false);
            Error::Sync(e.to_string())
        })?;
        let Some((key, table)) = self.live_peers_or_create(root).await else {
            return Ok(());
        };
        let acknowledged = session.acknowledged();
        // This stream was dialled by us, so it is the one the pair keeps if this host's
        // device id is the lower of the two. If it lost the tie-break the session is
        // dropped here, which closes the stream, and the surviving one is already in the
        // table.
        let Some(updates) = table.insert(device, session, true).await else {
            return Ok(());
        };
        self.watch_delivery(key.clone(), device, acknowledged);
        table.catch_up(device, &replica).await;
        crate::instrument::sync_session(true);
        // The session wrote files during the exchange; the index has to catch up before the
        // workspace is searchable again.
        self.reindex(&key).await;
        self.request_embed(&key);
        self.spawn_reader(Arc::clone(&table), updates, key, device);
        Ok(())
    }

    /// Forward what one session receives, and re-index it.
    ///
    /// This is the loop that makes A → S → B work: a batch that arrived on S's session with A
    /// is applied (the session crate does that before publishing it), the workspace behind
    /// `key` is re-indexed, and the batch is handed to every *other* session whose peer lacks
    /// it — `from` is the device it came from, so it never goes back to A.
    fn spawn_reader(
        self: &Arc<Self>,
        table: Arc<LivePeers>,
        mut updates: broadcast::Receiver<Vec<PathUpdate>>,
        key: PathBuf,
        from: GrainId,
    ) {
        let driver = Arc::clone(self);
        self.spawn_background(async move {
            loop {
                match updates.recv().await {
                    Ok(batch) => {
                        // The files are on disk now (the session guarantees it before
                        // publishing), so the index is behind them until the sweep below.
                        driver.reindex(&key).await;
                        driver.request_embed(&key);
                        table.fan_out(&batch, Some(from)).await;
                    }
                    // A burst bigger than the channel: the newest is kept and the gap is
                    // covered by the next session's exchange. Losing a batch here cannot lose
                    // an entry — the peer's vector does not cover it, so the next dial or push
                    // carries it again.
                    Err(broadcast::error::RecvError::Lagged(missed)) => {
                        tracing::debug!(
                            device = %from,
                            missed,
                            "fell behind on a live session; the next exchange catches up"
                        );
                    }
                    // The session closed. Its table entry is reaped by the next fan-out or
                    // dial pass, and the dialer opens a fresh one.
                    Err(broadcast::error::RecvError::Closed) => {
                        return;
                    }
                }
            }
        });
    }

    /// Follow what `device`'s session for `key` is known to have, and announce a delivery
    /// when it comes to cover every edit this device made.
    ///
    /// Checked at once too: the initial exchange may already have carried everything. Ends
    /// with the session, which closes the watch.
    fn watch_delivery(
        self: &Arc<Self>,
        key: PathBuf,
        device: GrainId,
        mut acknowledged: tokio::sync::watch::Receiver<sapphire_sync::VersionVector>,
    ) {
        let driver = Arc::clone(self);
        self.spawn_background(async move {
            loop {
                let known = acknowledged.borrow_and_update().clone();
                driver.check_delivered(&key, device, &known).await;
                if acknowledged.changed().await.is_err() {
                    return;
                }
            }
        });
    }

    /// Announce [`BackendEvent::SyncDelivered`] for `key` if `known` — what `device` is known to have —
    /// covers every edit this device has made and that was not announced yet.
    async fn check_delivered(
        &self,
        key: &Path,
        device: GrainId,
        known: &sapphire_sync::VersionVector,
    ) {
        let Some(replica) = self
            .synced
            .lock()
            .await
            .get(key)
            .map(|entry| Arc::clone(&entry.replica))
        else {
            return;
        };
        let (me, own) = {
            let replica = replica.lock().await;
            (replica.replica_id(), own_counter(&replica))
        };
        if known.get(&me) < own {
            return;
        }
        {
            let mut synced = self.synced.lock().await;
            let Some(entry) = synced.get_mut(key) else {
                return;
            };
            // Already announced, by this peer or another: a second peer catching up says
            // nothing new.
            if own <= entry.delivered_through {
                return;
            }
            entry.delivered_through = own;
        }
        let device_name = self
            .last_peers
            .lock()
            .await
            .as_ref()
            .and_then(|answer| answer.peers.iter().find(|p| p.device_id == device))
            .map_or_else(|| device.to_string(), |peer| peer.name.clone());
        tracing::info!(root = %key.display(), peer = %device_name, "local edits delivered");
        if let Some(host) = self.host.get() {
            host.emit(
                key,
                BackendEvent::SyncDelivered {
                    device_id: device,
                    device_name,
                },
            );
        }
    }

    /// The replica [`disable`](Self::disable) let go of for `key`, if something still holds it
    /// open.
    fn reclaim(&self, key: &Path) -> Reclaimed {
        let stopped = self.stopped.lock().expect("stopped replicas").remove(key);
        match stopped {
            None => Reclaimed::Closed,
            Some(replica) => replica
                .upgrade()
                .map_or(Reclaimed::Closing, Reclaimed::Open),
        }
    }

    /// The replica of a synced `root`.
    async fn replica_of(&self, root: &Path) -> Result<Arc<Mutex<Replica>>> {
        let key = root.canonicalize().map_err(Error::Io)?;
        let synced = self.synced.lock().await;
        match synced.get(&key) {
            Some(entry) => Ok(Arc::clone(&entry.replica)),
            None => Err(Error::UnknownWorkspace(key, self.ctx.app_name)),
        }
    }

    /// Watch the synced roots and, on a debounced report, scan and dial.
    ///
    /// This is the safety net for everything the app server did not do itself — a file
    /// edited in an editor, a `git checkout`. The exact path is [`scan`] called from
    /// `handlers.rs` right after a write the server made. A scan that finds nothing is
    /// cheap; a scan that is skipped loses an edit until the next one.
    ///
    /// Returns when the reporting channel closes — that is, when the [`Watcher`] this
    /// runtime holds is dropped, which happens when the runtime itself is.
    ///
    /// [`scan`]: SyncRuntime::scan
    pub async fn watch_changes(self: Arc<Self>) -> Result<()> {
        let (tx, mut rx) = mpsc::channel(64);
        let watcher = Arc::new(Watcher::start(self.roots().await, tx)?);
        let _ = self.watcher.set(Arc::clone(&watcher));

        // A root enabled between `roots()` above and the `set` just now was not in the
        // starting set, and `enable` could not reach a watcher that did not exist yet.
        // Watching the current set again is idempotent on `notify`'s side.
        for root in self.roots().await {
            if let Err(err) = watcher.watch(&root) {
                tracing::warn!(root = %root.display(), "could not watch: {err}");
            }
        }

        while let Some(root) = rx.recv().await {
            // An edit of the marker's `config.toml` changes the workspace's settings.
            self.refresh_settings();
            // Logged, not fatal: a failed scan or dial leaves the replica behind, and the
            // next report tries again. Neither may take the app server down (spec §10).
            if let Err(err) = self.scan(&root).await {
                tracing::warn!(root = %root.display(), "scan after a local edit failed: {err}");
            }
            self.request_embed(&root);
            if let Err(err) = self.sync_now(&root).await {
                tracing::warn!(root = %root.display(), "dial after a local edit failed: {err}");
            }
        }
        Ok(())
    }

    /// Tell the bridge the complete current set. A registration is not a delta.
    async fn reregister(&self) -> Result<()> {
        let workspaces: Vec<WorkspaceRegistration> = {
            let synced = self.synced.lock().await;
            synced
                .iter()
                .map(|(root, entry)| WorkspaceRegistration {
                    workspace_id: entry.workspace_id,
                    root: root.clone(),
                })
                .collect()
        };
        self.bridge()?
            .register(RegisterParams {
                app_name: self.ctx.app_name.to_owned(),
                exe_path: self.exe_path.clone(),
                managed_by: wire_managed_by(self.managed_by),
                workspaces,
            })
            .await
            .map_err(|e| Error::Bridge(e.to_string()))?;
        Ok(())
    }

    /// Whether the last roles the bridge reported say this pair does not link.
    ///
    /// Reads the cached device id only: asking the bridge here would register with an empty
    /// workspace list, racing `enable`'s registration. `false` (accept) when it is unknown.
    async fn should_skip(&self, workspace_id: GrainId, peer: GrainId) -> bool {
        let Some(&me) = self.device_id.get() else {
            return false;
        };
        let cached = self.last_peers.lock().await;
        let roles = cached.as_ref().and_then(|p| p.roles_for(workspace_id));
        topology::link(me, peer, roles) == topology::Link::Skip
    }

    /// Whether this host is `workspace_id`'s primary device, by the last roles the
    /// bridge reported. `false` when the bridge has not answered yet, or when this host's
    /// device id is not known yet (no workspace was enabled). Reads the cached id only:
    /// asking the bridge here would register with an empty workspace list, racing
    /// `enable`'s registration.
    pub async fn is_primary(&self, workspace_id: GrainId) -> bool {
        let Some(&me) = self.device_id.get() else {
            return false;
        };
        let cached = self.last_peers.lock().await;
        cached
            .as_ref()
            .and_then(|p| p.roles_for(workspace_id))
            .is_some_and(|r| r.primary == Some(me))
    }

    /// This host's device id, once the bridge has told it (#250).
    ///
    /// `None` until this runtime first registered — no workspace was enabled yet, or the
    /// bridge was not connected when one was. Never asks the bridge: a registration made
    /// only to learn the id would carry an empty workspace list, and briefly clear this
    /// application's routes.
    pub fn this_device(&self) -> Option<GrainId> {
        self.device_id.get().copied()
    }

    /// The workgroup's devices, this host's included, and the roles the bridge assigns per
    /// workspace (#250). Asked of the bridge each time; [`is_primary`](Self::is_primary)
    /// reads the answer afterwards.
    ///
    /// [`Error::BridgeDisconnected`] while there is no connection to the bridge.
    pub async fn peers(&self) -> Result<PeersResult> {
        let peers = self
            .bridge()?
            .peers()
            .await
            .map_err(|e| Error::Bridge(e.to_string()))?;
        *self.last_peers.lock().await = Some(peers.clone());
        Ok(peers)
    }

    /// This host's device id, asked of the bridge once.
    async fn device_id(&self) -> Result<GrainId> {
        if let Some(id) = self.device_id.get() {
            return Ok(*id);
        }
        let result = self
            .bridge()?
            .register(RegisterParams {
                app_name: self.ctx.app_name.to_owned(),
                exe_path: self.exe_path.clone(),
                managed_by: wire_managed_by(self.managed_by),
                workspaces: Vec::new(),
            })
            .await
            .map_err(|e| Error::Bridge(e.to_string()))?;
        let _ = self.device_id.set(result.device_id);
        Ok(result.device_id)
    }

    /// `<workspace cache dir>/sync/`.
    fn state_dir(&self, root: &Path) -> Result<PathBuf> {
        let workspace = Workspace::from_root(self.ctx, root)?;
        Ok(workspace.cache_dir().join("sync"))
    }
}

/// One dial of [`SyncRuntime::sync_now`].
type Dial<'a> = std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send + 'a>>;

/// Run `dials` concurrently on the calling task, until every one has finished.
/// The counter of the last edit `replica` recorded itself.
fn own_counter(replica: &Replica) -> u64 {
    replica.vv().get(&replica.replica_id())
}

async fn join_all(mut dials: Vec<Dial<'_>>) {
    std::future::poll_fn(|cx| {
        dials.retain_mut(|dial| dial.as_mut().poll(cx).is_pending());
        if dials.is_empty() {
            std::task::Poll::Ready(())
        } else {
            std::task::Poll::Pending
        }
    })
    .await
}

/// The bridge API's copy of how this server was started, for [`RegisterParams`].
fn wire_managed_by(managed_by: ManagedBy) -> sapphire_bridge_api::ManagedBy {
    match managed_by {
        ManagedBy::Service => sapphire_bridge_api::ManagedBy::Service,
        ManagedBy::Spawned => sapphire_bridge_api::ManagedBy::Spawned,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::sync::testing::StubBridge;
    use crate::test_support;
    use sapphire_workspace::AppKind;
    use std::ffi::OsString;

    static CTX: AppContext = AppContext::new("sapphire-synctest");

    /// The env vars `CTX.init` reads.
    const DIR_VARS: [&str; 3] = [
        "SAPPHIRE_SYNCTEST_CACHE_DIR",
        "SAPPHIRE_SYNCTEST_DATA_DIR",
        "SAPPHIRE_SYNCTEST_CONFIG_DIR",
    ];

    /// Points the context's directories at the test's scratch tree, and restores the
    /// previous values when dropped — including while unwinding from a panic.
    struct EnvGuard {
        previous: [Option<OsString>; 3],
        _lock: std::sync::MutexGuard<'static, ()>,
    }

    impl Drop for EnvGuard {
        fn drop(&mut self) {
            // SAFETY: `self._lock` still serialises the environment; it is dropped only
            // after this method returns.
            for (name, previous) in DIR_VARS.iter().zip(self.previous.iter_mut()) {
                match previous.take() {
                    Some(value) => unsafe { std::env::set_var(name, value) },
                    None => test_support::remove(name),
                }
            }
        }
    }

    /// Everything one test needs, held for the test's whole body.
    ///
    /// The context is a `static` shared by this binary, so its directories are resolved from
    /// the environment by whichever test initialises it first. The environment lock is
    /// therefore held for the whole test, as in the crate's other test modules, and `_tmp`
    /// is declared before `_env` so the scratch tree is gone only once the environment no
    /// longer points at it.
    struct Fixture {
        _tmp: tempfile::TempDir,
        _env: EnvGuard,
        root: PathBuf,
        stub: StubBridge,
        runtime: Arc<SyncRuntime>,
    }

    impl Fixture {
        /// A second workspace root beside the first, marker directory and all.
        fn second_root(&self) -> PathBuf {
            let root = self._tmp.path().join("ws2");
            std::fs::create_dir_all(root.join(".sapphire-synctest")).unwrap();
            root.canonicalize().unwrap()
        }
    }

    /// How long the fixture's runtime waits on a stream to a peer.
    const FIXTURE_OPEN_TIMEOUT: std::time::Duration = std::time::Duration::from_millis(500);

    /// `n` peers this host dials (a greater device id), as `bridge.peers` lists them.
    fn dialled_peers(me: GrainId, n: usize) -> Vec<sapphire_bridge_api::PeerInfo> {
        std::iter::repeat_with(GrainId::random)
            .filter(|id| *id > me)
            .take(n)
            .enumerate()
            .map(|(i, device_id)| sapphire_bridge_api::PeerInfo {
                device_id,
                name: format!("offline-{i}"),
                node_id: format!("node-{i}"),
                connected: false,
                priority: 1,
                availability: None,
            })
            .collect()
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn enabling_answers_at_once_with_a_peer_that_never_answers() {
        // #228: the first dial waited on a peer that was offline, past the client's limit.
        let f = fixture().await;
        let _hung = f.stub.hang_streams().await;
        f.stub.set_peers(dialled_peers(f.stub.device_id, 1));

        let started = std::time::Instant::now();
        f.runtime.enable(&f.root).await.unwrap();
        assert!(
            started.elapsed() < FIXTURE_OPEN_TIMEOUT,
            "enable waited {:?} on the dial",
            started.elapsed()
        );
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn peers_that_never_answer_are_dialled_together_and_given_up_on() {
        let f = fixture().await;
        let _hung = f.stub.hang_streams().await;
        f.runtime.enable(&f.root).await.unwrap();
        // The first dial, with nobody to dial yet, must be over before the peers appear:
        // one still running would claim them, and `sync_now` would skip them all.
        f.runtime.settle_dials().await;
        f.stub.set_peers(dialled_peers(f.stub.device_id, 3));

        let started = std::time::Instant::now();
        f.runtime.sync_now(&f.root).await.unwrap();
        let took = started.elapsed();
        assert!(took >= FIXTURE_OPEN_TIMEOUT, "gave up early: {took:?}");
        assert!(
            took < FIXTURE_OPEN_TIMEOUT * 2,
            "three dials one after another: {took:?}"
        );
    }

    async fn fixture() -> Fixture {
        let (tmp, env, root) = scratch();
        let (stub, client) = StubBridge::start_in(tmp.path().join("run")).await;
        let runtime = Arc::new(
            SyncRuntime::new(&CTX, client, "/bin/true".into(), ManagedBy::Service)
                .with_open_timeout(FIXTURE_OPEN_TIMEOUT),
        );
        Fixture {
            _tmp: tmp,
            _env: env,
            root,
            stub,
            runtime,
        }
    }

    /// The bridge connections a [`connecting`] fixture's connector hands out, in order;
    /// empty is "no bridge is running".
    type Bridges = Arc<std::sync::Mutex<std::collections::VecDeque<BridgeClient>>>;

    /// A fixture whose runtime reaches the bridge through a connector, and has not yet.
    /// `f.stub`'s client is not handed out: push it to the returned queue for that.
    async fn connecting() -> (Fixture, Bridges, BridgeClient) {
        let (tmp, env, root) = scratch();
        let (stub, client) = StubBridge::start_client_in(tmp.path().join("run")).await;
        let bridges: Bridges = Arc::default();
        let connector = BridgeConnector::new({
            let bridges = Arc::clone(&bridges);
            move || {
                let next = bridges.lock().unwrap().pop_front();
                async move { Ok(next) }
            }
        });
        let runtime = Arc::new(
            SyncRuntime::connecting(&CTX, connector, "/bin/true".into(), ManagedBy::Service)
                .with_open_timeout(FIXTURE_OPEN_TIMEOUT),
        );
        let f = Fixture {
            _tmp: tmp,
            _env: env,
            root,
            stub,
            runtime,
        };
        (f, bridges, client)
    }

    /// Wait up to ten seconds for `done`.
    async fn eventually(what: &str, mut done: impl AsyncFnMut() -> bool) {
        let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(10);
        while !done().await {
            assert!(tokio::time::Instant::now() < deadline, "never: {what}");
            tokio::time::sleep(std::time::Duration::from_millis(20)).await;
        }
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn a_runtime_started_without_a_bridge_syncs_once_one_starts() {
        // #244: a server that started before its bridge never synced until restarted.
        let (f, bridges, client) = connecting().await;
        f.runtime.connect().await.unwrap();
        assert!(!f.runtime.is_connected());

        let err = f.runtime.enable(&f.root).await.unwrap_err();
        assert!(matches!(err, Error::BridgeDisconnected), "{err:?}");
        let status = f.runtime.status(&f.root).await;
        assert!(status.enabled, "enabled, waiting for the bridge");
        assert!(!status.bridge_available);
        let id = status.workspace_id.expect("its id");

        let keeping = tokio::spawn(Arc::clone(&f.runtime).keep_connected());
        bridges.lock().unwrap().push_back(client);
        eventually("the waiting workspace is registered", async || {
            f.stub.last_workspaces() == vec![id]
        })
        .await;
        assert_eq!(f.runtime.roots().await, vec![f.root.clone()]);
        assert!(f.runtime.status(&f.root).await.bridge_available);
        keeping.abort();
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn a_runtime_that_loses_its_bridge_registers_with_the_next_one() {
        let (f, bridges, client) = connecting().await;
        bridges.lock().unwrap().push_back(client);
        f.runtime.connect().await.unwrap();
        assert!(f.runtime.is_connected());
        let id = f.runtime.enable(&f.root).await.unwrap();
        let keeping = tokio::spawn(Arc::clone(&f.runtime).keep_connected());

        f.stub.disconnect();
        eventually("the runtime notices", async || !f.runtime.is_connected()).await;
        let err = f.runtime.workspaces().await.unwrap_err();
        assert!(matches!(err, Error::BridgeDisconnected), "{err:?}");

        let (next, client) = StubBridge::start_client_in(f._tmp.path().join("run")).await;
        bridges.lock().unwrap().push_back(client);
        eventually("the synced workspace is registered again", async || {
            next.last_workspaces() == vec![id]
        })
        .await;
        assert!(f.runtime.is_connected());
        keeping.abort();
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn this_device_is_known_once_a_workspace_is_enabled_and_never_asked_for() {
        let f = fixture().await;
        assert_eq!(f.runtime.this_device(), None);
        assert!(
            f.stub.seen.lock().unwrap().registrations.is_empty(),
            "asking must not register"
        );

        f.runtime.enable(&f.root).await.unwrap();
        assert_eq!(f.runtime.this_device(), Some(f.stub.device_id));
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn peers_answers_the_workgroup_from_the_bridge() {
        let f = fixture().await;
        let listed = dialled_peers(f.stub.device_id, 2);
        f.stub.set_peers(listed.clone());

        let peers = f.runtime.peers().await.unwrap();
        let ids: Vec<_> = peers.peers.iter().map(|p| p.device_id).collect();
        assert_eq!(ids, listed.iter().map(|p| p.device_id).collect::<Vec<_>>());
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn peers_says_so_while_the_bridge_is_away() {
        let (f, _bridges, _client) = connecting().await;
        let err = f.runtime.peers().await.unwrap_err();
        assert!(matches!(err, Error::BridgeDisconnected), "{err:?}");
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn disabling_while_the_bridge_is_away_forgets_the_waiting_workspace() {
        let (f, bridges, client) = connecting().await;
        f.runtime.enable(&f.root).await.unwrap_err();
        f.runtime.disable(&f.root).await.unwrap();
        assert!(!f.runtime.status(&f.root).await.enabled);

        bridges.lock().unwrap().push_back(client);
        f.runtime.connect().await.unwrap();
        assert!(f.runtime.roots().await.is_empty());
        assert!(f.stub.last_workspaces().is_empty());
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn announcements_are_answered_from_a_reconnected_bridge() {
        let (f, bridges, client) = connecting().await;
        bridges.lock().unwrap().push_back(client);
        f.runtime.connect().await.unwrap();
        let id = f.runtime.enable(&f.root).await.unwrap();
        let keeping = tokio::spawn(Arc::clone(&f.runtime).keep_connected());
        let answering = tokio::spawn(Arc::clone(&f.runtime).run());

        f.stub.disconnect();
        let (next, client) = StubBridge::start_client_in(f._tmp.path().join("run")).await;
        bridges.lock().unwrap().push_back(client);
        eventually("reconnected", async || next.last_workspaces() == vec![id]).await;

        // The announcement loop follows the new connection: an announcement on it is
        // claimed, which with no data endpoint fails — and the loop lives on.
        next.announce(id, "ticket").await;
        tokio::time::sleep(std::time::Duration::from_millis(100)).await;
        assert!(!answering.is_finished(), "the announcement loop ended");
        answering.abort();
        keeping.abort();
    }

    /// The scratch tree, the environment pointed at it, and a workspace root in it.
    fn scratch() -> (tempfile::TempDir, EnvGuard, PathBuf) {
        let lock = test_support::lock();
        let tmp = tempfile::tempdir().unwrap();
        let previous = DIR_VARS.map(std::env::var_os);
        // SAFETY (via `test_support::set`): `lock` serialises every read and write of the
        // process environment in this test binary, and it is held until `drop` has restored
        // the old values.
        for (name, dir) in DIR_VARS
            .iter()
            .zip(["cache", "data", "config"].map(|cat| tmp.path().join(cat)))
        {
            test_support::set(name, &dir);
        }
        let env = EnvGuard {
            previous,
            _lock: lock,
        };
        CTX.init(AppKind::Server);

        let root = tmp.path().join("ws");
        std::fs::create_dir_all(root.join(".sapphire-synctest")).unwrap();
        let root = root.canonicalize().unwrap();
        (tmp, env, root)
    }

    /// An application with one `Live` key in the workspace: `sapphire-synctest.note`.
    struct NoteApp;

    impl crate::settings::AppSettings for NoteApp {
        fn defs(&self) -> Vec<sapphire_framework_settings::SettingDef> {
            vec![sapphire_framework_settings::SettingDef {
                key: "sapphire-synctest.note".into(),
                kind: sapphire_framework_settings::Kind::String { non_empty: false },
                scopes: vec![sapphire_framework_settings::Scope::Workspace],
                overridable: false,
                apply: sapphire_framework_settings::Apply::Live,
                default: None,
                section: "Test".into(),
                doc: "test".into(),
                process: sapphire_framework_settings::Process::App,
            }]
        }
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn a_workspace_config_changed_under_the_watcher_reaches_the_settings() {
        let f = fixture().await;
        // A context of its own, so its config directory is this test's: `CTX`'s is
        // whichever test resolved it first.
        let ctx: &'static AppContext = Box::leak(Box::new(AppContext::new("sapphire-synctest")));
        ctx.set_config_dir(f._tmp.path().join("settings"));
        let selection = crate::selection::SelectionFile::at(
            f._tmp.path().join("settings").join("workspace.toml"),
        );
        selection
            .save(&crate::selection::Selection {
                root: f.root.clone(),
                sync: true,
            })
            .unwrap();
        let settings =
            crate::settings::Settings::load_with(ctx, Arc::new(NoteApp), selection).unwrap();
        let changed = settings.changed();
        f.runtime.set_settings(Some(settings.clone()));
        f.runtime.enable(&f.root).await.unwrap();
        let watching = tokio::spawn(Arc::clone(&f.runtime).watch_changes());
        // Until the watcher is up, a write may go unseen: keep writing until it is noticed.
        let config = f.root.join(".sapphire-synctest").join("config.toml");
        eventually("the settings hear of the new value", async || {
            std::fs::write(&config, "[sapphire-synctest]\nnote = \"synced\"\n").unwrap();
            tokio::time::sleep(std::time::Duration::from_millis(200)).await;
            changed.has_changed().unwrap()
        })
        .await;
        assert_eq!(
            settings
                .get_as::<String>("sapphire-synctest.note")
                .as_deref(),
            Some("synced")
        );
        watching.abort();
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn enabling_registers_the_workspace_with_the_bridge() {
        let f = fixture().await;

        let id = f.runtime.enable(&f.root).await.unwrap();
        assert_eq!(f.stub.last_workspaces(), vec![id]);
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn enabling_twice_is_idempotent_and_keeps_the_same_id() {
        let f = fixture().await;

        let first = f.runtime.enable(&f.root).await.unwrap();
        let second = f.runtime.enable(&f.root).await.unwrap();
        assert_eq!(first, second);
        assert_eq!(
            f.stub.last_workspaces(),
            vec![first],
            "still exactly one workspace"
        );
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn a_registration_carries_the_complete_current_list() {
        let f = fixture().await;
        let second_root = f.second_root();

        let a = f.runtime.enable(&f.root).await.unwrap();
        let b = f.runtime.enable(&second_root).await.unwrap();

        let mut seen = f.stub.last_workspaces();
        seen.sort();
        let mut want = vec![a, b];
        want.sort();
        assert_eq!(seen, want, "registration is the whole set, not a delta");
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn no_later_registration_clears_this_apps_routes() {
        // The device id is asked of the bridge once, on the first `enable`, with an empty
        // workspace list — the one moment that is safe, because no route exists yet. Every
        // later registration must name the complete set: one with an empty list would
        // briefly clear this application's routes, and a peer asking for a workspace this
        // server owns would be told nobody has it.
        let f = fixture().await;
        let second_root = f.second_root();

        f.runtime.enable(&f.root).await.unwrap();
        f.runtime.enable(&second_root).await.unwrap();
        f.runtime.sync_now(&f.root).await.unwrap();
        f.runtime.disable(&f.root).await.unwrap();

        let seen = f.stub.seen.lock().expect("stub");
        let first_full = seen
            .registrations
            .iter()
            .position(|r| !r.workspaces.is_empty())
            .expect("enabling registers the workspace");
        let emptied_afterwards = seen.registrations[first_full + 1..]
            .iter()
            .filter(|r| r.workspaces.is_empty())
            .count();
        assert_eq!(
            emptied_afterwards, 0,
            "a registration after the first workspace must still name the whole set"
        );
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn disabling_unregisters_and_leaves_the_files_alone() {
        let f = fixture().await;
        std::fs::write(f.root.join("keep.md"), "content").unwrap();

        let id = f.runtime.enable(&f.root).await.unwrap();
        f.runtime.disable(&f.root).await.unwrap();

        assert!(f.stub.last_workspaces().is_empty());
        assert!(
            f.root.join("keep.md").exists(),
            "disabling sync must not touch files"
        );
        assert!(
            crate::sync::id::sync_id_path("sapphire-synctest", &f.root).exists(),
            "the sync id stays, so re-enabling rejoins the same workspace"
        );
        let _ = id;
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn re_enabling_after_disabling_reuses_the_identity() {
        let f = fixture().await;

        let first = f.runtime.enable(&f.root).await.unwrap();
        f.runtime.disable(&f.root).await.unwrap();
        let again = f.runtime.enable(&f.root).await.unwrap();

        assert_eq!(first, again);
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn status_reports_disabled_for_a_workspace_that_was_never_enabled() {
        let f = fixture().await;

        let status = f.runtime.status(&f.root).await;
        assert!(!status.enabled);
        assert!(status.workspace_id.is_none());
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn disabling_something_that_was_never_enabled_is_not_an_error() {
        let f = fixture().await;
        f.runtime
            .disable(&f.root)
            .await
            .expect("disabling twice is fine");
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn a_file_already_on_disk_is_recorded_on_enable() {
        // No watcher fires for a file written before the workspace was enabled; without a
        // scan on enable it was never recorded, and every session sent nothing.
        let f = fixture().await;
        std::fs::write(f.root.join("a.md"), "content").unwrap();
        f.runtime.enable(&f.root).await.unwrap();
        f.runtime.settle_dials().await;

        let replica = f.runtime.replica_of(&f.root).await.unwrap();
        let states = replica.lock().await.states().unwrap();
        assert!(
            states.iter().any(|(path, _)| path == "a.md"),
            "a file on disk before enable must be recorded: {states:?}"
        );
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn re_enabling_while_the_replica_is_still_held_takes_it_back() {
        // The first scan after enabling runs in the background; a disable and a re-enable
        // can both land while it still holds the store, which redb opens only once.
        let f = fixture().await;
        f.runtime.enable(&f.root).await.unwrap();
        f.runtime.settle_dials().await;
        let held = f.runtime.replica_of(&f.root).await.unwrap();

        f.runtime.disable(&f.root).await.unwrap();
        f.runtime
            .enable(&f.root)
            .await
            .expect("a re-enable must not open the store a second time");

        let again = f.runtime.replica_of(&f.root).await.unwrap();
        assert!(Arc::ptr_eq(&held, &again), "the open replica is taken back");
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn re_enabling_while_the_replica_is_still_closing_waits_for_it() {
        // The last holder let go just before the re-enable: the replica can no longer be
        // taken back, and its store closes a moment later, as it drops.
        let f = fixture().await;
        f.runtime.enable(&f.root).await.unwrap();
        f.runtime.settle_dials().await;
        let config = {
            let replica = f.runtime.replica_of(&f.root).await.unwrap();
            replica.lock().await.config().clone()
        };
        f.runtime.disable(&f.root).await.unwrap();

        // The store, open still, with nothing left to take back.
        let closing = Replica::open(config, Arc::new(SystemClock)).unwrap();
        let key = f.root.canonicalize().unwrap();
        f.runtime
            .stopped
            .lock()
            .unwrap()
            .insert(key, std::sync::Weak::new());
        let closed = tokio::spawn(async move {
            tokio::time::sleep(std::time::Duration::from_millis(100)).await;
            drop(closing);
        });

        f.runtime
            .enable(&f.root)
            .await
            .expect("a re-enable must wait for a store that is closing");
        closed.await.unwrap();
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn a_workspace_whose_root_vanished_is_reported_as_paused_not_deleted() {
        let f = fixture().await;
        std::fs::write(f.root.join("a.md"), "content").unwrap();
        f.runtime.enable(&f.root).await.unwrap();
        f.runtime.scan(&f.root).await.unwrap();

        // The drive is unmounted: the marker directory goes with it.
        std::fs::remove_dir_all(f.root.join(".sapphire-synctest")).unwrap();
        let _ = f.runtime.scan(&f.root).await;

        let status = f.runtime.status(&f.root).await;
        assert!(
            status.paused.is_some(),
            "a missing root must pause, not replicate as a mass deletion"
        );
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn a_directory_that_is_not_a_workspace_cannot_be_enabled() {
        let f = fixture().await;
        let plain = f._tmp.path().join("plain");
        std::fs::create_dir_all(&plain).unwrap();

        assert!(f.runtime.enable(&plain).await.is_err());
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn a_write_through_the_workspace_router_is_committed_to_the_replica() {
        // The brief's "main path": a write the app server itself made is scanned straight
        // after, not left to the watcher's debounce. Asserted against the replica's own
        // state, which is the only thing that can tell the two paths apart — reading the
        // file back would pass even if the scan never ran.
        let f = fixture().await;
        f.runtime.enable(&f.root).await.unwrap();

        let host = Arc::new(crate::WorkspaceHost::new(&CTX));
        host.select(&f.root, false).await.unwrap();
        let current = Arc::new(crate::Current::new(
            &CTX,
            host,
            Some(Arc::clone(&f.runtime)),
            crate::selection::SelectionFile::at(f._tmp.path().join("workspace.toml")),
        ));
        let router = Arc::new(crate::workspace_router(current));
        let (client_conn, server_conn) = sapphire_ipc::Connection::pair();
        tokio::spawn(async move {
            let info = sapphire_ipc::ServerInfo {
                version: "0.0.0".into(),
                api: sapphire_backend::protocol::API_VERSION,
                app_api: None,
                pid: std::process::id(),
                managed_by: ManagedBy::Spawned,
            };
            let _ = sapphire_ipc::serve(server_conn, router, "sapphire-synctest", info).await;
        });
        let info = sapphire_ipc::ClientInfo {
            kind: "test".into(),
            version: "0.0.0".into(),
            api: sapphire_backend::protocol::API_VERSION,
            app_api: None,
            pid: std::process::id(),
        };
        let (client, _) = sapphire_ipc::Client::handshake(client_conn, "sapphire-synctest", info)
            .await
            .unwrap();

        let _: sapphire_backend::protocol::Ack = client
            .call(
                sapphire_backend::protocol::WRITE_FILE,
                sapphire_backend::protocol::ContentParams {
                    path: PathBuf::from("through-the-server.md"),
                    content: "written by the server".into(),
                },
            )
            .await
            .unwrap();

        // `scan` runs before the reply is written, so by the time the call returns the
        // replica must already know the path.
        let synced = f.runtime.synced.lock().await;
        let entry = synced.get(&f.root).expect("enabled above");
        let replica = entry.replica.lock().await;
        let state = replica
            .state("through-the-server.md")
            .expect("a readable state");
        assert!(
            state.is_some(),
            "a write through the server must be scanned into the replica, not left to the watcher"
        );
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn an_announcement_for_an_unknown_workspace_is_ignored_not_fatal() {
        let f = fixture().await;
        f.runtime.enable(&f.root).await.unwrap();

        let driver = Arc::clone(&f.runtime);
        let handle = tokio::spawn(async move { driver.run().await });

        f.stub
            .announce(grain_id::GrainId::random(), "no-such-ticket")
            .await;
        tokio::time::sleep(std::time::Duration::from_millis(200)).await;

        assert!(
            !handle.is_finished(),
            "one bad announcement must not stop the runtime"
        );
        handle.abort();
    }
}