local-ci 0.20.0

Run the GitHub Actions jobs a branch would trigger, locally, in an isolated worktree, and print a short report
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
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
//! The job graph of a plan and the tasks it is run as.
//!
//! [`JobGraph`] is the workflow's jobs with their `needs` edges, a matrix
//! expanded into one node per variant plus the node later jobs `needs`
//! (the aggregation point). [`ExecutionTask`] is what can be placed on a host:
//! by default one task per job and one per matrix variant, following the
//! independent-job semantics of GitHub Actions (no files shared between jobs).
//! Jobs that the engine's unit keeps together because they write the same
//! files stay one task, and the task says why ([`GroupedReason`]).
//!
//! Nothing here runs anything; see [`crate::execute_task`].

use crate::artifact;
use crate::expr::NeedsRef;
use crate::plan::{self, Plan};
use crate::shard::{Discovered, RunnerInfo, ShardPlan, SplitMode};
use crate::wire::{JobSpec, MatrixValue, UnitSpec, WorkflowSpec};
use anyhow::{bail, Result};
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use std::collections::{BTreeMap, BTreeSet, HashMap};

/// `<workflow file>::<job id>`; a matrix variant adds `#<index>`.
pub fn node_id(workflow: &str, job: &str) -> String {
    format!("{workflow}::{job}")
}

fn variant_id(workflow: &str, job: &str, index: usize) -> String {
    format!("{workflow}::{job}#{index}")
}

/// How the outputs of a matrix job's variants become the outputs of the job.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum OutputAggregation {
    /// In the order of the matrix, whatever the order the variants finished
    /// in; for a name set by several variants the last one in matrix order
    /// wins. (GitHub takes the variant that finished last, which no one can
    /// plan for.)
    MatrixOrder,
    /// The shards of a split test job: each test once, whatever the shard it
    /// ran in; a test that was not reported, or twice, is a problem
    /// ([`crate::complete::aggregate_shard_group`]).
    TestIdentity,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum NodeKind {
    /// A job without a matrix.
    Job,
    /// A job whose matrix is an expression: its variants are known only when
    /// the jobs it needs have finished, so the node is one task that expands
    /// them when it runs.
    DynamicMatrix { expression: String },
    /// One variant of a matrix that is known at planning.
    MatrixVariant {
        /// The node later jobs depend on.
        group: String,
        index: usize,
        values: Map<String, Value>,
        label: String,
    },
    /// The point where a matrix job's variants meet: later jobs depend on
    /// this node, and it is done when every variant is.
    MatrixGroup {
        variants: Vec<String>,
        fail_fast: bool,
        max_parallel: Option<usize>,
        aggregation: OutputAggregation,
    },
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct JobNode {
    pub id: String,
    /// The workflow file.
    pub workflow: String,
    pub job: String,
    #[serde(flatten)]
    pub kind: NodeKind,
    /// Nodes that must be done first.
    pub depends_on: Vec<String>,
}

/// The jobs of the plan as a DAG. `nodes` are in dependency order (a node
/// comes after everything it depends on), otherwise in plan order.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct JobGraph {
    /// See [`crate::wire::WIRE_SCHEMA_VERSION`].
    #[serde(default = "crate::wire::wire_version")]
    pub schema_version: u32,
    pub nodes: Vec<JobNode>,
}

impl crate::wire::Digested for JobGraph {
    fn schema_version(&self) -> u32 {
        self.schema_version
    }
}
impl crate::wire::Digested for ExecutionTask {
    fn schema_version(&self) -> u32 {
        self.schema_version
    }
}

impl Default for JobGraph {
    fn default() -> Self {
        JobGraph {
            schema_version: crate::wire::wire_version(),
            nodes: vec![],
        }
    }
}

impl JobGraph {
    pub fn node(&self, id: &str) -> Option<&JobNode> {
        self.nodes.iter().find(|n| n.id == id)
    }
}

/// What a job's matrix is at planning.
pub(crate) enum MatrixPlan {
    None,
    Dynamic(String),
    Static(Vec<Map<String, Value>>),
}

pub(crate) fn matrix_plan(job: &JobSpec) -> MatrixPlan {
    let Some(m) = &job.matrix else {
        return MatrixPlan::None;
    };
    match m {
        MatrixValue::Map(_) if !m.has_expression() => match crate::matrix::expand(&m.to_json()) {
            // `expand` never returns no variant: it fails instead
            Ok(c) => MatrixPlan::Static(c),
            Err(_) => MatrixPlan::Dynamic("matrix".into()),
        },
        MatrixValue::String(s) => MatrixPlan::Dynamic(s.clone()),
        _ => MatrixPlan::Dynamic("matrix".into()),
    }
}

/// The graph of the jobs `units` hold. Fails on a `needs` that names no job of
/// the plan and on a cycle; [`unrunnable_workflows`] tells which workflows
/// those are, so a plan can leave them out.
pub fn build_graph(workflows: &[WorkflowSpec], units: &[UnitSpec]) -> Result<JobGraph> {
    let mut nodes: Vec<JobNode> = Vec::new();
    for wi in 0..workflows.len() {
        nodes.extend(workflow_nodes(workflows, units, wi)?);
    }
    order(nodes)
}

/// The workflows (by index) that cannot be a job graph, each with why: a
/// `needs` of a job that is not in the plan, a cycle, or a matrix with two
/// keys that print alike. GitHub does not start such a workflow; the others
/// are not affected.
pub fn unrunnable_workflows(
    workflows: &[WorkflowSpec],
    units: &[UnitSpec],
) -> Vec<(usize, String)> {
    let mut out = Vec::new();
    for (wi, w) in workflows.iter().enumerate() {
        let why = w
            .jobs
            .iter()
            .find_map(|j| {
                let key = j.matrix.as_ref()?.duplicate_key()?;
                Some(format!(
                    "the matrix of job `{}` has two keys that print as `{key}`",
                    j.id
                ))
            })
            .or_else(|| {
                workflow_nodes(workflows, units, wi)
                    .and_then(order)
                    .err()
                    .map(|e| format!("{e:#}"))
            });
        if let Some(why) = why {
            out.push((wi, why));
        }
    }
    out
}

/// The nodes of workflow `wi`, in the order of its jobs.
fn workflow_nodes(
    workflows: &[WorkflowSpec],
    units: &[UnitSpec],
    wi: usize,
) -> Result<Vec<JobNode>> {
    let w = &workflows[wi];
    let mut nodes: Vec<JobNode> = Vec::new();
    let planned: BTreeSet<&str> = units
        .iter()
        .filter(|u| u.workflow == wi)
        .flat_map(|u| u.jobs.iter().map(String::as_str))
        .collect();
    for job in w.jobs.iter().filter(|j| planned.contains(j.id.as_str())) {
        let mut deps = Vec::new();
        for n in &job.needs {
            if !planned.contains(n.as_str()) {
                bail!(
                    "job `{}` of {} needs `{n}`, which is not a job of the plan",
                    job.id,
                    w.file
                );
            }
            deps.push(node_id(&w.file, n));
        }
        let id = node_id(&w.file, &job.id);
        let mk = |id: String, kind: NodeKind, depends_on: Vec<String>| JobNode {
            id,
            workflow: w.file.clone(),
            job: job.id.clone(),
            kind,
            depends_on,
        };
        match matrix_plan(job) {
            MatrixPlan::None => nodes.push(mk(id, NodeKind::Job, deps)),
            MatrixPlan::Dynamic(expression) => {
                nodes.push(mk(id, NodeKind::DynamicMatrix { expression }, deps))
            }
            MatrixPlan::Static(combos) => {
                let mut ids = Vec::new();
                for (i, c) in combos.into_iter().enumerate() {
                    let vid = variant_id(&w.file, &job.id, i);
                    ids.push(vid.clone());
                    let label = crate::matrix::describe(&c);
                    nodes.push(mk(
                        vid,
                        NodeKind::MatrixVariant {
                            group: id.clone(),
                            index: i,
                            values: c,
                            label,
                        },
                        deps.clone(),
                    ));
                }
                nodes.push(mk(
                    id,
                    NodeKind::MatrixGroup {
                        variants: ids.clone(),
                        fail_fast: job.fail_fast,
                        max_parallel: job.max_parallel,
                        aggregation: OutputAggregation::MatrixOrder,
                    },
                    ids,
                ));
            }
        }
    }
    Ok(nodes)
}

/// Dependency order, keeping plan order where it allows; a cycle is an error.
fn order(nodes: Vec<JobNode>) -> Result<JobGraph> {
    let mut done: Vec<JobNode> = Vec::with_capacity(nodes.len());
    let mut seen: BTreeSet<String> = BTreeSet::new();
    let mut rest = nodes;
    while !rest.is_empty() {
        let at = rest
            .iter()
            .position(|n| n.depends_on.iter().all(|d| seen.contains(d)));
        match at {
            Some(i) => {
                let n = rest.remove(i);
                seen.insert(n.id.clone());
                done.push(n);
            }
            None => {
                let names: Vec<String> = rest
                    .iter()
                    .filter(|n| !matches!(n.kind, NodeKind::MatrixVariant { .. }))
                    .map(|n| n.id.clone())
                    .collect();
                bail!(
                    "the jobs depend on each other in a cycle: {}",
                    names.join(", ")
                );
            }
        }
    }
    Ok(JobGraph {
        nodes: done,
        ..JobGraph::default()
    })
}

// ---- tasks ----

/// Why jobs that could run apart are one task.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "reason", rename_all = "snake_case")]
pub enum GroupedReason {
    /// They write the same files or build output, so they must not run at
    /// the same time in one checkout, and splitting them would change what
    /// they read.
    SharedOutputs { files: Vec<String> },
    /// Their shared files would put a job between two of them that depends on
    /// the first and is needed by the second: the whole group stays together.
    OrderAroundSharedOutputs,
}

/// Where a task's files live.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct WorkspacePolicy {
    /// A checkout of the planned commit of its own; nothing is shared with
    /// another task (files cross jobs as artifacts, within one workflow run:
    /// see [`crate::artifact_io`]).
    pub own_checkout: bool,
    /// The jobs of the task share that checkout.
    pub shared_by_jobs: bool,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TaskService {
    pub job: String,
    pub id: String,
    pub image: String,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RetryPolicy {
    /// Times the task may be started for one run. A local run starts it once.
    pub max_attempts: u32,
}

/// What the result of the task has to contain to be accepted: what its jobs
/// declare (checked by [`crate::verify_task_result`] and
/// [`crate::validate_run_result`]).
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct OutputVerification {
    /// `<job>.<output>` for each output the jobs declare.
    pub job_outputs: Vec<String>,
    /// The artifacts the task must publish: a literal name uploaded by a step
    /// that always runs and fails without files
    /// ([`crate::artifact::required_upload`]). Each must be among the artifacts
    /// uploaded by the task's own jobs.
    pub artifacts: Vec<String>,
}

/// An output of a job this task needs.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct NeedsOutput {
    pub job: String,
    pub name: String,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TaskVariant {
    /// The node of the matrix job.
    pub group: String,
    pub index: usize,
    pub values: Map<String, Value>,
    pub label: String,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ExecutionTask {
    /// See [`crate::wire::WIRE_SCHEMA_VERSION`].
    #[serde(default = "crate::wire::wire_version")]
    pub schema_version: u32,
    pub id: String,
    /// The workflow file.
    pub workflow: String,
    /// The job ids, in the order they run.
    pub jobs: Vec<String>,
    /// The graph nodes the task runs.
    pub nodes: Vec<String>,
    /// The engine unit this came from (index into the plan's units).
    pub unit: usize,
    pub variant: Option<TaskVariant>,
    /// The job's matrix is an expression: the task expands and runs it all.
    pub dynamic_matrix: bool,
    /// Tasks whose results (and outputs) are needed before this one starts.
    pub depends_on: Vec<String>,
    /// Outputs of jobs outside the task that its expressions read.
    pub needs_outputs: Vec<NeedsOutput>,
    /// The artifacts the task's jobs download by name
    /// ([`crate::artifact::needed_download`]), of the same workflow run.
    pub artifacts: Vec<String>,
    pub workspace: WorkspacePolicy,
    pub services: Vec<TaskService>,
    pub exclusive: bool,
    pub retry: RetryPolicy,
    pub verification: OutputVerification,
    pub grouped_reason: Option<GroupedReason>,
    /// The part of a split job this task runs.
    #[serde(default)]
    pub shard: Option<TaskShard>,
}

/// Which part of a split job a task runs.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TaskShard {
    /// The shard group (the id of the job's task before the split).
    pub group: String,
    pub index: usize,
    pub count: usize,
    pub mode: SplitMode,
    /// The tests of this shard (empty for the runner's own shard feature).
    pub tests: Vec<String>,
    /// The runner and the arguments of the job's own command: what the shard's
    /// command is built from when the task runs.
    #[serde(default)]
    pub runner: RunnerInfo,
    /// The step of the job that runs the tests; the task runs the shard's
    /// command in its place.
    #[serde(default)]
    pub step: usize,
    /// The working directory (relative to the plan root, `""` for the root)
    /// the runner's tests were listed in: the step runs the shard's command
    /// there, so it must be the step's own directory.
    #[serde(default)]
    pub dir: String,
}

/// `<workflow file>::<job id>@<index>`: the task of one shard of a split job.
pub fn shard_task_id(workflow: &str, job: &str, index: usize) -> String {
    format!("{workflow}::{job}@{index}")
}

/// A request to split one job, made by the caller from a listing and a history
/// (the plan cannot run a runner). Nothing is split without one.
#[derive(Debug, Clone, PartialEq)]
pub struct ShardRequest {
    /// The workflow file.
    pub workflow: String,
    pub job: String,
    pub plan: ShardPlan,
    /// The step (index in the job's `steps`) whose `run` is the command the
    /// plan's runner was made from, with its working directory as the project
    /// root. A shard's task runs its own command in place of that step.
    pub step: usize,
    /// The directory the runner listed its tests in, relative to the plan root
    /// (`""` for the root). The step must run there: with another
    /// `working-directory` the shards would run other tests than the ones
    /// listed.
    pub dir: String,
    /// What the runner listed; the shards are checked against it.
    pub discovered: Discovered,
    /// A failed shard cancels the shards that have not finished.
    pub fail_fast: bool,
}

/// Why a task is not split into shards of one test run.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum NotSplit {
    /// No runner adapter can list and partition its tests yet.
    NoRunnerAdapter,
    /// Its jobs share a checkout.
    SharedWorkspace,
    /// It starts services that one shard cannot get a copy of.
    HasServices,
    /// It runs one variant of a matrix; the matrix is the split.
    MatrixVariant,
    /// A runner adapter knows the job and the split was not asked for.
    NotRequested,
    /// The runner cannot list its tests.
    NoEnumeration,
    /// The runner's listing failed.
    DiscoveryFailed,
    /// The listing is not all the tests (they are generated at run time) and
    /// the runner has no shard feature of its own.
    IncompleteDiscovery,
    /// A run cannot be told which tests to run.
    NoShardTarget,
    /// The shards would update state they share.
    SharedState,
    /// A database, port or container cannot be given to each shard.
    NoIsolation,
    /// The shards' results do not merge to the verdict of the whole job.
    InexactAggregation,
    /// Too few tests, or too little time, for a second shard to pay.
    TooSmall,
    /// The shards do not hold the listed tests exactly once.
    ManifestMismatch,
    /// The job's own command has a name filter, a skip or a tag: the shards
    /// would run the tests of the whole job.
    JobFilter,
    /// The job's own command has an argument (or shell syntax) that is not
    /// known to leave the set of tests alone.
    UnreadableArgs,
    /// The step the shard's command would replace is not one plain command of
    /// the runner the plan was made for.
    NotConnectable,
    /// A test file's path is part of another's: a path given as a filter
    /// would run both.
    PathOverlap,
    /// The shard's command would be too long to start.
    ArgvTooLarge,
    /// The job collects coverage; each shard would measure only its own tests
    /// and nothing here puts the reports together.
    CoverageNotMerged,
    /// A Playwright project depends on another (or has a teardown), so a
    /// shard that runs its tests also runs the other project's, in every
    /// shard.
    ProjectDependencies,
    /// The runner is too old for a shard to name its tests (Playwright before
    /// `--test-list`), or its version cannot be read.
    RunnerTooOld,
}

/// A job (or the jobs of a task) that could become several shards, and the
/// reasons it did not. A task is split only on a [`ShardRequest`].
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ShardGroup {
    pub id: String,
    pub workflow: String,
    pub source_jobs: Vec<String>,
    /// The tasks the source is run as (one, when nothing is split).
    pub shards: Vec<String>,
    pub split: bool,
    pub not_split: Vec<NotSplit>,
    /// How shard results are merged (none when not split).
    pub aggregation: Option<OutputAggregation>,
    /// The runner of a split job.
    #[serde(default)]
    pub runner: Option<RunnerInfo>,
    /// The tests the shards hold together, once each (empty when the runner's
    /// own shard feature divides them).
    #[serde(default)]
    pub manifest: Vec<String>,
    /// A failed shard cancels the others.
    #[serde(default)]
    pub fail_fast: bool,
    /// The tests of each shard, in the order of `shards` (empty for a native
    /// split).
    #[serde(default)]
    pub assignments: Vec<Vec<String>>,
    /// `manifest` is all the tests there are. `false` for a native split whose
    /// listing may be partial: every listed test must be reported, others may
    /// be.
    #[serde(default)]
    pub manifest_exact: bool,
}

/// The outputs of other jobs that `job` reads, found by reading its
/// expressions. An output named by `needs.x.outputs.y`, `needs['x'].outputs['y']`
/// and the like is that one; `needs.x.outputs` taken whole or `toJSON(needs)`
/// count as every output the job needed declares, since the plan cannot say
/// which are read.
fn needs_outputs_of(job: &JobSpec, wf: &WorkflowSpec) -> Vec<NeedsOutput> {
    fn strings(v: &Value, bare: bool, out: &mut Vec<(String, bool)>) {
        match v {
            Value::String(s) => out.push((s.clone(), bare)),
            Value::Array(a) => a.iter().for_each(|e| strings(e, false, out)),
            Value::Object(m) => {
                for (k, e) in m {
                    strings(e, k == "cond", out);
                }
            }
            _ => {}
        }
    }
    let mut texts = Vec::new();
    strings(
        &serde_json::to_value(job).unwrap_or_default(),
        false,
        &mut texts,
    );
    let declared = |id: &str| -> Vec<NeedsOutput> {
        wf.job(id)
            .map(|j| {
                j.outputs
                    .iter()
                    .map(|(name, _)| NeedsOutput {
                        job: id.to_string(),
                        name: name.clone(),
                    })
                    .collect()
            })
            .unwrap_or_default()
    };
    let mut out = BTreeSet::new();
    for (text, bare) in texts {
        for r in crate::expr::needs_refs(&text, bare) {
            match r {
                NeedsRef::Output { job, name } => {
                    out.insert(NeedsOutput { job, name });
                }
                NeedsRef::JobOutputs(id) => out.extend(declared(&id)),
                NeedsRef::All => job.needs.iter().for_each(|n| out.extend(declared(n))),
            }
        }
    }
    out.into_iter().collect()
}

/// Cut the plan's units into tasks. `plan` is the engine's view (the file
/// overlap is worked out on it); `specs` and `units` are the wire view of the
/// same plan.
pub(crate) fn derive_tasks(
    plan: &Plan,
    specs: &[WorkflowSpec],
    units: &[UnitSpec],
    requests: &[ShardRequest],
) -> (Vec<ExecutionTask>, Vec<ShardGroup>) {
    let mut tasks: Vec<ExecutionTask> = Vec::new();
    for (ui, u) in units.iter().enumerate() {
        let wf = &specs[u.workflow];
        let w = &plan.selected[u.workflow].wf;
        let pos: HashMap<&str, usize> = u
            .jobs
            .iter()
            .enumerate()
            .map(|(i, j)| (j.as_str(), i))
            .collect();
        let writes: Vec<BTreeSet<String>> = u
            .jobs
            .iter()
            .map(|id| {
                let j = w.jobs.iter().find(|j| &j.id == id).expect("job in plan");
                plan::writes(w, j)
            })
            .collect();
        // components of jobs that share written files
        let n = u.jobs.len();
        let mut comp: Vec<usize> = (0..n).collect();
        fn root(c: &[usize], mut i: usize) -> usize {
            while c[i] != i {
                i = c[i];
            }
            i
        }
        for a in 0..n {
            for b in (a + 1)..n {
                if !writes[a].is_disjoint(&writes[b]) {
                    let (x, y) = (root(&comp, a), root(&comp, b));
                    comp[x] = y;
                }
            }
        }
        let mut groups: Vec<Vec<usize>> = Vec::new();
        let mut seen_root: Vec<usize> = Vec::new();
        for i in 0..n {
            let r = root(&comp, i);
            match seen_root.iter().position(|x| *x == r) {
                Some(g) => groups[g].push(i),
                None => {
                    seen_root.push(r);
                    groups.push(vec![i]);
                }
            }
        }
        // Tasks run in the order of the unit (the workflow's order, with each
        // job after the jobs it needs). When cutting the unit into groups would
        // run a job earlier or later than that (a group around a job that is not
        // part of it, or groups that need each other in a cycle), the unit stays
        // one task in its own order.
        let mut merged = false;
        let in_unit_order = |groups: &[Vec<usize>], order: &[usize]| {
            order
                .iter()
                .flat_map(|&g| groups[g].iter().copied())
                .eq(0..n)
        };
        let mut ordered = order_groups(wf, u, &groups, &pos).filter(|o| in_unit_order(&groups, o));
        if ordered.is_none() {
            groups = vec![(0..n).collect()];
            merged = true;
            ordered = Some(vec![0]);
        }
        let ordered = ordered.unwrap();
        for gi in ordered {
            let g = &groups[gi];
            let ids: Vec<String> = g.iter().map(|&i| u.jobs[i].clone()).collect();
            let specs_of: Vec<&JobSpec> = ids
                .iter()
                .map(|id| wf.job(id).expect("job in plan"))
                .collect();
            let grouped_reason = (g.len() > 1).then(|| {
                if merged {
                    GroupedReason::OrderAroundSharedOutputs
                } else {
                    let mut count: BTreeMap<&String, usize> = BTreeMap::new();
                    for &i in g {
                        for f in &writes[i] {
                            *count.entry(f).or_default() += 1;
                        }
                    }
                    GroupedReason::SharedOutputs {
                        files: count
                            .into_iter()
                            .filter(|(_, c)| *c > 1)
                            .map(|(f, _)| f.clone())
                            .collect(),
                    }
                }
            });
            let base = ExecutionTask {
                schema_version: crate::wire::wire_version(),
                id: String::new(),
                workflow: wf.file.clone(),
                jobs: ids.clone(),
                nodes: vec![],
                unit: ui,
                variant: None,
                dynamic_matrix: false,
                depends_on: vec![],
                needs_outputs: vec![],
                artifacts: specs_of
                    .iter()
                    .flat_map(|j| &j.steps)
                    .filter_map(|s| artifact::needed_download(s.uses.as_deref()?, &s.with))
                    .collect::<BTreeSet<_>>()
                    .into_iter()
                    .collect(),
                workspace: WorkspacePolicy {
                    own_checkout: true,
                    shared_by_jobs: g.len() > 1,
                },
                services: specs_of
                    .iter()
                    .flat_map(|j| {
                        j.services.iter().map(|s| TaskService {
                            job: j.id.clone(),
                            id: s.id.clone(),
                            image: s.image.clone(),
                        })
                    })
                    .collect(),
                exclusive: g.iter().any(|&i| {
                    let j = w.jobs.iter().find(|j| j.id == u.jobs[i]).unwrap();
                    plan::is_exclusive(j)
                }),
                retry: RetryPolicy { max_attempts: 1 },
                verification: OutputVerification {
                    job_outputs: specs_of
                        .iter()
                        .flat_map(|j| j.outputs.iter().map(|(k, _)| format!("{}.{k}", j.id)))
                        .collect(),
                    artifacts: specs_of
                        .iter()
                        .filter(|j| j.matrix.is_none())
                        .flat_map(|j| &j.steps)
                        .filter_map(|s| {
                            artifact::required_upload(
                                s.uses.as_deref()?,
                                &s.with,
                                s.cond.as_deref().is_some_and(|c| !artifact::always_true(c))
                                    || s.continue_on_error,
                            )
                        })
                        .collect::<BTreeSet<_>>()
                        .into_iter()
                        .collect(),
                },
                grouped_reason,
                shard: None,
            };
            let external: BTreeSet<&str> = specs_of
                .iter()
                .flat_map(|j| j.needs.iter().map(String::as_str))
                .filter(|n| !ids.iter().any(|i| i == n))
                .collect();
            let needs_outputs: Vec<NeedsOutput> = specs_of
                .iter()
                .flat_map(|j| needs_outputs_of(j, wf))
                .filter(|o| external.contains(o.job.as_str()))
                .collect::<BTreeSet<_>>()
                .into_iter()
                .collect();
            let single = (g.len() == 1).then(|| specs_of[0]);
            match single.map(|j| (j, matrix_plan(j))) {
                Some((j, MatrixPlan::Static(combos))) => {
                    for (i, c) in combos.into_iter().enumerate() {
                        let id = variant_id(&wf.file, &j.id, i);
                        tasks.push(ExecutionTask {
                            id: id.clone(),
                            nodes: vec![id],
                            variant: Some(TaskVariant {
                                group: node_id(&wf.file, &j.id),
                                index: i,
                                label: crate::matrix::describe(&c),
                                values: c,
                            }),
                            needs_outputs: needs_outputs.clone(),
                            ..base.clone()
                        });
                    }
                }
                other => {
                    let dynamic = matches!(other, Some((_, MatrixPlan::Dynamic(_))));
                    let id = format!("{}::{}", wf.file, ids.join("+"));
                    let split = other
                        .as_ref()
                        .filter(|(_, m)| matches!(m, MatrixPlan::None))
                        .and_then(|(j, _)| request_for(requests, &wf.file, &j.id))
                        .filter(|r| {
                            structural_reasons(&base).is_empty()
                                && manifest_ok(r)
                                && connection_reason(r, w, ids.len()).is_none()
                        });
                    if let Some(r) = split.filter(|r| r.plan.is_split()) {
                        let count = r.plan.shards.len();
                        for s in &r.plan.shards {
                            tasks.push(ExecutionTask {
                                id: shard_task_id(&wf.file, &ids[0], s.index),
                                nodes: ids.iter().map(|j| node_id(&wf.file, j)).collect(),
                                needs_outputs: needs_outputs.clone(),
                                shard: Some(TaskShard {
                                    group: id.clone(),
                                    index: s.index,
                                    count,
                                    mode: r.plan.mode,
                                    tests: s.tests.clone(),
                                    runner: r.plan.runner.clone(),
                                    step: r.step,
                                    dir: r.dir.clone(),
                                }),
                                ..base.clone()
                            });
                        }
                        continue;
                    }
                    tasks.push(ExecutionTask {
                        id,
                        nodes: ids.iter().map(|j| node_id(&wf.file, j)).collect(),
                        dynamic_matrix: dynamic,
                        needs_outputs,
                        ..base
                    });
                }
            }
        }
    }
    // dependencies between tasks
    let mut by_job: HashMap<(String, String), Vec<String>> = HashMap::new();
    for t in &tasks {
        for j in &t.jobs {
            by_job
                .entry((t.workflow.clone(), j.clone()))
                .or_default()
                .push(t.id.clone());
        }
    }
    for t in &mut tasks {
        let wf = specs.iter().find(|w| w.file == t.workflow).unwrap();
        let mut deps = BTreeSet::new();
        for j in &t.jobs {
            for n in &wf.job(j).unwrap().needs {
                if t.jobs.contains(n) {
                    continue;
                }
                deps.extend(
                    by_job
                        .get(&(t.workflow.clone(), n.clone()))
                        .into_iter()
                        .flatten()
                        .cloned(),
                );
            }
        }
        t.depends_on = deps.into_iter().collect();
    }
    let mut shard_groups = Vec::new();
    for t in &tasks {
        let request = |t: &ExecutionTask| {
            (t.jobs.len() == 1)
                .then(|| request_for(requests, &t.workflow, &t.jobs[0]))
                .flatten()
        };
        if let Some(s) = &t.shard {
            if s.index == 0 {
                let r = request(t).expect("a shard comes from a request");
                shard_groups.push(ShardGroup {
                    id: s.group.clone(),
                    workflow: t.workflow.clone(),
                    source_jobs: t.jobs.clone(),
                    shards: tasks
                        .iter()
                        .filter(|o| o.shard.as_ref().is_some_and(|x| x.group == s.group))
                        .map(|o| o.id.clone())
                        .collect(),
                    split: true,
                    not_split: vec![],
                    aggregation: Some(OutputAggregation::TestIdentity),
                    runner: Some(r.plan.runner.clone()),
                    manifest: manifest_of(r),
                    fail_fast: r.fail_fast,
                    assignments: r.plan.shards.iter().map(|s| s.tests.clone()).collect(),
                    manifest_exact: r.plan.mode != SplitMode::Native || r.discovered.complete,
                });
            }
            continue;
        }
        let mut not_split = structural_reasons(t);
        let asked = request(t).filter(|_| t.variant.is_none());
        match asked {
            None => not_split.insert(0, NotSplit::NoRunnerAdapter),
            Some(r) => {
                if t.dynamic_matrix {
                    not_split.push(NotSplit::MatrixVariant);
                }
                not_split.extend(r.plan.not_split.iter().cloned());
                if !manifest_ok(r) {
                    not_split.push(NotSplit::ManifestMismatch);
                }
                let sel = plan.selected.iter().find(|x| x.wf.file == t.workflow);
                if let Some(why) = sel.and_then(|x| connection_reason(r, &x.wf, t.jobs.len())) {
                    not_split.push(why);
                }
            }
        }
        shard_groups.push(ShardGroup {
            id: t.id.clone(),
            workflow: t.workflow.clone(),
            source_jobs: t.jobs.clone(),
            shards: vec![t.id.clone()],
            split: false,
            not_split,
            aggregation: None,
            runner: asked.map(|r| r.plan.runner.clone()),
            manifest: vec![],
            fail_fast: false,
            assignments: vec![],
            manifest_exact: false,
        });
    }
    (tasks, shard_groups)
}

fn request_for<'a>(
    requests: &'a [ShardRequest],
    workflow: &str,
    job: &str,
) -> Option<&'a ShardRequest> {
    requests
        .iter()
        .find(|r| r.workflow == workflow && r.job == job)
}

/// What about the task itself keeps it from being split.
fn structural_reasons(t: &ExecutionTask) -> Vec<NotSplit> {
    let mut out = Vec::new();
    if t.workspace.shared_by_jobs {
        out.push(NotSplit::SharedWorkspace);
    }
    if !t.services.is_empty() {
        out.push(NotSplit::HasServices);
    }
    if t.variant.is_some() {
        out.push(NotSplit::MatrixVariant);
    }
    out
}

/// Why the step a request names cannot be replaced by a shard's command: it is
/// not one plain command of the runner the plan was made for, or it would run
/// under a shell that does not read one.
fn connection_reason(
    r: &ShardRequest,
    w: &crate::workflow::Workflow,
    jobs_in_task: usize,
) -> Option<NotSplit> {
    let job = w.jobs.iter().find(|j| j.id == r.job);
    let step = job
        .and_then(|j| j.steps.get(r.step))
        .filter(|_| jobs_in_task == 1);
    let (Some(job), Some(step)) = (job, step) else {
        return Some(NotSplit::NotConnectable);
    };
    // one plain command, run by a shell that reads it as one; a step that may
    // fail without failing the job would hide a failed shard
    let run = step.run.as_ref().filter(|_| {
        step.uses.is_none()
            && !step.continue_on_error
            && plan::step_dir(w, job, step) == r.dir
            && matches!(
                plan::resolved_shell(w, job, step).as_deref().map(str::trim),
                None | Some("bash") | Some("sh")
            )
    });
    let Some(run) = run else {
        return Some(NotSplit::NotConnectable);
    };
    // a container, a server on a port or a service the shards would share
    if crate::shard::isolation::reason_in(&w.env, job).is_some() {
        return Some(NotSplit::NoIsolation);
    }
    if crate::shard::isolation::coverage_reason(&w.env, job).is_some() {
        return Some(NotSplit::CoverageNotMerged);
    }
    let Some(parsed) = crate::shard::adapters::parse_command(run) else {
        return Some(NotSplit::NotConnectable);
    };
    if let Some(why) = parsed.blocked.first() {
        return Some(why.clone());
    }
    (parsed.runner != r.plan.runner.runner || parsed.args != r.plan.runner.config)
        .then_some(NotSplit::NotConnectable)
}

/// The shards of the request hold the tests it lists, each once.
fn manifest_ok(r: &ShardRequest) -> bool {
    r.plan.verify(&r.discovered).is_ok()
}

fn manifest_of(r: &ShardRequest) -> Vec<String> {
    r.plan
        .shards
        .iter()
        .flat_map(|s| s.tests.iter())
        .chain(
            r.discovered
                .tests
                .iter()
                .filter(|_| r.plan.mode == SplitMode::Native),
        )
        .cloned()
        .collect::<BTreeSet<_>>()
        .into_iter()
        .collect()
}

/// The groups in an order where each comes after the groups it needs, the
/// lowest first job first; `None` when they need each other in a cycle.
fn order_groups(
    wf: &WorkflowSpec,
    u: &UnitSpec,
    groups: &[Vec<usize>],
    pos: &HashMap<&str, usize>,
) -> Option<Vec<usize>> {
    let group_of = |job: &str| {
        groups
            .iter()
            .position(|g| g.iter().any(|&i| u.jobs[i] == job))
    };
    let deps: Vec<BTreeSet<usize>> = groups
        .iter()
        .enumerate()
        .map(|(gi, g)| {
            g.iter()
                .flat_map(|&i| wf.job(&u.jobs[i]).unwrap().needs.iter())
                .filter(|n| pos.contains_key(n.as_str()))
                .filter_map(|n| group_of(n))
                .filter(|d| *d != gi)
                .collect()
        })
        .collect();
    let mut done: Vec<usize> = Vec::new();
    while done.len() < groups.len() {
        let next = (0..groups.len())
            .filter(|g| !done.contains(g) && deps[*g].iter().all(|d| done.contains(d)))
            .min_by_key(|g| groups[*g][0])?;
        done.push(next);
    }
    Some(done)
}

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

    #[test]
    fn partial_unvalidated_task_view_omits_absent_local_dependency_edges() {
        let workflow = crate::workflow::parse(
            "ci.yml",
            r#"
on: push
jobs:
  producer:
    runs-on: x
    outputs:
      value: ${{ steps.make.outputs.value }}
    steps:
      - run: echo producer
  consumer:
    needs: producer
    runs-on: x
    steps:
      - run: echo '${{ needs.producer.outputs.value }}'
"#,
        )
        .unwrap();
        let event = crate::filter::Event {
            base: "main".into(),
            head: Some("feature".into()),
            changed: vec![],
            all: false,
        };
        let mut partial = plan::build(vec![workflow], &event, &[]);
        partial.units[0].jobs.retain(|job| job == "consumer");
        let specs: Vec<_> = partial
            .selected
            .iter()
            .map(WorkflowSpec::from_selected)
            .collect();
        let units: Vec<_> = partial.units.iter().map(UnitSpec::from_unit).collect();
        // Public FrozenPlan construction guarantees a closed, validated graph;
        // tests/job_dag.rs covers skipping workflows with missing needs. This
        // directly exercises the helper's existing defensive None handling
        // for a partial/unvalidated input, not a public execution contract.
        assert!(build_graph(&specs, &units).is_err());
        let (tasks, _) = derive_tasks(&partial, &specs, &units, &[]);
        assert_eq!(tasks.len(), 1);
        assert_eq!(tasks[0].jobs, ["consumer"]);
        assert!(tasks[0].depends_on.is_empty());
        assert_eq!(
            tasks[0]
                .needs_outputs
                .iter()
                .map(|o| format!("{}.{}", o.job, o.name))
                .collect::<Vec<_>>(),
            ["producer.value"]
        );
    }
}