layover-core 0.23.1

Domain types for Layover: factory configuration, route graph, itinerary accounting and rendezvous barriers.
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
//! Pipelines: named, triggerable entry points that carry flags.
//!
//! A route map says which agents *may* talk to each other. A pipeline says how work *enters* the
//! mesh: which agent receives it, whether a human starts it or a clock does, and which boolean
//! flags the run is parameterised by.
//!
//! Pipelines are deliberately thin. They do not describe a sequence of steps — agents still decide
//! where work goes next — so adding one does not turn the permission mesh into a pipeline engine.
//!
//! # Safety
//!
//! A schedule is the one part of Layover that starts work with no human present, so the floor on
//! how often it may fire is enforced here rather than left to the author. See
//! [`Schedule::MIN_INTERVAL_SECS`].

use std::collections::BTreeMap;
use std::fmt;
use std::str::FromStr;
use std::time::Duration;

use jiff::Timestamp;
use serde::{Deserialize, Serialize};

use crate::agent::AgentName;

/// The name of a pipeline, as written in `layover.toml`.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize)]
#[serde(transparent)]
pub struct PipelineName(String);

impl PipelineName {
    /// Creates a pipeline name.
    #[must_use]
    pub fn new(name: impl Into<String>) -> Self {
        Self(name.into())
    }

    /// Returns the name as a string slice.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

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

impl From<&str> for PipelineName {
    fn from(value: &str) -> Self {
        Self(value.to_owned())
    }
}

/// When a scheduled pipeline fires.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Schedule {
    /// A fixed interval between runs.
    Every(Duration),
    /// A five-field cron expression, evaluated in the Tower's local time zone.
    Cron(String),
}

impl Schedule {
    /// The shortest interval a schedule may declare.
    ///
    /// Every firing is a real, paid CLI invocation, and a schedule runs with nobody watching. A
    /// minute is the floor because it is the finest granularity a five-field cron expression can
    /// express, so allowing anything shorter would make `every` and `cron` disagree about what is
    /// possible.
    pub const MIN_INTERVAL_SECS: u64 = 60;

    /// Returns the fixed interval, if this schedule is one.
    #[must_use]
    pub fn interval(&self) -> Option<Duration> {
        match self {
            Self::Every(duration) => Some(*duration),
            Self::Cron(_) => None,
        }
    }

    /// The first firing strictly after `now`.
    ///
    /// Both forms are computed from the clock rather than from when the last run finished. Adding
    /// an interval to a finish time makes the period drift by however long the work took, so an
    /// hourly job slowly becomes a ninety-minute one.
    ///
    /// Returns `None` only for a cron expression that never matches — 31 February, say — which
    /// parses cleanly and is therefore accepted at load. A pipeline that can never fire is better
    /// left silent than made to fire at some arbitrary substitute time.
    #[must_use]
    pub fn next_after(&self, now: Timestamp) -> Option<Timestamp> {
        match self {
            Self::Every(interval) => {
                let step = i64::try_from(interval.as_secs()).ok()?.max(1);
                now.checked_add(jiff::SignedDuration::from_secs(step)).ok()
            }
            Self::Cron(expression) => {
                let cron = croner::Cron::from_str(expression).ok()?;
                // Local time, because somebody writing `0 8 * * *` means eight in the morning
                // where they are.
                let zoned = now.to_zoned(jiff::tz::TimeZone::system());

                cron.find_next_occurrence(&zoned, false)
                    .ok()
                    .map(|at| at.timestamp())
            }
        }
    }

    /// Returns a lower bound on the gap between firings, in seconds, when one can be established.
    ///
    /// For a fixed interval this is exact. For a cron expression it is read off the minute field,
    /// and only when that field states the answer unambiguously: `*` fires every minute and
    /// `*/n` every `n` minutes. Lists and ranges are left alone rather than guessed at, because a
    /// wrong lower bound here means a warning that is not true, and a validator that cries wolf is
    /// one people stop reading.
    #[must_use]
    pub fn min_gap_secs(&self) -> Option<u64> {
        match self {
            Self::Every(duration) => Some(duration.as_secs()),
            Self::Cron(expression) => {
                let minutes = expression.split_whitespace().next()?;
                if minutes == "*" {
                    return Some(60);
                }
                let step: u64 = minutes.strip_prefix("*/")?.parse().ok()?;
                step.checked_mul(60)
            }
        }
    }
}

impl fmt::Display for Schedule {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Every(duration) => write!(f, "every {}s", duration.as_secs()),
            Self::Cron(expression) => write!(f, "cron `{expression}`"),
        }
    }
}

/// What starts a pipeline.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Trigger {
    /// A human sends the first flight.
    Manual,
    /// The Tower sends the first flight on a clock.
    Scheduled(Schedule),
}

impl fmt::Display for Trigger {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Manual => f.write_str("manual"),
            Self::Scheduled(schedule) => write!(f, "{schedule}"),
        }
    }
}

impl Trigger {
    /// Returns the schedule, if this trigger has one.
    #[must_use]
    pub fn schedule(&self) -> Option<&Schedule> {
        match self {
            Self::Manual => None,
            Self::Scheduled(schedule) => Some(schedule),
        }
    }

    /// Returns `true` when only a human can start this pipeline.
    #[must_use]
    pub fn is_manual(&self) -> bool {
        matches!(self, Self::Manual)
    }
}

/// A boolean parameter a pipeline accepts at trigger time.
///
/// Flags are how one factory definition serves several situations without duplicating prompts:
/// a tester that also runs a remote end-to-end suite is the same agent with one extra paragraph
/// of instructions. See [`crate::prompt`].
#[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct FlagSpec {
    /// Value used when the trigger does not set the flag.
    #[serde(default)]
    pub default: bool,
    /// What turning this flag on actually does.
    #[serde(default)]
    pub description: Option<String>,
}

/// How an itinerary's workspace relates to other itineraries'.
///
/// A pipeline that can have several instances in flight — one per pull request, say — needs each
/// to work somewhere of its own, or two `read-write` agents in two unrelated itineraries will
/// clobber each other in the shared directory.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum Workspace {
    /// Every itinerary works in the one shared `work_dir`.
    ///
    /// The default, because it is what a single-instance pipeline wants and because a worktree
    /// per itinerary costs disk and setup time.
    #[default]
    Shared,
    /// Each itinerary gets its own git worktree, named after the itinerary.
    ///
    /// This is what makes several instances of one pipeline safe to run at once.
    PerItinerary,
}

impl Workspace {
    /// Returns `true` when each itinerary is isolated from the others.
    #[must_use]
    pub fn is_isolated(&self) -> bool {
        matches!(self, Self::PerItinerary)
    }
}

/// What happens when a scheduled pipeline is due and its previous wave has not finished.
///
/// The default is to skip. Starting a second copy means paying twice for one result and, where
/// agents share a workspace, two of them writing to the same files; skipping means being one
/// interval late. For unattended spending those are not comparable.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum Overlap {
    /// Miss this firing and wait for the next one.
    #[default]
    Skip,
    /// Start another instance anyway.
    ///
    /// Safe when instances cannot interfere — a `per-itinerary` workspace, or agents that only
    /// read — and a way to pay twice when they can.
    Allow,
}

impl Overlap {
    /// Returns `true` when a second instance may start.
    #[must_use]
    pub fn allows_second_instance(&self) -> bool {
        matches!(self, Self::Allow)
    }
}

/// A named entry point into the mesh.
#[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Pipeline {
    /// One line saying what this pipeline is for.
    #[serde(default)]
    pub description: Option<String>,
    /// The agent that receives the first flight.
    pub entry: AgentName,
    /// What starts it.
    #[serde(default = "manual_trigger")]
    pub trigger: Trigger,
    /// Whether instances of this pipeline share a workspace or get one each.
    #[serde(default)]
    pub workspace: Workspace,
    /// What to do when this pipeline is due again before the last wave has finished.
    #[serde(default)]
    pub overlap: Overlap,
    /// Whether this pipeline picks up booked layovers rather than starting fresh work.
    ///
    /// A resuming pipeline does not open an itinerary on every tick. It looks for work that was
    /// set down and is now due, and opens one seeded with what the earlier chain knew. A tick
    /// that finds nothing due costs nothing, which is what makes checking every twenty minutes
    /// affordable.
    #[serde(default)]
    pub resumes: bool,
    /// Boolean parameters this pipeline accepts, keyed by flag name.
    #[serde(default)]
    pub flags: BTreeMap<String, FlagSpec>,
}

impl Pipeline {
    /// Returns `true` when a second instance may start while the first is still going.
    #[must_use]
    pub fn allows_overlap(&self) -> bool {
        self.overlap.allows_second_instance()
    }

    /// Resolves the flag values for one run, filling in declared defaults.
    ///
    /// # Errors
    ///
    /// Returns [`FlagError::Undeclared`] if `overrides` names a flag this pipeline does not
    /// declare. Silently ignoring it would let a typo at trigger time change nothing while
    /// appearing to work.
    pub fn flags_for_run(&self, overrides: &BTreeMap<String, bool>) -> Result<Flags, FlagError> {
        if let Some(unknown) = overrides.keys().find(|key| !self.flags.contains_key(*key)) {
            return Err(FlagError::Undeclared {
                flag: unknown.clone(),
            });
        }

        let values = self
            .flags
            .iter()
            .map(|(name, spec)| {
                let value = overrides.get(name).copied().unwrap_or(spec.default);
                (name.clone(), value)
            })
            .collect();

        Ok(Flags(values))
    }
}

/// The resolved boolean parameters of a single run.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Flags(BTreeMap<String, bool>);

impl Flags {
    /// Builds a flag set directly, for tests and for triggers that declare no pipeline.
    #[must_use]
    pub fn new(values: BTreeMap<String, bool>) -> Self {
        Self(values)
    }

    /// Returns the value of `name`, or `None` when the flag was never declared.
    ///
    /// An undeclared flag is deliberately distinct from one that is declared and false: a prompt
    /// referring to a flag nobody declared is a mistake, not a false condition.
    #[must_use]
    pub fn get(&self, name: &str) -> Option<bool> {
        self.0.get(name).copied()
    }

    /// Returns every declared flag and its value.
    pub fn iter(&self) -> impl Iterator<Item = (&str, bool)> {
        self.0.iter().map(|(name, value)| (name.as_str(), *value))
    }

    /// Returns `true` when no flags are declared.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }
}

/// Why a flag could not be resolved.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum FlagError {
    /// The trigger set a flag the pipeline does not declare.
    #[error("flag `{flag}` is not declared by this pipeline")]
    Undeclared {
        /// The offending flag name.
        flag: String,
    },
}

/// Why a trigger could not be understood.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum TriggerError {
    /// The trigger keyword was not one Layover knows.
    #[error(
        "unknown trigger `{found}`; expected `manual`, `{{ every = \"1h\" }}` or `{{ cron = \"0 * * * *\" }}`"
    )]
    UnknownKeyword {
        /// What was written.
        found: String,
    },
    /// An `every` value was not a duration.
    #[error("could not read `{found}` as a duration; expected a number followed by s, m, h or d")]
    BadDuration {
        /// What was written.
        found: String,
    },
    /// An `every` value was shorter than the floor.
    #[error(
        "`every = \"{found}\"` fires more often than once every {minimum}s, which is not allowed \
         for unattended work"
    )]
    TooFrequent {
        /// What was written.
        found: String,
        /// The floor that was breached.
        minimum: u64,
    },
    /// A cron expression did not parse.
    #[error("could not read `{found}` as a cron expression: {reason}")]
    BadCron {
        /// What was written.
        found: String,
        /// Why the parser rejected it.
        reason: String,
    },
    /// A cron expression carried a seconds field.
    #[error(
        "cron expression `{found}` has {fields} fields; Layover accepts five-field expressions \
         only, because a seconds field can schedule work faster than a run can finish"
    )]
    SubMinuteCron {
        /// What was written.
        found: String,
        /// How many fields it had.
        fields: usize,
    },
    /// Both `every` and `cron` were given.
    #[error("a trigger sets both `every` and `cron`; give exactly one")]
    AmbiguousSchedule,
    /// A trigger table was empty.
    #[error("a trigger table sets neither `every` nor `cron`")]
    EmptySchedule,
}

const fn manual_trigger() -> Trigger {
    Trigger::Manual
}

/// Parses a duration such as `30s`, `15m`, `1h` or `2d`.
fn parse_duration(text: &str) -> Result<Duration, TriggerError> {
    let trimmed = text.trim();
    let bad = || TriggerError::BadDuration {
        found: text.to_owned(),
    };

    // Split on the last *character* rather than the last byte, so a stray multi-byte character
    // produces an error instead of a panic.
    let (digits, unit) = match trimmed.char_indices().next_back() {
        Some((index, unit)) => (&trimmed[..index], unit),
        None => return Err(bad()),
    };

    let multiplier = match unit {
        's' => 1_u64,
        'm' => 60,
        'h' => 60 * 60,
        'd' => 24 * 60 * 60,
        _ => return Err(bad()),
    };

    let amount: u64 = digits.parse().map_err(|_| bad())?;
    amount
        .checked_mul(multiplier)
        .map(Duration::from_secs)
        .ok_or_else(bad)
}

/// Validates a cron expression, rejecting anything finer than a minute.
fn parse_cron(expression: &str) -> Result<String, TriggerError> {
    let fields = expression.split_whitespace().count();
    if fields > 5 {
        return Err(TriggerError::SubMinuteCron {
            found: expression.to_owned(),
            fields,
        });
    }

    croner::Cron::from_str(expression).map_err(|error| TriggerError::BadCron {
        found: expression.to_owned(),
        reason: error.to_string(),
    })?;

    Ok(expression.to_owned())
}

/// The table form of a trigger, once its fields are known.
fn trigger_from_parts(
    every: Option<String>,
    cron: Option<String>,
) -> Result<Trigger, TriggerError> {
    match (every, cron) {
        (Some(every), None) => {
            let duration = parse_duration(&every)?;
            if duration.as_secs() < Schedule::MIN_INTERVAL_SECS {
                return Err(TriggerError::TooFrequent {
                    found: every,
                    minimum: Schedule::MIN_INTERVAL_SECS,
                });
            }
            Ok(Trigger::Scheduled(Schedule::Every(duration)))
        }
        (None, Some(cron)) => Ok(Trigger::Scheduled(Schedule::Cron(parse_cron(&cron)?))),
        (Some(_), Some(_)) => Err(TriggerError::AmbiguousSchedule),
        (None, None) => Err(TriggerError::EmptySchedule),
    }
}

impl<'de> Deserialize<'de> for Trigger {
    /// Accepts `"manual"` or a table with exactly one of `every` and `cron`.
    ///
    /// Hand-written rather than an untagged enum: untagged loses the inner error and reports only
    /// "data did not match any variant", which turns a one-character typo into a puzzle. A
    /// factory definition should say what is wrong with it while a human is still watching.
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        struct TriggerVisitor;

        impl<'de> serde::de::Visitor<'de> for TriggerVisitor {
            type Value = Trigger;

            fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                f.write_str(r#""manual", { every = "1h" } or { cron = "0 * * * *" }"#)
            }

            fn visit_str<E>(self, value: &str) -> Result<Trigger, E>
            where
                E: serde::de::Error,
            {
                if value == "manual" {
                    return Ok(Trigger::Manual);
                }
                Err(E::custom(TriggerError::UnknownKeyword {
                    found: value.to_owned(),
                }))
            }

            fn visit_map<M>(self, mut map: M) -> Result<Trigger, M::Error>
            where
                M: serde::de::MapAccess<'de>,
            {
                const FIELDS: &[&str] = &["every", "cron"];

                let mut every: Option<String> = None;
                let mut cron: Option<String> = None;

                while let Some(key) = map.next_key::<String>()? {
                    match key.as_str() {
                        "every" if every.is_some() => {
                            return Err(serde::de::Error::duplicate_field("every"));
                        }
                        "every" => every = Some(map.next_value()?),
                        "cron" if cron.is_some() => {
                            return Err(serde::de::Error::duplicate_field("cron"));
                        }
                        "cron" => cron = Some(map.next_value()?),
                        other => return Err(serde::de::Error::unknown_field(other, FIELDS)),
                    }
                }

                trigger_from_parts(every, cron).map_err(serde::de::Error::custom)
            }
        }

        deserializer.deserialize_any(TriggerVisitor)
    }
}

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

    fn pipeline(body: &str) -> Pipeline {
        toml::from_str(body).expect("pipeline parses")
    }

    fn trigger_error(body: &str) -> String {
        toml::from_str::<Pipeline>(body)
            .expect_err("trigger must be rejected")
            .to_string()
    }

    #[test]
    fn a_pipeline_defaults_to_manual() {
        let pipeline = pipeline(r#"entry = "analyst""#);

        assert_eq!(pipeline.trigger, Trigger::Manual);
        assert!(pipeline.trigger.is_manual());
        assert!(pipeline.trigger.schedule().is_none());
    }

    #[test]
    fn manual_may_be_stated_explicitly() {
        let pipeline = pipeline(
            r#"
            entry = "analyst"
            trigger = "manual"
            "#,
        );

        assert_eq!(pipeline.trigger, Trigger::Manual);
    }

    #[test]
    fn an_interval_schedule_is_parsed() {
        let pipeline = pipeline(
            r#"
            entry = "pr_scanner"
            trigger = { every = "1h" }
            "#,
        );

        assert_eq!(
            pipeline.trigger,
            Trigger::Scheduled(Schedule::Every(Duration::from_secs(3_600)))
        );
        assert_eq!(
            pipeline.trigger.schedule().and_then(Schedule::interval),
            Some(Duration::from_secs(3_600))
        );
    }

    #[test]
    fn every_unit_is_understood() {
        assert_eq!(parse_duration("90s"), Ok(Duration::from_secs(90)));
        assert_eq!(parse_duration("15m"), Ok(Duration::from_mins(15)));
        assert_eq!(parse_duration("2h"), Ok(Duration::from_secs(7_200)));
        assert_eq!(parse_duration("1d"), Ok(Duration::from_hours(24)));
    }

    #[test]
    fn a_malformed_duration_is_rejected() {
        assert!(parse_duration("soon").is_err());
        assert!(parse_duration("10").is_err());
        assert!(parse_duration("").is_err());
        assert!(parse_duration("1w").is_err());
        assert!(parse_duration("-5m").is_err());
        assert!(
            parse_duration("").is_err(),
            "must not panic on a multi-byte tail"
        );
        assert!(parse_duration("99999999999999999999d").is_err());
    }

    #[test]
    fn a_trigger_setting_both_forms_is_refused() {
        let message = trigger_error(
            r#"
            entry = "pr_scanner"
            trigger = { every = "1h", cron = "0 * * * *" }
            "#,
        );

        assert!(
            message.contains("both"),
            "a trigger must not silently pick one of two schedules, got: {message}"
        );
    }

    #[test]
    fn an_empty_trigger_table_is_refused() {
        let message = trigger_error(
            r#"
            entry = "pr_scanner"
            trigger = {}
            "#,
        );

        assert!(message.contains("neither"), "got: {message}");
    }

    #[test]
    fn an_unknown_trigger_field_is_refused_by_name() {
        // A one-character typo must say which character. An untagged enum would report only
        // "data did not match any variant", which is why this is deserialised by hand.
        let message = trigger_error(
            r#"
            entry = "pr_scanner"
            trigger = { evry = "1h" }
            "#,
        );

        assert!(
            message.contains("evry"),
            "the error must name the offending field, got: {message}"
        );
        assert!(
            message.contains("every") && message.contains("cron"),
            "the error must list what was expected, got: {message}"
        );
    }

    #[test]
    fn a_duplicate_trigger_field_is_refused() {
        let message = trigger_error(
            r#"
            entry = "pr_scanner"
            trigger = { every = "1h", every = "2h" }
            "#,
        );

        assert!(!message.is_empty(), "got: {message}");
    }

    #[test]
    fn a_schedule_faster_than_the_floor_is_refused() {
        let message = trigger_error(
            r#"
            entry = "pr_scanner"
            trigger = { every = "30s" }
            "#,
        );

        assert!(
            message.contains("fires more often"),
            "expected a frequency refusal, got: {message}"
        );
    }

    #[test]
    fn a_cron_schedule_is_validated_at_load_time() {
        let pipeline = pipeline(
            r#"
            entry = "pr_scanner"
            trigger = { cron = "0 * * * *" }
            "#,
        );

        assert_eq!(
            pipeline.trigger,
            Trigger::Scheduled(Schedule::Cron("0 * * * *".to_owned()))
        );
    }

    #[test]
    fn a_nonsense_cron_expression_is_refused() {
        let message = trigger_error(
            r#"
            entry = "pr_scanner"
            trigger = { cron = "every hour please" }
            "#,
        );

        assert!(
            message.contains("cron expression"),
            "expected a cron refusal, got: {message}"
        );
    }

    #[test]
    fn a_six_field_cron_expression_is_refused() {
        // A seconds field can schedule work faster than a run can finish, which is a fork bomb
        // with a clock attached.
        let message = trigger_error(
            r#"
            entry = "pr_scanner"
            trigger = { cron = "*/5 * * * * *" }
            "#,
        );

        assert!(
            message.contains("five-field"),
            "expected a granularity refusal, got: {message}"
        );
    }

    #[test]
    fn an_unknown_trigger_keyword_is_refused() {
        let message = trigger_error(
            r#"
            entry = "pr_scanner"
            trigger = "whenever"
            "#,
        );

        assert!(message.contains("unknown trigger"));
    }

    #[test]
    fn flags_fall_back_to_their_declared_defaults() {
        let pipeline = pipeline(
            r#"
            entry = "analyst"

            [flags]
            run_e2e = { default = false, description = "Run the remote suite" }
            verbose = { default = true }
            "#,
        );

        let flags = pipeline
            .flags_for_run(&BTreeMap::new())
            .expect("no overrides is always valid");

        assert_eq!(flags.get("run_e2e"), Some(false));
        assert_eq!(flags.get("verbose"), Some(true));
        assert_eq!(flags.get("undeclared"), None);
        assert!(!flags.is_empty());
    }

    #[test]
    fn a_trigger_override_wins_over_the_default() {
        let pipeline = pipeline(
            r#"
            entry = "analyst"

            [flags]
            run_e2e = { default = false }
            "#,
        );

        let overrides = BTreeMap::from([("run_e2e".to_owned(), true)]);
        let flags = pipeline.flags_for_run(&overrides).expect("declared flag");

        assert_eq!(flags.get("run_e2e"), Some(true));
    }

    #[test]
    fn setting_an_undeclared_flag_is_an_error() {
        // A typo at trigger time would otherwise change nothing while appearing to work.
        let pipeline = pipeline(r#"entry = "analyst""#);
        let overrides = BTreeMap::from([("run_e2ee".to_owned(), true)]);

        assert_eq!(
            pipeline.flags_for_run(&overrides),
            Err(FlagError::Undeclared {
                flag: "run_e2ee".to_owned()
            })
        );
    }

    #[test]
    fn flags_iterate_in_a_stable_order() {
        let flags = Flags::new(BTreeMap::from([
            ("b".to_owned(), true),
            ("a".to_owned(), false),
        ]));

        assert_eq!(
            flags.iter().collect::<Vec<_>>(),
            [("a", false), ("b", true)]
        );
    }
}