shep-daemon 0.15.2

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
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
//! The muster roll: persisted flock state for restart-survival (`shep muster`)
//!
//! `FlockRegistry::roll` turns the registry's [`AppConfig`]s plus a live
//! [`ProcessInfo`] listing into a [`FlockSnapshot`], written to `flock.json`
//! by `write_atomic`. A `SnapshotWriter` task debounces lifecycle events so a
//! restart storm produces one write. `restorable` re-validates every entry,
//! since the file is human-editable.
//!
//! `muster` reads the roll and starts those apps, from `boot` and from the
//! `Muster` request alike, stage by stage in `depends_on` order
//! (`crate::boot_order`). An app restores iff it was running when the roll was
//! saved (`instances_running > 0`) and `autostart` is still true; one the flock
//! already has is left where it stands, still reported as restored. Nothing
//! about the graph refuses a restore: a cycle, a name nothing answers to and a
//! dependency that opted out of `autostart` or is disabled are each warned
//! about and started
//! around, because the machine this runs on has nobody watching it.

use core::fmt;

use std::collections::{BTreeMap, BTreeSet};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex, PoisonError};

use serde::{Deserialize, Serialize};
use tempfile::NamedTempFile;
use tokio::sync::Notify;
use tokio::sync::broadcast::{self, error::RecvError};
use tokio::task::JoinHandle;
use tokio::time::{Duration, Instant, sleep_until};

use shep_core::config::graph::{BootPlan, render_cycle};
use shep_core::config::{AppConfig, NormalizeError, ResolvedApp, normalize};
use shep_core::protocol::{BusEvent, ProcessInfo};
use shep_core::status::ProcStatus;

use crate::bus::{Bus, SharedEvent};
use crate::supervisor::{BatchPolicy, SupervisorHandle};

/// Schema version of `flock.json`
pub(crate) const SNAPSHOT_VERSION: u32 = 1;

/// How long the writer lets a burst of lifecycle events settle before it
/// rewrites the roll.
///
/// One restart emits Exit + Restart + Start + Online within microseconds;
/// 250 ms folds a whole restart storm into a single atomic write while still
/// landing the roll orders of magnitude faster than the reboot it protects
/// against (spec ยง13.4).
pub(crate) const SNAPSHOT_DEBOUNCE_MS: u64 = 250;

/// The muster roll: which apps were registered, and how many were up
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FlockSnapshot {
    /// Schema version this roll was written under (`SNAPSHOT_VERSION`)
    pub version: u32,
    /// Wall-clock milliseconds since the Unix epoch when this roll was built
    pub saved_at_ms: u64,
    /// One entry per sheep still known to the flock at save time
    pub apps: Vec<SavedApp>,
}

impl FlockSnapshot {
    /// A roll holding `apps`, under the schema version this build writes.
    ///
    /// `saved_at_ms` is left at zero, which is what a roll nothing has
    /// written yet carries: `FlockRegistry::roll` is the path that stamps a
    /// real clock reading, and `restorable` reads only `instances_running`.
    /// The version constant is not public, so this is the only way a caller
    /// outside this crate can build a roll the daemon will accept rather
    /// than hard-coding a number that stops being true when the schema
    /// moves.
    #[must_use]
    pub fn with_apps(apps: Vec<SavedApp>) -> Self {
        Self {
            version: SNAPSHOT_VERSION,
            saved_at_ms: 0,
            apps,
        }
    }
}

/// One sheep's entry in a [`FlockSnapshot`]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SavedApp {
    /// The config the sheep was started from (Debug redacts `env`, through
    /// [`AppConfig`]'s own redacting `Debug`)
    pub app: AppConfig,
    /// How many instances of this sheep were running when the roll was built
    pub instances_running: u32,
    /// Whether an operator disabled the app, so a restore keeps it down. A
    /// roll written before the mark existed has no key and reads `false`.
    #[serde(default)]
    pub disabled: bool,
}

/// The daemon's record of the config each registered sheep was started from
///
/// The supervisor owns runtime state; nothing in a [`ProcessInfo`] can
/// reproduce the `AppConfig` a sheep came from, which is exactly what a roll
/// needs. Cheap to clone (one `Arc`).
#[derive(Debug, Clone, Default)]
pub(crate) struct FlockRegistry {
    apps: Arc<Mutex<BTreeMap<String, AppConfig>>>,
    /// Woken by every write to `apps`, so the roll writer schedules a file
    /// write for a change that moves no process and so publishes no
    /// [`BusEvent::Process`] of its own.
    dirty: Arc<Notify>,
}

impl FlockRegistry {
    /// Builds an empty registry
    #[must_use]
    pub(crate) fn new() -> Self {
        Self::default()
    }

    /// Records (or re-records) each app's config, keyed by name.
    pub(crate) fn record(&self, apps: &[ResolvedApp]) {
        let mut map = self.apps.lock().unwrap_or_else(PoisonError::into_inner);
        for app in apps {
            map.insert(app.config().name.clone(), app.config().clone());
        }
        drop(map);
        self.dirty.notify_one();
    }

    /// Records one app's config directly, for a successor rebuilding this
    /// registry from a handover blob.
    ///
    /// [`Self::record`] takes [`ResolvedApp`]s because every other caller has
    /// just normalized one. A successor holds only the [`AppConfig`] the blob
    /// carried, and the supervisor re-normalizes that on its own way to
    /// installing the sheep.
    ///
    /// The roll is written from this registry, so a successor that left it
    /// empty would overwrite a good roll within seconds of taking over.
    #[cfg(unix)]
    pub(crate) fn record_config(&self, config: &AppConfig) {
        self.apps
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .insert(config.name.clone(), config.clone());
        self.dirty.notify_one();
    }

    /// Schedules a roll write without changing what is recorded.
    ///
    /// For a change the roll reads from the live listing rather than from
    /// this map, such as the disabled mark: on an app that is already stopped
    /// it moves no process, so the bus has nothing to wake the writer with.
    pub(crate) fn touch(&self) {
        self.dirty.notify_one();
    }

    /// Drops every recorded app, so the next [`Self::roll`] describes an
    /// empty flock regardless of what is still live in the supervisor's own
    /// listing.
    ///
    /// The one caller is [`crate::boot::RunningDaemon::run`]'s teardown, under
    /// `BootOptions::delete_flock_on_shutdown`: `shep dev`'s isolated session,
    /// where nothing should survive for a later `shep muster`. A production
    /// shutdown never calls it.
    pub(crate) fn clear(&self) {
        self.apps
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .clear();
        self.dirty.notify_one();
    }

    /// Every registered sheep's name, with the names it says it waits for.
    ///
    /// Infallible, one lock and no normalize: the teardown builds its stop
    /// plan from this, and a fallible step on the one path that always runs
    /// would silently skip the staged stop instead of failing loudly.
    pub(crate) fn depends_on_by_name(&self) -> BTreeMap<String, Vec<String>> {
        self.apps
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .iter()
            .map(|(name, app)| (name.clone(), app.depends_on.clone()))
            .collect()
    }

    /// `name`'s `listen_timeout` and `graceful_timeout`, in that order, or
    /// `None` when no registered sheep has that name.
    ///
    /// The pair an ordered restart or reload bounds a stage's wait by. Read
    /// here because a `ProcessInfo` carries no timeout at all, and the
    /// alternative is a round trip to the actor per member of a stage.
    pub(crate) fn timeouts_of(&self, name: &str) -> Option<(Duration, Duration)> {
        self.apps
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .get(name)
            .map(|app| {
                (
                    app.listen_timeout.as_duration(),
                    app.graceful_timeout.as_duration(),
                )
            })
    }

    /// Builds the roll from the live listing, pruning names the flock no
    /// longer has (a deleted sheep must not resurrect).
    #[must_use]
    pub(crate) fn roll(&self, infos: &[ProcessInfo], now_ms: u64) -> FlockSnapshot {
        // A poisoned lock recovers instead of panicking: the map is a plain
        // BTreeMap, so a panic elsewhere cannot leave it inconsistent, and
        // taking the daemon down over it would be the worse failure.
        let mut apps = self.apps.lock().unwrap_or_else(PoisonError::into_inner);
        // One pass over the listing: a name present at all survives the
        // prune, and its value is how many of its instances are up.
        let mut running: BTreeMap<&str, u32> = BTreeMap::new();
        let mut parked: BTreeSet<&str> = BTreeSet::new();
        for info in infos {
            let up = running.entry(info.name.as_str()).or_default();
            *up = up.saturating_add(u32::from(is_running(info.status)));
            if info.disabled == Some(true) {
                parked.insert(info.name.as_str());
            }
        }
        apps.retain(|name, _| running.contains_key(name.as_str()));
        let saved = apps
            .iter()
            .map(|(name, app)| SavedApp {
                app: app.clone(),
                instances_running: running.get(name.as_str()).copied().unwrap_or(0),
                disabled: parked.contains(name.as_str()),
            })
            .collect();
        FlockSnapshot {
            saved_at_ms: now_ms,
            ..FlockSnapshot::with_apps(saved)
        }
    }
}

/// True for the statuses [`FlockRegistry::roll`] counts as "up".
///
/// [`ProcStatus::Stopping`] is absent: a reload's drainee and its replacement
/// hold the same instance slot, so counting both would report two running
/// instances for one.
fn is_running(status: ProcStatus) -> bool {
    matches!(
        status,
        ProcStatus::Online | ProcStatus::Starting | ProcStatus::WaitingRestart
    )
}

/// Error type returned from `write_atomic` and [`read`]
///
/// Wraps `io::Error`/`serde_json::Error` directly rather than stringifying
/// them, so callers keep the underlying diagnostic through
/// [`core::error::Error::source`]. That costs the enum
/// `Clone`/`PartialEq`/`Eq`.
///
/// `#[non_exhaustive]`: a future roll-format refusal would need its own
/// variant, distinct from [`Self::Parse`]'s version refusal.
#[non_exhaustive]
#[derive(Debug)]
pub enum SnapshotError {
    /// The roll path has no parent directory to create the temp file in
    /// (carries the path)
    NoParent(PathBuf),
    /// The roll failed to serialize to JSON
    Encode(serde_json::Error),
    /// The temp file, `fsync`, rename, or read failed
    Io(std::io::Error),
    /// The roll on disk is not valid JSON for a [`FlockSnapshot`]
    Decode(serde_json::Error),
    /// The roll on disk parsed, but its `version` is one this daemon does
    /// not know how to restore (carries the version message)
    Parse(String),
}

impl fmt::Display for SnapshotError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::NoParent(path) => {
                write!(f, "roll path `{}` has no parent directory", path.display())
            }
            Self::Encode(err) => write!(f, "muster roll failed to serialize: {err}"),
            Self::Io(err) => write!(f, "muster roll I/O failed: {err}"),
            Self::Decode(err) => write!(f, "muster roll is unreadable: {err}"),
            Self::Parse(msg) => write!(f, "muster roll is unreadable: {msg}"),
        }
    }
}

impl core::error::Error for SnapshotError {
    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
        match self {
            Self::Encode(err) | Self::Decode(err) => Some(err),
            Self::Io(err) => Some(err),
            Self::NoParent(_) | Self::Parse(_) => None,
        }
    }
}

impl From<serde_json::Error> for SnapshotError {
    fn from(source: serde_json::Error) -> Self {
        Self::Encode(source)
    }
}

impl From<std::io::Error> for SnapshotError {
    fn from(source: std::io::Error) -> Self {
        Self::Io(source)
    }
}

/// Writes `snapshot` to `path` atomically: a temp file in the same directory,
/// since `rename(2)` is atomic only within one filesystem, `fsync`ed, renamed
/// over `path`, then on unix the directory `fsync`ed so the rename survives a
/// power cut (a no-op on Windows). The temp file is owner-only (unix mode
/// 0600) and `persist` keeps that mode: the roll stores app `env` verbatim,
/// the one place shep writes secrets to disk.
///
/// # Errors
/// - [`SnapshotError::NoParent`]: the roll path has no directory to write into.
/// - [`SnapshotError::Encode`]: the roll failed to serialize.
/// - [`SnapshotError::Io`]: the temp file, fsync, or rename failed.
pub(crate) fn write_atomic(path: &Path, snapshot: &FlockSnapshot) -> Result<(), SnapshotError> {
    use std::io::Write;

    let parent = path
        .parent()
        .ok_or_else(|| SnapshotError::NoParent(path.to_path_buf()))?;
    let json = serde_json::to_vec_pretty(snapshot)?;

    let mut tmp = NamedTempFile::new_in(parent)?;
    tmp.write_all(&json)?;
    shep_core::atomic_file::publish(tmp, path).map_err(SnapshotError::Io)
}

/// Reads and validates a muster roll written by `write_atomic`.
///
/// Public only for `tests/daemon_e2e.rs`, which asserts on the roll a live
/// daemon wrote; the daemon's own restore path calls it from inside `boot`.
///
/// # Errors
/// - [`SnapshotError::Io`]: the roll could not be read.
/// - [`SnapshotError::Decode`]: the roll is not valid JSON for a roll.
/// - [`SnapshotError::Parse`]: a schema version this daemon does not know.
pub fn read(path: &Path) -> Result<FlockSnapshot, SnapshotError> {
    let bytes = std::fs::read(path)?;
    // Named, not `?`: `From<serde_json::Error>` maps to `Encode`, the write path.
    let snapshot: FlockSnapshot = serde_json::from_slice(&bytes).map_err(SnapshotError::Decode)?;
    if snapshot.version != SNAPSHOT_VERSION {
        return Err(SnapshotError::Parse(format!(
            "roll schema version {} is not one this daemon knows (expected {SNAPSHOT_VERSION})",
            snapshot.version
        )));
    }
    Ok(snapshot)
}

/// What a `muster` should put back: every member, which of them to start, and
/// the ones that failed re-validation
#[derive(Debug)]
pub(crate) struct Restorable {
    /// Every entry that still normalizes, in roll order. The flock is a
    /// membership list, so all of these are registered whether or not they
    /// run.
    pub(crate) members: Vec<ResolvedApp>,
    /// The subset of [`Self::members`] that was up when the roll was written
    /// and still opts into `autostart`.
    pub(crate) to_start: Vec<ResolvedApp>,
    /// The names in [`Self::members`] the roll marks disabled. None of them is
    /// in [`Self::to_start`].
    pub(crate) disabled: BTreeSet<String>,
    /// Sheep name, and why its saved config failed to normalize
    pub(crate) rejected: Vec<(String, NormalizeError)>,
}

/// Splits a loaded [`FlockSnapshot`] into members, the subset to start, and
/// the entries rejected on re-validation.
///
/// Membership survives everything but `delete`: a sheep stopped when the roll
/// was written comes back registered and `Stopped`. What to START is a
/// separate question, answered by `instances_running > 0 && autostart`. A
/// disabled entry is never started, whatever its count says.
///
/// The roll is a file a human can edit, so every entry is run back through
/// [`normalize()`] like peer input, and a bad one is collected into `rejected`
/// rather than aborting the muster.
#[must_use]
pub(crate) fn restorable(snapshot: FlockSnapshot) -> Restorable {
    let mut members = Vec::new();
    let mut to_start = Vec::new();
    let mut disabled = BTreeSet::new();
    let mut rejected = Vec::new();
    for saved in snapshot.apps {
        let name = saved.app.name.clone();
        let was_up = saved.instances_running > 0;
        let autostart = saved.app.autostart;
        match normalize(saved.app) {
            Ok(resolved) => {
                if saved.disabled {
                    disabled.insert(name);
                } else if was_up && autostart {
                    to_start.push(resolved.clone());
                }
                members.push(resolved);
            }
            Err(err) => rejected.push((name, err)),
        }
    }
    Restorable {
        members,
        to_start,
        disabled,
        rejected,
    }
}

/// Reads the muster roll and starts every app it restores, returning the
/// names it restored.
///
/// The daemon's one restore path: `boot` runs it under `--restore`, the
/// `Muster` request runs it for an operator. An app the flock already has is
/// left where it stands, still counted as restored, which makes the verb
/// idempotent; starting it again is not a no-op, since
/// [`instance_slots`](crate::assemble::instance_slots) takes the lowest free
/// slot. A missing roll is not an error; an unparseable one is reported.
///
/// `dogs` names every dog this shepherd holds and `boot_first_dogs` the ones
/// `[daemon] boot_first_dogs` promotes ahead of the flock. Neither is spawned
/// here: they are the graph's dog nodes, and the split is what positions a
/// dog in the plan. Which of them [`warn_about_the_graph`] reports on is
/// decided by the live flock as well, since an unpromoted dog is already up
/// on every restore but the boot's own.
///
/// # Errors
/// - [`SnapshotError`]: the roll exists but could not be read or parsed.
pub(crate) async fn muster(
    path: &Path,
    registry: &FlockRegistry,
    supervisor: &SupervisorHandle,
    events: &Bus,
    dogs: &[String],
    boot_first_dogs: &[String],
) -> Result<Vec<String>, SnapshotError> {
    let saved = match read(path) {
        Ok(saved) => saved,
        Err(SnapshotError::Io(err)) if err.kind() == std::io::ErrorKind::NotFound => {
            return Ok(Vec::new());
        }
        Err(err) => return Err(err),
    };
    let restorable = restorable(saved);
    for (name, err) in &restorable.rejected {
        tracing::warn!(name, %err, "muster roll entry rejected on restore");
    }
    if restorable.members.is_empty() {
        return Ok(Vec::new());
    }
    let restored: Vec<String> = restorable
        .members
        .iter()
        .map(|app| app.config().name.clone())
        .collect();

    // A supervisor that cannot be listed has no flock left to collide with,
    // and the `start` below announces its own failure.
    let running = supervisor.list_checked().await.unwrap_or_default();
    let known = |app: &ResolvedApp| running.iter().any(|info| info.name == app.config().name);
    warn_about_dogs_holding_sheep_names(&restorable.members, &running);

    // Membership for every entry `start` will not bring up, so a sheep saved
    // while stopped comes back listed. The ones being started are excluded:
    // registering them at rest first would leave an idle `instance: 0` entry
    // for `start` to allocate around, and the sheep would show up twice.
    let starting: Vec<&str> = restorable
        .to_start
        .iter()
        .map(|app| app.config().name.as_str())
        .collect();
    let members: Vec<ResolvedApp> = restorable
        .members
        .iter()
        .filter(|app| !known(app) && !starting.contains(&app.config().name.as_str()))
        .cloned()
        .collect();
    if !members.is_empty() {
        registry.record(&members);
        if let Err(err) = supervisor
            .register_at_rest(members, restorable.disabled.clone())
            .await
        {
            tracing::warn!(%err, "muster roll restore could not register one or more apps");
        }
    }

    let to_start: Vec<ResolvedApp> = restorable
        .to_start
        .into_iter()
        .filter(|app| !known(app))
        .collect();
    if to_start.is_empty() {
        return Ok(restored);
    }
    // Recorded whether or not the stages fully succeed: already-registered
    // entries must persist even when a later spawn fails. Only what this call
    // starts, so an app left where it stands keeps the config it is actually
    // running under.
    registry.record(&to_start);

    let plan = crate::boot_order::plan_for(&to_start, dogs, boot_first_dogs);
    warn_about_the_graph(
        &plan,
        &to_start,
        &restorable.members,
        &running,
        dogs,
        boot_first_dogs,
    );
    warn_about_disabled_dependencies(&to_start, &restorable.disabled);
    // `BatchPolicy::PerApp`, not `AllOrNothing`: a whole-batch refusal over an
    // app whose script provably is not there is right for an operator typing
    // `shep start` and wrong at an unattended boot, where a binary missing
    // after a rebuild would cost the machine its entire flock.
    // `PerApp` never answers `Err`; warned rather than discarded so a later
    // change of policy here cannot lose a failure silently.
    if let Err(err) = crate::boot_order::start_in_stages(
        &plan,
        &to_start,
        supervisor,
        events,
        BatchPolicy::PerApp,
    )
    .await
    {
        tracing::warn!(%err, "muster roll restore could not start one or more apps");
    }
    Ok(restored)
}

/// Warns for every dependency of a sheep this restore starts that the roll
/// marks disabled.
///
/// Nothing refuses: a disabled dependency is simply not there, so the
/// dependent starts without it, as with `autostart = false`.
fn warn_about_disabled_dependencies(to_start: &[ResolvedApp], disabled: &BTreeSet<String>) {
    for app in to_start {
        for target in &app.config().depends_on {
            if disabled.contains(target) {
                tracing::warn!(
                    sheep = %app.config().name,
                    dependency = %target,
                    "the dependency is disabled; starting without it"
                );
            }
        }
    }
}

/// Warns for every saved sheep whose name a running dog has taken.
///
/// A boot that restores the roll leaves out any dog a saved sheep names, so
/// this fires on an operator's `Muster` into a flock whose dog took the name
/// since: [`muster`]'s `known` finds the dog running and filters the roll's
/// sheep of that name out of both the membership pass and the start. Without
/// this line that sheep is lost in silence. A handover successor neither
/// filters nor restores, so it never reaches this.
///
/// The name stays in the restored list. It really is in the flock, and
/// dropping it would report the loss as an absence on a terminal that has no
/// room to say why; the reply carries no per-app note to put the reason in.
fn warn_about_dogs_holding_sheep_names(members: &[ResolvedApp], running: &[ProcessInfo]) {
    for app in members {
        let name = app.config().name.as_str();
        let Some(source) = running
            .iter()
            .find(|info| info.name == name)
            .and_then(|info| info.dog.as_ref())
        else {
            continue;
        };
        tracing::warn!(
            name,
            dog = ?source,
            "a dog holds this name, so the roll's sheep of that name is not restored"
        );
    }
}

/// Whether `running` holds an entry of this name that has been spawned and
/// not given up on.
///
/// `Stopped` and `Errored` are the two that answer `false`: a dog registered
/// under either has no process behind it, so a sheep that waits for it really
/// does start without it. Every other status covers a live child, a
/// `WaitingRestart` gap between two of them included.
fn is_up(running: &[ProcessInfo], name: &str) -> bool {
    running.iter().any(|info| {
        info.name == name && !matches!(info.status, ProcStatus::Stopped | ProcStatus::Errored)
    })
}

/// Reports every way the roll's dependency graph is not what it says it is.
///
/// Nothing here refuses. A restore runs on a machine nobody is watching, so
/// each of these brings the flock up and says what it did instead: a
/// dependency nothing in the roll answers to, one that opted out of
/// `autostart`, one that is a dog starting after the flock rather than
/// before it, and a cycle.
///
/// `running` is the flock as it stands, which is what keeps the dog warning
/// from firing on a dog that is already up: this runs at an operator's
/// `shep muster` as well as at boot.
fn warn_about_the_graph(
    plan: &BootPlan,
    to_start: &[ResolvedApp],
    members: &[ResolvedApp],
    running: &[ProcessInfo],
    dogs: &[String],
    boot_first_dogs: &[String],
) {
    // A member the roll holds but this restore will not start. Named
    // separately from the unresolved edges below, since "the sheep exists and
    // opted out" is a different thing for an operator to do about than "the
    // name is a typo".
    let opted_out: BTreeSet<&str> = members
        .iter()
        .filter(|app| !app.config().autostart)
        .map(|app| app.config().name.as_str())
        .collect();
    // Every name the roll can put back, whether or not this restore starts
    // it. The plan is built from `to_start` alone, so a dependency already
    // running, saved stopped, or opted out is absent from the graph and reads
    // as unresolved. On `shep muster` against a live flock that is the whole
    // flock, and the warning below would fire on the commonest path there is.
    let restorable: BTreeSet<&str> = members
        .iter()
        .map(|app| app.config().name.as_str())
        .collect();

    for unresolved in &plan.unresolved {
        // `opted_out` is a subset of this, and has its own warning below.
        if restorable.contains(unresolved.missing.as_str()) {
            continue;
        }
        tracing::warn!(
            sheep = %unresolved.dependent,
            missing = %unresolved.missing,
            "a dependency names nothing this flock has; starting without it"
        );
    }
    for app in to_start {
        for target in &app.config().depends_on {
            if opted_out.contains(target.as_str()) {
                tracing::warn!(
                    sheep = %app.config().name,
                    dependency = %target,
                    "the dependency sets autostart = false; starting without it"
                );
            }
        }
    }
    // A dog gets an ordinary graph position, but `boot` spawns dogs in two
    // groups and neither is at a stage boundary: the promoted ones before
    // this restore, the rest after every stage. So a dependency on an
    // unpromoted dog is ordered by the plan and by nothing else, and the
    // sheep starts while the dog is not running.
    let unpromoted: BTreeSet<&str> = dogs
        .iter()
        .map(String::as_str)
        .filter(|dog| !boot_first_dogs.iter().any(|first| first == dog))
        // A dog that is already up is not one this restore starts after the
        // flock, whatever the promotion list says. `muster` is the handler for
        // an operator's `Request::Muster` as well as the boot's restore, and
        // there both dog groups have been running for hours, so reading the
        // list alone would warn about every such edge on every `shep muster`.
        // The live flock, read the way `warn_about_dogs_holding_sheep_names`
        // reads it, is what tells the two apart.
        .filter(|dog| !is_up(running, dog))
        // A dog whose name a started sheep already holds is not a node at
        // all; the sheep is, and it is ordered properly. `plan_for` drops it
        // against exactly this list.
        .filter(|dog| !to_start.iter().any(|app| app.config().name == *dog))
        .collect();
    for app in to_start {
        for target in &app.config().depends_on {
            if unpromoted.contains(target.as_str()) {
                tracing::warn!(
                    sheep = %app.config().name,
                    dog = %target,
                    "the dependency is a dog outside [daemon] boot_first_dogs, \
                     which starts after the whole flock; starting without it"
                );
            }
        }
    }
    for cycle in &plan.cycles {
        tracing::warn!(
            cycle = %render_cycle(cycle),
            "a dependency cycle; those sheep start last, in no particular order"
        );
    }
    // `BootPlan::cycles` carries ONE representative path per knot, so a sheep
    // inside the knot but off that path is just as stuck and is never named by
    // the warning above. The cyclic stage is every one of them.
    //
    // Silent when the paths already named everybody, which is the plain
    // two-node cycle: `plan` puts every knot in one stage, so this line is
    // flock-wide and would otherwise repeat the line above verbatim.
    // `BootPlan::knots` carries the membership per knot; this line wants the
    // flock-wide set, which the cyclic stage already is.
    if let Some(stuck) = cyclic_stage(plan) {
        let named: BTreeSet<&str> = plan.cycles.iter().flatten().map(String::as_str).collect();
        if stuck.iter().any(|name| !named.contains(name.as_str())) {
            let names: Vec<&str> = stuck.iter().map(String::as_str).collect();
            tracing::warn!(
                stuck = ?names,
                "every sheep a dependency cycle holds; each of them starts last"
            );
        }
    }
}

/// The stage the plan lifted every cyclic node into, if the graph has a cycle.
///
/// [`BootPlan`] does not label that stage, so it is found by the property that
/// identifies it: a name the plan reports in a cycle appears in no other one.
fn cyclic_stage(plan: &BootPlan) -> Option<&Vec<String>> {
    let member = plan.cycles.first()?.first()?;
    plan.stages.iter().find(|stage| stage.contains(member))
}

/// True for lifecycle transitions the roll cares about; false for log
/// traffic and daemon-wide notices, which must not trigger a rewrite.
fn is_state_change(event: &BusEvent) -> bool {
    matches!(event, BusEvent::Process { .. })
}

/// Handle to the debounced writer task
#[derive(Debug)]
pub(crate) struct SnapshotWriter {
    handle: JoinHandle<()>,
    /// Read only by [`Self::writes`], which carries the same `allow`.
    #[allow(dead_code, reason = "read by this crate's own tests through `writes`")]
    writes: Arc<AtomicU64>,
}

impl SnapshotWriter {
    /// Completed roll writes since boot
    ///
    /// The metrics dog reads this off the wire, so the only callers are this
    /// module's tests and it is dead in a non-test build.
    // A trivial atomic load, not per-frame hot: #[inline], never #[inline(always)].
    #[inline]
    #[must_use]
    #[allow(dead_code, reason = "called by this module's own tests")]
    pub(crate) fn writes(&self) -> u64 {
        self.writes.load(Ordering::SeqCst)
    }

    /// Stops the writer and waits for it (the caller then owns roll timing)
    pub(crate) async fn stop(self) {
        self.handle.abort();
        let _ = self.handle.await;
    }
}

/// Spawns the debounced muster-roll writer.
///
/// Coalesces bursts of lifecycle events (spec ยง13.4: one restart storm, one
/// write) into a single [`write_atomic`] call per [`SNAPSHOT_DEBOUNCE_MS`]
/// window. Log traffic never resets or starts the debounce timer.
///
/// Woken by [`FlockRegistry`]'s own writes as well as by the bus, so a config
/// change that parks a field reaches the file without a process having moved.
pub(crate) fn spawn_snapshot_writer(
    path: PathBuf,
    supervisor: SupervisorHandle,
    registry: FlockRegistry,
    events: broadcast::Receiver<SharedEvent>,
) -> SnapshotWriter {
    let writes = Arc::new(AtomicU64::new(0));
    let task_writes = Arc::clone(&writes);
    let dirty = Arc::clone(&registry.dirty);
    let handle = tokio::spawn(run_writer(
        path,
        supervisor,
        registry,
        events,
        dirty,
        task_writes,
    ));
    SnapshotWriter { handle, writes }
}

/// The writer's actor loop. Cancel-safe: the debounce deadline is recomputed
/// from the stored `Option<Instant>` every iteration, so losing the `select!`
/// race never extends the window.
async fn run_writer(
    path: PathBuf,
    supervisor: SupervisorHandle,
    registry: FlockRegistry,
    mut events: broadcast::Receiver<SharedEvent>,
    dirty: Arc<Notify>,
    writes: Arc<AtomicU64>,
) {
    let mut deadline: Option<Instant> = None;
    loop {
        tokio::select! {
            received = events.recv() => match received {
                // Only lifecycle events change the roll; log lines must not
                // rewrite a file once per output line.
                Ok(event) => if is_state_change(&event) && deadline.is_none() {
                    deadline = Some(Instant::now() + Duration::from_millis(SNAPSHOT_DEBOUNCE_MS));
                },
                // A lag may have swallowed a lifecycle event: assume dirty.
                Err(RecvError::Lagged(_)) => if deadline.is_none() {
                    deadline = Some(Instant::now() + Duration::from_millis(SNAPSHOT_DEBOUNCE_MS));
                },
                Err(RecvError::Closed) => break,
            },
            // A config write that parks a field moves no process, so the bus
            // says nothing and only this arm reaches the file before the next
            // unrelated lifecycle event or a graceful shutdown.
            () = dirty.notified() => if deadline.is_none() {
                deadline = Some(Instant::now() + Duration::from_millis(SNAPSHOT_DEBOUNCE_MS));
            },
            () = sleep_until(deadline.unwrap_or_else(Instant::now)), if deadline.is_some() => {
                deadline = None;
                write_now(&path, &supervisor, &registry, &writes).await;
            }
        }
    }
}

// The write is a few KiB to a local file once per debounce window;
// `spawn_blocking` would buy a task hop and nothing else.
async fn write_now(
    path: &Path,
    supervisor: &SupervisorHandle,
    registry: &FlockRegistry,
    writes: &AtomicU64,
) {
    // Engine gone: there is nothing left to record and the shutdown path has
    // already written the final roll.
    let Ok(infos) = supervisor.list_checked().await else {
        return;
    };
    let roll = registry.roll(&infos, crate::now_ms()); // lock released before any IO
    match write_atomic(path, &roll) {
        Ok(()) => {
            writes.fetch_add(1, Ordering::SeqCst);
        }
        Err(err) => tracing::warn!(%err, "muster roll write failed"),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use shep_core::config::AppConfig;
    use shep_core::protocol::ProcessInfo;
    use shep_core::status::ProcStatus;

    fn info(id: u32, name: &str, status: ProcStatus) -> ProcessInfo {
        ProcessInfo::builder(id, name, status)
            .pid(Some(1000 + id))
            .out_file(Some(format!("/logs/{name}-0-out.log")))
            .err_file(Some(format!("/logs/{name}-0-err.log")))
            .build()
    }

    mod disabled;
    mod graph;
    mod io;
    mod muster;
    mod roll;
    mod secrets;
    mod writer;

    /// So a later `restart` has something to find.
    #[test]
    fn a_sheep_saved_while_stopped_is_still_a_member() {
        let roll = FlockSnapshot::with_apps(vec![SavedApp {
            app: AppConfig::minimal("api-auth", "./api-auth"),
            instances_running: 0,
            disabled: false,
        }]);
        let restorable = restorable(roll);
        assert_eq!(restorable.members.len(), 1, "stopping is not forgetting");
        assert_eq!(restorable.members[0].config().name, "api-auth");
        assert!(
            restorable.to_start.is_empty(),
            "but it stays stopped: it was not running when the roll was written"
        );
    }
}