granite-cli 0.2.7

CLI for discovering, configuring, and launching AI workflows powered by IBM Granite models.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
pub mod capabilities;
pub mod commands;
pub mod config;
mod context;
pub mod dependency;
pub mod launchers;
pub mod models;
pub mod proxy;
pub mod registry;
pub mod session;
pub mod sources;
pub mod utils;
pub mod version {
    include!(concat!(env!("OUT_DIR"), "/version.rs"));
}
// TODO: Re-enable once rewritten -- pub mod di;
pub mod providers;

// Third Party
use alog::{MessageLevel, alog};
use clap::{CommandFactory, Parser, Subcommand};

// Local
use commands::shared::remediation::DependentsPolicy;
use commands::{
    CapabilityCommands, HardwareCommands, LauncherCommands, ModelCommands, ProviderCommands,
    SetupCommands,
};
pub use context::AppContext;
use utils::ui::{UI_REGISTRY, Ui, run_interactive_tui};

// Hoist paste macro for use in our own macros
extern crate paste;

#[derive(Parser, Debug)]
#[command(name = "granite-cli")]
#[command(about = "Universal Model Adapter with Capabilities", long_about = None)]
struct Cli {
    #[command(subcommand)]
    command: Option<Commands>,

    /// Default logging level
    #[arg(
        short,
        long,
        global = true,
        default_value = "warning",
        env = "LOG_LEVEL"
    )]
    log_level: String,
    /// Per-level overrides
    #[arg(long, global = true, default_value = "", env = "LOG_FILTERS")]
    log_filters: String,
    /// Log with json format
    #[arg(long, global = true, env = "LOG_JSON")]
    log_json: bool,
    /// Include thread ID in log lines
    #[arg(long, global = true, env = "LOG_THREAD_ID")]
    log_thread_id: bool,
}

#[derive(clap::Args, Debug)]
struct ModelWithOutput {
    /// Output format
    #[arg(short, long, global = true, default_value = "terminal", value_parser = output_formats())]
    output: String,

    #[command(subcommand)]
    subcommand: ModelSubcommands,
}

#[derive(clap::Args, Debug)]
struct CapabilityWithOutput {
    /// Output format
    #[arg(short, long, global = true, default_value = "terminal", value_parser = output_formats())]
    output: String,

    #[command(subcommand)]
    subcommand: CapabilitySubcommands,
}

#[derive(clap::Args, Debug)]
struct ProviderWithOutput {
    /// Output format
    #[arg(short, long, global = true, default_value = "terminal", value_parser = output_formats())]
    output: String,

    #[command(subcommand)]
    subcommand: ProviderSubcommands,
}

#[derive(clap::Args, Debug)]
struct LaunchWithOutput {
    /// Output format
    #[arg(short, long, global = true, default_value = "terminal", value_parser = output_formats())]
    output: String,

    /// Launcher ID to launch
    launcher_id: String,

    /// Show overlay without launching
    #[arg(long)]
    dry_run: bool,

    /// Additional arguments to pass to the launcher
    #[arg(trailing_var_arg = true)]
    args: Vec<String>,
}

#[derive(clap::Args, Debug)]
struct LauncherWithOutput {
    /// Output format
    #[arg(short, long, global = true, default_value = "terminal", value_parser = output_formats())]
    output: String,

    #[command(subcommand)]
    subcommand: LauncherSubcommands,
}

#[derive(clap::Args, Debug)]
struct InternalArgs {
    #[command(subcommand)]
    subcommand: InternalSubcommands,
}

#[derive(Subcommand, Debug)]
enum InternalSubcommands {
    /// Read stdin and write it verbatim to `output_path`. Used as a
    /// cross-platform lifecycle-hook capture target so callers don't need to
    /// rely on shell-specific stdin-redirection syntax.
    BobHookCapture {
        /// File path to write stdin's contents to.
        output_path: std::path::PathBuf,
        /// Path to the PID lockfile written by `hook::register_or_fail`.
        /// If present, the subcommand reads this file to learn the registrant's
        /// PID and verifies (via process-tree introspection) that the current
        /// process is a descendant before writing the capture file.  This
        /// prevents an unmanaged `bob` process from accidentally overwriting
        /// our capture with the wrong session_id.
        #[arg(long)]
        ancestor_lock: Option<std::path::PathBuf>,
    },
}

#[derive(Subcommand, Debug)]
enum Commands {
    /// Model management commands
    Model(ModelWithOutput),

    /// Capability management commands
    Capability(CapabilityWithOutput),

    /// Provider management commands
    Provider(ProviderWithOutput),

    /// Launcher management commands
    Launcher(LauncherWithOutput),

    /// Unified setup wizard: discover and configure providers, models, launchers,
    /// and capabilities in a single guided flow.
    Setup {
        /// Auto-detect and configure everything that can be auto-configured.
        /// Consent is implied. Uses registry defaults for all config fields.
        /// Adds to the existing configuration: an entry that is already
        /// configured is kept as it is and is not re-checked.
        #[arg(long)]
        auto: bool,

        /// Pull model weights at the end of the wizard without prompting.
        /// Mutually exclusive with --skip-pull.
        #[arg(long, conflicts_with = "skip_pull")]
        pull: bool,

        /// Skip the model weight pull step entirely.
        /// Mutually exclusive with --pull.
        #[arg(long, conflicts_with = "pull")]
        skip_pull: bool,
    },

    /// Show hardware profile and recommended precision
    Hardware,

    /// Launch a configured launcher with Granite overlay
    Launch(LaunchWithOutput),

    /// Show version information
    Version,

    /// Print a shell completion script for granite-cli to stdout
    Completions {
        /// Shell to generate the completion script for
        shell: clap_complete::Shell,
    },

    /// Internal commands used by granite-cli itself. Not for direct use.
    #[command(hide = true)]
    Internal(InternalArgs),
}

#[derive(Subcommand, Debug)]
enum ModelSubcommands {
    /// Show the catalog of all available models
    Catalog {
        /// Filter by model type
        #[arg(short, long)]
        r#type: Option<models::ModelType>,
    },

    /// List all configured models
    List {
        /// Filter by model type
        #[arg(short, long)]
        r#type: Option<models::ModelType>,
    },

    /// Search the model catalog by ID or family
    Search {
        /// Case-insensitive substring to search for
        query: String,
    },

    /// Recommend models that fit current hardware
    Recommend {
        /// Filter by model type
        #[arg(short, long)]
        r#type: Option<models::ModelType>,

        /// Configured provider id(s) to check against (comma-separated or
        /// repeatable), or "all" to skip the provider check and show every
        /// model that fits the hardware regardless of configured providers
        #[arg(short = 'p', long = "providers", value_delimiter = ',')]
        providers: Vec<String>,

        /// Show all columns, including family and full context length
        #[arg(long)]
        wide: bool,
    },

    /// Show detailed model information
    Info {
        /// Model ID
        model_id: String,
    },

    /// Interactive model setup wizard
    Setup {
        /// Catalog model type to set up (e.g. `granite-3.1-8b-instruct`, or `custom`)
        model_type: String,

        /// Nickname for this model instance. Defaults to `model_type`;
        /// pass a distinct value to configure multiple named instances of
        /// the same catalog type (e.g. two precisions of the same model
        /// against different providers, or several custom models).
        #[arg(long = "id")]
        instance_id: Option<String>,
    },

    /// Pull (download) a configured model's weights via its provider
    Pull {
        /// Model ID to pull
        model_id: String,
    },

    /// Remove a configured model instance
    Remove {
        /// Configured model ID to remove
        model_id: String,

        /// How to handle other configured instances that depend on this model.
        /// `cascade` removes them too; `refuse` aborts with a non-zero exit;
        /// `leave` removes only this model and leaves dependents broken.
        /// Omitting the flag prompts interactively, or behaves like `leave`
        /// when there is no terminal to ask.
        #[arg(long, value_name = "POLICY")]
        dependents: Option<DependentsPolicy>,
    },
}

#[derive(Subcommand, Debug)]
enum CapabilitySubcommands {
    /// Show the catalog of all available capabilities
    Catalog,

    /// List all configured capabilities
    List,

    /// Show detailed capability information
    Info {
        /// Capability ID
        capability_id: String,
    },

    /// Interactive capability setup wizard
    Setup {
        /// Catalog capability type to set up (e.g. `agent-model`)
        capability_type: String,

        /// Nickname for this capability instance. Defaults to
        /// `capability_type`; pass a distinct value to configure multiple
        /// named instances of the same catalog type (e.g. `--id chat`).
        #[arg(long = "id")]
        instance_id: Option<String>,
    },

    /// Remove a configured capability instance
    Remove {
        /// Configured capability ID to remove
        capability_id: String,

        /// How to handle other configured instances that depend on this
        /// capability. `cascade` removes them too; `refuse` aborts with a
        /// non-zero exit; `leave` removes only this capability and leaves
        /// dependents broken. Omitting the flag prompts interactively, or
        /// behaves like `leave` when there is no terminal to ask.
        #[arg(long, value_name = "POLICY")]
        dependents: Option<DependentsPolicy>,
    },
}

#[derive(Subcommand, Debug)]
enum ProviderSubcommands {
    /// Show the catalog of all available providers
    Catalog {
        /// Show all columns, including description and endpoints
        #[arg(long)]
        wide: bool,
    },

    /// List all configured providers
    List,

    /// Show detailed provider information
    Info {
        /// Provider ID
        provider_id: String,
    },

    /// Interactive provider setup wizard
    Setup {
        /// Catalog provider type to set up (e.g. `openai-compatible`)
        provider_type: String,

        /// Nickname for this provider instance. Defaults to `provider_type`;
        /// pass a distinct value to configure multiple named instances of
        /// the same catalog type (e.g. `--id ollama`, `--id lm-studio`).
        #[arg(long = "id")]
        instance_id: Option<String>,
    },

    /// Check provider health
    Health {
        /// Provider ID (optional, checks all if not specified)
        provider_id: Option<String>,
    },

    /// Remove a configured provider instance
    Remove {
        /// Configured provider instance ID to remove
        provider_id: String,

        /// How to handle other configured instances that depend on this
        /// provider. `cascade` removes them too; `refuse` aborts with a
        /// non-zero exit; `leave` removes only this provider and leaves
        /// dependents broken. Omitting the flag prompts interactively, or
        /// behaves like `leave` when there is no terminal to ask.
        #[arg(long, value_name = "POLICY")]
        dependents: Option<DependentsPolicy>,
    },
}

#[derive(Subcommand, Debug)]
enum LauncherSubcommands {
    /// Show the catalog of all available launcher types
    Catalog,

    /// List all configured launcher instances
    List,

    /// Show detailed launcher information
    Info {
        /// Launcher ID
        launcher_id: String,
    },

    /// Interactive launcher setup wizard
    Setup {
        /// Catalog launcher type to set up (e.g. `claude`)
        launcher_type: String,

        /// Nickname for this launcher instance. Defaults to `launcher_type`;
        /// pass a distinct value to configure multiple named instances of
        /// the same catalog type (e.g. `--id claude-local`).
        #[arg(long = "id")]
        instance_id: Option<String>,
    },

    /// Remove a configured launcher instance
    Remove {
        /// Configured launcher instance ID to remove
        launcher_id: String,

        /// How to handle other configured instances that depend on this
        /// launcher. `cascade` removes them too; `refuse` aborts with a
        /// non-zero exit; `leave` removes only this launcher and leaves
        /// dependents broken. Omitting the flag prompts interactively, or
        /// behaves like `leave` when there is no terminal to ask.
        #[arg(long, value_name = "POLICY")]
        dependents: Option<DependentsPolicy>,
    },
}

/// Construct the `Ui` backend for `--output`, exiting on an unrecognized
/// format. No `Ui` exists yet at this point, so this is the one place in
/// `main` that still reports via `eprintln!` rather than `ctx.ui`.
fn construct_ui(output: &str) -> Box<dyn Ui> {
    UI_REGISTRY
        .construct(output, output, &serde_json::json!({}))
        .unwrap_or_else(|_| {
            eprintln!(
                "Unknown output format '{output}'. Valid: {}",
                output_format_names().join(", ")
            );
            std::process::exit(1);
        })
}

fn construct_context(
    output: &str,
    log_level: &str,
    log_filters: &str,
    log_json: bool,
    log_thread_id: bool,
) -> AppContext {
    // Set up Ui
    let ui = construct_ui(output);
    let ui: std::sync::Arc<dyn Ui> = std::sync::Arc::from(ui);

    // Configure logging
    let formatter_kind = if log_json {
        alog::FormatterKind::Json
    } else {
        alog::FormatterKind::Pretty
    };
    let ui_arc_clone = Arc::clone(&ui);
    let ui_writer = UiWriter {
        ui: Arc::clone(&ui),
    };
    alog::configure(alog::Config {
        default_level: log_level.parse().unwrap(),
        filters: alog::Filters::Spec(log_filters.to_string()),
        formatter: alog::FormatterKind::Custom(Box::new(UiFormatter::new(
            formatter_kind,
            ui_arc_clone,
        ))),
        writer: alog::Writer::Custom(Box::new(ui_writer)),
        thread_id: log_thread_id,
    });
    alog!("MAIN", MessageLevel::Debug, "Welcome to granite-cli!");

    // Initialize config
    let config = config::Config::new().unwrap_or_else(|e| {
        ui.error(&format!("Failed to load config: {e}"));
        std::process::exit(1);
    });
    AppContext::new(config, ui)
}

/*-- private --*/

use std::io::{self, Write};
use std::sync::Arc;

/// Names of the backends registered in `UI_REGISTRY`, sorted.
fn output_format_names() -> Vec<&'static str> {
    let mut names: Vec<&'static str> = UI_REGISTRY.entries().into_keys().collect();
    names.sort_unstable();
    names
}

/// Value parser for the `--output` flag of the `model`, `capability`,
/// `provider`, `launcher` and `launch` commands. It accepts the names from
/// `output_format_names()` and gives each value the backend's
/// `UiMetadata::description` as help text, which zsh and fish show when
/// completing.
fn output_formats() -> clap::builder::PossibleValuesParser {
    let entries = UI_REGISTRY.entries();
    output_format_names()
        .into_iter()
        .map(|name| clap::builder::PossibleValue::new(name).help(entries[name].description.clone()))
        .collect::<Vec<_>>()
        .into()
}

/// Write the completion script for `shell` to `out`, generated by
/// `clap_complete::generate` from `completion_command()`.
///
/// The script is generated into a buffer first because
/// `clap_complete::generate` panics on a write error, for example when
/// stdout is a closed pipe.
///
/// For bash, `clap_complete` 4.6 writes mismatched function labels when the
/// binary name contains a hyphen (clap-rs/clap#6421), and completion stops
/// after the first word. The bash script is therefore generated for the name
/// `granite_cli`, and its `complete -F _granite_cli ...` lines are changed to
/// register the function for `granite-cli`.
fn write_completions(shell: clap_complete::Shell, out: &mut dyn Write) -> io::Result<()> {
    let mut cmd = completion_command();
    let mut buf: Vec<u8> = Vec::new();
    if shell != clap_complete::Shell::Bash {
        clap_complete::generate(shell, &mut cmd, "granite-cli", &mut buf);
        return out.write_all(&buf);
    }
    // TODO: Generate bash completions for "granite-cli" directly and remove the
    // `granite_cli` renaming once clap-rs/clap#6421 is fixed in clap_complete.
    clap_complete::generate(shell, &mut cmd, "granite_cli", &mut buf);
    let script = String::from_utf8_lossy(&buf);
    for line in script.lines() {
        match line.strip_suffix(" granite_cli") {
            Some(prefix) if line.trim_start().starts_with("complete -F _granite_cli ") => {
                writeln!(out, "{prefix} granite-cli")?
            }
            _ => writeln!(out, "{line}")?,
        }
    }
    Ok(())
}

/// Build a copy of the `Cli` command without its hidden top-level
/// subcommands (`internal`). `clap_complete::generate` does not check
/// `hide = true` on subcommands, so `internal` and `bob-hook-capture` would
/// otherwise appear in the generated scripts. The copy keeps the name,
/// about text and arguments of `Cli`, which are the only top-level settings
/// `Cli` declares.
fn completion_command() -> clap::Command {
    let full = Cli::command();
    let mut cmd = clap::Command::new("granite-cli")
        .args(full.get_arguments().cloned())
        .subcommands(full.get_subcommands().filter(|s| !s.is_hide_set()).cloned());
    if let Some(about) = full.get_about() {
        cmd = cmd.about(about.clone());
    }
    cmd
}

/// A log sink that routes formatted records through a [`Ui`] backend.
struct UiWriter {
    ui: Arc<dyn Ui>,
}

impl Write for UiWriter {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        let text = std::str::from_utf8(buf).unwrap_or("");
        // The formatter adds a trailing newline; split and route each line.
        for line in text.split('\n') {
            if line.is_empty() {
                continue;
            }
            self.ui.info(line);
        }
        Ok(buf.len())
    }

    fn flush(&mut self) -> io::Result<()> {
        Ok(())
    }
}

/// Wraps an alog formatter and delegates to it.
struct UiFormatter {
    inner: Box<dyn alog::Formatter>,
    ui: Arc<dyn Ui>,
}

impl UiFormatter {
    fn new(kind: alog::FormatterKind, ui: Arc<dyn Ui>) -> Self {
        Self {
            inner: match kind {
                alog::FormatterKind::Pretty => Box::new(alog::PrettyFormatter::default()),
                alog::FormatterKind::Json => Box::new(alog::JsonFormatter),
                alog::FormatterKind::Custom(c) => c,
            },
            ui,
        }
    }
}

impl alog::Formatter for UiFormatter {
    fn format(&self, record: &alog::LogRecord<'_>) -> String {
        let formatted = self.inner.format(record).trim_end_matches('\n').to_string();
        match record.level {
            MessageLevel::Fatal | MessageLevel::Error => self.ui.error_mark(&formatted),
            MessageLevel::Warning => self.ui.warn_mark(&formatted),
            MessageLevel::Info => formatted,
            _ => self.ui.detail_mark(&formatted),
        }
    }
}

#[tokio::main]
async fn main() {
    tokio::spawn(async {
        if tokio::signal::ctrl_c().await.is_ok() {
            let _ = dialoguer::console::Term::stderr().show_cursor();
            std::process::exit(130);
        }
    });

    let cli = Cli::parse();
    let log_level = cli.log_level.clone();
    let log_filters = cli.log_filters.clone();
    let log_json = cli.log_json;
    let log_thread_id = cli.log_thread_id;
    let command = cli.command;

    let result: Result<(), ()> = match command {
        Some(Commands::Model(wrapper)) => {
            let mut ctx = construct_context(
                &wrapper.output,
                &log_level,
                &log_filters,
                log_json,
                log_thread_id,
            );
            run_model_command(&mut ctx, wrapper.subcommand)
                .await
                .map_err(|e| ctx.ui.error(&e.to_string()))
        }
        Some(Commands::Capability(wrapper)) => {
            let mut ctx = construct_context(
                &wrapper.output,
                &log_level,
                &log_filters,
                log_json,
                log_thread_id,
            );
            run_capability_command(&mut ctx, wrapper.subcommand)
                .await
                .map_err(|e| ctx.ui.error(&e.to_string()))
        }
        Some(Commands::Provider(wrapper)) => {
            let mut ctx = construct_context(
                &wrapper.output,
                &log_level,
                &log_filters,
                log_json,
                log_thread_id,
            );
            run_provider_command(&mut ctx, wrapper.subcommand)
                .await
                .map_err(|e| ctx.ui.error(&e.to_string()))
        }
        Some(Commands::Hardware) => {
            let ctx = construct_context(
                "terminal",
                &log_level,
                &log_filters,
                log_json,
                log_thread_id,
            );
            HardwareCommands::show(&ctx).map_err(|e| ctx.ui.error(&e.to_string()))
        }
        Some(Commands::Launcher(wrapper)) => {
            let mut ctx = construct_context(
                &wrapper.output,
                &log_level,
                &log_filters,
                log_json,
                log_thread_id,
            );
            run_launcher_command(&mut ctx, wrapper.subcommand)
                .await
                .map_err(|e| ctx.ui.error(&e.to_string()))
        }
        Some(Commands::Setup {
            auto,
            pull,
            skip_pull,
        }) => {
            let pull_opt = if pull {
                Some(true)
            } else if skip_pull {
                Some(false)
            } else {
                None
            };
            let mut ctx = construct_context(
                "terminal",
                &log_level,
                &log_filters,
                log_json,
                log_thread_id,
            );
            SetupCommands::run(&mut ctx, auto, pull_opt)
                .await
                .map_err(|e| ctx.ui.error(&e.to_string()))
        }
        Some(Commands::Launch(wrapper)) => {
            let mut ctx = construct_context(
                &wrapper.output,
                &log_level,
                &log_filters,
                log_json,
                log_thread_id,
            );
            run_launch(
                &mut ctx,
                &wrapper.launcher_id,
                &wrapper.args,
                wrapper.dry_run,
            )
            .await
            .map_err(|e| ctx.ui.error(&e.to_string()))
        }
        Some(Commands::Version) => {
            // Version info is simple text — no UI backend or config needed.
            println!("{}", version::version_string());
            Ok(())
        }
        Some(Commands::Completions { shell }) => {
            // No UI backend or config needed: the script is built from the
            // clap definition of `Cli` alone. A closed stdout pipe (for
            // example `granite-cli completions bash | head`) is not reported.
            match write_completions(shell, &mut std::io::stdout()) {
                Err(e) if e.kind() != std::io::ErrorKind::BrokenPipe => {
                    eprintln!("Error writing completion script: {e}");
                    Err(())
                }
                _ => Ok(()),
            }
        }
        Some(Commands::Internal(args)) => match args.subcommand {
            InternalSubcommands::BobHookCapture {
                output_path,
                ancestor_lock,
            } => {
                use std::io::Read;
                let mut buf = Vec::new();
                std::io::stdin()
                    .read_to_end(&mut buf)
                    .map_err(|e| eprintln!("Error reading stdin: {e}"))
                    .and_then(|_| {
                        // Ancestry gating: if this capture doesn't belong to
                        // the process that registered the hook, skip writing
                        // it. Exit 0 either way (never error: Bob's
                        // SessionStart hooks are fire-and-forget).
                        if !bob_hook_capture_should_write(ancestor_lock.as_deref()) {
                            return Ok(());
                        }

                        std::fs::write(&output_path, &buf)
                            .map_err(|e| eprintln!("Error writing {}: {e}", output_path.display()))
                    })
            }
        },
        None => {
            // `ctx` (and its `ui`) is consumed by value into the TUI `App`
            // before any error can occur, so it can't be used to report one.
            let ctx = construct_context(
                "terminal",
                &log_level,
                &log_filters,
                log_json,
                log_thread_id,
            );
            run_interactive_tui(ctx, log_level.parse().unwrap(), &log_filters)
                .await
                .map_err(|e| eprintln!("Error: {e}"))
        }
    };

    if result.is_err() {
        std::process::exit(1);
    }
}

async fn run_model_command(ctx: &mut AppContext, subcmd: ModelSubcommands) -> anyhow::Result<()> {
    match subcmd {
        ModelSubcommands::Catalog { r#type } => ModelCommands::catalog(ctx, r#type),
        ModelSubcommands::List { r#type } => ModelCommands::list(ctx, r#type),
        ModelSubcommands::Search { query } => ModelCommands::search(ctx, &query),
        ModelSubcommands::Recommend {
            r#type,
            providers,
            wide,
        } => ModelCommands::recommend(ctx, r#type, &providers, wide),
        ModelSubcommands::Info { model_id } => ModelCommands::info(ctx, &model_id).await,
        ModelSubcommands::Setup {
            model_type,
            instance_id,
        } => ModelCommands::setup(ctx, &model_type, instance_id.as_deref(), false).await,
        ModelSubcommands::Pull { model_id } => ModelCommands::pull(ctx, &model_id).await,
        ModelSubcommands::Remove {
            model_id,
            dependents,
        } => ModelCommands::remove(ctx, &model_id, dependents),
    }
}

async fn run_capability_command(
    ctx: &mut AppContext,
    subcmd: CapabilitySubcommands,
) -> anyhow::Result<()> {
    match subcmd {
        CapabilitySubcommands::Catalog => CapabilityCommands::catalog(ctx),
        CapabilitySubcommands::List => CapabilityCommands::list(ctx),
        CapabilitySubcommands::Info { capability_id } => {
            CapabilityCommands::info(ctx, &capability_id).await
        }
        CapabilitySubcommands::Setup {
            capability_type,
            instance_id,
        } => CapabilityCommands::setup(ctx, &capability_type, instance_id.as_deref(), false).await,
        CapabilitySubcommands::Remove {
            capability_id,
            dependents,
        } => CapabilityCommands::remove(ctx, &capability_id, dependents),
    }
}

async fn run_provider_command(
    ctx: &mut AppContext,
    subcmd: ProviderSubcommands,
) -> anyhow::Result<()> {
    match subcmd {
        ProviderSubcommands::Catalog { wide } => ProviderCommands::catalog(ctx, wide),
        ProviderSubcommands::List => ProviderCommands::list(ctx),
        ProviderSubcommands::Info { provider_id } => ProviderCommands::info(ctx, &provider_id),
        ProviderSubcommands::Setup {
            provider_type,
            instance_id,
        } => ProviderCommands::setup(ctx, &provider_type, instance_id.as_deref(), false).await,
        ProviderSubcommands::Health { provider_id } => {
            ProviderCommands::health(ctx, provider_id.as_deref()).await
        }
        ProviderSubcommands::Remove {
            provider_id,
            dependents,
        } => ProviderCommands::remove(ctx, &provider_id, dependents),
    }
}

async fn run_launcher_command(
    ctx: &mut AppContext,
    subcmd: LauncherSubcommands,
) -> anyhow::Result<()> {
    match subcmd {
        LauncherSubcommands::Catalog => LauncherCommands::catalog(ctx),
        LauncherSubcommands::List => LauncherCommands::list(ctx),
        LauncherSubcommands::Info { launcher_id } => LauncherCommands::info(ctx, &launcher_id),
        LauncherSubcommands::Setup {
            launcher_type,
            instance_id,
        } => LauncherCommands::setup(ctx, &launcher_type, instance_id.as_deref(), false).await,
        LauncherSubcommands::Remove {
            launcher_id,
            dependents,
        } => LauncherCommands::remove(ctx, &launcher_id, dependents),
    }
}

pub(crate) async fn run_launch(
    ctx: &mut AppContext,
    launcher_id: &str,
    args: &[String],
    dry_run: bool,
) -> anyhow::Result<()> {
    use crate::capabilities::CAPABILITY_REGISTRY;
    use crate::launchers::LAUNCHER_REGISTRY;
    use crate::launchers::LaunchContext;
    use crate::proxy::ProxyServer;
    use crate::session;

    // Load config fresh so we always pick up the latest saved state.
    ctx.set_config(crate::config::Config::new()?);

    // Configuration integrity first, before anything about the environment.
    LauncherCommands::prelaunch(ctx, launcher_id).await?;

    // A handle rather than a borrow of `ctx`: starting the session proxy
    // below takes `ctx` mutably, to point its sources at the proxy.
    let ui = std::sync::Arc::clone(&ctx.ui);
    let ui: &dyn Ui = ui.as_ref();

    let lc = ctx
        .config()
        .get_launcher(launcher_id)
        .ok_or_else(|| {
            anyhow::anyhow!(
                "No launcher configured with id '{launcher_id}'. \
                 Run `granite-cli launcher setup {launcher_id}` first."
            )
        })?
        .clone();

    // The session proxy boots for every non-dry-run launch. Usage tracking is
    // always on: the proxy's tracker accumulates stats throughout the session
    // and writes them to a persistent session file so they survive even an
    // abrupt ctrl-c.
    //
    // The `claude` launcher additionally requires the proxy for sub-agent
    // routing when any `BindingType::SubAgent` capability is enabled: it has
    // exactly one `ANTHROPIC_BASE_URL` for the whole session, so every
    // sub-agent's model must be multiplexed through the mini-router.
    // `opencode` (and any other launcher that later gains `BindingType::SubAgent`
    // support) configures each sub-agent's own provider directly in its
    // multi-provider config, so it never needs this path -- but the proxy still
    // boots for usage tracking.
    //
    // Skipped entirely under `dry_run`: there is no subprocess to point a
    // proxy at, and showing the real upstream URL in the overlay is more
    // useful than a not-yet-running one. When booted, it goes on the
    // application context, so every provider its sources hand out from then
    // on points at the proxy and a capability resolved against them is
    // routed through it, and tracked when a tracker is active.
    let boot_proxy = !dry_run;
    let proxy_server = if boot_proxy {
        Some(ProxyServer::start()?)
    } else {
        None
    };
    if let Some(server) = &proxy_server {
        // The set built before this point pointed at the real upstreams, so
        // it goes: from here every provider handed out points at the proxy,
        // and so does every capability resolved against it below.
        ctx.set_model_proxy(server.handle.clone());
    }
    let model_proxy = proxy_server.as_ref().map(|s| s.handle.clone());
    let sources = ctx.sources();

    // Compute the tracker early so the LaunchContext can hold it (e.g. for Bob,
    // which has no model-configuration capability and so never makes a request
    // the proxy could intercept). Reused at the writer-task setup below.
    let tracker = proxy_server.as_ref().map(|s| s.handle.tracker());

    // Build capability configs with their dependencies for session metadata
    // before consuming them in the binding loop below.
    let capabilities_with_deps: Vec<(
        crate::config::CapabilityConfig,
        Vec<crate::capabilities::Dependency>,
    )> = lc
        .enabled_capabilities
        .iter()
        .filter_map(|id| {
            let cap_cfg = ctx.config().get_capability(id)?;
            let cap_meta = CAPABILITY_REGISTRY.get(&cap_cfg.capability_type)?;
            Some((cap_cfg.clone(), cap_meta.dependencies.clone()))
        })
        .collect();

    // Generate a unique session ID and write the initial session file (empty
    // usage). Best-effort: a write failure must not prevent the session from
    // starting.
    let session_id = session::generate_session_id();
    let session_meta =
        session::create_session_meta(&session_id, ctx.config(), &lc, &capabilities_with_deps);
    session::write_session_file(&session_meta).ok();

    let mut launcher = LAUNCHER_REGISTRY
        .construct(&lc.launcher_type, &lc.launcher_id, &lc.config)
        .map_err(|e| anyhow::anyhow!("Failed to construct launcher: {e}"))?;

    let launch_ctx = LaunchContext {
        launcher_id: launcher_id.to_string(),
        working_dir: std::env::current_dir()?,
        base_env: std::collections::HashMap::new(),
        dry_run,
        usage_tracker: tracker.clone(),
        model_proxy: model_proxy.clone(),
    };

    // Every enabled capability resolves before the first one binds, so a
    // capability that cannot be built leaves the launcher untouched. What
    // resolved is kept alive past this point, so a capability that owns a
    // process-scoped resource -- e.g. `VisionMCPCapability`'s in-process MCP
    // server -- survives long enough for `on_shutdown` to tear it down after
    // the launched process exits, not before it starts.
    let bound_capabilities = crate::launchers::resolve_and_bind(
        launcher.as_mut(),
        &lc.enabled_capabilities,
        &sources.capabilities(),
    )
    .await?;

    // One route per model the bound capabilities name, registered before
    // anything is launched at them. The target is the real upstream, read
    // through the source's upstream view, which the swap above does not hide.
    if let Some(server) = &proxy_server {
        crate::proxy::register_proxy_routes(ctx, &lc.enabled_capabilities, &server.handle, ui);
    }

    for capability in &bound_capabilities {
        capability.on_pre_launch(&launch_ctx).await?;
    }

    // Set up reactive session-file flushing. A watch channel acts as a
    // single-element overwrite queue: the proxy's UsageTracker fires the
    // sender (non-blocking) on every record() call; the background writer
    // task blocks on changed() and flushes to disk whenever a new value
    // arrives. Rapid successive records coalesce into one write because watch
    // stores only the latest notification — the writer is never on the
    // response-to-client critical path.
    // `tracker` was computed earlier so the LaunchContext can hold it; reuse here.
    let writer_handle = if let Some(ref t) = tracker {
        let (tx, mut rx) = tokio::sync::watch::channel(());
        t.set_notifier(tx);
        let writer_session_id = session_id.clone();
        let writer_tracker = t.clone();
        Some(tokio::spawn(async move {
            loop {
                // Block until a record() fires the notifier, then write once.
                if rx.changed().await.is_err() {
                    // Sender dropped — session is shutting down.
                    break;
                }
                let snapshot = writer_tracker.snapshot();
                session::update_session_usage(&writer_session_id, &snapshot).ok();
            }
        }))
    } else {
        None
    };

    let launch_result = launcher.launch(args, &launch_ctx, ui).await;

    // Run post-launch/shutdown hooks regardless of how the launch went, so a
    // capability's background resources (e.g. an in-process MCP server) are
    // always torn down. Failures here are reported, not propagated -- the
    // launch itself already succeeded or failed on its own terms.
    for capability in bound_capabilities.iter().rev() {
        if let Err(e) = capability.on_post_launch(&launch_ctx).await {
            ui.warn(&format!(
                "on_post_launch failed for capability '{}': {e}",
                capability.instance_id()
            ));
        }
        if let Err(e) = capability.on_shutdown(&launch_ctx).await {
            ui.warn(&format!(
                "on_shutdown failed for capability '{}': {e}",
                capability.instance_id()
            ));
        }
    }

    // Shut down the background writer: dropping the tracker's notifier sender
    // (by dropping `tracker` after the final flush) will cause rx.changed() to
    // return Err and the writer task to exit its loop naturally. We abort here
    // as a belt-and-suspenders measure to avoid a dangling task during proxy
    // shutdown, then do a final flush with the definitive usage snapshot.
    if let Some(handle) = writer_handle {
        handle.abort();
    }
    if let Some(ref t) = tracker {
        session::finish_session(&session_id, &t.snapshot()).ok();
    }

    let status = launch_result?;

    if let Some(server) = proxy_server {
        print_usage_summary(ui, &server.handle.tracker());
        server.shutdown().await;
    }

    if !status.success() {
        anyhow::bail!(
            "'{}' exited with status {}",
            launcher_id,
            status.code().unwrap_or(-1)
        );
    }
    Ok(())
}

/// Print a per-binding + total usage table, skipped entirely if nothing was
/// recorded (e.g. the launched agent never made a request).
fn print_usage_summary(ui: &dyn Ui, tracker: &proxy::UsageTracker) {
    let snapshot = tracker.snapshot();
    if snapshot.is_empty() {
        return;
    }

    let mut rows: Vec<Vec<String>> = snapshot
        .iter()
        .map(|(label, s)| {
            vec![
                label.clone(),
                s.requests.to_string(),
                s.input_tokens.to_string(),
                s.output_tokens.to_string(),
                s.cache_creation_tokens.to_string(),
                s.cache_read_tokens.to_string(),
            ]
        })
        .collect();
    rows.sort_by(|a, b| a[0].cmp(&b[0]));

    let total = snapshot
        .values()
        .fold(proxy::UsageStats::default(), |mut acc, s| {
            acc.requests += s.requests;
            acc.input_tokens += s.input_tokens;
            acc.output_tokens += s.output_tokens;
            acc.cache_creation_tokens += s.cache_creation_tokens;
            acc.cache_read_tokens += s.cache_read_tokens;
            acc
        });
    rows.push(vec![
        "Total".to_string(),
        total.requests.to_string(),
        total.input_tokens.to_string(),
        total.output_tokens.to_string(),
        total.cache_creation_tokens.to_string(),
        total.cache_read_tokens.to_string(),
    ]);

    ui.table(
        "Usage",
        &[
            "Binding",
            "Requests",
            "Input Tokens",
            "Output Tokens",
            "Cache Write",
            "Cache Read",
        ],
        &rows,
    );
}

/// Check whether the `BobHookCapture` ancestry gating allows writing the
/// capture file. `ancestor_lock`, when present, points at the lockfile
/// written by `hook::register_or_fail` containing the registering process's
/// PID; if the current process isn't a descendant of that PID, the capture
/// belongs to some other (unmanaged, or a different granite-cli instance's)
/// `bob` session and must not be written. Every failure mode (no lock,
/// unreadable/unparseable lock, inconclusive ancestry lookup) proceeds to
/// write — this check exists only to close a narrow race, never to become a
/// new source of false negatives.
///
/// Shared by the real dispatch handler below and its unit tests, so the
/// tested logic is exactly what runs in production.
fn bob_hook_capture_should_write(ancestor_lock: Option<&std::path::Path>) -> bool {
    if let Some(lock_path) = ancestor_lock {
        let lock_content = match std::fs::read_to_string(lock_path) {
            Ok(c) => c,
            Err(_) => return true, // best-effort: can't read → proceed
        };
        let registrant_pid: u32 = match lock_content.trim().parse() {
            Ok(p) => p,
            Err(_) => return true, // best-effort: can't parse → proceed
        };

        if registrant_pid == 0 {
            return true; // no PID → proceed (no gating)
        }

        match crate::launchers::bob::hook::is_process_ancestor(registrant_pid, 16) {
            Some(false) => false,      // definitely not a descendant → skip
            Some(true) | None => true, // descendant or inconclusive → write
        }
    } else {
        true // no lock → always write
    }
}

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

    #[test]
    fn gating_skips_when_lock_pid_not_ancestor() {
        let tmp = tempfile::tempdir().unwrap();
        let lock_file = tmp.path().join("lock");
        // Write a PID that's definitely not our ancestor.
        std::fs::write(&lock_file, "999999999").unwrap();

        assert!(!bob_hook_capture_should_write(Some(&lock_file)));
    }

    #[test]
    fn gating_allows_when_no_lock_path() {
        assert!(bob_hook_capture_should_write(None));
    }

    #[test]
    fn gating_allows_when_lock_unreadable() {
        // Path doesn't exist → best-effort read fails → proceed.
        assert!(bob_hook_capture_should_write(Some(std::path::Path::new(
            "/no/such/file.lock"
        ))));
    }

    #[test]
    fn gating_allows_when_lock_contains_non_numeric() {
        let tmp = tempfile::tempdir().unwrap();
        let lock_file = tmp.path().join("lock");
        std::fs::write(&lock_file, "not-a-pid").unwrap();

        assert!(bob_hook_capture_should_write(Some(&lock_file)));
    }

    #[test]
    fn gating_allows_when_lock_pid_is_current_process() {
        let tmp = tempfile::tempdir().unwrap();
        let lock_file = tmp.path().join("lock");
        std::fs::write(&lock_file, std::process::id().to_string()).unwrap();

        assert!(bob_hook_capture_should_write(Some(&lock_file)));
    }

    #[test]
    fn gating_allows_when_lock_pid_zero() {
        let tmp = tempfile::tempdir().unwrap();
        let lock_file = tmp.path().join("lock");
        std::fs::write(&lock_file, "0").unwrap();

        assert!(bob_hook_capture_should_write(Some(&lock_file)));
    }
}

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

    use clap::ValueEnum;
    use clap_complete::Shell;

    fn completions_for(shell: Shell) -> String {
        let mut buf: Vec<u8> = Vec::new();
        write_completions(shell, &mut buf).unwrap();
        String::from_utf8(buf).unwrap()
    }

    #[test]
    fn completions_include_subcommands_for_every_shell() {
        for shell in Shell::value_variants() {
            let script = completions_for(*shell);
            assert!(!script.is_empty(), "empty script for {shell}");
            assert!(script.contains("model"), "`model` missing for {shell}");
            assert!(
                script.contains("completions"),
                "`completions` missing for {shell}"
            );
        }
    }

    #[test]
    fn completions_exclude_hidden_internal_subcommands() {
        for shell in Shell::value_variants() {
            let script = completions_for(*shell);
            assert!(
                !script.contains("bob-hook-capture"),
                "`bob-hook-capture` present for {shell}"
            );
            assert!(
                !script.contains("Internal commands used by granite-cli"),
                "`internal` present for {shell}"
            );
        }
    }

    #[test]
    fn completion_command_matches_cli_name_and_about() {
        let full = Cli::command();
        let cmd = completion_command();
        assert_eq!(cmd.get_name(), full.get_name());
        assert_eq!(
            cmd.get_about().map(|a| a.to_string()),
            full.get_about().map(|a| a.to_string())
        );
    }

    #[test]
    fn only_top_level_subcommands_are_hidden() {
        // `completion_command` removes hidden subcommands at the top level
        // only. This fails if a nested subcommand is marked `hide = true`.
        fn assert_no_hidden(cmd: &clap::Command) {
            for sub in cmd.get_subcommands() {
                assert!(
                    !sub.is_hide_set(),
                    "nested subcommand `{}` is hidden",
                    sub.get_name()
                );
                assert_no_hidden(sub);
            }
        }
        for sub in Cli::command().get_subcommands() {
            assert_no_hidden(sub);
        }
    }

    #[test]
    fn bash_completions_register_granite_cli() {
        let script = completions_for(Shell::Bash);
        let registrations: Vec<&str> = script
            .lines()
            .map(str::trim)
            .filter(|l| l.starts_with("complete -F "))
            .collect();
        assert!(!registrations.is_empty());
        for line in registrations {
            assert!(line.starts_with("complete -F _granite_cli "), "{line}");
            assert!(line.ends_with(" granite-cli"), "{line}");
        }
    }

    #[test]
    fn bash_completions_have_a_case_for_every_subcommand_label() {
        // Each `cmd="<label>"` set while walking the command words must have
        // a matching `<label>)` arm, or completion stops at that word
        // (clap-rs/clap#6421).
        let script = completions_for(Shell::Bash);
        let labels: Vec<&str> = script
            .lines()
            .filter_map(|l| l.trim().strip_prefix("cmd=\""))
            .filter_map(|l| l.strip_suffix('"'))
            .filter(|l| !l.is_empty())
            .collect();
        assert!(labels.contains(&"granite_cli__subcmd__model"));
        for label in labels {
            assert!(
                script.contains(&format!("\n        {label})\n")),
                "no case arm for {label}"
            );
        }
    }

    #[test]
    fn completions_include_output_format_and_model_type_values() {
        let script = completions_for(Shell::Bash);
        for name in output_format_names() {
            assert!(script.contains(name), "output format `{name}` missing");
        }
        assert!(
            script.contains("embedding"),
            "model type `embedding` missing"
        );
    }

    #[test]
    fn output_formats_match_ui_registry() {
        let mut expected: Vec<&str> = UI_REGISTRY.entries().into_keys().collect();
        expected.sort_unstable();
        assert_eq!(output_format_names(), expected);
        let cli =
            Cli::try_parse_from(["granite-cli", "model", "--output", "json", "list"]).unwrap();
        assert!(matches!(cli.command, Some(Commands::Model(w)) if w.output == "json"));
        assert!(Cli::try_parse_from(["granite-cli", "model", "--output", "yaml", "list"]).is_err());
    }

    #[test]
    fn model_type_flag_parses_into_model_type() {
        let cli =
            Cli::try_parse_from(["granite-cli", "model", "catalog", "--type", "vision"]).unwrap();
        let Some(Commands::Model(w)) = cli.command else {
            panic!("expected model command");
        };
        assert!(matches!(
            w.subcommand,
            ModelSubcommands::Catalog {
                r#type: Some(models::ModelType::Vision)
            }
        ));
        assert!(
            Cli::try_parse_from(["granite-cli", "model", "catalog", "--type", "audio"]).is_err()
        );
    }

    #[test]
    fn completions_command_parses_shell() {
        let cli = Cli::try_parse_from(["granite-cli", "completions", "bash"]).unwrap();
        assert!(matches!(
            cli.command,
            Some(Commands::Completions { shell: Shell::Bash })
        ));
    }

    #[test]
    fn completions_command_rejects_unknown_shell() {
        assert!(Cli::try_parse_from(["granite-cli", "completions", "tcsh"]).is_err());
    }
}

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

    use crate::utils::ui::base::tests::CaptureUi;

    #[tokio::test]
    async fn run_launch_unknown_launcher_returns_err() {
        let _home = crate::config::TestConfigHome::new();
        let mut ctx = AppContext::new(
            crate::config::Config::default(),
            Arc::new(CaptureUi::default()),
        );

        let result = run_launch(&mut ctx, "no-such-launcher", &[], true).await;
        assert!(result.is_err());

        let err = result.unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("Launch aborted"), "unexpected error: {msg}");
        assert!(msg.contains("no-such-launcher"), "unexpected error: {msg}");
    }
}