mahbot 0.11.1

An autonomous agentic engineering system that manages software development through role separation, subagents, and deterministic diagnostics.
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
//! `mahbot audio` — the product's own speech engines on the command line.
//!
//! Two operations, both the product's own engines and nothing else: `transcribe` turns an
//! existing recording into text on standard output, `speak` turns text into a spoken WAV file.
//! Both run on every platform. Neither needs the running product — this command line is
//! dispatched before the instance lock — and neither touches its stores or settings: both read
//! the machine-wide speech model folder ([`crate::speech`]), each set obtained the way the
//! product obtains it (once, saying so on standard error, no retry loop). A recording in a format
//! this machine has no decoder of its own for is converted by the product's own copy of its media
//! toolchain — the copy already installed, never one fetched here.
//!
//! The product's speech switches are not this command's: they govern what the app says aloud
//! and transcribes, while these operations are explicit requests carrying their own
//! destination — nothing is read from the settings and nothing changed there. `speak` renders
//! through the very path the app speaks a phrase through, so a file made here sounds like the
//! product speaking; the whole text is spoken, and a text the engine cannot speak to its end
//! fails the command rather than being cut short.
//!
//! The four exit classes are the product's own ([`crate::cli::Exit`]), and what separates them
//! is who judges. This command judges the words and the values they carry (a flag no operation
//! takes, a destination it cannot write, a recording the recogniser does not take) — the caller's
//! own misuse. What the ENGINE judges is a failed operation. What is missing to run at all is the
//! environment: a model folder that is not there or cannot be written, or the media toolchain a
//! recording's format is read through.
//!
//! The order is deliberate: the words first (an operation no word names, a flag this operation
//! does not take, one given twice, one with no value), then what the caller handed over, then
//! the work itself — because a typo must not cost a model download. The models are obtained
//! only after all of that, the work is done, and only then is anything printed or written.
//!
//! A read-only shell must never admit this family: `speak` writes a file at a path the caller
//! names, so the guard the inspecting roles hold lists it among its refusals.

use std::fmt::Write as _;
use std::path::PathBuf;

use crate::cli::{Exit, grammar};
use crate::util::{print_stderr, print_stdout};

// One flag of one operation: the value it carries and the line the help shows.
struct Flag {
    /// The spelling without its dashes (`--text` is `text`).
    name: &'static str,
    /// Whether the caller has to give it — the help says so beside the flag, and [`parse`] refuses
    /// its absence before reading any value.
    required: bool,
    /// One line for the operation's help.
    purpose: &'static str,
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum Operation {
    /// An existing recording into text.
    Transcribe,
    /// Text into a spoken file.
    Speak,
}

impl Operation {
    const ALL: [Self; 2] = [Self::Transcribe, Self::Speak];

    const fn word(self) -> &'static str {
        match self {
            Self::Transcribe => "transcribe",
            Self::Speak => "speak",
        }
    }

    const fn purpose(self) -> &'static str {
        match self {
            Self::Transcribe => "Turn a recording into text, printed on standard output",
            Self::Speak => "Turn text into a spoken WAV file",
        }
    }

    const fn usage(self) -> &'static str {
        match self {
            Self::Transcribe => "mahbot audio transcribe --input <file>",
            Self::Speak => "mahbot audio speak --text <text> --output <path>",
        }
    }

    /// The flags this operation takes — one list for the parser and the help both, so neither can
    /// name a flag the other does not read, nor disagree about which are required.
    const fn flags(self) -> &'static [Flag] {
        match self {
            Self::Transcribe => &[Flag {
                name: "input",
                required: true,
                purpose: "The recording to turn into text — any format the product reads as audio, \
                          by name or by content",
            }],
            Self::Speak => &[
                Flag {
                    name: "text",
                    required: true,
                    purpose: "The text to speak",
                },
                Flag {
                    name: "output",
                    required: true,
                    purpose: "Where to write the file: a name ending in '.wav', or an existing \
                              directory to have one named for you",
                },
            ],
        }
    }

    fn from_word(word: &str) -> Option<Self> {
        Self::ALL
            .into_iter()
            .find(|operation| operation.word() == word)
    }
}

/// One command line, read: what was asked for, or an end to it.
enum Invocation {
    /// `mahbot audio -h` — this command's own help.
    Help,
    /// `mahbot audio <operation> -h` — that operation's help.
    OperationHelp(Operation),
    Transcribe {
        input: PathBuf,
    },
    Speak(Speak),
}

/// What `speak` carries. The text is kept as the caller spelled it: the preparation the product
/// speaks through is applied to it, never the caller's words rewritten.
struct Speak {
    text: String,
    output: PathBuf,
}

// One operation's flag tail, read.

enum Tail {
    /// A `-h`/`--help` stood where a flag could — that operation's help.
    Help,
    /// The values given, in the order they were given.
    Values(Vec<(&'static str, String)>),
}

/// Read one operation's flag tail — the tokens after the operation word. The grammar is
/// [`crate::cli::grammar`]'s and a value is read VERBATIM: the very next token, whatever it looks
/// like (`--text -h` is the text `-h`). This command refuses only what is its own — a token that is
/// no flag, a flag this operation does not take, one given twice, one with no value — and names the
/// flag, never its value: a mistyped value is the caller's business.
fn parse_flags(operation: Operation, tokens: &[String]) -> Result<Tail, String> {
    let flags = operation.flags();
    let mut values: Vec<(&'static str, String)> = Vec::new();
    let mut rest = tokens.iter();
    while let Some(token) = rest.next() {
        let (name, inline) = match grammar::read_token(token)? {
            grammar::Token::Help => return Ok(Tail::Help),
            grammar::Token::Flag { name, inline } => (name, inline),
        };
        let Some(flag) = flags.iter().find(|flag| flag.name == name) else {
            return Err(grammar::unknown_flag(name, operation.word()));
        };
        if values.iter().any(|(given, _)| *given == flag.name) {
            return Err(grammar::repeated_flag(flag.name));
        }
        let value = match inline {
            Some(value) => value.to_string(),
            None => rest
                .next()
                .cloned()
                .ok_or_else(|| grammar::flag_value_missing(name))?,
        };
        values.push((flag.name, value));
    }
    Ok(Tail::Values(values))
}

fn parse(args: &[String]) -> Result<Invocation, String> {
    let Some(word) = args.first() else {
        return Err("no operation was given".to_string());
    };
    if grammar::is_help(word) {
        return Ok(Invocation::Help);
    }
    let Some(operation) = Operation::from_word(word) else {
        return Err(format!("unknown operation '{word}'"));
    };
    let values = match parse_flags(operation, &args[1..]) {
        Ok(Tail::Help) => return Ok(Invocation::OperationHelp(operation)),
        Ok(Tail::Values(values)) => values,
        Err(reason) => return Err(reason),
    };

    let given = |name: &str| {
        values
            .iter()
            .find(|(given, _)| *given == name)
            .map(|(_, value)| value.clone())
    };
    // The operation's own list says which flags are required, so the first one it calls required
    // and the tail lacks ends the parse here — which makes the lookups below safe: every required
    // value is present.
    if let Some(flag) = operation
        .flags()
        .iter()
        .find(|flag| flag.required && given(flag.name).is_none())
    {
        return Err(format!(
            "flag '--{}' is required for '{}'",
            flag.name,
            operation.word()
        ));
    }
    let required = |name: &str| given(name).expect("a required flag, checked just above");
    match operation {
        Operation::Transcribe => Ok(Invocation::Transcribe {
            input: PathBuf::from(required("input")),
        }),
        Operation::Speak => Ok(Invocation::Speak(Speak {
            text: required("text"),
            output: PathBuf::from(required("output")),
        })),
    }
}

// What these operations are — one block for both helps, so a caller entering through one
// operation's own help reads the same contract as one reading the family's.
const ENGINE_HELP: &str = "\nA frontend onto the product's own speech engines: everything happens \
     on this machine and nothing is sent anywhere. The engines need their model set and nothing \
     else — both read the machine-wide speech model folder — not the product's settings, not its \
     databases, and not the running product, so this works while it is stopped and running it \
     while the product is up does not disturb it. The first use of an operation fetches the \
     models it needs, once, saying so on standard error; if it cannot obtain them, it fails \
     rather than trying again.\n\
     \nA recording in a format this machine has no decoder of its own for is converted by the \
     product's own copy of its media toolchain first. That copy is used only where it is already \
     installed: this command installs nothing, and a machine without it refuses such a format \
     rather than fetching anything.\n\
     \nBoth operations run on every platform. The product's speech switches govern what the app \
     itself speaks and transcribes; these operations are explicit requests with their own \
     destination — they neither need those switches nor change them.\n";

/// The exit classes, word for word the numbering a script reads (see [`Exit`]).
const EXIT_CLASS_HELP: &str = "\nExit codes:\n  0  the operation was carried out\n  1  the engine \
     could not do the work\n  2  the environment — a component this command needs is not there: \
     the model set, or the media toolchain a recording's format is read through\n  3  this \
     command was misused — what was asked for cannot be done as asked\n\
     \nThe words are read before anything is fetched, so a malformed invocation is misuse and \
     never an environment failure.\n";

/// Where the two streams go — shared by both helps for the same reason.
const OUTPUT_HELP: &str = "\nOutput:\n  stdout  transcribe: the recognised text, and nothing at \
     all when the recording holds no speech; speak: the path of the file, when the destination \
     was a directory and this command named the file itself\n  stderr  everything this command \
     itself has to say\n";

/// `mahbot audio -h`: what this command is and what a caller reads back from it.
#[must_use]
fn top_help() -> String {
    let mut out =
        String::from("mahbot audio — the product's own speech engines on the command line\n");
    out.push_str("\nUsage: mahbot audio <operation> [flags]\n\nOperations:\n");
    let width = Operation::ALL
        .iter()
        .map(|operation| operation.word().len())
        .max()
        .unwrap_or_default();
    for operation in Operation::ALL {
        let _ = writeln!(
            out,
            "  {:<width$}  {}",
            operation.word(),
            operation.purpose()
        );
    }
    out.push_str(
        "\nA flag is `--<name> <value>` or `--<name>=<value>`, and its value is taken verbatim \
         (so a text may begin with a dash). `mahbot audio <operation> -h` names one operation's \
         flags in full.\n",
    );
    out.push_str(ENGINE_HELP);
    out.push_str(EXIT_CLASS_HELP);
    out.push_str(OUTPUT_HELP);
    out
}

/// `mahbot audio <operation> -h`: what one operation does and the flags it takes.
#[must_use]
fn operation_help(operation: Operation) -> String {
    let mut out = String::new();
    let _ = writeln!(
        out,
        "mahbot audio {} — {}",
        operation.word(),
        operation.purpose()
    );
    let _ = write!(out, "\nUsage: {}\n", operation.usage());
    out.push_str("\nFlags:\n");
    let flags = operation.flags();
    let rows: Vec<(String, &str)> = flags
        .iter()
        .map(|flag| {
            let required = if flag.required { " (required)" } else { "" };
            (format!("--{} <value>{required}", flag.name), flag.purpose)
        })
        .chain(std::iter::once((
            "-h, --help".to_string(),
            "Print this help and exit",
        )))
        .collect();
    let width = rows
        .iter()
        .map(|(spelling, _)| spelling.len())
        .max()
        .unwrap_or_default();
    for (spelling, purpose) in rows {
        let _ = writeln!(out, "  {spelling:<width$}  {purpose}");
    }
    out.push_str("\nA flag's value is the token right after it, taken verbatim.\n");
    out.push_str(match operation {
        Operation::Transcribe => {
            "The product's own local recognition, and no language is named: the engine detects \
             it. A recording the engine finds no speech in is not a failure — stdout then carries \
             nothing at all, which is how a script tells it from an empty line. A recording \
             longer than the engine will take fails, never a transcript with a part of it \
             missing.\n"
        }
        Operation::Speak => {
            "The whole text is spoken as one reading, through the same preparation of the text \
             the product speaks with: it is not split, and no length is imposed here — a text the \
             engine cannot speak to its end fails rather than being cut short. The destination is \
             never overwritten, the directory it names must already exist, and nothing is written \
             at all if the text cannot be spoken.\n"
        }
    });
    out.push_str(ENGINE_HELP);
    out.push_str(EXIT_CLASS_HELP);
    out.push_str(OUTPUT_HELP);
    out
}

// `mahbot audio` — the process exit code. Help goes to stdout and the caller's misuse is answered
// from the words alone. The two operations themselves live in [`engine`].

pub async fn run(args: &[String]) -> i32 {
    match parse(args) {
        Ok(Invocation::Help) => {
            print_stdout(&top_help());
            Exit::Ok.code()
        }
        Ok(Invocation::OperationHelp(operation)) => {
            print_stdout(&operation_help(operation));
            Exit::Ok.code()
        }
        Ok(Invocation::Transcribe { input }) => engine::transcribe(&input).await,
        Ok(Invocation::Speak(request)) => engine::speak(&request).await,
        Err(reason) => misuse(&reason),
    }
}

/// Refuse what was asked for as the caller's own mistake: his words cannot mean it, or cannot be
/// done as asked.
fn misuse(reason: &str) -> i32 {
    print_stderr(&format!("mahbot audio: {reason}"));
    print_stderr("run 'mahbot audio -h' for usage.");
    Exit::Usage.code()
}

mod engine {
    use super::{Speak, misuse};
    use crate::cli::Exit;
    use crate::speech::{
        INFERENCE_TIMEOUT, InferenceTimedOut, Sound, ensure_ready_once, sound_of_file, synth,
        transcribe_file_async,
    };
    use crate::util::{AUDIO_EXTENSIONS, print_stderr, print_stdout};
    use std::io::ErrorKind;
    use std::path::{Path, PathBuf};

    /// How many names to try before giving up on picking a free one: the chosen name is a fresh
    /// identifier, so the first is free for any practical purpose — the loop makes "never collides"
    /// true in principle too.
    const CHOSEN_NAME_TRIES: usize = 3;

    /// What the caller handed over, judged before anything is fetched: a typo must not cost a model
    /// download. `Err` is the exit class of the refusal.
    pub(super) fn judge(input: &Path) -> Result<(), i32> {
        // The type is read before anything is opened, because opening is what waits — a FIFO named
        // like a recording would hold it until somebody writes into it.
        match std::fs::metadata(input) {
            Ok(metadata) if metadata.is_file() => {}
            // A directory has metadata too, and is not a recording either.
            Ok(_) => {
                return Err(misuse(&format!(
                    "'{}' is not a file to transcribe",
                    input.display()
                )));
            }
            Err(e) => return Err(misuse(&cannot_read(input, &e))),
        }
        // The recogniser's own rule, asked of the recogniser itself: what the file's bytes are,
        // and, for bytes that name no codec, a name the media path reads as a sound. Opening is
        // part of asking, so an unreadable input is the caller's mistake, not a failed operation.
        let sound = match sound_of_file(input) {
            Err(e) => return Err(misuse(&cannot_read(input, &e))),
            Ok(Some(sound)) => sound,
            Ok(None) => {
                return Err(misuse(&format!(
                    "'{}' is not a recording — the product reads the names {}, and a file under \
                     another name only when its content is one of those containers",
                    input.display(),
                    listed(AUDIO_EXTENSIONS.iter().map(|name| format!("'.{name}'"))),
                )));
            }
        };
        // A sound this machine has no decoder for is read through the product's own copy of ffmpeg,
        // and this command never installs one: a missing copy is refused before the models are
        // fetched for a file that could not be read.
        if sound == Sound::Managed && crate::ffmpeg::installed().is_none() {
            print_stderr(&format!(
                "mahbot audio: '{}' is a sound this machine has no decoder for, and the product's \
                 media toolchain is not installed here — the file cannot be read, and nothing was \
                 transcribed",
                input.display()
            ));
            return Err(Exit::Environment.code());
        }
        Ok(())
    }

    /// `transcribe`: the recording into text on standard output.
    pub(super) async fn transcribe(input: &Path) -> i32 {
        if let Err(code) = judge(input) {
            return code;
        }

        if let Err(e) = ensure_ready_once(|dir| {
            announce_fetch(dir, "recognition");
        })
        .await
        {
            print_stderr(&format!(
                "mahbot audio: the recognition models cannot be made ready: {e:#}"
            ));
            return Exit::Environment.code();
        }

        match transcribe_file_async(input, INFERENCE_TIMEOUT).await {
            Ok(text) => {
                // No speech found is an answer, not a failure: nothing at all on stdout, so a
                // script can tell it from an empty line.
                let text = text.trim();
                if !text.is_empty() {
                    print_stdout(text);
                }
                Exit::Ok.code()
            }
            // The bound is told by the TYPE the failure carries, never by its wording.
            Err(e) if e.downcast_ref::<InferenceTimedOut>().is_some() => {
                print_stderr(&format!(
                    "mahbot audio: the recording is longer than the local recogniser will take — \
                     it reached the engine's own {}-minute inference bound, and nothing was \
                     transcribed rather than a transcript with a part of it missing",
                    INFERENCE_TIMEOUT.as_secs() / 60,
                ));
                Exit::Failed.code()
            }
            Err(e) => {
                print_stderr(&format!(
                    "mahbot audio: the recording could not be transcribed: {e:#}"
                ));
                Exit::Failed.code()
            }
        }
    }

    /// `speak`: the text into a spoken file, where the caller says to write it.
    pub(super) async fn speak(request: &Speak) -> i32 {
        // What the caller asked for, before anything is fetched.
        if request.text.trim().is_empty() {
            return misuse("the text to speak says nothing — it is empty or only whitespace");
        }
        let destination = match resolve_destination(&request.output) {
            Ok(destination) => destination,
            Err(reason) => return misuse(&reason),
        };

        // The set is obtained before the reading: a failure here is the environment's.
        if let Err(e) = synth::ensure_ready_once(|dir| {
            announce_fetch(dir, "speech synthesis");
        })
        .await
        {
            print_stderr(&format!(
                "mahbot audio: the speech synthesis model cannot be obtained: {e:#}"
            ));
            return Exit::Environment.code();
        }

        // The whole reading is rendered before anything is written, so a text that cannot be spoken
        // in full leaves nothing at the destination.
        let wav = match synth::render_speech_wav(&request.text).await {
            Ok(wav) => wav,
            Err(e) => {
                print_stderr(&format!(
                    "mahbot audio: nothing was written — the text could not be spoken: {e:#}"
                ));
                return Exit::Failed.code();
            }
        };

        match write_audio(&destination, &wav) {
            Ok(Some(path)) => {
                print_stdout(&path.display().to_string());
                Exit::Ok.code()
            }
            Ok(None) => Exit::Ok.code(),
            Err(reason) => misuse(&reason),
        }
    }

    /// Say on standard error that a model set is being fetched — a command line has only its
    /// standard error. Said as a fetch begins (before the first byte), and only when one begins at
    /// all: a set already on disk is silent, a first use that downloads gigabytes is not.
    fn announce_fetch(dir: &Path, what: &str) {
        print_stderr(&format!(
            "mahbot audio: fetching the {what} model set into {} — fetched once, and only when it \
             is not already there",
            dir.display()
        ));
    }

    /// The refusal for an input that cannot be read at all — the same whether the type probe or the
    /// open found it.
    fn cannot_read(path: &Path, e: &std::io::Error) -> String {
        format!("'{}' cannot be read: {e}", path.display())
    }

    /// Where the spoken file goes: the path the caller named, or a fresh name this command picks
    /// inside a directory he named.
    pub(super) enum Destination {
        /// The caller named the file itself.
        Named(PathBuf),
        /// The caller named a directory; the name is chosen when the file is created, so it cannot
        /// collide.
        Chosen(PathBuf),
    }

    /// Read what `--output` names, before anything is fetched or rendered. The name is also where
    /// the caller says which container he wants, and uncompressed WAV is the only one the product
    /// writes. The destination directory is the caller's to make: this command never creates one.
    pub(super) fn resolve_destination(output: &Path) -> Result<Destination, String> {
        if output.is_dir() {
            return Ok(Destination::Chosen(output.to_path_buf()));
        }
        // A name with an extension names a container, and uncompressed WAV is the only one the
        // product writes. Absence is not a reason here — every new destination is absent — so a
        // container this command cannot write is refused for what it is, whether or not something
        // of that name is already there.
        if output
            .extension()
            .is_some_and(|extension| !extension.eq_ignore_ascii_case("wav"))
        {
            return Err(format!(
                "'{}' is not a file this command can write — the spoken audio is uncompressed \
                 WAV, so name it '….wav', or name a directory to have one named for you",
                output.display()
            ));
        }
        // Nothing after a dot and nothing at that path: the caller named the directory he expected
        // to find, and this command creates none.
        if output.extension().is_none() && !output.exists() {
            return Err(format!(
                "'{}' is not there — this command creates nothing, so name a '.wav' file in a \
                 directory that is already there, or an existing directory to have one named for \
                 you",
                output.display()
            ));
        }
        if let Some(parent) = output.parent()
            && !parent.as_os_str().is_empty()
            && !parent.is_dir()
        {
            // Why it cannot be used is what the caller needs to hear: a parent that is there and is
            // not a directory is not a missing one.
            return Err(if parent.exists() {
                format!(
                    "'{}' cannot be written: '{}' is not a directory, and this command does not \
                     create one",
                    output.display(),
                    parent.display()
                )
            } else {
                format!(
                    "the destination directory {} does not exist — this command does not create it",
                    parent.display()
                )
            });
        }
        if output.exists() {
            return Err(already_exists(output));
        }
        Ok(Destination::Named(output.to_path_buf()))
    }

    /// The refusal for a destination already taken, said the same way whether the name was checked
    /// before the work or lost the race at the create.
    fn already_exists(path: &Path) -> String {
        format!(
            "{} already exists — this command never overwrites a file",
            path.display()
        )
    }

    /// The refusal for audio that could not be written — the same whether the caller named the file
    /// or this command chose the name.
    fn cannot_write(path: &Path, e: &std::io::Error) -> String {
        format!("{} could not be written: {e}", path.display())
    }

    /// Write the rendered audio where the destination points, returning the path when this command
    /// picked the name.
    fn write_audio(destination: &Destination, wav: &[u8]) -> Result<Option<PathBuf>, String> {
        match destination {
            Destination::Named(path) => match write_new(path, wav) {
                Ok(()) => Ok(None),
                Err(e) if e.kind() == ErrorKind::AlreadyExists => Err(already_exists(path)),
                Err(e) => Err(cannot_write(path, &e)),
            },
            Destination::Chosen(dir) => {
                for _ in 0..CHOSEN_NAME_TRIES {
                    let path = dir.join(format!("mahbot-speech-{}.wav", crate::generate_id()));
                    match write_new(&path, wav) {
                        Ok(()) => return Ok(Some(path)),
                        // The name was taken between choosing and creating: another name is one
                        // call away.
                        Err(e) if e.kind() == ErrorKind::AlreadyExists => {}
                        Err(e) => return Err(cannot_write(&path, &e)),
                    }
                }
                Err(format!("no free name could be picked in {}", dir.display()))
            }
        }
    }

    /// Create `path` and write the audio into it. `create_new` makes "never overwritten" true even
    /// against a file that appeared a moment ago, and a failed write removes what it created — a
    /// failure leaves nothing behind.
    fn write_new(path: &Path, wav: &[u8]) -> std::io::Result<()> {
        use std::io::Write as _;
        let mut file = std::fs::OpenOptions::new()
            .write(true)
            .create_new(true)
            .open(path)?;
        if let Err(e) = file.write_all(wav) {
            drop(file);
            let _ = std::fs::remove_file(path);
            return Err(e);
        }
        Ok(())
    }

    /// A list of names as a sentence reads them: `a, b or c`, and just `a` for one.
    fn listed<I, S>(names: I) -> String
    where
        I: IntoIterator<Item = S>,
        S: AsRef<str>,
    {
        let names: Vec<String> = names
            .into_iter()
            .map(|name| name.as_ref().to_string())
            .collect();
        match names.split_last() {
            Some((last, [])) => last.clone(),
            Some((last, head)) => format!("{} or {last}", head.join(", ")),
            None => String::new(),
        }
    }
}

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

    /// The grammar a caller meets before he hears anything: what a flag tail means, what one that
    /// means nothing says, and which required flags the operation's own list refuses by name.
    #[test]
    fn flag_tails_and_required_flags() {
        let values = |tokens: &[&str]| match parse_flags(Operation::Speak, &owned(tokens)) {
            Ok(Tail::Values(values)) => Ok(values),
            Ok(Tail::Help) => Err("help".to_string()),
            Err(reason) => Err(reason),
        };
        assert_eq!(
            values(&["--text", "-h", "--output", "a.wav"]).unwrap(),
            vec![("text", "-h".to_string()), ("output", "a.wav".to_string())],
            "a value is taken verbatim, even one that looks like a flag"
        );
        assert_eq!(
            values(&["--text=hi", "--output=b.wav"]).unwrap(),
            vec![("text", "hi".to_string()), ("output", "b.wav".to_string())]
        );
        // Neither of the removed spellings is accepted, silently or otherwise.
        for gone in ["--voice", "--language"] {
            assert!(
                values(&[gone, "x"]).is_err_and(|r| r.contains(gone)),
                "{gone} names no flag of this operation"
            );
        }
        assert!(values(&["--nope", "x"]).is_err_and(|r| r.contains("--nope")));
        assert!(
            values(&["--text", "a", "--text", "b"]).is_err_and(|r| r.contains("more than once"))
        );
        assert!(values(&["--text"]).is_err_and(|r| r.contains("needs a value")));
        assert!(values(&["text"]).is_err_and(|r| r.contains("--flag")));
        assert_eq!(values(&["--text", "hi", "-h"]).unwrap_err(), "help");

        // A required flag that was left out is refused by name, and the operation's own list says
        // which those are.
        let invocation = |tokens: &[&str]| parse(&owned(tokens));
        assert!(invocation(&["speak", "--text", "hi"]).is_err_and(|r| r.contains("--output")));
        assert!(invocation(&["transcribe"]).is_err_and(|r| r.contains("--input")));
        assert!(invocation(&["frobnicate"]).is_err_and(|r| r.contains("unknown operation")));
        assert!(invocation(&[]).is_err(), "no operation is misuse");
    }

    fn owned(tokens: &[&str]) -> Vec<String> {
        tokens.iter().map(|token| (*token).to_string()).collect()
    }

    #[test]
    fn destination_rules() {
        use super::engine::{Destination, resolve_destination};

        let dir = tempfile::tempdir().unwrap();
        let existing = dir.path().join("taken.wav");
        std::fs::write(&existing, b"x").unwrap();

        assert!(matches!(
            resolve_destination(dir.path()),
            Ok(Destination::Chosen(_))
        ));
        assert!(matches!(
            resolve_destination(&dir.path().join("fresh.wav")),
            Ok(Destination::Named(_))
        ));
        assert!(resolve_destination(&existing).is_err_and(|r| r.contains("never overwrites")));
        // A container this command cannot write is refused for THAT, whether or not the name is
        // already there.
        std::fs::write(dir.path().join("taken.ogg"), b"x").unwrap();
        for unsupported in ["absent.ogg", "taken.ogg"] {
            assert!(
                resolve_destination(&dir.path().join(unsupported)).is_err_and(|reason| {
                    reason.contains("is not a file this command can write")
                        && !reason.contains("is not there")
                }),
                "{unsupported} names a container the product does not write, and must be refused \
                 for that whether or not it is there"
            );
        }
        // A name with nothing after a dot and nothing at that path is the directory the caller
        // expected to find.
        for absent_dir in ["no-such-dir", "no-extension"] {
            assert!(
                resolve_destination(&dir.path().join(absent_dir)).is_err_and(|reason| {
                    reason.contains("is not there") && reason.contains(".wav")
                }),
                "{absent_dir} is an absent directory, not a file name"
            );
        }
        assert!(
            resolve_destination(&dir.path().join("absent").join("out.wav"))
                .is_err_and(|r| r.contains("does not exist"))
        );
        // A parent that is there and is not a directory is refused for what it is.
        assert!(
            resolve_destination(&existing.join("out.wav")).is_err_and(|r| {
                r.contains("is not a directory") && !r.contains("does not exist")
            })
        );
    }

    /// A sound this machine has no decoder for is refused before the models are fetched for a file
    /// that could not be read, and the formats this machine reads itself are never refused for the
    /// toolchain's absence.
    #[serial_test::serial(ffmpeg_live, ffmpeg_releases)] // relocates the install root, points the release hook
    #[test]
    fn a_format_that_needs_the_toolchain_is_refused_before_any_fetch() {
        use super::engine::judge;
        use crate::cli::Exit;

        let home = tempfile::tempdir().unwrap();
        let root = home.path().join("ffmpeg");
        let _root = crate::util::test::set_env_var(crate::ffmpeg::ROOT_ENV, root.to_str());
        let _hook = crate::ffmpeg::ReleasesHook::at("http://127.0.0.1:1");
        let recording = home.path().join("note.m4a");
        std::fs::write(&recording, b"\0\0\0\x20ftypM4A \0\0\0\0").unwrap();
        assert_eq!(judge(&recording), Err(Exit::Environment.code()));
        // A recording this process decodes itself is not refused for the toolchain's absence.
        let note = home.path().join("note.mp3");
        std::fs::write(&note, b"\0\0\0\0\0\0\0\0").unwrap();
        assert_eq!(judge(&note), Ok(()));
        // A file that is not a recording at all is the caller's mistake, not a missing tool.
        let text = home.path().join("notes.txt");
        std::fs::write(&text, b"words").unwrap();
        assert_eq!(judge(&text), Err(Exit::Usage.code()));
    }
}