faucet-cli 1.13.1

Config-driven CLI runner for faucet-stream pipelines (YAML / JSON, Meltano-style)
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
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
//! Runtime matrix-row selection — the composable selection model spanning
//! four issues that resolve through **one** eligibility → narrowing → parents
//! → skip formula:
//!
//! ```text
//! 1. eligible  = status gate ({mandatory, active} ∪ --status)          # #371
//! 2. narrowed  = (eligible ∩ --tag) ∪ (--select / --only by id)        # #376 / #370
//! 3. parents   = apply include_parents policy to narrowed              # #377
//! 4. run set   = parents − (--skip)                                    # #370
//! ```
//!
//! - **#370 — identity.** `--select <id>` (exact) / `--only <glob>` force-include
//!   a row *by name*, bypassing the status gate; `--skip <id|glob>` removes last.
//! - **#371 — readiness (`status`).** Each row's source carries a
//!   [`SourceStatus`] ladder. The status gate decides *eligibility*;
//!   `--status <tier>` additively widens the eligible set.
//! - **#376 — classification (`tags`).** `--tag <t>` narrows *within* the
//!   eligible set. A tag can only shrink the eligible set, never resurrect a
//!   non-ready (`available`/`draft`/`archived`) row.
//! - **#377 — `include_parents`.** The single, explicit policy that decides
//!   whether a selected row's `parent:` / `depends_on:` ancestors are pulled in.
//!   Default `off` (strict): a required ancestor missing from the run set is a
//!   hard, fail-fast error.
//!
//! Selection runs on the **expanded node list** (after `expand()`), so it never
//! alters `{name}::{row_id}` state-key derivation — bookmarks stay identical
//! across a full run and any selected subset.

use crate::config::{IncludeParents, SelectionSpec, SourceStatus};
use crate::error::{CliError, CliResult};
use crate::expand::{ExpandedNode, NodeRole};
use std::collections::{BTreeSet, HashMap, HashSet, VecDeque};

/// A fully-resolved selection request, built from CLI flags + config.
#[derive(Debug, Clone, Default)]
pub struct RunSelection {
    /// Exact row ids to force-include (bypass status/tags).
    pub select: Vec<String>,
    /// Glob patterns to force-include by id (bypass status/tags).
    pub only: Vec<String>,
    /// Row ids / globs to remove from the run set (applied last).
    pub skip: Vec<String>,
    /// Status tiers to add to the default `{mandatory, active}` eligible set.
    pub status: Vec<SourceStatus>,
    /// Tags to narrow the eligible set by (union within the list).
    pub tags: Vec<String>,
    /// Parent/dependency inclusion policy.
    pub include_parents: IncludeParents,
}

impl RunSelection {
    /// Resolve raw CLI strings + the config's `selection:` block into a typed
    /// [`RunSelection`]. Parses `--status` tiers and `--include-parents`,
    /// surfacing typed errors on unknown values. Precedence for the policy:
    /// `--include-parents` flag/env > `selection.include_parents` in config >
    /// built-in default (`off`).
    #[allow(clippy::too_many_arguments)]
    pub fn resolve(
        select: &[String],
        only: &[String],
        skip: &[String],
        status: &[String],
        tags: &[String],
        include_parents_flag: Option<&str>,
        cfg_selection: Option<&SelectionSpec>,
    ) -> CliResult<Self> {
        let status = status
            .iter()
            .map(|s| {
                SourceStatus::parse(s).ok_or_else(|| CliError::UnknownStatus {
                    value: s.clone(),
                    available: SourceStatus::ALL
                        .iter()
                        .map(|v| v.as_str().to_owned())
                        .collect(),
                })
            })
            .collect::<CliResult<Vec<_>>>()?;

        let include_parents = match include_parents_flag {
            Some(s) => IncludeParents::parse(s).ok_or_else(|| CliError::UnknownIncludeParents {
                value: s.to_owned(),
            })?,
            None => cfg_selection.map(|s| s.include_parents).unwrap_or_default(),
        };

        Ok(Self {
            select: dedup(select),
            only: dedup(only),
            skip: dedup(skip),
            status,
            tags: dedup(tags),
            include_parents,
        })
    }

    /// Build from the shared CLI [`SelectionArgs`](crate::cli::SelectionArgs)
    /// plus the config's `selection:` block.
    pub fn from_args(
        args: &crate::cli::SelectionArgs,
        cfg_selection: Option<&SelectionSpec>,
    ) -> CliResult<Self> {
        Self::resolve(
            &args.select,
            &args.only,
            &args.skip,
            &args.status,
            &args.tags,
            args.include_parents.as_deref(),
            cfg_selection,
        )
    }

    /// Whether any selector actively narrows/widens the run set (so callers
    /// like `validate` know to print a selection report). `--include-parents`
    /// alone does not count — it only governs ancestor inclusion.
    pub fn narrows(&self) -> bool {
        self.has_matrix_only_selector() || !self.status.is_empty()
    }

    /// Any row-narrowing selector present (matrix-only flags). `--status` and
    /// `--include-parents` are excluded because they are meaningful even on a
    /// single anonymous row.
    fn has_matrix_only_selector(&self) -> bool {
        !self.select.is_empty()
            || !self.only.is_empty()
            || !self.skip.is_empty()
            || !self.tags.is_empty()
    }
}

/// Run label carrying a run's canonical selection (#741), so a subset run is
/// distinguishable in `GET /v1/runs`.
pub const LABEL_SELECTION: &str = "selection";

/// The wire form of a row selection (#741) — the `selection` object every
/// run-starting surface accepts (HTTP bodies, MCP arguments, trigger files,
/// suites). Mirrors the CLI flags one for one; every field is optional, and an
/// empty object applies only the status gate.
#[derive(
    Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema,
)]
#[serde(deny_unknown_fields)]
pub struct SelectionRequest {
    /// Row ids to run, exactly (bypasses the status gate).
    #[serde(default)]
    pub select: Vec<String>,
    /// Row-id globs to run (`act*`; bypasses the status gate).
    #[serde(default)]
    pub only: Vec<String>,
    /// Row ids / globs removed last. A `mandatory` row needs an exact id.
    #[serde(default)]
    pub skip: Vec<String>,
    /// Narrow the eligible rows to those carrying any of these tags.
    #[serde(default)]
    pub tags: Vec<String>,
    /// Status tiers added to the default eligible set (`mandatory`, `active`).
    #[serde(default)]
    pub status: Vec<SourceStatus>,
    /// Whether a selected row's missing `parent:` / `depends_on:` ancestors
    /// are pulled in: `off` (default — refuse), `eligible`, or `all`.
    #[serde(default)]
    pub include_parents: Option<IncludeParents>,
}

impl SelectionRequest {
    /// Parse the CLI flags (typed errors on an unknown status tier or policy).
    pub fn from_args(args: &crate::cli::SelectionArgs) -> CliResult<Self> {
        let sel = RunSelection::from_args(args, None)?;
        let mut req = Self::from_run_selection(&sel);
        req.include_parents = args.include_parents.as_ref().map(|_| sel.include_parents);
        Ok(req)
    }

    /// The flags as a selection, `None` when no selection flag is set.
    pub fn from_flags(args: &crate::cli::SelectionArgs) -> CliResult<Option<Self>> {
        let any = !args.select.is_empty()
            || !args.only.is_empty()
            || !args.skip.is_empty()
            || !args.tags.is_empty()
            || !args.status.is_empty()
            || args.include_parents.is_some();
        if any {
            Self::from_args(args).map(Some)
        } else {
            Ok(None)
        }
    }

    /// Back to CLI flags (for handing a resolved selection to `faucet run`'s
    /// execution path).
    pub fn to_args(&self) -> crate::cli::SelectionArgs {
        crate::cli::SelectionArgs {
            select: self.select.clone(),
            only: self.only.clone(),
            skip: self.skip.clone(),
            status: self.status.iter().map(|s| s.as_str().to_string()).collect(),
            tags: self.tags.clone(),
            include_parents: self.include_parents.map(|p| p.as_str().to_string()),
        }
    }

    /// The wire form of a resolved selection (policy always explicit).
    pub fn from_run_selection(sel: &RunSelection) -> Self {
        Self {
            select: sel.select.clone(),
            only: sel.only.clone(),
            skip: sel.skip.clone(),
            tags: sel.tags.clone(),
            status: sel.status.clone(),
            include_parents: Some(sel.include_parents),
        }
    }

    /// The typed selection, with the config's `selection.include_parents` as
    /// the policy when the request does not name one.
    pub fn to_run_selection(&self, cfg: Option<&SelectionSpec>) -> RunSelection {
        let mut status = Vec::new();
        for s in &self.status {
            if !status.contains(s) {
                status.push(*s);
            }
        }
        RunSelection {
            select: dedup(&self.select),
            only: dedup(&self.only),
            skip: dedup(&self.skip),
            status,
            tags: dedup(&self.tags),
            include_parents: self
                .include_parents
                .or_else(|| cfg.map(|c| c.include_parents))
                .unwrap_or_default(),
        }
    }

    /// Whether no field is set (the status gate alone).
    pub fn is_empty(&self) -> bool {
        *self == Self::default()
    }

    /// A stable one-line form — sorted, deduplicated, fields in a fixed order:
    /// `select=contacts,deals;include_parents=eligible`, or `default` for an
    /// empty selection. The `selection` run label and the change-request
    /// fingerprint use it.
    pub fn canonical(&self) -> String {
        fn list(name: &str, items: &[String], out: &mut Vec<String>) {
            if items.is_empty() {
                return;
            }
            let set: BTreeSet<&str> = items.iter().map(String::as_str).collect();
            out.push(format!(
                "{name}={}",
                set.into_iter().collect::<Vec<_>>().join(",")
            ));
        }
        let mut parts = Vec::new();
        list("select", &self.select, &mut parts);
        list("only", &self.only, &mut parts);
        list("skip", &self.skip, &mut parts);
        list("tags", &self.tags, &mut parts);
        let status: Vec<String> = self.status.iter().map(|s| s.as_str().to_string()).collect();
        list("status", &status, &mut parts);
        if let Some(p) = self.include_parents {
            parts.push(format!("include_parents={}", p.as_str()));
        }
        if parts.is_empty() {
            "default".to_string()
        } else {
            parts.join(";")
        }
    }

    /// Apply to a loaded config's expanded rows. A topology-mode config
    /// (`pipeline.nodes`) has no rows to select, so any selection is refused
    /// rather than ignored.
    pub fn apply(
        &self,
        cfg: &crate::config::PipelineConfig,
        nodes: Vec<ExpandedNode>,
    ) -> CliResult<Vec<ExpandedNode>> {
        refuse_topology(cfg)?;
        select_nodes(
            nodes,
            &self.to_run_selection(cfg.selection.as_ref()),
            !cfg.matrix.is_empty(),
        )
    }
}

/// Why a selection on a topology-mode config is refused.
pub const TOPOLOGY_REFUSAL: &str = "this pipeline is a topology (`pipeline.nodes`) — it has no \
     matrix rows to select; run it without a selection";

/// Whether `e` is a refused selection (an unknown row / tag / status tier, an
/// empty run set, a missing ancestor, a topology) — a caller error, which the
/// control plane answers with 400.
pub fn is_selection_error(e: &CliError) -> bool {
    matches!(
        e,
        CliError::NoMatchForSelector { .. }
            | CliError::UnknownTag { .. }
            | CliError::EmptyRunSet { .. }
            | CliError::RunSetMissingAncestors { .. }
            | CliError::SelectorsWithoutMatrix { .. }
            | CliError::UnknownStatus { .. }
            | CliError::UnknownIncludeParents { .. }
    ) || matches!(e, CliError::Config(m) if m == TOPOLOGY_REFUSAL)
}

/// The refusal for a row selection on a topology-mode config.
pub fn refuse_topology(cfg: &crate::config::PipelineConfig) -> CliResult<()> {
    if crate::topology::is_topology(cfg) {
        return Err(CliError::Config(TOPOLOGY_REFUSAL.into()));
    }
    Ok(())
}

/// Apply `sel` to `nodes` (expanded, in BFS order) and return the running
/// subset, order-preserved. Errors on unknown tokens, an empty run set, or a
/// dependency violation under the active `include_parents` policy.
///
/// `has_matrix` is `false` for the single anonymous invocation (no `matrix:`);
/// matrix-only selectors (`--select`/`--only`/`--skip`/`--tag`) are then a hard
/// error. The status gate still applies to the lone row.
pub fn select_nodes(
    nodes: Vec<ExpandedNode>,
    sel: &RunSelection,
    has_matrix: bool,
) -> CliResult<Vec<ExpandedNode>> {
    let resolution = resolve(&nodes, sel, has_matrix);
    if let Some(e) = resolution.error {
        return Err(e);
    }
    let run: HashSet<&str> = resolution.run_set.iter().map(String::as_str).collect();
    Ok(nodes
        .into_iter()
        .filter(|n| run.contains(n.id.as_str()))
        .collect())
}

/// Why one row is — or is not — in a resolved run set (#741).
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)]
#[serde(tag = "decision", rename_all = "snake_case")]
pub enum RowDecision {
    /// Picked by the selection itself: `by` is `default` (the status gate with
    /// no narrowing selector), `select`, `only`, or `tag`.
    Selected { by: &'static str },
    /// Added as a required ancestor of `because` under `include_parents`.
    PulledIn { because: String },
    /// Not in the run set, and nothing needs it.
    Excluded { reason: String },
    /// Needed by a row in the run set but not allowed into it — the reason the
    /// selection is refused.
    Blocked { reason: String },
}

impl RowDecision {
    /// Whether the row runs.
    pub fn runs(&self) -> bool {
        matches!(self, Self::Selected { .. } | Self::PulledIn { .. })
    }
}

/// A selection resolved against a node list without running anything: the run
/// set in execution order, each row's decision, and the error a trigger with
/// this selection would return.
#[derive(Debug)]
pub struct Resolution {
    /// Row ids that run, in execution order (dependency level, then declared
    /// order). Empty when `error` is set.
    pub run_set: Vec<String>,
    /// One decision per row. Empty when the selection names an unknown row or
    /// tag (nothing is decided before the typo is fixed).
    pub decisions: HashMap<String, RowDecision>,
    pub error: Option<CliError>,
}

/// Resolve `sel` against `nodes` — the one implementation behind
/// [`select_nodes`] and the rows API's dry-run resolve.
pub fn resolve(nodes: &[ExpandedNode], sel: &RunSelection, has_matrix: bool) -> Resolution {
    let fail = |error: CliError, decisions: HashMap<String, RowDecision>| Resolution {
        run_set: Vec::new(),
        decisions,
        error: Some(error),
    };
    if !has_matrix && sel.has_matrix_only_selector() {
        let mut flags = Vec::new();
        if !sel.select.is_empty() {
            flags.push("--select");
        }
        if !sel.only.is_empty() {
            flags.push("--only");
        }
        if !sel.skip.is_empty() {
            flags.push("--skip");
        }
        if !sel.tags.is_empty() {
            flags.push("--tag");
        }
        return fail(
            CliError::SelectorsWithoutMatrix {
                flags: flags.join(", "),
            },
            HashMap::new(),
        );
    }

    // Typo protection: every identity/skip token must match ≥1 row id, and
    // every requested tag must be present on some row. Checked against the
    // full node set (before any gating), so a typo is caught regardless of
    // status.
    for token in &sel.select {
        if !nodes.iter().any(|n| &n.id == token) {
            return fail(
                CliError::NoMatchForSelector {
                    flag: "--select",
                    token: token.clone(),
                    available: all_ids(nodes),
                },
                HashMap::new(),
            );
        }
    }
    for token in sel.only.iter().chain(sel.skip.iter()) {
        let flag = if sel.only.contains(token) {
            "--only"
        } else {
            "--skip"
        };
        if !nodes.iter().any(|n| token_matches(token, &n.id)) {
            return fail(
                CliError::NoMatchForSelector {
                    flag,
                    token: token.clone(),
                    available: all_ids(nodes),
                },
                HashMap::new(),
            );
        }
    }
    if !sel.tags.is_empty() {
        let present: BTreeSet<&str> = nodes
            .iter()
            .flat_map(|n| n.tags.iter().map(String::as_str))
            .collect();
        for tag in &sel.tags {
            if !present.contains(tag.as_str()) {
                return fail(
                    CliError::UnknownTag {
                        tag: tag.clone(),
                        available: present.iter().map(|s| (*s).to_owned()).collect(),
                    },
                    HashMap::new(),
                );
            }
        }
    }

    // Effective status set = {mandatory, active} ∪ --status.
    let mut active_status: HashSet<SourceStatus> =
        HashSet::from([SourceStatus::Mandatory, SourceStatus::Active]);
    active_status.extend(sel.status.iter().copied());

    let has_identity = !sel.select.is_empty() || !sel.only.is_empty();
    let has_tag = !sel.tags.is_empty();

    let is_eligible = |n: &ExpandedNode| active_status.contains(&n.status);
    let by_select = |n: &ExpandedNode| sel.select.iter().any(|id| id == &n.id);
    let by_only = |n: &ExpandedNode| sel.only.iter().any(|g| token_matches(g, &n.id));
    let matches_tag = |n: &ExpandedNode| sel.tags.iter().any(|t| n.tags.iter().any(|nt| nt == t));

    // Stage 1 + 2: eligibility → narrowing.
    let mut decisions: HashMap<String, RowDecision> = HashMap::new();
    let mut run: HashSet<String> = HashSet::new();
    for n in nodes {
        let decision = if !has_identity && !has_tag {
            if is_eligible(n) {
                RowDecision::Selected { by: "default" }
            } else {
                RowDecision::Excluded {
                    reason: parked_reason(n.status),
                }
            }
        } else if has_identity && by_select(n) {
            RowDecision::Selected { by: "select" }
        } else if has_identity && by_only(n) {
            RowDecision::Selected { by: "only" }
        } else if has_tag && matches_tag(n) && is_eligible(n) {
            RowDecision::Selected { by: "tag" }
        } else if has_tag && matches_tag(n) {
            RowDecision::Excluded {
                reason: format!(
                    "tagged, but {} — a tag never resurrects a parked row",
                    parked_reason(n.status)
                ),
            }
        } else {
            RowDecision::Excluded {
                reason: "not selected".to_string(),
            }
        };
        if decision.runs() {
            run.insert(n.id.clone());
        }
        decisions.insert(n.id.clone(), decision);
    }

    // Stage 3: parent / dependency closure under the include_parents policy.
    let node_by_id: HashMap<&str, &ExpandedNode> =
        nodes.iter().map(|n| (n.id.as_str(), n)).collect();
    if let Err(e) = apply_parent_policy(
        &node_by_id,
        &active_status,
        sel.include_parents,
        &mut run,
        &mut decisions,
    ) {
        return fail(e, decisions);
    }

    // Stage 4: skip (applied last). A `mandatory` row is removable only by an
    // exact `--skip <id>`, never by a glob.
    for n in nodes {
        if !run.contains(&n.id) {
            continue;
        }
        let mandatory = n.status == SourceStatus::Mandatory;
        if let Some(tok) = sel
            .skip
            .iter()
            .find(|tok| skip_matches(tok, &n.id, mandatory))
        {
            run.remove(&n.id);
            decisions.insert(
                n.id.clone(),
                RowDecision::Excluded {
                    reason: format!("removed by skip `{tok}`"),
                },
            );
        }
    }

    // Post-skip integrity: skipping a row that a surviving row structurally
    // depends on would orphan the dependent (a child can't fan out without its
    // parent). Fail fast rather than run a broken graph.
    let mut orphans: Vec<String> = Vec::new();
    for n in nodes {
        if !run.contains(&n.id) {
            continue;
        }
        for (anc, kind) in required_ancestors(n) {
            if !run.contains(&anc) {
                orphans.push(format!("{} → {anc} ({kind})", n.id));
                decisions.insert(
                    n.id.clone(),
                    RowDecision::Blocked {
                        reason: format!("its {kind} `{anc}` was removed from the run set"),
                    },
                );
            }
        }
    }
    if !orphans.is_empty() {
        orphans.sort();
        orphans.dedup();
        return fail(
            CliError::RunSetMissingAncestors {
                pairs: orphans,
                policy: sel.include_parents.as_str(),
            },
            decisions,
        );
    }

    if run.is_empty() {
        let rows = nodes
            .iter()
            .map(|n| format!("{} [{}]", n.id, n.status.as_str()))
            .collect();
        return fail(CliError::EmptyRunSet { rows }, decisions);
    }

    let depths = execution_depths(nodes);
    let mut run_set: Vec<&ExpandedNode> = nodes.iter().filter(|n| run.contains(&n.id)).collect();
    run_set.sort_by_key(|n| depths.get(n.id.as_str()).copied().unwrap_or(0));
    Resolution {
        run_set: run_set.into_iter().map(|n| n.id.clone()).collect(),
        decisions,
        error: None,
    }
}

fn parked_reason(status: SourceStatus) -> String {
    format!(
        "status `{}` is not in the default eligible set (mandatory, active)",
        status.as_str()
    )
}

/// Each row's execution level: `0` for a row with no `parent:` / `depends_on:`
/// edge, else one more than its deepest ancestor. Rows at the same level may
/// run together.
pub fn execution_depths(nodes: &[ExpandedNode]) -> HashMap<&str, usize> {
    let by_id: HashMap<&str, &ExpandedNode> = nodes.iter().map(|n| (n.id.as_str(), n)).collect();
    fn depth<'a>(
        id: &'a str,
        by_id: &HashMap<&'a str, &'a ExpandedNode>,
        memo: &mut HashMap<&'a str, usize>,
        guard: usize,
    ) -> usize {
        if let Some(d) = memo.get(id) {
            return *d;
        }
        let d = match by_id.get(id) {
            Some(n) if guard < by_id.len() => required_ancestors(n)
                .iter()
                .filter_map(|(a, _)| by_id.get_key_value(a.as_str()).map(|(k, _)| *k))
                .map(|a| depth(a, by_id, memo, guard + 1) + 1)
                .max()
                .unwrap_or(0),
            _ => 0,
        };
        memo.insert(id, d);
        d
    }
    let mut memo = HashMap::new();
    for n in nodes {
        depth(n.id.as_str(), &by_id, &mut memo, 0);
    }
    memo
}

/// Walk the `parent:` / `depends_on:` ancestor closure of the current run set,
/// adding or rejecting ancestors per the policy. Collects **every** offending
/// pair (transitively) before erroring.
fn apply_parent_policy(
    node_by_id: &HashMap<&str, &ExpandedNode>,
    active_status: &HashSet<SourceStatus>,
    policy: IncludeParents,
    run: &mut HashSet<String>,
    decisions: &mut HashMap<String, RowDecision>,
) -> CliResult<()> {
    let mut violations: Vec<String> = Vec::new();
    let mut queue: VecDeque<String> = run.iter().cloned().collect();
    while let Some(id) = queue.pop_front() {
        // `id` is always a real node (run set only ever holds known ids).
        let node = match node_by_id.get(id.as_str()) {
            Some(n) => *n,
            None => continue,
        };
        for (anc, kind) in required_ancestors(node) {
            if run.contains(&anc) {
                continue;
            }
            // Ancestor id validity was proven at expand time.
            let anc_status = node_by_id.get(anc.as_str()).map(|n| n.status);
            let eligible = anc_status
                .map(|s| active_status.contains(&s))
                .unwrap_or(false);
            let pulled = |decisions: &mut HashMap<String, RowDecision>| {
                decisions.insert(
                    anc.clone(),
                    RowDecision::PulledIn {
                        because: id.clone(),
                    },
                );
            };
            match policy {
                IncludeParents::Off => {
                    violations.push(format!("{id} → {anc} ({kind})"));
                    decisions.insert(
                        anc.clone(),
                        RowDecision::Blocked {
                            reason: format!(
                                "required by `{id}` ({kind}) but not selected — include_parents is \
                                 off; select it, or use include_parents: eligible / all"
                            ),
                        },
                    );
                }
                IncludeParents::Eligible => {
                    if eligible {
                        if run.insert(anc.clone()) {
                            tracing::info!(
                                dependent = %id, ancestor = %anc, edge = kind,
                                "include_parents=eligible: auto-included required ancestor"
                            );
                            pulled(decisions);
                            queue.push_back(anc);
                        }
                    } else {
                        violations.push(format!("{id} → {anc} ({kind}, parked)"));
                        decisions.insert(
                            anc.clone(),
                            RowDecision::Blocked {
                                reason: format!(
                                    "required by `{id}` ({kind}) but parked (status `{}`) — \
                                     include_parents: eligible never pulls in a parked row; use \
                                     all, or select it by id",
                                    anc_status.map(SourceStatus::as_str).unwrap_or("unknown")
                                ),
                            },
                        );
                    }
                }
                IncludeParents::All => {
                    if run.insert(anc.clone()) {
                        if eligible {
                            tracing::info!(
                                dependent = %id, ancestor = %anc, edge = kind,
                                "include_parents=all: auto-included required ancestor"
                            );
                        } else {
                            tracing::warn!(
                                dependent = %id, ancestor = %anc, edge = kind,
                                "include_parents=all: pulling a parked ancestor into the run set"
                            );
                        }
                        pulled(decisions);
                        queue.push_back(anc);
                    }
                }
            }
        }
    }
    if !violations.is_empty() {
        violations.sort();
        violations.dedup();
        return Err(CliError::RunSetMissingAncestors {
            pairs: violations,
            policy: policy.as_str(),
        });
    }
    Ok(())
}

/// The `parent:` + `depends_on:` edges of `node` — the "required ancestors" a
/// run-set row cannot execute correctly without.
fn required_ancestors(node: &ExpandedNode) -> Vec<(String, &'static str)> {
    let mut out = Vec::new();
    if let NodeRole::Child { parent_id, .. } = &node.role {
        out.push((parent_id.clone(), "parent"));
    }
    for d in &node.depends_on {
        out.push((d.clone(), "depends_on"));
    }
    out
}

fn all_ids(nodes: &[ExpandedNode]) -> Vec<String> {
    nodes.iter().map(|n| n.id.clone()).collect()
}

/// Dedup a token list, preserving first-seen order.
fn dedup(items: &[String]) -> Vec<String> {
    let mut seen = HashSet::new();
    let mut out = Vec::new();
    for it in items {
        if seen.insert(it.clone()) {
            out.push(it.clone());
        }
    }
    out
}

/// Whether a `--skip` token removes `id`. A glob token never removes a
/// `mandatory` row; an exact-id token removes any row (including mandatory).
fn skip_matches(token: &str, id: &str, mandatory: bool) -> bool {
    if has_glob(token) {
        !mandatory && glob_match(token, id)
    } else {
        token == id
    }
}

/// Whether a token (exact id or glob) matches `id`.
fn token_matches(token: &str, id: &str) -> bool {
    if has_glob(token) {
        glob_match(token, id)
    } else {
        token == id
    }
}

fn has_glob(s: &str) -> bool {
    s.contains('*') || s.contains('?')
}

/// Minimal `*` (any run, incl. empty) / `?` (exactly one char) glob matcher.
/// Sufficient for row-id selection; no character classes or escaping.
pub(crate) fn glob_match(pattern: &str, text: &str) -> bool {
    let p: Vec<char> = pattern.chars().collect();
    let t: Vec<char> = text.chars().collect();
    // Iterative backtracking match.
    let (mut pi, mut ti) = (0usize, 0usize);
    let (mut star, mut mark) = (None::<usize>, 0usize);
    while ti < t.len() {
        if pi < p.len() && (p[pi] == '?' || p[pi] == t[ti]) {
            pi += 1;
            ti += 1;
        } else if pi < p.len() && p[pi] == '*' {
            star = Some(pi);
            mark = ti;
            pi += 1;
        } else if let Some(s) = star {
            pi = s + 1;
            mark += 1;
            ti = mark;
        } else {
            return false;
        }
    }
    while pi < p.len() && p[pi] == '*' {
        pi += 1;
    }
    pi == p.len()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::parse_with_extension;
    use crate::expand::expand;

    /// Build expanded nodes from YAML for selection tests.
    fn nodes(yaml: &str) -> Vec<ExpandedNode> {
        expand(&parse_with_extension(yaml, "yaml").unwrap()).unwrap()
    }

    fn ids(nodes: &[ExpandedNode]) -> Vec<String> {
        let mut v: Vec<String> = nodes.iter().map(|n| n.id.clone()).collect();
        v.sort();
        v
    }

    fn sel() -> RunSelection {
        RunSelection::default()
    }

    /// A HiBob-style multi-endpoint matrix used by most tests.
    const HIBOB: &str = r#"
version: 1
pipeline:
  sources:
    hibob: { type: rest, config: { base_url: https://api.hibob.com } }
  sinks:
    wh: { type: jsonl, config: { path: ./o } }
matrix:
  - id: people
    source: { ref: hibob, status: active, config: { path: /people } }
    sink: { ref: wh }
    tags: [core, daily]
  - id: payroll
    source: { ref: hibob, status: mandatory, config: { path: /payroll } }
    sink: { ref: wh }
    tags: [finance]
  - id: audit
    source: { ref: hibob, status: available, config: { path: /audit } }
    sink: { ref: wh }
    tags: [finance]
  - id: beta
    source: { ref: hibob, status: draft, config: { path: /beta } }
    sink: { ref: wh }
"#;

    #[test]
    fn glob_matches_star_and_question() {
        assert!(glob_match("timeoff_*", "timeoff_requests"));
        assert!(glob_match("timeoff_*", "timeoff_"));
        assert!(!glob_match("timeoff_*", "people"));
        assert!(glob_match("a?c", "abc"));
        assert!(!glob_match("a?c", "ac"));
        assert!(glob_match("*", "anything"));
        assert!(glob_match("p*e", "people"));
        assert!(!glob_match("p*e", "payroll"));
    }

    #[test]
    fn bare_run_includes_mandatory_and_active_only() {
        let out = select_nodes(nodes(HIBOB), &sel(), true).unwrap();
        assert_eq!(ids(&out), vec!["payroll", "people"]);
    }

    #[test]
    fn status_widens_eligible_set_additively() {
        let s = RunSelection {
            status: vec![SourceStatus::Available],
            ..sel()
        };
        let out = select_nodes(nodes(HIBOB), &s, true).unwrap();
        assert_eq!(ids(&out), vec!["audit", "payroll", "people"]);
    }

    #[test]
    fn select_by_id_bypasses_status_gate() {
        // `beta` is draft (parked) but explicitly selected → runs anyway.
        let s = RunSelection {
            select: vec!["beta".into()],
            ..sel()
        };
        let out = select_nodes(nodes(HIBOB), &s, true).unwrap();
        assert_eq!(ids(&out), vec!["beta"]);
    }

    #[test]
    fn only_glob_selects_subset() {
        let s = RunSelection {
            only: vec!["p*".into()],
            ..sel()
        };
        let out = select_nodes(nodes(HIBOB), &s, true).unwrap();
        // p* matches people + payroll (both identity-selected, status bypassed).
        assert_eq!(ids(&out), vec!["payroll", "people"]);
    }

    #[test]
    fn tag_narrows_within_eligible_only() {
        // `finance` tags payroll (mandatory, eligible) + audit (available, NOT
        // eligible). Bare --tag finance keeps only the eligible one.
        let s = RunSelection {
            tags: vec!["finance".into()],
            ..sel()
        };
        let out = select_nodes(nodes(HIBOB), &s, true).unwrap();
        assert_eq!(ids(&out), vec!["payroll"]);
    }

    #[test]
    fn tag_plus_status_resurrects_parked_row() {
        let s = RunSelection {
            tags: vec!["finance".into()],
            status: vec![SourceStatus::Available],
            ..sel()
        };
        let out = select_nodes(nodes(HIBOB), &s, true).unwrap();
        assert_eq!(ids(&out), vec!["audit", "payroll"]);
    }

    #[test]
    fn skip_removes_after_selection() {
        let s = RunSelection {
            status: vec![SourceStatus::Available],
            skip: vec!["audit".into()],
            ..sel()
        };
        let out = select_nodes(nodes(HIBOB), &s, true).unwrap();
        assert_eq!(ids(&out), vec!["payroll", "people"]);
    }

    #[test]
    fn mandatory_survives_glob_skip_but_not_exact_skip() {
        // A glob skip cannot drop the mandatory `payroll` row…
        let s = RunSelection {
            skip: vec!["p*".into()],
            ..sel()
        };
        let out = select_nodes(nodes(HIBOB), &s, true).unwrap();
        assert_eq!(ids(&out), vec!["payroll"]);
        // …but an exact-id skip can.
        let s = RunSelection {
            select: vec!["payroll".into()],
            skip: vec!["payroll".into()],
            ..sel()
        };
        let err = select_nodes(nodes(HIBOB), &s, true).unwrap_err();
        assert!(matches!(err, CliError::EmptyRunSet { .. }), "got {err:?}");
    }

    #[test]
    fn unknown_select_token_errors_with_available() {
        let s = RunSelection {
            select: vec!["peeple".into()],
            ..sel()
        };
        match select_nodes(nodes(HIBOB), &s, true).unwrap_err() {
            CliError::NoMatchForSelector {
                flag,
                token,
                available,
            } => {
                assert_eq!(flag, "--select");
                assert_eq!(token, "peeple");
                assert!(available.contains(&"people".to_string()));
            }
            other => panic!("expected NoMatchForSelector, got {other:?}"),
        }
    }

    #[test]
    fn unknown_tag_errors() {
        let s = RunSelection {
            tags: vec!["nope".into()],
            ..sel()
        };
        assert!(matches!(
            select_nodes(nodes(HIBOB), &s, true).unwrap_err(),
            CliError::UnknownTag { .. }
        ));
    }

    #[test]
    fn empty_run_set_errors_when_all_parked() {
        let yaml = r#"
version: 1
pipeline:
  sources:
    api: { type: rest, config: { base_url: https://x } }
  sinks:
    wh: { type: jsonl, config: { path: ./o } }
matrix:
  - id: a
    source: { ref: api, status: available }
    sink: { ref: wh }
  - id: b
    source: { ref: api, status: draft }
    sink: { ref: wh }
"#;
        assert!(matches!(
            select_nodes(nodes(yaml), &sel(), true).unwrap_err(),
            CliError::EmptyRunSet { .. }
        ));
    }

    const DEPS: &str = r#"
version: 1
pipeline:
  sources:
    api: { type: rest, config: { base_url: https://x } }
  sinks:
    wh: { type: jsonl, config: { path: ./o } }
matrix:
  - id: dims
    source: { ref: api, status: active }
    sink: { ref: wh }
    tags: [core]
  - id: facts
    source: { ref: api, status: active }
    sink: { ref: wh }
    tags: [finance]
    depends_on: [dims]
"#;

    #[test]
    fn include_parents_off_errors_on_missing_ancestor() {
        // Selecting only `facts` (via tag) drops its `depends_on: dims`.
        let s = RunSelection {
            tags: vec!["finance".into()],
            include_parents: IncludeParents::Off,
            ..sel()
        };
        match select_nodes(nodes(DEPS), &s, true).unwrap_err() {
            CliError::RunSetMissingAncestors { pairs, policy } => {
                assert_eq!(policy, "off");
                assert!(
                    pairs
                        .iter()
                        .any(|p| p.contains("facts") && p.contains("dims"))
                );
            }
            other => panic!("expected RunSetMissingAncestors, got {other:?}"),
        }
    }

    #[test]
    fn include_parents_eligible_pulls_in_active_ancestor() {
        let s = RunSelection {
            tags: vec!["finance".into()],
            include_parents: IncludeParents::Eligible,
            ..sel()
        };
        let out = select_nodes(nodes(DEPS), &s, true).unwrap();
        assert_eq!(ids(&out), vec!["dims", "facts"]);
    }

    #[test]
    fn include_parents_eligible_errors_on_parked_ancestor() {
        let yaml = r#"
version: 1
pipeline:
  sources:
    api: { type: rest, config: { base_url: https://x } }
  sinks:
    wh: { type: jsonl, config: { path: ./o } }
matrix:
  - id: dims
    source: { ref: api, status: available }
    sink: { ref: wh }
  - id: facts
    source: { ref: api, status: active }
    sink: { ref: wh }
    depends_on: [dims]
"#;
        let s = RunSelection {
            select: vec!["facts".into()],
            include_parents: IncludeParents::Eligible,
            ..sel()
        };
        assert!(matches!(
            select_nodes(nodes(yaml), &s, true).unwrap_err(),
            CliError::RunSetMissingAncestors { .. }
        ));
    }

    #[test]
    fn include_parents_all_pulls_in_parked_ancestor() {
        let yaml = r#"
version: 1
pipeline:
  sources:
    api: { type: rest, config: { base_url: https://x } }
  sinks:
    wh: { type: jsonl, config: { path: ./o } }
matrix:
  - id: dims
    source: { ref: api, status: draft }
    sink: { ref: wh }
  - id: facts
    source: { ref: api, status: active }
    sink: { ref: wh }
    depends_on: [dims]
"#;
        let s = RunSelection {
            select: vec!["facts".into()],
            include_parents: IncludeParents::All,
            ..sel()
        };
        let out = select_nodes(nodes(yaml), &s, true).unwrap();
        assert_eq!(ids(&out), vec!["dims", "facts"]);
    }

    #[test]
    fn select_ancestor_by_id_satisfies_dependency() {
        let s = RunSelection {
            select: vec!["facts".into(), "dims".into()],
            include_parents: IncludeParents::Off,
            ..sel()
        };
        let out = select_nodes(nodes(DEPS), &s, true).unwrap();
        assert_eq!(ids(&out), vec!["dims", "facts"]);
    }

    #[test]
    fn parent_edge_closure_respected() {
        // `posts` is a per-record child of `users`; selecting only `posts` must
        // pull in `users` under `eligible`.
        let yaml = r#"
version: 1
pipeline:
  sources:
    api: { type: rest, config: { base_url: https://x } }
  sinks:
    wh: { type: jsonl, config: { path: ./o, append: true } }
matrix:
  - id: users
    source: { ref: api, status: active }
    sink: { ref: wh }
  - id: posts
    parent: users
    source: { ref: api, status: active, config: { path: "/u/${users.id}/posts" } }
    sink: { ref: wh }
"#;
        let s = RunSelection {
            select: vec!["posts".into()],
            include_parents: IncludeParents::Eligible,
            ..sel()
        };
        let out = select_nodes(nodes(yaml), &s, true).unwrap();
        assert_eq!(ids(&out), vec!["posts", "users"]);
    }

    #[test]
    fn matrix_only_selectors_rejected_without_matrix() {
        let yaml = r#"
version: 1
pipeline:
  source: { type: rest, config: { base_url: https://x } }
  sink:   { type: jsonl, config: { path: ./o } }
"#;
        let s = RunSelection {
            select: vec!["row-0".into()],
            ..sel()
        };
        assert!(matches!(
            select_nodes(nodes(yaml), &s, false).unwrap_err(),
            CliError::SelectorsWithoutMatrix { .. }
        ));
    }

    #[test]
    fn no_selectors_keeps_plain_config_unchanged() {
        // A matrix with no status/tags anywhere must run every row (no
        // behaviour change for pre-selection configs).
        let yaml = r#"
version: 1
pipeline:
  source: { type: rest, config: { base_url: https://x } }
  sink:   { type: jsonl, config: { path: ./o } }
matrix:
  - { id: a }
  - { id: b }
  - { id: c }
"#;
        let out = select_nodes(nodes(yaml), &sel(), true).unwrap();
        assert_eq!(ids(&out), vec!["a", "b", "c"]);
    }

    #[test]
    fn selection_request_canonical_is_stable() {
        let a = SelectionRequest {
            select: vec!["deals".into(), "contacts".into(), "deals".into()],
            status: vec![SourceStatus::Available],
            tags: vec!["b".into(), "a".into()],
            include_parents: Some(IncludeParents::Eligible),
            ..Default::default()
        };
        assert_eq!(
            a.canonical(),
            "select=contacts,deals;tags=a,b;status=available;include_parents=eligible"
        );
        assert_eq!(SelectionRequest::default().canonical(), "default");
        assert!(SelectionRequest::default().is_empty());
        let b = SelectionRequest {
            only: vec!["a*".into()],
            skip: vec!["x".into()],
            ..Default::default()
        };
        assert_eq!(b.canonical(), "only=a*;skip=x");
    }

    #[test]
    fn selection_request_policy_falls_back_to_config() {
        let req = SelectionRequest {
            select: vec!["a".into(), "a".into()],
            status: vec![SourceStatus::Draft, SourceStatus::Draft],
            ..Default::default()
        };
        let cfg = SelectionSpec {
            include_parents: IncludeParents::All,
        };
        let sel = req.to_run_selection(Some(&cfg));
        assert_eq!(sel.select, vec!["a"]);
        assert_eq!(sel.status, vec![SourceStatus::Draft]);
        assert_eq!(sel.include_parents, IncludeParents::All);
        assert_eq!(
            req.to_run_selection(None).include_parents,
            IncludeParents::Off
        );
        let explicit = SelectionRequest {
            include_parents: Some(IncludeParents::Eligible),
            ..Default::default()
        };
        assert_eq!(
            explicit.to_run_selection(Some(&cfg)).include_parents,
            IncludeParents::Eligible
        );
    }

    #[test]
    fn selection_request_round_trips_cli_flags() {
        assert!(
            SelectionRequest::from_flags(&crate::cli::SelectionArgs::default())
                .unwrap()
                .is_none()
        );
        let args = crate::cli::SelectionArgs {
            select: vec!["a".into()],
            status: vec!["draft".into()],
            include_parents: Some("all".into()),
            ..Default::default()
        };
        let req = SelectionRequest::from_flags(&args).unwrap().unwrap();
        assert_eq!(req.include_parents, Some(IncludeParents::All));
        let back = req.to_args();
        assert_eq!(back.status, vec!["draft"]);
        assert_eq!(back.include_parents.as_deref(), Some("all"));
        let no_policy = crate::cli::SelectionArgs {
            tags: vec!["t".into()],
            ..Default::default()
        };
        let req = SelectionRequest::from_flags(&no_policy).unwrap().unwrap();
        assert_eq!(req.include_parents, None);
        let bad = crate::cli::SelectionArgs {
            status: vec!["nope".into()],
            ..Default::default()
        };
        assert!(SelectionRequest::from_flags(&bad).is_err());
        let json: SelectionRequest = serde_json::from_value(
            serde_json::json!({"select": ["a"], "include_parents": "eligible"}),
        )
        .unwrap();
        assert_eq!(json.include_parents, Some(IncludeParents::Eligible));
        assert!(
            serde_json::from_value::<SelectionRequest>(serde_json::json!({"rows": ["a"]})).is_err()
        );
    }

    #[test]
    fn apply_refuses_topology_and_selects_matrix_rows() {
        let topo = parse_with_extension(
            "version: 1\nname: t\npipeline:\n  sources:\n    s: { type: csv, config: { path: a.csv } }\n  sinks:\n    o: { type: jsonl, config: { path: o.jsonl } }\n  nodes:\n    src: { kind: source, ref: s }\n    w: { kind: sink, ref: o }\n  edges:\n    - { from: src, to: w }\n",
            "yaml",
        )
        .unwrap();
        let req = SelectionRequest {
            select: vec!["src".into()],
            ..Default::default()
        };
        let err = req.apply(&topo, Vec::new()).unwrap_err();
        assert!(is_selection_error(&err));
        let cfg = parse_with_extension(HIBOB, "yaml").unwrap();
        let req = SelectionRequest {
            select: vec!["beta".into()],
            ..Default::default()
        };
        let out = req.apply(&cfg, expand(&cfg).unwrap()).unwrap();
        assert_eq!(ids(&out), vec!["beta"]);
        assert!(!is_selection_error(&CliError::Config("x".into())));
        assert!(is_selection_error(&CliError::EmptyRunSet { rows: vec![] }));
    }

    #[test]
    fn resolve_explains_every_row() {
        let s = RunSelection {
            tags: vec!["finance".into()],
            skip: vec!["payroll".into()],
            select: vec!["people".into()],
            ..sel()
        };
        let r = resolve(&nodes(HIBOB), &s, true);
        assert!(r.error.is_none());
        assert_eq!(r.run_set, vec!["people"]);
        assert_eq!(
            r.decisions["people"],
            RowDecision::Selected { by: "select" }
        );
        assert!(
            matches!(&r.decisions["payroll"], RowDecision::Excluded { reason } if reason.contains("skip"))
        );
        assert!(
            matches!(&r.decisions["audit"], RowDecision::Excluded { reason } if reason.contains("tagged"))
        );
        assert!(
            matches!(&r.decisions["beta"], RowDecision::Excluded { reason } if reason == "not selected")
        );
        let bare = resolve(&nodes(HIBOB), &sel(), true);
        assert!(
            matches!(&bare.decisions["beta"], RowDecision::Excluded { reason } if reason.contains("draft"))
        );
        assert_eq!(
            bare.decisions["people"],
            RowDecision::Selected { by: "default" }
        );
        let by_only = resolve(
            &nodes(HIBOB),
            &RunSelection {
                only: vec!["pay*".into()],
                ..sel()
            },
            true,
        );
        assert_eq!(
            by_only.decisions["payroll"],
            RowDecision::Selected { by: "only" }
        );
        assert!(
            RowDecision::PulledIn {
                because: "x".into()
            }
            .runs()
        );
        assert!(!RowDecision::Blocked { reason: "x".into() }.runs());
    }

    #[test]
    fn resolve_orders_the_run_set_by_dependency_level() {
        let s = RunSelection {
            select: vec!["facts".into()],
            include_parents: IncludeParents::Eligible,
            ..sel()
        };
        let n = nodes(DEPS);
        let r = resolve(&n, &s, true);
        assert_eq!(r.run_set, vec!["dims", "facts"]);
        assert_eq!(
            r.decisions["dims"],
            RowDecision::PulledIn {
                because: "facts".into()
            }
        );
        let depths = execution_depths(&n);
        assert_eq!((depths["dims"], depths["facts"]), (0, 1));
        let orphan = resolve(
            &n,
            &RunSelection {
                select: vec!["facts".into(), "dims".into()],
                skip: vec!["dims".into()],
                ..sel()
            },
            true,
        );
        assert!(orphan.error.is_some());
        assert!(matches!(
            &orphan.decisions["facts"],
            RowDecision::Blocked { .. }
        ));
    }
}