weirdnet-sync 0.8.2

Weirdnet sync: hybrid logical clocks, an op log, LWW and OR-set merge, tombstone GC, SyncKey page sealing, a device roster with self-cert, revocation, key rotation with re-admission, user-signed device grants and service devices linked without the seed, majority-vote leases with fencing terms, signed plans and a job log, and the /weirdnet/sync/2 pull protocol over weirdnet-net.
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
//! Majority-vote leases with fencing terms (since 0.8).
//!
//! The op log is eventually consistent, so it can't give mutual exclusion by itself. A lease
//! comes from a small quorum protocol between *voters*: the user's devices that hold a role
//! grant (for Ymir, `ymir.inference`; [`Voters::Role`]). A candidate asks every voter at once
//! and holds the lease while a majority granted it, timed by its own monotonic clock and cut
//! short by the assumed clock-rate drift. Each new holder gets a higher [`Term`], the fencing
//! token: resources refuse commands with a term older than one they have seen
//! ([`FenceGuard`]), so a paused or partitioned ex-holder can do no harm.
//!
//! Asks and answers are [`Message`] variants on `/weirdnet/sync/2`, sealed
//! like every other request, so only active, admitted devices of the user take part. A
//! voter answers only candidates that are in the same voter set. Safety needs no wall clock
//! and no HLC; only rate drift within [`LeaseConfig::max_drift`].
//!
//! Voters keep the highest term per lease (persist [`Voter::export`]); after a restart a
//! voter refuses takeovers for one maximum lease duration, so it can't hand out an
//! overlapping lease.
use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Condvar, Mutex};
use std::time::{Duration, Instant};

use serde::{Deserialize, Serialize};
use weirdnet_net::profile::WEIRDNET;
use weirdnet_net::{Multiaddr, Node, PeerId};

use crate::error::{Result, SyncError};
use crate::net::SyncState;
use crate::protocol::{Message, decode, encode};
use crate::roster::{CallerCheck, Roster};

/// Default time a grant lasts at a voter.
pub const DEFAULT_DURATION: Duration = Duration::from_secs(15);
/// Default renewal interval (a third of the duration).
pub const DEFAULT_RENEW_EVERY: Duration = Duration::from_secs(5);
/// Default worst clock-rate drift assumed between devices.
pub const DEFAULT_MAX_DRIFT: f64 = 0.01;
/// Default delay per candidate rank after a lapse.
pub const DEFAULT_STAGGER: Duration = Duration::from_millis(500);
/// Taken off every lease besides the drift.
const EPSILON: Duration = Duration::from_millis(50);
/// The envelope type lease messages travel under.
pub const LEASE_TYPE: &str = "weirdnet.lease";
const MAX_NAME: usize = 200;

/// A lease name, scoped by the app: `ymir.scheduler`, `ymir.jobs/<job id>`. 1 to 200 of
/// `A-Z a-z 0-9 . - _ : / + #`.
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(try_from = "String", into = "String")]
pub struct LeaseName(String);

impl LeaseName {
    pub fn new(name: impl Into<String>) -> Result<Self> {
        let name = name.into();
        let ok = !name.is_empty() && name.len() <= MAX_NAME && name.chars().all(|c| c.is_ascii_alphanumeric() || ".-_:/+#".contains(c));
        if ok { Ok(Self(name)) } else { Err(SyncError::Protocol(format!("bad lease name {name:?}"))) }
    }

    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl TryFrom<String> for LeaseName {
    type Error = SyncError;
    fn try_from(s: String) -> Result<Self> {
        Self::new(s)
    }
}

impl From<LeaseName> for String {
    fn from(n: LeaseName) -> String {
        n.0
    }
}

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

/// Fencing token: strictly increases per lease name across holders.
#[derive(Debug, Copy, Clone, Default, Ord, PartialOrd, Eq, PartialEq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct Term(pub u64);

impl Term {
    pub fn next(self) -> Term {
        Term(self.0.saturating_add(1))
    }
}

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

/// Per-lease limits a [`Voter`] applies (since 0.8.1): the longest grant, and how long it
/// refuses another holder after a promise lapsed (its holder stopped renewing without letting
/// go). Set them per lease-name prefix with [`Voter::with_limits`]; a job's own
/// [`JobRules`](crate::jobs::JobRules) set them for its claim lease.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct LeaseLimits {
    pub max_duration: Duration,
    pub takeover_after: Duration,
}

impl LeaseLimits {
    pub fn new(max_duration: Duration, takeover_after: Duration) -> Self {
        Self { max_duration, takeover_after }
    }
}

/// Who votes (and who may stand): devices with an active cert in the roster.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Voters {
    /// Devices whose grant holds this role (`Roster::has_role`), for example `ymir.inference`.
    Role(String),
    /// A fixed set of peer ids (those with an active cert count).
    Peers(Vec<String>),
}

impl Voters {
    /// The voters now, sorted (the order is the candidate rank).
    pub fn resolve(&self, roster: &Roster) -> Vec<String> {
        let mut v: Vec<String> = match self {
            Voters::Role(role) => roster.active().filter(|c| roster.has_role(&c.peer_id, role)).map(|c| c.peer_id.clone()).collect(),
            Voters::Peers(peers) => peers.iter().filter(|p| roster.get(p).is_some_and(|c| c.active())).cloned().collect(),
        };
        v.sort();
        v.dedup();
        v
    }

    /// A digest of [`resolve`](Self::resolve): candidates send it with every ask and a voter
    /// grants only when its own view of the voter set is the same, so a candidate with a stale
    /// roster can't win a majority of a voter set that has since changed.
    pub fn digest(&self, roster: &Roster) -> String {
        let mut h = blake3::Hasher::new();
        h.update(b"weirdnet-sync/v1/voters\n");
        for v in self.resolve(roster) {
            h.update(v.as_bytes());
            h.update(b"\n");
        }
        h.finalize().to_hex()[..32].to_string()
    }

    pub fn contains(&self, roster: &Roster, peer: &str) -> bool {
        match self {
            Voters::Role(role) => roster.get(peer).is_some_and(|c| c.active()) && roster.has_role(peer, role),
            Voters::Peers(peers) => peers.iter().any(|p| p == peer) && roster.get(peer).is_some_and(|c| c.active()),
        }
    }
}

/// How a lease is held.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct LeaseConfig {
    /// How long a grant lasts at a voter (capped by the voter's maximum).
    pub duration: Duration,
    /// How often the [`Elector`] renews.
    pub renew_every: Duration,
    /// Assumed worst clock-rate drift; the holder's lease is shortened by it.
    pub max_drift: f64,
    pub voters: Voters,
    /// Extra wait per candidate rank after a lapse, so the lowest rank tries first.
    pub stagger: Duration,
    /// Write a [`LeaseRecord`] under this record type on each new term (informational).
    pub record_type: Option<String>,
}

impl LeaseConfig {
    /// The defaults (15 s, renew every 5 s, 1 % drift, 500 ms stagger) with these voters.
    pub fn new(voters: Voters) -> Self {
        Self {
            duration: DEFAULT_DURATION,
            renew_every: DEFAULT_RENEW_EVERY,
            max_drift: DEFAULT_MAX_DRIFT,
            voters,
            stagger: DEFAULT_STAGGER,
            record_type: None,
        }
    }

    /// Voters are the devices granted `role`.
    pub fn for_role(role: impl Into<String>) -> Self {
        Self::new(Voters::Role(role.into()))
    }

    /// Sets the duration and renews every third of it.
    pub fn with_duration(mut self, duration: Duration) -> Self {
        self.duration = duration;
        self.renew_every = duration / 3;
        self
    }

    pub fn with_stagger(mut self, stagger: Duration) -> Self {
        self.stagger = stagger;
        self
    }

    pub fn with_max_drift(mut self, drift: f64) -> Self {
        self.max_drift = drift.clamp(0.0, 0.5);
        self
    }

    pub fn with_record_type(mut self, typ: impl Into<String>) -> Self {
        self.record_type = Some(typ.into());
        self
    }
}

/// A candidate asks for (or renews) a lease.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LeaseAsk {
    pub name: LeaseName,
    pub term: Term,
    pub duration_ms: u32,
    pub renew: bool,
    /// [`Voters::digest`] of the voter set the candidate counts its majority in.
    pub voters: String,
}

/// A voter granted it.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LeaseGrant {
    pub name: LeaseName,
    pub term: Term,
    pub duration_ms: u32,
}

/// A voter did not grant it: who it promised, for how much longer, and the highest term it
/// has granted (ask with a higher one next time).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LeaseDeny {
    pub name: LeaseName,
    pub promised: Term,
    pub holder: String,
    pub remaining_ms: u32,
    pub max_term: Term,
}

/// The holder lets go (also the answer to one).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LeaseFree {
    pub name: LeaseName,
    pub term: Term,
}

/// Who holds a lease, written to the op log on each new term for display. Nothing trusts it.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LeaseRecord {
    pub name: LeaseName,
    pub term: Term,
    pub holder: String,
}

#[derive(Debug, Clone)]
struct Promise {
    term: Term,
    holder: String,
    until: Instant,
}

#[derive(Debug, Clone, Default)]
struct Slot {
    max_term: Term,
    /// Who `max_term` went to.
    max_holder: String,
    promise: Option<Promise>,
}

#[derive(Serialize, Deserialize)]
struct Persisted {
    v: u8,
    terms: Vec<(LeaseName, Term, String)>,
    /// The longest grant given (since 0.8.1), so a restart waits it out.
    #[serde(default)]
    longest_ms: u64,
}

type Persist = Arc<dyn Fn(&[u8]) -> std::io::Result<()> + Send + Sync>;

/// The voter side (one per device, kept in [`SyncState::voter`]). Not secret.
///
/// Give it a store with [`with_persist`](Self::with_persist) (or use
/// [`Identity::enable_voter`](crate::Identity::enable_voter)): every new term is then written
/// before it is granted. Without one, a voter that crashes before the app saved
/// [`export`](Self::export) may grant a term it already gave out, so two holders could share
/// a fencing token (their leases still never overlap in time).
#[derive(Clone)]
pub struct Voter {
    candidates: Voters,
    max_duration: Duration,
    grace_until: Instant,
    slots: HashMap<LeaseName, Slot>,
    persist: Option<Persist>,
    /// Limits per lease-name prefix, longest prefix first.
    limits: Vec<(String, LeaseLimits)>,
    /// The longest grant given so far.
    longest: Duration,
}

impl std::fmt::Debug for Voter {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Voter")
            .field("candidates", &self.candidates)
            .field("max_duration", &self.max_duration)
            .field("slots", &self.slots)
            .field("persist", &self.persist.is_some())
            .field("limits", &self.limits)
            .finish()
    }
}

impl Voter {
    /// A voter answering candidates in `candidates` (normally the same [`Voters`] the
    /// leases use). It refuses takeovers for [`DEFAULT_DURATION`] after it starts.
    pub fn new(candidates: Voters) -> Self {
        Self {
            candidates,
            max_duration: DEFAULT_DURATION,
            grace_until: Instant::now() + DEFAULT_DURATION,
            slots: HashMap::new(),
            persist: None,
            limits: Vec::new(),
            longest: Duration::ZERO,
        }
    }

    /// When it started (the grace runs from there).
    fn started(&self) -> Instant {
        self.grace_until - self.grace()
    }

    /// How long it refuses takeovers after starting: its longest possible grant.
    fn grace(&self) -> Duration {
        self.limits.iter().map(|(_, l)| l.max_duration).chain([self.max_duration, self.longest]).max().unwrap_or(self.max_duration)
    }

    /// Limits for lease names starting with `prefix` (since 0.8.1), for example
    /// `ymir.train/` with 60 s grants and a 5 min takeover wait. The longest matching prefix
    /// wins; the restart grace grows to the longest grant. A job's own rules take precedence
    /// for its claim lease when this device has synced the job.
    pub fn with_limits(mut self, prefix: impl Into<String>, max_duration: Duration, takeover_after: Duration) -> Self {
        let started = self.started();
        let prefix = prefix.into();
        self.limits.retain(|(p, _)| *p != prefix);
        self.limits.push((prefix, LeaseLimits::new(max_duration, takeover_after)));
        self.limits.sort_by(|a, b| b.0.len().cmp(&a.0.len()).then_with(|| a.0.cmp(&b.0)));
        self.grace_until = started + self.grace();
        self
    }

    /// The limits for `name`: the longest matching prefix's, if any.
    pub fn limits_for(&self, name: &LeaseName) -> Option<LeaseLimits> {
        self.limits.iter().find(|(p, _)| name.as_str().starts_with(p.as_str())).map(|(_, l)| *l)
    }

    /// Copies another voter's maximum and per-prefix limits (keeping this one's terms).
    pub(crate) fn with_settings_of(mut self, other: &Voter) -> Self {
        let started = self.started();
        self.max_duration = other.max_duration;
        self.limits = other.limits.clone();
        self.grace_until = started + self.grace();
        self
    }

    /// Write [`export`](Self::export) with `store` before each new term is granted (renewals
    /// write nothing). If the write fails the ask is denied.
    pub fn with_persist(mut self, store: impl Fn(&[u8]) -> std::io::Result<()> + Send + Sync + 'static) -> Self {
        self.persist = Some(Arc::new(store));
        self
    }

    /// The longest grant it gives, which is also the restart grace (default 15 s).
    pub fn with_max_duration(mut self, max: Duration) -> Self {
        let started = self.started();
        self.max_duration = max;
        self.grace_until = started + self.grace();
        self
    }

    /// The highest terms (and their holders), to persist; not secret.
    pub fn export(&self) -> Vec<u8> {
        let mut terms: Vec<_> = self.slots.iter().map(|(n, s)| (n.clone(), s.max_term, s.max_holder.clone())).collect();
        terms.sort();
        encode(&Persisted { v: 1, terms, longest_ms: self.longest.as_millis().min(u64::MAX as u128) as u64 }).unwrap_or_default()
    }

    /// A voter restarted from [`export`](Self::export): it never grants an old term again and
    /// refuses takeovers for one maximum duration.
    pub fn import(bytes: &[u8], candidates: Voters) -> Result<Self> {
        let p: Persisted = decode(bytes)?;
        let mut v = Self::new(candidates);
        let started = v.started();
        v.longest = Duration::from_millis(p.longest_ms);
        v.grace_until = started + v.grace();
        for (name, term, holder) in p.terms {
            v.slots.insert(name, Slot { max_term: term, max_holder: holder, promise: None });
        }
        Ok(v)
    }

    /// The longest grant it gives (and its restart grace).
    pub fn max_duration(&self) -> Duration {
        self.max_duration
    }

    /// The highest term granted for `name`.
    pub fn max_term(&self, name: &LeaseName) -> Term {
        self.slots.get(name).map(|s| s.max_term).unwrap_or_default()
    }

    pub fn candidates(&self) -> &Voters {
        &self.candidates
    }

    /// The voter rule. `from` is the transport-authenticated asker.
    pub fn on_ask(&mut self, from: &str, ask: &LeaseAsk, now: Instant) -> Message {
        self.on_ask_with(from, ask, now, None)
    }

    /// [`on_ask`](Self::on_ask) with `limits` in place of the prefix limits (since 0.8.1;
    /// the sync state passes a job's own rules here).
    pub fn on_ask_with(&mut self, from: &str, ask: &LeaseAsk, now: Instant, limits: Option<LeaseLimits>) -> Message {
        let in_grace = now < self.grace_until;
        let grace_left = self.grace_until.saturating_duration_since(now);
        let limits = limits.or_else(|| self.limits_for(&ask.name));
        let max = limits.map(|l| l.max_duration).unwrap_or(self.max_duration);
        let takeover_after = limits.map(|l| l.takeover_after).unwrap_or_default();
        let duration = Duration::from_millis(ask.duration_ms as u64).min(max);
        let longer = duration > self.longest;
        let slot = self.slots.entry(ask.name.clone()).or_default();
        let live = slot.promise.as_ref().filter(|p| p.until > now).cloned();
        // A promise that ran out without its holder letting go holds others off for the
        // takeover wait.
        let lapsed = slot.promise.as_ref().filter(|p| p.until <= now && p.holder != from && now < p.until + takeover_after).cloned();
        // Renewal (or re-establishing a lapsed promise): only the holder the term went to, and
        // only while no higher term was granted here.
        let renewal = ask.term == slot.max_term && slot.max_holder == from && ask.term.0 > 0;
        // Takeover or first grant: a higher term, nobody else promised.
        let takeover = ask.term > slot.max_term && live.as_ref().is_none_or(|p| p.holder == from) && lapsed.is_none() && !in_grace;
        if renewal || takeover {
            let before = slot.clone();
            slot.max_term = ask.term;
            slot.max_holder = from.to_string();
            slot.promise = Some(Promise { term: ask.term, holder: from.to_string(), until: now + duration });
            let longest_before = self.longest;
            self.longest = self.longest.max(duration);
            if (takeover || longer)
                && let Some(store) = self.persist.clone()
            {
                // The new term must be durable before anyone acts on it.
                if store(&self.export()).is_err() {
                    self.longest = longest_before;
                    let slot = self.slots.entry(ask.name.clone()).or_default();
                    *slot = before;
                    return Message::LeaseDeny(LeaseDeny {
                        name: ask.name.clone(),
                        promised: slot.max_term,
                        holder: String::new(),
                        remaining_ms: 0,
                        max_term: slot.max_term,
                    });
                }
            }
            return Message::LeaseGrant(LeaseGrant { name: ask.name.clone(), term: ask.term, duration_ms: duration.as_millis() as u32 });
        }
        let (promised, holder, remaining) = match (&live, &lapsed) {
            (Some(p), _) => (p.term, p.holder.clone(), p.until - now),
            (None, Some(p)) => (p.term, p.holder.clone(), (p.until + takeover_after).saturating_duration_since(now)),
            (None, None) if in_grace => (slot.max_term, slot.max_holder.clone(), grace_left),
            (None, None) => (slot.max_term, String::new(), Duration::ZERO),
        };
        Message::LeaseDeny(LeaseDeny {
            name: ask.name.clone(),
            promised,
            holder,
            remaining_ms: remaining.as_millis().min(u32::MAX as u128) as u32,
            max_term: slot.max_term,
        })
    }

    /// The holder lets go of `term`.
    pub fn on_free(&mut self, from: &str, free: &LeaseFree) {
        if let Some(slot) = self.slots.get_mut(&free.name)
            && slot.promise.as_ref().is_some_and(|p| p.term == free.term && p.holder == from)
        {
            slot.promise = None;
        }
    }
}

impl SyncState {
    /// Makes this device a voter (or stops it voting with `None`).
    pub fn set_voter(&mut self, voter: Option<Voter>) {
        self.voter = voter;
    }

    /// A lease message from `from` (already opened under the head key).
    pub(crate) fn on_lease(&mut self, from: &str, msg: Message) -> Message {
        use crate::protocol::Refuse;
        let check = self.roster.check_caller(&WEIRDNET, &self.user_key, from, None);
        if check != CallerCheck::Linked {
            return Message::Refuse(Refuse::new(check.reason().unwrap_or("unknown-device")));
        }
        // A job's claim lease follows the job's rules when this device has the job.
        let job = match &msg {
            Message::LeaseAsk(ask) if self.voter.is_some() => match self.job_lease_limits(&ask.name, from, ask.renew) {
                Ok(limits) => limits,
                Err(reason) => return Message::Refuse(Refuse::new(reason)),
            },
            _ => None,
        };
        let roster = &self.roster;
        let Some(voter) = self.voter.as_mut() else { return Message::Refuse(Refuse::new("not-a-voter")) };
        if !voter.candidates.contains(roster, from) {
            return Message::Refuse(Refuse::new("not-a-candidate"));
        }
        let me = self.our_cert.peer_id.as_str();
        match msg {
            // Only a device that is itself in the voter set votes, and only for a candidate
            // that counts the same voter set.
            Message::LeaseAsk(_) if !voter.candidates.contains(roster, me) => Message::Refuse(Refuse::new("not-a-voter")),
            Message::LeaseAsk(ask) if ask.voters != voter.candidates.digest(roster) => Message::Refuse(Refuse::new("voters-differ")),
            Message::LeaseAsk(ask) => voter.on_ask_with(from, &ask, Instant::now(), job),
            Message::LeaseFree(free) => {
                voter.on_free(from, &free);
                Message::LeaseFree(free)
            }
            _ => Message::Refuse(Refuse::new("unexpected")),
        }
    }
}

/// A held lease. Valid only while [`is_valid`](Self::is_valid); carry [`term`](Self::term) in
/// every fenced command.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LeaseHandle {
    name: LeaseName,
    term: Term,
    holder: String,
    valid_until: Instant,
}

impl LeaseHandle {
    pub fn name(&self) -> &LeaseName {
        &self.name
    }

    pub fn term(&self) -> Term {
        self.term
    }

    /// This device's peer id.
    pub fn holder(&self) -> &str {
        &self.holder
    }

    pub fn is_valid(&self) -> bool {
        Instant::now() < self.valid_until
    }

    pub fn remaining(&self) -> Duration {
        self.valid_until.saturating_duration_since(Instant::now())
    }
}

/// Why a lease was not got or kept.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum LeaseError {
    /// Someone else holds it; try again after `retry_in`.
    #[error("held by {holder} (term {term}), retry in {retry_in:?}")]
    Held { holder: String, term: Term, retry_in: Duration },
    /// Not enough voters answered with a grant.
    #[error("no quorum: {reached} of {needed} grants")]
    NoQuorum { reached: usize, needed: usize },
    /// The lease ran out before a renewal reached a quorum: stop acting.
    #[error("lease lost")]
    Lost,
    /// This device is not among the voters (only voters stand), or no longer is.
    #[error("not a voter for this lease")]
    NotEligible,
    /// Not claimable yet (a job's retry backoff, since 0.8.1); try again after `retry_in`.
    #[error("not yet, retry in {retry_in:?}")]
    NotYet { retry_in: Duration },
    #[error("{0}")]
    Sync(String),
}

impl From<SyncError> for LeaseError {
    fn from(e: SyncError) -> Self {
        LeaseError::Sync(e.to_string())
    }
}

/// What an [`Elector`] sees.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum LeaseState {
    Held {
        term: Term,
    },
    /// Learned from a voter's answer.
    Follower {
        holder: String,
        term: Term,
    },
    /// Nobody known to hold it.
    Vacant,
    NoQuorum {
        reached: usize,
        needed: usize,
    },
}

type SyncNode = Node<Vec<u8>, Vec<u8>>;
type AddrBook = Arc<dyn Fn(&str) -> Vec<Multiaddr> + Send + Sync>;

/// The candidate side: acquires, renews and releases leases through a sync node and its
/// shared state (as served by [`crate::start_node`]). The state's lock is never held across
/// the network.
#[derive(Clone)]
pub struct Leases {
    node: Arc<SyncNode>,
    state: Arc<Mutex<SyncState>>,
    addrs: AddrBook,
    terms: Arc<Mutex<HashMap<LeaseName, Term>>>,
}

enum Answer {
    Grant(Duration),
    Deny(LeaseDeny),
    None,
}

impl Leases {
    pub fn new(node: Arc<SyncNode>, state: Arc<Mutex<SyncState>>) -> Self {
        Self { node, state, addrs: Arc::new(|_| Vec::new()), terms: Arc::default() }
    }

    /// The shared state the leases use.
    pub fn state(&self) -> &Arc<Mutex<SyncState>> {
        &self.state
    }

    /// Addresses to dial each voter on (default none: the node's known addresses).
    pub fn with_addrs(mut self, addrs: impl Fn(&str) -> Vec<Multiaddr> + Send + Sync + 'static) -> Self {
        self.addrs = Arc::new(addrs);
        self
    }

    fn known_term(&self, name: &LeaseName) -> Term {
        let local = self.terms.lock().map(|t| t.get(name).copied().unwrap_or_default()).unwrap_or_default();
        let voted = self.state.lock().ok().and_then(|st| st.voter.as_ref().map(|v| v.max_term(name))).unwrap_or_default();
        local.max(voted)
    }

    /// The highest term this device has seen for `name` (asked for, granted or voted on);
    /// a holder whose term is below it has lost the lease (since 0.8.1).
    pub fn term_seen(&self, name: &LeaseName) -> Term {
        self.known_term(name)
    }

    fn learn(&self, name: &LeaseName, term: Term) {
        if let Ok(mut t) = self.terms.lock() {
            let e = t.entry(name.clone()).or_default();
            *e = (*e).max(term);
        }
    }

    /// Ask every voter at once (this device's own voter answers locally) and collect answers
    /// until all came, `needed` grants came, or `wait` passed. Unreachable voters can't hold
    /// a round up beyond `wait`; their threads finish on their own.
    fn ask_all(&self, voters: &[String], me: &str, msg: &Message, needed: usize, wait: Duration) -> Vec<Answer> {
        let (tx, rx) = std::sync::mpsc::channel();
        for v in voters {
            let (this, v, me, msg, tx) = (self.clone(), v.clone(), me.to_string(), msg.clone(), tx.clone());
            std::thread::spawn(move || {
                let _ = tx.send(this.ask_one(&v, &me, &msg));
            });
        }
        drop(tx);
        let deadline = Instant::now() + wait;
        let mut out = Vec::new();
        let mut grants = 0;
        while out.len() < voters.len() && grants < needed {
            let left = deadline.saturating_duration_since(Instant::now());
            match rx.recv_timeout(left) {
                Ok(a) => {
                    if matches!(a, Answer::Grant(_)) {
                        grants += 1;
                    }
                    out.push(a);
                }
                Err(_) => break,
            }
        }
        out
    }

    /// Tell voters to drop a promise, waiting at most `wait` for them.
    fn free_all(&self, voters: &[String], me: &str, free: LeaseFree, wait: Duration) {
        let _ = self.ask_all(voters, me, &Message::LeaseFree(free), usize::MAX, wait);
    }

    fn ask_one(&self, voter: &str, me: &str, msg: &Message) -> Answer {
        let reply = if voter == me {
            match self.state.lock() {
                Ok(mut st) => st.on_lease(me, msg.clone()),
                Err(_) => return Answer::None,
            }
        } else {
            match self.request(voter, msg) {
                Ok(m) => m,
                Err(_) => return Answer::None,
            }
        };
        match reply {
            Message::LeaseGrant(g) => Answer::Grant(Duration::from_millis(g.duration_ms as u64)),
            Message::LeaseDeny(d) => Answer::Deny(d),
            _ => Answer::None,
        }
    }

    fn request(&self, voter: &str, msg: &Message) -> Result<Message> {
        let peer: PeerId = voter.parse().map_err(|_| SyncError::Protocol("bad voter peer id".into()))?;
        // Envelopes are bound to the asking peer (us), as in every other sync request.
        let me = self.node.peer_id().to_string();
        let env = {
            let st = self.state.lock().map_err(|_| SyncError::Protocol("sync state lock poisoned".into()))?;
            let key = st.current_key();
            st.seal_envelope(&key, LEASE_TYPE, &me, msg)?
        };
        let addrs = (self.addrs)(voter);
        let reply = self.node.request(peer, &addrs, encode(&env)?)?;
        let env: crate::protocol::Envelope = decode(&reply)?;
        let st = self.state.lock().map_err(|_| SyncError::Protocol("sync state lock poisoned".into()))?;
        st.open_envelope(&env, &me)
    }

    fn voters(&self, cfg: &LeaseConfig) -> std::result::Result<(String, Vec<String>), LeaseError> {
        let st = self.state.lock().map_err(|_| LeaseError::Sync("sync state lock poisoned".into()))?;
        Ok((st.our_cert.peer_id.clone(), cfg.voters.resolve(&st.roster)))
    }

    fn digest(&self, cfg: &LeaseConfig) -> String {
        self.state.lock().map(|st| cfg.voters.digest(&st.roster)).unwrap_or_default()
    }

    /// Acquire `name` with a new term. Fails with [`LeaseError::Held`] while another
    /// device holds it (with when to try again) or [`LeaseError::NoQuorum`].
    pub fn acquire(&self, name: &LeaseName, cfg: &LeaseConfig) -> std::result::Result<LeaseHandle, LeaseError> {
        let (me, voters) = self.voters(cfg)?;
        let needed = voters.len() / 2 + 1;
        if !voters.contains(&me) {
            return Err(LeaseError::NotEligible);
        }
        let term = self.known_term(name).next();
        let ask = LeaseAsk {
            name: name.clone(),
            term,
            duration_ms: cfg.duration.as_millis().min(u32::MAX as u128) as u32,
            renew: false,
            voters: self.digest(cfg),
        };
        let t0 = Instant::now();
        let answers = self.ask_all(&voters, &me, &Message::LeaseAsk(ask), needed, ask_wait(cfg));
        let granted: Vec<Duration> = answers.iter().filter_map(|a| if let Answer::Grant(d) = a { Some(*d) } else { None }).collect();
        // A term some voter granted is used up; one nobody granted can be asked for again, so
        // retrying while a holder lives doesn't inflate the terms.
        if !granted.is_empty() {
            self.learn(name, term);
        }
        if granted.len() >= needed {
            let handle = LeaseHandle { name: name.clone(), term, holder: me.clone(), valid_until: valid_until(t0, &granted, cfg) };
            if let Some(typ) = &cfg.record_type
                && let Ok(mut st) = self.state.lock()
            {
                let rec = LeaseRecord { name: name.clone(), term, holder: me };
                if let Ok(bytes) = encode(&rec) {
                    let _ = st.write(wall_ms(), typ, name.as_str(), &bytes);
                }
            }
            return Ok(handle);
        }
        // Let go of the grants we did get, so the next candidate needn't wait them out.
        if !granted.is_empty() {
            self.free_all(&voters, &me, LeaseFree { name: name.clone(), term }, Duration::ZERO);
        }
        let denies: Vec<&LeaseDeny> = answers.iter().filter_map(|a| if let Answer::Deny(d) = a { Some(d) } else { None }).collect();
        if let Some(max) = denies.iter().map(|d| d.max_term).max() {
            self.learn(name, max);
        }
        if let Some(d) =
            denies.iter().filter(|d| !d.holder.is_empty() && d.holder != me && d.remaining_ms > 0).max_by_key(|d| (d.promised, d.remaining_ms))
        {
            return Err(LeaseError::Held { holder: d.holder.clone(), term: d.promised, retry_in: Duration::from_millis(d.remaining_ms as u64) });
        }
        Err(LeaseError::NoQuorum { reached: granted.len(), needed })
    }

    /// Renew a held lease (same term). Until it runs out a failed renewal is
    /// [`LeaseError::NoQuorum`] and the handle stays valid; after, [`LeaseError::Lost`].
    pub fn renew(&self, handle: &mut LeaseHandle, cfg: &LeaseConfig) -> std::result::Result<(), LeaseError> {
        let (me, voters) = self.voters(cfg)?;
        let needed = voters.len() / 2 + 1;
        let ask = LeaseAsk {
            name: handle.name.clone(),
            term: handle.term,
            duration_ms: cfg.duration.as_millis().min(u32::MAX as u128) as u32,
            renew: true,
            voters: self.digest(cfg),
        };
        let t0 = Instant::now();
        let answers = self.ask_all(&voters, &me, &Message::LeaseAsk(ask), needed, ask_wait(cfg));
        let granted: Vec<Duration> = answers.iter().filter_map(|a| if let Answer::Grant(d) = a { Some(*d) } else { None }).collect();
        if !voters.contains(&me) {
            return if handle.is_valid() { Err(LeaseError::NotEligible) } else { Err(LeaseError::Lost) };
        }
        if granted.len() >= needed {
            handle.valid_until = handle.valid_until.max(valid_until(t0, &granted, cfg));
            return Ok(());
        }
        for a in &answers {
            if let Answer::Deny(d) = a {
                self.learn(&handle.name, d.max_term);
            }
        }
        if handle.is_valid() { Err(LeaseError::NoQuorum { reached: granted.len(), needed }) } else { Err(LeaseError::Lost) }
    }

    /// Let go of a lease: voters drop the promise, so the next holder needn't wait it out.
    pub fn release(&self, handle: LeaseHandle, cfg: &LeaseConfig) {
        if let Ok((me, voters)) = self.voters(cfg) {
            self.free_all(&voters, &me, LeaseFree { name: handle.name.clone(), term: handle.term }, ask_wait(cfg));
        }
    }

    /// Keep acquiring and renewing `name` in the background (see [`Elector`]).
    pub fn elect(&self, name: LeaseName, cfg: LeaseConfig) -> Elector {
        Elector::start(self.clone(), name, cfg)
    }
}

/// How long a round waits for answers: a third of the lease, so a lease got is still worth
/// most of its duration.
fn ask_wait(cfg: &LeaseConfig) -> Duration {
    (cfg.duration / 3).max(Duration::from_millis(100))
}

fn valid_until(t0: Instant, granted: &[Duration], cfg: &LeaseConfig) -> Instant {
    let shortest = granted.iter().min().copied().unwrap_or_default().min(cfg.duration);
    let safe = shortest.mul_f64(1.0 - cfg.max_drift).saturating_sub(EPSILON);
    t0 + safe
}

/// A random wait in `[0, max)`.
fn jitter(max: Duration) -> Duration {
    let mut b = [0u8; 8];
    if max.is_zero() || getrandom::fill(&mut b).is_err() {
        return Duration::ZERO;
    }
    let ms = max.as_millis().max(1) as u64;
    Duration::from_millis(u64::from_le_bytes(b) % ms)
}

fn wall_ms() -> u64 {
    std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_millis() as u64).unwrap_or(0)
}

struct Shared {
    state: Mutex<(LeaseState, Option<LeaseHandle>)>,
    changed: Condvar,
    stop: AtomicBool,
}

/// Acquires and renews one lease in a background thread. No preemption: a live holder keeps
/// it; after a lapse the lowest-ranked candidate tries first. Dropping it (or
/// [`release`](Self::release)) lets go.
pub struct Elector {
    shared: Arc<Shared>,
    thread: Option<std::thread::JoinHandle<()>>,
}

impl Elector {
    fn start(leases: Leases, name: LeaseName, cfg: LeaseConfig) -> Self {
        let shared = Arc::new(Shared { state: Mutex::new((LeaseState::Vacant, None)), changed: Condvar::new(), stop: AtomicBool::new(false) });
        let s = shared.clone();
        let thread =
            std::thread::Builder::new().name(format!("lease {name}")).spawn(move || run_elector(leases, name, cfg, s)).expect("spawn lease thread");
        Self { shared, thread: Some(thread) }
    }

    pub fn state(&self) -> LeaseState {
        self.shared.state.lock().map(|s| s.0.clone()).unwrap_or(LeaseState::Vacant)
    }

    /// The lease while held and valid.
    pub fn handle(&self) -> Option<LeaseHandle> {
        self.shared.state.lock().ok().and_then(|s| s.1.clone()).filter(|h| h.is_valid())
    }

    /// Wait until `want` holds for the state, at most `timeout`. Returns the state then.
    pub fn wait_for(&self, timeout: Duration, want: impl Fn(&LeaseState) -> bool) -> LeaseState {
        let deadline = Instant::now() + timeout;
        let mut g = match self.shared.state.lock() {
            Ok(g) => g,
            Err(_) => return LeaseState::Vacant,
        };
        while !want(&g.0) {
            let left = deadline.saturating_duration_since(Instant::now());
            if left.is_zero() {
                break;
            }
            g = match self.shared.changed.wait_timeout(g, left) {
                Ok((g, _)) => g,
                Err(_) => return LeaseState::Vacant,
            };
        }
        g.0.clone()
    }

    /// Stop and let go of the lease.
    pub fn release(mut self) {
        self.stop_now();
    }

    fn stop_now(&mut self) {
        self.shared.stop.store(true, Ordering::SeqCst);
        self.shared.changed.notify_all();
        if let Some(t) = self.thread.take() {
            let _ = t.join();
        }
    }
}

impl Drop for Elector {
    fn drop(&mut self) {
        self.stop_now();
    }
}

fn set(shared: &Shared, state: LeaseState, handle: Option<LeaseHandle>) {
    if let Ok(mut g) = shared.state.lock() {
        *g = (state, handle);
    }
    shared.changed.notify_all();
}

fn sleep(shared: &Shared, d: Duration) {
    let deadline = Instant::now() + d;
    while !shared.stop.load(Ordering::SeqCst) {
        let left = deadline.saturating_duration_since(Instant::now());
        if left.is_zero() {
            return;
        }
        std::thread::sleep(left.min(Duration::from_millis(20)));
    }
}

fn run_elector(leases: Leases, name: LeaseName, cfg: LeaseConfig, shared: Arc<Shared>) {
    let mut held: Option<LeaseHandle> = None;
    while !shared.stop.load(Ordering::SeqCst) {
        let (me, voters) = match leases.voters(&cfg) {
            Ok(v) => v,
            Err(_) => return,
        };
        let rank = voters.iter().position(|v| *v == me).unwrap_or(voters.len()) as u32;
        if let Some(h) = held.as_mut() {
            sleep(&shared, cfg.renew_every.min(h.remaining()));
            if shared.stop.load(Ordering::SeqCst) {
                break;
            }
            match leases.renew(h, &cfg) {
                Ok(()) => set(&shared, LeaseState::Held { term: h.term }, Some(h.clone())),
                Err(LeaseError::NoQuorum { .. }) if h.is_valid() => {}
                Err(_) => {
                    held = None;
                    set(&shared, LeaseState::Vacant, None);
                }
            }
            continue;
        }
        match leases.acquire(&name, &cfg) {
            Ok(h) => {
                set(&shared, LeaseState::Held { term: h.term }, Some(h.clone()));
                held = Some(h);
            }
            Err(LeaseError::Held { holder, term, retry_in }) => {
                set(&shared, LeaseState::Follower { holder, term }, None);
                sleep(&shared, retry_in.min(cfg.duration) + cfg.stagger * rank + jitter(cfg.stagger));
            }
            Err(LeaseError::NoQuorum { reached, needed }) => {
                // Two candidates that split the votes back off by rank plus a random part, so
                // they don't keep colliding.
                set(&shared, LeaseState::NoQuorum { reached, needed }, None);
                let base = (cfg.stagger * (rank + 1)).min(cfg.renew_every.max(Duration::from_millis(50)));
                sleep(&shared, base + jitter(cfg.stagger * 2));
            }
            Err(_) => {
                set(&shared, LeaseState::Vacant, None);
                sleep(&shared, cfg.renew_every.max(Duration::from_millis(50)));
            }
        }
    }
    if let Some(h) = held {
        leases.release(h, &cfg);
    }
    set(&shared, LeaseState::Vacant, None);
}

/// Resource side: refuses commands that carry an older term than one already seen. Persist
/// [`export`](Self::export) so a restart keeps refusing.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct FenceGuard {
    seen: HashMap<LeaseName, Term>,
}

/// A command from an older term than the guard has seen.
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
#[error("stale term: {seen} already seen")]
pub struct Stale {
    pub seen: Term,
}

impl FenceGuard {
    pub fn new() -> Self {
        Self::default()
    }

    /// Accepts `term` if it is at least the highest seen (and records it).
    pub fn check(&mut self, name: &LeaseName, term: Term) -> std::result::Result<(), Stale> {
        let seen = self.seen.entry(name.clone()).or_default();
        if term < *seen {
            return Err(Stale { seen: *seen });
        }
        *seen = term;
        Ok(())
    }

    pub fn seen(&self, name: &LeaseName) -> Option<Term> {
        self.seen.get(name).copied()
    }

    pub fn export(&self) -> Vec<u8> {
        let mut v: Vec<_> = self.seen.iter().map(|(n, t)| (n.clone(), *t)).collect();
        v.sort();
        encode(&v).unwrap_or_default()
    }

    pub fn import(bytes: &[u8]) -> Result<Self> {
        let v: Vec<(LeaseName, Term)> = decode(bytes)?;
        Ok(Self { seen: v.into_iter().collect() })
    }
}

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

    fn name(s: &str) -> LeaseName {
        LeaseName::new(s).unwrap()
    }

    fn ask(n: &str, term: u64, renew: bool) -> LeaseAsk {
        LeaseAsk { name: name(n), term: Term(term), duration_ms: 1_000, renew, voters: String::new() }
    }

    /// An ask for an hour, so the voter's limits decide the grant.
    fn ask_long(n: &str, term: u64, renew: bool) -> LeaseAsk {
        LeaseAsk { duration_ms: 3_600_000, ..ask(n, term, renew) }
    }

    fn fresh() -> Voter {
        let mut v = Voter::new(Voters::Peers(vec![])).with_max_duration(Duration::from_secs(1));
        v.grace_until = Instant::now() - Duration::from_secs(1);
        v
    }

    #[test]
    fn a_voter_promises_one_holder_at_a_time() {
        let mut v = fresh();
        let t = Instant::now();
        assert!(matches!(v.on_ask("a", &ask("s", 1, false), t), Message::LeaseGrant(_)));
        // Another candidate, even with a higher term, waits for the promise to end.
        let Message::LeaseDeny(d) = v.on_ask("b", &ask("s", 2, false), t + Duration::from_millis(10)) else { panic!() };
        assert_eq!((d.holder.as_str(), d.promised, d.max_term), ("a", Term(1), Term(1)));
        assert!(d.remaining_ms > 900);
        // The holder renews; b still can't.
        assert!(matches!(v.on_ask("a", &ask("s", 1, true), t + Duration::from_millis(500)), Message::LeaseGrant(_)));
        assert!(matches!(v.on_ask("b", &ask("s", 2, false), t + Duration::from_millis(1_200)), Message::LeaseDeny(_)));
        // After it lapses b takes over with a higher term, never the same or a lower one.
        let later = t + Duration::from_millis(1_600);
        assert!(matches!(v.on_ask("b", &ask("s", 1, false), later), Message::LeaseDeny(_)));
        assert!(matches!(v.on_ask("b", &ask("s", 2, false), later), Message::LeaseGrant(_)));
        // a's renewal of the old term is refused now.
        assert!(matches!(v.on_ask("a", &ask("s", 1, true), later), Message::LeaseDeny(_)));
        // Release lets the next one in at once.
        v.on_free("b", &LeaseFree { name: name("s"), term: Term(2) });
        assert!(matches!(v.on_ask("a", &ask("s", 3, false), later), Message::LeaseGrant(_)));
        // Durations are capped.
        let mut long = ask("t", 1, false);
        long.duration_ms = 600_000;
        let Message::LeaseGrant(g) = v.on_ask("a", &long, later) else { panic!() };
        assert_eq!(g.duration_ms, 1_000);
    }

    #[test]
    fn a_restarted_voter_refuses_takeovers_for_one_duration_and_keeps_its_terms() {
        let mut v = fresh();
        let t = Instant::now();
        v.on_ask("a", &ask("s", 4, false), t);
        let bytes = v.export();
        let mut back = Voter::import(&bytes, Voters::Peers(vec![])).unwrap().with_max_duration(Duration::from_secs(1));
        assert_eq!(back.max_term(&name("s")), Term(4));
        let now = Instant::now();
        // b can't take over during the grace, a can renew the term it holds.
        let Message::LeaseDeny(d) = back.on_ask("b", &ask("s", 5, false), now) else { panic!() };
        assert_eq!(d.holder, "a");
        assert!(d.remaining_ms > 0);
        assert!(matches!(back.on_ask("a", &ask("s", 4, true), now), Message::LeaseGrant(_)));
        // A new lease name is refused during the grace too.
        assert!(matches!(back.on_ask("b", &ask("other", 1, false), now), Message::LeaseDeny(_)));
    }

    #[test]
    fn a_new_term_is_stored_before_it_is_granted() {
        use std::sync::atomic::AtomicUsize;
        let writes = Arc::new(AtomicUsize::new(0));
        let fail = Arc::new(AtomicBool::new(false));
        let (w, f) = (writes.clone(), fail.clone());
        let mut v = fresh().with_persist(move |bytes| {
            if f.load(Ordering::SeqCst) {
                return Err(std::io::Error::other("disk full"));
            }
            assert!(Voter::import(bytes, Voters::Peers(vec![])).is_ok());
            w.fetch_add(1, Ordering::SeqCst);
            Ok(())
        });
        let t = Instant::now();
        assert!(matches!(v.on_ask("a", &ask("s", 1, false), t), Message::LeaseGrant(_)));
        assert_eq!(writes.load(Ordering::SeqCst), 1);
        // Renewals write nothing.
        assert!(matches!(v.on_ask("a", &ask("s", 1, true), t), Message::LeaseGrant(_)));
        assert_eq!(writes.load(Ordering::SeqCst), 1);
        // A term that can't be stored is not granted, and leaves no trace.
        fail.store(true, Ordering::SeqCst);
        let later = t + Duration::from_secs(2);
        assert!(matches!(v.on_ask("b", &ask("s", 2, false), later), Message::LeaseDeny(_)));
        assert_eq!(v.max_term(&name("s")), Term(1));
        fail.store(false, Ordering::SeqCst);
        assert!(matches!(v.on_ask("b", &ask("s", 2, false), later), Message::LeaseGrant(_)));
        assert_eq!(writes.load(Ordering::SeqCst), 2);
    }

    #[test]
    fn the_fence_refuses_older_terms() {
        let mut g = FenceGuard::new();
        let n = name("ymir.scheduler");
        g.check(&n, Term(3)).unwrap();
        g.check(&n, Term(3)).unwrap();
        assert_eq!(g.check(&n, Term(2)), Err(Stale { seen: Term(3) }));
        g.check(&n, Term(4)).unwrap();
        let back = FenceGuard::import(&g.export()).unwrap();
        assert_eq!(back.seen(&n), Some(Term(4)));
    }

    #[test]
    fn names_are_checked_and_the_holder_stops_before_any_voter() {
        assert!(LeaseName::new("ymir.jobs/42").is_ok());
        assert!(LeaseName::new("").is_err());
        assert!(LeaseName::new("bad name").is_err());
        assert!(LeaseName::new("x".repeat(201)).is_err());
        let cfg = LeaseConfig::for_role("r");
        let t0 = Instant::now();
        let until = valid_until(t0, &[Duration::from_secs(15), Duration::from_secs(10)], &cfg);
        assert!(until < t0 + Duration::from_millis(9_900) && until > t0 + Duration::from_millis(9_800));
        assert_eq!((cfg.duration, cfg.renew_every, cfg.stagger, cfg.max_drift), (DEFAULT_DURATION, DEFAULT_RENEW_EVERY, DEFAULT_STAGGER, 0.01));
    }

    #[test]
    fn skewed_clock_rates_within_the_drift_never_overlap_two_holders() {
        // The holder counts from before it sent the ask, the voter from when it got it. With
        // clock rates up to half the drift apart either way, the holder's lease (in real time)
        // ends before the voter's promise does, so a takeover can't overlap it.
        let cfg = LeaseConfig::for_role("r");
        let t0 = Instant::now();
        let safe = valid_until(t0, &[cfg.duration], &cfg) - t0;
        let half = cfg.max_drift / 2.0;
        for (holder_rate, voter_rate) in [(1.0 - half, 1.0 + half), (1.0, 1.0 + cfg.max_drift - 1e-9), (1.0 + half, 1.0 - half), (1.0, 1.0)] {
            let holder_real = safe.as_secs_f64() / holder_rate;
            let voter_real = cfg.duration.as_secs_f64() / voter_rate;
            assert!(holder_real < voter_real, "holder {holder_rate} voter {voter_rate}: {holder_real} >= {voter_real}");
        }
        // Wall clocks play no part: a lease is the same however far apart they are.
        assert_eq!(valid_until(t0, &[cfg.duration], &cfg), t0 + safe);
    }

    #[test]
    fn prefix_limits_allow_longer_grants_and_hold_off_after_a_lapse() {
        let long = Duration::from_secs(60);
        let wait = Duration::from_secs(300);
        let mut v = Voter::new(Voters::Peers(vec![])).with_max_duration(Duration::from_millis(10)).with_limits("ymir.train/", long, wait);
        let t0 = Instant::now() + long; // past the grace
        let n = "ymir.train/j1";
        match v.on_ask("a", &ask_long(n, 1, false), t0) {
            Message::LeaseGrant(g) => assert_eq!(g.duration_ms, 60_000, "the prefix's 60 s, not the default maximum"),
            m => panic!("{m:?}"),
        }
        match v.on_ask("a", &ask_long("other", 1, false), t0) {
            Message::LeaseGrant(g) => assert_eq!(g.duration_ms, 10),
            m => panic!("{m:?}"),
        }
        // a stops renewing: its promise lapses at t0 + 60 s, and b waits 5 more minutes.
        let lapsed = t0 + long + Duration::from_secs(1);
        match v.on_ask("b", &ask_long(n, 2, false), lapsed) {
            Message::LeaseDeny(d) => {
                assert_eq!((d.holder.as_str(), d.promised), ("a", Term(1)));
                assert_eq!(d.remaining_ms, 299_000);
            }
            m => panic!("{m:?}"),
        }
        // a itself may come back on its term inside the wait (a network blip).
        assert!(matches!(v.on_ask("a", &ask_long(n, 1, true), lapsed), Message::LeaseGrant(_)));
        let later = lapsed + long + wait;
        assert!(matches!(v.on_ask("b", &ask_long(n, 2, false), later), Message::LeaseGrant(_)));
        // A released lease has no wait.
        v.on_free("b", &LeaseFree { name: name(n), term: Term(2) });
        assert!(matches!(v.on_ask("c", &ask_long(n, 3, false), later), Message::LeaseGrant(_)));
        // A job's own limits replace the prefix's.
        let job = LeaseLimits::new(Duration::from_secs(20), Duration::ZERO);
        match v.on_ask_with("c", &ask_long(n, 3, true), later, Some(job)) {
            Message::LeaseGrant(g) => assert_eq!(g.duration_ms, 20_000),
            m => panic!("{m:?}"),
        }
    }

    #[test]
    fn a_restarted_voter_waits_out_its_longest_grant() {
        let long = Duration::from_secs(60);
        let mut v = Voter::new(Voters::Peers(vec![])).with_max_duration(Duration::from_millis(10)).with_limits("j/", long, Duration::ZERO);
        assert!(matches!(v.on_ask("a", &ask_long("j/1", 1, false), Instant::now() + long), Message::LeaseGrant(_)));
        let bytes = v.export();
        // Restarted without the prefix limits: the stored longest grant still sets the grace.
        let mut r = Voter::import(&bytes, Voters::Peers(vec![])).unwrap().with_max_duration(Duration::from_millis(10));
        let now = Instant::now() + Duration::from_secs(30);
        assert!(matches!(r.on_ask("b", &ask_long("j/1", 2, false), now), Message::LeaseDeny(_)), "still inside a's possible 60 s");
        // A 0.8.0 voter.cbor (no longest grant) still loads.
        #[derive(Serialize)]
        struct Old {
            v: u8,
            terms: Vec<(LeaseName, Term, String)>,
        }
        let old = encode(&Old { v: 1, terms: vec![(name("j/1"), Term(4), "a".into())] }).unwrap();
        assert_eq!(Voter::import(&old, Voters::Peers(vec![])).unwrap().max_term(&name("j/1")), Term(4));
    }
}