fluxframe-cli 0.13.0

fluxframe CLI binary: list, check, run, benchmark.
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
//! Named-preset resolution and pipeline construction.
//!
//! Single source of truth for the `--preset NAME` flag, shared between
//! `fluxframe run` and `fluxframe check` so they always agree on
//! diagnostics and feature-gated behaviour.
//!
//! Two responsibilities live here:
//!
//! 1. [`resolve`] — turn an optional `--preset NAME` into a
//!    `(name, &Preset)` pair, with a structured §27 error when the
//!    requested preset (or the implicit `default`) is missing.
//! 2. [`build_chain`] — turn a `&Preset` into an [`EffectChain`],
//!    enforcing the configuration invariants both subcommands rely on
//!    (bg/fg without mask is a config bug; mask without the `ml`
//!    feature is a build mismatch).

use fluxframe_core::context::ProcessingContext;
use fluxframe_core::paths::ConfigBase;
use fluxframe_core::traits::VideoEffect;
use fluxframe_core::{FluxConfig, FluxError, Preset};
use fluxframe_effects::{EffectChain, PassthroughEffect};
use tracing::{info, warn};

/// Default preset name looked up when `--preset` is omitted.
pub const DEFAULT_PRESET_NAME: &str = "default";

/// Resolve the preset selected by the CLI.
///
/// Rules:
/// - `--preset NAME` given → exact lookup; structured error with the
///   list of available presets if missing.
/// - `--preset` omitted → [`DEFAULT_PRESET_NAME`]; structured error
///   pointing at the missing default if absent.
///
/// Returns the resolved name *and* a reference to the preset itself so
/// callers do not have to re-enter `cfg.presets.get(...)` later (and
/// the existence proof is carried in the type).
///
/// # Errors
///
/// Returns [`FluxError::Config`] when the requested preset (or the
/// implicit `default`) does not exist in `cfg.presets`.
pub fn resolve<'a>(
    cfg: &'a FluxConfig,
    requested: Option<&str>,
) -> Result<(&'a str, &'a Preset), FluxError> {
    let name = requested.unwrap_or(DEFAULT_PRESET_NAME);
    if let Some((key, preset)) = cfg.presets.get_key_value(name) {
        return Ok((key.as_str(), preset));
    }
    Err(resolution_error(cfg, requested))
}

/// Build the structured error returned when a preset cannot be
/// resolved. Centralised so both `run` and `check` produce identical
/// diagnostics.
fn resolution_error(cfg: &FluxConfig, requested: Option<&str>) -> FluxError {
    let available: Vec<&str> = cfg.presets.keys().map(String::as_str).collect();
    let reason = match requested {
        Some(name) => format!("preset '{name}' is not defined in the config"),
        None => format!("no --preset given and no '{DEFAULT_PRESET_NAME}' preset in the config"),
    };
    let hint = if available.is_empty() {
        format!("add a [presets.{DEFAULT_PRESET_NAME}] section to your fluxframe.toml")
    } else {
        format!("available presets: {}", available.join(", "))
    };
    FluxError::Config {
        reason,
        hint: Some(hint),
    }
}

/// A preset whose chain is built and prepared: the one asked for, or
/// a fallback of it ([`prepare_with_fallback`]).
pub(crate) struct PreparedPreset {
    pub(crate) chain: EffectChain,
    pub(crate) name: String,
    pub(crate) preset: Preset,
}

/// Build and prepare the chain of preset `name`.  While a preset cannot
/// run for lack of an accelerator and names a `fallback_preset`, the
/// fallback is tried instead (fallback presets come from `cfg`, saved
/// state).
///
/// # Errors
///
/// The last preset tried fails to build or prepare, or a fallback is
/// missing from `cfg` or revisits a preset (both rejected by config
/// validation).
pub(crate) fn prepare_with_fallback(
    cfg: &FluxConfig,
    config_base: &ConfigBase,
    name: &str,
    preset: &Preset,
    ctx: &ProcessingContext,
) -> Result<PreparedPreset, FluxError> {
    let mut name = name.to_string();
    let mut preset = preset.clone();
    let mut tried = Vec::new();
    loop {
        let mut chain = build_chain(&name, &preset, config_base)?;
        let err = match chain.prepare_all(ctx) {
            Ok(()) => {
                return Ok(PreparedPreset {
                    chain,
                    name,
                    preset,
                });
            }
            Err(e) => e,
        };
        let fallback = preset
            .fallback_preset
            .clone()
            .filter(|_| err.is_accelerator_unavailable());
        let Some(fallback) = fallback else {
            return Err(err.into());
        };
        tried.push(name);
        if tried.contains(&fallback) {
            return Err(FluxError::Config {
                reason: format!("fallback_preset cycle through '{fallback}'"),
                hint: None,
            });
        }
        let next = cfg
            .presets
            .get(&fallback)
            .ok_or_else(|| FluxError::Config {
                reason: format!("fallback_preset '{fallback}' is not defined"),
                hint: None,
            })?;
        warn!(
            preset = %tried.last().map_or("", String::as_str),
            fallback = %fallback,
            error = %err,
            "preset cannot run on this machine; switching to its fallback preset",
        );
        name = fallback;
        preset = next.clone();
    }
}

/// Build the [`EffectChain`] for the given preset.
///
/// - Preset with `[mask]` → composite chain (segmentation + sub-chains).
/// - Preset with no `[mask]`, no `[background]`, no `[foreground]` →
///   single [`PassthroughEffect`] so the chain stays non-empty.
/// - Anything else → structured error (bg/fg without mask is a config
///   bug regardless of feature flags; mask without `ml` is a build
///   mismatch).
///
/// Relative paths in the preset (the model, image parameters) are
/// resolved against `config_base`.
///
/// # Errors
///
/// Returns [`FluxError::Config`] when the preset declares
/// `[background]` or `[foreground]` without a `[mask]` section (those
/// sub-pipelines are only wired into the composite, so they would be a
/// silent no-op otherwise), or when the preset declares `[mask]` on a
/// build compiled without the `ml` feature. Composite-builder failures
/// (unknown effect names, malformed per-effect TOML, missing
/// `mask.model`, ...) propagate via `FluxError::from(EffectError)`.
pub fn build_chain(
    name: &str,
    preset: &Preset,
    config_base: &ConfigBase,
) -> Result<EffectChain, FluxError> {
    reject_planes_without_mask(name, preset)?;
    if preset.mask.is_some() {
        return build_composite_chain(name, preset, config_base);
    }
    Ok(build_passthrough_chain(name))
}

/// Hard-reject `[background]`/`[foreground]` declared without a
/// `[mask]` section.
///
/// The composite is only assembled when `mask` is present and the
/// plane sub-chains are *only* wired into the composite, so a preset
/// with bg/fg-only would silently drop the operator's configuration.
/// Surface that as a structured error in both `run` and `check`,
/// independent of the `ml` feature.
fn reject_planes_without_mask(name: &str, preset: &Preset) -> Result<(), FluxError> {
    if preset.mask.is_some() {
        return Ok(());
    }
    if preset.background.is_none() && preset.foreground.is_none() {
        return Ok(());
    }
    let mut sections: Vec<&'static str> = Vec::new();
    if preset.background.is_some() {
        sections.push("background");
    }
    if preset.foreground.is_some() {
        sections.push("foreground");
    }
    Err(FluxError::Config {
        reason: format!(
            "preset '{name}' declares [{}] but no [mask] section; \
             plane sub-pipelines are only applied through the composite \
             (which requires [mask])",
            sections.join("] / ["),
        ),
        hint: Some(
            "add a [mask] section with a segmentation `model = \"...\"` path, \
             or remove the background/foreground sections"
                .into(),
        ),
    })
}

/// Build a single-element chain wrapping a [`PassthroughEffect`] so
/// the runtime always sees a non-empty chain.
fn build_passthrough_chain(name: &str) -> EffectChain {
    info!(
        preset = %name,
        "preset has no [mask] section — running passthrough chain",
    );
    let effects: Vec<Box<dyn VideoEffect>> = vec![Box::new(PassthroughEffect::new())];
    EffectChain::new(effects)
}

/// Build a single-element chain wrapping the [`composite`] effect.
///
/// Returns a structured error on builds compiled without the `ml`
/// feature so the diagnostic matches the one `check` would print
/// (rather than the previous silent fall-through to passthrough).
///
/// [`composite`]: fluxframe_effects::composite
#[cfg(feature = "ml")]
fn build_composite_chain(
    name: &str,
    preset: &Preset,
    config_base: &ConfigBase,
) -> Result<EffectChain, FluxError> {
    use fluxframe_effects::composite::CompositeBuilder;
    use fluxframe_effects::{mask_effects, plane_effects, post_effects};

    // Unwrap is safe: `build_chain` only calls this branch after the
    // `preset.mask.is_some()` check.
    let mask_section = preset
        .mask
        .as_ref()
        .expect("build_composite_chain called without a [mask] section");
    let mask_registry = mask_effects::default_registry();
    let plane_registry = plane_effects::default_registry();
    let post_registry = post_effects::default_registry();
    let composite = CompositeBuilder::new(&mask_registry, &plane_registry, &post_registry)
        .build(
            mask_section,
            preset.background.as_ref(),
            preset.foreground.as_ref(),
            preset.post.as_ref(),
            config_base,
        )
        .map_err(FluxError::from)?;
    info!(
        preset = %name,
        "preset has a [mask] section — running composite chain",
    );
    let effects: Vec<Box<dyn VideoEffect>> = vec![Box::new(composite)];
    Ok(EffectChain::new(effects))
}

#[cfg(not(feature = "ml"))]
fn build_composite_chain(
    name: &str,
    _preset: &Preset,
    _config_base: &ConfigBase,
) -> Result<EffectChain, FluxError> {
    Err(FluxError::Config {
        reason: format!(
            "preset '{name}' has a [mask] sub-section but the binary was built \
             without the `ml` feature"
        ),
        hint: Some(
            "rebuild with `--features fluxframe-effects/ml` or remove the mask \
             section from the preset"
                .into(),
        ),
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    #[cfg(feature = "ml")]
    use crate::test_support::UnrunnableModel;
    use fluxframe_core::PipelineSection;

    fn cfg_with_preset(name: &str, body: &str) -> FluxConfig {
        let text = format!("[presets.{name}]\n{body}");
        FluxConfig::from_toml_str(&text).expect("preset toml parses")
    }

    // ---------- resolve ----------

    #[test]
    fn resolve_default_when_none_given() {
        let cfg = cfg_with_preset(DEFAULT_PRESET_NAME, "");
        let (name, _preset) = resolve(&cfg, None).expect("default resolves");
        assert_eq!(name, DEFAULT_PRESET_NAME);
    }

    #[test]
    fn resolve_fails_when_no_default_and_no_flag() {
        let cfg = cfg_with_preset("custom", "");
        let err = resolve(&cfg, None).expect_err("missing default must surface an error");
        let msg = format!("{err}");
        assert!(
            msg.contains(DEFAULT_PRESET_NAME),
            "expected '{DEFAULT_PRESET_NAME}' mention: {msg}",
        );
    }

    #[test]
    fn resolve_fails_when_named_preset_missing() {
        let cfg = cfg_with_preset(DEFAULT_PRESET_NAME, "");
        let err = resolve(&cfg, Some("nope")).expect_err("missing requested preset must error");
        let msg = format!("{err}");
        assert!(msg.contains("'nope'"), "expected requested name: {msg}");
    }

    #[test]
    fn resolve_picks_explicit_flag() {
        let cfg = cfg_with_preset("custom", "");
        let (name, _preset) = resolve(&cfg, Some("custom")).expect("custom resolves");
        assert_eq!(name, "custom");
    }

    // ---------- build_chain ----------

    #[test]
    fn build_chain_falls_back_to_passthrough_when_empty() {
        let preset = Preset::default();
        let chain = build_chain("raw", &preset, &ConfigBase::default())
            .expect("empty preset → passthrough");
        assert_eq!(chain.len(), 1);
        assert_eq!(chain.names(), vec![PassthroughEffect::NAME]);
    }

    #[test]
    fn build_chain_rejects_bg_without_mask() {
        let preset = Preset {
            background: Some(PipelineSection::default()),
            ..Preset::default()
        };
        // `EffectChain` is not `Debug`, so use a `let-else` instead of
        // `expect_err` to keep the panic message useful.
        let Err(err) = build_chain("bg-only", &preset, &ConfigBase::default()) else {
            panic!("bg without mask must surface as a config error");
        };
        let msg = format!("{err}");
        assert!(msg.contains("background"), "got: {msg}");
        assert!(msg.contains("mask"), "got: {msg}");
    }

    #[test]
    fn build_chain_rejects_fg_without_mask() {
        let preset = Preset {
            foreground: Some(PipelineSection::default()),
            ..Preset::default()
        };
        let Err(err) = build_chain("fg-only", &preset, &ConfigBase::default()) else {
            panic!("fg without mask must surface as a config error");
        };
        let msg = format!("{err}");
        assert!(msg.contains("foreground"), "got: {msg}");
        assert!(msg.contains("mask"), "got: {msg}");
    }

    /// Locate the repository's bundled selfie-segmentation model. The
    /// composite builder reads `model = "..."` from the preset and
    /// stores the path, but it does NOT load the file at build time
    /// (loading happens lazily in `prepare`), so any path will do for
    /// a build-only smoke test — we use the real file to stay close to
    /// production reality.
    #[cfg(feature = "ml")]
    fn segmentation_model_path() -> std::path::PathBuf {
        std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .join("../../models/selfie_segmentation.onnx")
    }

    #[cfg(feature = "ml")]
    #[test]
    fn build_chain_with_mask_returns_composite_chain() {
        use fluxframe_effects::CompositeEffect;
        let mask = PipelineSection {
            model: Some(segmentation_model_path()),
            ..PipelineSection::default()
        };
        let preset = Preset {
            mask: Some(mask),
            ..Preset::default()
        };

        let Ok(chain) = build_chain("composite", &preset, &ConfigBase::default()) else {
            panic!("mask present → composite chain should build");
        };
        // Composite collapses the whole sub-pipeline into a single
        // `VideoEffect`, so the outer chain length is exactly 1 and
        // the single effect's name is the composite identifier (not
        // the passthrough fallback).
        assert_eq!(chain.len(), 1);
        assert_eq!(chain.names(), vec![CompositeEffect::NAME]);
    }

    #[cfg(not(feature = "ml"))]
    #[test]
    fn build_chain_rejects_mask_without_ml_feature() {
        let mask = PipelineSection {
            model: Some(std::path::PathBuf::from("/tmp/dummy.onnx")),
            ..PipelineSection::default()
        };
        let preset = Preset {
            mask: Some(mask),
            ..Preset::default()
        };

        let Err(err) = build_chain("composite", &preset, &ConfigBase::default()) else {
            panic!("mask without ml feature must surface as a config error");
        };
        let msg = format!("{err}");
        assert!(msg.contains("ml"), "expected 'ml' mention: {msg}");
    }

    // ---------- prepare_with_fallback ----------

    /// A model restricted to a device no machine has: preparing it is
    /// always `AcceleratorUnavailable`, whatever the build and host.
    fn rgb_ctx() -> ProcessingContext {
        ProcessingContext {
            width: 8,
            height: 8,
            format: fluxframe_core::frame::PixelFormat::Rgb,
            fps: 30,
            counters: None,
        }
    }

    #[cfg(feature = "ml")]
    #[test]
    fn prepare_falls_back_when_no_accelerator_can_run_the_model() {
        let model = UnrunnableModel::new("switch");
        let cfg = model.config(Some("plain"));
        let prepared = prepare_with_fallback(
            &cfg,
            &ConfigBase::default(),
            "accel",
            &cfg.presets["accel"],
            &rgb_ctx(),
        )
        .expect("the fallback runs");
        assert_eq!(prepared.name, "plain");
        assert_eq!(prepared.preset, cfg.presets["plain"]);
    }

    #[cfg(feature = "ml")]
    #[test]
    fn prepare_without_fallback_reports_the_accelerator_error() {
        let model = UnrunnableModel::new("nofallback");
        let cfg = model.config(None);
        let err = prepare_with_fallback(
            &cfg,
            &ConfigBase::default(),
            "accel",
            &cfg.presets["accel"],
            &rgb_ctx(),
        )
        .err()
        .expect("no fallback to run");
        assert!(err.to_string().contains("NO_SUCH_DEVICE"), "{err}");
    }

    #[test]
    fn prepare_runs_a_preset_that_works_as_is() {
        let cfg = cfg_with_preset("plain", "fallback_preset = \"other\"\n[presets.other]\n");
        let prepared = prepare_with_fallback(
            &cfg,
            &ConfigBase::default(),
            "plain",
            &cfg.presets["plain"],
            &rgb_ctx(),
        )
        .expect("runs");
        assert_eq!(prepared.name, "plain");
    }
}