flodl-cli 0.5.0

libtorch manager and GPU diagnostic tool for Rust deep learning
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
//! Pure command-graph dispatch.
//!
//! `walk_commands` is the outer walker: it chases an arbitrarily nested
//! `commands:` graph starting from a top-level name + tail, and returns a
//! `WalkOutcome` describing what the caller should do (run a script,
//! spawn an entry, print help, error out, ...). The walker performs no
//! IO of its own: no process spawning, no stdout writes, no cwd reads.
//!
//! `classify_path_step` is the inner classifier used by `walk_commands`
//! for the `Path` arm: loads the child fdl.yml and inspects the tail to
//! decide whether to descend, render help, refresh the schema cache, or
//! forward to the entry.
//!
//! Keeping all impure actions (printing, spawning) in the caller makes
//! both functions straight-line and unit-testable against tempdir
//! fixtures.

use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

use crate::config::{self, CommandConfig, CommandKind, CommandSpec};

/// What a single `Path`-kind step resolved to. Every variant holds the
/// loaded `child` config when applicable, so the caller doesn't re-load.
pub enum PathOutcome {
    /// Failed to load the child `fdl.yml`. The string is the
    /// underlying error message.
    LoadFailed(String),
    /// Next tail token is a known sub-command of the child — descend.
    Descend {
        child: Box<CommandConfig>,
        new_dir: PathBuf,
        new_name: String,
    },
    /// Tail carries `--help` / `-h` at this level.
    ShowHelp { child: Box<CommandConfig> },
    /// Tail carries `--refresh-schema`.
    RefreshSchema {
        child: Box<CommandConfig>,
        child_dir: PathBuf,
    },
    /// Forward the tail to the child's entry.
    Exec {
        child: Box<CommandConfig>,
        child_dir: PathBuf,
    },
}

/// Classify a `Path`-kind step. Pure: loads the child config, inspects
/// the tail, and returns the matching [`PathOutcome`]. The caller owns
/// every side effect (printing, spawning).
pub fn classify_path_step(
    spec: &CommandSpec,
    name: &str,
    current_dir: &Path,
    tail: &[String],
    env: Option<&str>,
) -> PathOutcome {
    let child_dir = spec.resolve_path(name, current_dir);
    let child_cfg = match config::load_command_with_env(&child_dir, env) {
        Ok(c) => c,
        Err(e) => return PathOutcome::LoadFailed(e),
    };

    // Descent check runs first: `--help` / `--refresh-schema` apply to
    // the level the user is asking about, not to the parent. If the
    // next token names a nested entry, we descend before reading flags.
    if let Some(next) = tail.first() {
        if child_cfg.commands.contains_key(next) {
            return PathOutcome::Descend {
                child: Box::new(child_cfg),
                new_dir: child_dir,
                new_name: next.clone(),
            };
        }
    }

    if tail.iter().any(|a| a == "--help" || a == "-h") {
        return PathOutcome::ShowHelp {
            child: Box::new(child_cfg),
        };
    }

    if tail.iter().any(|a| a == "--refresh-schema") {
        return PathOutcome::RefreshSchema {
            child: Box::new(child_cfg),
            child_dir,
        };
    }

    PathOutcome::Exec {
        child: Box::new(child_cfg),
        child_dir,
    }
}

// ── Outer walker ────────────────────────────────────────────────────────

/// What the outer walker resolved a user invocation to. The caller owns
/// every impure action (spawning, printing, exit code); the walker just
/// returns the terminal state.
pub enum WalkOutcome {
    /// Top-level or nested `Run` — caller runs the inline script.
    RunScript {
        command: String,
        docker: Option<String>,
        cwd: PathBuf,
    },
    /// Path-or-Preset terminal → caller invokes the child's entry. For
    /// a Preset, `preset` is the preset name inside the enclosing
    /// `commands:` block; for a Path-Exec it is `None`.
    ExecCommand {
        config: Box<CommandConfig>,
        preset: Option<String>,
        tail: Vec<String>,
        cmd_dir: PathBuf,
    },
    /// Path terminal with `--refresh-schema` in the tail.
    RefreshSchema {
        config: Box<CommandConfig>,
        cmd_dir: PathBuf,
        cmd_name: String,
    },
    /// Path terminal with `--help` / `-h` in the tail.
    PrintCommandHelp {
        config: Box<CommandConfig>,
        name: String,
    },
    /// Preset terminal with `--help` / `-h` in the tail.
    PrintPresetHelp {
        config: Box<CommandConfig>,
        parent_label: String,
        preset_name: String,
    },
    /// Run terminal with `--help` / `-h` in the tail.
    PrintRunHelp {
        name: String,
        description: Option<String>,
        run: String,
        docker: Option<String>,
    },
    /// The top-level or descended-into name doesn't exist in the current
    /// `commands:` map. Caller prints the project-help banner.
    UnknownCommand { name: String },
    /// A Preset-kind command at the top level has nothing to reuse an
    /// `entry:` from. Caller prints a pointer to the fix.
    PresetAtTopLevel { name: String },
    /// Structural error: spec declares both `run:` and `path:`, or a
    /// child fdl.yml failed to load / parse. String is the diagnostic.
    Error(String),
}

/// Walk the command graph from a top-level name and produce a
/// [`WalkOutcome`]. Every transition is pure: the walker never spawns a
/// process, prints to stdout, or reads the process cwd. Inputs carry all
/// the context needed.
///
/// - `cmd_name`: the top-level token the user typed (`fdl <cmd_name> ...`).
/// - `tail`: positional args following `cmd_name` (typically `&args[2..]`).
/// - `top_commands`: the root `commands:` block (usually
///   `&project.commands`).
/// - `project_root`: the directory containing the base `fdl.yml`; acts
///   as the initial `current_dir` for Path resolution.
/// - `env`: active overlay name, threaded to each `load_command_with_env`
///   call so descended configs pick up env-layered fields.
pub fn walk_commands(
    cmd_name: &str,
    tail: &[String],
    top_commands: &BTreeMap<String, CommandSpec>,
    project_root: &Path,
    env: Option<&str>,
) -> WalkOutcome {
    let mut commands: BTreeMap<String, CommandSpec> = top_commands.clone();
    let mut enclosing: Option<CommandConfig> = None;
    let mut current_dir: PathBuf = project_root.to_path_buf();
    let mut name: String = cmd_name.to_string();
    let mut current_tail: Vec<String> = tail.to_vec();

    loop {
        let spec = match commands.get(&name) {
            Some(s) => s.clone(),
            None => return WalkOutcome::UnknownCommand { name },
        };

        let kind = match spec.kind() {
            Ok(k) => k,
            Err(e) => return WalkOutcome::Error(format!("command `{name}`: {e}")),
        };

        match kind {
            CommandKind::Run => {
                let command = spec
                    .run
                    .expect("Run kind guarantees `run` is set");
                if current_tail.iter().any(|a| a == "--help" || a == "-h") {
                    return WalkOutcome::PrintRunHelp {
                        name,
                        description: spec.description,
                        run: command,
                        docker: spec.docker,
                    };
                }
                return WalkOutcome::RunScript {
                    command,
                    docker: spec.docker,
                    cwd: current_dir,
                };
            }
            CommandKind::Path => {
                match classify_path_step(&spec, &name, &current_dir, &current_tail, env) {
                    PathOutcome::LoadFailed(msg) => return WalkOutcome::Error(msg),
                    PathOutcome::Descend {
                        child,
                        new_dir,
                        new_name,
                    } => {
                        commands = child.commands.clone();
                        enclosing = Some(*child);
                        current_dir = new_dir;
                        name = new_name;
                        // classify_path_step returned Descend because
                        // current_tail[0] named a nested command; consume
                        // that token before the next iteration.
                        if !current_tail.is_empty() {
                            current_tail.remove(0);
                        }
                    }
                    PathOutcome::ShowHelp { child } => {
                        return WalkOutcome::PrintCommandHelp {
                            config: child,
                            name,
                        };
                    }
                    PathOutcome::RefreshSchema { child, child_dir } => {
                        return WalkOutcome::RefreshSchema {
                            config: child,
                            cmd_dir: child_dir,
                            cmd_name: name,
                        };
                    }
                    PathOutcome::Exec { child, child_dir } => {
                        return WalkOutcome::ExecCommand {
                            config: child,
                            preset: None,
                            tail: current_tail,
                            cmd_dir: child_dir,
                        };
                    }
                }
            }
            CommandKind::Preset => {
                let Some(encl) = enclosing.take() else {
                    return WalkOutcome::PresetAtTopLevel { name };
                };

                if current_tail.iter().any(|a| a == "--help" || a == "-h") {
                    let parent_label = current_dir
                        .file_name()
                        .and_then(|n| n.to_str())
                        .unwrap_or("")
                        .to_string();
                    return WalkOutcome::PrintPresetHelp {
                        config: Box::new(encl),
                        parent_label,
                        preset_name: name,
                    };
                }

                return WalkOutcome::ExecCommand {
                    config: Box::new(encl),
                    preset: Some(name),
                    tail: current_tail,
                    cmd_dir: current_dir,
                };
            }
        }
    }
}

// ── Tests ───────────────────────────────────────────────────────────────

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

    /// Minimal tempdir helper — avoids pulling in the `tempfile` crate.
    struct TempDir(PathBuf);

    impl TempDir {
        fn new() -> Self {
            let base = std::env::temp_dir();
            let unique = format!(
                "flodl-dispatch-{}-{}",
                std::process::id(),
                std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .map(|d| d.as_nanos())
                    .unwrap_or(0)
            );
            let dir = base.join(unique);
            std::fs::create_dir_all(&dir).expect("tempdir creation");
            Self(dir)
        }
        fn path(&self) -> &Path {
            &self.0
        }
    }

    impl Drop for TempDir {
        fn drop(&mut self) {
            let _ = std::fs::remove_dir_all(&self.0);
        }
    }

    /// Write a sub-command fdl.yml at `dir/sub/fdl.yml` with the given body.
    fn mkcmd(base: &Path, sub: &str, body: &str) -> PathBuf {
        let dir = base.join(sub);
        std::fs::create_dir_all(&dir).expect("mkcmd dir");
        std::fs::write(dir.join("fdl.yml"), body).expect("mkcmd write");
        dir
    }

    fn path_spec() -> CommandSpec {
        // Convention-default Path: no fields set, `kind()` returns Path.
        CommandSpec::default()
    }

    #[test]
    fn classify_descends_when_tail_names_nested_command() {
        let tmp = TempDir::new();
        mkcmd(
            tmp.path(),
            "ddp-bench",
            "entry: echo\ncommands:\n  quick:\n    options: { model: linear }\n",
        );
        let spec = path_spec();
        let tail = vec!["quick".to_string()];
        let out = classify_path_step(&spec, "ddp-bench", tmp.path(), &tail, None);
        match out {
            PathOutcome::Descend { new_name, .. } => assert_eq!(new_name, "quick"),
            _ => panic!("expected Descend, got something else"),
        }
    }

    #[test]
    fn classify_show_help_when_tail_has_flag() {
        let tmp = TempDir::new();
        mkcmd(tmp.path(), "sub", "entry: echo\n");
        let spec = path_spec();
        let tail = vec!["--help".to_string()];
        let out = classify_path_step(&spec, "sub", tmp.path(), &tail, None);
        assert!(matches!(out, PathOutcome::ShowHelp { .. }));
    }

    #[test]
    fn classify_show_help_short_flag() {
        let tmp = TempDir::new();
        mkcmd(tmp.path(), "sub", "entry: echo\n");
        let spec = path_spec();
        let tail = vec!["-h".to_string()];
        let out = classify_path_step(&spec, "sub", tmp.path(), &tail, None);
        assert!(matches!(out, PathOutcome::ShowHelp { .. }));
    }

    #[test]
    fn classify_refresh_schema() {
        let tmp = TempDir::new();
        mkcmd(tmp.path(), "sub", "entry: echo\n");
        let spec = path_spec();
        let tail = vec!["--refresh-schema".to_string()];
        let out = classify_path_step(&spec, "sub", tmp.path(), &tail, None);
        assert!(matches!(out, PathOutcome::RefreshSchema { .. }));
    }

    #[test]
    fn classify_exec_when_tail_has_no_known_token() {
        let tmp = TempDir::new();
        mkcmd(tmp.path(), "sub", "entry: echo\n");
        let spec = path_spec();
        let tail = vec!["--model".to_string(), "linear".to_string()];
        let out = classify_path_step(&spec, "sub", tmp.path(), &tail, None);
        assert!(matches!(out, PathOutcome::Exec { .. }));
    }

    #[test]
    fn classify_exec_when_tail_is_empty() {
        let tmp = TempDir::new();
        mkcmd(tmp.path(), "sub", "entry: echo\n");
        let spec = path_spec();
        let tail: Vec<String> = vec![];
        let out = classify_path_step(&spec, "sub", tmp.path(), &tail, None);
        assert!(matches!(out, PathOutcome::Exec { .. }));
    }

    #[test]
    fn classify_descend_wins_over_help_at_same_level() {
        // `fdl sub quick --help` must render help for `quick` (handled
        // one level deeper), not for `sub`. Descent wins over help at
        // the current step.
        let tmp = TempDir::new();
        mkcmd(
            tmp.path(),
            "sub",
            "entry: echo\ncommands:\n  quick:\n    options: { x: 1 }\n",
        );
        let spec = path_spec();
        let tail = vec!["quick".to_string(), "--help".to_string()];
        let out = classify_path_step(&spec, "sub", tmp.path(), &tail, None);
        assert!(matches!(out, PathOutcome::Descend { .. }));
    }

    #[test]
    fn classify_load_failed_when_no_child_fdl_yml() {
        let tmp = TempDir::new();
        let spec = path_spec();
        let tail: Vec<String> = vec![];
        let out = classify_path_step(&spec, "missing", tmp.path(), &tail, None);
        match out {
            PathOutcome::LoadFailed(msg) => assert!(msg.contains("no fdl.yml")),
            _ => panic!("expected LoadFailed, got something else"),
        }
    }

    #[test]
    fn classify_uses_explicit_path() {
        // Explicit `path:` overrides the convention default. Drop the
        // child fdl.yml under `actual/` and point `path:` there.
        let tmp = TempDir::new();
        mkcmd(tmp.path(), "actual", "entry: echo\n");
        let spec = CommandSpec {
            path: Some("actual".into()),
            ..Default::default()
        };
        let tail: Vec<String> = vec![];
        // `name` here is the command's label, not where we load from —
        // `actual/` is the real dir courtesy of `path:`.
        let out = classify_path_step(&spec, "label", tmp.path(), &tail, None);
        assert!(matches!(out, PathOutcome::Exec { .. }));
    }

    // ── walk_commands: outer walker ──────────────────────────────────────
    //
    // These drive the full walk from top-level down, asserting on the
    // terminal WalkOutcome variant. No processes are spawned — the walker
    // is pure, so tests stay fast and hermetic.

    /// Build a top-level `commands:` map by parsing a short YAML snippet.
    fn top_commands(yaml: &str) -> BTreeMap<String, CommandSpec> {
        #[derive(serde::Deserialize)]
        struct Root {
            #[serde(default)]
            commands: BTreeMap<String, CommandSpec>,
        }
        serde_yaml::from_str::<Root>(yaml)
            .expect("parse top-level commands")
            .commands
    }

    fn args(xs: &[&str]) -> Vec<String> {
        xs.iter().map(|s| s.to_string()).collect()
    }

    #[test]
    fn walk_top_level_run_returns_run_script() {
        let tmp = TempDir::new();
        let commands = top_commands("commands:\n  greet:\n    run: echo hello\n");
        let out = walk_commands("greet", &[], &commands, tmp.path(), None);
        match out {
            WalkOutcome::RunScript { command, docker, cwd } => {
                assert_eq!(command, "echo hello");
                assert!(docker.is_none());
                assert_eq!(cwd, tmp.path());
            }
            _ => panic!("expected RunScript"),
        }
    }

    #[test]
    fn walk_top_level_run_with_docker_preserves_service() {
        let tmp = TempDir::new();
        let commands = top_commands(
            "commands:\n  dev:\n    run: cargo test\n    docker: dev\n",
        );
        let out = walk_commands("dev", &[], &commands, tmp.path(), None);
        match out {
            WalkOutcome::RunScript { docker, .. } => {
                assert_eq!(docker.as_deref(), Some("dev"));
            }
            _ => panic!("expected RunScript with docker"),
        }
    }

    #[test]
    fn walk_run_with_help_prints_help_not_script() {
        let tmp = TempDir::new();
        let commands = top_commands(
            "commands:\n  test:\n    description: Run all CPU tests\n    run: cargo test\n    docker: dev\n",
        );
        let tail = args(&["--help"]);
        let out = walk_commands("test", &tail, &commands, tmp.path(), None);
        match out {
            WalkOutcome::PrintRunHelp {
                name,
                description,
                run,
                docker,
            } => {
                assert_eq!(name, "test");
                assert_eq!(description.as_deref(), Some("Run all CPU tests"));
                assert_eq!(run, "cargo test");
                assert_eq!(docker.as_deref(), Some("dev"));
            }
            _ => panic!("expected PrintRunHelp"),
        }
    }

    #[test]
    fn walk_run_with_short_help_prints_help() {
        let tmp = TempDir::new();
        let commands = top_commands("commands:\n  test:\n    run: cargo test\n");
        let tail = args(&["-h"]);
        let out = walk_commands("test", &tail, &commands, tmp.path(), None);
        assert!(matches!(out, WalkOutcome::PrintRunHelp { .. }));
    }

    #[test]
    fn walk_unknown_top_level_returns_unknown() {
        let tmp = TempDir::new();
        let commands = top_commands("commands:\n  greet:\n    run: echo hello\n");
        let out = walk_commands("nope", &args(&["arg"]), &commands, tmp.path(), None);
        match out {
            WalkOutcome::UnknownCommand { name } => assert_eq!(name, "nope"),
            _ => panic!("expected UnknownCommand"),
        }
    }

    #[test]
    fn walk_top_level_preset_errors_without_enclosing() {
        // A top-level command with preset-shaped fields (`options:`) but
        // neither `run:` nor `path:` has no enclosing CommandConfig to
        // borrow an `entry:` from — must error loudly.
        let tmp = TempDir::new();
        let commands = top_commands(
            "commands:\n  orphan:\n    options: { model: linear }\n",
        );
        let out = walk_commands("orphan", &[], &commands, tmp.path(), None);
        match out {
            WalkOutcome::PresetAtTopLevel { name } => assert_eq!(name, "orphan"),
            _ => panic!("expected PresetAtTopLevel"),
        }
    }

    #[test]
    fn walk_run_and_path_both_set_is_error() {
        let tmp = TempDir::new();
        let commands = top_commands(
            "commands:\n  bad:\n    run: echo hi\n    path: ./sub\n",
        );
        let out = walk_commands("bad", &[], &commands, tmp.path(), None);
        match out {
            WalkOutcome::Error(msg) => {
                assert!(msg.contains("bad"), "got: {msg}");
                assert!(msg.contains("both `run:` and `path:`"), "got: {msg}");
            }
            _ => panic!("expected Error"),
        }
    }

    #[test]
    fn walk_path_exec_at_one_level() {
        // Top-level `ddp-bench` path-kind → no further descent → Exec.
        let tmp = TempDir::new();
        mkcmd(tmp.path(), "ddp-bench", "entry: cargo run -p ddp-bench\n");
        let commands = top_commands("commands:\n  ddp-bench: {}\n");
        let tail = args(&["--seed", "42"]);
        let out = walk_commands("ddp-bench", &tail, &commands, tmp.path(), None);
        match out {
            WalkOutcome::ExecCommand {
                preset,
                tail: returned_tail,
                cmd_dir,
                ..
            } => {
                assert!(preset.is_none());
                assert_eq!(returned_tail, args(&["--seed", "42"]));
                assert_eq!(cmd_dir, tmp.path().join("ddp-bench"));
            }
            _ => panic!("expected ExecCommand"),
        }
    }

    #[test]
    fn walk_path_then_preset_at_two_levels() {
        // fdl.yml: commands: { ddp-bench: {} }  → path kind, convention
        // ddp-bench/fdl.yml: commands: { quick: { options: { model: linear } } }
        // Invocation: `fdl ddp-bench quick --epochs 5`
        // Expected: descend into ddp-bench, resolve `quick` as preset,
        // emit ExecCommand with preset=Some("quick"), tail=["--epochs","5"].
        let tmp = TempDir::new();
        mkcmd(
            tmp.path(),
            "ddp-bench",
            "entry: cargo run -p ddp-bench\n\
             commands:\n  quick:\n    options: { model: linear }\n",
        );
        let commands = top_commands("commands:\n  ddp-bench: {}\n");
        let tail = args(&["quick", "--epochs", "5"]);
        let out = walk_commands("ddp-bench", &tail, &commands, tmp.path(), None);
        match out {
            WalkOutcome::ExecCommand {
                preset,
                tail: returned_tail,
                cmd_dir,
                ..
            } => {
                assert_eq!(preset.as_deref(), Some("quick"));
                assert_eq!(returned_tail, args(&["--epochs", "5"]));
                assert_eq!(cmd_dir, tmp.path().join("ddp-bench"));
            }
            _ => panic!("expected ExecCommand with preset"),
        }
    }

    #[test]
    fn walk_path_then_path_then_preset_at_three_levels() {
        // Three-level walk: `fdl a b quick`.
        // tmp/fdl.yml             → commands: { a: {} }
        // tmp/a/fdl.yml           → commands: { b: {} }   + entry (required for preset parent)
        // tmp/a/b/fdl.yml         → commands: { quick: { options: { x: 1 } } } + entry
        let tmp = TempDir::new();
        mkcmd(
            tmp.path(),
            "a",
            "entry: echo a\ncommands:\n  b: {}\n",
        );
        // b is a sibling directory under a/
        let b_dir = tmp.path().join("a").join("b");
        std::fs::create_dir_all(&b_dir).unwrap();
        std::fs::write(
            b_dir.join("fdl.yml"),
            "entry: echo b\ncommands:\n  quick:\n    options: { x: 1 }\n",
        )
        .unwrap();
        let commands = top_commands("commands:\n  a: {}\n");
        let tail = args(&["b", "quick"]);
        let out = walk_commands("a", &tail, &commands, tmp.path(), None);
        match out {
            WalkOutcome::ExecCommand {
                preset, cmd_dir, ..
            } => {
                assert_eq!(preset.as_deref(), Some("quick"));
                assert_eq!(cmd_dir, b_dir);
            }
            _ => panic!("expected ExecCommand with preset at depth 3"),
        }
    }

    #[test]
    fn walk_path_child_missing_returns_error() {
        // Convention-default Path for `ghost`, but tmp/ghost/fdl.yml doesn't exist.
        let tmp = TempDir::new();
        let commands = top_commands("commands:\n  ghost: {}\n");
        let out = walk_commands("ghost", &[], &commands, tmp.path(), None);
        match out {
            WalkOutcome::Error(msg) => assert!(msg.contains("no fdl.yml"), "got: {msg}"),
            _ => panic!("expected Error(LoadFailed)"),
        }
    }

    #[test]
    fn walk_path_help_prints_command_help() {
        let tmp = TempDir::new();
        mkcmd(tmp.path(), "ddp-bench", "entry: echo\n");
        let commands = top_commands("commands:\n  ddp-bench: {}\n");
        let tail = args(&["--help"]);
        let out = walk_commands("ddp-bench", &tail, &commands, tmp.path(), None);
        match out {
            WalkOutcome::PrintCommandHelp { name, .. } => assert_eq!(name, "ddp-bench"),
            _ => panic!("expected PrintCommandHelp"),
        }
    }

    #[test]
    fn walk_preset_help_prints_preset_help() {
        // `fdl ddp-bench quick --help` — help applies to the preset, not
        // the enclosing command (descent wins at the classify level, then
        // Preset-kind with `--help` in the tail emits PrintPresetHelp).
        let tmp = TempDir::new();
        mkcmd(
            tmp.path(),
            "ddp-bench",
            "entry: echo\ncommands:\n  quick:\n    options: { x: 1 }\n",
        );
        let commands = top_commands("commands:\n  ddp-bench: {}\n");
        let tail = args(&["quick", "--help"]);
        let out = walk_commands("ddp-bench", &tail, &commands, tmp.path(), None);
        match out {
            WalkOutcome::PrintPresetHelp {
                parent_label,
                preset_name,
                ..
            } => {
                assert_eq!(preset_name, "quick");
                assert_eq!(parent_label, "ddp-bench");
            }
            _ => panic!("expected PrintPresetHelp"),
        }
    }

    #[test]
    fn walk_path_refresh_schema() {
        let tmp = TempDir::new();
        mkcmd(tmp.path(), "ddp-bench", "entry: echo\n");
        let commands = top_commands("commands:\n  ddp-bench: {}\n");
        let tail = args(&["--refresh-schema"]);
        let out = walk_commands("ddp-bench", &tail, &commands, tmp.path(), None);
        match out {
            WalkOutcome::RefreshSchema { cmd_name, .. } => {
                assert_eq!(cmd_name, "ddp-bench");
            }
            _ => panic!("expected RefreshSchema"),
        }
    }

    #[test]
    fn walk_env_propagates_to_child_overlay() {
        // Base child says entry=echo-base; env overlay fdl.ci.yml
        // overrides entry=echo-ci. After descent with env=Some("ci"),
        // the ExecCommand carries the overlaid config.
        let tmp = TempDir::new();
        let child = mkcmd(tmp.path(), "ddp-bench", "entry: echo-base\n");
        std::fs::write(child.join("fdl.ci.yml"), "entry: echo-ci\n").unwrap();
        let commands = top_commands("commands:\n  ddp-bench: {}\n");
        let out = walk_commands("ddp-bench", &[], &commands, tmp.path(), Some("ci"));
        match out {
            WalkOutcome::ExecCommand { config, .. } => {
                assert_eq!(config.entry.as_deref(), Some("echo-ci"));
            }
            _ => panic!("expected ExecCommand with env-overlaid entry"),
        }
    }

    #[test]
    fn walk_env_none_ignores_overlay() {
        // Same fixtures as above, but env=None — base must win.
        let tmp = TempDir::new();
        let child = mkcmd(tmp.path(), "ddp-bench", "entry: echo-base\n");
        std::fs::write(child.join("fdl.ci.yml"), "entry: echo-ci\n").unwrap();
        let commands = top_commands("commands:\n  ddp-bench: {}\n");
        let out = walk_commands("ddp-bench", &[], &commands, tmp.path(), None);
        match out {
            WalkOutcome::ExecCommand { config, .. } => {
                assert_eq!(config.entry.as_deref(), Some("echo-base"));
            }
            _ => panic!("expected ExecCommand with base entry"),
        }
    }
}