swale 0.1.0

A scheduled asset-graph orchestrator on object storage.
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
//! The internal graph model: nodes, their edges and the checks a graph passes
//! before it runs. Every front-end builds a [`Graph`] from a [`GraphSpec`]
//! through [`Graph::build`].

use std::collections::{BTreeMap, BTreeSet};
use std::time::Duration;

use crate::operator::{OperatorError, OperatorSet};
use crate::template::Template;

/// Maximum length of a graph name and a node name together. A run id is
/// `{graph}-{partition}-{node}-r{n}` with a partition of up to 16 bytes and a
/// rerun count of up to 10 digits, and the runtime caps the id at 128 bytes.
pub const MAX_GRAPH_AND_NODE_NAME_LEN: usize = 98;

/// How the assets of a graph are partitioned.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Partitioning {
    /// One partition per day, formatted `20260915`.
    Daily,
    /// One partition per hour, formatted `20260915T02`.
    Hourly,
    /// A single partition, formatted `none`.
    #[default]
    Unpartitioned,
}

/// The condition on the upstream task instances of a task node.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum TriggerRule {
    /// Every upstream succeeded.
    #[default]
    AllSucceeded,
    /// Every upstream terminated.
    AllDone,
    /// At least one upstream failed.
    OneFailed,
}

/// What a node is: an asset node or a task node.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NodeKind {
    /// A node that materialises an asset. Its upstreams are the producers of
    /// the assets it consumes.
    Asset {
        /// The asset the node produces.
        produces: String,
        /// The assets the node consumes.
        consumes: Vec<String>,
    },
    /// A node without an asset. Its upstreams are declared directly.
    Task {
        /// The upstream nodes.
        after: Vec<String>,
        /// The condition on the upstreams.
        trigger_rule: TriggerRule,
    },
}

/// The input of one node to [`Graph::build`].
#[derive(Debug, Clone, PartialEq)]
pub struct NodeSpec {
    /// The node name, unique in the graph.
    pub name: String,
    /// Asset node or task node.
    pub kind: NodeKind,
    /// The operator name, registered in the [`OperatorSet`].
    pub operator: String,
    /// The pool the node runs in.
    pub pool: String,
    /// The retries after the first attempt.
    pub retries: u32,
    /// The operator parameters. A string value can contain template
    /// references.
    pub params: toml::Table,
}

/// The input to [`Graph::build`].
#[derive(Debug, Clone, PartialEq)]
pub struct GraphSpec {
    /// The graph name.
    pub name: String,
    /// A cron expression with five fields.
    pub schedule: Option<String>,
    /// The catch-up window of a newly published graph.
    pub catchup: Option<Duration>,
    /// The partitioning of the graph's assets.
    pub partitioning: Partitioning,
    /// The nodes, in any order.
    pub nodes: Vec<NodeSpec>,
}

/// A node of a built [`Graph`].
#[derive(Debug, Clone, PartialEq)]
pub struct Node {
    spec: NodeSpec,
    upstreams: Vec<String>,
    downstreams: Vec<String>,
}

impl Node {
    /// The node name.
    pub fn name(&self) -> &str {
        &self.spec.name
    }

    /// Asset node or task node.
    pub fn kind(&self) -> &NodeKind {
        &self.spec.kind
    }

    /// The asset the node produces, absent for a task node.
    pub fn asset(&self) -> Option<&str> {
        match &self.spec.kind {
            NodeKind::Asset { produces, .. } => Some(produces),
            NodeKind::Task { .. } => None,
        }
    }

    /// The condition on the upstreams. An asset node has
    /// [`TriggerRule::AllSucceeded`].
    pub fn trigger_rule(&self) -> TriggerRule {
        match &self.spec.kind {
            NodeKind::Asset { .. } => TriggerRule::AllSucceeded,
            NodeKind::Task { trigger_rule, .. } => *trigger_rule,
        }
    }

    /// The operator name.
    pub fn operator(&self) -> &str {
        &self.spec.operator
    }

    /// The pool the node runs in.
    pub fn pool(&self) -> &str {
        &self.spec.pool
    }

    /// The retries after the first attempt.
    pub fn retries(&self) -> u32 {
        self.spec.retries
    }

    /// The operator parameters.
    pub fn params(&self) -> &toml::Table {
        &self.spec.params
    }

    /// The names of the upstream nodes, in lexical order.
    pub fn upstreams(&self) -> &[String] {
        &self.upstreams
    }

    /// The names of the downstream nodes, in lexical order.
    pub fn downstreams(&self) -> &[String] {
        &self.downstreams
    }
}

/// A checked graph. The nodes are stored in definition order.
#[derive(Debug, Clone, PartialEq)]
pub struct Graph {
    name: String,
    schedule: Option<String>,
    catchup: Option<Duration>,
    partitioning: Partitioning,
    nodes: Vec<Node>,
    index: BTreeMap<String, usize>,
}

/// A fault in a [`GraphSpec`]. The message identifies the node where one
/// applies.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum Problem {
    /// The graph name is outside `[a-z0-9_]+`.
    #[error("graph name `{0}` is not `[a-z0-9_]+`")]
    InvalidGraphName(String),
    /// The schedule is not a cron expression.
    #[error("schedule `{expression}` is not a cron expression: {message}")]
    InvalidSchedule {
        /// The schedule text.
        expression: String,
        /// The parser's message.
        message: String,
    },
    /// The catch-up window is not a duration.
    #[error("catchup: {0}")]
    InvalidCatchup(String),
    /// The graph is empty.
    #[error("the graph is empty")]
    NoNodes,
    /// An asset node declares `after`.
    #[error(
        "node `{0}`: an asset node declares its upstreams with `consumes` and does not declare `after`"
    )]
    AssetNodeAfter(String),
    /// An asset node declares a trigger rule.
    #[error(
        "node `{0}`: an asset node does not declare `trigger_rule`, because its upstreams must all succeed"
    )]
    AssetNodeTriggerRule(String),
    /// A task node declares `consumes`.
    #[error(
        "node `{0}`: a task node declares its upstreams with `after` and does not declare `consumes`"
    )]
    TaskNodeConsumes(String),
    /// Two nodes have the same name.
    #[error("node `{0}` is defined more than once")]
    DuplicateNode(String),
    /// A node name is outside `[a-z0-9_]+`.
    #[error("node `{0}`: the name is not `[a-z0-9_]+`")]
    InvalidNodeName(String),
    /// The graph name and the node name exceed
    /// [`MAX_GRAPH_AND_NODE_NAME_LEN`] together.
    #[error(
        "node `{0}`: the graph and node names exceed {MAX_GRAPH_AND_NODE_NAME_LEN} bytes together"
    )]
    NamesTooLong(String),
    /// An asset name is outside `[a-z0-9_]+`.
    #[error("node `{node}`: asset name `{asset}` is not `[a-z0-9_]+`")]
    InvalidAssetName {
        /// The node that declares the asset.
        node: String,
        /// The asset name.
        asset: String,
    },
    /// Two nodes produce the same asset.
    #[error("asset `{asset}` is produced by more than one node: {}", .nodes.join(", "))]
    DuplicateAsset {
        /// The asset name.
        asset: String,
        /// The producing nodes.
        nodes: Vec<String>,
    },
    /// A consumed asset is not produced in the graph.
    #[error("node `{node}`: consumes `{asset}`, which no node produces")]
    UnknownAsset {
        /// The consuming node.
        node: String,
        /// The asset name.
        asset: String,
    },
    /// An `after` entry is not a node name.
    #[error("node `{node}`: `after` refers to unknown node `{after}`")]
    UnknownNode {
        /// The declaring node.
        node: String,
        /// The unknown name.
        after: String,
    },
    /// The operator is not registered.
    #[error("node `{node}`: unknown operator `{operator}`")]
    UnknownOperator {
        /// The node.
        node: String,
        /// The operator name.
        operator: String,
    },
    /// The parameters do not deserialize into the operator's type.
    #[error("node `{node}`: the parameters of operator `{operator}` are invalid: {message}")]
    InvalidParams {
        /// The node.
        node: String,
        /// The operator name.
        operator: String,
        /// The deserializer's message.
        message: String,
    },
    /// A parameter string is not a template.
    #[error("node `{node}`: parameter `{param}` is not a template: {message}")]
    InvalidTemplate {
        /// The node.
        node: String,
        /// The parameter path, dotted.
        param: String,
        /// The parser's message.
        message: String,
    },
    /// A template refers to a node that is not an upstream of the node.
    #[error(
        "node `{node}`: parameter `{param}` refers to `{upstream}`, which is not an upstream of the node"
    )]
    UnknownTemplateUpstream {
        /// The node.
        node: String,
        /// The parameter path, dotted.
        param: String,
        /// The referenced node.
        upstream: String,
    },
    /// The edges form a cycle. The path lists the nodes along the cycle, each
    /// an upstream of the one before it, with the first node repeated at the
    /// end.
    #[error("the graph has a cycle: {}", .0.join(" -> "))]
    Cycle(Vec<String>),
}

impl Graph {
    /// Builds and checks a graph. Every fault found is returned, and a graph
    /// is returned only when no fault is found.
    pub fn build(spec: GraphSpec, operators: &OperatorSet) -> Result<Graph, Vec<Problem>> {
        let mut problems = Vec::new();

        if !is_name(&spec.name) {
            problems.push(Problem::InvalidGraphName(spec.name.clone()));
        }
        if let Some(expression) = &spec.schedule
            && let Err(e) = croner::Cron::new(expression).parse()
        {
            problems.push(Problem::InvalidSchedule {
                expression: expression.clone(),
                message: e.to_string(),
            });
        }
        if spec.nodes.is_empty() {
            problems.push(Problem::NoNodes);
        }

        let mut names = BTreeSet::new();
        for node in &spec.nodes {
            if !is_name(&node.name) {
                problems.push(Problem::InvalidNodeName(node.name.clone()));
            } else if spec.name.len() + node.name.len() > MAX_GRAPH_AND_NODE_NAME_LEN {
                problems.push(Problem::NamesTooLong(node.name.clone()));
            }
            if !names.insert(node.name.as_str()) {
                problems.push(Problem::DuplicateNode(node.name.clone()));
            }
        }

        let mut producers: BTreeMap<&str, Vec<&str>> = BTreeMap::new();
        for node in &spec.nodes {
            if let NodeKind::Asset { produces, consumes } = &node.kind {
                for asset in std::iter::once(produces).chain(consumes) {
                    if !is_name(asset) {
                        problems.push(Problem::InvalidAssetName {
                            node: node.name.clone(),
                            asset: asset.clone(),
                        });
                    }
                }
                producers.entry(produces).or_default().push(&node.name);
            }
        }
        for (asset, nodes) in &producers {
            if nodes.len() > 1 {
                problems.push(Problem::DuplicateAsset {
                    asset: asset.to_string(),
                    nodes: nodes.iter().map(|n| n.to_string()).collect(),
                });
            }
        }

        let mut upstreams: Vec<BTreeSet<String>> = Vec::with_capacity(spec.nodes.len());
        for node in &spec.nodes {
            let mut set = BTreeSet::new();
            match &node.kind {
                NodeKind::Asset { consumes, .. } => {
                    for asset in consumes {
                        match producers.get(asset.as_str()) {
                            Some(nodes) => set.extend(nodes.iter().map(|n| n.to_string())),
                            None => problems.push(Problem::UnknownAsset {
                                node: node.name.clone(),
                                asset: asset.clone(),
                            }),
                        }
                    }
                }
                NodeKind::Task { after, .. } => {
                    for name in after {
                        if names.contains(name.as_str()) {
                            set.insert(name.clone());
                        } else {
                            problems.push(Problem::UnknownNode {
                                node: node.name.clone(),
                                after: name.clone(),
                            });
                        }
                    }
                }
            }
            upstreams.push(set);
        }

        for (node, upstream) in spec.nodes.iter().zip(&upstreams) {
            match operators.check(&node.operator, &node.params) {
                Ok(()) => {}
                Err(OperatorError::Unknown) => problems.push(Problem::UnknownOperator {
                    node: node.name.clone(),
                    operator: node.operator.clone(),
                }),
                Err(OperatorError::InvalidParams(message)) => {
                    problems.push(Problem::InvalidParams {
                        node: node.name.clone(),
                        operator: node.operator.clone(),
                        message,
                    })
                }
            }
            check_templates(
                &node.name,
                "",
                &toml::Value::Table(node.params.clone()),
                upstream,
                &mut problems,
            );
        }

        if !problems.is_empty() {
            return Err(problems);
        }

        if let Some(cycle) = find_cycle(&spec.nodes, &upstreams) {
            return Err(vec![Problem::Cycle(cycle)]);
        }

        let mut downstreams: BTreeMap<&str, BTreeSet<String>> = BTreeMap::new();
        for (node, upstream) in spec.nodes.iter().zip(&upstreams) {
            for up in upstream {
                downstreams
                    .entry(up.as_str())
                    .or_default()
                    .insert(node.name.clone());
            }
        }

        let mut nodes = Vec::with_capacity(spec.nodes.len());
        let mut index = BTreeMap::new();
        for (i, node_spec) in spec.nodes.into_iter().enumerate() {
            let downstream = downstreams
                .remove(node_spec.name.as_str())
                .unwrap_or_default();
            index.insert(node_spec.name.clone(), i);
            nodes.push(Node {
                upstreams: upstreams[i].iter().cloned().collect(),
                downstreams: downstream.into_iter().collect(),
                spec: node_spec,
            });
        }

        Ok(Graph {
            name: spec.name,
            schedule: spec.schedule,
            catchup: spec.catchup,
            partitioning: spec.partitioning,
            nodes,
            index,
        })
    }

    /// The graph name.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// The cron expression, absent for a graph run by target requests only.
    pub fn schedule(&self) -> Option<&str> {
        self.schedule.as_deref()
    }

    /// The catch-up window of a newly published graph.
    pub fn catchup(&self) -> Option<Duration> {
        self.catchup
    }

    /// The partitioning of the graph's assets.
    pub fn partitioning(&self) -> Partitioning {
        self.partitioning
    }

    /// The nodes in definition order.
    pub fn nodes(&self) -> &[Node] {
        &self.nodes
    }

    /// The node with `name`.
    pub fn node(&self, name: &str) -> Option<&Node> {
        self.index.get(name).map(|&i| &self.nodes[i])
    }

    /// The nodes without an upstream, in the order of [`Graph::nodes`].
    pub fn roots(&self) -> impl Iterator<Item = &Node> {
        self.nodes.iter().filter(|n| n.upstreams.is_empty())
    }

    /// The nodes without a downstream, in the order of [`Graph::nodes`].
    pub fn leaves(&self) -> impl Iterator<Item = &Node> {
        self.nodes.iter().filter(|n| n.downstreams.is_empty())
    }

    /// The number of edges.
    pub fn edge_count(&self) -> usize {
        self.nodes.iter().map(|n| n.upstreams.len()).sum()
    }
}

/// Whether `text` matches `[a-z0-9_]+`.
pub fn is_name(text: &str) -> bool {
    !text.is_empty()
        && text
            .bytes()
            .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'_')
}

fn check_templates(
    node: &str,
    path: &str,
    value: &toml::Value,
    upstreams: &BTreeSet<String>,
    problems: &mut Vec<Problem>,
) {
    let child = |key: &str| {
        if path.is_empty() {
            key.to_string()
        } else {
            format!("{path}.{key}")
        }
    };
    match value {
        toml::Value::String(text) => match Template::parse(text) {
            Ok(template) => {
                let mut seen = BTreeSet::new();
                for upstream in template.upstream_nodes() {
                    if !upstreams.contains(upstream) && seen.insert(upstream) {
                        problems.push(Problem::UnknownTemplateUpstream {
                            node: node.to_string(),
                            param: path.to_string(),
                            upstream: upstream.to_string(),
                        });
                    }
                }
            }
            Err(e) => problems.push(Problem::InvalidTemplate {
                node: node.to_string(),
                param: path.to_string(),
                message: e.to_string(),
            }),
        },
        toml::Value::Array(items) => {
            for (i, item) in items.iter().enumerate() {
                check_templates(node, &child(&i.to_string()), item, upstreams, problems);
            }
        }
        toml::Value::Table(table) => {
            for (key, item) in table {
                check_templates(node, &child(key), item, upstreams, problems);
            }
        }
        _ => {}
    }
}

/// The path of a cycle as node names, each an upstream of the one before it
/// and the first repeated at the end, or `None` when the edges are acyclic.
fn find_cycle(nodes: &[NodeSpec], upstreams: &[BTreeSet<String>]) -> Option<Vec<String>> {
    #[derive(Clone, Copy, PartialEq, Eq)]
    enum Mark {
        Unvisited,
        Active,
        Done,
    }

    fn visit(
        i: usize,
        nodes: &[NodeSpec],
        upstreams: &[BTreeSet<String>],
        index: &BTreeMap<&str, usize>,
        marks: &mut [Mark],
        path: &mut Vec<usize>,
    ) -> Option<Vec<String>> {
        marks[i] = Mark::Active;
        path.push(i);
        for up in &upstreams[i] {
            let j = index[up.as_str()];
            match marks[j] {
                Mark::Active => {
                    let start = path
                        .iter()
                        .position(|&p| p == j)
                        .expect("an active node is on the path");
                    let mut cycle: Vec<String> = path[start..]
                        .iter()
                        .map(|&p| nodes[p].name.clone())
                        .collect();
                    cycle.push(nodes[j].name.clone());
                    return Some(cycle);
                }
                Mark::Unvisited => {
                    if let Some(cycle) = visit(j, nodes, upstreams, index, marks, path) {
                        return Some(cycle);
                    }
                }
                Mark::Done => {}
            }
        }
        path.pop();
        marks[i] = Mark::Done;
        None
    }

    let index: BTreeMap<&str, usize> = nodes
        .iter()
        .enumerate()
        .map(|(i, n)| (n.name.as_str(), i))
        .collect();
    let mut marks = vec![Mark::Unvisited; nodes.len()];
    let mut path = Vec::new();
    for i in 0..nodes.len() {
        if marks[i] == Mark::Unvisited
            && let Some(cycle) = visit(i, nodes, upstreams, &index, &mut marks, &mut path)
        {
            return Some(cycle);
        }
    }
    None
}

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

    fn asset(name: &str, produces: &str, consumes: &[&str]) -> NodeSpec {
        NodeSpec {
            name: name.into(),
            kind: NodeKind::Asset {
                produces: produces.into(),
                consumes: consumes.iter().map(|s| s.to_string()).collect(),
            },
            operator: "subprocess".into(),
            pool: "default".into(),
            retries: 0,
            params: r#"argv = ["true"]"#.parse().unwrap(),
        }
    }

    fn task(name: &str, after: &[&str]) -> NodeSpec {
        NodeSpec {
            name: name.into(),
            kind: NodeKind::Task {
                after: after.iter().map(|s| s.to_string()).collect(),
                trigger_rule: TriggerRule::AllDone,
            },
            ..asset(name, "", &[])
        }
    }

    fn spec(nodes: Vec<NodeSpec>) -> GraphSpec {
        GraphSpec {
            name: "g".into(),
            schedule: Some("0 2 * * *".into()),
            catchup: None,
            partitioning: Partitioning::Daily,
            nodes,
        }
    }

    fn build(nodes: Vec<NodeSpec>) -> Result<Graph, Vec<Problem>> {
        Graph::build(spec(nodes), &OperatorSet::builtin())
    }

    #[test]
    fn edges_follow_from_assets_and_after_and_nodes_keep_definition_order() {
        let graph = build(vec![
            asset("load", "c", &["b"]),
            asset("transform", "b", &["a"]),
            task("notify", &["load", "transform"]),
            asset("extract", "a", &[]),
        ])
        .unwrap();
        let names: Vec<&str> = graph.nodes().iter().map(Node::name).collect();
        assert_eq!(names, ["load", "transform", "notify", "extract"]);
        assert_eq!(graph.node("load").unwrap().upstreams(), ["transform"]);
        assert_eq!(
            graph.node("transform").unwrap().downstreams(),
            ["load", "notify"]
        );
        assert_eq!(
            graph.node("notify").unwrap().upstreams(),
            ["load", "transform"]
        );
        assert_eq!(
            graph.roots().map(Node::name).collect::<Vec<_>>(),
            ["extract"]
        );
        assert_eq!(
            graph.leaves().map(Node::name).collect::<Vec<_>>(),
            ["notify"]
        );
        assert_eq!(graph.edge_count(), 4);
        assert_eq!(graph.node("load").unwrap().asset(), Some("c"));
        assert_eq!(graph.node("notify").unwrap().asset(), None);
        assert_eq!(
            graph.node("load").unwrap().trigger_rule(),
            TriggerRule::AllSucceeded
        );
        assert_eq!(
            graph.node("notify").unwrap().trigger_rule(),
            TriggerRule::AllDone
        );
    }

    #[test]
    fn every_fault_is_reported_at_once() {
        let mut bad = spec(vec![
            asset("Extract", "Raw", &["missing"]),
            asset("x", "dup", &[]),
            asset("x", "dup", &[]),
            NodeSpec {
                operator: "sql".into(),
                ..asset("y", "y", &[])
            },
            NodeSpec {
                params: "".parse().unwrap(),
                ..asset("z", "z", &[])
            },
            task("t", &["nowhere"]),
        ]);
        bad.name = "Bad-Graph".into();
        bad.schedule = Some("every day".into());
        let problems = Graph::build(bad, &OperatorSet::builtin()).unwrap_err();
        assert!(problems.contains(&Problem::InvalidGraphName("Bad-Graph".into())));
        assert!(
            matches!(&problems[1], Problem::InvalidSchedule { expression, .. } if expression == "every day")
        );
        assert!(problems.contains(&Problem::InvalidNodeName("Extract".into())));
        assert!(problems.contains(&Problem::InvalidAssetName {
            node: "Extract".into(),
            asset: "Raw".into(),
        }));
        assert!(problems.contains(&Problem::UnknownAsset {
            node: "Extract".into(),
            asset: "missing".into(),
        }));
        assert!(problems.contains(&Problem::DuplicateNode("x".into())));
        assert!(problems.contains(&Problem::DuplicateAsset {
            asset: "dup".into(),
            nodes: vec!["x".into(), "x".into()],
        }));
        assert!(problems.contains(&Problem::UnknownOperator {
            node: "y".into(),
            operator: "sql".into(),
        }));
        assert!(matches!(
            problems.iter().find(|p| matches!(p, Problem::InvalidParams { .. })),
            Some(Problem::InvalidParams { node, operator, .. }) if node == "z" && operator == "subprocess"
        ));
        assert!(problems.contains(&Problem::UnknownNode {
            node: "t".into(),
            after: "nowhere".into(),
        }));
    }

    #[test]
    fn empty_graph_is_rejected() {
        assert_eq!(build(vec![]), Err(vec![Problem::NoNodes]));
    }

    #[test]
    fn names_are_bounded_so_every_run_id_fits() {
        let long = "n".repeat(MAX_GRAPH_AND_NODE_NAME_LEN - 1);
        assert!(build(vec![asset(&long, "a", &[])]).is_ok());
        let too_long = "n".repeat(MAX_GRAPH_AND_NODE_NAME_LEN);
        assert_eq!(
            build(vec![asset(&too_long, "a", &[])]),
            Err(vec![Problem::NamesTooLong(too_long)])
        );
    }

    #[test]
    fn templates_are_checked_at_every_depth_against_declared_upstreams() {
        let mut load = asset("load", "c", &["a"]);
        load.params = r#"
            argv = ["{{ upstream.extract.rows }} {{ upstream.extract.rows }}"]
            [nested]
            list = ["ok {{ partition }}", "{{ upstream.other.x }}"]
            broken = "{{ partition"
        "#
        .parse()
        .unwrap();
        let problems = build(vec![
            asset("extract", "a", &[]),
            asset("other", "o", &[]),
            load,
        ])
        .unwrap_err();
        assert_eq!(
            problems,
            vec![
                Problem::InvalidParams {
                    node: "load".into(),
                    operator: "subprocess".into(),
                    message: problems
                        .iter()
                        .find_map(|p| match p {
                            Problem::InvalidParams { message, .. } => Some(message.clone()),
                            _ => None,
                        })
                        .unwrap(),
                },
                Problem::InvalidTemplate {
                    node: "load".into(),
                    param: "nested.broken".into(),
                    message: "`{{` at byte 0 is not closed".into(),
                },
                Problem::UnknownTemplateUpstream {
                    node: "load".into(),
                    param: "nested.list.1".into(),
                    upstream: "other".into(),
                },
            ]
        );
    }

    #[test]
    fn cycle_is_reported_as_its_path() {
        // `after_cycle` depends on the cycle and is not on it. The search
        // reaches the cycle from `a`, the first node in definition order that
        // is on it.
        let problems = build(vec![
            asset("root", "r", &[]),
            asset("a", "a", &["b", "r"]),
            asset("b", "b", &["a"]),
            asset("after_cycle", "d", &["b"]),
        ])
        .unwrap_err();
        assert_eq!(
            problems,
            vec![Problem::Cycle(vec!["a".into(), "b".into(), "a".into()])]
        );
        assert_eq!(
            problems[0].to_string(),
            "the graph has a cycle: a -> b -> a"
        );
    }

    #[test]
    fn is_name_accepts_lowercase_digits_and_underscore_only() {
        assert!(is_name("orders_daily_2"));
        for bad in ["", "Orders", "orders-daily", "orders daily", "ordérs"] {
            assert!(!is_name(bad), "{bad}");
        }
    }
}