oxo-flow-cli 0.28.0

CLI for the oxo-flow bioinformatics pipeline engine
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
use anyhow::{Context, Result};
use colored::Colorize;
use oxo_flow_core::config::WorkflowConfig;
use oxo_flow_core::dag::WorkflowDag;
use std::collections::HashMap;
use std::path::{Path, PathBuf};

use crate::commands::print_banner;

/// `"1 rule"` / `"2 rules"` — the summary counts read "1 rules" before the
/// CLI grew pluralisation (audit P4-1d). Now a thin alias for the shared
/// core helper (reuse-never-retype).
fn plural(count: usize, singular: &str, plural_form: &str) -> String {
    oxo_flow_core::plural(count, singular, plural_form)
}

pub async fn validate_command(
    workflow: PathBuf,
    as_include: bool,
    json: bool,
    ai: bool,
) -> Result<()> {
    // AI: auto-detect from workflow [ai] or explicit --ai flag. Suppressed
    // under --json (#539): the narrative is human-only, and the paid call's
    // output is useless to a JSON consumer.
    if !json
        && let Some(provider) = crate::commands::ai_template::try_resolve_ai(Some(&workflow), ai)
    {
        crate::commands::ai_check::analyze_workflow(&workflow, &provider, "validate", "").await?;
        println!();
    }

    let config_res = WorkflowConfig::from_file(&workflow);
    match config_res {
        Ok(cfg) => {
            if cfg.rules.is_empty() {
                if json {
                    let output = serde_json::json!({
                        "command": "validate",
                        "workflow": workflow.display().to_string(),
                        "valid": true,
                        "rules": 0,
                        "dependencies": 0,
                        "errors": [],
                        "warnings": [],
                        "missing_inputs": [],
                    });
                    println!("{}", serde_json::to_string_pretty(&output)?);
                } else {
                    eprintln!("{} {} — 0 rules", "✓".green().bold(), workflow.display());
                    eprintln!(
                        "  {} Workflow has no rules. Add [[rules]] sections to define pipeline steps.",
                        "âš  Warning:".yellow().bold()
                    );
                }
                return Ok(());
            }

            // Run semantic validation (E001-E008)
            let validation = oxo_flow_core::format::validate_format(&cfg);
            // W024-class warnings (#973 F6): `lint` already flags wildcards
            // with no declared source, but `validate` — the first gate in the
            // documented validate → dry-run → run path — stayed silent and
            // waved the workflow through with exit 0. Surface W024 here too
            // (warnings keep exit 0; only errors fail), so a green validate
            // no longer hides a workflow that cannot run. Scoped to W024 —
            // validate's contract is runnability; the full best-practice
            // sweep remains `lint`'s job and re-printing it here drowned
            // every validate in style noise.
            let lint_diags = if !as_include {
                oxo_flow_core::format::lint_format(&cfg, None)
            } else {
                Vec::new()
            };
            let warnings: Vec<&oxo_flow_core::format::Diagnostic> =
                lint_diags.iter().filter(|d| d.code == "W024").collect();
            let mut error_count = 0usize;
            let mut errors_json: Vec<serde_json::Value> = Vec::new();

            for d in &validation.diagnostics {
                if d.severity == oxo_flow_core::format::Severity::Error {
                    // --as-include skips input-existence checks (E010, W020) since
                    // sub-workflow fragments may reference files not yet present
                    if as_include && (d.code == "E010" || d.code == "W020") {
                        continue;
                    }
                    error_count += 1;
                    if json {
                        errors_json.push(serde_json::json!({
                            "code": d.code,
                            "message": d.message,
                            "rule": d.rule,
                            "suggestion": d.suggestion,
                        }));
                    } else {
                        eprintln!("  {} [{}]: {}", "error".red().bold(), d.code, d.message);
                        if let Some(ref rule) = d.rule {
                            eprintln!("    rule: {}", rule);
                        }
                        if let Some(ref suggestion) = d.suggestion {
                            eprintln!("    hint: {}", suggestion);
                        }
                    }
                }
            }

            // Check for missing input files (skip for --as-include).
            // Relative paths resolve against the workflow file's directory —
            // the same base the executor uses at run time (issue #68).
            let workflow_dir = oxo_flow_core::parent_dir(&workflow);
            let missing_inputs = if !as_include {
                collect_missing_inputs(&cfg, workflow_dir)
            } else {
                Vec::new()
            };
            // Pixi manifests are env inputs the executor needs before the
            // first rule runs (issue #848) — fail here with the same
            // severity as missing static inputs.
            let missing_manifests = if !as_include {
                collect_missing_pixi_manifests(&cfg, workflow_dir)
            } else {
                Vec::new()
            };
            for (rule, spec) in &missing_manifests {
                error_count += 1;
                let suggestion = "create the manifest at the resolved path or correct the \
                                  rule's `pixi` path (resolved relative to the workflow \
                                  directory)";
                if json {
                    errors_json.push(serde_json::json!({
                        "code": "E019",
                        "message": format!("pixi manifest '{spec}' declared by rule not found"),
                        "rule": rule,
                        "suggestion": suggestion,
                    }));
                } else {
                    eprintln!("  {} [E019]: {}", "error".red().bold(), suggestion);
                    eprintln!("    rule: {rule}");
                    eprintln!("    pixi manifest: {spec}");
                }
            }

            // Validate DAG construction (skip for --as-include)
            let (rules, dependencies) = if as_include {
                (cfg.rules.len(), 0)
            } else {
                match WorkflowDag::from_rules_with_config(
                    &cfg.rules,
                    &cfg.config_placeholder_values(),
                ) {
                    Ok(dag) => (dag.node_count(), dag.edge_count()),
                    Err(e) => {
                        if json {
                            let warnings_json: Vec<serde_json::Value> = warnings
                                .iter()
                                .map(|d| {
                                    serde_json::json!({
                                        "code": d.code,
                                        "message": d.message,
                                        "rule": d.rule,
                                        "suggestion": d.suggestion,
                                    })
                                })
                                .collect();
                            let output = serde_json::json!({
                                "command": "validate",
                                "workflow": workflow.display().to_string(),
                                "valid": false,
                                "rules": cfg.rules.len(),
                                "dependencies": 0,
                                "errors": [{"code": "DAG", "message": e.to_string()}],
                                "warnings": warnings_json,
                                "missing_inputs": [],
                            });
                            println!("{}", serde_json::to_string_pretty(&output)?);
                        } else {
                            eprintln!(
                                "{} {} — DAG error: {}",
                                "✗".red().bold(),
                                workflow.display(),
                                e
                            );
                        }
                        std::process::exit(1);
                    }
                }
            };

            if json {
                let warnings_json: Vec<serde_json::Value> = warnings
                    .iter()
                    .map(|d| {
                        serde_json::json!({
                            "code": d.code,
                            "message": d.message,
                            "rule": d.rule,
                            "suggestion": d.suggestion,
                        })
                    })
                    .collect();
                let output = serde_json::json!({
                    "command": "validate",
                    "workflow": workflow.display().to_string(),
                    "valid": error_count == 0,
                    "rules": rules,
                    "dependencies": dependencies,
                    "errors": errors_json,
                    "warnings": warnings_json,
                    "missing_inputs": missing_inputs,
                });
                println!("{}", serde_json::to_string_pretty(&output)?);
            } else {
                if error_count == 0 {
                    eprintln!(
                        "{} {} — {}, {}",
                        "✓".green().bold(),
                        workflow.display(),
                        plural(rules, "rule", "rules"),
                        plural(dependencies, "dependency", "dependencies")
                    );
                } else {
                    eprintln!(
                        "{} {} — {}",
                        "✗".red().bold(),
                        workflow.display(),
                        plural(error_count, "validation error", "validation errors")
                    );
                }

                for d in &warnings {
                    eprintln!(
                        "  {} [{}]: {}{}",
                        "warning".yellow().bold(),
                        d.code,
                        d.message,
                        d.rule
                            .as_ref()
                            .map(|r| format!(" (rule: {r})"))
                            .unwrap_or_default()
                    );
                    if let Some(ref suggestion) = d.suggestion {
                        eprintln!("    hint: {}", suggestion);
                    }
                }

                if !missing_inputs.is_empty() {
                    eprintln!(
                        "\n  {} The following input files do not exist:",
                        "âš  Warning:".yellow().bold()
                    );
                    for input in missing_inputs {
                        eprintln!("    - {}", input);
                    }
                }
            }

            // Exit with error if validation failed. Returning Err (not
            // process::exit) lets `test --json` compose steps into ONE
            // aggregate document — an inner exit bypassed emit_test_json
            // entirely on exactly the failure paths the command exists to
            // report (audit #666). The top-level dispatcher still exits 1.
            if error_count > 0 {
                anyhow::bail!(
                    "validation failed: {} in {}",
                    plural(error_count, "error", "errors"),
                    workflow.display()
                );
            }
        }
        Err(e) => {
            if json {
                let output = serde_json::json!({
                    "command": "validate",
                    "workflow": workflow.display().to_string(),
                    "valid": false,
                    "rules": 0,
                    "dependencies": 0,
                    "errors": [{"code": "PARSE", "message": e.to_string()}],
                    "warnings": [],
                    "missing_inputs": [],
                });
                println!("{}", serde_json::to_string_pretty(&output)?);
            } else {
                eprintln!("{} {} — {}", "✗".red().bold(), workflow.display(), e);
            }
            // The full parse error was already printed above (or embedded in
            // the JSON document); a bare context keeps the bail from
            // duplicating the message.
            anyhow::bail!("workflow parse failed");
        }
    }
    Ok(())
}

pub async fn lint_command(workflow: PathBuf, strict: bool, json: bool, ai: bool) -> Result<()> {
    print_banner();

    // AI: auto-detect from workflow [ai] or explicit --ai flag. Suppressed
    // under --json (#539): same contract as validate.
    if !json
        && let Some(provider) = crate::commands::ai_template::try_resolve_ai(Some(&workflow), ai)
    {
        crate::commands::ai_check::analyze_workflow(&workflow, &provider, "lint", "").await?;
        println!();
    }
    let config = match WorkflowConfig::from_file(&workflow) {
        Ok(config) => config,
        Err(e) => {
            // The JSON surface stays machine-readable on a parse failure too
            // (audit P4-6): it used to emit nothing on stdout, so a caller
            // could not tell a broken workflow from a crashed run — the same
            // case `validate --json` already reports as code=PARSE.
            if json {
                let output = serde_json::json!({
                    "command": "lint",
                    "workflow": workflow.display().to_string(),
                    "strict": strict,
                    "diagnostics": [{
                        "severity": "error",
                        "code": "PARSE",
                        "message": e.to_string(),
                        "rule": serde_json::Value::Null,
                        "suggestion": serde_json::Value::Null,
                    }],
                    "error_count": 1,
                    "warning_count": 0,
                    "info_count": 0,
                    "passed": false,
                });
                println!("{}", serde_json::to_string_pretty(&output)?);
            }
            return Err(e).with_context(|| format!("failed to parse {}", workflow.display()));
        }
    };

    let validation = oxo_flow_core::format::validate_format(&config);
    let lint_diags = oxo_flow_core::format::lint_format(&config, workflow.parent());

    // Read the raw file content: secret scanning and the schema pass both
    // need it. verify_schema (S001–S006) is wired here because the parsed
    // config cannot retain unknown top-level keys — this is the only place
    // S006 "unknown section" warnings can ever reach a user (issue #470).
    // S001–S005 are unreachable post-parse; their checks stay in
    // verify_schema as the schema-check contract.
    let raw_content = std::fs::read_to_string(&workflow).ok();
    let schema_diags = raw_content
        .as_deref()
        .map(oxo_flow_core::format::verify_schema)
        .map(|v| v.diagnostics)
        .unwrap_or_default();
    let secret_diags = raw_content
        .as_deref()
        .map(oxo_flow_core::format::scan_for_secrets)
        .unwrap_or_default();

    let mut error_count = 0usize;
    let mut warning_count = 0usize;
    let mut info_count = 0usize;

    for d in validation
        .diagnostics
        .iter()
        .chain(lint_diags.iter())
        .chain(schema_diags.iter())
        .chain(secret_diags.iter())
    {
        let prefix = match d.severity {
            oxo_flow_core::format::Severity::Error => {
                error_count += 1;
                "error".red().bold().to_string()
            }
            oxo_flow_core::format::Severity::Warning => {
                warning_count += 1;
                "warning".yellow().bold().to_string()
            }
            oxo_flow_core::format::Severity::Info => {
                info_count += 1;
                "info".blue().to_string()
            }
        };
        eprint!("  {} [{}]: {}", prefix, d.code, d.message);
        if let Some(ref rule) = d.rule {
            eprint!(" (rule: {})", rule);
        }
        eprintln!();
        // The fix hint is part of the human output too, not just --json —
        // the JSON-only placement silently dropped every suggestion from
        // the text report (issue #142 M11). Same style as validate.
        if let Some(ref suggestion) = d.suggestion {
            eprintln!("    hint: {}", suggestion);
        }
    }

    eprintln!(
        "\n{} {}, {}, {}",
        "Summary:".bold(),
        plural(error_count, "error", "errors"),
        plural(warning_count, "warning", "warnings"),
        plural(info_count, "info", "info")
    );

    // JSON output mode
    if json {
        let diagnostics: Vec<serde_json::Value> = validation
            .diagnostics
            .iter()
            .chain(lint_diags.iter())
            .chain(schema_diags.iter())
            .chain(secret_diags.iter())
            .map(|d| {
                serde_json::json!({
                    "severity": format!("{:?}", d.severity).to_lowercase(),
                    "code": d.code,
                    "message": d.message,
                    "rule": d.rule,
                    "suggestion": d.suggestion,
                })
            })
            .collect();

        let output = serde_json::json!({
            "command": "lint",
            "workflow": workflow.display().to_string(),
            "strict": strict,
            "diagnostics": diagnostics,
            "error_count": error_count,
            "warning_count": warning_count,
            "info_count": info_count,
            "passed": error_count == 0 && (!strict || warning_count == 0),
        });
        println!("{}", serde_json::to_string_pretty(&output).unwrap());
        if error_count > 0 || (strict && warning_count > 0) {
            // Return Err instead of process::exit so `test --json` can
            // compose this step into its aggregate document (audit #666).
            anyhow::bail!(
                "lint failed: {}, {}",
                plural(error_count, "error", "errors"),
                plural(warning_count, "warning", "warnings")
            );
        }
        return Ok(());
    }

    if error_count > 0 || (strict && warning_count > 0) {
        anyhow::bail!(
            "lint failed: {}, {}",
            plural(error_count, "error", "errors"),
            plural(warning_count, "warning", "warnings")
        );
    }
    Ok(())
}

/// Run the deep checks, print the human report to stderr, and build the JSON
/// document.
pub fn run_deep_check(
    workflow: &Path,
    workdir: Option<&Path>,
) -> Result<(
    oxo_flow_core::deep_check::DeepCheckReport,
    serde_json::Value,
)> {
    let config = WorkflowConfig::from_file(workflow)
        .with_context(|| format!("failed to parse {}", workflow.display()))?;
    // Judge existence from the same base the executor runs rules from:
    // `--workdir`, or the workflow file's directory (issue #68 semantics).
    let base_dir = workdir
        .map(Path::to_path_buf)
        .unwrap_or_else(|| oxo_flow_core::parent_dir(workflow).to_path_buf());
    let report = oxo_flow_core::deep_check::compute_deep_check(&config, &base_dir);

    print_deep_console(&report);

    let diagnostics: Vec<serde_json::Value> = report
        .findings
        .iter()
        .map(|f| {
            serde_json::json!({
                "severity": format!("{:?}", f.severity).to_lowercase(),
                "code": f.code,
                "message": f.message,
                "rule": f.rule,
                "suggestion": f.suggestion,
                "path": f.path,
            })
        })
        .collect();
    let document = serde_json::json!({
        "command": "deep-check",
        "workflow": workflow.display().to_string(),
        "diagnostics": diagnostics,
        "error_count": report.error_count,
        "warning_count": report.warning_count,
        "passed": report.passed,
    });
    Ok((report, document))
}

/// Print the human-readable deep-check report to stderr, grouped by category
/// with a green summary line per clean category (lint precedent: human
/// output goes to stderr even in `--json` mode).
fn print_deep_console(report: &oxo_flow_core::deep_check::DeepCheckReport) {
    let finding_lines = |code: &str| {
        for f in report.findings.iter().filter(|f| f.code == code) {
            let icon = if f.severity == oxo_flow_core::format::Severity::Error {
                "✗".red().bold()
            } else {
                "âš ".yellow().bold()
            };
            let rule_suffix = f
                .rule
                .as_deref()
                .map(|rule| format!(" (rule: {rule})"))
                .unwrap_or_default();
            eprintln!("    {} {}{} [{}]", icon, f.message, rule_suffix, f.code);
            if let Some(hint) = &f.suggestion {
                eprintln!("      hint: {}", hint.dimmed());
            }
        }
    };

    // Categories with nothing checked and no findings are omitted entirely.
    let has = |code: &str| report.findings.iter().any(|f| f.code == code);

    if report.scripts_checked > 0 || has("D001") {
        eprintln!("{}", "  Scripts:".bold());
        if report.scripts_checked > 0 && !has("D001") {
            eprintln!(
                "    {} {} script reference(s) found",
                "✓".green().bold(),
                report.scripts_checked
            );
        }
        finding_lines("D001");
    }

    if report.envs_checked > 0 || has("D002") {
        eprintln!("{}", "  Environments:".bold());
        if report.envs_checked > 0 && !has("D002") {
            eprintln!(
                "    {} {} environment definition(s) found",
                "✓".green().bold(),
                report.envs_checked
            );
        }
        finding_lines("D002");
    }

    if report.commands_probed > 0 || has("D003") {
        eprintln!("{}", "  Binaries:".bold());
        if report.commands_probed > 0 && !has("D003") {
            eprintln!(
                "    {} {} command(s) found in PATH",
                "✓".green().bold(),
                report.commands_probed
            );
        }
        finding_lines("D003");
    }

    if report.references_checked > 0 || has("D004") {
        eprintln!("{}", "  References:".bold());
        if report.references_checked > 0 && !has("D004") {
            eprintln!(
                "    {} {} found",
                "✓".green().bold(),
                plural(
                    report.references_checked,
                    "reference path",
                    "reference paths"
                )
            );
        }
        finding_lines("D004");
    }

    eprintln!(
        "\n{} {}, {}",
        "Deep check summary:".bold(),
        plural(report.error_count, "error", "errors"),
        plural(report.warning_count, "warning", "warnings")
    );
}

pub fn format_command(workflow: PathBuf, output: Option<PathBuf>, check: bool) -> Result<()> {
    let config = WorkflowConfig::from_file(&workflow)
        .with_context(|| format!("failed to parse {}", workflow.display()))?;

    let formatted = oxo_flow_core::format::format_workflow(&config);

    if check {
        let original = std::fs::read_to_string(&workflow)?;
        if original.trim() == formatted.trim() {
            eprintln!(
                "{} {} is already formatted",
                "✓".green().bold(),
                workflow.display()
            );
        } else {
            eprintln!(
                "{} {} needs formatting",
                "✗".red().bold(),
                workflow.display()
            );
            std::process::exit(1);
        }
    } else {
        match output {
            Some(path) => {
                std::fs::write(&path, &formatted)?;
                eprintln!("Formatted workflow written to {}", path.display());
            }
            None => {
                print!("{formatted}");
            }
        }
    }
    Ok(())
}

pub fn touch_command(
    workflow: PathBuf,
    rules: Vec<String>,
    workdir: Option<PathBuf>,
    cli_args: Vec<String>,
) -> Result<()> {
    print_banner();
    let mut config = WorkflowConfig::from_file(&workflow)
        .with_context(|| format!("failed to parse {}", workflow.display()))?;

    // Config overrides first: rules may branch on {config.*} before the
    // engine ever expands wildcards (issue #432). Parsing/validation reuses
    // the exact `run`/`dry-run` machinery so `touch` and `run` agree on
    // what an override means.
    let declared_config_keys: std::collections::HashSet<String> =
        config.config_meta.keys().cloned().collect();
    let cli_arg_values = super::run::parse_cli_overrides(cli_args, &declared_config_keys, "touch")?;
    super::run::apply_cli_overrides(&mut config, &cli_arg_values)?;
    super::run::warn_unknown_override_keys(&cli_arg_values, &declared_config_keys, &workflow);

    config.apply_defaults();
    // Expand wildcards so output patterns are concrete paths
    if let Err(e) = config.expand_wildcards() {
        eprintln!("  {} Could not expand wildcards: {}", "Note:".yellow(), e);
        eprintln!(
            "  {} Wildcard patterns in outputs will be skipped.",
            "Info:".dimmed()
        );
    }

    // Outputs may embed {config.*} references (e.g. "{config.out_dir}/x.bam")
    // — resolve them against the overridden config so `touch` and `run`
    // agree on where the files live.
    let config_values: HashMap<String, String> =
        super::run::config_placeholder_values(&config.config);

    // A typo'd rule name must not exit 0 with "0 file(s) touched" — scripts
    // would read that as success (audit #276 P4-5). The command fails at the
    // end when this list is non-empty, after touching whatever it did match.
    let unknown_rules: Vec<&String> = rules
        .iter()
        .filter(|name| !config.rules.iter().any(|rule| &rule.name == *name))
        .collect();
    let rules_to_touch: Vec<&oxo_flow_core::rule::Rule> = if rules.is_empty() {
        config.rules.iter().collect()
    } else {
        let matched: Vec<&oxo_flow_core::rule::Rule> = config
            .rules
            .iter()
            .filter(|r| rules.contains(&r.name))
            .collect();
        for name in &unknown_rules {
            eprintln!(
                "  {} rule '{name}' not found in workflow — no files touched for it",
                "âš ".yellow()
            );
        }
        if !unknown_rules.is_empty() {
            let expanded_hint = if config.rules.len() != config.rule_templates.len() {
                " (run `oxo-flow dry-run <workflow>` to list expanded rule names)"
            } else {
                ""
            };
            eprintln!(
                "  {} known rules: {}{expanded_hint}",
                "Info:".dimmed(),
                config
                    .rules
                    .iter()
                    .map(|r| r.name.as_str())
                    .collect::<Vec<_>>()
                    .join(", ")
            );
        }
        matched
    };

    let mut touched = 0usize;
    let mut failed_touched = 0usize;
    let mut skipped = 0usize;
    let mut skipped_patterns: Vec<(String, String)> = Vec::new(); // (rule_name, pattern)

    // Outputs live next to the workflow file (--workdir overrides) — the
    // same path-resolution convention every other workflow command uses.
    // The old current_dir() default touched files in the caller's cwd
    // when the workflow lived elsewhere (CLI alignment audit 2026-08-14).
    let base_dir = workdir
        .clone()
        .unwrap_or_else(|| workflow.parent().map(Path::to_path_buf).unwrap_or_default());

    for rule in &rules_to_touch {
        for output in &rule.output {
            let output =
                oxo_flow_core::executor::checkpoint::expand_config_in_path(output, &config_values);
            let has_wildcard = output.contains('{') && output.contains('}');
            if has_wildcard {
                skipped += 1;
                skipped_patterns.push((rule.name.clone(), output.clone()));
                continue;
            }

            // Path safety: reject path traversal and absolute paths
            if output.contains("..") || output.starts_with('/') || output.starts_with('~') {
                eprintln!("  {} {} (rejected: unsafe path)", "✗".red().bold(), output);
                continue;
            }

            let path = base_dir.join(&output);
            if path.exists() {
                // Update modification time
                match filetime::set_file_mtime(&path, filetime::FileTime::now()) {
                    Ok(()) => {
                        touched += 1;
                        eprintln!("  {} {}", "✓".green(), output);
                    }
                    Err(e) => {
                        eprintln!("  {} {} ({})", "✗".red(), output, e);
                        failed_touched += 1;
                    }
                }
            } else {
                // Create empty file to mark as "done"
                if let Some(parent) = path.parent()
                    && let Err(e) = std::fs::create_dir_all(parent)
                {
                    eprintln!(
                        "  {} {} (cannot create directory: {})",
                        "✗".red(),
                        output,
                        e
                    );
                    failed_touched += 1;
                    continue;
                }
                match std::fs::write(&path, "") {
                    Ok(()) => {
                        touched += 1;
                        eprintln!("  {} {} (created)", "✓".green(), output);
                    }
                    Err(e) => {
                        eprintln!("  {} {} (failed: {})", "✗".red(), output, e);
                        failed_touched += 1;
                    }
                }
            }
        }
    }

    eprintln!(
        "\n{} {}, {} skipped",
        "Done:".bold(),
        plural(touched, "file", "files"),
        plural(skipped, "wildcard pattern", "wildcard patterns")
    );

    if !skipped_patterns.is_empty() {
        eprintln!();
        for (rule_name, pattern) in &skipped_patterns {
            eprintln!(
                "  {} {} → {} (wildcard pattern — not expanded)",
                "Skipped:".yellow(),
                rule_name,
                pattern.dimmed()
            );
        }
        eprintln!();
        eprintln!(
            "  {} To touch expanded rules, use specific rule names after wildcard expansion.",
            "Tip:".bold().cyan()
        );
        eprintln!(
            "  {} Run 'oxo-flow dry-run {}' to see expanded rule names, then use:",
            "   ".dimmed(),
            workflow.display()
        );
        eprintln!(
            "  {}   oxo-flow touch {} --rule <expanded_name>",
            "   ".dimmed(),
            workflow.display()
        );
    }

    // Requesting a rule that does not exist is a usage error, not a no-op:
    // exit non-zero so scripts (and the audit that found this) can tell the
    // difference between "touched" and "nothing matched".
    if !unknown_rules.is_empty() {
        anyhow::bail!(
            "{} not found in '{}' — nothing was touched for them.\n  \
             Run `oxo-flow dry-run {}` to list the expanded rule names.",
            oxo_flow_core::plural(
                unknown_rules.len(),
                "requested rule name",
                "requested rule names"
            ),
            workflow.display(),
            workflow.display()
        );
    }

    // Per-file I/O failures are usage failures too (#540 semantics):
    // exiting 0 while every touch failed made calling pipelines record
    // "outputs marked up-to-date" when nothing was marked (audit #668).
    if failed_touched > 0 {
        anyhow::bail!(
            "touch: {} failed to mark in {}",
            oxo_flow_core::plural(failed_touched, "output path", "output paths"),
            workflow.display()
        );
    }

    Ok(())
}

/// Inputs that resolve to no file and no producer — the CLI validate /
/// doctor missing-input pass (issue #467).
///
/// `{config.*}` placeholders expand against the parsed config before the
/// concrete checks so a config-routed input is existence-checked against
/// its real path, and a concrete input whose producer declares the same
/// path through a config-routed output template is recognized as
/// generated instead of "missing". Engine wildcards (`{sample}`, …) keep
/// their raw-token handling: they need a declared sample domain to ever
/// bind (issue #324 F-3), and without one the run fails mid-flight with
/// a literal brace token — validate must flag that here.
fn collect_missing_inputs(cfg: &WorkflowConfig, workflow_dir: &std::path::Path) -> Vec<String> {
    // Sample placeholders need a declared sample domain to resolve at
    // run time: without any of sample_groups / pairs / sample_pattern the
    // wildcard can never bind.
    let sample_domain_declared = !cfg.sample_groups.is_empty()
        || !cfg.pairs.is_empty()
        || cfg.workflow.sample_pattern.is_some();
    const SAMPLE_PLACEHOLDERS: &[&str] = &[
        "{sample}",
        "{group}",
        "{pair_id}",
        "{experiment}",
        "{control}",
        "{tumor}",
        "{normal}",
        "{log}",
    ];
    let expand = |path: &str| oxo_flow_core::config::expand_config_vars_in_path(path, &cfg.config);
    let mut missing = Vec::new();
    for rule in &cfg.rules {
        // A `when`-gated-off rule can never run in this configuration —
        // its inputs are not required to exist (issue #493, same stance as
        // the validate-side E010/W020 gate).
        if rule.when.as_deref().is_some_and(|when| {
            !oxo_flow_core::executor::process::evaluate_condition_with_wildcards_and_base_dir(
                when,
                &cfg.config,
                &std::collections::HashMap::new(),
                Some(workflow_dir),
            )
        }) {
            continue;
        }
        // Same stance for a scatter rule with zero fan-out (issue #616):
        // declared `values` win; an unresolvable `values_from` keeps the
        // rule checked so the typo surfaces instead of passing silently.
        if rule.scatter.as_ref().is_some_and(|scatter| {
            if !scatter.values.is_empty() {
                return false;
            }
            match scatter.values_from.as_deref() {
                Option::None => true,
                Some(v_from) => cfg
                    .resolve_config_list(v_from)
                    .is_some_and(|values| values.is_empty()),
            }
        }) {
            continue;
        }
        for input in &rule.input {
            let expanded = expand(input);
            if expanded.contains('{') {
                if !sample_domain_declared
                    && SAMPLE_PLACEHOLDERS.iter().any(|ph| input.contains(ph))
                {
                    // Wildcard input with no sample domain to bind it.
                    missing.push(format!(
                        "{input} (no sample groups/pairs/sample_pattern declared)"
                    ));
                }
                continue;
            }
            if !workflow_dir.join(&expanded).exists() {
                // Also check if it's an output of another rule — outputs
                // expand with the same map so a concrete input matches its
                // config-routed producer template.
                let is_generated = cfg
                    .rules
                    .iter()
                    .any(|r| r.output.iter().any(|o| expand(o) == expanded));
                if !is_generated {
                    missing.push(expanded);
                }
            }
        }
    }
    missing
}

/// Preflight each rule's declared pixi manifest against the workflow
/// directory (issue #848): a missing manifest previously surfaced only at
/// run time, after validate had already reported success. Returns
/// (rule name, resolved spec) pairs for every declared-but-absent manifest.
/// Mirrors `collect_missing_inputs`' skip closures — a when-gated-off rule
/// and a zero-fan-out scatter rule can never run, so their manifests are
/// not required. Env specs are resolved (not config-expanded) exactly as
/// the executor does at dispatch (issue #427).
fn collect_missing_pixi_manifests(
    cfg: &WorkflowConfig,
    workflow_dir: &std::path::Path,
) -> Vec<(String, String)> {
    let mut missing = Vec::new();
    for rule in &cfg.rules {
        if rule.when.as_deref().is_some_and(|when| {
            !oxo_flow_core::executor::process::evaluate_condition_with_wildcards_and_base_dir(
                when,
                &cfg.config,
                &std::collections::HashMap::new(),
                Some(workflow_dir),
            )
        }) {
            continue;
        }
        if rule.scatter.as_ref().is_some_and(|scatter| {
            if !scatter.values.is_empty() {
                return false;
            }
            match scatter.values_from.as_deref() {
                Option::None => true,
                Some(v_from) => cfg
                    .resolve_config_list(v_from)
                    .is_some_and(|values| values.is_empty()),
            }
        }) {
            continue;
        }
        let resolved = rule.environment.resolve_relative_paths(Some(workflow_dir));
        if let Some(spec) = resolved.pixi
            && oxo_flow_core::environment::validate_pixi_manifest(&spec).is_err()
        {
            missing.push((rule.name.clone(), spec));
        }
    }
    missing
}

#[cfg(test)]
mod tests {
    use assert_cmd::Command;
    use predicates::prelude::PredicateBooleanExt;
    use std::io::Write;
    use tempfile::NamedTempFile;

    #[test]
    fn missing_inputs_resolve_config_placeholders_and_when_gates() {
        use super::collect_missing_inputs;
        // Issue #467: config-routed producers must satisfy concrete inputs
        // and config-routed inputs must be existence-checked. Issue #493:
        // a when-gated-off rule's missing inputs stay silent.
        let dir = tempfile::tempdir().unwrap();
        let real = dir.path().join("ref.fa");
        std::fs::write(&real, b"ACGT").unwrap();
        let toml = format!(
            r#"
[workflow]
name = "t"

[config]
out = "{}"
reference = "{}"
prepare_reference = false

[[rules]]
name = "produce"
output = ["{{config.out}}/x.txt"]
shell = "echo x > {{config.out}}/x.txt"

[[rules]]
name = "consume"
input = ["{}/x.txt", "{{config.reference}}", "definitely-missing.txt"]
output = ["final.txt"]
shell = "cat {{config.reference}} > final.txt"

[[rules]]
name = "gated_index"
input = ["{{config.reference}}.missing-sidecar"]
output = ["sidecar.done"]
when = "config.prepare_reference"
shell = "true"
"#,
            dir.path().display(),
            real.display(),
            dir.path().display()
        );
        let cfg = oxo_flow_core::config::WorkflowConfig::parse(&toml).unwrap();
        let missing = collect_missing_inputs(&cfg, dir.path());
        assert_eq!(
            missing,
            vec!["definitely-missing.txt".to_string()],
            "config-routed generated input and existing config-routed input must pass; \
             only the genuinely missing file may be flagged; the when-gated-off rule's \
             missing sidecar must stay silent (issue #493): {missing:?}"
        );
    }

    #[test]
    fn missing_inputs_skip_empty_scatter_rules() {
        use super::collect_missing_inputs;
        // Issue #616: a scatter rule with zero fan-out can never run —
        // dry-run's missing-input audit must mirror validate and stay
        // silent, while a non-empty scatter keeps the rule checked.
        let dir = tempfile::tempdir().unwrap();
        let toml = |items: &str, reference: &str| {
            format!(
                r#"
[workflow]
name = "t"

[config]
items = {0}
reference = "{1}"

[[rules]]
name = "enrich"
input = ["{{config.reference}}"]
output = ["enrich/{{item}}.tsv"]
shell = "Rscript enrich.R"

[rules.scatter]
variable = "item"
values_from = "config.items"
"#,
                items, reference
            )
        };
        // Empty list → zero instances → silent.
        let cfg =
            oxo_flow_core::config::WorkflowConfig::parse(&toml("[]", "missing/ref.fa")).unwrap();
        let missing = collect_missing_inputs(&cfg, dir.path());
        assert!(
            missing.is_empty(),
            "an empty-scatter rule's missing inputs must stay silent: {missing:?}"
        );
        // Non-empty list → the rule can run → the missing input is flagged.
        let cfg =
            oxo_flow_core::config::WorkflowConfig::parse(&toml(r#"["item-a"]"#, "missing/ref.fa"))
                .unwrap();
        let missing = collect_missing_inputs(&cfg, dir.path());
        assert_eq!(
            missing,
            vec!["missing/ref.fa".to_string()],
            "a non-empty scatter must keep its inputs existence-checked: {missing:?}"
        );
    }

    #[test]
    fn missing_pixi_manifests_resolve_against_the_workflow_dir() {
        use super::collect_missing_pixi_manifests;
        // Issue #848: a declared pixi manifest must be resolved against the
        // workflow directory — a manifest that exists only relative to the
        // process CWD must NOT satisfy the check, and an absolute spec
        // passes through unchanged.
        let dir = tempfile::tempdir().unwrap();
        let wf_dir = dir.path().join("wf");
        std::fs::create_dir_all(wf_dir.join("envs")).unwrap();
        std::fs::write(wf_dir.join("envs/pixi.toml"), "[project]\nname = \"x\"\n").unwrap();
        let toml = |pixi_spec: &str| {
            format!(
                r#"
[workflow]
name = "t"

[[rules]]
name = "r"
output = ["o.txt"]
shell = "echo x"

[rules.environment]
pixi = "{pixi_spec}"
"#
            )
        };
        // Resolved-and-present → silent.
        let cfg = oxo_flow_core::config::WorkflowConfig::parse(&toml("envs/pixi.toml")).unwrap();
        assert!(collect_missing_pixi_manifests(&cfg, &wf_dir).is_empty());
        // Absolute spec that exists → silent (spec passes through untouched).
        let absolute = wf_dir.join("envs/pixi.toml");
        let cfg =
            oxo_flow_core::config::WorkflowConfig::parse(&toml(&absolute.display().to_string()))
                .unwrap();
        assert!(collect_missing_pixi_manifests(&cfg, &wf_dir).is_empty());
        // Missing relative to the workflow dir → flagged with the RESOLVED
        // spec (not the raw declaration).
        let cfg = oxo_flow_core::config::WorkflowConfig::parse(&toml("envs/absent.toml")).unwrap();
        let missing = collect_missing_pixi_manifests(&cfg, &wf_dir);
        assert_eq!(
            missing,
            vec![(
                "r".to_string(),
                wf_dir.join("envs/absent.toml").display().to_string()
            )],
            "a missing manifest must be reported with its workflow-dir-resolved path: {missing:?}"
        );
    }

    #[test]
    fn missing_pixi_manifests_silence_gated_off_and_zero_scatter_rules() {
        use super::collect_missing_pixi_manifests;
        // Same stance as collect_missing_inputs (issues #493/#616): a rule
        // that can never run in this configuration does not require its
        // manifest to exist.
        let dir = tempfile::tempdir().unwrap();
        let toml = |when: &str, items: &str| {
            format!(
                r#"
[workflow]
name = "t"

[config]
prepare = false
items = {items}

[[rules]]
name = "gated"
output = ["a.txt"]
when = "{when}"
shell = "true"

[rules.environment]
pixi = "envs/absent.toml"

[[rules]]
name = "scattered"
output = ["b.txt"]
shell = "true"

[rules.scatter]
variable = "item"
values_from = "config.items"

[rules.environment]
pixi = "envs/absent.toml"
"#
            )
        };
        // when-gated-off + empty scatter list → both silent.
        let cfg =
            oxo_flow_core::config::WorkflowConfig::parse(&toml("config.prepare", "[]")).unwrap();
        assert!(collect_missing_pixi_manifests(&cfg, dir.path()).is_empty());
        // Non-empty scatter → the rule can run → its manifest is required.
        let cfg =
            oxo_flow_core::config::WorkflowConfig::parse(&toml("config.prepare", r#"["item-a"]"#))
                .unwrap();
        let missing = collect_missing_pixi_manifests(&cfg, dir.path());
        assert_eq!(
            missing.len(),
            1,
            "a non-empty scatter must keep its manifest existence-checked: {missing:?}"
        );
        assert_eq!(missing[0].0, "scattered");
        // when-gated-on → the gated rule's manifest is required too.
        let toml_on = toml("true", "[]");
        let cfg = oxo_flow_core::config::WorkflowConfig::parse(&toml_on).unwrap();
        let missing = collect_missing_pixi_manifests(&cfg, dir.path());
        assert!(
            missing.iter().any(|(rule, _)| rule == "gated"),
            "a when-gated-on rule's manifest must be checked: {missing:?}"
        );
    }

    #[test]
    fn test_as_include_skips_dag_validation() {
        // Create a fragment with rules that reference undefined inputs
        let fragment = r#"
[workflow]
name = "qc-fragment"

[[rules]]
name = "fastqc"
input = ["{sample}.fastq"]
output = ["{sample}_fastqc.html"]
shell = "fastqc {input}"
"#;
        let mut file = NamedTempFile::with_suffix(".oxoflow").unwrap();
        file.write_all(fragment.as_bytes()).unwrap();

        // Should pass with --as-include (skips DAG validation)
        Command::cargo_bin("oxo-flow")
            .unwrap()
            .arg("validate")
            .arg("--as-include")
            .arg(file.path())
            .assert()
            .success();
    }

    #[test]
    fn test_as_include_validates_syntax() {
        // Create an invalid fragment (missing required 'name' field)
        let fragment = r#"
[workflow]
name = "bad-fragment"

[[rules]]
# Missing required 'name' field
input = ["test.txt"]
"#;
        let mut file = NamedTempFile::with_suffix(".oxoflow").unwrap();
        file.write_all(fragment.as_bytes()).unwrap();

        // Should fail even with --as-include (syntax errors)
        Command::cargo_bin("oxo-flow")
            .unwrap()
            .arg("validate")
            .arg("--as-include")
            .arg(file.path())
            .assert()
            .failure();
    }

    #[test]
    fn validate_summary_pluralises_counts() {
        // Audit P4-1d: a single-rule workflow reported "1 rules".
        let single = r#"
[workflow]
name = "one-rule"

[[rules]]
name = "only"
output = ["o.txt"]
shell = "true"
"#;
        let mut file = NamedTempFile::with_suffix(".oxoflow").unwrap();
        file.write_all(single.as_bytes()).unwrap();

        Command::cargo_bin("oxo-flow")
            .unwrap()
            .arg("validate")
            .arg(file.path())
            .assert()
            .success()
            .stderr(
                predicates::str::contains("1 rule, 0 dependencies")
                    .and(predicates::str::contains("1 rules").not()),
            );
    }

    #[test]
    fn lint_json_reports_parse_failures() {
        // Audit P4-6: `lint --json` on an unparseable workflow used to print
        // nothing on stdout, leaving the caller without a machine-readable
        // verdict; `validate --json` already reports code=PARSE here.
        let broken = "[workflow\nname = \"broken\"\n";
        let mut file = NamedTempFile::with_suffix(".oxoflow").unwrap();
        file.write_all(broken.as_bytes()).unwrap();

        let assert = Command::cargo_bin("oxo-flow")
            .unwrap()
            .arg("lint")
            .arg("--json")
            .arg(file.path())
            .assert()
            .failure();

        let stdout = String::from_utf8_lossy(&assert.get_output().stdout).to_string();
        let parsed: serde_json::Value =
            serde_json::from_str(&stdout).expect("lint --json must emit JSON on stdout");
        assert_eq!(parsed["command"], "lint");
        assert_eq!(parsed["passed"], false);
        assert_eq!(parsed["error_count"], 1);
        assert_eq!(parsed["diagnostics"][0]["code"], "PARSE");
        assert_eq!(parsed["diagnostics"][0]["severity"], "error");
        assert!(
            !stdout.is_empty(),
            "the JSON surface must never be empty, even on a parse failure"
        );
    }

    #[test]
    fn test_touch_accepts_config_overrides() {
        // Issue #432: `touch` must accept the same KEY=VALUE override form
        // as `run`, and outputs that reference {config.*} must resolve
        // against the overridden value.
        let fragment = r#"
[workflow]
name = "touch-override"

[config]
out_dir = { default = "results", required = false }

[[rules]]
name = "step"
input = []
output = ["{config.out_dir}/done.flag"]
shell = "touch {output[0]}"
"#;
        let dir = tempfile::tempdir().unwrap();
        let wf = dir.path().join("wf.oxoflow");
        std::fs::write(&wf, fragment).unwrap();

        Command::cargo_bin("oxo-flow")
            .unwrap()
            .args(["touch", wf.to_str().unwrap(), "out_dir=other"])
            .assert()
            .success();

        assert!(
            dir.path().join("other/done.flag").exists(),
            "output must resolve under the overridden out_dir"
        );
        assert!(
            !dir.path().join("results/done.flag").exists(),
            "default out_dir must NOT be touched when overridden"
        );
    }

    #[test]
    fn test_touch_unknown_flag_is_command_aware() {
        // A run-only flag must be rejected with a touch-aware message
        // (issue #430 contract shared via parse_cli_overrides).
        let fragment = r#"
[workflow]
name = "touch-flag"

[[rules]]
name = "step"
input = []
output = ["a.txt"]
shell = "touch a.txt"
"#;
        let mut file = NamedTempFile::with_suffix(".oxoflow").unwrap();
        file.write_all(fragment.as_bytes()).unwrap();

        let assert = Command::cargo_bin("oxo-flow")
            .unwrap()
            .args(["touch", file.path().to_str().unwrap(), "--force"])
            .assert()
            .failure();

        let stderr = String::from_utf8_lossy(&assert.get_output().stderr).to_string();
        assert!(
            stderr.contains("touch"),
            "flag error must name the touch command, got: {stderr}"
        );
    }
}