oxo-flow-cli 0.29.1

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
//! Backend pre-flight: BackendPreflightFailure, install hints, primary env spec, backend_preflight_failures (#948 P2-5).

use super::config_placeholder_values;
use super::findings::{known_modules_hint, undefined_config_findings};
use anyhow::{Context, Result};
use colored::Colorize;
use oxo_flow_core::config::WorkflowConfig;
use oxo_flow_core::dag::WorkflowDag;
use oxo_flow_core::rule::Rule;
use std::path::Path;

/// The shared pre-DAG gate `run` and `dry-run` both apply (issue #142 H1,
/// #112): a typo'd `{config.*}` key or an unknown `--module` must abort
/// before any file is touched, and the DAG builds with the same
/// placeholder expansion on both paths. Caller-specific `--json` failure
/// emission stays with the caller.
pub(crate) fn build_dag_and_targets(
    config: &WorkflowConfig,
    target: Vec<String>,
    module: &[String],
) -> Result<(WorkflowDag, Vec<String>)> {
    let e005 = undefined_config_findings(config);
    if !e005.is_empty() {
        anyhow::bail!(
            "workflow references undefined config variables — fix before running:\n  {}",
            e005.join("\n  ")
        );
    }

    let dag = WorkflowDag::from_rules_with_config(
        &config.rules,
        &config_placeholder_values(&config.config),
    )
    .context("failed to build workflow DAG")?;

    if let Err(type_errors) = oxo_flow_core::types::validate_workflow_types(config, &dag) {
        let findings = type_errors
            .iter()
            .map(ToString::to_string)
            .collect::<Vec<_>>()
            .join("\n  ");
        anyhow::bail!(
            "declared biological type contracts are violated — fix before running:\n  {findings}"
        );
    }

    // --module partial runs (issue #112 elasticity): each module name
    // resolves to its rules plus the host producers of its declared
    // concrete inputs; upstream DAG dependents come via the target
    // machinery below.
    let mut target = target;
    for m in module {
        match config.module_closure(m) {
            Some(names) => target.extend(names),
            None => anyhow::bail!(
                "unknown module '{m}' — known modules: {}",
                known_modules_hint(&config.module_rules)
            ),
        }
    }
    Ok((dag, target))
}

/// One consolidated backend-availability failure from the run pre-flight:
/// identical (backend, message) pairs across a cohort's instances collapse
/// into a single entry whose `count` says how many pending rules share it.
#[derive(Debug, PartialEq)]
pub(crate) struct BackendPreflightFailure {
    pub(crate) kind: String,
    pub(crate) message: String,
    pub(crate) example_rule: String,
    pub(crate) spec: String,
    pub(crate) count: usize,
}

impl std::fmt::Display for BackendPreflightFailure {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{kind}: {message} — required by {count}, e.g. `{example_rule}` (environment: {spec})",
            kind = self.kind,
            message = self.message,
            count = oxo_flow_core::plural(self.count, "pending rule", "pending rules"),
            example_rule = self.example_rule,
            spec = self.spec,
        )?;
        // A spec defect already names its remedy in the message; appending
        // the generic install hint would contradict it. Empty hint = none.
        let hint = backend_install_hint(self);
        if !hint.is_empty() {
            write!(f, "; {hint}")?;
        }
        Ok(())
    }
}

/// Remediation hint for a preflight failure. The failure MODE decides the
/// advice (issue #848): a missing pixi MANIFEST is a workflow defect —
/// advise creating/correcting the declared file; a missing backend BINARY
/// is an environment defect — advise installing the tool.
pub(crate) fn backend_install_hint(failure: &BackendPreflightFailure) -> &'static str {
    if failure.kind == "pixi" && failure.message.contains("not found") {
        return "create the manifest or correct the rule's `pixi` path (resolved relative to the workflow directory)";
    }
    // A spec defect (e.g. a conda YAML with an empty `dependencies:` list)
    // is a workflow error, not a missing tool — installing the backend
    // cannot fix it, and the message already names the remedy
    // (journey-power P4).
    if failure.message.contains("declares no dependencies") {
        return "";
    }
    match failure.kind.as_str() {
        "pixi" => "install pixi (https://pixi.sh) and ensure it is on PATH",
        "mamba" => "install mamba or micromamba and ensure it is on PATH",
        "conda" => "install conda and ensure it is on PATH",
        "docker" => "install/start Docker and ensure `docker` is on PATH",
        "singularity" => "install Singularity/Apptainer and ensure it is on PATH",
        "modules" => "ensure environment modules (modulecmd) is on PATH",
        _ => "make the environment backend available on PATH",
    }
}

/// Primary spec string for an environment, for the failure message.
pub(crate) fn primary_env_spec(env: &oxo_flow_core::rule::EnvironmentSpec) -> String {
    env.mamba
        .as_deref()
        .or(env.conda.as_deref())
        .or(env.pixi.as_deref())
        .or(env.docker.as_deref())
        .or(env.singularity.as_deref())
        .or(env.venv.as_deref())
        .map(str::to_string)
        .unwrap_or_else(|| env.modules.join(","))
}

/// Validate each unique environment spec among the pending rules once
/// (instances fan out one rule template × N samples, so the spec set is
/// tiny even for large cohorts) and collapse the failures: rules sharing
/// one broken spec merge into a single entry, and distinct specs failing
/// with the identical message (the backend-missing case) merge too, so
/// the operator fixes one root cause per line. First-failure order is
/// preserved for stable output.
pub(crate) fn backend_preflight_failures(
    pending: &[(String, oxo_flow_core::rule::EnvironmentSpec)],
    resolver: &oxo_flow_core::environment::EnvironmentResolver,
) -> Vec<BackendPreflightFailure> {
    // spec cache_key → failure slot (None = spec validated OK)
    let mut spec_slot: std::collections::HashMap<String, Option<usize>> =
        std::collections::HashMap::new();
    // (backend kind, message) → failure slot, for merging identical root causes
    let mut msg_slot: std::collections::HashMap<(String, String), usize> =
        std::collections::HashMap::new();
    let mut out: Vec<BackendPreflightFailure> = Vec::new();
    for (rule_name, env) in pending {
        let key = resolver.cache_key(env);
        match spec_slot.get(&key) {
            Some(Some(slot)) => {
                out[*slot].count += 1;
                continue;
            }
            Some(None) => continue, // identical spec, already validated OK
            None => {}
        }
        if let Err(err) = resolver.validate_spec(env) {
            let (kind, message) = match &err {
                oxo_flow_core::OxoFlowError::Environment { kind, message } => {
                    (kind.clone(), message.clone())
                }
                other => ("environment".to_string(), other.to_string()),
            };
            let slot = match msg_slot.get(&(kind.clone(), message.clone())) {
                Some(&slot) => {
                    out[slot].count += 1;
                    slot
                }
                None => {
                    out.push(BackendPreflightFailure {
                        kind: kind.clone(),
                        message: message.clone(),
                        example_rule: rule_name.clone(),
                        spec: primary_env_spec(env),
                        count: 1,
                    });
                    msg_slot.insert((kind, message), out.len() - 1);
                    out.len() - 1
                }
            };
            spec_slot.insert(key, Some(slot));
        } else {
            spec_slot.insert(key, None);
        }
    }
    out
}

/// Extract pending rules' environment specs (with workflow-relative paths resolved)
/// from a preview plan. Skipped rules are excluded because this run never invokes
/// their backend.
pub(crate) fn pending_rule_environments(
    plan: &[crate::commands::run_preview::PreviewRule],
    config: &WorkflowConfig,
    workflow_dir: &Path,
) -> Vec<(String, oxo_flow_core::rule::EnvironmentSpec)> {
    plan.iter()
        .filter(|entry| !entry.status.is_skip())
        .filter_map(|entry| {
            config.get_rule(&entry.name).map(|rule| {
                (
                    entry.name.clone(),
                    rule.environment.resolve_relative_paths(Some(workflow_dir)),
                )
            })
        })
        .filter(|(_, env)| !env.is_empty())
        .collect()
}

/// Validate backend availability before scheduling: pending rules whose backend
/// binary is missing fail fast with actionable install hints before executing any rules.
pub(crate) fn validate_pending_backends(
    pending_envs: &[(String, oxo_flow_core::rule::EnvironmentSpec)],
    resolver: &oxo_flow_core::environment::EnvironmentResolver,
) -> Result<()> {
    let env_failures = backend_preflight_failures(pending_envs, resolver);
    if env_failures.is_empty() {
        return Ok(());
    }
    let detail = env_failures
        .iter()
        .map(|f| format!("  - {f}"))
        .collect::<Vec<_>>()
        .join("\n");
    let n_backends = env_failures.len();
    let backend_desc =
        oxo_flow_core::plural(n_backends, "environment backend", "environment backends");
    let verb = if n_backends == 1 { "is" } else { "are" };
    anyhow::bail!(
        "{backend_desc} required by pending rules {verb} unavailable; no rules were run:\n{detail}"
    )
}

/// Fail fast before any rule runs if any rule's declared resource request
/// can never fit an explicit `--max-memory` or `--max-threads` cap.
pub(crate) fn validate_resource_budget(
    scheduled: &[&Rule],
    max_threads: Option<u32>,
    max_memory_mb: Option<u64>,
) -> Result<()> {
    let breaches =
        oxo_flow_core::scheduler::check_budget_feasibility(scheduled, max_threads, max_memory_mb);
    if breaches.is_empty() {
        return Ok(());
    }
    let detail = breaches
        .iter()
        .map(|b| format!("  - {b}"))
        .collect::<Vec<_>>()
        .join("\n");
    anyhow::bail!(
        "resource budget too small for {}; no rules were run:\n{}",
        oxo_flow_core::plural(breaches.len(), "rule", "rules"),
        detail
    )
}

/// Disk pre-flight (issue #75): warn when a rule's declared `resources.disk`
/// exceeds available free space in the workdir, or when the workdir starts below
/// the configured `[engine] min_free_disk` threshold (issue #843).
pub(crate) fn warn_disk_preflight(
    scheduled: &[&Rule],
    workdir: &Path,
    engine: Option<&oxo_flow_core::config::EngineConfig>,
) {
    let disk_warnings = oxo_flow_core::scheduler::validate_disk_requirements(scheduled, workdir);
    for warning in &disk_warnings {
        eprintln!("  {} {}", "Warning:".bold().yellow(), warning);
    }

    if engine.is_some_and(|e| e.min_free_disk.is_some()) {
        #[cfg(not(unix))]
        {
            eprintln!(
                "  {} [engine] min_free_disk is configured but free-space monitoring is not supported on this platform — the monitor will stay inactive.",
                "Warning:".bold().yellow()
            );
        }
        #[cfg(unix)]
        {
            if let Some((min_mb, _reclaim_mb)) = engine.and_then(|e| e.disk_thresholds())
                && let Some(start_mb) = oxo_flow_core::scheduler::check_available_disk_mb(workdir)
                && start_mb < min_mb
            {
                eprintln!(
                    "  {} workdir starts below [engine] min_free_disk: {}MB free, threshold {}MB — the run will hold dispatches until space is freed.",
                    "Warning:".bold().yellow(),
                    start_mb,
                    min_mb
                );
            }
        }
    }
}

/// Warn when either the workdir or the system temp directory has less than 1 GB free
/// before any rule dispatches (issue #75).
pub(crate) fn warn_low_free_space(workdir: &Path) {
    for (label, path) in [
        ("workdir", workdir),
        ("temp dir", std::env::temp_dir().as_path()),
    ] {
        if let Some(free_kb) = super::refs::free_kilobytes(path) {
            let free_gb = free_kb as f64 / 1024.0 / 1024.0;
            if free_gb < 1.0 {
                tracing::warn!(
                    label,
                    path = %path.display(),
                    free_gb = format!("{free_gb:.1}"),
                    "less than 1 GB free disk space — the run may fail with ENOSPC mid-way"
                );
            }
        }
    }
}

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

    #[test]
    fn dag_preflight_rejects_declared_type_contract_violations() {
        let config = WorkflowConfig::parse(
            r#"
            [workflow]
            name = "type_gate"

            [types.raw_sam]
            format = "sam"

            [types.indexed_bam]
            format = "bam"
            index = "bai"

            [[rules]]
            name = "align"
            output = ["out.sam"]
            shell = "touch out.sam"
            [rules.types]
            outputs = { default = "raw_sam" }

            [[rules]]
            name = "call"
            input = ["out.sam"]
            output = ["calls.vcf"]
            shell = "touch calls.vcf"
            [rules.types]
            inputs = { default = "indexed_bam" }
            "#,
        )
        .unwrap();

        let err = build_dag_and_targets(&config, Vec::new(), &[]).unwrap_err();
        let message = err.to_string();
        assert!(message.contains("biological type contracts"), "{message}");
        assert!(message.contains("TYPE-001"), "{message}");
        assert!(message.contains("TYPE-003"), "{message}");
    }

    #[test]
    fn validate_resource_budget_accepts_feasible_rules() {
        let rule = Rule {
            name: "align".to_string(),
            resources: oxo_flow_core::rule::Resources {
                threads: 4,
                memory: Some("4G".to_string()),
                ..Default::default()
            },
            ..Default::default()
        };

        assert!(validate_resource_budget(&[&rule], Some(8), Some(8192)).is_ok());
    }

    #[test]
    fn validate_resource_budget_rejects_excessive_requirements() {
        let rule = Rule {
            name: "heavy_align".to_string(),
            resources: oxo_flow_core::rule::Resources {
                threads: 16,
                ..Default::default()
            },
            ..Default::default()
        };

        let err = validate_resource_budget(&[&rule], Some(8), None).unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("resource budget too small for 1 rule"));
        assert!(msg.contains("heavy_align"));
    }

    #[test]
    fn backend_install_hint_routes_by_kind_and_message() {
        let missing_manifest = BackendPreflightFailure {
            kind: "pixi".to_string(),
            message: "manifest not found".to_string(),
            example_rule: "rule_a".to_string(),
            spec: "pixi.toml".to_string(),
            count: 1,
        };
        assert!(backend_install_hint(&missing_manifest).contains("create the manifest"));

        let missing_binary = BackendPreflightFailure {
            kind: "conda".to_string(),
            message: "conda binary unavailable".to_string(),
            example_rule: "rule_b".to_string(),
            spec: "environment.yaml".to_string(),
            count: 1,
        };
        assert!(backend_install_hint(&missing_binary).contains("install conda"));

        let empty_conda_deps = BackendPreflightFailure {
            kind: "conda".to_string(),
            message: "environment file 'empty.yaml' declares no dependencies".to_string(),
            example_rule: "rule_c".to_string(),
            spec: "empty.yaml".to_string(),
            count: 1,
        };
        assert!(
            backend_install_hint(&empty_conda_deps).is_empty(),
            "a spec defect must not be answered with an install hint"
        );
    }
    #[test]
    fn pending_rule_environments_filters_skipped_and_empty() {
        let mut config = WorkflowConfig::parse(
            r#"
[workflow]
name = "test"
"#,
        )
        .unwrap();
        let r1 = Rule {
            name: "r1".to_string(),
            environment: oxo_flow_core::rule::EnvironmentSpec {
                pixi: Some("pixi.toml".to_string()),
                ..Default::default()
            },
            ..Default::default()
        };
        let r2 = Rule {
            name: "r2".to_string(),
            environment: oxo_flow_core::rule::EnvironmentSpec {
                conda: Some("env.yaml".to_string()),
                ..Default::default()
            },
            ..Default::default()
        };
        let r3 = Rule {
            name: "r3".to_string(),
            ..Default::default()
        };

        config.rules = vec![r1, r2, r3];

        let plan = vec![
            crate::commands::run_preview::PreviewRule {
                name: "r1".to_string(),
                status: crate::commands::run_preview::RuleStatus::NeverCompleted,
            },
            crate::commands::run_preview::PreviewRule {
                name: "r2".to_string(),
                status: crate::commands::run_preview::RuleStatus::Skipped,
            },
            crate::commands::run_preview::PreviewRule {
                name: "r3".to_string(),
                status: crate::commands::run_preview::RuleStatus::NeverCompleted,
            },
        ];

        let wf_dir = Path::new("/workflows");
        let pending = pending_rule_environments(&plan, &config, wf_dir);
        assert_eq!(pending.len(), 1);
        assert_eq!(pending[0].0, "r1");
        assert_eq!(
            pending[0].1.pixi.as_deref(),
            Some(Path::new("/workflows/pixi.toml").to_str().unwrap())
        );
    }

    fn pixi_env(manifest: &str) -> oxo_flow_core::rule::EnvironmentSpec {
        oxo_flow_core::rule::EnvironmentSpec {
            pixi: Some(manifest.to_string()),
            ..Default::default()
        }
    }

    #[test]
    fn backend_preflight_passes_empty_and_empty_specs() {
        let resolver = oxo_flow_core::environment::EnvironmentResolver::new();
        // Nothing pending → nothing to validate.
        assert!(backend_preflight_failures(&[], &resolver).is_empty());
        // A rule with no environment declaration never invokes a backend.
        let no_env = vec![("qc".to_string(), Default::default())];
        assert!(backend_preflight_failures(&no_env, &resolver).is_empty());
    }

    #[test]
    fn backend_preflight_reports_missing_pixi_manifest_once_per_unique_spec() {
        // Two pending instances of one rule template share the same spec:
        // validate it once and collapse the failures, keeping the FIRST
        // pending rule in execution order as the example (deterministic).
        let resolver = oxo_flow_core::environment::EnvironmentResolver::new();
        let pending = vec![
            ("trim_S1".to_string(), pixi_env("envs/absent/pixi.toml")),
            ("trim_S2".to_string(), pixi_env("envs/absent/pixi.toml")),
        ];
        let failures = backend_preflight_failures(&pending, &resolver);
        assert_eq!(
            failures.len(),
            1,
            "identical specs must group: {failures:?}"
        );
        let f = &failures[0];
        assert_eq!(f.kind, "pixi");
        assert_eq!(f.count, 2);
        assert_eq!(f.example_rule, "trim_S1");
        assert_eq!(f.spec, "envs/absent/pixi.toml");
        let rendered = f.to_string();
        assert!(
            rendered.contains("required by 2 pending rules"),
            "the message must carry the cohort size: {rendered}"
        );
        // Whether THIS test observes the manifest mode depends on whether
        // the pixi binary happens to be on PATH (validate_spec checks the
        // binary first); the hint split itself is covered without that
        // dependency by backend_install_hint_splits_manifest_not_found…
    }

    #[test]
    fn backend_install_hint_splits_manifest_not_found_from_missing_binary() {
        // Issue #848: the failure MODE decides the advice. A missing pixi
        // MANIFEST is a workflow defect — the hint must point at the file,
        // not tell the operator to install pixi; a missing backend BINARY
        // keeps the install advice. Constructed failures keep this test
        // independent of the test sandbox's PATH.
        let make = |message: &str| BackendPreflightFailure {
            kind: "pixi".to_string(),
            message: message.to_string(),
            example_rule: "trim_S1".to_string(),
            spec: "envs/absent/pixi.toml".to_string(),
            count: 2,
        };
        let manifest = backend_install_hint(&make(
            "pixi manifest 'envs/absent/pixi.toml' not found — the rule declares it via `pixi = \"envs/absent/pixi.toml\"` (resolved relative to the workflow directory)",
        ));
        assert_eq!(
            manifest,
            "create the manifest or correct the rule's `pixi` path (resolved relative to the workflow directory)"
        );
        let binary = backend_install_hint(&make("pixi is not installed or not in PATH"));
        assert_eq!(
            binary,
            "install pixi (https://pixi.sh) and ensure it is on PATH"
        );
        // The mode discriminator keys on "not found", which only the
        // manifest message carries — the binary message must not trip it.
        assert!(!"pixi is not installed or not in PATH".contains("not found"));
    }

    #[test]
    fn backend_preflight_missing_backend_binary_still_advises_install() {
        // Issue #848: the OTHER failure mode — pixi the BINARY is absent —
        // keeps the install advice. validate_spec checks the binary first,
        // so an unresolvable spec with pixi absent from PATH yields the
        // "not installed" message, distinct from the manifest check.
        let resolver = oxo_flow_core::environment::EnvironmentResolver::new();
        let pending = vec![("trim_S1".to_string(), pixi_env("envs/absent/pixi.toml"))];
        let failures = backend_preflight_failures(&pending, &resolver);
        // The exact mode depends on whether pixi is on PATH in the test
        // sandbox; assert the hint MATCHES whichever mode fired.
        let rendered = failures[0].to_string();
        if failures[0].message.contains("not found") {
            assert!(
                rendered.contains("create the manifest"),
                "manifest mode must carry the file advice: {rendered}"
            );
        } else {
            assert!(
                rendered.contains("install pixi"),
                "binary mode must keep the install advice: {rendered}"
            );
        }
    }
}