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
//! Checks about pipelines: their entry agents, their schedules and their flags.

use std::collections::BTreeMap;

use crate::config::Config;

use super::Diagnostic;

pub(super) fn check_pipelines(config: &Config, found: &mut Vec<Diagnostic>) {
    check_entries_exist(config, found);
    check_flag_names_are_usable(config, found);
    check_flag_declarations_agree(config, found);
    check_schedules_are_survivable(config, found);
    check_schedules_fit_the_reserve(config, found);
}

/// Warns when the Reserve cannot fund the scheduled work at all.
///
/// Per-itinerary Fuel cannot see this: each tick of a scheduled pipeline is a *new* itinerary with
/// a *full* Fuel budget, so every chain stays inside its rail while the total runs away. An hourly
/// pipeline at `fuel_usd = 20` permits `24 × 20 = $480` a day.
///
/// Deliberately **not** a warning when the Reserve is merely lower than that worst case — being
/// lower is the entire reason to have a cap, and hitting it is a graceful pause rather than a
/// failure. What is reported is the unambiguously broken shape: a Reserve too small to fund even
/// one run of the pipeline, which can therefore never complete.
fn check_schedules_fit_the_reserve(config: &Config, found: &mut Vec<Diagnostic>) {
    let scheduled: Vec<_> = config.scheduled_pipelines().collect();
    if scheduled.is_empty() {
        return;
    }

    if config.reserve.is_unlimited() {
        found.push(Diagnostic::warning(
            "this factory has a scheduled pipeline but no `[reserve] fuel_usd`; each tick mints a \
             fresh itinerary with a fresh Fuel budget, so nothing bounds total spend",
        ));
        return;
    }

    for (name, pipeline) in scheduled {
        let per_itinerary = config.fuel_for(&pipeline.entry);
        if per_itinerary > config.reserve.fuel_usd {
            found.push(Diagnostic::warning(format!(
                "pipeline `{name}` may spend ${per_itinerary:.2} on one itinerary but `[reserve] \
                 fuel_usd` is ${:.2}; it could never run to completion",
                config.reserve.fuel_usd
            )));
        }
    }
}

fn check_entries_exist(config: &Config, found: &mut Vec<Diagnostic>) {
    for (name, pipeline) in &config.pipelines {
        if !config.agents.contains_key(&pipeline.entry) {
            found.push(Diagnostic::error(format!(
                "pipeline `{name}` enters at unknown agent `{}`",
                pipeline.entry
            )));
        }
    }
}

/// A flag name appears inside `@include(...)`, so it has to be an identifier.
fn check_flag_names_are_usable(config: &Config, found: &mut Vec<Diagnostic>) {
    for (name, pipeline) in &config.pipelines {
        for flag in pipeline.flags.keys() {
            if !is_identifier(flag) {
                found.push(Diagnostic::error(format!(
                    "pipeline `{name}` declares flag `{flag}`, which is not a usable name; use \
                     letters, digits and underscores, starting with a letter or underscore"
                )));
            }
        }
    }
}

/// Two pipelines may declare the same flag, but not with different defaults.
///
/// Prompts are shared between pipelines, so the same `@include(run_e2e)` line is read by every
/// pipeline that reaches that agent. Disagreeing defaults make the prompt mean different things
/// depending on which trigger fired, which is exactly the kind of thing nobody notices until the
/// output is wrong.
fn check_flag_declarations_agree(config: &Config, found: &mut Vec<Diagnostic>) {
    let mut seen: BTreeMap<&str, (bool, &str)> = BTreeMap::new();

    for (name, pipeline) in &config.pipelines {
        for (flag, spec) in &pipeline.flags {
            match seen.get(flag.as_str()) {
                Some((default, first)) if *default != spec.default => {
                    found.push(Diagnostic::warning(format!(
                        "flag `{flag}` defaults to {} in pipeline `{first}` and {} in pipeline \
                         `{name}`; the same prompt condition will mean different things",
                        default, spec.default
                    )));
                }
                Some(_) => {}
                None => {
                    seen.insert(flag.as_str(), (spec.default, name.as_str()));
                }
            }
        }
    }
}

/// Warns when a schedule can fire again before the previous run could plausibly have finished.
///
/// The Tower skips a tick whose previous wave is still going, so this is not a runaway — it is a
/// schedule that will mostly not run, which looks from the outside exactly like one that is
/// working. Under `overlap = "allow"` it *is* a runaway, and the wording says which.
fn check_schedules_are_survivable(config: &Config, found: &mut Vec<Diagnostic>) {
    let timeout = config.defaults.timeout_sec;

    for (name, pipeline) in config.scheduled_pipelines() {
        let Some(schedule) = pipeline.trigger.schedule() else {
            continue;
        };
        let Some(gap) = schedule.min_gap_secs() else {
            continue;
        };

        if gap < timeout {
            let consequence = if pipeline.allows_overlap() {
                "and `overlap = \"allow\"` is set, so copies will pile up concurrently"
            } else {
                "so most ticks will be skipped"
            };

            found.push(Diagnostic::warning(format!(
                "pipeline `{name}` fires at least every {gap}s but a single run may take \
                 {timeout}s, {consequence}"
            )));
        }
    }
}

fn is_identifier(name: &str) -> bool {
    let mut chars = name.chars();
    let Some(first) = chars.next() else {
        return false;
    };

    (first.is_ascii_alphabetic() || first == '_')
        && chars.all(|c| c.is_ascii_alphanumeric() || c == '_')
}

#[cfg(test)]
mod tests {
    use crate::validate::testing::{assert_mentions, errors, parse, warnings};
    use crate::validate::validate;

    const AGENT: &str = r#"
        [agents.analyst]
        runner = "claude"
        description = "analyses"
        access = "read-only"
        prompt = "analyse"
    "#;

    #[test]
    fn a_pipeline_entering_at_an_unknown_agent_is_an_error() {
        let config = parse(&format!(
            r#"
            {AGENT}

            [pipelines.development]
            entry = "ghost"
            "#
        ));

        assert_mentions(&errors(&config), "unknown agent `ghost`");
    }

    #[test]
    fn a_flag_name_that_is_not_an_identifier_is_an_error() {
        // It has to survive being written inside `@include(...)`.
        let config = parse(&format!(
            r#"
            {AGENT}

            [pipelines.development]
            entry = "analyst"

            [pipelines.development.flags]
            "run-e2e" = {{ default = false }}
            "#
        ));

        assert_mentions(&errors(&config), "not a usable name");
    }

    #[test]
    fn two_pipelines_disagreeing_on_a_flag_default_warns() {
        let config = parse(&format!(
            r#"
            {AGENT}

            [pipelines.development]
            entry = "analyst"

            [pipelines.development.flags]
            run_e2e = {{ default = false }}

            [pipelines.nightly]
            entry = "analyst"
            trigger = {{ every = "1d" }}

            [pipelines.nightly.flags]
            run_e2e = {{ default = true }}
            "#
        ));

        assert_mentions(&warnings(&config), "will mean different things");
    }

    #[test]
    fn two_pipelines_agreeing_on_a_flag_default_is_fine() {
        let config = parse(&format!(
            r#"
            {AGENT}

            [pipelines.development]
            entry = "analyst"

            [pipelines.development.flags]
            run_e2e = {{ default = true }}

            [pipelines.nightly]
            entry = "analyst"
            trigger = {{ every = "1d" }}

            [pipelines.nightly.flags]
            run_e2e = {{ default = true }}
            "#
        ));

        assert_eq!(validate(&config), Vec::new());
    }

    #[test]
    fn a_schedule_faster_than_a_run_warns() {
        let config = parse(&format!(
            r#"
            [defaults]
            timeout_sec = 1800

            {AGENT}

            [pipelines.review-bot]
            entry = "analyst"
            trigger = {{ every = "5m" }}
            "#
        ));

        assert_mentions(&warnings(&config), "most ticks will be skipped");
    }

    #[test]
    fn a_schedule_faster_than_a_run_that_is_allowed_to_overlap_says_copies_will_pile_up() {
        // The same shape means two different things now, and "most ticks will be skipped" would
        // be actively misleading for the one that spends money.
        let config = parse(&format!(
            r#"
            [defaults]
            timeout_sec = 1800

            {AGENT}

            [pipelines.review-bot]
            entry = "analyst"
            trigger = {{ every = "5m" }}
            overlap = "allow"
            "#
        ));

        assert_mentions(&warnings(&config), "pile up concurrently");
    }

    #[test]
    fn a_schedule_slower_than_a_run_is_fine() {
        let config = parse(&format!(
            r#"
            [defaults]
            timeout_sec = 900

            [reserve]
            fuel_usd = 200.0

            {AGENT}

            [pipelines.review-bot]
            entry = "analyst"
            trigger = {{ every = "1h" }}
            "#
        ));

        assert_eq!(validate(&config), Vec::new());
    }

    #[test]
    fn a_cron_that_fires_every_minute_is_warned_about() {
        // `* * * * *` states its own frequency, so the overlap check can read it off the minute
        // field without evaluating the expression.
        let config = parse(&format!(
            r#"
            [defaults]
            timeout_sec = 1800

            {AGENT}

            [pipelines.review-bot]
            entry = "analyst"
            trigger = {{ cron = "* * * * *" }}
            "#
        ));

        assert_mentions(&warnings(&config), "most ticks will be skipped");
    }

    #[test]
    fn a_stepped_cron_is_read_off_the_minute_field() {
        let config = parse(&format!(
            r#"
            [defaults]
            timeout_sec = 1800

            {AGENT}

            [pipelines.review-bot]
            entry = "analyst"
            trigger = {{ cron = "*/5 * * * *" }}
            "#
        ));

        assert_mentions(&warnings(&config), "at least every 300s");
    }

    #[test]
    fn an_hourly_cron_is_not_warned_about() {
        let config = parse(&format!(
            r#"
            [defaults]
            timeout_sec = 1800

            {AGENT}

            [pipelines.review-bot]
            entry = "analyst"
            trigger = {{ cron = "0 * * * *" }}
            "#
        ));

        assert_eq!(validate(&config), Vec::new());
    }

    #[test]
    fn a_cron_whose_frequency_is_not_obvious_is_left_alone() {
        // A list or a range could mean almost anything. Guessing would produce warnings that are
        // not true, and a validator that cries wolf is one people stop reading.
        let config = parse(&format!(
            r#"
            [defaults]
            timeout_sec = 1800

            [reserve]
            fuel_usd = 0.0

            {AGENT}

            [pipelines.review-bot]
            entry = "analyst"
            trigger = {{ cron = "0,30 * * * *" }}
            "#
        ));

        assert_mentions(&warnings(&config), "nothing bounds total spend");
        assert!(
            !warnings(&config).iter().any(|m| m.contains("could spend")),
            "an indeterminate cron cannot be projected"
        );
    }

    #[test]
    fn a_reserve_too_small_for_one_itinerary_warns() {
        // Unambiguously broken: the pipeline could never run to completion even once.
        let config = parse(&format!(
            r#"
            [defaults]
            fuel_usd = 20.0

            [reserve]
            fuel_usd = 5.0

            {AGENT}

            [pipelines.review-bot]
            entry = "analyst"
            trigger = {{ every = "1h" }}
            "#
        ));

        assert_mentions(&warnings(&config), "could never run to completion");
    }

    #[test]
    fn a_reserve_below_the_theoretical_worst_case_is_not_flagged() {
        // 24 hourly ticks at $20 is $480 of theoretical worst case against a $120 Reserve — and
        // that is fine. Capping below worst case is the entire reason a cap exists, and reaching
        // it pauses the factory rather than breaking it.
        let config = parse(&format!(
            r#"
            [defaults]
            fuel_usd = 20.0

            [reserve]
            fuel_usd = 120.0

            {AGENT}

            [pipelines.review-bot]
            entry = "analyst"
            trigger = {{ every = "1h" }}
            "#
        ));

        assert_eq!(validate(&config), Vec::new());
    }

    #[test]
    fn a_scheduled_factory_with_no_reserve_warns() {
        let config = parse(&format!(
            r#"
            [reserve]
            fuel_usd = 0.0

            {AGENT}

            [pipelines.review-bot]
            entry = "analyst"
            trigger = {{ every = "1h" }}
            "#
        ));

        assert_mentions(&warnings(&config), "nothing bounds total spend");
    }

    #[test]
    fn a_manual_only_factory_needs_no_reserve() {
        // Nobody is spending money while nobody is pressing the button.
        let config = parse(&format!(
            r#"
            [reserve]
            fuel_usd = 0.0

            {AGENT}

            [pipelines.development]
            entry = "analyst"
            trigger = "manual"
            "#
        ));

        assert_eq!(validate(&config), Vec::new());
    }
}