shep-daemon 0.22.0

The supervision engine behind shep: process spawning, restart policy, the log plane, watch and cron restarts, and the RPC server
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
//! The registry: what gets armed, and what stops.

use super::*;
use shep_core::config::ResolvedApp;

/// The liveness-tier fixture app: `web` on `instances` instances, with a
/// TCP probe that reports only after `failure_threshold` failures.
fn liveness_app(failure_threshold: u32, instances: u32) -> ResolvedApp {
    app_with("web", |app| {
        app.instances = instances;
        app.liveness_probe = Some(ProbeConfig {
            failure_threshold,
            ..probe_config(ProbeKind::Tcp, "localhost:5432")
        });
    })
}

/// The interval the fixture app's probe carries, for a test's `interval * 3`
/// silence window.
fn probe_interval(app: &ResolvedApp) -> Duration {
    app.config()
        .liveness_probe
        .as_ref()
        .expect("the fixture just set one")
        .interval
        .as_duration()
}

#[tokio::test(start_paused = true)]
async fn extras_debug_names_the_seams_by_role_and_the_sleep_bound_by_value() {
    let rig = rig(Duration::from_secs(300));
    assert_eq!(
        format!("{:?}", rig.extras),
        r#"Extras { clock: "<dyn Clock>", enforcer: "<dyn LimitEnforcer>", max_cron_sleep: 300s, .. }"#
    );
}

#[tokio::test(start_paused = true)]
async fn instance_extras_debug_reports_an_arming_without_naming_the_enforcer() {
    let dir = tempfile::tempdir().unwrap();
    let paths = test_paths(&dir);
    let (handle, _rx, _fixture) = spawn_test_fixture();
    let rig = rig(DEFAULT_MAX_CRON_SLEEP);
    let mut registry = ExtrasRegistry::default();
    let capped = app_with("web", |app| app.max_memory = Some(MemSize::from_bytes(500)));
    let uncapped = app_with("api", |app| app.max_memory = None);

    registry.arm(
        &armed_entry(0, 0, 1000, capped, &paths),
        idle_prober(),
        &rig.extras,
        &handle,
    );
    registry.arm(
        &armed_entry(1, 0, 1001, uncapped, &paths),
        idle_prober(),
        &rig.extras,
        &handle,
    );

    assert_eq!(
        format!("{:?}", registry.instances[&0]),
        "InstanceExtras { limit_armed: true, liveness: None, .. }"
    );
    assert_eq!(
        format!("{:?}", registry.instances[&1]),
        "InstanceExtras { limit_armed: false, liveness: None, .. }"
    );
}

// Real sysinfo over this very test process, whose RSS is comfortably over
// one byte, on the paused clock the polling loop sleeps on.
#[tokio::test(start_paused = true)]
async fn real_extras_wire_the_enforcer_to_the_reports_channel() {
    let (breach_tx, mut breaches) = mpsc::channel(4);
    let (live_tx, _liveness) = mpsc::channel(4);
    let extras = Extras::real(
        ExtrasReports {
            breaches: breach_tx,
            liveness: live_tx,
        },
        Duration::from_secs(300),
    );
    assert_eq!(
        format!("{extras:?}"),
        r#"Extras { clock: "<dyn Clock>", enforcer: "<dyn LimitEnforcer>", max_cron_sleep: 300s, .. }"#,
        "`real` must carry the sleep bound it was handed, not re-derive one"
    );

    let limit = MemSize::from_bytes(1);
    extras.enforcer.arm(3, std::process::id(), limit);
    let breach = match tokio::time::timeout(EVENT_WAIT, breaches.recv()).await {
        Ok(Some(breach)) => breach,
        Ok(None) => panic!("the breach channel closed before a breach arrived"),
        Err(_) => panic!("timed out waiting for a breach from the real enforcer"),
    };
    assert_eq!(breach.id, 3);
    assert_eq!(breach.root_pid, std::process::id());
    assert_eq!(breach.limit, limit);
}

// The cwd is a real directory, so this app is one a watcher could really
// have been registered on.
#[tokio::test(start_paused = true)]
async fn an_app_configuring_no_extras_arms_nothing() {
    let dir = tempfile::tempdir().unwrap();
    let paths = test_paths(&dir);
    let root = tempfile::tempdir().unwrap();
    let (handle, _rx, _fixture) = spawn_test_fixture();
    let rig = rig(DEFAULT_MAX_CRON_SLEEP);
    let mut registry = ExtrasRegistry::default();

    let app = app_with("web", |app| {
        app.cwd = Some(root.path().display().to_string());
    });
    let entry = armed_entry(0, 0, 1000, app, &paths);
    registry.arm(&entry, idle_prober(), &rig.extras, &handle);

    assert!(
        registry.groups.is_empty(),
        "an app with neither cron_restart nor watch must arm no name-group tasks"
    );
    // Sampling is the exception: it is armed for every sheep with a pid.
    assert_eq!(
        format!("{:?}", registry.instances[&0]),
        "InstanceExtras { limit_armed: false, liveness: None, .. }",
        "an app with neither max_memory nor liveness_probe must arm nothing beyond sampling"
    );
    assert!(
        rig.enforcer.arms().is_empty(),
        "an app with no max_memory must not reach the enforcer at all"
    );
}

// The disarm at the end is the other half: a watch never dropped samples a
// dead pid forever, and hands its CPU baseline to whatever gets that number.
#[tokio::test(start_paused = true)]
async fn every_sheep_with_a_pid_is_watched_even_with_no_limit_and_no_probe() {
    let dir = tempfile::tempdir().unwrap();
    let paths = test_paths(&dir);
    let (handle, _rx, _fixture) = spawn_test_fixture();
    let rig = rig(DEFAULT_MAX_CRON_SLEEP);
    let mut registry = ExtrasRegistry::default();
    let app = app_with("web", |app| {
        app.max_memory = None;
        app.liveness_probe = None;
    });

    registry.arm(
        &armed_entry(7, 0, 4242, app, &paths),
        idle_prober(),
        &rig.extras,
        &handle,
    );

    assert_eq!(
        rig.extras.stats.watched_for_test(),
        vec![(7, 4242)],
        "an app with neither max_memory nor a liveness_probe is the ORDINARY case, and a \
         listing reporting `-` for every one of them is what this split exists to fix"
    );
    assert!(
        rig.enforcer.arms().is_empty(),
        "sampling is not enforcement: an app with no ceiling must still not be armed"
    );

    registry.disarm(7, "web");
    assert!(rig.extras.stats.watched_for_test().is_empty());
}

// A cron-restarting app is a member of its name group whatever it thinks of
// watching, so it is the one shape that reaches `arm_watch` unwatched. Its
// cwd is real, so a watcher really would register.
#[tokio::test(start_paused = true)]
async fn a_cron_only_app_with_a_real_cwd_arms_no_watch() {
    let dir = tempfile::tempdir().unwrap();
    let paths = test_paths(&dir);
    let root = tempfile::tempdir().unwrap();
    let (handle, _rx, _fixture) = spawn_test_fixture();
    let rig = rig(DEFAULT_MAX_CRON_SLEEP);
    let mut registry = ExtrasRegistry::default();
    let app = app_with("web", |app| {
        app.cron_restart = Some("0 * * * *".to_string());
        app.cwd = Some(root.path().display().to_string());
    });

    registry.arm(
        &armed_entry(0, 0, 1000, app, &paths),
        idle_prober(),
        &rig.extras,
        &handle,
    );

    let group = &registry.groups["web"];
    assert!(group.cron.is_some(), "the cron worker is what armed here");
    assert!(
        group.watch.is_none(),
        "an app that did not ask to be watched must get no watcher on its cwd"
    );
}

// `Etc/GMT+5` is UTC minus five, POSIX inverting the sign, so 05:00 local is
// 10:00Z. Read as UTC it fires five hours inside the silent window below.
#[tokio::test(start_paused = true)]
async fn a_cron_pattern_is_resolved_in_the_apps_own_timezone() {
    let dir = tempfile::tempdir().unwrap();
    let paths = test_paths(&dir);
    let (handle, mut rx, _fixture) = spawn_test_fixture();
    let rig = rig(DEFAULT_MAX_CRON_SLEEP);
    let mut registry = ExtrasRegistry::default();
    let app = app_with("web", |app| {
        app.cron_restart = Some("0 5 * * *".to_string());
        app.cron_timezone = Some("Etc/GMT+5".to_string());
    });
    handle.start(vec![app.clone()]).await.unwrap();

    registry.arm(
        &armed_entry(0, 0, 1000, app, &paths),
        idle_prober(),
        &rig.extras,
        &handle,
    );
    tokio::task::yield_now().await;

    // Six hours from midnight UTC: past a UTC reading of the pattern,
    // still four hours short of the app's own.
    assert_no_restart_within(&mut rx, "web", Duration::from_secs(6 * 3_600)).await;
    expect_restart(&mut rx, "web", Duration::from_secs(6 * 3_600)).await;
}

// An unresolvable cwd is the one config-shaped watch failure that survives
// normalization: `normalize` compiles both glob lists.
#[tokio::test(start_paused = true)]
async fn a_watch_root_that_will_not_resolve_costs_the_app_its_watch_and_nothing_else() {
    let dir = tempfile::tempdir().unwrap();
    let paths = test_paths(&dir);
    let root = tempfile::tempdir().unwrap();
    let (handle, _rx, _fixture) = spawn_test_fixture();
    let rig = rig(DEFAULT_MAX_CRON_SLEEP);
    let mut registry = ExtrasRegistry::default();
    let app = app_with("web", |app| {
        app.watch = true;
        app.cwd = Some(root.path().join("no-such-directory").display().to_string());
        app.cron_restart = Some("0 * * * *".to_string());
    });

    registry.arm(
        &armed_entry(0, 0, 1000, app, &paths),
        idle_prober(),
        &rig.extras,
        &handle,
    );

    let group = &registry.groups["web"];
    assert!(group.watch.is_none(), "the watch root cannot have resolved");
    assert!(
        group.cron.is_some(),
        "an unresolvable watch root must not cost this app its cron worker too"
    );
}

#[tokio::test(start_paused = true)]
async fn a_watch_root_that_will_not_resolve_says_in_the_log_which_app_lost_its_watch() {
    let dir = tempfile::tempdir().unwrap();
    let paths = test_paths(&dir);
    let root = tempfile::tempdir().unwrap();
    let (handle, _rx, _fixture) = spawn_test_fixture();
    let rig = rig(DEFAULT_MAX_CRON_SLEEP);
    let mut registry = ExtrasRegistry::default();
    let app = app_with("unwatchable", |app| {
        app.watch = true;
        app.cwd = Some(root.path().join("no-such-directory").display().to_string());
    });
    let entry = armed_entry(0, 0, 1000, app, &paths);

    let records = capture_logs(|| {
        registry.arm(&entry, idle_prober(), &rig.extras, &handle);
    });

    assert!(
        registry.groups["unwatchable"].watch.is_none(),
        "precondition: the watch root cannot have resolved"
    );
    assert!(
        records.contains("watch root could not be resolved"),
        "arming no watch must be reported, not swallowed: {records:?}"
    );
    assert!(
        records.contains("WARN"),
        "an app silently losing its watch is a warning, not a debug detail: {records:?}"
    );
    assert!(
        records.contains(r#"name="unwatchable""#),
        "the record must name the app that lost its watch: {records:?}"
    );
}

// The clock count is the only trace a second worker leaves: two
// `restart(Name)` commands racing the same sheep are collapsed by the
// actor's first-command-wins dedupe.
#[tokio::test(start_paused = true)]
async fn a_second_instance_of_a_name_arms_no_second_cron_worker() {
    let dir = tempfile::tempdir().unwrap();
    let paths = test_paths(&dir);
    let (handle, mut rx, _fixture) = spawn_test_fixture();
    let rig = rig(Duration::from_secs(600));
    let mut registry = ExtrasRegistry::default();
    let app = app_with("web", |app| {
        app.cron_restart = Some("0 * * * *".to_string());
        app.instances = 2;
    });
    handle.start(vec![app.clone()]).await.unwrap();

    for (id, instance, pid) in [(0, 0, 1000), (1, 1, 1001)] {
        let entry = armed_entry(id, instance, pid, app.clone(), &paths);
        registry.arm(&entry, idle_prober(), &rig.extras, &handle);
        // Lets the worker commit to its first `next` while the clock still
        // reads close to now: `advance` jumps first and polls after.
        tokio::task::yield_now().await;
    }

    assert_eq!(registry.groups.len(), 1, "one group, not one per instance");
    assert_eq!(
        registry.groups["web"].members,
        HashSet::from([0, 1]),
        "both instances must be recorded as keeping the group alive"
    );

    cross_one_hour().await;
    expect_restart(&mut rx, "web", EVENT_WAIT).await;
    assert!(
        rig.clock.reads() < 20,
        "one cron worker reads the clock ~13 times over this hour; two read ~26 (got {})",
        rig.clock.reads()
    );
}

#[tokio::test(start_paused = true)]
async fn a_respawn_re_arms_the_enforcer_with_the_new_pid() {
    let dir = tempfile::tempdir().unwrap();
    let paths = test_paths(&dir);
    let (handle, _rx, _fixture) = spawn_test_fixture();
    let rig = rig(DEFAULT_MAX_CRON_SLEEP);
    let mut registry = ExtrasRegistry::default();
    let limit = MemSize::from_bytes(500);
    let app = app_with("web", |app| app.max_memory = Some(limit));

    registry.arm(
        &armed_entry(4, 0, 1000, app.clone(), &paths),
        idle_prober(),
        &rig.extras,
        &handle,
    );
    registry.arm(
        &armed_entry(4, 0, 2000, app, &paths),
        idle_prober(),
        &rig.extras,
        &handle,
    );

    assert_eq!(
        rig.enforcer.arms(),
        vec![
            ArmCall {
                id: 4,
                root_pid: 1000,
                limit,
            },
            ArmCall {
                id: 4,
                root_pid: 2000,
                limit,
            },
        ]
    );
    assert_eq!(
        rig.enforcer.disarms(),
        vec![4],
        "a re-arm must undo the previous arming rather than leaking it"
    );
}

#[tokio::test(start_paused = true)]
async fn only_the_last_instance_leaving_stops_the_name_groups_cron_worker() {
    let dir = tempfile::tempdir().unwrap();
    let paths = test_paths(&dir);
    let (handle, mut rx, _fixture) = spawn_test_fixture();
    let rig = rig(DEFAULT_MAX_CRON_SLEEP);
    let mut registry = ExtrasRegistry::default();
    let app = app_with("web", |app| {
        app.cron_restart = Some("0 * * * *".to_string());
        app.instances = 2;
    });
    handle.start(vec![app.clone()]).await.unwrap();
    for (id, instance, pid) in [(0, 0, 1000), (1, 1, 1001)] {
        let entry = armed_entry(id, instance, pid, app.clone(), &paths);
        registry.arm(&entry, idle_prober(), &rig.extras, &handle);
        tokio::task::yield_now().await;
    }

    registry.disarm(0, "web");
    assert_eq!(
        registry.groups["web"].members,
        HashSet::from([1]),
        "a non-last disarm drops only its own membership"
    );
    cross_one_hour().await;
    // `ProcessSelector::Name` reaches both online instances, so one
    // occurrence produces two `Restart` events. A leftover would read as a
    // worker that outlived its disarm.
    expect_restart(&mut rx, "web", EVENT_WAIT).await;
    expect_restart(&mut rx, "web", EVENT_WAIT).await;

    registry.disarm(1, "web");
    assert!(
        !registry.groups.contains_key("web"),
        "the last instance leaving must take the group with it"
    );
    assert_no_restart_within(&mut rx, "web", PAST_THE_NEXT_OCCURRENCE).await;
}

#[tokio::test(start_paused = true)]
async fn disarming_an_id_that_was_never_armed_leaves_the_group_alone() {
    let dir = tempfile::tempdir().unwrap();
    let paths = test_paths(&dir);
    let (handle, mut rx, _fixture) = spawn_test_fixture();
    let rig = rig(DEFAULT_MAX_CRON_SLEEP);
    let mut registry = ExtrasRegistry::default();
    let app = app_with("web", |app| {
        app.cron_restart = Some("0 * * * *".to_string())
    });
    handle.start(vec![app.clone()]).await.unwrap();
    registry.arm(
        &armed_entry(0, 0, 1000, app, &paths),
        idle_prober(),
        &rig.extras,
        &handle,
    );
    tokio::task::yield_now().await;

    registry.disarm(99, "web"); // a name that exists, an id that does not
    registry.disarm(0, "other"); // an id that exists, a name that does not

    assert_eq!(registry.groups["web"].members, HashSet::from([0]));
    cross_one_hour().await;
    expect_restart(&mut rx, "web", EVENT_WAIT).await;
}

// The clock makes the claim: a fresh worker on this pattern reads it once
// before returning, so a second reading means a second worker.
#[tokio::test(start_paused = true)]
async fn a_cron_worker_that_ended_on_its_own_is_rebuilt_on_the_next_arm() {
    let dir = tempfile::tempdir().unwrap();
    let paths = test_paths(&dir);
    let (handle, _rx, _fixture) = spawn_test_fixture();
    let rig = rig(DEFAULT_MAX_CRON_SLEEP);
    let mut registry = ExtrasRegistry::default();
    // 30 February: a pattern croner parses and finds no occurrence for,
    // which is the `Ok(None)` arm `spawn_cron_worker` returns on.
    let app = app_with("web", |app| {
        app.cron_restart = Some("0 0 30 2 *".to_string());
    });

    registry.arm(
        &armed_entry(0, 0, 1000, app.clone(), &paths),
        idle_prober(),
        &rig.extras,
        &handle,
    );
    settle_finished(
        registry.groups["web"]
            .cron
            .as_ref()
            .expect("the first arm spawns a worker"),
    )
    .await;
    assert_eq!(
        rig.clock.reads(),
        1,
        "a worker on a pattern with no occurrence reads the clock once and ends"
    );

    registry.arm(
        &armed_entry(0, 0, 2000, app, &paths),
        idle_prober(),
        &rig.extras,
        &handle,
    );
    settle_finished(
        registry.groups["web"]
            .cron
            .as_ref()
            .expect("the re-arm must leave a worker behind"),
    )
    .await;
    assert_eq!(
        rig.clock.reads(),
        2,
        "a re-arm must rebuild a name-group task that ended on its own"
    );
}

// This app asks for a watch and gets none. Under a build-keyed membership it
// would be in no group at all, so stopping a later instance whose watch did
// arm would tear the watch down with this one still online.
#[tokio::test(start_paused = true)]
async fn an_instance_whose_watch_could_not_be_armed_still_joins_its_group() {
    let dir = tempfile::tempdir().unwrap();
    let paths = test_paths(&dir);
    let (handle, _rx, _fixture) = spawn_test_fixture();
    let rig = rig(DEFAULT_MAX_CRON_SLEEP);
    let mut registry = ExtrasRegistry::default();
    let limit = MemSize::from_bytes(500);
    // `canonicalize` failing is the one watch-arming failure `normalize`
    // lets through: it never checks that the cwd resolves.
    let missing = dir.path().join("no-such-directory");
    let app = app_with("web", |app| {
        app.watch = true;
        app.cwd = Some(missing.display().to_string());
        app.max_memory = Some(limit);
    });

    registry.arm(
        &armed_entry(0, 0, 1000, app, &paths),
        idle_prober(),
        &rig.extras,
        &handle,
    );

    let group = registry
        .groups
        .get("web")
        .expect("a watched app is a member of its group whether or not the watch armed");
    assert!(group.watch.is_none(), "this app's watch cannot have armed");
    assert_eq!(group.members, HashSet::from([0]));
    assert_eq!(
        rig.enforcer.arms(),
        vec![ArmCall {
            id: 0,
            root_pid: 1000,
            limit,
        }],
        "a watch that could not be armed must not take the memory limit with it"
    );
}

#[tokio::test(start_paused = true)]
async fn dropping_the_registry_stops_the_liveness_loop_it_armed() {
    let dir = tempfile::tempdir().unwrap();
    let paths = test_paths(&dir);
    let (handle, _rx, _fixture) = spawn_test_fixture();
    let mut rig = rig(DEFAULT_MAX_CRON_SLEEP);
    let app = liveness_app(1, 1);
    let interval = probe_interval(&app);

    let mut kept = ExtrasRegistry::default();
    kept.arm(
        &armed_entry(8, 1, 5678, app.clone(), &paths),
        failing_prober(),
        &rig.extras,
        &handle,
    );
    let mut discarded = ExtrasRegistry::default();
    discarded.arm(
        &armed_entry(7, 0, 1234, app, &paths),
        failing_prober(),
        &rig.extras,
        &handle,
    );
    drop(discarded);

    let failure = expect_liveness(&mut rig.liveness, EVENT_WAIT).await;
    assert_eq!(
        failure,
        LivenessReport {
            id: 8,
            pid: 5678,
            epoch: 1
        },
        "only the registry that is still alive may report"
    );
    assert_no_liveness_within(&mut rig.liveness, interval * 3).await;
}

// Two names, not two instances of one: the bus attributes a restart to a
// name, so the control and the subject have to be tellable apart on the
// wire. `kept` is that control.
#[tokio::test(start_paused = true)]
async fn dropping_the_registry_stops_the_name_group_worker_it_armed() {
    let dir = tempfile::tempdir().unwrap();
    let paths = test_paths(&dir);
    let (handle, mut rx, _fixture) = spawn_test_fixture();
    let rig = rig(DEFAULT_MAX_CRON_SLEEP);
    let hourly = |name: &str| {
        app_with(name, |app| {
            app.cron_restart = Some("0 * * * *".to_string());
        })
    };
    handle
        .start(vec![hourly("kept"), hourly("dropped")])
        .await
        .unwrap();

    let mut kept = ExtrasRegistry::default();
    kept.arm(
        &armed_entry(0, 0, 1000, hourly("kept"), &paths),
        idle_prober(),
        &rig.extras,
        &handle,
    );
    let mut discarded = ExtrasRegistry::default();
    discarded.arm(
        &armed_entry(1, 0, 1001, hourly("dropped"), &paths),
        idle_prober(),
        &rig.extras,
        &handle,
    );
    // Lets both workers commit to their first `next` while the clock still
    // reads close to now.
    tokio::task::yield_now().await;
    drop(discarded);

    cross_one_hour().await;
    expect_restart(&mut rx, "kept", EVENT_WAIT).await;
    assert_no_restart_within(&mut rx, "dropped", PAST_THE_NEXT_OCCURRENCE).await;
}

// A healthy liveness loop never ends on its own, so a stopped sheep would
// leak a task probing a pid that is gone.
#[tokio::test(start_paused = true)]
async fn disarming_an_id_stops_its_liveness_loop() {
    let dir = tempfile::tempdir().unwrap();
    let paths = test_paths(&dir);
    let (handle, _rx, _fixture) = spawn_test_fixture();
    let mut rig = rig(DEFAULT_MAX_CRON_SLEEP);
    let mut registry = ExtrasRegistry::default();
    let app = liveness_app(1, 2);
    let interval = probe_interval(&app);

    for (id, instance, pid) in [(0, 0, 1000), (1, 1, 1001)] {
        registry.arm(
            &armed_entry(id, instance, pid, app.clone(), &paths),
            failing_prober(),
            &rig.extras,
            &handle,
        );
    }
    registry.disarm(0, "web");

    let failure = expect_liveness(&mut rig.liveness, EVENT_WAIT).await;
    assert_eq!(
        failure,
        LivenessReport {
            id: 1,
            pid: 1001,
            epoch: 1
        },
        "only the instance that is still armed may report"
    );
    assert_no_liveness_within(&mut rig.liveness, interval * 3).await;
}

#[tokio::test(start_paused = true)]
async fn a_liveness_threshold_reports_this_instances_id_and_pid() {
    let dir = tempfile::tempdir().unwrap();
    let paths = test_paths(&dir);
    let (handle, _rx, _fixture) = spawn_test_fixture();
    let mut rig = rig(DEFAULT_MAX_CRON_SLEEP);
    let mut registry = ExtrasRegistry::default();
    let app = liveness_app(2, 1);
    let interval = probe_interval(&app);

    registry.arm(
        &armed_entry(7, 0, 1234, app, &paths),
        Arc::new(ScriptedProber::new(vec![Err(ProbeFailure::Timeout)])),
        &rig.extras,
        &handle,
    );

    let failure = expect_liveness(&mut rig.liveness, EVENT_WAIT).await;
    assert_eq!(
        failure,
        LivenessReport {
            id: 7,
            pid: 1234,
            epoch: 1
        }
    );
    // The scripted prober repeats its last outcome forever, so a loop still
    // probing after its report would report again inside this window.
    assert_no_liveness_within(&mut rig.liveness, interval * 3).await;
}

// `notify-debouncer-full` derives its poll tick as `delay / 4` and sleeps it
// on a dedicated OS thread, so a zero makes that thread spin. A direct call,
// since `normalize` refuses `watch_delay = "0"` and no fixture can carry one
// as far as `ExtrasRegistry::arm`.
#[test]
fn a_zero_watch_delay_is_floored_before_it_reaches_the_debouncer() {
    let mut app = AppConfig::minimal("web", "./srv");
    app.watch_delay = Some(UpDuration::from_millis(0));
    assert_eq!(watch_delay_for(&app), MIN_WATCH_DELAY);
}

// `normalize` accepts every non-zero `watch_delay`, so a floor above one
// millisecond would lengthen a round trip the user shortened on purpose.
#[test]
fn a_watch_delay_the_config_layer_accepts_is_never_clamped() {
    let mut app = AppConfig::minimal("web", "./srv");
    app.watch_delay = Some(UpDuration::from_millis(1));
    assert_eq!(watch_delay_for(&app), Duration::from_millis(1));

    app.watch_delay = Some(UpDuration::from_millis(20));
    assert_eq!(watch_delay_for(&app), Duration::from_millis(20));

    app.watch_delay = None;
    assert_eq!(watch_delay_for(&app), DEFAULT_WATCH_DELAY);
}