mise 2026.9.3

Dev tools, env vars, and tasks in one CLI
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
//! Per-file adaptive save scheduling. Ordinary edits save promptly: a
//! changed path is saved once it has been quiet for its settle time.
//! Sustained churn stretches that path's own interval — doubling on every
//! save that follows another one closely with several changes in between,
//! up to a maximum — so a constantly rewritten file is still saved
//! periodically but never floods the history. A busy path never delays an
//! ordinary one: every path is scheduled on its own. When a churning path
//! settles, its final state is captured after its settle time, and a
//! sustained quiet period resets the interval so a brief pause does not
//! restart aggressive saving.
//!
//! Every threshold is a named constant here; the schedule is pure and
//! clock-injected so each rule is tested without a filesystem.

use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};

use serde::{Deserialize, Serialize};

/// A save counts as churn — and doubles the interval — when the path
/// changed again within its settle time after the previous save (it never
/// really stopped) and at least this many changes arrived since. A person
/// saving from an editor every few seconds leaves gaps longer than the
/// settle time, so ordinary editing is never stretched.
pub(crate) const CHURN_CHANGES: u32 = 2;
/// A path quiet for this many intervals (at least `RESET_MIN`) forgets its
/// backoff.
pub(crate) const RESET_FACTOR: u32 = 4;
pub(crate) const RESET_MIN: Duration = Duration::from_secs(5 * 60);
/// The settle time of a stretched path: a fraction of its interval, never
/// below the base quiet period nor above this cap, so a settled file is
/// captured promptly whatever its interval grew to.
pub(crate) const SETTLE_DIVISOR: u32 = 8;
pub(crate) const SETTLE_MAX: Duration = Duration::from_secs(5 * 60);
/// A path whose interval reached this is reported as heavily throttled.
pub(crate) const HEAVY_INTERVAL: Duration = Duration::from_secs(60 * 60);

#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct Limits {
    /// The base quiet period (`history.watch.debounce`).
    pub base: Duration,
    /// The longest periodic interval (`history.watch.max_interval`).
    pub max: Duration,
}

impl Limits {
    fn clamp(&self, interval: Duration) -> Duration {
        interval.max(self.base).min(self.max.max(self.base))
    }
}

/// One path's scheduling state.
#[derive(Clone, Debug)]
pub(crate) struct PathSchedule {
    /// The current periodic interval.
    pub interval: Duration,
    /// When the path last changed (`None`: no change pending).
    pub last_change: Option<Instant>,
    /// Last observed event, retained after its pending batch is saved.
    pub last_seen: Option<Instant>,
    /// When the pending batch of changes began.
    pub pending_since: Option<Instant>,
    /// Changes since the last save.
    pub changes: u32,
    pub last_saved: Option<Instant>,
}

impl PathSchedule {
    fn new(base: Duration) -> Self {
        Self {
            interval: base,
            last_change: None,
            last_seen: None,
            pending_since: None,
            changes: 0,
            last_saved: None,
        }
    }

    pub(crate) fn pending(&self) -> bool {
        self.last_change.is_some()
    }

    /// Quiet needed before a pending change is saved.
    fn settle(&self, limits: &Limits) -> Duration {
        // never below the configured quiet period, even one above the cap
        (self.interval / SETTLE_DIVISOR)
            .max(limits.base)
            .min(SETTLE_MAX.max(limits.base))
    }

    /// When this path's pending change is due: quiet for its settle time,
    /// or its interval since the batch began, whichever comes first.
    pub(crate) fn due(&self, limits: &Limits) -> Option<Instant> {
        let last = self.last_change?;
        let quiet = last + self.settle(limits);
        let periodic = self.pending_since.unwrap_or(last) + self.interval;
        Some(quiet.min(periodic))
    }

    fn reset_after(&self) -> Duration {
        (self.interval * RESET_FACTOR).max(RESET_MIN)
    }
}

/// The persisted part: what a restart must not forget.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub(crate) struct PersistedSchedule {
    #[serde(default)]
    pub paths: BTreeMap<String, PersistedPath>,
}

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub(crate) struct PersistedPath {
    pub interval_secs: u64,
    /// When the path was last saved (unix seconds), for the reset rule and
    /// for telling a changed file from a quiet one after a restart.
    #[serde(default)]
    pub saved_epoch_secs: Option<u64>,
    /// Changes seen since that save, and when the pending batch began and
    /// last changed, so a restart neither forgets a pending save nor takes
    /// it early.
    #[serde(default)]
    pub pending_changes: u32,
    #[serde(default)]
    pub pending_since_epoch_secs: Option<u64>,
    #[serde(default)]
    pub last_change_epoch_secs: Option<u64>,
    #[serde(default)]
    pub last_seen_epoch_secs: Option<u64>,
}

#[derive(Debug)]
pub(crate) struct Schedule {
    limits: Limits,
    paths: BTreeMap<PathBuf, PathSchedule>,
}

impl Schedule {
    /// Forgets every path `keep` rejects: one the configuration no longer
    /// autosaves (excluded, untracked, switched to manual saving) must not
    /// be held or carried forward by any later capture.
    pub(crate) fn retain(&mut self, keep: impl Fn(&Path) -> bool) {
        self.paths.retain(|path, _| keep(path));
    }
}

/// What a save of a path reported: whether its interval changed.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Adjustment {
    Unchanged,
    Stretched,
    Reset,
}

impl Schedule {
    pub(crate) fn new(limits: Limits) -> Self {
        Self {
            limits,
            paths: BTreeMap::new(),
        }
    }

    pub(crate) fn limits(&self) -> &Limits {
        &self.limits
    }

    /// Restores stretched intervals from a previous run, with their last
    /// save and pending batch placed on this run's clock (`now` is the
    /// instant that corresponds to the wall clock `now_epoch`), so the
    /// schedule continues where it stopped: a path quiet longer than its
    /// reset period comes back at the base interval, and a pending save is
    /// neither forgotten nor taken early.
    pub(crate) fn restore(&mut self, persisted: &PersistedSchedule, now: Instant, now_epoch: u64) {
        let ago = |epoch: u64| {
            now.checked_sub(Duration::from_secs(now_epoch.saturating_sub(epoch)))
                .unwrap_or(now)
        };
        for (path, record) in &persisted.paths {
            let interval = self.limits.clamp(Duration::from_secs(record.interval_secs));
            if interval <= self.limits.base {
                continue;
            }
            let mut schedule = PathSchedule::new(self.limits.base);
            schedule.interval = interval;
            let quiet_for = record
                .saved_epoch_secs
                .map(|saved| now_epoch.saturating_sub(saved));
            if quiet_for.is_some_and(|quiet| Duration::from_secs(quiet) >= schedule.reset_after()) {
                continue;
            }
            schedule.last_saved = record.saved_epoch_secs.map(ago);
            schedule.last_seen = record
                .last_seen_epoch_secs
                .or(record.last_change_epoch_secs)
                .map(ago);
            if record.pending_changes > 0 || record.last_change_epoch_secs.is_some() {
                schedule.changes = record.pending_changes;
                schedule.pending_since =
                    Some(record.pending_since_epoch_secs.map(ago).unwrap_or(now));
                schedule.last_change = Some(record.last_change_epoch_secs.map(ago).unwrap_or(now));
            }
            self.paths.insert(PathBuf::from(path), schedule);
        }
    }

    /// The persisted form. Only stretched paths matter.
    pub(crate) fn persist(&self, now: Instant, now_epoch: u64) -> PersistedSchedule {
        let mut out = PersistedSchedule::default();
        let epoch =
            |at: Instant| now_epoch.saturating_sub(now.saturating_duration_since(at).as_secs());
        for (path, schedule) in &self.paths {
            if schedule.interval <= self.limits.base {
                continue;
            }
            out.paths.insert(
                path.to_string_lossy().into_owned(),
                PersistedPath {
                    interval_secs: schedule.interval.as_secs(),
                    saved_epoch_secs: schedule.last_saved.map(epoch),
                    pending_changes: schedule.changes,
                    pending_since_epoch_secs: schedule.pending_since.map(epoch),
                    last_change_epoch_secs: schedule.last_change.map(epoch),
                    last_seen_epoch_secs: schedule.last_seen.map(epoch),
                },
            );
        }
        out
    }

    /// Applies new limits (a settings change while running): every interval
    /// is clamped into the new range.
    pub(crate) fn set_limits(&mut self, limits: Limits) {
        for schedule in self.paths.values_mut() {
            schedule.interval = limits.clamp(schedule.interval);
        }
        self.limits = limits;
    }

    /// Whether `path` is throttled (its interval is above the base).
    pub(crate) fn is_throttled(&self, path: &Path) -> bool {
        self.paths
            .get(path)
            .is_some_and(|schedule| schedule.interval > self.limits.base)
    }

    /// A change to `path` was seen.
    pub(crate) fn note(&mut self, path: PathBuf, now: Instant) {
        let base = self.limits.base;
        let schedule = self
            .paths
            .entry(path)
            .or_insert_with(|| PathSchedule::new(base));
        // a path quiet for its reset period comes back at the base interval
        if let Some(saved) = schedule.last_saved
            && schedule.last_change.is_none()
            && now.saturating_duration_since(saved) >= schedule.reset_after()
        {
            schedule.interval = base;
        }
        schedule.pending_since.get_or_insert(now);
        schedule.last_change = Some(now);
        schedule.last_seen = Some(now);
        schedule.changes += 1;
    }

    /// A change to `path` may have happened (a same-second ambiguity after
    /// a restart): the path is pending, without a change counted toward
    /// churn detection.
    pub(crate) fn mark_pending(&mut self, path: PathBuf, now: Instant) {
        let base = self.limits.base;
        let schedule = self
            .paths
            .entry(path)
            .or_insert_with(|| PathSchedule::new(base));
        schedule.pending_since.get_or_insert(now);
        schedule.last_change = Some(now);
    }

    /// The next moment any pending path is due.
    pub(crate) fn deadline(&self) -> Option<Instant> {
        self.paths
            .values()
            .filter_map(|schedule| schedule.due(&self.limits))
            .min()
    }

    /// The pending paths that are due now.
    pub(crate) fn due_paths(&self, now: Instant) -> Vec<PathBuf> {
        self.paths
            .iter()
            .filter(|(_, schedule)| schedule.due(&self.limits).is_some_and(|due| due <= now))
            .map(|(path, _)| path.clone())
            .collect()
    }

    /// The pending paths that are not due yet: their live content is held
    /// back from captures other paths trigger, so a capture for another
    /// path cannot defeat this path's throttling.
    pub(crate) fn held_paths(&self, now: Instant) -> Vec<PathBuf> {
        self.paths
            .iter()
            .filter(|(_, schedule)| schedule.due(&self.limits).is_some_and(|due| due > now))
            .map(|(path, _)| path.clone())
            .collect()
    }

    /// Records that `path` was saved now and adapts its interval.
    pub(crate) fn saved(&mut self, path: &Path, now: Instant) -> Adjustment {
        let base = self.limits.base;
        let Some(schedule) = self.paths.get_mut(path) else {
            return Adjustment::Unchanged;
        };
        let settle = schedule.settle(&self.limits);
        let churn = match (schedule.last_saved, schedule.pending_since) {
            (Some(previous), Some(since)) => {
                since.saturating_duration_since(previous) <= settle
                    && schedule.changes >= CHURN_CHANGES
            }
            _ => false,
        };
        let reset = schedule.last_saved.is_some_and(|previous| {
            now.saturating_duration_since(previous) >= schedule.reset_after()
        }) && schedule.interval > base;
        let adjustment = if churn && schedule.interval < self.limits.max {
            schedule.interval = self.limits.clamp(schedule.interval * 2);
            Adjustment::Stretched
        } else if reset {
            schedule.interval = base;
            Adjustment::Reset
        } else {
            Adjustment::Unchanged
        };
        schedule.last_saved = Some(now);
        schedule.last_change = None;
        schedule.pending_since = None;
        schedule.changes = 0;
        adjustment
    }

    /// Forgets pending changes for every path (after a full capture that
    /// read everything live, such as an explicit save or a shutdown).
    pub(crate) fn clear_pending(&mut self, now: Instant) {
        for schedule in self.paths.values_mut() {
            if schedule.pending() {
                schedule.last_saved = Some(now);
                schedule.last_change = None;
                schedule.pending_since = None;
                schedule.changes = 0;
            }
        }
    }

    /// Stretched paths, for status and doctor.
    pub(crate) fn throttled(&self) -> Vec<(PathBuf, &PathSchedule)> {
        self.paths
            .iter()
            .filter(|(_, schedule)| schedule.interval > self.limits.base)
            .map(|(path, schedule)| (path.clone(), schedule))
            .collect()
    }

    pub(crate) fn get(&self, path: &Path) -> Option<&PathSchedule> {
        self.paths.get(path)
    }

    /// Drops paths that are neither pending nor saved within their reset
    /// period (a recent save is what lets the next one be recognized as
    /// churn; a stretched path past its reset period would come back at the
    /// base interval anyway, so it is forgotten rather than reported).
    pub(crate) fn prune(&mut self, now: Instant) {
        self.paths.retain(|_, schedule| {
            schedule.pending()
                || schedule.last_saved.is_some_and(|saved| {
                    now.saturating_duration_since(saved) < schedule.reset_after()
                })
        });
    }
}

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

    fn secs(n: u64) -> Duration {
        Duration::from_secs(n)
    }

    fn limits() -> Limits {
        Limits {
            base: secs(2),
            max: secs(24 * 3600),
        }
    }

    #[test]
    fn marked_pending_survives_restart_without_new_events() {
        let start = Instant::now();
        let path = PathBuf::from("state.json");
        let mut schedule = Schedule::new(limits());
        schedule.note(path.clone(), start);
        schedule.paths.get_mut(&path).unwrap().interval = secs(16);
        schedule.saved(&path, start + secs(2));
        schedule.mark_pending(path.clone(), start + secs(3));
        let persisted = schedule.persist(start + secs(3), 103);
        let mut restarted = Schedule::new(limits());
        restarted.restore(&persisted, start + secs(4), 104);
        assert!(restarted.get(&path).unwrap().pending());
    }

    #[test]
    fn last_seen_survives_saves_persistence_and_restart() {
        let start = Instant::now();
        let path = PathBuf::from("state.json");
        let mut schedule = Schedule::new(limits());
        schedule.note(path.clone(), start);
        schedule.paths.get_mut(&path).unwrap().interval = secs(16);
        schedule.saved(&path, start + secs(2));
        assert_eq!(schedule.get(&path).unwrap().last_seen, Some(start));
        let first = schedule.persist(start + secs(3), 103);
        let later = schedule.persist(start + secs(5), 105);
        assert_eq!(first.paths["state.json"].last_seen_epoch_secs, Some(100));
        assert_eq!(later.paths["state.json"].last_seen_epoch_secs, Some(100));
        let mut restored = Schedule::new(limits());
        restored.restore(&later, start + secs(6), 106);
        assert_eq!(restored.get(&path).unwrap().last_seen, Some(start));
        // Reconciliation uncertainty is not evidence of a new event.
        restored.mark_pending(path.clone(), start + secs(7));
        assert_eq!(restored.get(&path).unwrap().last_seen, Some(start));
    }

    #[test]
    fn an_ordinary_edit_saves_after_the_base_quiet() {
        let start = Instant::now();
        let mut schedule = Schedule::new(limits());
        schedule.note("a".into(), start);
        assert_eq!(schedule.deadline(), Some(start + secs(2)));
        assert!(schedule.due_paths(start + secs(1)).is_empty());
        assert_eq!(
            schedule.due_paths(start + secs(2)),
            vec![PathBuf::from("a")]
        );
        assert_eq!(
            schedule.saved(Path::new("a"), start + secs(2)),
            Adjustment::Unchanged
        );
        assert_eq!(schedule.get(Path::new("a")).unwrap().interval, secs(2));
    }

    #[test]
    fn churn_stretches_the_interval_and_keeps_periodic_saves() {
        let start = Instant::now();
        let mut schedule = Schedule::new(limits());
        let path = PathBuf::from("state.json");
        let mut now = start;
        let mut saves = 0;
        // one change per second for a long time: saves keep happening, ever
        // more rarely
        for tick in 0..2000u64 {
            now = start + secs(tick);
            schedule.note(path.clone(), now);
            if schedule.due_paths(now).contains(&path) {
                schedule.saved(&path, now);
                saves += 1;
            }
        }
        let interval = schedule.get(&path).unwrap().interval;
        assert!(interval >= secs(256), "interval only reached {interval:?}");
        assert!(saves < 30, "{saves} saves for 2000 changes");
        // still pending and still due periodically, never later than the interval
        let due = schedule.get(&path).unwrap().due(schedule.limits()).unwrap();
        assert!(due <= now + interval);
    }

    #[test]
    fn a_settled_file_is_captured_promptly() {
        let start = Instant::now();
        let mut schedule = Schedule::new(limits());
        let path = PathBuf::from("state.json");
        for tick in 0..600u64 {
            let now = start + secs(tick);
            schedule.note(path.clone(), now);
            if schedule.due_paths(now).contains(&path) {
                schedule.saved(&path, now);
            }
        }
        let interval = schedule.get(&path).unwrap().interval;
        assert!(interval >= secs(64));
        // the last change settles: due after the settle time, not the interval
        let last = start + secs(600);
        schedule.note(path.clone(), last);
        let settle = schedule.get(&path).unwrap().settle(schedule.limits());
        assert!(settle <= SETTLE_MAX && settle >= secs(2));
        assert_eq!(
            schedule.get(&path).unwrap().due(schedule.limits()),
            Some(last + settle)
        );
    }

    #[test]
    fn a_busy_path_never_delays_an_ordinary_one() {
        let start = Instant::now();
        let mut schedule = Schedule::new(limits());
        for tick in 0..100u64 {
            schedule.note("busy".into(), start + secs(tick));
            for path in schedule.due_paths(start + secs(tick)) {
                schedule.saved(&path, start + secs(tick));
            }
        }
        let now = start + secs(100);
        schedule.note("quiet".into(), now);
        schedule.note("busy".into(), now);
        assert_eq!(
            schedule.due_paths(now + secs(2)),
            vec![PathBuf::from("quiet")]
        );
        assert!(
            schedule
                .held_paths(now + secs(2))
                .contains(&PathBuf::from("busy"))
        );
    }

    #[test]
    fn sustained_quiet_resets_but_a_brief_pause_does_not() {
        let start = Instant::now();
        let mut schedule = Schedule::new(limits());
        let path = PathBuf::from("state.json");
        let mut now = start;
        for tick in 0..300u64 {
            now = start + secs(tick);
            schedule.note(path.clone(), now);
            if schedule.due_paths(now).contains(&path) {
                schedule.saved(&path, now);
            }
        }
        // drain the pending change
        let settle = schedule.get(&path).unwrap().settle(schedule.limits());
        schedule.saved(&path, now + settle);
        let stretched = schedule.get(&path).unwrap().interval;
        assert!(stretched > secs(2));
        // a brief pause: still stretched
        schedule.note(path.clone(), now + settle + secs(60));
        assert_eq!(schedule.get(&path).unwrap().interval, stretched);
        schedule.saved(&path, now + settle + secs(62));
        // a long quiet: back to base on the next change
        let reset_after = schedule.get(&path).unwrap().reset_after();
        schedule.note(path.clone(), now + settle + secs(62) + reset_after);
        assert_eq!(schedule.get(&path).unwrap().interval, secs(2));
    }

    #[test]
    fn an_editor_saving_every_few_seconds_is_never_stretched() {
        let start = Instant::now();
        let mut schedule = Schedule::new(limits());
        let path = PathBuf::from(".zshrc");
        // a person saves from an editor every five seconds for ten minutes:
        // each change settles before the next, so nothing counts as churn
        for tick in (0..600u64).step_by(5) {
            let now = start + secs(tick);
            schedule.note(path.clone(), now);
            assert_eq!(schedule.due_paths(now + secs(2)), vec![path.clone()]);
            assert_eq!(schedule.saved(&path, now + secs(2)), Adjustment::Unchanged);
            schedule.prune(now + secs(2));
        }
        assert_eq!(schedule.get(&path).unwrap().interval, secs(2));
    }

    #[test]
    fn pruning_between_saves_keeps_what_churn_detection_needs() {
        let start = Instant::now();
        let mut schedule = Schedule::new(limits());
        let path = PathBuf::from("state.json");
        // a burst: saved, pruned, and immediately busy again
        schedule.note(path.clone(), start);
        schedule.note(path.clone(), start + secs(1));
        schedule.saved(&path, start + secs(2));
        schedule.prune(start + secs(2));
        assert!(
            schedule.get(&path).is_some(),
            "a recently saved path is kept"
        );
        schedule.note(path.clone(), start + secs(3));
        schedule.note(path.clone(), start + secs(4));
        assert_eq!(
            schedule.saved(&path, start + secs(5)),
            Adjustment::Stretched
        );
        assert_eq!(schedule.get(&path).unwrap().interval, secs(4));
        // a path saved long ago is dropped
        let mut old = Schedule::new(limits());
        old.note("once".into(), start);
        old.saved(Path::new("once"), start + secs(2));
        old.prune(start + secs(2) + RESET_MIN);
        assert!(old.get(Path::new("once")).is_none());
    }

    #[test]
    fn a_restart_continues_the_schedule_where_it_stopped() {
        let start = Instant::now();
        let mut schedule = Schedule::new(limits());
        let path = PathBuf::from("/tmp/state.json");
        for tick in 0..300u64 {
            let now = start + secs(tick);
            schedule.note(path.clone(), now);
            if schedule.due_paths(now).contains(&path) {
                schedule.saved(&path, now);
            }
        }
        // a change is pending and not due yet
        let stopped = start + secs(300);
        schedule.note(path.clone(), stopped);
        let pending_before = schedule.get(&path).unwrap().changes;
        let due_before = schedule.get(&path).unwrap().due(schedule.limits()).unwrap();
        assert!(due_before > stopped + secs(2));
        let persisted = schedule.persist(stopped, 1_000_000);
        // ten seconds later the watcher is back: the same save is due at the
        // same moment, the last save is remembered, and the path is held
        let restarted_at = stopped + secs(10);
        let mut restarted = Schedule::new(limits());
        restarted.restore(&persisted, restarted_at, 1_000_010);
        let restored = restarted.get(&path).unwrap();
        assert_eq!(restored.changes, pending_before);
        assert!(restored.last_saved.is_some());
        assert_eq!(restored.due(restarted.limits()), Some(due_before));
        assert!(restarted.held_paths(restarted_at).contains(&path));
        assert!(restarted.due_paths(restarted_at).is_empty());
        // an overdue save is due at once
        let mut late = Schedule::new(limits());
        late.restore(
            &persisted,
            due_before + secs(1),
            1_000_000 + 1 + (due_before - stopped).as_secs(),
        );
        assert!(late.due_paths(due_before + secs(1)).contains(&path));
    }

    #[test]
    fn new_limits_clamp_every_interval() {
        let start = Instant::now();
        let mut schedule = Schedule::new(limits());
        let path = PathBuf::from("state.json");
        for tick in 0..600u64 {
            let now = start + secs(tick);
            schedule.note(path.clone(), now);
            if schedule.due_paths(now).contains(&path) {
                schedule.saved(&path, now);
            }
        }
        assert!(schedule.get(&path).unwrap().interval > secs(16));
        schedule.set_limits(Limits {
            base: secs(2),
            max: secs(16),
        });
        assert_eq!(schedule.get(&path).unwrap().interval, secs(16));
        assert!(schedule.is_throttled(&path));
    }

    #[test]
    fn a_throttled_path_past_its_reset_period_is_forgotten() {
        let start = Instant::now();
        let mut schedule = Schedule::new(limits());
        let path = PathBuf::from("state.json");
        for tick in 0..300u64 {
            let now = start + secs(tick);
            schedule.note(path.clone(), now);
            if schedule.due_paths(now).contains(&path) {
                schedule.saved(&path, now);
            }
        }
        let settle = schedule.get(&path).unwrap().settle(schedule.limits());
        schedule.saved(&path, start + secs(300) + settle);
        let reset_after = schedule.get(&path).unwrap().reset_after();
        schedule.prune(start + secs(300) + settle + reset_after - secs(1));
        assert!(schedule.is_throttled(&path));
        schedule.prune(start + secs(300) + settle + reset_after);
        assert!(schedule.get(&path).is_none());
    }

    #[test]
    fn persisted_intervals_survive_a_restart_unless_quiet_long_enough() {
        let start = Instant::now();
        let mut schedule = Schedule::new(limits());
        let path = PathBuf::from("/tmp/state.json");
        for tick in 0..300u64 {
            let now = start + secs(tick);
            schedule.note(path.clone(), now);
            if schedule.due_paths(now).contains(&path) {
                schedule.saved(&path, now);
            }
        }
        let persisted = schedule.persist(start + secs(300), 1_000_000);
        let record = persisted.paths.get("/tmp/state.json").unwrap();
        assert!(record.interval_secs > 2);

        let mut restarted = Schedule::new(limits());
        restarted.restore(&persisted, start + secs(360), 1_000_060);
        assert_eq!(
            restarted.get(&path).unwrap().interval,
            secs(record.interval_secs)
        );

        let mut later = Schedule::new(limits());
        later.restore(
            &persisted,
            start + secs(300 + 24 * 3600),
            1_000_000 + 24 * 3600,
        );
        assert!(later.get(&path).is_none());
    }
}