sapphire-framework-server 0.17.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
//! 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::protocol as proto;
use sapphire_bridge_api::{PeersResult, 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;
pub use watch::{DEBOUNCE, Watcher};

use live::{LivePeers, dial_backoff};

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

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>,
}

/// 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>,
}

/// 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),
        }
    }
}

/// Replication for one application's workspaces.
pub struct SyncRuntime {
    ctx: &'static AppContext,
    bridge: Arc<BridgeClient>,
    exe_path: PathBuf,
    managed_by: ManagedBy,
    synced: Mutex<HashMap<PathBuf, Synced>>,
    /// 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>>,
    /// 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 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<()>>,
}

impl SyncRuntime {
    /// A runtime for `ctx`'s application.
    pub fn new(
        ctx: &'static AppContext,
        bridge: Arc<BridgeClient>,
        exe_path: PathBuf,
        managed_by: ManagedBy,
    ) -> SyncRuntime {
        SyncRuntime {
            ctx,
            bridge,
            exe_path,
            managed_by,
            synced: Mutex::new(HashMap::new()),
            device_id: OnceCell::new(),
            watcher: OnceCell::new(),
            host: OnceCell::new(),
            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,
            first_dials: std::sync::Mutex::new(tokio::task::JoinSet::new()),
        }
    }

    /// 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
    }

    /// 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);
    }

    /// 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)?;

        // 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);
            }
            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);
            let replica = Replica::open(config, Arc::new(SystemClock))
                .map_err(|e| Error::Sync(e.to_string()))?;
            synced.insert(
                key.clone(),
                Synced {
                    workspace_id,
                    replica: Arc::new(Mutex::new(replica)),
                    paused: None,
                    last_error: None,
                },
            );
        }

        // 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).
        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.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.
    pub async fn disable(&self, root: &Path) -> Result<()> {
        self.stop(root).await.1
    }

    /// 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());
        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 {
            self.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
    /// already be a workspace of this application — the directory is placed, not made.
    ///
    /// [`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 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_available = self.bridge.status().await.is_ok();
        let answer = self.bridge.peers().await.ok();
        if let Some(answer) = &answer {
            *self.last_peers.lock().await = Some(answer.clone());
        }
        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,
            };
        };
        let synced = self.synced.lock().await;
        match synced.get(&key) {
            Some(entry) => 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: self.embedding_progress(&key).await,
            },
            None => SyncStatus {
                enabled: false,
                workspace_id: None,
                peers,
                paused: None,
                last_error: None,
                bridge_available,
                topology: proto::Topology::Mesh,
                embedding: None,
            },
        }
    }

    /// 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) {
        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;
        // 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) => {
                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;
        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);
        tokio::spawn(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
            .bridge
            .peers()
            .await
            .map_err(|e| Error::Bridge(e.to_string()))?;
        *self.last_peers.lock().await = Some(peers.clone());
        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 until the connection closes.
    pub async fn run(self: Arc<Self>) -> Result<()> {
        let mut incoming = self.bridge.incoming();
        loop {
            let announcement = match incoming.recv().await {
                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 Ok(()),
            };

            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(&self.bridge);
            let workspace_id = announcement.workspace_id;
            let peer = announcement.peer_device_id;
            let driver = Arc::clone(&self);
            tokio::spawn(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) => {
                        tracing::warn!("an inbound session failed: {err}");
                        return;
                    }
                };
                let Some((key, table)) = driver.live_peers_or_create(&root).await else {
                    return;
                };
                // 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;
                };
                // 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);
            });
        }
    }

    /// 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() {}
    }

    /// 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.bridge.peers().await {
                Ok(peers) => {
                    *self.last_peers.lock().await = Some(peers.clone());
                    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| Error::Sync(e.to_string()))?;
        let Some((key, table)) = self.live_peers_or_create(root).await else {
            return Ok(());
        };
        // 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(());
        };
        // 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);
        tokio::spawn(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;
                    }
                }
            }
        });
    }

    /// 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 {
            // 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, 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.
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 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();

        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,
        }
    }

    #[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_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();
    }
}